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 37 2e 31 35 2e 20 20 ersion 3.7.15.
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 73 69 6e 67 6c 65 20 74 72 61 6e 73 6c a single transl
0130: 61 74 69 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 ation.** unit.
0140: 54 68 69 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 This allows many
0150: 20 63 6f 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f compilers to do
0160: 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 optimizations t
0170: 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 hat would not be
0180: 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 20 69 66 20 .** possible if
0190: 74 68 65 20 66 69 6c 65 73 20 77 65 72 65 20 63 the files were c
01a0: 6f 6d 70 69 6c 65 64 20 73 65 70 61 72 61 74 65 ompiled separate
01b0: 6c 79 2e 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 ly. Performance
01c0: 20 69 6d 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a improvements.**
01d0: 20 6f 66 20 35 25 20 6f 72 20 6d 6f 72 65 20 61 of 5% or more a
01e0: 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e re commonly seen
01f0: 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 when SQLite is
0200: 63 6f 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 compiled as a si
0210: 6e 67 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 ngle.** translat
0220: 69 6f 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 ion unit..**.**
0230: 54 68 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c This file is all
0240: 20 79 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d you need to com
0250: 70 69 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f pile SQLite. To
0260: 20 75 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f use SQLite in o
0270: 74 68 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 ther.** programs
0280: 2c 20 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 , you need this
0290: 66 69 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 file and the "sq
02a0: 6c 69 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 lite3.h" header
02b0: 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 file that define
02c0: 73 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d s.** the program
02d0: 6d 69 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 ming interface t
02e0: 6f 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 o the SQLite lib
02f0: 72 61 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 rary. (If you d
0300: 6f 20 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 o not have .** t
0310: 68 65 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 he "sqlite3.h" h
0320: 65 61 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 eader file at ha
0330: 6e 64 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e nd, you will fin
0340: 64 20 61 20 63 6f 70 79 20 65 6d 62 65 64 64 65 d a copy embedde
0350: 64 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 d within.** the
0360: 74 65 78 74 20 6f 66 20 74 68 69 73 20 66 69 6c text of this fil
0370: 65 2e 20 20 53 65 61 72 63 68 20 66 6f 72 20 22 e. Search for "
0380: 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 Begin file sqlit
0390: 65 33 2e 68 22 20 74 6f 20 66 69 6e 64 20 74 68 e3.h" to find th
03a0: 65 20 73 74 61 72 74 0a 2a 2a 20 6f 66 20 74 68 e start.** of th
03b0: 65 20 65 6d 62 65 64 64 65 64 20 73 71 6c 69 74 e embedded sqlit
03c0: 65 33 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 e3.h header file
03d0: 2e 29 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f .) Additional co
03e0: 64 65 20 66 69 6c 65 73 20 6d 61 79 20 62 65 20 de files may be
03f0: 6e 65 65 64 65 64 0a 2a 2a 20 69 66 20 79 6f 75 needed.** if you
0400: 20 77 61 6e 74 20 61 20 77 72 61 70 70 65 72 20 want a wrapper
0410: 74 6f 20 69 6e 74 65 72 66 61 63 65 20 53 51 4c to interface SQL
0420: 69 74 65 20 77 69 74 68 20 79 6f 75 72 20 63 68 ite with your ch
0430: 6f 69 63 65 20 6f 66 20 70 72 6f 67 72 61 6d 6d oice of programm
0440: 69 6e 67 0a 2a 2a 20 6c 61 6e 67 75 61 67 65 2e ing.** language.
0450: 20 54 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 The code for th
0460: 65 20 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d e "sqlite3" comm
0470: 61 6e 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 20 69 and-line shell i
0480: 73 20 61 6c 73 6f 20 69 6e 20 61 0a 2a 2a 20 73 s also in a.** s
0490: 65 70 61 72 61 74 65 20 66 69 6c 65 2e 20 54 68 eparate file. Th
04a0: 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
04b0: 20 6f 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 only code for t
04c0: 68 65 20 63 6f 72 65 20 53 51 4c 69 74 65 20 6c he core SQLite l
04d0: 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 64 65 66 69 ibrary..*/.#defi
04e0: 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31 ne SQLITE_CORE 1
04f0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
0500: 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a 23 AMALGAMATION 1.#
0510: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52 ifndef SQLITE_PR
0520: 49 56 41 54 45 0a 23 20 64 65 66 69 6e 65 20 53 IVATE.# define S
0530: 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 QLITE_PRIVATE st
0540: 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 69 66 6e atic.#endif.#ifn
0550: 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23 def SQLITE_API.#
0560: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 define SQLITE_A
0570: 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a PI.#endif./*****
0580: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
0590: 66 69 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 file sqliteInt.h
05a0: 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
05b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
05c0: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
05d0: 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
05e0: 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
05f0: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
0600: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
0610: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
0620: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
0630: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
0640: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
0650: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
0660: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
0670: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
0680: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
0690: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
06a0: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
06b0: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
06c0: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
06d0: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
06e0: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
06f0: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
0700: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0710: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0720: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0730: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0740: 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e 74 .** Internal int
0750: 65 72 66 61 63 65 20 64 65 66 69 6e 69 74 69 6f erface definitio
0760: 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a ns for SQLite..*
0770: 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 *.*/.#ifndef _SQ
0780: 4c 49 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 LITEINT_H_.#defi
0790: 6e 65 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f ne _SQLITEINT_H_
07a0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 ../*.** These #d
07b0: 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e efines should en
07c0: 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 able >2GB file s
07d0: 75 70 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20 upport on POSIX
07e0: 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c if the.** underl
07f0: 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 ying operating s
0800: 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 ystem supports i
0810: 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 t. If the OS la
0820: 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c cks.** large fil
0830: 65 20 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 e support, or if
0840: 20 74 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f the OS is windo
0850: 77 73 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 ws, these should
0860: 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a be no-ops..**.*
0870: 2a 20 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20 * Ticket #2739:
0880: 20 54 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f The _LARGEFILE_
0890: 53 4f 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73 SOURCE macro mus
08a0: 74 20 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 t appear before
08b0: 61 6e 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69 any.** system #i
08c0: 6e 63 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c ncludes. Hence,
08d0: 20 74 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 this block of c
08e0: 6f 64 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 ode must be the
08f0: 76 65 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f very first.** co
0900: 64 65 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65 de in all source
0910: 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 files..**.** La
0920: 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 rge file support
0930: 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 can be disabled
0940: 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c using the -DSQL
0950: 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 ITE_DISABLE_LFS
0960: 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 switch.** on the
0970: 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e compiler comman
0980: 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 d line. This is
0990: 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f necessary if yo
09a0: 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a u are compiling.
09b0: 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d ** on a recent m
09c0: 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20 achine (ex: Red
09d0: 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 Hat 7.2) but you
09e0: 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 want your code
09f0: 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e to work.** on an
0a00: 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 older machine (
0a10: 65 78 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29 ex: Red Hat 6.0)
0a20: 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c . If you compil
0a30: 65 20 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32 e on Red Hat 7.2
0a40: 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 .** without this
0a50: 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 option, LFS is
0a60: 65 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 enable. But LFS
0a70: 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 does not exist
0a80: 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a in the kernel.**
0a90: 20 69 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c in Red Hat 6.0,
0aa0: 20 73 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e so the code won
0ab0: 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 't work. Hence,
0ac0: 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e for maximum bin
0ad0: 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 ary.** portabili
0ae0: 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d ty you should om
0af0: 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69 it LFS..**.** Si
0b00: 6d 69 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f milar is true fo
0b10: 72 20 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53 r Mac OS X. LFS
0b20: 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 is only support
0b30: 65 64 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39 ed on Mac OS X 9
0b40: 20 61 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 and later..*/.#
0b50: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 ifndef SQLITE_DI
0b60: 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 SABLE_LFS.# defi
0b70: 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 ne _LARGE_FILE
0b80: 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 1.# ifndef
0b90: 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 _FILE_OFFSET_BIT
0ba0: 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 S.# define _FI
0bb0: 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 LE_OFFSET_BITS 6
0bc0: 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 4.# endif.# defi
0bd0: 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f ne _LARGEFILE_SO
0be0: 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f URCE 1.#endif../
0bf0: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65 *.** Include the
0c00: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68 configuration h
0c10: 65 61 64 65 72 20 6f 75 74 70 75 74 20 62 79 20 eader output by
0c20: 27 63 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77 'configure' if w
0c30: 65 27 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a e're using the.*
0c40: 2a 20 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64 * autoconf-based
0c50: 20 62 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 build.*/.#ifdef
0c60: 20 5f 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f _HAVE_SQLITE_CO
0c70: 4e 46 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20 NFIG_H.#include
0c80: 22 63 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69 "config.h".#endi
0c90: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
0ca0: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 ** Include sqlit
0cb0: 65 4c 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20 eLimit.h in the
0cc0: 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
0cd0: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
0ce0: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
0cf0: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 ** Begin file sq
0d00: 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a liteLimit.h ****
0d10: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0d20: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0d30: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 */./*.** 2007 Ma
0d40: 79 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 y 7.**.** The au
0d50: 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
0d60: 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
0d70: 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
0d80: 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
0d90: 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
0da0: 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
0db0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
0dc0: 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
0dd0: 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
0de0: 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
0df0: 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
0e00: 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
0e10: 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
0e20: 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
0e30: 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
0e40: 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
0e50: 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
0e60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0e70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0e80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0e90: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0ea0: 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66 **.** .** This f
0eb0: 69 6c 65 20 64 65 66 69 6e 65 73 20 76 61 72 69 ile defines vari
0ec0: 6f 75 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68 ous limits of wh
0ed0: 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72 at SQLite can pr
0ee0: 6f 63 65 73 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a ocess..*/../*.**
0ef0: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e The maximum len
0f00: 67 74 68 20 6f 66 20 61 20 54 45 58 54 20 6f 72 gth of a TEXT or
0f10: 20 42 4c 4f 42 20 69 6e 20 62 79 74 65 73 2e 20 BLOB in bytes.
0f20: 20 20 54 68 69 73 20 61 6c 73 6f 0a 2a 2a 20 6c This also.** l
0f30: 69 6d 69 74 73 20 74 68 65 20 73 69 7a 65 20 6f imits the size o
0f40: 66 20 61 20 72 6f 77 20 69 6e 20 61 20 74 61 62 f a row in a tab
0f50: 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a le or index..**.
0f60: 2a 2a 20 54 68 65 20 68 61 72 64 20 6c 69 6d 69 ** The hard limi
0f70: 74 20 69 73 20 74 68 65 20 61 62 69 6c 69 74 79 t is the ability
0f80: 20 6f 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 of a 32-bit sig
0f90: 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 ned integer.** t
0fa0: 6f 20 63 6f 75 6e 74 20 74 68 65 20 73 69 7a 65 o count the size
0fb0: 3a 20 32 5e 33 31 2d 31 20 6f 72 20 32 31 34 37 : 2^31-1 or 2147
0fc0: 34 38 33 36 34 37 2e 0a 2a 2f 0a 23 69 66 6e 64 483647..*/.#ifnd
0fd0: 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 ef SQLITE_MAX_LE
0fe0: 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 NGTH.# define SQ
0ff0: 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 LITE_MAX_LENGTH
1000: 31 30 30 30 30 30 30 30 30 30 0a 23 65 6e 64 69 1000000000.#endi
1010: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 f../*.** This is
1020: 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d the maximum num
1030: 62 65 72 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 20 ber of.**.**
1040: 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74 * Columns in a t
1050: 61 62 6c 65 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c able.** * Col
1060: 75 6d 6e 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 umns in an index
1070: 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 .** * Columns
1080: 20 69 6e 20 61 20 76 69 65 77 0a 2a 2a 20 20 20 in a view.**
1090: 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 * Terms in the
10a0: 53 45 54 20 63 6c 61 75 73 65 20 6f 66 20 61 6e SET clause of an
10b0: 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e UPDATE statemen
10c0: 74 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 t.** * Terms
10d0: 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 in the result se
10e0: 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 t of a SELECT st
10f0: 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a 20 atement.** *
1100: 54 65 72 6d 73 20 69 6e 20 74 68 65 20 47 52 4f Terms in the GRO
1110: 55 50 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 UP BY or ORDER B
1120: 59 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 53 Y clauses of a S
1130: 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
1140: 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 .** * Terms i
1150: 6e 20 74 68 65 20 56 41 4c 55 45 53 20 63 6c 61 n the VALUES cla
1160: 75 73 65 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 use of an INSERT
1170: 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a statement.**.**
1180: 20 54 68 65 20 68 61 72 64 20 75 70 70 65 72 20 The hard upper
1190: 6c 69 6d 69 74 20 68 65 72 65 20 69 73 20 33 32 limit here is 32
11a0: 36 37 36 2e 20 20 4d 6f 73 74 20 64 61 74 61 62 676. Most datab
11b0: 61 73 65 20 70 65 6f 70 6c 65 20 77 69 6c 6c 0a ase people will.
11c0: 2a 2a 20 74 65 6c 6c 20 79 6f 75 20 74 68 61 74 ** tell you that
11d0: 20 69 6e 20 61 20 77 65 6c 6c 2d 6e 6f 72 6d 61 in a well-norma
11e0: 6c 69 7a 65 64 20 64 61 74 61 62 61 73 65 2c 20 lized database,
11f0: 79 6f 75 20 75 73 75 61 6c 6c 79 20 73 68 6f 75 you usually shou
1200: 6c 64 0a 2a 2a 20 6e 6f 74 20 68 61 76 65 20 6d ld.** not have m
1210: 6f 72 65 20 74 68 61 6e 20 61 20 64 6f 7a 65 6e ore than a dozen
1220: 20 6f 72 20 73 6f 20 63 6f 6c 75 6d 6e 73 20 69 or so columns i
1230: 6e 20 61 6e 79 20 74 61 62 6c 65 2e 20 20 41 6e n any table. An
1240: 64 20 69 66 0a 2a 2a 20 74 68 61 74 20 69 73 20 d if.** that is
1250: 74 68 65 20 63 61 73 65 2c 20 74 68 65 72 65 20 the case, there
1260: 69 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 68 is no point in h
1270: 61 76 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aving more than
1280: 61 20 66 65 77 0a 2a 2a 20 64 6f 7a 65 6e 20 76 a few.** dozen v
1290: 61 6c 75 65 73 20 69 6e 20 61 6e 79 20 6f 66 20 alues in any of
12a0: 74 68 65 20 6f 74 68 65 72 20 73 69 74 75 61 74 the other situat
12b0: 69 6f 6e 73 20 64 65 73 63 72 69 62 65 64 20 61 ions described a
12c0: 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 bove..*/.#ifndef
12d0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 SQLITE_MAX_COLU
12e0: 4d 4e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 MN.# define SQLI
12f0: 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 32 30 TE_MAX_COLUMN 20
1300: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 00.#endif../*.**
1310: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e The maximum len
1320: 67 74 68 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 gth of a single
1330: 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e SQL statement in
1340: 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 bytes..**.** It
1350: 20 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 20 used to be the
1360: 63 61 73 65 20 74 68 61 74 20 73 65 74 74 69 6e case that settin
1370: 67 20 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 g this value to
1380: 7a 65 72 6f 20 77 6f 75 6c 64 0a 2a 2a 20 74 75 zero would.** tu
1390: 72 6e 20 74 68 65 20 6c 69 6d 69 74 20 6f 66 66 rn the limit off
13a0: 2e 20 20 54 68 61 74 20 69 73 20 6e 6f 20 6c 6f . That is no lo
13b0: 6e 67 65 72 20 74 72 75 65 2e 20 20 49 74 20 69 nger true. It i
13c0: 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a 2a s not possible.*
13d0: 2a 20 74 6f 20 74 75 72 6e 20 74 68 69 73 20 6c * to turn this l
13e0: 69 6d 69 74 20 6f 66 66 2e 0a 2a 2f 0a 23 69 66 imit off..*/.#if
13f0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f ndef SQLITE_MAX_
1400: 53 51 4c 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 SQL_LENGTH.# def
1410: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 ine SQLITE_MAX_S
1420: 51 4c 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 QL_LENGTH 100000
1430: 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 0000.#endif../*.
1440: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 ** The maximum d
1450: 65 70 74 68 20 6f 66 20 61 6e 20 65 78 70 72 65 epth of an expre
1460: 73 73 69 6f 6e 20 74 72 65 65 2e 20 54 68 69 73 ssion tree. This
1470: 20 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 0a is limited to .
1480: 2a 2a 20 73 6f 6d 65 20 65 78 74 65 6e 74 20 62 ** some extent b
1490: 79 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c y SQLITE_MAX_SQL
14a0: 5f 4c 45 4e 47 54 48 2e 20 42 75 74 20 73 6f 6d _LENGTH. But som
14b0: 65 74 69 6d 65 20 79 6f 75 20 6d 69 67 68 74 20 etime you might
14c0: 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 70 6c 61 63 .** want to plac
14d0: 65 20 6d 6f 72 65 20 73 65 76 65 72 65 20 6c 69 e more severe li
14e0: 6d 69 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 mits on the comp
14f0: 6c 65 78 69 74 79 20 6f 66 20 61 6e 20 0a 2a 2a lexity of an .**
1500: 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a expression..**.
1510: 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 ** A value of 0
1520: 75 73 65 64 20 74 6f 20 6d 65 61 6e 20 74 68 61 used to mean tha
1530: 74 20 74 68 65 20 6c 69 6d 69 74 20 77 61 73 20 t the limit was
1540: 6e 6f 74 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2a not enforced..**
1550: 20 42 75 74 20 74 68 61 74 20 69 73 20 6e 6f 20 But that is no
1560: 6c 6f 6e 67 65 72 20 74 72 75 65 2e 20 20 54 68 longer true. Th
1570: 65 20 6c 69 6d 69 74 20 69 73 20 6e 6f 77 20 73 e limit is now s
1580: 74 72 69 63 74 6c 79 20 65 6e 66 6f 72 63 65 64 trictly enforced
1590: 0a 2a 2a 20 61 74 20 61 6c 6c 20 74 69 6d 65 73 .** at all times
15a0: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
15b0: 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 ITE_MAX_EXPR_DEP
15c0: 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 TH.# define SQLI
15d0: 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 TE_MAX_EXPR_DEPT
15e0: 48 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f H 1000.#endif../
15f0: 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d *.** The maximum
1600: 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 number of terms
1610: 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 in a compound S
1620: 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
1630: 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e .** The code gen
1640: 65 72 61 74 6f 72 20 66 6f 72 20 63 6f 6d 70 6f erator for compo
1650: 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 und SELECT state
1660: 6d 65 6e 74 73 20 64 6f 65 73 20 6f 6e 65 0a 2a ments does one.*
1670: 2a 20 6c 65 76 65 6c 20 6f 66 20 72 65 63 75 72 * level of recur
1680: 73 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 65 sion for each te
1690: 72 6d 2e 20 20 41 20 73 74 61 63 6b 20 6f 76 65 rm. A stack ove
16a0: 72 66 6c 6f 77 20 63 61 6e 20 72 65 73 75 6c 74 rflow can result
16b0: 0a 2a 2a 20 69 66 20 74 68 65 20 6e 75 6d 62 65 .** if the numbe
16c0: 72 20 6f 66 20 74 65 72 6d 73 20 69 73 20 74 6f r of terms is to
16d0: 6f 20 6c 61 72 67 65 2e 20 20 49 6e 20 70 72 61 o large. In pra
16e0: 63 74 69 63 65 2c 20 6d 6f 73 74 20 53 51 4c 0a ctice, most SQL.
16f0: 2a 2a 20 6e 65 76 65 72 20 68 61 73 20 6d 6f 72 ** never has mor
1700: 65 20 74 68 61 6e 20 33 20 6f 72 20 34 20 74 65 e than 3 or 4 te
1710: 72 6d 73 2e 20 20 55 73 65 20 61 20 76 61 6c 75 rms. Use a valu
1720: 65 20 6f 66 20 30 20 74 6f 20 64 69 73 61 62 6c e of 0 to disabl
1730: 65 0a 2a 2a 20 61 6e 79 20 6c 69 6d 69 74 20 6f e.** any limit o
1740: 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1750: 74 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f terms in a compo
1760: 75 6e 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 23 unt SELECT..*/.#
1770: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 ifndef SQLITE_MA
1780: 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 X_COMPOUND_SELEC
1790: 54 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 T.# define SQLIT
17a0: 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 E_MAX_COMPOUND_S
17b0: 45 4c 45 43 54 20 35 30 30 0a 23 65 6e 64 69 66 ELECT 500.#endif
17c0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 ../*.** The maxi
17d0: 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 mum number of op
17e0: 63 6f 64 65 73 20 69 6e 20 61 20 56 44 42 45 20 codes in a VDBE
17f0: 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 program..** Not
1800: 63 75 72 72 65 6e 74 6c 79 20 65 6e 66 6f 72 63 currently enforc
1810: 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ed..*/.#ifndef S
1820: 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f QLITE_MAX_VDBE_O
1830: 50 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 P.# define SQLIT
1840: 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 20 32 35 E_MAX_VDBE_OP 25
1850: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 000.#endif../*.*
1860: 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 * The maximum nu
1870: 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
1880: 73 20 74 6f 20 61 6e 20 53 51 4c 20 66 75 6e 63 s to an SQL func
1890: 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 tion..*/.#ifndef
18a0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 SQLITE_MAX_FUNC
18b0: 54 49 4f 4e 5f 41 52 47 0a 23 20 64 65 66 69 6e TION_ARG.# defin
18c0: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e e SQLITE_MAX_FUN
18d0: 43 54 49 4f 4e 5f 41 52 47 20 31 32 37 0a 23 65 CTION_ARG 127.#e
18e0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
18f0: 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
1900: 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 f in-memory page
1910: 73 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 s to use for the
1920: 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 0a 2a main database.*
1930: 2a 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 * table and for
1940: 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 temporary tables
1950: 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 44 45 . The SQLITE_DE
1960: 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 FAULT_CACHE_SIZE
1970: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1980: 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 TE_DEFAULT_CACHE
1990: 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 _SIZE.# define S
19a0: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 QLITE_DEFAULT_CA
19b0: 43 48 45 5f 53 49 5a 45 20 20 32 30 30 30 0a 23 CHE_SIZE 2000.#
19c0: 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 endif.#ifndef SQ
19d0: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d LITE_DEFAULT_TEM
19e0: 50 5f 43 41 43 48 45 5f 53 49 5a 45 0a 23 20 64 P_CACHE_SIZE.# d
19f0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 efine SQLITE_DEF
1a00: 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f AULT_TEMP_CACHE_
1a10: 53 49 5a 45 20 20 35 30 30 0a 23 65 6e 64 69 66 SIZE 500.#endif
1a20: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 ../*.** The defa
1a30: 75 6c 74 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 ult number of fr
1a40: 61 6d 65 73 20 74 6f 20 61 63 63 75 6d 75 6c 61 ames to accumula
1a50: 74 65 20 69 6e 20 74 68 65 20 6c 6f 67 20 66 69 te in the log fi
1a60: 6c 65 20 62 65 66 6f 72 65 0a 2a 2a 20 63 68 65 le before.** che
1a70: 63 6b 70 6f 69 6e 74 69 6e 67 20 74 68 65 20 64 ckpointing the d
1a80: 61 74 61 62 61 73 65 20 69 6e 20 57 41 4c 20 6d atabase in WAL m
1a90: 6f 64 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ode..*/.#ifndef
1aa0: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 57 SQLITE_DEFAULT_W
1ab0: 41 4c 5f 41 55 54 4f 43 48 45 43 4b 50 4f 49 4e AL_AUTOCHECKPOIN
1ac0: 54 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 T.# define SQLIT
1ad0: 45 5f 44 45 46 41 55 4c 54 5f 57 41 4c 5f 41 55 E_DEFAULT_WAL_AU
1ae0: 54 4f 43 48 45 43 4b 50 4f 49 4e 54 20 20 31 30 TOCHECKPOINT 10
1af0: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 00.#endif../*.**
1b00: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d The maximum num
1b10: 62 65 72 20 6f 66 20 61 74 74 61 63 68 65 64 20 ber of attached
1b20: 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 69 73 databases. This
1b30: 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e must be between
1b40: 20 30 0a 2a 2a 20 61 6e 64 20 36 32 2e 20 20 54 0.** and 62. T
1b50: 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f he upper bound o
1b60: 6e 20 36 32 20 69 73 20 62 65 63 61 75 73 65 20 n 62 is because
1b70: 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 a 64-bit integer
1b80: 20 62 69 74 6d 61 70 0a 2a 2a 20 69 73 20 75 73 bitmap.** is us
1b90: 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f ed internally to
1ba0: 20 74 72 61 63 6b 20 61 74 74 61 63 68 65 64 20 track attached
1bb0: 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a 23 69 databases..*/.#i
1bc0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
1bd0: 5f 41 54 54 41 43 48 45 44 0a 23 20 64 65 66 69 _ATTACHED.# defi
1be0: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 ne SQLITE_MAX_AT
1bf0: 54 41 43 48 45 44 20 31 30 0a 23 65 6e 64 69 66 TACHED 10.#endif
1c00: 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 .../*.** The max
1c10: 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 61 20 imum value of a
1c20: 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20 74 68 ?nnn wildcard th
1c30: 61 74 20 74 68 65 20 70 61 72 73 65 72 20 77 69 at the parser wi
1c40: 6c 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a 23 69 ll accept..*/.#i
1c50: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
1c60: 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 _VARIABLE_NUMBER
1c70: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
1c80: 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 _MAX_VARIABLE_NU
1c90: 4d 42 45 52 20 39 39 39 0a 23 65 6e 64 69 66 0a MBER 999.#endif.
1ca0: 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 70 61 67 65 ./* Maximum page
1cb0: 20 73 69 7a 65 2e 20 20 54 68 65 20 75 70 70 65 size. The uppe
1cc0: 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 20 r bound on this
1cd0: 76 61 6c 75 65 20 69 73 20 36 35 35 33 36 2e 20 value is 65536.
1ce0: 20 54 68 69 73 20 61 20 6c 69 6d 69 74 0a 2a 2a This a limit.**
1cf0: 20 69 6d 70 6f 73 65 64 20 62 79 20 74 68 65 20 imposed by the
1d00: 75 73 65 20 6f 66 20 31 36 2d 62 69 74 20 6f 66 use of 16-bit of
1d10: 66 73 65 74 73 20 77 69 74 68 69 6e 20 65 61 63 fsets within eac
1d20: 68 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 45 61 h page..**.** Ea
1d30: 72 6c 69 65 72 20 76 65 72 73 69 6f 6e 73 20 6f rlier versions o
1d40: 66 20 53 51 4c 69 74 65 20 61 6c 6c 6f 77 65 64 f SQLite allowed
1d50: 20 74 68 65 20 75 73 65 72 20 74 6f 20 63 68 61 the user to cha
1d60: 6e 67 65 20 74 68 69 73 20 76 61 6c 75 65 20 61 nge this value a
1d70: 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20 74 69 6d t.** compile tim
1d80: 65 2e 20 54 68 69 73 20 69 73 20 6e 6f 20 6c 6f e. This is no lo
1d90: 6e 67 65 72 20 70 65 72 6d 69 74 74 65 64 2c 20 nger permitted,
1da0: 6f 6e 20 74 68 65 20 67 72 6f 75 6e 64 73 20 74 on the grounds t
1db0: 68 61 74 20 69 74 20 63 72 65 61 74 65 73 0a 2a hat it creates.*
1dc0: 2a 20 61 20 6c 69 62 72 61 72 79 20 74 68 61 74 * a library that
1dd0: 20 69 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 20 is technically
1de0: 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 incompatible wit
1df0: 68 20 61 6e 20 53 51 4c 69 74 65 20 6c 69 62 72 h an SQLite libr
1e00: 61 72 79 20 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 ary .** compiled
1e10: 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e with a differen
1e20: 74 20 6c 69 6d 69 74 2e 20 49 66 20 61 20 70 72 t limit. If a pr
1e30: 6f 63 65 73 73 20 6f 70 65 72 61 74 69 6e 67 20 ocess operating
1e40: 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 0a 2a on a database .*
1e50: 2a 20 77 69 74 68 20 61 20 70 61 67 65 2d 73 69 * with a page-si
1e60: 7a 65 20 6f 66 20 36 35 35 33 36 20 62 79 74 65 ze of 65536 byte
1e70: 73 20 63 72 61 73 68 65 73 2c 20 74 68 65 6e 20 s crashes, then
1e80: 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 53 an instance of S
1e90: 51 4c 69 74 65 20 0a 2a 2a 20 63 6f 6d 70 69 6c QLite .** compil
1ea0: 65 64 20 77 69 74 68 20 74 68 65 20 64 65 66 61 ed with the defa
1eb0: 75 6c 74 20 70 61 67 65 2d 73 69 7a 65 20 6c 69 ult page-size li
1ec0: 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 mit will not be
1ed0: 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b able to rollback
1ee0: 20 0a 2a 2a 20 74 68 65 20 61 62 6f 72 74 65 64 .** the aborted
1ef0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 transaction. Th
1f00: 69 73 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f is could lead to
1f10: 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 database corrup
1f20: 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 tion..*/.#ifdef
1f30: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
1f40: 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 4c SIZE.# undef SQL
1f50: 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a ITE_MAX_PAGE_SIZ
1f60: 45 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 E.#endif.#define
1f70: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 SQLITE_MAX_PAGE
1f80: 5f 53 49 5a 45 20 36 35 35 33 36 0a 0a 0a 2f 2a _SIZE 65536.../*
1f90: 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 .** The default
1fa0: 73 69 7a 65 20 6f 66 20 61 20 64 61 74 61 62 61 size of a databa
1fb0: 73 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e se page..*/.#ifn
1fc0: 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 def SQLITE_DEFAU
1fd0: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 LT_PAGE_SIZE.# d
1fe0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 efine SQLITE_DEF
1ff0: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 31 AULT_PAGE_SIZE 1
2000: 30 32 34 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 024.#endif.#if S
2010: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 QLITE_DEFAULT_PA
2020: 47 45 5f 53 49 5a 45 3e 53 51 4c 49 54 45 5f 4d GE_SIZE>SQLITE_M
2030: 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 75 AX_PAGE_SIZE.# u
2040: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 ndef SQLITE_DEFA
2050: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 ULT_PAGE_SIZE.#
2060: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
2070: 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 FAULT_PAGE_SIZE
2080: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
2090: 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a SIZE.#endif../*.
20a0: 2a 2a 20 4f 72 64 69 6e 61 72 69 6c 79 2c 20 69 ** Ordinarily, i
20b0: 66 20 6e 6f 20 76 61 6c 75 65 20 69 73 20 65 78 f no value is ex
20c0: 70 6c 69 63 69 74 6c 79 20 70 72 6f 76 69 64 65 plicitly provide
20d0: 64 2c 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 d, SQLite create
20e0: 73 20 64 61 74 61 62 61 73 65 73 0a 2a 2a 20 77 s databases.** w
20f0: 69 74 68 20 70 61 67 65 20 73 69 7a 65 20 53 51 ith page size SQ
2100: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 LITE_DEFAULT_PAG
2110: 45 5f 53 49 5a 45 2e 20 48 6f 77 65 76 65 72 2c E_SIZE. However,
2120: 20 62 61 73 65 64 20 6f 6e 20 63 65 72 74 61 69 based on certai
2130: 6e 0a 2a 2a 20 64 65 76 69 63 65 20 63 68 61 72 n.** device char
2140: 61 63 74 65 72 69 73 74 69 63 73 20 28 73 65 63 acteristics (sec
2150: 74 6f 72 2d 73 69 7a 65 20 61 6e 64 20 61 74 6f tor-size and ato
2160: 6d 69 63 20 77 72 69 74 65 28 29 20 73 75 70 70 mic write() supp
2170: 6f 72 74 29 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 ort),.** SQLite
2180: 6d 61 79 20 63 68 6f 6f 73 65 20 61 20 6c 61 72 may choose a lar
2190: 67 65 72 20 76 61 6c 75 65 2e 20 54 68 69 73 20 ger value. This
21a0: 63 6f 6e 73 74 61 6e 74 20 69 73 20 74 68 65 20 constant is the
21b0: 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 0a 2a 2a maximum value.**
21c0: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 63 68 6f SQLite will cho
21d0: 6f 73 65 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 0a ose on its own..
21e0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
21f0: 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 E_MAX_DEFAULT_PA
2200: 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 GE_SIZE.# define
2210: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 SQLITE_MAX_DEFA
2220: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 38 31 ULT_PAGE_SIZE 81
2230: 39 32 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 92.#endif.#if SQ
2240: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
2250: 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c 49 54 _PAGE_SIZE>SQLIT
2260: 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a E_MAX_PAGE_SIZE.
2270: 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d # undef SQLITE_M
2280: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f AX_DEFAULT_PAGE_
2290: 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SIZE.# define SQ
22a0: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
22b0: 5f 50 41 47 45 5f 53 49 5a 45 20 53 51 4c 49 54 _PAGE_SIZE SQLIT
22c0: 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a E_MAX_PAGE_SIZE.
22d0: 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d #endif.../*.** M
22e0: 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
22f0: 20 70 61 67 65 73 20 69 6e 20 6f 6e 65 20 64 61 pages in one da
2300: 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
2310: 2a 2a 20 54 68 69 73 20 69 73 20 72 65 61 6c 6c ** This is reall
2320: 79 20 6a 75 73 74 20 74 68 65 20 64 65 66 61 75 y just the defau
2330: 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 lt value for the
2340: 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 20 max_page_count
2350: 70 72 61 67 6d 61 2e 0a 2a 2a 20 54 68 69 73 20 pragma..** This
2360: 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6c 6f 77 value can be low
2370: 65 72 65 64 20 28 6f 72 20 72 61 69 73 65 64 29 ered (or raised)
2380: 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73 69 at run-time usi
2390: 6e 67 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 6d ng that the.** m
23a0: 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 20 6d 61 ax_page_count ma
23b0: 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 cro..*/.#ifndef
23c0: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
23d0: 43 4f 55 4e 54 0a 23 20 64 65 66 69 6e 65 20 53 COUNT.# define S
23e0: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 QLITE_MAX_PAGE_C
23f0: 4f 55 4e 54 20 31 30 37 33 37 34 31 38 32 33 0a OUNT 1073741823.
2400: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 #endif../*.** Ma
2410: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28 69 6e ximum length (in
2420: 20 62 79 74 65 73 29 20 6f 66 20 74 68 65 20 70 bytes) of the p
2430: 61 74 74 65 72 6e 20 69 6e 20 61 20 4c 49 4b 45 attern in a LIKE
2440: 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70 65 72 or GLOB.** oper
2450: 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 ator..*/.#ifndef
2460: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 SQLITE_MAX_LIKE
2470: 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 0a _PATTERN_LENGTH.
2480: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
2490: 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e MAX_LIKE_PATTERN
24a0: 5f 4c 45 4e 47 54 48 20 35 30 30 30 30 0a 23 65 _LENGTH 50000.#e
24b0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 ndif../*.** Maxi
24c0: 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 72 65 63 mum depth of rec
24d0: 75 72 73 69 6f 6e 20 66 6f 72 20 74 72 69 67 67 ursion for trigg
24e0: 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 6c ers..**.** A val
24f0: 75 65 20 6f 66 20 31 20 6d 65 61 6e 73 20 74 68 ue of 1 means th
2500: 61 74 20 61 20 74 72 69 67 67 65 72 20 70 72 6f at a trigger pro
2510: 67 72 61 6d 20 77 69 6c 6c 20 6e 6f 74 20 62 65 gram will not be
2520: 20 61 62 6c 65 20 74 6f 20 69 74 73 65 6c 66 0a able to itself.
2530: 2a 2a 20 66 69 72 65 20 61 6e 79 20 74 72 69 67 ** fire any trig
2540: 67 65 72 73 2e 20 41 20 76 61 6c 75 65 20 6f 66 gers. A value of
2550: 20 30 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f 0 means that no
2560: 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d trigger program
2570: 73 20 61 74 20 61 6c 6c 20 0a 2a 2a 20 6d 61 79 s at all .** may
2580: 20 62 65 20 65 78 65 63 75 74 65 64 2e 0a 2a 2f be executed..*/
2590: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
25a0: 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54 MAX_TRIGGER_DEPT
25b0: 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 H.# define SQLIT
25c0: 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45 E_MAX_TRIGGER_DE
25d0: 50 54 48 20 31 30 30 30 0a 23 65 6e 64 69 66 0a PTH 1000.#endif.
25e0: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
25f0: 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 4c 69 End of sqliteLi
2600: 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mit.h **********
2610: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2620: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
2630: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
2640: 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
2650: 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
2660: 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
2670: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
2680: 0a 0a 2f 2a 20 44 69 73 61 62 6c 65 20 6e 75 69 ../* Disable nui
2690: 73 61 6e 63 65 20 77 61 72 6e 69 6e 67 73 20 6f sance warnings o
26a0: 6e 20 42 6f 72 6c 61 6e 64 20 63 6f 6d 70 69 6c n Borland compil
26b0: 65 72 73 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e ers */.#if defin
26c0: 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 ed(__BORLANDC__)
26d0: 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 72 .#pragma warn -r
26e0: 63 68 20 2f 2a 20 75 6e 72 65 61 63 68 61 62 6c ch /* unreachabl
26f0: 65 20 63 6f 64 65 20 2a 2f 0a 23 70 72 61 67 6d e code */.#pragm
2700: 61 20 77 61 72 6e 20 2d 63 63 63 20 2f 2a 20 43 a warn -ccc /* C
2710: 6f 6e 64 69 74 69 6f 6e 20 69 73 20 61 6c 77 61 ondition is alwa
2720: 79 73 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 ys true or false
2730: 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e */.#pragma warn
2740: 20 2d 61 75 73 20 2f 2a 20 41 73 73 69 67 6e 65 -aus /* Assigne
2750: 64 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72 d value is never
2760: 20 75 73 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 used */.#pragma
2770: 20 77 61 72 6e 20 2d 63 73 75 20 2f 2a 20 43 6f warn -csu /* Co
2780: 6d 70 61 72 69 6e 67 20 73 69 67 6e 65 64 20 61 mparing signed a
2790: 6e 64 20 75 6e 73 69 67 6e 65 64 20 2a 2f 0a 23 nd unsigned */.#
27a0: 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 73 70 61 pragma warn -spa
27b0: 20 2f 2a 20 53 75 73 70 69 63 69 6f 75 73 20 70 /* Suspicious p
27c0: 6f 69 6e 74 65 72 20 61 72 69 74 68 6d 65 74 69 ointer arithmeti
27d0: 63 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 c */.#endif../*
27e0: 4e 65 65 64 65 64 20 66 6f 72 20 76 61 72 69 6f Needed for vario
27f0: 75 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 2e us definitions..
2800: 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 47 4e . */.#ifndef _GN
2810: 55 5f 53 4f 55 52 43 45 0a 23 20 64 65 66 69 6e U_SOURCE.# defin
2820: 65 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 23 65 e _GNU_SOURCE.#e
2830: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c ndif../*.** Incl
2840: 75 64 65 20 73 74 61 6e 64 61 72 64 20 68 65 61 ude standard hea
2850: 64 65 72 20 66 69 6c 65 73 20 61 73 20 6e 65 63 der files as nec
2860: 65 73 73 61 72 79 0a 2a 2f 0a 23 69 66 64 65 66 essary.*/.#ifdef
2870: 20 48 41 56 45 5f 53 54 44 49 4e 54 5f 48 0a 23 HAVE_STDINT_H.#
2880: 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6e 74 2e include <stdint.
2890: 68 3e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 h>.#endif.#ifdef
28a0: 20 48 41 56 45 5f 49 4e 54 54 59 50 45 53 5f 48 HAVE_INTTYPES_H
28b0: 0a 23 69 6e 63 6c 75 64 65 20 3c 69 6e 74 74 79 .#include <intty
28c0: 70 65 73 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f pes.h>.#endif../
28d0: 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
28e0: 6e 67 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 ng macros are us
28f0: 65 64 20 74 6f 20 63 61 73 74 20 70 6f 69 6e 74 ed to cast point
2900: 65 72 73 20 74 6f 20 69 6e 74 65 67 65 72 73 20 ers to integers
2910: 61 6e 64 0a 2a 2a 20 69 6e 74 65 67 65 72 73 20 and.** integers
2920: 74 6f 20 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 to pointers. Th
2930: 65 20 77 61 79 20 79 6f 75 20 64 6f 20 74 68 69 e way you do thi
2940: 73 20 76 61 72 69 65 73 20 66 72 6f 6d 20 6f 6e s varies from on
2950: 65 20 63 6f 6d 70 69 6c 65 72 0a 2a 2a 20 74 6f e compiler.** to
2960: 20 74 68 65 20 6e 65 78 74 2c 20 73 6f 20 77 65 the next, so we
2970: 20 68 61 76 65 20 64 65 76 65 6c 6f 70 65 64 20 have developed
2980: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 the following se
2990: 74 20 6f 66 20 23 69 66 20 73 74 61 74 65 6d 65 t of #if stateme
29a0: 6e 74 73 0a 2a 2a 20 74 6f 20 67 65 6e 65 72 61 nts.** to genera
29b0: 74 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d te appropriate m
29c0: 61 63 72 6f 73 20 66 6f 72 20 61 20 77 69 64 65 acros for a wide
29d0: 20 72 61 6e 67 65 20 6f 66 20 63 6f 6d 70 69 6c range of compil
29e0: 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ers..**.** The c
29f0: 6f 72 72 65 63 74 20 22 41 4e 53 49 22 20 77 61 orrect "ANSI" wa
2a00: 79 20 74 6f 20 64 6f 20 74 68 69 73 20 69 73 20 y to do this is
2a10: 74 6f 20 75 73 65 20 74 68 65 20 69 6e 74 70 74 to use the intpt
2a20: 72 5f 74 20 74 79 70 65 2e 20 0a 2a 2a 20 55 6e r_t type. .** Un
2a30: 66 6f 72 74 75 6e 61 74 65 6c 79 2c 20 74 68 61 fortunately, tha
2a40: 74 20 74 79 70 65 64 65 66 20 69 73 20 6e 6f 74 t typedef is not
2a50: 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 61 6c available on al
2a60: 6c 20 63 6f 6d 70 69 6c 65 72 73 2c 20 6f 72 0a l compilers, or.
2a70: 2a 2a 20 69 66 20 69 74 20 69 73 20 61 76 61 69 ** if it is avai
2a80: 6c 61 62 6c 65 2c 20 69 74 20 72 65 71 75 69 72 lable, it requir
2a90: 65 73 20 61 6e 20 23 69 6e 63 6c 75 64 65 20 6f es an #include o
2aa0: 66 20 73 70 65 63 69 66 69 63 20 68 65 61 64 65 f specific heade
2ab0: 72 73 0a 2a 2a 20 74 68 61 74 20 76 61 72 79 20 rs.** that vary
2ac0: 66 72 6f 6d 20 6f 6e 65 20 6d 61 63 68 69 6e 65 from one machine
2ad0: 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a to the next..**
2ae0: 0a 2a 2a 20 54 69 63 6b 65 74 20 23 33 38 36 30 .** Ticket #3860
2af0: 3a 20 20 54 68 65 20 6c 6c 76 6d 2d 67 63 63 2d : The llvm-gcc-
2b00: 34 2e 32 20 63 6f 6d 70 69 6c 65 72 20 66 72 6f 4.2 compiler fro
2b10: 6d 20 41 70 70 6c 65 20 63 68 6f 6b 65 73 20 6f m Apple chokes o
2b20: 6e 0a 2a 2a 20 74 68 65 20 28 28 76 6f 69 64 2a n.** the ((void*
2b30: 29 26 28 28 63 68 61 72 2a 29 30 29 5b 58 5d 29 )&((char*)0)[X])
2b40: 20 63 6f 6e 73 74 72 75 63 74 2e 20 20 42 75 74 construct. But
2b50: 20 4d 53 56 43 20 63 68 6f 6b 65 73 20 6f 6e 20 MSVC chokes on
2b60: 28 28 76 6f 69 64 2a 29 28 58 29 29 2e 0a 2a 2a ((void*)(X))..**
2b70: 20 53 6f 20 77 65 20 68 61 76 65 20 74 6f 20 64 So we have to d
2b80: 65 66 69 6e 65 20 74 68 65 20 6d 61 63 72 6f 73 efine the macros
2b90: 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 77 61 in different wa
2ba0: 79 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 ys depending on
2bb0: 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 2e the.** compiler.
2bc0: 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
2bd0: 5f 5f 50 54 52 44 49 46 46 5f 54 59 50 45 5f 5f __PTRDIFF_TYPE__
2be0: 29 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20 ) /* This case
2bf0: 73 68 6f 75 6c 64 20 77 6f 72 6b 20 66 6f 72 20 should work for
2c00: 47 43 43 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 GCC */.# define
2c10: 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 SQLITE_INT_TO_PT
2c20: 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29 28 5f R(X) ((void*)(_
2c30: 5f 50 54 52 44 49 46 46 5f 54 59 50 45 5f 5f 29 _PTRDIFF_TYPE__)
2c40: 28 58 29 29 0a 23 20 64 65 66 69 6e 65 20 53 51 (X)).# define SQ
2c50: 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 LITE_PTR_TO_INT(
2c60: 58 29 20 20 28 28 69 6e 74 29 28 5f 5f 50 54 52 X) ((int)(__PTR
2c70: 44 49 46 46 5f 54 59 50 45 5f 5f 29 28 58 29 29 DIFF_TYPE__)(X))
2c80: 0a 23 65 6c 69 66 20 21 64 65 66 69 6e 65 64 28 .#elif !defined(
2c90: 5f 5f 47 4e 55 43 5f 5f 29 20 20 20 20 20 20 20 __GNUC__)
2ca0: 2f 2a 20 57 6f 72 6b 73 20 66 6f 72 20 63 6f 6d /* Works for com
2cb0: 70 69 6c 65 72 73 20 6f 74 68 65 72 20 74 68 61 pilers other tha
2cc0: 6e 20 4c 4c 56 4d 20 2a 2f 0a 23 20 64 65 66 69 n LLVM */.# defi
2cd0: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f ne SQLITE_INT_TO
2ce0: 5f 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a _PTR(X) ((void*
2cf0: 29 26 28 28 63 68 61 72 2a 29 30 29 5b 58 5d 29 )&((char*)0)[X])
2d00: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
2d10: 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58 29 20 20 _PTR_TO_INT(X)
2d20: 28 28 69 6e 74 29 28 28 28 63 68 61 72 2a 29 58 ((int)(((char*)X
2d30: 29 2d 28 63 68 61 72 2a 29 30 29 29 0a 23 65 6c )-(char*)0)).#el
2d40: 69 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f if defined(HAVE_
2d50: 53 54 44 49 4e 54 5f 48 29 20 20 20 2f 2a 20 55 STDINT_H) /* U
2d60: 73 65 20 74 68 69 73 20 63 61 73 65 20 69 66 20 se this case if
2d70: 77 65 20 68 61 76 65 20 41 4e 53 49 20 68 65 61 we have ANSI hea
2d80: 64 65 72 73 20 2a 2f 0a 23 20 64 65 66 69 6e 65 ders */.# define
2d90: 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 SQLITE_INT_TO_P
2da0: 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29 28 TR(X) ((void*)(
2db0: 69 6e 74 70 74 72 5f 74 29 28 58 29 29 0a 23 20 intptr_t)(X)).#
2dc0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 define SQLITE_PT
2dd0: 52 5f 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 R_TO_INT(X) ((i
2de0: 6e 74 29 28 69 6e 74 70 74 72 5f 74 29 28 58 29 nt)(intptr_t)(X)
2df0: 29 0a 23 65 6c 73 65 20 20 20 20 20 20 20 20 20 ).#else
2e00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2e10: 20 2f 2a 20 47 65 6e 65 72 61 74 65 73 20 61 20 /* Generates a
2e20: 77 61 72 6e 69 6e 67 20 2d 20 62 75 74 20 69 74 warning - but it
2e30: 20 61 6c 77 61 79 73 20 77 6f 72 6b 73 20 2a 2f always works */
2e40: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
2e50: 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20 20 _INT_TO_PTR(X)
2e60: 28 28 76 6f 69 64 2a 29 28 58 29 29 0a 23 20 64 ((void*)(X)).# d
2e70: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 efine SQLITE_PTR
2e80: 5f 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 6e _TO_INT(X) ((in
2e90: 74 29 28 58 29 29 0a 23 65 6e 64 69 66 0a 0a 2f t)(X)).#endif../
2ea0: 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f *.** The SQLITE_
2eb0: 54 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f THREADSAFE macro
2ec0: 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 must be defined
2ed0: 20 61 73 20 30 2c 20 31 2c 20 6f 72 20 32 2e 0a as 0, 1, or 2..
2ee0: 2a 2a 20 30 20 6d 65 61 6e 73 20 6d 75 74 65 78 ** 0 means mutex
2ef0: 65 73 20 61 72 65 20 70 65 72 6d 61 6e 65 6e 74 es are permanent
2f00: 6c 79 20 64 69 73 61 62 6c 65 20 61 6e 64 20 74 ly disable and t
2f10: 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 6e 65 he library is ne
2f20: 76 65 72 0a 2a 2a 20 74 68 72 65 61 64 73 61 66 ver.** threadsaf
2f30: 65 2e 20 20 31 20 6d 65 61 6e 73 20 74 68 65 20 e. 1 means the
2f40: 6c 69 62 72 61 72 79 20 69 73 20 73 65 72 69 61 library is seria
2f50: 6c 69 7a 65 64 20 77 68 69 63 68 20 69 73 20 74 lized which is t
2f60: 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 6c 65 he highest.** le
2f70: 76 65 6c 20 6f 66 20 74 68 72 65 61 64 73 61 66 vel of threadsaf
2f80: 65 74 79 2e 20 20 32 20 6d 65 61 6e 73 20 74 68 ety. 2 means th
2f90: 65 20 6c 69 62 61 72 79 20 69 73 20 6d 75 6c 74 e libary is mult
2fa0: 69 74 68 72 65 61 64 65 64 20 2d 20 6d 75 6c 74 ithreaded - mult
2fb0: 69 70 6c 65 0a 2a 2a 20 74 68 72 65 61 64 73 20 iple.** threads
2fc0: 63 61 6e 20 75 73 65 20 53 51 4c 69 74 65 20 61 can use SQLite a
2fd0: 73 20 6c 6f 6e 67 20 61 73 20 6e 6f 20 74 77 6f s long as no two
2fe0: 20 74 68 72 65 61 64 73 20 74 72 79 20 74 6f 20 threads try to
2ff0: 75 73 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 use the same.**
3000: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3010: 69 6f 6e 20 61 74 20 74 68 65 20 73 61 6d 65 20 ion at the same
3020: 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6c 64 65 time..**.** Olde
3030: 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 r versions of SQ
3040: 4c 69 74 65 20 75 73 65 64 20 61 6e 20 6f 70 74 Lite used an opt
3050: 69 6f 6e 61 6c 20 54 48 52 45 41 44 53 41 46 45 ional THREADSAFE
3060: 20 6d 61 63 72 6f 2e 0a 2a 2a 20 57 65 20 73 75 macro..** We su
3070: 70 70 6f 72 74 20 74 68 61 74 20 66 6f 72 20 6c pport that for l
3080: 65 67 61 63 79 2e 0a 2a 2f 0a 23 69 66 20 21 64 egacy..*/.#if !d
3090: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 efined(SQLITE_TH
30a0: 52 45 41 44 53 41 46 45 29 0a 23 69 66 20 64 65 READSAFE).#if de
30b0: 66 69 6e 65 64 28 54 48 52 45 41 44 53 41 46 45 fined(THREADSAFE
30c0: 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 ).# define SQLIT
30d0: 45 5f 54 48 52 45 41 44 53 41 46 45 20 54 48 52 E_THREADSAFE THR
30e0: 45 41 44 53 41 46 45 0a 23 65 6c 73 65 0a 23 20 EADSAFE.#else.#
30f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 define SQLITE_TH
3100: 52 45 41 44 53 41 46 45 20 31 20 2f 2a 20 49 4d READSAFE 1 /* IM
3110: 50 3a 20 52 2d 30 37 32 37 32 2d 32 32 33 30 39 P: R-07272-22309
3120: 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 */.#endif.#endi
3130: 66 0a 0a 2f 2a 0a 2a 2a 20 50 6f 77 65 72 73 61 f../*.** Powersa
3140: 66 65 20 6f 76 65 72 77 72 69 74 65 20 69 73 20 fe overwrite is
3150: 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 2e 20 20 on by default.
3160: 42 75 74 20 63 61 6e 20 62 65 20 74 75 72 6e 65 But can be turne
3170: 64 20 6f 66 66 20 75 73 69 6e 67 0a 2a 2a 20 74 d off using.** t
3180: 68 65 20 2d 44 53 51 4c 49 54 45 5f 50 4f 57 45 he -DSQLITE_POWE
3190: 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 3d RSAFE_OVERWRITE=
31a0: 30 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 0 command-line o
31b0: 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ption..*/.#ifnde
31c0: 66 20 53 51 4c 49 54 45 5f 50 4f 57 45 52 53 41 f SQLITE_POWERSA
31d0: 46 45 5f 4f 56 45 52 57 52 49 54 45 0a 23 20 64 FE_OVERWRITE.# d
31e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 4f 57 efine SQLITE_POW
31f0: 45 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 ERSAFE_OVERWRITE
3200: 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 1.#endif../*.**
3210: 20 54 68 65 20 53 51 4c 49 54 45 5f 44 45 46 41 The SQLITE_DEFA
3220: 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 20 6d 61 ULT_MEMSTATUS ma
3230: 63 72 6f 20 6d 75 73 74 20 62 65 20 64 65 66 69 cro must be defi
3240: 6e 65 64 20 61 73 20 65 69 74 68 65 72 20 30 20 ned as either 0
3250: 6f 72 20 31 2e 0a 2a 2a 20 49 74 20 64 65 74 65 or 1..** It dete
3260: 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f rmines whether o
3270: 72 20 6e 6f 74 20 74 68 65 20 66 65 61 74 75 72 r not the featur
3280: 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 0a 2a es related to .*
3290: 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f * SQLITE_CONFIG_
32a0: 4d 45 4d 53 54 41 54 55 53 20 61 72 65 20 61 76 MEMSTATUS are av
32b0: 61 69 6c 61 62 6c 65 20 62 79 20 64 65 66 61 75 ailable by defau
32c0: 6c 74 20 6f 72 20 6e 6f 74 2e 20 54 68 69 73 20 lt or not. This
32d0: 76 61 6c 75 65 20 63 61 6e 0a 2a 2a 20 62 65 20 value can.** be
32e0: 6f 76 65 72 72 69 64 64 65 6e 20 61 74 20 72 75 overridden at ru
32f0: 6e 74 69 6d 65 20 75 73 69 6e 67 20 74 68 65 20 ntime using the
3300: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
3310: 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 API..*/.#if !de
3320: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 46 fined(SQLITE_DEF
3330: 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 29 0a AULT_MEMSTATUS).
3340: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
3350: 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 DEFAULT_MEMSTATU
3360: 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a S 1.#endif../*.*
3370: 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 * Exactly one of
3380: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d the following m
3390: 61 63 72 6f 73 20 6d 75 73 74 20 62 65 20 64 65 acros must be de
33a0: 66 69 6e 65 64 20 69 6e 20 6f 72 64 65 72 20 74 fined in order t
33b0: 6f 0a 2a 2a 20 73 70 65 63 69 66 79 20 77 68 69 o.** specify whi
33c0: 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ch memory alloca
33d0: 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 74 tion subsystem t
33e0: 6f 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 o use..**.**
33f0: 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d SQLITE_SYSTEM_M
3400: 41 4c 4c 4f 43 20 20 20 20 20 20 20 20 20 20 2f ALLOC /
3410: 2f 20 55 73 65 20 6e 6f 72 6d 61 6c 20 73 79 73 / Use normal sys
3420: 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 tem malloc().**
3430: 20 20 20 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 SQLITE_WIN32
3440: 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 20 20 20 20 _MALLOC
3450: 20 20 2f 2f 20 55 73 65 20 57 69 6e 33 32 20 6e // Use Win32 n
3460: 61 74 69 76 65 20 68 65 61 70 20 41 50 49 0a 2a ative heap API.*
3470: 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 5a 45 52 * SQLITE_ZER
3480: 4f 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 20 20 20 O_MALLOC
3490: 20 20 20 20 2f 2f 20 55 73 65 20 61 20 73 74 75 // Use a stu
34a0: 62 20 61 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74 b allocator that
34b0: 20 61 6c 77 61 79 73 20 66 61 69 6c 73 0a 2a 2a always fails.**
34c0: 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 45 4d 44 SQLITE_MEMD
34d0: 45 42 55 47 20 20 20 20 20 20 20 20 20 20 20 20 EBUG
34e0: 20 20 20 2f 2f 20 44 65 62 75 67 67 69 6e 67 20 // Debugging
34f0: 76 65 72 73 69 6f 6e 20 6f 66 20 73 79 73 74 65 version of syste
3500: 6d 20 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 0a 2a 2a m malloc().**.**
3510: 20 4f 6e 20 57 69 6e 64 6f 77 73 2c 20 69 66 20 On Windows, if
3520: 74 68 65 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 the SQLITE_WIN32
3530: 5f 4d 41 4c 4c 4f 43 5f 56 41 4c 49 44 41 54 45 _MALLOC_VALIDATE
3540: 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 macro is define
3550: 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 61 73 73 d and the.** ass
3560: 65 72 74 28 29 20 6d 61 63 72 6f 20 69 73 20 65 ert() macro is e
3570: 6e 61 62 6c 65 64 2c 20 65 61 63 68 20 63 61 6c nabled, each cal
3580: 6c 20 69 6e 74 6f 20 74 68 65 20 57 69 6e 33 32 l into the Win32
3590: 20 6e 61 74 69 76 65 20 68 65 61 70 20 73 75 62 native heap sub
35a0: 73 79 73 74 65 6d 0a 2a 2a 20 77 69 6c 6c 20 63 system.** will c
35b0: 61 75 73 65 20 48 65 61 70 56 61 6c 69 64 61 74 ause HeapValidat
35c0: 65 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 2e 20 e to be called.
35d0: 20 49 66 20 68 65 61 70 20 76 61 6c 69 64 61 74 If heap validat
35e0: 69 6f 6e 20 73 68 6f 75 6c 64 20 66 61 69 6c 2c ion should fail,
35f0: 20 61 6e 0a 2a 2a 20 61 73 73 65 72 74 69 6f 6e an.** assertion
3600: 20 77 69 6c 6c 20 62 65 20 74 72 69 67 67 65 72 will be trigger
3610: 65 64 2e 0a 2a 2a 0a 2a 2a 20 28 48 69 73 74 6f ed..**.** (Histo
3620: 72 69 63 61 6c 20 6e 6f 74 65 3a 20 20 54 68 65 rical note: The
3630: 72 65 20 75 73 65 64 20 74 6f 20 62 65 20 73 65 re used to be se
3640: 76 65 72 61 6c 20 6f 74 68 65 72 20 6f 70 74 69 veral other opti
3650: 6f 6e 73 2c 20 62 75 74 20 77 65 27 76 65 0a 2a ons, but we've.*
3660: 2a 20 70 61 72 65 64 20 69 74 20 64 6f 77 6e 20 * pared it down
3670: 74 6f 20 6a 75 73 74 20 74 68 65 73 65 20 74 68 to just these th
3680: 72 65 65 2e 29 0a 2a 2a 0a 2a 2a 20 49 66 20 6e ree.).**.** If n
3690: 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f 76 65 one of the above
36a0: 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 are defined, th
36b0: 65 6e 20 73 65 74 20 53 51 4c 49 54 45 5f 53 59 en set SQLITE_SY
36c0: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 73 0a 2a STEM_MALLOC as.*
36d0: 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 2a * the default..*
36e0: 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 /.#if defined(SQ
36f0: 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c LITE_SYSTEM_MALL
3700: 4f 43 29 20 5c 0a 20 20 2b 20 64 65 66 69 6e 65 OC) \. + define
3710: 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 4d d(SQLITE_WIN32_M
3720: 41 4c 4c 4f 43 29 20 5c 0a 20 20 2b 20 64 65 66 ALLOC) \. + def
3730: 69 6e 65 64 28 53 51 4c 49 54 45 5f 5a 45 52 4f ined(SQLITE_ZERO
3740: 5f 4d 41 4c 4c 4f 43 29 20 5c 0a 20 20 2b 20 64 _MALLOC) \. + d
3750: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 efined(SQLITE_ME
3760: 4d 44 45 42 55 47 29 3e 31 0a 23 20 65 72 72 6f MDEBUG)>1.# erro
3770: 72 20 22 54 77 6f 20 6f 72 20 6d 6f 72 65 20 6f r "Two or more o
3780: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
3790: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e compile-time con
37a0: 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f figuration optio
37b0: 6e 73 5c 0a 20 61 72 65 20 64 65 66 69 6e 65 64 ns\. are defined
37c0: 20 62 75 74 20 61 74 20 6d 6f 73 74 20 6f 6e 65 but at most one
37d0: 20 69 73 20 61 6c 6c 6f 77 65 64 3a 5c 0a 20 53 is allowed:\. S
37e0: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
37f0: 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33 LOC, SQLITE_WIN3
3800: 32 5f 4d 41 4c 4c 4f 43 2c 20 53 51 4c 49 54 45 2_MALLOC, SQLITE
3810: 5f 4d 45 4d 44 45 42 55 47 2c 5c 0a 20 53 51 4c _MEMDEBUG,\. SQL
3820: 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 22 ITE_ZERO_MALLOC"
3830: 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 .#endif.#if defi
3840: 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 53 54 45 ned(SQLITE_SYSTE
3850: 4d 5f 4d 41 4c 4c 4f 43 29 20 5c 0a 20 20 2b 20 M_MALLOC) \. +
3860: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 defined(SQLITE_W
3870: 49 4e 33 32 5f 4d 41 4c 4c 4f 43 29 20 5c 0a 20 IN32_MALLOC) \.
3880: 20 2b 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 + defined(SQLIT
3890: 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 29 20 5c E_ZERO_MALLOC) \
38a0: 0a 20 20 2b 20 64 65 66 69 6e 65 64 28 53 51 4c . + defined(SQL
38b0: 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 3d 3d 30 ITE_MEMDEBUG)==0
38c0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
38d0: 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 31 _SYSTEM_MALLOC 1
38e0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 .#endif../*.** I
38f0: 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f f SQLITE_MALLOC_
3900: 53 4f 46 54 5f 4c 49 4d 49 54 20 69 73 20 6e 6f SOFT_LIMIT is no
3910: 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 72 79 t zero, then try
3920: 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 to keep the.**
3930: 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 sizes of memory
3940: 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 62 65 6c 6f allocations belo
3950: 77 20 74 68 69 73 20 76 61 6c 75 65 20 77 68 65 w this value whe
3960: 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a re possible..*/.
3970: 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
3980: 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f ITE_MALLOC_SOFT_
3990: 4c 49 4d 49 54 29 0a 23 20 64 65 66 69 6e 65 20 LIMIT).# define
39a0: 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f SQLITE_MALLOC_SO
39b0: 46 54 5f 4c 49 4d 49 54 20 31 30 32 34 0a 23 65 FT_LIMIT 1024.#e
39c0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 6e ndif../*.** We n
39d0: 65 65 64 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 eed to define _X
39e0: 4f 50 45 4e 5f 53 4f 55 52 43 45 20 61 73 20 66 OPEN_SOURCE as f
39f0: 6f 6c 6c 6f 77 73 20 69 6e 20 6f 72 64 65 72 20 ollows in order
3a00: 74 6f 20 65 6e 61 62 6c 65 0a 2a 2a 20 72 65 63 to enable.** rec
3a10: 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 6f ursive mutexes o
3a20: 6e 20 6d 6f 73 74 20 55 6e 69 78 20 73 79 73 74 n most Unix syst
3a30: 65 6d 73 2e 20 20 42 75 74 20 4d 61 63 20 4f 53 ems. But Mac OS
3a40: 20 58 20 69 73 20 64 69 66 66 65 72 65 6e 74 2e X is different.
3a50: 0a 2a 2a 20 54 68 65 20 5f 58 4f 50 45 4e 5f 53 .** The _XOPEN_S
3a60: 4f 55 52 43 45 20 64 65 66 69 6e 65 20 63 61 75 OURCE define cau
3a70: 73 65 73 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 ses problems for
3a80: 20 4d 61 63 20 4f 53 20 58 20 77 65 20 61 72 65 Mac OS X we are
3a90: 20 74 6f 6c 64 2c 0a 2a 2a 20 73 6f 20 69 74 20 told,.** so it
3aa0: 69 73 20 6f 6d 69 74 74 65 64 20 74 68 65 72 65 is omitted there
3ab0: 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 32 . See ticket #2
3ac0: 36 37 33 2e 0a 2a 2a 0a 2a 2a 20 4c 61 74 65 72 673..**.** Later
3ad0: 20 77 65 20 6c 65 61 72 6e 20 74 68 61 74 20 5f we learn that _
3ae0: 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 69 73 20 XOPEN_SOURCE is
3af0: 70 6f 6f 72 6c 79 20 6f 72 20 69 6e 63 6f 72 72 poorly or incorr
3b00: 65 63 74 6c 79 0a 2a 2a 20 69 6d 70 6c 65 6d 65 ectly.** impleme
3b10: 6e 74 65 64 20 6f 6e 20 73 6f 6d 65 20 73 79 73 nted on some sys
3b20: 74 65 6d 73 2e 20 20 53 6f 20 77 65 20 61 76 6f tems. So we avo
3b30: 69 64 20 64 65 66 69 6e 69 6e 67 20 69 74 20 61 id defining it a
3b40: 74 20 61 6c 6c 0a 2a 2a 20 69 66 20 69 74 20 69 t all.** if it i
3b50: 73 20 61 6c 72 65 61 64 79 20 64 65 66 69 6e 65 s already define
3b60: 64 20 6f 72 20 69 66 20 69 74 20 69 73 20 75 6e d or if it is un
3b70: 6e 65 65 64 65 64 20 62 65 63 61 75 73 65 20 77 needed because w
3b80: 65 20 61 72 65 0a 2a 2a 20 6e 6f 74 20 64 6f 69 e are.** not doi
3b90: 6e 67 20 61 20 74 68 72 65 61 64 73 61 66 65 20 ng a threadsafe
3ba0: 62 75 69 6c 64 2e 20 20 54 69 63 6b 65 74 20 23 build. Ticket #
3bb0: 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 2681..**.** See
3bc0: 61 6c 73 6f 20 74 69 63 6b 65 74 20 23 32 37 34 also ticket #274
3bd0: 31 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 1..*/.#if !defin
3be0: 65 64 28 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 ed(_XOPEN_SOURCE
3bf0: 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f ) && !defined(__
3c00: 44 41 52 57 49 4e 5f 5f 29 20 26 26 20 21 64 65 DARWIN__) && !de
3c10: 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
3c20: 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 && SQLITE_THREA
3c30: 44 53 41 46 45 0a 23 20 20 64 65 66 69 6e 65 20 DSAFE.# define
3c40: 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 35 30 _XOPEN_SOURCE 50
3c50: 30 20 20 2f 2a 20 4e 65 65 64 65 64 20 74 6f 20 0 /* Needed to
3c60: 65 6e 61 62 6c 65 20 70 74 68 72 65 61 64 20 72 enable pthread r
3c70: 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 ecursive mutexes
3c80: 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a */.#endif../*.*
3c90: 2a 20 54 68 65 20 54 43 4c 20 68 65 61 64 65 72 * The TCL header
3ca0: 73 20 61 72 65 20 6f 6e 6c 79 20 6e 65 65 64 65 s are only neede
3cb0: 64 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 d when compiling
3cc0: 20 74 68 65 20 54 43 4c 20 62 69 6e 64 69 6e 67 the TCL binding
3cd0: 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 s..*/.#if define
3ce0: 64 28 53 51 4c 49 54 45 5f 54 43 4c 29 20 7c 7c d(SQLITE_TCL) ||
3cf0: 20 64 65 66 69 6e 65 64 28 54 43 4c 53 48 29 0a defined(TCLSH).
3d00: 23 20 69 6e 63 6c 75 64 65 20 3c 74 63 6c 2e 68 # include <tcl.h
3d10: 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 >.#endif../*.**
3d20: 4e 44 45 42 55 47 20 61 6e 64 20 53 51 4c 49 54 NDEBUG and SQLIT
3d30: 45 5f 44 45 42 55 47 20 61 72 65 20 6f 70 70 6f E_DEBUG are oppo
3d40: 73 69 74 65 73 2e 20 20 49 74 20 73 68 6f 75 6c sites. It shoul
3d50: 64 20 61 6c 77 61 79 73 20 62 65 20 74 72 75 65 d always be true
3d60: 20 74 68 61 74 0a 2a 2a 20 64 65 66 69 6e 65 64 that.** defined
3d70: 28 4e 44 45 42 55 47 29 3d 3d 21 64 65 66 69 6e (NDEBUG)==!defin
3d80: 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
3d90: 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f . If this is no
3da0: 74 20 63 75 72 72 65 6e 74 6c 79 20 74 72 75 65 t currently true
3db0: 2c 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 74 72 75 ,.** make it tru
3dc0: 65 20 62 79 20 64 65 66 69 6e 69 6e 67 20 6f 72 e by defining or
3dd0: 20 75 6e 64 65 66 69 6e 69 6e 67 20 4e 44 45 42 undefining NDEB
3de0: 55 47 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e UG..**.** Settin
3df0: 67 20 4e 44 45 42 55 47 20 6d 61 6b 65 73 20 74 g NDEBUG makes t
3e00: 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c 65 72 20 he code smaller
3e10: 61 6e 64 20 72 75 6e 20 66 61 73 74 65 72 20 62 and run faster b
3e20: 79 20 64 69 73 61 62 6c 69 6e 67 20 74 68 65 0a y disabling the.
3e30: 2a 2a 20 6e 75 6d 62 65 72 20 61 73 73 65 72 74 ** number assert
3e40: 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e () statements in
3e50: 20 74 68 65 20 63 6f 64 65 2e 20 20 53 6f 20 77 the code. So w
3e60: 65 20 77 61 6e 74 20 74 68 65 20 64 65 66 61 75 e want the defau
3e70: 6c 74 20 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 lt action.** to
3e80: 62 65 20 66 6f 72 20 4e 44 45 42 55 47 20 74 6f be for NDEBUG to
3e90: 20 62 65 20 73 65 74 20 61 6e 64 20 4e 44 45 42 be set and NDEB
3ea0: 55 47 20 74 6f 20 62 65 20 75 6e 64 65 66 69 6e UG to be undefin
3eb0: 65 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 ed only if SQLIT
3ec0: 45 5f 44 45 42 55 47 0a 2a 2a 20 69 73 20 73 65 E_DEBUG.** is se
3ed0: 74 2e 20 20 54 68 75 73 20 4e 44 45 42 55 47 20 t. Thus NDEBUG
3ee0: 62 65 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69 becomes an opt-i
3ef0: 6e 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 6e n rather than an
3f00: 20 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 opt-out.** feat
3f10: 75 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 ure..*/.#if !def
3f20: 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 ined(NDEBUG) &&
3f30: 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
3f40: 44 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65 DEBUG) .# define
3f50: 20 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66 NDEBUG 1.#endif
3f60: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 4e 44 45 .#if defined(NDE
3f70: 42 55 47 29 20 26 26 20 64 65 66 69 6e 65 64 28 BUG) && defined(
3f80: 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 23 20 SQLITE_DEBUG).#
3f90: 75 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 65 6e undef NDEBUG.#en
3fa0: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 dif../*.** The t
3fb0: 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 estcase() macro
3fc0: 69 73 20 75 73 65 64 20 74 6f 20 61 69 64 20 69 is used to aid i
3fd0: 6e 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 n coverage testi
3fe0: 6e 67 2e 20 20 57 68 65 6e 20 0a 2a 2a 20 64 6f ng. When .** do
3ff0: 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 ing coverage tes
4000: 74 69 6e 67 2c 20 74 68 65 20 63 6f 6e 64 69 74 ting, the condit
4010: 69 6f 6e 20 69 6e 73 69 64 65 20 74 68 65 20 61 ion inside the a
4020: 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 65 rgument to.** te
4030: 73 74 63 61 73 65 28 29 20 6d 75 73 74 20 62 65 stcase() must be
4040: 20 65 76 61 6c 75 61 74 65 64 20 62 6f 74 68 20 evaluated both
4050: 74 72 75 65 20 61 6e 64 20 66 61 6c 73 65 20 69 true and false i
4060: 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 67 65 n order to.** ge
4070: 74 20 66 75 6c 6c 20 62 72 61 6e 63 68 20 63 6f t full branch co
4080: 76 65 72 61 67 65 2e 20 20 54 68 65 20 74 65 73 verage. The tes
4090: 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73 tcase() macro is
40a0: 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 74 6f 20 inserted.** to
40b0: 68 65 6c 70 20 65 6e 73 75 72 65 20 61 64 65 71 help ensure adeq
40c0: 75 61 74 65 20 74 65 73 74 20 63 6f 76 65 72 61 uate test covera
40d0: 67 65 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 ge in places whe
40e0: 72 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6e re simple.** con
40f0: 64 69 74 69 6f 6e 2f 64 65 63 69 73 69 6f 6e 20 dition/decision
4100: 63 6f 76 65 72 61 67 65 20 69 73 20 69 6e 61 64 coverage is inad
4110: 65 71 75 61 74 65 2e 20 20 46 6f 72 20 65 78 61 equate. For exa
4120: 6d 70 6c 65 2c 20 74 65 73 74 63 61 73 65 28 29 mple, testcase()
4130: 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 .** can be used
4140: 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 62 6f 75 to make sure bou
4150: 6e 64 61 72 79 20 76 61 6c 75 65 73 20 61 72 65 ndary values are
4160: 20 74 65 73 74 65 64 2e 20 20 46 6f 72 0a 2a 2a tested. For.**
4170: 20 62 69 74 6d 61 73 6b 20 74 65 73 74 73 2c 20 bitmask tests,
4180: 74 65 73 74 63 61 73 65 28 29 20 63 61 6e 20 62 testcase() can b
4190: 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 e used to make s
41a0: 75 72 65 20 65 61 63 68 20 62 69 74 0a 2a 2a 20 ure each bit.**
41b0: 69 73 20 73 69 67 6e 69 66 69 63 61 6e 74 20 61 is significant a
41c0: 6e 64 20 75 73 65 64 20 61 74 20 6c 65 61 73 74 nd used at least
41d0: 20 6f 6e 63 65 2e 20 20 4f 6e 20 73 77 69 74 63 once. On switc
41e0: 68 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 h statements.**
41f0: 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65 20 63 where multiple c
4200: 61 73 65 73 20 67 6f 20 74 6f 20 74 68 65 20 73 ases go to the s
4210: 61 6d 65 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 ame block of cod
4220: 65 2c 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a e, testcase().**
4230: 20 63 61 6e 20 69 6e 73 75 72 65 20 74 68 61 74 can insure that
4240: 20 61 6c 6c 20 63 61 73 65 73 20 61 72 65 20 65 all cases are e
4250: 76 61 6c 75 61 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a valuated..**.*/.
4260: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f #ifdef SQLITE_CO
4270: 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 VERAGE_TEST.SQLI
4280: 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
4290: 64 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 d sqlite3Coverag
42a0: 65 28 69 6e 74 29 3b 0a 23 20 64 65 66 69 6e 65 e(int);.# define
42b0: 20 74 65 73 74 63 61 73 65 28 58 29 20 20 69 66 testcase(X) if
42c0: 28 20 58 20 29 7b 20 73 71 6c 69 74 65 33 43 6f ( X ){ sqlite3Co
42d0: 76 65 72 61 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29 verage(__LINE__)
42e0: 3b 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 ; }.#else.# defi
42f0: 6e 65 20 74 65 73 74 63 61 73 65 28 58 29 0a 23 ne testcase(X).#
4300: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
4310: 20 54 45 53 54 4f 4e 4c 59 20 6d 61 63 72 6f 20 TESTONLY macro
4320: 69 73 20 75 73 65 64 20 74 6f 20 65 6e 63 6c 6f is used to enclo
4330: 73 65 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c se variable decl
4340: 61 72 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 6f arations or.** o
4350: 74 68 65 72 20 62 69 74 73 20 6f 66 20 63 6f 64 ther bits of cod
4360: 65 20 74 68 61 74 20 61 72 65 20 6e 65 65 64 65 e that are neede
4370: 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 65 d to support the
4380: 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 77 69 arguments.** wi
4390: 74 68 69 6e 20 74 65 73 74 63 61 73 65 28 29 20 thin testcase()
43a0: 61 6e 64 20 61 73 73 65 72 74 28 29 20 6d 61 63 and assert() mac
43b0: 72 6f 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 ros..*/.#if !def
43c0: 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 ined(NDEBUG) ||
43d0: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 defined(SQLITE_C
43e0: 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a 23 20 OVERAGE_TEST).#
43f0: 64 65 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28 define TESTONLY(
4400: 58 29 20 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 X) X.#else.# de
4410: 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 fine TESTONLY(X)
4420: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 .#endif../*.** S
4430: 6f 6d 65 74 69 6d 65 73 20 77 65 20 6e 65 65 64 ometimes we need
4440: 20 61 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 a small amount
4450: 6f 66 20 63 6f 64 65 20 73 75 63 68 20 61 73 20 of code such as
4460: 61 20 76 61 72 69 61 62 6c 65 20 69 6e 69 74 69 a variable initi
4470: 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 alization.** to
4480: 73 65 74 75 70 20 66 6f 72 20 61 20 6c 61 74 65 setup for a late
4490: 72 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 r assert() state
44a0: 6d 65 6e 74 2e 20 20 57 65 20 64 6f 20 6e 6f 74 ment. We do not
44b0: 20 77 61 6e 74 20 74 68 69 73 20 63 6f 64 65 20 want this code
44c0: 74 6f 0a 2a 2a 20 61 70 70 65 61 72 20 77 68 65 to.** appear whe
44d0: 6e 20 61 73 73 65 72 74 28 29 20 69 73 20 64 69 n assert() is di
44e0: 73 61 62 6c 65 64 2e 20 20 54 68 65 20 66 6f 6c sabled. The fol
44f0: 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 lowing macro is
4500: 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 75 73 65 therefore.** use
4510: 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 61 d to contain tha
4520: 74 20 73 65 74 75 70 20 63 6f 64 65 2e 20 20 54 t setup code. T
4530: 68 65 20 22 56 56 41 22 20 61 63 72 6f 6e 79 6d he "VVA" acronym
4540: 20 73 74 61 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 stands for.** "
4550: 56 65 72 69 66 69 63 61 74 69 6f 6e 2c 20 56 61 Verification, Va
4560: 6c 69 64 61 74 69 6f 6e 2c 20 61 6e 64 20 41 63 lidation, and Ac
4570: 63 72 65 64 69 74 61 74 69 6f 6e 22 2e 20 20 49 creditation". I
4580: 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 n other words, t
4590: 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69 74 68 69 he.** code withi
45a0: 6e 20 56 56 41 5f 4f 4e 4c 59 28 29 20 77 69 6c n VVA_ONLY() wil
45b0: 6c 20 6f 6e 6c 79 20 72 75 6e 20 64 75 72 69 6e l only run durin
45c0: 67 20 76 65 72 69 66 69 63 61 74 69 6f 6e 20 70 g verification p
45d0: 72 6f 63 65 73 73 65 73 2e 0a 2a 2f 0a 23 69 66 rocesses..*/.#if
45e0: 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 20 64 65 ndef NDEBUG.# de
45f0: 66 69 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 58 29 fine VVA_ONLY(X)
4600: 20 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 X.#else.# defi
4610: 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 58 29 0a 23 ne VVA_ONLY(X).#
4620: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
4630: 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 ALWAYS and NEVE
4640: 52 20 6d 61 63 72 6f 73 20 73 75 72 72 6f 75 6e R macros surroun
4650: 64 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 d boolean expres
4660: 73 69 6f 6e 73 20 77 68 69 63 68 20 0a 2a 2a 20 sions which .**
4670: 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 are intended to
4680: 61 6c 77 61 79 73 20 62 65 20 74 72 75 65 20 6f always be true o
4690: 72 20 66 61 6c 73 65 2c 20 72 65 73 70 65 63 74 r false, respect
46a0: 69 76 65 6c 79 2e 20 20 53 75 63 68 0a 2a 2a 20 ively. Such.**
46b0: 65 78 70 72 65 73 73 69 6f 6e 73 20 63 6f 75 6c expressions coul
46c0: 64 20 62 65 20 6f 6d 69 74 74 65 64 20 66 72 6f d be omitted fro
46d0: 6d 20 74 68 65 20 63 6f 64 65 20 63 6f 6d 70 6c m the code compl
46e0: 65 74 65 6c 79 2e 20 20 42 75 74 20 74 68 65 79 etely. But they
46f0: 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64 .** are included
4700: 20 69 6e 20 61 20 66 65 77 20 63 61 73 65 73 20 in a few cases
4710: 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 68 61 in order to enha
4720: 6e 63 65 20 74 68 65 20 72 65 73 69 6c 69 65 6e nce the resilien
4730: 63 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 ce.** of SQLite
4740: 74 6f 20 75 6e 65 78 70 65 63 74 65 64 20 62 65 to unexpected be
4750: 68 61 76 69 6f 72 20 2d 20 74 6f 20 6d 61 6b 65 havior - to make
4760: 20 74 68 65 20 63 6f 64 65 20 22 73 65 6c 66 2d the code "self-
4770: 68 65 61 6c 69 6e 67 22 0a 2a 2a 20 6f 72 20 22 healing".** or "
4780: 64 75 63 74 69 6c 65 22 20 72 61 74 68 65 72 20 ductile" rather
4790: 74 68 61 6e 20 62 65 69 6e 67 20 22 62 72 69 74 than being "brit
47a0: 74 6c 65 22 20 61 6e 64 20 63 72 61 73 68 69 6e tle" and crashin
47b0: 67 20 61 74 20 74 68 65 20 66 69 72 73 74 0a 2a g at the first.*
47c0: 2a 20 68 69 6e 74 20 6f 66 20 75 6e 70 6c 61 6e * hint of unplan
47d0: 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a ned behavior..**
47e0: 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 .** In other wor
47f0: 64 73 2c 20 41 4c 57 41 59 53 20 61 6e 64 20 4e ds, ALWAYS and N
4800: 45 56 45 52 20 61 72 65 20 61 64 64 65 64 20 66 EVER are added f
4810: 6f 72 20 64 65 66 65 6e 73 69 76 65 20 63 6f 64 or defensive cod
4820: 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f e..**.** When do
4830: 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 ing coverage tes
4840: 74 69 6e 67 20 41 4c 57 41 59 53 20 61 6e 64 20 ting ALWAYS and
4850: 4e 45 56 45 52 20 61 72 65 20 68 61 72 64 2d 63 NEVER are hard-c
4860: 6f 64 65 64 20 74 6f 0a 2a 2a 20 62 65 20 74 72 oded to.** be tr
4870: 75 65 20 61 6e 64 20 66 61 6c 73 65 20 73 6f 20 ue and false so
4880: 74 68 61 74 20 74 68 65 20 75 6e 72 65 61 63 68 that the unreach
4890: 61 62 6c 65 20 63 6f 64 65 20 74 68 65 6e 20 73 able code then s
48a0: 70 65 63 69 66 79 20 77 69 6c 6c 0a 2a 2a 20 6e pecify will.** n
48b0: 6f 74 20 62 65 20 63 6f 75 6e 74 65 64 20 61 73 ot be counted as
48c0: 20 75 6e 74 65 73 74 65 64 20 63 6f 64 65 2e 0a untested code..
48d0: 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
48e0: 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 QLITE_COVERAGE_T
48f0: 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20 41 4c EST).# define AL
4900: 57 41 59 53 28 58 29 20 20 20 20 20 20 28 31 29 WAYS(X) (1)
4910: 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52 28 .# define NEVER(
4920: 58 29 20 20 20 20 20 20 20 28 30 29 0a 23 65 6c X) (0).#el
4930: 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 if !defined(NDEB
4940: 55 47 29 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 UG).# define ALW
4950: 41 59 53 28 58 29 20 20 20 20 20 20 28 28 58 29 AYS(X) ((X)
4960: 3f 31 3a 28 61 73 73 65 72 74 28 30 29 2c 30 29 ?1:(assert(0),0)
4970: 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52 ).# define NEVER
4980: 28 58 29 20 20 20 20 20 20 20 28 28 58 29 3f 28 (X) ((X)?(
4990: 61 73 73 65 72 74 28 30 29 2c 31 29 3a 30 29 0a assert(0),1):0).
49a0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 41 #else.# define A
49b0: 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 28 58 LWAYS(X) (X
49c0: 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52 ).# define NEVER
49d0: 28 58 29 20 20 20 20 20 20 20 28 58 29 0a 23 65 (X) (X).#e
49e0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
49f0: 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 rn true (non-zer
4a00: 6f 29 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 o) if the input
4a10: 69 73 20 61 20 69 6e 74 65 67 65 72 20 74 68 61 is a integer tha
4a20: 74 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 0a 2a t is too large.*
4a30: 2a 20 74 6f 20 66 69 74 20 69 6e 20 33 32 2d 62 * to fit in 32-b
4a40: 69 74 73 2e 20 20 54 68 69 73 20 6d 61 63 72 6f its. This macro
4a50: 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 is used inside
4a60: 6f 66 20 76 61 72 69 6f 75 73 20 74 65 73 74 63 of various testc
4a70: 61 73 65 28 29 0a 2a 2a 20 6d 61 63 72 6f 73 20 ase().** macros
4a80: 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 77 to verify that w
4a90: 65 20 68 61 76 65 20 74 65 73 74 65 64 20 53 51 e have tested SQ
4aa0: 4c 69 74 65 20 66 6f 72 20 6c 61 72 67 65 2d 66 Lite for large-f
4ab0: 69 6c 65 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a ile support..*/.
4ac0: 23 64 65 66 69 6e 65 20 49 53 5f 42 49 47 5f 49 #define IS_BIG_I
4ad0: 4e 54 28 58 29 20 20 28 28 28 58 29 26 7e 28 69 NT(X) (((X)&~(i
4ae0: 36 34 29 30 78 66 66 66 66 66 66 66 66 29 21 3d 64)0xffffffff)!=
4af0: 30 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 0)../*.** The ma
4b00: 63 72 6f 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69 cro unlikely() i
4b10: 73 20 61 20 68 69 6e 74 20 74 68 61 74 20 73 75 s a hint that su
4b20: 72 72 6f 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61 rrounds a boolea
4b30: 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 n.** expression
4b40: 74 68 61 74 20 69 73 20 75 73 75 61 6c 6c 79 20 that is usually
4b50: 66 61 6c 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69 false. Macro li
4b60: 6b 65 6c 79 28 29 20 73 75 72 72 6f 75 6e 64 73 kely() surrounds
4b70: 0a 2a 2a 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 .** a boolean ex
4b80: 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 pression that is
4b90: 20 75 73 75 61 6c 6c 79 20 74 72 75 65 2e 20 20 usually true.
4ba0: 47 43 43 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a GCC is able to.*
4bb0: 2a 20 75 73 65 20 74 68 65 73 65 20 68 69 6e 74 * use these hint
4bc0: 73 20 74 6f 20 67 65 6e 65 72 61 74 65 20 62 65 s to generate be
4bd0: 74 74 65 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74 tter code, somet
4be0: 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 imes..*/.#if def
4bf0: 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 ined(__GNUC__) &
4c00: 26 20 30 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b & 0.# define lik
4c10: 65 6c 79 28 58 29 20 20 20 20 5f 5f 62 75 69 6c ely(X) __buil
4c20: 74 69 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 31 tin_expect((X),1
4c30: 29 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b ).# define unlik
4c40: 65 6c 79 28 58 29 20 20 5f 5f 62 75 69 6c 74 69 ely(X) __builti
4c50: 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 30 29 0a n_expect((X),0).
4c60: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 6c #else.# define l
4c70: 69 6b 65 6c 79 28 58 29 20 20 20 20 21 21 28 58 ikely(X) !!(X
4c80: 29 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b ).# define unlik
4c90: 65 6c 79 28 58 29 20 20 21 21 28 58 29 0a 23 65 ely(X) !!(X).#e
4ca0: 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
4cb0: 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 ***** Include sq
4cc0: 6c 69 74 65 33 2e 68 20 69 6e 20 74 68 65 20 6d lite3.h in the m
4cd0: 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 iddle of sqliteI
4ce0: 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
4cf0: 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
4d00: 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
4d10: 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a sqlite3.h *****
4d20: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4d30: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4d40: 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
4d50: 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
4d60: 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
4d70: 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
4d80: 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
4d90: 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
4da0: 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
4db0: 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
4dc0: 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
4dd0: 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
4de0: 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
4df0: 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
4e00: 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
4e10: 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
4e20: 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
4e30: 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
4e40: 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
4e50: 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
4e60: 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
4e70: 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
4e80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e90: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4ea0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4eb0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
4ec0: 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 This header file
4ed0: 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 defines the int
4ee0: 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 erface that the
4ef0: 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 0a 2a SQLite library.*
4f00: 2a 20 70 72 65 73 65 6e 74 73 20 74 6f 20 63 6c * presents to cl
4f10: 69 65 6e 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 ient programs.
4f20: 49 66 20 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c If a C-function,
4f30: 20 73 74 72 75 63 74 75 72 65 2c 20 64 61 74 61 structure, data
4f40: 74 79 70 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 type,.** or cons
4f50: 74 61 6e 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 tant definition
4f60: 64 6f 65 73 20 6e 6f 74 20 61 70 70 65 61 72 20 does not appear
4f70: 69 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 in this file, th
4f80: 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 en it is.** not
4f90: 61 20 70 75 62 6c 69 73 68 65 64 20 41 50 49 20 a published API
4fa0: 6f 66 20 53 51 4c 69 74 65 2c 20 69 73 20 73 75 of SQLite, is su
4fb0: 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 bject to change
4fc0: 77 69 74 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 without.** notic
4fd0: 65 2c 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f e, and should no
4fe0: 74 20 62 65 20 72 65 66 65 72 65 6e 63 65 64 20 t be referenced
4ff0: 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 by programs that
5000: 20 75 73 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a use SQLite..**.
5010: 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 ** Some of the d
5020: 65 66 69 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 efinitions that
5030: 61 72 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 are in this file
5040: 20 61 72 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a are marked as.*
5050: 2a 20 22 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 * "experimental"
5060: 2e 20 20 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 . Experimental
5070: 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 6e interfaces are n
5080: 6f 72 6d 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 ormally new.** f
5090: 65 61 74 75 72 65 73 20 72 65 63 65 6e 74 6c 79 eatures recently
50a0: 20 61 64 64 65 64 20 74 6f 20 53 51 4c 69 74 65 added to SQLite
50b0: 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 . We do not ant
50c0: 69 63 69 70 61 74 65 20 63 68 61 6e 67 65 73 0a icipate changes.
50d0: 2a 2a 20 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 ** to experiment
50e0: 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 62 75 al interfaces bu
50f0: 74 20 72 65 73 65 72 76 65 20 74 68 65 20 72 69 t reserve the ri
5100: 67 68 74 20 74 6f 20 6d 61 6b 65 20 6d 69 6e 6f ght to make mino
5110: 72 20 63 68 61 6e 67 65 73 0a 2a 2a 20 69 66 20 r changes.** if
5120: 65 78 70 65 72 69 65 6e 63 65 20 66 72 6f 6d 20 experience from
5130: 75 73 65 20 22 69 6e 20 74 68 65 20 77 69 6c 64 use "in the wild
5140: 22 20 73 75 67 67 65 73 74 20 73 75 63 68 20 63 " suggest such c
5150: 68 61 6e 67 65 73 20 61 72 65 20 70 72 75 64 65 hanges are prude
5160: 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 66 nt..**.** The of
5170: 66 69 63 69 61 6c 20 43 2d 6c 61 6e 67 75 61 67 ficial C-languag
5180: 65 20 41 50 49 20 64 6f 63 75 6d 65 6e 74 61 74 e API documentat
5190: 69 6f 6e 20 66 6f 72 20 53 51 4c 69 74 65 20 69 ion for SQLite i
51a0: 73 20 64 65 72 69 76 65 64 0a 2a 2a 20 66 72 6f s derived.** fro
51b0: 6d 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 m comments in th
51c0: 69 73 20 66 69 6c 65 2e 20 20 54 68 69 73 20 66 is file. This f
51d0: 69 6c 65 20 69 73 20 74 68 65 20 61 75 74 68 6f ile is the autho
51e0: 72 69 74 61 74 69 76 65 20 73 6f 75 72 63 65 0a ritative source.
51f0: 2a 2a 20 6f 6e 20 68 6f 77 20 53 51 4c 69 74 65 ** on how SQLite
5200: 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 interfaces are
5210: 73 75 70 70 6f 73 65 20 74 6f 20 6f 70 65 72 61 suppose to opera
5220: 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 te..**.** The na
5230: 6d 65 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 me of this file
5240: 75 6e 64 65 72 20 63 6f 6e 66 69 67 75 72 61 74 under configurat
5250: 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 20 69 ion management i
5260: 73 20 22 73 71 6c 69 74 65 2e 68 2e 69 6e 22 2e s "sqlite.h.in".
5270: 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 .** The makefile
5280: 20 6d 61 6b 65 73 20 73 6f 6d 65 20 6d 69 6e 6f makes some mino
5290: 72 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 69 r changes to thi
52a0: 73 20 66 69 6c 65 20 28 73 75 63 68 20 61 73 20 s file (such as
52b0: 69 6e 73 65 72 74 69 6e 67 0a 2a 2a 20 74 68 65 inserting.** the
52c0: 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 29 version number)
52d0: 20 61 6e 64 20 63 68 61 6e 67 65 73 20 69 74 73 and changes its
52e0: 20 6e 61 6d 65 20 74 6f 20 22 73 71 6c 69 74 65 name to "sqlite
52f0: 33 2e 68 22 20 61 73 0a 2a 2a 20 70 61 72 74 20 3.h" as.** part
5300: 6f 66 20 74 68 65 20 62 75 69 6c 64 20 70 72 6f of the build pro
5310: 63 65 73 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 cess..*/.#ifndef
5320: 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 64 65 _SQLITE3_H_.#de
5330: 66 69 6e 65 20 5f 53 51 4c 49 54 45 33 5f 48 5f fine _SQLITE3_H_
5340: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 .#include <stdar
5350: 67 2e 68 3e 20 20 20 20 20 2f 2a 20 4e 65 65 64 g.h> /* Need
5360: 65 64 20 66 6f 72 20 74 68 65 20 64 65 66 69 6e ed for the defin
5370: 69 74 69 6f 6e 20 6f 66 20 76 61 5f 6c 69 73 74 ition of va_list
5380: 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 */../*.** Make
5390: 73 75 72 65 20 77 65 20 63 61 6e 20 63 61 6c 6c sure we can call
53a0: 20 74 68 69 73 20 73 74 75 66 66 20 66 72 6f 6d this stuff from
53b0: 20 43 2b 2b 2e 0a 2a 2f 0a 23 69 66 20 30 0a 65 C++..*/.#if 0.e
53c0: 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 xtern "C" {.#end
53d0: 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 if.../*.** Add t
53e0: 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 6f 76 he ability to ov
53f0: 65 72 72 69 64 65 20 27 65 78 74 65 72 6e 27 0a erride 'extern'.
5400: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
5410: 45 5f 45 58 54 45 52 4e 0a 23 20 64 65 66 69 6e E_EXTERN.# defin
5420: 65 20 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 20 e SQLITE_EXTERN
5430: 65 78 74 65 72 6e 0a 23 65 6e 64 69 66 0a 0a 23 extern.#endif..#
5440: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 50 ifndef SQLITE_AP
5450: 49 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 I.# define SQLIT
5460: 45 5f 41 50 49 0a 23 65 6e 64 69 66 0a 0a 0a 2f E_API.#endif.../
5470: 2a 0a 2a 2a 20 54 68 65 73 65 20 6e 6f 2d 6f 70 *.** These no-op
5480: 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 65 64 macros are used
5490: 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 69 6e 74 in front of int
54a0: 65 72 66 61 63 65 73 20 74 6f 20 6d 61 72 6b 20 erfaces to mark
54b0: 74 68 6f 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 those.** interfa
54c0: 63 65 73 20 61 73 20 65 69 74 68 65 72 20 64 65 ces as either de
54d0: 70 72 65 63 61 74 65 64 20 6f 72 20 65 78 70 65 precated or expe
54e0: 72 69 6d 65 6e 74 61 6c 2e 20 20 4e 65 77 20 61 rimental. New a
54f0: 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 pplications.** s
5500: 68 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20 64 65 hould not use de
5510: 70 72 65 63 61 74 65 64 20 69 6e 74 65 72 66 61 precated interfa
5520: 63 65 73 20 2d 20 74 68 65 79 20 61 72 65 20 73 ces - they are s
5530: 75 70 70 6f 72 74 20 66 6f 72 20 62 61 63 6b 77 upport for backw
5540: 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 ards.** compatib
5550: 69 6c 69 74 79 20 6f 6e 6c 79 2e 20 20 41 70 70 ility only. App
5560: 6c 69 63 61 74 69 6f 6e 20 77 72 69 74 65 72 73 lication writers
5570: 20 73 68 6f 75 6c 64 20 62 65 20 61 77 61 72 65 should be aware
5580: 20 74 68 61 74 0a 2a 2a 20 65 78 70 65 72 69 6d that.** experim
5590: 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73 ental interfaces
55a0: 20 61 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 are subject to
55b0: 63 68 61 6e 67 65 20 69 6e 20 70 6f 69 6e 74 20 change in point
55c0: 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 releases..**.**
55d0: 54 68 65 73 65 20 6d 61 63 72 6f 73 20 75 73 65 These macros use
55e0: 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 6f 20 d to resolve to
55f0: 76 61 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 various kinds of
5600: 20 63 6f 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 compiler magic
5610: 74 68 61 74 0a 2a 2a 20 77 6f 75 6c 64 20 67 65 that.** would ge
5620: 6e 65 72 61 74 65 20 77 61 72 6e 69 6e 67 20 6d nerate warning m
5630: 65 73 73 61 67 65 73 20 77 68 65 6e 20 74 68 65 essages when the
5640: 79 20 77 65 72 65 20 75 73 65 64 2e 20 20 42 75 y were used. Bu
5650: 74 20 74 68 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c t that.** compil
5660: 65 72 20 6d 61 67 69 63 20 65 6e 64 65 64 20 75 er magic ended u
5670: 70 20 67 65 6e 65 72 61 74 69 6e 67 20 73 75 63 p generating suc
5680: 68 20 61 20 66 6c 75 72 72 79 20 6f 66 20 62 75 h a flurry of bu
5690: 67 20 72 65 70 6f 72 74 73 0a 2a 2a 20 74 68 61 g reports.** tha
56a0: 74 20 77 65 20 68 61 76 65 20 74 61 6b 65 6e 20 t we have taken
56b0: 69 74 20 61 6c 6c 20 6f 75 74 20 61 6e 64 20 67 it all out and g
56c0: 6f 6e 65 20 62 61 63 6b 20 74 6f 20 75 73 69 6e one back to usin
56d0: 67 20 73 69 6d 70 6c 65 0a 2a 2a 20 6e 6f 6f 70 g simple.** noop
56e0: 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 macros..*/.#def
56f0: 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 50 52 45 ine SQLITE_DEPRE
5700: 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20 53 51 CATED.#define SQ
5710: 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
5720: 4c 0a 0a 2f 2a 0a 2a 2a 20 45 6e 73 75 72 65 20 L../*.** Ensure
5730: 74 68 65 73 65 20 73 79 6d 62 6f 6c 73 20 77 65 these symbols we
5740: 72 65 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 62 re not defined b
5750: 79 20 73 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 y some previous
5760: 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a header file..*/.
5770: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45 #ifdef SQLITE_VE
5780: 52 53 49 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51 RSION.# undef SQ
5790: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e LITE_VERSION.#en
57a0: 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
57b0: 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 E_VERSION_NUMBER
57c0: 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f .# undef SQLITE_
57d0: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 VERSION_NUMBER.#
57e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 endif../*.** CAP
57f0: 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 I3REF: Compile-T
5800: 69 6d 65 20 4c 69 62 72 61 72 79 20 56 65 72 73 ime Library Vers
5810: 69 6f 6e 20 4e 75 6d 62 65 72 73 0a 2a 2a 0a 2a ion Numbers.**.*
5820: 2a 20 5e 28 54 68 65 20 5b 53 51 4c 49 54 45 5f * ^(The [SQLITE_
5830: 56 45 52 53 49 4f 4e 5d 20 43 20 70 72 65 70 72 VERSION] C prepr
5840: 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 69 6e ocessor macro in
5850: 20 74 68 65 20 73 71 6c 69 74 65 33 2e 68 20 68 the sqlite3.h h
5860: 65 61 64 65 72 0a 2a 2a 20 65 76 61 6c 75 61 74 eader.** evaluat
5870: 65 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6c es to a string l
5880: 69 74 65 72 61 6c 20 74 68 61 74 20 69 73 20 74 iteral that is t
5890: 68 65 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f he SQLite versio
58a0: 6e 20 69 6e 20 74 68 65 0a 2a 2a 20 66 6f 72 6d n in the.** form
58b0: 61 74 20 22 58 2e 59 2e 5a 22 20 77 68 65 72 65 at "X.Y.Z" where
58c0: 20 58 20 69 73 20 74 68 65 20 6d 61 6a 6f 72 20 X is the major
58d0: 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 28 version number (
58e0: 61 6c 77 61 79 73 20 33 20 66 6f 72 0a 2a 2a 20 always 3 for.**
58f0: 53 51 4c 69 74 65 33 29 20 61 6e 64 20 59 20 69 SQLite3) and Y i
5900: 73 20 74 68 65 20 6d 69 6e 6f 72 20 76 65 72 73 s the minor vers
5910: 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 5a ion number and Z
5920: 20 69 73 20 74 68 65 20 72 65 6c 65 61 73 65 20 is the release
5930: 6e 75 6d 62 65 72 2e 29 5e 0a 2a 2a 20 5e 28 54 number.)^.** ^(T
5940: 68 65 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 he [SQLITE_VERSI
5950: 4f 4e 5f 4e 55 4d 42 45 52 5d 20 43 20 70 72 65 ON_NUMBER] C pre
5960: 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 processor macro
5970: 72 65 73 6f 6c 76 65 73 20 74 6f 20 61 6e 20 69 resolves to an i
5980: 6e 74 65 67 65 72 0a 2a 2a 20 77 69 74 68 20 74 nteger.** with t
5990: 68 65 20 76 61 6c 75 65 20 28 58 2a 31 30 30 30 he value (X*1000
59a0: 30 30 30 20 2b 20 59 2a 31 30 30 30 20 2b 20 5a 000 + Y*1000 + Z
59b0: 29 20 77 68 65 72 65 20 58 2c 20 59 2c 20 61 6e ) where X, Y, an
59c0: 64 20 5a 20 61 72 65 20 74 68 65 20 73 61 6d 65 d Z are the same
59d0: 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 75 73 65 64 .** numbers used
59e0: 20 69 6e 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 in [SQLITE_VERS
59f0: 49 4f 4e 5d 2e 29 5e 0a 2a 2a 20 54 68 65 20 53 ION].)^.** The S
5a00: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 QLITE_VERSION_NU
5a10: 4d 42 45 52 20 66 6f 72 20 61 6e 79 20 67 69 76 MBER for any giv
5a20: 65 6e 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 en release of SQ
5a30: 4c 69 74 65 20 77 69 6c 6c 20 61 6c 73 6f 0a 2a Lite will also.*
5a40: 2a 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e * be larger than
5a50: 20 74 68 65 20 72 65 6c 65 61 73 65 20 66 72 6f the release fro
5a60: 6d 20 77 68 69 63 68 20 69 74 20 69 73 20 64 65 m which it is de
5a70: 72 69 76 65 64 2e 20 20 45 69 74 68 65 72 20 59 rived. Either Y
5a80: 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 68 65 6c 64 will.** be held
5a90: 20 63 6f 6e 73 74 61 6e 74 20 61 6e 64 20 5a 20 constant and Z
5aa0: 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e will be incremen
5ab0: 74 65 64 20 6f 72 20 65 6c 73 65 20 59 20 77 69 ted or else Y wi
5ac0: 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e 74 65 ll be incremente
5ad0: 64 0a 2a 2a 20 61 6e 64 20 5a 20 77 69 6c 6c 20 d.** and Z will
5ae0: 62 65 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f be reset to zero
5af0: 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65 ..**.** Since ve
5b00: 72 73 69 6f 6e 20 33 2e 36 2e 31 38 2c 20 53 51 rsion 3.6.18, SQ
5b10: 4c 69 74 65 20 73 6f 75 72 63 65 20 63 6f 64 65 Lite source code
5b20: 20 68 61 73 20 62 65 65 6e 20 73 74 6f 72 65 64 has been stored
5b30: 20 69 6e 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 in the.** <a hr
5b40: 65 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77 2e 66 ef="http://www.f
5b50: 6f 73 73 69 6c 2d 73 63 6d 2e 6f 72 67 2f 22 3e ossil-scm.org/">
5b60: 46 6f 73 73 69 6c 20 63 6f 6e 66 69 67 75 72 61 Fossil configura
5b70: 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 0a tion management.
5b80: 2a 2a 20 73 79 73 74 65 6d 3c 2f 61 3e 2e 20 20 ** system</a>.
5b90: 5e 54 68 65 20 53 51 4c 49 54 45 5f 53 4f 55 52 ^The SQLITE_SOUR
5ba0: 43 45 5f 49 44 20 6d 61 63 72 6f 20 65 76 61 6c CE_ID macro eval
5bb0: 75 61 74 65 73 20 74 6f 0a 2a 2a 20 61 20 73 74 uates to.** a st
5bc0: 72 69 6e 67 20 77 68 69 63 68 20 69 64 65 6e 74 ring which ident
5bd0: 69 66 69 65 73 20 61 20 70 61 72 74 69 63 75 6c ifies a particul
5be0: 61 72 20 63 68 65 63 6b 2d 69 6e 20 6f 66 20 53 ar check-in of S
5bf0: 51 4c 69 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20 QLite.** within
5c00: 69 74 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f its configuratio
5c10: 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 79 73 n management sys
5c20: 74 65 6d 2e 20 20 5e 54 68 65 20 53 51 4c 49 54 tem. ^The SQLIT
5c30: 45 5f 53 4f 55 52 43 45 5f 49 44 0a 2a 2a 20 73 E_SOURCE_ID.** s
5c40: 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 74 tring contains t
5c50: 68 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 he date and time
5c60: 20 6f 66 20 74 68 65 20 63 68 65 63 6b 2d 69 6e of the check-in
5c70: 20 28 55 54 43 29 20 61 6e 64 20 61 6e 20 53 48 (UTC) and an SH
5c80: 41 31 0a 2a 2a 20 68 61 73 68 20 6f 66 20 74 68 A1.** hash of th
5c90: 65 20 65 6e 74 69 72 65 20 73 6f 75 72 63 65 20 e entire source
5ca0: 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 tree..**.** See
5cb0: 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 6c also: [sqlite3_l
5cc0: 69 62 76 65 72 73 69 6f 6e 28 29 5d 2c 0a 2a 2a ibversion()],.**
5cd0: 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 [sqlite3_libver
5ce0: 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 5d 2c 20 sion_number()],
5cf0: 5b 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 [sqlite3_sourcei
5d00: 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 d()],.** [sqlite
5d10: 5f 76 65 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 _version()] and
5d20: 5b 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 [sqlite_source_i
5d30: 64 28 29 5d 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 d()]..*/.#define
5d40: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 20 SQLITE_VERSION
5d50: 20 20 20 20 20 20 20 22 33 2e 37 2e 31 35 22 0a "3.7.15".
5d60: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 #define SQLITE_V
5d70: 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 33 30 ERSION_NUMBER 30
5d80: 30 37 30 31 35 0a 23 64 65 66 69 6e 65 20 53 51 07015.#define SQ
5d90: 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 20 20 LITE_SOURCE_ID
5da0: 20 20 20 20 22 32 30 31 32 2d 31 32 2d 30 35 20 "2012-12-05
5db0: 31 34 3a 33 31 3a 31 33 20 39 66 36 63 36 38 38 14:31:13 9f6c688
5dc0: 35 36 62 36 39 34 33 37 33 62 37 66 66 62 31 32 56b694373b7ffb12
5dd0: 34 61 62 64 39 39 36 65 35 31 39 62 61 35 39 32 4abd996e519ba592
5de0: 31 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 1"../*.** CAPI3R
5df0: 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 62 EF: Run-Time Lib
5e00: 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75 6d rary Version Num
5e10: 62 65 72 73 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 bers.** KEYWORDS
5e20: 3a 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f : sqlite3_versio
5e30: 6e 2c 20 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 n, sqlite3_sourc
5e40: 65 69 64 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 eid.**.** These
5e50: 69 6e 74 65 72 66 61 63 65 73 20 70 72 6f 76 69 interfaces provi
5e60: 64 65 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f de the same info
5e70: 72 6d 61 74 69 6f 6e 20 61 73 20 74 68 65 20 5b rmation as the [
5e80: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d 2c SQLITE_VERSION],
5e90: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 .** [SQLITE_VERS
5ea0: 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2c 20 61 6e 64 ION_NUMBER], and
5eb0: 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f [SQLITE_SOURCE_
5ec0: 49 44 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 ID] C preprocess
5ed0: 6f 72 20 6d 61 63 72 6f 73 0a 2a 2a 20 62 75 74 or macros.** but
5ee0: 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 20 are associated
5ef0: 77 69 74 68 20 74 68 65 20 6c 69 62 72 61 72 79 with the library
5f00: 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 instead of the
5f10: 68 65 61 64 65 72 20 66 69 6c 65 2e 20 20 5e 28 header file. ^(
5f20: 43 61 75 74 69 6f 75 73 0a 2a 2a 20 70 72 6f 67 Cautious.** prog
5f30: 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 69 6e rammers might in
5f40: 63 6c 75 64 65 20 61 73 73 65 72 74 28 29 20 73 clude assert() s
5f50: 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 tatements in the
5f60: 69 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 ir application t
5f70: 6f 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 61 74 o.** verify that
5f80: 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 values returned
5f90: 20 62 79 20 74 68 65 73 65 20 69 6e 74 65 72 66 by these interf
5fa0: 61 63 65 73 20 6d 61 74 63 68 20 74 68 65 20 6d aces match the m
5fb0: 61 63 72 6f 73 20 69 6e 0a 2a 2a 20 74 68 65 20 acros in.** the
5fc0: 68 65 61 64 65 72 2c 20 61 6e 64 20 74 68 75 73 header, and thus
5fd0: 20 69 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 insure that the
5fe0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 0a application is.
5ff0: 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 ** compiled with
6000: 20 6d 61 74 63 68 69 6e 67 20 6c 69 62 72 61 72 matching librar
6010: 79 20 61 6e 64 20 68 65 61 64 65 72 20 66 69 6c y and header fil
6020: 65 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b es..**.** <block
6030: 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 61 quote><pre>.** a
6040: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6c ssert( sqlite3_l
6050: 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 ibversion_number
6060: 28 29 3d 3d 53 51 4c 49 54 45 5f 56 45 52 53 49 ()==SQLITE_VERSI
6070: 4f 4e 5f 4e 55 4d 42 45 52 20 29 3b 0a 2a 2a 20 ON_NUMBER );.**
6080: 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 73 assert( strcmp(s
6090: 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 qlite3_sourceid(
60a0: 29 2c 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f ),SQLITE_SOURCE_
60b0: 49 44 29 3d 3d 30 20 29 3b 0a 2a 2a 20 61 73 73 ID)==0 );.** ass
60c0: 65 72 74 28 20 73 74 72 63 6d 70 28 73 71 6c 69 ert( strcmp(sqli
60d0: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 te3_libversion()
60e0: 2c 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 29 ,SQLITE_VERSION)
60f0: 3d 3d 30 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e ==0 );.** </pre>
6100: 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 29 5e 0a </blockquote>)^.
6110: 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 **.** ^The sqlit
6120: 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 73 74 72 e3_version[] str
6130: 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 63 6f 6e ing constant con
6140: 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f tains the text o
6150: 66 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f f [SQLITE_VERSIO
6160: 4e 5d 0a 2a 2a 20 6d 61 63 72 6f 2e 20 20 5e 54 N].** macro. ^T
6170: 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 he sqlite3_libve
6180: 72 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e rsion() function
6190: 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
61a0: 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 74 6f 20 er to the.** to
61b0: 74 68 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 the sqlite3_vers
61c0: 69 6f 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e ion[] string con
61d0: 73 74 61 6e 74 2e 20 20 54 68 65 20 73 71 6c 69 stant. The sqli
61e0: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 te3_libversion()
61f0: 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 .** function is
6200: 70 72 6f 76 69 64 65 64 20 66 6f 72 20 75 73 65 provided for use
6210: 20 69 6e 20 44 4c 4c 73 20 73 69 6e 63 65 20 44 in DLLs since D
6220: 4c 4c 20 75 73 65 72 73 20 75 73 75 61 6c 6c 79 LL users usually
6230: 20 64 6f 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 do not have.**
6240: 64 69 72 65 63 74 20 61 63 63 65 73 73 20 74 6f direct access to
6250: 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 string constant
6260: 73 20 77 69 74 68 69 6e 20 74 68 65 20 44 4c 4c s within the DLL
6270: 2e 20 20 5e 54 68 65 0a 2a 2a 20 73 71 6c 69 74 . ^The.** sqlit
6280: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 e3_libversion_nu
6290: 6d 62 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 mber() function
62a0: 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 returns an integ
62b0: 65 72 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20 5b er equal to.** [
62c0: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e SQLITE_VERSION_N
62d0: 55 4d 42 45 52 5d 2e 20 20 5e 54 68 65 20 73 71 UMBER]. ^The sq
62e0: 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 lite3_sourceid()
62f0: 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
6300: 73 20 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 s .** a pointer
6310: 74 6f 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 73 to a string cons
6320: 74 61 6e 74 20 77 68 6f 73 65 20 76 61 6c 75 65 tant whose value
6330: 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
6340: 74 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f the .** [SQLITE_
6350: 53 4f 55 52 43 45 5f 49 44 5d 20 43 20 70 72 65 SOURCE_ID] C pre
6360: 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 2e processor macro.
6370: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
6380: 20 5b 73 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e [sqlite_version
6390: 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 5f ()] and [sqlite_
63a0: 73 6f 75 72 63 65 5f 69 64 28 29 5d 2e 0a 2a 2f source_id()]..*/
63b0: 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
63c0: 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 5f 76 t char sqlite3_v
63d0: 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 51 4c 49 54 ersion[] = SQLIT
63e0: 45 5f 56 45 52 53 49 4f 4e 3b 0a 53 51 4c 49 54 E_VERSION;.SQLIT
63f0: 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
6400: 20 2a 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 *sqlite3_libver
6410: 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 53 51 4c 49 sion(void);.SQLI
6420: 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 TE_API const cha
6430: 72 20 2a 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 r *sqlite3_sourc
6440: 65 69 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 eid(void);.SQLIT
6450: 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
6460: 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 3_libversion_num
6470: 62 65 72 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a ber(void);../*.*
6480: 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d * CAPI3REF: Run-
6490: 54 69 6d 65 20 4c 69 62 72 61 72 79 20 43 6f 6d Time Library Com
64a0: 70 69 6c 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 pilation Options
64b0: 20 44 69 61 67 6e 6f 73 74 69 63 73 0a 2a 2a 0a Diagnostics.**.
64c0: 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f ** ^The sqlite3_
64d0: 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 compileoption_us
64e0: 65 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 ed() function re
64f0: 74 75 72 6e 73 20 30 20 6f 72 20 31 20 0a 2a 2a turns 0 or 1 .**
6500: 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68 65 74 indicating whet
6510: 68 65 72 20 74 68 65 20 73 70 65 63 69 66 69 65 her the specifie
6520: 64 20 6f 70 74 69 6f 6e 20 77 61 73 20 64 65 66 d option was def
6530: 69 6e 65 64 20 61 74 20 0a 2a 2a 20 63 6f 6d 70 ined at .** comp
6540: 69 6c 65 20 74 69 6d 65 2e 20 20 5e 54 68 65 20 ile time. ^The
6550: 53 51 4c 49 54 45 5f 20 70 72 65 66 69 78 20 6d SQLITE_ prefix m
6560: 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20 66 72 ay be omitted fr
6570: 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 70 74 69 6f om the .** optio
6580: 6e 20 6e 61 6d 65 20 70 61 73 73 65 64 20 74 6f n name passed to
6590: 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 sqlite3_compile
65a0: 6f 70 74 69 6f 6e 5f 75 73 65 64 28 29 2e 20 20 option_used().
65b0: 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 .**.** ^The sqli
65c0: 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f te3_compileoptio
65d0: 6e 5f 67 65 74 28 29 20 66 75 6e 63 74 69 6f 6e n_get() function
65e0: 20 61 6c 6c 6f 77 73 20 69 74 65 72 61 74 69 6e allows iteratin
65f0: 67 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20 6c 69 g.** over the li
6600: 73 74 20 6f 66 20 6f 70 74 69 6f 6e 73 20 74 68 st of options th
6610: 61 74 20 77 65 72 65 20 64 65 66 69 6e 65 64 20 at were defined
6620: 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 at compile time
6630: 62 79 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 20 by.** returning
6640: 74 68 65 20 4e 2d 74 68 20 63 6f 6d 70 69 6c 65 the N-th compile
6650: 20 74 69 6d 65 20 6f 70 74 69 6f 6e 20 73 74 72 time option str
6660: 69 6e 67 2e 20 20 5e 49 66 20 4e 20 69 73 20 6f ing. ^If N is o
6670: 75 74 20 6f 66 20 72 61 6e 67 65 2c 0a 2a 2a 20 ut of range,.**
6680: 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f sqlite3_compileo
6690: 70 74 69 6f 6e 5f 67 65 74 28 29 20 72 65 74 75 ption_get() retu
66a0: 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 rns a NULL point
66b0: 65 72 2e 20 20 5e 54 68 65 20 53 51 4c 49 54 45 er. ^The SQLITE
66c0: 5f 20 0a 2a 2a 20 70 72 65 66 69 78 20 69 73 20 _ .** prefix is
66d0: 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 61 6e 79 omitted from any
66e0: 20 73 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 strings returne
66f0: 64 20 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 d by .** sqlite3
6700: 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 _compileoption_g
6710: 65 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 5e 53 75 70 et()..**.** ^Sup
6720: 70 6f 72 74 20 66 6f 72 20 74 68 65 20 64 69 61 port for the dia
6730: 67 6e 6f 73 74 69 63 20 66 75 6e 63 74 69 6f 6e gnostic function
6740: 73 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c s sqlite3_compil
6750: 65 6f 70 74 69 6f 6e 5f 75 73 65 64 28 29 0a 2a eoption_used().*
6760: 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f * and sqlite3_co
6770: 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 mpileoption_get(
6780: 29 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 ) may be omitted
6790: 20 62 79 20 73 70 65 63 69 66 79 69 6e 67 20 74 by specifying t
67a0: 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f he .** [SQLITE_O
67b0: 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f MIT_COMPILEOPTIO
67c0: 4e 5f 44 49 41 47 53 5d 20 6f 70 74 69 6f 6e 20 N_DIAGS] option
67d0: 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e at compile time.
67e0: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
67f0: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 5b SQL functions [
6800: 73 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70 sqlite_compileop
6810: 74 69 6f 6e 5f 75 73 65 64 28 29 5d 20 61 6e 64 tion_used()] and
6820: 0a 2a 2a 20 5b 73 71 6c 69 74 65 5f 63 6f 6d 70 .** [sqlite_comp
6830: 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 29 5d ileoption_get()]
6840: 20 61 6e 64 20 74 68 65 20 5b 63 6f 6d 70 69 6c and the [compil
6850: 65 5f 6f 70 74 69 6f 6e 73 20 70 72 61 67 6d 61 e_options pragma
6860: 5d 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 ]..*/.#ifndef SQ
6870: 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c LITE_OMIT_COMPIL
6880: 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 0a 53 51 EOPTION_DIAGS.SQ
6890: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
68a0: 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 ite3_compileopti
68b0: 6f 6e 5f 75 73 65 64 28 63 6f 6e 73 74 20 63 68 on_used(const ch
68c0: 61 72 20 2a 7a 4f 70 74 4e 61 6d 65 29 3b 0a 53 ar *zOptName);.S
68d0: 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
68e0: 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f char *sqlite3_co
68f0: 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 mpileoption_get(
6900: 69 6e 74 20 4e 29 3b 0a 23 65 6e 64 69 66 0a 0a int N);.#endif..
6910: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
6920: 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20 54 Test To See If T
6930: 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54 68 he Library Is Th
6940: 72 65 61 64 73 61 66 65 0a 2a 2a 0a 2a 2a 20 5e readsafe.**.** ^
6950: 54 68 65 20 73 71 6c 69 74 65 33 5f 74 68 72 65 The sqlite3_thre
6960: 61 64 73 61 66 65 28 29 20 66 75 6e 63 74 69 6f adsafe() functio
6970: 6e 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 20 69 n returns zero i
6980: 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 0a 2a 2a f and only if.**
6990: 20 53 51 4c 69 74 65 20 77 61 73 20 63 6f 6d 70 SQLite was comp
69a0: 69 6c 65 64 20 77 69 74 68 20 6d 75 74 65 78 69 iled with mutexi
69b0: 6e 67 20 63 6f 64 65 20 6f 6d 69 74 74 65 64 20 ng code omitted
69c0: 64 75 65 20 74 6f 20 74 68 65 0a 2a 2a 20 5b 53 due to the.** [S
69d0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
69e0: 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f ] compile-time o
69f0: 70 74 69 6f 6e 20 62 65 69 6e 67 20 73 65 74 20 ption being set
6a00: 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 to 0..**.** SQLi
6a10: 74 65 20 63 61 6e 20 62 65 20 63 6f 6d 70 69 6c te can be compil
6a20: 65 64 20 77 69 74 68 20 6f 72 20 77 69 74 68 6f ed with or witho
6a30: 75 74 20 6d 75 74 65 78 65 73 2e 20 20 57 68 65 ut mutexes. Whe
6a40: 6e 0a 2a 2a 20 74 68 65 20 5b 53 51 4c 49 54 45 n.** the [SQLITE
6a50: 5f 54 48 52 45 41 44 53 41 46 45 5d 20 43 20 70 _THREADSAFE] C p
6a60: 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 reprocessor macr
6a70: 6f 20 69 73 20 31 20 6f 72 20 32 2c 20 6d 75 74 o is 1 or 2, mut
6a80: 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e 61 62 exes.** are enab
6a90: 6c 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20 69 led and SQLite i
6aa0: 73 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 57 s threadsafe. W
6ab0: 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 hen the.** [SQLI
6ac0: 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 6d TE_THREADSAFE] m
6ad0: 61 63 72 6f 20 69 73 20 30 2c 20 0a 2a 2a 20 74 acro is 0, .** t
6ae0: 68 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6f he mutexes are o
6af0: 6d 69 74 74 65 64 2e 20 20 57 69 74 68 6f 75 74 mitted. Without
6b00: 20 74 68 65 20 6d 75 74 65 78 65 73 2c 20 69 74 the mutexes, it
6b10: 20 69 73 20 6e 6f 74 20 73 61 66 65 0a 2a 2a 20 is not safe.**
6b20: 74 6f 20 75 73 65 20 53 51 4c 69 74 65 20 63 6f to use SQLite co
6b30: 6e 63 75 72 72 65 6e 74 6c 79 20 66 72 6f 6d 20 ncurrently from
6b40: 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 74 68 more than one th
6b50: 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 45 6e 61 62 read..**.** Enab
6b60: 6c 69 6e 67 20 6d 75 74 65 78 65 73 20 69 6e 63 ling mutexes inc
6b70: 75 72 73 20 61 20 6d 65 61 73 75 72 61 62 6c 65 urs a measurable
6b80: 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 70 65 6e performance pen
6b90: 61 6c 74 79 2e 0a 2a 2a 20 53 6f 20 69 66 20 73 alty..** So if s
6ba0: 70 65 65 64 20 69 73 20 6f 66 20 75 74 6d 6f 73 peed is of utmos
6bb0: 74 20 69 6d 70 6f 72 74 61 6e 63 65 2c 20 69 74 t importance, it
6bc0: 20 6d 61 6b 65 73 20 73 65 6e 73 65 20 74 6f 20 makes sense to
6bd0: 64 69 73 61 62 6c 65 0a 2a 2a 20 74 68 65 20 6d disable.** the m
6be0: 75 74 65 78 65 73 2e 20 20 42 75 74 20 66 6f 72 utexes. But for
6bf0: 20 6d 61 78 69 6d 75 6d 20 73 61 66 65 74 79 2c maximum safety,
6c00: 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 20 mutexes should
6c10: 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 20 5e be enabled..** ^
6c20: 54 68 65 20 64 65 66 61 75 6c 74 20 62 65 68 61 The default beha
6c30: 76 69 6f 72 20 69 73 20 66 6f 72 20 6d 75 74 65 vior is for mute
6c40: 78 65 73 20 74 6f 20 62 65 20 65 6e 61 62 6c 65 xes to be enable
6c50: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e d..**.** This in
6c60: 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 terface can be u
6c70: 73 65 64 20 62 79 20 61 6e 20 61 70 70 6c 69 63 sed by an applic
6c80: 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 ation to make su
6c90: 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 76 re that the.** v
6ca0: 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 ersion of SQLite
6cb0: 20 74 68 61 74 20 69 74 20 69 73 20 6c 69 6e 6b that it is link
6cc0: 69 6e 67 20 61 67 61 69 6e 73 74 20 77 61 73 20 ing against was
6cd0: 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a compiled with.**
6ce0: 20 74 68 65 20 64 65 73 69 72 65 64 20 73 65 74 the desired set
6cf0: 74 69 6e 67 20 6f 66 20 74 68 65 20 5b 53 51 4c ting of the [SQL
6d00: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 ITE_THREADSAFE]
6d10: 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 macro..**.** Thi
6d20: 73 20 69 6e 74 65 72 66 61 63 65 20 6f 6e 6c 79 s interface only
6d30: 20 72 65 70 6f 72 74 73 20 6f 6e 20 74 68 65 20 reports on the
6d40: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6d 75 74 compile-time mut
6d50: 65 78 20 73 65 74 74 69 6e 67 0a 2a 2a 20 6f 66 ex setting.** of
6d60: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 the [SQLITE_THR
6d70: 45 41 44 53 41 46 45 5d 20 66 6c 61 67 2e 20 20 EADSAFE] flag.
6d80: 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d If SQLite is com
6d90: 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 53 51 piled with.** SQ
6da0: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d LITE_THREADSAFE=
6db0: 31 20 6f 72 20 3d 32 20 74 68 65 6e 20 6d 75 74 1 or =2 then mut
6dc0: 65 78 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 exes are enabled
6dd0: 20 62 79 20 64 65 66 61 75 6c 74 20 62 75 74 0a by default but.
6de0: 2a 2a 20 63 61 6e 20 62 65 20 66 75 6c 6c 79 20 ** can be fully
6df0: 6f 72 20 70 61 72 74 69 61 6c 6c 79 20 64 69 73 or partially dis
6e00: 61 62 6c 65 64 20 75 73 69 6e 67 20 61 20 63 61 abled using a ca
6e10: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 ll to [sqlite3_c
6e20: 6f 6e 66 69 67 28 29 5d 0a 2a 2a 20 77 69 74 68 onfig()].** with
6e30: 20 74 68 65 20 76 65 72 62 73 20 5b 53 51 4c 49 the verbs [SQLI
6e40: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 TE_CONFIG_SINGLE
6e50: 54 48 52 45 41 44 5d 2c 20 5b 53 51 4c 49 54 45 THREAD], [SQLITE
6e60: 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 _CONFIG_MULTITHR
6e70: 45 41 44 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c EAD],.** or [SQL
6e80: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 ITE_CONFIG_MUTEX
6e90: 5d 2e 20 20 5e 28 54 68 65 20 72 65 74 75 72 6e ]. ^(The return
6ea0: 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a value of the.**
6eb0: 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 sqlite3_threads
6ec0: 61 66 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 73 afe() function s
6ed0: 68 6f 77 73 20 6f 6e 6c 79 20 74 68 65 20 63 6f hows only the co
6ee0: 6d 70 69 6c 65 2d 74 69 6d 65 20 73 65 74 74 69 mpile-time setti
6ef0: 6e 67 20 6f 66 0a 2a 2a 20 74 68 72 65 61 64 20 ng of.** thread
6f00: 73 61 66 65 74 79 2c 20 6e 6f 74 20 61 6e 79 20 safety, not any
6f10: 72 75 6e 2d 74 69 6d 65 20 63 68 61 6e 67 65 73 run-time changes
6f20: 20 74 6f 20 74 68 61 74 20 73 65 74 74 69 6e 67 to that setting
6f30: 20 6d 61 64 65 20 62 79 0a 2a 2a 20 73 71 6c 69 made by.** sqli
6f40: 74 65 33 5f 63 6f 6e 66 69 67 28 29 2e 20 49 6e te3_config(). In
6f50: 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 other words, th
6f60: 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 e return value f
6f70: 72 6f 6d 20 73 71 6c 69 74 65 33 5f 74 68 72 65 rom sqlite3_thre
6f80: 61 64 73 61 66 65 28 29 0a 2a 2a 20 69 73 20 75 adsafe().** is u
6f90: 6e 63 68 61 6e 67 65 64 20 62 79 20 63 61 6c 6c nchanged by call
6fa0: 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e s to sqlite3_con
6fb0: 66 69 67 28 29 2e 29 5e 0a 2a 2a 0a 2a 2a 20 53 fig().)^.**.** S
6fc0: 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e ee the [threadin
6fd0: 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e 74 g mode] document
6fe0: 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 ation for additi
6ff0: 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
7000: 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
7010: 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 int sqlite3_thre
7020: 61 64 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f adsafe(void);../
7030: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
7040: 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 atabase Connecti
7050: 6f 6e 20 48 61 6e 64 6c 65 0a 2a 2a 20 4b 45 59 on Handle.** KEY
7060: 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61 73 65 WORDS: {database
7070: 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64 61 connection} {da
7080: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
7090: 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f ns}.**.** Each o
70a0: 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 pen SQLite datab
70b0: 61 73 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 ase is represent
70c0: 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65 72 20 ed by a pointer
70d0: 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f to an instance o
70e0: 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75 65 20 f.** the opaque
70f0: 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 structure named
7100: 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74 20 69 "sqlite3". It i
7110: 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68 69 6e s useful to thin
7120: 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 0a k of an sqlite3.
7130: 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20 61 6e ** pointer as an
7140: 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 5b 73 object. The [s
7150: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 qlite3_open()],
7160: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 [sqlite3_open16(
7170: 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 )], and.** [sqli
7180: 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 te3_open_v2()] i
7190: 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 69 74 nterfaces are it
71a0: 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c 20 s constructors,
71b0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f and [sqlite3_clo
71c0: 73 65 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 se()].** and [sq
71d0: 6c 69 74 65 33 5f 63 6c 6f 73 65 5f 76 32 28 29 lite3_close_v2()
71e0: 5d 20 61 72 65 20 69 74 73 20 64 65 73 74 72 75 ] are its destru
71f0: 63 74 6f 72 73 2e 20 20 54 68 65 72 65 20 61 72 ctors. There ar
7200: 65 20 6d 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 e many other.**
7210: 69 6e 74 65 72 66 61 63 65 73 20 28 73 75 63 68 interfaces (such
7220: 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f as.** [sqlite3_
7230: 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b prepare_v2()], [
7240: 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
7250: 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61 6e 64 0a unction()], and.
7260: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 ** [sqlite3_busy
7270: 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74 6f 20 6e _timeout()] to n
7280: 61 6d 65 20 62 75 74 20 74 68 72 65 65 29 20 74 ame but three) t
7290: 68 61 74 20 61 72 65 20 6d 65 74 68 6f 64 73 20 hat are methods
72a0: 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 on an.** sqlite3
72b0: 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65 object..*/.type
72c0: 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
72d0: 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a 2f 2a 0a e3 sqlite3;../*.
72e0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 36 34 2d ** CAPI3REF: 64-
72f0: 42 69 74 20 49 6e 74 65 67 65 72 20 54 79 70 65 Bit Integer Type
7300: 73 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 s.** KEYWORDS: s
7310: 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 qlite_int64 sqli
7320: 74 65 5f 75 69 6e 74 36 34 0a 2a 2a 0a 2a 2a 20 te_uint64.**.**
7330: 42 65 63 61 75 73 65 20 74 68 65 72 65 20 69 73 Because there is
7340: 20 6e 6f 20 63 72 6f 73 73 2d 70 6c 61 74 66 6f no cross-platfo
7350: 72 6d 20 77 61 79 20 74 6f 20 73 70 65 63 69 66 rm way to specif
7360: 79 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 y 64-bit integer
7370: 20 74 79 70 65 73 0a 2a 2a 20 53 51 4c 69 74 65 types.** SQLite
7380: 20 69 6e 63 6c 75 64 65 73 20 74 79 70 65 64 65 includes typede
7390: 66 73 20 66 6f 72 20 36 34 2d 62 69 74 20 73 69 fs for 64-bit si
73a0: 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67 6e 65 gned and unsigne
73b0: 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 0a 2a d integers..**.*
73c0: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e * The sqlite3_in
73d0: 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65 33 5f t64 and sqlite3_
73e0: 75 69 6e 74 36 34 20 61 72 65 20 74 68 65 20 70 uint64 are the p
73f0: 72 65 66 65 72 72 65 64 20 74 79 70 65 20 64 65 referred type de
7400: 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 finitions..** Th
7410: 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 61 e sqlite_int64 a
7420: 6e 64 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 nd sqlite_uint64
7430: 20 74 79 70 65 73 20 61 72 65 20 73 75 70 70 6f types are suppo
7440: 72 74 65 64 20 66 6f 72 20 62 61 63 6b 77 61 72 rted for backwar
7450: 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c ds.** compatibil
7460: 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 ity only..**.**
7470: 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 ^The sqlite3_int
7480: 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f 69 6e 64 and sqlite_in
7490: 74 36 34 20 74 79 70 65 73 20 63 61 6e 20 73 74 t64 types can st
74a0: 6f 72 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 ore integer valu
74b0: 65 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 2d 39 es.** between -9
74c0: 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 2233720368547758
74d0: 30 38 20 61 6e 64 20 2b 39 32 32 33 33 37 32 30 08 and +92233720
74e0: 33 36 38 35 34 37 37 35 38 30 37 20 69 6e 63 6c 36854775807 incl
74f0: 75 73 69 76 65 2e 20 20 5e 54 68 65 0a 2a 2a 20 usive. ^The.**
7500: 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 61 sqlite3_uint64 a
7510: 6e 64 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 nd sqlite_uint64
7520: 20 74 79 70 65 73 20 63 61 6e 20 73 74 6f 72 65 types can store
7530: 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 integer values
7540: 0a 2a 2a 20 62 65 74 77 65 65 6e 20 30 20 61 6e .** between 0 an
7550: 64 20 2b 31 38 34 34 36 37 34 34 30 37 33 37 30 d +1844674407370
7560: 39 35 35 31 36 31 35 20 69 6e 63 6c 75 73 69 76 9551615 inclusiv
7570: 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c e..*/.#ifdef SQL
7580: 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a 20 ITE_INT64_TYPE.
7590: 20 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 5f typedef SQLITE_
75a0: 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 74 INT64_TYPE sqlit
75b0: 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 e_int64;. typed
75c0: 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c 49 ef unsigned SQLI
75d0: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 TE_INT64_TYPE sq
75e0: 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c lite_uint64;.#el
75f0: 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f if defined(_MSC_
7600: 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 VER) || defined(
7610: 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 20 __BORLANDC__).
7620: 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 20 typedef __int64
7630: 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 sqlite_int64;.
7640: 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 typedef unsigned
7650: 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f __int64 sqlite_
7660: 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 20 uint64;.#else.
7670: 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f 6e typedef long lon
7680: 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e 74 g int sqlite_int
7690: 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e 64;. typedef un
76a0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
76b0: 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e 74 int sqlite_uint
76c0: 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 64;.#endif.typed
76d0: 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 ef sqlite_int64
76e0: 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a 74 sqlite3_int64;.t
76f0: 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 69 ypedef sqlite_ui
7700: 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69 6e nt64 sqlite3_uin
7710: 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 t64;../*.** If c
7720: 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70 ompiling for a p
7730: 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61 rocessor that la
7740: 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 cks floating poi
7750: 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 nt support,.** s
7760: 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 65 ubstitute intege
7770: 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70 r for floating-p
7780: 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 oint..*/.#ifdef
7790: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
77a0: 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 TING_POINT.# def
77b0: 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 ine double sqlit
77c0: 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66 0a e3_int64.#endif.
77d0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
77e0: 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61 62 Closing A Datab
77f0: 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a ase Connection.*
7800: 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 *.** ^The sqlite
7810: 33 5f 63 6c 6f 73 65 28 29 20 61 6e 64 20 73 71 3_close() and sq
7820: 6c 69 74 65 33 5f 63 6c 6f 73 65 5f 76 32 28 29 lite3_close_v2()
7830: 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 64 65 routines are de
7840: 73 74 72 75 63 74 6f 72 73 0a 2a 2a 20 66 6f 72 structors.** for
7850: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f the [sqlite3] o
7860: 62 6a 65 63 74 2e 0a 2a 2a 20 5e 43 61 6c 6c 73 bject..** ^Calls
7870: 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 to sqlite3_clos
7880: 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f e() and sqlite3_
7890: 63 6c 6f 73 65 5f 76 32 28 29 20 72 65 74 75 72 close_v2() retur
78a0: 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 0a 2a n SQLITE_OK if.*
78b0: 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 * the [sqlite3]
78c0: 6f 62 6a 65 63 74 20 69 73 20 73 75 63 63 65 73 object is succes
78d0: 73 66 75 6c 6c 79 20 64 65 73 74 72 6f 79 65 64 sfully destroyed
78e0: 20 61 6e 64 20 61 6c 6c 20 61 73 73 6f 63 69 61 and all associa
78f0: 74 65 64 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73 ted.** resources
7900: 20 61 72 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 are deallocated
7910: 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 74 68 65 20 ..**.** ^If the
7920: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
7930: 69 6f 6e 20 69 73 20 61 73 73 6f 63 69 61 74 65 ion is associate
7940: 64 20 77 69 74 68 20 75 6e 66 69 6e 61 6c 69 7a d with unfinaliz
7950: 65 64 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 ed prepared.** s
7960: 74 61 74 65 6d 65 6e 74 73 20 6f 72 20 75 6e 66 tatements or unf
7970: 69 6e 69 73 68 65 64 20 73 71 6c 69 74 65 33 5f inished sqlite3_
7980: 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 73 20 74 backup objects t
7990: 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 hen sqlite3_clos
79a0: 65 28 29 0a 2a 2a 20 77 69 6c 6c 20 6c 65 61 76 e().** will leav
79b0: 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 e the database c
79c0: 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 6e 20 61 onnection open a
79d0: 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 nd return [SQLIT
79e0: 45 5f 42 55 53 59 5d 2e 0a 2a 2a 20 5e 49 66 20 E_BUSY]..** ^If
79f0: 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5f 76 32 sqlite3_close_v2
7a00: 28 29 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 () is called wit
7a10: 68 20 75 6e 66 69 6e 61 6c 69 7a 65 64 20 70 72 h unfinalized pr
7a20: 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
7a30: 73 0a 2a 2a 20 61 6e 64 20 75 6e 66 69 6e 69 73 s.** and unfinis
7a40: 68 65 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b hed sqlite3_back
7a50: 75 70 73 2c 20 74 68 65 6e 20 74 68 65 20 64 61 ups, then the da
7a60: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
7a70: 6e 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 61 6e 20 n becomes.** an
7a80: 75 6e 75 73 61 62 6c 65 20 22 7a 6f 6d 62 69 65 unusable "zombie
7a90: 22 20 77 68 69 63 68 20 77 69 6c 6c 20 61 75 74 " which will aut
7aa0: 6f 6d 61 74 69 63 61 6c 6c 79 20 62 65 20 64 65 omatically be de
7ab0: 61 6c 6c 6f 63 61 74 65 64 20 77 68 65 6e 20 74 allocated when t
7ac0: 68 65 0a 2a 2a 20 6c 61 73 74 20 70 72 65 70 61 he.** last prepa
7ad0: 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 red statement is
7ae0: 20 66 69 6e 61 6c 69 7a 65 64 20 6f 72 20 74 68 finalized or th
7af0: 65 20 6c 61 73 74 20 73 71 6c 69 74 65 33 5f 62 e last sqlite3_b
7b00: 61 63 6b 75 70 20 69 73 0a 2a 2a 20 66 69 6e 69 ackup is.** fini
7b10: 73 68 65 64 2e 20 20 54 68 65 20 73 71 6c 69 74 shed. The sqlit
7b20: 65 33 5f 63 6c 6f 73 65 5f 76 32 28 29 20 69 6e e3_close_v2() in
7b30: 74 65 72 66 61 63 65 20 69 73 20 69 6e 74 65 6e terface is inten
7b40: 64 65 64 20 66 6f 72 20 75 73 65 20 77 69 74 68 ded for use with
7b50: 0a 2a 2a 20 68 6f 73 74 20 6c 61 6e 67 75 61 67 .** host languag
7b60: 65 73 20 74 68 61 74 20 61 72 65 20 67 61 72 62 es that are garb
7b70: 61 67 65 20 63 6f 6c 6c 65 63 74 65 64 2c 20 61 age collected, a
7b80: 6e 64 20 77 68 65 72 65 20 74 68 65 20 6f 72 64 nd where the ord
7b90: 65 72 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 64 er in which.** d
7ba0: 65 73 74 72 75 63 74 6f 72 73 20 61 72 65 20 63 estructors are c
7bb0: 61 6c 6c 65 64 20 69 73 20 61 72 62 69 74 72 61 alled is arbitra
7bc0: 72 79 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 ry..**.** Applic
7bd0: 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 5b 73 ations should [s
7be0: 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 qlite3_finalize
7bf0: 7c 20 66 69 6e 61 6c 69 7a 65 5d 20 61 6c 6c 20 | finalize] all
7c00: 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
7c10: 65 6e 74 73 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 ents],.** [sqlit
7c20: 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 7c 20 e3_blob_close |
7c30: 63 6c 6f 73 65 5d 20 61 6c 6c 20 5b 42 4c 4f 42 close] all [BLOB
7c40: 20 68 61 6e 64 6c 65 73 5d 2c 20 61 6e 64 20 0a handles], and .
7c50: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b ** [sqlite3_back
7c60: 75 70 5f 66 69 6e 69 73 68 20 7c 20 66 69 6e 69 up_finish | fini
7c70: 73 68 5d 20 61 6c 6c 20 5b 73 71 6c 69 74 65 33 sh] all [sqlite3
7c80: 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 73 _backup] objects
7c90: 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 associated.** w
7ca0: 69 74 68 20 74 68 65 20 5b 73 71 6c 69 74 65 33 ith the [sqlite3
7cb0: 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 74 ] object prior t
7cc0: 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 o attempting to
7cd0: 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 74 close the object
7ce0: 2e 20 20 5e 49 66 0a 2a 2a 20 73 71 6c 69 74 65 . ^If.** sqlite
7cf0: 33 5f 63 6c 6f 73 65 28 29 20 69 73 20 63 61 6c 3_close() is cal
7d00: 6c 65 64 20 6f 6e 20 61 20 5b 64 61 74 61 62 61 led on a [databa
7d10: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 se connection] t
7d20: 68 61 74 20 73 74 69 6c 6c 20 68 61 73 0a 2a 2a hat still has.**
7d30: 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 5b 70 72 outstanding [pr
7d40: 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
7d50: 73 5d 2c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 s], [BLOB handle
7d60: 73 5d 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 5b 73 s], and/or.** [s
7d70: 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f qlite3_backup] o
7d80: 62 6a 65 63 74 73 20 74 68 65 6e 20 69 74 20 72 bjects then it r
7d90: 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b eturns SQLITE_OK
7da0: 20 62 75 74 20 74 68 65 20 64 65 61 6c 6c 6f 63 but the dealloc
7db0: 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 72 65 73 6f ation.** of reso
7dc0: 75 72 63 65 73 20 69 73 20 64 65 66 65 72 72 65 urces is deferre
7dd0: 64 20 75 6e 74 69 6c 20 61 6c 6c 20 5b 70 72 65 d until all [pre
7de0: 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
7df0: 5d 2c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 ], [BLOB handles
7e00: 5d 2c 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 ],.** and [sqlit
7e10: 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 e3_backup] objec
7e20: 74 73 20 61 72 65 20 61 6c 73 6f 20 64 65 73 74 ts are also dest
7e30: 72 6f 79 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 royed..**.** ^If
7e40: 20 61 6e 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 an [sqlite3] ob
7e50: 6a 65 63 74 20 69 73 20 64 65 73 74 72 6f 79 65 ject is destroye
7e60: 64 20 77 68 69 6c 65 20 61 20 74 72 61 6e 73 61 d while a transa
7e70: 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a 2a ction is open,.*
7e80: 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f * the transactio
7e90: 6e 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c n is automatical
7ea0: 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a ly rolled back..
7eb0: 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 70 61 72 61 **.** The C para
7ec0: 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 meter to [sqlite
7ed0: 33 5f 63 6c 6f 73 65 28 43 29 5d 20 61 6e 64 20 3_close(C)] and
7ee0: 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5f 76 [sqlite3_close_v
7ef0: 32 28 43 29 5d 0a 2a 2a 20 6d 75 73 74 20 62 65 2(C)].** must be
7f00: 20 65 69 74 68 65 72 20 61 20 4e 55 4c 4c 0a 2a either a NULL.*
7f10: 2a 20 70 6f 69 6e 74 65 72 20 6f 72 20 61 6e 20 * pointer or an
7f20: 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 [sqlite3] object
7f30: 20 70 6f 69 6e 74 65 72 20 6f 62 74 61 69 6e 65 pointer obtaine
7f40: 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74 d.** from [sqlit
7f50: 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c e3_open()], [sql
7f60: 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 ite3_open16()],
7f70: 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f or.** [sqlite3_o
7f80: 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20 6e pen_v2()], and n
7f90: 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6c ot previously cl
7fa0: 6f 73 65 64 2e 0a 2a 2a 20 5e 43 61 6c 6c 69 6e osed..** ^Callin
7fb0: 67 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 g sqlite3_close(
7fc0: 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6c 6f ) or sqlite3_clo
7fd0: 73 65 5f 76 32 28 29 20 77 69 74 68 20 61 20 4e se_v2() with a N
7fe0: 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 61 ULL pointer.** a
7ff0: 72 67 75 6d 65 6e 74 20 69 73 20 61 20 68 61 72 rgument is a har
8000: 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a mless no-op..*/.
8010: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
8020: 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c qlite3_close(sql
8030: 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 ite3*);.SQLITE_A
8040: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
8050: 6c 6f 73 65 5f 76 32 28 73 71 6c 69 74 65 33 2a lose_v2(sqlite3*
8060: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 );../*.** The ty
8070: 70 65 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 63 pe for a callbac
8080: 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 k function..** T
8090: 68 69 73 20 69 73 20 6c 65 67 61 63 79 20 61 6e his is legacy an
80a0: 64 20 64 65 70 72 65 63 61 74 65 64 2e 20 20 49 d deprecated. I
80b0: 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 66 6f t is included fo
80c0: 72 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 r historical.**
80d0: 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 61 6e compatibility an
80e0: 64 20 69 73 20 6e 6f 74 20 64 6f 63 75 6d 65 6e d is not documen
80f0: 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ted..*/.typedef
8100: 69 6e 74 20 28 2a 73 71 6c 69 74 65 33 5f 63 61 int (*sqlite3_ca
8110: 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e llback)(void*,in
8120: 74 2c 63 68 61 72 2a 2a 2c 20 63 68 61 72 2a 2a t,char**, char**
8130: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
8140: 45 46 3a 20 4f 6e 65 2d 53 74 65 70 20 51 75 65 EF: One-Step Que
8150: 72 79 20 45 78 65 63 75 74 69 6f 6e 20 49 6e 74 ry Execution Int
8160: 65 72 66 61 63 65 0a 2a 2a 0a 2a 2a 20 54 68 65 erface.**.** The
8170: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 sqlite3_exec()
8180: 69 6e 74 65 72 66 61 63 65 20 69 73 20 61 20 63 interface is a c
8190: 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 onvenience wrapp
81a0: 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73 71 er around.** [sq
81b0: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
81c0: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 ()], [sqlite3_st
81d0: 65 70 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 ep()], and [sqli
81e0: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2c te3_finalize()],
81f0: 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 6f 77 73 20 .** that allows
8200: 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 an application t
8210: 6f 20 72 75 6e 20 6d 75 6c 74 69 70 6c 65 20 73 o run multiple s
8220: 74 61 74 65 6d 65 6e 74 73 20 6f 66 20 53 51 4c tatements of SQL
8230: 0a 2a 2a 20 77 69 74 68 6f 75 74 20 68 61 76 69 .** without havi
8240: 6e 67 20 74 6f 20 75 73 65 20 61 20 6c 6f 74 20 ng to use a lot
8250: 6f 66 20 43 20 63 6f 64 65 2e 20 0a 2a 2a 0a 2a of C code. .**.*
8260: 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 65 * ^The sqlite3_e
8270: 78 65 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 xec() interface
8280: 72 75 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 runs zero or mor
8290: 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2c e UTF-8 encoded,
82a0: 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 .** semicolon-se
82b0: 70 61 72 61 74 65 20 53 51 4c 20 73 74 61 74 65 parate SQL state
82c0: 6d 65 6e 74 73 20 70 61 73 73 65 64 20 69 6e 74 ments passed int
82d0: 6f 20 69 74 73 20 32 6e 64 20 61 72 67 75 6d 65 o its 2nd argume
82e0: 6e 74 2c 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f nt,.** in the co
82f0: 6e 74 65 78 74 20 6f 66 20 74 68 65 20 5b 64 61 ntext of the [da
8300: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
8310: 6e 5d 20 70 61 73 73 65 64 20 69 6e 20 61 73 20 n] passed in as
8320: 69 74 73 20 31 73 74 0a 2a 2a 20 61 72 67 75 6d its 1st.** argum
8330: 65 6e 74 2e 20 20 5e 49 66 20 74 68 65 20 63 61 ent. ^If the ca
8340: 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 llback function
8350: 6f 66 20 74 68 65 20 33 72 64 20 61 72 67 75 6d of the 3rd argum
8360: 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 ent to.** sqlite
8370: 33 5f 65 78 65 63 28 29 20 69 73 20 6e 6f 74 20 3_exec() is not
8380: 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 NULL, then it is
8390: 20 69 6e 76 6f 6b 65 64 20 66 6f 72 20 65 61 63 invoked for eac
83a0: 68 20 72 65 73 75 6c 74 20 72 6f 77 0a 2a 2a 20 h result row.**
83b0: 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74 68 coming out of th
83c0: 65 20 65 76 61 6c 75 61 74 65 64 20 53 51 4c 20 e evaluated SQL
83d0: 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 5e 54 68 statements. ^Th
83e0: 65 20 34 74 68 20 61 72 67 75 6d 65 6e 74 20 74 e 4th argument t
83f0: 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 78 65 o.** sqlite3_exe
8400: 63 28 29 20 69 73 20 72 65 6c 61 79 65 64 20 74 c() is relayed t
8410: 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 31 73 hrough to the 1s
8420: 74 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 65 61 t argument of ea
8430: 63 68 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69 ch.** callback i
8440: 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 5e 49 66 20 nvocation. ^If
8450: 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 70 6f 69 the callback poi
8460: 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f nter to sqlite3_
8470: 65 78 65 63 28 29 0a 2a 2a 20 69 73 20 4e 55 4c exec().** is NUL
8480: 4c 2c 20 74 68 65 6e 20 6e 6f 20 63 61 6c 6c 62 L, then no callb
8490: 61 63 6b 20 69 73 20 65 76 65 72 20 69 6e 76 6f ack is ever invo
84a0: 6b 65 64 20 61 6e 64 20 72 65 73 75 6c 74 20 72 ked and result r
84b0: 6f 77 73 20 61 72 65 0a 2a 2a 20 69 67 6e 6f 72 ows are.** ignor
84c0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 61 6e ed..**.** ^If an
84d0: 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 error occurs wh
84e0: 69 6c 65 20 65 76 61 6c 75 61 74 69 6e 67 20 74 ile evaluating t
84f0: 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
8500: 73 20 70 61 73 73 65 64 20 69 6e 74 6f 0a 2a 2a s passed into.**
8510: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2c sqlite3_exec(),
8520: 20 74 68 65 6e 20 65 78 65 63 75 74 69 6f 6e 20 then execution
8530: 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 of the current s
8540: 74 61 74 65 6d 65 6e 74 20 73 74 6f 70 73 20 61 tatement stops a
8550: 6e 64 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 nd.** subsequent
8560: 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 statements are
8570: 73 6b 69 70 70 65 64 2e 20 20 5e 49 66 20 74 68 skipped. ^If th
8580: 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 5th parameter
8590: 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 to sqlite3_exec(
85a0: 29 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c ).** is not NULL
85b0: 20 74 68 65 6e 20 61 6e 79 20 65 72 72 6f 72 20 then any error
85c0: 6d 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74 message is writt
85d0: 65 6e 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f en into memory o
85e0: 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 btained.** from
85f0: 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
8600: 29 5d 20 61 6e 64 20 70 61 73 73 65 64 20 62 61 )] and passed ba
8610: 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 35 ck through the 5
8620: 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a th parameter..**
8630: 20 54 6f 20 61 76 6f 69 64 20 6d 65 6d 6f 72 79 To avoid memory
8640: 20 6c 65 61 6b 73 2c 20 74 68 65 20 61 70 70 6c leaks, the appl
8650: 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 69 ication should i
8660: 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 66 nvoke [sqlite3_f
8670: 72 65 65 28 29 5d 0a 2a 2a 20 6f 6e 20 65 72 72 ree()].** on err
8680: 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e or message strin
8690: 67 73 20 72 65 74 75 72 6e 65 64 20 74 68 72 6f gs returned thro
86a0: 75 67 68 20 74 68 65 20 35 74 68 20 70 61 72 61 ugh the 5th para
86b0: 6d 65 74 65 72 20 6f 66 0a 2a 2a 20 6f 66 20 73 meter of.** of s
86c0: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 61 66 qlite3_exec() af
86d0: 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 6d 65 ter the error me
86e0: 73 73 61 67 65 20 73 74 72 69 6e 67 20 69 73 20 ssage string is
86f0: 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65 64 65 64 no longer needed
8700: 2e 0a 2a 2a 20 5e 49 66 20 74 68 65 20 35 74 68 ..** ^If the 5th
8710: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
8720: 6c 69 74 65 33 5f 65 78 65 63 28 29 20 69 73 20 lite3_exec() is
8730: 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 6e 6f 20 not NULL and no
8740: 65 72 72 6f 72 73 0a 2a 2a 20 6f 63 63 75 72 2c errors.** occur,
8750: 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 65 78 then sqlite3_ex
8760: 65 63 28 29 20 73 65 74 73 20 74 68 65 20 70 6f ec() sets the po
8770: 69 6e 74 65 72 20 69 6e 20 69 74 73 20 35 74 68 inter in its 5th
8780: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a parameter to.**
8790: 20 4e 55 4c 4c 20 62 65 66 6f 72 65 20 72 65 74 NULL before ret
87a0: 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5e 49 urning..**.** ^I
87b0: 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 65 78 65 f an sqlite3_exe
87c0: 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 c() callback ret
87d0: 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 urns non-zero, t
87e0: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 he sqlite3_exec(
87f0: 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 ).** routine ret
8800: 75 72 6e 73 20 53 51 4c 49 54 45 5f 41 42 4f 52 urns SQLITE_ABOR
8810: 54 20 77 69 74 68 6f 75 74 20 69 6e 76 6f 6b 69 T without invoki
8820: 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 ng the callback
8830: 61 67 61 69 6e 20 61 6e 64 0a 2a 2a 20 77 69 74 again and.** wit
8840: 68 6f 75 74 20 72 75 6e 6e 69 6e 67 20 61 6e 79 hout running any
8850: 20 73 75 62 73 65 71 75 65 6e 74 20 53 51 4c 20 subsequent SQL
8860: 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a statements..**.*
8870: 2a 20 5e 54 68 65 20 32 6e 64 20 61 72 67 75 6d * ^The 2nd argum
8880: 65 6e 74 20 74 6f 20 74 68 65 20 73 71 6c 69 74 ent to the sqlit
8890: 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 62 61 e3_exec() callba
88a0: 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 ck function is t
88b0: 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 he.** number of
88c0: 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 columns in the r
88d0: 65 73 75 6c 74 2e 20 20 5e 54 68 65 20 33 72 64 esult. ^The 3rd
88e0: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
88f0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 0a sqlite3_exec().
8900: 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 ** callback is a
8910: 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 n array of point
8920: 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20 6f ers to strings o
8930: 62 74 61 69 6e 65 64 20 61 73 20 69 66 20 66 72 btained as if fr
8940: 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 om.** [sqlite3_c
8950: 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20 6f olumn_text()], o
8960: 6e 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 ne for each colu
8970: 6d 6e 2e 20 20 5e 49 66 20 61 6e 20 65 6c 65 6d mn. ^If an elem
8980: 65 6e 74 20 6f 66 20 61 0a 2a 2a 20 72 65 73 75 ent of a.** resu
8990: 6c 74 20 72 6f 77 20 69 73 20 4e 55 4c 4c 20 74 lt row is NULL t
89a0: 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f hen the correspo
89b0: 6e 64 69 6e 67 20 73 74 72 69 6e 67 20 70 6f 69 nding string poi
89c0: 6e 74 65 72 20 66 6f 72 20 74 68 65 0a 2a 2a 20 nter for the.**
89d0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 sqlite3_exec() c
89e0: 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 4e 55 4c allback is a NUL
89f0: 4c 20 70 6f 69 6e 74 65 72 2e 20 20 5e 54 68 65 L pointer. ^The
8a00: 20 34 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 4th argument to
8a10: 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f the.** sqlite3_
8a20: 65 78 65 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 exec() callback
8a30: 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 is an array of p
8a40: 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72 69 6e ointers to strin
8a50: 67 73 20 77 68 65 72 65 20 65 61 63 68 0a 2a 2a gs where each.**
8a60: 20 65 6e 74 72 79 20 72 65 70 72 65 73 65 6e 74 entry represent
8a70: 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 63 6f s the name of co
8a80: 72 72 65 73 70 6f 6e 64 69 6e 67 20 72 65 73 75 rresponding resu
8a90: 6c 74 20 63 6f 6c 75 6d 6e 20 61 73 20 6f 62 74 lt column as obt
8aa0: 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 ained.** from [s
8ab0: 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 qlite3_column_na
8ac0: 6d 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 me()]..**.** ^If
8ad0: 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 the 2nd paramet
8ae0: 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 er to sqlite3_ex
8af0: 65 63 28 29 20 69 73 20 61 20 4e 55 4c 4c 20 70 ec() is a NULL p
8b00: 6f 69 6e 74 65 72 2c 20 61 20 70 6f 69 6e 74 65 ointer, a pointe
8b10: 72 0a 2a 2a 20 74 6f 20 61 6e 20 65 6d 70 74 79 r.** to an empty
8b20: 20 73 74 72 69 6e 67 2c 20 6f 72 20 61 20 70 6f string, or a po
8b30: 69 6e 74 65 72 20 74 68 61 74 20 63 6f 6e 74 61 inter that conta
8b40: 69 6e 73 20 6f 6e 6c 79 20 77 68 69 74 65 73 70 ins only whitesp
8b50: 61 63 65 20 61 6e 64 2f 6f 72 20 0a 2a 2a 20 53 ace and/or .** S
8b60: 51 4c 20 63 6f 6d 6d 65 6e 74 73 2c 20 74 68 65 QL comments, the
8b70: 6e 20 6e 6f 20 53 51 4c 20 73 74 61 74 65 6d 65 n no SQL stateme
8b80: 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 nts are evaluate
8b90: 64 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 d and the databa
8ba0: 73 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 68 61 se.** is not cha
8bb0: 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 74 nged..**.** Rest
8bc0: 72 69 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 rictions:.**.**
8bd0: 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 <ul>.** <li> The
8be0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 application mus
8bf0: 74 20 69 6e 73 75 72 65 20 74 68 61 74 20 74 68 t insure that th
8c00: 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 20 e 1st parameter
8c10: 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 to sqlite3_exec(
8c20: 29 0a 2a 2a 20 20 20 20 20 20 69 73 20 61 20 76 ).** is a v
8c30: 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 20 5b 64 alid and open [d
8c40: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
8c50: 6f 6e 5d 2e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 on]..** <li> The
8c60: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 application mus
8c70: 74 20 6e 6f 74 20 63 6c 6f 73 65 20 5b 64 61 74 t not close [dat
8c80: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
8c90: 5d 20 73 70 65 63 69 66 69 65 64 20 62 79 0a 2a ] specified by.*
8ca0: 2a 20 20 20 20 20 20 74 68 65 20 31 73 74 20 70 * the 1st p
8cb0: 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
8cc0: 74 65 33 5f 65 78 65 63 28 29 20 77 68 69 6c 65 te3_exec() while
8cd0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 sqlite3_exec()
8ce0: 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 20 3c is running..** <
8cf0: 6c 69 3e 20 54 68 65 20 61 70 70 6c 69 63 61 74 li> The applicat
8d00: 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 6d 6f 64 ion must not mod
8d10: 69 66 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 ify the SQL stat
8d20: 65 6d 65 6e 74 20 74 65 78 74 20 70 61 73 73 65 ement text passe
8d30: 64 20 69 6e 74 6f 0a 2a 2a 20 20 20 20 20 20 74 d into.** t
8d40: 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 he 2nd parameter
8d50: 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63 of sqlite3_exec
8d60: 28 29 20 77 68 69 6c 65 20 73 71 6c 69 74 65 33 () while sqlite3
8d70: 5f 65 78 65 63 28 29 20 69 73 20 72 75 6e 6e 69 _exec() is runni
8d80: 6e 67 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2f 0a ng..** </ul>.*/.
8d90: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
8da0: 71 6c 69 74 65 33 5f 65 78 65 63 28 0a 20 20 73 qlite3_exec(. s
8db0: 71 6c 69 74 65 33 2a 2c 20 20 20 20 20 20 20 20 qlite3*,
8dc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8dd0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 /* An
8de0: 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f open database */
8df0: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 . const char *s
8e00: 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ql,
8e10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
8e20: 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c 75 SQL to be evalu
8e30: 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 28 2a ated */. int (*
8e40: 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c callback)(void*,
8e50: 69 6e 74 2c 63 68 61 72 2a 2a 2c 63 68 61 72 2a int,char**,char*
8e60: 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b *), /* Callback
8e70: 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 function */. v
8e80: 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 20 20 20 oid *,
8e90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8ea0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 /* 1st
8eb0: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c argument to cal
8ec0: 6c 62 61 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 lback */. char
8ed0: 2a 2a 65 72 72 6d 73 67 20 20 20 20 20 20 20 20 **errmsg
8ee0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8ef0: 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d /* Error m
8f00: 73 67 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 sg written here
8f10: 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.);../*.** CAP
8f20: 49 33 52 45 46 3a 20 52 65 73 75 6c 74 20 43 6f I3REF: Result Co
8f30: 64 65 73 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a des.** KEYWORDS:
8f40: 20 53 51 4c 49 54 45 5f 4f 4b 20 7b 65 72 72 6f SQLITE_OK {erro
8f50: 72 20 63 6f 64 65 7d 20 7b 65 72 72 6f 72 20 63 r code} {error c
8f60: 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 odes}.** KEYWORD
8f70: 53 3a 20 7b 72 65 73 75 6c 74 20 63 6f 64 65 7d S: {result code}
8f80: 20 7b 72 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a {result codes}.
8f90: 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 53 51 4c 69 74 **.** Many SQLit
8fa0: 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 e functions retu
8fb0: 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 rn an integer re
8fc0: 73 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 sult code from t
8fd0: 68 65 20 73 65 74 20 73 68 6f 77 6e 0a 2a 2a 20 he set shown.**
8fe0: 68 65 72 65 20 69 6e 20 6f 72 64 65 72 20 74 6f here in order to
8ff0: 20 69 6e 64 69 63 61 74 65 20 73 75 63 63 65 73 indicate succes
9000: 73 20 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2a s or failure..**
9010: 0a 2a 2a 20 4e 65 77 20 65 72 72 6f 72 20 63 6f .** New error co
9020: 64 65 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 des may be added
9030: 20 69 6e 20 66 75 74 75 72 65 20 76 65 72 73 69 in future versi
9040: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a ons of SQLite..*
9050: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
9060: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 SQLITE_IOERR_REA
9070: 44 20 7c 20 65 78 74 65 6e 64 65 64 20 72 65 73 D | extended res
9080: 75 6c 74 20 63 6f 64 65 73 5d 2c 0a 2a 2a 20 5b ult codes],.** [
9090: 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 6f 6e 5f sqlite3_vtab_on_
90a0: 63 6f 6e 66 6c 69 63 74 28 29 5d 20 5b 53 51 4c conflict()] [SQL
90b0: 49 54 45 5f 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 ITE_ROLLBACK | r
90c0: 65 73 75 6c 74 20 63 6f 64 65 73 5d 2e 0a 2a 2f esult codes]..*/
90d0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
90e0: 4f 4b 20 20 20 20 20 20 20 20 20 20 20 30 20 20 OK 0
90f0: 20 2f 2a 20 53 75 63 63 65 73 73 66 75 6c 20 72 /* Successful r
9100: 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20 62 65 67 69 esult */./* begi
9110: 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72 6f 72 2d 63 nning-of-error-c
9120: 6f 64 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 odes */.#define
9130: 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 20 20 20 SQLITE_ERROR
9140: 20 20 20 20 31 20 20 20 2f 2a 20 53 51 4c 20 65 1 /* SQL e
9150: 72 72 6f 72 20 6f 72 20 6d 69 73 73 69 6e 67 20 rror or missing
9160: 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 database */.#def
9170: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 ine SQLITE_INTER
9180: 4e 41 4c 20 20 20 20 20 32 20 20 20 2f 2a 20 49 NAL 2 /* I
9190: 6e 74 65 72 6e 61 6c 20 6c 6f 67 69 63 20 65 72 nternal logic er
91a0: 72 6f 72 20 69 6e 20 53 51 4c 69 74 65 20 2a 2f ror in SQLite */
91b0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
91c0: 50 45 52 4d 20 20 20 20 20 20 20 20 20 33 20 20 PERM 3
91d0: 20 2f 2a 20 41 63 63 65 73 73 20 70 65 72 6d 69 /* Access permi
91e0: 73 73 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f 0a ssion denied */.
91f0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
9200: 42 4f 52 54 20 20 20 20 20 20 20 20 34 20 20 20 BORT 4
9210: 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 72 6f 75 74 /* Callback rout
9220: 69 6e 65 20 72 65 71 75 65 73 74 65 64 20 61 6e ine requested an
9230: 20 61 62 6f 72 74 20 2a 2f 0a 23 64 65 66 69 6e abort */.#defin
9240: 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 20 20 e SQLITE_BUSY
9250: 20 20 20 20 20 20 35 20 20 20 2f 2a 20 54 68 65 5 /* The
9260: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
9270: 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 66 s locked */.#def
9280: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 ine SQLITE_LOCKE
9290: 44 20 20 20 20 20 20 20 36 20 20 20 2f 2a 20 41 D 6 /* A
92a0: 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 table in the da
92b0: 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 tabase is locked
92c0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
92d0: 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20 TE_NOMEM
92e0: 37 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28 7 /* A malloc(
92f0: 29 20 66 61 69 6c 65 64 20 2a 2f 0a 23 64 65 66 ) failed */.#def
9300: 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 44 4f ine SQLITE_READO
9310: 4e 4c 59 20 20 20 20 20 38 20 20 20 2f 2a 20 41 NLY 8 /* A
9320: 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 ttempt to write
9330: 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61 62 a readonly datab
9340: 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ase */.#define S
9350: 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 QLITE_INTERRUPT
9360: 20 20 20 39 20 20 20 2f 2a 20 4f 70 65 72 61 74 9 /* Operat
9370: 69 6f 6e 20 74 65 72 6d 69 6e 61 74 65 64 20 62 ion terminated b
9380: 79 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 y sqlite3_interr
9390: 75 70 74 28 29 2a 2f 0a 23 64 65 66 69 6e 65 20 upt()*/.#define
93a0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 20 20 20 SQLITE_IOERR
93b0: 20 20 20 31 30 20 20 20 2f 2a 20 53 6f 6d 65 20 10 /* Some
93c0: 6b 69 6e 64 20 6f 66 20 64 69 73 6b 20 49 2f 4f kind of disk I/O
93d0: 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 error occurred
93e0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
93f0: 45 5f 43 4f 52 52 55 50 54 20 20 20 20 20 31 31 E_CORRUPT 11
9400: 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
9410: 73 65 20 64 69 73 6b 20 69 6d 61 67 65 20 69 73 se disk image is
9420: 20 6d 61 6c 66 6f 72 6d 65 64 20 2a 2f 0a 23 64 malformed */.#d
9430: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 54 efine SQLITE_NOT
9440: 46 4f 55 4e 44 20 20 20 20 31 32 20 20 20 2f 2a FOUND 12 /*
9450: 20 55 6e 6b 6e 6f 77 6e 20 6f 70 63 6f 64 65 20 Unknown opcode
9460: 69 6e 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f in sqlite3_file_
9470: 63 6f 6e 74 72 6f 6c 28 29 20 2a 2f 0a 23 64 65 control() */.#de
9480: 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4c 4c fine SQLITE_FULL
9490: 20 20 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20 13 /*
94a0: 49 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c 65 64 Insertion failed
94b0: 20 62 65 63 61 75 73 65 20 64 61 74 61 62 61 73 because databas
94c0: 65 20 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 64 65 e is full */.#de
94d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 fine SQLITE_CANT
94e0: 4f 50 45 4e 20 20 20 20 31 34 20 20 20 2f 2a 20 OPEN 14 /*
94f0: 55 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 Unable to open t
9500: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
9510: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
9520: 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20 31 TE_PROTOCOL 1
9530: 35 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 5 /* Database
9540: 6c 6f 63 6b 20 70 72 6f 74 6f 63 6f 6c 20 65 72 lock protocol er
9550: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ror */.#define S
9560: 51 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20 QLITE_EMPTY
9570: 20 20 31 36 20 20 20 2f 2a 20 44 61 74 61 62 61 16 /* Databa
9580: 73 65 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23 se is empty */.#
9590: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 43 define SQLITE_SC
95a0: 48 45 4d 41 20 20 20 20 20 20 31 37 20 20 20 2f HEMA 17 /
95b0: 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 73 * The database s
95c0: 63 68 65 6d 61 20 63 68 61 6e 67 65 64 20 2a 2f chema changed */
95d0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
95e0: 54 4f 4f 42 49 47 20 20 20 20 20 20 31 38 20 20 TOOBIG 18
95f0: 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c /* String or BL
9600: 4f 42 20 65 78 63 65 65 64 73 20 73 69 7a 65 20 OB exceeds size
9610: 6c 69 6d 69 74 20 2a 2f 0a 23 64 65 66 69 6e 65 limit */.#define
9620: 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 SQLITE_CONSTRAI
9630: 4e 54 20 20 31 39 20 20 20 2f 2a 20 41 62 6f 72 NT 19 /* Abor
9640: 74 20 64 75 65 20 74 6f 20 63 6f 6e 73 74 72 61 t due to constra
9650: 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f int violation */
9660: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
9670: 4d 49 53 4d 41 54 43 48 20 20 20 20 32 30 20 20 MISMATCH 20
9680: 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20 6d 69 /* Data type mi
9690: 73 6d 61 74 63 68 20 2a 2f 0a 23 64 65 66 69 6e smatch */.#defin
96a0: 65 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 e SQLITE_MISUSE
96b0: 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4c 69 62 21 /* Lib
96c0: 72 61 72 79 20 75 73 65 64 20 69 6e 63 6f 72 72 rary used incorr
96d0: 65 63 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 ectly */.#define
96e0: 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20 SQLITE_NOLFS
96f0: 20 20 20 20 32 32 20 20 20 2f 2a 20 55 73 65 73 22 /* Uses
9700: 20 4f 53 20 66 65 61 74 75 72 65 73 20 6e 6f 74 OS features not
9710: 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 68 6f supported on ho
9720: 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 st */.#define SQ
9730: 4c 49 54 45 5f 41 55 54 48 20 20 20 20 20 20 20 LITE_AUTH
9740: 20 32 33 20 20 20 2f 2a 20 41 75 74 68 6f 72 69 23 /* Authori
9750: 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f zation denied */
9760: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
9770: 46 4f 52 4d 41 54 20 20 20 20 20 20 32 34 20 20 FORMAT 24
9780: 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 20 64 61 /* Auxiliary da
9790: 74 61 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72 tabase format er
97a0: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ror */.#define S
97b0: 51 4c 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20 QLITE_RANGE
97c0: 20 20 32 35 20 20 20 2f 2a 20 32 6e 64 20 70 61 25 /* 2nd pa
97d0: 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
97e0: 65 33 5f 62 69 6e 64 20 6f 75 74 20 6f 66 20 72 e3_bind out of r
97f0: 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ange */.#define
9800: 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20 20 SQLITE_NOTADB
9810: 20 20 20 32 36 20 20 20 2f 2a 20 46 69 6c 65 20 26 /* File
9820: 6f 70 65 6e 65 64 20 74 68 61 74 20 69 73 20 6e opened that is n
9830: 6f 74 20 61 20 64 61 74 61 62 61 73 65 20 66 69 ot a database fi
9840: 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 le */.#define SQ
9850: 4c 49 54 45 5f 52 4f 57 20 20 20 20 20 20 20 20 LITE_ROW
9860: 20 31 30 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 100 /* sqlite3
9870: 5f 73 74 65 70 28 29 20 68 61 73 20 61 6e 6f 74 _step() has anot
9880: 68 65 72 20 72 6f 77 20 72 65 61 64 79 20 2a 2f her row ready */
9890: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
98a0: 44 4f 4e 45 20 20 20 20 20 20 20 20 31 30 31 20 DONE 101
98b0: 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 /* sqlite3_step
98c0: 28 29 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 () has finished
98d0: 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 2f 2a 20 executing */./*
98e0: 65 6e 64 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 end-of-error-cod
98f0: 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 es */../*.** CAP
9900: 49 33 52 45 46 3a 20 45 78 74 65 6e 64 65 64 20 I3REF: Extended
9910: 52 65 73 75 6c 74 20 43 6f 64 65 73 0a 2a 2a 20 Result Codes.**
9920: 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e KEYWORDS: {exten
9930: 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 7d 20 ded error code}
9940: 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 {extended error
9950: 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 codes}.** KEYWOR
9960: 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 72 65 DS: {extended re
9970: 73 75 6c 74 20 63 6f 64 65 7d 20 7b 65 78 74 65 sult code} {exte
9980: 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 nded result code
9990: 73 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 69 74 73 20 s}.**.** In its
99a0: 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 default configur
99b0: 61 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 41 50 ation, SQLite AP
99c0: 49 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 I routines retur
99d0: 6e 20 6f 6e 65 20 6f 66 20 32 36 20 69 6e 74 65 n one of 26 inte
99e0: 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f ger.** [SQLITE_O
99f0: 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64 65 73 K | result codes
9a00: 5d 2e 20 20 48 6f 77 65 76 65 72 2c 20 65 78 70 ]. However, exp
9a10: 65 72 69 65 6e 63 65 20 68 61 73 20 73 68 6f 77 erience has show
9a20: 6e 20 74 68 61 74 20 6d 61 6e 79 20 6f 66 0a 2a n that many of.*
9a30: 2a 20 74 68 65 73 65 20 72 65 73 75 6c 74 20 63 * these result c
9a40: 6f 64 65 73 20 61 72 65 20 74 6f 6f 20 63 6f 61 odes are too coa
9a50: 72 73 65 2d 67 72 61 69 6e 65 64 2e 20 20 54 68 rse-grained. Th
9a60: 65 79 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64 ey do not provid
9a70: 65 20 61 73 0a 2a 2a 20 6d 75 63 68 20 69 6e 66 e as.** much inf
9a80: 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 70 ormation about p
9a90: 72 6f 62 6c 65 6d 73 20 61 73 20 70 72 6f 67 72 roblems as progr
9aa0: 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 6c 69 6b ammers might lik
9ab0: 65 2e 20 20 49 6e 20 61 6e 20 65 66 66 6f 72 74 e. In an effort
9ac0: 20 74 6f 0a 2a 2a 20 61 64 64 72 65 73 73 20 74 to.** address t
9ad0: 68 69 73 2c 20 6e 65 77 65 72 20 76 65 72 73 69 his, newer versi
9ae0: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 28 76 ons of SQLite (v
9af0: 65 72 73 69 6f 6e 20 33 2e 33 2e 38 20 61 6e 64 ersion 3.3.8 and
9b00: 20 6c 61 74 65 72 29 20 69 6e 63 6c 75 64 65 0a later) include.
9b10: 2a 2a 20 73 75 70 70 6f 72 74 20 66 6f 72 20 61 ** support for a
9b20: 64 64 69 74 69 6f 6e 61 6c 20 72 65 73 75 6c 74 dditional result
9b30: 20 63 6f 64 65 73 20 74 68 61 74 20 70 72 6f 76 codes that prov
9b40: 69 64 65 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 ide more detaile
9b50: 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a d information.**
9b60: 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 20 54 about errors. T
9b70: 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 he extended resu
9b80: 6c 74 20 63 6f 64 65 73 20 61 72 65 20 65 6e 61 lt codes are ena
9b90: 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 bled or disabled
9ba0: 0a 2a 2a 20 6f 6e 20 61 20 70 65 72 20 64 61 74 .** on a per dat
9bb0: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
9bc0: 20 62 61 73 69 73 20 75 73 69 6e 67 20 74 68 65 basis using the
9bd0: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 .** [sqlite3_ext
9be0: 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 ended_result_cod
9bf0: 65 73 28 29 5d 20 41 50 49 2e 0a 2a 2a 0a 2a 2a es()] API..**.**
9c00: 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 76 61 Some of the ava
9c10: 69 6c 61 62 6c 65 20 65 78 74 65 6e 64 65 64 20 ilable extended
9c20: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 result codes are
9c30: 20 6c 69 73 74 65 64 20 68 65 72 65 2e 0a 2a 2a listed here..**
9c40: 20 4f 6e 65 20 6d 61 79 20 65 78 70 65 63 74 20 One may expect
9c50: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 78 the number of ex
9c60: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f tended result co
9c70: 64 65 73 20 77 69 6c 6c 20 62 65 20 65 78 70 61 des will be expa
9c80: 6e 64 0a 2a 2a 20 6f 76 65 72 20 74 69 6d 65 2e nd.** over time.
9c90: 20 20 53 6f 66 74 77 61 72 65 20 74 68 61 74 20 Software that
9ca0: 75 73 65 73 20 65 78 74 65 6e 64 65 64 20 72 65 uses extended re
9cb0: 73 75 6c 74 20 63 6f 64 65 73 20 73 68 6f 75 6c sult codes shoul
9cc0: 64 20 65 78 70 65 63 74 0a 2a 2a 20 74 6f 20 73 d expect.** to s
9cd0: 65 65 20 6e 65 77 20 72 65 73 75 6c 74 20 63 6f ee new result co
9ce0: 64 65 73 20 69 6e 20 66 75 74 75 72 65 20 72 65 des in future re
9cf0: 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 leases of SQLite
9d00: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ..**.** The SQLI
9d10: 54 45 5f 4f 4b 20 72 65 73 75 6c 74 20 63 6f 64 TE_OK result cod
9d20: 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 e will never be
9d30: 65 78 74 65 6e 64 65 64 2e 20 20 49 74 20 77 69 extended. It wi
9d40: 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20 ll always.** be
9d50: 65 78 61 63 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2f exactly zero..*/
9d60: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
9d70: 49 4f 45 52 52 5f 52 45 41 44 20 20 20 20 20 20 IOERR_READ
9d80: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
9d90: 49 4f 45 52 52 20 7c 20 28 31 3c 3c 38 29 29 0a IOERR | (1<<8)).
9da0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
9db0: 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 OERR_SHORT_READ
9dc0: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
9dd0: 4f 45 52 52 20 7c 20 28 32 3c 3c 38 29 29 0a 23 OERR | (2<<8)).#
9de0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f define SQLITE_IO
9df0: 45 52 52 5f 57 52 49 54 45 20 20 20 20 20 20 20 ERR_WRITE
9e00: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f (SQLITE_IO
9e10: 45 52 52 20 7c 20 28 33 3c 3c 38 29 29 0a 23 64 ERR | (3<<8)).#d
9e20: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
9e30: 52 52 5f 46 53 59 4e 43 20 20 20 20 20 20 20 20 RR_FSYNC
9e40: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
9e50: 52 52 20 7c 20 28 34 3c 3c 38 29 29 0a 23 64 65 RR | (4<<8)).#de
9e60: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 fine SQLITE_IOER
9e70: 52 5f 44 49 52 5f 46 53 59 4e 43 20 20 20 20 20 R_DIR_FSYNC
9e80: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 (SQLITE_IOER
9e90: 52 20 7c 20 28 35 3c 3c 38 29 29 0a 23 64 65 66 R | (5<<8)).#def
9ea0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
9eb0: 5f 54 52 55 4e 43 41 54 45 20 20 20 20 20 20 20 _TRUNCATE
9ec0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
9ed0: 20 7c 20 28 36 3c 3c 38 29 29 0a 23 64 65 66 69 | (6<<8)).#defi
9ee0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f ne SQLITE_IOERR_
9ef0: 46 53 54 41 54 20 20 20 20 20 20 20 20 20 20 20 FSTAT
9f00: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 (SQLITE_IOERR
9f10: 7c 20 28 37 3c 3c 38 29 29 0a 23 64 65 66 69 6e | (7<<8)).#defin
9f20: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 e SQLITE_IOERR_U
9f30: 4e 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20 NLOCK
9f40: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
9f50: 20 28 38 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 (8<<8)).#define
9f60: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 SQLITE_IOERR_RD
9f70: 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 LOCK
9f80: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 (SQLITE_IOERR |
9f90: 28 39 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 (9<<8)).#define
9fa0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c SQLITE_IOERR_DEL
9fb0: 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 28 ETE (
9fc0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
9fd0: 31 30 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 10<<8)).#define
9fe0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f SQLITE_IOERR_BLO
9ff0: 43 4b 45 44 20 20 20 20 20 20 20 20 20 20 20 28 CKED (
a000: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a010: 31 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 11<<8)).#define
a020: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d SQLITE_IOERR_NOM
a030: 45 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 28 EM (
a040: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a050: 31 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 12<<8)).#define
a060: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 SQLITE_IOERR_ACC
a070: 45 53 53 20 20 20 20 20 20 20 20 20 20 20 20 28 ESS (
a080: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a090: 31 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 13<<8)).#define
a0a0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
a0b0: 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 20 28 CKRESERVEDLOCK (
a0c0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a0d0: 31 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 14<<8)).#define
a0e0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 SQLITE_IOERR_LOC
a0f0: 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 K (
a100: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a110: 31 35 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 15<<8)).#define
a120: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f SQLITE_IOERR_CLO
a130: 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 28 SE (
a140: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a150: 31 36 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 16<<8)).#define
a160: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 SQLITE_IOERR_DIR
a170: 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 20 20 28 _CLOSE (
a180: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a190: 31 37 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 17<<8)).#define
a1a0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d SQLITE_IOERR_SHM
a1b0: 4f 50 45 4e 20 20 20 20 20 20 20 20 20 20 20 28 OPEN (
a1c0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a1d0: 31 38 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 18<<8)).#define
a1e0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d SQLITE_IOERR_SHM
a1f0: 53 49 5a 45 20 20 20 20 20 20 20 20 20 20 20 28 SIZE (
a200: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a210: 31 39 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 19<<8)).#define
a220: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d SQLITE_IOERR_SHM
a230: 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20 28 LOCK (
a240: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a250: 32 30 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 20<<8)).#define
a260: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d SQLITE_IOERR_SHM
a270: 4d 41 50 20 20 20 20 20 20 20 20 20 20 20 20 28 MAP (
a280: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a290: 32 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 21<<8)).#define
a2a0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 45 45 SQLITE_IOERR_SEE
a2b0: 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 K (
a2c0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a2d0: 32 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 22<<8)).#define
a2e0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c SQLITE_IOERR_DEL
a2f0: 45 54 45 5f 4e 4f 45 4e 54 20 20 20 20 20 20 28 ETE_NOENT (
a300: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
a310: 32 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 23<<8)).#define
a320: 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 SQLITE_LOCKED_SH
a330: 41 52 45 44 43 41 43 48 45 20 20 20 20 20 20 28 AREDCACHE (
a340: 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 7c 20 SQLITE_LOCKED |
a350: 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 (1<<8)).#define
a360: 20 53 51 4c 49 54 45 5f 42 55 53 59 5f 52 45 43 SQLITE_BUSY_REC
a370: 4f 56 45 52 59 20 20 20 20 20 20 20 20 20 20 20 OVERY
a380: 28 53 51 4c 49 54 45 5f 42 55 53 59 20 20 20 7c (SQLITE_BUSY |
a390: 20 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e (1<<8)).#defin
a3a0: 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 e SQLITE_CANTOPE
a3b0: 4e 5f 4e 4f 54 45 4d 50 44 49 52 20 20 20 20 20 N_NOTEMPDIR
a3c0: 20 28 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 (SQLITE_CANTOPE
a3d0: 4e 20 7c 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 N | (1<<8)).#def
a3e0: 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f ine SQLITE_CANTO
a3f0: 50 45 4e 5f 49 53 44 49 52 20 20 20 20 20 20 20 PEN_ISDIR
a400: 20 20 20 28 53 51 4c 49 54 45 5f 43 41 4e 54 4f (SQLITE_CANTO
a410: 50 45 4e 20 7c 20 28 32 3c 3c 38 29 29 0a 23 64 PEN | (2<<8)).#d
a420: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e efine SQLITE_CAN
a430: 54 4f 50 45 4e 5f 46 55 4c 4c 50 41 54 48 20 20 TOPEN_FULLPATH
a440: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 43 41 4e (SQLITE_CAN
a450: 54 4f 50 45 4e 20 7c 20 28 33 3c 3c 38 29 29 0a TOPEN | (3<<8)).
a460: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
a470: 4f 52 52 55 50 54 5f 56 54 41 42 20 20 20 20 20 ORRUPT_VTAB
a480: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 43 (SQLITE_C
a490: 4f 52 52 55 50 54 20 7c 20 28 31 3c 3c 38 29 29 ORRUPT | (1<<8))
a4a0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
a4b0: 52 45 41 44 4f 4e 4c 59 5f 52 45 43 4f 56 45 52 READONLY_RECOVER
a4c0: 59 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f Y (SQLITE_
a4d0: 52 45 41 44 4f 4e 4c 59 20 7c 20 28 31 3c 3c 38 READONLY | (1<<8
a4e0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
a4f0: 45 5f 52 45 41 44 4f 4e 4c 59 5f 43 41 4e 54 4c E_READONLY_CANTL
a500: 4f 43 4b 20 20 20 20 20 20 20 28 53 51 4c 49 54 OCK (SQLIT
a510: 45 5f 52 45 41 44 4f 4e 4c 59 20 7c 20 28 32 3c E_READONLY | (2<
a520: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
a530: 49 54 45 5f 41 42 4f 52 54 5f 52 4f 4c 4c 42 41 ITE_ABORT_ROLLBA
a540: 43 4b 20 20 20 20 20 20 20 20 20 20 28 53 51 4c CK (SQL
a550: 49 54 45 5f 41 42 4f 52 54 20 7c 20 28 32 3c 3c ITE_ABORT | (2<<
a560: 38 29 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 8))../*.** CAPI3
a570: 52 45 46 3a 20 46 6c 61 67 73 20 46 6f 72 20 46 REF: Flags For F
a580: 69 6c 65 20 4f 70 65 6e 20 4f 70 65 72 61 74 69 ile Open Operati
a590: 6f 6e 73 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 ons.**.** These
a5a0: 62 69 74 20 76 61 6c 75 65 73 20 61 72 65 20 69 bit values are i
a5b0: 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
a5c0: 69 6e 20 74 68 65 0a 2a 2a 20 33 72 64 20 70 61 in the.** 3rd pa
a5d0: 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 5b rameter to the [
a5e0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
a5f0: 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 )] interface and
a600: 0a 2a 2a 20 69 6e 20 74 68 65 20 34 74 68 20 70 .** in the 4th p
a610: 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 arameter to the
a620: 5b 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 [sqlite3_vfs.xOp
a630: 65 6e 5d 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 23 en] method..*/.#
a640: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a650: 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20 EN_READONLY
a660: 20 20 20 20 30 78 30 30 30 30 30 30 30 31 20 20 0x00000001
a670: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65 /* Ok for sqlite
a680: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 3_open_v2() */.#
a690: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a6a0: 45 4e 5f 52 45 41 44 57 52 49 54 45 20 20 20 20 EN_READWRITE
a6b0: 20 20 20 20 30 78 30 30 30 30 30 30 30 32 20 20 0x00000002
a6c0: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65 /* Ok for sqlite
a6d0: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 3_open_v2() */.#
a6e0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a6f0: 45 4e 5f 43 52 45 41 54 45 20 20 20 20 20 20 20 EN_CREATE
a700: 20 20 20 20 30 78 30 30 30 30 30 30 30 34 20 20 0x00000004
a710: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65 /* Ok for sqlite
a720: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 3_open_v2() */.#
a730: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a740: 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 EN_DELETEONCLOSE
a750: 20 20 20 20 30 78 30 30 30 30 30 30 30 38 20 20 0x00000008
a760: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 /* VFS only */.#
a770: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a780: 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 EN_EXCLUSIVE
a790: 20 20 20 20 30 78 30 30 30 30 30 30 31 30 20 20 0x00000010
a7a0: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 /* VFS only */.#
a7b0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a7c0: 45 4e 5f 41 55 54 4f 50 52 4f 58 59 20 20 20 20 EN_AUTOPROXY
a7d0: 20 20 20 20 30 78 30 30 30 30 30 30 32 30 20 20 0x00000020
a7e0: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 /* VFS only */.#
a7f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a800: 45 4e 5f 55 52 49 20 20 20 20 20 20 20 20 20 20 EN_URI
a810: 20 20 20 20 30 78 30 30 30 30 30 30 34 30 20 20 0x00000040
a820: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65 /* Ok for sqlite
a830: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 3_open_v2() */.#
a840: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a850: 45 4e 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 20 EN_MEMORY
a860: 20 20 20 20 30 78 30 30 30 30 30 30 38 30 20 20 0x00000080
a870: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65 /* Ok for sqlite
a880: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 3_open_v2() */.#
a890: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a8a0: 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20 20 20 EN_MAIN_DB
a8b0: 20 20 20 20 30 78 30 30 30 30 30 31 30 30 20 20 0x00000100
a8c0: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 /* VFS only */.#
a8d0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a8e0: 45 4e 5f 54 45 4d 50 5f 44 42 20 20 20 20 20 20 EN_TEMP_DB
a8f0: 20 20 20 20 30 78 30 30 30 30 30 32 30 30 20 20 0x00000200
a900: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 /* VFS only */.#
a910: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a920: 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 20 EN_TRANSIENT_DB
a930: 20 20 20 20 30 78 30 30 30 30 30 34 30 30 20 20 0x00000400
a940: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 /* VFS only */.#
a950: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a960: 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 EN_MAIN_JOURNAL
a970: 20 20 20 20 30 78 30 30 30 30 30 38 30 30 20 20 0x00000800
a980: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 /* VFS only */.#
a990: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a9a0: 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 EN_TEMP_JOURNAL
a9b0: 20 20 20 20 30 78 30 30 30 30 31 30 30 30 20 20 0x00001000
a9c0: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 /* VFS only */.#
a9d0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
a9e0: 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20 EN_SUBJOURNAL
a9f0: 20 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20 0x00002000
aa00: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 /* VFS only */.#
aa10: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
aa20: 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 EN_MASTER_JOURNA
aa30: 4c 20 20 20 30 78 30 30 30 30 34 30 30 30 20 20 L 0x00004000
aa40: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 /* VFS only */.#
aa50: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
aa60: 45 4e 5f 4e 4f 4d 55 54 45 58 20 20 20 20 20 20 EN_NOMUTEX
aa70: 20 20 20 20 30 78 30 30 30 30 38 30 30 30 20 20 0x00008000
aa80: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65 /* Ok for sqlite
aa90: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 3_open_v2() */.#
aaa0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
aab0: 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 20 20 20 EN_FULLMUTEX
aac0: 20 20 20 20 30 78 30 30 30 31 30 30 30 30 20 20 0x00010000
aad0: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65 /* Ok for sqlite
aae0: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 3_open_v2() */.#
aaf0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
ab00: 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 20 20 EN_SHAREDCACHE
ab10: 20 20 20 20 30 78 30 30 30 32 30 30 30 30 20 20 0x00020000
ab20: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65 /* Ok for sqlite
ab30: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 3_open_v2() */.#
ab40: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
ab50: 45 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45 20 EN_PRIVATECACHE
ab60: 20 20 20 20 30 78 30 30 30 34 30 30 30 30 20 20 0x00040000
ab70: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65 /* Ok for sqlite
ab80: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 3_open_v2() */.#
ab90: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 define SQLITE_OP
aba0: 45 4e 5f 57 41 4c 20 20 20 20 20 20 20 20 20 20 EN_WAL
abb0: 20 20 20 20 30 78 30 30 30 38 30 30 30 30 20 20 0x00080000
abc0: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 0a /* VFS only */..
abd0: 2f 2a 20 52 65 73 65 72 76 65 64 3a 20 20 20 20 /* Reserved:
abe0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
abf0: 20 20 20 20 20 30 78 30 30 46 30 30 30 30 30 20 0x00F00000
ac00: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 */../*.** CAPI3R
ac10: 45 46 3a 20 44 65 76 69 63 65 20 43 68 61 72 61 EF: Device Chara
ac20: 63 74 65 72 69 73 74 69 63 73 0a 2a 2a 0a 2a 2a cteristics.**.**
ac30: 20 54 68 65 20 78 44 65 76 69 63 65 43 68 61 72 The xDeviceChar
ac40: 61 63 74 65 72 69 73 74 69 63 73 20 6d 65 74 68 acteristics meth
ac50: 6f 64 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 od of the [sqlit
ac60: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a e3_io_methods].*
ac70: 2a 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 73 * object returns
ac80: 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 an integer whic
ac90: 68 20 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 h is a vector of
aca0: 20 74 68 65 73 65 0a 2a 2a 20 62 69 74 20 76 61 these.** bit va
acb0: 6c 75 65 73 20 65 78 70 72 65 73 73 69 6e 67 20 lues expressing
acc0: 49 2f 4f 20 63 68 61 72 61 63 74 65 72 69 73 74 I/O characterist
acd0: 69 63 73 20 6f 66 20 74 68 65 20 6d 61 73 73 20 ics of the mass
ace0: 73 74 6f 72 61 67 65 0a 2a 2a 20 64 65 76 69 63 storage.** devic
acf0: 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 e that holds the
ad00: 20 66 69 6c 65 20 74 68 61 74 20 74 68 65 20 5b file that the [
ad10: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
ad20: 64 73 5d 0a 2a 2a 20 72 65 66 65 72 73 20 74 6f ds].** refers to
ad30: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ..**.** The SQLI
ad40: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20 TE_IOCAP_ATOMIC
ad50: 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 property means t
ad60: 68 61 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f hat all writes o
ad70: 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72 f.** any size ar
ad80: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 e atomic. The S
ad90: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
ada0: 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 ICnnn values.**
adb0: 6d 65 61 6e 20 74 68 61 74 20 77 72 69 74 65 73 mean that writes
adc0: 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20 of blocks that
add0: 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e are nnn bytes in
ade0: 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65 size and.** are
adf0: 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61 aligned to an a
ae00: 64 64 72 65 73 73 20 77 68 69 63 68 20 69 73 20 ddress which is
ae10: 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 an integer multi
ae20: 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 ple of.** nnn ar
ae30: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 e atomic. The S
ae40: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 QLITE_IOCAP_SAFE
ae50: 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65 _APPEND value me
ae60: 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e ans.** that when
ae70: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 data is appende
ae80: 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65 d to a file, the
ae90: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 data is appende
aea0: 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20 d.** first then
aeb0: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
aec0: 66 69 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64 file is extended
aed0: 2c 20 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65 , never the othe
aee0: 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e r.** way around.
aef0: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 The SQLITE_IOC
af00: 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72 AP_SEQUENTIAL pr
af10: 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 operty means tha
af20: 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e t.** information
af30: 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64 is written to d
af40: 69 73 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 isk in the same
af50: 6f 72 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a order as calls.*
af60: 2a 20 74 6f 20 78 57 72 69 74 65 28 29 2e 20 20 * to xWrite().
af70: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 The SQLITE_IOCAP
af80: 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 _POWERSAFE_OVERW
af90: 52 49 54 45 20 70 72 6f 70 65 72 74 79 20 6d 65 RITE property me
afa0: 61 6e 73 20 74 68 61 74 0a 2a 2a 20 61 66 74 65 ans that.** afte
afb0: 72 20 72 65 62 6f 6f 74 20 66 6f 6c 6c 6f 77 69 r reboot followi
afc0: 6e 67 20 61 20 63 72 61 73 68 20 6f 72 20 70 6f ng a crash or po
afd0: 77 65 72 20 6c 6f 73 73 2c 20 74 68 65 20 6f 6e wer loss, the on
afe0: 6c 79 20 62 79 74 65 73 20 69 6e 20 61 0a 2a 2a ly bytes in a.**
aff0: 20 66 69 6c 65 20 74 68 61 74 20 77 65 72 65 20 file that were
b000: 77 72 69 74 74 65 6e 20 61 74 20 74 68 65 20 61 written at the a
b010: 70 70 6c 69 63 61 74 69 6f 6e 20 6c 65 76 65 6c pplication level
b020: 20 6d 69 67 68 74 20 68 61 76 65 20 63 68 61 6e might have chan
b030: 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 ged.** and that
b040: 61 64 6a 61 63 65 6e 74 20 62 79 74 65 73 2c 20 adjacent bytes,
b050: 65 76 65 6e 20 62 79 74 65 73 20 77 69 74 68 69 even bytes withi
b060: 6e 20 74 68 65 20 73 61 6d 65 20 73 65 63 74 6f n the same secto
b070: 72 20 61 72 65 0a 2a 2a 20 67 75 61 72 61 6e 74 r are.** guarant
b080: 65 65 64 20 74 6f 20 62 65 20 75 6e 63 68 61 6e eed to be unchan
b090: 67 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ged..*/.#define
b0a0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
b0b0: 4d 49 43 20 20 20 20 20 20 20 20 20 20 20 20 20 MIC
b0c0: 20 20 20 20 30 78 30 30 30 30 30 30 30 31 0a 23 0x00000001.#
b0d0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f define SQLITE_IO
b0e0: 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 20 20 20 CAP_ATOMIC512
b0f0: 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 0x000
b100: 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 00002.#define SQ
b110: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
b120: 43 31 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 C1K
b130: 20 20 30 78 30 30 30 30 30 30 30 34 0a 23 64 65 0x00000004.#de
b140: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
b150: 50 5f 41 54 4f 4d 49 43 32 4b 20 20 20 20 20 20 P_ATOMIC2K
b160: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 0x00000
b170: 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 008.#define SQLI
b180: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34 TE_IOCAP_ATOMIC4
b190: 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K
b1a0: 30 78 30 30 30 30 30 30 31 30 0a 23 64 65 66 69 0x00000010.#defi
b1b0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f ne SQLITE_IOCAP_
b1c0: 41 54 4f 4d 49 43 38 4b 20 20 20 20 20 20 20 20 ATOMIC8K
b1d0: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 32 0x0000002
b1e0: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
b1f0: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b _IOCAP_ATOMIC16K
b200: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 0x
b210: 30 30 30 30 30 30 34 30 0a 23 64 65 66 69 6e 65 00000040.#define
b220: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
b230: 4f 4d 49 43 33 32 4b 20 20 20 20 20 20 20 20 20 OMIC32K
b240: 20 20 20 20 20 30 78 30 30 30 30 30 30 38 30 0a 0x00000080.
b250: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
b260: 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 20 20 OCAP_ATOMIC64K
b270: 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 0x00
b280: 30 30 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53 000100.#define S
b290: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 QLITE_IOCAP_SAFE
b2a0: 5f 41 50 50 45 4e 44 20 20 20 20 20 20 20 20 20 _APPEND
b2b0: 20 20 20 30 78 30 30 30 30 30 32 30 30 0a 23 64 0x00000200.#d
b2c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 efine SQLITE_IOC
b2d0: 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 20 20 AP_SEQUENTIAL
b2e0: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 0x0000
b2f0: 30 34 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0400.#define SQL
b300: 49 54 45 5f 49 4f 43 41 50 5f 55 4e 44 45 4c 45 ITE_IOCAP_UNDELE
b310: 54 41 42 4c 45 5f 57 48 45 4e 5f 4f 50 45 4e 20 TABLE_WHEN_OPEN
b320: 20 30 78 30 30 30 30 30 38 30 30 0a 23 64 65 66 0x00000800.#def
b330: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 ine SQLITE_IOCAP
b340: 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 _POWERSAFE_OVERW
b350: 52 49 54 45 20 20 20 20 30 78 30 30 30 30 31 30 RITE 0x000010
b360: 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 00../*.** CAPI3R
b370: 45 46 3a 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 EF: File Locking
b380: 20 4c 65 76 65 6c 73 0a 2a 2a 0a 2a 2a 20 53 51 Levels.**.** SQ
b390: 4c 69 74 65 20 75 73 65 73 20 6f 6e 65 20 6f 66 Lite uses one of
b3a0: 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 76 these integer v
b3b0: 61 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 63 alues as the sec
b3c0: 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 ond.** argument
b3d0: 74 6f 20 63 61 6c 6c 73 20 69 74 20 6d 61 6b 65 to calls it make
b3e0: 73 20 74 6f 20 74 68 65 20 78 4c 6f 63 6b 28 29 s to the xLock()
b3f0: 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 6d and xUnlock() m
b400: 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e 20 ethods.** of an
b410: 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 [sqlite3_io_meth
b420: 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a ods] object..*/.
b430: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
b440: 4f 43 4b 5f 4e 4f 4e 45 20 20 20 20 20 20 20 20 OCK_NONE
b450: 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 0.#define SQLI
b460: 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 20 20 TE_LOCK_SHARED
b470: 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 1.#define
b480: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 SQLITE_LOCK_RESE
b490: 52 56 45 44 20 20 20 20 20 20 32 0a 23 64 65 66 RVED 2.#def
b4a0: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f ine SQLITE_LOCK_
b4b0: 50 45 4e 44 49 4e 47 20 20 20 20 20 20 20 33 0a PENDING 3.
b4c0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
b4d0: 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 OCK_EXCLUSIVE
b4e0: 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 4../*.** CAPI3
b4f0: 52 45 46 3a 20 53 79 6e 63 68 72 6f 6e 69 7a 61 REF: Synchroniza
b500: 74 69 6f 6e 20 54 79 70 65 20 46 6c 61 67 73 0a tion Type Flags.
b510: 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 74 **.** When SQLit
b520: 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 53 e invokes the xS
b530: 79 6e 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 ync() method of
b540: 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 an.** [sqlite3_i
b550: 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 o_methods] objec
b560: 74 20 69 74 20 75 73 65 73 20 61 20 63 6f 6d 62 t it uses a comb
b570: 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 ination of.** th
b580: 65 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 ese integer valu
b590: 65 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 es as the second
b5a0: 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
b5b0: 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 When the SQLITE
b5c0: 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 66 _SYNC_DATAONLY f
b5d0: 6c 61 67 20 69 73 20 75 73 65 64 2c 20 69 74 20 lag is used, it
b5e0: 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a means that the.*
b5f0: 2a 20 73 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e * sync operation
b600: 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 66 only needs to f
b610: 6c 75 73 68 20 64 61 74 61 20 74 6f 20 6d 61 73 lush data to mas
b620: 73 20 73 74 6f 72 61 67 65 2e 20 20 49 6e 6f 64 s storage. Inod
b630: 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e e.** information
b640: 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 66 6c 75 need not be flu
b650: 73 68 65 64 2e 20 49 66 20 74 68 65 20 6c 6f 77 shed. If the low
b660: 65 72 20 66 6f 75 72 20 62 69 74 73 20 6f 66 20 er four bits of
b670: 74 68 65 20 66 6c 61 67 0a 2a 2a 20 65 71 75 61 the flag.** equa
b680: 6c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f l SQLITE_SYNC_NO
b690: 52 4d 41 4c 2c 20 74 68 61 74 20 6d 65 61 6e 73 RMAL, that means
b6a0: 20 74 6f 20 75 73 65 20 6e 6f 72 6d 61 6c 20 66 to use normal f
b6b0: 73 79 6e 63 28 29 20 73 65 6d 61 6e 74 69 63 73 sync() semantics
b6c0: 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 77 65 ..** If the lowe
b6d0: 72 20 66 6f 75 72 20 62 69 74 73 20 65 71 75 61 r four bits equa
b6e0: 6c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 l SQLITE_SYNC_FU
b6f0: 4c 4c 2c 20 74 68 61 74 20 6d 65 61 6e 73 0a 2a LL, that means.*
b700: 2a 20 74 6f 20 75 73 65 20 4d 61 63 20 4f 53 20 * to use Mac OS
b710: 58 20 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63 X style fullsync
b720: 20 69 6e 73 74 65 61 64 20 6f 66 20 66 73 79 6e instead of fsyn
b730: 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 44 6f 20 6e 6f c()..**.** Do no
b740: 74 20 63 6f 6e 66 75 73 65 20 74 68 65 20 53 51 t confuse the SQ
b750: 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
b760: 20 61 6e 64 20 53 51 4c 49 54 45 5f 53 59 4e 43 and SQLITE_SYNC
b770: 5f 46 55 4c 4c 20 66 6c 61 67 73 0a 2a 2a 20 77 _FULL flags.** w
b780: 69 74 68 20 74 68 65 20 5b 50 52 41 47 4d 41 20 ith the [PRAGMA
b790: 73 79 6e 63 68 72 6f 6e 6f 75 73 5d 3d 4e 4f 52 synchronous]=NOR
b7a0: 4d 41 4c 20 61 6e 64 20 5b 50 52 41 47 4d 41 20 MAL and [PRAGMA
b7b0: 73 79 6e 63 68 72 6f 6e 6f 75 73 5d 3d 46 55 4c synchronous]=FUL
b7c0: 4c 0a 2a 2a 20 73 65 74 74 69 6e 67 73 2e 20 20 L.** settings.
b7d0: 54 68 65 20 5b 73 79 6e 63 68 72 6f 6e 6f 75 73 The [synchronous
b7e0: 20 70 72 61 67 6d 61 5d 20 64 65 74 65 72 6d 69 pragma] determi
b7f0: 6e 65 73 20 77 68 65 6e 20 63 61 6c 6c 73 20 74 nes when calls t
b800: 6f 20 74 68 65 0a 2a 2a 20 78 53 79 6e 63 20 56 o the.** xSync V
b810: 46 53 20 6d 65 74 68 6f 64 20 6f 63 63 75 72 20 FS method occur
b820: 61 6e 64 20 61 70 70 6c 69 65 73 20 75 6e 69 66 and applies unif
b830: 6f 72 6d 6c 79 20 61 63 72 6f 73 73 20 61 6c 6c ormly across all
b840: 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 2a 2a 20 54 platforms..** T
b850: 68 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e he SQLITE_SYNC_N
b860: 4f 52 4d 41 4c 20 61 6e 64 20 53 51 4c 49 54 45 ORMAL and SQLITE
b870: 5f 53 59 4e 43 5f 46 55 4c 4c 20 66 6c 61 67 73 _SYNC_FULL flags
b880: 20 64 65 74 65 72 6d 69 6e 65 20 68 6f 77 0a 2a determine how.*
b890: 2a 20 65 6e 65 72 67 65 74 69 63 20 6f 72 20 72 * energetic or r
b8a0: 69 67 6f 72 6f 75 73 20 6f 72 20 66 6f 72 63 65 igorous or force
b8b0: 66 75 6c 20 74 68 65 20 73 79 6e 63 20 6f 70 65 ful the sync ope
b8c0: 72 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 64 0a rations are and.
b8d0: 2a 2a 20 6f 6e 6c 79 20 6d 61 6b 65 20 61 20 64 ** only make a d
b8e0: 69 66 66 65 72 65 6e 63 65 20 6f 6e 20 4d 61 63 ifference on Mac
b8f0: 20 4f 53 58 20 66 6f 72 20 74 68 65 20 64 65 66 OSX for the def
b900: 61 75 6c 74 20 53 51 4c 69 74 65 20 63 6f 64 65 ault SQLite code
b910: 2e 0a 2a 2a 20 28 54 68 69 72 64 2d 70 61 72 74 ..** (Third-part
b920: 79 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 y VFS implementa
b930: 74 69 6f 6e 73 20 6d 69 67 68 74 20 61 6c 73 6f tions might also
b940: 20 6d 61 6b 65 20 74 68 65 20 64 69 73 74 69 6e make the distin
b950: 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e ction.** between
b960: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 SQLITE_SYNC_NOR
b970: 4d 41 4c 20 61 6e 64 20 53 51 4c 49 54 45 5f 53 MAL and SQLITE_S
b980: 59 4e 43 5f 46 55 4c 4c 2c 20 62 75 74 20 61 6d YNC_FULL, but am
b990: 6f 6e 67 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 ong the.** opera
b9a0: 74 69 6e 67 20 73 79 73 74 65 6d 73 20 6e 61 74 ting systems nat
b9b0: 69 76 65 6c 79 20 73 75 70 70 6f 72 74 65 64 20 ively supported
b9c0: 62 79 20 53 51 4c 69 74 65 2c 20 6f 6e 6c 79 20 by SQLite, only
b9d0: 4d 61 63 20 4f 53 58 0a 2a 2a 20 63 61 72 65 73 Mac OSX.** cares
b9e0: 20 61 62 6f 75 74 20 74 68 65 20 64 69 66 66 65 about the diffe
b9f0: 72 65 6e 63 65 2e 29 0a 2a 2f 0a 23 64 65 66 69 rence.).*/.#defi
ba00: 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e ne SQLITE_SYNC_N
ba10: 4f 52 4d 41 4c 20 20 20 20 20 20 20 20 30 78 30 ORMAL 0x0
ba20: 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 0002.#define SQL
ba30: 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 20 20 ITE_SYNC_FULL
ba40: 20 20 20 20 20 20 20 30 78 30 30 30 30 33 0a 23 0x00003.#
ba50: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 define SQLITE_SY
ba60: 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 20 20 20 20 NC_DATAONLY
ba70: 20 30 78 30 30 30 31 30 0a 0a 2f 2a 0a 2a 2a 20 0x00010../*.**
ba80: 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 CAPI3REF: OS Int
ba90: 65 72 66 61 63 65 20 4f 70 65 6e 20 46 69 6c 65 erface Open File
baa0: 20 48 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 41 6e Handle.**.** An
bab0: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 [sqlite3_file]
bac0: 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 object represent
bad0: 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 69 s an open file i
bae0: 6e 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69 74 n the .** [sqlit
baf0: 65 33 5f 76 66 73 20 7c 20 4f 53 20 69 6e 74 65 e3_vfs | OS inte
bb00: 72 66 61 63 65 20 6c 61 79 65 72 5d 2e 20 20 49 rface layer]. I
bb10: 6e 64 69 76 69 64 75 61 6c 20 4f 53 20 69 6e 74 ndividual OS int
bb20: 65 72 66 61 63 65 0a 2a 2a 20 69 6d 70 6c 65 6d erface.** implem
bb30: 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 0a 2a entations will.*
bb40: 2a 20 77 61 6e 74 20 74 6f 20 73 75 62 63 6c 61 * want to subcla
bb50: 73 73 20 74 68 69 73 20 6f 62 6a 65 63 74 20 62 ss this object b
bb60: 79 20 61 70 70 65 6e 64 69 6e 67 20 61 64 64 69 y appending addi
bb70: 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 0a 2a 2a tional fields.**
bb80: 20 66 6f 72 20 74 68 65 69 72 20 6f 77 6e 20 75 for their own u
bb90: 73 65 2e 20 20 54 68 65 20 70 4d 65 74 68 6f 64 se. The pMethod
bba0: 73 20 65 6e 74 72 79 20 69 73 20 61 20 70 6f 69 s entry is a poi
bbb0: 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 nter to an.** [s
bbc0: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
bbd0: 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64 s] object that d
bbe0: 65 66 69 6e 65 73 20 6d 65 74 68 6f 64 73 20 66 efines methods f
bbf0: 6f 72 20 70 65 72 66 6f 72 6d 69 6e 67 0a 2a 2a or performing.**
bc00: 20 49 2f 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 I/O operations
bc10: 6f 6e 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 on the open file
bc20: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
bc30: 75 63 74 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 uct sqlite3_file
bc40: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 3b 0a 73 sqlite3_file;.s
bc50: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 66 69 truct sqlite3_fi
bc60: 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 74 72 le {. const str
bc70: 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d uct sqlite3_io_m
bc80: 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 ethods *pMethods
bc90: 3b 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f ; /* Methods fo
bca0: 72 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 2a r an open file *
bcb0: 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.};../*.** CAPI
bcc0: 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 3REF: OS Interfa
bcd0: 63 65 20 46 69 6c 65 20 56 69 72 74 75 61 6c 20 ce File Virtual
bce0: 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 0a 2a Methods Object.*
bcf0: 2a 0a 2a 2a 20 45 76 65 72 79 20 66 69 6c 65 20 *.** Every file
bd00: 6f 70 65 6e 65 64 20 62 79 20 74 68 65 20 5b 73 opened by the [s
bd10: 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e qlite3_vfs.xOpen
bd20: 5d 20 6d 65 74 68 6f 64 20 70 6f 70 75 6c 61 74 ] method populat
bd30: 65 73 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 es an.** [sqlite
bd40: 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20 28 3_file] object (
bd50: 6f 72 2c 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 6c or, more commonl
bd60: 79 2c 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 y, a subclass of
bd70: 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
bd80: 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 29 20 77 _file] object) w
bd90: 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ith a pointer to
bda0: 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
bdb0: 74 68 69 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 this object..**
bdc0: 54 68 69 73 20 6f 62 6a 65 63 74 20 64 65 66 69 This object defi
bdd0: 6e 65 73 20 74 68 65 20 6d 65 74 68 6f 64 73 20 nes the methods
bde0: 75 73 65 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 used to perform
bdf0: 76 61 72 69 6f 75 73 20 6f 70 65 72 61 74 69 6f various operatio
be00: 6e 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74 68 ns.** against th
be10: 65 20 6f 70 65 6e 20 66 69 6c 65 20 72 65 70 72 e open file repr
be20: 65 73 65 6e 74 65 64 20 62 79 20 74 68 65 20 5b esented by the [
be30: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 sqlite3_file] ob
be40: 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ject..**.** If t
be50: 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 2e he [sqlite3_vfs.
be60: 78 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 73 65 xOpen] method se
be70: 74 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 ts the sqlite3_f
be80: 69 6c 65 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65 ile.pMethods ele
be90: 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 61 20 6e 6f ment .** to a no
bea0: 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 n-NULL pointer,
beb0: 74 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 then the sqlite3
bec0: 5f 69 6f 5f 6d 65 74 68 6f 64 73 2e 78 43 6c 6f _io_methods.xClo
bed0: 73 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 6d 61 79 se method.** may
bee0: 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 76 65 6e be invoked even
bef0: 20 69 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 if the [sqlite3
bf00: 5f 76 66 73 2e 78 4f 70 65 6e 5d 20 72 65 70 6f _vfs.xOpen] repo
bf10: 72 74 65 64 20 74 68 61 74 20 69 74 20 66 61 69 rted that it fai
bf20: 6c 65 64 2e 20 20 54 68 65 0a 2a 2a 20 6f 6e 6c led. The.** onl
bf30: 79 20 77 61 79 20 74 6f 20 70 72 65 76 65 6e 74 y way to prevent
bf40: 20 61 20 63 61 6c 6c 20 74 6f 20 78 43 6c 6f 73 a call to xClos
bf50: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 66 61 e following a fa
bf60: 69 6c 65 64 20 5b 73 71 6c 69 74 65 33 5f 76 66 iled [sqlite3_vf
bf70: 73 2e 78 4f 70 65 6e 5d 0a 2a 2a 20 69 73 20 66 s.xOpen].** is f
bf80: 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f or the [sqlite3_
bf90: 76 66 73 2e 78 4f 70 65 6e 5d 20 74 6f 20 73 65 vfs.xOpen] to se
bfa0: 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 t the sqlite3_fi
bfb0: 6c 65 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d le.pMethods elem
bfc0: 65 6e 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a ent.** to NULL..
bfd0: 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 **.** The flags
bfe0: 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 79 6e argument to xSyn
bff0: 63 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 c may be one of
c000: 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 [SQLITE_SYNC_NOR
c010: 4d 41 4c 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 MAL] or.** [SQLI
c020: 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20 TE_SYNC_FULL].
c030: 54 68 65 20 66 69 72 73 74 20 63 68 6f 69 63 65 The first choice
c040: 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 66 is the normal f
c050: 73 79 6e 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 sync()..** The s
c060: 65 63 6f 6e 64 20 63 68 6f 69 63 65 20 69 73 20 econd choice is
c070: 61 20 4d 61 63 20 4f 53 20 58 20 73 74 79 6c 65 a Mac OS X style
c080: 20 66 75 6c 6c 73 79 6e 63 2e 20 20 54 68 65 20 fullsync. The
c090: 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 [SQLITE_SYNC_DAT
c0a0: 41 4f 4e 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20 6d AONLY].** flag m
c0b0: 61 79 20 62 65 20 4f 52 65 64 20 69 6e 20 74 6f ay be ORed in to
c0c0: 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 6f indicate that o
c0d0: 6e 6c 79 20 74 68 65 20 64 61 74 61 20 6f 66 20 nly the data of
c0e0: 74 68 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 the file.** and
c0f0: 6e 6f 74 20 69 74 73 20 69 6e 6f 64 65 20 6e 65 not its inode ne
c100: 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 65 64 eds to be synced
c110: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 ..**.** The inte
c120: 67 65 72 20 76 61 6c 75 65 73 20 74 6f 20 78 4c ger values to xL
c130: 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 ock() and xUnloc
c140: 6b 28 29 20 61 72 65 20 6f 6e 65 20 6f 66 0a 2a k() are one of.*
c150: 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b * <ul>.** <li> [
c160: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 SQLITE_LOCK_NONE
c170: 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 ],.** <li> [SQLI
c180: 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c TE_LOCK_SHARED],
c190: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
c1a0: 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c _LOCK_RESERVED],
c1b0: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
c1c0: 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 _LOCK_PENDING],
c1d0: 6f 72 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 or.** <li> [SQLI
c1e0: 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 TE_LOCK_EXCLUSIV
c1f0: 45 5d 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 E]..** </ul>.**
c200: 78 4c 6f 63 6b 28 29 20 69 6e 63 72 65 61 73 65 xLock() increase
c210: 73 20 74 68 65 20 6c 6f 63 6b 2e 20 78 55 6e 6c s the lock. xUnl
c220: 6f 63 6b 28 29 20 64 65 63 72 65 61 73 65 73 20 ock() decreases
c230: 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 65 the lock..** The
c240: 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
c250: 6f 63 6b 28 29 20 6d 65 74 68 6f 64 20 63 68 65 ock() method che
c260: 63 6b 73 20 77 68 65 74 68 65 72 20 61 6e 79 20 cks whether any
c270: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
c280: 69 6f 6e 2c 0a 2a 2a 20 65 69 74 68 65 72 20 69 ion,.** either i
c290: 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 6f n this process o
c2a0: 72 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20 r in some other
c2b0: 70 72 6f 63 65 73 73 2c 20 69 73 20 68 6f 6c 64 process, is hold
c2c0: 69 6e 67 20 61 20 52 45 53 45 52 56 45 44 2c 0a ing a RESERVED,.
c2d0: 2a 2a 20 50 45 4e 44 49 4e 47 2c 20 6f 72 20 45 ** PENDING, or E
c2e0: 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e XCLUSIVE lock on
c2f0: 20 74 68 65 20 66 69 6c 65 2e 20 20 49 74 20 72 the file. It r
c300: 65 74 75 72 6e 73 20 74 72 75 65 0a 2a 2a 20 69 eturns true.** i
c310: 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 65 78 f such a lock ex
c320: 69 73 74 73 20 61 6e 64 20 66 61 6c 73 65 20 6f ists and false o
c330: 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 therwise..**.**
c340: 54 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c The xFileControl
c350: 28 29 20 6d 65 74 68 6f 64 20 69 73 20 61 20 67 () method is a g
c360: 65 6e 65 72 69 63 20 69 6e 74 65 72 66 61 63 65 eneric interface
c370: 20 74 68 61 74 20 61 6c 6c 6f 77 73 20 63 75 73 that allows cus
c380: 74 6f 6d 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 tom.** VFS imple
c390: 6d 65 6e 74 61 74 69 6f 6e 73 20 74 6f 20 64 69 mentations to di
c3a0: 72 65 63 74 6c 79 20 63 6f 6e 74 72 6f 6c 20 61 rectly control a
c3b0: 6e 20 6f 70 65 6e 20 66 69 6c 65 20 75 73 69 6e n open file usin
c3c0: 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 g the.** [sqlite
c3d0: 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 3_file_control()
c3e0: 5d 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54 68 ] interface. Th
c3f0: 65 20 73 65 63 6f 6e 64 20 22 6f 70 22 20 61 72 e second "op" ar
c400: 67 75 6d 65 6e 74 20 69 73 20 61 6e 0a 2a 2a 20 gument is an.**
c410: 69 6e 74 65 67 65 72 20 6f 70 63 6f 64 65 2e 20 integer opcode.
c420: 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d The third argum
c430: 65 6e 74 20 69 73 20 61 20 67 65 6e 65 72 69 63 ent is a generic
c440: 20 70 6f 69 6e 74 65 72 20 69 6e 74 65 6e 64 65 pointer intende
c450: 64 20 74 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f d to.** point to
c460: 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61 a structure tha
c470: 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 72 t may contain ar
c480: 67 75 6d 65 6e 74 73 20 6f 72 20 73 70 61 63 65 guments or space
c490: 20 69 6e 20 77 68 69 63 68 20 74 6f 0a 2a 2a 20 in which to.**
c4a0: 77 72 69 74 65 20 72 65 74 75 72 6e 20 76 61 6c write return val
c4b0: 75 65 73 2e 20 20 50 6f 74 65 6e 74 69 61 6c 20 ues. Potential
c4c0: 75 73 65 73 20 66 6f 72 20 78 46 69 6c 65 43 6f uses for xFileCo
c4d0: 6e 74 72 6f 6c 28 29 20 6d 69 67 68 74 20 62 65 ntrol() might be
c4e0: 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f .** functions to
c4f0: 20 65 6e 61 62 6c 65 20 62 6c 6f 63 6b 69 6e 67 enable blocking
c500: 20 6c 6f 63 6b 73 20 77 69 74 68 20 74 69 6d 65 locks with time
c510: 6f 75 74 73 2c 20 74 6f 20 63 68 61 6e 67 65 20 outs, to change
c520: 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 the.** locking s
c530: 74 72 61 74 65 67 79 20 28 66 6f 72 20 65 78 61 trategy (for exa
c540: 6d 70 6c 65 20 74 6f 20 75 73 65 20 64 6f 74 2d mple to use dot-
c550: 66 69 6c 65 20 6c 6f 63 6b 73 29 2c 20 74 6f 20 file locks), to
c560: 69 6e 71 75 69 72 65 0a 2a 2a 20 61 62 6f 75 74 inquire.** about
c570: 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 61 the status of a
c580: 20 6c 6f 63 6b 2c 20 6f 72 20 74 6f 20 62 72 65 lock, or to bre
c590: 61 6b 20 73 74 61 6c 65 20 6c 6f 63 6b 73 2e 20 ak stale locks.
c5a0: 20 54 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63 The SQLite.** c
c5b0: 6f 72 65 20 72 65 73 65 72 76 65 73 20 61 6c 6c ore reserves all
c5c0: 20 6f 70 63 6f 64 65 73 20 6c 65 73 73 20 74 68 opcodes less th
c5d0: 61 6e 20 31 30 30 20 66 6f 72 20 69 74 73 20 6f an 100 for its o
c5e0: 77 6e 20 75 73 65 2e 0a 2a 2a 20 41 20 5b 53 51 wn use..** A [SQ
c5f0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 LITE_FCNTL_LOCKS
c600: 54 41 54 45 20 7c 20 6c 69 73 74 20 6f 66 20 6f TATE | list of o
c610: 70 63 6f 64 65 73 5d 20 6c 65 73 73 20 74 68 61 pcodes] less tha
c620: 6e 20 31 30 30 20 69 73 20 61 76 61 69 6c 61 62 n 100 is availab
c630: 6c 65 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 le..** Applicati
c640: 6f 6e 73 20 74 68 61 74 20 64 65 66 69 6e 65 20 ons that define
c650: 61 20 63 75 73 74 6f 6d 20 78 46 69 6c 65 43 6f a custom xFileCo
c660: 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 73 68 6f ntrol method sho
c670: 75 6c 64 20 75 73 65 20 6f 70 63 6f 64 65 73 0a uld use opcodes.
c680: 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 ** greater than
c690: 31 30 30 20 74 6f 20 61 76 6f 69 64 20 63 6f 6e 100 to avoid con
c6a0: 66 6c 69 63 74 73 2e 20 20 56 46 53 20 69 6d 70 flicts. VFS imp
c6b0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f lementations sho
c6c0: 75 6c 64 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 uld.** return [S
c6d0: 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 5d 20 QLITE_NOTFOUND]
c6e0: 66 6f 72 20 66 69 6c 65 20 63 6f 6e 74 72 6f 6c for file control
c6f0: 20 6f 70 63 6f 64 65 73 20 74 68 61 74 20 74 68 opcodes that th
c700: 65 79 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65 63 ey do not.** rec
c710: 6f 67 6e 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 ognize..**.** Th
c720: 65 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20 e xSectorSize()
c730: 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 74 method returns t
c740: 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f he sector size o
c750: 66 20 74 68 65 0a 2a 2a 20 64 65 76 69 63 65 20 f the.** device
c760: 74 68 61 74 20 75 6e 64 65 72 6c 69 65 73 20 74 that underlies t
c770: 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 73 65 he file. The se
c780: 63 74 6f 72 20 73 69 7a 65 20 69 73 20 74 68 65 ctor size is the
c790: 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 77 72 69 74 .** minimum writ
c7a0: 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 65 e that can be pe
c7b0: 72 66 6f 72 6d 65 64 20 77 69 74 68 6f 75 74 20 rformed without
c7c0: 64 69 73 74 75 72 62 69 6e 67 0a 2a 2a 20 6f 74 disturbing.** ot
c7d0: 68 65 72 20 62 79 74 65 73 20 69 6e 20 74 68 65 her bytes in the
c7e0: 20 66 69 6c 65 2e 20 20 54 68 65 20 78 44 65 76 file. The xDev
c7f0: 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
c800: 63 73 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 cs().** method r
c810: 65 74 75 72 6e 73 20 61 20 62 69 74 20 76 65 63 eturns a bit vec
c820: 74 6f 72 20 64 65 73 63 72 69 62 69 6e 67 20 62 tor describing b
c830: 65 68 61 76 69 6f 72 73 20 6f 66 20 74 68 65 0a ehaviors of the.
c840: 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 65 ** underlying de
c850: 76 69 63 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e vice:.**.** <ul>
c860: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
c870: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 5d 0a 2a _IOCAP_ATOMIC].*
c880: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 * <li> [SQLITE_I
c890: 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 5d 0a OCAP_ATOMIC512].
c8a0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
c8b0: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 4b 5d 0a IOCAP_ATOMIC1K].
c8c0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
c8d0: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32 4b 5d 0a IOCAP_ATOMIC2K].
c8e0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
c8f0: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34 4b 5d 0a IOCAP_ATOMIC4K].
c900: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
c910: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 38 4b 5d 0a IOCAP_ATOMIC8K].
c920: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
c930: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b 5d IOCAP_ATOMIC16K]
c940: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 .** <li> [SQLITE
c950: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33 32 4b _IOCAP_ATOMIC32K
c960: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 ].** <li> [SQLIT
c970: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 E_IOCAP_ATOMIC64
c980: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 K].** <li> [SQLI
c990: 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 TE_IOCAP_SAFE_AP
c9a0: 50 45 4e 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 PEND].** <li> [S
c9b0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 QLITE_IOCAP_SEQU
c9c0: 45 4e 54 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e ENTIAL].** </ul>
c9d0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 .**.** The SQLIT
c9e0: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20 70 E_IOCAP_ATOMIC p
c9f0: 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 roperty means th
ca00: 61 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f 66 at all writes of
ca10: 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72 65 .** any size are
ca20: 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 atomic. The SQ
ca30: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
ca40: 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6d Cnnn values.** m
ca50: 65 61 6e 20 74 68 61 74 20 77 72 69 74 65 73 20 ean that writes
ca60: 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20 61 of blocks that a
ca70: 72 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e 20 re nnn bytes in
ca80: 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65 20 size and.** are
ca90: 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61 64 aligned to an ad
caa0: 64 72 65 73 73 20 77 68 69 63 68 20 69 73 20 61 dress which is a
cab0: 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 n integer multip
cac0: 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 le of.** nnn are
cad0: 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 atomic. The SQ
cae0: 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f LITE_IOCAP_SAFE_
caf0: 41 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65 61 APPEND value mea
cb00: 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e 20 ns.** that when
cb10: 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 64 data is appended
cb20: 20 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65 20 to a file, the
cb30: 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 64 data is appended
cb40: 0a 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20 74 .** first then t
cb50: 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 he size of the f
cb60: 69 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64 2c ile is extended,
cb70: 20 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65 72 never the other
cb80: 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e 20 .** way around.
cb90: 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 The SQLITE_IOCA
cba0: 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72 6f P_SEQUENTIAL pro
cbb0: 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 perty means that
cbc0: 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 .** information
cbd0: 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 is written to di
cbe0: 73 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f sk in the same o
cbf0: 72 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a 2a rder as calls.**
cc00: 20 74 6f 20 78 57 72 69 74 65 28 29 2e 0a 2a 2a to xWrite()..**
cc10: 0a 2a 2a 20 49 66 20 78 52 65 61 64 28 29 20 72 .** If xRead() r
cc20: 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 4f eturns SQLITE_IO
cc30: 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 ERR_SHORT_READ i
cc40: 74 20 6d 75 73 74 20 61 6c 73 6f 20 66 69 6c 6c t must also fill
cc50: 0a 2a 2a 20 69 6e 20 74 68 65 20 75 6e 72 65 61 .** in the unrea
cc60: 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 d portions of th
cc70: 65 20 62 75 66 66 65 72 20 77 69 74 68 20 7a 65 e buffer with ze
cc80: 72 6f 73 2e 20 20 41 20 56 46 53 20 74 68 61 74 ros. A VFS that
cc90: 0a 2a 2a 20 66 61 69 6c 73 20 74 6f 20 7a 65 72 .** fails to zer
cca0: 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 o-fill short rea
ccb0: 64 73 20 6d 69 67 68 74 20 73 65 65 6d 20 74 6f ds might seem to
ccc0: 20 77 6f 72 6b 2e 20 20 48 6f 77 65 76 65 72 2c work. However,
ccd0: 0a 2a 2a 20 66 61 69 6c 75 72 65 20 74 6f 20 7a .** failure to z
cce0: 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 ero-fill short r
ccf0: 65 61 64 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 eads will eventu
cd00: 61 6c 6c 79 20 6c 65 61 64 20 74 6f 0a 2a 2a 20 ally lead to.**
cd10: 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 database corrupt
cd20: 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ion..*/.typedef
cd30: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 struct sqlite3_i
cd40: 6f 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 o_methods sqlite
cd50: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 3_io_methods;.st
cd60: 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f ruct sqlite3_io_
cd70: 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20 methods {. int
cd80: 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 iVersion;. int
cd90: 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 (*xClose)(sqlite
cda0: 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 3_file*);. int
cdb0: 28 2a 78 52 65 61 64 29 28 73 71 6c 69 74 65 33 (*xRead)(sqlite3
cdc0: 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 _file*, void*, i
cdd0: 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 nt iAmt, sqlite3
cde0: 5f 69 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 _int64 iOfst);.
cdf0: 20 69 6e 74 20 28 2a 78 57 72 69 74 65 29 28 73 int (*xWrite)(s
ce00: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f qlite3_file*, co
ce10: 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 nst void*, int i
ce20: 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 Amt, sqlite3_int
ce30: 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 64 iOfst);. int
ce40: 20 28 2a 78 54 72 75 6e 63 61 74 65 29 28 73 71 (*xTruncate)(sq
ce50: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c lite3_file*, sql
ce60: 69 74 65 33 5f 69 6e 74 36 34 20 73 69 7a 65 29 ite3_int64 size)
ce70: 3b 0a 20 20 69 6e 74 20 28 2a 78 53 79 6e 63 29 ;. int (*xSync)
ce80: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 (sqlite3_file*,
ce90: 69 6e 74 20 66 6c 61 67 73 29 3b 0a 20 20 69 6e int flags);. in
cea0: 74 20 28 2a 78 46 69 6c 65 53 69 7a 65 29 28 73 t (*xFileSize)(s
ceb0: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 qlite3_file*, sq
cec0: 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 lite3_int64 *pSi
ced0: 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4c 6f ze);. int (*xLo
cee0: 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck)(sqlite3_file
cef0: 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 *, int);. int (
cf00: 2a 78 55 6e 6c 6f 63 6b 29 28 73 71 6c 69 74 65 *xUnlock)(sqlite
cf10: 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 3_file*, int);.
cf20: 20 69 6e 74 20 28 2a 78 43 68 65 63 6b 52 65 73 int (*xCheckRes
cf30: 65 72 76 65 64 4c 6f 63 6b 29 28 73 71 6c 69 74 ervedLock)(sqlit
cf40: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 2a 70 e3_file*, int *p
cf50: 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 ResOut);. int (
cf60: 2a 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 29 28 73 *xFileControl)(s
cf70: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
cf80: 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 t op, void *pArg
cf90: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 74 );. int (*xSect
cfa0: 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 5f orSize)(sqlite3_
cfb0: 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a file*);. int (*
cfc0: 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 xDeviceCharacter
cfd0: 69 73 74 69 63 73 29 28 73 71 6c 69 74 65 33 5f istics)(sqlite3_
cfe0: 66 69 6c 65 2a 29 3b 0a 20 20 2f 2a 20 4d 65 74 file*);. /* Met
cff0: 68 6f 64 73 20 61 62 6f 76 65 20 61 72 65 20 76 hods above are v
d000: 61 6c 69 64 20 66 6f 72 20 76 65 72 73 69 6f 6e alid for version
d010: 20 31 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 1 */. int (*xS
d020: 68 6d 4d 61 70 29 28 73 71 6c 69 74 65 33 5f 66 hmMap)(sqlite3_f
d030: 69 6c 65 2a 2c 20 69 6e 74 20 69 50 67 2c 20 69 ile*, int iPg, i
d040: 6e 74 20 70 67 73 7a 2c 20 69 6e 74 2c 20 76 6f nt pgsz, int, vo
d050: 69 64 20 76 6f 6c 61 74 69 6c 65 2a 2a 29 3b 0a id volatile**);.
d060: 20 20 69 6e 74 20 28 2a 78 53 68 6d 4c 6f 63 6b int (*xShmLock
d070: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c )(sqlite3_file*,
d080: 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 69 6e 74 int offset, int
d090: 20 6e 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a n, int flags);.
d0a0: 20 20 76 6f 69 64 20 28 2a 78 53 68 6d 42 61 72 void (*xShmBar
d0b0: 72 69 65 72 29 28 73 71 6c 69 74 65 33 5f 66 69 rier)(sqlite3_fi
d0c0: 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 le*);. int (*xS
d0d0: 68 6d 55 6e 6d 61 70 29 28 73 71 6c 69 74 65 33 hmUnmap)(sqlite3
d0e0: 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 64 65 6c 65 _file*, int dele
d0f0: 74 65 46 6c 61 67 29 3b 0a 20 20 2f 2a 20 4d 65 teFlag);. /* Me
d100: 74 68 6f 64 73 20 61 62 6f 76 65 20 61 72 65 20 thods above are
d110: 76 61 6c 69 64 20 66 6f 72 20 76 65 72 73 69 6f valid for versio
d120: 6e 20 32 20 2a 2f 0a 20 20 2f 2a 20 41 64 64 69 n 2 */. /* Addi
d130: 74 69 6f 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d tional methods m
d140: 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 ay be added in f
d150: 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 2a uture releases *
d160: 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.};../*.** CAPI
d170: 33 52 45 46 3a 20 53 74 61 6e 64 61 72 64 20 46 3REF: Standard F
d180: 69 6c 65 20 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f ile Control Opco
d190: 64 65 73 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 des.**.** These
d1a0: 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 integer constant
d1b0: 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f s are opcodes fo
d1c0: 72 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 r the xFileContr
d1d0: 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 ol method.** of
d1e0: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f the [sqlite3_io_
d1f0: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 methods] object
d200: 61 6e 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c and for the [sql
d210: 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f ite3_file_contro
d220: 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 l()].** interfac
d230: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 e..**.** The [SQ
d240: 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 LITE_FCNTL_LOCKS
d250: 54 41 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 TATE] opcode is
d260: 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 used for debuggi
d270: 6e 67 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 ng. This.** opc
d280: 6f 64 65 20 63 61 75 73 65 73 20 74 68 65 20 78 ode causes the x
d290: 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 FileControl meth
d2a0: 6f 64 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 od to write the
d2b0: 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 current state of
d2c0: 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e .** the lock (on
d2d0: 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 e of [SQLITE_LOC
d2e0: 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 K_NONE], [SQLITE
d2f0: 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a _LOCK_SHARED],.*
d300: 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 * [SQLITE_LOCK_R
d310: 45 53 45 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54 ESERVED], [SQLIT
d320: 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c E_LOCK_PENDING],
d330: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b or [SQLITE_LOCK
d340: 5f 45 58 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20 _EXCLUSIVE]).**
d350: 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 into an integer
d360: 74 68 61 74 20 74 68 65 20 70 41 72 67 20 61 72 that the pArg ar
d370: 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f gument points to
d380: 2e 20 54 68 69 73 20 63 61 70 61 62 69 6c 69 74 . This capabilit
d390: 79 0a 2a 2a 20 69 73 20 75 73 65 64 20 64 75 72 y.** is used dur
d3a0: 69 6e 67 20 74 65 73 74 69 6e 67 20 61 6e 64 20 ing testing and
d3b0: 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 62 65 only needs to be
d3c0: 20 73 75 70 70 6f 72 74 65 64 20 77 68 65 6e 20 supported when
d3d0: 53 51 4c 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69 SQLITE_TEST.** i
d3e0: 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 3c 75 s defined..** <u
d3f0: 6c 3e 0a 2a 2a 20 3c 6c 69 3e 5b 5b 53 51 4c 49 l>.** <li>[[SQLI
d400: 54 45 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 49 TE_FCNTL_SIZE_HI
d410: 4e 54 5d 5d 0a 2a 2a 20 54 68 65 20 5b 53 51 4c NT]].** The [SQL
d420: 49 54 45 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 ITE_FCNTL_SIZE_H
d430: 49 4e 54 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 INT] opcode is u
d440: 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 74 6f sed by SQLite to
d450: 20 67 69 76 65 20 74 68 65 20 56 46 53 0a 2a 2a give the VFS.**
d460: 20 6c 61 79 65 72 20 61 20 68 69 6e 74 20 6f 66 layer a hint of
d470: 20 68 6f 77 20 6c 61 72 67 65 20 74 68 65 20 64 how large the d
d480: 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 6c atabase file wil
d490: 6c 20 67 72 6f 77 20 74 6f 20 62 65 20 64 75 72 l grow to be dur
d4a0: 69 6e 67 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 ing the.** curre
d4b0: 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 nt transaction.
d4c0: 20 54 68 69 73 20 68 69 6e 74 20 69 73 20 6e 6f This hint is no
d4d0: 74 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 t guaranteed to
d4e0: 62 65 20 61 63 63 75 72 61 74 65 20 62 75 74 20 be accurate but
d4f0: 69 74 0a 2a 2a 20 69 73 20 6f 66 74 65 6e 20 63 it.** is often c
d500: 6c 6f 73 65 2e 20 20 54 68 65 20 75 6e 64 65 72 lose. The under
d510: 6c 79 69 6e 67 20 56 46 53 20 6d 69 67 68 74 20 lying VFS might
d520: 63 68 6f 6f 73 65 20 74 6f 20 70 72 65 61 6c 6c choose to preall
d530: 6f 63 61 74 65 20 64 61 74 61 62 61 73 65 0a 2a ocate database.*
d540: 2a 20 66 69 6c 65 20 73 70 61 63 65 20 62 61 73 * file space bas
d550: 65 64 20 6f 6e 20 74 68 69 73 20 68 69 6e 74 20 ed on this hint
d560: 69 6e 20 6f 72 64 65 72 20 74 6f 20 68 65 6c 70 in order to help
d570: 20 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 64 writes to the d
d580: 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 atabase.** file
d590: 72 75 6e 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a run faster..**.*
d5a0: 2a 20 3c 6c 69 3e 5b 5b 53 51 4c 49 54 45 5f 46 * <li>[[SQLITE_F
d5b0: 43 4e 54 4c 5f 43 48 55 4e 4b 5f 53 49 5a 45 5d CNTL_CHUNK_SIZE]
d5c0: 5d 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 ].** The [SQLITE
d5d0: 5f 46 43 4e 54 4c 5f 43 48 55 4e 4b 5f 53 49 5a _FCNTL_CHUNK_SIZ
d5e0: 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 E] opcode is use
d5f0: 64 20 74 6f 20 72 65 71 75 65 73 74 20 74 68 61 d to request tha
d600: 74 20 74 68 65 20 56 46 53 0a 2a 2a 20 65 78 74 t the VFS.** ext
d610: 65 6e 64 73 20 61 6e 64 20 74 72 75 6e 63 61 74 ends and truncat
d620: 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 es the database
d630: 66 69 6c 65 20 69 6e 20 63 68 75 6e 6b 73 20 6f file in chunks o
d640: 66 20 61 20 73 69 7a 65 20 73 70 65 63 69 66 69 f a size specifi
d650: 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 75 73 65 ed.** by the use
d660: 72 2e 20 54 68 65 20 66 6f 75 72 74 68 20 61 72 r. The fourth ar
d670: 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 gument to [sqlit
d680: 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 e3_file_control(
d690: 29 5d 20 73 68 6f 75 6c 64 20 0a 2a 2a 20 70 6f )] should .** po
d6a0: 69 6e 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 int to an intege
d6b0: 72 20 28 74 79 70 65 20 69 6e 74 29 20 63 6f 6e r (type int) con
d6c0: 74 61 69 6e 69 6e 67 20 74 68 65 20 6e 65 77 20 taining the new
d6d0: 63 68 75 6e 6b 2d 73 69 7a 65 20 74 6f 20 75 73 chunk-size to us
d6e0: 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6e 6f 6d e.** for the nom
d6f0: 69 6e 61 74 65 64 20 64 61 74 61 62 61 73 65 2e inated database.
d700: 20 41 6c 6c 6f 63 61 74 69 6e 67 20 64 61 74 61 Allocating data
d710: 62 61 73 65 20 66 69 6c 65 20 73 70 61 63 65 20 base file space
d720: 69 6e 20 6c 61 72 67 65 0a 2a 2a 20 63 68 75 6e in large.** chun
d730: 6b 73 20 28 73 61 79 20 31 4d 42 20 61 74 20 61 ks (say 1MB at a
d740: 20 74 69 6d 65 29 2c 20 6d 61 79 20 72 65 64 75 time), may redu
d750: 63 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 ce file-system f
d760: 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 61 6e 64 ragmentation and
d770: 0a 2a 2a 20 69 6d 70 72 6f 76 65 20 70 65 72 66 .** improve perf
d780: 6f 72 6d 61 6e 63 65 20 6f 6e 20 73 6f 6d 65 20 ormance on some
d790: 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 3c systems..**.** <
d7a0: 6c 69 3e 5b 5b 53 51 4c 49 54 45 5f 46 43 4e 54 li>[[SQLITE_FCNT
d7b0: 4c 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 5d 5d L_FILE_POINTER]]
d7c0: 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f .** The [SQLITE_
d7d0: 46 43 4e 54 4c 5f 46 49 4c 45 5f 50 4f 49 4e 54 FCNTL_FILE_POINT
d7e0: 45 52 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73 ER] opcode is us
d7f0: 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 70 ed to obtain a p
d800: 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 ointer.** to the
d810: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 [sqlite3_file]
d820: 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 object associate
d830: 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 d with a particu
d840: 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 lar database.**
d850: 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 53 65 65 connection. See
d860: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 the [sqlite3_fi
d870: 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 64 6f le_control()] do
d880: 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 0a cumentation for.
d890: 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e ** additional in
d8a0: 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a formation..**.**
d8b0: 20 3c 6c 69 3e 5b 5b 53 51 4c 49 54 45 5f 46 43 <li>[[SQLITE_FC
d8c0: 4e 54 4c 5f 53 59 4e 43 5f 4f 4d 49 54 54 45 44 NTL_SYNC_OMITTED
d8d0: 5d 5d 0a 2a 2a 20 5e 28 54 68 65 20 5b 53 51 4c ]].** ^(The [SQL
d8e0: 49 54 45 5f 46 43 4e 54 4c 5f 53 59 4e 43 5f 4f ITE_FCNTL_SYNC_O
d8f0: 4d 49 54 54 45 44 5d 20 6f 70 63 6f 64 65 20 69 MITTED] opcode i
d900: 73 20 67 65 6e 65 72 61 74 65 64 20 69 6e 74 65 s generated inte
d910: 72 6e 61 6c 6c 79 20 62 79 0a 2a 2a 20 53 51 4c rnally by.** SQL
d920: 69 74 65 20 61 6e 64 20 73 65 6e 74 20 74 6f 20 ite and sent to
d930: 61 6c 6c 20 56 46 53 65 73 20 69 6e 20 70 6c 61 all VFSes in pla
d940: 63 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 ce of a call to
d950: 74 68 65 20 78 53 79 6e 63 20 6d 65 74 68 6f 64 the xSync method
d960: 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 64 61 74 .** when the dat
d970: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
d980: 20 68 61 73 20 5b 50 52 41 47 4d 41 20 73 79 6e has [PRAGMA syn
d990: 63 68 72 6f 6e 6f 75 73 5d 20 73 65 74 20 74 6f chronous] set to
d9a0: 20 4f 46 46 2e 29 5e 0a 2a 2a 20 53 6f 6d 65 20 OFF.)^.** Some
d9b0: 73 70 65 63 69 61 6c 69 7a 65 64 20 56 46 53 65 specialized VFSe
d9c0: 73 20 6e 65 65 64 20 74 68 69 73 20 73 69 67 6e s need this sign
d9d0: 61 6c 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6f al in order to o
d9e0: 70 65 72 61 74 65 20 63 6f 72 72 65 63 74 6c 79 perate correctly
d9f0: 0a 2a 2a 20 77 68 65 6e 20 5b 50 52 41 47 4d 41 .** when [PRAGMA
da00: 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 7c 20 50 synchronous | P
da10: 52 41 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f 75 RAGMA synchronou
da20: 73 3d 4f 46 46 5d 20 69 73 20 73 65 74 2c 20 62 s=OFF] is set, b
da30: 75 74 20 6d 6f 73 74 20 0a 2a 2a 20 56 46 53 65 ut most .** VFSe
da40: 73 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 68 s do not need th
da50: 69 73 20 73 69 67 6e 61 6c 20 61 6e 64 20 73 68 is signal and sh
da60: 6f 75 6c 64 20 73 69 6c 65 6e 74 6c 79 20 69 67 ould silently ig
da70: 6e 6f 72 65 20 74 68 69 73 20 6f 70 63 6f 64 65 nore this opcode
da80: 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e ..** Application
da90: 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 63 61 6c s should not cal
daa0: 6c 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f l [sqlite3_file_
dab0: 63 6f 6e 74 72 6f 6c 28 29 5d 20 77 69 74 68 20 control()] with
dac0: 74 68 69 73 0a 2a 2a 20 6f 70 63 6f 64 65 20 61 this.** opcode a
dad0: 73 20 64 6f 69 6e 67 20 73 6f 20 6d 61 79 20 64 s doing so may d
dae0: 69 73 72 75 70 74 20 74 68 65 20 6f 70 65 72 61 isrupt the opera
daf0: 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 70 65 63 tion of the spec
db00: 69 61 6c 69 7a 65 64 20 56 46 53 65 73 0a 2a 2a ialized VFSes.**
db10: 20 74 68 61 74 20 64 6f 20 72 65 71 75 69 72 65 that do require
db20: 20 69 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 3c 6c 69 it. .**.** <li
db30: 3e 5b 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f >[[SQLITE_FCNTL_
db40: 57 49 4e 33 32 5f 41 56 5f 52 45 54 52 59 5d 5d WIN32_AV_RETRY]]
db50: 0a 2a 2a 20 5e 54 68 65 20 5b 53 51 4c 49 54 45 .** ^The [SQLITE
db60: 5f 46 43 4e 54 4c 5f 57 49 4e 33 32 5f 41 56 5f _FCNTL_WIN32_AV_
db70: 52 45 54 52 59 5d 20 6f 70 63 6f 64 65 20 69 73 RETRY] opcode is
db80: 20 75 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 used to configu
db90: 72 65 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 re automatic.**
dba0: 72 65 74 72 79 20 63 6f 75 6e 74 73 20 61 6e 64 retry counts and
dbb0: 20 69 6e 74 65 72 76 61 6c 73 20 66 6f 72 20 63 intervals for c
dbc0: 65 72 74 61 69 6e 20 64 69 73 6b 20 49 2f 4f 20 ertain disk I/O
dbd0: 6f 70 65 72 61 74 69 6f 6e 73 20 66 6f 72 20 74 operations for t
dbe0: 68 65 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 5b 56 he.** windows [V
dbf0: 46 53 5d 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 FS] in order to
dc00: 70 72 6f 76 69 64 65 20 72 6f 62 75 73 74 6e 65 provide robustne
dc10: 73 73 20 69 6e 20 74 68 65 20 70 72 65 73 65 6e ss in the presen
dc20: 63 65 20 6f 66 0a 2a 2a 20 61 6e 74 69 2d 76 69 ce of.** anti-vi
dc30: 72 75 73 20 70 72 6f 67 72 61 6d 73 2e 20 20 42 rus programs. B
dc40: 79 20 64 65 66 61 75 6c 74 2c 20 74 68 65 20 77 y default, the w
dc50: 69 6e 64 6f 77 73 20 56 46 53 20 77 69 6c 6c 20 indows VFS will
dc60: 72 65 74 72 79 20 66 69 6c 65 20 72 65 61 64 2c retry file read,
dc70: 0a 2a 2a 20 66 69 6c 65 20 77 72 69 74 65 2c 20 .** file write,
dc80: 61 6e 64 20 66 69 6c 65 20 64 65 6c 65 74 65 20 and file delete
dc90: 6f 70 65 72 61 74 69 6f 6e 73 20 75 70 20 74 6f operations up to
dca0: 20 31 30 20 74 69 6d 65 73 2c 20 77 69 74 68 20 10 times, with
dcb0: 61 20 64 65 6c 61 79 0a 2a 2a 20 6f 66 20 32 35 a delay.** of 25
dcc0: 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 62 65 milliseconds be
dcd0: 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 72 fore the first r
dce0: 65 74 72 79 20 61 6e 64 20 77 69 74 68 20 74 68 etry and with th
dcf0: 65 20 64 65 6c 61 79 20 69 6e 63 72 65 61 73 69 e delay increasi
dd00: 6e 67 0a 2a 2a 20 62 79 20 61 6e 20 61 64 64 69 ng.** by an addi
dd10: 74 69 6f 6e 61 6c 20 32 35 20 6d 69 6c 6c 69 73 tional 25 millis
dd20: 65 63 6f 6e 64 73 20 77 69 74 68 20 65 61 63 68 econds with each
dd30: 20 73 75 62 73 65 71 75 65 6e 74 20 72 65 74 72 subsequent retr
dd40: 79 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 6f y. This.** opco
dd50: 64 65 20 61 6c 6c 6f 77 73 20 74 68 65 73 65 20 de allows these
dd60: 74 77 6f 20 76 61 6c 75 65 73 20 28 31 30 20 72 two values (10 r
dd70: 65 74 72 69 65 73 20 61 6e 64 20 32 35 20 6d 69 etries and 25 mi
dd80: 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 64 65 lliseconds of de
dd90: 6c 61 79 29 0a 2a 2a 20 74 6f 20 62 65 20 61 64 lay).** to be ad
dda0: 6a 75 73 74 65 64 2e 20 20 54 68 65 20 76 61 6c justed. The val
ddb0: 75 65 73 20 61 72 65 20 63 68 61 6e 67 65 64 20 ues are changed
ddc0: 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 for all database
ddd0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a 2a 20 connections.**
dde0: 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 within the same
ddf0: 70 72 6f 63 65 73 73 2e 20 20 54 68 65 20 61 72 process. The ar
de00: 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e gument is a poin
de10: 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 ter to an array
de20: 6f 66 20 74 77 6f 0a 2a 2a 20 69 6e 74 65 67 65 of two.** intege
de30: 72 73 20 77 68 65 72 65 20 74 68 65 20 66 69 72 rs where the fir
de40: 73 74 20 69 6e 74 65 67 65 72 20 69 20 74 68 65 st integer i the
de50: 20 6e 65 77 20 72 65 74 72 79 20 63 6f 75 6e 74 new retry count
de60: 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 0a and the second.
de70: 2a 2a 20 69 6e 74 65 67 65 72 20 69 73 20 74 68 ** integer is th
de80: 65 20 64 65 6c 61 79 2e 20 20 49 66 20 65 69 74 e delay. If eit
de90: 68 65 72 20 69 6e 74 65 67 65 72 20 69 73 20 6e her integer is n
dea0: 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68 egative, then th
deb0: 65 20 73 65 74 74 69 6e 67 0a 2a 2a 20 69 73 20 e setting.** is
dec0: 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 75 74 20 not changed but
ded0: 69 6e 73 74 65 61 64 20 74 68 65 20 70 72 69 6f instead the prio
dee0: 72 20 76 61 6c 75 65 20 6f 66 20 74 68 61 74 20 r value of that
def0: 73 65 74 74 69 6e 67 20 69 73 20 77 72 69 74 74 setting is writt
df00: 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 61 en.** into the a
df10: 72 72 61 79 20 65 6e 74 72 79 2c 20 61 6c 6c 6f rray entry, allo
df20: 77 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 wing the current
df30: 20 72 65 74 72 79 20 73 65 74 74 69 6e 67 73 20 retry settings
df40: 74 6f 20 62 65 0a 2a 2a 20 69 6e 74 65 72 72 6f to be.** interro
df50: 67 61 74 65 64 2e 20 20 54 68 65 20 7a 44 62 4e gated. The zDbN
df60: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 ame parameter is
df70: 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 ignored..**.**
df80: 3c 6c 69 3e 5b 5b 53 51 4c 49 54 45 5f 46 43 4e <li>[[SQLITE_FCN
df90: 54 4c 5f 50 45 52 53 49 53 54 5f 57 41 4c 5d 5d TL_PERSIST_WAL]]
dfa0: 0a 2a 2a 20 5e 54 68 65 20 5b 53 51 4c 49 54 45 .** ^The [SQLITE
dfb0: 5f 46 43 4e 54 4c 5f 50 45 52 53 49 53 54 5f 57 _FCNTL_PERSIST_W
dfc0: 41 4c 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73 AL] opcode is us
dfd0: 65 64 20 74 6f 20 73 65 74 20 6f 72 20 71 75 65 ed to set or que
dfe0: 72 79 20 74 68 65 0a 2a 2a 20 70 65 72 73 69 73 ry the.** persis
dff0: 74 65 6e 74 20 5b 57 41 4c 20 7c 20 57 72 69 74 tent [WAL | Writ
e000: 65 20 41 68 65 61 64 20 4c 6f 67 5d 20 73 65 74 e Ahead Log] set
e010: 74 69 6e 67 2e 20 20 42 79 20 64 65 66 61 75 6c ting. By defaul
e020: 74 2c 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 t, the auxiliary
e030: 0a 2a 2a 20 77 72 69 74 65 20 61 68 65 61 64 20 .** write ahead
e040: 6c 6f 67 20 61 6e 64 20 73 68 61 72 65 64 20 6d log and shared m
e050: 65 6d 6f 72 79 20 66 69 6c 65 73 20 75 73 65 64 emory files used
e060: 20 66 6f 72 20 74 72 61 6e 73 61 63 74 69 6f 6e for transaction
e070: 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 61 72 65 20 control.** are
e080: 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 automatically de
e090: 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 6c leted when the l
e0a0: 61 74 65 73 74 20 63 6f 6e 6e 65 63 74 69 6f 6e atest connection
e0b0: 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
e0c0: 0a 2a 2a 20 63 6c 6f 73 65 73 2e 20 20 53 65 74 .** closes. Set
e0d0: 74 69 6e 67 20 70 65 72 73 69 73 74 65 6e 74 20 ting persistent
e0e0: 57 41 4c 20 6d 6f 64 65 20 63 61 75 73 65 73 20 WAL mode causes
e0f0: 74 68 6f 73 65 20 66 69 6c 65 73 20 74 6f 20 70 those files to p
e100: 65 72 73 69 73 74 20 61 66 74 65 72 0a 2a 2a 20 ersist after.**
e110: 63 6c 6f 73 65 2e 20 20 50 65 72 73 69 73 74 69 close. Persisti
e120: 6e 67 20 74 68 65 20 66 69 6c 65 73 20 69 73 20 ng the files is
e130: 75 73 65 66 75 6c 20 77 68 65 6e 20 6f 74 68 65 useful when othe
e140: 72 20 70 72 6f 63 65 73 73 65 73 20 74 68 61 74 r processes that
e150: 20 64 6f 20 6e 6f 74 0a 2a 2a 20 68 61 76 65 20 do not.** have
e160: 77 72 69 74 65 20 70 65 72 6d 69 73 73 69 6f 6e write permission
e170: 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72 on the director
e180: 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 y containing the
e190: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
e1a0: 61 6e 74 0a 2a 2a 20 74 6f 20 72 65 61 64 20 74 ant.** to read t
e1b0: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
e1c0: 2c 20 61 73 20 74 68 65 20 57 41 4c 20 61 6e 64 , as the WAL and
e1d0: 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 66 shared memory f
e1e0: 69 6c 65 73 20 6d 75 73 74 20 65 78 69 73 74 0a iles must exist.
e1f0: 2a 2a 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 ** in order for
e200: 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 the database to
e210: 62 65 20 72 65 61 64 61 62 6c 65 2e 20 20 54 68 be readable. Th
e220: 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 e fourth paramet
e230: 65 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 er to.** [sqlite
e240: 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 3_file_control()
e250: 5d 20 66 6f 72 20 74 68 69 73 20 6f 70 63 6f 64 ] for this opcod
e260: 65 20 73 68 6f 75 6c 64 20 62 65 20 61 20 70 6f e should be a po
e270: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 74 65 inter to an inte
e280: 67 65 72 2e 0a 2a 2a 20 54 68 61 74 20 69 6e 74 ger..** That int
e290: 65 67 65 72 20 69 73 20 30 20 74 6f 20 64 69 73 eger is 0 to dis
e2a0: 61 62 6c 65 20 70 65 72 73 69 73 74 65 6e 74 20 able persistent
e2b0: 57 41 4c 20 6d 6f 64 65 20 6f 72 20 31 20 74 6f WAL mode or 1 to
e2c0: 20 65 6e 61 62 6c 65 20 70 65 72 73 69 73 74 65 enable persiste
e2d0: 6e 74 0a 2a 2a 20 57 41 4c 20 6d 6f 64 65 2e 20 nt.** WAL mode.
e2e0: 20 49 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 If the integer
e2f0: 69 73 20 2d 31 2c 20 74 68 65 6e 20 69 74 20 69 is -1, then it i
e300: 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 s overwritten wi
e310: 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a th the current.*
e320: 2a 20 57 41 4c 20 70 65 72 73 69 73 74 65 6e 63 * WAL persistenc
e330: 65 20 73 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a e setting..**.**
e340: 20 3c 6c 69 3e 5b 5b 53 51 4c 49 54 45 5f 46 43 <li>[[SQLITE_FC
e350: 4e 54 4c 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 NTL_POWERSAFE_OV
e360: 45 52 57 52 49 54 45 5d 5d 0a 2a 2a 20 5e 54 68 ERWRITE]].** ^Th
e370: 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f e [SQLITE_FCNTL_
e380: 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 52 POWERSAFE_OVERWR
e390: 49 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 ITE] opcode is u
e3a0: 73 65 64 20 74 6f 20 73 65 74 20 6f 72 20 71 75 sed to set or qu
e3b0: 65 72 79 20 74 68 65 0a 2a 2a 20 70 65 72 73 69 ery the.** persi
e3c0: 73 74 65 6e 74 20 22 70 6f 77 65 72 73 61 66 65 stent "powersafe
e3d0: 2d 6f 76 65 72 77 72 69 74 65 22 20 6f 72 20 22 -overwrite" or "
e3e0: 50 53 4f 57 22 20 73 65 74 74 69 6e 67 2e 20 20 PSOW" setting.
e3f0: 54 68 65 20 50 53 4f 57 20 73 65 74 74 69 6e 67 The PSOW setting
e400: 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 73 20 74 .** determines t
e410: 68 65 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 he [SQLITE_IOCAP
e420: 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 _POWERSAFE_OVERW
e430: 52 49 54 45 5d 20 62 69 74 20 6f 66 20 74 68 65 RITE] bit of the
e440: 0a 2a 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 .** xDeviceChara
e450: 63 74 65 72 69 73 74 69 63 73 20 6d 65 74 68 6f cteristics metho
e460: 64 73 2e 20 54 68 65 20 66 6f 75 72 74 68 20 70 ds. The fourth p
e470: 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b arameter to.** [
e480: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e sqlite3_file_con
e490: 74 72 6f 6c 28 29 5d 20 66 6f 72 20 74 68 69 73 trol()] for this
e4a0: 20 6f 70 63 6f 64 65 20 73 68 6f 75 6c 64 20 62 opcode should b
e4b0: 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 e a pointer to a
e4c0: 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 54 68 n integer..** Th
e4d0: 61 74 20 69 6e 74 65 67 65 72 20 69 73 20 30 20 at integer is 0
e4e0: 74 6f 20 64 69 73 61 62 6c 65 20 7a 65 72 6f 2d to disable zero-
e4f0: 64 61 6d 61 67 65 20 6d 6f 64 65 20 6f 72 20 31 damage mode or 1
e500: 20 74 6f 20 65 6e 61 62 6c 65 20 7a 65 72 6f 2d to enable zero-
e510: 64 61 6d 61 67 65 0a 2a 2a 20 6d 6f 64 65 2e 20 damage.** mode.
e520: 20 49 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 If the integer
e530: 69 73 20 2d 31 2c 20 74 68 65 6e 20 69 74 20 69 is -1, then it i
e540: 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 s overwritten wi
e550: 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a th the current.*
e560: 2a 20 7a 65 72 6f 2d 64 61 6d 61 67 65 20 6d 6f * zero-damage mo
e570: 64 65 20 73 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a de setting..**.*
e580: 2a 20 3c 6c 69 3e 5b 5b 53 51 4c 49 54 45 5f 46 * <li>[[SQLITE_F
e590: 43 4e 54 4c 5f 4f 56 45 52 57 52 49 54 45 5d 5d CNTL_OVERWRITE]]
e5a0: 0a 2a 2a 20 5e 54 68 65 20 5b 53 51 4c 49 54 45 .** ^The [SQLITE
e5b0: 5f 46 43 4e 54 4c 5f 4f 56 45 52 57 52 49 54 45 _FCNTL_OVERWRITE
e5c0: 5d 20 6f 70 63 6f 64 65 20 69 73 20 69 6e 76 6f ] opcode is invo
e5d0: 6b 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 66 ked by SQLite af
e5e0: 74 65 72 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20 61 ter opening.** a
e5f0: 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
e600: 6f 6e 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 on to indicate t
e610: 68 61 74 2c 20 75 6e 6c 65 73 73 20 69 74 20 69 hat, unless it i
e620: 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 66 6f s rolled back fo
e630: 72 20 73 6f 6d 65 0a 2a 2a 20 72 65 61 73 6f 6e r some.** reason
e640: 2c 20 74 68 65 20 65 6e 74 69 72 65 20 64 61 74 , the entire dat
e650: 61 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 abase file will
e660: 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 62 be overwritten b
e670: 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 0a 2a y the current .*
e680: 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 * transaction. T
e690: 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 56 his is used by V
e6a0: 41 43 55 55 4d 20 6f 70 65 72 61 74 69 6f 6e 73 ACUUM operations
e6b0: 2e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 5b 5b 53 51 ..**.** <li>[[SQ
e6c0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 56 46 53 4e 41 LITE_FCNTL_VFSNA
e6d0: 4d 45 5d 5d 0a 2a 2a 20 5e 54 68 65 20 5b 53 51 ME]].** ^The [SQ
e6e0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 56 46 53 4e 41 LITE_FCNTL_VFSNA
e6f0: 4d 45 5d 20 6f 70 63 6f 64 65 20 63 61 6e 20 62 ME] opcode can b
e700: 65 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e e used to obtain
e710: 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a the names of.**
e720: 20 61 6c 6c 20 5b 56 46 53 65 73 5d 20 69 6e 20 all [VFSes] in
e730: 74 68 65 20 56 46 53 20 73 74 61 63 6b 2e 20 20 the VFS stack.
e740: 54 68 65 20 6e 61 6d 65 73 20 61 72 65 20 6f 66 The names are of
e750: 20 61 6c 6c 20 56 46 53 20 73 68 69 6d 73 20 61 all VFS shims a
e760: 6e 64 20 74 68 65 0a 2a 2a 20 66 69 6e 61 6c 20 nd the.** final
e770: 62 6f 74 74 6f 6d 2d 6c 65 76 65 6c 20 56 46 53 bottom-level VFS
e780: 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e 74 are written int
e790: 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 o memory obtaine
e7a0: 64 20 66 72 6f 6d 20 0a 2a 2a 20 5b 73 71 6c 69 d from .** [sqli
e7b0: 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 61 6e te3_malloc()] an
e7c0: 64 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 d the result is
e7d0: 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 68 stored in the ch
e7e0: 61 72 2a 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 ar* variable.**
e7f0: 74 68 61 74 20 74 68 65 20 66 6f 75 72 74 68 20 that the fourth
e800: 70 61 72 61 6d 65 74 65 72 20 6f 66 20 5b 73 71 parameter of [sq
e810: 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 lite3_file_contr
e820: 6f 6c 28 29 5d 20 70 6f 69 6e 74 73 20 74 6f 2e ol()] points to.
e830: 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69 .** The caller i
e840: 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f s responsible fo
e850: 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 6d 65 r freeing the me
e860: 6d 6f 72 79 20 77 68 65 6e 20 64 6f 6e 65 2e 20 mory when done.
e870: 20 41 73 20 77 69 74 68 0a 2a 2a 20 61 6c 6c 20 As with.** all
e880: 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 61 63 74 file-control act
e890: 69 6f 6e 73 2c 20 74 68 65 72 65 20 69 73 20 6e ions, there is n
e8a0: 6f 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 o guarantee that
e8b0: 20 74 68 69 73 20 77 69 6c 6c 20 61 63 74 75 61 this will actua
e8c0: 6c 6c 79 0a 2a 2a 20 64 6f 20 61 6e 79 74 68 69 lly.** do anythi
e8d0: 6e 67 2e 20 20 43 61 6c 6c 65 72 73 20 73 68 6f ng. Callers sho
e8e0: 75 6c 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 uld initialize t
e8f0: 68 65 20 63 68 61 72 2a 20 76 61 72 69 61 62 6c he char* variabl
e900: 65 20 74 6f 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 e to a NULL.** p
e910: 6f 69 6e 74 65 72 20 69 6e 20 63 61 73 65 20 74 ointer in case t
e920: 68 69 73 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c his file-control
e930: 20 69 73 20 6e 6f 74 20 69 6d 70 6c 65 6d 65 6e is not implemen
e940: 74 65 64 2e 20 20 54 68 69 73 20 66 69 6c 65 2d ted. This file-
e950: 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 69 73 20 69 6e control.** is in
e960: 74 65 6e 64 65 64 20 66 6f 72 20 64 69 61 67 6e tended for diagn
e970: 6f 73 74 69 63 20 75 73 65 20 6f 6e 6c 79 2e 0a ostic use only..
e980: 2a 2a 0a 2a 2a 20 3c 6c 69 3e 5b 5b 53 51 4c 49 **.** <li>[[SQLI
e990: 54 45 5f 46 43 4e 54 4c 5f 50 52 41 47 4d 41 5d TE_FCNTL_PRAGMA]
e9a0: 5d 0a 2a 2a 20 5e 57 68 65 6e 65 76 65 72 20 61 ].** ^Whenever a
e9b0: 20 5b 50 52 41 47 4d 41 5d 20 73 74 61 74 65 6d [PRAGMA] statem
e9c0: 65 6e 74 20 69 73 20 70 61 72 73 65 64 2c 20 61 ent is parsed, a
e9d0: 6e 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f n [SQLITE_FCNTL_
e9e0: 50 52 41 47 4d 41 5d 20 0a 2a 2a 20 66 69 6c 65 PRAGMA] .** file
e9f0: 20 63 6f 6e 74 72 6f 6c 20 69 73 20 73 65 6e 74 control is sent
ea00: 20 74 6f 20 74 68 65 20 6f 70 65 6e 20 5b 73 71 to the open [sq
ea10: 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 lite3_file] obje
ea20: 63 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ct corresponding
ea30: 0a 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62 .** to the datab
ea40: 61 73 65 20 66 69 6c 65 20 74 6f 20 77 68 69 63 ase file to whic
ea50: 68 20 74 68 65 20 70 72 61 67 6d 61 20 73 74 61 h the pragma sta
ea60: 74 65 6d 65 6e 74 20 72 65 66 65 72 73 2e 20 5e tement refers. ^
ea70: 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 The argument.**
ea80: 74 6f 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 46 to the [SQLITE_F
ea90: 43 4e 54 4c 5f 50 52 41 47 4d 41 5d 20 66 69 6c CNTL_PRAGMA] fil
eaa0: 65 20 63 6f 6e 74 72 6f 6c 20 69 73 20 61 6e 20 e control is an
eab0: 61 72 72 61 79 20 6f 66 0a 2a 2a 20 70 6f 69 6e array of.** poin
eac0: 74 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20 ters to strings
ead0: 28 63 68 61 72 2a 2a 29 20 69 6e 20 77 68 69 63 (char**) in whic
eae0: 68 20 74 68 65 20 73 65 63 6f 6e 64 20 65 6c 65 h the second ele
eaf0: 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61 ment of the arra
eb00: 79 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d 65 y.** is the name
eb10: 20 6f 66 20 74 68 65 20 70 72 61 67 6d 61 20 61 of the pragma a
eb20: 6e 64 20 74 68 65 20 74 68 69 72 64 20 65 6c 65 nd the third ele
eb30: 6d 65 6e 74 20 69 73 20 74 68 65 20 61 72 67 75 ment is the argu
eb40: 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 70 ment to the.** p
eb50: 72 61 67 6d 61 20 6f 72 20 4e 55 4c 4c 20 69 66 ragma or NULL if
eb60: 20 74 68 65 20 70 72 61 67 6d 61 20 68 61 73 20 the pragma has
eb70: 6e 6f 20 61 72 67 75 6d 65 6e 74 2e 20 20 5e 54 no argument. ^T
eb80: 68 65 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 he handler for a
eb90: 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 46 43 4e n.** [SQLITE_FCN
eba0: 54 4c 5f 50 52 41 47 4d 41 5d 20 66 69 6c 65 20 TL_PRAGMA] file
ebb0: 63 6f 6e 74 72 6f 6c 20 63 61 6e 20 6f 70 74 69 control can opti
ebc0: 6f 6e 61 6c 6c 79 20 6d 61 6b 65 20 74 68 65 20 onally make the
ebd0: 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 0a 2a 2a first element.**
ebe0: 20 6f 66 20 74 68 65 20 63 68 61 72 2a 2a 20 61 of the char** a
ebf0: 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 20 74 6f rgument point to
ec00: 20 61 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e a string obtain
ec10: 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 ed from [sqlite3
ec20: 5f 6d 70 72 69 6e 74 66 28 29 5d 0a 2a 2a 20 6f _mprintf()].** o
ec30: 72 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 r the equivalent
ec40: 20 61 6e 64 20 74 68 61 74 20 73 74 72 69 6e 67 and that string
ec50: 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 will become the
ec60: 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 70 result of the p
ec70: 72 61 67 6d 61 20 6f 72 0a 2a 2a 20 74 68 65 20 ragma or.** the
ec80: 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 66 error message if
ec90: 20 74 68 65 20 70 72 61 67 6d 61 20 66 61 69 6c the pragma fail
eca0: 73 2e 20 5e 49 66 20 74 68 65 0a 2a 2a 20 5b 53 s. ^If the.** [S
ecb0: 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 52 41 47 QLITE_FCNTL_PRAG
ecc0: 4d 41 5d 20 66 69 6c 65 20 63 6f 6e 74 72 6f 6c MA] file control
ecd0: 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
ece0: 5f 4e 4f 54 46 4f 55 4e 44 5d 2c 20 74 68 65 6e _NOTFOUND], then
ecf0: 20 6e 6f 72 6d 61 6c 20 0a 2a 2a 20 5b 50 52 41 normal .** [PRA
ed00: 47 4d 41 5d 20 70 72 6f 63 65 73 73 69 6e 67 20 GMA] processing
ed10: 63 6f 6e 74 69 6e 75 65 73 2e 20 20 5e 49 66 20 continues. ^If
ed20: 74 68 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 the [SQLITE_FCNT
ed30: 4c 5f 50 52 41 47 4d 41 5d 0a 2a 2a 20 66 69 6c L_PRAGMA].** fil
ed40: 65 20 63 6f 6e 74 72 6f 6c 20 72 65 74 75 72 6e e control return
ed50: 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74 s [SQLITE_OK], t
ed60: 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 61 hen the parser a
ed70: 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 0a ssumes that the.
ed80: 2a 2a 20 56 46 53 20 68 61 73 20 68 61 6e 64 6c ** VFS has handl
ed90: 65 64 20 74 68 65 20 50 52 41 47 4d 41 20 69 74 ed the PRAGMA it
eda0: 73 65 6c 66 20 61 6e 64 20 74 68 65 20 70 61 72 self and the par
edb0: 73 65 72 20 67 65 6e 65 72 61 74 65 73 20 61 20 ser generates a
edc0: 6e 6f 2d 6f 70 0a 2a 2a 20 70 72 65 70 61 72 65 no-op.** prepare
edd0: 64 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 5e 49 d statement. ^I
ede0: 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 46 43 f the [SQLITE_FC
edf0: 4e 54 4c 5f 50 52 41 47 4d 41 5d 20 66 69 6c 65 NTL_PRAGMA] file
ee00: 20 63 6f 6e 74 72 6f 6c 20 72 65 74 75 72 6e 73 control returns
ee10: 0a 2a 2a 20 61 6e 79 20 72 65 73 75 6c 74 20 63 .** any result c
ee20: 6f 64 65 20 6f 74 68 65 72 20 74 68 61 6e 20 5b ode other than [
ee30: 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 72 20 5b 53 SQLITE_OK] or [S
ee40: 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 5d 2c QLITE_NOTFOUND],
ee50: 20 74 68 61 74 20 6d 65 61 6e 73 0a 2a 2a 20 74 that means.** t
ee60: 68 61 74 20 74 68 65 20 56 46 53 20 65 6e 63 6f hat the VFS enco
ee70: 75 6e 74 65 72 65 64 20 61 6e 20 65 72 72 6f 72 untered an error
ee80: 20 77 68 69 6c 65 20 68 61 6e 64 6c 69 6e 67 20 while handling
ee90: 74 68 65 20 5b 50 52 41 47 4d 41 5d 20 61 6e 64 the [PRAGMA] and
eea0: 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 61 74 the.** compilat
eeb0: 69 6f 6e 20 6f 66 20 74 68 65 20 50 52 41 47 4d ion of the PRAGM
eec0: 41 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20 A fails with an
eed0: 65 72 72 6f 72 2e 20 20 5e 54 68 65 20 5b 53 51 error. ^The [SQ
eee0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 52 41 47 4d LITE_FCNTL_PRAGM
eef0: 41 5d 0a 2a 2a 20 66 69 6c 65 20 63 6f 6e 74 72 A].** file contr
ef00: 6f 6c 20 6f 63 63 75 72 73 20 61 74 20 74 68 65 ol occurs at the
ef10: 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 70 72 beginning of pr
ef20: 61 67 6d 61 20 73 74 61 74 65 6d 65 6e 74 20 61 agma statement a
ef30: 6e 61 6c 79 73 69 73 20 61 6e 64 20 73 6f 0a 2a nalysis and so.*
ef40: 2a 20 69 74 20 69 73 20 61 62 6c 65 20 74 6f 20 * it is able to
ef50: 6f 76 65 72 72 69 64 65 20 62 75 69 6c 74 2d 69 override built-i
ef60: 6e 20 5b 50 52 41 47 4d 41 5d 20 73 74 61 74 65 n [PRAGMA] state
ef70: 6d 65 6e 74 73 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a ments..** </ul>.
ef80: 2a 2a 0a 2a 2a 20 3c 6c 69 3e 5b 5b 53 51 4c 49 **.** <li>[[SQLI
ef90: 54 45 5f 46 43 4e 54 4c 5f 42 55 53 59 48 41 4e TE_FCNTL_BUSYHAN
efa0: 44 4c 45 52 5d 5d 0a 2a 2a 20 5e 54 68 69 73 20 DLER]].** ^This
efb0: 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 6d 61 79 file-control may
efc0: 20 62 65 20 69 6e 76 6f 6b 65 64 20 62 79 20 53 be invoked by S
efd0: 51 4c 69 74 65 20 6f 6e 20 74 68 65 20 64 61 74 QLite on the dat
efe0: 61 62 61 73 65 20 66 69 6c 65 20 68 61 6e 64 6c abase file handl
eff0: 65 0a 2a 2a 20 73 68 6f 72 74 6c 79 20 61 66 74 e.** shortly aft
f000: 65 72 20 69 74 20 69 73 20 6f 70 65 6e 65 64 20 er it is opened
f010: 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 6f 76 in order to prov
f020: 69 64 65 20 61 20 63 75 73 74 6f 6d 20 56 46 53 ide a custom VFS
f030: 20 77 69 74 68 20 61 63 63 65 73 73 0a 2a 2a 20 with access.**
f040: 74 6f 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f to the connectio
f050: 6e 73 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 ns busy-handler
f060: 63 61 6c 6c 62 61 63 6b 2e 20 54 68 65 20 61 72 callback. The ar
f070: 67 75 6d 65 6e 74 20 69 73 20 6f 66 20 74 79 70 gument is of typ
f080: 65 20 28 76 6f 69 64 20 2a 2a 29 0a 2a 2a 20 2d e (void **).** -
f090: 20 61 6e 20 61 72 72 61 79 20 6f 66 20 74 77 6f an array of two
f0a0: 20 28 76 6f 69 64 20 2a 29 20 76 61 6c 75 65 73 (void *) values
f0b0: 2e 20 54 68 65 20 66 69 72 73 74 20 28 76 6f 69 . The first (voi
f0c0: 64 20 2a 29 20 61 63 74 75 61 6c 6c 79 20 70 6f d *) actually po
f0d0: 69 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 66 75 6e ints.** to a fun
f0e0: 63 74 69 6f 6e 20 6f 66 20 74 79 70 65 20 28 69 ction of type (i
f0f0: 6e 74 20 28 2a 29 28 76 6f 69 64 20 2a 29 29 2e nt (*)(void *)).
f100: 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 76 In order to inv
f110: 6f 6b 65 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 oke the connecti
f120: 6f 6e 73 0a 2a 2a 20 62 75 73 79 2d 68 61 6e 64 ons.** busy-hand
f130: 6c 65 72 2c 20 74 68 69 73 20 66 75 6e 63 74 69 ler, this functi
f140: 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 76 on should be inv
f150: 6f 6b 65 64 20 77 69 74 68 20 74 68 65 20 73 65 oked with the se
f160: 63 6f 6e 64 20 28 76 6f 69 64 20 2a 29 20 69 6e cond (void *) in
f170: 0a 2a 2a 20 74 68 65 20 61 72 72 61 79 20 61 73 .** the array as
f180: 20 74 68 65 20 6f 6e 6c 79 20 61 72 67 75 6d 65 the only argume
f190: 6e 74 2e 20 49 66 20 69 74 20 72 65 74 75 72 6e nt. If it return
f1a0: 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
f1b0: 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a the operation.*
f1c0: 2a 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74 72 * should be retr
f1d0: 69 65 64 2e 20 49 66 20 69 74 20 72 65 74 75 72 ied. If it retur
f1e0: 6e 73 20 7a 65 72 6f 2c 20 74 68 65 20 63 75 73 ns zero, the cus
f1f0: 74 6f 6d 20 56 46 53 20 73 68 6f 75 6c 64 20 61 tom VFS should a
f200: 62 61 6e 64 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 bandon the.** cu
f210: 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 2e rrent operation.
f220: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
f230: 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 TE_FCNTL_LOCKSTA
f240: 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TE
f250: 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 1.#define SQLIT
f260: 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 E_GET_LOCKPROXYF
f270: 49 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ILE
f280: 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
f290: 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _SET_LOCKPROXYFI
f2a0: 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 33 LE 3
f2b0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
f2c0: 4c 41 53 54 5f 45 52 52 4e 4f 20 20 20 20 20 20 LAST_ERRNO
f2d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 4.
f2e0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 #define SQLITE_F
f2f0: 43 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e 54 20 20 CNTL_SIZE_HINT
f300: 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 5.#
f310: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 define SQLITE_FC
f320: 4e 54 4c 5f 43 48 55 4e 4b 5f 53 49 5a 45 20 20 NTL_CHUNK_SIZE
f330: 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 6.#d
f340: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e efine SQLITE_FCN
f350: 54 4c 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 TL_FILE_POINTER
f360: 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 7.#de
f370: 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 fine SQLITE_FCNT
f380: 4c 5f 53 59 4e 43 5f 4f 4d 49 54 54 45 44 20 20 L_SYNC_OMITTED
f390: 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 8.#def
f3a0: 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ine SQLITE_FCNTL
f3b0: 5f 57 49 4e 33 32 5f 41 56 5f 52 45 54 52 59 20 _WIN32_AV_RETRY
f3c0: 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 9.#defi
f3d0: 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f ne SQLITE_FCNTL_
f3e0: 50 45 52 53 49 53 54 5f 57 41 4c 20 20 20 20 20 PERSIST_WAL
f3f0: 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 10.#defin
f400: 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4f e SQLITE_FCNTL_O
f410: 56 45 52 57 52 49 54 45 20 20 20 20 20 20 20 20 VERWRITE
f420: 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 11.#define
f430: 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 56 46 SQLITE_FCNTL_VF
f440: 53 4e 41 4d 45 20 20 20 20 20 20 20 20 20 20 20 SNAME
f450: 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 12.#define
f460: 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 4f 57 SQLITE_FCNTL_POW
f470: 45 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 ERSAFE_OVERWRITE
f480: 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 53 13.#define S
f490: 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 52 41 47 QLITE_FCNTL_PRAG
f4a0: 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 MA
f4b0: 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 53 51 14.#define SQ
f4c0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 42 55 53 59 48 LITE_FCNTL_BUSYH
f4d0: 41 4e 44 4c 45 52 20 20 20 20 20 20 20 20 20 20 ANDLER
f4e0: 20 20 31 35 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 15../*.** CAPI
f4f0: 33 52 45 46 3a 20 4d 75 74 65 78 20 48 61 6e 64 3REF: Mutex Hand
f500: 6c 65 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 le.**.** The mut
f510: 65 78 20 6d 6f 64 75 6c 65 20 77 69 74 68 69 6e ex module within
f520: 20 53 51 4c 69 74 65 20 64 65 66 69 6e 65 73 20 SQLite defines
f530: 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 [sqlite3_mutex]
f540: 74 6f 20 62 65 20 61 6e 0a 2a 2a 20 61 62 73 74 to be an.** abst
f550: 72 61 63 74 20 74 79 70 65 20 66 6f 72 20 61 20 ract type for a
f560: 6d 75 74 65 78 20 6f 62 6a 65 63 74 2e 20 20 54 mutex object. T
f570: 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6e he SQLite core n
f580: 65 76 65 72 20 6c 6f 6f 6b 73 0a 2a 2a 20 61 74 ever looks.** at
f590: 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 the internal re
f5a0: 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
f5b0: 61 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 an [sqlite3_mute
f5c0: 78 5d 2e 20 20 49 74 20 6f 6e 6c 79 0a 2a 2a 20 x]. It only.**
f5d0: 64 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 deals with point
f5e0: 65 72 73 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 ers to the [sqli
f5f0: 74 65 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 te3_mutex] objec
f600: 74 2e 0a 2a 2a 0a 2a 2a 20 4d 75 74 65 78 65 73 t..**.** Mutexes
f610: 20 61 72 65 20 63 72 65 61 74 65 64 20 75 73 69 are created usi
f620: 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 ng [sqlite3_mute
f630: 78 5f 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 74 x_alloc()]..*/.t
f640: 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
f650: 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 71 6c 69 lite3_mutex sqli
f660: 74 65 33 5f 6d 75 74 65 78 3b 0a 0a 2f 2a 0a 2a te3_mutex;../*.*
f670: 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 * CAPI3REF: OS I
f680: 6e 74 65 72 66 61 63 65 20 4f 62 6a 65 63 74 0a nterface Object.
f690: 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 **.** An instanc
f6a0: 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 e of the sqlite3
f6b0: 5f 76 66 73 20 6f 62 6a 65 63 74 20 64 65 66 69 _vfs object defi
f6c0: 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 nes the interfac
f6d0: 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 e between.** the
f6e0: 20 53 51 4c 69 74 65 20 63 6f 72 65 20 61 6e 64 SQLite core and
f6f0: 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
f700: 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
f710: 2e 20 20 54 68 65 20 22 76 66 73 22 0a 2a 2a 20 . The "vfs".**
f720: 69 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 in the name of t
f730: 68 65 20 6f 62 6a 65 63 74 20 73 74 61 6e 64 73 he object stands
f740: 20 66 6f 72 20 22 76 69 72 74 75 61 6c 20 66 69 for "virtual fi
f750: 6c 65 20 73 79 73 74 65 6d 22 2e 20 20 53 65 65 le system". See
f760: 0a 2a 2a 20 74 68 65 20 5b 56 46 53 20 7c 20 56 .** the [VFS | V
f770: 46 53 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e FS documentation
f780: 5d 20 66 6f 72 20 66 75 72 74 68 65 72 20 69 6e ] for further in
f790: 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a formation..**.**
f7a0: 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 The value of th
f7b0: 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 e iVersion field
f7c0: 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 20 is initially 1
f7d0: 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72 67 65 but may be large
f7e0: 72 20 69 6e 0a 2a 2a 20 66 75 74 75 72 65 20 76 r in.** future v
f7f0: 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 ersions of SQLit
f800: 65 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 e. Additional f
f810: 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70 ields may be app
f820: 65 6e 64 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a ended to this.**
f830: 20 6f 62 6a 65 63 74 20 77 68 65 6e 20 74 68 65 object when the
f840: 20 69 56 65 72 73 69 6f 6e 20 76 61 6c 75 65 20 iVersion value
f850: 69 73 20 69 6e 63 72 65 61 73 65 64 2e 20 20 4e is increased. N
f860: 6f 74 65 20 74 68 61 74 20 74 68 65 20 73 74 72 ote that the str
f870: 75 63 74 75 72 65 0a 2a 2a 20 6f 66 20 74 68 65 ucture.** of the
f880: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a sqlite3_vfs obj
f890: 65 63 74 20 63 68 61 6e 67 65 73 20 69 6e 20 74 ect changes in t
f8a0: 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 he transaction b
f8b0: 65 74 77 65 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 etween.** SQLite
f8c0: 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 39 20 61 version 3.5.9 a
f8d0: 6e 64 20 33 2e 36 2e 30 20 61 6e 64 20 79 65 74 nd 3.6.0 and yet
f8e0: 20 74 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69 the iVersion fi
f8f0: 65 6c 64 20 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d eld was not.** m
f900: 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 odified..**.** T
f910: 68 65 20 73 7a 4f 73 46 69 6c 65 20 66 69 65 6c he szOsFile fiel
f920: 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 d is the size of
f930: 20 74 68 65 20 73 75 62 63 6c 61 73 73 65 64 20 the subclassed
f940: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 0a 2a [sqlite3_file].*
f950: 2a 20 73 74 72 75 63 74 75 72 65 20 75 73 65 64 * structure used
f960: 20 62 79 20 74 68 69 73 20 56 46 53 2e 20 20 6d by this VFS. m
f970: 78 50 61 74 68 6e 61 6d 65 20 69 73 20 74 68 65 xPathname is the
f980: 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 maximum length
f990: 6f 66 0a 2a 2a 20 61 20 70 61 74 68 6e 61 6d 65 of.** a pathname
f9a0: 20 69 6e 20 74 68 69 73 20 56 46 53 2e 0a 2a 2a in this VFS..**
f9b0: 0a 2a 2a 20 52 65 67 69 73 74 65 72 65 64 20 73 .** Registered s
f9c0: 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 qlite3_vfs objec
f9d0: 74 73 20 61 72 65 20 6b 65 70 74 20 6f 6e 20 61 ts are kept on a
f9e0: 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 66 6f 72 linked list for
f9f0: 6d 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 4e med by.** the pN
fa00: 65 78 74 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 ext pointer. Th
fa10: 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 e [sqlite3_vfs_r
fa20: 65 67 69 73 74 65 72 28 29 5d 0a 2a 2a 20 61 6e egister()].** an
fa30: 64 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 d [sqlite3_vfs_u
fa40: 6e 72 65 67 69 73 74 65 72 28 29 5d 20 69 6e 74 nregister()] int
fa50: 65 72 66 61 63 65 73 20 6d 61 6e 61 67 65 20 74 erfaces manage t
fa60: 68 69 73 20 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 his list.** in a
fa70: 20 74 68 72 65 61 64 2d 73 61 66 65 20 77 61 79 thread-safe way
fa80: 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f . The [sqlite3_
fa90: 76 66 73 5f 66 69 6e 64 28 29 5d 20 69 6e 74 65 vfs_find()] inte
faa0: 72 66 61 63 65 0a 2a 2a 20 73 65 61 72 63 68 65 rface.** searche
fab0: 73 20 74 68 65 20 6c 69 73 74 2e 20 20 4e 65 69 s the list. Nei
fac0: 74 68 65 72 20 74 68 65 20 61 70 70 6c 69 63 61 ther the applica
fad0: 74 69 6f 6e 20 63 6f 64 65 20 6e 6f 72 20 74 68 tion code nor th
fae0: 65 20 56 46 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65 e VFS.** impleme
faf0: 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 ntation should u
fb00: 73 65 20 74 68 65 20 70 4e 65 78 74 20 70 6f 69 se the pNext poi
fb10: 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 nter..**.** The
fb20: 70 4e 65 78 74 20 66 69 65 6c 64 20 69 73 20 74 pNext field is t
fb30: 68 65 20 6f 6e 6c 79 20 66 69 65 6c 64 20 69 6e he only field in
fb40: 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 the sqlite3_vfs
fb50: 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 .** structure th
fb60: 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 65 at SQLite will e
fb70: 76 65 72 20 6d 6f 64 69 66 79 2e 20 20 53 51 4c ver modify. SQL
fb80: 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 ite will only ac
fb90: 63 65 73 73 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 cess.** or modif
fba0: 79 20 74 68 69 73 20 66 69 65 6c 64 20 77 68 69 y this field whi
fbb0: 6c 65 20 68 6f 6c 64 69 6e 67 20 61 20 70 61 72 le holding a par
fbc0: 74 69 63 75 6c 61 72 20 73 74 61 74 69 63 20 6d ticular static m
fbd0: 75 74 65 78 2e 0a 2a 2a 20 54 68 65 20 61 70 70 utex..** The app
fbe0: 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 lication should
fbf0: 6e 65 76 65 72 20 6d 6f 64 69 66 79 20 61 6e 79 never modify any
fc00: 74 68 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65 thing within the
fc10: 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 sqlite3_vfs.**
fc20: 6f 62 6a 65 63 74 20 6f 6e 63 65 20 74 68 65 20 object once the
fc30: 6f 62 6a 65 63 74 20 68 61 73 20 62 65 65 6e 20 object has been
fc40: 72 65 67 69 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a registered..**.*
fc50: 2a 20 54 68 65 20 7a 4e 61 6d 65 20 66 69 65 6c * The zName fiel
fc60: 64 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 d holds the name
fc70: 20 6f 66 20 74 68 65 20 56 46 53 20 6d 6f 64 75 of the VFS modu
fc80: 6c 65 2e 20 20 54 68 65 20 6e 61 6d 65 20 6d 75 le. The name mu
fc90: 73 74 0a 2a 2a 20 62 65 20 75 6e 69 71 75 65 20 st.** be unique
fca0: 61 63 72 6f 73 73 20 61 6c 6c 20 56 46 53 20 6d across all VFS m
fcb0: 6f 64 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 5b 5b odules..**.** [[
fcc0: 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 sqlite3_vfs.xOpe
fcd0: 6e 5d 5d 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 67 n]].** ^SQLite g
fce0: 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 uarantees that t
fcf0: 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 70 61 72 he zFilename par
fd00: 61 6d 65 74 65 72 20 74 6f 20 78 4f 70 65 6e 0a ameter to xOpen.
fd10: 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 4e ** is either a N
fd20: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 6f 72 20 73 ULL pointer or s
fd30: 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 0a 2a tring obtained.*
fd40: 2a 20 66 72 6f 6d 20 78 46 75 6c 6c 50 61 74 68 * from xFullPath
fd50: 6e 61 6d 65 28 29 20 77 69 74 68 20 61 6e 20 6f name() with an o
fd60: 70 74 69 6f 6e 61 6c 20 73 75 66 66 69 78 20 61 ptional suffix a
fd70: 64 64 65 64 2e 0a 2a 2a 20 5e 49 66 20 61 20 73 dded..** ^If a s
fd80: 75 66 66 69 78 20 69 73 20 61 64 64 65 64 20 74 uffix is added t
fd90: 6f 20 74 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 o the zFilename
fda0: 70 61 72 61 6d 65 74 65 72 2c 20 69 74 20 77 69 parameter, it wi
fdb0: 6c 6c 0a 2a 2a 20 63 6f 6e 73 69 73 74 20 6f 66 ll.** consist of
fdc0: 20 61 20 73 69 6e 67 6c 65 20 22 2d 22 20 63 68 a single "-" ch
fdd0: 61 72 61 63 74 65 72 20 66 6f 6c 6c 6f 77 65 64 aracter followed
fde0: 20 62 79 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e by no more than
fdf0: 0a 2a 2a 20 31 31 20 61 6c 70 68 61 6e 75 6d 65 .** 11 alphanume
fe00: 72 69 63 20 61 6e 64 2f 6f 72 20 22 2d 22 20 63 ric and/or "-" c
fe10: 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 5e 53 haracters..** ^S
fe20: 51 4c 69 74 65 20 66 75 72 74 68 65 72 20 67 75 QLite further gu
fe30: 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a 2a arantees that.**
fe40: 20 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c 6c the string will
fe50: 20 62 65 20 76 61 6c 69 64 20 61 6e 64 20 75 6e be valid and un
fe60: 63 68 61 6e 67 65 64 20 75 6e 74 69 6c 20 78 43 changed until xC
fe70: 6c 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63 61 6c lose() is.** cal
fe80: 6c 65 64 2e 20 42 65 63 61 75 73 65 20 6f 66 20 led. Because of
fe90: 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e the previous sen
fea0: 74 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20 5b 73 tence,.** the [s
feb0: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61 6e qlite3_file] can
fec0: 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61 20 safely store a
fed0: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a pointer to the.*
fee0: 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69 74 * filename if it
fef0: 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d 62 needs to rememb
ff00: 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 er the filename
ff10: 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e for some reason.
ff20: 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c 65 .** If the zFile
ff30: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 74 name parameter t
ff40: 6f 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55 4c o xOpen is a NUL
ff50: 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 78 L pointer then x
ff60: 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 76 Open.** must inv
ff70: 65 6e 74 20 69 74 73 20 6f 77 6e 20 74 65 6d 70 ent its own temp
ff80: 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 20 74 orary name for t
ff90: 68 65 20 66 69 6c 65 2e 20 20 5e 57 68 65 6e 65 he file. ^Whene
ffa0: 76 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 6c ver the .** xFil
ffb0: 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 ename parameter
ffc0: 69 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c 20 is NULL it will
ffd0: 61 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73 65 also be the case
ffe0: 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c 61 that the.** fla
fff0: 67 73 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c gs parameter wil
10000 6c 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 l include [SQLIT
10010 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 E_OPEN_DELETEONC
10020 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 LOSE]..**.** The
10030 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 flags argument
10040 74 6f 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c 75 to xOpen() inclu
10050 64 65 73 20 61 6c 6c 20 62 69 74 73 20 73 65 74 des all bits set
10060 20 69 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67 73 in.** the flags
10070 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 argument to [sq
10080 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d lite3_open_v2()]
10090 2e 20 20 4f 72 20 69 66 20 5b 73 71 6c 69 74 65 . Or if [sqlite
100a0 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72 20 3_open()].** or
100b0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 [sqlite3_open16(
100c0 29 5d 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e )] is used, then
100d0 20 66 6c 61 67 73 20 69 6e 63 6c 75 64 65 73 20 flags includes
100e0 61 74 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51 4c at least.** [SQL
100f0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
10100 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 TE] | [SQLITE_OP
10110 45 4e 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a 20 EN_CREATE]. .**
10120 49 66 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73 If xOpen() opens
10130 20 61 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c a file read-onl
10140 79 20 74 68 65 6e 20 69 74 20 73 65 74 73 20 2a y then it sets *
10150 70 4f 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20 pOutFlags to.**
10160 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f include [SQLITE_
10170 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20 OPEN_READONLY].
10180 20 4f 74 68 65 72 20 62 69 74 73 20 69 6e 20 2a Other bits in *
10190 70 4f 75 74 46 6c 61 67 73 20 6d 61 79 20 62 65 pOutFlags may be
101a0 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 5e 28 53 51 set..**.** ^(SQ
101b0 4c 69 74 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61 Lite will also a
101c0 64 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f dd one of the fo
101d0 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f llowing flags to
101e0 20 74 68 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 the xOpen().**
101f0 63 61 6c 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 call, depending
10200 6f 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 62 65 on the object be
10210 69 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a ing opened:.**.*
10220 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 * <ul>.** <li>
10230 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 [SQLITE_OPEN_MAI
10240 4e 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b N_DB].** <li> [
10250 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
10260 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 _JOURNAL].** <li
10270 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f > [SQLITE_OPEN_
10280 54 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e TEMP_DB].** <li>
10290 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 [SQLITE_OPEN_T
102a0 45 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 EMP_JOURNAL].**
102b0 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 <li> [SQLITE_OP
102c0 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d EN_TRANSIENT_DB]
102d0 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 .** <li> [SQLIT
102e0 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 E_OPEN_SUBJOURNA
102f0 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c L].** <li> [SQL
10300 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f ITE_OPEN_MASTER_
10310 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e JOURNAL].** <li>
10320 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 57 [SQLITE_OPEN_W
10330 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 29 5e 0a 2a AL].** </ul>)^.*
10340 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 49 2f *.** The file I/
10350 4f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e O implementation
10360 20 63 61 6e 20 75 73 65 20 74 68 65 20 6f 62 6a can use the obj
10370 65 63 74 20 74 79 70 65 20 66 6c 61 67 73 20 74 ect type flags t
10380 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 65 20 o.** change the
10390 77 61 79 20 69 74 20 64 65 61 6c 73 20 77 69 74 way it deals wit
103a0 68 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 65 78 h files. For ex
103b0 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 ample, an applic
103c0 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 64 6f ation.** that do
103d0 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 es not care abou
103e0 74 20 63 72 61 73 68 20 72 65 63 6f 76 65 72 79 t crash recovery
103f0 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d 69 67 or rollback mig
10400 68 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6f ht make.** the o
10410 70 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c pen of a journal
10420 20 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 file a no-op.
10430 57 72 69 74 65 73 20 74 6f 20 74 68 69 73 20 6a Writes to this j
10440 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a 2a 20 ournal would.**
10450 61 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c 20 also be no-ops,
10460 61 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 74 20 and any attempt
10470 74 6f 20 72 65 61 64 20 74 68 65 20 6a 6f 75 72 to read the jour
10480 6e 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 72 6e nal would return
10490 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 .** SQLITE_IOERR
104a0 2e 20 20 4f 72 20 74 68 65 20 69 6d 70 6c 65 6d . Or the implem
104b0 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 72 entation might r
104c0 65 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 61 20 ecognize that a
104d0 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 database.** file
104e0 20 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 20 70 will be doing p
104f0 61 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 63 74 age-aligned sect
10500 6f 72 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 or reads and wri
10510 74 65 73 20 69 6e 20 61 20 72 61 6e 64 6f 6d 0a tes in a random.
10520 2a 2a 20 6f 72 64 65 72 20 61 6e 64 20 73 65 74 ** order and set
10530 20 75 70 20 69 74 73 20 49 2f 4f 20 73 75 62 73 up its I/O subs
10540 79 73 74 65 6d 20 61 63 63 6f 72 64 69 6e 67 6c ystem accordingl
10550 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 y..**.** SQLite
10560 6d 69 67 68 74 20 61 6c 73 6f 20 61 64 64 20 6f might also add o
10570 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ne of the follow
10580 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65 ing flags to the
10590 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a 0a 2a xOpen method:.*
105a0 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
105b0 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 > [SQLITE_OPEN_D
105c0 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a ELETEONCLOSE].**
105d0 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 <li> [SQLITE_OP
105e0 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a 2a 2a EN_EXCLUSIVE].**
105f0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 </ul>.**.** The
10600 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 [SQLITE_OPEN_DE
10610 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 6c 61 LETEONCLOSE] fla
10620 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65 g means the file
10630 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 65 should be.** de
10640 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20 69 73 leted when it is
10650 20 63 6c 6f 73 65 64 2e 20 20 5e 54 68 65 20 5b closed. ^The [
10660 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 SQLITE_OPEN_DELE
10670 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69 TEONCLOSE].** wi
10680 6c 6c 20 62 65 20 73 65 74 20 66 6f 72 20 54 45 ll be set for TE
10690 4d 50 20 64 61 74 61 62 61 73 65 73 20 61 6e 64 MP databases and
106a0 20 74 68 65 69 72 20 6a 6f 75 72 6e 61 6c 73 2c their journals,
106b0 20 74 72 61 6e 73 69 65 6e 74 0a 2a 2a 20 64 61 transient.** da
106c0 74 61 62 61 73 65 73 2c 20 61 6e 64 20 73 75 62 tabases, and sub
106d0 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 journals..**.**
106e0 5e 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 ^The [SQLITE_OPE
106f0 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 66 6c 61 N_EXCLUSIVE] fla
10700 67 20 69 73 20 61 6c 77 61 79 73 20 75 73 65 64 g is always used
10710 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69 6f 6e 0a in conjunction.
10720 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c ** with the [SQL
10730 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d ITE_OPEN_CREATE]
10740 20 66 6c 61 67 2c 20 77 68 69 63 68 20 61 72 65 flag, which are
10750 20 62 6f 74 68 20 64 69 72 65 63 74 6c 79 0a 2a both directly.*
10760 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74 6f 20 74 * analogous to t
10770 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64 20 4f 5f he O_EXCL and O_
10780 43 52 45 41 54 20 66 6c 61 67 73 20 6f 66 20 74 CREAT flags of t
10790 68 65 20 50 4f 53 49 58 20 6f 70 65 6e 28 29 0a he POSIX open().
107a0 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 53 51 4c ** API. The SQL
107b0 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
107c0 56 45 20 66 6c 61 67 2c 20 77 68 65 6e 20 70 61 VE flag, when pa
107d0 69 72 65 64 20 77 69 74 68 20 74 68 65 20 0a 2a ired with the .*
107e0 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 * SQLITE_OPEN_CR
107f0 45 41 54 45 2c 20 69 73 20 75 73 65 64 20 74 6f EATE, is used to
10800 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 66 indicate that f
10810 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 ile should alway
10820 73 0a 2a 2a 20 62 65 20 63 72 65 61 74 65 64 2c s.** be created,
10830 20 61 6e 64 20 74 68 61 74 20 69 74 20 69 73 20 and that it is
10840 61 6e 20 65 72 72 6f 72 20 69 66 20 69 74 20 61 an error if it a
10850 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 0a 2a lready exists..*
10860 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f 74 3c 2f * It is <i>not</
10870 69 3e 20 75 73 65 64 20 74 6f 20 69 6e 64 69 63 i> used to indic
10880 61 74 65 20 74 68 65 20 66 69 6c 65 20 73 68 6f ate the file sho
10890 75 6c 64 20 62 65 20 6f 70 65 6e 65 64 20 0a 2a uld be opened .*
108a0 2a 20 66 6f 72 20 65 78 63 6c 75 73 69 76 65 20 * for exclusive
108b0 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 5e 41 access..**.** ^A
108c0 74 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c 65 t least szOsFile
108d0 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 bytes of memory
108e0 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 are allocated b
108f0 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f 20 68 y SQLite.** to h
10900 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74 65 old the [sqlite
10910 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75 72 3_file] structur
10920 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
10930 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e third.** argumen
10940 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 65 t to xOpen. The
10950 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 6f xOpen method do
10960 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a 2a es not have to.*
10970 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 * allocate the s
10980 74 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 6f tructure; it sho
10990 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 74 uld just fill it
109a0 20 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a in. Note that.
109b0 2a 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 ** the xOpen met
109c0 68 6f 64 20 6d 75 73 74 20 73 65 74 20 74 68 65 hod must set the
109d0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d sqlite3_file.pM
109e0 65 74 68 6f 64 73 20 74 6f 20 65 69 74 68 65 72 ethods to either
109f0 0a 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73 71 6c .** a valid [sql
10a00 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d ite3_io_methods]
10a10 20 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20 4e 55 object or to NU
10a20 4c 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73 74 20 LL. xOpen must
10a30 64 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65 6e 20 do.** this even
10a40 69 66 20 74 68 65 20 6f 70 65 6e 20 66 61 69 6c if the open fail
10a50 73 2e 20 20 53 51 4c 69 74 65 20 65 78 70 65 63 s. SQLite expec
10a60 74 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 ts that the sqli
10a70 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f 64 te3_file.pMethod
10a80 73 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77 69 6c s.** element wil
10a90 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74 65 72 l be valid after
10aa0 20 78 4f 70 65 6e 20 72 65 74 75 72 6e 73 20 72 xOpen returns r
10ab0 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 egardless of the
10ac0 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 66 success.** or f
10ad0 61 69 6c 75 72 65 20 6f 66 20 74 68 65 20 78 4f ailure of the xO
10ae0 70 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 pen call..**.**
10af0 5b 5b 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 41 [[sqlite3_vfs.xA
10b00 63 63 65 73 73 5d 5d 0a 2a 2a 20 5e 54 68 65 20 ccess]].** ^The
10b10 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 flags argument t
10b20 6f 20 78 41 63 63 65 73 73 28 29 20 6d 61 79 20 o xAccess() may
10b30 62 65 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 53 be [SQLITE_ACCES
10b40 53 5f 45 58 49 53 54 53 5d 0a 2a 2a 20 74 6f 20 S_EXISTS].** to
10b50 74 65 73 74 20 66 6f 72 20 74 68 65 20 65 78 69 test for the exi
10b60 73 74 65 6e 63 65 20 6f 66 20 61 20 66 69 6c 65 stence of a file
10b70 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 , or [SQLITE_ACC
10b80 45 53 53 5f 52 45 41 44 57 52 49 54 45 5d 20 74 ESS_READWRITE] t
10b90 6f 0a 2a 2a 20 74 65 73 74 20 77 68 65 74 68 65 o.** test whethe
10ba0 72 20 61 20 66 69 6c 65 20 69 73 20 72 65 61 64 r a file is read
10bb0 61 62 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c able and writabl
10bc0 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 e, or [SQLITE_AC
10bd0 43 45 53 53 5f 52 45 41 44 5d 0a 2a 2a 20 74 6f CESS_READ].** to
10be0 20 74 65 73 74 20 77 68 65 74 68 65 72 20 61 20 test whether a
10bf0 66 69 6c 65 20 69 73 20 61 74 20 6c 65 61 73 74 file is at least
10c00 20 72 65 61 64 61 62 6c 65 2e 20 20 20 54 68 65 readable. The
10c10 20 66 69 6c 65 20 63 61 6e 20 62 65 20 61 0a 2a file can be a.*
10c20 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a * directory..**.
10c30 2a 2a 20 5e 53 51 4c 69 74 65 20 77 69 6c 6c 20 ** ^SQLite will
10c40 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 74 65 20 always allocate
10c50 61 74 20 6c 65 61 73 74 20 6d 78 50 61 74 68 6e at least mxPathn
10c60 61 6d 65 2b 31 20 62 79 74 65 73 20 66 6f 72 20 ame+1 bytes for
10c70 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 the.** output bu
10c80 66 66 65 72 20 78 46 75 6c 6c 50 61 74 68 6e 61 ffer xFullPathna
10c90 6d 65 2e 20 20 54 68 65 20 65 78 61 63 74 20 73 me. The exact s
10ca0 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 ize of the outpu
10cb0 74 20 62 75 66 66 65 72 0a 2a 2a 20 69 73 20 61 t buffer.** is a
10cc0 6c 73 6f 20 70 61 73 73 65 64 20 61 73 20 61 20 lso passed as a
10cd0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 6f 74 parameter to bot
10ce0 68 20 20 6d 65 74 68 6f 64 73 2e 20 49 66 20 74 h methods. If t
10cf0 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 he output buffer
10d00 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 .** is not large
10d10 20 65 6e 6f 75 67 68 2c 20 5b 53 51 4c 49 54 45 enough, [SQLITE
10d20 5f 43 41 4e 54 4f 50 45 4e 5d 20 73 68 6f 75 6c _CANTOPEN] shoul
10d30 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 53 d be returned. S
10d40 69 6e 63 65 20 74 68 69 73 20 69 73 0a 2a 2a 20 ince this is.**
10d50 68 61 6e 64 6c 65 64 20 61 73 20 61 20 66 61 74 handled as a fat
10d60 61 6c 20 65 72 72 6f 72 20 62 79 20 53 51 4c 69 al error by SQLi
10d70 74 65 2c 20 76 66 73 20 69 6d 70 6c 65 6d 65 6e te, vfs implemen
10d80 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 65 tations should e
10d90 6e 64 65 61 76 6f 72 0a 2a 2a 20 74 6f 20 70 72 ndeavor.** to pr
10da0 65 76 65 6e 74 20 74 68 69 73 20 62 79 20 73 65 event this by se
10db0 74 74 69 6e 67 20 6d 78 50 61 74 68 6e 61 6d 65 tting mxPathname
10dc0 20 74 6f 20 61 20 73 75 66 66 69 63 69 65 6e 74 to a sufficient
10dd0 6c 79 20 6c 61 72 67 65 20 76 61 6c 75 65 2e 0a ly large value..
10de0 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f **.** The xRando
10df0 6d 6e 65 73 73 28 29 2c 20 78 53 6c 65 65 70 28 mness(), xSleep(
10e00 29 2c 20 78 43 75 72 72 65 6e 74 54 69 6d 65 28 ), xCurrentTime(
10e10 29 2c 20 61 6e 64 20 78 43 75 72 72 65 6e 74 54 ), and xCurrentT
10e20 69 6d 65 49 6e 74 36 34 28 29 0a 2a 2a 20 69 6e imeInt64().** in
10e30 74 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 74 terfaces are not
10e40 20 73 74 72 69 63 74 6c 79 20 61 20 70 61 72 74 strictly a part
10e50 20 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73 74 of the filesyst
10e60 65 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 em, but they are
10e70 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 .** included in
10e80 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72 the VFS structur
10e90 65 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65 e for completene
10ea0 73 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 ss..** The xRand
10eb0 6f 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f omness() functio
10ec0 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 n attempts to re
10ed0 74 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65 turn nBytes byte
10ee0 73 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61 s.** of good-qua
10ef0 6c 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20 lity randomness
10f00 69 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20 into zOut. The
10f10 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a return value is.
10f20 2a 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75 ** the actual nu
10f30 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
10f40 20 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 randomness obta
10f50 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c ined..** The xSl
10f60 65 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75 eep() method cau
10f70 73 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 ses the calling
10f80 74 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20 thread to sleep
10f90 66 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 for at.** least
10fa0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 the number of mi
10fb0 63 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e croseconds given
10fc0 2e 20 20 5e 54 68 65 20 78 43 75 72 72 65 6e 74 . ^The xCurrent
10fd0 54 69 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 Time().** method
10fe0 20 72 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 returns a Julia
10ff0 6e 20 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 n Day Number for
11000 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 the current dat
11010 65 20 61 6e 64 20 74 69 6d 65 20 61 73 0a 2a 2a e and time as.**
11020 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e a floating poin
11030 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 5e 54 68 65 t value..** ^The
11040 20 78 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 xCurrentTimeInt
11050 36 34 28 29 20 6d 65 74 68 6f 64 20 72 65 74 75 64() method retu
11060 72 6e 73 2c 20 61 73 20 61 6e 20 69 6e 74 65 67 rns, as an integ
11070 65 72 2c 20 74 68 65 20 4a 75 6c 69 61 6e 0a 2a er, the Julian.*
11080 2a 20 44 61 79 20 4e 75 6d 62 65 72 20 6d 75 6c * Day Number mul
11090 74 69 70 6c 69 65 64 20 62 79 20 38 36 34 30 30 tiplied by 86400
110a0 30 30 30 20 28 74 68 65 20 6e 75 6d 62 65 72 20 000 (the number
110b0 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 of milliseconds
110c0 69 6e 20 0a 2a 2a 20 61 20 32 34 2d 68 6f 75 72 in .** a 24-hour
110d0 20 64 61 79 29 2e 20 20 0a 2a 2a 20 5e 53 51 4c day). .** ^SQL
110e0 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 ite will use the
110f0 20 78 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 xCurrentTimeInt
11100 36 34 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 67 64() method to g
11110 65 74 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a et the current.*
11120 2a 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 20 * date and time
11130 69 66 20 74 68 61 74 20 6d 65 74 68 6f 64 20 69 if that method i
11140 73 20 61 76 61 69 6c 61 62 6c 65 20 28 69 66 20 s available (if
11150 69 56 65 72 73 69 6f 6e 20 69 73 20 32 20 6f 72 iVersion is 2 or
11160 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 61 6e 64 .** greater and
11170 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 6f the function po
11180 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c inter is not NUL
11190 4c 29 20 61 6e 64 20 77 69 6c 6c 20 66 61 6c 6c L) and will fall
111a0 20 62 61 63 6b 0a 2a 2a 20 74 6f 20 78 43 75 72 back.** to xCur
111b0 72 65 6e 74 54 69 6d 65 28 29 20 69 66 20 78 43 rentTime() if xC
111c0 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 urrentTimeInt64(
111d0 29 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65 ) is unavailable
111e0 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 78 53 65 ..**.** ^The xSe
111f0 74 53 79 73 74 65 6d 43 61 6c 6c 28 29 2c 20 78 tSystemCall(), x
11200 47 65 74 53 79 73 74 65 6d 43 61 6c 6c 28 29 2c GetSystemCall(),
11210 20 61 6e 64 20 78 4e 65 73 74 53 79 73 74 65 6d and xNestSystem
11220 43 61 6c 6c 28 29 20 69 6e 74 65 72 66 61 63 65 Call() interface
11230 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 75 73 65 s.** are not use
11240 64 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 d by the SQLite
11250 63 6f 72 65 2e 20 20 54 68 65 73 65 20 6f 70 74 core. These opt
11260 69 6f 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 73 ional interfaces
11270 20 61 72 65 20 70 72 6f 76 69 64 65 64 0a 2a 2a are provided.**
11280 20 62 79 20 73 6f 6d 65 20 56 46 53 65 73 20 74 by some VFSes t
11290 6f 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 o facilitate tes
112a0 74 69 6e 67 20 6f 66 20 74 68 65 20 56 46 53 20 ting of the VFS
112b0 63 6f 64 65 2e 20 42 79 20 6f 76 65 72 72 69 64 code. By overrid
112c0 69 6e 67 20 0a 2a 2a 20 73 79 73 74 65 6d 20 63 ing .** system c
112d0 61 6c 6c 73 20 77 69 74 68 20 66 75 6e 63 74 69 alls with functi
112e0 6f 6e 73 20 75 6e 64 65 72 20 69 74 73 20 63 6f ons under its co
112f0 6e 74 72 6f 6c 2c 20 61 20 74 65 73 74 20 70 72 ntrol, a test pr
11300 6f 67 72 61 6d 20 63 61 6e 0a 2a 2a 20 73 69 6d ogram can.** sim
11310 75 6c 61 74 65 20 66 61 75 6c 74 73 20 61 6e 64 ulate faults and
11320 20 65 72 72 6f 72 20 63 6f 6e 64 69 74 69 6f 6e error condition
11330 73 20 74 68 61 74 20 77 6f 75 6c 64 20 6f 74 68 s that would oth
11340 65 72 77 69 73 65 20 62 65 20 64 69 66 66 69 63 erwise be diffic
11350 75 6c 74 0a 2a 2a 20 6f 72 20 69 6d 70 6f 73 73 ult.** or imposs
11360 69 62 6c 65 20 74 6f 20 69 6e 64 75 63 65 2e 20 ible to induce.
11370 20 54 68 65 20 73 65 74 20 6f 66 20 73 79 73 74 The set of syst
11380 65 6d 20 63 61 6c 6c 73 20 74 68 61 74 20 63 61 em calls that ca
11390 6e 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 0a n be overridden.
113a0 2a 2a 20 76 61 72 69 65 73 20 66 72 6f 6d 20 6f ** varies from o
113b0 6e 65 20 56 46 53 20 74 6f 20 61 6e 6f 74 68 65 ne VFS to anothe
113c0 72 2c 20 61 6e 64 20 66 72 6f 6d 20 6f 6e 65 20 r, and from one
113d0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 73 version of the s
113e0 61 6d 65 20 56 46 53 20 74 6f 20 74 68 65 0a 2a ame VFS to the.*
113f0 2a 20 6e 65 78 74 2e 20 20 41 70 70 6c 69 63 61 * next. Applica
11400 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 74 tions that use t
11410 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 20 hese interfaces
11420 6d 75 73 74 20 62 65 20 70 72 65 70 61 72 65 64 must be prepared
11430 20 66 6f 72 20 61 6e 79 0a 2a 2a 20 6f 72 20 61 for any.** or a
11440 6c 6c 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 ll of these inte
11450 72 66 61 63 65 73 20 74 6f 20 62 65 20 4e 55 4c rfaces to be NUL
11460 4c 20 6f 72 20 66 6f 72 20 74 68 65 69 72 20 62 L or for their b
11470 65 68 61 76 69 6f 72 20 74 6f 20 63 68 61 6e 67 ehavior to chang
11480 65 0a 2a 2a 20 66 72 6f 6d 20 6f 6e 65 20 72 65 e.** from one re
11490 6c 65 61 73 65 20 74 6f 20 74 68 65 20 6e 65 78 lease to the nex
114a0 74 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 t. Applications
114b0 20 6d 75 73 74 20 6e 6f 74 20 61 74 74 65 6d 70 must not attemp
114c0 74 20 74 6f 20 61 63 63 65 73 73 0a 2a 2a 20 61 t to access.** a
114d0 6e 79 20 6f 66 20 74 68 65 73 65 20 6d 65 74 68 ny of these meth
114e0 6f 64 73 20 69 66 20 74 68 65 20 69 56 65 72 73 ods if the iVers
114f0 69 6f 6e 20 6f 66 20 74 68 65 20 56 46 53 20 69 ion of the VFS i
11500 73 20 6c 65 73 73 20 74 68 61 6e 20 33 2e 0a 2a s less than 3..*
11510 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
11520 20 73 71 6c 69 74 65 33 5f 76 66 73 20 73 71 6c sqlite3_vfs sql
11530 69 74 65 33 5f 76 66 73 3b 0a 74 79 70 65 64 65 ite3_vfs;.typede
11540 66 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 f void (*sqlite3
11550 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 28 76 6f _syscall_ptr)(vo
11560 69 64 29 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 id);.struct sqli
11570 74 65 33 5f 76 66 73 20 7b 0a 20 20 69 6e 74 20 te3_vfs {. int
11580 69 56 65 72 73 69 6f 6e 3b 20 20 20 20 20 20 20 iVersion;
11590 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74 75 72 /* Structur
115a0 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 e version number
115b0 20 28 63 75 72 72 65 6e 74 6c 79 20 33 29 20 2a (currently 3) *
115c0 2f 0a 20 20 69 6e 74 20 73 7a 4f 73 46 69 6c 65 /. int szOsFile
115d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
115e0 53 69 7a 65 20 6f 66 20 73 75 62 63 6c 61 73 73 Size of subclass
115f0 65 64 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ed sqlite3_file
11600 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 74 68 6e */. int mxPathn
11610 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ame; /*
11620 20 4d 61 78 69 6d 75 6d 20 66 69 6c 65 20 70 61 Maximum file pa
11630 74 68 6e 61 6d 65 20 6c 65 6e 67 74 68 20 2a 2f thname length */
11640 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
11650 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e pNext; /* N
11660 65 78 74 20 72 65 67 69 73 74 65 72 65 64 20 56 ext registered V
11670 46 53 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 FS */. const ch
11680 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 ar *zName;
11690 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 /* Name of this
116a0 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 virtual file sy
116b0 73 74 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a stem */. void *
116c0 70 41 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 pAppData;
116d0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
116e0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 70 65 application-spe
116f0 63 69 66 69 63 20 64 61 74 61 20 2a 2f 0a 20 20 cific data */.
11700 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c int (*xOpen)(sql
11710 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 ite3_vfs*, const
11720 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 73 71 char *zName, sq
11730 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 20 20 lite3_file*,.
11740 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 int
11750 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 4f 75 74 flags, int *pOut
11760 46 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a Flags);. int (*
11770 78 44 65 6c 65 74 65 29 28 73 71 6c 69 74 65 33 xDelete)(sqlite3
11780 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 _vfs*, const cha
11790 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 73 79 r *zName, int sy
117a0 6e 63 44 69 72 29 3b 0a 20 20 69 6e 74 20 28 2a ncDir);. int (*
117b0 78 41 63 63 65 73 73 29 28 73 71 6c 69 74 65 33 xAccess)(sqlite3
117c0 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 _vfs*, const cha
117d0 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c r *zName, int fl
117e0 61 67 73 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 ags, int *pResOu
117f0 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6c t);. int (*xFul
11800 6c 50 61 74 68 6e 61 6d 65 29 28 73 71 6c 69 74 lPathname)(sqlit
11810 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 e3_vfs*, const c
11820 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 har *zName, int
11830 6e 4f 75 74 2c 20 63 68 61 72 20 2a 7a 4f 75 74 nOut, char *zOut
11840 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c );. void *(*xDl
11850 4f 70 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 Open)(sqlite3_vf
11860 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a s*, const char *
11870 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 76 6f zFilename);. vo
11880 69 64 20 28 2a 78 44 6c 45 72 72 6f 72 29 28 73 id (*xDlError)(s
11890 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 qlite3_vfs*, int
118a0 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 45 nByte, char *zE
118b0 72 72 4d 73 67 29 3b 0a 20 20 76 6f 69 64 20 28 rrMsg);. void (
118c0 2a 28 2a 78 44 6c 53 79 6d 29 28 73 71 6c 69 74 *(*xDlSym)(sqlit
118d0 65 33 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c 20 63 e3_vfs*,void*, c
118e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 onst char *zSymb
118f0 6f 6c 29 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f ol))(void);. vo
11900 69 64 20 28 2a 78 44 6c 43 6c 6f 73 65 29 28 73 id (*xDlClose)(s
11910 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 76 6f 69 qlite3_vfs*, voi
11920 64 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 61 d*);. int (*xRa
11930 6e 64 6f 6d 6e 65 73 73 29 28 73 71 6c 69 74 65 ndomness)(sqlite
11940 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e 42 79 74 3_vfs*, int nByt
11950 65 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b 0a e, char *zOut);.
11960 20 20 69 6e 74 20 28 2a 78 53 6c 65 65 70 29 28 int (*xSleep)(
11970 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e sqlite3_vfs*, in
11980 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b t microseconds);
11990 0a 20 20 69 6e 74 20 28 2a 78 43 75 72 72 65 6e . int (*xCurren
119a0 74 54 69 6d 65 29 28 73 71 6c 69 74 65 33 5f 76 tTime)(sqlite3_v
119b0 66 73 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 20 fs*, double*);.
119c0 20 69 6e 74 20 28 2a 78 47 65 74 4c 61 73 74 45 int (*xGetLastE
119d0 72 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 rror)(sqlite3_vf
119e0 73 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 s*, int, char *)
119f0 3b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 ;. /*. ** The
11a00 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20 61 72 methods above ar
11a10 65 20 69 6e 20 76 65 72 73 69 6f 6e 20 31 20 6f e in version 1 o
11a20 66 20 74 68 65 20 73 71 6c 69 74 65 5f 76 66 73 f the sqlite_vfs
11a30 20 6f 62 6a 65 63 74 0a 20 20 2a 2a 20 64 65 66 object. ** def
11a40 69 6e 69 74 69 6f 6e 2e 20 20 54 68 6f 73 65 20 inition. Those
11a50 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61 72 65 20 that follow are
11a60 61 64 64 65 64 20 69 6e 20 76 65 72 73 69 6f 6e added in version
11a70 20 32 20 6f 72 20 6c 61 74 65 72 0a 20 20 2a 2f 2 or later. */
11a80 0a 20 20 69 6e 74 20 28 2a 78 43 75 72 72 65 6e . int (*xCurren
11a90 74 54 69 6d 65 49 6e 74 36 34 29 28 73 71 6c 69 tTimeInt64)(sqli
11aa0 74 65 33 5f 76 66 73 2a 2c 20 73 71 6c 69 74 65 te3_vfs*, sqlite
11ab0 33 5f 69 6e 74 36 34 2a 29 3b 0a 20 20 2f 2a 0a 3_int64*);. /*.
11ac0 20 20 2a 2a 20 54 68 65 20 6d 65 74 68 6f 64 73 ** The methods
11ad0 20 61 62 6f 76 65 20 61 72 65 20 69 6e 20 76 65 above are in ve
11ae0 72 73 69 6f 6e 73 20 31 20 61 6e 64 20 32 20 6f rsions 1 and 2 o
11af0 66 20 74 68 65 20 73 71 6c 69 74 65 5f 76 66 73 f the sqlite_vfs
11b00 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a 20 54 68 object.. ** Th
11b10 6f 73 65 20 62 65 6c 6f 77 20 61 72 65 20 66 6f ose below are fo
11b20 72 20 76 65 72 73 69 6f 6e 20 33 20 61 6e 64 20 r version 3 and
11b30 67 72 65 61 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 greater.. */.
11b40 69 6e 74 20 28 2a 78 53 65 74 53 79 73 74 65 6d int (*xSetSystem
11b50 43 61 6c 6c 29 28 73 71 6c 69 74 65 33 5f 76 66 Call)(sqlite3_vf
11b60 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a s*, const char *
11b70 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 5f 73 zName, sqlite3_s
11b80 79 73 63 61 6c 6c 5f 70 74 72 29 3b 0a 20 20 73 yscall_ptr);. s
11b90 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 qlite3_syscall_p
11ba0 74 72 20 28 2a 78 47 65 74 53 79 73 74 65 6d 43 tr (*xGetSystemC
11bb0 61 6c 6c 29 28 73 71 6c 69 74 65 33 5f 76 66 73 all)(sqlite3_vfs
11bc0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a *, const char *z
11bd0 4e 61 6d 65 29 3b 0a 20 20 63 6f 6e 73 74 20 63 Name);. const c
11be0 68 61 72 20 2a 28 2a 78 4e 65 78 74 53 79 73 74 har *(*xNextSyst
11bf0 65 6d 43 61 6c 6c 29 28 73 71 6c 69 74 65 33 5f emCall)(sqlite3_
11c00 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 vfs*, const char
11c10 20 2a 7a 4e 61 6d 65 29 3b 0a 20 20 2f 2a 0a 20 *zName);. /*.
11c20 20 2a 2a 20 54 68 65 20 6d 65 74 68 6f 64 73 20 ** The methods
11c30 61 62 6f 76 65 20 61 72 65 20 69 6e 20 76 65 72 above are in ver
11c40 73 69 6f 6e 73 20 31 20 74 68 72 6f 75 67 68 20 sions 1 through
11c50 33 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 3 of the sqlite_
11c60 76 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a vfs object.. **
11c70 20 4e 65 77 20 66 69 65 6c 64 73 20 6d 61 79 20 New fields may
11c80 62 65 20 61 70 70 65 6e 64 65 64 20 69 6e 20 66 be appended in f
11c90 69 67 75 72 65 20 76 65 72 73 69 6f 6e 73 2e 20 igure versions.
11ca0 20 54 68 65 20 69 56 65 72 73 69 6f 6e 0a 20 20 The iVersion.
11cb0 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c 20 69 6e ** value will in
11cc0 63 72 65 6d 65 6e 74 20 77 68 65 6e 65 76 65 72 crement whenever
11cd0 20 74 68 69 73 20 68 61 70 70 65 6e 73 2e 20 0a this happens. .
11ce0 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 */.};../*.** C
11cf0 41 50 49 33 52 45 46 3a 20 46 6c 61 67 73 20 66 API3REF: Flags f
11d00 6f 72 20 74 68 65 20 78 41 63 63 65 73 73 20 56 or the xAccess V
11d10 46 53 20 6d 65 74 68 6f 64 0a 2a 2a 0a 2a 2a 20 FS method.**.**
11d20 54 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f These integer co
11d30 6e 73 74 61 6e 74 73 20 63 61 6e 20 62 65 20 75 nstants can be u
11d40 73 65 64 20 61 73 20 74 68 65 20 74 68 69 72 64 sed as the third
11d50 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a parameter to.**
11d60 20 74 68 65 20 78 41 63 63 65 73 73 20 6d 65 74 the xAccess met
11d70 68 6f 64 20 6f 66 20 61 6e 20 5b 73 71 6c 69 74 hod of an [sqlit
11d80 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 20 e3_vfs] object.
11d90 20 54 68 65 79 20 64 65 74 65 72 6d 69 6e 65 0a They determine.
11da0 2a 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 ** what kind of
11db0 70 65 72 6d 69 73 73 69 6f 6e 73 20 74 68 65 20 permissions the
11dc0 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 69 xAccess method i
11dd0 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a 2a s looking for..*
11de0 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43 * With SQLITE_AC
11df0 43 45 53 53 5f 45 58 49 53 54 53 2c 20 74 68 65 CESS_EXISTS, the
11e00 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a xAccess method.
11e10 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 6b 73 ** simply checks
11e20 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c whether the fil
11e30 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69 74 e exists..** Wit
11e40 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f h SQLITE_ACCESS_
11e50 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20 78 READWRITE, the x
11e60 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a Access method.**
11e70 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 checks whether
11e80 74 68 65 20 6e 61 6d 65 64 20 64 69 72 65 63 74 the named direct
11e90 6f 72 79 20 69 73 20 62 6f 74 68 20 72 65 61 64 ory is both read
11ea0 61 62 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c able and writabl
11eb0 65 0a 2a 2a 20 28 69 6e 20 6f 74 68 65 72 20 77 e.** (in other w
11ec0 6f 72 64 73 2c 20 69 66 20 66 69 6c 65 73 20 63 ords, if files c
11ed0 61 6e 20 62 65 20 61 64 64 65 64 2c 20 72 65 6d an be added, rem
11ee0 6f 76 65 64 2c 20 61 6e 64 20 72 65 6e 61 6d 65 oved, and rename
11ef0 64 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 d within.** the
11f00 64 69 72 65 63 74 6f 72 79 29 2e 0a 2a 2a 20 54 directory)..** T
11f10 68 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 he SQLITE_ACCESS
11f20 5f 52 45 41 44 57 52 49 54 45 20 63 6f 6e 73 74 _READWRITE const
11f30 61 6e 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 ant is currently
11f40 20 75 73 65 64 20 6f 6e 6c 79 20 62 79 20 74 68 used only by th
11f50 65 0a 2a 2a 20 5b 74 65 6d 70 5f 73 74 6f 72 65 e.** [temp_store
11f60 5f 64 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d _directory pragm
11f70 61 5d 2c 20 74 68 6f 75 67 68 20 74 68 69 73 20 a], though this
11f80 63 6f 75 6c 64 20 63 68 61 6e 67 65 20 69 6e 20 could change in
11f90 61 20 66 75 74 75 72 65 0a 2a 2a 20 72 65 6c 65 a future.** rele
11fa0 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a ase of SQLite..*
11fb0 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43 * With SQLITE_AC
11fc0 43 45 53 53 5f 52 45 41 44 2c 20 74 68 65 20 78 CESS_READ, the x
11fd0 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a Access method.**
11fe0 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 checks whether
11ff0 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 61 64 the file is read
12000 61 62 6c 65 2e 20 20 54 68 65 20 53 51 4c 49 54 able. The SQLIT
12010 45 5f 41 43 43 45 53 53 5f 52 45 41 44 20 63 6f E_ACCESS_READ co
12020 6e 73 74 61 6e 74 20 69 73 0a 2a 2a 20 63 75 72 nstant is.** cur
12030 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 2c 20 74 rently unused, t
12040 68 6f 75 67 68 20 69 74 20 6d 69 67 68 74 20 62 hough it might b
12050 65 20 75 73 65 64 20 69 6e 20 61 20 66 75 74 75 e used in a futu
12060 72 65 20 72 65 6c 65 61 73 65 20 6f 66 0a 2a 2a re release of.**
12070 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 66 SQLite..*/.#def
12080 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 ine SQLITE_ACCES
12090 53 5f 45 58 49 53 54 53 20 20 20 20 30 0a 23 64 S_EXISTS 0.#d
120a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 efine SQLITE_ACC
120b0 45 53 53 5f 52 45 41 44 57 52 49 54 45 20 31 20 ESS_READWRITE 1
120c0 20 20 2f 2a 20 55 73 65 64 20 62 79 20 50 52 41 /* Used by PRA
120d0 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 GMA temp_store_d
120e0 69 72 65 63 74 6f 72 79 20 2a 2f 0a 23 64 65 66 irectory */.#def
120f0 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 ine SQLITE_ACCES
12100 53 5f 52 45 41 44 20 20 20 20 20 20 32 20 20 20 S_READ 2
12110 2f 2a 20 55 6e 75 73 65 64 20 2a 2f 0a 0a 2f 2a /* Unused */../*
12120 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6c .** CAPI3REF: Fl
12130 61 67 73 20 66 6f 72 20 74 68 65 20 78 53 68 6d ags for the xShm
12140 4c 6f 63 6b 20 56 46 53 20 6d 65 74 68 6f 64 0a Lock VFS method.
12150 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 **.** These inte
12160 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 ger constants de
12170 66 69 6e 65 20 74 68 65 20 76 61 72 69 6f 75 73 fine the various
12180 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 locking operati
12190 6f 6e 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 62 ons.** allowed b
121a0 79 20 74 68 65 20 78 53 68 6d 4c 6f 63 6b 20 6d y the xShmLock m
121b0 65 74 68 6f 64 20 6f 66 20 5b 73 71 6c 69 74 65 ethod of [sqlite
121c0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 2e 20 20 3_io_methods].
121d0 54 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 The.** following
121e0 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 6c 65 are the only le
121f0 67 61 6c 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 gal combinations
12200 20 6f 66 20 66 6c 61 67 73 20 74 6f 20 74 68 65 of flags to the
12210 0a 2a 2a 20 78 53 68 6d 4c 6f 63 6b 20 6d 65 74 .** xShmLock met
12220 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a hod:.**.** <ul>.
12230 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
12240 53 48 4d 5f 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 SHM_LOCK | SQLIT
12250 45 5f 53 48 4d 5f 53 48 41 52 45 44 0a 2a 2a 20 E_SHM_SHARED.**
12260 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 53 48 4d <li> SQLITE_SHM
12270 5f 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53 _LOCK | SQLITE_S
12280 48 4d 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 HM_EXCLUSIVE.**
12290 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 53 48 4d <li> SQLITE_SHM
122a0 5f 55 4e 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 _UNLOCK | SQLITE
122b0 5f 53 48 4d 5f 53 48 41 52 45 44 0a 2a 2a 20 3c _SHM_SHARED.** <
122c0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 53 48 4d 5f li> SQLITE_SHM_
122d0 55 4e 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f UNLOCK | SQLITE_
122e0 53 48 4d 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a SHM_EXCLUSIVE.**
122f0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 </ul>.**.** Whe
12300 6e 20 75 6e 6c 6f 63 6b 69 6e 67 2c 20 74 68 65 n unlocking, the
12310 20 73 61 6d 65 20 53 48 41 52 45 44 20 6f 72 20 same SHARED or
12320 45 58 43 4c 55 53 49 56 45 20 66 6c 61 67 20 6d EXCLUSIVE flag m
12330 75 73 74 20 62 65 20 73 75 70 70 6c 69 65 64 20 ust be supplied
12340 61 73 0a 2a 2a 20 77 61 73 20 67 69 76 65 6e 20 as.** was given
12350 6e 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e no the correspon
12360 64 69 6e 67 20 6c 6f 63 6b 2e 20 20 0a 2a 2a 0a ding lock. .**.
12370 2a 2a 20 54 68 65 20 78 53 68 6d 4c 6f 63 6b 20 ** The xShmLock
12380 6d 65 74 68 6f 64 20 63 61 6e 20 74 72 61 6e 73 method can trans
12390 69 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 75 6e ition between un
123a0 6c 6f 63 6b 65 64 20 61 6e 64 20 53 48 41 52 45 locked and SHARE
123b0 44 20 6f 72 0a 2a 2a 20 62 65 74 77 65 65 6e 20 D or.** between
123c0 75 6e 6c 6f 63 6b 65 64 20 61 6e 64 20 45 58 43 unlocked and EXC
123d0 4c 55 53 49 56 45 2e 20 20 49 74 20 63 61 6e 6e LUSIVE. It cann
123e0 6f 74 20 74 72 61 6e 73 69 74 69 6f 6e 20 62 65 ot transition be
123f0 74 77 65 65 6e 20 53 48 41 52 45 44 0a 2a 2a 20 tween SHARED.**
12400 61 6e 64 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a and EXCLUSIVE..*
12410 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
12420 5f 53 48 4d 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 _SHM_UNLOCK
12430 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 1.#define SQLI
12440 54 45 5f 53 48 4d 5f 4c 4f 43 4b 20 20 20 20 20 TE_SHM_LOCK
12450 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 2.#define SQ
12460 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52 45 44 20 LITE_SHM_SHARED
12470 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4.#define
12480 53 51 4c 49 54 45 5f 53 48 4d 5f 45 58 43 4c 55 SQLITE_SHM_EXCLU
12490 53 49 56 45 20 20 20 20 38 0a 0a 2f 2a 0a 2a 2a SIVE 8../*.**
124a0 20 43 41 50 49 33 52 45 46 3a 20 4d 61 78 69 6d CAPI3REF: Maxim
124b0 75 6d 20 78 53 68 6d 4c 6f 63 6b 20 69 6e 64 65 um xShmLock inde
124c0 78 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 68 6d x.**.** The xShm
124d0 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 6f 6e 20 5b Lock method on [
124e0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
124f0 64 73 5d 20 6d 61 79 20 75 73 65 20 76 61 6c 75 ds] may use valu
12500 65 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 30 20 es.** between 0
12510 61 6e 64 20 74 68 69 73 20 75 70 70 65 72 20 62 and this upper b
12520 6f 75 6e 64 20 61 73 20 69 74 73 20 22 6f 66 66 ound as its "off
12530 73 65 74 22 20 61 72 67 75 6d 65 6e 74 2e 0a 2a set" argument..*
12540 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 * The SQLite cor
12550 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 61 74 74 e will never att
12560 65 6d 70 74 20 74 6f 20 61 63 71 75 69 72 65 20 empt to acquire
12570 6f 72 20 72 65 6c 65 61 73 65 20 61 0a 2a 2a 20 or release a.**
12580 6c 6f 63 6b 20 6f 75 74 73 69 64 65 20 6f 66 20 lock outside of
12590 74 68 69 73 20 72 61 6e 67 65 0a 2a 2f 0a 23 64 this range.*/.#d
125a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 48 4d efine SQLITE_SHM
125b0 5f 4e 4c 4f 43 4b 20 20 20 20 20 20 20 20 38 0a _NLOCK 8.
125c0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
125d0 3a 20 49 6e 69 74 69 61 6c 69 7a 65 20 54 68 65 : Initialize The
125e0 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 0a SQLite Library.
125f0 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 **.** ^The sqlit
12600 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
12610 72 6f 75 74 69 6e 65 20 69 6e 69 74 69 61 6c 69 routine initiali
12620 7a 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69 74 zes the.** SQLit
12630 65 20 6c 69 62 72 61 72 79 2e 20 20 5e 54 68 65 e library. ^The
12640 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
12650 6e 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 n() routine.** d
12660 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 72 eallocates any r
12670 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 77 65 esources that we
12680 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 re allocated by
12690 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
126a0 7a 65 28 29 2e 0a 2a 2a 20 54 68 65 73 65 20 72 ze()..** These r
126b0 6f 75 74 69 6e 65 73 20 61 72 65 20 64 65 73 69 outines are desi
126c0 67 6e 65 64 20 74 6f 20 61 69 64 20 69 6e 20 70 gned to aid in p
126d0 72 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a rocess initializ
126e0 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 68 75 ation and.** shu
126f0 74 64 6f 77 6e 20 6f 6e 20 65 6d 62 65 64 64 65 tdown on embedde
12700 64 20 73 79 73 74 65 6d 73 2e 20 20 57 6f 72 6b d systems. Work
12710 73 74 61 74 69 6f 6e 20 61 70 70 6c 69 63 61 74 station applicat
12720 69 6f 6e 73 20 75 73 69 6e 67 0a 2a 2a 20 53 51 ions using.** SQ
12730 4c 69 74 65 20 6e 6f 72 6d 61 6c 6c 79 20 64 6f Lite normally do
12740 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 69 6e 76 not need to inv
12750 6f 6b 65 20 65 69 74 68 65 72 20 6f 66 20 74 68 oke either of th
12760 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a ese routines..**
12770 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 .** A call to sq
12780 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
12790 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74 () is an "effect
127a0 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20 ive" call if it
127b0 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 is.** the first
127c0 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 time sqlite3_ini
127d0 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 tialize() is inv
127e0 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 oked during the
127f0 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 lifetime of.** t
12800 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 he process, or i
12810 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73 f it is the firs
12820 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 t time sqlite3_i
12830 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 nitialize() is i
12840 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 nvoked.** follow
12850 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 ing a call to sq
12860 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
12870 2e 20 20 5e 28 4f 6e 6c 79 20 61 6e 20 65 66 66 . ^(Only an eff
12880 65 63 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f ective call.** o
12890 66 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 f sqlite3_initia
128a0 6c 69 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20 lize() does any
128b0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 initialization.
128c0 20 41 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 All other calls
128d0 0a 2a 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73 .** are harmless
128e0 20 6e 6f 2d 6f 70 73 2e 29 5e 0a 2a 2a 0a 2a 2a no-ops.)^.**.**
128f0 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 A call to sqlit
12900 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 69 73 e3_shutdown() is
12910 20 61 6e 20 22 65 66 66 65 63 74 69 76 65 22 20 an "effective"
12920 63 61 6c 6c 20 69 66 20 69 74 20 69 73 20 74 68 call if it is th
12930 65 20 66 69 72 73 74 0a 2a 2a 20 63 61 6c 6c 20 e first.** call
12940 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 to sqlite3_shutd
12950 6f 77 6e 28 29 20 73 69 6e 63 65 20 74 68 65 20 own() since the
12960 6c 61 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 last sqlite3_ini
12970 74 69 61 6c 69 7a 65 28 29 2e 20 20 5e 28 4f 6e tialize(). ^(On
12980 6c 79 0a 2a 2a 20 61 6e 20 65 66 66 65 63 74 69 ly.** an effecti
12990 76 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 ve call to sqlit
129a0 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 64 6f e3_shutdown() do
129b0 65 73 20 61 6e 79 20 64 65 69 6e 69 74 69 61 6c es any deinitial
129c0 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 20 41 6c 6c 20 ization..** All
129d0 6f 74 68 65 72 20 76 61 6c 69 64 20 63 61 6c 6c other valid call
129e0 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 s to sqlite3_shu
129f0 74 64 6f 77 6e 28 29 20 61 72 65 20 68 61 72 6d tdown() are harm
12a00 6c 65 73 73 20 6e 6f 2d 6f 70 73 2e 29 5e 0a 2a less no-ops.)^.*
12a10 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
12a20 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 6e _initialize() in
12a30 74 65 72 66 61 63 65 20 69 73 20 74 68 72 65 61 terface is threa
12a40 64 73 61 66 65 2c 20 62 75 74 20 73 71 6c 69 74 dsafe, but sqlit
12a50 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a 2a e3_shutdown().**
12a60 20 69 73 20 6e 6f 74 2e 20 20 54 68 65 20 73 71 is not. The sq
12a70 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 lite3_shutdown()
12a80 20 69 6e 74 65 72 66 61 63 65 20 6d 75 73 74 20 interface must
12a90 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 66 only be called f
12aa0 72 6f 6d 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 rom a.** single
12ab0 74 68 72 65 61 64 2e 20 20 41 6c 6c 20 6f 70 65 thread. All ope
12ac0 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e n [database conn
12ad0 65 63 74 69 6f 6e 73 5d 20 6d 75 73 74 20 62 65 ections] must be
12ae0 20 63 6c 6f 73 65 64 20 61 6e 64 20 61 6c 6c 0a closed and all.
12af0 2a 2a 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 ** other SQLite
12b00 72 65 73 6f 75 72 63 65 73 20 6d 75 73 74 20 62 resources must b
12b10 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 70 72 e deallocated pr
12b20 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 0a ior to invoking.
12b30 2a 2a 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 ** sqlite3_shutd
12b40 6f 77 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f own()..**.** Amo
12b50 6e 67 20 6f 74 68 65 72 20 74 68 69 6e 67 73 2c ng other things,
12b60 20 5e 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 ^sqlite3_initia
12b70 6c 69 7a 65 28 29 20 77 69 6c 6c 20 69 6e 76 6f lize() will invo
12b80 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 ke.** sqlite3_os
12b90 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d 69 6c 61 _init(). Simila
12ba0 72 6c 79 2c 20 5e 73 71 6c 69 74 65 33 5f 73 68 rly, ^sqlite3_sh
12bb0 75 74 64 6f 77 6e 28 29 0a 2a 2a 20 77 69 6c 6c utdown().** will
12bc0 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f invoke sqlite3_
12bd0 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20 os_end()..**.**
12be0 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 ^The sqlite3_ini
12bf0 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e tialize() routin
12c00 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 e returns [SQLIT
12c10 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 E_OK] on success
12c20 2e 0a 2a 2a 20 5e 49 66 20 66 6f 72 20 73 6f 6d ..** ^If for som
12c30 65 20 72 65 61 73 6f 6e 2c 20 73 71 6c 69 74 65 e reason, sqlite
12c40 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 3_initialize() i
12c50 73 20 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74 s unable to init
12c60 69 61 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 6c 69 ialize.** the li
12c70 62 72 61 72 79 20 28 70 65 72 68 61 70 73 20 69 brary (perhaps i
12c80 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 t is unable to a
12c90 6c 6c 6f 63 61 74 65 20 61 20 6e 65 65 64 65 64 llocate a needed
12ca0 20 72 65 73 6f 75 72 63 65 20 73 75 63 68 0a 2a resource such.*
12cb0 2a 20 61 73 20 61 20 6d 75 74 65 78 29 20 69 74 * as a mutex) it
12cc0 20 72 65 74 75 72 6e 73 20 61 6e 20 5b 65 72 72 returns an [err
12cd0 6f 72 20 63 6f 64 65 5d 20 6f 74 68 65 72 20 74 or code] other t
12ce0 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e han [SQLITE_OK].
12cf0 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 .**.** ^The sqli
12d00 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
12d10 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
12d20 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 ed internally by
12d30 20 6d 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 53 many other.** S
12d40 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 QLite interfaces
12d50 20 73 6f 20 74 68 61 74 20 61 6e 20 61 70 70 6c so that an appl
12d60 69 63 61 74 69 6f 6e 20 75 73 75 61 6c 6c 79 20 ication usually
12d70 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
12d80 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 .** invoke sqlit
12d90 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
12da0 64 69 72 65 63 74 6c 79 2e 20 20 46 6f 72 20 65 directly. For e
12db0 78 61 6d 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33 xample, [sqlite3
12dc0 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c _open()].** call
12dd0 73 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 s sqlite3_initia
12de0 6c 69 7a 65 28 29 20 73 6f 20 74 68 65 20 53 51 lize() so the SQ
12df0 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c Lite library wil
12e00 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c l be automatical
12e10 6c 79 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 ly.** initialize
12e20 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f d when [sqlite3_
12e30 6f 70 65 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65 open()] is calle
12e40 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 d if it has not
12e50 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a be initialized.*
12e60 2a 20 61 6c 72 65 61 64 79 2e 20 20 5e 48 6f 77 * already. ^How
12e70 65 76 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20 ever, if SQLite
12e80 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 is compiled with
12e90 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 the [SQLITE_OMI
12ea0 54 5f 41 55 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63 T_AUTOINIT].** c
12eb0 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 ompile-time opti
12ec0 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 61 75 74 on, then the aut
12ed0 6f 6d 61 74 69 63 20 63 61 6c 6c 73 20 74 6f 20 omatic calls to
12ee0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
12ef0 7a 65 28 29 0a 2a 2a 20 61 72 65 20 6f 6d 69 74 ze().** are omit
12f00 74 65 64 20 61 6e 64 20 74 68 65 20 61 70 70 6c ted and the appl
12f10 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 63 61 6c ication must cal
12f20 6c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 l sqlite3_initia
12f30 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c 79 0a lize() directly.
12f40 2a 2a 20 70 72 69 6f 72 20 74 6f 20 75 73 69 6e ** prior to usin
12f50 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51 4c 69 g any other SQLi
12f60 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20 20 46 te interface. F
12f70 6f 72 20 6d 61 78 69 6d 75 6d 20 70 6f 72 74 61 or maximum porta
12f80 62 69 6c 69 74 79 2c 0a 2a 2a 20 69 74 20 69 73 bility,.** it is
12f90 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 recommended tha
12fa0 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 t applications a
12fb0 6c 77 61 79 73 20 69 6e 76 6f 6b 65 20 73 71 6c lways invoke sql
12fc0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
12fd0 29 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 70 72 ).** directly pr
12fe0 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 6e 79 ior to using any
12ff0 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69 6e other SQLite in
13000 74 65 72 66 61 63 65 2e 20 20 46 75 74 75 72 65 terface. Future
13010 20 72 65 6c 65 61 73 65 73 0a 2a 2a 20 6f 66 20 releases.** of
13020 53 51 4c 69 74 65 20 6d 61 79 20 72 65 71 75 69 SQLite may requi
13030 72 65 20 74 68 69 73 2e 20 20 49 6e 20 6f 74 68 re this. In oth
13040 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 62 65 er words, the be
13050 68 61 76 69 6f 72 20 65 78 68 69 62 69 74 65 64 havior exhibited
13060 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 .** when SQLite
13070 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 is compiled with
13080 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 [SQLITE_OMIT_AU
13090 54 4f 49 4e 49 54 5d 20 6d 69 67 68 74 20 62 65 TOINIT] might be
130a0 63 6f 6d 65 20 74 68 65 0a 2a 2a 20 64 65 66 61 come the.** defa
130b0 75 6c 74 20 62 65 68 61 76 69 6f 72 20 69 6e 20 ult behavior in
130c0 73 6f 6d 65 20 66 75 74 75 72 65 20 72 65 6c 65 some future rele
130d0 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a ase of SQLite..*
130e0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
130f0 5f 6f 73 5f 69 6e 69 74 28 29 20 72 6f 75 74 69 _os_init() routi
13100 6e 65 20 64 6f 65 73 20 6f 70 65 72 61 74 69 6e ne does operatin
13110 67 2d 73 79 73 74 65 6d 20 73 70 65 63 69 66 69 g-system specifi
13120 63 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74 c.** initializat
13130 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 74 ion of the SQLit
13140 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 e library. The
13150 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 sqlite3_os_end()
13160 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 75 6e 64 6f .** routine undo
13170 65 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 es the effect of
13180 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
13190 28 29 2e 20 20 54 79 70 69 63 61 6c 20 74 61 73 (). Typical tas
131a0 6b 73 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 ks.** performed
131b0 62 79 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 by these routine
131c0 73 20 69 6e 63 6c 75 64 65 20 61 6c 6c 6f 63 61 s include alloca
131d0 74 69 6f 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 tion or dealloca
131e0 74 69 6f 6e 0a 2a 2a 20 6f 66 20 73 74 61 74 69 tion.** of stati
131f0 63 20 72 65 73 6f 75 72 63 65 73 2c 20 69 6e 69 c resources, ini
13200 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 67 tialization of g
13210 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2c lobal variables,
13220 0a 2a 2a 20 73 65 74 74 69 6e 67 20 75 70 20 61 .** setting up a
13230 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 default [sqlite
13240 33 5f 76 66 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 3_vfs] module, o
13250 72 20 73 65 74 74 69 6e 67 20 75 70 0a 2a 2a 20 r setting up.**
13260 61 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 a default config
13270 75 72 61 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73 uration using [s
13280 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d qlite3_config()]
13290 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c ..**.** The appl
132a0 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e ication should n
132b0 65 76 65 72 20 69 6e 76 6f 6b 65 20 65 69 74 68 ever invoke eith
132c0 65 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e er sqlite3_os_in
132d0 69 74 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 it().** or sqlit
132e0 65 33 5f 6f 73 5f 65 6e 64 28 29 20 64 69 72 65 e3_os_end() dire
132f0 63 74 6c 79 2e 20 20 54 68 65 20 61 70 70 6c 69 ctly. The appli
13300 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e cation should on
13310 6c 79 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c ly invoke.** sql
13320 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
13330 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 68 ) and sqlite3_sh
13340 75 74 64 6f 77 6e 28 29 2e 20 20 54 68 65 20 73 utdown(). The s
13350 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 qlite3_os_init()
13360 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 .** interface is
13370 20 63 61 6c 6c 65 64 20 61 75 74 6f 6d 61 74 69 called automati
13380 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33 cally by sqlite3
13390 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e _initialize() an
133a0 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f d.** sqlite3_os_
133b0 65 6e 64 28 29 20 69 73 20 63 61 6c 6c 65 64 20 end() is called
133c0 62 79 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 by sqlite3_shutd
133d0 6f 77 6e 28 29 2e 20 20 41 70 70 72 6f 70 72 69 own(). Appropri
133e0 61 74 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 ate.** implement
133f0 61 74 69 6f 6e 73 20 66 6f 72 20 73 71 6c 69 74 ations for sqlit
13400 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 e3_os_init() and
13410 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
13420 29 0a 2a 2a 20 61 72 65 20 62 75 69 6c 74 20 69 ).** are built i
13430 6e 74 6f 20 53 51 4c 69 74 65 20 77 68 65 6e 20 nto SQLite when
13440 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 66 it is compiled f
13450 6f 72 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 or Unix, Windows
13460 2c 20 6f 72 20 4f 53 2f 32 2e 0a 2a 2a 20 57 68 , or OS/2..** Wh
13470 65 6e 20 5b 63 75 73 74 6f 6d 20 62 75 69 6c 64 en [custom build
13480 73 20 7c 20 62 75 69 6c 74 20 66 6f 72 20 6f 74 s | built for ot
13490 68 65 72 20 70 6c 61 74 66 6f 72 6d 73 5d 0a 2a her platforms].*
134a0 2a 20 28 75 73 69 6e 67 20 74 68 65 20 5b 53 51 * (using the [SQ
134b0 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 31 5d LITE_OS_OTHER=1]
134c0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a compile-time.**
134d0 20 6f 70 74 69 6f 6e 29 20 74 68 65 20 61 70 70 option) the app
134e0 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 75 lication must su
134f0 70 70 6c 79 20 61 20 73 75 69 74 61 62 6c 65 20 pply a suitable
13500 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 implementation f
13510 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 or.** sqlite3_os
13520 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69 _init() and sqli
13530 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 20 41 te3_os_end(). A
13540 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 n application-su
13550 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d pplied.** implem
13560 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 entation of sqli
13570 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 6f 72 te3_os_init() or
13580 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
13590 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74 75 72 6e ).** must return
135a0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 [SQLITE_OK] on
135b0 73 75 63 63 65 73 73 20 61 6e 64 20 73 6f 6d 65 success and some
135c0 20 6f 74 68 65 72 20 5b 65 72 72 6f 72 20 63 6f other [error co
135d0 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 69 6c de] upon.** fail
135e0 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ure..*/.SQLITE_A
135f0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
13600 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b nitialize(void);
13610 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
13620 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
13630 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 (void);.SQLITE_A
13640 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
13650 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 s_init(void);.SQ
13660 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
13670 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 ite3_os_end(void
13680 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
13690 45 46 3a 20 43 6f 6e 66 69 67 75 72 69 6e 67 20 EF: Configuring
136a0 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62 72 61 The SQLite Libra
136b0 72 79 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c ry.**.** The sql
136c0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 6e ite3_config() in
136d0 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 terface is used
136e0 74 6f 20 6d 61 6b 65 20 67 6c 6f 62 61 6c 20 63 to make global c
136f0 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 onfiguration.**
13700 63 68 61 6e 67 65 73 20 74 6f 20 53 51 4c 69 74 changes to SQLit
13710 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74 75 e in order to tu
13720 6e 65 20 53 51 4c 69 74 65 20 74 6f 20 74 68 65 ne SQLite to the
13730 20 73 70 65 63 69 66 69 63 20 6e 65 65 64 73 20 specific needs
13740 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 of.** the applic
13750 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 ation. The defa
13760 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f ult configuratio
13770 6e 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 n is recommended
13780 20 66 6f 72 20 6d 6f 73 74 0a 2a 2a 20 61 70 70 for most.** app
13790 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 73 6f lications and so
137a0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
137b0 20 75 73 75 61 6c 6c 79 20 6e 6f 74 20 6e 65 63 usually not nec
137c0 65 73 73 61 72 79 2e 20 20 49 74 20 69 73 0a 2a essary. It is.*
137d0 2a 20 70 72 6f 76 69 64 65 64 20 74 6f 20 73 75 * provided to su
137e0 70 70 6f 72 74 20 72 61 72 65 20 61 70 70 6c 69 pport rare appli
137f0 63 61 74 69 6f 6e 73 20 77 69 74 68 20 75 6e 75 cations with unu
13800 73 75 61 6c 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a sual needs..**.*
13810 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f * The sqlite3_co
13820 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 nfig() interface
13830 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 is not threadsa
13840 66 65 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61 fe. The applica
13850 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 73 tion.** must ins
13860 75 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 ure that no othe
13870 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 r SQLite interfa
13880 63 65 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 ces are invoked
13890 62 79 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 by other.** thre
138a0 61 64 73 20 77 68 69 6c 65 20 73 71 6c 69 74 65 ads while sqlite
138b0 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 72 75 3_config() is ru
138c0 6e 6e 69 6e 67 2e 20 20 46 75 72 74 68 65 72 6d nning. Furtherm
138d0 6f 72 65 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e ore, sqlite3_con
138e0 66 69 67 28 29 0a 2a 2a 20 6d 61 79 20 6f 6e 6c fig().** may onl
138f0 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 69 y be invoked pri
13900 6f 72 20 74 6f 20 6c 69 62 72 61 72 79 20 69 6e or to library in
13910 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 73 69 itialization usi
13920 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 ng.** [sqlite3_i
13930 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 6f 72 20 nitialize()] or
13940 61 66 74 65 72 20 73 68 75 74 64 6f 77 6e 20 62 after shutdown b
13950 79 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 y [sqlite3_shutd
13960 6f 77 6e 28 29 5d 2e 0a 2a 2a 20 5e 49 66 20 73 own()]..** ^If s
13970 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 qlite3_config()
13980 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 is called after
13990 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c [sqlite3_initial
139a0 69 7a 65 28 29 5d 20 61 6e 64 20 62 65 66 6f 72 ize()] and befor
139b0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 68 e.** [sqlite3_sh
139c0 75 74 64 6f 77 6e 28 29 5d 20 74 68 65 6e 20 69 utdown()] then i
139d0 74 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 t will return SQ
139e0 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 20 LITE_MISUSE..**
139f0 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74 Note, however, t
13a00 68 61 74 20 5e 73 71 6c 69 74 65 33 5f 63 6f 6e hat ^sqlite3_con
13a10 66 69 67 28 29 20 63 61 6e 20 62 65 20 63 61 6c fig() can be cal
13a20 6c 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 led as part of t
13a30 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 he.** implementa
13a40 74 69 6f 6e 20 6f 66 20 61 6e 20 61 70 70 6c 69 tion of an appli
13a50 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 5b cation-defined [
13a60 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 sqlite3_os_init(
13a70 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 )]..**.** The fi
13a80 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
13a90 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
13aa0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a is an integer.*
13ab0 2a 20 5b 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e * [configuration
13ac0 20 6f 70 74 69 6f 6e 5d 20 74 68 61 74 20 64 65 option] that de
13ad0 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68 61 74 termines.** what
13ae0 20 70 72 6f 70 65 72 74 79 20 6f 66 20 53 51 4c property of SQL
13af0 69 74 65 20 69 73 20 74 6f 20 62 65 20 63 6f 6e ite is to be con
13b00 66 69 67 75 72 65 64 2e 20 20 53 75 62 73 65 71 figured. Subseq
13b10 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 0a 2a uent arguments.*
13b20 2a 20 76 61 72 79 20 64 65 70 65 6e 64 69 6e 67 * vary depending
13b30 20 6f 6e 20 74 68 65 20 5b 63 6f 6e 66 69 67 75 on the [configu
13b40 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a ration option].*
13b50 2a 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 * in the first a
13b60 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 5e rgument..**.** ^
13b70 57 68 65 6e 20 61 20 63 6f 6e 66 69 67 75 72 61 When a configura
13b80 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73 tion option is s
13b90 65 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 et, sqlite3_conf
13ba0 69 67 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51 ig() returns [SQ
13bb0 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 5e 49 66 LITE_OK]..** ^If
13bc0 20 74 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 75 the option is u
13bd0 6e 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c 69 74 65 nknown or SQLite
13be0 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 73 65 is unable to se
13bf0 74 20 74 68 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 t the option.**
13c00 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e then this routin
13c10 65 20 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e 2d e returns a non-
13c20 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 zero [error code
13c30 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 ]..*/.SQLITE_API
13c40 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e int sqlite3_con
13c50 66 69 67 28 69 6e 74 2c 20 2e 2e 2e 29 3b 0a 0a fig(int, ...);..
13c60 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
13c70 43 6f 6e 66 69 67 75 72 65 20 64 61 74 61 62 61 Configure databa
13c80 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a se connections.*
13c90 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
13ca0 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 _db_config() int
13cb0 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 erface is used t
13cc0 6f 20 6d 61 6b 65 20 63 6f 6e 66 69 67 75 72 61 o make configura
13cd0 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20 tion.** changes
13ce0 74 6f 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 to a [database c
13cf0 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 onnection]. The
13d00 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 73 69 interface is si
13d10 6d 69 6c 61 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c milar to.** [sql
13d20 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 65 ite3_config()] e
13d30 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 63 xcept that the c
13d40 68 61 6e 67 65 73 20 61 70 70 6c 79 20 74 6f 20 hanges apply to
13d50 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 5b 64 61 74 a single.** [dat
13d60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
13d70 5d 20 28 73 70 65 63 69 66 69 65 64 20 69 6e 20 ] (specified in
13d80 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
13d90 6e 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 nt)..**.** The s
13da0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
13db0 6f 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e o sqlite3_db_con
13dc0 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 20 20 69 73 fig(D,V,...) is
13dd0 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f the.** [SQLITE_
13de0 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 DBCONFIG_LOOKASI
13df0 44 45 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 DE | configurati
13e00 6f 6e 20 76 65 72 62 5d 20 2d 20 61 6e 20 69 6e on verb] - an in
13e10 74 65 67 65 72 20 63 6f 64 65 20 0a 2a 2a 20 74 teger code .** t
13e20 68 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 68 hat indicates wh
13e30 61 74 20 61 73 70 65 63 74 20 6f 66 20 74 68 65 at aspect of the
13e40 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
13e50 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 ction] is being
13e60 63 6f 6e 66 69 67 75 72 65 64 2e 0a 2a 2a 20 53 configured..** S
13e70 75 62 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65 ubsequent argume
13e80 6e 74 73 20 76 61 72 79 20 64 65 70 65 6e 64 69 nts vary dependi
13e90 6e 67 20 6f 6e 20 74 68 65 20 63 6f 6e 66 69 67 ng on the config
13ea0 75 72 61 74 69 6f 6e 20 76 65 72 62 2e 0a 2a 2a uration verb..**
13eb0 0a 2a 2a 20 5e 43 61 6c 6c 73 20 74 6f 20 73 71 .** ^Calls to sq
13ec0 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 lite3_db_config(
13ed0 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
13ee0 4f 4b 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 OK if and only i
13ef0 66 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 69 73 f.** the call is
13f00 20 63 6f 6e 73 69 64 65 72 65 64 20 73 75 63 63 considered succ
13f10 65 73 73 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 essful..*/.SQLIT
13f20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
13f30 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 3_db_config(sqli
13f40 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e te3*, int op, ..
13f50 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .);../*.** CAPI3
13f60 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f REF: Memory Allo
13f70 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 0a cation Routines.
13f80 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 **.** An instanc
13f90 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
13fa0 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 defines the int
13fb0 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 20 53 erface between S
13fc0 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c 6f 77 QLite.** and low
13fd0 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
13fe0 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 location routine
13ff0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 s..**.** This ob
14000 6a 65 63 74 20 69 73 20 75 73 65 64 20 69 6e 20 ject is used in
14010 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65 20 69 only one place i
14020 6e 20 74 68 65 20 53 51 4c 69 74 65 20 69 6e 74 n the SQLite int
14030 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70 6f 69 erface..** A poi
14040 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 nter to an insta
14050 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
14060 63 74 20 69 73 20 74 68 65 20 61 72 67 75 6d 65 ct is the argume
14070 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 nt to.** [sqlite
14080 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 65 6e 3_config()] when
14090 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 the configurati
140a0 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a 20 on option is.**
140b0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
140c0 41 4c 4c 4f 43 5d 20 6f 72 20 5b 53 51 4c 49 54 ALLOC] or [SQLIT
140d0 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c E_CONFIG_GETMALL
140e0 4f 43 5d 2e 20 20 0a 2a 2a 20 42 79 20 63 72 65 OC]. .** By cre
140f0 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e 63 ating an instanc
14100 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
14110 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 20 .** and passing
14120 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 it to [sqlite3_c
14130 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 onfig]([SQLITE_C
14140 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 29 0a 2a ONFIG_MALLOC]).*
14150 2a 20 64 75 72 69 6e 67 20 63 6f 6e 66 69 67 75 * during configu
14160 72 61 74 69 6f 6e 2c 20 61 6e 20 61 70 70 6c 69 ration, an appli
14170 63 61 74 69 6f 6e 20 63 61 6e 20 73 70 65 63 69 cation can speci
14180 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 fy an alternativ
14190 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f e.** memory allo
141a0 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
141b0 20 66 6f 72 20 53 51 4c 69 74 65 20 74 6f 20 75 for SQLite to u
141c0 73 65 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 se for all of it
141d0 73 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 65 6d s.** dynamic mem
141e0 6f 72 79 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a ory needs..**.**
141f0 20 4e 6f 74 65 20 74 68 61 74 20 53 51 4c 69 74 Note that SQLit
14200 65 20 63 6f 6d 65 73 20 77 69 74 68 20 73 65 76 e comes with sev
14210 65 72 61 6c 20 5b 62 75 69 6c 74 2d 69 6e 20 6d eral [built-in m
14220 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73 emory allocators
14230 5d 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 70 65 ].** that are pe
14240 72 66 65 63 74 6c 79 20 61 64 65 71 75 61 74 65 rfectly adequate
14250 20 66 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65 for the overwhe
14260 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f lming majority o
14270 66 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a f applications.*
14280 2a 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 * and that this
14290 6f 62 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 object is only u
142a0 73 65 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 seful to a tiny
142b0 6d 69 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c minority of appl
142c0 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 ications.** with
142d0 20 73 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d specialized mem
142e0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
142f0 65 71 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 equirements. Th
14300 69 73 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 is object is.**
14310 61 6c 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67 also used during
14320 20 74 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 testing of SQLi
14330 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 te in order to s
14340 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e pecify an altern
14350 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 ative.** memory
14360 61 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73 allocator that s
14370 69 6d 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 imulates memory
14380 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f out-of-memory co
14390 6e 64 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f nditions in.** o
143a0 72 64 65 72 20 74 6f 20 76 65 72 69 66 79 20 74 rder to verify t
143b0 68 61 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76 hat SQLite recov
143c0 65 72 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66 ers gracefully f
143d0 72 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 rom such.** cond
143e0 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 itions..**.** Th
143f0 65 20 78 4d 61 6c 6c 6f 63 2c 20 78 52 65 61 6c e xMalloc, xReal
14400 6c 6f 63 2c 20 61 6e 64 20 78 46 72 65 65 20 6d loc, and xFree m
14410 65 74 68 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b ethods must work
14420 20 6c 69 6b 65 20 74 68 65 0a 2a 2a 20 6d 61 6c like the.** mal
14430 6c 6f 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 loc(), realloc()
14440 20 61 6e 64 20 66 72 65 65 28 29 20 66 75 6e 63 and free() func
14450 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73 tions from the s
14460 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 tandard C librar
14470 79 2e 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 67 75 y..** ^SQLite gu
14480 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 arantees that th
14490 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
144a0 74 20 74 6f 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 t to.** xRealloc
144b0 20 69 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c is always a val
144c0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 ue returned by a
144d0 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 78 prior call to x
144e0 52 6f 75 6e 64 75 70 2e 0a 2a 2a 0a 2a 2a 20 78 Roundup..**.** x
144f0 53 69 7a 65 20 73 68 6f 75 6c 64 20 72 65 74 75 Size should retu
14500 72 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 rn the allocated
14510 20 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 size of a memor
14520 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 y allocation.**
14530 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 previously obtai
14540 6e 65 64 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 ned from xMalloc
14550 20 6f 72 20 78 52 65 61 6c 6c 6f 63 2e 20 20 54 or xRealloc. T
14560 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a he allocated siz
14570 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 e.** is always a
14580 74 20 6c 65 61 73 74 20 61 73 20 62 69 67 20 61 t least as big a
14590 73 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 s the requested
145a0 73 69 7a 65 20 62 75 74 20 6d 61 79 20 62 65 20 size but may be
145b0 6c 61 72 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 larger..**.** Th
145c0 65 20 78 52 6f 75 6e 64 75 70 20 6d 65 74 68 6f e xRoundup metho
145d0 64 20 72 65 74 75 72 6e 73 20 77 68 61 74 20 77 d returns what w
145e0 6f 75 6c 64 20 62 65 20 74 68 65 20 61 6c 6c 6f ould be the allo
145f0 63 61 74 65 64 20 73 69 7a 65 20 6f 66 0a 2a 2a cated size of.**
14600 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
14610 74 69 6f 6e 20 67 69 76 65 6e 20 61 20 70 61 72 tion given a par
14620 74 69 63 75 6c 61 72 20 72 65 71 75 65 73 74 65 ticular requeste
14630 64 20 73 69 7a 65 2e 20 20 4d 6f 73 74 20 6d 65 d size. Most me
14640 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f mory.** allocato
14650 72 73 20 72 6f 75 6e 64 20 75 70 20 6d 65 6d 6f rs round up memo
14660 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 ry allocations a
14670 74 20 6c 65 61 73 74 20 74 6f 20 74 68 65 20 6e t least to the n
14680 65 78 74 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 ext multiple.**
14690 6f 66 20 38 2e 20 20 53 6f 6d 65 20 61 6c 6c 6f of 8. Some allo
146a0 63 61 74 6f 72 73 20 72 6f 75 6e 64 20 75 70 20 cators round up
146b0 74 6f 20 61 20 6c 61 72 67 65 72 20 6d 75 6c 74 to a larger mult
146c0 69 70 6c 65 20 6f 72 20 74 6f 20 61 20 70 6f 77 iple or to a pow
146d0 65 72 20 6f 66 20 32 2e 0a 2a 2a 20 45 76 65 72 er of 2..** Ever
146e0 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 y memory allocat
146f0 69 6f 6e 20 72 65 71 75 65 73 74 20 63 6f 6d 69 ion request comi
14700 6e 67 20 69 6e 20 74 68 72 6f 75 67 68 20 5b 73 ng in through [s
14710 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
14720 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f .** or [sqlite3_
14730 72 65 61 6c 6c 6f 63 28 29 5d 20 66 69 72 73 74 realloc()] first
14740 20 63 61 6c 6c 73 20 78 52 6f 75 6e 64 75 70 2e calls xRoundup.
14750 20 20 49 66 20 78 52 6f 75 6e 64 75 70 20 72 65 If xRoundup re
14760 74 75 72 6e 73 20 30 2c 20 0a 2a 2a 20 74 68 61 turns 0, .** tha
14770 74 20 63 61 75 73 65 73 20 74 68 65 20 63 6f 72 t causes the cor
14780 72 65 73 70 6f 6e 64 69 6e 67 20 6d 65 6d 6f 72 responding memor
14790 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 y allocation to
147a0 66 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 fail..**.** The
147b0 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 69 6e 69 xInit method ini
147c0 74 69 61 6c 69 7a 65 73 20 74 68 65 20 6d 65 6d tializes the mem
147d0 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20 ory allocator.
147e0 28 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a (For example,.**
147f0 20 69 74 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 it might alloca
14800 74 65 20 61 6e 79 20 72 65 71 75 69 72 65 20 6d te any require m
14810 75 74 65 78 65 73 20 6f 72 20 69 6e 69 74 69 61 utexes or initia
14820 6c 69 7a 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 lize internal da
14830 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 ta.** structures
14840 2e 20 20 54 68 65 20 78 53 68 75 74 64 6f 77 6e . The xShutdown
14850 20 6d 65 74 68 6f 64 20 69 73 20 69 6e 76 6f 6b method is invok
14860 65 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29 20 ed (indirectly)
14870 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 by.** [sqlite3_s
14880 68 75 74 64 6f 77 6e 28 29 5d 20 61 6e 64 20 73 hutdown()] and s
14890 68 6f 75 6c 64 20 64 65 61 6c 6c 6f 63 61 74 65 hould deallocate
148a0 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 20 61 any resources a
148b0 63 71 75 69 72 65 64 0a 2a 2a 20 62 79 20 78 49 cquired.** by xI
148c0 6e 69 74 2e 20 20 54 68 65 20 70 41 70 70 44 61 nit. The pAppDa
148d0 74 61 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 ta pointer is us
148e0 65 64 20 61 73 20 74 68 65 20 6f 6e 6c 79 20 70 ed as the only p
148f0 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 78 arameter to.** x
14900 49 6e 69 74 20 61 6e 64 20 78 53 68 75 74 64 6f Init and xShutdo
14910 77 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 wn..**.** SQLite
14920 20 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 holds the [SQLI
14930 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
14940 4d 41 53 54 45 52 5d 20 6d 75 74 65 78 20 77 68 MASTER] mutex wh
14950 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73 0a 2a 2a en it invokes.**
14960 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74 68 6f the xInit metho
14970 64 2c 20 73 6f 20 74 68 65 20 78 49 6e 69 74 20 d, so the xInit
14980 6d 65 74 68 6f 64 20 6e 65 65 64 20 6e 6f 74 20 method need not
14990 62 65 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 be threadsafe.
149a0 54 68 65 0a 2a 2a 20 78 53 68 75 74 64 6f 77 6e The.** xShutdown
149b0 20 6d 65 74 68 6f 64 20 69 73 20 6f 6e 6c 79 20 method is only
149c0 63 61 6c 6c 65 64 20 66 72 6f 6d 20 5b 73 71 6c called from [sql
149d0 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d ite3_shutdown()]
149e0 20 73 6f 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e so it does.** n
149f0 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 74 68 ot need to be th
14a00 72 65 61 64 73 61 66 65 20 65 69 74 68 65 72 2e readsafe either.
14a10 20 20 46 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 For all other
14a20 6d 65 74 68 6f 64 73 2c 20 53 51 4c 69 74 65 0a methods, SQLite.
14a30 2a 2a 20 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 ** holds the [SQ
14a40 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
14a50 43 5f 4d 45 4d 5d 20 6d 75 74 65 78 20 61 73 20 C_MEM] mutex as
14a60 6c 6f 6e 67 20 61 73 20 74 68 65 0a 2a 2a 20 5b long as the.** [
14a70 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 SQLITE_CONFIG_ME
14a80 4d 53 54 41 54 55 53 5d 20 63 6f 6e 66 69 67 75 MSTATUS] configu
14a90 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 ration option is
14aa0 20 74 75 72 6e 65 64 20 6f 6e 20 28 77 68 69 63 turned on (whic
14ab0 68 0a 2a 2a 20 69 74 20 69 73 20 62 79 20 64 65 h.** it is by de
14ac0 66 61 75 6c 74 29 20 61 6e 64 20 73 6f 20 74 68 fault) and so th
14ad0 65 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61 75 e methods are au
14ae0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 72 69 tomatically seri
14af0 61 6c 69 7a 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 alized..** Howev
14b00 65 72 2c 20 69 66 20 5b 53 51 4c 49 54 45 5f 43 er, if [SQLITE_C
14b10 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d ONFIG_MEMSTATUS]
14b20 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74 68 is disabled, th
14b30 65 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 en the other.**
14b40 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 62 65 20 methods must be
14b50 74 68 72 65 61 64 73 61 66 65 20 6f 72 20 65 6c threadsafe or el
14b60 73 65 20 6d 61 6b 65 20 74 68 65 69 72 20 6f 77 se make their ow
14b70 6e 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20 66 n arrangements f
14b80 6f 72 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 61 74 or.** serializat
14b90 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 ion..**.** SQLit
14ba0 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 69 6e 76 e will never inv
14bb0 6f 6b 65 20 78 49 6e 69 74 28 29 20 6d 6f 72 65 oke xInit() more
14bc0 20 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 6f than once witho
14bd0 75 74 20 61 6e 20 69 6e 74 65 72 76 65 6e 69 6e ut an intervenin
14be0 67 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 53 68 g.** call to xSh
14bf0 75 74 64 6f 77 6e 28 29 2e 0a 2a 2f 0a 74 79 70 utdown()..*/.typ
14c00 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
14c10 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 te3_mem_methods
14c20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
14c30 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 ods;.struct sqli
14c40 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 te3_mem_methods
14c50 7b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 4d 61 6c {. void *(*xMal
14c60 6c 6f 63 29 28 69 6e 74 29 3b 20 20 20 20 20 20 loc)(int);
14c70 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c /* Memory all
14c80 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e ocation function
14c90 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 */. void (*xFr
14ca0 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 ee)(void*);
14cb0 20 20 20 20 20 2f 2a 20 46 72 65 65 20 61 20 70 /* Free a p
14cc0 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 rior allocation
14cd0 2a 2f 0a 20 20 76 6f 69 64 20 2a 28 2a 78 52 65 */. void *(*xRe
14ce0 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e 74 alloc)(void*,int
14cf0 29 3b 20 20 2f 2a 20 52 65 73 69 7a 65 20 61 6e ); /* Resize an
14d00 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 allocation */.
14d10 20 69 6e 74 20 28 2a 78 53 69 7a 65 29 28 76 6f int (*xSize)(vo
14d20 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 id*);
14d30 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 /* Return the si
14d40 7a 65 20 6f 66 20 61 6e 20 61 6c 6c 6f 63 61 74 ze of an allocat
14d50 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 ion */. int (*x
14d60 52 6f 75 6e 64 75 70 29 28 69 6e 74 29 3b 20 20 Roundup)(int);
14d70 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 /* Round
14d80 20 75 70 20 72 65 71 75 65 73 74 20 73 69 7a 65 up request size
14d90 20 74 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 to allocation s
14da0 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 ize */. int (*x
14db0 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 20 20 20 Init)(void*);
14dc0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 /* Initi
14dd0 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 alize the memory
14de0 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 allocator */.
14df0 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77 6e void (*xShutdown
14e00 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 2f )(void*); /
14e10 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 * Deinitialize t
14e20 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
14e30 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 tor */. void *p
14e40 41 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 AppData;
14e50 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d /* Argum
14e60 65 6e 74 20 74 6f 20 78 49 6e 69 74 28 29 20 61 ent to xInit() a
14e70 6e 64 20 78 53 68 75 74 64 6f 77 6e 28 29 20 2a nd xShutdown() *
14e80 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.};../*.** CAPI
14e90 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 61 74 3REF: Configurat
14ea0 69 6f 6e 20 4f 70 74 69 6f 6e 73 0a 2a 2a 20 4b ion Options.** K
14eb0 45 59 57 4f 52 44 53 3a 20 7b 63 6f 6e 66 69 67 EYWORDS: {config
14ec0 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 7d 0a uration option}.
14ed0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 **.** These cons
14ee0 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 76 tants are the av
14ef0 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20 ailable integer
14f00 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 configuration op
14f10 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61 tions that.** ca
14f20 6e 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 n be passed as t
14f30 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
14f40 74 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 t to the [sqlite
14f50 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 3_config()] inte
14f60 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 rface..**.** New
14f70 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f configuration o
14f80 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 ptions may be ad
14f90 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 ded in future re
14fa0 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 leases of SQLite
14fb0 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f ..** Existing co
14fc0 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 nfiguration opti
14fd0 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 64 69 73 ons might be dis
14fe0 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c continued. Appl
14ff0 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 ications.** shou
15000 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 ld check the ret
15010 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 urn code from [s
15020 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d qlite3_config()]
15030 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
15040 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 at.** the call w
15050 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c orked. The [sql
15060 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 ite3_config()] i
15070 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 nterface will re
15080 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 turn a.** non-ze
15090 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 ro [error code]
150a0 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 if a discontinue
150b0 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 d or unsupported
150c0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f configuration o
150d0 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f ption.** is invo
150e0 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a ked..**.** <dl>.
150f0 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 ** [[SQLITE_CONF
15100 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 5d IG_SINGLETHREAD]
15110 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e ] <dt>SQLITE_CON
15120 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 FIG_SINGLETHREAD
15130 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 </dt>.** <dd>The
15140 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 re are no argume
15150 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 nts to this opti
15160 6f 6e 2e 20 20 5e 54 68 69 73 20 6f 70 74 69 6f on. ^This optio
15170 6e 20 73 65 74 73 20 74 68 65 0a 2a 2a 20 5b 74 n sets the.** [t
15180 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 74 hreading mode] t
15190 6f 20 53 69 6e 67 6c 65 2d 74 68 72 65 61 64 2e o Single-thread.
151a0 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 In other words
151b0 2c 20 69 74 20 64 69 73 61 62 6c 65 73 0a 2a 2a , it disables.**
151c0 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 61 6e all mutexing an
151d0 64 20 70 75 74 73 20 53 51 4c 69 74 65 20 69 6e d puts SQLite in
151e0 74 6f 20 61 20 6d 6f 64 65 20 77 68 65 72 65 20 to a mode where
151f0 69 74 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 it can only be u
15200 73 65 64 0a 2a 2a 20 62 79 20 61 20 73 69 6e 67 sed.** by a sing
15210 6c 65 20 74 68 72 65 61 64 2e 20 20 20 5e 49 66 le thread. ^If
15220 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 SQLite is compi
15230 6c 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 led with.** the
15240 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 [SQLITE_THREADSA
15250 46 45 20 7c 20 53 51 4c 49 54 45 5f 54 48 52 45 FE | SQLITE_THRE
15260 41 44 53 41 46 45 3d 30 5d 20 63 6f 6d 70 69 6c ADSAFE=0] compil
15270 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 74 68 e-time option th
15280 65 6e 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 en.** it is not
15290 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 68 61 6e possible to chan
152a0 67 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e ge the [threadin
152b0 67 20 6d 6f 64 65 5d 20 66 72 6f 6d 20 69 74 73 g mode] from its
152c0 20 64 65 66 61 75 6c 74 0a 2a 2a 20 76 61 6c 75 default.** valu
152d0 65 20 6f 66 20 53 69 6e 67 6c 65 2d 74 68 72 65 e of Single-thre
152e0 61 64 20 61 6e 64 20 73 6f 20 5b 73 71 6c 69 74 ad and so [sqlit
152f0 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 69 6c e3_config()] wil
15300 6c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 5b 53 51 l return .** [SQ
15310 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 63 LITE_ERROR] if c
15320 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 alled with the S
15330 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e QLITE_CONFIG_SIN
15340 47 4c 45 54 48 52 45 41 44 0a 2a 2a 20 63 6f 6e GLETHREAD.** con
15350 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f figuration optio
15360 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b n.</dd>.**.** [[
15370 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 SQLITE_CONFIG_MU
15380 4c 54 49 54 48 52 45 41 44 5d 5d 20 3c 64 74 3e LTITHREAD]] <dt>
15390 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 SQLITE_CONFIG_MU
153a0 4c 54 49 54 48 52 45 41 44 3c 2f 64 74 3e 0a 2a LTITHREAD</dt>.*
153b0 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 * <dd>There are
153c0 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 no arguments to
153d0 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 5e 54 this option. ^T
153e0 68 69 73 20 6f 70 74 69 6f 6e 20 73 65 74 73 20 his option sets
153f0 74 68 65 0a 2a 2a 20 5b 74 68 72 65 61 64 69 6e the.** [threadin
15400 67 20 6d 6f 64 65 5d 20 74 6f 20 4d 75 6c 74 69 g mode] to Multi
15410 2d 74 68 72 65 61 64 2e 20 20 49 6e 20 6f 74 68 -thread. In oth
15420 65 72 20 77 6f 72 64 73 2c 20 69 74 20 64 69 73 er words, it dis
15430 61 62 6c 65 73 0a 2a 2a 20 6d 75 74 65 78 69 6e ables.** mutexin
15440 67 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 g on [database c
15450 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b onnection] and [
15460 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
15470 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 nt] objects..**
15480 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 The application
15490 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 is responsible f
154a0 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 or serializing a
154b0 63 63 65 73 73 20 74 6f 0a 2a 2a 20 5b 64 61 74 ccess to.** [dat
154c0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
154d0 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 s] and [prepared
154e0 20 73 74 61 74 65 6d 65 6e 74 73 5d 2e 20 20 42 statements]. B
154f0 75 74 20 6f 74 68 65 72 20 6d 75 74 65 78 65 73 ut other mutexes
15500 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20 .** are enabled
15510 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 so that SQLite w
15520 69 6c 6c 20 62 65 20 73 61 66 65 20 74 6f 20 75 ill be safe to u
15530 73 65 20 69 6e 20 61 20 6d 75 6c 74 69 2d 74 68 se in a multi-th
15540 72 65 61 64 65 64 0a 2a 2a 20 65 6e 76 69 72 6f readed.** enviro
15550 6e 6d 65 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 nment as long as
15560 20 6e 6f 20 74 77 6f 20 74 68 72 65 61 64 73 20 no two threads
15570 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 attempt to use t
15580 68 65 20 73 61 6d 65 0a 2a 2a 20 5b 64 61 74 61 he same.** [data
15590 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
155a0 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
155b0 65 2e 20 20 5e 49 66 20 53 51 4c 69 74 65 20 69 e. ^If SQLite i
155c0 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a s compiled with.
155d0 2a 2a 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 ** the [SQLITE_T
155e0 48 52 45 41 44 53 41 46 45 20 7c 20 53 51 4c 49 HREADSAFE | SQLI
155f0 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 30 5d TE_THREADSAFE=0]
15600 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 compile-time op
15610 74 69 6f 6e 20 74 68 65 6e 0a 2a 2a 20 69 74 20 tion then.** it
15620 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
15630 74 6f 20 73 65 74 20 74 68 65 20 4d 75 6c 74 69 to set the Multi
15640 2d 74 68 72 65 61 64 20 5b 74 68 72 65 61 64 69 -thread [threadi
15650 6e 67 20 6d 6f 64 65 5d 20 61 6e 64 0a 2a 2a 20 ng mode] and.**
15660 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 [sqlite3_config(
15670 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b )] will return [
15680 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 SQLITE_ERROR] if
15690 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65 called with the
156a0 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 .** SQLITE_CONFI
156b0 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 20 63 6f G_MULTITHREAD co
156c0 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 nfiguration opti
156d0 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b on.</dd>.**.** [
156e0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 [SQLITE_CONFIG_S
156f0 45 52 49 41 4c 49 5a 45 44 5d 5d 20 3c 64 74 3e ERIALIZED]] <dt>
15700 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 SQLITE_CONFIG_SE
15710 52 49 41 4c 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a RIALIZED</dt>.**
15720 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e <dd>There are n
15730 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 o arguments to t
15740 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 5e 54 68 his option. ^Th
15750 69 73 20 6f 70 74 69 6f 6e 20 73 65 74 73 20 74 is option sets t
15760 68 65 0a 2a 2a 20 5b 74 68 72 65 61 64 69 6e 67 he.** [threading
15770 20 6d 6f 64 65 5d 20 74 6f 20 53 65 72 69 61 6c mode] to Serial
15780 69 7a 65 64 2e 20 49 6e 20 6f 74 68 65 72 20 77 ized. In other w
15790 6f 72 64 73 2c 20 74 68 69 73 20 6f 70 74 69 6f ords, this optio
157a0 6e 20 65 6e 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c n enables.** all
157b0 20 6d 75 74 65 78 65 73 20 69 6e 63 6c 75 64 69 mutexes includi
157c0 6e 67 20 74 68 65 20 72 65 63 75 72 73 69 76 65 ng the recursive
157d0 0a 2a 2a 20 6d 75 74 65 78 65 73 20 6f 6e 20 5b .** mutexes on [
157e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
157f0 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 ion] and [prepar
15800 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 ed statement] ob
15810 6a 65 63 74 73 2e 0a 2a 2a 20 49 6e 20 74 68 69 jects..** In thi
15820 73 20 6d 6f 64 65 20 28 77 68 69 63 68 20 69 73 s mode (which is
15830 20 74 68 65 20 64 65 66 61 75 6c 74 20 77 68 65 the default whe
15840 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 n SQLite is comp
15850 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 5b 53 51 iled with.** [SQ
15860 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d LITE_THREADSAFE=
15870 31 5d 29 20 74 68 65 20 53 51 4c 69 74 65 20 6c 1]) the SQLite l
15880 69 62 72 61 72 79 20 77 69 6c 6c 20 69 74 73 65 ibrary will itse
15890 6c 66 20 73 65 72 69 61 6c 69 7a 65 20 61 63 63 lf serialize acc
158a0 65 73 73 0a 2a 2a 20 74 6f 20 5b 64 61 74 61 62 ess.** to [datab
158b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d ase connections]
158c0 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73 and [prepared s
158d0 74 61 74 65 6d 65 6e 74 73 5d 20 73 6f 20 74 68 tatements] so th
158e0 61 74 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 at the.** applic
158f0 61 74 69 6f 6e 20 69 73 20 66 72 65 65 20 74 6f ation is free to
15900 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 5b 64 use the same [d
15910 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
15920 6f 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a 20 73 61 on] or the.** sa
15930 6d 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 me [prepared sta
15940 74 65 6d 65 6e 74 5d 20 69 6e 20 64 69 66 66 65 tement] in diffe
15950 72 65 6e 74 20 74 68 72 65 61 64 73 20 61 74 20 rent threads at
15960 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a the same time..*
15970 2a 20 5e 49 66 20 53 51 4c 69 74 65 20 69 73 20 * ^If SQLite is
15980 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a compiled with.**
15990 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 the [SQLITE_THR
159a0 45 41 44 53 41 46 45 20 7c 20 53 51 4c 49 54 45 EADSAFE | SQLITE
159b0 5f 54 48 52 45 41 44 53 41 46 45 3d 30 5d 20 63 _THREADSAFE=0] c
159c0 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 ompile-time opti
159d0 6f 6e 20 74 68 65 6e 0a 2a 2a 20 69 74 20 69 73 on then.** it is
159e0 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
159f0 20 73 65 74 20 74 68 65 20 53 65 72 69 61 6c 69 set the Seriali
15a00 7a 65 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d zed [threading m
15a10 6f 64 65 5d 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c ode] and.** [sql
15a20 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 ite3_config()] w
15a30 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 ill return [SQLI
15a40 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 63 61 6c TE_ERROR] if cal
15a50 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 led with the.**
15a60 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 SQLITE_CONFIG_SE
15a70 52 49 41 4c 49 5a 45 44 20 63 6f 6e 66 69 67 75 RIALIZED configu
15a80 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 2e 3c 2f ration option.</
15a90 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 dd>.**.** [[SQLI
15aa0 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 TE_CONFIG_MALLOC
15ab0 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f ]] <dt>SQLITE_CO
15ac0 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e NFIG_MALLOC</dt>
15ad0 0a 2a 2a 20 3c 64 64 3e 20 5e 28 54 68 69 73 20 .** <dd> ^(This
15ae0 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 option takes a s
15af0 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 ingle argument w
15b00 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 hich is a pointe
15b10 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 r to an.** insta
15b20 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 nce of the [sqli
15b30 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d te3_mem_methods]
15b40 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 structure. The
15b50 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66 argument specif
15b60 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 ies.** alternati
15b70 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d ve low-level mem
15b80 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
15b90 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 73 outines to be us
15ba0 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a ed in place of.*
15bb0 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c * the memory all
15bc0 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 ocation routines
15bd0 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 built into SQLi
15be0 74 65 2e 29 5e 20 5e 53 51 4c 69 74 65 20 6d 61 te.)^ ^SQLite ma
15bf0 6b 65 73 0a 2a 2a 20 69 74 73 20 6f 77 6e 20 70 kes.** its own p
15c00 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 74 rivate copy of t
15c10 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 he content of th
15c20 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d e [sqlite3_mem_m
15c30 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 ethods] structur
15c40 65 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 e.** before the
15c50 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 [sqlite3_config(
15c60 29 5d 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e )] call returns.
15c70 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 </dd>.**.** [[SQ
15c80 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d LITE_CONFIG_GETM
15c90 41 4c 4c 4f 43 5d 5d 20 3c 64 74 3e 53 51 4c 49 ALLOC]] <dt>SQLI
15ca0 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c TE_CONFIG_GETMAL
15cb0 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e LOC</dt>.** <dd>
15cc0 20 5e 28 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 ^(This option t
15cd0 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 akes a single ar
15ce0 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 gument which is
15cf0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a a pointer to an.
15d00 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ** instance of t
15d10 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f he [sqlite3_mem_
15d20 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75 methods] structu
15d30 72 65 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 re. The [sqlite
15d40 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a 2a 3_mem_methods].*
15d50 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 66 * structure is f
15d60 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 illed with the c
15d70 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 urrently defined
15d80 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
15d90 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 29 5e 0a 2a on routines.)^.*
15da0 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 * This option ca
15db0 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65 n be used to ove
15dc0 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c rload the defaul
15dd0 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 t memory allocat
15de0 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 ion.** routines
15df0 77 69 74 68 20 61 20 77 72 61 70 70 65 72 20 74 with a wrapper t
15e00 68 61 74 20 73 69 6d 75 6c 61 74 69 6f 6e 73 20 hat simulations
15e10 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
15e20 6e 20 66 61 69 6c 75 72 65 20 6f 72 0a 2a 2a 20 n failure or.**
15e30 74 72 61 63 6b 73 20 6d 65 6d 6f 72 79 20 75 73 tracks memory us
15e40 61 67 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 age, for example
15e50 2e 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b . </dd>.**.** [[
15e60 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 SQLITE_CONFIG_ME
15e70 4d 53 54 41 54 55 53 5d 5d 20 3c 64 74 3e 53 51 MSTATUS]] <dt>SQ
15e80 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 LITE_CONFIG_MEMS
15e90 54 41 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 TATUS</dt>.** <d
15ea0 64 3e 20 5e 54 68 69 73 20 6f 70 74 69 6f 6e 20 d> ^This option
15eb0 74 61 6b 65 73 20 73 69 6e 67 6c 65 20 61 72 67 takes single arg
15ec0 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 69 6e ument of type in
15ed0 74 2c 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 t, interpreted a
15ee0 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c s a .** boolean,
15ef0 20 77 68 69 63 68 20 65 6e 61 62 6c 65 73 20 6f which enables o
15f00 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 r disables the c
15f10 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 6d 65 6d ollection of mem
15f20 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a ory allocation .
15f30 2a 2a 20 73 74 61 74 69 73 74 69 63 73 2e 20 5e ** statistics. ^
15f40 28 57 68 65 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c (When memory all
15f50 6f 63 61 74 69 6f 6e 20 73 74 61 74 69 73 74 69 ocation statisti
15f60 63 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 2c cs are disabled,
15f70 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 the .** followi
15f80 6e 67 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 ng SQLite interf
15f90 61 63 65 73 20 62 65 63 6f 6d 65 20 6e 6f 6e 2d aces become non-
15fa0 6f 70 65 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a 20 operational:.**
15fb0 20 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e <ul>.** <li>
15fc0 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 [sqlite3_memory
15fd0 5f 75 73 65 64 28 29 5d 0a 2a 2a 20 20 20 3c 6c _used()].** <l
15fe0 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f i> [sqlite3_memo
15ff0 72 79 5f 68 69 67 68 77 61 74 65 72 28 29 5d 0a ry_highwater()].
16000 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 ** <li> [sqlit
16010 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d e3_soft_heap_lim
16020 69 74 36 34 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 it64()].** <li
16030 3e 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 > [sqlite3_statu
16040 73 28 29 5d 0a 2a 2a 20 20 20 3c 2f 75 6c 3e 29 s()].** </ul>)
16050 5e 0a 2a 2a 20 5e 4d 65 6d 6f 72 79 20 61 6c 6c ^.** ^Memory all
16060 6f 63 61 74 69 6f 6e 20 73 74 61 74 69 73 74 69 ocation statisti
16070 63 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 62 cs are enabled b
16080 79 20 64 65 66 61 75 6c 74 20 75 6e 6c 65 73 73 y default unless
16090 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 63 6f SQLite is.** co
160a0 6d 70 69 6c 65 64 20 77 69 74 68 20 5b 53 51 4c mpiled with [SQL
160b0 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 ITE_DEFAULT_MEMS
160c0 54 41 54 55 53 5d 3d 30 20 69 6e 20 77 68 69 63 TATUS]=0 in whic
160d0 68 20 63 61 73 65 20 6d 65 6d 6f 72 79 0a 2a 2a h case memory.**
160e0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 74 61 74 allocation stat
160f0 69 73 74 69 63 73 20 61 72 65 20 64 69 73 61 62 istics are disab
16100 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 0a led by default..
16110 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b ** </dd>.**.** [
16120 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 [SQLITE_CONFIG_S
16130 43 52 41 54 43 48 5d 5d 20 3c 64 74 3e 53 51 4c CRATCH]] <dt>SQL
16140 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 ITE_CONFIG_SCRAT
16150 43 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 20 CH</dt>.** <dd>
16160 5e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 ^This option spe
16170 63 69 66 69 65 73 20 61 20 73 74 61 74 69 63 20 cifies a static
16180 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68 memory buffer th
16190 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73 at SQLite can us
161a0 65 20 66 6f 72 0a 2a 2a 20 73 63 72 61 74 63 68 e for.** scratch
161b0 20 6d 65 6d 6f 72 79 2e 20 20 54 68 65 72 65 20 memory. There
161c0 61 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 are three argume
161d0 6e 74 73 3a 20 20 41 20 70 6f 69 6e 74 65 72 20 nts: A pointer
161e0 61 6e 20 38 2d 62 79 74 65 0a 2a 2a 20 61 6c 69 an 8-byte.** ali
161f0 67 6e 65 64 20 6d 65 6d 6f 72 79 20 62 75 66 66 gned memory buff
16200 65 72 20 66 72 6f 6d 20 77 68 69 63 68 20 74 68 er from which th
16210 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 e scratch alloca
16220 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65 0a 2a 2a tions will be.**
16230 20 64 72 61 77 6e 2c 20 74 68 65 20 73 69 7a 65 drawn, the size
16240 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 68 of each scratch
16250 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 73 7a 29 allocation (sz)
16260 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6d 61 78 ,.** and the max
16270 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 73 imum number of s
16280 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f cratch allocatio
16290 6e 73 20 28 4e 29 2e 20 20 54 68 65 20 73 7a 0a ns (N). The sz.
162a0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 ** argument must
162b0 20 62 65 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f be a multiple o
162c0 66 20 31 36 2e 0a 2a 2a 20 54 68 65 20 66 69 72 f 16..** The fir
162d0 73 74 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 st argument must
162e0 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f be a pointer to
162f0 20 61 6e 20 38 2d 62 79 74 65 20 61 6c 69 67 6e an 8-byte align
16300 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 6f 66 20 ed buffer.** of
16310 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79 at least sz*N by
16320 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a tes of memory..*
16330 2a 20 5e 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 * ^SQLite will u
16340 73 65 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 se no more than
16350 74 77 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 two scratch buff
16360 65 72 73 20 70 65 72 20 74 68 72 65 61 64 2e 20 ers per thread.
16370 20 53 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c 64 20 So.** N should
16380 62 65 20 73 65 74 20 74 6f 20 74 77 69 63 65 20 be set to twice
16390 74 68 65 20 65 78 70 65 63 74 65 64 20 6d 61 78 the expected max
163a0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 imum number of t
163b0 68 72 65 61 64 73 2e 0a 2a 2a 20 5e 53 51 4c 69 hreads..** ^SQLi
163c0 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 72 65 te will never re
163d0 71 75 69 72 65 20 61 20 73 63 72 61 74 63 68 20 quire a scratch
163e0 62 75 66 66 65 72 20 74 68 61 74 20 69 73 20 6d buffer that is m
163f0 6f 72 65 20 74 68 61 6e 20 36 0a 2a 2a 20 74 69 ore than 6.** ti
16400 6d 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 mes the database
16410 20 70 61 67 65 20 73 69 7a 65 2e 20 5e 49 66 20 page size. ^If
16420 53 51 4c 69 74 65 20 6e 65 65 64 73 20 6e 65 65 SQLite needs nee
16430 64 73 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a ds additional.**
16440 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 scratch memory
16450 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 70 beyond what is p
16460 72 6f 76 69 64 65 64 20 62 79 20 74 68 69 73 20 rovided by this
16470 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 configuration op
16480 74 69 6f 6e 2c 20 74 68 65 6e 20 0a 2a 2a 20 5b tion, then .** [
16490 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
164a0 5d 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 ] will be used t
164b0 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d o obtain the mem
164c0 6f 72 79 20 6e 65 65 64 65 64 2e 3c 2f 64 64 3e ory needed.</dd>
164d0 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f .**.** [[SQLITE_
164e0 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
164f0 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f ]] <dt>SQLITE_CO
16500 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 3c 2f NFIG_PAGECACHE</
16510 64 74 3e 0a 2a 2a 20 3c 64 64 3e 20 5e 54 68 69 dt>.** <dd> ^Thi
16520 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 s option specifi
16530 65 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f es a static memo
16540 72 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53 ry buffer that S
16550 51 4c 69 74 65 20 63 61 6e 20 75 73 65 20 66 6f QLite can use fo
16560 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 r.** the databas
16570 65 20 70 61 67 65 20 63 61 63 68 65 20 77 69 74 e page cache wit
16580 68 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 h the default pa
16590 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 ge cache impleme
165a0 6e 74 61 74 69 6f 6e 2e 20 20 0a 2a 2a 20 54 68 ntation. .** Th
165b0 69 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e is configuration
165c0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 should not be u
165d0 73 65 64 20 69 66 20 61 6e 20 61 70 70 6c 69 63 sed if an applic
165e0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 20 70 61 67 ation-define pag
165f0 65 0a 2a 2a 20 63 61 63 68 65 20 69 6d 70 6c 65 e.** cache imple
16600 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6c 6f 61 mentation is loa
16610 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 53 51 ded using the SQ
16620 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 LITE_CONFIG_PCAC
16630 48 45 32 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 54 HE2 option..** T
16640 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 61 here are three a
16650 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 rguments to this
16660 20 6f 70 74 69 6f 6e 3a 20 41 20 70 6f 69 6e 74 option: A point
16670 65 72 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69 er to 8-byte ali
16680 67 6e 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 gned.** memory,
16690 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68 the size of each
166a0 20 70 61 67 65 20 62 75 66 66 65 72 20 28 73 7a page buffer (sz
166b0 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 ), and the numbe
166c0 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29 2e 0a r of pages (N)..
166d0 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75 6d 65 ** The sz argume
166e0 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 nt should be the
166f0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 size of the lar
16700 67 65 73 74 20 64 61 74 61 62 61 73 65 20 70 61 gest database pa
16710 67 65 0a 2a 2a 20 28 61 20 70 6f 77 65 72 20 6f ge.** (a power o
16720 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31 f two between 51
16730 32 20 61 6e 64 20 33 32 37 36 38 29 20 70 6c 75 2 and 32768) plu
16740 73 20 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61 s a little extra
16750 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 70 61 67 for each.** pag
16760 65 20 68 65 61 64 65 72 2e 20 20 5e 54 68 65 20 e header. ^The
16770 70 61 67 65 20 68 65 61 64 65 72 20 73 69 7a 65 page header size
16780 20 69 73 20 32 30 20 74 6f 20 34 30 20 62 79 74 is 20 to 40 byt
16790 65 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 0a es depending on.
167a0 2a 2a 20 74 68 65 20 68 6f 73 74 20 61 72 63 68 ** the host arch
167b0 69 74 65 63 74 75 72 65 2e 20 20 5e 49 74 20 69 itecture. ^It i
167c0 73 20 68 61 72 6d 6c 65 73 73 2c 20 61 70 61 72 s harmless, apar
167d0 74 20 66 72 6f 6d 20 74 68 65 20 77 61 73 74 65 t from the waste
167e0 64 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 6f 20 d memory,.** to
167f0 6d 61 6b 65 20 73 7a 20 61 20 6c 69 74 74 6c 65 make sz a little
16800 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 54 68 65 too large. The
16810 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 first.** argume
16820 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 nt should point
16830 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e to an allocation
16840 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a of at least sz*
16850 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 N bytes of memor
16860 79 2e 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 77 69 y..** ^SQLite wi
16870 6c 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 ll use the memor
16880 79 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 y provided by th
16890 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
168a0 20 74 6f 20 73 61 74 69 73 66 79 20 69 74 73 0a to satisfy its.
168b0 2a 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20 ** memory needs
168c0 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 for the first N
168d0 70 61 67 65 73 20 74 68 61 74 20 69 74 20 61 64 pages that it ad
168e0 64 73 20 74 6f 20 63 61 63 68 65 2e 20 20 5e 49 ds to cache. ^I
168f0 66 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 f additional.**
16900 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 page cache memor
16910 79 20 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f y is needed beyo
16920 6e 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 69 nd what is provi
16930 64 65 64 20 62 79 20 74 68 69 73 20 6f 70 74 69 ded by this opti
16940 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 on, then.** SQLi
16950 74 65 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 te goes to [sqli
16960 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f te3_malloc()] fo
16970 72 20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c r the additional
16980 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a storage space..
16990 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 ** The pointer i
169a0 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 n the first argu
169b0 6d 65 6e 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20 ment must.** be
169c0 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d aligned to an 8-
169d0 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 6f 72 byte boundary or
169e0 20 73 75 62 73 65 71 75 65 6e 74 20 62 65 68 61 subsequent beha
169f0 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 0a 2a vior of SQLite.*
16a00 2a 20 77 69 6c 6c 20 62 65 20 75 6e 64 65 66 69 * will be undefi
16a10 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ned.</dd>.**.**
16a20 5b 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [[SQLITE_CONFIG_
16a30 48 45 41 50 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 HEAP]] <dt>SQLIT
16a40 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 3c 2f 64 E_CONFIG_HEAP</d
16a50 74 3e 0a 2a 2a 20 3c 64 64 3e 20 5e 54 68 69 73 t>.** <dd> ^This
16a60 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65 option specifie
16a70 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72 s a static memor
16a80 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53 51 y buffer that SQ
16a90 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a Lite will use.**
16aa0 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 for all of its
16ab0 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 61 dynamic memory a
16ac0 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 20 llocation needs
16ad0 62 65 79 6f 6e 64 20 74 68 6f 73 65 20 70 72 6f beyond those pro
16ae0 76 69 64 65 64 0a 2a 2a 20 66 6f 72 20 62 79 20 vided.** for by
16af0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 [SQLITE_CONFIG_S
16b00 43 52 41 54 43 48 5d 20 61 6e 64 20 5b 53 51 4c CRATCH] and [SQL
16b10 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
16b20 41 43 48 45 5d 2e 0a 2a 2a 20 54 68 65 72 65 20 ACHE]..** There
16b30 61 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 are three argume
16b40 6e 74 73 3a 20 41 6e 20 38 2d 62 79 74 65 20 61 nts: An 8-byte a
16b50 6c 69 67 6e 65 64 20 70 6f 69 6e 74 65 72 20 74 ligned pointer t
16b60 6f 20 74 68 65 20 6d 65 6d 6f 72 79 2c 0a 2a 2a o the memory,.**
16b70 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
16b80 79 74 65 73 20 69 6e 20 74 68 65 20 6d 65 6d 6f ytes in the memo
16b90 72 79 20 62 75 66 66 65 72 2c 20 61 6e 64 20 74 ry buffer, and t
16ba0 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 he minimum alloc
16bb0 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2a 20 5e ation size..** ^
16bc0 49 66 20 74 68 65 20 66 69 72 73 74 20 70 6f 69 If the first poi
16bd0 6e 74 65 72 20 28 74 68 65 20 6d 65 6d 6f 72 79 nter (the memory
16be0 20 70 6f 69 6e 74 65 72 29 20 69 73 20 4e 55 4c pointer) is NUL
16bf0 4c 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 72 L, then SQLite r
16c00 65 76 65 72 74 73 0a 2a 2a 20 74 6f 20 75 73 69 everts.** to usi
16c10 6e 67 20 69 74 73 20 64 65 66 61 75 6c 74 20 6d ng its default m
16c20 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
16c30 28 74 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c (the system mall
16c40 6f 63 28 29 20 69 6d 70 6c 65 6d 65 6e 74 61 74 oc() implementat
16c50 69 6f 6e 29 2c 0a 2a 2a 20 75 6e 64 6f 69 6e 67 ion),.** undoing
16c60 20 61 6e 79 20 70 72 69 6f 72 20 69 6e 76 6f 63 any prior invoc
16c70 61 74 69 6f 6e 20 6f 66 20 5b 53 51 4c 49 54 45 ation of [SQLITE
16c80 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2e _CONFIG_MALLOC].
16c90 20 20 5e 49 66 20 74 68 65 0a 2a 2a 20 6d 65 6d ^If the.** mem
16ca0 6f 72 79 20 70 6f 69 6e 74 65 72 20 69 73 20 6e ory pointer is n
16cb0 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 65 69 74 68 ot NULL and eith
16cc0 65 72 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c er [SQLITE_ENABL
16cd0 45 5f 4d 45 4d 53 59 53 33 5d 20 6f 72 0a 2a 2a E_MEMSYS3] or.**
16ce0 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f [SQLITE_ENABLE_
16cf0 4d 45 4d 53 59 53 35 5d 20 61 72 65 20 64 65 66 MEMSYS5] are def
16d00 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 61 ined, then the a
16d10 6c 74 65 72 6e 61 74 69 76 65 20 6d 65 6d 6f 72 lternative memor
16d20 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 20 69 y.** allocator i
16d30 73 20 65 6e 67 61 67 65 64 20 74 6f 20 68 61 6e s engaged to han
16d40 64 6c 65 20 61 6c 6c 20 6f 66 20 53 51 4c 69 74 dle all of SQLit
16d50 65 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 es memory alloca
16d60 74 69 6f 6e 20 6e 65 65 64 73 2e 0a 2a 2a 20 54 tion needs..** T
16d70 68 65 20 66 69 72 73 74 20 70 6f 69 6e 74 65 72 he first pointer
16d80 20 28 74 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 (the memory poi
16d90 6e 74 65 72 29 20 6d 75 73 74 20 62 65 20 61 6c nter) must be al
16da0 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 igned to an 8-by
16db0 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 20 6f te.** boundary o
16dc0 72 20 73 75 62 73 65 71 75 65 6e 74 20 62 65 68 r subsequent beh
16dd0 61 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 20 avior of SQLite
16de0 77 69 6c 6c 20 62 65 20 75 6e 64 65 66 69 6e 65 will be undefine
16df0 64 2e 0a 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75 d..** The minimu
16e00 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a m allocation siz
16e10 65 20 69 73 20 63 61 70 70 65 64 20 61 74 20 32 e is capped at 2
16e20 2a 2a 31 32 2e 20 52 65 61 73 6f 6e 61 62 6c 65 **12. Reasonable
16e30 20 76 61 6c 75 65 73 0a 2a 2a 20 66 6f 72 20 74 values.** for t
16e40 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 he minimum alloc
16e50 61 74 69 6f 6e 20 73 69 7a 65 20 61 72 65 20 32 ation size are 2
16e60 2a 2a 35 20 74 68 72 6f 75 67 68 20 32 2a 2a 38 **5 through 2**8
16e70 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 .</dd>.**.** [[S
16e80 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 QLITE_CONFIG_MUT
16e90 45 58 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f EX]] <dt>SQLITE_
16ea0 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74 CONFIG_MUTEX</dt
16eb0 3e 0a 2a 2a 20 3c 64 64 3e 20 5e 28 54 68 69 73 >.** <dd> ^(This
16ec0 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 option takes a
16ed0 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 single argument
16ee0 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 which is a point
16ef0 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 er to an.** inst
16f00 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c ance of the [sql
16f10 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
16f20 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 ds] structure.
16f30 54 68 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65 The argument spe
16f40 63 69 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e cifies.** altern
16f50 61 74 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 ative low-level
16f60 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 74 mutex routines t
16f70 6f 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 o be used in pla
16f80 63 65 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 ce.** the mutex
16f90 72 6f 75 74 69 6e 65 73 20 62 75 69 6c 74 20 69 routines built i
16fa0 6e 74 6f 20 53 51 4c 69 74 65 2e 29 5e 20 20 5e nto SQLite.)^ ^
16fb0 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 63 SQLite makes a c
16fc0 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f opy of the.** co
16fd0 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 5b 73 71 ntent of the [sq
16fe0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
16ff0 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65 20 62 ods] structure b
17000 65 66 6f 72 65 20 74 68 65 20 63 61 6c 6c 20 74 efore the call t
17010 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f o.** [sqlite3_co
17020 6e 66 69 67 28 29 5d 20 72 65 74 75 72 6e 73 2e nfig()] returns.
17030 20 5e 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 ^If SQLite is c
17040 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 ompiled with.**
17050 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 the [SQLITE_THRE
17060 41 44 53 41 46 45 20 7c 20 53 51 4c 49 54 45 5f ADSAFE | SQLITE_
17070 54 48 52 45 41 44 53 41 46 45 3d 30 5d 20 63 6f THREADSAFE=0] co
17080 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f mpile-time optio
17090 6e 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 65 6e n then.** the en
170a0 74 69 72 65 20 6d 75 74 65 78 69 6e 67 20 73 75 tire mutexing su
170b0 62 73 79 73 74 65 6d 20 69 73 20 6f 6d 69 74 74 bsystem is omitt
170c0 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c ed from the buil
170d0 64 20 61 6e 64 20 68 65 6e 63 65 20 63 61 6c 6c d and hence call
170e0 73 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 s to.** [sqlite3
170f0 5f 63 6f 6e 66 69 67 28 29 5d 20 77 69 74 68 20 _config()] with
17100 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 the SQLITE_CONFI
17110 47 5f 4d 55 54 45 58 20 63 6f 6e 66 69 67 75 72 G_MUTEX configur
17120 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 77 69 6c ation option wil
17130 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c l.** return [SQL
17140 49 54 45 5f 45 52 52 4f 52 5d 2e 3c 2f 64 64 3e ITE_ERROR].</dd>
17150 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f .**.** [[SQLITE_
17160 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 5d CONFIG_GETMUTEX]
17170 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e ] <dt>SQLITE_CON
17180 46 49 47 5f 47 45 54 4d 55 54 45 58 3c 2f 64 74 FIG_GETMUTEX</dt
17190 3e 0a 2a 2a 20 3c 64 64 3e 20 5e 28 54 68 69 73 >.** <dd> ^(This
171a0 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 option takes a
171b0 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 single argument
171c0 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 which is a point
171d0 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 er to an.** inst
171e0 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c ance of the [sql
171f0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
17200 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 ds] structure.
17210 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f The.** [sqlite3_
17220 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a mutex_methods].*
17230 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 66 * structure is f
17240 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 illed with the c
17250 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 urrently defined
17260 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e mutex routines.
17270 29 5e 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f )^.** This optio
17280 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f n can be used to
17290 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65 overload the de
172a0 66 61 75 6c 74 20 6d 75 74 65 78 20 61 6c 6c 6f fault mutex allo
172b0 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e cation.** routin
172c0 65 73 20 77 69 74 68 20 61 20 77 72 61 70 70 65 es with a wrappe
172d0 72 20 75 73 65 64 20 74 6f 20 74 72 61 63 6b 20 r used to track
172e0 6d 75 74 65 78 20 75 73 61 67 65 20 66 6f 72 20 mutex usage for
172f0 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70 performance.** p
17300 72 6f 66 69 6c 69 6e 67 20 6f 72 20 74 65 73 74 rofiling or test
17310 69 6e 67 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 ing, for example
17320 2e 20 20 20 5e 49 66 20 53 51 4c 69 74 65 20 69 . ^If SQLite i
17330 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a s compiled with.
17340 2a 2a 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 ** the [SQLITE_T
17350 48 52 45 41 44 53 41 46 45 20 7c 20 53 51 4c 49 HREADSAFE | SQLI
17360 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 30 5d TE_THREADSAFE=0]
17370 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 compile-time op
17380 74 69 6f 6e 20 74 68 65 6e 0a 2a 2a 20 74 68 65 tion then.** the
17390 20 65 6e 74 69 72 65 20 6d 75 74 65 78 69 6e 67 entire mutexing
173a0 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 6f 6d subsystem is om
173b0 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 62 itted from the b
173c0 75 69 6c 64 20 61 6e 64 20 68 65 6e 63 65 20 63 uild and hence c
173d0 61 6c 6c 73 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 alls to.** [sqli
173e0 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 69 te3_config()] wi
173f0 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f th the SQLITE_CO
17400 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 20 63 6f NFIG_GETMUTEX co
17410 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 nfiguration opti
17420 6f 6e 20 77 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 on will.** retur
17430 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d n [SQLITE_ERROR]
17440 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 .</dd>.**.** [[S
17450 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f QLITE_CONFIG_LOO
17460 4b 41 53 49 44 45 5d 5d 20 3c 64 74 3e 53 51 4c KASIDE]] <dt>SQL
17470 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 ITE_CONFIG_LOOKA
17480 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 SIDE</dt>.** <dd
17490 3e 20 5e 28 54 68 69 73 20 6f 70 74 69 6f 6e 20 > ^(This option
174a0 74 61 6b 65 73 20 74 77 6f 20 61 72 67 75 6d 65 takes two argume
174b0 6e 74 73 20 74 68 61 74 20 64 65 74 65 72 6d 69 nts that determi
174c0 6e 65 20 74 68 65 20 64 65 66 61 75 6c 74 0a 2a ne the default.*
174d0 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 * memory allocat
174e0 69 6f 6e 20 66 6f 72 20 74 68 65 20 6c 6f 6f 6b ion for the look
174f0 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c aside memory all
17500 6f 63 61 74 6f 72 20 6f 6e 20 65 61 63 68 0a 2a ocator on each.*
17510 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e * [database conn
17520 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69 ection]. The fi
17530 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 rst argument is
17540 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 the.** size of e
17550 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 ach lookaside bu
17560 66 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68 ffer slot and th
17570 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 e second is the
17580 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f number of.** slo
17590 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 ts allocated to
175a0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f each database co
175b0 6e 6e 65 63 74 69 6f 6e 2e 29 5e 20 20 5e 28 54 nnection.)^ ^(T
175c0 68 69 73 20 6f 70 74 69 6f 6e 20 73 65 74 73 20 his option sets
175d0 74 68 65 0a 2a 2a 20 3c 69 3e 64 65 66 61 75 6c the.** <i>defaul
175e0 74 3c 2f 69 3e 20 6c 6f 6f 6b 61 73 69 64 65 20 t</i> lookaside
175f0 73 69 7a 65 2e 20 54 68 65 20 5b 53 51 4c 49 54 size. The [SQLIT
17600 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 E_DBCONFIG_LOOKA
17610 53 49 44 45 5d 0a 2a 2a 20 76 65 72 62 20 74 6f SIDE].** verb to
17620 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e [sqlite3_db_con
17630 66 69 67 28 29 5d 20 63 61 6e 20 62 65 20 75 73 fig()] can be us
17640 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 ed to change the
17650 20 6c 6f 6f 6b 61 73 69 64 65 0a 2a 2a 20 63 6f lookaside.** co
17660 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 6e 20 69 nfiguration on i
17670 6e 64 69 76 69 64 75 61 6c 20 63 6f 6e 6e 65 63 ndividual connec
17680 74 69 6f 6e 73 2e 29 5e 20 3c 2f 64 64 3e 0a 2a tions.)^ </dd>.*
17690 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43 4f *.** [[SQLITE_CO
176a0 4e 46 49 47 5f 50 43 41 43 48 45 32 5d 5d 20 3c NFIG_PCACHE2]] <
176b0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
176c0 5f 50 43 41 43 48 45 32 3c 2f 64 74 3e 0a 2a 2a _PCACHE2</dt>.**
176d0 20 3c 64 64 3e 20 5e 28 54 68 69 73 20 6f 70 74 <dd> ^(This opt
176e0 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 ion takes a sing
176f0 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 le argument whic
17700 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 h is a pointer t
17710 6f 0a 2a 2a 20 61 6e 20 5b 73 71 6c 69 74 65 33 o.** an [sqlite3
17720 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 _pcache_methods2
17730 5d 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 ] object. This
17740 6f 62 6a 65 63 74 20 73 70 65 63 69 66 69 65 73 object specifies
17750 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 0a 2a the interface.*
17760 2a 20 74 6f 20 61 20 63 75 73 74 6f 6d 20 70 61 * to a custom pa
17770 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 ge cache impleme
17780 6e 74 61 74 69 6f 6e 2e 29 5e 20 20 5e 53 51 4c ntation.)^ ^SQL
17790 69 74 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 ite makes a copy
177a0 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 62 6a 65 63 of the.** objec
177b0 74 20 61 6e 64 20 75 73 65 73 20 69 74 20 66 6f t and uses it fo
177c0 72 20 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d r page cache mem
177d0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e ory allocations.
177e0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 </dd>.**.** [[SQ
177f0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 LITE_CONFIG_GETP
17800 43 41 43 48 45 32 5d 5d 20 3c 64 74 3e 53 51 4c CACHE2]] <dt>SQL
17810 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 ITE_CONFIG_GETPC
17820 41 43 48 45 32 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 ACHE2</dt>.** <d
17830 64 3e 20 5e 28 54 68 69 73 20 6f 70 74 69 6f 6e d> ^(This option
17840 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 takes a single
17850 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 argument which i
17860 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
17870 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 63 n.** [sqlite3_pc
17880 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 5d 20 6f ache_methods2] o
17890 62 6a 65 63 74 2e 20 20 53 51 4c 69 74 65 20 63 bject. SQLite c
178a0 6f 70 69 65 73 20 6f 66 20 74 68 65 20 63 75 72 opies of the cur
178b0 72 65 6e 74 0a 2a 2a 20 70 61 67 65 20 63 61 63 rent.** page cac
178c0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
178d0 6e 20 69 6e 74 6f 20 74 68 61 74 20 6f 62 6a 65 n into that obje
178e0 63 74 2e 29 5e 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a ct.)^ </dd>.**.*
178f0 2a 20 5b 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 * [[SQLITE_CONFI
17900 47 5f 4c 4f 47 5d 5d 20 3c 64 74 3e 53 51 4c 49 G_LOG]] <dt>SQLI
17910 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 47 3c 2f 64 TE_CONFIG_LOG</d
17920 74 3e 0a 2a 2a 20 3c 64 64 3e 20 5e 54 68 65 20 t>.** <dd> ^The
17930 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f SQLITE_CONFIG_LO
17940 47 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 G option takes t
17950 77 6f 20 61 72 67 75 6d 65 6e 74 73 3a 20 61 20 wo arguments: a
17960 70 6f 69 6e 74 65 72 20 74 6f 20 61 0a 2a 2a 20 pointer to a.**
17970 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 function with a
17980 63 61 6c 6c 20 73 69 67 6e 61 74 75 72 65 20 6f call signature o
17990 66 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c f void(*)(void*,
179a0 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 int,const char*)
179b0 2c 20 0a 2a 2a 20 61 6e 64 20 61 20 70 6f 69 6e , .** and a poin
179c0 74 65 72 20 74 6f 20 76 6f 69 64 2e 20 5e 49 66 ter to void. ^If
179d0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 6f the function po
179e0 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c inter is not NUL
179f0 4c 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 76 6f L, it is.** invo
17a00 6b 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f ked by [sqlite3_
17a10 6c 6f 67 28 29 5d 20 74 6f 20 70 72 6f 63 65 73 log()] to proces
17a20 73 20 65 61 63 68 20 6c 6f 67 67 69 6e 67 20 65 s each logging e
17a30 76 65 6e 74 2e 20 20 5e 49 66 20 74 68 65 0a 2a vent. ^If the.*
17a40 2a 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 * function point
17a50 65 72 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 er is NULL, the
17a60 5b 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 5d 20 [sqlite3_log()]
17a70 69 6e 74 65 72 66 61 63 65 20 62 65 63 6f 6d 65 interface become
17a80 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20 5e 54 s a no-op..** ^T
17a90 68 65 20 76 6f 69 64 20 70 6f 69 6e 74 65 72 20 he void pointer
17aa0 74 68 61 74 20 69 73 20 74 68 65 20 73 65 63 6f that is the seco
17ab0 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 53 nd argument to S
17ac0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 47 QLITE_CONFIG_LOG
17ad0 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 74 68 is.** passed th
17ae0 72 6f 75 67 68 20 61 73 20 74 68 65 20 66 69 72 rough as the fir
17af0 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 st parameter to
17b00 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
17b10 64 65 66 69 6e 65 64 20 6c 6f 67 67 65 72 0a 2a defined logger.*
17b20 2a 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 6e 65 * function whene
17b30 76 65 72 20 74 68 61 74 20 66 75 6e 63 74 69 6f ver that functio
17b40 6e 20 69 73 20 69 6e 76 6f 6b 65 64 2e 20 20 5e n is invoked. ^
17b50 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d The second param
17b60 65 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 6c eter to.** the l
17b70 6f 67 67 65 72 20 66 75 6e 63 74 69 6f 6e 20 69 ogger function i
17b80 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 s a copy of the
17b90 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
17ba0 74 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e to the correspon
17bb0 64 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ding.** [sqlite3
17bc0 5f 6c 6f 67 28 29 5d 20 63 61 6c 6c 20 61 6e 64 _log()] call and
17bd0 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 is intended to
17be0 62 65 20 61 20 5b 72 65 73 75 6c 74 20 63 6f 64 be a [result cod
17bf0 65 5d 20 6f 72 20 61 6e 0a 2a 2a 20 5b 65 78 74 e] or an.** [ext
17c00 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 ended result cod
17c10 65 5d 2e 20 20 5e 54 68 65 20 74 68 69 72 64 20 e]. ^The third
17c20 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 parameter passed
17c30 20 74 6f 20 74 68 65 20 6c 6f 67 67 65 72 20 69 to the logger i
17c40 73 0a 2a 2a 20 6c 6f 67 20 6d 65 73 73 61 67 65 s.** log message
17c50 20 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e after formattin
17c60 67 20 76 69 61 20 5b 73 71 6c 69 74 65 33 5f 73 g via [sqlite3_s
17c70 6e 70 72 69 6e 74 66 28 29 5d 2e 0a 2a 2a 20 54 nprintf()]..** T
17c80 68 65 20 53 51 4c 69 74 65 20 6c 6f 67 67 69 6e he SQLite loggin
17c90 67 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e g interface is n
17ca0 6f 74 20 72 65 65 6e 74 72 61 6e 74 3b 20 74 68 ot reentrant; th
17cb0 65 20 6c 6f 67 67 65 72 20 66 75 6e 63 74 69 6f e logger functio
17cc0 6e 0a 2a 2a 20 73 75 70 70 6c 69 65 64 20 62 79 n.** supplied by
17cd0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
17ce0 20 6d 75 73 74 20 6e 6f 74 20 69 6e 76 6f 6b 65 must not invoke
17cf0 20 61 6e 79 20 53 51 4c 69 74 65 20 69 6e 74 65 any SQLite inte
17d00 72 66 61 63 65 2e 0a 2a 2a 20 49 6e 20 61 20 6d rface..** In a m
17d10 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 ulti-threaded ap
17d20 70 6c 69 63 61 74 69 6f 6e 2c 20 74 68 65 20 61 plication, the a
17d30 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
17d40 65 64 20 6c 6f 67 67 65 72 0a 2a 2a 20 66 75 6e ed logger.** fun
17d50 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 74 68 ction must be th
17d60 72 65 61 64 73 61 66 65 2e 20 3c 2f 64 64 3e 0a readsafe. </dd>.
17d70 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43 **.** [[SQLITE_C
17d80 4f 4e 46 49 47 5f 55 52 49 5d 5d 20 3c 64 74 3e ONFIG_URI]] <dt>
17d90 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 55 52 SQLITE_CONFIG_UR
17da0 49 0a 2a 2a 20 3c 64 64 3e 20 54 68 69 73 20 6f I.** <dd> This o
17db0 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 ption takes a si
17dc0 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 6f 66 ngle argument of
17dd0 20 74 79 70 65 20 69 6e 74 2e 20 49 66 20 6e 6f type int. If no
17de0 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 0a 2a 2a 20 n-zero, then.**
17df0 55 52 49 20 68 61 6e 64 6c 69 6e 67 20 69 73 20 URI handling is
17e00 67 6c 6f 62 61 6c 6c 79 20 65 6e 61 62 6c 65 64 globally enabled
17e10 2e 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 . If the paramet
17e20 65 72 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e er is zero, then
17e30 20 55 52 49 20 68 61 6e 64 6c 69 6e 67 0a 2a 2a URI handling.**
17e40 20 69 73 20 67 6c 6f 62 61 6c 6c 79 20 64 69 73 is globally dis
17e50 61 62 6c 65 64 2e 20 49 66 20 55 52 49 20 68 61 abled. If URI ha
17e60 6e 64 6c 69 6e 67 20 69 73 20 67 6c 6f 62 61 6c ndling is global
17e70 6c 79 20 65 6e 61 62 6c 65 64 2c 20 61 6c 6c 20 ly enabled, all
17e80 66 69 6c 65 6e 61 6d 65 73 0a 2a 2a 20 70 61 73 filenames.** pas
17e90 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f sed to [sqlite3_
17ea0 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 open()], [sqlite
17eb0 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 5b 73 3_open_v2()], [s
17ec0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d qlite3_open16()]
17ed0 20 6f 72 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 or.** specified
17ee0 20 61 73 20 70 61 72 74 20 6f 66 20 5b 41 54 54 as part of [ATT
17ef0 41 43 48 5d 20 63 6f 6d 6d 61 6e 64 73 20 61 72 ACH] commands ar
17f00 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 e interpreted as
17f10 20 55 52 49 73 2c 20 72 65 67 61 72 64 6c 65 73 URIs, regardles
17f20 73 0a 2a 2a 20 6f 66 20 77 68 65 74 68 65 72 20 s.** of whether
17f30 6f 72 20 6e 6f 74 20 74 68 65 20 5b 53 51 4c 49 or not the [SQLI
17f40 54 45 5f 4f 50 45 4e 5f 55 52 49 5d 20 66 6c 61 TE_OPEN_URI] fla
17f50 67 20 69 73 20 73 65 74 20 77 68 65 6e 20 74 68 g is set when th
17f60 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f e database.** co
17f70 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e nnection is open
17f80 65 64 2e 20 49 66 20 69 74 20 69 73 20 67 6c 6f ed. If it is glo
17f90 62 61 6c 6c 79 20 64 69 73 61 62 6c 65 64 2c 20 bally disabled,
17fa0 66 69 6c 65 6e 61 6d 65 73 20 61 72 65 0a 2a 2a filenames are.**
17fb0 20 6f 6e 6c 79 20 69 6e 74 65 72 70 72 65 74 65 only interprete
17fc0 64 20 61 73 20 55 52 49 73 20 69 66 20 74 68 65 d as URIs if the
17fd0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49 SQLITE_OPEN_URI
17fe0 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 68 65 flag is set whe
17ff0 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 n the.** databas
18000 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
18010 6f 70 65 6e 65 64 2e 20 42 79 20 64 65 66 61 75 opened. By defau
18020 6c 74 2c 20 55 52 49 20 68 61 6e 64 6c 69 6e 67 lt, URI handling
18030 20 69 73 20 67 6c 6f 62 61 6c 6c 79 0a 2a 2a 20 is globally.**
18040 64 69 73 61 62 6c 65 64 2e 20 54 68 65 20 64 65 disabled. The de
18050 66 61 75 6c 74 20 76 61 6c 75 65 20 6d 61 79 20 fault value may
18060 62 65 20 63 68 61 6e 67 65 64 20 62 79 20 63 6f be changed by co
18070 6d 70 69 6c 69 6e 67 20 77 69 74 68 20 74 68 65 mpiling with the
18080 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 53 45 5f .** [SQLITE_USE_
18090 55 52 49 5d 20 73 79 6d 62 6f 6c 20 64 65 66 69 URI] symbol defi
180a0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c ned..**.** [[SQL
180b0 49 54 45 5f 43 4f 4e 46 49 47 5f 43 4f 56 45 52 ITE_CONFIG_COVER
180c0 49 4e 47 5f 49 4e 44 45 58 5f 53 43 41 4e 5d 5d ING_INDEX_SCAN]]
180d0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 <dt>SQLITE_CONF
180e0 49 47 5f 43 4f 56 45 52 49 4e 47 5f 49 4e 44 45 IG_COVERING_INDE
180f0 58 5f 53 43 41 4e 0a 2a 2a 20 3c 64 64 3e 20 54 X_SCAN.** <dd> T
18100 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 73 20 his option taks
18110 61 20 73 69 6e 67 6c 65 20 69 6e 74 65 67 65 72 a single integer
18120 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 argument which
18130 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 is interpreted a
18140 73 0a 2a 2a 20 61 20 62 6f 6f 6c 65 61 6e 20 69 s.** a boolean i
18150 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 61 62 6c n order to enabl
18160 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 e or disable the
18170 20 75 73 65 20 6f 66 20 63 6f 76 65 72 69 6e 67 use of covering
18180 20 69 6e 64 69 63 65 73 20 66 6f 72 0a 2a 2a 20 indices for.**
18190 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 73 full table scans
181a0 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 6f 70 in the query op
181b0 74 69 6d 69 7a 65 72 2e 20 20 54 68 65 20 64 65 timizer. The de
181c0 66 61 75 6c 74 20 73 65 74 74 69 6e 67 20 69 73 fault setting is
181d0 20 64 65 74 65 72 6d 69 6e 65 64 0a 2a 2a 20 62 determined.** b
181e0 79 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 41 4c y the [SQLITE_AL
181f0 4c 4f 57 5f 43 4f 56 45 52 49 4e 47 5f 49 4e 44 LOW_COVERING_IND
18200 45 58 5f 53 43 41 4e 5d 20 63 6f 6d 70 69 6c 65 EX_SCAN] compile
18210 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2c 20 6f 72 -time option, or
18220 20 69 73 20 22 6f 6e 22 0a 2a 2a 20 69 66 20 74 is "on".** if t
18230 68 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 hat compile-time
18240 20 6f 70 74 69 6f 6e 20 69 73 20 6f 6d 69 74 74 option is omitt
18250 65 64 2e 0a 2a 2a 20 54 68 65 20 61 62 69 6c 69 ed..** The abili
18260 74 79 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 ty to disable th
18270 65 20 75 73 65 20 6f 66 20 63 6f 76 65 72 69 6e e use of coverin
18280 67 20 69 6e 64 69 63 65 73 20 66 6f 72 20 66 75 g indices for fu
18290 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 73 0a 2a ll table scans.*
182a0 2a 20 69 73 20 62 65 63 61 75 73 65 20 73 6f 6d * is because som
182b0 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 63 6f e incorrectly co
182c0 64 65 64 20 6c 65 67 61 63 79 20 61 70 70 6c 69 ded legacy appli
182d0 63 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 6d 61 cations might ma
182e0 6c 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 61 6c lfunction.** mal
182f0 66 75 6e 63 74 69 6f 6e 20 77 68 65 6e 20 74 68 function when th
18300 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 e optimization i
18310 73 20 65 6e 61 62 6c 65 64 2e 20 20 50 72 6f 76 s enabled. Prov
18320 69 64 69 6e 67 20 74 68 65 20 61 62 69 6c 69 74 iding the abilit
18330 79 20 74 6f 0a 2a 2a 20 64 69 73 61 62 6c 65 20 y to.** disable
18340 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e the optimization
18350 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 6c 64 65 allows the olde
18360 72 2c 20 62 75 67 67 79 20 61 70 70 6c 69 63 61 r, buggy applica
18370 74 69 6f 6e 20 63 6f 64 65 20 74 6f 20 77 6f 72 tion code to wor
18380 6b 0a 2a 2a 20 77 69 74 68 6f 75 74 20 63 68 61 k.** without cha
18390 6e 67 65 20 65 76 65 6e 20 77 69 74 68 20 6e 65 nge even with ne
183a0 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 wer versions of
183b0 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 5b 5b SQLite..**.** [[
183c0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 SQLITE_CONFIG_PC
183d0 41 43 48 45 5d 5d 20 5b 5b 53 51 4c 49 54 45 5f ACHE]] [[SQLITE_
183e0 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 CONFIG_GETPCACHE
183f0 5d 5d 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 ]].** <dt>SQLITE
18400 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 61 _CONFIG_PCACHE a
18410 6e 64 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 nd SQLITE_CONFIG
18420 5f 47 45 54 50 43 41 43 48 45 0a 2a 2a 20 3c 64 _GETPCACHE.** <d
18430 64 3e 20 54 68 65 73 65 20 6f 70 74 69 6f 6e 73 d> These options
18440 20 61 72 65 20 6f 62 73 6f 6c 65 74 65 20 61 6e are obsolete an
18450 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 d should not be
18460 75 73 65 64 20 62 79 20 6e 65 77 20 63 6f 64 65 used by new code
18470 2e 0a 2a 2a 20 54 68 65 79 20 61 72 65 20 72 65 ..** They are re
18480 74 61 69 6e 65 64 20 66 6f 72 20 62 61 63 6b 77 tained for backw
18490 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 ards compatibili
184a0 74 79 20 62 75 74 20 61 72 65 20 6e 6f 77 20 6e ty but are now n
184b0 6f 2d 6f 70 73 2e 0a 2a 2a 20 3c 2f 64 6c 3e 0a o-ops..** </dl>.
184c0 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43 **.** [[SQLITE_C
184d0 4f 4e 46 49 47 5f 53 51 4c 4c 4f 47 5d 5d 0a 2a ONFIG_SQLLOG]].*
184e0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
184f0 46 49 47 5f 53 51 4c 4c 4f 47 0a 2a 2a 20 3c 64 FIG_SQLLOG.** <d
18500 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 69 73 d>This option is
18510 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
18520 69 66 20 73 71 6c 69 74 65 20 69 73 20 63 6f 6d if sqlite is com
18530 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a piled with the.*
18540 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f * SQLITE_ENABLE_
18550 53 51 4c 4c 4f 47 20 70 72 65 2d 70 72 6f 63 65 SQLLOG pre-proce
18560 73 73 6f 72 20 6d 61 63 72 6f 20 64 65 66 69 6e ssor macro defin
18570 65 64 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 ed. The first ar
18580 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 0a 2a 2a gument should.**
18590 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f be a pointer to
185a0 20 61 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20 74 a function of t
185b0 79 70 65 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 ype void(*)(void
185c0 2a 2c 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 *,sqlite3*,const
185d0 20 63 68 61 72 2a 2c 20 69 6e 74 29 2e 0a 2a 2a char*, int)..**
185e0 20 54 68 65 20 73 65 63 6f 6e 64 20 73 68 6f 75 The second shou
185f0 6c 64 20 62 65 20 6f 66 20 74 79 70 65 20 28 76 ld be of type (v
18600 6f 69 64 2a 29 2e 20 54 68 65 20 63 61 6c 6c 62 oid*). The callb
18610 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 ack is invoked b
18620 79 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a y the library.**
18630 20 69 6e 20 74 68 72 65 65 20 73 65 70 61 72 61 in three separa
18640 74 65 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 te circumstances
18650 2c 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 , identified by
18660 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 the value passed
18670 20 61 73 20 74 68 65 0a 2a 2a 20 66 6f 75 72 74 as the.** fourt
18680 68 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20 h parameter. If
18690 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d the fourth param
186a0 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 eter is 0, then
186b0 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
186c0 6e 65 63 74 69 6f 6e 0a 2a 2a 20 70 61 73 73 65 nection.** passe
186d0 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 d as the second
186e0 61 72 67 75 6d 65 6e 74 20 68 61 73 20 6a 75 73 argument has jus
186f0 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 20 54 t been opened. T
18700 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
18710 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 61 t.** points to a
18720 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 buffer containi
18730 6e 67 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 ng the name of t
18740 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
18750 20 66 69 6c 65 2e 20 49 66 20 74 68 65 0a 2a 2a file. If the.**
18760 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 fourth paramete
18770 72 20 69 73 20 31 2c 20 74 68 65 6e 20 74 68 65 r is 1, then the
18780 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 SQL statement t
18790 68 61 74 20 74 68 65 20 74 68 69 72 64 20 70 61 hat the third pa
187a0 72 61 6d 65 74 65 72 0a 2a 2a 20 70 6f 69 6e 74 rameter.** point
187b0 73 20 74 6f 20 68 61 73 20 6a 75 73 74 20 62 65 s to has just be
187c0 65 6e 20 65 78 65 63 75 74 65 64 2e 20 4f 72 2c en executed. Or,
187d0 20 69 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 if the fourth p
187e0 61 72 61 6d 65 74 65 72 20 69 73 20 32 2c 20 74 arameter is 2, t
187f0 68 65 6e 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e 65 hen.** the conne
18800 63 74 69 6f 6e 20 62 65 69 6e 67 20 70 61 73 73 ction being pass
18810 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
18820 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 62 65 parameter is be
18830 69 6e 67 20 63 6c 6f 73 65 64 2e 20 54 68 65 0a ing closed. The.
18840 2a 2a 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 ** third paramet
18850 65 72 20 69 73 20 70 61 73 73 65 64 20 4e 55 4c er is passed NUL
18860 4c 20 49 6e 20 74 68 69 73 20 63 61 73 65 2e 0a L In this case..
18870 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 ** </dl>.*/.#def
18880 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ine SQLITE_CONFI
18890 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20 20 G_SINGLETHREAD
188a0 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64 65 1 /* nil */.#de
188b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 fine SQLITE_CONF
188c0 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 20 20 IG_MULTITHREAD
188d0 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64 2 /* nil */.#d
188e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
188f0 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 20 20 FIG_SERIALIZED
18900 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 3 /* nil */.#
18910 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
18920 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 NFIG_MALLOC
18930 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65 33 4 /* sqlite3
18940 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f _mem_methods* */
18950 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
18960 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 CONFIG_GETMALLOC
18970 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69 74 5 /* sqlit
18980 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 e3_mem_methods*
18990 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
189a0 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 E_CONFIG_SCRATCH
189b0 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76 6f 69 6 /* voi
189c0 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 d*, int sz, int
189d0 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c N */.#define SQL
189e0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
189f0 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a 20 76 ACHE 7 /* v
18a00 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e oid*, int sz, in
18a10 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 t N */.#define S
18a20 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 QLITE_CONFIG_HEA
18a30 50 20 20 20 20 20 20 20 20 20 20 38 20 20 2f 2a P 8 /*
18a40 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42 79 74 void*, int nByt
18a50 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a 23 64 e, int min */.#d
18a60 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
18a70 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 20 20 FIG_MEMSTATUS
18a80 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61 6e 20 9 /* boolean
18a90 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
18aa0 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 20 20 E_CONFIG_MUTEX
18ab0 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73 71 6c 10 /* sql
18ac0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
18ad0 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ds* */.#define S
18ae0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 QLITE_CONFIG_GET
18af0 4d 55 54 45 58 20 20 20 20 20 31 31 20 20 2f 2a MUTEX 11 /*
18b00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
18b10 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a 20 70 72 ethods* */./* pr
18b20 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 54 45 5f eviously SQLITE_
18b30 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 4c 4c 4f CONFIG_CHUNKALLO
18b40 43 20 31 32 20 77 68 69 63 68 20 69 73 20 6e 6f C 12 which is no
18b50 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 0a 23 64 w unused. */ .#d
18b60 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
18b70 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 FIG_LOOKASIDE
18b80 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 6e 74 20 13 /* int int
18b90 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
18ba0 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 E_CONFIG_PCACHE
18bb0 20 20 20 20 20 20 31 34 20 20 2f 2a 20 6e 6f 2d 14 /* no-
18bc0 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 op */.#define SQ
18bd0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 LITE_CONFIG_GETP
18be0 43 41 43 48 45 20 20 20 20 31 35 20 20 2f 2a 20 CACHE 15 /*
18bf0 6e 6f 2d 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 no-op */.#define
18c00 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c SQLITE_CONFIG_L
18c10 4f 47 20 20 20 20 20 20 20 20 20 20 31 36 20 20 OG 16
18c20 2f 2a 20 78 46 75 6e 63 2c 20 76 6f 69 64 2a 20 /* xFunc, void*
18c30 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
18c40 45 5f 43 4f 4e 46 49 47 5f 55 52 49 20 20 20 20 E_CONFIG_URI
18c50 20 20 20 20 20 20 31 37 20 20 2f 2a 20 69 6e 74 17 /* int
18c60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
18c70 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 TE_CONFIG_PCACHE
18c80 32 20 20 20 20 20 20 31 38 20 20 2f 2a 20 73 71 2 18 /* sq
18c90 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
18ca0 68 6f 64 73 32 2a 20 2a 2f 0a 23 64 65 66 69 6e hods2* */.#defin
18cb0 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f e SQLITE_CONFIG_
18cc0 47 45 54 50 43 41 43 48 45 32 20 20 20 31 39 20 GETPCACHE2 19
18cd0 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 /* sqlite3_pcac
18ce0 68 65 5f 6d 65 74 68 6f 64 73 32 2a 20 2a 2f 0a he_methods2* */.
18cf0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
18d00 4f 4e 46 49 47 5f 43 4f 56 45 52 49 4e 47 5f 49 ONFIG_COVERING_I
18d10 4e 44 45 58 5f 53 43 41 4e 20 32 30 20 20 2f 2a NDEX_SCAN 20 /*
18d20 20 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 int */.#define
18d30 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 51 SQLITE_CONFIG_SQ
18d40 4c 4c 4f 47 20 20 20 20 20 20 20 32 31 20 20 2f LLOG 21 /
18d50 2a 20 78 53 71 6c 6c 6f 67 2c 20 76 6f 69 64 2a * xSqllog, void*
18d60 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 */../*.** CAPI3
18d70 52 45 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f REF: Database Co
18d80 6e 6e 65 63 74 69 6f 6e 20 43 6f 6e 66 69 67 75 nnection Configu
18d90 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 0a 2a ration Options.*
18da0 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 *.** These const
18db0 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 76 61 ants are the ava
18dc0 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20 63 ilable integer c
18dd0 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 onfiguration opt
18de0 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e ions that.** can
18df0 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 be passed as th
18e00 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
18e10 74 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 t to the [sqlite
18e20 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 3_db_config()] i
18e30 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
18e40 4e 65 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f New configuratio
18e50 6e 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 n options may be
18e60 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 added in future
18e70 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c releases of SQL
18e80 69 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 ite..** Existing
18e90 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f configuration o
18ea0 70 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 ptions might be
18eb0 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 discontinued. A
18ec0 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 pplications.** s
18ed0 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20 hould check the
18ee0 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d return code from
18ef0 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e [sqlite3_db_con
18f00 66 69 67 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73 fig()] to make s
18f10 75 72 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 ure that.** the
18f20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 20 20 5e 54 call worked. ^T
18f30 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 he [sqlite3_db_c
18f40 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 onfig()] interfa
18f50 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 ce will return a
18f60 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 .** non-zero [er
18f70 72 6f 72 20 63 6f 64 65 5d 20 69 66 20 61 20 64 ror code] if a d
18f80 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 75 iscontinued or u
18f90 6e 73 75 70 70 6f 72 74 65 64 20 63 6f 6e 66 69 nsupported confi
18fa0 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 0a guration option.
18fb0 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a ** is invoked..*
18fc0 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 *.** <dl>.** <dt
18fd0 3e 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 >SQLITE_DBCONFIG
18fe0 5f 4c 4f 4f 4b 41 53 49 44 45 3c 2f 64 74 3e 0a _LOOKASIDE</dt>.
18ff0 2a 2a 20 3c 64 64 3e 20 5e 54 68 69 73 20 6f 70 ** <dd> ^This op
19000 74 69 6f 6e 20 74 61 6b 65 73 20 74 68 72 65 65 tion takes three
19010 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 72 67 75 additional argu
19020 6d 65 6e 74 73 20 74 68 61 74 20 64 65 74 65 72 ments that deter
19030 6d 69 6e 65 20 74 68 65 20 0a 2a 2a 20 5b 6c 6f mine the .** [lo
19040 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 okaside memory a
19050 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e 66 69 67 llocator] config
19060 75 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 uration for the
19070 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
19080 74 69 6f 6e 5d 2e 0a 2a 2a 20 5e 54 68 65 20 66 tion]..** ^The f
19090 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 28 74 irst argument (t
190a0 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 he third paramet
190b0 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 64 er to [sqlite3_d
190c0 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 73 20 61 b_config()] is a
190d0 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 .** pointer to a
190e0 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 memory buffer t
190f0 6f 20 75 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 o use for lookas
19100 69 64 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 5e ide memory..** ^
19110 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 The first argume
19120 6e 74 20 61 66 74 65 72 20 74 68 65 20 53 51 4c nt after the SQL
19130 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f ITE_DBCONFIG_LOO
19140 4b 41 53 49 44 45 20 76 65 72 62 0a 2a 2a 20 6d KASIDE verb.** m
19150 61 79 20 62 65 20 4e 55 4c 4c 20 69 6e 20 77 68 ay be NULL in wh
19160 69 63 68 20 63 61 73 65 20 53 51 4c 69 74 65 20 ich case SQLite
19170 77 69 6c 6c 20 61 6c 6c 6f 63 61 74 65 20 74 68 will allocate th
19180 65 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 62 e.** lookaside b
19190 75 66 66 65 72 20 69 74 73 65 6c 66 20 75 73 69 uffer itself usi
191a0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c ng [sqlite3_mall
191b0 6f 63 28 29 5d 2e 20 5e 54 68 65 20 73 65 63 6f oc()]. ^The seco
191c0 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 nd argument is t
191d0 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 he.** size of ea
191e0 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 ch lookaside buf
191f0 66 65 72 20 73 6c 6f 74 2e 20 20 5e 54 68 65 20 fer slot. ^The
19200 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 third argument i
19210 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a s the number of.
19220 2a 2a 20 73 6c 6f 74 73 2e 20 20 54 68 65 20 73 ** slots. The s
19230 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 ize of the buffe
19240 72 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 r in the first a
19250 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 rgument must be
19260 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 greater than.**
19270 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 or equal to the
19280 70 72 6f 64 75 63 74 20 6f 66 20 74 68 65 20 73 product of the s
19290 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 econd and third
192a0 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 20 arguments. The
192b0 62 75 66 66 65 72 0a 2a 2a 20 6d 75 73 74 20 62 buffer.** must b
192c0 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 e aligned to an
192d0 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 8-byte boundary.
192e0 20 20 5e 49 66 20 74 68 65 20 73 65 63 6f 6e 64 ^If the second
192f0 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 argument to.**
19300 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f SQLITE_DBCONFIG_
19310 4c 4f 4f 4b 41 53 49 44 45 20 69 73 20 6e 6f 74 LOOKASIDE is not
19320 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 a multiple of 8
19330 2c 20 69 74 20 69 73 20 69 6e 74 65 72 6e 61 6c , it is internal
19340 6c 79 0a 2a 2a 20 72 6f 75 6e 64 65 64 20 64 6f ly.** rounded do
19350 77 6e 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 wn to the next s
19360 6d 61 6c 6c 65 72 20 6d 75 6c 74 69 70 6c 65 20 maller multiple
19370 6f 66 20 38 2e 20 20 5e 28 54 68 65 20 6c 6f 6f of 8. ^(The loo
19380 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 0a 2a 2a kaside memory.**
19390 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 configuration f
193a0 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f or a database co
193b0 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 20 6f 6e 6c nnection can onl
193c0 79 20 62 65 20 63 68 61 6e 67 65 64 20 77 68 65 y be changed whe
193d0 6e 20 74 68 61 74 0a 2a 2a 20 63 6f 6e 6e 65 63 n that.** connec
193e0 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 75 72 72 tion is not curr
193f0 65 6e 74 6c 79 20 75 73 69 6e 67 20 6c 6f 6f 6b ently using look
19400 61 73 69 64 65 20 6d 65 6d 6f 72 79 2c 20 6f 72 aside memory, or
19410 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 0a in other words.
19420 2a 2a 20 77 68 65 6e 20 74 68 65 20 22 63 75 72 ** when the "cur
19430 72 65 6e 74 20 76 61 6c 75 65 22 20 72 65 74 75 rent value" retu
19440 72 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 rned by.** [sqli
19450 74 65 33 5f 64 62 5f 73 74 61 74 75 73 5d 28 44 te3_db_status](D
19460 2c 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f ,[SQLITE_CONFIG_
19470 4c 4f 4f 4b 41 53 49 44 45 5d 2c 2e 2e 2e 29 20 LOOKASIDE],...)
19480 69 73 20 7a 65 72 6f 2e 0a 2a 2a 20 41 6e 79 20 is zero..** Any
19490 61 74 74 65 6d 70 74 20 74 6f 20 63 68 61 6e 67 attempt to chang
194a0 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 e the lookaside
194b0 6d 65 6d 6f 72 79 20 63 6f 6e 66 69 67 75 72 61 memory configura
194c0 74 69 6f 6e 20 77 68 65 6e 20 6c 6f 6f 6b 61 73 tion when lookas
194d0 69 64 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 69 73 ide.** memory is
194e0 20 69 6e 20 75 73 65 20 6c 65 61 76 65 73 20 74 in use leaves t
194f0 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e he configuration
19500 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 72 unchanged and r
19510 65 74 75 72 6e 73 20 0a 2a 2a 20 5b 53 51 4c 49 eturns .** [SQLI
19520 54 45 5f 42 55 53 59 5d 2e 29 5e 3c 2f 64 64 3e TE_BUSY].)^</dd>
19530 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
19540 45 5f 44 42 43 4f 4e 46 49 47 5f 45 4e 41 42 4c E_DBCONFIG_ENABL
19550 45 5f 46 4b 45 59 3c 2f 64 74 3e 0a 2a 2a 20 3c E_FKEY</dt>.** <
19560 64 64 3e 20 5e 54 68 69 73 20 6f 70 74 69 6f 6e dd> ^This option
19570 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e 61 62 is used to enab
19580 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 le or disable th
19590 65 20 65 6e 66 6f 72 63 65 6d 65 6e 74 20 6f 66 e enforcement of
195a0 0a 2a 2a 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 .** [foreign key
195b0 20 63 6f 6e 73 74 72 61 69 6e 74 73 5d 2e 20 20 constraints].
195c0 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 There should be
195d0 74 77 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 two additional a
195e0 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 20 54 68 65 rguments..** The
195f0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
19600 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 is an integer wh
19610 69 63 68 20 69 73 20 30 20 74 6f 20 64 69 73 61 ich is 0 to disa
19620 62 6c 65 20 46 4b 20 65 6e 66 6f 72 63 65 6d 65 ble FK enforceme
19630 6e 74 2c 0a 2a 2a 20 70 6f 73 69 74 69 76 65 20 nt,.** positive
19640 74 6f 20 65 6e 61 62 6c 65 20 46 4b 20 65 6e 66 to enable FK enf
19650 6f 72 63 65 6d 65 6e 74 20 6f 72 20 6e 65 67 61 orcement or nega
19660 74 69 76 65 20 74 6f 20 6c 65 61 76 65 20 46 4b tive to leave FK
19670 20 65 6e 66 6f 72 63 65 6d 65 6e 74 0a 2a 2a 20 enforcement.**
19680 75 6e 63 68 61 6e 67 65 64 2e 20 20 54 68 65 20 unchanged. The
19690 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
196a0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
196b0 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e 74 6f an integer into
196c0 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 77 72 69 which.** is wri
196d0 74 74 65 6e 20 30 20 6f 72 20 31 20 74 6f 20 69 tten 0 or 1 to i
196e0 6e 64 69 63 61 74 65 20 77 68 65 74 68 65 72 20 ndicate whether
196f0 46 4b 20 65 6e 66 6f 72 63 65 6d 65 6e 74 20 69 FK enforcement i
19700 73 20 6f 66 66 20 6f 72 20 6f 6e 0a 2a 2a 20 66 s off or on.** f
19710 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 63 61 ollowing this ca
19720 6c 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 ll. The second
19730 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 62 65 parameter may be
19740 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
19750 20 69 6e 0a 2a 2a 20 77 68 69 63 68 20 63 61 73 in.** which cas
19760 65 20 74 68 65 20 46 4b 20 65 6e 66 6f 72 63 65 e the FK enforce
19770 6d 65 6e 74 20 73 65 74 74 69 6e 67 20 69 73 20 ment setting is
19780 6e 6f 74 20 72 65 70 6f 72 74 65 64 20 62 61 63 not reported bac
19790 6b 2e 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c k. </dd>.**.** <
197a0 64 74 3e 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 dt>SQLITE_DBCONF
197b0 49 47 5f 45 4e 41 42 4c 45 5f 54 52 49 47 47 45 IG_ENABLE_TRIGGE
197c0 52 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 20 5e R</dt>.** <dd> ^
197d0 54 68 69 73 20 6f 70 74 69 6f 6e 20 69 73 20 75 This option is u
197e0 73 65 64 20 74 6f 20 65 6e 61 62 6c 65 20 6f 72 sed to enable or
197f0 20 64 69 73 61 62 6c 65 20 5b 43 52 45 41 54 45 disable [CREATE
19800 20 54 52 49 47 47 45 52 20 7c 20 74 72 69 67 67 TRIGGER | trigg
19810 65 72 73 5d 2e 0a 2a 2a 20 54 68 65 72 65 20 73 ers]..** There s
19820 68 6f 75 6c 64 20 62 65 20 74 77 6f 20 61 64 64 hould be two add
19830 69 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 itional argument
19840 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 s..** The first
19850 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 69 argument is an i
19860 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 nteger which is
19870 30 20 74 6f 20 64 69 73 61 62 6c 65 20 74 72 69 0 to disable tri
19880 67 67 65 72 73 2c 0a 2a 2a 20 70 6f 73 69 74 69 ggers,.** positi
19890 76 65 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 69 ve to enable tri
198a0 67 67 65 72 73 20 6f 72 20 6e 65 67 61 74 69 76 ggers or negativ
198b0 65 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20 73 e to leave the s
198c0 65 74 74 69 6e 67 20 75 6e 63 68 61 6e 67 65 64 etting unchanged
198d0 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 ..** The second
198e0 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 parameter is a p
198f0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 74 ointer to an int
19900 65 67 65 72 20 69 6e 74 6f 20 77 68 69 63 68 0a eger into which.
19910 2a 2a 20 69 73 20 77 72 69 74 74 65 6e 20 30 20 ** is written 0
19920 6f 72 20 31 20 74 6f 20 69 6e 64 69 63 61 74 65 or 1 to indicate
19930 20 77 68 65 74 68 65 72 20 74 72 69 67 67 65 72 whether trigger
19940 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 6f s are disabled o
19950 72 20 65 6e 61 62 6c 65 64 0a 2a 2a 20 66 6f 6c r enabled.** fol
19960 6c 6f 77 69 6e 67 20 74 68 69 73 20 63 61 6c 6c lowing this call
19970 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 . The second pa
19980 72 61 6d 65 74 65 72 20 6d 61 79 20 62 65 20 61 rameter may be a
19990 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 69 NULL pointer, i
199a0 6e 0a 2a 2a 20 77 68 69 63 68 20 63 61 73 65 20 n.** which case
199b0 74 68 65 20 74 72 69 67 67 65 72 20 73 65 74 74 the trigger sett
199c0 69 6e 67 20 69 73 20 6e 6f 74 20 72 65 70 6f 72 ing is not repor
199d0 74 65 64 20 62 61 63 6b 2e 20 3c 2f 64 64 3e 0a ted back. </dd>.
199e0 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 **.** </dl>.*/.#
199f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 define SQLITE_DB
19a00 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 CONFIG_LOOKASIDE
19a10 20 20 20 20 20 20 20 31 30 30 31 20 20 2f 2a 20 1001 /*
19a20 76 6f 69 64 2a 20 69 6e 74 20 69 6e 74 20 2a 2f void* int int */
19a30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
19a40 44 42 43 4f 4e 46 49 47 5f 45 4e 41 42 4c 45 5f DBCONFIG_ENABLE_
19a50 46 4b 45 59 20 20 20 20 20 31 30 30 32 20 20 2f FKEY 1002 /
19a60 2a 20 69 6e 74 20 69 6e 74 2a 20 2a 2f 0a 23 64 * int int* */.#d
19a70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 43 efine SQLITE_DBC
19a80 4f 4e 46 49 47 5f 45 4e 41 42 4c 45 5f 54 52 49 ONFIG_ENABLE_TRI
19a90 47 47 45 52 20 20 31 30 30 33 20 20 2f 2a 20 69 GGER 1003 /* i
19aa0 6e 74 20 69 6e 74 2a 20 2a 2f 0a 0a 0a 2f 2a 0a nt int* */.../*.
19ab0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 ** CAPI3REF: Ena
19ac0 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 ble Or Disable E
19ad0 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 xtended Result C
19ae0 6f 64 65 73 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 odes.**.** ^The
19af0 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
19b00 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20 _result_codes()
19b10 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 routine enables
19b20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a or disables the.
19b30 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 ** [extended res
19b40 75 6c 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75 ult codes] featu
19b50 72 65 20 6f 66 20 53 51 4c 69 74 65 2e 20 5e 54 re of SQLite. ^T
19b60 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 he extended resu
19b70 6c 74 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 lt.** codes are
19b80 64 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 disabled by defa
19b90 75 6c 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 ult for historic
19ba0 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 al compatibility
19bb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
19bc0 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 int sqlite3_exte
19bd0 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 nded_result_code
19be0 73 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 s(sqlite3*, int
19bf0 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 onoff);../*.** C
19c00 41 50 49 33 52 45 46 3a 20 4c 61 73 74 20 49 6e API3REF: Last In
19c10 73 65 72 74 20 52 6f 77 69 64 0a 2a 2a 0a 2a 2a sert Rowid.**.**
19c20 20 5e 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 ^Each entry in
19c30 61 6e 20 53 51 4c 69 74 65 20 74 61 62 6c 65 20 an SQLite table
19c40 68 61 73 20 61 20 75 6e 69 71 75 65 20 36 34 2d has a unique 64-
19c50 62 69 74 20 73 69 67 6e 65 64 0a 2a 2a 20 69 6e bit signed.** in
19c60 74 65 67 65 72 20 6b 65 79 20 63 61 6c 6c 65 64 teger key called
19c70 20 74 68 65 20 5b 52 4f 57 49 44 20 7c 20 22 72 the [ROWID | "r
19c80 6f 77 69 64 22 5d 2e 20 5e 54 68 65 20 72 6f 77 owid"]. ^The row
19c90 69 64 20 69 73 20 61 6c 77 61 79 73 20 61 76 61 id is always ava
19ca0 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 61 6e 20 ilable.** as an
19cb0 75 6e 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d undeclared colum
19cc0 6e 20 6e 61 6d 65 64 20 52 4f 57 49 44 2c 20 4f n named ROWID, O
19cd0 49 44 2c 20 6f 72 20 5f 52 4f 57 49 44 5f 20 61 ID, or _ROWID_ a
19ce0 73 20 6c 6f 6e 67 20 61 73 20 74 68 6f 73 65 0a s long as those.
19cf0 2a 2a 20 6e 61 6d 65 73 20 61 72 65 20 6e 6f 74 ** names are not
19d00 20 61 6c 73 6f 20 75 73 65 64 20 62 79 20 65 78 also used by ex
19d10 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 plicitly declare
19d20 64 20 63 6f 6c 75 6d 6e 73 2e 20 5e 49 66 0a 2a d columns. ^If.*
19d30 2a 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 * the table has
19d40 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 79 70 65 a column of type
19d50 20 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 [INTEGER PRIMAR
19d60 59 20 4b 45 59 5d 20 74 68 65 6e 20 74 68 61 74 Y KEY] then that
19d70 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 61 6e column.** is an
19d80 6f 74 68 65 72 20 61 6c 69 61 73 20 66 6f 72 20 other alias for
19d90 74 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a the rowid..**.**
19da0 20 5e 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 ^This routine r
19db0 65 74 75 72 6e 73 20 74 68 65 20 5b 72 6f 77 69 eturns the [rowi
19dc0 64 5d 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 d] of the most r
19dd0 65 63 65 6e 74 0a 2a 2a 20 73 75 63 63 65 73 73 ecent.** success
19de0 66 75 6c 20 5b 49 4e 53 45 52 54 5d 20 69 6e 74 ful [INSERT] int
19df0 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
19e00 72 6f 6d 20 74 68 65 20 5b 64 61 74 61 62 61 73 rom the [databas
19e10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a e connection].**
19e20 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 in the first ar
19e30 67 75 6d 65 6e 74 2e 20 20 5e 41 73 20 6f 66 20 gument. ^As of
19e40 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 SQLite version 3
19e50 2e 37 2e 37 2c 20 74 68 69 73 20 72 6f 75 74 69 .7.7, this routi
19e60 6e 65 73 0a 2a 2a 20 72 65 63 6f 72 64 73 20 74 nes.** records t
19e70 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 he last insert r
19e80 6f 77 69 64 20 6f 66 20 62 6f 74 68 20 6f 72 64 owid of both ord
19e90 69 6e 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 inary tables and
19ea0 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 [virtual tables
19eb0 5d 2e 0a 2a 2a 20 5e 49 66 20 6e 6f 20 73 75 63 ]..** ^If no suc
19ec0 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d cessful [INSERT]
19ed0 73 0a 2a 2a 20 68 61 76 65 20 65 76 65 72 20 6f s.** have ever o
19ee0 63 63 75 72 72 65 64 20 6f 6e 20 74 68 61 74 20 ccurred on that
19ef0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
19f00 69 6f 6e 2c 20 7a 65 72 6f 20 69 73 20 72 65 74 ion, zero is ret
19f10 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 28 49 urned..**.** ^(I
19f20 66 20 61 6e 20 5b 49 4e 53 45 52 54 5d 20 6f 63 f an [INSERT] oc
19f30 63 75 72 73 20 77 69 74 68 69 6e 20 61 20 74 72 curs within a tr
19f40 69 67 67 65 72 20 6f 72 20 77 69 74 68 69 6e 20 igger or within
19f50 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a [virtual table
19f60 5d 0a 2a 2a 20 6d 65 74 68 6f 64 2c 20 74 68 65 ].** method, the
19f70 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 n this routine w
19f80 69 6c 6c 20 72 65 74 75 72 6e 20 74 68 65 20 5b ill return the [
19f90 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 69 6e rowid] of the in
19fa0 73 65 72 74 65 64 0a 2a 2a 20 72 6f 77 20 61 73 serted.** row as
19fb0 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 72 69 long as the tri
19fc0 67 67 65 72 20 6f 72 20 76 69 72 74 75 61 6c 20 gger or virtual
19fd0 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 69 73 20 table method is
19fe0 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 20 42 75 74 20 running..** But
19ff0 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 65 72 once the trigger
1a000 20 6f 72 20 76 69 72 74 75 61 6c 20 74 61 62 6c or virtual tabl
1a010 65 20 6d 65 74 68 6f 64 20 65 6e 64 73 2c 20 74 e method ends, t
1a020 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
1a030 64 20 0a 2a 2a 20 62 79 20 74 68 69 73 20 72 6f d .** by this ro
1a040 75 74 69 6e 65 20 72 65 76 65 72 74 73 20 74 6f utine reverts to
1a050 20 77 68 61 74 20 69 74 20 77 61 73 20 62 65 66 what it was bef
1a060 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72 20 ore the trigger
1a070 6f 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 or virtual.** ta
1a080 62 6c 65 20 6d 65 74 68 6f 64 20 62 65 67 61 6e ble method began
1a090 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 41 6e 20 5b 49 .)^.**.** ^An [I
1a0a0 4e 53 45 52 54 5d 20 74 68 61 74 20 66 61 69 6c NSERT] that fail
1a0b0 73 20 64 75 65 20 74 6f 20 61 20 63 6f 6e 73 74 s due to a const
1a0c0 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 raint violation
1a0d0 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 73 75 63 63 is not a.** succ
1a0e0 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d 20 essful [INSERT]
1a0f0 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 and does not cha
1a100 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 72 65 nge the value re
1a110 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 0a 2a turned by this.*
1a120 2a 20 72 6f 75 74 69 6e 65 2e 20 20 5e 54 68 75 * routine. ^Thu
1a130 73 20 49 4e 53 45 52 54 20 4f 52 20 46 41 49 4c s INSERT OR FAIL
1a140 2c 20 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f , INSERT OR IGNO
1a150 52 45 2c 20 49 4e 53 45 52 54 20 4f 52 20 52 4f RE, INSERT OR RO
1a160 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 6e 64 20 49 LLBACK,.** and I
1a170 4e 53 45 52 54 20 4f 52 20 41 42 4f 52 54 20 6d NSERT OR ABORT m
1a180 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 ake no changes t
1a190 6f 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c o the return val
1a1a0 75 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 72 6f ue of this.** ro
1a1b0 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65 69 72 utine when their
1a1c0 20 69 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 insertion fails
1a1d0 2e 20 20 5e 28 57 68 65 6e 20 49 4e 53 45 52 54 . ^(When INSERT
1a1e0 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a 20 65 OR REPLACE.** e
1a1f0 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f 6e 73 ncounters a cons
1a200 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e traint violation
1a210 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 66 61 , it does not fa
1a220 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e 53 45 il. The.** INSE
1a230 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20 RT continues to
1a240 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 65 72 completion after
1a250 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 20 74 deleting rows t
1a260 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 74 68 hat caused.** th
1a270 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 72 6f e constraint pro
1a280 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 20 4f blem so INSERT O
1a290 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c 20 61 R REPLACE will a
1a2a0 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a 2a 20 lways change.**
1a2b0 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 the return value
1a2c0 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 of this interfa
1a2d0 63 65 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 46 6f 72 ce.)^.**.** ^For
1a2e0 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 the purposes of
1a2f0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 this routine, a
1a300 6e 20 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 6f n [INSERT] is co
1a310 6e 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 nsidered to.** b
1a320 65 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 65 e successful eve
1a330 6e 20 69 66 20 69 74 20 69 73 20 73 75 62 73 65 n if it is subse
1a340 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 quently rolled b
1a350 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ack..**.** This
1a360 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 63 63 65 function is acce
1a370 73 73 69 62 6c 65 20 74 6f 20 53 51 4c 20 73 74 ssible to SQL st
1a380 61 74 65 6d 65 6e 74 73 20 76 69 61 20 74 68 65 atements via the
1a390 0a 2a 2a 20 5b 6c 61 73 74 5f 69 6e 73 65 72 74 .** [last_insert
1a3a0 5f 72 6f 77 69 64 28 29 20 53 51 4c 20 66 75 6e _rowid() SQL fun
1a3b0 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 ction]..**.** If
1a3c0 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 65 a separate thre
1a3d0 61 64 20 70 65 72 66 6f 72 6d 73 20 61 20 6e 65 ad performs a ne
1a3e0 77 20 5b 49 4e 53 45 52 54 5d 20 6f 6e 20 74 68 w [INSERT] on th
1a3f0 65 20 73 61 6d 65 0a 2a 2a 20 64 61 74 61 62 61 e same.** databa
1a400 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 se connection wh
1a410 69 6c 65 20 74 68 65 20 5b 73 71 6c 69 74 65 33 ile the [sqlite3
1a420 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 _last_insert_row
1a430 69 64 28 29 5d 0a 2a 2a 20 66 75 6e 63 74 69 6f id()].** functio
1a440 6e 20 69 73 20 72 75 6e 6e 69 6e 67 20 61 6e 64 n is running and
1a450 20 74 68 75 73 20 63 68 61 6e 67 65 73 20 74 68 thus changes th
1a460 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 5b 72 e last insert [r
1a470 6f 77 69 64 5d 2c 0a 2a 2a 20 74 68 65 6e 20 74 owid],.** then t
1a480 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
1a490 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 6c 61 d by [sqlite3_la
1a4a0 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 st_insert_rowid(
1a4b0 29 5d 20 69 73 0a 2a 2a 20 75 6e 70 72 65 64 69 )] is.** unpredi
1a4c0 63 74 61 62 6c 65 20 61 6e 64 20 6d 69 67 68 74 ctable and might
1a4d0 20 6e 6f 74 20 65 71 75 61 6c 20 65 69 74 68 65 not equal eithe
1a4e0 72 20 74 68 65 20 6f 6c 64 20 6f 72 20 74 68 65 r the old or the
1a4f0 20 6e 65 77 0a 2a 2a 20 6c 61 73 74 20 69 6e 73 new.** last ins
1a500 65 72 74 20 5b 72 6f 77 69 64 5d 2e 0a 2a 2f 0a ert [rowid]..*/.
1a510 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
1a520 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e3_int64 sqlite3
1a530 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 _last_insert_row
1a540 69 64 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f id(sqlite3*);../
1a550 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 *.** CAPI3REF: C
1a560 6f 75 6e 74 20 54 68 65 20 4e 75 6d 62 65 72 20 ount The Number
1a570 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66 69 65 64 Of Rows Modified
1a580 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 66 75 6e .**.** ^This fun
1a590 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
1a5a0 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 e number of data
1a5b0 62 61 73 65 20 72 6f 77 73 20 74 68 61 74 20 77 base rows that w
1a5c0 65 72 65 20 63 68 61 6e 67 65 64 0a 2a 2a 20 6f ere changed.** o
1a5d0 72 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 r inserted or de
1a5e0 6c 65 74 65 64 20 62 79 20 74 68 65 20 6d 6f 73 leted by the mos
1a5f0 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c t recently compl
1a600 65 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 eted SQL stateme
1a610 6e 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 5b 64 61 nt.** on the [da
1a620 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1a630 6e 5d 20 73 70 65 63 69 66 69 65 64 20 62 79 20 n] specified by
1a640 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 the first parame
1a650 74 65 72 2e 0a 2a 2a 20 5e 28 4f 6e 6c 79 20 63 ter..** ^(Only c
1a660 68 61 6e 67 65 73 20 74 68 61 74 20 61 72 65 20 hanges that are
1a670 64 69 72 65 63 74 6c 79 20 73 70 65 63 69 66 69 directly specifi
1a680 65 64 20 62 79 20 74 68 65 20 5b 49 4e 53 45 52 ed by the [INSER
1a690 54 5d 2c 20 5b 55 50 44 41 54 45 5d 2c 0a 2a 2a T], [UPDATE],.**
1a6a0 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 73 74 61 or [DELETE] sta
1a6b0 74 65 6d 65 6e 74 20 61 72 65 20 63 6f 75 6e 74 tement are count
1a6c0 65 64 2e 20 20 41 75 78 69 6c 69 61 72 79 20 63 ed. Auxiliary c
1a6d0 68 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 hanges caused by
1a6e0 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 6f 72 20 .** triggers or
1a6f0 5b 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 74 [foreign key act
1a700 69 6f 6e 73 5d 20 61 72 65 20 6e 6f 74 20 63 6f ions] are not co
1a710 75 6e 74 65 64 2e 29 5e 20 55 73 65 20 74 68 65 unted.)^ Use the
1a720 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 .** [sqlite3_tot
1a730 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 66 75 al_changes()] fu
1a740 6e 63 74 69 6f 6e 20 74 6f 20 66 69 6e 64 20 74 nction to find t
1a750 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
1a760 6f 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20 69 6e of changes.** in
1a770 63 6c 75 64 69 6e 67 20 63 68 61 6e 67 65 73 20 cluding changes
1a780 63 61 75 73 65 64 20 62 79 20 74 72 69 67 67 65 caused by trigge
1a790 72 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b rs and foreign k
1a7a0 65 79 20 61 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a ey actions..**.*
1a7b0 2a 20 5e 43 68 61 6e 67 65 73 20 74 6f 20 61 20 * ^Changes to a
1a7c0 76 69 65 77 20 74 68 61 74 20 61 72 65 20 73 69 view that are si
1a7d0 6d 75 6c 61 74 65 64 20 62 79 20 61 6e 20 5b 49 mulated by an [I
1a7e0 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 NSTEAD OF trigge
1a7f0 72 5d 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 63 6f r].** are not co
1a800 75 6e 74 65 64 2e 20 20 4f 6e 6c 79 20 72 65 61 unted. Only rea
1a810 6c 20 74 61 62 6c 65 20 63 68 61 6e 67 65 73 20 l table changes
1a820 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 0a are counted..**.
1a830 2a 2a 20 5e 28 41 20 22 72 6f 77 20 63 68 61 6e ** ^(A "row chan
1a840 67 65 22 20 69 73 20 61 20 63 68 61 6e 67 65 20 ge" is a change
1a850 74 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 to a single row
1a860 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c of a single tabl
1a870 65 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 61 e.** caused by a
1a880 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45 n INSERT, DELETE
1a890 2c 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 , or UPDATE stat
1a8a0 65 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68 61 ement. Rows tha
1a8b0 74 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67 65 64 t.** are changed
1a8c0 20 61 73 20 73 69 64 65 20 65 66 66 65 63 74 73 as side effects
1a8d0 20 6f 66 20 5b 52 45 50 4c 41 43 45 5d 20 63 6f of [REPLACE] co
1a8e0 6e 73 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 74 nstraint resolut
1a8f0 69 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b ion,.** rollback
1a900 2c 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 , ABORT processi
1a910 6e 67 2c 20 5b 44 52 4f 50 20 54 41 42 4c 45 5d ng, [DROP TABLE]
1a920 2c 20 6f 72 20 62 79 20 61 6e 79 20 6f 74 68 65 , or by any othe
1a930 72 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 6d 73 20 r.** mechanisms
1a940 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 do not count as
1a950 64 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 direct row chang
1a960 65 73 2e 29 5e 0a 2a 2a 0a 2a 2a 20 41 20 22 74 es.)^.**.** A "t
1a970 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 22 20 rigger context"
1a980 69 73 20 61 20 73 63 6f 70 65 20 6f 66 20 65 78 is a scope of ex
1a990 65 63 75 74 69 6f 6e 20 74 68 61 74 20 62 65 67 ecution that beg
1a9a0 69 6e 73 20 61 6e 64 0a 2a 2a 20 65 6e 64 73 20 ins and.** ends
1a9b0 77 69 74 68 20 74 68 65 20 73 63 72 69 70 74 20 with the script
1a9c0 6f 66 20 61 20 5b 43 52 45 41 54 45 20 54 52 49 of a [CREATE TRI
1a9d0 47 47 45 52 20 7c 20 74 72 69 67 67 65 72 5d 2e GGER | trigger].
1a9e0 20 0a 2a 2a 20 4d 6f 73 74 20 53 51 4c 20 73 74 .** Most SQL st
1a9f0 61 74 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 atements are.**
1aa00 65 76 61 6c 75 61 74 65 64 20 6f 75 74 73 69 64 evaluated outsid
1aa10 65 20 6f 66 20 61 6e 79 20 74 72 69 67 67 65 72 e of any trigger
1aa20 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 22 . This is the "
1aa30 74 6f 70 20 6c 65 76 65 6c 22 0a 2a 2a 20 74 72 top level".** tr
1aa40 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 igger context.
1aa50 49 66 20 61 20 74 72 69 67 67 65 72 20 66 69 72 If a trigger fir
1aa60 65 73 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 es from the top
1aa70 6c 65 76 65 6c 2c 20 61 0a 2a 2a 20 6e 65 77 20 level, a.** new
1aa80 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 20 trigger context
1aa90 69 73 20 65 6e 74 65 72 65 64 20 66 6f 72 20 74 is entered for t
1aaa0 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 he duration of t
1aab0 68 61 74 20 6f 6e 65 0a 2a 2a 20 74 72 69 67 67 hat one.** trigg
1aac0 65 72 2e 20 20 53 75 62 74 72 69 67 67 65 72 73 er. Subtriggers
1aad0 20 63 72 65 61 74 65 20 73 75 62 63 6f 6e 74 65 create subconte
1aae0 78 74 73 20 66 6f 72 20 74 68 65 69 72 20 64 75 xts for their du
1aaf0 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 5e 43 ration..**.** ^C
1ab00 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f alling [sqlite3_
1ab10 65 78 65 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 exec()] or [sqli
1ab20 74 65 33 5f 73 74 65 70 28 29 5d 20 72 65 63 75 te3_step()] recu
1ab30 72 73 69 76 65 6c 79 20 64 6f 65 73 0a 2a 2a 20 rsively does.**
1ab40 6e 6f 74 20 63 72 65 61 74 65 20 61 20 6e 65 77 not create a new
1ab50 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 trigger context
1ab60 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 66 75 ..**.** ^This fu
1ab70 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
1ab80 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 72 he number of dir
1ab90 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65 73 20 ect row changes
1aba0 69 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 72 in the.** most r
1abb0 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 ecent INSERT, UP
1abc0 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 20 DATE, or DELETE
1abd0 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e statement within
1abe0 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 69 the same.** tri
1abf0 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a gger context..**
1ac00 0a 2a 2a 20 5e 54 68 75 73 2c 20 77 68 65 6e 20 .** ^Thus, when
1ac10 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 called from the
1ac20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 top level, this
1ac30 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
1ac40 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f the.** number o
1ac50 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 f changes in the
1ac60 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53 most recent INS
1ac70 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 ERT, UPDATE, or
1ac80 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 61 DELETE.** that a
1ac90 6c 73 6f 20 6f 63 63 75 72 72 65 64 20 61 74 20 lso occurred at
1aca0 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2e 20 20 the top level.
1acb0 5e 28 57 69 74 68 69 6e 20 74 68 65 20 62 6f 64 ^(Within the bod
1acc0 79 20 6f 66 20 61 20 74 72 69 67 67 65 72 2c 0a y of a trigger,.
1acd0 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 ** the sqlite3_c
1ace0 68 61 6e 67 65 73 28 29 20 69 6e 74 65 72 66 61 hanges() interfa
1acf0 63 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 ce can be called
1ad00 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d to find the num
1ad10 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e 67 65 ber of.** change
1ad20 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 s in the most re
1ad30 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 cently completed
1ad40 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c INSERT, UPDATE,
1ad50 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73 74 or DELETE.** st
1ad60 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 atement within t
1ad70 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 73 he body of the s
1ad80 61 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a 20 ame trigger..**
1ad90 48 6f 77 65 76 65 72 2c 20 74 68 65 20 6e 75 6d However, the num
1ada0 62 65 72 20 72 65 74 75 72 6e 65 64 20 64 6f 65 ber returned doe
1adb0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 s not include ch
1adc0 61 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65 64 20 anges.** caused
1add0 62 79 20 73 75 62 74 72 69 67 67 65 72 73 20 73 by subtriggers s
1ade0 69 6e 63 65 20 74 68 6f 73 65 20 68 61 76 65 20 ince those have
1adf0 74 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74 65 78 their own contex
1ae00 74 2e 29 5e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 t.)^.**.** See a
1ae10 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 lso the [sqlite3
1ae20 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 _total_changes()
1ae30 5d 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 ] interface, the
1ae40 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 .** [count_chang
1ae50 65 73 20 70 72 61 67 6d 61 5d 2c 20 61 6e 64 20 es pragma], and
1ae60 74 68 65 20 5b 63 68 61 6e 67 65 73 28 29 20 53 the [changes() S
1ae70 51 4c 20 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2a QL function]..**
1ae80 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 .** If a separat
1ae90 65 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 e thread makes c
1aea0 68 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 hanges on the sa
1aeb0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
1aec0 65 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 ection.** while
1aed0 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 [sqlite3_changes
1aee0 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 ()] is running t
1aef0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 hen the value re
1af00 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 75 6e 70 turned.** is unp
1af10 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6e redictable and n
1af20 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a ot meaningful..*
1af30 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1af40 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
1af50 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a (sqlite3*);../*.
1af60 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 6f 74 ** CAPI3REF: Tot
1af70 61 6c 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 al Number Of Row
1af80 73 20 4d 6f 64 69 66 69 65 64 0a 2a 2a 0a 2a 2a s Modified.**.**
1af90 20 5e 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 ^This function
1afa0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
1afb0 65 72 20 6f 66 20 72 6f 77 20 63 68 61 6e 67 65 er of row change
1afc0 73 20 63 61 75 73 65 64 20 62 79 20 5b 49 4e 53 s caused by [INS
1afd0 45 52 54 5d 2c 0a 2a 2a 20 5b 55 50 44 41 54 45 ERT],.** [UPDATE
1afe0 5d 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 73 74 ] or [DELETE] st
1aff0 61 74 65 6d 65 6e 74 73 20 73 69 6e 63 65 20 74 atements since t
1b000 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
1b010 6e 65 63 74 69 6f 6e 5d 20 77 61 73 20 6f 70 65 nection] was ope
1b020 6e 65 64 2e 0a 2a 2a 20 5e 28 54 68 65 20 63 6f ned..** ^(The co
1b030 75 6e 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 unt returned by
1b040 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 sqlite3_total_ch
1b050 61 6e 67 65 73 28 29 20 69 6e 63 6c 75 64 65 73 anges() includes
1b060 20 61 6c 6c 20 63 68 61 6e 67 65 73 0a 2a 2a 20 all changes.**
1b070 66 72 6f 6d 20 61 6c 6c 20 5b 43 52 45 41 54 45 from all [CREATE
1b080 20 54 52 49 47 47 45 52 20 7c 20 74 72 69 67 67 TRIGGER | trigg
1b090 65 72 5d 20 63 6f 6e 74 65 78 74 73 20 61 6e 64 er] contexts and
1b0a0 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 changes made by
1b0b0 0a 2a 2a 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 .** [foreign key
1b0c0 20 61 63 74 69 6f 6e 73 5d 2e 20 48 6f 77 65 76 actions]. Howev
1b0d0 65 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 er,.** the count
1b0e0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 does not includ
1b0f0 65 20 63 68 61 6e 67 65 73 20 75 73 65 64 20 74 e changes used t
1b100 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 5b 52 45 50 o implement [REP
1b110 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74 LACE] constraint
1b120 73 2c 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 s,.** do rollbac
1b130 6b 73 20 6f 72 20 41 42 4f 52 54 20 70 72 6f 63 ks or ABORT proc
1b140 65 73 73 69 6e 67 2c 20 6f 72 20 5b 44 52 4f 50 essing, or [DROP
1b150 20 54 41 42 4c 45 5d 20 70 72 6f 63 65 73 73 69 TABLE] processi
1b160 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 75 6e ng. The.** coun
1b170 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 t does not inclu
1b180 64 65 20 72 6f 77 73 20 6f 66 20 76 69 65 77 73 de rows of views
1b190 20 74 68 61 74 20 66 69 72 65 20 61 6e 20 5b 49 that fire an [I
1b1a0 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 NSTEAD OF trigge
1b1b0 72 5d 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 69 66 r],.** though if
1b1c0 20 74 68 65 20 49 4e 53 54 45 41 44 20 4f 46 20 the INSTEAD OF
1b1d0 74 72 69 67 67 65 72 20 6d 61 6b 65 73 20 63 68 trigger makes ch
1b1e0 61 6e 67 65 73 20 6f 66 20 69 74 73 20 6f 77 6e anges of its own
1b1f0 2c 20 74 68 6f 73 65 20 63 68 61 6e 67 65 73 20 , those changes
1b200 0a 2a 2a 20 61 72 65 20 63 6f 75 6e 74 65 64 2e .** are counted.
1b210 29 5e 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 )^.** ^The sqlit
1b220 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
1b230 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6e () function coun
1b240 74 73 20 74 68 65 20 63 68 61 6e 67 65 73 20 61 ts the changes a
1b250 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 74 68 65 s soon as.** the
1b260 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 statement that
1b270 6d 61 6b 65 73 20 74 68 65 6d 20 69 73 20 63 6f makes them is co
1b280 6d 70 6c 65 74 65 64 20 28 77 68 65 6e 20 74 68 mpleted (when th
1b290 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 e statement hand
1b2a0 6c 65 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20 le.** is passed
1b2b0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 to [sqlite3_rese
1b2c0 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 t()] or [sqlite3
1b2d0 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a _finalize()])..*
1b2e0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 *.** See also th
1b2f0 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 e [sqlite3_chang
1b300 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2c es()] interface,
1b310 20 74 68 65 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 the.** [count_c
1b320 68 61 6e 67 65 73 20 70 72 61 67 6d 61 5d 2c 20 hanges pragma],
1b330 61 6e 64 20 74 68 65 20 5b 74 6f 74 61 6c 5f 63 and the [total_c
1b340 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e hanges() SQL fun
1b350 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 ction]..**.** If
1b360 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 65 a separate thre
1b370 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 ad makes changes
1b380 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 on the same dat
1b390 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1b3a0 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 .** while [sqlit
1b3b0 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
1b3c0 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 ()] is running t
1b3d0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a hen the value.**
1b3e0 20 72 65 74 75 72 6e 65 64 20 69 73 20 75 6e 70 returned is unp
1b3f0 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6e redictable and n
1b400 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a ot meaningful..*
1b410 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1b420 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 sqlite3_total_c
1b430 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 hanges(sqlite3*)
1b440 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
1b450 46 3a 20 49 6e 74 65 72 72 75 70 74 20 41 20 4c F: Interrupt A L
1b460 6f 6e 67 2d 52 75 6e 6e 69 6e 67 20 51 75 65 72 ong-Running Quer
1b470 79 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 66 75 y.**.** ^This fu
1b480 6e 63 74 69 6f 6e 20 63 61 75 73 65 73 20 61 6e nction causes an
1b490 79 20 70 65 6e 64 69 6e 67 20 64 61 74 61 62 61 y pending databa
1b4a0 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 se operation to
1b4b0 61 62 6f 72 74 20 61 6e 64 0a 2a 2a 20 72 65 74 abort and.** ret
1b4c0 75 72 6e 20 61 74 20 69 74 73 20 65 61 72 6c 69 urn at its earli
1b4d0 65 73 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e est opportunity.
1b4e0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
1b4f0 20 74 79 70 69 63 61 6c 6c 79 0a 2a 2a 20 63 61 typically.** ca
1b500 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 lled in response
1b510 20 74 6f 20 61 20 75 73 65 72 20 61 63 74 69 6f to a user actio
1b520 6e 20 73 75 63 68 20 61 73 20 70 72 65 73 73 69 n such as pressi
1b530 6e 67 20 22 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f ng "Cancel".** o
1b540 72 20 43 74 72 6c 2d 43 20 77 68 65 72 65 20 74 r Ctrl-C where t
1b550 68 65 20 75 73 65 72 20 77 61 6e 74 73 20 61 20 he user wants a
1b560 6c 6f 6e 67 20 71 75 65 72 79 20 6f 70 65 72 61 long query opera
1b570 74 69 6f 6e 20 74 6f 20 68 61 6c 74 0a 2a 2a 20 tion to halt.**
1b580 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a immediately..**.
1b590 2a 2a 20 5e 49 74 20 69 73 20 73 61 66 65 20 74 ** ^It is safe t
1b5a0 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 o call this rout
1b5b0 69 6e 65 20 66 72 6f 6d 20 61 20 74 68 72 65 61 ine from a threa
1b5c0 64 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d d different from
1b5d0 20 74 68 65 0a 2a 2a 20 74 68 72 65 61 64 20 74 the.** thread t
1b5e0 68 61 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 hat is currently
1b5f0 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 64 61 74 running the dat
1b600 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 2e abase operation.
1b610 20 20 42 75 74 20 69 74 0a 2a 2a 20 69 73 20 6e But it.** is n
1b620 6f 74 20 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 ot safe to call
1b630 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 this routine wit
1b640 68 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f h a [database co
1b650 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61 74 0a 2a nnection] that.*
1b660 2a 20 69 73 20 63 6c 6f 73 65 64 20 6f 72 20 6d * is closed or m
1b670 69 67 68 74 20 63 6c 6f 73 65 20 62 65 66 6f 72 ight close befor
1b680 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 e sqlite3_interr
1b690 75 70 74 28 29 20 72 65 74 75 72 6e 73 2e 0a 2a upt() returns..*
1b6a0 2a 0a 2a 2a 20 5e 49 66 20 61 6e 20 53 51 4c 20 *.** ^If an SQL
1b6b0 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 76 65 72 operation is ver
1b6c0 79 20 6e 65 61 72 6c 79 20 66 69 6e 69 73 68 65 y nearly finishe
1b6d0 64 20 61 74 20 74 68 65 20 74 69 6d 65 20 77 68 d at the time wh
1b6e0 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e en.** sqlite3_in
1b6f0 74 65 72 72 75 70 74 28 29 20 69 73 20 63 61 6c terrupt() is cal
1b700 6c 65 64 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 led, then it mig
1b710 68 74 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 6f ht not have an o
1b720 70 70 6f 72 74 75 6e 69 74 79 0a 2a 2a 20 74 6f pportunity.** to
1b730 20 62 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 be interrupted
1b740 61 6e 64 20 6d 69 67 68 74 20 63 6f 6e 74 69 6e and might contin
1b750 75 65 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e ue to completion
1b760 2e 0a 2a 2a 0a 2a 2a 20 5e 41 6e 20 53 51 4c 20 ..**.** ^An SQL
1b770 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 69 operation that i
1b780 73 20 69 6e 74 65 72 72 75 70 74 65 64 20 77 69 s interrupted wi
1b790 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 ll return [SQLIT
1b7a0 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e 0a 2a 2a E_INTERRUPT]..**
1b7b0 20 5e 49 66 20 74 68 65 20 69 6e 74 65 72 72 75 ^If the interru
1b7c0 70 74 65 64 20 53 51 4c 20 6f 70 65 72 61 74 69 pted SQL operati
1b7d0 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52 54 2c on is an INSERT,
1b7e0 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 UPDATE, or DELE
1b7f0 54 45 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e TE.** that is in
1b800 73 69 64 65 20 61 6e 20 65 78 70 6c 69 63 69 74 side an explicit
1b810 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 transaction, th
1b820 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 en the entire tr
1b830 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c ansaction.** wil
1b840 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b l be rolled back
1b850 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a automatically..
1b860 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 **.** ^The sqlit
1b870 65 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29 20 e3_interrupt(D)
1b880 63 61 6c 6c 20 69 73 20 69 6e 20 65 66 66 65 63 call is in effec
1b890 74 20 75 6e 74 69 6c 20 61 6c 6c 20 63 75 72 72 t until all curr
1b8a0 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 0a 2a 2a ently running.**
1b8b0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 SQL statements
1b8c0 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e on [database con
1b8d0 6e 65 63 74 69 6f 6e 5d 20 44 20 63 6f 6d 70 6c nection] D compl
1b8e0 65 74 65 2e 20 20 5e 41 6e 79 20 6e 65 77 20 53 ete. ^Any new S
1b8f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a QL statements.**
1b900 20 74 68 61 74 20 61 72 65 20 73 74 61 72 74 65 that are starte
1b910 64 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 d after the sqli
1b920 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 te3_interrupt()
1b930 63 61 6c 6c 20 61 6e 64 20 62 65 66 6f 72 65 20 call and before
1b940 74 68 65 20 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 the .** running
1b950 73 74 61 74 65 6d 65 6e 74 73 20 72 65 61 63 68 statements reach
1b960 65 73 20 7a 65 72 6f 20 61 72 65 20 69 6e 74 65 es zero are inte
1b970 72 72 75 70 74 65 64 20 61 73 20 69 66 20 74 68 rrupted as if th
1b980 65 79 20 68 61 64 20 62 65 65 6e 0a 2a 2a 20 72 ey had been.** r
1b990 75 6e 6e 69 6e 67 20 70 72 69 6f 72 20 74 6f 20 unning prior to
1b9a0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 the sqlite3_inte
1b9b0 72 72 75 70 74 28 29 20 63 61 6c 6c 2e 20 20 5e rrupt() call. ^
1b9c0 4e 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e New SQL statemen
1b9d0 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 ts.** that are s
1b9e0 74 61 72 74 65 64 20 61 66 74 65 72 20 74 68 65 tarted after the
1b9f0 20 72 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 running stateme
1ba00 6e 74 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 nt count reaches
1ba10 20 7a 65 72 6f 20 61 72 65 0a 2a 2a 20 6e 6f 74 zero are.** not
1ba20 20 65 66 66 65 63 74 65 64 20 62 79 20 74 68 65 effected by the
1ba30 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
1ba40 70 74 28 29 2e 0a 2a 2a 20 5e 41 20 63 61 6c 6c pt()..** ^A call
1ba50 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 to sqlite3_inte
1ba60 72 72 75 70 74 28 44 29 20 74 68 61 74 20 6f 63 rrupt(D) that oc
1ba70 63 75 72 73 20 77 68 65 6e 20 74 68 65 72 65 20 curs when there
1ba80 61 72 65 20 6e 6f 20 72 75 6e 6e 69 6e 67 0a 2a are no running.*
1ba90 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 * SQL statements
1baa0 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 is a no-op and
1bab0 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e has no effect on
1bac0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a SQL statements.
1bad0 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61 72 ** that are star
1bae0 74 65 64 20 61 66 74 65 72 20 74 68 65 20 73 71 ted after the sq
1baf0 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 lite3_interrupt(
1bb00 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a ) call returns..
1bb10 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 **.** If the dat
1bb20 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1bb30 20 63 6c 6f 73 65 73 20 77 68 69 6c 65 20 5b 73 closes while [s
1bb40 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
1bb50 28 29 5d 0a 2a 2a 20 69 73 20 72 75 6e 6e 69 6e ()].** is runnin
1bb60 67 20 74 68 65 6e 20 62 61 64 20 74 68 69 6e 67 g then bad thing
1bb70 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 s will likely ha
1bb80 70 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ppen..*/.SQLITE_
1bb90 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1bba0 5f 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 _interrupt(sqlit
1bbb0 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 e3*);../*.** CAP
1bbc0 49 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 I3REF: Determine
1bbd0 20 49 66 20 41 6e 20 53 51 4c 20 53 74 61 74 65 If An SQL State
1bbe0 6d 65 6e 74 20 49 73 20 43 6f 6d 70 6c 65 74 65 ment Is Complete
1bbf0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
1bc00 74 69 6e 65 73 20 61 72 65 20 75 73 65 66 75 6c tines are useful
1bc10 20 64 75 72 69 6e 67 20 63 6f 6d 6d 61 6e 64 2d during command-
1bc20 6c 69 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 65 line input to de
1bc30 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 0a 2a termine if the.*
1bc40 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 * currently ente
1bc50 72 65 64 20 74 65 78 74 20 73 65 65 6d 73 20 74 red text seems t
1bc60 6f 20 66 6f 72 6d 20 61 20 63 6f 6d 70 6c 65 74 o form a complet
1bc70 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
1bc80 6f 72 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 6f or.** if additio
1bc90 6e 61 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 65 nal input is nee
1bca0 64 65 64 20 62 65 66 6f 72 65 20 73 65 6e 64 69 ded before sendi
1bcb0 6e 67 20 74 68 65 20 74 65 78 74 20 69 6e 74 6f ng the text into
1bcc0 0a 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70 .** SQLite for p
1bcd0 61 72 73 69 6e 67 2e 20 20 5e 54 68 65 73 65 20 arsing. ^These
1bce0 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 routines return
1bcf0 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 73 1 if the input s
1bd00 74 72 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72 73 tring.** appears
1bd10 20 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 74 to be a complet
1bd20 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e e SQL statement.
1bd30 20 20 5e 41 20 73 74 61 74 65 6d 65 6e 74 20 69 ^A statement i
1bd40 73 20 6a 75 64 67 65 64 20 74 6f 20 62 65 0a 2a s judged to be.*
1bd50 2a 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74 * complete if it
1bd60 20 65 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d ends with a sem
1bd70 69 63 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 icolon token and
1bd80 20 69 73 20 6e 6f 74 20 61 20 70 72 65 66 69 78 is not a prefix
1bd90 20 6f 66 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f of a.** well-fo
1bda0 72 6d 65 64 20 43 52 45 41 54 45 20 54 52 49 47 rmed CREATE TRIG
1bdb0 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 GER statement.
1bdc0 5e 53 65 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74 ^Semicolons that
1bdd0 20 61 72 65 20 65 6d 62 65 64 64 65 64 20 77 69 are embedded wi
1bde0 74 68 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c thin.** string l
1bdf0 69 74 65 72 61 6c 73 20 6f 72 20 71 75 6f 74 65 iterals or quote
1be00 64 20 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d d identifier nam
1be10 65 73 20 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61 es or comments a
1be20 72 65 20 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 re not.** indepe
1be30 6e 64 65 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68 ndent tokens (th
1be40 65 79 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 ey are part of t
1be50 68 65 20 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63 he token in whic
1be60 68 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d h they are.** em
1be70 62 65 64 64 65 64 29 20 61 6e 64 20 74 68 75 73 bedded) and thus
1be80 20 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 do not count as
1be90 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 65 72 a statement ter
1bea0 6d 69 6e 61 74 6f 72 2e 20 20 5e 57 68 69 74 65 minator. ^White
1beb0 73 70 61 63 65 0a 2a 2a 20 61 6e 64 20 63 6f 6d space.** and com
1bec0 6d 65 6e 74 73 20 74 68 61 74 20 66 6f 6c 6c 6f ments that follo
1bed0 77 20 74 68 65 20 66 69 6e 61 6c 20 73 65 6d 69 w the final semi
1bee0 63 6f 6c 6f 6e 20 61 72 65 20 69 67 6e 6f 72 65 colon are ignore
1bef0 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 73 65 20 d..**.** ^These
1bf00 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 routines return
1bf10 30 20 69 66 20 74 68 65 20 73 74 61 74 65 6d 65 0 if the stateme
1bf20 6e 74 20 69 73 20 69 6e 63 6f 6d 70 6c 65 74 65 nt is incomplete
1bf30 2e 20 20 5e 49 66 20 61 0a 2a 2a 20 6d 65 6d 6f . ^If a.** memo
1bf40 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 ry allocation fa
1bf50 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 ils, then SQLITE
1bf60 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e _NOMEM is return
1bf70 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 73 65 ed..**.** ^These
1bf80 20 72 6f 75 74 69 6e 65 73 20 64 6f 20 6e 6f 74 routines do not
1bf90 20 70 61 72 73 65 20 74 68 65 20 53 51 4c 20 73 parse the SQL s
1bfa0 74 61 74 65 6d 65 6e 74 73 20 74 68 75 73 0a 2a tatements thus.*
1bfb0 2a 20 77 69 6c 6c 20 6e 6f 74 20 64 65 74 65 63 * will not detec
1bfc0 74 20 73 79 6e 74 61 63 74 69 63 61 6c 6c 79 20 t syntactically
1bfd0 69 6e 63 6f 72 72 65 63 74 20 53 51 4c 2e 0a 2a incorrect SQL..*
1bfe0 2a 0a 2a 2a 20 5e 28 49 66 20 53 51 4c 69 74 65 *.** ^(If SQLite
1bff0 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e has not been in
1c000 69 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 itialized using
1c010 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c [sqlite3_initial
1c020 69 7a 65 28 29 5d 20 70 72 69 6f 72 20 0a 2a 2a ize()] prior .**
1c030 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c to invoking sql
1c040 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 ite3_complete16(
1c050 29 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 69 ) then sqlite3_i
1c060 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 nitialize() is i
1c070 6e 76 6f 6b 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 nvoked.** automa
1c080 74 69 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 tically by sqlit
1c090 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 2e e3_complete16().
1c0a0 20 20 49 66 20 74 68 61 74 20 69 6e 69 74 69 61 If that initia
1c0b0 6c 69 7a 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a lization fails,.
1c0c0 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 ** then the retu
1c0d0 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 rn value from sq
1c0e0 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 lite3_complete16
1c0f0 28 29 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 7a () will be non-z
1c100 65 72 6f 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73 ero.** regardles
1c110 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 s of whether or
1c120 6e 6f 74 20 74 68 65 20 69 6e 70 75 74 20 53 51 not the input SQ
1c130 4c 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 29 5e L is complete.)^
1c140 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 .**.** The input
1c150 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d to [sqlite3_com
1c160 70 6c 65 74 65 28 29 5d 20 6d 75 73 74 20 62 65 plete()] must be
1c170 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 a zero-terminat
1c180 65 64 0a 2a 2a 20 55 54 46 2d 38 20 73 74 72 69 ed.** UTF-8 stri
1c190 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e ng..**.** The in
1c1a0 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f put to [sqlite3_
1c1b0 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d 20 6d 75 complete16()] mu
1c1c0 73 74 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 st be a zero-ter
1c1d0 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31 minated.** UTF-1
1c1e0 36 20 73 74 72 69 6e 67 20 69 6e 20 6e 61 74 69 6 string in nati
1c1f0 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a ve byte order..*
1c200 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1c210 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 sqlite3_complet
1c220 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 e(const char *sq
1c230 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 l);.SQLITE_API i
1c240 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c nt sqlite3_compl
1c250 65 74 65 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 ete16(const void
1c260 20 2a 73 71 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 *sql);../*.** C
1c270 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65 API3REF: Registe
1c280 72 20 41 20 43 61 6c 6c 62 61 63 6b 20 54 6f 20 r A Callback To
1c290 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f 42 55 Handle SQLITE_BU
1c2a0 53 59 20 45 72 72 6f 72 73 0a 2a 2a 0a 2a 2a 20 SY Errors.**.**
1c2b0 5e 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 ^This routine se
1c2c0 74 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 ts a callback fu
1c2d0 6e 63 74 69 6f 6e 20 74 68 61 74 20 6d 69 67 68 nction that migh
1c2e0 74 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 t be invoked whe
1c2f0 6e 65 76 65 72 0a 2a 2a 20 61 6e 20 61 74 74 65 never.** an atte
1c300 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f mpt is made to o
1c310 70 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 pen a database t
1c320 61 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 able that anothe
1c330 72 20 74 68 72 65 61 64 0a 2a 2a 20 6f 72 20 70 r thread.** or p
1c340 72 6f 63 65 73 73 20 68 61 73 20 6c 6f 63 6b 65 rocess has locke
1c350 64 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 74 68 65 d..**.** ^If the
1c360 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 busy callback i
1c370 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 5b 53 51 s NULL, then [SQ
1c380 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 LITE_BUSY] or [S
1c390 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 QLITE_IOERR_BLOC
1c3a0 4b 45 44 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 KED].** is retur
1c3b0 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 ned immediately
1c3c0 75 70 6f 6e 20 65 6e 63 6f 75 6e 74 65 72 69 6e upon encounterin
1c3d0 67 20 74 68 65 20 6c 6f 63 6b 2e 20 20 5e 49 66 g the lock. ^If
1c3e0 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 the busy callba
1c3f0 63 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c ck.** is not NUL
1c400 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c L, then the call
1c410 62 61 63 6b 20 6d 69 67 68 74 20 62 65 20 69 6e back might be in
1c420 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20 61 voked with two a
1c430 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 rguments..**.**
1c440 5e 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d ^The first argum
1c450 65 6e 74 20 74 6f 20 74 68 65 20 62 75 73 79 20 ent to the busy
1c460 68 61 6e 64 6c 65 72 20 69 73 20 61 20 63 6f 70 handler is a cop
1c470 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 y of the void* p
1c480 6f 69 6e 74 65 72 20 77 68 69 63 68 0a 2a 2a 20 ointer which.**
1c490 69 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67 is the third arg
1c4a0 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 ument to sqlite3
1c4b0 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 2e _busy_handler().
1c4c0 20 20 5e 54 68 65 20 73 65 63 6f 6e 64 20 61 72 ^The second ar
1c4d0 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 gument to.** the
1c4e0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 63 61 busy handler ca
1c4f0 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e 75 llback is the nu
1c500 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 mber of times th
1c510 61 74 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 at the busy hand
1c520 6c 65 72 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 ler has.** been
1c530 69 6e 76 6f 6b 65 64 20 66 6f 72 20 74 68 69 73 invoked for this
1c540 20 6c 6f 63 6b 69 6e 67 20 65 76 65 6e 74 2e 20 locking event.
1c550 20 5e 49 66 20 74 68 65 0a 2a 2a 20 62 75 73 79 ^If the.** busy
1c560 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e callback return
1c570 73 20 30 2c 20 74 68 65 6e 20 6e 6f 20 61 64 64 s 0, then no add
1c580 69 74 69 6f 6e 61 6c 20 61 74 74 65 6d 70 74 73 itional attempts
1c590 20 61 72 65 20 6d 61 64 65 20 74 6f 0a 2a 2a 20 are made to.**
1c5a0 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 62 access the datab
1c5b0 61 73 65 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f ase and [SQLITE_
1c5c0 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 BUSY] or [SQLITE
1c5d0 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 _IOERR_BLOCKED]
1c5e0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 is returned..**
1c5f0 5e 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b ^If the callback
1c600 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 returns non-zer
1c610 6f 2c 20 74 68 65 6e 20 61 6e 6f 74 68 65 72 20 o, then another
1c620 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 attempt.** is ma
1c630 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 64 de to open the d
1c640 61 74 61 62 61 73 65 20 66 6f 72 20 72 65 61 64 atabase for read
1c650 69 6e 67 20 61 6e 64 20 74 68 65 20 63 79 63 6c ing and the cycl
1c660 65 20 72 65 70 65 61 74 73 2e 0a 2a 2a 0a 2a 2a e repeats..**.**
1c670 20 54 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 The presence of
1c680 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 a busy handler
1c690 64 6f 65 73 20 6e 6f 74 20 67 75 61 72 61 6e 74 does not guarant
1c6a0 65 65 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 ee that it will
1c6b0 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 be invoked.** wh
1c6c0 65 6e 20 74 68 65 72 65 20 69 73 20 6c 6f 63 6b en there is lock
1c6d0 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 20 5e 49 66 contention. ^If
1c6e0 20 53 51 4c 69 74 65 20 64 65 74 65 72 6d 69 6e SQLite determin
1c6f0 65 73 20 74 68 61 74 20 69 6e 76 6f 6b 69 6e 67 es that invoking
1c700 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 68 61 6e the busy.** han
1c710 64 6c 65 72 20 63 6f 75 6c 64 20 72 65 73 75 6c dler could resul
1c720 74 20 69 6e 20 61 20 64 65 61 64 6c 6f 63 6b 2c t in a deadlock,
1c730 20 69 74 20 77 69 6c 6c 20 67 6f 20 61 68 65 61 it will go ahea
1c740 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 d and return [SQ
1c750 4c 49 54 45 5f 42 55 53 59 5d 0a 2a 2a 20 6f 72 LITE_BUSY].** or
1c760 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 [SQLITE_IOERR_B
1c770 4c 4f 43 4b 45 44 5d 20 69 6e 73 74 65 61 64 20 LOCKED] instead
1c780 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 of invoking the
1c790 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a busy handler..**
1c7a0 20 43 6f 6e 73 69 64 65 72 20 61 20 73 63 65 6e Consider a scen
1c7b0 61 72 69 6f 20 77 68 65 72 65 20 6f 6e 65 20 70 ario where one p
1c7c0 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e rocess is holdin
1c7d0 67 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 74 68 g a read lock th
1c7e0 61 74 0a 2a 2a 20 69 74 20 69 73 20 74 72 79 69 at.** it is tryi
1c7f0 6e 67 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f ng to promote to
1c800 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b a reserved lock
1c810 20 61 6e 64 0a 2a 2a 20 61 20 73 65 63 6f 6e 64 and.** a second
1c820 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 process is hold
1c830 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 20 6c ing a reserved l
1c840 6f 63 6b 20 74 68 61 74 20 69 74 20 69 73 20 74 ock that it is t
1c850 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 70 72 6f 6d rying.** to prom
1c860 6f 74 65 20 74 6f 20 61 6e 20 65 78 63 6c 75 73 ote to an exclus
1c870 69 76 65 20 6c 6f 63 6b 2e 20 20 54 68 65 20 66 ive lock. The f
1c880 69 72 73 74 20 70 72 6f 63 65 73 73 20 63 61 6e irst process can
1c890 6e 6f 74 20 70 72 6f 63 65 65 64 0a 2a 2a 20 62 not proceed.** b
1c8a0 65 63 61 75 73 65 20 69 74 20 69 73 20 62 6c 6f ecause it is blo
1c8b0 63 6b 65 64 20 62 79 20 74 68 65 20 73 65 63 6f cked by the seco
1c8c0 6e 64 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e nd and the secon
1c8d0 64 20 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74 d process cannot
1c8e0 0a 2a 2a 20 70 72 6f 63 65 65 64 20 62 65 63 61 .** proceed beca
1c8f0 75 73 65 20 69 74 20 69 73 20 62 6c 6f 63 6b 65 use it is blocke
1c900 64 20 62 79 20 74 68 65 20 66 69 72 73 74 2e 20 d by the first.
1c910 20 49 66 20 62 6f 74 68 20 70 72 6f 63 65 73 73 If both process
1c920 65 73 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 es.** invoke the
1c930 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2c 20 busy handlers,
1c940 6e 65 69 74 68 65 72 20 77 69 6c 6c 20 6d 61 6b neither will mak
1c950 65 20 61 6e 79 20 70 72 6f 67 72 65 73 73 2e 20 e any progress.
1c960 20 54 68 65 72 65 66 6f 72 65 2c 0a 2a 2a 20 53 Therefore,.** S
1c970 51 4c 69 74 65 20 72 65 74 75 72 6e 73 20 5b 53 QLite returns [S
1c980 51 4c 49 54 45 5f 42 55 53 59 5d 20 66 6f 72 20 QLITE_BUSY] for
1c990 74 68 65 20 66 69 72 73 74 20 70 72 6f 63 65 73 the first proces
1c9a0 73 2c 20 68 6f 70 69 6e 67 20 74 68 61 74 20 74 s, hoping that t
1c9b0 68 69 73 0a 2a 2a 20 77 69 6c 6c 20 69 6e 64 75 his.** will indu
1c9c0 63 65 20 74 68 65 20 66 69 72 73 74 20 70 72 6f ce the first pro
1c9d0 63 65 73 73 20 74 6f 20 72 65 6c 65 61 73 65 20 cess to release
1c9e0 69 74 73 20 72 65 61 64 20 6c 6f 63 6b 20 61 6e its read lock an
1c9f0 64 20 61 6c 6c 6f 77 0a 2a 2a 20 74 68 65 20 73 d allow.** the s
1ca00 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 74 6f econd process to
1ca10 20 70 72 6f 63 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 proceed..**.**
1ca20 5e 54 68 65 20 64 65 66 61 75 6c 74 20 62 75 73 ^The default bus
1ca30 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 y callback is NU
1ca40 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 5b LL..**.** ^The [
1ca50 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72 SQLITE_BUSY] err
1ca60 6f 72 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 or is converted
1ca70 74 6f 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 to [SQLITE_IOERR
1ca80 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 77 68 65 _BLOCKED].** whe
1ca90 6e 20 53 51 4c 69 74 65 20 69 73 20 69 6e 20 74 n SQLite is in t
1caa0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 6c he middle of a l
1cab0 61 72 67 65 20 74 72 61 6e 73 61 63 74 69 6f 6e arge transaction
1cac0 20 77 68 65 72 65 20 61 6c 6c 20 74 68 65 0a 2a where all the.*
1cad0 2a 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 6e * changes will n
1cae0 6f 74 20 66 69 74 20 69 6e 74 6f 20 74 68 65 20 ot fit into the
1caf0 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e in-memory cache.
1cb00 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a SQLite will.**
1cb10 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 already hold a
1cb20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e RESERVED lock on
1cb30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1cb40 6c 65 2c 20 62 75 74 20 69 74 20 6e 65 65 64 73 le, but it needs
1cb50 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 .** to promote t
1cb60 68 69 73 20 6c 6f 63 6b 20 74 6f 20 45 58 43 4c his lock to EXCL
1cb70 55 53 49 56 45 20 73 6f 20 74 68 61 74 20 69 74 USIVE so that it
1cb80 20 63 61 6e 20 73 70 69 6c 6c 20 63 61 63 68 65 can spill cache
1cb90 0a 2a 2a 20 70 61 67 65 73 20 69 6e 74 6f 20 74 .** pages into t
1cba0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1cbb0 20 77 69 74 68 6f 75 74 20 68 61 72 6d 20 74 6f without harm to
1cbc0 20 63 6f 6e 63 75 72 72 65 6e 74 0a 2a 2a 20 72 concurrent.** r
1cbd0 65 61 64 65 72 73 2e 20 20 5e 49 66 20 69 74 20 eaders. ^If it
1cbe0 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 70 72 6f is unable to pro
1cbf0 6d 6f 74 65 20 74 68 65 20 6c 6f 63 6b 2c 20 74 mote the lock, t
1cc00 68 65 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 hen the in-memor
1cc10 79 0a 2a 2a 20 63 61 63 68 65 20 77 69 6c 6c 20 y.** cache will
1cc20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e be left in an in
1cc30 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 consistent state
1cc40 20 61 6e 64 20 73 6f 20 74 68 65 20 65 72 72 6f and so the erro
1cc50 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20 70 72 6f r.** code is pro
1cc60 6d 6f 74 65 64 20 66 72 6f 6d 20 74 68 65 20 72 moted from the r
1cc70 65 6c 61 74 69 76 65 6c 79 20 62 65 6e 69 67 6e elatively benign
1cc80 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 74 [SQLITE_BUSY] t
1cc90 6f 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 65 o.** the more se
1cca0 76 65 72 65 20 5b 53 51 4c 49 54 45 5f 49 4f 45 vere [SQLITE_IOE
1ccb0 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e 20 20 5e 54 RR_BLOCKED]. ^T
1ccc0 68 69 73 20 65 72 72 6f 72 20 63 6f 64 65 20 70 his error code p
1ccd0 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a 20 66 6f 72 63 romotion.** forc
1cce0 65 73 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 es an automatic
1ccf0 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 20 rollback of the
1cd00 63 68 61 6e 67 65 73 2e 20 20 53 65 65 20 74 68 changes. See th
1cd10 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 2f 63 e.** <a href="/c
1cd20 76 73 74 72 61 63 2f 77 69 6b 69 3f 70 3d 43 6f vstrac/wiki?p=Co
1cd30 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e rruptionFollowin
1cd40 67 42 75 73 79 45 72 72 6f 72 22 3e 0a 2a 2a 20 gBusyError">.**
1cd50 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 CorruptionFollow
1cd60 69 6e 67 42 75 73 79 45 72 72 6f 72 3c 2f 61 3e ingBusyError</a>
1cd70 20 77 69 6b 69 20 70 61 67 65 20 66 6f 72 20 61 wiki page for a
1cd80 20 64 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 77 discussion of w
1cd90 68 79 0a 2a 2a 20 74 68 69 73 20 69 73 20 69 6d hy.** this is im
1cda0 70 6f 72 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 5e portant..**.** ^
1cdb0 28 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 (There can only
1cdc0 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79 be a single busy
1cdd0 20 68 61 6e 64 6c 65 72 20 64 65 66 69 6e 65 64 handler defined
1cde0 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 5b 64 61 for each.** [da
1cdf0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1ce00 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20 61 20 6e n]. Setting a n
1ce10 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 ew busy handler
1ce20 63 6c 65 61 72 73 20 61 6e 79 0a 2a 2a 20 70 72 clears any.** pr
1ce30 65 76 69 6f 75 73 6c 79 20 73 65 74 20 68 61 6e eviously set han
1ce40 64 6c 65 72 2e 29 5e 20 20 5e 4e 6f 74 65 20 74 dler.)^ ^Note t
1ce50 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c hat calling [sql
1ce60 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 ite3_busy_timeou
1ce70 74 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73 t()].** will als
1ce80 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 72 20 74 o set or clear t
1ce90 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e he busy handler.
1cea0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 20 .**.** The busy
1ceb0 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20 callback should
1cec0 6e 6f 74 20 74 61 6b 65 20 61 6e 79 20 61 63 74 not take any act
1ced0 69 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 66 ions which modif
1cee0 79 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 y the.** databas
1cef0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 e connection tha
1cf00 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 t invoked the bu
1cf10 73 79 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e 79 sy handler. Any
1cf20 20 73 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a 2a such actions.**
1cf30 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 66 result in undef
1cf40 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a ined behavior..*
1cf50 2a 20 0a 2a 2a 20 41 20 62 75 73 79 20 68 61 6e * .** A busy han
1cf60 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 63 6c dler must not cl
1cf70 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ose the database
1cf80 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f connection.** o
1cf90 72 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 r [prepared stat
1cfa0 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 6e 76 6f ement] that invo
1cfb0 6b 65 64 20 74 68 65 20 62 75 73 79 20 68 61 6e ked the busy han
1cfc0 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f dler..*/.SQLITE_
1cfd0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1cfe0 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 73 71 6c busy_handler(sql
1cff0 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f ite3*, int(*)(vo
1d000 69 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 64 2a 29 id*,int), void*)
1d010 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
1d020 46 3a 20 53 65 74 20 41 20 42 75 73 79 20 54 69 F: Set A Busy Ti
1d030 6d 65 6f 75 74 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 meout.**.** ^Thi
1d040 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 s routine sets a
1d050 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 [sqlite3_busy_h
1d060 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 20 68 61 andler | busy ha
1d070 6e 64 6c 65 72 5d 20 74 68 61 74 20 73 6c 65 65 ndler] that slee
1d080 70 73 0a 2a 2a 20 66 6f 72 20 61 20 73 70 65 63 ps.** for a spec
1d090 69 66 69 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 ified amount of
1d0a0 74 69 6d 65 20 77 68 65 6e 20 61 20 74 61 62 6c time when a tabl
1d0b0 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 5e 54 e is locked. ^T
1d0c0 68 65 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 69 he handler.** wi
1d0d0 6c 6c 20 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c ll sleep multipl
1d0e0 65 20 74 69 6d 65 73 20 75 6e 74 69 6c 20 61 74 e times until at
1d0f0 20 6c 65 61 73 74 20 22 6d 73 22 20 6d 69 6c 6c least "ms" mill
1d100 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 iseconds of slee
1d110 70 69 6e 67 0a 2a 2a 20 68 61 76 65 20 61 63 63 ping.** have acc
1d120 75 6d 75 6c 61 74 65 64 2e 20 20 5e 41 66 74 65 umulated. ^Afte
1d130 72 20 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20 r at least "ms"
1d140 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 milliseconds of
1d150 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a 20 74 68 65 sleeping,.** the
1d160 20 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 handler returns
1d170 20 30 20 77 68 69 63 68 20 63 61 75 73 65 73 20 0 which causes
1d180 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
1d190 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 to return.** [S
1d1a0 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b QLITE_BUSY] or [
1d1b0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f SQLITE_IOERR_BLO
1d1c0 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 43 61 CKED]..**.** ^Ca
1d1d0 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 lling this routi
1d1e0 6e 65 20 77 69 74 68 20 61 6e 20 61 72 67 75 6d ne with an argum
1d1f0 65 6e 74 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 ent less than or
1d200 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 0a 2a equal to zero.*
1d210 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 6c 6c 20 * turns off all
1d220 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2e 0a 2a busy handlers..*
1d230 2a 0a 2a 2a 20 5e 28 54 68 65 72 65 20 63 61 6e *.** ^(There can
1d240 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c only be a singl
1d250 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 e busy handler f
1d260 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a or a particular.
1d270 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
1d280 6e 65 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e 79 nection] any any
1d290 20 67 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20 given moment.
1d2a0 49 66 20 61 6e 6f 74 68 65 72 20 62 75 73 79 20 If another busy
1d2b0 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20 64 handler.** was d
1d2c0 65 66 69 6e 65 64 20 20 28 75 73 69 6e 67 20 5b efined (using [
1d2d0 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e sqlite3_busy_han
1d2e0 64 6c 65 72 28 29 5d 29 20 70 72 69 6f 72 20 74 dler()]) prior t
1d2f0 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 o calling.** thi
1d300 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 20 s routine, that
1d310 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c other busy handl
1d320 65 72 20 69 73 20 63 6c 65 61 72 65 64 2e 29 5e er is cleared.)^
1d330 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1d340 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f nt sqlite3_busy_
1d350 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 2a timeout(sqlite3*
1d360 2c 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a , int ms);../*.*
1d370 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 76 * CAPI3REF: Conv
1d380 65 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 73 enience Routines
1d390 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 65 For Running Que
1d3a0 72 69 65 73 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ries.**.** This
1d3b0 69 73 20 61 20 6c 65 67 61 63 79 20 69 6e 74 65 is a legacy inte
1d3c0 72 66 61 63 65 20 74 68 61 74 20 69 73 20 70 72 rface that is pr
1d3d0 65 73 65 72 76 65 64 20 66 6f 72 20 62 61 63 6b eserved for back
1d3e0 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c wards compatibil
1d3f0 69 74 79 2e 0a 2a 2a 20 55 73 65 20 6f 66 20 74 ity..** Use of t
1d400 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 his interface is
1d410 20 6e 6f 74 20 72 65 63 6f 6d 6d 65 6e 64 65 64 not recommended
1d420 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 69 74 69 ..**.** Definiti
1d430 6f 6e 3a 20 41 20 3c 62 3e 72 65 73 75 6c 74 20 on: A <b>result
1d440 74 61 62 6c 65 3c 2f 62 3e 20 69 73 20 6d 65 6d table</b> is mem
1d450 6f 72 79 20 64 61 74 61 20 73 74 72 75 63 74 75 ory data structu
1d460 72 65 20 63 72 65 61 74 65 64 20 62 79 20 74 68 re created by th
1d470 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 e.** [sqlite3_ge
1d480 74 5f 74 61 62 6c 65 28 29 5d 20 69 6e 74 65 72 t_table()] inter
1d490 66 61 63 65 2e 20 20 41 20 72 65 73 75 6c 74 20 face. A result
1d4a0 74 61 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68 table records th
1d4b0 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 20 71 75 e.** complete qu
1d4c0 65 72 79 20 72 65 73 75 6c 74 73 20 66 72 6f 6d ery results from
1d4d0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 71 75 65 one or more que
1d4e0 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ries..**.** The
1d4f0 74 61 62 6c 65 20 63 6f 6e 63 65 70 74 75 61 6c table conceptual
1d500 6c 79 20 68 61 73 20 61 20 6e 75 6d 62 65 72 20 ly has a number
1d510 6f 66 20 72 6f 77 73 20 61 6e 64 20 63 6f 6c 75 of rows and colu
1d520 6d 6e 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 mns. But.** the
1d530 73 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6e se numbers are n
1d540 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 72 ot part of the r
1d550 65 73 75 6c 74 20 74 61 62 6c 65 20 69 74 73 65 esult table itse
1d560 6c 66 2e 20 20 54 68 65 73 65 0a 2a 2a 20 6e 75 lf. These.** nu
1d570 6d 62 65 72 73 20 61 72 65 20 6f 62 74 61 69 6e mbers are obtain
1d580 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e 20 20 ed separately.
1d590 4c 65 74 20 4e 20 62 65 20 74 68 65 20 6e 75 6d Let N be the num
1d5a0 62 65 72 20 6f 66 20 72 6f 77 73 0a 2a 2a 20 61 ber of rows.** a
1d5b0 6e 64 20 4d 20 62 65 20 74 68 65 20 6e 75 6d 62 nd M be the numb
1d5c0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 0a 2a er of columns..*
1d5d0 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61 *.** A result ta
1d5e0 62 6c 65 20 69 73 20 61 6e 20 61 72 72 61 79 20 ble is an array
1d5f0 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 7a of pointers to z
1d600 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 ero-terminated U
1d610 54 46 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a TF-8 strings..**
1d620 20 54 68 65 72 65 20 61 72 65 20 28 4e 2b 31 29 There are (N+1)
1d630 2a 4d 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 *M elements in t
1d640 68 65 20 61 72 72 61 79 2e 20 20 54 68 65 20 66 he array. The f
1d650 69 72 73 74 20 4d 20 70 6f 69 6e 74 65 72 73 20 irst M pointers
1d660 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 7a 65 72 6f point.** to zero
1d670 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 -terminated stri
1d680 6e 67 73 20 74 68 61 74 20 20 63 6f 6e 74 61 69 ngs that contai
1d690 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 n the names of t
1d6a0 68 65 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 20 54 he columns..** T
1d6b0 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74 he remaining ent
1d6c0 72 69 65 73 20 61 6c 6c 20 70 6f 69 6e 74 20 74 ries all point t
1d6d0 6f 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 2e o query results.
1d6e0 20 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 72 65 NULL values re
1d6f0 73 75 6c 74 0a 2a 2a 20 69 6e 20 4e 55 4c 4c 20 sult.** in NULL
1d700 70 6f 69 6e 74 65 72 73 2e 20 20 41 6c 6c 20 6f pointers. All o
1d710 74 68 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 ther values are
1d720 69 6e 20 74 68 65 69 72 20 55 54 46 2d 38 20 7a in their UTF-8 z
1d730 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a ero-terminated.*
1d740 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 * string represe
1d750 6e 74 61 74 69 6f 6e 20 61 73 20 72 65 74 75 72 ntation as retur
1d760 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f ned by [sqlite3_
1d770 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2e 0a column_text()]..
1d780 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 **.** A result t
1d790 61 62 6c 65 20 6d 69 67 68 74 20 63 6f 6e 73 69 able might consi
1d7a0 73 74 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 st of one or mor
1d7b0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
1d7c0 69 6f 6e 73 2e 0a 2a 2a 20 49 74 20 69 73 20 6e ions..** It is n
1d7d0 6f 74 20 73 61 66 65 20 74 6f 20 70 61 73 73 20 ot safe to pass
1d7e0 61 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 64 a result table d
1d7f0 69 72 65 63 74 6c 79 20 74 6f 20 5b 73 71 6c 69 irectly to [sqli
1d800 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 20 te3_free()]..**
1d810 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 73 A result table s
1d820 68 6f 75 6c 64 20 62 65 20 64 65 61 6c 6c 6f 63 hould be dealloc
1d830 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 ated using [sqli
1d840 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 te3_free_table()
1d850 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 28 41 73 20 61 6e ]..**.** ^(As an
1d860 20 65 78 61 6d 70 6c 65 20 6f 66 20 74 68 65 20 example of the
1d870 72 65 73 75 6c 74 20 74 61 62 6c 65 20 66 6f 72 result table for
1d880 6d 61 74 2c 20 73 75 70 70 6f 73 65 20 61 20 71 mat, suppose a q
1d890 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20 69 uery result.** i
1d8a0 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
1d8b0 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e .** <blockquote>
1d8c0 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 <pre>.**
1d8d0 4e 61 6d 65 20 20 20 20 20 20 20 20 7c 20 41 67 Name | Ag
1d8e0 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d 2d 2d 2d e.** ----
1d8f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d900 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 20 41 6c ---.** Al
1d910 69 63 65 20 20 20 20 20 20 20 7c 20 34 33 0a 2a ice | 43.*
1d920 2a 20 20 20 20 20 20 20 20 42 6f 62 20 20 20 20 * Bob
1d930 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20 20 20 20 | 28.**
1d940 20 20 20 20 43 69 6e 64 79 20 20 20 20 20 20 20 Cindy
1d950 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f | 21.** </pre></
1d960 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a blockquote>.**.*
1d970 2a 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 20 * There are two
1d980 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29 20 61 6e column (M==2) an
1d990 64 20 74 68 72 65 65 20 72 6f 77 73 20 28 4e 3d d three rows (N=
1d9a0 3d 33 29 2e 20 20 54 68 75 73 20 74 68 65 0a 2a =3). Thus the.*
1d9b0 2a 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 68 * result table h
1d9c0 61 73 20 38 20 65 6e 74 72 69 65 73 2e 20 20 53 as 8 entries. S
1d9d0 75 70 70 6f 73 65 20 74 68 65 20 72 65 73 75 6c uppose the resul
1d9e0 74 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 t table is store
1d9f0 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 72 61 79 d.** in an array
1da00 20 6e 61 6d 65 73 20 61 7a 52 65 73 75 6c 74 2e names azResult.
1da10 20 20 54 68 65 6e 20 61 7a 52 65 73 75 6c 74 20 Then azResult
1da20 68 6f 6c 64 73 20 74 68 69 73 20 63 6f 6e 74 65 holds this conte
1da30 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b nt:.**.** <block
1da40 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 quote><pre>.**
1da50 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 azResult&#
1da60 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65 22 3b 0a 91;0] = "Name";.
1da70 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 ** azResu
1da80 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22 41 67 65 lt[1] = "Age
1da90 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 ";.** azR
1daa0 65 73 75 6c 74 26 23 39 31 3b 32 5d 20 3d 20 22 esult[2] = "
1dab0 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20 20 20 20 Alice";.**
1dac0 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 33 azResult[3
1dad0 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20 20 20 20 ] = "43";.**
1dae0 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 azResult[
1daf0 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a 2a 2a 20 ;4] = "Bob";.**
1db00 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 azResult&
1db10 23 39 31 3b 35 5d 20 3d 20 22 32 38 22 3b 0a 2a #91;5] = "28";.*
1db20 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c * azResul
1db30 74 26 23 39 31 3b 36 5d 20 3d 20 22 43 69 6e 64 t[6] = "Cind
1db40 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a y";.** az
1db50 52 65 73 75 6c 74 26 23 39 31 3b 37 5d 20 3d 20 Result[7] =
1db60 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c "21";.** </pre><
1db70 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 29 5e 0a 2a /blockquote>)^.*
1db80 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 *.** ^The sqlite
1db90 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66 75 3_get_table() fu
1dba0 6e 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 nction evaluates
1dbb0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 one or more.**
1dbc0 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61 semicolon-separa
1dbd0 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ted SQL statemen
1dbe0 74 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74 ts in the zero-t
1dbf0 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 0a erminated UTF-8.
1dc00 2a 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74 73 ** string of its
1dc10 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 2nd parameter a
1dc20 6e 64 20 72 65 74 75 72 6e 73 20 61 20 72 65 73 nd returns a res
1dc30 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 68 65 ult table to the
1dc40 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 76 65 .** pointer give
1dc50 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70 61 72 n in its 3rd par
1dc60 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 66 ameter..**.** Af
1dc70 74 65 72 20 74 68 65 20 61 70 70 6c 69 63 61 74 ter the applicat
1dc80 69 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 ion has finished
1dc90 20 77 69 74 68 20 74 68 65 20 72 65 73 75 6c 74 with the result
1dca0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65 from sqlite3_ge
1dcb0 74 5f 74 61 62 6c 65 28 29 2c 0a 2a 2a 20 69 74 t_table(),.** it
1dcc0 20 6d 75 73 74 20 70 61 73 73 20 74 68 65 20 72 must pass the r
1dcd0 65 73 75 6c 74 20 74 61 62 6c 65 20 70 6f 69 6e esult table poin
1dce0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 ter to sqlite3_f
1dcf0 72 65 65 5f 74 61 62 6c 65 28 29 20 69 6e 20 6f ree_table() in o
1dd00 72 64 65 72 20 74 6f 0a 2a 2a 20 72 65 6c 65 61 rder to.** relea
1dd10 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 se the memory th
1dd20 61 74 20 77 61 73 20 6d 61 6c 6c 6f 63 65 64 2e at was malloced.
1dd30 20 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 Because of the
1dd40 20 77 61 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c way the.** [sql
1dd50 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68 ite3_malloc()] h
1dd60 61 70 70 65 6e 73 20 77 69 74 68 69 6e 20 73 71 appens within sq
1dd70 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 lite3_get_table(
1dd80 29 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a ), the calling.*
1dd90 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 * function must
1dda0 6e 6f 74 20 74 72 79 20 74 6f 20 63 61 6c 6c 20 not try to call
1ddb0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
1ddc0 20 64 69 72 65 63 74 6c 79 2e 20 20 4f 6e 6c 79 directly. Only
1ddd0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 .** [sqlite3_fre
1dde0 65 5f 74 61 62 6c 65 28 29 5d 20 69 73 20 61 62 e_table()] is ab
1ddf0 6c 65 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 le to release th
1de00 65 20 6d 65 6d 6f 72 79 20 70 72 6f 70 65 72 6c e memory properl
1de10 79 20 61 6e 64 20 73 61 66 65 6c 79 2e 0a 2a 2a y and safely..**
1de20 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
1de30 67 65 74 5f 74 61 62 6c 65 28 29 20 69 6e 74 65 get_table() inte
1de40 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65 rface is impleme
1de50 6e 74 65 64 20 61 73 20 61 20 77 72 61 70 70 65 nted as a wrappe
1de60 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73 71 6c r around.** [sql
1de70 69 74 65 33 5f 65 78 65 63 28 29 5d 2e 20 20 54 ite3_exec()]. T
1de80 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 he sqlite3_get_t
1de90 61 62 6c 65 28 29 20 72 6f 75 74 69 6e 65 20 64 able() routine d
1dea0 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 63 63 oes not have acc
1deb0 65 73 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 69 6e ess.** to any in
1dec0 74 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 ternal data stru
1ded0 63 74 75 72 65 73 20 6f 66 20 53 51 4c 69 74 65 ctures of SQLite
1dee0 2e 20 20 49 74 20 75 73 65 73 20 6f 6e 6c 79 20 . It uses only
1def0 74 68 65 20 70 75 62 6c 69 63 0a 2a 2a 20 69 6e the public.** in
1df00 74 65 72 66 61 63 65 20 64 65 66 69 6e 65 64 20 terface defined
1df10 68 65 72 65 2e 20 20 41 73 20 61 20 63 6f 6e 73 here. As a cons
1df20 65 71 75 65 6e 63 65 2c 20 65 72 72 6f 72 73 20 equence, errors
1df30 74 68 61 74 20 6f 63 63 75 72 20 69 6e 20 74 68 that occur in th
1df40 65 0a 2a 2a 20 77 72 61 70 70 65 72 20 6c 61 79 e.** wrapper lay
1df50 65 72 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 er outside of th
1df60 65 20 69 6e 74 65 72 6e 61 6c 20 5b 73 71 6c 69 e internal [sqli
1df70 74 65 33 5f 65 78 65 63 28 29 5d 20 63 61 6c 6c te3_exec()] call
1df80 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 65 66 6c are not.** refl
1df90 65 63 74 65 64 20 69 6e 20 73 75 62 73 65 71 75 ected in subsequ
1dfa0 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 ent calls to [sq
1dfb0 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d lite3_errcode()]
1dfc0 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f or.** [sqlite3_
1dfd0 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2f 0a 53 51 errmsg()]..*/.SQ
1dfe0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1dff0 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a ite3_get_table(.
1e000 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
1e010 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 /* An op
1e020 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 en database */.
1e030 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 const char *zSq
1e040 6c 2c 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f l, /* SQL to
1e050 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f be evaluated */
1e060 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65 . char ***pazRe
1e070 73 75 6c 74 2c 20 20 20 20 2f 2a 20 52 65 73 75 sult, /* Resu
1e080 6c 74 73 20 6f 66 20 74 68 65 20 71 75 65 72 79 lts of the query
1e090 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 77 */. int *pnRow
1e0a0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
1e0b0 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 umber of result
1e0c0 72 6f 77 73 20 77 72 69 74 74 65 6e 20 68 65 72 rows written her
1e0d0 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f e */. int *pnCo
1e0e0 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 lumn, /*
1e0f0 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 Number of result
1e100 20 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74 65 6e columns written
1e110 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 here */. char
1e120 2a 2a 70 7a 45 72 72 6d 73 67 20 20 20 20 20 20 **pzErrmsg
1e130 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 /* Error msg wr
1e140 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b itten here */.);
1e150 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1e160 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 sqlite3_free_ta
1e170 62 6c 65 28 63 68 61 72 20 2a 2a 72 65 73 75 6c ble(char **resul
1e180 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
1e190 52 45 46 3a 20 46 6f 72 6d 61 74 74 65 64 20 53 REF: Formatted S
1e1a0 74 72 69 6e 67 20 50 72 69 6e 74 69 6e 67 20 46 tring Printing F
1e1b0 75 6e 63 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 54 unctions.**.** T
1e1c0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 hese routines ar
1e1d0 65 20 77 6f 72 6b 2d 61 6c 69 6b 65 73 20 6f 66 e work-alikes of
1e1e0 20 74 68 65 20 22 70 72 69 6e 74 66 28 29 22 20 the "printf()"
1e1f0 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63 74 69 family of functi
1e200 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 ons.** from the
1e210 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 standard C libra
1e220 72 79 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 ry..**.** ^The s
1e230 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 qlite3_mprintf()
1e240 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 6d 70 and sqlite3_vmp
1e250 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 73 rintf() routines
1e260 20 77 72 69 74 65 20 74 68 65 69 72 0a 2a 2a 20 write their.**
1e270 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d 65 6d results into mem
1e280 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
1e290 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f m [sqlite3_mallo
1e2a0 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72 c()]..** The str
1e2b0 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 ings returned by
1e2c0 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75 74 69 these two routi
1e2d0 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a nes should be.**
1e2e0 20 72 65 6c 65 61 73 65 64 20 62 79 20 5b 73 71 released by [sq
1e2f0 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 20 20 lite3_free()].
1e300 5e 42 6f 74 68 20 72 6f 75 74 69 6e 65 73 20 72 ^Both routines r
1e310 65 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 eturn a.** NULL
1e320 70 6f 69 6e 74 65 72 20 69 66 20 5b 73 71 6c 69 pointer if [sqli
1e330 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 73 te3_malloc()] is
1e340 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 unable to alloc
1e350 61 74 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d 65 ate enough.** me
1e360 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 mory to hold the
1e370 20 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e resulting strin
1e380 67 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 73 g..**.** ^(The s
1e390 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1e3a0 29 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d ) routine is sim
1e3b0 69 6c 61 72 20 74 6f 20 22 73 6e 70 72 69 6e 74 ilar to "snprint
1e3c0 66 28 29 22 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 f()" from.** the
1e3d0 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 standard C libr
1e3e0 61 72 79 2e 20 20 54 68 65 20 72 65 73 75 6c 74 ary. The result
1e3f0 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f is written into
1e400 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 73 the.** buffer s
1e410 75 70 70 6c 69 65 64 20 61 73 20 74 68 65 20 73 upplied as the s
1e420 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
1e430 77 68 6f 73 65 20 73 69 7a 65 20 69 73 20 67 69 whose size is gi
1e440 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 69 ven by.** the fi
1e450 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 4e rst parameter. N
1e460 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f 72 64 ote that the ord
1e470 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 er of the.** fir
1e480 73 74 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72 st two parameter
1e490 73 20 69 73 20 72 65 76 65 72 73 65 64 20 66 72 s is reversed fr
1e4a0 6f 6d 20 73 6e 70 72 69 6e 74 66 28 29 2e 29 5e om snprintf().)^
1e4b0 20 20 54 68 69 73 20 69 73 20 61 6e 0a 2a 2a 20 This is an.**
1e4c0 68 69 73 74 6f 72 69 63 61 6c 20 61 63 63 69 64 historical accid
1e4d0 65 6e 74 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 ent that cannot
1e4e0 62 65 20 66 69 78 65 64 20 77 69 74 68 6f 75 74 be fixed without
1e4f0 20 62 72 65 61 6b 69 6e 67 0a 2a 2a 20 62 61 63 breaking.** bac
1e500 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 kwards compatibi
1e510 6c 69 74 79 2e 20 20 5e 28 4e 6f 74 65 20 61 6c lity. ^(Note al
1e520 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f so that sqlite3_
1e530 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 72 65 snprintf().** re
1e540 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
1e550 74 6f 20 69 74 73 20 62 75 66 66 65 72 20 69 6e to its buffer in
1e560 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e 75 6d stead of the num
1e570 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61 63 ber of.** charac
1e580 74 65 72 73 20 61 63 74 75 61 6c 6c 79 20 77 72 ters actually wr
1e590 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 62 itten into the b
1e5a0 75 66 66 65 72 2e 29 5e 20 20 57 65 20 61 64 6d uffer.)^ We adm
1e5b0 69 74 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e it that.** the n
1e5c0 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
1e5d0 65 72 73 20 77 72 69 74 74 65 6e 20 77 6f 75 6c ers written woul
1e5e0 64 20 62 65 20 61 20 6d 6f 72 65 20 75 73 65 66 d be a more usef
1e5f0 75 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c ul return.** val
1e600 75 65 20 62 75 74 20 77 65 20 63 61 6e 6e 6f 74 ue but we cannot
1e610 20 63 68 61 6e 67 65 20 74 68 65 20 69 6d 70 6c change the impl
1e620 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 ementation of sq
1e630 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 lite3_snprintf()
1e640 0a 2a 2a 20 6e 6f 77 20 77 69 74 68 6f 75 74 20 .** now without
1e650 62 72 65 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69 breaking compati
1e660 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 5e 41 bility..**.** ^A
1e670 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 62 75 s long as the bu
1e680 66 66 65 72 20 73 69 7a 65 20 69 73 20 67 72 65 ffer size is gre
1e690 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 ater than zero,
1e6a0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1e6b0 28 29 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 73 ().** guarantees
1e6c0 20 74 68 61 74 20 74 68 65 20 62 75 66 66 65 72 that the buffer
1e6d0 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d is always zero-
1e6e0 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 5e 54 68 terminated. ^Th
1e6f0 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d e first.** param
1e700 65 74 65 72 20 22 6e 22 20 69 73 20 74 68 65 20 eter "n" is the
1e710 74 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 total size of th
1e720 65 20 62 75 66 66 65 72 2c 20 69 6e 63 6c 75 64 e buffer, includ
1e730 69 6e 67 20 73 70 61 63 65 20 66 6f 72 0a 2a 2a ing space for.**
1e740 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e the zero termin
1e750 61 74 6f 72 2e 20 20 53 6f 20 74 68 65 20 6c 6f ator. So the lo
1e760 6e 67 65 73 74 20 73 74 72 69 6e 67 20 74 68 61 ngest string tha
1e770 74 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 t can be complet
1e780 65 6c 79 0a 2a 2a 20 77 72 69 74 74 65 6e 20 77 ely.** written w
1e790 69 6c 6c 20 62 65 20 6e 2d 31 20 63 68 61 72 61 ill be n-1 chara
1e7a0 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 cters..**.** ^Th
1e7b0 65 20 73 71 6c 69 74 65 33 5f 76 73 6e 70 72 69 e sqlite3_vsnpri
1e7c0 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 20 69 73 ntf() routine is
1e7d0 20 61 20 76 61 72 61 72 67 73 20 76 65 72 73 69 a varargs versi
1e7e0 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e on of sqlite3_sn
1e7f0 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 printf()..**.**
1e800 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 These routines a
1e810 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20 73 6f 6d ll implement som
1e820 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 6f 72 e additional for
1e830 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70 74 69 6f matting.** optio
1e840 6e 73 20 74 68 61 74 20 61 72 65 20 75 73 65 66 ns that are usef
1e850 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72 75 63 74 ul for construct
1e860 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ing SQL statemen
1e870 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 ts..** All of th
1e880 65 20 75 73 75 61 6c 20 70 72 69 6e 74 66 28 29 e usual printf()
1e890 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69 formatting opti
1e8a0 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49 6e 20 61 ons apply. In a
1e8b0 64 64 69 74 69 6f 6e 2c 20 74 68 65 72 65 0a 2a ddition, there.*
1e8c0 2a 20 69 73 20 61 72 65 20 22 25 71 22 2c 20 22 * is are "%q", "
1e8d0 25 51 22 2c 20 61 6e 64 20 22 25 7a 22 20 6f 70 %Q", and "%z" op
1e8e0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54 tions..**.** ^(T
1e8f0 68 65 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f 72 he %q option wor
1e900 6b 73 20 6c 69 6b 65 20 25 73 20 69 6e 20 74 68 ks like %s in th
1e910 61 74 20 69 74 20 73 75 62 73 74 69 74 75 74 65 at it substitute
1e920 73 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 s a nul-terminat
1e930 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66 72 6f ed.** string fro
1e940 6d 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 6c m the argument l
1e950 69 73 74 2e 20 20 42 75 74 20 25 71 20 61 6c 73 ist. But %q als
1e960 6f 20 64 6f 75 62 6c 65 73 20 65 76 65 72 79 20 o doubles every
1e970 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 2e 0a '\'' character..
1e980 2a 2a 20 25 71 20 69 73 20 64 65 73 69 67 6e 65 ** %q is designe
1e990 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 d for use inside
1e9a0 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 a string litera
1e9b0 6c 2e 29 5e 20 20 42 79 20 64 6f 75 62 6c 69 6e l.)^ By doublin
1e9c0 67 20 65 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63 g each '\''.** c
1e9d0 68 61 72 61 63 74 65 72 20 69 74 20 65 73 63 61 haracter it esca
1e9e0 70 65 73 20 74 68 61 74 20 63 68 61 72 61 63 74 pes that charact
1e9f0 65 72 20 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74 er and allows it
1ea00 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 to be inserted
1ea10 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 into.** the stri
1ea20 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 ng..**.** For ex
1ea30 61 6d 70 6c 65 2c 20 61 73 73 75 6d 65 20 74 68 ample, assume th
1ea40 65 20 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c e string variabl
1ea50 65 20 7a 54 65 78 74 20 63 6f 6e 74 61 69 6e 73 e zText contains
1ea60 20 74 65 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73 text as follows
1ea70 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 :.**.** <blockqu
1ea80 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 ote><pre>.** ch
1ea90 61 72 20 2a 7a 54 65 78 74 20 3d 20 22 49 74 27 ar *zText = "It'
1eaa0 73 20 61 20 68 61 70 70 79 20 64 61 79 21 22 3b s a happy day!";
1eab0 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 .** </pre></bloc
1eac0 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e kquote>.**.** On
1ead0 65 20 63 61 6e 20 75 73 65 20 74 68 69 73 20 74 e can use this t
1eae0 65 78 74 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 ext in an SQL st
1eaf0 61 74 65 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f atement as follo
1eb00 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b ws:.**.** <block
1eb10 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 quote><pre>.**
1eb20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c char *zSQL = sql
1eb30 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e ite3_mprintf("IN
1eb40 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 SERT INTO table
1eb50 56 41 4c 55 45 53 28 27 25 71 27 29 22 2c 20 7a VALUES('%q')", z
1eb60 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 Text);.** sqlit
1eb70 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c e3_exec(db, zSQL
1eb80 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 , 0, 0, 0);.**
1eb90 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 sqlite3_free(zSQ
1eba0 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 L);.** </pre></b
1ebb0 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
1ebc0 20 42 65 63 61 75 73 65 20 74 68 65 20 25 71 20 Because the %q
1ebd0 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 69 73 format string is
1ebe0 20 75 73 65 64 2c 20 74 68 65 20 27 5c 27 27 20 used, the '\''
1ebf0 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a 54 65 character in zTe
1ec00 78 74 0a 2a 2a 20 69 73 20 65 73 63 61 70 65 64 xt.** is escaped
1ec10 20 61 6e 64 20 74 68 65 20 53 51 4c 20 67 65 6e and the SQL gen
1ec20 65 72 61 74 65 64 20 69 73 20 61 73 20 66 6f 6c erated is as fol
1ec30 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f lows:.**.** <blo
1ec40 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a ckquote><pre>.**
1ec50 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 INSERT INTO ta
1ec60 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49 74 27 ble1 VALUES('It'
1ec70 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21 27 's a happy day!'
1ec80 29 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f ).** </pre></blo
1ec90 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 ckquote>.**.** T
1eca0 68 69 73 20 69 73 20 63 6f 72 72 65 63 74 2e 20 his is correct.
1ecb0 20 48 61 64 20 77 65 20 75 73 65 64 20 25 73 20 Had we used %s
1ecc0 69 6e 73 74 65 61 64 20 6f 66 20 25 71 2c 20 74 instead of %q, t
1ecd0 68 65 20 67 65 6e 65 72 61 74 65 64 20 53 51 4c he generated SQL
1ece0 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 6c .** would have l
1ecf0 6f 6f 6b 65 64 20 6c 69 6b 65 20 74 68 69 73 3a ooked like this:
1ed00 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f .**.** <blockquo
1ed10 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 te><pre>.** INS
1ed20 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 ERT INTO table1
1ed30 56 41 4c 55 45 53 28 27 49 74 27 73 20 61 20 68 VALUES('It's a h
1ed40 61 70 70 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20 appy day!');.**
1ed50 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f </pre></blockquo
1ed60 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 te>.**.** This s
1ed70 65 63 6f 6e 64 20 65 78 61 6d 70 6c 65 20 69 73 econd example is
1ed80 20 61 6e 20 53 51 4c 20 73 79 6e 74 61 78 20 65 an SQL syntax e
1ed90 72 72 6f 72 2e 20 20 41 73 20 61 20 67 65 6e 65 rror. As a gene
1eda0 72 61 6c 20 72 75 6c 65 20 79 6f 75 20 73 68 6f ral rule you sho
1edb0 75 6c 64 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 uld.** always us
1edc0 65 20 25 71 20 69 6e 73 74 65 61 64 20 6f 66 20 e %q instead of
1edd0 25 73 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e %s when insertin
1ede0 67 20 74 65 78 74 20 69 6e 74 6f 20 61 20 73 74 g text into a st
1edf0 72 69 6e 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a ring literal..**
1ee00 0a 2a 2a 20 5e 28 54 68 65 20 25 51 20 6f 70 74 .** ^(The %Q opt
1ee10 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 ion works like %
1ee20 71 20 65 78 63 65 70 74 20 69 74 20 61 6c 73 6f q except it also
1ee30 20 61 64 64 73 20 73 69 6e 67 6c 65 20 71 75 6f adds single quo
1ee40 74 65 73 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 tes around.** th
1ee50 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 e outside of the
1ee60 20 74 6f 74 61 6c 20 73 74 72 69 6e 67 2e 20 20 total string.
1ee70 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 66 Additionally, if
1ee80 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 the parameter i
1ee90 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e n the.** argumen
1eea0 74 20 6c 69 73 74 20 69 73 20 61 20 4e 55 4c 4c t list is a NULL
1eeb0 20 70 6f 69 6e 74 65 72 2c 20 25 51 20 73 75 62 pointer, %Q sub
1eec0 73 74 69 74 75 74 65 73 20 74 68 65 20 74 65 78 stitutes the tex
1eed0 74 20 22 4e 55 4c 4c 22 20 28 77 69 74 68 6f 75 t "NULL" (withou
1eee0 74 0a 2a 2a 20 73 69 6e 67 6c 65 20 71 75 6f 74 t.** single quot
1eef0 65 73 29 2e 29 5e 20 20 53 6f 2c 20 66 6f 72 20 es).)^ So, for
1ef00 65 78 61 6d 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 example, one cou
1ef10 6c 64 20 73 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 ld say:.**.** <b
1ef20 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a lockquote><pre>.
1ef30 2a 2a 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d ** char *zSQL =
1ef40 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 sqlite3_mprintf
1ef50 28 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 ("INSERT INTO ta
1ef60 62 6c 65 20 56 41 4c 55 45 53 28 25 51 29 22 2c ble VALUES(%Q)",
1ef70 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c zText);.** sql
1ef80 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 ite3_exec(db, zS
1ef90 51 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a QL, 0, 0, 0);.**
1efa0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
1efb0 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c SQL);.** </pre><
1efc0 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a /blockquote>.**.
1efd0 2a 2a 20 54 68 65 20 63 6f 64 65 20 61 62 6f 76 ** The code abov
1efe0 65 20 77 69 6c 6c 20 72 65 6e 64 65 72 20 61 20 e will render a
1eff0 63 6f 72 72 65 63 74 20 53 51 4c 20 73 74 61 74 correct SQL stat
1f000 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 7a 53 51 ement in the zSQ
1f010 4c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 65 76 L.** variable ev
1f020 65 6e 20 69 66 20 74 68 65 20 7a 54 65 78 74 20 en if the zText
1f030 76 61 72 69 61 62 6c 65 20 69 73 20 61 20 4e 55 variable is a NU
1f040 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a LL pointer..**.*
1f050 2a 20 5e 28 54 68 65 20 22 25 7a 22 20 66 6f 72 * ^(The "%z" for
1f060 6d 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e 20 77 matting option w
1f070 6f 72 6b 73 20 6c 69 6b 65 20 22 25 73 22 20 62 orks like "%s" b
1f080 75 74 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 61 ut with the.** a
1f090 64 64 69 74 69 6f 6e 20 74 68 61 74 20 61 66 74 ddition that aft
1f0a0 65 72 20 74 68 65 20 73 74 72 69 6e 67 20 68 61 er the string ha
1f0b0 73 20 62 65 65 6e 20 72 65 61 64 20 61 6e 64 20 s been read and
1f0c0 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 copied into.** t
1f0d0 68 65 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69 he result, [sqli
1f0e0 74 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 63 te3_free()] is c
1f0f0 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e 70 alled on the inp
1f100 75 74 20 73 74 72 69 6e 67 2e 29 5e 0a 2a 2f 0a ut string.)^.*/.
1f110 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 SQLITE_API char
1f120 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 *sqlite3_mprintf
1f130 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e (const char*,...
1f140 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 );.SQLITE_API ch
1f150 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 ar *sqlite3_vmpr
1f160 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a intf(const char*
1f170 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 , va_list);.SQLI
1f180 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c TE_API char *sql
1f190 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e ite3_snprintf(in
1f1a0 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 t,char*,const ch
1f1b0 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 ar*, ...);.SQLIT
1f1c0 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 E_API char *sqli
1f1d0 74 65 33 5f 76 73 6e 70 72 69 6e 74 66 28 69 6e te3_vsnprintf(in
1f1e0 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 t,char*,const ch
1f1f0 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 0a ar*, va_list);..
1f200 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
1f210 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f Memory Allocatio
1f220 6e 20 53 75 62 73 79 73 74 65 6d 0a 2a 2a 0a 2a n Subsystem.**.*
1f230 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 * The SQLite cor
1f240 65 20 75 73 65 73 20 74 68 65 73 65 20 74 68 72 e uses these thr
1f250 65 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 ee routines for
1f260 61 6c 6c 20 6f 66 20 69 74 73 20 6f 77 6e 0a 2a all of its own.*
1f270 2a 20 69 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 * internal memor
1f280 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 y allocation nee
1f290 64 73 2e 20 22 43 6f 72 65 22 20 69 6e 20 74 68 ds. "Core" in th
1f2a0 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74 65 e previous sente
1f2b0 6e 63 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 nce.** does not
1f2c0 69 6e 63 6c 75 64 65 20 6f 70 65 72 61 74 69 6e include operatin
1f2d0 67 2d 73 79 73 74 65 6d 20 73 70 65 63 69 66 69 g-system specifi
1f2e0 63 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 c VFS implementa
1f2f0 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 57 69 tion. The.** Wi
1f300 6e 64 6f 77 73 20 56 46 53 20 75 73 65 73 20 6e ndows VFS uses n
1f310 61 74 69 76 65 20 6d 61 6c 6c 6f 63 28 29 20 61 ative malloc() a
1f320 6e 64 20 66 72 65 65 28 29 20 66 6f 72 20 73 6f nd free() for so
1f330 6d 65 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a me operations..*
1f340 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 *.** ^The sqlite
1f350 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 3_malloc() routi
1f360 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 ne returns a poi
1f370 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f 63 6b 0a nter to a block.
1f380 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 61 74 20 ** of memory at
1f390 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e least N bytes in
1f3a0 20 6c 65 6e 67 74 68 2c 20 77 68 65 72 65 20 4e length, where N
1f3b0 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74 65 is the paramete
1f3c0 72 2e 0a 2a 2a 20 5e 49 66 20 73 71 6c 69 74 65 r..** ^If sqlite
1f3d0 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20 75 6e 3_malloc() is un
1f3e0 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20 73 able to obtain s
1f3f0 75 66 66 69 63 69 65 6e 74 20 66 72 65 65 0a 2a ufficient free.*
1f400 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20 72 65 74 * memory, it ret
1f410 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e urns a NULL poin
1f420 74 65 72 2e 20 20 5e 49 66 20 74 68 65 20 70 61 ter. ^If the pa
1f430 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a 2a 20 rameter N to.**
1f440 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
1f450 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 is zero or nega
1f460 74 69 76 65 20 74 68 65 6e 20 73 71 6c 69 74 65 tive then sqlite
1f470 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 3_malloc() retur
1f480 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 ns.** a NULL poi
1f490 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 5e 43 61 6c nter..**.** ^Cal
1f4a0 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 ling sqlite3_fre
1f4b0 65 28 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74 e() with a point
1f4c0 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 er previously re
1f4d0 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c turned.** by sql
1f4e0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 ite3_malloc() or
1f4f0 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 sqlite3_realloc
1f500 28 29 20 72 65 6c 65 61 73 65 73 20 74 68 61 74 () releases that
1f510 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74 68 memory so.** th
1f520 61 74 20 69 74 20 6d 69 67 68 74 20 62 65 20 72 at it might be r
1f530 65 75 73 65 64 2e 20 20 5e 54 68 65 20 73 71 6c eused. ^The sql
1f540 69 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75 74 ite3_free() rout
1f550 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f ine is.** a no-o
1f560 70 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20 77 p if is called w
1f570 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ith a NULL point
1f580 65 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20 4e er. Passing a N
1f590 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 ULL pointer.** t
1f5a0 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 o sqlite3_free()
1f5b0 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 41 is harmless. A
1f5c0 66 74 65 72 20 62 65 69 6e 67 20 66 72 65 65 64 fter being freed
1f5d0 2c 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75 , memory.** shou
1f5e0 6c 64 20 6e 65 69 74 68 65 72 20 62 65 20 72 65 ld neither be re
1f5f0 61 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20 ad nor written.
1f600 20 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70 72 Even reading pr
1f610 65 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a eviously freed.*
1f620 2a 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 72 * memory might r
1f630 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d 65 esult in a segme
1f640 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72 ntation fault or
1f650 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72 other severe er
1f660 72 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 ror..** Memory c
1f670 6f 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 67 orruption, a seg
1f680 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c mentation fault,
1f690 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72 65 or other severe
1f6a0 20 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20 error.** might
1f6b0 72 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65 result if sqlite
1f6c0 33 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c 3_free() is call
1f6d0 65 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 ed with a non-NU
1f6e0 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0a LL pointer that.
1f6f0 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69 ** was not obtai
1f700 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
1f710 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c _malloc() or sql
1f720 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a ite3_realloc()..
1f730 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 73 71 6c 69 **.** ^(The sqli
1f740 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e te3_realloc() in
1f750 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 terface attempts
1f760 20 74 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 to resize a.**
1f770 70 72 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c prior memory all
1f780 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 ocation to be at
1f790 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20 least N bytes,
1f7a0 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a where N is the.*
1f7b0 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 * second paramet
1f7c0 65 72 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 er. The memory
1f7d0 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 allocation to be
1f7e0 20 72 65 73 69 7a 65 64 20 69 73 20 74 68 65 20 resized is the
1f7f0 66 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 first.** paramet
1f800 65 72 2e 29 5e 20 5e 20 49 66 20 74 68 65 20 66 er.)^ ^ If the f
1f810 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 irst parameter t
1f820 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f o sqlite3_reallo
1f830 63 28 29 0a 2a 2a 20 69 73 20 61 20 4e 55 4c 4c c().** is a NULL
1f840 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 69 74 pointer then it
1f850 73 20 62 65 68 61 76 69 6f 72 20 69 73 20 69 64 s behavior is id
1f860 65 6e 74 69 63 61 6c 20 74 6f 20 63 61 6c 6c 69 entical to calli
1f870 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 ng.** sqlite3_ma
1f880 6c 6c 6f 63 28 4e 29 20 77 68 65 72 65 20 4e 20 lloc(N) where N
1f890 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 is the second pa
1f8a0 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
1f8b0 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a e3_realloc()..**
1f8c0 20 5e 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 ^If the second
1f8d0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
1f8e0 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 ite3_realloc() i
1f8f0 73 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67 s zero or.** neg
1f900 61 74 69 76 65 20 74 68 65 6e 20 74 68 65 20 62 ative then the b
1f910 65 68 61 76 69 6f 72 20 69 73 20 65 78 61 63 74 ehavior is exact
1f920 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73 20 63 ly the same as c
1f930 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 alling.** sqlite
1f940 33 5f 66 72 65 65 28 50 29 20 77 68 65 72 65 20 3_free(P) where
1f950 50 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70 P is the first p
1f960 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
1f970 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a te3_realloc()..*
1f980 2a 20 5e 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c * ^sqlite3_reall
1f990 6f 63 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 oc() returns a p
1f9a0 6f 69 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d 6f ointer to a memo
1f9b0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a ry allocation.**
1f9c0 20 6f 66 20 61 74 20 6c 65 61 73 74 20 4e 20 62 of at least N b
1f9d0 79 74 65 73 20 69 6e 20 73 69 7a 65 20 6f 72 20 ytes in size or
1f9e0 4e 55 4c 4c 20 69 66 20 73 75 66 66 69 63 69 65 NULL if sufficie
1f9f0 6e 74 20 6d 65 6d 6f 72 79 20 69 73 20 75 6e 61 nt memory is una
1fa00 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 5e 49 66 vailable..** ^If
1fa10 20 4d 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f M is the size o
1fa20 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f f the prior allo
1fa30 63 61 74 69 6f 6e 2c 20 74 68 65 6e 20 6d 69 6e cation, then min
1fa40 28 4e 2c 4d 29 20 62 79 74 65 73 0a 2a 2a 20 6f (N,M) bytes.** o
1fa50 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f f the prior allo
1fa60 63 61 74 69 6f 6e 20 61 72 65 20 63 6f 70 69 65 cation are copie
1fa70 64 20 69 6e 74 6f 20 74 68 65 20 62 65 67 69 6e d into the begin
1fa80 6e 69 6e 67 20 6f 66 20 62 75 66 66 65 72 20 72 ning of buffer r
1fa90 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 eturned.** by sq
1faa0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 lite3_realloc()
1fab0 61 6e 64 20 74 68 65 20 70 72 69 6f 72 20 61 6c and the prior al
1fac0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 location is free
1fad0 64 2e 0a 2a 2a 20 5e 49 66 20 73 71 6c 69 74 65 d..** ^If sqlite
1fae0 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74 75 3_realloc() retu
1faf0 72 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 rns NULL, then t
1fb00 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 he prior allocat
1fb10 69 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 66 72 ion.** is not fr
1fb20 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 eed..**.** ^The
1fb30 6d 65 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 20 memory returned
1fb40 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f by sqlite3_mallo
1fb50 63 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f c() and sqlite3_
1fb60 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 realloc().** is
1fb70 61 6c 77 61 79 73 20 61 6c 69 67 6e 65 64 20 74 always aligned t
1fb80 6f 20 61 74 20 6c 65 61 73 74 20 61 6e 20 38 20 o at least an 8
1fb90 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2c 20 6f byte boundary, o
1fba0 72 20 74 6f 20 61 0a 2a 2a 20 34 20 62 79 74 65 r to a.** 4 byte
1fbb0 20 62 6f 75 6e 64 61 72 79 20 69 66 20 74 68 65 boundary if the
1fbc0 20 5b 53 51 4c 49 54 45 5f 34 5f 42 59 54 45 5f [SQLITE_4_BYTE_
1fbd0 41 4c 49 47 4e 45 44 5f 4d 41 4c 4c 4f 43 5d 20 ALIGNED_MALLOC]
1fbe0 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 compile-time.**
1fbf0 6f 70 74 69 6f 6e 20 69 73 20 75 73 65 64 2e 0a option is used..
1fc00 2a 2a 0a 2a 2a 20 49 6e 20 53 51 4c 69 74 65 20 **.** In SQLite
1fc10 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 20 61 6e version 3.5.0 an
1fc20 64 20 33 2e 35 2e 31 2c 20 69 74 20 77 61 73 20 d 3.5.1, it was
1fc30 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 66 69 possible to defi
1fc40 6e 65 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 ne.** the SQLITE
1fc50 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c _OMIT_MEMORY_ALL
1fc60 4f 43 41 54 49 4f 4e 20 77 68 69 63 68 20 77 6f OCATION which wo
1fc70 75 6c 64 20 63 61 75 73 65 20 74 68 65 20 62 75 uld cause the bu
1fc80 69 6c 74 2d 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d ilt-in.** implem
1fc90 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 entation of thes
1fca0 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 e routines to be
1fcb0 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 61 74 20 omitted. That
1fcc0 63 61 70 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 capability.** is
1fcd0 20 6e 6f 20 6c 6f 6e 67 65 72 20 70 72 6f 76 69 no longer provi
1fce0 64 65 64 2e 20 20 4f 6e 6c 79 20 62 75 69 6c 74 ded. Only built
1fcf0 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 -in memory alloc
1fd00 61 74 6f 72 73 20 63 61 6e 20 62 65 20 75 73 65 ators can be use
1fd10 64 2e 0a 2a 2a 0a 2a 2a 20 50 72 69 6f 72 20 74 d..**.** Prior t
1fd20 6f 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e o SQLite version
1fd30 20 33 2e 37 2e 31 30 2c 20 74 68 65 20 57 69 6e 3.7.10, the Win
1fd40 64 6f 77 73 20 4f 53 20 69 6e 74 65 72 66 61 63 dows OS interfac
1fd50 65 20 6c 61 79 65 72 20 63 61 6c 6c 65 64 0a 2a e layer called.*
1fd60 2a 20 74 68 65 20 73 79 73 74 65 6d 20 6d 61 6c * the system mal
1fd70 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 loc() and free()
1fd80 20 64 69 72 65 63 74 6c 79 20 77 68 65 6e 20 63 directly when c
1fd90 6f 6e 76 65 72 74 69 6e 67 0a 2a 2a 20 66 69 6c onverting.** fil
1fda0 65 6e 61 6d 65 73 20 62 65 74 77 65 65 6e 20 74 enames between t
1fdb0 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e he UTF-8 encodin
1fdc0 67 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 g used by SQLite
1fdd0 0a 2a 2a 20 61 6e 64 20 77 68 61 74 65 76 65 72 .** and whatever
1fde0 20 66 69 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 69 filename encodi
1fdf0 6e 67 20 69 73 20 75 73 65 64 20 62 79 20 74 68 ng is used by th
1fe00 65 20 70 61 72 74 69 63 75 6c 61 72 20 57 69 6e e particular Win
1fe10 64 6f 77 73 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 dows.** installa
1fe20 74 69 6f 6e 2e 20 20 4d 65 6d 6f 72 79 20 61 6c tion. Memory al
1fe30 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 73 20 location errors
1fe40 77 65 72 65 20 64 65 74 65 63 74 65 64 2c 20 62 were detected, b
1fe50 75 74 0a 2a 2a 20 74 68 65 79 20 77 65 72 65 20 ut.** they were
1fe60 72 65 70 6f 72 74 65 64 20 62 61 63 6b 20 61 73 reported back as
1fe70 20 5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 [SQLITE_CANTOPE
1fe80 4e 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 N] or.** [SQLITE
1fe90 5f 49 4f 45 52 52 5d 20 72 61 74 68 65 72 20 74 _IOERR] rather t
1fea0 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 han [SQLITE_NOME
1feb0 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f M]..**.** The po
1fec0 69 6e 74 65 72 20 61 72 67 75 6d 65 6e 74 73 20 inter arguments
1fed0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 to [sqlite3_free
1fee0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
1fef0 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 6d _realloc()].** m
1ff00 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 55 ust be either NU
1ff10 4c 4c 20 6f 72 20 65 6c 73 65 20 70 6f 69 6e 74 LL or else point
1ff20 65 72 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ers obtained fro
1ff30 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 69 6e 76 m a prior.** inv
1ff40 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 ocation of [sqli
1ff50 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 te3_malloc()] or
1ff60 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f [sqlite3_reallo
1ff70 63 28 29 5d 20 74 68 61 74 20 68 61 76 65 0a 2a c()] that have.*
1ff80 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 72 * not yet been r
1ff90 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eleased..**.** T
1ffa0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d he application m
1ffb0 75 73 74 20 6e 6f 74 20 72 65 61 64 20 6f 72 20 ust not read or
1ffc0 77 72 69 74 65 20 61 6e 79 20 70 61 72 74 20 6f write any part o
1ffd0 66 0a 2a 2a 20 61 20 62 6c 6f 63 6b 20 6f 66 20 f.** a block of
1ffe0 6d 65 6d 6f 72 79 20 61 66 74 65 72 20 69 74 20 memory after it
1fff0 68 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 65 has been release
20000 64 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 d using.** [sqli
20010 74 65 33 5f 66 72 65 65 28 29 5d 20 6f 72 20 5b te3_free()] or [
20020 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
20030 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 )]..*/.SQLITE_AP
20040 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f I void *sqlite3_
20050 6d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c malloc(int);.SQL
20060 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
20070 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f lite3_realloc(vo
20080 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 id*, int);.SQLIT
20090 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
200a0 65 33 5f 66 72 65 65 28 76 6f 69 64 2a 29 3b 0a e3_free(void*);.
200b0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
200c0 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 6f Memory Allocato
200d0 72 20 53 74 61 74 69 73 74 69 63 73 0a 2a 2a 0a r Statistics.**.
200e0 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 76 69 64 ** SQLite provid
200f0 65 73 20 74 68 65 73 65 20 74 77 6f 20 69 6e 74 es these two int
20100 65 72 66 61 63 65 73 20 66 6f 72 20 72 65 70 6f erfaces for repo
20110 72 74 69 6e 67 20 6f 6e 20 74 68 65 20 73 74 61 rting on the sta
20120 74 75 73 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 tus.** of the [s
20130 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
20140 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 , [sqlite3_free(
20150 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 )], and [sqlite3
20160 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 72 _realloc()].** r
20170 6f 75 74 69 6e 65 73 2c 20 77 68 69 63 68 20 66 outines, which f
20180 6f 72 6d 20 74 68 65 20 62 75 69 6c 74 2d 69 6e orm the built-in
20190 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
201a0 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2a on subsystem..**
201b0 0a 2a 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74 65 .** ^The [sqlite
201c0 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 3_memory_used()]
201d0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
201e0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
201f0 79 74 65 73 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 ytes.** of memor
20200 79 20 63 75 72 72 65 6e 74 6c 79 20 6f 75 74 73 y currently outs
20210 74 61 6e 64 69 6e 67 20 28 6d 61 6c 6c 6f 63 65 tanding (malloce
20220 64 20 62 75 74 20 6e 6f 74 20 66 72 65 65 64 29 d but not freed)
20230 2e 0a 2a 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74 ..** ^The [sqlit
20240 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 e3_memory_highwa
20250 74 65 72 28 29 5d 20 72 6f 75 74 69 6e 65 20 72 ter()] routine r
20260 65 74 75 72 6e 73 20 74 68 65 20 6d 61 78 69 6d eturns the maxim
20270 75 6d 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 5b um.** value of [
20280 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 sqlite3_memory_u
20290 73 65 64 28 29 5d 20 73 69 6e 63 65 20 74 68 65 sed()] since the
202a0 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b high-water mark
202b0 0a 2a 2a 20 77 61 73 20 6c 61 73 74 20 72 65 73 .** was last res
202c0 65 74 2e 20 20 5e 54 68 65 20 76 61 6c 75 65 73 et. ^The values
202d0 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 returned by [sq
202e0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 lite3_memory_use
202f0 64 28 29 5d 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c d()] and.** [sql
20300 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 ite3_memory_high
20310 77 61 74 65 72 28 29 5d 20 69 6e 63 6c 75 64 65 water()] include
20320 20 61 6e 79 20 6f 76 65 72 68 65 61 64 0a 2a 2a any overhead.**
20330 20 61 64 64 65 64 20 62 79 20 53 51 4c 69 74 65 added by SQLite
20340 20 69 6e 20 69 74 73 20 69 6d 70 6c 65 6d 65 6e in its implemen
20350 74 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 tation of [sqlit
20360 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 0a 2a 2a e3_malloc()],.**
20370 20 62 75 74 20 6e 6f 74 20 6f 76 65 72 68 65 61 but not overhea
20380 64 20 61 64 64 65 64 20 62 79 20 74 68 65 20 61 d added by the a
20390 6e 79 20 75 6e 64 65 72 6c 79 69 6e 67 20 73 79 ny underlying sy
203a0 73 74 65 6d 20 6c 69 62 72 61 72 79 0a 2a 2a 20 stem library.**
203b0 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 5b 73 routines that [s
203c0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d qlite3_malloc()]
203d0 20 6d 61 79 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a may call..**.**
203e0 20 5e 54 68 65 20 6d 65 6d 6f 72 79 20 68 69 67 ^The memory hig
203f0 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 73 20 h-water mark is
20400 72 65 73 65 74 20 74 6f 20 74 68 65 20 63 75 72 reset to the cur
20410 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 0a 2a 2a rent value of.**
20420 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 [sqlite3_memory
20430 5f 75 73 65 64 28 29 5d 20 69 66 20 61 6e 64 20 _used()] if and
20440 6f 6e 6c 79 20 69 66 20 74 68 65 20 70 61 72 61 only if the para
20450 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c meter to.** [sql
20460 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 ite3_memory_high
20470 77 61 74 65 72 28 29 5d 20 69 73 20 74 72 75 65 water()] is true
20480 2e 20 20 5e 54 68 65 20 76 61 6c 75 65 20 72 65 . ^The value re
20490 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 5b 73 71 turned.** by [sq
204a0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 lite3_memory_hig
204b0 68 77 61 74 65 72 28 31 29 5d 20 69 73 20 74 68 hwater(1)] is th
204c0 65 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 e high-water mar
204d0 6b 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 74 68 k.** prior to th
204e0 65 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 e reset..*/.SQLI
204f0 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 TE_API sqlite3_i
20500 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d nt64 sqlite3_mem
20510 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b 0a ory_used(void);.
20520 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
20530 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e3_int64 sqlite3
20540 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 _memory_highwate
20550 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29 r(int resetFlag)
20560 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
20570 46 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f 6d F: Pseudo-Random
20580 20 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74 6f Number Generato
20590 72 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 r.**.** SQLite c
205a0 6f 6e 74 61 69 6e 73 20 61 20 68 69 67 68 2d 71 ontains a high-q
205b0 75 61 6c 69 74 79 20 70 73 65 75 64 6f 2d 72 61 uality pseudo-ra
205c0 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 ndom number gene
205d0 72 61 74 6f 72 20 28 50 52 4e 47 29 20 75 73 65 rator (PRNG) use
205e0 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63 74 20 72 d to.** select r
205f0 61 6e 64 6f 6d 20 5b 52 4f 57 49 44 20 7c 20 52 andom [ROWID | R
20600 4f 57 49 44 73 5d 20 77 68 65 6e 20 69 6e 73 65 OWIDs] when inse
20610 72 74 69 6e 67 20 6e 65 77 20 72 65 63 6f 72 64 rting new record
20620 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 74 s into a table t
20630 68 61 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 75 hat.** already u
20640 73 65 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 ses the largest
20650 70 6f 73 73 69 62 6c 65 20 5b 52 4f 57 49 44 5d possible [ROWID]
20660 2e 20 20 54 68 65 20 50 52 4e 47 20 69 73 20 61 . The PRNG is a
20670 6c 73 6f 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 lso used for.**
20680 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 72 61 6e the build-in ran
20690 64 6f 6d 28 29 20 61 6e 64 20 72 61 6e 64 6f 6d dom() and random
206a0 62 6c 6f 62 28 29 20 53 51 4c 20 66 75 6e 63 74 blob() SQL funct
206b0 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 6e 74 65 ions. This inte
206c0 72 66 61 63 65 20 61 6c 6c 6f 77 73 0a 2a 2a 20 rface allows.**
206d0 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 6f 20 applications to
206e0 61 63 63 65 73 73 20 74 68 65 20 73 61 6d 65 20 access the same
206f0 50 52 4e 47 20 66 6f 72 20 6f 74 68 65 72 20 70 PRNG for other p
20700 75 72 70 6f 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 5e urposes..**.** ^
20710 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72 A call to this r
20720 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 4e 20 outine stores N
20730 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e bytes of randomn
20740 65 73 73 20 69 6e 74 6f 20 62 75 66 66 65 72 20 ess into buffer
20750 50 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 P..**.** ^The fi
20760 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f rst time this ro
20770 75 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65 64 utine is invoked
20780 20 28 65 69 74 68 65 72 20 69 6e 74 65 72 6e 61 (either interna
20790 6c 6c 79 20 6f 72 20 62 79 0a 2a 2a 20 74 68 65 lly or by.** the
207a0 20 61 70 70 6c 69 63 61 74 69 6f 6e 29 20 74 68 application) th
207b0 65 20 50 52 4e 47 20 69 73 20 73 65 65 64 65 64 e PRNG is seeded
207c0 20 75 73 69 6e 67 20 72 61 6e 64 6f 6d 6e 65 73 using randomnes
207d0 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 s obtained.** fr
207e0 6f 6d 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e 65 om the xRandomne
207f0 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 ss method of the
20800 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 default [sqlite
20810 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 3_vfs] object..*
20820 2a 20 5e 4f 6e 20 61 6c 6c 20 73 75 62 73 65 71 * ^On all subseq
20830 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73 uent invocations
20840 2c 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e , the pseudo-ran
20850 64 6f 6d 6e 65 73 73 20 69 73 20 67 65 6e 65 72 domness is gener
20860 61 74 65 64 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c ated.** internal
20870 6c 79 20 61 6e 64 20 77 69 74 68 6f 75 74 20 72 ly and without r
20880 65 63 6f 75 72 73 65 20 74 6f 20 74 68 65 20 5b ecourse to the [
20890 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 78 52 61 sqlite3_vfs] xRa
208a0 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20 6d 65 74 68 ndomness.** meth
208b0 6f 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 od..*/.SQLITE_AP
208c0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
208d0 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c andomness(int N,
208e0 20 76 6f 69 64 20 2a 50 29 3b 0a 0a 2f 2a 0a 2a void *P);../*.*
208f0 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 * CAPI3REF: Comp
20900 69 6c 65 2d 54 69 6d 65 20 41 75 74 68 6f 72 69 ile-Time Authori
20910 7a 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 zation Callbacks
20920 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 72 6f 75 .**.** ^This rou
20930 74 69 6e 65 20 72 65 67 69 73 74 65 72 73 20 61 tine registers a
20940 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c n authorizer cal
20950 6c 62 61 63 6b 20 77 69 74 68 20 61 20 70 61 72 lback with a par
20960 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61 ticular.** [data
20970 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
20980 2c 20 73 75 70 70 6c 69 65 64 20 69 6e 20 74 68 , supplied in th
20990 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
209a0 2e 0a 2a 2a 20 5e 54 68 65 20 61 75 74 68 6f 72 ..** ^The author
209b0 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 izer callback is
209c0 20 69 6e 76 6f 6b 65 64 20 61 73 20 53 51 4c 20 invoked as SQL
209d0 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 62 statements are b
209e0 65 69 6e 67 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a eing compiled.**
209f0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 by [sqlite3_pre
20a00 70 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 pare()] or its v
20a10 61 72 69 61 6e 74 73 20 5b 73 71 6c 69 74 65 33 ariants [sqlite3
20a20 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 0a _prepare_v2()],.
20a30 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ** [sqlite3_prep
20a40 61 72 65 31 36 28 29 5d 20 61 6e 64 20 5b 73 71 are16()] and [sq
20a50 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f lite3_prepare16_
20a60 76 32 28 29 5d 2e 20 20 5e 41 74 20 76 61 72 69 v2()]. ^At vari
20a70 6f 75 73 0a 2a 2a 20 70 6f 69 6e 74 73 20 64 75 ous.** points du
20a80 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61 ring the compila
20a90 74 69 6f 6e 20 70 72 6f 63 65 73 73 2c 20 61 73 tion process, as
20aa0 20 6c 6f 67 69 63 20 69 73 20 62 65 69 6e 67 20 logic is being
20ab0 63 72 65 61 74 65 64 0a 2a 2a 20 74 6f 20 70 65 created.** to pe
20ac0 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 61 63 rform various ac
20ad0 74 69 6f 6e 73 2c 20 74 68 65 20 61 75 74 68 6f tions, the autho
20ae0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 rizer callback i
20af0 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20 s invoked to.**
20b00 73 65 65 20 69 66 20 74 68 6f 73 65 20 61 63 74 see if those act
20b10 69 6f 6e 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 ions are allowed
20b20 2e 20 20 5e 54 68 65 20 61 75 74 68 6f 72 69 7a . ^The authoriz
20b30 65 72 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 er callback shou
20b40 6c 64 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51 ld.** return [SQ
20b50 4c 49 54 45 5f 4f 4b 5d 20 74 6f 20 61 6c 6c 6f LITE_OK] to allo
20b60 77 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 5b 53 w the action, [S
20b70 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 6f QLITE_IGNORE] to
20b80 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 0a 2a 2a disallow the.**
20b90 20 73 70 65 63 69 66 69 63 20 61 63 74 69 6f 6e specific action
20ba0 20 62 75 74 20 61 6c 6c 6f 77 20 74 68 65 20 53 but allow the S
20bb0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 QL statement to
20bc0 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 0a 2a continue to be.*
20bd0 2a 20 63 6f 6d 70 69 6c 65 64 2c 20 6f 72 20 5b * compiled, or [
20be0 53 51 4c 49 54 45 5f 44 45 4e 59 5d 20 74 6f 20 SQLITE_DENY] to
20bf0 63 61 75 73 65 20 74 68 65 20 65 6e 74 69 72 65 cause the entire
20c00 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 SQL statement t
20c10 6f 20 62 65 0a 2a 2a 20 72 65 6a 65 63 74 65 64 o be.** rejected
20c20 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 with an error.
20c30 20 5e 49 66 20 74 68 65 20 61 75 74 68 6f 72 69 ^If the authori
20c40 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 zer callback ret
20c50 75 72 6e 73 0a 2a 2a 20 61 6e 79 20 76 61 6c 75 urns.** any valu
20c60 65 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 e other than [SQ
20c70 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 2c 20 5b 53 LITE_IGNORE], [S
20c80 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f 72 20 5b 53 QLITE_OK], or [S
20c90 51 4c 49 54 45 5f 44 45 4e 59 5d 0a 2a 2a 20 74 QLITE_DENY].** t
20ca0 68 65 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33 hen the [sqlite3
20cb0 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f _prepare_v2()] o
20cc0 72 20 65 71 75 69 76 61 6c 65 6e 74 20 63 61 6c r equivalent cal
20cd0 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64 l that triggered
20ce0 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a .** the authoriz
20cf0 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 er will fail wit
20d00 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 h an error messa
20d10 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 ge..**.** When t
20d20 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 he callback retu
20d30 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c rns [SQLITE_OK],
20d40 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 that means the
20d50 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 72 65 71 operation.** req
20d60 75 65 73 74 65 64 20 69 73 20 6f 6b 2e 20 20 5e uested is ok. ^
20d70 57 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 When the callbac
20d80 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 k returns [SQLIT
20d90 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20 E_DENY], the.**
20da0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
20db0 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69 76 61 _v2()] or equiva
20dc0 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74 20 74 lent call that t
20dd0 72 69 67 67 65 72 65 64 20 74 68 65 0a 2a 2a 20 riggered the.**
20de0 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c 6c 20 authorizer will
20df0 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65 72 72 fail with an err
20e00 6f 72 20 6d 65 73 73 61 67 65 20 65 78 70 6c 61 or message expla
20e10 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 61 63 ining that.** ac
20e20 63 65 73 73 20 69 73 20 64 65 6e 69 65 64 2e 20 cess is denied.
20e30 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 72 73 .**.** ^The firs
20e40 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 t parameter to t
20e50 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 he authorizer ca
20e60 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70 79 llback is a copy
20e70 20 6f 66 20 74 68 65 20 74 68 69 72 64 0a 2a 2a of the third.**
20e80 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
20e90 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 e sqlite3_set_au
20ea0 74 68 6f 72 69 7a 65 72 28 29 20 69 6e 74 65 72 thorizer() inter
20eb0 66 61 63 65 2e 20 5e 54 68 65 20 73 65 63 6f 6e face. ^The secon
20ec0 64 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 d parameter.** t
20ed0 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 o the callback i
20ee0 73 20 61 6e 20 69 6e 74 65 67 65 72 20 5b 53 51 s an integer [SQ
20ef0 4c 49 54 45 5f 43 4f 50 59 20 7c 20 61 63 74 69 LITE_COPY | acti
20f00 6f 6e 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70 on code] that sp
20f10 65 63 69 66 69 65 73 0a 2a 2a 20 74 68 65 20 70 ecifies.** the p
20f20 61 72 74 69 63 75 6c 61 72 20 61 63 74 69 6f 6e articular action
20f30 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 to be authorize
20f40 64 2e 20 5e 54 68 65 20 74 68 69 72 64 20 74 68 d. ^The third th
20f50 72 6f 75 67 68 20 73 69 78 74 68 20 70 61 72 61 rough sixth para
20f60 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 74 68 65 meters.** to the
20f70 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 20 7a 65 callback are ze
20f80 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 ro-terminated st
20f90 72 69 6e 67 73 20 74 68 61 74 20 63 6f 6e 74 61 rings that conta
20fa0 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a in additional.**
20fb0 20 64 65 74 61 69 6c 73 20 61 62 6f 75 74 20 74 details about t
20fc0 68 65 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 he action to be
20fd0 61 75 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a authorized..**.*
20fe0 2a 20 5e 49 66 20 74 68 65 20 61 63 74 69 6f 6e * ^If the action
20ff0 20 63 6f 64 65 20 69 73 20 5b 53 51 4c 49 54 45 code is [SQLITE
21000 5f 52 45 41 44 5d 0a 2a 2a 20 61 6e 64 20 74 68 _READ].** and th
21010 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 e callback retur
21020 6e 73 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 ns [SQLITE_IGNOR
21030 45 5d 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 5b E] then the.** [
21040 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
21050 6e 74 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 nt] statement is
21060 20 63 6f 6e 73 74 72 75 63 74 65 64 20 74 6f 20 constructed to
21070 73 75 62 73 74 69 74 75 74 65 0a 2a 2a 20 61 20 substitute.** a
21080 4e 55 4c 4c 20 76 61 6c 75 65 20 69 6e 20 70 6c NULL value in pl
21090 61 63 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ace of the table
210a0 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 77 6f 75 column that wou
210b0 6c 64 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 ld have.** been
210c0 72 65 61 64 20 69 66 20 5b 53 51 4c 49 54 45 5f read if [SQLITE_
210d0 4f 4b 5d 20 68 61 64 20 62 65 65 6e 20 72 65 74 OK] had been ret
210e0 75 72 6e 65 64 2e 20 20 54 68 65 20 5b 53 51 4c urned. The [SQL
210f0 49 54 45 5f 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72 ITE_IGNORE].** r
21100 65 74 75 72 6e 20 63 61 6e 20 62 65 20 75 73 65 eturn can be use
21110 64 20 74 6f 20 64 65 6e 79 20 61 6e 20 75 6e 74 d to deny an unt
21120 72 75 73 74 65 64 20 75 73 65 72 20 61 63 63 65 rusted user acce
21130 73 73 20 74 6f 20 69 6e 64 69 76 69 64 75 61 6c ss to individual
21140 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 61 .** columns of a
21150 20 74 61 62 6c 65 2e 0a 2a 2a 20 5e 49 66 20 74 table..** ^If t
21160 68 65 20 61 63 74 69 6f 6e 20 63 6f 64 65 20 69 he action code i
21170 73 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 s [SQLITE_DELETE
21180 5d 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 ] and the callba
21190 63 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 5b 53 ck returns.** [S
211a0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 68 QLITE_IGNORE] th
211b0 65 6e 20 74 68 65 20 5b 44 45 4c 45 54 45 5d 20 en the [DELETE]
211c0 6f 70 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 operation procee
211d0 64 73 20 62 75 74 20 74 68 65 0a 2a 2a 20 5b 74 ds but the.** [t
211e0 72 75 6e 63 61 74 65 20 6f 70 74 69 6d 69 7a 61 runcate optimiza
211f0 74 69 6f 6e 5d 20 69 73 20 64 69 73 61 62 6c 65 tion] is disable
21200 64 20 61 6e 64 20 61 6c 6c 20 72 6f 77 73 20 61 d and all rows a
21210 72 65 20 64 65 6c 65 74 65 64 20 69 6e 64 69 76 re deleted indiv
21220 69 64 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 idually..**.** A
21230 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20 n authorizer is
21240 75 73 65 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 used when [sqlit
21250 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 72 65 e3_prepare | pre
21260 70 61 72 69 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 paring].** SQL s
21270 74 61 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61 tatements from a
21280 6e 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72 n untrusted sour
21290 63 65 2c 20 74 6f 20 65 6e 73 75 72 65 20 74 68 ce, to ensure th
212a0 61 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 at the SQL state
212b0 6d 65 6e 74 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 ments.** do not
212c0 74 72 79 20 74 6f 20 61 63 63 65 73 73 20 64 61 try to access da
212d0 74 61 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 ta they are not
212e0 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65 65 2c 20 allowed to see,
212f0 6f 72 20 74 68 61 74 20 74 68 65 79 20 64 6f 20 or that they do
21300 6e 6f 74 0a 2a 2a 20 74 72 79 20 74 6f 20 65 78 not.** try to ex
21310 65 63 75 74 65 20 6d 61 6c 69 63 69 6f 75 73 20 ecute malicious
21320 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 statements that
21330 64 61 6d 61 67 65 20 74 68 65 20 64 61 74 61 62 damage the datab
21340 61 73 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 ase. For.** exa
21350 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 mple, an applica
21360 74 69 6f 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61 tion may allow a
21370 20 75 73 65 72 20 74 6f 20 65 6e 74 65 72 20 61 user to enter a
21380 72 62 69 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20 rbitrary.** SQL
21390 71 75 65 72 69 65 73 20 66 6f 72 20 65 76 61 6c queries for eval
213a0 75 61 74 69 6f 6e 20 62 79 20 61 20 64 61 74 61 uation by a data
213b0 62 61 73 65 2e 20 20 42 75 74 20 74 68 65 20 61 base. But the a
213c0 70 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a pplication does.
213d0 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 ** not want the
213e0 75 73 65 72 20 74 6f 20 62 65 20 61 62 6c 65 20 user to be able
213f0 74 6f 20 6d 61 6b 65 20 61 72 62 69 74 72 61 72 to make arbitrar
21400 79 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 y changes to the
21410 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 41 .** database. A
21420 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 75 n authorizer cou
21430 6c 64 20 74 68 65 6e 20 62 65 20 70 75 74 20 69 ld then be put i
21440 6e 20 70 6c 61 63 65 20 77 68 69 6c 65 20 74 68 n place while th
21450 65 0a 2a 2a 20 75 73 65 72 2d 65 6e 74 65 72 65 e.** user-entere
21460 64 20 53 51 4c 20 69 73 20 62 65 69 6e 67 20 5b d SQL is being [
21470 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 sqlite3_prepare
21480 7c 20 70 72 65 70 61 72 65 64 5d 20 74 68 61 74 | prepared] that
21490 0a 2a 2a 20 64 69 73 61 6c 6c 6f 77 73 20 65 76 .** disallows ev
214a0 65 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 erything except
214b0 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 [SELECT] stateme
214c0 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 nts..**.** Appli
214d0 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 cations that nee
214e0 64 20 74 6f 20 70 72 6f 63 65 73 73 20 53 51 4c d to process SQL
214f0 20 66 72 6f 6d 20 75 6e 74 72 75 73 74 65 64 20 from untrusted
21500 73 6f 75 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74 sources.** might
21510 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c also consider l
21520 6f 77 65 72 69 6e 67 20 72 65 73 6f 75 72 63 65 owering resource
21530 20 6c 69 6d 69 74 73 20 75 73 69 6e 67 20 5b 73 limits using [s
21540 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a qlite3_limit()].
21550 2a 2a 20 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20 ** and limiting
21560 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 75 73 database size us
21570 69 6e 67 20 74 68 65 20 5b 6d 61 78 5f 70 61 67 ing the [max_pag
21580 65 5f 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 e_count] [PRAGMA
21590 5d 0a 2a 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e ].** in addition
215a0 20 74 6f 20 75 73 69 6e 67 20 61 6e 20 61 75 74 to using an aut
215b0 68 6f 72 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 5e horizer..**.** ^
215c0 28 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 61 (Only a single a
215d0 75 74 68 6f 72 69 7a 65 72 20 63 61 6e 20 62 65 uthorizer can be
215e0 20 69 6e 20 70 6c 61 63 65 20 6f 6e 20 61 20 64 in place on a d
215f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
21600 6f 6e 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 2e on.** at a time.
21610 20 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 73 Each call to s
21620 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f qlite3_set_autho
21630 72 69 7a 65 72 20 6f 76 65 72 72 69 64 65 73 20 rizer overrides
21640 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 20 the.** previous
21650 63 61 6c 6c 2e 29 5e 20 20 5e 44 69 73 61 62 6c call.)^ ^Disabl
21660 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 e the authorizer
21670 20 62 79 20 69 6e 73 74 61 6c 6c 69 6e 67 20 61 by installing a
21680 20 4e 55 4c 4c 20 63 61 6c 6c 62 61 63 6b 2e 0a NULL callback..
21690 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 ** The authorize
216a0 72 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 r is disabled by
216b0 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 default..**.**
216c0 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 The authorizer c
216d0 61 6c 6c 62 61 63 6b 20 6d 75 73 74 20 6e 6f 74 allback must not
216e0 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 do anything tha
216f0 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a t will modify.**
21700 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
21710 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e nnection that in
21720 76 6f 6b 65 64 20 74 68 65 20 61 75 74 68 6f 72 voked the author
21730 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a izer callback..*
21740 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c * Note that [sql
21750 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
21760 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
21770 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 step()] both mod
21780 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 ify their.** dat
21790 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
217a0 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e s for the meanin
217b0 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e g of "modify" in
217c0 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e this paragraph.
217d0 0a 2a 2a 0a 2a 2a 20 5e 57 68 65 6e 20 5b 73 71 .**.** ^When [sq
217e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
217f0 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f 20 70 ()] is used to p
21800 72 65 70 61 72 65 20 61 20 73 74 61 74 65 6d 65 repare a stateme
21810 6e 74 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 nt, the.** state
21820 6d 65 6e 74 20 6d 69 67 68 74 20 62 65 20 72 65 ment might be re
21830 2d 70 72 65 70 61 72 65 64 20 64 75 72 69 6e 67 -prepared during
21840 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
21850 5d 20 64 75 65 20 74 6f 20 61 20 0a 2a 2a 20 73 ] due to a .** s
21860 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 20 20 48 chema change. H
21870 65 6e 63 65 2c 20 74 68 65 20 61 70 70 6c 69 63 ence, the applic
21880 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 65 6e 73 ation should ens
21890 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 ure that the.**
218a0 63 6f 72 72 65 63 74 20 61 75 74 68 6f 72 69 7a correct authoriz
218b0 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 6d 61 er callback rema
218c0 69 6e 73 20 69 6e 20 70 6c 61 63 65 20 64 75 72 ins in place dur
218d0 69 6e 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33 ing the [sqlite3
218e0 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 _step()]..**.**
218f0 5e 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 61 ^Note that the a
21900 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
21910 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e ck is invoked on
21920 6c 79 20 64 75 72 69 6e 67 0a 2a 2a 20 5b 73 71 ly during.** [sq
21930 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d lite3_prepare()]
21940 20 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 or its variants
21950 2e 20 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e . Authorization
21960 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f is not.** perfo
21970 72 6d 65 64 20 64 75 72 69 6e 67 20 73 74 61 74 rmed during stat
21980 65 6d 65 6e 74 20 65 76 61 6c 75 61 74 69 6f 6e ement evaluation
21990 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 in [sqlite3_ste
219a0 70 28 29 5d 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 p()], unless.**
219b0 61 73 20 73 74 61 74 65 64 20 69 6e 20 74 68 65 as stated in the
219c0 20 70 72 65 76 69 6f 75 73 20 70 61 72 61 67 72 previous paragr
219d0 61 70 68 2c 20 73 71 6c 69 74 65 33 5f 73 74 65 aph, sqlite3_ste
219e0 70 28 29 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 73 p() invokes.** s
219f0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
21a00 32 28 29 20 74 6f 20 72 65 70 72 65 70 61 72 65 2() to reprepare
21a10 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74 a statement aft
21a20 65 72 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e er a schema chan
21a30 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ge..*/.SQLITE_AP
21a40 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 I int sqlite3_se
21a50 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 t_authorizer(.
21a60 73 71 6c 69 74 65 33 2a 2c 0a 20 20 69 6e 74 20 sqlite3*,. int
21a70 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 (*xAuth)(void*,i
21a80 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 nt,const char*,c
21a90 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 onst char*,const
21aa0 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 char*,const cha
21ab0 72 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 r*),. void *pUs
21ac0 65 72 44 61 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a erData.);../*.**
21ad0 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f CAPI3REF: Autho
21ae0 72 69 7a 65 72 20 52 65 74 75 72 6e 20 43 6f 64 rizer Return Cod
21af0 65 73 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 es.**.** The [sq
21b00 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 lite3_set_author
21b10 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 izer | authorize
21b20 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 r callback funct
21b30 69 6f 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 ion] must.** ret
21b40 75 72 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49 urn either [SQLI
21b50 54 45 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 TE_OK] or one of
21b60 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 these two const
21b70 61 6e 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a ants in order.**
21b80 20 74 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 to signal SQLit
21b90 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 e whether or not
21ba0 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70 the action is p
21bb0 65 72 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74 ermitted. See t
21bc0 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 he.** [sqlite3_s
21bd0 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 et_authorizer |
21be0 61 75 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d authorizer docum
21bf0 65 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 entation] for ad
21c00 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f ditional.** info
21c10 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e rmation..**.** N
21c20 6f 74 65 20 74 68 61 74 20 53 51 4c 49 54 45 5f ote that SQLITE_
21c30 49 47 4e 4f 52 45 20 69 73 20 61 6c 73 6f 20 75 IGNORE is also u
21c40 73 65 64 20 61 73 20 61 20 5b 53 51 4c 49 54 45 sed as a [SQLITE
21c50 5f 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 65 74 75 _ROLLBACK | retu
21c60 72 6e 20 63 6f 64 65 5d 0a 2a 2a 20 66 72 6f 6d rn code].** from
21c70 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 74 the [sqlite3_vt
21c80 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63 74 28 29 ab_on_conflict()
21c90 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a ] interface..*/.
21ca0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
21cb0 45 4e 59 20 20 20 31 20 20 20 2f 2a 20 41 62 6f ENY 1 /* Abo
21cc0 72 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 rt the SQL state
21cd0 6d 65 6e 74 20 77 69 74 68 20 61 6e 20 65 72 72 ment with an err
21ce0 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 or */.#define SQ
21cf0 4c 49 54 45 5f 49 47 4e 4f 52 45 20 32 20 20 20 LITE_IGNORE 2
21d00 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c 6f 77 20 61 /* Don't allow a
21d10 63 63 65 73 73 2c 20 62 75 74 20 64 6f 6e 27 74 ccess, but don't
21d20 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 generate an err
21d30 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 or */../*.** CAP
21d40 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a 65 I3REF: Authorize
21d50 72 20 41 63 74 69 6f 6e 20 43 6f 64 65 73 0a 2a r Action Codes.*
21d60 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 *.** The [sqlite
21d70 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 3_set_authorizer
21d80 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 ()] interface re
21d90 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 gisters a callba
21da0 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 ck function.** t
21db0 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 hat is invoked t
21dc0 6f 20 61 75 74 68 6f 72 69 7a 65 20 63 65 72 74 o authorize cert
21dd0 61 69 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ain SQL statemen
21de0 74 20 61 63 74 69 6f 6e 73 2e 20 20 54 68 65 0a t actions. The.
21df0 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 ** second parame
21e00 74 65 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 ter to the callb
21e10 61 63 6b 20 69 73 20 61 6e 20 69 6e 74 65 67 65 ack is an intege
21e20 72 20 63 6f 64 65 20 74 68 61 74 20 73 70 65 63 r code that spec
21e30 69 66 69 65 73 0a 2a 2a 20 77 68 61 74 20 61 63 ifies.** what ac
21e40 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 61 75 tion is being au
21e50 74 68 6f 72 69 7a 65 64 2e 20 20 54 68 65 73 65 thorized. These
21e60 20 61 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 are the integer
21e70 20 61 63 74 69 6f 6e 20 63 6f 64 65 73 20 74 68 action codes th
21e80 61 74 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 at.** the author
21e90 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d 61 izer callback ma
21ea0 79 20 62 65 20 70 61 73 73 65 64 2e 0a 2a 2a 0a y be passed..**.
21eb0 2a 2a 20 54 68 65 73 65 20 61 63 74 69 6f 6e 20 ** These action
21ec0 63 6f 64 65 20 76 61 6c 75 65 73 20 73 69 67 6e code values sign
21ed0 69 66 79 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 ify what kind of
21ee0 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 74 6f operation is to
21ef0 20 62 65 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 be.** authorize
21f00 64 2e 20 20 54 68 65 20 33 72 64 20 61 6e 64 20 d. The 3rd and
21f10 34 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 4th parameters t
21f20 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 o the authorizat
21f30 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 ion.** callback
21f40 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 function will be
21f50 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 4e parameters or N
21f60 55 4c 4c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e ULL depending on
21f70 20 77 68 69 63 68 20 6f 66 20 74 68 65 73 65 0a which of these.
21f80 2a 2a 20 63 6f 64 65 73 20 69 73 20 75 73 65 64 ** codes is used
21f90 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 as the second p
21fa0 61 72 61 6d 65 74 65 72 2e 20 20 5e 28 54 68 65 arameter. ^(The
21fb0 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 5th parameter t
21fc0 6f 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f 72 69 o the.** authori
21fd0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 zer callback is
21fe0 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
21ff0 64 61 74 61 62 61 73 65 20 28 22 6d 61 69 6e 22 database ("main"
22000 2c 20 22 74 65 6d 70 22 2c 0a 2a 2a 20 65 74 63 , "temp",.** etc
22010 2e 29 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 .) if applicable
22020 2e 29 5e 20 20 5e 54 68 65 20 36 74 68 20 70 61 .)^ ^The 6th pa
22030 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 rameter to the a
22040 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
22050 63 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d ck.** is the nam
22060 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d 6d e of the inner-m
22070 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72 20 76 ost trigger or v
22080 69 65 77 20 74 68 61 74 20 69 73 20 72 65 73 70 iew that is resp
22090 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74 onsible for.** t
220a0 68 65 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 he access attemp
220b0 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 69 t or NULL if thi
220c0 73 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74 s access attempt
220d0 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72 6f is directly fro
220e0 6d 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20 53 m.** top-level S
220f0 51 4c 20 63 6f 64 65 2e 0a 2a 2f 0a 2f 2a 2a 2a QL code..*/./***
22100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
22110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
22120 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72 64 20 2a 2a 2a ******** 3rd ***
22130 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34 74 68 20 2a 2a ********* 4th **
22140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 *********/.#defi
22150 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 ne SQLITE_CREATE
22160 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 _INDEX
22170 31 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 1 /* Index Nam
22180 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d e Table Nam
22190 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e e */.#defin
221a0 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f e SQLITE_CREATE_
221b0 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 32 TABLE 2
221c0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
221d0 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 NULL
221e0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
221f0 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 SQLITE_CREATE_T
22200 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 33 20 EMP_INDEX 3
22210 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 /* Index Name
22220 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 Table Name
22230 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
22240 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 SQLITE_CREATE_TE
22250 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 34 20 20 MP_TABLE 4
22260 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 /* Table Name
22270 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 NULL
22280 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
22290 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d QLITE_CREATE_TEM
222a0 50 5f 54 52 49 47 47 45 52 20 20 20 35 20 20 20 P_TRIGGER 5
222b0 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 /* Trigger Name
222c0 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 Table Name
222d0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
222e0 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 LITE_CREATE_TEMP
222f0 5f 56 49 45 57 20 20 20 20 20 20 36 20 20 20 2f _VIEW 6 /
22300 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 * View Name
22310 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
22320 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
22330 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49 47 47 ITE_CREATE_TRIGG
22340 45 52 20 20 20 20 20 20 20 20 37 20 20 20 2f 2a ER 7 /*
22350 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 Trigger Name
22360 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
22370 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
22380 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 20 20 TE_CREATE_VIEW
22390 20 20 20 20 20 20 20 20 20 38 20 20 20 2f 2a 20 8 /*
223a0 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 View Name
223b0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
223c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
223d0 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 20 E_DELETE
223e0 20 20 20 20 20 20 20 20 39 20 20 20 2f 2a 20 54 9 /* T
223f0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e able Name N
22400 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
22410 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
22420 5f 44 52 4f 50 5f 49 4e 44 45 58 20 20 20 20 20 _DROP_INDEX
22430 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 49 6e 10 /* In
22440 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 dex Name Ta
22450 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f ble Name */
22460 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
22470 44 52 4f 50 5f 54 41 42 4c 45 20 20 20 20 20 20 DROP_TABLE
22480 20 20 20 20 20 31 31 20 20 20 2f 2a 20 54 61 62 11 /* Tab
22490 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c le Name NUL
224a0 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
224b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
224c0 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 20 20 ROP_TEMP_INDEX
224d0 20 20 20 20 31 32 20 20 20 2f 2a 20 49 6e 64 65 12 /* Inde
224e0 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c x Name Tabl
224f0 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 e Name */.#
22500 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 define SQLITE_DR
22510 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 20 20 20 OP_TEMP_TABLE
22520 20 20 20 31 33 20 20 20 2f 2a 20 54 61 62 6c 65 13 /* Table
22530 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 Name NULL
22540 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
22550 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f efine SQLITE_DRO
22560 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 20 20 P_TEMP_TRIGGER
22570 20 20 31 34 20 20 20 2f 2a 20 54 72 69 67 67 65 14 /* Trigge
22580 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20 r Name Table
22590 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 Name */.#de
225a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 fine SQLITE_DROP
225b0 5f 54 45 4d 50 5f 56 49 45 57 20 20 20 20 20 20 _TEMP_VIEW
225c0 20 31 35 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 15 /* View Na
225d0 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 me NULL
225e0 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
225f0 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f ine SQLITE_DROP_
22600 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20 20 TRIGGER
22610 31 36 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 16 /* Trigger
22620 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 Name Table Na
22630 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 me */.#defi
22640 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 ne SQLITE_DROP_V
22650 49 45 57 20 20 20 20 20 20 20 20 20 20 20 20 31 IEW 1
22660 37 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 7 /* View Name
22670 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 NULL
22680 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
22690 65 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 20 e SQLITE_INSERT
226a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 38 18
226b0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
226c0 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 NULL
226d0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
226e0 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41 20 20 SQLITE_PRAGMA
226f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39 20 19
22700 20 20 2f 2a 20 50 72 61 67 6d 61 20 4e 61 6d 65 /* Pragma Name
22710 20 20 20 20 20 31 73 74 20 61 72 67 20 6f 72 20 1st arg or
22720 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NULL */.#define
22730 53 51 4c 49 54 45 5f 52 45 41 44 20 20 20 20 20 SQLITE_READ
22740 20 20 20 20 20 20 20 20 20 20 20 20 32 30 20 20 20
22750 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 /* Table Name
22760 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 Column Name
22770 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
22780 51 4c 49 54 45 5f 53 45 4c 45 43 54 20 20 20 20 QLITE_SELECT
22790 20 20 20 20 20 20 20 20 20 20 20 32 31 20 20 20 21
227a0 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 /* NULL
227b0 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 NULL
227c0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
227d0 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e LITE_TRANSACTION
227e0 20 20 20 20 20 20 20 20 20 20 32 32 20 20 20 2f 22 /
227f0 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 20 20 * Operation
22800 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
22810 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
22820 49 54 45 5f 55 50 44 41 54 45 20 20 20 20 20 20 ITE_UPDATE
22830 20 20 20 20 20 20 20 20 20 32 33 20 20 20 2f 2a 23 /*
22840 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
22850 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 Column Name
22860 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
22870 54 45 5f 41 54 54 41 43 48 20 20 20 20 20 20 20 TE_ATTACH
22880 20 20 20 20 20 20 20 20 32 34 20 20 20 2f 2a 20 24 /*
22890 46 69 6c 65 6e 61 6d 65 20 20 20 20 20 20 20 20 Filename
228a0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
228b0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
228c0 45 5f 44 45 54 41 43 48 20 20 20 20 20 20 20 20 E_DETACH
228d0 20 20 20 20 20 20 20 32 35 20 20 20 2f 2a 20 44 25 /* D
228e0 61 74 61 62 61 73 65 20 4e 61 6d 65 20 20 20 4e atabase Name N
228f0 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
22900 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
22910 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 20 20 20 _ALTER_TABLE
22920 20 20 20 20 20 20 32 36 20 20 20 2f 2a 20 44 61 26 /* Da
22930 74 61 62 61 73 65 20 4e 61 6d 65 20 20 20 54 61 tabase Name Ta
22940 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f ble Name */
22950 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
22960 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 REINDEX
22970 20 20 20 20 20 32 37 20 20 20 2f 2a 20 49 6e 64 27 /* Ind
22980 65 78 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c ex Name NUL
22990 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
229a0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
229b0 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 20 NALYZE
229c0 20 20 20 20 32 38 20 20 20 2f 2a 20 54 61 62 6c 28 /* Tabl
229d0 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c e Name NULL
229e0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
229f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 define SQLITE_CR
22a00 45 41 54 45 5f 56 54 41 42 4c 45 20 20 20 20 20 EATE_VTABLE
22a10 20 20 20 32 39 20 20 20 2f 2a 20 54 61 62 6c 65 29 /* Table
22a20 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75 6c Name Modul
22a30 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 e Name */.#d
22a40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f efine SQLITE_DRO
22a50 50 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 P_VTABLE
22a60 20 20 33 30 20 20 20 2f 2a 20 54 61 62 6c 65 20 30 /* Table
22a70 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75 6c 65 Name Module
22a80 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 Name */.#de
22a90 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 fine SQLITE_FUNC
22aa0 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 TION
22ab0 20 33 31 20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 31 /* NULL
22ac0 20 20 20 20 20 20 20 20 20 46 75 6e 63 74 69 6f Functio
22ad0 6e 20 4e 61 6d 65 20 20 20 2a 2f 0a 23 64 65 66 n Name */.#def
22ae0 69 6e 65 20 53 51 4c 49 54 45 5f 53 41 56 45 50 ine SQLITE_SAVEP
22af0 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 OINT
22b00 33 32 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 32 /* Operatio
22b10 6e 20 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e n Savepoin
22b20 74 20 4e 61 6d 65 20 20 2a 2f 0a 23 64 65 66 69 t Name */.#defi
22b30 6e 65 20 53 51 4c 49 54 45 5f 43 4f 50 59 20 20 ne SQLITE_COPY
22b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22b50 30 20 20 20 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 0 /* No longer
22b60 20 75 73 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 used */../*.**
22b70 43 41 50 49 33 52 45 46 3a 20 54 72 61 63 69 6e CAPI3REF: Tracin
22b80 67 20 41 6e 64 20 50 72 6f 66 69 6c 69 6e 67 20 g And Profiling
22b90 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 Functions.**.**
22ba0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 These routines r
22bb0 65 67 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b egister callback
22bc0 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
22bd0 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a can be used for.
22be0 2a 2a 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70 ** tracing and p
22bf0 72 6f 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65 rofiling the exe
22c00 63 75 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 cution of SQL st
22c10 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 atements..**.**
22c20 5e 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 ^The callback fu
22c30 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 nction registere
22c40 64 20 62 79 20 73 71 6c 69 74 65 33 5f 74 72 61 d by sqlite3_tra
22c50 63 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 ce() is invoked
22c60 61 74 0a 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 at.** various ti
22c70 6d 65 73 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 mes when an SQL
22c80 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62 65 69 statement is bei
22c90 6e 67 20 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 ng run by [sqlit
22ca0 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 5e e3_step()]..** ^
22cb0 54 68 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63 The sqlite3_trac
22cc0 65 28 29 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 e() callback is
22cd0 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 20 55 invoked with a U
22ce0 54 46 2d 38 20 72 65 6e 64 65 72 69 6e 67 20 6f TF-8 rendering o
22cf0 66 20 74 68 65 0a 2a 2a 20 53 51 4c 20 73 74 61 f the.** SQL sta
22d00 74 65 6d 65 6e 74 20 74 65 78 74 20 61 73 20 74 tement text as t
22d10 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 69 72 he statement fir
22d20 73 74 20 62 65 67 69 6e 73 20 65 78 65 63 75 74 st begins execut
22d30 69 6e 67 2e 0a 2a 2a 20 5e 28 41 64 64 69 74 69 ing..** ^(Additi
22d40 6f 6e 61 6c 20 73 71 6c 69 74 65 33 5f 74 72 61 onal sqlite3_tra
22d50 63 65 28 29 20 63 61 6c 6c 62 61 63 6b 73 20 6d ce() callbacks m
22d60 69 67 68 74 20 6f 63 63 75 72 0a 2a 2a 20 61 73 ight occur.** as
22d70 20 65 61 63 68 20 74 72 69 67 67 65 72 65 64 20 each triggered
22d80 73 75 62 70 72 6f 67 72 61 6d 20 69 73 20 65 6e subprogram is en
22d90 74 65 72 65 64 2e 20 20 54 68 65 20 63 61 6c 6c tered. The call
22da0 62 61 63 6b 73 20 66 6f 72 20 74 72 69 67 67 65 backs for trigge
22db0 72 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 20 rs.** contain a
22dc0 55 54 46 2d 38 20 53 51 4c 20 63 6f 6d 6d 65 6e UTF-8 SQL commen
22dd0 74 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 65 t that identifie
22de0 73 20 74 68 65 20 74 72 69 67 67 65 72 2e 29 5e s the trigger.)^
22df0 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 63 61 6c 6c .**.** ^The call
22e00 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 back function re
22e10 67 69 73 74 65 72 65 64 20 62 79 20 73 71 6c 69 gistered by sqli
22e20 74 65 33 5f 70 72 6f 66 69 6c 65 28 29 20 69 73 te3_profile() is
22e30 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 61 73 20 65 invoked.** as e
22e40 61 63 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ach SQL statemen
22e50 74 20 66 69 6e 69 73 68 65 73 2e 20 20 5e 54 68 t finishes. ^Th
22e60 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 e profile callba
22e70 63 6b 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 ck contains.** t
22e80 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 he original stat
22e90 65 6d 65 6e 74 20 74 65 78 74 20 61 6e 64 20 61 ement text and a
22ea0 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 77 61 n estimate of wa
22eb0 6c 6c 2d 63 6c 6f 63 6b 20 74 69 6d 65 0a 2a 2a ll-clock time.**
22ec0 20 6f 66 20 68 6f 77 20 6c 6f 6e 67 20 74 68 61 of how long tha
22ed0 74 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 6f 6b t statement took
22ee0 20 74 6f 20 72 75 6e 2e 20 20 5e 54 68 65 20 70 to run. ^The p
22ef0 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 0a rofile callback.
22f00 2a 2a 20 74 69 6d 65 20 69 73 20 69 6e 20 75 6e ** time is in un
22f10 69 74 73 20 6f 66 20 6e 61 6e 6f 73 65 63 6f 6e its of nanosecon
22f20 64 73 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20 ds, however the
22f30 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e current implemen
22f40 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 6e 6c tation.** is onl
22f50 79 20 63 61 70 61 62 6c 65 20 6f 66 20 6d 69 6c y capable of mil
22f60 6c 69 73 65 63 6f 6e 64 20 72 65 73 6f 6c 75 74 lisecond resolut
22f70 69 6f 6e 20 73 6f 20 74 68 65 20 73 69 78 20 6c ion so the six l
22f80 65 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 east significant
22f90 0a 2a 2a 20 64 69 67 69 74 73 20 69 6e 20 74 68 .** digits in th
22fa0 65 20 74 69 6d 65 20 61 72 65 20 6d 65 61 6e 69 e time are meani
22fb0 6e 67 6c 65 73 73 2e 20 20 46 75 74 75 72 65 20 ngless. Future
22fc0 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 versions of SQLi
22fd0 74 65 0a 2a 2a 20 6d 69 67 68 74 20 70 72 6f 76 te.** might prov
22fe0 69 64 65 20 67 72 65 61 74 65 72 20 72 65 73 6f ide greater reso
22ff0 6c 75 74 69 6f 6e 20 6f 6e 20 74 68 65 20 70 72 lution on the pr
23000 6f 66 69 6c 65 72 20 63 61 6c 6c 62 61 63 6b 2e ofiler callback.
23010 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 The.** sqlite3
23020 5f 70 72 6f 66 69 6c 65 28 29 20 66 75 6e 63 74 _profile() funct
23030 69 6f 6e 20 69 73 20 63 6f 6e 73 69 64 65 72 65 ion is considere
23040 64 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 d experimental a
23050 6e 64 20 69 73 0a 2a 2a 20 73 75 62 6a 65 63 74 nd is.** subject
23060 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 66 75 to change in fu
23070 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 ture versions of
23080 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 SQLite..*/.SQLI
23090 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
230a0 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c 69 74 ite3_trace(sqlit
230b0 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54 72 61 63 e3*, void(*xTrac
230c0 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 e)(void*,const c
230d0 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53 har*), void*);.S
230e0 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 QLITE_API SQLITE
230f0 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f _EXPERIMENTAL vo
23100 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66 id *sqlite3_prof
23110 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c 0a 20 20 ile(sqlite3*,.
23120 20 76 6f 69 64 28 2a 78 50 72 6f 66 69 6c 65 29 void(*xProfile)
23130 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 (void*,const cha
23140 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 r*,sqlite3_uint6
23150 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 4), void*);../*.
23160 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 51 75 65 ** CAPI3REF: Que
23170 72 79 20 50 72 6f 67 72 65 73 73 20 43 61 6c 6c ry Progress Call
23180 62 61 63 6b 73 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 backs.**.** ^The
23190 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 sqlite3_progres
231a0 73 5f 68 61 6e 64 6c 65 72 28 44 2c 4e 2c 58 2c s_handler(D,N,X,
231b0 50 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 P) interface cau
231c0 73 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b ses the callback
231d0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 58 20 74 .** function X t
231e0 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 65 72 o be invoked per
231f0 69 6f 64 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 iodically during
23200 20 6c 6f 6e 67 20 72 75 6e 6e 69 6e 67 20 63 61 long running ca
23210 6c 6c 73 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 lls to.** [sqlit
23220 65 33 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c e3_exec()], [sql
23230 69 74 65 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 ite3_step()] and
23240 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 [sqlite3_get_ta
23250 62 6c 65 28 29 5d 20 66 6f 72 0a 2a 2a 20 64 61 ble()] for.** da
23260 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
23270 6e 20 44 2e 20 20 41 6e 20 65 78 61 6d 70 6c 65 n D. An example
23280 20 75 73 65 20 66 6f 72 20 74 68 69 73 0a 2a 2a use for this.**
23290 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 74 6f interface is to
232a0 20 6b 65 65 70 20 61 20 47 55 49 20 75 70 64 61 keep a GUI upda
232b0 74 65 64 20 64 75 72 69 6e 67 20 61 20 6c 61 72 ted during a lar
232c0 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 ge query..**.**
232d0 5e 54 68 65 20 70 61 72 61 6d 65 74 65 72 20 50 ^The parameter P
232e0 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 is passed throu
232f0 67 68 20 61 73 20 74 68 65 20 6f 6e 6c 79 20 70 gh as the only p
23300 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 arameter to the
23310 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e .** callback fun
23320 63 74 69 6f 6e 20 58 2e 20 20 5e 54 68 65 20 70 ction X. ^The p
23330 61 72 61 6d 65 74 65 72 20 4e 20 69 73 20 74 68 arameter N is th
23340 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 2a 2a 20 e number of .**
23350 5b 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 [virtual machine
23360 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 5d 20 74 instructions] t
23370 68 61 74 20 61 72 65 20 65 76 61 6c 75 61 74 65 hat are evaluate
23380 64 20 62 65 74 77 65 65 6e 20 73 75 63 63 65 73 d between succes
23390 73 69 76 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 sive.** invocati
233a0 6f 6e 73 20 6f 66 20 74 68 65 20 63 61 6c 6c 62 ons of the callb
233b0 61 63 6b 20 58 2e 0a 2a 2a 0a 2a 2a 20 5e 4f 6e ack X..**.** ^On
233c0 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 67 ly a single prog
233d0 72 65 73 73 20 68 61 6e 64 6c 65 72 20 6d 61 79 ress handler may
233e0 20 62 65 20 64 65 66 69 6e 65 64 20 61 74 20 6f be defined at o
233f0 6e 65 20 74 69 6d 65 20 70 65 72 0a 2a 2a 20 5b ne time per.** [
23400 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
23410 69 6f 6e 5d 3b 20 73 65 74 74 69 6e 67 20 61 20 ion]; setting a
23420 6e 65 77 20 70 72 6f 67 72 65 73 73 20 68 61 6e new progress han
23430 64 6c 65 72 20 63 61 6e 63 65 6c 73 20 74 68 65 dler cancels the
23440 0a 2a 2a 20 6f 6c 64 20 6f 6e 65 2e 20 20 5e 53 .** old one. ^S
23450 65 74 74 69 6e 67 20 70 61 72 61 6d 65 74 65 72 etting parameter
23460 20 58 20 74 6f 20 4e 55 4c 4c 20 64 69 73 61 62 X to NULL disab
23470 6c 65 73 20 74 68 65 20 70 72 6f 67 72 65 73 73 les the progress
23480 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 5e 54 68 handler..** ^Th
23490 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c e progress handl
234a0 65 72 20 69 73 20 61 6c 73 6f 20 64 69 73 61 62 er is also disab
234b0 6c 65 64 20 62 79 20 73 65 74 74 69 6e 67 20 4e led by setting N
234c0 20 74 6f 20 61 20 76 61 6c 75 65 20 6c 65 73 73 to a value less
234d0 0a 2a 2a 20 74 68 61 6e 20 31 2e 0a 2a 2a 0a 2a .** than 1..**.*
234e0 2a 20 5e 49 66 20 74 68 65 20 70 72 6f 67 72 65 * ^If the progre
234f0 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 ss callback retu
23500 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 rns non-zero, th
23510 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a e operation is.*
23520 2a 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 20 * interrupted.
23530 54 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e This feature can
23540 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c be used to impl
23550 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e 63 ement a.** "Canc
23560 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 20 el" button on a
23570 47 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 61 GUI progress dia
23580 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54 log box..**.** T
23590 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 he progress hand
235a0 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d 75 73 ler callback mus
235b0 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e t not do anythin
235c0 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 g that will modi
235d0 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 fy.** the databa
235e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 se connection th
235f0 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 70 at invoked the p
23600 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 72 2e rogress handler.
23610 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 .** Note that [s
23620 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
23630 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 2()] and [sqlite
23640 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 3_step()] both m
23650 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 odify their.** d
23660 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
23670 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e ons for the mean
23680 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 ing of "modify"
23690 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 in this paragrap
236a0 68 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f h..**.*/.SQLITE_
236b0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
236c0 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 _progress_handle
236d0 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c r(sqlite3*, int,
236e0 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20 int(*)(void*),
236f0 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 void*);../*.** C
23700 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 69 6e 67 API3REF: Opening
23710 20 41 20 4e 65 77 20 44 61 74 61 62 61 73 65 20 A New Database
23720 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a Connection.**.**
23730 20 5e 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 ^These routines
23740 20 6f 70 65 6e 20 61 6e 20 53 51 4c 69 74 65 20 open an SQLite
23750 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 73 database file as
23760 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 specified by th
23770 65 20 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 e .** filename a
23780 72 67 75 6d 65 6e 74 2e 20 5e 54 68 65 20 66 69 rgument. ^The fi
23790 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 lename argument
237a0 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 is interpreted a
237b0 73 20 55 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 s UTF-8 for.** s
237c0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e qlite3_open() an
237d0 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 d sqlite3_open_v
237e0 32 28 29 20 61 6e 64 20 61 73 20 55 54 46 2d 31 2() and as UTF-1
237f0 36 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 6 in the native
23800 62 79 74 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f byte.** order fo
23810 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 r sqlite3_open16
23820 28 29 2e 20 5e 28 41 20 5b 64 61 74 61 62 61 73 (). ^(A [databas
23830 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 e connection] ha
23840 6e 64 6c 65 20 69 73 20 75 73 75 61 6c 6c 79 0a ndle is usually.
23850 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a ** returned in *
23860 70 70 44 62 2c 20 65 76 65 6e 20 69 66 20 61 6e ppDb, even if an
23870 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 20 error occurs.
23880 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 74 69 The only excepti
23890 6f 6e 20 69 73 20 74 68 61 74 0a 2a 2a 20 69 66 on is that.** if
238a0 20 53 51 4c 69 74 65 20 69 73 20 75 6e 61 62 6c SQLite is unabl
238b0 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 e to allocate me
238c0 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 mory to hold the
238d0 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 [sqlite3] objec
238e0 74 2c 0a 2a 2a 20 61 20 4e 55 4c 4c 20 77 69 6c t,.** a NULL wil
238f0 6c 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 l be written int
23900 6f 20 2a 70 70 44 62 20 69 6e 73 74 65 61 64 20 o *ppDb instead
23910 6f 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 of a pointer to
23920 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 0a 2a 2a the [sqlite3].**
23930 20 6f 62 6a 65 63 74 2e 29 5e 20 5e 28 49 66 20 object.)^ ^(If
23940 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
23950 6f 70 65 6e 65 64 20 28 61 6e 64 2f 6f 72 20 63 opened (and/or c
23960 72 65 61 74 65 64 29 20 73 75 63 63 65 73 73 66 reated) successf
23970 75 6c 6c 79 2c 20 74 68 65 6e 0a 2a 2a 20 5b 53 ully, then.** [S
23980 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 QLITE_OK] is ret
23990 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73 urned. Otherwis
239a0 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 e an [error code
239b0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 29 5e ] is returned.)^
239c0 20 5e 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 ^The.** [sqlite
239d0 33 5f 65 72 72 6d 73 67 28 29 5d 20 6f 72 20 5b 3_errmsg()] or [
239e0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 sqlite3_errmsg16
239f0 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 63 61 6e ()] routines can
23a00 20 62 65 20 75 73 65 64 20 74 6f 20 6f 62 74 61 be used to obta
23a10 69 6e 0a 2a 2a 20 61 6e 20 45 6e 67 6c 69 73 68 in.** an English
23a20 20 6c 61 6e 67 75 61 67 65 20 64 65 73 63 72 69 language descri
23a30 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 65 72 72 ption of the err
23a40 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 66 or following a f
23a50 61 69 6c 75 72 65 20 6f 66 20 61 6e 79 0a 2a 2a ailure of any.**
23a60 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
23a70 6f 70 65 6e 28 29 20 72 6f 75 74 69 6e 65 73 2e open() routines.
23a80 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 64 65 66 61 .**.** ^The defa
23a90 75 6c 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 ult encoding for
23aa0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 69 the database wi
23ab0 6c 6c 20 62 65 20 55 54 46 2d 38 20 69 66 0a 2a ll be UTF-8 if.*
23ac0 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 * sqlite3_open()
23ad0 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e or sqlite3_open
23ae0 5f 76 32 28 29 20 69 73 20 63 61 6c 6c 65 64 20 _v2() is called
23af0 61 6e 64 0a 2a 2a 20 55 54 46 2d 31 36 20 69 6e and.** UTF-16 in
23b00 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 the native byte
23b10 20 6f 72 64 65 72 20 69 66 20 73 71 6c 69 74 65 order if sqlite
23b20 33 5f 6f 70 65 6e 31 36 28 29 20 69 73 20 75 73 3_open16() is us
23b30 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 ed..**.** Whethe
23b40 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f r or not an erro
23b50 72 20 6f 63 63 75 72 73 20 77 68 65 6e 20 69 74 r occurs when it
23b60 20 69 73 20 6f 70 65 6e 65 64 2c 20 72 65 73 6f is opened, reso
23b70 75 72 63 65 73 0a 2a 2a 20 61 73 73 6f 63 69 61 urces.** associa
23b80 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61 ted with the [da
23b90 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
23ba0 6e 5d 20 68 61 6e 64 6c 65 20 73 68 6f 75 6c 64 n] handle should
23bb0 20 62 65 20 72 65 6c 65 61 73 65 64 20 62 79 0a be released by.
23bc0 2a 2a 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f ** passing it to
23bd0 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 [sqlite3_close(
23be0 29 5d 20 77 68 65 6e 20 69 74 20 69 73 20 6e 6f )] when it is no
23bf0 20 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 longer required
23c00 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
23c10 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 6e te3_open_v2() in
23c20 74 65 72 66 61 63 65 20 77 6f 72 6b 73 20 6c 69 terface works li
23c30 6b 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 ke sqlite3_open(
23c40 29 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 ).** except that
23c50 20 69 74 20 61 63 63 65 70 74 73 20 74 77 6f 20 it accepts two
23c60 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d additional param
23c70 65 74 65 72 73 20 66 6f 72 20 61 64 64 69 74 69 eters for additi
23c80 6f 6e 61 6c 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 onal control.**
23c90 6f 76 65 72 20 74 68 65 20 6e 65 77 20 64 61 74 over the new dat
23ca0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
23cb0 2e 20 20 5e 28 54 68 65 20 66 6c 61 67 73 20 70 . ^(The flags p
23cc0 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 73 arameter to.** s
23cd0 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
23ce0 20 63 61 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 can take one of
23cf0 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e .** the followin
23d00 67 20 74 68 72 65 65 20 76 61 6c 75 65 73 2c 20 g three values,
23d10 6f 70 74 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 optionally combi
23d20 6e 65 64 20 77 69 74 68 20 74 68 65 20 0a 2a 2a ned with the .**
23d30 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f [SQLITE_OPEN_NO
23d40 4d 55 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f MUTEX], [SQLITE_
23d50 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c OPEN_FULLMUTEX],
23d60 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 [SQLITE_OPEN_SH
23d70 41 52 45 44 43 41 43 48 45 5d 2c 0a 2a 2a 20 5b AREDCACHE],.** [
23d80 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 SQLITE_OPEN_PRIV
23d90 41 54 45 43 41 43 48 45 5d 2c 20 61 6e 64 2f 6f ATECACHE], and/o
23da0 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 r [SQLITE_OPEN_U
23db0 52 49 5d 20 66 6c 61 67 73 3a 29 5e 0a 2a 2a 0a RI] flags:)^.**.
23dc0 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 5e 28 3c 64 74 ** <dl>.** ^(<dt
23dd0 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 >[SQLITE_OPEN_RE
23de0 41 44 4f 4e 4c 59 5d 3c 2f 64 74 3e 0a 2a 2a 20 ADONLY]</dt>.**
23df0 3c 64 64 3e 54 68 65 20 64 61 74 61 62 61 73 65 <dd>The database
23e00 20 69 73 20 6f 70 65 6e 65 64 20 69 6e 20 72 65 is opened in re
23e10 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 2e 20 20 49 ad-only mode. I
23e20 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 64 f the database d
23e30 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 oes not.** alrea
23e40 64 79 20 65 78 69 73 74 2c 20 61 6e 20 65 72 72 dy exist, an err
23e50 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 3c or is returned.<
23e60 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 28 3c /dd>)^.**.** ^(<
23e70 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f dt>[SQLITE_OPEN_
23e80 52 45 41 44 57 52 49 54 45 5d 3c 2f 64 74 3e 0a READWRITE]</dt>.
23e90 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62 ** <dd>The datab
23ea0 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f ase is opened fo
23eb0 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 r reading and wr
23ec0 69 74 69 6e 67 20 69 66 20 70 6f 73 73 69 62 6c iting if possibl
23ed0 65 2c 20 6f 72 20 72 65 61 64 69 6e 67 0a 2a 2a e, or reading.**
23ee0 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 66 69 6c only if the fil
23ef0 65 20 69 73 20 77 72 69 74 65 20 70 72 6f 74 65 e is write prote
23f00 63 74 65 64 20 62 79 20 74 68 65 20 6f 70 65 72 cted by the oper
23f10 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 49 ating system. I
23f20 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63 61 73 65 n either.** case
23f30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 the database mu
23f40 73 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 st already exist
23f50 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 , otherwise an e
23f60 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 rror is returned
23f70 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5e .</dd>)^.**.** ^
23f80 28 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 (<dt>[SQLITE_OPE
23f90 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b N_READWRITE] | [
23fa0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
23fb0 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e TE]</dt>.** <dd>
23fc0 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 The database is
23fd0 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 opened for readi
23fe0 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 ng and writing,
23ff0 61 6e 64 20 69 73 20 63 72 65 61 74 65 64 20 69 and is created i
24000 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e 6f 74 f.** it does not
24010 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 20 already exist.
24020 54 68 69 73 20 69 73 20 74 68 65 20 62 65 68 61 This is the beha
24030 76 69 6f 72 20 74 68 61 74 20 69 73 20 61 6c 77 vior that is alw
24040 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 ays used for.**
24050 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 sqlite3_open() a
24060 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 nd sqlite3_open1
24070 36 28 29 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 20 3c 6().</dd>)^.** <
24080 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 /dl>.**.** If th
24090 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 e 3rd parameter
240a0 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f to sqlite3_open_
240b0 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20 v2() is not one
240c0 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e of the.** combin
240d0 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f ations shown abo
240e0 76 65 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 63 6f ve optionally co
240f0 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 mbined with othe
24100 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 r.** [SQLITE_OPE
24110 4e 5f 52 45 41 44 4f 4e 4c 59 20 7c 20 53 51 4c N_READONLY | SQL
24120 49 54 45 5f 4f 50 45 4e 5f 2a 20 62 69 74 73 5d ITE_OPEN_* bits]
24130 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68 .** then the beh
24140 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
24150 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 74 68 ed..**.** ^If th
24160 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e e [SQLITE_OPEN_N
24170 4f 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20 OMUTEX] flag is
24180 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 64 61 set, then the da
24190 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
241a0 6e 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e 20 74 68 n.** opens in th
241b0 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 20 5b e multi-thread [
241c0 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 threading mode]
241d0 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 73 as long as the s
241e0 69 6e 67 6c 65 2d 74 68 72 65 61 64 0a 2a 2a 20 ingle-thread.**
241f0 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20 62 65 65 mode has not bee
24200 6e 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 n set at compile
24210 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d 74 -time or start-t
24220 69 6d 65 2e 20 20 5e 49 66 20 74 68 65 0a 2a 2a ime. ^If the.**
24230 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 [SQLITE_OPEN_FU
24240 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 LLMUTEX] flag is
24250 20 73 65 74 20 74 68 65 6e 20 74 68 65 20 64 61 set then the da
24260 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
24270 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 74 68 n opens.** in th
24280 65 20 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 e serialized [th
24290 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 75 6e reading mode] un
242a0 6c 65 73 73 20 73 69 6e 67 6c 65 2d 74 68 72 65 less single-thre
242b0 61 64 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f ad was.** previo
242c0 75 73 6c 79 20 73 65 6c 65 63 74 65 64 20 61 74 usly selected at
242d0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 compile-time or
242e0 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 20 start-time..**
242f0 5e 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 ^The [SQLITE_OPE
24300 4e 5f 53 48 41 52 45 44 43 41 43 48 45 5d 20 66 N_SHAREDCACHE] f
24310 6c 61 67 20 63 61 75 73 65 73 20 74 68 65 20 64 lag causes the d
24320 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
24330 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 6c 69 67 on to be.** elig
24340 69 62 6c 65 20 74 6f 20 75 73 65 20 5b 73 68 61 ible to use [sha
24350 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c red cache mode],
24360 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 regardless of w
24370 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 68 hether or not sh
24380 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 73 ared.** cache is
24390 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 20 5b enabled using [
243a0 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 sqlite3_enable_s
243b0 68 61 72 65 64 5f 63 61 63 68 65 28 29 5d 2e 20 hared_cache()].
243c0 20 5e 54 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 ^The.** [SQLITE
243d0 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45 43 41 43 _OPEN_PRIVATECAC
243e0 48 45 5d 20 66 6c 61 67 20 63 61 75 73 65 73 20 HE] flag causes
243f0 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
24400 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 0a 2a nection to not.*
24410 2a 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e * participate in
24420 20 5b 73 68 61 72 65 64 20 63 61 63 68 65 20 6d [shared cache m
24430 6f 64 65 5d 20 65 76 65 6e 20 69 66 20 69 74 20 ode] even if it
24440 69 73 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a is enabled..**.*
24450 2a 20 5e 54 68 65 20 66 6f 75 72 74 68 20 70 61 * ^The fourth pa
24460 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
24470 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 e3_open_v2() is
24480 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a the name of the.
24490 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d ** [sqlite3_vfs]
244a0 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65 66 object that def
244b0 69 6e 65 73 20 74 68 65 20 6f 70 65 72 61 74 69 ines the operati
244c0 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 ng system interf
244d0 61 63 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 ace that.** the
244e0 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e new database con
244f0 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 nection should u
24500 73 65 2e 20 20 5e 49 66 20 74 68 65 20 66 6f 75 se. ^If the fou
24510 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 rth parameter is
24520 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 .** a NULL point
24530 65 72 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 er then the defa
24540 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 ult [sqlite3_vfs
24550 5d 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 ] object is used
24560 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 74 68 65 20 ..**.** ^If the
24570 66 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 filename is ":me
24580 6d 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70 mory:", then a p
24590 72 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 rivate, temporar
245a0 79 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 y in-memory data
245b0 62 61 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74 base.** is creat
245c0 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 ed for the conne
245d0 63 74 69 6f 6e 2e 20 20 5e 54 68 69 73 20 69 6e ction. ^This in
245e0 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
245f0 20 77 69 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 will vanish whe
24600 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 n.** the databas
24610 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 e connection is
24620 63 6c 6f 73 65 64 2e 20 20 46 75 74 75 72 65 20 closed. Future
24630 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 versions of SQLi
24640 74 65 20 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 te might.** make
24650 20 75 73 65 20 6f 66 20 61 64 64 69 74 69 6f 6e use of addition
24660 61 6c 20 73 70 65 63 69 61 6c 20 66 69 6c 65 6e al special filen
24670 61 6d 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 ames that begin
24680 77 69 74 68 20 74 68 65 20 22 3a 22 20 63 68 61 with the ":" cha
24690 72 61 63 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 racter..** It is
246a0 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 recommended tha
246b0 74 20 77 68 65 6e 20 61 20 64 61 74 61 62 61 73 t when a databas
246c0 65 20 66 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 e filename actua
246d0 6c 6c 79 20 64 6f 65 73 20 62 65 67 69 6e 20 77 lly does begin w
246e0 69 74 68 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 ith.** a ":" cha
246f0 72 61 63 74 65 72 20 79 6f 75 20 73 68 6f 75 6c racter you shoul
24700 64 20 70 72 65 66 69 78 20 74 68 65 20 66 69 6c d prefix the fil
24710 65 6e 61 6d 65 20 77 69 74 68 20 61 20 70 61 74 ename with a pat
24720 68 6e 61 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a hname such as.**
24730 20 22 2e 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 "./" to avoid a
24740 6d 62 69 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 mbiguity..**.**
24750 5e 49 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 ^If the filename
24760 20 69 73 20 61 6e 20 65 6d 70 74 79 20 73 74 72 is an empty str
24770 69 6e 67 2c 20 74 68 65 6e 20 61 20 70 72 69 76 ing, then a priv
24780 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79 0a 2a ate, temporary.*
24790 2a 20 6f 6e 2d 64 69 73 6b 20 64 61 74 61 62 61 * on-disk databa
247a0 73 65 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 se will be creat
247b0 65 64 2e 20 20 5e 54 68 69 73 20 70 72 69 76 61 ed. ^This priva
247c0 74 65 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c te database will
247d0 20 62 65 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 be.** automatic
247e0 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 61 73 20 ally deleted as
247f0 73 6f 6f 6e 20 61 73 20 74 68 65 20 64 61 74 61 soon as the data
24800 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
24810 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a is closed..**.**
24820 20 5b 5b 55 52 49 20 66 69 6c 65 6e 61 6d 65 73 [[URI filenames
24830 20 69 6e 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e in sqlite3_open
24840 28 29 5d 5d 20 3c 68 33 3e 55 52 49 20 46 69 6c ()]] <h3>URI Fil
24850 65 6e 61 6d 65 73 3c 2f 68 33 3e 0a 2a 2a 0a 2a enames</h3>.**.*
24860 2a 20 5e 49 66 20 5b 55 52 49 20 66 69 6c 65 6e * ^If [URI filen
24870 61 6d 65 5d 20 69 6e 74 65 72 70 72 65 74 61 74 ame] interpretat
24880 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 ion is enabled,
24890 61 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 and the filename
248a0 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 62 65 67 argument.** beg
248b0 69 6e 73 20 77 69 74 68 20 22 66 69 6c 65 3a 22 ins with "file:"
248c0 2c 20 74 68 65 6e 20 74 68 65 20 66 69 6c 65 6e , then the filen
248d0 61 6d 65 20 69 73 20 69 6e 74 65 72 70 72 65 74 ame is interpret
248e0 65 64 20 61 73 20 61 20 55 52 49 2e 20 5e 55 52 ed as a URI. ^UR
248f0 49 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 69 6e I.** filename in
24900 74 65 72 70 72 65 74 61 74 69 6f 6e 20 69 73 20 terpretation is
24910 65 6e 61 62 6c 65 64 20 69 66 20 74 68 65 20 5b enabled if the [
24920 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49 5d SQLITE_OPEN_URI]
24930 20 66 6c 61 67 20 69 73 0a 2a 2a 20 73 65 74 20 flag is.** set
24940 69 6e 20 74 68 65 20 66 6f 75 72 74 68 20 61 72 in the fourth ar
24950 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 gument to sqlite
24960 33 5f 6f 70 65 6e 5f 76 32 28 29 2c 20 6f 72 20 3_open_v2(), or
24970 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 62 65 65 if it has.** bee
24980 6e 20 65 6e 61 62 6c 65 64 20 67 6c 6f 62 61 6c n enabled global
24990 6c 79 20 75 73 69 6e 67 20 74 68 65 20 5b 53 51 ly using the [SQ
249a0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 55 52 49 5d LITE_CONFIG_URI]
249b0 20 6f 70 74 69 6f 6e 20 77 69 74 68 20 74 68 65 option with the
249c0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e .** [sqlite3_con
249d0 66 69 67 28 29 5d 20 6d 65 74 68 6f 64 20 6f 72 fig()] method or
249e0 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54 45 5f by the [SQLITE_
249f0 55 53 45 5f 55 52 49 5d 20 63 6f 6d 70 69 6c 65 USE_URI] compile
24a00 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2e 0a 2a 2a -time option..**
24a10 20 41 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 As of SQLite ve
24a20 72 73 69 6f 6e 20 33 2e 37 2e 37 2c 20 55 52 49 rsion 3.7.7, URI
24a30 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 65 72 70 filename interp
24a40 72 65 74 61 74 69 6f 6e 20 69 73 20 74 75 72 6e retation is turn
24a50 65 64 20 6f 66 66 0a 2a 2a 20 62 79 20 64 65 66 ed off.** by def
24a60 61 75 6c 74 2c 20 62 75 74 20 66 75 74 75 72 65 ault, but future
24a70 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c releases of SQL
24a80 69 74 65 20 6d 69 67 68 74 20 65 6e 61 62 6c 65 ite might enable
24a90 20 55 52 49 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a URI filename.**
24aa0 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 interpretation
24ab0 62 79 20 64 65 66 61 75 6c 74 2e 20 20 53 65 65 by default. See
24ac0 20 22 5b 55 52 49 20 66 69 6c 65 6e 61 6d 65 73 "[URI filenames
24ad0 5d 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 ]" for additiona
24ae0 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e l.** information
24af0 2e 0a 2a 2a 0a 2a 2a 20 55 52 49 20 66 69 6c 65 ..**.** URI file
24b00 6e 61 6d 65 73 20 61 72 65 20 70 61 72 73 65 64 names are parsed
24b10 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 52 46 according to RF
24b20 43 20 33 39 38 36 2e 20 5e 49 66 20 74 68 65 20 C 3986. ^If the
24b30 55 52 49 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a URI contains an.
24b40 2a 2a 20 61 75 74 68 6f 72 69 74 79 2c 20 74 68 ** authority, th
24b50 65 6e 20 69 74 20 6d 75 73 74 20 62 65 20 65 69 en it must be ei
24b60 74 68 65 72 20 61 6e 20 65 6d 70 74 79 20 73 74 ther an empty st
24b70 72 69 6e 67 20 6f 72 20 74 68 65 20 73 74 72 69 ring or the stri
24b80 6e 67 20 0a 2a 2a 20 22 6c 6f 63 61 6c 68 6f 73 ng .** "localhos
24b90 74 22 2e 20 5e 49 66 20 74 68 65 20 61 75 74 68 t". ^If the auth
24ba0 6f 72 69 74 79 20 69 73 20 6e 6f 74 20 61 6e 20 ority is not an
24bb0 65 6d 70 74 79 20 73 74 72 69 6e 67 20 6f 72 20 empty string or
24bc0 22 6c 6f 63 61 6c 68 6f 73 74 22 2c 20 61 6e 20 "localhost", an
24bd0 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 72 65 74 .** error is ret
24be0 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c urned to the cal
24bf0 6c 65 72 2e 20 5e 54 68 65 20 66 72 61 67 6d 65 ler. ^The fragme
24c00 6e 74 20 63 6f 6d 70 6f 6e 65 6e 74 20 6f 66 20 nt component of
24c10 61 20 55 52 49 2c 20 69 66 20 0a 2a 2a 20 70 72 a URI, if .** pr
24c20 65 73 65 6e 74 2c 20 69 73 20 69 67 6e 6f 72 65 esent, is ignore
24c30 64 2e 0a 2a 2a 0a 2a 2a 20 5e 53 51 4c 69 74 65 d..**.** ^SQLite
24c40 20 75 73 65 73 20 74 68 65 20 70 61 74 68 20 63 uses the path c
24c50 6f 6d 70 6f 6e 65 6e 74 20 6f 66 20 74 68 65 20 omponent of the
24c60 55 52 49 20 61 73 20 74 68 65 20 6e 61 6d 65 20 URI as the name
24c70 6f 66 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 of the disk file
24c80 0a 2a 2a 20 77 68 69 63 68 20 63 6f 6e 74 61 69 .** which contai
24c90 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ns the database.
24ca0 20 5e 49 66 20 74 68 65 20 70 61 74 68 20 62 65 ^If the path be
24cb0 67 69 6e 73 20 77 69 74 68 20 61 20 27 2f 27 20 gins with a '/'
24cc0 63 68 61 72 61 63 74 65 72 2c 20 0a 2a 2a 20 74 character, .** t
24cd0 68 65 6e 20 69 74 20 69 73 20 69 6e 74 65 72 70 hen it is interp
24ce0 72 65 74 65 64 20 61 73 20 61 6e 20 61 62 73 6f reted as an abso
24cf0 6c 75 74 65 20 70 61 74 68 2e 20 5e 49 66 20 74 lute path. ^If t
24d00 68 65 20 70 61 74 68 20 64 6f 65 73 20 6e 6f 74 he path does not
24d10 20 62 65 67 69 6e 20 0a 2a 2a 20 77 69 74 68 20 begin .** with
24d20 61 20 27 2f 27 20 28 6d 65 61 6e 69 6e 67 20 74 a '/' (meaning t
24d30 68 61 74 20 74 68 65 20 61 75 74 68 6f 72 69 74 hat the authorit
24d40 79 20 73 65 63 74 69 6f 6e 20 69 73 20 6f 6d 69 y section is omi
24d50 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 55 52 tted from the UR
24d60 49 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 I).** then the p
24d70 61 74 68 20 69 73 20 69 6e 74 65 72 70 72 65 74 ath is interpret
24d80 65 64 20 61 73 20 61 20 72 65 6c 61 74 69 76 65 ed as a relative
24d90 20 70 61 74 68 2e 20 0a 2a 2a 20 5e 4f 6e 20 77 path. .** ^On w
24da0 69 6e 64 6f 77 73 2c 20 74 68 65 20 66 69 72 73 indows, the firs
24db0 74 20 63 6f 6d 70 6f 6e 65 6e 74 20 6f 66 20 61 t component of a
24dc0 6e 20 61 62 73 6f 6c 75 74 65 20 70 61 74 68 20 n absolute path
24dd0 0a 2a 2a 20 69 73 20 61 20 64 72 69 76 65 20 73 .** is a drive s
24de0 70 65 63 69 66 69 63 61 74 69 6f 6e 20 28 65 2e pecification (e.
24df0 67 2e 20 22 43 3a 22 29 2e 0a 2a 2a 0a 2a 2a 20 g. "C:")..**.**
24e00 5b 5b 63 6f 72 65 20 55 52 49 20 71 75 65 72 79 [[core URI query
24e10 20 70 61 72 61 6d 65 74 65 72 73 5d 5d 0a 2a 2a parameters]].**
24e20 20 54 68 65 20 71 75 65 72 79 20 63 6f 6d 70 6f The query compo
24e30 6e 65 6e 74 20 6f 66 20 61 20 55 52 49 20 6d 61 nent of a URI ma
24e40 79 20 63 6f 6e 74 61 69 6e 20 70 61 72 61 6d 65 y contain parame
24e50 74 65 72 73 20 74 68 61 74 20 61 72 65 20 69 6e ters that are in
24e60 74 65 72 70 72 65 74 65 64 0a 2a 2a 20 65 69 74 terpreted.** eit
24e70 68 65 72 20 62 79 20 53 51 4c 69 74 65 20 69 74 her by SQLite it
24e80 73 65 6c 66 2c 20 6f 72 20 62 79 20 61 20 5b 56 self, or by a [V
24e90 46 53 20 7c 20 63 75 73 74 6f 6d 20 56 46 53 20 FS | custom VFS
24ea0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 5d 2e implementation].
24eb0 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 74 65 72 .** SQLite inter
24ec0 70 72 65 74 73 20 74 68 65 20 66 6f 6c 6c 6f 77 prets the follow
24ed0 69 6e 67 20 74 68 72 65 65 20 71 75 65 72 79 20 ing three query
24ee0 70 61 72 61 6d 65 74 65 72 73 3a 0a 2a 2a 0a 2a parameters:.**.*
24ef0 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e * <ul>.** <li>
24f00 20 3c 62 3e 76 66 73 3c 2f 62 3e 3a 20 5e 54 68 <b>vfs</b>: ^Th
24f10 65 20 22 76 66 73 22 20 70 61 72 61 6d 65 74 65 e "vfs" paramete
24f20 72 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f r may be used to
24f30 20 73 70 65 63 69 66 79 20 74 68 65 20 6e 61 6d specify the nam
24f40 65 20 6f 66 0a 2a 2a 20 20 20 20 20 61 20 56 46 e of.** a VF
24f50 53 20 6f 62 6a 65 63 74 20 74 68 61 74 20 70 72 S object that pr
24f60 6f 76 69 64 65 73 20 74 68 65 20 6f 70 65 72 61 ovides the opera
24f70 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 ting system inte
24f80 72 66 61 63 65 20 74 68 61 74 20 73 68 6f 75 6c rface that shoul
24f90 64 0a 2a 2a 20 20 20 20 20 62 65 20 75 73 65 64 d.** be used
24fa0 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 to access the d
24fb0 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 atabase file on
24fc0 64 69 73 6b 2e 20 5e 49 66 20 74 68 69 73 20 6f disk. ^If this o
24fd0 70 74 69 6f 6e 20 69 73 20 73 65 74 20 74 6f 0a ption is set to.
24fe0 2a 2a 20 20 20 20 20 61 6e 20 65 6d 70 74 79 20 ** an empty
24ff0 73 74 72 69 6e 67 20 74 68 65 20 64 65 66 61 75 string the defau
25000 6c 74 20 56 46 53 20 6f 62 6a 65 63 74 20 69 73 lt VFS object is
25010 20 75 73 65 64 2e 20 5e 53 70 65 63 69 66 79 69 used. ^Specifyi
25020 6e 67 20 61 6e 20 75 6e 6b 6e 6f 77 6e 0a 2a 2a ng an unknown.**
25030 20 20 20 20 20 56 46 53 20 69 73 20 61 6e 20 65 VFS is an e
25040 72 72 6f 72 2e 20 5e 49 66 20 73 71 6c 69 74 65 rror. ^If sqlite
25050 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 75 3_open_v2() is u
25060 73 65 64 20 61 6e 64 20 74 68 65 20 76 66 73 20 sed and the vfs
25070 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 option is.**
25080 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 74 present, then t
25090 68 65 20 56 46 53 20 73 70 65 63 69 66 69 65 64 he VFS specified
250a0 20 62 79 20 74 68 65 20 6f 70 74 69 6f 6e 20 74 by the option t
250b0 61 6b 65 73 20 70 72 65 63 65 64 65 6e 63 65 20 akes precedence
250c0 6f 76 65 72 0a 2a 2a 20 20 20 20 20 74 68 65 20 over.** the
250d0 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 value passed as
250e0 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d the fourth param
250f0 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f eter to sqlite3_
25100 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a 2a 2a open_v2()..**.**
25110 20 20 20 3c 6c 69 3e 20 3c 62 3e 6d 6f 64 65 3c <li> <b>mode<
25120 2f 62 3e 3a 20 5e 28 54 68 65 20 6d 6f 64 65 20 /b>: ^(The mode
25130 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 62 65 parameter may be
25140 20 73 65 74 20 74 6f 20 65 69 74 68 65 72 20 22 set to either "
25150 72 6f 22 2c 20 22 72 77 22 2c 0a 2a 2a 20 20 20 ro", "rw",.**
25160 20 20 22 72 77 63 22 2c 20 6f 72 20 22 6d 65 6d "rwc", or "mem
25170 6f 72 79 22 2e 20 41 74 74 65 6d 70 74 69 6e 67 ory". Attempting
25180 20 74 6f 20 73 65 74 20 69 74 20 74 6f 20 61 6e to set it to an
25190 79 20 6f 74 68 65 72 20 76 61 6c 75 65 20 69 73 y other value is
251a0 0a 2a 2a 20 20 20 20 20 61 6e 20 65 72 72 6f 72 .** an error
251b0 29 5e 2e 20 0a 2a 2a 20 20 20 20 20 5e 49 66 20 )^. .** ^If
251c0 22 72 6f 22 20 69 73 20 73 70 65 63 69 66 69 65 "ro" is specifie
251d0 64 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 d, then the data
251e0 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 base is opened f
251f0 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 0a 2a 2a or read-only .**
25200 20 20 20 20 20 61 63 63 65 73 73 2c 20 6a 75 73 access, jus
25210 74 20 61 73 20 69 66 20 74 68 65 20 5b 53 51 4c t as if the [SQL
25220 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
25230 59 5d 20 66 6c 61 67 20 68 61 64 20 62 65 65 6e Y] flag had been
25240 20 73 65 74 20 69 6e 20 74 68 65 20 0a 2a 2a 20 set in the .**
25250 20 20 20 20 74 68 69 72 64 20 61 72 67 75 6d 65 third argume
25260 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 nt to sqlite3_op
25270 65 6e 5f 76 32 28 29 2e 20 5e 49 66 20 74 68 65 en_v2(). ^If the
25280 20 6d 6f 64 65 20 6f 70 74 69 6f 6e 20 69 73 20 mode option is
25290 73 65 74 20 74 6f 20 0a 2a 2a 20 20 20 20 20 22 set to .** "
252a0 72 77 22 2c 20 74 68 65 6e 20 74 68 65 20 64 61 rw", then the da
252b0 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 tabase is opened
252c0 20 66 6f 72 20 72 65 61 64 2d 77 72 69 74 65 20 for read-write
252d0 28 62 75 74 20 6e 6f 74 20 63 72 65 61 74 65 29 (but not create)
252e0 20 0a 2a 2a 20 20 20 20 20 61 63 63 65 73 73 2c .** access,
252f0 20 61 73 20 69 66 20 53 51 4c 49 54 45 5f 4f 50 as if SQLITE_OP
25300 45 4e 5f 52 45 41 44 57 52 49 54 45 20 28 62 75 EN_READWRITE (bu
25310 74 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4f 50 45 t not SQLITE_OPE
25320 4e 5f 43 52 45 41 54 45 29 20 68 61 64 20 0a 2a N_CREATE) had .*
25330 2a 20 20 20 20 20 62 65 65 6e 20 73 65 74 2e 20 * been set.
25340 5e 56 61 6c 75 65 20 22 72 77 63 22 20 69 73 20 ^Value "rwc" is
25350 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 73 65 equivalent to se
25360 74 74 69 6e 67 20 62 6f 74 68 20 0a 2a 2a 20 20 tting both .**
25370 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 SQLITE_OPEN_R
25380 45 41 44 57 52 49 54 45 20 61 6e 64 20 53 51 4c EADWRITE and SQL
25390 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2e ITE_OPEN_CREATE.
253a0 20 20 5e 49 66 20 74 68 65 20 6d 6f 64 65 20 6f ^If the mode o
253b0 70 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 20 ption is.**
253c0 73 65 74 20 74 6f 20 22 6d 65 6d 6f 72 79 22 20 set to "memory"
253d0 74 68 65 6e 20 61 20 70 75 72 65 20 5b 69 6e 2d then a pure [in-
253e0 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 5d memory database]
253f0 20 74 68 61 74 20 6e 65 76 65 72 20 72 65 61 64 that never read
25400 73 0a 2a 2a 20 20 20 20 20 6f 72 20 77 72 69 74 s.** or writ
25410 65 73 20 66 72 6f 6d 20 64 69 73 6b 20 69 73 20 es from disk is
25420 75 73 65 64 2e 20 5e 49 74 20 69 73 20 61 6e 20 used. ^It is an
25430 65 72 72 6f 72 20 74 6f 20 73 70 65 63 69 66 79 error to specify
25440 20 61 20 76 61 6c 75 65 20 66 6f 72 0a 2a 2a 20 a value for.**
25450 20 20 20 20 74 68 65 20 6d 6f 64 65 20 70 61 72 the mode par
25460 61 6d 65 74 65 72 20 74 68 61 74 20 69 73 20 6c ameter that is l
25470 65 73 73 20 72 65 73 74 72 69 63 74 69 76 65 20 ess restrictive
25480 74 68 61 6e 20 74 68 61 74 20 73 70 65 63 69 66 than that specif
25490 69 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 74 68 ied by.** th
254a0 65 20 66 6c 61 67 73 20 70 61 73 73 65 64 20 69 e flags passed i
254b0 6e 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 n the third para
254c0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 meter to sqlite3
254d0 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a 2a _open_v2()..**.*
254e0 2a 20 20 20 3c 6c 69 3e 20 3c 62 3e 63 61 63 68 * <li> <b>cach
254f0 65 3c 2f 62 3e 3a 20 5e 54 68 65 20 63 61 63 68 e</b>: ^The cach
25500 65 20 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 e parameter may
25510 62 65 20 73 65 74 20 74 6f 20 65 69 74 68 65 72 be set to either
25520 20 22 73 68 61 72 65 64 22 20 6f 72 0a 2a 2a 20 "shared" or.**
25530 20 20 20 20 22 70 72 69 76 61 74 65 22 2e 20 5e "private". ^
25540 53 65 74 74 69 6e 67 20 69 74 20 74 6f 20 22 73 Setting it to "s
25550 68 61 72 65 64 22 20 69 73 20 65 71 75 69 76 61 hared" is equiva
25560 6c 65 6e 74 20 74 6f 20 73 65 74 74 69 6e 67 20 lent to setting
25570 74 68 65 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 the.** SQLIT
25580 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 E_OPEN_SHAREDCAC
25590 48 45 20 62 69 74 20 69 6e 20 74 68 65 20 66 6c HE bit in the fl
255a0 61 67 73 20 61 72 67 75 6d 65 6e 74 20 70 61 73 ags argument pas
255b0 73 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 73 71 sed to.** sq
255c0 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e lite3_open_v2().
255d0 20 5e 53 65 74 74 69 6e 67 20 74 68 65 20 63 61 ^Setting the ca
255e0 63 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f che parameter to
255f0 20 22 70 72 69 76 61 74 65 22 20 69 73 20 0a 2a "private" is .*
25600 2a 20 20 20 20 20 65 71 75 69 76 61 6c 65 6e 74 * equivalent
25610 20 74 6f 20 73 65 74 74 69 6e 67 20 74 68 65 20 to setting the
25620 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 SQLITE_OPEN_PRIV
25630 41 54 45 43 41 43 48 45 20 62 69 74 2e 0a 2a 2a ATECACHE bit..**
25640 20 20 20 20 20 5e 49 66 20 73 71 6c 69 74 65 33 ^If sqlite3
25650 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 75 73 _open_v2() is us
25660 65 64 20 61 6e 64 20 74 68 65 20 22 63 61 63 68 ed and the "cach
25670 65 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 e" parameter is
25680 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 20 20 present in.**
25690 20 20 61 20 55 52 49 20 66 69 6c 65 6e 61 6d 65 a URI filename
256a0 2c 20 69 74 73 20 76 61 6c 75 65 20 6f 76 65 72 , its value over
256b0 72 69 64 65 73 20 61 6e 79 20 62 65 68 61 76 69 rides any behavi
256c0 6f 75 72 20 72 65 71 75 65 73 74 65 64 20 62 79 our requested by
256d0 20 73 65 74 74 69 6e 67 0a 2a 2a 20 20 20 20 20 setting.**
256e0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 SQLITE_OPEN_PRIV
256f0 41 54 45 43 41 43 48 45 20 6f 72 20 53 51 4c 49 ATECACHE or SQLI
25700 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 TE_OPEN_SHAREDCA
25710 43 48 45 20 66 6c 61 67 2e 0a 2a 2a 20 3c 2f 75 CHE flag..** </u
25720 6c 3e 0a 2a 2a 0a 2a 2a 20 5e 53 70 65 63 69 66 l>.**.** ^Specif
25730 79 69 6e 67 20 61 6e 20 75 6e 6b 6e 6f 77 6e 20 ying an unknown
25740 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68 65 parameter in the
25750 20 71 75 65 72 79 20 63 6f 6d 70 6f 6e 65 6e 74 query component
25760 20 6f 66 20 61 20 55 52 49 20 69 73 20 6e 6f 74 of a URI is not
25770 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 2e 20 20 46 an.** error. F
25780 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f uture versions o
25790 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 20 75 f SQLite might u
257a0 6e 64 65 72 73 74 61 6e 64 20 61 64 64 69 74 69 nderstand additi
257b0 6f 6e 61 6c 20 71 75 65 72 79 0a 2a 2a 20 70 61 onal query.** pa
257c0 72 61 6d 65 74 65 72 73 2e 20 20 53 65 65 20 22 rameters. See "
257d0 5b 71 75 65 72 79 20 70 61 72 61 6d 65 74 65 72 [query parameter
257e0 73 20 77 69 74 68 20 73 70 65 63 69 61 6c 20 6d s with special m
257f0 65 61 6e 69 6e 67 20 74 6f 20 53 51 4c 69 74 65 eaning to SQLite
25800 5d 22 20 66 6f 72 0a 2a 2a 20 61 64 64 69 74 69 ]" for.** additi
25810 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
25820 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 55 52 49 20 66 69 ..**.** [[URI fi
25830 6c 65 6e 61 6d 65 20 65 78 61 6d 70 6c 65 73 5d lename examples]
25840 5d 20 3c 68 33 3e 55 52 49 20 66 69 6c 65 6e 61 ] <h3>URI filena
25850 6d 65 20 65 78 61 6d 70 6c 65 73 3c 2f 68 33 3e me examples</h3>
25860 0a 2a 2a 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f .**.** <table bo
25870 72 64 65 72 3d 22 31 22 20 61 6c 69 67 6e 3d 63 rder="1" align=c
25880 65 6e 74 65 72 20 63 65 6c 6c 70 61 64 64 69 6e enter cellpaddin
25890 67 3d 35 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e g=5>.** <tr><th>
258a0 20 55 52 49 20 66 69 6c 65 6e 61 6d 65 73 20 3c URI filenames <
258b0 74 68 3e 20 52 65 73 75 6c 74 73 0a 2a 2a 20 3c th> Results.** <
258c0 74 72 3e 3c 74 64 3e 20 66 69 6c 65 3a 64 61 74 tr><td> file:dat
258d0 61 2e 64 62 20 3c 74 64 3e 20 0a 2a 2a 20 20 20 a.db <td> .**
258e0 20 20 20 20 20 20 20 4f 70 65 6e 20 74 68 65 20 Open the
258f0 66 69 6c 65 20 22 64 61 74 61 2e 64 62 22 20 69 file "data.db" i
25900 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 69 n the current di
25910 72 65 63 74 6f 72 79 2e 0a 2a 2a 20 3c 74 72 3e rectory..** <tr>
25920 3c 74 64 3e 20 66 69 6c 65 3a 2f 68 6f 6d 65 2f <td> file:/home/
25930 66 72 65 64 2f 64 61 74 61 2e 64 62 3c 62 72 3e fred/data.db<br>
25940 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c .** fil
25950 65 3a 2f 2f 2f 68 6f 6d 65 2f 66 72 65 64 2f 64 e:///home/fred/d
25960 61 74 61 2e 64 62 20 3c 62 72 3e 20 0a 2a 2a 20 ata.db <br> .**
25970 20 20 20 20 20 20 20 20 20 66 69 6c 65 3a 2f 2f file://
25980 6c 6f 63 61 6c 68 6f 73 74 2f 68 6f 6d 65 2f 66 localhost/home/f
25990 72 65 64 2f 64 61 74 61 2e 64 62 20 3c 62 72 3e red/data.db <br>
259a0 20 3c 74 64 3e 20 0a 2a 2a 20 20 20 20 20 20 20 <td> .**
259b0 20 20 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61 Open the data
259c0 62 61 73 65 20 66 69 6c 65 20 22 2f 68 6f 6d 65 base file "/home
259d0 2f 66 72 65 64 2f 64 61 74 61 2e 64 62 22 2e 0a /fred/data.db"..
259e0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 66 69 6c 65 ** <tr><td> file
259f0 3a 2f 2f 64 61 72 6b 73 74 61 72 2f 68 6f 6d 65 ://darkstar/home
25a00 2f 66 72 65 64 2f 64 61 74 61 2e 64 62 20 3c 74 /fred/data.db <t
25a10 64 3e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 d> .**
25a20 41 6e 20 65 72 72 6f 72 2e 20 22 64 61 72 6b 73 An error. "darks
25a30 74 61 72 22 20 69 73 20 6e 6f 74 20 61 20 72 65 tar" is not a re
25a40 63 6f 67 6e 69 7a 65 64 20 61 75 74 68 6f 72 69 cognized authori
25a50 74 79 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 20 73 ty..** <tr><td s
25a60 74 79 6c 65 3d 22 77 68 69 74 65 2d 73 70 61 63 tyle="white-spac
25a70 65 3a 6e 6f 77 72 61 70 22 3e 20 0a 2a 2a 20 20 e:nowrap"> .**
25a80 20 20 20 20 20 20 20 20 66 69 6c 65 3a 2f 2f 2f file:///
25a90 43 3a 2f 44 6f 63 75 6d 65 6e 74 73 25 32 30 61 C:/Documents%20a
25aa0 6e 64 25 32 30 53 65 74 74 69 6e 67 73 2f 66 72 nd%20Settings/fr
25ab0 65 64 2f 44 65 73 6b 74 6f 70 2f 64 61 74 61 2e ed/Desktop/data.
25ac0 64 62 0a 2a 2a 20 20 20 20 20 3c 74 64 3e 20 57 db.** <td> W
25ad0 69 6e 64 6f 77 73 20 6f 6e 6c 79 3a 20 4f 70 65 indows only: Ope
25ae0 6e 20 74 68 65 20 66 69 6c 65 20 22 64 61 74 61 n the file "data
25af0 2e 64 62 22 20 6f 6e 20 66 72 65 64 27 73 20 64 .db" on fred's d
25b00 65 73 6b 74 6f 70 20 6f 6e 20 64 72 69 76 65 0a esktop on drive.
25b10 2a 2a 20 20 20 20 20 20 20 20 20 20 43 3a 2e 20 ** C:.
25b20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 25 32 Note that the %2
25b30 30 20 65 73 63 61 70 69 6e 67 20 69 6e 20 74 68 0 escaping in th
25b40 69 73 20 65 78 61 6d 70 6c 65 20 69 73 20 6e 6f is example is no
25b50 74 20 73 74 72 69 63 74 6c 79 20 0a 2a 2a 20 20 t strictly .**
25b60 20 20 20 20 20 20 20 20 6e 65 63 65 73 73 61 72 necessar
25b70 79 20 2d 20 73 70 61 63 65 20 63 68 61 72 61 63 y - space charac
25b80 74 65 72 73 20 63 61 6e 20 62 65 20 75 73 65 64 ters can be used
25b90 20 6c 69 74 65 72 61 6c 6c 79 0a 2a 2a 20 20 20 literally.**
25ba0 20 20 20 20 20 20 20 69 6e 20 55 52 49 20 66 69 in URI fi
25bb0 6c 65 6e 61 6d 65 73 2e 0a 2a 2a 20 3c 74 72 3e lenames..** <tr>
25bc0 3c 74 64 3e 20 66 69 6c 65 3a 64 61 74 61 2e 64 <td> file:data.d
25bd0 62 3f 6d 6f 64 65 3d 72 6f 26 63 61 63 68 65 3d b?mode=ro&cache=
25be0 70 72 69 76 61 74 65 20 3c 74 64 3e 20 0a 2a 2a private <td> .**
25bf0 20 20 20 20 20 20 20 20 20 20 4f 70 65 6e 20 66 Open f
25c00 69 6c 65 20 22 64 61 74 61 2e 64 62 22 20 69 6e ile "data.db" in
25c10 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 69 72 the current dir
25c20 65 63 74 6f 72 79 20 66 6f 72 20 72 65 61 64 2d ectory for read-
25c30 6f 6e 6c 79 20 61 63 63 65 73 73 2e 0a 2a 2a 20 only access..**
25c40 20 20 20 20 20 20 20 20 20 52 65 67 61 72 64 6c Regardl
25c50 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f ess of whether o
25c60 72 20 6e 6f 74 20 73 68 61 72 65 64 2d 63 61 63 r not shared-cac
25c70 68 65 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c he mode is enabl
25c80 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20 ed by.**
25c90 20 20 64 65 66 61 75 6c 74 2c 20 75 73 65 20 61 default, use a
25ca0 20 70 72 69 76 61 74 65 20 63 61 63 68 65 2e 0a private cache..
25cb0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 66 69 6c 65 ** <tr><td> file
25cc0 3a 2f 68 6f 6d 65 2f 66 72 65 64 2f 64 61 74 61 :/home/fred/data
25cd0 2e 64 62 3f 76 66 73 3d 75 6e 69 78 2d 6e 6f 6c .db?vfs=unix-nol
25ce0 6f 63 6b 20 3c 74 64 3e 0a 2a 2a 20 20 20 20 20 ock <td>.**
25cf0 20 20 20 20 20 4f 70 65 6e 20 66 69 6c 65 20 22 Open file "
25d00 2f 68 6f 6d 65 2f 66 72 65 64 2f 64 61 74 61 2e /home/fred/data.
25d10 64 62 22 2e 20 55 73 65 20 74 68 65 20 73 70 65 db". Use the spe
25d20 63 69 61 6c 20 56 46 53 20 22 75 6e 69 78 2d 6e cial VFS "unix-n
25d30 6f 6c 6f 63 6b 22 2e 0a 2a 2a 20 3c 74 72 3e 3c olock"..** <tr><
25d40 74 64 3e 20 66 69 6c 65 3a 64 61 74 61 2e 64 62 td> file:data.db
25d50 3f 6d 6f 64 65 3d 72 65 61 64 6f 6e 6c 79 20 3c ?mode=readonly <
25d60 74 64 3e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 td> .**
25d70 20 41 6e 20 65 72 72 6f 72 2e 20 22 72 65 61 64 An error. "read
25d80 6f 6e 6c 79 22 20 69 73 20 6e 6f 74 20 61 20 76 only" is not a v
25d90 61 6c 69 64 20 6f 70 74 69 6f 6e 20 66 6f 72 20 alid option for
25da0 74 68 65 20 22 6d 6f 64 65 22 20 70 61 72 61 6d the "mode" param
25db0 65 74 65 72 2e 0a 2a 2a 20 3c 2f 74 61 62 6c 65 eter..** </table
25dc0 3e 0a 2a 2a 0a 2a 2a 20 5e 55 52 49 20 68 65 78 >.**.** ^URI hex
25dd0 61 64 65 63 69 6d 61 6c 20 65 73 63 61 70 65 20 adecimal escape
25de0 73 65 71 75 65 6e 63 65 73 20 28 25 48 48 29 20 sequences (%HH)
25df0 61 72 65 20 73 75 70 70 6f 72 74 65 64 20 77 69 are supported wi
25e00 74 68 69 6e 20 74 68 65 20 70 61 74 68 20 61 6e thin the path an
25e10 64 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6d 70 6f d.** query compo
25e20 6e 65 6e 74 73 20 6f 66 20 61 20 55 52 49 2e 20 nents of a URI.
25e30 41 20 68 65 78 61 64 65 63 69 6d 61 6c 20 65 73 A hexadecimal es
25e40 63 61 70 65 20 73 65 71 75 65 6e 63 65 20 63 6f cape sequence co
25e50 6e 73 69 73 74 73 20 6f 66 20 61 0a 2a 2a 20 70 nsists of a.** p
25e60 65 72 63 65 6e 74 20 73 69 67 6e 20 2d 20 22 25 ercent sign - "%
25e70 22 20 2d 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 " - followed by
25e80 65 78 61 63 74 6c 79 20 74 77 6f 20 68 65 78 61 exactly two hexa
25e90 64 65 63 69 6d 61 6c 20 64 69 67 69 74 73 20 0a decimal digits .
25ea0 2a 2a 20 73 70 65 63 69 66 79 69 6e 67 20 61 6e ** specifying an
25eb0 20 6f 63 74 65 74 20 76 61 6c 75 65 2e 20 5e 42 octet value. ^B
25ec0 65 66 6f 72 65 20 74 68 65 20 70 61 74 68 20 6f efore the path o
25ed0 72 20 71 75 65 72 79 20 63 6f 6d 70 6f 6e 65 6e r query componen
25ee0 74 73 20 6f 66 20 61 0a 2a 2a 20 55 52 49 20 66 ts of a.** URI f
25ef0 69 6c 65 6e 61 6d 65 20 61 72 65 20 69 6e 74 65 ilename are inte
25f00 72 70 72 65 74 65 64 2c 20 74 68 65 79 20 61 72 rpreted, they ar
25f10 65 20 65 6e 63 6f 64 65 64 20 75 73 69 6e 67 20 e encoded using
25f20 55 54 46 2d 38 20 61 6e 64 20 61 6c 6c 20 0a 2a UTF-8 and all .*
25f30 2a 20 68 65 78 61 64 65 63 69 6d 61 6c 20 65 73 * hexadecimal es
25f40 63 61 70 65 20 73 65 71 75 65 6e 63 65 73 20 72 cape sequences r
25f50 65 70 6c 61 63 65 64 20 62 79 20 61 20 73 69 6e eplaced by a sin
25f60 67 6c 65 20 62 79 74 65 20 63 6f 6e 74 61 69 6e gle byte contain
25f70 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 ing the.** corre
25f80 73 70 6f 6e 64 69 6e 67 20 6f 63 74 65 74 2e 20 sponding octet.
25f90 49 66 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 If this process
25fa0 67 65 6e 65 72 61 74 65 73 20 61 6e 20 69 6e 76 generates an inv
25fb0 61 6c 69 64 20 55 54 46 2d 38 20 65 6e 63 6f 64 alid UTF-8 encod
25fc0 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 ing,.** the resu
25fd0 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 lts are undefine
25fe0 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 65 d..**.** <b>Note
25ff0 20 74 6f 20 57 69 6e 64 6f 77 73 20 75 73 65 72 to Windows user
26000 73 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63 6f s:</b> The enco
26010 64 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68 ding used for th
26020 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d e filename argum
26030 65 6e 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65 ent.** of sqlite
26040 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 3_open() and sql
26050 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 6d ite3_open_v2() m
26060 75 73 74 20 62 65 20 55 54 46 2d 38 2c 20 6e 6f ust be UTF-8, no
26070 74 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 63 6f t whatever.** co
26080 64 65 70 61 67 65 20 69 73 20 63 75 72 72 65 6e depage is curren
26090 74 6c 79 20 64 65 66 69 6e 65 64 2e 20 20 46 69 tly defined. Fi
260a0 6c 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e 69 lenames containi
260b0 6e 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61 6c ng international
260c0 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 6d .** characters m
260d0 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 ust be converted
260e0 20 74 6f 20 55 54 46 2d 38 20 70 72 69 6f 72 20 to UTF-8 prior
260f0 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65 6d 20 to passing them
26100 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f into.** sqlite3_
26110 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 open() or sqlite
26120 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a 3_open_v2()..**.
26130 2a 2a 20 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 ** <b>Note to Wi
26140 6e 64 6f 77 73 20 52 75 6e 74 69 6d 65 20 75 73 ndows Runtime us
26150 65 72 73 3a 3c 2f 62 3e 20 20 54 68 65 20 74 65 ers:</b> The te
26160 6d 70 6f 72 61 72 79 20 64 69 72 65 63 74 6f 72 mporary director
26170 79 20 6d 75 73 74 20 62 65 20 73 65 74 0a 2a 2a y must be set.**
26180 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e prior to callin
26190 67 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 g sqlite3_open()
261a0 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e or sqlite3_open
261b0 5f 76 32 28 29 2e 20 20 4f 74 68 65 72 77 69 73 _v2(). Otherwis
261c0 65 2c 20 76 61 72 69 6f 75 73 0a 2a 2a 20 66 65 e, various.** fe
261d0 61 74 75 72 65 73 20 74 68 61 74 20 72 65 71 75 atures that requ
261e0 69 72 65 20 74 68 65 20 75 73 65 20 6f 66 20 74 ire the use of t
261f0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 6d emporary files m
26200 61 79 20 66 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 53 ay fail..**.** S
26210 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 ee also: [sqlite
26220 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3_temp_directory
26230 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
26240 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e int sqlite3_open
26250 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a (. const char *
26260 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 filename, /* D
26270 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 atabase filename
26280 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 (UTF-8) */. sq
26290 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20 lite3 **ppDb
262a0 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 /* OUT: SQ
262b0 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a Lite db handle *
262c0 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 /.);.SQLITE_API
262d0 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e int sqlite3_open
262e0 31 36 28 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 16(. const void
262f0 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a *filename, /*
26300 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 Database filena
26310 6d 65 20 28 55 54 46 2d 31 36 29 20 2a 2f 0a 20 me (UTF-16) */.
26320 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 sqlite3 **ppDb
26330 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
26340 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c SQLite db handl
26350 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 e */.);.SQLITE_A
26360 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
26370 70 65 6e 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20 pen_v2(. const
26380 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 char *filename,
26390 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 /* Database fi
263a0 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a lename (UTF-8) *
263b0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 /. sqlite3 **pp
263c0 44 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f Db, /* O
263d0 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 UT: SQLite db ha
263e0 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c ndle */. int fl
263f0 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ags,
26400 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 /* Flags */.
26410 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 const char *zVfs
26420 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
26430 6f 66 20 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f of VFS module to
26440 20 75 73 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a use */.);../*.*
26450 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61 * CAPI3REF: Obta
26460 69 6e 20 56 61 6c 75 65 73 20 46 6f 72 20 55 52 in Values For UR
26470 49 20 50 61 72 61 6d 65 74 65 72 73 0a 2a 2a 0a I Parameters.**.
26480 2a 2a 20 54 68 65 73 65 20 61 72 65 20 75 74 69 ** These are uti
26490 6c 69 74 79 20 72 6f 75 74 69 6e 65 73 2c 20 75 lity routines, u
264a0 73 65 66 75 6c 20 74 6f 20 56 46 53 20 69 6d 70 seful to VFS imp
264b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2c 20 74 68 lementations, th
264c0 61 74 20 63 68 65 63 6b 0a 2a 2a 20 74 6f 20 73 at check.** to s
264d0 65 65 20 69 66 20 61 20 64 61 74 61 62 61 73 65 ee if a database
264e0 20 66 69 6c 65 20 77 61 73 20 61 20 55 52 49 20 file was a URI
264f0 74 68 61 74 20 63 6f 6e 74 61 69 6e 65 64 20 61 that contained a
26500 20 73 70 65 63 69 66 69 63 20 71 75 65 72 79 20 specific query
26510 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2c 20 61 .** parameter, a
26520 6e 64 20 69 66 20 73 6f 20 6f 62 74 61 69 6e 73 nd if so obtains
26530 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
26540 61 74 20 71 75 65 72 79 20 70 61 72 61 6d 65 74 at query paramet
26550 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 46 20 69 er..**.** If F i
26560 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 s the database f
26570 69 6c 65 6e 61 6d 65 20 70 6f 69 6e 74 65 72 20 ilename pointer
26580 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20 passed into the
26590 78 4f 70 65 6e 28 29 20 6d 65 74 68 6f 64 20 6f xOpen() method o
265a0 66 20 0a 2a 2a 20 61 20 56 46 53 20 69 6d 70 6c f .** a VFS impl
265b0 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68 65 6e 20 ementation when
265c0 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 the flags parame
265d0 74 65 72 20 74 6f 20 78 4f 70 65 6e 28 29 20 68 ter to xOpen() h
265e0 61 73 20 6f 6e 65 20 6f 72 20 0a 2a 2a 20 6d 6f as one or .** mo
265f0 72 65 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 re of the [SQLIT
26600 45 5f 4f 50 45 4e 5f 55 52 49 5d 20 6f 72 20 5b E_OPEN_URI] or [
26610 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
26620 5f 44 42 5d 20 62 69 74 73 20 73 65 74 20 61 6e _DB] bits set an
26630 64 0a 2a 2a 20 50 20 69 73 20 74 68 65 20 6e 61 d.** P is the na
26640 6d 65 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 me of the query
26650 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 0a parameter, then.
26660 2a 2a 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 70 ** sqlite3_uri_p
26670 61 72 61 6d 65 74 65 72 28 46 2c 50 29 20 72 65 arameter(F,P) re
26680 74 75 72 6e 73 20 74 68 65 20 76 61 6c 75 65 20 turns the value
26690 6f 66 20 74 68 65 20 50 0a 2a 2a 20 70 61 72 61 of the P.** para
266a0 6d 65 74 65 72 20 69 66 20 69 74 20 65 78 69 73 meter if it exis
266b0 74 73 20 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69 ts or a NULL poi
266c0 6e 74 65 72 20 69 66 20 50 20 64 6f 65 73 20 6e nter if P does n
266d0 6f 74 20 61 70 70 65 61 72 20 61 73 20 61 20 0a ot appear as a .
266e0 2a 2a 20 71 75 65 72 79 20 70 61 72 61 6d 65 74 ** query paramet
266f0 65 72 20 6f 6e 20 46 2e 20 20 49 66 20 50 20 69 er on F. If P i
26700 73 20 61 20 71 75 65 72 79 20 70 61 72 61 6d 65 s a query parame
26710 74 65 72 20 6f 66 20 46 0a 2a 2a 20 68 61 73 20 ter of F.** has
26720 6e 6f 20 65 78 70 6c 69 63 69 74 20 76 61 6c 75 no explicit valu
26730 65 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f e, then sqlite3_
26740 75 72 69 5f 70 61 72 61 6d 65 74 65 72 28 46 2c uri_parameter(F,
26750 50 29 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 20 P) returns.** a
26760 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 6d pointer to an em
26770 70 74 79 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a pty string..**.*
26780 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 75 72 * The sqlite3_ur
26790 69 5f 62 6f 6f 6c 65 61 6e 28 46 2c 50 2c 42 29 i_boolean(F,P,B)
267a0 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d 65 73 routine assumes
267b0 20 74 68 61 74 20 50 20 69 73 20 61 20 62 6f 6f that P is a boo
267c0 6c 65 61 6e 0a 2a 2a 20 70 61 72 61 6d 65 74 65 lean.** paramete
267d0 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 72 r and returns tr
267e0 75 65 20 28 31 29 20 6f 72 20 66 61 6c 73 65 20 ue (1) or false
267f0 28 30 29 20 61 63 63 6f 72 64 69 6e 67 20 74 6f (0) according to
26800 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 the value.** of
26810 20 50 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 P. The sqlite3
26820 5f 75 72 69 5f 62 6f 6f 6c 65 61 6e 28 46 2c 50 _uri_boolean(F,P
26830 2c 42 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 ,B) routine retu
26840 72 6e 73 20 74 72 75 65 20 28 31 29 20 69 66 20 rns true (1) if
26850 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 the.** value of
26860 71 75 65 72 79 20 70 61 72 61 6d 65 74 65 72 20 query parameter
26870 50 20 69 73 20 6f 6e 65 20 6f 66 20 22 79 65 73 P is one of "yes
26880 22 2c 20 22 74 72 75 65 22 2c 20 6f 72 20 22 6f ", "true", or "o
26890 6e 22 20 69 6e 20 61 6e 79 0a 2a 2a 20 63 61 73 n" in any.** cas
268a0 65 20 6f 72 20 69 66 20 74 68 65 20 76 61 6c 75 e or if the valu
268b0 65 20 62 65 67 69 6e 73 20 77 69 74 68 20 61 20 e begins with a
268c0 6e 6f 6e 2d 7a 65 72 6f 20 6e 75 6d 62 65 72 2e non-zero number.
268d0 20 20 54 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 The .** sqlite
268e0 33 5f 75 72 69 5f 62 6f 6f 6c 65 61 6e 28 46 2c 3_uri_boolean(F,
268f0 50 2c 42 29 20 72 6f 75 74 69 6e 65 73 20 72 65 P,B) routines re
26900 74 75 72 6e 73 20 66 61 6c 73 65 20 28 30 29 20 turns false (0)
26910 69 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a if the value of.
26920 2a 2a 20 71 75 65 72 79 20 70 61 72 61 6d 65 74 ** query paramet
26930 65 72 20 50 20 69 73 20 6f 6e 65 20 6f 66 20 22 er P is one of "
26940 6e 6f 22 2c 20 22 66 61 6c 73 65 22 2c 20 6f 72 no", "false", or
26950 20 22 6f 66 66 22 20 69 6e 20 61 6e 79 20 63 61 "off" in any ca
26960 73 65 20 6f 72 0a 2a 2a 20 69 66 20 74 68 65 20 se or.** if the
26970 76 61 6c 75 65 20 62 65 67 69 6e 73 20 77 69 74 value begins wit
26980 68 20 61 20 6e 75 6d 65 72 69 63 20 7a 65 72 6f h a numeric zero
26990 2e 20 20 49 66 20 50 20 69 73 20 6e 6f 74 20 61 . If P is not a
269a0 20 71 75 65 72 79 0a 2a 2a 20 70 61 72 61 6d 65 query.** parame
269b0 74 65 72 20 6f 6e 20 46 20 6f 72 20 69 66 20 74 ter on F or if t
269c0 68 65 20 76 61 6c 75 65 20 6f 66 20 50 20 69 73 he value of P is
269d0 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 does not match
269e0 61 6e 79 20 6f 66 20 74 68 65 0a 2a 2a 20 61 62 any of the.** ab
269f0 6f 76 65 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 ove, then sqlite
26a00 33 5f 75 72 69 5f 62 6f 6f 6c 65 61 6e 28 46 2c 3_uri_boolean(F,
26a10 50 2c 42 29 20 72 65 74 75 72 6e 73 20 28 42 21 P,B) returns (B!
26a20 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 =0)..**.** The s
26a30 71 6c 69 74 65 33 5f 75 72 69 5f 69 6e 74 36 34 qlite3_uri_int64
26a40 28 46 2c 50 2c 44 29 20 72 6f 75 74 69 6e 65 20 (F,P,D) routine
26a50 63 6f 6e 76 65 72 74 73 20 74 68 65 20 76 61 6c converts the val
26a60 75 65 20 6f 66 20 50 20 69 6e 74 6f 20 61 0a 2a ue of P into a.*
26a70 2a 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 * 64-bit signed
26a80 69 6e 74 65 67 65 72 20 61 6e 64 20 72 65 74 75 integer and retu
26a90 72 6e 73 20 74 68 61 74 20 69 6e 74 65 67 65 72 rns that integer
26aa0 2c 20 6f 72 20 44 20 69 66 20 50 20 64 6f 65 73 , or D if P does
26ab0 20 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 2e 20 20 not.** exist.
26ac0 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 If the value of
26ad0 50 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 6f P is something o
26ae0 74 68 65 72 20 74 68 61 6e 20 61 6e 20 69 6e 74 ther than an int
26af0 65 67 65 72 2c 20 74 68 65 6e 0a 2a 2a 20 7a 65 eger, then.** ze
26b00 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a ro is returned..
26b10 2a 2a 20 0a 2a 2a 20 49 66 20 46 20 69 73 20 61 ** .** If F is a
26b20 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 NULL pointer, t
26b30 68 65 6e 20 73 71 6c 69 74 65 33 5f 75 72 69 5f hen sqlite3_uri_
26b40 70 61 72 61 6d 65 74 65 72 28 46 2c 50 29 20 72 parameter(F,P) r
26b50 65 74 75 72 6e 73 20 4e 55 4c 4c 20 61 6e 64 0a eturns NULL and.
26b60 2a 2a 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 62 ** sqlite3_uri_b
26b70 6f 6f 6c 65 61 6e 28 46 2c 50 2c 42 29 20 72 65 oolean(F,P,B) re
26b80 74 75 72 6e 73 20 42 2e 20 20 49 66 20 46 20 69 turns B. If F i
26b90 73 20 6e 6f 74 20 61 20 4e 55 4c 4c 20 70 6f 69 s not a NULL poi
26ba0 6e 74 65 72 20 61 6e 64 0a 2a 2a 20 69 73 20 6e nter and.** is n
26bb0 6f 74 20 61 20 64 61 74 61 62 61 73 65 20 66 69 ot a database fi
26bc0 6c 65 20 70 61 74 68 6e 61 6d 65 20 70 6f 69 6e le pathname poin
26bd0 74 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 ter that SQLite
26be0 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20 passed into the
26bf0 78 4f 70 65 6e 0a 2a 2a 20 56 46 53 20 6d 65 74 xOpen.** VFS met
26c00 68 6f 64 2c 20 74 68 65 6e 20 74 68 65 20 62 65 hod, then the be
26c10 68 61 76 69 6f 72 20 6f 66 20 74 68 69 73 20 72 havior of this r
26c20 6f 75 74 69 6e 65 20 69 73 20 75 6e 64 65 66 69 outine is undefi
26c30 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 62 6c 79 ned and probably
26c40 0a 2a 2a 20 75 6e 64 65 73 69 72 61 62 6c 65 2e .** undesirable.
26c50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
26c60 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
26c70 65 33 5f 75 72 69 5f 70 61 72 61 6d 65 74 65 72 e3_uri_parameter
26c80 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 (const char *zFi
26c90 6c 65 6e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 lename, const ch
26ca0 61 72 20 2a 7a 50 61 72 61 6d 29 3b 0a 53 51 4c ar *zParam);.SQL
26cb0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
26cc0 74 65 33 5f 75 72 69 5f 62 6f 6f 6c 65 61 6e 28 te3_uri_boolean(
26cd0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
26ce0 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a e, const char *z
26cf0 50 61 72 61 6d 2c 20 69 6e 74 20 62 44 65 66 61 Param, int bDefa
26d00 75 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ult);.SQLITE_API
26d10 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
26d20 71 6c 69 74 65 33 5f 75 72 69 5f 69 6e 74 36 34 qlite3_uri_int64
26d30 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f (const char*, co
26d40 6e 73 74 20 63 68 61 72 2a 2c 20 73 71 6c 69 74 nst char*, sqlit
26d50 65 33 5f 69 6e 74 36 34 29 3b 0a 0a 0a 2f 2a 0a e3_int64);.../*.
26d60 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 72 72 ** CAPI3REF: Err
26d70 6f 72 20 43 6f 64 65 73 20 41 6e 64 20 4d 65 73 or Codes And Mes
26d80 73 61 67 65 73 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 sages.**.** ^The
26d90 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 sqlite3_errcode
26da0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 () interface ret
26db0 75 72 6e 73 20 74 68 65 20 6e 75 6d 65 72 69 63 urns the numeric
26dc0 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 6f [result code] o
26dd0 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 r.** [extended r
26de0 65 73 75 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20 esult code] for
26df0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
26e00 66 61 69 6c 65 64 20 73 71 6c 69 74 65 33 5f 2a failed sqlite3_*
26e10 20 41 50 49 20 63 61 6c 6c 0a 2a 2a 20 61 73 73 API call.** ass
26e20 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 5b ociated with a [
26e30 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
26e40 69 6f 6e 5d 2e 20 49 66 20 61 20 70 72 69 6f 72 ion]. If a prior
26e50 20 41 50 49 20 63 61 6c 6c 20 66 61 69 6c 65 64 API call failed
26e60 0a 2a 2a 20 62 75 74 20 74 68 65 20 6d 6f 73 74 .** but the most
26e70 20 72 65 63 65 6e 74 20 41 50 49 20 63 61 6c 6c recent API call
26e80 20 73 75 63 63 65 65 64 65 64 2c 20 74 68 65 20 succeeded, the
26e90 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f return value fro
26ea0 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 m.** sqlite3_err
26eb0 63 6f 64 65 28 29 20 69 73 20 75 6e 64 65 66 69 code() is undefi
26ec0 6e 65 64 2e 20 20 5e 54 68 65 20 73 71 6c 69 74 ned. ^The sqlit
26ed0 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 e3_extended_errc
26ee0 6f 64 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 ode().** interfa
26ef0 63 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 65 ce is the same e
26f00 78 63 65 70 74 20 74 68 61 74 20 69 74 20 61 6c xcept that it al
26f10 77 61 79 73 20 72 65 74 75 72 6e 73 20 74 68 65 ways returns the
26f20 20 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 .** [extended r
26f30 65 73 75 6c 74 20 63 6f 64 65 5d 20 65 76 65 6e esult code] even
26f40 20 77 68 65 6e 20 65 78 74 65 6e 64 65 64 20 72 when extended r
26f50 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 0a esult codes are.
26f60 2a 2a 20 64 69 73 61 62 6c 65 64 2e 0a 2a 2a 0a ** disabled..**.
26f70 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f ** ^The sqlite3_
26f80 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 6c errmsg() and sql
26f90 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 20 ite3_errmsg16()
26fa0 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 2d 6c return English-l
26fb0 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65 78 74 20 anguage.** text
26fc0 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 that describes t
26fd0 68 65 20 65 72 72 6f 72 2c 20 61 73 20 65 69 74 he error, as eit
26fe0 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 54 46 her UTF-8 or UTF
26ff0 2d 31 36 20 72 65 73 70 65 63 74 69 76 65 6c 79 -16 respectively
27000 2e 0a 2a 2a 20 5e 28 4d 65 6d 6f 72 79 20 74 6f ..** ^(Memory to
27010 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 hold the error
27020 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 69 message string i
27030 73 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 6e s managed intern
27040 61 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70 70 ally..** The app
27050 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f lication does no
27060 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 t need to worry
27070 61 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74 68 about freeing th
27080 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f 77 e result..** How
27090 65 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20 ever, the error
270a0 73 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65 20 string might be
270b0 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 64 overwritten or d
270c0 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a eallocated by.**
270d0 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
270e0 73 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 s to other SQLit
270f0 65 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63 e interface func
27100 74 69 6f 6e 73 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e tions.)^.**.** ^
27110 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 73 The sqlite3_errs
27120 74 72 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 tr() interface r
27130 65 74 75 72 6e 73 20 74 68 65 20 45 6e 67 6c 69 eturns the Engli
27140 73 68 2d 6c 61 6e 67 75 61 67 65 20 74 65 78 74 sh-language text
27150 0a 2a 2a 20 74 68 61 74 20 64 65 73 63 72 69 62 .** that describ
27160 65 73 20 74 68 65 20 5b 72 65 73 75 6c 74 20 63 es the [result c
27170 6f 64 65 5d 2c 20 61 73 20 55 54 46 2d 38 2e 0a ode], as UTF-8..
27180 2a 2a 20 5e 28 4d 65 6d 6f 72 79 20 74 6f 20 68 ** ^(Memory to h
27190 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 6d 65 old the error me
271a0 73 73 61 67 65 20 73 74 72 69 6e 67 20 69 73 20 ssage string is
271b0 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 6e 61 6c managed internal
271c0 6c 79 0a 2a 2a 20 61 6e 64 20 6d 75 73 74 20 6e ly.** and must n
271d0 6f 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74 ot be freed by t
271e0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 29 5e he application)^
271f0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 ..**.** When the
27200 20 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 serialized [thr
27210 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 eading mode] is
27220 69 6e 20 75 73 65 2c 20 69 74 20 6d 69 67 68 74 in use, it might
27230 20 62 65 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 be the.** case
27240 74 68 61 74 20 61 20 73 65 63 6f 6e 64 20 65 72 that a second er
27250 72 6f 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 ror occurs on a
27260 73 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20 separate thread
27270 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 in between.** th
27280 65 20 74 69 6d 65 20 6f 66 20 74 68 65 20 66 69 e time of the fi
27290 72 73 74 20 65 72 72 6f 72 20 61 6e 64 20 74 68 rst error and th
272a0 65 20 63 61 6c 6c 20 74 6f 20 74 68 65 73 65 20 e call to these
272b0 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 57 interfaces..** W
272c0 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 hen that happens
272d0 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 65 72 72 , the second err
272e0 6f 72 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72 or will be repor
272f0 74 65 64 20 73 69 6e 63 65 20 74 68 65 73 65 0a ted since these.
27300 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 61 6c ** interfaces al
27310 77 61 79 73 20 72 65 70 6f 72 74 20 74 68 65 20 ways report the
27320 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 75 most recent resu
27330 6c 74 2e 20 20 54 6f 20 61 76 6f 69 64 0a 2a 2a lt. To avoid.**
27340 20 74 68 69 73 2c 20 65 61 63 68 20 74 68 72 65 this, each thre
27350 61 64 20 63 61 6e 20 6f 62 74 61 69 6e 20 65 78 ad can obtain ex
27360 63 6c 75 73 69 76 65 20 75 73 65 20 6f 66 20 74 clusive use of t
27370 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
27380 6e 65 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 62 79 nection] D.** by
27390 20 69 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 invoking [sqlit
273a0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 5d 28 e3_mutex_enter](
273b0 5b 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 [sqlite3_db_mute
273c0 78 5d 28 44 29 29 20 62 65 66 6f 72 65 20 62 65 x](D)) before be
273d0 67 69 6e 6e 69 6e 67 0a 2a 2a 20 74 6f 20 75 73 ginning.** to us
273e0 65 20 44 20 61 6e 64 20 69 6e 76 6f 6b 69 6e 67 e D and invoking
273f0 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f [sqlite3_mutex_
27400 6c 65 61 76 65 5d 28 5b 73 71 6c 69 74 65 33 5f leave]([sqlite3_
27410 64 62 5f 6d 75 74 65 78 5d 28 44 29 29 20 61 66 db_mutex](D)) af
27420 74 65 72 0a 2a 2a 20 61 6c 6c 20 63 61 6c 6c 73 ter.** all calls
27430 20 74 6f 20 74 68 65 20 69 6e 74 65 72 66 61 63 to the interfac
27440 65 73 20 6c 69 73 74 65 64 20 68 65 72 65 20 61 es listed here a
27450 72 65 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a re completed..**
27460 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 66 .** If an interf
27470 61 63 65 20 66 61 69 6c 73 20 77 69 74 68 20 53 ace fails with S
27480 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 74 68 QLITE_MISUSE, th
27490 61 74 20 6d 65 61 6e 73 20 74 68 65 20 69 6e 74 at means the int
274a0 65 72 66 61 63 65 0a 2a 2a 20 77 61 73 20 69 6e erface.** was in
274b0 76 6f 6b 65 64 20 69 6e 63 6f 72 72 65 63 74 6c voked incorrectl
274c0 79 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 y by the applica
274d0 74 69 6f 6e 2e 20 20 49 6e 20 74 68 61 74 20 63 tion. In that c
274e0 61 73 65 2c 20 74 68 65 0a 2a 2a 20 65 72 72 6f ase, the.** erro
274f0 72 20 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 r code and messa
27500 67 65 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f ge may or may no
27510 74 20 62 65 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c t be set..*/.SQL
27520 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
27530 74 65 33 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 te3_errcode(sqli
27540 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 te3 *db);.SQLITE
27550 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
27560 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 _extended_errcod
27570 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a e(sqlite3 *db);.
27580 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
27590 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 65 char *sqlite3_e
275a0 72 72 6d 73 67 28 73 71 6c 69 74 65 33 2a 29 3b rrmsg(sqlite3*);
275b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
275c0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
275d0 65 72 72 6d 73 67 31 36 28 73 71 6c 69 74 65 33 errmsg16(sqlite3
275e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 *);.SQLITE_API c
275f0 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
27600 65 33 5f 65 72 72 73 74 72 28 69 6e 74 29 3b 0a e3_errstr(int);.
27610 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
27620 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 4f SQL Statement O
27630 62 6a 65 63 74 0a 2a 2a 20 4b 45 59 57 4f 52 44 bject.** KEYWORD
27640 53 3a 20 7b 70 72 65 70 61 72 65 64 20 73 74 61 S: {prepared sta
27650 74 65 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 tement} {prepare
27660 64 20 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a d statements}.**
27670 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
27680 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 of this object r
27690 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 epresents a sing
276a0 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 le SQL statement
276b0 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 ..** This object
276c0 20 69 73 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e is variously kn
276d0 6f 77 6e 20 61 73 20 61 20 22 70 72 65 70 61 72 own as a "prepar
276e0 65 64 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 ed statement" or
276f0 20 61 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 a.** "compiled
27700 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 22 20 6f SQL statement" o
27710 72 20 73 69 6d 70 6c 79 20 61 73 20 61 20 22 73 r simply as a "s
27720 74 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a tatement"..**.**
27730 20 54 68 65 20 6c 69 66 65 20 6f 66 20 61 20 73 The life of a s
27740 74 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 tatement object
27750 67 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c goes something l
27760 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
27770 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 <ol>.** <li> Cre
27780 61 74 65 20 74 68 65 20 6f 62 6a 65 63 74 20 75 ate the object u
27790 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 sing [sqlite3_pr
277a0 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 epare_v2()] or a
277b0 20 72 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 related.**
277c0 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c function..** <l
277d0 69 3e 20 42 69 6e 64 20 76 61 6c 75 65 73 20 74 i> Bind values t
277e0 6f 20 5b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 o [host paramete
277f0 72 73 5d 20 75 73 69 6e 67 20 74 68 65 20 73 71 rs] using the sq
27800 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a lite3_bind_*().*
27810 2a 20 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 * interface
27820 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 s..** <li> Run t
27830 68 65 20 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e he SQL by callin
27840 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 g [sqlite3_step(
27850 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 )] one or more t
27860 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 imes..** <li> Re
27870 73 65 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e set the statemen
27880 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 t using [sqlite3
27890 5f 72 65 73 65 74 28 29 5d 20 74 68 65 6e 20 67 _reset()] then g
278a0 6f 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 o back.** t
278b0 6f 20 73 74 65 70 20 32 2e 20 20 44 6f 20 74 68 o step 2. Do th
278c0 69 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 is zero or more
278d0 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 times..** <li> D
278e0 65 73 74 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 estroy the objec
278f0 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 t using [sqlite3
27900 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a _finalize()]..**
27910 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 </ol>.**.** Ref
27920 65 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 er to documentat
27930 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 ion on individua
27940 6c 20 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20 l methods above
27950 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a for additional.*
27960 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a * information..*
27970 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
27980 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 sqlite3_stmt sq
27990 6c 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a lite3_stmt;../*.
279a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e ** CAPI3REF: Run
279b0 2d 74 69 6d 65 20 4c 69 6d 69 74 73 0a 2a 2a 0a -time Limits.**.
279c0 2a 2a 20 5e 28 54 68 69 73 20 69 6e 74 65 72 66 ** ^(This interf
279d0 61 63 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 ace allows the s
279e0 69 7a 65 20 6f 66 20 76 61 72 69 6f 75 73 20 63 ize of various c
279f0 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 onstructs to be
27a00 6c 69 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 limited.** on a
27a10 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f connection by co
27a20 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 nnection basis.
27a30 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d The first param
27a40 65 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b eter is the.** [
27a50 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
27a60 69 6f 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 ion] whose limit
27a70 20 69 73 20 74 6f 20 62 65 20 73 65 74 20 6f 72 is to be set or
27a80 20 71 75 65 72 69 65 64 2e 20 20 54 68 65 0a 2a queried. The.*
27a90 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 * second paramet
27aa0 65 72 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 er is one of the
27ab0 20 5b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 [limit categori
27ac0 65 73 5d 20 74 68 61 74 20 64 65 66 69 6e 65 20 es] that define
27ad0 61 0a 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f a.** class of co
27ae0 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 73 nstructs to be s
27af0 69 7a 65 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 ize limited. Th
27b00 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 e third paramete
27b10 72 20 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 r is the.** new
27b20 6c 69 6d 69 74 20 66 6f 72 20 74 68 61 74 20 63 limit for that c
27b30 6f 6e 73 74 72 75 63 74 2e 29 5e 0a 2a 2a 0a 2a onstruct.)^.**.*
27b40 2a 20 5e 49 66 20 74 68 65 20 6e 65 77 20 6c 69 * ^If the new li
27b50 6d 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 76 mit is a negativ
27b60 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c 69 e number, the li
27b70 6d 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 mit is unchanged
27b80 2e 0a 2a 2a 20 5e 28 46 6f 72 20 65 61 63 68 20 ..** ^(For each
27b90 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 20 53 limit category S
27ba0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 3c 69 3e 4e QLITE_LIMIT_<i>N
27bb0 41 4d 45 3c 2f 69 3e 20 74 68 65 72 65 20 69 73 AME</i> there is
27bc0 20 61 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c a .** [limits |
27bd0 20 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e hard upper boun
27be0 64 5d 0a 2a 2a 20 73 65 74 20 61 74 20 63 6f 6d d].** set at com
27bf0 70 69 6c 65 2d 74 69 6d 65 20 62 79 20 61 20 43 pile-time by a C
27c00 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 preprocessor ma
27c10 63 72 6f 20 63 61 6c 6c 65 64 0a 2a 2a 20 5b 6c cro called.** [l
27c20 69 6d 69 74 73 20 7c 20 53 51 4c 49 54 45 5f 4d imits | SQLITE_M
27c30 41 58 5f 3c 69 3e 4e 41 4d 45 3c 2f 69 3e 5d 2e AX_<i>NAME</i>].
27c40 0a 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 .** (The "_LIMIT
27c50 5f 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 _" in the name i
27c60 73 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d s changed to "_M
27c70 41 58 5f 22 2e 29 29 5e 0a 2a 2a 20 5e 41 74 74 AX_".))^.** ^Att
27c80 65 6d 70 74 73 20 74 6f 20 69 6e 63 72 65 61 73 empts to increas
27c90 65 20 61 20 6c 69 6d 69 74 20 61 62 6f 76 65 20 e a limit above
27ca0 69 74 73 20 68 61 72 64 20 75 70 70 65 72 20 62 its hard upper b
27cb0 6f 75 6e 64 20 61 72 65 0a 2a 2a 20 73 69 6c 65 ound are.** sile
27cc0 6e 74 6c 79 20 74 72 75 6e 63 61 74 65 64 20 74 ntly truncated t
27cd0 6f 20 74 68 65 20 68 61 72 64 20 75 70 70 65 72 o the hard upper
27ce0 20 62 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 5e 52 bound..**.** ^R
27cf0 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 egardless of whe
27d00 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
27d10 6c 69 6d 69 74 20 77 61 73 20 63 68 61 6e 67 65 limit was change
27d20 64 2c 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69 d, the .** [sqli
27d30 74 65 33 5f 6c 69 6d 69 74 28 29 5d 20 69 6e 74 te3_limit()] int
27d40 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 erface returns t
27d50 68 65 20 70 72 69 6f 72 20 76 61 6c 75 65 20 6f he prior value o
27d60 66 20 74 68 65 20 6c 69 6d 69 74 2e 0a 2a 2a 20 f the limit..**
27d70 5e 48 65 6e 63 65 2c 20 74 6f 20 66 69 6e 64 20 ^Hence, to find
27d80 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 the current valu
27d90 65 20 6f 66 20 61 20 6c 69 6d 69 74 20 77 69 74 e of a limit wit
27da0 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 20 69 74 hout changing it
27db0 2c 0a 2a 2a 20 73 69 6d 70 6c 79 20 69 6e 76 6f ,.** simply invo
27dc0 6b 65 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 ke this interfac
27dd0 65 20 77 69 74 68 20 74 68 65 20 74 68 69 72 64 e with the third
27de0 20 70 61 72 61 6d 65 74 65 72 20 73 65 74 20 74 parameter set t
27df0 6f 20 2d 31 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 2d o -1..**.** Run-
27e00 74 69 6d 65 20 6c 69 6d 69 74 73 20 61 72 65 20 time limits are
27e10 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 intended for use
27e20 20 69 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 in applications
27e30 20 74 68 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 that manage.**
27e40 62 6f 74 68 20 74 68 65 69 72 20 6f 77 6e 20 69 both their own i
27e50 6e 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 nternal database
27e60 20 61 6e 64 20 61 6c 73 6f 20 64 61 74 61 62 61 and also databa
27e70 73 65 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e ses that are con
27e80 74 72 6f 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e trolled.** by un
27e90 74 72 75 73 74 65 64 20 65 78 74 65 72 6e 61 6c trusted external
27ea0 20 73 6f 75 72 63 65 73 2e 20 20 41 6e 20 65 78 sources. An ex
27eb0 61 6d 70 6c 65 20 61 70 70 6c 69 63 61 74 69 6f ample applicatio
27ec0 6e 20 6d 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 n might be a.**
27ed0 77 65 62 20 62 72 6f 77 73 65 72 20 74 68 61 74 web browser that
27ee0 20 68 61 73 20 69 74 73 20 6f 77 6e 20 64 61 74 has its own dat
27ef0 61 62 61 73 65 73 20 66 6f 72 20 73 74 6f 72 69 abases for stori
27f00 6e 67 20 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a ng history and.*
27f10 2a 20 73 65 70 61 72 61 74 65 20 64 61 74 61 62 * separate datab
27f20 61 73 65 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 ases controlled
27f30 62 79 20 4a 61 76 61 53 63 72 69 70 74 20 61 70 by JavaScript ap
27f40 70 6c 69 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c plications downl
27f50 6f 61 64 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65 oaded.** off the
27f60 20 49 6e 74 65 72 6e 65 74 2e 20 20 54 68 65 20 Internet. The
27f70 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 internal databas
27f80 65 73 20 63 61 6e 20 62 65 20 67 69 76 65 6e 20 es can be given
27f90 74 68 65 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 the.** large, de
27fa0 66 61 75 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 fault limits. D
27fb0 61 74 61 62 61 73 65 73 20 6d 61 6e 61 67 65 64 atabases managed
27fc0 20 62 79 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 by external sou
27fd0 72 63 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67 rces can.** be g
27fe0 69 76 65 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65 iven much smalle
27ff0 72 20 6c 69 6d 69 74 73 20 64 65 73 69 67 6e 65 r limits designe
28000 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 64 d to prevent a d
28010 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 enial of service
28020 0a 2a 2a 20 61 74 74 61 63 6b 2e 20 20 44 65 76 .** attack. Dev
28030 65 6c 6f 70 65 72 73 20 6d 69 67 68 74 20 61 6c elopers might al
28040 73 6f 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74 so want to use t
28050 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f he [sqlite3_set_
28060 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a authorizer()].**
28070 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 66 75 interface to fu
28080 72 74 68 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e rther control un
28090 74 72 75 73 74 65 64 20 53 51 4c 2e 20 20 54 68 trusted SQL. Th
280a0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 e size of the da
280b0 74 61 62 61 73 65 0a 2a 2a 20 63 72 65 61 74 65 tabase.** create
280c0 64 20 62 79 20 61 6e 20 75 6e 74 72 75 73 74 65 d by an untruste
280d0 64 20 73 63 72 69 70 74 20 63 61 6e 20 62 65 20 d script can be
280e0 63 6f 6e 74 61 69 6e 65 64 20 75 73 69 6e 67 20 contained using
280f0 74 68 65 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 the.** [max_page
28100 5f 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d _count] [PRAGMA]
28110 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d ..**.** New run-
28120 74 69 6d 65 20 6c 69 6d 69 74 20 63 61 74 65 67 time limit categ
28130 6f 72 69 65 73 20 6d 61 79 20 62 65 20 61 64 64 ories may be add
28140 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c ed in future rel
28150 65 61 73 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 eases..*/.SQLITE
28160 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
28170 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 2a 2c _limit(sqlite3*,
28180 20 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e 65 77 int id, int new
28190 56 61 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 Val);../*.** CAP
281a0 49 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 I3REF: Run-Time
281b0 4c 69 6d 69 74 20 43 61 74 65 67 6f 72 69 65 73 Limit Categories
281c0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 6c .** KEYWORDS: {l
281d0 69 6d 69 74 20 63 61 74 65 67 6f 72 79 7d 20 7b imit category} {
281e0 2a 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 *limit categorie
281f0 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 s}.**.** These c
28200 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e 65 20 onstants define
28210 76 61 72 69 6f 75 73 20 70 65 72 66 6f 72 6d 61 various performa
28220 6e 63 65 20 6c 69 6d 69 74 73 0a 2a 2a 20 74 68 nce limits.** th
28230 61 74 20 63 61 6e 20 62 65 20 6c 6f 77 65 72 65 at can be lowere
28240 64 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73 d at run-time us
28250 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d ing [sqlite3_lim
28260 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 79 it()]..** The sy
28270 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20 6d 65 nopsis of the me
28280 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 anings of the va
28290 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69 73 20 rious limits is
282a0 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a 2a 20 shown below..**
282b0 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 Additional infor
282c0 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 mation is availa
282d0 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73 20 7c ble at [limits |
282e0 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c 69 74 Limits in SQLit
282f0 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a e]..**.** <dl>.*
28300 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 * [[SQLITE_LIMIT
28310 5f 4c 45 4e 47 54 48 5d 5d 20 5e 28 3c 64 74 3e _LENGTH]] ^(<dt>
28320 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
28330 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e GTH</dt>.** <dd>
28340 54 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 The maximum size
28350 20 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f of any string o
28360 72 20 42 4c 4f 42 20 6f 72 20 74 61 62 6c 65 20 r BLOB or table
28370 72 6f 77 2c 20 69 6e 20 62 79 74 65 73 2e 3c 64 row, in bytes.<d
28380 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c d>)^.**.** [[SQL
28390 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 ITE_LIMIT_SQL_LE
283a0 4e 47 54 48 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c NGTH]] ^(<dt>SQL
283b0 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 ITE_LIMIT_SQL_LE
283c0 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 NGTH</dt>.** <dd
283d0 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e >The maximum len
283e0 67 74 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 gth of an SQL st
283f0 61 74 65 6d 65 6e 74 2c 20 69 6e 20 62 79 74 65 atement, in byte
28400 73 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 s.</dd>)^.**.**
28410 5b 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 [[SQLITE_LIMIT_C
28420 4f 4c 55 4d 4e 5d 5d 20 5e 28 3c 64 74 3e 53 51 OLUMN]] ^(<dt>SQ
28430 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d LITE_LIMIT_COLUM
28440 4e 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 N</dt>.** <dd>Th
28450 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
28460 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 of columns in a
28470 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f table definitio
28480 6e 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 72 n or in the.** r
28490 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 5b esult set of a [
284a0 53 45 4c 45 43 54 5d 20 6f 72 20 74 68 65 20 6d SELECT] or the m
284b0 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
284c0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 20 69 columns in an i
284d0 6e 64 65 78 0a 2a 2a 20 6f 72 20 69 6e 20 61 6e ndex.** or in an
284e0 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f ORDER BY or GRO
284f0 55 50 20 42 59 20 63 6c 61 75 73 65 2e 3c 2f 64 UP BY clause.</d
28500 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c d>)^.**.** [[SQL
28510 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 ITE_LIMIT_EXPR_D
28520 45 50 54 48 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c EPTH]] ^(<dt>SQL
28530 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 ITE_LIMIT_EXPR_D
28540 45 50 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 EPTH</dt>.** <dd
28550 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 >The maximum dep
28560 74 68 20 6f 66 20 74 68 65 20 70 61 72 73 65 20 th of the parse
28570 74 72 65 65 20 6f 6e 20 61 6e 79 20 65 78 70 72 tree on any expr
28580 65 73 73 69 6f 6e 2e 3c 2f 64 64 3e 29 5e 0a 2a ession.</dd>)^.*
28590 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c 49 *.** [[SQLITE_LI
285a0 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c MIT_COMPOUND_SEL
285b0 45 43 54 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 ECT]] ^(<dt>SQLI
285c0 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e TE_LIMIT_COMPOUN
285d0 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a 2a D_SELECT</dt>.**
285e0 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d <dd>The maximum
285f0 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 number of terms
28600 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 in a compound S
28610 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
28620 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b </dd>)^.**.** [[
28630 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42 SQLITE_LIMIT_VDB
28640 45 5f 4f 50 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c E_OP]] ^(<dt>SQL
28650 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f 4f ITE_LIMIT_VDBE_O
28660 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 P</dt>.** <dd>Th
28670 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
28680 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 of instructions
28690 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20 6d 61 in a virtual ma
286a0 63 68 69 6e 65 20 70 72 6f 67 72 61 6d 0a 2a 2a chine program.**
286b0 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 used to impleme
286c0 6e 74 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d nt an SQL statem
286d0 65 6e 74 2e 20 20 54 68 69 73 20 6c 69 6d 69 74 ent. This limit
286e0 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
286f0 79 0a 2a 2a 20 65 6e 66 6f 72 63 65 64 2c 20 74 y.** enforced, t
28700 68 6f 75 67 68 20 74 68 61 74 20 6d 69 67 68 74 hough that might
28710 20 62 65 20 61 64 64 65 64 20 69 6e 20 73 6f 6d be added in som
28720 65 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 e future release
28730 20 6f 66 0a 2a 2a 20 53 51 4c 69 74 65 2e 3c 2f of.** SQLite.</
28740 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 dd>)^.**.** [[SQ
28750 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 LITE_LIMIT_FUNCT
28760 49 4f 4e 5f 41 52 47 5d 5d 20 5e 28 3c 64 74 3e ION_ARG]] ^(<dt>
28770 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e SQLITE_LIMIT_FUN
28780 43 54 49 4f 4e 5f 41 52 47 3c 2f 64 74 3e 0a 2a CTION_ARG</dt>.*
28790 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
287a0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 m number of argu
287b0 6d 65 6e 74 73 20 6f 6e 20 61 20 66 75 6e 63 74 ments on a funct
287c0 69 6f 6e 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a ion.</dd>)^.**.*
287d0 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 * [[SQLITE_LIMIT
287e0 5f 41 54 54 41 43 48 45 44 5d 5d 20 5e 28 3c 64 _ATTACHED]] ^(<d
287f0 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 t>SQLITE_LIMIT_A
28800 54 54 41 43 48 45 44 3c 2f 64 74 3e 0a 2a 2a 20 TTACHED</dt>.**
28810 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 <dd>The maximum
28820 6e 75 6d 62 65 72 20 6f 66 20 5b 41 54 54 41 43 number of [ATTAC
28830 48 20 7c 20 61 74 74 61 63 68 65 64 20 64 61 74 H | attached dat
28840 61 62 61 73 65 73 5d 2e 29 5e 3c 2f 64 64 3e 0a abases].)^</dd>.
28850 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c **.** [[SQLITE_L
28860 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 IMIT_LIKE_PATTER
28870 4e 5f 4c 45 4e 47 54 48 5d 5d 0a 2a 2a 20 5e 28 N_LENGTH]].** ^(
28880 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 <dt>SQLITE_LIMIT
28890 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 _LIKE_PATTERN_LE
288a0 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 NGTH</dt>.** <dd
288b0 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e >The maximum len
288c0 67 74 68 20 6f 66 20 74 68 65 20 70 61 74 74 65 gth of the patte
288d0 72 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 rn argument to t
288e0 68 65 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20 he [LIKE] or.**
288f0 5b 47 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 73 [GLOB] operators
28900 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b .</dd>)^.**.** [
28910 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 [SQLITE_LIMIT_VA
28920 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 5d 0a RIABLE_NUMBER]].
28930 2a 2a 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f ** ^(<dt>SQLITE_
28940 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e LIMIT_VARIABLE_N
28950 55 4d 42 45 52 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 UMBER</dt>.** <d
28960 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 69 6e d>The maximum in
28970 64 65 78 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e dex number of an
28980 79 20 5b 70 61 72 61 6d 65 74 65 72 5d 20 69 6e y [parameter] in
28990 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e an SQL statemen
289a0 74 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c t.)^.**.** [[SQL
289b0 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 ITE_LIMIT_TRIGGE
289c0 52 5f 44 45 50 54 48 5d 5d 20 5e 28 3c 64 74 3e R_DEPTH]] ^(<dt>
289d0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 SQLITE_LIMIT_TRI
289e0 47 47 45 52 5f 44 45 50 54 48 3c 2f 64 74 3e 0a GGER_DEPTH</dt>.
289f0 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d ** <dd>The maxim
28a00 75 6d 20 64 65 70 74 68 20 6f 66 20 72 65 63 75 um depth of recu
28a10 72 73 69 6f 6e 20 66 6f 72 20 74 72 69 67 67 65 rsion for trigge
28a20 72 73 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 20 3c 2f rs.</dd>)^.** </
28a30 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 dl>.*/.#define S
28a40 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
28a50 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TH
28a60 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 0.#define
28a70 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c SQLITE_LIMIT_SQL
28a80 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 _LENGTH
28a90 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 1.#define
28aa0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f SQLITE_LIMIT_CO
28ab0 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 LUMN
28ac0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 2.#defin
28ad0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 e SQLITE_LIMIT_E
28ae0 58 50 52 5f 44 45 50 54 48 20 20 20 20 20 20 20 XPR_DEPTH
28af0 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 3.#defi
28b00 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ne SQLITE_LIMIT_
28b10 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 COMPOUND_SELECT
28b20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 4.#def
28b30 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 ine SQLITE_LIMIT
28b40 5f 56 44 42 45 5f 4f 50 20 20 20 20 20 20 20 20 _VDBE_OP
28b50 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 5.#de
28b60 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 fine SQLITE_LIMI
28b70 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 T_FUNCTION_ARG
28b80 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 6.#d
28b90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d efine SQLITE_LIM
28ba0 49 54 5f 41 54 54 41 43 48 45 44 20 20 20 20 20 IT_ATTACHED
28bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 7.#
28bc0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 define SQLITE_LI
28bd0 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e MIT_LIKE_PATTERN
28be0 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 38 0a _LENGTH 8.
28bf0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
28c00 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 IMIT_VARIABLE_NU
28c10 4d 42 45 52 20 20 20 20 20 20 20 20 20 20 20 39 MBER 9
28c20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
28c30 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 LIMIT_TRIGGER_DE
28c40 50 54 48 20 20 20 20 20 20 20 20 20 20 20 20 31 PTH 1
28c50 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 0../*.** CAPI3RE
28c60 46 3a 20 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 F: Compiling An
28c70 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 0a 2a 2a SQL Statement.**
28c80 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 KEYWORDS: {SQL
28c90 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c statement compil
28ca0 65 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65 er}.**.** To exe
28cb0 63 75 74 65 20 61 6e 20 53 51 4c 20 71 75 65 72 cute an SQL quer
28cc0 79 2c 20 69 74 20 6d 75 73 74 20 66 69 72 73 74 y, it must first
28cd0 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e 74 be compiled int
28ce0 6f 20 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a 2a o a byte-code.**
28cf0 20 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 6f program using o
28d00 6e 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 ne of these rout
28d10 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ines..**.** The
28d20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 first argument,
28d30 22 64 62 22 2c 20 69 73 20 61 20 5b 64 61 74 61 "db", is a [data
28d40 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
28d50 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 obtained from a
28d60 0a 2a 2a 20 70 72 69 6f 72 20 73 75 63 63 65 73 .** prior succes
28d70 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 sful call to [sq
28d80 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b lite3_open()], [
28d90 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
28da0 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 )] or.** [sqlite
28db0 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 20 20 54 68 3_open16()]. Th
28dc0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
28dd0 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 68 ction must not h
28de0 61 76 65 20 62 65 65 6e 20 63 6c 6f 73 65 64 2e ave been closed.
28df0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e .**.** The secon
28e00 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 7a 53 71 d argument, "zSq
28e10 6c 22 2c 20 69 73 20 74 68 65 20 73 74 61 74 65 l", is the state
28e20 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70 69 ment to be compi
28e30 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 led, encoded.**
28e40 61 73 20 65 69 74 68 65 72 20 55 54 46 2d 38 20 as either UTF-8
28e50 6f 72 20 55 54 46 2d 31 36 2e 20 20 54 68 65 20 or UTF-16. The
28e60 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
28e70 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 ) and sqlite3_pr
28e80 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a 20 69 6e epare_v2().** in
28e90 74 65 72 66 61 63 65 73 20 75 73 65 20 55 54 46 terfaces use UTF
28ea0 2d 38 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 5f -8, and sqlite3_
28eb0 70 72 65 70 61 72 65 31 36 28 29 20 61 6e 64 20 prepare16() and
28ec0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
28ed0 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65 20 55 54 6_v2().** use UT
28ee0 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 F-16..**.** ^If
28ef0 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65 the nByte argume
28f00 6e 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 nt is less than
28f10 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71 6c 20 zero, then zSql
28f20 69 73 20 72 65 61 64 20 75 70 20 74 6f 20 74 68 is read up to th
28f30 65 0a 2a 2a 20 66 69 72 73 74 20 7a 65 72 6f 20 e.** first zero
28f40 74 65 72 6d 69 6e 61 74 6f 72 2e 20 5e 49 66 20 terminator. ^If
28f50 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67 nByte is non-neg
28f60 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20 69 ative, then it i
28f70 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a s the maximum.**
28f80 20 6e 75 6d 62 65 72 20 6f 66 20 20 62 79 74 65 number of byte
28f90 73 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c s read from zSql
28fa0 2e 20 20 5e 57 68 65 6e 20 6e 42 79 74 65 20 69 . ^When nByte i
28fb0 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 s non-negative,
28fc0 74 68 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69 the.** zSql stri
28fd0 6e 67 20 65 6e 64 73 20 61 74 20 65 69 74 68 65 ng ends at eithe
28fe0 72 20 74 68 65 20 66 69 72 73 74 20 27 5c 30 30 r the first '\00
28ff0 30 27 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63 0' or '\u0000' c
29000 68 61 72 61 63 74 65 72 20 6f 72 0a 2a 2a 20 74 haracter or.** t
29010 68 65 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65 he nByte-th byte
29020 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 , whichever come
29030 73 20 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 s first. If the
29040 63 61 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20 caller knows.**
29050 74 68 61 74 20 74 68 65 20 73 75 70 70 6c 69 65 that the supplie
29060 64 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d d string is nul-
29070 74 65 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e terminated, then
29080 20 74 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c there is a smal
29090 6c 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 l.** performance
290a0 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65 advantage to be
290b0 20 67 61 69 6e 65 64 20 62 79 20 70 61 73 73 69 gained by passi
290c0 6e 67 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61 ng an nByte para
290d0 6d 65 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73 meter that.** is
290e0 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 equal to the nu
290f0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
29100 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e the input strin
29110 67 20 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f g <i>including</
29120 69 3e 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65 i>.** the nul-te
29130 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 73 20 61 rminator bytes a
29140 73 20 74 68 69 73 20 73 61 76 65 73 20 53 51 4c s this saves SQL
29150 69 74 65 20 66 72 6f 6d 20 68 61 76 69 6e 67 20 ite from having
29160 74 6f 0a 2a 2a 20 6d 61 6b 65 20 61 20 63 6f 70 to.** make a cop
29170 79 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 73 y of the input s
29180 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 tring..**.** ^If
29190 20 70 7a 54 61 69 6c 20 69 73 20 6e 6f 74 20 4e pzTail is not N
291a0 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 54 61 69 6c ULL then *pzTail
291b0 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e is made to poin
291c0 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 t to the first b
291d0 79 74 65 0a 2a 2a 20 70 61 73 74 20 74 68 65 20 yte.** past the
291e0 65 6e 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 end of the first
291f0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 SQL statement i
29200 6e 20 7a 53 71 6c 2e 20 20 54 68 65 73 65 20 72 n zSql. These r
29210 6f 75 74 69 6e 65 73 20 6f 6e 6c 79 0a 2a 2a 20 outines only.**
29220 63 6f 6d 70 69 6c 65 20 74 68 65 20 66 69 72 73 compile the firs
29230 74 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a t statement in z
29240 53 71 6c 2c 20 73 6f 20 2a 70 7a 54 61 69 6c 20 Sql, so *pzTail
29250 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 is left pointing
29260 20 74 6f 0a 2a 2a 20 77 68 61 74 20 72 65 6d 61 to.** what rema
29270 69 6e 73 20 75 6e 63 6f 6d 70 69 6c 65 64 2e 0a ins uncompiled..
29280 2a 2a 0a 2a 2a 20 5e 2a 70 70 53 74 6d 74 20 69 **.** ^*ppStmt i
29290 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
292a0 74 6f 20 61 20 63 6f 6d 70 69 6c 65 64 20 5b 70 to a compiled [p
292b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
292c0 74 5d 20 74 68 61 74 20 63 61 6e 20 62 65 0a 2a t] that can be.*
292d0 2a 20 65 78 65 63 75 74 65 64 20 75 73 69 6e 67 * executed using
292e0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
292f0 5d 2e 20 20 5e 49 66 20 74 68 65 72 65 20 69 73 ]. ^If there is
29300 20 61 6e 20 65 72 72 6f 72 2c 20 2a 70 70 53 74 an error, *ppSt
29310 6d 74 20 69 73 20 73 65 74 0a 2a 2a 20 74 6f 20 mt is set.** to
29320 4e 55 4c 4c 2e 20 20 5e 49 66 20 74 68 65 20 69 NULL. ^If the i
29330 6e 70 75 74 20 74 65 78 74 20 63 6f 6e 74 61 69 nput text contai
29340 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66 20 74 68 ns no SQL (if th
29350 65 20 69 6e 70 75 74 20 69 73 20 61 6e 20 65 6d e input is an em
29360 70 74 79 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 72 pty.** string or
29370 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74 68 65 6e a comment) then
29380 20 2a 70 70 53 74 6d 74 20 69 73 20 73 65 74 20 *ppStmt is set
29390 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 54 68 65 20 to NULL..** The
293a0 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 calling procedur
293b0 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 e is responsible
293c0 20 66 6f 72 20 64 65 6c 65 74 69 6e 67 20 74 68 for deleting th
293d0 65 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 53 51 e compiled.** SQ
293e0 4c 20 73 74 61 74 65 6d 65 6e 74 20 75 73 69 6e L statement usin
293f0 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c g [sqlite3_final
29400 69 7a 65 28 29 5d 20 61 66 74 65 72 20 69 74 20 ize()] after it
29410 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 has finished wit
29420 68 20 69 74 2e 0a 2a 2a 20 70 70 53 74 6d 74 20 h it..** ppStmt
29430 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e may not be NULL.
29440 0a 2a 2a 0a 2a 2a 20 5e 4f 6e 20 73 75 63 63 65 .**.** ^On succe
29450 73 73 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f ss, the sqlite3_
29460 70 72 65 70 61 72 65 28 29 20 66 61 6d 69 6c 79 prepare() family
29470 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 72 65 74 of routines ret
29480 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 3b urn [SQLITE_OK];
29490 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 61 6e .** otherwise an
294a0 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 [error code] is
294b0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
294c0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 The sqlite3_pre
294d0 70 61 72 65 5f 76 32 28 29 20 61 6e 64 20 73 71 pare_v2() and sq
294e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f lite3_prepare16_
294f0 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 73 20 v2() interfaces
29500 61 72 65 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 are.** recommend
29510 65 64 20 66 6f 72 20 61 6c 6c 20 6e 65 77 20 70 ed for all new p
29520 72 6f 67 72 61 6d 73 2e 20 54 68 65 20 74 77 6f rograms. The two
29530 20 6f 6c 64 65 72 20 69 6e 74 65 72 66 61 63 65 older interface
29540 73 20 61 72 65 20 72 65 74 61 69 6e 65 64 0a 2a s are retained.*
29550 2a 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 20 * for backwards
29560 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c 20 62 compatibility, b
29570 75 74 20 74 68 65 69 72 20 75 73 65 20 69 73 20 ut their use is
29580 64 69 73 63 6f 75 72 61 67 65 64 2e 0a 2a 2a 20 discouraged..**
29590 5e 49 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 ^In the "v2" int
295a0 65 72 66 61 63 65 73 2c 20 74 68 65 20 70 72 65 erfaces, the pre
295b0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a pared statement.
295c0 2a 2a 20 74 68 61 74 20 69 73 20 72 65 74 75 72 ** that is retur
295d0 6e 65 64 20 28 74 68 65 20 5b 73 71 6c 69 74 65 ned (the [sqlite
295e0 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 29 20 3_stmt] object)
295f0 63 6f 6e 74 61 69 6e 73 20 61 20 63 6f 70 79 20 contains a copy
29600 6f 66 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e of the.** origin
29610 61 6c 20 53 51 4c 20 74 65 78 74 2e 20 54 68 69 al SQL text. Thi
29620 73 20 63 61 75 73 65 73 20 74 68 65 20 5b 73 71 s causes the [sq
29630 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 69 6e lite3_step()] in
29640 74 65 72 66 61 63 65 20 74 6f 0a 2a 2a 20 62 65 terface to.** be
29650 68 61 76 65 20 64 69 66 66 65 72 65 6e 74 6c 79 have differently
29660 20 69 6e 20 74 68 72 65 65 20 77 61 79 73 3a 0a in three ways:.
29670 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c **.** <ol>.** <l
29680 69 3e 0a 2a 2a 20 5e 49 66 20 74 68 65 20 64 61 i>.** ^If the da
29690 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63 68 tabase schema ch
296a0 61 6e 67 65 73 2c 20 69 6e 73 74 65 61 64 20 6f anges, instead o
296b0 66 20 72 65 74 75 72 6e 69 6e 67 20 5b 53 51 4c f returning [SQL
296c0 49 54 45 5f 53 43 48 45 4d 41 5d 20 61 73 20 69 ITE_SCHEMA] as i
296d0 74 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 64 t.** always used
296e0 20 74 6f 20 64 6f 2c 20 5b 73 71 6c 69 74 65 33 to do, [sqlite3
296f0 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 61 75 _step()] will au
29700 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f tomatically reco
29710 6d 70 69 6c 65 20 74 68 65 20 53 51 4c 0a 2a 2a mpile the SQL.**
29720 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 statement and t
29730 72 79 20 74 6f 20 72 75 6e 20 69 74 20 61 67 61 ry to run it aga
29740 69 6e 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 0a in..** </li>.**.
29750 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 5e 57 68 65 6e ** <li>.** ^When
29760 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
29770 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 , [sqlite3_step(
29780 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 6f )] will return o
29790 6e 65 20 6f 66 20 74 68 65 20 64 65 74 61 69 6c ne of the detail
297a0 65 64 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 ed.** [error cod
297b0 65 73 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 es] or [extended
297c0 20 65 72 72 6f 72 20 63 6f 64 65 73 5d 2e 20 20 error codes].
297d0 5e 54 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 ^The legacy beha
297e0 76 69 6f 72 20 77 61 73 20 74 68 61 74 0a 2a 2a vior was that.**
297f0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
29800 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 72 65 74 ] would only ret
29810 75 72 6e 20 61 20 67 65 6e 65 72 69 63 20 5b 53 urn a generic [S
29820 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 72 65 73 QLITE_ERROR] res
29830 75 6c 74 20 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 ult code.** and
29840 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
29850 77 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 6d 61 would have to ma
29860 6b 65 20 61 20 73 65 63 6f 6e 64 20 63 61 6c 6c ke a second call
29870 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 to [sqlite3_res
29880 65 74 28 29 5d 0a 2a 2a 20 69 6e 20 6f 72 64 65 et()].** in orde
29890 72 20 74 6f 20 66 69 6e 64 20 74 68 65 20 75 6e r to find the un
298a0 64 65 72 6c 79 69 6e 67 20 63 61 75 73 65 20 6f derlying cause o
298b0 66 20 74 68 65 20 70 72 6f 62 6c 65 6d 2e 20 57 f the problem. W
298c0 69 74 68 20 74 68 65 20 22 76 32 22 20 70 72 65 ith the "v2" pre
298d0 70 61 72 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 pare.** interfac
298e0 65 73 2c 20 74 68 65 20 75 6e 64 65 72 6c 79 69 es, the underlyi
298f0 6e 67 20 72 65 61 73 6f 6e 20 66 6f 72 20 74 68 ng reason for th
29900 65 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 e error is retur
29910 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e ned immediately.
29920 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 .** </li>.**.**
29930 3c 6c 69 3e 0a 2a 2a 20 5e 49 66 20 74 68 65 20 <li>.** ^If the
29940 73 70 65 63 69 66 69 63 20 76 61 6c 75 65 20 62 specific value b
29950 6f 75 6e 64 20 74 6f 20 5b 70 61 72 61 6d 65 74 ound to [paramet
29960 65 72 20 7c 20 68 6f 73 74 20 70 61 72 61 6d 65 er | host parame
29970 74 65 72 5d 20 69 6e 20 74 68 65 20 0a 2a 2a 20 ter] in the .**
29980 57 48 45 52 45 20 63 6c 61 75 73 65 20 6d 69 67 WHERE clause mig
29990 68 74 20 69 6e 66 6c 75 65 6e 63 65 20 74 68 65 ht influence the
299a0 20 63 68 6f 69 63 65 20 6f 66 20 71 75 65 72 79 choice of query
299b0 20 70 6c 61 6e 20 66 6f 72 20 61 20 73 74 61 74 plan for a stat
299c0 65 6d 65 6e 74 2c 0a 2a 2a 20 74 68 65 6e 20 74 ement,.** then t
299d0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c he statement wil
299e0 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c l be automatical
299f0 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 2c 20 61 ly recompiled, a
29a00 73 20 69 66 20 74 68 65 72 65 20 68 61 64 20 62 s if there had b
29a10 65 65 6e 20 0a 2a 2a 20 61 20 73 63 68 65 6d 61 een .** a schema
29a20 20 63 68 61 6e 67 65 2c 20 6f 6e 20 74 68 65 20 change, on the
29a30 66 69 72 73 74 20 20 5b 73 71 6c 69 74 65 33 5f first [sqlite3_
29a40 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 66 6f 6c step()] call fol
29a50 6c 6f 77 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 lowing any chang
29a60 65 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73 71 6c e.** to the [sql
29a70 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20 7c ite3_bind_text |
29a80 20 62 69 6e 64 69 6e 67 73 5d 20 6f 66 20 74 68 bindings] of th
29a90 61 74 20 5b 70 61 72 61 6d 65 74 65 72 5d 2e 20 at [parameter].
29aa0 0a 2a 2a 20 5e 54 68 65 20 73 70 65 63 69 66 69 .** ^The specifi
29ab0 63 20 76 61 6c 75 65 20 6f 66 20 57 48 45 52 45 c value of WHERE
29ac0 2d 63 6c 61 75 73 65 20 5b 70 61 72 61 6d 65 74 -clause [paramet
29ad0 65 72 5d 20 6d 69 67 68 74 20 69 6e 66 6c 75 65 er] might influe
29ae0 6e 63 65 20 74 68 65 20 0a 2a 2a 20 63 68 6f 69 nce the .** choi
29af0 63 65 20 6f 66 20 71 75 65 72 79 20 70 6c 61 6e ce of query plan
29b00 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 if the paramete
29b10 72 20 69 73 20 74 68 65 20 6c 65 66 74 2d 68 61 r is the left-ha
29b20 6e 64 20 73 69 64 65 20 6f 66 20 61 20 5b 4c 49 nd side of a [LI
29b30 4b 45 5d 0a 2a 2a 20 6f 72 20 5b 47 4c 4f 42 5d KE].** or [GLOB]
29b40 20 6f 70 65 72 61 74 6f 72 20 6f 72 20 69 66 20 operator or if
29b50 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 the parameter is
29b60 20 63 6f 6d 70 61 72 65 64 20 74 6f 20 61 6e 20 compared to an
29b70 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 0a 2a indexed column.*
29b80 2a 20 61 6e 64 20 74 68 65 20 5b 53 51 4c 49 54 * and the [SQLIT
29b90 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 5d 20 E_ENABLE_STAT3]
29ba0 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 compile-time opt
29bb0 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a ion is enabled..
29bc0 2a 2a 20 74 68 65 20 0a 2a 2a 20 3c 2f 6c 69 3e ** the .** </li>
29bd0 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2f 0a 53 51 4c .** </ol>.*/.SQL
29be0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
29bf0 74 65 33 5f 70 72 65 70 61 72 65 28 0a 20 20 73 te3_prepare(. s
29c00 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 qlite3 *db,
29c10 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
29c20 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 se handle */. c
29c30 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c onst char *zSql,
29c40 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 /* SQL st
29c50 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 38 20 65 atement, UTF-8 e
29c60 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 ncoded */. int
29c70 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 nByte,
29c80 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c /* Maximum l
29c90 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e ength of zSql in
29ca0 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c bytes. */. sql
29cb0 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 ite3_stmt **ppSt
29cc0 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 mt, /* OUT: Sta
29cd0 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f tement handle */
29ce0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a . const char **
29cf0 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 pzTail /* OU
29d00 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e T: Pointer to un
29d10 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 used portion of
29d20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 zSql */.);.SQLIT
29d30 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
29d40 33 5f 70 72 65 70 61 72 65 5f 76 32 28 0a 20 20 3_prepare_v2(.
29d50 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
29d60 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
29d70 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ase handle */.
29d80 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c const char *zSql
29d90 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 , /* SQL s
29da0 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 38 20 tatement, UTF-8
29db0 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 encoded */. int
29dc0 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 nByte,
29dd0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
29de0 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 length of zSql i
29df0 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 n bytes. */. sq
29e00 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 lite3_stmt **ppS
29e10 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 tmt, /* OUT: St
29e20 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a atement handle *
29e30 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
29e40 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f *pzTail /* O
29e50 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 UT: Pointer to u
29e60 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 nused portion of
29e70 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 zSql */.);.SQLI
29e80 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
29e90 65 33 5f 70 72 65 70 61 72 65 31 36 28 0a 20 20 e3_prepare16(.
29ea0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
29eb0 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
29ec0 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ase handle */.
29ed0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c const void *zSql
29ee0 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 , /* SQL s
29ef0 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 tatement, UTF-16
29f00 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e encoded */. in
29f10 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 t nByte,
29f20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
29f30 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 length of zSql
29f40 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 in bytes. */. s
29f50 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 qlite3_stmt **pp
29f60 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 Stmt, /* OUT: S
29f70 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 tatement handle
29f80 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
29f90 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 **pzTail /*
29fa0 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 OUT: Pointer to
29fb0 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f unused portion o
29fc0 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c f zSql */.);.SQL
29fd0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
29fe0 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 te3_prepare16_v2
29ff0 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
2a000 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
2a010 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a atabase handle *
2a020 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
2a030 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 zSql, /* S
2a040 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 QL statement, UT
2a050 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a F-16 encoded */.
2a060 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 int nByte,
2a070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
2a080 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a imum length of z
2a090 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f Sql in bytes. */
2a0a0 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
2a0b0 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 **ppStmt, /* OU
2a0c0 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e T: Statement han
2a0d0 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 dle */. const v
2a0e0 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 oid **pzTail
2a0f0 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 /* OUT: Pointer
2a100 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 to unused porti
2a110 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b on of zSql */.);
2a120 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
2a130 3a 20 52 65 74 72 69 65 76 69 6e 67 20 53 74 61 : Retrieving Sta
2a140 74 65 6d 65 6e 74 20 53 51 4c 0a 2a 2a 0a 2a 2a tement SQL.**.**
2a150 20 5e 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 ^This interface
2a160 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
2a170 72 65 74 72 69 65 76 65 20 61 20 73 61 76 65 64 retrieve a saved
2a180 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6f 72 69 copy of the ori
2a190 67 69 6e 61 6c 0a 2a 2a 20 53 51 4c 20 74 65 78 ginal.** SQL tex
2a1a0 74 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 t used to create
2a1b0 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 a [prepared sta
2a1c0 74 65 6d 65 6e 74 5d 20 69 66 20 74 68 61 74 20 tement] if that
2a1d0 73 74 61 74 65 6d 65 6e 74 20 77 61 73 0a 2a 2a statement was.**
2a1e0 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 compiled using
2a1f0 65 69 74 68 65 72 20 5b 73 71 6c 69 74 65 33 5f either [sqlite3_
2a200 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 prepare_v2()] or
2a210 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
2a220 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2f 0a 53 51 e16_v2()]..*/.SQ
2a230 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 LITE_API const c
2a240 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c har *sqlite3_sql
2a250 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
2a260 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 Stmt);../*.** CA
2a270 50 49 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e PI3REF: Determin
2a280 65 20 49 66 20 41 6e 20 53 51 4c 20 53 74 61 74 e If An SQL Stat
2a290 65 6d 65 6e 74 20 57 72 69 74 65 73 20 54 68 65 ement Writes The
2a2a0 20 44 61 74 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20 Database.**.**
2a2b0 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d ^The sqlite3_stm
2a2c0 74 5f 72 65 61 64 6f 6e 6c 79 28 58 29 20 69 6e t_readonly(X) in
2a2d0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
2a2e0 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 true (non-zero)
2a2f0 69 66 0a 2a 2a 20 61 6e 64 20 6f 6e 6c 79 20 69 if.** and only i
2a300 66 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 f the [prepared
2a310 73 74 61 74 65 6d 65 6e 74 5d 20 58 20 6d 61 6b statement] X mak
2a320 65 73 20 6e 6f 20 64 69 72 65 63 74 20 63 68 61 es no direct cha
2a330 6e 67 65 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63 nges to.** the c
2a340 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 64 61 ontent of the da
2a350 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
2a360 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 61 70 ** Note that [ap
2a370 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
2a380 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d d SQL functions]
2a390 20 6f 72 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 or.** [virtual
2a3a0 74 61 62 6c 65 73 5d 20 6d 69 67 68 74 20 63 68 tables] might ch
2a3b0 61 6e 67 65 20 74 68 65 20 64 61 74 61 62 61 73 ange the databas
2a3c0 65 20 69 6e 64 69 72 65 63 74 6c 79 20 61 73 20 e indirectly as
2a3d0 61 20 73 69 64 65 20 65 66 66 65 63 74 2e 20 20 a side effect.
2a3e0 0a 2a 2a 20 5e 28 46 6f 72 20 65 78 61 6d 70 6c .** ^(For exampl
2a3f0 65 2c 20 69 66 20 61 6e 20 61 70 70 6c 69 63 61 e, if an applica
2a400 74 69 6f 6e 20 64 65 66 69 6e 65 73 20 61 20 66 tion defines a f
2a410 75 6e 63 74 69 6f 6e 20 22 65 76 61 6c 28 29 22 unction "eval()"
2a420 20 74 68 61 74 20 0a 2a 2a 20 63 61 6c 6c 73 20 that .** calls
2a430 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d [sqlite3_exec()]
2a440 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f , then the follo
2a450 77 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65 wing SQL stateme
2a460 6e 74 20 77 6f 75 6c 64 0a 2a 2a 20 63 68 61 6e nt would.** chan
2a470 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ge the database
2a480 66 69 6c 65 20 74 68 72 6f 75 67 68 20 73 69 64 file through sid
2a490 65 2d 65 66 66 65 63 74 73 3a 0a 2a 2a 0a 2a 2a e-effects:.**.**
2a4a0 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 <blockquote><pr
2a4b0 65 3e 0a 2a 2a 20 20 20 20 53 45 4c 45 43 54 20 e>.** SELECT
2a4c0 65 76 61 6c 28 27 44 45 4c 45 54 45 20 46 52 4f eval('DELETE FRO
2a4d0 4d 20 74 31 27 29 20 46 52 4f 4d 20 74 32 3b 0a M t1') FROM t2;.
2a4e0 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b ** </pre></block
2a4f0 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 42 75 74 quote>.**.** But
2a500 20 62 65 63 61 75 73 65 20 74 68 65 20 5b 53 45 because the [SE
2a510 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 20 LECT] statement
2a520 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 does not change
2a530 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
2a540 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20 73 e.** directly, s
2a550 71 6c 69 74 65 33 5f 73 74 6d 74 5f 72 65 61 64 qlite3_stmt_read
2a560 6f 6e 6c 79 28 29 20 77 6f 75 6c 64 20 73 74 69 only() would sti
2a570 6c 6c 20 72 65 74 75 72 6e 20 74 72 75 65 2e 29 ll return true.)
2a580 5e 0a 2a 2a 0a 2a 2a 20 5e 54 72 61 6e 73 61 63 ^.**.** ^Transac
2a590 74 69 6f 6e 20 63 6f 6e 74 72 6f 6c 20 73 74 61 tion control sta
2a5a0 74 65 6d 65 6e 74 73 20 73 75 63 68 20 61 73 20 tements such as
2a5b0 5b 42 45 47 49 4e 5d 2c 20 5b 43 4f 4d 4d 49 54 [BEGIN], [COMMIT
2a5c0 5d 2c 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2c 0a 2a ], [ROLLBACK],.*
2a5d0 2a 20 5b 53 41 56 45 50 4f 49 4e 54 5d 2c 20 61 * [SAVEPOINT], a
2a5e0 6e 64 20 5b 52 45 4c 45 41 53 45 5d 20 63 61 75 nd [RELEASE] cau
2a5f0 73 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f se sqlite3_stmt_
2a600 72 65 61 64 6f 6e 6c 79 28 29 20 74 6f 20 72 65 readonly() to re
2a610 74 75 72 6e 20 74 72 75 65 2c 0a 2a 2a 20 73 69 turn true,.** si
2a620 6e 63 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e nce the statemen
2a630 74 73 20 74 68 65 6d 73 65 6c 76 65 73 20 64 6f ts themselves do
2a640 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f not actually mo
2a650 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73 dify the databas
2a660 65 20 62 75 74 0a 2a 2a 20 72 61 74 68 65 72 20 e but.** rather
2a670 74 68 65 79 20 63 6f 6e 74 72 6f 6c 20 74 68 65 they control the
2a680 20 74 69 6d 69 6e 67 20 6f 66 20 77 68 65 6e 20 timing of when
2a690 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73 other statements
2a6a0 20 6d 6f 64 69 66 79 20 74 68 65 20 0a 2a 2a 20 modify the .**
2a6b0 64 61 74 61 62 61 73 65 2e 20 20 5e 54 68 65 20 database. ^The
2a6c0 5b 41 54 54 41 43 48 5d 20 61 6e 64 20 5b 44 45 [ATTACH] and [DE
2a6d0 54 41 43 48 5d 20 73 74 61 74 65 6d 65 6e 74 73 TACH] statements
2a6e0 20 61 6c 73 6f 20 63 61 75 73 65 0a 2a 2a 20 73 also cause.** s
2a6f0 71 6c 69 74 65 33 5f 73 74 6d 74 5f 72 65 61 64 qlite3_stmt_read
2a700 6f 6e 6c 79 28 29 20 74 6f 20 72 65 74 75 72 6e only() to return
2a710 20 74 72 75 65 20 73 69 6e 63 65 2c 20 77 68 69 true since, whi
2a720 6c 65 20 74 68 6f 73 65 20 73 74 61 74 65 6d 65 le those stateme
2a730 6e 74 73 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 nts.** change th
2a740 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 e configuration
2a750 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 63 6f of a database co
2a760 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 79 20 64 nnection, they d
2a770 6f 20 6e 6f 74 20 6d 61 6b 65 20 0a 2a 2a 20 63 o not make .** c
2a780 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 63 6f hanges to the co
2a790 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 64 61 74 ntent of the dat
2a7a0 61 62 61 73 65 20 66 69 6c 65 73 20 6f 6e 20 64 abase files on d
2a7b0 69 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 isk..*/.SQLITE_A
2a7c0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 PI int sqlite3_s
2a7d0 74 6d 74 5f 72 65 61 64 6f 6e 6c 79 28 73 71 6c tmt_readonly(sql
2a7e0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
2a7f0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
2a800 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 20 49 66 EF: Determine If
2a810 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74 A Prepared Stat
2a820 65 6d 65 6e 74 20 48 61 73 20 42 65 65 6e 20 52 ement Has Been R
2a830 65 73 65 74 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 eset.**.** ^The
2a840 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 62 75 73 sqlite3_stmt_bus
2a850 79 28 53 29 20 69 6e 74 65 72 66 61 63 65 20 72 y(S) interface r
2a860 65 74 75 72 6e 73 20 74 72 75 65 20 28 6e 6f 6e eturns true (non
2a870 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 0a 2a 2a -zero) if the.**
2a880 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
2a890 6d 65 6e 74 5d 20 53 20 68 61 73 20 62 65 65 6e ment] S has been
2a8a0 20 73 74 65 70 70 65 64 20 61 74 20 6c 65 61 73 stepped at leas
2a8b0 74 20 6f 6e 63 65 20 75 73 69 6e 67 20 0a 2a 2a t once using .**
2a8c0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 [sqlite3_step(S
2a8d0 29 5d 20 62 75 74 20 68 61 73 20 6e 6f 74 20 72 )] but has not r
2a8e0 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e un to completion
2a8f0 20 61 6e 64 2f 6f 72 20 68 61 73 20 6e 6f 74 20 and/or has not
2a900 0a 2a 2a 20 62 65 65 6e 20 72 65 73 65 74 20 75 .** been reset u
2a910 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 sing [sqlite3_re
2a920 73 65 74 28 53 29 5d 2e 20 20 5e 54 68 65 20 73 set(S)]. ^The s
2a930 71 6c 69 74 65 33 5f 73 74 6d 74 5f 62 75 73 79 qlite3_stmt_busy
2a940 28 53 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 (S).** interface
2a950 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 20 69 returns false i
2a960 66 20 53 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f f S is a NULL po
2a970 69 6e 74 65 72 2e 20 20 49 66 20 53 20 69 73 20 inter. If S is
2a980 6e 6f 74 20 61 20 0a 2a 2a 20 4e 55 4c 4c 20 70 not a .** NULL p
2a990 6f 69 6e 74 65 72 20 61 6e 64 20 69 73 20 6e 6f ointer and is no
2a9a0 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 t a pointer to a
2a9b0 20 76 61 6c 69 64 20 5b 70 72 65 70 61 72 65 64 valid [prepared
2a9c0 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 6f statement].** o
2a9d0 62 6a 65 63 74 2c 20 74 68 65 6e 20 74 68 65 20 bject, then the
2a9e0 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 behavior is unde
2a9f0 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 62 fined and probab
2aa00 6c 79 20 75 6e 64 65 73 69 72 61 62 6c 65 2e 0a ly undesirable..
2aa10 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
2aa20 66 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 face can be used
2aa30 20 69 6e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 in combination
2aa40 5b 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 [sqlite3_next_st
2aa50 6d 74 28 29 5d 0a 2a 2a 20 74 6f 20 6c 6f 63 61 mt()].** to loca
2aa60 74 65 20 61 6c 6c 20 70 72 65 70 61 72 65 64 20 te all prepared
2aa70 73 74 61 74 65 6d 65 6e 74 73 20 61 73 73 6f 63 statements assoc
2aa80 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74 iated with a dat
2aa90 61 62 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63 abase .** connec
2aaa0 74 69 6f 6e 20 74 68 61 74 20 61 72 65 20 69 6e tion that are in
2aab0 20 6e 65 65 64 20 6f 66 20 62 65 69 6e 67 20 72 need of being r
2aac0 65 73 65 74 2e 20 20 54 68 69 73 20 63 61 6e 20 eset. This can
2aad0 62 65 20 75 73 65 64 2c 0a 2a 2a 20 66 6f 72 20 be used,.** for
2aae0 65 78 61 6d 70 6c 65 2c 20 69 6e 20 64 69 61 67 example, in diag
2aaf0 6e 6f 73 74 69 63 20 72 6f 75 74 69 6e 65 73 20 nostic routines
2ab00 74 6f 20 73 65 61 72 63 68 20 66 6f 72 20 70 72 to search for pr
2ab10 65 70 61 72 65 64 20 0a 2a 2a 20 73 74 61 74 65 epared .** state
2ab20 6d 65 6e 74 73 20 74 68 61 74 20 61 72 65 20 68 ments that are h
2ab30 6f 6c 64 69 6e 67 20 61 20 74 72 61 6e 73 61 63 olding a transac
2ab40 74 69 6f 6e 20 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 tion open..*/.SQ
2ab50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
2ab60 69 74 65 33 5f 73 74 6d 74 5f 62 75 73 79 28 73 ite3_stmt_busy(s
2ab70 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a qlite3_stmt*);..
2ab80 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2ab90 44 79 6e 61 6d 69 63 61 6c 6c 79 20 54 79 70 65 Dynamically Type
2aba0 64 20 56 61 6c 75 65 20 4f 62 6a 65 63 74 0a 2a d Value Object.*
2abb0 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 6f * KEYWORDS: {pro
2abc0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
2abd0 61 6c 75 65 7d 20 7b 75 6e 70 72 6f 74 65 63 74 alue} {unprotect
2abe0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
2abf0 7d 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 }.**.** SQLite u
2ac00 73 65 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f ses the sqlite3_
2ac10 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 74 6f 20 value object to
2ac20 72 65 70 72 65 73 65 6e 74 20 61 6c 6c 20 76 61 represent all va
2ac30 6c 75 65 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e lues.** that can
2ac40 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 be stored in a
2ac50 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 20 database table.
2ac60 53 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e 61 SQLite uses dyna
2ac70 6d 69 63 20 74 79 70 69 6e 67 0a 2a 2a 20 66 6f mic typing.** fo
2ac80 72 20 74 68 65 20 76 61 6c 75 65 73 20 69 74 20 r the values it
2ac90 73 74 6f 72 65 73 2e 20 20 5e 56 61 6c 75 65 73 stores. ^Values
2aca0 20 73 74 6f 72 65 64 20 69 6e 20 73 71 6c 69 74 stored in sqlit
2acb0 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 e3_value objects
2acc0 0a 2a 2a 20 63 61 6e 20 62 65 20 69 6e 74 65 67 .** can be integ
2acd0 65 72 73 2c 20 66 6c 6f 61 74 69 6e 67 20 70 6f ers, floating po
2ace0 69 6e 74 20 76 61 6c 75 65 73 2c 20 73 74 72 69 int values, stri
2acf0 6e 67 73 2c 20 42 4c 4f 42 73 2c 20 6f 72 20 4e ngs, BLOBs, or N
2ad00 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 73 71 ULL..**.** An sq
2ad10 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
2ad20 63 74 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 ct may be either
2ad30 20 22 70 72 6f 74 65 63 74 65 64 22 20 6f 72 20 "protected" or
2ad40 22 75 6e 70 72 6f 74 65 63 74 65 64 22 2e 0a 2a "unprotected"..*
2ad50 2a 20 53 6f 6d 65 20 69 6e 74 65 72 66 61 63 65 * Some interface
2ad60 73 20 72 65 71 75 69 72 65 20 61 20 70 72 6f 74 s require a prot
2ad70 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
2ad80 6c 75 65 2e 20 20 4f 74 68 65 72 20 69 6e 74 65 lue. Other inte
2ad90 72 66 61 63 65 73 0a 2a 2a 20 77 69 6c 6c 20 61 rfaces.** will a
2ada0 63 63 65 70 74 20 65 69 74 68 65 72 20 61 20 70 ccept either a p
2adb0 72 6f 74 65 63 74 65 64 20 6f 72 20 61 6e 20 75 rotected or an u
2adc0 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 nprotected sqlit
2add0 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a 20 45 76 65 e3_value..** Eve
2ade0 72 79 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 ry interface tha
2adf0 74 20 61 63 63 65 70 74 73 20 73 71 6c 69 74 65 t accepts sqlite
2ae00 33 5f 76 61 6c 75 65 20 61 72 67 75 6d 65 6e 74 3_value argument
2ae10 73 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 s specifies.** w
2ae20 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 69 74 hether or not it
2ae30 20 72 65 71 75 69 72 65 73 20 61 20 70 72 6f 74 requires a prot
2ae40 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
2ae50 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 lue..**.** The t
2ae60 65 72 6d 73 20 22 70 72 6f 74 65 63 74 65 64 22 erms "protected"
2ae70 20 61 6e 64 20 22 75 6e 70 72 6f 74 65 63 74 65 and "unprotecte
2ae80 64 22 20 72 65 66 65 72 20 74 6f 20 77 68 65 74 d" refer to whet
2ae90 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 61 20 her or not.** a
2aea0 6d 75 74 65 78 20 69 73 20 68 65 6c 64 2e 20 20 mutex is held.
2aeb0 41 6e 20 69 6e 74 65 72 6e 61 6c 20 6d 75 74 65 An internal mute
2aec0 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20 61 20 x is held for a
2aed0 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c protected.** sql
2aee0 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
2aef0 74 20 62 75 74 20 6e 6f 20 6d 75 74 65 78 20 69 t but no mutex i
2af00 73 20 68 65 6c 64 20 66 6f 72 20 61 6e 20 75 6e s held for an un
2af10 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c protected.** sql
2af20 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
2af30 74 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 t. If SQLite is
2af40 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 62 65 20 compiled to be
2af50 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 0a single-threaded.
2af60 2a 2a 20 28 77 69 74 68 20 5b 53 51 4c 49 54 45 ** (with [SQLITE
2af70 5f 54 48 52 45 41 44 53 41 46 45 3d 30 5d 20 61 _THREADSAFE=0] a
2af80 6e 64 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33 nd with [sqlite3
2af90 5f 74 68 72 65 61 64 73 61 66 65 28 29 5d 20 72 _threadsafe()] r
2afa0 65 74 75 72 6e 69 6e 67 20 30 29 0a 2a 2a 20 6f eturning 0).** o
2afb0 72 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 72 r if SQLite is r
2afc0 75 6e 20 69 6e 20 6f 6e 65 20 6f 66 20 72 65 64 un in one of red
2afd0 75 63 65 64 20 6d 75 74 65 78 20 6d 6f 64 65 73 uced mutex modes
2afe0 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e .** [SQLITE_CON
2aff0 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 FIG_SINGLETHREAD
2b000 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e ] or [SQLITE_CON
2b010 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d FIG_MULTITHREAD]
2b020 0a 2a 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69 .** then there i
2b030 73 20 6e 6f 20 64 69 73 74 69 6e 63 74 69 6f 6e s no distinction
2b040 20 62 65 74 77 65 65 6e 20 70 72 6f 74 65 63 74 between protect
2b050 65 64 20 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 ed and unprotect
2b060 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 ed.** sqlite3_va
2b070 6c 75 65 20 6f 62 6a 65 63 74 73 20 61 6e 64 20 lue objects and
2b080 74 68 65 79 20 63 61 6e 20 62 65 20 75 73 65 64 they can be used
2b090 20 69 6e 74 65 72 63 68 61 6e 67 65 61 62 6c 79 interchangeably
2b0a0 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 . However,.** f
2b0b0 6f 72 20 6d 61 78 69 6d 75 6d 20 63 6f 64 65 20 or maximum code
2b0c0 70 6f 72 74 61 62 69 6c 69 74 79 20 69 74 20 69 portability it i
2b0d0 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 s recommended th
2b0e0 61 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a at applications.
2b0f0 2a 2a 20 73 74 69 6c 6c 20 6d 61 6b 65 20 74 68 ** still make th
2b100 65 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 e distinction be
2b110 74 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20 tween protected
2b120 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a and unprotected.
2b130 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ** sqlite3_value
2b140 20 6f 62 6a 65 63 74 73 20 65 76 65 6e 20 77 68 objects even wh
2b150 65 6e 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 en not strictly
2b160 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 required..**.**
2b170 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c ^The sqlite3_val
2b180 75 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 ue objects that
2b190 61 72 65 20 70 61 73 73 65 64 20 61 73 20 70 61 are passed as pa
2b1a0 72 61 6d 65 74 65 72 73 20 69 6e 74 6f 20 74 68 rameters into th
2b1b0 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 e.** implementat
2b1c0 69 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 63 61 74 ion of [applicat
2b1d0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 ion-defined SQL
2b1e0 66 75 6e 63 74 69 6f 6e 73 5d 20 61 72 65 20 70 functions] are p
2b1f0 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 5e 54 68 rotected..** ^Th
2b200 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 e sqlite3_value
2b210 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 object returned
2b220 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 by.** [sqlite3_c
2b230 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 olumn_value()] i
2b240 73 20 75 6e 70 72 6f 74 65 63 74 65 64 2e 0a 2a s unprotected..*
2b250 2a 20 55 6e 70 72 6f 74 65 63 74 65 64 20 73 71 * Unprotected sq
2b260 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
2b270 63 74 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 cts may only be
2b280 75 73 65 64 20 77 69 74 68 0a 2a 2a 20 5b 73 71 used with.** [sq
2b290 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c lite3_result_val
2b2a0 75 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 ue()] and [sqlit
2b2b0 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d e3_bind_value()]
2b2c0 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 ..** The [sqlite
2b2d0 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 7c 20 73 3_value_blob | s
2b2e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
2b2f0 65 28 29 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a e()] family of.*
2b300 2a 20 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 * interfaces req
2b310 75 69 72 65 20 70 72 6f 74 65 63 74 65 64 20 73 uire protected s
2b320 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
2b330 65 63 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ects..*/.typedef
2b340 20 73 74 72 75 63 74 20 4d 65 6d 20 73 71 6c 69 struct Mem sqli
2b350 74 65 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a te3_value;../*.*
2b360 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20 * CAPI3REF: SQL
2b370 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 Function Context
2b380 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 Object.**.** Th
2b390 65 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 e context in whi
2b3a0 63 68 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 ch an SQL functi
2b3b0 6f 6e 20 65 78 65 63 75 74 65 73 20 69 73 20 73 on executes is s
2b3c0 74 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73 tored in an.** s
2b3d0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f qlite3_context o
2b3e0 62 6a 65 63 74 2e 20 20 5e 41 20 70 6f 69 6e 74 bject. ^A point
2b3f0 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 er to an sqlite3
2b400 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 0a _context object.
2b410 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 66 69 72 ** is always fir
2b420 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 st parameter to
2b430 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 [application-def
2b440 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f ined SQL functio
2b450 6e 73 5d 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c ns]..** The appl
2b460 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
2b470 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 SQL function imp
2b480 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c lementation will
2b490 20 70 61 73 73 20 74 68 69 73 0a 2a 2a 20 70 6f pass this.** po
2b4a0 69 6e 74 65 72 20 74 68 72 6f 75 67 68 20 69 6e inter through in
2b4b0 74 6f 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c to calls to [sql
2b4c0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 ite3_result_int
2b4d0 7c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 | sqlite3_result
2b4e0 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ()],.** [sqlite3
2b4f0 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 _aggregate_conte
2b500 78 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f xt()], [sqlite3_
2b510 75 73 65 72 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a user_data()],.**
2b520 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 [sqlite3_contex
2b530 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29 5d 2c 20 t_db_handle()],
2b540 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 [sqlite3_get_aux
2b550 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f data()],.** and/
2b560 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f or [sqlite3_set_
2b570 61 75 78 64 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74 auxdata()]..*/.t
2b580 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
2b590 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 71 lite3_context sq
2b5a0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a lite3_context;..
2b5b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2b5c0 42 69 6e 64 69 6e 67 20 56 61 6c 75 65 73 20 54 Binding Values T
2b5d0 6f 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 o Prepared State
2b5e0 6d 65 6e 74 73 0a 2a 2a 20 4b 45 59 57 4f 52 44 ments.** KEYWORD
2b5f0 53 3a 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 S: {host paramet
2b600 65 72 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 er} {host parame
2b610 74 65 72 73 7d 20 7b 68 6f 73 74 20 70 61 72 61 ters} {host para
2b620 6d 65 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20 4b meter name}.** K
2b630 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 70 61 EYWORDS: {SQL pa
2b640 72 61 6d 65 74 65 72 7d 20 7b 53 51 4c 20 70 61 rameter} {SQL pa
2b650 72 61 6d 65 74 65 72 73 7d 20 7b 70 61 72 61 6d rameters} {param
2b660 65 74 65 72 20 62 69 6e 64 69 6e 67 7d 0a 2a 2a eter binding}.**
2b670 0a 2a 2a 20 5e 28 49 6e 20 74 68 65 20 53 51 4c .** ^(In the SQL
2b680 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 20 statement text
2b690 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 input to [sqlite
2b6a0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 3_prepare_v2()]
2b6b0 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 and its variants
2b6c0 2c 0a 2a 2a 20 6c 69 74 65 72 61 6c 73 20 6d 61 ,.** literals ma
2b6d0 79 20 62 65 20 72 65 70 6c 61 63 65 64 20 62 79 y be replaced by
2b6e0 20 61 20 5b 70 61 72 61 6d 65 74 65 72 5d 20 74 a [parameter] t
2b6f0 68 61 74 20 6d 61 74 63 68 65 73 20 6f 6e 65 20 hat matches one
2b700 6f 66 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 of following.**
2b710 74 65 6d 70 6c 61 74 65 73 3a 0a 2a 2a 0a 2a 2a templates:.**.**
2b720 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 3f <ul>.** <li> ?
2b730 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a 2a .** <li> ?NNN.*
2b740 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a 2a 20 * <li> :VVV.**
2b750 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a 20 3c 6c <li> @VVV.** <l
2b760 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c 2f 75 6c i> $VVV.** </ul
2b770 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 74 >.**.** In the t
2b780 65 6d 70 6c 61 74 65 73 20 61 62 6f 76 65 2c 20 emplates above,
2b790 4e 4e 4e 20 72 65 70 72 65 73 65 6e 74 73 20 61 NNN represents a
2b7a0 6e 20 69 6e 74 65 67 65 72 20 6c 69 74 65 72 61 n integer litera
2b7b0 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56 56 20 72 65 l,.** and VVV re
2b7c0 70 72 65 73 65 6e 74 73 20 61 6e 20 61 6c 70 68 presents an alph
2b7d0 61 6e 75 6d 65 72 69 63 20 69 64 65 6e 74 69 66 anumeric identif
2b7e0 69 65 72 2e 29 5e 20 20 5e 54 68 65 20 76 61 6c ier.)^ ^The val
2b7f0 75 65 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 ues of these.**
2b800 70 61 72 61 6d 65 74 65 72 73 20 28 61 6c 73 6f parameters (also
2b810 20 63 61 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 called "host pa
2b820 72 61 6d 65 74 65 72 20 6e 61 6d 65 73 22 20 6f rameter names" o
2b830 72 20 22 53 51 4c 20 70 61 72 61 6d 65 74 65 72 r "SQL parameter
2b840 73 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65 s").** can be se
2b850 74 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 t using the sqli
2b860 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 te3_bind_*() rou
2b870 74 69 6e 65 73 20 64 65 66 69 6e 65 64 20 68 65 tines defined he
2b880 72 65 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 re..**.** ^The f
2b890 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
2b8a0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e the sqlite3_bin
2b8b0 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 69 d_*() routines i
2b8c0 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61 20 70 6f s always.** a po
2b8d0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71 inter to the [sq
2b8e0 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 lite3_stmt] obje
2b8f0 63 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d ct returned from
2b900 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 .** [sqlite3_pre
2b910 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74 pare_v2()] or it
2b920 73 20 76 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a s variants..**.*
2b930 2a 20 5e 54 68 65 20 73 65 63 6f 6e 64 20 61 72 * ^The second ar
2b940 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e gument is the in
2b950 64 65 78 20 6f 66 20 74 68 65 20 53 51 4c 20 70 dex of the SQL p
2b960 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20 73 arameter to be s
2b970 65 74 2e 0a 2a 2a 20 5e 54 68 65 20 6c 65 66 74 et..** ^The left
2b980 6d 6f 73 74 20 53 51 4c 20 70 61 72 61 6d 65 74 most SQL paramet
2b990 65 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 er has an index
2b9a0 6f 66 20 31 2e 20 20 5e 57 68 65 6e 20 74 68 65 of 1. ^When the
2b9b0 20 73 61 6d 65 20 6e 61 6d 65 64 0a 2a 2a 20 53 same named.** S
2b9c0 51 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 QL parameter is
2b9d0 75 73 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f used more than o
2b9e0 6e 63 65 2c 20 73 65 63 6f 6e 64 20 61 6e 64 20 nce, second and
2b9f0 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 6f 63 subsequent.** oc
2ba00 63 75 72 72 65 6e 63 65 73 20 68 61 76 65 20 74 currences have t
2ba10 68 65 20 73 61 6d 65 20 69 6e 64 65 78 20 61 73 he same index as
2ba20 20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75 72 the first occur
2ba30 72 65 6e 63 65 2e 0a 2a 2a 20 5e 54 68 65 20 69 rence..** ^The i
2ba40 6e 64 65 78 20 66 6f 72 20 6e 61 6d 65 64 20 70 ndex for named p
2ba50 61 72 61 6d 65 74 65 72 73 20 63 61 6e 20 62 65 arameters can be
2ba60 20 6c 6f 6f 6b 65 64 20 75 70 20 75 73 69 6e 67 looked up using
2ba70 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
2ba80 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
2ba90 69 6e 64 65 78 28 29 5d 20 41 50 49 20 69 66 20 index()] API if
2baa0 64 65 73 69 72 65 64 2e 20 20 5e 54 68 65 20 69 desired. ^The i
2bab0 6e 64 65 78 0a 2a 2a 20 66 6f 72 20 22 3f 4e 4e ndex.** for "?NN
2bac0 4e 22 20 70 61 72 61 6d 65 74 65 72 73 20 69 73 N" parameters is
2bad0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4e 4e the value of NN
2bae0 4e 2e 0a 2a 2a 20 5e 54 68 65 20 4e 4e 4e 20 76 N..** ^The NNN v
2baf0 61 6c 75 65 20 6d 75 73 74 20 62 65 20 62 65 74 alue must be bet
2bb00 77 65 65 6e 20 31 20 61 6e 64 20 74 68 65 20 5b ween 1 and the [
2bb10 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d sqlite3_limit()]
2bb20 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 5b 53 .** parameter [S
2bb30 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 QLITE_LIMIT_VARI
2bb40 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 28 64 65 ABLE_NUMBER] (de
2bb50 66 61 75 6c 74 20 76 61 6c 75 65 3a 20 39 39 39 fault value: 999
2bb60 29 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 74 68 )..**.** ^The th
2bb70 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ird argument is
2bb80 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 69 6e the value to bin
2bb90 64 20 74 6f 20 74 68 65 20 70 61 72 61 6d 65 74 d to the paramet
2bba0 65 72 2e 0a 2a 2a 0a 2a 2a 20 5e 28 49 6e 20 74 er..**.** ^(In t
2bbb0 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 74 68 hose routines th
2bbc0 61 74 20 68 61 76 65 20 61 20 66 6f 75 72 74 68 at have a fourth
2bbd0 20 61 72 67 75 6d 65 6e 74 2c 20 69 74 73 20 76 argument, its v
2bbe0 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 6e alue is the.** n
2bbf0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
2bc00 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e n the parameter.
2bc10 20 20 54 6f 20 62 65 20 63 6c 65 61 72 3a 20 74 To be clear: t
2bc20 68 65 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a he value is the.
2bc30 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 3c 75 3e ** number of <u>
2bc40 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20 74 68 65 bytes</u> in the
2bc50 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74 68 65 20 value, not the
2bc60 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 number of charac
2bc70 74 65 72 73 2e 29 5e 0a 2a 2a 20 5e 49 66 20 74 ters.)^.** ^If t
2bc80 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 he fourth parame
2bc90 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 ter to sqlite3_b
2bca0 69 6e 64 5f 74 65 78 74 28 29 20 6f 72 20 73 71 ind_text() or sq
2bcb0 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 lite3_bind_text1
2bcc0 36 28 29 0a 2a 2a 20 69 73 20 6e 65 67 61 74 69 6().** is negati
2bcd0 76 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 65 6e ve, then the len
2bce0 67 74 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e gth of the strin
2bcf0 67 20 69 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 g is.** the numb
2bd00 65 72 20 6f 66 20 62 79 74 65 73 20 75 70 20 74 er of bytes up t
2bd10 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f o the first zero
2bd20 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 20 terminator..**
2bd30 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 If the fourth pa
2bd40 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
2bd50 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 29 20 69 e3_bind_blob() i
2bd60 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e s negative, then
2bd70 0a 2a 2a 20 74 68 65 20 62 65 68 61 76 69 6f 72 .** the behavior
2bd80 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
2bd90 2a 20 49 66 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 * If a non-negat
2bda0 69 76 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d ive fourth param
2bdb0 65 74 65 72 20 69 73 20 70 72 6f 76 69 64 65 64 eter is provided
2bdc0 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 to sqlite3_bind
2bdd0 5f 74 65 78 74 28 29 0a 2a 2a 20 6f 72 20 73 71 _text().** or sq
2bde0 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 lite3_bind_text1
2bdf0 36 28 29 20 74 68 65 6e 20 74 68 61 74 20 70 61 6() then that pa
2be00 72 61 6d 65 74 65 72 20 6d 75 73 74 20 62 65 20 rameter must be
2be10 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 0a the byte offset.
2be20 2a 2a 20 77 68 65 72 65 20 74 68 65 20 4e 55 4c ** where the NUL
2be30 20 74 65 72 6d 69 6e 61 74 6f 72 20 77 6f 75 6c terminator woul
2be40 64 20 6f 63 63 75 72 20 61 73 73 75 6d 69 6e 67 d occur assuming
2be50 20 74 68 65 20 73 74 72 69 6e 67 20 77 65 72 65 the string were
2be60 20 4e 55 4c 0a 2a 2a 20 74 65 72 6d 69 6e 61 74 NUL.** terminat
2be70 65 64 2e 20 20 49 66 20 61 6e 79 20 4e 55 4c 20 ed. If any NUL
2be80 63 68 61 72 61 63 74 65 72 73 20 6f 63 63 75 72 characters occur
2be90 20 61 74 20 62 79 74 65 20 6f 66 66 73 65 74 73 at byte offsets
2bea0 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 less than .** t
2beb0 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
2bec0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 fourth parameter
2bed0 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 then the result
2bee0 69 6e 67 20 73 74 72 69 6e 67 20 76 61 6c 75 65 ing string value
2bef0 20 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 61 69 6e will.** contain
2bf00 20 65 6d 62 65 64 64 65 64 20 4e 55 4c 73 2e 20 embedded NULs.
2bf10 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 The result of e
2bf20 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 76 6f 6c xpressions invol
2bf30 76 69 6e 67 20 73 74 72 69 6e 67 73 0a 2a 2a 20 ving strings.**
2bf40 77 69 74 68 20 65 6d 62 65 64 64 65 64 20 4e 55 with embedded NU
2bf50 4c 73 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e Ls is undefined.
2bf60 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 66 74 .**.** ^The fift
2bf70 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 h argument to sq
2bf80 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 lite3_bind_blob(
2bf90 29 2c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f ), sqlite3_bind_
2bfa0 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a 2a 20 73 text(), and.** s
2bfb0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 qlite3_bind_text
2bfc0 31 36 28 29 20 69 73 20 61 20 64 65 73 74 72 75 16() is a destru
2bfd0 63 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 73 ctor used to dis
2bfe0 70 6f 73 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 pose of the BLOB
2bff0 20 6f 72 0a 2a 2a 20 73 74 72 69 6e 67 20 61 66 or.** string af
2c000 74 65 72 20 53 51 4c 69 74 65 20 68 61 73 20 66 ter SQLite has f
2c010 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74 2e inished with it.
2c020 20 20 5e 54 68 65 20 64 65 73 74 72 75 63 74 6f ^The destructo
2c030 72 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 74 r is called.** t
2c040 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 o dispose of the
2c050 20 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 20 BLOB or string
2c060 65 76 65 6e 20 69 66 20 74 68 65 20 63 61 6c 6c even if the call
2c070 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 to sqlite3_bind
2c080 5f 62 6c 6f 62 28 29 2c 0a 2a 2a 20 73 71 6c 69 _blob(),.** sqli
2c090 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 29 2c te3_bind_text(),
2c0a0 20 6f 72 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 or sqlite3_bind
2c0b0 5f 74 65 78 74 31 36 28 29 20 66 61 69 6c 73 2e _text16() fails.
2c0c0 20 20 0a 2a 2a 20 5e 49 66 20 74 68 65 20 66 69 .** ^If the fi
2c0d0 66 74 68 20 61 72 67 75 6d 65 6e 74 20 69 73 0a fth argument is.
2c0e0 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 76 ** the special v
2c0f0 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 alue [SQLITE_STA
2c100 54 49 43 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74 TIC], then SQLit
2c110 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 e assumes that t
2c120 68 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f he.** informatio
2c130 6e 20 69 73 20 69 6e 20 73 74 61 74 69 63 2c 20 n is in static,
2c140 75 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 unmanaged space
2c150 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 and does not nee
2c160 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a d to be freed..*
2c170 2a 20 5e 49 66 20 74 68 65 20 66 69 66 74 68 20 * ^If the fifth
2c180 61 72 67 75 6d 65 6e 74 20 68 61 73 20 74 68 65 argument has the
2c190 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 54 value [SQLITE_T
2c1a0 52 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e 0a RANSIENT], then.
2c1b0 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 ** SQLite makes
2c1c0 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 its own private
2c1d0 63 6f 70 79 20 6f 66 20 74 68 65 20 64 61 74 61 copy of the data
2c1e0 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 immediately, be
2c1f0 66 6f 72 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69 fore.** the sqli
2c200 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 te3_bind_*() rou
2c210 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a tine returns..**
2c220 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 .** ^The sqlite3
2c230 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 _bind_zeroblob()
2c240 20 72 6f 75 74 69 6e 65 20 62 69 6e 64 73 20 61 routine binds a
2c250 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20 BLOB of length
2c260 4e 20 74 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c N that.** is fil
2c270 6c 65 64 20 77 69 74 68 20 7a 65 72 6f 65 73 2e led with zeroes.
2c280 20 20 5e 41 20 7a 65 72 6f 62 6c 6f 62 20 75 73 ^A zeroblob us
2c290 65 73 20 61 20 66 69 78 65 64 20 61 6d 6f 75 6e es a fixed amoun
2c2a0 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 t of memory.** (
2c2b0 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 just an integer
2c2c0 74 6f 20 68 6f 6c 64 20 69 74 73 20 73 69 7a 65 to hold its size
2c2d0 29 20 77 68 69 6c 65 20 69 74 20 69 73 20 62 65 ) while it is be
2c2e0 69 6e 67 20 70 72 6f 63 65 73 73 65 64 2e 0a 2a ing processed..*
2c2f0 2a 20 5a 65 72 6f 62 6c 6f 62 73 20 61 72 65 20 * Zeroblobs are
2c300 69 6e 74 65 6e 64 65 64 20 74 6f 20 73 65 72 76 intended to serv
2c310 65 20 61 73 20 70 6c 61 63 65 68 6f 6c 64 65 72 e as placeholder
2c320 73 20 66 6f 72 20 42 4c 4f 42 73 20 77 68 6f 73 s for BLOBs whos
2c330 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20 e.** content is
2c340 6c 61 74 65 72 20 77 72 69 74 74 65 6e 20 75 73 later written us
2c350 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ing.** [sqlite3_
2c360 62 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 blob_open | incr
2c370 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f emental BLOB I/O
2c380 5d 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 5e ] routines..** ^
2c390 41 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 A negative value
2c3a0 20 66 6f 72 20 74 68 65 20 7a 65 72 6f 62 6c 6f for the zeroblo
2c3b0 62 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 7a b results in a z
2c3c0 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 2e ero-length BLOB.
2c3d0 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 61 6e 79 20 6f .**.** ^If any o
2c3e0 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 f the sqlite3_bi
2c3f0 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 nd_*() routines
2c400 61 72 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 are called with
2c410 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a a NULL pointer.*
2c420 2a 20 66 6f 72 20 74 68 65 20 5b 70 72 65 70 61 * for the [prepa
2c430 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f red statement] o
2c440 72 20 77 69 74 68 20 61 20 70 72 65 70 61 72 65 r with a prepare
2c450 64 20 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 d statement for
2c460 77 68 69 63 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 which.** [sqlite
2c470 33 5f 73 74 65 70 28 29 5d 20 68 61 73 20 62 65 3_step()] has be
2c480 65 6e 20 63 61 6c 6c 65 64 20 6d 6f 72 65 20 72 en called more r
2c490 65 63 65 6e 74 6c 79 20 74 68 61 6e 20 5b 73 71 ecently than [sq
2c4a0 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 2c 0a lite3_reset()],.
2c4b0 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c ** then the call
2c4c0 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 will return [SQ
2c4d0 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 20 20 49 LITE_MISUSE]. I
2c4e0 66 20 61 6e 79 20 73 71 6c 69 74 65 33 5f 62 69 f any sqlite3_bi
2c4f0 6e 64 5f 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 nd_().** routine
2c500 20 69 73 20 70 61 73 73 65 64 20 61 20 5b 70 72 is passed a [pr
2c510 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
2c520 5d 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 ] that has been
2c530 66 69 6e 61 6c 69 7a 65 64 2c 20 74 68 65 0a 2a finalized, the.*
2c540 2a 20 72 65 73 75 6c 74 20 69 73 20 75 6e 64 65 * result is unde
2c550 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 62 fined and probab
2c560 6c 79 20 68 61 72 6d 66 75 6c 2e 0a 2a 2a 0a 2a ly harmful..**.*
2c570 2a 20 5e 42 69 6e 64 69 6e 67 73 20 61 72 65 20 * ^Bindings are
2c580 6e 6f 74 20 63 6c 65 61 72 65 64 20 62 79 20 74 not cleared by t
2c590 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 he [sqlite3_rese
2c5a0 74 28 29 5d 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a t()] routine..**
2c5b0 20 5e 55 6e 62 6f 75 6e 64 20 70 61 72 61 6d 65 ^Unbound parame
2c5c0 74 65 72 73 20 61 72 65 20 69 6e 74 65 72 70 72 ters are interpr
2c5d0 65 74 65 64 20 61 73 20 4e 55 4c 4c 2e 0a 2a 2a eted as NULL..**
2c5e0 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 .** ^The sqlite3
2c5f0 5f 62 69 6e 64 5f 2a 20 72 6f 75 74 69 6e 65 73 _bind_* routines
2c600 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
2c610 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 6f OK] on success o
2c620 72 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 r an.** [error c
2c630 6f 64 65 5d 20 69 66 20 61 6e 79 74 68 69 6e 67 ode] if anything
2c640 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 20 goes wrong..**
2c650 5e 5b 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d 20 ^[SQLITE_RANGE]
2c660 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 is returned if t
2c670 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 he parameter.**
2c680 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f 66 20 index is out of
2c690 72 61 6e 67 65 2e 20 20 5e 5b 53 51 4c 49 54 45 range. ^[SQLITE
2c6a0 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74 75 72 _NOMEM] is retur
2c6b0 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 ned if malloc()
2c6c0 66 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 fails..**.** See
2c6d0 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f also: [sqlite3_
2c6e0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 bind_parameter_c
2c6f0 6f 75 6e 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c ount()],.** [sql
2c700 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
2c710 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64 ter_name()], and
2c720 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 [sqlite3_bind_p
2c730 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 arameter_index()
2c740 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 ]..*/.SQLITE_API
2c750 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
2c760 64 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 d_blob(sqlite3_s
2c770 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 tmt*, int, const
2c780 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 2c 20 76 void*, int n, v
2c790 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
2c7a0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2c7b0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 qlite3_bind_doub
2c7c0 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a le(sqlite3_stmt*
2c7d0 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65 29 3b 0a , int, double);.
2c7e0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2c7f0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 qlite3_bind_int(
2c800 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
2c810 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
2c820 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2c830 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 71 6c 69 _bind_int64(sqli
2c840 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 te3_stmt*, int,
2c850 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a sqlite3_int64);.
2c860 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2c870 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c qlite3_bind_null
2c880 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
2c890 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 int);.SQLITE_API
2c8a0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
2c8b0 64 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 d_text(sqlite3_s
2c8c0 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 tmt*, int, const
2c8d0 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e 2c 20 76 char*, int n, v
2c8e0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
2c8f0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2c900 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 qlite3_bind_text
2c910 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 16(sqlite3_stmt*
2c920 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 , int, const voi
2c930 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 d*, int, void(*)
2c940 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 (void*));.SQLITE
2c950 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2c960 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69 _bind_value(sqli
2c970 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 te3_stmt*, int,
2c980 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61 const sqlite3_va
2c990 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
2c9a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
2c9b0 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 nd_zeroblob(sqli
2c9c0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 te3_stmt*, int,
2c9d0 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 int n);../*.** C
2c9e0 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 API3REF: Number
2c9f0 4f 66 20 53 51 4c 20 50 61 72 61 6d 65 74 65 72 Of SQL Parameter
2ca00 73 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 72 6f s.**.** ^This ro
2ca10 75 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 65 utine can be use
2ca20 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 d to find the nu
2ca30 6d 62 65 72 20 6f 66 20 5b 53 51 4c 20 70 61 72 mber of [SQL par
2ca40 61 6d 65 74 65 72 73 5d 0a 2a 2a 20 69 6e 20 61 ameters].** in a
2ca50 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
2ca60 6d 65 6e 74 5d 2e 20 20 53 51 4c 20 70 61 72 61 ment]. SQL para
2ca70 6d 65 74 65 72 73 20 61 72 65 20 74 6f 6b 65 6e meters are token
2ca80 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d s of the.** form
2ca90 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 3a "?", "?NNN", ":
2caa0 41 41 41 22 2c 20 22 24 41 41 41 22 2c 20 6f 72 AAA", "$AAA", or
2cab0 20 22 40 41 41 41 22 20 74 68 61 74 20 73 65 72 "@AAA" that ser
2cac0 76 65 20 61 73 0a 2a 2a 20 70 6c 61 63 65 68 6f ve as.** placeho
2cad0 6c 64 65 72 73 20 66 6f 72 20 76 61 6c 75 65 73 lders for values
2cae0 20 74 68 61 74 20 61 72 65 20 5b 73 71 6c 69 74 that are [sqlit
2caf0 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 e3_bind_blob | b
2cb00 6f 75 6e 64 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 ound].** to the
2cb10 70 61 72 61 6d 65 74 65 72 73 20 61 74 20 61 20 parameters at a
2cb20 6c 61 74 65 72 20 74 69 6d 65 2e 0a 2a 2a 0a 2a later time..**.*
2cb30 2a 20 5e 28 54 68 69 73 20 72 6f 75 74 69 6e 65 * ^(This routine
2cb40 20 61 63 74 75 61 6c 6c 79 20 72 65 74 75 72 6e actually return
2cb50 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 s the index of t
2cb60 68 65 20 6c 61 72 67 65 73 74 20 28 72 69 67 68 he largest (righ
2cb70 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 61 6d 65 tmost).** parame
2cb80 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20 66 6f 72 ter. For all for
2cb90 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e 4e 2c 20 ms except ?NNN,
2cba0 74 68 69 73 20 77 69 6c 6c 20 63 6f 72 72 65 73 this will corres
2cbb0 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a 2a 20 6e pond to the.** n
2cbc0 75 6d 62 65 72 20 6f 66 20 75 6e 69 71 75 65 20 umber of unique
2cbd0 70 61 72 61 6d 65 74 65 72 73 2e 20 20 49 66 20 parameters. If
2cbe0 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68 parameters of th
2cbf0 65 20 3f 4e 4e 4e 20 66 6f 72 6d 20 61 72 65 20 e ?NNN form are
2cc00 75 73 65 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d used,.** there m
2cc10 61 79 20 62 65 20 67 61 70 73 20 69 6e 20 74 68 ay be gaps in th
2cc20 65 20 6c 69 73 74 2e 29 5e 0a 2a 2a 0a 2a 2a 20 e list.)^.**.**
2cc30 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 See also: [sqlit
2cc40 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c e3_bind_blob|sql
2cc50 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a ite3_bind()],.**
2cc60 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 [sqlite3_bind_p
2cc70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d arameter_name()]
2cc80 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 , and.** [sqlite
2cc90 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 3_bind_parameter
2cca0 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2f 0a 53 51 _index()]..*/.SQ
2ccb0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
2ccc0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
2ccd0 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 ter_count(sqlite
2cce0 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 3_stmt*);../*.**
2ccf0 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 CAPI3REF: Name
2cd00 4f 66 20 41 20 48 6f 73 74 20 50 61 72 61 6d 65 Of A Host Parame
2cd10 74 65 72 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 ter.**.** ^The s
2cd20 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
2cd30 6d 65 74 65 72 5f 6e 61 6d 65 28 50 2c 4e 29 20 meter_name(P,N)
2cd40 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
2cd50 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 s.** the name of
2cd60 20 74 68 65 20 4e 2d 74 68 20 5b 53 51 4c 20 70 the N-th [SQL p
2cd70 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 74 68 65 arameter] in the
2cd80 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
2cd90 6d 65 6e 74 5d 20 50 2e 0a 2a 2a 20 5e 28 53 51 ment] P..** ^(SQ
2cda0 4c 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 L parameters of
2cdb0 74 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20 the form "?NNN"
2cdc0 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40 41 or ":AAA" or "@A
2cdd0 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a 2a AA" or "$AAA".**
2cde0 20 68 61 76 65 20 61 20 6e 61 6d 65 20 77 68 69 have a name whi
2cdf0 63 68 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 ch is the string
2ce00 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 41 "?NNN" or ":AAA
2ce10 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20 22 " or "@AAA" or "
2ce20 24 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 63 74 $AAA".** respect
2ce30 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68 ively..** In oth
2ce40 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 69 6e er words, the in
2ce50 69 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 24 22 itial ":" or "$"
2ce60 20 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 0a 2a or "@" or "?".*
2ce70 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 20 61 73 * is included as
2ce80 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d part of the nam
2ce90 65 2e 29 5e 0a 2a 2a 20 5e 50 61 72 61 6d 65 74 e.)^.** ^Paramet
2cea0 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 ers of the form
2ceb0 22 3f 22 20 77 69 74 68 6f 75 74 20 61 20 66 6f "?" without a fo
2cec0 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 67 65 72 20 llowing integer
2ced0 68 61 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 have no name.**
2cee0 61 6e 64 20 61 72 65 20 72 65 66 65 72 72 65 64 and are referred
2cef0 20 74 6f 20 61 73 20 22 6e 61 6d 65 6c 65 73 73 to as "nameless
2cf00 22 20 6f 72 20 22 61 6e 6f 6e 79 6d 6f 75 73 20 " or "anonymous
2cf10 70 61 72 61 6d 65 74 65 72 73 22 2e 0a 2a 2a 0a parameters"..**.
2cf20 2a 2a 20 5e 54 68 65 20 66 69 72 73 74 20 68 6f ** ^The first ho
2cf30 73 74 20 70 61 72 61 6d 65 74 65 72 20 68 61 73 st parameter has
2cf40 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2c 20 an index of 1,
2cf50 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 not 0..**.** ^If
2cf60 20 74 68 65 20 76 61 6c 75 65 20 4e 20 69 73 20 the value N is
2cf70 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 20 out of range or
2cf80 69 66 20 74 68 65 20 4e 2d 74 68 20 70 61 72 61 if the N-th para
2cf90 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e 61 6d 65 meter is.** name
2cfa0 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 less, then NULL
2cfb0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 5e 54 is returned. ^T
2cfc0 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 he returned stri
2cfd0 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 ng is.** always
2cfe0 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e in UTF-8 encodin
2cff0 67 20 65 76 65 6e 20 69 66 20 74 68 65 20 6e 61 g even if the na
2d000 6d 65 64 20 70 61 72 61 6d 65 74 65 72 20 77 61 med parameter wa
2d010 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 s.** originally
2d020 73 70 65 63 69 66 69 65 64 20 61 73 20 55 54 46 specified as UTF
2d030 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f -16 in [sqlite3_
2d040 70 72 65 70 61 72 65 31 36 28 29 5d 20 6f 72 0a prepare16()] or.
2d050 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ** [sqlite3_prep
2d060 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a are16_v2()]..**.
2d070 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 ** See also: [sq
2d080 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c lite3_bind_blob|
2d090 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c sqlite3_bind()],
2d0a0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e .** [sqlite3_bin
2d0b0 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e d_parameter_coun
2d0c0 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 t()], and.** [sq
2d0d0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
2d0e0 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a eter_index()]..*
2d0f0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
2d100 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
2d110 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
2d120 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d name(sqlite3_stm
2d130 74 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a t*, int);../*.**
2d140 20 43 41 50 49 33 52 45 46 3a 20 49 6e 64 65 78 CAPI3REF: Index
2d150 20 4f 66 20 41 20 50 61 72 61 6d 65 74 65 72 20 Of A Parameter
2d160 57 69 74 68 20 41 20 47 69 76 65 6e 20 4e 61 6d With A Given Nam
2d170 65 0a 2a 2a 0a 2a 2a 20 5e 52 65 74 75 72 6e 20 e.**.** ^Return
2d180 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 the index of an
2d190 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69 SQL parameter gi
2d1a0 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 5e ven its name. ^
2d1b0 54 68 65 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c The.** index val
2d1c0 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 73 ue returned is s
2d1d0 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 uitable for use
2d1e0 61 73 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a as the second.**
2d1f0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
2d200 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
2d210 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d |sqlite3_bind()]
2d220 2e 20 20 5e 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 . ^A zero.** is
2d230 20 72 65 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 returned if no
2d240 6d 61 74 63 68 69 6e 67 20 70 61 72 61 6d 65 74 matching paramet
2d250 65 72 20 69 73 20 66 6f 75 6e 64 2e 20 20 5e 54 er is found. ^T
2d260 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 he parameter.**
2d270 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 67 69 76 name must be giv
2d280 65 6e 20 69 6e 20 55 54 46 2d 38 20 65 76 65 6e en in UTF-8 even
2d290 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c if the original
2d2a0 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 77 61 statement.** wa
2d2b0 73 20 70 72 65 70 61 72 65 64 20 66 72 6f 6d 20 s prepared from
2d2c0 55 54 46 2d 31 36 20 74 65 78 74 20 75 73 69 6e UTF-16 text usin
2d2d0 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 g [sqlite3_prepa
2d2e0 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a re16_v2()]..**.*
2d2f0 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c * See also: [sql
2d300 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 ite3_bind_blob|s
2d310 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a qlite3_bind()],.
2d320 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 ** [sqlite3_bind
2d330 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 _parameter_count
2d340 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c ()], and.** [sql
2d350 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
2d360 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2f ter_index()]..*/
2d370 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
2d380 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
2d390 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c ameter_index(sql
2d3a0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 63 6f 6e 73 ite3_stmt*, cons
2d3b0 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 3b 0a t char *zName);.
2d3c0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
2d3d0 20 52 65 73 65 74 20 41 6c 6c 20 42 69 6e 64 69 Reset All Bindi
2d3e0 6e 67 73 20 4f 6e 20 41 20 50 72 65 70 61 72 65 ngs On A Prepare
2d3f0 64 20 53 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a d Statement.**.*
2d400 2a 20 5e 43 6f 6e 74 72 61 72 79 20 74 6f 20 74 * ^Contrary to t
2d410 68 65 20 69 6e 74 75 69 74 69 6f 6e 20 6f 66 20 he intuition of
2d420 6d 61 6e 79 2c 20 5b 73 71 6c 69 74 65 33 5f 72 many, [sqlite3_r
2d430 65 73 65 74 28 29 5d 20 64 6f 65 73 20 6e 6f 74 eset()] does not
2d440 20 72 65 73 65 74 0a 2a 2a 20 74 68 65 20 5b 73 reset.** the [s
2d450 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
2d460 20 7c 20 62 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 | bindings] on
2d470 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 a [prepared stat
2d480 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 5e 55 73 65 20 ement]..** ^Use
2d490 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 this routine to
2d4a0 72 65 73 65 74 20 61 6c 6c 20 68 6f 73 74 20 70 reset all host p
2d4b0 61 72 61 6d 65 74 65 72 73 20 74 6f 20 4e 55 4c arameters to NUL
2d4c0 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 L..*/.SQLITE_API
2d4d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 int sqlite3_cle
2d4e0 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 ar_bindings(sqli
2d4f0 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a te3_stmt*);../*.
2d500 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d ** CAPI3REF: Num
2d510 62 65 72 20 4f 66 20 43 6f 6c 75 6d 6e 73 20 49 ber Of Columns I
2d520 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 0a 2a n A Result Set.*
2d530 2a 0a 2a 2a 20 5e 52 65 74 75 72 6e 20 74 68 65 *.** ^Return the
2d540 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
2d550 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 ns in the result
2d560 20 73 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 set returned by
2d570 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 the.** [prepare
2d580 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 5e 54 d statement]. ^T
2d590 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
2d5a0 72 6e 73 20 30 20 69 66 20 70 53 74 6d 74 20 69 rns 0 if pStmt i
2d5b0 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 73 74 61 74 s an SQL.** stat
2d5c0 65 6d 65 6e 74 20 74 68 61 74 20 64 6f 65 73 20 ement that does
2d5d0 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 74 61 20 not return data
2d5e0 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e 20 (for example an
2d5f0 5b 55 50 44 41 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a [UPDATE])..**.**
2d600 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 See also: [sqli
2d610 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 29 te3_data_count()
2d620 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
2d630 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 int sqlite3_colu
2d640 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 mn_count(sqlite3
2d650 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a _stmt *pStmt);..
2d660 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2d670 43 6f 6c 75 6d 6e 20 4e 61 6d 65 73 20 49 6e 20 Column Names In
2d680 41 20 52 65 73 75 6c 74 20 53 65 74 0a 2a 2a 0a A Result Set.**.
2d690 2a 2a 20 5e 54 68 65 73 65 20 72 6f 75 74 69 6e ** ^These routin
2d6a0 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 es return the na
2d6b0 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 me assigned to a
2d6c0 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 particular colu
2d6d0 6d 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 mn.** in the res
2d6e0 75 6c 74 20 73 65 74 20 6f 66 20 61 20 5b 53 45 ult set of a [SE
2d6f0 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e LECT] statement.
2d700 20 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ^The sqlite3_c
2d710 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 0a 2a 2a 20 olumn_name().**
2d720 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
2d730 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
2d740 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
2d750 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a UTF-8 string.**
2d760 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c and sqlite3_col
2d770 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 72 65 74 umn_name16() ret
2d780 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
2d790 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 o a zero-termina
2d7a0 74 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 ted.** UTF-16 st
2d7b0 72 69 6e 67 2e 20 20 5e 54 68 65 20 66 69 72 73 ring. ^The firs
2d7c0 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 t parameter is t
2d7d0 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
2d7e0 74 65 6d 65 6e 74 5d 0a 2a 2a 20 74 68 61 74 20 tement].** that
2d7f0 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 5b implements the [
2d800 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e SELECT] statemen
2d810 74 2e 20 5e 54 68 65 20 73 65 63 6f 6e 64 20 70 t. ^The second p
2d820 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 0a arameter is the.
2d830 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 ** column number
2d840 2e 20 20 5e 54 68 65 20 6c 65 66 74 6d 6f 73 74 . ^The leftmost
2d850 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 75 6d 62 65 column is numbe
2d860 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 r 0..**.** ^The
2d870 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
2d880 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64 pointer is valid
2d890 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 74 68 until either th
2d8a0 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
2d8b0 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 20 64 65 73 ement].** is des
2d8c0 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74 troyed by [sqlit
2d8d0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f e3_finalize()] o
2d8e0 72 20 75 6e 74 69 6c 20 74 68 65 20 73 74 61 74 r until the stat
2d8f0 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d 61 74 ement is automat
2d900 69 63 61 6c 6c 79 0a 2a 2a 20 72 65 70 72 65 70 ically.** reprep
2d910 61 72 65 64 20 62 79 20 74 68 65 20 66 69 72 73 ared by the firs
2d920 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 t call to [sqlit
2d930 65 33 5f 73 74 65 70 28 29 5d 20 66 6f 72 20 61 e3_step()] for a
2d940 20 70 61 72 74 69 63 75 6c 61 72 20 72 75 6e 0a particular run.
2d950 2a 2a 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 ** or until the
2d960 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 next call to.**
2d970 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e sqlite3_column_n
2d980 61 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 65 33 ame() or sqlite3
2d990 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 _column_name16()
2d9a0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 6f 6c on the same col
2d9b0 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 73 umn..**.** ^If s
2d9c0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
2d9d0 66 61 69 6c 73 20 64 75 72 69 6e 67 20 74 68 65 fails during the
2d9e0 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 65 processing of e
2d9f0 69 74 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a ither routine.**
2da00 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 75 (for example du
2da10 72 69 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 6f ring a conversio
2da20 6e 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 n from UTF-8 to
2da30 55 54 46 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a UTF-16) then a.*
2da40 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 * NULL pointer i
2da50 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
2da60 2a 20 5e 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 * ^The name of a
2da70 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 result column i
2da80 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 s the value of t
2da90 68 65 20 22 41 53 22 20 63 6c 61 75 73 65 20 66 he "AS" clause f
2daa0 6f 72 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d or.** that colum
2dab0 6e 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 61 n, if there is a
2dac0 6e 20 41 53 20 63 6c 61 75 73 65 2e 20 20 49 66 n AS clause. If
2dad0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 41 53 20 there is no AS
2dae0 63 6c 61 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74 clause.** then t
2daf0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 he name of the c
2db00 6f 6c 75 6d 6e 20 69 73 20 75 6e 73 70 65 63 69 olumn is unspeci
2db10 66 69 65 64 20 61 6e 64 20 6d 61 79 20 63 68 61 fied and may cha
2db20 6e 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 nge from.** one
2db30 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74 release of SQLit
2db40 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a e to the next..*
2db50 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
2db60 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
2db70 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c _column_name(sql
2db80 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
2db90 4e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 N);.SQLITE_API c
2dba0 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
2dbb0 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 e3_column_name16
2dbc0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
2dbd0 69 6e 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 int N);../*.** C
2dbe0 41 50 49 33 52 45 46 3a 20 53 6f 75 72 63 65 20 API3REF: Source
2dbf0 4f 66 20 44 61 74 61 20 49 6e 20 41 20 51 75 65 Of Data In A Que
2dc00 72 79 20 52 65 73 75 6c 74 0a 2a 2a 0a 2a 2a 20 ry Result.**.**
2dc10 5e 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 ^These routines
2dc20 70 72 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20 provide a means
2dc30 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
2dc40 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 database, table
2dc50 2c 20 61 6e 64 0a 2a 2a 20 74 61 62 6c 65 20 63 , and.** table c
2dc60 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74 68 olumn that is th
2dc70 65 20 6f 72 69 67 69 6e 20 6f 66 20 61 20 70 61 e origin of a pa
2dc80 72 74 69 63 75 6c 61 72 20 72 65 73 75 6c 74 20 rticular result
2dc90 63 6f 6c 75 6d 6e 20 69 6e 0a 2a 2a 20 5b 53 45 column in.** [SE
2dca0 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e LECT] statement.
2dcb0 0a 2a 2a 20 5e 54 68 65 20 6e 61 6d 65 20 6f 66 .** ^The name of
2dcc0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 the database or
2dcd0 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e table or column
2dce0 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 can be returned
2dcf0 20 61 73 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 as.** either a
2dd00 55 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 UTF-8 or UTF-16
2dd10 73 74 72 69 6e 67 2e 20 20 5e 54 68 65 20 5f 64 string. ^The _d
2dd20 61 74 61 62 61 73 65 5f 20 72 6f 75 74 69 6e 65 atabase_ routine
2dd30 73 20 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 s return.** the
2dd40 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 74 database name, t
2dd50 68 65 20 5f 74 61 62 6c 65 5f 20 72 6f 75 74 69 he _table_ routi
2dd60 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 74 nes return the t
2dd70 61 62 6c 65 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a able name, and.*
2dd80 2a 20 74 68 65 20 6f 72 69 67 69 6e 5f 20 72 6f * the origin_ ro
2dd90 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 utines return th
2dda0 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a e column name..*
2ddb0 2a 20 5e 54 68 65 20 72 65 74 75 72 6e 65 64 20 * ^The returned
2ddc0 73 74 72 69 6e 67 20 69 73 20 76 61 6c 69 64 20 string is valid
2ddd0 75 6e 74 69 6c 20 74 68 65 20 5b 70 72 65 70 61 until the [prepa
2dde0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 red statement] i
2ddf0 73 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20 75 s destroyed.** u
2de00 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 sing [sqlite3_fi
2de10 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74 nalize()] or unt
2de20 69 6c 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 il the statement
2de30 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c is automaticall
2de40 79 0a 2a 2a 20 72 65 70 72 65 70 61 72 65 64 20 y.** reprepared
2de50 62 79 20 74 68 65 20 66 69 72 73 74 20 63 61 6c by the first cal
2de60 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 l to [sqlite3_st
2de70 65 70 28 29 5d 20 66 6f 72 20 61 20 70 61 72 74 ep()] for a part
2de80 69 63 75 6c 61 72 20 72 75 6e 0a 2a 2a 20 6f 72 icular run.** or
2de90 20 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 until the same
2dea0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 information is r
2deb0 65 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 equested.** agai
2dec0 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 n in a different
2ded0 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a encoding..**.**
2dee0 20 5e 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75 ^The names retu
2def0 72 6e 65 64 20 61 72 65 20 74 68 65 20 6f 72 69 rned are the ori
2df00 67 69 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 ginal un-aliased
2df10 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a names of the.**
2df20 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 database, table
2df30 2c 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a , and column..**
2df40 0a 2a 2a 20 5e 54 68 65 20 66 69 72 73 74 20 61 .** ^The first a
2df50 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 73 65 rgument to these
2df60 20 69 6e 74 65 72 66 61 63 65 73 20 69 73 20 61 interfaces is a
2df70 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
2df80 6d 65 6e 74 5d 2e 0a 2a 2a 20 5e 54 68 65 73 65 ment]..** ^These
2df90 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 functions retur
2dfa0 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 n information ab
2dfb0 6f 75 74 20 74 68 65 20 4e 74 68 20 72 65 73 75 out the Nth resu
2dfc0 6c 74 20 63 6f 6c 75 6d 6e 20 72 65 74 75 72 6e lt column return
2dfd0 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73 74 61 ed by.** the sta
2dfe0 74 65 6d 65 6e 74 2c 20 77 68 65 72 65 20 4e 20 tement, where N
2dff0 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 66 75 is the second fu
2e000 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e nction argument.
2e010 0a 2a 2a 20 5e 54 68 65 20 6c 65 66 74 2d 6d 6f .** ^The left-mo
2e020 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 63 6f 6c st column is col
2e030 75 6d 6e 20 30 20 66 6f 72 20 74 68 65 73 65 20 umn 0 for these
2e040 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 routines..**.**
2e050 5e 49 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 ^If the Nth colu
2e060 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 mn returned by t
2e070 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 he statement is
2e080 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 an expression or
2e090 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64 .** subquery and
2e0a0 20 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e is not a column
2e0b0 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c value, then all
2e0c0 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 of these functi
2e0d0 6f 6e 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 ons return.** NU
2e0e0 4c 4c 2e 20 20 5e 54 68 65 73 65 20 72 6f 75 74 LL. ^These rout
2e0f0 69 6e 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 ine might also r
2e100 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 eturn NULL if a
2e110 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
2e120 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 n error.** occur
2e130 73 2e 20 20 5e 4f 74 68 65 72 77 69 73 65 2c 20 s. ^Otherwise,
2e140 74 68 65 79 20 72 65 74 75 72 6e 20 74 68 65 20 they return the
2e150 6e 61 6d 65 20 6f 66 20 74 68 65 20 61 74 74 61 name of the atta
2e160 63 68 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 ched database, t
2e170 61 62 6c 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6c 75 able,.** or colu
2e180 6d 6e 20 74 68 61 74 20 71 75 65 72 79 20 72 65 mn that query re
2e190 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 77 61 73 20 sult column was
2e1a0 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 2e 0a extracted from..
2e1b0 2a 2a 0a 2a 2a 20 5e 41 73 20 77 69 74 68 20 61 **.** ^As with a
2e1c0 6c 6c 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 ll other SQLite
2e1d0 41 50 49 73 2c 20 74 68 6f 73 65 20 77 68 6f 73 APIs, those whos
2e1e0 65 20 6e 61 6d 65 73 20 65 6e 64 20 77 69 74 68 e names end with
2e1f0 20 22 31 36 22 20 72 65 74 75 72 6e 0a 2a 2a 20 "16" return.**
2e200 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 73 UTF-16 encoded s
2e210 74 72 69 6e 67 73 20 61 6e 64 20 74 68 65 20 6f trings and the o
2e220 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 72 ther functions r
2e230 65 74 75 72 6e 20 55 54 46 2d 38 2e 0a 2a 2a 0a eturn UTF-8..**.
2e240 2a 2a 20 5e 54 68 65 73 65 20 41 50 49 73 20 61 ** ^These APIs a
2e250 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c re only availabl
2e260 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 e if the library
2e270 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 was compiled wi
2e280 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 th the.** [SQLIT
2e290 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f E_ENABLE_COLUMN_
2e2a0 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65 70 METADATA] C-prep
2e2b0 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 2e rocessor symbol.
2e2c0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 77 6f 20 6f 72 .**.** If two or
2e2d0 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61 more threads ca
2e2e0 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f ll one or more o
2e2f0 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 f these routines
2e300 20 61 67 61 69 6e 73 74 20 74 68 65 20 73 61 6d against the sam
2e310 65 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74 e.** prepared st
2e320 61 74 65 6d 65 6e 74 20 61 6e 64 20 63 6f 6c 75 atement and colu
2e330 6d 6e 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 mn at the same t
2e340 69 6d 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 ime then the res
2e350 75 6c 74 73 20 61 72 65 0a 2a 2a 20 75 6e 64 65 ults are.** unde
2e360 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 fined..**.** If
2e370 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 two or more thre
2e380 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 ads call one or
2e390 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 more.** [sqlite3
2e3a0 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 _column_database
2e3b0 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e 20 6d _name | column m
2e3c0 65 74 61 64 61 74 61 20 69 6e 74 65 72 66 61 63 etadata interfac
2e3d0 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 es].** for the s
2e3e0 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 ame [prepared st
2e3f0 61 74 65 6d 65 6e 74 5d 20 61 6e 64 20 72 65 73 atement] and res
2e400 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 74 ult column.** at
2e410 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 the same time t
2e420 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 hen the results
2e430 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a are undefined..*
2e440 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
2e450 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
2e460 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 _column_database
2e470 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
2e480 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 mt*,int);.SQLITE
2e490 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
2e4a0 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
2e4b0 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 database_name16(
2e4c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e sqlite3_stmt*,in
2e4d0 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 t);.SQLITE_API c
2e4e0 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
2e4f0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f e3_column_table_
2e500 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d name(sqlite3_stm
2e510 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t*,int);.SQLITE_
2e520 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
2e530 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
2e540 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 able_name16(sqli
2e550 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a te3_stmt*,int);.
2e560 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
2e570 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
2e580 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d olumn_origin_nam
2e590 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c e(sqlite3_stmt*,
2e5a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 int);.SQLITE_API
2e5b0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
2e5c0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 ite3_column_orig
2e5d0 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 in_name16(sqlite
2e5e0 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 3_stmt*,int);../
2e5f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
2e600 65 63 6c 61 72 65 64 20 44 61 74 61 74 79 70 65 eclared Datatype
2e610 20 4f 66 20 41 20 51 75 65 72 79 20 52 65 73 75 Of A Query Resu
2e620 6c 74 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 66 lt.**.** ^(The f
2e630 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 irst parameter i
2e640 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 s a [prepared st
2e650 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20 atement]..** If
2e660 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69 this statement i
2e670 73 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 s a [SELECT] sta
2e680 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 4e tement and the N
2e690 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 th column of the
2e6a0 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 72 65 73 .** returned res
2e6b0 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61 74 20 ult set of that
2e6c0 5b 53 45 4c 45 43 54 5d 20 69 73 20 61 20 74 61 [SELECT] is a ta
2e6d0 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e 6f 74 20 ble column (not
2e6e0 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e an.** expression
2e6f0 20 6f 72 20 73 75 62 71 75 65 72 79 29 20 74 68 or subquery) th
2e700 65 6e 20 74 68 65 20 64 65 63 6c 61 72 65 64 20 en the declared
2e710 74 79 70 65 20 6f 66 20 74 68 65 20 74 61 62 6c type of the tabl
2e720 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 72 e.** column is r
2e730 65 74 75 72 6e 65 64 2e 29 5e 20 20 5e 49 66 20 eturned.)^ ^If
2e740 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f the Nth column o
2e750 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 f the result set
2e760 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 is an.** expres
2e770 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 sion or subquery
2e780 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f , then a NULL po
2e790 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 inter is returne
2e7a0 64 2e 0a 2a 2a 20 5e 54 68 65 20 72 65 74 75 72 d..** ^The retur
2e7b0 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c ned string is al
2e7c0 77 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 ways UTF-8 encod
2e7d0 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 28 46 6f 72 20 ed..**.** ^(For
2e7e0 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e 20 74 example, given t
2e7f0 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 he database sche
2e800 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 ma:.**.** CREATE
2e810 20 54 41 42 4c 45 20 74 31 28 63 31 20 56 41 52 TABLE t1(c1 VAR
2e820 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20 61 6e 64 IANT);.**.** and
2e830 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
2e840 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 tatement to be c
2e850 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a 20 53 ompiled:.**.** S
2e860 45 4c 45 43 54 20 63 31 20 2b 20 31 2c 20 63 31 ELECT c1 + 1, c1
2e870 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a 2a 20 FROM t1;.**.**
2e880 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 75 this routine wou
2e890 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 74 ld return the st
2e8a0 72 69 6e 67 20 22 56 41 52 49 41 4e 54 22 20 66 ring "VARIANT" f
2e8b0 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 72 65 or the second re
2e8c0 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 28 sult.** column (
2e8d0 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20 4e 55 4c i==1), and a NUL
2e8e0 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 L pointer for th
2e8f0 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 20 63 e first result c
2e900 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 2e 29 5e 0a olumn (i==0).)^.
2e910 2a 2a 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 75 73 **.** ^SQLite us
2e920 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74 es dynamic run-t
2e930 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 5e 53 6f ime typing. ^So
2e940 20 6a 75 73 74 20 62 65 63 61 75 73 65 20 61 20 just because a
2e950 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 column.** is dec
2e960 6c 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e lared to contain
2e970 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 79 a particular ty
2e980 70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e pe does not mean
2e990 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74 that the.** dat
2e9a0 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74 a stored in that
2e9b0 20 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68 column is of th
2e9c0 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e e declared type.
2e9d0 20 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73 SQLite is.** s
2e9e0 74 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62 trongly typed, b
2e9f0 75 74 20 74 68 65 20 74 79 70 69 6e 67 20 69 73 ut the typing is
2ea00 20 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61 dynamic not sta
2ea10 74 69 63 2e 20 20 5e 54 79 70 65 0a 2a 2a 20 69 tic. ^Type.** i
2ea20 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
2ea30 68 20 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c h individual val
2ea40 75 65 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 ues, not with th
2ea50 65 20 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 e containers.**
2ea60 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f used to hold tho
2ea70 73 65 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 se values..*/.SQ
2ea80 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 LITE_API const c
2ea90 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c har *sqlite3_col
2eaa0 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c umn_decltype(sql
2eab0 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b ite3_stmt*,int);
2eac0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
2ead0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
2eae0 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 column_decltype1
2eaf0 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 6(sqlite3_stmt*,
2eb00 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 int);../*.** CAP
2eb10 49 33 52 45 46 3a 20 45 76 61 6c 75 61 74 65 20 I3REF: Evaluate
2eb20 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 An SQL Statement
2eb30 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 5b .**.** After a [
2eb40 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2eb50 6e 74 5d 20 68 61 73 20 62 65 65 6e 20 70 72 65 nt] has been pre
2eb60 70 61 72 65 64 20 75 73 69 6e 67 20 65 69 74 68 pared using eith
2eb70 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 er.** [sqlite3_p
2eb80 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 repare_v2()] or
2eb90 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
2eba0 31 36 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 16_v2()] or one
2ebb0 6f 66 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a of the legacy.**
2ebc0 20 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c interfaces [sql
2ebd0 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 ite3_prepare()]
2ebe0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 or [sqlite3_prep
2ebf0 61 72 65 31 36 28 29 5d 2c 20 74 68 69 73 20 66 are16()], this f
2ec00 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 unction.** must
2ec10 62 65 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 be called one or
2ec20 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 65 more times to e
2ec30 76 61 6c 75 61 74 65 20 74 68 65 20 73 74 61 74 valuate the stat
2ec40 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ement..**.** The
2ec50 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 details of the
2ec60 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20 behavior of the
2ec70 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 sqlite3_step() i
2ec80 6e 74 65 72 66 61 63 65 20 64 65 70 65 6e 64 0a nterface depend.
2ec90 2a 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 74 68 ** on whether th
2eca0 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 e statement was
2ecb0 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 74 prepared using t
2ecc0 68 65 20 6e 65 77 65 72 20 22 76 32 22 20 69 6e he newer "v2" in
2ecd0 74 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 6c 69 terface.** [sqli
2ece0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
2ecf0 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 ] and [sqlite3_p
2ed00 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f repare16_v2()] o
2ed10 72 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67 61 r the older lega
2ed20 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 cy.** interface
2ed30 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
2ed40 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
2ed50 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20 20 _prepare16()].
2ed60 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 0a 2a The use of the.*
2ed70 2a 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65 72 * new "v2" inter
2ed80 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e face is recommen
2ed90 64 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70 6c ded for new appl
2eda0 69 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68 65 ications but the
2edb0 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 legacy.** inter
2edc0 66 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e face will contin
2edd0 75 65 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 ue to be support
2ede0 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 49 6e 20 74 68 ed..**.** ^In th
2edf0 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 e legacy interfa
2ee00 63 65 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 ce, the return v
2ee10 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 65 69 74 alue will be eit
2ee20 68 65 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 her [SQLITE_BUSY
2ee30 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f ],.** [SQLITE_DO
2ee40 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 NE], [SQLITE_ROW
2ee50 5d 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 ], [SQLITE_ERROR
2ee60 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 ], or [SQLITE_MI
2ee70 53 55 53 45 5d 2e 0a 2a 2a 20 5e 57 69 74 68 20 SUSE]..** ^With
2ee80 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 the "v2" interfa
2ee90 63 65 2c 20 61 6e 79 20 6f 66 20 74 68 65 20 6f ce, any of the o
2eea0 74 68 65 72 20 5b 72 65 73 75 6c 74 20 63 6f 64 ther [result cod
2eeb0 65 73 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e es] or.** [exten
2eec0 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 ded result codes
2eed0 5d 20 6d 69 67 68 74 20 62 65 20 72 65 74 75 72 ] might be retur
2eee0 6e 65 64 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a 0a ned as well..**.
2eef0 2a 2a 20 5e 5b 53 51 4c 49 54 45 5f 42 55 53 59 ** ^[SQLITE_BUSY
2ef00 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 ] means that the
2ef10 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 database engine
2ef20 20 77 61 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 was unable to a
2ef30 63 71 75 69 72 65 20 74 68 65 0a 2a 2a 20 64 61 cquire the.** da
2ef40 74 61 62 61 73 65 20 6c 6f 63 6b 73 20 69 74 20 tabase locks it
2ef50 6e 65 65 64 73 20 74 6f 20 64 6f 20 69 74 73 20 needs to do its
2ef60 6a 6f 62 2e 20 20 5e 49 66 20 74 68 65 20 73 74 job. ^If the st
2ef70 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b 43 4f atement is a [CO
2ef80 4d 4d 49 54 5d 0a 2a 2a 20 6f 72 20 6f 63 63 75 MMIT].** or occu
2ef90 72 73 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e rs outside of an
2efa0 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 explicit transa
2efb0 63 74 69 6f 6e 2c 20 74 68 65 6e 20 79 6f 75 20 ction, then you
2efc0 63 61 6e 20 72 65 74 72 79 20 74 68 65 0a 2a 2a can retry the.**
2efd0 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 66 20 statement. If
2efe0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 the statement is
2eff0 20 6e 6f 74 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 not a [COMMIT]
2f000 61 6e 64 20 6f 63 63 75 72 73 20 77 69 74 68 69 and occurs withi
2f010 6e 20 61 6e 0a 2a 2a 20 65 78 70 6c 69 63 69 74 n an.** explicit
2f020 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 68 65 transaction the
2f030 6e 20 79 6f 75 20 73 68 6f 75 6c 64 20 72 6f 6c n you should rol
2f040 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 lback the transa
2f050 63 74 69 6f 6e 20 62 65 66 6f 72 65 0a 2a 2a 20 ction before.**
2f060 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2a 0a 2a continuing..**.*
2f070 2a 20 5e 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d * ^[SQLITE_DONE]
2f080 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
2f090 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 66 69 statement has fi
2f0a0 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e 67 nished executing
2f0b0 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 .** successfully
2f0c0 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 . sqlite3_step(
2f0d0 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ) should not be
2f0e0 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 20 called again on
2f0f0 74 68 69 73 20 76 69 72 74 75 61 6c 0a 2a 2a 20 this virtual.**
2f100 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75 74 20 machine without
2f110 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20 5b 73 first calling [s
2f120 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 qlite3_reset()]
2f130 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 72 to reset the vir
2f140 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 tual.** machine
2f150 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 back to its init
2f160 69 61 6c 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a ial state..**.**
2f170 20 5e 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 ^If the SQL sta
2f180 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 78 65 tement being exe
2f190 63 75 74 65 64 20 72 65 74 75 72 6e 73 20 61 6e cuted returns an
2f1a0 79 20 64 61 74 61 2c 20 74 68 65 6e 20 5b 53 51 y data, then [SQ
2f1b0 4c 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 69 73 20 LITE_ROW].** is
2f1c0 72 65 74 75 72 6e 65 64 20 65 61 63 68 20 74 69 returned each ti
2f1d0 6d 65 20 61 20 6e 65 77 20 72 6f 77 20 6f 66 20 me a new row of
2f1e0 64 61 74 61 20 69 73 20 72 65 61 64 79 20 66 6f data is ready fo
2f1f0 72 20 70 72 6f 63 65 73 73 69 6e 67 20 62 79 20 r processing by
2f200 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20 54 the.** caller. T
2f210 68 65 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65 he values may be
2f220 20 61 63 63 65 73 73 65 64 20 75 73 69 6e 67 20 accessed using
2f230 74 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 the [column acce
2f240 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a ss functions]..*
2f250 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 * sqlite3_step()
2f260 20 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69 6e is called again
2f270 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 to retrieve the
2f280 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61 74 next row of dat
2f290 61 2e 0a 2a 2a 0a 2a 2a 20 5e 5b 53 51 4c 49 54 a..**.** ^[SQLIT
2f2a0 45 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20 74 E_ERROR] means t
2f2b0 68 61 74 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 hat a run-time e
2f2c0 72 72 6f 72 20 28 73 75 63 68 20 61 73 20 61 20 rror (such as a
2f2d0 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 69 constraint.** vi
2f2e0 6f 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f 63 63 olation) has occ
2f2f0 75 72 72 65 64 2e 20 20 73 71 6c 69 74 65 33 5f urred. sqlite3_
2f300 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f step() should no
2f310 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 t be called agai
2f320 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e 20 n on.** the VM.
2f330 4d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e More information
2f340 20 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 62 79 may be found by
2f350 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 calling [sqlite
2f360 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20 3_errmsg()]..**
2f370 5e 57 69 74 68 20 74 68 65 20 6c 65 67 61 63 79 ^With the legacy
2f380 20 69 6e 74 65 72 66 61 63 65 2c 20 61 20 6d 6f interface, a mo
2f390 72 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f re specific erro
2f3a0 72 20 63 6f 64 65 20 28 66 6f 72 20 65 78 61 6d r code (for exam
2f3b0 70 6c 65 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f ple,.** [SQLITE_
2f3c0 49 4e 54 45 52 52 55 50 54 5d 2c 20 5b 53 51 4c INTERRUPT], [SQL
2f3d0 49 54 45 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 51 ITE_SCHEMA], [SQ
2f3e0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5d 2c 20 61 LITE_CORRUPT], a
2f3f0 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a 2a 2a 20 nd so forth).**
2f400 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 20 can be obtained
2f410 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 by calling [sqli
2f420 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 6e 20 te3_reset()] on
2f430 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 the.** [prepared
2f440 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 5e 49 statement]. ^I
2f450 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 n the "v2" inter
2f460 66 61 63 65 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72 face,.** the mor
2f470 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 e specific error
2f480 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
2f490 64 20 64 69 72 65 63 74 6c 79 20 62 79 20 73 71 d directly by sq
2f4a0 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2a lite3_step()..**
2f4b0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 .** [SQLITE_MISU
2f4c0 53 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 SE] means that t
2f4d0 68 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 he this routine
2f4e0 77 61 73 20 63 61 6c 6c 65 64 20 69 6e 61 70 70 was called inapp
2f4f0 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 20 50 ropriately..** P
2f500 65 72 68 61 70 73 20 69 74 20 77 61 73 20 63 61 erhaps it was ca
2f510 6c 6c 65 64 20 6f 6e 20 61 20 5b 70 72 65 70 61 lled on a [prepa
2f520 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 red statement] t
2f530 68 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 hat has.** alrea
2f540 64 79 20 62 65 65 6e 20 5b 73 71 6c 69 74 65 33 dy been [sqlite3
2f550 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 _finalize | fina
2f560 6c 69 7a 65 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65 lized] or on one
2f570 20 74 68 61 74 20 68 61 64 0a 2a 2a 20 70 72 65 that had.** pre
2f580 76 69 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 64 viously returned
2f590 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
2f5a0 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d or [SQLITE_DONE]
2f5b0 2e 20 20 4f 72 20 69 74 20 63 6f 75 6c 64 0a 2a . Or it could.*
2f5c0 2a 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 * be the case th
2f5d0 61 74 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 at the same data
2f5e0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
2f5f0 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 62 79 is being used by
2f600 20 74 77 6f 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 two or.** more
2f610 74 68 72 65 61 64 73 20 61 74 20 74 68 65 20 73 threads at the s
2f620 61 6d 65 20 6d 6f 6d 65 6e 74 20 69 6e 20 74 69 ame moment in ti
2f630 6d 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6c me..**.** For al
2f640 6c 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 l versions of SQ
2f650 4c 69 74 65 20 75 70 20 74 6f 20 61 6e 64 20 69 Lite up to and i
2f660 6e 63 6c 75 64 69 6e 67 20 33 2e 36 2e 32 33 2e ncluding 3.6.23.
2f670 31 2c 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 1, a call to.**
2f680 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
2f690 5d 20 77 61 73 20 72 65 71 75 69 72 65 64 20 61 ] was required a
2f6a0 66 74 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 fter sqlite3_ste
2f6b0 70 28 29 20 72 65 74 75 72 6e 65 64 20 61 6e 79 p() returned any
2f6c0 74 68 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 20 74 thing.** other t
2f6d0 68 61 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d han [SQLITE_ROW]
2f6e0 20 62 65 66 6f 72 65 20 61 6e 79 20 73 75 62 73 before any subs
2f6f0 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f equent invocatio
2f700 6e 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f n of.** sqlite3_
2f710 73 74 65 70 28 29 2e 20 20 46 61 69 6c 75 72 65 step(). Failure
2f720 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 70 72 to reset the pr
2f730 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
2f740 20 75 73 69 6e 67 20 0a 2a 2a 20 5b 73 71 6c 69 using .** [sqli
2f750 74 65 33 5f 72 65 73 65 74 28 29 5d 20 77 6f 75 te3_reset()] wou
2f760 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 ld result in an
2f770 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 [SQLITE_MISUSE]
2f780 72 65 74 75 72 6e 20 66 72 6f 6d 0a 2a 2a 20 73 return from.** s
2f790 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20 qlite3_step().
2f7a0 42 75 74 20 61 66 74 65 72 20 76 65 72 73 69 6f But after versio
2f7b0 6e 20 33 2e 36 2e 32 33 2e 31 2c 20 73 71 6c 69 n 3.6.23.1, sqli
2f7c0 74 65 33 5f 73 74 65 70 28 29 20 62 65 67 61 6e te3_step() began
2f7d0 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c .** calling [sql
2f7e0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 61 75 ite3_reset()] au
2f7f0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 20 74 tomatically in t
2f800 68 69 73 20 63 69 72 63 75 6d 73 74 61 6e 63 65 his circumstance
2f810 20 72 61 74 68 65 72 0a 2a 2a 20 74 68 61 6e 20 rather.** than
2f820 72 65 74 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54 returning [SQLIT
2f830 45 5f 4d 49 53 55 53 45 5d 2e 20 20 54 68 69 73 E_MISUSE]. This
2f840 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 is not consider
2f850 65 64 20 61 20 63 6f 6d 70 61 74 69 62 69 6c 69 ed a compatibili
2f860 74 79 0a 2a 2a 20 62 72 65 61 6b 20 62 65 63 61 ty.** break beca
2f870 75 73 65 20 61 6e 79 20 61 70 70 6c 69 63 61 74 use any applicat
2f880 69 6f 6e 20 74 68 61 74 20 65 76 65 72 20 72 65 ion that ever re
2f890 63 65 69 76 65 73 20 61 6e 20 53 51 4c 49 54 45 ceives an SQLITE
2f8a0 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 0a 2a 2a _MISUSE error.**
2f8b0 20 69 73 20 62 72 6f 6b 65 6e 20 62 79 20 64 65 is broken by de
2f8c0 66 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65 20 5b finition. The [
2f8d0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
2f8e0 52 45 53 45 54 5d 20 63 6f 6d 70 69 6c 65 2d 74 RESET] compile-t
2f8f0 69 6d 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 63 61 ime option.** ca
2f900 6e 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 73 n be used to res
2f910 74 6f 72 65 20 74 68 65 20 6c 65 67 61 63 79 20 tore the legacy
2f920 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 behavior..**.**
2f930 3c 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72 66 61 <b>Goofy Interfa
2f940 63 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 20 49 6e ce Alert:</b> In
2f950 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 the legacy inte
2f960 72 66 61 63 65 2c 20 74 68 65 20 73 71 6c 69 74 rface, the sqlit
2f970 65 33 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 49 e3_step().** API
2f980 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 always returns
2f990 61 20 67 65 6e 65 72 69 63 20 65 72 72 6f 72 20 a generic error
2f9a0 63 6f 64 65 2c 20 5b 53 51 4c 49 54 45 5f 45 52 code, [SQLITE_ER
2f9b0 52 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 6e 67 20 ROR], following
2f9c0 61 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 6f 74 68 any.** error oth
2f9d0 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f er than [SQLITE_
2f9e0 42 55 53 59 5d 20 61 6e 64 20 5b 53 51 4c 49 54 BUSY] and [SQLIT
2f9f0 45 5f 4d 49 53 55 53 45 5d 2e 20 20 59 6f 75 20 E_MISUSE]. You
2fa00 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 5b 73 71 must call.** [sq
2fa10 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f lite3_reset()] o
2fa20 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c r [sqlite3_final
2fa30 69 7a 65 28 29 5d 20 69 6e 20 6f 72 64 65 72 20 ize()] in order
2fa40 74 6f 20 66 69 6e 64 20 6f 6e 65 20 6f 66 20 74 to find one of t
2fa50 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 5b he.** specific [
2fa60 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 74 68 61 error codes] tha
2fa70 74 20 62 65 74 74 65 72 20 64 65 73 63 72 69 62 t better describ
2fa80 65 73 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2a es the error..**
2fa90 20 57 65 20 61 64 6d 69 74 20 74 68 61 74 20 74 We admit that t
2faa0 68 69 73 20 69 73 20 61 20 67 6f 6f 66 79 20 64 his is a goofy d
2fab0 65 73 69 67 6e 2e 20 20 54 68 65 20 70 72 6f 62 esign. The prob
2fac0 6c 65 6d 20 68 61 73 20 62 65 65 6e 20 66 69 78 lem has been fix
2fad0 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 22 ed.** with the "
2fae0 76 32 22 20 69 6e 74 65 72 66 61 63 65 2e 20 20 v2" interface.
2faf0 49 66 20 79 6f 75 20 70 72 65 70 61 72 65 20 61 If you prepare a
2fb00 6c 6c 20 6f 66 20 79 6f 75 72 20 53 51 4c 20 73 ll of your SQL s
2fb10 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 75 73 69 tatements.** usi
2fb20 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74 ng either [sqlit
2fb30 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
2fb40 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 or [sqlite3_pre
2fb50 70 61 72 65 31 36 5f 76 32 28 29 5d 20 69 6e 73 pare16_v2()] ins
2fb60 74 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6c tead.** of the l
2fb70 65 67 61 63 79 20 5b 73 71 6c 69 74 65 33 5f 70 egacy [sqlite3_p
2fb80 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73 repare()] and [s
2fb90 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
2fba0 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 2c 0a ()] interfaces,.
2fbb0 2a 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f 72 65 ** then the more
2fbc0 20 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72 specific [error
2fbd0 20 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74 75 codes] are retu
2fbe0 72 6e 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a rned directly.**
2fbf0 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70 by sqlite3_step
2fc00 28 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 (). The use of
2fc10 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 the "v2" interfa
2fc20 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 ce is recommende
2fc30 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 d..*/.SQLITE_API
2fc40 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 int sqlite3_ste
2fc50 70 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 p(sqlite3_stmt*)
2fc60 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
2fc70 46 3a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c F: Number of col
2fc80 75 6d 6e 73 20 69 6e 20 61 20 72 65 73 75 6c 74 umns in a result
2fc90 20 73 65 74 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 set.**.** ^The
2fca0 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 sqlite3_data_cou
2fcb0 6e 74 28 50 29 20 69 6e 74 65 72 66 61 63 65 20 nt(P) interface
2fcc0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
2fcd0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
2fce0 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 the.** current
2fcf0 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c row of the resul
2fd00 74 20 73 65 74 20 6f 66 20 5b 70 72 65 70 61 72 t set of [prepar
2fd10 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 50 2e ed statement] P.
2fd20 0a 2a 2a 20 5e 49 66 20 70 72 65 70 61 72 65 64 .** ^If prepared
2fd30 20 73 74 61 74 65 6d 65 6e 74 20 50 20 64 6f 65 statement P doe
2fd40 73 20 6e 6f 74 20 68 61 76 65 20 72 65 73 75 6c s not have resul
2fd50 74 73 20 72 65 61 64 79 20 74 6f 20 72 65 74 75 ts ready to retu
2fd60 72 6e 0a 2a 2a 20 28 76 69 61 20 63 61 6c 6c 73 rn.** (via calls
2fd70 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 to the [sqlite3
2fd80 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 20 7c 20 73 71 _column_int | sq
2fd90 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 2a 28 29 lite3_column_*()
2fda0 5d 20 6f 66 0a 2a 2a 20 69 6e 74 65 72 66 61 63 ] of.** interfac
2fdb0 65 73 29 20 74 68 65 6e 20 73 71 6c 69 74 65 33 es) then sqlite3
2fdc0 5f 64 61 74 61 5f 63 6f 75 6e 74 28 50 29 20 72 _data_count(P) r
2fdd0 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 20 5e 54 68 eturns 0..** ^Th
2fde0 65 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 e sqlite3_data_c
2fdf0 6f 75 6e 74 28 50 29 20 72 6f 75 74 69 6e 65 20 ount(P) routine
2fe00 61 6c 73 6f 20 72 65 74 75 72 6e 73 20 30 20 69 also returns 0 i
2fe10 66 20 50 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f f P is a NULL po
2fe20 69 6e 74 65 72 2e 0a 2a 2a 20 5e 54 68 65 20 73 inter..** ^The s
2fe30 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e qlite3_data_coun
2fe40 74 28 50 29 20 72 6f 75 74 69 6e 65 20 72 65 74 t(P) routine ret
2fe50 75 72 6e 73 20 30 20 69 66 20 74 68 65 20 70 72 urns 0 if the pr
2fe60 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 0a 2a evious call to.*
2fe70 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 5d * [sqlite3_step]
2fe80 28 50 29 20 72 65 74 75 72 6e 65 64 20 5b 53 51 (P) returned [SQ
2fe90 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 20 5e 54 68 LITE_DONE]. ^Th
2fea0 65 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 e sqlite3_data_c
2feb0 6f 75 6e 74 28 50 29 0a 2a 2a 20 77 69 6c 6c 20 ount(P).** will
2fec0 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 return non-zero
2fed0 69 66 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c if previous call
2fee0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 to [sqlite3_ste
2fef0 70 5d 28 50 29 20 72 65 74 75 72 6e 65 64 0a 2a p](P) returned.*
2ff00 2a 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 * [SQLITE_ROW],
2ff10 65 78 63 65 70 74 20 69 6e 20 74 68 65 20 63 61 except in the ca
2ff20 73 65 20 6f 66 20 74 68 65 20 5b 50 52 41 47 4d se of the [PRAGM
2ff30 41 20 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76 61 A incremental_va
2ff40 63 75 75 6d 5d 0a 2a 2a 20 77 68 65 72 65 20 69 cuum].** where i
2ff50 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 t always returns
2ff60 20 7a 65 72 6f 20 73 69 6e 63 65 20 65 61 63 68 zero since each
2ff70 20 73 74 65 70 20 6f 66 20 74 68 61 74 20 6d 75 step of that mu
2ff80 6c 74 69 2d 73 74 65 70 0a 2a 2a 20 70 72 61 67 lti-step.** prag
2ff90 6d 61 20 72 65 74 75 72 6e 73 20 30 20 63 6f 6c ma returns 0 col
2ffa0 75 6d 6e 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a umns of data..**
2ffb0 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
2ffc0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f qlite3_column_co
2ffd0 75 6e 74 28 29 5d 0a 2a 2f 0a 53 51 4c 49 54 45 unt()].*/.SQLITE
2ffe0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2fff0 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 _data_count(sqli
30000 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
30010 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
30020 46 3a 20 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 F: Fundamental D
30030 61 74 61 74 79 70 65 73 0a 2a 2a 20 4b 45 59 57 atatypes.** KEYW
30040 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 54 45 58 ORDS: SQLITE_TEX
30050 54 0a 2a 2a 0a 2a 2a 20 5e 28 45 76 65 72 79 20 T.**.** ^(Every
30060 76 61 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 20 value in SQLite
30070 68 61 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 20 has one of five
30080 66 75 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 61 fundamental data
30090 74 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c types:.**.** <ul
300a0 3e 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 >.** <li> 64-bit
300b0 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a signed integer.
300c0 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 49 ** <li> 64-bit I
300d0 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 EEE floating poi
300e0 6e 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c 69 nt number.** <li
300f0 3e 20 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 3e > string.** <li>
30100 20 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e 55 BLOB.** <li> NU
30110 4c 4c 0a 2a 2a 20 3c 2f 75 6c 3e 29 5e 0a 2a 2a LL.** </ul>)^.**
30120 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 .** These consta
30130 6e 74 73 20 61 72 65 20 63 6f 64 65 73 20 66 6f nts are codes fo
30140 72 20 65 61 63 68 20 6f 66 20 74 68 6f 73 65 20 r each of those
30150 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 types..**.** Not
30160 65 20 74 68 61 74 20 74 68 65 20 53 51 4c 49 54 e that the SQLIT
30170 45 5f 54 45 58 54 20 63 6f 6e 73 74 61 6e 74 20 E_TEXT constant
30180 77 61 73 20 61 6c 73 6f 20 75 73 65 64 20 69 6e was also used in
30190 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 SQLite version
301a0 32 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6d 70 6c 2.** for a compl
301b0 65 74 65 6c 79 20 64 69 66 66 65 72 65 6e 74 20 etely different
301c0 6d 65 61 6e 69 6e 67 2e 20 20 53 6f 66 74 77 61 meaning. Softwa
301d0 72 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 re that links ag
301e0 61 69 6e 73 74 20 62 6f 74 68 0a 2a 2a 20 53 51 ainst both.** SQ
301f0 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32 20 61 Lite version 2 a
30200 6e 64 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f nd SQLite versio
30210 6e 20 33 20 73 68 6f 75 6c 64 20 75 73 65 20 53 n 3 should use S
30220 51 4c 49 54 45 33 5f 54 45 58 54 2c 20 6e 6f 74 QLITE3_TEXT, not
30230 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 58 54 2e .** SQLITE_TEXT.
30240 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
30250 54 45 5f 49 4e 54 45 47 45 52 20 20 31 0a 23 64 TE_INTEGER 1.#d
30260 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 4c 4f efine SQLITE_FLO
30270 41 54 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 AT 2.#define
30280 53 51 4c 49 54 45 5f 42 4c 4f 42 20 20 20 20 20 SQLITE_BLOB
30290 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 4.#define SQLITE
302a0 5f 4e 55 4c 4c 20 20 20 20 20 35 0a 23 69 66 64 _NULL 5.#ifd
302b0 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 ef SQLITE_TEXT.#
302c0 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 undef SQLITE_TE
302d0 58 54 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e XT.#else.# defin
302e0 65 20 53 51 4c 49 54 45 5f 54 45 58 54 20 20 20 e SQLITE_TEXT
302f0 20 20 33 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 3.#endif.#defi
30300 6e 65 20 53 51 4c 49 54 45 33 5f 54 45 58 54 20 ne SQLITE3_TEXT
30310 20 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 3../*.** CAP
30320 49 33 52 45 46 3a 20 52 65 73 75 6c 74 20 56 61 I3REF: Result Va
30330 6c 75 65 73 20 46 72 6f 6d 20 41 20 51 75 65 72 lues From A Quer
30340 79 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b y.** KEYWORDS: {
30350 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 column access fu
30360 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 nctions}.**.** T
30370 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 66 6f hese routines fo
30380 72 6d 20 74 68 65 20 22 72 65 73 75 6c 74 20 73 rm the "result s
30390 65 74 22 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a et" interface..*
303a0 2a 0a 2a 2a 20 5e 54 68 65 73 65 20 72 6f 75 74 *.** ^These rout
303b0 69 6e 65 73 20 72 65 74 75 72 6e 20 69 6e 66 6f ines return info
303c0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 20 rmation about a
303d0 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 single column of
303e0 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
303f0 72 65 73 75 6c 74 20 72 6f 77 20 6f 66 20 61 20 result row of a
30400 71 75 65 72 79 2e 20 20 5e 49 6e 20 65 76 65 72 query. ^In ever
30410 79 20 63 61 73 65 20 74 68 65 20 66 69 72 73 74 y case the first
30420 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 argument is a p
30430 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 ointer.** to the
30440 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
30450 6d 65 6e 74 5d 20 74 68 61 74 20 69 73 20 62 65 ment] that is be
30460 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 28 74 ing evaluated (t
30470 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 he [sqlite3_stmt
30480 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 72 *].** that was r
30490 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b 73 71 eturned from [sq
304a0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
304b0 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 69 74 ()] or one of it
304c0 73 20 76 61 72 69 61 6e 74 73 29 0a 2a 2a 20 61 s variants).** a
304d0 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 nd the second ar
304e0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e gument is the in
304f0 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d dex of the colum
30500 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66 6f n for which info
30510 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c rmation.** shoul
30520 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 5e d be returned. ^
30530 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c The leftmost col
30540 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c umn of the resul
30550 74 20 73 65 74 20 68 61 73 20 74 68 65 20 69 6e t set has the in
30560 64 65 78 20 30 2e 0a 2a 2a 20 5e 54 68 65 20 6e dex 0..** ^The n
30570 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
30580 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 63 in the result c
30590 61 6e 20 62 65 20 64 65 74 65 72 6d 69 6e 65 64 an be determined
305a0 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 using.** [sqlit
305b0 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 e3_column_count(
305c0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 )]..**.** If the
305d0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 64 SQL statement d
305e0 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c oes not currentl
305f0 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c y point to a val
30600 69 64 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 68 id row, or if th
30610 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 e.** column inde
30620 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 x is out of rang
30630 65 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 e, the result is
30640 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 54 undefined..** T
30650 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61 hese routines ma
30660 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 y only be called
30670 20 77 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72 when the most r
30680 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a ecent call to.**
30690 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
306a0 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b ] has returned [
306b0 53 51 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20 SQLITE_ROW] and
306c0 6e 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 neither.** [sqli
306d0 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72 te3_reset()] nor
306e0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
306f0 7a 65 28 29 5d 20 68 61 76 65 20 62 65 65 6e 20 ze()] have been
30700 63 61 6c 6c 65 64 20 73 75 62 73 65 71 75 65 6e called subsequen
30710 74 6c 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f tly..** If any o
30720 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 f these routines
30730 20 61 72 65 20 63 61 6c 6c 65 64 20 61 66 74 65 are called afte
30740 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 r [sqlite3_reset
30750 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 ()] or.** [sqlit
30760 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f e3_finalize()] o
30770 72 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33 r after [sqlite3
30780 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 74 _step()] has ret
30790 75 72 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69 urned.** somethi
307a0 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 ng other than [S
307b0 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20 QLITE_ROW], the
307c0 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 results are unde
307d0 66 69 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71 fined..** If [sq
307e0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 lite3_step()] or
307f0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
30800 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 )] or [sqlite3_f
30810 69 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72 inalize()].** ar
30820 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20 e called from a
30830 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 different thread
30840 20 77 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68 while any of th
30850 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 ese routines.**
30860 61 72 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 65 are pending, the
30870 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 n the results ar
30880 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a e undefined..**.
30890 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f ** ^The sqlite3_
308a0 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f column_type() ro
308b0 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 utine returns th
308c0 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 e.** [SQLITE_INT
308d0 45 47 45 52 20 7c 20 64 61 74 61 74 79 70 65 20 EGER | datatype
308e0 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 69 6e code] for the in
308f0 69 74 69 61 6c 20 64 61 74 61 20 74 79 70 65 0a itial data type.
30900 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ** of the result
30910 20 63 6f 6c 75 6d 6e 2e 20 20 5e 54 68 65 20 72 column. ^The r
30920 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 eturned value is
30930 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f one of [SQLITE_
30940 49 4e 54 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 INTEGER],.** [SQ
30950 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 LITE_FLOAT], [SQ
30960 4c 49 54 45 5f 54 45 58 54 5d 2c 20 5b 53 51 4c LITE_TEXT], [SQL
30970 49 54 45 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 ITE_BLOB], or [S
30980 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 QLITE_NULL]. Th
30990 65 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 e value.** retur
309a0 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 ned by sqlite3_c
309b0 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 olumn_type() is
309c0 6f 6e 6c 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20 only meaningful
309d0 69 66 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 63 6f if no type.** co
309e0 6e 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 6f nversions have o
309f0 63 63 75 72 72 65 64 20 61 73 20 64 65 73 63 72 ccurred as descr
30a00 69 62 65 64 20 62 65 6c 6f 77 2e 20 20 41 66 74 ibed below. Aft
30a10 65 72 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 er a type conver
30a20 73 69 6f 6e 2c 0a 2a 2a 20 74 68 65 20 76 61 6c sion,.** the val
30a30 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 ue returned by s
30a40 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 qlite3_column_ty
30a50 70 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 pe() is undefine
30a60 64 2e 20 20 46 75 74 75 72 65 0a 2a 2a 20 76 65 d. Future.** ve
30a70 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 rsions of SQLite
30a80 20 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 may change the
30a90 62 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 behavior of sqli
30aa0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 te3_column_type(
30ab0 29 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 ).** following a
30ac0 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e type conversion
30ad0 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 74 68 65 20 ..**.** ^If the
30ae0 72 65 73 75 6c 74 20 69 73 20 61 20 42 4c 4f 42 result is a BLOB
30af0 20 6f 72 20 55 54 46 2d 38 20 73 74 72 69 6e 67 or UTF-8 string
30b00 20 74 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65 then the sqlite
30b10 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3_column_bytes()
30b20 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 .** routine retu
30b30 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
30b40 66 20 62 79 74 65 73 20 69 6e 20 74 68 61 74 20 f bytes in that
30b50 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a BLOB or string..
30b60 2a 2a 20 5e 49 66 20 74 68 65 20 72 65 73 75 6c ** ^If the resul
30b70 74 20 69 73 20 61 20 55 54 46 2d 31 36 20 73 74 t is a UTF-16 st
30b80 72 69 6e 67 2c 20 74 68 65 6e 20 73 71 6c 69 74 ring, then sqlit
30b90 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 e3_column_bytes(
30ba0 29 20 63 6f 6e 76 65 72 74 73 0a 2a 2a 20 74 68 ) converts.** th
30bb0 65 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d e string to UTF-
30bc0 38 20 61 6e 64 20 74 68 65 6e 20 72 65 74 75 72 8 and then retur
30bd0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
30be0 20 62 79 74 65 73 2e 0a 2a 2a 20 5e 49 66 20 74 bytes..** ^If t
30bf0 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 6e he result is a n
30c00 75 6d 65 72 69 63 20 76 61 6c 75 65 20 74 68 65 umeric value the
30c10 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e n sqlite3_column
30c20 5f 62 79 74 65 73 28 29 20 75 73 65 73 0a 2a 2a _bytes() uses.**
30c30 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e [sqlite3_snprin
30c40 74 66 28 29 5d 20 74 6f 20 63 6f 6e 76 65 72 74 tf()] to convert
30c50 20 74 68 61 74 20 76 61 6c 75 65 20 74 6f 20 61 that value to a
30c60 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 61 6e UTF-8 string an
30c70 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 d returns.** the
30c80 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
30c90 20 69 6e 20 74 68 61 74 20 73 74 72 69 6e 67 2e in that string.
30ca0 0a 2a 2a 20 5e 49 66 20 74 68 65 20 72 65 73 75 .** ^If the resu
30cb0 6c 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e lt is NULL, then
30cc0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
30cd0 62 79 74 65 73 28 29 20 72 65 74 75 72 6e 73 20 bytes() returns
30ce0 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 zero..**.** ^If
30cf0 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 the result is a
30d00 42 4c 4f 42 20 6f 72 20 55 54 46 2d 31 36 20 73 BLOB or UTF-16 s
30d10 74 72 69 6e 67 20 74 68 65 6e 20 74 68 65 20 73 tring then the s
30d20 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
30d30 74 65 73 31 36 28 29 0a 2a 2a 20 72 6f 75 74 69 tes16().** routi
30d40 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e ne returns the n
30d50 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
30d60 6e 20 74 68 61 74 20 42 4c 4f 42 20 6f 72 20 73 n that BLOB or s
30d70 74 72 69 6e 67 2e 0a 2a 2a 20 5e 49 66 20 74 68 tring..** ^If th
30d80 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 55 54 e result is a UT
30d90 46 2d 38 20 73 74 72 69 6e 67 2c 20 74 68 65 6e F-8 string, then
30da0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
30db0 62 79 74 65 73 31 36 28 29 20 63 6f 6e 76 65 72 bytes16() conver
30dc0 74 73 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 ts.** the string
30dd0 20 74 6f 20 55 54 46 2d 31 36 20 61 6e 64 20 74 to UTF-16 and t
30de0 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 hen returns the
30df0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 2e number of bytes.
30e00 0a 2a 2a 20 5e 49 66 20 74 68 65 20 72 65 73 75 .** ^If the resu
30e10 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 69 63 20 lt is a numeric
30e20 76 61 6c 75 65 20 74 68 65 6e 20 73 71 6c 69 74 value then sqlit
30e30 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 e3_column_bytes1
30e40 36 28 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c 6() uses.** [sql
30e50 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d ite3_snprintf()]
30e60 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 74 to convert that
30e70 20 76 61 6c 75 65 20 74 6f 20 61 20 55 54 46 2d value to a UTF-
30e80 31 36 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65 16 string and re
30e90 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d turns.** the num
30ea0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
30eb0 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 that string..**
30ec0 5e 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 ^If the result i
30ed0 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 71 6c s NULL, then sql
30ee0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
30ef0 73 31 36 28 29 20 72 65 74 75 72 6e 73 20 7a 65 s16() returns ze
30f00 72 6f 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 76 ro..**.** ^The v
30f10 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 62 alues returned b
30f20 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d y [sqlite3_colum
30f30 6e 5f 62 79 74 65 73 28 29 5d 20 61 6e 64 20 0a n_bytes()] and .
30f40 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ** [sqlite3_colu
30f50 6d 6e 5f 62 79 74 65 73 31 36 28 29 5d 20 64 6f mn_bytes16()] do
30f60 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65 not include the
30f70 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 zero terminator
30f80 73 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 s at the end.**
30f90 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20 of the string.
30fa0 5e 46 6f 72 20 63 6c 61 72 69 74 79 3a 20 74 68 ^For clarity: th
30fb0 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 e values returne
30fc0 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 d by.** [sqlite3
30fd0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 5d _column_bytes()]
30fe0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f and [sqlite3_co
30ff0 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 5d 20 lumn_bytes16()]
31000 61 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f are the number o
31010 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68 f.** bytes in th
31020 65 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20 74 68 e string, not th
31030 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 e number of char
31040 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 5e 53 acters..**.** ^S
31050 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 trings returned
31060 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d by sqlite3_colum
31070 6e 5f 74 65 78 74 28 29 20 61 6e 64 20 73 71 6c n_text() and sql
31080 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
31090 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 65 6d 16(),.** even em
310a0 70 74 79 20 73 74 72 69 6e 67 73 2c 20 61 72 65 pty strings, are
310b0 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 72 always zero-ter
310c0 6d 69 6e 61 74 65 64 2e 20 20 5e 54 68 65 20 72 minated. ^The r
310d0 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 66 eturn.** value f
310e0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 rom sqlite3_colu
310f0 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 20 61 20 mn_blob() for a
31100 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 zero-length BLOB
31110 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 is a NULL point
31120 65 72 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 6f er..**.** ^The o
31130 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 bject returned b
31140 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d y [sqlite3_colum
31150 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 61 6e n_value()] is an
31160 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 .** [unprotected
31170 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 sqlite3_value]
31180 6f 62 6a 65 63 74 2e 20 20 41 6e 20 75 6e 70 72 object. An unpr
31190 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
311a0 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 value object.**
311b0 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 may only be used
311c0 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 62 with [sqlite3_b
311d0 69 6e 64 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64 ind_value()] and
311e0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 [sqlite3_result
311f0 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 49 66 _value()]..** If
31200 20 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65 the [unprotecte
31210 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d d sqlite3_value]
31220 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 object returned
31230 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f by.** [sqlite3_
31240 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 column_value()]
31250 69 73 20 75 73 65 64 20 69 6e 20 61 6e 79 20 6f is used in any o
31260 74 68 65 72 20 77 61 79 2c 20 69 6e 63 6c 75 64 ther way, includ
31270 69 6e 67 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 ing calls.** to
31280 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 routines like [s
31290 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
312a0 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 ()], [sqlite3_va
312b0 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 lue_text()],.**
312c0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 or [sqlite3_valu
312d0 65 5f 62 79 74 65 73 28 29 5d 2c 20 74 68 65 6e e_bytes()], then
312e0 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 the behavior is
312f0 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a undefined..**.*
31300 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 * These routines
31310 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76 attempt to conv
31320 65 72 74 20 74 68 65 20 76 61 6c 75 65 20 77 68 ert the value wh
31330 65 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e ere appropriate.
31340 20 20 5e 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c ^For.** exampl
31350 65 2c 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e e, if the intern
31360 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f al representatio
31370 6e 20 69 73 20 46 4c 4f 41 54 20 61 6e 64 20 61 n is FLOAT and a
31380 20 74 65 78 74 20 72 65 73 75 6c 74 0a 2a 2a 20 text result.**
31390 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 5b 73 is requested, [s
313a0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
313b0 29 5d 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 )] is used inter
313c0 6e 61 6c 6c 79 20 74 6f 20 70 65 72 66 6f 72 6d nally to perform
313d0 20 74 68 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 the.** conversi
313e0 6f 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 on automatically
313f0 2e 20 20 5e 28 54 68 65 20 66 6f 6c 6c 6f 77 69 . ^(The followi
31400 6e 67 20 74 61 62 6c 65 20 64 65 74 61 69 6c 73 ng table details
31410 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 the conversions
31420 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 61 70 70 .** that are app
31430 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f lied:.**.** <blo
31440 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 ckquote>.** <tab
31450 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a le border="1">.*
31460 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 6e 74 65 72 * <tr><th> Inter
31470 6e 61 6c 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e nal<br>Type <th>
31480 20 52 65 71 75 65 73 74 65 64 3c 62 72 3e 54 79 Requested<br>Ty
31490 70 65 20 3c 74 68 3e 20 20 43 6f 6e 76 65 72 73 pe <th> Convers
314a0 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 ion.**.** <tr><t
314b0 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e d> NULL <td>
314c0 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 INTEGER <td>
314d0 52 65 73 75 6c 74 20 69 73 20 30 0a 2a 2a 20 3c Result is 0.** <
314e0 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 tr><td> NULL
314f0 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 <td> FLOAT
31500 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30 <td> Result is 0
31510 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 .0.** <tr><td>
31520 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 54 NULL <td> T
31530 45 58 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75 EXT <td> Resu
31540 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 lt is NULL point
31550 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 er.** <tr><td>
31560 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 42 NULL <td> B
31570 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52 65 73 75 LOB <td> Resu
31580 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 lt is NULL point
31590 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 er.** <tr><td> I
315a0 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 46 4c NTEGER <td> FL
315b0 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 OAT <td> Conv
315c0 65 72 74 20 66 72 6f 6d 20 69 6e 74 65 67 65 72 ert from integer
315d0 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 to float.** <tr
315e0 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c ><td> INTEGER <
315f0 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 td> TEXT <t
31600 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 69 d> ASCII renderi
31610 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 ng of the intege
31620 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e r.** <tr><td> IN
31630 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 42 4c TEGER <td> BL
31640 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 OB <td> Same
31650 61 73 20 49 4e 54 45 47 45 52 2d 3e 54 45 58 54 as INTEGER->TEXT
31660 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c .** <tr><td> FL
31670 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 OAT <td> INTEG
31680 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 ER <td> Conver
31690 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20 74 6f 20 t from float to
316a0 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c integer.** <tr><
316b0 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 td> FLOAT <td
316c0 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e > TEXT <td>
316d0 20 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e 67 ASCII rendering
316e0 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 0a 2a 2a of the float.**
316f0 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 <tr><td> FLOAT
31700 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 <td> BLOB
31710 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20 46 <td> Same as F
31720 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 LOAT->TEXT.** <t
31730 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20 r><td> TEXT
31740 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c <td> INTEGER <
31750 74 64 3e 20 55 73 65 20 61 74 6f 69 28 29 0a 2a td> Use atoi().*
31760 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 * <tr><td> TEXT
31770 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 <td> FLOAT
31780 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 66 <td> Use atof
31790 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 ().** <tr><td>
317a0 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 20 42 TEXT <td> B
317b0 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e 6f 20 63 LOB <td> No c
317c0 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 hange.** <tr><td
317d0 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 > BLOB <td>
317e0 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43 INTEGER <td> C
317f0 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20 74 onvert to TEXT t
31800 68 65 6e 20 75 73 65 20 61 74 6f 69 28 29 0a 2a hen use atoi().*
31810 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 * <tr><td> BLOB
31820 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 <td> FLOAT
31830 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 <td> Convert
31840 74 6f 20 54 45 58 54 20 74 68 65 6e 20 75 73 65 to TEXT then use
31850 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c atof().** <tr><
31860 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 td> BLOB <td
31870 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e > TEXT <td>
31880 20 41 64 64 20 61 20 7a 65 72 6f 20 74 65 72 6d Add a zero term
31890 69 6e 61 74 6f 72 20 69 66 20 6e 65 65 64 65 64 inator if needed
318a0 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 .** </table>.**
318b0 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 29 5e 0a </blockquote>)^.
318c0 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 **.** The table
318d0 61 62 6f 76 65 20 6d 61 6b 65 73 20 72 65 66 65 above makes refe
318e0 72 65 6e 63 65 20 74 6f 20 73 74 61 6e 64 61 72 rence to standar
318f0 64 20 43 20 6c 69 62 72 61 72 79 20 66 75 6e 63 d C library func
31900 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a 20 tions atoi().**
31910 61 6e 64 20 61 74 6f 66 28 29 2e 20 20 53 51 4c and atof(). SQL
31920 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 ite does not rea
31930 6c 6c 79 20 75 73 65 20 74 68 65 73 65 20 66 75 lly use these fu
31940 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20 68 61 73 nctions. It has
31950 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 75 69 its.** own equi
31960 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c 20 valent internal
31970 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 20 61 routines. The a
31980 74 6f 69 28 29 20 61 6e 64 20 61 74 6f 66 28 29 toi() and atof()
31990 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75 73 names are.** us
319a0 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ed in the table
319b0 66 6f 72 20 62 72 65 76 69 74 79 20 61 6e 64 20 for brevity and
319c0 62 65 63 61 75 73 65 20 74 68 65 79 20 61 72 65 because they are
319d0 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f 73 familiar to mos
319e0 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d 65 t.** C programme
319f0 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 rs..**.** Note t
31a00 68 61 74 20 77 68 65 6e 20 74 79 70 65 20 63 6f hat when type co
31a10 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63 75 72 2c nversions occur,
31a20 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e pointers return
31a30 65 64 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20 63 ed by prior.** c
31a40 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f alls to sqlite3_
31a50 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 73 column_blob(), s
31a60 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
31a70 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 xt(), and/or.**
31a80 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
31a90 65 78 74 31 36 28 29 20 6d 61 79 20 62 65 20 69 ext16() may be i
31aa0 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 20 54 nvalidated..** T
31ab0 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 ype conversions
31ac0 61 6e 64 20 70 6f 69 6e 74 65 72 20 69 6e 76 61 and pointer inva
31ad0 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 lidations might
31ae0 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 20 occur.** in the
31af0 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73 3a following cases:
31b00 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c .**.** <ul>.** <
31b10 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 li> The initial
31b20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20 42 4c 4f content is a BLO
31b30 42 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f B and sqlite3_co
31b40 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 2a lumn_text() or.*
31b50 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 * sqlite3_c
31b60 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 olumn_text16() i
31b70 73 20 63 61 6c 6c 65 64 2e 20 20 41 20 7a 65 72 s called. A zer
31b80 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 6d 69 67 o-terminator mig
31b90 68 74 0a 2a 2a 20 20 20 20 20 20 6e 65 65 64 20 ht.** need
31ba0 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 to be added to t
31bb0 68 65 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e 0a he string.</li>.
31bc0 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 ** <li> The init
31bd0 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 ial content is U
31be0 54 46 2d 38 20 74 65 78 74 20 61 6e 64 20 73 71 TF-8 text and sq
31bf0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
31c00 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20 20 20 20 es16() or.**
31c10 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e sqlite3_column
31c20 5f 74 65 78 74 31 36 28 29 20 69 73 20 63 61 6c _text16() is cal
31c30 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e led. The conten
31c40 74 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 t must be conver
31c50 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55 ted.** to U
31c60 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c TF-16.</li>.** <
31c70 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 li> The initial
31c80 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 31 content is UTF-1
31c90 36 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69 74 6 text and sqlit
31ca0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 e3_column_bytes(
31cb0 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c ) or.** sql
31cc0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
31cd0 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 () is called. T
31ce0 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 he content must
31cf0 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 be converted.**
31d00 20 20 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c 2f to UTF-8.</
31d10 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a li>.** </ul>.**.
31d20 2a 2a 20 5e 43 6f 6e 76 65 72 73 69 6f 6e 73 20 ** ^Conversions
31d30 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 62 65 between UTF-16be
31d40 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20 61 72 and UTF-16le ar
31d50 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20 69 6e e always done in
31d60 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a 2a 2a place and do.**
31d70 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20 not invalidate
31d80 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 2c a prior pointer,
31d90 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 though of cours
31da0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
31db0 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 74 the buffer.** t
31dc0 68 61 74 20 74 68 65 20 70 72 69 6f 72 20 70 6f hat the prior po
31dd0 69 6e 74 65 72 20 72 65 66 65 72 65 6e 63 65 73 inter references
31de0 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 will have been
31df0 6d 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65 72 modified. Other
31e00 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e kinds.** of con
31e10 76 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e 65 version are done
31e20 20 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20 69 in place when i
31e30 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20 62 t is possible, b
31e40 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 68 65 ut sometimes the
31e50 79 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 6f 73 y.** are not pos
31e60 73 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68 6f sible and in tho
31e70 73 65 20 63 61 73 65 73 20 70 72 69 6f 72 20 70 se cases prior p
31e80 6f 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76 61 ointers are inva
31e90 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 lidated..**.** T
31ea0 68 65 20 73 61 66 65 73 74 20 61 6e 64 20 65 61 he safest and ea
31eb0 73 69 65 73 74 20 74 6f 20 72 65 6d 65 6d 62 65 siest to remembe
31ec0 72 20 70 6f 6c 69 63 79 20 69 73 20 74 6f 20 69 r policy is to i
31ed0 6e 76 6f 6b 65 20 74 68 65 73 65 20 72 6f 75 74 nvoke these rout
31ee0 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f ines.** in one o
31ef0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
31f00 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e ways:.**.** <ul>
31f10 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 .** <li>sqlite3
31f20 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 66 _column_text() f
31f30 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 ollowed by sqlit
31f40 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 e3_column_bytes(
31f50 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 )</li>.** <li>s
31f60 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c qlite3_column_bl
31f70 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 ob() followed by
31f80 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
31f90 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 bytes()</li>.**
31fa0 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c <li>sqlite3_col
31fb0 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f 6c umn_text16() fol
31fc0 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 lowed by sqlite3
31fd0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 _column_bytes16(
31fe0 29 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a )</li>.** </ul>.
31ff0 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 **.** In other w
32000 6f 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c 64 ords, you should
32010 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63 6f call sqlite3_co
32020 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 lumn_text(),.**
32030 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
32040 6c 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 lob(), or sqlite
32050 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
32060 29 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63 65 ) first to force
32070 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 the result.** i
32080 6e 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 20 nto the desired
32090 66 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e 76 format, then inv
320a0 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 oke sqlite3_colu
320b0 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a mn_bytes() or.**
320c0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
320d0 62 79 74 65 73 31 36 28 29 20 74 6f 20 66 69 6e bytes16() to fin
320e0 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 d the size of th
320f0 65 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e 6f e result. Do no
32100 74 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a 20 74 t mix calls.** t
32110 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e o sqlite3_column
32120 5f 74 65 78 74 28 29 20 6f 72 20 73 71 6c 69 74 _text() or sqlit
32130 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 e3_column_blob()
32140 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 0a 2a with calls to.*
32150 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e * sqlite3_column
32160 5f 62 79 74 65 73 31 36 28 29 2c 20 61 6e 64 20 _bytes16(), and
32170 64 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73 do not mix calls
32180 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 to sqlite3_colu
32190 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 77 mn_text16().** w
321a0 69 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c ith calls to sql
321b0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
321c0 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 s()..**.** ^The
321d0 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 pointers returne
321e0 64 20 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69 d are valid unti
321f0 6c 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 l a type convers
32200 69 6f 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a ion occurs as.**
32210 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 described above
32220 2c 20 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69 , or until [sqli
32230 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b te3_step()] or [
32240 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d sqlite3_reset()]
32250 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f or.** [sqlite3_
32260 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63 finalize()] is c
32270 61 6c 6c 65 64 2e 20 20 5e 54 68 65 20 6d 65 6d alled. ^The mem
32280 6f 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74 ory space used t
32290 6f 20 68 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a o hold strings.*
322a0 2a 20 61 6e 64 20 42 4c 4f 42 73 20 69 73 20 66 * and BLOBs is f
322b0 72 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c reed automatical
322c0 6c 79 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f ly. Do <b>not</
322d0 62 3e 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e b> pass the poin
322e0 74 65 72 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a ters returned.**
322f0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
32300 5f 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 _blob()], [sqlit
32310 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 e3_column_text()
32320 5d 2c 20 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20 ], etc. into.**
32330 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
32340 2e 0a 2a 2a 0a 2a 2a 20 5e 28 49 66 20 61 20 6d ..**.** ^(If a m
32350 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
32360 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 error occurs du
32370 72 69 6e 67 20 74 68 65 20 65 76 61 6c 75 61 74 ring the evaluat
32380 69 6f 6e 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66 ion of any.** of
32390 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2c these routines,
323a0 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 a default value
323b0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 is returned. T
323c0 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 he default value
323d0 0a 2a 2a 20 69 73 20 65 69 74 68 65 72 20 74 68 .** is either th
323e0 65 20 69 6e 74 65 67 65 72 20 30 2c 20 74 68 65 e integer 0, the
323f0 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
32400 6e 75 6d 62 65 72 20 30 2e 30 2c 20 6f 72 20 61 number 0.0, or a
32410 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 NULL.** pointer
32420 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 . Subsequent ca
32430 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f lls to [sqlite3_
32440 65 72 72 63 6f 64 65 28 29 5d 20 77 69 6c 6c 20 errcode()] will
32450 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 return.** [SQLIT
32460 45 5f 4e 4f 4d 45 4d 5d 2e 29 5e 0a 2a 2f 0a 53 E_NOMEM].)^.*/.S
32470 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
32480 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f void *sqlite3_co
32490 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 lumn_blob(sqlite
324a0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 3_stmt*, int iCo
324b0 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 l);.SQLITE_API i
324c0 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d nt sqlite3_colum
324d0 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f n_bytes(sqlite3_
324e0 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
324f0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
32500 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
32510 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f bytes16(sqlite3_
32520 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
32530 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 ;.SQLITE_API dou
32540 62 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ble sqlite3_colu
32550 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 mn_double(sqlite
32560 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 3_stmt*, int iCo
32570 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 l);.SQLITE_API i
32580 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d nt sqlite3_colum
32590 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 n_int(sqlite3_st
325a0 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a mt*, int iCol);.
325b0 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
325c0 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e3_int64 sqlite3
325d0 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 _column_int64(sq
325e0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
325f0 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 iCol);.SQLITE_A
32600 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 PI const unsigne
32610 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f d char *sqlite3_
32620 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 column_text(sqli
32630 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 te3_stmt*, int i
32640 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 Col);.SQLITE_API
32650 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
32660 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
32670 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 16(sqlite3_stmt*
32680 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c , int iCol);.SQL
32690 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
326a0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 te3_column_type(
326b0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
326c0 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 nt iCol);.SQLITE
326d0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c _API sqlite3_val
326e0 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ue *sqlite3_colu
326f0 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 mn_value(sqlite3
32700 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
32710 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
32720 45 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 EF: Destroy A Pr
32730 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 epared Statement
32740 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 5e 54 Object.**.** ^T
32750 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c he sqlite3_final
32760 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 ize() function i
32770 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 65 6c 65 s called to dele
32780 74 65 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 te a [prepared s
32790 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 5e 49 tatement]..** ^I
327a0 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e f the most recen
327b0 74 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 t evaluation of
327c0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 65 6e the statement en
327d0 63 6f 75 6e 74 65 72 65 64 20 6e 6f 20 65 72 72 countered no err
327e0 6f 72 73 0a 2a 2a 20 6f 72 20 69 66 20 74 68 65 ors.** or if the
327f0 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 65 statement is ne
32800 76 65 72 20 62 65 65 6e 20 65 76 61 6c 75 61 74 ver been evaluat
32810 65 64 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 ed, then sqlite3
32820 5f 66 69 6e 61 6c 69 7a 65 28 29 20 72 65 74 75 _finalize() retu
32830 72 6e 73 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b rns.** SQLITE_OK
32840 2e 20 20 5e 49 66 20 74 68 65 20 6d 6f 73 74 20 . ^If the most
32850 72 65 63 65 6e 74 20 65 76 61 6c 75 61 74 69 6f recent evaluatio
32860 6e 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 53 n of statement S
32870 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 0a 2a 2a failed, then.**
32880 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a sqlite3_finaliz
32890 65 28 53 29 20 72 65 74 75 72 6e 73 20 74 68 65 e(S) returns the
328a0 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72 appropriate [er
328b0 72 6f 72 20 63 6f 64 65 5d 20 6f 72 0a 2a 2a 20 ror code] or.**
328c0 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 [extended error
328d0 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 code]..**.** ^Th
328e0 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 e sqlite3_finali
328f0 7a 65 28 53 29 20 72 6f 75 74 69 6e 65 20 63 61 ze(S) routine ca
32900 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 n be called at a
32910 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 0a ny point during.
32920 2a 2a 20 74 68 65 20 6c 69 66 65 20 63 79 63 6c ** the life cycl
32930 65 20 6f 66 20 5b 70 72 65 70 61 72 65 64 20 73 e of [prepared s
32940 74 61 74 65 6d 65 6e 74 5d 20 53 3a 0a 2a 2a 20 tatement] S:.**
32950 62 65 66 6f 72 65 20 73 74 61 74 65 6d 65 6e 74 before statement
32960 20 53 20 69 73 20 65 76 65 72 20 65 76 61 6c 75 S is ever evalu
32970 61 74 65 64 2c 20 61 66 74 65 72 0a 2a 2a 20 6f ated, after.** o
32980 6e 65 20 6f 72 20 6d 6f 72 65 20 63 61 6c 6c 73 ne or more calls
32990 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 to [sqlite3_res
329a0 65 74 28 29 5d 2c 20 6f 72 20 61 66 74 65 72 20 et()], or after
329b0 61 6e 79 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 5b any call.** to [
329c0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
329d0 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 regardless of wh
329e0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
329f0 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 0a 2a statement has.*
32a00 2a 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 * completed exec
32a10 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 5e 49 6e ution..**.** ^In
32a20 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 voking sqlite3_f
32a30 69 6e 61 6c 69 7a 65 28 29 20 6f 6e 20 61 20 4e inalize() on a N
32a40 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 61 ULL pointer is a
32a50 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e harmless no-op.
32a60 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 .**.** The appli
32a70 63 61 74 69 6f 6e 20 6d 75 73 74 20 66 69 6e 61 cation must fina
32a80 6c 69 7a 65 20 65 76 65 72 79 20 5b 70 72 65 70 lize every [prep
32a90 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
32aa0 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 in order to avoi
32ab0 64 0a 2a 2a 20 72 65 73 6f 75 72 63 65 20 6c 65 d.** resource le
32ac0 61 6b 73 2e 20 20 49 74 20 69 73 20 61 20 67 72 aks. It is a gr
32ad0 69 65 76 6f 75 73 20 65 72 72 6f 72 20 66 6f 72 ievous error for
32ae0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
32af0 20 74 6f 20 74 72 79 20 74 6f 20 75 73 65 0a 2a to try to use.*
32b00 2a 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61 * a prepared sta
32b10 74 65 6d 65 6e 74 20 61 66 74 65 72 20 69 74 20 tement after it
32b20 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a has been finaliz
32b30 65 64 2e 20 20 41 6e 79 20 75 73 65 20 6f 66 20 ed. Any use of
32b40 61 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 a prepared.** st
32b50 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 69 74 atement after it
32b60 20 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 has been finali
32b70 7a 65 64 20 63 61 6e 20 72 65 73 75 6c 74 20 69 zed can result i
32b80 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 0a n undefined and.
32b90 2a 2a 20 75 6e 64 65 73 69 72 61 62 6c 65 20 62 ** undesirable b
32ba0 65 68 61 76 69 6f 72 20 73 75 63 68 20 61 73 20 ehavior such as
32bb0 73 65 67 66 61 75 6c 74 73 20 61 6e 64 20 68 65 segfaults and he
32bc0 61 70 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a ap corruption..*
32bd0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
32be0 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a sqlite3_finaliz
32bf0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
32c00 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 pStmt);../*.** C
32c10 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 API3REF: Reset A
32c20 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d Prepared Statem
32c30 65 6e 74 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a ent Object.**.**
32c40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
32c50 65 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 et() function is
32c60 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 73 65 74 called to reset
32c70 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 a [prepared sta
32c80 74 65 6d 65 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 63 tement].** objec
32c90 74 20 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e t back to its in
32ca0 69 74 69 61 6c 20 73 74 61 74 65 2c 20 72 65 61 itial state, rea
32cb0 64 79 20 74 6f 20 62 65 20 72 65 2d 65 78 65 63 dy to be re-exec
32cc0 75 74 65 64 2e 0a 2a 2a 20 5e 41 6e 79 20 53 51 uted..** ^Any SQ
32cd0 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61 72 69 L statement vari
32ce0 61 62 6c 65 73 20 74 68 61 74 20 68 61 64 20 76 ables that had v
32cf0 61 6c 75 65 73 20 62 6f 75 6e 64 20 74 6f 20 74 alues bound to t
32d00 68 65 6d 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 hem using.** the
32d10 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 [sqlite3_bind_b
32d20 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 lob | sqlite3_bi
32d30 6e 64 5f 2a 28 29 20 41 50 49 5d 20 72 65 74 61 nd_*() API] reta
32d40 69 6e 20 74 68 65 69 72 20 76 61 6c 75 65 73 2e in their values.
32d50 0a 2a 2a 20 55 73 65 20 5b 73 71 6c 69 74 65 33 .** Use [sqlite3
32d60 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 _clear_bindings(
32d70 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 )] to reset the
32d80 62 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 bindings..**.**
32d90 5e 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 ^The [sqlite3_re
32da0 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63 set(S)] interfac
32db0 65 20 72 65 73 65 74 73 20 74 68 65 20 5b 70 72 e resets the [pr
32dc0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
32dd0 5d 20 53 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 74 ] S.** back to t
32de0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
32df0 69 74 73 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a its program..**.
32e00 2a 2a 20 5e 49 66 20 74 68 65 20 6d 6f 73 74 20 ** ^If the most
32e10 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b recent call to [
32e20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d sqlite3_step(S)]
32e30 20 66 6f 72 20 74 68 65 0a 2a 2a 20 5b 70 72 65 for the.** [pre
32e40 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
32e50 20 53 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c S returned [SQL
32e60 49 54 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c ITE_ROW] or [SQL
32e70 49 54 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 6f 72 ITE_DONE],.** or
32e80 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 if [sqlite3_ste
32e90 70 28 53 29 5d 20 68 61 73 20 6e 65 76 65 72 20 p(S)] has never
32ea0 62 65 66 6f 72 65 20 62 65 65 6e 20 63 61 6c 6c before been call
32eb0 65 64 20 6f 6e 20 53 2c 0a 2a 2a 20 74 68 65 6e ed on S,.** then
32ec0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
32ed0 53 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c S)] returns [SQL
32ee0 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 5e ITE_OK]..**.** ^
32ef0 49 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 If the most rece
32f00 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 nt call to [sqli
32f10 74 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 te3_step(S)] for
32f20 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 the.** [prepare
32f30 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 d statement] S i
32f40 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72 72 6f ndicated an erro
32f50 72 2c 20 74 68 65 6e 0a 2a 2a 20 5b 73 71 6c 69 r, then.** [sqli
32f60 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 72 65 te3_reset(S)] re
32f70 74 75 72 6e 73 20 61 6e 20 61 70 70 72 6f 70 72 turns an appropr
32f80 69 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 iate [error code
32f90 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 5b 73 ]..**.** ^The [s
32fa0 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d qlite3_reset(S)]
32fb0 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 73 20 interface does
32fc0 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 not change the v
32fd0 61 6c 75 65 73 0a 2a 2a 20 6f 66 20 61 6e 79 20 alues.** of any
32fe0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c [sqlite3_bind_bl
32ff0 6f 62 7c 62 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 ob|bindings] on
33000 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 the [prepared st
33010 61 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 atement] S..*/.S
33020 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
33030 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 lite3_reset(sqli
33040 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
33050 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
33060 46 3a 20 43 72 65 61 74 65 20 4f 72 20 52 65 64 F: Create Or Red
33070 65 66 69 6e 65 20 53 51 4c 20 46 75 6e 63 74 69 efine SQL Functi
33080 6f 6e 73 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a ons.** KEYWORDS:
33090 20 7b 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 {function creat
330a0 69 6f 6e 20 72 6f 75 74 69 6e 65 73 7d 0a 2a 2a ion routines}.**
330b0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c KEYWORDS: {appl
330c0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
330d0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 7d 0a 2a 2a SQL function}.**
330e0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c KEYWORDS: {appl
330f0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
33100 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a SQL functions}.*
33110 2a 0a 2a 2a 20 5e 54 68 65 73 65 20 66 75 6e 63 *.** ^These func
33120 74 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76 tions (collectiv
33130 65 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75 ely known as "fu
33140 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 nction creation
33150 72 6f 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72 routines").** ar
33160 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 53 51 e used to add SQ
33170 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 L functions or a
33180 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f 20 ggregates or to
33190 72 65 64 65 66 69 6e 65 20 74 68 65 20 62 65 68 redefine the beh
331a0 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73 avior.** of exis
331b0 74 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f ting SQL functio
331c0 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 ns or aggregates
331d0 2e 20 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 . The only diff
331e0 65 72 65 6e 63 65 73 20 62 65 74 77 65 65 6e 0a erences between.
331f0 2a 2a 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 ** these routine
33200 73 20 61 72 65 20 74 68 65 20 74 65 78 74 20 65 s are the text e
33210 6e 63 6f 64 69 6e 67 20 65 78 70 65 63 74 65 64 ncoding expected
33220 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 65 63 6f for.** the seco
33230 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 74 68 nd parameter (th
33240 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 e name of the fu
33250 6e 63 74 69 6f 6e 20 62 65 69 6e 67 20 63 72 65 nction being cre
33260 61 74 65 64 29 0a 2a 2a 20 61 6e 64 20 74 68 65 ated).** and the
33270 20 70 72 65 73 65 6e 63 65 20 6f 72 20 61 62 73 presence or abs
33280 65 6e 63 65 20 6f 66 20 61 20 64 65 73 74 72 75 ence of a destru
33290 63 74 6f 72 20 63 61 6c 6c 62 61 63 6b 20 66 6f ctor callback fo
332a0 72 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 r.** the applica
332b0 74 69 6f 6e 20 64 61 74 61 20 70 6f 69 6e 74 65 tion data pointe
332c0 72 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 r..**.** ^The fi
332d0 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 rst parameter is
332e0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
332f0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f 20 77 68 onnection] to wh
33300 69 63 68 20 74 68 65 20 53 51 4c 0a 2a 2a 20 66 ich the SQL.** f
33310 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 unction is to be
33320 20 61 64 64 65 64 2e 20 20 5e 49 66 20 61 6e 20 added. ^If an
33330 61 70 70 6c 69 63 61 74 69 6f 6e 20 75 73 65 73 application uses
33340 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 more than one d
33350 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 atabase.** conne
33360 63 74 69 6f 6e 20 74 68 65 6e 20 61 70 70 6c 69 ction then appli
33370 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 cation-defined S
33380 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73 QL functions mus
33390 74 20 62 65 20 61 64 64 65 64 0a 2a 2a 20 74 6f t be added.** to
333a0 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 each database c
333b0 6f 6e 6e 65 63 74 69 6f 6e 20 73 65 70 61 72 61 onnection separa
333c0 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 tely..**.** ^The
333d0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
333e0 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 r is the name of
333f0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
33400 6e 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 n to be created
33410 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e or.** redefined.
33420 20 20 5e 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 ^The length of
33430 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d the name is lim
33440 69 74 65 64 20 74 6f 20 32 35 35 20 62 79 74 65 ited to 255 byte
33450 73 20 69 6e 20 61 20 55 54 46 2d 38 0a 2a 2a 20 s in a UTF-8.**
33460 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 representation,
33470 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65 exclusive of the
33480 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 zero-terminator
33490 2e 20 20 5e 4e 6f 74 65 20 74 68 61 74 20 74 68 . ^Note that th
334a0 65 20 6e 61 6d 65 0a 2a 2a 20 6c 65 6e 67 74 68 e name.** length
334b0 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 55 54 46 limit is in UTF
334c0 2d 38 20 62 79 74 65 73 2c 20 6e 6f 74 20 63 68 -8 bytes, not ch
334d0 61 72 61 63 74 65 72 73 20 6e 6f 72 20 55 54 46 aracters nor UTF
334e0 2d 31 36 20 62 79 74 65 73 2e 20 20 0a 2a 2a 20 -16 bytes. .**
334f0 5e 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 ^Any attempt to
33500 63 72 65 61 74 65 20 61 20 66 75 6e 63 74 69 6f create a functio
33510 6e 20 77 69 74 68 20 61 20 6c 6f 6e 67 65 72 20 n with a longer
33520 6e 61 6d 65 0a 2a 2a 20 77 69 6c 6c 20 72 65 73 name.** will res
33530 75 6c 74 20 69 6e 20 5b 53 51 4c 49 54 45 5f 4d ult in [SQLITE_M
33540 49 53 55 53 45 5d 20 62 65 69 6e 67 20 72 65 74 ISUSE] being ret
33550 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 urned..**.** ^Th
33560 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 e third paramete
33570 72 20 28 6e 41 72 67 29 0a 2a 2a 20 69 73 20 74 r (nArg).** is t
33580 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 he number of arg
33590 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 20 uments that the
335a0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a SQL function or.
335b0 2a 2a 20 61 67 67 72 65 67 61 74 65 20 74 61 6b ** aggregate tak
335c0 65 73 2e 20 5e 49 66 20 74 68 69 73 20 70 61 72 es. ^If this par
335d0 61 6d 65 74 65 72 20 69 73 20 2d 31 2c 20 74 68 ameter is -1, th
335e0 65 6e 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 en the SQL funct
335f0 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 ion or.** aggreg
33600 61 74 65 20 6d 61 79 20 74 61 6b 65 20 61 6e 79 ate may take any
33610 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d number of argum
33620 65 6e 74 73 20 62 65 74 77 65 65 6e 20 30 20 61 ents between 0 a
33630 6e 64 20 74 68 65 20 6c 69 6d 69 74 0a 2a 2a 20 nd the limit.**
33640 73 65 74 20 62 79 20 5b 73 71 6c 69 74 65 33 5f set by [sqlite3_
33650 6c 69 6d 69 74 5d 28 5b 53 51 4c 49 54 45 5f 4c limit]([SQLITE_L
33660 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 IMIT_FUNCTION_AR
33670 47 5d 29 2e 20 20 49 66 20 74 68 65 20 74 68 69 G]). If the thi
33680 72 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 rd.** parameter
33690 69 73 20 6c 65 73 73 20 74 68 61 6e 20 2d 31 20 is less than -1
336a0 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 or greater than
336b0 31 32 37 20 74 68 65 6e 20 74 68 65 20 62 65 68 127 then the beh
336c0 61 76 69 6f 72 20 69 73 0a 2a 2a 20 75 6e 64 65 avior is.** unde
336d0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 fined..**.** ^Th
336e0 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 e fourth paramet
336f0 65 72 2c 20 65 54 65 78 74 52 65 70 2c 20 73 70 er, eTextRep, sp
33700 65 63 69 66 69 65 73 20 77 68 61 74 0a 2a 2a 20 ecifies what.**
33710 5b 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 20 74 [SQLITE_UTF8 | t
33720 65 78 74 20 65 6e 63 6f 64 69 6e 67 5d 20 74 68 ext encoding] th
33730 69 73 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 is SQL function
33740 70 72 65 66 65 72 73 20 66 6f 72 0a 2a 2a 20 69 prefers for.** i
33750 74 73 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 ts parameters.
33760 45 76 65 72 79 20 53 51 4c 20 66 75 6e 63 74 69 Every SQL functi
33770 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f on implementatio
33780 6e 20 6d 75 73 74 20 62 65 20 61 62 6c 65 20 74 n must be able t
33790 6f 20 77 6f 72 6b 0a 2a 2a 20 77 69 74 68 20 55 o work.** with U
337a0 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c 20 TF-8, UTF-16le,
337b0 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 75 or UTF-16be. Bu
337c0 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 t some implement
337d0 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a 2a ations may be.**
337e0 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 more efficient
337f0 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 6e with one encodin
33800 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e 20 g than another.
33810 20 5e 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e ^An application
33820 20 6d 61 79 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 may.** invoke s
33830 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
33840 6e 63 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69 nction() or sqli
33850 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
33860 69 6f 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c 65 ion16() multiple
33870 0a 2a 2a 20 74 69 6d 65 73 20 77 69 74 68 20 74 .** times with t
33880 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e he same function
33890 20 62 75 74 20 77 69 74 68 20 64 69 66 66 65 72 but with differ
338a0 65 6e 74 20 76 61 6c 75 65 73 20 6f 66 20 65 54 ent values of eT
338b0 65 78 74 52 65 70 2e 0a 2a 2a 20 5e 57 68 65 6e extRep..** ^When
338c0 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d multiple implem
338d0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 entations of the
338e0 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 same function a
338f0 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 53 51 re available, SQ
33900 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 Lite.** will pic
33910 6b 20 74 68 65 20 6f 6e 65 20 74 68 61 74 20 69 k the one that i
33920 6e 76 6f 6c 76 65 73 20 74 68 65 20 6c 65 61 73 nvolves the leas
33930 74 20 61 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61 t amount of data
33940 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 conversion..**
33950 49 66 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 If there is only
33960 20 61 20 73 69 6e 67 6c 65 20 69 6d 70 6c 65 6d a single implem
33970 65 6e 74 61 74 69 6f 6e 20 77 68 69 63 68 20 64 entation which d
33980 6f 65 73 20 6e 6f 74 20 63 61 72 65 20 77 68 61 oes not care wha
33990 74 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 t text.** encodi
339a0 6e 67 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e ng is used, then
339b0 20 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 the fourth argu
339c0 6d 65 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 5b ment should be [
339d0 53 51 4c 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a SQLITE_ANY]..**.
339e0 2a 2a 20 5e 28 54 68 65 20 66 69 66 74 68 20 70 ** ^(The fifth p
339f0 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 61 arameter is an a
33a00 72 62 69 74 72 61 72 79 20 70 6f 69 6e 74 65 72 rbitrary pointer
33a10 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 . The implement
33a20 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 ation of the.**
33a30 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 67 61 69 function can gai
33a40 6e 20 61 63 63 65 73 73 20 74 6f 20 74 68 69 73 n access to this
33a50 20 70 6f 69 6e 74 65 72 20 75 73 69 6e 67 20 5b pointer using [
33a60 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 sqlite3_user_dat
33a70 61 28 29 5d 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 54 a()].)^.**.** ^T
33a80 68 65 20 73 69 78 74 68 2c 20 73 65 76 65 6e 74 he sixth, sevent
33a90 68 20 61 6e 64 20 65 69 67 68 74 68 20 70 61 72 h and eighth par
33aa0 61 6d 65 74 65 72 73 2c 20 78 46 75 6e 63 2c 20 ameters, xFunc,
33ab0 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 6c xStep and xFinal
33ac0 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 , are.** pointer
33ad0 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61 67 65 20 s to C-language
33ae0 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
33af0 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c mplement the SQL
33b00 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 function or.**
33b10 61 67 67 72 65 67 61 74 65 2e 20 5e 41 20 73 63 aggregate. ^A sc
33b20 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f alar SQL functio
33b30 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 6d n requires an im
33b40 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
33b50 74 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63 61 6c the xFunc.** cal
33b60 6c 62 61 63 6b 20 6f 6e 6c 79 3b 20 4e 55 4c 4c lback only; NULL
33b70 20 70 6f 69 6e 74 65 72 73 20 6d 75 73 74 20 62 pointers must b
33b80 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
33b90 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 6c xStep and xFinal
33ba0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 2e 20 .** parameters.
33bb0 5e 41 6e 20 61 67 67 72 65 67 61 74 65 20 53 51 ^An aggregate SQ
33bc0 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 L function requi
33bd0 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 res an implement
33be0 61 74 69 6f 6e 20 6f 66 20 78 53 74 65 70 0a 2a ation of xStep.*
33bf0 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 61 6e 64 * and xFinal and
33c00 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 6d 75 NULL pointer mu
33c10 73 74 20 62 65 20 70 61 73 73 65 64 20 66 6f 72 st be passed for
33c20 20 78 46 75 6e 63 2e 20 5e 54 6f 20 64 65 6c 65 xFunc. ^To dele
33c30 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a te an existing.*
33c40 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f * SQL function o
33c50 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61 73 r aggregate, pas
33c60 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20 s NULL pointers
33c70 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 20 66 75 for all three fu
33c80 6e 63 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 62 61 nction.** callba
33c90 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 5e 28 49 66 20 cks..**.** ^(If
33ca0 74 68 65 20 6e 69 6e 74 68 20 70 61 72 61 6d 65 the ninth parame
33cb0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 ter to sqlite3_c
33cc0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 5f 76 reate_function_v
33cd0 32 28 29 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 2() is not NULL,
33ce0 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 64 .** then it is d
33cf0 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 estructor for th
33d00 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 61 e application da
33d10 74 61 20 70 6f 69 6e 74 65 72 2e 20 0a 2a 2a 20 ta pointer. .**
33d20 54 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 69 The destructor i
33d30 73 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 74 s invoked when t
33d40 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 he function is d
33d50 65 6c 65 74 65 64 2c 20 65 69 74 68 65 72 20 62 eleted, either b
33d60 79 20 62 65 69 6e 67 0a 2a 2a 20 6f 76 65 72 6c y being.** overl
33d70 6f 61 64 65 64 20 6f 72 20 77 68 65 6e 20 74 68 oaded or when th
33d80 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
33d90 63 74 69 6f 6e 20 63 6c 6f 73 65 73 2e 29 5e 0a ction closes.)^.
33da0 2a 2a 20 5e 54 68 65 20 64 65 73 74 72 75 63 74 ** ^The destruct
33db0 6f 72 20 69 73 20 61 6c 73 6f 20 69 6e 76 6f 6b or is also invok
33dc0 65 64 20 69 66 20 74 68 65 20 63 61 6c 6c 20 74 ed if the call t
33dd0 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 o.** sqlite3_cre
33de0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 5f 76 32 28 ate_function_v2(
33df0 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 5e 57 68 65 ) fails..** ^Whe
33e00 6e 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 n the destructor
33e10 20 63 61 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 callback of the
33e20 20 74 65 6e 74 68 20 70 61 72 61 6d 65 74 65 72 tenth parameter
33e30 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 69 74 0a is invoked, it.
33e40 2a 2a 20 69 73 20 70 61 73 73 65 64 20 61 20 73 ** is passed a s
33e50 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 ingle argument w
33e60 68 69 63 68 20 69 73 20 61 20 63 6f 70 79 20 6f hich is a copy o
33e70 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f f the applicatio
33e80 6e 20 64 61 74 61 20 0a 2a 2a 20 70 6f 69 6e 74 n data .** point
33e90 65 72 20 77 68 69 63 68 20 77 61 73 20 74 68 65 er which was the
33ea0 20 66 69 66 74 68 20 70 61 72 61 6d 65 74 65 72 fifth parameter
33eb0 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 61 to sqlite3_crea
33ec0 74 65 5f 66 75 6e 63 74 69 6f 6e 5f 76 32 28 29 te_function_v2()
33ed0 2e 0a 2a 2a 0a 2a 2a 20 5e 49 74 20 69 73 20 70 ..**.** ^It is p
33ee0 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 67 69 ermitted to regi
33ef0 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 69 6d ster multiple im
33f00 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 plementations of
33f10 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 75 6e the same.** fun
33f20 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68 65 20 ctions with the
33f30 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 69 same name but wi
33f40 74 68 20 65 69 74 68 65 72 20 64 69 66 66 65 72 th either differ
33f50 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 0a 2a ing numbers of.*
33f60 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 64 * arguments or d
33f70 69 66 66 65 72 69 6e 67 20 70 72 65 66 65 72 72 iffering preferr
33f80 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 ed text encoding
33f90 73 2e 20 20 5e 53 51 4c 69 74 65 20 77 69 6c 6c s. ^SQLite will
33fa0 20 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 6c use.** the impl
33fb0 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 ementation that
33fc0 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74 most closely mat
33fd0 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e 20 ches the way in
33fe0 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c which the.** SQL
33ff0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
34000 64 2e 20 20 5e 41 20 66 75 6e 63 74 69 6f 6e 20 d. ^A function
34010 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 implementation w
34020 69 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 ith a non-negati
34030 76 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d ve.** nArg param
34040 65 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72 eter is a better
34050 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 match than a fu
34060 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 nction implement
34070 61 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 ation with.** a
34080 6e 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 negative nArg.
34090 5e 41 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 ^A function wher
340a0 65 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20 e the preferred
340b0 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a text encoding.**
340c0 20 6d 61 74 63 68 65 73 20 74 68 65 20 64 61 74 matches the dat
340d0 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 abase encoding i
340e0 73 20 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 s a better.** ma
340f0 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 tch than a funct
34100 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e ion where the en
34110 63 6f 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 coding is differ
34120 65 6e 74 2e 20 20 0a 2a 2a 20 5e 41 20 66 75 6e ent. .** ^A fun
34130 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 ction where the
34140 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 encoding differe
34150 6e 63 65 20 69 73 20 62 65 74 77 65 65 6e 20 55 nce is between U
34160 54 46 31 36 6c 65 20 61 6e 64 20 55 54 46 31 36 TF16le and UTF16
34170 62 65 0a 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 be.** is a close
34180 72 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 r match than a f
34190 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 unction where th
341a0 65 20 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 e encoding diffe
341b0 72 65 6e 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 rence is.** betw
341c0 65 65 6e 20 55 54 46 38 20 61 6e 64 20 55 54 46 een UTF8 and UTF
341d0 31 36 2e 0a 2a 2a 0a 2a 2a 20 5e 42 75 69 6c 74 16..**.** ^Built
341e0 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 -in functions ma
341f0 79 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 20 y be overloaded
34200 62 79 20 6e 65 77 20 61 70 70 6c 69 63 61 74 69 by new applicati
34210 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on-defined funct
34220 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 5e 41 6e 20 ions..**.** ^An
34230 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
34240 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 ned function is
34250 70 65 72 6d 69 74 74 65 64 20 74 6f 20 63 61 6c permitted to cal
34260 6c 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c 69 74 l other.** SQLit
34270 65 20 69 6e 74 65 72 66 61 63 65 73 2e 20 20 48 e interfaces. H
34280 6f 77 65 76 65 72 2c 20 73 75 63 68 20 63 61 6c owever, such cal
34290 6c 73 20 6d 75 73 74 20 6e 6f 74 0a 2a 2a 20 63 ls must not.** c
342a0 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 73 lose the databas
342b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6e 6f 72 e connection nor
342c0 20 66 69 6e 61 6c 69 7a 65 20 6f 72 20 72 65 73 finalize or res
342d0 65 74 20 74 68 65 20 70 72 65 70 61 72 65 64 0a et the prepared.
342e0 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 ** statement in
342f0 77 68 69 63 68 20 74 68 65 20 66 75 6e 63 74 69 which the functi
34300 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a on is running..*
34310 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
34320 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
34330 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 function(. sqli
34340 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 te3 *db,. const
34350 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e char *zFunction
34360 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 Name,. int nArg
34370 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 ,. int eTextRep
34380 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a ,. void *pApp,.
34390 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 void (*xFunc)(
343a0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
343b0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c ,int,sqlite3_val
343c0 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a ue**),. void (*
343d0 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 xStep)(sqlite3_c
343e0 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 ontext*,int,sqli
343f0 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 te3_value**),.
34400 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 void (*xFinal)(s
34410 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 qlite3_context*)
34420 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
34430 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
34440 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20 e_function16(.
34450 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 sqlite3 *db,. c
34460 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63 onst void *zFunc
34470 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 tionName,. int
34480 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 nArg,. int eTex
34490 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 tRep,. void *pA
344a0 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 pp,. void (*xFu
344b0 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 nc)(sqlite3_cont
344c0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 ext*,int,sqlite3
344d0 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 _value**),. voi
344e0 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 d (*xStep)(sqlit
344f0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c e3_context*,int,
34500 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 sqlite3_value**)
34510 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 ,. void (*xFina
34520 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 l)(sqlite3_conte
34530 78 74 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 xt*).);.SQLITE_A
34540 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
34550 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 5f 76 reate_function_v
34560 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2(. sqlite3 *db
34570 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ,. const char *
34580 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 zFunctionName,.
34590 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 int nArg,. int
345a0 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 eTextRep,. voi
345b0 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 d *pApp,. void
345c0 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 (*xFunc)(sqlite3
345d0 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 _context*,int,sq
345e0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a lite3_value**),.
345f0 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 void (*xStep)(
34600 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
34610 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c ,int,sqlite3_val
34620 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a ue**),. void (*
34630 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f xFinal)(sqlite3_
34640 63 6f 6e 74 65 78 74 2a 29 2c 0a 20 20 76 6f 69 context*),. voi
34650 64 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 d(*xDestroy)(voi
34660 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 d*).);../*.** CA
34670 50 49 33 52 45 46 3a 20 54 65 78 74 20 45 6e 63 PI3REF: Text Enc
34680 6f 64 69 6e 67 73 0a 2a 2a 0a 2a 2a 20 54 68 65 odings.**.** The
34690 73 65 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 se constant defi
346a0 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 ne integer codes
346b0 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 20 that represent
346c0 74 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74 the various.** t
346d0 65 78 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75 ext encodings su
346e0 70 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 pported by SQLit
346f0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 e..*/.#define SQ
34700 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 LITE_UTF8
34710 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 1.#define SQ
34720 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 LITE_UTF16LE
34730 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 2.#define SQ
34740 4c 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 LITE_UTF16BE
34750 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 3.#define SQ
34760 4c 49 54 45 5f 55 54 46 31 36 20 20 20 20 20 20 LITE_UTF16
34770 20 20 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20 4 /* Use
34780 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 native byte orde
34790 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c r */.#define SQL
347a0 49 54 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20 ITE_ANY
347b0 20 20 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74 5 /* sqlit
347c0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
347d0 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 on only */.#defi
347e0 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f ne SQLITE_UTF16_
347f0 41 4c 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a ALIGNED 8 /*
34800 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
34810 63 6f 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a collation only *
34820 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 /../*.** CAPI3RE
34830 46 3a 20 44 65 70 72 65 63 61 74 65 64 20 46 75 F: Deprecated Fu
34840 6e 63 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45 nctions.** DEPRE
34850 43 41 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73 CATED.**.** Thes
34860 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 e functions are
34870 5b 64 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49 [deprecated]. I
34880 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 n order to maint
34890 61 69 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 ain.** backwards
348a0 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 compatibility w
348b0 69 74 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20 ith older code,
348c0 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 these functions
348d0 63 6f 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20 continue .** to
348e0 62 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48 be supported. H
348f0 6f 77 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c owever, new appl
34900 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 ications should
34910 61 76 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65 avoid.** the use
34920 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 of these functi
34930 6f 6e 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e ons. To help en
34940 63 6f 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74 courage people t
34950 6f 20 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67 o avoid.** using
34960 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 these functions
34970 2c 20 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69 , we are not goi
34980 6e 67 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77 ng to tell you w
34990 68 61 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a hat they do..*/.
349a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
349b0 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53 MIT_DEPRECATED.S
349c0 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 QLITE_API SQLITE
349d0 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 _DEPRECATED int
349e0 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 sqlite3_aggregat
349f0 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f e_count(sqlite3_
34a00 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 context*);.SQLIT
34a10 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 E_API SQLITE_DEP
34a20 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 RECATED int sqli
34a30 74 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 te3_expired(sqli
34a40 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 te3_stmt*);.SQLI
34a50 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 TE_API SQLITE_DE
34a60 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c PRECATED int sql
34a70 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 ite3_transfer_bi
34a80 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 ndings(sqlite3_s
34a90 74 6d 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 tmt*, sqlite3_st
34aa0 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 mt*);.SQLITE_API
34ab0 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 SQLITE_DEPRECAT
34ac0 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 ED int sqlite3_g
34ad0 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f lobal_recover(vo
34ae0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 id);.SQLITE_API
34af0 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 SQLITE_DEPRECATE
34b00 44 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 D void sqlite3_t
34b10 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f hread_cleanup(vo
34b20 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 id);.SQLITE_API
34b30 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 SQLITE_DEPRECATE
34b40 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 D int sqlite3_me
34b50 6d 6f 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28 mory_alarm(void(
34b60 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 *)(void*,sqlite3
34b70 5f 69 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64 _int64,int),void
34b80 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 *,sqlite3_int64)
34b90 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
34ba0 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e CAPI3REF: Obtain
34bb0 69 6e 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e ing SQL Function
34bc0 20 50 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65 Parameter Value
34bd0 73 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c 61 s.**.** The C-la
34be0 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65 6e 74 nguage implement
34bf0 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66 75 6e ation of SQL fun
34c00 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 ctions and aggre
34c10 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20 74 68 gates uses.** th
34c20 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65 72 66 is set of interf
34c30 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 ace routines to
34c40 61 63 63 65 73 73 20 74 68 65 20 70 61 72 61 6d access the param
34c50 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e 0a 2a eter values on.*
34c60 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 6f * the function o
34c70 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a r aggregate..**.
34c80 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28 66 6f ** The xFunc (fo
34c90 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f r scalar functio
34ca0 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28 66 6f ns) or xStep (fo
34cb0 72 20 61 67 67 72 65 67 61 74 65 73 29 20 70 61 r aggregates) pa
34cc0 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 5b rameters.** to [
34cd0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
34ce0 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b unction()] and [
34cf0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
34d00 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20 unction16()].**
34d10 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63 6b 73 define callbacks
34d20 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 that implement
34d30 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e the SQL function
34d40 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 s and aggregates
34d50 2e 0a 2a 2a 20 54 68 65 20 33 72 64 20 70 61 72 ..** The 3rd par
34d60 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73 65 20 ameter to these
34d70 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61 6e 20 callbacks is an
34d80 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 array of pointer
34d90 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63 74 s to.** [protect
34da0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
34db0 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 72 ] objects. Ther
34dc0 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74 65 e is one [sqlite
34dd0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 3_value] object
34de0 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61 72 61 for.** each para
34df0 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53 51 4c meter to the SQL
34e00 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 function. Thes
34e10 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 e routines are u
34e20 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61 63 sed to.** extrac
34e30 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 t values from th
34e40 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 e [sqlite3_value
34e50 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a ] objects..**.**
34e60 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
34e70 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68 20 5b work only with [
34e80 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
34e90 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 3_value] objects
34ea0 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 74 ..** Any attempt
34eb0 20 74 6f 20 75 73 65 20 74 68 65 73 65 20 72 6f to use these ro
34ec0 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b 75 6e utines on an [un
34ed0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
34ee0 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a 65 3_value].** obje
34ef0 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e ct results in un
34f00 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 defined behavior
34f10 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 73 65 20 72 ..**.** ^These r
34f20 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75 73 outines work jus
34f30 74 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72 65 t like the corre
34f40 73 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e sponding [column
34f50 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e access function
34f60 73 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 s].** except tha
34f70 74 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 t these routine
34f80 73 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65 20 s take a single
34f90 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 [protected sqlit
34fa0 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
34fb0 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 .** pointer inst
34fc0 65 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74 65 ead of a [sqlite
34fd0 33 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 72 3_stmt*] pointer
34fe0 20 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72 20 and an integer
34ff0 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a column number..*
35000 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 *.** ^The sqlite
35010 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 3_value_text16()
35020 20 69 6e 74 65 72 66 61 63 65 20 65 78 74 72 61 interface extra
35030 63 74 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 cts a UTF-16 str
35040 69 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 ing.** in the na
35050 74 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20 tive byte-order
35060 6f 66 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68 of the host mach
35070 69 6e 65 2e 20 20 5e 54 68 65 0a 2a 2a 20 73 71 ine. ^The.** sq
35080 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
35090 31 36 62 65 28 29 20 61 6e 64 20 73 71 6c 69 74 16be() and sqlit
350a0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c e3_value_text16l
350b0 65 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a e() interfaces.*
350c0 2a 20 65 78 74 72 61 63 74 20 55 54 46 2d 31 36 * extract UTF-16
350d0 20 73 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d strings as big-
350e0 65 6e 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c endian and littl
350f0 65 2d 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74 e-endian respect
35100 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54 ively..**.** ^(T
35110 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 he sqlite3_value
35120 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 29 20 _numeric_type()
35130 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 interface attemp
35140 74 73 20 74 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e ts to apply.** n
35150 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74 79 20 umeric affinity
35160 74 6f 20 74 68 65 20 76 61 6c 75 65 2e 20 20 54 to the value. T
35170 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 61 his means that a
35180 6e 20 61 74 74 65 6d 70 74 20 69 73 0a 2a 2a 20 n attempt is.**
35190 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65 72 74 20 made to convert
351a0 74 68 65 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 the value to an
351b0 69 6e 74 65 67 65 72 20 6f 72 20 66 6c 6f 61 74 integer or float
351c0 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49 66 0a 2a ing point. If.*
351d0 2a 20 73 75 63 68 20 61 20 63 6f 6e 76 65 72 73 * such a convers
351e0 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 20 ion is possible
351f0 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 without loss of
35200 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 69 6e 20 information (in
35210 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 other.** words,
35220 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 if the value is
35230 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 6c 6f a string that lo
35240 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 oks like a numbe
35250 72 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 r).** then the c
35260 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 65 72 onversion is per
35270 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65 72 77 69 formed. Otherwi
35280 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73 69 6f 6e se no conversion
35290 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 54 68 65 20 occurs..** The
352a0 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 [SQLITE_INTEGER
352b0 7c 20 64 61 74 61 74 79 70 65 5d 20 61 66 74 65 | datatype] afte
352c0 72 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 r conversion is
352d0 72 65 74 75 72 6e 65 64 2e 29 5e 0a 2a 2a 0a 2a returned.)^.**.*
352e0 2a 20 50 6c 65 61 73 65 20 70 61 79 20 70 61 72 * Please pay par
352f0 74 69 63 75 6c 61 72 20 61 74 74 65 6e 74 69 6f ticular attentio
35300 6e 20 74 6f 20 74 68 65 20 66 61 63 74 20 74 68 n to the fact th
35310 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 72 at the pointer r
35320 65 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 eturned.** from
35330 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 [sqlite3_value_b
35340 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 lob()], [sqlite3
35350 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 20 _value_text()],
35360 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 or.** [sqlite3_v
35370 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20 63 alue_text16()] c
35380 61 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 an be invalidate
35390 64 20 62 79 20 61 20 73 75 62 73 65 71 75 65 6e d by a subsequen
353a0 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 t call to.** [sq
353b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
353c0 73 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 s()], [sqlite3_v
353d0 61 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d 2c alue_bytes16()],
353e0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
353f0 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b text()],.** or [
35400 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
35410 78 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 xt16()]..**.** T
35420 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 75 hese routines mu
35430 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f st be called fro
35440 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 m the same threa
35450 64 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c 20 d as.** the SQL
35460 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73 75 function that su
35470 70 70 6c 69 65 64 20 74 68 65 20 5b 73 71 6c 69 pplied the [sqli
35480 74 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 72 61 te3_value*] para
35490 6d 65 74 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 meters..*/.SQLIT
354a0 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
354b0 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *sqlite3_value_
354c0 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 6c blob(sqlite3_val
354d0 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
354e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
354f0 75 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 ue_bytes(sqlite3
35500 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 _value*);.SQLITE
35510 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
35520 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73 _value_bytes16(s
35530 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a qlite3_value*);.
35540 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c SQLITE_API doubl
35550 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f e sqlite3_value_
35560 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 double(sqlite3_v
35570 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 alue*);.SQLITE_A
35580 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 PI int sqlite3_v
35590 61 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 alue_int(sqlite3
355a0 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 _value*);.SQLITE
355b0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 _API sqlite3_int
355c0 36 34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 64 sqlite3_value
355d0 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 _int64(sqlite3_v
355e0 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 alue*);.SQLITE_A
355f0 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 PI const unsigne
35600 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f d char *sqlite3_
35610 76 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74 value_text(sqlit
35620 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 e3_value*);.SQLI
35630 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
35640 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 d *sqlite3_value
35650 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f _text16(sqlite3_
35660 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
35670 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
35680 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
35690 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 xt16le(sqlite3_v
356a0 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 alue*);.SQLITE_A
356b0 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
356c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
356d0 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f 76 61 t16be(sqlite3_va
356e0 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
356f0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 I int sqlite3_va
35700 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 33 lue_type(sqlite3
35710 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 _value*);.SQLITE
35720 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
35730 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 _value_numeric_t
35740 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 ype(sqlite3_valu
35750 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 e*);../*.** CAPI
35760 33 52 45 46 3a 20 4f 62 74 61 69 6e 20 41 67 67 3REF: Obtain Agg
35770 72 65 67 61 74 65 20 46 75 6e 63 74 69 6f 6e 20 regate Function
35780 43 6f 6e 74 65 78 74 0a 2a 2a 0a 2a 2a 20 49 6d Context.**.** Im
35790 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 plementations of
357a0 20 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 aggregate SQL f
357b0 75 6e 63 74 69 6f 6e 73 20 75 73 65 20 74 68 69 unctions use thi
357c0 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 s.** routine to
357d0 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 allocate memory
357e0 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 69 for storing thei
357f0 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 5e r state..**.** ^
35800 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 The first time t
35810 68 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 he sqlite3_aggre
35820 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43 2c 4e gate_context(C,N
35830 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c ) routine is cal
35840 6c 65 64 20 0a 2a 2a 20 66 6f 72 20 61 20 70 61 led .** for a pa
35850 72 74 69 63 75 6c 61 72 20 61 67 67 72 65 67 61 rticular aggrega
35860 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51 4c te function, SQL
35870 69 74 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 73 ite.** allocates
35880 20 4e 20 6f 66 20 6d 65 6d 6f 72 79 2c 20 7a 65 N of memory, ze
35890 72 6f 65 73 20 6f 75 74 20 74 68 61 74 20 6d 65 roes out that me
358a0 6d 6f 72 79 2c 20 61 6e 64 20 72 65 74 75 72 6e mory, and return
358b0 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 s a pointer.** t
358c0 6f 20 74 68 65 20 6e 65 77 20 6d 65 6d 6f 72 79 o the new memory
358d0 2e 20 5e 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 . ^On second and
358e0 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
358f0 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f s to.** sqlite3_
35900 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 aggregate_contex
35910 74 28 29 20 66 6f 72 20 74 68 65 20 73 61 6d 65 t() for the same
35920 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
35930 69 6f 6e 20 69 6e 73 74 61 6e 63 65 2c 0a 2a 2a ion instance,.**
35940 20 74 68 65 20 73 61 6d 65 20 62 75 66 66 65 72 the same buffer
35950 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 53 is returned. S
35960 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 qlite3_aggregate
35970 5f 63 6f 6e 74 65 78 74 28 29 20 69 73 20 6e 6f _context() is no
35980 72 6d 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64 rmally.** called
35990 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69 once for each i
359a0 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 nvocation of the
359b0 20 78 53 74 65 70 20 63 61 6c 6c 62 61 63 6b 20 xStep callback
359c0 61 6e 64 20 74 68 65 6e 20 6f 6e 65 0a 2a 2a 20 and then one.**
359d0 6c 61 73 74 20 74 69 6d 65 20 77 68 65 6e 20 74 last time when t
359e0 68 65 20 78 46 69 6e 61 6c 20 63 61 6c 6c 62 61 he xFinal callba
359f0 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 2e 20 20 ck is invoked.
35a00 5e 28 57 68 65 6e 20 6e 6f 20 72 6f 77 73 20 6d ^(When no rows m
35a10 61 74 63 68 0a 2a 2a 20 61 6e 20 61 67 67 72 65 atch.** an aggre
35a20 67 61 74 65 20 71 75 65 72 79 2c 20 74 68 65 20 gate query, the
35a30 78 53 74 65 70 28 29 20 63 61 6c 6c 62 61 63 6b xStep() callback
35a40 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74 of the aggregat
35a50 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6d e function.** im
35a60 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
35a70 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 61 6e 64 never called and
35a80 20 78 46 69 6e 61 6c 28 29 20 69 73 20 63 61 6c xFinal() is cal
35a90 6c 65 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 led exactly once
35aa0 2e 0a 2a 2a 20 49 6e 20 74 68 6f 73 65 20 63 61 ..** In those ca
35ab0 73 65 73 2c 20 73 71 6c 69 74 65 33 5f 61 67 67 ses, sqlite3_agg
35ac0 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 regate_context()
35ad0 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c 65 64 might be called
35ae0 20 66 6f 72 20 74 68 65 0a 2a 2a 20 66 69 72 73 for the.** firs
35af0 74 20 74 69 6d 65 20 66 72 6f 6d 20 77 69 74 68 t time from with
35b00 69 6e 20 78 46 69 6e 61 6c 28 29 2e 29 5e 0a 2a in xFinal().)^.*
35b10 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 *.** ^The sqlite
35b20 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 3_aggregate_cont
35b30 65 78 74 28 43 2c 4e 29 20 72 6f 75 74 69 6e 65 ext(C,N) routine
35b40 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 returns a NULL
35b50 70 6f 69 6e 74 65 72 20 69 66 20 4e 20 69 73 0a pointer if N is.
35b60 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 ** less than or
35b70 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 20 6f 72 equal to zero or
35b80 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c if a memory all
35b90 6f 63 61 74 65 20 65 72 72 6f 72 20 6f 63 63 75 ocate error occu
35ba0 72 73 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 rs..**.** ^(The
35bb0 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20 amount of space
35bc0 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c allocated by sql
35bd0 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 ite3_aggregate_c
35be0 6f 6e 74 65 78 74 28 43 2c 4e 29 20 69 73 0a 2a ontext(C,N) is.*
35bf0 2a 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 * determined by
35c00 74 68 65 20 4e 20 70 61 72 61 6d 65 74 65 72 20 the N parameter
35c10 6f 6e 20 66 69 72 73 74 20 73 75 63 63 65 73 73 on first success
35c20 66 75 6c 20 63 61 6c 6c 2e 20 20 43 68 61 6e 67 ful call. Chang
35c30 69 6e 67 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 ing the.** value
35c40 20 6f 66 20 4e 20 69 6e 20 73 75 62 73 65 71 75 of N in subsequ
35c50 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ent call to sqli
35c60 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
35c70 6e 74 65 78 74 28 29 20 77 69 74 68 69 6e 0a 2a ntext() within.*
35c80 2a 20 74 68 65 20 73 61 6d 65 20 61 67 67 72 65 * the same aggre
35c90 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e gate function in
35ca0 73 74 61 6e 63 65 20 77 69 6c 6c 20 6e 6f 74 20 stance will not
35cb0 72 65 73 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 resize the memor
35cc0 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e y.** allocation.
35cd0 29 5e 0a 2a 2a 0a 2a 2a 20 5e 53 51 4c 69 74 65 )^.**.** ^SQLite
35ce0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 66 automatically f
35cf0 72 65 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 rees the memory
35d00 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a allocated by .**
35d10 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 sqlite3_aggrega
35d20 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 77 68 65 te_context() whe
35d30 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 n the aggregate
35d40 71 75 65 72 79 20 63 6f 6e 63 6c 75 64 65 73 2e query concludes.
35d50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
35d60 20 70 61 72 61 6d 65 74 65 72 20 6d 75 73 74 20 parameter must
35d70 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 be a copy of the
35d80 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e .** [sqlite3_con
35d90 74 65 78 74 20 7c 20 53 51 4c 20 66 75 6e 63 74 text | SQL funct
35da0 69 6f 6e 20 63 6f 6e 74 65 78 74 5d 20 74 68 61 ion context] tha
35db0 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70 t is the first p
35dc0 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 arameter.** to t
35dd0 68 65 20 78 53 74 65 70 20 6f 72 20 78 46 69 6e he xStep or xFin
35de0 61 6c 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 al callback rout
35df0 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ine that impleme
35e00 6e 74 73 20 74 68 65 20 61 67 67 72 65 67 61 74 nts the aggregat
35e10 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a e.** function..*
35e20 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
35e30 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 e must be called
35e40 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 from the same t
35e50 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a hread in which.*
35e60 2a 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 * the aggregate
35e70 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 SQL function is
35e80 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 running..*/.SQLI
35e90 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
35ea0 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 ite3_aggregate_c
35eb0 6f 6e 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63 ontext(sqlite3_c
35ec0 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79 ontext*, int nBy
35ed0 74 65 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 tes);../*.** CAP
35ee0 49 33 52 45 46 3a 20 55 73 65 72 20 44 61 74 61 I3REF: User Data
35ef0 20 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 0a 2a For Functions.*
35f00 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 *.** ^The sqlite
35f10 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 69 6e 3_user_data() in
35f20 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
35f30 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 a copy of.** the
35f40 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 77 61 pointer that wa
35f50 73 20 74 68 65 20 70 55 73 65 72 44 61 74 61 20 s the pUserData
35f60 70 61 72 61 6d 65 74 65 72 20 28 74 68 65 20 35 parameter (the 5
35f70 74 68 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a th parameter).**
35f80 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
35f90 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
35fa0 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 ()].** and [sqli
35fb0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
35fc0 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 ion16()] routine
35fd0 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 6c s that originall
35fe0 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 20 y.** registered
35ff0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
36000 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
36010 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
36020 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c tine must be cal
36030 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d led from the sam
36040 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 e thread in whic
36050 68 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 h.** the applica
36060 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
36070 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 ction is running
36080 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
36090 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 73 void *sqlite3_us
360a0 65 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33 5f er_data(sqlite3_
360b0 63 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a context*);../*.*
360c0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 * CAPI3REF: Data
360d0 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 base Connection
360e0 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a For Functions.**
360f0 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 .** ^The sqlite3
36100 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
36110 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 le() interface r
36120 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 eturns a copy of
36130 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 .** the pointer
36140 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 to the [database
36150 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 connection] (th
36160 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 e 1st parameter)
36170 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 .** of the [sqli
36180 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
36190 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 ion()].** and [s
361a0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
361b0 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 nction16()] rout
361c0 69 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e ines that origin
361d0 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 ally.** register
361e0 65 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 ed the applicati
361f0 6f 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on defined funct
36200 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ion..*/.SQLITE_A
36210 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 PI sqlite3 *sqli
36220 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
36230 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f andle(sqlite3_co
36240 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 ntext*);../*.**
36250 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 CAPI3REF: Functi
36260 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 on Auxiliary Dat
36270 61 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c a.**.** The foll
36280 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 74 69 owing two functi
36290 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 64 20 ons may be used
362a0 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 66 75 by scalar SQL fu
362b0 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 61 73 nctions to.** as
362c0 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61 74 61 sociate metadata
362d0 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20 76 with argument v
362e0 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20 73 61 alues. If the sa
362f0 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 me value is pass
36300 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69 70 6c ed to.** multipl
36310 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66 e invocations of
36320 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20 66 75 the same SQL fu
36330 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20 71 75 nction during qu
36340 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c 20 75 ery execution, u
36350 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63 69 72 nder.** some cir
36360 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65 20 61 cumstances the a
36370 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61 64 61 ssociated metada
36380 74 61 20 6d 61 79 20 62 65 20 70 72 65 73 65 72 ta may be preser
36390 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a 2a 2a ved. This may.**
363a0 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20 65 78 be used, for ex
363b0 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20 61 20 ample, to add a
363c0 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73 73 69 regular-expressi
363d0 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63 61 6c on matching scal
363e0 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 ar.** function.
363f0 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76 65 72 The compiled ver
36400 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 67 75 sion of the regu
36410 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 lar expression i
36420 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 6d s stored as.** m
36430 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 61 74 etadata associat
36440 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 20 ed with the SQL
36450 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 value passed as
36460 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 the regular expr
36470 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74 65 72 ession.** patter
36480 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 n. The compiled
36490 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 regular express
364a0 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75 73 65 ion can be reuse
364b0 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a d on multiple.**
364c0 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20 invocations of
364d0 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f the same functio
364e0 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f 72 n so that the or
364f0 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e 20 73 iginal pattern s
36500 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20 6e 6f tring.** does no
36510 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 63 t need to be rec
36520 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63 68 20 ompiled on each
36530 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a invocation..**.*
36540 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 67 * ^The sqlite3_g
36550 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74 et_auxdata() int
36560 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 erface returns a
36570 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
36580 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73 6f metadata.** asso
36590 63 69 61 74 65 64 20 62 79 20 74 68 65 20 73 71 ciated by the sq
365a0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 lite3_set_auxdat
365b0 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 a() function wit
365c0 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65 h the Nth argume
365d0 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 74 nt.** value to t
365e0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 he application-d
365f0 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e efined function.
36600 20 5e 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61 ^If no metadata
36610 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a has been ever.*
36620 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74 * been set for t
36630 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20 he Nth argument
36640 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c of the function,
36650 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65 or if the corre
36660 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 sponding.** func
36670 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68 tion parameter h
36680 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 as changed since
36690 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77 the meta-data w
366a0 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 as set,.** then
366b0 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
366c0 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 20 ata() returns a
366d0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a NULL pointer..**
366e0 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 .** ^The sqlite3
366f0 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 _set_auxdata() i
36700 6e 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74 nterface saves t
36710 68 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 he metadata.** p
36720 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 ointed to by its
36730 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 3rd parameter a
36740 73 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 s the metadata f
36750 6f 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 or the N-th.** a
36760 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 rgument of the a
36770 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
36780 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 ed function. Su
36790 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c bsequent.** call
367a0 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 s to sqlite3_get
367b0 5f 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 _auxdata() might
367c0 20 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 return this dat
367d0 61 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 a, if it has.**
367e0 6e 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 not been destroy
367f0 65 64 2e 0a 2a 2a 20 5e 49 66 20 69 74 20 69 73 ed..** ^If it is
36800 20 6e 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 not NULL, SQLit
36810 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 e will invoke th
36820 65 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 e destructor.**
36830 66 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 function given b
36840 79 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 y the 4th parame
36850 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 ter to sqlite3_s
36860 65 74 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a et_auxdata() on.
36870 2a 2a 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 ** the metadata
36880 77 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 when the corresp
36890 6f 6e 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 onding function
368a0 70 61 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 parameter change
368b0 73 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 s.** or when the
368c0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 SQL statement c
368d0 6f 6d 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 ompletes, whiche
368e0 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e ver comes first.
368f0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 .**.** SQLite is
36900 20 66 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 free to call th
36910 65 20 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 e destructor and
36920 20 64 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f drop metadata o
36930 6e 20 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 n any.** paramet
36940 65 72 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 er of any functi
36950 6f 6e 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 on at any time.
36960 20 5e 54 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 ^The only guara
36970 6e 74 65 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 ntee is that.**
36980 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 77 the destructor w
36990 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 65 ill be called be
369a0 66 6f 72 65 20 74 68 65 20 6d 65 74 61 64 61 74 fore the metadat
369b0 61 20 69 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a a is dropped..**
369c0 0a 2a 2a 20 5e 28 49 6e 20 70 72 61 63 74 69 63 .** ^(In practic
369d0 65 2c 20 6d 65 74 61 64 61 74 61 20 69 73 20 70 e, metadata is p
369e0 72 65 73 65 72 76 65 64 20 62 65 74 77 65 65 6e reserved between
369f0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 function calls
36a00 66 6f 72 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f for.** expressio
36a10 6e 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 73 ns that are cons
36a20 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 tant at compile
36a30 74 69 6d 65 2e 20 54 68 69 73 20 69 6e 63 6c 75 time. This inclu
36a40 64 65 73 20 6c 69 74 65 72 61 6c 0a 2a 2a 20 76 des literal.** v
36a50 61 6c 75 65 73 20 61 6e 64 20 5b 70 61 72 61 6d alues and [param
36a60 65 74 65 72 73 5d 2e 29 5e 0a 2a 2a 0a 2a 2a 20 eters].)^.**.**
36a70 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d These routines m
36a80 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 ust be called fr
36a90 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 om the same thre
36aa0 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 ad in which.** t
36ab0 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 he SQL function
36ac0 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 53 is running..*/.S
36ad0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
36ae0 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
36af0 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ata(sqlite3_cont
36b00 65 78 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 ext*, int N);.SQ
36b10 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
36b20 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 lite3_set_auxdat
36b30 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 a(sqlite3_contex
36b40 74 2a 2c 20 69 6e 74 20 4e 2c 20 76 6f 69 64 2a t*, int N, void*
36b50 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a , void (*)(void*
36b60 29 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ));.../*.** CAPI
36b70 33 52 45 46 3a 20 43 6f 6e 73 74 61 6e 74 73 20 3REF: Constants
36b80 44 65 66 69 6e 69 6e 67 20 53 70 65 63 69 61 6c Defining Special
36b90 20 44 65 73 74 72 75 63 74 6f 72 20 42 65 68 61 Destructor Beha
36ba0 76 69 6f 72 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 vior.**.** These
36bb0 20 61 72 65 20 73 70 65 63 69 61 6c 20 76 61 6c are special val
36bc0 75 65 73 20 66 6f 72 20 74 68 65 20 64 65 73 74 ues for the dest
36bd0 72 75 63 74 6f 72 20 74 68 61 74 20 69 73 20 70 ructor that is p
36be0 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 0a assed in as the.
36bf0 2a 2a 20 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e ** final argumen
36c00 74 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 t to routines li
36c10 6b 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 ke [sqlite3_resu
36c20 6c 74 5f 62 6c 6f 62 28 29 5d 2e 20 20 5e 49 66 lt_blob()]. ^If
36c30 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 0a the destructor.
36c40 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 ** argument is S
36c50 51 4c 49 54 45 5f 53 54 41 54 49 43 2c 20 69 74 QLITE_STATIC, it
36c60 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
36c70 63 6f 6e 74 65 6e 74 20 70 6f 69 6e 74 65 72 20 content pointer
36c80 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 is constant.** a
36c90 6e 64 20 77 69 6c 6c 20 6e 65 76 65 72 20 63 68 nd will never ch
36ca0 61 6e 67 65 2e 20 20 49 74 20 64 6f 65 73 20 6e ange. It does n
36cb0 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 64 65 ot need to be de
36cc0 73 74 72 6f 79 65 64 2e 20 20 5e 54 68 65 0a 2a stroyed. ^The.*
36cd0 2a 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 * SQLITE_TRANSIE
36ce0 4e 54 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 74 NT value means t
36cf0 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 hat the content
36d00 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 68 61 6e will likely chan
36d10 67 65 20 69 6e 0a 2a 2a 20 74 68 65 20 6e 65 61 ge in.** the nea
36d20 72 20 66 75 74 75 72 65 20 61 6e 64 20 74 68 61 r future and tha
36d30 74 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64 20 t SQLite should
36d40 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 70 72 69 make its own pri
36d50 76 61 74 65 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 vate copy of.**
36d60 74 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 66 6f the content befo
36d70 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a re returning..**
36d80 0a 2a 2a 20 54 68 65 20 74 79 70 65 64 65 66 20 .** The typedef
36d90 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 is necessary to
36da0 77 6f 72 6b 20 61 72 6f 75 6e 64 20 70 72 6f 62 work around prob
36db0 6c 65 6d 73 20 69 6e 20 63 65 72 74 61 69 6e 0a lems in certain.
36dc0 2a 2a 20 43 2b 2b 20 63 6f 6d 70 69 6c 65 72 73 ** C++ compilers
36dd0 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 32 . See ticket #2
36de0 31 39 31 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 191..*/.typedef
36df0 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 5f 64 void (*sqlite3_d
36e00 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 28 estructor_type)(
36e10 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65 20 void*);.#define
36e20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20 20 20 SQLITE_STATIC
36e30 20 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 ((sqlite3_des
36e40 74 72 75 63 74 6f 72 5f 74 79 70 65 29 30 29 0a tructor_type)0).
36e50 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 #define SQLITE_T
36e60 52 41 4e 53 49 45 4e 54 20 20 20 28 28 73 71 6c RANSIENT ((sql
36e70 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f ite3_destructor_
36e80 74 79 70 65 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 type)-1)../*.**
36e90 43 41 50 49 33 52 45 46 3a 20 53 65 74 74 69 6e CAPI3REF: Settin
36ea0 67 20 54 68 65 20 52 65 73 75 6c 74 20 4f 66 20 g The Result Of
36eb0 41 6e 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 0a An SQL Function.
36ec0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 **.** These rout
36ed0 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 62 79 ines are used by
36ee0 20 74 68 65 20 78 46 75 6e 63 20 6f 72 20 78 46 the xFunc or xF
36ef0 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74 inal callbacks t
36f00 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 hat.** implement
36f10 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 SQL functions a
36f20 6e 64 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 nd aggregates.
36f30 53 65 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f See.** [sqlite3_
36f40 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 create_function(
36f50 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
36f60 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 create_function1
36f70 36 28 29 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69 6()].** for addi
36f80 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
36f90 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 on..**.** These
36fa0 66 75 6e 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76 functions work v
36fb0 65 72 79 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 ery much like th
36fc0 65 20 5b 70 61 72 61 6d 65 74 65 72 20 62 69 6e e [parameter bin
36fd0 64 69 6e 67 5d 20 66 61 6d 69 6c 79 20 6f 66 0a ding] family of.
36fe0 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 ** functions use
36ff0 64 20 74 6f 20 62 69 6e 64 20 76 61 6c 75 65 73 d to bind values
37000 20 74 6f 20 68 6f 73 74 20 70 61 72 61 6d 65 74 to host paramet
37010 65 72 73 20 69 6e 20 70 72 65 70 61 72 65 64 20 ers in prepared
37020 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52 statements..** R
37030 65 66 65 72 20 74 6f 20 74 68 65 20 5b 53 51 4c efer to the [SQL
37040 20 70 61 72 61 6d 65 74 65 72 5d 20 64 6f 63 75 parameter] docu
37050 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 mentation for ad
37060 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
37070 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 tion..**.** ^The
37080 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
37090 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65 blob() interface
370a0 20 73 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 sets the result
370b0 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c from.** an appl
370c0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
370d0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 function to be t
370e0 68 65 20 42 4c 4f 42 20 77 68 6f 73 65 20 63 6f he BLOB whose co
370f0 6e 74 65 6e 74 20 69 73 20 70 6f 69 6e 74 65 64 ntent is pointed
37100 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65 20 73 65 .** to by the se
37110 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 cond parameter a
37120 6e 64 20 77 68 69 63 68 20 69 73 20 4e 20 62 79 nd which is N by
37130 74 65 73 20 6c 6f 6e 67 20 77 68 65 72 65 20 4e tes long where N
37140 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 is the.** third
37150 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a parameter..**.*
37160 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72 * ^The sqlite3_r
37170 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 esult_zeroblob()
37180 20 69 6e 74 65 72 66 61 63 65 73 20 73 65 74 20 interfaces set
37190 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a the result of.**
371a0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
371b0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
371c0 6e 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 63 n to be a BLOB c
371d0 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 ontaining all ze
371e0 72 6f 0a 2a 2a 20 62 79 74 65 73 20 61 6e 64 20 ro.** bytes and
371f0 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c N bytes in size,
37200 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 where N is the
37210 76 61 6c 75 65 20 6f 66 20 74 68 65 20 32 6e 64 value of the 2nd
37220 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a parameter..**.*
37230 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72 * ^The sqlite3_r
37240 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 29 20 69 esult_double() i
37250 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 nterface sets th
37260 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a e result from.**
37270 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d an application-
37280 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
37290 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e to be a floatin
372a0 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 73 70 g point value sp
372b0 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 69 74 ecified.** by it
372c0 73 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a s 2nd argument..
372d0 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 **.** ^The sqlit
372e0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
372f0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 ) and sqlite3_re
37300 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 66 sult_error16() f
37310 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 63 61 75 73 unctions.** caus
37320 65 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 65 e the implemente
37330 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 d SQL function t
37340 6f 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 o throw an excep
37350 74 69 6f 6e 2e 0a 2a 2a 20 5e 53 51 4c 69 74 65 tion..** ^SQLite
37360 20 75 73 65 73 20 74 68 65 20 73 74 72 69 6e 67 uses the string
37370 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 pointed to by t
37380 68 65 0a 2a 2a 20 32 6e 64 20 70 61 72 61 6d 65 he.** 2nd parame
37390 74 65 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 72 ter of sqlite3_r
373a0 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 6f 72 esult_error() or
373b0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
373c0 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20 61 73 20 error16().** as
373d0 74 68 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 the text of an e
373e0 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 20 20 5e rror message. ^
373f0 53 51 4c 69 74 65 20 69 6e 74 65 72 70 72 65 74 SQLite interpret
37400 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d s the error.** m
37410 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 66 72 essage string fr
37420 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c om sqlite3_resul
37430 74 5f 65 72 72 6f 72 28 29 20 61 73 20 55 54 46 t_error() as UTF
37440 2d 38 2e 20 5e 53 51 4c 69 74 65 0a 2a 2a 20 69 -8. ^SQLite.** i
37450 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 73 74 nterprets the st
37460 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 65 ring from sqlite
37470 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 3_result_error16
37480 28 29 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20 () as UTF-16 in
37490 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 65 20 6f native.** byte o
374a0 72 64 65 72 2e 20 20 5e 49 66 20 74 68 65 20 74 rder. ^If the t
374b0 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 hird parameter t
374c0 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 o sqlite3_result
374d0 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73 _error().** or s
374e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
374f0 72 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61 74 ror16() is negat
37500 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20 ive then SQLite
37510 74 61 6b 65 73 20 61 73 20 74 68 65 20 65 72 72 takes as the err
37520 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61 6c or.** message al
37530 6c 20 74 65 78 74 20 75 70 20 74 68 72 6f 75 67 l text up throug
37540 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f h the first zero
37550 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 5e character..** ^
37560 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61 72 If the third par
37570 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 ameter to sqlite
37580 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 3_result_error()
37590 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 or.** sqlite3_r
375a0 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 esult_error16()
375b0 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 is non-negative
375c0 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 then SQLite take
375d0 73 20 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 62 s that many.** b
375e0 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 ytes (not charac
375f0 74 65 72 73 29 20 66 72 6f 6d 20 74 68 65 20 32 ters) from the 2
37600 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 nd parameter as
37610 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 the error messag
37620 65 2e 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 e..** ^The sqlit
37630 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
37640 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 ) and sqlite3_re
37650 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a sult_error16().*
37660 2a 20 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 * routines make
37670 61 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f a private copy o
37680 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 f the error mess
37690 61 67 65 20 74 65 78 74 20 62 65 66 6f 72 65 0a age text before.
376a0 2a 2a 20 74 68 65 79 20 72 65 74 75 72 6e 2e 20 ** they return.
376b0 20 48 65 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c Hence, the call
376c0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e ing function can
376d0 20 64 65 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a deallocate or.*
376e0 2a 20 6d 6f 64 69 66 79 20 74 68 65 20 74 65 78 * modify the tex
376f0 74 20 61 66 74 65 72 20 74 68 65 79 20 72 65 74 t after they ret
37700 75 72 6e 20 77 69 74 68 6f 75 74 20 68 61 72 6d urn without harm
37710 2e 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 ..** ^The sqlite
37720 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 3_result_error_c
37730 6f 64 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 ode() function c
37740 68 61 6e 67 65 73 20 74 68 65 20 65 72 72 6f 72 hanges the error
37750 20 63 6f 64 65 0a 2a 2a 20 72 65 74 75 72 6e 65 code.** returne
37760 64 20 62 79 20 53 51 4c 69 74 65 20 61 73 20 61 d by SQLite as a
37770 20 72 65 73 75 6c 74 20 6f 66 20 61 6e 20 65 72 result of an er
37780 72 6f 72 20 69 6e 20 61 20 66 75 6e 63 74 69 6f ror in a functio
37790 6e 2e 20 20 5e 42 79 20 64 65 66 61 75 6c 74 2c n. ^By default,
377a0 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63 6f .** the error co
377b0 64 65 20 69 73 20 53 51 4c 49 54 45 5f 45 52 52 de is SQLITE_ERR
377c0 4f 52 2e 20 20 5e 41 20 73 75 62 73 65 71 75 65 OR. ^A subseque
377d0 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 nt call to sqlit
377e0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
377f0 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f ).** or sqlite3_
37800 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 result_error16()
37810 20 72 65 73 65 74 73 20 74 68 65 20 65 72 72 6f resets the erro
37820 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54 45 r code to SQLITE
37830 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 5e 54 _ERROR..**.** ^T
37840 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
37850 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 29 t_error_toobig()
37860 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 73 65 interface cause
37870 73 20 53 51 4c 69 74 65 20 74 6f 20 74 68 72 6f s SQLite to thro
37880 77 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 69 6e w an.** error in
37890 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 61 20 dicating that a
378a0 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 69 string or BLOB i
378b0 73 20 74 6f 6f 20 6c 6f 6e 67 20 74 6f 20 72 65 s too long to re
378c0 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 5e present..**.** ^
378d0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 The sqlite3_resu
378e0 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 29 lt_error_nomem()
378f0 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 73 65 interface cause
37900 73 20 53 51 4c 69 74 65 20 74 6f 20 74 68 72 6f s SQLite to thro
37910 77 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 69 6e w an.** error in
37920 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 61 20 dicating that a
37930 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
37940 6e 20 66 61 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 n failed..**.**
37950 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 ^The sqlite3_res
37960 75 6c 74 5f 69 6e 74 28 29 20 69 6e 74 65 72 66 ult_int() interf
37970 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74 ace sets the ret
37980 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 urn value.** of
37990 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
379a0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
379b0 20 74 6f 20 62 65 20 74 68 65 20 33 32 2d 62 69 to be the 32-bi
379c0 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
379d0 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 6e 20 .** value given
379e0 69 6e 20 74 68 65 20 32 6e 64 20 61 72 67 75 6d in the 2nd argum
379f0 65 6e 74 2e 0a 2a 2a 20 5e 54 68 65 20 73 71 6c ent..** ^The sql
37a00 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 ite3_result_int6
37a10 34 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 4() interface se
37a20 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ts the return va
37a30 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 lue.** of the ap
37a40 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
37a50 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 d function to be
37a60 20 74 68 65 20 36 34 2d 62 69 74 20 73 69 67 6e the 64-bit sign
37a70 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61 ed integer.** va
37a80 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 lue given in the
37a90 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2nd argument..*
37aa0 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 *.** ^The sqlite
37ab0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20 3_result_null()
37ac0 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 interface sets t
37ad0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a he return value.
37ae0 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 ** of the applic
37af0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
37b00 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c nction to be NUL
37b10 4c 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 L..**.** ^The sq
37b20 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
37b30 74 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 t(), sqlite3_res
37b40 75 6c 74 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a ult_text16(),.**
37b50 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
37b60 74 65 78 74 31 36 6c 65 28 29 2c 20 61 6e 64 20 text16le(), and
37b70 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
37b80 65 78 74 31 36 62 65 28 29 20 69 6e 74 65 72 66 ext16be() interf
37b90 61 63 65 73 0a 2a 2a 20 73 65 74 20 74 68 65 20 aces.** set the
37ba0 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 return value of
37bb0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
37bc0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
37bd0 20 74 6f 20 62 65 0a 2a 2a 20 61 20 74 65 78 74 to be.** a text
37be0 20 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 73 string which is
37bf0 20 72 65 70 72 65 73 65 6e 74 65 64 20 61 73 20 represented as
37c00 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 20 6e 61 UTF-8, UTF-16 na
37c10 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2c tive byte order,
37c20 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c .** UTF-16 littl
37c30 65 20 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 e endian, or UTF
37c40 2d 31 36 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 -16 big endian,
37c50 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a respectively..**
37c60 20 5e 53 51 4c 69 74 65 20 74 61 6b 65 73 20 74 ^SQLite takes t
37c70 68 65 20 74 65 78 74 20 72 65 73 75 6c 74 20 66 he text result f
37c80 72 6f 6d 20 74 68 65 20 61 70 70 6c 69 63 61 74 rom the applicat
37c90 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 ion from.** the
37ca0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 2nd parameter of
37cb0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 the sqlite3_res
37cc0 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 ult_text* interf
37cd0 61 63 65 73 2e 0a 2a 2a 20 5e 49 66 20 74 68 65 aces..** ^If the
37ce0 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 3rd parameter t
37cf0 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 o the sqlite3_re
37d00 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 sult_text* inter
37d10 66 61 63 65 73 0a 2a 2a 20 69 73 20 6e 65 67 61 faces.** is nega
37d20 74 69 76 65 2c 20 74 68 65 6e 20 53 51 4c 69 74 tive, then SQLit
37d30 65 20 74 61 6b 65 73 20 72 65 73 75 6c 74 20 74 e takes result t
37d40 65 78 74 20 66 72 6f 6d 20 74 68 65 20 32 6e 64 ext from the 2nd
37d50 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 68 parameter.** th
37d60 72 6f 75 67 68 20 74 68 65 20 66 69 72 73 74 20 rough the first
37d70 7a 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e 0a zero character..
37d80 2a 2a 20 5e 49 66 20 74 68 65 20 33 72 64 20 70 ** ^If the 3rd p
37d90 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 arameter to the
37da0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
37db0 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a ext* interfaces.
37dc0 2a 2a 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 ** is non-negati
37dd0 76 65 2c 20 74 68 65 6e 20 61 73 20 6d 61 6e 79 ve, then as many
37de0 20 62 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 bytes (not char
37df0 61 63 74 65 72 73 29 20 6f 66 20 74 68 65 20 74 acters) of the t
37e00 65 78 74 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 ext.** pointed t
37e10 6f 20 62 79 20 74 68 65 20 32 6e 64 20 70 61 72 o by the 2nd par
37e20 61 6d 65 74 65 72 20 61 72 65 20 74 61 6b 65 6e ameter are taken
37e30 20 61 73 20 74 68 65 20 61 70 70 6c 69 63 61 74 as the applicat
37e40 69 6f 6e 2d 64 65 66 69 6e 65 64 0a 2a 2a 20 66 ion-defined.** f
37e50 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 20 unction result.
37e60 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61 If the 3rd para
37e70 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 6e 65 67 meter is non-neg
37e80 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74 0a 2a ative, then it.*
37e90 2a 20 6d 75 73 74 20 62 65 20 74 68 65 20 62 79 * must be the by
37ea0 74 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 te offset into t
37eb0 68 65 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 he string where
37ec0 74 68 65 20 4e 55 4c 20 74 65 72 6d 69 6e 61 74 the NUL terminat
37ed0 6f 72 20 77 6f 75 6c 64 0a 2a 2a 20 61 70 70 65 or would.** appe
37ee0 61 72 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 ar if the string
37ef0 20 77 68 65 72 65 20 4e 55 4c 20 74 65 72 6d 69 where NUL termi
37f00 6e 61 74 65 64 2e 20 20 49 66 20 61 6e 79 20 4e nated. If any N
37f10 55 4c 20 63 68 61 72 61 63 74 65 72 73 20 6f 63 UL characters oc
37f20 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 20 73 74 cur.** in the st
37f30 72 69 6e 67 20 61 74 20 61 20 62 79 74 65 20 6f ring at a byte o
37f40 66 66 73 65 74 20 74 68 61 74 20 69 73 20 6c 65 ffset that is le
37f50 73 73 20 74 68 61 6e 20 74 68 65 20 76 61 6c 75 ss than the valu
37f60 65 20 6f 66 20 74 68 65 20 33 72 64 0a 2a 2a 20 e of the 3rd.**
37f70 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20 parameter, then
37f80 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 73 74 the resulting st
37f90 72 69 6e 67 20 77 69 6c 6c 20 63 6f 6e 74 61 69 ring will contai
37fa0 6e 20 65 6d 62 65 64 64 65 64 20 4e 55 4c 73 20 n embedded NULs
37fb0 61 6e 64 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c and the.** resul
37fc0 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 t of expressions
37fd0 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e 20 73 74 operating on st
37fe0 72 69 6e 67 73 20 77 69 74 68 20 65 6d 62 65 64 rings with embed
37ff0 64 65 64 20 4e 55 4c 73 20 69 73 20 75 6e 64 65 ded NULs is unde
38000 66 69 6e 65 64 2e 0a 2a 2a 20 5e 49 66 20 74 68 fined..** ^If th
38010 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 4th parameter
38020 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 to the sqlite3_r
38030 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 esult_text* inte
38040 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c rfaces.** or sql
38050 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
38060 20 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 is a non-NULL p
38070 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c ointer, then SQL
38080 69 74 65 20 63 61 6c 6c 73 20 74 68 61 74 0a 2a ite calls that.*
38090 2a 20 66 75 6e 63 74 69 6f 6e 20 61 73 20 74 68 * function as th
380a0 65 20 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 e destructor on
380b0 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 the text or BLOB
380c0 20 72 65 73 75 6c 74 20 77 68 65 6e 20 69 74 20 result when it
380d0 68 61 73 0a 2a 2a 20 66 69 6e 69 73 68 65 64 20 has.** finished
380e0 75 73 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c using that resul
380f0 74 2e 0a 2a 2a 20 5e 49 66 20 74 68 65 20 34 74 t..** ^If the 4t
38100 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 h parameter to t
38110 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
38120 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 t_text* interfac
38130 65 73 20 6f 72 20 74 6f 0a 2a 2a 20 73 71 6c 69 es or to.** sqli
38140 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 te3_result_blob
38150 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 is the special c
38160 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 onstant SQLITE_S
38170 54 41 54 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 TATIC, then SQLi
38180 74 65 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 te.** assumes th
38190 61 74 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 at the text or B
381a0 4c 4f 42 20 72 65 73 75 6c 74 20 69 73 20 69 6e LOB result is in
381b0 20 63 6f 6e 73 74 61 6e 74 20 73 70 61 63 65 20 constant space
381c0 61 6e 64 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 and does not.**
381d0 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 copy the content
381e0 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 of the paramete
381f0 72 20 6e 6f 72 20 63 61 6c 6c 20 61 20 64 65 73 r nor call a des
38200 74 72 75 63 74 6f 72 20 6f 6e 20 74 68 65 20 63 tructor on the c
38210 6f 6e 74 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 69 ontent.** when i
38220 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 t has finished u
38230 73 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74 sing that result
38240 2e 0a 2a 2a 20 5e 49 66 20 74 68 65 20 34 74 68 ..** ^If the 4th
38250 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
38260 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
38270 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 _text* interface
38280 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f s.** or sqlite3_
38290 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74 result_blob is t
382a0 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 he special const
382b0 61 6e 74 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 ant SQLITE_TRANS
382c0 49 45 4e 54 0a 2a 2a 20 74 68 65 6e 20 53 51 4c IENT.** then SQL
382d0 69 74 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 ite makes a copy
382e0 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 of the result i
382f0 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e nto space obtain
38300 65 64 20 66 72 6f 6d 0a 2a 2a 20 66 72 6f 6d 20 ed from.** from
38310 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
38320 29 5d 20 62 65 66 6f 72 65 20 69 74 20 72 65 74 )] before it ret
38330 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 urns..**.** ^The
38340 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
38350 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66 61 63 value() interfac
38360 65 20 73 65 74 73 20 74 68 65 20 72 65 73 75 6c e sets the resul
38370 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c t of.** the appl
38380 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
38390 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 function to be a
383a0 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20 5b 75 6e copy the.** [un
383b0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
383c0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 3_value] object
383d0 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 specified by the
383e0 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 2nd parameter.
383f0 20 5e 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 ^The.** sqlite3
38400 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 _result_value()
38410 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20 interface makes
38420 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 5b 73 a copy of the [s
38430 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a qlite3_value].**
38440 20 73 6f 20 74 68 61 74 20 74 68 65 20 5b 73 71 so that the [sq
38450 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 73 70 65 lite3_value] spe
38460 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 70 61 cified in the pa
38470 72 61 6d 65 74 65 72 20 6d 61 79 20 63 68 61 6e rameter may chan
38480 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 65 61 6c ge or.** be deal
38490 6c 6f 63 61 74 65 64 20 61 66 74 65 72 20 73 71 located after sq
384a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c lite3_result_val
384b0 75 65 28 29 20 72 65 74 75 72 6e 73 20 77 69 74 ue() returns wit
384c0 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 5e 41 hout harm..** ^A
384d0 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 [protected sqli
384e0 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 te3_value] objec
384f0 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62 65 20 t may always be
38500 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a 2a 2a used where an.**
38510 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 [unprotected sq
38520 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a lite3_value] obj
38530 65 63 74 20 69 73 20 72 65 71 75 69 72 65 64 2c ect is required,
38540 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 6b 69 so either.** ki
38550 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 76 nd of [sqlite3_v
38560 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 61 6e alue] object can
38570 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68 be used with th
38580 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a is interface..**
38590 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72 6f 75 .** If these rou
385a0 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64 tines are called
385b0 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 from within the
385c0 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 different threa
385d0 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 6f 6e d.** than the on
385e0 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
385f0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 application-def
38600 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 68 ined function th
38610 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a 20 74 at received.** t
38620 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 he [sqlite3_cont
38630 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74 68 ext] pointer, th
38640 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e e results are un
38650 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 defined..*/.SQLI
38660 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
38670 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 te3_result_blob(
38680 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
38690 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 , const void*, i
386a0 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 nt, void(*)(void
386b0 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 *));.SQLITE_API
386c0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
386d0 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 ult_double(sqlit
386e0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 6f 75 e3_context*, dou
386f0 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ble);.SQLITE_API
38700 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
38710 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 74 sult_error(sqlit
38720 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e e3_context*, con
38730 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a st char*, int);.
38740 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
38750 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
38760 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f 63 rror16(sqlite3_c
38770 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 ontext*, const v
38780 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 oid*, int);.SQLI
38790 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
387a0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
387b0 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33 5f _toobig(sqlite3_
387c0 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 context*);.SQLIT
387d0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
387e0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f e3_result_error_
387f0 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63 6f nomem(sqlite3_co
38800 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f ntext*);.SQLITE_
38810 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
38820 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f _result_error_co
38830 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 de(sqlite3_conte
38840 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 xt*, int);.SQLIT
38850 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
38860 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73 71 e3_result_int(sq
38870 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
38880 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 int);.SQLITE_API
38890 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
388a0 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69 74 sult_int64(sqlit
388b0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71 6c e3_context*, sql
388c0 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c ite3_int64);.SQL
388d0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
388e0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c ite3_result_null
388f0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
38900 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 *);.SQLITE_API v
38910 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
38920 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f lt_text(sqlite3_
38930 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 context*, const
38940 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 char*, int, void
38950 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c (*)(void*));.SQL
38960 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
38970 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
38980 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 16(sqlite3_conte
38990 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a xt*, const void*
389a0 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 , int, void(*)(v
389b0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 oid*));.SQLITE_A
389c0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
389d0 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 28 result_text16le(
389e0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
389f0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 , const void*, i
38a00 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a nt,void(*)(void*
38a10 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 ));.SQLITE_API v
38a20 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
38a30 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69 lt_text16be(sqli
38a40 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f te3_context*, co
38a50 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 76 nst void*, int,v
38a60 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
38a70 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
38a80 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 sqlite3_result_v
38a90 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e alue(sqlite3_con
38aa0 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f 76 text*, sqlite3_v
38ab0 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 alue*);.SQLITE_A
38ac0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
38ad0 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 result_zeroblob(
38ae0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
38af0 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a , int n);../*.**
38b00 20 43 41 50 49 33 52 45 46 3a 20 44 65 66 69 6e CAPI3REF: Defin
38b10 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e 67 20 e New Collating
38b20 53 65 71 75 65 6e 63 65 73 0a 2a 2a 0a 2a 2a 20 Sequences.**.**
38b30 5e 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 ^These functions
38b40 20 61 64 64 2c 20 72 65 6d 6f 76 65 2c 20 6f 72 add, remove, or
38b50 20 6d 6f 64 69 66 79 20 61 20 5b 63 6f 6c 6c 61 modify a [colla
38b60 74 69 6f 6e 5d 20 61 73 73 6f 63 69 61 74 65 64 tion] associated
38b70 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 64 61 .** with the [da
38b80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
38b90 6e 5d 20 73 70 65 63 69 66 69 65 64 20 61 73 20 n] specified as
38ba0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
38bb0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 6e nt..**.** ^The n
38bc0 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 ame of the colla
38bd0 74 69 6f 6e 20 69 73 20 61 20 55 54 46 2d 38 20 tion is a UTF-8
38be0 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 73 71 string.** for sq
38bf0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
38c00 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73 71 6c lation() and sql
38c10 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c ite3_create_coll
38c20 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 61 6e ation_v2().** an
38c30 64 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e d a UTF-16 strin
38c40 67 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65 g in native byte
38c50 20 6f 72 64 65 72 20 66 6f 72 20 73 71 6c 69 74 order for sqlit
38c60 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
38c70 69 6f 6e 31 36 28 29 2e 0a 2a 2a 20 5e 43 6f 6c ion16()..** ^Col
38c80 6c 61 74 69 6f 6e 20 6e 61 6d 65 73 20 74 68 61 lation names tha
38c90 74 20 63 6f 6d 70 61 72 65 20 65 71 75 61 6c 20 t compare equal
38ca0 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 5b 73 71 according to [sq
38cb0 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 29 lite3_strnicmp()
38cc0 5d 20 61 72 65 0a 2a 2a 20 63 6f 6e 73 69 64 65 ] are.** conside
38cd0 72 65 64 20 74 6f 20 62 65 20 74 68 65 20 73 61 red to be the sa
38ce0 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 5e me name..**.** ^
38cf0 28 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d (The third argum
38d00 65 6e 74 20 28 65 54 65 78 74 52 65 70 29 20 6d ent (eTextRep) m
38d10 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 ust be one of th
38d20 65 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 20 e constants:.**
38d30 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 <ul>.** <li> [SQ
38d40 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a 20 3c LITE_UTF8],.** <
38d50 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 li> [SQLITE_UTF1
38d60 36 4c 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 6LE],.** <li> [S
38d70 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c 0a QLITE_UTF16BE],.
38d80 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
38d90 55 54 46 31 36 5d 2c 20 6f 72 0a 2a 2a 20 3c 6c UTF16], or.** <l
38da0 69 3e 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 i> [SQLITE_UTF16
38db0 5f 41 4c 49 47 4e 45 44 5d 2e 0a 2a 2a 20 3c 2f _ALIGNED]..** </
38dc0 75 6c 3e 29 5e 0a 2a 2a 20 5e 54 68 65 20 65 54 ul>)^.** ^The eT
38dd0 65 78 74 52 65 70 20 61 72 67 75 6d 65 6e 74 20 extRep argument
38de0 64 65 74 65 72 6d 69 6e 65 73 20 74 68 65 20 65 determines the e
38df0 6e 63 6f 64 69 6e 67 20 6f 66 20 73 74 72 69 6e ncoding of strin
38e00 67 73 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20 gs passed.** to
38e10 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 the collating fu
38e20 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2c nction callback,
38e30 20 78 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 5e xCallback..** ^
38e40 54 68 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 The [SQLITE_UTF1
38e50 36 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 55 6] and [SQLITE_U
38e60 54 46 31 36 5f 41 4c 49 47 4e 45 44 5d 20 76 61 TF16_ALIGNED] va
38e70 6c 75 65 73 20 66 6f 72 20 65 54 65 78 74 52 65 lues for eTextRe
38e80 70 0a 2a 2a 20 66 6f 72 63 65 20 73 74 72 69 6e p.** force strin
38e90 67 73 20 74 6f 20 62 65 20 55 54 46 31 36 20 77 gs to be UTF16 w
38ea0 69 74 68 20 6e 61 74 69 76 65 20 62 79 74 65 20 ith native byte
38eb0 6f 72 64 65 72 2e 0a 2a 2a 20 5e 54 68 65 20 5b order..** ^The [
38ec0 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 SQLITE_UTF16_ALI
38ed0 47 4e 45 44 5d 20 76 61 6c 75 65 20 66 6f 72 20 GNED] value for
38ee0 65 54 65 78 74 52 65 70 20 66 6f 72 63 65 73 20 eTextRep forces
38ef0 73 74 72 69 6e 67 73 20 74 6f 20 62 65 67 69 6e strings to begin
38f00 0a 2a 2a 20 6f 6e 20 61 6e 20 65 76 65 6e 20 62 .** on an even b
38f10 79 74 65 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a yte address..**.
38f20 2a 2a 20 5e 54 68 65 20 66 6f 75 72 74 68 20 61 ** ^The fourth a
38f30 72 67 75 6d 65 6e 74 2c 20 70 41 72 67 2c 20 69 rgument, pArg, i
38f40 73 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e s an application
38f50 20 64 61 74 61 20 70 6f 69 6e 74 65 72 20 74 68 data pointer th
38f60 61 74 20 69 73 20 70 61 73 73 65 64 0a 2a 2a 20 at is passed.**
38f70 74 68 72 6f 75 67 68 20 61 73 20 74 68 65 20 66 through as the f
38f80 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
38f90 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 the collating f
38fa0 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b unction callback
38fb0 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 66 ..**.** ^The fif
38fc0 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 78 43 61 th argument, xCa
38fd0 6c 6c 62 61 63 6b 2c 20 69 73 20 61 20 70 6f 69 llback, is a poi
38fe0 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6c 6c nter to the coll
38ff0 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a ating function..
39000 2a 2a 20 5e 4d 75 6c 74 69 70 6c 65 20 63 6f 6c ** ^Multiple col
39010 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 lating functions
39020 20 63 61 6e 20 62 65 20 72 65 67 69 73 74 65 72 can be register
39030 65 64 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d ed using the sam
39040 65 20 6e 61 6d 65 20 62 75 74 0a 2a 2a 20 77 69 e name but.** wi
39050 74 68 20 64 69 66 66 65 72 65 6e 74 20 65 54 65 th different eTe
39060 78 74 52 65 70 20 70 61 72 61 6d 65 74 65 72 73 xtRep parameters
39070 20 61 6e 64 20 53 51 4c 69 74 65 20 77 69 6c 6c and SQLite will
39080 20 75 73 65 20 77 68 69 63 68 65 76 65 72 0a 2a use whichever.*
39090 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 * function requi
390a0 72 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 6d res the least am
390b0 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 74 72 61 ount of data tra
390c0 6e 73 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 nsformation..**
390d0 5e 49 66 20 74 68 65 20 78 43 61 6c 6c 62 61 63 ^If the xCallbac
390e0 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55 k argument is NU
390f0 4c 4c 20 74 68 65 6e 20 74 68 65 20 63 6f 6c 6c LL then the coll
39100 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 ating function i
39110 73 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 20 20 5e s.** deleted. ^
39120 57 68 65 6e 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 When all collati
39130 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 ng functions hav
39140 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6e 61 6d ing the same nam
39150 65 20 61 72 65 20 64 65 6c 65 74 65 64 2c 0a 2a e are deleted,.*
39160 2a 20 74 68 61 74 20 63 6f 6c 6c 61 74 69 6f 6e * that collation
39170 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 75 73 is no longer us
39180 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 able..**.** ^The
39190 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 collating funct
391a0 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 ion callback is
391b0 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 20 63 invoked with a c
391c0 6f 70 79 20 6f 66 20 74 68 65 20 70 41 72 67 20 opy of the pArg
391d0 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 .** application
391e0 64 61 74 61 20 70 6f 69 6e 74 65 72 20 61 6e 64 data pointer and
391f0 20 77 69 74 68 20 74 77 6f 20 73 74 72 69 6e 67 with two string
39200 73 20 69 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e s in the encodin
39210 67 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 g specified.** b
39220 79 20 74 68 65 20 65 54 65 78 74 52 65 70 20 61 y the eTextRep a
39230 72 67 75 6d 65 6e 74 2e 20 20 54 68 65 20 63 6f rgument. The co
39240 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e llating function
39250 20 6d 75 73 74 20 72 65 74 75 72 6e 20 61 6e 0a must return an.
39260 2a 2a 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 ** integer that
39270 69 73 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 is negative, zer
39280 6f 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 0a 2a o, or positive.*
39290 2a 20 69 66 20 74 68 65 20 66 69 72 73 74 20 73 * if the first s
392a0 74 72 69 6e 67 20 69 73 20 6c 65 73 73 20 74 68 tring is less th
392b0 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 an, equal to, or
392c0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
392d0 65 20 73 65 63 6f 6e 64 2c 0a 2a 2a 20 72 65 73 e second,.** res
392e0 70 65 63 74 69 76 65 6c 79 2e 20 20 41 20 63 6f pectively. A co
392f0 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e llating function
39300 20 6d 75 73 74 20 61 6c 77 61 79 73 20 72 65 74 must always ret
39310 75 72 6e 20 74 68 65 20 73 61 6d 65 20 61 6e 73 urn the same ans
39320 77 65 72 0a 2a 2a 20 67 69 76 65 6e 20 74 68 65 wer.** given the
39330 20 73 61 6d 65 20 69 6e 70 75 74 73 2e 20 20 49 same inputs. I
39340 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 6f f two or more co
39350 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e llating function
39360 73 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64 s are registered
39370 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61 6d 65 20 .** to the same
39380 63 6f 6c 6c 61 74 69 6f 6e 20 6e 61 6d 65 20 28 collation name (
39390 75 73 69 6e 67 20 64 69 66 66 65 72 65 6e 74 20 using different
393a0 65 54 65 78 74 52 65 70 20 76 61 6c 75 65 73 29 eTextRep values)
393b0 20 74 68 65 6e 20 61 6c 6c 0a 2a 2a 20 6d 75 73 then all.** mus
393c0 74 20 67 69 76 65 20 61 6e 20 65 71 75 69 76 61 t give an equiva
393d0 6c 65 6e 74 20 61 6e 73 77 65 72 20 77 68 65 6e lent answer when
393e0 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 65 71 invoked with eq
393f0 75 69 76 61 6c 65 6e 74 20 73 74 72 69 6e 67 73 uivalent strings
39400 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 ..** The collati
39410 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 ng function must
39420 20 6f 62 65 79 20 74 68 65 20 66 6f 6c 6c 6f 77 obey the follow
39430 69 6e 67 20 70 72 6f 70 65 72 74 69 65 73 20 66 ing properties f
39440 6f 72 20 61 6c 6c 0a 2a 2a 20 73 74 72 69 6e 67 or all.** string
39450 73 20 41 2c 20 42 2c 20 61 6e 64 20 43 3a 0a 2a s A, B, and C:.*
39460 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ol>.** <li
39470 3e 20 49 66 20 41 3d 3d 42 20 74 68 65 6e 20 42 > If A==B then B
39480 3d 3d 41 2e 0a 2a 2a 20 3c 6c 69 3e 20 49 66 20 ==A..** <li> If
39490 41 3d 3d 42 20 61 6e 64 20 42 3d 3d 43 20 74 68 A==B and B==C th
394a0 65 6e 20 41 3d 3d 43 2e 0a 2a 2a 20 3c 6c 69 3e en A==C..** <li>
394b0 20 49 66 20 41 26 6c 74 3b 42 20 54 48 45 4e 20 If A<B THEN
394c0 42 26 67 74 3b 41 2e 0a 2a 2a 20 3c 6c 69 3e 20 B>A..** <li>
394d0 49 66 20 41 26 6c 74 3b 42 20 61 6e 64 20 42 26 If A<B and B&
394e0 6c 74 3b 43 20 74 68 65 6e 20 41 26 6c 74 3b 43 lt;C then A<C
394f0 2e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a ..** </ol>.**.**
39500 20 49 66 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 If a collating
39510 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 73 20 61 function fails a
39520 6e 79 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 ny of the above
39530 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 6e 64 20 constraints and
39540 74 68 61 74 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e that.** collatin
39550 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 20 72 g function is r
39560 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 75 73 egistered and us
39570 65 64 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68 ed, then the beh
39580 61 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 0a avior of SQLite.
39590 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e ** is undefined.
395a0 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 .**.** ^The sqli
395b0 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 te3_create_colla
395c0 74 69 6f 6e 5f 76 32 28 29 20 77 6f 72 6b 73 20 tion_v2() works
395d0 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f 63 72 65 like sqlite3_cre
395e0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 0a ate_collation().
395f0 2a 2a 20 77 69 74 68 20 74 68 65 20 61 64 64 69 ** with the addi
39600 74 69 6f 6e 20 74 68 61 74 20 74 68 65 20 78 44 tion that the xD
39610 65 73 74 72 6f 79 20 63 61 6c 6c 62 61 63 6b 20 estroy callback
39620 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 70 41 is invoked on pA
39630 72 67 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63 rg when.** the c
39640 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f ollating functio
39650 6e 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a n is deleted..**
39660 20 5e 43 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 ^Collating func
39670 74 69 6f 6e 73 20 61 72 65 20 64 65 6c 65 74 65 tions are delete
39680 64 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 d when they are
39690 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 6c 61 overridden by la
396a0 74 65 72 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 ter.** calls to
396b0 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 72 the collation cr
396c0 65 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 eation functions
396d0 20 6f 72 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 or when the.**
396e0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
396f0 74 69 6f 6e 5d 20 69 73 20 63 6c 6f 73 65 64 20 tion] is closed
39700 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 using [sqlite3_c
39710 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 5e lose()]..**.** ^
39720 54 68 65 20 78 44 65 73 74 72 6f 79 20 63 61 6c The xDestroy cal
39730 6c 62 61 63 6b 20 69 73 20 3c 75 3e 6e 6f 74 3c lback is <u>not<
39740 2f 75 3e 20 63 61 6c 6c 65 64 20 69 66 20 74 68 /u> called if th
39750 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 72 e .** sqlite3_cr
39760 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 eate_collation_v
39770 32 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 2() function fai
39780 6c 73 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e ls. Application
39790 73 20 74 68 61 74 20 69 6e 76 6f 6b 65 0a 2a 2a s that invoke.**
397a0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
397b0 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 20 77 collation_v2() w
397c0 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 78 ith a non-NULL x
397d0 44 65 73 74 72 6f 79 20 61 72 67 75 6d 65 6e 74 Destroy argument
397e0 20 73 68 6f 75 6c 64 20 0a 2a 2a 20 63 68 65 63 should .** chec
397f0 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 k the return cod
39800 65 20 61 6e 64 20 64 69 73 70 6f 73 65 20 6f 66 e and dispose of
39810 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
39820 20 64 61 74 61 20 70 6f 69 6e 74 65 72 0a 2a 2a data pointer.**
39830 20 74 68 65 6d 73 65 6c 76 65 73 20 72 61 74 68 themselves rath
39840 65 72 20 74 68 61 6e 20 65 78 70 65 63 74 69 6e er than expectin
39850 67 20 53 51 4c 69 74 65 20 74 6f 20 64 65 61 6c g SQLite to deal
39860 20 77 69 74 68 20 69 74 20 66 6f 72 20 74 68 65 with it for the
39870 6d 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69 m..** This is di
39880 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 65 76 65 fferent from eve
39890 72 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 ry other SQLite
398a0 69 6e 74 65 72 66 61 63 65 2e 20 20 54 68 65 20 interface. The
398b0 69 6e 63 6f 6e 73 69 73 74 65 6e 63 79 20 0a 2a inconsistency .*
398c0 2a 20 69 73 20 75 6e 66 6f 72 74 75 6e 61 74 65 * is unfortunate
398d0 20 62 75 74 20 63 61 6e 6e 6f 74 20 62 65 20 63 but cannot be c
398e0 68 61 6e 67 65 64 20 77 69 74 68 6f 75 74 20 62 hanged without b
398f0 72 65 61 6b 69 6e 67 20 62 61 63 6b 77 61 72 64 reaking backward
39900 73 20 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c s .** compatibil
39910 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 ity..**.** See a
39920 6c 73 6f 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63 lso: [sqlite3_c
39930 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 ollation_needed(
39940 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
39950 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 collation_needed
39960 31 36 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 16()]..*/.SQLITE
39970 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
39980 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
39990 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a n(. sqlite3*, .
399a0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
399b0 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 ame, . int eTex
399c0 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 20 2a 70 tRep, . void *p
399d0 41 72 67 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d Arg,. int(*xCom
399e0 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c pare)(void*,int,
399f0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c const void*,int,
39a00 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a const void*).);.
39a10 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
39a20 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
39a30 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a 20 20 73 71 llation_v2(. sq
39a40 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 lite3*, . const
39a50 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 char *zName, .
39a60 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a int eTextRep, .
39a70 20 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 void *pArg,.
39a80 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 int(*xCompare)(v
39a90 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 oid*,int,const v
39aa0 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 oid*,int,const v
39ab0 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 oid*),. void(*x
39ac0 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a Destroy)(void*).
39ad0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
39ae0 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 t sqlite3_create
39af0 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 _collation16(.
39b00 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e sqlite3*, . con
39b10 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a st void *zName,.
39b20 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 int eTextRep,
39b30 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 . void *pArg,.
39b40 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 int(*xCompare)(
39b50 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 void*,int,const
39b60 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 void*,int,const
39b70 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a void*).);../*.**
39b80 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c 6c 61 CAPI3REF: Colla
39b90 74 69 6f 6e 20 4e 65 65 64 65 64 20 43 61 6c 6c tion Needed Call
39ba0 62 61 63 6b 73 0a 2a 2a 0a 2a 2a 20 5e 54 6f 20 backs.**.** ^To
39bb0 61 76 6f 69 64 20 68 61 76 69 6e 67 20 74 6f 20 avoid having to
39bc0 72 65 67 69 73 74 65 72 20 61 6c 6c 20 63 6f 6c register all col
39bd0 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 lation sequences
39be0 20 62 65 66 6f 72 65 20 61 20 64 61 74 61 62 61 before a databa
39bf0 73 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 se.** can be use
39c00 64 2c 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c d, a single call
39c10 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6d 61 back function ma
39c20 79 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 y be registered
39c30 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b 64 61 74 with the.** [dat
39c40 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
39c50 5d 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 ] to be invoked
39c60 77 68 65 6e 65 76 65 72 20 61 6e 20 75 6e 64 65 whenever an unde
39c70 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 0a fined collation.
39c80 2a 2a 20 73 65 71 75 65 6e 63 65 20 69 73 20 72 ** sequence is r
39c90 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e equired..**.** ^
39ca0 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 If the function
39cb0 69 73 20 72 65 67 69 73 74 65 72 65 64 20 75 73 is registered us
39cc0 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f ing the sqlite3_
39cd0 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 collation_needed
39ce0 28 29 20 41 50 49 2c 0a 2a 2a 20 74 68 65 6e 20 () API,.** then
39cf0 69 74 20 69 73 20 70 61 73 73 65 64 20 74 68 65 it is passed the
39d00 20 6e 61 6d 65 73 20 6f 66 20 75 6e 64 65 66 69 names of undefi
39d10 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 ned collation se
39d20 71 75 65 6e 63 65 73 20 61 73 20 73 74 72 69 6e quences as strin
39d30 67 73 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e gs.** encoded in
39d40 20 55 54 46 2d 38 2e 20 5e 49 66 20 73 71 6c 69 UTF-8. ^If sqli
39d50 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 te3_collation_ne
39d60 65 64 65 64 31 36 28 29 20 69 73 20 75 73 65 64 eded16() is used
39d70 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 61 ,.** the names a
39d80 72 65 20 70 61 73 73 65 64 20 61 73 20 55 54 46 re passed as UTF
39d90 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20 6e -16 in machine n
39da0 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 ative byte order
39db0 2e 0a 2a 2a 20 5e 41 20 63 61 6c 6c 20 74 6f 20 ..** ^A call to
39dc0 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 either function
39dd0 72 65 70 6c 61 63 65 73 20 74 68 65 20 65 78 69 replaces the exi
39de0 73 74 69 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 2d sting collation-
39df0 6e 65 65 64 65 64 20 63 61 6c 6c 62 61 63 6b 2e needed callback.
39e00 0a 2a 2a 0a 2a 2a 20 5e 28 57 68 65 6e 20 74 68 .**.** ^(When th
39e10 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e e callback is in
39e20 76 6f 6b 65 64 2c 20 74 68 65 20 66 69 72 73 74 voked, the first
39e30 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 argument passed
39e40 20 69 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 is a copy.** of
39e50 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
39e60 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f ment to sqlite3_
39e70 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 collation_needed
39e80 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 () or.** sqlite3
39e90 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 _collation_neede
39ea0 64 31 36 28 29 2e 20 20 54 68 65 20 73 65 63 6f d16(). The seco
39eb0 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 nd argument is t
39ec0 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 he database.** c
39ed0 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 onnection. The
39ee0 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 third argument i
39ef0 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 s one of [SQLITE
39f00 5f 55 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f _UTF8], [SQLITE_
39f10 55 54 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 UTF16BE],.** or
39f20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d [SQLITE_UTF16LE]
39f30 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 , indicating the
39f40 20 6d 6f 73 74 20 64 65 73 69 72 61 62 6c 65 20 most desirable
39f50 66 6f 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c form of the coll
39f60 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 ation.** sequenc
39f70 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 e function requi
39f80 72 65 64 2e 20 20 54 68 65 20 66 6f 75 72 74 68 red. The fourth
39f90 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 parameter is th
39fa0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a e name of the.**
39fb0 20 72 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 required collat
39fc0 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 29 5e 0a ion sequence.)^.
39fd0 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 **.** The callba
39fe0 63 6b 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 ck function shou
39ff0 6c 64 20 72 65 67 69 73 74 65 72 20 74 68 65 20 ld register the
3a000 64 65 73 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f desired collatio
3a010 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 n using.** [sqli
3a020 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 te3_create_colla
3a030 74 69 6f 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 tion()], [sqlite
3a040 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 3_create_collati
3a050 6f 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b on16()], or.** [
3a060 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
3a070 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a ollation_v2()]..
3a080 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
3a090 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 t sqlite3_collat
3a0a0 69 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 ion_needed(. sq
3a0b0 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a lite3*, . void*
3a0c0 2c 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 , . void(*)(voi
3a0d0 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 d*,sqlite3*,int
3a0e0 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 eTextRep,const c
3a0f0 68 61 72 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f har*).);.SQLITE_
3a100 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
3a110 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 collation_needed
3a120 31 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 16(. sqlite3*,
3a130 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 . void*,. void
3a140 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 (*)(void*,sqlite
3a150 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 3*,int eTextRep,
3a160 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a const void*).);.
3a170 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 .#ifdef SQLITE_H
3a180 41 53 5f 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53 AS_CODEC./*.** S
3a190 70 65 63 69 66 79 20 74 68 65 20 6b 65 79 20 66 pecify the key f
3a1a0 6f 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 or an encrypted
3a1b0 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 database. This
3a1c0 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 routine should b
3a1d0 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69 67 68 e.** called righ
3a1e0 74 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f t after sqlite3_
3a1f0 6f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 open()..**.** Th
3a200 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d e code to implem
3a210 65 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20 ent this API is
3a220 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e not available in
3a230 20 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 the public rele
3a240 61 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 ase.** of SQLite
3a250 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
3a260 69 6e 74 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 int sqlite3_key(
3a270 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
3a280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3a290 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f /* Database to
3a2a0 20 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 be rekeyed */.
3a2b0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 const void *pKe
3a2c0 79 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 y, int nKey
3a2d0 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b /* The key */.);
3a2e0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
3a2f0 68 65 20 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65 he key on an ope
3a300 6e 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 n database. If
3a310 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 the current data
3a320 62 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 base is not.** e
3a330 6e 63 72 79 70 74 65 64 2c 20 74 68 69 73 20 72 ncrypted, this r
3a340 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72 outine will encr
3a350 79 70 74 20 69 74 2e 20 20 49 66 20 70 4e 65 77 ypt it. If pNew
3a360 3d 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20 ==0 or nNew==0,
3a370 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
3a380 69 73 20 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a is decrypted..**
3a390 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 .** The code to
3a3a0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 implement this A
3a3b0 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 PI is not availa
3a3c0 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 ble in the publi
3a3d0 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 c release.** of
3a3e0 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 SQLite..*/.SQLIT
3a3f0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
3a400 33 5f 72 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 3_rekey(. sqlit
3a410 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
3a420 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
3a430 61 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 abase to be reke
3a440 79 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 yed */. const v
3a450 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e oid *pKey, int n
3a460 4b 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6e Key /* The n
3a470 65 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a ew key */.);../*
3a480 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 68 65 20 .** Specify the
3a490 61 63 74 69 76 61 74 69 6f 6e 20 6b 65 79 20 66 activation key f
3a4a0 6f 72 20 61 20 53 45 45 20 64 61 74 61 62 61 73 or a SEE databas
3a4b0 65 2e 20 20 55 6e 6c 65 73 73 20 0a 2a 2a 20 61 e. Unless .** a
3a4c0 63 74 69 76 61 74 65 64 2c 20 6e 6f 6e 65 20 6f ctivated, none o
3a4d0 66 20 74 68 65 20 53 45 45 20 72 6f 75 74 69 6e f the SEE routin
3a4e0 65 73 20 77 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f es will work..*/
3a4f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
3a500 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61 74 sqlite3_activat
3a510 65 5f 73 65 65 28 0a 20 20 63 6f 6e 73 74 20 63 e_see(. const c
3a520 68 61 72 20 2a 7a 50 61 73 73 50 68 72 61 73 65 har *zPassPhrase
3a530 20 20 20 20 20 20 20 20 2f 2a 20 41 63 74 69 76 /* Activ
3a540 61 74 69 6f 6e 20 70 68 72 61 73 65 20 2a 2f 0a ation phrase */.
3a550 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 );.#endif..#ifde
3a560 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
3a570 43 45 52 4f 44 0a 2f 2a 0a 2a 2a 20 53 70 65 63 CEROD./*.** Spec
3a580 69 66 79 20 74 68 65 20 61 63 74 69 76 61 74 69 ify the activati
3a590 6f 6e 20 6b 65 79 20 66 6f 72 20 61 20 43 45 52 on key for a CER
3a5a0 4f 44 20 64 61 74 61 62 61 73 65 2e 20 20 55 6e OD database. Un
3a5b0 6c 65 73 73 20 0a 2a 2a 20 61 63 74 69 76 61 74 less .** activat
3a5c0 65 64 2c 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 ed, none of the
3a5d0 43 45 52 4f 44 20 72 6f 75 74 69 6e 65 73 20 77 CEROD routines w
3a5e0 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a 53 51 4c ill work..*/.SQL
3a5f0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
3a600 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65 ite3_activate_ce
3a610 72 6f 64 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 rod(. const cha
3a620 72 20 2a 7a 50 61 73 73 50 68 72 61 73 65 20 20 r *zPassPhrase
3a630 20 20 20 20 20 20 2f 2a 20 41 63 74 69 76 61 74 /* Activat
3a640 69 6f 6e 20 70 68 72 61 73 65 20 2a 2f 0a 29 3b ion phrase */.);
3a650 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
3a660 41 50 49 33 52 45 46 3a 20 53 75 73 70 65 6e 64 API3REF: Suspend
3a670 20 45 78 65 63 75 74 69 6f 6e 20 46 6f 72 20 41 Execution For A
3a680 20 53 68 6f 72 74 20 54 69 6d 65 0a 2a 2a 0a 2a Short Time.**.*
3a690 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6c * The sqlite3_sl
3a6a0 65 65 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 eep() function c
3a6b0 61 75 73 65 73 20 74 68 65 20 63 75 72 72 65 6e auses the curren
3a6c0 74 20 74 68 72 65 61 64 20 74 6f 20 73 75 73 70 t thread to susp
3a6d0 65 6e 64 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a end execution.**
3a6e0 20 66 6f 72 20 61 74 20 6c 65 61 73 74 20 61 20 for at least a
3a6f0 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 number of millis
3a700 65 63 6f 6e 64 73 20 73 70 65 63 69 66 69 65 64 econds specified
3a710 20 69 6e 20 69 74 73 20 70 61 72 61 6d 65 74 65 in its paramete
3a720 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 r..**.** If the
3a730 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
3a740 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 does not suppor
3a750 74 20 73 6c 65 65 70 20 72 65 71 75 65 73 74 73 t sleep requests
3a760 20 77 69 74 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 with.** millise
3a770 63 6f 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 cond time resolu
3a780 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 tion, then the t
3a790 69 6d 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e ime will be roun
3a7a0 64 65 64 20 75 70 20 74 6f 0a 2a 2a 20 74 68 65 ded up to.** the
3a7b0 20 6e 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e nearest second.
3a7c0 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d The number of m
3a7d0 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 illiseconds of s
3a7e0 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a leep actually.**
3a7f0 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 requested from
3a800 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
3a810 73 74 65 6d 20 69 73 20 72 65 74 75 72 6e 65 64 stem is returned
3a820 2e 0a 2a 2a 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 ..**.** ^SQLite
3a830 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 69 73 20 implements this
3a840 69 6e 74 65 72 66 61 63 65 20 62 79 20 63 61 6c interface by cal
3a850 6c 69 6e 67 20 74 68 65 20 78 53 6c 65 65 70 28 ling the xSleep(
3a860 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 ).** method of t
3a870 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 he default [sqli
3a880 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e te3_vfs] object.
3a890 20 20 49 66 20 74 68 65 20 78 53 6c 65 65 70 28 If the xSleep(
3a8a0 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 ) method.** of t
3a8b0 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 he default VFS i
3a8c0 73 20 6e 6f 74 20 69 6d 70 6c 65 6d 65 6e 74 65 s not implemente
3a8d0 64 20 63 6f 72 72 65 63 74 6c 79 2c 20 6f 72 20 d correctly, or
3a8e0 6e 6f 74 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 not implemented
3a8f0 61 74 0a 2a 2a 20 61 6c 6c 2c 20 74 68 65 6e 20 at.** all, then
3a900 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 the behavior of
3a910 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 29 20 sqlite3_sleep()
3a920 6d 61 79 20 64 65 76 69 61 74 65 20 66 72 6f 6d may deviate from
3a930 20 74 68 65 20 64 65 73 63 72 69 70 74 69 6f 6e the description
3a940 0a 2a 2a 20 69 6e 20 74 68 65 20 70 72 65 76 69 .** in the previ
3a950 6f 75 73 20 70 61 72 61 67 72 61 70 68 73 2e 0a ous paragraphs..
3a960 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
3a970 74 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 t sqlite3_sleep(
3a980 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 int);../*.** CAP
3a990 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 54 I3REF: Name Of T
3a9a0 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c 64 69 6e he Folder Holdin
3a9b0 67 20 54 65 6d 70 6f 72 61 72 79 20 46 69 6c 65 g Temporary File
3a9c0 73 0a 2a 2a 0a 2a 2a 20 5e 28 49 66 20 74 68 69 s.**.** ^(If thi
3a9d0 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c s global variabl
3a9e0 65 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 e is made to poi
3a9f0 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77 nt to a string w
3aa00 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 6e hich is.** the n
3aa10 61 6d 65 20 6f 66 20 61 20 66 6f 6c 64 65 72 20 ame of a folder
3aa20 28 61 2e 6b 2e 61 2e 20 64 69 72 65 63 74 6f 72 (a.k.a. director
3aa30 79 29 2c 20 74 68 65 6e 20 61 6c 6c 20 74 65 6d y), then all tem
3aa40 70 6f 72 61 72 79 20 66 69 6c 65 73 0a 2a 2a 20 porary files.**
3aa50 63 72 65 61 74 65 64 20 62 79 20 53 51 4c 69 74 created by SQLit
3aa60 65 20 77 68 65 6e 20 75 73 69 6e 67 20 61 20 62 e when using a b
3aa70 75 69 6c 74 2d 69 6e 20 5b 73 71 6c 69 74 65 33 uilt-in [sqlite3
3aa80 5f 76 66 73 20 7c 20 56 46 53 5d 0a 2a 2a 20 77 _vfs | VFS].** w
3aa90 69 6c 6c 20 62 65 20 70 6c 61 63 65 64 20 69 6e ill be placed in
3aaa0 20 74 68 61 74 20 64 69 72 65 63 74 6f 72 79 2e that directory.
3aab0 29 5e 20 20 5e 49 66 20 74 68 69 73 20 76 61 72 )^ ^If this var
3aac0 69 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 4e 55 iable.** is a NU
3aad0 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e LL pointer, then
3aae0 20 53 51 4c 69 74 65 20 70 65 72 66 6f 72 6d 73 SQLite performs
3aaf0 20 61 20 73 65 61 72 63 68 20 66 6f 72 20 61 6e a search for an
3ab00 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 appropriate.**
3ab10 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64 temporary file d
3ab20 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 irectory..**.**
3ab30 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 It is not safe t
3ab40 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64 69 66 79 o read or modify
3ab50 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 this variable i
3ab60 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 0a n more than one.
3ab70 2a 2a 20 74 68 72 65 61 64 20 61 74 20 61 20 74 ** thread at a t
3ab80 69 6d 65 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 ime. It is not
3ab90 73 61 66 65 20 74 6f 20 72 65 61 64 20 6f 72 20 safe to read or
3aba0 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61 72 69 modify this vari
3abb0 61 62 6c 65 0a 2a 2a 20 69 66 20 61 20 5b 64 61 able.** if a [da
3abc0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3abd0 6e 5d 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 n] is being used
3abe0 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
3abf0 65 20 69 6e 20 61 20 73 65 70 61 72 61 74 65 0a e in a separate.
3ac00 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 20 49 74 ** thread..** It
3ac10 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 68 61 is intended tha
3ac20 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 t this variable
3ac30 62 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a 20 61 be set once.** a
3ac40 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73 s part of proces
3ac50 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e s initialization
3ac60 20 61 6e 64 20 62 65 66 6f 72 65 20 61 6e 79 20 and before any
3ac70 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 SQLite interface
3ac80 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68 61 76 .** routines hav
3ac90 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 61 6e e been called an
3aca0 64 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69 d that this vari
3acb0 61 62 6c 65 20 72 65 6d 61 69 6e 20 75 6e 63 68 able remain unch
3acc0 61 6e 67 65 64 0a 2a 2a 20 74 68 65 72 65 61 66 anged.** thereaf
3acd0 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 ter..**.** ^The
3ace0 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 [temp_store_dire
3acf0 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 6d 61 ctory pragma] ma
3ad00 79 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61 y modify this va
3ad10 72 69 61 62 6c 65 20 61 6e 64 20 63 61 75 73 65 riable and cause
3ad20 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69 6e 74 20 .** it to point
3ad30 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e to memory obtain
3ad40 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 ed from [sqlite3
3ad50 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 5e 46 75 72 74 _malloc]. ^Furt
3ad60 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 68 65 20 hermore,.** the
3ad70 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 [temp_store_dire
3ad80 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 61 6c ctory pragma] al
3ad90 77 61 79 73 20 61 73 73 75 6d 65 73 20 74 68 61 ways assumes tha
3ada0 74 20 61 6e 79 20 73 74 72 69 6e 67 0a 2a 2a 20 t any string.**
3adb0 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61 62 that this variab
3adc0 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 le points to is
3add0 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f held in memory o
3ade0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0a 2a 2a btained from .**
3adf0 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
3ae00 5d 20 61 6e 64 20 74 68 65 20 70 72 61 67 6d 61 ] and the pragma
3ae10 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f 20 may attempt to
3ae20 66 72 65 65 20 74 68 61 74 20 6d 65 6d 6f 72 79 free that memory
3ae30 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 .** using [sqlit
3ae40 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 48 65 6e e3_free]..** Hen
3ae50 63 65 2c 20 69 66 20 74 68 69 73 20 76 61 72 69 ce, if this vari
3ae60 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 69 65 64 able is modified
3ae70 20 64 69 72 65 63 74 6c 79 2c 20 65 69 74 68 65 directly, eithe
3ae80 72 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a 2a r it should be.*
3ae90 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f 72 20 6d * made NULL or m
3aea0 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 ade to point to
3aeb0 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 memory obtained
3aec0 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 from [sqlite3_ma
3aed0 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 6c 73 65 lloc].** or else
3aee0 20 74 68 65 20 75 73 65 20 6f 66 20 74 68 65 20 the use of the
3aef0 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 [temp_store_dire
3af00 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 73 68 ctory pragma] sh
3af10 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 65 64 2e ould be avoided.
3af20 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 65 20 74 .**.** <b>Note t
3af30 6f 20 57 69 6e 64 6f 77 73 20 52 75 6e 74 69 6d o Windows Runtim
3af40 65 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20 54 68 e users:</b> Th
3af50 65 20 74 65 6d 70 6f 72 61 72 79 20 64 69 72 65 e temporary dire
3af60 63 74 6f 72 79 20 6d 75 73 74 20 62 65 20 73 65 ctory must be se
3af70 74 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 63 61 t.** prior to ca
3af80 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f lling [sqlite3_o
3af90 70 65 6e 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 pen] or [sqlite3
3afa0 5f 6f 70 65 6e 5f 76 32 5d 2e 20 20 4f 74 68 65 _open_v2]. Othe
3afb0 72 77 69 73 65 2c 20 76 61 72 69 6f 75 73 0a 2a rwise, various.*
3afc0 2a 20 66 65 61 74 75 72 65 73 20 74 68 61 74 20 * features that
3afd0 72 65 71 75 69 72 65 20 74 68 65 20 75 73 65 20 require the use
3afe0 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c of temporary fil
3aff0 65 73 20 6d 61 79 20 66 61 69 6c 2e 20 20 48 65 es may fail. He
3b000 72 65 20 69 73 20 61 6e 0a 2a 2a 20 65 78 61 6d re is an.** exam
3b010 70 6c 65 20 6f 66 20 68 6f 77 20 74 6f 20 64 6f ple of how to do
3b020 20 74 68 69 73 20 75 73 69 6e 67 20 43 2b 2b 20 this using C++
3b030 77 69 74 68 20 74 68 65 20 57 69 6e 64 6f 77 73 with the Windows
3b040 20 52 75 6e 74 69 6d 65 3a 0a 2a 2a 0a 2a 2a 20 Runtime:.**.**
3b050 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 <blockquote><pre
3b060 3e 0a 2a 2a 20 4c 50 43 57 53 54 52 20 7a 50 61 >.** LPCWSTR zPa
3b070 74 68 20 3d 20 57 69 6e 64 6f 77 73 3a 3a 53 74 th = Windows::St
3b080 6f 72 61 67 65 3a 3a 41 70 70 6c 69 63 61 74 69 orage::Applicati
3b090 6f 6e 44 61 74 61 3a 3a 43 75 72 72 65 6e 74 2d onData::Current-
3b0a0 3e 0a 2a 2a 20 26 6e 62 73 70 3b 20 20 20 20 20 >.**
3b0b0 54 65 6d 70 6f 72 61 72 79 46 6f 6c 64 65 72 2d TemporaryFolder-
3b0c0 3e 50 61 74 68 2d 3e 44 61 74 61 28 29 3b 0a 2a >Path->Data();.*
3b0d0 2a 20 63 68 61 72 20 7a 50 61 74 68 42 75 66 26 * char zPathBuf&
3b0e0 23 39 31 3b 4d 41 58 5f 50 41 54 48 20 2b 20 31 #91;MAX_PATH + 1
3b0f0 26 23 39 33 3b 3b 0a 2a 2a 20 6d 65 6d 73 65 74 ];.** memset
3b100 28 7a 50 61 74 68 42 75 66 2c 20 30 2c 20 73 69 (zPathBuf, 0, si
3b110 7a 65 6f 66 28 7a 50 61 74 68 42 75 66 29 29 3b zeof(zPathBuf));
3b120 0a 2a 2a 20 57 69 64 65 43 68 61 72 54 6f 4d 75 .** WideCharToMu
3b130 6c 74 69 42 79 74 65 28 43 50 5f 55 54 46 38 2c ltiByte(CP_UTF8,
3b140 20 30 2c 20 7a 50 61 74 68 2c 20 2d 31 2c 20 7a 0, zPath, -1, z
3b150 50 61 74 68 42 75 66 2c 20 73 69 7a 65 6f 66 28 PathBuf, sizeof(
3b160 7a 50 61 74 68 42 75 66 29 2c 0a 2a 2a 20 26 6e zPathBuf),.** &n
3b170 62 73 70 3b 20 20 20 20 20 4e 55 4c 4c 2c 20 4e bsp; NULL, N
3b180 55 4c 4c 29 3b 0a 2a 2a 20 73 71 6c 69 74 65 33 ULL);.** sqlite3
3b190 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 _temp_directory
3b1a0 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 = sqlite3_mprint
3b1b0 66 28 22 25 73 22 2c 20 7a 50 61 74 68 42 75 66 f("%s", zPathBuf
3b1c0 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c );.** </pre></bl
3b1d0 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2f 0a 53 51 4c ockquote>.*/.SQL
3b1e0 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 ITE_API char *sq
3b1f0 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 lite3_temp_direc
3b200 74 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 tory;../*.** CAP
3b210 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 54 I3REF: Name Of T
3b220 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c 64 69 6e he Folder Holdin
3b230 67 20 44 61 74 61 62 61 73 65 20 46 69 6c 65 73 g Database Files
3b240 0a 2a 2a 0a 2a 2a 20 5e 28 49 66 20 74 68 69 73 .**.** ^(If this
3b250 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
3b260 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e is made to poin
3b270 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77 68 t to a string wh
3b280 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 6e 61 ich is.** the na
3b290 6d 65 20 6f 66 20 61 20 66 6f 6c 64 65 72 20 28 me of a folder (
3b2a0 61 2e 6b 2e 61 2e 20 64 69 72 65 63 74 6f 72 79 a.k.a. directory
3b2b0 29 2c 20 74 68 65 6e 20 61 6c 6c 20 64 61 74 61 ), then all data
3b2c0 62 61 73 65 20 66 69 6c 65 73 0a 2a 2a 20 73 70 base files.** sp
3b2d0 65 63 69 66 69 65 64 20 77 69 74 68 20 61 20 72 ecified with a r
3b2e0 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 elative pathname
3b2f0 20 61 6e 64 20 63 72 65 61 74 65 64 20 6f 72 20 and created or
3b300 61 63 63 65 73 73 65 64 20 62 79 0a 2a 2a 20 53 accessed by.** S
3b310 51 4c 69 74 65 20 77 68 65 6e 20 75 73 69 6e 67 QLite when using
3b320 20 61 20 62 75 69 6c 74 2d 69 6e 20 77 69 6e 64 a built-in wind
3b330 6f 77 73 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 ows [sqlite3_vfs
3b340 20 7c 20 56 46 53 5d 20 77 69 6c 6c 20 62 65 20 | VFS] will be
3b350 61 73 73 75 6d 65 64 0a 2a 2a 20 74 6f 20 62 65 assumed.** to be
3b360 20 72 65 6c 61 74 69 76 65 20 74 6f 20 74 68 61 relative to tha
3b370 74 20 64 69 72 65 63 74 6f 72 79 2e 29 5e 20 5e t directory.)^ ^
3b380 49 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 If this variable
3b390 20 69 73 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f is a NULL.** po
3b3a0 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 inter, then SQLi
3b3b0 74 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 te assumes that
3b3c0 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c all database fil
3b3d0 65 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 es specified.**
3b3e0 77 69 74 68 20 61 20 72 65 6c 61 74 69 76 65 20 with a relative
3b3f0 70 61 74 68 6e 61 6d 65 20 61 72 65 20 72 65 6c pathname are rel
3b400 61 74 69 76 65 20 74 6f 20 74 68 65 20 63 75 72 ative to the cur
3b410 72 65 6e 74 20 64 69 72 65 63 74 6f 72 79 0a 2a rent directory.*
3b420 2a 20 66 6f 72 20 74 68 65 20 70 72 6f 63 65 73 * for the proces
3b430 73 2e 20 20 4f 6e 6c 79 20 74 68 65 20 77 69 6e s. Only the win
3b440 64 6f 77 73 20 56 46 53 20 6d 61 6b 65 73 20 75 dows VFS makes u
3b450 73 65 20 6f 66 20 74 68 69 73 20 67 6c 6f 62 61 se of this globa
3b460 6c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 3b 20 69 l.** variable; i
3b470 74 20 69 73 20 69 67 6e 6f 72 65 64 20 62 79 20 t is ignored by
3b480 74 68 65 20 75 6e 69 78 20 56 46 53 2e 0a 2a 2a the unix VFS..**
3b490 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 65 .** Changing the
3b4a0 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 76 value of this v
3b4b0 61 72 69 61 62 6c 65 20 77 68 69 6c 65 20 61 20 ariable while a
3b4c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3b4d0 69 6f 6e 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 63 ion is.** open c
3b4e0 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 61 20 63 an result in a c
3b4f0 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 2e orrupt database.
3b500 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 .**.** It is not
3b510 20 73 61 66 65 20 74 6f 20 72 65 61 64 20 6f 72 safe to read or
3b520 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61 72 modify this var
3b530 69 61 62 6c 65 20 69 6e 20 6d 6f 72 65 20 74 68 iable in more th
3b540 61 6e 20 6f 6e 65 0a 2a 2a 20 74 68 72 65 61 64 an one.** thread
3b550 20 61 74 20 61 20 74 69 6d 65 2e 20 20 49 74 20 at a time. It
3b560 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 is not safe to r
3b570 65 61 64 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 ead or modify th
3b580 69 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 is variable.** i
3b590 66 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f f a [database co
3b5a0 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 nnection] is bei
3b5b0 6e 67 20 75 73 65 64 20 61 74 20 74 68 65 20 73 ng used at the s
3b5c0 61 6d 65 20 74 69 6d 65 20 69 6e 20 61 20 73 65 ame time in a se
3b5d0 70 61 72 61 74 65 0a 2a 2a 20 74 68 72 65 61 64 parate.** thread
3b5e0 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6e 74 65 6e ..** It is inten
3b5f0 64 65 64 20 74 68 61 74 20 74 68 69 73 20 76 61 ded that this va
3b600 72 69 61 62 6c 65 20 62 65 20 73 65 74 20 6f 6e riable be set on
3b610 63 65 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 ce.** as part of
3b620 20 70 72 6f 63 65 73 73 20 69 6e 69 74 69 61 6c process initial
3b630 69 7a 61 74 69 6f 6e 20 61 6e 64 20 62 65 66 6f ization and befo
3b640 72 65 20 61 6e 79 20 53 51 4c 69 74 65 20 69 6e re any SQLite in
3b650 74 65 72 66 61 63 65 0a 2a 2a 20 72 6f 75 74 69 terface.** routi
3b660 6e 65 73 20 68 61 76 65 20 62 65 65 6e 20 63 61 nes have been ca
3b670 6c 6c 65 64 20 61 6e 64 20 74 68 61 74 20 74 68 lled and that th
3b680 69 73 20 76 61 72 69 61 62 6c 65 20 72 65 6d 61 is variable rema
3b690 69 6e 20 75 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 in unchanged.**
3b6a0 74 68 65 72 65 61 66 74 65 72 2e 0a 2a 2a 0a 2a thereafter..**.*
3b6b0 2a 20 5e 54 68 65 20 5b 64 61 74 61 5f 73 74 6f * ^The [data_sto
3b6c0 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70 72 61 re_directory pra
3b6d0 67 6d 61 5d 20 6d 61 79 20 6d 6f 64 69 66 79 20 gma] may modify
3b6e0 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 61 6e this variable an
3b6f0 64 20 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 6f d cause.** it to
3b700 20 70 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 point to memory
3b710 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b obtained from [
3b720 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 2e sqlite3_malloc].
3b730 20 20 5e 46 75 72 74 68 65 72 6d 6f 72 65 2c 0a ^Furthermore,.
3b740 2a 2a 20 74 68 65 20 5b 64 61 74 61 5f 73 74 6f ** the [data_sto
3b750 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70 72 61 re_directory pra
3b760 67 6d 61 5d 20 61 6c 77 61 79 73 20 61 73 73 75 gma] always assu
3b770 6d 65 73 20 74 68 61 74 20 61 6e 79 20 73 74 72 mes that any str
3b780 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 ing.** that this
3b790 20 76 61 72 69 61 62 6c 65 20 70 6f 69 6e 74 73 variable points
3b7a0 20 74 6f 20 69 73 20 68 65 6c 64 20 69 6e 20 6d to is held in m
3b7b0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
3b7c0 72 6f 6d 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 rom .** [sqlite3
3b7d0 5f 6d 61 6c 6c 6f 63 5d 20 61 6e 64 20 74 68 65 _malloc] and the
3b7e0 20 70 72 61 67 6d 61 20 6d 61 79 20 61 74 74 65 pragma may atte
3b7f0 6d 70 74 20 74 6f 20 66 72 65 65 20 74 68 61 74 mpt to free that
3b800 20 6d 65 6d 6f 72 79 0a 2a 2a 20 75 73 69 6e 67 memory.** using
3b810 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5d 2e [sqlite3_free].
3b820 0a 2a 2a 20 48 65 6e 63 65 2c 20 69 66 20 74 68 .** Hence, if th
3b830 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 6d is variable is m
3b840 6f 64 69 66 69 65 64 20 64 69 72 65 63 74 6c 79 odified directly
3b850 2c 20 65 69 74 68 65 72 20 69 74 20 73 68 6f 75 , either it shou
3b860 6c 64 20 62 65 0a 2a 2a 20 6d 61 64 65 20 4e 55 ld be.** made NU
3b870 4c 4c 20 6f 72 20 6d 61 64 65 20 74 6f 20 70 6f LL or made to po
3b880 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 int to memory ob
3b890 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c tained from [sql
3b8a0 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 0a 2a 2a 20 ite3_malloc].**
3b8b0 6f 72 20 65 6c 73 65 20 74 68 65 20 75 73 65 20 or else the use
3b8c0 6f 66 20 74 68 65 20 5b 64 61 74 61 5f 73 74 6f of the [data_sto
3b8d0 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70 72 61 re_directory pra
3b8e0 67 6d 61 5d 20 73 68 6f 75 6c 64 20 62 65 20 61 gma] should be a
3b8f0 76 6f 69 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 voided..*/.SQLIT
3b900 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 E_API char *sqli
3b910 74 65 33 5f 64 61 74 61 5f 64 69 72 65 63 74 6f te3_data_directo
3b920 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 ry;../*.** CAPI3
3b930 52 45 46 3a 20 54 65 73 74 20 46 6f 72 20 41 75 REF: Test For Au
3b940 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f 64 65 0a 2a to-Commit Mode.*
3b950 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 75 74 * KEYWORDS: {aut
3b960 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 7d 0a 2a 2a ocommit mode}.**
3b970 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 .** ^The sqlite3
3b980 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 _get_autocommit(
3b990 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ) interface retu
3b9a0 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a rns non-zero or.
3b9b0 2a 2a 20 7a 65 72 6f 20 69 66 20 74 68 65 20 67 ** zero if the g
3b9c0 69 76 65 6e 20 64 61 74 61 62 61 73 65 20 63 6f iven database co
3b9d0 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 72 20 69 nnection is or i
3b9e0 73 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f 6d s not in autocom
3b9f0 6d 69 74 20 6d 6f 64 65 2c 0a 2a 2a 20 72 65 73 mit mode,.** res
3ba00 70 65 63 74 69 76 65 6c 79 2e 20 20 5e 41 75 74 pectively. ^Aut
3ba10 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 ocommit mode is
3ba20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a on by default..*
3ba30 2a 20 5e 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f * ^Autocommit mo
3ba40 64 65 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 de is disabled b
3ba50 79 20 61 20 5b 42 45 47 49 4e 5d 20 73 74 61 74 y a [BEGIN] stat
3ba60 65 6d 65 6e 74 2e 0a 2a 2a 20 5e 41 75 74 6f 63 ement..** ^Autoc
3ba70 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 72 65 ommit mode is re
3ba80 2d 65 6e 61 62 6c 65 64 20 62 79 20 61 20 5b 43 -enabled by a [C
3ba90 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c 4c 42 OMMIT] or [ROLLB
3baa0 41 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 ACK]..**.** If c
3bab0 65 72 74 61 69 6e 20 6b 69 6e 64 73 20 6f 66 20 ertain kinds of
3bac0 65 72 72 6f 72 73 20 6f 63 63 75 72 20 6f 6e 20 errors occur on
3bad0 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 a statement with
3bae0 69 6e 20 61 20 6d 75 6c 74 69 2d 73 74 61 74 65 in a multi-state
3baf0 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 ment.** transact
3bb00 69 6f 6e 20 28 65 72 72 6f 72 73 20 69 6e 63 6c ion (errors incl
3bb10 75 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f 46 55 uding [SQLITE_FU
3bb20 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 4f 45 LL], [SQLITE_IOE
3bb30 52 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f RR],.** [SQLITE_
3bb40 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f NOMEM], [SQLITE_
3bb50 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51 4c 49 BUSY], and [SQLI
3bb60 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 29 20 74 TE_INTERRUPT]) t
3bb70 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 hen the.** trans
3bb80 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 62 65 20 action might be
3bb90 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 6f rolled back auto
3bba0 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 matically. The
3bbb0 6f 6e 6c 79 20 77 61 79 20 74 6f 0a 2a 2a 20 66 only way to.** f
3bbc0 69 6e 64 20 6f 75 74 20 77 68 65 74 68 65 72 20 ind out whether
3bbd0 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 SQLite automatic
3bbe0 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b ally rolled back
3bbf0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
3bc00 20 61 66 74 65 72 0a 2a 2a 20 61 6e 20 65 72 72 after.** an err
3bc10 6f 72 20 69 73 20 74 6f 20 75 73 65 20 74 68 69 or is to use thi
3bc20 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a s function..**.*
3bc30 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 * If another thr
3bc40 65 61 64 20 63 68 61 6e 67 65 73 20 74 68 65 20 ead changes the
3bc50 61 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75 autocommit statu
3bc60 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 s of the databas
3bc70 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 e.** connection
3bc80 77 68 69 6c 65 20 74 68 69 73 20 72 6f 75 74 69 while this routi
3bc90 6e 65 20 69 73 20 72 75 6e 6e 69 6e 67 2c 20 74 ne is running, t
3bca0 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 hen the return v
3bcb0 61 6c 75 65 0a 2a 2a 20 69 73 20 75 6e 64 65 66 alue.** is undef
3bcc0 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ined..*/.SQLITE_
3bcd0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
3bce0 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 73 get_autocommit(s
3bcf0 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a qlite3*);../*.**
3bd00 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64 20 CAPI3REF: Find
3bd10 54 68 65 20 44 61 74 61 62 61 73 65 20 48 61 6e The Database Han
3bd20 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72 65 dle Of A Prepare
3bd30 64 20 53 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a d Statement.**.*
3bd40 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 64 * ^The sqlite3_d
3bd50 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72 66 61 b_handle interfa
3bd60 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 5b ce returns the [
3bd70 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3bd80 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 74 ion] handle.** t
3bd90 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65 70 61 o which a [prepa
3bda0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 62 red statement] b
3bdb0 65 6c 6f 6e 67 73 2e 20 20 5e 54 68 65 20 5b 64 elongs. ^The [d
3bdc0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3bdd0 6f 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 on].** returned
3bde0 62 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 by sqlite3_db_ha
3bdf0 6e 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d 65 ndle is the same
3be00 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
3be10 63 74 69 6f 6e 5d 0a 2a 2a 20 74 68 61 74 20 77 ction].** that w
3be20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 as the first arg
3be30 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 ument.** to the
3be40 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
3be50 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28 6f 72 20 _v2()] call (or
3be60 69 74 73 20 76 61 72 69 61 6e 74 73 29 20 74 68 its variants) th
3be70 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 0a 2a at was used to.*
3be80 2a 20 63 72 65 61 74 65 20 74 68 65 20 73 74 61 * create the sta
3be90 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 66 69 tement in the fi
3bea0 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2f 0a 53 51 rst place..*/.SQ
3beb0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
3bec0 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e *sqlite3_db_han
3bed0 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 dle(sqlite3_stmt
3bee0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
3bef0 52 45 46 3a 20 52 65 74 75 72 6e 20 54 68 65 20 REF: Return The
3bf00 46 69 6c 65 6e 61 6d 65 20 46 6f 72 20 41 20 44 Filename For A D
3bf10 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 atabase Connecti
3bf20 6f 6e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 on.**.** ^The sq
3bf30 6c 69 74 65 33 5f 64 62 5f 66 69 6c 65 6e 61 6d lite3_db_filenam
3bf40 65 28 44 2c 4e 29 20 69 6e 74 65 72 66 61 63 65 e(D,N) interface
3bf50 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
3bf60 65 72 20 74 6f 20 61 20 66 69 6c 65 6e 61 6d 65 er to a filename
3bf70 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 .** associated w
3bf80 69 74 68 20 64 61 74 61 62 61 73 65 20 4e 20 6f ith database N o
3bf90 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 20 f connection D.
3bfa0 20 5e 54 68 65 20 6d 61 69 6e 20 64 61 74 61 62 ^The main datab
3bfb0 61 73 65 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 ase file.** has
3bfc0 74 68 65 20 6e 61 6d 65 20 22 6d 61 69 6e 22 2e the name "main".
3bfd0 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f If there is no
3bfe0 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 attached databa
3bff0 73 65 20 4e 20 6f 6e 20 74 68 65 20 64 61 74 61 se N on the data
3c000 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 base.** connecti
3c010 6f 6e 20 44 2c 20 6f 72 20 69 66 20 64 61 74 61 on D, or if data
3c020 62 61 73 65 20 4e 20 69 73 20 61 20 74 65 6d 70 base N is a temp
3c030 6f 72 61 72 79 20 6f 72 20 69 6e 2d 6d 65 6d 6f orary or in-memo
3c040 72 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 ry database, the
3c050 6e 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e n.** a NULL poin
3c060 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ter is returned.
3c070 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 6c 65 .**.** ^The file
3c080 6e 61 6d 65 20 72 65 74 75 72 6e 65 64 20 62 79 name returned by
3c090 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
3c0a0 73 20 74 68 65 20 6f 75 74 70 75 74 20 6f 66 20 s the output of
3c0b0 74 68 65 0a 2a 2a 20 78 46 75 6c 6c 50 61 74 68 the.** xFullPath
3c0c0 6e 61 6d 65 20 6d 65 74 68 6f 64 20 6f 66 20 74 name method of t
3c0d0 68 65 20 5b 56 46 53 5d 2e 20 20 5e 49 6e 20 6f he [VFS]. ^In o
3c0e0 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 ther words, the
3c0f0 66 69 6c 65 6e 61 6d 65 0a 2a 2a 20 77 69 6c 6c filename.** will
3c100 20 62 65 20 61 6e 20 61 62 73 6f 6c 75 74 65 20 be an absolute
3c110 70 61 74 68 6e 61 6d 65 2c 20 65 76 65 6e 20 69 pathname, even i
3c120 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 75 f the filename u
3c130 73 65 64 0a 2a 2a 20 74 6f 20 6f 70 65 6e 20 74 sed.** to open t
3c140 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 69 67 he database orig
3c150 69 6e 61 6c 6c 79 20 77 61 73 20 61 20 55 52 49 inally was a URI
3c160 20 6f 72 20 72 65 6c 61 74 69 76 65 20 70 61 74 or relative pat
3c170 68 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 hname..*/.SQLITE
3c180 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
3c190 2a 73 71 6c 69 74 65 33 5f 64 62 5f 66 69 6c 65 *sqlite3_db_file
3c1a0 6e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 64 62 name(sqlite3 *db
3c1b0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 , const char *zD
3c1c0 62 4e 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 bName);../*.** C
3c1d0 41 50 49 33 52 45 46 3a 20 44 65 74 65 72 6d 69 API3REF: Determi
3c1e0 6e 65 20 69 66 20 61 20 64 61 74 61 62 61 73 65 ne if a database
3c1f0 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 0a 2a 2a is read-only.**
3c200 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 .** ^The sqlite3
3c210 5f 64 62 5f 72 65 61 64 6f 6e 6c 79 28 44 2c 4e _db_readonly(D,N
3c220 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ) interface retu
3c230 72 6e 73 20 31 20 69 66 20 74 68 65 20 64 61 74 rns 1 if the dat
3c240 61 62 61 73 65 20 4e 0a 2a 2a 20 6f 66 20 63 6f abase N.** of co
3c250 6e 6e 65 63 74 69 6f 6e 20 44 20 69 73 20 72 65 nnection D is re
3c260 61 64 2d 6f 6e 6c 79 2c 20 30 20 69 66 20 69 74 ad-only, 0 if it
3c270 20 69 73 20 72 65 61 64 2f 77 72 69 74 65 2c 20 is read/write,
3c280 6f 72 20 2d 31 20 69 66 20 4e 20 69 73 20 6e 6f or -1 if N is no
3c290 74 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 t.** the name of
3c2a0 20 61 20 64 61 74 61 62 61 73 65 20 6f 6e 20 63 a database on c
3c2b0 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 0a 2a 2f 0a onnection D..*/.
3c2c0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
3c2d0 71 6c 69 74 65 33 5f 64 62 5f 72 65 61 64 6f 6e qlite3_db_readon
3c2e0 6c 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ly(sqlite3 *db,
3c2f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e const char *zDbN
3c300 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ame);../*.** CAP
3c310 49 33 52 45 46 3a 20 46 69 6e 64 20 74 68 65 20 I3REF: Find the
3c320 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73 74 next prepared st
3c330 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 5e 54 atement.**.** ^T
3c340 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 72 65 his interface re
3c350 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
3c360 74 6f 20 74 68 65 20 6e 65 78 74 20 5b 70 72 65 to the next [pre
3c370 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
3c380 20 61 66 74 65 72 0a 2a 2a 20 70 53 74 6d 74 20 after.** pStmt
3c390 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
3c3a0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
3c3b0 6e 6e 65 63 74 69 6f 6e 5d 20 70 44 62 2e 20 20 nnection] pDb.
3c3c0 5e 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55 4c ^If pStmt is NUL
3c3d0 4c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 69 L.** then this i
3c3e0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
3c3f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
3c400 65 20 66 69 72 73 74 20 70 72 65 70 61 72 65 64 e first prepared
3c410 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 73 statement.** as
3c420 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
3c430 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
3c440 63 74 69 6f 6e 20 70 44 62 2e 20 20 5e 49 66 20 ction pDb. ^If
3c450 6e 6f 20 70 72 65 70 61 72 65 64 20 73 74 61 74 no prepared stat
3c460 65 6d 65 6e 74 0a 2a 2a 20 73 61 74 69 73 66 69 ement.** satisfi
3c470 65 73 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e es the condition
3c480 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e s of this routin
3c490 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 e, it returns NU
3c4a0 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 64 LL..**.** The [d
3c4b0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3c4c0 6f 6e 5d 20 70 6f 69 6e 74 65 72 20 44 20 69 6e on] pointer D in
3c4d0 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 a call to.** [s
3c4e0 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 qlite3_next_stmt
3c4f0 28 44 2c 53 29 5d 20 6d 75 73 74 20 72 65 66 65 (D,S)] must refe
3c500 72 20 74 6f 20 61 6e 20 6f 70 65 6e 20 64 61 74 r to an open dat
3c510 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 abase.** connect
3c520 69 6f 6e 20 61 6e 64 20 69 6e 20 70 61 72 74 69 ion and in parti
3c530 63 75 6c 61 72 20 6d 75 73 74 20 6e 6f 74 20 62 cular must not b
3c540 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 e a NULL pointer
3c550 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
3c560 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71 sqlite3_stmt *sq
3c570 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 lite3_next_stmt(
3c580 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71 sqlite3 *pDb, sq
3c590 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
3c5a0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
3c5b0 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 20 REF: Commit And
3c5c0 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 63 Rollback Notific
3c5d0 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 0a ation Callbacks.
3c5e0 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 **.** ^The sqlit
3c5f0 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 e3_commit_hook()
3c600 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 interface regis
3c610 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a ters a callback.
3c620 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 ** function to b
3c630 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 e invoked whenev
3c640 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e er a transaction
3c650 20 69 73 20 5b 43 4f 4d 4d 49 54 20 7c 20 63 6f is [COMMIT | co
3c660 6d 6d 69 74 74 65 64 5d 2e 0a 2a 2a 20 5e 41 6e mmitted]..** ^An
3c670 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 y callback set b
3c680 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c y a previous cal
3c690 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d l to sqlite3_com
3c6a0 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f mit_hook().** fo
3c6b0 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 r the same datab
3c6c0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 ase connection i
3c6d0 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a s overridden..**
3c6e0 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72 6f ^The sqlite3_ro
3c6f0 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e llback_hook() in
3c700 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65 72 terface register
3c710 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 s a callback.**
3c720 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 function to be i
3c730 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 nvoked whenever
3c740 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
3c750 20 5b 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c [ROLLBACK | rol
3c760 6c 65 64 20 62 61 63 6b 5d 2e 0a 2a 2a 20 5e 41 led back]..** ^A
3c770 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 ny callback set
3c780 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 by a previous ca
3c790 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 6f ll to sqlite3_ro
3c7a0 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 0a 2a 2a llback_hook().**
3c7b0 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 64 61 for the same da
3c7c0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3c7d0 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65 6e 2e n is overridden.
3c7e0 0a 2a 2a 20 5e 54 68 65 20 70 41 72 67 20 61 72 .** ^The pArg ar
3c7f0 67 75 6d 65 6e 74 20 69 73 20 70 61 73 73 65 64 gument is passed
3c800 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 through to the
3c810 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 5e 49 66 callback..** ^If
3c820 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e the callback on
3c830 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 66 a commit hook f
3c840 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
3c850 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 non-zero,.** the
3c860 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 69 73 20 n the commit is
3c870 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 converted into a
3c880 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a rollback..**.**
3c890 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f ^The sqlite3_co
3c8a0 6d 6d 69 74 5f 68 6f 6f 6b 28 44 2c 43 2c 50 29 mmit_hook(D,C,P)
3c8b0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 6f 6c and sqlite3_rol
3c8c0 6c 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c 43 2c 50 lback_hook(D,C,P
3c8d0 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 72 ) functions.** r
3c8e0 65 74 75 72 6e 20 74 68 65 20 50 20 61 72 67 75 eturn the P argu
3c8f0 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 72 ment from the pr
3c900 65 76 69 6f 75 73 20 63 61 6c 6c 20 6f 66 20 74 evious call of t
3c910 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e he same function
3c920 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 .** on the same
3c930 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
3c940 74 69 6f 6e 5d 20 44 2c 20 6f 72 20 4e 55 4c 4c tion] D, or NULL
3c950 20 66 6f 72 0a 2a 2a 20 74 68 65 20 66 69 72 73 for.** the firs
3c960 74 20 63 61 6c 6c 20 66 6f 72 20 65 61 63 68 20 t call for each
3c970 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 44 2e 0a 2a function on D..*
3c980 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d 6d 69 74 20 *.** The commit
3c990 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f and rollback hoo
3c9a0 6b 20 63 61 6c 6c 62 61 63 6b 73 20 61 72 65 20 k callbacks are
3c9b0 6e 6f 74 20 72 65 65 6e 74 72 61 6e 74 2e 0a 2a not reentrant..*
3c9c0 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 * The callback i
3c9d0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 mplementation mu
3c9e0 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 st not do anythi
3c9f0 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 ng that will mod
3ca00 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 ify.** the datab
3ca10 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 ase connection t
3ca20 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 hat invoked the
3ca30 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e 79 20 61 callback. Any a
3ca40 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d 6f 64 ctions.** to mod
3ca50 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73 65 ify the database
3ca60 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 connection must
3ca70 20 62 65 20 64 65 66 65 72 72 65 64 20 75 6e 74 be deferred unt
3ca80 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a 2a 20 il after the.**
3ca90 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74 68 completion of th
3caa0 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 e [sqlite3_step(
3cab0 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 )] call that tri
3cac0 67 67 65 72 65 64 20 74 68 65 20 63 6f 6d 6d 69 ggered the commi
3cad0 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62 61 63 6b t.** or rollback
3cae0 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20 66 69 72 hook in the fir
3caf0 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20 4e 6f 74 st place..** Not
3cb00 65 20 74 68 61 74 20 72 75 6e 6e 69 6e 67 20 61 e that running a
3cb10 6e 79 20 6f 74 68 65 72 20 53 51 4c 20 73 74 61 ny other SQL sta
3cb20 74 65 6d 65 6e 74 73 2c 20 69 6e 63 6c 75 64 69 tements, includi
3cb30 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d ng SELECT statem
3cb40 65 6e 74 73 2c 0a 2a 2a 20 6f 72 20 6d 65 72 65 ents,.** or mere
3cb50 6c 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 ly calling [sqli
3cb60 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
3cb70 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 ] and [sqlite3_s
3cb80 74 65 70 28 29 5d 20 77 69 6c 6c 20 6d 6f 64 69 tep()] will modi
3cb90 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 fy.** the databa
3cba0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 se connections f
3cbb0 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f or the meaning o
3cbc0 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 f "modify" in th
3cbd0 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a is paragraph..**
3cbe0 0a 2a 2a 20 5e 52 65 67 69 73 74 65 72 69 6e 67 .** ^Registering
3cbf0 20 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e a NULL function
3cc00 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 61 disables the ca
3cc10 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 5e 57 llback..**.** ^W
3cc20 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 hen the commit h
3cc30 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 ook callback rou
3cc40 74 69 6e 65 20 72 65 74 75 72 6e 73 20 7a 65 72 tine returns zer
3cc50 6f 2c 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 0a o, the [COMMIT].
3cc60 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 ** operation is
3cc70 61 6c 6c 6f 77 65 64 20 74 6f 20 63 6f 6e 74 69 allowed to conti
3cc80 6e 75 65 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 5e nue normally. ^
3cc90 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f If the commit ho
3cca0 6f 6b 0a 2a 2a 20 72 65 74 75 72 6e 73 20 6e 6f ok.** returns no
3ccb0 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 n-zero, then the
3ccc0 20 5b 43 4f 4d 4d 49 54 5d 20 69 73 20 63 6f 6e [COMMIT] is con
3ccd0 76 65 72 74 65 64 20 69 6e 74 6f 20 61 20 5b 52 verted into a [R
3cce0 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a 20 5e 54 68 OLLBACK]..** ^Th
3ccf0 65 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 e rollback hook
3cd00 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 is invoked on a
3cd10 72 6f 6c 6c 62 61 63 6b 20 74 68 61 74 20 72 65 rollback that re
3cd20 73 75 6c 74 73 20 66 72 6f 6d 20 61 20 63 6f 6d sults from a com
3cd30 6d 69 74 0a 2a 2a 20 68 6f 6f 6b 20 72 65 74 75 mit.** hook retu
3cd40 72 6e 69 6e 67 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 rning non-zero,
3cd50 6a 75 73 74 20 61 73 20 69 74 20 77 6f 75 6c 64 just as it would
3cd60 20 62 65 20 77 69 74 68 20 61 6e 79 20 6f 74 68 be with any oth
3cd70 65 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a er rollback..**.
3cd80 2a 2a 20 5e 46 6f 72 20 74 68 65 20 70 75 72 70 ** ^For the purp
3cd90 6f 73 65 73 20 6f 66 20 74 68 69 73 20 41 50 49 oses of this API
3cda0 2c 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 , a transaction
3cdb0 69 73 20 73 61 69 64 20 74 6f 20 68 61 76 65 20 is said to have
3cdc0 62 65 65 6e 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 been.** rolled b
3cdd0 61 63 6b 20 69 66 20 61 6e 20 65 78 70 6c 69 63 ack if an explic
3cde0 69 74 20 22 52 4f 4c 4c 42 41 43 4b 22 20 73 74 it "ROLLBACK" st
3cdf0 61 74 65 6d 65 6e 74 20 69 73 20 65 78 65 63 75 atement is execu
3ce00 74 65 64 2c 20 6f 72 0a 2a 2a 20 61 6e 20 65 72 ted, or.** an er
3ce10 72 6f 72 20 6f 72 20 63 6f 6e 73 74 72 61 69 6e ror or constrain
3ce20 74 20 63 61 75 73 65 73 20 61 6e 20 69 6d 70 6c t causes an impl
3ce30 69 63 69 74 20 72 6f 6c 6c 62 61 63 6b 20 74 6f icit rollback to
3ce40 20 6f 63 63 75 72 2e 0a 2a 2a 20 5e 54 68 65 20 occur..** ^The
3ce50 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 rollback callbac
3ce60 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 k is not invoked
3ce70 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f if a transactio
3ce80 6e 20 69 73 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 n is.** automati
3ce90 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 cally rolled bac
3cea0 6b 20 62 65 63 61 75 73 65 20 74 68 65 20 64 61 k because the da
3ceb0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3cec0 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a n is closed..**.
3ced0 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 ** See also the
3cee0 5b 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f [sqlite3_update_
3cef0 68 6f 6f 6b 28 29 5d 20 69 6e 74 65 72 66 61 63 hook()] interfac
3cf00 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
3cf10 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 void *sqlite3_c
3cf20 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 73 71 6c 69 74 ommit_hook(sqlit
3cf30 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 e3*, int(*)(void
3cf40 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 *), void*);.SQLI
3cf50 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
3cf60 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f ite3_rollback_ho
3cf70 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 ok(sqlite3*, voi
3cf80 64 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f d(*)(void *), vo
3cf90 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 id*);../*.** CAP
3cfa0 49 33 52 45 46 3a 20 44 61 74 61 20 43 68 61 6e I3REF: Data Chan
3cfb0 67 65 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 ge Notification
3cfc0 43 61 6c 6c 62 61 63 6b 73 0a 2a 2a 0a 2a 2a 20 Callbacks.**.**
3cfd0 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 75 70 64 ^The sqlite3_upd
3cfe0 61 74 65 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 72 ate_hook() inter
3cff0 66 61 63 65 20 72 65 67 69 73 74 65 72 73 20 61 face registers a
3d000 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 callback functi
3d010 6f 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b on.** with the [
3d020 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3d030 69 6f 6e 5d 20 69 64 65 6e 74 69 66 69 65 64 20 ion] identified
3d040 62 79 20 74 68 65 20 66 69 72 73 74 20 61 72 67 by the first arg
3d050 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 62 65 20 69 ument.** to be i
3d060 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 nvoked whenever
3d070 61 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 a row is updated
3d080 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 , inserted or de
3d090 6c 65 74 65 64 2e 0a 2a 2a 20 5e 41 6e 79 20 63 leted..** ^Any c
3d0a0 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20 61 allback set by a
3d0b0 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 previous call t
3d0c0 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a o this function.
3d0d0 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 ** for the same
3d0e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3d0f0 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65 ion is overridde
3d100 6e 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 65 n..**.** ^The se
3d110 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 cond argument is
3d120 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
3d130 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e e function to in
3d140 76 6f 6b 65 20 77 68 65 6e 20 61 0a 2a 2a 20 72 voke when a.** r
3d150 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c 20 69 ow is updated, i
3d160 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74 nserted or delet
3d170 65 64 2e 0a 2a 2a 20 5e 54 68 65 20 66 69 72 73 ed..** ^The firs
3d180 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 t argument to th
3d190 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 e callback is a
3d1a0 63 6f 70 79 20 6f 66 20 74 68 65 20 74 68 69 72 copy of the thir
3d1b0 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f d argument.** to
3d1c0 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f sqlite3_update_
3d1d0 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 5e 54 68 65 20 hook()..** ^The
3d1e0 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 63 6b 20 second callback
3d1f0 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 65 20 argument is one
3d200 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45 52 of [SQLITE_INSER
3d210 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 T], [SQLITE_DELE
3d220 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 TE],.** or [SQLI
3d230 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 65 70 65 TE_UPDATE], depe
3d240 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 65 nding on the ope
3d250 72 61 74 69 6f 6e 20 74 68 61 74 20 63 61 75 73 ration that caus
3d260 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a ed the callback.
3d270 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 ** to be invoked
3d280 2e 0a 2a 2a 20 5e 54 68 65 20 74 68 69 72 64 20 ..** ^The third
3d290 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d and fourth argum
3d2a0 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c 6c ents to the call
3d2b0 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69 back contain poi
3d2c0 6e 74 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a 20 nters to the.**
3d2d0 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 61 62 database and tab
3d2e0 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 6e 69 le name containi
3d2f0 6e 67 20 74 68 65 20 61 66 66 65 63 74 65 64 20 ng the affected
3d300 72 6f 77 2e 0a 2a 2a 20 5e 54 68 65 20 66 69 6e row..** ^The fin
3d310 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61 al callback para
3d320 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 72 6f meter is the [ro
3d330 77 69 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 2e wid] of the row.
3d340 0a 2a 2a 20 5e 49 6e 20 74 68 65 20 63 61 73 65 .** ^In the case
3d350 20 6f 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74 of an update, t
3d360 68 69 73 20 69 73 20 74 68 65 20 5b 72 6f 77 69 his is the [rowi
3d370 64 5d 20 61 66 74 65 72 20 74 68 65 20 75 70 64 d] after the upd
3d380 61 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e ate takes place.
3d390 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 75 70 64 .**.** ^(The upd
3d3a0 61 74 65 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 ate hook is not
3d3b0 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 invoked when int
3d3c0 65 72 6e 61 6c 20 73 79 73 74 65 6d 20 74 61 62 ernal system tab
3d3d0 6c 65 73 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 les are.** modif
3d3e0 69 65 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 ied (i.e. sqlite
3d3f0 5f 6d 61 73 74 65 72 20 61 6e 64 20 73 71 6c 69 _master and sqli
3d400 74 65 5f 73 65 71 75 65 6e 63 65 29 2e 29 5e 0a te_sequence).)^.
3d410 2a 2a 0a 2a 2a 20 5e 49 6e 20 74 68 65 20 63 75 **.** ^In the cu
3d420 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 rrent implementa
3d430 74 69 6f 6e 2c 20 74 68 65 20 75 70 64 61 74 65 tion, the update
3d440 20 68 6f 6f 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 hook.** is not
3d450 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 64 75 70 invoked when dup
3d460 6c 69 63 61 74 69 6f 6e 20 72 6f 77 73 20 61 72 lication rows ar
3d470 65 20 64 65 6c 65 74 65 64 20 62 65 63 61 75 73 e deleted becaus
3d480 65 20 6f 66 20 61 6e 0a 2a 2a 20 5b 4f 4e 20 43 e of an.** [ON C
3d490 4f 4e 46 4c 49 43 54 20 7c 20 4f 4e 20 43 4f 4e ONFLICT | ON CON
3d4a0 46 4c 49 43 54 20 52 45 50 4c 41 43 45 5d 20 63 FLICT REPLACE] c
3d4b0 6c 61 75 73 65 2e 20 20 5e 4e 6f 72 20 69 73 20 lause. ^Nor is
3d4c0 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 0a the update hook.
3d4d0 2a 2a 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 ** invoked when
3d4e0 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 rows are deleted
3d4f0 20 75 73 69 6e 67 20 74 68 65 20 5b 74 72 75 6e using the [trun
3d500 63 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f cate optimizatio
3d510 6e 5d 2e 0a 2a 2a 20 54 68 65 20 65 78 63 65 70 n]..** The excep
3d520 74 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 6e tions defined in
3d530 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 20 this paragraph
3d540 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20 might change in
3d550 61 20 66 75 74 75 72 65 0a 2a 2a 20 72 65 6c 65 a future.** rele
3d560 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a ase of SQLite..*
3d570 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20 *.** The update
3d580 68 6f 6f 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 hook implementat
3d590 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 ion must not do
3d5a0 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 anything that wi
3d5b0 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 ll modify.** the
3d5c0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
3d5d0 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 tion that invoke
3d5e0 64 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f d the update hoo
3d5f0 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 0a k. Any actions.
3d600 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 ** to modify the
3d610 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
3d620 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 66 tion must be def
3d630 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 65 erred until afte
3d640 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 r the.** complet
3d650 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 ion of the [sqli
3d660 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c te3_step()] call
3d670 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64 20 that triggered
3d680 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e the update hook.
3d690 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 .** Note that [s
3d6a0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
3d6b0 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 2()] and [sqlite
3d6c0 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 3_step()] both m
3d6d0 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 odify their.** d
3d6e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3d6f0 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e ons for the mean
3d700 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 ing of "modify"
3d710 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 in this paragrap
3d720 68 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 h..**.** ^The sq
3d730 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f lite3_update_hoo
3d740 6b 28 44 2c 43 2c 50 29 20 66 75 6e 63 74 69 6f k(D,C,P) functio
3d750 6e 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74 68 65 n.** returns the
3d760 20 50 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d P argument from
3d770 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 61 the previous ca
3d780 6c 6c 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 61 6d ll.** on the sam
3d790 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
3d7a0 65 63 74 69 6f 6e 5d 20 44 2c 20 6f 72 20 4e 55 ection] D, or NU
3d7b0 4c 4c 20 66 6f 72 0a 2a 2a 20 74 68 65 20 66 69 LL for.** the fi
3d7c0 72 73 74 20 63 61 6c 6c 20 6f 6e 20 44 2e 0a 2a rst call on D..*
3d7d0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 *.** See also th
3d7e0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 e [sqlite3_commi
3d7f0 74 5f 68 6f 6f 6b 28 29 5d 20 61 6e 64 20 5b 73 t_hook()] and [s
3d800 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f qlite3_rollback_
3d810 68 6f 6f 6b 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 hook()].** inter
3d820 66 61 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 faces..*/.SQLITE
3d830 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
3d840 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a e3_update_hook(.
3d850 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76 sqlite3*, . v
3d860 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 2c 69 6e oid(*)(void *,in
3d870 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c t ,char const *,
3d880 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c char const *,sql
3d890 69 74 65 33 5f 69 6e 74 36 34 29 2c 0a 20 20 76 ite3_int64),. v
3d8a0 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 oid*.);../*.** C
3d8b0 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 API3REF: Enable
3d8c0 4f 72 20 44 69 73 61 62 6c 65 20 53 68 61 72 65 Or Disable Share
3d8d0 64 20 50 61 67 65 72 20 43 61 63 68 65 0a 2a 2a d Pager Cache.**
3d8e0 0a 2a 2a 20 5e 28 54 68 69 73 20 72 6f 75 74 69 .** ^(This routi
3d8f0 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69 ne enables or di
3d900 73 61 62 6c 65 73 20 74 68 65 20 73 68 61 72 69 sables the shari
3d910 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ng of the databa
3d920 73 65 20 63 61 63 68 65 0a 2a 2a 20 61 6e 64 20 se cache.** and
3d930 73 63 68 65 6d 61 20 64 61 74 61 20 73 74 72 75 schema data stru
3d940 63 74 75 72 65 73 20 62 65 74 77 65 65 6e 20 5b ctures between [
3d950 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3d960 69 6f 6e 20 7c 20 63 6f 6e 6e 65 63 74 69 6f 6e ion | connection
3d970 73 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61 6d s].** to the sam
3d980 65 20 64 61 74 61 62 61 73 65 2e 20 53 68 61 72 e database. Shar
3d990 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 69 ing is enabled i
3d9a0 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 f the argument i
3d9b0 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 64 69 s true.** and di
3d9c0 73 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72 sabled if the ar
3d9d0 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65 2e gument is false.
3d9e0 29 5e 0a 2a 2a 0a 2a 2a 20 5e 43 61 63 68 65 20 )^.**.** ^Cache
3d9f0 73 68 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c sharing is enabl
3da00 65 64 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 ed and disabled
3da10 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20 70 72 for an entire pr
3da20 6f 63 65 73 73 2e 0a 2a 2a 20 54 68 69 73 20 69 ocess..** This i
3da30 73 20 61 20 63 68 61 6e 67 65 20 61 73 20 6f 66 s a change as of
3da40 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 SQLite version
3da50 33 2e 35 2e 30 2e 20 49 6e 20 70 72 69 6f 72 20 3.5.0. In prior
3da60 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 versions of SQLi
3da70 74 65 2c 0a 2a 2a 20 73 68 61 72 69 6e 67 20 77 te,.** sharing w
3da80 61 73 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 as enabled or di
3da90 73 61 62 6c 65 64 20 66 6f 72 20 65 61 63 68 20 sabled for each
3daa0 74 68 72 65 61 64 20 73 65 70 61 72 61 74 65 6c thread separatel
3dab0 79 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 63 y..**.** ^(The c
3dac0 61 63 68 65 20 73 68 61 72 69 6e 67 20 6d 6f 64 ache sharing mod
3dad0 65 20 73 65 74 20 62 79 20 74 68 69 73 20 69 6e e set by this in
3dae0 74 65 72 66 61 63 65 20 65 66 66 65 63 74 73 20 terface effects
3daf0 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a all subsequent.*
3db00 2a 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 * calls to [sqli
3db10 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 te3_open()], [sq
3db20 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d lite3_open_v2()]
3db30 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6f , and [sqlite3_o
3db40 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20 45 78 69 pen16()]..** Exi
3db50 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 sting database c
3db60 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 6f 6e 74 69 onnections conti
3db70 6e 75 65 20 75 73 65 20 74 68 65 20 73 68 61 72 nue use the shar
3db80 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74 68 61 74 ing mode.** that
3db90 20 77 61 73 20 69 6e 20 65 66 66 65 63 74 20 61 was in effect a
3dba0 74 20 74 68 65 20 74 69 6d 65 20 74 68 65 79 20 t the time they
3dbb0 77 65 72 65 20 6f 70 65 6e 65 64 2e 29 5e 0a 2a were opened.)^.*
3dbc0 2a 0a 2a 2a 20 5e 28 54 68 69 73 20 72 6f 75 74 *.** ^(This rout
3dbd0 69 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c ine returns [SQL
3dbe0 49 54 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65 ITE_OK] if share
3dbf0 64 20 63 61 63 68 65 20 77 61 73 20 65 6e 61 62 d cache was enab
3dc00 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 0a led or disabled.
3dc10 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e ** successfully.
3dc20 20 20 41 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 An [error code
3dc30 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 74 ] is returned ot
3dc40 68 65 72 77 69 73 65 2e 29 5e 0a 2a 2a 0a 2a 2a herwise.)^.**.**
3dc50 20 5e 53 68 61 72 65 64 20 63 61 63 68 65 20 69 ^Shared cache i
3dc60 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 64 65 s disabled by de
3dc70 66 61 75 6c 74 2e 20 42 75 74 20 74 68 69 73 20 fault. But this
3dc80 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 0a might change in.
3dc90 2a 2a 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 ** future releas
3dca0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41 es of SQLite. A
3dcb0 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 pplications that
3dcc0 20 63 61 72 65 20 61 62 6f 75 74 20 73 68 61 72 care about shar
3dcd0 65 64 0a 2a 2a 20 63 61 63 68 65 20 73 65 74 74 ed.** cache sett
3dce0 69 6e 67 20 73 68 6f 75 6c 64 20 73 65 74 20 69 ing should set i
3dcf0 74 20 65 78 70 6c 69 63 69 74 6c 79 2e 0a 2a 2a t explicitly..**
3dd00 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
3dd10 63 65 20 69 73 20 74 68 72 65 61 64 73 61 66 65 ce is threadsafe
3dd20 20 6f 6e 20 70 72 6f 63 65 73 73 6f 72 73 20 77 on processors w
3dd30 68 65 72 65 20 77 72 69 74 69 6e 67 20 61 0a 2a here writing a.*
3dd40 2a 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 * 32-bit integer
3dd50 20 69 73 20 61 74 6f 6d 69 63 2e 0a 2a 2a 0a 2a is atomic..**.*
3dd60 2a 20 53 65 65 20 41 6c 73 6f 3a 20 20 5b 53 51 * See Also: [SQ
3dd70 4c 69 74 65 20 53 68 61 72 65 64 2d 43 61 63 68 Lite Shared-Cach
3dd80 65 20 4d 6f 64 65 5d 0a 2a 2f 0a 53 51 4c 49 54 e Mode].*/.SQLIT
3dd90 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
3dda0 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 3_enable_shared_
3ddb0 63 61 63 68 65 28 69 6e 74 29 3b 0a 0a 2f 2a 0a cache(int);../*.
3ddc0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 74 74 ** CAPI3REF: Att
3ddd0 65 6d 70 74 20 54 6f 20 46 72 65 65 20 48 65 61 empt To Free Hea
3dde0 70 20 4d 65 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 5e p Memory.**.** ^
3ddf0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 The sqlite3_rele
3de00 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 69 6e 74 ase_memory() int
3de10 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 erface attempts
3de20 74 6f 20 66 72 65 65 20 4e 20 62 79 74 65 73 0a to free N bytes.
3de30 2a 2a 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 ** of heap memor
3de40 79 20 62 79 20 64 65 61 6c 6c 6f 63 61 74 69 6e y by deallocatin
3de50 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 g non-essential
3de60 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
3de70 6e 73 0a 2a 2a 20 68 65 6c 64 20 62 79 20 74 68 ns.** held by th
3de80 65 20 64 61 74 61 62 61 73 65 20 6c 69 62 72 61 e database libra
3de90 72 79 2e 20 20 20 4d 65 6d 6f 72 79 20 75 73 65 ry. Memory use
3dea0 64 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 d to cache datab
3deb0 61 73 65 0a 2a 2a 20 70 61 67 65 73 20 74 6f 20 ase.** pages to
3dec0 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72 6d 61 improve performa
3ded0 6e 63 65 20 69 73 20 61 6e 20 65 78 61 6d 70 6c nce is an exampl
3dee0 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 e of non-essenti
3def0 61 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 5e 73 al memory..** ^s
3df00 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
3df10 65 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73 20 emory() returns
3df20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
3df30 74 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 65 tes actually fre
3df40 65 64 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 69 67 ed,.** which mig
3df50 68 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c 65 ht be more or le
3df60 73 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f 75 ss than the amou
3df70 6e 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a 2a nt requested..**
3df80 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 ^The sqlite3_re
3df90 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 72 lease_memory() r
3dfa0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
3dfb0 70 20 72 65 74 75 72 6e 69 6e 67 20 7a 65 72 6f p returning zero
3dfc0 0a 2a 2a 20 69 66 20 53 51 4c 69 74 65 20 69 73 .** if SQLite is
3dfd0 20 6e 6f 74 20 63 6f 6d 70 69 6c 65 64 20 77 69 not compiled wi
3dfe0 74 68 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c th [SQLITE_ENABL
3dff0 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d E_MEMORY_MANAGEM
3e000 45 4e 54 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 ENT]..**.** See
3e010 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64 also: [sqlite3_d
3e020 62 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 b_release_memory
3e030 28 29 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ()].*/.SQLITE_AP
3e040 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 I int sqlite3_re
3e050 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 lease_memory(int
3e060 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
3e070 45 46 3a 20 46 72 65 65 20 4d 65 6d 6f 72 79 20 EF: Free Memory
3e080 55 73 65 64 20 42 79 20 41 20 44 61 74 61 62 61 Used By A Databa
3e090 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a se Connection.**
3e0a0 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 .** ^The sqlite3
3e0b0 5f 64 62 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f _db_release_memo
3e0c0 72 79 28 44 29 20 69 6e 74 65 72 66 61 63 65 20 ry(D) interface
3e0d0 61 74 74 65 6d 70 74 73 20 74 6f 20 66 72 65 65 attempts to free
3e0e0 20 61 73 20 6d 75 63 68 20 68 65 61 70 0a 2a 2a as much heap.**
3e0f0 20 6d 65 6d 6f 72 79 20 61 73 20 70 6f 73 73 69 memory as possi
3e100 62 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 ble from databas
3e110 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 20 e connection D.
3e120 55 6e 6c 69 6b 65 20 74 68 65 0a 2a 2a 20 5b 73 Unlike the.** [s
3e130 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
3e140 65 6d 6f 72 79 28 29 5d 20 69 6e 74 65 72 66 61 emory()] interfa
3e150 63 65 2c 20 74 68 69 73 20 69 6e 74 65 72 66 61 ce, this interfa
3e160 63 65 20 69 73 20 65 66 66 65 63 74 20 65 76 65 ce is effect eve
3e170 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 65 6e 20 5b n.** when then [
3e180 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 SQLITE_ENABLE_ME
3e190 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 5d MORY_MANAGEMENT]
3e1a0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 compile-time op
3e1b0 74 69 6f 6e 20 69 73 0a 2a 2a 20 6f 6d 69 74 74 tion is.** omitt
3e1c0 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c ed..**.** See al
3e1d0 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c so: [sqlite3_rel
3e1e0 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 0a 2a ease_memory()].*
3e1f0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
3e200 20 73 71 6c 69 74 65 33 5f 64 62 5f 72 65 6c 65 sqlite3_db_rele
3e210 61 73 65 5f 6d 65 6d 6f 72 79 28 73 71 6c 69 74 ase_memory(sqlit
3e220 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 e3*);../*.** CAP
3e230 49 33 52 45 46 3a 20 49 6d 70 6f 73 65 20 41 20 I3REF: Impose A
3e240 4c 69 6d 69 74 20 4f 6e 20 48 65 61 70 20 53 69 Limit On Heap Si
3e250 7a 65 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 ze.**.** ^The sq
3e260 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f lite3_soft_heap_
3e270 6c 69 6d 69 74 36 34 28 29 20 69 6e 74 65 72 66 limit64() interf
3e280 61 63 65 20 73 65 74 73 20 61 6e 64 2f 6f 72 20 ace sets and/or
3e290 71 75 65 72 69 65 73 20 74 68 65 0a 2a 2a 20 73 queries the.** s
3e2a0 6f 66 74 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 oft limit on the
3e2b0 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 61 70 20 amount of heap
3e2c0 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 61 79 20 memory that may
3e2d0 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 be allocated by
3e2e0 53 51 4c 69 74 65 2e 0a 2a 2a 20 5e 53 51 4c 69 SQLite..** ^SQLi
3e2f0 74 65 20 73 74 72 69 76 65 73 20 74 6f 20 6b 65 te strives to ke
3e300 65 70 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 75 ep heap memory u
3e310 74 69 6c 69 7a 61 74 69 6f 6e 20 62 65 6c 6f 77 tilization below
3e320 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a 2a the soft heap.*
3e330 2a 20 6c 69 6d 69 74 20 62 79 20 72 65 64 75 63 * limit by reduc
3e340 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ing the number o
3e350 66 20 70 61 67 65 73 20 68 65 6c 64 20 69 6e 20 f pages held in
3e360 74 68 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a the page cache.*
3e370 2a 20 61 73 20 68 65 61 70 20 6d 65 6d 6f 72 79 * as heap memory
3e380 20 75 73 61 67 65 73 20 61 70 70 72 6f 61 63 68 usages approach
3e390 65 73 20 74 68 65 20 6c 69 6d 69 74 2e 0a 2a 2a es the limit..**
3e3a0 20 5e 54 68 65 20 73 6f 66 74 20 68 65 61 70 20 ^The soft heap
3e3b0 6c 69 6d 69 74 20 69 73 20 22 73 6f 66 74 22 20 limit is "soft"
3e3c0 62 65 63 61 75 73 65 20 65 76 65 6e 20 74 68 6f because even tho
3e3d0 75 67 68 20 53 51 4c 69 74 65 20 73 74 72 69 76 ugh SQLite striv
3e3e0 65 73 20 74 6f 20 73 74 61 79 0a 2a 2a 20 62 65 es to stay.** be
3e3f0 6c 6f 77 20 74 68 65 20 6c 69 6d 69 74 2c 20 69 low the limit, i
3e400 74 20 77 69 6c 6c 20 65 78 63 65 65 64 20 74 68 t will exceed th
3e410 65 20 6c 69 6d 69 74 20 72 61 74 68 65 72 20 74 e limit rather t
3e420 68 61 6e 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 han generate.**
3e430 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d an [SQLITE_NOMEM
3e440 5d 20 65 72 72 6f 72 2e 20 20 49 6e 20 6f 74 68 ] error. In oth
3e450 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 73 6f er words, the so
3e460 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 0a 2a ft heap limit .*
3e470 2a 20 69 73 20 61 64 76 69 73 6f 72 79 20 6f 6e * is advisory on
3e480 6c 79 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 72 ly..**.** ^The r
3e490 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d eturn value from
3e4a0 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 sqlite3_soft_he
3e4b0 61 70 5f 6c 69 6d 69 74 36 34 28 29 20 69 73 20 ap_limit64() is
3e4c0 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 the size of.** t
3e4d0 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d he soft heap lim
3e4e0 69 74 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 it prior to the
3e4f0 63 61 6c 6c 2c 20 6f 72 20 6e 65 67 61 74 69 76 call, or negativ
3e500 65 20 69 6e 20 74 68 65 20 63 61 73 65 20 6f 66 e in the case of
3e510 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 2e 20 20 5e an.** error. ^
3e520 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 If the argument
3e530 4e 20 69 73 20 6e 65 67 61 74 69 76 65 0a 2a 2a N is negative.**
3e540 20 74 68 65 6e 20 6e 6f 20 63 68 61 6e 67 65 20 then no change
3e550 69 73 20 6d 61 64 65 20 74 6f 20 74 68 65 20 73 is made to the s
3e560 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 2e 20 oft heap limit.
3e570 20 48 65 6e 63 65 2c 20 74 68 65 20 63 75 72 72 Hence, the curr
3e580 65 6e 74 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 ent.** size of t
3e590 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d he soft heap lim
3e5a0 69 74 20 63 61 6e 20 62 65 20 64 65 74 65 72 6d it can be determ
3e5b0 69 6e 65 64 20 62 79 20 69 6e 76 6f 6b 69 6e 67 ined by invoking
3e5c0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 .** sqlite3_soft
3e5d0 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34 28 29 20 _heap_limit64()
3e5e0 77 69 74 68 20 61 20 6e 65 67 61 74 69 76 65 20 with a negative
3e5f0 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
3e600 5e 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 ^If the argument
3e610 20 4e 20 69 73 20 7a 65 72 6f 20 74 68 65 6e 20 N is zero then
3e620 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 the soft heap li
3e630 6d 69 74 20 69 73 20 64 69 73 61 62 6c 65 64 2e mit is disabled.
3e640 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 73 6f 66 .**.** ^(The sof
3e650 74 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20 t heap limit is
3e660 6e 6f 74 20 65 6e 66 6f 72 63 65 64 20 69 6e 20 not enforced in
3e670 74 68 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c the current impl
3e680 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 66 ementation.** if
3e690 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 one or more of
3e6a0 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 64 69 74 following condit
3e6b0 69 6f 6e 73 20 61 72 65 20 74 72 75 65 3a 0a 2a ions are true:.*
3e6c0 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
3e6d0 3e 20 54 68 65 20 73 6f 66 74 20 68 65 61 70 20 > The soft heap
3e6e0 6c 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 limit is set to
3e6f0 7a 65 72 6f 2e 0a 2a 2a 20 3c 6c 69 3e 20 4d 65 zero..** <li> Me
3e700 6d 6f 72 79 20 61 63 63 6f 75 6e 74 69 6e 67 20 mory accounting
3e710 69 73 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e is disabled usin
3e720 67 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 g a combination
3e730 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 5b of the.** [
3e740 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 sqlite3_config](
3e750 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
3e760 45 4d 53 54 41 54 55 53 5d 2c 2e 2e 2e 29 20 73 EMSTATUS],...) s
3e770 74 61 72 74 2d 74 69 6d 65 20 6f 70 74 69 6f 6e tart-time option
3e780 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 74 68 65 and.** the
3e790 20 5b 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 [SQLITE_DEFAULT
3e7a0 5f 4d 45 4d 53 54 41 54 55 53 5d 20 63 6f 6d 70 _MEMSTATUS] comp
3e7b0 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2e ile-time option.
3e7c0 0a 2a 2a 20 3c 6c 69 3e 20 41 6e 20 61 6c 74 65 .** <li> An alte
3e7d0 72 6e 61 74 69 76 65 20 70 61 67 65 20 63 61 63 rnative page cac
3e7e0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
3e7f0 6e 20 69 73 20 73 70 65 63 69 66 69 65 64 20 75 n is specified u
3e800 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 5b 73 71 sing.** [sq
3e810 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 lite3_config]([S
3e820 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 QLITE_CONFIG_PCA
3e830 43 48 45 32 5d 2c 2e 2e 2e 29 2e 0a 2a 2a 20 3c CHE2],...)..** <
3e840 6c 69 3e 20 54 68 65 20 70 61 67 65 20 63 61 63 li> The page cac
3e850 68 65 20 61 6c 6c 6f 63 61 74 65 73 20 66 72 6f he allocates fro
3e860 6d 20 69 74 73 20 6f 77 6e 20 6d 65 6d 6f 72 79 m its own memory
3e870 20 70 6f 6f 6c 20 73 75 70 70 6c 69 65 64 0a 2a pool supplied.*
3e880 2a 20 20 20 20 20 20 62 79 20 5b 73 71 6c 69 74 * by [sqlit
3e890 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 e3_config]([SQLI
3e8a0 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 TE_CONFIG_PAGECA
3e8b0 43 48 45 5d 2c 2e 2e 2e 29 20 72 61 74 68 65 72 CHE],...) rather
3e8c0 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 66 72 than.** fr
3e8d0 6f 6d 20 74 68 65 20 68 65 61 70 2e 0a 2a 2a 20 om the heap..**
3e8e0 3c 2f 75 6c 3e 29 5e 0a 2a 2a 0a 2a 2a 20 42 65 </ul>)^.**.** Be
3e8f0 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c ginning with SQL
3e900 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e ite version 3.7.
3e910 33 2c 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 3, the soft heap
3e920 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 63 limit is enforc
3e930 65 64 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73 73 ed.** regardless
3e940 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e of whether or n
3e950 6f 74 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 45 ot the [SQLITE_E
3e960 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e NABLE_MEMORY_MAN
3e970 41 47 45 4d 45 4e 54 5d 0a 2a 2a 20 63 6f 6d 70 AGEMENT].** comp
3e980 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 ile-time option
3e990 69 73 20 69 6e 76 6f 6b 65 64 2e 20 20 57 69 74 is invoked. Wit
3e9a0 68 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 h [SQLITE_ENABLE
3e9b0 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 _MEMORY_MANAGEME
3e9c0 4e 54 5d 2c 0a 2a 2a 20 74 68 65 20 73 6f 66 74 NT],.** the soft
3e9d0 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20 65 heap limit is e
3e9e0 6e 66 6f 72 63 65 64 20 6f 6e 20 65 76 65 72 79 nforced on every
3e9f0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
3ea00 6f 6e 2e 20 20 57 69 74 68 6f 75 74 0a 2a 2a 20 on. Without.**
3ea10 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d [SQLITE_ENABLE_M
3ea20 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 EMORY_MANAGEMENT
3ea30 5d 2c 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 ], the soft heap
3ea40 20 6c 69 6d 69 74 20 69 73 20 6f 6e 6c 79 20 65 limit is only e
3ea50 6e 66 6f 72 63 65 64 0a 2a 2a 20 77 68 65 6e 20 nforced.** when
3ea60 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 memory is alloca
3ea70 74 65 64 20 62 79 20 74 68 65 20 70 61 67 65 20 ted by the page
3ea80 63 61 63 68 65 2e 20 20 54 65 73 74 69 6e 67 20 cache. Testing
3ea90 73 75 67 67 65 73 74 73 20 74 68 61 74 20 62 65 suggests that be
3eaa0 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 70 61 67 cause.** the pag
3eab0 65 20 63 61 63 68 65 20 69 73 20 74 68 65 20 70 e cache is the p
3eac0 72 65 64 6f 6d 69 6e 61 74 65 20 6d 65 6d 6f 72 redominate memor
3ead0 79 20 75 73 65 72 20 69 6e 20 53 51 4c 69 74 65 y user in SQLite
3eae0 2c 20 6d 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63 , most.** applic
3eaf0 61 74 69 6f 6e 73 20 77 69 6c 6c 20 61 63 68 69 ations will achi
3eb00 65 76 65 20 61 64 65 71 75 61 74 65 20 73 6f 66 eve adequate sof
3eb10 74 20 68 65 61 70 20 6c 69 6d 69 74 20 65 6e 66 t heap limit enf
3eb20 6f 72 63 65 6d 65 6e 74 20 77 69 74 68 6f 75 74 orcement without
3eb30 0a 2a 2a 20 74 68 65 20 75 73 65 20 6f 66 20 5b .** the use of [
3eb40 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 SQLITE_ENABLE_ME
3eb50 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 5d MORY_MANAGEMENT]
3eb60 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 69 72 63 ..**.** The circ
3eb70 75 6d 73 74 61 6e 63 65 73 20 75 6e 64 65 72 20 umstances under
3eb80 77 68 69 63 68 20 53 51 4c 69 74 65 20 77 69 6c which SQLite wil
3eb90 6c 20 65 6e 66 6f 72 63 65 20 74 68 65 20 73 6f l enforce the so
3eba0 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 6d 61 ft heap limit ma
3ebb0 79 0a 2a 2a 20 63 68 61 6e 67 65 73 20 69 6e 20 y.** changes in
3ebc0 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 future releases
3ebd0 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 of SQLite..*/.SQ
3ebe0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
3ebf0 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 73 _int64 sqlite3_s
3ec00 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34 oft_heap_limit64
3ec10 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 4e (sqlite3_int64 N
3ec20 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
3ec30 45 46 3a 20 44 65 70 72 65 63 61 74 65 64 20 53 EF: Deprecated S
3ec40 6f 66 74 20 48 65 61 70 20 4c 69 6d 69 74 20 49 oft Heap Limit I
3ec50 6e 74 65 72 66 61 63 65 0a 2a 2a 20 44 45 50 52 nterface.** DEPR
3ec60 45 43 41 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 69 ECATED.**.** Thi
3ec70 73 20 69 73 20 61 20 64 65 70 72 65 63 61 74 65 s is a deprecate
3ec80 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 d version of the
3ec90 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 [sqlite3_soft_h
3eca0 65 61 70 5f 6c 69 6d 69 74 36 34 28 29 5d 0a 2a eap_limit64()].*
3ecb0 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54 68 * interface. Th
3ecc0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 70 72 is routine is pr
3ecd0 6f 76 69 64 65 64 20 66 6f 72 20 68 69 73 74 6f ovided for histo
3ece0 72 69 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c rical compatibil
3ecf0 69 74 79 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 41 6c ity.** only. Al
3ed00 6c 20 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f l new applicatio
3ed10 6e 73 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 ns should use th
3ed20 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6f e.** [sqlite3_so
3ed30 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34 28 ft_heap_limit64(
3ed40 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 61 74 )] interface rat
3ed50 68 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e her than this on
3ed60 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
3ed70 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 SQLITE_DEPRECAT
3ed80 45 44 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f ED void sqlite3_
3ed90 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 soft_heap_limit(
3eda0 69 6e 74 20 4e 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 int N);.../*.**
3edb0 43 41 50 49 33 52 45 46 3a 20 45 78 74 72 61 63 CAPI3REF: Extrac
3edc0 74 20 4d 65 74 61 64 61 74 61 20 41 62 6f 75 74 t Metadata About
3edd0 20 41 20 43 6f 6c 75 6d 6e 20 4f 66 20 41 20 54 A Column Of A T
3ede0 61 62 6c 65 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 able.**.** ^This
3edf0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
3ee00 20 6d 65 74 61 64 61 74 61 20 61 62 6f 75 74 20 metadata about
3ee10 61 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d a specific colum
3ee20 6e 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 0a n of a specific.
3ee30 2a 2a 20 64 61 74 61 62 61 73 65 20 74 61 62 6c ** database tabl
3ee40 65 20 61 63 63 65 73 73 69 62 6c 65 20 75 73 69 e accessible usi
3ee50 6e 67 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 ng the [database
3ee60 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e connection] han
3ee70 64 6c 65 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 dle.** passed as
3ee80 20 74 68 65 20 66 69 72 73 74 20 66 75 6e 63 74 the first funct
3ee90 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a ion argument..**
3eea0 0a 2a 2a 20 5e 54 68 65 20 63 6f 6c 75 6d 6e 20 .** ^The column
3eeb0 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 is identified by
3eec0 20 74 68 65 20 73 65 63 6f 6e 64 2c 20 74 68 69 the second, thi
3eed0 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 rd and fourth pa
3eee0 72 61 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 74 rameters to.** t
3eef0 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 5e 54 his function. ^T
3ef00 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
3ef10 74 65 72 20 69 73 20 65 69 74 68 65 72 20 74 68 ter is either th
3ef20 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 e name of the da
3ef30 74 61 62 61 73 65 0a 2a 2a 20 28 69 2e 65 2e 20 tabase.** (i.e.
3ef40 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 2c 20 "main", "temp",
3ef50 6f 72 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 or an attached d
3ef60 61 74 61 62 61 73 65 29 20 63 6f 6e 74 61 69 6e atabase) contain
3ef70 69 6e 67 20 74 68 65 20 73 70 65 63 69 66 69 65 ing the specifie
3ef80 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 4e 55 d.** table or NU
3ef90 4c 4c 2e 20 5e 49 66 20 69 74 20 69 73 20 4e 55 LL. ^If it is NU
3efa0 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61 74 74 LL, then all att
3efb0 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 ached databases
3efc0 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a 2a 20 are searched.**
3efd0 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 75 73 for the table us
3efe0 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61 6c 67 ing the same alg
3eff0 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79 20 74 orithm used by t
3f000 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 he database engi
3f010 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c 76 65 ne to.** resolve
3f020 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 unqualified tab
3f030 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a le references..*
3f040 2a 0a 2a 2a 20 5e 54 68 65 20 74 68 69 72 64 20 *.** ^The third
3f050 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d and fourth param
3f060 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 75 eters to this fu
3f070 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74 nction are the t
3f080 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a able and column.
3f090 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 ** name of the d
3f0a0 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c 20 72 esired column, r
3f0b0 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e 65 69 espectively. Nei
3f0c0 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 70 61 ther of these pa
3f0d0 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 79 20 rameters.** may
3f0e0 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 5e be NULL..**.** ^
3f0f0 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 74 75 Metadata is retu
3f100 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 rned by writing
3f110 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f to the memory lo
3f120 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61 cations passed a
3f130 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e 64 s the 5th.** and
3f140 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 subsequent para
3f150 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 meters to this f
3f160 75 6e 63 74 69 6f 6e 2e 20 5e 41 6e 79 20 6f 66 unction. ^Any of
3f170 20 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 these arguments
3f180 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c may be.** NULL,
3f190 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 in which case t
3f1a0 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
3f1b0 20 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 element of meta
3f1c0 64 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e data is omitted.
3f1d0 0a 2a 2a 0a 2a 2a 20 5e 28 3c 62 6c 6f 63 6b 71 .**.** ^(<blockq
3f1e0 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 uote>.** <table
3f1f0 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c border="1">.** <
3f200 74 72 3e 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 tr><th> Paramete
3f210 72 20 3c 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 r <th> Output<br
3f220 3e 54 79 70 65 20 3c 74 68 3e 20 20 44 65 73 63 >Type <th> Desc
3f230 72 69 70 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 ription.**.** <t
3f240 72 3e 3c 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 r><td> 5th <td>
3f250 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e const char* <td>
3f260 20 44 61 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 Data type.** <t
3f270 72 3e 3c 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 r><td> 6th <td>
3f280 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e const char* <td>
3f290 20 4e 61 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 Name of default
3f2a0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
3f2b0 6e 63 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 nce.** <tr><td>
3f2c0 37 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 7th <td> int
3f2d0 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 <td> True i
3f2e0 66 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e f column has a N
3f2f0 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 OT NULL constrai
3f300 6e 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 nt.** <tr><td> 8
3f310 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 th <td> int
3f320 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 <td> True if
3f330 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 column is part
3f340 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b of the PRIMARY K
3f350 45 59 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 EY.** <tr><td> 9
3f360 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 th <td> int
3f370 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 <td> True if
3f380 20 63 6f 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f column is [AUTO
3f390 49 4e 43 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f INCREMENT].** </
3f3a0 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 table>.** </bloc
3f3b0 6b 71 75 6f 74 65 3e 29 5e 0a 2a 2a 0a 2a 2a 20 kquote>)^.**.**
3f3c0 5e 54 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e ^The memory poin
3f3d0 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 63 68 ted to by the ch
3f3e0 61 72 61 63 74 65 72 20 70 6f 69 6e 74 65 72 73 aracter pointers
3f3f0 20 72 65 74 75 72 6e 65 64 20 66 6f 72 20 74 68 returned for th
3f400 65 0a 2a 2a 20 64 65 63 6c 61 72 61 74 69 6f 6e e.** declaration
3f410 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 type and collat
3f420 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 ion sequence is
3f430 76 61 6c 69 64 20 6f 6e 6c 79 20 75 6e 74 69 6c valid only until
3f440 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 63 61 6c the next.** cal
3f450 6c 20 74 6f 20 61 6e 79 20 53 51 4c 69 74 65 20 l to any SQLite
3f460 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a API function..**
3f470 0a 2a 2a 20 5e 49 66 20 74 68 65 20 73 70 65 63 .** ^If the spec
3f480 69 66 69 65 64 20 74 61 62 6c 65 20 69 73 20 61 ified table is a
3f490 63 74 75 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 ctually a view,
3f4a0 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 an [error code]
3f4b0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
3f4c0 2a 2a 20 5e 49 66 20 74 68 65 20 73 70 65 63 69 ** ^If the speci
3f4d0 66 69 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 22 fied column is "
3f4e0 72 6f 77 69 64 22 2c 20 22 6f 69 64 22 20 6f 72 rowid", "oid" or
3f4f0 20 22 5f 72 6f 77 69 64 5f 22 20 61 6e 64 20 61 "_rowid_" and a
3f500 6e 0a 2a 2a 20 5b 49 4e 54 45 47 45 52 20 50 52 n.** [INTEGER PR
3f510 49 4d 41 52 59 20 4b 45 59 5d 20 63 6f 6c 75 6d IMARY KEY] colum
3f520 6e 20 68 61 73 20 62 65 65 6e 20 65 78 70 6c 69 n has been expli
3f530 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 2c 20 citly declared,
3f540 74 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a then the output.
3f550 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 ** parameters ar
3f560 65 20 73 65 74 20 66 6f 72 20 74 68 65 20 65 78 e set for the ex
3f570 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 plicitly declare
3f580 64 20 63 6f 6c 75 6d 6e 2e 20 5e 28 49 66 20 74 d column. ^(If t
3f590 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 65 78 here is no.** ex
3f5a0 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 plicitly declare
3f5b0 64 20 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41 d [INTEGER PRIMA
3f5c0 52 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e 2c 20 RY KEY] column,
3f5d0 74 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a then the output.
3f5e0 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 ** parameters ar
3f5f0 65 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73 e set as follows
3f600 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a :.**.** <pre>.**
3f610 20 20 20 20 20 64 61 74 61 20 74 79 70 65 3a 20 data type:
3f620 22 49 4e 54 45 47 45 52 22 0a 2a 2a 20 20 20 20 "INTEGER".**
3f630 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
3f640 6e 63 65 3a 20 22 42 49 4e 41 52 59 22 0a 2a 2a nce: "BINARY".**
3f650 20 20 20 20 20 6e 6f 74 20 6e 75 6c 6c 3a 20 30 not null: 0
3f660 0a 2a 2a 20 20 20 20 20 70 72 69 6d 61 72 79 20 .** primary
3f670 6b 65 79 3a 20 31 0a 2a 2a 20 20 20 20 20 61 75 key: 1.** au
3f680 74 6f 20 69 6e 63 72 65 6d 65 6e 74 3a 20 30 0a to increment: 0.
3f690 2a 2a 20 3c 2f 70 72 65 3e 29 5e 0a 2a 2a 0a 2a ** </pre>)^.**.*
3f6a0 2a 20 5e 28 54 68 69 73 20 66 75 6e 63 74 69 6f * ^(This functio
3f6b0 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e 65 20 6f n may load one o
3f6c0 72 20 6d 6f 72 65 20 73 63 68 65 6d 61 73 20 66 r more schemas f
3f6d0 72 6f 6d 20 64 61 74 61 62 61 73 65 20 66 69 6c rom database fil
3f6e0 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 72 72 es. If an.** err
3f6f0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
3f700 20 74 68 69 73 20 70 72 6f 63 65 73 73 2c 20 6f this process, o
3f710 72 20 69 66 20 74 68 65 20 72 65 71 75 65 73 74 r if the request
3f720 65 64 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 ed table or colu
3f730 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 mn.** cannot be
3f740 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 72 72 6f 72 found, an [error
3f750 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e code] is return
3f760 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 ed and an error
3f770 6d 65 73 73 61 67 65 20 6c 65 66 74 0a 2a 2a 20 message left.**
3f780 69 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 in the [database
3f790 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 6f connection] (to
3f7a0 20 62 65 20 72 65 74 72 69 65 76 65 64 20 75 73 be retrieved us
3f7b0 69 6e 67 20 73 71 6c 69 74 65 33 5f 65 72 72 6d ing sqlite3_errm
3f7c0 73 67 28 29 29 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e sg()).)^.**.** ^
3f7d0 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 This API is only
3f7e0 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 available if th
3f7f0 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f e library was co
3f800 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a mpiled with the.
3f810 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c ** [SQLITE_ENABL
3f820 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 E_COLUMN_METADAT
3f830 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f A] C-preprocesso
3f840 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 r symbol defined
3f850 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
3f860 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c int sqlite3_tabl
3f870 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 e_column_metadat
3f880 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 a(. sqlite3 *db
3f890 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
3f8a0 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 /* Connection h
3f8b0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
3f8c0 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 char *zDbName,
3f8d0 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
3f8e0 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 se name or NULL
3f8f0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
3f900 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 *zTableName,
3f910 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a /* Table name *
3f920 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
3f930 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 zColumnName,
3f940 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a /* Column name *
3f950 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a /. char const *
3f960 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20 *pzDataType,
3f970 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61 /* OUTPUT: Decla
3f980 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f red data type */
3f990 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a . char const **
3f9a0 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f pzCollSeq, /
3f9b0 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74 * OUTPUT: Collat
3f9c0 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d ion sequence nam
3f9d0 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 e */. int *pNot
3f9e0 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 Null,
3f9f0 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 /* OUTPUT: Tr
3fa00 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 ue if NOT NULL c
3fa10 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73 onstraint exists
3fa20 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d */. int *pPrim
3fa30 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20 aryKey,
3fa40 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 /* OUTPUT: Tru
3fa50 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74 e if column part
3fa60 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 of PK */. int
3fa70 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20 *pAutoinc
3fa80 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 /* OUTPU
3fa90 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d T: True if colum
3faa0 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d n is auto-increm
3fab0 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a ent */.);../*.**
3fac0 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20 CAPI3REF: Load
3fad0 41 6e 20 45 78 74 65 6e 73 69 6f 6e 0a 2a 2a 0a An Extension.**.
3fae0 2a 2a 20 5e 54 68 69 73 20 69 6e 74 65 72 66 61 ** ^This interfa
3faf0 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 4c 69 ce loads an SQLi
3fb00 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 te extension lib
3fb10 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20 6e 61 rary from the na
3fb20 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 med file..**.**
3fb30 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61 ^The sqlite3_loa
3fb40 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e d_extension() in
3fb50 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 terface attempts
3fb60 20 74 6f 20 6c 6f 61 64 20 61 6e 0a 2a 2a 20 53 to load an.** S
3fb70 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 QLite extension
3fb80 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 65 library containe
3fb90 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 7a 46 d in the file zF
3fba0 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 ile..**.** ^The
3fbb0 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73 20 7a entry point is z
3fbc0 50 72 6f 63 2e 0a 2a 2a 20 5e 7a 50 72 6f 63 20 Proc..** ^zProc
3fbd0 6d 61 79 20 62 65 20 30 2c 20 69 6e 20 77 68 69 may be 0, in whi
3fbe0 63 68 20 63 61 73 65 20 74 68 65 20 6e 61 6d 65 ch case the name
3fbf0 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 70 6f of the entry po
3fc00 69 6e 74 0a 2a 2a 20 64 65 66 61 75 6c 74 73 20 int.** defaults
3fc10 74 6f 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 to "sqlite3_exte
3fc20 6e 73 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 20 nsion_init"..**
3fc30 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61 ^The sqlite3_loa
3fc40 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e d_extension() in
3fc50 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 0a terface returns.
3fc60 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f ** [SQLITE_OK] o
3fc70 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 5b 53 n success and [S
3fc80 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 QLITE_ERROR] if
3fc90 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 something goes w
3fca0 72 6f 6e 67 2e 0a 2a 2a 20 5e 49 66 20 61 6e 20 rong..** ^If an
3fcb0 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 64 error occurs and
3fcc0 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e 6f 74 pzErrMsg is not
3fcd0 20 30 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 0, then the.**
3fce0 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 [sqlite3_load_ex
3fcf0 74 65 6e 73 69 6f 6e 28 29 5d 20 69 6e 74 65 72 tension()] inter
3fd00 66 61 63 65 20 73 68 61 6c 6c 20 61 74 74 65 6d face shall attem
3fd10 70 74 20 74 6f 0a 2a 2a 20 66 69 6c 6c 20 2a 70 pt to.** fill *p
3fd20 7a 45 72 72 4d 73 67 20 77 69 74 68 20 65 72 72 zErrMsg with err
3fd30 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 or message text
3fd40 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 stored in memory
3fd50 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f .** obtained fro
3fd60 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f m [sqlite3_mallo
3fd70 63 28 29 5d 2e 20 54 68 65 20 63 61 6c 6c 69 6e c()]. The callin
3fd80 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 68 g function.** sh
3fd90 6f 75 6c 64 20 66 72 65 65 20 74 68 69 73 20 6d ould free this m
3fda0 65 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 emory by calling
3fdb0 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
3fdc0 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 45 78 74 65 6e 73 ]..**.** ^Extens
3fdd0 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d 75 73 74 ion loading must
3fde0 20 62 65 20 65 6e 61 62 6c 65 64 20 75 73 69 6e be enabled usin
3fdf0 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 6e g.** [sqlite3_en
3fe00 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 able_load_extens
3fe10 69 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 ion()] prior to
3fe20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 41 50 49 calling this API
3fe30 2c 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 61 ,.** otherwise a
3fe40 6e 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 n error will be
3fe50 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
3fe60 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 6c 6f See also the [lo
3fe70 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 53 ad_extension() S
3fe80 51 4c 20 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2f QL function]..*/
3fe90 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
3fea0 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 sqlite3_load_ext
3feb0 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 ension(. sqlite
3fec0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
3fed0 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 /* Load the exte
3fee0 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 nsion into this
3fef0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
3ff00 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ion */. const c
3ff10 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f har *zFile, /
3ff20 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 * Name of the sh
3ff30 61 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e ared library con
3ff40 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f taining extensio
3ff50 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 n */. const cha
3ff60 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 r *zProc, /*
3ff70 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 44 65 Entry point. De
3ff80 72 69 76 65 64 20 66 72 6f 6d 20 7a 46 69 6c 65 rived from zFile
3ff90 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72 20 if 0 */. char
3ffa0 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 **pzErrMsg
3ffb0 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d 65 /* Put error me
3ffc0 73 73 61 67 65 20 68 65 72 65 20 69 66 20 6e 6f ssage here if no
3ffd0 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a t 0 */.);../*.**
3ffe0 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c CAPI3REF: Enabl
3fff0 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74 e Or Disable Ext
40000 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 0a 2a ension Loading.*
40010 2a 0a 2a 2a 20 5e 53 6f 20 61 73 20 6e 6f 74 20 *.** ^So as not
40020 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 69 74 79 to open security
40030 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20 holes in older
40040 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 applications tha
40050 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72 65 70 61 t are.** unprepa
40060 72 65 64 20 74 6f 20 64 65 61 6c 20 77 69 74 68 red to deal with
40070 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 extension loadi
40080 6e 67 2c 20 61 6e 64 20 61 73 20 61 20 6d 65 61 ng, and as a mea
40090 6e 73 20 6f 66 20 64 69 73 61 62 6c 69 6e 67 0a ns of disabling.
400a0 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 ** extension loa
400b0 64 69 6e 67 20 77 68 69 6c 65 20 65 76 61 6c 75 ding while evalu
400c0 61 74 69 6e 67 20 75 73 65 72 2d 65 6e 74 65 72 ating user-enter
400d0 65 64 20 53 51 4c 2c 20 74 68 65 20 66 6f 6c 6c ed SQL, the foll
400e0 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20 69 73 20 owing API.** is
400f0 70 72 6f 76 69 64 65 64 20 74 6f 20 74 75 72 6e provided to turn
40100 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 6f the [sqlite3_lo
40110 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 ad_extension()]
40120 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20 61 6e 64 mechanism on and
40130 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 5e 45 78 74 off..**.** ^Ext
40140 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 ension loading i
40150 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 s off by default
40160 2e 20 53 65 65 20 74 69 63 6b 65 74 20 23 31 38 . See ticket #18
40170 36 33 2e 0a 2a 2a 20 5e 43 61 6c 6c 20 74 68 65 63..** ^Call the
40180 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f sqlite3_enable_
40190 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 load_extension()
401a0 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 6f 6e routine with on
401b0 6f 66 66 3d 3d 31 0a 2a 2a 20 74 6f 20 74 75 72 off==1.** to tur
401c0 6e 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 n extension load
401d0 69 6e 67 20 6f 6e 20 61 6e 64 20 63 61 6c 6c 20 ing on and call
401e0 69 74 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 30 it with onoff==0
401f0 20 74 6f 20 74 75 72 6e 0a 2a 2a 20 69 74 20 62 to turn.** it b
40200 61 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a ack off again..*
40210 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
40220 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f sqlite3_enable_
40230 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73 load_extension(s
40240 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 qlite3 *db, int
40250 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 onoff);../*.** C
40260 41 50 49 33 52 45 46 3a 20 41 75 74 6f 6d 61 74 API3REF: Automat
40270 69 63 61 6c 6c 79 20 4c 6f 61 64 20 53 74 61 74 ically Load Stat
40280 69 63 61 6c 6c 79 20 4c 69 6e 6b 65 64 20 45 78 ically Linked Ex
40290 74 65 6e 73 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 5e tensions.**.** ^
402a0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 63 This interface c
402b0 61 75 73 65 73 20 74 68 65 20 78 45 6e 74 72 79 auses the xEntry
402c0 50 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e Point() function
402d0 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 66 to be invoked f
402e0 6f 72 0a 2a 2a 20 65 61 63 68 20 6e 65 77 20 5b or.** each new [
402f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
40300 69 6f 6e 5d 20 74 68 61 74 20 69 73 20 63 72 65 ion] that is cre
40310 61 74 65 64 2e 20 20 54 68 65 20 69 64 65 61 20 ated. The idea
40320 68 65 72 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 here is that.**
40330 78 45 6e 74 72 79 50 6f 69 6e 74 28 29 20 69 73 xEntryPoint() is
40340 20 74 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 the entry point
40350 20 66 6f 72 20 61 20 73 74 61 74 69 63 61 6c 6c for a staticall
40360 79 20 6c 69 6e 6b 65 64 20 53 51 4c 69 74 65 20 y linked SQLite
40370 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 74 68 61 extension.** tha
40380 74 20 69 73 20 74 6f 20 62 65 20 61 75 74 6f 6d t is to be autom
40390 61 74 69 63 61 6c 6c 79 20 6c 6f 61 64 65 64 20 atically loaded
403a0 69 6e 74 6f 20 61 6c 6c 20 6e 65 77 20 64 61 74 into all new dat
403b0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
403c0 73 2e 0a 2a 2a 0a 2a 2a 20 5e 28 45 76 65 6e 20 s..**.** ^(Even
403d0 74 68 6f 75 67 68 20 74 68 65 20 66 75 6e 63 74 though the funct
403e0 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 20 73 68 ion prototype sh
403f0 6f 77 73 20 74 68 61 74 20 78 45 6e 74 72 79 50 ows that xEntryP
40400 6f 69 6e 74 28 29 20 74 61 6b 65 73 0a 2a 2a 20 oint() takes.**
40410 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 no arguments and
40420 20 72 65 74 75 72 6e 73 20 76 6f 69 64 2c 20 53 returns void, S
40430 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 78 45 QLite invokes xE
40440 6e 74 72 79 50 6f 69 6e 74 28 29 20 77 69 74 68 ntryPoint() with
40450 20 74 68 72 65 65 0a 2a 2a 20 61 72 67 75 6d 65 three.** argume
40460 6e 74 73 20 61 6e 64 20 65 78 70 65 63 74 73 20 nts and expects
40470 61 6e 64 20 69 6e 74 65 67 65 72 20 72 65 73 75 and integer resu
40480 6c 74 20 61 73 20 69 66 20 74 68 65 20 73 69 67 lt as if the sig
40490 6e 61 74 75 72 65 20 6f 66 20 74 68 65 0a 2a 2a nature of the.**
404a0 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 77 68 65 entry point whe
404b0 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a re as follows:.*
404c0 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 *.** <blockquote
404d0 3e 3c 70 72 65 3e 0a 2a 2a 20 26 6e 62 73 70 3b ><pre>.**
404e0 20 20 69 6e 74 20 78 45 6e 74 72 79 50 6f 69 6e int xEntryPoin
404f0 74 28 0a 2a 2a 20 26 6e 62 73 70 3b 20 20 20 20 t(.**
40500 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 2a 2a 20 sqlite3 *db,.**
40510 26 6e 62 73 70 3b 20 20 20 20 63 6f 6e 73 74 20 const
40520 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c char **pzErrMsg,
40530 0a 2a 2a 20 26 6e 62 73 70 3b 20 20 20 20 63 6f .** co
40540 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 nst struct sqlit
40550 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 e3_api_routines
40560 2a 70 54 68 75 6e 6b 0a 2a 2a 20 26 6e 62 73 70 *pThunk.**  
40570 3b 20 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c ; );.** </pre><
40580 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 29 5e 0a 2a /blockquote>)^.*
40590 2a 0a 2a 2a 20 49 66 20 74 68 65 20 78 45 6e 74 *.** If the xEnt
405a0 72 79 50 6f 69 6e 74 20 72 6f 75 74 69 6e 65 20 ryPoint routine
405b0 65 6e 63 6f 75 6e 74 65 72 73 20 61 6e 20 65 72 encounters an er
405c0 72 6f 72 2c 20 69 74 20 73 68 6f 75 6c 64 20 6d ror, it should m
405d0 61 6b 65 20 2a 70 7a 45 72 72 4d 73 67 0a 2a 2a ake *pzErrMsg.**
405e0 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 61 70 70 point to an app
405f0 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d ropriate error m
40600 65 73 73 61 67 65 20 28 6f 62 74 61 69 6e 65 64 essage (obtained
40610 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d from [sqlite3_m
40620 70 72 69 6e 74 66 28 29 5d 29 0a 2a 2a 20 61 6e printf()]).** an
40630 64 20 72 65 74 75 72 6e 20 61 6e 20 61 70 70 72 d return an appr
40640 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 20 63 opriate [error c
40650 6f 64 65 5d 2e 20 20 5e 53 51 4c 69 74 65 20 65 ode]. ^SQLite e
40660 6e 73 75 72 65 73 20 74 68 61 74 20 2a 70 7a 45 nsures that *pzE
40670 72 72 4d 73 67 0a 2a 2a 20 69 73 20 4e 55 4c 4c rrMsg.** is NULL
40680 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 before calling
40690 74 68 65 20 78 45 6e 74 72 79 50 6f 69 6e 74 28 the xEntryPoint(
406a0 29 2e 20 20 5e 53 51 4c 69 74 65 20 77 69 6c 6c ). ^SQLite will
406b0 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73 71 6c 69 invoke.** [sqli
406c0 74 65 33 5f 66 72 65 65 28 29 5d 20 6f 6e 20 2a te3_free()] on *
406d0 70 7a 45 72 72 4d 73 67 20 61 66 74 65 72 20 78 pzErrMsg after x
406e0 45 6e 74 72 79 50 6f 69 6e 74 28 29 20 72 65 74 EntryPoint() ret
406f0 75 72 6e 73 2e 20 20 5e 49 66 20 61 6e 79 0a 2a urns. ^If any.*
40700 2a 20 78 45 6e 74 72 79 50 6f 69 6e 74 28 29 20 * xEntryPoint()
40710 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 returns an error
40720 2c 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f , the [sqlite3_o
40730 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 pen()], [sqlite3
40740 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 6f _open16()],.** o
40750 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f r [sqlite3_open_
40760 76 32 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 v2()] call that
40770 70 72 6f 76 6f 6b 65 64 20 74 68 65 20 78 45 6e provoked the xEn
40780 74 72 79 50 6f 69 6e 74 28 29 20 77 69 6c 6c 20 tryPoint() will
40790 66 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 5e 43 61 6c fail..**.** ^Cal
407a0 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 61 75 74 ling sqlite3_aut
407b0 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 58 29 20 77 o_extension(X) w
407c0 69 74 68 20 61 6e 20 65 6e 74 72 79 20 70 6f 69 ith an entry poi
407d0 6e 74 20 58 20 74 68 61 74 20 69 73 20 61 6c 72 nt X that is alr
407e0 65 61 64 79 0a 2a 2a 20 6f 6e 20 74 68 65 20 6c eady.** on the l
407f0 69 73 74 20 6f 66 20 61 75 74 6f 6d 61 74 69 63 ist of automatic
40800 20 65 78 74 65 6e 73 69 6f 6e 73 20 69 73 20 61 extensions is a
40810 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e harmless no-op.
40820 20 5e 4e 6f 20 65 6e 74 72 79 20 70 6f 69 6e 74 ^No entry point
40830 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c .** will be call
40840 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 ed more than onc
40850 65 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 e for each datab
40860 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 ase connection t
40870 68 61 74 20 69 73 20 6f 70 65 6e 65 64 2e 0a 2a hat is opened..*
40880 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
40890 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 sqlite3_reset_au
408a0 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 2e to_extension()].
408b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
408c0 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f nt sqlite3_auto_
408d0 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28 extension(void (
408e0 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76 6f *xEntryPoint)(vo
408f0 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 id));../*.** CAP
40900 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 75 74 I3REF: Reset Aut
40910 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f 6e omatic Extension
40920 20 4c 6f 61 64 69 6e 67 0a 2a 2a 0a 2a 2a 20 5e Loading.**.** ^
40930 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 64 This interface d
40940 69 73 61 62 6c 65 73 20 61 6c 6c 20 61 75 74 6f isables all auto
40950 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 matic extensions
40960 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 72 previously.** r
40970 65 67 69 73 74 65 72 65 64 20 75 73 69 6e 67 20 egistered using
40980 5b 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 [sqlite3_auto_ex
40990 74 65 6e 73 69 6f 6e 28 29 5d 2e 0a 2a 2f 0a 53 tension()]..*/.S
409a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
409b0 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 qlite3_reset_aut
409c0 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 o_extension(void
409d0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e );../*.** The in
409e0 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 terface to the v
409f0 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 irtual-table mec
40a00 68 61 6e 69 73 6d 20 69 73 20 63 75 72 72 65 6e hanism is curren
40a10 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a tly considered.*
40a20 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65 * to be experime
40a30 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 ntal. The inter
40a40 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67 face might chang
40a50 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c e in incompatibl
40a60 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 e ways..** If th
40a70 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 is is a problem
40a80 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 for you, do not
40a90 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63 use the interfac
40aa0 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a e at this time..
40ab0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 **.** When the v
40ac0 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 irtual-table mec
40ad0 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 hanism stabilize
40ae0 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 s, we will decla
40af0 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 re the.** interf
40b00 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f ace fixed, suppo
40b10 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65 rt it indefinite
40b20 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 ly, and remove t
40b30 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a his comment..*/.
40b40 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 ./*.** Structure
40b50 73 20 75 73 65 64 20 62 79 20 74 68 65 20 76 69 s used by the vi
40b60 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74 65 rtual table inte
40b70 72 66 61 63 65 0a 2a 2f 0a 74 79 70 65 64 65 66 rface.*/.typedef
40b80 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
40b90 76 74 61 62 20 73 71 6c 69 74 65 33 5f 76 74 61 vtab sqlite3_vta
40ba0 62 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 b;.typedef struc
40bb0 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f t sqlite3_index_
40bc0 69 6e 66 6f 20 73 71 6c 69 74 65 33 5f 69 6e 64 info sqlite3_ind
40bd0 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 65 64 65 66 ex_info;.typedef
40be0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
40bf0 76 74 61 62 5f 63 75 72 73 6f 72 20 73 71 6c 69 vtab_cursor sqli
40c00 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 3b te3_vtab_cursor;
40c10 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
40c20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 73 sqlite3_module s
40c30 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 3b 0a 0a qlite3_module;..
40c40 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
40c50 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 4f 62 Virtual Table Ob
40c60 6a 65 63 74 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 ject.** KEYWORDS
40c70 3a 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 : sqlite3_module
40c80 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 {virtual table
40c90 6d 6f 64 75 6c 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 module}.**.** Th
40ca0 69 73 20 73 74 72 75 63 74 75 72 65 2c 20 73 6f is structure, so
40cb0 6d 65 74 69 6d 65 73 20 63 61 6c 6c 65 64 20 61 metimes called a
40cc0 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 "virtual table
40cd0 6d 6f 64 75 6c 65 22 2c 20 0a 2a 2a 20 64 65 66 module", .** def
40ce0 69 6e 65 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 ines the impleme
40cf0 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 5b 76 69 ntation of a [vi
40d00 72 74 75 61 6c 20 74 61 62 6c 65 73 5d 2e 20 20 rtual tables].
40d10 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 .** This structu
40d20 72 65 20 63 6f 6e 73 69 73 74 73 20 6d 6f 73 74 re consists most
40d30 6c 79 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f ly of methods fo
40d40 72 20 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a r the module..**
40d50 0a 2a 2a 20 5e 41 20 76 69 72 74 75 61 6c 20 74 .** ^A virtual t
40d60 61 62 6c 65 20 6d 6f 64 75 6c 65 20 69 73 20 63 able module is c
40d70 72 65 61 74 65 64 20 62 79 20 66 69 6c 6c 69 6e reated by fillin
40d80 67 20 69 6e 20 61 20 70 65 72 73 69 73 74 65 6e g in a persisten
40d90 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 t.** instance of
40da0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
40db0 61 6e 64 20 70 61 73 73 69 6e 67 20 61 20 70 6f and passing a po
40dc0 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 69 6e inter to that in
40dd0 73 74 61 6e 63 65 0a 2a 2a 20 74 6f 20 5b 73 71 stance.** to [sq
40de0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 lite3_create_mod
40df0 75 6c 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 ule()] or [sqlit
40e00 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 e3_create_module
40e10 5f 76 32 28 29 5d 2e 0a 2a 2a 20 5e 54 68 65 20 _v2()]..** ^The
40e20 72 65 67 69 73 74 72 61 74 69 6f 6e 20 72 65 6d registration rem
40e30 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 6c ains valid until
40e40 20 69 74 20 69 73 20 72 65 70 6c 61 63 65 64 20 it is replaced
40e50 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a by a different.*
40e60 2a 20 6d 6f 64 75 6c 65 20 6f 72 20 75 6e 74 69 * module or unti
40e70 6c 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 l the [database
40e80 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 63 6c 6f 73 connection] clos
40e90 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 es. The content
40ea0 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75 .** of this stru
40eb0 63 74 75 72 65 20 6d 75 73 74 20 6e 6f 74 20 63 cture must not c
40ec0 68 61 6e 67 65 20 77 68 69 6c 65 20 69 74 20 69 hange while it i
40ed0 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 s registered wit
40ee0 68 0a 2a 2a 20 61 6e 79 20 64 61 74 61 62 61 73 h.** any databas
40ef0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f e connection..*/
40f00 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
40f10 6d 6f 64 75 6c 65 20 7b 0a 20 20 69 6e 74 20 69 module {. int i
40f20 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 Version;. int (
40f30 2a 78 43 72 65 61 74 65 29 28 73 71 6c 69 74 65 *xCreate)(sqlite
40f40 33 2a 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 3*, void *pAux,.
40f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
40f60 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 nt argc, const c
40f70 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c har *const*argv,
40f80 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
40f90 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 sqlite3_vtab **p
40fa0 70 56 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a pVTab, char**);.
40fb0 20 20 69 6e 74 20 28 2a 78 43 6f 6e 6e 65 63 74 int (*xConnect
40fc0 29 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 )(sqlite3*, void
40fd0 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20 *pAux,.
40fe0 20 20 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c int argc,
40ff0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e const char *con
41000 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20 st*argv,.
41010 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
41020 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 vtab **ppVTab, c
41030 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a har**);. int (*
41040 78 42 65 73 74 49 6e 64 65 78 29 28 73 71 6c 69 xBestIndex)(sqli
41050 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c te3_vtab *pVTab,
41060 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 sqlite3_index_i
41070 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 nfo*);. int (*x
41080 44 69 73 63 6f 6e 6e 65 63 74 29 28 73 71 6c 69 Disconnect)(sqli
41090 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 te3_vtab *pVTab)
410a0 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 73 74 72 ;. int (*xDestr
410b0 6f 79 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 oy)(sqlite3_vtab
410c0 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 *pVTab);. int
410d0 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74 65 33 (*xOpen)(sqlite3
410e0 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 _vtab *pVTab, sq
410f0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
41100 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 3b 0a 20 r **ppCursor);.
41110 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 73 int (*xClose)(s
41120 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
41130 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 or*);. int (*xF
41140 69 6c 74 65 72 29 28 73 71 6c 69 74 65 33 5f 76 ilter)(sqlite3_v
41150 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 69 6e 74 tab_cursor*, int
41160 20 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 20 63 idxNum, const c
41170 68 61 72 20 2a 69 64 78 53 74 72 2c 0a 20 20 20 har *idxStr,.
41180 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 int
41190 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 argc, sqlite3_v
411a0 61 6c 75 65 20 2a 2a 61 72 67 76 29 3b 0a 20 20 alue **argv);.
411b0 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 73 71 6c int (*xNext)(sql
411c0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
411d0 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 45 6f 66 *);. int (*xEof
411e0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 )(sqlite3_vtab_c
411f0 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 ursor*);. int (
41200 2a 78 43 6f 6c 75 6d 6e 29 28 73 71 6c 69 74 65 *xColumn)(sqlite
41210 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 3_vtab_cursor*,
41220 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
41230 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a , int);. int (*
41240 78 52 6f 77 69 64 29 28 73 71 6c 69 74 65 33 5f xRowid)(sqlite3_
41250 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 73 71 vtab_cursor*, sq
41260 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 52 6f lite3_int64 *pRo
41270 77 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55 wid);. int (*xU
41280 70 64 61 74 65 29 28 73 71 6c 69 74 65 33 5f 76 pdate)(sqlite3_v
41290 74 61 62 20 2a 2c 20 69 6e 74 2c 20 73 71 6c 69 tab *, int, sqli
412a0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 2c 20 73 71 te3_value **, sq
412b0 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 29 3b 0a lite3_int64 *);.
412c0 20 20 69 6e 74 20 28 2a 78 42 65 67 69 6e 29 28 int (*xBegin)(
412d0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
412e0 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 Tab);. int (*xS
412f0 79 6e 63 29 28 73 71 6c 69 74 65 33 5f 76 74 61 ync)(sqlite3_vta
41300 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 b *pVTab);. int
41310 20 28 2a 78 43 6f 6d 6d 69 74 29 28 73 71 6c 69 (*xCommit)(sqli
41320 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 te3_vtab *pVTab)
41330 3b 0a 20 20 69 6e 74 20 28 2a 78 52 6f 6c 6c 62 ;. int (*xRollb
41340 61 63 6b 29 28 73 71 6c 69 74 65 33 5f 76 74 61 ack)(sqlite3_vta
41350 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 b *pVTab);. int
41360 20 28 2a 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e (*xFindFunction
41370 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a )(sqlite3_vtab *
41380 70 56 74 61 62 2c 20 69 6e 74 20 6e 41 72 67 2c pVtab, int nArg,
41390 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
413a0 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 me,.
413b0 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 20 void
413c0 28 2a 2a 70 78 46 75 6e 63 29 28 73 71 6c 69 74 (**pxFunc)(sqlit
413d0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c e3_context*,int,
413e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 sqlite3_value**)
413f0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
41400 20 20 20 20 20 20 20 20 20 76 6f 69 64 20 2a 2a void **
41410 70 70 41 72 67 29 3b 0a 20 20 69 6e 74 20 28 2a ppArg);. int (*
41420 78 52 65 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 xRename)(sqlite3
41430 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20 63 6f _vtab *pVtab, co
41440 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 77 29 3b nst char *zNew);
41450 0a 20 20 2f 2a 20 54 68 65 20 6d 65 74 68 6f 64 . /* The method
41460 73 20 61 62 6f 76 65 20 61 72 65 20 69 6e 20 76 s above are in v
41470 65 72 73 69 6f 6e 20 31 20 6f 66 20 74 68 65 20 ersion 1 of the
41480 73 71 6c 69 74 65 5f 6d 6f 64 75 6c 65 20 6f 62 sqlite_module ob
41490 6a 65 63 74 2e 20 54 68 6f 73 65 20 0a 20 20 2a ject. Those . *
414a0 2a 20 62 65 6c 6f 77 20 61 72 65 20 66 6f 72 20 * below are for
414b0 76 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 67 72 version 2 and gr
414c0 65 61 74 65 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 eater. */. int
414d0 28 2a 78 53 61 76 65 70 6f 69 6e 74 29 28 73 71 (*xSavepoint)(sq
414e0 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 lite3_vtab *pVTa
414f0 62 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 b, int);. int (
41500 2a 78 52 65 6c 65 61 73 65 29 28 73 71 6c 69 74 *xRelease)(sqlit
41510 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 e3_vtab *pVTab,
41520 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 int);. int (*xR
41530 6f 6c 6c 62 61 63 6b 54 6f 29 28 73 71 6c 69 74 ollbackTo)(sqlit
41540 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 e3_vtab *pVTab,
41550 69 6e 74 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 int);.};../*.**
41560 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 CAPI3REF: Virtua
41570 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67 l Table Indexing
41580 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 Information.**
41590 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 KEYWORDS: sqlite
415a0 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 0a 3_index_info.**.
415b0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 ** The sqlite3_i
415c0 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 ndex_info struct
415d0 75 72 65 20 61 6e 64 20 69 74 73 20 73 75 62 73 ure and its subs
415e0 74 72 75 63 74 75 72 65 73 20 69 73 20 75 73 65 tructures is use
415f0 64 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20 d as part.** of
41600 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 the [virtual tab
41610 6c 65 5d 20 69 6e 74 65 72 66 61 63 65 20 74 6f le] interface to
41620 0a 2a 2a 20 70 61 73 73 20 69 6e 66 6f 72 6d 61 .** pass informa
41630 74 69 6f 6e 20 69 6e 74 6f 20 61 6e 64 20 72 65 tion into and re
41640 63 65 69 76 65 20 74 68 65 20 72 65 70 6c 79 20 ceive the reply
41650 66 72 6f 6d 20 74 68 65 20 5b 78 42 65 73 74 49 from the [xBestI
41660 6e 64 65 78 5d 0a 2a 2a 20 6d 65 74 68 6f 64 20 ndex].** method
41670 6f 66 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61 of a [virtual ta
41680 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 54 68 ble module]. Th
41690 65 20 66 69 65 6c 64 73 20 75 6e 64 65 72 20 2a e fields under *
416a0 2a 49 6e 70 75 74 73 2a 2a 20 61 72 65 20 74 68 *Inputs** are th
416b0 65 0a 2a 2a 20 69 6e 70 75 74 73 20 74 6f 20 78 e.** inputs to x
416c0 42 65 73 74 49 6e 64 65 78 20 61 6e 64 20 61 72 BestIndex and ar
416d0 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 78 42 e read-only. xB
416e0 65 73 74 49 6e 64 65 78 20 69 6e 73 65 72 74 73 estIndex inserts
416f0 20 69 74 73 0a 2a 2a 20 72 65 73 75 6c 74 73 20 its.** results
41700 69 6e 74 6f 20 74 68 65 20 2a 2a 4f 75 74 70 75 into the **Outpu
41710 74 73 2a 2a 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a ts** fields..**.
41720 2a 2a 20 5e 28 54 68 65 20 61 43 6f 6e 73 74 72 ** ^(The aConstr
41730 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 72 65 63 aint[] array rec
41740 6f 72 64 73 20 57 48 45 52 45 20 63 6c 61 75 73 ords WHERE claus
41750 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 66 e constraints of
41760 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a the form:.**.**
41770 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 63 6f 6c <blockquote>col
41780 75 6d 6e 20 4f 50 20 65 78 70 72 3c 2f 62 6c 6f umn OP expr</blo
41790 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 77 ckquote>.**.** w
417a0 68 65 72 65 20 4f 50 20 69 73 20 3d 2c 20 26 6c here OP is =, &l
417b0 74 3b 2c 20 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c t;, <=, >,
417c0 20 6f 72 20 26 67 74 3b 3d 2e 29 5e 20 20 5e 28 or >=.)^ ^(
417d0 54 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 6f The particular o
417e0 70 65 72 61 74 6f 72 20 69 73 0a 2a 2a 20 73 74 perator is.** st
417f0 6f 72 65 64 20 69 6e 20 61 43 6f 6e 73 74 72 61 ored in aConstra
41800 69 6e 74 5b 5d 2e 6f 70 20 75 73 69 6e 67 20 6f int[].op using o
41810 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 53 51 ne of the.** [SQ
41820 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 LITE_INDEX_CONST
41830 52 41 49 4e 54 5f 45 51 20 7c 20 53 51 4c 49 54 RAINT_EQ | SQLIT
41840 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 E_INDEX_CONSTRAI
41850 4e 54 5f 20 76 61 6c 75 65 73 5d 2e 29 5e 0a 2a NT_ values].)^.*
41860 2a 20 5e 28 54 68 65 20 69 6e 64 65 78 20 6f 66 * ^(The index of
41870 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 73 the column is s
41880 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 61 43 6f 6e tored in.** aCon
41890 73 74 72 61 69 6e 74 5b 5d 2e 69 43 6f 6c 75 6d straint[].iColum
418a0 6e 2e 29 5e 20 20 5e 28 61 43 6f 6e 73 74 72 61 n.)^ ^(aConstra
418b0 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69 73 20 int[].usable is
418c0 54 52 55 45 20 69 66 20 74 68 65 0a 2a 2a 20 65 TRUE if the.** e
418d0 78 70 72 20 6f 6e 20 74 68 65 20 72 69 67 68 74 xpr on the right
418e0 2d 68 61 6e 64 20 73 69 64 65 20 63 61 6e 20 62 -hand side can b
418f0 65 20 65 76 61 6c 75 61 74 65 64 20 28 61 6e 64 e evaluated (and
41900 20 74 68 75 73 20 74 68 65 20 63 6f 6e 73 74 72 thus the constr
41910 61 69 6e 74 0a 2a 2a 20 69 73 20 75 73 61 62 6c aint.** is usabl
41920 65 29 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 e) and false if
41930 69 74 20 63 61 6e 6e 6f 74 2e 29 5e 0a 2a 2a 0a it cannot.)^.**.
41940 2a 2a 20 5e 54 68 65 20 6f 70 74 69 6d 69 7a 65 ** ^The optimize
41950 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 r automatically
41960 69 6e 76 65 72 74 73 20 74 65 72 6d 73 20 6f 66 inverts terms of
41970 20 74 68 65 20 66 6f 72 6d 20 22 65 78 70 72 20 the form "expr
41980 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20 61 6e OP column".** an
41990 64 20 6d 61 6b 65 73 20 6f 74 68 65 72 20 73 69 d makes other si
419a0 6d 70 6c 69 66 69 63 61 74 69 6f 6e 73 20 74 6f mplifications to
419b0 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
419c0 65 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 e in an attempt
419d0 74 6f 0a 2a 2a 20 67 65 74 20 61 73 20 6d 61 6e to.** get as man
419e0 79 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 y WHERE clause t
419f0 65 72 6d 73 20 69 6e 74 6f 20 74 68 65 20 66 6f erms into the fo
41a00 72 6d 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 61 rm shown above a
41a10 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 5e s possible..** ^
41a20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b The aConstraint[
41a30 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 72 65 70 ] array only rep
41a40 6f 72 74 73 20 57 48 45 52 45 20 63 6c 61 75 73 orts WHERE claus
41a50 65 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65 e terms that are
41a60 0a 2a 2a 20 72 65 6c 65 76 61 6e 74 20 74 6f 20 .** relevant to
41a70 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 the particular v
41a80 69 72 74 75 61 6c 20 74 61 62 6c 65 20 62 65 69 irtual table bei
41a90 6e 67 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a ng queried..**.*
41aa0 2a 20 5e 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 * ^Information a
41ab0 62 6f 75 74 20 74 68 65 20 4f 52 44 45 52 20 42 bout the ORDER B
41ac0 59 20 63 6c 61 75 73 65 20 69 73 20 73 74 6f 72 Y clause is stor
41ad0 65 64 20 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d ed in aOrderBy[]
41ae0 2e 0a 2a 2a 20 5e 45 61 63 68 20 74 65 72 6d 20 ..** ^Each term
41af0 6f 66 20 61 4f 72 64 65 72 42 79 20 72 65 63 6f of aOrderBy reco
41b00 72 64 73 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 rds a column of
41b10 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
41b20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b use..**.** The [
41b30 78 42 65 73 74 49 6e 64 65 78 5d 20 6d 65 74 68 xBestIndex] meth
41b40 6f 64 20 6d 75 73 74 20 66 69 6c 6c 20 61 43 6f od must fill aCo
41b50 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 nstraintUsage[]
41b60 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e with information
41b70 0a 2a 2a 20 61 62 6f 75 74 20 77 68 61 74 20 70 .** about what p
41b80 61 72 61 6d 65 74 65 72 73 20 74 6f 20 70 61 73 arameters to pas
41b90 73 20 74 6f 20 78 46 69 6c 74 65 72 2e 20 20 5e s to xFilter. ^
41ba0 49 66 20 61 72 67 76 49 6e 64 65 78 3e 30 20 74 If argvIndex>0 t
41bb0 68 65 6e 0a 2a 2a 20 74 68 65 20 72 69 67 68 74 hen.** the right
41bc0 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 -hand side of th
41bd0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
41be0 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 69 73 aConstraint[] is
41bf0 20 65 76 61 6c 75 61 74 65 64 0a 2a 2a 20 61 6e evaluated.** an
41c00 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 61 72 d becomes the ar
41c10 67 76 49 6e 64 65 78 2d 74 68 20 65 6e 74 72 79 gvIndex-th entry
41c20 20 69 6e 20 61 72 67 76 2e 20 20 5e 28 49 66 20 in argv. ^(If
41c30 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 aConstraintUsage
41c40 5b 5d 2e 6f 6d 69 74 0a 2a 2a 20 69 73 20 74 72 [].omit.** is tr
41c50 75 65 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e ue, then the con
41c60 73 74 72 61 69 6e 74 20 69 73 20 61 73 73 75 6d straint is assum
41c70 65 64 20 74 6f 20 62 65 20 66 75 6c 6c 79 20 68 ed to be fully h
41c80 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a andled by the.**
41c90 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61 virtual table a
41ca0 6e 64 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 nd is not checke
41cb0 64 20 61 67 61 69 6e 20 62 79 20 53 51 4c 69 74 d again by SQLit
41cc0 65 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 e.)^.**.** ^The
41cd0 69 64 78 4e 75 6d 20 61 6e 64 20 69 64 78 50 74 idxNum and idxPt
41ce0 72 20 76 61 6c 75 65 73 20 61 72 65 20 72 65 63 r values are rec
41cf0 6f 72 64 65 64 20 61 6e 64 20 70 61 73 73 65 64 orded and passed
41d00 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 5b 78 46 into the.** [xF
41d10 69 6c 74 65 72 5d 20 6d 65 74 68 6f 64 2e 0a 2a ilter] method..*
41d20 2a 20 5e 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 * ^[sqlite3_free
41d30 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f 20 66 ()] is used to f
41d40 72 65 65 20 69 64 78 50 74 72 20 69 66 20 61 6e ree idxPtr if an
41d50 64 20 6f 6e 6c 79 20 69 66 0a 2a 2a 20 6e 65 65 d only if.** nee
41d60 64 54 6f 46 72 65 65 49 64 78 50 74 72 20 69 73 dToFreeIdxPtr is
41d70 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 true..**.** ^Th
41d80 65 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 e orderByConsume
41d90 64 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 75 74 d means that out
41da0 70 75 74 20 66 72 6f 6d 20 5b 78 46 69 6c 74 65 put from [xFilte
41db0 72 5d 2f 5b 78 4e 65 78 74 5d 20 77 69 6c 6c 20 r]/[xNext] will
41dc0 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 74 68 65 20 occur in.** the
41dd0 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20 74 6f correct order to
41de0 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52 44 satisfy the ORD
41df0 45 52 20 42 59 20 63 6c 61 75 73 65 20 73 6f 20 ER BY clause so
41e00 74 68 61 74 20 6e 6f 20 73 65 70 61 72 61 74 65 that no separate
41e10 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 73 74 65 70 .** sorting step
41e20 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a is required..**
41e30 0a 2a 2a 20 5e 54 68 65 20 65 73 74 69 6d 61 74 .** ^The estimat
41e40 65 64 43 6f 73 74 20 76 61 6c 75 65 20 69 73 20 edCost value is
41e50 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 an estimate of t
41e60 68 65 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e 67 he cost of doing
41e70 20 74 68 65 0a 2a 2a 20 70 61 72 74 69 63 75 6c the.** particul
41e80 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 75 ar lookup. A fu
41e90 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 74 61 62 ll scan of a tab
41ea0 6c 65 20 77 69 74 68 20 4e 20 65 6e 74 72 69 65 le with N entrie
41eb0 73 20 73 68 6f 75 6c 64 20 68 61 76 65 0a 2a 2a s should have.**
41ec0 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20 41 a cost of N. A
41ed0 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20 6f binary search o
41ee0 66 20 61 20 74 61 62 6c 65 20 6f 66 20 4e 20 65 f a table of N e
41ef0 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 ntries should ha
41f00 76 65 20 61 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 ve a.** cost of
41f10 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 6c 6f approximately lo
41f20 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 g(N)..*/.struct
41f30 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e sqlite3_index_in
41f40 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e 70 75 74 73 fo {. /* Inputs
41f50 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 74 */. int nConst
41f60 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 raint;
41f70 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
41f80 74 72 69 65 73 20 69 6e 20 61 43 6f 6e 73 74 72 tries in aConstr
41f90 61 69 6e 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 aint */. struct
41fa0 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 sqlite3_index_c
41fb0 6f 6e 73 74 72 61 69 6e 74 20 7b 0a 20 20 20 20 onstraint {.
41fc0 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 int iColumn;
41fd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
41fe0 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74 2d 68 61 6e lumn on left-han
41ff0 64 20 73 69 64 65 20 6f 66 20 63 6f 6e 73 74 72 d side of constr
42000 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 75 6e 73 aint */. uns
42010 69 67 6e 65 64 20 63 68 61 72 20 6f 70 3b 20 20 igned char op;
42020 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72 /* Constr
42030 61 69 6e 74 20 6f 70 65 72 61 74 6f 72 20 2a 2f aint operator */
42040 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 . unsigned c
42050 68 61 72 20 75 73 61 62 6c 65 3b 20 20 20 20 20 har usable;
42060 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
42070 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 75 73 constraint is us
42080 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 69 6e 74 able */. int
42090 20 69 54 65 72 6d 4f 66 66 73 65 74 3b 20 20 20 iTermOffset;
420a0 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 69 /* Used i
420b0 6e 74 65 72 6e 61 6c 6c 79 20 2d 20 78 42 65 73 nternally - xBes
420c0 74 49 6e 64 65 78 20 73 68 6f 75 6c 64 20 69 67 tIndex should ig
420d0 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f nore */. } *aCo
420e0 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 20 nstraint;
420f0 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6f 66 /* Table of
42100 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f WHERE clause co
42110 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 nstraints */. i
42120 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 20 20 20 nt nOrderBy;
42130 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
42140 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 ber of terms in
42150 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
42160 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 use */. struct
42170 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 sqlite3_index_or
42180 64 65 72 62 79 20 7b 0a 20 20 20 20 20 69 6e 74 derby {. int
42190 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 iColumn;
421a0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e /* Column
421b0 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 number */.
421c0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 65 unsigned char de
421d0 73 63 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 sc; /* Tru
421e0 65 20 66 6f 72 20 44 45 53 43 2e 20 20 46 61 6c e for DESC. Fal
421f0 73 65 20 66 6f 72 20 41 53 43 2e 20 2a 2f 0a 20 se for ASC. */.
42200 20 7d 20 2a 61 4f 72 64 65 72 42 79 3b 20 20 20 } *aOrderBy;
42210 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
42220 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
42230 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f 75 74 70 75 se */. /* Outpu
42240 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 ts */. struct s
42250 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e qlite3_index_con
42260 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20 7b 0a straint_usage {.
42270 20 20 20 20 69 6e 74 20 61 72 67 76 49 6e 64 65 int argvInde
42280 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 x; /*
42290 69 66 20 3e 30 2c 20 63 6f 6e 73 74 72 61 69 6e if >0, constrain
422a0 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 72 67 t is part of arg
422b0 76 20 74 6f 20 78 46 69 6c 74 65 72 20 2a 2f 0a v to xFilter */.
422c0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 unsigned cha
422d0 72 20 6f 6d 69 74 3b 20 20 20 20 20 20 2f 2a 20 r omit; /*
422e0 44 6f 20 6e 6f 74 20 63 6f 64 65 20 61 20 74 65 Do not code a te
422f0 73 74 20 66 6f 72 20 74 68 69 73 20 63 6f 6e 73 st for this cons
42300 74 72 61 69 6e 74 20 2a 2f 0a 20 20 7d 20 2a 61 traint */. } *a
42310 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 3b ConstraintUsage;
42320 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 3b 20 20 . int idxNum;
42330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
42340 20 4e 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 Number used to
42350 69 64 65 6e 74 69 66 79 20 74 68 65 20 69 6e 64 identify the ind
42360 65 78 20 2a 2f 0a 20 20 63 68 61 72 20 2a 69 64 ex */. char *id
42370 78 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 xStr;
42380 20 20 20 2f 2a 20 53 74 72 69 6e 67 2c 20 70 6f /* String, po
42390 73 73 69 62 6c 79 20 6f 62 74 61 69 6e 65 64 20 ssibly obtained
423a0 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c from sqlite3_mal
423b0 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 65 loc */. int nee
423c0 64 54 6f 46 72 65 65 49 64 78 53 74 72 3b 20 20 dToFreeIdxStr;
423d0 20 20 20 20 2f 2a 20 46 72 65 65 20 69 64 78 53 /* Free idxS
423e0 74 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 tr using sqlite3
423f0 5f 66 72 65 65 28 29 20 69 66 20 74 72 75 65 20 _free() if true
42400 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 65 72 42 79 */. int orderBy
42410 43 6f 6e 73 75 6d 65 64 3b 20 20 20 20 20 20 20 Consumed;
42420 2f 2a 20 54 72 75 65 20 69 66 20 6f 75 74 70 75 /* True if outpu
42430 74 20 69 73 20 61 6c 72 65 61 64 79 20 6f 72 64 t is already ord
42440 65 72 65 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 ered */. double
42450 20 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3b 20 estimatedCost;
42460 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 /* Estimate
42470 64 20 63 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 d cost of using
42480 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b this index */.};
42490 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
424a0 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 : Virtual Table
424b0 43 6f 6e 73 74 72 61 69 6e 74 20 4f 70 65 72 61 Constraint Opera
424c0 74 6f 72 20 43 6f 64 65 73 0a 2a 2a 0a 2a 2a 20 tor Codes.**.**
424d0 54 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65 66 These macros def
424e0 69 6e 65 64 20 74 68 65 20 61 6c 6c 6f 77 65 64 ined the allowed
424f0 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 0a values for the.
42500 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6e 64 65 ** [sqlite3_inde
42510 78 5f 69 6e 66 6f 5d 2e 61 43 6f 6e 73 74 72 61 x_info].aConstra
42520 69 6e 74 5b 5d 2e 6f 70 20 66 69 65 6c 64 2e 20 int[].op field.
42530 20 45 61 63 68 20 76 61 6c 75 65 20 72 65 70 72 Each value repr
42540 65 73 65 6e 74 73 0a 2a 2a 20 61 6e 20 6f 70 65 esents.** an ope
42550 72 61 74 6f 72 20 74 68 61 74 20 69 73 20 70 61 rator that is pa
42560 72 74 20 6f 66 20 61 20 63 6f 6e 73 74 72 61 69 rt of a constrai
42570 6e 74 20 74 65 72 6d 20 69 6e 20 74 68 65 20 77 nt term in the w
42580 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 0a 2a HERE clause of.*
42590 2a 20 61 20 71 75 65 72 79 20 74 68 61 74 20 75 * a query that u
425a0 73 65 73 20 61 20 5b 76 69 72 74 75 61 6c 20 74 ses a [virtual t
425b0 61 62 6c 65 5d 2e 0a 2a 2f 0a 23 64 65 66 69 6e able]..*/.#defin
425c0 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 e SQLITE_INDEX_C
425d0 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 20 20 20 ONSTRAINT_EQ
425e0 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
425f0 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e _INDEX_CONSTRAIN
42600 54 5f 47 54 20 20 20 20 34 0a 23 64 65 66 69 6e T_GT 4.#defin
42610 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 e SQLITE_INDEX_C
42620 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 20 20 20 20 ONSTRAINT_LE
42630 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 8.#define SQLITE
42640 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e _INDEX_CONSTRAIN
42650 54 5f 4c 54 20 20 20 20 31 36 0a 23 64 65 66 69 T_LT 16.#defi
42660 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f ne SQLITE_INDEX_
42670 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 20 20 20 CONSTRAINT_GE
42680 20 33 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 32.#define SQLI
42690 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 TE_INDEX_CONSTRA
426a0 49 4e 54 5f 4d 41 54 43 48 20 36 34 0a 0a 2f 2a INT_MATCH 64../*
426b0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 .** CAPI3REF: Re
426c0 67 69 73 74 65 72 20 41 20 56 69 72 74 75 61 6c gister A Virtual
426d0 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 Table Implement
426e0 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 ation.**.** ^The
426f0 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 se routines are
42700 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 used to register
42710 20 61 20 6e 65 77 20 5b 76 69 72 74 75 61 6c 20 a new [virtual
42720 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 6e 61 table module] na
42730 6d 65 2e 0a 2a 2a 20 5e 4d 6f 64 75 6c 65 20 6e me..** ^Module n
42740 61 6d 65 73 20 6d 75 73 74 20 62 65 20 72 65 67 ames must be reg
42750 69 73 74 65 72 65 64 20 62 65 66 6f 72 65 0a 2a istered before.*
42760 2a 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 * creating a new
42770 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d [virtual table]
42780 20 75 73 69 6e 67 20 74 68 65 20 6d 6f 64 75 6c using the modul
42790 65 20 61 6e 64 20 62 65 66 6f 72 65 20 75 73 69 e and before usi
427a0 6e 67 20 61 0a 2a 2a 20 70 72 65 65 78 69 73 74 ng a.** preexist
427b0 69 6e 67 20 5b 76 69 72 74 75 61 6c 20 74 61 62 ing [virtual tab
427c0 6c 65 5d 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 le] for the modu
427d0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 6d le..**.** ^The m
427e0 6f 64 75 6c 65 20 6e 61 6d 65 20 69 73 20 72 65 odule name is re
427f0 67 69 73 74 65 72 65 64 20 6f 6e 20 74 68 65 20 gistered on the
42800 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
42810 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 65 64 0a tion] specified.
42820 2a 2a 20 62 79 20 74 68 65 20 66 69 72 73 74 20 ** by the first
42830 70 61 72 61 6d 65 74 65 72 2e 20 20 5e 54 68 65 parameter. ^The
42840 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 name of the mod
42850 75 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 ule is given by
42860 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 the .** second p
42870 61 72 61 6d 65 74 65 72 2e 20 20 5e 54 68 65 20 arameter. ^The
42880 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 third parameter
42890 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a is a pointer to.
428a0 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 ** the implement
428b0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 76 69 ation of the [vi
428c0 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 rtual table modu
428d0 6c 65 5d 2e 20 20 20 5e 54 68 65 20 66 6f 75 72 le]. ^The four
428e0 74 68 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 th.** parameter
428f0 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 is an arbitrary
42900 63 6c 69 65 6e 74 20 64 61 74 61 20 70 6f 69 6e client data poin
42910 74 65 72 20 74 68 61 74 20 69 73 20 70 61 73 73 ter that is pass
42920 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 6e ed through.** in
42930 74 6f 20 74 68 65 20 5b 78 43 72 65 61 74 65 5d to the [xCreate]
42940 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 63 74 5d 20 and [xConnect]
42950 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 76 methods of the v
42960 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 irtual table mod
42970 75 6c 65 0a 2a 2a 20 77 68 65 6e 20 61 20 6e 65 ule.** when a ne
42980 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 w virtual table
42990 69 73 20 62 65 20 62 65 69 6e 67 20 63 72 65 61 is be being crea
429a0 74 65 64 20 6f 72 20 72 65 69 6e 69 74 69 61 6c ted or reinitial
429b0 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 ized..**.** ^The
429c0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
429d0 6d 6f 64 75 6c 65 5f 76 32 28 29 20 69 6e 74 65 module_v2() inte
429e0 72 66 61 63 65 20 68 61 73 20 61 20 66 69 66 74 rface has a fift
429f0 68 20 70 61 72 61 6d 65 74 65 72 20 77 68 69 63 h parameter whic
42a00 68 0a 2a 2a 20 69 73 20 61 20 70 6f 69 6e 74 65 h.** is a pointe
42a10 72 20 74 6f 20 61 20 64 65 73 74 72 75 63 74 6f r to a destructo
42a20 72 20 66 6f 72 20 74 68 65 20 70 43 6c 69 65 6e r for the pClien
42a30 74 44 61 74 61 2e 20 20 5e 53 51 4c 69 74 65 20 tData. ^SQLite
42a40 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 will.** invoke t
42a50 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 he destructor fu
42a60 6e 63 74 69 6f 6e 20 28 69 66 20 69 74 20 69 73 nction (if it is
42a70 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 65 6e 20 not NULL) when
42a80 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c 6f 6e SQLite.** no lon
42a90 67 65 72 20 6e 65 65 64 73 20 74 68 65 20 70 43 ger needs the pC
42aa0 6c 69 65 6e 74 44 61 74 61 20 70 6f 69 6e 74 65 lientData pointe
42ab0 72 2e 20 20 5e 54 68 65 20 64 65 73 74 72 75 63 r. ^The destruc
42ac0 74 6f 72 20 77 69 6c 6c 20 61 6c 73 6f 0a 2a 2a tor will also.**
42ad0 20 62 65 20 69 6e 76 6f 6b 65 64 20 69 66 20 74 be invoked if t
42ae0 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 he call to sqlit
42af0 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 e3_create_module
42b00 5f 76 32 28 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 _v2() fails..**
42b10 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 ^The sqlite3_cre
42b20 61 74 65 5f 6d 6f 64 75 6c 65 28 29 0a 2a 2a 20 ate_module().**
42b30 69 6e 74 65 72 66 61 63 65 20 69 73 20 65 71 75 interface is equ
42b40 69 76 61 6c 65 6e 74 20 74 6f 20 73 71 6c 69 74 ivalent to sqlit
42b50 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 e3_create_module
42b60 5f 76 32 28 29 20 77 69 74 68 20 61 20 4e 55 4c _v2() with a NUL
42b70 4c 0a 2a 2a 20 64 65 73 74 72 75 63 74 6f 72 2e L.** destructor.
42b80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
42b90 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
42ba0 65 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 e_module(. sqli
42bb0 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 te3 *db,
42bc0 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 /* SQLite
42bd0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 connection to r
42be0 65 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 egister module w
42bf0 69 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ith */. const c
42c00 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 har *zName,
42c10 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
42c20 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 he module */. c
42c30 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 onst sqlite3_mod
42c40 75 6c 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 ule *p, /* Met
42c50 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 hods for the mod
42c60 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ule */. void *p
42c70 43 6c 69 65 6e 74 44 61 74 61 20 20 20 20 20 20 ClientData
42c80 20 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 /* Client da
42c90 74 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 ta for xCreate/x
42ca0 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 53 51 Connect */.);.SQ
42cb0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
42cc0 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 ite3_create_modu
42cd0 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 le_v2(. sqlite3
42ce0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
42cf0 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f /* SQLite co
42d00 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 nnection to regi
42d10 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 ster module with
42d20 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
42d30 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 *zName,
42d40 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
42d50 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 module */. cons
42d60 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
42d70 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 *p, /* Method
42d80 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 s for the module
42d90 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 */. void *pCli
42da0 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20 20 entData,
42db0 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20 /* Client data
42dc0 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e for xCreate/xCon
42dd0 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a nect */. void(*
42de0 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 xDestroy)(void*)
42df0 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 /* Module d
42e00 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 estructor functi
42e10 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 on */.);../*.**
42e20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 CAPI3REF: Virtua
42e30 6c 20 54 61 62 6c 65 20 49 6e 73 74 61 6e 63 65 l Table Instance
42e40 20 4f 62 6a 65 63 74 0a 2a 2a 20 4b 45 59 57 4f Object.** KEYWO
42e50 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 RDS: sqlite3_vta
42e60 62 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 5b 76 b.**.** Every [v
42e70 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 irtual table mod
42e80 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ule] implementat
42e90 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62 63 6c ion uses a subcl
42ea0 61 73 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 6f ass.** of this o
42eb0 62 6a 65 63 74 20 74 6f 20 64 65 73 63 72 69 62 bject to describ
42ec0 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 e a particular i
42ed0 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 nstance.** of th
42ee0 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 e [virtual table
42ef0 5d 2e 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 ]. Each subclas
42f00 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 s will.** be tai
42f10 6c 6f 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 lored to the spe
42f20 63 69 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 cific needs of t
42f30 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d he module implem
42f40 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 entation..** The
42f50 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 purpose of this
42f60 20 73 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 superclass is t
42f70 6f 20 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e o define certain
42f80 20 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 fields that are
42f90 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c .** common to al
42fa0 6c 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 l module impleme
42fb0 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 ntations..**.**
42fc0 5e 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 ^Virtual tables
42fd0 6d 65 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20 methods can set
42fe0 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
42ff0 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0a by assigning a.
43000 2a 2a 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e ** string obtain
43010 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 ed from [sqlite3
43020 5f 6d 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a _mprintf()] to z
43030 45 72 72 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 ErrMsg. The met
43040 68 6f 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 hod should.** ta
43050 6b 65 20 63 61 72 65 20 74 68 61 74 20 61 6e 79 ke care that any
43060 20 70 72 69 6f 72 20 73 74 72 69 6e 67 20 69 73 prior string is
43070 20 66 72 65 65 64 20 62 79 20 61 20 63 61 6c 6c freed by a call
43080 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 to [sqlite3_fre
43090 65 28 29 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f e()].** prior to
430a0 20 61 73 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 assigning a new
430b0 20 73 74 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d string to zErrM
430c0 73 67 2e 20 20 5e 41 66 74 65 72 20 74 68 65 20 sg. ^After the
430d0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a error message.**
430e0 20 69 73 20 64 65 6c 69 76 65 72 65 64 20 75 70 is delivered up
430f0 20 74 6f 20 74 68 65 20 63 6c 69 65 6e 74 20 61 to the client a
43100 70 70 6c 69 63 61 74 69 6f 6e 2c 20 74 68 65 20 pplication, the
43110 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 string will be a
43120 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 utomatically.**
43130 66 72 65 65 64 20 62 79 20 73 71 6c 69 74 65 33 freed by sqlite3
43140 5f 66 72 65 65 28 29 20 61 6e 64 20 74 68 65 20 _free() and the
43150 7a 45 72 72 4d 73 67 20 66 69 65 6c 64 20 77 69 zErrMsg field wi
43160 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e 0a 2a 2f ll be zeroed..*/
43170 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
43180 76 74 61 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73 vtab {. const s
43190 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
431a0 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 Module; /* The
431b0 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68 69 73 20 module for this
431c0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f virtual table */
431d0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
431e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
431f0 20 20 20 2f 2a 20 4e 4f 20 4c 4f 4e 47 45 52 20 /* NO LONGER
43200 55 53 45 44 20 2a 2f 0a 20 20 63 68 61 72 20 2a USED */. char *
43210 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 zErrMsg;
43220 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 /* Err
43230 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 or message from
43240 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 sqlite3_mprintf(
43250 29 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 ) */. /* Virtua
43260 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e l table implemen
43270 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 tations will typ
43280 69 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 ically add addit
43290 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a ional fields */.
432a0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 };../*.** CAPI3R
432b0 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c EF: Virtual Tabl
432c0 65 20 43 75 72 73 6f 72 20 4f 62 6a 65 63 74 0a e Cursor Object.
432d0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c ** KEYWORDS: sql
432e0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
432f0 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 {virtual table
43300 63 75 72 73 6f 72 7d 0a 2a 2a 0a 2a 2a 20 45 76 cursor}.**.** Ev
43310 65 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62 ery [virtual tab
43320 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 le module] imple
43330 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 mentation uses a
43340 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 subclass of the
43350 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 .** following st
43360 72 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72 ructure to descr
43370 69 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 ibe cursors that
43380 20 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 0a point into the.
43390 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c ** [virtual tabl
433a0 65 5d 20 61 6e 64 20 61 72 65 20 75 73 65 64 0a e] and are used.
433b0 2a 2a 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 ** to loop throu
433c0 67 68 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 gh the virtual t
433d0 61 62 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 61 able. Cursors a
433e0 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 re created using
433f0 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
43400 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 _module.xOpen |
43410 78 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 xOpen] method of
43420 20 74 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 20 the module and
43430 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a are destroyed.**
43440 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 by the [sqlite3
43450 5f 6d 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 7c _module.xClose |
43460 20 78 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 2e xClose] method.
43470 20 20 43 75 72 73 6f 72 73 20 61 72 65 20 75 73 Cursors are us
43480 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 46 ed.** by the [xF
43490 69 6c 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c ilter], [xNext],
434a0 20 5b 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d [xEof], [xColum
434b0 6e 5d 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 5d n], and [xRowid]
434c0 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 methods.** of t
434d0 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68 he module. Each
434e0 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
434f0 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69 tation will defi
43500 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e ne.** the conten
43510 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74 t of a cursor st
43520 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20 ructure to suit
43530 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a its own needs..*
43540 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63 *.** This superc
43550 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f lass exists in o
43560 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66 rder to define f
43570 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 ields of the cur
43580 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 sor that.** are
43590 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d common to all im
435a0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a plementations..*
435b0 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 /.struct sqlite3
435c0 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20 _vtab_cursor {.
435d0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
435e0 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69 Vtab; /* Vi
435f0 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74 rtual table of t
43600 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 his cursor */.
43610 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 /* Virtual table
43620 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
43630 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 will typically
43640 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 add additional f
43650 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ields */.};../*.
43660 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 63 ** CAPI3REF: Dec
43670 6c 61 72 65 20 54 68 65 20 53 63 68 65 6d 61 20 lare The Schema
43680 4f 66 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 Of A Virtual Tab
43690 6c 65 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 5b 78 le.**.** ^The [x
436a0 43 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f Create] and [xCo
436b0 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f nnect] methods o
436c0 66 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 f a.** [virtual
436d0 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 61 table module] ca
436e0 6c 6c 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 ll this interfac
436f0 65 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65 20 e.** to declare
43700 74 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65 20 the format (the
43710 6e 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74 79 names and dataty
43720 70 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d pes of the colum
43730 6e 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 ns) of.** the vi
43740 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68 65 rtual tables the
43750 79 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a y implement..*/.
43760 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
43770 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 qlite3_declare_v
43780 74 61 62 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f tab(sqlite3*, co
43790 6e 73 74 20 63 68 61 72 20 2a 7a 53 51 4c 29 3b nst char *zSQL);
437a0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
437b0 3a 20 4f 76 65 72 6c 6f 61 64 20 41 20 46 75 6e : Overload A Fun
437c0 63 74 69 6f 6e 20 46 6f 72 20 41 20 56 69 72 74 ction For A Virt
437d0 75 61 6c 20 54 61 62 6c 65 0a 2a 2a 0a 2a 2a 20 ual Table.**.**
437e0 5e 28 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 ^(Virtual tables
437f0 20 63 61 6e 20 70 72 6f 76 69 64 65 20 61 6c 74 can provide alt
43800 65 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d 65 ernative impleme
43810 6e 74 61 74 69 6f 6e 73 20 6f 66 20 66 75 6e 63 ntations of func
43820 74 69 6f 6e 73 0a 2a 2a 20 75 73 69 6e 67 20 74 tions.** using t
43830 68 65 20 5b 78 46 69 6e 64 46 75 6e 63 74 69 6f he [xFindFunctio
43840 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 n] method of the
43850 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 [virtual table
43860 6d 6f 64 75 6c 65 5d 2e 20 20 0a 2a 2a 20 42 75 module]. .** Bu
43870 74 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e t global version
43880 73 20 6f 66 20 74 68 6f 73 65 20 66 75 6e 63 74 s of those funct
43890 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74 20 65 78 69 ions.** must exi
438a0 73 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 st in order to b
438b0 65 20 6f 76 65 72 6c 6f 61 64 65 64 2e 29 5e 0a e overloaded.)^.
438c0 2a 2a 0a 2a 2a 20 5e 28 54 68 69 73 20 41 50 49 **.** ^(This API
438d0 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67 6c makes sure a gl
438e0 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66 20 obal version of
438f0 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 a function with
43900 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 a particular.**
43910 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 name and number
43920 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 65 78 of parameters ex
43930 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75 63 ists. If no suc
43940 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 h function exist
43950 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73 s.** before this
43960 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c 20 API is called,
43970 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 a new function i
43980 73 20 63 72 65 61 74 65 64 2e 29 5e 20 20 5e 54 s created.)^ ^T
43990 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
439a0 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 n.** of the new
439b0 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 function always
439c0 63 61 75 73 65 73 20 61 6e 20 65 78 63 65 70 74 causes an except
439d0 69 6f 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e ion to be thrown
439e0 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 . So.** the new
439f0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 function is not
43a00 20 67 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 good for anythi
43a10 6e 67 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49 ng by itself. I
43a20 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f ts only.** purpo
43a30 73 65 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c se is to be a pl
43a40 61 63 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 aceholder functi
43a50 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f on that can be o
43a60 76 65 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 verloaded.** by
43a70 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a [virtual table
43a80 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 ]..*/.SQLITE_API
43a90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 int sqlite3_ove
43aa0 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 73 rload_function(s
43ab0 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 qlite3*, const c
43ac0 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 har *zFuncName,
43ad0 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a int nArg);../*.*
43ae0 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 * The interface
43af0 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 to the virtual-t
43b00 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 64 able mechanism d
43b10 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28 62 61 efined above (ba
43b20 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63 6f ck up.** to a co
43b30 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c 79 mment remarkably
43b40 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 similar to this
43b50 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65 6e 74 one) is current
43b60 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a ly considered.**
43b70 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e to be experimen
43b80 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66 tal. The interf
43b90 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 ace might change
43ba0 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 in incompatible
43bb0 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69 ways..** If thi
43bc0 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66 s is a problem f
43bd0 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 or you, do not u
43be0 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 se the interface
43bf0 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a at this time..*
43c00 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69 *.** When the vi
43c10 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68 rtual-table mech
43c20 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73 anism stabilizes
43c30 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72 , we will declar
43c40 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61 e the.** interfa
43c50 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72 ce fixed, suppor
43c60 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c t it indefinitel
43c70 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 y, and remove th
43c80 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a 0a is comment..*/..
43c90 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
43ca0 41 20 48 61 6e 64 6c 65 20 54 6f 20 41 6e 20 4f A Handle To An O
43cb0 70 65 6e 20 42 4c 4f 42 0a 2a 2a 20 4b 45 59 57 pen BLOB.** KEYW
43cc0 4f 52 44 53 3a 20 7b 42 4c 4f 42 20 68 61 6e 64 ORDS: {BLOB hand
43cd0 6c 65 7d 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 le} {BLOB handle
43ce0 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 s}.**.** An inst
43cf0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a ance of this obj
43d00 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 ect represents a
43d10 6e 20 6f 70 65 6e 20 42 4c 4f 42 20 6f 6e 20 77 n open BLOB on w
43d20 68 69 63 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 hich.** [sqlite3
43d30 5f 62 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 _blob_open | inc
43d40 72 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f remental BLOB I/
43d50 4f 5d 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72 O] can be perfor
43d60 6d 65 64 2e 0a 2a 2a 20 5e 4f 62 6a 65 63 74 73 med..** ^Objects
43d70 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 61 72 of this type ar
43d80 65 20 63 72 65 61 74 65 64 20 62 79 20 5b 73 71 e created by [sq
43d90 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 lite3_blob_open(
43da0 29 5d 0a 2a 2a 20 61 6e 64 20 64 65 73 74 72 6f )].** and destro
43db0 79 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f yed by [sqlite3_
43dc0 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a blob_close()]..*
43dd0 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74 65 33 5f * ^The [sqlite3_
43de0 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 6e 64 blob_read()] and
43df0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 [sqlite3_blob_w
43e00 72 69 74 65 28 29 5d 20 69 6e 74 65 72 66 61 63 rite()] interfac
43e10 65 73 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 es.** can be use
43e20 64 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 d to read or wri
43e30 74 65 20 73 6d 61 6c 6c 20 73 75 62 73 65 63 74 te small subsect
43e40 69 6f 6e 73 20 6f 66 20 74 68 65 20 42 4c 4f 42 ions of the BLOB
43e50 2e 0a 2a 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74 ..** ^The [sqlit
43e60 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 29 5d e3_blob_bytes()]
43e70 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 interface retur
43e80 6e 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 ns the size of t
43e90 68 65 20 42 4c 4f 42 20 69 6e 20 62 79 74 65 73 he BLOB in bytes
43ea0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
43eb0 75 63 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 uct sqlite3_blob
43ec0 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 3b 0a 0a sqlite3_blob;..
43ed0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
43ee0 4f 70 65 6e 20 41 20 42 4c 4f 42 20 46 6f 72 20 Open A BLOB For
43ef0 49 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 0a Incremental I/O.
43f00 2a 2a 0a 2a 2a 20 5e 28 54 68 69 73 20 69 6e 74 **.** ^(This int
43f10 65 72 66 61 63 65 73 20 6f 70 65 6e 73 20 61 20 erfaces opens a
43f20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 20 7c 20 68 [BLOB handle | h
43f30 61 6e 64 6c 65 5d 20 74 6f 20 74 68 65 20 42 4c andle] to the BL
43f40 4f 42 20 6c 6f 63 61 74 65 64 0a 2a 2a 20 69 6e OB located.** in
43f50 20 72 6f 77 20 69 52 6f 77 2c 20 63 6f 6c 75 6d row iRow, colum
43f60 6e 20 7a 43 6f 6c 75 6d 6e 2c 20 74 61 62 6c 65 n zColumn, table
43f70 20 7a 54 61 62 6c 65 20 69 6e 20 64 61 74 61 62 zTable in datab
43f80 61 73 65 20 7a 44 62 3b 0a 2a 2a 20 69 6e 20 6f ase zDb;.** in o
43f90 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 ther words, the
43fa0 73 61 6d 65 20 42 4c 4f 42 20 74 68 61 74 20 77 same BLOB that w
43fb0 6f 75 6c 64 20 62 65 20 73 65 6c 65 63 74 65 64 ould be selected
43fc0 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e by:.**.** <pre>
43fd0 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 7a .** SELECT z
43fe0 43 6f 6c 75 6d 6e 20 46 52 4f 4d 20 7a 44 62 2e Column FROM zDb.
43ff0 7a 54 61 62 6c 65 20 57 48 45 52 45 20 5b 72 6f zTable WHERE [ro
44000 77 69 64 5d 20 3d 20 69 52 6f 77 3b 0a 2a 2a 20 wid] = iRow;.**
44010 3c 2f 70 72 65 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5e </pre>)^.**.** ^
44020 49 66 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 If the flags par
44030 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 ameter is non-ze
44040 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 42 4c 4f ro, then the BLO
44050 42 20 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 B is opened for
44060 72 65 61 64 0a 2a 2a 20 61 6e 64 20 77 72 69 74 read.** and writ
44070 65 20 61 63 63 65 73 73 2e 20 5e 49 66 20 69 74 e access. ^If it
44080 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 20 42 4c is zero, the BL
44090 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 OB is opened for
440a0 20 72 65 61 64 20 61 63 63 65 73 73 2e 0a 2a 2a read access..**
440b0 20 5e 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 ^It is not poss
440c0 69 62 6c 65 20 74 6f 20 6f 70 65 6e 20 61 20 63 ible to open a c
440d0 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 70 61 olumn that is pa
440e0 72 74 20 6f 66 20 61 6e 20 69 6e 64 65 78 20 6f rt of an index o
440f0 72 20 70 72 69 6d 61 72 79 20 0a 2a 2a 20 6b 65 r primary .** ke
44100 79 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 5e y for writing. ^
44110 49 66 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 20 If [foreign key
44120 63 6f 6e 73 74 72 61 69 6e 74 73 5d 20 61 72 65 constraints] are
44130 20 65 6e 61 62 6c 65 64 2c 20 69 74 20 69 73 20 enabled, it is
44140 0a 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 .** not possible
44150 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d to open a colum
44160 6e 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f n that is part o
44170 66 20 61 20 5b 63 68 69 6c 64 20 6b 65 79 5d 20 f a [child key]
44180 66 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a for writing..**.
44190 2a 2a 20 5e 4e 6f 74 65 20 74 68 61 74 20 74 68 ** ^Note that th
441a0 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 e database name
441b0 69 73 20 6e 6f 74 20 74 68 65 20 66 69 6c 65 6e is not the filen
441c0 61 6d 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ame that contain
441d0 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 s.** the databas
441e0 65 20 62 75 74 20 72 61 74 68 65 72 20 74 68 65 e but rather the
441f0 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 6f symbolic name o
44200 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 f the database t
44210 68 61 74 0a 2a 2a 20 61 70 70 65 61 72 73 20 61 hat.** appears a
44220 66 74 65 72 20 74 68 65 20 41 53 20 6b 65 79 77 fter the AS keyw
44230 6f 72 64 20 77 68 65 6e 20 74 68 65 20 64 61 74 ord when the dat
44240 61 62 61 73 65 20 69 73 20 63 6f 6e 6e 65 63 74 abase is connect
44250 65 64 20 75 73 69 6e 67 20 5b 41 54 54 41 43 48 ed using [ATTACH
44260 5d 2e 0a 2a 2a 20 5e 46 6f 72 20 74 68 65 20 6d ]..** ^For the m
44270 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c ain database fil
44280 65 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 e, the database
44290 6e 61 6d 65 20 69 73 20 22 6d 61 69 6e 22 2e 0a name is "main"..
442a0 2a 2a 20 5e 46 6f 72 20 54 45 4d 50 20 74 61 62 ** ^For TEMP tab
442b0 6c 65 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 les, the databas
442c0 65 20 6e 61 6d 65 20 69 73 20 22 74 65 6d 70 22 e name is "temp"
442d0 2e 0a 2a 2a 0a 2a 2a 20 5e 28 4f 6e 20 73 75 63 ..**.** ^(On suc
442e0 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b cess, [SQLITE_OK
442f0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e ] is returned an
44300 64 20 74 68 65 20 6e 65 77 20 5b 42 4c 4f 42 20 d the new [BLOB
44310 68 61 6e 64 6c 65 5d 20 69 73 20 77 72 69 74 74 handle] is writt
44320 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f 62 en.** to *ppBlob
44330 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 5b . Otherwise an [
44340 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 error code] is r
44350 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 42 eturned and *ppB
44360 6c 6f 62 20 69 73 20 73 65 74 0a 2a 2a 20 74 6f lob is set.** to
44370 20 62 65 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 be a null point
44380 65 72 2e 29 5e 0a 2a 2a 20 5e 54 68 69 73 20 66 er.)^.** ^This f
44390 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65 unction sets the
443a0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
443b0 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20 63 6f 64 ction] error cod
443c0 65 20 61 6e 64 20 6d 65 73 73 61 67 65 0a 2a 2a e and message.**
443d0 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 accessible via
443e0 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 [sqlite3_errcode
443f0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
44400 5f 65 72 72 6d 73 67 28 29 5d 20 61 6e 64 20 72 _errmsg()] and r
44410 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 69 elated.** functi
44420 6f 6e 73 2e 20 5e 4e 6f 74 65 20 74 68 61 74 20 ons. ^Note that
44430 74 68 65 20 2a 70 70 42 6c 6f 62 20 76 61 72 69 the *ppBlob vari
44440 61 62 6c 65 20 69 73 20 61 6c 77 61 79 73 20 69 able is always i
44450 6e 69 74 69 61 6c 69 7a 65 64 20 69 6e 20 61 0a nitialized in a.
44460 2a 2a 20 77 61 79 20 74 68 61 74 20 6d 61 6b 65 ** way that make
44470 73 20 69 74 20 73 61 66 65 20 74 6f 20 69 6e 76 s it safe to inv
44480 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f oke [sqlite3_blo
44490 62 5f 63 6c 6f 73 65 28 29 5d 20 6f 6e 20 2a 70 b_close()] on *p
444a0 70 42 6c 6f 62 0a 2a 2a 20 72 65 67 61 72 64 6c pBlob.** regardl
444b0 65 73 73 20 6f 66 20 74 68 65 20 73 75 63 63 65 ess of the succe
444c0 73 73 20 6f 72 20 66 61 69 6c 75 72 65 20 6f 66 ss or failure of
444d0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
444e0 2a 0a 2a 2a 20 5e 28 49 66 20 74 68 65 20 72 6f *.** ^(If the ro
444f0 77 20 74 68 61 74 20 61 20 42 4c 4f 42 20 68 61 w that a BLOB ha
44500 6e 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 ndle points to i
44510 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e s modified by an
44520 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b 44 .** [UPDATE], [D
44530 45 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b 4f ELETE], or by [O
44540 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65 N CONFLICT] side
44550 2d 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 6e -effects.** then
44560 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 the BLOB handle
44570 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 is marked as "e
44580 78 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 73 xpired"..** This
44590 20 69 73 20 74 72 75 65 20 69 66 20 61 6e 79 20 is true if any
445a0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 6f column of the ro
445b0 77 20 69 73 20 63 68 61 6e 67 65 64 2c 20 65 76 w is changed, ev
445c0 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f en a column.** o
445d0 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e ther than the on
445e0 65 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c e the BLOB handl
445f0 65 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 29 5e 0a e is open on.)^.
44600 2a 2a 20 5e 43 61 6c 6c 73 20 74 6f 20 5b 73 71 ** ^Calls to [sq
44610 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 lite3_blob_read(
44620 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
44630 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 66 6f blob_write()] fo
44640 72 0a 2a 2a 20 61 6e 20 65 78 70 69 72 65 64 20 r.** an expired
44650 42 4c 4f 42 20 68 61 6e 64 6c 65 20 66 61 69 6c BLOB handle fail
44660 20 77 69 74 68 20 61 20 72 65 74 75 72 6e 20 63 with a return c
44670 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 ode of [SQLITE_A
44680 42 4f 52 54 5d 2e 0a 2a 2a 20 5e 28 43 68 61 6e BORT]..** ^(Chan
44690 67 65 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f ges written into
446a0 20 61 20 42 4c 4f 42 20 70 72 69 6f 72 20 74 6f a BLOB prior to
446b0 20 74 68 65 20 42 4c 4f 42 20 65 78 70 69 72 69 the BLOB expiri
446c0 6e 67 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 6f ng are not.** ro
446d0 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65 lled back by the
446e0 20 65 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 expiration of t
446f0 68 65 20 42 4c 4f 42 2e 20 20 53 75 63 68 20 63 he BLOB. Such c
44700 68 61 6e 67 65 73 20 77 69 6c 6c 20 65 76 65 6e hanges will even
44710 74 75 61 6c 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 tually.** commit
44720 20 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 if the transact
44730 69 6f 6e 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f ion continues to
44740 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 29 5e 0a 2a completion.)^.*
44750 2a 0a 2a 2a 20 5e 55 73 65 20 74 68 65 20 5b 73 *.** ^Use the [s
44760 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 qlite3_blob_byte
44770 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 74 s()] interface t
44780 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
44790 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6f size of.** the o
447a0 70 65 6e 65 64 20 62 6c 6f 62 2e 20 20 5e 54 68 pened blob. ^Th
447b0 65 20 73 69 7a 65 20 6f 66 20 61 20 62 6c 6f 62 e size of a blob
447c0 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e may not be chan
447d0 67 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 69 ged by this.** i
447e0 6e 74 65 72 66 61 63 65 2e 20 20 55 73 65 20 74 nterface. Use t
447f0 68 65 20 5b 55 50 44 41 54 45 5d 20 53 51 4c 20 he [UPDATE] SQL
44800 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 68 61 6e 67 command to chang
44810 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 0a e the size of a.
44820 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 5e ** blob..**.** ^
44830 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e The [sqlite3_bin
44840 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20 61 6e d_zeroblob()] an
44850 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c d [sqlite3_resul
44860 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20 69 6e t_zeroblob()] in
44870 74 65 72 66 61 63 65 73 0a 2a 2a 20 61 6e 64 20 terfaces.** and
44880 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 5b 7a 65 the built-in [ze
44890 72 6f 62 6c 6f 62 5d 20 53 51 4c 20 66 75 6e 63 roblob] SQL func
448a0 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 tion can be used
448b0 2c 20 69 66 20 64 65 73 69 72 65 64 2c 0a 2a 2a , if desired,.**
448c0 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 65 6d to create an em
448d0 70 74 79 2c 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 pty, zero-filled
448e0 20 62 6c 6f 62 20 69 6e 20 77 68 69 63 68 20 74 blob in which t
448f0 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 o read or write
44900 75 73 69 6e 67 0a 2a 2a 20 74 68 69 73 20 69 6e using.** this in
44910 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 terface..**.** T
44920 6f 20 61 76 6f 69 64 20 61 20 72 65 73 6f 75 72 o avoid a resour
44930 63 65 20 6c 65 61 6b 2c 20 65 76 65 72 79 20 6f ce leak, every o
44940 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 pen [BLOB handle
44950 5d 20 73 68 6f 75 6c 64 20 65 76 65 6e 74 75 61 ] should eventua
44960 6c 6c 79 0a 2a 2a 20 62 65 20 72 65 6c 65 61 73 lly.** be releas
44970 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 ed by a call to
44980 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c [sqlite3_blob_cl
44990 6f 73 65 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 ose()]..*/.SQLIT
449a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
449b0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 3_blob_open(. s
449c0 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 qlite3*,. const
449d0 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f char *zDb,. co
449e0 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 nst char *zTable
449f0 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ,. const char *
44a00 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 zColumn,. sqlit
44a10 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 e3_int64 iRow,.
44a20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 71 int flags,. sq
44a30 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 lite3_blob **ppB
44a40 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 lob.);../*.** CA
44a50 50 49 33 52 45 46 3a 20 4d 6f 76 65 20 61 20 42 PI3REF: Move a B
44a60 4c 4f 42 20 48 61 6e 64 6c 65 20 74 6f 20 61 20 LOB Handle to a
44a70 4e 65 77 20 52 6f 77 0a 2a 2a 0a 2a 2a 20 5e 54 New Row.**.** ^T
44a80 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
44a90 75 73 65 64 20 74 6f 20 6d 6f 76 65 20 61 6e 20 used to move an
44aa0 65 78 69 73 74 69 6e 67 20 62 6c 6f 62 20 68 61 existing blob ha
44ab0 6e 64 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 ndle so that it
44ac0 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 64 points.** to a d
44ad0 69 66 66 65 72 65 6e 74 20 72 6f 77 20 6f 66 20 ifferent row of
44ae0 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
44af0 65 20 74 61 62 6c 65 2e 20 5e 54 68 65 20 6e 65 e table. ^The ne
44b00 77 20 72 6f 77 20 69 73 20 69 64 65 6e 74 69 66 w row is identif
44b10 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 72 6f ied.** by the ro
44b20 77 69 64 20 76 61 6c 75 65 20 70 61 73 73 65 64 wid value passed
44b30 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 as the second a
44b40 72 67 75 6d 65 6e 74 2e 20 4f 6e 6c 79 20 74 68 rgument. Only th
44b50 65 20 72 6f 77 20 63 61 6e 20 62 65 0a 2a 2a 20 e row can be.**
44b60 63 68 61 6e 67 65 64 2e 20 5e 54 68 65 20 64 61 changed. ^The da
44b70 74 61 62 61 73 65 2c 20 74 61 62 6c 65 20 61 6e tabase, table an
44b80 64 20 63 6f 6c 75 6d 6e 20 6f 6e 20 77 68 69 63 d column on whic
44b90 68 20 74 68 65 20 62 6c 6f 62 20 68 61 6e 64 6c h the blob handl
44ba0 65 20 69 73 20 6f 70 65 6e 0a 2a 2a 20 72 65 6d e is open.** rem
44bb0 61 69 6e 20 74 68 65 20 73 61 6d 65 2e 20 4d 6f ain the same. Mo
44bc0 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 ving an existing
44bd0 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 74 6f 20 blob handle to
44be0 61 20 6e 65 77 20 72 6f 77 20 63 61 6e 20 62 65 a new row can be
44bf0 0a 2a 2a 20 66 61 73 74 65 72 20 74 68 61 6e 20 .** faster than
44c00 63 6c 6f 73 69 6e 67 20 74 68 65 20 65 78 69 73 closing the exis
44c10 74 69 6e 67 20 68 61 6e 64 6c 65 20 61 6e 64 20 ting handle and
44c20 6f 70 65 6e 69 6e 67 20 61 20 6e 65 77 20 6f 6e opening a new on
44c30 65 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 6e e..**.** ^(The n
44c40 65 77 20 72 6f 77 20 6d 75 73 74 20 6d 65 65 74 ew row must meet
44c50 20 74 68 65 20 73 61 6d 65 20 63 72 69 74 65 72 the same criter
44c60 69 61 20 61 73 20 66 6f 72 20 5b 73 71 6c 69 74 ia as for [sqlit
44c70 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 e3_blob_open()]
44c80 2d 0a 2a 2a 20 69 74 20 6d 75 73 74 20 65 78 69 -.** it must exi
44c90 73 74 20 61 6e 64 20 74 68 65 72 65 20 6d 75 73 st and there mus
44ca0 74 20 62 65 20 65 69 74 68 65 72 20 61 20 62 6c t be either a bl
44cb0 6f 62 20 6f 72 20 74 65 78 74 20 76 61 6c 75 65 ob or text value
44cc0 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 74 68 stored in.** th
44cd0 65 20 6e 6f 6d 69 6e 61 74 65 64 20 63 6f 6c 75 e nominated colu
44ce0 6d 6e 2e 29 5e 20 5e 49 66 20 74 68 65 20 6e 65 mn.)^ ^If the ne
44cf0 77 20 72 6f 77 20 69 73 20 6e 6f 74 20 70 72 65 w row is not pre
44d00 73 65 6e 74 20 69 6e 20 74 68 65 20 74 61 62 6c sent in the tabl
44d10 65 2c 20 6f 72 20 69 66 0a 2a 2a 20 69 74 20 64 e, or if.** it d
44d20 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 oes not contain
44d30 61 20 62 6c 6f 62 20 6f 72 20 74 65 78 74 20 76 a blob or text v
44d40 61 6c 75 65 2c 20 6f 72 20 69 66 20 61 6e 6f 74 alue, or if anot
44d50 68 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73 her error occurs
44d60 2c 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 65 , an.** SQLite e
44d70 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
44d80 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 62 6c urned and the bl
44d90 6f 62 20 68 61 6e 64 6c 65 20 69 73 20 63 6f 6e ob handle is con
44da0 73 69 64 65 72 65 64 20 61 62 6f 72 74 65 64 2e sidered aborted.
44db0 0a 2a 2a 20 5e 41 6c 6c 20 73 75 62 73 65 71 75 .** ^All subsequ
44dc0 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 ent calls to [sq
44dd0 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 lite3_blob_read(
44de0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f )], [sqlite3_blo
44df0 62 5f 77 72 69 74 65 28 29 5d 20 6f 72 0a 2a 2a b_write()] or.**
44e00 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 [sqlite3_blob_r
44e10 65 6f 70 65 6e 28 29 5d 20 6f 6e 20 61 6e 20 61 eopen()] on an a
44e20 62 6f 72 74 65 64 20 62 6c 6f 62 20 68 61 6e 64 borted blob hand
44e30 6c 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72 le immediately r
44e40 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f eturn.** SQLITE_
44e50 41 42 4f 52 54 2e 20 5e 43 61 6c 6c 69 6e 67 20 ABORT. ^Calling
44e60 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 [sqlite3_blob_by
44e70 74 65 73 28 29 5d 20 6f 6e 20 61 6e 20 61 62 6f tes()] on an abo
44e80 72 74 65 64 20 62 6c 6f 62 20 68 61 6e 64 6c 65 rted blob handle
44e90 0a 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 .** always retur
44ea0 6e 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 5e ns zero..**.** ^
44eb0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 This function se
44ec0 74 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ts the database
44ed0 68 61 6e 64 6c 65 20 65 72 72 6f 72 20 63 6f 64 handle error cod
44ee0 65 20 61 6e 64 20 6d 65 73 73 61 67 65 2e 0a 2a e and message..*
44ef0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c /.SQLITE_API SQL
44f00 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c ITE_EXPERIMENTAL
44f10 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f int sqlite3_blo
44f20 62 5f 72 65 6f 70 65 6e 28 73 71 6c 69 74 65 33 b_reopen(sqlite3
44f30 5f 62 6c 6f 62 20 2a 2c 20 73 71 6c 69 74 65 33 _blob *, sqlite3
44f40 5f 69 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a 2a 20 _int64);../*.**
44f50 43 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 CAPI3REF: Close
44f60 41 20 42 4c 4f 42 20 48 61 6e 64 6c 65 0a 2a 2a A BLOB Handle.**
44f70 0a 2a 2a 20 5e 43 6c 6f 73 65 73 20 61 6e 20 6f .** ^Closes an o
44f80 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 pen [BLOB handle
44f90 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 43 6c 6f 73 69 6e ]..**.** ^Closin
44fa0 67 20 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 g a BLOB shall c
44fb0 61 75 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 ause the current
44fc0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 transaction to
44fd0 63 6f 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 commit.** if the
44fe0 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 re are no other
44ff0 42 4c 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e BLOBs, no pendin
45000 67 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 g prepared state
45010 6d 65 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a ments, and the.*
45020 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
45030 63 74 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 ction is in [aut
45040 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a ocommit mode]..*
45050 2a 20 5e 49 66 20 61 6e 79 20 77 72 69 74 65 73 * ^If any writes
45060 20 77 65 72 65 20 6d 61 64 65 20 74 6f 20 74 68 were made to th
45070 65 20 42 4c 4f 42 2c 20 74 68 65 79 20 6d 69 67 e BLOB, they mig
45080 68 74 20 62 65 20 68 65 6c 64 20 69 6e 20 63 61 ht be held in ca
45090 63 68 65 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 che.** until the
450a0 20 63 6c 6f 73 65 20 6f 70 65 72 61 74 69 6f 6e close operation
450b0 20 69 66 20 74 68 65 79 20 77 69 6c 6c 20 66 69 if they will fi
450c0 74 2e 0a 2a 2a 0a 2a 2a 20 5e 28 43 6c 6f 73 69 t..**.** ^(Closi
450d0 6e 67 20 74 68 65 20 42 4c 4f 42 20 6f 66 74 65 ng the BLOB ofte
450e0 6e 20 66 6f 72 63 65 73 20 74 68 65 20 63 68 61 n forces the cha
450f0 6e 67 65 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 64 nges.** out to d
45100 69 73 6b 20 61 6e 64 20 73 6f 20 69 66 20 61 6e isk and so if an
45110 79 20 49 2f 4f 20 65 72 72 6f 72 73 20 6f 63 63 y I/O errors occ
45120 75 72 2c 20 74 68 65 79 20 77 69 6c 6c 20 6c 69 ur, they will li
45130 6b 65 6c 79 20 6f 63 63 75 72 0a 2a 2a 20 61 74 kely occur.** at
45140 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e 20 74 the time when t
45150 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 he BLOB is close
45160 64 2e 20 20 41 6e 79 20 65 72 72 6f 72 73 20 74 d. Any errors t
45170 68 61 74 20 6f 63 63 75 72 20 64 75 72 69 6e 67 hat occur during
45180 0a 2a 2a 20 63 6c 6f 73 69 6e 67 20 61 72 65 20 .** closing are
45190 72 65 70 6f 72 74 65 64 20 61 73 20 61 20 6e 6f reported as a no
451a0 6e 2d 7a 65 72 6f 20 72 65 74 75 72 6e 20 76 61 n-zero return va
451b0 6c 75 65 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 28 54 lue.)^.**.** ^(T
451c0 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 he BLOB is close
451d0 64 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c d unconditionall
451e0 79 2e 20 20 45 76 65 6e 20 69 66 20 74 68 69 73 y. Even if this
451f0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
45200 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 .** an error cod
45210 65 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 20 73 e, the BLOB is s
45220 74 69 6c 6c 20 63 6c 6f 73 65 64 2e 29 5e 0a 2a till closed.)^.*
45230 2a 0a 2a 2a 20 5e 43 61 6c 6c 69 6e 67 20 74 68 *.** ^Calling th
45240 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 is routine with
45250 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 20 28 a null pointer (
45260 73 75 63 68 20 61 73 20 77 6f 75 6c 64 20 62 65 such as would be
45270 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 returned.** by
45280 61 20 66 61 69 6c 65 64 20 63 61 6c 6c 20 74 6f a failed call to
45290 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f [sqlite3_blob_o
452a0 70 65 6e 28 29 5d 29 20 69 73 20 61 20 68 61 72 pen()]) is a har
452b0 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a mless no-op..*/.
452c0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
452d0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 qlite3_blob_clos
452e0 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a e(sqlite3_blob *
452f0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
45300 45 46 3a 20 52 65 74 75 72 6e 20 54 68 65 20 53 EF: Return The S
45310 69 7a 65 20 4f 66 20 41 6e 20 4f 70 65 6e 20 42 ize Of An Open B
45320 4c 4f 42 0a 2a 2a 0a 2a 2a 20 5e 52 65 74 75 72 LOB.**.** ^Retur
45330 6e 73 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 ns the size in b
45340 79 74 65 73 20 6f 66 20 74 68 65 20 42 4c 4f 42 ytes of the BLOB
45350 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 accessible via
45360 74 68 65 20 0a 2a 2a 20 73 75 63 63 65 73 73 66 the .** successf
45370 75 6c 6c 79 20 6f 70 65 6e 65 64 20 5b 42 4c 4f ully opened [BLO
45380 42 20 68 61 6e 64 6c 65 5d 20 69 6e 20 69 74 73 B handle] in its
45390 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e 74 2e 20 only argument.
453a0 20 5e 54 68 65 0a 2a 2a 20 69 6e 63 72 65 6d 65 ^The.** increme
453b0 6e 74 61 6c 20 62 6c 6f 62 20 49 2f 4f 20 72 6f ntal blob I/O ro
453c0 75 74 69 6e 65 73 20 63 61 6e 20 6f 6e 6c 79 20 utines can only
453d0 72 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 read or overwrit
453e0 69 6e 67 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 ing existing.**
453f0 62 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 20 74 68 blob content; th
45400 65 79 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 ey cannot change
45410 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 62 the size of a b
45420 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 lob..**.** This
45430 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 routine only wor
45440 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20 68 61 ks on a [BLOB ha
45450 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 61 73 20 ndle] which has
45460 62 65 65 6e 20 63 72 65 61 74 65 64 0a 2a 2a 20 been created.**
45470 62 79 20 61 20 70 72 69 6f 72 20 73 75 63 63 65 by a prior succe
45480 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 ssful call to [s
45490 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e qlite3_blob_open
454a0 28 29 5d 20 61 6e 64 20 77 68 69 63 68 20 68 61 ()] and which ha
454b0 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e 20 63 6c s not.** been cl
454c0 6f 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 osed by [sqlite3
454d0 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 20 _blob_close()].
454e0 20 50 61 73 73 69 6e 67 20 61 6e 79 20 6f 74 68 Passing any oth
454f0 65 72 20 70 6f 69 6e 74 65 72 20 69 6e 0a 2a 2a er pointer in.**
45500 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 to this routine
45510 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 results in unde
45520 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 62 fined and probab
45530 6c 79 20 75 6e 64 65 73 69 72 61 62 6c 65 20 62 ly undesirable b
45540 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 ehavior..*/.SQLI
45550 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
45560 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 73 71 e3_blob_bytes(sq
45570 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a lite3_blob *);..
45580 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
45590 52 65 61 64 20 44 61 74 61 20 46 72 6f 6d 20 41 Read Data From A
455a0 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 BLOB Incrementa
455b0 6c 6c 79 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 69 73 lly.**.** ^(This
455c0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
455d0 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 20 66 d to read data f
455e0 72 6f 6d 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f rom an open [BLO
455f0 42 20 68 61 6e 64 6c 65 5d 20 69 6e 74 6f 20 61 B handle] into a
45600 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c .** caller-suppl
45610 69 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 79 ied buffer. N by
45620 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 tes of data are
45630 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 75 66 66 copied into buff
45640 65 72 20 5a 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 er Z.** from the
45650 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 open BLOB, star
45660 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 69 ting at offset i
45670 4f 66 66 73 65 74 2e 29 5e 0a 2a 2a 0a 2a 2a 20 Offset.)^.**.**
45680 5e 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73 ^If offset iOffs
45690 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 et is less than
456a0 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 N bytes from the
456b0 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 end of the BLOB
456c0 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 ,.** [SQLITE_ERR
456d0 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 OR] is returned
456e0 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72 and no data is r
456f0 65 61 64 2e 20 20 5e 49 66 20 4e 20 6f 72 20 69 ead. ^If N or i
45700 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c 65 73 Offset is.** les
45710 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 51 s than zero, [SQ
45720 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 LITE_ERROR] is r
45730 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 eturned and no d
45740 61 74 61 20 69 73 20 72 65 61 64 2e 0a 2a 2a 20 ata is read..**
45750 5e 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 ^The size of the
45760 20 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e 63 65 blob (and hence
45770 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c the maximum val
45780 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74 29 ue of N+iOffset)
45790 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65 72 .** can be deter
457a0 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20 mined using the
457b0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 [sqlite3_blob_by
457c0 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 tes()] interface
457d0 2e 0a 2a 2a 0a 2a 2a 20 5e 41 6e 20 61 74 74 65 ..**.** ^An atte
457e0 6d 70 74 20 74 6f 20 72 65 61 64 20 66 72 6f 6d mpt to read from
457f0 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c 4f an expired [BLO
45800 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20 B handle] fails
45810 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 with an.** error
45820 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 code of [SQLITE
45830 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 5e _ABORT]..**.** ^
45840 28 4f 6e 20 73 75 63 63 65 73 73 2c 20 73 71 6c (On success, sql
45850 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 ite3_blob_read()
45860 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
45870 4f 4b 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 OK..** Otherwise
45880 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 , an [error code
45890 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 ] or an [extende
458a0 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 d error code] is
458b0 20 72 65 74 75 72 6e 65 64 2e 29 5e 0a 2a 2a 0a returned.)^.**.
458c0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
458d0 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 only works on a
458e0 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 [BLOB handle] wh
458f0 69 63 68 20 68 61 73 20 62 65 65 6e 20 63 72 65 ich has been cre
45900 61 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 ated.** by a pri
45910 6f 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 or successful ca
45920 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 ll to [sqlite3_b
45930 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 lob_open()] and
45940 77 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a which has not.**
45950 20 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 been closed by
45960 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c [sqlite3_blob_cl
45970 6f 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 ose()]. Passing
45980 20 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 any other point
45990 65 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 er in.** to this
459a0 20 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 routine results
459b0 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e in undefined an
459c0 64 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 d probably undes
459d0 69 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e irable behavior.
459e0 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
459f0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 [sqlite3_blob_w
45a00 72 69 74 65 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 rite()]..*/.SQLI
45a10 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
45a20 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c e3_blob_read(sql
45a30 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f 69 ite3_blob *, voi
45a40 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e 74 d *Z, int N, int
45a50 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a iOffset);../*.*
45a60 2a 20 43 41 50 49 33 52 45 46 3a 20 57 72 69 74 * CAPI3REF: Writ
45a70 65 20 44 61 74 61 20 49 6e 74 6f 20 41 20 42 4c e Data Into A BL
45a80 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 OB Incrementally
45a90 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 66 75 6e .**.** ^This fun
45aa0 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f ction is used to
45ab0 20 77 72 69 74 65 20 64 61 74 61 20 69 6e 74 6f write data into
45ac0 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 an open [BLOB h
45ad0 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 61 0a 2a 2a andle] from a.**
45ae0 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 caller-supplied
45af0 20 62 75 66 66 65 72 2e 20 5e 4e 20 62 79 74 65 buffer. ^N byte
45b00 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f s of data are co
45b10 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75 pied from the bu
45b20 66 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20 74 ffer Z.** into t
45b30 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 he open BLOB, st
45b40 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 arting at offset
45b50 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 iOffset..**.**
45b60 5e 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 68 61 ^If the [BLOB ha
45b70 6e 64 6c 65 5d 20 70 61 73 73 65 64 20 61 73 20 ndle] passed as
45b80 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
45b90 6e 74 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e 65 nt was not opene
45ba0 64 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 d for.** writing
45bb0 20 28 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 (the flags para
45bc0 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 meter to [sqlite
45bd0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 77 3_blob_open()] w
45be0 61 73 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 68 69 as zero),.** thi
45bf0 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
45c00 6e 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f ns [SQLITE_READO
45c10 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 NLY]..**.** ^Thi
45c20 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f s function may o
45c30 6e 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63 nly modify the c
45c40 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 42 ontents of the B
45c50 4c 4f 42 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f LOB; it is.** no
45c60 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e t possible to in
45c70 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 crease the size
45c80 6f 66 20 61 20 42 4c 4f 42 20 75 73 69 6e 67 20 of a BLOB using
45c90 74 68 69 73 20 41 50 49 2e 0a 2a 2a 20 5e 49 66 this API..** ^If
45ca0 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 20 offset iOffset
45cb0 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 is less than N b
45cc0 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e ytes from the en
45cd0 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a d of the BLOB,.*
45ce0 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d * [SQLITE_ERROR]
45cf0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
45d00 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 no data is writ
45d10 74 65 6e 2e 20 20 5e 49 66 20 4e 20 69 73 0a 2a ten. ^If N is.*
45d20 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f * less than zero
45d30 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
45d40 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
45d50 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74 no data is writt
45d60 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 en..** The size
45d70 6f 66 20 74 68 65 20 42 4c 4f 42 20 28 61 6e 64 of the BLOB (and
45d80 20 68 65 6e 63 65 20 74 68 65 20 6d 61 78 69 6d hence the maxim
45d90 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e 2b 69 4f um value of N+iO
45da0 66 66 73 65 74 29 0a 2a 2a 20 63 61 6e 20 62 65 ffset).** can be
45db0 20 64 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e determined usin
45dc0 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 g the [sqlite3_b
45dd0 6c 6f 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 lob_bytes()] int
45de0 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 5e 41 erface..**.** ^A
45df0 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 n attempt to wri
45e00 74 65 20 74 6f 20 61 6e 20 65 78 70 69 72 65 64 te to an expired
45e10 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 [BLOB handle] f
45e20 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 ails with an.**
45e30 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 error code of [S
45e40 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 20 20 5e QLITE_ABORT]. ^
45e50 57 72 69 74 65 73 20 74 6f 20 74 68 65 20 42 4c Writes to the BL
45e60 4f 42 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 OB that occurred
45e70 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 5b .** before the [
45e80 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 78 70 BLOB handle] exp
45e90 69 72 65 64 20 61 72 65 20 6e 6f 74 20 72 6f 6c ired are not rol
45ea0 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65 0a led back by the.
45eb0 2a 2a 20 65 78 70 69 72 61 74 69 6f 6e 20 6f 66 ** expiration of
45ec0 20 74 68 65 20 68 61 6e 64 6c 65 2c 20 74 68 6f the handle, tho
45ed0 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20 74 68 ugh of course th
45ee0 6f 73 65 20 63 68 61 6e 67 65 73 20 6d 69 67 68 ose changes migh
45ef0 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f t.** have been o
45f00 76 65 72 77 72 69 74 74 65 6e 20 62 79 20 74 68 verwritten by th
45f10 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 e statement that
45f20 20 65 78 70 69 72 65 64 20 74 68 65 20 42 4c 4f expired the BLO
45f30 42 20 68 61 6e 64 6c 65 0a 2a 2a 20 6f 72 20 62 B handle.** or b
45f40 79 20 6f 74 68 65 72 20 69 6e 64 65 70 65 6e 64 y other independ
45f50 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a ent statements..
45f60 2a 2a 0a 2a 2a 20 5e 28 4f 6e 20 73 75 63 63 65 **.** ^(On succe
45f70 73 73 2c 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 ss, sqlite3_blob
45f80 5f 77 72 69 74 65 28 29 20 72 65 74 75 72 6e 73 _write() returns
45f90 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 4f SQLITE_OK..** O
45fa0 74 68 65 72 77 69 73 65 2c 20 61 6e 20 20 5b 65 therwise, an [e
45fb0 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e rror code] or an
45fc0 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 [extended error
45fd0 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e code] is return
45fe0 65 64 2e 29 5e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ed.)^.**.** This
45ff0 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f routine only wo
46000 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20 68 rks on a [BLOB h
46010 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 61 73 andle] which has
46020 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a 2a 2a been created.**
46030 20 62 79 20 61 20 70 72 69 6f 72 20 73 75 63 63 by a prior succ
46040 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b essful call to [
46050 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 sqlite3_blob_ope
46060 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68 20 68 n()] and which h
46070 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e 20 63 as not.** been c
46080 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 losed by [sqlite
46090 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 3_blob_close()].
460a0 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20 6f 74 Passing any ot
460b0 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e 0a 2a her pointer in.*
460c0 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e * to this routin
460d0 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 e results in und
460e0 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 efined and proba
460f0 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c 65 20 bly undesirable
46100 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 behavior..**.**
46110 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 See also: [sqlit
46120 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 2e e3_blob_read()].
46130 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
46140 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f nt sqlite3_blob_
46150 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c write(sqlite3_bl
46160 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 ob *, const void
46170 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 *z, int n, int
46180 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a iOffset);../*.**
46190 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 CAPI3REF: Virtu
461a0 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d 20 4f al File System O
461b0 62 6a 65 63 74 73 0a 2a 2a 0a 2a 2a 20 41 20 76 bjects.**.** A v
461c0 69 72 74 75 61 6c 20 66 69 6c 65 73 79 73 74 65 irtual filesyste
461d0 6d 20 28 56 46 53 29 20 69 73 20 61 6e 20 5b 73 m (VFS) is an [s
461e0 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 qlite3_vfs] obje
461f0 63 74 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 ct.** that SQLit
46200 65 20 75 73 65 73 20 74 6f 20 69 6e 74 65 72 61 e uses to intera
46210 63 74 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 75 ct.** with the u
46220 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 nderlying operat
46230 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 4d 6f 73 ing system. Mos
46240 74 20 53 51 4c 69 74 65 20 62 75 69 6c 64 73 20 t SQLite builds
46250 63 6f 6d 65 20 77 69 74 68 20 61 0a 2a 2a 20 73 come with a.** s
46260 69 6e 67 6c 65 20 64 65 66 61 75 6c 74 20 56 46 ingle default VF
46270 53 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 S that is approp
46280 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 68 6f riate for the ho
46290 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20 st computer..**
462a0 4e 65 77 20 56 46 53 65 73 20 63 61 6e 20 62 65 New VFSes can be
462b0 20 72 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 registered and
462c0 65 78 69 73 74 69 6e 67 20 56 46 53 65 73 20 63 existing VFSes c
462d0 61 6e 20 62 65 20 75 6e 72 65 67 69 73 74 65 72 an be unregister
462e0 65 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ed..** The follo
462f0 77 69 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20 wing interfaces
46300 61 72 65 20 70 72 6f 76 69 64 65 64 2e 0a 2a 2a are provided..**
46310 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 .** ^The sqlite3
46320 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 6e 74 65 _vfs_find() inte
46330 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 rface returns a
46340 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 56 46 53 pointer to a VFS
46350 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e given its name.
46360 0a 2a 2a 20 5e 4e 61 6d 65 73 20 61 72 65 20 63 .** ^Names are c
46370 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a ase sensitive..*
46380 2a 20 5e 4e 61 6d 65 73 20 61 72 65 20 7a 65 72 * ^Names are zer
46390 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 o-terminated UTF
463a0 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 5e -8 strings..** ^
463b0 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d If there is no m
463c0 61 74 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69 atch, a NULL poi
463d0 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 nter is returned
463e0 2e 0a 2a 2a 20 5e 49 66 20 7a 56 66 73 4e 61 6d ..** ^If zVfsNam
463f0 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 e is NULL then t
46400 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 he default VFS i
46410 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
46420 2a 20 5e 4e 65 77 20 56 46 53 65 73 20 61 72 65 * ^New VFSes are
46430 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 registered with
46440 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 sqlite3_vfs_reg
46450 69 73 74 65 72 28 29 2e 0a 2a 2a 20 5e 45 61 63 ister()..** ^Eac
46460 68 20 6e 65 77 20 56 46 53 20 62 65 63 6f 6d 65 h new VFS become
46470 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 46 s the default VF
46480 53 20 69 66 20 74 68 65 20 6d 61 6b 65 44 66 6c S if the makeDfl
46490 74 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a t flag is set..*
464a0 2a 20 5e 54 68 65 20 73 61 6d 65 20 56 46 53 20 * ^The same VFS
464b0 63 61 6e 20 62 65 20 72 65 67 69 73 74 65 72 65 can be registere
464c0 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 d multiple times
464d0 20 77 69 74 68 6f 75 74 20 69 6e 6a 75 72 79 2e without injury.
464e0 0a 2a 2a 20 5e 54 6f 20 6d 61 6b 65 20 61 6e 20 .** ^To make an
464f0 65 78 69 73 74 69 6e 67 20 56 46 53 20 69 6e 74 existing VFS int
46500 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 46 o the default VF
46510 53 2c 20 72 65 67 69 73 74 65 72 20 69 74 20 61 S, register it a
46520 67 61 69 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65 gain.** with the
46530 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 73 makeDflt flag s
46540 65 74 2e 20 20 49 66 20 74 77 6f 20 64 69 66 66 et. If two diff
46550 65 72 65 6e 74 20 56 46 53 65 73 20 77 69 74 68 erent VFSes with
46560 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6e 61 6d the.** same nam
46570 65 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64 e are registered
46580 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 , the behavior i
46590 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49 66 s undefined. If
465a0 20 61 0a 2a 2a 20 56 46 53 20 69 73 20 72 65 67 a.** VFS is reg
465b0 69 73 74 65 72 65 64 20 77 69 74 68 20 61 20 6e istered with a n
465c0 61 6d 65 20 74 68 61 74 20 69 73 20 4e 55 4c 4c ame that is NULL
465d0 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72 or an empty str
465e0 69 6e 67 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ing,.** then the
465f0 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
46600 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 55 efined..**.** ^U
46610 6e 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20 nregister a VFS
46620 77 69 74 68 20 74 68 65 20 73 71 6c 69 74 65 33 with the sqlite3
46630 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28 _vfs_unregister(
46640 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 ) interface..**
46650 5e 28 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 ^(If the default
46660 20 56 46 53 20 69 73 20 75 6e 72 65 67 69 73 74 VFS is unregist
46670 65 72 65 64 2c 20 61 6e 6f 74 68 65 72 20 56 46 ered, another VF
46680 53 20 69 73 20 63 68 6f 73 65 6e 20 61 73 0a 2a S is chosen as.*
46690 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 * the default.
466a0 54 68 65 20 63 68 6f 69 63 65 20 66 6f 72 20 74 The choice for t
466b0 68 65 20 6e 65 77 20 56 46 53 20 69 73 20 61 72 he new VFS is ar
466c0 62 69 74 72 61 72 79 2e 29 5e 0a 2a 2f 0a 53 51 bitrary.)^.*/.SQ
466d0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
466e0 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 _vfs *sqlite3_vf
466f0 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 s_find(const cha
46700 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 51 r *zVfsName);.SQ
46710 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
46720 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 ite3_vfs_registe
46730 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 r(sqlite3_vfs*,
46740 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a 53 int makeDflt);.S
46750 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
46760 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 lite3_vfs_unregi
46770 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 ster(sqlite3_vfs
46780 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
46790 52 45 46 3a 20 4d 75 74 65 78 65 73 0a 2a 2a 0a REF: Mutexes.**.
467a0 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f ** The SQLite co
467b0 72 65 20 75 73 65 73 20 74 68 65 73 65 20 72 6f re uses these ro
467c0 75 74 69 6e 65 73 20 66 6f 72 20 74 68 72 65 61 utines for threa
467d0 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e 69 7a 61 d.** synchroniza
467e0 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20 74 68 65 tion. Though the
467f0 79 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 y are intended f
46800 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 or internal.** u
46810 73 65 20 62 79 20 53 51 4c 69 74 65 2c 20 63 6f se by SQLite, co
46820 64 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 de that links ag
46830 61 69 6e 73 74 20 53 51 4c 69 74 65 20 69 73 0a ainst SQLite is.
46840 2a 2a 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 ** permitted to
46850 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 use any of these
46860 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a routines..**.**
46870 20 54 68 65 20 53 51 4c 69 74 65 20 73 6f 75 72 The SQLite sour
46880 63 65 20 63 6f 64 65 20 63 6f 6e 74 61 69 6e 73 ce code contains
46890 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d multiple implem
468a0 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 entations.** of
468b0 74 68 65 73 65 20 6d 75 74 65 78 20 72 6f 75 74 these mutex rout
468c0 69 6e 65 73 2e 20 20 41 6e 20 61 70 70 72 6f 70 ines. An approp
468d0 72 69 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 riate implementa
468e0 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 6c 65 63 tion.** is selec
468f0 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c ted automaticall
46900 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d y at compile-tim
46910 65 2e 20 20 5e 28 54 68 65 20 66 6f 6c 6c 6f 77 e. ^(The follow
46920 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 ing.** implement
46930 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61 69 6c ations are avail
46940 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51 4c 69 able in the SQLi
46950 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c te core:.**.** <
46960 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 ul>.** <li> SQ
46970 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 LITE_MUTEX_PTHRE
46980 41 44 53 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 ADS.** <li> SQ
46990 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 0a 2a LITE_MUTEX_W32.*
469a0 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f * <li> SQLITE_
469b0 4d 55 54 45 58 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f MUTEX_NOOP.** </
469c0 75 6c 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 ul>)^.**.** ^The
469d0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f SQLITE_MUTEX_NO
469e0 4f 50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f OP implementatio
469f0 6e 20 69 73 20 61 20 73 65 74 20 6f 66 20 72 6f n is a set of ro
46a00 75 74 69 6e 65 73 0a 2a 2a 20 74 68 61 74 20 64 utines.** that d
46a10 6f 65 73 20 6e 6f 20 72 65 61 6c 20 6c 6f 63 6b oes no real lock
46a20 69 6e 67 20 61 6e 64 20 69 73 20 61 70 70 72 6f ing and is appro
46a30 70 72 69 61 74 65 20 66 6f 72 20 75 73 65 20 69 priate for use i
46a40 6e 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 2d 74 68 n.** a single-th
46a50 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 readed applicati
46a60 6f 6e 2e 20 20 5e 54 68 65 20 53 51 4c 49 54 45 on. ^The SQLITE
46a70 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 20 _MUTEX_PTHREADS
46a80 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 and.** SQLITE_MU
46a90 54 45 58 5f 57 33 32 20 69 6d 70 6c 65 6d 65 6e TEX_W32 implemen
46aa0 74 61 74 69 6f 6e 73 20 61 72 65 20 61 70 70 72 tations are appr
46ab0 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 65 20 opriate for use
46ac0 6f 6e 20 55 6e 69 78 0a 2a 2a 20 61 6e 64 20 57 on Unix.** and W
46ad0 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 5e 28 indows..**.** ^(
46ae0 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d If SQLite is com
46af0 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 piled with the S
46b00 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 QLITE_MUTEX_APPD
46b10 45 46 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a EF preprocessor.
46b20 2a 2a 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 ** macro defined
46b30 20 28 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45 (with "-DSQLITE
46b40 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22 _MUTEX_APPDEF=1"
46b50 29 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 ), then no mutex
46b60 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
46b70 6f 6e 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77 on is included w
46b80 69 74 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e ith the library.
46b90 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
46ba0 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e e.** application
46bb0 20 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 63 must supply a c
46bc0 75 73 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c ustom mutex impl
46bd0 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 ementation using
46be0 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f the.** [SQLITE_
46bf0 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 CONFIG_MUTEX] op
46c00 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
46c10 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e te3_config() fun
46c20 63 74 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 ction.** before
46c30 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f calling sqlite3_
46c40 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 initialize() or
46c50 61 6e 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 any other public
46c60 20 73 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e sqlite3_.** fun
46c70 63 74 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 ction that calls
46c80 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
46c90 69 7a 65 28 29 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e ize().)^.**.** ^
46ca0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
46cb0 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e x_alloc() routin
46cc0 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 e allocates a ne
46cd0 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 w.** mutex and r
46ce0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
46cf0 20 74 6f 20 69 74 2e 20 5e 49 66 20 69 74 20 72 to it. ^If it r
46d00 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 eturns NULL.** t
46d10 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 hat means that a
46d20 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 mutex could not
46d30 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 be allocated.
46d40 5e 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 ^SQLite.** will
46d50 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b unwind its stack
46d60 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 and return an e
46d70 72 72 6f 72 2e 20 20 5e 28 54 68 65 20 61 72 67 rror. ^(The arg
46d80 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 ument.** to sqli
46d90 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
46da0 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 ) is one of thes
46db0 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 e integer consta
46dc0 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a nts:.**.** <ul>.
46dd0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
46de0 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c MUTEX_FAST.** <l
46df0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
46e00 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 3c 6c _RECURSIVE.** <l
46e10 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
46e20 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a _STATIC_MASTER.*
46e30 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
46e40 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0a UTEX_STATIC_MEM.
46e50 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
46e60 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d MUTEX_STATIC_MEM
46e70 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 2.** <li> SQLIT
46e80 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 E_MUTEX_STATIC_P
46e90 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c RNG.** <li> SQL
46ea0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
46eb0 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 _LRU.** <li> SQ
46ec0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
46ed0 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c 3e 29 C_LRU2.** </ul>)
46ee0 5e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 72 ^.**.** ^The fir
46ef0 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 st two constants
46f00 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 (SQLITE_MUTEX_F
46f10 41 53 54 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d AST and SQLITE_M
46f20 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 0a UTEX_RECURSIVE).
46f30 2a 2a 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 ** cause sqlite3
46f40 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 _mutex_alloc() t
46f50 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 o create.** a ne
46f60 77 20 6d 75 74 65 78 2e 20 20 5e 54 68 65 20 6e w mutex. ^The n
46f70 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 ew mutex is recu
46f80 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 rsive when SQLIT
46f90 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
46fa0 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 E.** is used but
46fb0 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 not necessarily
46fc0 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f so when SQLITE_
46fd0 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 MUTEX_FAST is us
46fe0 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 ed..** The mutex
46ff0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
47000 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
47010 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 make a distinct
47020 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 ion.** between S
47030 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
47040 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 RSIVE and SQLITE
47050 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 _MUTEX_FAST if i
47060 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 t does.** not wa
47070 6e 74 20 74 6f 2e 20 20 5e 53 51 4c 69 74 65 20 nt to. ^SQLite
47080 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 will only reques
47090 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 t a recursive mu
470a0 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 tex in.** cases
470b0 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 where it really
470c0 6e 65 65 64 73 20 6f 6e 65 2e 20 20 5e 49 66 20 needs one. ^If
470d0 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 a faster non-rec
470e0 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 ursive mutex.**
470f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 implementation i
47100 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 s available on t
47110 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d he host platform
47120 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 , the mutex subs
47130 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 ystem.** might r
47140 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 eturn such a mut
47150 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 ex in response t
47160 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 o SQLITE_MUTEX_F
47170 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 AST..**.** ^The
47180 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 other allowed pa
47190 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 rameters to sqli
471a0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
471b0 29 20 28 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 ) (anything othe
471c0 72 0a 2a 2a 20 74 68 61 6e 20 53 51 4c 49 54 45 r.** than SQLITE
471d0 5f 4d 55 54 45 58 5f 46 41 53 54 20 61 6e 64 20 _MUTEX_FAST and
471e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
471f0 55 52 53 49 56 45 29 20 65 61 63 68 20 72 65 74 URSIVE) each ret
47200 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 urn.** a pointer
47210 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 to a static pre
47220 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 existing mutex.
47230 20 5e 53 69 78 20 73 74 61 74 69 63 20 6d 75 74 ^Six static mut
47240 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 exes are.** used
47250 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 by the current
47260 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 version of SQLit
47270 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 e. Future versi
47280 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a ons of SQLite.**
47290 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f may add additio
472a0 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 nal static mutex
472b0 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 es. Static mute
472c0 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 xes are for inte
472d0 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 rnal.** use by S
472e0 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 QLite only. App
472f0 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 lications that u
47300 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 se SQLite mutexe
47310 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 s should.** use
47320 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 only the dynamic
47330 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 mutexes returne
47340 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 d by SQLITE_MUTE
47350 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c X_FAST or.** SQL
47360 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
47370 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 5e 4e 6f 74 65 IVE..**.** ^Note
47380 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 that if one of
47390 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 the dynamic mute
473a0 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 x parameters (SQ
473b0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a LITE_MUTEX_FAST.
473c0 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 ** or SQLITE_MUT
473d0 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 EX_RECURSIVE) is
473e0 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 used then sqlit
473f0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
47400 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 .** returns a di
47410 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e fferent mutex on
47420 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 5e 42 every call. ^B
47430 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69 ut for the stati
47440 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73 c.** mutex types
47450 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78 , the same mutex
47460 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
47470 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20 every call that
47480 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 has.** the same
47490 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a type number..**.
474a0 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f ** ^The sqlite3_
474b0 6d 75 74 65 78 5f 66 72 65 65 28 29 20 72 6f 75 mutex_free() rou
474c0 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 tine deallocates
474d0 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a a previously.**
474e0 20 61 6c 6c 6f 63 61 74 65 64 20 64 79 6e 61 6d allocated dynam
474f0 69 63 20 6d 75 74 65 78 2e 20 20 5e 53 51 4c 69 ic mutex. ^SQLi
47500 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f te is careful to
47510 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 deallocate ever
47520 79 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 75 74 y.** dynamic mut
47530 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 ex that it alloc
47540 61 74 65 73 2e 20 20 54 68 65 20 64 79 6e 61 6d ates. The dynam
47550 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73 74 20 ic mutexes must
47560 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75 73 65 not be in.** use
47570 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 64 when they are d
47580 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 41 74 74 eallocated. Att
47590 65 6d 70 74 69 6e 67 20 74 6f 20 64 65 61 6c 6c empting to deall
475a0 6f 63 61 74 65 20 61 20 73 74 61 74 69 63 0a 2a ocate a static.*
475b0 2a 20 6d 75 74 65 78 20 72 65 73 75 6c 74 73 20 * mutex results
475c0 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68 in undefined beh
475d0 61 76 69 6f 72 2e 20 20 5e 53 51 4c 69 74 65 20 avior. ^SQLite
475e0 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74 65 never deallocate
475f0 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d 75 s.** a static mu
47600 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 tex..**.** ^The
47610 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
47620 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 ter() and sqlite
47630 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 3_mutex_try() ro
47640 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a utines attempt.*
47650 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 * to enter a mut
47660 65 78 2e 20 20 5e 49 66 20 61 6e 6f 74 68 65 72 ex. ^If another
47670 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 thread is alrea
47680 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 dy within the mu
47690 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f tex,.** sqlite3_
476a0 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 mutex_enter() wi
476b0 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c ll block and sql
476c0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
476d0 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 will return.**
476e0 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 5e 54 SQLITE_BUSY. ^T
476f0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
47700 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 _try() interface
47710 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
47720 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 _OK].** upon suc
47730 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 cessful entry.
47740 5e 28 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 ^(Mutexes create
47750 64 20 75 73 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 d using.** SQLIT
47760 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
47770 45 20 63 61 6e 20 62 65 20 65 6e 74 65 72 65 64 E can be entered
47780 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 multiple times
47790 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
477a0 61 64 2e 0a 2a 2a 20 49 6e 20 73 75 63 68 20 63 ad..** In such c
477b0 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 ases the,.** mut
477c0 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 ex must be exite
477d0 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 d an equal numbe
477e0 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 r of times befor
477f0 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 e another thread
47800 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 29 5e .** can enter.)^
47810 20 20 5e 28 49 66 20 74 68 65 20 73 61 6d 65 20 ^(If the same
47820 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 thread tries to
47830 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 0a enter any other.
47840 2a 2a 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 ** kind of mutex
47850 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c more than once,
47860 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 the behavior is
47870 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 53 undefined..** S
47880 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 QLite will never
47890 20 65 78 68 69 62 69 74 0a 2a 2a 20 73 75 63 68 exhibit.** such
478a0 20 62 65 68 61 76 69 6f 72 20 69 6e 20 69 74 73 behavior in its
478b0 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75 74 65 own use of mute
478c0 78 65 73 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 28 53 xes.)^.**.** ^(S
478d0 6f 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 ome systems (for
478e0 20 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 example, Window
478f0 73 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 s 95) do not sup
47900 70 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 port the operati
47910 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 on.** implemente
47920 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 d by sqlite3_mut
47930 65 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 ex_try(). On th
47940 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c ose systems, sql
47950 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
47960 0a 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 .** will always
47970 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 return SQLITE_BU
47980 53 59 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 SY. The SQLite
47990 63 6f 72 65 20 6f 6e 6c 79 20 65 76 65 72 20 75 core only ever u
479a0 73 65 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d ses.** sqlite3_m
479b0 75 74 65 78 5f 74 72 79 28 29 20 61 73 20 61 6e utex_try() as an
479c0 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 73 6f optimization so
479d0 20 74 68 69 73 20 69 73 20 61 63 63 65 70 74 61 this is accepta
479e0 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 29 5e 0a ble behavior.)^.
479f0 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 **.** ^The sqlit
47a00 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
47a10 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 routine exits a
47a20 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a mutex that was.
47a30 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e ** previously en
47a40 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d tered by the sam
47a50 65 20 74 68 72 65 61 64 2e 20 20 20 5e 28 54 68 e thread. ^(Th
47a60 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 e behavior.** is
47a70 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 undefined if th
47a80 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 e mutex is not c
47a90 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 urrently entered
47aa0 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 by the.** calli
47ab0 6e 67 20 74 68 72 65 61 64 20 6f 72 20 69 73 20 ng thread or is
47ac0 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c not currently al
47ad0 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 located. SQLite
47ae0 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 will.** never d
47af0 6f 20 65 69 74 68 65 72 2e 29 5e 0a 2a 2a 0a 2a o either.)^.**.*
47b00 2a 20 5e 49 66 20 74 68 65 20 61 72 67 75 6d 65 * ^If the argume
47b10 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 nt to sqlite3_mu
47b20 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c tex_enter(), sql
47b30 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
47b40 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f , or.** sqlite3_
47b50 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73 mutex_leave() is
47b60 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
47b70 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20 then all three
47b80 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61 routines.** beha
47b90 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a ve as no-ops..**
47ba0 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
47bb0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
47bc0 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 d()] and [sqlite
47bd0 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
47be0 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 )]..*/.SQLITE_AP
47bf0 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 I sqlite3_mutex
47c00 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 *sqlite3_mutex_a
47c10 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 lloc(int);.SQLIT
47c20 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
47c30 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 e3_mutex_free(sq
47c40 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 lite3_mutex*);.S
47c50 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
47c60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
47c70 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 er(sqlite3_mutex
47c80 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
47c90 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nt sqlite3_mutex
47ca0 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 _try(sqlite3_mut
47cb0 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ex*);.SQLITE_API
47cc0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 void sqlite3_mu
47cd0 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 tex_leave(sqlite
47ce0 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 3_mutex*);../*.*
47cf0 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 * CAPI3REF: Mute
47d00 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 x Methods Object
47d10 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e .**.** An instan
47d20 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ce of this struc
47d30 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 ture defines the
47d40 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 low-level routi
47d50 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 61 nes.** used to a
47d60 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73 65 20 llocate and use
47d70 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 55 mutexes..**.** U
47d80 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65 66 61 sually, the defa
47d90 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d ult mutex implem
47da0 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76 69 64 entations provid
47db0 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 72 65 ed by SQLite are
47dc0 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 2c 20 .** sufficient,
47dd0 68 6f 77 65 76 65 72 20 74 68 65 20 75 73 65 72 however the user
47de0 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f 6e 20 has the option
47df0 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 of substituting
47e00 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d 70 6c a custom.** impl
47e10 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 73 ementation for s
47e20 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70 6c 6f pecialized deplo
47e30 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74 65 6d yments or system
47e40 73 20 66 6f 72 20 77 68 69 63 68 20 53 51 4c 69 s for which SQLi
47e50 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 70 te.** does not p
47e60 72 6f 76 69 64 65 20 61 20 73 75 69 74 61 62 6c rovide a suitabl
47e70 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
47e80 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 . In this case,
47e90 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72 65 61 the user.** crea
47ea0 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 tes and populate
47eb0 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
47ec0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
47ed0 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20 73 71 to pass.** to sq
47ee0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 61 lite3_config() a
47ef0 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 5b 53 long with the [S
47f00 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 QLITE_CONFIG_MUT
47f10 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 41 EX] option..** A
47f20 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 6e 20 dditionally, an
47f30 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
47f40 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 structure can b
47f50 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a 2a 20 e used as an.**
47f60 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 output variable
47f70 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20 74 68 when querying th
47f80 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74 68 65 e system for the
47f90 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 0a 2a current mutex.*
47fa0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
47fb0 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53 51 4c , using the [SQL
47fc0 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 ITE_CONFIG_GETMU
47fd0 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 0a TEX] option..**.
47fe0 2a 2a 20 5e 54 68 65 20 78 4d 75 74 65 78 49 6e ** ^The xMutexIn
47ff0 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 it method define
48000 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74 d by this struct
48010 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 ure is invoked a
48020 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 73 s.** part of sys
48030 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 tem initializati
48040 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 on by the sqlite
48050 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 3_initialize() f
48060 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 5e 54 68 65 unction..** ^The
48070 20 78 4d 75 74 65 78 49 6e 69 74 20 72 6f 75 74 xMutexInit rout
48080 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 ine is called by
48090 20 53 51 4c 69 74 65 20 65 78 61 63 74 6c 79 20 SQLite exactly
480a0 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 0a 2a 2a once for each.**
480b0 20 65 66 66 65 63 74 69 76 65 20 63 61 6c 6c 20 effective call
480c0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 to [sqlite3_init
480d0 69 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a ialize()]..**.**
480e0 20 5e 54 68 65 20 78 4d 75 74 65 78 45 6e 64 20 ^The xMutexEnd
480f0 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64 20 62 method defined b
48100 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 y this structure
48110 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 0a 2a is invoked as.*
48120 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74 65 6d * part of system
48130 20 73 68 75 74 64 6f 77 6e 20 62 79 20 74 68 65 shutdown by the
48140 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
48150 6e 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 n() function. Th
48160 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 e.** implementat
48170 69 6f 6e 20 6f 66 20 74 68 69 73 20 6d 65 74 68 ion of this meth
48180 6f 64 20 69 73 20 65 78 70 65 63 74 65 64 20 74 od is expected t
48190 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6f 75 o release all ou
481a0 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 72 65 73 tstanding.** res
481b0 6f 75 72 63 65 73 20 6f 62 74 61 69 6e 65 64 20 ources obtained
481c0 62 79 20 74 68 65 20 6d 75 74 65 78 20 6d 65 74 by the mutex met
481d0 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 hods implementat
481e0 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c 79 0a ion, especially.
481f0 2a 2a 20 74 68 6f 73 65 20 6f 62 74 61 69 6e 65 ** those obtaine
48200 64 20 62 79 20 74 68 65 20 78 4d 75 74 65 78 49 d by the xMutexI
48210 6e 69 74 20 6d 65 74 68 6f 64 2e 20 20 5e 54 68 nit method. ^Th
48220 65 20 78 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a e xMutexEnd().**
48230 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 6e interface is in
48240 76 6f 6b 65 64 20 65 78 61 63 74 6c 79 20 6f 6e voked exactly on
48250 63 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c 6c ce for each call
48260 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 to [sqlite3_shu
48270 74 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 tdown()]..**.**
48280 5e 28 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 ^(The remaining
48290 73 65 76 65 6e 20 6d 65 74 68 6f 64 73 20 64 65 seven methods de
482a0 66 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 fined by this st
482b0 72 75 63 74 75 72 65 20 28 78 4d 75 74 65 78 41 ructure (xMutexA
482c0 6c 6c 6f 63 2c 0a 2a 2a 20 78 4d 75 74 65 78 46 lloc,.** xMutexF
482d0 72 65 65 2c 20 78 4d 75 74 65 78 45 6e 74 65 72 ree, xMutexEnter
482e0 2c 20 78 4d 75 74 65 78 54 72 79 2c 20 78 4d 75 , xMutexTry, xMu
482f0 74 65 78 4c 65 61 76 65 2c 20 78 4d 75 74 65 78 texLeave, xMutex
48300 48 65 6c 64 20 61 6e 64 0a 2a 2a 20 78 4d 75 74 Held and.** xMut
48310 65 78 4e 6f 74 68 65 6c 64 29 20 69 6d 70 6c 65 exNotheld) imple
48320 6d 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ment the followi
48330 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20 28 72 ng interfaces (r
48340 65 73 70 65 63 74 69 76 65 6c 79 29 3a 0a 2a 2a espectively):.**
48350 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c .** <ul>.** <l
48360 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 i> [sqlite3_mut
48370 65 78 5f 61 6c 6c 6f 63 28 29 5d 20 3c 2f 6c 69 ex_alloc()] </li
48380 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 >.** <li> [sq
48390 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 lite3_mutex_free
483a0 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c ()] </li>.** <
483b0 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 li> [sqlite3_mu
483c0 74 65 78 5f 65 6e 74 65 72 28 29 5d 20 3c 2f 6c tex_enter()] </l
483d0 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 i>.** <li> [s
483e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
483f0 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c ()] </li>.** <
48400 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 li> [sqlite3_mu
48410 74 65 78 5f 6c 65 61 76 65 28 29 5d 20 3c 2f 6c tex_leave()] </l
48420 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 i>.** <li> [s
48430 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
48440 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 d()] </li>.**
48450 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d <li> [sqlite3_m
48460 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 20 utex_notheld()]
48470 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 29 5e </li>.** </ul>)^
48480 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 .**.** The only
48490 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 74 68 difference is th
484a0 61 74 20 74 68 65 20 70 75 62 6c 69 63 20 73 71 at the public sq
484b0 6c 69 74 65 33 5f 58 58 58 20 66 75 6e 63 74 69 lite3_XXX functi
484c0 6f 6e 73 20 65 6e 75 6d 65 72 61 74 65 64 0a 2a ons enumerated.*
484d0 2a 20 61 62 6f 76 65 20 73 69 6c 65 6e 74 6c 79 * above silently
484e0 20 69 67 6e 6f 72 65 20 61 6e 79 20 69 6e 76 6f ignore any invo
484f0 63 61 74 69 6f 6e 73 20 74 68 61 74 20 70 61 73 cations that pas
48500 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 s a NULL pointer
48510 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 61 instead.** of a
48520 20 76 61 6c 69 64 20 6d 75 74 65 78 20 68 61 6e valid mutex han
48530 64 6c 65 2e 20 54 68 65 20 69 6d 70 6c 65 6d 65 dle. The impleme
48540 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 ntations of the
48550 6d 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 0a methods defined.
48560 2a 2a 20 62 79 20 74 68 69 73 20 73 74 72 75 63 ** by this struc
48570 74 75 72 65 20 61 72 65 20 6e 6f 74 20 72 65 71 ture are not req
48580 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 uired to handle
48590 74 68 69 73 20 63 61 73 65 2c 20 74 68 65 20 72 this case, the r
485a0 65 73 75 6c 74 73 0a 2a 2a 20 6f 66 20 70 61 73 esults.** of pas
485b0 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e sing a NULL poin
485c0 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 ter instead of a
485d0 20 76 61 6c 69 64 20 6d 75 74 65 78 20 68 61 6e valid mutex han
485e0 64 6c 65 20 61 72 65 20 75 6e 64 65 66 69 6e 65 dle are undefine
485f0 64 0a 2a 2a 20 28 69 2e 65 2e 20 69 74 20 69 73 d.** (i.e. it is
48600 20 61 63 63 65 70 74 61 62 6c 65 20 74 6f 20 70 acceptable to p
48610 72 6f 76 69 64 65 20 61 6e 20 69 6d 70 6c 65 6d rovide an implem
48620 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 73 65 entation that se
48630 67 66 61 75 6c 74 73 20 69 66 0a 2a 2a 20 69 74 gfaults if.** it
48640 20 69 73 20 70 61 73 73 65 64 20 61 20 4e 55 4c is passed a NUL
48650 4c 20 70 6f 69 6e 74 65 72 29 2e 0a 2a 2a 0a 2a L pointer)..**.*
48660 2a 20 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74 * The xMutexInit
48670 28 29 20 6d 65 74 68 6f 64 20 6d 75 73 74 20 62 () method must b
48680 65 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 5e e threadsafe. ^
48690 49 74 20 6d 75 73 74 20 62 65 20 68 61 72 6d 6c It must be harml
486a0 65 73 73 20 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 ess to.** invoke
486b0 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 75 xMutexInit() mu
486c0 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 ltiple times wit
486d0 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f hin the same pro
486e0 63 65 73 73 20 61 6e 64 20 77 69 74 68 6f 75 74 cess and without
486f0 0a 2a 2a 20 69 6e 74 65 72 76 65 6e 69 6e 67 20 .** intervening
48700 63 61 6c 6c 73 20 74 6f 20 78 4d 75 74 65 78 45 calls to xMutexE
48710 6e 64 28 29 2e 20 20 53 65 63 6f 6e 64 20 61 6e nd(). Second an
48720 64 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c d subsequent cal
48730 6c 73 20 74 6f 0a 2a 2a 20 78 4d 75 74 65 78 49 ls to.** xMutexI
48740 6e 69 74 28 29 20 6d 75 73 74 20 62 65 20 6e 6f nit() must be no
48750 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 5e 78 4d 75 -ops..**.** ^xMu
48760 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74 20 6e texInit() must n
48770 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 65 ot use SQLite me
48780 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
48790 28 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 ([sqlite3_malloc
487a0 28 29 5d 0a 2a 2a 20 61 6e 64 20 69 74 73 20 61 ()].** and its a
487b0 73 73 6f 63 69 61 74 65 73 29 2e 20 20 5e 53 69 ssociates). ^Si
487c0 6d 69 6c 61 72 6c 79 2c 20 78 4d 75 74 65 78 41 milarly, xMutexA
487d0 6c 6c 6f 63 28 29 20 6d 75 73 74 20 6e 6f 74 20 lloc() must not
487e0 75 73 65 20 53 51 4c 69 74 65 20 6d 65 6d 6f 72 use SQLite memor
487f0 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 y.** allocation
48800 66 6f 72 20 61 20 73 74 61 74 69 63 20 6d 75 74 for a static mut
48810 65 78 2e 20 20 5e 48 6f 77 65 76 65 72 20 78 4d ex. ^However xM
48820 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d 61 79 20 utexAlloc() may
48830 75 73 65 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 65 use SQLite.** me
48840 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
48850 66 6f 72 20 61 20 66 61 73 74 20 6f 72 20 72 65 for a fast or re
48860 63 75 72 73 69 76 65 20 6d 75 74 65 78 2e 0a 2a cursive mutex..*
48870 2a 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 77 69 6c *.** ^SQLite wil
48880 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 4d 75 l invoke the xMu
48890 74 65 78 45 6e 64 28 29 20 6d 65 74 68 6f 64 20 texEnd() method
488a0 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 when [sqlite3_sh
488b0 75 74 64 6f 77 6e 28 29 5d 20 69 73 0a 2a 2a 20 utdown()] is.**
488c0 63 61 6c 6c 65 64 2c 20 62 75 74 20 6f 6e 6c 79 called, but only
488d0 20 69 66 20 74 68 65 20 70 72 69 6f 72 20 63 61 if the prior ca
488e0 6c 6c 20 74 6f 20 78 4d 75 74 65 78 49 6e 69 74 ll to xMutexInit
488f0 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 54 45 returned SQLITE
48900 5f 4f 4b 2e 0a 2a 2a 20 49 66 20 78 4d 75 74 65 _OK..** If xMute
48910 78 49 6e 69 74 20 66 61 69 6c 73 20 69 6e 20 61 xInit fails in a
48920 6e 79 20 77 61 79 2c 20 69 74 20 69 73 20 65 78 ny way, it is ex
48930 70 65 63 74 65 64 20 74 6f 20 63 6c 65 61 6e 20 pected to clean
48940 75 70 20 61 66 74 65 72 20 69 74 73 65 6c 66 0a up after itself.
48950 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74 75 ** prior to retu
48960 72 6e 69 6e 67 2e 0a 2a 2f 0a 74 79 70 65 64 65 rning..*/.typede
48970 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 f struct sqlite3
48980 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 _mutex_methods s
48990 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
489a0 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c hods;.struct sql
489b0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
489c0 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 ds {. int (*xMu
489d0 74 65 78 49 6e 69 74 29 28 76 6f 69 64 29 3b 0a texInit)(void);.
489e0 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 45 6e int (*xMutexEn
489f0 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71 6c 69 d)(void);. sqli
48a00 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 78 4d 75 te3_mutex *(*xMu
48a10 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a texAlloc)(int);.
48a20 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 46 void (*xMutexF
48a30 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 ree)(sqlite3_mut
48a40 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a ex *);. void (*
48a50 78 4d 75 74 65 78 45 6e 74 65 72 29 28 73 71 6c xMutexEnter)(sql
48a60 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 ite3_mutex *);.
48a70 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54 72 79 int (*xMutexTry
48a80 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 )(sqlite3_mutex
48a90 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 *);. void (*xMu
48aa0 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69 74 65 texLeave)(sqlite
48ab0 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 3_mutex *);. in
48ac0 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64 29 28 t (*xMutexHeld)(
48ad0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 sqlite3_mutex *)
48ae0 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 ;. int (*xMutex
48af0 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 74 65 33 Notheld)(sqlite3
48b00 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f _mutex *);.};../
48b10 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d *.** CAPI3REF: M
48b20 75 74 65 78 20 56 65 72 69 66 69 63 61 74 69 6f utex Verificatio
48b30 6e 20 52 6f 75 74 69 6e 65 73 0a 2a 2a 0a 2a 2a n Routines.**.**
48b40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
48b50 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 ex_held() and sq
48b60 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
48b70 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 73 0a 2a eld() routines.*
48b80 2a 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 * are intended f
48b90 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 61 73 or use inside as
48ba0 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
48bb0 73 2e 20 20 5e 54 68 65 20 53 51 4c 69 74 65 20 s. ^The SQLite
48bc0 63 6f 72 65 0a 2a 2a 20 6e 65 76 65 72 20 75 73 core.** never us
48bd0 65 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 es these routine
48be0 73 20 65 78 63 65 70 74 20 69 6e 73 69 64 65 20 s except inside
48bf0 61 6e 20 61 73 73 65 72 74 28 29 20 61 6e 64 20 an assert() and
48c00 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 applications.**
48c10 61 72 65 20 61 64 76 69 73 65 64 20 74 6f 20 66 are advised to f
48c20 6f 6c 6c 6f 77 20 74 68 65 20 6c 65 61 64 20 6f ollow the lead o
48c30 66 20 74 68 65 20 63 6f 72 65 2e 20 20 5e 54 68 f the core. ^Th
48c40 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e e SQLite core on
48c50 6c 79 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20 69 ly.** provides i
48c60 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 mplementations f
48c70 6f 72 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 or these routine
48c80 73 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d s when it is com
48c90 70 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 piled.** with th
48ca0 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 66 e SQLITE_DEBUG f
48cb0 6c 61 67 2e 20 20 5e 45 78 74 65 72 6e 61 6c 20 lag. ^External
48cc0 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 mutex implementa
48cd0 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c tions.** are onl
48ce0 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 70 72 y required to pr
48cf0 6f 76 69 64 65 20 74 68 65 73 65 20 72 6f 75 74 ovide these rout
48d00 69 6e 65 73 20 69 66 20 53 51 4c 49 54 45 5f 44 ines if SQLITE_D
48d10 45 42 55 47 20 69 73 0a 2a 2a 20 64 65 66 69 6e EBUG is.** defin
48d20 65 64 20 61 6e 64 20 69 66 20 4e 44 45 42 55 47 ed and if NDEBUG
48d30 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e is not defined.
48d40 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 73 65 20 72 6f .**.** ^These ro
48d50 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 72 65 utines should re
48d60 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
48d70 20 6d 75 74 65 78 20 69 6e 20 74 68 65 69 72 20 mutex in their
48d80 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 68 argument.** is h
48d90 65 6c 64 20 6f 72 20 6e 6f 74 20 68 65 6c 64 2c eld or not held,
48da0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2c 20 62 respectively, b
48db0 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 y the calling th
48dc0 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 read..**.** ^The
48dd0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
48de0 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 is not required
48df0 74 6f 20 70 72 6f 76 69 64 65 20 76 65 72 73 69 to provide versi
48e00 6f 6e 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 ons of these.**
48e10 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 63 routines that ac
48e20 74 75 61 6c 6c 79 20 77 6f 72 6b 2e 20 49 66 20 tually work. If
48e30 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
48e40 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 on does not prov
48e50 69 64 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 ide working.** v
48e60 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 ersions of these
48e70 20 72 6f 75 74 69 6e 65 73 2c 20 69 74 20 73 68 routines, it sh
48e80 6f 75 6c 64 20 61 74 20 6c 65 61 73 74 20 70 72 ould at least pr
48e90 6f 76 69 64 65 20 73 74 75 62 73 20 74 68 61 74 ovide stubs that
48ea0 20 61 6c 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 always.** retur
48eb0 6e 20 74 72 75 65 20 73 6f 20 74 68 61 74 20 6f n true so that o
48ec0 6e 65 20 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 ne does not get
48ed0 73 70 75 72 69 6f 75 73 20 61 73 73 65 72 74 69 spurious asserti
48ee0 6f 6e 20 66 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a on failures..**.
48ef0 2a 2a 20 5e 49 66 20 74 68 65 20 61 72 67 75 6d ** ^If the argum
48f00 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d ent to sqlite3_m
48f10 75 74 65 78 5f 68 65 6c 64 28 29 20 69 73 20 61 utex_held() is a
48f20 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 NULL pointer th
48f30 65 6e 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 6e en.** the routin
48f40 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 e should return
48f50 31 2e 20 20 20 54 68 69 73 20 73 65 65 6d 73 20 1. This seems
48f60 63 6f 75 6e 74 65 72 2d 69 6e 74 75 69 74 69 76 counter-intuitiv
48f70 65 20 73 69 6e 63 65 0a 2a 2a 20 63 6c 65 61 72 e since.** clear
48f80 6c 79 20 74 68 65 20 6d 75 74 65 78 20 63 61 6e ly the mutex can
48f90 6e 6f 74 20 62 65 20 68 65 6c 64 20 69 66 20 69 not be held if i
48fa0 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 t does not exist
48fb0 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 20 72 65 . But.** the re
48fc0 61 73 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64 ason the mutex d
48fd0 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73 oes not exist is
48fe0 20 62 65 63 61 75 73 65 20 74 68 65 20 62 75 69 because the bui
48ff0 6c 64 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69 ld is not.** usi
49000 6e 67 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 ng mutexes. And
49010 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 we do not want
49020 74 68 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e the assert() con
49030 74 61 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63 taining the.** c
49040 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d all to sqlite3_m
49050 75 74 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 utex_held() to f
49060 61 69 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 ail, so a non-ze
49070 72 6f 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20 ro return is.**
49080 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
49090 74 68 69 6e 67 20 74 6f 20 64 6f 2e 20 20 5e 54 thing to do. ^T
490a0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
490b0 5f 6e 6f 74 68 65 6c 64 28 29 0a 2a 2a 20 69 6e _notheld().** in
490c0 74 65 72 66 61 63 65 20 73 68 6f 75 6c 64 20 61 terface should a
490d0 6c 73 6f 20 72 65 74 75 72 6e 20 31 20 77 68 65 lso return 1 whe
490e0 6e 20 67 69 76 65 6e 20 61 20 4e 55 4c 4c 20 70 n given a NULL p
490f0 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 23 69 66 6e 64 ointer..*/.#ifnd
49100 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 ef NDEBUG.SQLITE
49110 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
49120 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 _mutex_held(sqli
49130 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c te3_mutex*);.SQL
49140 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
49150 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
49160 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a d(sqlite3_mutex*
49170 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a );.#endif../*.**
49180 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 CAPI3REF: Mutex
49190 20 54 79 70 65 73 0a 2a 2a 0a 2a 2a 20 54 68 65 Types.**.** The
491a0 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f [sqlite3_mutex_
491b0 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65 72 66 61 alloc()] interfa
491c0 63 65 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c ce takes a singl
491d0 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 e argument.** wh
491e0 69 63 68 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 ich is one of th
491f0 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 ese integer cons
49200 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tants..**.** The
49210 20 73 65 74 20 6f 66 20 73 74 61 74 69 63 20 6d set of static m
49220 75 74 65 78 65 73 20 6d 61 79 20 63 68 61 6e 67 utexes may chang
49230 65 20 66 72 6f 6d 20 6f 6e 65 20 53 51 4c 69 74 e from one SQLit
49240 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74 68 65 e release to the
49250 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 70 70 6c 69 .** next. Appli
49260 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 cations that ove
49270 72 72 69 64 65 20 74 68 65 20 62 75 69 6c 74 2d rride the built-
49280 69 6e 20 6d 75 74 65 78 20 6c 6f 67 69 63 20 6d in mutex logic m
49290 75 73 74 20 62 65 0a 2a 2a 20 70 72 65 70 61 72 ust be.** prepar
492a0 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f 64 61 74 ed to accommodat
492b0 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 e additional sta
492c0 74 69 63 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a tic mutexes..*/.
492d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d #define SQLITE_M
492e0 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 UTEX_FAST
492f0 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 0.#define
49300 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
49310 55 52 53 49 56 45 20 20 20 20 20 20 20 20 31 0a URSIVE 1.
49320 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d #define SQLITE_M
49330 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
49340 45 52 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 ER 2.#define
49350 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
49360 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 33 20 TIC_MEM 3
49370 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c /* sqlite3_mall
49380 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 oc() */.#define
49390 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
493a0 54 49 43 5f 4d 45 4d 32 20 20 20 20 20 20 34 20 TIC_MEM2 4
493b0 20 2f 2a 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a /* NOT USED */.
493c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d #define SQLITE_M
493d0 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e UTEX_STATIC_OPEN
493e0 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 4 /* sqli
493f0 74 65 33 42 74 72 65 65 4f 70 65 6e 28 29 20 2a te3BtreeOpen() *
49400 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
49410 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 _MUTEX_STATIC_PR
49420 4e 47 20 20 20 20 20 20 35 20 20 2f 2a 20 73 71 NG 5 /* sq
49430 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28 29 20 2a lite3_random() *
49440 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
49450 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 _MUTEX_STATIC_LR
49460 55 20 20 20 20 20 20 20 36 20 20 2f 2a 20 6c 72 U 6 /* lr
49470 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 23 u page list */.#
49480 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 define SQLITE_MU
49490 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 TEX_STATIC_LRU2
494a0 20 20 20 20 20 37 20 20 2f 2a 20 4e 4f 54 20 55 7 /* NOT U
494b0 53 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 SED */.#define S
494c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
494d0 49 43 5f 50 4d 45 4d 20 20 20 20 20 20 37 20 20 IC_PMEM 7
494e0 2f 2a 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 /* sqlite3PageMa
494f0 6c 6c 6f 63 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a lloc() */../*.**
49500 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 69 CAPI3REF: Retri
49510 65 76 65 20 74 68 65 20 6d 75 74 65 78 20 66 6f eve the mutex fo
49520 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e r a database con
49530 6e 65 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 5e 54 nection.**.** ^T
49540 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 72 65 his interface re
49550 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
49560 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 the [sqlite3_mut
49570 65 78 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 ex] object that
49580 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 65 73 20 61 .** serializes a
49590 63 63 65 73 73 20 74 6f 20 74 68 65 20 5b 64 61 ccess to the [da
495a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
495b0 6e 5d 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 n] given in the
495c0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 65 6e argument.** when
495d0 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 the [threading
495e0 6d 6f 64 65 5d 20 69 73 20 53 65 72 69 61 6c 69 mode] is Seriali
495f0 7a 65 64 2e 0a 2a 2a 20 5e 49 66 20 74 68 65 20 zed..** ^If the
49600 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d [threading mode]
49610 20 69 73 20 53 69 6e 67 6c 65 2d 74 68 72 65 61 is Single-threa
49620 64 20 6f 72 20 4d 75 6c 74 69 2d 74 68 72 65 61 d or Multi-threa
49630 64 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 d then this.** r
49640 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 outine returns a
49650 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a NULL pointer..*
49660 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
49670 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 ite3_mutex *sqli
49680 74 65 33 5f 64 62 5f 6d 75 74 65 78 28 73 71 6c te3_db_mutex(sql
49690 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ite3*);../*.** C
496a0 41 50 49 33 52 45 46 3a 20 4c 6f 77 2d 4c 65 76 API3REF: Low-Lev
496b0 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f 66 20 44 61 el Control Of Da
496c0 74 61 62 61 73 65 20 46 69 6c 65 73 0a 2a 2a 0a tabase Files.**.
496d0 2a 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74 65 33 ** ^The [sqlite3
496e0 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d _file_control()]
496f0 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 interface makes
49700 20 61 20 64 69 72 65 63 74 20 63 61 6c 6c 20 74 a direct call t
49710 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f o the.** xFileCo
49720 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72 ntrol method for
49730 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f the [sqlite3_io
49740 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 _methods] object
49750 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 associated.** w
49760 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 ith a particular
49770 20 64 61 74 61 62 61 73 65 20 69 64 65 6e 74 69 database identi
49780 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f fied by the seco
49790 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 5e 54 68 nd argument. ^Th
497a0 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 e.** name of the
497b0 20 64 61 74 61 62 61 73 65 20 69 73 20 22 6d 61 database is "ma
497c0 69 6e 22 20 66 6f 72 20 74 68 65 20 6d 61 69 6e in" for the main
497d0 20 64 61 74 61 62 61 73 65 20 6f 72 20 22 74 65 database or "te
497e0 6d 70 22 20 66 6f 72 20 74 68 65 0a 2a 2a 20 54 mp" for the.** T
497f0 45 4d 50 20 64 61 74 61 62 61 73 65 2c 20 6f 72 EMP database, or
49800 20 74 68 65 20 6e 61 6d 65 20 74 68 61 74 20 61 the name that a
49810 70 70 65 61 72 73 20 61 66 74 65 72 20 74 68 65 ppears after the
49820 20 41 53 20 6b 65 79 77 6f 72 64 20 66 6f 72 0a AS keyword for.
49830 2a 2a 20 64 61 74 61 62 61 73 65 73 20 74 68 61 ** databases tha
49840 74 20 61 72 65 20 61 64 64 65 64 20 75 73 69 6e t are added usin
49850 67 20 74 68 65 20 5b 41 54 54 41 43 48 5d 20 53 g the [ATTACH] S
49860 51 4c 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 20 5e QL command..** ^
49870 41 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 63 A NULL pointer c
49880 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c an be used in pl
49890 61 63 65 20 6f 66 20 22 6d 61 69 6e 22 20 74 6f ace of "main" to
498a0 20 72 65 66 65 72 20 74 6f 20 74 68 65 0a 2a 2a refer to the.**
498b0 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
498c0 69 6c 65 2e 0a 2a 2a 20 5e 54 68 65 20 74 68 69 ile..** ^The thi
498d0 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 rd and fourth pa
498e0 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 rameters to this
498f0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 72 65 20 routine.** are
49900 70 61 73 73 65 64 20 64 69 72 65 63 74 6c 79 20 passed directly
49910 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73 through to the s
49920 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 econd and third
49930 70 61 72 61 6d 65 74 65 72 73 20 6f 66 0a 2a 2a parameters of.**
49940 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f the xFileContro
49950 6c 20 6d 65 74 68 6f 64 2e 20 20 5e 54 68 65 20 l method. ^The
49960 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 return value of
49970 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c the xFileControl
49980 0a 2a 2a 20 6d 65 74 68 6f 64 20 62 65 63 6f 6d .** method becom
49990 65 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 es the return va
499a0 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 lue of this rout
499b0 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 ine..**.** ^The
499c0 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 46 49 4c SQLITE_FCNTL_FIL
499d0 45 5f 50 4f 49 4e 54 45 52 20 76 61 6c 75 65 20 E_POINTER value
499e0 66 6f 72 20 74 68 65 20 6f 70 20 70 61 72 61 6d for the op param
499f0 65 74 65 72 20 63 61 75 73 65 73 0a 2a 2a 20 61 eter causes.** a
49a00 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
49a10 75 6e 64 65 72 6c 79 69 6e 67 20 5b 73 71 6c 69 underlying [sqli
49a20 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 te3_file] object
49a30 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 to be written i
49a40 6e 74 6f 0a 2a 2a 20 74 68 65 20 73 70 61 63 65 nto.** the space
49a50 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 pointed to by t
49a60 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 he 4th parameter
49a70 2e 20 20 5e 54 68 65 20 53 51 4c 49 54 45 5f 46 . ^The SQLITE_F
49a80 43 4e 54 4c 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 CNTL_FILE_POINTE
49a90 52 0a 2a 2a 20 63 61 73 65 20 69 73 20 61 20 73 R.** case is a s
49aa0 68 6f 72 74 2d 63 69 72 63 75 69 74 20 70 61 74 hort-circuit pat
49ab0 68 20 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 h which does not
49ac0 20 61 63 74 75 61 6c 6c 79 20 69 6e 76 6f 6b 65 actually invoke
49ad0 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 the.** underlyi
49ae0 6e 67 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 ng sqlite3_io_me
49af0 74 68 6f 64 73 2e 78 46 69 6c 65 43 6f 6e 74 72 thods.xFileContr
49b00 6f 6c 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a ol method..**.**
49b10 20 5e 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 ^If the second
49b20 70 61 72 61 6d 65 74 65 72 20 28 7a 44 62 4e 61 parameter (zDbNa
49b30 6d 65 29 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 me) does not mat
49b40 63 68 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 ch the name of a
49b50 6e 79 0a 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62 ny.** open datab
49b60 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 ase file, then S
49b70 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 QLITE_ERROR is r
49b80 65 74 75 72 6e 65 64 2e 20 20 5e 54 68 69 73 20 eturned. ^This
49b90 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 error.** code is
49ba0 20 6e 6f 74 20 72 65 6d 65 6d 62 65 72 65 64 20 not remembered
49bb0 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 and will not be
49bc0 72 65 63 61 6c 6c 65 64 20 62 79 20 5b 73 71 6c recalled by [sql
49bd0 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 0a ite3_errcode()].
49be0 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 ** or [sqlite3_e
49bf0 72 72 6d 73 67 28 29 5d 2e 20 20 54 68 65 20 75 rrmsg()]. The u
49c00 6e 64 65 72 6c 79 69 6e 67 20 78 46 69 6c 65 43 nderlying xFileC
49c10 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 6d 69 ontrol method mi
49c20 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 72 65 74 75 ght.** also retu
49c30 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e rn SQLITE_ERROR.
49c40 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61 There is no wa
49c50 79 20 74 6f 20 64 69 73 74 69 6e 67 75 69 73 68 y to distinguish
49c60 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 6e 20 69 between.** an i
49c70 6e 63 6f 72 72 65 63 74 20 7a 44 62 4e 61 6d 65 ncorrect zDbName
49c80 20 61 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f 45 and an SQLITE_E
49c90 52 52 4f 52 20 72 65 74 75 72 6e 20 66 72 6f 6d RROR return from
49ca0 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a the underlying.
49cb0 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 ** xFileControl
49cc0 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 method..**.** Se
49cd0 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f e also: [SQLITE_
49ce0 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d FCNTL_LOCKSTATE]
49cf0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
49d00 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f nt sqlite3_file_
49d10 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 2a control(sqlite3*
49d20 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 , const char *zD
49d30 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70 2c 20 76 bName, int op, v
49d40 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 oid*);../*.** CA
49d50 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e 67 20 PI3REF: Testing
49d60 49 6e 74 65 72 66 61 63 65 0a 2a 2a 0a 2a 2a 20 Interface.**.**
49d70 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 ^The sqlite3_tes
49d80 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 t_control() inte
49d90 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f rface is used to
49da0 20 72 65 61 64 20 6f 75 74 20 69 6e 74 65 72 6e read out intern
49db0 61 6c 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20 53 al.** state of S
49dc0 51 4c 69 74 65 20 61 6e 64 20 74 6f 20 69 6e 6a QLite and to inj
49dd0 65 63 74 20 66 61 75 6c 74 73 20 69 6e 74 6f 20 ect faults into
49de0 53 51 4c 69 74 65 20 66 6f 72 20 74 65 73 74 69 SQLite for testi
49df0 6e 67 0a 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 ng.** purposes.
49e00 20 5e 54 68 65 20 66 69 72 73 74 20 70 61 72 61 ^The first para
49e10 6d 65 74 65 72 20 69 73 20 61 6e 20 6f 70 65 72 meter is an oper
49e20 61 74 69 6f 6e 20 63 6f 64 65 20 74 68 61 74 20 ation code that
49e30 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 74 68 determines.** th
49e40 65 20 6e 75 6d 62 65 72 2c 20 6d 65 61 6e 69 6e e number, meanin
49e50 67 2c 20 61 6e 64 20 6f 70 65 72 61 74 69 6f 6e g, and operation
49e60 20 6f 66 20 61 6c 6c 20 73 75 62 73 65 71 75 65 of all subseque
49e70 6e 74 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a nt parameters..*
49e80 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 *.** This interf
49e90 61 63 65 20 69 73 20 6e 6f 74 20 66 6f 72 20 75 ace is not for u
49ea0 73 65 20 62 79 20 61 70 70 6c 69 63 61 74 69 6f se by applicatio
49eb0 6e 73 2e 20 20 49 74 20 65 78 69 73 74 73 20 73 ns. It exists s
49ec0 6f 6c 65 6c 79 0a 2a 2a 20 66 6f 72 20 76 65 72 olely.** for ver
49ed0 69 66 79 69 6e 67 20 74 68 65 20 63 6f 72 72 65 ifying the corre
49ee0 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 ct operation of
49ef0 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 the SQLite libra
49f00 72 79 2e 20 20 44 65 70 65 6e 64 69 6e 67 0a 2a ry. Depending.*
49f10 2a 20 6f 6e 20 68 6f 77 20 74 68 65 20 53 51 4c * on how the SQL
49f20 69 74 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 ite library is c
49f30 6f 6d 70 69 6c 65 64 2c 20 74 68 69 73 20 69 6e ompiled, this in
49f40 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 6e 6f terface might no
49f50 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 t exist..**.** T
49f60 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 he details of th
49f70 65 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 e operation code
49f80 73 2c 20 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 s, their meaning
49f90 73 2c 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 s, the parameter
49fa0 73 0a 2a 2a 20 74 68 65 79 20 74 61 6b 65 2c 20 s.** they take,
49fb0 61 6e 64 20 77 68 61 74 20 74 68 65 79 20 64 6f and what they do
49fc0 20 61 72 65 20 61 6c 6c 20 73 75 62 6a 65 63 74 are all subject
49fd0 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f to change witho
49fe0 75 74 20 6e 6f 74 69 63 65 2e 0a 2a 2a 20 55 6e ut notice..** Un
49ff0 6c 69 6b 65 20 6d 6f 73 74 20 6f 66 20 74 68 65 like most of the
4a000 20 53 51 4c 69 74 65 20 41 50 49 2c 20 74 68 69 SQLite API, thi
4a010 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f s function is no
4a020 74 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 0a t guaranteed to.
4a030 2a 2a 20 6f 70 65 72 61 74 65 20 63 6f 6e 73 69 ** operate consi
4a040 73 74 65 6e 74 6c 79 20 66 72 6f 6d 20 6f 6e 65 stently from one
4a050 20 72 65 6c 65 61 73 65 20 74 6f 20 74 68 65 20 release to the
4a060 6e 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f next..*/.SQLITE_
4a070 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
4a080 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74 test_control(int
4a090 20 6f 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a op, ...);../*.*
4a0a0 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 * CAPI3REF: Test
4a0b0 69 6e 67 20 49 6e 74 65 72 66 61 63 65 20 4f 70 ing Interface Op
4a0c0 65 72 61 74 69 6f 6e 20 43 6f 64 65 73 0a 2a 2a eration Codes.**
4a0d0 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 .** These consta
4a0e0 6e 74 73 20 61 72 65 20 74 68 65 20 76 61 6c 69 nts are the vali
4a0f0 64 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 d operation code
4a100 20 70 61 72 61 6d 65 74 65 72 73 20 75 73 65 64 parameters used
4a110 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 .** as the first
4a120 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 argument to [sq
4a130 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 lite3_test_contr
4a140 6f 6c 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ol()]..**.** The
4a150 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 6e se parameters an
4a160 64 20 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 d their meanings
4a170 20 61 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 are subject to
4a180 63 68 61 6e 67 65 0a 2a 2a 20 77 69 74 68 6f 75 change.** withou
4a190 74 20 6e 6f 74 69 63 65 2e 20 20 54 68 65 73 65 t notice. These
4a1a0 20 76 61 6c 75 65 73 20 61 72 65 20 66 6f 72 20 values are for
4a1b0 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 testing purposes
4a1c0 20 6f 6e 6c 79 2e 0a 2a 2a 20 41 70 70 6c 69 63 only..** Applic
4a1d0 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f ations should no
4a1e0 74 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 t use any of the
4a1f0 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72 se parameters or
4a200 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
4a210 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d _test_control()]
4a220 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 interface..*/.#
4a230 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
4a240 53 54 43 54 52 4c 5f 46 49 52 53 54 20 20 20 20 STCTRL_FIRST
4a250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4a260 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5.#define SQLITE
4a270 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 53 _TESTCTRL_PRNG_S
4a280 41 56 45 20 20 20 20 20 20 20 20 20 20 20 20 20 AVE
4a290 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 5.#define SQL
4a2a0 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e ITE_TESTCTRL_PRN
4a2b0 47 5f 52 45 53 54 4f 52 45 20 20 20 20 20 20 20 G_RESTORE
4a2c0 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 6.#define
4a2d0 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f SQLITE_TESTCTRL_
4a2e0 50 52 4e 47 5f 52 45 53 45 54 20 20 20 20 20 20 PRNG_RESET
4a2f0 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 7.#defi
4a300 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 ne SQLITE_TESTCT
4a310 52 4c 5f 42 49 54 56 45 43 5f 54 45 53 54 20 20 RL_BITVEC_TEST
4a320 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 8.#d
4a330 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 efine SQLITE_TES
4a340 54 43 54 52 4c 5f 46 41 55 4c 54 5f 49 4e 53 54 TCTRL_FAULT_INST
4a350 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 39 ALL 9
4a360 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
4a370 54 45 53 54 43 54 52 4c 5f 42 45 4e 49 47 4e 5f TESTCTRL_BENIGN_
4a380 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 20 20 20 20 MALLOC_HOOKS
4a390 20 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 10.#define SQLI
4a3a0 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e 44 TE_TESTCTRL_PEND
4a3b0 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20 20 ING_BYTE
4a3c0 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20 53 11.#define S
4a3d0 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 QLITE_TESTCTRL_A
4a3e0 53 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20 SSERT
4a3f0 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 12.#defin
4a400 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 e SQLITE_TESTCTR
4a410 4c 5f 41 4c 57 41 59 53 20 20 20 20 20 20 20 20 L_ALWAYS
4a420 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 13.#de
4a430 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 fine SQLITE_TEST
4a440 43 54 52 4c 5f 52 45 53 45 52 56 45 20 20 20 20 CTRL_RESERVE
4a450 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 0a 14.
4a460 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 #define SQLITE_T
4a470 45 53 54 43 54 52 4c 5f 4f 50 54 49 4d 49 5a 41 ESTCTRL_OPTIMIZA
4a480 54 49 4f 4e 53 20 20 20 20 20 20 20 20 20 20 20 TIONS
4a490 31 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 15.#define SQLIT
4a4a0 45 5f 54 45 53 54 43 54 52 4c 5f 49 53 4b 45 59 E_TESTCTRL_ISKEY
4a4b0 57 4f 52 44 20 20 20 20 20 20 20 20 20 20 20 20 WORD
4a4c0 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53 51 16.#define SQ
4a4d0 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 53 43 LITE_TESTCTRL_SC
4a4e0 52 41 54 43 48 4d 41 4c 4c 4f 43 20 20 20 20 20 RATCHMALLOC
4a4f0 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65 17.#define
4a500 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c SQLITE_TESTCTRL
4a510 5f 4c 4f 43 41 4c 54 49 4d 45 5f 46 41 55 4c 54 _LOCALTIME_FAULT
4a520 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65 66 18.#def
4a530 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 ine SQLITE_TESTC
4a540 54 52 4c 5f 45 58 50 4c 41 49 4e 5f 53 54 4d 54 TRL_EXPLAIN_STMT
4a550 20 20 20 20 20 20 20 20 20 20 20 20 31 39 0a 23 19.#
4a560 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
4a570 53 54 43 54 52 4c 5f 4c 41 53 54 20 20 20 20 20 STCTRL_LAST
4a580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4a590 39 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 9../*.** CAPI3RE
4a5a0 46 3a 20 53 51 4c 69 74 65 20 52 75 6e 74 69 6d F: SQLite Runtim
4a5b0 65 20 53 74 61 74 75 73 0a 2a 2a 0a 2a 2a 20 5e e Status.**.** ^
4a5c0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 This interface i
4a5d0 73 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 s used to retrie
4a5e0 76 65 20 72 75 6e 74 69 6d 65 20 73 74 61 74 75 ve runtime statu
4a5f0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a s information.**
4a600 20 61 62 6f 75 74 20 74 68 65 20 70 65 72 66 6f about the perfo
4a610 72 6d 61 6e 63 65 20 6f 66 20 53 51 4c 69 74 65 rmance of SQLite
4a620 2c 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 , and optionally
4a630 20 74 6f 20 72 65 73 65 74 20 76 61 72 69 6f 75 to reset variou
4a640 73 0a 2a 2a 20 68 69 67 68 77 61 74 65 72 20 6d s.** highwater m
4a650 61 72 6b 73 2e 20 20 5e 54 68 65 20 66 69 72 73 arks. ^The firs
4a660 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e t argument is an
4a670 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f integer code fo
4a680 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 r.** the specifi
4a690 63 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 6d c parameter to m
4a6a0 65 61 73 75 72 65 2e 20 20 5e 28 52 65 63 6f 67 easure. ^(Recog
4a6b0 6e 69 7a 65 64 20 69 6e 74 65 67 65 72 20 63 6f nized integer co
4a6c0 64 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20 74 68 des.** are of th
4a6d0 65 20 66 6f 72 6d 20 5b 73 74 61 74 75 73 20 70 e form [status p
4a6e0 61 72 61 6d 65 74 65 72 73 20 7c 20 53 51 4c 49 arameters | SQLI
4a6f0 54 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e 29 TE_STATUS_...].)
4a700 5e 0a 2a 2a 20 5e 54 68 65 20 63 75 72 72 65 6e ^.** ^The curren
4a710 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 t value of the p
4a720 61 72 61 6d 65 74 65 72 20 69 73 20 72 65 74 75 arameter is retu
4a730 72 6e 65 64 20 69 6e 74 6f 20 2a 70 43 75 72 72 rned into *pCurr
4a740 65 6e 74 2e 0a 2a 2a 20 5e 54 68 65 20 68 69 67 ent..** ^The hig
4a750 68 65 73 74 20 72 65 63 6f 72 64 65 64 20 76 61 hest recorded va
4a760 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 lue is returned
4a770 69 6e 20 2a 70 48 69 67 68 77 61 74 65 72 2e 20 in *pHighwater.
4a780 20 5e 49 66 20 74 68 65 0a 2a 2a 20 72 65 73 65 ^If the.** rese
4a790 74 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 tFlag is true, t
4a7a0 68 65 6e 20 74 68 65 20 68 69 67 68 65 73 74 20 hen the highest
4a7b0 72 65 63 6f 72 64 20 76 61 6c 75 65 20 69 73 20 record value is
4a7c0 72 65 73 65 74 20 61 66 74 65 72 0a 2a 2a 20 2a reset after.** *
4a7d0 70 48 69 67 68 77 61 74 65 72 20 69 73 20 77 72 pHighwater is wr
4a7e0 69 74 74 65 6e 2e 20 20 5e 28 53 6f 6d 65 20 70 itten. ^(Some p
4a7f0 61 72 61 6d 65 74 65 72 73 20 64 6f 20 6e 6f 74 arameters do not
4a800 20 72 65 63 6f 72 64 20 74 68 65 20 68 69 67 68 record the high
4a810 65 73 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46 est.** value. F
4a820 6f 72 20 74 68 6f 73 65 20 70 61 72 61 6d 65 74 or those paramet
4a830 65 72 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 69 ers.** nothing i
4a840 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a s written into *
4a850 70 48 69 67 68 77 61 74 65 72 20 61 6e 64 20 74 pHighwater and t
4a860 68 65 20 72 65 73 65 74 46 6c 61 67 20 69 73 20 he resetFlag is
4a870 69 67 6e 6f 72 65 64 2e 29 5e 0a 2a 2a 20 5e 28 ignored.)^.** ^(
4a880 4f 74 68 65 72 20 70 61 72 61 6d 65 74 65 72 73 Other parameters
4a890 20 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68 65 record only the
4a8a0 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 highwater mark
4a8b0 61 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72 72 and not the curr
4a8c0 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46 ent.** value. F
4a8d0 6f 72 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 or these latter
4a8e0 70 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68 69 parameters nothi
4a8f0 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e ng is written in
4a900 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 29 5e 0a to *pCurrent.)^.
4a910 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 **.** ^The sqlit
4a920 65 33 5f 73 74 61 74 75 73 28 29 20 72 6f 75 74 e3_status() rout
4a930 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ine returns SQLI
4a940 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
4a950 20 61 6e 64 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 and a.** non-ze
4a960 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 ro [error code]
4a970 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a on failure..**.*
4a980 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
4a990 73 20 74 68 72 65 61 64 73 61 66 65 20 62 75 74 s threadsafe but
4a9a0 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 is not atomic.
4a9b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 This routine ca
4a9c0 6e 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 n be.** called w
4a9d0 68 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61 hile other threa
4a9e0 64 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74 ds are running t
4a9f0 68 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65 he same or diffe
4aa00 72 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 69 rent SQLite.** i
4aa10 6e 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65 nterfaces. Howe
4aa20 76 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72 ver the values r
4aa30 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72 eturned in *pCur
4aa40 72 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 69 rent and.** *pHi
4aa50 67 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 20 ghwater reflect
4aa60 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 51 the status of SQ
4aa70 4c 69 74 65 20 61 74 20 64 69 66 66 65 72 65 6e Lite at differen
4aa80 74 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d 65 t points in time
4aa90 0a 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 6f .** and it is po
4aaa0 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 ssible that anot
4aab0 68 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 74 her thread might
4aac0 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 61 change the para
4aad0 6d 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 77 meter.** in betw
4aae0 65 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 68 een the times wh
4aaf0 65 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64 en *pCurrent and
4ab00 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 65 *pHighwater are
4ab10 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 written..**.**
4ab20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 See also: [sqlit
4ab30 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 0a e3_db_status()].
4ab40 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
4ab50 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 t sqlite3_status
4ab60 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 (int op, int *pC
4ab70 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 urrent, int *pHi
4ab80 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 ghwater, int res
4ab90 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a etFlag);.../*.**
4aba0 20 43 41 50 49 33 52 45 46 3a 20 53 74 61 74 75 CAPI3REF: Statu
4abb0 73 20 50 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 s Parameters.**
4abc0 4b 45 59 57 4f 52 44 53 3a 20 7b 73 74 61 74 75 KEYWORDS: {statu
4abd0 73 20 70 61 72 61 6d 65 74 65 72 73 7d 0a 2a 2a s parameters}.**
4abe0 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 .** These intege
4abf0 72 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 73 69 r constants desi
4ac00 67 6e 61 74 65 20 76 61 72 69 6f 75 73 20 72 75 gnate various ru
4ac10 6e 2d 74 69 6d 65 20 73 74 61 74 75 73 20 70 61 n-time status pa
4ac20 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61 74 rameters.** that
4ac30 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 can be returned
4ac40 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 by [sqlite3_sta
4ac50 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 tus()]..**.** <d
4ac60 6c 3e 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53 l>.** [[SQLITE_S
4ac70 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 TATUS_MEMORY_USE
4ac80 44 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 D]] ^(<dt>SQLITE
4ac90 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 _STATUS_MEMORY_U
4aca0 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e SED</dt>.** <dd>
4acb0 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 This parameter i
4acc0 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 61 6d s the current am
4acd0 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 ount of memory c
4ace0 68 65 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75 73 hecked out.** us
4acf0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c ing [sqlite3_mal
4ad00 6c 6f 63 28 29 5d 2c 20 65 69 74 68 65 72 20 64 loc()], either d
4ad10 69 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72 irectly or indir
4ad20 65 63 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 66 ectly. The.** f
4ad30 69 67 75 72 65 20 69 6e 63 6c 75 64 65 73 20 63 igure includes c
4ad40 61 6c 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73 71 alls made to [sq
4ad50 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 lite3_malloc()]
4ad60 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 by the applicati
4ad70 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72 6e on.** and intern
4ad80 61 6c 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 al memory usage
4ad90 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 by the SQLite li
4ada0 62 72 61 72 79 2e 20 20 53 63 72 61 74 63 68 20 brary. Scratch
4adb0 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72 6f memory.** contro
4adc0 6c 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f lled by [SQLITE_
4add0 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 CONFIG_SCRATCH]
4ade0 61 6e 64 20 61 75 78 69 6c 69 61 72 79 20 70 61 and auxiliary pa
4adf0 67 65 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d 6f ge-cache.** memo
4ae00 72 79 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 ry controlled by
4ae10 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
4ae20 50 41 47 45 43 41 43 48 45 5d 20 69 73 20 6e 6f PAGECACHE] is no
4ae30 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a 2a t included in.**
4ae40 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 2e this parameter.
4ae50 20 20 54 68 65 20 61 6d 6f 75 6e 74 20 72 65 74 The amount ret
4ae60 75 72 6e 65 64 20 69 73 20 74 68 65 20 73 75 6d urned is the sum
4ae70 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 of the allocati
4ae80 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20 72 on.** sizes as r
4ae90 65 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 78 eported by the x
4aea0 53 69 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20 5b Size method in [
4aeb0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
4aec0 6f 64 73 5d 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a ods].</dd>)^.**.
4aed0 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53 54 41 54 ** [[SQLITE_STAT
4aee0 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 5d 5d US_MALLOC_SIZE]]
4aef0 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ^(<dt>SQLITE_ST
4af00 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 ATUS_MALLOC_SIZE
4af10 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
4af20 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f s parameter reco
4af30 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 rds the largest
4af40 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
4af50 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e n request.** han
4af60 64 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f ded to [sqlite3_
4af70 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 malloc()] or [sq
4af80 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d lite3_realloc()]
4af90 20 28 6f 72 20 74 68 65 69 72 0a 2a 2a 20 69 6e (or their.** in
4afa0 74 65 72 6e 61 6c 20 65 71 75 69 76 61 6c 65 6e ternal equivalen
4afb0 74 73 29 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 ts). Only the v
4afc0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e alue returned in
4afd0 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 the.** *pHighwa
4afe0 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f ter parameter to
4aff0 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 [sqlite3_status
4b000 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 ()] is of intere
4b010 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c st. .** The val
4b020 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 ue written into
4b030 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 the *pCurrent pa
4b040 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 rameter is undef
4b050 69 6e 65 64 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a ined.</dd>)^.**.
4b060 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53 54 41 54 ** [[SQLITE_STAT
4b070 55 53 5f 4d 41 4c 4c 4f 43 5f 43 4f 55 4e 54 5d US_MALLOC_COUNT]
4b080 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 53 ] ^(<dt>SQLITE_S
4b090 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 43 4f 55 TATUS_MALLOC_COU
4b0a0 4e 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 NT</dt>.** <dd>T
4b0b0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 his parameter re
4b0c0 63 6f 72 64 73 20 74 68 65 20 6e 75 6d 62 65 72 cords the number
4b0d0 20 6f 66 20 73 65 70 61 72 61 74 65 20 6d 65 6d of separate mem
4b0e0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a ory allocations.
4b0f0 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 ** currently che
4b100 63 6b 65 64 20 6f 75 74 2e 3c 2f 64 64 3e 29 5e cked out.</dd>)^
4b110 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f .**.** [[SQLITE_
4b120 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 STATUS_PAGECACHE
4b130 5f 55 53 45 44 5d 5d 20 5e 28 3c 64 74 3e 53 51 _USED]] ^(<dt>SQ
4b140 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
4b150 43 41 43 48 45 5f 55 53 45 44 3c 2f 64 74 3e 0a CACHE_USED</dt>.
4b160 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 ** <dd>This para
4b170 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 meter returns th
4b180 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
4b190 73 20 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68 s used out of th
4b1a0 65 0a 2a 2a 20 5b 70 61 67 65 63 61 63 68 65 20 e.** [pagecache
4b1b0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
4b1c0 5d 20 74 68 61 74 20 77 61 73 20 63 6f 6e 66 69 ] that was confi
4b1d0 67 75 72 65 64 20 75 73 69 6e 67 20 0a 2a 2a 20 gured using .**
4b1e0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 [SQLITE_CONFIG_P
4b1f0 41 47 45 43 41 43 48 45 5d 2e 20 20 54 68 65 0a AGECACHE]. The.
4b200 2a 2a 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 ** value returne
4b210 64 20 69 73 20 69 6e 20 70 61 67 65 73 2c 20 6e d is in pages, n
4b220 6f 74 20 69 6e 20 62 79 74 65 73 2e 3c 2f 64 64 ot in bytes.</dd
4b230 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 >)^.**.** [[SQLI
4b240 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 TE_STATUS_PAGECA
4b250 43 48 45 5f 4f 56 45 52 46 4c 4f 57 5d 5d 20 0a CHE_OVERFLOW]] .
4b260 2a 2a 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f ** ^(<dt>SQLITE_
4b270 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 STATUS_PAGECACHE
4b280 5f 4f 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a _OVERFLOW</dt>.*
4b290 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d * <dd>This param
4b2a0 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 eter returns the
4b2b0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
4b2c0 20 6f 66 20 70 61 67 65 20 63 61 63 68 65 0a 2a of page cache.*
4b2d0 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 * allocation whi
4b2e0 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ch could not be
4b2f0 73 61 74 69 73 66 69 65 64 20 62 79 20 74 68 65 satisfied by the
4b300 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
4b310 50 41 47 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 PAGECACHE].** bu
4b320 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 ffer and where f
4b330 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f orced to overflo
4b340 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 w to [sqlite3_ma
4b350 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a lloc()]. The.**
4b360 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 returned value
4b370 69 6e 63 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 includes allocat
4b380 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c ions that overfl
4b390 6f 77 65 64 20 62 65 63 61 75 73 65 20 74 68 65 owed because the
4b3a0 79 0a 2a 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c y.** where too l
4b3b0 61 72 67 65 20 28 74 68 65 79 20 77 65 72 65 20 arge (they were
4b3c0 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 larger than the
4b3d0 22 73 7a 22 20 70 61 72 61 6d 65 74 65 72 20 74 "sz" parameter t
4b3e0 6f 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e o.** [SQLITE_CON
4b3f0 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 29 20 FIG_PAGECACHE])
4b400 61 6e 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 and allocations
4b410 74 68 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 that overflowed
4b420 62 65 63 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 because.** no sp
4b430 61 63 65 20 77 61 73 20 6c 65 66 74 20 69 6e 20 ace was left in
4b440 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 3c the page cache.<
4b450 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 /dd>)^.**.** [[S
4b460 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 QLITE_STATUS_PAG
4b470 45 43 41 43 48 45 5f 53 49 5a 45 5d 5d 20 5e 28 ECACHE_SIZE]] ^(
4b480 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 <dt>SQLITE_STATU
4b490 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45 S_PAGECACHE_SIZE
4b4a0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
4b4b0 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f s parameter reco
4b4c0 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 rds the largest
4b4d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
4b4e0 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e n request.** han
4b4f0 64 65 64 20 74 6f 20 5b 70 61 67 65 63 61 63 68 ded to [pagecach
4b500 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
4b510 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 or]. Only the v
4b520 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e alue returned in
4b530 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 the.** *pHighwa
4b540 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f ter parameter to
4b550 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 [sqlite3_status
4b560 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 ()] is of intere
4b570 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c st. .** The val
4b580 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 ue written into
4b590 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 the *pCurrent pa
4b5a0 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 rameter is undef
4b5b0 69 6e 65 64 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a ined.</dd>)^.**.
4b5c0 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53 54 41 54 ** [[SQLITE_STAT
4b5d0 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 5d US_SCRATCH_USED]
4b5e0 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 53 ] ^(<dt>SQLITE_S
4b5f0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 TATUS_SCRATCH_US
4b600 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 ED</dt>.** <dd>T
4b610 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 his parameter re
4b620 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
4b630 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 of allocations
4b640 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68 65 0a used out of the.
4b650 2a 2a 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f ** [scratch memo
4b660 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f ry allocator] co
4b670 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a 2a nfigured using.*
4b680 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 * [SQLITE_CONFIG
4b690 5f 53 43 52 41 54 43 48 5d 2e 20 20 54 68 65 20 _SCRATCH]. The
4b6a0 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 value returned i
4b6b0 73 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 s in allocations
4b6c0 2c 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79 74 65 , not.** in byte
4b6d0 73 2e 20 20 53 69 6e 63 65 20 61 20 73 69 6e 67 s. Since a sing
4b6e0 6c 65 20 74 68 72 65 61 64 20 6d 61 79 20 6f 6e le thread may on
4b6f0 6c 79 20 68 61 76 65 20 6f 6e 65 20 73 63 72 61 ly have one scra
4b700 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a tch allocation.*
4b710 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 * outstanding at
4b720 20 74 69 6d 65 2c 20 74 68 69 73 20 70 61 72 61 time, this para
4b730 6d 65 74 65 72 20 61 6c 73 6f 20 72 65 70 6f 72 meter also repor
4b740 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ts the number of
4b750 20 74 68 72 65 61 64 73 0a 2a 2a 20 75 73 69 6e threads.** usin
4b760 67 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 g scratch memory
4b770 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
4b780 65 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 e.</dd>)^.**.**
4b790 5b 5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f [[SQLITE_STATUS_
4b7a0 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 SCRATCH_OVERFLOW
4b7b0 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f ]] ^(<dt>SQLITE_
4b7c0 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f STATUS_SCRATCH_O
4b7d0 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 VERFLOW</dt>.**
4b7e0 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 <dd>This paramet
4b7f0 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e er returns the n
4b800 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
4b810 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 f scratch memory
4b820 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 .** allocation w
4b830 68 69 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 hich could not b
4b840 65 20 73 61 74 69 73 66 69 65 64 20 62 79 20 74 e satisfied by t
4b850 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 he [SQLITE_CONFI
4b860 47 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 75 G_SCRATCH].** bu
4b870 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 ffer and where f
4b880 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f orced to overflo
4b890 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 w to [sqlite3_ma
4b8a0 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 61 lloc()]. The va
4b8b0 6c 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 lues.** returned
4b8c0 20 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c 6f include overflo
4b8d0 77 73 20 62 65 63 61 75 73 65 20 74 68 65 20 72 ws because the r
4b8e0 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 equested allocat
4b8f0 69 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c ion was too.** l
4b900 61 72 67 65 72 20 28 74 68 61 74 20 69 73 2c 20 arger (that is,
4b910 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 because the requ
4b920 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e ested allocation
4b930 20 77 61 73 20 6c 61 72 67 65 72 20 74 68 61 6e was larger than
4b940 20 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 72 the.** "sz" par
4b950 61 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 54 ameter to [SQLIT
4b960 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 E_CONFIG_SCRATCH
4b970 5d 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 6e ]) and because n
4b980 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 o scratch buffer
4b990 0a 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 61 .** slots were a
4b9a0 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 vailable..** </d
4b9b0 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c d>)^.**.** [[SQL
4b9c0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
4b9d0 43 48 5f 53 49 5a 45 5d 5d 20 5e 28 3c 64 74 3e CH_SIZE]] ^(<dt>
4b9e0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 SQLITE_STATUS_SC
4b9f0 52 41 54 43 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a RATCH_SIZE</dt>.
4ba00 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 ** <dd>This para
4ba10 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 meter records th
4ba20 65 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 e largest memory
4ba30 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 allocation requ
4ba40 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f est.** handed to
4ba50 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 [scratch memory
4ba60 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e allocator]. On
4ba70 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 ly the value ret
4ba80 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 urned in the.**
4ba90 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72 61 *pHighwater para
4baa0 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 meter to [sqlite
4bab0 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 6f 3_status()] is o
4bac0 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a f interest. .**
4bad0 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 74 The value writt
4bae0 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 en into the *pCu
4baf0 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 rrent parameter
4bb00 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 is undefined.</d
4bb10 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c d>)^.**.** [[SQL
4bb20 49 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 ITE_STATUS_PARSE
4bb30 52 5f 53 54 41 43 4b 5d 5d 20 5e 28 3c 64 74 3e R_STACK]] ^(<dt>
4bb40 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
4bb50 52 53 45 52 5f 53 54 41 43 4b 3c 2f 64 74 3e 0a RSER_STACK</dt>.
4bb60 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 ** <dd>This para
4bb70 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 meter records th
4bb80 65 20 64 65 65 70 65 73 74 20 70 61 72 73 65 72 e deepest parser
4bb90 20 73 74 61 63 6b 2e 20 20 49 74 20 69 73 20 6f stack. It is o
4bba0 6e 6c 79 0a 2a 2a 20 6d 65 61 6e 69 6e 67 66 75 nly.** meaningfu
4bbb0 6c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63 l if SQLite is c
4bbc0 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b 59 59 ompiled with [YY
4bbd0 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 TRACKMAXSTACKDEP
4bbe0 54 48 5d 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 20 3c TH].</dd>)^.** <
4bbf0 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 73 /dl>.**.** New s
4bc00 74 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 73 tatus parameters
4bc10 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 66 72 may be added fr
4bc20 6f 6d 20 74 69 6d 65 20 74 6f 20 74 69 6d 65 2e om time to time.
4bc30 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
4bc40 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 TE_STATUS_MEMORY
4bc50 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 20 30 _USED 0
4bc60 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
4bc70 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 STATUS_PAGECACHE
4bc80 5f 55 53 45 44 20 20 20 20 20 20 20 31 0a 23 64 _USED 1.#d
4bc90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 efine SQLITE_STA
4bca0 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 TUS_PAGECACHE_OV
4bcb0 45 52 46 4c 4f 57 20 20 20 32 0a 23 64 65 66 69 ERFLOW 2.#defi
4bcc0 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 ne SQLITE_STATUS
4bcd0 5f 53 43 52 41 54 43 48 5f 55 53 45 44 20 20 20 _SCRATCH_USED
4bce0 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 3.#define
4bcf0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 SQLITE_STATUS_SC
4bd00 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 20 20 RATCH_OVERFLOW
4bd10 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 4.#define SQL
4bd20 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f ITE_STATUS_MALLO
4bd30 43 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 20 C_SIZE
4bd40 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5.#define SQLITE
4bd50 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 _STATUS_PARSER_S
4bd60 54 41 43 4b 20 20 20 20 20 20 20 20 20 36 0a 23 TACK 6.#
4bd70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
4bd80 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53 ATUS_PAGECACHE_S
4bd90 49 5a 45 20 20 20 20 20 20 20 37 0a 23 64 65 66 IZE 7.#def
4bda0 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 ine SQLITE_STATU
4bdb0 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 20 20 S_SCRATCH_SIZE
4bdc0 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 8.#define
4bdd0 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d SQLITE_STATUS_M
4bde0 41 4c 4c 4f 43 5f 43 4f 55 4e 54 20 20 20 20 20 ALLOC_COUNT
4bdf0 20 20 20 20 39 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 9../*.** CAP
4be00 49 33 52 45 46 3a 20 44 61 74 61 62 61 73 65 20 I3REF: Database
4be10 43 6f 6e 6e 65 63 74 69 6f 6e 20 53 74 61 74 75 Connection Statu
4be20 73 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 69 6e s.**.** ^This in
4be30 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 terface is used
4be40 74 6f 20 72 65 74 72 69 65 76 65 20 72 75 6e 74 to retrieve runt
4be50 69 6d 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 ime status infor
4be60 6d 61 74 69 6f 6e 20 0a 2a 2a 20 61 62 6f 75 74 mation .** about
4be70 20 61 20 73 69 6e 67 6c 65 20 5b 64 61 74 61 62 a single [datab
4be80 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e ase connection].
4be90 20 20 5e 54 68 65 20 66 69 72 73 74 20 61 72 67 ^The first arg
4bea0 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 ument is the.**
4beb0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
4bec0 69 6f 6e 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 ion object to be
4bed0 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 20 interrogated.
4bee0 5e 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 ^The second argu
4bef0 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e ment.** is an in
4bf00 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2c 20 teger constant,
4bf10 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 73 taken from the s
4bf20 65 74 20 6f 66 0a 2a 2a 20 5b 53 51 4c 49 54 45 et of.** [SQLITE
4bf30 5f 44 42 53 54 41 54 55 53 20 6f 70 74 69 6f 6e _DBSTATUS option
4bf40 73 5d 2c 20 74 68 61 74 0a 2a 2a 20 64 65 74 65 s], that.** dete
4bf50 72 6d 69 6e 65 73 20 74 68 65 20 70 61 72 61 6d rmines the param
4bf60 65 74 65 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 eter to interrog
4bf70 61 74 65 2e 20 20 54 68 65 20 73 65 74 20 6f 66 ate. The set of
4bf80 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 42 53 .** [SQLITE_DBS
4bf90 54 41 54 55 53 20 6f 70 74 69 6f 6e 73 5d 20 69 TATUS options] i
4bfa0 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 74 6f 20 67 s likely.** to g
4bfb0 72 6f 77 20 69 6e 20 66 75 74 75 72 65 20 72 65 row in future re
4bfc0 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 leases of SQLite
4bfd0 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 63 75 72 ..**.** ^The cur
4bfe0 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 rent value of th
4bff0 65 20 72 65 71 75 65 73 74 65 64 20 70 61 72 61 e requested para
4c000 6d 65 74 65 72 20 69 73 20 77 72 69 74 74 65 6e meter is written
4c010 20 69 6e 74 6f 20 2a 70 43 75 72 0a 2a 2a 20 61 into *pCur.** a
4c020 6e 64 20 74 68 65 20 68 69 67 68 65 73 74 20 69 nd the highest i
4c030 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61 6c nstantaneous val
4c040 75 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e ue is written in
4c050 74 6f 20 2a 70 48 69 77 74 72 2e 20 20 5e 49 66 to *pHiwtr. ^If
4c060 0a 2a 2a 20 74 68 65 20 72 65 73 65 74 46 6c 67 .** the resetFlg
4c070 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
4c080 68 65 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 he highest insta
4c090 6e 74 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 ntaneous value i
4c0a0 73 0a 2a 2a 20 72 65 73 65 74 20 62 61 63 6b 20 s.** reset back
4c0b0 64 6f 77 6e 20 74 6f 20 74 68 65 20 63 75 72 72 down to the curr
4c0c0 65 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a ent value..**.**
4c0d0 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62 ^The sqlite3_db
4c0e0 5f 73 74 61 74 75 73 28 29 20 72 6f 75 74 69 6e _status() routin
4c0f0 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 e returns SQLITE
4c100 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 _OK on success a
4c110 6e 64 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f nd a.** non-zero
4c120 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e [error code] on
4c130 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 failure..**.**
4c140 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 See also: [sqlit
4c150 65 33 5f 73 74 61 74 75 73 28 29 5d 20 61 6e 64 e3_status()] and
4c160 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 [sqlite3_stmt_s
4c170 74 61 74 75 73 28 29 5d 2e 0a 2a 2f 0a 53 51 4c tatus()]..*/.SQL
4c180 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
4c190 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71 te3_db_status(sq
4c1a0 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 lite3*, int op,
4c1b0 69 6e 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a int *pCur, int *
4c1c0 70 48 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65 pHiwtr, int rese
4c1d0 74 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 tFlg);../*.** CA
4c1e0 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 PI3REF: Status P
4c1f0 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61 arameters for da
4c200 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
4c210 6e 73 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 ns.** KEYWORDS:
4c220 7b 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 {SQLITE_DBSTATUS
4c230 20 6f 70 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 options}.**.**
4c240 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 These constants
4c250 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c are the availabl
4c260 65 20 69 6e 74 65 67 65 72 20 22 76 65 72 62 73 e integer "verbs
4c270 22 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 61 " that can be pa
4c280 73 73 65 64 20 61 73 0a 2a 2a 20 74 68 65 20 73 ssed as.** the s
4c290 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
4c2a0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 o the [sqlite3_d
4c2b0 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65 b_status()] inte
4c2c0 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 rface..**.** New
4c2d0 20 76 65 72 62 73 20 6d 61 79 20 62 65 20 61 64 verbs may be ad
4c2e0 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 ded in future re
4c2f0 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 leases of SQLite
4c300 2e 20 45 78 69 73 74 69 6e 67 20 76 65 72 62 73 . Existing verbs
4c310 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 64 69 73 .** might be dis
4c320 63 6f 6e 74 69 6e 75 65 64 2e 20 41 70 70 6c 69 continued. Appli
4c330 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 63 cations should c
4c340 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 heck the return
4c350 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71 code from.** [sq
4c360 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 lite3_db_status(
4c370 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 )] to make sure
4c380 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20 77 6f that the call wo
4c390 72 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 rked..** The [sq
4c3a0 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 lite3_db_status(
4c3b0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c )] interface wil
4c3c0 6c 20 72 65 74 75 72 6e 20 61 20 6e 6f 6e 2d 7a l return a non-z
4c3d0 65 72 6f 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a ero error code.*
4c3e0 2a 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e * if a discontin
4c3f0 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 ued or unsupport
4c400 65 64 20 76 65 72 62 20 69 73 20 69 6e 76 6f 6b ed verb is invok
4c410 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a ed..**.** <dl>.*
4c420 2a 20 5b 5b 53 51 4c 49 54 45 5f 44 42 53 54 41 * [[SQLITE_DBSTA
4c430 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 TUS_LOOKASIDE_US
4c440 45 44 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 ED]] ^(<dt>SQLIT
4c450 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 E_DBSTATUS_LOOKA
4c460 53 49 44 45 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a SIDE_USED</dt>.*
4c470 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d * <dd>This param
4c480 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 eter returns the
4c490 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 6f 6b 61 number of looka
4c4a0 73 69 64 65 20 6d 65 6d 6f 72 79 20 73 6c 6f 74 side memory slot
4c4b0 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 63 s currently.** c
4c4c0 68 65 63 6b 65 64 20 6f 75 74 2e 3c 2f 64 64 3e hecked out.</dd>
4c4d0 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 )^.**.** [[SQLIT
4c4e0 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 E_DBSTATUS_LOOKA
4c4f0 53 49 44 45 5f 48 49 54 5d 5d 20 5e 28 3c 64 74 SIDE_HIT]] ^(<dt
4c500 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 >SQLITE_DBSTATUS
4c510 5f 4c 4f 4f 4b 41 53 49 44 45 5f 48 49 54 3c 2f _LOOKASIDE_HIT</
4c520 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
4c530 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e parameter return
4c540 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6d 61 6c s the number mal
4c550 6c 6f 63 20 61 74 74 65 6d 70 74 73 20 74 68 61 loc attempts tha
4c560 74 20 77 65 72 65 20 0a 2a 2a 20 73 61 74 69 73 t were .** satis
4c570 66 69 65 64 20 75 73 69 6e 67 20 6c 6f 6f 6b 61 fied using looka
4c580 73 69 64 65 20 6d 65 6d 6f 72 79 2e 20 4f 6e 6c side memory. Onl
4c590 79 20 74 68 65 20 68 69 67 68 2d 77 61 74 65 72 y the high-water
4c5a0 20 76 61 6c 75 65 20 69 73 20 6d 65 61 6e 69 6e value is meanin
4c5b0 67 66 75 6c 3b 0a 2a 2a 20 74 68 65 20 63 75 72 gful;.** the cur
4c5c0 72 65 6e 74 20 76 61 6c 75 65 20 69 73 20 61 6c rent value is al
4c5d0 77 61 79 73 20 7a 65 72 6f 2e 29 5e 0a 2a 2a 0a ways zero.)^.**.
4c5e0 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 44 42 53 54 ** [[SQLITE_DBST
4c5f0 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 4d ATUS_LOOKASIDE_M
4c600 49 53 53 5f 53 49 5a 45 5d 5d 0a 2a 2a 20 5e 28 ISS_SIZE]].** ^(
4c610 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 <dt>SQLITE_DBSTA
4c620 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 4d 49 TUS_LOOKASIDE_MI
4c630 53 53 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 SS_SIZE</dt>.**
4c640 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 <dd>This paramet
4c650 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e er returns the n
4c660 75 6d 62 65 72 20 6d 61 6c 6c 6f 63 20 61 74 74 umber malloc att
4c670 65 6d 70 74 73 20 74 68 61 74 20 6d 69 67 68 74 empts that might
4c680 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 73 61 have.** been sa
4c690 74 69 73 66 69 65 64 20 75 73 69 6e 67 20 6c 6f tisfied using lo
4c6a0 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 62 okaside memory b
4c6b0 75 74 20 66 61 69 6c 65 64 20 64 75 65 20 74 6f ut failed due to
4c6c0 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 0a 2a the amount of.*
4c6d0 2a 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 73 74 * memory request
4c6e0 65 64 20 62 65 69 6e 67 20 6c 61 72 67 65 72 20 ed being larger
4c6f0 74 68 61 6e 20 74 68 65 20 6c 6f 6f 6b 61 73 69 than the lookasi
4c700 64 65 20 73 6c 6f 74 20 73 69 7a 65 2e 0a 2a 2a de slot size..**
4c710 20 4f 6e 6c 79 20 74 68 65 20 68 69 67 68 2d 77 Only the high-w
4c720 61 74 65 72 20 76 61 6c 75 65 20 69 73 20 6d 65 ater value is me
4c730 61 6e 69 6e 67 66 75 6c 3b 0a 2a 2a 20 74 68 65 aningful;.** the
4c740 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 69 current value i
4c750 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2e 29 5e s always zero.)^
4c760 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f .**.** [[SQLITE_
4c770 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 DBSTATUS_LOOKASI
4c780 44 45 5f 4d 49 53 53 5f 46 55 4c 4c 5d 5d 0a 2a DE_MISS_FULL]].*
4c790 2a 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 44 * ^(<dt>SQLITE_D
4c7a0 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 BSTATUS_LOOKASID
4c7b0 45 5f 4d 49 53 53 5f 46 55 4c 4c 3c 2f 64 74 3e E_MISS_FULL</dt>
4c7c0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 .** <dd>This par
4c7d0 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 ameter returns t
4c7e0 68 65 20 6e 75 6d 62 65 72 20 6d 61 6c 6c 6f 63 he number malloc
4c7f0 20 61 74 74 65 6d 70 74 73 20 74 68 61 74 20 6d attempts that m
4c800 69 67 68 74 20 68 61 76 65 0a 2a 2a 20 62 65 65 ight have.** bee
4c810 6e 20 73 61 74 69 73 66 69 65 64 20 75 73 69 6e n satisfied usin
4c820 67 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f g lookaside memo
4c830 72 79 20 62 75 74 20 66 61 69 6c 65 64 20 64 75 ry but failed du
4c840 65 20 74 6f 20 61 6c 6c 20 6c 6f 6f 6b 61 73 69 e to all lookasi
4c850 64 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 72 de.** memory alr
4c860 65 61 64 79 20 62 65 69 6e 67 20 69 6e 20 75 73 eady being in us
4c870 65 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 68 e..** Only the h
4c880 69 67 68 2d 77 61 74 65 72 20 76 61 6c 75 65 20 igh-water value
4c890 69 73 20 6d 65 61 6e 69 6e 67 66 75 6c 3b 0a 2a is meaningful;.*
4c8a0 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 * the current va
4c8b0 6c 75 65 20 69 73 20 61 6c 77 61 79 73 20 7a 65 lue is always ze
4c8c0 72 6f 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 ro.)^.**.** [[SQ
4c8d0 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 LITE_DBSTATUS_CA
4c8e0 43 48 45 5f 55 53 45 44 5d 5d 20 5e 28 3c 64 74 CHE_USED]] ^(<dt
4c8f0 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 >SQLITE_DBSTATUS
4c900 5f 43 41 43 48 45 5f 55 53 45 44 3c 2f 64 74 3e _CACHE_USED</dt>
4c910 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 .** <dd>This par
4c920 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 ameter returns t
4c930 68 65 20 61 70 70 72 6f 78 69 6d 61 74 65 20 6e he approximate n
4c940 75 6d 62 65 72 20 6f 66 20 6f 66 20 62 79 74 65 umber of of byte
4c950 73 20 6f 66 20 68 65 61 70 0a 2a 2a 20 6d 65 6d s of heap.** mem
4c960 6f 72 79 20 75 73 65 64 20 62 79 20 61 6c 6c 20 ory used by all
4c970 70 61 67 65 72 20 63 61 63 68 65 73 20 61 73 73 pager caches ass
4c980 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
4c990 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
4c9a0 74 69 6f 6e 2e 29 5e 0a 2a 2a 20 5e 54 68 65 20 tion.)^.** ^The
4c9b0 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61 highwater mark a
4c9c0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 53 ssociated with S
4c9d0 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 QLITE_DBSTATUS_C
4c9e0 41 43 48 45 5f 55 53 45 44 20 69 73 20 61 6c 77 ACHE_USED is alw
4c9f0 61 79 73 20 30 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 ays 0..**.** [[S
4ca00 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 53 QLITE_DBSTATUS_S
4ca10 43 48 45 4d 41 5f 55 53 45 44 5d 5d 20 5e 28 3c CHEMA_USED]] ^(<
4ca20 64 74 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 dt>SQLITE_DBSTAT
4ca30 55 53 5f 53 43 48 45 4d 41 5f 55 53 45 44 3c 2f US_SCHEMA_USED</
4ca40 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
4ca50 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e parameter return
4ca60 73 20 74 68 65 20 61 70 70 72 6f 78 69 6d 61 74 s the approximat
4ca70 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 66 20 62 e number of of b
4ca80 79 74 65 73 20 6f 66 20 68 65 61 70 0a 2a 2a 20 ytes of heap.**
4ca90 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 73 memory used to s
4caa0 74 6f 72 65 20 74 68 65 20 73 63 68 65 6d 61 20 tore the schema
4cab0 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 for all database
4cac0 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 s associated.**
4cad0 77 69 74 68 20 74 68 65 20 63 6f 6e 6e 65 63 74 with the connect
4cae0 69 6f 6e 20 2d 20 6d 61 69 6e 2c 20 74 65 6d 70 ion - main, temp
4caf0 2c 20 61 6e 64 20 61 6e 79 20 5b 41 54 54 41 43 , and any [ATTAC
4cb00 48 5d 2d 65 64 20 64 61 74 61 62 61 73 65 73 2e H]-ed databases.
4cb10 29 5e 20 0a 2a 2a 20 5e 54 68 65 20 66 75 6c 6c )^ .** ^The full
4cb20 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 amount of memor
4cb30 79 20 75 73 65 64 20 62 79 20 74 68 65 20 73 63 y used by the sc
4cb40 68 65 6d 61 73 20 69 73 20 72 65 70 6f 72 74 65 hemas is reporte
4cb50 64 2c 20 65 76 65 6e 20 69 66 20 74 68 65 0a 2a d, even if the.*
4cb60 2a 20 73 63 68 65 6d 61 20 6d 65 6d 6f 72 79 20 * schema memory
4cb70 69 73 20 73 68 61 72 65 64 20 77 69 74 68 20 6f is shared with o
4cb80 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f ther database co
4cb90 6e 6e 65 63 74 69 6f 6e 73 20 64 75 65 20 74 6f nnections due to
4cba0 0a 2a 2a 20 5b 73 68 61 72 65 64 20 63 61 63 68 .** [shared cach
4cbb0 65 20 6d 6f 64 65 5d 20 62 65 69 6e 67 20 65 6e e mode] being en
4cbc0 61 62 6c 65 64 2e 0a 2a 2a 20 5e 54 68 65 20 68 abled..** ^The h
4cbd0 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61 73 ighwater mark as
4cbe0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 53 51 sociated with SQ
4cbf0 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 53 43 LITE_DBSTATUS_SC
4cc00 48 45 4d 41 5f 55 53 45 44 20 69 73 20 61 6c 77 HEMA_USED is alw
4cc10 61 79 73 20 30 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 ays 0..**.** [[S
4cc20 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 53 QLITE_DBSTATUS_S
4cc30 54 4d 54 5f 55 53 45 44 5d 5d 20 5e 28 3c 64 74 TMT_USED]] ^(<dt
4cc40 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 >SQLITE_DBSTATUS
4cc50 5f 53 54 4d 54 5f 55 53 45 44 3c 2f 64 74 3e 0a _STMT_USED</dt>.
4cc60 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 ** <dd>This para
4cc70 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 meter returns th
4cc80 65 20 61 70 70 72 6f 78 69 6d 61 74 65 20 6e 75 e approximate nu
4cc90 6d 62 65 72 20 6f 66 20 6f 66 20 62 79 74 65 73 mber of of bytes
4cca0 20 6f 66 20 68 65 61 70 0a 2a 2a 20 61 6e 64 20 of heap.** and
4ccb0 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 lookaside memory
4ccc0 20 75 73 65 64 20 62 79 20 61 6c 6c 20 70 72 65 used by all pre
4ccd0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
4cce0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
4ccf0 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
4cd00 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 29 5e 0a 2a connection.)^.*
4cd10 2a 20 5e 54 68 65 20 68 69 67 68 77 61 74 65 72 * ^The highwater
4cd20 20 6d 61 72 6b 20 61 73 73 6f 63 69 61 74 65 64 mark associated
4cd30 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 42 53 with SQLITE_DBS
4cd40 54 41 54 55 53 5f 53 54 4d 54 5f 55 53 45 44 20 TATUS_STMT_USED
4cd50 69 73 20 61 6c 77 61 79 73 20 30 2e 0a 2a 2a 20 is always 0..**
4cd60 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 </dd>.**.** [[SQ
4cd70 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 LITE_DBSTATUS_CA
4cd80 43 48 45 5f 48 49 54 5d 5d 20 5e 28 3c 64 74 3e CHE_HIT]] ^(<dt>
4cd90 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f SQLITE_DBSTATUS_
4cda0 43 41 43 48 45 5f 48 49 54 3c 2f 64 74 3e 0a 2a CACHE_HIT</dt>.*
4cdb0 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d * <dd>This param
4cdc0 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 eter returns the
4cdd0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 72 number of pager
4cde0 20 63 61 63 68 65 20 68 69 74 73 20 74 68 61 74 cache hits that
4cdf0 20 68 61 76 65 0a 2a 2a 20 6f 63 63 75 72 72 65 have.** occurre
4ce00 64 2e 29 5e 20 5e 54 68 65 20 68 69 67 68 77 61 d.)^ ^The highwa
4ce10 74 65 72 20 6d 61 72 6b 20 61 73 73 6f 63 69 61 ter mark associa
4ce20 74 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f ted with SQLITE_
4ce30 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f 48 DBSTATUS_CACHE_H
4ce40 49 54 20 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 IT .** is always
4ce50 20 30 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 0..** </dd>.**.
4ce60 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 44 42 53 54 ** [[SQLITE_DBST
4ce70 41 54 55 53 5f 43 41 43 48 45 5f 4d 49 53 53 5d ATUS_CACHE_MISS]
4ce80 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 44 ] ^(<dt>SQLITE_D
4ce90 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f 4d 49 BSTATUS_CACHE_MI
4cea0 53 53 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 SS</dt>.** <dd>T
4ceb0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 his parameter re
4cec0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
4ced0 20 6f 66 20 70 61 67 65 72 20 63 61 63 68 65 20 of pager cache
4cee0 6d 69 73 73 65 73 20 74 68 61 74 20 68 61 76 65 misses that have
4cef0 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2e 29 5e 20 .** occurred.)^
4cf00 5e 54 68 65 20 68 69 67 68 77 61 74 65 72 20 6d ^The highwater m
4cf10 61 72 6b 20 61 73 73 6f 63 69 61 74 65 64 20 77 ark associated w
4cf20 69 74 68 20 53 51 4c 49 54 45 5f 44 42 53 54 41 ith SQLITE_DBSTA
4cf30 54 55 53 5f 43 41 43 48 45 5f 4d 49 53 53 20 0a TUS_CACHE_MISS .
4cf40 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 30 2e 0a ** is always 0..
4cf50 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b ** </dd>.**.** [
4cf60 5b 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 [SQLITE_DBSTATUS
4cf70 5f 43 41 43 48 45 5f 57 52 49 54 45 5d 5d 20 5e _CACHE_WRITE]] ^
4cf80 28 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42 53 54 (<dt>SQLITE_DBST
4cf90 41 54 55 53 5f 43 41 43 48 45 5f 57 52 49 54 45 ATUS_CACHE_WRITE
4cfa0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
4cfb0 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 s parameter retu
4cfc0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
4cfd0 66 20 64 69 72 74 79 20 63 61 63 68 65 20 65 6e f dirty cache en
4cfe0 74 72 69 65 73 20 74 68 61 74 20 68 61 76 65 0a tries that have.
4cff0 2a 2a 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 ** been written
4d000 74 6f 20 64 69 73 6b 2e 20 53 70 65 63 69 66 69 to disk. Specifi
4d010 63 61 6c 6c 79 2c 20 74 68 65 20 6e 75 6d 62 65 cally, the numbe
4d020 72 20 6f 66 20 70 61 67 65 73 20 77 72 69 74 74 r of pages writt
4d030 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 77 61 6c en to the.** wal
4d040 20 66 69 6c 65 20 69 6e 20 77 61 6c 20 6d 6f 64 file in wal mod
4d050 65 20 64 61 74 61 62 61 73 65 73 2c 20 6f 72 20 e databases, or
4d060 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
4d070 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 ges written to t
4d080 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 he.** database f
4d090 69 6c 65 20 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 ile in rollback
4d0a0 6d 6f 64 65 20 64 61 74 61 62 61 73 65 73 2e 20 mode databases.
4d0b0 41 6e 79 20 70 61 67 65 73 20 77 72 69 74 74 65 Any pages writte
4d0c0 6e 20 61 73 20 70 61 72 74 20 6f 66 0a 2a 2a 20 n as part of.**
4d0d0 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c transaction roll
4d0e0 62 61 63 6b 20 6f 72 20 64 61 74 61 62 61 73 65 back or database
4d0f0 20 72 65 63 6f 76 65 72 79 20 6f 70 65 72 61 74 recovery operat
4d100 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 69 6e 63 ions are not inc
4d110 6c 75 64 65 64 2e 0a 2a 2a 20 49 66 20 61 6e 20 luded..** If an
4d120 49 4f 20 6f 72 20 6f 74 68 65 72 20 65 72 72 6f IO or other erro
4d130 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 77 r occurs while w
4d140 72 69 74 69 6e 67 20 61 20 70 61 67 65 20 74 6f riting a page to
4d150 20 64 69 73 6b 2c 20 74 68 65 20 65 66 66 65 63 disk, the effec
4d160 74 0a 2a 2a 20 6f 6e 20 73 75 62 73 65 71 75 65 t.** on subseque
4d170 6e 74 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 nt SQLITE_DBSTAT
4d180 55 53 5f 43 41 43 48 45 5f 57 52 49 54 45 20 72 US_CACHE_WRITE r
4d190 65 71 75 65 73 74 73 20 69 73 20 75 6e 64 65 66 equests is undef
4d1a0 69 6e 65 64 2e 29 5e 20 5e 54 68 65 0a 2a 2a 20 ined.)^ ^The.**
4d1b0 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61 highwater mark a
4d1c0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 53 ssociated with S
4d1d0 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 QLITE_DBSTATUS_C
4d1e0 41 43 48 45 5f 57 52 49 54 45 20 69 73 20 61 6c ACHE_WRITE is al
4d1f0 77 61 79 73 20 30 2e 0a 2a 2a 20 3c 2f 64 64 3e ways 0..** </dd>
4d200 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 .** </dl>.*/.#de
4d210 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54 fine SQLITE_DBST
4d220 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 ATUS_LOOKASIDE_U
4d230 53 45 44 20 20 20 20 20 20 20 30 0a 23 64 65 66 SED 0.#def
4d240 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 ine SQLITE_DBSTA
4d250 54 55 53 5f 43 41 43 48 45 5f 55 53 45 44 20 20 TUS_CACHE_USED
4d260 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 1.#defi
4d270 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 ne SQLITE_DBSTAT
4d280 55 53 5f 53 43 48 45 4d 41 5f 55 53 45 44 20 20 US_SCHEMA_USED
4d290 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 2.#defin
4d2a0 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 e SQLITE_DBSTATU
4d2b0 53 5f 53 54 4d 54 5f 55 53 45 44 20 20 20 20 20 S_STMT_USED
4d2c0 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 3.#define
4d2d0 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 SQLITE_DBSTATUS
4d2e0 5f 4c 4f 4f 4b 41 53 49 44 45 5f 48 49 54 20 20 _LOOKASIDE_HIT
4d2f0 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4.#define
4d300 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f SQLITE_DBSTATUS_
4d310 4c 4f 4f 4b 41 53 49 44 45 5f 4d 49 53 53 5f 53 LOOKASIDE_MISS_S
4d320 49 5a 45 20 20 35 0a 23 64 65 66 69 6e 65 20 53 IZE 5.#define S
4d330 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c QLITE_DBSTATUS_L
4d340 4f 4f 4b 41 53 49 44 45 5f 4d 49 53 53 5f 46 55 OOKASIDE_MISS_FU
4d350 4c 4c 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51 LL 6.#define SQ
4d360 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 LITE_DBSTATUS_CA
4d370 43 48 45 5f 48 49 54 20 20 20 20 20 20 20 20 20 CHE_HIT
4d380 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 7.#define SQL
4d390 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43 ITE_DBSTATUS_CAC
4d3a0 48 45 5f 4d 49 53 53 20 20 20 20 20 20 20 20 20 HE_MISS
4d3b0 20 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8.#define SQLI
4d3c0 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43 48 TE_DBSTATUS_CACH
4d3d0 45 5f 57 52 49 54 45 20 20 20 20 20 20 20 20 20 E_WRITE
4d3e0 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 9.#define SQLIT
4d3f0 45 5f 44 42 53 54 41 54 55 53 5f 4d 41 58 20 20 E_DBSTATUS_MAX
4d400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d410 39 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 64 9 /* Largest d
4d420 65 66 69 6e 65 64 20 44 42 53 54 41 54 55 53 20 efined DBSTATUS
4d430 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 */.../*.** CAPI3
4d440 52 45 46 3a 20 50 72 65 70 61 72 65 64 20 53 74 REF: Prepared St
4d450 61 74 65 6d 65 6e 74 20 53 74 61 74 75 73 0a 2a atement Status.*
4d460 2a 0a 2a 2a 20 5e 28 45 61 63 68 20 70 72 65 70 *.** ^(Each prep
4d470 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d ared statement m
4d480 61 69 6e 74 61 69 6e 73 20 76 61 72 69 6f 75 73 aintains various
4d490 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 54 4d 54 .** [SQLITE_STMT
4d4a0 53 54 41 54 55 53 20 63 6f 75 6e 74 65 72 73 5d STATUS counters]
4d4b0 20 74 68 61 74 20 6d 65 61 73 75 72 65 20 74 68 that measure th
4d4c0 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 74 e number.** of t
4d4d0 69 6d 65 73 20 69 74 20 68 61 73 20 70 65 72 66 imes it has perf
4d4e0 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63 20 6f ormed specific o
4d4f0 70 65 72 61 74 69 6f 6e 73 2e 29 5e 20 20 54 68 perations.)^ Th
4d500 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63 61 6e ese counters can
4d510 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 6d .** be used to m
4d520 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 72 66 6f onitor the perfo
4d530 72 6d 61 6e 63 65 20 63 68 61 72 61 63 74 65 72 rmance character
4d540 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 70 72 istics of the pr
4d550 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d epared.** statem
4d560 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 ents. For examp
4d570 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 65 le, if the numbe
4d580 72 20 6f 66 20 74 61 62 6c 65 20 73 74 65 70 73 r of table steps
4d590 20 67 72 65 61 74 6c 79 20 65 78 63 65 65 64 73 greatly exceeds
4d5a0 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f .** the number o
4d5b0 66 20 74 61 62 6c 65 20 73 65 61 72 63 68 65 73 f table searches
4d5c0 20 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 73 2c or result rows,
4d5d0 20 74 68 61 74 20 77 6f 75 6c 64 20 74 65 6e 64 that would tend
4d5e0 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 to indicate.**
4d5f0 74 68 61 74 20 74 68 65 20 70 72 65 70 61 72 65 that the prepare
4d600 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 75 d statement is u
4d610 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 6c sing a full tabl
4d620 65 20 73 63 61 6e 20 72 61 74 68 65 72 20 74 68 e scan rather th
4d630 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 an.** an index.
4d640 20 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 69 73 20 69 .**.** ^(This i
4d650 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 nterface is used
4d660 20 74 6f 20 72 65 74 72 69 65 76 65 20 61 6e 64 to retrieve and
4d670 20 72 65 73 65 74 20 63 6f 75 6e 74 65 72 20 76 reset counter v
4d680 61 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 alues from.** a
4d690 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
4d6a0 65 6e 74 5d 2e 20 20 54 68 65 20 66 69 72 73 74 ent]. The first
4d6b0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
4d6c0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
4d6d0 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f ent.** object to
4d6e0 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 be interrogated
4d6f0 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 . The second ar
4d700 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 gument.** is an
4d710 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72 integer code for
4d720 20 61 20 73 70 65 63 69 66 69 63 20 5b 53 51 4c a specific [SQL
4d730 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 20 63 ITE_STMTSTATUS c
4d740 6f 75 6e 74 65 72 5d 0a 2a 2a 20 74 6f 20 62 65 ounter].** to be
4d750 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 29 5e interrogated.)^
4d760 0a 2a 2a 20 5e 54 68 65 20 63 75 72 72 65 6e 74 .** ^The current
4d770 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 value of the re
4d780 71 75 65 73 74 65 64 20 63 6f 75 6e 74 65 72 20 quested counter
4d790 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 is returned..**
4d7a0 5e 49 66 20 74 68 65 20 72 65 73 65 74 46 6c 67 ^If the resetFlg
4d7b0 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
4d7c0 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 72 65 he counter is re
4d7d0 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 set to zero afte
4d7e0 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 r this.** interf
4d7f0 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 ace call returns
4d800 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
4d810 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 : [sqlite3_statu
4d820 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 s()] and [sqlite
4d830 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e 0a 3_db_status()]..
4d840 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
4d850 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 t sqlite3_stmt_s
4d860 74 61 74 75 73 28 73 71 6c 69 74 65 33 5f 73 74 tatus(sqlite3_st
4d870 6d 74 2a 2c 20 69 6e 74 20 6f 70 2c 69 6e 74 20 mt*, int op,int
4d880 72 65 73 65 74 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a resetFlg);../*.*
4d890 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 61 74 * CAPI3REF: Stat
4d8a0 75 73 20 50 61 72 61 6d 65 74 65 72 73 20 66 6f us Parameters fo
4d8b0 72 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 r prepared state
4d8c0 6d 65 6e 74 73 0a 2a 2a 20 4b 45 59 57 4f 52 44 ments.** KEYWORD
4d8d0 53 3a 20 7b 53 51 4c 49 54 45 5f 53 54 4d 54 53 S: {SQLITE_STMTS
4d8e0 54 41 54 55 53 20 63 6f 75 6e 74 65 72 7d 20 7b TATUS counter} {
4d8f0 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 SQLITE_STMTSTATU
4d900 53 20 63 6f 75 6e 74 65 72 73 7d 0a 2a 2a 0a 2a S counters}.**.*
4d910 2a 20 54 68 65 73 65 20 70 72 65 70 72 6f 63 65 * These preproce
4d920 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 66 69 ssor macros defi
4d930 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 ne integer codes
4d940 20 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 6e 74 that name count
4d950 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 73 73 er.** values ass
4d960 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
4d970 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 [sqlite3_stmt_s
4d980 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61 tatus()] interfa
4d990 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 6e 69 ce..** The meani
4d9a0 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72 69 6f ngs of the vario
4d9b0 75 73 20 63 6f 75 6e 74 65 72 73 20 61 72 65 20 us counters are
4d9c0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
4d9d0 2a 20 3c 64 6c 3e 0a 2a 2a 20 5b 5b 53 51 4c 49 * <dl>.** [[SQLI
4d9e0 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55 TE_STMTSTATUS_FU
4d9f0 4c 4c 53 43 41 4e 5f 53 54 45 50 5d 5d 20 3c 64 LLSCAN_STEP]] <d
4da00 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 t>SQLITE_STMTSTA
4da10 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 TUS_FULLSCAN_STE
4da20 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 5e 54 P</dt>.** <dd>^T
4da30 68 69 73 20 69 73 20 74 68 65 20 6e 75 6d 62 65 his is the numbe
4da40 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 r of times that
4da50 53 51 4c 69 74 65 20 68 61 73 20 73 74 65 70 70 SQLite has stepp
4da60 65 64 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a ed forward in.**
4da70 20 61 20 74 61 62 6c 65 20 61 73 20 70 61 72 74 a table as part
4da80 20 6f 66 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 of a full table
4da90 20 73 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75 scan. Large nu
4daa0 6d 62 65 72 73 20 66 6f 72 20 74 68 69 73 20 63 mbers for this c
4dab0 6f 75 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e ounter.** may in
4dac0 64 69 63 61 74 65 20 6f 70 70 6f 72 74 75 6e 69 dicate opportuni
4dad0 74 69 65 73 20 66 6f 72 20 70 65 72 66 6f 72 6d ties for perform
4dae0 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 ance improvement
4daf0 20 74 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72 through .** car
4db00 65 66 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 eful use of indi
4db10 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ces.</dd>.**.**
4db20 5b 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 [[SQLITE_STMTSTA
4db30 54 55 53 5f 53 4f 52 54 5d 5d 20 3c 64 74 3e 53 TUS_SORT]] <dt>S
4db40 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 QLITE_STMTSTATUS
4db50 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 _SORT</dt>.** <d
4db60 64 3e 5e 54 68 69 73 20 69 73 20 74 68 65 20 6e d>^This is the n
4db70 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 6f 70 umber of sort op
4db80 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20 68 61 erations that ha
4db90 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 ve occurred..**
4dba0 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 A non-zero value
4dbb0 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 72 in this counter
4dbc0 20 6d 61 79 20 69 6e 64 69 63 61 74 65 20 61 6e may indicate an
4dbd0 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 0a opportunity to.
4dbe0 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 70 ** improvement p
4dbf0 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f 75 erformance throu
4dc00 67 68 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f gh careful use o
4dc10 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a f indices.</dd>.
4dc20 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53 **.** [[SQLITE_S
4dc30 54 4d 54 53 54 41 54 55 53 5f 41 55 54 4f 49 4e TMTSTATUS_AUTOIN
4dc40 44 45 58 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45 DEX]] <dt>SQLITE
4dc50 5f 53 54 4d 54 53 54 41 54 55 53 5f 41 55 54 4f _STMTSTATUS_AUTO
4dc60 49 4e 44 45 58 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 INDEX</dt>.** <d
4dc70 64 3e 5e 54 68 69 73 20 69 73 20 74 68 65 20 6e d>^This is the n
4dc80 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e umber of rows in
4dc90 73 65 72 74 65 64 20 69 6e 74 6f 20 74 72 61 6e serted into tran
4dca0 73 69 65 6e 74 20 69 6e 64 69 63 65 73 20 74 68 sient indices th
4dcb0 61 74 0a 2a 2a 20 77 65 72 65 20 63 72 65 61 74 at.** were creat
4dcc0 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ed automatically
4dcd0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 68 65 6c in order to hel
4dce0 70 20 6a 6f 69 6e 73 20 72 75 6e 20 66 61 73 74 p joins run fast
4dcf0 65 72 2e 0a 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 72 er..** A non-zer
4dd00 6f 20 76 61 6c 75 65 20 69 6e 20 74 68 69 73 20 o value in this
4dd10 63 6f 75 6e 74 65 72 20 6d 61 79 20 69 6e 64 69 counter may indi
4dd20 63 61 74 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e cate an opportun
4dd30 69 74 79 20 74 6f 0a 2a 2a 20 69 6d 70 72 6f 76 ity to.** improv
4dd40 65 6d 65 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 ement performanc
4dd50 65 20 62 79 20 61 64 64 69 6e 67 20 70 65 72 6d e by adding perm
4dd60 61 6e 65 6e 74 20 69 6e 64 69 63 65 73 20 74 68 anent indices th
4dd70 61 74 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e 65 65 at do not.** nee
4dd80 64 20 74 6f 20 62 65 20 72 65 69 6e 69 74 69 61 d to be reinitia
4dd90 6c 69 7a 65 64 20 65 61 63 68 20 74 69 6d 65 20 lized each time
4dda0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 the statement is
4ddb0 20 72 75 6e 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f run.</dd>.** </
4ddc0 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 dl>.*/.#define S
4ddd0 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 QLITE_STMTSTATUS
4dde0 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 20 20 _FULLSCAN_STEP
4ddf0 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 1.#define SQL
4de00 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 ITE_STMTSTATUS_S
4de10 4f 52 54 20 20 20 20 20 20 20 20 20 20 20 20 20 ORT
4de20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 2.#define SQLIT
4de30 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 41 55 54 E_STMTSTATUS_AUT
4de40 4f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 33 OINDEX 3
4de50 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
4de60 3a 20 43 75 73 74 6f 6d 20 50 61 67 65 20 43 61 : Custom Page Ca
4de70 63 68 65 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a che Object.**.**
4de80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 The sqlite3_pca
4de90 63 68 65 20 74 79 70 65 20 69 73 20 6f 70 61 71 che type is opaq
4dea0 75 65 2e 20 20 49 74 20 69 73 20 69 6d 70 6c 65 ue. It is imple
4deb0 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 mented by.** the
4dec0 20 70 6c 75 67 67 61 62 6c 65 20 6d 6f 64 75 6c pluggable modul
4ded0 65 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 e. The SQLite c
4dee0 6f 72 65 20 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c ore has no knowl
4def0 65 64 67 65 20 6f 66 0a 2a 2a 20 69 74 73 20 73 edge of.** its s
4df00 69 7a 65 20 6f 72 20 69 6e 74 65 72 6e 61 6c 20 ize or internal
4df10 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 6e 65 structure and ne
4df20 76 65 72 20 64 65 61 6c 73 20 77 69 74 68 20 74 ver deals with t
4df30 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 he.** sqlite3_pc
4df40 61 63 68 65 20 6f 62 6a 65 63 74 20 65 78 63 65 ache object exce
4df50 70 74 20 62 79 20 68 6f 6c 64 69 6e 67 20 61 6e pt by holding an
4df60 64 20 70 61 73 73 69 6e 67 20 70 6f 69 6e 74 65 d passing pointe
4df70 72 73 0a 2a 2a 20 74 6f 20 74 68 65 20 6f 62 6a rs.** to the obj
4df80 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b ect..**.** See [
4df90 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d sqlite3_pcache_m
4dfa0 65 74 68 6f 64 73 32 5d 20 66 6f 72 20 61 64 64 ethods2] for add
4dfb0 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
4dfc0 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ion..*/.typedef
4dfd0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 struct sqlite3_p
4dfe0 63 61 63 68 65 20 73 71 6c 69 74 65 33 5f 70 63 cache sqlite3_pc
4dff0 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ache;../*.** CAP
4e000 49 33 52 45 46 3a 20 43 75 73 74 6f 6d 20 50 61 I3REF: Custom Pa
4e010 67 65 20 43 61 63 68 65 20 4f 62 6a 65 63 74 0a ge Cache Object.
4e020 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
4e030 33 5f 70 63 61 63 68 65 5f 70 61 67 65 20 6f 62 3_pcache_page ob
4e040 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 ject represents
4e050 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 69 6e a single page in
4e060 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 63 61 63 the.** page cac
4e070 68 65 2e 20 20 54 68 65 20 70 61 67 65 20 63 61 he. The page ca
4e080 63 68 65 20 77 69 6c 6c 20 61 6c 6c 6f 63 61 74 che will allocat
4e090 65 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 e instances of t
4e0a0 68 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 his.** object.
4e0b0 56 61 72 69 6f 75 73 20 6d 65 74 68 6f 64 73 20 Various methods
4e0c0 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 of the page cach
4e0d0 65 20 75 73 65 20 70 6f 69 6e 74 65 72 73 20 74 e use pointers t
4e0e0 6f 20 69 6e 73 74 61 6e 63 65 73 0a 2a 2a 20 6f o instances.** o
4e0f0 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 61 73 f this object as
4e100 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 61 parameters or a
4e110 73 20 74 68 65 69 72 20 72 65 74 75 72 6e 20 76 s their return v
4e120 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 alue..**.** See
4e130 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f [sqlite3_pcache_
4e140 6d 65 74 68 6f 64 73 32 5d 20 66 6f 72 20 61 64 methods2] for ad
4e150 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
4e160 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 tion..*/.typedef
4e170 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
4e180 70 63 61 63 68 65 5f 70 61 67 65 20 73 71 6c 69 pcache_page sqli
4e190 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67 65 3b te3_pcache_page;
4e1a0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
4e1b0 70 63 61 63 68 65 5f 70 61 67 65 20 7b 0a 20 20 pcache_page {.
4e1c0 76 6f 69 64 20 2a 70 42 75 66 3b 20 20 20 20 20 void *pBuf;
4e1d0 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e /* The conten
4e1e0 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f t of the page */
4e1f0 0a 20 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b . void *pExtra;
4e200 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 69 /* Extra i
4e210 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 73 6f 63 nformation assoc
4e220 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 70 iated with the p
4e230 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a age */.};../*.**
4e240 20 43 41 50 49 33 52 45 46 3a 20 41 70 70 6c 69 CAPI3REF: Appli
4e250 63 61 74 69 6f 6e 20 44 65 66 69 6e 65 64 20 50 cation Defined P
4e260 61 67 65 20 43 61 63 68 65 2e 0a 2a 2a 20 4b 45 age Cache..** KE
4e270 59 57 4f 52 44 53 3a 20 7b 70 61 67 65 20 63 61 YWORDS: {page ca
4e280 63 68 65 7d 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 che}.**.** ^(The
4e290 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 [sqlite3_config
4e2a0 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ]([SQLITE_CONFIG
4e2b0 5f 50 43 41 43 48 45 32 5d 2c 20 2e 2e 2e 29 20 _PCACHE2], ...)
4e2c0 69 6e 74 65 72 66 61 63 65 20 63 61 6e 0a 2a 2a interface can.**
4e2d0 20 72 65 67 69 73 74 65 72 20 61 6e 20 61 6c 74 register an alt
4e2e0 65 72 6e 61 74 69 76 65 20 70 61 67 65 20 63 61 ernative page ca
4e2f0 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 che implementati
4e300 6f 6e 20 62 79 20 70 61 73 73 69 6e 67 20 69 6e on by passing in
4e310 20 61 6e 20 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 an .** instance
4e320 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
4e330 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 20 pcache_methods2
4e340 73 74 72 75 63 74 75 72 65 2e 29 5e 0a 2a 2a 20 structure.)^.**
4e350 49 6e 20 6d 61 6e 79 20 61 70 70 6c 69 63 61 74 In many applicat
4e360 69 6f 6e 73 2c 20 6d 6f 73 74 20 6f 66 20 74 68 ions, most of th
4e370 65 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 61 6c e heap memory al
4e380 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 53 located by .** S
4e390 51 4c 69 74 65 20 69 73 20 75 73 65 64 20 66 6f QLite is used fo
4e3a0 72 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 r the page cache
4e3b0 2e 0a 2a 2a 20 42 79 20 69 6d 70 6c 65 6d 65 6e ..** By implemen
4e3c0 74 69 6e 67 20 61 20 0a 2a 2a 20 63 75 73 74 6f ting a .** custo
4e3d0 6d 20 70 61 67 65 20 63 61 63 68 65 20 75 73 69 m page cache usi
4e3e0 6e 67 20 74 68 69 73 20 41 50 49 2c 20 61 6e 20 ng this API, an
4e3f0 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20 application can
4e400 62 65 74 74 65 72 20 63 6f 6e 74 72 6f 6c 0a 2a better control.*
4e410 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 * the amount of
4e420 6d 65 6d 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20 memory consumed
4e430 62 79 20 53 51 4c 69 74 65 2c 20 74 68 65 20 77 by SQLite, the w
4e440 61 79 20 69 6e 20 77 68 69 63 68 20 0a 2a 2a 20 ay in which .**
4e450 74 68 61 74 20 6d 65 6d 6f 72 79 20 69 73 20 61 that memory is a
4e460 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 72 65 6c llocated and rel
4e470 65 61 73 65 64 2c 20 61 6e 64 20 74 68 65 20 70 eased, and the p
4e480 6f 6c 69 63 69 65 73 20 75 73 65 64 20 74 6f 20 olicies used to
4e490 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 65 78 .** determine ex
4e4a0 61 63 74 6c 79 20 77 68 69 63 68 20 70 61 72 74 actly which part
4e4b0 73 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 s of a database
4e4c0 66 69 6c 65 20 61 72 65 20 63 61 63 68 65 64 20 file are cached
4e4d0 61 6e 64 20 66 6f 72 20 0a 2a 2a 20 68 6f 77 20 and for .** how
4e4e0 6c 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 long..**.** The
4e4f0 61 6c 74 65 72 6e 61 74 69 76 65 20 70 61 67 65 alternative page
4e500 20 63 61 63 68 65 20 6d 65 63 68 61 6e 69 73 6d cache mechanism
4e510 20 69 73 20 61 6e 0a 2a 2a 20 65 78 74 72 65 6d is an.** extrem
4e520 65 20 6d 65 61 73 75 72 65 20 74 68 61 74 20 69 e measure that i
4e530 73 20 6f 6e 6c 79 20 6e 65 65 64 65 64 20 62 79 s only needed by
4e540 20 74 68 65 20 6d 6f 73 74 20 64 65 6d 61 6e 64 the most demand
4e550 69 6e 67 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 ing applications
4e560 2e 0a 2a 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 ..** The built-i
4e570 6e 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 n page cache is
4e580 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 recommended for
4e590 6d 6f 73 74 20 75 73 65 73 2e 0a 2a 2a 0a 2a 2a most uses..**.**
4e5a0 20 5e 28 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 ^(The contents
4e5b0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 of the sqlite3_p
4e5c0 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 20 73 cache_methods2 s
4e5d0 74 72 75 63 74 75 72 65 20 61 72 65 20 63 6f 70 tructure are cop
4e5e0 69 65 64 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 74 ied to an.** int
4e5f0 65 72 6e 61 6c 20 62 75 66 66 65 72 20 62 79 20 ernal buffer by
4e600 53 51 4c 69 74 65 20 77 69 74 68 69 6e 20 74 68 SQLite within th
4e610 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 e call to [sqlit
4e620 65 33 5f 63 6f 6e 66 69 67 5d 2e 20 20 48 65 6e e3_config]. Hen
4e630 63 65 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 ce.** the applic
4e640 61 74 69 6f 6e 20 6d 61 79 20 64 69 73 63 61 72 ation may discar
4e650 64 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 d the parameter
4e660 61 66 74 65 72 20 74 68 65 20 63 61 6c 6c 20 74 after the call t
4e670 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f o.** [sqlite3_co
4e680 6e 66 69 67 28 29 5d 20 72 65 74 75 72 6e 73 2e nfig()] returns.
4e690 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 74 68 65 20 78 )^.**.** [[the x
4e6a0 49 6e 69 74 28 29 20 70 61 67 65 20 63 61 63 68 Init() page cach
4e6b0 65 20 6d 65 74 68 6f 64 5d 5d 0a 2a 2a 20 5e 28 e method]].** ^(
4e6c0 54 68 65 20 78 49 6e 69 74 28 29 20 6d 65 74 68 The xInit() meth
4e6d0 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 od is called onc
4e6e0 65 20 66 6f 72 20 65 61 63 68 20 65 66 66 65 63 e for each effec
4e6f0 74 69 76 65 20 0a 2a 2a 20 63 61 6c 6c 20 74 6f tive .** call to
4e700 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 [sqlite3_initia
4e710 6c 69 7a 65 28 29 5d 29 5e 0a 2a 2a 20 28 75 73 lize()])^.** (us
4e720 75 61 6c 6c 79 20 6f 6e 6c 79 20 6f 6e 63 65 20 ually only once
4e730 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 during the lifet
4e740 69 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 63 65 ime of the proce
4e750 73 73 29 2e 20 5e 28 54 68 65 20 78 49 6e 69 74 ss). ^(The xInit
4e760 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 69 73 20 ().** method is
4e770 70 61 73 73 65 64 20 61 20 63 6f 70 79 20 6f 66 passed a copy of
4e780 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
4e790 63 68 65 5f 6d 65 74 68 6f 64 73 32 2e 70 41 72 che_methods2.pAr
4e7a0 67 20 76 61 6c 75 65 2e 29 5e 0a 2a 2a 20 54 68 g value.)^.** Th
4e7b0 65 20 69 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 e intent of the
4e7c0 78 49 6e 69 74 28 29 20 6d 65 74 68 6f 64 20 69 xInit() method i
4e7d0 73 20 74 6f 20 73 65 74 20 75 70 20 67 6c 6f 62 s to set up glob
4e7e0 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 72 al data structur
4e7f0 65 73 20 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 es .** required
4e800 62 79 20 74 68 65 20 63 75 73 74 6f 6d 20 70 61 by the custom pa
4e810 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 ge cache impleme
4e820 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 20 5e 28 49 ntation. .** ^(I
4e830 66 20 74 68 65 20 78 49 6e 69 74 28 29 20 6d 65 f the xInit() me
4e840 74 68 6f 64 20 69 73 20 4e 55 4c 4c 2c 20 74 68 thod is NULL, th
4e850 65 6e 20 74 68 65 20 0a 2a 2a 20 62 75 69 6c 74 en the .** built
4e860 2d 69 6e 20 64 65 66 61 75 6c 74 20 70 61 67 65 -in default page
4e870 20 63 61 63 68 65 20 69 73 20 75 73 65 64 20 69 cache is used i
4e880 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 61 70 nstead of the ap
4e890 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 plication define
4e8a0 64 0a 2a 2a 20 70 61 67 65 20 63 61 63 68 65 2e d.** page cache.
4e8b0 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 74 68 65 20 78 )^.**.** [[the x
4e8c0 53 68 75 74 64 6f 77 6e 28 29 20 70 61 67 65 20 Shutdown() page
4e8d0 63 61 63 68 65 20 6d 65 74 68 6f 64 5d 5d 0a 2a cache method]].*
4e8e0 2a 20 5e 54 68 65 20 78 53 68 75 74 64 6f 77 6e * ^The xShutdown
4e8f0 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c () method is cal
4e900 6c 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f led by [sqlite3_
4e910 73 68 75 74 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 20 shutdown()]..**
4e920 49 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 It can be used t
4e930 6f 20 63 6c 65 61 6e 20 75 70 20 0a 2a 2a 20 61 o clean up .** a
4e940 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 ny outstanding r
4e950 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72 65 20 esources before
4e960 70 72 6f 63 65 73 73 20 73 68 75 74 64 6f 77 6e process shutdown
4e970 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a , if required..*
4e980 2a 20 5e 54 68 65 20 78 53 68 75 74 64 6f 77 6e * ^The xShutdown
4e990 28 29 20 6d 65 74 68 6f 64 20 6d 61 79 20 62 65 () method may be
4e9a0 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 5e 53 51 NULL..**.** ^SQ
4e9b0 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61 6c Lite automatical
4e9c0 6c 79 20 73 65 72 69 61 6c 69 7a 65 73 20 63 61 ly serializes ca
4e9d0 6c 6c 73 20 74 6f 20 74 68 65 20 78 49 6e 69 74 lls to the xInit
4e9e0 20 6d 65 74 68 6f 64 2c 0a 2a 2a 20 73 6f 20 74 method,.** so t
4e9f0 68 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 he xInit method
4ea00 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65 need not be thre
4ea10 61 64 73 61 66 65 2e 20 20 5e 54 68 65 0a 2a 2a adsafe. ^The.**
4ea20 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f xShutdown metho
4ea30 64 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 d is only called
4ea40 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 73 from [sqlite3_s
4ea50 68 75 74 64 6f 77 6e 28 29 5d 20 73 6f 20 69 74 hutdown()] so it
4ea60 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 does.** not nee
4ea70 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 61 d to be threadsa
4ea80 66 65 20 65 69 74 68 65 72 2e 20 20 41 6c 6c 20 fe either. All
4ea90 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 20 6d 75 other methods mu
4eaa0 73 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65 st be threadsafe
4eab0 0a 2a 2a 20 69 6e 20 6d 75 6c 74 69 74 68 72 65 .** in multithre
4eac0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e aded application
4ead0 73 2e 0a 2a 2a 0a 2a 2a 20 5e 53 51 4c 69 74 65 s..**.** ^SQLite
4eae0 20 77 69 6c 6c 20 6e 65 76 65 72 20 69 6e 76 6f will never invo
4eaf0 6b 65 20 78 49 6e 69 74 28 29 20 6d 6f 72 65 20 ke xInit() more
4eb00 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 6f 75 than once withou
4eb10 74 20 61 6e 20 69 6e 74 65 72 76 65 6e 69 6e 67 t an intervening
4eb20 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 53 68 75 .** call to xShu
4eb30 74 64 6f 77 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b tdown()..**.** [
4eb40 5b 74 68 65 20 78 43 72 65 61 74 65 28 29 20 70 [the xCreate() p
4eb50 61 67 65 20 63 61 63 68 65 20 6d 65 74 68 6f 64 age cache method
4eb60 73 5d 5d 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 69 s]].** ^SQLite i
4eb70 6e 76 6f 6b 65 73 20 74 68 65 20 78 43 72 65 61 nvokes the xCrea
4eb80 74 65 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 63 te() method to c
4eb90 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20 63 onstruct a new c
4eba0 61 63 68 65 20 69 6e 73 74 61 6e 63 65 2e 0a 2a ache instance..*
4ebb0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 74 79 * SQLite will ty
4ebc0 70 69 63 61 6c 6c 79 20 63 72 65 61 74 65 20 6f pically create o
4ebd0 6e 65 20 63 61 63 68 65 20 69 6e 73 74 61 6e 63 ne cache instanc
4ebe0 65 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 e for each open
4ebf0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a database file,.*
4ec00 2a 20 74 68 6f 75 67 68 20 74 68 69 73 20 69 73 * though this is
4ec10 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 2e not guaranteed.
4ec20 20 5e 54 68 65 0a 2a 2a 20 66 69 72 73 74 20 70 ^The.** first p
4ec30 61 72 61 6d 65 74 65 72 2c 20 73 7a 50 61 67 65 arameter, szPage
4ec40 2c 20 69 73 20 74 68 65 20 73 69 7a 65 20 69 6e , is the size in
4ec50 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 bytes of the pa
4ec60 67 65 73 20 74 68 61 74 20 6d 75 73 74 0a 2a 2a ges that must.**
4ec70 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 be allocated by
4ec80 20 74 68 65 20 63 61 63 68 65 2e 20 20 5e 73 7a the cache. ^sz
4ec90 50 61 67 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 Page will always
4eca0 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e a power of two.
4ecb0 20 20 5e 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 ^The.** second
4ecc0 20 70 61 72 61 6d 65 74 65 72 20 73 7a 45 78 74 parameter szExt
4ecd0 72 61 20 69 73 20 61 20 6e 75 6d 62 65 72 20 6f ra is a number o
4ece0 66 20 62 79 74 65 73 20 6f 66 20 65 78 74 72 61 f bytes of extra
4ecf0 20 73 74 6f 72 61 67 65 20 0a 2a 2a 20 61 73 73 storage .** ass
4ed00 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 ociated with eac
4ed10 68 20 70 61 67 65 20 63 61 63 68 65 20 65 6e 74 h page cache ent
4ed20 72 79 2e 20 20 5e 54 68 65 20 73 7a 45 78 74 72 ry. ^The szExtr
4ed30 61 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c a parameter will
4ed40 0a 2a 2a 20 61 20 6e 75 6d 62 65 72 20 6c 65 73 .** a number les
4ed50 73 20 74 68 61 6e 20 32 35 30 2e 20 20 53 51 4c s than 250. SQL
4ed60 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 ite will use the
4ed70 0a 2a 2a 20 65 78 74 72 61 20 73 7a 45 78 74 72 .** extra szExtr
4ed80 61 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 a bytes on each
4ed90 70 61 67 65 20 74 6f 20 73 74 6f 72 65 20 6d 65 page to store me
4eda0 74 61 64 61 74 61 20 61 62 6f 75 74 20 74 68 65 tadata about the
4edb0 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 64 underlying.** d
4edc0 61 74 61 62 61 73 65 20 70 61 67 65 20 6f 6e 20 atabase page on
4edd0 64 69 73 6b 2e 20 20 54 68 65 20 76 61 6c 75 65 disk. The value
4ede0 20 70 61 73 73 65 64 20 69 6e 74 6f 20 73 7a 45 passed into szE
4edf0 78 74 72 61 20 64 65 70 65 6e 64 73 0a 2a 2a 20 xtra depends.**
4ee00 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20 76 65 on the SQLite ve
4ee10 72 73 69 6f 6e 2c 20 74 68 65 20 74 61 72 67 65 rsion, the targe
4ee20 74 20 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64 20 t platform, and
4ee30 68 6f 77 20 53 51 4c 69 74 65 20 77 61 73 20 63 how SQLite was c
4ee40 6f 6d 70 69 6c 65 64 2e 0a 2a 2a 20 5e 54 68 65 ompiled..** ^The
4ee50 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 third argument
4ee60 74 6f 20 78 43 72 65 61 74 65 28 29 2c 20 62 50 to xCreate(), bP
4ee70 75 72 67 65 61 62 6c 65 2c 20 69 73 20 74 72 75 urgeable, is tru
4ee80 65 20 69 66 20 74 68 65 20 63 61 63 68 65 20 62 e if the cache b
4ee90 65 69 6e 67 0a 2a 2a 20 63 72 65 61 74 65 64 20 eing.** created
4eea0 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20 will be used to
4eeb0 63 61 63 68 65 20 64 61 74 61 62 61 73 65 20 70 cache database p
4eec0 61 67 65 73 20 6f 66 20 61 20 66 69 6c 65 20 73 ages of a file s
4eed0 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 2c 20 6f tored on disk, o
4eee0 72 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74 r.** false if it
4eef0 20 69 73 20 75 73 65 64 20 66 6f 72 20 61 6e 20 is used for an
4ef00 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
4ef10 73 65 2e 20 54 68 65 20 63 61 63 68 65 20 69 6d se. The cache im
4ef20 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 plementation.**
4ef30 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f does not have to
4ef40 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 73 70 65 do anything spe
4ef50 63 69 61 6c 20 62 61 73 65 64 20 77 69 74 68 20 cial based with
4ef60 74 68 65 20 76 61 6c 75 65 20 6f 66 20 62 50 75 the value of bPu
4ef70 72 67 65 61 62 6c 65 3b 0a 2a 2a 20 69 74 20 69 rgeable;.** it i
4ef80 73 20 70 75 72 65 6c 79 20 61 64 76 69 73 6f 72 s purely advisor
4ef90 79 2e 20 20 5e 4f 6e 20 61 20 63 61 63 68 65 20 y. ^On a cache
4efa0 77 68 65 72 65 20 62 50 75 72 67 65 61 62 6c 65 where bPurgeable
4efb0 20 69 73 20 66 61 6c 73 65 2c 20 53 51 4c 69 74 is false, SQLit
4efc0 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 e will.** never
4efd0 69 6e 76 6f 6b 65 20 78 55 6e 70 69 6e 28 29 20 invoke xUnpin()
4efe0 65 78 63 65 70 74 20 74 6f 20 64 65 6c 69 62 65 except to delibe
4eff0 72 61 74 65 6c 79 20 64 65 6c 65 74 65 20 61 20 rately delete a
4f000 70 61 67 65 2e 0a 2a 2a 20 5e 49 6e 20 6f 74 68 page..** ^In oth
4f010 65 72 20 77 6f 72 64 73 2c 20 63 61 6c 6c 73 20 er words, calls
4f020 74 6f 20 78 55 6e 70 69 6e 28 29 20 6f 6e 20 61 to xUnpin() on a
4f030 20 63 61 63 68 65 20 77 69 74 68 20 62 50 75 72 cache with bPur
4f040 67 65 61 62 6c 65 20 73 65 74 20 74 6f 0a 2a 2a geable set to.**
4f050 20 66 61 6c 73 65 20 77 69 6c 6c 20 61 6c 77 61 false will alwa
4f060 79 73 20 68 61 76 65 20 74 68 65 20 22 64 69 73 ys have the "dis
4f070 63 61 72 64 22 20 66 6c 61 67 20 73 65 74 20 74 card" flag set t
4f080 6f 20 74 72 75 65 2e 20 20 0a 2a 2a 20 5e 48 65 o true. .** ^He
4f090 6e 63 65 2c 20 61 20 63 61 63 68 65 20 63 72 65 nce, a cache cre
4f0a0 61 74 65 64 20 77 69 74 68 20 62 50 75 72 67 65 ated with bPurge
4f0b0 61 62 6c 65 20 66 61 6c 73 65 20 77 69 6c 6c 0a able false will.
4f0c0 2a 2a 20 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e ** never contain
4f0d0 20 61 6e 79 20 75 6e 70 69 6e 6e 65 64 20 70 61 any unpinned pa
4f0e0 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 74 68 65 ges..**.** [[the
4f0f0 20 78 43 61 63 68 65 73 69 7a 65 28 29 20 70 61 xCachesize() pa
4f100 67 65 20 63 61 63 68 65 20 6d 65 74 68 6f 64 5d ge cache method]
4f110 5d 0a 2a 2a 20 5e 28 54 68 65 20 78 43 61 63 68 ].** ^(The xCach
4f120 65 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 6d esize() method m
4f130 61 79 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 ay be called at
4f140 61 6e 79 20 74 69 6d 65 20 62 79 20 53 51 4c 69 any time by SQLi
4f150 74 65 20 74 6f 20 73 65 74 20 74 68 65 0a 2a 2a te to set the.**
4f160 20 73 75 67 67 65 73 74 65 64 20 6d 61 78 69 6d suggested maxim
4f170 75 6d 20 63 61 63 68 65 2d 73 69 7a 65 20 28 6e um cache-size (n
4f180 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 umber of pages s
4f190 74 6f 72 65 64 20 62 79 29 20 74 68 65 20 63 61 tored by) the ca
4f1a0 63 68 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 che.** instance
4f1b0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
4f1c0 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 rst argument. Th
4f1d0 69 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 is is the value
4f1e0 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 configured using
4f1f0 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 22 .** the SQLite "
4f200 5b 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73 69 [PRAGMA cache_si
4f210 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e 29 5e 20 ze]" command.)^
4f220 20 41 73 20 77 69 74 68 20 74 68 65 20 62 50 75 As with the bPu
4f230 72 67 65 61 62 6c 65 0a 2a 2a 20 70 61 72 61 6d rgeable.** param
4f240 65 74 65 72 2c 20 74 68 65 20 69 6d 70 6c 65 6d eter, the implem
4f250 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 entation is not
4f260 72 65 71 75 69 72 65 64 20 74 6f 20 64 6f 20 61 required to do a
4f270 6e 79 74 68 69 6e 67 20 77 69 74 68 20 74 68 69 nything with thi
4f280 73 0a 2a 2a 20 76 61 6c 75 65 3b 20 69 74 20 69 s.** value; it i
4f290 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e s advisory only.
4f2a0 0a 2a 2a 0a 2a 2a 20 5b 5b 74 68 65 20 78 50 61 .**.** [[the xPa
4f2b0 67 65 63 6f 75 6e 74 28 29 20 70 61 67 65 20 63 gecount() page c
4f2c0 61 63 68 65 20 6d 65 74 68 6f 64 73 5d 5d 0a 2a ache methods]].*
4f2d0 2a 20 54 68 65 20 78 50 61 67 65 63 6f 75 6e 74 * The xPagecount
4f2e0 28 29 20 6d 65 74 68 6f 64 20 6d 75 73 74 20 72 () method must r
4f2f0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
4f300 20 6f 66 20 70 61 67 65 73 20 63 75 72 72 65 6e of pages curren
4f310 74 6c 79 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e tly.** stored in
4f320 20 74 68 65 20 63 61 63 68 65 2c 20 62 6f 74 68 the cache, both
4f330 20 70 69 6e 6e 65 64 20 61 6e 64 20 75 6e 70 69 pinned and unpi
4f340 6e 6e 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 5b 5b 74 nned..** .** [[t
4f350 68 65 20 78 46 65 74 63 68 28 29 20 70 61 67 65 he xFetch() page
4f360 20 63 61 63 68 65 20 6d 65 74 68 6f 64 73 5d 5d cache methods]]
4f370 0a 2a 2a 20 54 68 65 20 78 46 65 74 63 68 28 29 .** The xFetch()
4f380 20 6d 65 74 68 6f 64 20 6c 6f 63 61 74 65 73 20 method locates
4f390 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 a page in the ca
4f3a0 63 68 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 che and returns
4f3b0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 0a 2a 2a a pointer to .**
4f3c0 20 61 6e 20 73 71 6c 69 74 65 33 5f 70 63 61 63 an sqlite3_pcac
4f3d0 68 65 5f 70 61 67 65 20 6f 62 6a 65 63 74 20 61 he_page object a
4f3e0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
4f3f0 68 61 74 20 70 61 67 65 2c 20 6f 72 20 61 20 4e hat page, or a N
4f400 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 ULL pointer..**
4f410 54 68 65 20 70 42 75 66 20 65 6c 65 6d 65 6e 74 The pBuf element
4f420 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e 65 64 of the returned
4f430 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f sqlite3_pcache_
4f440 70 61 67 65 20 6f 62 6a 65 63 74 20 77 69 6c 6c page object will
4f450 20 62 65 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 be a.** pointer
4f460 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 to a buffer of
4f470 73 7a 50 61 67 65 20 62 79 74 65 73 20 75 73 65 szPage bytes use
4f480 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 d to store the c
4f490 6f 6e 74 65 6e 74 20 6f 66 20 61 20 0a 2a 2a 20 ontent of a .**
4f4a0 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 single database
4f4b0 70 61 67 65 2e 20 20 54 68 65 20 70 45 78 74 72 page. The pExtr
4f4c0 61 20 65 6c 65 6d 65 6e 74 20 6f 66 20 73 71 6c a element of sql
4f4d0 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67 65 ite3_pcache_page
4f4e0 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 61 20 70 6f will be.** a po
4f4f0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 7a 45 inter to the szE
4f500 78 74 72 61 20 62 79 74 65 73 20 6f 66 20 65 78 xtra bytes of ex
4f510 74 72 61 20 73 74 6f 72 61 67 65 20 74 68 61 74 tra storage that
4f520 20 53 51 4c 69 74 65 20 68 61 73 20 72 65 71 75 SQLite has requ
4f530 65 73 74 65 64 0a 2a 2a 20 66 6f 72 20 65 61 63 ested.** for eac
4f540 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 h entry in the p
4f550 61 67 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a age cache..**.**
4f560 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 The page to be
4f570 66 65 74 63 68 65 64 20 69 73 20 64 65 74 65 72 fetched is deter
4f580 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6b 65 79 mined by the key
4f590 2e 20 5e 54 68 65 20 6d 69 6e 69 6d 75 6d 20 6b . ^The minimum k
4f5a0 65 79 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 31 ey value.** is 1
4f5b0 2e 20 20 41 66 74 65 72 20 69 74 20 68 61 73 20 . After it has
4f5c0 62 65 65 6e 20 72 65 74 72 69 65 76 65 64 20 75 been retrieved u
4f5d0 73 69 6e 67 20 78 46 65 74 63 68 2c 20 74 68 65 sing xFetch, the
4f5e0 20 70 61 67 65 20 69 73 20 63 6f 6e 73 69 64 65 page is conside
4f5f0 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20 22 70 69 red.** to be "pi
4f600 6e 6e 65 64 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 nned"..**.** If
4f610 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 the requested pa
4f620 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e ge is already in
4f630 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2c the page cache,
4f640 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 63 then the page c
4f650 61 63 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e ache.** implemen
4f660 74 61 74 69 6f 6e 20 6d 75 73 74 20 72 65 74 75 tation must retu
4f670 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
4f680 74 68 65 20 70 61 67 65 20 62 75 66 66 65 72 20 the page buffer
4f690 77 69 74 68 20 69 74 73 20 63 6f 6e 74 65 6e 74 with its content
4f6a0 0a 2a 2a 20 69 6e 74 61 63 74 2e 20 20 49 66 20 .** intact. If
4f6b0 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 the requested pa
4f6c0 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 ge is not alread
4f6d0 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 y in the cache,
4f6e0 74 68 65 6e 20 74 68 65 0a 2a 2a 20 63 61 63 68 then the.** cach
4f6f0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
4f700 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 should use the
4f710 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 value of the cre
4f720 61 74 65 46 6c 61 67 0a 2a 2a 20 70 61 72 61 6d ateFlag.** param
4f730 65 74 65 72 20 74 6f 20 68 65 6c 70 20 69 74 20 eter to help it
4f740 64 65 74 65 72 6d 69 6e 65 64 20 77 68 61 74 20 determined what
4f750 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 3a 0a action to take:.
4f760 2a 2a 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 **.** <table bor
4f770 64 65 72 3d 31 20 77 69 64 74 68 3d 38 35 25 20 der=1 width=85%
4f780 61 6c 69 67 6e 3d 63 65 6e 74 65 72 3e 0a 2a 2a align=center>.**
4f790 20 3c 74 72 3e 3c 74 68 3e 20 63 72 65 61 74 65 <tr><th> create
4f7a0 46 6c 61 67 20 3c 74 68 3e 20 42 65 68 61 76 69 Flag <th> Behavi
4f7b0 6f 75 72 20 77 68 65 6e 20 70 61 67 65 20 69 73 our when page is
4f7c0 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 not already in
4f7d0 63 61 63 68 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 cache.** <tr><td
4f7e0 3e 20 30 20 3c 74 64 3e 20 44 6f 20 6e 6f 74 20 > 0 <td> Do not
4f7f0 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 allocate a new p
4f800 61 67 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c age. Return NUL
4f810 4c 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 31 L..** <tr><td> 1
4f820 20 3c 74 64 3e 20 41 6c 6c 6f 63 61 74 65 20 61 <td> Allocate a
4f830 20 6e 65 77 20 70 61 67 65 20 69 66 20 69 74 20 new page if it
4f840 65 61 73 79 20 61 6e 64 20 63 6f 6e 76 65 6e 69 easy and conveni
4f850 65 6e 74 20 74 6f 20 64 6f 20 73 6f 2e 0a 2a 2a ent to do so..**
4f860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f870 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 Otherwise retur
4f880 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 3c 74 72 3e 3c n NULL..** <tr><
4f890 74 64 3e 20 32 20 3c 74 64 3e 20 4d 61 6b 65 20 td> 2 <td> Make
4f8a0 65 76 65 72 79 20 65 66 66 6f 72 74 20 74 6f 20 every effort to
4f8b0 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 allocate a new p
4f8c0 61 67 65 2e 20 20 4f 6e 6c 79 20 72 65 74 75 72 age. Only retur
4f8d0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 n.**
4f8e0 20 20 20 20 20 4e 55 4c 4c 20 69 66 20 61 6c 6c NULL if all
4f8f0 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 70 61 ocating a new pa
4f900 67 65 20 69 73 20 65 66 66 65 63 74 69 76 65 6c ge is effectivel
4f910 79 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a y impossible..**
4f920 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 </table>.**.**
4f930 5e 28 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f ^(SQLite will no
4f940 72 6d 61 6c 6c 79 20 69 6e 76 6f 6b 65 20 78 46 rmally invoke xF
4f950 65 74 63 68 28 29 20 77 69 74 68 20 61 20 63 72 etch() with a cr
4f960 65 61 74 65 46 6c 61 67 20 6f 66 20 30 20 6f 72 eateFlag of 0 or
4f970 20 31 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 1. SQLite.** w
4f980 69 6c 6c 20 6f 6e 6c 79 20 75 73 65 20 61 20 63 ill only use a c
4f990 72 65 61 74 65 46 6c 61 67 20 6f 66 20 32 20 61 reateFlag of 2 a
4f9a0 66 74 65 72 20 61 20 70 72 69 6f 72 20 63 61 6c fter a prior cal
4f9b0 6c 20 77 69 74 68 20 61 20 63 72 65 61 74 65 46 l with a createF
4f9c0 6c 61 67 20 6f 66 20 31 0a 2a 2a 20 66 61 69 6c lag of 1.** fail
4f9d0 65 64 2e 29 5e 20 20 49 6e 20 62 65 74 77 65 65 ed.)^ In betwee
4f9e0 6e 20 74 68 65 20 74 6f 20 78 46 65 74 63 68 28 n the to xFetch(
4f9f0 29 20 63 61 6c 6c 73 2c 20 53 51 4c 69 74 65 20 ) calls, SQLite
4fa00 6d 61 79 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74 may.** attempt t
4fa10 6f 20 75 6e 70 69 6e 20 6f 6e 65 20 6f 72 20 6d o unpin one or m
4fa20 6f 72 65 20 63 61 63 68 65 20 70 61 67 65 73 20 ore cache pages
4fa30 62 79 20 73 70 69 6c 6c 69 6e 67 20 74 68 65 20 by spilling the
4fa40 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 70 69 content of.** pi
4fa50 6e 6e 65 64 20 70 61 67 65 73 20 74 6f 20 64 69 nned pages to di
4fa60 73 6b 20 61 6e 64 20 73 79 6e 63 68 69 6e 67 20 sk and synching
4fa70 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
4fa80 73 74 65 6d 20 64 69 73 6b 20 63 61 63 68 65 2e stem disk cache.
4fa90 0a 2a 2a 0a 2a 2a 20 5b 5b 74 68 65 20 78 55 6e .**.** [[the xUn
4faa0 70 69 6e 28 29 20 70 61 67 65 20 63 61 63 68 65 pin() page cache
4fab0 20 6d 65 74 68 6f 64 5d 5d 0a 2a 2a 20 5e 78 55 method]].** ^xU
4fac0 6e 70 69 6e 28 29 20 69 73 20 63 61 6c 6c 65 64 npin() is called
4fad0 20 62 79 20 53 51 4c 69 74 65 20 77 69 74 68 20 by SQLite with
4fae0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 63 a pointer to a c
4faf0 75 72 72 65 6e 74 6c 79 20 70 69 6e 6e 65 64 20 urrently pinned
4fb00 70 61 67 65 0a 2a 2a 20 61 73 20 69 74 73 20 73 page.** as its s
4fb10 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 econd argument.
4fb20 20 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61 If the third pa
4fb30 72 61 6d 65 74 65 72 2c 20 64 69 73 63 61 72 64 rameter, discard
4fb40 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a , is non-zero,.*
4fb50 2a 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 * then the page
4fb60 6d 75 73 74 20 62 65 20 65 76 69 63 74 65 64 20 must be evicted
4fb70 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 2e 0a from the cache..
4fb80 2a 2a 20 5e 49 66 20 74 68 65 20 64 69 73 63 61 ** ^If the disca
4fb90 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a rd parameter is.
4fba0 2a 2a 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 ** zero, then th
4fbb0 65 20 70 61 67 65 20 6d 61 79 20 62 65 20 64 69 e page may be di
4fbc0 73 63 61 72 64 65 64 20 6f 72 20 72 65 74 61 69 scarded or retai
4fbd0 6e 65 64 20 61 74 20 74 68 65 20 64 69 73 63 72 ned at the discr
4fbe0 65 74 69 6f 6e 20 6f 66 0a 2a 2a 20 70 61 67 65 etion of.** page
4fbf0 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 cache implement
4fc00 61 74 69 6f 6e 2e 20 5e 54 68 65 20 70 61 67 65 ation. ^The page
4fc10 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 cache implement
4fc20 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79 20 63 68 6f ation.** may cho
4fc30 6f 73 65 20 74 6f 20 65 76 69 63 74 20 75 6e 70 ose to evict unp
4fc40 69 6e 6e 65 64 20 70 61 67 65 73 20 61 74 20 61 inned pages at a
4fc50 6e 79 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 ny time..**.** T
4fc60 68 65 20 63 61 63 68 65 20 6d 75 73 74 20 6e 6f he cache must no
4fc70 74 20 70 65 72 66 6f 72 6d 20 61 6e 79 20 72 65 t perform any re
4fc80 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 ference counting
4fc90 2e 20 41 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 63 . A single .** c
4fca0 61 6c 6c 20 74 6f 20 78 55 6e 70 69 6e 28 29 20 all to xUnpin()
4fcb0 75 6e 70 69 6e 73 20 74 68 65 20 70 61 67 65 20 unpins the page
4fcc0 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 regardless of th
4fcd0 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f e number of prio
4fce0 72 20 63 61 6c 6c 73 20 0a 2a 2a 20 74 6f 20 78 r calls .** to x
4fcf0 46 65 74 63 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b Fetch()..**.** [
4fd00 5b 74 68 65 20 78 52 65 6b 65 79 28 29 20 70 61 [the xRekey() pa
4fd10 67 65 20 63 61 63 68 65 20 6d 65 74 68 6f 64 73 ge cache methods
4fd20 5d 5d 0a 2a 2a 20 54 68 65 20 78 52 65 6b 65 79 ]].** The xRekey
4fd30 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 73 65 () method is use
4fd40 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 d to change the
4fd50 6b 65 79 20 76 61 6c 75 65 20 61 73 73 6f 63 69 key value associ
4fd60 61 74 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a ated with the.**
4fd70 20 70 61 67 65 20 70 61 73 73 65 64 20 61 73 20 page passed as
4fd80 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
4fd90 65 6e 74 2e 20 49 66 20 74 68 65 20 63 61 63 68 ent. If the cach
4fda0 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 e.** previously
4fdb0 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 contains an entr
4fdc0 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 y associated wit
4fdd0 68 20 6e 65 77 4b 65 79 2c 20 69 74 20 6d 75 73 h newKey, it mus
4fde0 74 20 62 65 0a 2a 2a 20 64 69 73 63 61 72 64 65 t be.** discarde
4fdf0 64 2e 20 5e 41 6e 79 20 70 72 69 6f 72 20 63 61 d. ^Any prior ca
4fe00 63 68 65 20 65 6e 74 72 79 20 61 73 73 6f 63 69 che entry associ
4fe10 61 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79 ated with newKey
4fe20 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e is guaranteed n
4fe30 6f 74 0a 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e ot.** to be pinn
4fe40 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 ed..**.** When S
4fe50 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 65 20 QLite calls the
4fe60 78 54 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 xTruncate() meth
4fe70 6f 64 2c 20 74 68 65 20 63 61 63 68 65 20 6d 75 od, the cache mu
4fe80 73 74 20 64 69 73 63 61 72 64 20 61 6c 6c 0a 2a st discard all.*
4fe90 2a 20 65 78 69 73 74 69 6e 67 20 63 61 63 68 65 * existing cache
4fea0 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 70 61 entries with pa
4feb0 67 65 20 6e 75 6d 62 65 72 73 20 28 6b 65 79 73 ge numbers (keys
4fec0 29 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f ) greater than o
4fed0 72 20 65 71 75 61 6c 0a 2a 2a 20 74 6f 20 74 68 r equal.** to th
4fee0 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 e value of the i
4fef0 4c 69 6d 69 74 20 70 61 72 61 6d 65 74 65 72 20 Limit parameter
4ff00 70 61 73 73 65 64 20 74 6f 20 78 54 72 75 6e 63 passed to xTrunc
4ff10 61 74 65 28 29 2e 20 49 66 20 61 6e 79 0a 2a 2a ate(). If any.**
4ff20 20 6f 66 20 74 68 65 73 65 20 70 61 67 65 73 20 of these pages
4ff30 61 72 65 20 70 69 6e 6e 65 64 2c 20 74 68 65 79 are pinned, they
4ff40 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 are implicitly
4ff50 75 6e 70 69 6e 6e 65 64 2c 20 6d 65 61 6e 69 6e unpinned, meanin
4ff60 67 20 74 68 61 74 0a 2a 2a 20 74 68 65 79 20 63 g that.** they c
4ff70 61 6e 20 62 65 20 73 61 66 65 6c 79 20 64 69 73 an be safely dis
4ff80 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 5b 5b carded..**.** [[
4ff90 74 68 65 20 78 44 65 73 74 72 6f 79 28 29 20 70 the xDestroy() p
4ffa0 61 67 65 20 63 61 63 68 65 20 6d 65 74 68 6f 64 age cache method
4ffb0 5d 5d 0a 2a 2a 20 5e 54 68 65 20 78 44 65 73 74 ]].** ^The xDest
4ffc0 72 6f 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20 roy() method is
4ffd0 75 73 65 64 20 74 6f 20 64 65 6c 65 74 65 20 61 used to delete a
4ffe0 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65 64 cache allocated
4fff0 20 62 79 20 78 43 72 65 61 74 65 28 29 2e 0a 2a by xCreate()..*
50000 2a 20 41 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 * All resources
50010 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
50020 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 61 the specified ca
50030 63 68 65 20 73 68 6f 75 6c 64 20 62 65 20 66 72 che should be fr
50040 65 65 64 2e 20 5e 41 66 74 65 72 0a 2a 2a 20 63 eed. ^After.** c
50050 61 6c 6c 69 6e 67 20 74 68 65 20 78 44 65 73 74 alling the xDest
50060 72 6f 79 28 29 20 6d 65 74 68 6f 64 2c 20 53 51 roy() method, SQ
50070 4c 69 74 65 20 63 6f 6e 73 69 64 65 72 73 20 74 Lite considers t
50080 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 he [sqlite3_pcac
50090 68 65 2a 5d 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 he*].** handle i
500a0 6e 76 61 6c 69 64 2c 20 61 6e 64 20 77 69 6c 6c nvalid, and will
500b0 20 6e 6f 74 20 75 73 65 20 69 74 20 77 69 74 68 not use it with
500c0 20 61 6e 79 20 6f 74 68 65 72 20 73 71 6c 69 74 any other sqlit
500d0 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
500e0 73 32 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e s2.** functions.
500f0 0a 2a 2a 0a 2a 2a 20 5b 5b 74 68 65 20 78 53 68 .**.** [[the xSh
50100 72 69 6e 6b 28 29 20 70 61 67 65 20 63 61 63 68 rink() page cach
50110 65 20 6d 65 74 68 6f 64 5d 5d 0a 2a 2a 20 5e 53 e method]].** ^S
50120 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 QLite invokes th
50130 65 20 78 53 68 72 69 6e 6b 28 29 20 6d 65 74 68 e xShrink() meth
50140 6f 64 20 77 68 65 6e 20 69 74 20 77 61 6e 74 73 od when it wants
50150 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20 the page cache
50160 74 6f 0a 2a 2a 20 66 72 65 65 20 75 70 20 61 73 to.** free up as
50170 20 6d 75 63 68 20 6f 66 20 68 65 61 70 20 6d 65 much of heap me
50180 6d 6f 72 79 20 61 73 20 70 6f 73 73 69 62 6c 65 mory as possible
50190 2e 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 . The page cach
501a0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
501b0 0a 2a 2a 20 69 73 20 6e 6f 74 20 6f 62 6c 69 67 .** is not oblig
501c0 61 74 65 64 20 74 6f 20 66 72 65 65 20 61 6e 79 ated to free any
501d0 20 6d 65 6d 6f 72 79 2c 20 62 75 74 20 77 65 6c memory, but wel
501e0 6c 2d 62 65 68 61 76 65 64 20 69 6d 70 6c 65 6d l-behaved implem
501f0 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 entations should
50200 0a 2a 2a 20 64 6f 20 74 68 65 69 72 20 62 65 73 .** do their bes
50210 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 t..*/.typedef st
50220 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61 ruct sqlite3_pca
50230 63 68 65 5f 6d 65 74 68 6f 64 73 32 20 73 71 6c che_methods2 sql
50240 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
50250 6f 64 73 32 3b 0a 73 74 72 75 63 74 20 73 71 6c ods2;.struct sql
50260 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
50270 6f 64 73 32 20 7b 0a 20 20 69 6e 74 20 69 56 65 ods2 {. int iVe
50280 72 73 69 6f 6e 3b 0a 20 20 76 6f 69 64 20 2a 70 rsion;. void *p
50290 41 72 67 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e Arg;. int (*xIn
502a0 69 74 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f it)(void*);. vo
502b0 69 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 id (*xShutdown)(
502c0 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 void*);. sqlite
502d0 33 5f 70 63 61 63 68 65 20 2a 28 2a 78 43 72 65 3_pcache *(*xCre
502e0 61 74 65 29 28 69 6e 74 20 73 7a 50 61 67 65 2c ate)(int szPage,
502f0 20 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 69 6e int szExtra, in
50300 74 20 62 50 75 72 67 65 61 62 6c 65 29 3b 0a 20 t bPurgeable);.
50310 20 76 6f 69 64 20 28 2a 78 43 61 63 68 65 73 69 void (*xCachesi
50320 7a 65 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 ze)(sqlite3_pcac
50330 68 65 2a 2c 20 69 6e 74 20 6e 43 61 63 68 65 73 he*, int nCaches
50340 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 50 ize);. int (*xP
50350 61 67 65 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 agecount)(sqlite
50360 33 5f 70 63 61 63 68 65 2a 29 3b 0a 20 20 73 71 3_pcache*);. sq
50370 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67 lite3_pcache_pag
50380 65 20 2a 28 2a 78 46 65 74 63 68 29 28 73 71 6c e *(*xFetch)(sql
50390 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e ite3_pcache*, un
503a0 73 69 67 6e 65 64 20 6b 65 79 2c 20 69 6e 74 20 signed key, int
503b0 63 72 65 61 74 65 46 6c 61 67 29 3b 0a 20 20 76 createFlag);. v
503c0 6f 69 64 20 28 2a 78 55 6e 70 69 6e 29 28 73 71 oid (*xUnpin)(sq
503d0 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 73 lite3_pcache*, s
503e0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 qlite3_pcache_pa
503f0 67 65 2a 2c 20 69 6e 74 20 64 69 73 63 61 72 64 ge*, int discard
50400 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 6b );. void (*xRek
50410 65 79 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 ey)(sqlite3_pcac
50420 68 65 2a 2c 20 73 71 6c 69 74 65 33 5f 70 63 61 he*, sqlite3_pca
50430 63 68 65 5f 70 61 67 65 2a 2c 20 0a 20 20 20 20 che_page*, .
50440 20 20 75 6e 73 69 67 6e 65 64 20 6f 6c 64 4b 65 unsigned oldKe
50450 79 2c 20 75 6e 73 69 67 6e 65 64 20 6e 65 77 4b y, unsigned newK
50460 65 79 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 54 ey);. void (*xT
50470 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33 runcate)(sqlite3
50480 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e _pcache*, unsign
50490 65 64 20 69 4c 69 6d 69 74 29 3b 0a 20 20 76 6f ed iLimit);. vo
504a0 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 73 id (*xDestroy)(s
504b0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29 3b qlite3_pcache*);
504c0 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 72 69 6e . void (*xShrin
504d0 6b 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 k)(sqlite3_pcach
504e0 65 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 e*);.};../*.** T
504f0 68 69 73 20 69 73 20 74 68 65 20 6f 62 73 6f 6c his is the obsol
50500 65 74 65 20 70 63 61 63 68 65 5f 6d 65 74 68 6f ete pcache_metho
50510 64 73 20 6f 62 6a 65 63 74 20 74 68 61 74 20 68 ds object that h
50520 61 73 20 6e 6f 77 20 62 65 65 6e 20 72 65 70 6c as now been repl
50530 61 63 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 aced.** by sqlit
50540 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
50550 73 32 2e 20 20 54 68 69 73 20 6f 62 6a 65 63 74 s2. This object
50560 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 20 is not used by
50570 53 51 4c 69 74 65 2e 20 20 49 74 20 69 73 0a 2a SQLite. It is.*
50580 2a 20 72 65 74 61 69 6e 65 64 20 69 6e 20 74 68 * retained in th
50590 65 20 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f e header file fo
505a0 72 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 r backwards comp
505b0 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a atibility only..
505c0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
505d0 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 t sqlite3_pcache
505e0 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 _methods sqlite3
505f0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3b _pcache_methods;
50600 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
50610 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 7b pcache_methods {
50620 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20 . void *pArg;.
50630 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 76 6f int (*xInit)(vo
50640 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 id*);. void (*x
50650 53 68 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 Shutdown)(void*)
50660 3b 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 ;. sqlite3_pcac
50670 68 65 20 2a 28 2a 78 43 72 65 61 74 65 29 28 69 he *(*xCreate)(i
50680 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62 nt szPage, int b
50690 50 75 72 67 65 61 62 6c 65 29 3b 0a 20 20 76 6f Purgeable);. vo
506a0 69 64 20 28 2a 78 43 61 63 68 65 73 69 7a 65 29 id (*xCachesize)
506b0 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a (sqlite3_pcache*
506c0 2c 20 69 6e 74 20 6e 43 61 63 68 65 73 69 7a 65 , int nCachesize
506d0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 50 61 67 65 );. int (*xPage
506e0 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 70 count)(sqlite3_p
506f0 63 61 63 68 65 2a 29 3b 0a 20 20 76 6f 69 64 20 cache*);. void
50700 2a 28 2a 78 46 65 74 63 68 29 28 73 71 6c 69 74 *(*xFetch)(sqlit
50710 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 e3_pcache*, unsi
50720 67 6e 65 64 20 6b 65 79 2c 20 69 6e 74 20 63 72 gned key, int cr
50730 65 61 74 65 46 6c 61 67 29 3b 0a 20 20 76 6f 69 eateFlag);. voi
50740 64 20 28 2a 78 55 6e 70 69 6e 29 28 73 71 6c 69 d (*xUnpin)(sqli
50750 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69 te3_pcache*, voi
50760 64 2a 2c 20 69 6e 74 20 64 69 73 63 61 72 64 29 d*, int discard)
50770 3b 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 6b 65 ;. void (*xReke
50780 79 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 y)(sqlite3_pcach
50790 65 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67 e*, void*, unsig
507a0 6e 65 64 20 6f 6c 64 4b 65 79 2c 20 75 6e 73 69 ned oldKey, unsi
507b0 67 6e 65 64 20 6e 65 77 4b 65 79 29 3b 0a 20 20 gned newKey);.
507c0 76 6f 69 64 20 28 2a 78 54 72 75 6e 63 61 74 65 void (*xTruncate
507d0 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 )(sqlite3_pcache
507e0 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 4c 69 6d *, unsigned iLim
507f0 69 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 it);. void (*xD
50800 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f estroy)(sqlite3_
50810 70 63 61 63 68 65 2a 29 3b 0a 7d 3b 0a 0a 0a 2f pcache*);.};.../
50820 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f *.** CAPI3REF: O
50830 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 4f 62 6a nline Backup Obj
50840 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ect.**.** The sq
50850 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a lite3_backup obj
50860 65 63 74 20 72 65 63 6f 72 64 73 20 73 74 61 74 ect records stat
50870 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 e information ab
50880 6f 75 74 20 61 6e 20 6f 6e 67 6f 69 6e 67 0a 2a out an ongoing.*
50890 2a 20 6f 6e 6c 69 6e 65 20 62 61 63 6b 75 70 20 * online backup
508a0 6f 70 65 72 61 74 69 6f 6e 2e 20 20 5e 54 68 65 operation. ^The
508b0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
508c0 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 object is create
508d0 64 20 62 79 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 d by.** a call t
508e0 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 o [sqlite3_backu
508f0 70 5f 69 6e 69 74 28 29 5d 20 61 6e 64 20 69 73 p_init()] and is
50900 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 61 20 destroyed by a
50910 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 call to.** [sqli
50920 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 te3_backup_finis
50930 68 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 h()]..**.** See
50940 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 Also: [Using the
50950 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42 SQLite Online B
50960 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2f 0a 74 79 ackup API].*/.ty
50970 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
50980 69 74 65 33 5f 62 61 63 6b 75 70 20 73 71 6c 69 ite3_backup sqli
50990 74 65 33 5f 62 61 63 6b 75 70 3b 0a 0a 2f 2a 0a te3_backup;../*.
509a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 6e 6c ** CAPI3REF: Onl
509b0 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49 2e 0a ine Backup API..
509c0 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 63 6b 75 70 **.** The backup
509d0 20 41 50 49 20 63 6f 70 69 65 73 20 74 68 65 20 API copies the
509e0 63 6f 6e 74 65 6e 74 20 6f 66 20 6f 6e 65 20 64 content of one d
509f0 61 74 61 62 61 73 65 20 69 6e 74 6f 20 61 6e 6f atabase into ano
50a00 74 68 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 75 ther..** It is u
50a10 73 65 66 75 6c 20 65 69 74 68 65 72 20 66 6f 72 seful either for
50a20 20 63 72 65 61 74 69 6e 67 20 62 61 63 6b 75 70 creating backup
50a30 73 20 6f 66 20 64 61 74 61 62 61 73 65 73 20 6f s of databases o
50a40 72 0a 2a 2a 20 66 6f 72 20 63 6f 70 79 69 6e 67 r.** for copying
50a50 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
50a60 61 73 65 73 20 74 6f 20 6f 72 20 66 72 6f 6d 20 ases to or from
50a70 70 65 72 73 69 73 74 65 6e 74 20 66 69 6c 65 73 persistent files
50a80 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 . .**.** See Als
50a90 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51 o: [Using the SQ
50aa0 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42 61 63 6b Lite Online Back
50ab0 75 70 20 41 50 49 5d 0a 2a 2a 0a 2a 2a 20 5e 53 up API].**.** ^S
50ac0 51 4c 69 74 65 20 68 6f 6c 64 73 20 61 20 77 72 QLite holds a wr
50ad0 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite transaction
50ae0 6f 70 65 6e 20 6f 6e 20 74 68 65 20 64 65 73 74 open on the dest
50af0 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
50b00 20 66 69 6c 65 0a 2a 2a 20 66 6f 72 20 74 68 65 file.** for the
50b10 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 duration of the
50b20 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f backup operatio
50b30 6e 2e 0a 2a 2a 20 5e 54 68 65 20 73 6f 75 72 63 n..** ^The sourc
50b40 65 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 e database is re
50b50 61 64 2d 6c 6f 63 6b 65 64 20 6f 6e 6c 79 20 77 ad-locked only w
50b60 68 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e 67 hile it is being
50b70 20 72 65 61 64 3b 0a 2a 2a 20 69 74 20 69 73 20 read;.** it is
50b80 6e 6f 74 20 6c 6f 63 6b 65 64 20 63 6f 6e 74 69 not locked conti
50b90 6e 75 6f 75 73 6c 79 20 66 6f 72 20 74 68 65 20 nuously for the
50ba0 65 6e 74 69 72 65 20 62 61 63 6b 75 70 20 6f 70 entire backup op
50bb0 65 72 61 74 69 6f 6e 2e 0a 2a 2a 20 5e 54 68 75 eration..** ^Thu
50bc0 73 2c 20 74 68 65 20 62 61 63 6b 75 70 20 6d 61 s, the backup ma
50bd0 79 20 62 65 20 70 65 72 66 6f 72 6d 65 64 20 6f y be performed o
50be0 6e 20 61 20 6c 69 76 65 20 73 6f 75 72 63 65 20 n a live source
50bf0 64 61 74 61 62 61 73 65 20 77 69 74 68 6f 75 74 database without
50c00 0a 2a 2a 20 70 72 65 76 65 6e 74 69 6e 67 20 6f .** preventing o
50c10 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f ther database co
50c20 6e 6e 65 63 74 69 6f 6e 73 20 66 72 6f 6d 0a 2a nnections from.*
50c30 2a 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 * reading or wri
50c40 74 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72 ting to the sour
50c50 63 65 20 64 61 74 61 62 61 73 65 20 77 68 69 6c ce database whil
50c60 65 20 74 68 65 20 62 61 63 6b 75 70 20 69 73 20 e the backup is
50c70 75 6e 64 65 72 77 61 79 2e 0a 2a 2a 20 0a 2a 2a underway..** .**
50c80 20 5e 28 54 6f 20 70 65 72 66 6f 72 6d 20 61 20 ^(To perform a
50c90 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
50ca0 3a 20 0a 2a 2a 20 20 20 3c 6f 6c 3e 0a 2a 2a 20 : .** <ol>.**
50cb0 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 <li><b>sqlit
50cc0 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 e3_backup_init()
50cd0 3c 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64 20 6f </b> is called o
50ce0 6e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a nce to initializ
50cf0 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 e the.**
50d00 20 62 61 63 6b 75 70 2c 20 0a 2a 2a 20 20 20 20 backup, .**
50d10 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f <li><b>sqlite3_
50d20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 backup_step()</b
50d30 3e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 65 20 > is called one
50d40 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f or more times to
50d50 20 74 72 61 6e 73 66 65 72 20 0a 2a 2a 20 20 20 transfer .**
50d60 20 20 20 20 20 20 74 68 65 20 64 61 74 61 20 62 the data b
50d70 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20 64 etween the two d
50d80 61 74 61 62 61 73 65 73 2c 20 61 6e 64 20 66 69 atabases, and fi
50d90 6e 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 3c 6c 69 nally.** <li
50da0 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b ><b>sqlite3_back
50db0 75 70 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e 20 up_finish()</b>
50dc0 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 6c is called to rel
50dd0 65 61 73 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 ease all resourc
50de0 65 73 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 es .** a
50df0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
50e00 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 he backup operat
50e10 69 6f 6e 2e 20 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e ion. .** </ol>
50e20 29 5e 0a 2a 2a 20 54 68 65 72 65 20 73 68 6f 75 )^.** There shou
50e30 6c 64 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e ld be exactly on
50e40 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 e call to sqlite
50e50 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 3_backup_finish(
50e60 29 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 73 75 ) for each.** su
50e70 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f ccessful call to
50e80 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
50e90 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b 5b init()..**.** [[
50ea0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 sqlite3_backup_i
50eb0 6e 69 74 28 29 5d 5d 20 3c 62 3e 73 71 6c 69 74 nit()]] <b>sqlit
50ec0 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 e3_backup_init()
50ed0 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 </b>.**.** ^The
50ee0 44 20 61 6e 64 20 4e 20 61 72 67 75 6d 65 6e 74 D and N argument
50ef0 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 s to sqlite3_bac
50f00 6b 75 70 5f 69 6e 69 74 28 44 2c 4e 2c 53 2c 4d kup_init(D,N,S,M
50f10 29 20 61 72 65 20 74 68 65 20 0a 2a 2a 20 5b 64 ) are the .** [d
50f20 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
50f30 6f 6e 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 on] associated w
50f40 69 74 68 20 74 68 65 20 64 65 73 74 69 6e 61 74 ith the destinat
50f50 69 6f 6e 20 64 61 74 61 62 61 73 65 20 0a 2a 2a ion database .**
50f60 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 and the databas
50f70 65 20 6e 61 6d 65 2c 20 72 65 73 70 65 63 74 69 e name, respecti
50f80 76 65 6c 79 2e 0a 2a 2a 20 5e 54 68 65 20 64 61 vely..** ^The da
50f90 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 tabase name is "
50fa0 6d 61 69 6e 22 20 66 6f 72 20 74 68 65 20 6d 61 main" for the ma
50fb0 69 6e 20 64 61 74 61 62 61 73 65 2c 20 22 74 65 in database, "te
50fc0 6d 70 22 20 66 6f 72 20 74 68 65 0a 2a 2a 20 74 mp" for the.** t
50fd0 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 emporary databas
50fe0 65 2c 20 6f 72 20 74 68 65 20 6e 61 6d 65 20 73 e, or the name s
50ff0 70 65 63 69 66 69 65 64 20 61 66 74 65 72 20 74 pecified after t
51000 68 65 20 41 53 20 6b 65 79 77 6f 72 64 20 69 6e he AS keyword in
51010 0a 2a 2a 20 61 6e 20 5b 41 54 54 41 43 48 5d 20 .** an [ATTACH]
51020 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 61 6e statement for an
51030 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 attached databa
51040 73 65 2e 0a 2a 2a 20 5e 54 68 65 20 53 20 61 6e se..** ^The S an
51050 64 20 4d 20 61 72 67 75 6d 65 6e 74 73 20 70 61 d M arguments pa
51060 73 73 65 64 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 ssed to .** sqli
51070 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 te3_backup_init(
51080 44 2c 4e 2c 53 2c 4d 29 20 69 64 65 6e 74 69 66 D,N,S,M) identif
51090 79 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 y the [database
510a0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 61 connection].** a
510b0 6e 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 nd database name
510c0 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 of the source d
510d0 61 74 61 62 61 73 65 2c 20 72 65 73 70 65 63 74 atabase, respect
510e0 69 76 65 6c 79 2e 0a 2a 2a 20 5e 54 68 65 20 73 ively..** ^The s
510f0 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e ource and destin
51100 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 ation [database
51110 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 28 70 61 connections] (pa
51120 72 61 6d 65 74 65 72 73 20 53 20 61 6e 64 20 44 rameters S and D
51130 29 0a 2a 2a 20 6d 75 73 74 20 62 65 20 64 69 66 ).** must be dif
51140 66 65 72 65 6e 74 20 6f 72 20 65 6c 73 65 20 73 ferent or else s
51150 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e qlite3_backup_in
51160 69 74 28 44 2c 4e 2c 53 2c 4d 29 20 77 69 6c 6c it(D,N,S,M) will
51170 20 66 61 69 6c 20 77 69 74 68 0a 2a 2a 20 61 6e fail with.** an
51180 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 5e 49 error..**.** ^I
51190 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
511a0 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33 s within sqlite3
511b0 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 44 2c 4e _backup_init(D,N
511c0 2c 53 2c 4d 29 2c 20 74 68 65 6e 20 4e 55 4c 4c ,S,M), then NULL
511d0 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 is.** returned
511e0 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 and an error cod
511f0 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 e and error mess
51200 61 67 65 20 61 72 65 20 73 74 6f 72 65 64 20 69 age are stored i
51210 6e 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 n the.** destina
51220 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 tion [database c
51230 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a onnection] D..**
51240 20 5e 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 ^The error code
51250 20 61 6e 64 20 6d 65 73 73 61 67 65 20 66 6f 72 and message for
51260 20 74 68 65 20 66 61 69 6c 65 64 20 63 61 6c 6c the failed call
51270 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
51280 75 70 5f 69 6e 69 74 28 29 0a 2a 2a 20 63 61 6e up_init().** can
51290 20 62 65 20 72 65 74 72 69 65 76 65 64 20 75 73 be retrieved us
512a0 69 6e 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33 ing the [sqlite3
512b0 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71 _errcode()], [sq
512c0 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c lite3_errmsg()],
512d0 20 61 6e 64 2f 6f 72 0a 2a 2a 20 5b 73 71 6c 69 and/or.** [sqli
512e0 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 20 te3_errmsg16()]
512f0 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 5e 41 functions..** ^A
51300 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c successful call
51310 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
51320 75 70 5f 69 6e 69 74 28 29 20 72 65 74 75 72 6e up_init() return
51330 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
51340 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 61 n.** [sqlite3_ba
51350 63 6b 75 70 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a ckup] object..**
51360 20 5e 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 ^The [sqlite3_b
51370 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 6d 61 ackup] object ma
51380 79 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 y be used with t
51390 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 he sqlite3_backu
513a0 70 5f 73 74 65 70 28 29 20 61 6e 64 0a 2a 2a 20 p_step() and.**
513b0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 sqlite3_backup_f
513c0 69 6e 69 73 68 28 29 20 66 75 6e 63 74 69 6f 6e inish() function
513d0 73 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 65 s to perform the
513e0 20 73 70 65 63 69 66 69 65 64 20 62 61 63 6b 75 specified backu
513f0 70 20 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e p .** operation.
51400 0a 2a 2a 0a 2a 2a 20 5b 5b 73 71 6c 69 74 65 33 .**.** [[sqlite3
51410 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 5d 5d _backup_step()]]
51420 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b <b>sqlite3_back
51430 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 0a 2a 2a up_step()</b>.**
51440 0a 2a 2a 20 5e 46 75 6e 63 74 69 6f 6e 20 73 71 .** ^Function sq
51450 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
51460 70 28 42 2c 4e 29 20 77 69 6c 6c 20 63 6f 70 79 p(B,N) will copy
51470 20 75 70 20 74 6f 20 4e 20 70 61 67 65 73 20 62 up to N pages b
51480 65 74 77 65 65 6e 20 0a 2a 2a 20 74 68 65 20 73 etween .** the s
51490 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e ource and destin
514a0 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 73 20 ation databases
514b0 73 70 65 63 69 66 69 65 64 20 62 79 20 5b 73 71 specified by [sq
514c0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 lite3_backup] ob
514d0 6a 65 63 74 20 42 2e 0a 2a 2a 20 5e 49 66 20 4e ject B..** ^If N
514e0 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 61 6c is negative, al
514f0 6c 20 72 65 6d 61 69 6e 69 6e 67 20 73 6f 75 72 l remaining sour
51500 63 65 20 70 61 67 65 73 20 61 72 65 20 63 6f 70 ce pages are cop
51510 69 65 64 2e 20 0a 2a 2a 20 5e 49 66 20 73 71 6c ied. .** ^If sql
51520 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
51530 28 42 2c 4e 29 20 73 75 63 63 65 73 73 66 75 6c (B,N) successful
51540 6c 79 20 63 6f 70 69 65 73 20 4e 20 70 61 67 65 ly copies N page
51550 73 20 61 6e 64 20 74 68 65 72 65 0a 2a 2a 20 61 s and there.** a
51560 72 65 20 73 74 69 6c 6c 20 6d 6f 72 65 20 70 61 re still more pa
51570 67 65 73 20 74 6f 20 62 65 20 63 6f 70 69 65 64 ges to be copied
51580 2c 20 74 68 65 6e 20 74 68 65 20 66 75 6e 63 74 , then the funct
51590 69 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 51 4c ion returns [SQL
515a0 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 5e 49 66 20 ITE_OK]..** ^If
515b0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
515c0 74 65 70 28 42 2c 4e 29 20 73 75 63 63 65 73 73 tep(B,N) success
515d0 66 75 6c 6c 79 20 66 69 6e 69 73 68 65 73 20 63 fully finishes c
515e0 6f 70 79 69 6e 67 20 61 6c 6c 20 70 61 67 65 73 opying all pages
515f0 0a 2a 2a 20 66 72 6f 6d 20 73 6f 75 72 63 65 20 .** from source
51600 74 6f 20 64 65 73 74 69 6e 61 74 69 6f 6e 2c 20 to destination,
51610 74 68 65 6e 20 69 74 20 72 65 74 75 72 6e 73 20 then it returns
51620 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 0a 2a [SQLITE_DONE]..*
51630 2a 20 5e 49 66 20 61 6e 20 65 72 72 6f 72 20 6f * ^If an error o
51640 63 63 75 72 73 20 77 68 69 6c 65 20 72 75 6e 6e ccurs while runn
51650 69 6e 67 20 73 71 6c 69 74 65 33 5f 62 61 63 6b ing sqlite3_back
51660 75 70 5f 73 74 65 70 28 42 2c 4e 29 2c 0a 2a 2a up_step(B,N),.**
51670 20 74 68 65 6e 20 61 6e 20 5b 65 72 72 6f 72 20 then an [error
51680 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 code] is returne
51690 64 2e 20 5e 41 73 20 77 65 6c 6c 20 61 73 20 5b d. ^As well as [
516a0 53 51 4c 49 54 45 5f 4f 4b 5d 20 61 6e 64 0a 2a SQLITE_OK] and.*
516b0 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c * [SQLITE_DONE],
516c0 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 a call to sqlit
516d0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
516e0 20 6d 61 79 20 72 65 74 75 72 6e 20 5b 53 51 4c may return [SQL
516f0 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 2c 0a 2a ITE_READONLY],.*
51700 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d * [SQLITE_NOMEM]
51710 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c , [SQLITE_BUSY],
51720 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d [SQLITE_LOCKED]
51730 2c 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49 , or an.** [SQLI
51740 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 TE_IOERR_ACCESS
51750 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 | SQLITE_IOERR_X
51760 58 58 5d 20 65 78 74 65 6e 64 65 64 20 65 72 72 XX] extended err
51770 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 5e or code..**.** ^
51780 28 54 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 (The sqlite3_bac
51790 6b 75 70 5f 73 74 65 70 28 29 20 6d 69 67 68 74 kup_step() might
517a0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
517b0 52 45 41 44 4f 4e 4c 59 5d 20 69 66 0a 2a 2a 20 READONLY] if.**
517c0 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 74 68 65 <ol>.** <li> the
517d0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
517e0 61 62 61 73 65 20 77 61 73 20 6f 70 65 6e 65 64 abase was opened
517f0 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 6f 72 0a 2a read-only, or.*
51800 2a 20 3c 6c 69 3e 20 74 68 65 20 64 65 73 74 69 * <li> the desti
51810 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
51820 69 73 20 75 73 69 6e 67 20 77 72 69 74 65 2d 61 is using write-a
51830 68 65 61 64 2d 6c 6f 67 20 6a 6f 75 72 6e 61 6c head-log journal
51840 69 6e 67 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 ing.** and the d
51850 65 73 74 69 6e 61 74 69 6f 6e 20 61 6e 64 20 73 estination and s
51860 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a 65 73 ource page sizes
51870 20 64 69 66 66 65 72 2c 20 6f 72 0a 2a 2a 20 3c differ, or.** <
51880 6c 69 3e 20 74 68 65 20 64 65 73 74 69 6e 61 74 li> the destinat
51890 69 6f 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 ion database is
518a0 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
518b0 61 62 61 73 65 20 61 6e 64 20 74 68 65 0a 2a 2a abase and the.**
518c0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 61 6e 64 destination and
518d0 20 73 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a source page siz
518e0 65 73 20 64 69 66 66 65 72 2e 0a 2a 2a 20 3c 2f es differ..** </
518f0 6f 6c 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 ol>)^.**.** ^If
51900 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
51910 74 65 70 28 29 20 63 61 6e 6e 6f 74 20 6f 62 74 tep() cannot obt
51920 61 69 6e 20 61 20 72 65 71 75 69 72 65 64 20 66 ain a required f
51930 69 6c 65 2d 73 79 73 74 65 6d 20 6c 6f 63 6b 2c ile-system lock,
51940 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 73 71 then.** the [sq
51950 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c lite3_busy_handl
51960 65 72 20 7c 20 62 75 73 79 2d 68 61 6e 64 6c 65 er | busy-handle
51970 72 20 66 75 6e 63 74 69 6f 6e 5d 0a 2a 2a 20 69 r function].** i
51980 73 20 69 6e 76 6f 6b 65 64 20 28 69 66 20 6f 6e s invoked (if on
51990 65 20 69 73 20 73 70 65 63 69 66 69 65 64 29 2e e is specified).
519a0 20 5e 49 66 20 74 68 65 20 0a 2a 2a 20 62 75 73 ^If the .** bus
519b0 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e y-handler return
519c0 73 20 6e 6f 6e 2d 7a 65 72 6f 20 62 65 66 6f 72 s non-zero befor
519d0 65 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 76 e the lock is av
519e0 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 0a 2a ailable, then .*
519f0 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 * [SQLITE_BUSY]
51a00 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 is returned to t
51a10 68 65 20 63 61 6c 6c 65 72 2e 20 5e 49 6e 20 74 he caller. ^In t
51a20 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c his case the cal
51a30 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f l to.** sqlite3_
51a40 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 backup_step() ca
51a50 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 n be retried lat
51a60 65 72 2e 20 5e 49 66 20 74 68 65 20 73 6f 75 72 er. ^If the sour
51a70 63 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 ce.** [database
51a80 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 69 connection].** i
51a90 73 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20 s being used to
51aa0 77 72 69 74 65 20 74 6f 20 74 68 65 20 73 6f 75 write to the sou
51ab0 72 63 65 20 64 61 74 61 62 61 73 65 20 77 68 65 rce database whe
51ac0 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 n sqlite3_backup
51ad0 5f 73 74 65 70 28 29 0a 2a 2a 20 69 73 20 63 61 _step().** is ca
51ae0 6c 6c 65 64 2c 20 74 68 65 6e 20 5b 53 51 4c 49 lled, then [SQLI
51af0 54 45 5f 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 TE_LOCKED] is re
51b00 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 turned immediate
51b10 6c 79 2e 20 5e 41 67 61 69 6e 2c 20 69 6e 20 74 ly. ^Again, in t
51b20 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 his.** case the
51b30 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
51b40 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 backup_step() ca
51b50 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 n be retried lat
51b60 65 72 20 6f 6e 2e 20 5e 28 49 66 0a 2a 2a 20 5b er on. ^(If.** [
51b70 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 SQLITE_IOERR_ACC
51b80 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 ESS | SQLITE_IOE
51b90 52 52 5f 58 58 58 5d 2c 20 5b 53 51 4c 49 54 45 RR_XXX], [SQLITE
51ba0 5f 4e 4f 4d 45 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b _NOMEM], or.** [
51bb0 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d SQLITE_READONLY]
51bc0 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 is returned, th
51bd0 65 6e 20 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 en .** there is
51be0 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 72 65 74 72 no point in retr
51bf0 79 69 6e 67 20 74 68 65 20 63 61 6c 6c 20 74 6f ying the call to
51c00 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
51c10 73 74 65 70 28 29 2e 20 54 68 65 73 65 20 0a 2a step(). These .*
51c20 2a 20 65 72 72 6f 72 73 20 61 72 65 20 63 6f 6e * errors are con
51c30 73 69 64 65 72 65 64 20 66 61 74 61 6c 2e 29 5e sidered fatal.)^
51c40 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f The applicatio
51c50 6e 20 6d 75 73 74 20 61 63 63 65 70 74 20 0a 2a n must accept .*
51c60 2a 20 74 68 61 74 20 74 68 65 20 62 61 63 6b 75 * that the backu
51c70 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 73 20 p operation has
51c80 66 61 69 6c 65 64 20 61 6e 64 20 70 61 73 73 20 failed and pass
51c90 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 the backup opera
51ca0 74 69 6f 6e 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 tion handle .**
51cb0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 to the sqlite3_b
51cc0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 74 ackup_finish() t
51cd0 6f 20 72 65 6c 65 61 73 65 20 61 73 73 6f 63 69 o release associ
51ce0 61 74 65 64 20 72 65 73 6f 75 72 63 65 73 2e 0a ated resources..
51cf0 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 72 73 74 **.** ^The first
51d00 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
51d10 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6f _backup_step() o
51d20 62 74 61 69 6e 73 20 61 6e 20 65 78 63 6c 75 73 btains an exclus
51d30 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 74 ive lock.** on t
51d40 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 66 he destination f
51d50 69 6c 65 2e 20 5e 54 68 65 20 65 78 63 6c 75 73 ile. ^The exclus
51d60 69 76 65 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 ive lock is not
51d70 72 65 6c 65 61 73 65 64 20 75 6e 74 69 6c 20 65 released until e
51d80 69 74 68 65 72 20 0a 2a 2a 20 73 71 6c 69 74 65 ither .** sqlite
51d90 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 3_backup_finish(
51da0 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 72 20 74 ) is called or t
51db0 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 he backup operat
51dc0 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 20 ion is complete
51dd0 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f .** and sqlite3_
51de0 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 72 65 backup_step() re
51df0 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 4f turns [SQLITE_DO
51e00 4e 45 5d 2e 20 20 5e 45 76 65 72 79 20 63 61 6c NE]. ^Every cal
51e10 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f l to.** sqlite3_
51e20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6f 62 backup_step() ob
51e30 74 61 69 6e 73 20 61 20 5b 73 68 61 72 65 64 20 tains a [shared
51e40 6c 6f 63 6b 5d 20 6f 6e 20 74 68 65 20 73 6f 75 lock] on the sou
51e50 72 63 65 20 64 61 74 61 62 61 73 65 20 74 68 61 rce database tha
51e60 74 0a 2a 2a 20 6c 61 73 74 73 20 66 6f 72 20 74 t.** lasts for t
51e70 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 he duration of t
51e80 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 he sqlite3_backu
51e90 70 5f 73 74 65 70 28 29 20 63 61 6c 6c 2e 0a 2a p_step() call..*
51ea0 2a 20 5e 42 65 63 61 75 73 65 20 74 68 65 20 73 * ^Because the s
51eb0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69 ource database i
51ec0 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 62 65 74 s not locked bet
51ed0 77 65 65 6e 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a ween calls to.**
51ee0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
51ef0 73 74 65 70 28 29 2c 20 74 68 65 20 73 6f 75 72 step(), the sour
51f00 63 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 ce database may
51f10 62 65 20 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d be modified mid-
51f20 77 61 79 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 way.** through t
51f30 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 he backup proces
51f40 73 2e 20 20 5e 49 66 20 74 68 65 20 73 6f 75 72 s. ^If the sour
51f50 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6d ce database is m
51f60 6f 64 69 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a odified by an.**
51f70 20 65 78 74 65 72 6e 61 6c 20 70 72 6f 63 65 73 external proces
51f80 73 20 6f 72 20 76 69 61 20 61 20 64 61 74 61 62 s or via a datab
51f90 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f ase connection o
51fa0 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e ther than the on
51fb0 65 20 62 65 69 6e 67 0a 2a 2a 20 75 73 65 64 20 e being.** used
51fc0 62 79 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 by the backup op
51fd0 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 eration, then th
51fe0 65 20 62 61 63 6b 75 70 20 77 69 6c 6c 20 62 65 e backup will be
51ff0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a automatically.*
52000 2a 20 72 65 73 74 61 72 74 65 64 20 62 79 20 74 * restarted by t
52010 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 he next call to
52020 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
52030 74 65 70 28 29 2e 20 5e 49 66 20 74 68 65 20 73 tep(). ^If the s
52040 6f 75 72 63 65 20 0a 2a 2a 20 64 61 74 61 62 61 ource .** databa
52050 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 se is modified b
52060 79 20 74 68 65 20 75 73 69 6e 67 20 74 68 65 20 y the using the
52070 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
52080 6e 6e 65 63 74 69 6f 6e 20 61 73 20 69 73 20 75 nnection as is u
52090 73 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 62 61 sed.** by the ba
520a0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 ckup operation,
520b0 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75 70 20 then the backup
520c0 64 61 74 61 62 61 73 65 20 69 73 20 61 75 74 6f database is auto
520d0 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 75 70 64 matically.** upd
520e0 61 74 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 ated at the same
520f0 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 73 time..**.** [[s
52100 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
52110 6e 69 73 68 28 29 5d 5d 20 3c 62 3e 73 71 6c 69 nish()]] <b>sqli
52120 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 te3_backup_finis
52130 68 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 h()</b>.**.** Wh
52140 65 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 en sqlite3_backu
52150 70 5f 73 74 65 70 28 29 20 68 61 73 20 72 65 74 p_step() has ret
52160 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 44 4f urned [SQLITE_DO
52170 4e 45 5d 2c 20 6f 72 20 77 68 65 6e 20 74 68 65 NE], or when the
52180 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e .** application
52190 20 77 69 73 68 65 73 20 74 6f 20 61 62 61 6e 64 wishes to aband
521a0 6f 6e 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 on the backup op
521b0 65 72 61 74 69 6f 6e 2c 20 74 68 65 20 61 70 70 eration, the app
521c0 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 lication.** shou
521d0 6c 64 20 64 65 73 74 72 6f 79 20 74 68 65 20 5b ld destroy the [
521e0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 sqlite3_backup]
521f0 62 79 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f by passing it to
52200 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
52210 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 20 5e 54 68 finish()..** ^Th
52220 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 e sqlite3_backup
52230 5f 66 69 6e 69 73 68 28 29 20 69 6e 74 65 72 66 _finish() interf
52240 61 63 65 73 20 72 65 6c 65 61 73 65 73 20 61 6c aces releases al
52250 6c 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 61 l.** resources a
52260 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
52270 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b he [sqlite3_back
52280 75 70 5d 20 6f 62 6a 65 63 74 2e 20 0a 2a 2a 20 up] object. .**
52290 5e 49 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b ^If sqlite3_back
522a0 75 70 5f 73 74 65 70 28 29 20 68 61 73 20 6e 6f up_step() has no
522b0 74 20 79 65 74 20 72 65 74 75 72 6e 65 64 20 5b t yet returned [
522c0 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 74 68 SQLITE_DONE], th
522d0 65 6e 20 61 6e 79 0a 2a 2a 20 61 63 74 69 76 65 en any.** active
522e0 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
522f0 6f 6e 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e on on the destin
52300 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 69 ation database i
52310 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a s rolled back..*
52320 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 * The [sqlite3_b
52330 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 ackup] object is
52340 20 69 6e 76 61 6c 69 64 0a 2a 2a 20 61 6e 64 20 invalid.** and
52350 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20 may not be used
52360 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c following a call
52370 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
52380 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a up_finish()..**.
52390 2a 2a 20 5e 54 68 65 20 76 61 6c 75 65 20 72 65 ** ^The value re
523a0 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 turned by sqlite
523b0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 20 3_backup_finish
523c0 69 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 is [SQLITE_OK] i
523d0 66 20 6e 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f f no.** sqlite3_
523e0 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 65 72 backup_step() er
523f0 72 6f 72 73 20 6f 63 63 75 72 72 65 64 2c 20 72 rors occurred, r
52400 65 67 61 72 64 6c 65 73 73 20 6f 72 20 77 68 65 egardless or whe
52410 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 73 ther or not.** s
52420 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
52430 65 70 28 29 20 63 6f 6d 70 6c 65 74 65 64 2e 0a ep() completed..
52440 2a 2a 20 5e 49 66 20 61 6e 20 6f 75 74 2d 6f 66 ** ^If an out-of
52450 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 69 74 69 6f -memory conditio
52460 6e 20 6f 72 20 49 4f 20 65 72 72 6f 72 20 6f 63 n or IO error oc
52470 63 75 72 72 65 64 20 64 75 72 69 6e 67 20 61 6e curred during an
52480 79 20 70 72 69 6f 72 0a 2a 2a 20 73 71 6c 69 74 y prior.** sqlit
52490 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
524a0 20 63 61 6c 6c 20 6f 6e 20 74 68 65 20 73 61 6d call on the sam
524b0 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 e [sqlite3_backu
524c0 70 5d 20 6f 62 6a 65 63 74 2c 20 74 68 65 6e 0a p] object, then.
524d0 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
524e0 70 5f 66 69 6e 69 73 68 28 29 20 72 65 74 75 72 p_finish() retur
524f0 6e 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e ns the correspon
52500 64 69 6e 67 20 5b 65 72 72 6f 72 20 63 6f 64 65 ding [error code
52510 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 41 20 72 65 74 75 ]..**.** ^A retu
52520 72 6e 20 6f 66 20 5b 53 51 4c 49 54 45 5f 42 55 rn of [SQLITE_BU
52530 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c SY] or [SQLITE_L
52540 4f 43 4b 45 44 5d 20 66 72 6f 6d 20 73 71 6c 69 OCKED] from sqli
52550 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
52560 29 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 20 70 65 ).** is not a pe
52570 72 6d 61 6e 65 6e 74 20 65 72 72 6f 72 20 61 6e rmanent error an
52580 64 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 d does not affec
52590 74 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c t the return val
525a0 75 65 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 ue of.** sqlite3
525b0 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 _backup_finish()
525c0 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 73 71 6c 69 74 65 ..**.** [[sqlite
525d0 33 5f 62 61 63 6b 75 70 5f 5f 72 65 6d 61 69 6e 3_backup__remain
525e0 69 6e 67 28 29 5d 5d 20 5b 5b 73 71 6c 69 74 65 ing()]] [[sqlite
525f0 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 3_backup_pagecou
52600 6e 74 28 29 5d 5d 0a 2a 2a 20 3c 62 3e 73 71 6c nt()]].** <b>sql
52610 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 ite3_backup_rema
52620 69 6e 69 6e 67 28 29 20 61 6e 64 20 73 71 6c 69 ining() and sqli
52630 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 te3_backup_pagec
52640 6f 75 6e 74 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a ount()</b>.**.**
52650 20 5e 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 73 ^Each call to s
52660 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
52670 65 70 28 29 20 73 65 74 73 20 74 77 6f 20 76 61 ep() sets two va
52680 6c 75 65 73 20 69 6e 73 69 64 65 0a 2a 2a 20 74 lues inside.** t
52690 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b he [sqlite3_back
526a0 75 70 5d 20 6f 62 6a 65 63 74 3a 20 74 68 65 20 up] object: the
526b0 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
526c0 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 63 6b still to be back
526d0 65 64 0a 2a 2a 20 75 70 20 61 6e 64 20 74 68 65 ed.** up and the
526e0 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
526f0 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f pages in the so
52700 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69 urce database fi
52710 6c 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 le..** The sqlit
52720 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e e3_backup_remain
52730 69 6e 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 ing() and sqlite
52740 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 3_backup_pagecou
52750 6e 74 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a nt() interfaces.
52760 2a 2a 20 72 65 74 72 69 65 76 65 20 74 68 65 73 ** retrieve thes
52770 65 20 74 77 6f 20 76 61 6c 75 65 73 2c 20 72 65 e two values, re
52780 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a spectively..**.*
52790 2a 20 5e 54 68 65 20 76 61 6c 75 65 73 20 72 65 * ^The values re
527a0 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 65 20 turned by these
527b0 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6f 6e functions are on
527c0 6c 79 20 75 70 64 61 74 65 64 20 62 79 0a 2a 2a ly updated by.**
527d0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
527e0 73 74 65 70 28 29 2e 20 5e 49 66 20 74 68 65 20 step(). ^If the
527f0 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 source database
52800 69 73 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69 is modified duri
52810 6e 67 20 61 20 62 61 63 6b 75 70 0a 2a 2a 20 6f ng a backup.** o
52820 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 peration, then t
52830 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e 6f he values are no
52840 74 20 75 70 64 61 74 65 64 20 74 6f 20 61 63 63 t updated to acc
52850 6f 75 6e 74 20 66 6f 72 20 61 6e 79 20 65 78 74 ount for any ext
52860 72 61 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 74 ra.** pages that
52870 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 need to be upda
52880 74 65 64 20 6f 72 20 74 68 65 20 73 69 7a 65 20 ted or the size
52890 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 of the source da
528a0 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 63 tabase file.** c
528b0 68 61 6e 67 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 3c hanging..**.** <
528c0 62 3e 43 6f 6e 63 75 72 72 65 6e 74 20 55 73 61 b>Concurrent Usa
528d0 67 65 20 6f 66 20 44 61 74 61 62 61 73 65 20 48 ge of Database H
528e0 61 6e 64 6c 65 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a andles</b>.**.**
528f0 20 5e 54 68 65 20 73 6f 75 72 63 65 20 5b 64 61 ^The source [da
52900 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
52910 6e 5d 20 6d 61 79 20 62 65 20 75 73 65 64 20 62 n] may be used b
52920 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f y the applicatio
52930 6e 20 66 6f 72 20 6f 74 68 65 72 0a 2a 2a 20 70 n for other.** p
52940 75 72 70 6f 73 65 73 20 77 68 69 6c 65 20 61 20 urposes while a
52950 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
52960 20 69 73 20 75 6e 64 65 72 77 61 79 20 6f 72 20 is underway or
52970 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 being initialize
52980 64 2e 0a 2a 2a 20 5e 49 66 20 53 51 4c 69 74 65 d..** ^If SQLite
52990 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 61 6e 64 is compiled and
529a0 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 73 configured to s
529b0 75 70 70 6f 72 74 20 74 68 72 65 61 64 73 61 66 upport threadsaf
529c0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f e database.** co
529d0 6e 6e 65 63 74 69 6f 6e 73 2c 20 74 68 65 6e 20 nnections, then
529e0 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
529f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d ase connection m
52a00 61 79 20 62 65 20 75 73 65 64 20 63 6f 6e 63 75 ay be used concu
52a10 72 72 65 6e 74 6c 79 0a 2a 2a 20 66 72 6f 6d 20 rrently.** from
52a20 77 69 74 68 69 6e 20 6f 74 68 65 72 20 74 68 72 within other thr
52a30 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 eads..**.** Howe
52a40 76 65 72 2c 20 74 68 65 20 61 70 70 6c 69 63 61 ver, the applica
52a50 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61 6e tion must guaran
52a60 74 65 65 20 74 68 61 74 20 74 68 65 20 64 65 73 tee that the des
52a70 74 69 6e 61 74 69 6f 6e 20 0a 2a 2a 20 5b 64 61 tination .** [da
52a80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
52a90 6e 5d 20 69 73 20 6e 6f 74 20 70 61 73 73 65 64 n] is not passed
52aa0 20 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 41 50 to any other AP
52ab0 49 20 28 62 79 20 61 6e 79 20 74 68 72 65 61 64 I (by any thread
52ac0 29 20 61 66 74 65 72 20 0a 2a 2a 20 73 71 6c 69 ) after .** sqli
52ad0 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 te3_backup_init(
52ae0 29 20 69 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 ) is called and
52af0 62 65 66 6f 72 65 20 74 68 65 20 63 6f 72 72 65 before the corre
52b00 73 70 6f 6e 64 69 6e 67 20 63 61 6c 6c 20 74 6f sponding call to
52b10 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b .** sqlite3_back
52b20 75 70 5f 66 69 6e 69 73 68 28 29 2e 20 20 53 51 up_finish(). SQ
52b30 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 63 75 Lite does not cu
52b40 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 20 74 6f rrently check to
52b50 20 73 65 65 0a 2a 2a 20 69 66 20 74 68 65 20 61 see.** if the a
52b60 70 70 6c 69 63 61 74 69 6f 6e 20 69 6e 63 6f 72 pplication incor
52b70 72 65 63 74 6c 79 20 61 63 63 65 73 73 65 73 20 rectly accesses
52b80 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
52b90 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
52ba0 74 69 6f 6e 5d 0a 2a 2a 20 61 6e 64 20 73 6f 20 tion].** and so
52bb0 6e 6f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 no error code is
52bc0 20 72 65 70 6f 72 74 65 64 2c 20 62 75 74 20 74 reported, but t
52bd0 68 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 6d 61 he operations ma
52be0 79 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 0a 2a 2a y malfunction.**
52bf0 20 6e 65 76 65 72 74 68 65 6c 65 73 73 2e 20 20 nevertheless.
52c00 55 73 65 20 6f 66 20 74 68 65 20 64 65 73 74 69 Use of the desti
52c10 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
52c20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 connection while
52c30 20 61 0a 2a 2a 20 62 61 63 6b 75 70 20 69 73 20 a.** backup is
52c40 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 69 67 68 in progress migh
52c50 74 20 61 6c 73 6f 20 61 6c 73 6f 20 63 61 75 73 t also also caus
52c60 65 20 61 20 6d 75 74 65 78 20 64 65 61 64 6c 6f e a mutex deadlo
52c70 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 75 6e ck..**.** If run
52c80 6e 69 6e 67 20 69 6e 20 5b 73 68 61 72 65 64 20 ning in [shared
52c90 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 74 68 65 cache mode], the
52ca0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 application mus
52cb0 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 t.** guarantee t
52cc0 68 61 74 20 74 68 65 20 73 68 61 72 65 64 20 63 hat the shared c
52cd0 61 63 68 65 20 75 73 65 64 20 62 79 20 74 68 65 ache used by the
52ce0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
52cf0 61 62 61 73 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 abase.** is not
52d00 61 63 63 65 73 73 65 64 20 77 68 69 6c 65 20 74 accessed while t
52d10 68 65 20 62 61 63 6b 75 70 20 69 73 20 72 75 6e he backup is run
52d20 6e 69 6e 67 2e 20 49 6e 20 70 72 61 63 74 69 63 ning. In practic
52d30 65 20 74 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20 e this means.**
52d40 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 63 61 that the applica
52d50 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61 6e tion must guaran
52d60 74 65 65 20 74 68 61 74 20 74 68 65 20 64 69 73 tee that the dis
52d70 6b 20 66 69 6c 65 20 62 65 69 6e 67 20 0a 2a 2a k file being .**
52d80 20 62 61 63 6b 65 64 20 75 70 20 74 6f 20 69 73 backed up to is
52d90 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20 62 79 not accessed by
52da0 20 61 6e 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 any connection
52db0 77 69 74 68 69 6e 20 74 68 65 20 70 72 6f 63 65 within the proce
52dc0 73 73 2c 0a 2a 2a 20 6e 6f 74 20 6a 75 73 74 20 ss,.** not just
52dd0 74 68 65 20 73 70 65 63 69 66 69 63 20 63 6f 6e the specific con
52de0 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 77 61 73 nection that was
52df0 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 passed to sqlit
52e00 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 e3_backup_init()
52e10 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c ..**.** The [sql
52e20 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a ite3_backup] obj
52e30 65 63 74 20 69 74 73 65 6c 66 20 69 73 20 70 61 ect itself is pa
52e40 72 74 69 61 6c 6c 79 20 74 68 72 65 61 64 73 61 rtially threadsa
52e50 66 65 2e 20 4d 75 6c 74 69 70 6c 65 20 0a 2a 2a fe. Multiple .**
52e60 20 74 68 72 65 61 64 73 20 6d 61 79 20 73 61 66 threads may saf
52e70 65 6c 79 20 6d 61 6b 65 20 6d 75 6c 74 69 70 6c ely make multipl
52e80 65 20 63 6f 6e 63 75 72 72 65 6e 74 20 63 61 6c e concurrent cal
52e90 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 ls to sqlite3_ba
52ea0 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2a 20 ckup_step()..**
52eb0 48 6f 77 65 76 65 72 2c 20 74 68 65 20 73 71 6c However, the sql
52ec0 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 ite3_backup_rema
52ed0 69 6e 69 6e 67 28 29 20 61 6e 64 20 73 71 6c 69 ining() and sqli
52ee0 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 te3_backup_pagec
52ef0 6f 75 6e 74 28 29 0a 2a 2a 20 41 50 49 73 20 61 ount().** APIs a
52f00 72 65 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 re not strictly
52f10 73 70 65 61 6b 69 6e 67 20 74 68 72 65 61 64 73 speaking threads
52f20 61 66 65 2e 20 49 66 20 74 68 65 79 20 61 72 65 afe. If they are
52f30 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 0a invoked at the.
52f40 2a 2a 20 73 61 6d 65 20 74 69 6d 65 20 61 73 20 ** same time as
52f50 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 another thread i
52f60 73 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 s invoking sqlit
52f70 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
52f80 20 69 74 20 69 73 0a 2a 2a 20 70 6f 73 73 69 62 it is.** possib
52f90 6c 65 20 74 68 61 74 20 74 68 65 79 20 72 65 74 le that they ret
52fa0 75 72 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c 75 urn invalid valu
52fb0 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 es..*/.SQLITE_AP
52fc0 49 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 I sqlite3_backup
52fd0 20 2a 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 *sqlite3_backup
52fe0 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 _init(. sqlite3
52ff0 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 20 20 *pDest,
53000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
53010 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 /* Destination d
53020 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a atabase handle *
53030 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
53040 7a 44 65 73 74 4e 61 6d 65 2c 20 20 20 20 20 20 zDestName,
53050 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
53060 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 stination databa
53070 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 71 6c se name */. sql
53080 69 74 65 33 20 2a 70 53 6f 75 72 63 65 2c 20 20 ite3 *pSource,
53090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
530a0 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 /* Source da
530b0 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f tabase handle */
530c0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
530d0 53 6f 75 72 63 65 4e 61 6d 65 20 20 20 20 20 20 SourceName
530e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 /* Sou
530f0 72 63 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d rce database nam
53100 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 e */.);.SQLITE_A
53110 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
53120 61 63 6b 75 70 5f 73 74 65 70 28 73 71 6c 69 74 ackup_step(sqlit
53130 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69 6e e3_backup *p, in
53140 74 20 6e 50 61 67 65 29 3b 0a 53 51 4c 49 54 45 t nPage);.SQLITE
53150 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
53160 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 73 _backup_finish(s
53170 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 qlite3_backup *p
53180 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
53190 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 t sqlite3_backup
531a0 5f 72 65 6d 61 69 6e 69 6e 67 28 73 71 6c 69 74 _remaining(sqlit
531b0 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 53 e3_backup *p);.S
531c0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
531d0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 lite3_backup_pag
531e0 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 ecount(sqlite3_b
531f0 61 63 6b 75 70 20 2a 70 29 3b 0a 0a 2f 2a 0a 2a ackup *p);../*.*
53200 2a 20 43 41 50 49 33 52 45 46 3a 20 55 6e 6c 6f * CAPI3REF: Unlo
53210 63 6b 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 0a ck Notification.
53220 2a 2a 0a 2a 2a 20 5e 57 68 65 6e 20 72 75 6e 6e **.** ^When runn
53230 69 6e 67 20 69 6e 20 73 68 61 72 65 64 2d 63 61 ing in shared-ca
53240 63 68 65 20 6d 6f 64 65 2c 20 61 20 64 61 74 61 che mode, a data
53250 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 6d base operation m
53260 61 79 20 66 61 69 6c 20 77 69 74 68 0a 2a 2a 20 ay fail with.**
53270 61 6e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 an [SQLITE_LOCKE
53280 44 5d 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 D] error if the
53290 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 6f required locks o
532a0 6e 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 n the shared-cac
532b0 68 65 20 6f 72 0a 2a 2a 20 69 6e 64 69 76 69 64 he or.** individ
532c0 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68 69 ual tables withi
532d0 6e 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 n the shared-cac
532e0 68 65 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 he cannot be obt
532f0 61 69 6e 65 64 2e 20 53 65 65 0a 2a 2a 20 5b 53 ained. See.** [S
53300 51 4c 69 74 65 20 53 68 61 72 65 64 2d 43 61 63 QLite Shared-Cac
53310 68 65 20 4d 6f 64 65 5d 20 66 6f 72 20 61 20 64 he Mode] for a d
53320 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 73 68 escription of sh
53330 61 72 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b 69 ared-cache locki
53340 6e 67 2e 20 0a 2a 2a 20 5e 54 68 69 73 20 41 50 ng. .** ^This AP
53350 49 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f I may be used to
53360 20 72 65 67 69 73 74 65 72 20 61 20 63 61 6c 6c register a call
53370 62 61 63 6b 20 74 68 61 74 20 53 51 4c 69 74 65 back that SQLite
53380 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 0a 2a 2a will invoke .**
53390 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65 63 when the connec
533a0 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 68 tion currently h
533b0 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 71 75 69 olding the requi
533c0 72 65 64 20 6c 6f 63 6b 20 72 65 6c 69 6e 71 75 red lock relinqu
533d0 69 73 68 65 73 20 69 74 2e 0a 2a 2a 20 5e 54 68 ishes it..** ^Th
533e0 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20 61 is API is only a
533f0 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 vailable if the
53400 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 library was comp
53410 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a iled with the.**
53420 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f [SQLITE_ENABLE_
53430 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 5d 20 43 UNLOCK_NOTIFY] C
53440 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73 79 -preprocessor sy
53450 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2a mbol defined..**
53460 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55 .** See Also: [U
53470 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20 sing the SQLite
53480 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69 63 61 74 Unlock Notificat
53490 69 6f 6e 20 46 65 61 74 75 72 65 5d 2e 0a 2a 2a ion Feature]..**
534a0 0a 2a 2a 20 5e 53 68 61 72 65 64 2d 63 61 63 68 .** ^Shared-cach
534b0 65 20 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 e locks are rele
534c0 61 73 65 64 20 77 68 65 6e 20 61 20 64 61 74 61 ased when a data
534d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
534e0 63 6f 6e 63 6c 75 64 65 73 0a 2a 2a 20 69 74 73 concludes.** its
534f0 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 current transac
53500 74 69 6f 6e 2c 20 65 69 74 68 65 72 20 62 79 20 tion, either by
53510 63 6f 6d 6d 69 74 74 69 6e 67 20 69 74 20 6f 72 committing it or
53520 20 72 6f 6c 6c 69 6e 67 20 69 74 20 62 61 63 6b rolling it back
53530 2e 20 0a 2a 2a 0a 2a 2a 20 5e 57 68 65 6e 20 61 . .**.** ^When a
53540 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 6b 6e 6f connection (kno
53550 77 6e 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 65 wn as the blocke
53560 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 66 61 d connection) fa
53570 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a ils to obtain a.
53580 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 ** shared-cache
53590 6c 6f 63 6b 20 61 6e 64 20 53 51 4c 49 54 45 5f lock and SQLITE_
535a0 4c 4f 43 4b 45 44 20 69 73 20 72 65 74 75 72 6e LOCKED is return
535b0 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 ed to the caller
535c0 2c 20 74 68 65 0a 2a 2a 20 69 64 65 6e 74 69 74 , the.** identit
535d0 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 y of the databas
535e0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 74 68 e connection (th
535f0 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 e blocking conne
53600 63 74 69 6f 6e 29 20 74 68 61 74 0a 2a 2a 20 68 ction) that.** h
53610 61 73 20 6c 6f 63 6b 65 64 20 74 68 65 20 72 65 as locked the re
53620 71 75 69 72 65 64 20 72 65 73 6f 75 72 63 65 20 quired resource
53630 69 73 20 73 74 6f 72 65 64 20 69 6e 74 65 72 6e is stored intern
53640 61 6c 6c 79 2e 20 5e 41 66 74 65 72 20 61 6e 20 ally. ^After an
53650 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 .** application
53660 72 65 63 65 69 76 65 73 20 61 6e 20 53 51 4c 49 receives an SQLI
53670 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72 2c TE_LOCKED error,
53680 20 69 74 20 6d 61 79 20 63 61 6c 6c 20 74 68 65 it may call the
53690 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f .** sqlite3_unlo
536a0 63 6b 5f 6e 6f 74 69 66 79 28 29 20 6d 65 74 68 ck_notify() meth
536b0 6f 64 20 77 69 74 68 20 74 68 65 20 62 6c 6f 63 od with the bloc
536c0 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 ked connection h
536d0 61 6e 64 6c 65 20 61 73 20 0a 2a 2a 20 74 68 65 andle as .** the
536e0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
536f0 74 6f 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 to register for
53700 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 a callback that
53710 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 0a will be invoked.
53720 2a 2a 20 77 68 65 6e 20 74 68 65 20 62 6c 6f 63 ** when the bloc
53730 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 king connections
53740 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 current transac
53750 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 tion is conclude
53760 64 2e 20 5e 54 68 65 0a 2a 2a 20 63 61 6c 6c 62 d. ^The.** callb
53770 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 ack is invoked f
53780 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 5b rom within the [
53790 73 71 6c 69 74 65 33 5f 73 74 65 70 5d 20 6f 72 sqlite3_step] or
537a0 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5d [sqlite3_close]
537b0 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20 63 6f .** call that co
537c0 6e 63 6c 75 64 65 73 20 74 68 65 20 62 6c 6f 63 ncludes the bloc
537d0 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 king connections
537e0 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a transaction..**
537f0 0a 2a 2a 20 5e 28 49 66 20 73 71 6c 69 74 65 33 .** ^(If sqlite3
53800 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
53810 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 is called in a
53820 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 multi-threaded a
53830 70 70 6c 69 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 pplication,.** t
53840 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 here is a chance
53850 20 74 68 61 74 20 74 68 65 20 62 6c 6f 63 6b 69 that the blocki
53860 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 ng connection wi
53870 6c 6c 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a ll have already.
53880 2a 2a 20 63 6f 6e 63 6c 75 64 65 64 20 69 74 73 ** concluded its
53890 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 79 20 transaction by
538a0 74 68 65 20 74 69 6d 65 20 73 71 6c 69 74 65 33 the time sqlite3
538b0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
538c0 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 is invoked..**
538d0 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c If this happens,
538e0 20 74 68 65 6e 20 74 68 65 20 73 70 65 63 69 66 then the specif
538f0 69 65 64 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 ied callback is
53900 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 invoked immediat
53910 65 6c 79 2c 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 ely,.** from wit
53920 68 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 hin the call to
53930 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e sqlite3_unlock_n
53940 6f 74 69 66 79 28 29 2e 29 5e 0a 2a 2a 0a 2a 2a otify().)^.**.**
53950 20 5e 49 66 20 74 68 65 20 62 6c 6f 63 6b 65 64 ^If the blocked
53960 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 61 connection is a
53970 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f 62 74 ttempting to obt
53980 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b ain a write-lock
53990 20 6f 6e 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d on a.** shared-
539a0 63 61 63 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 cache table, and
539b0 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f more than one o
539c0 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 ther connection
539d0 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 73 0a currently holds.
539e0 2a 2a 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f ** a read-lock o
539f0 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 n the same table
53a00 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 61 72 , then SQLite ar
53a10 62 69 74 72 61 72 69 6c 79 20 73 65 6c 65 63 74 bitrarily select
53a20 73 20 6f 6e 65 20 6f 66 20 0a 2a 2a 20 74 68 65 s one of .** the
53a30 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f other connectio
53a40 6e 73 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 ns to use as the
53a50 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 blocking connec
53a60 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 tion..**.** ^(Th
53a70 65 72 65 20 6d 61 79 20 62 65 20 61 74 20 6d 6f ere may be at mo
53a80 73 74 20 6f 6e 65 20 75 6e 6c 6f 63 6b 2d 6e 6f st one unlock-no
53a90 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 tify callback re
53aa0 67 69 73 74 65 72 65 64 20 62 79 20 61 20 0a 2a gistered by a .*
53ab0 2a 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 * blocked connec
53ac0 74 69 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65 33 tion. If sqlite3
53ad0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
53ae0 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 is called when
53af0 74 68 65 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20 63 the.** blocked c
53b00 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 64 onnection alread
53b10 79 20 68 61 73 20 61 20 72 65 67 69 73 74 65 72 y has a register
53b20 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 ed unlock-notify
53b30 20 63 61 6c 6c 62 61 63 6b 2c 0a 2a 2a 20 74 68 callback,.** th
53b40 65 6e 20 74 68 65 20 6e 65 77 20 63 61 6c 6c 62 en the new callb
53b50 61 63 6b 20 72 65 70 6c 61 63 65 73 20 74 68 65 ack replaces the
53b60 20 6f 6c 64 2e 29 5e 20 5e 49 66 20 73 71 6c 69 old.)^ ^If sqli
53b70 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 te3_unlock_notif
53b80 79 28 29 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 y() is.** called
53b90 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 with a NULL poi
53ba0 6e 74 65 72 20 61 73 20 69 74 73 20 73 65 63 6f nter as its seco
53bb0 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 74 68 65 nd argument, the
53bc0 6e 20 61 6e 79 20 65 78 69 73 74 69 6e 67 0a 2a n any existing.*
53bd0 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 * unlock-notify
53be0 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 61 6e 63 callback is canc
53bf0 65 6c 65 64 2e 20 5e 54 68 65 20 62 6c 6f 63 6b eled. ^The block
53c00 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 0a ed connections .
53c10 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 ** unlock-notify
53c20 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61 6c callback may al
53c30 73 6f 20 62 65 20 63 61 6e 63 65 6c 65 64 20 62 so be canceled b
53c40 79 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 62 6c y closing the bl
53c50 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 6e 65 63 74 ocked.** connect
53c60 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ion using [sqlit
53c70 65 33 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a e3_close()]..**.
53c80 2a 2a 20 54 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f ** The unlock-no
53c90 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 tify callback is
53ca0 20 6e 6f 74 20 72 65 65 6e 74 72 61 6e 74 2e 20 not reentrant.
53cb0 49 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f If an applicatio
53cc0 6e 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 61 6e 79 n invokes.** any
53cd0 20 73 71 6c 69 74 65 33 5f 78 78 78 20 41 50 49 sqlite3_xxx API
53ce0 20 66 75 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20 functions from
53cf0 77 69 74 68 69 6e 20 61 6e 20 75 6e 6c 6f 63 6b within an unlock
53d00 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
53d10 2c 20 61 0a 2a 2a 20 63 72 61 73 68 20 6f 72 20 , a.** crash or
53d20 64 65 61 64 6c 6f 63 6b 20 6d 61 79 20 62 65 20 deadlock may be
53d30 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a the result..**.*
53d40 2a 20 5e 55 6e 6c 65 73 73 20 64 65 61 64 6c 6f * ^Unless deadlo
53d50 63 6b 20 69 73 20 64 65 74 65 63 74 65 64 20 28 ck is detected (
53d60 73 65 65 20 62 65 6c 6f 77 29 2c 20 73 71 6c 69 see below), sqli
53d70 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 te3_unlock_notif
53d80 79 28 29 20 61 6c 77 61 79 73 0a 2a 2a 20 72 65 y() always.** re
53d90 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e turns SQLITE_OK.
53da0 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 61 6c 6c 62 61 .**.** <b>Callba
53db0 63 6b 20 49 6e 76 6f 63 61 74 69 6f 6e 20 44 65 ck Invocation De
53dc0 74 61 69 6c 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 tails</b>.**.**
53dd0 57 68 65 6e 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e When an unlock-n
53de0 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 otify callback i
53df0 73 20 72 65 67 69 73 74 65 72 65 64 2c 20 74 68 s registered, th
53e00 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 70 72 e application pr
53e10 6f 76 69 64 65 73 20 61 20 0a 2a 2a 20 73 69 6e ovides a .** sin
53e20 67 6c 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 gle void* pointe
53e30 72 20 74 68 61 74 20 69 73 20 70 61 73 73 65 64 r that is passed
53e40 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b to the callback
53e50 20 77 68 65 6e 20 69 74 20 69 73 20 69 6e 76 6f when it is invo
53e60 6b 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c ked..** However,
53e70 20 74 68 65 20 73 69 67 6e 61 74 75 72 65 20 6f the signature o
53e80 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 f the callback f
53e90 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 77 73 20 53 unction allows S
53ea0 51 4c 69 74 65 20 74 6f 20 70 61 73 73 0a 2a 2a QLite to pass.**
53eb0 20 69 74 20 61 6e 20 61 72 72 61 79 20 6f 66 20 it an array of
53ec0 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20 70 6f void* context po
53ed0 69 6e 74 65 72 73 2e 20 54 68 65 20 66 69 72 73 inters. The firs
53ee0 74 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 t argument passe
53ef0 64 20 74 6f 0a 2a 2a 20 61 6e 20 75 6e 6c 6f 63 d to.** an unloc
53f00 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 k-notify callbac
53f10 6b 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 k is a pointer t
53f20 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 76 6f o an array of vo
53f30 69 64 2a 20 70 6f 69 6e 74 65 72 73 2c 0a 2a 2a id* pointers,.**
53f40 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 and the second
53f50 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
53f60 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 entries in the
53f70 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 array..**.** Whe
53f80 6e 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e n a blocking con
53f90 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 nections transac
53fa0 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 tion is conclude
53fb0 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 65 0a d, there may be.
53fc0 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 ** more than one
53fd0 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 blocked connect
53fe0 69 6f 6e 20 74 68 61 74 20 68 61 73 20 72 65 67 ion that has reg
53ff0 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 istered for an u
54000 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20 nlock-notify.**
54010 63 61 6c 6c 62 61 63 6b 2e 20 5e 49 66 20 74 77 callback. ^If tw
54020 6f 20 6f 72 20 6d 6f 72 65 20 73 75 63 68 20 62 o or more such b
54030 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f locked connectio
54040 6e 73 20 68 61 76 65 20 73 70 65 63 69 66 69 65 ns have specifie
54050 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 61 d the.** same ca
54060 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2c llback function,
54070 20 74 68 65 6e 20 69 6e 73 74 65 61 64 20 6f 66 then instead of
54080 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 61 invoking the ca
54090 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a llback function.
540a0 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 ** multiple time
540b0 73 2c 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 s, it is invoked
540c0 20 6f 6e 63 65 20 77 69 74 68 20 74 68 65 20 73 once with the s
540d0 65 74 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e 74 et of void* cont
540e0 65 78 74 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20 ext pointers.**
540f0 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 specified by the
54100 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 blocked connect
54110 69 6f 6e 73 20 62 75 6e 64 6c 65 64 20 74 6f 67 ions bundled tog
54120 65 74 68 65 72 20 69 6e 74 6f 20 61 6e 20 61 72 ether into an ar
54130 72 61 79 2e 0a 2a 2a 20 54 68 69 73 20 67 69 76 ray..** This giv
54140 65 73 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 es the applicati
54150 6f 6e 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 on an opportunit
54160 79 20 74 6f 20 70 72 69 6f 72 69 74 69 7a 65 20 y to prioritize
54170 61 6e 79 20 61 63 74 69 6f 6e 73 20 0a 2a 2a 20 any actions .**
54180 72 65 6c 61 74 65 64 20 74 6f 20 74 68 65 20 73 related to the s
54190 65 74 20 6f 66 20 75 6e 62 6c 6f 63 6b 65 64 20 et of unblocked
541a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
541b0 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 44 ions..**.** <b>D
541c0 65 61 64 6c 6f 63 6b 20 44 65 74 65 63 74 69 6f eadlock Detectio
541d0 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 41 73 73 75 n</b>.**.** Assu
541e0 6d 69 6e 67 20 74 68 61 74 20 61 66 74 65 72 20 ming that after
541f0 72 65 67 69 73 74 65 72 69 6e 67 20 66 6f 72 20 registering for
54200 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 an unlock-notify
54210 20 63 61 6c 6c 62 61 63 6b 20 61 20 0a 2a 2a 20 callback a .**
54220 64 61 74 61 62 61 73 65 20 77 61 69 74 73 20 66 database waits f
54230 6f 72 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 or the callback
54240 74 6f 20 62 65 20 69 73 73 75 65 64 20 62 65 66 to be issued bef
54250 6f 72 65 20 74 61 6b 69 6e 67 20 61 6e 79 20 66 ore taking any f
54260 75 72 74 68 65 72 0a 2a 2a 20 61 63 74 69 6f 6e urther.** action
54270 20 28 61 20 72 65 61 73 6f 6e 61 62 6c 65 20 61 (a reasonable a
54280 73 73 75 6d 70 74 69 6f 6e 29 2c 20 74 68 65 6e ssumption), then
54290 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49 20 using this API
542a0 6d 61 79 20 63 61 75 73 65 20 74 68 65 0a 2a 2a may cause the.**
542b0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 application to
542c0 64 65 61 64 6c 6f 63 6b 2e 20 46 6f 72 20 65 78 deadlock. For ex
542d0 61 6d 70 6c 65 2c 20 69 66 20 63 6f 6e 6e 65 63 ample, if connec
542e0 74 69 6f 6e 20 58 20 69 73 20 77 61 69 74 69 6e tion X is waitin
542f0 67 20 66 6f 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 g for.** connect
54300 69 6f 6e 20 59 27 73 20 74 72 61 6e 73 61 63 74 ion Y's transact
54310 69 6f 6e 20 74 6f 20 62 65 20 63 6f 6e 63 6c 75 ion to be conclu
54320 64 65 64 2c 20 61 6e 64 20 73 69 6d 69 6c 61 72 ded, and similar
54330 6c 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a ly connection.**
54340 20 59 20 69 73 20 77 61 69 74 69 6e 67 20 6f 6e Y is waiting on
54350 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 27 73 20 connection X's
54360 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 transaction, the
54370 6e 20 6e 65 69 74 68 65 72 20 63 6f 6e 6e 65 63 n neither connec
54380 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 70 72 6f tion.** will pro
54390 63 65 65 64 20 61 6e 64 20 74 68 65 20 73 79 73 ceed and the sys
543a0 74 65 6d 20 6d 61 79 20 72 65 6d 61 69 6e 20 64 tem may remain d
543b0 65 61 64 6c 6f 63 6b 65 64 20 69 6e 64 65 66 69 eadlocked indefi
543c0 6e 69 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 6f nitely..**.** To
543d0 20 61 76 6f 69 64 20 74 68 69 73 20 73 63 65 6e avoid this scen
543e0 61 72 69 6f 2c 20 74 68 65 20 73 71 6c 69 74 65 ario, the sqlite
543f0 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 3_unlock_notify(
54400 29 20 70 65 72 66 6f 72 6d 73 20 64 65 61 64 6c ) performs deadl
54410 6f 63 6b 0a 2a 2a 20 64 65 74 65 63 74 69 6f 6e ock.** detection
54420 2e 20 5e 49 66 20 61 20 67 69 76 65 6e 20 63 61 . ^If a given ca
54430 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e ll to sqlite3_un
54440 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 77 6f lock_notify() wo
54450 75 6c 64 20 70 75 74 20 74 68 65 0a 2a 2a 20 73 uld put the.** s
54460 79 73 74 65 6d 20 69 6e 20 61 20 64 65 61 64 6c ystem in a deadl
54470 6f 63 6b 65 64 20 73 74 61 74 65 2c 20 74 68 65 ocked state, the
54480 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 n SQLITE_LOCKED
54490 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
544a0 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 no.** unlock-not
544b0 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 ify callback is
544c0 72 65 67 69 73 74 65 72 65 64 2e 20 54 68 65 20 registered. The
544d0 73 79 73 74 65 6d 20 69 73 20 73 61 69 64 20 74 system is said t
544e0 6f 20 62 65 20 69 6e 0a 2a 2a 20 61 20 64 65 61 o be in.** a dea
544f0 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 20 69 66 dlocked state if
54500 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 41 20 68 61 connection A ha
54510 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 s registered for
54520 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 an unlock-notif
54530 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e y.** callback on
54540 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 the conclusion
54550 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42 27 of connection B'
54560 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 s transaction, a
54570 6e 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a nd connection.**
54580 20 42 20 68 61 73 20 69 74 73 65 6c 66 20 72 65 B has itself re
54590 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 gistered for an
545a0 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
545b0 6c 6c 62 61 63 6b 20 77 68 65 6e 20 63 6f 6e 6e llback when conn
545c0 65 63 74 69 6f 6e 0a 2a 2a 20 41 27 73 20 74 72 ection.** A's tr
545d0 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e ansaction is con
545e0 63 6c 75 64 65 64 2e 20 5e 49 6e 64 69 72 65 63 cluded. ^Indirec
545f0 74 20 64 65 61 64 6c 6f 63 6b 20 69 73 20 61 6c t deadlock is al
54600 73 6f 20 64 65 74 65 63 74 65 64 2c 20 73 6f 0a so detected, so.
54610 2a 2a 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 ** the system is
54620 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65 64 also considered
54630 20 74 6f 20 62 65 20 64 65 61 64 6c 6f 63 6b 65 to be deadlocke
54640 64 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 d if connection
54650 42 20 68 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 B has.** registe
54660 72 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 red for an unloc
54670 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 k-notify callbac
54680 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73 k on the conclus
54690 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f ion of connectio
546a0 6e 0a 2a 2a 20 43 27 73 20 74 72 61 6e 73 61 63 n.** C's transac
546b0 74 69 6f 6e 2c 20 77 68 65 72 65 20 63 6f 6e 6e tion, where conn
546c0 65 63 74 69 6f 6e 20 43 20 69 73 20 77 61 69 74 ection C is wait
546d0 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f ing on connectio
546e0 6e 20 41 2e 20 5e 41 6e 79 0a 2a 2a 20 6e 75 6d n A. ^Any.** num
546f0 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66 ber of levels of
54700 20 69 6e 64 69 72 65 63 74 69 6f 6e 20 61 72 65 indirection are
54710 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a 2a 2a 20 allowed..**.**
54720 3c 62 3e 54 68 65 20 22 44 52 4f 50 20 54 41 42 <b>The "DROP TAB
54730 4c 45 22 20 45 78 63 65 70 74 69 6f 6e 3c 2f 62 LE" Exception</b
54740 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63 >.**.** When a c
54750 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
54760 73 74 65 70 28 29 5d 20 72 65 74 75 72 6e 73 20 step()] returns
54770 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20 69 SQLITE_LOCKED, i
54780 74 20 69 73 20 61 6c 6d 6f 73 74 20 0a 2a 2a 20 t is almost .**
54790 61 6c 77 61 79 73 20 61 70 70 72 6f 70 72 69 61 always appropria
547a0 74 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 te to call sqlit
547b0 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 e3_unlock_notify
547c0 28 29 2e 20 54 68 65 72 65 20 69 73 20 68 6f 77 (). There is how
547d0 65 76 65 72 2c 0a 2a 2a 20 6f 6e 65 20 65 78 63 ever,.** one exc
547e0 65 70 74 69 6f 6e 2e 20 57 68 65 6e 20 65 78 65 eption. When exe
547f0 63 75 74 69 6e 67 20 61 20 22 44 52 4f 50 20 54 cuting a "DROP T
54800 41 42 4c 45 22 20 6f 72 20 22 44 52 4f 50 20 49 ABLE" or "DROP I
54810 4e 44 45 58 22 20 73 74 61 74 65 6d 65 6e 74 2c NDEX" statement,
54820 0a 2a 2a 20 53 51 4c 69 74 65 20 63 68 65 63 6b .** SQLite check
54830 73 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 s if there are a
54840 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 ny currently exe
54850 63 75 74 69 6e 67 20 53 45 4c 45 43 54 20 73 74 cuting SELECT st
54860 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 atements.** that
54870 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 65 20 73 belong to the s
54880 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 ame connection.
54890 49 66 20 74 68 65 72 65 20 61 72 65 2c 20 53 51 If there are, SQ
548a0 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 0a 2a LITE_LOCKED is.*
548b0 2a 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 * returned. In t
548c0 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20 69 his case there i
548d0 73 20 6e 6f 20 22 62 6c 6f 63 6b 69 6e 67 20 63 s no "blocking c
548e0 6f 6e 6e 65 63 74 69 6f 6e 22 2c 20 73 6f 20 69 onnection", so i
548f0 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 nvoking.** sqlit
54900 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 e3_unlock_notify
54910 28 29 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 () results in th
54920 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 e unlock-notify
54930 63 61 6c 6c 62 61 63 6b 20 62 65 69 6e 67 0a 2a callback being.*
54940 2a 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 * invoked immedi
54950 61 74 65 6c 79 2e 20 49 66 20 74 68 65 20 61 70 ately. If the ap
54960 70 6c 69 63 61 74 69 6f 6e 20 74 68 65 6e 20 72 plication then r
54970 65 2d 61 74 74 65 6d 70 74 73 20 74 68 65 20 22 e-attempts the "
54980 44 52 4f 50 20 54 41 42 4c 45 22 0a 2a 2a 20 6f DROP TABLE".** o
54990 72 20 22 44 52 4f 50 20 49 4e 44 45 58 22 20 71 r "DROP INDEX" q
549a0 75 65 72 79 2c 20 61 6e 20 69 6e 66 69 6e 69 74 uery, an infinit
549b0 65 20 6c 6f 6f 70 20 6d 69 67 68 74 20 62 65 20 e loop might be
549c0 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a the result..**.*
549d0 2a 20 4f 6e 65 20 77 61 79 20 61 72 6f 75 6e 64 * One way around
549e0 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 69 73 this problem is
549f0 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 65 78 to check the ex
54a00 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 tended error cod
54a10 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 e returned.** by
54a20 20 61 6e 20 73 71 6c 69 74 65 33 5f 73 74 65 70 an sqlite3_step
54a30 28 29 20 63 61 6c 6c 2e 20 5e 28 49 66 20 74 68 () call. ^(If th
54a40 65 72 65 20 69 73 20 61 20 62 6c 6f 63 6b 69 6e ere is a blockin
54a50 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 g connection, th
54a60 65 6e 20 74 68 65 0a 2a 2a 20 65 78 74 65 6e 64 en the.** extend
54a70 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 ed error code is
54a80 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c set to SQLITE_L
54a90 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 OCKED_SHAREDCACH
54aa0 45 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e E. Otherwise, in
54ab0 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 .** the special
54ac0 22 44 52 4f 50 20 54 41 42 4c 45 2f 49 4e 44 45 "DROP TABLE/INDE
54ad0 58 22 20 63 61 73 65 2c 20 74 68 65 20 65 78 74 X" case, the ext
54ae0 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 ended error code
54af0 20 69 73 20 6a 75 73 74 20 0a 2a 2a 20 53 51 4c is just .** SQL
54b00 49 54 45 5f 4c 4f 43 4b 45 44 2e 29 5e 0a 2a 2f ITE_LOCKED.)^.*/
54b10 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
54b20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e sqlite3_unlock_n
54b30 6f 74 69 66 79 28 0a 20 20 73 71 6c 69 74 65 33 otify(. sqlite3
54b40 20 2a 70 42 6c 6f 63 6b 65 64 2c 20 20 20 20 20 *pBlocked,
54b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54b60 20 20 20 20 20 2f 2a 20 57 61 69 74 69 6e 67 20 /* Waiting
54b70 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
54b80 76 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 void (*xNotify)(
54b90 76 6f 69 64 20 2a 2a 61 70 41 72 67 2c 20 69 6e void **apArg, in
54ba0 74 20 6e 41 72 67 29 2c 20 20 20 20 2f 2a 20 43 t nArg), /* C
54bb0 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
54bc0 20 74 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 to invoke */.
54bd0 76 6f 69 64 20 2a 70 4e 6f 74 69 66 79 41 72 67 void *pNotifyArg
54be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54bf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
54c00 72 67 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 rgument to pass
54c10 74 6f 20 78 4e 6f 74 69 66 79 20 2a 2f 0a 29 3b to xNotify */.);
54c20 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 .../*.** CAPI3RE
54c30 46 3a 20 53 74 72 69 6e 67 20 43 6f 6d 70 61 72 F: String Compar
54c40 69 73 6f 6e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 ison.**.** ^The
54c50 5b 73 71 6c 69 74 65 33 5f 73 74 72 69 63 6d 70 [sqlite3_stricmp
54c60 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
54c70 5f 73 74 72 6e 69 63 6d 70 28 29 5d 20 41 50 49 _strnicmp()] API
54c80 73 20 61 6c 6c 6f 77 20 61 70 70 6c 69 63 61 74 s allow applicat
54c90 69 6f 6e 73 0a 2a 2a 20 61 6e 64 20 65 78 74 65 ions.** and exte
54ca0 6e 73 69 6f 6e 73 20 74 6f 20 63 6f 6d 70 61 72 nsions to compar
54cb0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f e the contents o
54cc0 66 20 74 77 6f 20 62 75 66 66 65 72 73 20 63 6f f two buffers co
54cd0 6e 74 61 69 6e 69 6e 67 20 55 54 46 2d 38 0a 2a ntaining UTF-8.*
54ce0 2a 20 73 74 72 69 6e 67 73 20 69 6e 20 61 20 63 * strings in a c
54cf0 61 73 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20 ase-independent
54d00 66 61 73 68 69 6f 6e 2c 20 75 73 69 6e 67 20 74 fashion, using t
54d10 68 65 20 73 61 6d 65 20 64 65 66 69 6e 69 74 69 he same definiti
54d20 6f 6e 20 6f 66 20 22 63 61 73 65 0a 2a 2a 20 69 on of "case.** i
54d30 6e 64 65 70 65 6e 64 65 6e 63 65 22 20 74 68 61 ndependence" tha
54d40 74 20 53 51 4c 69 74 65 20 75 73 65 73 20 69 6e t SQLite uses in
54d50 74 65 72 6e 61 6c 6c 79 20 77 68 65 6e 20 63 6f ternally when co
54d60 6d 70 61 72 69 6e 67 20 69 64 65 6e 74 69 66 69 mparing identifi
54d70 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ers..*/.SQLITE_A
54d80 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 PI int sqlite3_s
54d90 74 72 69 63 6d 70 28 63 6f 6e 73 74 20 63 68 61 tricmp(const cha
54da0 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 r *, const char
54db0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
54dc0 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 nt sqlite3_strni
54dd0 63 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a cmp(const char *
54de0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 , const char *,
54df0 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 int);../*.** CAP
54e00 49 33 52 45 46 3a 20 45 72 72 6f 72 20 4c 6f 67 I3REF: Error Log
54e10 67 69 6e 67 20 49 6e 74 65 72 66 61 63 65 0a 2a ging Interface.*
54e20 2a 0a 2a 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74 *.** ^The [sqlit
54e30 65 33 5f 6c 6f 67 28 29 5d 20 69 6e 74 65 72 66 e3_log()] interf
54e40 61 63 65 20 77 72 69 74 65 73 20 61 20 6d 65 73 ace writes a mes
54e50 73 61 67 65 20 69 6e 74 6f 20 74 68 65 20 65 72 sage into the er
54e60 72 6f 72 20 6c 6f 67 0a 2a 2a 20 65 73 74 61 62 ror log.** estab
54e70 6c 69 73 68 65 64 20 62 79 20 74 68 65 20 5b 53 lished by the [S
54e80 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 47 QLITE_CONFIG_LOG
54e90 5d 20 6f 70 74 69 6f 6e 20 74 6f 20 5b 73 71 6c ] option to [sql
54ea0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a ite3_config()]..
54eb0 2a 2a 20 5e 49 66 20 6c 6f 67 67 69 6e 67 20 69 ** ^If logging i
54ec0 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 7a s enabled, the z
54ed0 46 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 61 6e Format string an
54ee0 64 20 73 75 62 73 65 71 75 65 6e 74 20 61 72 67 d subsequent arg
54ef0 75 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 75 73 uments are.** us
54f00 65 64 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33 ed with [sqlite3
54f10 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 _snprintf()] to
54f20 67 65 6e 65 72 61 74 65 20 74 68 65 20 66 69 6e generate the fin
54f30 61 6c 20 6f 75 74 70 75 74 20 73 74 72 69 6e 67 al output string
54f40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
54f50 74 65 33 5f 6c 6f 67 28 29 20 69 6e 74 65 72 66 te3_log() interf
54f60 61 63 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 ace is intended
54f70 66 6f 72 20 75 73 65 20 62 79 20 65 78 74 65 6e for use by exten
54f80 73 69 6f 6e 73 20 73 75 63 68 20 61 73 0a 2a 2a sions such as.**
54f90 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2c virtual tables,
54fa0 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 collating funct
54fb0 69 6f 6e 73 2c 20 61 6e 64 20 53 51 4c 20 66 75 ions, and SQL fu
54fc0 6e 63 74 69 6f 6e 73 2e 20 20 57 68 69 6c 65 20 nctions. While
54fd0 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 74 68 there is.** noth
54fe0 69 6e 67 20 74 6f 20 70 72 65 76 65 6e 74 20 61 ing to prevent a
54ff0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 72 n application fr
55000 6f 6d 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 om calling sqlit
55010 65 33 5f 6c 6f 67 28 29 2c 20 64 6f 69 6e 67 20 e3_log(), doing
55020 73 6f 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 so.** is conside
55030 72 65 64 20 62 61 64 20 66 6f 72 6d 2e 0a 2a 2a red bad form..**
55040 0a 2a 2a 20 54 68 65 20 7a 46 6f 72 6d 61 74 20 .** The zFormat
55050 73 74 72 69 6e 67 20 6d 75 73 74 20 6e 6f 74 20 string must not
55060 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 be NULL..**.** T
55070 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b o avoid deadlock
55080 73 20 61 6e 64 20 6f 74 68 65 72 20 74 68 72 65 s and other thre
55090 61 64 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2c 20 ading problems,
550a0 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 the sqlite3_log(
550b0 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 69 6c ) routine.** wil
550c0 6c 20 6e 6f 74 20 75 73 65 20 64 79 6e 61 6d 69 l not use dynami
550d0 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 cally allocated
550e0 6d 65 6d 6f 72 79 2e 20 20 54 68 65 20 6c 6f 67 memory. The log
550f0 20 6d 65 73 73 61 67 65 20 69 73 20 73 74 6f 72 message is stor
55100 65 64 20 69 6e 0a 2a 2a 20 61 20 66 69 78 65 64 ed in.** a fixed
55110 2d 6c 65 6e 67 74 68 20 62 75 66 66 65 72 20 6f -length buffer o
55120 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 49 66 n the stack. If
55130 20 74 68 65 20 6c 6f 67 20 6d 65 73 73 61 67 65 the log message
55140 20 69 73 20 6c 6f 6e 67 65 72 20 74 68 61 6e 0a is longer than.
55150 2a 2a 20 61 20 66 65 77 20 68 75 6e 64 72 65 64 ** a few hundred
55160 20 63 68 61 72 61 63 74 65 72 73 2c 20 69 74 20 characters, it
55170 77 69 6c 6c 20 62 65 20 74 72 75 6e 63 61 74 65 will be truncate
55180 64 20 74 6f 20 74 68 65 20 6c 65 6e 67 74 68 20 d to the length
55190 6f 66 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 of the.** buffer
551a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
551b0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6c 6f 67 void sqlite3_log
551c0 28 69 6e 74 20 69 45 72 72 43 6f 64 65 2c 20 63 (int iErrCode, c
551d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
551e0 61 74 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a at, ...);../*.**
551f0 20 43 41 50 49 33 52 45 46 3a 20 57 72 69 74 65 CAPI3REF: Write
55200 2d 41 68 65 61 64 20 4c 6f 67 20 43 6f 6d 6d 69 -Ahead Log Commi
55210 74 20 48 6f 6f 6b 0a 2a 2a 0a 2a 2a 20 5e 54 68 t Hook.**.** ^Th
55220 65 20 5b 73 71 6c 69 74 65 33 5f 77 61 6c 5f 68 e [sqlite3_wal_h
55230 6f 6f 6b 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 ook()] function
55240 69 73 20 75 73 65 64 20 74 6f 20 72 65 67 69 73 is used to regis
55250 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 ter a callback t
55260 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 69 hat.** will be i
55270 6e 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65 nvoked each time
55280 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e a database conn
55290 65 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 64 ection commits d
552a0 61 74 61 20 74 6f 20 61 0a 2a 2a 20 5b 77 72 69 ata to a.** [wri
552b0 74 65 2d 61 68 65 61 64 20 6c 6f 67 5d 20 28 69 te-ahead log] (i
552c0 2e 65 2e 20 77 68 65 6e 65 76 65 72 20 61 20 74 .e. whenever a t
552d0 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f ransaction is co
552e0 6d 6d 69 74 74 65 64 20 69 6e 0a 2a 2a 20 5b 6a mmitted in.** [j
552f0 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 7c 20 6a 6f ournal_mode | jo
55300 75 72 6e 61 6c 5f 6d 6f 64 65 3d 57 41 4c 20 6d urnal_mode=WAL m
55310 6f 64 65 5d 29 2e 20 0a 2a 2a 0a 2a 2a 20 5e 54 ode]). .**.** ^T
55320 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 he callback is i
55330 6e 76 6f 6b 65 64 20 62 79 20 53 51 4c 69 74 65 nvoked by SQLite
55340 20 61 66 74 65 72 20 74 68 65 20 63 6f 6d 6d 69 after the commi
55350 74 20 68 61 73 20 74 61 6b 65 6e 20 70 6c 61 63 t has taken plac
55360 65 20 61 6e 64 20 0a 2a 2a 20 74 68 65 20 61 73 e and .** the as
55370 73 6f 63 69 61 74 65 64 20 77 72 69 74 65 2d 6c sociated write-l
55380 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
55390 61 73 65 20 72 65 6c 65 61 73 65 64 2c 20 73 6f ase released, so
553a0 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
553b0 69 6f 6e 20 0a 2a 2a 20 6d 61 79 20 72 65 61 64 ion .** may read
553c0 2c 20 77 72 69 74 65 20 6f 72 20 5b 63 68 65 63 , write or [chec
553d0 6b 70 6f 69 6e 74 5d 20 74 68 65 20 64 61 74 61 kpoint] the data
553e0 62 61 73 65 20 61 73 20 72 65 71 75 69 72 65 64 base as required
553f0 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 72 ..**.** ^The fir
55400 73 74 20 70 61 72 61 6d 65 74 65 72 20 70 61 73 st parameter pas
55410 73 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 sed to the callb
55420 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 ack function whe
55430 6e 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 0a n it is invoked.
55440 2a 2a 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 ** is a copy of
55450 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 the third parame
55460 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 73 71 ter passed to sq
55470 6c 69 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29 lite3_wal_hook()
55480 20 77 68 65 6e 0a 2a 2a 20 72 65 67 69 73 74 65 when.** registe
55490 72 69 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63 ring the callbac
554a0 6b 2e 20 5e 54 68 65 20 73 65 63 6f 6e 64 20 69 k. ^The second i
554b0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 s a copy of the
554c0 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e database handle.
554d0 0a 2a 2a 20 5e 54 68 65 20 74 68 69 72 64 20 70 .** ^The third p
554e0 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 arameter is the
554f0 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
55500 62 61 73 65 20 74 68 61 74 20 77 61 73 20 77 72 base that was wr
55510 69 74 74 65 6e 20 74 6f 20 2d 0a 2a 2a 20 65 69 itten to -.** ei
55520 74 68 65 72 20 22 6d 61 69 6e 22 20 6f 72 20 74 ther "main" or t
55530 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 5b 41 he name of an [A
55540 54 54 41 43 48 5d 2d 65 64 20 64 61 74 61 62 61 TTACH]-ed databa
55550 73 65 2e 20 5e 54 68 65 20 66 6f 75 72 74 68 20 se. ^The fourth
55560 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20 parameter.** is
55570 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
55580 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e ges currently in
55590 20 74 68 65 20 77 72 69 74 65 2d 61 68 65 61 64 the write-ahead
555a0 20 6c 6f 67 20 66 69 6c 65 2c 0a 2a 2a 20 69 6e log file,.** in
555b0 63 6c 75 64 69 6e 67 20 74 68 6f 73 65 20 74 68 cluding those th
555c0 61 74 20 77 65 72 65 20 6a 75 73 74 20 63 6f 6d at were just com
555d0 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 mitted..**.** Th
555e0 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 e callback funct
555f0 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 72 6d 61 ion should norma
55600 6c 6c 79 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 lly return [SQLI
55610 54 45 5f 4f 4b 5d 2e 20 20 5e 49 66 20 61 6e 20 TE_OK]. ^If an
55620 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 error.** code is
55630 20 72 65 74 75 72 6e 65 64 2c 20 74 68 61 74 20 returned, that
55640 65 72 72 6f 72 20 77 69 6c 6c 20 70 72 6f 70 61 error will propa
55650 67 61 74 65 20 62 61 63 6b 20 75 70 20 74 68 72 gate back up thr
55660 6f 75 67 68 20 74 68 65 0a 2a 2a 20 53 51 4c 69 ough the.** SQLi
55670 74 65 20 63 6f 64 65 20 62 61 73 65 20 74 6f 20 te code base to
55680 63 61 75 73 65 20 74 68 65 20 73 74 61 74 65 6d cause the statem
55690 65 6e 74 20 74 68 61 74 20 70 72 6f 76 6f 6b 65 ent that provoke
556a0 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a d the callback.*
556b0 2a 20 74 6f 20 72 65 70 6f 72 74 20 61 6e 20 65 * to report an e
556c0 72 72 6f 72 2c 20 74 68 6f 75 67 68 20 74 68 65 rror, though the
556d0 20 63 6f 6d 6d 69 74 20 77 69 6c 6c 20 68 61 76 commit will hav
556e0 65 20 73 74 69 6c 6c 20 6f 63 63 75 72 72 65 64 e still occurred
556f0 2e 20 49 66 20 74 68 65 0a 2a 2a 20 63 61 6c 6c . If the.** call
55700 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 back returns [SQ
55710 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 LITE_ROW] or [SQ
55720 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 6f 72 20 69 LITE_DONE], or i
55730 66 20 69 74 20 72 65 74 75 72 6e 73 20 61 20 76 f it returns a v
55740 61 6c 75 65 0a 2a 2a 20 74 68 61 74 20 64 6f 65 alue.** that doe
55750 73 20 6e 6f 74 20 63 6f 72 72 65 73 70 6f 6e 64 s not correspond
55760 20 74 6f 20 61 6e 79 20 76 61 6c 69 64 20 53 51 to any valid SQ
55770 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2c Lite error code,
55780 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 the results.**
55790 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a are undefined..*
557a0 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 *.** A single da
557b0 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6d 61 tabase handle ma
557c0 79 20 68 61 76 65 20 61 74 20 6d 6f 73 74 20 61 y have at most a
557d0 20 73 69 6e 67 6c 65 20 77 72 69 74 65 2d 61 68 single write-ah
557e0 65 61 64 20 6c 6f 67 20 63 61 6c 6c 62 61 63 6b ead log callback
557f0 20 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 20 .** registered
55800 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 5e 43 61 at one time. ^Ca
55810 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 77 lling [sqlite3_w
55820 61 6c 5f 68 6f 6f 6b 28 29 5d 20 72 65 70 6c 61 al_hook()] repla
55830 63 65 73 20 61 6e 79 0a 2a 2a 20 70 72 65 76 69 ces any.** previ
55840 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 ously registered
55850 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 write-ahead log
55860 20 63 61 6c 6c 62 61 63 6b 2e 20 5e 4e 6f 74 65 callback. ^Note
55870 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 5b 73 71 that the.** [sq
55880 6c 69 74 65 33 5f 77 61 6c 5f 61 75 74 6f 63 68 lite3_wal_autoch
55890 65 63 6b 70 6f 69 6e 74 28 29 5d 20 69 6e 74 65 eckpoint()] inte
558a0 72 66 61 63 65 20 61 6e 64 20 74 68 65 0a 2a 2a rface and the.**
558b0 20 5b 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 [wal_autocheckp
558c0 6f 69 6e 74 20 70 72 61 67 6d 61 5d 20 62 6f 74 oint pragma] bot
558d0 68 20 69 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 h invoke [sqlite
558e0 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29 5d 20 61 6e 3_wal_hook()] an
558f0 64 20 77 69 6c 6c 0a 2a 2a 20 74 68 6f 73 65 20 d will.** those
55900 6f 76 65 72 77 72 69 74 65 20 61 6e 79 20 70 72 overwrite any pr
55910 69 6f 72 20 5b 73 71 6c 69 74 65 33 5f 77 61 6c ior [sqlite3_wal
55920 5f 68 6f 6f 6b 28 29 5d 20 73 65 74 74 69 6e 67 _hook()] setting
55930 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
55940 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 77 void *sqlite3_w
55950 61 6c 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 al_hook(. sqlit
55960 65 33 2a 2c 20 0a 20 20 69 6e 74 28 2a 29 28 76 e3*, . int(*)(v
55970 6f 69 64 20 2a 2c 73 71 6c 69 74 65 33 2a 2c 63 oid *,sqlite3*,c
55980 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 2c onst char*,int),
55990 0a 20 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a . void*.);../*.
559a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e ** CAPI3REF: Con
559b0 66 69 67 75 72 65 20 61 6e 20 61 75 74 6f 2d 63 figure an auto-c
559c0 68 65 63 6b 70 6f 69 6e 74 0a 2a 2a 0a 2a 2a 20 heckpoint.**.**
559d0 5e 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 77 61 ^The [sqlite3_wa
559e0 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e 74 l_autocheckpoint
559f0 28 44 2c 4e 29 5d 20 69 73 20 61 20 77 72 61 70 (D,N)] is a wrap
55a00 70 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73 per around.** [s
55a10 71 6c 69 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28 qlite3_wal_hook(
55a20 29 5d 20 74 68 61 74 20 63 61 75 73 65 73 20 61 )] that causes a
55a30 6e 79 20 64 61 74 61 62 61 73 65 20 6f 6e 20 5b ny database on [
55a40 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
55a50 69 6f 6e 5d 20 44 0a 2a 2a 20 74 6f 20 61 75 74 ion] D.** to aut
55a60 6f 6d 61 74 69 63 61 6c 6c 79 20 5b 63 68 65 63 omatically [chec
55a70 6b 70 6f 69 6e 74 5d 0a 2a 2a 20 61 66 74 65 72 kpoint].** after
55a80 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 committing a tr
55a90 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 ansaction if the
55aa0 72 65 20 61 72 65 20 4e 20 6f 72 0a 2a 2a 20 6d re are N or.** m
55ab0 6f 72 65 20 66 72 61 6d 65 73 20 69 6e 20 74 68 ore frames in th
55ac0 65 20 5b 77 72 69 74 65 2d 61 68 65 61 64 20 6c e [write-ahead l
55ad0 6f 67 5d 20 66 69 6c 65 2e 20 20 5e 50 61 73 73 og] file. ^Pass
55ae0 69 6e 67 20 7a 65 72 6f 20 6f 72 20 0a 2a 2a 20 ing zero or .**
55af0 61 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 a negative value
55b00 20 61 73 20 74 68 65 20 6e 46 72 61 6d 65 20 70 as the nFrame p
55b10 61 72 61 6d 65 74 65 72 20 64 69 73 61 62 6c 65 arameter disable
55b20 73 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 63 s automatic.** c
55b30 68 65 63 6b 70 6f 69 6e 74 73 20 65 6e 74 69 72 heckpoints entir
55b40 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 ely..**.** ^The
55b50 63 61 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 65 callback registe
55b60 72 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 red by this func
55b70 74 69 6f 6e 20 72 65 70 6c 61 63 65 73 20 61 6e tion replaces an
55b80 79 20 65 78 69 73 74 69 6e 67 20 63 61 6c 6c 62 y existing callb
55b90 61 63 6b 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 ack.** registere
55ba0 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 d using [sqlite3
55bb0 5f 77 61 6c 5f 68 6f 6f 6b 28 29 5d 2e 20 20 5e _wal_hook()]. ^
55bc0 4c 69 6b 65 77 69 73 65 2c 20 72 65 67 69 73 74 Likewise, regist
55bd0 65 72 69 6e 67 20 61 20 63 61 6c 6c 62 61 63 6b ering a callback
55be0 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 .** using [sqlit
55bf0 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29 5d 20 64 e3_wal_hook()] d
55c00 69 73 61 62 6c 65 73 20 74 68 65 20 61 75 74 6f isables the auto
55c10 6d 61 74 69 63 20 63 68 65 63 6b 70 6f 69 6e 74 matic checkpoint
55c20 20 6d 65 63 68 61 6e 69 73 6d 0a 2a 2a 20 63 6f mechanism.** co
55c30 6e 66 69 67 75 72 65 64 20 62 79 20 74 68 69 73 nfigured by this
55c40 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
55c50 20 5e 54 68 65 20 5b 77 61 6c 5f 61 75 74 6f 63 ^The [wal_autoc
55c60 68 65 63 6b 70 6f 69 6e 74 20 70 72 61 67 6d 61 heckpoint pragma
55c70 5d 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f ] can be used to
55c80 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 69 6e 74 invoke this int
55c90 65 72 66 61 63 65 0a 2a 2a 20 66 72 6f 6d 20 53 erface.** from S
55ca0 51 4c 2e 0a 2a 2a 0a 2a 2a 20 5e 45 76 65 72 79 QL..**.** ^Every
55cb0 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20 63 new [database c
55cc0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 64 65 66 61 75 onnection] defau
55cd0 6c 74 73 20 74 6f 20 68 61 76 69 6e 67 20 74 68 lts to having th
55ce0 65 20 61 75 74 6f 2d 63 68 65 63 6b 70 6f 69 6e e auto-checkpoin
55cf0 74 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 77 69 74 t.** enabled wit
55d00 68 20 61 20 74 68 72 65 73 68 6f 6c 64 20 6f 66 h a threshold of
55d10 20 31 30 30 30 20 6f 72 20 5b 53 51 4c 49 54 45 1000 or [SQLITE
55d20 5f 44 45 46 41 55 4c 54 5f 57 41 4c 5f 41 55 54 _DEFAULT_WAL_AUT
55d30 4f 43 48 45 43 4b 50 4f 49 4e 54 5d 0a 2a 2a 20 OCHECKPOINT].**
55d40 70 61 67 65 73 2e 20 20 54 68 65 20 75 73 65 20 pages. The use
55d50 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 of this interfac
55d60 65 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 6e 65 63 e.** is only nec
55d70 65 73 73 61 72 79 20 69 66 20 74 68 65 20 64 65 essary if the de
55d80 66 61 75 6c 74 20 73 65 74 74 69 6e 67 20 69 73 fault setting is
55d90 20 66 6f 75 6e 64 20 74 6f 20 62 65 20 73 75 62 found to be sub
55da0 6f 70 74 69 6d 61 6c 0a 2a 2a 20 66 6f 72 20 61 optimal.** for a
55db0 20 70 61 72 74 69 63 75 6c 61 72 20 61 70 70 6c particular appl
55dc0 69 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 ication..*/.SQLI
55dd0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
55de0 65 33 5f 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b e3_wal_autocheck
55df0 70 6f 69 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 point(sqlite3 *d
55e00 62 2c 20 69 6e 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a b, int N);../*.*
55e10 2a 20 43 41 50 49 33 52 45 46 3a 20 43 68 65 63 * CAPI3REF: Chec
55e20 6b 70 6f 69 6e 74 20 61 20 64 61 74 61 62 61 73 kpoint a databas
55e30 65 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 5b 73 71 e.**.** ^The [sq
55e40 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 lite3_wal_checkp
55e50 6f 69 6e 74 28 44 2c 58 29 5d 20 69 6e 74 65 72 oint(D,X)] inter
55e60 66 61 63 65 20 63 61 75 73 65 73 20 64 61 74 61 face causes data
55e70 62 61 73 65 20 6e 61 6d 65 64 20 58 0a 2a 2a 20 base named X.**
55e80 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e on [database con
55e90 6e 65 63 74 69 6f 6e 5d 20 44 20 74 6f 20 62 65 nection] D to be
55ea0 20 5b 63 68 65 63 6b 70 6f 69 6e 74 65 64 5d 2e [checkpointed].
55eb0 20 20 5e 49 66 20 58 20 69 73 20 4e 55 4c 4c 20 ^If X is NULL
55ec0 6f 72 20 61 6e 0a 2a 2a 20 65 6d 70 74 79 20 73 or an.** empty s
55ed0 74 72 69 6e 67 2c 20 74 68 65 6e 20 61 20 63 68 tring, then a ch
55ee0 65 63 6b 70 6f 69 6e 74 20 69 73 20 72 75 6e 20 eckpoint is run
55ef0 6f 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 on all databases
55f00 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f of.** connectio
55f10 6e 20 44 2e 20 20 5e 49 66 20 74 68 65 20 64 61 n D. ^If the da
55f20 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
55f30 6e 20 44 20 69 73 20 6e 6f 74 20 69 6e 0a 2a 2a n D is not in.**
55f40 20 5b 57 41 4c 20 7c 20 77 72 69 74 65 2d 61 68 [WAL | write-ah
55f50 65 61 64 20 6c 6f 67 20 6d 6f 64 65 5d 20 74 68 ead log mode] th
55f60 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 en this interfac
55f70 65 20 69 73 20 61 20 68 61 72 6d 6c 65 73 73 20 e is a harmless
55f80 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 no-op..**.** ^Th
55f90 65 20 5b 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e e [wal_checkpoin
55fa0 74 20 70 72 61 67 6d 61 5d 20 63 61 6e 20 62 65 t pragma] can be
55fb0 20 75 73 65 64 20 74 6f 20 69 6e 76 6f 6b 65 20 used to invoke
55fc0 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 0a 2a this interface.*
55fd0 2a 20 66 72 6f 6d 20 53 51 4c 2e 20 20 5e 54 68 * from SQL. ^Th
55fe0 65 20 5b 73 71 6c 69 74 65 33 5f 77 61 6c 5f 61 e [sqlite3_wal_a
55ff0 75 74 6f 63 68 65 63 6b 70 6f 69 6e 74 28 29 5d utocheckpoint()]
56000 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20 74 interface and t
56010 68 65 0a 2a 2a 20 5b 77 61 6c 5f 61 75 74 6f 63 he.** [wal_autoc
56020 68 65 63 6b 70 6f 69 6e 74 20 70 72 61 67 6d 61 heckpoint pragma
56030 5d 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f ] can be used to
56040 20 63 61 75 73 65 20 74 68 69 73 20 69 6e 74 65 cause this inte
56050 72 66 61 63 65 20 74 6f 20 62 65 0a 2a 2a 20 72 rface to be.** r
56060 75 6e 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 un whenever the
56070 57 41 4c 20 72 65 61 63 68 65 73 20 61 20 63 65 WAL reaches a ce
56080 72 74 61 69 6e 20 73 69 7a 65 20 74 68 72 65 73 rtain size thres
56090 68 6f 6c 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 hold..**.** See
560a0 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 77 also: [sqlite3_w
560b0 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 5f 76 32 al_checkpoint_v2
560c0 28 29 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ()].*/.SQLITE_AP
560d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 61 I int sqlite3_wa
560e0 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 28 73 71 6c l_checkpoint(sql
560f0 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 ite3 *db, const
56100 63 68 61 72 20 2a 7a 44 62 29 3b 0a 0a 2f 2a 0a char *zDb);../*.
56110 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 68 65 ** CAPI3REF: Che
56120 63 6b 70 6f 69 6e 74 20 61 20 64 61 74 61 62 61 ckpoint a databa
56130 73 65 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 61 20 63 se.**.** Run a c
56140 68 65 63 6b 70 6f 69 6e 74 20 6f 70 65 72 61 74 heckpoint operat
56150 69 6f 6e 20 6f 6e 20 57 41 4c 20 64 61 74 61 62 ion on WAL datab
56160 61 73 65 20 7a 44 62 20 61 74 74 61 63 68 65 64 ase zDb attached
56170 20 74 6f 20 64 61 74 61 62 61 73 65 20 0a 2a 2a to database .**
56180 20 68 61 6e 64 6c 65 20 64 62 2e 20 54 68 65 20 handle db. The
56190 73 70 65 63 69 66 69 63 20 6f 70 65 72 61 74 69 specific operati
561a0 6f 6e 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 on is determined
561b0 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 6f 66 by the value of
561c0 20 74 68 65 20 0a 2a 2a 20 65 4d 6f 64 65 20 70 the .** eMode p
561d0 61 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 arameter:.**.**
561e0 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 <dl>.** <dt>SQLI
561f0 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 50 41 TE_CHECKPOINT_PA
56200 53 53 49 56 45 3c 64 64 3e 0a 2a 2a 20 20 20 43 SSIVE<dd>.** C
56210 68 65 63 6b 70 6f 69 6e 74 20 61 73 20 6d 61 6e heckpoint as man
56220 79 20 66 72 61 6d 65 73 20 61 73 20 70 6f 73 73 y frames as poss
56230 69 62 6c 65 20 77 69 74 68 6f 75 74 20 77 61 69 ible without wai
56240 74 69 6e 67 20 66 6f 72 20 61 6e 79 20 64 61 74 ting for any dat
56250 61 62 61 73 65 20 0a 2a 2a 20 20 20 72 65 61 64 abase .** read
56260 65 72 73 20 6f 72 20 77 72 69 74 65 72 73 20 74 ers or writers t
56270 6f 20 66 69 6e 69 73 68 2e 20 53 79 6e 63 20 74 o finish. Sync t
56280 68 65 20 64 62 20 66 69 6c 65 20 69 66 20 61 6c he db file if al
56290 6c 20 66 72 61 6d 65 73 20 69 6e 20 74 68 65 20 l frames in the
562a0 6c 6f 67 0a 2a 2a 20 20 20 61 72 65 20 63 68 65 log.** are che
562b0 63 6b 70 6f 69 6e 74 65 64 2e 20 54 68 69 73 20 ckpointed. This
562c0 6d 6f 64 65 20 69 73 20 74 68 65 20 73 61 6d 65 mode is the same
562d0 20 61 73 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 as calling .**
562e0 20 20 73 71 6c 69 74 65 33 5f 77 61 6c 5f 63 68 sqlite3_wal_ch
562f0 65 63 6b 70 6f 69 6e 74 28 29 2e 20 54 68 65 20 eckpoint(). The
56300 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c busy-handler cal
56310 6c 62 61 63 6b 20 69 73 20 6e 65 76 65 72 20 69 lback is never i
56320 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 nvoked..**.** <d
56330 74 3e 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f t>SQLITE_CHECKPO
56340 49 4e 54 5f 46 55 4c 4c 3c 64 64 3e 0a 2a 2a 20 INT_FULL<dd>.**
56350 20 20 54 68 69 73 20 6d 6f 64 65 20 62 6c 6f 63 This mode bloc
56360 6b 73 20 28 63 61 6c 6c 73 20 74 68 65 20 62 75 ks (calls the bu
56370 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 sy-handler callb
56380 61 63 6b 29 20 75 6e 74 69 6c 20 74 68 65 72 65 ack) until there
56390 20 69 73 20 6e 6f 0a 2a 2a 20 20 20 64 61 74 61 is no.** data
563a0 62 61 73 65 20 77 72 69 74 65 72 20 61 6e 64 20 base writer and
563b0 61 6c 6c 20 72 65 61 64 65 72 73 20 61 72 65 20 all readers are
563c0 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 reading from the
563d0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 64 61 74 most recent dat
563e0 61 62 61 73 65 0a 2a 2a 20 20 20 73 6e 61 70 73 abase.** snaps
563f0 68 6f 74 2e 20 49 74 20 74 68 65 6e 20 63 68 65 hot. It then che
56400 63 6b 70 6f 69 6e 74 73 20 61 6c 6c 20 66 72 61 ckpoints all fra
56410 6d 65 73 20 69 6e 20 74 68 65 20 6c 6f 67 20 66 mes in the log f
56420 69 6c 65 20 61 6e 64 20 73 79 6e 63 73 20 74 68 ile and syncs th
56430 65 0a 2a 2a 20 20 20 64 61 74 61 62 61 73 65 20 e.** database
56440 66 69 6c 65 2e 20 54 68 69 73 20 63 61 6c 6c 20 file. This call
56450 62 6c 6f 63 6b 73 20 64 61 74 61 62 61 73 65 20 blocks database
56460 77 72 69 74 65 72 73 20 77 68 69 6c 65 20 69 74 writers while it
56470 20 69 73 20 72 75 6e 6e 69 6e 67 2c 0a 2a 2a 20 is running,.**
56480 20 20 62 75 74 20 6e 6f 74 20 64 61 74 61 62 61 but not databa
56490 73 65 20 72 65 61 64 65 72 73 2e 0a 2a 2a 0a 2a se readers..**.*
564a0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 48 45 * <dt>SQLITE_CHE
564b0 43 4b 50 4f 49 4e 54 5f 52 45 53 54 41 52 54 3c CKPOINT_RESTART<
564c0 64 64 3e 0a 2a 2a 20 20 20 54 68 69 73 20 6d 6f dd>.** This mo
564d0 64 65 20 77 6f 72 6b 73 20 74 68 65 20 73 61 6d de works the sam
564e0 65 20 77 61 79 20 61 73 20 53 51 4c 49 54 45 5f e way as SQLITE_
564f0 43 48 45 43 4b 50 4f 49 4e 54 5f 46 55 4c 4c 2c CHECKPOINT_FULL,
56500 20 65 78 63 65 70 74 20 61 66 74 65 72 20 0a 2a except after .*
56510 2a 20 20 20 63 68 65 63 6b 70 6f 69 6e 74 69 6e * checkpointin
56520 67 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 20 69 g the log file i
56530 74 20 62 6c 6f 63 6b 73 20 28 63 61 6c 6c 73 20 t blocks (calls
56540 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 the busy-handler
56550 20 63 61 6c 6c 62 61 63 6b 29 0a 2a 2a 20 20 20 callback).**
56560 75 6e 74 69 6c 20 61 6c 6c 20 72 65 61 64 65 72 until all reader
56570 73 20 61 72 65 20 72 65 61 64 69 6e 67 20 66 72 s are reading fr
56580 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
56590 66 69 6c 65 20 6f 6e 6c 79 2e 20 54 68 69 73 20 file only. This
565a0 65 6e 73 75 72 65 73 20 0a 2a 2a 20 20 20 74 68 ensures .** th
565b0 61 74 20 74 68 65 20 6e 65 78 74 20 63 6c 69 65 at the next clie
565c0 6e 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 nt to write to t
565d0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
565e0 20 72 65 73 74 61 72 74 73 20 74 68 65 20 6c 6f restarts the lo
565f0 67 20 66 69 6c 65 20 0a 2a 2a 20 20 20 66 72 6f g file .** fro
56600 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 2e m the beginning.
56610 20 54 68 69 73 20 63 61 6c 6c 20 62 6c 6f 63 6b This call block
56620 73 20 64 61 74 61 62 61 73 65 20 77 72 69 74 65 s database write
56630 72 73 20 77 68 69 6c 65 20 69 74 20 69 73 20 72 rs while it is r
56640 75 6e 6e 69 6e 67 2c 0a 2a 2a 20 20 20 62 75 74 unning,.** but
56650 20 6e 6f 74 20 64 61 74 61 62 61 73 65 20 72 65 not database re
56660 61 64 65 72 73 2e 0a 2a 2a 20 3c 2f 64 6c 3e 0a aders..** </dl>.
56670 2a 2a 0a 2a 2a 20 49 66 20 70 6e 4c 6f 67 20 69 **.** If pnLog i
56680 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e s not NULL, then
56690 20 2a 70 6e 4c 6f 67 20 69 73 20 73 65 74 20 74 *pnLog is set t
566a0 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 o the total numb
566b0 65 72 20 6f 66 20 66 72 61 6d 65 73 20 69 6e 0a er of frames in.
566c0 2a 2a 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 20 ** the log file
566d0 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 before returning
566e0 2e 20 49 66 20 70 6e 43 6b 70 74 20 69 73 20 6e . If pnCkpt is n
566f0 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a 70 ot NULL, then *p
56700 6e 43 6b 70 74 20 69 73 20 73 65 74 20 74 6f 0a nCkpt is set to.
56710 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d ** the total num
56720 62 65 72 20 6f 66 20 63 68 65 63 6b 70 6f 69 6e ber of checkpoin
56730 74 65 64 20 66 72 61 6d 65 73 20 28 69 6e 63 6c ted frames (incl
56740 75 64 69 6e 67 20 61 6e 79 20 74 68 61 74 20 77 uding any that w
56750 65 72 65 20 61 6c 72 65 61 64 79 0a 2a 2a 20 63 ere already.** c
56760 68 65 63 6b 70 6f 69 6e 74 65 64 20 77 68 65 6e heckpointed when
56770 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
56780 73 20 63 61 6c 6c 65 64 29 2e 20 2a 70 6e 4c 6f s called). *pnLo
56790 67 20 61 6e 64 20 2a 70 6e 43 6b 70 74 20 6d 61 g and *pnCkpt ma
567a0 79 20 62 65 0a 2a 2a 20 70 6f 70 75 6c 61 74 65 y be.** populate
567b0 64 20 65 76 65 6e 20 69 66 20 73 71 6c 69 74 65 d even if sqlite
567c0 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 3_wal_checkpoint
567d0 5f 76 32 28 29 20 72 65 74 75 72 6e 73 20 6f 74 _v2() returns ot
567e0 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f her than SQLITE_
567f0 4f 4b 2e 0a 2a 2a 20 49 66 20 6e 6f 20 76 61 6c OK..** If no val
56800 75 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c ues are availabl
56810 65 20 62 65 63 61 75 73 65 20 6f 66 20 61 6e 20 e because of an
56820 65 72 72 6f 72 2c 20 74 68 65 79 20 61 72 65 20 error, they are
56830 62 6f 74 68 20 73 65 74 20 74 6f 20 2d 31 0a 2a both set to -1.*
56840 2a 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 * before returni
56850 6e 67 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 ng to communicat
56860 65 20 74 68 69 73 20 74 6f 20 74 68 65 20 63 61 e this to the ca
56870 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 ller..**.** All
56880 63 61 6c 6c 73 20 6f 62 74 61 69 6e 20 61 6e 20 calls obtain an
56890 65 78 63 6c 75 73 69 76 65 20 22 63 68 65 63 6b exclusive "check
568a0 70 6f 69 6e 74 22 20 6c 6f 63 6b 20 6f 6e 20 74 point" lock on t
568b0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
568c0 2e 20 49 66 0a 2a 2a 20 61 6e 79 20 6f 74 68 65 . If.** any othe
568d0 72 20 70 72 6f 63 65 73 73 20 69 73 20 72 75 6e r process is run
568e0 6e 69 6e 67 20 61 20 63 68 65 63 6b 70 6f 69 6e ning a checkpoin
568f0 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 74 20 74 t operation at t
56900 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 74 68 he same time, th
56910 65 20 0a 2a 2a 20 6c 6f 63 6b 20 63 61 6e 6e 6f e .** lock canno
56920 74 20 62 65 20 6f 62 74 61 69 6e 65 64 20 61 6e t be obtained an
56930 64 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 d SQLITE_BUSY is
56940 20 72 65 74 75 72 6e 65 64 2e 20 45 76 65 6e 20 returned. Even
56950 69 66 20 74 68 65 72 65 20 69 73 20 61 20 0a 2a if there is a .*
56960 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 * busy-handler c
56970 6f 6e 66 69 67 75 72 65 64 2c 20 69 74 20 77 69 onfigured, it wi
56980 6c 6c 20 6e 6f 74 20 62 65 20 69 6e 76 6f 6b 65 ll not be invoke
56990 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a d in this case..
569a0 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 **.** The SQLITE
569b0 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 46 55 4c 4c _CHECKPOINT_FULL
569c0 20 61 6e 64 20 52 45 53 54 41 52 54 20 6d 6f 64 and RESTART mod
569d0 65 73 20 61 6c 73 6f 20 6f 62 74 61 69 6e 20 74 es also obtain t
569e0 68 65 20 65 78 63 6c 75 73 69 76 65 20 0a 2a 2a he exclusive .**
569f0 20 22 77 72 69 74 65 72 22 20 6c 6f 63 6b 20 6f "writer" lock o
56a00 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
56a10 69 6c 65 2e 20 49 66 20 74 68 65 20 77 72 69 74 ile. If the writ
56a20 65 72 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 er lock cannot b
56a30 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 69 6d e obtained.** im
56a40 6d 65 64 69 61 74 65 6c 79 2c 20 61 6e 64 20 61 mediately, and a
56a50 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 73 busy-handler is
56a60 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 69 74 20 configured, it
56a70 69 73 20 69 6e 76 6f 6b 65 64 20 61 6e 64 20 74 is invoked and t
56a80 68 65 20 77 72 69 74 65 72 0a 2a 2a 20 6c 6f 63 he writer.** loc
56a90 6b 20 72 65 74 72 69 65 64 20 75 6e 74 69 6c 20 k retried until
56aa0 65 69 74 68 65 72 20 74 68 65 20 62 75 73 79 2d either the busy-
56ab0 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 20 handler returns
56ac0 30 20 6f 72 20 74 68 65 20 6c 6f 63 6b 20 69 73 0 or the lock is
56ad0 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 .** successfully
56ae0 20 6f 62 74 61 69 6e 65 64 2e 20 54 68 65 20 62 obtained. The b
56af0 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 73 20 61 usy-handler is a
56b00 6c 73 6f 20 69 6e 76 6f 6b 65 64 20 77 68 69 6c lso invoked whil
56b10 65 20 77 61 69 74 69 6e 67 20 66 6f 72 0a 2a 2a e waiting for.**
56b20 20 64 61 74 61 62 61 73 65 20 72 65 61 64 65 72 database reader
56b30 73 20 61 73 20 64 65 73 63 72 69 62 65 64 20 61 s as described a
56b40 62 6f 76 65 2e 20 49 66 20 74 68 65 20 62 75 73 bove. If the bus
56b50 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e y-handler return
56b60 73 20 30 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 s 0 before.** th
56b70 65 20 77 72 69 74 65 72 20 6c 6f 63 6b 20 69 73 e writer lock is
56b80 20 6f 62 74 61 69 6e 65 64 20 6f 72 20 77 68 69 obtained or whi
56b90 6c 65 20 77 61 69 74 69 6e 67 20 66 6f 72 20 64 le waiting for d
56ba0 61 74 61 62 61 73 65 20 72 65 61 64 65 72 73 2c atabase readers,
56bb0 20 74 68 65 0a 2a 2a 20 63 68 65 63 6b 70 6f 69 the.** checkpoi
56bc0 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 72 6f nt operation pro
56bd0 63 65 65 64 73 20 66 72 6f 6d 20 74 68 61 74 20 ceeds from that
56be0 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 73 61 6d point in the sam
56bf0 65 20 77 61 79 20 61 73 20 0a 2a 2a 20 53 51 4c e way as .** SQL
56c00 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 50 ITE_CHECKPOINT_P
56c10 41 53 53 49 56 45 20 2d 20 63 68 65 63 6b 70 6f ASSIVE - checkpo
56c20 69 6e 74 69 6e 67 20 61 73 20 6d 61 6e 79 20 66 inting as many f
56c30 72 61 6d 65 73 20 61 73 20 70 6f 73 73 69 62 6c rames as possibl
56c40 65 20 0a 2a 2a 20 77 69 74 68 6f 75 74 20 62 6c e .** without bl
56c50 6f 63 6b 69 6e 67 20 61 6e 79 20 66 75 72 74 68 ocking any furth
56c60 65 72 2e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 er. SQLITE_BUSY
56c70 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74 is returned in t
56c80 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 his case..**.**
56c90 49 66 20 70 61 72 61 6d 65 74 65 72 20 7a 44 62 If parameter zDb
56ca0 20 69 73 20 4e 55 4c 4c 20 6f 72 20 70 6f 69 6e is NULL or poin
56cb0 74 73 20 74 6f 20 61 20 7a 65 72 6f 20 6c 65 6e ts to a zero len
56cc0 67 74 68 20 73 74 72 69 6e 67 2c 20 74 68 65 6e gth string, then
56cd0 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 65 the.** specifie
56ce0 64 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 d operation is a
56cf0 74 74 65 6d 70 74 65 64 20 6f 6e 20 61 6c 6c 20 ttempted on all
56d00 57 41 4c 20 64 61 74 61 62 61 73 65 73 2e 20 49 WAL databases. I
56d10 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 0a n this case the.
56d20 2a 2a 20 76 61 6c 75 65 73 20 77 72 69 74 74 65 ** values writte
56d30 6e 20 74 6f 20 6f 75 74 70 75 74 20 70 61 72 61 n to output para
56d40 6d 65 74 65 72 73 20 2a 70 6e 4c 6f 67 20 61 6e meters *pnLog an
56d50 64 20 2a 70 6e 43 6b 70 74 20 61 72 65 20 75 6e d *pnCkpt are un
56d60 64 65 66 69 6e 65 64 2e 20 49 66 20 0a 2a 2a 20 defined. If .**
56d70 61 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 65 an SQLITE_BUSY e
56d80 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 rror is encounte
56d90 72 65 64 20 77 68 65 6e 20 70 72 6f 63 65 73 73 red when process
56da0 69 6e 67 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 ing one or more
56db0 6f 66 20 74 68 65 20 0a 2a 2a 20 61 74 74 61 63 of the .** attac
56dc0 68 65 64 20 57 41 4c 20 64 61 74 61 62 61 73 65 hed WAL database
56dd0 73 2c 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e s, the operation
56de0 20 69 73 20 73 74 69 6c 6c 20 61 74 74 65 6d 70 is still attemp
56df0 74 65 64 20 6f 6e 20 61 6e 79 20 72 65 6d 61 69 ted on any remai
56e00 6e 69 6e 67 20 0a 2a 2a 20 61 74 74 61 63 68 65 ning .** attache
56e10 64 20 64 61 74 61 62 61 73 65 73 20 61 6e 64 20 d databases and
56e20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 SQLITE_BUSY is r
56e30 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 eturned to the c
56e40 61 6c 6c 65 72 2e 20 49 66 20 61 6e 79 20 6f 74 aller. If any ot
56e50 68 65 72 20 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 her .** error oc
56e60 63 75 72 73 20 77 68 69 6c 65 20 70 72 6f 63 65 curs while proce
56e70 73 73 69 6e 67 20 61 6e 20 61 74 74 61 63 68 65 ssing an attache
56e80 64 20 64 61 74 61 62 61 73 65 2c 20 70 72 6f 63 d database, proc
56e90 65 73 73 69 6e 67 20 69 73 20 61 62 61 6e 64 6f essing is abando
56ea0 6e 65 64 20 0a 2a 2a 20 61 6e 64 20 74 68 65 20 ned .** and the
56eb0 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 error code retur
56ec0 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ned to the calle
56ed0 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 r immediately. I
56ee0 66 20 6e 6f 20 65 72 72 6f 72 20 0a 2a 2a 20 28 f no error .** (
56ef0 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 6f SQLITE_BUSY or o
56f00 74 68 65 72 77 69 73 65 29 20 69 73 20 65 6e 63 therwise) is enc
56f10 6f 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20 70 ountered while p
56f20 72 6f 63 65 73 73 69 6e 67 20 74 68 65 20 61 74 rocessing the at
56f30 74 61 63 68 65 64 20 0a 2a 2a 20 64 61 74 61 62 tached .** datab
56f40 61 73 65 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 ases, SQLITE_OK
56f50 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
56f60 2a 2a 20 49 66 20 64 61 74 61 62 61 73 65 20 7a ** If database z
56f70 44 62 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f Db is the name o
56f80 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 f an attached da
56f90 74 61 62 61 73 65 20 74 68 61 74 20 69 73 20 6e tabase that is n
56fa0 6f 74 20 69 6e 20 57 41 4c 0a 2a 2a 20 6d 6f 64 ot in WAL.** mod
56fb0 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 e, SQLITE_OK is
56fc0 72 65 74 75 72 6e 65 64 20 61 6e 64 20 62 6f 74 returned and bot
56fd0 68 20 2a 70 6e 4c 6f 67 20 61 6e 64 20 2a 70 6e h *pnLog and *pn
56fe0 43 6b 70 74 20 73 65 74 20 74 6f 20 2d 31 2e 20 Ckpt set to -1.
56ff0 49 66 0a 2a 2a 20 7a 44 62 20 69 73 20 6e 6f 74 If.** zDb is not
57000 20 4e 55 4c 4c 20 28 6f 72 20 61 20 7a 65 72 6f NULL (or a zero
57010 20 6c 65 6e 67 74 68 20 73 74 72 69 6e 67 29 20 length string)
57020 61 6e 64 20 69 73 20 6e 6f 74 20 74 68 65 20 6e and is not the n
57030 61 6d 65 20 6f 66 20 61 6e 79 0a 2a 2a 20 61 74 ame of any.** at
57040 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 2c tached database,
57050 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 SQLITE_ERROR is
57060 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
57070 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 caller..*/.SQLI
57080 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
57090 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e e3_wal_checkpoin
570a0 74 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 t_v2(. sqlite3
570b0 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 *db,
570c0 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
570d0 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ase handle */.
570e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c const char *zDb,
570f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
57100 2f 2a 20 4e 61 6d 65 20 6f 66 20 61 74 74 61 63 /* Name of attac
57110 68 65 64 20 64 61 74 61 62 61 73 65 20 28 6f 72 hed database (or
57120 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 69 6e 74 20 NULL) */. int
57130 65 4d 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 eMode,
57140 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
57150 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 QLITE_CHECKPOINT
57160 5f 2a 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e _* value */. in
57170 74 20 2a 70 6e 4c 6f 67 2c 20 20 20 20 20 20 20 t *pnLog,
57180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
57190 20 4f 55 54 3a 20 53 69 7a 65 20 6f 66 20 57 41 OUT: Size of WA
571a0 4c 20 6c 6f 67 20 69 6e 20 66 72 61 6d 65 73 20 L log in frames
571b0 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6b 70 74 */. int *pnCkpt
571c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
571d0 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 /* OUT: Tot
571e0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 61 al number of fra
571f0 6d 65 73 20 63 68 65 63 6b 70 6f 69 6e 74 65 64 mes checkpointed
57200 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 */.);../*.** CA
57210 50 49 33 52 45 46 3a 20 43 68 65 63 6b 70 6f 69 PI3REF: Checkpoi
57220 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 61 72 nt operation par
57230 61 6d 65 74 65 72 73 0a 2a 2a 0a 2a 2a 20 54 68 ameters.**.** Th
57240 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 ese constants ca
57250 6e 20 62 65 20 75 73 65 64 20 61 73 20 74 68 65 n be used as the
57260 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 3rd parameter t
57270 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 77 61 o.** [sqlite3_wa
57280 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 5f 76 32 28 l_checkpoint_v2(
57290 29 5d 2e 20 20 53 65 65 20 74 68 65 20 5b 73 71 )]. See the [sq
572a0 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 lite3_wal_checkp
572b0 6f 69 6e 74 5f 76 32 28 29 5d 0a 2a 2a 20 64 6f oint_v2()].** do
572c0 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 cumentation for
572d0 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
572e0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 mation about the
572f0 20 6d 65 61 6e 69 6e 67 20 61 6e 64 20 75 73 65 meaning and use
57300 20 6f 66 0a 2a 2a 20 65 61 63 68 20 6f 66 20 74 of.** each of t
57310 68 65 73 65 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a hese values..*/.
57320 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
57330 48 45 43 4b 50 4f 49 4e 54 5f 50 41 53 53 49 56 HECKPOINT_PASSIV
57340 45 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 E 0.#define SQLI
57350 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 46 55 TE_CHECKPOINT_FU
57360 4c 4c 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 LL 1.#define
57370 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e SQLITE_CHECKPOIN
57380 54 5f 52 45 53 54 41 52 54 20 32 0a 0a 2f 2a 0a T_RESTART 2../*.
57390 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 ** CAPI3REF: Vir
573a0 74 75 61 6c 20 54 61 62 6c 65 20 49 6e 74 65 72 tual Table Inter
573b0 66 61 63 65 20 43 6f 6e 66 69 67 75 72 61 74 69 face Configurati
573c0 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 on.**.** This fu
573d0 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 nction may be ca
573e0 6c 6c 65 64 20 62 79 20 65 69 74 68 65 72 20 74 lled by either t
573f0 68 65 20 5b 78 43 6f 6e 6e 65 63 74 5d 20 6f 72 he [xConnect] or
57400 20 5b 78 43 72 65 61 74 65 5d 20 6d 65 74 68 6f [xCreate] metho
57410 64 0a 2a 2a 20 6f 66 20 61 20 5b 76 69 72 74 75 d.** of a [virtu
57420 61 6c 20 74 61 62 6c 65 5d 20 69 6d 70 6c 65 6d al table] implem
57430 65 6e 74 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 66 entation to conf
57440 69 67 75 72 65 0a 2a 2a 20 76 61 72 69 6f 75 73 igure.** various
57450 20 66 61 63 65 74 73 20 6f 66 20 74 68 65 20 76 facets of the v
57460 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74 irtual table int
57470 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 erface..**.** If
57480 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 this interface
57490 69 73 20 69 6e 76 6f 6b 65 64 20 6f 75 74 73 69 is invoked outsi
574a0 64 65 20 74 68 65 20 63 6f 6e 74 65 78 74 20 6f de the context o
574b0 66 20 61 6e 20 78 43 6f 6e 6e 65 63 74 20 6f 72 f an xConnect or
574c0 0a 2a 2a 20 78 43 72 65 61 74 65 20 76 69 72 74 .** xCreate virt
574d0 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 ual table method
574e0 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 then the behavi
574f0 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e or is undefined.
57500 0a 2a 2a 0a 2a 2a 20 41 74 20 70 72 65 73 65 6e .**.** At presen
57510 74 2c 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 t, there is only
57520 20 6f 6e 65 20 6f 70 74 69 6f 6e 20 74 68 61 74 one option that
57530 20 6d 61 79 20 62 65 20 63 6f 6e 66 69 67 75 72 may be configur
57540 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 69 73 ed using.** this
57550 20 66 75 6e 63 74 69 6f 6e 2e 20 28 53 65 65 20 function. (See
57560 5b 53 51 4c 49 54 45 5f 56 54 41 42 5f 43 4f 4e [SQLITE_VTAB_CON
57570 53 54 52 41 49 4e 54 5f 53 55 50 50 4f 52 54 5d STRAINT_SUPPORT]
57580 2e 29 20 20 46 75 72 74 68 65 72 20 6f 70 74 69 .) Further opti
57590 6f 6e 73 0a 2a 2a 20 6d 61 79 20 62 65 20 61 64 ons.** may be ad
575a0 64 65 64 20 69 6e 20 74 68 65 20 66 75 74 75 72 ded in the futur
575b0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
575c0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 74 61 int sqlite3_vta
575d0 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 65 33 b_config(sqlite3
575e0 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b *, int op, ...);
575f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
57600 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 : Virtual Table
57610 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70 Configuration Op
57620 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 54 68 65 73 tions.**.** Thes
57630 65 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 e macros define
57640 74 68 65 20 76 61 72 69 6f 75 73 20 6f 70 74 69 the various opti
57650 6f 6e 73 20 74 6f 20 74 68 65 0a 2a 2a 20 5b 73 ons to the.** [s
57660 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 6f 6e 66 qlite3_vtab_conf
57670 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 ig()] interface
57680 74 68 61 74 20 5b 76 69 72 74 75 61 6c 20 74 61 that [virtual ta
57690 62 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ble] implementat
576a0 69 6f 6e 73 0a 2a 2a 20 63 61 6e 20 75 73 65 20 ions.** can use
576b0 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 61 6e 64 to customize and
576c0 20 6f 70 74 69 6d 69 7a 65 20 74 68 65 69 72 20 optimize their
576d0 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 behavior..**.**
576e0 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 <dl>.** <dt>SQLI
576f0 54 45 5f 56 54 41 42 5f 43 4f 4e 53 54 52 41 49 TE_VTAB_CONSTRAI
57700 4e 54 5f 53 55 50 50 4f 52 54 0a 2a 2a 20 3c 64 NT_SUPPORT.** <d
57710 64 3e 43 61 6c 6c 73 20 6f 66 20 74 68 65 20 66 d>Calls of the f
57720 6f 72 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f orm.** [sqlite3_
57730 76 74 61 62 5f 63 6f 6e 66 69 67 5d 28 64 62 2c vtab_config](db,
57740 53 51 4c 49 54 45 5f 56 54 41 42 5f 43 4f 4e 53 SQLITE_VTAB_CONS
57750 54 52 41 49 4e 54 5f 53 55 50 50 4f 52 54 2c 58 TRAINT_SUPPORT,X
57760 29 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 2c ) are supported,
57770 0a 2a 2a 20 77 68 65 72 65 20 58 20 69 73 20 61 .** where X is a
57780 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20 58 n integer. If X
57790 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 is zero, then t
577a0 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c he [virtual tabl
577b0 65 5d 20 77 68 6f 73 65 0a 2a 2a 20 5b 78 43 72 e] whose.** [xCr
577c0 65 61 74 65 5d 20 6f 72 20 5b 78 43 6f 6e 6e 65 eate] or [xConne
577d0 63 74 5d 20 6d 65 74 68 6f 64 20 69 6e 76 6f 6b ct] method invok
577e0 65 64 20 5b 73 71 6c 69 74 65 33 5f 76 74 61 62 ed [sqlite3_vtab
577f0 5f 63 6f 6e 66 69 67 28 29 5d 20 64 6f 65 73 20 _config()] does
57800 6e 6f 74 0a 2a 2a 20 73 75 70 70 6f 72 74 20 63 not.** support c
57810 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 49 6e 20 onstraints. In
57820 74 68 69 73 20 63 6f 6e 66 69 67 75 72 61 74 69 this configurati
57830 6f 6e 20 28 77 68 69 63 68 20 69 73 20 74 68 65 on (which is the
57840 20 64 65 66 61 75 6c 74 29 20 69 66 0a 2a 2a 20 default) if.**
57850 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 5b 78 a call to the [x
57860 55 70 64 61 74 65 5d 20 6d 65 74 68 6f 64 20 72 Update] method r
57870 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 43 eturns [SQLITE_C
57880 4f 4e 53 54 52 41 49 4e 54 5d 2c 20 74 68 65 6e ONSTRAINT], then
57890 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20 73 the entire.** s
578a0 74 61 74 65 6d 65 6e 74 20 69 73 20 72 6f 6c 6c tatement is roll
578b0 65 64 20 62 61 63 6b 20 61 73 20 69 66 20 5b 4f ed back as if [O
578c0 4e 20 43 4f 4e 46 4c 49 43 54 20 7c 20 4f 52 20 N CONFLICT | OR
578d0 41 42 4f 52 54 5d 20 68 61 64 20 62 65 65 6e 0a ABORT] had been.
578e0 2a 2a 20 73 70 65 63 69 66 69 65 64 20 61 73 20 ** specified as
578f0 70 61 72 74 20 6f 66 20 74 68 65 20 75 73 65 72 part of the user
57900 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c s SQL statement,
57910 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 regardless of t
57920 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 4f 4e 20 he actual.** ON
57930 43 4f 4e 46 4c 49 43 54 20 6d 6f 64 65 20 73 70 CONFLICT mode sp
57940 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 ecified..**.** I
57950 66 20 58 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c f X is non-zero,
57960 20 74 68 65 6e 20 74 68 65 20 76 69 72 74 75 61 then the virtua
57970 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e l table implemen
57980 74 61 74 69 6f 6e 20 67 75 61 72 61 6e 74 65 65 tation guarantee
57990 73 0a 2a 2a 20 74 68 61 74 20 69 66 20 5b 78 55 s.** that if [xU
579a0 70 64 61 74 65 5d 20 72 65 74 75 72 6e 73 20 5b pdate] returns [
579b0 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
579c0 54 5d 2c 20 69 74 20 77 69 6c 6c 20 64 6f 20 73 T], it will do s
579d0 6f 20 62 65 66 6f 72 65 0a 2a 2a 20 61 6e 79 20 o before.** any
579e0 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 74 6f modifications to
579f0 20 69 6e 74 65 72 6e 61 6c 20 6f 72 20 70 65 72 internal or per
57a00 73 69 73 74 65 6e 74 20 64 61 74 61 20 73 74 72 sistent data str
57a10 75 63 74 75 72 65 73 20 68 61 76 65 20 62 65 65 uctures have bee
57a20 6e 20 6d 61 64 65 2e 0a 2a 2a 20 49 66 20 74 68 n made..** If th
57a30 65 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 e [ON CONFLICT]
57a40 6d 6f 64 65 20 69 73 20 41 42 4f 52 54 2c 20 46 mode is ABORT, F
57a50 41 49 4c 2c 20 49 47 4e 4f 52 45 20 6f 72 20 52 AIL, IGNORE or R
57a60 4f 4c 4c 42 41 43 4b 2c 20 53 51 4c 69 74 65 20 OLLBACK, SQLite
57a70 0a 2a 2a 20 69 73 20 61 62 6c 65 20 74 6f 20 72 .** is able to r
57a80 6f 6c 6c 20 62 61 63 6b 20 61 20 73 74 61 74 65 oll back a state
57a90 6d 65 6e 74 20 6f 72 20 64 61 74 61 62 61 73 65 ment or database
57aa0 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e transaction, an
57ab0 64 20 61 62 61 6e 64 6f 6e 0a 2a 2a 20 6f 72 20 d abandon.** or
57ac0 63 6f 6e 74 69 6e 75 65 20 70 72 6f 63 65 73 73 continue process
57ad0 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 ing the current
57ae0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 73 SQL statement as
57af0 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 0a 2a appropriate. .*
57b00 2a 20 49 66 20 74 68 65 20 4f 4e 20 43 4f 4e 46 * If the ON CONF
57b10 4c 49 43 54 20 6d 6f 64 65 20 69 73 20 52 45 50 LICT mode is REP
57b20 4c 41 43 45 20 61 6e 64 20 74 68 65 20 5b 78 55 LACE and the [xU
57b30 70 64 61 74 65 5d 20 6d 65 74 68 6f 64 20 72 65 pdate] method re
57b40 74 75 72 6e 73 0a 2a 2a 20 5b 53 51 4c 49 54 45 turns.** [SQLITE
57b50 5f 43 4f 4e 53 54 52 41 49 4e 54 5d 2c 20 53 51 _CONSTRAINT], SQ
57b60 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 68 69 Lite handles thi
57b70 73 20 61 73 20 69 66 20 74 68 65 20 4f 4e 20 43 s as if the ON C
57b80 4f 4e 46 4c 49 43 54 20 6d 6f 64 65 0a 2a 2a 20 ONFLICT mode.**
57b90 68 61 64 20 62 65 65 6e 20 41 42 4f 52 54 2e 0a had been ABORT..
57ba0 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 **.** Virtual ta
57bb0 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ble implementati
57bc0 6f 6e 73 20 74 68 61 74 20 61 72 65 20 72 65 71 ons that are req
57bd0 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 uired to handle
57be0 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a 20 6d 75 OR REPLACE.** mu
57bf0 73 74 20 64 6f 20 73 6f 20 77 69 74 68 69 6e 20 st do so within
57c00 74 68 65 20 5b 78 55 70 64 61 74 65 5d 20 6d 65 the [xUpdate] me
57c10 74 68 6f 64 2e 20 49 66 20 61 20 63 61 6c 6c 20 thod. If a call
57c20 74 6f 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69 to the .** [sqli
57c30 74 65 33 5f 76 74 61 62 5f 6f 6e 5f 63 6f 6e 66 te3_vtab_on_conf
57c40 6c 69 63 74 28 29 5d 20 66 75 6e 63 74 69 6f 6e lict()] function
57c50 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
57c60 74 68 65 20 63 75 72 72 65 6e 74 20 4f 4e 20 0a the current ON .
57c70 2a 2a 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 ** CONFLICT poli
57c80 63 79 20 69 73 20 52 45 50 4c 41 43 45 2c 20 74 cy is REPLACE, t
57c90 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
57ca0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
57cb0 73 68 6f 75 6c 64 20 0a 2a 2a 20 73 69 6c 65 6e should .** silen
57cc0 74 6c 79 20 72 65 70 6c 61 63 65 20 74 68 65 20 tly replace the
57cd0 61 70 70 72 6f 70 72 69 61 74 65 20 72 6f 77 73 appropriate rows
57ce0 20 77 69 74 68 69 6e 20 74 68 65 20 78 55 70 64 within the xUpd
57cf0 61 74 65 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 ate callback and
57d00 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .** return SQLIT
57d10 45 5f 4f 4b 2e 20 4f 72 2c 20 69 66 20 74 68 69 E_OK. Or, if thi
57d20 73 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c s is not possibl
57d30 65 2c 20 69 74 20 6d 61 79 20 72 65 74 75 72 6e e, it may return
57d40 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 .** SQLITE_CONST
57d50 52 41 49 4e 54 2c 20 69 6e 20 77 68 69 63 68 20 RAINT, in which
57d60 63 61 73 65 20 53 51 4c 69 74 65 20 66 61 6c 6c case SQLite fall
57d70 73 20 62 61 63 6b 20 74 6f 20 4f 52 20 41 42 4f s back to OR ABO
57d80 52 54 20 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e RT .** constrain
57d90 74 20 68 61 6e 64 6c 69 6e 67 2e 0a 2a 2a 20 3c t handling..** <
57da0 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 /dl>.*/.#define
57db0 53 51 4c 49 54 45 5f 56 54 41 42 5f 43 4f 4e 53 SQLITE_VTAB_CONS
57dc0 54 52 41 49 4e 54 5f 53 55 50 50 4f 52 54 20 31 TRAINT_SUPPORT 1
57dd0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
57de0 3a 20 44 65 74 65 72 6d 69 6e 65 20 54 68 65 20 : Determine The
57df0 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43 6f Virtual Table Co
57e00 6e 66 6c 69 63 74 20 50 6f 6c 69 63 79 0a 2a 2a nflict Policy.**
57e10 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
57e20 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 n may only be ca
57e30 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e lled from within
57e40 20 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 5b a call to the [
57e50 78 55 70 64 61 74 65 5d 20 6d 65 74 68 6f 64 0a xUpdate] method.
57e60 2a 2a 20 6f 66 20 61 20 5b 76 69 72 74 75 61 6c ** of a [virtual
57e70 20 74 61 62 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e table] implemen
57e80 74 61 74 69 6f 6e 20 66 6f 72 20 61 6e 20 49 4e tation for an IN
57e90 53 45 52 54 20 6f 72 20 55 50 44 41 54 45 20 6f SERT or UPDATE o
57ea0 70 65 72 61 74 69 6f 6e 2e 20 5e 54 68 65 0a 2a peration. ^The.*
57eb0 2a 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 * value returned
57ec0 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 is one of [SQLI
57ed0 54 45 5f 52 4f 4c 4c 42 41 43 4b 5d 2c 20 5b 53 TE_ROLLBACK], [S
57ee0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 2c 20 5b QLITE_IGNORE], [
57ef0 53 51 4c 49 54 45 5f 46 41 49 4c 5d 2c 0a 2a 2a SQLITE_FAIL],.**
57f00 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2c [SQLITE_ABORT],
57f10 20 6f 72 20 5b 53 51 4c 49 54 45 5f 52 45 50 4c or [SQLITE_REPL
57f20 41 43 45 5d 2c 20 61 63 63 6f 72 64 69 6e 67 20 ACE], according
57f30 74 6f 20 74 68 65 20 5b 4f 4e 20 43 4f 4e 46 4c to the [ON CONFL
57f40 49 43 54 5d 20 6d 6f 64 65 0a 2a 2a 20 6f 66 20 ICT] mode.** of
57f50 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
57f60 74 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64 t that triggered
57f70 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 68 65 the call to the
57f80 20 5b 78 55 70 64 61 74 65 5d 20 6d 65 74 68 6f [xUpdate] metho
57f90 64 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 76 69 72 d of the.** [vir
57fa0 74 75 61 6c 20 74 61 62 6c 65 5d 2e 0a 2a 2f 0a tual table]..*/.
57fb0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
57fc0 71 6c 69 74 65 33 5f 76 74 61 62 5f 6f 6e 5f 63 qlite3_vtab_on_c
57fd0 6f 6e 66 6c 69 63 74 28 73 71 6c 69 74 65 33 20 onflict(sqlite3
57fe0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
57ff0 52 45 46 3a 20 43 6f 6e 66 6c 69 63 74 20 72 65 REF: Conflict re
58000 73 6f 6c 75 74 69 6f 6e 20 6d 6f 64 65 73 0a 2a solution modes.*
58010 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 *.** These const
58020 61 6e 74 73 20 61 72 65 20 72 65 74 75 72 6e 65 ants are returne
58030 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 76 74 d by [sqlite3_vt
58040 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63 74 28 29 ab_on_conflict()
58050 5d 20 74 6f 0a 2a 2a 20 69 6e 66 6f 72 6d 20 61 ] to.** inform a
58060 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d [virtual table]
58070 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
58080 77 68 61 74 20 74 68 65 20 5b 4f 4e 20 43 4f 4e what the [ON CON
58090 46 4c 49 43 54 5d 20 6d 6f 64 65 0a 2a 2a 20 69 FLICT] mode.** i
580a0 73 20 66 6f 72 20 74 68 65 20 53 51 4c 20 73 74 s for the SQL st
580b0 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 76 atement being ev
580c0 61 6c 75 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e aluated..**.** N
580d0 6f 74 65 20 74 68 61 74 20 74 68 65 20 5b 53 51 ote that the [SQ
580e0 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 63 6f 6e LITE_IGNORE] con
580f0 73 74 61 6e 74 20 69 73 20 61 6c 73 6f 20 75 73 stant is also us
58100 65 64 20 61 73 20 61 20 70 6f 74 65 6e 74 69 61 ed as a potentia
58110 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 l.** return valu
58120 65 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 e from the [sqli
58130 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a te3_set_authoriz
58140 65 72 28 29 5d 20 63 61 6c 6c 62 61 63 6b 20 61 er()] callback a
58150 6e 64 20 74 68 61 74 0a 2a 2a 20 5b 53 51 4c 49 nd that.** [SQLI
58160 54 45 5f 41 42 4f 52 54 5d 20 69 73 20 61 6c 73 TE_ABORT] is als
58170 6f 20 61 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 o a [result code
58180 5d 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ]..*/.#define SQ
58190 4c 49 54 45 5f 52 4f 4c 4c 42 41 43 4b 20 31 0a LITE_ROLLBACK 1.
581a0 2f 2a 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 /* #define SQLIT
581b0 45 5f 49 47 4e 4f 52 45 20 32 20 2f 2f 20 41 6c E_IGNORE 2 // Al
581c0 73 6f 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 so used by sqlit
581d0 65 33 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 20 e3_authorizer()
581e0 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 23 64 65 66 callback */.#def
581f0 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 49 4c 20 ine SQLITE_FAIL
58200 20 20 20 20 33 0a 2f 2a 20 23 64 65 66 69 6e 65 3./* #define
58210 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 34 20 SQLITE_ABORT 4
58220 20 2f 2f 20 41 6c 73 6f 20 61 6e 20 65 72 72 6f // Also an erro
58230 72 20 63 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e r code */.#defin
58240 65 20 53 51 4c 49 54 45 5f 52 45 50 4c 41 43 45 e SQLITE_REPLACE
58250 20 20 35 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 5..../*.** Und
58260 6f 20 74 68 65 20 68 61 63 6b 20 74 68 61 74 20 o the hack that
58270 63 6f 6e 76 65 72 74 73 20 66 6c 6f 61 74 69 6e converts floatin
58280 67 20 70 6f 69 6e 74 20 74 79 70 65 73 20 74 6f g point types to
58290 20 69 6e 74 65 67 65 72 20 66 6f 72 0a 2a 2a 20 integer for.**
582a0 62 75 69 6c 64 73 20 6f 6e 20 70 72 6f 63 65 73 builds on proces
582b0 73 6f 72 73 20 77 69 74 68 6f 75 74 20 66 6c 6f sors without flo
582c0 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 ating point supp
582d0 6f 72 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ort..*/.#ifdef S
582e0 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
582f0 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 75 6e 64 65 ING_POINT.# unde
58300 66 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a f double.#endif.
58310 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 45 6e 64 .#if 0.} /* End
58320 20 6f 66 20 74 68 65 20 27 65 78 74 65 72 6e 20 of the 'extern
58330 22 43 22 27 20 62 6c 6f 63 6b 20 2a 2f 0a 23 65 "C"' block */.#e
58340 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a ndif.#endif../*.
58350 2a 2a 20 32 30 31 30 20 41 75 67 75 73 74 20 33 ** 2010 August 3
58360 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 0.**.** The auth
58370 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
58380 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
58390 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
583a0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
583b0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
583c0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
583d0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
583e0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
583f0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
58400 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
58410 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
58420 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
58430 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
58440 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
58450 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
58460 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
58470 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
58480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
584a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
584b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
584c0 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 53 51 .*/..#ifndef _SQ
584d0 4c 49 54 45 33 52 54 52 45 45 5f 48 5f 0a 23 64 LITE3RTREE_H_.#d
584e0 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 33 52 54 efine _SQLITE3RT
584f0 52 45 45 5f 48 5f 0a 0a 0a 23 69 66 20 30 0a 65 REE_H_...#if 0.e
58500 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 xtern "C" {.#end
58510 69 66 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 if..typedef stru
58520 63 74 20 73 71 6c 69 74 65 33 5f 72 74 72 65 65 ct sqlite3_rtree
58530 5f 67 65 6f 6d 65 74 72 79 20 73 71 6c 69 74 65 _geometry sqlite
58540 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79 3_rtree_geometry
58550 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 ;../*.** Registe
58560 72 20 61 20 67 65 6f 6d 65 74 72 79 20 63 61 6c r a geometry cal
58570 6c 62 61 63 6b 20 6e 61 6d 65 64 20 7a 47 65 6f lback named zGeo
58580 6d 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 m that can be us
58590 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e ed as part of an
585a0 0a 2a 2a 20 52 2d 54 72 65 65 20 67 65 6f 6d 65 .** R-Tree geome
585b0 74 72 79 20 71 75 65 72 79 20 61 73 20 66 6f 6c try query as fol
585c0 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 lows:.**.** SE
585d0 4c 45 43 54 20 2e 2e 2e 20 46 52 4f 4d 20 3c 72 LECT ... FROM <r
585e0 74 72 65 65 3e 20 57 48 45 52 45 20 3c 72 74 72 tree> WHERE <rtr
585f0 65 65 20 63 6f 6c 3e 20 4d 41 54 43 48 20 24 7a ee col> MATCH $z
58600 47 65 6f 6d 28 2e 2e 2e 20 70 61 72 61 6d 73 20 Geom(... params
58610 2e 2e 2e 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ...).*/.SQLITE_A
58620 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 PI int sqlite3_r
58630 74 72 65 65 5f 67 65 6f 6d 65 74 72 79 5f 63 61 tree_geometry_ca
58640 6c 6c 62 61 63 6b 28 0a 20 20 73 71 6c 69 74 65 llback(. sqlite
58650 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 3 *db,. const c
58660 68 61 72 20 2a 7a 47 65 6f 6d 2c 0a 23 69 66 64 har *zGeom,.#ifd
58670 65 66 20 53 51 4c 49 54 45 5f 52 54 52 45 45 5f ef SQLITE_RTREE_
58680 49 4e 54 5f 4f 4e 4c 59 0a 20 20 69 6e 74 20 28 INT_ONLY. int (
58690 2a 78 47 65 6f 6d 29 28 73 71 6c 69 74 65 33 5f *xGeom)(sqlite3_
586a0 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79 2a 2c rtree_geometry*,
586b0 20 69 6e 74 20 6e 2c 20 73 71 6c 69 74 65 33 5f int n, sqlite3_
586c0 69 6e 74 36 34 20 2a 61 2c 20 69 6e 74 20 2a 70 int64 *a, int *p
586d0 52 65 73 29 2c 0a 23 65 6c 73 65 0a 20 20 69 6e Res),.#else. in
586e0 74 20 28 2a 78 47 65 6f 6d 29 28 73 71 6c 69 74 t (*xGeom)(sqlit
586f0 65 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 e3_rtree_geometr
58700 79 2a 2c 20 69 6e 74 20 6e 2c 20 64 6f 75 62 6c y*, int n, doubl
58710 65 20 2a 61 2c 20 69 6e 74 20 2a 70 52 65 73 29 e *a, int *pRes)
58720 2c 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 20 ,.#endif. void
58730 2a 70 43 6f 6e 74 65 78 74 0a 29 3b 0a 0a 0a 2f *pContext.);.../
58740 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 *.** A pointer t
58750 6f 20 61 20 73 74 72 75 63 74 75 72 65 20 6f 66 o a structure of
58760 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 the following t
58770 79 70 65 20 69 73 20 70 61 73 73 65 64 20 61 73 ype is passed as
58780 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 the first.** ar
58790 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 62 61 gument to callba
587a0 63 6b 73 20 72 65 67 69 73 74 65 72 65 64 20 75 cks registered u
587b0 73 69 6e 67 20 72 74 72 65 65 5f 67 65 6f 6d 65 sing rtree_geome
587c0 74 72 79 5f 63 61 6c 6c 62 61 63 6b 28 29 2e 0a try_callback()..
587d0 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
587e0 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79 3_rtree_geometry
587f0 20 7b 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6e 74 {. void *pCont
58800 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ext;
58810 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 /* Copy of
58820 70 43 6f 6e 74 65 78 74 20 70 61 73 73 65 64 20 pContext passed
58830 74 6f 20 73 5f 72 5f 67 5f 63 28 29 20 2a 2f 0a to s_r_g_c() */.
58840 20 20 69 6e 74 20 6e 50 61 72 61 6d 3b 20 20 20 int nParam;
58850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58860 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72 72 /* Size of arr
58870 61 79 20 61 50 61 72 61 6d 5b 5d 20 2a 2f 0a 20 ay aParam[] */.
58880 20 64 6f 75 62 6c 65 20 2a 61 50 61 72 61 6d 3b double *aParam;
58890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
588a0 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 73 20 70 /* Parameters p
588b0 61 73 73 65 64 20 74 6f 20 53 51 4c 20 67 65 6f assed to SQL geo
588c0 6d 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 m function */.
588d0 76 6f 69 64 20 2a 70 55 73 65 72 3b 20 20 20 20 void *pUser;
588e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
588f0 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 69 6d 70 6c /* Callback impl
58900 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 72 20 ementation user
58910 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 28 data */. void (
58920 2a 78 44 65 6c 55 73 65 72 29 28 76 6f 69 64 20 *xDelUser)(void
58930 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43 61 6c *); /* Cal
58940 6c 65 64 20 62 79 20 53 51 4c 69 74 65 20 74 6f led by SQLite to
58950 20 63 6c 65 61 6e 20 75 70 20 70 55 73 65 72 20 clean up pUser
58960 2a 2f 0a 7d 3b 0a 0a 0a 23 69 66 20 30 0a 7d 20 */.};...#if 0.}
58970 20 2f 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 27 /* end of the '
58980 65 78 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 extern "C"' bloc
58990 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 23 65 6e k */.#endif..#en
589a0 64 69 66 20 20 2f 2a 20 69 66 6e 64 65 66 20 5f dif /* ifndef _
589b0 53 51 4c 49 54 45 33 52 54 52 45 45 5f 48 5f 20 SQLITE3RTREE_H_
589c0 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */.../**********
589d0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 **** End of sqli
589e0 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a te3.h **********
589f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58a10 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
58a20 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
58a30 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
58a40 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 f in sqliteInt.h
58a50 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
58a60 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
58a70 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61 73 **** Include has
58a80 68 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c h.h in the middl
58a90 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 e of sqliteInt.h
58aa0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
58ab0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
58ac0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
58ad0 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a hash.h *********
58ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58b00 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 ***/./*.** 2001
58b10 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a September 22.**.
58b20 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
58b30 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
58b40 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
58b50 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
58b60 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
58b70 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
58b80 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
58b90 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
58ba0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
58bb0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
58bc0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
58bd0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
58be0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
58bf0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
58c00 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
58c10 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
58c20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
58c30 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
58c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
58c80 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65 his is the heade
58c90 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67 r file for the g
58ca0 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c eneric hash-tabl
58cb0 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a e implemenation.
58cc0 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 ** used in SQLit
58cd0 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 e..*/.#ifndef _S
58ce0 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0a 23 64 QLITE_HASH_H_.#d
58cf0 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 48 41 efine _SQLITE_HA
58d00 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 SH_H_../* Forwar
58d10 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f d declarations o
58d20 66 20 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f f structures. */
58d30 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
58d40 48 61 73 68 20 48 61 73 68 3b 0a 74 79 70 65 64 Hash Hash;.typed
58d50 65 66 20 73 74 72 75 63 74 20 48 61 73 68 45 6c ef struct HashEl
58d60 65 6d 20 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a em HashElem;../*
58d70 20 41 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 A complete hash
58d80 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 table is an ins
58d90 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
58da0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
58db0 2e 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 ..** The interna
58dc0 6c 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ls of this struc
58dd0 74 75 72 65 20 61 72 65 20 69 6e 74 65 6e 64 65 ture are intende
58de0 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 2d d to be opaque -
58df0 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 - client.** code
58e00 20 73 68 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65 should not atte
58e10 6d 70 74 20 74 6f 20 61 63 63 65 73 73 20 6f 72 mpt to access or
58e20 20 6d 6f 64 69 66 79 20 74 68 65 20 66 69 65 6c modify the fiel
58e30 64 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ds of this struc
58e40 74 75 72 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 ture.** directly
58e50 2e 20 20 43 68 61 6e 67 65 20 74 68 69 73 20 73 . Change this s
58e60 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 62 79 tructure only by
58e70 20 75 73 69 6e 67 20 74 68 65 20 72 6f 75 74 69 using the routi
58e80 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f nes below..** Ho
58e90 77 65 76 65 72 2c 20 73 6f 6d 65 20 6f 66 20 74 wever, some of t
58ea0 68 65 20 22 70 72 6f 63 65 64 75 72 65 73 22 20 he "procedures"
58eb0 61 6e 64 20 22 66 75 6e 63 74 69 6f 6e 73 22 20 and "functions"
58ec0 66 6f 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e for modifying an
58ed0 64 0a 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 d.** accessing t
58ee0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 his structure ar
58ef0 65 20 72 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c e really macros,
58f00 20 73 6f 20 77 65 20 63 61 6e 27 74 20 72 65 61 so we can't rea
58f10 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 lly make.** this
58f20 20 73 74 72 75 63 74 75 72 65 20 6f 70 61 71 75 structure opaqu
58f30 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65 6c 65 e..**.** All ele
58f40 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 68 61 73 ments of the has
58f50 68 20 74 61 62 6c 65 20 61 72 65 20 6f 6e 20 61 h table are on a
58f60 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c single doubly-l
58f70 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 20 48 inked list..** H
58f80 61 73 68 2e 66 69 72 73 74 20 70 6f 69 6e 74 73 ash.first points
58f90 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 to the head of
58fa0 74 68 69 73 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a this list..**.**
58fb0 20 54 68 65 72 65 20 61 72 65 20 48 61 73 68 2e There are Hash.
58fc0 68 74 73 69 7a 65 20 62 75 63 6b 65 74 73 2e 20 htsize buckets.
58fd0 20 45 61 63 68 20 62 75 63 6b 65 74 20 70 6f 69 Each bucket poi
58fe0 6e 74 73 20 74 6f 20 61 20 73 70 6f 74 20 69 6e nts to a spot in
58ff0 0a 2a 2a 20 74 68 65 20 67 6c 6f 62 61 6c 20 64 .** the global d
59000 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 oubly-linked lis
59010 74 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 t. The contents
59020 20 6f 66 20 74 68 65 20 62 75 63 6b 65 74 20 61 of the bucket a
59030 72 65 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e re the.** elemen
59040 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 70 6c 75 t pointed to plu
59050 73 20 74 68 65 20 6e 65 78 74 20 5f 68 74 2e 63 s the next _ht.c
59060 6f 75 6e 74 2d 31 20 65 6c 65 6d 65 6e 74 73 20 ount-1 elements
59070 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a in the list..**.
59080 2a 2a 20 48 61 73 68 2e 68 74 73 69 7a 65 20 61 ** Hash.htsize a
59090 6e 64 20 48 61 73 68 2e 68 74 20 6d 61 79 20 62 nd Hash.ht may b
590a0 65 20 7a 65 72 6f 2e 20 20 49 6e 20 74 68 61 74 e zero. In that
590b0 20 63 61 73 65 20 6c 6f 6f 6b 75 70 20 69 73 20 case lookup is
590c0 64 6f 6e 65 0a 2a 2a 20 62 79 20 61 20 6c 69 6e done.** by a lin
590d0 65 61 72 20 73 65 61 72 63 68 20 6f 66 20 74 68 ear search of th
590e0 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 2e 20 20 e global list.
590f0 46 6f 72 20 73 6d 61 6c 6c 20 74 61 62 6c 65 73 For small tables
59100 2c 20 74 68 65 20 0a 2a 2a 20 48 61 73 68 2e 68 , the .** Hash.h
59110 74 20 74 61 62 6c 65 20 69 73 20 6e 65 76 65 72 t table is never
59120 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 63 61 75 allocated becau
59130 73 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20 se if there are
59140 66 65 77 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 few elements.**
59150 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 69 74 in the table, it
59160 20 69 73 20 66 61 73 74 65 72 20 74 6f 20 64 6f is faster to do
59170 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68 a linear search
59180 20 74 68 61 6e 20 74 6f 20 6d 61 6e 61 67 65 0a than to manage.
59190 2a 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c ** the hash tabl
591a0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 e..*/.struct Has
591b0 68 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 h {. unsigned i
591c0 6e 74 20 68 74 73 69 7a 65 3b 20 20 20 20 20 20 nt htsize;
591d0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 75 63 /* Number of buc
591e0 6b 65 74 73 20 69 6e 20 74 68 65 20 68 61 73 68 kets in the hash
591f0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 table */. unsi
59200 67 6e 65 64 20 69 6e 74 20 63 6f 75 6e 74 3b 20 gned int count;
59210 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
59220 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 of entries in th
59230 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 is table */. Ha
59240 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20 20 shElem *first;
59250 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
59260 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 irst element of
59270 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 the array */. s
59280 74 72 75 63 74 20 5f 68 74 20 7b 20 20 20 20 20 truct _ht {
59290 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 /* the
592a0 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 hash table */.
592b0 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 int count;
592c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
592d0 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
592e0 73 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68 s with this hash
592f0 20 2a 2f 0a 20 20 20 20 48 61 73 68 45 6c 65 6d */. HashElem
59300 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 20 20 20 *chain;
59310 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
59320 20 66 69 72 73 74 20 65 6e 74 72 79 20 77 69 74 first entry wit
59330 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 h this hash */.
59340 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 } *ht;.};../* E
59350 61 63 68 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 ach element in t
59360 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 he hash table is
59370 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
59380 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a the following .*
59390 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 6c * structure. Al
593a0 6c 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 73 l elements are s
593b0 74 6f 72 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c tored on a singl
593c0 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 e doubly-linked
593d0 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 list..**.** Agai
593e0 6e 2c 20 74 68 69 73 20 73 74 72 75 63 74 75 72 n, this structur
593f0 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f e is intended to
59400 20 62 65 20 6f 70 61 71 75 65 2c 20 62 75 74 20 be opaque, but
59410 69 74 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 0a it can't really.
59420 2a 2a 20 62 65 20 6f 70 61 71 75 65 20 62 65 63 ** be opaque bec
59430 61 75 73 65 20 69 74 20 69 73 20 75 73 65 64 20 ause it is used
59440 62 79 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 by macros..*/.st
59450 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 7b 0a ruct HashElem {.
59460 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 HashElem *next
59470 2c 20 2a 70 72 65 76 3b 20 20 20 20 20 20 20 2f , *prev; /
59480 2a 20 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69 * Next and previ
59490 6f 75 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 ous elements in
594a0 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 the table */. v
594b0 6f 69 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20 oid *data;
594c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
594d0 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 ata associated w
594e0 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 ith this element
594f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
59500 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79 *pKey; int nKey
59510 3b 20 20 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69 ; /* Key associ
59520 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65 ated with this e
59530 6c 65 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a lement */.};../*
59540 0a 2a 2a 20 41 63 63 65 73 73 20 72 6f 75 74 69 .** Access routi
59550 6e 65 73 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c nes. To delete,
59560 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 70 insert a NULL p
59570 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 ointer..*/.SQLIT
59580 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
59590 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 48 qlite3HashInit(H
595a0 61 73 68 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ash*);.SQLITE_PR
595b0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
595c0 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 48 61 te3HashInsert(Ha
595d0 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 sh*, const char
595e0 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c *pKey, int nKey,
595f0 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a 53 void *pData);.S
59600 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
59610 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46 id *sqlite3HashF
59620 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 2a 2c ind(const Hash*,
59630 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 const char *pKe
59640 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 y, int nKey);.SQ
59650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
59660 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 d sqlite3HashCle
59670 61 72 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a ar(Hash*);../*.*
59680 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f * Macros for loo
59690 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c ping over all el
596a0 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68 ements of a hash
596b0 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69 table. The idi
596c0 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68 om is.** like th
596d0 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 73 68 is:.**.** Hash
596e0 20 68 3b 0a 2a 2a 20 20 20 48 61 73 68 45 6c 65 h;.** HashEle
596f0 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a m *p;.** ....*
59700 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 * for(p=sqlite
59710 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20 70 HashFirst(&h); p
59720 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 ; p=sqliteHashNe
59730 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53 xt(p)){.** S
59740 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70 44 omeStructure *pD
59750 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61 73 68 ata = sqliteHash
59760 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20 20 Data(p);.**
59770 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 // do something
59780 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 20 20 with pData.**
59790 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c }.*/.#define sql
597a0 69 74 65 48 61 73 68 46 69 72 73 74 28 48 29 20 iteHashFirst(H)
597b0 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64 ((H)->first).#d
597c0 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 efine sqliteHash
597d0 4e 65 78 74 28 45 29 20 20 20 28 28 45 29 2d 3e Next(E) ((E)->
597e0 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20 73 71 next).#define sq
597f0 6c 69 74 65 48 61 73 68 44 61 74 61 28 45 29 20 liteHashData(E)
59800 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 2f 2a ((E)->data)./*
59810 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 #define sqliteH
59820 61 73 68 4b 65 79 28 45 29 20 20 20 20 28 28 45 ashKey(E) ((E
59830 29 2d 3e 70 4b 65 79 29 20 2f 2f 20 4e 4f 54 20 )->pKey) // NOT
59840 55 53 45 44 20 2a 2f 0a 2f 2a 20 23 64 65 66 69 USED */./* #defi
59850 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4b 65 79 ne sqliteHashKey
59860 73 69 7a 65 28 45 29 20 28 28 45 29 2d 3e 6e 4b size(E) ((E)->nK
59870 65 79 29 20 20 2f 2f 20 4e 4f 54 20 55 53 45 44 ey) // NOT USED
59880 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 */../*.** Numbe
59890 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
598a0 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a a hash table.*/.
598b0 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 /* #define sqlit
598c0 65 48 61 73 68 43 6f 75 6e 74 28 48 29 20 20 28 eHashCount(H) (
598d0 28 48 29 2d 3e 63 6f 75 6e 74 29 20 2f 2f 20 4e (H)->count) // N
598e0 4f 54 20 55 53 45 44 20 2a 2f 0a 0a 23 65 6e 64 OT USED */..#end
598f0 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 48 41 if /* _SQLITE_HA
59900 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a SH_H_ */../*****
59910 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
59920 20 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a hash.h ********
59930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59950 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
59960 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
59970 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
59980 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
59990 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
599a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
599b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
599c0 65 20 70 61 72 73 65 2e 68 20 69 6e 20 74 68 65 e parse.h in the
599d0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 middle of sqlit
599e0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
599f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
59a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
59a10 66 69 6c 65 20 70 61 72 73 65 2e 68 20 2a 2a 2a file parse.h ***
59a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59a40 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e ********/.#defin
59a50 65 20 54 4b 5f 53 45 4d 49 20 20 20 20 20 20 20 e TK_SEMI
59a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59a70 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 54 1.#define T
59a80 4b 5f 45 58 50 4c 41 49 4e 20 20 20 20 20 20 20 K_EXPLAIN
59a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59aa0 20 20 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 51 2.#define TK_Q
59ab0 55 45 52 59 20 20 20 20 20 20 20 20 20 20 20 20 UERY
59ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
59ad0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 41 4e .#define TK_PLAN
59ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59af0 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 4.#d
59b00 65 66 69 6e 65 20 54 4b 5f 42 45 47 49 4e 20 20 efine TK_BEGIN
59b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59b20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 5.#defi
59b30 6e 65 20 54 4b 5f 54 52 41 4e 53 41 43 54 49 4f ne TK_TRANSACTIO
59b40 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 N
59b50 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 6.#define
59b60 54 4b 5f 44 45 46 45 52 52 45 44 20 20 20 20 20 TK_DEFERRED
59b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59b80 20 20 20 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 7.#define TK_
59b90 49 4d 4d 45 44 49 41 54 45 20 20 20 20 20 20 20 IMMEDIATE
59ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59bb0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 43 8.#define TK_EXC
59bc0 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 20 20 LUSIVE
59bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 0a 23 9.#
59be0 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 49 54 define TK_COMMIT
59bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59c00 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 10.#def
59c10 69 6e 65 20 54 4b 5f 45 4e 44 20 20 20 20 20 20 ine TK_END
59c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59c30 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 11.#define
59c40 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 20 TK_ROLLBACK
59c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59c60 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 54 4b 12.#define TK
59c70 5f 53 41 56 45 50 4f 49 4e 54 20 20 20 20 20 20 _SAVEPOINT
59c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59c90 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 13.#define TK_RE
59ca0 4c 45 41 53 45 20 20 20 20 20 20 20 20 20 20 20 LEASE
59cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 0a 14.
59cc0 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 20 20 20 #define TK_TO
59cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59ce0 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64 65 15.#de
59cf0 66 69 6e 65 20 54 4b 5f 54 41 42 4c 45 20 20 20 fine TK_TABLE
59d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59d10 20 20 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e 16.#defin
59d20 65 20 54 4b 5f 43 52 45 41 54 45 20 20 20 20 20 e TK_CREATE
59d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59d40 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65 20 54 17.#define T
59d50 4b 5f 49 46 20 20 20 20 20 20 20 20 20 20 20 20 K_IF
59d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59d70 20 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 18.#define TK_N
59d80 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OT
59d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39 19
59da0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 49 53 .#define TK_EXIS
59db0 54 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TS
59dc0 20 20 20 20 20 20 20 20 20 20 20 32 30 0a 23 64 20.#d
59dd0 65 66 69 6e 65 20 54 4b 5f 54 45 4d 50 20 20 20 efine TK_TEMP
59de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59df0 20 20 20 20 20 20 20 20 32 31 0a 23 64 65 66 69 21.#defi
59e00 6e 65 20 54 4b 5f 4c 50 20 20 20 20 20 20 20 20 ne TK_LP
59e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59e20 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20 22.#define
59e30 54 4b 5f 52 50 20 20 20 20 20 20 20 20 20 20 20 TK_RP
59e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59e50 20 20 32 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 23.#define TK_
59e60 41 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AS
59e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
59e80 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4.#define TK_COM
59e90 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 MA
59ea0 20 20 20 20 20 20 20 20 20 20 20 20 32 35 0a 23 25.#
59eb0 64 65 66 69 6e 65 20 54 4b 5f 49 44 20 20 20 20 define TK_ID
59ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59ed0 20 20 20 20 20 20 20 20 20 32 36 0a 23 64 65 66 26.#def
59ee0 69 6e 65 20 54 4b 5f 49 4e 44 45 58 45 44 20 20 ine TK_INDEXED
59ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59f00 20 20 20 20 20 20 32 37 0a 23 64 65 66 69 6e 65 27.#define
59f10 20 54 4b 5f 41 42 4f 52 54 20 20 20 20 20 20 20 TK_ABORT
59f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59f30 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20 54 4b 28.#define TK
59f40 5f 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 _ACTION
59f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59f60 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 46 29.#define TK_AF
59f70 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 TER
59f80 20 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a 30.
59f90 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 41 4c 59 #define TK_ANALY
59fa0 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ZE
59fb0 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 64 65 31.#de
59fc0 66 69 6e 65 20 54 4b 5f 41 53 43 20 20 20 20 20 fine TK_ASC
59fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59fe0 20 20 20 20 20 20 20 33 32 0a 23 64 65 66 69 6e 32.#defin
59ff0 65 20 54 4b 5f 41 54 54 41 43 48 20 20 20 20 20 e TK_ATTACH
5a000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a010 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20 54 33.#define T
5a020 4b 5f 42 45 46 4f 52 45 20 20 20 20 20 20 20 20 K_BEFORE
5a030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a040 20 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 34.#define TK_B
5a050 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Y
5a060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 35 35
5a070 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 43 .#define TK_CASC
5a080 41 44 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ADE
5a090 20 20 20 20 20 20 20 20 20 20 20 33 36 0a 23 64 36.#d
5a0a0 65 66 69 6e 65 20 54 4b 5f 43 41 53 54 20 20 20 efine TK_CAST
5a0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a0c0 20 20 20 20 20 20 20 20 33 37 0a 23 64 65 66 69 37.#defi
5a0d0 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 20 20 ne TK_COLUMNKW
5a0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a0f0 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e 65 20 38.#define
5a100 54 4b 5f 43 4f 4e 46 4c 49 43 54 20 20 20 20 20 TK_CONFLICT
5a110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a120 20 20 33 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 39.#define TK_
5a130 44 41 54 41 42 41 53 45 20 20 20 20 20 20 20 20 DATABASE
5a140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
5a150 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 53 0.#define TK_DES
5a160 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 C
5a170 20 20 20 20 20 20 20 20 20 20 20 20 34 31 0a 23 41.#
5a180 64 65 66 69 6e 65 20 54 4b 5f 44 45 54 41 43 48 define TK_DETACH
5a190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a1a0 20 20 20 20 20 20 20 20 20 34 32 0a 23 64 65 66 42.#def
5a1b0 69 6e 65 20 54 4b 5f 45 41 43 48 20 20 20 20 20 ine TK_EACH
5a1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a1d0 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e 65 43.#define
5a1e0 20 54 4b 5f 46 41 49 4c 20 20 20 20 20 20 20 20 TK_FAIL
5a1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a200 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20 54 4b 44.#define TK
5a210 5f 46 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 _FOR
5a220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a230 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 47 45.#define TK_IG
5a240 4e 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 NORE
5a250 20 20 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 46.
5a260 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 49 54 49 #define TK_INITI
5a270 41 4c 4c 59 20 20 20 20 20 20 20 20 20 20 20 20 ALLY
5a280 20 20 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 47.#de
5a290 66 69 6e 65 20 54 4b 5f 49 4e 53 54 45 41 44 20 fine TK_INSTEAD
5a2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a2b0 20 20 20 20 20 20 20 34 38 0a 23 64 65 66 69 6e 48.#defin
5a2c0 65 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 20 20 20 e TK_LIKE_KW
5a2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a2e0 20 20 20 20 34 39 0a 23 64 65 66 69 6e 65 20 54 49.#define T
5a2f0 4b 5f 4d 41 54 43 48 20 20 20 20 20 20 20 20 20 K_MATCH
5a300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a310 20 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 50.#define TK_N
5a320 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 O
5a330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31 51
5a340 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4b 45 59 20 .#define TK_KEY
5a350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a360 20 20 20 20 20 20 20 20 20 20 20 35 32 0a 23 64 52.#d
5a370 65 66 69 6e 65 20 54 4b 5f 4f 46 20 20 20 20 20 efine TK_OF
5a380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a390 20 20 20 20 20 20 20 20 35 33 0a 23 64 65 66 69 53.#defi
5a3a0 6e 65 20 54 4b 5f 4f 46 46 53 45 54 20 20 20 20 ne TK_OFFSET
5a3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a3c0 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20 54.#define
5a3d0 54 4b 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20 TK_PRAGMA
5a3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a3f0 20 20 35 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55.#define TK_
5a400 52 41 49 53 45 20 20 20 20 20 20 20 20 20 20 20 RAISE
5a410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
5a420 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 50 6.#define TK_REP
5a430 4c 41 43 45 20 20 20 20 20 20 20 20 20 20 20 20 LACE
5a440 20 20 20 20 20 20 20 20 20 20 20 20 35 37 0a 23 57.#
5a450 64 65 66 69 6e 65 20 54 4b 5f 52 45 53 54 52 49 define TK_RESTRI
5a460 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CT
5a470 20 20 20 20 20 20 20 20 20 35 38 0a 23 64 65 66 58.#def
5a480 69 6e 65 20 54 4b 5f 52 4f 57 20 20 20 20 20 20 ine TK_ROW
5a490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a4a0 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65 59.#define
5a4b0 20 54 4b 5f 54 52 49 47 47 45 52 20 20 20 20 20 TK_TRIGGER
5a4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a4d0 20 20 20 36 30 0a 23 64 65 66 69 6e 65 20 54 4b 60.#define TK
5a4e0 5f 56 41 43 55 55 4d 20 20 20 20 20 20 20 20 20 _VACUUM
5a4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a500 36 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 61.#define TK_VI
5a510 45 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 EW
5a520 20 20 20 20 20 20 20 20 20 20 20 20 20 36 32 0a 62.
5a530 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 52 54 55 #define TK_VIRTU
5a540 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AL
5a550 20 20 20 20 20 20 20 20 20 20 36 33 0a 23 64 65 63.#de
5a560 66 69 6e 65 20 54 4b 5f 52 45 49 4e 44 45 58 20 fine TK_REINDEX
5a570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a580 20 20 20 20 20 20 20 36 34 0a 23 64 65 66 69 6e 64.#defin
5a590 65 20 54 4b 5f 52 45 4e 41 4d 45 20 20 20 20 20 e TK_RENAME
5a5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a5b0 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65 20 54 65.#define T
5a5c0 4b 5f 43 54 49 4d 45 5f 4b 57 20 20 20 20 20 20 K_CTIME_KW
5a5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a5e0 20 36 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 66.#define TK_A
5a5f0 4e 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NY
5a600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 37 67
5a610 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 20 20 .#define TK_OR
5a620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a630 20 20 20 20 20 20 20 20 20 20 20 36 38 0a 23 64 68.#d
5a640 65 66 69 6e 65 20 54 4b 5f 41 4e 44 20 20 20 20 efine TK_AND
5a650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a660 20 20 20 20 20 20 20 20 36 39 0a 23 64 65 66 69 69.#defi
5a670 6e 65 20 54 4b 5f 49 53 20 20 20 20 20 20 20 20 ne TK_IS
5a680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a690 20 20 20 20 20 37 30 0a 23 64 65 66 69 6e 65 20 70.#define
5a6a0 54 4b 5f 42 45 54 57 45 45 4e 20 20 20 20 20 20 TK_BETWEEN
5a6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a6c0 20 20 37 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 71.#define TK_
5a6d0 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 IN
5a6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
5a6f0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e 2.#define TK_ISN
5a700 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 ULL
5a710 20 20 20 20 20 20 20 20 20 20 20 20 37 33 0a 23 73.#
5a720 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c define TK_NOTNUL
5a730 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 L
5a740 20 20 20 20 20 20 20 20 20 37 34 0a 23 64 65 66 74.#def
5a750 69 6e 65 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 ine TK_NE
5a760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a770 20 20 20 20 20 20 37 35 0a 23 64 65 66 69 6e 65 75.#define
5a780 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20 20 TK_EQ
5a790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a7a0 20 20 20 37 36 0a 23 64 65 66 69 6e 65 20 54 4b 76.#define TK
5a7b0 5f 47 54 20 20 20 20 20 20 20 20 20 20 20 20 20 _GT
5a7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a7d0 37 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 45 77.#define TK_LE
5a7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 38 0a 78.
5a800 23 64 65 66 69 6e 65 20 54 4b 5f 4c 54 20 20 20 #define TK_LT
5a810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a820 20 20 20 20 20 20 20 20 20 20 37 39 0a 23 64 65 79.#de
5a830 66 69 6e 65 20 54 4b 5f 47 45 20 20 20 20 20 20 fine TK_GE
5a840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a850 20 20 20 20 20 20 20 38 30 0a 23 64 65 66 69 6e 80.#defin
5a860 65 20 54 4b 5f 45 53 43 41 50 45 20 20 20 20 20 e TK_ESCAPE
5a870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a880 20 20 20 20 38 31 0a 23 64 65 66 69 6e 65 20 54 81.#define T
5a890 4b 5f 42 49 54 41 4e 44 20 20 20 20 20 20 20 20 K_BITAND
5a8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a8b0 20 38 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 82.#define TK_B
5a8c0 49 54 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 ITOR
5a8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 33 83
5a8e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 53 48 49 .#define TK_LSHI
5a8f0 46 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 FT
5a900 20 20 20 20 20 20 20 20 20 20 20 38 34 0a 23 64 84.#d
5a910 65 66 69 6e 65 20 54 4b 5f 52 53 48 49 46 54 20 efine TK_RSHIFT
5a920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a930 20 20 20 20 20 20 20 20 38 35 0a 23 64 65 66 69 85.#defi
5a940 6e 65 20 54 4b 5f 50 4c 55 53 20 20 20 20 20 20 ne TK_PLUS
5a950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a960 20 20 20 20 20 38 36 0a 23 64 65 66 69 6e 65 20 86.#define
5a970 54 4b 5f 4d 49 4e 55 53 20 20 20 20 20 20 20 20 TK_MINUS
5a980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a990 20 20 38 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 87.#define TK_
5a9a0 53 54 41 52 20 20 20 20 20 20 20 20 20 20 20 20 STAR
5a9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
5a9c0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 4c 41 8.#define TK_SLA
5a9d0 53 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 SH
5a9e0 20 20 20 20 20 20 20 20 20 20 20 20 38 39 0a 23 89.#
5a9f0 64 65 66 69 6e 65 20 54 4b 5f 52 45 4d 20 20 20 define TK_REM
5aa00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aa10 20 20 20 20 20 20 20 20 20 39 30 0a 23 64 65 66 90.#def
5aa20 69 6e 65 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 ine TK_CONCAT
5aa30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aa40 20 20 20 20 20 20 39 31 0a 23 64 65 66 69 6e 65 91.#define
5aa50 20 54 4b 5f 43 4f 4c 4c 41 54 45 20 20 20 20 20 TK_COLLATE
5aa60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aa70 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 54 4b 92.#define TK
5aa80 5f 42 49 54 4e 4f 54 20 20 20 20 20 20 20 20 20 _BITNOT
5aa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aaa0 39 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 93.#define TK_ST
5aab0 52 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 RING
5aac0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 34 0a 94.
5aad0 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 5f #define TK_JOIN_
5aae0 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 KW
5aaf0 20 20 20 20 20 20 20 20 20 20 39 35 0a 23 64 65 95.#de
5ab00 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 52 41 49 fine TK_CONSTRAI
5ab10 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NT
5ab20 20 20 20 20 20 20 20 39 36 0a 23 64 65 66 69 6e 96.#defin
5ab30 65 20 54 4b 5f 44 45 46 41 55 4c 54 20 20 20 20 e TK_DEFAULT
5ab40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ab50 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65 20 54 97.#define T
5ab60 4b 5f 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 K_NULL
5ab70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ab80 20 39 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 98.#define TK_P
5ab90 52 49 4d 41 52 59 20 20 20 20 20 20 20 20 20 20 RIMARY
5aba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 39 99
5abb0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 51 .#define TK_UNIQ
5abc0 55 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 UE
5abd0 20 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 100.#
5abe0 64 65 66 69 6e 65 20 54 4b 5f 43 48 45 43 4b 20 define TK_CHECK
5abf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ac00 20 20 20 20 20 20 20 20 20 31 30 31 0a 23 64 65 101.#de
5ac10 66 69 6e 65 20 54 4b 5f 52 45 46 45 52 45 4e 43 fine TK_REFERENC
5ac20 45 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ES
5ac30 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 66 69 102.#defi
5ac40 6e 65 20 54 4b 5f 41 55 54 4f 49 4e 43 52 20 20 ne TK_AUTOINCR
5ac50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ac60 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65 103.#define
5ac70 20 54 4b 5f 4f 4e 20 20 20 20 20 20 20 20 20 20 TK_ON
5ac80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ac90 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65 20 54 104.#define T
5aca0 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 K_INSERT
5acb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5acc0 20 31 30 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 105.#define TK_
5acd0 44 45 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 DELETE
5ace0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
5acf0 30 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 50 06.#define TK_UP
5ad00 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 DATE
5ad10 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37 107
5ad20 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 54 20 .#define TK_SET
5ad30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ad40 20 20 20 20 20 20 20 20 20 20 20 31 30 38 0a 23 108.#
5ad50 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 define TK_DEFERR
5ad60 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 ABLE
5ad70 20 20 20 20 20 20 20 20 20 31 30 39 0a 23 64 65 109.#de
5ad80 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49 47 4e 20 fine TK_FOREIGN
5ad90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ada0 20 20 20 20 20 20 20 31 31 30 0a 23 64 65 66 69 110.#defi
5adb0 6e 65 20 54 4b 5f 44 52 4f 50 20 20 20 20 20 20 ne TK_DROP
5adc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5add0 20 20 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65 111.#define
5ade0 20 54 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 20 20 TK_UNION
5adf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ae00 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 20 54 112.#define T
5ae10 4b 5f 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 K_ALL
5ae20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ae30 20 31 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 113.#define TK_
5ae40 45 58 43 45 50 54 20 20 20 20 20 20 20 20 20 20 EXCEPT
5ae50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
5ae60 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 14.#define TK_IN
5ae70 54 45 52 53 45 43 54 20 20 20 20 20 20 20 20 20 TERSECT
5ae80 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 35 115
5ae90 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4c 45 .#define TK_SELE
5aea0 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CT
5aeb0 20 20 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 116.#
5aec0 64 65 66 69 6e 65 20 54 4b 5f 44 49 53 54 49 4e define TK_DISTIN
5aed0 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CT
5aee0 20 20 20 20 20 20 20 20 20 31 31 37 0a 23 64 65 117.#de
5aef0 66 69 6e 65 20 54 4b 5f 44 4f 54 20 20 20 20 20 fine TK_DOT
5af00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5af10 20 20 20 20 20 20 20 31 31 38 0a 23 64 65 66 69 118.#defi
5af20 6e 65 20 54 4b 5f 46 52 4f 4d 20 20 20 20 20 20 ne TK_FROM
5af30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5af40 20 20 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65 119.#define
5af50 20 54 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 20 20 TK_JOIN
5af60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5af70 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 20 54 120.#define T
5af80 4b 5f 55 53 49 4e 47 20 20 20 20 20 20 20 20 20 K_USING
5af90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5afa0 20 31 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 121.#define TK_
5afb0 4f 52 44 45 52 20 20 20 20 20 20 20 20 20 20 20 ORDER
5afc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
5afd0 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 52 22.#define TK_GR
5afe0 4f 55 50 20 20 20 20 20 20 20 20 20 20 20 20 20 OUP
5aff0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33 123
5b000 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48 41 56 49 .#define TK_HAVI
5b010 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NG
5b020 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23 124.#
5b030 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4d 49 54 20 define TK_LIMIT
5b040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b050 20 20 20 20 20 20 20 20 20 31 32 35 0a 23 64 65 125.#de
5b060 66 69 6e 65 20 54 4b 5f 57 48 45 52 45 20 20 20 fine TK_WHERE
5b070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b080 20 20 20 20 20 20 20 31 32 36 0a 23 64 65 66 69 126.#defi
5b090 6e 65 20 54 4b 5f 49 4e 54 4f 20 20 20 20 20 20 ne TK_INTO
5b0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b0b0 20 20 20 20 20 31 32 37 0a 23 64 65 66 69 6e 65 127.#define
5b0c0 20 54 4b 5f 56 41 4c 55 45 53 20 20 20 20 20 20 TK_VALUES
5b0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b0e0 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 54 128.#define T
5b0f0 4b 5f 49 4e 54 45 47 45 52 20 20 20 20 20 20 20 K_INTEGER
5b100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b110 20 31 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 129.#define TK_
5b120 46 4c 4f 41 54 20 20 20 20 20 20 20 20 20 20 20 FLOAT
5b130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
5b140 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 4c 30.#define TK_BL
5b150 4f 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OB
5b160 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 31 131
5b170 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 47 49 .#define TK_REGI
5b180 53 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 STER
5b190 20 20 20 20 20 20 20 20 20 20 20 31 33 32 0a 23 132.#
5b1a0 64 65 66 69 6e 65 20 54 4b 5f 56 41 52 49 41 42 define TK_VARIAB
5b1b0 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LE
5b1c0 20 20 20 20 20 20 20 20 20 31 33 33 0a 23 64 65 133.#de
5b1d0 66 69 6e 65 20 54 4b 5f 43 41 53 45 20 20 20 20 fine TK_CASE
5b1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b1f0 20 20 20 20 20 20 20 31 33 34 0a 23 64 65 66 69 134.#defi
5b200 6e 65 20 54 4b 5f 57 48 45 4e 20 20 20 20 20 20 ne TK_WHEN
5b210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b220 20 20 20 20 20 31 33 35 0a 23 64 65 66 69 6e 65 135.#define
5b230 20 54 4b 5f 54 48 45 4e 20 20 20 20 20 20 20 20 TK_THEN
5b240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b250 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 54 136.#define T
5b260 4b 5f 45 4c 53 45 20 20 20 20 20 20 20 20 20 20 K_ELSE
5b270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b280 20 31 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 137.#define TK_
5b290 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 INDEX
5b2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
5b2b0 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4c 38.#define TK_AL
5b2c0 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 TER
5b2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 39 139
5b2e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 44 44 20 .#define TK_ADD
5b2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b300 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 23 140.#
5b310 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 54 45 58 define TK_TO_TEX
5b320 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
5b330 20 20 20 20 20 20 20 20 20 31 34 31 0a 23 64 65 141.#de
5b340 66 69 6e 65 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 fine TK_TO_BLOB
5b350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b360 20 20 20 20 20 20 20 31 34 32 0a 23 64 65 66 69 142.#defi
5b370 6e 65 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 ne TK_TO_NUMERIC
5b380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b390 20 20 20 20 20 31 34 33 0a 23 64 65 66 69 6e 65 143.#define
5b3a0 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 20 20 20 TK_TO_INT
5b3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b3c0 20 20 20 31 34 34 0a 23 64 65 66 69 6e 65 20 54 144.#define T
5b3d0 4b 5f 54 4f 5f 52 45 41 4c 20 20 20 20 20 20 20 K_TO_REAL
5b3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b3f0 20 31 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 145.#define TK_
5b400 49 53 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 ISNOT
5b410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
5b420 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 46.#define TK_EN
5b430 44 5f 4f 46 5f 46 49 4c 45 20 20 20 20 20 20 20 D_OF_FILE
5b440 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 37 147
5b450 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4c 4c 45 .#define TK_ILLE
5b460 47 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 GAL
5b470 20 20 20 20 20 20 20 20 20 20 20 31 34 38 0a 23 148.#
5b480 64 65 66 69 6e 65 20 54 4b 5f 53 50 41 43 45 20 define TK_SPACE
5b490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b4a0 20 20 20 20 20 20 20 20 20 31 34 39 0a 23 64 65 149.#de
5b4b0 66 69 6e 65 20 54 4b 5f 55 4e 43 4c 4f 53 45 44 fine TK_UNCLOSED
5b4c0 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20 _STRING
5b4d0 20 20 20 20 20 20 20 31 35 30 0a 23 64 65 66 69 150.#defi
5b4e0 6e 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 20 ne TK_FUNCTION
5b4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b500 20 20 20 20 20 31 35 31 0a 23 64 65 66 69 6e 65 151.#define
5b510 20 54 4b 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 TK_COLUMN
5b520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b530 20 20 20 31 35 32 0a 23 64 65 66 69 6e 65 20 54 152.#define T
5b540 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 20 K_AGG_FUNCTION
5b550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b560 20 31 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 153.#define TK_
5b570 41 47 47 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 AGG_COLUMN
5b580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
5b590 35 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 54.#define TK_CO
5b5a0 4e 53 54 5f 46 55 4e 43 20 20 20 20 20 20 20 20 NST_FUNC
5b5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 35 155
5b5c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4d 49 4e .#define TK_UMIN
5b5d0 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 US
5b5e0 20 20 20 20 20 20 20 20 20 20 20 31 35 36 0a 23 156.#
5b5f0 64 65 66 69 6e 65 20 54 4b 5f 55 50 4c 55 53 20 define TK_UPLUS
5b600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b610 20 20 20 20 20 20 20 20 20 31 35 37 0a 0a 2f 2a 157../*
5b620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
5b630 64 20 6f 66 20 70 61 72 73 65 2e 68 20 2a 2a 2a d of parse.h ***
5b640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5b650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5b660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
5b670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f ************* Co
5b680 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 ntinuing where w
5b690 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 e left off in sq
5b6a0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
5b6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 ************/.#i
5b6c0 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e nclude <stdio.h>
5b6d0 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 .#include <stdli
5b6e0 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 b.h>.#include <s
5b6f0 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 tring.h>.#includ
5b700 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e e <assert.h>.#in
5b710 63 6c 75 64 65 20 3c 73 74 64 64 65 66 2e 68 3e clude <stddef.h>
5b720 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 ../*.** If compi
5b730 6c 69 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65 ling for a proce
5b740 73 73 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20 ssor that lacks
5b750 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 floating point s
5b760 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74 upport,.** subst
5b770 69 74 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f itute integer fo
5b780 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 r floating-point
5b790 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
5b7a0 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
5b7b0 50 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64 POINT.# define d
5b7c0 6f 75 62 6c 65 20 73 71 6c 69 74 65 5f 69 6e 74 ouble sqlite_int
5b7d0 36 34 0a 23 20 64 65 66 69 6e 65 20 66 6c 6f 61 64.# define floa
5b7e0 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 t sqlite_int64.#
5b7f0 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 define LONGDOUB
5b800 4c 45 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 69 LE_TYPE sqlite_i
5b810 6e 74 36 34 0a 23 20 69 66 6e 64 65 66 20 53 51 nt64.# ifndef SQ
5b820 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 20 LITE_BIG_DBL.#
5b830 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 define SQLITE_B
5b840 49 47 5f 44 42 4c 20 28 28 28 73 71 6c 69 74 65 IG_DBL (((sqlite
5b850 33 5f 69 6e 74 36 34 29 31 29 3c 3c 35 30 29 0a 3_int64)1)<<50).
5b860 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 # endif.# define
5b870 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 SQLITE_OMIT_DAT
5b880 45 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20 ETIME_FUNCS 1.#
5b890 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d define SQLITE_OM
5b8a0 49 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64 IT_TRACE 1.# und
5b8b0 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f ef SQLITE_MIXED_
5b8c0 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f ENDIAN_64BIT_FLO
5b8d0 41 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 AT.# undef SQLIT
5b8e0 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 65 6e E_HAVE_ISNAN.#en
5b8f0 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 dif.#ifndef SQLI
5b900 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66 TE_BIG_DBL.# def
5b910 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 ine SQLITE_BIG_D
5b920 42 4c 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66 BL (1e99).#endif
5b930 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d ../*.** OMIT_TEM
5b940 50 44 42 20 69 73 20 73 65 74 20 74 6f 20 31 20 PDB is set to 1
5b950 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 if SQLITE_OMIT_T
5b960 45 4d 50 44 42 20 69 73 20 64 65 66 69 6e 65 64 EMPDB is defined
5b970 2c 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77 , or 0.** afterw
5b980 61 72 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73 ard. Having this
5b990 20 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73 macro allows us
5b9a0 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 43 20 to cause the C
5b9b0 63 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20 compiler .** to
5b9c0 6f 6d 69 74 20 63 6f 64 65 20 75 73 65 64 20 62 omit code used b
5b9d0 79 20 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69 y TEMP tables wi
5b9e0 74 68 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e thout messy #ifn
5b9f0 64 65 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a def statements..
5ba00 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
5ba10 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65 _OMIT_TEMPDB.#de
5ba20 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 fine OMIT_TEMPDB
5ba30 20 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 1.#else.#define
5ba40 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23 OMIT_TEMPDB 0.#
5ba50 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
5ba60 20 22 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e "file format" n
5ba70 75 6d 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65 umber is an inte
5ba80 67 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 ger that is incr
5ba90 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 emented whenever
5baa0 0a 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76 .** the VDBE-lev
5bab0 65 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 el file format c
5bac0 68 61 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c hanges. The fol
5bad0 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 lowing macros de
5bae0 66 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 fine the.** the
5baf0 64 65 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 default file for
5bb00 6d 61 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61 mat for new data
5bb10 62 61 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61 bases and the ma
5bb20 78 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 ximum file forma
5bb30 74 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69 t.** that the li
5bb40 62 72 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a brary can read..
5bb50 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5bb60 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 E_MAX_FILE_FORMA
5bb70 54 20 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 T 4.#ifndef SQLI
5bb80 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f TE_DEFAULT_FILE_
5bb90 46 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20 FORMAT.# define
5bba0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 SQLITE_DEFAULT_F
5bbb0 49 4c 45 5f 46 4f 52 4d 41 54 20 34 0a 23 65 6e ILE_FORMAT 4.#en
5bbc0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 dif../*.** Deter
5bbd0 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 72 69 mine whether tri
5bbe0 67 67 65 72 73 20 61 72 65 20 72 65 63 75 72 73 ggers are recurs
5bbf0 69 76 65 20 62 79 20 64 65 66 61 75 6c 74 2e 20 ive by default.
5bc00 20 54 68 69 73 20 63 61 6e 20 62 65 0a 2a 2a 20 This can be.**
5bc10 63 68 61 6e 67 65 64 20 61 74 20 72 75 6e 2d 74 changed at run-t
5bc20 69 6d 65 20 75 73 69 6e 67 20 61 20 70 72 61 67 ime using a prag
5bc30 6d 61 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ma..*/.#ifndef S
5bc40 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 QLITE_DEFAULT_RE
5bc50 43 55 52 53 49 56 45 5f 54 52 49 47 47 45 52 53 CURSIVE_TRIGGERS
5bc60 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
5bc70 5f 44 45 46 41 55 4c 54 5f 52 45 43 55 52 53 49 _DEFAULT_RECURSI
5bc80 56 45 5f 54 52 49 47 47 45 52 53 20 30 0a 23 65 VE_TRIGGERS 0.#e
5bc90 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 ndif../*.** Prov
5bca0 69 64 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 ide a default va
5bcb0 6c 75 65 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 lue for SQLITE_T
5bcc0 45 4d 50 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 EMP_STORE in cas
5bcd0 65 20 69 74 20 69 73 20 6e 6f 74 20 73 70 65 63 e it is not spec
5bce0 69 66 69 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 ified.** on the
5bcf0 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a command-line.*/.
5bd00 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 #ifndef SQLITE_T
5bd10 45 4d 50 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 EMP_STORE.# defi
5bd20 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 ne SQLITE_TEMP_S
5bd30 54 4f 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f TORE 1.#endif../
5bd40 2a 0a 2a 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f *.** GCC does no
5bd50 74 20 64 65 66 69 6e 65 20 74 68 65 20 6f 66 66 t define the off
5bd60 73 65 74 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f setof() macro so
5bd70 20 77 65 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 we'll have to d
5bd80 6f 20 69 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 o it.** ourselve
5bd90 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 s..*/.#ifndef of
5bda0 66 73 65 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f fsetof.#define o
5bdb0 66 66 73 65 74 6f 66 28 53 54 52 55 43 54 55 52 ffsetof(STRUCTUR
5bdc0 45 2c 46 49 45 4c 44 29 20 28 28 69 6e 74 29 28 E,FIELD) ((int)(
5bdd0 28 63 68 61 72 2a 29 26 28 28 53 54 52 55 43 54 (char*)&((STRUCT
5bde0 55 52 45 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 URE*)0)->FIELD))
5bdf0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
5be00 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 heck to see if t
5be10 68 69 73 20 6d 61 63 68 69 6e 65 20 75 73 65 73 his machine uses
5be20 20 45 42 43 44 49 43 2e 20 20 28 59 65 73 2c 20 EBCDIC. (Yes,
5be30 62 65 6c 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a believe it or.**
5be40 20 6e 6f 74 2c 20 74 68 65 72 65 20 61 72 65 20 not, there are
5be50 73 74 69 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f still machines o
5be60 75 74 20 74 68 65 72 65 20 74 68 61 74 20 75 73 ut there that us
5be70 65 20 45 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 e EBCDIC.).*/.#i
5be80 66 20 27 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a f 'A' == '\301'.
5be90 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
5bea0 45 42 43 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 EBCDIC 1.#else.#
5beb0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 define SQLITE_A
5bec0 53 43 49 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f SCII 1.#endif../
5bed0 2a 0a 2a 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 *.** Integers of
5bee0 20 6b 6e 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 known sizes. T
5bef0 68 65 73 65 20 74 79 70 65 64 65 66 73 20 6d 69 hese typedefs mi
5bf00 67 68 74 20 63 68 61 6e 67 65 20 66 6f 72 20 61 ght change for a
5bf10 72 63 68 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 rchitectures.**
5bf20 77 68 65 72 65 20 74 68 65 20 73 69 7a 65 73 20 where the sizes
5bf30 76 65 72 79 2e 20 20 50 72 65 70 72 6f 63 65 73 very. Preproces
5bf40 73 6f 72 20 6d 61 63 72 6f 73 20 61 72 65 20 61 sor macros are a
5bf50 76 61 69 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 vailable so that
5bf60 20 74 68 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 the.** types ca
5bf70 6e 20 62 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c n be convenientl
5bf80 79 20 72 65 64 65 66 69 6e 65 64 20 61 74 20 63 y redefined at c
5bf90 6f 6d 70 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 ompile-type. Li
5bfa0 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 ke this:.**.**
5bfb0 20 20 20 20 20 20 20 63 63 20 27 2d 44 55 49 4e cc '-DUIN
5bfc0 54 50 54 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c TPTR_TYPE=long l
5bfd0 6f 6e 67 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a ong int' ....*/.
5bfe0 23 69 66 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 #ifndef UINT32_T
5bff0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 YPE.# ifdef HAVE
5c000 5f 55 49 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 _UINT32_T.# def
5c010 69 6e 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20 ine UINT32_TYPE
5c020 75 69 6e 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a uint32_t.# else.
5c030 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 33 32 # define UINT32
5c040 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 69 _TYPE unsigned i
5c050 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 nt.# endif.#endi
5c060 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 31 36 f.#ifndef UINT16
5c070 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 _TYPE.# ifdef HA
5c080 56 45 5f 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 VE_UINT16_T.# d
5c090 65 66 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 efine UINT16_TYP
5c0a0 45 20 75 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 E uint16_t.# els
5c0b0 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 e.# define UINT
5c0c0 31 36 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 16_TYPE unsigned
5c0d0 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 short int.# end
5c0e0 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 if.#endif.#ifnde
5c0f0 66 20 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 f INT16_TYPE.# i
5c100 66 64 65 66 20 48 41 56 45 5f 49 4e 54 31 36 5f fdef HAVE_INT16_
5c110 54 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 31 T.# define INT1
5c120 36 5f 54 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 6_TYPE int16_t.#
5c130 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 else.# define
5c140 49 4e 54 31 36 5f 54 59 50 45 20 73 68 6f 72 74 INT16_TYPE short
5c150 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e int.# endif.#en
5c160 64 69 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 dif.#ifndef UINT
5c170 38 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 8_TYPE.# ifdef H
5c180 41 56 45 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 AVE_UINT8_T.# d
5c190 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 efine UINT8_TYPE
5c1a0 20 75 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a uint8_t.# else.
5c1b0 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 38 5f # define UINT8_
5c1c0 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 63 68 TYPE unsigned ch
5c1d0 61 72 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 ar.# endif.#endi
5c1e0 66 0a 23 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 f.#ifndef INT8_T
5c1f0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 YPE.# ifdef HAVE
5c200 5f 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 69 6e _INT8_T.# defin
5c210 65 20 49 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 e INT8_TYPE int8
5c220 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 _t.# else.# def
5c230 69 6e 65 20 49 4e 54 38 5f 54 59 50 45 20 73 69 ine INT8_TYPE si
5c240 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69 gned char.# endi
5c250 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 f.#endif.#ifndef
5c260 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 LONGDOUBLE_TYPE
5c270 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f .# define LONGDO
5c280 55 42 4c 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 UBLE_TYPE long d
5c290 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 ouble.#endif.typ
5c2a0 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 edef sqlite_int6
5c2b0 34 20 69 36 34 3b 20 20 20 20 20 20 20 20 20 20 4 i64;
5c2c0 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 8-byte signed
5c2d0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 integer */.type
5c2e0 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 def sqlite_uint6
5c2f0 34 20 75 36 34 3b 20 20 20 20 20 20 20 20 20 2f 4 u64; /
5c300 2a 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 * 8-byte unsigne
5c310 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 d integer */.typ
5c320 65 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45 edef UINT32_TYPE
5c330 20 75 33 32 3b 20 20 20 20 20 20 20 20 20 20 20 u32;
5c340 2f 2a 20 34 2d 62 79 74 65 20 75 6e 73 69 67 6e /* 4-byte unsign
5c350 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 ed integer */.ty
5c360 70 65 64 65 66 20 55 49 4e 54 31 36 5f 54 59 50 pedef UINT16_TYP
5c370 45 20 75 31 36 3b 20 20 20 20 20 20 20 20 20 20 E u16;
5c380 20 2f 2a 20 32 2d 62 79 74 65 20 75 6e 73 69 67 /* 2-byte unsig
5c390 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 ned integer */.t
5c3a0 79 70 65 64 65 66 20 49 4e 54 31 36 5f 54 59 50 ypedef INT16_TYP
5c3b0 45 20 69 31 36 3b 20 20 20 20 20 20 20 20 20 20 E i16;
5c3c0 20 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e /* 2-byte sign
5c3d0 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 ed integer */.ty
5c3e0 70 65 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 pedef UINT8_TYPE
5c3f0 20 75 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 u8;
5c400 20 2f 2a 20 31 2d 62 79 74 65 20 75 6e 73 69 67 /* 1-byte unsig
5c410 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 ned integer */.t
5c420 79 70 65 64 65 66 20 49 4e 54 38 5f 54 59 50 45 ypedef INT8_TYPE
5c430 20 69 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 i8;
5c440 20 20 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e /* 1-byte sign
5c450 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f ed integer */../
5c460 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f *.** SQLITE_MAX_
5c470 55 33 32 20 69 73 20 61 20 75 36 34 20 63 6f 6e U32 is a u64 con
5c480 73 74 61 6e 74 20 74 68 61 74 20 69 73 20 74 68 stant that is th
5c490 65 20 6d 61 78 69 6d 75 6d 20 75 36 34 20 76 61 e maximum u64 va
5c4a0 6c 75 65 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 lue.** that can
5c4b0 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 75 be stored in a u
5c4c0 33 32 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 32 without loss
5c4d0 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20 76 61 of data. The va
5c4e0 6c 75 65 0a 2a 2a 20 69 73 20 30 78 30 30 30 30 lue.** is 0x0000
5c4f0 30 30 30 30 66 66 66 66 66 66 66 66 2e 20 20 42 0000ffffffff. B
5c500 75 74 20 62 65 63 61 75 73 65 20 6f 66 20 71 75 ut because of qu
5c510 69 72 6b 73 20 6f 66 20 73 6f 6d 65 20 63 6f 6d irks of some com
5c520 70 69 6c 65 72 73 2c 20 77 65 0a 2a 2a 20 68 61 pilers, we.** ha
5c530 76 65 20 74 6f 20 73 70 65 63 69 66 79 20 74 68 ve to specify th
5c540 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 6c e value in the l
5c550 65 73 73 20 69 6e 74 75 69 74 69 76 65 20 6d 61 ess intuitive ma
5c560 6e 6e 65 72 20 73 68 6f 77 6e 3a 0a 2a 2f 0a 23 nner shown:.*/.#
5c570 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
5c580 58 5f 55 33 32 20 20 28 28 28 28 75 36 34 29 31 X_U32 ((((u64)1
5c590 29 3c 3c 33 32 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a )<<32)-1)../*.**
5c5a0 20 54 68 65 20 64 61 74 61 74 79 70 65 20 75 73 The datatype us
5c5b0 65 64 20 74 6f 20 73 74 6f 72 65 20 65 73 74 69 ed to store esti
5c5c0 6d 61 74 65 73 20 6f 66 20 74 68 65 20 6e 75 6d mates of the num
5c5d0 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 61 ber of rows in a
5c5e0 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 .** table or ind
5c5f0 65 78 2e 20 20 54 68 69 73 20 69 73 20 61 6e 20 ex. This is an
5c600 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
5c610 20 74 79 70 65 2e 20 20 46 6f 72 20 39 39 2e 39 type. For 99.9
5c620 25 20 6f 66 0a 2a 2a 20 74 68 65 20 77 6f 72 6c % of.** the worl
5c630 64 2c 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 d, a 32-bit inte
5c640 67 65 72 20 69 73 20 73 75 66 66 69 63 69 65 6e ger is sufficien
5c650 74 2e 20 20 42 75 74 20 61 20 36 34 2d 62 69 74 t. But a 64-bit
5c660 20 69 6e 74 65 67 65 72 0a 2a 2a 20 63 61 6e 20 integer.** can
5c670 62 65 20 75 73 65 64 20 61 74 20 63 6f 6d 70 69 be used at compi
5c680 6c 65 2d 74 69 6d 65 20 69 66 20 64 65 73 69 72 le-time if desir
5c690 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ed..*/.#ifdef SQ
5c6a0 4c 49 54 45 5f 36 34 42 49 54 5f 53 54 41 54 53 LITE_64BIT_STATS
5c6b0 0a 20 74 79 70 65 64 65 66 20 75 36 34 20 74 52 . typedef u64 tR
5c6c0 6f 77 63 6e 74 3b 20 20 20 20 2f 2a 20 36 34 2d owcnt; /* 64-
5c6d0 62 69 74 20 6f 6e 6c 79 20 69 66 20 72 65 71 75 bit only if requ
5c6e0 65 73 74 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 ested at compile
5c6f0 2d 74 69 6d 65 20 2a 2f 0a 23 65 6c 73 65 0a 20 -time */.#else.
5c700 74 79 70 65 64 65 66 20 75 33 32 20 74 52 6f 77 typedef u32 tRow
5c710 63 6e 74 3b 20 20 20 20 2f 2a 20 33 32 2d 62 69 cnt; /* 32-bi
5c720 74 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 t is the default
5c730 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a */.#endif../*.*
5c740 2a 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65 * Macros to dete
5c750 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 68 rmine whether th
5c760 65 20 6d 61 63 68 69 6e 65 20 69 73 20 62 69 67 e machine is big
5c770 20 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 or little endia
5c780 6e 2c 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 n,.** evaluated
5c790 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 at runtime..*/.#
5c7a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 ifdef SQLITE_AMA
5c7b0 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 LGAMATION.SQLITE
5c7c0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 _PRIVATE const i
5c7d0 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 nt sqlite3one =
5c7e0 31 3b 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 1;.#else.SQLITE_
5c7f0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e PRIVATE const in
5c800 74 20 73 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 t sqlite3one;.#e
5c810 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 ndif.#if defined
5c820 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 (i386) || define
5c830 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 d(__i386__) || d
5c840 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 5c efined(_M_IX86)\
5c850 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
5c860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ||
5c870 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 defined(__x86_6
5c880 34 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4) || defined(__
5c890 78 38 36 5f 36 34 5f 5f 29 0a 23 20 64 65 66 69 x86_64__).# defi
5c8a0 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 ne SQLITE_BIGEND
5c8b0 49 41 4e 20 20 20 20 30 0a 23 20 64 65 66 69 6e IAN 0.# defin
5c8c0 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 e SQLITE_LITTLEE
5c8d0 4e 44 49 41 4e 20 31 0a 23 20 64 65 66 69 6e 65 NDIAN 1.# define
5c8e0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 SQLITE_UTF16NAT
5c8f0 49 56 45 20 20 53 51 4c 49 54 45 5f 55 54 46 31 IVE SQLITE_UTF1
5c900 36 4c 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6LE.#else.# defi
5c910 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 ne SQLITE_BIGEND
5c920 49 41 4e 20 20 20 20 28 2a 28 63 68 61 72 20 2a IAN (*(char *
5c930 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d )(&sqlite3one)==
5c940 30 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 0).# define SQLI
5c950 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 TE_LITTLEENDIAN
5c960 28 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69 (*(char *)(&sqli
5c970 74 65 33 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65 te3one)==1).# de
5c980 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 fine SQLITE_UTF1
5c990 36 4e 41 54 49 56 45 20 28 53 51 4c 49 54 45 5f 6NATIVE (SQLITE_
5c9a0 42 49 47 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45 BIGENDIAN?SQLITE
5c9b0 5f 55 54 46 31 36 42 45 3a 53 51 4c 49 54 45 5f _UTF16BE:SQLITE_
5c9c0 55 54 46 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a UTF16LE).#endif.
5c9d0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 ./*.** Constants
5c9e0 20 66 6f 72 20 74 68 65 20 6c 61 72 67 65 73 74 for the largest
5c9f0 20 61 6e 64 20 73 6d 61 6c 6c 65 73 74 20 70 6f and smallest po
5ca00 73 73 69 62 6c 65 20 36 34 2d 62 69 74 20 73 69 ssible 64-bit si
5ca10 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a gned integers..*
5ca20 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 61 * These macros a
5ca30 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20 77 re designed to w
5ca40 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e ork correctly on
5ca50 20 62 6f 74 68 20 33 32 2d 62 69 74 20 61 6e 64 both 32-bit and
5ca60 20 36 34 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 64-bit.** compi
5ca70 6c 65 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 lers..*/.#define
5ca80 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 20 20 LARGEST_INT64
5ca90 28 30 78 66 66 66 66 66 66 66 66 7c 28 28 28 69 (0xffffffff|(((i
5caa0 36 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c 64)0x7fffffff)<<
5cab0 33 32 29 29 0a 23 64 65 66 69 6e 65 20 53 4d 41 32)).#define SMA
5cac0 4c 4c 45 53 54 5f 49 4e 54 36 34 20 28 28 28 69 LLEST_INT64 (((i
5cad0 36 34 29 2d 31 29 20 2d 20 4c 41 52 47 45 53 54 64)-1) - LARGEST
5cae0 5f 49 4e 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 _INT64)../* .**
5caf0 52 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62 65 Round up a numbe
5cb00 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c 61 r to the next la
5cb10 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 rger multiple of
5cb20 20 38 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 8. This is use
5cb30 64 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d d.** to force 8-
5cb40 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f byte alignment o
5cb50 6e 20 36 34 2d 62 69 74 20 61 72 63 68 69 74 65 n 64-bit archite
5cb60 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 ctures..*/.#defi
5cb70 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20 20 ne ROUND8(x)
5cb80 20 28 28 28 78 29 2b 37 29 26 7e 37 29 0a 0a 2f (((x)+7)&~7)../
5cb90 2a 0a 2a 2a 20 52 6f 75 6e 64 20 64 6f 77 6e 20 *.** Round down
5cba0 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 6d to the nearest m
5cbb0 75 6c 74 69 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a ultiple of 8.*/.
5cbc0 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 44 4f 57 #define ROUNDDOW
5cbd0 4e 38 28 78 29 20 28 28 78 29 26 7e 37 29 0a 0a N8(x) ((x)&~7)..
5cbe0 2f 2a 0a 2a 2a 20 41 73 73 65 72 74 20 74 68 61 /*.** Assert tha
5cbf0 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 58 20 t the pointer X
5cc00 69 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e is aligned to an
5cc10 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 8-byte boundary
5cc20 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61 63 72 6f . This.** macro
5cc30 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 77 69 is used only wi
5cc40 74 68 69 6e 20 61 73 73 65 72 74 28 29 20 74 6f thin assert() to
5cc50 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65 verify that the
5cc60 20 63 6f 64 65 20 67 65 74 73 0a 2a 2a 20 61 6c code gets.** al
5cc70 6c 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 73 74 l alignment rest
5cc80 72 69 63 74 69 6f 6e 73 20 63 6f 72 72 65 63 74 rictions correct
5cc90 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c 20 ..**.** Except,
5cca0 69 66 20 53 51 4c 49 54 45 5f 34 5f 42 59 54 45 if SQLITE_4_BYTE
5ccb0 5f 41 4c 49 47 4e 45 44 5f 4d 41 4c 4c 4f 43 20 _ALIGNED_MALLOC
5ccc0 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e is defined, then
5ccd0 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 the.** underlyi
5cce0 6e 67 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c ng malloc() impl
5ccf0 65 6d 65 6e 74 69 6f 6e 20 6d 69 67 68 74 20 72 emention might r
5cd00 65 74 75 72 6e 20 75 73 20 34 2d 62 79 74 65 20 eturn us 4-byte
5cd10 61 6c 69 67 6e 65 64 0a 2a 2a 20 70 6f 69 6e 74 aligned.** point
5cd20 65 72 73 2e 20 20 49 6e 20 74 68 61 74 20 63 61 ers. In that ca
5cd30 73 65 2c 20 6f 6e 6c 79 20 76 65 72 69 66 79 20 se, only verify
5cd40 34 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 4-byte alignment
5cd50 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
5cd60 54 45 5f 34 5f 42 59 54 45 5f 41 4c 49 47 4e 45 TE_4_BYTE_ALIGNE
5cd70 44 5f 4d 41 4c 4c 4f 43 0a 23 20 64 65 66 69 6e D_MALLOC.# defin
5cd80 65 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 e EIGHT_BYTE_ALI
5cd90 47 4e 4d 45 4e 54 28 58 29 20 20 20 28 28 28 28 GNMENT(X) ((((
5cda0 63 68 61 72 2a 29 28 58 29 20 2d 20 28 63 68 61 char*)(X) - (cha
5cdb0 72 2a 29 30 29 26 33 29 3d 3d 30 29 0a 23 65 6c r*)0)&3)==0).#el
5cdc0 73 65 0a 23 20 64 65 66 69 6e 65 20 45 49 47 48 se.# define EIGH
5cdd0 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 T_BYTE_ALIGNMENT
5cde0 28 58 29 20 20 20 28 28 28 28 63 68 61 72 2a 29 (X) ((((char*)
5cdf0 28 58 29 20 2d 20 28 63 68 61 72 2a 29 30 29 26 (X) - (char*)0)&
5ce00 37 29 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 0a 0a 7)==0).#endif...
5ce10 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
5ce20 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
5ce30 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ng structure is
5ce40 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 used to store th
5ce50 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a e busy-handler.*
5ce60 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 * callback for a
5ce70 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20 68 61 given sqlite ha
5ce80 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 ndle. .**.** The
5ce90 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 sqlite.busyHand
5cea0 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 ler member of th
5ceb0 65 20 73 71 6c 69 74 65 20 73 74 72 75 63 74 20 e sqlite struct
5cec0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62 75 73 contains the bus
5ced0 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f y.** callback fo
5cee0 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 r the database h
5cef0 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61 67 65 andle. Each page
5cf00 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 65 r opened via the
5cf10 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c sqlite.** handl
5cf20 65 20 69 73 20 70 61 73 73 65 64 20 61 20 70 6f e is passed a po
5cf30 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65 2e inter to sqlite.
5cf40 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54 68 65 busyHandler. The
5cf50 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a busy-handler.**
5cf60 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75 72 callback is cur
5cf70 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20 6f rently invoked o
5cf80 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 nly from within
5cf90 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65 pager.c..*/.type
5cfa0 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79 48 def struct BusyH
5cfb0 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64 6c andler BusyHandl
5cfc0 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73 79 48 er;.struct BusyH
5cfd0 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20 28 andler {. int (
5cfe0 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c 69 *xFunc)(void *,i
5cff0 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62 75 73 nt); /* The bus
5d000 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 y callback */.
5d010 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20 20 void *pArg;
5d020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
5d030 72 73 74 20 61 72 67 20 74 6f 20 62 75 73 79 20 rst arg to busy
5d040 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e callback */. in
5d050 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20 20 t nBusy;
5d060 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 /* Incr
5d070 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 emented with eac
5d080 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d h busy call */.}
5d090 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 ;../*.** Name of
5d0a0 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 61 the master data
5d0b0 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68 65 base table. The
5d0c0 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65 master database
5d0d0 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 73 table.** is a s
5d0e0 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68 61 pecial table tha
5d0f0 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 t holds the name
5d100 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65 73 s and attributes
5d110 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72 20 of all.** user
5d120 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 tables and indic
5d130 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d es..*/.#define M
5d140 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20 20 ASTER_NAME
5d150 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 "sqlite_master"
5d160 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d 41 .#define TEMP_MA
5d170 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c 69 STER_NAME "sqli
5d180 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 0a te_temp_master".
5d190 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 2d ./*.** The root-
5d1a0 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73 74 page of the mast
5d1b0 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c er database tabl
5d1c0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 e..*/.#define MA
5d1d0 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20 20 STER_ROOT
5d1e0 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 1../*.** The nam
5d1f0 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 e of the schema
5d200 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e table..*/.#defin
5d210 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 78 e SCHEMA_TABLE(x
5d220 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50 44 ) ((!OMIT_TEMPD
5d230 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50 5f B)&&(x==1)?TEMP_
5d240 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53 54 MASTER_NAME:MAST
5d250 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20 ER_NAME)../*.**
5d260 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d 61 A convenience ma
5d270 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e 73 cro that returns
5d280 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
5d290 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61 6e lements in.** an
5d2a0 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66 69 array..*/.#defi
5d2b0 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29 20 ne ArraySize(X)
5d2c0 20 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 ((int)(sizeof
5d2d0 28 58 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d 29 (X)/sizeof(X[0])
5d2e0 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f ))../*.** The fo
5d2f0 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 61 73 llowing value as
5d300 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 6d 65 a destructor me
5d310 61 6e 73 20 74 6f 20 75 73 65 20 73 71 6c 69 74 ans to use sqlit
5d320 65 33 44 62 46 72 65 65 28 29 2e 0a 2a 2a 20 54 e3DbFree()..** T
5d330 68 65 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 he sqlite3DbFree
5d340 28 29 20 72 6f 75 74 69 6e 65 20 72 65 71 75 69 () routine requi
5d350 72 65 73 20 74 77 6f 20 70 61 72 61 6d 65 74 65 res two paramete
5d360 72 73 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 rs instead of th
5d370 65 20 0a 2a 2a 20 6f 6e 65 20 70 61 72 61 6d 65 e .** one parame
5d380 74 65 72 20 74 68 61 74 20 64 65 73 74 72 75 63 ter that destruc
5d390 74 6f 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 77 61 tors normally wa
5d3a0 6e 74 2e 20 20 53 6f 20 77 65 20 68 61 76 65 20 nt. So we have
5d3b0 74 6f 20 69 6e 74 72 6f 64 75 63 65 20 0a 2a 2a to introduce .**
5d3c0 20 74 68 69 73 20 6d 61 67 69 63 20 76 61 6c 75 this magic valu
5d3d0 65 20 74 68 61 74 20 74 68 65 20 63 6f 64 65 20 e that the code
5d3e0 6b 6e 6f 77 73 20 74 6f 20 68 61 6e 64 6c 65 20 knows to handle
5d3f0 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 41 6e differently. An
5d400 79 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 77 69 y .** pointer wi
5d410 6c 6c 20 77 6f 72 6b 20 68 65 72 65 20 61 73 20 ll work here as
5d420 6c 6f 6e 67 20 61 73 20 69 74 20 69 73 20 64 69 long as it is di
5d430 73 74 69 6e 63 74 20 66 72 6f 6d 20 53 51 4c 49 stinct from SQLI
5d440 54 45 5f 53 54 41 54 49 43 0a 2a 2a 20 61 6e 64 TE_STATIC.** and
5d450 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
5d460 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 T..*/.#define SQ
5d470 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 20 20 28 LITE_DYNAMIC (
5d480 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63 (sqlite3_destruc
5d490 74 6f 72 5f 74 79 70 65 29 73 71 6c 69 74 65 33 tor_type)sqlite3
5d4a0 4d 61 6c 6c 6f 63 53 69 7a 65 29 0a 0a 2f 2a 0a MallocSize)../*.
5d4b0 2a 2a 20 57 68 65 6e 20 53 51 4c 49 54 45 5f 4f ** When SQLITE_O
5d4c0 4d 49 54 5f 57 53 44 20 69 73 20 64 65 66 69 6e MIT_WSD is defin
5d4d0 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 ed, it means tha
5d4e0 74 20 74 68 65 20 74 61 72 67 65 74 20 70 6c 61 t the target pla
5d4f0 74 66 6f 72 6d 20 64 6f 65 73 0a 2a 2a 20 6e 6f tform does.** no
5d500 74 20 73 75 70 70 6f 72 74 20 57 72 69 74 61 62 t support Writab
5d510 6c 65 20 53 74 61 74 69 63 20 44 61 74 61 20 28 le Static Data (
5d520 57 53 44 29 20 73 75 63 68 20 61 73 20 67 6c 6f WSD) such as glo
5d530 62 61 6c 20 61 6e 64 20 73 74 61 74 69 63 20 76 bal and static v
5d540 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 20 41 6c 6c ariables..** All
5d550 20 76 61 72 69 61 62 6c 65 73 20 6d 75 73 74 20 variables must
5d560 65 69 74 68 65 72 20 62 65 20 6f 6e 20 74 68 65 either be on the
5d570 20 73 74 61 63 6b 20 6f 72 20 64 79 6e 61 6d 69 stack or dynami
5d580 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 cally allocated
5d590 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 68 65 61 70 from.** the heap
5d5a0 2e 20 20 57 68 65 6e 20 57 53 44 20 69 73 20 75 . When WSD is u
5d5b0 6e 73 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20 nsupported, the
5d5c0 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 variable declara
5d5d0 74 69 6f 6e 73 20 73 63 61 74 74 65 72 65 64 0a tions scattered.
5d5e0 2a 2a 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 ** throughout th
5d5f0 65 20 53 51 4c 69 74 65 20 63 6f 64 65 20 6d 75 e SQLite code mu
5d600 73 74 20 62 65 63 6f 6d 65 20 63 6f 6e 73 74 61 st become consta
5d610 6e 74 73 20 69 6e 73 74 65 61 64 2e 20 20 54 68 nts instead. Th
5d620 65 20 53 51 4c 49 54 45 5f 57 53 44 0a 2a 2a 20 e SQLITE_WSD.**
5d630 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 66 6f macro is used fo
5d640 72 20 74 68 69 73 20 70 75 72 70 6f 73 65 2e 20 r this purpose.
5d650 20 41 6e 64 20 69 6e 73 74 65 61 64 20 6f 66 20 And instead of
5d660 72 65 66 65 72 65 6e 63 69 6e 67 20 74 68 65 20 referencing the
5d670 76 61 72 69 61 62 6c 65 0a 2a 2a 20 64 69 72 65 variable.** dire
5d680 63 74 6c 79 2c 20 77 65 20 75 73 65 20 69 74 73 ctly, we use its
5d690 20 63 6f 6e 73 74 61 6e 74 20 61 73 20 61 20 6b constant as a k
5d6a0 65 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 ey to lookup the
5d6b0 20 72 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 run-time alloca
5d6c0 74 65 64 0a 2a 2a 20 62 75 66 66 65 72 20 74 68 ted.** buffer th
5d6d0 61 74 20 68 6f 6c 64 73 20 72 65 61 6c 20 76 61 at holds real va
5d6e0 72 69 61 62 6c 65 2e 20 20 54 68 65 20 63 6f 6e riable. The con
5d6f0 73 74 61 6e 74 20 69 73 20 61 6c 73 6f 20 74 68 stant is also th
5d700 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a e initializer.**
5d710 20 66 6f 72 20 74 68 65 20 72 75 6e 2d 74 69 6d for the run-tim
5d720 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 e allocated buff
5d730 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 er..**.** In the
5d740 20 75 73 75 61 6c 20 63 61 73 65 20 77 68 65 72 usual case wher
5d750 65 20 57 53 44 20 69 73 20 73 75 70 70 6f 72 74 e WSD is support
5d760 65 64 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 57 ed, the SQLITE_W
5d770 53 44 20 61 6e 64 20 47 4c 4f 42 41 4c 0a 2a 2a SD and GLOBAL.**
5d780 20 6d 61 63 72 6f 73 20 62 65 63 6f 6d 65 20 6e macros become n
5d790 6f 2d 6f 70 73 20 61 6e 64 20 68 61 76 65 20 7a o-ops and have z
5d7a0 65 72 6f 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 ero performance
5d7b0 69 6d 70 61 63 74 2e 0a 2a 2f 0a 23 69 66 64 65 impact..*/.#ifde
5d7c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 f SQLITE_OMIT_WS
5d7d0 44 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 D. #define SQLI
5d7e0 54 45 5f 57 53 44 20 63 6f 6e 73 74 0a 20 20 23 TE_WSD const. #
5d7f0 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c define GLOBAL(t,
5d800 76 29 20 28 2a 28 74 2a 29 73 71 6c 69 74 65 33 v) (*(t*)sqlite3
5d810 5f 77 73 64 5f 66 69 6e 64 28 28 76 6f 69 64 2a _wsd_find((void*
5d820 29 26 28 76 29 2c 20 73 69 7a 65 6f 66 28 76 29 )&(v), sizeof(v)
5d830 29 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c )). #define sql
5d840 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
5d850 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 53 GLOBAL(struct S
5d860 71 6c 69 74 65 33 43 6f 6e 66 69 67 2c 20 73 71 qlite3Config, sq
5d870 6c 69 74 65 33 43 6f 6e 66 69 67 29 0a 53 51 4c lite3Config).SQL
5d880 49 54 45 5f 41 50 49 20 20 20 69 6e 74 20 73 71 ITE_API int sq
5d890 6c 69 74 65 33 5f 77 73 64 5f 69 6e 69 74 28 69 lite3_wsd_init(i
5d8a0 6e 74 20 4e 2c 20 69 6e 74 20 4a 29 3b 0a 53 51 nt N, int J);.SQ
5d8b0 4c 49 54 45 5f 41 50 49 20 20 20 76 6f 69 64 20 LITE_API void
5d8c0 2a 73 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e *sqlite3_wsd_fin
5d8d0 64 28 76 6f 69 64 20 2a 4b 2c 20 69 6e 74 20 4c d(void *K, int L
5d8e0 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 );.#else. #defi
5d8f0 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 20 0a 20 ne SQLITE_WSD .
5d900 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28 #define GLOBAL(
5d910 74 2c 76 29 20 76 0a 20 20 23 64 65 66 69 6e 65 t,v) v. #define
5d920 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
5d930 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 nfig sqlite3Conf
5d940 69 67 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a ig.#endif../*.**
5d950 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d The following m
5d960 61 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 74 acros are used t
5d970 6f 20 73 75 70 70 72 65 73 73 20 63 6f 6d 70 69 o suppress compi
5d980 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 61 6e 64 ler warnings and
5d990 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 63 to.** make it c
5d9a0 6c 65 61 72 20 74 6f 20 68 75 6d 61 6e 20 72 65 lear to human re
5d9b0 61 64 65 72 73 20 77 68 65 6e 20 61 20 66 75 6e aders when a fun
5d9c0 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 ction parameter
5d9d0 69 73 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 is deliberately
5d9e0 0a 2a 2a 20 6c 65 66 74 20 75 6e 75 73 65 64 20 .** left unused
5d9f0 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 within the body
5da00 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 54 of a function. T
5da10 68 69 73 20 75 73 75 61 6c 6c 79 20 68 61 70 70 his usually happ
5da20 65 6e 73 20 77 68 65 6e 0a 2a 2a 20 61 20 66 75 ens when.** a fu
5da30 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
5da40 20 76 69 61 20 61 20 66 75 6e 63 74 69 6f 6e 20 via a function
5da50 70 6f 69 6e 74 65 72 2e 20 46 6f 72 20 65 78 61 pointer. For exa
5da60 6d 70 6c 65 20 74 68 65 20 0a 2a 2a 20 69 6d 70 mple the .** imp
5da70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 lementation of a
5da80 6e 20 53 51 4c 20 61 67 67 72 65 67 61 74 65 20 n SQL aggregate
5da90 73 74 65 70 20 63 61 6c 6c 62 61 63 6b 20 6d 61 step callback ma
5daa0 79 20 6e 6f 74 20 75 73 65 20 74 68 65 0a 2a 2a y not use the.**
5dab0 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 parameter indic
5dac0 61 74 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 ating the number
5dad0 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 70 61 of arguments pa
5dae0 73 73 65 64 20 74 6f 20 74 68 65 20 61 67 67 72 ssed to the aggr
5daf0 65 67 61 74 65 2c 0a 2a 2a 20 69 66 20 69 74 20 egate,.** if it
5db00 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 69 73 20 knows that this
5db10 69 73 20 65 6e 66 6f 72 63 65 64 20 65 6c 73 65 is enforced else
5db20 77 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 where..**.** Whe
5db30 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 n a function par
5db40 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 ameter is not us
5db50 65 64 20 61 74 20 61 6c 6c 20 77 69 74 68 69 6e ed at all within
5db60 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 66 the body of a f
5db70 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 69 74 20 69 unction,.** it i
5db80 73 20 67 65 6e 65 72 61 6c 6c 79 20 6e 61 6d 65 s generally name
5db90 64 20 22 4e 6f 74 55 73 65 64 22 20 6f 72 20 22 d "NotUsed" or "
5dba0 4e 6f 74 55 73 65 64 32 22 20 74 6f 20 6d 61 6b NotUsed2" to mak
5dbb0 65 20 74 68 69 6e 67 73 20 65 76 65 6e 20 63 6c e things even cl
5dbc0 65 61 72 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 earer..** Howeve
5dbd0 72 2c 20 74 68 65 73 65 20 6d 61 63 72 6f 73 20 r, these macros
5dbe0 6d 61 79 20 61 6c 73 6f 20 62 65 20 75 73 65 64 may also be used
5dbf0 20 74 6f 20 73 75 70 70 72 65 73 73 20 77 61 72 to suppress war
5dc00 6e 69 6e 67 73 20 72 65 6c 61 74 65 64 20 74 6f nings related to
5dc10 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 74 .** parameters t
5dc20 68 61 74 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e hat may or may n
5dc30 6f 74 20 62 65 20 75 73 65 64 20 64 65 70 65 6e ot be used depen
5dc40 64 69 6e 67 20 6f 6e 20 63 6f 6d 70 69 6c 61 74 ding on compilat
5dc50 69 6f 6e 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 20 ion options..**
5dc60 46 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 6f 73 For example thos
5dc70 65 20 70 61 72 61 6d 65 74 65 72 73 20 6f 6e 6c e parameters onl
5dc80 79 20 75 73 65 64 20 69 6e 20 61 73 73 65 72 74 y used in assert
5dc90 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 () statements. I
5dca0 6e 20 74 68 65 73 65 0a 2a 2a 20 63 61 73 65 73 n these.** cases
5dcb0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 the parameters
5dcc0 61 72 65 20 6e 61 6d 65 64 20 61 73 20 70 65 72 are named as per
5dcd0 20 74 68 65 20 75 73 75 61 6c 20 63 6f 6e 76 65 the usual conve
5dce0 6e 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 ntions..*/.#defi
5dcf0 6e 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 ne UNUSED_PARAME
5dd00 54 45 52 28 78 29 20 28 76 6f 69 64 29 28 78 29 TER(x) (void)(x)
5dd10 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f .#define UNUSED_
5dd20 50 41 52 41 4d 45 54 45 52 32 28 78 2c 79 29 20 PARAMETER2(x,y)
5dd30 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
5dd40 28 78 29 2c 55 4e 55 53 45 44 5f 50 41 52 41 4d (x),UNUSED_PARAM
5dd50 45 54 45 52 28 79 29 0a 0a 2f 2a 0a 2a 2a 20 46 ETER(y)../*.** F
5dd60 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 orward reference
5dd70 73 20 74 6f 20 73 74 72 75 63 74 75 72 65 73 0a s to structures.
5dd80 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
5dd90 74 20 41 67 67 49 6e 66 6f 20 41 67 67 49 6e 66 t AggInfo AggInf
5dda0 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 o;.typedef struc
5ddb0 74 20 41 75 74 68 43 6f 6e 74 65 78 74 20 41 75 t AuthContext Au
5ddc0 74 68 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 thContext;.typed
5ddd0 65 66 20 73 74 72 75 63 74 20 41 75 74 6f 69 6e ef struct Autoin
5dde0 63 49 6e 66 6f 20 41 75 74 6f 69 6e 63 49 6e 66 cInfo AutoincInf
5ddf0 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 o;.typedef struc
5de00 74 20 42 69 74 76 65 63 20 42 69 74 76 65 63 3b t Bitvec Bitvec;
5de10 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
5de20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65 71 3b CollSeq CollSeq;
5de30 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
5de40 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a 74 Column Column;.t
5de50 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44 62 ypedef struct Db
5de60 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73 74 72 Db;.typedef str
5de70 75 63 74 20 53 63 68 65 6d 61 20 53 63 68 65 6d uct Schema Schem
5de80 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 a;.typedef struc
5de90 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74 79 70 t Expr Expr;.typ
5dea0 65 64 65 66 20 73 74 72 75 63 74 20 45 78 70 72 edef struct Expr
5deb0 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b 0a 74 List ExprList;.t
5dec0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 ypedef struct Ex
5ded0 70 72 53 70 61 6e 20 45 78 70 72 53 70 61 6e 3b prSpan ExprSpan;
5dee0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
5def0 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 65 64 FKey FKey;.typed
5df00 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 ef struct FuncDe
5df10 73 74 72 75 63 74 6f 72 20 46 75 6e 63 44 65 73 structor FuncDes
5df20 74 72 75 63 74 6f 72 3b 0a 74 79 70 65 64 65 66 tructor;.typedef
5df30 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 20 struct FuncDef
5df40 46 75 6e 63 44 65 66 3b 0a 74 79 70 65 64 65 66 FuncDef;.typedef
5df50 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 48 struct FuncDefH
5df60 61 73 68 20 46 75 6e 63 44 65 66 48 61 73 68 3b ash FuncDefHash;
5df70 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
5df80 49 64 4c 69 73 74 20 49 64 4c 69 73 74 3b 0a 74 IdList IdList;.t
5df90 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e ypedef struct In
5dfa0 64 65 78 20 49 6e 64 65 78 3b 0a 74 79 70 65 64 dex Index;.typed
5dfb0 65 66 20 73 74 72 75 63 74 20 49 6e 64 65 78 53 ef struct IndexS
5dfc0 61 6d 70 6c 65 20 49 6e 64 65 78 53 61 6d 70 6c ample IndexSampl
5dfd0 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 e;.typedef struc
5dfe0 74 20 4b 65 79 43 6c 61 73 73 20 4b 65 79 43 6c t KeyClass KeyCl
5dff0 61 73 73 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ass;.typedef str
5e000 75 63 74 20 4b 65 79 49 6e 66 6f 20 4b 65 79 49 uct KeyInfo KeyI
5e010 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 nfo;.typedef str
5e020 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 20 4c 6f uct Lookaside Lo
5e030 6f 6b 61 73 69 64 65 3b 0a 74 79 70 65 64 65 66 okaside;.typedef
5e040 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 struct Lookasid
5e050 65 53 6c 6f 74 20 4c 6f 6f 6b 61 73 69 64 65 53 eSlot LookasideS
5e060 6c 6f 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 lot;.typedef str
5e070 75 63 74 20 4d 6f 64 75 6c 65 20 4d 6f 64 75 6c uct Module Modul
5e080 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 e;.typedef struc
5e090 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 4e 61 t NameContext Na
5e0a0 6d 65 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 meContext;.typed
5e0b0 65 66 20 73 74 72 75 63 74 20 50 61 72 73 65 20 ef struct Parse
5e0c0 50 61 72 73 65 3b 0a 74 79 70 65 64 65 66 20 73 Parse;.typedef s
5e0d0 74 72 75 63 74 20 52 6f 77 53 65 74 20 52 6f 77 truct RowSet Row
5e0e0 53 65 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 Set;.typedef str
5e0f0 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 53 61 uct Savepoint Sa
5e100 76 65 70 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66 vepoint;.typedef
5e110 20 73 74 72 75 63 74 20 53 65 6c 65 63 74 20 53 struct Select S
5e120 65 6c 65 63 74 3b 0a 74 79 70 65 64 65 66 20 73 elect;.typedef s
5e130 74 72 75 63 74 20 53 65 6c 65 63 74 44 65 73 74 truct SelectDest
5e140 20 53 65 6c 65 63 74 44 65 73 74 3b 0a 74 79 70 SelectDest;.typ
5e150 65 64 65 66 20 73 74 72 75 63 74 20 53 72 63 4c edef struct SrcL
5e160 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 79 70 ist SrcList;.typ
5e170 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72 41 edef struct StrA
5e180 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b 0a 74 ccum StrAccum;.t
5e190 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61 ypedef struct Ta
5e1a0 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 65 64 ble Table;.typed
5e1b0 65 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 4c ef struct TableL
5e1c0 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b 0a 74 ock TableLock;.t
5e1d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 6f ypedef struct To
5e1e0 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 65 64 ken Token;.typed
5e1f0 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 ef struct Trigge
5e200 72 20 54 72 69 67 67 65 72 3b 0a 74 79 70 65 64 r Trigger;.typed
5e210 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 ef struct Trigge
5e220 72 50 72 67 20 54 72 69 67 67 65 72 50 72 67 3b rPrg TriggerPrg;
5e230 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
5e240 54 72 69 67 67 65 72 53 74 65 70 20 54 72 69 67 TriggerStep Trig
5e250 67 65 72 53 74 65 70 3b 0a 74 79 70 65 64 65 66 gerStep;.typedef
5e260 20 73 74 72 75 63 74 20 55 6e 70 61 63 6b 65 64 struct Unpacked
5e270 52 65 63 6f 72 64 20 55 6e 70 61 63 6b 65 64 52 Record UnpackedR
5e280 65 63 6f 72 64 3b 0a 74 79 70 65 64 65 66 20 73 ecord;.typedef s
5e290 74 72 75 63 74 20 56 54 61 62 6c 65 20 56 54 61 truct VTable VTa
5e2a0 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ble;.typedef str
5e2b0 75 63 74 20 56 74 61 62 43 74 78 20 56 74 61 62 uct VtabCtx Vtab
5e2c0 43 74 78 3b 0a 74 79 70 65 64 65 66 20 73 74 72 Ctx;.typedef str
5e2d0 75 63 74 20 57 61 6c 6b 65 72 20 57 61 6c 6b 65 uct Walker Walke
5e2e0 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 r;.typedef struc
5e2f0 74 20 57 68 65 72 65 50 6c 61 6e 20 57 68 65 72 t WherePlan Wher
5e300 65 50 6c 61 6e 3b 0a 74 79 70 65 64 65 66 20 73 ePlan;.typedef s
5e310 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20 truct WhereInfo
5e320 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64 WhereInfo;.typed
5e330 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4c ef struct WhereL
5e340 65 76 65 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b evel WhereLevel;
5e350 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f ../*.** Defer so
5e360 75 72 63 69 6e 67 20 76 64 62 65 2e 68 20 61 6e urcing vdbe.h an
5e370 64 20 62 74 72 65 65 2e 68 20 75 6e 74 69 6c 20 d btree.h until
5e380 61 66 74 65 72 20 74 68 65 20 22 75 38 22 20 61 after the "u8" a
5e390 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64 nd .** "BusyHand
5e3a0 6c 65 72 22 20 74 79 70 65 64 65 66 73 2e 20 76 ler" typedefs. v
5e3b0 64 62 65 2e 68 20 61 6c 73 6f 20 72 65 71 75 69 dbe.h also requi
5e3c0 72 65 73 20 61 20 66 65 77 20 6f 66 20 74 68 65 res a few of the
5e3d0 20 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74 opaque.** point
5e3e0 65 72 20 74 79 70 65 73 20 28 69 2e 65 2e 20 46 er types (i.e. F
5e3f0 75 6e 63 44 65 66 29 20 64 65 66 69 6e 65 64 20 uncDef) defined
5e400 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a above..*/./*****
5e410 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
5e420 65 20 62 74 72 65 65 2e 68 20 69 6e 20 74 68 65 e btree.h in the
5e430 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 middle of sqlit
5e440 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
5e450 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
5e460 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
5e470 66 69 6c 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a file btree.h ***
5e480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e4a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
5e4b0 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
5e4c0 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
5e4d0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
5e4e0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
5e4f0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
5e500 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
5e510 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
5e520 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
5e530 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
5e540 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
5e550 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
5e560 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
5e570 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
5e580 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
5e590 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
5e5a0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
5e5b0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
5e5c0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
5e5d0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
5e5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
5e620 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 .** This header
5e630 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 file defines the
5e640 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 interface that
5e650 74 68 65 20 73 71 6c 69 74 65 20 42 2d 54 72 65 the sqlite B-Tre
5e660 65 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73 e file.** subsys
5e670 74 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e tem. See commen
5e680 74 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 ts in the source
5e690 20 63 6f 64 65 20 66 6f 72 20 61 20 64 65 74 61 code for a deta
5e6a0 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e iled description
5e6b0 0a 2a 2a 20 6f 66 20 77 68 61 74 20 65 61 63 68 .** of what each
5e6c0 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 interface routi
5e6d0 6e 65 20 64 6f 65 73 2e 0a 2a 2f 0a 23 69 66 6e ne does..*/.#ifn
5e6e0 64 65 66 20 5f 42 54 52 45 45 5f 48 5f 0a 23 64 def _BTREE_H_.#d
5e6f0 65 66 69 6e 65 20 5f 42 54 52 45 45 5f 48 5f 0a efine _BTREE_H_.
5e700 0a 2f 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 64 ./* TODO: This d
5e710 65 66 69 6e 69 74 69 6f 6e 20 69 73 20 6a 75 73 efinition is jus
5e720 74 20 69 6e 63 6c 75 64 65 64 20 73 6f 20 6f 74 t included so ot
5e730 68 65 72 20 6d 6f 64 75 6c 65 73 20 63 6f 6d 70 her modules comp
5e740 69 6c 65 2e 20 49 74 0a 2a 2a 20 6e 65 65 64 73 ile. It.** needs
5e750 20 74 6f 20 62 65 20 72 65 76 69 73 69 74 65 64 to be revisited
5e760 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
5e770 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 ITE_N_BTREE_META
5e780 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65 10../*.** If de
5e790 66 69 6e 65 64 20 61 73 20 6e 6f 6e 2d 7a 65 72 fined as non-zer
5e7a0 6f 2c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 o, auto-vacuum i
5e7b0 73 20 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 s enabled by def
5e7c0 61 75 6c 74 2e 20 4f 74 68 65 72 77 69 73 65 0a ault. Otherwise.
5e7d0 2a 2a 20 69 74 20 6d 75 73 74 20 62 65 20 74 75 ** it must be tu
5e7e0 72 6e 65 64 20 6f 6e 20 66 6f 72 20 65 61 63 68 rned on for each
5e7f0 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 database using
5e800 22 50 52 41 47 4d 41 20 61 75 74 6f 5f 76 61 63 "PRAGMA auto_vac
5e810 75 75 6d 20 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66 uum = 1"..*/.#if
5e820 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 ndef SQLITE_DEFA
5e830 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 ULT_AUTOVACUUM.
5e840 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f #define SQLITE_
5e850 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 DEFAULT_AUTOVACU
5e860 55 4d 20 30 0a 23 65 6e 64 69 66 0a 0a 23 64 65 UM 0.#endif..#de
5e870 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 fine BTREE_AUTOV
5e880 41 43 55 55 4d 5f 4e 4f 4e 45 20 30 20 20 20 20 ACUUM_NONE 0
5e890 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f /* Do not do
5e8a0 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a auto-vacuum */.
5e8b0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 #define BTREE_AU
5e8c0 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 20 31 20 TOVACUUM_FULL 1
5e8d0 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 66 75 6c /* Do ful
5e8e0 6c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f l auto-vacuum */
5e8f0 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 .#define BTREE_A
5e900 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 20 32 UTOVACUUM_INCR 2
5e910 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 /* Incre
5e920 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 2a 2f mental vacuum */
5e930 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 ../*.** Forward
5e940 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 declarations of
5e950 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 structure.*/.typ
5e960 65 64 65 66 20 73 74 72 75 63 74 20 42 74 72 65 edef struct Btre
5e970 65 20 42 74 72 65 65 3b 0a 74 79 70 65 64 65 66 e Btree;.typedef
5e980 20 73 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 struct BtCursor
5e990 20 42 74 43 75 72 73 6f 72 3b 0a 74 79 70 65 64 BtCursor;.typed
5e9a0 65 66 20 73 74 72 75 63 74 20 42 74 53 68 61 72 ef struct BtShar
5e9b0 65 64 20 42 74 53 68 61 72 65 64 3b 0a 0a 0a 53 ed BtShared;...S
5e9c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5e9d0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 t sqlite3BtreeOp
5e9e0 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 en(. sqlite3_vf
5e9f0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 2f s *pVfs, /
5ea00 2a 20 56 46 53 20 74 6f 20 75 73 65 20 77 69 74 * VFS to use wit
5ea10 68 20 74 68 69 73 20 62 2d 74 72 65 65 20 2a 2f h this b-tree */
5ea20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
5ea30 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e Filename, /* N
5ea40 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 ame of database
5ea50 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a file to open */.
5ea60 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
5ea70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 /* As
5ea80 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 sociated databas
5ea90 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
5eaa0 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 Btree **ppBtre
5eab0 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 e, /* Re
5eac0 74 75 72 6e 20 6f 70 65 6e 20 42 74 72 65 65 2a turn open Btree*
5ead0 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 here */. int f
5eae0 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
5eaf0 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a /* Flags */.
5eb00 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 int vfsFlags
5eb10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c /* Fl
5eb20 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 ags passed throu
5eb30 67 68 20 74 6f 20 56 46 53 20 6f 70 65 6e 20 2a gh to VFS open *
5eb40 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 /.);../* The fla
5eb50 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 gs parameter to
5eb60 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e sqlite3BtreeOpen
5eb70 20 63 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 can be the bitw
5eb80 69 73 65 20 6f 72 20 6f 66 20 74 68 65 0a 2a 2a ise or of the.**
5eb90 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 following value
5eba0 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 s..**.** NOTE:
5ebb0 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 These values mus
5ebc0 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 t match the corr
5ebd0 65 73 70 6f 6e 64 69 6e 67 20 50 41 47 45 52 5f esponding PAGER_
5ebe0 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 70 61 values in.** pa
5ebf0 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e ger.h..*/.#defin
5ec00 65 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55 e BTREE_OMIT_JOU
5ec10 52 4e 41 4c 20 20 31 20 20 2f 2a 20 44 6f 20 6e RNAL 1 /* Do n
5ec20 6f 74 20 63 72 65 61 74 65 20 6f 72 20 75 73 65 ot create or use
5ec30 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 a rollback jour
5ec40 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 nal */.#define B
5ec50 54 52 45 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 TREE_MEMORY
5ec60 20 20 20 32 20 20 2f 2a 20 54 68 69 73 20 69 73 2 /* This is
5ec70 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 44 42 an in-memory DB
5ec80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 */.#define BTRE
5ec90 45 5f 53 49 4e 47 4c 45 20 20 20 20 20 20 20 20 E_SINGLE
5eca0 34 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 63 4 /* The file c
5ecb0 6f 6e 74 61 69 6e 73 20 61 74 20 6d 6f 73 74 20 ontains at most
5ecc0 31 20 62 2d 74 72 65 65 20 2a 2f 0a 23 64 65 66 1 b-tree */.#def
5ecd0 69 6e 65 20 42 54 52 45 45 5f 55 4e 4f 52 44 45 ine BTREE_UNORDE
5ece0 52 45 44 20 20 20 20 20 38 20 20 2f 2a 20 55 73 RED 8 /* Us
5ecf0 65 20 6f 66 20 61 20 68 61 73 68 20 69 6d 70 6c e of a hash impl
5ed00 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 4f 4b ementation is OK
5ed10 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 */..SQLITE_PRIV
5ed20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
5ed30 74 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 2a treeClose(Btree*
5ed40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5ed50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
5ed60 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 eeSetCacheSize(B
5ed70 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 tree*,int);.SQLI
5ed80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
5ed90 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 qlite3BtreeSetSa
5eda0 66 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 2a fetyLevel(Btree*
5edb0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 ,int,int,int);.S
5edc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5edd0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 t sqlite3BtreeSy
5ede0 6e 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 65 ncDisabled(Btree
5edf0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
5ee00 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
5ee10 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42 reeSetPageSize(B
5ee20 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 50 61 tree *p, int nPa
5ee30 67 65 73 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 gesize, int nRes
5ee40 65 72 76 65 2c 20 69 6e 74 20 65 46 69 78 29 3b erve, int eFix);
5ee50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5ee60 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
5ee70 47 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 GetPageSize(Btre
5ee80 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
5ee90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
5eea0 74 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 treeMaxPageCount
5eeb0 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 (Btree*,int);.SQ
5eec0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 LITE_PRIVATE u32
5eed0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 sqlite3BtreeLas
5eee0 74 50 61 67 65 28 42 74 72 65 65 2a 29 3b 0a 53 tPage(Btree*);.S
5eef0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5ef00 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 t sqlite3BtreeSe
5ef10 63 75 72 65 44 65 6c 65 74 65 28 42 74 72 65 65 cureDelete(Btree
5ef20 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
5ef30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
5ef40 65 33 42 74 72 65 65 47 65 74 52 65 73 65 72 76 e3BtreeGetReserv
5ef50 65 28 42 74 72 65 65 2a 29 3b 0a 23 69 66 20 64 e(Btree*);.#if d
5ef60 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 efined(SQLITE_HA
5ef70 53 5f 43 4f 44 45 43 29 20 7c 7c 20 64 65 66 69 S_CODEC) || defi
5ef80 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
5ef90 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
5efa0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
5efb0 65 47 65 74 52 65 73 65 72 76 65 4e 6f 4d 75 74 eGetReserveNoMut
5efc0 65 78 28 42 74 72 65 65 20 2a 70 29 3b 0a 23 65 ex(Btree *p);.#e
5efd0 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
5efe0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
5eff0 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 treeSetAutoVacuu
5f000 6d 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 29 3b m(Btree *, int);
5f010 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5f020 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
5f030 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 GetAutoVacuum(Bt
5f040 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ree *);.SQLITE_P
5f050 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
5f060 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e e3BtreeBeginTran
5f070 73 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 s(Btree*,int);.S
5f080 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5f090 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
5f0a0 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72 mmitPhaseOne(Btr
5f0b0 65 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 ee*, const char
5f0c0 2a 7a 4d 61 73 74 65 72 29 3b 0a 53 51 4c 49 54 *zMaster);.SQLIT
5f0d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
5f0e0 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
5f0f0 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 2a 2c PhaseTwo(Btree*,
5f100 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
5f110 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5f120 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 3BtreeCommit(Btr
5f130 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
5f140 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
5f150 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 BtreeRollback(Bt
5f160 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ree*,int);.SQLIT
5f170 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
5f180 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 lite3BtreeBeginS
5f190 74 6d 74 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b tmt(Btree*,int);
5f1a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5f1b0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
5f1c0 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 CreateTable(Btre
5f1d0 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 20 66 6c e*, int*, int fl
5f1e0 61 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ags);.SQLITE_PRI
5f1f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
5f200 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 42 BtreeIsInTrans(B
5f210 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tree*);.SQLITE_P
5f220 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
5f230 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 e3BtreeIsInReadT
5f240 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53 51 rans(Btree*);.SQ
5f250 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
5f260 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 sqlite3BtreeIsI
5f270 6e 42 61 63 6b 75 70 28 42 74 72 65 65 2a 29 3b nBackup(Btree*);
5f280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5f290 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 void *sqlite3Btr
5f2a0 65 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a eeSchema(Btree *
5f2b0 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 , int, void(*)(v
5f2c0 6f 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45 5f oid *));.SQLITE_
5f2d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
5f2e0 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f te3BtreeSchemaLo
5f2f0 63 6b 65 64 28 42 74 72 65 65 20 2a 70 42 74 72 cked(Btree *pBtr
5f300 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ee);.SQLITE_PRIV
5f310 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
5f320 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 treeLockTable(Bt
5f330 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 ree *pBtree, int
5f340 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 74 iTab, u8 isWrit
5f350 65 4c 6f 63 6b 29 3b 0a 53 51 4c 49 54 45 5f 50 eLock);.SQLITE_P
5f360 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
5f370 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 e3BtreeSavepoint
5f380 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 69 (Btree *, int, i
5f390 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 nt);..SQLITE_PRI
5f3a0 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 VATE const char
5f3b0 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 *sqlite3BtreeGet
5f3c0 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 20 2a Filename(Btree *
5f3d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5f3e0 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 E const char *sq
5f3f0 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 lite3BtreeGetJou
5f400 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a rnalname(Btree *
5f410 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
5f420 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
5f430 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 eeCopyFile(Btree
5f440 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a 53 *, Btree *);..S
5f450 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
5f460 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e t sqlite3BtreeIn
5f470 63 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a crVacuum(Btree *
5f480 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 );../* The flags
5f490 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
5f4a0 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 lite3BtreeCreate
5f4b0 54 61 62 6c 65 20 63 61 6e 20 62 65 20 74 68 65 Table can be the
5f4c0 20 62 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20 6f bitwise OR.** o
5f4d0 66 20 74 68 65 20 66 6c 61 67 73 20 73 68 6f 77 f the flags show
5f4e0 6e 20 62 65 6c 6f 77 2e 0a 2a 2a 0a 2a 2a 20 45 n below..**.** E
5f4f0 76 65 72 79 20 53 51 4c 69 74 65 20 74 61 62 6c very SQLite tabl
5f500 65 20 6d 75 73 74 20 68 61 76 65 20 65 69 74 68 e must have eith
5f510 65 72 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 20 er BTREE_INTKEY
5f520 6f 72 20 42 54 52 45 45 5f 42 4c 4f 42 4b 45 59 or BTREE_BLOBKEY
5f530 20 73 65 74 2e 0a 2a 2a 20 57 69 74 68 20 42 54 set..** With BT
5f540 52 45 45 5f 49 4e 54 4b 45 59 2c 20 74 68 65 20 REE_INTKEY, the
5f550 74 61 62 6c 65 20 6b 65 79 20 69 73 20 61 20 36 table key is a 6
5f560 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 61 6e 4-bit integer an
5f570 64 20 61 72 62 69 74 72 61 72 79 20 64 61 74 61 d arbitrary data
5f580 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 69 6e .** is stored in
5f590 20 74 68 65 20 6c 65 61 76 65 73 2e 20 20 28 42 the leaves. (B
5f5a0 54 52 45 45 5f 49 4e 54 4b 45 59 20 69 73 20 75 TREE_INTKEY is u
5f5b0 73 65 64 20 66 6f 72 20 53 51 4c 20 74 61 62 6c sed for SQL tabl
5f5c0 65 73 2e 29 20 20 57 69 74 68 0a 2a 2a 20 42 54 es.) With.** BT
5f5d0 52 45 45 5f 42 4c 4f 42 4b 45 59 2c 20 74 68 65 REE_BLOBKEY, the
5f5e0 20 6b 65 79 20 69 73 20 61 6e 20 61 72 62 69 74 key is an arbit
5f5f0 72 61 72 79 20 42 4c 4f 42 20 61 6e 64 20 6e 6f rary BLOB and no
5f600 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 content is stor
5f610 65 64 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20 2d ed.** anywhere -
5f620 20 74 68 65 20 6b 65 79 20 69 73 20 74 68 65 20 the key is the
5f630 63 6f 6e 74 65 6e 74 2e 20 20 28 42 54 52 45 45 content. (BTREE
5f640 5f 42 4c 4f 42 4b 45 59 20 69 73 20 75 73 65 64 _BLOBKEY is used
5f650 20 66 6f 72 20 53 51 4c 0a 2a 2a 20 69 6e 64 69 for SQL.** indi
5f660 63 65 73 2e 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 ces.).*/.#define
5f670 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 20 20 20 BTREE_INTKEY
5f680 20 20 31 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 1 /* Table
5f690 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69 74 20 has only 64-bit
5f6a0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 6b signed integer k
5f6b0 65 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 eys */.#define B
5f6c0 54 52 45 45 5f 42 4c 4f 42 4b 45 59 20 20 20 20 TREE_BLOBKEY
5f6d0 32 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 2 /* Table ha
5f6e0 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f s keys only - no
5f6f0 20 64 61 74 61 20 2a 2f 0a 0a 53 51 4c 49 54 45 data */..SQLITE
5f700 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
5f710 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 ite3BtreeDropTab
5f720 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 le(Btree*, int,
5f730 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
5f740 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
5f750 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 3BtreeClearTable
5f760 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e (Btree*, int, in
5f770 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
5f780 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
5f790 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 BtreeTripAllCurs
5f7a0 6f 72 73 28 42 74 72 65 65 2a 2c 20 69 6e 74 29 ors(Btree*, int)
5f7b0 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
5f7c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
5f7d0 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65 reeGetMeta(Btree
5f7e0 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 69 64 *pBtree, int id
5f7f0 78 2c 20 75 33 32 20 2a 70 56 61 6c 75 65 29 3b x, u32 *pValue);
5f800 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5f810 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
5f820 55 70 64 61 74 65 4d 65 74 61 28 42 74 72 65 65 UpdateMeta(Btree
5f830 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 *, int idx, u32
5f840 76 61 6c 75 65 29 3b 0a 0a 53 51 4c 49 54 45 5f value);..SQLITE_
5f850 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
5f860 74 65 33 42 74 72 65 65 4e 65 77 44 62 28 42 74 te3BtreeNewDb(Bt
5f870 72 65 65 20 2a 70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 ree *p);../*.**
5f880 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d The second param
5f890 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 eter to sqlite3B
5f8a0 74 72 65 65 47 65 74 4d 65 74 61 20 6f 72 20 73 treeGetMeta or s
5f8b0 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 qlite3BtreeUpdat
5f8c0 65 4d 65 74 61 0a 2a 2a 20 73 68 6f 75 6c 64 20 eMeta.** should
5f8d0 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f be one of the fo
5f8e0 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 20 llowing values.
5f8f0 54 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 The integer valu
5f900 65 73 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 es are assigned
5f910 0a 2a 2a 20 74 6f 20 63 6f 6e 73 74 61 6e 74 73 .** to constants
5f920 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f 66 66 so that the off
5f930 73 65 74 20 6f 66 20 74 68 65 20 63 6f 72 72 65 set of the corre
5f940 73 70 6f 6e 64 69 6e 67 20 66 69 65 6c 64 20 69 sponding field i
5f950 6e 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 64 n an.** SQLite d
5f960 61 74 61 62 61 73 65 20 68 65 61 64 65 72 20 6d atabase header m
5f970 61 79 20 62 65 20 66 6f 75 6e 64 20 75 73 69 6e ay be found usin
5f980 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 g the following
5f990 66 6f 72 6d 75 6c 61 3a 0a 2a 2a 0a 2a 2a 20 20 formula:.**.**
5f9a0 20 6f 66 66 73 65 74 20 3d 20 33 36 20 2b 20 28 offset = 36 + (
5f9b0 69 64 78 20 2a 20 34 29 0a 2a 2a 0a 2a 2a 20 46 idx * 4).**.** F
5f9c0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 or example, the
5f9d0 66 72 65 65 2d 70 61 67 65 2d 63 6f 75 6e 74 20 free-page-count
5f9e0 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 65 64 field is located
5f9f0 20 61 74 20 62 79 74 65 20 6f 66 66 73 65 74 20 at byte offset
5fa00 33 36 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74 36 of.** the dat
5fa10 61 62 61 73 65 20 66 69 6c 65 20 68 65 61 64 65 abase file heade
5fa20 72 2e 20 54 68 65 20 69 6e 63 72 2d 76 61 63 75 r. The incr-vacu
5fa30 75 6d 2d 66 6c 61 67 20 66 69 65 6c 64 20 69 73 um-flag field is
5fa40 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 62 located at.** b
5fa50 79 74 65 20 6f 66 66 73 65 74 20 36 34 20 28 3d yte offset 64 (=
5fa60 3d 20 33 36 2b 34 2a 37 29 2e 0a 2a 2f 0a 23 64 = 36+4*7)..*/.#d
5fa70 65 66 69 6e 65 20 42 54 52 45 45 5f 46 52 45 45 efine BTREE_FREE
5fa80 5f 50 41 47 45 5f 43 4f 55 4e 54 20 20 20 20 20 _PAGE_COUNT
5fa90 30 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 0.#define BTREE_
5faa0 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 20 SCHEMA_VERSION
5fab0 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 42 54 1.#define BT
5fac0 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 REE_FILE_FORMAT
5fad0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 2.#defin
5fae0 65 20 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f e BTREE_DEFAULT_
5faf0 43 41 43 48 45 5f 53 49 5a 45 20 20 33 0a 23 64 CACHE_SIZE 3.#d
5fb00 65 66 69 6e 65 20 42 54 52 45 45 5f 4c 41 52 47 efine BTREE_LARG
5fb10 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 20 20 EST_ROOT_PAGE
5fb20 34 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4.#define BTREE_
5fb30 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 20 20 20 TEXT_ENCODING
5fb40 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 42 54 5.#define BT
5fb50 52 45 45 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e REE_USER_VERSION
5fb60 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 6.#defin
5fb70 65 20 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 e BTREE_INCR_VAC
5fb80 55 55 4d 20 20 20 20 20 20 20 20 20 37 0a 0a 2f UUM 7../
5fb90 2a 0a 2a 2a 20 56 61 6c 75 65 73 20 74 68 61 74 *.** Values that
5fba0 20 6d 61 79 20 62 65 20 4f 52 27 64 20 74 6f 67 may be OR'd tog
5fbb0 65 74 68 65 72 20 74 6f 20 66 6f 72 6d 20 74 68 ether to form th
5fbc0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
5fbd0 74 20 6f 66 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 t of an.** sqlit
5fbe0 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 69 6e e3BtreeCursorHin
5fbf0 74 73 28 29 20 63 61 6c 6c 2e 0a 2a 2f 0a 23 64 ts() call..*/.#d
5fc00 65 66 69 6e 65 20 42 54 52 45 45 5f 42 55 4c 4b efine BTREE_BULK
5fc10 4c 4f 41 44 20 30 78 30 30 30 30 30 30 30 31 0a LOAD 0x00000001.
5fc20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5fc30 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
5fc40 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 2a Cursor(. Btree*
5fc50 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
5fc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5fc70 2a 20 42 54 72 65 65 20 63 6f 6e 74 61 69 6e 69 * BTree containi
5fc80 6e 67 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e ng table to open
5fc90 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 */. int iTable
5fca0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
5fcb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
5fcc0 64 65 78 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 dex of root page
5fcd0 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 */. int wrFlag
5fce0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
5fcf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 /* 1
5fd00 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 20 30 20 for writing. 0
5fd10 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f for read-only */
5fd20 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 . struct KeyInf
5fd30 6f 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 o*,
5fd40 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
5fd50 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d argument to com
5fd60 70 61 72 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f pare function */
5fd70 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
5fd80 72 73 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 rsor
5fd90 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 /* Space
5fda0 20 74 6f 20 77 72 69 74 65 20 63 75 72 73 6f 72 to write cursor
5fdb0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 3b structure */.);
5fdc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5fdd0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
5fde0 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 CursorSize(void)
5fdf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5fe00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
5fe10 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 42 74 43 eeCursorZero(BtC
5fe20 75 72 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 ursor*);..SQLITE
5fe30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
5fe40 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 ite3BtreeCloseCu
5fe50 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b rsor(BtCursor*);
5fe60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
5fe70 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
5fe80 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a MovetoUnpacked(.
5fe90 20 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55 BtCursor*,. U
5fea0 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 npackedRecord *p
5feb0 55 6e 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e 74 UnKey,. i64 int
5fec0 4b 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c Key,. int bias,
5fed0 0a 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a . int *pRes.);.
5fee0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5fef0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
5ff00 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 ursorHasMoved(Bt
5ff10 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a Cursor*, int*);.
5ff20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
5ff30 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 nt sqlite3BtreeD
5ff40 65 6c 65 74 65 28 42 74 43 75 72 73 6f 72 2a 29 elete(BtCursor*)
5ff50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
5ff60 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
5ff70 65 49 6e 73 65 72 74 28 42 74 43 75 72 73 6f 72 eInsert(BtCursor
5ff80 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 *, const void *p
5ff90 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20 Key, i64 nKey,.
5ffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ffb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ffc0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 const void *pDa
5ffd0 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 ta, int nData,.
5ffe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5fff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
60000 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 int nZero, int
60010 62 69 61 73 2c 20 69 6e 74 20 73 65 65 6b 52 65 bias, int seekRe
60020 73 75 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sult);.SQLITE_PR
60030 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
60040 33 42 74 72 65 65 46 69 72 73 74 28 42 74 43 75 3BtreeFirst(BtCu
60050 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 rsor*, int *pRes
60060 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
60070 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
60080 65 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 2a eeLast(BtCursor*
60090 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 , int *pRes);.SQ
600a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
600b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 sqlite3BtreeNex
600c0 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 t(BtCursor*, int
600d0 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f *pRes);.SQLITE_
600e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
600f0 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 te3BtreeEof(BtCu
60100 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 rsor*);.SQLITE_P
60110 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
60120 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 e3BtreePrevious(
60130 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a BtCursor*, int *
60140 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pRes);.SQLITE_PR
60150 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
60160 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 3BtreeKeySize(Bt
60170 43 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70 53 Cursor*, i64 *pS
60180 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ize);.SQLITE_PRI
60190 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
601a0 42 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f BtreeKey(BtCurso
601b0 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 r*, u32 offset,
601c0 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b u32 amt, void*);
601d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
601e0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
601f0 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 te3BtreeKeyFetch
60200 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 (BtCursor*, int
60210 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *pAmt);.SQLITE_P
60220 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 RIVATE const voi
60230 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 d *sqlite3BtreeD
60240 61 74 61 46 65 74 63 68 28 42 74 43 75 72 73 6f ataFetch(BtCurso
60250 72 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a r*, int *pAmt);.
60260 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
60270 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 nt sqlite3BtreeD
60280 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 ataSize(BtCursor
60290 2a 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a *, u32 *pSize);.
602a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
602b0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 nt sqlite3BtreeD
602c0 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 ata(BtCursor*, u
602d0 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 32 offset, u32 a
602e0 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 mt, void*);.SQLI
602f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
60300 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 sqlite3BtreeSetC
60310 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 achedRowid(BtCur
60320 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e sor*, sqlite3_in
60330 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 t64);.SQLITE_PRI
60340 56 41 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 VATE sqlite3_int
60350 36 34 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 64 sqlite3BtreeG
60360 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 etCachedRowid(Bt
60370 43 75 72 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 Cursor*);..SQLIT
60380 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
60390 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 sqlite3BtreeInte
603a0 67 72 69 74 79 43 68 65 63 6b 28 42 74 72 65 65 grityCheck(Btree
603b0 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 69 *, int *aRoot, i
603c0 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 69 nt nRoot, int, i
603d0 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
603e0 56 41 54 45 20 73 74 72 75 63 74 20 50 61 67 65 VATE struct Page
603f0 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 r *sqlite3BtreeP
60400 61 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a 53 ager(Btree*);..S
60410 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
60420 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 t sqlite3BtreePu
60430 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c tData(BtCursor*,
60440 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 u32 offset, u32
60450 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 amt, void*);.SQ
60460 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
60470 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 d sqlite3BtreeCa
60480 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 cheOverflow(BtCu
60490 72 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f rsor *);.SQLITE_
604a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
604b0 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 ite3BtreeClearCu
604c0 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 29 rsor(BtCursor *)
604d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
604e0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
604f0 65 53 65 74 56 65 72 73 69 6f 6e 28 42 74 72 65 eSetVersion(Btre
60500 65 20 2a 70 42 74 2c 20 69 6e 74 20 69 56 65 72 e *pBt, int iVer
60510 73 69 6f 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sion);.SQLITE_PR
60520 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
60530 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 69 6e e3BtreeCursorHin
60540 74 73 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 75 ts(BtCursor *, u
60550 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d 61 73 6b nsigned int mask
60560 29 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 );..#ifndef NDEB
60570 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
60580 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
60590 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 eeCursorIsValid(
605a0 42 74 43 75 72 73 6f 72 2a 29 3b 0a 23 65 6e 64 BtCursor*);.#end
605b0 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
605c0 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 TE_OMIT_BTREECOU
605d0 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 NT.SQLITE_PRIVAT
605e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
605f0 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 eeCount(BtCursor
60600 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 23 65 6e 64 *, i64 *);.#end
60610 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
60620 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 E_TEST.SQLITE_PR
60630 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
60640 33 42 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f 3BtreeCursorInfo
60650 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a (BtCursor*, int*
60660 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
60670 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
60680 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69 te3BtreeCursorLi
60690 73 74 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64 st(Btree*);.#end
606a0 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
606b0 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 53 51 4c 49 TE_OMIT_WAL.SQLI
606c0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
606d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 68 65 sqlite3BtreeChe
606e0 63 6b 70 6f 69 6e 74 28 42 74 72 65 65 2a 2c 20 ckpoint(Btree*,
606f0 69 6e 74 2c 20 69 6e 74 20 2a 2c 20 69 6e 74 20 int, int *, int
60700 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a *);.#endif../*.*
60710 2a 20 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 * If we are not
60720 75 73 69 6e 67 20 73 68 61 72 65 64 20 63 61 63 using shared cac
60730 68 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 he, then there i
60740 73 20 6e 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 s no need to.**
60750 75 73 65 20 6d 75 74 65 78 65 73 20 74 6f 20 61 use mutexes to a
60760 63 63 65 73 73 20 74 68 65 20 42 74 53 68 61 72 ccess the BtShar
60770 65 64 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 ed structures.
60780 53 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 45 So make the.** E
60790 6e 74 65 72 20 61 6e 64 20 4c 65 61 76 65 20 70 nter and Leave p
607a0 72 6f 63 65 64 75 72 65 73 20 6e 6f 2d 6f 70 73 rocedures no-ops
607b0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
607c0 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
607d0 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 CACHE.SQLITE_PRI
607e0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
607f0 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 te3BtreeEnter(Bt
60800 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
60810 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
60820 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c ite3BtreeEnterAl
60830 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c l(sqlite3*);.#el
60840 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 se.# define sqli
60850 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 58 29 te3BtreeEnter(X)
60860 20 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 .# define sqlit
60870 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 e3BtreeEnterAll(
60880 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 X).#endif..#if !
60890 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
608a0 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
608b0 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 ) && SQLITE_THRE
608c0 41 44 53 41 46 45 0a 53 51 4c 49 54 45 5f 50 52 ADSAFE.SQLITE_PR
608d0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
608e0 74 65 33 42 74 72 65 65 53 68 61 72 61 62 6c 65 te3BtreeSharable
608f0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
60900 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
60910 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
60920 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 e(Btree*);.SQLIT
60930 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
60940 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
60950 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f erCursor(BtCurso
60960 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
60970 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
60980 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 e3BtreeLeaveCurs
60990 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 or(BtCursor*);.S
609a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
609b0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
609c0 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 eLeaveAll(sqlite
609d0 33 2a 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 3*);.#ifndef NDE
609e0 42 55 47 0a 20 20 2f 2a 20 54 68 65 73 65 20 72 BUG. /* These r
609f0 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 outines are used
60a00 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 inside assert()
60a10 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 statements only
60a20 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 . */.SQLITE_PRIV
60a30 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
60a40 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 3BtreeHoldsMutex
60a50 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
60a60 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
60a70 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
60a80 41 6c 6c 4d 75 74 65 78 65 73 28 73 71 6c 69 74 AllMutexes(sqlit
60a90 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e3*);.SQLITE_PRI
60aa0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
60ab0 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c e3SchemaMutexHel
60ac0 64 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 53 d(sqlite3*,int,S
60ad0 63 68 65 6d 61 2a 29 3b 0a 23 65 6e 64 69 66 0a chema*);.#endif.
60ae0 23 65 6c 73 65 0a 0a 23 20 64 65 66 69 6e 65 20 #else..# define
60af0 73 71 6c 69 74 65 33 42 74 72 65 65 53 68 61 72 sqlite3BtreeShar
60b00 61 62 6c 65 28 58 29 20 30 0a 23 20 64 65 66 69 able(X) 0.# defi
60b10 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c ne sqlite3BtreeL
60b20 65 61 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 eave(X).# define
60b30 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
60b40 65 72 43 75 72 73 6f 72 28 58 29 0a 23 20 64 65 erCursor(X).# de
60b50 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 fine sqlite3Btre
60b60 65 4c 65 61 76 65 43 75 72 73 6f 72 28 58 29 0a eLeaveCursor(X).
60b70 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
60b80 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 58 29 BtreeLeaveAll(X)
60b90 0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ..# define sqlit
60ba0 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
60bb0 78 28 58 29 20 31 0a 23 20 64 65 66 69 6e 65 20 x(X) 1.# define
60bc0 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
60bd0 73 41 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 31 sAllMutexes(X) 1
60be0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
60bf0 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 3SchemaMutexHeld
60c00 28 58 2c 59 2c 5a 29 20 31 0a 23 65 6e 64 69 66 (X,Y,Z) 1.#endif
60c10 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42 54 ...#endif /* _BT
60c20 52 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a REE_H_ */../****
60c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
60c40 66 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a f btree.h ******
60c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
60c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
60c90 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
60ca0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 eft off in sqlit
60cb0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
60cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
60cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
60ce0 64 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68 65 de vdbe.h in the
60cf0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 middle of sqlit
60d00 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
60d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
60d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
60d30 20 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a 2a file vdbe.h ***
60d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
60d70 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
60d80 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
60d90 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
60da0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
60db0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
60dc0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
60dd0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
60de0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
60df0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
60e00 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
60e10 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
60e20 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
60e30 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
60e40 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
60e50 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
60e60 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
60e70 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
60e80 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
60e90 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
60ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
60ee0 2a 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c 65 *.** Header file
60ef0 20 66 6f 72 20 74 68 65 20 56 69 72 74 75 61 6c for the Virtual
60f00 20 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e 65 DataBase Engine
60f10 20 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68 (VDBE).**.** Th
60f20 69 73 20 68 65 61 64 65 72 20 64 65 66 69 6e 65 is header define
60f30 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 s the interface
60f40 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 64 to the virtual d
60f50 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a 2a atabase engine.*
60f60 2a 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65 20 * or VDBE. The
60f70 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 20 VDBE implements
60f80 61 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63 68 an abstract mach
60f90 69 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61 0a ine that runs a.
60fa0 2a 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 61 ** simple progra
60fb0 6d 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 20 m to access and
60fc0 6d 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 72 modify the under
60fd0 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e 0a lying database..
60fe0 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 */.#ifndef _SQLI
60ff0 54 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 69 TE_VDBE_H_.#defi
61000 6e 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f ne _SQLITE_VDBE_
61010 48 5f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c H_./* #include <
61020 73 74 64 69 6f 2e 68 3e 20 2a 2f 0a 0a 2f 2a 0a stdio.h> */../*.
61030 2a 2a 20 41 20 73 69 6e 67 6c 65 20 56 44 42 45 ** A single VDBE
61040 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 is an opaque st
61050 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 56 ructure named "V
61060 64 62 65 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 dbe". Only rout
61070 69 6e 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 ines.** in the s
61080 6f 75 72 63 65 20 66 69 6c 65 20 73 71 6c 69 74 ource file sqlit
61090 65 56 64 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f eVdbe.c are allo
610a0 77 65 64 20 74 6f 20 73 65 65 20 74 68 65 20 69 wed to see the i
610b0 6e 73 69 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69 nsides.** of thi
610c0 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a s structure..*/.
610d0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 typedef struct V
610e0 64 62 65 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a dbe Vdbe;../*.**
610f0 20 54 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 The names of th
61100 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 e following type
61110 73 20 64 65 63 6c 61 72 65 64 20 69 6e 20 76 64 s declared in vd
61120 62 65 49 6e 74 2e 68 20 61 72 65 20 72 65 71 75 beInt.h are requ
61130 69 72 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 ired.** for the
61140 56 64 62 65 4f 70 20 64 65 66 69 6e 69 74 69 6f VdbeOp definitio
61150 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 n..*/.typedef st
61160 72 75 63 74 20 56 64 62 65 46 75 6e 63 20 56 64 ruct VdbeFunc Vd
61170 62 65 46 75 6e 63 3b 0a 74 79 70 65 64 65 66 20 beFunc;.typedef
61180 73 74 72 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a struct Mem Mem;.
61190 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 typedef struct S
611a0 75 62 50 72 6f 67 72 61 6d 20 53 75 62 50 72 6f ubProgram SubPro
611b0 67 72 61 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 gram;../*.** A s
611c0 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f ingle instructio
611d0 6e 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c n of the virtual
611e0 20 6d 61 63 68 69 6e 65 20 68 61 73 20 61 6e 20 machine has an
611f0 6f 70 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61 73 opcode.** and as
61200 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20 6f many as three o
61210 70 65 72 61 6e 64 73 2e 20 20 54 68 65 20 69 6e perands. The in
61220 73 74 72 75 63 74 69 6f 6e 20 69 73 20 72 65 63 struction is rec
61230 6f 72 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20 69 orded.** as an i
61240 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
61250 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
61260 72 65 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 re:.*/.struct Vd
61270 62 65 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63 6f beOp {. u8 opco
61280 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 de; /*
61290 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74 What operation t
612a0 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73 o perform */. s
612b0 69 67 6e 65 64 20 63 68 61 72 20 70 34 74 79 70 igned char p4typ
612c0 65 3b 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 e; /* One of the
612d0 20 50 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e 74 P4_xxx constant
612e0 73 20 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75 38 s for p4 */. u8
612f0 20 6f 70 66 6c 61 67 73 3b 20 20 20 20 20 20 20 opflags;
61300 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 74 68 65 /* Mask of the
61310 20 4f 50 46 4c 47 5f 2a 20 66 6c 61 67 73 20 69 OPFLG_* flags i
61320 6e 20 6f 70 63 6f 64 65 73 2e 68 20 2a 2f 0a 20 n opcodes.h */.
61330 20 75 38 20 70 35 3b 20 20 20 20 20 20 20 20 20 u8 p5;
61340 20 20 20 20 20 2f 2a 20 46 69 66 74 68 20 70 61 /* Fifth pa
61350 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 75 6e rameter is an un
61360 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 65 72 signed character
61370 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 */. int p1;
61380 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
61390 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 st operand */.
613a0 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 int p2;
613b0 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 /* Second pa
613c0 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 74 rameter (often t
613d0 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 he jump destinat
613e0 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e 74 20 70 33 ion) */. int p3
613f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
61400 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d The third param
61410 65 74 65 72 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 eter */. union
61420 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a { /*
61430 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 fourth paramete
61440 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 20 r */. int i;
61450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
61460 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 /* Integer value
61470 20 69 66 20 70 34 74 79 70 65 3d 3d 50 34 5f 49 if p4type==P4_I
61480 4e 54 33 32 20 2a 2f 0a 20 20 20 20 76 6f 69 64 NT32 */. void
61490 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 *p;
614a0 20 20 20 2f 2a 20 47 65 6e 65 72 69 63 20 70 6f /* Generic po
614b0 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 63 68 61 inter */. cha
614c0 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 r *z;
614d0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
614e0 6f 20 64 61 74 61 20 66 6f 72 20 73 74 72 69 6e o data for strin
614f0 67 20 28 63 68 61 72 20 61 72 72 61 79 29 20 74 g (char array) t
61500 79 70 65 73 20 2a 2f 0a 20 20 20 20 69 36 34 20 ypes */. i64
61510 2a 70 49 36 34 3b 20 20 20 20 20 20 20 20 20 20 *pI64;
61520 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 /* Used when
61530 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e 54 p4type is P4_INT
61540 36 34 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 64 */. double
61550 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20 *pReal;
61560 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 /* Used when p4
61570 74 79 70 65 20 69 73 20 50 34 5f 52 45 41 4c 20 type is P4_REAL
61580 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a */. FuncDef *
61590 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 2f 2a pFunc; /*
615a0 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 Used when p4typ
615b0 65 20 69 73 20 50 34 5f 46 55 4e 43 44 45 46 20 e is P4_FUNCDEF
615c0 2a 2f 0a 20 20 20 20 56 64 62 65 46 75 6e 63 20 */. VdbeFunc
615d0 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 20 2f 2a *pVdbeFunc; /*
615e0 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 Used when p4typ
615f0 65 20 69 73 20 50 34 5f 56 44 42 45 46 55 4e 43 e is P4_VDBEFUNC
61600 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 */. CollSeq
61610 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 2f *pColl; /
61620 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 * Used when p4ty
61630 70 65 20 69 73 20 50 34 5f 43 4f 4c 4c 53 45 51 pe is P4_COLLSEQ
61640 20 2a 2f 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65 */. Mem *pMe
61650 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f m; /
61660 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 * Used when p4ty
61670 70 65 20 69 73 20 50 34 5f 4d 45 4d 20 2a 2f 0a pe is P4_MEM */.
61680 20 20 20 20 56 54 61 62 6c 65 20 2a 70 56 74 61 VTable *pVta
61690 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 b; /* Us
616a0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
616b0 73 20 50 34 5f 56 54 41 42 20 2a 2f 0a 20 20 20 s P4_VTAB */.
616c0 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
616d0 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20 fo; /* Used
616e0 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 when p4type is P
616f0 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f 0a 20 20 20 4_KEYINFO */.
61700 20 69 6e 74 20 2a 61 69 3b 20 20 20 20 20 20 20 int *ai;
61710 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
61720 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 when p4type is P
61730 34 5f 49 4e 54 41 52 52 41 59 20 2a 2f 0a 20 20 4_INTARRAY */.
61740 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 SubProgram *pP
61750 72 6f 67 72 61 6d 3b 20 20 2f 2a 20 55 73 65 64 rogram; /* Used
61760 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 when p4type is
61770 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 2a 2f P4_SUBPROGRAM */
61780 0a 20 20 20 20 69 6e 74 20 28 2a 78 41 64 76 61 . int (*xAdva
61790 6e 63 65 29 28 42 74 43 75 72 73 6f 72 20 2a 2c nce)(BtCursor *,
617a0 20 69 6e 74 20 2a 29 3b 0a 20 20 7d 20 70 34 3b int *);. } p4;
617b0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
617c0 45 42 55 47 0a 20 20 63 68 61 72 20 2a 7a 43 6f EBUG. char *zCo
617d0 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 mment;
617e0 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20 69 6d /* Comment to im
617f0 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c 69 74 prove readabilit
61800 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 y */.#endif.#ifd
61810 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a ef VDBE_PROFILE.
61820 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 int cnt;
61830 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
61840 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 mber of times th
61850 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 is instruction w
61860 61 73 20 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 as executed */.
61870 20 75 36 34 20 63 79 63 6c 65 73 3b 20 20 20 20 u64 cycles;
61880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 /* Tot
61890 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 20 65 78 al time spent ex
618a0 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e 73 ecuting this ins
618b0 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 truction */.#end
618c0 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 if.};.typedef st
618d0 72 75 63 74 20 56 64 62 65 4f 70 20 56 64 62 65 ruct VdbeOp Vdbe
618e0 4f 70 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 75 Op;.../*.** A su
618f0 62 2d 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74 b-routine used t
61900 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 74 72 o implement a tr
61910 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a 2a igger program..*
61920 2f 0a 73 74 72 75 63 74 20 53 75 62 50 72 6f 67 /.struct SubProg
61930 72 61 6d 20 7b 0a 20 20 56 64 62 65 4f 70 20 2a ram {. VdbeOp *
61940 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 aOp;
61950 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f /* Array o
61960 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 73 75 f opcodes for su
61970 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 b-program */. i
61980 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 20 20 nt nOp;
61990 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
619a0 45 6c 65 6d 65 6e 74 73 20 69 6e 20 61 4f 70 5b Elements in aOp[
619b0 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b ] */. int nMem;
619c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
619d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
619e0 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 72 f memory cells r
619f0 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 equired */. int
61a00 20 6e 43 73 72 3b 20 20 20 20 20 20 20 20 20 20 nCsr;
61a10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
61a20 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 mber of cursors
61a30 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e required */. in
61a40 74 20 6e 4f 6e 63 65 3b 20 20 20 20 20 20 20 20 t nOnce;
61a50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
61a60 75 6d 62 65 72 20 6f 66 20 4f 50 5f 4f 6e 63 65 umber of OP_Once
61a70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f instructions */
61a80 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e 3b 20 . void *token;
61a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
61aa0 20 2f 2a 20 69 64 20 74 68 61 74 20 6d 61 79 20 /* id that may
61ab0 62 65 20 75 73 65 64 20 74 6f 20 72 65 63 75 72 be used to recur
61ac0 73 69 76 65 20 74 72 69 67 67 65 72 73 20 2a 2f sive triggers */
61ad0 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 . SubProgram *p
61ae0 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 Next;
61af0 20 2f 2a 20 4e 65 78 74 20 73 75 62 2d 70 72 6f /* Next sub-pro
61b00 67 72 61 6d 20 61 6c 72 65 61 64 79 20 76 69 73 gram already vis
61b10 69 74 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ited */.};../*.*
61b20 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65 72 73 * A smaller vers
61b30 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20 75 73 ion of VdbeOp us
61b40 65 64 20 66 6f 72 20 74 68 65 20 56 64 62 65 41 ed for the VdbeA
61b50 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e 63 74 ddOpList() funct
61b60 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 69 ion because.** i
61b70 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73 73 20 t takes up less
61b80 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 space..*/.struct
61b90 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20 20 VdbeOpList {.
61ba0 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 u8 opcode;
61bb0 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 /* What oper
61bc0 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d ation to perform
61bd0 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 */. signed cha
61be0 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46 69 72 r p1; /* Fir
61bf0 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 st operand */.
61c00 73 69 67 6e 65 64 20 63 68 61 72 20 70 32 3b 20 signed char p2;
61c10 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 /* Second pa
61c20 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 74 rameter (often t
61c30 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 he jump destinat
61c40 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65 64 ion) */. signed
61c50 20 63 68 61 72 20 70 33 3b 20 20 20 20 20 2f 2a char p3; /*
61c60 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 Third parameter
61c70 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 */.};.typedef s
61c80 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69 73 74 truct VdbeOpList
61c90 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f 2a VdbeOpList;../*
61ca0 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 .** Allowed valu
61cb0 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70 34 74 es of VdbeOp.p4t
61cc0 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ype.*/.#define P
61cd0 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30 20 20 4_NOTUSED 0
61ce0 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72 61 6d /* The P4 param
61cf0 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64 eter is not used
61d00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 44 */.#define P4_D
61d10 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20 2f 2a YNAMIC (-1) /*
61d20 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 Pointer to a st
61d30 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 ring obtained fr
61d40 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 om sqliteMalloc(
61d50 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f ) */.#define P4_
61d60 53 54 41 54 49 43 20 20 20 28 2d 32 29 20 20 2f STATIC (-2) /
61d70 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 * Pointer to a s
61d80 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a tatic string */.
61d90 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c 53 #define P4_COLLS
61da0 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 34 20 EQ (-4) /* P4
61db0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
61dc0 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 a CollSeq struct
61dd0 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ure */.#define P
61de0 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 29 20 4_FUNCDEF (-5)
61df0 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e /* P4 is a poin
61e00 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 65 66 ter to a FuncDef
61e10 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 structure */.#d
61e20 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f efine P4_KEYINFO
61e30 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 69 73 (-6) /* P4 is
61e40 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
61e50 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
61e60 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f e */.#define P4_
61e70 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20 20 2f VDBEFUNC (-7) /
61e80 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
61e90 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e 63 20 r to a VdbeFunc
61ea0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 structure */.#de
61eb0 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20 20 fine P4_MEM
61ec0 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69 73 20 (-8) /* P4 is
61ed0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d a pointer to a M
61ee0 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75 72 65 em* structure
61ef0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 54 */.#define P4_T
61f00 52 41 4e 53 49 45 4e 54 20 20 30 20 20 20 2f 2a RANSIENT 0 /*
61f10 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
61f20 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20 to a transient
61f30 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e string */.#defin
61f40 65 20 50 34 5f 56 54 41 42 20 20 20 20 20 28 2d e P4_VTAB (-
61f50 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61 20 70 10) /* P4 is a p
61f60 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c ointer to an sql
61f70 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 ite3_vtab struct
61f80 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ure */.#define P
61f90 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31 31 29 4_MPRINTF (-11)
61fa0 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74 72 69 /* P4 is a stri
61fb0 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ng obtained from
61fc0 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 sqlite3_mprintf
61fd0 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 () */.#define P4
61fe0 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32 29 20 _REAL (-12)
61ff0 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62 69 /* P4 is a 64-bi
62000 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 t floating point
62010 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e value */.#defin
62020 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20 28 2d e P4_INT64 (-
62030 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61 20 36 13) /* P4 is a 6
62040 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 4-bit signed int
62050 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 eger */.#define
62060 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d 31 34 P4_INT32 (-14
62070 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33 32 2d ) /* P4 is a 32-
62080 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
62090 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 er */.#define P4
620a0 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 35 29 20 _INTARRAY (-15)
620b0 2f 2a 20 50 34 20 69 73 20 61 20 76 65 63 74 6f /* P4 is a vecto
620c0 72 20 6f 66 20 33 32 2d 62 69 74 20 69 6e 74 65 r of 32-bit inte
620d0 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 gers */.#define
620e0 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 20 28 P4_SUBPROGRAM (
620f0 2d 31 38 29 20 2f 2a 20 50 34 20 69 73 20 61 20 -18) /* P4 is a
62100 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 75 62 pointer to a Sub
62110 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 72 Program structur
62120 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f e */.#define P4_
62130 41 44 56 41 4e 43 45 20 20 28 2d 31 39 29 20 2f ADVANCE (-19) /
62140 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
62150 72 20 74 6f 20 42 74 72 65 65 4e 65 78 74 28 29 r to BtreeNext()
62160 20 6f 72 20 42 74 72 65 65 50 72 65 76 28 29 20 or BtreePrev()
62170 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69 */../* When addi
62180 6e 67 20 61 20 50 34 20 61 72 67 75 6d 65 6e 74 ng a P4 argument
62190 20 75 73 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46 using P4_KEYINF
621a0 4f 2c 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 O, a copy of the
621b0 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 KeyInfo structu
621c0 72 65 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20 re.** is made.
621d0 54 68 61 74 20 63 6f 70 79 20 69 73 20 66 72 65 That copy is fre
621e0 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 ed when the Vdbe
621f0 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 is finalized.
62200 42 75 74 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 But if the.** ar
62210 67 75 6d 65 6e 74 20 69 73 20 50 34 5f 4b 45 59 gument is P4_KEY
62220 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68 INFO_HANDOFF, th
62230 65 20 70 61 73 73 65 64 20 69 6e 20 70 6f 69 6e e passed in poin
62240 74 65 72 20 69 73 20 75 73 65 64 2e 20 20 49 74 ter is used. It
62250 20 73 74 69 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 still.** gets f
62260 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 reed when the Vd
62270 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 be is finalized
62280 73 6f 20 69 74 20 73 74 69 6c 6c 20 73 68 6f 75 so it still shou
62290 6c 64 20 62 65 20 6f 62 74 61 69 6e 65 64 0a 2a ld be obtained.*
622a0 2a 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 * from a single
622b0 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 sqliteMalloc().
622c0 20 42 75 74 20 6e 6f 20 63 6f 70 79 20 69 73 20 But no copy is
622d0 6d 61 64 65 20 61 6e 64 20 74 68 65 20 63 61 6c made and the cal
622e0 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e ling.** function
622f0 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 should *not* tr
62300 79 20 74 6f 20 66 72 65 65 20 74 68 65 20 4b 65 y to free the Ke
62310 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e yInfo..*/.#defin
62320 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e e P4_KEYINFO_HAN
62330 44 4f 46 46 20 28 2d 31 36 29 0a 23 64 65 66 69 DOFF (-16).#defi
62340 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 ne P4_KEYINFO_ST
62350 41 54 49 43 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a ATIC (-17)../*.
62360 2a 2a 20 54 68 65 20 56 64 62 65 2e 61 43 6f 6c ** The Vdbe.aCol
62370 4e 61 6d 65 20 61 72 72 61 79 20 63 6f 6e 74 61 Name array conta
62380 69 6e 73 20 35 6e 20 4d 65 6d 20 73 74 72 75 63 ins 5n Mem struc
62390 74 75 72 65 73 2c 20 77 68 65 72 65 20 6e 20 69 tures, where n i
623a0 73 20 74 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 s the .** number
623b0 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 of columns of d
623c0 61 74 61 20 72 65 74 75 72 6e 65 64 20 62 79 20 ata returned by
623d0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a the statement..*
623e0 2f 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d /.#define COLNAM
623f0 45 5f 4e 41 4d 45 20 20 20 20 20 30 0a 23 64 65 E_NAME 0.#de
62400 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 fine COLNAME_DEC
62410 4c 54 59 50 45 20 31 0a 23 64 65 66 69 6e 65 20 LTYPE 1.#define
62420 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 COLNAME_DATABASE
62430 20 32 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 2.#define COLNA
62440 4d 45 5f 54 41 42 4c 45 20 20 20 20 33 0a 23 64 ME_TABLE 3.#d
62450 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f efine COLNAME_CO
62460 4c 55 4d 4e 20 20 20 34 0a 23 69 66 64 65 66 20 LUMN 4.#ifdef
62470 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f SQLITE_ENABLE_CO
62480 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 LUMN_METADATA.#
62490 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e define COLNAME_N
624a0 20 20 20 20 20 20 20 20 35 20 20 20 20 20 20 2f 5 /
624b0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 43 4f 4c 4e * Number of COLN
624c0 41 4d 45 5f 78 78 78 20 73 79 6d 62 6f 6c 73 20 AME_xxx symbols
624d0 2a 2f 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 */.#else.# ifdef
624e0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 SQLITE_OMIT_DEC
624f0 4c 54 59 50 45 0a 23 20 20 20 64 65 66 69 6e 65 LTYPE.# define
62500 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 COLNAME_N
62510 31 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 1 /* Store
62520 6f 6e 6c 79 20 74 68 65 20 6e 61 6d 65 20 2a 2f only the name */
62530 0a 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 .# else.# defi
62540 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 ne COLNAME_N
62550 20 20 32 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 2 /* Stor
62560 65 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 64 e the name and d
62570 65 63 6c 74 79 70 65 20 2a 2f 0a 23 20 65 6e 64 ecltype */.# end
62580 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a if.#endif../*.**
62590 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d The following m
625a0 61 63 72 6f 20 63 6f 6e 76 65 72 74 73 20 61 20 acro converts a
625b0 72 65 6c 61 74 69 76 65 20 61 64 64 72 65 73 73 relative address
625c0 20 69 6e 20 74 68 65 20 70 32 20 66 69 65 6c 64 in the p2 field
625d0 0a 2a 2a 20 6f 66 20 61 20 56 64 62 65 4f 70 20 .** of a VdbeOp
625e0 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 61 structure into a
625f0 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 negative number
62600 20 73 6f 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c so that .** sql
62610 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 ite3VdbeAddOpLis
62620 74 28 29 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 t() knows that t
62630 68 65 20 61 64 64 72 65 73 73 20 69 73 20 72 65 he address is re
62640 6c 61 74 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 lative. Calling
62650 0a 2a 2a 20 74 68 65 20 6d 61 63 72 6f 20 61 67 .** the macro ag
62660 61 69 6e 20 72 65 73 74 6f 72 65 73 20 74 68 65 ain restores the
62670 20 61 64 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 address..*/.#de
62680 66 69 6e 65 20 41 44 44 52 28 58 29 20 20 28 2d fine ADDR(X) (-
62690 31 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 1-(X))../*.** Th
626a0 65 20 6d 61 6b 65 66 69 6c 65 20 73 63 61 6e 73 e makefile scans
626b0 20 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 the vdbe.c sour
626c0 63 65 20 66 69 6c 65 20 61 6e 64 20 63 72 65 61 ce file and crea
626d0 74 65 73 20 74 68 65 20 22 6f 70 63 6f 64 65 73 tes the "opcodes
626e0 2e 68 22 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 .h".** header fi
626f0 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 le that defines
62700 61 20 6e 75 6d 62 65 72 20 66 6f 72 20 65 61 63 a number for eac
62710 68 20 6f 70 63 6f 64 65 20 75 73 65 64 20 62 79 h opcode used by
62720 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a the VDBE..*/./*
62730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
62740 63 6c 75 64 65 20 6f 70 63 6f 64 65 73 2e 68 20 clude opcodes.h
62750 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
62760 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a vdbe.h ********
62770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
62780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
62790 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 gin file opcodes
627a0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
627b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
627c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
627d0 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 Automatically g
627e0 65 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f enerated. Do no
627f0 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 t edit */./* See
62800 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 the mkopcodeh.a
62810 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 wk script for de
62820 74 61 69 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65 tails */.#define
62830 20 4f 50 5f 47 6f 74 6f 20 20 20 20 20 20 20 20 OP_Goto
62840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62850 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 1.#defi
62860 6e 65 20 4f 50 5f 47 6f 73 75 62 20 20 20 20 20 ne OP_Gosub
62870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62880 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 2.#de
62890 66 69 6e 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 fine OP_Return
628a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
628b0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 3.#
628c0 64 65 66 69 6e 65 20 4f 50 5f 59 69 65 6c 64 20 define OP_Yield
628d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
628e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
628f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 .#define OP_Halt
62900 49 66 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 IfNull
62910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62920 20 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 5.#define OP_Ha
62930 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 lt
62940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62950 20 20 20 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 6.#define OP_
62960 49 6e 74 65 67 65 72 20 20 20 20 20 20 20 20 20 Integer
62970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62980 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 4f 7.#define O
62990 50 5f 49 6e 74 36 34 20 20 20 20 20 20 20 20 20 P_Int64
629a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
629b0 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 8.#define
629c0 20 4f 50 5f 52 65 61 6c 20 20 20 20 20 20 20 20 OP_Real
629d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
629e0 20 20 20 20 20 20 20 31 33 30 20 20 20 2f 2a 20 130 /*
629f0 73 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 same as TK_FLOAT
62a00 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f */.#define O
62a10 50 5f 53 74 72 69 6e 67 38 20 20 20 20 20 20 20 P_String8
62a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62a30 20 20 20 20 20 20 39 34 20 20 20 2f 2a 20 73 61 94 /* sa
62a40 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 20 me as TK_STRING
62a50 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
62a60 53 74 72 69 6e 67 20 20 20 20 20 20 20 20 20 20 String
62a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62a80 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 4f 9.#define O
62a90 50 5f 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 P_Null
62aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62ab0 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65 10.#define
62ac0 20 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20 OP_Blob
62ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62ae0 20 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 11.#defi
62af0 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 20 ne OP_Variable
62b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62b10 20 20 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 12.#de
62b20 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 20 20 20 20 fine OP_Move
62b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62b40 20 20 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 13.#
62b50 64 65 66 69 6e 65 20 4f 50 5f 43 6f 70 79 20 20 define OP_Copy
62b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
62b80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 43 6f 70 .#define OP_SCop
62b90 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
62ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62bb0 31 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 15.#define OP_Re
62bc0 73 75 6c 74 52 6f 77 20 20 20 20 20 20 20 20 20 sultRow
62bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62be0 20 20 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 16.#define OP_
62bf0 43 6f 6e 63 61 74 20 20 20 20 20 20 20 20 20 20 Concat
62c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62c10 20 20 20 20 39 31 20 20 20 2f 2a 20 73 61 6d 65 91 /* same
62c20 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 as TK_CONCAT
62c30 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 */.#define OP_Ad
62c40 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
62c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62c60 20 20 38 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 86 /* same a
62c70 73 20 54 4b 5f 50 4c 55 53 20 20 20 20 20 2a 2f s TK_PLUS */
62c80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 75 62 74 .#define OP_Subt
62c90 72 61 63 74 20 20 20 20 20 20 20 20 20 20 20 20 ract
62ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62cb0 38 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 87 /* same as
62cc0 54 4b 5f 4d 49 4e 55 53 20 20 20 20 2a 2f 0a 23 TK_MINUS */.#
62cd0 64 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 define OP_Multip
62ce0 6c 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ly
62cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38 88
62d00 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
62d10 5f 53 54 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 _STAR */.#de
62d20 66 69 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20 fine OP_Divide
62d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62d40 20 20 20 20 20 20 20 20 20 20 20 20 38 39 20 20 89
62d50 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 /* same as TK_S
62d60 4c 41 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69 LASH */.#defi
62d70 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 ne OP_Remainder
62d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62d90 20 20 20 20 20 20 20 20 20 20 39 30 20 20 20 2f 90 /
62da0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d * same as TK_REM
62db0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
62dc0 20 4f 50 5f 43 6f 6c 6c 53 65 71 20 20 20 20 20 OP_CollSeq
62dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62de0 20 20 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 17.#defi
62df0 6e 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20 ne OP_Function
62e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62e10 20 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65 18.#de
62e20 66 69 6e 65 20 4f 50 5f 42 69 74 41 6e 64 20 20 fine OP_BitAnd
62e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62e40 20 20 20 20 20 20 20 20 20 20 20 20 38 32 20 20 82
62e50 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 /* same as TK_B
62e60 49 54 41 4e 44 20 20 20 2a 2f 0a 23 64 65 66 69 ITAND */.#defi
62e70 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 20 20 ne OP_BitOr
62e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62e90 20 20 20 20 20 20 20 20 20 20 38 33 20 20 20 2f 83 /
62ea0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 * same as TK_BIT
62eb0 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 OR */.#define
62ec0 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 20 20 OP_ShiftLeft
62ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62ee0 20 20 20 20 20 20 20 20 38 34 20 20 20 2f 2a 20 84 /*
62ef0 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46 same as TK_LSHIF
62f00 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f T */.#define O
62f10 50 5f 53 68 69 66 74 52 69 67 68 74 20 20 20 20 P_ShiftRight
62f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62f30 20 20 20 20 20 20 38 35 20 20 20 2f 2a 20 73 61 85 /* sa
62f40 6d 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 20 me as TK_RSHIFT
62f50 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
62f60 41 64 64 49 6d 6d 20 20 20 20 20 20 20 20 20 20 AddImm
62f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62f80 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 20.#define O
62f90 50 5f 4d 75 73 74 42 65 49 6e 74 20 20 20 20 20 P_MustBeInt
62fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62fb0 20 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 21.#define
62fc0 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 OP_RealAffinity
62fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62fe0 20 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69 22.#defi
62ff0 6e 65 20 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 ne OP_ToText
63000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63010 20 20 20 20 20 20 20 20 20 31 34 31 20 20 20 2f 141 /
63020 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f * same as TK_TO_
63030 54 45 58 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65 TEXT */.#define
63040 20 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20 OP_ToBlob
63050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63060 20 20 20 20 20 20 20 31 34 32 20 20 20 2f 2a 20 142 /*
63070 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c same as TK_TO_BL
63080 4f 42 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f OB */.#define O
63090 50 5f 54 6f 4e 75 6d 65 72 69 63 20 20 20 20 20 P_ToNumeric
630a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
630b0 20 20 20 20 20 31 34 33 20 20 20 2f 2a 20 73 61 143 /* sa
630c0 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 me as TK_TO_NUME
630d0 52 49 43 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 RIC*/.#define OP
630e0 5f 54 6f 49 6e 74 20 20 20 20 20 20 20 20 20 20 _ToInt
630f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63100 20 20 20 20 31 34 34 20 20 20 2f 2a 20 73 61 6d 144 /* sam
63110 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 e as TK_TO_INT
63120 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 */.#define OP_T
63130 6f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20 oReal
63140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63150 20 20 31 34 35 20 20 20 2f 2a 20 73 61 6d 65 20 145 /* same
63160 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 2a as TK_TO_REAL *
63170 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 71 20 /.#define OP_Eq
63180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
631a0 20 37 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 76 /* same as
631b0 20 54 4b 5f 45 51 20 20 20 20 20 20 20 2a 2f 0a TK_EQ */.
631c0 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20 #define OP_Ne
631d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
631e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
631f0 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 5 /* same as T
63200 4b 5f 4e 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 K_NE */.#d
63210 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20 20 efine OP_Lt
63220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63230 20 20 20 20 20 20 20 20 20 20 20 20 20 37 39 20 79
63240 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
63250 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 LT */.#def
63260 69 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20 20 ine OP_Le
63270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63280 20 20 20 20 20 20 20 20 20 20 20 37 38 20 20 20 78
63290 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 /* same as TK_LE
632a0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
632b0 65 20 4f 50 5f 47 74 20 20 20 20 20 20 20 20 20 e OP_Gt
632c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
632d0 20 20 20 20 20 20 20 20 20 37 37 20 20 20 2f 2a 77 /*
632e0 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 20 20 same as TK_GT
632f0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
63300 4f 50 5f 47 65 20 20 20 20 20 20 20 20 20 20 20 OP_Ge
63310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63320 20 20 20 20 20 20 20 38 30 20 20 20 2f 2a 20 73 80 /* s
63330 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 20 20 20 ame as TK_GE
63340 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
63350 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 20 20 20 _Permutation
63360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63370 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e 65 20 23.#define
63380 4f 50 5f 43 6f 6d 70 61 72 65 20 20 20 20 20 20 OP_Compare
63390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
633a0 20 20 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 24.#defin
633b0 65 20 4f 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 e OP_Jump
633c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
633d0 20 20 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 25.#def
633e0 69 6e 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20 ine OP_And
633f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63400 20 20 20 20 20 20 20 20 20 20 20 36 39 20 20 20 69
63410 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e /* same as TK_AN
63420 44 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e D */.#defin
63430 65 20 4f 50 5f 4f 72 20 20 20 20 20 20 20 20 20 e OP_Or
63440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63450 20 20 20 20 20 20 20 20 20 36 38 20 20 20 2f 2a 68 /*
63460 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 20 20 same as TK_OR
63470 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
63480 4f 50 5f 4e 6f 74 20 20 20 20 20 20 20 20 20 20 OP_Not
63490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
634a0 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 73 19 /* s
634b0 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 20 20 20 ame as TK_NOT
634c0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
634d0 5f 42 69 74 4e 6f 74 20 20 20 20 20 20 20 20 20 _BitNot
634e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
634f0 20 20 20 20 20 39 33 20 20 20 2f 2a 20 73 61 6d 93 /* sam
63500 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 20 20 e as TK_BITNOT
63510 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f */.#define OP_O
63520 6e 63 65 20 20 20 20 20 20 20 20 20 20 20 20 20 nce
63530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63540 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 26.#define OP
63550 5f 49 66 20 20 20 20 20 20 20 20 20 20 20 20 20 _If
63560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63570 20 20 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20 27.#define
63580 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 20 20 20 OP_IfNot
63590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
635a0 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e 28.#defin
635b0 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 20 20 e OP_IsNull
635c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
635d0 20 20 20 20 20 20 20 20 20 37 33 20 20 20 2f 2a 73 /*
635e0 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 same as TK_ISNU
635f0 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 LL */.#define
63600 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20 20 20 OP_NotNull
63610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63620 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a 20 73 74 /* s
63630 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c ame as TK_NOTNUL
63640 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 L */.#define OP
63650 5f 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 20 20 _Column
63660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63670 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65 20 29.#define
63680 4f 50 5f 41 66 66 69 6e 69 74 79 20 20 20 20 20 OP_Affinity
63690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
636a0 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e 30.#defin
636b0 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 e OP_MakeRecord
636c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
636d0 20 20 20 20 20 20 20 20 20 33 31 0a 23 64 65 66 31.#def
636e0 69 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 20 20 20 ine OP_Count
636f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63700 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 23 64 32.#d
63710 65 66 69 6e 65 20 4f 50 5f 53 61 76 65 70 6f 69 efine OP_Savepoi
63720 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nt
63730 20 20 20 20 20 20 20 20 20 20 20 20 20 33 33 0a 33.
63740 23 64 65 66 69 6e 65 20 4f 50 5f 41 75 74 6f 43 #define OP_AutoC
63750 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 20 20 ommit
63760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
63770 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 72 61 4.#define OP_Tra
63780 6e 73 61 63 74 69 6f 6e 20 20 20 20 20 20 20 20 nsaction
63790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
637a0 20 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 35.#define OP_R
637b0 65 61 64 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 eadCookie
637c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
637d0 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 4f 50 36.#define OP
637e0 5f 53 65 74 43 6f 6f 6b 69 65 20 20 20 20 20 20 _SetCookie
637f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63800 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65 20 37.#define
63810 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 OP_VerifyCookie
63820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63830 20 20 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e 38.#defin
63840 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 e OP_OpenRead
63850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63860 20 20 20 20 20 20 20 20 20 33 39 0a 23 64 65 66 39.#def
63870 69 6e 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 ine OP_OpenWrite
63880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63890 20 20 20 20 20 20 20 20 20 20 20 34 30 0a 23 64 40.#d
638a0 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 41 75 74 efine OP_OpenAut
638b0 6f 69 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 oindex
638c0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 31 0a 41.
638d0 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 45 #define OP_OpenE
638e0 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 20 phemeral
638f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
63900 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 2.#define OP_Sor
63910 74 65 72 4f 70 65 6e 20 20 20 20 20 20 20 20 20 terOpen
63920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63930 20 34 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 43.#define OP_O
63940 70 65 6e 50 73 65 75 64 6f 20 20 20 20 20 20 20 penPseudo
63950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63960 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20 4f 50 44.#define OP
63970 5f 43 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 _Close
63980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63990 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65 20 45.#define
639a0 4f 50 5f 53 65 65 6b 4c 74 20 20 20 20 20 20 20 OP_SeekLt
639b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
639c0 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 46.#defin
639d0 65 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 e OP_SeekLe
639e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
639f0 20 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 66 47.#def
63a00 69 6e 65 20 4f 50 5f 53 65 65 6b 47 65 20 20 20 ine OP_SeekGe
63a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63a20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a 23 64 48.#d
63a30 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 74 20 efine OP_SeekGt
63a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63a50 20 20 20 20 20 20 20 20 20 20 20 20 20 34 39 0a 49.
63a60 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 20 #define OP_Seek
63a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
63a90 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 0.#define OP_Not
63aa0 46 6f 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 Found
63ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63ac0 20 35 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 51.#define OP_F
63ad0 6f 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 ound
63ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63af0 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20 4f 50 52.#define OP
63b00 5f 49 73 55 6e 69 71 75 65 20 20 20 20 20 20 20 _IsUnique
63b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63b20 20 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 53.#define
63b30 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20 OP_NotExists
63b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63b50 20 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 54.#defin
63b60 65 20 4f 50 5f 53 65 71 75 65 6e 63 65 20 20 20 e OP_Sequence
63b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63b80 20 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 55.#def
63b90 69 6e 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 ine OP_NewRowid
63ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63bb0 20 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 56.#d
63bc0 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 20 efine OP_Insert
63bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63be0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 37 0a 57.
63bf0 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 #define OP_Inser
63c00 74 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 tInt
63c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
63c20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 6c 8.#define OP_Del
63c30 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ete
63c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63c50 20 35 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 59.#define OP_R
63c60 65 73 65 74 43 6f 75 6e 74 20 20 20 20 20 20 20 esetCount
63c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63c80 20 20 20 36 30 0a 23 64 65 66 69 6e 65 20 4f 50 60.#define OP
63c90 5f 53 6f 72 74 65 72 43 6f 6d 70 61 72 65 20 20 _SorterCompare
63ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63cb0 20 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65 20 61.#define
63cc0 4f 50 5f 53 6f 72 74 65 72 44 61 74 61 20 20 20 OP_SorterData
63cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63ce0 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e 62.#defin
63cf0 65 20 4f 50 5f 52 6f 77 4b 65 79 20 20 20 20 20 e OP_RowKey
63d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63d10 20 20 20 20 20 20 20 20 20 36 33 0a 23 64 65 66 63.#def
63d20 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74 61 20 20 ine OP_RowData
63d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63d40 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 23 64 64.#d
63d50 65 66 69 6e 65 20 4f 50 5f 52 6f 77 69 64 20 20 efine OP_Rowid
63d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63d70 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35 0a 65.
63d80 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 6c 6c 52 #define OP_NullR
63d90 6f 77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ow
63da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
63db0 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 61 73 6.#define OP_Las
63dc0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
63dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63de0 20 36 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 67.#define OP_S
63df0 6f 72 74 65 72 53 6f 72 74 20 20 20 20 20 20 20 orterSort
63e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63e10 20 20 20 37 30 0a 23 64 65 66 69 6e 65 20 4f 50 70.#define OP
63e20 5f 53 6f 72 74 20 20 20 20 20 20 20 20 20 20 20 _Sort
63e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63e40 20 20 20 20 20 37 31 0a 23 64 65 66 69 6e 65 20 71.#define
63e50 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 20 20 20 OP_Rewind
63e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63e70 20 20 20 20 20 20 20 37 32 0a 23 64 65 66 69 6e 72.#defin
63e80 65 20 4f 50 5f 53 6f 72 74 65 72 4e 65 78 74 20 e OP_SorterNext
63e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63ea0 20 20 20 20 20 20 20 20 20 38 31 0a 23 64 65 66 81.#def
63eb0 69 6e 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20 ine OP_Prev
63ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63ed0 20 20 20 20 20 20 20 20 20 20 20 39 32 0a 23 64 92.#d
63ee0 65 66 69 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20 efine OP_Next
63ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63f00 20 20 20 20 20 20 20 20 20 20 20 20 20 39 35 0a 95.
63f10 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 65 #define OP_Sorte
63f20 72 49 6e 73 65 72 74 20 20 20 20 20 20 20 20 20 rInsert
63f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
63f40 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 6.#define OP_Idx
63f50 49 6e 73 65 72 74 20 20 20 20 20 20 20 20 20 20 Insert
63f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63f70 20 39 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 97.#define OP_I
63f80 64 78 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 dxDelete
63f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63fa0 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 4f 50 98.#define OP
63fb0 5f 49 64 78 52 6f 77 69 64 20 20 20 20 20 20 20 _IdxRowid
63fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63fd0 20 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 99.#define
63fe0 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20 OP_IdxLT
63ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64000 20 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 100.#defin
64010 65 20 4f 50 5f 49 64 78 47 45 20 20 20 20 20 20 e OP_IdxGE
64020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64030 20 20 20 20 20 20 20 20 31 30 31 0a 23 64 65 66 101.#def
64040 69 6e 65 20 4f 50 5f 44 65 73 74 72 6f 79 20 20 ine OP_Destroy
64050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64060 20 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 102.#d
64070 65 66 69 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20 efine OP_Clear
64080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64090 20 20 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 103.
640a0 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 74 #define OP_Creat
640b0 65 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 eIndex
640c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
640d0 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 4.#define OP_Cre
640e0 61 74 65 54 61 62 6c 65 20 20 20 20 20 20 20 20 ateTable
640f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64100 31 30 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 105.#define OP_P
64110 61 72 73 65 53 63 68 65 6d 61 20 20 20 20 20 20 arseSchema
64120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64130 20 20 31 30 36 0a 23 64 65 66 69 6e 65 20 4f 50 106.#define OP
64140 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 20 20 _LoadAnalysis
64150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64160 20 20 20 20 31 30 37 0a 23 64 65 66 69 6e 65 20 107.#define
64170 4f 50 5f 44 72 6f 70 54 61 62 6c 65 20 20 20 20 OP_DropTable
64180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64190 20 20 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e 108.#defin
641a0 65 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 20 20 e OP_DropIndex
641b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
641c0 20 20 20 20 20 20 20 20 31 30 39 0a 23 64 65 66 109.#def
641d0 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 ine OP_DropTrigg
641e0 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 er
641f0 20 20 20 20 20 20 20 20 20 20 31 31 30 0a 23 64 110.#d
64200 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69 efine OP_Integri
64210 74 79 43 6b 20 20 20 20 20 20 20 20 20 20 20 20 tyCk
64220 20 20 20 20 20 20 20 20 20 20 20 20 31 31 31 0a 111.
64230 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65 #define OP_RowSe
64240 74 41 64 64 20 20 20 20 20 20 20 20 20 20 20 20 tAdd
64250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
64260 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 2.#define OP_Row
64270 53 65 74 52 65 61 64 20 20 20 20 20 20 20 20 20 SetRead
64280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64290 31 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 113.#define OP_R
642a0 6f 77 53 65 74 54 65 73 74 20 20 20 20 20 20 20 owSetTest
642b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
642c0 20 20 31 31 34 0a 23 64 65 66 69 6e 65 20 4f 50 114.#define OP
642d0 5f 50 72 6f 67 72 61 6d 20 20 20 20 20 20 20 20 _Program
642e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
642f0 20 20 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20 115.#define
64300 4f 50 5f 50 61 72 61 6d 20 20 20 20 20 20 20 20 OP_Param
64310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64320 20 20 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e 116.#defin
64330 65 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 20 20 e OP_FkCounter
64340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64350 20 20 20 20 20 20 20 20 31 31 37 0a 23 64 65 66 117.#def
64360 69 6e 65 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 20 ine OP_FkIfZero
64370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64380 20 20 20 20 20 20 20 20 20 20 31 31 38 0a 23 64 118.#d
64390 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 78 20 efine OP_MemMax
643a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
643b0 20 20 20 20 20 20 20 20 20 20 20 20 31 31 39 0a 119.
643c0 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 50 6f 73 #define OP_IfPos
643d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
643e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
643f0 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 4e 0.#define OP_IfN
64400 65 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 eg
64410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64420 31 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 121.#define OP_I
64430 66 5a 65 72 6f 20 20 20 20 20 20 20 20 20 20 20 fZero
64440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64450 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20 4f 50 122.#define OP
64460 5f 41 67 67 53 74 65 70 20 20 20 20 20 20 20 20 _AggStep
64470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64480 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20 123.#define
64490 4f 50 5f 41 67 67 46 69 6e 61 6c 20 20 20 20 20 OP_AggFinal
644a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
644b0 20 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 124.#defin
644c0 65 20 4f 50 5f 43 68 65 63 6b 70 6f 69 6e 74 20 e OP_Checkpoint
644d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
644e0 20 20 20 20 20 20 20 20 31 32 35 0a 23 64 65 66 125.#def
644f0 69 6e 65 20 4f 50 5f 4a 6f 75 72 6e 61 6c 4d 6f ine OP_JournalMo
64500 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 de
64510 20 20 20 20 20 20 20 20 20 20 31 32 36 0a 23 64 126.#d
64520 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d 20 efine OP_Vacuum
64530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64540 20 20 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 127.
64550 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 63 72 56 #define OP_IncrV
64560 61 63 75 75 6d 20 20 20 20 20 20 20 20 20 20 20 acuum
64570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
64580 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 8.#define OP_Exp
64590 69 72 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ire
645a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
645b0 31 32 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 129.#define OP_T
645c0 61 62 6c 65 4c 6f 63 6b 20 20 20 20 20 20 20 20 ableLock
645d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
645e0 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 131.#define OP
645f0 5f 56 42 65 67 69 6e 20 20 20 20 20 20 20 20 20 _VBegin
64600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64610 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20 132.#define
64620 4f 50 5f 56 43 72 65 61 74 65 20 20 20 20 20 20 OP_VCreate
64630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64640 20 20 20 20 20 20 31 33 33 0a 23 64 65 66 69 6e 133.#defin
64650 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20 20 e OP_VDestroy
64660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64670 20 20 20 20 20 20 20 20 31 33 34 0a 23 64 65 66 134.#def
64680 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 20 20 ine OP_VOpen
64690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
646a0 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 64 135.#d
646b0 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c 74 65 72 efine OP_VFilter
646c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
646d0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36 0a 136.
646e0 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 6f 6c 75 #define OP_VColu
646f0 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 mn
64700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 13
64710 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 4e 65 7.#define OP_VNe
64720 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 xt
64730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64740 31 33 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 138.#define OP_V
64750 52 65 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 Rename
64760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64770 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 4f 50 139.#define OP
64780 5f 56 55 70 64 61 74 65 20 20 20 20 20 20 20 20 _VUpdate
64790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
647a0 20 20 20 20 31 34 30 0a 23 64 65 66 69 6e 65 20 140.#define
647b0 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 20 20 20 OP_Pagecount
647c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
647d0 20 20 20 20 20 20 31 34 36 0a 23 64 65 66 69 6e 146.#defin
647e0 65 20 4f 50 5f 4d 61 78 50 67 63 6e 74 20 20 20 e OP_MaxPgcnt
647f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64800 20 20 20 20 20 20 20 20 31 34 37 0a 23 64 65 66 147.#def
64810 69 6e 65 20 4f 50 5f 54 72 61 63 65 20 20 20 20 ine OP_Trace
64820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64830 20 20 20 20 20 20 20 20 20 20 31 34 38 0a 23 64 148.#d
64840 65 66 69 6e 65 20 4f 50 5f 4e 6f 6f 70 20 20 20 efine OP_Noop
64850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64860 20 20 20 20 20 20 20 20 20 20 20 20 31 34 39 0a 149.
64870 23 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 #define OP_Expla
64880 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 in
64890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 15
648a0 30 0a 0a 0a 2f 2a 20 50 72 6f 70 65 72 74 69 65 0.../* Propertie
648b0 73 20 73 75 63 68 20 61 73 20 22 6f 75 74 32 22 s such as "out2"
648c0 20 6f 72 20 22 6a 75 6d 70 22 20 74 68 61 74 20 or "jump" that
648d0 61 72 65 20 73 70 65 63 69 66 69 65 64 20 69 6e are specified in
648e0 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c .** comments fol
648f0 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73 65 lowing the "case
64900 22 20 66 6f 72 20 65 61 63 68 20 6f 70 63 6f 64 " for each opcod
64910 65 20 69 6e 20 74 68 65 20 76 64 62 65 2e 63 0a e in the vdbe.c.
64920 2a 2a 20 61 72 65 20 65 6e 63 6f 64 65 64 20 69 ** are encoded i
64930 6e 74 6f 20 62 69 74 76 65 63 74 6f 72 73 20 61 nto bitvectors a
64940 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 s follows:.*/.#d
64950 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4a 55 4d 50 efine OPFLG_JUMP
64960 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 0x00
64970 30 31 20 20 2f 2a 20 6a 75 6d 70 3a 20 20 50 32 01 /* jump: P2
64980 20 68 6f 6c 64 73 20 6a 6d 70 20 74 61 72 67 65 holds jmp targe
64990 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 t */.#define OPF
649a0 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 LG_OUT2_PRERELEA
649b0 53 45 20 30 78 30 30 30 32 20 20 2f 2a 20 6f 75 SE 0x0002 /* ou
649c0 74 32 2d 70 72 65 72 65 6c 65 61 73 65 3a 20 2a t2-prerelease: *
649d0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f /.#define OPFLG_
649e0 49 4e 31 20 20 20 20 20 20 20 20 20 20 20 20 20 IN1
649f0 30 78 30 30 30 34 20 20 2f 2a 20 69 6e 31 3a 20 0x0004 /* in1:
64a00 20 20 50 31 20 69 73 20 61 6e 20 69 6e 70 75 74 P1 is an input
64a10 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c */.#define OPFL
64a20 47 5f 49 4e 32 20 20 20 20 20 20 20 20 20 20 20 G_IN2
64a30 20 20 30 78 30 30 30 38 20 20 2f 2a 20 69 6e 32 0x0008 /* in2
64a40 3a 20 20 20 50 32 20 69 73 20 61 6e 20 69 6e 70 : P2 is an inp
64a50 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 ut */.#define OP
64a60 46 4c 47 5f 49 4e 33 20 20 20 20 20 20 20 20 20 FLG_IN3
64a70 20 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 69 0x0010 /* i
64a80 6e 33 3a 20 20 20 50 33 20 69 73 20 61 6e 20 69 n3: P3 is an i
64a90 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nput */.#define
64aa0 4f 50 46 4c 47 5f 4f 55 54 32 20 20 20 20 20 20 OPFLG_OUT2
64ab0 20 20 20 20 20 20 30 78 30 30 32 30 20 20 2f 2a 0x0020 /*
64ac0 20 6f 75 74 32 3a 20 20 50 32 20 69 73 20 61 6e out2: P2 is an
64ad0 20 6f 75 74 70 75 74 20 2a 2f 0a 23 64 65 66 69 output */.#defi
64ae0 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 33 20 20 20 ne OPFLG_OUT3
64af0 20 20 20 20 20 20 20 20 20 30 78 30 30 34 30 20 0x0040
64b00 20 2f 2a 20 6f 75 74 33 3a 20 20 50 33 20 69 73 /* out3: P3 is
64b10 20 61 6e 20 6f 75 74 70 75 74 20 2a 2f 0a 23 64 an output */.#d
64b20 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 49 54 efine OPFLG_INIT
64b30 49 41 4c 49 5a 45 52 20 7b 5c 0a 2f 2a 20 20 20 IALIZER {\./*
64b40 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 0 */ 0x00, 0x01,
64b50 20 30 78 30 31 2c 20 30 78 30 34 2c 20 30 78 30 0x01, 0x04, 0x0
64b60 34 2c 20 30 78 31 30 2c 20 30 78 30 30 2c 20 30 4, 0x10, 0x00, 0
64b70 78 30 32 2c 5c 0a 2f 2a 20 20 20 38 20 2a 2f 20 x02,\./* 8 */
64b80 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
64b90 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
64ba0 30 30 2c 20 30 78 30 30 2c 20 30 78 32 34 2c 5c 00, 0x00, 0x24,\
64bb0 0a 2f 2a 20 20 31 36 20 2a 2f 20 30 78 30 30 2c ./* 16 */ 0x00,
64bc0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 32 0x00, 0x00, 0x2
64bd0 34 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 4, 0x04, 0x05, 0
64be0 78 30 34 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 x04, 0x00,\./*
64bf0 32 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 24 */ 0x00, 0x01
64c00 2c 20 30 78 30 31 2c 20 30 78 30 35 2c 20 30 78 , 0x01, 0x05, 0x
64c10 30 35 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 05, 0x00, 0x00,
64c20 30 78 30 30 2c 5c 0a 2f 2a 20 20 33 32 20 2a 2f 0x00,\./* 32 */
64c30 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 0x02, 0x00, 0x0
64c40 30 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 0, 0x00, 0x02, 0
64c50 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x10, 0x00, 0x00,
64c60 5c 0a 2f 2a 20 20 34 30 20 2a 2f 20 30 78 30 30 \./* 40 */ 0x00
64c70 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
64c80 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
64c90 30 78 31 31 2c 20 30 78 31 31 2c 5c 0a 2f 2a 20 0x11, 0x11,\./*
64ca0 20 34 38 20 2a 2f 20 30 78 31 31 2c 20 30 78 31 48 */ 0x11, 0x1
64cb0 31 2c 20 30 78 30 38 2c 20 30 78 31 31 2c 20 30 1, 0x08, 0x11, 0
64cc0 78 31 31 2c 20 30 78 31 31 2c 20 30 78 31 31 2c x11, 0x11, 0x11,
64cd0 20 30 78 30 32 2c 5c 0a 2f 2a 20 20 35 36 20 2a 0x02,\./* 56 *
64ce0 2f 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 / 0x02, 0x00, 0x
64cf0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
64d00 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
64d10 2c 5c 0a 2f 2a 20 20 36 34 20 2a 2f 20 30 78 30 ,\./* 64 */ 0x0
64d20 30 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 0, 0x02, 0x00, 0
64d30 78 30 31 2c 20 30 78 34 63 2c 20 30 78 34 63 2c x01, 0x4c, 0x4c,
64d40 20 30 78 30 31 2c 20 30 78 30 31 2c 5c 0a 2f 2a 0x01, 0x01,\./*
64d50 20 20 37 32 20 2a 2f 20 30 78 30 31 2c 20 30 78 72 */ 0x01, 0x
64d60 30 35 2c 20 30 78 30 35 2c 20 30 78 31 35 2c 20 05, 0x05, 0x15,
64d70 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 0x15, 0x15, 0x15
64d80 2c 20 30 78 31 35 2c 5c 0a 2f 2a 20 20 38 30 20 , 0x15,\./* 80
64d90 2a 2f 20 30 78 31 35 2c 20 30 78 30 31 2c 20 30 */ 0x15, 0x01, 0
64da0 78 34 63 2c 20 30 78 34 63 2c 20 30 78 34 63 2c x4c, 0x4c, 0x4c,
64db0 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 34 0x4c, 0x4c, 0x4
64dc0 63 2c 5c 0a 2f 2a 20 20 38 38 20 2a 2f 20 30 78 c,\./* 88 */ 0x
64dd0 34 63 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 20 4c, 0x4c, 0x4c,
64de0 30 78 34 63 2c 20 30 78 30 31 2c 20 30 78 32 34 0x4c, 0x01, 0x24
64df0 2c 20 30 78 30 32 2c 20 30 78 30 31 2c 5c 0a 2f , 0x02, 0x01,\./
64e00 2a 20 20 39 36 20 2a 2f 20 30 78 30 38 2c 20 30 * 96 */ 0x08, 0
64e10 78 30 38 2c 20 30 78 30 30 2c 20 30 78 30 32 2c x08, 0x00, 0x02,
64e20 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 0x01, 0x01, 0x0
64e30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31 30 34 2, 0x00,\./* 104
64e40 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 32 2c 20 */ 0x02, 0x02,
64e50 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
64e60 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
64e70 30 30 2c 5c 0a 2f 2a 20 31 31 32 20 2a 2f 20 30 00,\./* 112 */ 0
64e80 78 30 63 2c 20 30 78 34 35 2c 20 30 78 31 35 2c x0c, 0x45, 0x15,
64e90 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 0x01, 0x02, 0x0
64ea0 30 2c 20 30 78 30 31 2c 20 30 78 30 38 2c 5c 0a 0, 0x01, 0x08,\.
64eb0 2f 2a 20 31 32 30 20 2a 2f 20 30 78 30 35 2c 20 /* 120 */ 0x05,
64ec0 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 30 30 0x05, 0x05, 0x00
64ed0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
64ee0 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31 32 02, 0x00,\./* 12
64ef0 38 20 2a 2f 20 30 78 30 31 2c 20 30 78 30 30 2c 8 */ 0x01, 0x00,
64f00 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 0x02, 0x00, 0x0
64f10 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
64f20 78 30 30 2c 5c 0a 2f 2a 20 31 33 36 20 2a 2f 20 x00,\./* 136 */
64f30 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 31 0x01, 0x00, 0x01
64f40 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
64f50 30 34 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 5c 04, 0x04, 0x04,\
64f60 0a 2f 2a 20 31 34 34 20 2a 2f 20 30 78 30 34 2c ./* 144 */ 0x04,
64f70 20 30 78 30 34 2c 20 30 78 30 32 2c 20 30 78 30 0x04, 0x02, 0x0
64f80 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 2, 0x00, 0x00, 0
64f90 78 30 30 2c 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a x00,}../********
64fa0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 ****** End of op
64fb0 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a codes.h ********
64fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64fe0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
64ff0 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
65000 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
65010 6f 66 66 20 69 6e 20 76 64 62 65 2e 68 20 2a 2a off in vdbe.h **
65020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
65030 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 *****/../*.** Pr
65040 6f 74 6f 74 79 70 65 73 20 66 6f 72 20 74 68 65 ototypes for the
65050 20 56 44 42 45 20 69 6e 74 65 72 66 61 63 65 2e VDBE interface.
65060 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f See comments o
65070 6e 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 n the implementa
65080 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 20 64 65 tion.** for a de
65090 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 77 68 61 scription of wha
650a0 74 20 65 61 63 68 20 6f 66 20 74 68 65 73 65 20 t each of these
650b0 72 6f 75 74 69 6e 65 73 20 64 6f 65 73 2e 0a 2a routines does..*
650c0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
650d0 20 56 64 62 65 20 2a 73 71 6c 69 74 65 33 56 64 Vdbe *sqlite3Vd
650e0 62 65 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 beCreate(sqlite3
650f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
65100 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
65110 62 65 41 64 64 4f 70 30 28 56 64 62 65 2a 2c 69 beAddOp0(Vdbe*,i
65120 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
65130 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
65140 64 62 65 41 64 64 4f 70 31 28 56 64 62 65 2a 2c dbeAddOp1(Vdbe*,
65150 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
65160 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
65170 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 56 ite3VdbeAddOp2(V
65180 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 dbe*,int,int,int
65190 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
651a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
651b0 65 41 64 64 4f 70 33 28 56 64 62 65 2a 2c 69 6e eAddOp3(Vdbe*,in
651c0 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a t,int,int,int);.
651d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
651e0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
651f0 64 4f 70 34 28 56 64 62 65 2a 2c 69 6e 74 2c 69 dOp4(Vdbe*,int,i
65200 6e 74 2c 69 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 nt,int,int,const
65210 20 63 68 61 72 20 2a 7a 50 34 2c 69 6e 74 29 3b char *zP4,int);
65220 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
65230 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
65240 64 64 4f 70 34 49 6e 74 28 56 64 62 65 2a 2c 69 ddOp4Int(Vdbe*,i
65250 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 nt,int,int,int,i
65260 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
65270 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
65280 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 dbeAddOpList(Vdb
65290 65 2a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 e*, int nOp, Vdb
652a0 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 eOpList const *a
652b0 4f 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 Op);.SQLITE_PRIV
652c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
652d0 56 64 62 65 41 64 64 50 61 72 73 65 53 63 68 65 VdbeAddParseSche
652e0 6d 61 4f 70 28 56 64 62 65 2a 2c 69 6e 74 2c 63 maOp(Vdbe*,int,c
652f0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
65300 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
65310 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 56 e3VdbeChangeP1(V
65320 64 62 65 2a 2c 20 75 33 32 20 61 64 64 72 2c 20 dbe*, u32 addr,
65330 69 6e 74 20 50 31 29 3b 0a 53 51 4c 49 54 45 5f int P1);.SQLITE_
65340 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
65350 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 ite3VdbeChangeP2
65360 28 56 64 62 65 2a 2c 20 75 33 32 20 61 64 64 72 (Vdbe*, u32 addr
65370 2c 20 69 6e 74 20 50 32 29 3b 0a 53 51 4c 49 54 , int P2);.SQLIT
65380 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
65390 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
653a0 50 33 28 56 64 62 65 2a 2c 20 75 33 32 20 61 64 P3(Vdbe*, u32 ad
653b0 64 72 2c 20 69 6e 74 20 50 33 29 3b 0a 53 51 4c dr, int P3);.SQL
653c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
653d0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
653e0 67 65 50 35 28 56 64 62 65 2a 2c 20 75 38 20 50 geP5(Vdbe*, u8 P
653f0 35 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 5);.SQLITE_PRIVA
65400 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
65410 64 62 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65 dbeJumpHere(Vdbe
65420 2a 2c 20 69 6e 74 20 61 64 64 72 29 3b 0a 53 51 *, int addr);.SQ
65430 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
65440 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 d sqlite3VdbeCha
65450 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c ngeToNoop(Vdbe*,
65460 20 69 6e 74 20 61 64 64 72 29 3b 0a 53 51 4c 49 int addr);.SQLI
65470 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
65480 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
65490 65 50 34 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 eP4(Vdbe*, int a
654a0 64 64 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 ddr, const char
654b0 2a 7a 50 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 *zP4, int N);.SQ
654c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
654d0 64 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 d sqlite3VdbeUse
654e0 73 42 74 72 65 65 28 56 64 62 65 2a 2c 20 69 6e sBtree(Vdbe*, in
654f0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
65500 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 TE VdbeOp *sqlit
65510 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 e3VdbeGetOp(Vdbe
65520 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
65530 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
65540 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c te3VdbeMakeLabel
65550 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f (Vdbe*);.SQLITE_
65560 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
65570 69 74 65 33 56 64 62 65 52 75 6e 4f 6e 6c 79 4f ite3VdbeRunOnlyO
65580 6e 63 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 nce(Vdbe*);.SQLI
65590 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
655a0 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
655b0 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 e(Vdbe*);.SQLITE
655c0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
655d0 6c 69 74 65 33 56 64 62 65 43 6c 65 61 72 4f 62 lite3VdbeClearOb
655e0 6a 65 63 74 28 73 71 6c 69 74 65 33 2a 2c 56 64 ject(sqlite3*,Vd
655f0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
65600 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
65610 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 3VdbeMakeReady(V
65620 64 62 65 2a 2c 50 61 72 73 65 2a 29 3b 0a 53 51 dbe*,Parse*);.SQ
65630 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
65640 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 sqlite3VdbeFina
65650 6c 69 7a 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c lize(Vdbe*);.SQL
65660 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
65670 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f sqlite3VdbeReso
65680 6c 76 65 4c 61 62 65 6c 28 56 64 62 65 2a 2c 20 lveLabel(Vdbe*,
65690 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
656a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
656b0 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 VdbeCurrentAddr(
656c0 56 64 62 65 2a 29 3b 0a 23 69 66 64 65 66 20 53 Vdbe*);.#ifdef S
656d0 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 QLITE_DEBUG.SQLI
656e0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
656f0 20 73 71 6c 69 74 65 33 56 64 62 65 41 73 73 65 sqlite3VdbeAsse
65700 72 74 4d 61 79 41 62 6f 72 74 28 56 64 62 65 20 rtMayAbort(Vdbe
65710 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
65720 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
65730 71 6c 69 74 65 33 56 64 62 65 54 72 61 63 65 28 qlite3VdbeTrace(
65740 56 64 62 65 2a 2c 46 49 4c 45 2a 29 3b 0a 23 65 Vdbe*,FILE*);.#e
65750 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
65760 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
65770 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73 VdbeResetStepRes
65780 75 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 ult(Vdbe*);.SQLI
65790 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
657a0 73 71 6c 69 74 65 33 56 64 62 65 52 65 77 69 6e sqlite3VdbeRewin
657b0 64 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 d(Vdbe*);.SQLITE
657c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
657d0 69 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64 ite3VdbeReset(Vd
657e0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
657f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
65800 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 3VdbeSetNumCols(
65810 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 Vdbe*,int);.SQLI
65820 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
65830 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c qlite3VdbeSetCol
65840 4e 61 6d 65 28 56 64 62 65 2a 2c 20 69 6e 74 2c Name(Vdbe*, int,
65850 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 int, const char
65860 20 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 *, void(*)(void
65870 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *));.SQLITE_PRIV
65880 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
65890 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 VdbeCountChanges
658a0 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f (Vdbe*);.SQLITE_
658b0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 20 PRIVATE sqlite3
658c0 2a 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 56 *sqlite3VdbeDb(V
658d0 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dbe*);.SQLITE_PR
658e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
658f0 65 33 56 64 62 65 53 65 74 53 71 6c 28 56 64 62 e3VdbeSetSql(Vdb
65900 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a e*, const char *
65910 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 29 3b 0a z, int n, int);.
65920 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
65930 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 oid sqlite3VdbeS
65940 77 61 70 28 56 64 62 65 2a 2c 56 64 62 65 2a 29 wap(Vdbe*,Vdbe*)
65950 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
65960 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 VdbeOp *sqlite3
65970 56 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28 VdbeTakeOpArray(
65980 56 64 62 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 Vdbe*, int*, int
65990 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
659a0 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 TE sqlite3_value
659b0 20 2a 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 *sqlite3VdbeGet
659c0 56 61 6c 75 65 28 56 64 62 65 2a 2c 20 69 6e 74 Value(Vdbe*, int
659d0 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 , u8);.SQLITE_PR
659e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
659f0 65 33 56 64 62 65 53 65 74 56 61 72 6d 61 73 6b e3VdbeSetVarmask
65a00 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 (Vdbe*, int);.#i
65a10 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
65a20 54 5f 54 52 41 43 45 0a 53 51 4c 49 54 45 5f 50 T_TRACE.SQLITE_P
65a30 52 49 56 41 54 45 20 20 20 63 68 61 72 20 2a 73 RIVATE char *s
65a40 71 6c 69 74 65 33 56 64 62 65 45 78 70 61 6e 64 qlite3VdbeExpand
65a50 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 Sql(Vdbe*, const
65a60 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a char*);.#endif.
65a70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
65a80 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
65a90 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 4b 65 79 RecordUnpack(Key
65aa0 49 6e 66 6f 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 Info*,int,const
65ab0 76 6f 69 64 2a 2c 55 6e 70 61 63 6b 65 64 52 65 void*,UnpackedRe
65ac0 63 6f 72 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 cord*);.SQLITE_P
65ad0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
65ae0 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 e3VdbeRecordComp
65af0 61 72 65 28 69 6e 74 2c 63 6f 6e 73 74 20 76 6f are(int,const vo
65b00 69 64 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f id*,UnpackedReco
65b10 72 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 rd*);.SQLITE_PRI
65b20 56 41 54 45 20 55 6e 70 61 63 6b 65 64 52 65 63 VATE UnpackedRec
65b30 6f 72 64 20 2a 73 71 6c 69 74 65 33 56 64 62 65 ord *sqlite3Vdbe
65b40 41 6c 6c 6f 63 55 6e 70 61 63 6b 65 64 52 65 63 AllocUnpackedRec
65b50 6f 72 64 28 4b 65 79 49 6e 66 6f 20 2a 2c 20 63 ord(KeyInfo *, c
65b60 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 har *, int, char
65b70 20 2a 2a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 **);..#ifndef S
65b80 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 QLITE_OMIT_TRIGG
65b90 45 52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ER.SQLITE_PRIVAT
65ba0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
65bb0 62 65 4c 69 6e 6b 53 75 62 50 72 6f 67 72 61 6d beLinkSubProgram
65bc0 28 56 64 62 65 20 2a 2c 20 53 75 62 50 72 6f 67 (Vdbe *, SubProg
65bd0 72 61 6d 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a ram *);.#endif..
65be0 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
65bf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
65c00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
65c10 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 eComment(Vdbe*,
65c20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e const char*, ...
65c30 29 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 );.# define Vdbe
65c40 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 Comment(X) sqli
65c50 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58 te3VdbeComment X
65c60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
65c70 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 void sqlite3Vd
65c80 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 beNoopComment(Vd
65c90 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a be*, const char*
65ca0 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 , ...);.# define
65cb0 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 VdbeNoopComment
65cc0 28 58 29 20 20 73 71 6c 69 74 65 33 56 64 62 65 (X) sqlite3Vdbe
65cd0 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 65 NoopComment X.#e
65ce0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 64 62 lse.# define Vdb
65cf0 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 65 eComment(X).# de
65d00 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d fine VdbeNoopCom
65d10 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a ment(X).#endif..
65d20 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a #endif../*******
65d30 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 ******* End of v
65d40 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dbe.h **********
65d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
65d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
65d70 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
65d80 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
65d90 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
65da0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e off in sqliteIn
65db0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
65dc0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
65dd0 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
65de0 70 61 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d pager.h in the m
65df0 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 iddle of sqliteI
65e00 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
65e10 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
65e20 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
65e30 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a le pager.h *****
65e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
65e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
65e60 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
65e70 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 01 September 15.
65e80 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
65e90 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
65ea0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
65eb0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
65ec0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
65ed0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
65ee0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
65ef0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
65f00 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
65f10 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
65f20 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
65f30 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
65f40 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
65f50 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
65f60 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
65f70 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
65f80 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
65f90 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
65fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
65fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
65fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
65fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
65fe0 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 * This header fi
65ff0 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 le defines the i
66000 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 nterface that th
66010 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 e sqlite page ca
66020 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d che.** subsystem
66030 2e 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 . The page cach
66040 65 20 73 75 62 73 79 73 74 65 6d 20 72 65 61 64 e subsystem read
66050 73 20 61 6e 64 20 77 72 69 74 65 73 20 61 20 66 s and writes a f
66060 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74 ile a page.** at
66070 20 61 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76 a time and prov
66080 69 64 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66 ides a journal f
66090 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a or rollback..*/.
660a0 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 45 52 5f .#ifndef _PAGER_
660b0 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 41 47 45 H_.#define _PAGE
660c0 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 R_H_../*.** Defa
660d0 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 ult maximum size
660e0 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 for persistent
660f0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 20 41 journal files. A
66100 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a 20 76 61 negative .** va
66110 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c 69 6d lue means no lim
66120 69 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 6d it. This value m
66130 61 79 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e ay be overridden
66140 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 73 using the .** s
66150 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
66160 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20 41 50 alSizeLimit() AP
66170 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 50 52 41 I. See also "PRA
66180 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 GMA journal_size
66190 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 69 66 6e _limit"..*/.#ifn
661a0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 def SQLITE_DEFAU
661b0 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f LT_JOURNAL_SIZE_
661c0 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 6e 65 20 LIMIT. #define
661d0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a SQLITE_DEFAULT_J
661e0 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 OURNAL_SIZE_LIMI
661f0 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a T -1.#endif../*.
66200 2a 2a 20 54 68 65 20 74 79 70 65 20 75 73 65 64 ** The type used
66210 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20 to represent a
66220 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 page number. Th
66230 65 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20 e first page in
66240 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63 61 6c a file.** is cal
66250 6c 65 64 20 70 61 67 65 20 31 2e 20 20 30 20 69 led page 1. 0 i
66260 73 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 s used to repres
66270 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 67 65 22 ent "not a page"
66280 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 33 32 ..*/.typedef u32
66290 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 Pgno;../*.** Ea
662a0 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 73 20 ch open file is
662b0 6d 61 6e 61 67 65 64 20 62 79 20 61 20 73 65 70 managed by a sep
662c0 61 72 61 74 65 20 69 6e 73 74 61 6e 63 65 20 6f arate instance o
662d0 66 20 74 68 65 20 22 50 61 67 65 72 22 20 73 74 f the "Pager" st
662e0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 ructure..*/.type
662f0 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72 def struct Pager
66300 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 48 Pager;../*.** H
66310 61 6e 64 6c 65 20 74 79 70 65 20 66 6f 72 20 70 andle type for p
66320 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ages..*/.typedef
66330 20 73 74 72 75 63 74 20 50 67 48 64 72 20 44 62 struct PgHdr Db
66340 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 Page;../*.** Pag
66350 65 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f 4d e number PAGER_M
66360 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72 20 J_PGNO is never
66370 75 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69 74 used in an SQLit
66380 65 20 64 61 74 61 62 61 73 65 20 28 69 74 20 69 e database (it i
66390 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66 6f s.** reserved fo
663a0 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e 64 r working around
663b0 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69 78 a windows/posix
663c0 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 incompatibility
663d0 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 ). It is.** used
663e0 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 in the journal
663f0 74 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74 20 to signify that
66400 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 the remainder of
66410 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
66420 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65 64 e .** is devoted
66430 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d 61 to storing a ma
66440 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
66450 65 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e 6f e - there are no
66460 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a 2a more pages to.*
66470 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65 65 * roll back. See
66480 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66 75 comments for fu
66490 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73 74 nction writeMast
664a0 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 6e 20 70 erJournal() in p
664b0 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f 72 20 64 ager.c .** for d
664c0 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 etails..*/.#defi
664d0 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f ne PAGER_MJ_PGNO
664e0 28 78 29 20 28 28 50 67 6e 6f 29 28 28 50 45 4e (x) ((Pgno)((PEN
664f0 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d 3e DING_BYTE/((x)->
66500 70 61 67 65 53 69 7a 65 29 29 2b 31 29 29 0a 0a pageSize))+1))..
66510 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 /*.** Allowed va
66520 6c 75 65 73 20 66 6f 72 20 74 68 65 20 66 6c 61 lues for the fla
66530 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 gs parameter to
66540 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
66550 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 ()..**.** NOTE:
66560 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 These values mus
66570 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 t match the corr
66580 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 45 45 5f esponding BTREE_
66590 20 76 61 6c 75 65 73 20 69 6e 20 62 74 72 65 65 values in btree
665a0 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 .h..*/.#define P
665b0 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 AGER_OMIT_JOURNA
665c0 4c 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20 L 0x0001 /*
665d0 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 72 6f 6c Do not use a rol
665e0 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f lback journal */
665f0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4d .#define PAGER_M
66600 45 4d 4f 52 59 20 20 20 20 20 20 20 20 30 78 30 EMORY 0x0
66610 30 30 32 20 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 002 /* In-mem
66620 6f 72 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a ory database */.
66630 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c ./*.** Valid val
66640 75 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f ues for the seco
66650 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 nd argument to s
66660 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 qlite3PagerLocki
66670 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 ngMode()..*/.#de
66680 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 fine PAGER_LOCKI
66690 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 NGMODE_QUERY
666a0 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 -1.#define PAG
666b0 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e ER_LOCKINGMODE_N
666c0 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 65 ORMAL 0.#de
666d0 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 fine PAGER_LOCKI
666e0 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 NGMODE_EXCLUSIVE
666f0 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 65 1../*.** Nume
66700 72 69 63 20 63 6f 6e 73 74 61 6e 74 73 20 74 68 ric constants th
66710 61 74 20 65 6e 63 6f 64 65 20 74 68 65 20 6a 6f at encode the jo
66720 75 72 6e 61 6c 6d 6f 64 65 2e 20 20 0a 2a 2f 0a urnalmode. .*/.
66730 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f #define PAGER_JO
66740 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20 URNALMODE_QUERY
66750 20 20 20 20 28 2d 31 29 20 20 2f 2a 20 51 75 65 (-1) /* Que
66760 72 79 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ry the value of
66770 6a 6f 75 72 6e 61 6c 6d 6f 64 65 20 2a 2f 0a 23 journalmode */.#
66780 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 define PAGER_JOU
66790 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 RNALMODE_DELETE
667a0 20 20 20 20 20 30 20 20 20 2f 2a 20 43 6f 6d 6d 0 /* Comm
667b0 69 74 20 62 79 20 64 65 6c 65 74 69 6e 67 20 6a it by deleting j
667c0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 23 ournal file */.#
667d0 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 define PAGER_JOU
667e0 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 RNALMODE_PERSIST
667f0 20 20 20 20 20 31 20 20 20 2f 2a 20 43 6f 6d 6d 1 /* Comm
66800 69 74 20 62 79 20 7a 65 72 6f 69 6e 67 20 6a 6f it by zeroing jo
66810 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a urnal header */.
66820 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f #define PAGER_JO
66830 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 URNALMODE_OFF
66840 20 20 20 20 20 20 32 20 20 20 2f 2a 20 4a 6f 75 2 /* Jou
66850 72 6e 61 6c 20 6f 6d 69 74 74 65 64 2e 20 20 2a rnal omitted. *
66860 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f /.#define PAGER_
66870 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e JOURNALMODE_TRUN
66880 43 41 54 45 20 20 20 20 33 20 20 20 2f 2a 20 43 CATE 3 /* C
66890 6f 6d 6d 69 74 20 62 79 20 74 72 75 6e 63 61 74 ommit by truncat
668a0 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 ing journal */.#
668b0 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 define PAGER_JOU
668c0 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 RNALMODE_MEMORY
668d0 20 20 20 20 20 34 20 20 20 2f 2a 20 49 6e 2d 6d 4 /* In-m
668e0 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 emory journal fi
668f0 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 le */.#define PA
66900 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
66910 57 41 4c 20 20 20 20 20 20 20 20 20 35 20 20 20 WAL 5
66920 2f 2a 20 55 73 65 20 77 72 69 74 65 2d 61 68 65 /* Use write-ahe
66930 61 64 20 6c 6f 67 67 69 6e 67 20 2a 2f 0a 0a 2f ad logging */../
66940 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 64 *.** The remaind
66950 65 72 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 er of this file
66960 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 65 63 contains the dec
66970 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 larations of the
66980 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 68 functions.** th
66990 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20 50 at make up the P
669a0 61 67 65 72 20 73 75 62 2d 73 79 73 74 65 6d 20 ager sub-system
669b0 41 50 49 2e 20 53 65 65 20 73 6f 75 72 63 65 20 API. See source
669c0 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f code comments fo
669d0 72 20 0a 2a 2a 20 61 20 64 65 74 61 69 6c 65 64 r .** a detailed
669e0 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 description of
669f0 65 61 63 68 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f each routine..*/
66a00 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20 63 6c ../* Open and cl
66a10 6f 73 65 20 61 20 50 61 67 65 72 20 63 6f 6e 6e ose a Pager conn
66a20 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a 53 51 4c 49 ection. */ .SQLI
66a30 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
66a40 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 qlite3PagerOpen(
66a50 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c . sqlite3_vfs*,
66a60 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67 . Pager **ppPag
66a70 65 72 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 er,. const char
66a80 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 69 6e 74 2c *,. int,. int,
66a90 0a 20 20 69 6e 74 2c 0a 20 20 76 6f 69 64 28 2a . int,. void(*
66aa0 29 28 44 62 50 61 67 65 2a 29 0a 29 3b 0a 53 51 )(DbPage*).);.SQ
66ab0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
66ac0 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f sqlite3PagerClo
66ad0 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 se(Pager *pPager
66ae0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
66af0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
66b00 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 erReadFileheader
66b10 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75 6e (Pager*, int, un
66b20 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 0a signed char*);..
66b30 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 /* Functions use
66b40 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 65 20 61 d to configure a
66b50 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 2a Pager object. *
66b60 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
66b70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
66b80 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 erSetBusyhandler
66b90 28 50 61 67 65 72 2a 2c 20 69 6e 74 28 2a 29 28 (Pager*, int(*)(
66ba0 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 20 2a 29 void *), void *)
66bb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
66bc0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
66bd0 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 61 67 rSetPagesize(Pag
66be0 65 72 2a 2c 20 75 33 32 2a 2c 20 69 6e 74 29 3b er*, u32*, int);
66bf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
66c00 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
66c10 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67 MaxPageCount(Pag
66c20 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 er*, int);.SQLIT
66c30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
66c40 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61 qlite3PagerSetCa
66c50 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20 chesize(Pager*,
66c60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
66c70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
66c80 33 50 61 67 65 72 53 68 72 69 6e 6b 28 50 61 67 3PagerShrink(Pag
66c90 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 er*);.SQLITE_PRI
66ca0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
66cb0 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 3PagerSetSafetyL
66cc0 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 6e 74 2c evel(Pager*,int,
66cd0 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
66ce0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
66cf0 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 ite3PagerLocking
66d00 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e Mode(Pager *, in
66d10 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
66d20 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
66d30 67 65 72 53 65 74 4a 6f 75 72 6e 61 6c 4d 6f 64 gerSetJournalMod
66d40 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b e(Pager *, int);
66d50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
66d60 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
66d70 47 65 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 GetJournalMode(P
66d80 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
66d90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
66da0 65 33 50 61 67 65 72 4f 6b 54 6f 43 68 61 6e 67 e3PagerOkToChang
66db0 65 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67 eJournalMode(Pag
66dc0 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 er*);.SQLITE_PRI
66dd0 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 VATE i64 sqlite3
66de0 50 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 PagerJournalSize
66df0 4c 69 6d 69 74 28 50 61 67 65 72 20 2a 2c 20 69 Limit(Pager *, i
66e00 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 64);.SQLITE_PRIV
66e10 41 54 45 20 73 71 6c 69 74 65 33 5f 62 61 63 6b ATE sqlite3_back
66e20 75 70 20 2a 2a 73 71 6c 69 74 65 33 50 61 67 65 up **sqlite3Page
66e30 72 42 61 63 6b 75 70 50 74 72 28 50 61 67 65 72 rBackupPtr(Pager
66e40 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e *);../* Function
66e50 73 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e s used to obtain
66e60 20 61 6e 64 20 72 65 6c 65 61 73 65 20 70 61 67 and release pag
66e70 65 20 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f e references. */
66e80 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 .SQLITE_PRIVATE
66e90 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
66ea0 72 41 63 71 75 69 72 65 28 50 61 67 65 72 20 2a rAcquire(Pager *
66eb0 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e pPager, Pgno pgn
66ec0 6f 2c 20 44 62 50 61 67 65 20 2a 2a 70 70 50 61 o, DbPage **ppPa
66ed0 67 65 2c 20 69 6e 74 20 63 6c 72 46 6c 61 67 29 ge, int clrFlag)
66ee0 3b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 ;.#define sqlite
66ef0 33 50 61 67 65 72 47 65 74 28 41 2c 42 2c 43 29 3PagerGet(A,B,C)
66f00 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 sqlite3PagerAcq
66f10 75 69 72 65 28 41 2c 42 2c 43 2c 30 29 0a 53 51 uire(A,B,C,0).SQ
66f20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 LITE_PRIVATE DbP
66f30 61 67 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 age *sqlite3Page
66f40 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 rLookup(Pager *p
66f50 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f Pager, Pgno pgno
66f60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
66f70 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
66f80 67 65 72 52 65 66 28 44 62 50 61 67 65 2a 29 3b gerRef(DbPage*);
66f90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
66fa0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
66fb0 72 55 6e 72 65 66 28 44 62 50 61 67 65 2a 29 3b rUnref(DbPage*);
66fc0 0a 0a 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 73 20 ../* Operations
66fd0 6f 6e 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 on page referenc
66fe0 65 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es. */.SQLITE_PR
66ff0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
67000 33 50 61 67 65 72 57 72 69 74 65 28 44 62 50 61 3PagerWrite(DbPa
67010 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ge*);.SQLITE_PRI
67020 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
67030 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 3PagerDontWrite(
67040 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 DbPage*);.SQLITE
67050 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
67060 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 ite3PagerMovepag
67070 65 28 50 61 67 65 72 2a 2c 44 62 50 61 67 65 2a e(Pager*,DbPage*
67080 2c 50 67 6e 6f 2c 69 6e 74 29 3b 0a 53 51 4c 49 ,Pgno,int);.SQLI
67090 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
670a0 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52 qlite3PagerPageR
670b0 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65 2a 29 efcount(DbPage*)
670c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
670d0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 void *sqlite3Pa
670e0 67 65 72 47 65 74 44 61 74 61 28 44 62 50 61 67 gerGetData(DbPag
670f0 65 20 2a 29 3b 20 0a 53 51 4c 49 54 45 5f 50 52 e *); .SQLITE_PR
67100 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
67110 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 te3PagerGetExtra
67120 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 0a 2f 2a (DbPage *); ../*
67130 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 Functions used
67140 74 6f 20 6d 61 6e 61 67 65 20 70 61 67 65 72 20 to manage pager
67150 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 6e 64 transactions and
67160 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 2a 2f 0a savepoints. */.
67170 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
67180 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
67190 50 61 67 65 63 6f 75 6e 74 28 50 61 67 65 72 2a Pagecount(Pager*
671a0 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f , int*);.SQLITE_
671b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
671c0 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 50 61 te3PagerBegin(Pa
671d0 67 65 72 2a 2c 20 69 6e 74 20 65 78 46 6c 61 67 ger*, int exFlag
671e0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
671f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
67200 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 e3PagerCommitPha
67210 73 65 4f 6e 65 28 50 61 67 65 72 2a 2c 63 6f 6e seOne(Pager*,con
67220 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 st char *zMaster
67230 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
67240 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
67250 65 33 50 61 67 65 72 45 78 63 6c 75 73 69 76 65 e3PagerExclusive
67260 4c 6f 63 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51 Lock(Pager*);.SQ
67270 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
67280 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e sqlite3PagerSyn
67290 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 c(Pager *pPager)
672a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
672b0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
672c0 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 rCommitPhaseTwo(
672d0 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Pager*);.SQLITE_
672e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
672f0 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
67300 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
67310 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
67320 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76 ite3PagerOpenSav
67330 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 epoint(Pager *pP
67340 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 ager, int n);.SQ
67350 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
67360 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 sqlite3PagerSav
67370 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 epoint(Pager *pP
67380 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e ager, int op, in
67390 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 53 t iSavepoint);.S
673a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
673b0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 t sqlite3PagerSh
673c0 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a aredLock(Pager *
673d0 70 50 61 67 65 72 29 3b 0a 0a 23 69 66 6e 64 65 pPager);..#ifnde
673e0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 f SQLITE_OMIT_WA
673f0 4c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 L.SQLITE_PRIVATE
67400 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 int sqlite3Pa
67410 67 65 72 43 68 65 63 6b 70 6f 69 6e 74 28 50 61 gerCheckpoint(Pa
67420 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
67430 2c 20 69 6e 74 2a 2c 20 69 6e 74 2a 29 3b 0a 53 , int*, int*);.S
67440 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
67450 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
67460 57 61 6c 53 75 70 70 6f 72 74 65 64 28 50 61 67 WalSupported(Pag
67470 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c er *pPager);.SQL
67480 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
67490 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 61 t sqlite3PagerWa
674a0 6c 43 61 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 lCallback(Pager
674b0 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c 49 54 45 *pPager);.SQLITE
674c0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
674d0 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 57 qlite3PagerOpenW
674e0 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 al(Pager *pPager
674f0 2c 20 69 6e 74 20 2a 70 69 73 4f 70 65 6e 29 3b , int *pisOpen);
67500 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
67510 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 int sqlite3Pag
67520 65 72 43 6c 6f 73 65 57 61 6c 28 50 61 67 65 72 erCloseWal(Pager
67530 20 2a 70 50 61 67 65 72 29 3b 0a 23 65 6e 64 69 *pPager);.#endi
67540 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
67550 5f 45 4e 41 42 4c 45 5f 5a 49 50 56 46 53 0a 53 _ENABLE_ZIPVFS.S
67560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
67570 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
67580 57 61 6c 46 72 61 6d 65 73 69 7a 65 28 50 61 67 WalFramesize(Pag
67590 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 23 65 6e er *pPager);.#en
675a0 64 69 66 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e dif../* Function
675b0 73 20 75 73 65 64 20 74 6f 20 71 75 65 72 79 20 s used to query
675c0 70 61 67 65 72 20 73 74 61 74 65 20 61 6e 64 20 pager state and
675d0 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e 20 2a configuration. *
675e0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
675f0 20 75 38 20 73 71 6c 69 74 65 33 50 61 67 65 72 u8 sqlite3Pager
67600 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 65 72 Isreadonly(Pager
67610 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
67620 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
67630 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61 67 65 gerRefcount(Page
67640 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
67650 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
67660 61 67 65 72 4d 65 6d 55 73 65 64 28 50 61 67 65 agerMemUsed(Page
67670 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
67680 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
67690 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 sqlite3PagerFile
676a0 6e 61 6d 65 28 50 61 67 65 72 2a 2c 20 69 6e 74 name(Pager*, int
676b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
676c0 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f E const sqlite3_
676d0 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65 vfs *sqlite3Page
676e0 72 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 rVfs(Pager*);.SQ
676f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
67700 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74 ite3_file *sqlit
67710 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 65 e3PagerFile(Page
67720 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
67730 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
67740 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 sqlite3PagerJour
67750 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b nalname(Pager*);
67760 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
67770 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
67780 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a Nosync(Pager*);.
67790 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
677a0 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 oid *sqlite3Page
677b0 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 72 rTempSpace(Pager
677c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
677d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
677e0 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72 gerIsMemdb(Pager
677f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
67800 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
67810 61 67 65 72 43 61 63 68 65 53 74 61 74 28 50 61 agerCacheStat(Pa
67820 67 65 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c ger *, int, int,
67830 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f int *);.SQLITE_
67840 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
67850 69 74 65 33 50 61 67 65 72 43 6c 65 61 72 43 61 ite3PagerClearCa
67860 63 68 65 28 50 61 67 65 72 20 2a 29 3b 0a 53 51 che(Pager *);.SQ
67870 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
67880 20 73 71 6c 69 74 65 33 53 65 63 74 6f 72 53 69 sqlite3SectorSi
67890 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ze(sqlite3_file
678a0 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e *);../* Function
678b0 73 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63 61 s used to trunca
678c0 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 te the database
678d0 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f file. */.SQLITE_
678e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
678f0 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
67900 65 49 6d 61 67 65 28 50 61 67 65 72 2a 2c 50 67 eImage(Pager*,Pg
67910 6e 6f 29 3b 0a 0a 23 69 66 20 64 65 66 69 6e 65 no);..#if define
67920 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 d(SQLITE_HAS_COD
67930 45 43 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 EC) && !defined(
67940 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 29 SQLITE_OMIT_WAL)
67950 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
67960 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
67970 65 72 43 6f 64 65 63 28 44 62 50 61 67 65 20 2a erCodec(DbPage *
67980 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 75 );.#endif../* Fu
67990 6e 63 74 69 6f 6e 73 20 74 6f 20 73 75 70 70 6f nctions to suppo
679a0 72 74 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 rt testing and d
679b0 65 62 75 67 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 ebugging. */.#if
679c0 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
679d0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
679e0 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 45 ITE_TEST).SQLITE
679f0 5f 50 52 49 56 41 54 45 20 20 20 50 67 6e 6f 20 _PRIVATE Pgno
67a00 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
67a10 6e 75 6d 62 65 72 28 44 62 50 61 67 65 2a 29 3b number(DbPage*);
67a20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
67a30 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 int sqlite3Pag
67a40 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 44 62 erIswriteable(Db
67a50 50 61 67 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 Page*);.#endif.#
67a60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
67a70 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
67a80 20 20 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 int *sqlite3P
67a90 61 67 65 72 53 74 61 74 73 28 50 61 67 65 72 2a agerStats(Pager*
67aa0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
67ab0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
67ac0 50 61 67 65 72 52 65 66 64 75 6d 70 28 50 61 67 PagerRefdump(Pag
67ad0 65 72 2a 29 3b 0a 20 20 76 6f 69 64 20 64 69 73 er*);. void dis
67ae0 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
67af0 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a o_errors(void);.
67b00 20 20 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69 void enable_si
67b10 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
67b20 73 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 23 s(void);.#else.#
67b30 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f define disable_
67b40 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
67b50 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 ors().# define e
67b60 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f nable_simulated_
67b70 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 io_errors().#end
67b80 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50 if..#endif /* _P
67b90 41 47 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a AGER_H_ */../***
67ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
67bb0 6f 66 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a of pager.h *****
67bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
67bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
67be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
67bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
67c00 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
67c10 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
67c20 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
67c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
67c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
67c50 75 64 65 20 70 63 61 63 68 65 2e 68 20 69 6e 20 ude pcache.h in
67c60 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 the middle of sq
67c70 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
67c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
67c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
67ca0 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e 68 20 n file pcache.h
67cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
67cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
67cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
67ce0 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30 35 * 2008 August 05
67cf0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
67d00 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
67d10 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
67d20 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
67d30 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
67d40 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
67d50 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
67d60 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
67d70 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
67d80 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
67d90 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
67da0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
67db0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
67dc0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
67dd0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
67de0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
67df0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
67e00 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
67e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
67e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
67e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
67e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
67e50 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 ** This header f
67e60 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 ile defines the
67e70 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 interface that t
67e80 68 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 he sqlite page c
67e90 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 ache.** subsyste
67ea0 6d 2e 20 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 m. .*/..#ifndef
67eb0 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 70 65 _PCACHE_H_..type
67ec0 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72 def struct PgHdr
67ed0 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65 66 20 PgHdr;.typedef
67ee0 73 74 72 75 63 74 20 50 43 61 63 68 65 20 50 43 struct PCache PC
67ef0 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 65 ache;../*.** Eve
67f00 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 ry page in the c
67f10 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c ache is controll
67f20 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 ed by an instanc
67f30 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
67f40 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e ng.** structure.
67f50 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 64 72 .*/.struct PgHdr
67f60 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 {. sqlite3_pca
67f70 63 68 65 5f 70 61 67 65 20 2a 70 50 61 67 65 3b che_page *pPage;
67f80 20 20 20 20 2f 2a 20 50 63 61 63 68 65 20 6f 62 /* Pcache ob
67f90 6a 65 63 74 20 70 61 67 65 20 68 61 6e 64 6c 65 ject page handle
67fa0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 */. void *pDat
67fb0 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a;
67fc0 20 20 20 20 20 2f 2a 20 50 61 67 65 20 64 61 74 /* Page dat
67fd0 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 78 a */. void *pEx
67fe0 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 tra;
67ff0 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 63 /* Extra c
68000 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 50 67 48 64 ontent */. PgHd
68010 72 20 2a 70 44 69 72 74 79 3b 20 20 20 20 20 20 r *pDirty;
68020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
68030 61 6e 73 69 65 6e 74 20 6c 69 73 74 20 6f 66 20 ansient list of
68040 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 dirty pages */.
68050 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 Pager *pPager;
68060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68070 2f 2a 20 54 68 65 20 70 61 67 65 72 20 74 68 69 /* The pager thi
68080 73 20 70 61 67 65 20 69 73 20 70 61 72 74 20 6f s page is part o
68090 66 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f f */. Pgno pgno
680a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
680b0 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
680c0 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 70 61 mber for this pa
680d0 67 65 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ge */.#ifdef SQL
680e0 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a ITE_CHECK_PAGES.
680f0 20 20 75 33 32 20 70 61 67 65 48 61 73 68 3b 20 u32 pageHash;
68100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68110 20 2f 2a 20 48 61 73 68 20 6f 66 20 70 61 67 65 /* Hash of page
68120 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 23 65 6e 64 content */.#end
68130 69 66 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 if. u16 flags;
68140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68150 20 20 20 20 2f 2a 20 50 47 48 44 52 20 66 6c 61 /* PGHDR fla
68160 67 73 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 gs defined below
68170 20 2a 2f 0a 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a */.. /********
68180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
68190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
681a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
681b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 **************.
681c0 20 2a 2a 20 45 6c 65 6d 65 6e 74 73 20 61 62 6f ** Elements abo
681d0 76 65 20 61 72 65 20 70 75 62 6c 69 63 2e 20 20 ve are public.
681e0 41 6c 6c 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 All that follows
681f0 20 69 73 20 70 72 69 76 61 74 65 20 74 6f 20 70 is private to p
68200 63 61 63 68 65 2e 63 0a 20 20 2a 2a 20 61 6e 64 cache.c. ** and
68210 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 should not be a
68220 63 63 65 73 73 65 64 20 62 79 20 6f 74 68 65 72 ccessed by other
68230 20 6d 6f 64 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20 modules.. */.
68240 20 69 31 36 20 6e 52 65 66 3b 20 20 20 20 20 20 i16 nRef;
68250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68260 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 /* Number of use
68270 72 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 rs of this page
68280 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 */. PCache *pCa
68290 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 che;
682a0 20 20 20 20 2f 2a 20 43 61 63 68 65 20 74 68 61 /* Cache tha
682b0 74 20 6f 77 6e 73 20 74 68 69 73 20 70 61 67 65 t owns this page
682c0 20 2a 2f 0a 0a 20 20 50 67 48 64 72 20 2a 70 44 */.. PgHdr *pD
682d0 69 72 74 79 4e 65 78 74 3b 20 20 20 20 20 20 20 irtyNext;
682e0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6c /* Next el
682f0 65 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 6f 66 ement in list of
68300 20 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a dirty pages */.
68310 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 50 PgHdr *pDirtyP
68320 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 rev;
68330 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 65 6c 65 /* Previous ele
68340 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 ment in list of
68350 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 7d dirty pages */.}
68360 3b 0a 0a 2f 2a 20 42 69 74 20 76 61 6c 75 65 73 ;../* Bit values
68370 20 66 6f 72 20 50 67 48 64 72 2e 66 6c 61 67 73 for PgHdr.flags
68380 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 */.#define PGHD
68390 52 5f 44 49 52 54 59 20 20 20 20 20 20 20 20 20 R_DIRTY
683a0 20 20 20 20 30 78 30 30 32 20 20 2f 2a 20 50 61 0x002 /* Pa
683b0 67 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a ge has changed *
683c0 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f /.#define PGHDR_
683d0 4e 45 45 44 5f 53 59 4e 43 20 20 20 20 20 20 20 NEED_SYNC
683e0 20 20 30 78 30 30 34 20 20 2f 2a 20 46 73 79 6e 0x004 /* Fsyn
683f0 63 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a c the rollback j
68400 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 0a 20 20 ournal before.
68410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68430 20 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 ** writing
68440 74 68 69 73 20 70 61 67 65 20 74 6f 20 74 68 65 this page to the
68450 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 database */.#de
68460 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f fine PGHDR_NEED_
68470 52 45 41 44 20 20 20 20 20 20 20 20 20 30 78 30 READ 0x0
68480 30 38 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 69 08 /* Content i
68490 73 20 75 6e 72 65 61 64 20 2a 2f 0a 23 64 65 66 s unread */.#def
684a0 69 6e 65 20 50 47 48 44 52 5f 52 45 55 53 45 5f ine PGHDR_REUSE_
684b0 55 4e 4c 49 4b 45 4c 59 20 20 20 20 30 78 30 31 UNLIKELY 0x01
684c0 30 20 20 2f 2a 20 41 20 68 69 6e 74 20 74 68 61 0 /* A hint tha
684d0 74 20 72 65 75 73 65 20 69 73 20 75 6e 6c 69 6b t reuse is unlik
684e0 65 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ely */.#define P
684f0 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 GHDR_DONT_WRITE
68500 20 20 20 20 20 20 20 30 78 30 32 30 20 20 2f 2a 0x020 /*
68510 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 63 6f Do not write co
68520 6e 74 65 6e 74 20 74 6f 20 64 69 73 6b 20 2a 2f ntent to disk */
68530 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 ../* Initialize
68540 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 65 and shutdown the
68550 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73 page cache subs
68560 79 73 74 65 6d 20 2a 2f 0a 53 51 4c 49 54 45 5f ystem */.SQLITE_
68570 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
68580 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c te3PcacheInitial
68590 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ize(void);.SQLIT
685a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
685b0 71 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74 qlite3PcacheShut
685c0 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 down(void);../*
685d0 50 61 67 65 20 63 61 63 68 65 20 62 75 66 66 65 Page cache buffe
685e0 72 20 6d 61 6e 61 67 65 6d 65 6e 74 3a 0a 2a 2a r management:.**
685f0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
68600 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 49 54 45 implement SQLITE
68610 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
68620 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 E..*/.SQLITE_PRI
68630 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
68640 33 50 43 61 63 68 65 42 75 66 66 65 72 53 65 74 3PCacheBufferSet
68650 75 70 28 76 6f 69 64 20 2a 2c 20 69 6e 74 20 73 up(void *, int s
68660 7a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 20 43 z, int n);../* C
68670 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 reate a new page
68680 72 20 63 61 63 68 65 2e 0a 2a 2a 20 55 6e 64 65 r cache..** Unde
68690 72 20 6d 65 6d 6f 72 79 20 73 74 72 65 73 73 2c r memory stress,
686a0 20 69 6e 76 6f 6b 65 20 78 53 74 72 65 73 73 20 invoke xStress
686b0 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 to try to make p
686c0 61 67 65 73 20 63 6c 65 61 6e 2e 0a 2a 2a 20 4f ages clean..** O
686d0 6e 6c 79 20 63 6c 65 61 6e 20 61 6e 64 20 75 6e nly clean and un
686e0 70 69 6e 6e 65 64 20 70 61 67 65 73 20 63 61 6e pinned pages can
686f0 20 62 65 20 72 65 63 6c 61 69 6d 65 64 2e 0a 2a be reclaimed..*
68700 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
68710 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
68720 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73 cheOpen(. int s
68730 7a 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 zPage,
68740 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
68750 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 e of every page
68760 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61 */. int szExtra
68770 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
68780 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 /* Extra spa
68790 63 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ce associated wi
687a0 74 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a th each page */.
687b0 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 int bPurgeable
687c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
687d0 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 65 /* True if page
687e0 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 s are on backing
687f0 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 store */. int
68800 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64 2a (*xStress)(void*
68810 2c 20 50 67 48 64 72 2a 29 2c 20 2f 2a 20 43 61 , PgHdr*), /* Ca
68820 6c 6c 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b ll to try to mak
68830 65 20 70 61 67 65 73 20 63 6c 65 61 6e 20 2a 2f e pages clean */
68840 0a 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 . void *pStress
68850 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
68860 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f /* Argument to
68870 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 50 43 xStress */. PC
68880 61 63 68 65 20 2a 70 54 6f 49 6e 69 74 20 20 20 ache *pToInit
68890 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
688a0 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 Preallocated spa
688b0 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 63 68 ce for the PCach
688c0 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 4d 6f 64 69 e */.);../* Modi
688d0 66 79 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 fy the page-size
688e0 20 61 66 74 65 72 20 74 68 65 20 63 61 63 68 65 after the cache
688f0 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 has been create
68900 64 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d. */.SQLITE_PRI
68910 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
68920 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69 3PcacheSetPageSi
68930 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 69 6e 74 ze(PCache *, int
68940 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 );../* Return th
68950 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 e size in bytes
68960 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 of a PCache obje
68970 63 74 2e 20 20 55 73 65 64 20 74 6f 20 70 72 65 ct. Used to pre
68980 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 73 74 6f 72 allocate.** stor
68990 61 67 65 20 73 70 61 63 65 2e 0a 2a 2f 0a 53 51 age space..*/.SQ
689a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
689b0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 sqlite3PcacheSi
689c0 7a 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 4f 6e ze(void);../* On
689d0 65 20 72 65 6c 65 61 73 65 20 70 65 72 20 73 75 e release per su
689e0 63 63 65 73 73 66 75 6c 20 66 65 74 63 68 2e 20 ccessful fetch.
689f0 20 50 61 67 65 20 69 73 20 70 69 6e 6e 65 64 20 Page is pinned
68a00 75 6e 74 69 6c 20 72 65 6c 65 61 73 65 64 2e 0a until released..
68a10 2a 2a 20 52 65 66 65 72 65 6e 63 65 20 63 6f 75 ** Reference cou
68a20 6e 74 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 nted. .*/.SQLITE
68a30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
68a40 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28 ite3PcacheFetch(
68a50 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 2c 20 69 PCache*, Pgno, i
68a60 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20 50 nt createFlag, P
68a70 67 48 64 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f gHdr**);.SQLITE_
68a80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
68a90 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 ite3PcacheReleas
68aa0 65 28 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 e(PgHdr*);..SQLI
68ab0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
68ac0 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f sqlite3PcacheDro
68ad0 70 28 50 67 48 64 72 2a 29 3b 20 20 20 20 20 20 p(PgHdr*);
68ae0 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 70 61 67 /* Remove pag
68af0 65 20 66 72 6f 6d 20 63 61 63 68 65 20 2a 2f 0a e from cache */.
68b00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
68b10 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
68b20 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72 eMakeDirty(PgHdr
68b30 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 *); /* Make s
68b40 75 72 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b ure page is mark
68b50 65 64 20 64 69 72 74 79 20 2a 2f 0a 53 51 4c 49 ed dirty */.SQLI
68b60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
68b70 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
68b80 65 43 6c 65 61 6e 28 50 67 48 64 72 2a 29 3b 20 eClean(PgHdr*);
68b90 20 20 20 2f 2a 20 4d 61 72 6b 20 61 20 73 69 6e /* Mark a sin
68ba0 67 6c 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 gle page as clea
68bb0 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 n */.SQLITE_PRIV
68bc0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
68bd0 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50 PcacheCleanAll(P
68be0 43 61 63 68 65 2a 29 3b 20 20 20 20 2f 2a 20 4d Cache*); /* M
68bf0 61 72 6b 20 61 6c 6c 20 64 69 72 74 79 20 6c 69 ark all dirty li
68c00 73 74 20 70 61 67 65 73 20 61 73 20 63 6c 65 61 st pages as clea
68c10 6e 20 2a 2f 0a 0a 2f 2a 20 43 68 61 6e 67 65 20 n */../* Change
68c20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 a page number.
68c30 55 73 65 64 20 62 79 20 69 6e 63 72 2d 76 61 63 Used by incr-vac
68c40 75 75 6d 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 uum. */.SQLITE_P
68c50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
68c60 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 50 67 te3PcacheMove(Pg
68c70 48 64 72 2a 2c 20 50 67 6e 6f 29 3b 0a 0a 2f 2a Hdr*, Pgno);../*
68c80 20 52 65 6d 6f 76 65 20 61 6c 6c 20 70 61 67 65 Remove all page
68c90 73 20 77 69 74 68 20 70 67 6e 6f 3e 78 2e 20 20 s with pgno>x.
68ca0 52 65 73 65 74 20 74 68 65 20 63 61 63 68 65 20 Reset the cache
68cb0 69 66 20 78 3d 3d 30 20 2a 2f 0a 53 51 4c 49 54 if x==0 */.SQLIT
68cc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
68cd0 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e qlite3PcacheTrun
68ce0 63 61 74 65 28 50 43 61 63 68 65 2a 2c 20 50 67 cate(PCache*, Pg
68cf0 6e 6f 20 78 29 3b 0a 0a 2f 2a 20 47 65 74 20 61 no x);../* Get a
68d00 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 list of all dir
68d10 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 ty pages in the
68d20 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79 cache, sorted by
68d30 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a page number */.
68d40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 SQLITE_PRIVATE P
68d50 67 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61 gHdr *sqlite3Pca
68d60 63 68 65 44 69 72 74 79 4c 69 73 74 28 50 43 61 cheDirtyList(PCa
68d70 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 65 73 65 74 che*);../* Reset
68d80 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65 20 63 and close the c
68d90 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 53 ache object */.S
68da0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
68db0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
68dc0 43 6c 6f 73 65 28 50 43 61 63 68 65 2a 29 3b 0a Close(PCache*);.
68dd0 0a 2f 2a 20 43 6c 65 61 72 20 66 6c 61 67 73 20 ./* Clear flags
68de0 66 72 6f 6d 20 70 61 67 65 73 20 6f 66 20 74 68 from pages of th
68df0 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a e page cache */.
68e00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
68e10 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
68e20 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 eClearSyncFlags(
68e30 50 43 61 63 68 65 20 2a 29 3b 0a 0a 2f 2a 20 44 PCache *);../* D
68e40 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 iscard the conte
68e50 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 nts of the cache
68e60 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
68e70 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
68e80 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 68 cacheClear(PCach
68e90 65 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 e*);../* Return
68ea0 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
68eb0 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 of outstanding
68ec0 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 page references
68ed0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
68ee0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
68ef0 63 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63 cheRefCount(PCac
68f00 68 65 2a 29 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d he*);../* Increm
68f10 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 ent the referenc
68f20 65 20 63 6f 75 6e 74 20 6f 66 20 61 6e 20 65 78 e count of an ex
68f30 69 73 74 69 6e 67 20 70 61 67 65 20 2a 2f 0a 53 isting page */.S
68f40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
68f50 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
68f60 52 65 66 28 50 67 48 64 72 2a 29 3b 0a 0a 53 51 Ref(PgHdr*);..SQ
68f70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
68f80 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 sqlite3PcachePa
68f90 67 65 52 65 66 63 6f 75 6e 74 28 50 67 48 64 72 geRefcount(PgHdr
68fa0 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 *);../* Return t
68fb0 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
68fc0 6f 66 20 70 61 67 65 73 20 73 74 6f 72 65 64 20 of pages stored
68fd0 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a in the cache */.
68fe0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
68ff0 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
69000 50 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65 Pagecount(PCache
69010 2a 29 3b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 *);..#if defined
69020 28 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 (SQLITE_CHECK_PA
69030 47 45 53 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 GES) || defined(
69040 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a SQLITE_DEBUG)./*
69050 20 49 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 Iterate through
69060 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 all dirty pages
69070 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 currently store
69080 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 20 d in the cache.
69090 54 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 This.** interfac
690a0 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 e is only availa
690b0 62 6c 65 20 69 66 20 53 51 4c 49 54 45 5f 43 48 ble if SQLITE_CH
690c0 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65 66 ECK_PAGES is def
690d0 69 6e 65 64 20 77 68 65 6e 20 74 68 65 20 0a 2a ined when the .*
690e0 2a 20 6c 69 62 72 61 72 79 20 69 73 20 62 75 69 * library is bui
690f0 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 lt..*/.SQLITE_PR
69100 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
69110 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65 44 e3PcacheIterateD
69120 69 72 74 79 28 50 43 61 63 68 65 20 2a 70 43 61 irty(PCache *pCa
69130 63 68 65 2c 20 76 6f 69 64 20 28 2a 78 49 74 65 che, void (*xIte
69140 72 29 28 50 67 48 64 72 20 2a 29 29 3b 0a 23 65 r)(PgHdr *));.#e
69150 6e 64 69 66 0a 0a 2f 2a 20 53 65 74 20 61 6e 64 ndif../* Set and
69160 20 67 65 74 20 74 68 65 20 73 75 67 67 65 73 74 get the suggest
69170 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 66 6f ed cache-size fo
69180 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 r the specified
69190 70 61 67 65 72 2d 63 61 63 68 65 2e 0a 2a 2a 0a pager-cache..**.
691a0 2a 2a 20 49 66 20 6e 6f 20 67 6c 6f 62 61 6c 20 ** If no global
691b0 6d 61 78 69 6d 75 6d 20 69 73 20 63 6f 6e 66 69 maximum is confi
691c0 67 75 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 gured, then the
691d0 73 79 73 74 65 6d 20 61 74 74 65 6d 70 74 73 20 system attempts
691e0 74 6f 20 6c 69 6d 69 74 0a 2a 2a 20 74 68 65 20 to limit.** the
691f0 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
69200 70 61 67 65 73 20 63 61 63 68 65 64 20 62 79 20 pages cached by
69210 70 75 72 67 65 61 62 6c 65 20 70 61 67 65 72 2d purgeable pager-
69220 63 61 63 68 65 73 20 74 6f 20 74 68 65 20 73 75 caches to the su
69230 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75 67 67 m.** of the sugg
69240 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 ested cache-size
69250 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
69260 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
69270 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65 73 3PcacheSetCaches
69280 69 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 69 6e ize(PCache *, in
69290 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 t);.#ifdef SQLIT
692a0 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 E_TEST.SQLITE_PR
692b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
692c0 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65 73 3PcacheGetCaches
692d0 69 7a 65 28 50 43 61 63 68 65 20 2a 29 3b 0a 23 ize(PCache *);.#
692e0 65 6e 64 69 66 0a 0a 2f 2a 20 46 72 65 65 20 75 endif../* Free u
692f0 70 20 61 73 20 6d 75 63 68 20 6d 65 6d 6f 72 79 p as much memory
69300 20 61 73 20 70 6f 73 73 69 62 6c 65 20 66 72 6f as possible fro
69310 6d 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 m the page cache
69320 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
69330 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
69340 63 61 63 68 65 53 68 72 69 6e 6b 28 50 43 61 63 cacheShrink(PCac
69350 68 65 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 he*);..#ifdef SQ
69360 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f LITE_ENABLE_MEMO
69370 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a RY_MANAGEMENT./*
69380 20 54 72 79 20 74 6f 20 72 65 74 75 72 6e 20 6d Try to return m
69390 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 74 68 emory used by th
693a0 65 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 e pcache module
693b0 74 6f 20 74 68 65 20 6d 61 69 6e 20 6d 65 6d 6f to the main memo
693c0 72 79 20 68 65 61 70 20 2a 2f 0a 53 51 4c 49 54 ry heap */.SQLIT
693d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
693e0 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 lite3PcacheRelea
693f0 73 65 4d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 23 seMemory(int);.#
69400 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
69410 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
69420 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
69430 6c 69 74 65 33 50 63 61 63 68 65 53 74 61 74 73 lite3PcacheStats
69440 28 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c (int*,int*,int*,
69450 69 6e 74 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 int*);.#endif..S
69460 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
69470 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 id sqlite3PCache
69480 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 SetDefault(void)
69490 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50 43 ;..#endif /* _PC
694a0 41 43 48 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a ACHE_H_ */../***
694b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
694c0 6f 66 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a of pcache.h ****
694d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
694e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
694f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
69500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
69510 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
69520 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
69530 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
69540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a **********/../**
69550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
69560 6c 75 64 65 20 6f 73 2e 68 20 69 6e 20 74 68 65 lude os.h in the
69570 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 middle of sqlit
69580 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
69590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
695a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
695b0 69 6e 20 66 69 6c 65 20 6f 73 2e 68 20 2a 2a 2a in file os.h ***
695c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
695d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
695e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
695f0 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
69600 72 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 16.**.** The a
69610 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
69620 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
69630 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
69640 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
69650 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
69660 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
69670 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
69680 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
69690 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
696a0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
696b0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
696c0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
696d0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
696e0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
696f0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
69700 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
69710 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
69720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
69730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
69740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
69750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
69760 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
69770 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 his header file
69780 28 74 6f 67 65 74 68 65 72 20 77 69 74 68 20 69 (together with i
69790 73 20 63 6f 6d 70 61 6e 69 6f 6e 20 43 20 73 6f s companion C so
697a0 75 72 63 65 2d 63 6f 64 65 20 66 69 6c 65 0a 2a urce-code file.*
697b0 2a 20 22 6f 73 2e 63 22 29 20 61 74 74 65 6d 70 * "os.c") attemp
697c0 74 20 74 6f 20 61 62 73 74 72 61 63 74 20 74 68 t to abstract th
697d0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 e underlying ope
697e0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 6f rating system so
697f0 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 53 51 4c that.** the SQL
69800 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c ite library will
69810 20 77 6f 72 6b 20 6f 6e 20 62 6f 74 68 20 50 4f work on both PO
69820 53 49 58 20 61 6e 64 20 77 69 6e 64 6f 77 73 20 SIX and windows
69830 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 systems..**.** T
69840 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 his header file
69850 69 73 20 23 69 6e 63 6c 75 64 65 2d 65 64 20 62 is #include-ed b
69860 79 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 61 6e y sqliteInt.h an
69870 64 20 74 68 75 73 20 65 6e 64 73 20 75 70 0a 2a d thus ends up.*
69880 2a 20 62 65 69 6e 67 20 69 6e 63 6c 75 64 65 64 * being included
69890 20 62 79 20 65 76 65 72 79 20 73 6f 75 72 63 65 by every source
698a0 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 file..*/.#ifnde
698b0 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a f _SQLITE_OS_H_.
698c0 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f #define _SQLITE_
698d0 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46 69 67 OS_H_../*.** Fig
698e0 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 61 72 ure out if we ar
698f0 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 55 e dealing with U
69900 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 6f 72 nix, Windows, or
69910 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 6f some other.** o
69920 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e perating system.
69930 20 20 41 66 74 65 72 20 74 68 65 20 66 6f 6c 6c After the foll
69940 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 70 owing block of p
69950 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72 6f 73 reprocess macros
69960 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 51 4c 49 ,.** all of SQLI
69970 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 51 4c 49 TE_OS_UNIX, SQLI
69980 54 45 5f 4f 53 5f 57 49 4e 2c 20 61 6e 64 20 53 TE_OS_WIN, and S
69990 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20 0a QLITE_OS_OTHER .
699a0 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 64 20 ** will defined
699b0 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72 20 30 to either 1 or 0
699c0 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f . One of the fo
699d0 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 20 54 ur will be 1. T
699e0 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 68 72 he other .** thr
699f0 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a 2f ee will be 0..*/
69a00 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
69a10 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 ITE_OS_OTHER).#
69a20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 if SQLITE_OS_OTH
69a30 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66 20 ER==1.# undef
69a40 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 SQLITE_OS_UNIX.#
69a50 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
69a60 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 75 _OS_UNIX 0.# u
69a70 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 ndef SQLITE_OS_W
69a80 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 IN.# define SQ
69a90 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 LITE_OS_WIN 0.#
69aa0 65 6c 73 65 0a 23 20 20 20 75 6e 64 65 66 20 53 else.# undef S
69ab0 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 0a 23 QLITE_OS_OTHER.#
69ac0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 endif.#endif.#i
69ad0 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
69ae0 45 5f 4f 53 5f 55 4e 49 58 29 20 26 26 20 21 64 E_OS_UNIX) && !d
69af0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 efined(SQLITE_OS
69b00 5f 4f 54 48 45 52 29 0a 23 20 64 65 66 69 6e 65 _OTHER).# define
69b10 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 SQLITE_OS_OTHER
69b20 20 30 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 0.# ifndef SQLI
69b30 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 69 66 TE_OS_WIN.# if
69b40 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 29 defined(_WIN32)
69b50 20 7c 7c 20 64 65 66 69 6e 65 64 28 57 49 4e 33 || defined(WIN3
69b60 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 2) || defined(__
69b70 43 59 47 57 49 4e 5f 5f 29 20 7c 7c 20 64 65 66 CYGWIN__) || def
69b80 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 33 32 5f 5f ined(__MINGW32__
69b90 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 42 ) || defined(__B
69ba0 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 20 20 20 20 ORLANDC__).#
69bb0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
69bc0 53 5f 57 49 4e 20 31 0a 23 20 20 20 20 20 64 65 S_WIN 1.# de
69bd0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 fine SQLITE_OS_U
69be0 4e 49 58 20 30 0a 23 20 20 20 65 6c 73 65 0a 23 NIX 0.# else.#
69bf0 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 define SQLI
69c00 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 TE_OS_WIN 0.#
69c10 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
69c20 4f 53 5f 55 4e 49 58 20 31 0a 23 20 20 65 6e 64 OS_UNIX 1.# end
69c30 69 66 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 if.# else.# def
69c40 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e ine SQLITE_OS_UN
69c50 49 58 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6c IX 0.# endif.#el
69c60 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 se.# ifndef SQLI
69c70 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 65 66 TE_OS_WIN.# def
69c80 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 ine SQLITE_OS_WI
69c90 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 N 0.# endif.#end
69ca0 69 66 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f if..#if SQLITE_O
69cb0 53 5f 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 S_WIN.# include
69cc0 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a 23 65 6e 64 <windows.h>.#end
69cd0 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d if../*.** Determ
69ce0 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 64 65 ine if we are de
69cf0 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64 6f aling with Windo
69d00 77 73 20 4e 54 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 ws NT..**.** We
69d10 6f 75 67 68 74 20 74 6f 20 62 65 20 61 62 6c 65 ought to be able
69d20 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 to determine if
69d30 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e we are compilin
69d40 67 20 66 6f 72 20 77 69 6e 39 38 20 6f 72 20 77 g for win98 or w
69d50 69 6e 4e 54 0a 2a 2a 20 75 73 69 6e 67 20 74 68 inNT.** using th
69d60 65 20 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 20 6d e _WIN32_WINNT m
69d70 61 63 72 6f 20 61 73 20 66 6f 6c 6c 6f 77 73 3a acro as follows:
69d80 0a 2a 2a 0a 2a 2a 20 23 69 66 20 64 65 66 69 6e .**.** #if defin
69d90 65 64 28 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 29 ed(_WIN32_WINNT)
69da0 0a 2a 2a 20 23 20 64 65 66 69 6e 65 20 53 51 4c .** # define SQL
69db0 49 54 45 5f 4f 53 5f 57 49 4e 4e 54 20 31 0a 2a ITE_OS_WINNT 1.*
69dc0 2a 20 23 65 6c 73 65 0a 2a 2a 20 23 20 64 65 66 * #else.** # def
69dd0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 ine SQLITE_OS_WI
69de0 4e 4e 54 20 30 0a 2a 2a 20 23 65 6e 64 69 66 0a NNT 0.** #endif.
69df0 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 76 **.** However, v
69e00 73 32 30 30 35 20 64 6f 65 73 20 6e 6f 74 20 73 s2005 does not s
69e10 65 74 20 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 20 et _WIN32_WINNT
69e20 62 79 20 64 65 66 61 75 6c 74 2c 20 61 73 20 69 by default, as i
69e30 74 20 6f 75 67 68 74 20 74 6f 2c 0a 2a 2a 20 73 t ought to,.** s
69e40 6f 20 74 68 65 20 61 62 6f 76 65 20 74 65 73 74 o the above test
69e50 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 2e 20 does not work.
69e60 20 57 65 27 6c 6c 20 6a 75 73 74 20 61 73 73 75 We'll just assu
69e70 6d 65 20 74 68 61 74 20 65 76 65 72 79 74 68 69 me that everythi
69e80 6e 67 20 69 73 0a 2a 2a 20 77 69 6e 4e 54 20 75 ng is.** winNT u
69e90 6e 6c 65 73 73 20 74 68 65 20 70 72 6f 67 72 61 nless the progra
69ea0 6d 6d 65 72 20 65 78 70 6c 69 63 69 74 6c 79 20 mmer explicitly
69eb0 73 61 79 73 20 6f 74 68 65 72 77 69 73 65 20 62 says otherwise b
69ec0 79 20 73 65 74 74 69 6e 67 0a 2a 2a 20 53 51 4c y setting.** SQL
69ed0 49 54 45 5f 4f 53 5f 57 49 4e 4e 54 20 74 6f 20 ITE_OS_WINNT to
69ee0 30 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 0..*/.#if SQLITE
69ef0 5f 4f 53 5f 57 49 4e 20 26 26 20 21 64 65 66 69 _OS_WIN && !defi
69f00 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 57 49 ned(SQLITE_OS_WI
69f10 4e 4e 54 29 0a 23 20 64 65 66 69 6e 65 20 53 51 NNT).# define SQ
69f20 4c 49 54 45 5f 4f 53 5f 57 49 4e 4e 54 20 31 0a LITE_OS_WINNT 1.
69f30 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 #endif../*.** De
69f40 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 61 72 termine if we ar
69f50 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 57 e dealing with W
69f60 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 63 68 indowsCE - which
69f70 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 has a much.** r
69f80 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 educed API..*/.#
69f90 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 if defined(_WIN3
69fa0 32 5f 57 43 45 29 0a 23 20 64 65 66 69 6e 65 20 2_WCE).# define
69fb0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 SQLITE_OS_WINCE
69fc0 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 1.#else.# define
69fd0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
69fe0 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0.#endif../*.**
69ff0 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 Determine if we
6a000 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 are dealing wit
6a010 68 20 57 69 6e 52 54 2c 20 77 68 69 63 68 20 70 h WinRT, which p
6a020 72 6f 76 69 64 65 73 20 6f 6e 6c 79 20 61 20 73 rovides only a s
6a030 75 62 73 65 74 20 6f 66 0a 2a 2a 20 74 68 65 20 ubset of.** the
6a040 66 75 6c 6c 20 57 69 6e 33 32 20 41 50 49 2e 0a full Win32 API..
6a050 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 */.#if !defined(
6a060 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 29 SQLITE_OS_WINRT)
6a070 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
6a080 5f 4f 53 5f 57 49 4e 52 54 20 30 0a 23 65 6e 64 _OS_WINRT 0.#end
6a090 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 63 if../*.** When c
6a0a0 6f 6d 70 69 6c 65 64 20 66 6f 72 20 57 69 6e 43 ompiled for WinC
6a0b0 45 20 6f 72 20 57 69 6e 52 54 2c 20 74 68 65 72 E or WinRT, ther
6a0c0 65 20 69 73 20 6e 6f 20 63 6f 6e 63 65 70 74 20 e is no concept
6a0d0 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a of the current.*
6a0e0 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a 20 2a 2f * directory.. */
6a0f0 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f .#if !SQLITE_OS_
6a100 57 49 4e 43 45 20 26 26 20 21 53 51 4c 49 54 45 WINCE && !SQLITE
6a110 5f 4f 53 5f 57 49 4e 52 54 0a 23 20 64 65 66 69 _OS_WINRT.# defi
6a120 6e 65 20 53 51 4c 49 54 45 5f 43 55 52 44 49 52 ne SQLITE_CURDIR
6a130 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 66 1.#endif../* If
6a140 20 74 68 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e the SET_FULLSYN
6a150 43 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 20 64 C macro is not d
6a160 65 66 69 6e 65 64 20 61 62 6f 76 65 2c 20 74 68 efined above, th
6a170 65 6e 20 6d 61 6b 65 20 69 74 0a 2a 2a 20 61 20 en make it.** a
6a180 6e 6f 2d 6f 70 0a 2a 2f 0a 23 69 66 6e 64 65 66 no-op.*/.#ifndef
6a190 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 0a 23 20 SET_FULLSYNC.#
6a1a0 64 65 66 69 6e 65 20 53 45 54 5f 46 55 4c 4c 53 define SET_FULLS
6a1b0 59 4e 43 28 78 2c 79 29 0a 23 65 6e 64 69 66 0a YNC(x,y).#endif.
6a1c0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 ./*.** The defau
6a1d0 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 64 69 73 lt size of a dis
6a1e0 6b 20 73 65 63 74 6f 72 0a 2a 2f 0a 23 69 66 6e k sector.*/.#ifn
6a1f0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 def SQLITE_DEFAU
6a200 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a 23 LT_SECTOR_SIZE.#
6a210 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 define SQLITE_D
6a220 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 EFAULT_SECTOR_SI
6a230 5a 45 20 34 30 39 36 0a 23 65 6e 64 69 66 0a 0a ZE 4096.#endif..
6a240 2f 2a 0a 2a 2a 20 54 65 6d 70 6f 72 61 72 79 20 /*.** Temporary
6a250 66 69 6c 65 73 20 61 72 65 20 6e 61 6d 65 64 20 files are named
6a260 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 74 68 starting with th
6a270 69 73 20 70 72 65 66 69 78 20 66 6f 6c 6c 6f 77 is prefix follow
6a280 65 64 20 62 79 20 31 36 20 72 61 6e 64 6f 6d 0a ed by 16 random.
6a290 2a 2a 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 ** alphanumeric
6a2a0 63 68 61 72 61 63 74 65 72 73 2c 20 61 6e 64 20 characters, and
6a2b0 6e 6f 20 66 69 6c 65 20 65 78 74 65 6e 73 69 6f no file extensio
6a2c0 6e 2e 20 54 68 65 79 20 61 72 65 20 73 74 6f 72 n. They are stor
6a2d0 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 4f 53 27 ed in the.** OS'
6a2e0 73 20 73 74 61 6e 64 61 72 64 20 74 65 6d 70 6f s standard tempo
6a2f0 72 61 72 79 20 66 69 6c 65 20 64 69 72 65 63 74 rary file direct
6a300 6f 72 79 2c 20 61 6e 64 20 61 72 65 20 64 65 6c ory, and are del
6a310 65 74 65 64 20 70 72 69 6f 72 20 74 6f 20 65 78 eted prior to ex
6a320 69 74 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 it..** If sqlite
6a330 20 69 73 20 62 65 69 6e 67 20 65 6d 62 65 64 64 is being embedd
6a340 65 64 20 69 6e 20 61 6e 6f 74 68 65 72 20 70 72 ed in another pr
6a350 6f 67 72 61 6d 2c 20 79 6f 75 20 6d 61 79 20 77 ogram, you may w
6a360 69 73 68 20 74 6f 20 63 68 61 6e 67 65 20 74 68 ish to change th
6a370 65 0a 2a 2a 20 70 72 65 66 69 78 20 74 6f 20 72 e.** prefix to r
6a380 65 66 6c 65 63 74 20 79 6f 75 72 20 70 72 6f 67 eflect your prog
6a390 72 61 6d 27 73 20 6e 61 6d 65 2c 20 73 6f 20 74 ram's name, so t
6a3a0 68 61 74 20 69 66 20 79 6f 75 72 20 70 72 6f 67 hat if your prog
6a3b0 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 70 72 65 ram exits.** pre
6a3c0 6d 61 74 75 72 65 6c 79 2c 20 6f 6c 64 20 74 65 maturely, old te
6a3d0 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 63 61 mporary files ca
6a3e0 6e 20 62 65 20 65 61 73 69 6c 79 20 69 64 65 6e n be easily iden
6a3f0 74 69 66 69 65 64 2e 20 54 68 69 73 20 63 61 6e tified. This can
6a400 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 75 73 69 6e be done.** usin
6a410 67 20 2d 44 53 51 4c 49 54 45 5f 54 45 4d 50 5f g -DSQLITE_TEMP_
6a420 46 49 4c 45 5f 50 52 45 46 49 58 3d 6d 79 70 72 FILE_PREFIX=mypr
6a430 65 66 69 78 5f 20 6f 6e 20 74 68 65 20 63 6f 6d efix_ on the com
6a440 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 piler command li
6a450 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 36 2d 31 ne..**.** 2006-1
6a460 30 2d 33 31 3a 20 20 54 68 65 20 64 65 66 61 75 0-31: The defau
6a470 6c 74 20 70 72 65 66 69 78 20 75 73 65 64 20 74 lt prefix used t
6a480 6f 20 62 65 20 22 73 71 6c 69 74 65 5f 22 2e 20 o be "sqlite_".
6a490 20 42 75 74 20 74 68 65 6e 0a 2a 2a 20 4d 63 61 But then.** Mca
6a4a0 66 65 65 20 73 74 61 72 74 65 64 20 75 73 69 6e fee started usin
6a4b0 67 20 53 51 4c 69 74 65 20 69 6e 20 74 68 65 69 g SQLite in thei
6a4c0 72 20 61 6e 74 69 2d 76 69 72 75 73 20 70 72 6f r anti-virus pro
6a4d0 64 75 63 74 20 61 6e 64 20 69 74 0a 2a 2a 20 73 duct and it.** s
6a4e0 74 61 72 74 65 64 20 70 75 74 74 69 6e 67 20 66 tarted putting f
6a4f0 69 6c 65 73 20 77 69 74 68 20 74 68 65 20 22 73 iles with the "s
6a500 71 6c 69 74 65 22 20 6e 61 6d 65 20 69 6e 20 74 qlite" name in t
6a510 68 65 20 63 3a 2f 74 65 6d 70 20 66 6f 6c 64 65 he c:/temp folde
6a520 72 2e 0a 2a 2a 20 54 68 69 73 20 61 6e 6e 6f 79 r..** This annoy
6a530 65 64 20 6d 61 6e 79 20 77 69 6e 64 6f 77 73 20 ed many windows
6a540 75 73 65 72 73 2e 20 20 54 68 6f 73 65 20 75 73 users. Those us
6a550 65 72 73 20 77 6f 75 6c 64 20 74 68 65 6e 20 64 ers would then d
6a560 6f 20 61 20 0a 2a 2a 20 47 6f 6f 67 6c 65 20 73 o a .** Google s
6a570 65 61 72 63 68 20 66 6f 72 20 22 73 71 6c 69 74 earch for "sqlit
6a580 65 22 2c 20 66 69 6e 64 20 74 68 65 20 74 65 6c e", find the tel
6a590 65 70 68 6f 6e 65 20 6e 75 6d 62 65 72 73 20 6f ephone numbers o
6a5a0 66 20 74 68 65 0a 2a 2a 20 64 65 76 65 6c 6f 70 f the.** develop
6a5b0 65 72 73 20 61 6e 64 20 63 61 6c 6c 20 74 6f 20 ers and call to
6a5c0 77 61 6b 65 20 74 68 65 6d 20 75 70 20 61 74 20 wake them up at
6a5d0 6e 69 67 68 74 20 61 6e 64 20 63 6f 6d 70 6c 61 night and compla
6a5e0 69 6e 2e 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 in..** For this
6a5f0 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 65 66 61 reason, the defa
6a600 75 6c 74 20 6e 61 6d 65 20 70 72 65 66 69 78 20 ult name prefix
6a610 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 62 65 is changed to be
6a620 20 22 73 71 6c 69 74 65 22 20 0a 2a 2a 20 73 70 "sqlite" .** sp
6a630 65 6c 6c 65 64 20 62 61 63 6b 77 61 72 64 73 2e elled backwards.
6a640 20 20 53 6f 20 74 68 65 20 74 65 6d 70 20 66 69 So the temp fi
6a650 6c 65 73 20 61 72 65 20 73 74 69 6c 6c 20 69 64 les are still id
6a660 65 6e 74 69 66 69 65 64 2c 20 62 75 74 0a 2a 2a entified, but.**
6a670 20 61 6e 79 62 6f 64 79 20 73 6d 61 72 74 20 65 anybody smart e
6a680 6e 6f 75 67 68 20 74 6f 20 66 69 67 75 72 65 20 nough to figure
6a690 6f 75 74 20 74 68 65 20 63 6f 64 65 20 69 73 20 out the code is
6a6a0 61 6c 73 6f 20 6c 69 6b 65 6c 79 20 73 6d 61 72 also likely smar
6a6b0 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 6b t.** enough to k
6a6c0 6e 6f 77 20 74 68 61 74 20 63 61 6c 6c 69 6e 67 now that calling
6a6d0 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 20 77 the developer w
6a6e0 69 6c 6c 20 6e 6f 74 20 68 65 6c 70 20 67 65 74 ill not help get
6a6f0 20 72 69 64 0a 2a 2a 20 6f 66 20 74 68 65 20 66 rid.** of the f
6a700 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ile..*/.#ifndef
6a710 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 SQLITE_TEMP_FILE
6a720 5f 50 52 45 46 49 58 0a 23 20 64 65 66 69 6e 65 _PREFIX.# define
6a730 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c SQLITE_TEMP_FIL
6a740 45 5f 50 52 45 46 49 58 20 22 65 74 69 6c 71 73 E_PREFIX "etilqs
6a750 5f 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a _".#endif../*.**
6a760 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 The following v
6a770 61 6c 75 65 73 20 6d 61 79 20 62 65 20 70 61 73 alues may be pas
6a780 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
6a790 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a d argument to.**
6a7a0 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 sqlite3OsLock()
6a7b0 2e 20 54 68 65 20 76 61 72 69 6f 75 73 20 6c 6f . The various lo
6a7c0 63 6b 73 20 65 78 68 69 62 69 74 20 74 68 65 20 cks exhibit the
6a7d0 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 6d 61 6e 74 following semant
6a7e0 69 63 73 3a 0a 2a 2a 0a 2a 2a 20 53 48 41 52 45 ics:.**.** SHARE
6a7f0 44 3a 20 20 20 20 41 6e 79 20 6e 75 6d 62 65 72 D: Any number
6a800 20 6f 66 20 70 72 6f 63 65 73 73 65 73 20 6d 61 of processes ma
6a810 79 20 68 6f 6c 64 20 61 20 53 48 41 52 45 44 20 y hold a SHARED
6a820 6c 6f 63 6b 20 73 69 6d 75 6c 74 61 6e 65 6f 75 lock simultaneou
6a830 73 6c 79 2e 0a 2a 2a 20 52 45 53 45 52 56 45 44 sly..** RESERVED
6a840 3a 20 20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 : A single proc
6a850 65 73 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 52 ess may hold a R
6a860 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 ESERVED lock on
6a870 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20 a file at.**
6a880 20 20 20 20 20 20 20 20 61 6e 79 20 74 69 6d 65 any time
6a890 2e 20 4f 74 68 65 72 20 70 72 6f 63 65 73 73 65 . Other processe
6a8a0 73 20 6d 61 79 20 68 6f 6c 64 20 61 6e 64 20 6f s may hold and o
6a8b0 62 74 61 69 6e 20 6e 65 77 20 53 48 41 52 45 44 btain new SHARED
6a8c0 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 50 45 4e 44 49 locks..** PENDI
6a8d0 4e 47 3a 20 20 20 41 20 73 69 6e 67 6c 65 20 70 NG: A single p
6a8e0 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c 64 20 rocess may hold
6a8f0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f a PENDING lock o
6a900 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 20 n a file at.**
6a910 20 20 20 20 20 20 20 20 20 20 61 6e 79 20 6f 6e any on
6a920 65 20 74 69 6d 65 2e 20 45 78 69 73 74 69 6e 67 e time. Existing
6a930 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 SHARED locks ma
6a940 79 20 70 65 72 73 69 73 74 2c 20 62 75 74 20 6e y persist, but n
6a950 6f 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20 o new.**
6a960 20 20 20 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 SHARED locks
6a970 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 may be obtained
6a980 20 62 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 by other proces
6a990 73 65 73 2e 0a 2a 2a 20 45 58 43 4c 55 53 49 56 ses..** EXCLUSIV
6a9a0 45 3a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 20 E: An EXCLUSIVE
6a9b0 6c 6f 63 6b 20 70 72 65 63 6c 75 64 65 73 20 61 lock precludes a
6a9c0 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 2e 0a ll other locks..
6a9d0 2a 2a 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 4c 4f **.** PENDING_LO
6a9e0 43 4b 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 61 CK may not be pa
6a9f0 73 73 65 64 20 64 69 72 65 63 74 6c 79 20 74 6f ssed directly to
6aa00 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 sqlite3OsLock()
6aa10 2e 20 49 6e 73 74 65 61 64 2c 20 61 0a 2a 2a 20 . Instead, a.**
6aa20 70 72 6f 63 65 73 73 20 74 68 61 74 20 72 65 71 process that req
6aa30 75 65 73 74 73 20 61 6e 20 45 58 43 4c 55 53 49 uests an EXCLUSI
6aa40 56 45 20 6c 6f 63 6b 20 6d 61 79 20 61 63 74 75 VE lock may actu
6aa50 61 6c 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 45 ally obtain a PE
6aa60 4e 44 49 4e 47 0a 2a 2a 20 6c 6f 63 6b 2e 20 54 NDING.** lock. T
6aa70 68 69 73 20 63 61 6e 20 62 65 20 75 70 67 72 61 his can be upgra
6aa80 64 65 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 ded to an EXCLUS
6aa90 49 56 45 20 6c 6f 63 6b 20 62 79 20 61 20 73 75 IVE lock by a su
6aaa0 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f bsequent call to
6aab0 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 .** sqlite3OsLoc
6aac0 6b 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 k()..*/.#define
6aad0 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 NO_LOCK
6aae0 30 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 0.#define SHARED
6aaf0 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 66 _LOCK 1.#def
6ab00 69 6e 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 ine RESERVED_LOC
6ab10 4b 20 20 20 32 0a 23 64 65 66 69 6e 65 20 50 45 K 2.#define PE
6ab20 4e 44 49 4e 47 5f 4c 4f 43 4b 20 20 20 20 33 0a NDING_LOCK 3.
6ab30 23 64 65 66 69 6e 65 20 45 58 43 4c 55 53 49 56 #define EXCLUSIV
6ab40 45 5f 4c 4f 43 4b 20 20 34 0a 0a 2f 2a 0a 2a 2a E_LOCK 4../*.**
6ab50 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 4e 6f File Locking No
6ab60 74 65 73 3a 20 20 28 4d 6f 73 74 6c 79 20 61 62 tes: (Mostly ab
6ab70 6f 75 74 20 77 69 6e 64 6f 77 73 20 62 75 74 20 out windows but
6ab80 61 6c 73 6f 20 73 6f 6d 65 20 69 6e 66 6f 20 66 also some info f
6ab90 6f 72 20 55 6e 69 78 29 0a 2a 2a 0a 2a 2a 20 57 or Unix).**.** W
6aba0 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 4c 6f 63 e cannot use Loc
6abb0 6b 46 69 6c 65 45 78 28 29 20 6f 72 20 55 6e 6c kFileEx() or Unl
6abc0 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f 6e 20 57 ockFileEx() on W
6abd0 69 6e 39 35 2f 39 38 2f 4d 45 20 62 65 63 61 75 in95/98/ME becau
6abe0 73 65 0a 2a 2a 20 74 68 6f 73 65 20 66 75 6e 63 se.** those func
6abf0 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 76 tions are not av
6ac00 61 69 6c 61 62 6c 65 2e 20 20 53 6f 20 77 65 20 ailable. So we
6ac10 75 73 65 20 6f 6e 6c 79 20 4c 6f 63 6b 46 69 6c use only LockFil
6ac20 65 28 29 20 61 6e 64 0a 2a 2a 20 55 6e 6c 6f 63 e() and.** Unloc
6ac30 6b 46 69 6c 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 4c kFile()..**.** L
6ac40 6f 63 6b 46 69 6c 65 28 29 20 70 72 65 76 65 6e ockFile() preven
6ac50 74 73 20 6e 6f 74 20 6a 75 73 74 20 77 72 69 74 ts not just writ
6ac60 69 6e 67 20 62 75 74 20 61 6c 73 6f 20 72 65 61 ing but also rea
6ac70 64 69 6e 67 20 62 79 20 6f 74 68 65 72 20 70 72 ding by other pr
6ac80 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 41 20 53 48 ocesses..** A SH
6ac90 41 52 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 ARED_LOCK is obt
6aca0 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 ained by locking
6acb0 20 61 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d a single random
6acc0 6c 79 2d 63 68 6f 73 65 6e 20 0a 2a 2a 20 62 79 ly-chosen .** by
6acd0 74 65 20 6f 75 74 20 6f 66 20 61 20 73 70 65 63 te out of a spec
6ace0 69 66 69 63 20 72 61 6e 67 65 20 6f 66 20 62 79 ific range of by
6acf0 74 65 73 2e 20 54 68 65 20 6c 6f 63 6b 20 62 79 tes. The lock by
6ad00 74 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 61 te is obtained a
6ad10 74 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 73 6f 20 t .** random so
6ad20 74 77 6f 20 73 65 70 61 72 61 74 65 20 72 65 61 two separate rea
6ad30 64 65 72 73 20 63 61 6e 20 70 72 6f 62 61 62 6c ders can probabl
6ad40 79 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c y access the fil
6ad50 65 20 61 74 20 74 68 65 20 0a 2a 2a 20 73 61 6d e at the .** sam
6ad60 65 20 74 69 6d 65 2c 20 75 6e 6c 65 73 73 20 74 e time, unless t
6ad70 68 65 79 20 61 72 65 20 75 6e 6c 75 63 6b 79 20 hey are unlucky
6ad80 61 6e 64 20 63 68 6f 6f 73 65 20 74 68 65 20 73 and choose the s
6ad90 61 6d 65 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a ame lock byte..*
6ada0 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 5f 4c * An EXCLUSIVE_L
6adb0 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 OCK is obtained
6adc0 62 79 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 62 by locking all b
6add0 79 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67 ytes in the rang
6ade0 65 2e 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 e..** There can
6adf0 6f 6e 6c 79 20 62 65 20 6f 6e 65 20 77 72 69 74 only be one writ
6ae00 65 72 2e 20 20 41 20 52 45 53 45 52 56 45 44 5f er. A RESERVED_
6ae10 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 LOCK is obtained
6ae20 20 62 79 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 61 by locking.** a
6ae30 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66 20 single byte of
6ae40 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 69 73 the file that is
6ae50 20 64 65 73 69 67 6e 61 74 65 64 20 61 73 20 74 designated as t
6ae60 68 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b he reserved lock
6ae70 20 62 79 74 65 2e 0a 2a 2a 20 41 20 50 45 4e 44 byte..** A PEND
6ae80 49 4e 47 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 ING_LOCK is obta
6ae90 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 ined by locking
6aea0 61 20 64 65 73 69 67 6e 61 74 65 64 20 62 79 74 a designated byt
6aeb0 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d e different from
6aec0 0a 2a 2a 20 74 68 65 20 52 45 53 45 52 56 45 44 .** the RESERVED
6aed0 5f 4c 4f 43 4b 20 62 79 74 65 2e 0a 2a 2a 0a 2a _LOCK byte..**.*
6aee0 2a 20 4f 6e 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 * On WinNT/2K/XP
6aef0 20 73 79 73 74 65 6d 73 2c 20 4c 6f 63 6b 46 69 systems, LockFi
6af00 6c 65 45 78 28 29 20 61 6e 64 20 55 6e 6c 6f 63 leEx() and Unloc
6af10 6b 46 69 6c 65 45 78 28 29 20 61 72 65 20 61 76 kFileEx() are av
6af20 61 69 6c 61 62 6c 65 2c 0a 2a 2a 20 77 68 69 63 ailable,.** whic
6af30 68 20 6d 65 61 6e 73 20 77 65 20 63 61 6e 20 75 h means we can u
6af40 73 65 20 72 65 61 64 65 72 2f 77 72 69 74 65 72 se reader/writer
6af50 20 6c 6f 63 6b 73 2e 20 20 57 68 65 6e 20 72 65 locks. When re
6af60 61 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b ader/writer lock
6af70 73 0a 2a 2a 20 61 72 65 20 75 73 65 64 2c 20 74 s.** are used, t
6af80 68 65 20 6c 6f 63 6b 20 69 73 20 70 6c 61 63 65 he lock is place
6af90 64 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 72 61 d on the same ra
6afa0 6e 67 65 20 6f 66 20 62 79 74 65 73 20 74 68 61 nge of bytes tha
6afb0 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 66 6f 72 t is used.** for
6afc0 20 70 72 6f 62 61 62 69 6c 69 73 74 69 63 20 6c probabilistic l
6afd0 6f 63 6b 69 6e 67 20 69 6e 20 57 69 6e 39 35 2f ocking in Win95/
6afe0 39 38 2f 4d 45 2e 20 20 48 65 6e 63 65 2c 20 74 98/ME. Hence, t
6aff0 68 65 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d he locking schem
6b000 65 0a 2a 2a 20 77 69 6c 6c 20 73 75 70 70 6f 72 e.** will suppor
6b010 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69 t two or more Wi
6b020 6e 39 35 20 72 65 61 64 65 72 73 20 6f 72 20 74 n95 readers or t
6b030 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69 6e 4e 54 wo or more WinNT
6b040 20 72 65 61 64 65 72 73 2e 0a 2a 2a 20 42 75 74 readers..** But
6b050 20 61 20 73 69 6e 67 6c 65 20 57 69 6e 39 35 20 a single Win95
6b060 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b reader will lock
6b070 20 6f 75 74 20 61 6c 6c 20 57 69 6e 4e 54 20 72 out all WinNT r
6b080 65 61 64 65 72 73 20 61 6e 64 20 61 20 73 69 6e eaders and a sin
6b090 67 6c 65 0a 2a 2a 20 57 69 6e 4e 54 20 72 65 61 gle.** WinNT rea
6b0a0 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 der will lock ou
6b0b0 74 20 61 6c 6c 20 6f 74 68 65 72 20 57 69 6e 39 t all other Win9
6b0c0 35 20 72 65 61 64 65 72 73 2e 0a 2a 2a 0a 2a 2a 5 readers..**.**
6b0d0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 23 The following #
6b0e0 64 65 66 69 6e 65 73 20 73 70 65 63 69 66 79 20 defines specify
6b0f0 74 68 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 the range of byt
6b100 65 73 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b es used for lock
6b110 69 6e 67 2e 0a 2a 2a 20 53 48 41 52 45 44 5f 53 ing..** SHARED_S
6b120 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65 IZE is the numbe
6b130 72 20 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c r of bytes avail
6b140 61 62 6c 65 20 69 6e 20 74 68 65 20 70 6f 6f 6c able in the pool
6b150 20 66 72 6f 6d 20 77 68 69 63 68 0a 2a 2a 20 61 from which.** a
6b160 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 69 73 20 random byte is
6b170 73 65 6c 65 63 74 65 64 20 66 6f 72 20 61 20 73 selected for a s
6b180 68 61 72 65 64 20 6c 6f 63 6b 2e 20 20 54 68 65 hared lock. The
6b190 20 70 6f 6f 6c 20 6f 66 20 62 79 74 65 73 20 66 pool of bytes f
6b1a0 6f 72 0a 2a 2a 20 73 68 61 72 65 64 20 6c 6f 63 or.** shared loc
6b1b0 6b 73 20 62 65 67 69 6e 73 20 61 74 20 53 48 41 ks begins at SHA
6b1c0 52 45 44 5f 46 49 52 53 54 2e 20 0a 2a 2a 0a 2a RED_FIRST. .**.*
6b1d0 2a 20 54 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 * The same locki
6b1e0 6e 67 20 73 74 72 61 74 65 67 79 20 61 6e 64 0a ng strategy and.
6b1f0 2a 2a 20 62 79 74 65 20 72 61 6e 67 65 73 20 61 ** byte ranges a
6b200 72 65 20 75 73 65 64 20 66 6f 72 20 55 6e 69 78 re used for Unix
6b210 2e 20 20 54 68 69 73 20 6c 65 61 76 65 73 20 6f . This leaves o
6b220 70 65 6e 20 74 68 65 20 70 6f 73 73 69 62 6c 69 pen the possibli
6b230 74 79 20 6f 66 20 68 61 76 69 6e 67 0a 2a 2a 20 ty of having.**
6b240 63 6c 69 65 6e 74 73 20 6f 6e 20 77 69 6e 39 35 clients on win95
6b250 2c 20 77 69 6e 4e 54 2c 20 61 6e 64 20 75 6e 69 , winNT, and uni
6b260 78 20 61 6c 6c 20 74 61 6c 6b 69 6e 67 20 74 6f x all talking to
6b270 20 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 64 the same shared
6b280 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 61 6c 6c file.** and all
6b290 20 6c 6f 63 6b 69 6e 67 20 63 6f 72 72 65 63 74 locking correct
6b2a0 6c 79 2e 20 20 54 6f 20 64 6f 20 73 6f 20 77 6f ly. To do so wo
6b2b0 75 6c 64 20 72 65 71 75 69 72 65 20 74 68 61 74 uld require that
6b2c0 20 73 61 6d 62 61 20 28 6f 72 20 77 68 61 74 65 samba (or whate
6b2d0 76 65 72 0a 2a 2a 20 74 6f 6f 6c 20 69 73 20 62 ver.** tool is b
6b2e0 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 66 69 eing used for fi
6b2f0 6c 65 20 73 68 61 72 69 6e 67 29 20 69 6d 70 6c le sharing) impl
6b300 65 6d 65 6e 74 73 20 6c 6f 63 6b 73 20 63 6f 72 ements locks cor
6b310 72 65 63 74 6c 79 20 62 65 74 77 65 65 6e 0a 2a rectly between.*
6b320 2a 20 77 69 6e 64 6f 77 73 20 61 6e 64 20 75 6e * windows and un
6b330 69 78 2e 20 20 49 27 6d 20 67 75 65 73 73 69 6e ix. I'm guessin
6b340 67 20 74 68 61 74 20 69 73 6e 27 74 20 6c 69 6b g that isn't lik
6b350 65 6c 79 20 74 6f 20 68 61 70 70 65 6e 2c 20 62 ely to happen, b
6b360 75 74 20 62 79 0a 2a 2a 20 75 73 69 6e 67 20 74 ut by.** using t
6b370 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 he same locking
6b380 72 61 6e 67 65 20 77 65 20 61 72 65 20 61 74 20 range we are at
6b390 6c 65 61 73 74 20 6f 70 65 6e 20 74 6f 20 74 68 least open to th
6b3a0 65 20 70 6f 73 73 69 62 69 6c 69 74 79 2e 0a 2a e possibility..*
6b3b0 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e 67 20 69 6e 20 *.** Locking in
6b3c0 77 69 6e 64 6f 77 73 20 69 73 20 6d 61 6e 64 69 windows is mandi
6b3d0 74 6f 72 79 2e 20 20 46 6f 72 20 74 68 69 73 20 tory. For this
6b3e0 72 65 61 73 6f 6e 2c 20 77 65 20 63 61 6e 6e 6f reason, we canno
6b3f0 74 20 73 74 6f 72 65 0a 2a 2a 20 61 63 74 75 61 t store.** actua
6b400 6c 20 64 61 74 61 20 69 6e 20 74 68 65 20 62 79 l data in the by
6b410 74 65 73 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 tes used for loc
6b420 6b 69 6e 67 2e 20 20 54 68 65 20 70 61 67 65 72 king. The pager
6b430 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 73 never allocates
6b440 0a 2a 2a 20 74 68 65 20 70 61 67 65 73 20 69 6e .** the pages in
6b450 76 6f 6c 76 65 64 20 69 6e 20 6c 6f 63 6b 69 6e volved in lockin
6b460 67 20 74 68 65 72 65 66 6f 72 65 2e 20 20 53 48 g therefore. SH
6b470 41 52 45 44 5f 53 49 5a 45 20 69 73 20 73 65 6c ARED_SIZE is sel
6b480 65 63 74 65 64 20 73 6f 0a 2a 2a 20 74 68 61 74 ected so.** that
6b490 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 all locks will
6b4a0 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 fit on a single
6b4b0 70 61 67 65 20 65 76 65 6e 20 61 74 20 74 68 65 page even at the
6b4c0 20 6d 69 6e 69 6d 75 6d 20 70 61 67 65 20 73 69 minimum page si
6b4d0 7a 65 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 ze..** PENDING_B
6b4e0 59 54 45 20 64 65 66 69 6e 65 73 20 74 68 65 20 YTE defines the
6b4f0 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 beginning of the
6b500 20 6c 6f 63 6b 73 2e 20 20 42 79 20 64 65 66 61 locks. By defa
6b510 75 6c 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 ult PENDING_BYTE
6b520 0a 2a 2a 20 69 73 20 73 65 74 20 68 69 67 68 20 .** is set high
6b530 73 6f 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74 so that we don't
6b540 20 68 61 76 65 20 74 6f 20 61 6c 6c 6f 63 61 74 have to allocat
6b550 65 20 61 6e 20 75 6e 75 73 65 64 20 70 61 67 65 e an unused page
6b560 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f 72 20 76 except.** for v
6b570 65 72 79 20 6c 61 72 67 65 20 64 61 74 61 62 61 ery large databa
6b580 73 65 73 2e 20 20 42 75 74 20 6f 6e 65 20 73 68 ses. But one sh
6b590 6f 75 6c 64 20 74 65 73 74 20 74 68 65 20 70 61 ould test the pa
6b5a0 67 65 20 73 6b 69 70 70 69 6e 67 20 6c 6f 67 69 ge skipping logi
6b5b0 63 20 0a 2a 2a 20 62 79 20 73 65 74 74 69 6e 67 c .** by setting
6b5c0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6c 6f PENDING_BYTE lo
6b5d0 77 20 61 6e 64 20 72 75 6e 6e 69 6e 67 20 74 68 w and running th
6b5e0 65 20 65 6e 74 69 72 65 20 72 65 67 72 65 73 73 e entire regress
6b5f0 69 6f 6e 20 73 75 69 74 65 2e 0a 2a 2a 0a 2a 2a ion suite..**.**
6b600 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 76 61 Changing the va
6b610 6c 75 65 20 6f 66 20 50 45 4e 44 49 4e 47 5f 42 lue of PENDING_B
6b620 59 54 45 20 72 65 73 75 6c 74 73 20 69 6e 20 61 YTE results in a
6b630 20 73 75 62 74 6c 79 20 69 6e 63 6f 6d 70 61 74 subtly incompat
6b640 69 62 6c 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 ible.** file for
6b650 6d 61 74 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 mat. Depending
6b660 6f 6e 20 68 6f 77 20 69 74 20 69 73 20 63 68 61 on how it is cha
6b670 6e 67 65 64 2c 20 79 6f 75 20 6d 69 67 68 74 20 nged, you might
6b680 6e 6f 74 20 6e 6f 74 69 63 65 0a 2a 2a 20 74 68 not notice.** th
6b690 65 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 e incompatibilit
6b6a0 79 20 72 69 67 68 74 20 61 77 61 79 2c 20 65 76 y right away, ev
6b6b0 65 6e 20 72 75 6e 6e 69 6e 67 20 61 20 66 75 6c en running a ful
6b6c0 6c 20 72 65 67 72 65 73 73 69 6f 6e 20 74 65 73 l regression tes
6b6d0 74 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c t..** The defaul
6b6e0 74 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 50 45 t location of PE
6b6f0 4e 44 49 4e 47 5f 42 59 54 45 20 69 73 20 74 68 NDING_BYTE is th
6b700 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73 e first byte pas
6b710 74 20 74 68 65 0a 2a 2a 20 31 47 42 20 62 6f 75 t the.** 1GB bou
6b720 6e 64 61 72 79 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 ndary..**.*/.#if
6b730 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
6b740 57 53 44 0a 23 20 64 65 66 69 6e 65 20 50 45 4e WSD.# define PEN
6b750 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 28 30 DING_BYTE (0
6b760 78 34 30 30 30 30 30 30 30 29 0a 23 65 6c 73 65 x40000000).#else
6b770 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e .# define PENDIN
6b780 47 5f 42 59 54 45 20 20 20 20 20 20 73 71 6c 69 G_BYTE sqli
6b790 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 0a 23 te3PendingByte.#
6b7a0 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 52 45 endif.#define RE
6b7b0 53 45 52 56 45 44 5f 42 59 54 45 20 20 20 20 20 SERVED_BYTE
6b7c0 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 29 (PENDING_BYTE+1)
6b7d0 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f .#define SHARED_
6b7e0 46 49 52 53 54 20 20 20 20 20 20 28 50 45 4e 44 FIRST (PEND
6b7f0 49 4e 47 5f 42 59 54 45 2b 32 29 0a 23 64 65 66 ING_BYTE+2).#def
6b800 69 6e 65 20 53 48 41 52 45 44 5f 53 49 5a 45 20 ine SHARED_SIZE
6b810 20 20 20 20 20 20 35 31 30 0a 0a 2f 2a 0a 2a 2a 510../*.**
6b820 20 57 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 Wrapper around
6b830 4f 53 20 73 70 65 63 69 66 69 63 20 73 71 6c 69 OS specific sqli
6b840 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 66 75 te3_os_init() fu
6b850 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 nction..*/.SQLIT
6b860 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6b870 6c 69 74 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 lite3OsInit(void
6b880 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74 );../* .** Funct
6b890 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 73 69 ions for accessi
6b8a0 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ng sqlite3_file
6b8b0 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49 methods .*/.SQLI
6b8c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6b8d0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 qlite3OsClose(sq
6b8e0 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 53 51 lite3_file*);.SQ
6b8f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6b900 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73 sqlite3OsRead(s
6b910 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f qlite3_file*, vo
6b920 69 64 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 id*, int amt, i6
6b930 34 20 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 4 offset);.SQLIT
6b940 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6b950 6c 69 74 65 33 4f 73 57 72 69 74 65 28 73 71 6c lite3OsWrite(sql
6b960 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 ite3_file*, cons
6b970 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74 t void*, int amt
6b980 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a 53 , i64 offset);.S
6b990 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6b9a0 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 t sqlite3OsTrunc
6b9b0 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ate(sqlite3_file
6b9c0 2a 2c 20 69 36 34 20 73 69 7a 65 29 3b 0a 53 51 *, i64 size);.SQ
6b9d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6b9e0 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 sqlite3OsSync(s
6b9f0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
6ba00 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6ba10 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
6ba20 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 FileSize(sqlite3
6ba30 5f 66 69 6c 65 2a 2c 20 69 36 34 20 2a 70 53 69 _file*, i64 *pSi
6ba40 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ze);.SQLITE_PRIV
6ba50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
6ba60 73 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 sLock(sqlite3_fi
6ba70 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 le*, int);.SQLIT
6ba80 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6ba90 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 lite3OsUnlock(sq
6baa0 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 lite3_file*, int
6bab0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6bac0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 E int sqlite3OsC
6bad0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
6bae0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
6baf0 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 d, int *pResOut)
6bb00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6bb10 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 int sqlite3OsFi
6bb20 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 leControl(sqlite
6bb30 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 3_file*,int,void
6bb40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6bb50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f TE void sqlite3O
6bb60 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 48 69 6e 74 sFileControlHint
6bb70 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 (sqlite3_file*,i
6bb80 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 nt,void*);.#defi
6bb90 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f ne SQLITE_FCNTL_
6bba0 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 30 78 63 DB_UNCHANGED 0xc
6bbb0 61 30 39 33 66 61 30 0a 53 51 4c 49 54 45 5f 50 a093fa0.SQLITE_P
6bbc0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6bbd0 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 e3OsSectorSize(s
6bbe0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
6bbf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6bc00 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 int sqlite3OsDe
6bc10 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
6bc20 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ics(sqlite3_file
6bc30 20 2a 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 *id);.SQLITE_PR
6bc40 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6bc50 33 4f 73 53 68 6d 4d 61 70 28 73 71 6c 69 74 65 3OsShmMap(sqlite
6bc60 33 5f 66 69 6c 65 20 2a 2c 69 6e 74 2c 69 6e 74 3_file *,int,int
6bc70 2c 69 6e 74 2c 76 6f 69 64 20 76 6f 6c 61 74 69 ,int,void volati
6bc80 6c 65 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 le **);.SQLITE_P
6bc90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6bca0 65 33 4f 73 53 68 6d 4c 6f 63 6b 28 73 71 6c 69 e3OsShmLock(sqli
6bcb0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
6bcc0 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 t, int, int);.SQ
6bcd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6bce0 64 20 73 71 6c 69 74 65 33 4f 73 53 68 6d 42 61 d sqlite3OsShmBa
6bcf0 72 72 69 65 72 28 73 71 6c 69 74 65 33 5f 66 69 rrier(sqlite3_fi
6bd00 6c 65 20 2a 69 64 29 3b 0a 53 51 4c 49 54 45 5f le *id);.SQLITE_
6bd10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6bd20 74 65 33 4f 73 53 68 6d 55 6e 6d 61 70 28 73 71 te3OsShmUnmap(sq
6bd30 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
6bd40 69 6e 74 29 3b 0a 0a 0a 2f 2a 20 0a 2a 2a 20 46 int);.../* .** F
6bd50 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 63 unctions for acc
6bd60 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 76 essing sqlite3_v
6bd70 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 fs methods .*/.S
6bd80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6bd90 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 t sqlite3OsOpen(
6bda0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 sqlite3_vfs *, c
6bdb0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c onst char *, sql
6bdc0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2c ite3_file*, int,
6bdd0 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f int *);.SQLITE_
6bde0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6bdf0 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 te3OsDelete(sqli
6be00 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 te3_vfs *, const
6be10 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 char *, int);.S
6be20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6be30 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 t sqlite3OsAcces
6be40 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c s(sqlite3_vfs *,
6be50 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 const char *, i
6be60 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 nt, int *pResOut
6be70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6be80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 E int sqlite3OsF
6be90 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c 69 ullPathname(sqli
6bea0 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 te3_vfs *, const
6beb0 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68 char *, int, ch
6bec0 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 ar *);.#ifndef S
6bed0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
6bee0 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 EXTENSION.SQLITE
6bef0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
6bf00 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 qlite3OsDlOpen(s
6bf10 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f qlite3_vfs *, co
6bf20 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c nst char *);.SQL
6bf30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6bf40 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f sqlite3OsDlErro
6bf50 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c r(sqlite3_vfs *,
6bf60 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 int, char *);.S
6bf70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6bf80 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c id (*sqlite3OsDl
6bf90 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 Sym(sqlite3_vfs
6bfa0 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 74 *, void *, const
6bfb0 20 63 68 61 72 20 2a 29 29 28 76 6f 69 64 29 3b char *))(void);
6bfc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6bfd0 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c void sqlite3OsDl
6bfe0 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 Close(sqlite3_vf
6bff0 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a 23 65 s *, void *);.#e
6c000 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
6c010 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
6c020 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ON */.SQLITE_PRI
6c030 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6c040 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c OsRandomness(sql
6c050 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c ite3_vfs *, int,
6c060 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 char *);.SQLITE
6c070 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6c080 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 ite3OsSleep(sqli
6c090 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29 3b te3_vfs *, int);
6c0a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6c0b0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 int sqlite3OsCur
6c0c0 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 73 71 rentTimeInt64(sq
6c0d0 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 73 71 6c lite3_vfs *, sql
6c0e0 69 74 65 33 5f 69 6e 74 36 34 2a 29 3b 0a 0a 2f ite3_int64*);../
6c0f0 2a 0a 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 *.** Convenience
6c100 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 6f functions for o
6c110 70 65 6e 69 6e 67 20 61 6e 64 20 63 6c 6f 73 69 pening and closi
6c120 6e 67 20 66 69 6c 65 73 20 75 73 69 6e 67 20 0a ng files using .
6c130 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f ** sqlite3_mallo
6c140 63 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 c() to obtain sp
6c150 61 63 65 20 66 6f 72 20 74 68 65 20 66 69 6c 65 ace for the file
6c160 2d 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75 72 -handle structur
6c170 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
6c180 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6c190 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73 71 6c OsOpenMalloc(sql
6c1a0 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 ite3_vfs *, cons
6c1b0 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 t char *, sqlite
6c1c0 33 5f 66 69 6c 65 20 2a 2a 2c 20 69 6e 74 2c 69 3_file **, int,i
6c1d0 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
6c1e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6c1f0 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 OsCloseFree(sqli
6c200 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 65 te3_file *);..#e
6c210 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f ndif /* _SQLITE_
6c220 4f 53 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a OS_H_ */../*****
6c230 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
6c240 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os.h **********
6c250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6c260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6c270 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
6c280 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
6c290 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
6c2a0 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
6c2b0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
6c2c0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
6c2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
6c2e0 65 20 6d 75 74 65 78 2e 68 20 69 6e 20 74 68 65 e mutex.h in the
6c2f0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 middle of sqlit
6c300 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
6c310 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
6c320 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
6c330 66 69 6c 65 20 6d 75 74 65 78 2e 68 20 2a 2a 2a file mutex.h ***
6c340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6c350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6c360 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
6c370 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2007 August 28.*
6c380 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
6c390 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
6c3a0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
6c3b0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
6c3c0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
6c3d0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
6c3e0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
6c3f0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
6c400 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
6c410 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
6c420 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
6c430 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
6c440 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
6c450 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
6c460 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
6c470 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
6c480 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
6c490 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
6c4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6c4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6c4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6c4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
6c4e0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
6c4f0 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6d 6d 6f ntains the commo
6c500 6e 20 68 65 61 64 65 72 20 66 6f 72 20 61 6c 6c n header for all
6c510 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 mutex implement
6c520 61 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 ations..** The s
6c530 71 6c 69 74 65 49 6e 74 2e 68 20 68 65 61 64 65 qliteInt.h heade
6c540 72 20 23 69 6e 63 6c 75 64 65 73 20 74 68 69 73 r #includes this
6c550 20 66 69 6c 65 20 73 6f 20 74 68 61 74 20 69 74 file so that it
6c560 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a is available.**
6c570 20 74 6f 20 61 6c 6c 20 73 6f 75 72 63 65 20 66 to all source f
6c580 69 6c 65 73 2e 20 20 57 65 20 62 72 65 61 6b 20 iles. We break
6c590 69 74 20 6f 75 74 20 69 6e 20 61 6e 20 65 66 66 it out in an eff
6c5a0 6f 72 74 20 74 6f 20 6b 65 65 70 20 74 68 65 20 ort to keep the
6c5b0 63 6f 64 65 0a 2a 2a 20 62 65 74 74 65 72 20 6f code.** better o
6c5c0 72 67 61 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 rganized..**.**
6c5d0 4e 4f 54 45 3a 20 20 73 6f 75 72 63 65 20 66 69 NOTE: source fi
6c5e0 6c 65 73 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a les should *not*
6c5f0 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73 20 68 #include this h
6c600 65 61 64 65 72 20 66 69 6c 65 20 64 69 72 65 63 eader file direc
6c610 74 6c 79 2e 0a 2a 2a 20 53 6f 75 72 63 65 20 66 tly..** Source f
6c620 69 6c 65 73 20 73 68 6f 75 6c 64 20 23 69 6e 63 iles should #inc
6c630 6c 75 64 65 20 74 68 65 20 73 71 6c 69 74 65 49 lude the sqliteI
6c640 6e 74 2e 68 20 66 69 6c 65 20 61 6e 64 20 6c 65 nt.h file and le
6c650 74 20 74 68 61 74 20 66 69 6c 65 0a 2a 2a 20 69 t that file.** i
6c660 6e 63 6c 75 64 65 20 74 68 69 73 20 6f 6e 65 20 nclude this one
6c670 69 6e 64 69 72 65 63 74 6c 79 2e 0a 2a 2f 0a 0a indirectly..*/..
6c680 0a 2f 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 ./*.** Figure ou
6c690 74 20 77 68 61 74 20 76 65 72 73 69 6f 6e 20 6f t what version o
6c6a0 66 20 74 68 65 20 63 6f 64 65 20 74 6f 20 75 73 f the code to us
6c6b0 65 2e 20 20 54 68 65 20 63 68 6f 69 63 65 73 20 e. The choices
6c6c0 61 72 65 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 are.**.** SQLI
6c6d0 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 20 20 20 TE_MUTEX_OMIT
6c6e0 20 20 20 20 20 20 4e 6f 20 6d 75 74 65 78 20 6c No mutex l
6c6f0 6f 67 69 63 2e 20 20 4e 6f 74 20 65 76 65 6e 20 ogic. Not even
6c700 73 74 75 62 73 2e 20 20 54 68 65 0a 2a 2a 20 20 stubs. The.**
6c710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c720 20 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78 mutex
6c730 65 73 20 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 es implemention
6c740 63 61 6e 6e 6f 74 20 62 65 20 6f 76 65 72 72 69 cannot be overri
6c750 64 64 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 dden.**
6c760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c770 20 20 20 20 61 74 20 73 74 61 72 74 2d 74 69 6d at start-tim
6c780 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 e..**.** SQLIT
6c790 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20 E_MUTEX_NOOP
6c7a0 20 20 20 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d For single-
6c7b0 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 threaded applica
6c7c0 74 69 6f 6e 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 tions. No.**
6c7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c7e0 20 20 20 20 20 20 20 20 20 20 6d 75 74 75 61 6c mutual
6c7f0 20 65 78 63 6c 75 73 69 6f 6e 20 69 73 20 70 72 exclusion is pr
6c800 6f 76 69 64 65 64 2e 20 20 42 75 74 20 74 68 69 ovided. But thi
6c810 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 s.**
6c820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c830 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
6c840 63 61 6e 20 62 65 20 6f 76 65 72 72 69 64 64 65 can be overridde
6c850 6e 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 n at.**
6c860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c870 20 20 20 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a start-time..
6c880 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d **.** SQLITE_M
6c890 55 54 45 58 5f 50 54 48 52 45 41 44 53 20 20 20 UTEX_PTHREADS
6c8a0 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 For multi-thre
6c8b0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e aded application
6c8c0 73 20 6f 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a s on Unix..**.**
6c8d0 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f SQLITE_MUTEX_
6c8e0 57 33 32 20 20 20 20 20 20 20 20 20 20 46 6f 72 W32 For
6c8f0 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 multi-threaded
6c900 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 applications on
6c910 57 69 6e 33 32 2e 0a 2a 2f 0a 23 69 66 20 21 53 Win32..*/.#if !S
6c920 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
6c930 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
6c940 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 23 65 6e 64 _MUTEX_OMIT.#end
6c950 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 if.#if SQLITE_TH
6c960 52 45 41 44 53 41 46 45 20 26 26 20 21 64 65 66 READSAFE && !def
6c970 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 ined(SQLITE_MUTE
6c980 58 5f 4e 4f 4f 50 29 0a 23 20 20 69 66 20 53 51 X_NOOP).# if SQ
6c990 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 20 20 LITE_OS_UNIX.#
6c9a0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
6c9b0 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 0a 23 MUTEX_PTHREADS.#
6c9c0 20 20 65 6c 69 66 20 53 51 4c 49 54 45 5f 4f 53 elif SQLITE_OS
6c9d0 5f 57 49 4e 0a 23 20 20 20 20 64 65 66 69 6e 65 _WIN.# define
6c9e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 SQLITE_MUTEX_W3
6c9f0 32 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 2.# else.# d
6ca00 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 efine SQLITE_MUT
6ca10 45 58 5f 4e 4f 4f 50 0a 23 20 20 65 6e 64 69 66 EX_NOOP.# endif
6ca20 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
6ca30 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 SQLITE_MUTEX_OMI
6ca40 54 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 T./*.** If this
6ca50 69 73 20 61 20 6e 6f 2d 6f 70 20 69 6d 70 6c 65 is a no-op imple
6ca60 6d 65 6e 74 61 74 69 6f 6e 2c 20 69 6d 70 6c 65 mentation, imple
6ca70 6d 65 6e 74 20 65 76 65 72 79 74 68 69 6e 67 20 ment everything
6ca80 61 73 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 as macros..*/.#d
6ca90 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 efine sqlite3_mu
6caa0 74 65 78 5f 61 6c 6c 6f 63 28 58 29 20 20 20 20 tex_alloc(X)
6cab0 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a ((sqlite3_mutex*
6cac0 29 38 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 )8).#define sqli
6cad0 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 58 te3_mutex_free(X
6cae0 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 ).#define sqlite
6caf0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 58 29 3_mutex_enter(X)
6cb00 20 20 20 20 0a 23 64 65 66 69 6e 65 20 73 71 6c .#define sql
6cb10 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 58 ite3_mutex_try(X
6cb20 29 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b ) SQLITE_OK
6cb30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
6cb40 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 58 29 20 _mutex_leave(X)
6cb50 20 20 20 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 .#define sqli
6cb60 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 58 te3_mutex_held(X
6cb70 29 20 20 20 20 20 28 28 76 6f 69 64 29 28 58 29 ) ((void)(X)
6cb80 2c 31 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 ,1).#define sqli
6cb90 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
6cba0 64 28 58 29 20 20 28 28 76 6f 69 64 29 28 58 29 d(X) ((void)(X)
6cbb0 2c 31 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 ,1).#define sqli
6cbc0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 58 29 te3MutexAlloc(X)
6cbd0 20 20 20 20 20 20 28 28 73 71 6c 69 74 65 33 5f ((sqlite3_
6cbe0 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 66 69 6e mutex*)8).#defin
6cbf0 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 6e e sqlite3MutexIn
6cc00 69 74 28 29 20 20 20 20 20 20 20 20 53 51 4c 49 it() SQLI
6cc10 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71 TE_OK.#define sq
6cc20 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 29 0a lite3MutexEnd().
6cc30 23 64 65 66 69 6e 65 20 4d 55 54 45 58 5f 4c 4f #define MUTEX_LO
6cc40 47 49 43 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 GIC(X).#else.#de
6cc50 66 69 6e 65 20 4d 55 54 45 58 5f 4c 4f 47 49 43 fine MUTEX_LOGIC
6cc60 28 58 29 20 20 20 20 20 20 20 20 20 20 20 20 58 (X) X
6cc70 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e .#endif /* defin
6cc80 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ed(SQLITE_MUTEX_
6cc90 4f 4d 49 54 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a OMIT) */../*****
6cca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
6ccb0 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a mutex.h *******
6ccc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6ccd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
6cce0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
6ccf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
6cd00 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
6cd10 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
6cd20 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
6cd30 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a ********/.../*.*
6cd40 2a 20 45 61 63 68 20 64 61 74 61 62 61 73 65 20 * Each database
6cd50 66 69 6c 65 20 74 6f 20 62 65 20 61 63 63 65 73 file to be acces
6cd60 73 65 64 20 62 79 20 74 68 65 20 73 79 73 74 65 sed by the syste
6cd70 6d 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 m is an instance
6cd80 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f .** of the follo
6cd90 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 20 wing structure.
6cda0 20 54 68 65 72 65 20 61 72 65 20 6e 6f 72 6d 61 There are norma
6cdb0 6c 6c 79 20 74 77 6f 20 6f 66 20 74 68 65 73 65 lly two of these
6cdc0 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 69 structures.** i
6cdd0 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61 44 62 n the sqlite.aDb
6cde0 5b 5d 20 61 72 72 61 79 2e 20 20 61 44 62 5b 30 [] array. aDb[0
6cdf0 5d 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61 ] is the main da
6ce00 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 0a tabase file and.
6ce10 2a 2a 20 61 44 62 5b 31 5d 20 69 73 20 74 68 65 ** aDb[1] is the
6ce20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 database file u
6ce30 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 65 6d 70 sed to hold temp
6ce40 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 41 orary tables. A
6ce50 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 61 74 dditional.** dat
6ce60 61 62 61 73 65 73 20 6d 61 79 20 62 65 20 61 74 abases may be at
6ce70 74 61 63 68 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 tached..*/.struc
6ce80 74 20 44 62 20 7b 0a 20 20 63 68 61 72 20 2a 7a t Db {. char *z
6ce90 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a Name; /*
6cea0 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 64 61 Name of this da
6ceb0 74 61 62 61 73 65 20 2a 2f 0a 20 20 42 74 72 65 tabase */. Btre
6cec0 65 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 e *pBt;
6ced0 20 2f 2a 20 54 68 65 20 42 2a 54 72 65 65 20 73 /* The B*Tree s
6cee0 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 69 tructure for thi
6cef0 73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 s database file
6cf00 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 3b */. u8 inTrans;
6cf10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 3a 20 /* 0:
6cf20 6e 6f 74 20 77 72 69 74 61 62 6c 65 2e 20 20 31 not writable. 1
6cf30 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 : Transaction.
6cf40 32 3a 20 43 68 65 63 6b 70 6f 69 6e 74 20 2a 2f 2: Checkpoint */
6cf50 0a 20 20 75 38 20 73 61 66 65 74 79 5f 6c 65 76 . u8 safety_lev
6cf60 65 6c 3b 20 20 20 20 20 2f 2a 20 48 6f 77 20 61 el; /* How a
6cf70 67 67 72 65 73 73 69 76 65 20 61 74 20 73 79 6e ggressive at syn
6cf80 63 69 6e 67 20 64 61 74 61 20 74 6f 20 64 69 73 cing data to dis
6cf90 6b 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 k */. Schema *p
6cfa0 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 50 Schema; /* P
6cfb0 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 62 61 ointer to databa
6cfc0 73 65 20 73 63 68 65 6d 61 20 28 70 6f 73 73 69 se schema (possi
6cfd0 62 6c 79 20 73 68 61 72 65 64 29 20 2a 2f 0a 7d bly shared) */.}
6cfe0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 ;../*.** An inst
6cff0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
6d000 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
6d010 73 74 6f 72 65 73 20 61 20 64 61 74 61 62 61 73 stores a databas
6d020 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 e schema..**.**
6d030 4d 6f 73 74 20 53 63 68 65 6d 61 20 6f 62 6a 65 Most Schema obje
6d040 63 74 73 20 61 72 65 20 61 73 73 6f 63 69 61 74 cts are associat
6d050 65 64 20 77 69 74 68 20 61 20 42 74 72 65 65 2e ed with a Btree.
6d060 20 20 54 68 65 20 65 78 63 65 70 74 69 6f 6e 20 The exception
6d070 69 73 0a 2a 2a 20 74 68 65 20 53 63 68 65 6d 61 is.** the Schema
6d080 20 66 6f 72 20 74 68 65 20 54 45 4d 50 20 64 61 for the TEMP da
6d090 74 61 62 61 65 73 20 28 73 71 6c 69 74 65 33 2e tabaes (sqlite3.
6d0a0 61 44 62 5b 31 5d 29 20 77 68 69 63 68 20 69 73 aDb[1]) which is
6d0b0 20 66 72 65 65 2d 73 74 61 6e 64 69 6e 67 2e 0a free-standing..
6d0c0 2a 2a 20 49 6e 20 73 68 61 72 65 64 20 63 61 63 ** In shared cac
6d0d0 68 65 20 6d 6f 64 65 2c 20 61 20 73 69 6e 67 6c he mode, a singl
6d0e0 65 20 53 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 e Schema object
6d0f0 63 61 6e 20 62 65 20 73 68 61 72 65 64 20 62 79 can be shared by
6d100 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 42 74 72 multiple.** Btr
6d110 65 65 73 20 74 68 61 74 20 72 65 66 65 72 20 74 ees that refer t
6d120 6f 20 74 68 65 20 73 61 6d 65 20 75 6e 64 65 72 o the same under
6d130 6c 79 69 6e 67 20 42 74 53 68 61 72 65 64 20 6f lying BtShared o
6d140 62 6a 65 63 74 2e 0a 2a 2a 20 0a 2a 2a 20 53 63 bject..** .** Sc
6d150 68 65 6d 61 20 6f 62 6a 65 63 74 73 20 61 72 65 hema objects are
6d160 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 automatically d
6d170 65 61 6c 6c 6f 63 61 74 65 64 20 77 68 65 6e 20 eallocated when
6d180 74 68 65 20 6c 61 73 74 20 42 74 72 65 65 20 74 the last Btree t
6d190 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 hat.** reference
6d1a0 73 20 74 68 65 6d 20 69 73 20 64 65 73 74 72 6f s them is destro
6d1b0 79 65 64 2e 20 20 20 54 68 65 20 54 45 4d 50 20 yed. The TEMP
6d1c0 53 63 68 65 6d 61 20 69 73 20 6d 61 6e 75 61 6c Schema is manual
6d1d0 6c 79 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 73 ly freed by.** s
6d1e0 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a qlite3_close()..
6d1f0 2a 0a 2a 2a 20 41 20 74 68 72 65 61 64 20 6d 75 *.** A thread mu
6d200 73 74 20 62 65 20 68 6f 6c 64 69 6e 67 20 61 20 st be holding a
6d210 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 63 6f 72 mutex on the cor
6d220 72 65 73 70 6f 6e 64 69 6e 67 20 42 74 72 65 65 responding Btree
6d230 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 in order.** to
6d240 61 63 63 65 73 73 20 53 63 68 65 6d 61 20 63 6f access Schema co
6d250 6e 74 65 6e 74 2e 20 20 54 68 69 73 20 69 6d 70 ntent. This imp
6d260 6c 69 65 73 20 74 68 61 74 20 74 68 65 20 74 68 lies that the th
6d270 72 65 61 64 20 6d 75 73 74 20 61 6c 73 6f 20 62 read must also b
6d280 65 0a 2a 2a 20 68 6f 6c 64 69 6e 67 20 61 20 6d e.** holding a m
6d290 75 74 65 78 20 6f 6e 20 74 68 65 20 73 71 6c 69 utex on the sqli
6d2a0 74 65 33 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 te3 connection p
6d2b0 6f 69 6e 74 65 72 20 74 68 61 74 20 6f 77 6e 73 ointer that owns
6d2c0 20 74 68 65 20 42 74 72 65 65 2e 0a 2a 2a 20 46 the Btree..** F
6d2d0 6f 72 20 61 20 54 45 4d 50 20 53 63 68 65 6d 61 or a TEMP Schema
6d2e0 2c 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6e 6e 65 , only the conne
6d2f0 63 74 69 6f 6e 20 6d 75 74 65 78 20 69 73 20 72 ction mutex is r
6d300 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 72 75 equired..*/.stru
6d310 63 74 20 53 63 68 65 6d 61 20 7b 0a 20 20 69 6e ct Schema {. in
6d320 74 20 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b t schema_cookie;
6d330 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 73 /* Database s
6d340 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 6e 75 chema version nu
6d350 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 66 69 mber for this fi
6d360 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 47 65 6e le */. int iGen
6d370 65 72 61 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 eration; /*
6d380 47 65 6e 65 72 61 74 69 6f 6e 20 63 6f 75 6e 74 Generation count
6d390 65 72 2e 20 20 49 6e 63 72 65 6d 65 6e 74 65 64 er. Incremented
6d3a0 20 77 69 74 68 20 65 61 63 68 20 63 68 61 6e 67 with each chang
6d3b0 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 62 6c 48 e */. Hash tblH
6d3c0 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 ash; /* A
6d3d0 6c 6c 20 74 61 62 6c 65 73 20 69 6e 64 65 78 65 ll tables indexe
6d3e0 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 d by name */. H
6d3f0 61 73 68 20 69 64 78 48 61 73 68 3b 20 20 20 20 ash idxHash;
6d400 20 20 20 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 /* All (name
6d410 64 29 20 69 6e 64 69 63 65 73 20 69 6e 64 65 78 d) indices index
6d420 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 ed by name */.
6d430 48 61 73 68 20 74 72 69 67 48 61 73 68 3b 20 20 Hash trigHash;
6d440 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67 /* All trig
6d450 67 65 72 73 20 69 6e 64 65 78 65 64 20 62 79 20 gers indexed by
6d460 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 66 name */. Hash f
6d470 6b 65 79 48 61 73 68 3b 20 20 20 20 20 20 20 2f keyHash; /
6d480 2a 20 41 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 * All foreign ke
6d490 79 73 20 62 79 20 72 65 66 65 72 65 6e 63 65 64 ys by referenced
6d4a0 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 table name */.
6d4b0 20 54 61 62 6c 65 20 2a 70 53 65 71 54 61 62 3b Table *pSeqTab;
6d4c0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c /* The sql
6d4d0 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 ite_sequence tab
6d4e0 6c 65 20 75 73 65 64 20 62 79 20 41 55 54 4f 49 le used by AUTOI
6d4f0 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 75 38 NCREMENT */. u8
6d500 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 file_format;
6d510 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 66 6f 72 /* Schema for
6d520 6d 61 74 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 mat version for
6d530 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 this file */. u
6d540 38 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 8 enc;
6d550 20 20 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f /* Text enco
6d560 64 69 6e 67 20 75 73 65 64 20 62 79 20 74 68 69 ding used by thi
6d570 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 s database */.
6d580 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 u16 flags;
6d590 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 /* Flags as
6d5a0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
6d5b0 69 73 20 73 63 68 65 6d 61 20 2a 2f 0a 20 20 69 is schema */. i
6d5c0 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 3b 20 20 nt cache_size;
6d5d0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6d5e0 20 70 61 67 65 73 20 74 6f 20 75 73 65 20 69 6e pages to use in
6d5f0 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 7d 3b the cache */.};
6d600 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 ../*.** These ma
6d610 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 cros can be used
6d620 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 6f to test, set, o
6d630 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e 20 r clear bits in
6d640 74 68 65 20 0a 2a 2a 20 44 62 2e 70 53 63 68 65 the .** Db.pSche
6d650 6d 61 2d 3e 66 6c 61 67 73 20 66 69 65 6c 64 2e ma->flags field.
6d660 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 62 48 61 .*/.#define DbHa
6d670 73 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 sProperty(D,I,P)
6d680 20 20 20 20 20 28 28 28 44 29 2d 3e 61 44 62 5b (((D)->aDb[
6d690 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 I].pSchema->flag
6d6a0 73 26 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 s&(P))==(P)).#de
6d6b0 66 69 6e 65 20 44 62 48 61 73 41 6e 79 50 72 6f fine DbHasAnyPro
6d6c0 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 28 28 perty(D,I,P) ((
6d6d0 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 (D)->aDb[I].pSch
6d6e0 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 ema->flags&(P))!
6d6f0 3d 30 29 0a 23 64 65 66 69 6e 65 20 44 62 53 65 =0).#define DbSe
6d700 74 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 tProperty(D,I,P)
6d710 20 20 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d (D)->aDb[I]
6d720 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 7c .pSchema->flags|
6d730 3d 28 50 29 0a 23 64 65 66 69 6e 65 20 44 62 43 =(P).#define DbC
6d740 6c 65 61 72 50 72 6f 70 65 72 74 79 28 44 2c 49 learProperty(D,I
6d750 2c 50 29 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 ,P) (D)->aDb[I
6d760 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 ].pSchema->flags
6d770 26 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c &=~(P)../*.** Al
6d780 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 lowed values for
6d790 20 74 68 65 20 44 42 2e 70 53 63 68 65 6d 61 2d the DB.pSchema-
6d7a0 3e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a >flags field..**
6d7b0 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63 68 65 6d .** The DB_Schem
6d7c0 61 4c 6f 61 64 65 64 20 66 6c 61 67 20 69 73 20 aLoaded flag is
6d7d0 73 65 74 20 61 66 74 65 72 20 74 68 65 20 64 61 set after the da
6d7e0 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 tabase schema ha
6d7f0 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69 s been.** read i
6d800 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 nto internal has
6d810 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 h tables..**.**
6d820 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 DB_UnresetViews
6d830 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f means that one o
6d840 72 20 6d 6f 72 65 20 76 69 65 77 73 20 68 61 76 r more views hav
6d850 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 e column names t
6d860 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e hat.** have been
6d870 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66 filled out. If
6d880 20 74 68 65 20 73 63 68 65 6d 61 20 63 68 61 6e the schema chan
6d890 67 65 73 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d ges, these colum
6d8a0 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a n names might.**
6d8b0 20 63 68 61 6e 67 65 73 20 61 6e 64 20 73 6f 20 changes and so
6d8c0 74 68 65 20 76 69 65 77 20 77 69 6c 6c 20 6e 65 the view will ne
6d8d0 65 64 20 74 6f 20 62 65 20 72 65 73 65 74 2e 0a ed to be reset..
6d8e0 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 53 63 */.#define DB_Sc
6d8f0 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 20 30 78 hemaLoaded 0x
6d900 30 30 30 31 20 20 2f 2a 20 54 68 65 20 73 63 68 0001 /* The sch
6d910 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 ema has been loa
6d920 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 ded */.#define D
6d930 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 20 B_UnresetViews
6d940 20 20 30 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d 0x0002 /* Som
6d950 65 20 76 69 65 77 73 20 68 61 76 65 20 64 65 66 e views have def
6d960 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 ined column name
6d970 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f s */.#define DB_
6d980 45 6d 70 74 79 20 20 20 20 20 20 20 20 20 20 20 Empty
6d990 30 78 30 30 30 34 20 20 2f 2a 20 54 68 65 20 66 0x0004 /* The f
6d9a0 69 6c 65 20 69 73 20 65 6d 70 74 79 20 28 6c 65 ile is empty (le
6d9b0 6e 67 74 68 20 30 20 62 79 74 65 73 29 20 2a 2f ngth 0 bytes) */
6d9c0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 ../*.** The numb
6d9d0 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 er of different
6d9e0 6b 69 6e 64 73 20 6f 66 20 74 68 69 6e 67 73 20 kinds of things
6d9f0 74 68 61 74 20 63 61 6e 20 62 65 20 6c 69 6d 69 that can be limi
6da00 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 ted.** using the
6da10 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 sqlite3_limit()
6da20 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 interface..*/.#
6da30 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f define SQLITE_N_
6da40 4c 49 4d 49 54 20 28 53 51 4c 49 54 45 5f 4c 49 LIMIT (SQLITE_LI
6da50 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 MIT_TRIGGER_DEPT
6da60 48 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b H+1)../*.** Look
6da70 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69 73 20 aside malloc is
6da80 61 20 73 65 74 20 6f 66 20 66 69 78 65 64 2d 73 a set of fixed-s
6da90 69 7a 65 20 62 75 66 66 65 72 73 20 74 68 61 74 ize buffers that
6daa0 20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 can be used.**
6dab0 74 6f 20 73 61 74 69 73 66 79 20 73 6d 61 6c 6c to satisfy small
6dac0 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d 6f 72 transient memor
6dad0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 y allocation req
6dae0 75 65 73 74 73 20 66 6f 72 20 6f 62 6a 65 63 74 uests for object
6daf0 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 s.** associated
6db00 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 with a particula
6db10 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
6db20 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 73 65 20 ction. The use
6db30 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 of.** lookaside
6db40 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65 73 20 malloc provides
6db50 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20 70 65 a significant pe
6db60 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63 rformance enhanc
6db70 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72 6f 78 ement.** (approx
6db80 20 31 30 25 29 20 62 79 20 61 76 6f 69 64 69 6e 10%) by avoidin
6db90 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f g numerous mallo
6dba0 63 2f 66 72 65 65 20 72 65 71 75 65 73 74 73 20 c/free requests
6dbb0 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a while parsing.**
6dbc0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e SQL statements.
6dbd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f 6b 61 .**.** The Looka
6dbe0 73 69 64 65 20 73 74 72 75 63 74 75 72 65 20 68 side structure h
6dbf0 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61 74 69 olds configurati
6dc00 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 on information a
6dc10 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b bout the.** look
6dc20 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73 75 62 aside malloc sub
6dc30 73 79 73 74 65 6d 2e 20 20 45 61 63 68 20 61 76 system. Each av
6dc40 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 61 ailable memory a
6dc50 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a 2a 20 llocation in.**
6dc60 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 73 75 the lookaside su
6dc70 62 73 79 73 74 65 6d 20 69 73 20 73 74 6f 72 65 bsystem is store
6dc80 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 d on a linked li
6dc90 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64 65 53 st of LookasideS
6dca0 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 0a lot.** objects..
6dcb0 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 **.** Lookaside
6dcc0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 allocations are
6dcd0 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 66 6f 72 only allowed for
6dce0 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 objects that ar
6dcf0 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 e associated.**
6dd00 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 with a particula
6dd10 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
6dd20 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65 2c 20 73 ction. Hence, s
6dd30 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f chema informatio
6dd40 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 73 n cannot.** be s
6dd50 74 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b 61 73 69 tored in lookasi
6dd60 64 65 20 62 65 63 61 75 73 65 20 69 6e 20 73 68 de because in sh
6dd70 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 20 ared cache mode
6dd80 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 the schema infor
6dd90 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 68 61 mation.** is sha
6dda0 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 red by multiple
6ddb0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
6ddc0 69 6f 6e 73 2e 20 20 54 68 65 72 65 66 6f 72 65 ions. Therefore
6ddd0 2c 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a , while parsing.
6dde0 2a 2a 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d ** schema inform
6ddf0 61 74 69 6f 6e 2c 20 74 68 65 20 4c 6f 6f 6b 61 ation, the Looka
6de00 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 66 6c side.bEnabled fl
6de10 61 67 20 69 73 20 63 6c 65 61 72 65 64 20 73 6f ag is cleared so
6de20 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 that.** lookasi
6de30 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 de allocations a
6de40 72 65 20 6e 6f 74 20 75 73 65 64 20 74 6f 20 63 re not used to c
6de50 6f 6e 73 74 72 75 63 74 20 74 68 65 20 73 63 68 onstruct the sch
6de60 65 6d 61 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a ema objects..*/.
6de70 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 struct Lookaside
6de80 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20 20 20 20 {. u16 sz;
6de90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6dea0 53 69 7a 65 20 6f 66 20 65 61 63 68 20 62 75 66 Size of each buf
6deb0 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a fer in bytes */.
6dec0 20 20 75 38 20 62 45 6e 61 62 6c 65 64 3b 20 20 u8 bEnabled;
6ded0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c /* Fal
6dee0 73 65 20 74 6f 20 64 69 73 61 62 6c 65 20 6e 65 se to disable ne
6def0 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f w lookaside allo
6df00 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 38 20 cations */. u8
6df10 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20 20 20 bMalloced;
6df20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
6df30 70 53 74 61 72 74 20 6f 62 74 61 69 6e 65 64 20 pStart obtained
6df40 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c from sqlite3_mal
6df50 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e loc() */. int n
6df60 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 Out;
6df70 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
6df80 62 75 66 66 65 72 73 20 63 75 72 72 65 6e 74 6c buffers currentl
6df90 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f y checked out */
6dfa0 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b 20 20 20 . int mxOut;
6dfb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 69 /* Hi
6dfc0 67 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 ghwater mark for
6dfd0 20 6e 4f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 61 nOut */. int a
6dfe0 6e 53 74 61 74 5b 33 5d 3b 20 20 20 20 20 20 20 nStat[3];
6dff0 20 20 20 2f 2a 20 30 3a 20 68 69 74 73 2e 20 20 /* 0: hits.
6e000 31 3a 20 73 69 7a 65 20 6d 69 73 73 65 73 2e 20 1: size misses.
6e010 20 32 3a 20 66 75 6c 6c 20 6d 69 73 73 65 73 20 2: full misses
6e020 2a 2f 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c */. LookasideSl
6e030 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 2f 2a 20 ot *pFree; /*
6e040 4c 69 73 74 20 6f 66 20 61 76 61 69 6c 61 62 6c List of availabl
6e050 65 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 20 76 e buffers */. v
6e060 6f 69 64 20 2a 70 53 74 61 72 74 3b 20 20 20 20 oid *pStart;
6e070 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
6e080 62 79 74 65 20 6f 66 20 61 76 61 69 6c 61 62 6c byte of availabl
6e090 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 2a e memory space *
6e0a0 2f 0a 20 20 76 6f 69 64 20 2a 70 45 6e 64 3b 20 /. void *pEnd;
6e0b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
6e0c0 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 65 irst byte past e
6e0d0 6e 64 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 nd of available
6e0e0 73 70 61 63 65 20 2a 2f 0a 7d 3b 0a 73 74 72 75 space */.};.stru
6e0f0 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 ct LookasideSlot
6e100 20 7b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c {. LookasideSl
6e110 6f 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a ot *pNext; /*
6e120 20 4e 65 78 74 20 62 75 66 66 65 72 20 69 6e 20 Next buffer in
6e130 74 68 65 20 6c 69 73 74 20 6f 66 20 66 72 65 65 the list of free
6e140 20 62 75 66 66 65 72 73 20 2a 2f 0a 7d 3b 0a 0a buffers */.};..
6e150 2f 2a 0a 2a 2a 20 41 20 68 61 73 68 20 74 61 62 /*.** A hash tab
6e160 6c 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 le for function
6e170 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 0a definitions..**.
6e180 2a 2a 20 48 61 73 68 20 65 61 63 68 20 46 75 6e ** Hash each Fun
6e190 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 69 cDef structure i
6e1a0 6e 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 46 nto one of the F
6e1b0 75 6e 63 44 65 66 48 61 73 68 2e 61 5b 5d 20 73 uncDefHash.a[] s
6e1c0 6c 6f 74 73 2e 0a 2a 2a 20 43 6f 6c 6c 69 73 69 lots..** Collisi
6e1d0 6f 6e 73 20 61 72 65 20 6f 6e 20 74 68 65 20 46 ons are on the F
6e1e0 75 6e 63 44 65 66 2e 70 48 61 73 68 20 63 68 61 uncDef.pHash cha
6e1f0 69 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75 in..*/.struct Fu
6e200 6e 63 44 65 66 48 61 73 68 20 7b 0a 20 20 46 75 ncDefHash {. Fu
6e210 6e 63 44 65 66 20 2a 61 5b 32 33 5d 3b 20 20 20 ncDef *a[23];
6e220 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c /* Hash tabl
6e230 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20 e for functions
6e240 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 */.};../*.** Eac
6e250 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 h database conne
6e260 63 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74 ction is an inst
6e270 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
6e280 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e owing structure.
6e290 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 .*/.struct sqlit
6e2a0 65 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 e3 {. sqlite3_v
6e2b0 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 fs *pVfs;
6e2c0 20 20 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 /* OS Inter
6e2d0 66 61 63 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 face */. struct
6e2e0 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20 20 Vdbe *pVdbe;
6e2f0 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 /* List
6e300 6f 66 20 61 63 74 69 76 65 20 76 69 72 74 75 61 of active virtua
6e310 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f 0a 20 20 l machines */.
6e320 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43 6f CollSeq *pDfltCo
6e330 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ll; /*
6e340 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c The default col
6e350 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
6e360 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 73 71 (BINARY) */. sq
6e370 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
6e380 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 ex; /* C
6e390 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 74 65 78 20 onnection mutex
6e3a0 2a 2f 0a 20 20 44 62 20 2a 61 44 62 3b 20 20 20 */. Db *aDb;
6e3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e3c0 20 20 20 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e /* All backen
6e3d0 64 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b ds */. int nDb;
6e3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e3f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6e400 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72 72 of backends curr
6e410 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a ently in use */.
6e420 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 int flags;
6e430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e440 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 6f 75 73 /* Miscellaneous
6e450 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65 6c 6f flags. See belo
6e460 77 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 w */. i64 lastR
6e470 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 owid;
6e480 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 20 6f 66 /* ROWID of
6e490 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 73 most recent ins
6e4a0 65 72 74 20 28 73 65 65 20 61 62 6f 76 65 29 20 ert (see above)
6e4b0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e */. unsigned in
6e4c0 74 20 6f 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 t openFlags;
6e4d0 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 /* Flags pass
6e4e0 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 ed to sqlite3_vf
6e4f0 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 s.xOpen() */. i
6e500 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20 nt errCode;
6e510 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6e520 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f Most recent erro
6e530 72 20 63 6f 64 65 20 28 53 51 4c 49 54 45 5f 2a r code (SQLITE_*
6e540 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 4d 61 ) */. int errMa
6e550 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 sk;
6e560 20 20 20 20 20 2f 2a 20 26 20 72 65 73 75 6c 74 /* & result
6e570 20 63 6f 64 65 73 20 77 69 74 68 20 74 68 69 73 codes with this
6e580 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
6e590 67 20 2a 2f 0a 20 20 75 31 36 20 64 62 4f 70 74 g */. u16 dbOpt
6e5a0 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 Flags;
6e5b0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 6f /* Flags to
6e5c0 20 65 6e 61 62 6c 65 2f 64 69 73 61 62 6c 65 20 enable/disable
6e5d0 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 2a 2f optimizations */
6e5e0 0a 20 20 75 38 20 61 75 74 6f 43 6f 6d 6d 69 74 . u8 autoCommit
6e5f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6e600 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d /* The auto-com
6e610 6d 69 74 20 66 6c 61 67 2e 20 2a 2f 0a 20 20 75 mit flag. */. u
6e620 38 20 74 65 6d 70 5f 73 74 6f 72 65 3b 20 20 20 8 temp_store;
6e630 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6e640 31 3a 20 66 69 6c 65 20 32 3a 20 6d 65 6d 6f 72 1: file 2: memor
6e650 79 20 30 3a 20 64 65 66 61 75 6c 74 20 2a 2f 0a y 0: default */.
6e660 20 20 75 38 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 u8 mallocFaile
6e670 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
6e680 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 68 61 /* True if we ha
6e690 76 65 20 73 65 65 6e 20 61 20 6d 61 6c 6c 6f 63 ve seen a malloc
6e6a0 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 75 38 failure */. u8
6e6b0 20 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 3b 20 20 dfltLockMode;
6e6c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
6e6d0 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 2d 6d efault locking-m
6e6e0 6f 64 65 20 66 6f 72 20 61 74 74 61 63 68 65 64 ode for attached
6e6f0 20 64 62 73 20 2a 2f 0a 20 20 73 69 67 6e 65 64 dbs */. signed
6e700 20 63 68 61 72 20 6e 65 78 74 41 75 74 6f 76 61 char nextAutova
6e710 63 3b 20 20 20 20 20 20 2f 2a 20 41 75 74 6f 76 c; /* Autov
6e720 61 63 20 73 65 74 74 69 6e 67 20 61 66 74 65 72 ac setting after
6e730 20 56 41 43 55 55 4d 20 69 66 20 3e 3d 30 20 2a VACUUM if >=0 *
6e740 2f 0a 20 20 75 38 20 73 75 70 70 72 65 73 73 45 /. u8 suppressE
6e750 72 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rr;
6e760 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 69 73 73 75 /* Do not issu
6e770 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 e error messages
6e780 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38 if true */. u8
6e790 20 76 74 61 62 4f 6e 43 6f 6e 66 6c 69 63 74 3b vtabOnConflict;
6e7a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
6e7b0 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 66 alue to return f
6e7c0 6f 72 20 73 33 5f 76 74 61 62 5f 6f 6e 5f 63 6f or s3_vtab_on_co
6e7d0 6e 66 6c 69 63 74 28 29 20 2a 2f 0a 20 20 75 38 nflict() */. u8
6e7e0 20 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 isTransactionSa
6e7f0 76 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 54 vepoint; /* T
6e800 72 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 72 rue if the outer
6e810 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 20 69 most savepoint i
6e820 73 20 61 20 54 53 20 2a 2f 0a 20 20 69 6e 74 20 s a TS */. int
6e830 6e 65 78 74 50 61 67 65 73 69 7a 65 3b 20 20 20 nextPagesize;
6e840 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
6e850 65 73 69 7a 65 20 61 66 74 65 72 20 56 41 43 55 esize after VACU
6e860 55 4d 20 69 66 20 3e 30 20 2a 2f 0a 20 20 75 33 UM if >0 */. u3
6e870 32 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20 2 magic;
6e880 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
6e890 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 agic number for
6e8a0 64 65 74 65 63 74 20 6c 69 62 72 61 72 79 20 6d detect library m
6e8b0 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e isuse */. int n
6e8c0 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 Change;
6e8d0 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
6e8e0 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 e returned by sq
6e8f0 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 lite3_changes()
6e900 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 61 6c 43 */. int nTotalC
6e910 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 hange;
6e920 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 /* Value retu
6e930 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
6e940 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 total_changes()
6e950 2a 2f 0a 20 20 69 6e 74 20 61 4c 69 6d 69 74 5b */. int aLimit[
6e960 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 5d 3b SQLITE_N_LIMIT];
6e970 20 20 20 2f 2a 20 4c 69 6d 69 74 73 20 2a 2f 0a /* Limits */.
6e980 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 struct sqlite3
6e990 49 6e 69 74 49 6e 66 6f 20 7b 20 20 20 20 20 20 InitInfo {
6e9a0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 /* Information u
6e9b0 73 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 sed during initi
6e9c0 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 alization */.
6e9d0 20 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 int newTnum;
6e9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6e9f0 52 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c Rootpage of tabl
6ea00 65 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 e being initiali
6ea10 7a 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 69 44 zed */. u8 iD
6ea20 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
6ea30 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 /* Which
6ea40 64 62 20 66 69 6c 65 20 69 73 20 62 65 69 6e 67 db file is being
6ea50 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
6ea60 20 20 20 20 75 38 20 62 75 73 79 3b 20 20 20 20 u8 busy;
6ea70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ea80 2f 2a 20 54 52 55 45 20 69 66 20 63 75 72 72 65 /* TRUE if curre
6ea90 6e 74 6c 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e ntly initializin
6eaa0 67 20 2a 2f 0a 20 20 20 20 75 38 20 6f 72 70 68 g */. u8 orph
6eab0 61 6e 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 anTrigger;
6eac0 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 74 61 /* Last sta
6ead0 74 65 6d 65 6e 74 20 69 73 20 6f 72 70 68 61 6e tement is orphan
6eae0 65 64 20 54 45 4d 50 20 74 72 69 67 67 65 72 20 ed TEMP trigger
6eaf0 2a 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a 20 20 69 */. } init;. i
6eb00 6e 74 20 61 63 74 69 76 65 56 64 62 65 43 6e 74 nt activeVdbeCnt
6eb10 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
6eb20 4e 75 6d 62 65 72 20 6f 66 20 56 44 42 45 73 20 Number of VDBEs
6eb30 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 currently execut
6eb40 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 77 72 69 ing */. int wri
6eb50 74 65 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20 teVdbeCnt;
6eb60 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6eb70 20 6f 66 20 61 63 74 69 76 65 20 56 44 42 45 73 of active VDBEs
6eb80 20 74 68 61 74 20 61 72 65 20 77 72 69 74 69 6e that are writin
6eb90 67 20 2a 2f 0a 20 20 69 6e 74 20 76 64 62 65 45 g */. int vdbeE
6eba0 78 65 63 43 6e 74 3b 20 20 20 20 20 20 20 20 20 xecCnt;
6ebb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6ebc0 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20 74 f nested calls t
6ebd0 6f 20 56 64 62 65 45 78 65 63 28 29 20 2a 2f 0a o VdbeExec() */.
6ebe0 20 20 69 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e int nExtension
6ebf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6ec00 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61 /* Number of loa
6ec10 64 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a ded extensions *
6ec20 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45 78 74 65 /. void **aExte
6ec30 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 nsion;
6ec40 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 68 /* Array of sh
6ec50 61 72 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e ared library han
6ec60 64 6c 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28 dles */. void (
6ec70 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 *xTrace)(void*,c
6ec80 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 20 20 20 onst char*);
6ec90 20 20 20 20 2f 2a 20 54 72 61 63 65 20 66 75 6e /* Trace fun
6eca0 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 ction */. void
6ecb0 2a 70 54 72 61 63 65 41 72 67 3b 20 20 20 20 20 *pTraceArg;
6ecc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ecd0 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
6ece0 20 74 6f 20 74 68 65 20 74 72 61 63 65 20 66 75 to the trace fu
6ecf0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 nction */. void
6ed00 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 (*xProfile)(voi
6ed10 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 d*,const char*,u
6ed20 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c 69 64); /* Profili
6ed30 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 ng function */.
6ed40 20 76 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65 41 void *pProfileA
6ed50 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rg;
6ed60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
6ed70 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69 6c gument to profil
6ed80 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 e function */.
6ed90 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72 67 void *pCommitArg
6eda0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6edb0 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f /* Argument to
6edc0 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b xCommitCallback
6edd0 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20 28 () */ . int (
6ede0 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b *xCommitCallback
6edf0 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a 20 )(void*); /*
6ee00 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79 Invoked at every
6ee10 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f commit. */. vo
6ee20 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 67 id *pRollbackArg
6ee30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6ee40 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 /* Argument to x
6ee50 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b RollbackCallback
6ee60 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64 20 () */ . void
6ee70 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 (*xRollbackCallb
6ee80 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 ack)(void*); /*
6ee90 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79 Invoked at every
6eea0 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f commit. */. vo
6eeb0 69 64 20 2a 70 55 70 64 61 74 65 41 72 67 3b 0a id *pUpdateArg;.
6eec0 20 20 76 6f 69 64 20 28 2a 78 55 70 64 61 74 65 void (*xUpdate
6eed0 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c Callback)(void*,
6eee0 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a int, const char*
6eef0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c ,const char*,sql
6ef00 69 74 65 5f 69 6e 74 36 34 29 3b 0a 23 69 66 6e ite_int64);.#ifn
6ef10 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
6ef20 57 41 4c 0a 20 20 69 6e 74 20 28 2a 78 57 61 6c WAL. int (*xWal
6ef30 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a Callback)(void *
6ef40 2c 20 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e , sqlite3 *, con
6ef50 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b st char *, int);
6ef60 0a 20 20 76 6f 69 64 20 2a 70 57 61 6c 41 72 67 . void *pWalArg
6ef70 3b 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 28 ;.#endif. void(
6ef80 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f *xCollNeeded)(vo
6ef90 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 id*,sqlite3*,int
6efa0 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 eTextRep,const
6efb0 63 68 61 72 2a 29 3b 0a 20 20 76 6f 69 64 28 2a char*);. void(*
6efc0 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 xCollNeeded16)(v
6efd0 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e oid*,sqlite3*,in
6efe0 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 t eTextRep,const
6eff0 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 void*);. void
6f000 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b *pCollNeededArg;
6f010 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
6f020 20 2a 70 45 72 72 3b 20 20 20 20 20 20 20 20 20 *pErr;
6f030 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 /* Most recent
6f040 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f error message */
6f050 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 . char *zErrMsg
6f060 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6f070 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 /* Most recent
6f080 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55 error message (U
6f090 54 46 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a 2f TF-8 encoded) */
6f0a0 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 . char *zErrMsg
6f0b0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 16;
6f0c0 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 /* Most recent
6f0d0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55 error message (U
6f0e0 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 29 20 2a TF-16 encoded) *
6f0f0 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 /. union {.
6f100 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 49 volatile int isI
6f110 6e 74 65 72 72 75 70 74 65 64 3b 20 2f 2a 20 54 nterrupted; /* T
6f120 72 75 65 20 69 66 20 73 71 6c 69 74 65 33 5f 69 rue if sqlite3_i
6f130 6e 74 65 72 72 75 70 74 20 68 61 73 20 62 65 65 nterrupt has bee
6f140 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 n called */.
6f150 64 6f 75 62 6c 65 20 6e 6f 74 55 73 65 64 31 3b double notUsed1;
6f160 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
6f170 70 61 63 65 72 20 2a 2f 0a 20 20 7d 20 75 31 3b pacer */. } u1;
6f180 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f 6f . Lookaside loo
6f190 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 kaside;
6f1a0 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 /* Lookaside ma
6f1b0 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 72 61 74 69 lloc configurati
6f1c0 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 on */.#ifndef SQ
6f1d0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
6f1e0 49 5a 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28 2a IZATION. int (*
6f1f0 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 xAuth)(void*,int
6f200 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e ,const char*,con
6f210 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 st char*,const c
6f220 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
6f230 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
6f240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6f250 20 20 20 2f 2a 20 41 63 63 65 73 73 20 61 75 74 /* Access aut
6f260 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 horization funct
6f270 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ion */. void *p
6f280 41 75 74 68 41 72 67 3b 20 20 20 20 20 20 20 20 AuthArg;
6f290 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72 /* 1st ar
6f2a0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 63 gument to the ac
6f2b0 63 65 73 73 20 61 75 74 68 20 66 75 6e 63 74 69 cess auth functi
6f2c0 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 on */.#endif.#if
6f2d0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
6f2e0 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 _PROGRESS_CALLBA
6f2f0 43 4b 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f 67 CK. int (*xProg
6f300 72 65 73 73 29 28 76 6f 69 64 20 2a 29 3b 20 20 ress)(void *);
6f310 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 65 /* The progre
6f320 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 ss callback */.
6f330 20 76 6f 69 64 20 2a 70 50 72 6f 67 72 65 73 73 void *pProgress
6f340 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f Arg; /
6f350 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 * Argument to th
6f360 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 e progress callb
6f370 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 ack */. int nPr
6f380 6f 67 72 65 73 73 4f 70 73 3b 20 20 20 20 20 20 ogressOps;
6f390 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6f3a0 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 of opcodes for
6f3b0 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 progress callbac
6f3c0 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e k */.#endif.#ifn
6f3d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
6f3e0 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 VIRTUALTABLE. i
6f3f0 6e 74 20 6e 56 54 72 61 6e 73 3b 20 20 20 20 20 nt nVTrans;
6f400 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6f410 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f Allocated size o
6f420 66 20 61 56 54 72 61 6e 73 20 2a 2f 0a 20 20 48 f aVTrans */. H
6f430 61 73 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20 20 ash aModule;
6f440 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6f450 70 6f 70 75 6c 61 74 65 64 20 62 79 20 73 71 6c populated by sql
6f460 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 ite3_create_modu
6f470 6c 65 28 29 20 2a 2f 0a 20 20 56 74 61 62 43 74 le() */. VtabCt
6f480 78 20 2a 70 56 74 61 62 43 74 78 3b 20 20 20 20 x *pVtabCtx;
6f490 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 /* Conte
6f4a0 78 74 20 66 6f 72 20 61 63 74 69 76 65 20 76 74 xt for active vt
6f4b0 61 62 20 63 6f 6e 6e 65 63 74 2f 63 72 65 61 74 ab connect/creat
6f4c0 65 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 2a e */. VTable **
6f4d0 61 56 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 aVTrans;
6f4e0 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 /* Virtual
6f4f0 74 61 62 6c 65 73 20 77 69 74 68 20 6f 70 65 6e tables with open
6f500 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a 2f transactions */
6f510 0a 20 20 56 54 61 62 6c 65 20 2a 70 44 69 73 63 . VTable *pDisc
6f520 6f 6e 6e 65 63 74 3b 20 20 20 20 2f 2a 20 44 69 onnect; /* Di
6f530 73 63 6f 6e 6e 65 63 74 20 74 68 65 73 65 20 69 sconnect these i
6f540 6e 20 6e 65 78 74 20 73 71 6c 69 74 65 33 5f 70 n next sqlite3_p
6f550 72 65 70 61 72 65 28 29 20 2a 2f 0a 23 65 6e 64 repare() */.#end
6f560 69 66 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 if. FuncDefHash
6f570 20 61 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 aFunc;
6f580 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 /* Hash table
6f590 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 of connection f
6f5a0 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 48 61 unctions */. Ha
6f5b0 73 68 20 61 43 6f 6c 6c 53 65 71 3b 20 20 20 20 sh aCollSeq;
6f5c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
6f5d0 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 ll collating seq
6f5e0 75 65 6e 63 65 73 20 2a 2f 0a 20 20 42 75 73 79 uences */. Busy
6f5f0 48 61 6e 64 6c 65 72 20 62 75 73 79 48 61 6e 64 Handler busyHand
6f600 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20 42 75 73 ler; /* Bus
6f610 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 y callback */.
6f620 44 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d 3b Db aDbStatic[2];
6f630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6f640 20 53 74 61 74 69 63 20 73 70 61 63 65 20 66 6f Static space fo
6f650 72 20 74 68 65 20 32 20 64 65 66 61 75 6c 74 20 r the 2 default
6f660 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 53 61 backends */. Sa
6f670 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f vepoint *pSavepo
6f680 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c int; /* L
6f690 69 73 74 20 6f 66 20 61 63 74 69 76 65 20 73 61 ist of active sa
6f6a0 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e vepoints */. in
6f6b0 74 20 62 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 t busyTimeout;
6f6c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
6f6d0 75 73 79 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 usy handler time
6f6e0 6f 75 74 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a out, in msec */.
6f6f0 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 int nSavepoint
6f700 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6f710 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e /* Number of non
6f720 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 -transaction sav
6f730 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 epoints */. int
6f740 20 6e 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 nStatement;
6f750 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
6f760 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 73 mber of nested s
6f770 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 tatement-transac
6f780 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 69 36 34 20 tions */. i64
6f790 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 20 20 nDeferredCons;
6f7a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 74 /* Net
6f7b0 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 deferred constr
6f7c0 61 69 6e 74 73 20 74 68 69 73 20 74 72 61 6e 73 aints this trans
6f7d0 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 69 6e 74 action. */. int
6f7e0 20 2a 70 6e 42 79 74 65 73 46 72 65 65 64 3b 20 *pnBytesFreed;
6f7f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
6f800 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 6e 63 72 65 not NULL, incre
6f810 6d 65 6e 74 20 74 68 69 73 20 69 6e 20 44 62 46 ment this in DbF
6f820 72 65 65 28 29 20 2a 2f 0a 0a 23 69 66 64 65 66 ree() */..#ifdef
6f830 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 SQLITE_ENABLE_U
6f840 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 20 2f NLOCK_NOTIFY. /
6f850 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
6f860 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 61 6c variables are al
6f870 6c 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 l protected by t
6f880 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 he STATIC_MASTER
6f890 20 0a 20 20 2a 2a 20 6d 75 74 65 78 2c 20 6e 6f . ** mutex, no
6f8a0 74 20 62 79 20 73 71 6c 69 74 65 33 2e 6d 75 74 t by sqlite3.mut
6f8b0 65 78 2e 20 54 68 65 79 20 61 72 65 20 75 73 65 ex. They are use
6f8c0 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6e 6f 74 d by code in not
6f8d0 69 66 79 2e 63 2e 20 0a 20 20 2a 2a 0a 20 20 2a ify.c. . **. *
6f8e0 2a 20 57 68 65 6e 20 58 2e 70 55 6e 6c 6f 63 6b * When X.pUnlock
6f8f0 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 Connection==Y, t
6f900 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 58 hat means that X
6f910 20 69 73 20 77 61 69 74 69 6e 67 20 66 6f 72 20 is waiting for
6f920 59 20 74 6f 0a 20 20 2a 2a 20 75 6e 6c 6f 63 6b Y to. ** unlock
6f930 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 so that it can
6f940 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 proceed.. **.
6f950 2a 2a 20 57 68 65 6e 20 58 2e 70 42 6c 6f 63 6b ** When X.pBlock
6f960 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 ingConnection==Y
6f970 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 , that means tha
6f980 74 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 t something that
6f990 20 58 20 74 72 69 65 64 0a 20 20 2a 2a 20 74 72 X tried. ** tr
6f9a0 69 65 64 20 74 6f 20 64 6f 20 72 65 63 65 6e 74 ied to do recent
6f9b0 6c 79 20 66 61 69 6c 65 64 20 77 69 74 68 20 61 ly failed with a
6f9c0 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 n SQLITE_LOCKED
6f9d0 65 72 72 6f 72 20 64 75 65 20 74 6f 20 6c 6f 63 error due to loc
6f9e0 6b 73 0a 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 ks. ** held by
6f9f0 59 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 Y.. */. sqlite
6fa00 33 20 2a 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 3 *pBlockingConn
6fa10 65 63 74 69 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e 65 ection; /* Conne
6fa20 63 74 69 6f 6e 20 74 68 61 74 20 63 61 75 73 65 ction that cause
6fa30 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 d SQLITE_LOCKED
6fa40 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 55 */. sqlite3 *pU
6fa50 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3b nlockConnection;
6fa60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
6fa70 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 77 61 74 63 nnection to watc
6fa80 68 20 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a 2f 0a h for unlock */.
6fa90 20 20 76 6f 69 64 20 2a 70 55 6e 6c 6f 63 6b 41 void *pUnlockA
6faa0 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rg;
6fab0 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d /* Argum
6fac0 65 6e 74 20 74 6f 20 78 55 6e 6c 6f 63 6b 4e 6f ent to xUnlockNo
6fad0 74 69 66 79 20 2a 2f 0a 20 20 76 6f 69 64 20 28 tify */. void (
6fae0 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29 28 *xUnlockNotify)(
6faf0 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 3b 20 20 void **, int);
6fb00 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69 66 79 /* Unlock notify
6fb10 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 73 callback */. s
6fb20 71 6c 69 74 65 33 20 2a 70 4e 65 78 74 42 6c 6f qlite3 *pNextBlo
6fb30 63 6b 65 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 cked; /*
6fb40 4e 65 78 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 Next in list of
6fb50 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e all blocked conn
6fb60 65 63 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 ections */.#endi
6fb70 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 f.};../*.** A ma
6fb80 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 20 cro to discover
6fb90 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 the encoding of
6fba0 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 23 a database..*/.#
6fbb0 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29 20 28 define ENC(db) (
6fbc0 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 (db)->aDb[0].pSc
6fbd0 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a hema->enc)../*.*
6fbe0 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 * Possible value
6fbf0 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 s for the sqlite
6fc00 33 2e 66 6c 61 67 73 2e 0a 2a 2f 0a 23 64 65 66 3.flags..*/.#def
6fc10 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 62 65 54 ine SQLITE_VdbeT
6fc20 72 61 63 65 20 20 20 20 20 20 30 78 30 30 30 30 race 0x0000
6fc30 30 30 30 31 20 20 2f 2a 20 54 72 75 65 20 74 6f 0001 /* True to
6fc40 20 74 72 61 63 65 20 56 44 42 45 20 65 78 65 63 trace VDBE exec
6fc50 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 ution */.#define
6fc60 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 SQLITE_InternCh
6fc70 61 6e 67 65 73 20 20 30 78 30 30 30 30 30 30 30 anges 0x0000000
6fc80 32 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74 65 2 /* Uncommitte
6fc90 64 20 48 61 73 68 20 74 61 62 6c 65 20 63 68 61 d Hash table cha
6fca0 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nges */.#define
6fcb0 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 SQLITE_FullColNa
6fcc0 6d 65 73 20 20 20 30 78 30 30 30 30 30 30 30 34 mes 0x00000004
6fcd0 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20 63 /* Show full c
6fce0 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20 53 olumn names on S
6fcf0 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 6e 65 ELECT */.#define
6fd00 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c SQLITE_ShortCol
6fd10 4e 61 6d 65 73 20 20 30 78 30 30 30 30 30 30 30 Names 0x0000000
6fd20 38 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72 74 8 /* Show short
6fd30 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 2a columns names *
6fd40 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
6fd50 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20 20 _CountRows
6fd60 30 78 30 30 30 30 30 30 31 30 20 20 2f 2a 20 43 0x00000010 /* C
6fd70 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67 65 ount rows change
6fd80 64 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f 0a d by INSERT, */.
6fd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6fda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6fdb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 44 /* D
6fdc0 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45 ELETE, or UPDATE
6fdd0 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 and return */.
6fde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6fdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6fe00 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 74 68 /* th
6fe10 65 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 61 20 e count using a
6fe20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64 65 callback. */.#de
6fe30 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 6c 6c fine SQLITE_Null
6fe40 43 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 30 30 Callback 0x000
6fe50 30 30 30 32 30 20 20 2f 2a 20 49 6e 76 6f 6b 65 00020 /* Invoke
6fe60 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e the callback on
6fe70 63 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 20 20 ce if the */.
6fe80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6fe90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6fea0 20 20 20 20 20 20 20 2f 2a 20 20 20 72 65 73 75 /* resu
6feb0 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 20 lt set is empty
6fec0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
6fed0 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20 20 E_SqlTrace
6fee0 20 30 78 30 30 30 30 30 30 34 30 20 20 2f 2a 20 0x00000040 /*
6fef0 44 65 62 75 67 20 70 72 69 6e 74 20 53 51 4c 20 Debug print SQL
6ff00 61 73 20 69 74 20 65 78 65 63 75 74 65 73 20 2a as it executes *
6ff10 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
6ff20 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20 20 _VdbeListing
6ff30 30 78 30 30 30 30 30 30 38 30 20 20 2f 2a 20 44 0x00000080 /* D
6ff40 65 62 75 67 20 6c 69 73 74 69 6e 67 73 20 6f 66 ebug listings of
6ff50 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20 2a VDBE programs *
6ff60 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
6ff70 5f 57 72 69 74 65 53 63 68 65 6d 61 20 20 20 20 _WriteSchema
6ff80 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 4f 0x00000100 /* O
6ff90 4b 20 74 6f 20 75 70 64 61 74 65 20 53 51 4c 49 K to update SQLI
6ffa0 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 20 20 20 TE_MASTER */.
6ffb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ffc0 20 20 20 20 20 20 2f 2a 20 20 20 30 78 30 30 30 /* 0x000
6ffd0 30 30 32 30 30 20 20 55 6e 75 73 65 64 20 2a 2f 00200 Unused */
6ffe0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
6fff0 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 20 20 30 IgnoreChecks 0
70000 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 44 6f x00000400 /* Do
70010 20 6e 6f 74 20 65 6e 66 6f 72 63 65 20 63 68 65 not enforce che
70020 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a ck constraints *
70030 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
70040 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 _ReadUncommitted
70050 20 30 78 30 30 30 30 38 30 30 20 20 2f 2a 20 46 0x0000800 /* F
70060 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 or shared-cache
70070 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 mode */.#define
70080 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c SQLITE_LegacyFil
70090 65 46 6d 74 20 20 30 78 30 30 30 30 31 30 30 30 eFmt 0x00001000
700a0 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20 /* Create new
700b0 64 61 74 61 62 61 73 65 73 20 69 6e 20 66 6f 72 databases in for
700c0 6d 61 74 20 31 20 2a 2f 0a 23 64 65 66 69 6e 65 mat 1 */.#define
700d0 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e SQLITE_FullFSyn
700e0 63 20 20 20 20 20 20 30 78 30 30 30 30 32 30 30 c 0x0000200
700f0 30 20 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20 66 0 /* Use full f
70100 73 79 6e 63 20 6f 6e 20 74 68 65 20 62 61 63 6b sync on the back
70110 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 end */.#define S
70120 51 4c 49 54 45 5f 43 6b 70 74 46 75 6c 6c 46 53 QLITE_CkptFullFS
70130 79 6e 63 20 20 30 78 30 30 30 30 34 30 30 30 20 ync 0x00004000
70140 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20 66 73 79 /* Use full fsy
70150 6e 63 20 66 6f 72 20 63 68 65 63 6b 70 6f 69 6e nc for checkpoin
70160 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c t */.#define SQL
70170 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 ITE_RecoveryMode
70180 20 20 20 30 78 30 30 30 30 38 30 30 30 20 20 2f 0x00008000 /
70190 2a 20 49 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 * Ignore schema
701a0 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e errors */.#defin
701b0 65 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73 65 e SQLITE_Reverse
701c0 4f 72 64 65 72 20 20 20 30 78 30 30 30 31 30 30 Order 0x000100
701d0 30 30 20 20 2f 2a 20 52 65 76 65 72 73 65 20 75 00 /* Reverse u
701e0 6e 6f 72 64 65 72 65 64 20 53 45 4c 45 43 54 73 nordered SELECTs
701f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
70200 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 20 20 TE_RecTriggers
70210 20 20 30 78 30 30 30 32 30 30 30 30 20 20 2f 2a 0x00020000 /*
70220 20 45 6e 61 62 6c 65 20 72 65 63 75 72 73 69 76 Enable recursiv
70230 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 64 e triggers */.#d
70240 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 6f 72 efine SQLITE_For
70250 65 69 67 6e 4b 65 79 73 20 20 20 20 30 78 30 30 eignKeys 0x00
70260 30 34 30 30 30 30 20 20 2f 2a 20 45 6e 66 6f 72 040000 /* Enfor
70270 63 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 ce foreign key c
70280 6f 6e 73 74 72 61 69 6e 74 73 20 20 2a 2f 0a 23 onstraints */.#
70290 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 75 define SQLITE_Au
702a0 74 6f 49 6e 64 65 78 20 20 20 20 20 20 30 78 30 toIndex 0x0
702b0 30 30 38 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 0080000 /* Enab
702c0 6c 65 20 61 75 74 6f 6d 61 74 69 63 20 69 6e 64 le automatic ind
702d0 65 78 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 exes */.#define
702e0 53 51 4c 49 54 45 5f 50 72 65 66 65 72 42 75 69 SQLITE_PreferBui
702f0 6c 74 69 6e 20 20 30 78 30 30 31 30 30 30 30 30 ltin 0x00100000
70300 20 20 2f 2a 20 50 72 65 66 65 72 65 6e 63 65 20 /* Preference
70310 74 6f 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 to built-in func
70320 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
70330 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f ITE_LoadExtensio
70340 6e 20 20 30 78 30 30 32 30 30 30 30 30 20 20 2f n 0x00200000 /
70350 2a 20 45 6e 61 62 6c 65 20 6c 6f 61 64 5f 65 78 * Enable load_ex
70360 74 65 6e 73 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 tension */.#defi
70370 6e 65 20 53 51 4c 49 54 45 5f 45 6e 61 62 6c 65 ne SQLITE_Enable
70380 54 72 69 67 67 65 72 20 20 30 78 30 30 34 30 30 Trigger 0x00400
70390 30 30 30 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 000 /* True to
703a0 65 6e 61 62 6c 65 20 74 72 69 67 67 65 72 73 20 enable triggers
703b0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 73 20 6f */../*.** Bits o
703c0 66 20 74 68 65 20 73 71 6c 69 74 65 33 2e 64 62 f the sqlite3.db
703d0 4f 70 74 46 6c 61 67 73 20 66 69 65 6c 64 20 74 OptFlags field t
703e0 68 61 74 20 61 72 65 20 75 73 65 64 20 62 79 20 hat are used by
703f0 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 the.** sqlite3_t
70400 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 est_control(SQLI
70410 54 45 5f 54 45 53 54 43 54 52 4c 5f 4f 50 54 49 TE_TESTCTRL_OPTI
70420 4d 49 5a 41 54 49 4f 4e 53 2c 2e 2e 2e 29 20 69 MIZATIONS,...) i
70430 6e 74 65 72 66 61 63 65 20 74 6f 0a 2a 2a 20 73 nterface to.** s
70440 65 6c 65 63 74 69 76 65 6c 79 20 64 69 73 61 62 electively disab
70450 6c 65 20 76 61 72 69 6f 75 73 20 6f 70 74 69 6d le various optim
70460 69 7a 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 izations..*/.#de
70470 66 69 6e 65 20 53 51 4c 49 54 45 5f 51 75 65 72 fine SQLITE_Quer
70480 79 46 6c 61 74 74 65 6e 65 72 20 30 78 30 30 30 yFlattener 0x000
70490 31 20 20 20 2f 2a 20 51 75 65 72 79 20 66 6c 61 1 /* Query fla
704a0 74 74 65 6e 69 6e 67 20 2a 2f 0a 23 64 65 66 69 ttening */.#defi
704b0 6e 65 20 53 51 4c 49 54 45 5f 43 6f 6c 75 6d 6e ne SQLITE_Column
704c0 43 61 63 68 65 20 20 20 20 30 78 30 30 30 32 20 Cache 0x0002
704d0 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 63 61 63 68 /* Column cach
704e0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c e */.#define SQL
704f0 49 54 45 5f 47 72 6f 75 70 42 79 4f 72 64 65 72 ITE_GroupByOrder
70500 20 20 20 30 78 30 30 30 34 20 20 20 2f 2a 20 47 0x0004 /* G
70510 52 4f 55 50 42 59 20 63 6f 76 65 72 20 6f 66 20 ROUPBY cover of
70520 4f 52 44 45 52 42 59 20 2a 2f 0a 23 64 65 66 69 ORDERBY */.#defi
70530 6e 65 20 53 51 4c 49 54 45 5f 46 61 63 74 6f 72 ne SQLITE_Factor
70540 4f 75 74 43 6f 6e 73 74 20 30 78 30 30 30 38 20 OutConst 0x0008
70550 20 20 2f 2a 20 43 6f 6e 73 74 61 6e 74 20 66 61 /* Constant fa
70560 63 74 6f 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 ctoring */.#defi
70570 6e 65 20 53 51 4c 49 54 45 5f 49 64 78 52 65 61 ne SQLITE_IdxRea
70580 6c 41 73 49 6e 74 20 20 20 30 78 30 30 31 30 20 lAsInt 0x0010
70590 20 20 2f 2a 20 53 74 6f 72 65 20 52 45 41 4c 20 /* Store REAL
705a0 61 73 20 49 4e 54 20 69 6e 20 69 6e 64 69 63 65 as INT in indice
705b0 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
705c0 49 54 45 5f 44 69 73 74 69 6e 63 74 4f 70 74 20 ITE_DistinctOpt
705d0 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a 20 44 0x0020 /* D
705e0 49 53 54 49 4e 43 54 20 75 73 69 6e 67 20 69 6e ISTINCT using in
705f0 64 65 78 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 dexes */.#define
70600 20 53 51 4c 49 54 45 5f 43 6f 76 65 72 49 64 78 SQLITE_CoverIdx
70610 53 63 61 6e 20 20 20 30 78 30 30 34 30 20 20 20 Scan 0x0040
70620 2f 2a 20 43 6f 76 65 72 69 6e 67 20 69 6e 64 65 /* Covering inde
70630 78 20 73 63 61 6e 73 20 2a 2f 0a 23 64 65 66 69 x scans */.#defi
70640 6e 65 20 53 51 4c 49 54 45 5f 4f 72 64 65 72 42 ne SQLITE_OrderB
70650 79 49 64 78 4a 6f 69 6e 20 30 78 30 30 38 30 20 yIdxJoin 0x0080
70660 20 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 6f 66 /* ORDER BY of
70670 20 6a 6f 69 6e 73 20 76 69 61 20 69 6e 64 65 78 joins via index
70680 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
70690 54 45 5f 53 75 62 71 43 6f 72 6f 75 74 69 6e 65 TE_SubqCoroutine
706a0 20 20 30 78 30 31 30 30 20 20 20 2f 2a 20 45 76 0x0100 /* Ev
706b0 61 6c 75 61 74 65 20 73 75 62 71 75 65 72 69 65 aluate subquerie
706c0 73 20 61 73 20 63 6f 72 6f 75 74 69 6e 65 73 20 s as coroutines
706d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
706e0 45 5f 41 6c 6c 4f 70 74 73 20 20 20 20 20 20 20 E_AllOpts
706f0 20 30 78 66 66 66 66 20 20 20 2f 2a 20 41 6c 6c 0xffff /* All
70700 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 2a optimizations *
70710 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 /../*.** Macros
70720 66 6f 72 20 74 65 73 74 69 6e 67 20 77 68 65 74 for testing whet
70730 68 65 72 20 6f 72 20 6e 6f 74 20 6f 70 74 69 6d her or not optim
70740 69 7a 61 74 69 6f 6e 73 20 61 72 65 20 65 6e 61 izations are ena
70750 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 bled or disabled
70760 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
70770 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
70780 5f 54 45 53 54 0a 23 64 65 66 69 6e 65 20 4f 70 _TEST.#define Op
70790 74 69 6d 69 7a 61 74 69 6f 6e 44 69 73 61 62 6c timizationDisabl
707a0 65 64 28 64 62 2c 20 6d 61 73 6b 29 20 20 28 28 ed(db, mask) ((
707b0 28 64 62 29 2d 3e 64 62 4f 70 74 46 6c 61 67 73 (db)->dbOptFlags
707c0 26 28 6d 61 73 6b 29 29 21 3d 30 29 0a 23 64 65 &(mask))!=0).#de
707d0 66 69 6e 65 20 4f 70 74 69 6d 69 7a 61 74 69 6f fine Optimizatio
707e0 6e 45 6e 61 62 6c 65 64 28 64 62 2c 20 6d 61 73 nEnabled(db, mas
707f0 6b 29 20 20 20 28 28 28 64 62 29 2d 3e 64 62 4f k) (((db)->dbO
70800 70 74 46 6c 61 67 73 26 28 6d 61 73 6b 29 29 3d ptFlags&(mask))=
70810 3d 30 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e =0).#else.#defin
70820 65 20 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 44 69 e OptimizationDi
70830 73 61 62 6c 65 64 28 64 62 2c 20 6d 61 73 6b 29 sabled(db, mask)
70840 20 20 30 0a 23 64 65 66 69 6e 65 20 4f 70 74 69 0.#define Opti
70850 6d 69 7a 61 74 69 6f 6e 45 6e 61 62 6c 65 64 28 mizationEnabled(
70860 64 62 2c 20 6d 61 73 6b 29 20 20 20 31 0a 23 65 db, mask) 1.#e
70870 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 ndif../*.** Poss
70880 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20 ible values for
70890 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63 the sqlite.magic
708a0 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 65 20 6e field..** The n
708b0 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74 61 69 umbers are obtai
708c0 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d 20 61 6e ned at random an
708d0 64 20 68 61 76 65 20 6e 6f 20 73 70 65 63 69 61 d have no specia
708e0 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74 68 65 72 l meaning, other
708f0 0a 2a 2a 20 74 68 61 6e 20 62 65 69 6e 67 20 64 .** than being d
70900 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 6f 6e 65 istinct from one
70910 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 23 64 65 another..*/.#de
70920 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 fine SQLITE_MAGI
70930 43 5f 4f 50 45 4e 20 20 20 20 20 30 78 61 30 32 C_OPEN 0xa02
70940 39 61 36 39 37 20 20 2f 2a 20 44 61 74 61 62 61 9a697 /* Databa
70950 73 65 20 69 73 20 6f 70 65 6e 20 2a 2f 0a 23 64 se is open */.#d
70960 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 efine SQLITE_MAG
70970 49 43 5f 43 4c 4f 53 45 44 20 20 20 30 78 39 66 IC_CLOSED 0x9f
70980 33 63 32 64 33 33 20 20 2f 2a 20 44 61 74 61 62 3c2d33 /* Datab
70990 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20 2a 2f ase is closed */
709a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
709b0 4d 41 47 49 43 5f 53 49 43 4b 20 20 20 20 20 30 MAGIC_SICK 0
709c0 78 34 62 37 37 31 32 39 30 20 20 2f 2a 20 45 72 x4b771290 /* Er
709d0 72 6f 72 20 61 6e 64 20 61 77 61 69 74 69 6e 67 ror and awaiting
709e0 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 65 66 69 6e close */.#defin
709f0 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 e SQLITE_MAGIC_B
70a00 55 53 59 20 20 20 20 20 30 78 66 30 33 62 37 39 USY 0xf03b79
70a10 30 36 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 06 /* Database
70a20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 currently in use
70a30 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
70a40 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 20 20 TE_MAGIC_ERROR
70a50 20 20 30 78 62 35 33 35 37 39 33 30 20 20 2f 2a 0xb5357930 /*
70a60 20 41 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 An SQLITE_MISUS
70a70 45 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 E error occurred
70a80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
70a90 54 45 5f 4d 41 47 49 43 5f 5a 4f 4d 42 49 45 20 TE_MAGIC_ZOMBIE
70aa0 20 20 30 78 36 34 63 66 66 63 37 66 20 20 2f 2a 0x64cffc7f /*
70ab0 20 43 6c 6f 73 65 20 77 69 74 68 20 6c 61 73 74 Close with last
70ac0 20 73 74 61 74 65 6d 65 6e 74 20 63 6c 6f 73 65 statement close
70ad0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 */../*.** Each
70ae0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 SQL function is
70af0 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69 6e defined by an in
70b00 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
70b10 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 llowing.** struc
70b20 74 75 72 65 2e 20 20 41 20 70 6f 69 6e 74 65 72 ture. A pointer
70b30 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 to this structu
70b40 72 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 re is stored in
70b50 74 68 65 20 73 71 6c 69 74 65 2e 61 46 75 6e 63 the sqlite.aFunc
70b60 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 20 .** hash table.
70b70 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 66 When multiple f
70b80 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 74 68 unctions have th
70b90 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 74 68 65 e same name, the
70ba0 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 70 hash table.** p
70bb0 6f 69 6e 74 73 20 74 6f 20 61 20 6c 69 6e 6b 65 oints to a linke
70bc0 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65 20 d list of these
70bd0 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 73 structures..*/.s
70be0 74 72 75 63 74 20 46 75 6e 63 44 65 66 20 7b 0a truct FuncDef {.
70bf0 20 20 69 31 36 20 6e 41 72 67 3b 20 20 20 20 20 i16 nArg;
70c00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
70c10 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 of arguments.
70c20 2d 31 20 6d 65 61 6e 73 20 75 6e 6c 69 6d 69 74 -1 means unlimit
70c30 65 64 20 2a 2f 0a 20 20 75 38 20 69 50 72 65 66 ed */. u8 iPref
70c40 45 6e 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Enc; /*
70c50 50 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 Preferred text e
70c60 6e 63 6f 64 69 6e 67 20 28 53 51 4c 49 54 45 5f ncoding (SQLITE_
70c70 55 54 46 38 2c 20 31 36 4c 45 2c 20 31 36 42 45 UTF8, 16LE, 16BE
70c80 29 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 73 3b ) */. u8 flags;
70c90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
70ca0 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 ome combination
70cb0 6f 66 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 2a of SQLITE_FUNC_*
70cc0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 */. void *pUse
70cd0 72 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 55 73 rData; /* Us
70ce0 65 72 20 64 61 74 61 20 70 61 72 61 6d 65 74 65 er data paramete
70cf0 72 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a r */. FuncDef *
70d00 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e pNext; /* N
70d10 65 78 74 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 ext function wit
70d20 68 20 73 61 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20 h same name */.
70d30 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 void (*xFunc)(s
70d40 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
70d50 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
70d60 65 2a 2a 29 3b 20 2f 2a 20 52 65 67 75 6c 61 72 e**); /* Regular
70d70 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 function */. v
70d80 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c oid (*xStep)(sql
70d90 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
70da0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t,sqlite3_value*
70db0 2a 29 3b 20 2f 2a 20 41 67 67 72 65 67 61 74 65 *); /* Aggregate
70dc0 20 73 74 65 70 20 2a 2f 0a 20 20 76 6f 69 64 20 step */. void
70dd0 28 2a 78 46 69 6e 61 6c 69 7a 65 29 28 73 71 6c (*xFinalize)(sql
70de0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 20 ite3_context*);
70df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
70e00 2a 20 41 67 67 72 65 67 61 74 65 20 66 69 6e 61 * Aggregate fina
70e10 6c 69 7a 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 lizer */. char
70e20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 *zName;
70e30 2f 2a 20 53 51 4c 20 6e 61 6d 65 20 6f 66 20 74 /* SQL name of t
70e40 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a he function. */.
70e50 20 20 46 75 6e 63 44 65 66 20 2a 70 48 61 73 68 FuncDef *pHash
70e60 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 77 ; /* Next w
70e70 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 ith a different
70e80 6e 61 6d 65 20 62 75 74 20 74 68 65 20 73 61 6d name but the sam
70e90 65 20 68 61 73 68 20 2a 2f 0a 20 20 46 75 6e 63 e hash */. Func
70ea0 44 65 73 74 72 75 63 74 6f 72 20 2a 70 44 65 73 Destructor *pDes
70eb0 74 72 75 63 74 6f 72 3b 20 20 20 2f 2a 20 52 65 tructor; /* Re
70ec0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 20 ference counted
70ed0 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 destructor funct
70ee0 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ion */.};../*.**
70ef0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
70f00 65 6e 63 61 70 73 75 6c 61 74 65 73 20 61 20 75 encapsulates a u
70f10 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 73 ser-function des
70f20 74 72 75 63 74 6f 72 20 63 61 6c 6c 62 61 63 6b tructor callback
70f30 20 28 61 73 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 (as.** configur
70f40 65 64 20 75 73 69 6e 67 20 63 72 65 61 74 65 5f ed using create_
70f50 66 75 6e 63 74 69 6f 6e 5f 76 32 28 29 29 20 61 function_v2()) a
70f60 6e 64 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 nd a reference c
70f70 6f 75 6e 74 65 72 2e 20 57 68 65 6e 0a 2a 2a 20 ounter. When.**
70f80 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 5f create_function_
70f90 76 32 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74 v2() is called t
70fa0 6f 20 63 72 65 61 74 65 20 61 20 66 75 6e 63 74 o create a funct
70fb0 69 6f 6e 20 77 69 74 68 20 61 20 64 65 73 74 72 ion with a destr
70fc0 75 63 74 6f 72 2c 0a 2a 2a 20 61 20 73 69 6e 67 uctor,.** a sing
70fd0 6c 65 20 6f 62 6a 65 63 74 20 6f 66 20 74 68 69 le object of thi
70fe0 73 20 74 79 70 65 20 69 73 20 61 6c 6c 6f 63 61 s type is alloca
70ff0 74 65 64 2e 20 46 75 6e 63 44 65 73 74 72 75 63 ted. FuncDestruc
71000 74 6f 72 2e 6e 52 65 66 20 69 73 20 73 65 74 20 tor.nRef is set
71010 74 6f 20 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 to .** the numbe
71020 72 20 6f 66 20 46 75 6e 63 44 65 66 20 6f 62 6a r of FuncDef obj
71030 65 63 74 73 20 63 72 65 61 74 65 64 20 28 65 69 ects created (ei
71040 74 68 65 72 20 31 20 6f 72 20 33 2c 20 64 65 70 ther 1 or 3, dep
71050 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 ending on whethe
71060 72 0a 2a 2a 20 6f 72 20 6e 6f 74 20 74 68 65 20 r.** or not the
71070 73 70 65 63 69 66 69 65 64 20 65 6e 63 6f 64 69 specified encodi
71080 6e 67 20 69 73 20 53 51 4c 49 54 45 5f 41 4e 59 ng is SQLITE_ANY
71090 29 2e 20 54 68 65 20 46 75 6e 63 44 65 66 2e 70 ). The FuncDef.p
710a0 44 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 6d 65 Destructor.** me
710b0 6d 62 65 72 20 6f 66 20 65 61 63 68 20 6f 66 20 mber of each of
710c0 74 68 65 20 6e 65 77 20 46 75 6e 63 44 65 66 20 the new FuncDef
710d0 6f 62 6a 65 63 74 73 20 69 73 20 73 65 74 20 74 objects is set t
710e0 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 61 o point to the a
710f0 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 46 75 6e 63 llocated.** Func
71100 44 65 73 74 72 75 63 74 6f 72 2e 0a 2a 2a 0a 2a Destructor..**.*
71110 2a 20 54 68 65 72 65 61 66 74 65 72 2c 20 77 68 * Thereafter, wh
71120 65 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 46 75 en one of the Fu
71130 6e 63 44 65 66 20 6f 62 6a 65 63 74 73 20 69 73 ncDef objects is
71140 20 64 65 6c 65 74 65 64 2c 20 74 68 65 20 72 65 deleted, the re
71150 66 65 72 65 6e 63 65 0a 2a 2a 20 63 6f 75 6e 74 ference.** count
71160 20 6f 6e 20 74 68 69 73 20 6f 62 6a 65 63 74 20 on this object
71170 69 73 20 64 65 63 72 65 6d 65 6e 74 65 64 2e 20 is decremented.
71180 57 68 65 6e 20 69 74 20 72 65 61 63 68 65 73 20 When it reaches
71190 30 2c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 0, the destructo
711a0 72 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20 r.** is invoked
711b0 61 6e 64 20 74 68 65 20 46 75 6e 63 44 65 73 74 and the FuncDest
711c0 72 75 63 74 6f 72 20 73 74 72 75 63 74 75 72 65 ructor structure
711d0 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 freed..*/.struc
711e0 74 20 46 75 6e 63 44 65 73 74 72 75 63 74 6f 72 t FuncDestructor
711f0 20 7b 0a 20 20 69 6e 74 20 6e 52 65 66 3b 0a 20 {. int nRef;.
71200 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 void (*xDestroy
71210 29 28 76 6f 69 64 20 2a 29 3b 0a 20 20 76 6f 69 )(void *);. voi
71220 64 20 2a 70 55 73 65 72 44 61 74 61 3b 0a 7d 3b d *pUserData;.};
71230 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 ../*.** Possible
71240 20 76 61 6c 75 65 73 20 66 6f 72 20 46 75 6e 63 values for Func
71250 44 65 66 2e 66 6c 61 67 73 2e 20 20 4e 6f 74 65 Def.flags. Note
71260 20 74 68 61 74 20 74 68 65 20 5f 4c 45 4e 47 54 that the _LENGT
71270 48 20 61 6e 64 20 5f 54 59 50 45 4f 46 0a 2a 2a H and _TYPEOF.**
71280 20 76 61 6c 75 65 73 20 6d 75 73 74 20 63 6f 72 values must cor
71290 72 65 73 70 6f 6e 64 20 74 6f 20 4f 50 46 4c 41 respond to OPFLA
712a0 47 5f 4c 45 4e 47 54 48 41 52 47 20 61 6e 64 20 G_LENGTHARG and
712b0 4f 50 46 4c 41 47 5f 54 59 50 45 4f 46 41 52 47 OPFLAG_TYPEOFARG
712c0 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20 . There.** are
712d0 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
712e0 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 64 65 20 nts in the code
712f0 74 6f 20 76 65 72 69 66 79 20 74 68 69 73 2e 0a to verify this..
71300 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
71310 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20 20 20 E_FUNC_LIKE
71320 30 78 30 31 20 2f 2a 20 43 61 6e 64 69 64 61 74 0x01 /* Candidat
71330 65 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f e for the LIKE o
71340 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a 23 ptimization */.#
71350 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 define SQLITE_FU
71360 4e 43 5f 43 41 53 45 20 20 20 20 20 30 78 30 32 NC_CASE 0x02
71370 20 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 69 /* Case-sensiti
71380 76 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e ve LIKE-type fun
71390 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 ction */.#define
713a0 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 SQLITE_FUNC_EPH
713b0 45 4d 20 20 20 20 30 78 30 34 20 2f 2a 20 45 70 EM 0x04 /* Ep
713c0 68 65 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 65 hemeral. Delete
713d0 20 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 23 64 with VDBE */.#d
713e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e efine SQLITE_FUN
713f0 43 5f 4e 45 45 44 43 4f 4c 4c 20 30 78 30 38 20 C_NEEDCOLL 0x08
71400 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e /* sqlite3GetFun
71410 63 43 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68 74 cCollSeq() might
71420 20 62 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 23 64 be called */.#d
71430 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e efine SQLITE_FUN
71440 43 5f 43 4f 55 4e 54 20 20 20 20 30 78 31 30 20 C_COUNT 0x10
71450 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 63 6f 75 6e /* Built-in coun
71460 74 28 2a 29 20 61 67 67 72 65 67 61 74 65 20 2a t(*) aggregate *
71470 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
71480 5f 46 55 4e 43 5f 43 4f 41 4c 45 53 43 45 20 30 _FUNC_COALESCE 0
71490 78 32 30 20 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 x20 /* Built-in
714a0 63 6f 61 6c 65 73 63 65 28 29 20 6f 72 20 69 66 coalesce() or if
714b0 6e 75 6c 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 null() function
714c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
714d0 45 5f 46 55 4e 43 5f 4c 45 4e 47 54 48 20 20 20 E_FUNC_LENGTH
714e0 30 78 34 30 20 2f 2a 20 42 75 69 6c 74 2d 69 6e 0x40 /* Built-in
714f0 20 6c 65 6e 67 74 68 28 29 20 66 75 6e 63 74 69 length() functi
71500 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 on */.#define SQ
71510 4c 49 54 45 5f 46 55 4e 43 5f 54 59 50 45 4f 46 LITE_FUNC_TYPEOF
71520 20 20 20 30 78 38 30 20 2f 2a 20 42 75 69 6c 74 0x80 /* Built
71530 2d 69 6e 20 74 79 70 65 6f 66 28 29 20 66 75 6e -in typeof() fun
71540 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 ction */../*.**
71550 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 The following th
71560 72 65 65 20 6d 61 63 72 6f 73 2c 20 46 55 4e 43 ree macros, FUNC
71570 54 49 4f 4e 28 29 2c 20 4c 49 4b 45 46 55 4e 43 TION(), LIKEFUNC
71580 28 29 20 61 6e 64 20 41 47 47 52 45 47 41 54 45 () and AGGREGATE
71590 28 29 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74 () are.** used t
715a0 6f 20 63 72 65 61 74 65 20 74 68 65 20 69 6e 69 o create the ini
715b0 74 69 61 6c 69 7a 65 72 73 20 66 6f 72 20 74 68 tializers for th
715c0 65 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 e FuncDef struct
715d0 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 55 ures..**.** FU
715e0 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 NCTION(zName, nA
715f0 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 rg, iArg, bNC, x
71600 46 75 6e 63 29 0a 2a 2a 20 20 20 20 20 55 73 65 Func).** Use
71610 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 d to create a sc
71620 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 alar function de
71630 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 finition of a fu
71640 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a nction zName .**
71650 20 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65 64 implemented
71660 20 62 79 20 43 20 66 75 6e 63 74 69 6f 6e 20 78 by C function x
71670 46 75 6e 63 20 74 68 61 74 20 61 63 63 65 70 74 Func that accept
71680 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 s nArg arguments
71690 2e 20 54 68 65 0a 2a 2a 20 20 20 20 20 76 61 6c . The.** val
716a0 75 65 20 70 61 73 73 65 64 20 61 73 20 69 41 72 ue passed as iAr
716b0 67 20 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 g is cast to a (
716c0 76 6f 69 64 2a 29 20 61 6e 64 20 6d 61 64 65 20 void*) and made
716d0 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 20 20 20 available.**
716e0 20 61 73 20 74 68 65 20 75 73 65 72 2d 64 61 74 as the user-dat
716f0 61 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f a (sqlite3_user_
71700 64 61 74 61 28 29 29 20 66 6f 72 20 74 68 65 20 data()) for the
71710 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 0a 2a 2a function. If .**
71720 20 20 20 20 20 61 72 67 75 6d 65 6e 74 20 62 4e argument bN
71730 43 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 C is true, then
71740 74 68 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f the SQLITE_FUNC_
71750 4e 45 45 44 43 4f 4c 4c 20 66 6c 61 67 20 69 73 NEEDCOLL flag is
71760 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 47 set..**.** AG
71770 47 52 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e GREGATE(zName, n
71780 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 Arg, iArg, bNC,
71790 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 0a 2a xStep, xFinal).*
717a0 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 * Used to cr
717b0 65 61 74 65 20 61 6e 20 61 67 67 72 65 67 61 74 eate an aggregat
717c0 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e e function defin
717d0 69 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 ition implemente
717e0 64 20 62 79 0a 2a 2a 20 20 20 20 20 74 68 65 20 d by.** the
717f0 43 20 66 75 6e 63 74 69 6f 6e 73 20 78 53 74 65 C functions xSte
71800 70 20 61 6e 64 20 78 46 69 6e 61 6c 2e 20 54 68 p and xFinal. Th
71810 65 20 66 69 72 73 74 20 66 6f 75 72 20 70 61 72 e first four par
71820 61 6d 65 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 ameters.** a
71830 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 69 re interpreted i
71840 6e 20 74 68 65 20 73 61 6d 65 20 77 61 79 20 61 n the same way a
71850 73 20 74 68 65 20 66 69 72 73 74 20 34 20 70 61 s the first 4 pa
71860 72 61 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 20 rameters to.**
71870 20 20 20 46 55 4e 43 54 49 4f 4e 28 29 2e 0a 2a FUNCTION()..*
71880 2a 0a 2a 2a 20 20 20 4c 49 4b 45 46 55 4e 43 28 *.** LIKEFUNC(
71890 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 zName, nArg, pAr
718a0 67 2c 20 66 6c 61 67 73 29 0a 2a 2a 20 20 20 20 g, flags).**
718b0 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 Used to create
718c0 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f a scalar functio
718d0 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 n definition of
718e0 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 a function zName
718f0 20 0a 2a 2a 20 20 20 20 20 74 68 61 74 20 61 63 .** that ac
71900 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 75 6d cepts nArg argum
71910 65 6e 74 73 20 61 6e 64 20 69 73 20 69 6d 70 6c ents and is impl
71920 65 6d 65 6e 74 65 64 20 62 79 20 61 20 63 61 6c emented by a cal
71930 6c 20 74 6f 20 43 20 0a 2a 2a 20 20 20 20 20 66 l to C .** f
71940 75 6e 63 74 69 6f 6e 20 6c 69 6b 65 46 75 6e 63 unction likeFunc
71950 2e 20 41 72 67 75 6d 65 6e 74 20 70 41 72 67 20 . Argument pArg
71960 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f is cast to a (vo
71970 69 64 20 2a 29 20 61 6e 64 20 6d 61 64 65 0a 2a id *) and made.*
71980 2a 20 20 20 20 20 61 76 61 69 6c 61 62 6c 65 20 * available
71990 61 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 as the function
719a0 75 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 user-data (sqlit
719b0 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29 2e e3_user_data()).
719c0 20 54 68 65 0a 2a 2a 20 20 20 20 20 46 75 6e 63 The.** Func
719d0 44 65 66 2e 66 6c 61 67 73 20 76 61 72 69 61 62 Def.flags variab
719e0 6c 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 le is set to the
719f0 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 value passed as
71a00 20 74 68 65 20 66 6c 61 67 73 0a 2a 2a 20 20 20 the flags.**
71a10 20 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a parameter..*/.
71a20 23 64 65 66 69 6e 65 20 46 55 4e 43 54 49 4f 4e #define FUNCTION
71a30 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 (zName, nArg, iA
71a40 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 rg, bNC, xFunc)
71a50 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 \. {nArg, SQLIT
71a60 45 5f 55 54 46 38 2c 20 28 62 4e 43 2a 53 51 4c E_UTF8, (bNC*SQL
71a70 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c ITE_FUNC_NEEDCOL
71a80 4c 29 2c 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f L), \. SQLITE_
71a90 49 4e 54 5f 54 4f 5f 50 54 52 28 69 41 72 67 29 INT_TO_PTR(iArg)
71aa0 2c 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 , 0, xFunc, 0, 0
71ab0 2c 20 23 7a 4e 61 6d 65 2c 20 30 2c 20 30 7d 0a , #zName, 0, 0}.
71ac0 23 64 65 66 69 6e 65 20 46 55 4e 43 54 49 4f 4e #define FUNCTION
71ad0 32 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 2(zName, nArg, i
71ae0 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 2c Arg, bNC, xFunc,
71af0 20 65 78 74 72 61 46 6c 61 67 73 29 20 5c 0a 20 extraFlags) \.
71b00 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 {nArg, SQLITE_U
71b10 54 46 38 2c 20 28 62 4e 43 2a 53 51 4c 49 54 45 TF8, (bNC*SQLITE
71b20 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 29 7c _FUNC_NEEDCOLL)|
71b30 65 78 74 72 61 46 6c 61 67 73 2c 20 5c 0a 20 20 extraFlags, \.
71b40 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 SQLITE_INT_TO_P
71b50 54 52 28 69 41 72 67 29 2c 20 30 2c 20 78 46 75 TR(iArg), 0, xFu
71b60 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 nc, 0, 0, #zName
71b70 2c 20 30 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 , 0, 0}.#define
71b80 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 STR_FUNCTION(zNa
71b90 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 me, nArg, pArg,
71ba0 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 bNC, xFunc) \.
71bb0 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 {nArg, SQLITE_UT
71bc0 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45 5f 46 F8, bNC*SQLITE_F
71bd0 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a UNC_NEEDCOLL, \.
71be0 20 20 20 70 41 72 67 2c 20 30 2c 20 78 46 75 6e pArg, 0, xFun
71bf0 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c c, 0, 0, #zName,
71c00 20 30 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 4c 0, 0}.#define L
71c10 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e IKEFUNC(zName, n
71c20 41 72 67 2c 20 61 72 67 2c 20 66 6c 61 67 73 29 Arg, arg, flags)
71c30 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 \. {nArg, SQLI
71c40 54 45 5f 55 54 46 38 2c 20 66 6c 61 67 73 2c 20 TE_UTF8, flags,
71c50 28 76 6f 69 64 20 2a 29 61 72 67 2c 20 30 2c 20 (void *)arg, 0,
71c60 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20 likeFunc, 0, 0,
71c70 23 7a 4e 61 6d 65 2c 20 30 2c 20 30 7d 0a 23 64 #zName, 0, 0}.#d
71c80 65 66 69 6e 65 20 41 47 47 52 45 47 41 54 45 28 efine AGGREGATE(
71c90 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 zName, nArg, arg
71ca0 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 , nc, xStep, xFi
71cb0 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 nal) \. {nArg,
71cc0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e 63 2a SQLITE_UTF8, nc*
71cd0 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 SQLITE_FUNC_NEED
71ce0 43 4f 4c 4c 2c 20 5c 0a 20 20 20 53 51 4c 49 54 COLL, \. SQLIT
71cf0 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 61 72 67 E_INT_TO_PTR(arg
71d00 29 2c 20 30 2c 20 30 2c 20 78 53 74 65 70 2c 78 ), 0, 0, xStep,x
71d10 46 69 6e 61 6c 2c 23 7a 4e 61 6d 65 2c 30 2c 30 Final,#zName,0,0
71d20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 75 72 }../*.** All cur
71d30 72 65 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 20 rent savepoints
71d40 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 are stored in a
71d50 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 74 61 72 linked list star
71d60 74 69 6e 67 20 61 74 0a 2a 2a 20 73 71 6c 69 74 ting at.** sqlit
71d70 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e 20 54 e3.pSavepoint. T
71d80 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 he first element
71d90 20 69 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 in the list is
71da0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c the most recentl
71db0 79 0a 2a 2a 20 6f 70 65 6e 65 64 20 73 61 76 65 y.** opened save
71dc0 70 6f 69 6e 74 2e 20 53 61 76 65 70 6f 69 6e 74 point. Savepoint
71dd0 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 s are added to t
71de0 68 65 20 6c 69 73 74 20 62 79 20 74 68 65 20 76 he list by the v
71df0 64 62 65 0a 2a 2a 20 4f 50 5f 53 61 76 65 70 6f dbe.** OP_Savepo
71e00 69 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e int instruction.
71e10 0a 2a 2f 0a 73 74 72 75 63 74 20 53 61 76 65 70 .*/.struct Savep
71e20 6f 69 6e 74 20 7b 0a 20 20 63 68 61 72 20 2a 7a oint {. char *z
71e30 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 Name;
71e40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
71e50 53 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 20 28 Savepoint name (
71e60 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 29 20 nul-terminated)
71e70 2a 2f 0a 20 20 69 36 34 20 6e 44 65 66 65 72 72 */. i64 nDeferr
71e80 65 64 43 6f 6e 73 3b 20 20 20 20 20 20 20 20 20 edCons;
71e90 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
71ea0 65 72 20 6f 66 20 64 65 66 65 72 72 65 64 20 66 er of deferred f
71eb0 6b 20 76 69 6f 6c 61 74 69 6f 6e 73 20 2a 2f 0a k violations */.
71ec0 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 Savepoint *pNe
71ed0 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 xt;
71ee0 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 /* Parent
71ef0 73 61 76 65 70 6f 69 6e 74 20 28 69 66 20 61 6e savepoint (if an
71f00 79 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 y) */.};../*.**
71f10 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 The following ar
71f20 65 20 75 73 65 64 20 61 73 20 74 68 65 20 73 65 e used as the se
71f30 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 cond parameter t
71f40 6f 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 o sqlite3Savepoi
71f50 6e 74 28 29 2c 0a 2a 2a 20 61 6e 64 20 61 73 20 nt(),.** and as
71f60 74 68 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 the P1 argument
71f70 74 6f 20 74 68 65 20 4f 50 5f 53 61 76 65 70 6f to the OP_Savepo
71f80 69 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e int instruction.
71f90 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 41 56 45 .*/.#define SAVE
71fa0 50 4f 49 4e 54 5f 42 45 47 49 4e 20 20 20 20 20 POINT_BEGIN
71fb0 20 30 0a 23 64 65 66 69 6e 65 20 53 41 56 45 50 0.#define SAVEP
71fc0 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 20 20 20 OINT_RELEASE
71fd0 31 0a 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f 1.#define SAVEPO
71fe0 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 32 INT_ROLLBACK 2
71ff0 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 .../*.** Each SQ
72000 4c 69 74 65 20 6d 6f 64 75 6c 65 20 28 76 69 72 Lite module (vir
72010 74 75 61 6c 20 74 61 62 6c 65 20 64 65 66 69 6e tual table defin
72020 69 74 69 6f 6e 29 20 69 73 20 64 65 66 69 6e 65 ition) is define
72030 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 d by an.** insta
72040 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
72050 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2c 20 wing structure,
72060 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 stored in the sq
72070 6c 69 74 65 33 2e 61 4d 6f 64 75 6c 65 0a 2a 2a lite3.aModule.**
72080 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a hash table..*/.
72090 73 74 72 75 63 74 20 4d 6f 64 75 6c 65 20 7b 0a struct Module {.
720a0 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f const sqlite3_
720b0 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b module *pModule;
720c0 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 /* Callba
720d0 63 6b 20 70 6f 69 6e 74 65 72 73 20 2a 2f 0a 20 ck pointers */.
720e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
720f0 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 me;
72100 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 70 61 /* Name pa
72110 73 73 65 64 20 74 6f 20 63 72 65 61 74 65 5f 6d ssed to create_m
72120 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 76 6f 69 odule() */. voi
72130 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20 d *pAux;
72140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72150 20 20 2f 2a 20 70 41 75 78 20 70 61 73 73 65 64 /* pAux passed
72160 20 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c to create_modul
72170 65 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a e() */. void (*
72180 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a xDestroy)(void *
72190 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ); /*
721a0 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 Module destruct
721b0 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 7d or function */.}
721c0 3b 0a 0a 2f 2a 0a 2a 2a 20 69 6e 66 6f 72 6d 61 ;../*.** informa
721d0 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 tion about each
721e0 63 6f 6c 75 6d 6e 20 6f 66 20 61 6e 20 53 51 4c column of an SQL
721f0 20 74 61 62 6c 65 20 69 73 20 68 65 6c 64 20 69 table is held i
72200 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a n an instance.**
72210 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
72220 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f re..*/.struct Co
72230 6c 75 6d 6e 20 7b 0a 20 20 63 68 61 72 20 2a 7a lumn {. char *z
72240 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d Name; /* Nam
72250 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e e of this column
72260 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 66 6c */. Expr *pDfl
72270 74 3b 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c t; /* Defaul
72280 74 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 t value of this
72290 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 column */. char
722a0 20 2a 7a 44 66 6c 74 3b 20 20 20 20 20 2f 2a 20 *zDflt; /*
722b0 4f 72 69 67 69 6e 61 6c 20 74 65 78 74 20 6f 66 Original text of
722c0 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c the default val
722d0 75 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 ue */. char *zT
722e0 79 70 65 3b 20 20 20 20 20 2f 2a 20 44 61 74 61 ype; /* Data
722f0 20 74 79 70 65 20 66 6f 72 20 74 68 69 73 20 63 type for this c
72300 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 olumn */. char
72310 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 *zColl; /* C
72320 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
72330 65 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 75 73 65 e. If NULL, use
72340 20 74 68 65 20 64 65 66 61 75 6c 74 20 2a 2f 0a the default */.
72350 20 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 u8 notNull;
72360 20 20 20 2f 2a 20 41 6e 20 4f 45 5f 20 63 6f 64 /* An OE_ cod
72370 65 20 66 6f 72 20 68 61 6e 64 6c 69 6e 67 20 61 e for handling a
72380 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 NOT NULL constr
72390 61 69 6e 74 20 2a 2f 0a 20 20 63 68 61 72 20 61 aint */. char a
723a0 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 4f 6e ffinity; /* On
723b0 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f e of the SQLITE_
723c0 41 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 2a AFF_... values *
723d0 2f 0a 20 20 75 31 36 20 63 6f 6c 46 6c 61 67 73 /. u16 colFlags
723e0 3b 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 20 ; /* Boolean
723f0 70 72 6f 70 65 72 74 69 65 73 2e 20 20 53 65 65 properties. See
72400 20 43 4f 4c 46 4c 41 47 5f 20 64 65 66 69 6e 65 COLFLAG_ define
72410 73 20 62 65 6c 6f 77 20 2a 2f 0a 7d 3b 0a 0a 2f s below */.};../
72420 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 * Allowed values
72430 20 66 6f 72 20 43 6f 6c 75 6d 6e 2e 63 6f 6c 46 for Column.colF
72440 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 lags:.*/.#define
72450 20 43 4f 4c 46 4c 41 47 5f 50 52 49 4d 4b 45 59 COLFLAG_PRIMKEY
72460 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20 43 0x0001 /* C
72470 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 olumn is part of
72480 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 the primary key
72490 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4f 4c 46 */.#define COLF
724a0 4c 41 47 5f 48 49 44 44 45 4e 20 20 20 30 78 30 LAG_HIDDEN 0x0
724b0 30 30 32 20 20 20 20 2f 2a 20 41 20 68 69 64 64 002 /* A hidd
724c0 65 6e 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 en column in a v
724d0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a irtual table */.
724e0 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c 61 74 ./*.** A "Collat
724f0 69 6e 67 20 53 65 71 75 65 6e 63 65 22 20 69 73 ing Sequence" is
72500 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69 defined by an i
72510 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
72520 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 ollowing.** stru
72530 63 74 75 72 65 2e 20 43 6f 6e 63 65 70 74 75 61 cture. Conceptua
72540 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69 6e 67 lly, a collating
72550 20 73 65 71 75 65 6e 63 65 20 63 6f 6e 73 69 73 sequence consis
72560 74 73 20 6f 66 20 61 20 6e 61 6d 65 20 61 6e 64 ts of a name and
72570 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e .** a comparison
72580 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 65 routine that de
72590 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65 72 20 fines the order
725a0 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e 63 65 of that sequence
725b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61 ..**.** There ma
725c0 79 20 74 77 6f 20 73 65 70 61 72 61 74 65 20 69 y two separate i
725d0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
725e0 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 f the collation
725f0 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a 2a 2a function, one.**
72600 20 74 68 61 74 20 70 72 6f 63 65 73 73 65 73 20 that processes
72610 74 65 78 74 20 69 6e 20 55 54 46 2d 38 20 65 6e text in UTF-8 en
72620 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65 71 2e coding (CollSeq.
72630 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74 68 65 xCmp) and anothe
72640 72 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63 65 73 r that.** proces
72650 73 65 73 20 74 65 78 74 20 65 6e 63 6f 64 65 64 ses text encoded
72660 20 69 6e 20 55 54 46 2d 31 36 20 28 43 6f 6c 6c in UTF-16 (Coll
72670 53 65 71 2e 78 43 6d 70 31 36 29 2c 20 75 73 69 Seq.xCmp16), usi
72680 6e 67 20 74 68 65 20 6d 61 63 68 69 6e 65 0a 2a ng the machine.*
72690 2a 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 * native byte or
726a0 64 65 72 2e 20 57 68 65 6e 20 61 20 63 6f 6c 6c der. When a coll
726b0 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 ation sequence i
726c0 73 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c 69 74 s invoked, SQLit
726d0 65 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74 68 65 e selects.** the
726e0 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 77 69 version that wi
726f0 6c 6c 20 72 65 71 75 69 72 65 20 74 68 65 20 6c ll require the l
72700 65 61 73 74 20 65 78 70 65 6e 73 69 76 65 20 65 east expensive e
72710 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61 6e 73 ncoding.** trans
72720 6c 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e 79 2e lations, if any.
72730 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c 6c 53 .**.** The CollS
72740 65 71 2e 70 55 73 65 72 20 6d 65 6d 62 65 72 20 eq.pUser member
72750 76 61 72 69 61 62 6c 65 20 69 73 20 61 6e 20 65 variable is an e
72760 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 74 xtra parameter t
72770 68 61 74 20 70 61 73 73 65 64 20 69 6e 0a 2a 2a hat passed in.**
72780 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
72790 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 55 54 gument to the UT
727a0 46 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 F-8 comparison f
727b0 75 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e 0a 2a unction, xCmp..*
727c0 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 31 * CollSeq.pUser1
727d0 36 20 69 73 20 74 68 65 20 65 71 75 69 76 61 6c 6 is the equival
727e0 65 6e 74 20 66 6f 72 20 74 68 65 20 55 54 46 2d ent for the UTF-
727f0 31 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 16 comparison fu
72800 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d 70 31 nction,.** xCmp1
72810 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 74 68 6..**.** If both
72820 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20 61 6e CollSeq.xCmp an
72830 64 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 d CollSeq.xCmp16
72840 20 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 are NULL, it me
72850 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 ans that the.**
72860 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
72870 63 65 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e ce is undefined.
72880 20 20 49 6e 64 69 63 65 73 20 62 75 69 6c 74 20 Indices built
72890 6f 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a on an undefined.
728a0 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 ** collating seq
728b0 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20 62 65 uence may not be
728c0 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e read or written
728d0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 6c ..*/.struct Coll
728e0 53 65 71 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e Seq {. char *zN
728f0 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ame; /*
72900 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c Name of the col
72910 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2c lating sequence,
72920 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a UTF-8 encoded *
72930 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20 /. u8 enc;
72940 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 /* Tex
72950 74 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e 64 6c t encoding handl
72960 65 64 20 62 79 20 78 43 6d 70 28 29 20 2a 2f 0a ed by xCmp() */.
72970 20 20 76 6f 69 64 20 2a 70 55 73 65 72 3b 20 20 void *pUser;
72980 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
72990 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6d argument to xCm
729a0 70 28 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 p() */. int (*x
729b0 43 6d 70 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 Cmp)(void*,int,
729c0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 const void*, int
729d0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a , const void*);.
729e0 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
729f0 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 oid*); /* Destr
72a00 75 63 74 6f 72 20 66 6f 72 20 70 55 73 65 72 20 uctor for pUser
72a10 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 */.};../*.** A s
72a20 6f 72 74 20 6f 72 64 65 72 20 63 61 6e 20 62 65 ort order can be
72a30 20 65 69 74 68 65 72 20 41 53 43 20 6f 72 20 44 either ASC or D
72a40 45 53 43 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ESC..*/.#define
72a50 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 20 20 SQLITE_SO_ASC
72a60 20 20 20 20 30 20 20 2f 2a 20 53 6f 72 74 20 69 0 /* Sort i
72a70 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 n ascending orde
72a80 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c r */.#define SQL
72a90 49 54 45 5f 53 4f 5f 44 45 53 43 20 20 20 20 20 ITE_SO_DESC
72aa0 20 31 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 1 /* Sort in a
72ab0 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a scending order *
72ac0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 /../*.** Column
72ad0 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 2e 0a affinity types..
72ae0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 75 73 65 64 **.** These used
72af0 20 74 6f 20 68 61 76 65 20 6d 6e 65 6d 6f 6e 69 to have mnemoni
72b00 63 20 6e 61 6d 65 20 6c 69 6b 65 20 27 69 27 20 c name like 'i'
72b10 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 for SQLITE_AFF_I
72b20 4e 54 45 47 45 52 20 61 6e 64 0a 2a 2a 20 27 74 NTEGER and.** 't
72b30 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 ' for SQLITE_AFF
72b40 5f 54 45 58 54 2e 20 20 42 75 74 20 77 65 20 63 _TEXT. But we c
72b50 61 6e 20 73 61 76 65 20 61 20 6c 69 74 74 6c 65 an save a little
72b60 20 73 70 61 63 65 20 61 6e 64 20 69 6d 70 72 6f space and impro
72b70 76 65 0a 2a 2a 20 74 68 65 20 73 70 65 65 64 20 ve.** the speed
72b80 61 20 6c 69 74 74 6c 65 20 62 79 20 6e 75 6d 62 a little by numb
72b90 65 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65 73 ering the values
72ba0 20 63 6f 6e 73 65 63 75 74 69 76 65 6c 79 2e 20 consecutively.
72bb0 20 0a 2a 2a 0a 2a 2a 20 42 75 74 20 72 61 74 68 .**.** But rath
72bc0 65 72 20 74 68 61 6e 20 73 74 61 72 74 20 77 69 er than start wi
72bd0 74 68 20 30 20 6f 72 20 31 2c 20 77 65 20 62 65 th 0 or 1, we be
72be0 67 69 6e 20 77 69 74 68 20 27 61 27 2e 20 20 54 gin with 'a'. T
72bf0 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 68 65 6e hat way,.** when
72c00 20 6d 75 6c 74 69 70 6c 65 20 61 66 66 69 6e 69 multiple affini
72c10 74 79 20 74 79 70 65 73 20 61 72 65 20 63 6f 6e ty types are con
72c20 63 61 74 65 6e 61 74 65 64 20 69 6e 74 6f 20 61 catenated into a
72c30 20 73 74 72 69 6e 67 20 61 6e 64 0a 2a 2a 20 75 string and.** u
72c40 73 65 64 20 61 73 20 74 68 65 20 50 34 20 6f 70 sed as the P4 op
72c50 65 72 61 6e 64 2c 20 74 68 65 79 20 77 69 6c 6c erand, they will
72c60 20 62 65 20 6d 6f 72 65 20 72 65 61 64 61 62 6c be more readabl
72c70 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 61 6c e..**.** Note al
72c80 73 6f 20 74 68 61 74 20 74 68 65 20 6e 75 6d 65 so that the nume
72c90 72 69 63 20 74 79 70 65 73 20 61 72 65 20 67 72 ric types are gr
72ca0 6f 75 70 65 64 20 74 6f 67 65 74 68 65 72 20 73 ouped together s
72cb0 6f 20 74 68 61 74 20 74 65 73 74 69 6e 67 0a 2a o that testing.*
72cc0 2a 20 66 6f 72 20 61 20 6e 75 6d 65 72 69 63 20 * for a numeric
72cd0 74 79 70 65 20 69 73 20 61 20 73 69 6e 67 6c 65 type is a single
72ce0 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a comparison..*/.
72cf0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
72d00 46 46 5f 54 45 58 54 20 20 20 20 20 27 61 27 0a FF_TEXT 'a'.
72d10 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
72d20 46 46 5f 4e 4f 4e 45 20 20 20 20 20 27 62 27 0a FF_NONE 'b'.
72d30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
72d40 46 46 5f 4e 55 4d 45 52 49 43 20 20 27 63 27 0a FF_NUMERIC 'c'.
72d50 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
72d60 46 46 5f 49 4e 54 45 47 45 52 20 20 27 64 27 0a FF_INTEGER 'd'.
72d70 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
72d80 46 46 5f 52 45 41 4c 20 20 20 20 20 27 65 27 0a FF_REAL 'e'.
72d90 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
72da0 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 IsNumericAffinit
72db0 79 28 58 29 20 20 28 28 58 29 3e 3d 53 51 4c 49 y(X) ((X)>=SQLI
72dc0 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 29 0a TE_AFF_NUMERIC).
72dd0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 ./*.** The SQLIT
72de0 45 5f 41 46 46 5f 4d 41 53 4b 20 76 61 6c 75 65 E_AFF_MASK value
72df0 73 20 6d 61 73 6b 73 20 6f 66 66 20 74 68 65 20 s masks off the
72e00 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74 73 significant bits
72e10 20 6f 66 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 of an.** affini
72e20 74 79 20 76 61 6c 75 65 2e 20 0a 2a 2f 0a 23 64 ty value. .*/.#d
72e30 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 efine SQLITE_AFF
72e40 5f 4d 41 53 4b 20 20 20 20 20 30 78 36 37 0a 0a _MASK 0x67..
72e50 2f 2a 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c /*.** Additional
72e60 20 62 69 74 20 76 61 6c 75 65 73 20 74 68 61 74 bit values that
72e70 20 63 61 6e 20 62 65 20 4f 52 65 64 20 77 69 74 can be ORed wit
72e80 68 20 61 6e 20 61 66 66 69 6e 69 74 79 20 77 69 h an affinity wi
72e90 74 68 6f 75 74 0a 2a 2a 20 63 68 61 6e 67 69 6e thout.** changin
72ea0 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 2e 0a g the affinity..
72eb0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
72ec0 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 20 20 30 E_JUMPIFNULL 0
72ed0 78 30 38 20 20 2f 2a 20 6a 75 6d 70 73 20 69 66 x08 /* jumps if
72ee0 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 either operand
72ef0 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 is NULL */.#defi
72f00 6e 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 ne SQLITE_STOREP
72f10 32 20 20 20 20 20 20 30 78 31 30 20 20 2f 2a 20 2 0x10 /*
72f20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 Store result in
72f30 72 65 67 5b 50 32 5d 20 72 61 74 68 65 72 20 74 reg[P2] rather t
72f40 68 61 6e 20 6a 75 6d 70 20 2a 2f 0a 23 64 65 66 han jump */.#def
72f50 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 ine SQLITE_NULLE
72f60 51 20 20 20 20 20 20 20 30 78 38 30 20 20 2f 2a Q 0x80 /*
72f70 20 4e 55 4c 4c 3d 4e 55 4c 4c 20 2a 2f 0a 0a 2f NULL=NULL */../
72f80 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 20 6f *.** An object o
72f90 66 20 74 68 69 73 20 74 79 70 65 20 69 73 20 63 f this type is c
72fa0 72 65 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 reated for each
72fb0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70 72 virtual table pr
72fc0 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74 68 65 20 esent in.** the
72fd0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e database schema.
72fe0 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 .**.** If the d
72ff0 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 atabase schema i
73000 73 20 73 68 61 72 65 64 2c 20 74 68 65 6e 20 74 s shared, then t
73010 68 65 72 65 20 69 73 20 6f 6e 65 20 69 6e 73 74 here is one inst
73020 61 6e 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 ance of this.**
73030 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 65 61 structure for ea
73040 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ch database conn
73050 65 63 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 2a ection (sqlite3*
73060 29 20 74 68 61 74 20 75 73 65 73 20 74 68 65 20 ) that uses the
73070 73 68 61 72 65 64 0a 2a 2a 20 73 63 68 65 6d 61 shared.** schema
73080 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 . This is becaus
73090 65 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 e each database
730a0 63 6f 6e 6e 65 63 74 69 6f 6e 20 72 65 71 75 69 connection requi
730b0 72 65 73 20 69 74 73 20 6f 77 6e 20 75 6e 69 71 res its own uniq
730c0 75 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f ue.** instance o
730d0 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 f the sqlite3_vt
730e0 61 62 2a 20 68 61 6e 64 6c 65 20 75 73 65 64 20 ab* handle used
730f0 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 76 69 to access the vi
73100 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 rtual table .**
73110 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 implementation.
73120 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 20 68 61 sqlite3_vtab* ha
73130 6e 64 6c 65 73 20 63 61 6e 20 6e 6f 74 20 62 65 ndles can not be
73140 20 73 68 61 72 65 64 20 62 65 74 77 65 65 6e 20 shared between
73150 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e .** database con
73160 6e 65 63 74 69 6f 6e 73 2c 20 65 76 65 6e 20 77 nections, even w
73170 68 65 6e 20 74 68 65 20 72 65 73 74 20 6f 66 20 hen the rest of
73180 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 the in-memory da
73190 74 61 62 61 73 65 20 0a 2a 2a 20 73 63 68 65 6d tabase .** schem
731a0 61 20 69 73 20 73 68 61 72 65 64 2c 20 61 73 20 a is shared, as
731b0 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
731c0 6f 6e 20 6f 66 74 65 6e 20 73 74 6f 72 65 73 20 on often stores
731d0 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
731e0 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c connection handl
731f0 65 20 70 61 73 73 65 64 20 74 6f 20 69 74 20 76 e passed to it v
73200 69 61 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 28 ia the xConnect(
73210 29 20 6f 72 20 78 43 72 65 61 74 65 28 29 20 6d ) or xCreate() m
73220 65 74 68 6f 64 0a 2a 2a 20 64 75 72 69 6e 67 20 ethod.** during
73230 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 initialization i
73240 6e 74 65 72 6e 61 6c 6c 79 2e 20 54 68 69 73 20 nternally. This
73250 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
73260 69 6f 6e 20 68 61 6e 64 6c 65 20 6d 61 79 0a 2a ion handle may.*
73270 2a 20 74 68 65 6e 20 62 65 20 75 73 65 64 20 62 * then be used b
73280 79 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 y the virtual ta
73290 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ble implementati
732a0 6f 6e 20 74 6f 20 61 63 63 65 73 73 20 72 65 61 on to access rea
732b0 6c 20 74 61 62 6c 65 73 20 0a 2a 2a 20 77 69 74 l tables .** wit
732c0 68 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hin the database
732d0 2e 20 53 6f 20 74 68 61 74 20 74 68 65 79 20 61 . So that they a
732e0 70 70 65 61 72 20 61 73 20 70 61 72 74 20 6f 66 ppear as part of
732f0 20 74 68 65 20 63 61 6c 6c 65 72 73 20 0a 2a 2a the callers .**
73300 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 transaction, th
73310 65 73 65 20 61 63 63 65 73 73 65 73 20 6e 65 65 ese accesses nee
73320 64 20 74 6f 20 62 65 20 6d 61 64 65 20 76 69 61 d to be made via
73330 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
73340 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f se .** connectio
73350 6e 20 61 73 20 74 68 61 74 20 75 73 65 64 20 74 n as that used t
73360 6f 20 65 78 65 63 75 74 65 20 53 51 4c 20 6f 70 o execute SQL op
73370 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 erations on the
73380 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a virtual table..*
73390 2a 0a 2a 2a 20 41 6c 6c 20 56 54 61 62 6c 65 20 *.** All VTable
733a0 6f 62 6a 65 63 74 73 20 74 68 61 74 20 63 6f 72 objects that cor
733b0 72 65 73 70 6f 6e 64 20 74 6f 20 61 20 73 69 6e respond to a sin
733c0 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 61 20 73 gle table in a s
733d0 68 61 72 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 hared.** databas
733e0 65 20 73 63 68 65 6d 61 20 61 72 65 20 69 6e 69 e schema are ini
733f0 74 69 61 6c 6c 79 20 73 74 6f 72 65 64 20 69 6e tially stored in
73400 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 70 a linked-list p
73410 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20 ointed to by.**
73420 74 68 65 20 54 61 62 6c 65 2e 70 56 54 61 62 6c the Table.pVTabl
73430 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c e member variabl
73440 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 e of the corresp
73450 6f 6e 64 69 6e 67 20 54 61 62 6c 65 20 6f 62 6a onding Table obj
73460 65 63 74 2e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 ect..** When an
73470 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
73480 29 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72 ) operation is r
73490 65 71 75 69 72 65 64 20 74 6f 20 61 63 63 65 73 equired to acces
734a0 73 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a s the virtual.**
734b0 20 74 61 62 6c 65 2c 20 69 74 20 73 65 61 72 63 table, it searc
734c0 68 65 73 20 74 68 65 20 6c 69 73 74 20 66 6f 72 hes the list for
734d0 20 74 68 65 20 56 54 61 62 6c 65 20 74 68 61 74 the VTable that
734e0 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 corresponds to
734f0 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
73500 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 6f 69 6e 67 connection doing
73510 20 74 68 65 20 70 72 65 70 61 72 69 6e 67 20 73 the preparing s
73520 6f 20 61 73 20 74 6f 20 75 73 65 20 74 68 65 20 o as to use the
73530 63 6f 72 72 65 63 74 0a 2a 2a 20 73 71 6c 69 74 correct.** sqlit
73540 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 20 e3_vtab* handle
73550 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 in the compiled
73560 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 query..**.** Whe
73570 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 54 n an in-memory T
73580 61 62 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 64 able object is d
73590 65 6c 65 74 65 64 20 28 66 6f 72 20 65 78 61 6d eleted (for exam
735a0 70 6c 65 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 ple when the.**
735b0 73 63 68 65 6d 61 20 69 73 20 62 65 69 6e 67 20 schema is being
735c0 72 65 6c 6f 61 64 65 64 20 66 6f 72 20 73 6f 6d reloaded for som
735d0 65 20 72 65 61 73 6f 6e 29 2c 20 74 68 65 20 56 e reason), the V
735e0 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 72 Table objects ar
735f0 65 20 6e 6f 74 20 0a 2a 2a 20 64 65 6c 65 74 65 e not .** delete
73600 64 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 d and the sqlite
73610 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 73 20 3_vtab* handles
73620 61 72 65 20 6e 6f 74 20 78 44 69 73 63 6f 6e 6e are not xDisconn
73630 65 63 74 28 29 65 64 20 0a 2a 2a 20 69 6d 6d 65 ect()ed .** imme
73640 64 69 61 74 65 6c 79 2e 20 49 6e 73 74 65 61 64 diately. Instead
73650 2c 20 74 68 65 79 20 61 72 65 20 6d 6f 76 65 64 , they are moved
73660 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 2e from the Table.
73670 70 56 54 61 62 6c 65 20 6c 69 73 74 20 74 6f 0a pVTable list to.
73680 2a 2a 20 61 6e 6f 74 68 65 72 20 6c 69 6e 6b 65 ** another linke
73690 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 79 d list headed by
736a0 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69 the sqlite3.pDi
736b0 73 63 6f 6e 6e 65 63 74 20 6d 65 6d 62 65 72 20 sconnect member
736c0 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 of the.** corres
736d0 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 20 ponding sqlite3
736e0 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 79 20 structure. They
736f0 61 72 65 20 74 68 65 6e 20 64 65 6c 65 74 65 64 are then deleted
73700 2f 78 44 69 73 63 6f 6e 6e 65 63 74 65 64 20 0a /xDisconnected .
73710 2a 2a 20 6e 65 78 74 20 74 69 6d 65 20 61 20 73 ** next time a s
73720 74 61 74 65 6d 65 6e 74 20 69 73 20 70 72 65 70 tatement is prep
73730 61 72 65 64 20 75 73 69 6e 67 20 73 61 69 64 20 ared using said
73740 73 71 6c 69 74 65 33 2a 2e 20 54 68 69 73 20 69 sqlite3*. This i
73750 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 61 76 6f s done.** to avo
73760 69 64 20 64 65 61 64 6c 6f 63 6b 20 69 73 73 75 id deadlock issu
73770 65 73 20 69 6e 76 6f 6c 76 69 6e 67 20 6d 75 6c es involving mul
73780 74 69 70 6c 65 20 73 71 6c 69 74 65 33 2e 6d 75 tiple sqlite3.mu
73790 74 65 78 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 20 tex mutexes..**
737a0 52 65 66 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 Refer to comment
737b0 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e s above function
737c0 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f sqlite3VtabUnlo
737d0 63 6b 4c 69 73 74 28 29 20 66 6f 72 20 61 6e 0a ckList() for an.
737e0 2a 2a 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 61 ** explanation a
737f0 73 20 74 6f 20 77 68 79 20 69 74 20 69 73 20 73 s to why it is s
73800 61 66 65 20 74 6f 20 61 64 64 20 61 6e 20 65 6e afe to add an en
73810 74 72 79 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 try to an sqlite
73820 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 0a 2a 2a 3.pDisconnect.**
73830 20 6c 69 73 74 20 77 69 74 68 6f 75 74 20 68 6f list without ho
73840 6c 64 69 6e 67 20 74 68 65 20 63 6f 72 72 65 73 lding the corres
73850 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 2e ponding sqlite3.
73860 6d 75 74 65 78 20 6d 75 74 65 78 2e 0a 2a 2a 0a mutex mutex..**.
73870 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 66 6f ** The memory fo
73880 72 20 6f 62 6a 65 63 74 73 20 6f 66 20 74 68 69 r objects of thi
73890 73 20 74 79 70 65 20 69 73 20 61 6c 77 61 79 73 s type is always
738a0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a allocated by .*
738b0 2a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f * sqlite3DbMallo
738c0 63 28 29 2c 20 75 73 69 6e 67 20 74 68 65 20 63 c(), using the c
738d0 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 onnection handle
738e0 20 73 74 6f 72 65 64 20 69 6e 20 56 54 61 62 6c stored in VTabl
738f0 65 2e 64 62 20 61 73 20 0a 2a 2a 20 74 68 65 20 e.db as .** the
73900 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a first argument..
73910 2a 2f 0a 73 74 72 75 63 74 20 56 54 61 62 6c 65 */.struct VTable
73920 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 {. sqlite3 *db
73930 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
73940 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * Database conne
73950 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 ction associated
73960 20 77 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 with this table
73970 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d */. Module *pM
73980 6f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 od;
73990 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d 6f /* Pointer to mo
739a0 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 dule implementat
739b0 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ion */. sqlite3
739c0 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 _vtab *pVtab;
739d0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
739e0 20 76 74 61 62 20 69 6e 73 74 61 6e 63 65 20 2a vtab instance *
739f0 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 /. int nRef;
73a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
73a10 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 Number of point
73a20 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 ers to this stru
73a30 63 74 75 72 65 20 2a 2f 0a 20 20 75 38 20 62 43 cture */. u8 bC
73a40 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 onstraint;
73a50 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
73a60 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 constraints are
73a70 73 75 70 70 6f 72 74 65 64 20 2a 2f 0a 20 20 69 supported */. i
73a80 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 3b 20 20 nt iSavepoint;
73a90 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 /* Dept
73aa0 68 20 6f 66 20 74 68 65 20 53 41 56 45 50 4f 49 h of the SAVEPOI
73ab0 4e 54 20 73 74 61 63 6b 20 2a 2f 0a 20 20 56 54 NT stack */. VT
73ac0 61 62 6c 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 able *pNext;
73ad0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
73ae0 69 6e 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 in linked list (
73af0 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 7d 3b see above) */.};
73b00 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c ../*.** Each SQL
73b10 20 74 61 62 6c 65 20 69 73 20 72 65 70 72 65 73 table is repres
73b20 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 ented in memory
73b30 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f by an instance o
73b40 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 f the.** followi
73b50 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ng structure..**
73b60 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d 65 20 .** Table.zName
73b70 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 is the name of t
73b80 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 63 he table. The c
73b90 61 73 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 ase of the origi
73ba0 6e 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 nal.** CREATE TA
73bb0 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 BLE statement is
73bc0 20 73 74 6f 72 65 64 2c 20 62 75 74 20 63 61 73 stored, but cas
73bd0 65 20 69 73 20 6e 6f 74 20 73 69 67 6e 69 66 69 e is not signifi
73be0 63 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 cant for.** comp
73bf0 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 arisons..**.** T
73c00 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 68 65 able.nCol is the
73c10 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
73c20 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 ns in this table
73c30 2e 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20 69 73 . Table.aCol is
73c40 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f a.** pointer to
73c50 20 61 6e 20 61 72 72 61 79 20 6f 66 20 43 6f 6c an array of Col
73c60 75 6d 6e 20 73 74 72 75 63 74 75 72 65 73 2c 20 umn structures,
73c70 6f 6e 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c one for each col
73c80 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 umn..**.** If th
73c90 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49 e table has an I
73ca0 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
73cb0 45 59 2c 20 74 68 65 6e 20 54 61 62 6c 65 2e 69 EY, then Table.i
73cc0 50 4b 65 79 20 69 73 20 74 68 65 20 69 6e 64 65 PKey is the inde
73cd0 78 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 x of.** the colu
73ce0 6d 6e 20 74 68 61 74 20 69 73 20 74 68 61 74 20 mn that is that
73cf0 6b 65 79 2e 20 20 20 4f 74 68 65 72 77 69 73 65 key. Otherwise
73d00 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 Table.iPKey is
73d10 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 0a negative. Note.
73d20 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61 74 61 ** that the data
73d30 74 79 70 65 20 6f 66 20 74 68 65 20 50 52 49 4d type of the PRIM
73d40 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 ARY KEY must be
73d50 49 4e 54 45 47 45 52 20 66 6f 72 20 74 68 69 73 INTEGER for this
73d60 20 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 65 20 field to.** be
73d70 73 65 74 2e 20 20 41 6e 20 49 4e 54 45 47 45 52 set. An INTEGER
73d80 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20 PRIMARY KEY is
73d90 75 73 65 64 20 61 73 20 74 68 65 20 72 6f 77 69 used as the rowi
73da0 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f d for each row o
73db0 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20 f.** the table.
73dc0 20 49 66 20 61 20 74 61 62 6c 65 20 68 61 73 20 If a table has
73dd0 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 no INTEGER PRIMA
73de0 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 61 20 72 RY KEY, then a r
73df0 61 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a 20 69 andom rowid.** i
73e00 73 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 s generated for
73e10 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 20 each row of the
73e20 74 61 62 6c 65 2e 20 20 54 46 5f 48 61 73 50 72 table. TF_HasPr
73e30 69 6d 61 72 79 4b 65 79 20 69 73 20 73 65 74 20 imaryKey is set
73e40 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 if.** the table
73e50 68 61 73 20 61 6e 79 20 50 52 49 4d 41 52 59 20 has any PRIMARY
73e60 4b 45 59 2c 20 49 4e 54 45 47 45 52 20 6f 72 20 KEY, INTEGER or
73e70 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a otherwise..**.**
73e80 20 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73 20 74 Table.tnum is t
73e90 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 he page number f
73ea0 6f 72 20 74 68 65 20 72 6f 6f 74 20 42 54 72 65 or the root BTre
73eb0 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 e page of the ta
73ec0 62 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20 64 61 ble in the.** da
73ed0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 tabase file. If
73ee0 20 54 61 62 6c 65 2e 69 44 62 20 69 73 20 74 68 Table.iDb is th
73ef0 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 e index of the d
73f00 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 62 61 atabase table ba
73f10 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 6c 69 ckend.** in sqli
73f20 74 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69 73 20 te.aDb[]. 0 is
73f30 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 for the main dat
73f40 61 62 61 73 65 20 61 6e 64 20 31 20 69 73 20 66 abase and 1 is f
73f50 6f 72 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 or the file that
73f60 0a 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70 6f 72 .** holds tempor
73f70 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 20 69 ary tables and i
73f80 6e 64 69 63 65 73 2e 20 20 49 66 20 54 46 5f 45 ndices. If TF_E
73f90 70 68 65 6d 65 72 61 6c 20 69 73 20 73 65 74 0a phemeral is set.
73fa0 2a 2a 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c ** then the tabl
73fb0 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 e is stored in a
73fc0 20 66 69 6c 65 20 74 68 61 74 20 69 73 20 61 75 file that is au
73fd0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 tomatically dele
73fe0 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 ted.** when the
73ff0 56 44 42 45 20 63 75 72 73 6f 72 20 74 6f 20 74 VDBE cursor to t
74000 68 65 20 74 61 62 6c 65 20 69 73 20 63 6c 6f 73 he table is clos
74010 65 64 2e 20 20 49 6e 20 74 68 69 73 20 63 61 73 ed. In this cas
74020 65 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0a 2a 2a e Table.tnum .**
74030 20 72 65 66 65 72 73 20 56 44 42 45 20 63 75 72 refers VDBE cur
74040 73 6f 72 20 6e 75 6d 62 65 72 20 74 68 61 74 20 sor number that
74050 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c 65 20 holds the table
74060 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 68 65 open, not to the
74070 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 6e 75 root.** page nu
74080 6d 62 65 72 2e 20 20 54 72 61 6e 73 69 65 6e 74 mber. Transient
74090 20 74 61 62 6c 65 73 20 61 72 65 20 75 73 65 64 tables are used
740a0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 to hold the res
740b0 75 6c 74 73 20 6f 66 20 61 0a 2a 2a 20 73 75 62 ults of a.** sub
740c0 2d 71 75 65 72 79 20 74 68 61 74 20 61 70 70 65 -query that appe
740d0 61 72 73 20 69 6e 73 74 65 61 64 20 6f 66 20 61 ars instead of a
740e0 20 72 65 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 real table name
740f0 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 in the FROM cla
74100 75 73 65 20 0a 2a 2a 20 6f 66 20 61 20 53 45 4c use .** of a SEL
74110 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a ECT statement..*
74120 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 20 7b /.struct Table {
74130 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 . char *zName;
74140 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
74150 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 of the table or
74160 76 69 65 77 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e view */. Column
74170 20 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f *aCol; /
74180 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 * Information ab
74190 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 out each column
741a0 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 */. Index *pInd
741b0 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 ex; /* Lis
741c0 74 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65 73 t of SQL indexes
741d0 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 on this table.
741e0 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 */. Select *pSe
741f0 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 4e 55 4c lect; /* NUL
74200 4c 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 50 L for tables. P
74210 6f 69 6e 74 73 20 74 6f 20 64 65 66 69 6e 69 74 oints to definit
74220 69 6f 6e 20 69 66 20 61 20 76 69 65 77 2e 20 2a ion if a view. *
74230 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b /. FKey *pFKey;
74240 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b /* Link
74250 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 66 ed list of all f
74260 6f 72 65 69 67 6e 20 6b 65 79 73 20 69 6e 20 74 oreign keys in t
74270 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 his table */. c
74280 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 har *zColAff;
74290 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 /* String de
742a0 66 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69 6e fining the affin
742b0 69 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 ity of each colu
742c0 6d 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 mn */.#ifndef SQ
742d0 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a LITE_OMIT_CHECK.
742e0 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 65 ExprList *pChe
742f0 63 6b 3b 20 20 20 20 2f 2a 20 41 6c 6c 20 43 48 ck; /* All CH
74300 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 ECK constraints
74310 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 74 52 6f 77 */.#endif. tRow
74320 63 6e 74 20 6e 52 6f 77 45 73 74 3b 20 20 20 20 cnt nRowEst;
74330 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 72 6f /* Estimated ro
74340 77 73 20 69 6e 20 74 61 62 6c 65 20 2d 20 66 72 ws in table - fr
74350 6f 6d 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 om sqlite_stat1
74360 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 74 table */. int t
74370 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 num;
74380 2f 2a 20 52 6f 6f 74 20 42 54 72 65 65 20 6e 6f /* Root BTree no
74390 64 65 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c de for this tabl
743a0 65 20 28 73 65 65 20 6e 6f 74 65 20 61 62 6f 76 e (see note abov
743b0 65 29 20 2a 2f 0a 20 20 69 31 36 20 69 50 4b 65 e) */. i16 iPKe
743c0 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 y; /*
743d0 49 66 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c If not negative,
743e0 20 75 73 65 20 61 43 6f 6c 5b 69 50 4b 65 79 5d use aCol[iPKey]
743f0 20 61 73 20 74 68 65 20 70 72 69 6d 61 72 79 20 as the primary
74400 6b 65 79 20 2a 2f 0a 20 20 69 31 36 20 6e 43 6f key */. i16 nCo
74410 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a l; /*
74420 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d Number of colum
74430 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 ns in this table
74440 20 2a 2f 0a 20 20 75 31 36 20 6e 52 65 66 3b 20 */. u16 nRef;
74450 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
74460 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 mber of pointers
74470 20 74 6f 20 74 68 69 73 20 54 61 62 6c 65 20 2a to this Table *
74480 2f 0a 20 20 75 38 20 74 61 62 46 6c 61 67 73 3b /. u8 tabFlags;
74490 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b /* Mask
744a0 20 6f 66 20 54 46 5f 2a 20 76 61 6c 75 65 73 20 of TF_* values
744b0 2a 2f 0a 20 20 75 38 20 6b 65 79 43 6f 6e 66 3b */. u8 keyConf;
744c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 /* Wha
744d0 74 20 74 6f 20 64 6f 20 69 6e 20 63 61 73 65 20 t to do in case
744e0 6f 66 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f of uniqueness co
744f0 6e 66 6c 69 63 74 20 6f 6e 20 69 50 4b 65 79 20 nflict on iPKey
74500 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
74510 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c E_OMIT_ALTERTABL
74520 45 0a 20 20 69 6e 74 20 61 64 64 43 6f 6c 4f 66 E. int addColOf
74530 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 fset; /* Offs
74540 65 74 20 69 6e 20 43 52 45 41 54 45 20 54 41 42 et in CREATE TAB
74550 4c 45 20 73 74 6d 74 20 74 6f 20 61 64 64 20 61 LE stmt to add a
74560 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 new column */.#
74570 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 endif.#ifndef SQ
74580 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
74590 4c 54 41 42 4c 45 0a 20 20 69 6e 74 20 6e 4d 6f LTABLE. int nMo
745a0 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20 2f 2a duleArg; /*
745b0 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d Number of argum
745c0 65 6e 74 73 20 74 6f 20 74 68 65 20 6d 6f 64 75 ents to the modu
745d0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 le */. char **a
745e0 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 20 2f 2a 20 zModuleArg; /*
745f0 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d 6f 64 75 Text of all modu
74600 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20 69 73 20 le args. [0] is
74610 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 module name */.
74620 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65 VTable *pVTable
74630 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 ; /* List of
74640 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 2e VTable objects.
74650 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 54 72 69 */.#endif. Tri
74660 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 gger *pTrigger;
74670 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 /* List of tri
74680 67 67 65 72 73 20 73 74 6f 72 65 64 20 69 6e 20 ggers stored in
74690 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 53 63 68 pSchema */. Sch
746a0 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 ema *pSchema;
746b0 20 20 2f 2a 20 53 63 68 65 6d 61 20 74 68 61 74 /* Schema that
746c0 20 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 74 contains this t
746d0 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 able */. Table
746e0 2a 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 20 20 2f *pNextZombie; /
746f0 2a 20 4e 65 78 74 20 6f 6e 20 74 68 65 20 50 61 * Next on the Pa
74700 72 73 65 2e 70 5a 6f 6d 62 69 65 54 61 62 20 6c rse.pZombieTab l
74710 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ist */.};../*.**
74720 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 Allowed values
74730 66 6f 72 20 54 61 62 65 2e 74 61 62 46 6c 61 67 for Tabe.tabFlag
74740 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 s..*/.#define TF
74750 5f 52 65 61 64 6f 6e 6c 79 20 20 20 20 20 20 20 _Readonly
74760 20 30 78 30 31 20 20 20 20 2f 2a 20 52 65 61 64 0x01 /* Read
74770 2d 6f 6e 6c 79 20 73 79 73 74 65 6d 20 74 61 62 -only system tab
74780 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 le */.#define TF
74790 5f 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 _Ephemeral
747a0 20 30 78 30 32 20 20 20 20 2f 2a 20 41 6e 20 65 0x02 /* An e
747b0 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 2a phemeral table *
747c0 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 48 61 73 /.#define TF_Has
747d0 50 72 69 6d 61 72 79 4b 65 79 20 20 20 30 78 30 PrimaryKey 0x0
747e0 34 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 4 /* Table ha
747f0 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 s a primary key
74800 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 41 75 */.#define TF_Au
74810 74 6f 69 6e 63 72 65 6d 65 6e 74 20 20 20 30 78 toincrement 0x
74820 30 38 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 08 /* Integer
74830 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 primary key is
74840 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f autoincrement */
74850 0a 23 64 65 66 69 6e 65 20 54 46 5f 56 69 72 74 .#define TF_Virt
74860 75 61 6c 20 20 20 20 20 20 20 20 20 30 78 31 30 ual 0x10
74870 20 20 20 20 2f 2a 20 49 73 20 61 20 76 69 72 74 /* Is a virt
74880 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 0a 0a 2f ual table */.../
74890 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65 *.** Test to see
748a0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
748b0 61 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 a table is a vir
748c0 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 tual table. Thi
748d0 73 20 69 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20 s is.** done as
748e0 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 a macro so that
748f0 69 74 20 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d it will be optim
74900 69 7a 65 64 20 6f 75 74 20 77 68 65 6e 20 76 69 ized out when vi
74910 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73 rtual.** table s
74920 75 70 70 6f 72 74 20 69 73 20 6f 6d 69 74 74 65 upport is omitte
74930 64 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 d from the build
74940 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
74950 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
74960 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 TABLE.# define
74970 49 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 IsVirtual(X)
74980 20 20 28 28 28 58 29 2d 3e 74 61 62 46 6c 61 67 (((X)->tabFlag
74990 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 s & TF_Virtual)!
749a0 3d 30 29 0a 23 20 20 64 65 66 69 6e 65 20 49 73 =0).# define Is
749b0 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 HiddenColumn(X)
749c0 28 28 28 58 29 2d 3e 63 6f 6c 46 6c 61 67 73 20 (((X)->colFlags
749d0 26 20 43 4f 4c 46 4c 41 47 5f 48 49 44 44 45 4e & COLFLAG_HIDDEN
749e0 29 21 3d 30 29 0a 23 65 6c 73 65 0a 23 20 20 64 )!=0).#else.# d
749f0 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c 28 efine IsVirtual(
74a00 58 29 20 20 20 20 20 20 30 0a 23 20 20 64 65 66 X) 0.# def
74a10 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 ine IsHiddenColu
74a20 6d 6e 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a mn(X) 0.#endif..
74a30 2f 2a 0a 2a 2a 20 45 61 63 68 20 66 6f 72 65 69 /*.** Each forei
74a40 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e gn key constrain
74a50 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 t is an instance
74a60 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
74a70 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a g structure..**.
74a80 2a 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b 65 79 ** A foreign key
74a90 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 is associated w
74aa0 69 74 68 20 74 77 6f 20 74 61 62 6c 65 73 2e 20 ith two tables.
74ab0 20 54 68 65 20 22 66 72 6f 6d 22 20 74 61 62 6c The "from" tabl
74ac0 65 20 69 73 0a 2a 2a 20 74 68 65 20 74 61 62 6c e is.** the tabl
74ad0 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 e that contains
74ae0 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 the REFERENCES c
74af0 6c 61 75 73 65 20 74 68 61 74 20 63 72 65 61 74 lause that creat
74b00 65 73 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a es the foreign.*
74b10 2a 20 6b 65 79 2e 20 20 54 68 65 20 22 74 6f 22 * key. The "to"
74b20 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 74 61 table is the ta
74b30 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d 65 ble that is name
74b40 64 20 69 6e 20 74 68 65 20 52 45 46 45 52 45 4e d in the REFEREN
74b50 43 45 53 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 43 CES clause..** C
74b60 6f 6e 73 69 64 65 72 20 74 68 69 73 20 65 78 61 onsider this exa
74b70 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 mple:.**.**
74b80 43 52 45 41 54 45 20 54 41 42 4c 45 20 65 78 31 CREATE TABLE ex1
74b90 28 0a 2a 2a 20 20 20 20 20 20 20 61 20 49 4e 54 (.** a INT
74ba0 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
74bb0 2c 0a 2a 2a 20 20 20 20 20 20 20 62 20 49 4e 54 ,.** b INT
74bc0 45 47 45 52 20 43 4f 4e 53 54 52 41 49 4e 54 20 EGER CONSTRAINT
74bd0 66 6b 31 20 52 45 46 45 52 45 4e 43 45 53 20 65 fk1 REFERENCES e
74be0 78 32 28 78 29 0a 2a 2a 20 20 20 20 20 29 3b 0a x2(x).** );.
74bf0 2a 2a 0a 2a 2a 20 46 6f 72 20 66 6f 72 65 69 67 **.** For foreig
74c00 6e 20 6b 65 79 20 22 66 6b 31 22 2c 20 74 68 65 n key "fk1", the
74c10 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 22 from-table is "
74c20 65 78 31 22 20 61 6e 64 20 74 68 65 20 74 6f 2d ex1" and the to-
74c30 74 61 62 6c 65 20 69 73 20 22 65 78 32 22 2e 0a table is "ex2"..
74c40 2a 2a 0a 2a 2a 20 45 61 63 68 20 52 45 46 45 52 **.** Each REFER
74c50 45 4e 43 45 53 20 63 6c 61 75 73 65 20 67 65 6e ENCES clause gen
74c60 65 72 61 74 65 73 20 61 6e 20 69 6e 73 74 61 6e erates an instan
74c70 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
74c80 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a ing structure.**
74c90 20 77 68 69 63 68 20 69 73 20 61 74 74 61 63 68 which is attach
74ca0 65 64 20 74 6f 20 74 68 65 20 66 72 6f 6d 2d 74 ed to the from-t
74cb0 61 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d 74 61 able. The to-ta
74cc0 62 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65 78 69 ble need not exi
74cd0 73 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 66 st when.** the f
74ce0 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 63 72 65 rom-table is cre
74cf0 61 74 65 64 2e 20 20 54 68 65 20 65 78 69 73 74 ated. The exist
74d00 65 6e 63 65 20 6f 66 20 74 68 65 20 74 6f 2d 74 ence of the to-t
74d10 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 68 65 63 able is not chec
74d20 6b 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 ked..*/.struct F
74d30 4b 65 79 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70 Key {. Table *p
74d40 46 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 61 62 From; /* Tab
74d50 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 le containing th
74d60 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 e REFERENCES cla
74d70 75 73 65 20 28 61 6b 61 3a 20 43 68 69 6c 64 29 use (aka: Child)
74d80 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 */. FKey *pNex
74d90 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 tFrom; /* Next
74da0 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 foreign key in p
74db0 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a From */. char *
74dc0 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e zTo; /* N
74dd0 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 61 ame of table tha
74de0 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 t the key points
74df0 20 74 6f 20 28 61 6b 61 3a 20 50 61 72 65 6e 74 to (aka: Parent
74e00 29 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 ) */. FKey *pNe
74e10 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 74 xtTo; /* Next
74e20 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 foreign key on
74e30 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f 20 table named zTo
74e40 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 50 72 65 76 */. FKey *pPrev
74e50 54 6f 3b 20 20 20 20 2f 2a 20 50 72 65 76 69 6f To; /* Previo
74e60 75 73 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f us foreign key o
74e70 6e 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 n table named zT
74e80 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b o */. int nCol;
74e90 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
74ea0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
74eb0 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 2f this key */. /
74ec0 2a 20 45 56 3a 20 52 2d 33 30 33 32 33 2d 32 31 * EV: R-30323-21
74ed0 39 31 37 20 2a 2f 0a 20 20 75 38 20 69 73 44 65 917 */. u8 isDe
74ee0 66 65 72 72 65 64 3b 20 20 20 20 2f 2a 20 54 72 ferred; /* Tr
74ef0 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 ue if constraint
74f00 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 65 66 checking is def
74f10 65 72 72 65 64 20 74 69 6c 6c 20 43 4f 4d 4d 49 erred till COMMI
74f20 54 20 2a 2f 0a 20 20 75 38 20 61 41 63 74 69 6f T */. u8 aActio
74f30 6e 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 2f n[2]; /
74f40 2a 20 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64 20 * ON DELETE and
74f50 4f 4e 20 55 50 44 41 54 45 20 61 63 74 69 6f 6e ON UPDATE action
74f60 73 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 20 s, respectively
74f70 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 61 70 */. Trigger *ap
74f80 54 72 69 67 67 65 72 5b 32 5d 3b 20 20 2f 2a 20 Trigger[2]; /*
74f90 54 72 69 67 67 65 72 73 20 66 6f 72 20 61 41 63 Triggers for aAc
74fa0 74 69 6f 6e 5b 5d 20 61 63 74 69 6f 6e 73 20 2a tion[] actions *
74fb0 2f 0a 20 20 73 74 72 75 63 74 20 73 43 6f 6c 4d /. struct sColM
74fc0 61 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69 6e 67 ap { /* Mapping
74fd0 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 of columns in p
74fe0 46 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e 73 20 From to columns
74ff0 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20 20 20 69 6e in zTo */. in
75000 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 t iFrom;
75010 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c /* Index of col
75020 75 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a umn in pFrom */.
75030 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 char *zCol;
75040 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
75050 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54 6f 2e f column in zTo.
75060 20 20 49 66 20 30 20 75 73 65 20 50 52 49 4d 41 If 0 use PRIMA
75070 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 20 61 43 RY KEY */. } aC
75080 6f 6c 5b 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a ol[1]; /*
75090 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 One entry for e
750a0 61 63 68 20 6f 66 20 6e 43 6f 6c 20 63 6f 6c 75 ach of nCol colu
750b0 6d 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a mn s */.};../*.*
750c0 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f 72 74 * SQLite support
750d0 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e 74 s many different
750e0 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 65 ways to resolve
750f0 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a a constraint.**
75100 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 error. ROLLBAC
75110 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 K processing mea
75120 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73 74 72 ns that a constr
75130 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a aint violation.*
75140 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f 70 65 * causes the ope
75150 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 ration in proces
75160 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f s to fail and fo
75170 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 r the current tr
75180 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 ansaction.** to
75190 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 be rolled back.
751a0 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e ABORT processin
751b0 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 g means the oper
751c0 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 ation in process
751d0 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 6e .** fails and an
751e0 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20 y prior changes
751f0 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70 from that one op
75200 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61 63 6b eration are back
75210 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74 ed out,.** but t
75220 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
75230 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 s not rolled bac
75240 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 73 73 k. FAIL process
75250 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a ing means that.*
75260 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 * the operation
75270 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74 6f 70 in progress stop
75280 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e s and returns an
75290 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 75 error code. Bu
752a0 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 t prior.** chang
752b0 65 73 20 64 75 65 20 74 6f 20 74 68 65 20 73 61 es due to the sa
752c0 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 me operation are
752d0 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20 not backed out
752e0 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a and no rollback.
752f0 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f ** occurs. IGNO
75300 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 RE means that th
75310 65 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77 e particular row
75320 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 that caused the
75330 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 constraint.** e
75340 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65 rror is not inse
75350 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e rted or updated.
75360 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e Processing con
75370 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 tinues and no er
75380 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e ror.** is return
75390 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 61 ed. REPLACE mea
753a0 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 73 74 ns that preexist
753b0 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f 77 ing database row
753c0 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a s that caused.**
753d0 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 a UNIQUE constr
753e0 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61 aint violation a
753f0 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 68 re removed so th
75400 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73 65 72 at the new inser
75410 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 63 t or.** update c
75420 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 72 6f an proceed. Pro
75430 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 cessing continue
75440 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69 s and no error i
75450 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a s reported..**.*
75460 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45 54 4e * RESTRICT, SETN
75470 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 45 ULL, and CASCADE
75480 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f actions apply o
75490 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b nly to foreign k
754a0 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54 eys..** RESTRICT
754b0 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
754c0 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 49 ABORT for IMMEDI
754d0 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 ATE foreign keys
754e0 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 and the.** same
754f0 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 as ROLLBACK for
75500 20 44 45 46 45 52 52 45 44 20 6b 65 79 73 2e 20 DEFERRED keys.
75510 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 SETNULL means t
75520 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e 0a hat the foreign.
75530 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20 74 6f ** key is set to
75540 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 20 NULL. CASCADE
75550 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44 45 4c means that a DEL
75560 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f 66 ETE or UPDATE of
75570 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 the.** referenc
75580 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20 ed table row is
75590 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74 6f 20 propagated into
755a0 74 68 65 20 72 6f 77 20 74 68 61 74 20 68 6f 6c the row that hol
755b0 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67 ds the.** foreig
755c0 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 n key..** .** Th
755d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 e following symb
755e0 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72 65 20 olic values are
755f0 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 used to record w
75600 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20 hich type.** of
75610 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a action to take..
75620 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f */.#define OE_No
75630 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68 ne 0 /* Th
75640 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72 ere is no constr
75650 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f aint to check */
75660 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c .#define OE_Roll
75670 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c back 1 /* Fail
75680 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 the operation a
75690 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 nd rollback the
756a0 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 transaction */.#
756b0 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20 define OE_Abort
756c0 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f 2 /* Back o
756d0 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 20 64 ut changes but d
756e0 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72 o no rollback tr
756f0 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 ansaction */.#de
75700 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 20 fine OE_Fail
75710 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 65 3 /* Stop the
75720 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 20 6c operation but l
75730 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 63 eave all prior c
75740 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e hanges */.#defin
75750 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 20 e OE_Ignore 4
75760 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 /* Ignore the
75770 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f error. Do not do
75780 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55 the INSERT or U
75790 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 PDATE */.#define
757a0 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 20 20 OE_Replace 5
757b0 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 73 74 /* Delete exist
757c0 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 65 6e ing record, then
757d0 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 do INSERT or UP
757e0 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 DATE */..#define
757f0 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36 20 20 OE_Restrict 6
75800 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72 /* OE_Abort for
75810 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52 IMMEDIATE, OE_R
75820 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 45 ollback for DEFE
75830 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 RRED */.#define
75840 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 20 OE_SetNull 7
75850 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 /* Set the forei
75860 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 gn key value to
75870 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NULL */.#define
75880 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20 20 20 OE_SetDflt 8
75890 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 /* Set the forei
758a0 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 gn key value to
758b0 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23 its default */.#
758c0 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 64 define OE_Cascad
758d0 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63 61 64 e 9 /* Cascad
758e0 65 20 74 68 65 20 63 68 61 6e 67 65 73 20 2a 2f e the changes */
758f0 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 66 ..#define OE_Def
75900 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f 20 ault 99 /* Do
75910 77 68 61 74 65 76 65 72 20 74 68 65 20 64 65 66 whatever the def
75920 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 2a ault action is *
75930 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 /.../*.** An ins
75940 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
75950 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
75960 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 is passed as th
75970 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d e first.** argum
75980 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64 ent to sqlite3Vd
75990 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64 beKeyCompare and
759a0 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 is used to cont
759b0 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 rol the .** comp
759c0 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 77 arison of the tw
759d0 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f o index keys..*/
759e0 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 .struct KeyInfo
759f0 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b {. sqlite3 *db;
75a00 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
75a10 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
75a20 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 on */. u8 enc;
75a30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
75a40 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f ext encoding - o
75a50 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 ne of the SQLITE
75a60 5f 55 54 46 2a 20 76 61 6c 75 65 73 20 2a 2f 0a _UTF* values */.
75a70 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 u16 nField;
75a80 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
75a90 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 of entries in aC
75aa0 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 oll[] */. u8 *a
75ab0 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f SortOrder; /
75ac0 2a 20 53 6f 72 74 20 6f 72 64 65 72 20 66 6f 72 * Sort order for
75ad0 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 20 20 4d each column. M
75ae0 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 ay be NULL */.
75af0 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c 5b 31 CollSeq *aColl[1
75b00 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 ]; /* Collating
75b10 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 61 sequence for ea
75b20 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6b ch term of the k
75b30 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ey */.};../*.**
75b40 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
75b50 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
75b60 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66 ucture holds inf
75b70 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 ormation about a
75b80 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 6e 64 65 78 .** single index
75b90 20 72 65 63 6f 72 64 20 74 68 61 74 20 68 61 73 record that has
75ba0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 61 already been pa
75bb0 72 73 65 64 20 6f 75 74 20 69 6e 74 6f 20 69 6e rsed out into in
75bc0 64 69 76 69 64 75 61 6c 0a 2a 2a 20 76 61 6c 75 dividual.** valu
75bd0 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 63 6f es..**.** A reco
75be0 72 64 20 69 73 20 61 6e 20 6f 62 6a 65 63 74 20 rd is an object
75bf0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 6f 6e that contains on
75c00 65 20 6f 72 20 6d 6f 72 65 20 66 69 65 6c 64 73 e or more fields
75c10 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 52 65 63 of data..** Rec
75c20 6f 72 64 73 20 61 72 65 20 75 73 65 64 20 74 6f ords are used to
75c30 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74 65 store the conte
75c40 6e 74 20 6f 66 20 61 20 74 61 62 6c 65 20 72 6f nt of a table ro
75c50 77 20 61 6e 64 20 74 6f 20 73 74 6f 72 65 0a 2a w and to store.*
75c60 2a 20 74 68 65 20 6b 65 79 20 6f 66 20 61 6e 20 * the key of an
75c70 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f 62 20 65 index. A blob e
75c80 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 65 63 ncoding of a rec
75c90 6f 72 64 20 69 73 20 63 72 65 61 74 65 64 20 62 ord is created b
75ca0 79 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d 61 6b 65 y.** the OP_Make
75cb0 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 Record opcode of
75cc0 20 74 68 65 20 56 44 42 45 20 61 6e 64 20 69 73 the VDBE and is
75cd0 20 64 69 73 61 73 73 65 6d 62 6c 65 64 20 62 79 disassembled by
75ce0 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 6f 6c 75 6d the.** OP_Colum
75cf0 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 n opcode..**.**
75d00 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68 This structure h
75d10 6f 6c 64 73 20 61 20 72 65 63 6f 72 64 20 74 68 olds a record th
75d20 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 at has already b
75d30 65 65 6e 20 64 69 73 61 73 73 65 6d 62 6c 65 64 een disassembled
75d40 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 20 63 6f 6e .** into its con
75d50 73 74 69 74 75 65 6e 74 20 66 69 65 6c 64 73 2e stituent fields.
75d60 0a 2a 2f 0a 73 74 72 75 63 74 20 55 6e 70 61 63 .*/.struct Unpac
75d70 6b 65 64 52 65 63 6f 72 64 20 7b 0a 20 20 4b 65 kedRecord {. Ke
75d80 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b yInfo *pKeyInfo;
75d90 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 61 /* Collation a
75da0 6e 64 20 73 6f 72 74 2d 6f 72 64 65 72 20 69 6e nd sort-order in
75db0 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 formation */. u
75dc0 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 16 nField;
75dd0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
75de0 65 6e 74 72 69 65 73 20 69 6e 20 61 70 4d 65 6d entries in apMem
75df0 5b 5d 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 73 [] */. u8 flags
75e00 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 ; /* B
75e10 6f 6f 6c 65 61 6e 20 73 65 74 74 69 6e 67 73 2e oolean settings.
75e20 20 20 55 4e 50 41 43 4b 45 44 5f 2e 2e 2e 20 62 UNPACKED_... b
75e30 65 6c 6f 77 20 2a 2f 0a 20 20 69 36 34 20 72 6f elow */. i64 ro
75e40 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a wid; /*
75e50 20 55 73 65 64 20 62 79 20 55 4e 50 41 43 4b 45 Used by UNPACKE
75e60 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 D_PREFIX_SEARCH
75e70 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 */. Mem *aMem;
75e80 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
75e90 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 es */.};../*.**
75ea0 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f Allowed values o
75eb0 66 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 f UnpackedRecord
75ec0 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e .flags.*/.#defin
75ed0 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b e UNPACKED_INCRK
75ee0 45 59 20 20 20 20 20 20 20 30 78 30 31 20 20 2f EY 0x01 /
75ef0 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 20 * Make this key
75f00 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65 an epsilon large
75f10 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 r */.#define UNP
75f20 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 ACKED_PREFIX_MAT
75f30 43 48 20 20 30 78 30 32 20 20 2f 2a 20 41 20 70 CH 0x02 /* A p
75f40 72 65 66 69 78 20 6d 61 74 63 68 20 69 73 20 63 refix match is c
75f50 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a onsidered OK */.
75f60 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 #define UNPACKED
75f70 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 30 _PREFIX_SEARCH 0
75f80 78 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 66 x04 /* Ignore f
75f90 69 6e 61 6c 20 28 72 6f 77 69 64 29 20 66 69 65 inal (rowid) fie
75fa0 6c 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 ld */../*.** Eac
75fb0 68 20 53 51 4c 20 69 6e 64 65 78 20 69 73 20 72 h SQL index is r
75fc0 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 epresented in me
75fd0 6d 6f 72 79 20 62 79 20 61 6e 0a 2a 2a 20 69 6e mory by an.** in
75fe0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
75ff0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
76000 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c e..**.** The col
76010 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c umns of the tabl
76020 65 20 74 68 61 74 20 61 72 65 20 74 6f 20 62 65 e that are to be
76030 20 69 6e 64 65 78 65 64 20 61 72 65 20 64 65 73 indexed are des
76040 63 72 69 62 65 64 0a 2a 2a 20 62 79 20 74 68 65 cribed.** by the
76050 20 61 69 43 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c aiColumn[] fiel
76060 64 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 d of this struct
76070 75 72 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c ure. For exampl
76080 65 2c 20 73 75 70 70 6f 73 65 0a 2a 2a 20 77 65 e, suppose.** we
76090 20 68 61 76 65 20 74 68 65 20 66 6f 6c 6c 6f 77 have the follow
760a0 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 69 6e ing table and in
760b0 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 dex:.**.** C
760c0 52 45 41 54 45 20 54 41 42 4c 45 20 45 78 31 28 REATE TABLE Ex1(
760d0 63 31 20 69 6e 74 2c 20 63 32 20 69 6e 74 2c 20 c1 int, c2 int,
760e0 63 33 20 74 65 78 74 29 3b 0a 2a 2a 20 20 20 20 c3 text);.**
760f0 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 45 78 CREATE INDEX Ex
76100 32 20 4f 4e 20 45 78 31 28 63 33 2c 63 31 29 3b 2 ON Ex1(c3,c1);
76110 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 54 61 .**.** In the Ta
76120 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 64 65 ble structure de
76130 73 63 72 69 62 69 6e 67 20 45 78 31 2c 20 6e 43 scribing Ex1, nC
76140 6f 6c 3d 3d 33 20 62 65 63 61 75 73 65 20 74 68 ol==3 because th
76150 65 72 65 20 61 72 65 0a 2a 2a 20 74 68 72 65 65 ere are.** three
76160 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
76170 74 61 62 6c 65 2e 20 20 49 6e 20 74 68 65 20 49 table. In the I
76180 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 64 ndex structure d
76190 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20 45 78 32 escribing.** Ex2
761a0 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e , nColumn==2 sin
761b0 63 65 20 32 20 6f 66 20 74 68 65 20 33 20 63 6f ce 2 of the 3 co
761c0 6c 75 6d 6e 73 20 6f 66 20 45 78 31 20 61 72 65 lumns of Ex1 are
761d0 20 69 6e 64 65 78 65 64 2e 0a 2a 2a 20 54 68 65 indexed..** The
761e0 20 76 61 6c 75 65 20 6f 66 20 61 69 43 6f 6c 75 value of aiColu
761f0 6d 6e 20 69 73 20 7b 32 2c 20 30 7d 2e 20 20 61 mn is {2, 0}. a
76200 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 iColumn[0]==2 be
76210 63 61 75 73 65 20 74 68 65 20 0a 2a 2a 20 66 69 cause the .** fi
76220 72 73 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 rst column to be
76230 20 69 6e 64 65 78 65 64 20 28 63 33 29 20 68 61 indexed (c3) ha
76240 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 32 20 s an index of 2
76250 69 6e 20 45 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a in Ex1.aCol[]..*
76260 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 63 6f 6c * The second col
76270 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78 65 umn to be indexe
76280 64 20 28 63 31 29 20 68 61 73 20 61 6e 20 69 6e d (c1) has an in
76290 64 65 78 20 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 dex of 0 in.** E
762a0 78 31 2e 61 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 x1.aCol[], hence
762b0 20 45 78 32 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d Ex2.aiColumn[1]
762c0 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 ==0..**.** The I
762d0 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 66 69 65 ndex.onError fie
762e0 6c 64 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 ld determines wh
762f0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
76300 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 73 indexed columns
76310 0a 2a 2a 20 6d 75 73 74 20 62 65 20 75 6e 69 71 .** must be uniq
76320 75 65 20 61 6e 64 20 77 68 61 74 20 74 6f 20 64 ue and what to d
76330 6f 20 69 66 20 74 68 65 79 20 61 72 65 20 6e 6f o if they are no
76340 74 2e 20 20 57 68 65 6e 20 49 6e 64 65 78 2e 6f t. When Index.o
76350 6e 45 72 72 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a nError=OE_None,.
76360 2a 2a 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73 ** it means this
76370 20 69 73 20 6e 6f 74 20 61 20 75 6e 69 71 75 65 is not a unique
76380 20 69 6e 64 65 78 2e 20 20 4f 74 68 65 72 77 69 index. Otherwi
76390 73 65 20 69 74 20 69 73 20 61 20 75 6e 69 71 75 se it is a uniqu
763a0 65 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74 e index.** and t
763b0 68 65 20 76 61 6c 75 65 20 6f 66 20 49 6e 64 65 he value of Inde
763c0 78 2e 6f 6e 45 72 72 6f 72 20 69 6e 64 69 63 61 x.onError indica
763d0 74 65 20 74 68 65 20 77 68 69 63 68 20 63 6f 6e te the which con
763e0 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e flict resolution
763f0 20 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 20 74 .** algorithm t
76400 6f 20 65 6d 70 6c 6f 79 20 77 68 65 6e 65 76 65 o employ wheneve
76410 72 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 r an attempt is
76420 6d 61 64 65 20 74 6f 20 69 6e 73 65 72 74 20 61 made to insert a
76430 20 6e 6f 6e 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 non-unique.** e
76440 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 lement..*/.struc
76450 74 20 49 6e 64 65 78 20 7b 0a 20 20 63 68 61 72 t Index {. char
76460 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 *zName; /*
76470 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 69 6e 64 Name of this ind
76480 65 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 ex */. int *aiC
76490 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69 63 olumn; /* Whic
764a0 68 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73 h columns are us
764b0 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 ed by this index
764c0 2e 20 20 31 73 74 20 69 73 20 30 20 2a 2f 0a 20 . 1st is 0 */.
764d0 20 74 52 6f 77 63 6e 74 20 2a 61 69 52 6f 77 45 tRowcnt *aiRowE
764e0 73 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 st; /* Result of
764f0 20 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e 20 72 ANALYZE: Est. r
76500 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62 79 20 ows selected by
76510 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 each column */.
76520 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20 Table *pTable;
76530 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 61 62 /* The SQL tab
76540 6c 65 20 62 65 69 6e 67 20 69 6e 64 65 78 65 64 le being indexed
76550 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c */. char *zCol
76560 41 66 66 3b 20 20 20 2f 2a 20 53 74 72 69 6e 67 Aff; /* String
76570 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 61 66 defining the af
76580 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68 20 63 finity of each c
76590 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 olumn */. Index
765a0 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 54 *pNext; /* T
765b0 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20 61 73 he next index as
765c0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
765d0 65 20 73 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a e same table */.
765e0 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d Schema *pSchem
765f0 61 3b 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e a; /* Schema con
76600 74 61 69 6e 69 6e 67 20 74 68 69 73 20 69 6e 64 taining this ind
76610 65 78 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 ex */. u8 *aSor
76620 74 4f 72 64 65 72 3b 20 20 2f 2a 20 41 72 72 61 tOrder; /* Arra
76630 79 20 6f 66 20 73 69 7a 65 20 49 6e 64 65 78 2e y of size Index.
76640 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75 65 3d 3d 44 nColumn. True==D
76650 45 53 43 2c 20 46 61 6c 73 65 3d 3d 41 53 43 20 ESC, False==ASC
76660 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f */. char **azCo
76670 6c 6c 3b 20 20 20 2f 2a 20 41 72 72 61 79 20 6f ll; /* Array o
76680 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 f collation sequ
76690 65 6e 63 65 20 6e 61 6d 65 73 20 66 6f 72 20 69 ence names for i
766a0 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 ndex */. int nC
766b0 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 olumn; /* Nu
766c0 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
766d0 69 6e 20 74 68 65 20 74 61 62 6c 65 20 75 73 65 in the table use
766e0 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 20 d by this index
766f0 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 */. int tnum;
76700 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f /* Page co
76710 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20 6f 66 ntaining root of
76720 20 74 68 69 73 20 69 6e 64 65 78 20 69 6e 20 64 this index in d
76730 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
76740 20 20 75 38 20 6f 6e 45 72 72 6f 72 3b 20 20 20 u8 onError;
76750 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c 20 /* OE_Abort,
76760 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f 52 65 OE_Ignore, OE_Re
76770 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e place, or OE_Non
76780 65 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f 49 6e e */. u8 autoIn
76790 64 65 78 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 dex; /* True
767a0 69 66 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 if is automatica
767b0 6c 6c 79 20 63 72 65 61 74 65 64 20 28 65 78 3a lly created (ex:
767c0 20 62 79 20 55 4e 49 51 55 45 29 20 2a 2f 0a 20 by UNIQUE) */.
767d0 20 75 38 20 62 55 6e 6f 72 64 65 72 65 64 3b 20 u8 bUnordered;
767e0 20 20 2f 2a 20 55 73 65 20 74 68 69 73 20 69 6e /* Use this in
767f0 64 65 78 20 66 6f 72 20 3d 3d 20 6f 72 20 49 4e dex for == or IN
76800 20 71 75 65 72 69 65 73 20 6f 6e 6c 79 20 2a 2f queries only */
76810 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
76820 4e 41 42 4c 45 5f 53 54 41 54 33 0a 20 20 69 6e NABLE_STAT3. in
76830 74 20 6e 53 61 6d 70 6c 65 3b 20 20 20 20 20 20 t nSample;
76840 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
76850 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 of elements in
76860 61 53 61 6d 70 6c 65 5b 5d 20 2a 2f 0a 20 20 74 aSample[] */. t
76870 52 6f 77 63 6e 74 20 61 76 67 45 71 3b 20 20 20 Rowcnt avgEq;
76880 20 20 20 20 20 20 20 20 2f 2a 20 41 76 65 72 61 /* Avera
76890 67 65 20 6e 45 71 20 76 61 6c 75 65 20 66 6f 72 ge nEq value for
768a0 20 6b 65 79 20 76 61 6c 75 65 73 20 6e 6f 74 20 key values not
768b0 69 6e 20 61 53 61 6d 70 6c 65 20 2a 2f 0a 20 20 in aSample */.
768c0 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a 61 53 61 IndexSample *aSa
768d0 6d 70 6c 65 3b 20 20 20 20 2f 2a 20 53 61 6d 70 mple; /* Samp
768e0 6c 65 73 20 6f 66 20 74 68 65 20 6c 65 66 74 2d les of the left-
768f0 6d 6f 73 74 20 6b 65 79 20 2a 2f 0a 23 65 6e 64 most key */.#end
76900 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 if.};../*.** Eac
76910 68 20 73 61 6d 70 6c 65 20 73 74 6f 72 65 64 20 h sample stored
76920 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 in the sqlite_st
76930 61 74 33 20 74 61 62 6c 65 20 69 73 20 72 65 70 at3 table is rep
76940 72 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f resented in memo
76950 72 79 20 0a 2a 2a 20 75 73 69 6e 67 20 61 20 73 ry .** using a s
76960 74 72 75 63 74 75 72 65 20 6f 66 20 74 68 69 73 tructure of this
76970 20 74 79 70 65 2e 20 20 53 65 65 20 64 6f 63 75 type. See docu
76980 6d 65 6e 74 61 74 69 6f 6e 20 61 74 20 74 68 65 mentation at the
76990 20 74 6f 70 20 6f 66 20 74 68 65 0a 2a 2a 20 61 top of the.** a
769a0 6e 61 6c 79 7a 65 2e 63 20 73 6f 75 72 63 65 20 nalyze.c source
769b0 66 69 6c 65 20 66 6f 72 20 61 64 64 69 74 69 6f file for additio
769c0 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
769d0 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 65 78 .*/.struct Index
769e0 53 61 6d 70 6c 65 20 7b 0a 20 20 75 6e 69 6f 6e Sample {. union
769f0 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 {. char *z;
76a00 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 /* Value
76a10 69 66 20 65 54 79 70 65 20 69 73 20 53 51 4c 49 if eType is SQLI
76a20 54 45 5f 54 45 58 54 20 6f 72 20 53 51 4c 49 54 TE_TEXT or SQLIT
76a30 45 5f 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 64 6f E_BLOB */. do
76a40 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20 2f 2a uble r; /*
76a50 20 56 61 6c 75 65 20 69 66 20 65 54 79 70 65 20 Value if eType
76a60 69 73 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 is SQLITE_FLOAT
76a70 2a 2f 0a 20 20 20 20 69 36 34 20 69 3b 20 20 20 */. i64 i;
76a80 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 /* Value
76a90 69 66 20 65 54 79 70 65 20 69 73 20 53 51 4c 49 if eType is SQLI
76aa0 54 45 5f 49 4e 54 45 47 45 52 20 2a 2f 0a 20 20 TE_INTEGER */.
76ab0 7d 20 75 3b 0a 20 20 75 38 20 65 54 79 70 65 3b } u;. u8 eType;
76ac0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 /* SQLI
76ad0 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f TE_NULL, SQLITE_
76ae0 49 4e 54 45 47 45 52 20 2e 2e 2e 20 65 74 63 2e INTEGER ... etc.
76af0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b */. int nByte;
76b00 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
76b10 69 6e 20 62 79 74 65 20 6f 66 20 74 65 78 74 20 in byte of text
76b20 6f 72 20 62 6c 6f 62 2e 20 2a 2f 0a 20 20 74 52 or blob. */. tR
76b30 6f 77 63 6e 74 20 6e 45 71 3b 20 20 20 20 20 20 owcnt nEq;
76b40 2f 2a 20 45 73 74 2e 20 6e 75 6d 62 65 72 20 6f /* Est. number o
76b50 66 20 72 6f 77 73 20 77 68 65 72 65 20 74 68 65 f rows where the
76b60 20 6b 65 79 20 65 71 75 61 6c 73 20 74 68 69 73 key equals this
76b70 20 73 61 6d 70 6c 65 20 2a 2f 0a 20 20 74 52 6f sample */. tRo
76b80 77 63 6e 74 20 6e 4c 74 3b 20 20 20 20 20 20 2f wcnt nLt; /
76b90 2a 20 45 73 74 2e 20 6e 75 6d 62 65 72 20 6f 66 * Est. number of
76ba0 20 72 6f 77 73 20 77 68 65 72 65 20 6b 65 79 20 rows where key
76bb0 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 69 is less than thi
76bc0 73 20 73 61 6d 70 6c 65 20 2a 2f 0a 20 20 74 52 s sample */. tR
76bd0 6f 77 63 6e 74 20 6e 44 4c 74 3b 20 20 20 20 20 owcnt nDLt;
76be0 2f 2a 20 45 73 74 2e 20 6e 75 6d 62 65 72 20 6f /* Est. number o
76bf0 66 20 64 69 73 74 69 6e 63 74 20 6b 65 79 73 20 f distinct keys
76c00 6c 65 73 73 20 74 68 61 6e 20 74 68 69 73 20 73 less than this s
76c10 61 6d 70 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ample */.};../*.
76c20 2a 2a 20 45 61 63 68 20 74 6f 6b 65 6e 20 63 6f ** Each token co
76c30 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74 68 65 20 ming out of the
76c40 6c 65 78 65 72 20 69 73 20 61 6e 20 69 6e 73 74 lexer is an inst
76c50 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 ance of.** this
76c60 73 74 72 75 63 74 75 72 65 2e 20 20 54 6f 6b 65 structure. Toke
76c70 6e 73 20 61 72 65 20 61 6c 73 6f 20 75 73 65 64 ns are also used
76c80 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 as part of an e
76c90 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a xpression..**.**
76ca0 20 4e 6f 74 65 20 69 66 20 54 6f 6b 65 6e 2e 7a Note if Token.z
76cb0 3d 3d 30 20 74 68 65 6e 20 54 6f 6b 65 6e 2e 64 ==0 then Token.d
76cc0 79 6e 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 61 yn and Token.n a
76cd0 72 65 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 re undefined and
76ce0 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 .** may contain
76cf0 72 61 6e 64 6f 6d 20 76 61 6c 75 65 73 2e 20 20 random values.
76d00 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20 Do not make any
76d10 61 73 73 75 6d 70 74 69 6f 6e 73 20 61 62 6f 75 assumptions abou
76d20 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a 20 61 t Token.dyn.** a
76d30 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68 65 6e 20 nd Token.n when
76d40 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f 0a 73 Token.z==0..*/.s
76d50 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b 0a 20 20 truct Token {.
76d60 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 20 20 const char *z;
76d70 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 /* Text of th
76d80 65 20 74 6f 6b 65 6e 2e 20 20 4e 6f 74 20 4e 55 e token. Not NU
76d90 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 21 20 2a LL-terminated! *
76da0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
76db0 20 6e 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 n; /* Number
76dc0 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 of characters i
76dd0 6e 20 74 68 69 73 20 74 6f 6b 65 6e 20 2a 2f 0a n this token */.
76de0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 };../*.** An ins
76df0 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 tance of this st
76e00 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
76e10 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 information nee
76e20 64 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 0a ded to generate.
76e30 2a 2a 20 63 6f 64 65 20 66 6f 72 20 61 20 53 45 ** code for a SE
76e40 4c 45 43 54 20 74 68 61 74 20 63 6f 6e 74 61 69 LECT that contai
76e50 6e 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e ns aggregate fun
76e60 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 ctions..**.** If
76e70 20 45 78 70 72 2e 6f 70 3d 3d 54 4b 5f 41 47 47 Expr.op==TK_AGG
76e80 5f 43 4f 4c 55 4d 4e 20 6f 72 20 54 4b 5f 41 47 _COLUMN or TK_AG
76e90 47 5f 46 55 4e 43 54 49 4f 4e 20 74 68 65 6e 20 G_FUNCTION then
76ea0 45 78 70 72 2e 70 41 67 67 49 6e 66 6f 20 69 73 Expr.pAggInfo is
76eb0 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f a.** pointer to
76ec0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e this structure.
76ed0 20 20 54 68 65 20 45 78 70 72 2e 69 43 6f 6c 75 The Expr.iColu
76ee0 6d 6e 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 mn field is the
76ef0 69 6e 64 65 78 20 69 6e 0a 2a 2a 20 41 67 67 49 index in.** AggI
76f00 6e 66 6f 2e 61 43 6f 6c 5b 5d 20 6f 72 20 41 67 nfo.aCol[] or Ag
76f10 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 5d 20 6f 66 gInfo.aFunc[] of
76f20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 information nee
76f30 64 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 0a ded to generate.
76f40 2a 2a 20 63 6f 64 65 20 66 6f 72 20 74 68 61 74 ** code for that
76f50 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 67 67 node..**.** Agg
76f60 49 6e 66 6f 2e 70 47 72 6f 75 70 42 79 20 61 6e Info.pGroupBy an
76f70 64 20 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 2e d AggInfo.aFunc.
76f80 70 45 78 70 72 20 70 6f 69 6e 74 20 74 6f 20 66 pExpr point to f
76f90 69 65 6c 64 73 20 77 69 74 68 69 6e 20 74 68 65 ields within the
76fa0 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 65 6c .** original Sel
76fb0 65 63 74 20 73 74 72 75 63 74 75 72 65 20 74 68 ect structure th
76fc0 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 at describes the
76fd0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
76fe0 74 2e 20 20 54 68 65 73 65 0a 2a 2a 20 66 69 65 t. These.** fie
76ff0 6c 64 73 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 lds do not need
77000 74 6f 20 62 65 20 66 72 65 65 64 20 77 68 65 6e to be freed when
77010 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 deallocating th
77020 65 20 41 67 67 49 6e 66 6f 20 73 74 72 75 63 74 e AggInfo struct
77030 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 ure..*/.struct A
77040 67 67 49 6e 66 6f 20 7b 0a 20 20 75 38 20 64 69 ggInfo {. u8 di
77050 72 65 63 74 4d 6f 64 65 3b 20 20 20 20 20 20 20 rectMode;
77060 20 20 20 2f 2a 20 44 69 72 65 63 74 20 72 65 6e /* Direct ren
77070 64 65 72 69 6e 67 20 6d 6f 64 65 20 6d 65 61 6e dering mode mean
77080 73 20 74 61 6b 65 20 64 61 74 61 20 64 69 72 65 s take data dire
77090 63 74 6c 79 0a 20 20 20 20 20 20 20 20 20 20 20 ctly.
770a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
770b0 2a 20 66 72 6f 6d 20 73 6f 75 72 63 65 20 74 61 * from source ta
770c0 62 6c 65 73 20 72 61 74 68 65 72 20 74 68 61 6e bles rather than
770d0 20 66 72 6f 6d 20 61 63 63 75 6d 75 6c 61 74 6f from accumulato
770e0 72 73 20 2a 2f 0a 20 20 75 38 20 75 73 65 53 6f rs */. u8 useSo
770f0 72 74 69 6e 67 49 64 78 3b 20 20 20 20 20 20 20 rtingIdx;
77100 2f 2a 20 49 6e 20 64 69 72 65 63 74 20 6d 6f 64 /* In direct mod
77110 65 2c 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 e, reference the
77120 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 72 sorting index r
77130 61 74 68 65 72 0a 20 20 20 20 20 20 20 20 20 20 ather.
77140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77150 2a 2a 20 74 68 61 6e 20 74 68 65 20 73 6f 75 72 ** than the sour
77160 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e ce table */. in
77170 74 20 73 6f 72 74 69 6e 67 49 64 78 3b 20 20 20 t sortingIdx;
77180 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 /* Cursor
77190 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f number of the so
771a0 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 rting index */.
771b0 20 69 6e 74 20 73 6f 72 74 69 6e 67 49 64 78 50 int sortingIdxP
771c0 54 61 62 3b 20 20 20 20 20 2f 2a 20 43 75 72 73 Tab; /* Curs
771d0 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 70 73 65 or number of pse
771e0 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 69 udo-table */. i
771f0 6e 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d nt nSortingColum
77200 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 n; /* Number
77210 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 of columns in t
77220 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 he sorting index
77230 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
77240 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a pGroupBy; /*
77250 20 54 68 65 20 67 72 6f 75 70 20 62 79 20 63 6c The group by cl
77260 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 ause */. struct
77270 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 7b 20 20 AggInfo_col {
77280 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 63 6f /* For each co
77290 6c 75 6d 6e 20 75 73 65 64 20 69 6e 20 73 6f 75 lumn used in sou
772a0 72 63 65 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 rce tables */.
772b0 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 Table *pTab;
772c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f /* So
772d0 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 urce table */.
772e0 20 20 69 6e 74 20 69 54 61 62 6c 65 3b 20 20 20 int iTable;
772f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 /* Cu
77300 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 rsor number of t
77310 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 he source table
77320 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 */. int iColu
77330 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 mn;
77340 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 /* Column number
77350 20 77 69 74 68 69 6e 20 74 68 65 20 73 6f 75 72 within the sour
77360 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 ce table */.
77370 69 6e 74 20 69 53 6f 72 74 65 72 43 6f 6c 75 6d int iSorterColum
77380 6e 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 n; /* Colu
77390 6d 6e 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 mn number in the
773a0 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a sorting index *
773b0 2f 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 /. int iMem;
773c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
773d0 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f * Memory locatio
773e0 6e 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61 n that acts as a
773f0 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 ccumulator */.
77400 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 Expr *pExpr;
77410 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
77420 65 20 6f 72 69 67 69 6e 61 6c 20 65 78 70 72 65 e original expre
77430 73 73 69 6f 6e 20 2a 2f 0a 20 20 7d 20 2a 61 43 ssion */. } *aC
77440 6f 6c 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d ol;. int nColum
77450 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a n; /*
77460 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 64 20 Number of used
77470 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 5b entries in aCol[
77480 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 ] */. int nAccu
77490 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f mulator; /
774a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 * Number of colu
774b0 6d 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 mns that show th
774c0 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 rough to the out
774d0 70 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 put..
774e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
774f0 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c * Additional col
77500 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e umns are used on
77510 6c 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 ly as parameters
77520 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 to.
77530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
77540 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
77550 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 ions */. struct
77560 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 AggInfo_func {
77570 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 /* For each ag
77580 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
77590 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 */. Expr *pE
775a0 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 xpr;
775b0 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 /* Expression e
775c0 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 ncoding the func
775d0 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 tion */. Func
775e0 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 Def *pFunc;
775f0 20 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 /* The aggr
77600 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 egate function i
77610 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f mplementation */
77620 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 . int iMem;
77630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
77640 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e Memory location
77650 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63 that acts as ac
77660 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 cumulator */.
77670 20 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 int iDistinct;
77680 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 /* Eph
77690 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 emeral table use
776a0 64 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 d to enforce DIS
776b0 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 TINCT */. } *aF
776c0 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 unc;. int nFunc
776d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
776e0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
776f0 69 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a ies in aFunc[] *
77700 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 /.};../*.** The
77710 64 61 74 61 74 79 70 65 20 79 6e 56 61 72 20 69 datatype ynVar i
77720 73 20 61 20 73 69 67 6e 65 64 20 69 6e 74 65 67 s a signed integ
77730 65 72 2c 20 65 69 74 68 65 72 20 31 36 2d 62 69 er, either 16-bi
77740 74 20 6f 72 20 33 32 2d 62 69 74 2e 0a 2a 2a 20 t or 32-bit..**
77750 55 73 75 61 6c 6c 79 20 69 74 20 69 73 20 31 36 Usually it is 16
77760 2d 62 69 74 73 2e 20 20 42 75 74 20 69 66 20 53 -bits. But if S
77770 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 QLITE_MAX_VARIAB
77780 4c 45 5f 4e 55 4d 42 45 52 20 69 73 20 67 72 65 LE_NUMBER is gre
77790 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 33 32 37 ater.** than 327
777a0 36 37 20 77 65 20 68 61 76 65 20 74 6f 20 6d 61 67 we have to ma
777b0 6b 65 20 69 74 20 33 32 2d 62 69 74 2e 20 20 31 ke it 32-bit. 1
777c0 36 2d 62 69 74 20 69 73 20 70 72 65 66 65 72 72 6-bit is preferr
777d0 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 69 74 ed because.** it
777e0 20 75 73 65 73 20 6c 65 73 73 20 6d 65 6d 6f 72 uses less memor
777f0 79 20 69 6e 20 74 68 65 20 45 78 70 72 20 6f 62 y in the Expr ob
77800 6a 65 63 74 2c 20 77 68 69 63 68 20 69 73 20 61 ject, which is a
77810 20 62 69 67 20 6d 65 6d 6f 72 79 20 75 73 65 72 big memory user
77820 0a 2a 2a 20 69 6e 20 73 79 73 74 65 6d 73 20 77 .** in systems w
77830 69 74 68 20 6c 6f 74 73 20 6f 66 20 70 72 65 70 ith lots of prep
77840 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e ared statements.
77850 20 20 41 6e 64 20 66 65 77 20 61 70 70 6c 69 63 And few applic
77860 61 74 69 6f 6e 73 0a 2a 2a 20 6e 65 65 64 20 6d ations.** need m
77870 6f 72 65 20 74 68 61 6e 20 61 62 6f 75 74 20 31 ore than about 1
77880 30 20 6f 72 20 32 30 20 76 61 72 69 61 62 6c 65 0 or 20 variable
77890 73 2e 20 20 42 75 74 20 73 6f 6d 65 20 65 78 74 s. But some ext
778a0 72 65 6d 65 20 75 73 65 72 73 20 77 61 6e 74 0a reme users want.
778b0 2a 2a 20 74 6f 20 68 61 76 65 20 70 72 65 70 61 ** to have prepa
778c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 77 red statements w
778d0 69 74 68 20 6f 76 65 72 20 33 32 37 36 37 20 76 ith over 32767 v
778e0 61 72 69 61 62 6c 65 73 2c 20 61 6e 64 20 66 6f ariables, and fo
778f0 72 20 74 68 65 6d 0a 2a 2a 20 74 68 65 20 6f 70 r them.** the op
77900 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c tion is availabl
77910 65 20 28 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 e (at compile-ti
77920 6d 65 29 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 me)..*/.#if SQLI
77930 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f TE_MAX_VARIABLE_
77940 4e 55 4d 42 45 52 3c 3d 33 32 37 36 37 0a 74 79 NUMBER<=32767.ty
77950 70 65 64 65 66 20 69 31 36 20 79 6e 56 61 72 3b pedef i16 ynVar;
77960 0a 23 65 6c 73 65 0a 74 79 70 65 64 65 66 20 69 .#else.typedef i
77970 6e 74 20 79 6e 56 61 72 3b 0a 23 65 6e 64 69 66 nt ynVar;.#endif
77980 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6e 6f 64 ../*.** Each nod
77990 65 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 e of an expressi
779a0 6f 6e 20 69 6e 20 74 68 65 20 70 61 72 73 65 20 on in the parse
779b0 74 72 65 65 20 69 73 20 61 6e 20 69 6e 73 74 61 tree is an insta
779c0 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 nce.** of this s
779d0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
779e0 45 78 70 72 2e 6f 70 20 69 73 20 74 68 65 20 6f Expr.op is the o
779f0 70 63 6f 64 65 2e 20 54 68 65 20 69 6e 74 65 67 pcode. The integ
77a00 65 72 20 70 61 72 73 65 72 20 74 6f 6b 65 6e 20 er parser token
77a10 63 6f 64 65 73 20 61 72 65 20 72 65 75 73 65 64 codes are reused
77a20 0a 2a 2a 20 61 73 20 6f 70 63 6f 64 65 73 20 68 .** as opcodes h
77a30 65 72 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 ere. For example
77a40 2c 20 74 68 65 20 70 61 72 73 65 72 20 64 65 66 , the parser def
77a50 69 6e 65 73 20 54 4b 5f 47 45 20 74 6f 20 62 65 ines TK_GE to be
77a60 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 63 an integer.** c
77a70 6f 64 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 ode representing
77a80 20 74 68 65 20 22 3e 3d 22 20 6f 70 65 72 61 74 the ">=" operat
77a90 6f 72 2e 20 54 68 69 73 20 73 61 6d 65 20 69 6e or. This same in
77aa0 74 65 67 65 72 20 63 6f 64 65 20 69 73 20 72 65 teger code is re
77ab0 75 73 65 64 0a 2a 2a 20 74 6f 20 72 65 70 72 65 used.** to repre
77ac0 73 65 6e 74 20 74 68 65 20 67 72 65 61 74 65 72 sent the greater
77ad0 2d 74 68 61 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 -than-or-equal-t
77ae0 6f 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 74 68 o operator in th
77af0 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 e expression.**
77b00 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 tree..**.** If t
77b10 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
77b20 20 61 6e 20 53 51 4c 20 6c 69 74 65 72 61 6c 20 an SQL literal
77b30 28 54 4b 5f 49 4e 54 45 47 45 52 2c 20 54 4b 5f (TK_INTEGER, TK_
77b40 46 4c 4f 41 54 2c 20 54 4b 5f 42 4c 4f 42 2c 20 FLOAT, TK_BLOB,
77b50 0a 2a 2a 20 6f 72 20 54 4b 5f 53 54 52 49 4e 47 .** or TK_STRING
77b60 29 2c 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b ), then Expr.tok
77b70 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 en contains the
77b80 74 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c 20 text of the SQL
77b90 6c 69 74 65 72 61 6c 2e 20 49 66 0a 2a 2a 20 74 literal. If.** t
77ba0 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
77bb0 20 61 20 76 61 72 69 61 62 6c 65 20 28 54 4b 5f a variable (TK_
77bc0 56 41 52 49 41 42 4c 45 29 2c 20 74 68 65 6e 20 VARIABLE), then
77bd0 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 Expr.token conta
77be0 69 6e 73 20 74 68 65 20 0a 2a 2a 20 76 61 72 69 ins the .** vari
77bf0 61 62 6c 65 20 6e 61 6d 65 2e 20 46 69 6e 61 6c able name. Final
77c00 6c 79 2c 20 69 66 20 74 68 65 20 65 78 70 72 65 ly, if the expre
77c10 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 ssion is an SQL
77c20 66 75 6e 63 74 69 6f 6e 20 28 54 4b 5f 46 55 4e function (TK_FUN
77c30 43 54 49 4f 4e 29 2c 0a 2a 2a 20 74 68 65 6e 20 CTION),.** then
77c40 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 Expr.token conta
77c50 69 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 ins the name of
77c60 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a the function..**
77c70 0a 2a 2a 20 45 78 70 72 2e 70 52 69 67 68 74 20 .** Expr.pRight
77c80 61 6e 64 20 45 78 70 72 2e 70 4c 65 66 74 20 61 and Expr.pLeft a
77c90 72 65 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 re the left and
77ca0 72 69 67 68 74 20 73 75 62 65 78 70 72 65 73 73 right subexpress
77cb0 69 6f 6e 73 20 6f 66 20 61 0a 2a 2a 20 62 69 6e ions of a.** bin
77cc0 61 72 79 20 6f 70 65 72 61 74 6f 72 2e 20 45 69 ary operator. Ei
77cd0 74 68 65 72 20 6f 72 20 62 6f 74 68 20 6d 61 79 ther or both may
77ce0 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 be NULL..**.**
77cf0 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 69 73 20 Expr.x.pList is
77d00 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 a list of argume
77d10 6e 74 73 20 69 66 20 74 68 65 20 65 78 70 72 65 nts if the expre
77d20 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 ssion is an SQL
77d30 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 61 20 43 function,.** a C
77d40 41 53 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6f ASE expression o
77d50 72 20 61 6e 20 49 4e 20 65 78 70 72 65 73 73 69 r an IN expressi
77d60 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 on of the form "
77d70 3c 6c 68 73 3e 20 49 4e 20 28 3c 79 3e 2c 20 3c <lhs> IN (<y>, <
77d80 7a 3e 2e 2e 2e 29 22 2e 0a 2a 2a 20 45 78 70 72 z>...)"..** Expr
77d90 2e 78 2e 70 53 65 6c 65 63 74 20 69 73 20 75 73 .x.pSelect is us
77da0 65 64 20 69 66 20 74 68 65 20 65 78 70 72 65 73 ed if the expres
77db0 73 69 6f 6e 20 69 73 20 61 20 73 75 62 2d 73 65 sion is a sub-se
77dc0 6c 65 63 74 20 6f 72 20 61 6e 20 65 78 70 72 65 lect or an expre
77dd0 73 73 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 ssion of.** the
77de0 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28 form "<lhs> IN (
77df0 53 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 20 49 66 SELECT ...)". If
77e00 20 74 68 65 20 45 50 5f 78 49 73 53 65 6c 65 63 the EP_xIsSelec
77e10 74 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 t bit is set in
77e20 74 68 65 0a 2a 2a 20 45 78 70 72 2e 66 6c 61 67 the.** Expr.flag
77e30 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 45 78 70 s mask, then Exp
77e40 72 2e 78 2e 70 53 65 6c 65 63 74 20 69 73 20 76 r.x.pSelect is v
77e50 61 6c 69 64 2e 20 4f 74 68 65 72 77 69 73 65 2c alid. Otherwise,
77e60 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 69 73 Expr.x.pList is
77e70 20 0a 2a 2a 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a .** valid..**.*
77e80 2a 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 * An expression
77e90 6f 66 20 74 68 65 20 66 6f 72 6d 20 49 44 20 6f of the form ID o
77ea0 72 20 49 44 2e 49 44 20 72 65 66 65 72 73 20 74 r ID.ID refers t
77eb0 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 o a column in a
77ec0 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f 72 20 73 75 table..** For su
77ed0 63 68 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 ch expressions,
77ee0 45 78 70 72 2e 6f 70 20 69 73 20 73 65 74 20 74 Expr.op is set t
77ef0 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 o TK_COLUMN and
77f00 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73 0a 2a Expr.iTable is.*
77f10 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20 63 75 * the integer cu
77f20 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 61 rsor number of a
77f30 20 56 44 42 45 20 63 75 72 73 6f 72 20 70 6f 69 VDBE cursor poi
77f40 6e 74 69 6e 67 20 74 6f 20 74 68 61 74 20 74 61 nting to that ta
77f50 62 6c 65 20 61 6e 64 0a 2a 2a 20 45 78 70 72 2e ble and.** Expr.
77f60 69 43 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 63 iColumn is the c
77f70 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 olumn number for
77f80 20 74 68 65 20 73 70 65 63 69 66 69 63 20 63 6f the specific co
77f90 6c 75 6d 6e 2e 20 20 49 66 20 74 68 65 0a 2a 2a lumn. If the.**
77fa0 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 75 expression is u
77fb0 73 65 64 20 61 73 20 61 20 72 65 73 75 6c 74 20 sed as a result
77fc0 69 6e 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 in an aggregate
77fd0 53 45 4c 45 43 54 2c 20 74 68 65 6e 20 74 68 65 SELECT, then the
77fe0 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 61 6c 73 .** value is als
77ff0 6f 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 o stored in the
78000 45 78 70 72 2e 69 41 67 67 20 63 6f 6c 75 6d 6e Expr.iAgg column
78010 20 69 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 in the aggregat
78020 65 20 73 6f 20 74 68 61 74 0a 2a 2a 20 69 74 20 e so that.** it
78030 63 61 6e 20 62 65 20 61 63 63 65 73 73 65 64 20 can be accessed
78040 61 66 74 65 72 20 61 6c 6c 20 61 67 67 72 65 67 after all aggreg
78050 61 74 65 73 20 61 72 65 20 63 6f 6d 70 75 74 65 ates are compute
78060 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
78070 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e expression is an
78080 20 75 6e 62 6f 75 6e 64 20 76 61 72 69 61 62 6c unbound variabl
78090 65 20 6d 61 72 6b 65 72 20 28 61 20 71 75 65 73 e marker (a ques
780a0 74 69 6f 6e 20 6d 61 72 6b 20 0a 2a 2a 20 63 68 tion mark .** ch
780b0 61 72 61 63 74 65 72 20 27 3f 27 20 69 6e 20 74 aracter '?' in t
780c0 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 29 he original SQL)
780d0 20 74 68 65 6e 20 74 68 65 20 45 78 70 72 2e 69 then the Expr.i
780e0 54 61 62 6c 65 20 68 6f 6c 64 73 20 74 68 65 20 Table holds the
780f0 69 6e 64 65 78 20 0a 2a 2a 20 6e 75 6d 62 65 72 index .** number
78100 20 66 6f 72 20 74 68 61 74 20 76 61 72 69 61 62 for that variab
78110 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 le..**.** If the
78120 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 expression is a
78130 20 73 75 62 71 75 65 72 79 20 74 68 65 6e 20 45 subquery then E
78140 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 68 6f 6c 64 xpr.iColumn hold
78150 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 s an integer.**
78160 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 register number
78170 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 containing the r
78180 65 73 75 6c 74 20 6f 66 20 74 68 65 20 73 75 62 esult of the sub
78190 71 75 65 72 79 2e 20 20 49 66 20 74 68 65 0a 2a query. If the.*
781a0 2a 20 73 75 62 71 75 65 72 79 20 67 69 76 65 73 * subquery gives
781b0 20 61 20 63 6f 6e 73 74 61 6e 74 20 72 65 73 75 a constant resu
781c0 6c 74 2c 20 74 68 65 6e 20 69 54 61 62 6c 65 20 lt, then iTable
781d0 69 73 20 2d 31 2e 20 20 49 66 20 74 68 65 20 73 is -1. If the s
781e0 75 62 71 75 65 72 79 0a 2a 2a 20 67 69 76 65 73 ubquery.** gives
781f0 20 61 20 64 69 66 66 65 72 65 6e 74 20 61 6e 73 a different ans
78200 77 65 72 20 61 74 20 64 69 66 66 65 72 65 6e 74 wer at different
78210 20 74 69 6d 65 73 20 64 75 72 69 6e 67 20 73 74 times during st
78220 61 74 65 6d 65 6e 74 20 70 72 6f 63 65 73 73 69 atement processi
78230 6e 67 0a 2a 2a 20 74 68 65 6e 20 69 54 61 62 6c ng.** then iTabl
78240 65 20 69 73 20 74 68 65 20 61 64 64 72 65 73 73 e is the address
78250 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e 65 of a subroutine
78260 20 74 68 61 74 20 63 6f 6d 70 75 74 65 73 20 74 that computes t
78270 68 65 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a he subquery..**.
78280 2a 2a 20 49 66 20 74 68 65 20 45 78 70 72 20 69 ** If the Expr i
78290 73 20 6f 66 20 74 79 70 65 20 4f 50 5f 43 6f 6c s of type OP_Col
782a0 75 6d 6e 2c 20 61 6e 64 20 74 68 65 20 74 61 62 umn, and the tab
782b0 6c 65 20 69 74 20 69 73 20 73 65 6c 65 63 74 69 le it is selecti
782c0 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69 73 20 61 20 ng from.** is a
782d0 64 69 73 6b 20 74 61 62 6c 65 20 6f 72 20 74 68 disk table or th
782e0 65 20 22 6f 6c 64 2e 2a 22 20 70 73 65 75 64 6f e "old.*" pseudo
782f0 2d 74 61 62 6c 65 2c 20 74 68 65 6e 20 70 54 61 -table, then pTa
78300 62 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a b points to the.
78310 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ** corresponding
78320 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f table definitio
78330 6e 2e 0a 2a 2a 0a 2a 2a 20 41 4c 4c 4f 43 41 54 n..**.** ALLOCAT
78340 49 4f 4e 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a ION NOTES:.**.**
78350 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 63 61 Expr objects ca
78360 6e 20 75 73 65 20 61 20 6c 6f 74 20 6f 66 20 6d n use a lot of m
78370 65 6d 6f 72 79 20 73 70 61 63 65 20 69 6e 20 64 emory space in d
78380 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 atabase schema.
78390 20 54 6f 0a 2a 2a 20 68 65 6c 70 20 72 65 64 75 To.** help redu
783a0 63 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 ce memory requir
783b0 65 6d 65 6e 74 73 2c 20 73 6f 6d 65 74 69 6d 65 ements, sometime
783c0 73 20 61 6e 20 45 78 70 72 20 6f 62 6a 65 63 74 s an Expr object
783d0 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 75 6e will be.** trun
783e0 63 61 74 65 64 2e 20 20 41 6e 64 20 74 6f 20 72 cated. And to r
783f0 65 64 75 63 65 20 74 68 65 20 6e 75 6d 62 65 72 educe the number
78400 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 of memory alloc
78410 61 74 69 6f 6e 73 2c 20 73 6f 6d 65 74 69 6d 65 ations, sometime
78420 73 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 s.** two or more
78430 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 77 69 Expr objects wi
78440 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 ll be stored in
78450 61 20 73 69 6e 67 6c 65 20 6d 65 6d 6f 72 79 20 a single memory
78460 61 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 allocation,.** t
78470 6f 67 65 74 68 65 72 20 77 69 74 68 20 45 78 70 ogether with Exp
78480 72 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 73 r.zToken strings
78490 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 ..**.** If the E
784a0 50 5f 52 65 64 75 63 65 64 20 61 6e 64 20 45 50 P_Reduced and EP
784b0 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 73 _TokenOnly flags
784c0 20 61 72 65 20 73 65 74 20 77 68 65 6e 0a 2a 2a are set when.**
784d0 20 61 6e 20 45 78 70 72 20 6f 62 6a 65 63 74 20 an Expr object
784e0 69 73 20 74 72 75 6e 63 61 74 65 64 2e 20 20 57 is truncated. W
784f0 68 65 6e 20 45 50 5f 52 65 64 75 63 65 64 20 69 hen EP_Reduced i
78500 73 20 73 65 74 2c 20 74 68 65 6e 20 61 6c 6c 0a s set, then all.
78510 2a 2a 20 74 68 65 20 63 68 69 6c 64 20 45 78 70 ** the child Exp
78520 72 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 r objects in the
78530 20 45 78 70 72 2e 70 4c 65 66 74 20 61 6e 64 20 Expr.pLeft and
78540 45 78 70 72 2e 70 52 69 67 68 74 20 73 75 62 74 Expr.pRight subt
78550 72 65 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 rees.** are cont
78560 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 ained within the
78570 20 73 61 6d 65 20 6d 65 6d 6f 72 79 20 61 6c 6c same memory all
78580 6f 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 65 2c 20 ocation. Note,
78590 68 6f 77 65 76 65 72 2c 20 74 68 61 74 0a 2a 2a however, that.**
785a0 20 74 68 65 20 73 75 62 74 72 65 65 73 20 69 6e the subtrees in
785b0 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 6f 72 Expr.x.pList or
785c0 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 Expr.x.pSelect
785d0 61 72 65 20 61 6c 77 61 79 73 20 73 65 70 61 72 are always separ
785e0 61 74 65 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 ately.** allocat
785f0 65 64 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f ed, regardless o
78600 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 f whether or not
78610 20 45 50 5f 52 65 64 75 63 65 64 20 69 73 20 73 EP_Reduced is s
78620 65 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 et..*/.struct Ex
78630 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b 20 20 20 pr {. u8 op;
78640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
78650 20 4f 70 65 72 61 74 69 6f 6e 20 70 65 72 66 6f Operation perfo
78660 72 6d 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64 rmed by this nod
78670 65 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 e */. char affi
78680 6e 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a nity; /*
78690 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 The affinity of
786a0 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 the column or 0
786b0 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e if not a column
786c0 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b */. u16 flags;
786d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
786e0 56 61 72 69 6f 75 73 20 66 6c 61 67 73 2e 20 20 Various flags.
786f0 45 50 5f 2a 20 53 65 65 20 62 65 6c 6f 77 20 2a EP_* See below *
78700 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 /. union {.
78710 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 char *zToken;
78720 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 /* Token
78730 76 61 6c 75 65 2e 20 5a 65 72 6f 20 74 65 72 6d value. Zero term
78740 69 6e 61 74 65 64 20 61 6e 64 20 64 65 71 75 6f inated and dequo
78750 74 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ted */. int i
78760 56 61 6c 75 65 3b 20 20 20 20 20 20 20 20 20 20 Value;
78770 20 20 2f 2a 20 4e 6f 6e 2d 6e 65 67 61 74 69 76 /* Non-negativ
78780 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 e integer value
78790 69 66 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 2a if EP_IntValue *
787a0 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 49 /. } u;.. /* I
787b0 66 20 74 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e f the EP_TokenOn
787c0 6c 79 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 ly flag is set i
787d0 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73 n the Expr.flags
787e0 20 6d 61 73 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20 mask, then no.
787f0 20 2a 2a 20 73 70 61 63 65 20 69 73 20 61 6c 6c ** space is all
78800 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 66 ocated for the f
78810 69 65 6c 64 73 20 62 65 6c 6f 77 20 74 68 69 73 ields below this
78820 20 70 6f 69 6e 74 2e 20 41 6e 20 61 74 74 65 6d point. An attem
78830 70 74 20 74 6f 0a 20 20 2a 2a 20 61 63 63 65 73 pt to. ** acces
78840 73 20 74 68 65 6d 20 77 69 6c 6c 20 72 65 73 75 s them will resu
78850 6c 74 20 69 6e 20 61 20 73 65 67 66 61 75 6c 74 lt in a segfault
78860 20 6f 72 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e or malfunction.
78870 20 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a . ************
78880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
788a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
788b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 45 78 *********/.. Ex
788c0 70 72 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 pr *pLeft;
788d0 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 /* Left sub
788e0 6e 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a node */. Expr *
788f0 70 52 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 pRight;
78900 20 2f 2a 20 52 69 67 68 74 20 73 75 62 6e 6f 64 /* Right subnod
78910 65 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 e */. union {.
78920 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 ExprList *pLi
78930 73 74 3b 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 st; /* Funct
78940 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 ion arguments or
78950 20 69 6e 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 in "<expr> IN (
78960 3c 65 78 70 72 2d 6c 69 73 74 29 22 20 2a 2f 0a <expr-list)" */.
78970 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c Select *pSel
78980 65 63 74 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 ect; /* Used
78990 20 66 6f 72 20 73 75 62 2d 73 65 6c 65 63 74 73 for sub-selects
789a0 20 61 6e 64 20 22 3c 65 78 70 72 3e 20 49 4e 20 and "<expr> IN
789b0 28 3c 73 65 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 (<select>)" */.
789c0 20 7d 20 78 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 } x;. CollSeq
789d0 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 2f *pColl; /
789e0 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 * The collation
789f0 74 79 70 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 type of the colu
78a00 6d 6e 20 6f 72 20 30 20 2a 2f 0a 0a 20 20 2f 2a mn or 0 */.. /*
78a10 20 49 66 20 74 68 65 20 45 50 5f 52 65 64 75 63 If the EP_Reduc
78a20 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 ed flag is set i
78a30 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73 n the Expr.flags
78a40 20 6d 61 73 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20 mask, then no.
78a50 20 2a 2a 20 73 70 61 63 65 20 69 73 20 61 6c 6c ** space is all
78a60 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 66 ocated for the f
78a70 69 65 6c 64 73 20 62 65 6c 6f 77 20 74 68 69 73 ields below this
78a80 20 70 6f 69 6e 74 2e 20 41 6e 20 61 74 74 65 6d point. An attem
78a90 70 74 20 74 6f 0a 20 20 2a 2a 20 61 63 63 65 73 pt to. ** acces
78aa0 73 20 74 68 65 6d 20 77 69 6c 6c 20 72 65 73 75 s them will resu
78ab0 6c 74 20 69 6e 20 61 20 73 65 67 66 61 75 6c 74 lt in a segfault
78ac0 20 6f 72 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e or malfunction.
78ad0 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a . *************
78ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78b10 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 53 ********/..#if S
78b20 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 QLITE_MAX_EXPR_D
78b30 45 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65 EPTH>0. int nHe
78b40 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 ight;
78b50 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 74 68 65 /* Height of the
78b60 20 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20 tree headed by
78b70 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e this node */.#en
78b80 64 69 66 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 dif. int iTable
78b90 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
78ba0 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 75 72 73 6f TK_COLUMN: curso
78bb0 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c r number of tabl
78bc0 65 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e e holding column
78bd0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
78be0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f ** TK_
78bf0 52 45 47 49 53 54 45 52 3a 20 72 65 67 69 73 74 REGISTER: regist
78c00 65 72 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 er number.
78c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78c20 20 20 20 2a 2a 20 54 4b 5f 54 52 49 47 47 45 52 ** TK_TRIGGER
78c30 3a 20 31 20 2d 3e 20 6e 65 77 2c 20 30 20 2d 3e : 1 -> new, 0 ->
78c40 20 6f 6c 64 20 2a 2f 0a 20 20 79 6e 56 61 72 20 old */. ynVar
78c50 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 iColumn;
78c60 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 /* TK_COLUMN: c
78c70 6f 6c 75 6d 6e 20 69 6e 64 65 78 2e 20 20 2d 31 olumn index. -1
78c80 20 66 6f 72 20 72 6f 77 69 64 2e 0a 20 20 20 20 for rowid..
78c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78ca0 20 20 20 20 20 2a 2a 20 54 4b 5f 56 41 52 49 41 ** TK_VARIA
78cb0 42 4c 45 3a 20 76 61 72 69 61 62 6c 65 20 6e 75 BLE: variable nu
78cc0 6d 62 65 72 20 28 61 6c 77 61 79 73 20 3e 3d 20 mber (always >=
78cd0 31 29 2e 20 2a 2f 0a 20 20 69 31 36 20 69 41 67 1). */. i16 iAg
78ce0 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
78cf0 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 /* Which entry i
78d00 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c n pAggInfo->aCol
78d10 5b 5d 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 [] or ->aFunc[]
78d20 2a 2f 0a 20 20 69 31 36 20 69 52 69 67 68 74 4a */. i16 iRightJ
78d30 6f 69 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 oinTable; /* I
78d40 66 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 f EP_FromJoin, t
78d50 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f he right table o
78d60 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 f the join */.
78d70 75 38 20 66 6c 61 67 73 32 3b 20 20 20 20 20 20 u8 flags2;
78d80 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 /* Second
78d90 20 73 65 74 20 6f 66 20 66 6c 61 67 73 2e 20 20 set of flags.
78da0 45 50 32 5f 2e 2e 2e 20 2a 2f 0a 20 20 75 38 20 EP2_... */. u8
78db0 6f 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 op2;
78dc0 20 20 20 20 2f 2a 20 54 4b 5f 52 45 47 49 53 54 /* TK_REGIST
78dd0 45 52 3a 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c ER: original val
78de0 75 65 20 6f 66 20 45 78 70 72 2e 6f 70 0a 20 20 ue of Expr.op.
78df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78e00 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f 43 4f 4c ** TK_COL
78e10 55 4d 4e 3a 20 74 68 65 20 76 61 6c 75 65 20 6f UMN: the value o
78e20 66 20 70 35 20 66 6f 72 20 4f 50 5f 43 6f 6c 75 f p5 for OP_Colu
78e30 6d 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 mn.
78e40 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 ** T
78e50 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 K_AGG_FUNCTION:
78e60 6e 65 73 74 69 6e 67 20 64 65 70 74 68 20 2a 2f nesting depth */
78e70 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 . AggInfo *pAgg
78e80 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65 Info; /* Use
78e90 64 20 62 79 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 d by TK_AGG_COLU
78ea0 4d 4e 20 61 6e 64 20 54 4b 5f 41 47 47 5f 46 55 MN and TK_AGG_FU
78eb0 4e 43 54 49 4f 4e 20 2a 2f 0a 20 20 54 61 62 6c NCTION */. Tabl
78ec0 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 e *pTab;
78ed0 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72 20 /* Table for
78ee0 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73 TK_COLUMN expres
78ef0 73 69 6f 6e 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a sions. */.};../*
78f00 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
78f10 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69 6e g are the meanin
78f20 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 gs of bits in th
78f30 65 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69 65 e Expr.flags fie
78f40 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 ld..*/.#define E
78f50 50 5f 46 72 6f 6d 4a 6f 69 6e 20 20 20 30 78 30 P_FromJoin 0x0
78f60 30 30 31 20 20 2f 2a 20 4f 72 69 67 69 6e 61 74 001 /* Originat
78f70 65 64 20 69 6e 20 4f 4e 20 6f 72 20 55 53 49 4e ed in ON or USIN
78f80 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f G clause of a jo
78f90 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 in */.#define EP
78fa0 5f 41 67 67 20 20 20 20 20 20 20 20 30 78 30 30 _Agg 0x00
78fb0 30 32 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20 02 /* Contains
78fc0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 61 67 67 72 one or more aggr
78fd0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 egate functions
78fe0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 52 65 */.#define EP_Re
78ff0 73 6f 6c 76 65 64 20 20 20 30 78 30 30 30 34 20 solved 0x0004
79000 20 2f 2a 20 49 44 73 20 68 61 76 65 20 62 65 65 /* IDs have bee
79010 6e 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 43 4f n resolved to CO
79020 4c 55 4d 4e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 LUMNs */.#define
79030 20 45 50 5f 45 72 72 6f 72 20 20 20 20 20 20 30 EP_Error 0
79040 78 30 30 30 38 20 20 2f 2a 20 45 78 70 72 65 73 x0008 /* Expres
79050 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 6f 6e sion contains on
79060 65 20 6f 72 20 6d 6f 72 65 20 65 72 72 6f 72 73 e or more errors
79070 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 */.#define EP_D
79080 69 73 74 69 6e 63 74 20 20 20 30 78 30 30 31 30 istinct 0x0010
79090 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66 /* Aggregate f
790a0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 44 49 53 unction with DIS
790b0 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 2a 2f TINCT keyword */
790c0 0a 23 64 65 66 69 6e 65 20 45 50 5f 56 61 72 53 .#define EP_VarS
790d0 65 6c 65 63 74 20 20 30 78 30 30 32 30 20 20 2f elect 0x0020 /
790e0 2a 20 70 53 65 6c 65 63 74 20 69 73 20 63 6f 72 * pSelect is cor
790f0 72 65 6c 61 74 65 64 2c 20 6e 6f 74 20 63 6f 6e related, not con
79100 73 74 61 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 stant */.#define
79110 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 20 30 EP_DblQuoted 0
79120 78 30 30 34 30 20 20 2f 2a 20 74 6f 6b 65 6e 2e x0040 /* token.
79130 7a 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 z was originally
79140 20 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a 23 64 65 in "..." */.#de
79150 66 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e fine EP_InfixFun
79160 63 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72 c 0x0080 /* Tr
79170 75 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 ue for an infix
79180 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 function: LIKE,
79190 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 GLOB, etc */.#de
791a0 66 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 fine EP_ExpColla
791b0 74 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f te 0x0100 /* Co
791c0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
791d0 20 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69 specified expli
791e0 63 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 citly */.#define
791f0 20 45 50 5f 46 69 78 65 64 44 65 73 74 20 20 30 EP_FixedDest 0
79200 78 30 32 30 30 20 20 2f 2a 20 52 65 73 75 6c 74 x0200 /* Result
79210 20 6e 65 65 64 65 64 20 69 6e 20 61 20 73 70 65 needed in a spe
79220 63 69 66 69 63 20 72 65 67 69 73 74 65 72 20 2a cific register *
79230 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 49 6e 74 /.#define EP_Int
79240 56 61 6c 75 65 20 20 20 30 78 30 34 30 30 20 20 Value 0x0400
79250 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 /* Integer value
79260 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e contained in u.
79270 69 56 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e iValue */.#defin
79280 65 20 45 50 5f 78 49 73 53 65 6c 65 63 74 20 20 e EP_xIsSelect
79290 30 78 30 38 30 30 20 20 2f 2a 20 78 2e 70 53 65 0x0800 /* x.pSe
792a0 6c 65 63 74 20 69 73 20 76 61 6c 69 64 20 28 6f lect is valid (o
792b0 74 68 65 72 77 69 73 65 20 78 2e 70 4c 69 73 74 therwise x.pList
792c0 20 69 73 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 is) */.#define
792d0 45 50 5f 48 69 6e 74 20 20 20 20 20 20 20 30 78 EP_Hint 0x
792e0 31 30 30 30 20 20 2f 2a 20 4e 6f 74 20 75 73 65 1000 /* Not use
792f0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f d */.#define EP_
79300 52 65 64 75 63 65 64 20 20 20 20 30 78 32 30 30 Reduced 0x200
79310 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75 63 0 /* Expr struc
79320 74 20 69 73 20 45 58 50 52 5f 52 45 44 55 43 45 t is EXPR_REDUCE
79330 44 53 49 5a 45 20 62 79 74 65 73 20 6f 6e 6c 79 DSIZE bytes only
79340 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 54 */.#define EP_T
79350 6f 6b 65 6e 4f 6e 6c 79 20 20 30 78 34 30 30 30 okenOnly 0x4000
79360 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75 63 74 /* Expr struct
79370 20 69 73 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e is EXPR_TOKENON
79380 4c 59 53 49 5a 45 20 62 79 74 65 73 20 6f 6e 6c LYSIZE bytes onl
79390 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f y */.#define EP_
793a0 53 74 61 74 69 63 20 20 20 20 20 30 78 38 30 30 Static 0x800
793b0 30 20 20 2f 2a 20 48 65 6c 64 20 69 6e 20 6d 65 0 /* Held in me
793c0 6d 6f 72 79 20 6e 6f 74 20 6f 62 74 61 69 6e 65 mory not obtaine
793d0 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 20 d from malloc()
793e0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f */../*.** The fo
793f0 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20 llowing are the
79400 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69 74 73 meanings of bits
79410 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 in the Expr.fla
79420 67 73 32 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 gs2 field..*/.#d
79430 65 66 69 6e 65 20 45 50 32 5f 4d 61 6c 6c 6f 63 efine EP2_Malloc
79440 65 64 54 6f 6b 65 6e 20 20 30 78 30 30 30 31 20 edToken 0x0001
79450 20 2f 2a 20 4e 65 65 64 20 74 6f 20 73 71 6c 69 /* Need to sqli
79460 74 65 33 44 62 46 72 65 65 28 29 20 45 78 70 72 te3DbFree() Expr
79470 2e 7a 54 6f 6b 65 6e 20 2a 2f 0a 23 64 65 66 69 .zToken */.#defi
79480 6e 65 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 ne EP2_Irreducib
79490 6c 65 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a le 0x0002 /*
794a0 20 43 61 6e 6e 6f 74 20 45 58 50 52 44 55 50 5f Cannot EXPRDUP_
794b0 52 45 44 55 43 45 20 74 68 69 73 20 45 78 70 72 REDUCE this Expr
794c0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 */../*.** The p
794d0 73 65 75 64 6f 2d 72 6f 75 74 69 6e 65 20 73 71 seudo-routine sq
794e0 6c 69 74 65 33 45 78 70 72 53 65 74 49 72 72 65 lite3ExprSetIrre
794f0 64 75 63 69 62 6c 65 20 73 65 74 73 20 74 68 65 ducible sets the
79500 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 6c 65 EP2_Irreducible
79510 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 61 6e 20 65 .** flag on an e
79520 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 xpression struct
79530 75 72 65 2e 20 20 54 68 69 73 20 66 6c 61 67 20 ure. This flag
79540 69 73 20 75 73 65 64 20 66 6f 72 20 56 56 26 41 is used for VV&A
79550 20 6f 6e 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 72 only. The.** r
79560 6f 75 74 69 6e 65 20 69 73 20 69 6d 70 6c 65 6d outine is implem
79570 65 6e 74 65 64 20 61 73 20 61 20 6d 61 63 72 6f ented as a macro
79580 20 74 68 61 74 20 6f 6e 6c 79 20 77 6f 72 6b 73 that only works
79590 20 77 68 65 6e 20 69 6e 20 64 65 62 75 67 67 69 when in debuggi
795a0 6e 67 20 6d 6f 64 65 2c 0a 2a 2a 20 73 6f 20 61 ng mode,.** so a
795b0 73 20 6e 6f 74 20 74 6f 20 62 75 72 64 65 6e 20 s not to burden
795c0 70 72 6f 64 75 63 74 69 6f 6e 20 63 6f 64 65 2e production code.
795d0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
795e0 45 5f 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 E_DEBUG.# define
795f0 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69 ExprSetIrreduci
79600 62 6c 65 28 58 29 20 20 28 58 29 2d 3e 66 6c 61 ble(X) (X)->fla
79610 67 73 32 20 7c 3d 20 45 50 32 5f 49 72 72 65 64 gs2 |= EP2_Irred
79620 75 63 69 62 6c 65 0a 23 65 6c 73 65 0a 23 20 64 ucible.#else.# d
79630 65 66 69 6e 65 20 45 78 70 72 53 65 74 49 72 72 efine ExprSetIrr
79640 65 64 75 63 69 62 6c 65 28 58 29 0a 23 65 6e 64 educible(X).#end
79650 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 if../*.** These
79660 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 macros can be us
79670 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c ed to test, set,
79680 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 or clear bits i
79690 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e 66 n the .** Expr.f
796a0 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 lags field..*/.#
796b0 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 72 define ExprHasPr
796c0 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 operty(E,P)
796d0 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29 (((E)->flags&(P)
796e0 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 )==(P)).#define
796f0 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 ExprHasAnyProper
79700 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d 3e ty(E,P) (((E)->
79710 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 flags&(P))!=0).#
79720 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 50 72 define ExprSetPr
79730 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 operty(E,P)
79740 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a (E)->flags|=(P).
79750 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65 61 #define ExprClea
79760 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 rProperty(E,P)
79770 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 (E)->flags&=~(P
79780 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 )../*.** Macros
79790 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
797a0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
797b0 20 72 65 71 75 69 72 65 64 20 62 79 20 61 20 6e required by a n
797c0 6f 72 6d 61 6c 20 45 78 70 72 20 0a 2a 2a 20 73 ormal Expr .** s
797d0 74 72 75 63 74 2c 20 61 6e 20 45 78 70 72 20 73 truct, an Expr s
797e0 74 72 75 63 74 20 77 69 74 68 20 74 68 65 20 45 truct with the E
797f0 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20 73 P_Reduced flag s
79800 65 74 20 69 6e 20 45 78 70 72 2e 66 6c 61 67 73 et in Expr.flags
79810 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 45 78 70 72 .** and an Expr
79820 20 73 74 72 75 63 74 20 77 69 74 68 20 74 68 65 struct with the
79830 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c EP_TokenOnly fl
79840 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 ag set..*/.#defi
79850 6e 65 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 ne EXPR_FULLSIZE
79860 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f sizeo
79870 66 28 45 78 70 72 29 20 20 20 20 20 20 20 20 20 f(Expr)
79880 20 20 2f 2a 20 46 75 6c 6c 20 73 69 7a 65 20 2a /* Full size *
79890 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 52 /.#define EXPR_R
798a0 45 44 55 43 45 44 53 49 5a 45 20 20 20 20 20 20 EDUCEDSIZE
798b0 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c offsetof(Expr,
798c0 69 54 61 62 6c 65 29 20 20 2f 2a 20 43 6f 6d 6d iTable) /* Comm
798d0 6f 6e 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 23 on features */.#
798e0 64 65 66 69 6e 65 20 45 58 50 52 5f 54 4f 4b 45 define EXPR_TOKE
798f0 4e 4f 4e 4c 59 53 49 5a 45 20 20 20 20 20 20 6f NONLYSIZE o
79900 66 66 73 65 74 6f 66 28 45 78 70 72 2c 70 4c 65 ffsetof(Expr,pLe
79910 66 74 29 20 20 20 2f 2a 20 46 65 77 65 72 20 66 ft) /* Fewer f
79920 65 61 74 75 72 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a eatures */../*.*
79930 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 * Flags passed t
79940 6f 20 74 68 65 20 73 71 6c 69 74 65 33 45 78 70 o the sqlite3Exp
79950 72 44 75 70 28 29 20 66 75 6e 63 74 69 6f 6e 2e rDup() function.
79960 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72 20 See the header
79970 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62 6f 76 comment .** abov
79980 65 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 e sqlite3ExprDup
79990 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a () for details..
799a0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 44 */.#define EXPRD
799b0 55 50 5f 52 45 44 55 43 45 20 20 20 20 20 20 20 UP_REDUCE
799c0 20 20 30 78 30 30 30 31 20 20 2f 2a 20 55 73 65 0x0001 /* Use
799d0 64 20 72 65 64 75 63 65 64 2d 73 69 7a 65 20 45 d reduced-size E
799e0 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0a 0a 2f 2a xpr nodes */../*
799f0 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 65 78 .** A list of ex
79a00 70 72 65 73 73 69 6f 6e 73 2e 20 20 45 61 63 68 pressions. Each
79a10 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 79 20 expression may
79a20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 61 76 65 20 optionally have
79a30 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 41 6e 20 65 a.** name. An e
79a40 78 70 72 2f 6e 61 6d 65 20 63 6f 6d 62 69 6e 61 xpr/name combina
79a50 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 tion can be used
79a60 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79 73 in several ways
79a70 2c 20 73 75 63 68 0a 2a 2a 20 61 73 20 74 68 65 , such.** as the
79a80 20 6c 69 73 74 20 6f 66 20 22 65 78 70 72 20 41 list of "expr A
79a90 53 20 49 44 22 20 66 69 65 6c 64 73 20 66 6f 6c S ID" fields fol
79aa0 6c 6f 77 69 6e 67 20 61 20 22 53 45 4c 45 43 54 lowing a "SELECT
79ab0 22 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 6c " or in the.** l
79ac0 69 73 74 20 6f 66 20 22 49 44 20 3d 20 65 78 70 ist of "ID = exp
79ad0 72 22 20 69 74 65 6d 73 20 69 6e 20 61 6e 20 55 r" items in an U
79ae0 50 44 41 54 45 2e 20 20 41 20 6c 69 73 74 20 6f PDATE. A list o
79af0 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 f expressions ca
79b00 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 65 n.** also be use
79b10 64 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e d as the argumen
79b20 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2c t to a function,
79b30 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 in which case t
79b40 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a 2a 20 66 69 he a.zName.** fi
79b50 65 6c 64 20 69 73 20 6e 6f 74 20 75 73 65 64 2e eld is not used.
79b60 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 72 4c .*/.struct ExprL
79b70 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78 70 ist {. int nExp
79b80 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f r; /
79b90 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 70 72 * Number of expr
79ba0 65 73 73 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6c essions on the l
79bb0 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 45 43 ist */. int iEC
79bc0 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 ursor;
79bd0 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20 61 /* VDBE Cursor a
79be0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
79bf0 68 69 73 20 45 78 70 72 4c 69 73 74 20 2a 2f 0a his ExprList */.
79c00 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
79c10 74 5f 69 74 65 6d 20 7b 20 2f 2a 20 46 6f 72 20 t_item { /* For
79c20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 each expression
79c30 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 in the list */.
79c40 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 Expr *pExpr;
79c50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
79c60 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 list of express
79c70 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72 ions */. char
79c80 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 *zName;
79c90 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f /* Token asso
79ca0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 ciated with this
79cb0 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 expression */.
79cc0 20 20 20 63 68 61 72 20 2a 7a 53 70 61 6e 3b 20 char *zSpan;
79cd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 /* Ori
79ce0 67 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 ginal text of th
79cf0 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a e expression */.
79d00 20 20 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 u8 sortOrder
79d10 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 ; /* 1
79d20 66 6f 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f for DESC or 0 fo
79d30 72 20 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 r ASC */. u8
79d40 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 done;
79d50 20 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f /* A flag to
79d60 20 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 indicate when p
79d70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e rocessing is fin
79d80 69 73 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 ished */. u16
79d90 20 69 4f 72 64 65 72 42 79 43 6f 6c 3b 20 20 20 iOrderByCol;
79da0 20 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 /* For ORDER
79db0 20 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 BY, column numb
79dc0 65 72 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 er in result set
79dd0 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 */. u16 iAli
79de0 61 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f as; /
79df0 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 * Index into Par
79e00 73 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 se.aAlias[] for
79e10 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b zName */. } *a;
79e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
79e30 20 20 2f 2a 20 41 6c 6c 6f 63 20 61 20 70 6f 77 /* Alloc a pow
79e40 65 72 20 6f 66 20 74 77 6f 20 67 72 65 61 74 65 er of two greate
79e50 72 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 6e 45 r or equal to nE
79e60 78 70 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a xpr */.};../*.**
79e70 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
79e80 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 this structure i
79e90 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61 s used by the pa
79ea0 72 73 65 72 20 74 6f 20 72 65 63 6f 72 64 20 62 rser to record b
79eb0 6f 74 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 oth.** the parse
79ec0 20 74 72 65 65 20 66 6f 72 20 61 6e 20 65 78 70 tree for an exp
79ed0 72 65 73 73 69 6f 6e 20 61 6e 64 20 74 68 65 20 ression and the
79ee0 73 70 61 6e 20 6f 66 20 69 6e 70 75 74 20 74 65 span of input te
79ef0 78 74 20 66 6f 72 20 61 6e 0a 2a 2a 20 65 78 70 xt for an.** exp
79f00 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 ression..*/.stru
79f10 63 74 20 45 78 70 72 53 70 61 6e 20 7b 0a 20 20 ct ExprSpan {.
79f20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 Expr *pExpr;
79f30 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 /* The exp
79f40 72 65 73 73 69 6f 6e 20 70 61 72 73 65 20 74 72 ression parse tr
79f50 65 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ee */. const ch
79f60 61 72 20 2a 7a 53 74 61 72 74 3b 20 20 20 2f 2a ar *zStart; /*
79f70 20 46 69 72 73 74 20 63 68 61 72 61 63 74 65 72 First character
79f80 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20 2a of input text *
79f90 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
79fa0 7a 45 6e 64 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 zEnd; /* One
79fb0 20 63 68 61 72 61 63 74 65 72 20 70 61 73 74 20 character past
79fc0 74 68 65 20 65 6e 64 20 6f 66 20 69 6e 70 75 74 the end of input
79fd0 20 74 65 78 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a text */.};../*.
79fe0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
79ff0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
7a000 20 63 61 6e 20 68 6f 6c 64 20 61 20 73 69 6d 70 can hold a simp
7a010 6c 65 20 6c 69 73 74 20 6f 66 20 69 64 65 6e 74 le list of ident
7a020 69 66 69 65 72 73 2c 0a 2a 2a 20 73 75 63 68 20 ifiers,.** such
7a030 61 73 20 74 68 65 20 6c 69 73 74 20 22 61 2c 62 as the list "a,b
7a040 2c 63 22 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f ,c" in the follo
7a050 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 3a wing statements:
7a060 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 49 4e 53 45 .**.** INSE
7a070 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29 RT INTO t(a,b,c)
7a080 20 56 41 4c 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 VALUES ...;.**
7a090 20 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 CREATE INDE
7a0a0 58 20 69 64 78 20 4f 4e 20 74 28 61 2c 62 2c 63 X idx ON t(a,b,c
7a0b0 29 3b 0a 2a 2a 20 20 20 20 20 20 43 52 45 41 54 );.** CREAT
7a0c0 45 20 54 52 49 47 47 45 52 20 74 72 69 67 20 42 E TRIGGER trig B
7a0d0 45 46 4f 52 45 20 55 50 44 41 54 45 20 4f 4e 20 EFORE UPDATE ON
7a0e0 74 28 61 2c 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a t(a,b,c) ...;.**
7a0f0 0a 2a 2a 20 54 68 65 20 49 64 4c 69 73 74 2e 61 .** The IdList.a
7a100 2e 69 64 78 20 66 69 65 6c 64 20 69 73 20 75 73 .idx field is us
7a110 65 64 20 77 68 65 6e 20 74 68 65 20 49 64 4c 69 ed when the IdLi
7a120 73 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 st represents th
7a130 65 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c e list of.** col
7a140 75 6d 6e 20 6e 61 6d 65 73 20 61 66 74 65 72 20 umn names after
7a150 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 a table name in
7a160 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d an INSERT statem
7a170 65 6e 74 2e 20 20 49 6e 20 74 68 65 20 73 74 61 ent. In the sta
7a180 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 tement.**.**
7a190 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 28 61 INSERT INTO t(a
7a1a0 2c 62 2c 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 ,b,c) ....**.**
7a1b0 49 66 20 22 61 22 20 69 73 20 74 68 65 20 6b 2d If "a" is the k-
7a1c0 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 th column of tab
7a1d0 6c 65 20 22 74 22 2c 20 74 68 65 6e 20 49 64 4c le "t", then IdL
7a1e0 69 73 74 2e 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e ist.a[0].idx==k.
7a1f0 0a 2a 2f 0a 73 74 72 75 63 74 20 49 64 4c 69 73 .*/.struct IdLis
7a200 74 20 7b 0a 20 20 73 74 72 75 63 74 20 49 64 4c t {. struct IdL
7a210 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 63 ist_item {. c
7a220 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
7a230 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
7a240 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 20 20 identifier */.
7a250 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 int idx;
7a260 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 /* Index in
7a270 73 6f 6d 65 20 54 61 62 6c 65 2e 61 43 6f 6c 5b some Table.aCol[
7a280 5d 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 ] of a column na
7a290 6d 65 64 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d med zName */. }
7a2a0 20 2a 61 3b 0a 20 20 69 6e 74 20 6e 49 64 3b 20 *a;. int nId;
7a2b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
7a2c0 72 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72 73 r of identifiers
7a2d0 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a on the list */.
7a2e0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 };../*.** The bi
7a2f0 74 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 tmask datatype d
7a300 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 efined below is
7a310 75 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 used for various
7a320 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a optimizations..
7a330 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 **.** Changing t
7a340 68 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 his from a 64-bi
7a350 74 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 t to a 32-bit ty
7a360 70 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 pe limits the nu
7a370 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 mber of.** table
7a380 73 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 s in a join to 3
7a390 32 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 2 instead of 64.
7a3a0 20 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 But it also re
7a3b0 64 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a duces the size.*
7a3c0 2a 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 * of the library
7a3d0 20 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e by 738 bytes on
7a3e0 20 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 ix86..*/.typede
7a3f0 66 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a f u64 Bitmask;..
7a400 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 /*.** The number
7a410 20 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 of bits in a Bi
7a420 74 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 tmask. "BMS" me
7a430 61 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a ans "BitMask Siz
7a440 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 e"..*/.#define B
7a450 4d 53 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f MS ((int)(sizeo
7a460 66 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a f(Bitmask)*8))..
7a470 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
7a480 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65 ing structure de
7a490 73 63 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d scribes the FROM
7a4a0 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c clause of a SEL
7a4b0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a ECT statement..*
7a4c0 2a 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 * Each table or
7a4d0 73 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 subquery in the
7a4e0 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 FROM clause is a
7a4f0 20 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e separate elemen
7a500 74 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c t of.** the SrcL
7a510 69 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a ist.a[] array..*
7a520 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 *.** With the ad
7a530 64 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 dition of multip
7a540 6c 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 le database supp
7a550 6f 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ort, the followi
7a560 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 ng structure.**
7a570 63 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 can also be used
7a580 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 to describe a p
7a590 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 articular table
7a5a0 73 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c such as the tabl
7a5b0 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 e that.** is mod
7a5c0 69 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 ified by an INSE
7a5d0 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 RT, DELETE, or U
7a5e0 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e PDATE statement.
7a5f0 20 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 In standard SQ
7a600 4c 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 L,.** such a tab
7a610 6c 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d le must be a sim
7a620 70 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 ple name: ID. B
7a630 75 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 ut in SQLite, th
7a640 65 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e e table can.** n
7a650 6f 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 ow be identified
7a660 20 62 79 20 61 20 64 61 74 61 62 61 73 65 20 6e by a database n
7a670 61 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e ame, a dot, then
7a680 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a the table name:
7a690 20 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 ID.ID..**.** Th
7a6a0 65 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 e jointype start
7a6b0 73 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 s out showing th
7a6c0 65 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 e join type betw
7a6d0 65 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 een the current
7a6e0 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 table.** and the
7a6f0 20 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 next table on t
7a700 68 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 he list. The pa
7a710 72 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 rser builds the
7a720 6c 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a list this way..*
7a730 2a 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 * But sqlite3Src
7a740 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 ListShiftJoinTyp
7a750 65 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 e() later shifts
7a760 20 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 the jointypes s
7a770 6f 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a o that each.** j
7a780 6f 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 ointype expresse
7a790 73 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 s the join betwe
7a7a0 65 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 en the table and
7a7b0 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 the previous ta
7a7c0 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 ble..**.** In th
7a7d0 65 20 63 6f 6c 55 73 65 64 20 66 69 65 6c 64 2c e colUsed field,
7a7e0 20 74 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20 the high-order
7a7f0 62 69 74 20 28 62 69 74 20 36 33 29 20 69 73 20 bit (bit 63) is
7a800 73 65 74 20 69 66 20 74 68 65 20 74 61 62 6c 65 set if the table
7a810 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 .** contains mor
7a820 65 20 74 68 61 6e 20 36 33 20 63 6f 6c 75 6d 6e e than 63 column
7a830 73 20 61 6e 64 20 74 68 65 20 36 34 2d 74 68 20 s and the 64-th
7a840 6f 72 20 6c 61 74 65 72 20 63 6f 6c 75 6d 6e 20 or later column
7a850 69 73 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 is used..*/.stru
7a860 63 74 20 53 72 63 4c 69 73 74 20 7b 0a 20 20 69 ct SrcList {. i
7a870 31 36 20 6e 53 72 63 3b 20 20 20 20 20 20 20 20 16 nSrc;
7a880 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 /* Number of tab
7a890 6c 65 73 20 6f 72 20 73 75 62 71 75 65 72 69 65 les or subquerie
7a8a0 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c s in the FROM cl
7a8b0 61 75 73 65 20 2a 2f 0a 20 20 69 31 36 20 6e 41 ause */. i16 nA
7a8c0 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75 lloc; /* Nu
7a8d0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
7a8e0 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 5b 5d allocated in a[]
7a8f0 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 73 74 72 75 below */. stru
7a900 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
7a910 7b 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53 {. Schema *pS
7a920 63 68 65 6d 61 3b 20 20 2f 2a 20 53 63 68 65 6d chema; /* Schem
7a930 61 20 74 6f 20 77 68 69 63 68 20 74 68 69 73 20 a to which this
7a940 69 74 65 6d 20 69 73 20 66 69 78 65 64 20 2a 2f item is fixed */
7a950 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 61 74 61 . char *zData
7a960 62 61 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f base; /* Name o
7a970 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 f database holdi
7a980 6e 67 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f ng this table */
7a990 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 . char *zName
7a9a0 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f ; /* Name o
7a9b0 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 f the table */.
7a9c0 20 20 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 3b char *zAlias;
7a9d0 20 20 20 20 20 2f 2a 20 54 68 65 20 22 42 22 20 /* The "B"
7a9e0 70 61 72 74 20 6f 66 20 61 20 22 41 20 41 53 20 part of a "A AS
7a9f0 42 22 20 70 68 72 61 73 65 2e 20 20 7a 4e 61 6d B" phrase. zNam
7aa00 65 20 69 73 20 74 68 65 20 22 41 22 20 2a 2f 0a e is the "A" */.
7aa10 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b Table *pTab;
7aa20 20 20 20 20 20 20 2f 2a 20 41 6e 20 53 51 4c 20 /* An SQL
7aa30 74 61 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 table correspond
7aa40 69 6e 67 20 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a ing to zName */.
7aa50 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c Select *pSel
7aa60 65 63 74 3b 20 20 2f 2a 20 41 20 53 45 4c 45 43 ect; /* A SELEC
7aa70 54 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64 T statement used
7aa80 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 in place of a t
7aa90 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 able name */.
7aaa0 20 69 6e 74 20 61 64 64 72 46 69 6c 6c 53 75 62 int addrFillSub
7aab0 3b 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 ; /* Address of
7aac0 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 6d subroutine to m
7aad0 61 6e 69 66 65 73 74 20 61 20 73 75 62 71 75 65 anifest a subque
7aae0 72 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 ry */. int re
7aaf0 67 52 65 74 75 72 6e 3b 20 20 20 20 2f 2a 20 52 gReturn; /* R
7ab00 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 egister holding
7ab10 72 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 6f return address o
7ab20 66 20 61 64 64 72 46 69 6c 6c 53 75 62 20 2a 2f f addrFillSub */
7ab30 0a 20 20 20 20 75 38 20 6a 6f 69 6e 74 79 70 65 . u8 jointype
7ab40 3b 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f ; /* Type o
7ab50 66 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e 20 74 f join between t
7ab60 68 69 73 20 61 62 6c 65 20 61 6e 64 20 74 68 65 his able and the
7ab70 20 70 72 65 76 69 6f 75 73 20 2a 2f 0a 20 20 20 previous */.
7ab80 20 75 6e 73 69 67 6e 65 64 20 6e 6f 74 49 6e 64 unsigned notInd
7ab90 65 78 65 64 20 3a 31 3b 20 20 20 20 2f 2a 20 54 exed :1; /* T
7aba0 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20 rue if there is
7abb0 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c a NOT INDEXED cl
7abc0 61 75 73 65 20 2a 2f 0a 20 20 20 20 75 6e 73 69 ause */. unsi
7abd0 67 6e 65 64 20 69 73 43 6f 72 72 65 6c 61 74 65 gned isCorrelate
7abe0 64 20 3a 31 3b 20 20 2f 2a 20 54 72 75 65 20 69 d :1; /* True i
7abf0 66 20 73 75 62 2d 71 75 65 72 79 20 69 73 20 63 f sub-query is c
7ac00 6f 72 72 65 6c 61 74 65 64 20 2a 2f 0a 20 20 20 orrelated */.
7ac10 20 75 6e 73 69 67 6e 65 64 20 76 69 61 43 6f 72 unsigned viaCor
7ac20 6f 75 74 69 6e 65 20 3a 31 3b 20 20 2f 2a 20 49 outine :1; /* I
7ac30 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 mplemented as a
7ac40 63 6f 2d 72 6f 75 74 69 6e 65 20 2a 2f 0a 23 69 co-routine */.#i
7ac50 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
7ac60 54 5f 45 58 50 4c 41 49 4e 0a 20 20 20 20 75 38 T_EXPLAIN. u8
7ac70 20 69 53 65 6c 65 63 74 49 64 3b 20 20 20 20 20 iSelectId;
7ac80 2f 2a 20 49 66 20 70 53 65 6c 65 63 74 21 3d 30 /* If pSelect!=0
7ac90 2c 20 74 68 65 20 69 64 20 6f 66 20 74 68 65 20 , the id of the
7aca0 73 75 62 2d 73 65 6c 65 63 74 20 69 6e 20 45 51 sub-select in EQ
7acb0 50 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 20 20 P */.#endif.
7acc0 69 6e 74 20 69 43 75 72 73 6f 72 3b 20 20 20 20 int iCursor;
7acd0 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 /* The VDBE cu
7ace0 72 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 64 rsor number used
7acf0 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73 20 to access this
7ad00 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70 table */. Exp
7ad10 72 20 2a 70 4f 6e 3b 20 20 20 20 20 20 20 20 2f r *pOn; /
7ad20 2a 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 * The ON clause
7ad30 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 of a join */.
7ad40 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b IdList *pUsing;
7ad50 20 20 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 /* The USING
7ad60 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e clause of a join
7ad70 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 */. Bitmask
7ad80 63 6f 6c 55 73 65 64 3b 20 20 2f 2a 20 42 69 74 colUsed; /* Bit
7ad90 20 4e 20 28 31 3c 3c 4e 29 20 73 65 74 20 69 66 N (1<<N) set if
7ada0 20 63 6f 6c 75 6d 6e 20 4e 20 6f 66 20 70 54 61 column N of pTa
7adb0 62 20 69 73 20 75 73 65 64 20 2a 2f 0a 20 20 20 b is used */.
7adc0 20 63 68 61 72 20 2a 7a 49 6e 64 65 78 3b 20 20 char *zIndex;
7add0 20 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 /* Identifier
7ade0 20 66 72 6f 6d 20 22 49 4e 44 45 58 45 44 20 42 from "INDEXED B
7adf0 59 20 3c 7a 49 6e 64 65 78 3e 22 20 63 6c 61 75 Y <zIndex>" clau
7ae00 73 65 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 se */. Index
7ae10 2a 70 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 49 *pIndex; /* I
7ae20 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 63 ndex structure c
7ae30 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
7ae40 7a 49 6e 64 65 78 2c 20 69 66 20 61 6e 79 20 2a zIndex, if any *
7ae50 2f 0a 20 20 7d 20 61 5b 31 5d 3b 20 20 20 20 20 /. } a[1];
7ae60 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 /* One e
7ae70 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 69 64 ntry for each id
7ae80 65 6e 74 69 66 69 65 72 20 6f 6e 20 74 68 65 20 entifier on the
7ae90 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a list */.};../*.*
7aea0 2a 20 50 65 72 6d 69 74 74 65 64 20 76 61 6c 75 * Permitted valu
7aeb0 65 73 20 6f 66 20 74 68 65 20 53 72 63 4c 69 73 es of the SrcLis
7aec0 74 2e 61 2e 6a 6f 69 6e 74 79 70 65 20 66 69 65 t.a.jointype fie
7aed0 6c 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 ld.*/.#define JT
7aee0 5f 49 4e 4e 45 52 20 20 20 20 20 30 78 30 30 30 _INNER 0x000
7aef0 31 20 20 20 20 2f 2a 20 41 6e 79 20 6b 69 6e 64 1 /* Any kind
7af00 20 6f 66 20 69 6e 6e 65 72 20 6f 72 20 63 72 6f of inner or cro
7af10 73 73 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 ss join */.#defi
7af20 6e 65 20 4a 54 5f 43 52 4f 53 53 20 20 20 20 20 ne JT_CROSS
7af30 30 78 30 30 30 32 20 20 20 20 2f 2a 20 45 78 70 0x0002 /* Exp
7af40 6c 69 63 69 74 20 75 73 65 20 6f 66 20 74 68 65 licit use of the
7af50 20 43 52 4f 53 53 20 6b 65 79 77 6f 72 64 20 2a CROSS keyword *
7af60 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4e 41 54 /.#define JT_NAT
7af70 55 52 41 4c 20 20 20 30 78 30 30 30 34 20 20 20 URAL 0x0004
7af80 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 22 /* True for a "
7af90 6e 61 74 75 72 61 6c 22 20 6a 6f 69 6e 20 2a 2f natural" join */
7afa0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4c 45 46 54 .#define JT_LEFT
7afb0 20 20 20 20 20 20 30 78 30 30 30 38 20 20 20 20 0x0008
7afc0 2f 2a 20 4c 65 66 74 20 6f 75 74 65 72 20 6a 6f /* Left outer jo
7afd0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 in */.#define JT
7afe0 5f 52 49 47 48 54 20 20 20 20 20 30 78 30 30 31 _RIGHT 0x001
7aff0 30 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 75 0 /* Right ou
7b000 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 ter join */.#def
7b010 69 6e 65 20 4a 54 5f 4f 55 54 45 52 20 20 20 20 ine JT_OUTER
7b020 20 30 78 30 30 32 30 20 20 20 20 2f 2a 20 54 68 0x0020 /* Th
7b030 65 20 22 4f 55 54 45 52 22 20 6b 65 79 77 6f 72 e "OUTER" keywor
7b040 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a d is present */.
7b050 23 64 65 66 69 6e 65 20 4a 54 5f 45 52 52 4f 52 #define JT_ERROR
7b060 20 20 20 20 20 30 78 30 30 34 30 20 20 20 20 2f 0x0040 /
7b070 2a 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 * unknown or uns
7b080 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 upported join ty
7b090 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 pe */.../*.** A
7b0a0 57 68 65 72 65 50 6c 61 6e 20 6f 62 6a 65 63 74 WherePlan object
7b0b0 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 holds informati
7b0c0 6f 6e 20 74 68 61 74 20 64 65 73 63 72 69 62 65 on that describe
7b0d0 73 20 61 20 6c 6f 6f 6b 75 70 0a 2a 2a 20 73 74 s a lookup.** st
7b0e0 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 rategy..**.** Th
7b0f0 69 73 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 74 is object is int
7b100 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 ended to be opaq
7b110 75 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 ue outside of th
7b120 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 e where.c module
7b130 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6e 63 6c 75 ..** It is inclu
7b140 64 65 64 20 68 65 72 65 20 6f 6e 6c 79 20 73 6f ded here only so
7b150 20 74 68 61 74 20 74 68 61 74 20 63 6f 6d 70 69 that that compi
7b160 6c 65 72 20 77 69 6c 6c 20 6b 6e 6f 77 20 68 6f ler will know ho
7b170 77 20 62 69 67 20 69 74 0a 2a 2a 20 69 73 2e 20 w big it.** is.
7b180 20 4e 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 65 None of the fie
7b190 6c 64 73 20 69 6e 20 74 68 69 73 20 6f 62 6a 65 lds in this obje
7b1a0 63 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 ct should be use
7b1b0 64 20 6f 75 74 73 69 64 65 20 6f 66 0a 2a 2a 20 d outside of.**
7b1c0 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 the where.c modu
7b1d0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e le..**.** Within
7b1e0 20 74 68 65 20 75 6e 69 6f 6e 2c 20 70 49 64 78 the union, pIdx
7b1f0 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 is only used wh
7b200 65 6e 20 77 73 46 6c 61 67 73 26 57 48 45 52 45 en wsFlags&WHERE
7b210 5f 49 4e 44 45 58 45 44 20 69 73 20 74 72 75 65 _INDEXED is true
7b220 2e 0a 2a 2a 20 70 54 65 72 6d 20 69 73 20 6f 6e ..** pTerm is on
7b230 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 ly used when wsF
7b240 6c 61 67 73 26 57 48 45 52 45 5f 4d 55 4c 54 49 lags&WHERE_MULTI
7b250 5f 4f 52 20 69 73 20 74 72 75 65 2e 20 20 41 6e _OR is true. An
7b260 64 20 70 56 74 61 62 49 64 78 0a 2a 2a 20 69 73 d pVtabIdx.** is
7b270 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 only used when
7b280 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 56 49 wsFlags&WHERE_VI
7b290 52 54 55 41 4c 54 41 42 4c 45 20 69 73 20 74 72 RTUALTABLE is tr
7b2a0 75 65 2e 20 20 49 74 20 69 73 20 6e 65 76 65 72 ue. It is never
7b2b0 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 the.** case tha
7b2c0 74 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 t more than one
7b2d0 6f 66 20 74 68 65 73 65 20 63 6f 6e 64 69 74 69 of these conditi
7b2e0 6f 6e 73 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a ons is true..*/.
7b2f0 73 74 72 75 63 74 20 57 68 65 72 65 50 6c 61 6e struct WherePlan
7b300 20 7b 0a 20 20 75 33 32 20 77 73 46 6c 61 67 73 {. u32 wsFlags
7b310 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
7b320 20 20 20 20 2f 2a 20 57 48 45 52 45 5f 2a 20 66 /* WHERE_* f
7b330 6c 61 67 73 20 74 68 61 74 20 64 65 73 63 72 69 lags that descri
7b340 62 65 20 74 68 65 20 73 74 72 61 74 65 67 79 20 be the strategy
7b350 2a 2f 0a 20 20 75 31 36 20 6e 45 71 3b 20 20 20 */. u16 nEq;
7b360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7b370 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
7b380 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 == constraints
7b390 2a 2f 0a 20 20 75 31 36 20 6e 4f 42 53 61 74 3b */. u16 nOBSat;
7b3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7b3b0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
7b3c0 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 ORDER BY terms
7b3d0 73 61 74 69 73 66 69 65 64 20 2a 2f 0a 20 20 64 satisfied */. d
7b3e0 6f 75 62 6c 65 20 6e 52 6f 77 3b 20 20 20 20 20 ouble nRow;
7b3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
7b400 20 45 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65 Estimated numbe
7b410 72 20 6f 66 20 72 6f 77 73 20 28 66 6f 72 20 45 r of rows (for E
7b420 51 50 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b QP) */. union {
7b430 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 . Index *pIdx
7b440 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
7b450 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 77 68 65 /* Index whe
7b460 6e 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 20 n WHERE_INDEXED
7b470 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 20 20 73 is true */. s
7b480 74 72 75 63 74 20 57 68 65 72 65 54 65 72 6d 20 truct WhereTerm
7b490 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 2f 2a *pTerm; /*
7b4a0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 WHERE clause te
7b4b0 72 6d 20 66 6f 72 20 4f 52 2d 73 65 61 72 63 68 rm for OR-search
7b4c0 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f */. sqlite3_
7b4d0 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56 74 61 index_info *pVta
7b4e0 62 49 64 78 3b 20 20 2f 2a 20 56 69 72 74 75 61 bIdx; /* Virtua
7b4f0 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 20 74 6f l table index to
7b500 20 75 73 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d use */. } u;.}
7b510 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 ;../*.** For eac
7b520 68 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 69 6e h nested loop in
7b530 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 a WHERE clause
7b540 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
7b550 74 68 65 20 57 68 65 72 65 49 6e 66 6f 0a 2a 2a the WhereInfo.**
7b560 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 structure conta
7b570 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 ins a single ins
7b580 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 tance of this st
7b590 72 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 73 ructure. This s
7b5a0 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 69 tructure.** is i
7b5b0 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 70 72 ntended to be pr
7b5c0 69 76 61 74 65 20 74 6f 20 74 68 65 20 77 68 65 ivate to the whe
7b5d0 72 65 2e 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20 re.c module and
7b5e0 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a should not be.**
7b5f0 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 access or modif
7b600 69 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 ied by other mod
7b610 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ules..**.** The
7b620 70 49 64 78 49 6e 66 6f 20 66 69 65 6c 64 20 69 pIdxInfo field i
7b630 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 70 s used to help p
7b640 69 63 6b 20 74 68 65 20 62 65 73 74 20 69 6e 64 ick the best ind
7b650 65 78 20 6f 6e 20 61 0a 2a 2a 20 76 69 72 74 75 ex on a.** virtu
7b660 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 70 al table. The p
7b670 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 IdxInfo pointer
7b680 63 6f 6e 74 61 69 6e 73 20 69 6e 64 65 78 69 6e contains indexin
7b690 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e g.** information
7b6a0 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 for the i-th ta
7b6b0 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 ble in the FROM
7b6c0 63 6c 61 75 73 65 20 62 65 66 6f 72 65 20 72 65 clause before re
7b6d0 6f 72 64 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c ordering..** All
7b6e0 20 74 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f the pIdxInfo po
7b6f0 69 6e 74 65 72 73 20 61 72 65 20 66 72 65 65 64 inters are freed
7b700 20 62 79 20 77 68 65 72 65 49 6e 66 6f 46 72 65 by whereInfoFre
7b710 65 28 29 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a e() in where.c..
7b720 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 69 6e 66 ** All other inf
7b730 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 ormation in the
7b740 69 2d 74 68 20 57 68 65 72 65 4c 65 76 65 6c 20 i-th WhereLevel
7b750 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 69 object for the i
7b760 2d 74 68 20 74 61 62 6c 65 0a 2a 2a 20 61 66 74 -th table.** aft
7b770 65 72 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f er FROM clause o
7b780 72 64 65 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 rdering..*/.stru
7b790 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a ct WhereLevel {.
7b7a0 20 20 57 68 65 72 65 50 6c 61 6e 20 70 6c 61 6e WherePlan plan
7b7b0 3b 20 20 20 20 20 20 20 2f 2a 20 71 75 65 72 79 ; /* query
7b7c0 20 70 6c 61 6e 20 66 6f 72 20 74 68 69 73 20 65 plan for this e
7b7d0 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 46 52 lement of the FR
7b7e0 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 OM clause */. i
7b7f0 6e 74 20 69 4c 65 66 74 4a 6f 69 6e 3b 20 20 20 nt iLeftJoin;
7b800 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 /* Memory c
7b810 65 6c 6c 20 75 73 65 64 20 74 6f 20 69 6d 70 6c ell used to impl
7b820 65 6d 65 6e 74 20 4c 45 46 54 20 4f 55 54 45 52 ement LEFT OUTER
7b830 20 4a 4f 49 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 JOIN */. int i
7b840 54 61 62 43 75 72 3b 20 20 20 20 20 20 20 20 20 TabCur;
7b850 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 /* The VDBE cur
7b860 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 sor used to acce
7b870 73 73 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a ss the table */.
7b880 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 int iIdxCur;
7b890 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
7b8a0 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 DBE cursor used
7b8b0 74 6f 20 61 63 63 65 73 73 20 70 49 64 78 20 2a to access pIdx *
7b8c0 2f 0a 20 20 69 6e 74 20 61 64 64 72 42 72 6b 3b /. int addrBrk;
7b8d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d /* Jum
7b8e0 70 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 p here to break
7b8f0 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 out of the loop
7b900 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4e 78 74 */. int addrNxt
7b910 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 ; /* Ju
7b920 6d 70 20 68 65 72 65 20 74 6f 20 73 74 61 72 74 mp here to start
7b930 20 74 68 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d the next IN com
7b940 62 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e bination */. in
7b950 74 20 61 64 64 72 43 6f 6e 74 3b 20 20 20 20 20 t addrCont;
7b960 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 /* Jump here
7b970 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 to continue wit
7b980 68 20 74 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20 h the next loop
7b990 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61 cycle */. int a
7b9a0 64 64 72 46 69 72 73 74 3b 20 20 20 20 20 20 20 ddrFirst;
7b9b0 20 2f 2a 20 46 69 72 73 74 20 69 6e 73 74 72 75 /* First instru
7b9c0 63 74 69 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f ction of interio
7b9d0 72 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f r of the loop */
7b9e0 0a 20 20 75 38 20 69 46 72 6f 6d 3b 20 20 20 20 . u8 iFrom;
7b9f0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 /* Whic
7ba00 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 46 h entry in the F
7ba10 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 ROM clause */.
7ba20 75 38 20 6f 70 2c 20 70 35 3b 20 20 20 20 20 20 u8 op, p5;
7ba30 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 /* Opcode
7ba40 61 6e 64 20 50 35 20 6f 66 20 74 68 65 20 6f 70 and P5 of the op
7ba50 63 6f 64 65 20 74 68 61 74 20 65 6e 64 73 20 74 code that ends t
7ba60 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 he loop */. int
7ba70 20 70 31 2c 20 70 32 3b 20 20 20 20 20 20 20 20 p1, p2;
7ba80 20 20 20 2f 2a 20 4f 70 65 72 61 6e 64 73 20 6f /* Operands o
7ba90 66 20 74 68 65 20 6f 70 63 6f 64 65 20 75 73 65 f the opcode use
7baa0 64 20 74 6f 20 65 6e 64 73 20 74 68 65 20 6c 6f d to ends the lo
7bab0 6f 70 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 op */. union {
7bac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
7bad0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 Information tha
7bae0 74 20 64 65 70 65 6e 64 73 20 6f 6e 20 70 6c 61 t depends on pla
7baf0 6e 2e 77 73 46 6c 61 67 73 20 2a 2f 0a 20 20 20 n.wsFlags */.
7bb00 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20 20 struct {.
7bb10 69 6e 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 int nIn;
7bb20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
7bb30 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 49 of entries in aI
7bb40 6e 4c 6f 6f 70 5b 5d 20 2a 2f 0a 20 20 20 20 20 nLoop[] */.
7bb50 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 7b struct InLoop {
7bb60 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 75 . int iCu
7bb70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
7bb80 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 /* The VDBE curs
7bb90 6f 72 20 75 73 65 64 20 62 79 20 74 68 69 73 20 or used by this
7bba0 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 IN operator */.
7bbb0 20 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 49 int addrI
7bbc0 6e 54 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a nTop; /*
7bbd0 20 54 6f 70 20 6f 66 20 74 68 65 20 49 4e 20 6c Top of the IN l
7bbe0 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20 7d 20 2a oop */. } *
7bbf0 61 49 6e 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 aInLoop;
7bc00 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f /* Informatio
7bc10 6e 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 n about each nes
7bc20 74 65 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 ted IN operator
7bc30 2a 2f 0a 20 20 20 20 7d 20 69 6e 3b 20 20 20 20 */. } in;
7bc40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
7bc50 55 73 65 64 20 77 68 65 6e 20 70 6c 61 6e 2e 77 Used when plan.w
7bc60 73 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 5f sFlags&WHERE_IN_
7bc70 41 42 4c 45 20 2a 2f 0a 20 20 20 20 49 6e 64 65 ABLE */. Inde
7bc80 78 20 2a 70 43 6f 76 69 64 78 3b 20 20 20 20 20 x *pCovidx;
7bc90 20 20 2f 2a 20 50 6f 73 73 69 62 6c 65 20 63 6f /* Possible co
7bca0 76 65 72 69 6e 67 20 69 6e 64 65 78 20 66 6f 72 vering index for
7bcb0 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 WHERE_MULTI_OR
7bcc0 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 64 6f 75 62 */. } u;. doub
7bcd0 6c 65 20 72 4f 70 74 43 6f 73 74 3b 20 20 20 20 le rOptCost;
7bce0 20 20 2f 2a 20 22 4f 70 74 69 6d 61 6c 22 20 63 /* "Optimal" c
7bcf0 6f 73 74 20 66 6f 72 20 74 68 69 73 20 6c 65 76 ost for this lev
7bd00 65 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 el */.. /* The
7bd10 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 following field
7bd20 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 is really not pa
7bd30 72 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e rt of the curren
7bd40 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 t level. But.
7bd50 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61 ** we need a pla
7bd60 63 65 20 74 6f 20 63 61 63 68 65 20 76 69 72 74 ce to cache virt
7bd70 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 20 ual table index
7bd80 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 information for
7bd90 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 74 75 61 each. ** virtua
7bda0 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 l table in the F
7bdb0 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64 20 74 ROM clause and t
7bdc0 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 73 74 he WhereLevel st
7bdd0 72 75 63 74 75 72 65 20 69 73 0a 20 20 2a 2a 20 ructure is. **
7bde0 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61 a convenient pla
7bdf0 63 65 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 ce since there i
7be00 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 76 65 6c s one WhereLevel
7be10 20 66 6f 72 20 65 61 63 68 20 46 52 4f 4d 20 63 for each FROM c
7be20 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c 65 6d 65 lause. ** eleme
7be30 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 nt.. */. sqlit
7be40 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 e3_index_info *p
7be50 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64 IdxInfo; /* Ind
7be60 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 68 ex info for n-th
7be70 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f source table */
7be80 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73 .};../*.** Flags
7be90 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 appropriate for
7bea0 20 74 68 65 20 77 63 74 72 6c 46 6c 61 67 73 20 the wctrlFlags
7beb0 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c parameter of sql
7bec0 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 ite3WhereBegin()
7bed0 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72 .** and the Wher
7bee0 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c 61 67 73 eInfo.wctrlFlags
7bef0 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66 member..*/.#def
7bf00 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42 ine WHERE_ORDERB
7bf10 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 30 30 Y_NORMAL 0x000
7bf20 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 64 0 /* No-op */.#d
7bf30 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 efine WHERE_ORDE
7bf40 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 78 30 RBY_MIN 0x0
7bf50 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 001 /* ORDER BY
7bf60 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d processing for m
7bf70 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 in() func */.#de
7bf80 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 fine WHERE_ORDER
7bf90 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 30 30 BY_MAX 0x00
7bfa0 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 70 02 /* ORDER BY p
7bfb0 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d 61 rocessing for ma
7bfc0 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66 x() func */.#def
7bfd0 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 ine WHERE_ONEPAS
7bfe0 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 30 30 S_DESIRED 0x000
7bff0 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 6f 20 4 /* Want to do
7c000 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 45 2f one-pass UPDATE/
7c010 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 69 6e DELETE */.#defin
7c020 65 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54 e WHERE_DUPLICAT
7c030 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 30 38 20 ES_OK 0x0008
7c040 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 72 6e 20 /* Ok to return
7c050 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 61 6e 20 a row more than
7c060 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 once */.#define
7c070 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 5f WHERE_OMIT_OPEN_
7c080 43 4c 4f 53 45 20 20 30 78 30 30 31 30 20 2f 2a CLOSE 0x0010 /*
7c090 20 54 61 62 6c 65 20 63 75 72 73 6f 72 73 20 61 Table cursors a
7c0a0 72 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 re already open
7c0b0 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 */.#define WHERE
7c0c0 5f 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20 20 _FORCE_TABLE
7c0d0 20 20 30 78 30 30 32 30 20 2f 2a 20 44 6f 20 6e 0x0020 /* Do n
7c0e0 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2d ot use an index-
7c0f0 6f 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0a 23 only search */.#
7c100 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 define WHERE_ONE
7c110 54 41 42 4c 45 5f 4f 4e 4c 59 20 20 20 20 30 78 TABLE_ONLY 0x
7c120 30 30 34 30 20 2f 2a 20 4f 6e 6c 79 20 63 6f 64 0040 /* Only cod
7c130 65 20 74 68 65 20 31 73 74 20 74 61 62 6c 65 20 e the 1st table
7c140 69 6e 20 70 54 61 62 4c 69 73 74 20 2a 2f 0a 23 in pTabList */.#
7c150 64 65 66 69 6e 65 20 57 48 45 52 45 5f 41 4e 44 define WHERE_AND
7c160 5f 4f 4e 4c 59 20 20 20 20 20 20 20 20 20 30 78 _ONLY 0x
7c170 30 30 38 30 20 2f 2a 20 44 6f 6e 27 74 20 75 73 0080 /* Don't us
7c180 65 20 69 6e 64 69 63 65 73 20 66 6f 72 20 4f 52 e indices for OR
7c190 20 74 65 72 6d 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a terms */../*.**
7c1a0 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 The WHERE claus
7c1b0 65 20 70 72 6f 63 65 73 73 69 6e 67 20 72 6f 75 e processing rou
7c1c0 74 69 6e 65 20 68 61 73 20 74 77 6f 20 68 61 6c tine has two hal
7c1d0 76 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69 72 ves. The.** fir
7c1e0 73 74 20 70 61 72 74 20 64 6f 65 73 20 74 68 65 st part does the
7c1f0 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 57 48 start of the WH
7c200 45 52 45 20 6c 6f 6f 70 20 61 6e 64 20 74 68 65 ERE loop and the
7c210 20 73 65 63 6f 6e 64 0a 2a 2a 20 68 61 6c 66 20 second.** half
7c220 64 6f 65 73 20 74 68 65 20 74 61 69 6c 20 6f 66 does the tail of
7c230 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e the WHERE loop.
7c240 20 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 An instance of
7c250 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 .** this structu
7c260 72 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 re is returned b
7c270 79 20 74 68 65 20 66 69 72 73 74 20 68 61 6c 66 y the first half
7c280 20 61 6e 64 20 70 61 73 73 65 64 0a 2a 2a 20 69 and passed.** i
7c290 6e 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 68 nto the second h
7c2a0 61 6c 66 20 74 6f 20 67 69 76 65 20 73 6f 6d 65 alf to give some
7c2b0 20 63 6f 6e 74 69 6e 75 69 74 79 2e 0a 2a 2f 0a continuity..*/.
7c2c0 73 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f struct WhereInfo
7c2d0 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 {. Parse *pPar
7c2e0 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f se; /
7c2f0 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f * Parsing and co
7c300 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f de generating co
7c310 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 ntext */. SrcLi
7c320 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 st *pTabList;
7c330 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 /* List of
7c340 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f tables in the jo
7c350 69 6e 20 2a 2f 0a 20 20 75 31 36 20 6e 4f 42 53 in */. u16 nOBS
7c360 61 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 at;
7c370 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4f /* Number of O
7c380 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 73 61 RDER BY terms sa
7c390 74 69 73 66 69 65 64 20 62 79 20 69 6e 64 69 63 tisfied by indic
7c3a0 65 73 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 es */. u16 wctr
7c3b0 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 lFlags;
7c3c0 20 20 2f 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 /* Flags origi
7c3d0 6e 61 6c 6c 79 20 70 61 73 73 65 64 20 74 6f 20 nally passed to
7c3e0 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 sqlite3WhereBegi
7c3f0 6e 28 29 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e n() */. u8 okOn
7c400 65 50 61 73 73 3b 20 20 20 20 20 20 20 20 20 20 ePass;
7c410 20 20 20 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 20 /* Ok to use
7c420 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 one-pass algorit
7c430 68 6d 20 66 6f 72 20 55 50 44 41 54 45 2f 44 45 hm for UPDATE/DE
7c440 4c 45 54 45 20 2a 2f 0a 20 20 75 38 20 75 6e 74 LETE */. u8 unt
7c450 65 73 74 65 64 54 65 72 6d 73 3b 20 20 20 20 20 estedTerms;
7c460 20 20 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 20 57 /* Not all W
7c470 48 45 52 45 20 74 65 72 6d 73 20 72 65 73 6f 6c HERE terms resol
7c480 76 65 64 20 62 79 20 6f 75 74 65 72 20 6c 6f 6f ved by outer loo
7c490 70 20 2a 2f 0a 20 20 75 38 20 65 44 69 73 74 69 p */. u8 eDisti
7c4a0 6e 63 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 nct;
7c4b0 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 57 /* One of the W
7c4c0 48 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 2a 20 HERE_DISTINCT_*
7c4d0 76 61 6c 75 65 73 20 62 65 6c 6f 77 20 2a 2f 0a values below */.
7c4e0 20 20 69 6e 74 20 69 54 6f 70 3b 20 20 20 20 20 int iTop;
7c4f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
7c500 68 65 20 76 65 72 79 20 62 65 67 69 6e 6e 69 6e he very beginnin
7c510 67 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c g of the WHERE l
7c520 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f oop */. int iCo
7c530 6e 74 69 6e 75 65 3b 20 20 20 20 20 20 20 20 20 ntinue;
7c540 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 /* Jump here
7c550 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 to continue with
7c560 20 6e 65 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a next record */.
7c570 20 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 20 int iBreak;
7c580 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a /* J
7c590 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65 61 ump here to brea
7c5a0 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f k out of the loo
7c5b0 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 65 76 65 p */. int nLeve
7c5c0 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l;
7c5d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 /* Number of ne
7c5e0 73 74 65 64 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 sted loop */. s
7c5f0 74 72 75 63 74 20 57 68 65 72 65 43 6c 61 75 73 truct WhereClaus
7c600 65 20 2a 70 57 43 3b 20 20 2f 2a 20 44 65 63 6f e *pWC; /* Deco
7c610 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 mposition of the
7c620 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f WHERE clause */
7c630 0a 20 20 64 6f 75 62 6c 65 20 73 61 76 65 64 4e . double savedN
7c640 51 75 65 72 79 4c 6f 6f 70 3b 20 20 20 2f 2a 20 QueryLoop; /*
7c650 70 50 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f pParse->nQueryLo
7c660 6f 70 20 6f 75 74 73 69 64 65 20 74 68 65 20 57 op outside the W
7c670 48 45 52 45 20 6c 6f 6f 70 20 2a 2f 0a 20 20 64 HERE loop */. d
7c680 6f 75 62 6c 65 20 6e 52 6f 77 4f 75 74 3b 20 20 ouble nRowOut;
7c690 20 20 20 20 20 20 20 20 20 2f 2a 20 45 73 74 69 /* Esti
7c6a0 6d 61 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 mated number of
7c6b0 6f 75 74 70 75 74 20 72 6f 77 73 20 2a 2f 0a 20 output rows */.
7c6c0 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d WhereLevel a[1]
7c6d0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e ; /* In
7c6e0 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
7c6f0 65 61 63 68 20 6e 65 73 74 20 6c 6f 6f 70 20 69 each nest loop i
7c700 6e 20 57 48 45 52 45 20 2a 2f 0a 7d 3b 0a 0a 2f n WHERE */.};../
7c710 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 * Allowed values
7c720 20 66 6f 72 20 57 68 65 72 65 49 6e 66 6f 2e 65 for WhereInfo.e
7c730 44 69 73 74 69 6e 63 74 20 61 6e 64 20 44 69 73 Distinct and Dis
7c740 74 69 6e 63 74 43 74 78 2e 65 54 6e 63 74 54 79 tinctCtx.eTnctTy
7c750 70 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 pe */.#define WH
7c760 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 4e 4f 4f ERE_DISTINCT_NOO
7c770 50 20 20 20 20 20 20 30 20 20 2f 2a 20 44 49 53 P 0 /* DIS
7c780 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 6e 6f TINCT keyword no
7c790 74 20 75 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e t used */.#defin
7c7a0 65 20 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54 e WHERE_DISTINCT
7c7b0 5f 55 4e 49 51 55 45 20 20 20 20 31 20 20 2f 2a _UNIQUE 1 /*
7c7c0 20 4e 6f 20 64 75 70 6c 69 63 61 74 65 73 20 2a No duplicates *
7c7d0 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
7c7e0 44 49 53 54 49 4e 43 54 5f 4f 52 44 45 52 45 44 DISTINCT_ORDERED
7c7f0 20 20 20 32 20 20 2f 2a 20 41 6c 6c 20 64 75 70 2 /* All dup
7c800 6c 69 63 61 74 65 73 20 61 72 65 20 61 64 6a 61 licates are adja
7c810 63 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 cent */.#define
7c820 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 55 WHERE_DISTINCT_U
7c830 4e 4f 52 44 45 52 45 44 20 33 20 20 2f 2a 20 44 NORDERED 3 /* D
7c840 75 70 6c 69 63 61 74 65 73 20 61 72 65 20 73 63 uplicates are sc
7c850 61 74 74 65 72 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a attered */../*.*
7c860 2a 20 41 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 * A NameContext
7c870 64 65 66 69 6e 65 73 20 61 20 63 6f 6e 74 65 78 defines a contex
7c880 74 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 t in which to re
7c890 73 6f 6c 76 65 20 74 61 62 6c 65 20 61 6e 64 20 solve table and
7c8a0 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e column.** names.
7c8b0 20 20 54 68 65 20 63 6f 6e 74 65 78 74 20 63 6f The context co
7c8c0 6e 73 69 73 74 73 20 6f 66 20 61 20 6c 69 73 74 nsists of a list
7c8d0 20 6f 66 20 74 61 62 6c 65 73 20 28 74 68 65 20 of tables (the
7c8e0 70 53 72 63 4c 69 73 74 29 20 66 69 65 6c 64 20 pSrcList) field
7c8f0 61 6e 64 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66 and.** a list of
7c900 20 6e 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f named expressio
7c910 6e 20 28 70 45 4c 69 73 74 29 2e 20 20 54 68 65 n (pEList). The
7c920 20 6e 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f named expressio
7c930 6e 20 6c 69 73 74 20 6d 61 79 0a 2a 2a 20 62 65 n list may.** be
7c940 20 4e 55 4c 4c 2e 20 20 54 68 65 20 70 53 72 63 NULL. The pSrc
7c950 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 corresponds to
7c960 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 the FROM clause
7c970 6f 66 20 61 20 53 45 4c 45 43 54 20 6f 72 0a 2a of a SELECT or.*
7c980 2a 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 62 * to the table b
7c990 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e eing operated on
7c9a0 20 62 79 20 49 4e 53 45 52 54 2c 20 55 50 44 41 by INSERT, UPDA
7c9b0 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 2e 20 20 TE, or DELETE.
7c9c0 54 68 65 0a 2a 2a 20 70 45 4c 69 73 74 20 63 6f The.** pEList co
7c9d0 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 rresponds to the
7c9e0 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 result set of a
7c9f0 20 53 45 4c 45 43 54 20 61 6e 64 20 69 73 20 4e SELECT and is N
7ca00 55 4c 4c 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 ULL for.** other
7ca10 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a statements..**.
7ca20 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20 ** NameContexts
7ca30 63 61 6e 20 62 65 20 6e 65 73 74 65 64 2e 20 20 can be nested.
7ca40 57 68 65 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e When resolving n
7ca50 61 6d 65 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d ames, the inner-
7ca60 6d 6f 73 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 most .** context
7ca70 20 69 73 20 73 65 61 72 63 68 65 64 20 66 69 72 is searched fir
7ca80 73 74 2e 20 20 49 66 20 6e 6f 20 6d 61 74 63 68 st. If no match
7ca90 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e is found, the n
7caa0 65 78 74 20 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e ext outer.** con
7cab0 74 65 78 74 20 69 73 20 63 68 65 63 6b 65 64 2e text is checked.
7cac0 20 20 49 66 20 74 68 65 72 65 20 69 73 20 73 74 If there is st
7cad0 69 6c 6c 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68 ill no match, th
7cae0 65 20 6e 65 78 74 20 63 6f 6e 74 65 78 74 0a 2a e next context.*
7caf0 2a 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 54 * is checked. T
7cb00 68 69 73 20 70 72 6f 63 65 73 73 20 63 6f 6e 74 his process cont
7cb10 69 6e 75 65 73 20 75 6e 74 69 6c 20 65 69 74 68 inues until eith
7cb20 65 72 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f er a match is fo
7cb30 75 6e 64 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f und.** or all co
7cb40 6e 74 65 78 74 73 20 61 72 65 20 63 68 65 63 6b ntexts are check
7cb50 2e 20 20 57 68 65 6e 20 61 20 6d 61 74 63 68 20 . When a match
7cb60 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 52 is found, the nR
7cb70 65 66 20 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 ef member of.**
7cb80 74 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 74 the context cont
7cb90 61 69 6e 69 6e 67 20 74 68 65 20 6d 61 74 63 68 aining the match
7cba0 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e is incremented.
7cbb0 20 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 75 62 .**.** Each sub
7cbc0 71 75 65 72 79 20 67 65 74 73 20 61 20 6e 65 77 query gets a new
7cbd0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 20 20 54 NameContext. T
7cbe0 68 65 20 70 4e 65 78 74 20 66 69 65 6c 64 20 70 he pNext field p
7cbf0 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 oints to the.**
7cc00 4e 61 6d 65 43 6f 6e 74 65 78 74 20 69 6e 20 74 NameContext in t
7cc10 68 65 20 70 61 72 65 6e 74 20 71 75 65 72 79 2e he parent query.
7cc20 20 20 54 68 75 73 20 74 68 65 20 70 72 6f 63 65 Thus the proce
7cc30 73 73 20 6f 66 20 73 63 61 6e 6e 69 6e 67 20 74 ss of scanning t
7cc40 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 he.** NameContex
7cc50 74 20 6c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e t list correspon
7cc60 64 73 20 74 6f 20 73 65 61 72 63 68 69 6e 67 20 ds to searching
7cc70 74 68 72 6f 75 67 68 20 73 75 63 63 65 73 73 69 through successi
7cc80 76 65 6c 79 20 6f 75 74 65 72 0a 2a 2a 20 73 75 vely outer.** su
7cc90 62 71 75 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 bqueries looking
7cca0 20 66 6f 72 20 61 20 6d 61 74 63 68 2e 0a 2a 2f for a match..*/
7ccb0 0a 73 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 .struct NameCont
7ccc0 65 78 74 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 ext {. Parse *p
7ccd0 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 Parse; /*
7cce0 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 The parser */.
7ccf0 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 SrcList *pSrcLis
7cd00 74 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d t; /* One or m
7cd10 6f 72 65 20 74 61 62 6c 65 73 20 75 73 65 64 20 ore tables used
7cd20 74 6f 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 to resolve names
7cd30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
7cd40 70 45 4c 69 73 74 3b 20 20 20 20 2f 2a 20 4f 70 pEList; /* Op
7cd50 74 69 6f 6e 61 6c 20 6c 69 73 74 20 6f 66 20 6e tional list of n
7cd60 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 73 amed expressions
7cd70 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 */. AggInfo *p
7cd80 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e AggInfo; /* In
7cd90 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
7cda0 61 67 67 72 65 67 61 74 65 73 20 61 74 20 74 68 aggregates at th
7cdb0 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 is level */. Na
7cdc0 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74 meContext *pNext
7cdd0 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72 ; /* Next outer
7cde0 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 name context.
7cdf0 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f NULL for outermo
7ce00 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 st */. int nRef
7ce10 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
7ce20 4e 75 6d 62 65 72 20 6f 66 20 6e 61 6d 65 73 20 Number of names
7ce30 72 65 73 6f 6c 76 65 64 20 62 79 20 74 68 69 73 resolved by this
7ce40 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e context */. in
7ce50 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 t nErr;
7ce60 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
7ce70 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 errors encounter
7ce80 65 64 20 77 68 69 6c 65 20 72 65 73 6f 6c 76 69 ed while resolvi
7ce90 6e 67 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 75 38 ng names */. u8
7cea0 20 6e 63 46 6c 61 67 73 3b 20 20 20 20 20 20 20 ncFlags;
7ceb0 20 20 20 2f 2a 20 5a 65 72 6f 20 6f 72 20 6d 6f /* Zero or mo
7cec0 72 65 20 4e 43 5f 2a 20 66 6c 61 67 73 20 64 65 re NC_* flags de
7ced0 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 7d fined below */.}
7cee0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 ;../*.** Allowed
7cef0 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 values for the
7cf00 4e 61 6d 65 43 6f 6e 74 65 78 74 2c 20 6e 63 46 NameContext, ncF
7cf10 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 lags field..*/.#
7cf20 64 65 66 69 6e 65 20 4e 43 5f 41 6c 6c 6f 77 41 define NC_AllowA
7cf30 67 67 20 20 30 78 30 31 20 20 20 20 2f 2a 20 41 gg 0x01 /* A
7cf40 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
7cf50 6e 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 68 ns are allowed h
7cf60 65 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4e ere */.#define N
7cf70 43 5f 48 61 73 41 67 67 20 20 20 20 30 78 30 32 C_HasAgg 0x02
7cf80 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f /* One or mo
7cf90 72 65 20 61 67 67 72 65 67 61 74 65 20 66 75 6e re aggregate fun
7cfa0 63 74 69 6f 6e 73 20 73 65 65 6e 20 2a 2f 0a 23 ctions seen */.#
7cfb0 64 65 66 69 6e 65 20 4e 43 5f 49 73 43 68 65 63 define NC_IsChec
7cfc0 6b 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 k 0x04 /* T
7cfd0 72 75 65 20 69 66 20 72 65 73 6f 6c 76 69 6e 67 rue if resolving
7cfe0 20 6e 61 6d 65 73 20 69 6e 20 61 20 43 48 45 43 names in a CHEC
7cff0 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a K constraint */.
7d000 23 64 65 66 69 6e 65 20 4e 43 5f 49 6e 41 67 67 #define NC_InAgg
7d010 46 75 6e 63 20 30 78 30 38 20 20 20 20 2f 2a 20 Func 0x08 /*
7d020 54 72 75 65 20 69 66 20 61 6e 61 6c 79 7a 69 6e True if analyzin
7d030 67 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 61 g arguments to a
7d040 6e 20 61 67 67 20 66 75 6e 63 20 2a 2f 0a 0a 2f n agg func */../
7d050 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
7d060 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
7d070 67 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 g structure cont
7d080 61 69 6e 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 ains all informa
7d090 74 69 6f 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 74 tion.** needed t
7d0a0 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 o generate code
7d0b0 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 53 45 4c for a single SEL
7d0c0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a ECT statement..*
7d0d0 2a 0a 2a 2a 20 6e 4c 69 6d 69 74 20 69 73 20 73 *.** nLimit is s
7d0e0 65 74 20 74 6f 20 2d 31 20 69 66 20 74 68 65 72 et to -1 if ther
7d0f0 65 20 69 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c e is no LIMIT cl
7d100 61 75 73 65 2e 20 20 6e 4f 66 66 73 65 74 20 69 ause. nOffset i
7d110 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 s set to 0..** I
7d120 66 20 74 68 65 72 65 20 69 73 20 61 20 4c 49 4d f there is a LIM
7d130 49 54 20 63 6c 61 75 73 65 2c 20 74 68 65 20 70 IT clause, the p
7d140 61 72 73 65 72 20 73 65 74 73 20 6e 4c 69 6d 69 arser sets nLimi
7d150 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f t to the value o
7d160 66 20 74 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 f the.** limit a
7d170 6e 64 20 6e 4f 66 66 73 65 74 20 74 6f 20 74 68 nd nOffset to th
7d180 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f e value of the o
7d190 66 66 73 65 74 20 28 6f 72 20 30 20 69 66 20 74 ffset (or 0 if t
7d1a0 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f here is not.** o
7d1b0 66 66 73 65 74 29 2e 20 20 42 75 74 20 6c 61 74 ffset). But lat
7d1c0 65 72 20 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e er on, nLimit an
7d1d0 64 20 6e 4f 66 66 73 65 74 20 62 65 63 6f 6d 65 d nOffset become
7d1e0 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 the memory loca
7d1f0 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 tions.** in the
7d200 56 44 42 45 20 74 68 61 74 20 72 65 63 6f 72 64 VDBE that record
7d210 20 74 68 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f the limit and o
7d220 66 66 73 65 74 20 63 6f 75 6e 74 65 72 73 2e 0a ffset counters..
7d230 2a 2a 0a 2a 2a 20 61 64 64 72 4f 70 65 6e 45 70 **.** addrOpenEp
7d240 68 6d 5b 5d 20 65 6e 74 72 69 65 73 20 63 6f 6e hm[] entries con
7d250 74 61 69 6e 20 74 68 65 20 61 64 64 72 65 73 73 tain the address
7d260 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d of OP_OpenEphem
7d270 65 72 61 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a eral opcodes..**
7d280 20 54 68 65 73 65 20 61 64 64 72 65 73 73 65 73 These addresses
7d290 20 6d 75 73 74 20 62 65 20 73 74 6f 72 65 64 20 must be stored
7d2a0 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 67 so that we can g
7d2b0 6f 20 62 61 63 6b 20 61 6e 64 20 66 69 6c 6c 20 o back and fill
7d2c0 69 6e 0a 2a 2a 20 74 68 65 20 50 34 5f 4b 45 59 in.** the P4_KEY
7d2d0 49 4e 46 4f 20 61 6e 64 20 50 32 20 70 61 72 61 INFO and P2 para
7d2e0 6d 65 74 65 72 73 20 6c 61 74 65 72 2e 20 20 4e meters later. N
7d2f0 65 69 74 68 65 72 20 74 68 65 20 4b 65 79 49 6e either the KeyIn
7d300 66 6f 20 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 fo nor.** the nu
7d310 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
7d320 69 6e 20 50 32 20 63 61 6e 20 62 65 20 63 6f 6d in P2 can be com
7d330 70 75 74 65 64 20 61 74 20 74 68 65 20 73 61 6d puted at the sam
7d340 65 20 74 69 6d 65 0a 2a 2a 20 61 73 20 74 68 65 e time.** as the
7d350 20 4f 50 5f 4f 70 65 6e 45 70 68 6d 20 69 6e 73 OP_OpenEphm ins
7d360 74 72 75 63 74 69 6f 6e 20 69 73 20 63 6f 64 65 truction is code
7d370 64 20 62 65 63 61 75 73 65 20 6e 6f 74 0a 2a 2a d because not.**
7d380 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 enough informat
7d390 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 6f ion about the co
7d3a0 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 69 73 20 mpound query is
7d3b0 6b 6e 6f 77 6e 20 61 74 20 74 68 61 74 20 70 6f known at that po
7d3c0 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 49 int..** The KeyI
7d3d0 6e 66 6f 20 66 6f 72 20 61 64 64 72 4f 70 65 6e nfo for addrOpen
7d3e0 54 72 61 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 Tran[0] and [1]
7d3f0 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 contains collati
7d400 6e 67 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 ng sequences.**
7d410 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 for the result s
7d420 65 74 2e 20 20 54 68 65 20 4b 65 79 49 6e 66 6f et. The KeyInfo
7d430 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 45 70 68 for addrOpenEph
7d440 6d 5b 32 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f m[2] contains co
7d450 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 llating.** seque
7d460 6e 63 65 73 20 66 6f 72 20 74 68 65 20 4f 52 44 nces for the ORD
7d470 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f ER BY clause..*/
7d480 0a 73 74 72 75 63 74 20 53 65 6c 65 63 74 20 7b .struct Select {
7d490 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c . ExprList *pEL
7d4a0 69 73 74 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 ist; /* The
7d4b0 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 72 fields of the r
7d4c0 65 73 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6f 70 esult */. u8 op
7d4d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
7d4e0 20 20 2f 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f /* One of: TK_
7d4f0 55 4e 49 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f UNION TK_ALL TK_
7d500 49 4e 54 45 52 53 45 43 54 20 54 4b 5f 45 58 43 INTERSECT TK_EXC
7d510 45 50 54 20 2a 2f 0a 20 20 75 31 36 20 73 65 6c EPT */. u16 sel
7d520 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 Flags;
7d530 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 5f 2a 20 /* Various SF_*
7d540 76 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e 74 20 values */. int
7d550 69 4c 69 6d 69 74 2c 20 69 4f 66 66 73 65 74 3b iLimit, iOffset;
7d560 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67 /* Memory reg
7d570 69 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 4c isters holding L
7d580 49 4d 49 54 20 26 20 4f 46 46 53 45 54 20 63 6f IMIT & OFFSET co
7d590 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 unters */. int
7d5a0 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 33 5d 3b addrOpenEphm[3];
7d5b0 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e 45 70 68 /* OP_OpenEph
7d5c0 65 6d 20 6f 70 63 6f 64 65 73 20 72 65 6c 61 74 em opcodes relat
7d5d0 65 64 20 74 6f 20 74 68 69 73 20 73 65 6c 65 63 ed to this selec
7d5e0 74 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 6e 53 t */. double nS
7d5f0 65 6c 65 63 74 52 6f 77 3b 20 20 20 20 20 2f 2a electRow; /*
7d600 20 45 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65 Estimated numbe
7d610 72 20 6f 66 20 72 65 73 75 6c 74 20 72 6f 77 73 r of result rows
7d620 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 */. SrcList *p
7d630 53 72 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Src; /*
7d640 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 The FROM clause
7d650 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 */. Expr *pWher
7d660 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 e; /* T
7d670 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
7d680 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
7d690 47 72 6f 75 70 42 79 3b 20 20 20 20 2f 2a 20 54 GroupBy; /* T
7d6a0 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 he GROUP BY clau
7d6b0 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48 se */. Expr *pH
7d6c0 61 76 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f aving; /
7d6d0 2a 20 54 68 65 20 48 41 56 49 4e 47 20 63 6c 61 * The HAVING cla
7d6e0 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 use */. ExprLis
7d6f0 74 20 2a 70 4f 72 64 65 72 42 79 3b 20 20 20 20 t *pOrderBy;
7d700 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 /* The ORDER BY
7d710 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 clause */. Sele
7d720 63 74 20 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 ct *pPrior;
7d730 20 20 20 2f 2a 20 50 72 69 6f 72 20 73 65 6c 65 /* Prior sele
7d740 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 ct in a compound
7d750 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e select statemen
7d760 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 t */. Select *p
7d770 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Next; /*
7d780 20 4e 65 78 74 20 73 65 6c 65 63 74 20 74 6f 20 Next select to
7d790 74 68 65 20 6c 65 66 74 20 69 6e 20 61 20 63 6f the left in a co
7d7a0 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 53 65 6c 65 mpound */. Sele
7d7b0 63 74 20 2a 70 52 69 67 68 74 6d 6f 73 74 3b 20 ct *pRightmost;
7d7c0 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 /* Right-most
7d7d0 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d select in a com
7d7e0 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 74 61 pound select sta
7d7f0 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 tement */. Expr
7d800 20 2a 70 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 *pLimit;
7d810 20 20 20 2f 2a 20 4c 49 4d 49 54 20 65 78 70 72 /* LIMIT expr
7d820 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 ession. NULL mea
7d830 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a ns not used. */.
7d840 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74 3b Expr *pOffset;
7d850 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 46 46 53 /* OFFS
7d860 45 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e ET expression. N
7d870 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 ULL means not us
7d880 65 64 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ed. */.};../*.**
7d890 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 Allowed values
7d8a0 66 6f 72 20 53 65 6c 65 63 74 2e 73 65 6c 46 6c for Select.selFl
7d8b0 61 67 73 2e 20 20 54 68 65 20 22 53 46 22 20 70 ags. The "SF" p
7d8c0 72 65 66 69 78 20 73 74 61 6e 64 73 20 66 6f 72 refix stands for
7d8d0 0a 2a 2a 20 22 53 65 6c 65 63 74 20 46 6c 61 67 .** "Select Flag
7d8e0 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 "..*/.#define SF
7d8f0 5f 44 69 73 74 69 6e 63 74 20 20 20 20 20 20 20 _Distinct
7d900 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 75 74 70 0x0001 /* Outp
7d910 75 74 20 73 68 6f 75 6c 64 20 62 65 20 44 49 53 ut should be DIS
7d920 54 49 4e 43 54 20 2a 2f 0a 23 64 65 66 69 6e 65 TINCT */.#define
7d930 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 20 20 20 SF_Resolved
7d940 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 49 0x0002 /* I
7d950 64 65 6e 74 69 66 69 65 72 73 20 68 61 76 65 20 dentifiers have
7d960 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 2a 2f been resolved */
7d970 0a 23 64 65 66 69 6e 65 20 53 46 5f 41 67 67 72 .#define SF_Aggr
7d980 65 67 61 74 65 20 20 20 20 20 20 20 30 78 30 30 egate 0x00
7d990 30 34 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20 04 /* Contains
7d9a0 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
7d9b0 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ons */.#define S
7d9c0 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 20 F_UsesEphemeral
7d9d0 20 20 30 78 30 30 30 38 20 20 2f 2a 20 55 73 65 0x0008 /* Use
7d9e0 73 20 74 68 65 20 4f 70 65 6e 45 70 68 65 6d 65 s the OpenEpheme
7d9f0 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 64 ral opcode */.#d
7da00 65 66 69 6e 65 20 53 46 5f 45 78 70 61 6e 64 65 efine SF_Expande
7da10 64 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20 d 0x0010
7da20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 /* sqlite3Selec
7da30 74 45 78 70 61 6e 64 28 29 20 63 61 6c 6c 65 64 tExpand() called
7da40 20 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 64 65 66 on this */.#def
7da50 69 6e 65 20 53 46 5f 48 61 73 54 79 70 65 49 6e ine SF_HasTypeIn
7da60 66 6f 20 20 20 20 20 30 78 30 30 32 30 20 20 2f fo 0x0020 /
7da70 2a 20 46 52 4f 4d 20 73 75 62 71 75 65 72 69 65 * FROM subquerie
7da80 73 20 68 61 76 65 20 54 61 62 6c 65 20 6d 65 74 s have Table met
7da90 61 64 61 74 61 20 2a 2f 0a 23 64 65 66 69 6e 65 adata */.#define
7daa0 20 53 46 5f 55 73 65 53 6f 72 74 65 72 20 20 20 SF_UseSorter
7dab0 20 20 20 20 30 78 30 30 34 30 20 20 2f 2a 20 53 0x0040 /* S
7dac0 6f 72 74 20 75 73 69 6e 67 20 61 20 73 6f 72 74 ort using a sort
7dad0 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 er */.#define SF
7dae0 5f 56 61 6c 75 65 73 20 20 20 20 20 20 20 20 20 _Values
7daf0 20 30 78 30 30 38 30 20 20 2f 2a 20 53 79 6e 74 0x0080 /* Synt
7db00 68 65 73 69 7a 65 64 20 66 72 6f 6d 20 56 41 4c hesized from VAL
7db10 55 45 53 20 63 6c 61 75 73 65 20 2a 2f 0a 23 64 UES clause */.#d
7db20 65 66 69 6e 65 20 53 46 5f 4d 61 74 65 72 69 61 efine SF_Materia
7db30 6c 69 7a 65 20 20 20 20 20 30 78 30 31 30 30 20 lize 0x0100
7db40 20 2f 2a 20 46 6f 72 63 65 20 6d 61 74 65 72 69 /* Force materi
7db50 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 76 69 65 alization of vie
7db60 77 73 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 ws */.../*.** Th
7db70 65 20 72 65 73 75 6c 74 73 20 6f 66 20 61 20 73 e results of a s
7db80 65 6c 65 63 74 20 63 61 6e 20 62 65 20 64 69 73 elect can be dis
7db90 74 72 69 62 75 74 65 64 20 69 6e 20 73 65 76 65 tributed in seve
7dba0 72 61 6c 20 77 61 79 73 2e 20 20 54 68 65 0a 2a ral ways. The.*
7dbb0 2a 20 22 53 52 54 22 20 70 72 65 66 69 78 20 6d * "SRT" prefix m
7dbc0 65 61 6e 73 20 22 53 45 4c 45 43 54 20 52 65 73 eans "SELECT Res
7dbd0 75 6c 74 20 54 79 70 65 22 2e 0a 2a 2f 0a 23 64 ult Type"..*/.#d
7dbe0 65 66 69 6e 65 20 53 52 54 5f 55 6e 69 6f 6e 20 efine SRT_Union
7dbf0 20 20 20 20 20 20 20 31 20 20 2f 2a 20 53 74 6f 1 /* Sto
7dc00 72 65 20 72 65 73 75 6c 74 20 61 73 20 6b 65 79 re result as key
7dc10 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20 2a 2f s in an index */
7dc20 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 78 63 .#define SRT_Exc
7dc30 65 70 74 20 20 20 20 20 20 20 32 20 20 2f 2a 20 ept 2 /*
7dc40 52 65 6d 6f 76 65 20 72 65 73 75 6c 74 20 66 72 Remove result fr
7dc50 6f 6d 20 61 20 55 4e 49 4f 4e 20 69 6e 64 65 78 om a UNION index
7dc60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f */.#define SRT_
7dc70 45 78 69 73 74 73 20 20 20 20 20 20 20 33 20 20 Exists 3
7dc80 2f 2a 20 53 74 6f 72 65 20 31 20 69 66 20 74 68 /* Store 1 if th
7dc90 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f 74 20 e result is not
7dca0 65 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 empty */.#define
7dcb0 20 53 52 54 5f 44 69 73 63 61 72 64 20 20 20 20 SRT_Discard
7dcc0 20 20 34 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 4 /* Do not s
7dcd0 61 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 ave the results
7dce0 61 6e 79 77 68 65 72 65 20 2a 2f 0a 0a 2f 2a 20 anywhere */../*
7dcf0 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 The ORDER BY cla
7dd00 75 73 65 20 69 73 20 69 67 6e 6f 72 65 64 20 66 use is ignored f
7dd10 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 or all of the ab
7dd20 6f 76 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 49 ove */.#define I
7dd30 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62 79 28 gnorableOrderby(
7dd40 58 29 20 28 28 58 2d 3e 65 44 65 73 74 29 3c 3d X) ((X->eDest)<=
7dd50 53 52 54 5f 44 69 73 63 61 72 64 29 0a 0a 23 64 SRT_Discard)..#d
7dd60 65 66 69 6e 65 20 53 52 54 5f 4f 75 74 70 75 74 efine SRT_Output
7dd70 20 20 20 20 20 20 20 35 20 20 2f 2a 20 4f 75 74 5 /* Out
7dd80 70 75 74 20 65 61 63 68 20 72 6f 77 20 6f 66 20 put each row of
7dd90 72 65 73 75 6c 74 20 2a 2f 0a 23 64 65 66 69 6e result */.#defin
7dda0 65 20 53 52 54 5f 4d 65 6d 20 20 20 20 20 20 20 e SRT_Mem
7ddb0 20 20 20 36 20 20 2f 2a 20 53 74 6f 72 65 20 72 6 /* Store r
7ddc0 65 73 75 6c 74 20 69 6e 20 61 20 6d 65 6d 6f 72 esult in a memor
7ddd0 79 20 63 65 6c 6c 20 2a 2f 0a 23 64 65 66 69 6e y cell */.#defin
7dde0 65 20 53 52 54 5f 53 65 74 20 20 20 20 20 20 20 e SRT_Set
7ddf0 20 20 20 37 20 20 2f 2a 20 53 74 6f 72 65 20 72 7 /* Store r
7de00 65 73 75 6c 74 73 20 61 73 20 6b 65 79 73 20 69 esults as keys i
7de10 6e 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 n an index */.#d
7de20 65 66 69 6e 65 20 53 52 54 5f 54 61 62 6c 65 20 efine SRT_Table
7de30 20 20 20 20 20 20 20 38 20 20 2f 2a 20 53 74 6f 8 /* Sto
7de40 72 65 20 72 65 73 75 6c 74 20 61 73 20 64 61 74 re result as dat
7de50 61 20 77 69 74 68 20 61 6e 20 61 75 74 6f 6d 61 a with an automa
7de60 74 69 63 20 72 6f 77 69 64 20 2a 2f 0a 23 64 65 tic rowid */.#de
7de70 66 69 6e 65 20 53 52 54 5f 45 70 68 65 6d 54 61 fine SRT_EphemTa
7de80 62 20 20 20 20 20 39 20 20 2f 2a 20 43 72 65 61 b 9 /* Crea
7de90 74 65 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 te transient tab
7dea0 20 61 6e 64 20 73 74 6f 72 65 20 6c 69 6b 65 20 and store like
7deb0 53 52 54 5f 54 61 62 6c 65 20 2a 2f 0a 23 64 65 SRT_Table */.#de
7dec0 66 69 6e 65 20 53 52 54 5f 43 6f 72 6f 75 74 69 fine SRT_Corouti
7ded0 6e 65 20 20 20 31 30 20 20 2f 2a 20 47 65 6e 65 ne 10 /* Gene
7dee0 72 61 74 65 20 61 20 73 69 6e 67 6c 65 20 72 6f rate a single ro
7def0 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 0a w of result */..
7df00 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
7df10 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
7df20 20 64 65 73 63 72 69 62 65 73 20 77 68 65 72 65 describes where
7df30 20 74 6f 20 70 75 74 20 6f 66 20 74 68 65 20 72 to put of the r
7df40 65 73 75 6c 74 73 20 6f 66 0a 2a 2a 20 61 20 53 esults of.** a S
7df50 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
7df60 0a 2a 2f 0a 73 74 72 75 63 74 20 53 65 6c 65 63 .*/.struct Selec
7df70 74 44 65 73 74 20 7b 0a 20 20 75 38 20 65 44 65 tDest {. u8 eDe
7df80 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 48 st; /* H
7df90 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 ow to dispose of
7dfa0 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20 4f the results. O
7dfb0 6e 20 6f 66 20 53 52 54 5f 2a 20 61 62 6f 76 65 n of SRT_* above
7dfc0 2e 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 53 . */. char affS
7dfd0 64 73 74 3b 20 20 20 20 20 2f 2a 20 41 66 66 69 dst; /* Affi
7dfe0 6e 69 74 79 20 75 73 65 64 20 77 68 65 6e 20 65 nity used when e
7dff0 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f Dest==SRT_Set */
7e000 0a 20 20 69 6e 74 20 69 53 44 50 61 72 6d 3b 20 . int iSDParm;
7e010 20 20 20 20 20 2f 2a 20 41 20 70 61 72 61 6d 65 /* A parame
7e020 74 65 72 20 75 73 65 64 20 62 79 20 74 68 65 20 ter used by the
7e030 65 44 65 73 74 20 64 69 73 70 6f 73 61 6c 20 6d eDest disposal m
7e040 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69 ethod */. int i
7e050 53 64 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 Sdst; /*
7e060 42 61 73 65 20 72 65 67 69 73 74 65 72 20 77 68 Base register wh
7e070 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 ere results are
7e080 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 written */. int
7e090 20 6e 53 64 73 74 3b 20 20 20 20 20 20 20 20 2f nSdst; /
7e0a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 * Number of regi
7e0b0 73 74 65 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 sters allocated
7e0c0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 */.};../*.** Dur
7e0d0 69 6e 67 20 63 6f 64 65 20 67 65 6e 65 72 61 74 ing code generat
7e0e0 69 6f 6e 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 ion of statement
7e0f0 73 20 74 68 61 74 20 64 6f 20 69 6e 73 65 72 74 s that do insert
7e100 73 20 69 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45 s into AUTOINCRE
7e110 4d 45 4e 54 20 0a 2a 2a 20 74 61 62 6c 65 73 2c MENT .** tables,
7e120 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 the following i
7e130 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 74 nformation is at
7e140 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 54 61 tached to the Ta
7e150 62 6c 65 2e 75 2e 61 75 74 6f 49 6e 63 2e 70 0a ble.u.autoInc.p.
7e160 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66 20 65 61 ** pointer of ea
7e170 63 68 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 ch autoincrement
7e180 20 74 61 62 6c 65 20 74 6f 20 72 65 63 6f 72 64 table to record
7e190 20 73 6f 6d 65 20 73 69 64 65 20 69 6e 66 6f 72 some side infor
7e1a0 6d 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74 mation that.** t
7e1b0 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f he code generato
7e1c0 72 20 6e 65 65 64 73 2e 20 20 57 65 20 68 61 76 r needs. We hav
7e1d0 65 20 74 6f 20 6b 65 65 70 20 70 65 72 2d 74 61 e to keep per-ta
7e1e0 62 6c 65 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e ble autoincremen
7e1f0 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e t.** information
7e200 20 69 6e 20 63 61 73 65 20 69 6e 73 65 72 74 73 in case inserts
7e210 20 61 72 65 20 64 6f 77 6e 20 77 69 74 68 69 6e are down within
7e220 20 74 72 69 67 67 65 72 73 2e 20 20 54 72 69 67 triggers. Trig
7e230 67 65 72 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e gers do not.** n
7e240 6f 72 6d 61 6c 6c 79 20 63 6f 6f 72 64 69 6e 61 ormally coordina
7e250 74 65 20 74 68 65 69 72 20 61 63 74 69 76 69 74 te their activit
7e260 69 65 73 2c 20 62 75 74 20 77 65 20 64 6f 20 6e ies, but we do n
7e270 65 65 64 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 eed to coordinat
7e280 65 20 74 68 65 0a 2a 2a 20 6c 6f 61 64 69 6e 67 e the.** loading
7e290 20 61 6e 64 20 73 61 76 69 6e 67 20 6f 66 20 61 and saving of a
7e2a0 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 69 6e 66 utoincrement inf
7e2b0 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 ormation..*/.str
7e2c0 75 63 74 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 uct AutoincInfo
7e2d0 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 {. AutoincInfo
7e2e0 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 *pNext; /* Nex
7e2f0 74 20 69 6e 66 6f 20 62 6c 6f 63 6b 20 69 6e 20 t info block in
7e300 61 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 a list of them a
7e310 6c 6c 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 ll */. Table *p
7e320 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Tab; /*
7e330 20 54 61 62 6c 65 20 74 68 69 73 20 69 6e 66 6f Table this info
7e340 20 62 6c 6f 63 6b 20 72 65 66 65 72 73 20 74 6f block refers to
7e350 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 */. int iDb;
7e360 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
7e370 6e 64 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e ndex in sqlite3.
7e380 61 44 62 5b 5d 20 6f 66 20 64 61 74 61 62 61 73 aDb[] of databas
7e390 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a e holding pTab *
7e3a0 2f 0a 20 20 69 6e 74 20 72 65 67 43 74 72 3b 20 /. int regCtr;
7e3b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
7e3c0 6f 72 79 20 72 65 67 69 73 74 65 72 20 68 6f 6c ory register hol
7e3d0 64 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 63 ding the rowid c
7e3e0 6f 75 6e 74 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a ounter */.};../*
7e3f0 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 .** Size of the
7e400 63 6f 6c 75 6d 6e 20 63 61 63 68 65 0a 2a 2f 0a column cache.*/.
7e410 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e #ifndef SQLITE_N
7e420 5f 43 4f 4c 43 41 43 48 45 0a 23 20 64 65 66 69 _COLCACHE.# defi
7e430 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 ne SQLITE_N_COLC
7e440 41 43 48 45 20 31 30 0a 23 65 6e 64 69 66 0a 0a ACHE 10.#endif..
7e450 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 6f /*.** At least o
7e460 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ne instance of t
7e470 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
7e480 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 ucture is create
7e490 64 20 66 6f 72 20 65 61 63 68 20 0a 2a 2a 20 74 d for each .** t
7e4a0 72 69 67 67 65 72 20 74 68 61 74 20 6d 61 79 20 rigger that may
7e4b0 62 65 20 66 69 72 65 64 20 77 68 69 6c 65 20 70 be fired while p
7e4c0 61 72 73 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 arsing an INSERT
7e4d0 2c 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 , UPDATE or DELE
7e4e0 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e TE.** statement.
7e4f0 20 41 6c 6c 20 73 75 63 68 20 6f 62 6a 65 63 74 All such object
7e500 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 s are stored in
7e510 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 the linked list
7e520 68 65 61 64 65 64 20 61 74 0a 2a 2a 20 50 61 72 headed at.** Par
7e530 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 61 se.pTriggerPrg a
7e540 6e 64 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 nd deleted once
7e550 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c statement compil
7e560 61 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a ation has been.*
7e570 2a 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a * completed..**.
7e580 2a 2a 20 41 20 56 64 62 65 20 73 75 62 2d 70 72 ** A Vdbe sub-pr
7e590 6f 67 72 61 6d 20 74 68 61 74 20 69 6d 70 6c 65 ogram that imple
7e5a0 6d 65 6e 74 73 20 74 68 65 20 62 6f 64 79 20 61 ments the body a
7e5b0 6e 64 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f nd WHEN clause o
7e5c0 66 20 74 72 69 67 67 65 72 0a 2a 2a 20 54 72 69 f trigger.** Tri
7e5d0 67 67 65 72 50 72 67 2e 70 54 72 69 67 67 65 72 ggerPrg.pTrigger
7e5e0 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 64 65 66 , assuming a def
7e5f0 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 ault ON CONFLICT
7e600 20 63 6c 61 75 73 65 20 6f 66 0a 2a 2a 20 54 72 clause of.** Tr
7e610 69 67 67 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c iggerPrg.orconf,
7e620 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 is stored in th
7e630 65 20 54 72 69 67 67 65 72 50 72 67 2e 70 50 72 e TriggerPrg.pPr
7e640 6f 67 72 61 6d 20 76 61 72 69 61 62 6c 65 2e 0a ogram variable..
7e650 2a 2a 20 54 68 65 20 50 61 72 73 65 2e 70 54 72 ** The Parse.pTr
7e660 69 67 67 65 72 50 72 67 20 6c 69 73 74 20 6e 65 iggerPrg list ne
7e670 76 65 72 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f ver contains two
7e680 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 entries with th
7e690 65 20 73 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 73 e same.** values
7e6a0 20 66 6f 72 20 62 6f 74 68 20 70 54 72 69 67 67 for both pTrigg
7e6b0 65 72 20 61 6e 64 20 6f 72 63 6f 6e 66 2e 0a 2a er and orconf..*
7e6c0 2a 0a 2a 2a 20 54 68 65 20 54 72 69 67 67 65 72 *.** The Trigger
7e6d0 50 72 67 2e 61 43 6f 6c 6d 61 73 6b 5b 30 5d 20 Prg.aColmask[0]
7e6e0 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 variable is set
7e6f0 74 6f 20 61 20 6d 61 73 6b 20 6f 66 20 6f 6c 64 to a mask of old
7e700 2e 2a 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 61 63 .* columns.** ac
7e710 63 65 73 73 65 64 20 28 6f 72 20 73 65 74 20 74 cessed (or set t
7e720 6f 20 30 20 66 6f 72 20 74 72 69 67 67 65 72 73 o 0 for triggers
7e730 20 66 69 72 65 64 20 61 73 20 61 20 72 65 73 75 fired as a resu
7e740 6c 74 20 6f 66 20 49 4e 53 45 52 54 20 0a 2a 2a lt of INSERT .**
7e750 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 20 53 69 statements). Si
7e760 6d 69 6c 61 72 6c 79 2c 20 74 68 65 20 54 72 69 milarly, the Tri
7e770 67 67 65 72 50 72 67 2e 61 43 6f 6c 6d 61 73 6b ggerPrg.aColmask
7e780 5b 31 5d 20 76 61 72 69 61 62 6c 65 20 69 73 20 [1] variable is
7e790 73 65 74 20 74 6f 0a 2a 2a 20 61 20 6d 61 73 6b set to.** a mask
7e7a0 20 6f 66 20 6e 65 77 2e 2a 20 63 6f 6c 75 6d 6e of new.* column
7e7b0 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 72 s used by the pr
7e7c0 6f 67 72 61 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74 ogram..*/.struct
7e7d0 20 54 72 69 67 67 65 72 50 72 67 20 7b 0a 20 20 TriggerPrg {.
7e7e0 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 Trigger *pTrigge
7e7f0 72 3b 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67 r; /* Trigg
7e800 65 72 20 74 68 69 73 20 70 72 6f 67 72 61 6d 20 er this program
7e810 77 61 73 20 63 6f 64 65 64 20 66 72 6f 6d 20 2a was coded from *
7e820 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a /. TriggerPrg *
7e830 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e pNext; /* N
7e840 65 78 74 20 65 6e 74 72 79 20 69 6e 20 50 61 72 ext entry in Par
7e850 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 6c se.pTriggerPrg l
7e860 69 73 74 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 ist */. SubProg
7e870 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 ram *pProgram;
7e880 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6d 70 6c /* Program impl
7e890 65 6d 65 6e 74 69 6e 67 20 70 54 72 69 67 67 65 ementing pTrigge
7e8a0 72 2f 6f 72 63 6f 6e 66 20 2a 2f 0a 20 20 69 6e r/orconf */. in
7e8b0 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 t orconf;
7e8c0 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 /* Default
7e8d0 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c ON CONFLICT pol
7e8e0 69 63 79 20 2a 2f 0a 20 20 75 33 32 20 61 43 6f icy */. u32 aCo
7e8f0 6c 6d 61 73 6b 5b 32 5d 3b 20 20 20 20 20 20 20 lmask[2];
7e900 20 2f 2a 20 4d 61 73 6b 73 20 6f 66 20 6f 6c 64 /* Masks of old
7e910 2e 2a 2c 20 6e 65 77 2e 2a 20 63 6f 6c 75 6d 6e .*, new.* column
7e920 73 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 7d 3b s accessed */.};
7e930 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 79 44 62 4d ../*.** The yDbM
7e940 61 73 6b 20 64 61 74 61 74 79 70 65 20 66 6f 72 ask datatype for
7e950 20 74 68 65 20 62 69 74 6d 61 73 6b 20 6f 66 20 the bitmask of
7e960 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 all attached dat
7e970 61 62 61 73 65 73 2e 0a 2a 2f 0a 23 69 66 20 53 abases..*/.#if S
7e980 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 QLITE_MAX_ATTACH
7e990 45 44 3e 33 30 0a 20 20 74 79 70 65 64 65 66 20 ED>30. typedef
7e9a0 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 79 sqlite3_uint64 y
7e9b0 44 62 4d 61 73 6b 3b 0a 23 65 6c 73 65 0a 20 20 DbMask;.#else.
7e9c0 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 typedef unsigned
7e9d0 20 69 6e 74 20 79 44 62 4d 61 73 6b 3b 0a 23 65 int yDbMask;.#e
7e9e0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 ndif../*.** An S
7e9f0 51 4c 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 QL parser contex
7ea00 74 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 74 68 t. A copy of th
7ea10 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 is structure is
7ea20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a 2a passed through.*
7ea30 2a 20 74 68 65 20 70 61 72 73 65 72 20 61 6e 64 * the parser and
7ea40 20 64 6f 77 6e 20 69 6e 74 6f 20 61 6c 6c 20 74 down into all t
7ea50 68 65 20 70 61 72 73 65 72 20 61 63 74 69 6f 6e he parser action
7ea60 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 routine in orde
7ea70 72 20 74 6f 0a 2a 2a 20 63 61 72 72 79 20 61 72 r to.** carry ar
7ea80 6f 75 6e 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ound information
7ea90 20 74 68 61 74 20 69 73 20 67 6c 6f 62 61 6c 20 that is global
7eaa0 74 6f 20 74 68 65 20 65 6e 74 69 72 65 20 70 61 to the entire pa
7eab0 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 rse..**.** The s
7eac0 74 72 75 63 74 75 72 65 20 69 73 20 64 69 76 69 tructure is divi
7ead0 64 65 64 20 69 6e 74 6f 20 74 77 6f 20 70 61 72 ded into two par
7eae0 74 73 2e 20 20 57 68 65 6e 20 74 68 65 20 70 61 ts. When the pa
7eaf0 72 73 65 72 20 61 6e 64 20 63 6f 64 65 0a 2a 2a rser and code.**
7eb00 20 67 65 6e 65 72 61 74 65 20 63 61 6c 6c 20 74 generate call t
7eb10 68 65 6d 73 65 6c 76 65 73 20 72 65 63 75 72 73 hemselves recurs
7eb20 69 76 65 6c 79 2c 20 74 68 65 20 66 69 72 73 74 ively, the first
7eb30 20 70 61 72 74 20 6f 66 20 74 68 65 20 73 74 72 part of the str
7eb40 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 63 6f 6e ucture.** is con
7eb50 73 74 61 6e 74 20 62 75 74 20 74 68 65 20 73 65 stant but the se
7eb60 63 6f 6e 64 20 70 61 72 74 20 69 73 20 72 65 73 cond part is res
7eb70 65 74 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e et at the beginn
7eb80 69 6e 67 20 61 6e 64 20 65 6e 64 20 6f 66 0a 2a ing and end of.*
7eb90 2a 20 65 61 63 68 20 72 65 63 75 72 73 69 6f 6e * each recursion
7eba0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 54 61 62 ..**.** The nTab
7ebb0 6c 65 4c 6f 63 6b 20 61 6e 64 20 61 54 61 62 6c leLock and aTabl
7ebc0 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 73 20 eLock variables
7ebd0 61 72 65 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 are only used if
7ebe0 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 the shared-cach
7ebf0 65 20 0a 2a 2a 20 66 65 61 74 75 72 65 20 69 73 e .** feature is
7ec00 20 65 6e 61 62 6c 65 64 20 28 69 66 20 73 71 6c enabled (if sql
7ec10 69 74 65 33 54 73 64 28 29 2d 3e 75 73 65 53 68 ite3Tsd()->useSh
7ec20 61 72 65 64 44 61 74 61 20 69 73 20 74 72 75 65 aredData is true
7ec30 29 2e 20 54 68 65 79 20 61 72 65 0a 2a 2a 20 75 ). They are.** u
7ec40 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 sed to store the
7ec50 20 73 65 74 20 6f 66 20 74 61 62 6c 65 2d 6c 6f set of table-lo
7ec60 63 6b 73 20 72 65 71 75 69 72 65 64 20 62 79 20 cks required by
7ec70 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65 the statement be
7ec80 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2e ing.** compiled.
7ec90 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 Function sqlite
7eca0 33 54 61 62 6c 65 4c 6f 63 6b 28 29 20 69 73 20 3TableLock() is
7ecb0 75 73 65 64 20 74 6f 20 61 64 64 20 65 6e 74 72 used to add entr
7ecc0 69 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 69 ies to the.** li
7ecd0 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 st..*/.struct Pa
7ece0 72 73 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 rse {. sqlite3
7ecf0 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 *db; /*
7ed00 54 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 The main databas
7ed10 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 e structure */.
7ed20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 char *zErrMsg;
7ed30 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f /* An erro
7ed40 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 56 r message */. V
7ed50 64 62 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 dbe *pVdbe;
7ed60 20 20 20 20 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 /* An engine
7ed70 20 66 6f 72 20 65 78 65 63 75 74 69 6e 67 20 64 for executing d
7ed80 61 74 61 62 61 73 65 20 62 79 74 65 63 6f 64 65 atabase bytecode
7ed90 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
7eda0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
7edb0 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 turn code from e
7edc0 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 xecution */. u8
7edd0 20 63 6f 6c 4e 61 6d 65 73 53 65 74 3b 20 20 20 colNamesSet;
7ede0 20 20 20 2f 2a 20 54 52 55 45 20 61 66 74 65 72 /* TRUE after
7edf0 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61 6d 65 20 68 OP_ColumnName h
7ee00 61 73 20 62 65 65 6e 20 69 73 73 75 65 64 20 74 as been issued t
7ee10 6f 20 70 56 64 62 65 20 2a 2f 0a 20 20 75 38 20 o pVdbe */. u8
7ee20 63 68 65 63 6b 53 63 68 65 6d 61 3b 20 20 20 20 checkSchema;
7ee30 20 20 2f 2a 20 43 61 75 73 65 73 20 73 63 68 65 /* Causes sche
7ee40 6d 61 20 63 6f 6f 6b 69 65 20 63 68 65 63 6b 20 ma cookie check
7ee50 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 2a after an error *
7ee60 2f 0a 20 20 75 38 20 6e 65 73 74 65 64 3b 20 20 /. u8 nested;
7ee70 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
7ee80 65 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c er of nested cal
7ee90 6c 73 20 74 6f 20 74 68 65 20 70 61 72 73 65 72 ls to the parser
7eea0 2f 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 /code generator
7eeb0 2a 2f 0a 20 20 75 38 20 6e 54 65 6d 70 52 65 67 */. u8 nTempReg
7eec0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
7eed0 62 65 72 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 ber of temporary
7eee0 20 72 65 67 69 73 74 65 72 73 20 69 6e 20 61 54 registers in aT
7eef0 65 6d 70 52 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 empReg[] */. u8
7ef00 20 6e 54 65 6d 70 49 6e 55 73 65 3b 20 20 20 20 nTempInUse;
7ef10 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
7ef20 61 54 65 6d 70 52 65 67 5b 5d 20 63 75 72 72 65 aTempReg[] curre
7ef30 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 ntly checked out
7ef40 20 2a 2f 0a 20 20 75 38 20 6e 43 6f 6c 43 61 63 */. u8 nColCac
7ef50 68 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 he; /* Nu
7ef60 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
7ef70 69 6e 20 61 43 6f 6c 43 61 63 68 65 5b 5d 20 2a in aColCache[] *
7ef80 2f 0a 20 20 75 38 20 69 43 6f 6c 43 61 63 68 65 /. u8 iColCache
7ef90 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 ; /* Next
7efa0 20 65 6e 74 72 79 20 69 6e 20 61 43 6f 6c 43 61 entry in aColCa
7efb0 63 68 65 5b 5d 20 74 6f 20 72 65 70 6c 61 63 65 che[] to replace
7efc0 20 2a 2f 0a 20 20 75 38 20 69 73 4d 75 6c 74 69 */. u8 isMulti
7efd0 57 72 69 74 65 3b 20 20 20 20 20 2f 2a 20 54 72 Write; /* Tr
7efe0 75 65 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20 ue if statement
7eff0 6d 61 79 20 6d 6f 64 69 66 79 2f 69 6e 73 65 72 may modify/inser
7f000 74 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 t multiple rows
7f010 2a 2f 0a 20 20 75 38 20 6d 61 79 41 62 6f 72 74 */. u8 mayAbort
7f020 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
7f030 65 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20 6d e if statement m
7f040 61 79 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 ay throw an ABOR
7f050 54 20 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 T exception */.
7f060 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38 5d int aTempReg[8]
7f070 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 ; /* Holding
7f080 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f 72 area for tempor
7f090 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a 2f ary registers */
7f0a0 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65 67 . int nRangeReg
7f0b0 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 ; /* Size
7f0c0 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 of the temporary
7f0d0 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b 20 register block
7f0e0 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65 52 */. int iRangeR
7f0f0 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 eg; /* Fir
7f100 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 st register in t
7f110 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 emporary registe
7f120 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 r block */. int
7f130 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 20 nErr;
7f140 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
7f150 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20 20 rrors seen */.
7f160 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20 20 int nTab;
7f170 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
7f180 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c f previously all
7f190 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72 73 ocated VDBE curs
7f1a0 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 ors */. int nMe
7f1b0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a m; /*
7f1c0 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 Number of memor
7f1d0 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f 20 y cells used so
7f1e0 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65 far */. int nSe
7f1f0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
7f200 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73 20 Number of sets
7f210 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 used so far */.
7f220 20 69 6e 74 20 6e 4f 6e 63 65 3b 20 20 20 20 20 int nOnce;
7f230 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
7f240 6f 66 20 4f 50 5f 4f 6e 63 65 20 69 6e 73 74 72 of OP_Once instr
7f250 75 63 74 69 6f 6e 73 20 73 6f 20 66 61 72 20 2a uctions so far *
7f260 2f 0a 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 /. int ckBase;
7f270 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 /* Base
7f280 20 72 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 register of dat
7f290 61 20 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 a during check c
7f2a0 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 onstraints */.
7f2b0 69 6e 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b int iCacheLevel;
7f2c0 20 20 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65 /* ColCache
7f2d0 20 76 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c valid when aCol
7f2e0 43 61 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d Cache[].iLevel<=
7f2f0 69 43 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20 iCacheLevel */.
7f300 20 69 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20 int iCacheCnt;
7f310 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 /* Counter
7f320 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 used to generat
7f330 65 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72 e aColCache[].lr
7f340 75 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 73 74 u values */. st
7f350 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 7b ruct yColCache {
7f360 0a 20 20 20 20 69 6e 74 20 69 54 61 62 6c 65 3b . int iTable;
7f370 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 /* Ta
7f380 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 ble cursor numbe
7f390 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f r */. int iCo
7f3a0 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f lumn; /
7f3b0 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 6e * Table column n
7f3c0 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 umber */. u8
7f3d0 74 65 6d 70 52 65 67 3b 20 20 20 20 20 20 20 20 tempReg;
7f3e0 20 20 20 2f 2a 20 69 52 65 67 20 69 73 20 61 20 /* iReg is a
7f3f0 74 65 6d 70 20 72 65 67 69 73 74 65 72 20 74 68 temp register th
7f400 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66 at needs to be f
7f410 72 65 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 reed */. int
7f420 69 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 iLevel;
7f430 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 6c 65 76 /* Nesting lev
7f440 65 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 52 el */. int iR
7f450 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 eg;
7f460 2f 2a 20 52 65 67 20 77 69 74 68 20 76 61 6c 75 /* Reg with valu
7f470 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e e of this column
7f480 2e 20 30 20 6d 65 61 6e 73 20 6e 6f 6e 65 2e 20 . 0 means none.
7f490 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 75 3b 20 */. int lru;
7f4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
7f4b0 4c 65 61 73 74 20 72 65 63 65 6e 74 6c 79 20 75 Least recently u
7f4c0 73 65 64 20 65 6e 74 72 79 20 68 61 73 20 74 68 sed entry has th
7f4d0 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65 e smallest value
7f4e0 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 61 63 68 */. } aColCach
7f4f0 65 5b 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 e[SQLITE_N_COLCA
7f500 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e 65 20 66 6f CHE]; /* One fo
7f510 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 63 61 r each column ca
7f520 63 68 65 20 65 6e 74 72 79 20 2a 2f 0a 20 20 79 che entry */. y
7f530 44 62 4d 61 73 6b 20 77 72 69 74 65 4d 61 73 6b DbMask writeMask
7f540 3b 20 20 20 2f 2a 20 53 74 61 72 74 20 61 20 77 ; /* Start a w
7f550 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
7f560 20 6f 6e 20 74 68 65 73 65 20 64 61 74 61 62 61 on these databa
7f570 73 65 73 20 2a 2f 0a 20 20 79 44 62 4d 61 73 6b ses */. yDbMask
7f580 20 63 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 2f 2a cookieMask; /*
7f590 20 42 69 74 6d 61 73 6b 20 6f 66 20 73 63 68 65 Bitmask of sche
7f5a0 6d 61 20 76 65 72 69 66 69 65 64 20 64 61 74 61 ma verified data
7f5b0 62 61 73 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 bases */. int c
7f5c0 6f 6f 6b 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 ookieGoto;
7f5d0 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 /* Address of OP
7f5e0 5f 47 6f 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 _Goto to cookie
7f5f0 76 65 72 69 66 69 65 72 20 73 75 62 72 6f 75 74 verifier subrout
7f600 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f ine */. int coo
7f610 6b 69 65 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f kieValue[SQLITE_
7f620 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b MAX_ATTACHED+2];
7f630 20 20 2f 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 /* Values of c
7f640 6f 6f 6b 69 65 73 20 74 6f 20 76 65 72 69 66 79 ookies to verify
7f650 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 */. int regRow
7f660 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 id; /* Re
7f670 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 gister holding r
7f680 6f 77 69 64 20 6f 66 20 43 52 45 41 54 45 20 54 owid of CREATE T
7f690 41 42 4c 45 20 65 6e 74 72 79 20 2a 2f 0a 20 20 ABLE entry */.
7f6a0 69 6e 74 20 72 65 67 52 6f 6f 74 3b 20 20 20 20 int regRoot;
7f6b0 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 /* Register
7f6c0 20 68 6f 6c 64 69 6e 67 20 72 6f 6f 74 20 70 61 holding root pa
7f6d0 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 6e 65 ge number for ne
7f6e0 77 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 69 w objects */. i
7f6f0 6e 74 20 6e 4d 61 78 41 72 67 3b 20 20 20 20 20 nt nMaxArg;
7f700 20 20 20 20 2f 2a 20 4d 61 78 20 61 72 67 73 20 /* Max args
7f710 70 61 73 73 65 64 20 74 6f 20 75 73 65 72 20 66 passed to user f
7f720 75 6e 63 74 69 6f 6e 20 62 79 20 73 75 62 2d 70 unction by sub-p
7f730 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 54 6f 6b 65 rogram */. Toke
7f740 6e 20 63 6f 6e 73 74 72 61 69 6e 74 4e 61 6d 65 n constraintName
7f750 3b 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 ;/* Name of the
7f760 63 6f 6e 73 74 72 61 69 6e 74 20 63 75 72 72 65 constraint curre
7f770 6e 74 6c 79 20 62 65 69 6e 67 20 70 61 72 73 65 ntly being parse
7f780 64 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c d */.#ifndef SQL
7f790 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
7f7a0 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 54 61 62 CACHE. int nTab
7f7b0 6c 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f leLock; /
7f7c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b * Number of lock
7f7d0 73 20 69 6e 20 61 54 61 62 6c 65 4c 6f 63 6b 20 s in aTableLock
7f7e0 2a 2f 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a */. TableLock *
7f7f0 61 54 61 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 aTableLock; /* R
7f800 65 71 75 69 72 65 64 20 74 61 62 6c 65 20 6c 6f equired table lo
7f810 63 6b 73 20 66 6f 72 20 73 68 61 72 65 64 2d 63 cks for shared-c
7f820 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e ache mode */.#en
7f830 64 69 66 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 dif. AutoincInf
7f840 6f 20 2a 70 41 69 6e 63 3b 20 20 2f 2a 20 49 6e o *pAinc; /* In
7f850 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
7f860 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 63 6f AUTOINCREMENT co
7f870 75 6e 74 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a 20 unters */.. /*
7f880 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 Information used
7f890 20 77 68 69 6c 65 20 63 6f 64 69 6e 67 20 74 72 while coding tr
7f8a0 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 20 igger programs.
7f8b0 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70 */. Parse *pTop
7f8c0 6c 65 76 65 6c 3b 20 20 20 20 2f 2a 20 50 61 72 level; /* Par
7f8d0 73 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 se structure for
7f8e0 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20 28 6f main program (o
7f8f0 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 54 61 62 r NULL) */. Tab
7f900 6c 65 20 2a 70 54 72 69 67 67 65 72 54 61 62 3b le *pTriggerTab;
7f910 20 20 2f 2a 20 54 61 62 6c 65 20 74 72 69 67 67 /* Table trigg
7f920 65 72 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f ers are being co
7f930 64 65 64 20 66 6f 72 20 2a 2f 0a 20 20 64 6f 75 ded for */. dou
7f940 62 6c 65 20 6e 51 75 65 72 79 4c 6f 6f 70 3b 20 ble nQueryLoop;
7f950 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 6e /* Estimated n
7f960 75 6d 62 65 72 20 6f 66 20 69 74 65 72 61 74 69 umber of iterati
7f970 6f 6e 73 20 6f 66 20 61 20 71 75 65 72 79 20 2a ons of a query *
7f980 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 73 6b 3b /. u32 oldmask;
7f990 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b /* Mask
7f9a0 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e of old.* column
7f9b0 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a s referenced */.
7f9c0 20 20 75 33 32 20 6e 65 77 6d 61 73 6b 3b 20 20 u32 newmask;
7f9d0 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f /* Mask o
7f9e0 66 20 6e 65 77 2e 2a 20 63 6f 6c 75 6d 6e 73 20 f new.* columns
7f9f0 72 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 referenced */.
7fa00 75 38 20 65 54 72 69 67 67 65 72 4f 70 3b 20 20 u8 eTriggerOp;
7fa10 20 20 20 20 20 2f 2a 20 54 4b 5f 55 50 44 41 54 /* TK_UPDAT
7fa20 45 2c 20 54 4b 5f 49 4e 53 45 52 54 20 6f 72 20 E, TK_INSERT or
7fa30 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 TK_DELETE */. u
7fa40 38 20 65 4f 72 63 6f 6e 66 3b 20 20 20 20 20 20 8 eOrconf;
7fa50 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f /* Default O
7fa60 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 N CONFLICT polic
7fa70 79 20 66 6f 72 20 74 72 69 67 67 65 72 20 73 74 y for trigger st
7fa80 65 70 73 20 2a 2f 0a 20 20 75 38 20 64 69 73 61 eps */. u8 disa
7fa90 62 6c 65 54 72 69 67 67 65 72 73 3b 20 20 2f 2a bleTriggers; /*
7faa0 20 54 72 75 65 20 74 6f 20 64 69 73 61 62 6c 65 True to disable
7fab0 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 0a 20 20 triggers */..
7fac0 2f 2a 20 41 62 6f 76 65 20 69 73 20 63 6f 6e 73 /* Above is cons
7fad0 74 61 6e 74 20 62 65 74 77 65 65 6e 20 72 65 63 tant between rec
7fae0 75 72 73 69 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 ursions. Below
7faf0 69 73 20 72 65 73 65 74 20 62 65 66 6f 72 65 20 is reset before
7fb00 61 6e 64 20 61 66 74 65 72 0a 20 20 2a 2a 20 65 and after. ** e
7fb10 61 63 68 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f ach recursion */
7fb20 0a 0a 20 20 69 6e 74 20 6e 56 61 72 3b 20 20 20 .. int nVar;
7fb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
7fb40 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27 20 76 Number of '?' v
7fb50 61 72 69 61 62 6c 65 73 20 73 65 65 6e 20 69 6e ariables seen in
7fb60 20 74 68 65 20 53 51 4c 20 73 6f 20 66 61 72 20 the SQL so far
7fb70 2a 2f 0a 20 20 69 6e 74 20 6e 7a 56 61 72 3b 20 */. int nzVar;
7fb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
7fb90 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 * Number of avai
7fba0 6c 61 62 6c 65 20 73 6c 6f 74 73 20 69 6e 20 61 lable slots in a
7fbb0 7a 56 61 72 5b 5d 20 2a 2f 0a 20 20 75 38 20 65 zVar[] */. u8 e
7fbc0 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 20 20 20 xplain;
7fbd0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
7fbe0 20 74 68 65 20 45 58 50 4c 41 49 4e 20 66 6c 61 the EXPLAIN fla
7fbf0 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e 20 74 68 g is found on th
7fc00 65 20 71 75 65 72 79 20 2a 2f 0a 23 69 66 6e 64 e query */.#ifnd
7fc10 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
7fc20 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 38 IRTUALTABLE. u8
7fc30 20 64 65 63 6c 61 72 65 56 74 61 62 3b 20 20 20 declareVtab;
7fc40 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
7fc50 69 66 20 69 6e 73 69 64 65 20 73 71 6c 69 74 65 if inside sqlite
7fc60 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 3_declare_vtab()
7fc70 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 74 61 62 4c */. int nVtabL
7fc80 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 ock;
7fc90 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 /* Number of vir
7fca0 74 75 61 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c tual tables to l
7fcb0 6f 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 ock */.#endif.
7fcc0 69 6e 74 20 6e 41 6c 69 61 73 3b 20 20 20 20 20 int nAlias;
7fcd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
7fce0 62 65 72 20 6f 66 20 61 6c 69 61 73 65 64 20 72 ber of aliased r
7fcf0 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e esult set column
7fd00 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 65 69 67 s */. int nHeig
7fd10 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ht;
7fd20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 /* Expression t
7fd30 72 65 65 20 68 65 69 67 68 74 20 6f 66 20 63 75 ree height of cu
7fd40 72 72 65 6e 74 20 73 75 62 2d 73 65 6c 65 63 74 rrent sub-select
7fd50 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
7fd60 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a TE_OMIT_EXPLAIN.
7fd70 20 20 69 6e 74 20 69 53 65 6c 65 63 74 49 64 3b int iSelectId;
7fd80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
7fd90 44 20 6f 66 20 63 75 72 72 65 6e 74 20 73 65 6c D of current sel
7fda0 65 63 74 20 66 6f 72 20 45 58 50 4c 41 49 4e 20 ect for EXPLAIN
7fdb0 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 output */. int
7fdc0 69 4e 65 78 74 53 65 6c 65 63 74 49 64 3b 20 20 iNextSelectId;
7fdd0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 61 76 /* Next av
7fde0 61 69 6c 61 62 6c 65 20 73 65 6c 65 63 74 20 49 ailable select I
7fdf0 44 20 66 6f 72 20 45 58 50 4c 41 49 4e 20 6f 75 D for EXPLAIN ou
7fe00 74 70 75 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 tput */.#endif.
7fe10 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20 20 char **azVar;
7fe20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
7fe30 69 6e 74 65 72 73 20 74 6f 20 6e 61 6d 65 73 20 inters to names
7fe40 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f of parameters */
7fe50 0a 20 20 56 64 62 65 20 2a 70 52 65 70 72 65 70 . Vdbe *pReprep
7fe60 61 72 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 are; /*
7fe70 56 4d 20 62 65 69 6e 67 20 72 65 70 72 65 70 61 VM being reprepa
7fe80 72 65 64 20 28 73 71 6c 69 74 65 33 52 65 70 72 red (sqlite3Repr
7fe90 65 70 61 72 65 28 29 29 20 2a 2f 0a 20 20 69 6e epare()) */. in
7fea0 74 20 2a 61 41 6c 69 61 73 3b 20 20 20 20 20 20 t *aAlias;
7feb0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 /* Regis
7fec0 74 65 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 ter used to hold
7fed0 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 aliased result
7fee0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
7fef0 2a 7a 54 61 69 6c 3b 20 20 20 20 20 20 20 20 2f *zTail; /
7ff00 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20 70 * All SQL text p
7ff10 61 73 74 20 74 68 65 20 6c 61 73 74 20 73 65 6d ast the last sem
7ff20 69 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a 2f icolon parsed */
7ff30 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 54 61 . Table *pNewTa
7ff40 62 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ble; /*
7ff50 41 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6f A table being co
7ff60 6e 73 74 72 75 63 74 65 64 20 62 79 20 43 52 45 nstructed by CRE
7ff70 41 54 45 20 54 41 42 4c 45 20 2a 2f 0a 20 20 54 ATE TABLE */. T
7ff80 72 69 67 67 65 72 20 2a 70 4e 65 77 54 72 69 67 rigger *pNewTrig
7ff90 67 65 72 3b 20 20 20 20 20 2f 2a 20 54 72 69 67 ger; /* Trig
7ffa0 67 65 72 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 ger under constr
7ffb0 75 63 74 20 62 79 20 61 20 43 52 45 41 54 45 20 uct by a CREATE
7ffc0 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 63 6f 6e TRIGGER */. con
7ffd0 73 74 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f st char *zAuthCo
7ffe0 6e 74 65 78 74 3b 20 2f 2a 20 54 68 65 20 36 74 ntext; /* The 6t
7fff0 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 64 h parameter to d
80000 62 2d 3e 78 41 75 74 68 20 63 61 6c 6c 62 61 63 b->xAuth callbac
80010 6b 73 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4e ks */. Token sN
80020 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 ameToken;
80030 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 20 /* Token with
80040 75 6e 71 75 61 6c 69 66 69 65 64 20 73 63 68 65 unqualified sche
80050 6d 61 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a ma object name *
80060 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 54 /. Token sLastT
80070 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a oken; /*
80080 20 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20 The last token
80090 70 61 72 73 65 64 20 2a 2f 0a 23 69 66 6e 64 65 parsed */.#ifnde
800a0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
800b0 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 54 6f 6b RTUALTABLE. Tok
800c0 65 6e 20 73 41 72 67 3b 20 20 20 20 20 20 20 20 en sArg;
800d0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 /* Comple
800e0 74 65 20 74 65 78 74 20 6f 66 20 61 20 6d 6f 64 te text of a mod
800f0 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a ule argument */.
80100 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62 Table **apVtab
80110 4c 6f 63 6b 3b 20 20 20 20 20 20 20 2f 2a 20 50 Lock; /* P
80120 6f 69 6e 74 65 72 20 74 6f 20 76 69 72 74 75 61 ointer to virtua
80130 6c 20 74 61 62 6c 65 73 20 6e 65 65 64 69 6e 67 l tables needing
80140 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 6e 64 locking */.#end
80150 69 66 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f 6d if. Table *pZom
80160 62 69 65 54 61 62 3b 20 20 20 20 20 20 20 20 2f bieTab; /
80170 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c 65 20 * List of Table
80180 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c 65 74 objects to delet
80190 65 20 61 66 74 65 72 20 63 6f 64 65 20 67 65 6e e after code gen
801a0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 */. TriggerPrg
801b0 20 2a 70 54 72 69 67 67 65 72 50 72 67 3b 20 20 *pTriggerPrg;
801c0 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f /* Linked list o
801d0 66 20 63 6f 64 65 64 20 74 72 69 67 67 65 72 73 f coded triggers
801e0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 */.};../*.** Re
801f0 74 75 72 6e 20 74 72 75 65 20 69 66 20 63 75 72 turn true if cur
80200 72 65 6e 74 6c 79 20 69 6e 73 69 64 65 20 61 6e rently inside an
80210 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 sqlite3_declare
80220 5f 76 74 61 62 28 29 20 63 61 6c 6c 2e 0a 2a 2f _vtab() call..*/
80230 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
80240 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
80250 0a 20 20 23 64 65 66 69 6e 65 20 49 4e 5f 44 45 . #define IN_DE
80260 43 4c 41 52 45 5f 56 54 41 42 20 30 0a 23 65 6c CLARE_VTAB 0.#el
80270 73 65 0a 20 20 23 64 65 66 69 6e 65 20 49 4e 5f se. #define IN_
80280 44 45 43 4c 41 52 45 5f 56 54 41 42 20 28 70 50 DECLARE_VTAB (pP
80290 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61 arse->declareVta
802a0 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a b).#endif../*.**
802b0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
802c0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
802d0 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 64 ructure can be d
802e0 65 63 6c 61 72 65 64 20 6f 6e 20 61 20 73 74 61 eclared on a sta
802f0 63 6b 20 61 6e 64 20 75 73 65 64 0a 2a 2a 20 74 ck and used.** t
80300 6f 20 73 61 76 65 20 74 68 65 20 50 61 72 73 65 o save the Parse
80310 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 76 61 .zAuthContext va
80320 6c 75 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 lue so that it c
80330 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 6c an be restored l
80340 61 74 65 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ater..*/.struct
80350 41 75 74 68 43 6f 6e 74 65 78 74 20 7b 0a 20 20 AuthContext {.
80360 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 75 74 const char *zAut
80370 68 43 6f 6e 74 65 78 74 3b 20 20 20 2f 2a 20 50 hContext; /* P
80380 75 74 20 73 61 76 65 64 20 50 61 72 73 65 2e 7a ut saved Parse.z
80390 41 75 74 68 43 6f 6e 74 65 78 74 20 68 65 72 65 AuthContext here
803a0 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 */. Parse *pPa
803b0 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 rse;
803c0 20 20 2f 2a 20 54 68 65 20 50 61 72 73 65 20 73 /* The Parse s
803d0 74 72 75 63 74 75 72 65 20 2a 2f 0a 7d 3b 0a 0a tructure */.};..
803e0 2f 2a 0a 2a 2a 20 42 69 74 66 69 65 6c 64 20 66 /*.** Bitfield f
803f0 6c 61 67 73 20 66 6f 72 20 50 35 20 76 61 6c 75 lags for P5 valu
80400 65 20 69 6e 20 76 61 72 69 6f 75 73 20 6f 70 63 e in various opc
80410 6f 64 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 odes..*/.#define
80420 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 OPFLAG_NCHANGE
80430 20 20 20 20 20 20 30 78 30 31 20 20 20 20 2f 2a 0x01 /*
80440 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 64 Set to update d
80450 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 23 64 b->nChange */.#d
80460 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c 41 53 efine OPFLAG_LAS
80470 54 52 4f 57 49 44 20 20 20 20 20 30 78 30 32 20 TROWID 0x02
80480 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 /* Set to upd
80490 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 ate db->lastRowi
804a0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 d */.#define OPF
804b0 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20 20 20 LAG_ISUPDATE
804c0 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 68 69 0x04 /* Thi
804d0 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 20 61 s OP_Insert is a
804e0 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a 2f 0a n sql UPDATE */.
804f0 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 41 #define OPFLAG_A
80500 50 50 45 4e 44 20 20 20 20 20 20 20 20 30 78 30 PPEND 0x0
80510 38 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 8 /* This is
80520 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 likely to be an
80530 61 70 70 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e append */.#defin
80540 65 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b e OPFLAG_USESEEK
80550 52 45 53 55 4c 54 20 30 78 31 30 20 20 20 20 2f RESULT 0x10 /
80560 2a 20 54 72 79 20 74 6f 20 61 76 6f 69 64 20 61 * Try to avoid a
80570 20 73 65 65 6b 20 69 6e 20 42 74 72 65 65 49 6e seek in BtreeIn
80580 73 65 72 74 28 29 20 2a 2f 0a 23 64 65 66 69 6e sert() */.#defin
80590 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 e OPFLAG_CLEARCA
805a0 43 48 45 20 20 20 20 30 78 32 30 20 20 20 20 2f CHE 0x20 /
805b0 2a 20 43 6c 65 61 72 20 70 73 65 75 64 6f 2d 74 * Clear pseudo-t
805c0 61 62 6c 65 20 63 61 63 68 65 20 69 6e 20 4f 50 able cache in OP
805d0 5f 43 6f 6c 75 6d 6e 20 2a 2f 0a 23 64 65 66 69 _Column */.#defi
805e0 6e 65 20 4f 50 46 4c 41 47 5f 4c 45 4e 47 54 48 ne OPFLAG_LENGTH
805f0 41 52 47 20 20 20 20 20 30 78 34 30 20 20 20 20 ARG 0x40
80600 2f 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 6c /* OP_Column onl
80610 79 20 75 73 65 64 20 66 6f 72 20 6c 65 6e 67 74 y used for lengt
80620 68 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f h() */.#define O
80630 50 46 4c 41 47 5f 54 59 50 45 4f 46 41 52 47 20 PFLAG_TYPEOFARG
80640 20 20 20 20 30 78 38 30 20 20 20 20 2f 2a 20 4f 0x80 /* O
80650 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 6c 79 20 75 73 P_Column only us
80660 65 64 20 66 6f 72 20 74 79 70 65 6f 66 28 29 20 ed for typeof()
80670 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 */.#define OPFLA
80680 47 5f 42 55 4c 4b 43 53 52 20 20 20 20 20 20 20 G_BULKCSR
80690 30 78 30 31 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 0x01 /* OP_Op
806a0 65 6e 2a 2a 20 75 73 65 64 20 74 6f 20 6f 70 65 en** used to ope
806b0 6e 20 62 75 6c 6b 20 63 75 72 73 6f 72 20 2a 2f n bulk cursor */
806c0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f .#define OPFLAG_
806d0 50 32 49 53 52 45 47 20 20 20 20 20 20 20 30 78 P2ISREG 0x
806e0 30 32 20 20 20 20 2f 2a 20 50 32 20 74 6f 20 4f 02 /* P2 to O
806f0 50 5f 4f 70 65 6e 2a 2a 20 69 73 20 61 20 72 65 P_Open** is a re
80700 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 2a 2f gister number */
80710 0a 0a 2f 2a 0a 20 2a 20 45 61 63 68 20 74 72 69 ../*. * Each tri
80720 67 67 65 72 20 70 72 65 73 65 6e 74 20 69 6e 20 gger present in
80730 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 the database sch
80740 65 6d 61 20 69 73 20 73 74 6f 72 65 64 20 61 73 ema is stored as
80750 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a an instance of.
80760 20 2a 20 73 74 72 75 63 74 20 54 72 69 67 67 65 * struct Trigge
80770 72 2e 20 0a 20 2a 0a 20 2a 20 50 6f 69 6e 74 65 r. . *. * Pointe
80780 72 73 20 74 6f 20 69 6e 73 74 61 6e 63 65 73 20 rs to instances
80790 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 of struct Trigge
807a0 72 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 r are stored in
807b0 74 77 6f 20 77 61 79 73 2e 0a 20 2a 20 31 2e 20 two ways.. * 1.
807c0 49 6e 20 74 68 65 20 22 74 72 69 67 48 61 73 68 In the "trigHash
807d0 22 20 68 61 73 68 20 74 61 62 6c 65 20 28 70 61 " hash table (pa
807e0 72 74 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 rt of the sqlite
807f0 33 2a 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 3* that represen
80800 74 73 20 74 68 65 20 0a 20 2a 20 20 20 20 64 61 ts the . * da
80810 74 61 62 61 73 65 29 2e 20 54 68 69 73 20 61 6c tabase). This al
80820 6c 6f 77 73 20 54 72 69 67 67 65 72 20 73 74 72 lows Trigger str
80830 75 63 74 75 72 65 73 20 74 6f 20 62 65 20 72 65 uctures to be re
80840 74 72 69 65 76 65 64 20 62 79 20 6e 61 6d 65 2e trieved by name.
80850 0a 20 2a 20 32 2e 20 41 6c 6c 20 74 72 69 67 67 . * 2. All trigg
80860 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ers associated w
80870 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74 61 62 ith a single tab
80880 6c 65 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 le form a linked
80890 20 6c 69 73 74 2c 20 75 73 69 6e 67 20 74 68 65 list, using the
808a0 0a 20 2a 20 20 20 20 70 4e 65 78 74 20 6d 65 6d . * pNext mem
808b0 62 65 72 20 6f 66 20 73 74 72 75 63 74 20 54 72 ber of struct Tr
808c0 69 67 67 65 72 2e 20 41 20 70 6f 69 6e 74 65 72 igger. A pointer
808d0 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6c to the first el
808e0 65 6d 65 6e 74 20 6f 66 20 74 68 65 0a 20 2a 20 ement of the. *
808f0 20 20 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 linked list i
80900 73 20 73 74 6f 72 65 64 20 61 73 20 74 68 65 20 s stored as the
80910 22 70 54 72 69 67 67 65 72 22 20 6d 65 6d 62 65 "pTrigger" membe
80920 72 20 6f 66 20 74 68 65 20 61 73 73 6f 63 69 61 r of the associa
80930 74 65 64 0a 20 2a 20 20 20 20 73 74 72 75 63 74 ted. * struct
80940 20 54 61 62 6c 65 2e 0a 20 2a 0a 20 2a 20 54 68 Table.. *. * Th
80950 65 20 22 73 74 65 70 5f 6c 69 73 74 22 20 6d 65 e "step_list" me
80960 6d 62 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 mber points to t
80970 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 he first element
80980 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 of a linked lis
80990 74 0a 20 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 t. * containing
809a0 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
809b0 74 73 20 73 70 65 63 69 66 69 65 64 20 61 73 20 ts specified as
809c0 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 the trigger prog
809d0 72 61 6d 2e 0a 20 2a 2f 0a 73 74 72 75 63 74 20 ram.. */.struct
809e0 54 72 69 67 67 65 72 20 7b 0a 20 20 63 68 61 72 Trigger {. char
809f0 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 *zName;
80a00 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 /* The name
80a10 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20 20 of the trigger
80a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80a30 20 20 20 20 20 20 2a 2f 0a 20 20 63 68 61 72 20 */. char
80a40 2a 74 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 *table;
80a50 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 /* The table
80a60 6f 72 20 76 69 65 77 20 74 6f 20 77 68 69 63 68 or view to which
80a70 20 74 68 65 20 74 72 69 67 67 65 72 20 61 70 70 the trigger app
80a80 6c 69 65 73 20 2a 2f 0a 20 20 75 38 20 6f 70 3b lies */. u8 op;
80a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80aa0 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 /* One of TK_D
80ab0 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 ELETE, TK_UPDATE
80ac0 2c 20 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 , TK_INSERT
80ad0 20 20 20 20 2a 2f 0a 20 20 75 38 20 74 72 5f 74 */. u8 tr_t
80ae0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
80af0 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 52 49 47 47 /* One of TRIGG
80b00 45 52 5f 42 45 46 4f 52 45 2c 20 54 52 49 47 47 ER_BEFORE, TRIGG
80b10 45 52 5f 41 46 54 45 52 20 2a 2f 0a 20 20 45 78 ER_AFTER */. Ex
80b20 70 72 20 2a 70 57 68 65 6e 3b 20 20 20 20 20 20 pr *pWhen;
80b30 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 /* The WHE
80b40 4e 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 N clause of the
80b50 65 78 70 72 65 73 73 69 6f 6e 20 28 6d 61 79 20 expression (may
80b60 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 49 64 be NULL) */. Id
80b70 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 73 3b 20 List *pColumns;
80b80 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 /* If this
80b90 20 69 73 20 61 6e 20 55 50 44 41 54 45 20 4f 46 is an UPDATE OF
80ba0 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 74 <column-list> t
80bb0 72 69 67 67 65 72 2c 0a 20 20 20 20 20 20 20 20 rigger,.
80bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80bd0 20 20 20 20 20 74 68 65 20 3c 63 6f 6c 75 6d 6e the <column
80be0 2d 6c 69 73 74 3e 20 69 73 20 73 74 6f 72 65 64 -list> is stored
80bf0 20 68 65 72 65 20 2a 2f 0a 20 20 53 63 68 65 6d here */. Schem
80c00 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 a *pSchema;
80c10 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e /* Schema con
80c20 74 61 69 6e 69 6e 67 20 74 68 65 20 74 72 69 67 taining the trig
80c30 67 65 72 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 ger */. Schema
80c40 2a 70 54 61 62 53 63 68 65 6d 61 3b 20 20 20 20 *pTabSchema;
80c50 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 /* Schema conta
80c60 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 ining the table
80c70 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 */. TriggerStep
80c80 20 2a 73 74 65 70 5f 6c 69 73 74 3b 20 2f 2a 20 *step_list; /*
80c90 4c 69 6e 6b 20 6c 69 73 74 20 6f 66 20 74 72 69 Link list of tri
80ca0 67 67 65 72 20 70 72 6f 67 72 61 6d 20 73 74 65 gger program ste
80cb0 70 73 20 20 20 20 20 20 20 20 20 20 20 20 20 2a ps *
80cc0 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4e 65 /. Trigger *pNe
80cd0 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e xt; /* N
80ce0 65 78 74 20 74 72 69 67 67 65 72 20 61 73 73 6f ext trigger asso
80cf0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
80d00 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a table */.};../*.
80d10 2a 2a 20 41 20 74 72 69 67 67 65 72 20 69 73 20 ** A trigger is
80d20 65 69 74 68 65 72 20 61 20 42 45 46 4f 52 45 20 either a BEFORE
80d30 6f 72 20 61 6e 20 41 46 54 45 52 20 74 72 69 67 or an AFTER trig
80d40 67 65 72 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 ger. The follow
80d50 69 6e 67 20 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a ing constants.**
80d60 20 64 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68 determine which
80d70 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 . .**.** If ther
80d80 65 20 61 72 65 20 6d 75 6c 74 69 70 6c 65 20 74 e are multiple t
80d90 72 69 67 67 65 72 73 2c 20 79 6f 75 20 6d 69 67 riggers, you mig
80da0 68 74 20 6f 66 20 73 6f 6d 65 20 42 45 46 4f 52 ht of some BEFOR
80db0 45 20 61 6e 64 20 73 6f 6d 65 20 41 46 54 45 52 E and some AFTER
80dc0 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20 63 61 73 ..** In that cas
80dd0 65 73 2c 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 es, the constant
80de0 73 20 62 65 6c 6f 77 20 63 61 6e 20 62 65 20 4f s below can be O
80df0 52 65 64 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2f Red together..*/
80e00 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45 52 .#define TRIGGER
80e10 5f 42 45 46 4f 52 45 20 20 31 0a 23 64 65 66 69 _BEFORE 1.#defi
80e20 6e 65 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 ne TRIGGER_AFTER
80e30 20 20 20 32 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 2../*. * An i
80e40 6e 73 74 61 6e 63 65 20 6f 66 20 73 74 72 75 63 nstance of struc
80e50 74 20 54 72 69 67 67 65 72 53 74 65 70 20 69 73 t TriggerStep is
80e60 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61 used to store a
80e70 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 single SQL stat
80e80 65 6d 65 6e 74 0a 20 2a 20 74 68 61 74 20 69 73 ement. * that is
80e90 20 61 20 70 61 72 74 20 6f 66 20 61 20 74 72 69 a part of a tri
80ea0 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 20 0a 20 gger-program. .
80eb0 2a 0a 20 2a 20 49 6e 73 74 61 6e 63 65 73 20 6f *. * Instances o
80ec0 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 f struct Trigger
80ed0 53 74 65 70 20 61 72 65 20 73 74 6f 72 65 64 20 Step are stored
80ee0 69 6e 20 61 20 73 69 6e 67 6c 79 20 6c 69 6e 6b in a singly link
80ef0 65 64 20 6c 69 73 74 20 28 6c 69 6e 6b 65 64 0a ed list (linked.
80f00 20 2a 20 75 73 69 6e 67 20 74 68 65 20 22 70 4e * using the "pN
80f10 65 78 74 22 20 6d 65 6d 62 65 72 29 20 72 65 66 ext" member) ref
80f20 65 72 65 6e 63 65 64 20 62 79 20 74 68 65 20 22 erenced by the "
80f30 73 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 step_list" membe
80f40 72 20 6f 66 20 74 68 65 20 0a 20 2a 20 61 73 73 r of the . * ass
80f50 6f 63 69 61 74 65 64 20 73 74 72 75 63 74 20 54 ociated struct T
80f60 72 69 67 67 65 72 20 69 6e 73 74 61 6e 63 65 2e rigger instance.
80f70 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 The first eleme
80f80 6e 74 20 6f 66 20 74 68 65 20 6c 69 6e 6b 65 64 nt of the linked
80f90 20 6c 69 73 74 20 69 73 0a 20 2a 20 74 68 65 20 list is. * the
80fa0 66 69 72 73 74 20 73 74 65 70 20 6f 66 20 74 68 first step of th
80fb0 65 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 e trigger-progra
80fc0 6d 2e 0a 20 2a 20 0a 20 2a 20 54 68 65 20 22 6f m.. * . * The "o
80fd0 70 22 20 6d 65 6d 62 65 72 20 69 6e 64 69 63 61 p" member indica
80fe0 74 65 73 20 77 68 65 74 68 65 72 20 74 68 69 73 tes whether this
80ff0 20 69 73 20 61 20 22 44 45 4c 45 54 45 22 2c 20 is a "DELETE",
81000 22 49 4e 53 45 52 54 22 2c 20 22 55 50 44 41 54 "INSERT", "UPDAT
81010 45 22 20 6f 72 0a 20 2a 20 22 53 45 4c 45 43 54 E" or. * "SELECT
81020 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 " statement. The
81030 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 meanings of the
81040 20 6f 74 68 65 72 20 6d 65 6d 62 65 72 73 20 69 other members i
81050 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 s determined by
81060 74 68 65 20 0a 20 2a 20 76 61 6c 75 65 20 6f 66 the . * value of
81070 20 22 6f 70 22 20 61 73 20 66 6f 6c 6c 6f 77 73 "op" as follows
81080 3a 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 :. *. * (op == T
81090 4b 5f 49 4e 53 45 52 54 29 0a 20 2a 20 6f 72 63 K_INSERT). * orc
810a0 6f 6e 66 20 20 20 20 2d 3e 20 73 74 6f 72 65 73 onf -> stores
810b0 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 the ON CONFLICT
810c0 20 61 6c 67 6f 72 69 74 68 6d 0a 20 2a 20 70 53 algorithm. * pS
810d0 65 6c 65 63 74 20 20 20 2d 3e 20 49 66 20 74 68 elect -> If th
810e0 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 is is an INSERT
810f0 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 20 INTO ... SELECT
81100 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 ... statement, t
81110 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 hen. *
81120 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 73 20 this stores
81130 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
81140 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
81150 74 2e 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c t. Otherwise NUL
81160 4c 2e 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 L.. * target
81170 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 -> A token holdi
81180 6e 67 20 74 68 65 20 71 75 6f 74 65 64 20 6e 61 ng the quoted na
81190 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
811a0 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 0a to insert into..
811b0 20 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 * pExprList ->
811c0 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e If this is an IN
811d0 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 SERT INTO ... VA
811e0 4c 55 45 53 20 2e 2e 2e 20 73 74 61 74 65 6d 65 LUES ... stateme
811f0 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 nt, then. *
81200 20 20 20 20 20 20 20 20 20 74 68 69 73 20 73 74 this st
81210 6f 72 65 73 20 76 61 6c 75 65 73 20 74 6f 20 62 ores values to b
81220 65 20 69 6e 73 65 72 74 65 64 2e 20 4f 74 68 65 e inserted. Othe
81230 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 rwise NULL.. * p
81240 49 64 4c 69 73 74 20 20 20 2d 3e 20 49 66 20 74 IdList -> If t
81250 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 his is an INSERT
81260 20 49 4e 54 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 INTO ... (<colu
81270 6d 6e 2d 6e 61 6d 65 73 3e 29 20 56 41 4c 55 45 mn-names>) VALUE
81280 53 20 2e 2e 2e 20 0a 20 2a 20 20 20 20 20 20 20 S ... . *
81290 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 statement
812a0 2c 20 74 68 65 6e 20 74 68 69 73 20 73 74 6f 72 , then this stor
812b0 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 es the column-na
812c0 6d 65 73 20 74 6f 20 62 65 0a 20 2a 20 20 20 20 mes to be. *
812d0 20 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 insert
812e0 65 64 20 69 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 ed into.. *. * (
812f0 6f 70 20 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29 op == TK_DELETE)
81300 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e . * target ->
81310 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 A token holding
81320 20 74 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 the quoted name
81330 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f of the table to
81340 20 64 65 6c 65 74 65 20 66 72 6f 6d 2e 0a 20 2a delete from.. *
81350 20 70 57 68 65 72 65 20 20 20 20 2d 3e 20 54 68 pWhere -> Th
81360 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f e WHERE clause o
81370 66 20 74 68 65 20 44 45 4c 45 54 45 20 73 74 61 f the DELETE sta
81380 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 tement if one is
81390 20 73 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 specified.. *
813a0 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 Othe
813b0 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a rwise NULL.. * .
813c0 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 * (op == TK_UPD
813d0 41 54 45 29 0a 20 2a 20 74 61 72 67 65 74 20 20 ATE). * target
813e0 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c -> A token hol
813f0 64 69 6e 67 20 74 68 65 20 71 75 6f 74 65 64 20 ding the quoted
81400 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c name of the tabl
81410 65 20 74 6f 20 75 70 64 61 74 65 20 72 6f 77 73 e to update rows
81420 20 6f 66 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 of.. * pWhere
81430 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 -> The WHERE c
81440 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44 lause of the UPD
81450 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 ATE statement if
81460 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 one is specifie
81470 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 d.. *
81480 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c Otherwise NUL
81490 4c 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73 74 20 L.. * pExprList
814a0 2d 3e 20 41 20 6c 69 73 74 20 6f 66 20 74 68 65 -> A list of the
814b0 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 75 70 64 61 columns to upda
814c0 74 65 20 61 6e 64 20 74 68 65 20 65 78 70 72 65 te and the expre
814d0 73 73 69 6f 6e 73 20 74 6f 20 75 70 64 61 74 65 ssions to update
814e0 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 . *
814f0 20 74 68 65 6d 20 74 6f 2e 20 53 65 65 20 73 71 them to. See sq
81500 6c 69 74 65 33 55 70 64 61 74 65 28 29 20 64 6f lite3Update() do
81510 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 22 cumentation of "
81520 70 43 68 61 6e 67 65 73 22 0a 20 2a 20 20 20 20 pChanges". *
81530 20 20 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 argume
81540 6e 74 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74 72 75 nt.. * . */.stru
81550 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20 7b ct TriggerStep {
81560 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 . u8 op;
81570 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f /* One o
81580 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f f TK_DELETE, TK_
81590 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 UPDATE, TK_INSER
815a0 54 2c 20 54 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a T, TK_SELECT */.
815b0 20 20 75 38 20 6f 72 63 6f 6e 66 3b 20 20 20 20 u8 orconf;
815c0 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c /* OE_Rol
815d0 6c 62 61 63 6b 20 65 74 63 2e 20 2a 2f 0a 20 20 lback etc. */.
815e0 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b 20 Trigger *pTrig;
815f0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 69 67 /* The trig
81600 67 65 72 20 74 68 61 74 20 74 68 69 73 20 73 74 ger that this st
81610 65 70 20 69 73 20 61 20 70 61 72 74 20 6f 66 20 ep is a part of
81620 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 */. Select *pSe
81630 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 53 45 4c lect; /* SEL
81640 45 43 54 20 73 74 61 74 6d 65 6e 74 20 6f 72 20 ECT statment or
81650 52 48 53 20 6f 66 20 49 4e 53 45 52 54 20 49 4e RHS of INSERT IN
81660 54 4f 20 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e TO .. SELECT ...
81670 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74 61 72 67 */. Token targ
81680 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 61 et; /* Ta
81690 72 67 65 74 20 74 61 62 6c 65 20 66 6f 72 20 44 rget table for D
816a0 45 4c 45 54 45 2c 20 55 50 44 41 54 45 2c 20 49 ELETE, UPDATE, I
816b0 4e 53 45 52 54 20 2a 2f 0a 20 20 45 78 70 72 20 NSERT */. Expr
816c0 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 20 *pWhere;
816d0 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 /* The WHERE cla
816e0 75 73 65 20 66 6f 72 20 44 45 4c 45 54 45 20 6f use for DELETE o
816f0 72 20 55 50 44 41 54 45 20 73 74 65 70 73 20 2a r UPDATE steps *
81700 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 /. ExprList *pE
81710 78 70 72 4c 69 73 74 3b 20 2f 2a 20 53 45 54 20 xprList; /* SET
81720 63 6c 61 75 73 65 20 66 6f 72 20 55 50 44 41 54 clause for UPDAT
81730 45 2e 20 20 56 41 4c 55 45 53 20 63 6c 61 75 73 E. VALUES claus
81740 65 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0a e for INSERT */.
81750 20 20 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73 IdList *pIdLis
81760 74 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e t; /* Column
81770 20 6e 61 6d 65 73 20 66 6f 72 20 49 4e 53 45 52 names for INSER
81780 54 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 T */. TriggerSt
81790 65 70 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e ep *pNext; /* N
817a0 65 78 74 20 69 6e 20 74 68 65 20 6c 69 6e 6b 2d ext in the link-
817b0 6c 69 73 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 list */. Trigge
817c0 72 53 74 65 70 20 2a 70 4c 61 73 74 3b 20 20 2f rStep *pLast; /
817d0 2a 20 4c 61 73 74 20 65 6c 65 6d 65 6e 74 20 69 * Last element i
817e0 6e 20 6c 69 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c n link-list. Val
817f0 69 64 20 66 6f 72 20 31 73 74 20 65 6c 65 6d 20 id for 1st elem
81800 6f 6e 6c 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a only */.};../*.*
81810 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
81820 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
81830 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 ns information u
81840 73 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 sed by the sqlit
81850 65 46 69 78 2e 2e 2e 0a 2a 2a 20 72 6f 75 74 69 eFix....** routi
81860 6e 65 73 20 61 73 20 74 68 65 79 20 77 61 6c 6b nes as they walk
81870 20 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 the parse tree
81880 74 6f 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65 to make database
81890 20 72 65 66 65 72 65 6e 63 65 73 0a 2a 2a 20 65 references.** e
818a0 78 70 6c 69 63 69 74 2e 20 20 0a 2a 2f 0a 74 79 xplicit. .*/.ty
818b0 70 65 64 65 66 20 73 74 72 75 63 74 20 44 62 46 pedef struct DbF
818c0 69 78 65 72 20 44 62 46 69 78 65 72 3b 0a 73 74 ixer DbFixer;.st
818d0 72 75 63 74 20 44 62 46 69 78 65 72 20 7b 0a 20 ruct DbFixer {.
818e0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 Parse *pParse;
818f0 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 /* The pars
81900 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 45 72 ing context. Er
81910 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 72 69 ror messages wri
81920 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 53 tten here */. S
81930 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 chema *pSchema;
81940 20 20 20 2f 2a 20 46 69 78 20 69 74 65 6d 73 20 /* Fix items
81950 74 6f 20 74 68 69 73 20 73 63 68 65 6d 61 20 2a to this schema *
81960 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
81970 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20 zDb; /* Make
81980 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74 73 sure all objects
81990 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 are contained i
819a0 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 n this database
819b0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
819c0 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70 65 *zType; /* Type
819d0 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 of the containe
819e0 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72 72 r - used for err
819f0 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 or messages */.
81a00 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 4e const Token *pN
81a10 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 ame; /* Name of
81a20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d 20 the container -
81a30 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d used for error m
81a40 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f essages */.};../
81a50 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 65 64 *.** An objected
81a60 20 75 73 65 64 20 74 6f 20 61 63 63 75 6d 75 6c used to accumul
81a70 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 20 ate the text of
81a80 61 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 77 a string where w
81a90 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63 65 e.** do not nece
81aa0 73 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 6f 77 ssarily know how
81ab0 20 62 69 67 20 74 68 65 20 73 74 72 69 6e 67 20 big the string
81ac0 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 20 65 will be in the e
81ad0 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 74 nd..*/.struct St
81ae0 72 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c 69 74 rAccum {. sqlit
81af0 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 e3 *db;
81b00 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74 61 /* Optional data
81b10 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 69 base for lookasi
81b20 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c 4c de. Can be NULL
81b30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 61 73 */. char *zBas
81b40 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 e; /* A
81b50 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e base allocation.
81b60 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c 6f Not from mallo
81b70 63 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 c. */. char *zT
81b80 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ext; /*
81b90 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c 65 The string colle
81ba0 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 cted so far */.
81bb0 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20 20 int nChar;
81bc0 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 /* Length
81bd0 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 73 6f of the string so
81be0 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e far */. int n
81bf0 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f Alloc; /
81c00 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 * Amount of spac
81c10 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 7a e allocated in z
81c20 54 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 20 6d Text */. int m
81c30 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f xAlloc; /
81c40 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 * Maximum allowe
81c50 64 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 20 d string length
81c60 2a 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c 6f 63 */. u8 malloc
81c70 46 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 65 63 Failed; /* Bec
81c80 6f 6d 65 73 20 74 72 75 65 20 69 66 20 61 6e 79 omes true if any
81c90 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
81ca0 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 75 38 on fails */. u8
81cb0 20 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 20 20 useMalloc;
81cc0 20 20 20 2f 2a 20 30 3a 20 6e 6f 6e 65 2c 20 20 /* 0: none,
81cd0 31 3a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 1: sqlite3DbMall
81ce0 6f 63 2c 20 20 32 3a 20 73 71 6c 69 74 65 33 5f oc, 2: sqlite3_
81cf0 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20 20 malloc */. u8
81d00 20 74 6f 6f 42 69 67 3b 20 20 20 20 20 20 20 20 tooBig;
81d10 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 /* Becomes true
81d20 20 69 66 20 73 74 72 69 6e 67 20 73 69 7a 65 20 if string size
81d30 65 78 63 65 65 64 73 20 6c 69 6d 69 74 73 20 2a exceeds limits *
81d40 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 6f /.};../*.** A po
81d50 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 inter to this st
81d60 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 ructure is used
81d70 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 69 to communicate i
81d80 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 72 nformation.** fr
81d90 6f 6d 20 73 71 6c 69 74 65 33 49 6e 69 74 20 61 om sqlite3Init a
81da0 6e 64 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d nd OP_ParseSchem
81db0 61 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 a into the sqlit
81dc0 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e 0a e3InitCallback..
81dd0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
81de0 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 t {. sqlite3 *d
81df0 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 b; /* The
81e00 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 database being
81e10 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 initialized */.
81e20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 char **pzErrMsg
81e30 3b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 ; /* Error me
81e40 73 73 61 67 65 20 73 74 6f 72 65 64 20 68 65 72 ssage stored her
81e50 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 e */. int iDb;
81e60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 /* 0
81e70 66 6f 72 20 6d 61 69 6e 20 64 61 74 61 62 61 73 for main databas
81e80 65 2e 20 20 31 20 66 6f 72 20 54 45 4d 50 2c 20 e. 1 for TEMP,
81e90 32 2e 2e 20 66 6f 72 20 41 54 54 41 43 48 65 64 2.. for ATTACHed
81ea0 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
81eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
81ec0 75 6c 74 20 63 6f 64 65 20 73 74 6f 72 65 64 20 ult code stored
81ed0 68 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44 61 here */.} InitDa
81ee0 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 ta;../*.** Struc
81ef0 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ture containing
81f00 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 global configura
81f10 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 74 68 tion data for th
81f20 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 e SQLite library
81f30 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 ..**.** This str
81f40 75 63 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e 74 ucture also cont
81f50 61 69 6e 73 20 73 6f 6d 65 20 73 74 61 74 65 20 ains some state
81f60 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
81f70 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f struct Sqlite3Co
81f80 6e 66 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d 65 nfig {. int bMe
81f90 6d 73 74 61 74 3b 20 20 20 20 20 20 20 20 20 20 mstat;
81fa0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
81fb0 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65 6d ue to enable mem
81fc0 6f 72 79 20 73 74 61 74 75 73 20 2a 2f 0a 20 20 ory status */.
81fd0 69 6e 74 20 62 43 6f 72 65 4d 75 74 65 78 3b 20 int bCoreMutex;
81fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
81ff0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 /* True to ena
82000 62 6c 65 20 63 6f 72 65 20 6d 75 74 65 78 69 6e ble core mutexin
82010 67 20 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c 6c g */. int bFull
82020 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 Mutex;
82030 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
82040 20 74 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c 20 to enable full
82050 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 6e mutexing */. in
82060 74 20 62 4f 70 65 6e 55 72 69 3b 20 20 20 20 20 t bOpenUri;
82070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82080 2f 2a 20 54 72 75 65 20 74 6f 20 69 6e 74 65 72 /* True to inter
82090 70 72 65 74 20 66 69 6c 65 6e 61 6d 65 73 20 61 pret filenames a
820a0 73 20 55 52 49 73 20 2a 2f 0a 20 20 69 6e 74 20 s URIs */. int
820b0 62 55 73 65 43 69 73 3b 20 20 20 20 20 20 20 20 bUseCis;
820c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
820d0 20 55 73 65 20 63 6f 76 65 72 69 6e 67 20 69 6e Use covering in
820e0 64 69 63 65 73 20 66 6f 72 20 66 75 6c 6c 2d 73 dices for full-s
820f0 63 61 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 cans */. int mx
82100 53 74 72 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 Strlen;
82110 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
82120 61 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65 aximum string le
82130 6e 67 74 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a ngth */. int sz
82140 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 Lookaside;
82150 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
82160 65 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 efault lookaside
82170 20 62 75 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a buffer size */.
82180 20 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65 int nLookaside
82190 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
821a0 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c /* Default l
821b0 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 ookaside buffer
821c0 63 6f 75 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 count */. sqlit
821d0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d e3_mem_methods m
821e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
821f0 4c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 Low-level memory
82200 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65 allocation inte
82210 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 rface */. sqlit
82220 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
82230 20 6d 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 mutex; /*
82240 4c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 Low-level mutex
82250 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 interface */. s
82260 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
82270 74 68 6f 64 73 32 20 70 63 61 63 68 65 32 3b 20 thods2 pcache2;
82280 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 70 61 /* Low-level pa
82290 67 65 2d 63 61 63 68 65 20 69 6e 74 65 72 66 61 ge-cache interfa
822a0 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 48 ce */. void *pH
822b0 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 eap;
822c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61 /* Hea
822d0 70 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 p storage space
822e0 2a 2f 0a 20 20 69 6e 74 20 6e 48 65 61 70 3b 20 */. int nHeap;
822f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82300 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
82310 66 20 70 48 65 61 70 5b 5d 20 2a 2f 0a 20 20 69 f pHeap[] */. i
82320 6e 74 20 6d 6e 52 65 71 2c 20 6d 78 52 65 71 3b nt mnReq, mxReq;
82330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82340 20 2f 2a 20 4d 69 6e 20 61 6e 64 20 6d 61 78 20 /* Min and max
82350 68 65 61 70 20 72 65 71 75 65 73 74 73 20 73 69 heap requests si
82360 7a 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 zes */. void *p
82370 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 Scratch;
82380 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 /* Sc
82390 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 2a 2f 0a ratch memory */.
823a0 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b int szScratch;
823b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
823c0 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 /* Size of e
823d0 61 63 68 20 73 63 72 61 74 63 68 20 62 75 66 66 ach scratch buff
823e0 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 63 72 er */. int nScr
823f0 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 atch;
82400 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
82410 62 65 72 20 6f 66 20 73 63 72 61 74 63 68 20 62 ber of scratch b
82420 75 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 uffers */. void
82430 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 *pPage;
82440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
82450 20 50 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f Page cache memo
82460 72 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 ry */. int szPa
82470 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
82480 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
82490 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 69 e of each page i
824a0 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 n pPage[] */. i
824b0 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 nt nPage;
824c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
824d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
824e0 67 65 73 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a ges in pPage[] *
824f0 2f 0a 20 20 69 6e 74 20 6d 78 50 61 72 73 65 72 /. int mxParser
82500 53 74 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 Stack;
82510 20 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d 75 6d /* maximum
82520 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 depth of the pa
82530 72 73 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 rser stack */.
82540 69 6e 74 20 73 68 61 72 65 64 43 61 63 68 65 45 int sharedCacheE
82550 6e 61 62 6c 65 64 3b 20 20 20 20 20 20 20 20 20 nabled;
82560 20 20 2f 2a 20 74 72 75 65 20 69 66 20 73 68 61 /* true if sha
82570 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 65 red-cache mode e
82580 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 2f 2a 20 54 nabled */. /* T
82590 68 65 20 61 62 6f 76 65 20 6d 69 67 68 74 20 62 he above might b
825a0 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f e initialized to
825b0 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54 68 65 20 non-zero. The
825c0 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65 64 20 74 following need t
825d0 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 69 6e o always. ** in
825e0 69 74 69 61 6c 6c 79 20 62 65 20 7a 65 72 6f 2c itially be zero,
825f0 20 68 6f 77 65 76 65 72 2e 20 2a 2f 0a 20 20 69 however. */. i
82600 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20 nt isInit;
82610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82620 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 69 /* True after i
82630 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68 61 nitialization ha
82640 73 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 s finished */.
82650 69 6e 74 20 69 6e 50 72 6f 67 72 65 73 73 3b 20 int inProgress;
82660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82670 20 20 2f 2a 20 54 72 75 65 20 77 68 69 6c 65 20 /* True while
82680 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 initialization i
82690 6e 20 70 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20 n progress */.
826a0 69 6e 74 20 69 73 4d 75 74 65 78 49 6e 69 74 3b int isMutexInit;
826b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
826c0 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 /* True after
826d0 6d 75 74 65 78 65 73 20 61 72 65 20 69 6e 69 74 mutexes are init
826e0 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 ialized */. int
826f0 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20 isMallocInit;
82700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
82710 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c * True after mal
82720 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a loc is initializ
82730 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 73 50 43 ed */. int isPC
82740 61 63 68 65 49 6e 69 74 3b 20 20 20 20 20 20 20 acheInit;
82750 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
82760 65 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63 20 69 e after malloc i
82770 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f s initialized */
82780 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
82790 20 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20 20 20 *pInitMutex;
827a0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 /* Mutex us
827b0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e ed by sqlite3_in
827c0 69 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a 20 20 itialize() */.
827d0 69 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75 74 65 int nRefInitMute
827e0 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
827f0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 /* Number of u
82800 73 65 72 73 20 6f 66 20 70 49 6e 69 74 4d 75 74 sers of pInitMut
82810 65 78 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 ex */. void (*x
82820 4c 6f 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 Log)(void*,int,c
82830 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 2f 2a 20 onst char*); /*
82840 46 75 6e 63 74 69 6f 6e 20 66 6f 72 20 6c 6f 67 Function for log
82850 67 69 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20 2a ging */. void *
82860 70 4c 6f 67 41 72 67 3b 20 20 20 20 20 20 20 20 pLogArg;
82870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
82880 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 * First argument
82890 20 74 6f 20 78 4c 6f 67 28 29 20 2a 2f 0a 20 20 to xLog() */.
828a0 69 6e 74 20 62 4c 6f 63 61 6c 74 69 6d 65 46 61 int bLocaltimeFa
828b0 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ult;
828c0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 66 61 69 /* True to fai
828d0 6c 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 63 61 l localtime() ca
828e0 6c 6c 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 lls */.#ifdef SQ
828f0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 51 4c 4c LITE_ENABLE_SQLL
82900 4f 47 0a 20 20 76 6f 69 64 28 2a 78 53 71 6c 6c OG. void(*xSqll
82910 6f 67 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 og)(void*,sqlite
82920 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 3*,const char*,
82930 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 2a 70 53 int);. void *pS
82940 71 6c 6c 6f 67 41 72 67 3b 0a 23 65 6e 64 69 66 qllogArg;.#endif
82950 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 65 .};../*.** Conte
82960 78 74 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65 xt pointer passe
82970 64 20 64 6f 77 6e 20 74 68 72 6f 75 67 68 20 74 d down through t
82980 68 65 20 74 72 65 65 2d 77 61 6c 6b 2e 0a 2a 2f he tree-walk..*/
82990 0a 73 74 72 75 63 74 20 57 61 6c 6b 65 72 20 7b .struct Walker {
829a0 0a 20 20 69 6e 74 20 28 2a 78 45 78 70 72 43 61 . int (*xExprCa
829b0 6c 6c 62 61 63 6b 29 28 57 61 6c 6b 65 72 2a 2c llback)(Walker*,
829c0 20 45 78 70 72 2a 29 3b 20 20 20 20 20 2f 2a 20 Expr*); /*
829d0 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 65 78 70 Callback for exp
829e0 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e ressions */. in
829f0 74 20 28 2a 78 53 65 6c 65 63 74 43 61 6c 6c 62 t (*xSelectCallb
82a00 61 63 6b 29 28 57 61 6c 6b 65 72 2a 2c 53 65 6c ack)(Walker*,Sel
82a10 65 63 74 2a 29 3b 20 20 2f 2a 20 43 61 6c 6c 62 ect*); /* Callb
82a20 61 63 6b 20 66 6f 72 20 53 45 4c 45 43 54 73 20 ack for SELECTs
82a30 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 */. Parse *pPar
82a40 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 se;
82a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
82a60 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 * Parser context
82a70 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 77 61 6c 6b . */. int walk
82a80 65 72 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 erDepth;
82a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82aa0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
82ab0 75 62 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 75 ubqueries */. u
82ac0 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 nion {
82ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82ae0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 /* Extr
82af0 61 20 64 61 74 61 20 66 6f 72 20 63 61 6c 6c 62 a data for callb
82b00 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 ack */. NameC
82b10 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 20 20 20 ontext *pNC;
82b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82b30 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 20 /* Naming
82b40 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 69 context */. i
82b50 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
82b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82b70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 /* Int
82b80 65 67 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20 eger value */.
82b90 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c SrcList *pSrcL
82ba0 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ist;
82bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
82bc0 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 FROM clause */.
82bd0 20 20 20 73 74 72 75 63 74 20 53 72 63 43 6f 75 struct SrcCou
82be0 6e 74 20 2a 70 53 72 63 43 6f 75 6e 74 3b 20 20 nt *pSrcCount;
82bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
82c00 20 43 6f 75 6e 74 69 6e 67 20 63 6f 6c 75 6d 6e Counting column
82c10 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 20 references */.
82c20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 } u;.};../* For
82c30 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e ward declaration
82c40 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 s */.SQLITE_PRIV
82c50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
82c60 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c alkExpr(Walker*,
82c70 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
82c80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
82c90 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 te3WalkExprList(
82ca0 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73 Walker*, ExprLis
82cb0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
82cc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
82cd0 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 alkSelect(Walker
82ce0 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c *, Select*);.SQL
82cf0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
82d00 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
82d10 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53 tExpr(Walker*, S
82d20 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f elect*);.SQLITE_
82d30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
82d40 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f te3WalkSelectFro
82d50 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 m(Walker*, Selec
82d60 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 t*);../*.** Retu
82d70 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 rn code from the
82d80 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b parse-tree walk
82d90 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 61 ing primitives a
82da0 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 6c 6c nd their.** call
82db0 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e backs..*/.#defin
82dc0 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20 e WRC_Continue
82dd0 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 0 /* Continu
82de0 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68 69 6c e down into chil
82df0 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 dren */.#define
82e00 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20 20 WRC_Prune
82e10 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 1 /* Omit chil
82e20 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69 6e 75 dren but continu
82e30 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c 69 6e e walking siblin
82e40 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 gs */.#define WR
82e50 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 32 20 C_Abort 2
82e60 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74 68 65 /* Abandon the
82e70 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f tree walk */../
82e80 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49 *.** Assuming zI
82e90 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 n points to the
82ea0 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 20 first byte of a
82eb0 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2c UTF-8 character,
82ec0 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 6e 20 .** advance zIn
82ed0 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 to point to the
82ee0 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 first byte of th
82ef0 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63 68 61 e next UTF-8 cha
82f00 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 racter..*/.#defi
82f10 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 ne SQLITE_SKIP_U
82f20 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 20 20 TF8(zIn) {
82f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82f40 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e \. if( (*(zIn
82f50 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 20 20 ++))>=0xc0 ){
82f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82f70 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
82f80 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20 while( (*zIn &
82f90 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a 0xc0)==0x80 ){ z
82fa0 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 20 20 In++; }
82fb0 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 \. }
82fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
82fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d \.}
82ff0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ../*.** The SQLI
83000 54 45 5f 2a 5f 42 4b 50 54 20 6d 61 63 72 6f 73 TE_*_BKPT macros
83010 20 61 72 65 20 73 75 62 73 74 69 74 75 74 65 73 are substitutes
83020 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72 20 63 for the error c
83030 6f 64 65 73 20 77 69 74 68 0a 2a 2a 20 74 68 65 odes with.** the
83040 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 same name but w
83050 69 74 68 6f 75 74 20 74 68 65 20 5f 42 4b 50 54 ithout the _BKPT
83060 20 73 75 66 66 69 78 2e 20 20 54 68 65 73 65 20 suffix. These
83070 6d 61 63 72 6f 73 20 69 6e 76 6f 6b 65 0a 2a 2a macros invoke.**
83080 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 72 routines that r
83090 65 70 6f 72 74 20 74 68 65 20 6c 69 6e 65 2d 6e eport the line-n
830a0 75 6d 62 65 72 20 6f 6e 20 77 68 69 63 68 20 74 umber on which t
830b0 68 65 20 65 72 72 6f 72 20 6f 72 69 67 69 6e 61 he error origina
830c0 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c ted.** using sql
830d0 69 74 65 33 5f 6c 6f 67 28 29 2e 20 20 54 68 65 ite3_log(). The
830e0 20 72 6f 75 74 69 6e 65 73 20 61 6c 73 6f 20 70 routines also p
830f0 72 6f 76 69 64 65 20 61 20 63 6f 6e 76 65 6e 69 rovide a conveni
83100 65 6e 74 20 70 6c 61 63 65 0a 2a 2a 20 74 6f 20 ent place.** to
83110 73 65 74 20 61 20 64 65 62 75 67 67 65 72 20 62 set a debugger b
83120 72 65 61 6b 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 reakpoint..*/.SQ
83130 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
83140 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 45 sqlite3CorruptE
83150 72 72 6f 72 28 69 6e 74 29 3b 0a 53 51 4c 49 54 rror(int);.SQLIT
83160 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
83170 6c 69 74 65 33 4d 69 73 75 73 65 45 72 72 6f 72 lite3MisuseError
83180 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 (int);.SQLITE_PR
83190 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
831a0 33 43 61 6e 74 6f 70 65 6e 45 72 72 6f 72 28 69 3CantopenError(i
831b0 6e 74 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c nt);.#define SQL
831c0 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
831d0 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 45 sqlite3CorruptE
831e0 72 72 6f 72 28 5f 5f 4c 49 4e 45 5f 5f 29 0a 23 rror(__LINE__).#
831f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49 define SQLITE_MI
83200 53 55 53 45 5f 42 4b 50 54 20 73 71 6c 69 74 65 SUSE_BKPT sqlite
83210 33 4d 69 73 75 73 65 45 72 72 6f 72 28 5f 5f 4c 3MisuseError(__L
83220 49 4e 45 5f 5f 29 0a 23 64 65 66 69 6e 65 20 53 INE__).#define S
83230 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 QLITE_CANTOPEN_B
83240 4b 50 54 20 73 71 6c 69 74 65 33 43 61 6e 74 6f KPT sqlite3Canto
83250 70 65 6e 45 72 72 6f 72 28 5f 5f 4c 49 4e 45 5f penError(__LINE_
83260 5f 29 0a 0a 0a 2f 2a 0a 2a 2a 20 46 54 53 34 20 _).../*.** FTS4
83270 69 73 20 72 65 61 6c 6c 79 20 61 6e 20 65 78 74 is really an ext
83280 65 6e 73 69 6f 6e 20 66 6f 72 20 46 54 53 33 2e ension for FTS3.
83290 20 20 49 74 20 69 73 20 65 6e 61 62 6c 65 64 20 It is enabled
832a0 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c using the.** SQL
832b0 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 ITE_ENABLE_FTS3
832c0 6d 61 63 72 6f 2e 20 20 42 75 74 20 74 6f 20 61 macro. But to a
832d0 76 6f 69 64 20 63 6f 6e 66 75 73 69 6f 6e 20 77 void confusion w
832e0 65 20 61 6c 73 6f 20 61 6c 6c 0a 2a 2a 20 74 68 e also all.** th
832f0 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f e SQLITE_ENABLE_
83300 46 54 53 34 20 6d 61 63 72 6f 20 74 6f 20 73 65 FTS4 macro to se
83310 72 76 65 20 61 73 20 61 6e 20 61 6c 69 73 73 65 rve as an alisse
83320 20 66 6f 72 20 53 51 4c 49 54 45 5f 45 4e 41 42 for SQLITE_ENAB
83330 4c 45 5f 46 54 53 33 2e 0a 2a 2f 0a 23 69 66 20 LE_FTS3..*/.#if
83340 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 defined(SQLITE_E
83350 4e 41 42 4c 45 5f 46 54 53 34 29 20 26 26 20 21 NABLE_FTS4) && !
83360 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 defined(SQLITE_E
83370 4e 41 42 4c 45 5f 46 54 53 33 29 0a 23 20 64 65 NABLE_FTS3).# de
83380 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 fine SQLITE_ENAB
83390 4c 45 5f 46 54 53 33 0a 23 65 6e 64 69 66 0a 0a LE_FTS3.#endif..
833a0 2f 2a 0a 2a 2a 20 54 68 65 20 63 74 79 70 65 2e /*.** The ctype.
833b0 68 20 68 65 61 64 65 72 20 69 73 20 6e 65 65 64 h header is need
833c0 65 64 20 66 6f 72 20 6e 6f 6e 2d 41 53 43 49 49 ed for non-ASCII
833d0 20 73 79 73 74 65 6d 73 2e 20 20 49 74 20 69 73 systems. It is
833e0 20 61 6c 73 6f 0a 2a 2a 20 6e 65 65 64 65 64 20 also.** needed
833f0 62 79 20 46 54 53 33 20 77 68 65 6e 20 46 54 53 by FTS3 when FTS
83400 33 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 3 is included in
83410 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f the amalgamatio
83420 6e 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e n..*/.#if !defin
83430 65 64 28 53 51 4c 49 54 45 5f 41 53 43 49 49 29 ed(SQLITE_ASCII)
83440 20 7c 7c 20 5c 0a 20 20 20 20 28 64 65 66 69 6e || \. (defin
83450 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ed(SQLITE_ENABLE
83460 5f 46 54 53 33 29 20 26 26 20 64 65 66 69 6e 65 _FTS3) && define
83470 64 28 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d d(SQLITE_AMALGAM
83480 41 54 49 4f 4e 29 29 0a 23 20 69 6e 63 6c 75 64 ATION)).# includ
83490 65 20 3c 63 74 79 70 65 2e 68 3e 0a 23 65 6e 64 e <ctype.h>.#end
834a0 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f if../*.** The fo
834b0 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d llowing macros m
834c0 69 6d 69 63 20 74 68 65 20 73 74 61 6e 64 61 72 imic the standar
834d0 64 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 d library functi
834e0 6f 6e 73 20 74 6f 75 70 70 65 72 28 29 2c 0a 2a ons toupper(),.*
834f0 2a 20 69 73 73 70 61 63 65 28 29 2c 20 69 73 61 * isspace(), isa
83500 6c 6e 75 6d 28 29 2c 20 69 73 64 69 67 69 74 28 lnum(), isdigit(
83510 29 20 61 6e 64 20 69 73 78 64 69 67 69 74 28 29 ) and isxdigit()
83520 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 , respectively.
83530 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 20 76 65 The.** sqlite ve
83540 72 73 69 6f 6e 73 20 6f 6e 6c 79 20 77 6f 72 6b rsions only work
83550 20 66 6f 72 20 41 53 43 49 49 20 63 68 61 72 61 for ASCII chara
83560 63 74 65 72 73 2c 20 72 65 67 61 72 64 6c 65 73 cters, regardles
83570 73 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a s of locale..*/.
83580 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 #ifdef SQLITE_AS
83590 43 49 49 0a 23 20 64 65 66 69 6e 65 20 73 71 6c CII.# define sql
835a0 69 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20 ite3Toupper(x)
835b0 28 28 78 29 26 7e 28 73 71 6c 69 74 65 33 43 74 ((x)&~(sqlite3Ct
835c0 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 ypeMap[(unsigned
835d0 20 63 68 61 72 29 28 78 29 5d 26 30 78 32 30 29 char)(x)]&0x20)
835e0 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
835f0 65 33 49 73 73 70 61 63 65 28 78 29 20 20 20 28 e3Isspace(x) (
83600 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b sqlite3CtypeMap[
83610 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 (unsigned char)(
83620 78 29 5d 26 30 78 30 31 29 0a 23 20 64 65 66 69 x)]&0x01).# defi
83630 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 ne sqlite3Isalnu
83640 6d 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 m(x) (sqlite3C
83650 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 typeMap[(unsigne
83660 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 36 d char)(x)]&0x06
83670 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
83680 65 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 28 e3Isalpha(x) (
83690 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b sqlite3CtypeMap[
836a0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 (unsigned char)(
836b0 78 29 5d 26 30 78 30 32 29 0a 23 20 64 65 66 69 x)]&0x02).# defi
836c0 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 ne sqlite3Isdigi
836d0 74 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 t(x) (sqlite3C
836e0 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 typeMap[(unsigne
836f0 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 34 d char)(x)]&0x04
83700 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
83710 65 33 49 73 78 64 69 67 69 74 28 78 29 20 20 28 e3Isxdigit(x) (
83720 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b sqlite3CtypeMap[
83730 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 (unsigned char)(
83740 78 29 5d 26 30 78 30 38 29 0a 23 20 64 65 66 69 x)]&0x08).# defi
83750 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 ne sqlite3Tolowe
83760 72 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 55 r(x) (sqlite3U
83770 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 pperToLower[(uns
83780 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 29 igned char)(x)])
83790 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
837a0 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 78 sqlite3Toupper(x
837b0 29 20 20 20 74 6f 75 70 70 65 72 28 28 75 6e 73 ) toupper((uns
837c0 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a igned char)(x)).
837d0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
837e0 49 73 73 70 61 63 65 28 78 29 20 20 20 69 73 73 Isspace(x) iss
837f0 70 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63 pace((unsigned c
83800 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e har)(x)).# defin
83810 65 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d e sqlite3Isalnum
83820 28 78 29 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 (x) isalnum((u
83830 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 nsigned char)(x)
83840 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
83850 65 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 69 e3Isalpha(x) i
83860 73 61 6c 70 68 61 28 28 75 6e 73 69 67 6e 65 64 salpha((unsigned
83870 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 char)(x)).# def
83880 69 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 ine sqlite3Isdig
83890 69 74 28 78 29 20 20 20 69 73 64 69 67 69 74 28 it(x) isdigit(
838a0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 (unsigned char)(
838b0 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c x)).# define sql
838c0 69 74 65 33 49 73 78 64 69 67 69 74 28 78 29 20 ite3Isxdigit(x)
838d0 20 69 73 78 64 69 67 69 74 28 28 75 6e 73 69 67 isxdigit((unsig
838e0 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 ned char)(x)).#
838f0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f define sqlite3To
83900 6c 6f 77 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 lower(x) tolow
83910 65 72 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 er((unsigned cha
83920 72 29 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f r)(x)).#endif../
83930 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 *.** Internal fu
83940 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 nction prototype
83950 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c s.*/.#define sql
83960 69 74 65 33 53 74 72 49 43 6d 70 20 73 71 6c 69 ite3StrICmp sqli
83970 74 65 33 5f 73 74 72 69 63 6d 70 0a 53 51 4c 49 te3_stricmp.SQLI
83980 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
83990 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 qlite3Strlen30(c
839a0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 64 65 onst char*);.#de
839b0 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 72 4e fine sqlite3StrN
839c0 49 43 6d 70 20 73 71 6c 69 74 65 33 5f 73 74 72 ICmp sqlite3_str
839d0 6e 69 63 6d 70 0a 0a 53 51 4c 49 54 45 5f 50 52 nicmp..SQLITE_PR
839e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
839f0 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 3MallocInit(void
83a00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
83a10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 E void sqlite3Ma
83a20 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b 0a 53 llocEnd(void);.S
83a30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
83a40 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f id *sqlite3Mallo
83a50 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 c(int);.SQLITE_P
83a60 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
83a70 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 ite3MallocZero(i
83a80 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
83a90 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
83aa0 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 3DbMallocZero(sq
83ab0 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 lite3*, int);.SQ
83ac0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
83ad0 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c d *sqlite3DbMall
83ae0 6f 63 52 61 77 28 73 71 6c 69 74 65 33 2a 2c 20 ocRaw(sqlite3*,
83af0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
83b00 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
83b10 65 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 e3DbStrDup(sqlit
83b20 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 e3*,const char*)
83b30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
83b40 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 char *sqlite3Db
83b50 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 2a StrNDup(sqlite3*
83b60 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e ,const char*, in
83b70 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
83b80 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
83b90 52 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 Realloc(void*, i
83ba0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
83bb0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
83bc0 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 3DbReallocOrFree
83bd0 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 (sqlite3 *, void
83be0 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 *, int);.SQLITE
83bf0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
83c00 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 qlite3DbRealloc(
83c10 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 sqlite3 *, void
83c20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
83c30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
83c40 69 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 ite3DbFree(sqlit
83c50 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c e3*, void*);.SQL
83c60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
83c70 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a sqlite3MallocSiz
83c80 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 e(void*);.SQLITE
83c90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
83ca0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 ite3DbMallocSize
83cb0 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a (sqlite3*, void*
83cc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
83cd0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 E void *sqlite3S
83ce0 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 cratchMalloc(int
83cf0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
83d00 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 E void sqlite3Sc
83d10 72 61 74 63 68 46 72 65 65 28 76 6f 69 64 2a 29 ratchFree(void*)
83d20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
83d30 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 void *sqlite3Pa
83d40 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 geMalloc(int);.S
83d50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
83d60 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 id sqlite3PageFr
83d70 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 ee(void*);.SQLIT
83d80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
83d90 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 qlite3MemSetDefa
83da0 75 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ult(void);.SQLIT
83db0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
83dc0 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c qlite3BenignMall
83dd0 6f 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29 ocHooks(void (*)
83de0 28 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 2a 29 (void), void (*)
83df0 28 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f (void));.SQLITE_
83e00 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
83e10 74 65 33 48 65 61 70 4e 65 61 72 6c 79 46 75 6c te3HeapNearlyFul
83e20 6c 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 l(void);../*.**
83e30 4f 6e 20 73 79 73 74 65 6d 73 20 77 69 74 68 20 On systems with
83e40 61 6d 70 6c 65 20 73 74 61 63 6b 20 73 70 61 63 ample stack spac
83e50 65 20 61 6e 64 20 74 68 61 74 20 73 75 70 70 6f e and that suppo
83e60 72 74 20 61 6c 6c 6f 63 61 28 29 2c 20 6d 61 6b rt alloca(), mak
83e70 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61 6c 6c 6f e.** use of allo
83e80 63 61 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 ca() to obtain s
83e90 70 61 63 65 20 66 6f 72 20 6c 61 72 67 65 20 61 pace for large a
83ea0 75 74 6f 6d 61 74 69 63 20 6f 62 6a 65 63 74 73 utomatic objects
83eb0 2e 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a . By default,.*
83ec0 2a 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66 * obtain space f
83ed0 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a rom malloc()..**
83ee0 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 28 29 .** The alloca()
83ef0 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 72 routine never r
83f00 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 20 20 54 68 eturns NULL. Th
83f10 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20 63 6f is will cause co
83f20 64 65 20 70 61 74 68 73 0a 2a 2a 20 74 68 61 74 de paths.** that
83f30 20 64 65 61 6c 20 77 69 74 68 20 73 71 6c 69 74 deal with sqlit
83f40 65 33 53 74 61 63 6b 41 6c 6c 6f 63 28 29 20 66 e3StackAlloc() f
83f50 61 69 6c 75 72 65 73 20 74 6f 20 62 65 20 75 6e ailures to be un
83f60 72 65 61 63 68 61 62 6c 65 2e 0a 2a 2f 0a 23 69 reachable..*/.#i
83f70 66 64 65 66 20 53 51 4c 49 54 45 5f 55 53 45 5f fdef SQLITE_USE_
83f80 41 4c 4c 4f 43 41 0a 23 20 64 65 66 69 6e 65 20 ALLOCA.# define
83f90 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f sqlite3StackAllo
83fa0 63 52 61 77 28 44 2c 4e 29 20 20 20 61 6c 6c 6f cRaw(D,N) allo
83fb0 63 61 28 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 ca(N).# define s
83fc0 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 qlite3StackAlloc
83fd0 5a 65 72 6f 28 44 2c 4e 29 20 20 6d 65 6d 73 65 Zero(D,N) memse
83fe0 74 28 61 6c 6c 6f 63 61 28 4e 29 2c 20 30 2c 20 t(alloca(N), 0,
83ff0 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 N).# define sqli
84000 74 65 33 53 74 61 63 6b 46 72 65 65 28 44 2c 50 te3StackFree(D,P
84010 29 20 20 20 20 20 20 20 0a 23 65 6c 73 65 0a 23 ) .#else.#
84020 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 define sqlite3S
84030 74 61 63 6b 41 6c 6c 6f 63 52 61 77 28 44 2c 4e tackAllocRaw(D,N
84040 29 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c ) sqlite3DbMal
84050 6c 6f 63 52 61 77 28 44 2c 4e 29 0a 23 20 64 65 locRaw(D,N).# de
84060 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 fine sqlite3Stac
84070 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 kAllocZero(D,N)
84080 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
84090 5a 65 72 6f 28 44 2c 4e 29 0a 23 20 64 65 66 69 Zero(D,N).# defi
840a0 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 ne sqlite3StackF
840b0 72 65 65 28 44 2c 50 29 20 20 20 20 20 20 20 73 ree(D,P) s
840c0 71 6c 69 74 65 33 44 62 46 72 65 65 28 44 2c 50 qlite3DbFree(D,P
840d0 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ).#endif..#ifdef
840e0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
840f0 45 4d 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 52 EMSYS3.SQLITE_PR
84100 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 IVATE const sqli
84110 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 te3_mem_methods
84120 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 *sqlite3MemGetMe
84130 6d 73 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 6e msys3(void);.#en
84140 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
84150 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 E_ENABLE_MEMSYS5
84160 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
84170 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 const sqlite3_me
84180 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 m_methods *sqlit
84190 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 e3MemGetMemsys5(
841a0 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a void);.#endif...
841b0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
841c0 55 54 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 45 UTEX_OMIT.SQLITE
841d0 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 _PRIVATE sqlit
841e0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
841f0 20 63 6f 6e 73 74 20 2a 73 71 6c 69 74 65 33 44 const *sqlite3D
84200 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 efaultMutex(void
84210 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
84220 45 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 E sqlite3_mute
84230 78 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 x_methods const
84240 2a 73 71 6c 69 74 65 33 4e 6f 6f 70 4d 75 74 65 *sqlite3NoopMute
84250 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f x(void);.SQLITE_
84260 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 PRIVATE sqlite
84270 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 3_mutex *sqlite3
84280 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 29 3b MutexAlloc(int);
84290 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
842a0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 int sqlite3Mut
842b0 65 78 49 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 exInit(void);.SQ
842c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
842d0 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 nt sqlite3MutexE
842e0 6e 64 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 nd(void);.#endif
842f0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
84300 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74 int sqlite3Stat
84310 75 73 56 61 6c 75 65 28 69 6e 74 29 3b 0a 53 51 usValue(int);.SQ
84320 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
84330 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 d sqlite3StatusA
84340 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 dd(int, int);.SQ
84350 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
84360 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 d sqlite3StatusS
84370 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 23 et(int, int);..#
84380 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
84390 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
843a0 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
843b0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 int sqlite3Is
843c0 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 23 65 6c NaN(double);.#el
843d0 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 se.# define sqli
843e0 74 65 33 49 73 4e 61 4e 28 58 29 20 20 30 0a 23 te3IsNaN(X) 0.#
843f0 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
84400 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
84410 65 33 56 58 50 72 69 6e 74 66 28 53 74 72 41 63 e3VXPrintf(StrAc
84420 63 75 6d 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 cum*, int, const
84430 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 char*, va_list)
84440 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
84450 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 53 51 4c 49 _OMIT_TRACE.SQLI
84460 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
84470 73 71 6c 69 74 65 33 58 50 72 69 6e 74 66 28 53 sqlite3XPrintf(S
84480 74 72 41 63 63 75 6d 2a 2c 20 63 6f 6e 73 74 20 trAccum*, const
84490 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 65 6e char*, ...);.#en
844a0 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
844b0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
844c0 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a MPrintf(sqlite3*
844d0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e ,const char*, ..
844e0 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .);.SQLITE_PRIVA
844f0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
84500 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 VMPrintf(sqlite3
84510 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 *,const char*, v
84520 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f a_list);.SQLITE_
84530 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
84540 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71 lite3MAppendf(sq
84550 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2c 63 6f 6e lite3*,char*,con
84560 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 st char*,...);.#
84570 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
84580 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e E_TEST) || defin
84590 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
845a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
845b0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 void sqlite3De
845c0 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 bugPrintf(const
845d0 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 65 6e char*, ...);.#en
845e0 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 dif.#if defined(
845f0 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 53 51 4c SQLITE_TEST).SQL
84600 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
84610 69 64 20 2a 73 71 6c 69 74 65 33 54 65 73 74 54 id *sqlite3TestT
84620 65 78 74 54 6f 50 74 72 28 63 6f 6e 73 74 20 63 extToPtr(const c
84630 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f har*);.#endif../
84640 2a 20 4f 75 74 70 75 74 20 66 6f 72 6d 61 74 74 * Output formatt
84650 69 6e 67 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 ing for SQLITE_T
84660 45 53 54 43 54 52 4c 5f 45 58 50 4c 41 49 4e 20 ESTCTRL_EXPLAIN
84670 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
84680 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 54 52 45 QLITE_ENABLE_TRE
84690 45 5f 45 58 50 4c 41 49 4e 29 0a 53 51 4c 49 54 E_EXPLAIN).SQLIT
846a0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
846b0 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 42 sqlite3ExplainB
846c0 65 67 69 6e 28 56 64 62 65 2a 29 3b 0a 53 51 4c egin(Vdbe*);.SQL
846d0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
846e0 69 64 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 id sqlite3Explai
846f0 6e 50 72 69 6e 74 66 28 56 64 62 65 2a 2c 20 63 nPrintf(Vdbe*, c
84700 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
84710 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
84720 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 void sqlite3E
84730 78 70 6c 61 69 6e 4e 4c 28 56 64 62 65 2a 29 3b xplainNL(Vdbe*);
84740 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
84750 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 void sqlite3Ex
84760 70 6c 61 69 6e 50 75 73 68 28 56 64 62 65 2a 29 plainPush(Vdbe*)
84770 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
84780 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 void sqlite3E
84790 78 70 6c 61 69 6e 50 6f 70 28 56 64 62 65 2a 29 xplainPop(Vdbe*)
847a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
847b0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 void sqlite3E
847c0 78 70 6c 61 69 6e 46 69 6e 69 73 68 28 56 64 62 xplainFinish(Vdb
847d0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
847e0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
847f0 65 33 45 78 70 6c 61 69 6e 53 65 6c 65 63 74 28 e3ExplainSelect(
84800 56 64 62 65 2a 2c 20 53 65 6c 65 63 74 2a 29 3b Vdbe*, Select*);
84810 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
84820 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 void sqlite3Ex
84830 70 6c 61 69 6e 45 78 70 72 28 56 64 62 65 2a 2c plainExpr(Vdbe*,
84840 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
84850 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
84860 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 qlite3ExplainExp
84870 72 4c 69 73 74 28 56 64 62 65 2a 2c 20 45 78 70 rList(Vdbe*, Exp
84880 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f rList*);.SQLITE_
84890 50 52 49 56 41 54 45 20 20 20 63 6f 6e 73 74 20 PRIVATE const
848a0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 64 62 char *sqlite3Vdb
848b0 65 45 78 70 6c 61 6e 61 74 69 6f 6e 28 56 64 62 eExplanation(Vdb
848c0 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 e*);.#else.# def
848d0 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70 6c 61 ine sqlite3Expla
848e0 69 6e 42 65 67 69 6e 28 58 29 0a 23 20 64 65 66 inBegin(X).# def
848f0 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70 6c 61 ine sqlite3Expla
84900 69 6e 53 65 6c 65 63 74 28 41 2c 42 29 0a 23 20 inSelect(A,B).#
84910 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78 define sqlite3Ex
84920 70 6c 61 69 6e 45 78 70 72 28 41 2c 42 29 0a 23 plainExpr(A,B).#
84930 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 define sqlite3E
84940 78 70 6c 61 69 6e 45 78 70 72 4c 69 73 74 28 41 xplainExprList(A
84950 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c ,B).# define sql
84960 69 74 65 33 45 78 70 6c 61 69 6e 46 69 6e 69 73 ite3ExplainFinis
84970 68 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 h(X).# define sq
84980 6c 69 74 65 33 56 64 62 65 45 78 70 6c 61 6e 61 lite3VdbeExplana
84990 74 69 6f 6e 28 58 29 20 30 0a 23 65 6e 64 69 66 tion(X) 0.#endif
849a0 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ...SQLITE_PRIVAT
849b0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 E void sqlite3Se
849c0 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c tString(char **,
849d0 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 sqlite3*, const
849e0 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 char*, ...);.SQ
849f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
84a00 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 d sqlite3ErrorMs
84a10 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 g(Parse*, const
84a20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c char*, ...);.SQL
84a30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
84a40 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 sqlite3Dequote(c
84a50 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
84a60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
84a70 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 3KeywordCode(con
84a80 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
84a90 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
84aa0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
84ab0 74 65 33 52 75 6e 50 61 72 73 65 72 28 50 61 72 te3RunParser(Par
84ac0 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a se*, const char*
84ad0 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 , char **);.SQLI
84ae0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
84af0 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 sqlite3FinishCod
84b00 69 6e 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c ing(Parse*);.SQL
84b10 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
84b20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 sqlite3GetTempRe
84b30 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 g(Parse*);.SQLIT
84b40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
84b50 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
84b60 70 52 65 67 28 50 61 72 73 65 2a 2c 69 6e 74 29 pReg(Parse*,int)
84b70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
84b80 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 int sqlite3GetT
84b90 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 2a 2c empRange(Parse*,
84ba0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
84bb0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
84bc0 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 3ReleaseTempRang
84bd0 65 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 e(Parse*,int,int
84be0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
84bf0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c E void sqlite3Cl
84c00 65 61 72 54 65 6d 70 52 65 67 43 61 63 68 65 28 earTempRegCache(
84c10 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
84c20 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
84c30 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 73 lite3ExprAlloc(s
84c40 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 qlite3*,int,cons
84c50 74 20 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 t Token*,int);.S
84c60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
84c70 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 pr *sqlite3Expr(
84c80 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e sqlite3*,int,con
84c90 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 st char*);.SQLIT
84ca0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
84cb0 71 6c 69 74 65 33 45 78 70 72 41 74 74 61 63 68 qlite3ExprAttach
84cc0 53 75 62 74 72 65 65 73 28 73 71 6c 69 74 65 33 Subtrees(sqlite3
84cd0 2a 2c 45 78 70 72 2a 2c 45 78 70 72 2a 2c 45 78 *,Expr*,Expr*,Ex
84ce0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
84cf0 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 VATE Expr *sqlit
84d00 65 33 50 45 78 70 72 28 50 61 72 73 65 2a 2c 20 e3PExpr(Parse*,
84d10 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 int, Expr*, Expr
84d20 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 *, const Token*)
84d30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
84d40 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 Expr *sqlite3Ex
84d50 70 72 41 6e 64 28 73 71 6c 69 74 65 33 2a 2c 45 prAnd(sqlite3*,E
84d60 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 xpr*, Expr*);.SQ
84d70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
84d80 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 46 75 r *sqlite3ExprFu
84d90 6e 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 45 78 nction(Parse*,Ex
84da0 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 prList*, Token*)
84db0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
84dc0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
84dd0 72 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 rAssignVarNumber
84de0 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b (Parse*, Expr*);
84df0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
84e00 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
84e10 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c Delete(sqlite3*,
84e20 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
84e30 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 PRIVATE ExprList
84e40 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 *sqlite3ExprLis
84e50 74 41 70 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 tAppend(Parse*,E
84e60 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 2a 29 3b xprList*,Expr*);
84e70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
84e80 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
84e90 4c 69 73 74 53 65 74 4e 61 6d 65 28 50 61 72 73 ListSetName(Pars
84ea0 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 54 6f 6b e*,ExprList*,Tok
84eb0 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 en*,int);.SQLITE
84ec0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
84ed0 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 lite3ExprListSet
84ee0 53 70 61 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 Span(Parse*,Expr
84ef0 4c 69 73 74 2a 2c 45 78 70 72 53 70 61 6e 2a 29 List*,ExprSpan*)
84f00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
84f10 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
84f20 72 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 rListDelete(sqli
84f30 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 te3*, ExprList*)
84f40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
84f50 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 int sqlite3Init
84f60 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a (sqlite3*, char*
84f70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
84f80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e TE int sqlite3In
84f90 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a itCallback(void*
84fa0 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63 , int, char**, c
84fb0 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 har**);.SQLITE_P
84fc0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
84fd0 74 65 33 50 72 61 67 6d 61 28 50 61 72 73 65 2a te3Pragma(Parse*
84fe0 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 ,Token*,Token*,T
84ff0 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 oken*,int);.SQLI
85000 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
85010 73 71 6c 69 74 65 33 52 65 73 65 74 41 6c 6c 53 sqlite3ResetAllS
85020 63 68 65 6d 61 73 4f 66 43 6f 6e 6e 65 63 74 69 chemasOfConnecti
85030 6f 6e 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 on(sqlite3*);.SQ
85040 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
85050 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 4f 6e d sqlite3ResetOn
85060 65 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33 2a eSchema(sqlite3*
85070 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
85080 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
85090 65 33 43 6f 6c 6c 61 70 73 65 44 61 74 61 62 61 e3CollapseDataba
850a0 73 65 41 72 72 61 79 28 73 71 6c 69 74 65 33 2a seArray(sqlite3*
850b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
850c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 E void sqlite3Be
850d0 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c ginParse(Parse*,
850e0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
850f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
85100 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 3CommitInternalC
85110 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 hanges(sqlite3*)
85120 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
85130 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 52 Table *sqlite3R
85140 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 esultSetOfSelect
85150 28 50 61 72 73 65 2a 2c 53 65 6c 65 63 74 2a 29 (Parse*,Select*)
85160 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
85170 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 void sqlite3Ope
85180 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 50 61 72 nMasterTable(Par
85190 73 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 se *, int);.SQLI
851a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
851b0 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c sqlite3StartTabl
851c0 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c e(Parse*,Token*,
851d0 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 69 Token*,int,int,i
851e0 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f nt,int);.SQLITE_
851f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
85200 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61 ite3AddColumn(Pa
85210 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 rse*,Token*);.SQ
85220 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
85230 64 20 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e d sqlite3AddNotN
85240 75 6c 6c 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 ull(Parse*, int)
85250 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
85260 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 void sqlite3Add
85270 50 72 69 6d 61 72 79 4b 65 79 28 50 61 72 73 65 PrimaryKey(Parse
85280 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e *, ExprList*, in
85290 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 t, int, int);.SQ
852a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
852b0 64 20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 d sqlite3AddChec
852c0 6b 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 73 kConstraint(Pars
852d0 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 e*, Expr*);.SQLI
852e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
852f0 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e sqlite3AddColumn
85300 54 79 70 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 Type(Parse*,Toke
85310 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
85320 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
85330 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 AddDefaultValue(
85340 50 61 72 73 65 2a 2c 45 78 70 72 53 70 61 6e 2a Parse*,ExprSpan*
85350 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
85360 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 E void sqlite3Ad
85370 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 50 61 72 dCollateType(Par
85380 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 se*, Token*);.SQ
85390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
853a0 64 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c d sqlite3EndTabl
853b0 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c e(Parse*,Token*,
853c0 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 29 3b Token*,Select*);
853d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
853e0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 72 73 65 int sqlite3Parse
853f0 55 72 69 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c Uri(const char*,
85400 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 73 69 const char*,unsi
85410 67 6e 65 64 20 69 6e 74 2a 2c 0a 20 20 20 20 20 gned int*,.
85420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
85430 71 6c 69 74 65 33 5f 76 66 73 2a 2a 2c 63 68 61 qlite3_vfs**,cha
85440 72 2a 2a 2c 63 68 61 72 20 2a 2a 29 3b 0a 53 51 r**,char **);.SQ
85450 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 74 72 LITE_PRIVATE Btr
85460 65 65 20 2a 73 71 6c 69 74 65 33 44 62 4e 61 6d ee *sqlite3DbNam
85470 65 54 6f 42 74 72 65 65 28 73 71 6c 69 74 65 33 eToBtree(sqlite3
85480 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a *,const char*);.
85490 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
854a0 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 4f 6e nt sqlite3CodeOn
854b0 63 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51 ce(Parse *);..SQ
854c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 LITE_PRIVATE Bit
854d0 76 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 vec *sqlite3Bitv
854e0 65 63 43 72 65 61 74 65 28 75 33 32 29 3b 0a 53 ecCreate(u32);.S
854f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
85500 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 t sqlite3BitvecT
85510 65 73 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 est(Bitvec*, u32
85520 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
85530 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 E int sqlite3Bit
85540 76 65 63 53 65 74 28 42 69 74 76 65 63 2a 2c 20 vecSet(Bitvec*,
85550 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u32);.SQLITE_PRI
85560 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
85570 33 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 3BitvecClear(Bit
85580 76 65 63 2a 2c 20 75 33 32 2c 20 76 6f 69 64 2a vec*, u32, void*
85590 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
855a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 E void sqlite3Bi
855b0 74 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76 tvecDestroy(Bitv
855c0 65 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ec*);.SQLITE_PRI
855d0 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
855e0 42 69 74 76 65 63 53 69 7a 65 28 42 69 74 76 65 BitvecSize(Bitve
855f0 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 c*);.SQLITE_PRIV
85600 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
85610 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 itvecBuiltinTest
85620 28 69 6e 74 2c 69 6e 74 2a 29 3b 0a 0a 53 51 4c (int,int*);..SQL
85630 49 54 45 5f 50 52 49 56 41 54 45 20 52 6f 77 53 ITE_PRIVATE RowS
85640 65 74 20 2a 73 71 6c 69 74 65 33 52 6f 77 53 65 et *sqlite3RowSe
85650 74 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 tInit(sqlite3*,
85660 76 6f 69 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 void*, unsigned
85670 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
85680 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
85690 33 52 6f 77 53 65 74 43 6c 65 61 72 28 52 6f 77 3RowSetClear(Row
856a0 53 65 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Set*);.SQLITE_PR
856b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
856c0 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 e3RowSetInsert(R
856d0 6f 77 53 65 74 2a 2c 20 69 36 34 29 3b 0a 53 51 owSet*, i64);.SQ
856e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
856f0 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 sqlite3RowSetTe
85700 73 74 28 52 6f 77 53 65 74 2a 2c 20 75 38 20 69 st(RowSet*, u8 i
85710 42 61 74 63 68 2c 20 69 36 34 29 3b 0a 53 51 4c Batch, i64);.SQL
85720 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
85730 73 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 78 sqlite3RowSetNex
85740 74 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 2a 29 t(RowSet*, i64*)
85750 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
85760 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 E void sqlite3Cr
85770 65 61 74 65 56 69 65 77 28 50 61 72 73 65 2a 2c eateView(Parse*,
85780 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f Token*,Token*,To
85790 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 ken*,Select*,int
857a0 2c 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 ,int);..#if !def
857b0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
857c0 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e _VIEW) || !defin
857d0 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ed(SQLITE_OMIT_V
857e0 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 53 51 4c IRTUALTABLE).SQL
857f0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
85800 74 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 t sqlite3ViewGet
85810 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 ColumnNames(Pars
85820 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 e*,Table*);.#els
85830 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 e.# define sqlit
85840 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e e3ViewGetColumnN
85850 61 6d 65 73 28 41 2c 42 29 20 30 0a 23 65 6e 64 ames(A,B) 0.#end
85860 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 if..SQLITE_PRIVA
85870 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
85880 72 6f 70 54 61 62 6c 65 28 50 61 72 73 65 2a 2c ropTable(Parse*,
85890 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 SrcList*, int,
858a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
858b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
858c0 33 43 6f 64 65 44 72 6f 70 54 61 62 6c 65 28 50 3CodeDropTable(P
858d0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 arse*, Table*, i
858e0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
858f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
85900 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 lite3DeleteTable
85910 28 73 71 6c 69 74 65 33 2a 2c 20 54 61 62 6c 65 (sqlite3*, Table
85920 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 *);.#ifndef SQLI
85930 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 TE_OMIT_AUTOINCR
85940 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 EMENT.SQLITE_PRI
85950 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
85960 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 te3Autoincrement
85970 42 65 67 69 6e 28 50 61 72 73 65 20 2a 70 50 61 Begin(Parse *pPa
85980 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 rse);.SQLITE_PRI
85990 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
859a0 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 te3Autoincrement
859b0 45 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 End(Parse *pPars
859c0 65 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 e);.#else.# defi
859d0 6e 65 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e ne sqlite3Autoin
859e0 63 72 65 6d 65 6e 74 42 65 67 69 6e 28 58 29 0a crementBegin(X).
859f0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
85a00 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 AutoincrementEnd
85a10 28 58 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 (X).#endif.SQLIT
85a20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
85a30 6c 69 74 65 33 43 6f 64 65 43 6f 72 6f 75 74 69 lite3CodeCorouti
85a40 6e 65 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 ne(Parse*, Selec
85a50 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 2a 29 t*, SelectDest*)
85a60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
85a70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 73 void sqlite3Ins
85a80 65 72 74 28 50 61 72 73 65 2a 2c 20 53 72 63 4c ert(Parse*, SrcL
85a90 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c ist*, ExprList*,
85aa0 20 53 65 6c 65 63 74 2a 2c 20 49 64 4c 69 73 74 Select*, IdList
85ab0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
85ac0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
85ad0 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 lite3ArrayAlloca
85ae0 74 65 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 te(sqlite3*,void
85af0 2a 2c 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 29 *,int,int*,int*)
85b00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
85b10 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 IdList *sqlite3
85b20 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c IdListAppend(sql
85b30 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 ite3*, IdList*,
85b40 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
85b50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
85b60 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49 te3IdListIndex(I
85b70 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61 dList*,const cha
85b80 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
85b90 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c ATE SrcList *sql
85ba0 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 ite3SrcListEnlar
85bb0 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 ge(sqlite3*, Src
85bc0 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 List*, int, int)
85bd0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
85be0 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 SrcList *sqlite
85bf0 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 73 3SrcListAppend(s
85c00 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 qlite3*, SrcList
85c10 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
85c20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
85c30 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 TE SrcList *sqli
85c40 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 te3SrcListAppend
85c50 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c FromTerm(Parse*,
85c60 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e SrcList*, Token
85c70 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 *, Token*,.
85c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
85c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
85ca0 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a Token*, Select*
85cb0 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a , Expr*, IdList*
85cc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
85cd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 E void sqlite3Sr
85ce0 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 50 cListIndexedBy(P
85cf0 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 arse *, SrcList
85d00 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c *, Token *);.SQL
85d10 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
85d20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 sqlite3IndexedBy
85d30 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c 20 Lookup(Parse *,
85d40 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
85d50 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tem *);.SQLITE_P
85d60 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
85d70 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a te3SrcListShiftJ
85d80 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a oinType(SrcList*
85d90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
85da0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 E void sqlite3Sr
85db0 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f cListAssignCurso
85dc0 72 73 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 rs(Parse*, SrcLi
85dd0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
85de0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
85df0 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 3IdListDelete(sq
85e00 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29 lite3*, IdList*)
85e10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
85e20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 void sqlite3Src
85e30 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 ListDelete(sqlit
85e40 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a e3*, SrcList*);.
85e50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 SQLITE_PRIVATE I
85e60 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 43 72 65 ndex *sqlite3Cre
85e70 61 74 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c ateIndex(Parse*,
85e80 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 Token*,Token*,Sr
85e90 63 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a cList*,ExprList*
85ea0 2c 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 ,int,Token*,.
85eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
85ec0 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 Token*, int
85ed0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
85ee0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
85ef0 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 te3DropIndex(Par
85f00 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 se*, SrcList*, i
85f10 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
85f20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 ATE int sqlite3S
85f30 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 elect(Parse*, Se
85f40 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 lect*, SelectDes
85f50 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
85f60 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 ATE Select *sqli
85f70 74 65 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72 te3SelectNew(Par
85f80 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 se*,ExprList*,Sr
85f90 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 cList*,Expr*,Exp
85fa0 72 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 rList*,.
85fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
85fc0 20 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a Expr*,ExprList*
85fd0 2c 69 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a ,int,Expr*,Expr*
85fe0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
85ff0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 E void sqlite3Se
86000 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 lectDelete(sqlit
86010 65 33 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 e3*, Select*);.S
86020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 QLITE_PRIVATE Ta
86030 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c ble *sqlite3SrcL
86040 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a istLookup(Parse*
86050 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c , SrcList*);.SQL
86060 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
86070 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c sqlite3IsReadOnl
86080 79 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a y(Parse*, Table*
86090 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
860a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
860b0 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 te3OpenTable(Par
860c0 73 65 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 se*, int iCur, i
860d0 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 nt iDb, Table*,
860e0 69 6e 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 int);.#if define
860f0 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
86100 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 UPDATE_DELETE_LI
86110 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 MIT) && !defined
86120 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 (SQLITE_OMIT_SUB
86130 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 QUERY).SQLITE_PR
86140 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
86150 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61 te3LimitWhere(Pa
86160 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a rse *, SrcList *
86170 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 , Expr *, ExprLi
86180 73 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 st *, Expr *, Ex
86190 70 72 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 pr *, char *);.#
861a0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 endif.SQLITE_PRI
861b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
861c0 33 44 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 3DeleteFrom(Pars
861d0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 e*, SrcList*, Ex
861e0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
861f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
86200 33 55 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 3Update(Parse*,
86210 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 SrcList*, ExprLi
86220 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 st*, Expr*, int)
86230 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
86240 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 WhereInfo *sqli
86250 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 te3WhereBegin(Pa
86260 72 73 65 2a 2c 53 72 63 4c 69 73 74 2a 2c 45 78 rse*,SrcList*,Ex
86270 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 45 78 pr*,ExprList*,Ex
86280 70 72 4c 69 73 74 2a 2c 75 31 36 2c 69 6e 74 29 prList*,u16,int)
86290 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
862a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65 void sqlite3Whe
862b0 72 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 2a reEnd(WhereInfo*
862c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
862d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
862e0 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 50 rCodeGetColumn(P
862f0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 arse*, Table*, i
86300 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 75 38 nt, int, int, u8
86310 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
86320 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
86330 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 4f prCodeGetColumnO
86340 66 54 61 62 6c 65 28 56 64 62 65 2a 2c 20 54 61 fTable(Vdbe*, Ta
86350 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 ble*, int, int,
86360 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
86370 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
86380 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 3ExprCodeMove(Pa
86390 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 rse*, int, int,
863a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
863b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
863c0 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 65 28 3ExprCacheStore(
863d0 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Parse*, int, int
863e0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
863f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
86400 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68 te3ExprCachePush
86410 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 (Parse*);.SQLITE
86420 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
86430 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f lite3ExprCachePo
86440 70 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a p(Parse*, int);.
86450 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
86460 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
86470 61 63 68 65 52 65 6d 6f 76 65 28 50 61 72 73 65 acheRemove(Parse
86480 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 *, int, int);.SQ
86490 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
864a0 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 d sqlite3ExprCac
864b0 68 65 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b heClear(Parse*);
864c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
864d0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
864e0 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 CacheAffinityCha
864f0 6e 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c nge(Parse*, int,
86500 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
86510 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
86520 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 2a 3ExprCode(Parse*
86530 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 , Expr*, int);.S
86540 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
86550 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
86560 65 54 65 6d 70 28 50 61 72 73 65 2a 2c 20 45 78 eTemp(Parse*, Ex
86570 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 pr*, int*);.SQLI
86580 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
86590 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 qlite3ExprCodeTa
865a0 72 67 65 74 28 50 61 72 73 65 2a 2c 20 45 78 70 rget(Parse*, Exp
865b0 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
865c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
865d0 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43 ite3ExprCodeAndC
865e0 61 63 68 65 28 50 61 72 73 65 2a 2c 20 45 78 70 ache(Parse*, Exp
865f0 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
86600 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
86610 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 6e lite3ExprCodeCon
86620 73 74 61 6e 74 73 28 50 61 72 73 65 2a 2c 20 45 stants(Parse*, E
86630 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
86640 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
86650 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 3ExprCodeExprLis
86660 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 t(Parse*, ExprLi
86670 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a st*, int, int);.
86680 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
86690 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 oid sqlite3ExprI
866a0 66 54 72 75 65 28 50 61 72 73 65 2a 2c 20 45 78 fTrue(Parse*, Ex
866b0 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a pr*, int, int);.
866c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
866d0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 oid sqlite3ExprI
866e0 66 46 61 6c 73 65 28 50 61 72 73 65 2a 2c 20 45 fFalse(Parse*, E
866f0 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b xpr*, int, int);
86700 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
86710 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 46 69 Table *sqlite3Fi
86720 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a ndTable(sqlite3*
86730 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f ,const char*, co
86740 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
86750 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 TE_PRIVATE Table
86760 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 *sqlite3LocateT
86770 61 62 6c 65 28 50 61 72 73 65 2a 2c 69 6e 74 20 able(Parse*,int
86780 69 73 56 69 65 77 2c 63 6f 6e 73 74 20 63 68 61 isView,const cha
86790 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 r*, const char*)
867a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
867b0 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c Table *sqlite3L
867c0 6f 63 61 74 65 54 61 62 6c 65 49 74 65 6d 28 50 ocateTableItem(P
867d0 61 72 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 arse*,int isView
867e0 2c 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f ,struct SrcList_
867f0 69 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f item *);.SQLITE_
86800 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73 PRIVATE Index *s
86810 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 qlite3FindIndex(
86820 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 sqlite3*,const c
86830 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 har*, const char
86840 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
86850 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 TE void sqlite3U
86860 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 nlinkAndDeleteTa
86870 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 ble(sqlite3*,int
86880 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 ,const char*);.S
86890 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
868a0 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b id sqlite3Unlink
868b0 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73 AndDeleteIndex(s
868c0 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 qlite3*,int,cons
868d0 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
868e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
868f0 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 73 lite3Vacuum(Pars
86900 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
86910 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
86920 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a 2c unVacuum(char**,
86930 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 sqlite3*);.SQLI
86940 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
86950 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d *sqlite3NameFrom
86960 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20 Token(sqlite3*,
86970 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
86980 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
86990 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45 te3ExprCompare(E
869a0 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 xpr*, Expr*);.SQ
869b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
869c0 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
869d0 43 6f 6d 70 61 72 65 28 45 78 70 72 4c 69 73 74 Compare(ExprList
869e0 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 *, ExprList*);.S
869f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
86a00 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e id sqlite3ExprAn
86a10 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 alyzeAggregates(
86a20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 NameContext*, Ex
86a30 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
86a40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
86a50 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 3ExprAnalyzeAggL
86a60 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a ist(NameContext*
86a70 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c ,ExprList*);.SQL
86a80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
86a90 73 71 6c 69 74 65 33 46 75 6e 63 74 69 6f 6e 55 sqlite3FunctionU
86aa0 73 65 73 54 68 69 73 53 72 63 28 45 78 70 72 2a sesThisSrc(Expr*
86ab0 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c , SrcList*);.SQL
86ac0 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 ITE_PRIVATE Vdbe
86ad0 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 *sqlite3GetVdbe
86ae0 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 (Parse*);.SQLITE
86af0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
86b00 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 lite3PrngSaveSta
86b10 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 te(void);.SQLITE
86b20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
86b30 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 lite3PrngRestore
86b40 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c State(void);.SQL
86b50 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
86b60 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 sqlite3PrngRese
86b70 74 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 tState(void);.SQ
86b80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
86b90 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 d sqlite3Rollbac
86ba0 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 2c 69 6e kAll(sqlite3*,in
86bb0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
86bc0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
86bd0 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 odeVerifySchema(
86be0 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 Parse*, int);.SQ
86bf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
86c00 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 d sqlite3CodeVer
86c10 69 66 79 4e 61 6d 65 64 53 63 68 65 6d 61 28 50 ifyNamedSchema(P
86c20 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 arse*, const cha
86c30 72 20 2a 7a 44 62 29 3b 0a 53 51 4c 49 54 45 5f r *zDb);.SQLITE_
86c40 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
86c50 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 ite3BeginTransac
86c60 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74 tion(Parse*, int
86c70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
86c80 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f E void sqlite3Co
86c90 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 mmitTransaction(
86ca0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
86cb0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
86cc0 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e ite3RollbackTran
86cd0 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b saction(Parse*);
86ce0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
86cf0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65 void sqlite3Save
86d00 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 6e point(Parse*, in
86d10 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 t, Token*);.SQLI
86d20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
86d30 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 sqlite3CloseSave
86d40 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20 2a points(sqlite3 *
86d50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
86d60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4c 65 E void sqlite3Le
86d70 61 76 65 4d 75 74 65 78 41 6e 64 43 6c 6f 73 65 aveMutexAndClose
86d80 5a 6f 6d 62 69 65 28 73 71 6c 69 74 65 33 2a 29 Zombie(sqlite3*)
86d90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
86da0 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
86db0 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72 2a IsConstant(Expr*
86dc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
86dd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
86de0 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f rIsConstantNotJo
86df0 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 in(Expr*);.SQLIT
86e00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
86e10 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
86e20 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 antOrFunction(Ex
86e30 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
86e40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
86e50 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78 ExprIsInteger(Ex
86e60 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 pr*, int*);.SQLI
86e70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
86e80 71 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65 4e qlite3ExprCanBeN
86e90 75 6c 6c 28 63 6f 6e 73 74 20 45 78 70 72 2a 29 ull(const Expr*)
86ea0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
86eb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
86ec0 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 rCodeIsNullJump(
86ed0 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 45 78 70 Vdbe*, const Exp
86ee0 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 r*, int, int);.S
86ef0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
86f00 74 20 73 71 6c 69 74 65 33 45 78 70 72 4e 65 65 t sqlite3ExprNee
86f10 64 73 4e 6f 41 66 66 69 6e 69 74 79 43 68 61 6e dsNoAffinityChan
86f20 67 65 28 63 6f 6e 73 74 20 45 78 70 72 2a 2c 20 ge(const Expr*,
86f30 63 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 char);.SQLITE_PR
86f40 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
86f50 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20 63 3IsRowid(const c
86f60 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
86f70 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
86f80 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c e3GenerateRowDel
86f90 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61 62 6c ete(Parse*, Tabl
86fa0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e e*, int, int, in
86fb0 74 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69 6e t, Trigger *, in
86fc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
86fd0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 TE void sqlite3G
86fe0 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 enerateRowIndexD
86ff0 65 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61 elete(Parse*, Ta
87000 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 ble*, int, int*)
87010 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
87020 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 6e 65 int sqlite3Gene
87030 72 61 74 65 49 6e 64 65 78 4b 65 79 28 50 61 72 rateIndexKey(Par
87040 73 65 2a 2c 20 49 6e 64 65 78 2a 2c 20 69 6e 74 se*, Index*, int
87050 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
87060 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
87070 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 sqlite3Generate
87080 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 ConstraintChecks
87090 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 2c 69 (Parse*,Table*,i
870a0 6e 74 2c 69 6e 74 2c 0a 20 20 20 20 20 20 20 20 nt,int,.
870b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
870c0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 int
870d0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e *,int,int,int,in
870e0 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f t,int*);.SQLITE_
870f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
87100 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 ite3CompleteInse
87110 72 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 54 61 rtion(Parse*, Ta
87120 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 ble*, int, int,
87130 69 6e 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 int*, int, int,
87140 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
87150 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
87160 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 OpenTableAndIndi
87170 63 65 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c ces(Parse*, Tabl
87180 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 e*, int, int);.S
87190 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
871a0 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 id sqlite3BeginW
871b0 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 50 61 riteOperation(Pa
871c0 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b rse*, int, int);
871d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
871e0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 75 6c 74 void sqlite3Mult
871f0 69 57 72 69 74 65 28 50 61 72 73 65 2a 29 3b 0a iWrite(Parse*);.
87200 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
87210 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 79 41 62 oid sqlite3MayAb
87220 6f 72 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c ort(Parse*);.SQL
87230 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
87240 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 sqlite3HaltCons
87250 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 traint(Parse*, i
87260 6e 74 2c 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b nt, char*, int);
87270 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
87280 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 Expr *sqlite3Exp
87290 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 rDup(sqlite3*,Ex
872a0 70 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 pr*,int);.SQLITE
872b0 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 _PRIVATE ExprLis
872c0 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 t *sqlite3ExprLi
872d0 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 stDup(sqlite3*,E
872e0 78 70 72 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 xprList*,int);.S
872f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 QLITE_PRIVATE Sr
87300 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 cList *sqlite3Sr
87310 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 cListDup(sqlite3
87320 2a 2c 53 72 63 4c 69 73 74 2a 2c 69 6e 74 29 3b *,SrcList*,int);
87330 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
87340 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 IdList *sqlite3I
87350 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 dListDup(sqlite3
87360 2a 2c 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 *,IdList*);.SQLI
87370 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 TE_PRIVATE Selec
87380 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 t *sqlite3Select
87390 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c Dup(sqlite3*,Sel
873a0 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ect*,int);.SQLIT
873b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
873c0 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 qlite3FuncDefIns
873d0 65 72 74 28 46 75 6e 63 44 65 66 48 61 73 68 2a ert(FuncDefHash*
873e0 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c , FuncDef*);.SQL
873f0 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 ITE_PRIVATE Func
87400 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 Def *sqlite3Find
87410 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 Function(sqlite3
87420 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e *,const char*,in
87430 74 2c 69 6e 74 2c 75 38 2c 75 38 29 3b 0a 53 51 t,int,u8,u8);.SQ
87440 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
87450 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 d sqlite3Registe
87460 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e rBuiltinFunction
87470 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c s(sqlite3*);.SQL
87480 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
87490 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 sqlite3Register
874a0 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e DateTimeFunction
874b0 73 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f s(void);.SQLITE_
874c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
874d0 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62 ite3RegisterGlob
874e0 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 alFunctions(void
874f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
87500 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 E int sqlite3Saf
87510 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 etyCheckOk(sqlit
87520 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e3*);.SQLITE_PRI
87530 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
87540 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f SafetyCheckSickO
87550 72 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 rOk(sqlite3*);.S
87560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
87570 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 id sqlite3Change
87580 43 6f 6f 6b 69 65 28 50 61 72 73 65 2a 2c 20 69 Cookie(Parse*, i
87590 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e nt);..#if !defin
875a0 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ed(SQLITE_OMIT_V
875b0 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64 IEW) && !defined
875c0 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 (SQLITE_OMIT_TRI
875d0 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 GGER).SQLITE_PRI
875e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
875f0 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 3MaterializeView
87600 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
87610 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 65 Expr*, int);.#e
87620 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
87630 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
87640 52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 R.SQLITE_PRIVATE
87650 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 void sqlite3B
87660 65 67 69 6e 54 72 69 67 67 65 72 28 50 61 72 73 eginTrigger(Pars
87670 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e e*, Token*,Token
87680 2a 2c 69 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 74 *,int,int,IdList
87690 2a 2c 53 72 63 4c 69 73 74 2a 2c 0a 20 20 20 20 *,SrcList*,.
876a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
876b0 20 20 20 20 20 20 20 45 78 70 72 2a 2c 69 6e 74 Expr*,int
876c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
876d0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
876e0 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 67 lite3FinishTrigg
876f0 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67 67 er(Parse*, Trigg
87700 65 72 53 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a 29 erStep*, Token*)
87710 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
87720 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 void sqlite3D
87730 72 6f 70 54 72 69 67 67 65 72 28 50 61 72 73 65 ropTrigger(Parse
87740 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 *, SrcList*, int
87750 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
87760 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
87770 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 50 DropTriggerPtr(P
87780 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 2a 29 arse*, Trigger*)
87790 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
877a0 20 20 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69 Trigger *sqli
877b0 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74 te3TriggersExist
877c0 28 50 61 72 73 65 20 2a 2c 20 54 61 62 6c 65 2a (Parse *, Table*
877d0 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a , int, ExprList*
877e0 2c 20 69 6e 74 20 2a 70 4d 61 73 6b 29 3b 0a 53 , int *pMask);.S
877f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
87800 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 Trigger *sqlite3
87810 54 72 69 67 67 65 72 4c 69 73 74 28 50 61 72 73 TriggerList(Pars
87820 65 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 e *, Table *);.S
87830 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
87840 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 void sqlite3Code
87850 52 6f 77 54 72 69 67 67 65 72 28 50 61 72 73 65 RowTrigger(Parse
87860 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69 6e *, Trigger *, in
87870 74 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e t, ExprList*, in
87880 74 2c 20 54 61 62 6c 65 20 2a 2c 0a 20 20 20 20 t, Table *,.
87890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
878a0 20 20 20 20 20 20 20 20 69 6e 74 2c 20 69 6e 74 int, int
878b0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
878c0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
878d0 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 lite3CodeRowTrig
878e0 67 65 72 44 69 72 65 63 74 28 50 61 72 73 65 20 gerDirect(Parse
878f0 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 54 61 *, Trigger *, Ta
87900 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c ble *, int, int,
87910 20 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 73 71 int);. void sq
87920 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 72 73 liteViewTriggers
87930 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
87940 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70 Expr*, int, Exp
87950 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f rList*);.SQLITE_
87960 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
87970 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 qlite3DeleteTrig
87980 67 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 2a gerStep(sqlite3*
87990 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b , TriggerStep*);
879a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
879b0 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 TriggerStep *s
879c0 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c qlite3TriggerSel
879d0 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a ectStep(sqlite3*
879e0 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 ,Select*);.SQLIT
879f0 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 E_PRIVATE Trig
87a00 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 gerStep *sqlite3
87a10 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 TriggerInsertSte
87a20 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e p(sqlite3*,Token
87a30 2a 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20 *, IdList*,.
87a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
87a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
87a60 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 65 ExprList*,Se
87a70 6c 65 63 74 2a 2c 75 38 29 3b 0a 53 51 4c 49 54 lect*,u8);.SQLIT
87a80 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 E_PRIVATE Trig
87a90 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 gerStep *sqlite3
87aa0 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65 TriggerUpdateSte
87ab0 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e p(sqlite3*,Token
87ac0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70 *,ExprList*, Exp
87ad0 72 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f r*, u8);.SQLITE_
87ae0 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 PRIVATE Trigge
87af0 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 rStep *sqlite3Tr
87b00 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28 iggerDeleteStep(
87b10 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c sqlite3*,Token*,
87b20 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
87b30 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
87b40 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 qlite3DeleteTrig
87b50 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 72 ger(sqlite3*, Tr
87b60 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f igger*);.SQLITE_
87b70 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
87b80 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 qlite3UnlinkAndD
87b90 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c eleteTrigger(sql
87ba0 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 ite3*,int,const
87bb0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
87bc0 52 49 56 41 54 45 20 20 20 75 33 32 20 73 71 6c RIVATE u32 sql
87bd0 69 74 65 33 54 72 69 67 67 65 72 43 6f 6c 6d 61 ite3TriggerColma
87be0 73 6b 28 50 61 72 73 65 2a 2c 54 72 69 67 67 65 sk(Parse*,Trigge
87bf0 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 6e 74 r*,ExprList*,int
87c00 2c 69 6e 74 2c 54 61 62 6c 65 2a 2c 69 6e 74 29 ,int,Table*,int)
87c10 3b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ;.# define sqlit
87c20 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 e3ParseToplevel(
87c30 70 29 20 28 28 70 29 2d 3e 70 54 6f 70 6c 65 76 p) ((p)->pToplev
87c40 65 6c 20 3f 20 28 70 29 2d 3e 70 54 6f 70 6c 65 el ? (p)->pTople
87c50 76 65 6c 20 3a 20 28 70 29 29 0a 23 65 6c 73 65 vel : (p)).#else
87c60 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
87c70 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 42 3TriggersExist(B
87c80 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23 20 64 65 ,C,D,E,F) 0.# de
87c90 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 6c 65 fine sqlite3Dele
87ca0 74 65 54 72 69 67 67 65 72 28 41 2c 42 29 0a 23 teTrigger(A,B).#
87cb0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 define sqlite3D
87cc0 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 41 2c ropTriggerPtr(A,
87cd0 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 B).# define sqli
87ce0 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 te3UnlinkAndDele
87cf0 74 65 54 72 69 67 67 65 72 28 41 2c 42 2c 43 29 teTrigger(A,B,C)
87d00 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
87d10 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 3CodeRowTrigger(
87d20 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c A,B,C,D,E,F,G,H,
87d30 49 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 I).# define sqli
87d40 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 te3CodeRowTrigge
87d50 72 44 69 72 65 63 74 28 41 2c 42 2c 43 2c 44 2c rDirect(A,B,C,D,
87d60 45 2c 46 29 0a 23 20 64 65 66 69 6e 65 20 73 71 E,F).# define sq
87d70 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 lite3TriggerList
87d80 28 58 2c 20 59 29 20 30 0a 23 20 64 65 66 69 6e (X, Y) 0.# defin
87d90 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f e sqlite3ParseTo
87da0 70 6c 65 76 65 6c 28 70 29 20 70 0a 23 20 64 65 plevel(p) p.# de
87db0 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67 fine sqlite3Trig
87dc0 67 65 72 43 6f 6c 6d 61 73 6b 28 41 2c 42 2c 43 gerColmask(A,B,C
87dd0 2c 44 2c 45 2c 46 2c 47 29 20 30 0a 23 65 6e 64 ,D,E,F,G) 0.#end
87de0 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 if..SQLITE_PRIVA
87df0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f TE int sqlite3Jo
87e00 69 6e 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54 inType(Parse*, T
87e10 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 oken*, Token*, T
87e20 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
87e30 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
87e40 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e te3CreateForeign
87e50 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72 Key(Parse*, Expr
87e60 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45 List*, Token*, E
87e70 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a xprList*, int);.
87e80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
87e90 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 72 oid sqlite3Defer
87ea0 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 65 ForeignKey(Parse
87eb0 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 6e 64 65 66 *, int);.#ifndef
87ec0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
87ed0 48 4f 52 49 5a 41 54 49 4f 4e 0a 53 51 4c 49 54 HORIZATION.SQLIT
87ee0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
87ef0 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 sqlite3AuthRead
87f00 28 50 61 72 73 65 2a 2c 45 78 70 72 2a 2c 53 63 (Parse*,Expr*,Sc
87f10 68 65 6d 61 2a 2c 53 72 63 4c 69 73 74 2a 29 3b hema*,SrcList*);
87f20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
87f30 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 int sqlite3Aut
87f40 68 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 69 6e hCheck(Parse*,in
87f50 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 t, const char*,
87f60 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e const char*, con
87f70 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 st char*);.SQLIT
87f80 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
87f90 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 sqlite3AuthCont
87fa0 65 78 74 50 75 73 68 28 50 61 72 73 65 2a 2c 20 extPush(Parse*,
87fb0 41 75 74 68 43 6f 6e 74 65 78 74 2a 2c 20 63 6f AuthContext*, co
87fc0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
87fd0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
87fe0 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e d sqlite3AuthCon
87ff0 74 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 textPop(AuthCont
88000 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ext*);.SQLITE_PR
88010 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
88020 74 65 33 41 75 74 68 52 65 61 64 43 6f 6c 28 50 te3AuthReadCol(P
88030 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 arse*, const cha
88040 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 r *, const char
88050 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 *, int);.#else.#
88060 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 define sqlite3A
88070 75 74 68 52 65 61 64 28 61 2c 62 2c 63 2c 64 29 uthRead(a,b,c,d)
88080 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
88090 33 41 75 74 68 43 68 65 63 6b 28 61 2c 62 2c 63 3AuthCheck(a,b,c
880a0 2c 64 2c 65 29 20 20 20 20 53 51 4c 49 54 45 5f ,d,e) SQLITE_
880b0 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 OK.# define sqli
880c0 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 te3AuthContextPu
880d0 73 68 28 61 2c 62 2c 63 29 0a 23 20 64 65 66 69 sh(a,b,c).# defi
880e0 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f ne sqlite3AuthCo
880f0 6e 74 65 78 74 50 6f 70 28 61 29 20 20 28 28 76 ntextPop(a) ((v
88100 6f 69 64 29 28 61 29 29 0a 23 65 6e 64 69 66 0a oid)(a)).#endif.
88110 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
88120 6f 69 64 20 73 71 6c 69 74 65 33 41 74 74 61 63 oid sqlite3Attac
88130 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c h(Parse*, Expr*,
88140 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a Expr*, Expr*);.
88150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
88160 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74 61 63 oid sqlite3Detac
88170 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 h(Parse*, Expr*)
88180 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
88190 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 49 int sqlite3FixI
881a0 6e 69 74 28 44 62 46 69 78 65 72 2a 2c 20 50 61 nit(DbFixer*, Pa
881b0 72 73 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 rse*, int, const
881c0 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f char*, const To
881d0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
881e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
881f0 33 46 69 78 53 72 63 4c 69 73 74 28 44 62 46 69 3FixSrcList(DbFi
88200 78 65 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b xer*, SrcList*);
88210 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
88220 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 65 int sqlite3FixSe
88230 6c 65 63 74 28 44 62 46 69 78 65 72 2a 2c 20 53 lect(DbFixer*, S
88240 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f elect*);.SQLITE_
88250 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
88260 74 65 33 46 69 78 45 78 70 72 28 44 62 46 69 78 te3FixExpr(DbFix
88270 65 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c er*, Expr*);.SQL
88280 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
88290 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 sqlite3FixExprLi
882a0 73 74 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70 st(DbFixer*, Exp
882b0 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f rList*);.SQLITE_
882c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
882d0 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65 te3FixTriggerSte
882e0 70 28 44 62 46 69 78 65 72 2a 2c 20 54 72 69 67 p(DbFixer*, Trig
882f0 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 gerStep*);.SQLIT
88300 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
88310 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 lite3AtoF(const
88320 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a char *z, double*
88330 2c 20 69 6e 74 2c 20 75 38 29 3b 0a 53 51 4c 49 , int, u8);.SQLI
88340 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
88350 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 63 qlite3GetInt32(c
88360 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 onst char *, int
88370 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
88380 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 TE int sqlite3At
88390 6f 69 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b oi(const char*);
883a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
883b0 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 int sqlite3Utf16
883c0 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f ByteLen(const vo
883d0 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e id *pData, int n
883e0 43 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Char);.SQLITE_PR
883f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
88400 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 3Utf8CharLen(con
88410 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 st char *pData,
88420 69 6e 74 20 6e 42 79 74 65 29 3b 0a 53 51 4c 49 int nByte);.SQLI
88430 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 TE_PRIVATE u32 s
88440 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 63 qlite3Utf8Read(c
88450 6f 6e 73 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a onst u8**);../*.
88460 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 72 ** Routines to r
88470 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 76 61 ead and write va
88480 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e riable-length in
88490 74 65 67 65 72 73 2e 20 20 54 68 65 73 65 20 75 tegers. These u
884a0 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 64 65 66 sed to.** be def
884b0 69 6e 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75 ined locally, bu
884c0 74 20 6e 6f 77 20 77 65 20 75 73 65 20 74 68 65 t now we use the
884d0 20 76 61 72 69 6e 74 20 72 6f 75 74 69 6e 65 73 varint routines
884e0 20 69 6e 20 74 68 65 20 75 74 69 6c 2e 63 0a 2a in the util.c.*
884f0 2a 20 66 69 6c 65 2e 20 20 43 6f 64 65 20 73 68 * file. Code sh
88500 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 ould use the MAC
88510 52 4f 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20 RO forms below,
88520 61 73 20 74 68 65 20 56 61 72 69 6e 74 33 32 20 as the Varint32
88530 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 versions.** are
88540 63 6f 64 65 64 20 74 6f 20 61 73 73 75 6d 65 20 coded to assume
88550 74 68 65 20 73 69 6e 67 6c 65 20 62 79 74 65 20 the single byte
88560 63 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 case is already
88570 68 61 6e 64 6c 65 64 20 28 77 68 69 63 68 20 0a handled (which .
88580 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 ** the MACRO for
88590 6d 20 64 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49 m does)..*/.SQLI
885a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
885b0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 qlite3PutVarint(
885c0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 unsigned char*,
885d0 75 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u64);.SQLITE_PRI
885e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
885f0 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69 PutVarint32(unsi
88600 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 29 gned char*, u32)
88610 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
88620 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 u8 sqlite3GetVa
88630 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 rint(const unsig
88640 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 36 34 20 ned char *, u64
88650 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
88660 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 TE u8 sqlite3Get
88670 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 Varint32(const u
88680 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 nsigned char *,
88690 75 33 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 u32 *);.SQLITE_P
886a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
886b0 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 e3VarintLen(u64
886c0 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 v);../*.** The h
886d0 65 61 64 65 72 20 6f 66 20 61 20 72 65 63 6f 72 eader of a recor
886e0 64 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 d consists of a
886f0 73 65 71 75 65 6e 63 65 20 76 61 72 69 61 62 6c sequence variabl
88700 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
88710 73 2e 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 s..** These inte
88720 67 65 72 73 20 61 72 65 20 61 6c 6d 6f 73 74 20 gers are almost
88730 61 6c 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64 always small and
88740 20 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73 20 are encoded as
88750 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a a single byte..*
88760 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
88770 6d 61 63 72 6f 73 20 74 61 6b 65 20 61 64 76 61 macros take adva
88780 6e 74 61 67 65 20 74 68 69 73 20 66 61 63 74 20 ntage this fact
88790 74 6f 20 70 72 6f 76 69 64 65 20 61 20 66 61 73 to provide a fas
887a0 74 20 65 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 t encode.** and
887b0 64 65 63 6f 64 65 20 6f 66 20 74 68 65 20 69 6e decode of the in
887c0 74 65 67 65 72 73 20 69 6e 20 61 20 72 65 63 6f tegers in a reco
887d0 72 64 20 68 65 61 64 65 72 2e 20 20 49 74 20 69 rd header. It i
887e0 73 20 66 61 73 74 65 72 20 66 6f 72 20 74 68 65 s faster for the
887f0 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 common.** case
88800 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 67 65 where the intege
88810 72 20 69 73 20 61 20 73 69 6e 67 6c 65 20 62 79 r is a single by
88820 74 65 2e 20 20 49 74 20 69 73 20 61 20 6c 69 74 te. It is a lit
88830 74 6c 65 20 73 6c 6f 77 65 72 20 77 68 65 6e 20 tle slower when
88840 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 the.** integer i
88850 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79 s two or more by
88860 74 65 73 2e 20 20 42 75 74 20 6f 76 65 72 61 6c tes. But overal
88870 6c 20 69 74 20 69 73 20 66 61 73 74 65 72 2e 0a l it is faster..
88880 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 **.** The follow
88890 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20 ing expressions
888a0 61 72 65 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a are equivalent:.
888b0 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 **.** x = sq
888c0 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 lite3GetVarint32
888d0 28 20 41 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 20 ( A, &B );.**
888e0 20 20 78 20 3d 20 73 71 6c 69 74 65 33 50 75 74 x = sqlite3Put
888f0 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 Varint32( A, B )
88900 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 ;.**.** x =
88910 67 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 getVarint32( A,
88920 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 B );.** x =
88930 70 75 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 putVarint32( A,
88940 42 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 B );.**.*/.#defi
88950 6e 65 20 67 65 74 56 61 72 69 6e 74 33 32 28 41 ne getVarint32(A
88960 2c 42 29 20 20 28 75 38 29 28 28 2a 28 41 29 3c ,B) (u8)((*(A)<
88970 28 75 38 29 30 78 38 30 29 20 3f 20 28 28 42 29 (u8)0x80) ? ((B)
88980 20 3d 20 28 75 33 32 29 2a 28 41 29 29 2c 31 20 = (u32)*(A)),1
88990 3a 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 : sqlite3GetVari
889a0 6e 74 33 32 28 28 41 29 2c 20 28 75 33 32 20 2a nt32((A), (u32 *
889b0 29 26 28 42 29 29 29 0a 23 64 65 66 69 6e 65 20 )&(B))).#define
889c0 70 75 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 putVarint32(A,B)
889d0 20 20 28 75 38 29 28 28 28 75 33 32 29 28 42 29 (u8)(((u32)(B)
889e0 3c 28 75 33 32 29 30 78 38 30 29 20 3f 20 28 2a <(u32)0x80) ? (*
889f0 28 41 29 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 (A) = (unsigned
88a00 63 68 61 72 29 28 42 29 29 2c 31 20 3a 20 73 71 char)(B)),1 : sq
88a10 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 lite3PutVarint32
88a20 28 28 41 29 2c 20 28 42 29 29 29 0a 23 64 65 66 ((A), (B))).#def
88a30 69 6e 65 20 67 65 74 56 61 72 69 6e 74 20 20 20 ine getVarint
88a40 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e sqlite3GetVarin
88a50 74 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61 72 t.#define putVar
88a60 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 50 75 int sqlite3Pu
88a70 74 56 61 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 45 tVarint...SQLITE
88a80 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 _PRIVATE const c
88a90 68 61 72 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 har *sqlite3Inde
88aa0 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 xAffinityStr(Vdb
88ab0 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 e *, Index *);.S
88ac0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
88ad0 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 id sqlite3TableA
88ae0 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 ffinityStr(Vdbe
88af0 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c *, Table *);.SQL
88b00 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
88b10 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 sqlite3CompareA
88b20 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 ffinity(Expr *pE
88b30 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 29 3b xpr, char aff2);
88b40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
88b50 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 int sqlite3Index
88b60 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 AffinityOk(Expr
88b70 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64 78 *pExpr, char idx
88b80 5f 61 66 66 69 6e 69 74 79 29 3b 0a 53 51 4c 49 _affinity);.SQLI
88b90 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
88ba0 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e sqlite3ExprAffin
88bb0 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 ity(Expr *pExpr)
88bc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
88bd0 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 69 int sqlite3Atoi
88be0 36 34 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 64(const char*,
88bf0 69 36 34 2a 2c 20 69 6e 74 2c 20 75 38 29 3b 0a i64*, int, u8);.
88c00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
88c10 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 oid sqlite3Error
88c20 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 (sqlite3*, int,
88c30 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 const char*,...)
88c40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
88c50 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 void *sqlite3He
88c60 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a xToBlob(sqlite3*
88c70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c , const char *z,
88c80 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f int n);.SQLITE_
88c90 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 PRIVATE u8 sqlit
88ca0 65 33 48 65 78 54 6f 49 6e 74 28 69 6e 74 20 68 e3HexToInt(int h
88cb0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
88cc0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f E int sqlite3Two
88cd0 50 61 72 74 4e 61 6d 65 28 50 61 72 73 65 20 2a PartName(Parse *
88ce0 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e , Token *, Token
88cf0 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 *, Token **);.S
88d00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
88d10 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
88d20 33 45 72 72 53 74 72 28 69 6e 74 29 3b 0a 53 51 3ErrStr(int);.SQ
88d30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
88d40 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 sqlite3ReadSche
88d50 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ma(Parse *pParse
88d60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
88d70 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 E CollSeq *sqlit
88d80 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71 e3FindCollSeq(sq
88d90 6c 69 74 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63 lite3*,u8 enc, c
88da0 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b onst char*,int);
88db0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
88dc0 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 CollSeq *sqlite3
88dd0 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 LocateCollSeq(Pa
88de0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e rse *pParse, con
88df0 73 74 20 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0a st char*zName);.
88e00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 SQLITE_PRIVATE C
88e10 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 ollSeq *sqlite3E
88e20 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 xprCollSeq(Parse
88e30 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
88e40 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50 pExpr);.SQLITE_P
88e50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
88e60 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 ite3ExprSetColl(
88e70 45 78 70 72 2a 2c 20 43 6f 6c 6c 53 65 71 2a 29 Expr*, CollSeq*)
88e80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
88e90 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 Expr *sqlite3Ex
88ea0 70 72 53 65 74 43 6f 6c 6c 42 79 54 6f 6b 65 6e prSetCollByToken
88eb0 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
88ec0 45 78 70 72 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a Expr*, Token*);.
88ed0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
88ee0 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 nt sqlite3CheckC
88ef0 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20 ollSeq(Parse *,
88f00 43 6f 6c 6c 53 65 71 20 2a 29 3b 0a 53 51 4c 49 CollSeq *);.SQLI
88f10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
88f20 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 qlite3CheckObjec
88f30 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 63 tName(Parse *, c
88f40 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 onst char *);.SQ
88f50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
88f60 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 d sqlite3VdbeSet
88f70 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 Changes(sqlite3
88f80 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
88f90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
88fa0 74 65 33 41 64 64 49 6e 74 36 34 28 69 36 34 2a te3AddInt64(i64*
88fb0 2c 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,i64);.SQLITE_PR
88fc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
88fd0 33 53 75 62 49 6e 74 36 34 28 69 36 34 2a 2c 69 3SubInt64(i64*,i
88fe0 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 64);.SQLITE_PRIV
88ff0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
89000 75 6c 49 6e 74 36 34 28 69 36 34 2a 2c 69 36 34 ulInt64(i64*,i64
89010 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
89020 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 62 73 E int sqlite3Abs
89030 49 6e 74 33 32 28 69 6e 74 29 3b 0a 23 69 66 64 Int32(int);.#ifd
89040 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
89050 5f 38 5f 33 5f 4e 41 4d 45 53 0a 53 51 4c 49 54 _8_3_NAMES.SQLIT
89060 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
89070 71 6c 69 74 65 33 46 69 6c 65 53 75 66 66 69 78 qlite3FileSuffix
89080 33 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 3(const char*, c
89090 68 61 72 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 har*);.#else.# d
890a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 69 6c efine sqlite3Fil
890b0 65 53 75 66 66 69 78 33 28 58 2c 59 29 0a 23 65 eSuffix3(X,Y).#e
890c0 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
890d0 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 ATE u8 sqlite3Ge
890e0 74 42 6f 6f 6c 65 61 6e 28 63 6f 6e 73 74 20 63 tBoolean(const c
890f0 68 61 72 20 2a 7a 2c 69 6e 74 29 3b 0a 0a 53 51 har *z,int);..SQ
89100 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
89110 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
89120 56 61 6c 75 65 54 65 78 74 28 73 71 6c 69 74 65 ValueText(sqlite
89130 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 3_value*, u8);.S
89140 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
89150 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 t sqlite3ValueBy
89160 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 tes(sqlite3_valu
89170 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f e*, u8);.SQLITE_
89180 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
89190 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 ite3ValueSetStr(
891a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 sqlite3_value*,
891b0 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 int, const void
891c0 2a 2c 75 38 2c 20 0a 20 20 20 20 20 20 20 20 20 *,u8, .
891d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 v
891e0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
891f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
89200 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 oid sqlite3Value
89210 46 72 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c Free(sqlite3_val
89220 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ue*);.SQLITE_PRI
89230 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c VATE sqlite3_val
89240 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 ue *sqlite3Value
89250 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a New(sqlite3 *);.
89260 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
89270 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 31 har *sqlite3Utf1
89280 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 2c 20 6to8(sqlite3 *,
89290 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 const void*, int
892a0 2c 20 75 38 29 3b 0a 23 69 66 64 65 66 20 53 51 , u8);.#ifdef SQ
892b0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 LITE_ENABLE_STAT
892c0 33 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 3.SQLITE_PRIVATE
892d0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 char *sqlite3Ut
892e0 66 38 74 6f 31 36 28 73 71 6c 69 74 65 33 20 2a f8to16(sqlite3 *
892f0 2c 20 75 38 2c 20 63 68 61 72 20 2a 2c 20 69 6e , u8, char *, in
89300 74 2c 20 69 6e 74 20 2a 29 3b 0a 23 65 6e 64 69 t, int *);.#endi
89310 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 f.SQLITE_PRIVATE
89320 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 int sqlite3Valu
89330 65 46 72 6f 6d 45 78 70 72 28 73 71 6c 69 74 65 eFromExpr(sqlite
89340 33 20 2a 2c 20 45 78 70 72 20 2a 2c 20 75 38 2c 3 *, Expr *, u8,
89350 20 75 38 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c u8, sqlite3_val
89360 75 65 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ue **);.SQLITE_P
89370 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
89380 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 te3ValueApplyAff
89390 69 6e 69 74 79 28 73 71 6c 69 74 65 33 5f 76 61 inity(sqlite3_va
893a0 6c 75 65 20 2a 2c 20 75 38 2c 20 75 38 29 3b 0a lue *, u8, u8);.
893b0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 #ifndef SQLITE_A
893c0 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 MALGAMATION.SQLI
893d0 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
893e0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 unsigned char s
893f0 71 6c 69 74 65 33 4f 70 63 6f 64 65 50 72 6f 70 qlite3OpcodeProp
89400 65 72 74 79 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 erty[];.SQLITE_P
89410 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 RIVATE const uns
89420 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 igned char sqlit
89430 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d e3UpperToLower[]
89440 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
89450 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
89460 63 68 61 72 20 73 71 6c 69 74 65 33 43 74 79 70 char sqlite3Ctyp
89470 65 4d 61 70 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 eMap[];.SQLITE_P
89480 52 49 56 41 54 45 20 63 6f 6e 73 74 20 54 6f 6b RIVATE const Tok
89490 65 6e 20 73 71 6c 69 74 65 33 49 6e 74 54 6f 6b en sqlite3IntTok
894a0 65 6e 73 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 ens[];.SQLITE_PR
894b0 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 IVATE SQLITE_WSD
894c0 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 struct Sqlite3C
894d0 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e onfig sqlite3Con
894e0 66 69 67 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 fig;.SQLITE_PRIV
894f0 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 46 ATE SQLITE_WSD F
89500 75 6e 63 44 65 66 48 61 73 68 20 73 71 6c 69 74 uncDefHash sqlit
89510 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e e3GlobalFunction
89520 73 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 s;.#ifndef SQLIT
89530 45 5f 4f 4d 49 54 5f 57 53 44 0a 53 51 4c 49 54 E_OMIT_WSD.SQLIT
89540 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
89550 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 lite3PendingByte
89560 3b 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a ;.#endif.#endif.
89570 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
89580 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 oid sqlite3RootP
89590 61 67 65 4d 6f 76 65 64 28 73 71 6c 69 74 65 33 ageMoved(sqlite3
895a0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 *, int, int, int
895b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
895c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 E void sqlite3Re
895d0 69 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 54 6f index(Parse*, To
895e0 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 ken*, Token*);.S
895f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
89600 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 id sqlite3AlterF
89610 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a unctions(void);.
89620 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
89630 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 oid sqlite3Alter
89640 52 65 6e 61 6d 65 54 61 62 6c 65 28 50 61 72 73 RenameTable(Pars
89650 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f e*, SrcList*, To
89660 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
89670 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
89680 33 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 3GetToken(const
89690 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c unsigned char *,
896a0 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f int *);.SQLITE_
896b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
896c0 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 ite3NestedParse(
896d0 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 Parse*, const ch
896e0 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 ar*, ...);.SQLIT
896f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
89700 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 qlite3ExpirePrep
89710 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73 aredStatements(s
89720 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 qlite3*);.SQLITE
89730 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
89740 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 ite3CodeSubselec
89750 74 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 t(Parse *, Expr
89760 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 *, int, int);.SQ
89770 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
89780 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 d sqlite3SelectP
89790 72 65 70 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 rep(Parse*, Sele
897a0 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 ct*, NameContext
897b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
897c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 TE int sqlite3Re
897d0 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 4e solveExprNames(N
897e0 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70 ameContext*, Exp
897f0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
89800 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
89810 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d ResolveSelectNam
89820 65 73 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 es(Parse*, Selec
89830 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a t*, NameContext*
89840 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
89850 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 E int sqlite3Res
89860 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 olveOrderGroupBy
89870 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a (Parse*, Select*
89880 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e , ExprList*, con
89890 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 st char*);.SQLIT
898a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
898b0 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 qlite3ColumnDefa
898c0 75 6c 74 28 56 64 62 65 20 2a 2c 20 54 61 62 6c ult(Vdbe *, Tabl
898d0 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a e *, int, int);.
898e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
898f0 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 oid sqlite3Alter
89900 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 FinishAddColumn(
89910 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a Parse *, Token *
89920 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
89930 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c E void sqlite3Al
89940 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d terBeginAddColum
89950 6e 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 n(Parse *, SrcLi
89960 73 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 st *);.SQLITE_PR
89970 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 IVATE CollSeq *s
89980 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 qlite3GetCollSeq
89990 28 50 61 72 73 65 2a 2c 20 75 38 2c 20 43 6f 6c (Parse*, u8, Col
899a0 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 74 20 63 68 lSeq *, const ch
899b0 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
899c0 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 VATE char sqlite
899d0 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 63 6f 3AffinityType(co
899e0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
899f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
89a00 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 50 sqlite3Analyze(P
89a10 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 arse*, Token*, T
89a20 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
89a30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
89a40 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 e3InvokeBusyHand
89a50 6c 65 72 28 42 75 73 79 48 61 6e 64 6c 65 72 2a ler(BusyHandler*
89a60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
89a70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e E int sqlite3Fin
89a80 64 44 62 28 73 71 6c 69 74 65 33 2a 2c 20 54 6f dDb(sqlite3*, To
89a90 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
89aa0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
89ab0 33 46 69 6e 64 44 62 4e 61 6d 65 28 73 71 6c 69 3FindDbName(sqli
89ac0 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 te3 *, const cha
89ad0 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 r *);.SQLITE_PRI
89ae0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
89af0 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 73 71 6c AnalysisLoad(sql
89b00 69 74 65 33 2a 2c 69 6e 74 20 69 44 42 29 3b 0a ite3*,int iDB);.
89b10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
89b20 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 oid sqlite3Delet
89b30 65 49 6e 64 65 78 53 61 6d 70 6c 65 73 28 73 71 eIndexSamples(sq
89b40 6c 69 74 65 33 2a 2c 49 6e 64 65 78 2a 29 3b 0a lite3*,Index*);.
89b50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
89b60 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 61 75 oid sqlite3Defau
89b70 6c 74 52 6f 77 45 73 74 28 49 6e 64 65 78 2a 29 ltRowEst(Index*)
89b80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
89b90 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 void sqlite3Reg
89ba0 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f isterLikeFunctio
89bb0 6e 73 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 ns(sqlite3*, int
89bc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
89bd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c E int sqlite3IsL
89be0 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 ikeFunction(sqli
89bf0 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 2a 2c te3*,Expr*,int*,
89c00 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
89c10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
89c20 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f te3MinimumFileFo
89c30 72 6d 61 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 rmat(Parse*, int
89c40 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
89c50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
89c60 74 65 33 53 63 68 65 6d 61 43 6c 65 61 72 28 76 te3SchemaClear(v
89c70 6f 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oid *);.SQLITE_P
89c80 52 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a 73 RIVATE Schema *s
89c90 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 qlite3SchemaGet(
89ca0 73 71 6c 69 74 65 33 20 2a 2c 20 42 74 72 65 65 sqlite3 *, Btree
89cb0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
89cc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 ATE int sqlite3S
89cd0 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c chemaToIndex(sql
89ce0 69 74 65 33 20 2a 64 62 2c 20 53 63 68 65 6d 61 ite3 *db, Schema
89cf0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
89d00 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c ATE KeyInfo *sql
89d10 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f ite3IndexKeyinfo
89d20 28 50 61 72 73 65 20 2a 2c 20 49 6e 64 65 78 20 (Parse *, Index
89d30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
89d40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72 TE int sqlite3Cr
89d50 65 61 74 65 46 75 6e 63 28 73 71 6c 69 74 65 33 eateFunc(sqlite3
89d60 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
89d70 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 , int, int, void
89d80 20 2a 2c 20 0a 20 20 76 6f 69 64 20 28 2a 29 28 *, . void (*)(
89d90 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
89da0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c ,int,sqlite3_val
89db0 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 ue **),. void (
89dc0 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 *)(sqlite3_conte
89dd0 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f xt*,int,sqlite3_
89de0 76 61 6c 75 65 20 2a 2a 29 2c 20 76 6f 69 64 20 value **), void
89df0 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (*)(sqlite3_cont
89e00 65 78 74 2a 29 2c 0a 20 20 46 75 6e 63 44 65 73 ext*),. FuncDes
89e10 74 72 75 63 74 6f 72 20 2a 70 44 65 73 74 72 75 tructor *pDestru
89e20 63 74 6f 72 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 ctor.);.SQLITE_P
89e30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
89e40 65 33 41 70 69 45 78 69 74 28 73 71 6c 69 74 65 e3ApiExit(sqlite
89e50 33 20 2a 64 62 2c 20 69 6e 74 29 3b 0a 53 51 4c 3 *db, int);.SQL
89e60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
89e70 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 sqlite3OpenTempD
89e80 61 74 61 62 61 73 65 28 50 61 72 73 65 20 2a 29 atabase(Parse *)
89e90 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
89ea0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 E void sqlite3St
89eb0 72 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 63 rAccumInit(StrAc
89ec0 63 75 6d 2a 2c 20 63 68 61 72 2a 2c 20 69 6e 74 cum*, char*, int
89ed0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
89ee0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
89ef0 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
89f00 64 28 53 74 72 41 63 63 75 6d 2a 2c 63 6f 6e 73 d(StrAccum*,cons
89f10 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 t char*,int);.SQ
89f20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
89f30 64 20 73 71 6c 69 74 65 33 41 70 70 65 6e 64 53 d sqlite3AppendS
89f40 70 61 63 65 28 53 74 72 41 63 63 75 6d 2a 2c 69 pace(StrAccum*,i
89f50 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
89f60 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
89f70 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 3StrAccumFinish(
89f80 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49 StrAccum*);.SQLI
89f90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
89fa0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 sqlite3StrAccumR
89fb0 65 73 65 74 28 53 74 72 41 63 63 75 6d 2a 29 3b eset(StrAccum*);
89fc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
89fd0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 void sqlite3Sele
89fe0 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c 65 63 ctDestInit(Selec
89ff0 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b tDest*,int,int);
8a000 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
8a010 45 78 70 72 20 2a 73 71 6c 69 74 65 33 43 72 65 Expr *sqlite3Cre
8a020 61 74 65 43 6f 6c 75 6d 6e 45 78 70 72 28 73 71 ateColumnExpr(sq
8a030 6c 69 74 65 33 20 2a 2c 20 53 72 63 4c 69 73 74 lite3 *, SrcList
8a040 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a *, int, int);..
8a050 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
8a060 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 oid sqlite3Backu
8a070 70 52 65 73 74 61 72 74 28 73 71 6c 69 74 65 33 pRestart(sqlite3
8a080 5f 62 61 63 6b 75 70 20 2a 29 3b 0a 53 51 4c 49 _backup *);.SQLI
8a090 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
8a0a0 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 sqlite3BackupUpd
8a0b0 61 74 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b ate(sqlite3_back
8a0c0 75 70 20 2a 2c 20 50 67 6e 6f 2c 20 63 6f 6e 73 up *, Pgno, cons
8a0d0 74 20 75 38 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 t u8 *);../*.**
8a0e0 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f The interface to
8a0f0 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72 the LEMON-gener
8a100 61 74 65 64 20 70 61 72 73 65 72 0a 2a 2f 0a 53 ated parser.*/.S
8a110 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
8a120 69 64 20 2a 73 71 6c 69 74 65 33 50 61 72 73 65 id *sqlite3Parse
8a130 72 41 6c 6c 6f 63 28 76 6f 69 64 2a 28 2a 29 28 rAlloc(void*(*)(
8a140 73 69 7a 65 5f 74 29 29 3b 0a 53 51 4c 49 54 45 size_t));.SQLITE
8a150 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
8a160 6c 69 74 65 33 50 61 72 73 65 72 46 72 65 65 28 lite3ParserFree(
8a170 76 6f 69 64 2a 2c 20 76 6f 69 64 28 2a 29 28 76 void*, void(*)(v
8a180 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 oid*));.SQLITE_P
8a190 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
8a1a0 74 65 33 50 61 72 73 65 72 28 76 6f 69 64 2a 2c te3Parser(void*,
8a1b0 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20 50 61 72 int, Token, Par
8a1c0 73 65 2a 29 3b 0a 23 69 66 64 65 66 20 59 59 54 se*);.#ifdef YYT
8a1d0 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 RACKMAXSTACKDEPT
8a1e0 48 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 H.SQLITE_PRIVATE
8a1f0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 int sqlite3Pa
8a200 72 73 65 72 53 74 61 63 6b 50 65 61 6b 28 76 6f rserStackPeak(vo
8a210 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 id*);.#endif..SQ
8a220 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
8a230 64 20 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 d sqlite3AutoLoa
8a240 64 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 dExtensions(sqli
8a250 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 te3*);.#ifndef S
8a260 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
8a270 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 EXTENSION.SQLITE
8a280 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
8a290 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 sqlite3CloseExte
8a2a0 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 nsions(sqlite3*)
8a2b0 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ;.#else.# define
8a2c0 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 sqlite3CloseExt
8a2d0 65 6e 73 69 6f 6e 73 28 58 29 0a 23 65 6e 64 69 ensions(X).#endi
8a2e0 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
8a2f0 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
8a300 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 CHE.SQLITE_PRIVA
8a310 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
8a320 33 54 61 62 6c 65 4c 6f 63 6b 28 50 61 72 73 65 3TableLock(Parse
8a330 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 75 38 *, int, int, u8
8a340 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b , const char *);
8a350 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
8a360 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 sqlite3TableLoc
8a370 6b 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e k(v,w,x,y,z).#en
8a380 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
8a390 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 TE_TEST.SQLITE_P
8a3a0 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c RIVATE int sql
8a3b0 69 74 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69 ite3Utf8To8(unsi
8a3c0 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 23 65 6e gned char*);.#en
8a3d0 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
8a3e0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
8a3f0 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 73 ABLE.# define s
8a400 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 qlite3VtabClear(
8a410 59 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c Y).# define sql
8a420 69 74 65 33 56 74 61 62 53 79 6e 63 28 58 2c 59 ite3VtabSync(X,Y
8a430 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 20 64 ) SQLITE_OK.# d
8a440 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 efine sqlite3Vta
8a450 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a 23 20 20 bRollback(X).#
8a460 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 define sqlite3Vt
8a470 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23 20 20 64 abCommit(X).# d
8a480 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 efine sqlite3Vta
8a490 62 49 6e 53 79 6e 63 28 64 62 29 20 30 0a 23 20 bInSync(db) 0.#
8a4a0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 define sqlite3V
8a4b0 74 61 62 4c 6f 63 6b 28 58 29 20 0a 23 20 20 64 tabLock(X) .# d
8a4c0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 efine sqlite3Vta
8a4d0 62 55 6e 6c 6f 63 6b 28 58 29 0a 23 20 20 64 65 bUnlock(X).# de
8a4e0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 fine sqlite3Vtab
8a4f0 55 6e 6c 6f 63 6b 4c 69 73 74 28 58 29 0a 23 20 UnlockList(X).#
8a500 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 define sqlite3V
8a510 74 61 62 53 61 76 65 70 6f 69 6e 74 28 58 2c 20 tabSavepoint(X,
8a520 59 2c 20 5a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a Y, Z) SQLITE_OK.
8a530 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 # define sqlite
8a540 33 47 65 74 56 54 61 62 6c 65 28 58 2c 59 29 20 3GetVTable(X,Y)
8a550 20 28 28 56 54 61 62 6c 65 2a 29 30 29 0a 23 65 ((VTable*)0).#e
8a560 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 lse.SQLITE_PRIVA
8a570 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 TE void sqlit
8a580 65 33 56 74 61 62 43 6c 65 61 72 28 73 71 6c 69 e3VtabClear(sqli
8a590 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 2a 29 te3 *db, Table*)
8a5a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
8a5b0 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 void sqlite3
8a5c0 56 74 61 62 44 69 73 63 6f 6e 6e 65 63 74 28 73 VtabDisconnect(s
8a5d0 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c qlite3 *db, Tabl
8a5e0 65 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 e *p);.SQLITE_PR
8a5f0 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c IVATE int sql
8a600 69 74 65 33 56 74 61 62 53 79 6e 63 28 73 71 6c ite3VtabSync(sql
8a610 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a ite3 *db, char *
8a620 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
8a630 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 TE int sqlite
8a640 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 3VtabRollback(sq
8a650 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 lite3 *db);.SQLI
8a660 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e TE_PRIVATE in
8a670 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d t sqlite3VtabCom
8a680 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 mit(sqlite3 *db)
8a690 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
8a6a0 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 void sqlite3
8a6b0 56 74 61 62 4c 6f 63 6b 28 56 54 61 62 6c 65 20 VtabLock(VTable
8a6c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
8a6d0 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 TE void sqlit
8a6e0 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54 61 e3VtabUnlock(VTa
8a6f0 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ble *);.SQLITE_P
8a700 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 RIVATE void s
8a710 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b qlite3VtabUnlock
8a720 4c 69 73 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a List(sqlite3*);.
8a730 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
8a740 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 int sqlite3Vta
8a750 62 53 61 76 65 70 6f 69 6e 74 28 73 71 6c 69 74 bSavepoint(sqlit
8a760 65 33 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b e3 *, int, int);
8a770 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
8a780 20 20 20 56 54 61 62 6c 65 20 2a 73 71 6c 69 74 VTable *sqlit
8a790 65 33 47 65 74 56 54 61 62 6c 65 28 73 71 6c 69 e3GetVTable(sqli
8a7a0 74 65 33 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 23 te3*, Table*);.#
8a7b0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
8a7c0 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 28 VtabInSync(db) (
8a7d0 28 64 62 29 2d 3e 6e 56 54 72 61 6e 73 3e 30 20 (db)->nVTrans>0
8a7e0 26 26 20 28 64 62 29 2d 3e 61 56 54 72 61 6e 73 && (db)->aVTrans
8a7f0 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 ==0).#endif.SQLI
8a800 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
8a810 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 sqlite3VtabMakeW
8a820 72 69 74 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 ritable(Parse*,T
8a830 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 able*);.SQLITE_P
8a840 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
8a850 74 65 33 56 74 61 62 42 65 67 69 6e 50 61 72 73 te3VtabBeginPars
8a860 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a e(Parse*, Token*
8a870 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a , Token*, Token*
8a880 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
8a890 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
8a8a0 74 65 33 56 74 61 62 46 69 6e 69 73 68 50 61 72 te3VtabFinishPar
8a8b0 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e se(Parse*, Token
8a8c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
8a8d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
8a8e0 74 61 62 41 72 67 49 6e 69 74 28 50 61 72 73 65 tabArgInit(Parse
8a8f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
8a900 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
8a910 74 61 62 41 72 67 45 78 74 65 6e 64 28 50 61 72 tabArgExtend(Par
8a920 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 se*, Token*);.SQ
8a930 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
8a940 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c sqlite3VtabCall
8a950 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 2c Create(sqlite3*,
8a960 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 int, const char
8a970 20 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 *, char **);.SQ
8a980 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
8a990 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c sqlite3VtabCall
8a9a0 43 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a 2c 20 Connect(Parse*,
8a9b0 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Table*);.SQLITE_
8a9c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
8a9d0 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 te3VtabCallDestr
8a9e0 6f 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 oy(sqlite3*, int
8a9f0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b , const char *);
8aa00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
8aa10 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 42 int sqlite3VtabB
8aa20 65 67 69 6e 28 73 71 6c 69 74 65 33 20 2a 2c 20 egin(sqlite3 *,
8aa30 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 VTable *);.SQLIT
8aa40 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 E_PRIVATE FuncDe
8aa50 66 20 2a 73 71 6c 69 74 65 33 56 74 61 62 4f 76 f *sqlite3VtabOv
8aa60 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 73 erloadFunction(s
8aa70 71 6c 69 74 65 33 20 2a 2c 46 75 6e 63 44 65 66 qlite3 *,FuncDef
8aa80 2a 2c 20 69 6e 74 20 6e 41 72 67 2c 20 45 78 70 *, int nArg, Exp
8aa90 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
8aaa0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
8aab0 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 InvalidFunction(
8aac0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
8aad0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c ,int,sqlite3_val
8aae0 75 65 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ue**);.SQLITE_PR
8aaf0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
8ab00 33 56 64 62 65 50 61 72 61 6d 65 74 65 72 49 6e 3VdbeParameterIn
8ab10 64 65 78 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 dex(Vdbe*, const
8ab20 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 char*, int);.SQ
8ab30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
8ab40 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72 sqlite3Transfer
8ab50 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 Bindings(sqlite3
8ab60 5f 73 74 6d 74 20 2a 2c 20 73 71 6c 69 74 65 33 _stmt *, sqlite3
8ab70 5f 73 74 6d 74 20 2a 29 3b 0a 53 51 4c 49 54 45 _stmt *);.SQLITE
8ab80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
8ab90 69 74 65 33 52 65 70 72 65 70 61 72 65 28 56 64 ite3Reprepare(Vd
8aba0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
8abb0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
8abc0 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 3ExprListCheckLe
8abd0 6e 67 74 68 28 50 61 72 73 65 2a 2c 20 45 78 70 ngth(Parse*, Exp
8abe0 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68 rList*, const ch
8abf0 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
8ac00 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 VATE CollSeq *sq
8ac10 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 lite3BinaryCompa
8ac20 72 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 reCollSeq(Parse
8ac30 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 20 *, Expr *, Expr
8ac40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
8ac50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 65 TE int sqlite3Te
8ac60 6d 70 49 6e 4d 65 6d 6f 72 79 28 63 6f 6e 73 74 mpInMemory(const
8ac70 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 sqlite3*);.SQLI
8ac80 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
8ac90 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4a 6f char *sqlite3Jo
8aca0 75 72 6e 61 6c 4d 6f 64 65 6e 61 6d 65 28 69 6e urnalModename(in
8acb0 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 t);.#ifndef SQLI
8acc0 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 53 51 4c 49 TE_OMIT_WAL.SQLI
8acd0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
8ace0 20 73 71 6c 69 74 65 33 43 68 65 63 6b 70 6f 69 sqlite3Checkpoi
8acf0 6e 74 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 nt(sqlite3*, int
8ad00 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69 6e 74 , int, int*, int
8ad10 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
8ad20 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
8ad30 57 61 6c 44 65 66 61 75 6c 74 48 6f 6f 6b 28 76 WalDefaultHook(v
8ad40 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 63 6f oid*,sqlite3*,co
8ad50 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a nst char*,int);.
8ad60 23 65 6e 64 69 66 0a 0a 2f 2a 20 44 65 63 6c 61 #endif../* Decla
8ad70 72 61 74 69 6f 6e 73 20 66 6f 72 20 66 75 6e 63 rations for func
8ad80 74 69 6f 6e 73 20 69 6e 20 66 6b 65 79 2e 63 2e tions in fkey.c.
8ad90 20 41 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72 All of these ar
8ada0 65 20 72 65 70 6c 61 63 65 64 20 62 79 0a 2a 2a e replaced by.**
8adb0 20 6e 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 69 66 no-op macros if
8adc0 20 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 OMIT_FOREIGN_KE
8add0 59 20 69 73 20 64 65 66 69 6e 65 64 2e 20 49 6e Y is defined. In
8ade0 20 74 68 69 73 20 63 61 73 65 20 6e 6f 20 66 6f this case no fo
8adf0 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 20 66 75 6e reign.** key fun
8ae00 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 61 76 ctionality is av
8ae10 61 69 6c 61 62 6c 65 2e 20 49 66 20 4f 4d 49 54 ailable. If OMIT
8ae20 5f 54 52 49 47 47 45 52 20 69 73 20 64 65 66 69 _TRIGGER is defi
8ae30 6e 65 64 20 62 75 74 0a 2a 2a 20 4f 4d 49 54 5f ned but.** OMIT_
8ae40 46 4f 52 45 49 47 4e 5f 4b 45 59 20 69 73 20 6e FOREIGN_KEY is n
8ae50 6f 74 2c 20 6f 6e 6c 79 20 73 6f 6d 65 20 6f 66 ot, only some of
8ae60 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 the functions a
8ae70 72 65 20 6e 6f 2d 6f 70 65 64 2e 20 49 6e 0a 2a re no-oped. In.*
8ae80 2a 20 74 68 69 73 20 63 61 73 65 20 66 6f 72 65 * this case fore
8ae90 69 67 6e 20 6b 65 79 73 20 61 72 65 20 70 61 72 ign keys are par
8aea0 73 65 64 2c 20 62 75 74 20 6e 6f 20 6f 74 68 65 sed, but no othe
8aeb0 72 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 r functionality
8aec0 69 73 20 0a 2a 2a 20 70 72 6f 76 69 64 65 64 20 is .** provided
8aed0 28 65 6e 66 6f 72 63 65 6d 65 6e 74 20 6f 66 20 (enforcement of
8aee0 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 72 FK constraints r
8aef0 65 71 75 69 72 65 73 20 74 68 65 20 74 72 69 67 equires the trig
8af00 67 65 72 73 20 73 75 62 2d 73 79 73 74 65 6d 29 gers sub-system)
8af10 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 ..*/.#if !define
8af20 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f d(SQLITE_OMIT_FO
8af30 52 45 49 47 4e 5f 4b 45 59 29 20 26 26 20 21 64 REIGN_KEY) && !d
8af40 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
8af50 49 54 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49 IT_TRIGGER).SQLI
8af60 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
8af70 64 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b d sqlite3FkCheck
8af80 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
8af90 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
8afa0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
8afb0 64 20 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54 d sqlite3FkDropT
8afc0 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 able(Parse*, Src
8afd0 4c 69 73 74 20 2a 2c 20 54 61 62 6c 65 2a 29 3b List *, Table*);
8afe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
8aff0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b void sqlite3Fk
8b000 41 63 74 69 6f 6e 73 28 50 61 72 73 65 2a 2c 20 Actions(Parse*,
8b010 54 61 62 6c 65 2a 2c 20 45 78 70 72 4c 69 73 74 Table*, ExprList
8b020 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
8b030 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
8b040 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 lite3FkRequired(
8b050 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 Parse*, Table*,
8b060 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int*, int);.SQLI
8b070 54 45 5f 50 52 49 56 41 54 45 20 20 20 75 33 32 TE_PRIVATE u32
8b080 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 73 sqlite3FkOldmas
8b090 6b 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a k(Parse*, Table*
8b0a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
8b0b0 45 20 20 20 46 4b 65 79 20 2a 73 71 6c 69 74 65 E FKey *sqlite
8b0c0 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 54 61 3FkReferences(Ta
8b0d0 62 6c 65 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 ble *);.#else.
8b0e0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 #define sqlite3F
8b0f0 6b 41 63 74 69 6f 6e 73 28 61 2c 62 2c 63 2c 64 kActions(a,b,c,d
8b100 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 ). #define sqli
8b110 74 65 33 46 6b 43 68 65 63 6b 28 61 2c 62 2c 63 te3FkCheck(a,b,c
8b120 2c 64 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 ,d). #define sq
8b130 6c 69 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65 lite3FkDropTable
8b140 28 61 2c 62 2c 63 29 0a 20 20 23 64 65 66 69 6e (a,b,c). #defin
8b150 65 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 e sqlite3FkOldma
8b160 73 6b 28 61 2c 62 29 20 20 20 20 20 20 30 0a 20 sk(a,b) 0.
8b170 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
8b180 46 6b 52 65 71 75 69 72 65 64 28 61 2c 62 2c 63 FkRequired(a,b,c
8b190 2c 64 29 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 ,d) 0.#endif.#if
8b1a0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
8b1b0 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 53 51 4c _FOREIGN_KEY.SQL
8b1c0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
8b1d0 69 64 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 id sqlite3FkDele
8b1e0 74 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 54 61 te(sqlite3 *, Ta
8b1f0 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 ble*);.#else. #
8b200 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b define sqlite3Fk
8b210 44 65 6c 65 74 65 28 61 2c 62 29 0a 23 65 6e 64 Delete(a,b).#end
8b220 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 76 61 69 6c if.../*.** Avail
8b230 61 62 6c 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 able fault injec
8b240 74 6f 72 73 2e 20 20 53 68 6f 75 6c 64 20 62 65 tors. Should be
8b250 20 6e 75 6d 62 65 72 65 64 20 62 65 67 69 6e 6e numbered beginn
8b260 69 6e 67 20 77 69 74 68 20 30 2e 0a 2a 2f 0a 23 ing with 0..*/.#
8b270 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 define SQLITE_FA
8b280 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c ULTINJECTOR_MALL
8b290 4f 43 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 OC 0.#define
8b2a0 20 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a SQLITE_FAULTINJ
8b2b0 45 43 54 4f 52 5f 43 4f 55 4e 54 20 20 20 20 20 ECTOR_COUNT
8b2c0 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 1../*.** The in
8b2d0 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 63 terface to the c
8b2e0 6f 64 65 20 69 6e 20 66 61 75 6c 74 2e 63 20 75 ode in fault.c u
8b2f0 73 65 64 20 66 6f 72 20 69 64 65 6e 74 69 66 79 sed for identify
8b300 69 6e 67 20 22 62 65 6e 69 67 6e 22 0a 2a 2a 20 ing "benign".**
8b310 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 2e malloc failures.
8b320 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 70 72 This is only pr
8b330 65 73 65 6e 74 20 69 66 20 53 51 4c 49 54 45 5f esent if SQLITE_
8b340 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 OMIT_BUILTIN_TES
8b350 54 0a 2a 2a 20 69 73 20 6e 6f 74 20 64 65 66 69 T.** is not defi
8b360 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ned..*/.#ifndef
8b370 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
8b380 54 49 4e 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f TIN_TEST.SQLITE_
8b390 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
8b3a0 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
8b3b0 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 53 nMalloc(void);.S
8b3c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
8b3d0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42 void sqlite3EndB
8b3e0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 enignMalloc(void
8b3f0 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 );.#else. #defi
8b400 6e 65 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 ne sqlite3BeginB
8b410 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 20 20 enignMalloc().
8b420 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 #define sqlite3E
8b430 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 ndBenignMalloc()
8b440 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 .#endif..#define
8b450 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 IN_INDEX_ROWID
8b460 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 1.#def
8b470 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 ine IN_INDEX_EPH
8b480 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 2.#
8b490 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f define IN_INDEX_
8b4a0 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 INDEX
8b4b0 33 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 3.SQLITE_PRIVATE
8b4c0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 int sqlite3Find
8b4d0 49 6e 49 6e 64 65 78 28 50 61 72 73 65 20 2a 2c InIndex(Parse *,
8b4e0 20 45 78 70 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a Expr *, int*);.
8b4f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
8b500 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 NABLE_ATOMIC_WRI
8b510 54 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 TE.SQLITE_PRIVAT
8b520 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a E int sqlite3J
8b530 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 ournalOpen(sqlit
8b540 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 e3_vfs *, const
8b550 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f char *, sqlite3_
8b560 66 69 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 file *, int, int
8b570 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
8b580 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a E int sqlite3J
8b590 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 ournalSize(sqlit
8b5a0 65 33 5f 76 66 73 20 2a 29 3b 0a 53 51 4c 49 54 e3_vfs *);.SQLIT
8b5b0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
8b5c0 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 sqlite3JournalCr
8b5d0 65 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c eate(sqlite3_fil
8b5e0 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e *);.SQLITE_PRI
8b5f0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
8b600 65 33 4a 6f 75 72 6e 61 6c 45 78 69 73 74 73 28 e3JournalExists(
8b610 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 29 sqlite3_file *p)
8b620 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e ;.#else. #defin
8b630 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c e sqlite3Journal
8b640 53 69 7a 65 28 70 56 66 73 29 20 28 28 70 56 66 Size(pVfs) ((pVf
8b650 73 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a 20 20 s)->szOsFile).
8b660 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4a #define sqlite3J
8b670 6f 75 72 6e 61 6c 45 78 69 73 74 73 28 70 29 20 ournalExists(p)
8b680 31 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 1.#endif..SQLITE
8b690 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
8b6a0 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f lite3MemJournalO
8b6b0 70 65 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 pen(sqlite3_file
8b6c0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
8b6d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
8b6e0 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 76 6f emJournalSize(vo
8b6f0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
8b700 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
8b710 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 sMemJournal(sqli
8b720 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 te3_file *);..#i
8b730 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 f SQLITE_MAX_EXP
8b740 52 5f 44 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 R_DEPTH>0.SQLITE
8b750 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
8b760 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 sqlite3ExprSetHe
8b770 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 ight(Parse *pPar
8b780 73 65 2c 20 45 78 70 72 20 2a 70 29 3b 0a 53 51 se, Expr *p);.SQ
8b790 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
8b7a0 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 nt sqlite3Select
8b7b0 45 78 70 72 48 65 69 67 68 74 28 53 65 6c 65 63 ExprHeight(Selec
8b7c0 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 t *);.SQLITE_PRI
8b7d0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
8b7e0 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 e3ExprCheckHeigh
8b7f0 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a t(Parse*, int);.
8b800 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
8b810 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 sqlite3ExprSetHe
8b820 69 67 68 74 28 78 2c 79 29 0a 20 20 23 64 65 66 ight(x,y). #def
8b830 69 6e 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 ine sqlite3Selec
8b840 74 45 78 70 72 48 65 69 67 68 74 28 78 29 20 30 tExprHeight(x) 0
8b850 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
8b860 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 e3ExprCheckHeigh
8b870 74 28 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53 t(x,y).#endif..S
8b880 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 QLITE_PRIVATE u3
8b890 32 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 2 sqlite3Get4byt
8b8a0 65 28 63 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51 e(const u8*);.SQ
8b8b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
8b8c0 64 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 d sqlite3Put4byt
8b8d0 65 28 75 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 e(u8*, u32);..#i
8b8e0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
8b8f0 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 LE_UNLOCK_NOTIFY
8b900 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
8b910 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f void sqlite3Co
8b920 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 nnectionBlocked(
8b930 73 71 6c 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 sqlite3 *, sqlit
8b940 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 e3 *);.SQLITE_PR
8b950 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
8b960 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e ite3ConnectionUn
8b970 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a locked(sqlite3 *
8b980 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 db);.SQLITE_PRIV
8b990 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
8b9a0 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 e3ConnectionClos
8b9b0 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b ed(sqlite3 *db);
8b9c0 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
8b9d0 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 sqlite3Connecti
8b9e0 6f 6e 42 6c 6f 63 6b 65 64 28 78 2c 79 29 0a 20 onBlocked(x,y).
8b9f0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
8ba00 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b ConnectionUnlock
8ba10 65 64 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20 ed(x). #define
8ba20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f sqlite3Connectio
8ba30 6e 43 6c 6f 73 65 64 28 78 29 0a 23 65 6e 64 69 nClosed(x).#endi
8ba40 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
8ba50 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 _DEBUG.SQLITE_PR
8ba60 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
8ba70 69 74 65 33 50 61 72 73 65 72 54 72 61 63 65 28 ite3ParserTrace(
8ba80 46 49 4c 45 2a 2c 20 63 68 61 72 20 2a 29 3b 0a FILE*, char *);.
8ba90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 #endif../*.** If
8baa0 20 74 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 the SQLITE_ENAB
8bab0 4c 45 20 49 4f 54 52 41 43 45 20 65 78 69 73 74 LE IOTRACE exist
8bac0 73 20 74 68 65 6e 20 74 68 65 20 67 6c 6f 62 61 s then the globa
8bad0 6c 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 73 71 l variable.** sq
8bae0 6c 69 74 65 33 49 6f 54 72 61 63 65 20 69 73 20 lite3IoTrace is
8baf0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 a pointer to a p
8bb00 72 69 6e 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69 rintf-like routi
8bb10 6e 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 72 ne used to.** pr
8bb20 69 6e 74 20 49 2f 4f 20 74 72 61 63 69 6e 67 20 int I/O tracing
8bb30 6d 65 73 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 69 messages. .*/.#i
8bb40 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
8bb50 4c 45 5f 49 4f 54 52 41 43 45 0a 23 20 64 65 66 LE_IOTRACE.# def
8bb60 69 6e 65 20 49 4f 54 52 41 43 45 28 41 29 20 20 ine IOTRACE(A)
8bb70 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61 if( sqlite3IoTra
8bb80 63 65 20 29 7b 20 73 71 6c 69 74 65 33 49 6f 54 ce ){ sqlite3IoT
8bb90 72 61 63 65 20 41 3b 20 7d 0a 53 51 4c 49 54 45 race A; }.SQLITE
8bba0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
8bbb0 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 sqlite3VdbeIOTra
8bbc0 63 65 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 ceSql(Vdbe*);.SQ
8bbd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
8bbe0 64 20 28 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 d (*sqlite3IoTra
8bbf0 63 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c ce)(const char*,
8bc00 2e 2e 2e 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 ...);.#else.# de
8bc10 66 69 6e 65 20 49 4f 54 52 41 43 45 28 41 29 0a fine IOTRACE(A).
8bc20 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
8bc30 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 58 VdbeIOTraceSql(X
8bc40 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
8bc50 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 These routines a
8bc60 72 65 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 re available for
8bc70 20 74 68 65 20 6d 65 6d 32 2e 63 20 64 65 62 75 the mem2.c debu
8bc80 67 67 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c gging memory all
8bc90 6f 63 61 74 6f 72 0a 2a 2a 20 6f 6e 6c 79 2e 20 ocator.** only.
8bca0 20 54 68 65 79 20 61 72 65 20 75 73 65 64 20 74 They are used t
8bcb0 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 64 69 o verify that di
8bcc0 66 66 65 72 65 6e 74 20 22 74 79 70 65 73 22 20 fferent "types"
8bcd0 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c of memory.** all
8bce0 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 70 72 6f ocations are pro
8bcf0 70 65 72 6c 79 20 74 72 61 63 6b 65 64 20 62 79 perly tracked by
8bd00 20 74 68 65 20 73 79 73 74 65 6d 2e 0a 2a 2a 0a the system..**.
8bd10 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 ** sqlite3Memdeb
8bd20 75 67 53 65 74 54 79 70 65 28 29 20 73 65 74 73 ugSetType() sets
8bd30 20 74 68 65 20 22 74 79 70 65 22 20 6f 66 20 61 the "type" of a
8bd40 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 n allocation to
8bd50 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 4d 45 one of.** the ME
8bd60 4d 54 59 50 45 5f 2a 20 6d 61 63 72 6f 73 20 64 MTYPE_* macros d
8bd70 65 66 69 6e 65 64 20 62 65 6c 6f 77 2e 20 20 54 efined below. T
8bd80 68 65 20 74 79 70 65 20 6d 75 73 74 20 62 65 20 he type must be
8bd90 61 20 62 69 74 6d 61 73 6b 20 77 69 74 68 0a 2a a bitmask with.*
8bda0 2a 20 61 20 73 69 6e 67 6c 65 20 62 69 74 20 73 * a single bit s
8bdb0 65 74 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 et..**.** sqlite
8bdc0 33 4d 65 6d 64 65 62 75 67 48 61 73 54 79 70 65 3MemdebugHasType
8bdd0 28 29 20 72 65 74 75 72 6e 73 20 74 72 75 65 20 () returns true
8bde0 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 62 69 if any of the bi
8bdf0 74 73 20 69 6e 20 69 74 73 20 73 65 63 6f 6e 64 ts in its second
8be00 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 61 74 .** argument mat
8be10 63 68 20 74 68 65 20 74 79 70 65 20 73 65 74 20 ch the type set
8be20 62 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 by the previous
8be30 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 sqlite3MemdebugS
8be40 65 74 54 79 70 65 28 29 2e 0a 2a 2a 20 73 71 6c etType()..** sql
8be50 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54 ite3MemdebugHasT
8be60 79 70 65 28 29 20 69 73 20 69 6e 74 65 6e 64 65 ype() is intende
8be70 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 d for use inside
8be80 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
8be90 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 ents..**.** sqli
8bea0 74 65 33 4d 65 6d 64 65 62 75 67 4e 6f 54 79 70 te3MemdebugNoTyp
8beb0 65 28 29 20 72 65 74 75 72 6e 73 20 74 72 75 65 e() returns true
8bec0 20 69 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 if none of the
8bed0 62 69 74 73 20 69 6e 20 69 74 73 20 73 65 63 6f bits in its seco
8bee0 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d nd.** argument m
8bef0 61 74 63 68 20 74 68 65 20 74 79 70 65 20 73 65 atch the type se
8bf00 74 20 62 79 20 74 68 65 20 70 72 65 76 69 6f 75 t by the previou
8bf10 73 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 s sqlite3Memdebu
8bf20 67 53 65 74 54 79 70 65 28 29 2e 0a 2a 2a 0a 2a gSetType()..**.*
8bf30 2a 20 50 65 72 68 61 70 73 20 74 68 65 20 6d 6f * Perhaps the mo
8bf40 73 74 20 69 6d 70 6f 72 74 61 6e 74 20 70 6f 69 st important poi
8bf50 6e 74 20 69 73 20 74 68 65 20 64 69 66 66 65 72 nt is the differ
8bf60 65 6e 63 65 20 62 65 74 77 65 65 6e 20 4d 45 4d ence between MEM
8bf70 54 59 50 45 5f 48 45 41 50 0a 2a 2a 20 61 6e 64 TYPE_HEAP.** and
8bf80 20 4d 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 MEMTYPE_LOOKASI
8bf90 44 45 2e 20 20 49 66 20 61 6e 20 61 6c 6c 6f 63 DE. If an alloc
8bfa0 61 74 69 6f 6e 20 69 73 20 4d 45 4d 54 59 50 45 ation is MEMTYPE
8bfb0 5f 4c 4f 4f 4b 41 53 49 44 45 2c 20 74 68 61 74 _LOOKASIDE, that
8bfc0 20 6d 65 61 6e 73 0a 2a 2a 20 69 74 20 6d 69 67 means.** it mig
8bfd0 68 74 20 68 61 76 65 20 62 65 65 6e 20 61 6c 6c ht have been all
8bfe0 6f 63 61 74 65 64 20 62 79 20 6c 6f 6f 6b 61 73 ocated by lookas
8bff0 69 64 65 2c 20 65 78 63 65 70 74 20 74 68 65 20 ide, except the
8c000 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 0a 2a allocation was.*
8c010 2a 20 74 6f 6f 20 6c 61 72 67 65 20 6f 72 20 6c * too large or l
8c020 6f 6f 6b 61 73 69 64 65 20 77 61 73 20 61 6c 72 ookaside was alr
8c030 65 61 64 79 20 66 75 6c 6c 2e 20 20 49 74 20 69 eady full. It i
8c040 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 76 s important to v
8c050 65 72 69 66 79 0a 2a 2a 20 74 68 61 74 20 61 6c erify.** that al
8c060 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6d locations that m
8c070 69 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 73 ight have been s
8c080 61 74 69 73 66 69 65 64 20 62 79 20 6c 6f 6f 6b atisfied by look
8c090 61 73 69 64 65 20 61 72 65 20 6e 6f 74 0a 2a 2a aside are not.**
8c0a0 20 70 61 73 73 65 64 20 62 61 63 6b 20 74 6f 20 passed back to
8c0b0 6e 6f 6e 2d 6c 6f 6f 6b 61 73 69 64 65 20 66 72 non-lookaside fr
8c0c0 65 65 28 29 20 72 6f 75 74 69 6e 65 73 2e 20 20 ee() routines.
8c0d0 41 73 73 65 72 74 73 20 73 75 63 68 20 61 73 20 Asserts such as
8c0e0 74 68 65 0a 2a 2a 20 65 78 61 6d 70 6c 65 20 61 the.** example a
8c0f0 62 6f 76 65 20 61 72 65 20 70 6c 61 63 65 64 20 bove are placed
8c100 6f 6e 20 74 68 65 20 6e 6f 6e 2d 6c 6f 6f 6b 61 on the non-looka
8c110 73 69 64 65 20 66 72 65 65 28 29 20 72 6f 75 74 side free() rout
8c120 69 6e 65 73 20 74 6f 20 76 65 72 69 66 79 0a 2a ines to verify.*
8c130 2a 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e * this constrain
8c140 74 2e 20 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 t. .**.** All of
8c150 20 74 68 69 73 20 69 73 20 6e 6f 2d 6f 70 20 66 this is no-op f
8c160 6f 72 20 61 20 70 72 6f 64 75 63 74 69 6f 6e 20 or a production
8c170 62 75 69 6c 64 2e 20 20 49 74 20 6f 6e 6c 79 20 build. It only
8c180 63 6f 6d 65 73 20 69 6e 74 6f 0a 2a 2a 20 70 6c comes into.** pl
8c190 61 79 20 77 68 65 6e 20 74 68 65 20 53 51 4c 49 ay when the SQLI
8c1a0 54 45 5f 4d 45 4d 44 45 42 55 47 20 63 6f 6d 70 TE_MEMDEBUG comp
8c1b0 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 ile-time option
8c1c0 69 73 20 75 73 65 64 2e 0a 2a 2f 0a 23 69 66 64 is used..*/.#ifd
8c1d0 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 ef SQLITE_MEMDEB
8c1e0 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
8c1f0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
8c200 4d 65 6d 64 65 62 75 67 53 65 74 54 79 70 65 28 MemdebugSetType(
8c210 76 6f 69 64 2a 2c 75 38 29 3b 0a 53 51 4c 49 54 void*,u8);.SQLIT
8c220 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
8c230 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 48 sqlite3MemdebugH
8c240 61 73 54 79 70 65 28 76 6f 69 64 2a 2c 75 38 29 asType(void*,u8)
8c250 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
8c260 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 int sqlite3Me
8c270 6d 64 65 62 75 67 4e 6f 54 79 70 65 28 76 6f 69 mdebugNoType(voi
8c280 64 2a 2c 75 38 29 3b 0a 23 65 6c 73 65 0a 23 20 d*,u8);.#else.#
8c290 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 65 define sqlite3Me
8c2a0 6d 64 65 62 75 67 53 65 74 54 79 70 65 28 58 2c mdebugSetType(X,
8c2b0 59 29 20 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a Y) /* no-op */.
8c2c0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
8c2d0 4d 65 6d 64 65 62 75 67 48 61 73 54 79 70 65 28 MemdebugHasType(
8c2e0 58 2c 59 29 20 20 31 0a 23 20 64 65 66 69 6e 65 X,Y) 1.# define
8c2f0 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 sqlite3Memdebug
8c300 4e 6f 54 79 70 65 28 58 2c 59 29 20 20 20 31 0a NoType(X,Y) 1.
8c310 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 4d #endif.#define M
8c320 45 4d 54 59 50 45 5f 48 45 41 50 20 20 20 20 20 EMTYPE_HEAP
8c330 20 20 30 78 30 31 20 20 2f 2a 20 47 65 6e 65 72 0x01 /* Gener
8c340 61 6c 20 68 65 61 70 20 61 6c 6c 6f 63 61 74 69 al heap allocati
8c350 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d ons */.#define M
8c360 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 EMTYPE_LOOKASIDE
8c370 20 20 30 78 30 32 20 20 2f 2a 20 4d 69 67 68 74 0x02 /* Might
8c380 20 68 61 76 65 20 62 65 65 6e 20 6c 6f 6f 6b 61 have been looka
8c390 73 69 64 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a 23 side memory */.#
8c3a0 64 65 66 69 6e 65 20 4d 45 4d 54 59 50 45 5f 53 define MEMTYPE_S
8c3b0 43 52 41 54 43 48 20 20 20 20 30 78 30 34 20 20 CRATCH 0x04
8c3c0 2f 2a 20 53 63 72 61 74 63 68 20 61 6c 6c 6f 63 /* Scratch alloc
8c3d0 61 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e ations */.#defin
8c3e0 65 20 4d 45 4d 54 59 50 45 5f 50 43 41 43 48 45 e MEMTYPE_PCACHE
8c3f0 20 20 20 20 20 30 78 30 38 20 20 2f 2a 20 50 61 0x08 /* Pa
8c400 67 65 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 ge cache allocat
8c410 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ions */.#define
8c420 4d 45 4d 54 59 50 45 5f 44 42 20 20 20 20 20 20 MEMTYPE_DB
8c430 20 20 20 30 78 31 30 20 20 2f 2a 20 55 73 65 73 0x10 /* Uses
8c440 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
8c450 2c 20 6e 6f 74 20 73 71 6c 69 74 65 5f 6d 61 6c , not sqlite_mal
8c460 6c 6f 63 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f loc */..#endif /
8c470 2a 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f 20 * _SQLITEINT_H_
8c480 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
8c490 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 *** End of sqlit
8c4a0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
8c4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c4d0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
8c4e0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 67 *** Begin file g
8c4f0 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a lobal.c ********
8c500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c520 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a **/./*.** 2008 J
8c530 75 6e 65 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65 une 13.**.** The
8c540 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
8c550 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
8c560 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
8c570 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
8c580 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
8c590 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
8c5a0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
8c5b0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
8c5c0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
8c5d0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
8c5e0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
8c5f0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
8c600 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
8c610 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
8c620 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
8c630 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
8c640 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
8c650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c690 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
8c6a0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 64 file contains d
8c6b0 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 67 6c efinitions of gl
8c6c0 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 61 obal variables a
8c6d0 6e 64 20 63 6f 6e 74 61 6e 74 73 2e 0a 2a 2f 0a nd contants..*/.
8c6e0 0a 2f 2a 20 41 6e 20 61 72 72 61 79 20 74 6f 20 ./* An array to
8c6f0 6d 61 70 20 61 6c 6c 20 75 70 70 65 72 2d 63 61 map all upper-ca
8c700 73 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e se characters in
8c710 74 6f 20 74 68 65 69 72 20 63 6f 72 72 65 73 70 to their corresp
8c720 6f 6e 64 69 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d onding.** lower-
8c730 63 61 73 65 20 63 68 61 72 61 63 74 65 72 2e 20 case character.
8c740 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6f 6e .**.** SQLite on
8c750 6c 79 20 63 6f 6e 73 69 64 65 72 73 20 55 53 2d ly considers US-
8c760 41 53 43 49 49 20 28 6f 72 20 45 42 43 44 49 43 ASCII (or EBCDIC
8c770 29 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 57 ) characters. W
8c780 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 68 61 6e 64 e do not.** hand
8c790 6c 65 20 63 61 73 65 20 63 6f 6e 76 65 72 73 69 le case conversi
8c7a0 6f 6e 73 20 66 6f 72 20 74 68 65 20 55 54 46 20 ons for the UTF
8c7b0 63 68 61 72 61 63 74 65 72 20 73 65 74 20 73 69 character set si
8c7c0 6e 63 65 20 74 68 65 20 74 61 62 6c 65 73 0a 2a nce the tables.*
8c7d0 2a 20 69 6e 76 6f 6c 76 65 64 20 61 72 65 20 6e * involved are n
8c7e0 65 61 72 6c 79 20 61 73 20 62 69 67 20 6f 72 20 early as big or
8c7f0 62 69 67 67 65 72 20 74 68 61 6e 20 53 51 4c 69 bigger than SQLi
8c800 74 65 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 te itself..*/.SQ
8c810 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
8c820 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
8c830 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c sqlite3UpperToL
8c840 6f 77 65 72 5b 5d 20 3d 20 7b 0a 23 69 66 64 65 ower[] = {.#ifde
8c850 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 f SQLITE_ASCII.
8c860 20 20 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 0, 1, 2,
8c870 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 3, 4, 5, 6,
8c880 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 7, 8, 9, 10,
8c890 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 11, 12, 13, 14,
8c8a0 31 35 2c 20 31 36 2c 20 31 37 2c 0a 20 20 20 20 15, 16, 17,.
8c8b0 20 31 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 18, 19, 20, 21,
8c8c0 20 32 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 22, 23, 24, 25,
8c8d0 20 32 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 26, 27, 28, 29,
8c8e0 20 33 30 2c 20 33 31 2c 20 33 32 2c 20 33 33 2c 30, 31, 32, 33,
8c8f0 20 33 34 2c 20 33 35 2c 0a 20 20 20 20 20 33 36 34, 35,. 36
8c900 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 , 37, 38, 39, 40
8c910 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 , 41, 42, 43, 44
8c920 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 34 38 , 45, 46, 47, 48
8c930 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 , 49, 50, 51, 52
8c940 2c 20 35 33 2c 0a 20 20 20 20 20 35 34 2c 20 35 , 53,. 54, 5
8c950 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 5, 56, 57, 58, 5
8c960 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 9, 60, 61, 62, 6
8c970 33 2c 20 36 34 2c 20 39 37 2c 20 39 38 2c 20 39 3, 64, 97, 98, 9
8c980 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 9,100,101,102,10
8c990 33 2c 0a 20 20 20 20 31 30 34 2c 31 30 35 2c 31 3,. 104,105,1
8c9a0 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 06,107,108,109,1
8c9b0 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 10,111,112,113,1
8c9c0 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 14,115,116,117,1
8c9d0 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 0a 18,119,120,121,.
8c9e0 20 20 20 20 31 32 32 2c 20 39 31 2c 20 39 32 2c 122, 91, 92,
8c9f0 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 39 36 2c 93, 94, 95, 96,
8ca00 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 97, 98, 99,100,
8ca10 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 101,102,103,104,
8ca20 31 30 35 2c 31 30 36 2c 31 30 37 2c 0a 20 20 20 105,106,107,.
8ca30 20 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 108,109,110,111
8ca40 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 ,112,113,114,115
8ca50 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 ,116,117,118,119
8ca60 2c 31 32 30 2c 31 32 31 2c 31 32 32 2c 31 32 33 ,120,121,122,123
8ca70 2c 31 32 34 2c 31 32 35 2c 0a 20 20 20 20 31 32 ,124,125,. 12
8ca80 36 2c 31 32 37 2c 31 32 38 2c 31 32 39 2c 31 33 6,127,128,129,13
8ca90 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 0,131,132,133,13
8caa0 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 4,135,136,137,13
8cab0 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 8,139,140,141,14
8cac0 32 2c 31 34 33 2c 0a 20 20 20 20 31 34 34 2c 31 2,143,. 144,1
8cad0 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 45,146,147,148,1
8cae0 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 49,150,151,152,1
8caf0 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 53,154,155,156,1
8cb00 35 37 2c 31 35 38 2c 31 35 39 2c 31 36 30 2c 31 57,158,159,160,1
8cb10 36 31 2c 0a 20 20 20 20 31 36 32 2c 31 36 33 2c 61,. 162,163,
8cb20 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 164,165,166,167,
8cb30 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 168,169,170,171,
8cb40 31 37 32 2c 31 37 33 2c 31 37 34 2c 31 37 35 2c 172,173,174,175,
8cb50 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 176,177,178,179,
8cb60 0a 20 20 20 20 31 38 30 2c 31 38 31 2c 31 38 32 . 180,181,182
8cb70 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 ,183,184,185,186
8cb80 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 ,187,188,189,190
8cb90 2c 31 39 31 2c 31 39 32 2c 31 39 33 2c 31 39 34 ,191,192,193,194
8cba0 2c 31 39 35 2c 31 39 36 2c 31 39 37 2c 0a 20 20 ,195,196,197,.
8cbb0 20 20 31 39 38 2c 31 39 39 2c 32 30 30 2c 32 30 198,199,200,20
8cbc0 31 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 1,202,203,204,20
8cbd0 35 2c 32 30 36 2c 32 30 37 2c 32 30 38 2c 32 30 5,206,207,208,20
8cbe0 39 2c 32 31 30 2c 32 31 31 2c 32 31 32 2c 32 31 9,210,211,212,21
8cbf0 33 2c 32 31 34 2c 32 31 35 2c 0a 20 20 20 20 32 3,214,215,. 2
8cc00 31 36 2c 32 31 37 2c 32 31 38 2c 32 31 39 2c 32 16,217,218,219,2
8cc10 32 30 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 32 20,221,222,223,2
8cc20 32 34 2c 32 32 35 2c 32 32 36 2c 32 32 37 2c 32 24,225,226,227,2
8cc30 32 38 2c 32 32 39 2c 32 33 30 2c 32 33 31 2c 32 28,229,230,231,2
8cc40 33 32 2c 32 33 33 2c 0a 20 20 20 20 32 33 34 2c 32,233,. 234,
8cc50 32 33 35 2c 32 33 36 2c 32 33 37 2c 32 33 38 2c 235,236,237,238,
8cc60 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 239,240,241,242,
8cc70 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 243,244,245,246,
8cc80 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 35 30 2c 247,248,249,250,
8cc90 32 35 31 2c 0a 20 20 20 20 32 35 32 2c 32 35 33 251,. 252,253
8cca0 2c 32 35 34 2c 32 35 35 0a 23 65 6e 64 69 66 0a ,254,255.#endif.
8ccb0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 #ifdef SQLITE_EB
8ccc0 43 44 49 43 0a 20 20 20 20 20 20 30 2c 20 20 31 CDIC. 0, 1
8ccd0 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 , 2, 3, 4, 5
8cce0 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 , 6, 7, 8, 9
8ccf0 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 , 10, 11, 12, 13
8cd00 2c 20 31 34 2c 20 31 35 2c 20 2f 2a 20 30 78 20 , 14, 15, /* 0x
8cd10 2a 2f 0a 20 20 20 20 20 31 36 2c 20 31 37 2c 20 */. 16, 17,
8cd20 31 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 18, 19, 20, 21,
8cd30 32 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 22, 23, 24, 25,
8cd40 32 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 26, 27, 28, 29,
8cd50 33 30 2c 20 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 30, 31, /* 1x */
8cd60 0a 20 20 20 20 20 33 32 2c 20 33 33 2c 20 33 34 . 32, 33, 34
8cd70 2c 20 33 35 2c 20 33 36 2c 20 33 37 2c 20 33 38 , 35, 36, 37, 38
8cd80 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 , 39, 40, 41, 42
8cd90 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 , 43, 44, 45, 46
8cda0 2c 20 34 37 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20 , 47, /* 2x */.
8cdb0 20 20 20 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 48, 49, 50,
8cdc0 35 31 2c 20 35 32 2c 20 35 33 2c 20 35 34 2c 20 51, 52, 53, 54,
8cdd0 35 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 55, 56, 57, 58,
8cde0 35 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 59, 60, 61, 62,
8cdf0 36 33 2c 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 63, /* 3x */.
8ce00 20 20 36 34 2c 20 36 35 2c 20 36 36 2c 20 36 37 64, 65, 66, 67
8ce10 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 , 68, 69, 70, 71
8ce20 2c 20 37 32 2c 20 37 33 2c 20 37 34 2c 20 37 35 , 72, 73, 74, 75
8ce30 2c 20 37 36 2c 20 37 37 2c 20 37 38 2c 20 37 39 , 76, 77, 78, 79
8ce40 2c 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 20 , /* 4x */.
8ce50 38 30 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 80, 81, 82, 83,
8ce60 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 84, 85, 86, 87,
8ce70 38 38 2c 20 38 39 2c 20 39 30 2c 20 39 31 2c 20 88, 89, 90, 91,
8ce80 39 32 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 92, 93, 94, 95,
8ce90 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 20 39 36 /* 5x */. 96
8cea0 2c 20 39 37 2c 20 36 36 2c 20 36 37 2c 20 36 38 , 97, 66, 67, 68
8ceb0 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 , 69, 70, 71, 72
8cec0 2c 20 37 33 2c 31 30 36 2c 31 30 37 2c 31 30 38 , 73,106,107,108
8ced0 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 20 2f 2a ,109,110,111, /*
8cee0 20 36 78 20 2a 2f 0a 20 20 20 20 31 31 32 2c 20 6x */. 112,
8cef0 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 81, 82, 83, 84,
8cf00 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 85, 86, 87, 88,
8cf10 38 39 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 89,122,123,124,1
8cf20 32 35 2c 31 32 36 2c 31 32 37 2c 20 2f 2a 20 37 25,126,127, /* 7
8cf30 78 20 2a 2f 0a 20 20 20 20 31 32 38 2c 31 32 39 x */. 128,129
8cf40 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 ,130,131,132,133
8cf50 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 ,134,135,136,137
8cf60 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 ,138,139,140,141
8cf70 2c 31 34 32 2c 31 34 33 2c 20 2f 2a 20 38 78 20 ,142,143, /* 8x
8cf80 2a 2f 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31 */. 144,145,1
8cf90 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 46,147,148,149,1
8cfa0 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 50,151,152,153,1
8cfb0 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 54,155,156,157,1
8cfc0 35 36 2c 31 35 39 2c 20 2f 2a 20 39 78 20 2a 2f 56,159, /* 9x */
8cfd0 0a 20 20 20 20 31 36 30 2c 31 36 31 2c 31 36 32 . 160,161,162
8cfe0 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 ,163,164,165,166
8cff0 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 ,167,168,169,170
8d000 2c 31 37 31 2c 31 34 30 2c 31 34 31 2c 31 34 32 ,171,140,141,142
8d010 2c 31 37 35 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20 ,175, /* Ax */.
8d020 20 20 20 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 176,177,178,1
8d030 37 39 2c 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 79,180,181,182,1
8d040 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 83,184,185,186,1
8d050 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 87,188,189,190,1
8d060 39 31 2c 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 91, /* Bx */.
8d070 20 31 39 32 2c 31 32 39 2c 31 33 30 2c 31 33 31 192,129,130,131
8d080 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 ,132,133,134,135
8d090 2c 31 33 36 2c 31 33 37 2c 32 30 32 2c 32 30 33 ,136,137,202,203
8d0a0 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 ,204,205,206,207
8d0b0 2c 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 20 32 , /* Cx */. 2
8d0c0 30 38 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 08,145,146,147,1
8d0d0 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 48,149,150,151,1
8d0e0 35 32 2c 31 35 33 2c 32 31 38 2c 32 31 39 2c 32 52,153,218,219,2
8d0f0 32 30 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 20 20,221,222,223,
8d100 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20 32 32 34 /* Dx */. 224
8d110 2c 32 32 35 2c 31 36 32 2c 31 36 33 2c 31 36 34 ,225,162,163,164
8d120 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 ,165,166,167,168
8d130 2c 31 36 39 2c 32 33 32 2c 32 30 33 2c 32 30 34 ,169,232,203,204
8d140 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a ,205,206,207, /*
8d150 20 45 78 20 2a 2f 0a 20 20 20 20 32 33 39 2c 32 Ex */. 239,2
8d160 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 40,241,242,243,2
8d170 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 44,245,246,247,2
8d180 34 38 2c 32 34 39 2c 32 31 39 2c 32 32 30 2c 32 48,249,219,220,2
8d190 32 31 2c 32 32 32 2c 32 35 35 2c 20 2f 2a 20 46 21,222,255, /* F
8d1a0 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a x */.#endif.};..
8d1b0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
8d1c0 69 6e 67 20 32 35 36 20 62 79 74 65 20 6c 6f 6f ing 256 byte loo
8d1d0 6b 75 70 20 74 61 62 6c 65 20 69 73 20 75 73 65 kup table is use
8d1e0 64 20 74 6f 20 73 75 70 70 6f 72 74 20 53 51 4c d to support SQL
8d1f0 69 74 65 73 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a ites built-in.**
8d200 20 65 71 75 69 76 61 6c 65 6e 74 73 20 74 6f 20 equivalents to
8d210 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
8d220 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79 20 66 andard library f
8d230 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 unctions:.**.**
8d240 20 20 69 73 73 70 61 63 65 28 29 20 20 20 20 20 isspace()
8d250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8d260 20 20 20 30 78 30 31 0a 2a 2a 20 20 20 69 73 61 0x01.** isa
8d270 6c 70 68 61 28 29 20 20 20 20 20 20 20 20 20 20 lpha()
8d280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 0x
8d290 30 32 0a 2a 2a 20 20 20 69 73 64 69 67 69 74 28 02.** isdigit(
8d2a0 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 )
8d2b0 20 20 20 20 20 20 20 20 20 30 78 30 34 0a 2a 2a 0x04.**
8d2c0 20 20 20 69 73 61 6c 6e 75 6d 28 29 20 20 20 20 isalnum()
8d2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8d2e0 20 20 20 20 30 78 30 36 0a 2a 2a 20 20 20 69 73 0x06.** is
8d2f0 78 64 69 67 69 74 28 29 20 20 20 20 20 20 20 20 xdigit()
8d300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
8d310 78 30 38 0a 2a 2a 20 20 20 74 6f 75 70 70 65 72 x08.** toupper
8d320 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ()
8d330 20 20 20 20 20 20 20 20 20 20 30 78 32 30 0a 2a 0x20.*
8d340 2a 20 20 20 53 51 4c 69 74 65 20 69 64 65 6e 74 * SQLite ident
8d350 69 66 69 65 72 20 63 68 61 72 61 63 74 65 72 20 ifier character
8d360 20 20 20 20 20 30 78 34 30 0a 2a 2a 0a 2a 2a 20 0x40.**.**
8d370 42 69 74 20 30 78 32 30 20 69 73 20 73 65 74 20 Bit 0x20 is set
8d380 69 66 20 74 68 65 20 6d 61 70 70 65 64 20 63 68 if the mapped ch
8d390 61 72 61 63 74 65 72 20 72 65 71 75 69 72 65 73 aracter requires
8d3a0 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 6f 20 translation to
8d3b0 75 70 70 65 72 0a 2a 2a 20 63 61 73 65 2e 20 69 upper.** case. i
8d3c0 2e 65 2e 20 69 66 20 74 68 65 20 63 68 61 72 61 .e. if the chara
8d3d0 63 74 65 72 20 69 73 20 61 20 6c 6f 77 65 72 2d cter is a lower-
8d3e0 63 61 73 65 20 41 53 43 49 49 20 63 68 61 72 61 case ASCII chara
8d3f0 63 74 65 72 2e 0a 2a 2a 20 49 66 20 78 20 69 73 cter..** If x is
8d400 20 61 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 53 a lower-case AS
8d410 43 49 49 20 63 68 61 72 61 63 74 65 72 2c 20 74 CII character, t
8d420 68 65 6e 20 69 74 73 20 75 70 70 65 72 2d 63 61 hen its upper-ca
8d430 73 65 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a se equivalent.**
8d440 20 69 73 20 28 78 20 2d 20 30 78 32 30 29 2e 20 is (x - 0x20).
8d450 54 68 65 72 65 66 6f 72 65 20 74 6f 75 70 70 65 Therefore touppe
8d460 72 28 29 20 63 61 6e 20 62 65 20 69 6d 70 6c 65 r() can be imple
8d470 6d 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a 2a 2a mented as:.**.**
8d480 20 20 20 28 78 20 26 20 7e 28 6d 61 70 5b 78 5d (x & ~(map[x]
8d490 26 30 78 32 30 29 29 0a 2a 2a 0a 2a 2a 20 53 74 &0x20)).**.** St
8d4a0 61 6e 64 61 72 64 20 66 75 6e 63 74 69 6f 6e 20 andard function
8d4b0 74 6f 6c 6f 77 65 72 28 29 20 69 73 20 69 6d 70 tolower() is imp
8d4c0 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 74 lemented using t
8d4d0 68 65 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 he sqlite3UpperT
8d4e0 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 61 72 72 61 oLower[].** arra
8d4f0 79 2e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 20 y. tolower() is
8d500 75 73 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e 20 used more often
8d510 74 68 61 6e 20 74 6f 75 70 70 65 72 28 29 20 62 than toupper() b
8d520 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 y SQLite..**.**
8d530 42 69 74 20 30 78 34 30 20 69 73 20 73 65 74 20 Bit 0x40 is set
8d540 69 66 20 74 68 65 20 63 68 61 72 61 63 74 65 72 if the character
8d550 20 6e 6f 6e 2d 61 6c 70 68 61 6e 75 6d 65 72 69 non-alphanumeri
8d560 63 20 61 6e 64 20 63 61 6e 20 62 65 20 75 73 65 c and can be use
8d570 64 20 69 6e 20 61 6e 20 0a 2a 2a 20 53 51 4c 69 d in an .** SQLi
8d580 74 65 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 te identifier.
8d590 49 64 65 6e 74 69 66 69 65 72 73 20 61 72 65 20 Identifiers are
8d5a0 61 6c 70 68 61 6e 75 6d 65 72 69 63 73 2c 20 22 alphanumerics, "
8d5b0 5f 22 2c 20 22 24 22 2c 20 61 6e 64 20 61 6e 79 _", "$", and any
8d5c0 0a 2a 2a 20 6e 6f 6e 2d 41 53 43 49 49 20 55 54 .** non-ASCII UT
8d5d0 46 20 63 68 61 72 61 63 74 65 72 2e 20 48 65 6e F character. Hen
8d5e0 63 65 20 74 68 65 20 74 65 73 74 20 66 6f 72 20 ce the test for
8d5f0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 whether or not a
8d600 20 63 68 61 72 61 63 74 65 72 20 69 73 0a 2a 2a character is.**
8d610 20 70 61 72 74 20 6f 66 20 61 6e 20 69 64 65 6e part of an iden
8d620 74 69 66 69 65 72 20 69 73 20 30 78 34 36 2e 0a tifier is 0x46..
8d630 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 27 73 20 76 **.** SQLite's v
8d640 65 72 73 69 6f 6e 73 20 61 72 65 20 69 64 65 6e ersions are iden
8d650 74 69 63 61 6c 20 74 6f 20 74 68 65 20 73 74 61 tical to the sta
8d660 6e 64 61 72 64 20 76 65 72 73 69 6f 6e 73 20 61 ndard versions a
8d670 73 73 75 6d 69 6e 67 20 61 0a 2a 2a 20 6c 6f 63 ssuming a.** loc
8d680 61 6c 65 20 6f 66 20 22 43 22 2e 20 54 68 65 79 ale of "C". They
8d690 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 are implemented
8d6a0 20 61 73 20 6d 61 63 72 6f 73 20 69 6e 20 73 71 as macros in sq
8d6b0 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 liteInt.h..*/.#i
8d6c0 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 fdef SQLITE_ASCI
8d6d0 49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 I.SQLITE_PRIVATE
8d6e0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
8d6f0 63 68 61 72 20 73 71 6c 69 74 65 33 43 74 79 70 char sqlite3Ctyp
8d700 65 4d 61 70 5b 32 35 36 5d 20 3d 20 7b 0a 20 20 eMap[256] = {.
8d710 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
8d720 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
8d730 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
8d740 20 2f 2a 20 30 30 2e 2e 30 37 20 20 20 20 2e 2e /* 00..07 ..
8d750 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 ...... */. 0x00
8d760 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 , 0x01, 0x01, 0x
8d770 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 01, 0x01, 0x01,
8d780 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
8d790 30 38 2e 2e 30 66 20 20 20 20 2e 2e 2e 2e 2e 2e 08..0f ......
8d7a0 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
8d7b0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
8d7c0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
8d7d0 2c 20 30 78 30 30 2c 20 20 2f 2a 20 31 30 2e 2e , 0x00, /* 10..
8d7e0 31 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 17 ........ *
8d7f0 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 /. 0x00, 0x00,
8d800 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
8d810 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
8d820 30 30 2c 20 20 2f 2a 20 31 38 2e 2e 31 66 20 20 00, /* 18..1f
8d830 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
8d840 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 0x01, 0x00, 0x00
8d850 2c 20 30 78 30 30 2c 20 30 78 34 30 2c 20 30 78 , 0x00, 0x40, 0x
8d860 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
8d870 20 2f 2a 20 32 30 2e 2e 32 37 20 20 20 20 20 21 /* 20..27 !
8d880 22 23 24 25 26 27 20 2a 2f 0a 20 20 30 78 30 30 "#$%&' */. 0x00
8d890 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
8d8a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
8d8b0 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
8d8c0 32 38 2e 2e 32 66 20 20 20 20 28 29 2a 2b 2c 2d 28..2f ()*+,-
8d8d0 2e 2f 20 2a 2f 0a 20 20 30 78 30 63 2c 20 30 78 ./ */. 0x0c, 0x
8d8e0 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 0c, 0x0c, 0x0c,
8d8f0 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 0x0c, 0x0c, 0x0c
8d900 2c 20 30 78 30 63 2c 20 20 2f 2a 20 33 30 2e 2e , 0x0c, /* 30..
8d910 33 37 20 20 20 20 30 31 32 33 34 35 36 37 20 2a 37 01234567 *
8d920 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20 /. 0x0c, 0x0c,
8d930 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
8d940 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
8d950 30 30 2c 20 20 2f 2a 20 33 38 2e 2e 33 66 20 20 00, /* 38..3f
8d960 20 20 38 39 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 89:;<=>? */..
8d970 20 30 78 30 30 2c 20 30 78 30 61 2c 20 30 78 30 0x00, 0x0a, 0x0
8d980 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 a, 0x0a, 0x0a, 0
8d990 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 32 2c x0a, 0x0a, 0x02,
8d9a0 20 20 2f 2a 20 34 30 2e 2e 34 37 20 20 20 20 40 /* 40..47 @
8d9b0 41 42 43 44 45 46 47 20 2a 2f 0a 20 20 30 78 30 ABCDEFG */. 0x0
8d9c0 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 2, 0x02, 0x02, 0
8d9d0 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c x02, 0x02, 0x02,
8d9e0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 20 2f 2a 0x02, 0x02, /*
8d9f0 20 34 38 2e 2e 34 66 20 20 20 20 48 49 4a 4b 4c 48..4f HIJKL
8da00 4d 4e 4f 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 MNO */. 0x02, 0
8da10 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c x02, 0x02, 0x02,
8da20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
8da30 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 35 30 2e 2, 0x02, /* 50.
8da40 2e 35 37 20 20 20 20 50 51 52 53 54 55 56 57 20 .57 PQRSTUVW
8da50 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c */. 0x02, 0x02,
8da60 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 0x02, 0x00, 0x0
8da70 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
8da80 78 34 30 2c 20 20 2f 2a 20 35 38 2e 2e 35 66 20 x40, /* 58..5f
8da90 20 20 20 58 59 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 XYZ[\]^_ */.
8daa0 20 30 78 30 30 2c 20 30 78 32 61 2c 20 30 78 32 0x00, 0x2a, 0x2
8dab0 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 a, 0x2a, 0x2a, 0
8dac0 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 32 2c x2a, 0x2a, 0x22,
8dad0 20 20 2f 2a 20 36 30 2e 2e 36 37 20 20 20 20 60 /* 60..67 `
8dae0 61 62 63 64 65 66 67 20 2a 2f 0a 20 20 30 78 32 abcdefg */. 0x2
8daf0 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 2, 0x22, 0x22, 0
8db00 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c x22, 0x22, 0x22,
8db10 20 30 78 32 32 2c 20 30 78 32 32 2c 20 20 2f 2a 0x22, 0x22, /*
8db20 20 36 38 2e 2e 36 66 20 20 20 20 68 69 6a 6b 6c 68..6f hijkl
8db30 6d 6e 6f 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 mno */. 0x22, 0
8db40 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c x22, 0x22, 0x22,
8db50 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 0x22, 0x22, 0x2
8db60 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 37 30 2e 2, 0x22, /* 70.
8db70 2e 37 37 20 20 20 20 70 71 72 73 74 75 76 77 20 .77 pqrstuvw
8db80 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c */. 0x22, 0x22,
8db90 20 30 78 32 32 2c 20 30 78 30 30 2c 20 30 78 30 0x22, 0x00, 0x0
8dba0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
8dbb0 78 30 30 2c 20 20 2f 2a 20 37 38 2e 2e 37 66 20 x00, /* 78..7f
8dbc0 20 20 20 78 79 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a xyz{|}~. */..
8dbd0 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 0x40, 0x40, 0x
8dbe0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
8dbf0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
8dc00 2c 20 20 2f 2a 20 38 30 2e 2e 38 37 20 20 20 20 , /* 80..87
8dc10 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 ........ */. 0x
8dc20 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
8dc30 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
8dc40 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f , 0x40, 0x40, /
8dc50 2a 20 38 38 2e 2e 38 66 20 20 20 20 2e 2e 2e 2e * 88..8f ....
8dc60 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 .... */. 0x40,
8dc70 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
8dc80 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
8dc90 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 39 30 40, 0x40, /* 90
8dca0 2e 2e 39 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..97 ........
8dcb0 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 */. 0x40, 0x40
8dcc0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
8dcd0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
8dce0 30 78 34 30 2c 20 20 2f 2a 20 39 38 2e 2e 39 66 0x40, /* 98..9f
8dcf0 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
8dd00 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 0x40, 0x40, 0x
8dd10 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
8dd20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
8dd30 2c 20 20 2f 2a 20 61 30 2e 2e 61 37 20 20 20 20 , /* a0..a7
8dd40 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 ........ */. 0x
8dd50 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
8dd60 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
8dd70 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f , 0x40, 0x40, /
8dd80 2a 20 61 38 2e 2e 61 66 20 20 20 20 2e 2e 2e 2e * a8..af ....
8dd90 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 .... */. 0x40,
8dda0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
8ddb0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
8ddc0 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 62 30 40, 0x40, /* b0
8ddd0 2e 2e 62 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..b7 ........
8dde0 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 */. 0x40, 0x40
8ddf0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
8de00 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
8de10 30 78 34 30 2c 20 20 2f 2a 20 62 38 2e 2e 62 66 0x40, /* b8..bf
8de20 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
8de30 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 . 0x40, 0x40, 0
8de40 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
8de50 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
8de60 30 2c 20 20 2f 2a 20 63 30 2e 2e 63 37 20 20 20 0, /* c0..c7
8de70 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
8de80 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
8de90 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
8dea0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 0, 0x40, 0x40,
8deb0 2f 2a 20 63 38 2e 2e 63 66 20 20 20 20 2e 2e 2e /* c8..cf ...
8dec0 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c ..... */. 0x40,
8ded0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
8dee0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
8def0 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 64 x40, 0x40, /* d
8df00 30 2e 2e 64 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 0..d7 .......
8df10 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 . */. 0x40, 0x4
8df20 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
8df30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
8df40 20 30 78 34 30 2c 20 20 2f 2a 20 64 38 2e 2e 64 0x40, /* d8..d
8df50 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f f ........ */
8df60 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 . 0x40, 0x40, 0
8df70 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
8df80 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
8df90 30 2c 20 20 2f 2a 20 65 30 2e 2e 65 37 20 20 20 0, /* e0..e7
8dfa0 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
8dfb0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
8dfc0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
8dfd0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 0, 0x40, 0x40,
8dfe0 2f 2a 20 65 38 2e 2e 65 66 20 20 20 20 2e 2e 2e /* e8..ef ...
8dff0 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c ..... */. 0x40,
8e000 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
8e010 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
8e020 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 66 x40, 0x40, /* f
8e030 30 2e 2e 66 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 0..f7 .......
8e040 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 . */. 0x40, 0x4
8e050 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
8e060 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
8e070 20 30 78 34 30 20 20 20 2f 2a 20 66 38 2e 2e 66 0x40 /* f8..f
8e080 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f f ........ */
8e090 0a 7d 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e .};.#endif..#ifn
8e0a0 64 65 66 20 53 51 4c 49 54 45 5f 55 53 45 5f 55 def SQLITE_USE_U
8e0b0 52 49 0a 23 20 64 65 66 69 6e 65 20 20 53 51 4c RI.# define SQL
8e0c0 49 54 45 5f 55 53 45 5f 55 52 49 20 30 0a 23 65 ITE_USE_URI 0.#e
8e0d0 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
8e0e0 4c 49 54 45 5f 41 4c 4c 4f 57 5f 43 4f 56 45 52 LITE_ALLOW_COVER
8e0f0 49 4e 47 5f 49 4e 44 45 58 5f 53 43 41 4e 0a 23 ING_INDEX_SCAN.#
8e100 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 define SQLITE_A
8e110 4c 4c 4f 57 5f 43 4f 56 45 52 49 4e 47 5f 49 4e LLOW_COVERING_IN
8e120 44 45 58 5f 53 43 41 4e 20 31 0a 23 65 6e 64 69 DEX_SCAN 1.#endi
8e130 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c f../*.** The fol
8e140 6c 6f 77 69 6e 67 20 73 69 6e 67 6c 65 74 6f 6e lowing singleton
8e150 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 67 6c contains the gl
8e160 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 obal configurati
8e170 6f 6e 20 66 6f 72 0a 2a 2a 20 74 68 65 20 53 51 on for.** the SQ
8e180 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f Lite library..*/
8e190 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
8e1a0 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 SQLITE_WSD struc
8e1b0 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 t Sqlite3Config
8e1c0 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 3d 20 sqlite3Config =
8e1d0 7b 0a 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41 {. SQLITE_DEFA
8e1e0 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 2c 20 20 ULT_MEMSTATUS,
8e1f0 2f 2a 20 62 4d 65 6d 73 74 61 74 20 2a 2f 0a 20 /* bMemstat */.
8e200 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 1,
8e210 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
8e220 62 43 6f 72 65 4d 75 74 65 78 20 2a 2f 0a 20 20 bCoreMutex */.
8e230 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
8e240 46 45 3d 3d 31 2c 20 20 20 20 20 20 2f 2a 20 62 FE==1, /* b
8e250 46 75 6c 6c 4d 75 74 65 78 20 2a 2f 0a 20 20 20 FullMutex */.
8e260 53 51 4c 49 54 45 5f 55 53 45 5f 55 52 49 2c 20 SQLITE_USE_URI,
8e270 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 62 4f /* bO
8e280 70 65 6e 55 72 69 20 2a 2f 0a 20 20 20 53 51 4c penUri */. SQL
8e290 49 54 45 5f 41 4c 4c 4f 57 5f 43 4f 56 45 52 49 ITE_ALLOW_COVERI
8e2a0 4e 47 5f 49 4e 44 45 58 5f 53 43 41 4e 2c 20 20 NG_INDEX_SCAN,
8e2b0 20 2f 2a 20 62 55 73 65 43 69 73 20 2a 2f 0a 20 /* bUseCis */.
8e2c0 20 20 30 78 37 66 66 66 66 66 66 65 2c 20 20 20 0x7ffffffe,
8e2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
8e2e0 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a 20 20 20 31 mxStrlen */. 1
8e2f0 32 38 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 28,
8e300 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 4c /* szL
8e310 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 35 ookaside */. 5
8e320 30 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 00,
8e330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 4c 6f /* nLo
8e340 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 7b 30 okaside */. {0
8e350 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c ,0,0,0,0,0,0,0},
8e360 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 20 2a 2f /* m */
8e370 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 . {0,0,0,0,0,0
8e380 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20 20 20 2f ,0,0,0}, /
8e390 2a 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 7b 30 * mutex */. {0
8e3a0 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 ,0,0,0,0,0,0,0,0
8e3b0 2c 30 2c 30 2c 30 2c 30 7d 2c 2f 2a 20 70 63 61 ,0,0,0,0},/* pca
8e3c0 63 68 65 32 20 2a 2f 0a 20 20 20 28 76 6f 69 64 che2 */. (void
8e3d0 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 *)0,
8e3e0 20 20 20 20 20 20 2f 2a 20 70 48 65 61 70 20 2a /* pHeap *
8e3f0 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 /. 0,
8e400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e410 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20 20 20 30 /* nHeap */. 0
8e420 2c 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 , 0,
8e430 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 6e 48 /* mnH
8e440 65 61 70 2c 20 6d 78 48 65 61 70 20 2a 2f 0a 20 eap, mxHeap */.
8e450 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 20 (void*)0,
8e460 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
8e470 70 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30 pScratch */. 0
8e480 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
8e490 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 53 /* szS
8e4a0 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30 2c 20 cratch */. 0,
8e4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e4c0 20 20 20 20 20 20 20 20 2f 2a 20 6e 53 63 72 61 /* nScra
8e4d0 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a tch */. (void*
8e4e0 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 )0,
8e4f0 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 2a 2f /* pPage */
8e500 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 . 0,
8e510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
8e520 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20 20 20 30 * szPage */. 0
8e530 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
8e540 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 50 61 /* nPa
8e550 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 ge */. 0,
8e560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e570 20 20 20 20 2f 2a 20 6d 78 50 61 72 73 65 72 53 /* mxParserS
8e580 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c 20 20 20 tack */. 0,
8e590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e5a0 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65 64 43 /* sharedC
8e5b0 61 63 68 65 45 6e 61 62 6c 65 64 20 2a 2f 0a 20 acheEnabled */.
8e5c0 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 73 /* All the res
8e5d0 74 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 t should always
8e5e0 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 be initialized t
8e5f0 6f 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 30 2c 20 o zero */. 0,
8e600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e610 20 20 20 20 20 20 20 20 2f 2a 20 69 73 49 6e 69 /* isIni
8e620 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 t */. 0,
8e630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e640 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72 65 73 73 /* inProgress
8e650 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
8e660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e670 20 20 2f 2a 20 69 73 4d 75 74 65 78 49 6e 69 74 /* isMutexInit
8e680 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
8e690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e6a0 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 /* isMallocIni
8e6b0 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 t */. 0,
8e6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e6d0 20 20 20 2f 2a 20 69 73 50 43 61 63 68 65 49 6e /* isPCacheIn
8e6e0 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 it */. 0,
8e6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e700 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d 75 74 65 /* pInitMute
8e710 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 x */. 0,
8e720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e730 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 74 4d 75 /* nRefInitMu
8e740 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 tex */. 0,
8e750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e760 20 20 20 20 20 2f 2a 20 78 4c 6f 67 20 2a 2f 0a /* xLog */.
8e770 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
8e780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
8e790 20 70 4c 6f 67 41 72 67 20 2a 2f 0a 20 20 20 30 pLogArg */. 0
8e7a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
8e7b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 62 4c 6f /* bLo
8e7c0 63 61 6c 74 69 6d 65 46 61 75 6c 74 20 2a 2f 0a caltimeFault */.
8e7d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
8e7e0 41 42 4c 45 5f 53 51 4c 4c 4f 47 0a 20 20 20 30 ABLE_SQLLOG. 0
8e7f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
8e800 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 71 /* xSq
8e810 6c 6c 6f 67 20 2a 2f 0a 20 20 20 30 20 20 20 20 llog */. 0
8e820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8e830 20 20 20 20 20 20 2f 2a 20 70 53 71 6c 6c 6f 67 /* pSqllog
8e840 41 72 67 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b Arg */.#endif.};
8e850 0a 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 74 61 .../*.** Hash ta
8e860 62 6c 65 20 66 6f 72 20 67 6c 6f 62 61 6c 20 66 ble for global f
8e870 75 6e 63 74 69 6f 6e 73 20 2d 20 66 75 6e 63 74 unctions - funct
8e880 69 6f 6e 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 ions common to a
8e890 6c 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 ll.** database c
8e8a0 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 41 66 74 onnections. Aft
8e8b0 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f er initializatio
8e8c0 6e 2c 20 74 68 69 73 20 74 61 62 6c 65 20 69 73 n, this table is
8e8d0 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 2a .** read-only..*
8e8e0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
8e8f0 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 SQLITE_WSD Func
8e900 44 65 66 48 61 73 68 20 73 71 6c 69 74 65 33 47 DefHash sqlite3G
8e910 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a lobalFunctions;.
8e920 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 20 ./*.** Constant
8e930 74 6f 6b 65 6e 73 20 66 6f 72 20 76 61 6c 75 65 tokens for value
8e940 73 20 30 20 61 6e 64 20 31 2e 0a 2a 2f 0a 53 51 s 0 and 1..*/.SQ
8e950 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
8e960 73 74 20 54 6f 6b 65 6e 20 73 71 6c 69 74 65 33 st Token sqlite3
8e970 49 6e 74 54 6f 6b 65 6e 73 5b 5d 20 3d 20 7b 0a IntTokens[] = {.
8e980 20 20 20 7b 20 22 30 22 2c 20 31 20 7d 2c 0a 20 { "0", 1 },.
8e990 20 20 7b 20 22 31 22 2c 20 31 20 7d 0a 7d 3b 0a { "1", 1 }.};.
8e9a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 ../*.** The valu
8e9b0 65 20 6f 66 20 74 68 65 20 22 70 65 6e 64 69 6e e of the "pendin
8e9c0 67 22 20 62 79 74 65 20 6d 75 73 74 20 62 65 20 g" byte must be
8e9d0 30 78 34 30 30 30 30 30 30 30 20 28 31 20 62 79 0x40000000 (1 by
8e9e0 74 65 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 te past the.** 1
8e9f0 2d 67 69 62 61 62 79 74 65 20 62 6f 75 6e 64 61 -gibabyte bounda
8ea00 72 79 29 20 69 6e 20 61 20 63 6f 6d 70 61 74 69 ry) in a compati
8ea10 62 6c 65 20 64 61 74 61 62 61 73 65 2e 20 20 53 ble database. S
8ea20 51 4c 69 74 65 20 6e 65 76 65 72 20 75 73 65 73 QLite never uses
8ea30 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
8ea40 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 page that conta
8ea50 69 6e 73 20 74 68 65 20 70 65 6e 64 69 6e 67 20 ins the pending
8ea60 62 79 74 65 2e 20 20 49 74 20 6e 65 76 65 72 20 byte. It never
8ea70 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 72 attempts.** to r
8ea80 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 68 61 ead or write tha
8ea90 74 20 70 61 67 65 2e 20 20 54 68 65 20 70 65 6e t page. The pen
8eaa0 64 69 6e 67 20 62 79 74 65 20 70 61 67 65 20 69 ding byte page i
8eab0 73 20 73 65 74 20 61 73 73 69 67 6e 0a 2a 2a 20 s set assign.**
8eac0 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 56 for use by the V
8ead0 46 53 20 6c 61 79 65 72 73 20 61 73 20 73 70 61 FS layers as spa
8eae0 63 65 20 66 6f 72 20 6d 61 6e 61 67 69 6e 67 20 ce for managing
8eaf0 66 69 6c 65 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a file locks..**.*
8eb00 2a 20 44 75 72 69 6e 67 20 74 65 73 74 69 6e 67 * During testing
8eb10 2c 20 69 74 20 69 73 20 6f 66 74 65 6e 20 64 65 , it is often de
8eb20 73 69 72 61 62 6c 65 20 74 6f 20 6d 6f 76 65 20 sirable to move
8eb30 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 the pending byte
8eb40 20 74 6f 0a 2a 2a 20 61 20 64 69 66 66 65 72 65 to.** a differe
8eb50 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 nt position in t
8eb60 68 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 61 he file. This a
8eb70 6c 6c 6f 77 73 20 63 6f 64 65 20 74 68 61 74 20 llows code that
8eb80 68 61 73 20 74 6f 0a 2a 2a 20 64 65 61 6c 20 77 has to.** deal w
8eb90 69 74 68 20 74 68 65 20 70 65 6e 64 69 6e 67 20 ith the pending
8eba0 62 79 74 65 20 74 6f 20 72 75 6e 20 6f 6e 20 66 byte to run on f
8ebb0 69 6c 65 73 20 74 68 61 74 20 61 72 65 20 6d 75 iles that are mu
8ebc0 63 68 20 73 6d 61 6c 6c 65 72 0a 2a 2a 20 74 68 ch smaller.** th
8ebd0 61 6e 20 31 20 47 69 42 2e 20 20 54 68 65 20 73 an 1 GiB. The s
8ebe0 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 qlite3_test_cont
8ebf0 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 rol() interface
8ec00 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 0a 2a can be used to.*
8ec10 2a 20 6d 6f 76 65 20 74 68 65 20 70 65 6e 64 69 * move the pendi
8ec20 6e 67 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 ng byte..**.** I
8ec30 4d 50 4f 52 54 41 4e 54 3a 20 20 43 68 61 6e 67 MPORTANT: Chang
8ec40 69 6e 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20 ing the pending
8ec50 62 79 74 65 20 74 6f 20 61 6e 79 20 76 61 6c 75 byte to any valu
8ec60 65 20 6f 74 68 65 72 20 74 68 61 6e 0a 2a 2a 20 e other than.**
8ec70 30 78 34 30 30 30 30 30 30 30 20 72 65 73 75 6c 0x40000000 resul
8ec80 74 73 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 ts in an incompa
8ec90 74 69 62 6c 65 20 64 61 74 61 62 61 73 65 20 66 tible database f
8eca0 69 6c 65 20 66 6f 72 6d 61 74 21 0a 2a 2a 20 43 ile format!.** C
8ecb0 68 61 6e 67 69 6e 67 20 74 68 65 20 70 65 6e 64 hanging the pend
8ecc0 69 6e 67 20 62 79 74 65 20 64 75 72 69 6e 67 20 ing byte during
8ecd0 6f 70 65 72 61 74 69 6e 67 20 72 65 73 75 6c 74 operating result
8ece0 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a s in undefined.*
8ecf0 2a 20 61 6e 64 20 64 69 6c 65 74 65 72 69 6f 75 * and dileteriou
8ed00 73 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 23 s behavior..*/.#
8ed10 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
8ed20 49 54 5f 57 53 44 0a 53 51 4c 49 54 45 5f 50 52 IT_WSD.SQLITE_PR
8ed30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
8ed40 33 50 65 6e 64 69 6e 67 42 79 74 65 20 3d 20 30 3PendingByte = 0
8ed50 78 34 30 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 x40000000;.#endi
8ed60 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 70 65 72 74 f../*.** Propert
8ed70 69 65 73 20 6f 66 20 6f 70 63 6f 64 65 73 2e 20 ies of opcodes.
8ed80 20 54 68 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 The OPFLG_INITI
8ed90 41 4c 49 5a 45 52 20 6d 61 63 72 6f 20 69 73 0a ALIZER macro is.
8eda0 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 6d 6b ** created by mk
8edb0 6f 70 63 6f 64 65 68 2e 61 77 6b 20 64 75 72 69 opcodeh.awk duri
8edc0 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 20 ng compilation.
8edd0 20 44 61 74 61 20 69 73 20 6f 62 74 61 69 6e 65 Data is obtaine
8ede0 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 63 6f d.** from the co
8edf0 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e 67 mments following
8ee00 20 74 68 65 20 22 63 61 73 65 20 4f 50 5f 78 78 the "case OP_xx
8ee10 78 78 3a 22 20 73 74 61 74 65 6d 65 6e 74 73 20 xx:" statements
8ee20 69 6e 0a 2a 2a 20 74 68 65 20 76 64 62 65 2e 63 in.** the vdbe.c
8ee30 20 66 69 6c 65 2e 20 20 0a 2a 2f 0a 53 51 4c 49 file. .*/.SQLI
8ee40 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
8ee50 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 unsigned char s
8ee60 71 6c 69 74 65 33 4f 70 63 6f 64 65 50 72 6f 70 qlite3OpcodeProp
8ee70 65 72 74 79 5b 5d 20 3d 20 4f 50 46 4c 47 5f 49 erty[] = OPFLG_I
8ee80 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a 2f 2a 2a NITIALIZER;../**
8ee90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
8eea0 20 6f 66 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a of global.c ***
8eeb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8eec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8eed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
8eee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
8eef0 69 6e 20 66 69 6c 65 20 63 74 69 6d 65 2e 63 20 in file ctime.c
8ef00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ef10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ef20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
8ef30 2a 2a 20 32 30 31 30 20 46 65 62 72 75 61 72 79 ** 2010 February
8ef40 20 32 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 23.**.** The au
8ef50 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
8ef60 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
8ef70 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
8ef80 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
8ef90 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
8efa0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
8efb0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
8efc0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
8efd0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
8efe0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
8eff0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
8f000 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
8f010 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
8f020 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
8f030 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
8f040 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
8f050 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
8f060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8f070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8f080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8f090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8f0a0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
8f0b0 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 72 6f le implements ro
8f0c0 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 72 utines used to r
8f0d0 65 70 6f 72 74 20 77 68 61 74 20 63 6f 6d 70 69 eport what compi
8f0e0 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 73 0a le-time options.
8f0f0 2a 2a 20 53 51 4c 69 74 65 20 77 61 73 20 62 75 ** SQLite was bu
8f100 69 6c 74 20 77 69 74 68 2e 0a 2a 2f 0a 0a 23 69 ilt with..*/..#i
8f110 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
8f120 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f T_COMPILEOPTION_
8f130 44 49 41 47 53 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e DIAGS.../*.** An
8f140 20 61 72 72 61 79 20 6f 66 20 6e 61 6d 65 73 20 array of names
8f150 6f 66 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 2d 74 of all compile-t
8f160 69 6d 65 20 6f 70 74 69 6f 6e 73 2e 20 20 54 68 ime options. Th
8f170 69 73 20 61 72 72 61 79 20 73 68 6f 75 6c 64 20 is array should
8f180 0a 2a 2a 20 62 65 20 73 6f 72 74 65 64 20 41 2d .** be sorted A-
8f190 5a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 72 Z..**.** This ar
8f1a0 72 61 79 20 6c 6f 6f 6b 73 20 6c 61 72 67 65 2c ray looks large,
8f1b0 20 62 75 74 20 69 6e 20 61 20 74 79 70 69 63 61 but in a typica
8f1c0 6c 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e 20 61 l installation a
8f1d0 63 74 75 61 6c 6c 79 20 75 73 65 73 0a 2a 2a 20 ctually uses.**
8f1e0 6f 6e 6c 79 20 61 20 68 61 6e 64 66 75 6c 20 6f only a handful o
8f1f0 66 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f f compile-time o
8f200 70 74 69 6f 6e 73 2c 20 73 6f 20 6d 6f 73 74 20 ptions, so most
8f210 74 69 6d 65 73 20 74 68 69 73 20 61 72 72 61 79 times this array
8f220 20 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 is usually.** r
8f230 61 74 68 65 72 20 73 68 6f 72 74 20 61 6e 64 20 ather short and
8f240 75 73 65 73 20 6c 69 74 74 6c 65 20 6d 65 6d 6f uses little memo
8f250 72 79 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 ry space..*/.sta
8f260 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
8f270 20 63 6f 6e 73 74 20 61 7a 43 6f 6d 70 69 6c 65 const azCompile
8f280 4f 70 74 5b 5d 20 3d 20 7b 0a 0a 2f 2a 20 54 68 Opt[] = {../* Th
8f290 65 73 65 20 6d 61 63 72 6f 73 20 61 72 65 20 70 ese macros are p
8f2a0 72 6f 76 69 64 65 64 20 74 6f 20 22 73 74 72 69 rovided to "stri
8f2b0 6e 67 69 66 79 22 20 74 68 65 20 76 61 6c 75 65 ngify" the value
8f2c0 20 6f 66 20 74 68 65 20 64 65 66 69 6e 65 0a 2a of the define.*
8f2d0 2a 20 66 6f 72 20 74 68 6f 73 65 20 6f 70 74 69 * for those opti
8f2e0 6f 6e 73 20 69 6e 20 77 68 69 63 68 20 74 68 65 ons in which the
8f2f0 20 76 61 6c 75 65 20 69 73 20 6d 65 61 6e 69 6e value is meanin
8f300 67 66 75 6c 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 gful. */.#define
8f310 20 43 54 49 4d 45 4f 50 54 5f 56 41 4c 5f 28 6f CTIMEOPT_VAL_(o
8f320 70 74 29 20 23 6f 70 74 0a 23 64 65 66 69 6e 65 pt) #opt.#define
8f330 20 43 54 49 4d 45 4f 50 54 5f 56 41 4c 28 6f 70 CTIMEOPT_VAL(op
8f340 74 29 20 43 54 49 4d 45 4f 50 54 5f 56 41 4c 5f t) CTIMEOPT_VAL_
8f350 28 6f 70 74 29 0a 0a 23 69 66 64 65 66 20 53 51 (opt)..#ifdef SQ
8f360 4c 49 54 45 5f 33 32 42 49 54 5f 52 4f 57 49 44 LITE_32BIT_ROWID
8f370 0a 20 20 22 33 32 42 49 54 5f 52 4f 57 49 44 22 . "32BIT_ROWID"
8f380 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 ,.#endif.#ifdef
8f390 53 51 4c 49 54 45 5f 34 5f 42 59 54 45 5f 41 4c SQLITE_4_BYTE_AL
8f3a0 49 47 4e 45 44 5f 4d 41 4c 4c 4f 43 0a 20 20 22 IGNED_MALLOC. "
8f3b0 34 5f 42 59 54 45 5f 41 4c 49 47 4e 45 44 5f 4d 4_BYTE_ALIGNED_M
8f3c0 41 4c 4c 4f 43 22 2c 0a 23 65 6e 64 69 66 0a 23 ALLOC",.#endif.#
8f3d0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 41 53 ifdef SQLITE_CAS
8f3e0 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 E_SENSITIVE_LIKE
8f3f0 0a 20 20 22 43 41 53 45 5f 53 45 4e 53 49 54 49 . "CASE_SENSITI
8f400 56 45 5f 4c 49 4b 45 22 2c 0a 23 65 6e 64 69 66 VE_LIKE",.#endif
8f410 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
8f420 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 22 43 48 HECK_PAGES. "CH
8f430 45 43 4b 5f 50 41 47 45 53 22 2c 0a 23 65 6e 64 ECK_PAGES",.#end
8f440 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 if.#ifdef SQLITE
8f450 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 0a 20 _COVERAGE_TEST.
8f460 20 22 43 4f 56 45 52 41 47 45 5f 54 45 53 54 22 "COVERAGE_TEST"
8f470 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 ,.#endif.#ifdef
8f480 53 51 4c 49 54 45 5f 43 55 52 44 49 52 0a 20 20 SQLITE_CURDIR.
8f490 22 43 55 52 44 49 52 22 2c 0a 23 65 6e 64 69 66 "CURDIR",.#endif
8f4a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
8f4b0 45 42 55 47 0a 20 20 22 44 45 42 55 47 22 2c 0a EBUG. "DEBUG",.
8f4c0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 #endif.#ifdef SQ
8f4d0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 LITE_DEFAULT_LOC
8f4e0 4b 49 4e 47 5f 4d 4f 44 45 0a 20 20 22 44 45 46 KING_MODE. "DEF
8f4f0 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 AULT_LOCKING_MOD
8f500 45 3d 22 20 43 54 49 4d 45 4f 50 54 5f 56 41 4c E=" CTIMEOPT_VAL
8f510 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f (SQLITE_DEFAULT_
8f520 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 29 2c 0a 23 LOCKING_MODE),.#
8f530 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
8f540 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 ITE_DISABLE_DIRS
8f550 59 4e 43 0a 20 20 22 44 49 53 41 42 4c 45 5f 44 YNC. "DISABLE_D
8f560 49 52 53 59 4e 43 22 2c 0a 23 65 6e 64 69 66 0a IRSYNC",.#endif.
8f570 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49 #ifdef SQLITE_DI
8f580 53 41 42 4c 45 5f 4c 46 53 0a 20 20 22 44 49 53 SABLE_LFS. "DIS
8f590 41 42 4c 45 5f 4c 46 53 22 2c 0a 23 65 6e 64 69 ABLE_LFS",.#endi
8f5a0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
8f5b0 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
8f5c0 49 54 45 0a 20 20 22 45 4e 41 42 4c 45 5f 41 54 ITE. "ENABLE_AT
8f5d0 4f 4d 49 43 5f 57 52 49 54 45 22 2c 0a 23 65 6e OMIC_WRITE",.#en
8f5e0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
8f5f0 45 5f 45 4e 41 42 4c 45 5f 43 45 52 4f 44 0a 20 E_ENABLE_CEROD.
8f600 20 22 45 4e 41 42 4c 45 5f 43 45 52 4f 44 22 2c "ENABLE_CEROD",
8f610 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
8f620 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c QLITE_ENABLE_COL
8f630 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 20 20 22 UMN_METADATA. "
8f640 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 ENABLE_COLUMN_ME
8f650 54 41 44 41 54 41 22 2c 0a 23 65 6e 64 69 66 0a TADATA",.#endif.
8f660 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
8f670 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 ABLE_EXPENSIVE_A
8f680 53 53 45 52 54 0a 20 20 22 45 4e 41 42 4c 45 5f SSERT. "ENABLE_
8f690 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 EXPENSIVE_ASSERT
8f6a0 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 ",.#endif.#ifdef
8f6b0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 SQLITE_ENABLE_F
8f6c0 54 53 31 0a 20 20 22 45 4e 41 42 4c 45 5f 46 54 TS1. "ENABLE_FT
8f6d0 53 31 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 S1",.#endif.#ifd
8f6e0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
8f6f0 5f 46 54 53 32 0a 20 20 22 45 4e 41 42 4c 45 5f _FTS2. "ENABLE_
8f700 46 54 53 32 22 2c 0a 23 65 6e 64 69 66 0a 23 69 FTS2",.#endif.#i
8f710 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
8f720 4c 45 5f 46 54 53 33 0a 20 20 22 45 4e 41 42 4c LE_FTS3. "ENABL
8f730 45 5f 46 54 53 33 22 2c 0a 23 65 6e 64 69 66 0a E_FTS3",.#endif.
8f740 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
8f750 41 42 4c 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 ABLE_FTS3_PARENT
8f760 48 45 53 49 53 0a 20 20 22 45 4e 41 42 4c 45 5f HESIS. "ENABLE_
8f770 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53 49 53 FTS3_PARENTHESIS
8f780 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 ",.#endif.#ifdef
8f790 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 SQLITE_ENABLE_F
8f7a0 54 53 34 0a 20 20 22 45 4e 41 42 4c 45 5f 46 54 TS4. "ENABLE_FT
8f7b0 53 34 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 S4",.#endif.#ifd
8f7c0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
8f7d0 5f 49 43 55 0a 20 20 22 45 4e 41 42 4c 45 5f 49 _ICU. "ENABLE_I
8f7e0 43 55 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 CU",.#endif.#ifd
8f7f0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
8f800 5f 49 4f 54 52 41 43 45 0a 20 20 22 45 4e 41 42 _IOTRACE. "ENAB
8f810 4c 45 5f 49 4f 54 52 41 43 45 22 2c 0a 23 65 6e LE_IOTRACE",.#en
8f820 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
8f830 45 5f 45 4e 41 42 4c 45 5f 4c 4f 41 44 5f 45 58 E_ENABLE_LOAD_EX
8f840 54 45 4e 53 49 4f 4e 0a 20 20 22 45 4e 41 42 4c TENSION. "ENABL
8f850 45 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e E_LOAD_EXTENSION
8f860 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 ",.#endif.#ifdef
8f870 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
8f880 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 22 OCKING_STYLE. "
8f890 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
8f8a0 54 59 4c 45 3d 22 20 43 54 49 4d 45 4f 50 54 5f TYLE=" CTIMEOPT_
8f8b0 56 41 4c 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c VAL(SQLITE_ENABL
8f8c0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 E_LOCKING_STYLE)
8f8d0 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 ,.#endif.#ifdef
8f8e0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 SQLITE_ENABLE_ME
8f8f0 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a MORY_MANAGEMENT.
8f900 20 20 22 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 "ENABLE_MEMORY
8f910 5f 4d 41 4e 41 47 45 4d 45 4e 54 22 2c 0a 23 65 _MANAGEMENT",.#e
8f920 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
8f930 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
8f940 33 0a 20 20 22 45 4e 41 42 4c 45 5f 4d 45 4d 53 3. "ENABLE_MEMS
8f950 59 53 33 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 YS3",.#endif.#if
8f960 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
8f970 45 5f 4d 45 4d 53 59 53 35 0a 20 20 22 45 4e 41 E_MEMSYS5. "ENA
8f980 42 4c 45 5f 4d 45 4d 53 59 53 35 22 2c 0a 23 65 BLE_MEMSYS5",.#e
8f990 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
8f9a0 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 TE_ENABLE_OVERSI
8f9b0 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 0a 20 20 ZE_CELL_CHECK.
8f9c0 22 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 "ENABLE_OVERSIZE
8f9d0 5f 43 45 4c 4c 5f 43 48 45 43 4b 22 2c 0a 23 65 _CELL_CHECK",.#e
8f9e0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
8f9f0 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45 0a TE_ENABLE_RTREE.
8fa00 20 20 22 45 4e 41 42 4c 45 5f 52 54 52 45 45 22 "ENABLE_RTREE"
8fa10 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 ,.#endif.#ifdef
8fa20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 SQLITE_ENABLE_ST
8fa30 41 54 33 0a 20 20 22 45 4e 41 42 4c 45 5f 53 54 AT3. "ENABLE_ST
8fa40 41 54 33 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 AT3",.#endif.#if
8fa50 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
8fa60 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a E_UNLOCK_NOTIFY.
8fa70 20 20 22 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b "ENABLE_UNLOCK
8fa80 5f 4e 4f 54 49 46 59 22 2c 0a 23 65 6e 64 69 66 _NOTIFY",.#endif
8fa90 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
8faa0 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c NABLE_UPDATE_DEL
8fab0 45 54 45 5f 4c 49 4d 49 54 0a 20 20 22 45 4e 41 ETE_LIMIT. "ENA
8fac0 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 BLE_UPDATE_DELET
8fad0 45 5f 4c 49 4d 49 54 22 2c 0a 23 65 6e 64 69 66 E_LIMIT",.#endif
8fae0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 .#ifdef SQLITE_H
8faf0 41 53 5f 43 4f 44 45 43 0a 20 20 22 48 41 53 5f AS_CODEC. "HAS_
8fb00 43 4f 44 45 43 22 2c 0a 23 65 6e 64 69 66 0a 23 CODEC",.#endif.#
8fb10 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56 ifdef SQLITE_HAV
8fb20 45 5f 49 53 4e 41 4e 0a 20 20 22 48 41 56 45 5f E_ISNAN. "HAVE_
8fb30 49 53 4e 41 4e 22 2c 0a 23 65 6e 64 69 66 0a 23 ISNAN",.#endif.#
8fb40 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d ifdef SQLITE_HOM
8fb50 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 EGROWN_RECURSIVE
8fb60 5f 4d 55 54 45 58 0a 20 20 22 48 4f 4d 45 47 52 _MUTEX. "HOMEGR
8fb70 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 OWN_RECURSIVE_MU
8fb80 54 45 58 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 TEX",.#endif.#if
8fb90 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 def SQLITE_IGNOR
8fba0 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 E_AFP_LOCK_ERROR
8fbb0 53 0a 20 20 22 49 47 4e 4f 52 45 5f 41 46 50 5f S. "IGNORE_AFP_
8fbc0 4c 4f 43 4b 5f 45 52 52 4f 52 53 22 2c 0a 23 65 LOCK_ERRORS",.#e
8fbd0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
8fbe0 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f TE_IGNORE_FLOCK_
8fbf0 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 22 49 LOCK_ERRORS. "I
8fc00 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b GNORE_FLOCK_LOCK
8fc10 5f 45 52 52 4f 52 53 22 2c 0a 23 65 6e 64 69 66 _ERRORS",.#endif
8fc20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 .#ifdef SQLITE_I
8fc30 4e 54 36 34 5f 54 59 50 45 0a 20 20 22 49 4e 54 NT64_TYPE. "INT
8fc40 36 34 5f 54 59 50 45 22 2c 0a 23 65 6e 64 69 66 64_TYPE",.#endif
8fc50 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4c .#ifdef SQLITE_L
8fc60 4f 43 4b 5f 54 52 41 43 45 0a 20 20 22 4c 4f 43 OCK_TRACE. "LOC
8fc70 4b 5f 54 52 41 43 45 22 2c 0a 23 65 6e 64 69 66 K_TRACE",.#endif
8fc80 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d .#ifdef SQLITE_M
8fc90 41 58 5f 53 43 48 45 4d 41 5f 52 45 54 52 59 0a AX_SCHEMA_RETRY.
8fca0 20 20 22 4d 41 58 5f 53 43 48 45 4d 41 5f 52 45 "MAX_SCHEMA_RE
8fcb0 54 52 59 3d 22 20 43 54 49 4d 45 4f 50 54 5f 56 TRY=" CTIMEOPT_V
8fcc0 41 4c 28 53 51 4c 49 54 45 5f 4d 41 58 5f 53 43 AL(SQLITE_MAX_SC
8fcd0 48 45 4d 41 5f 52 45 54 52 59 29 2c 0a 23 65 6e HEMA_RETRY),.#en
8fce0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
8fcf0 45 5f 4d 45 4d 44 45 42 55 47 0a 20 20 22 4d 45 E_MEMDEBUG. "ME
8fd00 4d 44 45 42 55 47 22 2c 0a 23 65 6e 64 69 66 0a MDEBUG",.#endif.
8fd10 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 #ifdef SQLITE_MI
8fd20 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 XED_ENDIAN_64BIT
8fd30 5f 46 4c 4f 41 54 0a 20 20 22 4d 49 58 45 44 5f _FLOAT. "MIXED_
8fd40 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f ENDIAN_64BIT_FLO
8fd50 41 54 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 AT",.#endif.#ifd
8fd60 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e ef SQLITE_NO_SYN
8fd70 43 0a 20 20 22 4e 4f 5f 53 59 4e 43 22 2c 0a 23 C. "NO_SYNC",.#
8fd80 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
8fd90 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 ITE_OMIT_ALTERTA
8fda0 42 4c 45 0a 20 20 22 4f 4d 49 54 5f 41 4c 54 45 BLE. "OMIT_ALTE
8fdb0 52 54 41 42 4c 45 22 2c 0a 23 65 6e 64 69 66 0a RTABLE",.#endif.
8fdc0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
8fdd0 49 54 5f 41 4e 41 4c 59 5a 45 0a 20 20 22 4f 4d IT_ANALYZE. "OM
8fde0 49 54 5f 41 4e 41 4c 59 5a 45 22 2c 0a 23 65 6e IT_ANALYZE",.#en
8fdf0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
8fe00 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 0a 20 20 E_OMIT_ATTACH.
8fe10 22 4f 4d 49 54 5f 41 54 54 41 43 48 22 2c 0a 23 "OMIT_ATTACH",.#
8fe20 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
8fe30 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 ITE_OMIT_AUTHORI
8fe40 5a 41 54 49 4f 4e 0a 20 20 22 4f 4d 49 54 5f 41 ZATION. "OMIT_A
8fe50 55 54 48 4f 52 49 5a 41 54 49 4f 4e 22 2c 0a 23 UTHORIZATION",.#
8fe60 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
8fe70 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 ITE_OMIT_AUTOINC
8fe80 52 45 4d 45 4e 54 0a 20 20 22 4f 4d 49 54 5f 41 REMENT. "OMIT_A
8fe90 55 54 4f 49 4e 43 52 45 4d 45 4e 54 22 2c 0a 23 UTOINCREMENT",.#
8fea0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
8feb0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 ITE_OMIT_AUTOINI
8fec0 54 0a 20 20 22 4f 4d 49 54 5f 41 55 54 4f 49 4e T. "OMIT_AUTOIN
8fed0 49 54 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 IT",.#endif.#ifd
8fee0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
8fef0 55 54 4f 4d 41 54 49 43 5f 49 4e 44 45 58 0a 20 UTOMATIC_INDEX.
8ff00 20 22 4f 4d 49 54 5f 41 55 54 4f 4d 41 54 49 43 "OMIT_AUTOMATIC
8ff10 5f 49 4e 44 45 58 22 2c 0a 23 65 6e 64 69 66 0a _INDEX",.#endif.
8ff20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
8ff30 49 54 5f 41 55 54 4f 52 45 53 45 54 0a 20 20 22 IT_AUTORESET. "
8ff40 4f 4d 49 54 5f 41 55 54 4f 52 45 53 45 54 22 2c OMIT_AUTORESET",
8ff50 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
8ff60 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
8ff70 41 43 55 55 4d 0a 20 20 22 4f 4d 49 54 5f 41 55 ACUUM. "OMIT_AU
8ff80 54 4f 56 41 43 55 55 4d 22 2c 0a 23 65 6e 64 69 TOVACUUM",.#endi
8ff90 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
8ffa0 4f 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54 OMIT_BETWEEN_OPT
8ffb0 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 22 4f 4d 49 IMIZATION. "OMI
8ffc0 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49 4d 49 T_BETWEEN_OPTIMI
8ffd0 5a 41 54 49 4f 4e 22 2c 0a 23 65 6e 64 69 66 0a ZATION",.#endif.
8ffe0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
8fff0 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a IT_BLOB_LITERAL.
90000 20 20 22 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 "OMIT_BLOB_LIT
90010 45 52 41 4c 22 2c 0a 23 65 6e 64 69 66 0a 23 69 ERAL",.#endif.#i
90020 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
90030 5f 42 54 52 45 45 43 4f 55 4e 54 0a 20 20 22 4f _BTREECOUNT. "O
90040 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 22 2c MIT_BTREECOUNT",
90050 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
90060 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 QLITE_OMIT_BUILT
90070 49 4e 5f 54 45 53 54 0a 20 20 22 4f 4d 49 54 5f IN_TEST. "OMIT_
90080 42 55 49 4c 54 49 4e 5f 54 45 53 54 22 2c 0a 23 BUILTIN_TEST",.#
90090 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
900a0 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 ITE_OMIT_CAST.
900b0 22 4f 4d 49 54 5f 43 41 53 54 22 2c 0a 23 65 6e "OMIT_CAST",.#en
900c0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
900d0 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 22 E_OMIT_CHECK. "
900e0 4f 4d 49 54 5f 43 48 45 43 4b 22 2c 0a 23 65 6e OMIT_CHECK",.#en
900f0 64 69 66 0a 2f 2a 20 2f 2f 20 72 65 64 75 6e 64 dif./* // redund
90100 61 6e 74 0a 2a 2a 20 23 69 66 64 65 66 20 53 51 ant.** #ifdef SQ
90110 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c LITE_OMIT_COMPIL
90120 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 0a 2a 2a EOPTION_DIAGS.**
90130 20 20 20 22 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 "OMIT_COMPILE
90140 4f 50 54 49 4f 4e 5f 44 49 41 47 53 22 2c 0a 2a OPTION_DIAGS",.*
90150 2a 20 23 65 6e 64 69 66 0a 2a 2f 0a 23 69 66 64 * #endif.*/.#ifd
90160 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 ef SQLITE_OMIT_C
90170 4f 4d 50 4c 45 54 45 0a 20 20 22 4f 4d 49 54 5f OMPLETE. "OMIT_
90180 43 4f 4d 50 4c 45 54 45 22 2c 0a 23 65 6e 64 69 COMPLETE",.#endi
90190 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
901a0 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 OMIT_COMPOUND_SE
901b0 4c 45 43 54 0a 20 20 22 4f 4d 49 54 5f 43 4f 4d LECT. "OMIT_COM
901c0 50 4f 55 4e 44 5f 53 45 4c 45 43 54 22 2c 0a 23 POUND_SELECT",.#
901d0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
901e0 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d ITE_OMIT_DATETIM
901f0 45 5f 46 55 4e 43 53 0a 20 20 22 4f 4d 49 54 5f E_FUNCS. "OMIT_
90200 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 22 2c DATETIME_FUNCS",
90210 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
90220 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 QLITE_OMIT_DECLT
90230 59 50 45 0a 20 20 22 4f 4d 49 54 5f 44 45 43 4c YPE. "OMIT_DECL
90240 54 59 50 45 22 2c 0a 23 65 6e 64 69 66 0a 23 69 TYPE",.#endif.#i
90250 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
90260 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20 22 4f _DEPRECATED. "O
90270 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 22 2c MIT_DEPRECATED",
90280 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
90290 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 QLITE_OMIT_DISKI
902a0 4f 0a 20 20 22 4f 4d 49 54 5f 44 49 53 4b 49 4f O. "OMIT_DISKIO
902b0 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 ",.#endif.#ifdef
902c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 SQLITE_OMIT_EXP
902d0 4c 41 49 4e 0a 20 20 22 4f 4d 49 54 5f 45 58 50 LAIN. "OMIT_EXP
902e0 4c 41 49 4e 22 2c 0a 23 65 6e 64 69 66 0a 23 69 LAIN",.#endif.#i
902f0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
90300 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 0a 20 20 _FLAG_PRAGMAS.
90310 22 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d "OMIT_FLAG_PRAGM
90320 41 53 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 AS",.#endif.#ifd
90330 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
90340 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 LOATING_POINT.
90350 22 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 "OMIT_FLOATING_P
90360 4f 49 4e 54 22 2c 0a 23 65 6e 64 69 66 0a 23 69 OINT",.#endif.#i
90370 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
90380 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 22 _FOREIGN_KEY. "
90390 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 OMIT_FOREIGN_KEY
903a0 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 ",.#endif.#ifdef
903b0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54 SQLITE_OMIT_GET
903c0 5f 54 41 42 4c 45 0a 20 20 22 4f 4d 49 54 5f 47 _TABLE. "OMIT_G
903d0 45 54 5f 54 41 42 4c 45 22 2c 0a 23 65 6e 64 69 ET_TABLE",.#endi
903e0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
903f0 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 OMIT_INCRBLOB.
90400 22 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 22 2c "OMIT_INCRBLOB",
90410 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
90420 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
90430 52 49 54 59 5f 43 48 45 43 4b 0a 20 20 22 4f 4d RITY_CHECK. "OM
90440 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 IT_INTEGRITY_CHE
90450 43 4b 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 CK",.#endif.#ifd
90460 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c ef SQLITE_OMIT_L
90470 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e IKE_OPTIMIZATION
90480 0a 20 20 22 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 . "OMIT_LIKE_OP
90490 54 49 4d 49 5a 41 54 49 4f 4e 22 2c 0a 23 65 6e TIMIZATION",.#en
904a0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
904b0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
904c0 4e 53 49 4f 4e 0a 20 20 22 4f 4d 49 54 5f 4c 4f NSION. "OMIT_LO
904d0 41 44 5f 45 58 54 45 4e 53 49 4f 4e 22 2c 0a 23 AD_EXTENSION",.#
904e0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
904f0 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 ITE_OMIT_LOCALTI
90500 4d 45 0a 20 20 22 4f 4d 49 54 5f 4c 4f 43 41 4c ME. "OMIT_LOCAL
90510 54 49 4d 45 22 2c 0a 23 65 6e 64 69 66 0a 23 69 TIME",.#endif.#i
90520 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
90530 5f 4c 4f 4f 4b 41 53 49 44 45 0a 20 20 22 4f 4d _LOOKASIDE. "OM
90540 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 22 2c 0a 23 IT_LOOKASIDE",.#
90550 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
90560 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 ITE_OMIT_MEMORYD
90570 42 0a 20 20 22 4f 4d 49 54 5f 4d 45 4d 4f 52 59 B. "OMIT_MEMORY
90580 44 42 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 DB",.#endif.#ifd
90590 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d ef SQLITE_OMIT_M
905a0 45 52 47 45 5f 53 4f 52 54 0a 20 20 22 4f 4d 49 ERGE_SORT. "OMI
905b0 54 5f 4d 45 52 47 45 5f 53 4f 52 54 22 2c 0a 23 T_MERGE_SORT",.#
905c0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
905d0 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 ITE_OMIT_OR_OPTI
905e0 4d 49 5a 41 54 49 4f 4e 0a 20 20 22 4f 4d 49 54 MIZATION. "OMIT
905f0 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e _OR_OPTIMIZATION
90600 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 ",.#endif.#ifdef
90610 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 SQLITE_OMIT_PAG
90620 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 22 4f 4d ER_PRAGMAS. "OM
90630 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 IT_PAGER_PRAGMAS
90640 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 ",.#endif.#ifdef
90650 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41 SQLITE_OMIT_PRA
90660 47 4d 41 0a 20 20 22 4f 4d 49 54 5f 50 52 41 47 GMA. "OMIT_PRAG
90670 4d 41 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 MA",.#endif.#ifd
90680 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 ef SQLITE_OMIT_P
90690 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b ROGRESS_CALLBACK
906a0 0a 20 20 22 4f 4d 49 54 5f 50 52 4f 47 52 45 53 . "OMIT_PROGRES
906b0 53 5f 43 41 4c 4c 42 41 43 4b 22 2c 0a 23 65 6e S_CALLBACK",.#en
906c0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
906d0 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 E_OMIT_QUICKBALA
906e0 4e 43 45 0a 20 20 22 4f 4d 49 54 5f 51 55 49 43 NCE. "OMIT_QUIC
906f0 4b 42 41 4c 41 4e 43 45 22 2c 0a 23 65 6e 64 69 KBALANCE",.#endi
90700 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
90710 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 20 20 22 OMIT_REINDEX. "
90720 4f 4d 49 54 5f 52 45 49 4e 44 45 58 22 2c 0a 23 OMIT_REINDEX",.#
90730 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
90740 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f ITE_OMIT_SCHEMA_
90750 50 52 41 47 4d 41 53 0a 20 20 22 4f 4d 49 54 5f PRAGMAS. "OMIT_
90760 53 43 48 45 4d 41 5f 50 52 41 47 4d 41 53 22 2c SCHEMA_PRAGMAS",
90770 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
90780 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d QLITE_OMIT_SCHEM
90790 41 5f 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41 A_VERSION_PRAGMA
907a0 53 0a 20 20 22 4f 4d 49 54 5f 53 43 48 45 4d 41 S. "OMIT_SCHEMA
907b0 5f 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53 _VERSION_PRAGMAS
907c0 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 ",.#endif.#ifdef
907d0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
907e0 52 45 44 5f 43 41 43 48 45 0a 20 20 22 4f 4d 49 RED_CACHE. "OMI
907f0 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 22 2c T_SHARED_CACHE",
90800 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
90810 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
90820 45 52 59 0a 20 20 22 4f 4d 49 54 5f 53 55 42 51 ERY. "OMIT_SUBQ
90830 55 45 52 59 22 2c 0a 23 65 6e 64 69 66 0a 23 69 UERY",.#endif.#i
90840 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
90850 5f 54 43 4c 5f 56 41 52 49 41 42 4c 45 0a 20 20 _TCL_VARIABLE.
90860 22 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49 41 42 "OMIT_TCL_VARIAB
90870 4c 45 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 LE",.#endif.#ifd
90880 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
90890 45 4d 50 44 42 0a 20 20 22 4f 4d 49 54 5f 54 45 EMPDB. "OMIT_TE
908a0 4d 50 44 42 22 2c 0a 23 65 6e 64 69 66 0a 23 69 MPDB",.#endif.#i
908b0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
908c0 5f 54 52 41 43 45 0a 20 20 22 4f 4d 49 54 5f 54 _TRACE. "OMIT_T
908d0 52 41 43 45 22 2c 0a 23 65 6e 64 69 66 0a 23 69 RACE",.#endif.#i
908e0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
908f0 5f 54 52 49 47 47 45 52 0a 20 20 22 4f 4d 49 54 _TRIGGER. "OMIT
90900 5f 54 52 49 47 47 45 52 22 2c 0a 23 65 6e 64 69 _TRIGGER",.#endi
90910 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
90920 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 OMIT_TRUNCATE_OP
90930 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 22 4f 4d TIMIZATION. "OM
90940 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 IT_TRUNCATE_OPTI
90950 4d 49 5a 41 54 49 4f 4e 22 2c 0a 23 65 6e 64 69 MIZATION",.#endi
90960 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
90970 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 22 4f 4d OMIT_UTF16. "OM
90980 49 54 5f 55 54 46 31 36 22 2c 0a 23 65 6e 64 69 IT_UTF16",.#endi
90990 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
909a0 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 20 20 22 4f OMIT_VACUUM. "O
909b0 4d 49 54 5f 56 41 43 55 55 4d 22 2c 0a 23 65 6e MIT_VACUUM",.#en
909c0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
909d0 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 22 4f E_OMIT_VIEW. "O
909e0 4d 49 54 5f 56 49 45 57 22 2c 0a 23 65 6e 64 69 MIT_VIEW",.#endi
909f0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
90a00 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
90a10 45 0a 20 20 22 4f 4d 49 54 5f 56 49 52 54 55 41 E. "OMIT_VIRTUA
90a20 4c 54 41 42 4c 45 22 2c 0a 23 65 6e 64 69 66 0a LTABLE",.#endif.
90a30 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
90a40 49 54 5f 57 41 4c 0a 20 20 22 4f 4d 49 54 5f 57 IT_WAL. "OMIT_W
90a50 41 4c 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 AL",.#endif.#ifd
90a60 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 ef SQLITE_OMIT_W
90a70 53 44 0a 20 20 22 4f 4d 49 54 5f 57 53 44 22 2c SD. "OMIT_WSD",
90a80 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
90a90 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f QLITE_OMIT_XFER_
90aa0 4f 50 54 0a 20 20 22 4f 4d 49 54 5f 58 46 45 52 OPT. "OMIT_XFER
90ab0 5f 4f 50 54 22 2c 0a 23 65 6e 64 69 66 0a 23 69 _OPT",.#endif.#i
90ac0 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46 fdef SQLITE_PERF
90ad0 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 20 20 ORMANCE_TRACE.
90ae0 22 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 "PERFORMANCE_TRA
90af0 43 45 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 CE",.#endif.#ifd
90b00 65 66 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f ef SQLITE_PROXY_
90b10 44 45 42 55 47 0a 20 20 22 50 52 4f 58 59 5f 44 DEBUG. "PROXY_D
90b20 45 42 55 47 22 2c 0a 23 65 6e 64 69 66 0a 23 69 EBUG",.#endif.#i
90b30 66 64 65 66 20 53 51 4c 49 54 45 5f 52 54 52 45 fdef SQLITE_RTRE
90b40 45 5f 49 4e 54 5f 4f 4e 4c 59 0a 20 20 22 52 54 E_INT_ONLY. "RT
90b50 52 45 45 5f 49 4e 54 5f 4f 4e 4c 59 22 2c 0a 23 REE_INT_ONLY",.#
90b60 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
90b70 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 ITE_SECURE_DELET
90b80 45 0a 20 20 22 53 45 43 55 52 45 5f 44 45 4c 45 E. "SECURE_DELE
90b90 54 45 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 TE",.#endif.#ifd
90ba0 65 66 20 53 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f ef SQLITE_SMALL_
90bb0 53 54 41 43 4b 0a 20 20 22 53 4d 41 4c 4c 5f 53 STACK. "SMALL_S
90bc0 54 41 43 4b 22 2c 0a 23 65 6e 64 69 66 0a 23 69 TACK",.#endif.#i
90bd0 66 64 65 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e fdef SQLITE_SOUN
90be0 44 45 58 0a 20 20 22 53 4f 55 4e 44 45 58 22 2c DEX. "SOUNDEX",
90bf0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
90c00 51 4c 49 54 45 5f 54 43 4c 0a 20 20 22 54 43 4c QLITE_TCL. "TCL
90c10 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 ",.#endif.#ifdef
90c20 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f SQLITE_TEMP_STO
90c30 52 45 0a 20 20 22 54 45 4d 50 5f 53 54 4f 52 45 RE. "TEMP_STORE
90c40 3d 22 20 43 54 49 4d 45 4f 50 54 5f 56 41 4c 28 =" CTIMEOPT_VAL(
90c50 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 SQLITE_TEMP_STOR
90c60 45 29 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 E),.#endif.#ifde
90c70 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
90c80 22 54 45 53 54 22 2c 0a 23 65 6e 64 69 66 0a 23 "TEST",.#endif.#
90c90 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 48 52 ifdef SQLITE_THR
90ca0 45 41 44 53 41 46 45 0a 20 20 22 54 48 52 45 41 EADSAFE. "THREA
90cb0 44 53 41 46 45 3d 22 20 43 54 49 4d 45 4f 50 54 DSAFE=" CTIMEOPT
90cc0 5f 56 41 4c 28 53 51 4c 49 54 45 5f 54 48 52 45 _VAL(SQLITE_THRE
90cd0 41 44 53 41 46 45 29 2c 0a 23 65 6e 64 69 66 0a ADSAFE),.#endif.
90ce0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 55 53 #ifdef SQLITE_US
90cf0 45 5f 41 4c 4c 4f 43 41 0a 20 20 22 55 53 45 5f E_ALLOCA. "USE_
90d00 41 4c 4c 4f 43 41 22 2c 0a 23 65 6e 64 69 66 0a ALLOCA",.#endif.
90d10 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 5a 45 #ifdef SQLITE_ZE
90d20 52 4f 5f 4d 41 4c 4c 4f 43 0a 20 20 22 5a 45 52 RO_MALLOC. "ZER
90d30 4f 5f 4d 41 4c 4c 4f 43 22 0a 23 65 6e 64 69 66 O_MALLOC".#endif
90d40 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e .};../*.** Given
90d50 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 the name of a c
90d60 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 ompile-time opti
90d70 6f 6e 2c 20 72 65 74 75 72 6e 20 74 72 75 65 20 on, return true
90d80 69 66 20 74 68 61 74 20 6f 70 74 69 6f 6e 0a 2a if that option.*
90d90 2a 20 77 61 73 20 75 73 65 64 20 61 6e 64 20 66 * was used and f
90da0 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a alse if not..**.
90db0 2a 2a 20 54 68 65 20 6e 61 6d 65 20 63 61 6e 20 ** The name can
90dc0 6f 70 74 69 6f 6e 61 6c 6c 79 20 62 65 67 69 6e optionally begin
90dd0 20 77 69 74 68 20 22 53 51 4c 49 54 45 5f 22 20 with "SQLITE_"
90de0 62 75 74 20 74 68 65 20 22 53 51 4c 49 54 45 5f but the "SQLITE_
90df0 22 20 70 72 65 66 69 78 0a 2a 2a 20 69 73 20 6e " prefix.** is n
90e00 6f 74 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 ot required for
90e10 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 a match..*/.SQLI
90e20 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
90e30 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e e3_compileoption
90e40 5f 75 73 65 64 28 63 6f 6e 73 74 20 63 68 61 72 _used(const char
90e50 20 2a 7a 4f 70 74 4e 61 6d 65 29 7b 0a 20 20 69 *zOptName){. i
90e60 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20 73 nt i, n;. if( s
90e70 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a qlite3StrNICmp(z
90e80 4f 70 74 4e 61 6d 65 2c 20 22 53 51 4c 49 54 45 OptName, "SQLITE
90e90 5f 22 2c 20 37 29 3d 3d 30 20 29 20 7a 4f 70 74 _", 7)==0 ) zOpt
90ea0 4e 61 6d 65 20 2b 3d 20 37 3b 0a 20 20 6e 20 3d Name += 7;. n =
90eb0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
90ec0 28 7a 4f 70 74 4e 61 6d 65 29 3b 0a 0a 20 20 2f (zOptName);.. /
90ed0 2a 20 53 69 6e 63 65 20 41 72 72 61 79 53 69 7a * Since ArraySiz
90ee0 65 28 61 7a 43 6f 6d 70 69 6c 65 4f 70 74 29 20 e(azCompileOpt)
90ef0 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 69 6e 20 73 is normally in s
90f00 69 6e 67 6c 65 20 64 69 67 69 74 73 2c 20 61 0a ingle digits, a.
90f10 20 20 2a 2a 20 6c 69 6e 65 61 72 20 73 65 61 72 ** linear sear
90f20 63 68 20 69 73 20 61 64 65 71 75 61 74 65 2e 20 ch is adequate.
90f30 20 4e 6f 20 6e 65 65 64 20 66 6f 72 20 61 20 62 No need for a b
90f40 69 6e 61 72 79 20 73 65 61 72 63 68 2e 20 2a 2f inary search. */
90f50 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 . for(i=0; i<Ar
90f60 72 61 79 53 69 7a 65 28 61 7a 43 6f 6d 70 69 6c raySize(azCompil
90f70 65 4f 70 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 eOpt); i++){.
90f80 20 69 66 28 20 20 20 28 73 71 6c 69 74 65 33 53 if( (sqlite3S
90f90 74 72 4e 49 43 6d 70 28 7a 4f 70 74 4e 61 6d 65 trNICmp(zOptName
90fa0 2c 20 61 7a 43 6f 6d 70 69 6c 65 4f 70 74 5b 69 , azCompileOpt[i
90fb0 5d 2c 20 6e 29 3d 3d 30 29 0a 20 20 20 20 20 20 ], n)==0).
90fc0 20 26 26 20 28 20 28 61 7a 43 6f 6d 70 69 6c 65 && ( (azCompile
90fd0 4f 70 74 5b 69 5d 5b 6e 5d 3d 3d 30 29 20 7c 7c Opt[i][n]==0) ||
90fe0 20 28 61 7a 43 6f 6d 70 69 6c 65 4f 70 74 5b 69 (azCompileOpt[i
90ff0 5d 5b 6e 5d 3d 3d 27 3d 27 29 20 29 20 29 20 72 ][n]=='=') ) ) r
91000 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 eturn 1;. }. r
91010 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
91020 2a 20 52 65 74 75 72 6e 20 74 68 65 20 4e 2d 74 * Return the N-t
91030 68 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f h compile-time o
91040 70 74 69 6f 6e 20 73 74 72 69 6e 67 2e 20 20 49 ption string. I
91050 66 20 4e 20 69 73 20 6f 75 74 20 6f 66 20 72 61 f N is out of ra
91060 6e 67 65 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 61 nge,.** return a
91070 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a NULL pointer..*
91080 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
91090 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
910a0 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 _compileoption_g
910b0 65 74 28 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28 et(int N){. if(
910c0 20 4e 3e 3d 30 20 26 26 20 4e 3c 41 72 72 61 79 N>=0 && N<Array
910d0 53 69 7a 65 28 61 7a 43 6f 6d 70 69 6c 65 4f 70 Size(azCompileOp
910e0 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e t) ){. return
910f0 20 61 7a 43 6f 6d 70 69 6c 65 4f 70 74 5b 4e 5d azCompileOpt[N]
91100 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 ;. }. return 0
91110 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
91120 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 QLITE_OMIT_COMPI
91130 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 20 2a LEOPTION_DIAGS *
91140 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
91150 2a 2a 20 45 6e 64 20 6f 66 20 63 74 69 6d 65 2e ** End of ctime.
91160 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
91170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91190 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
911a0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 74 ** Begin file st
911b0 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a atus.c *********
911c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
911d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
911e0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 */./*.** 2008 Ju
911f0 6e 65 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ne 18.**.** The
91200 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
91210 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
91220 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
91230 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
91240 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
91250 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
91260 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
91270 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
91280 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
91290 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
912a0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
912b0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
912c0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
912d0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
912e0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
912f0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
91300 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
91310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91350 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
91360 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 module implement
91370 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 s the sqlite3_st
91380 61 74 75 73 28 29 20 69 6e 74 65 72 66 61 63 65 atus() interface
91390 20 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20 and related.**
913a0 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a functionality..*
913b0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
913c0 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 49 6e * Include vdbeIn
913d0 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c t.h in the middl
913e0 65 20 6f 66 20 73 74 61 74 75 73 2e 63 20 2a 2a e of status.c **
913f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91400 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
91410 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 * Begin file vdb
91420 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
91430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91450 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 /./*.** 2003 Sep
91460 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 tember 6.**.** T
91470 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
91480 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
91490 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
914a0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
914b0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
914c0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
914d0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
914e0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
914f0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
91500 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
91510 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
91520 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
91530 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
91540 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
91550 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
91560 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
91570 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
91580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
915a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
915b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
915c0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
915d0 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69 is the header fi
915e0 6c 65 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69 le for informati
915f0 6f 6e 20 74 68 61 74 20 69 73 20 70 72 69 76 61 on that is priva
91600 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42 te to the.** VDB
91610 45 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 E. This informa
91620 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c tion used to all
91630 20 62 65 20 61 74 20 74 68 65 20 74 6f 70 20 6f be at the top o
91640 66 20 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20 f the single.**
91650 73 6f 75 72 63 65 20 63 6f 64 65 20 66 69 6c 65 source code file
91660 20 22 76 64 62 65 2e 63 22 2e 20 20 57 68 65 6e "vdbe.c". When
91670 20 74 68 61 74 20 66 69 6c 65 20 62 65 63 61 6d that file becam
91680 65 20 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a e too big (over.
91690 2a 2a 20 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f ** 6000 lines lo
916a0 6e 67 29 20 69 74 20 77 61 73 20 73 70 6c 69 74 ng) it was split
916b0 20 75 70 20 69 6e 74 6f 20 73 65 76 65 72 61 6c up into several
916c0 20 73 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 61 smaller files a
916d0 6e 64 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 65 nd.** this heade
916e0 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 r information wa
916f0 73 20 66 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a s factored out..
91700 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 56 44 42 45 */.#ifndef _VDBE
91710 49 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f INT_H_.#define _
91720 56 44 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a VDBEINT_H_../*.*
91730 2a 20 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61 * SQL is transla
91740 74 65 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65 ted into a seque
91750 6e 63 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69 nce of instructi
91760 6f 6e 73 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65 ons to be.** exe
91770 63 75 74 65 64 20 62 79 20 61 20 76 69 72 74 75 cuted by a virtu
91780 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 45 61 63 al machine. Eac
91790 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 h instruction is
917a0 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 an instance.**
917b0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
917c0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 structure..*/.t
917d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 ypedef struct Vd
917e0 62 65 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 beOp Op;../*.**
917f0 42 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a Boolean values.*
91800 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e /.typedef unsign
91810 65 64 20 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f ed char Bool;../
91820 2a 20 4f 70 61 71 75 65 20 74 79 70 65 20 75 73 * Opaque type us
91830 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 76 64 ed by code in vd
91840 62 65 73 6f 72 74 2e 63 20 2a 2f 0a 74 79 70 65 besort.c */.type
91850 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 53 def struct VdbeS
91860 6f 72 74 65 72 20 56 64 62 65 53 6f 72 74 65 72 orter VdbeSorter
91870 3b 0a 0a 2f 2a 20 4f 70 61 71 75 65 20 74 79 70 ;../* Opaque typ
91880 65 20 75 73 65 64 20 62 79 20 74 68 65 20 65 78 e used by the ex
91890 70 6c 61 69 6e 65 72 20 2a 2f 0a 74 79 70 65 64 plainer */.typed
918a0 65 66 20 73 74 72 75 63 74 20 45 78 70 6c 61 69 ef struct Explai
918b0 6e 20 45 78 70 6c 61 69 6e 3b 0a 0a 2f 2a 0a 2a n Explain;../*.*
918c0 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61 20 * A cursor is a
918d0 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 20 73 pointer into a s
918e0 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69 74 68 ingle BTree with
918f0 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 in a database fi
91900 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f le..** The curso
91910 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20 61 20 r can seek to a
91920 42 54 72 65 65 20 65 6e 74 72 79 20 77 69 74 68 BTree entry with
91930 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65 a particular ke
91940 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 6f 76 y, or.** loop ov
91950 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 6f er all entries o
91960 66 20 74 68 65 20 42 74 72 65 65 2e 20 20 59 6f f the Btree. Yo
91970 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73 65 72 u can also inser
91980 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a 20 65 t new BTree.** e
91990 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72 69 65 ntries or retrie
919a0 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20 64 61 ve the key or da
919b0 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 ta from the entr
919c0 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f y that the curso
919d0 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c r.** is currentl
919e0 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a y pointing to..*
919f0 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75 72 73 * .** Every curs
91a00 6f 72 20 74 68 61 74 20 74 68 65 20 76 69 72 74 or that the virt
91a10 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 20 ual machine has
91a20 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73 65 6e open is represen
91a30 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 ted by an.** ins
91a40 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
91a50 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
91a60 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 ..*/.struct Vdbe
91a70 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 43 75 72 Cursor {. BtCur
91a80 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 sor *pCursor;
91a90 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 73 /* The cursor s
91aa0 74 72 75 63 74 75 72 65 20 6f 66 20 74 68 65 20 tructure of the
91ab0 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 20 42 74 72 backend */. Btr
91ac0 65 65 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 ee *pBt;
91ad0 20 20 20 2f 2a 20 53 65 70 61 72 61 74 65 20 66 /* Separate f
91ae0 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70 ile holding temp
91af0 6f 72 61 72 79 20 74 61 62 6c 65 20 2a 2f 0a 20 orary table */.
91b00 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
91b10 66 6f 3b 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 fo; /* Info a
91b20 62 6f 75 74 20 69 6e 64 65 78 20 6b 65 79 73 20 bout index keys
91b30 6e 65 65 64 65 64 20 62 79 20 69 6e 64 65 78 20 needed by index
91b40 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 cursors */. int
91b50 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 iDb;
91b60 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 /* Index of c
91b70 75 72 73 6f 72 20 64 61 74 61 62 61 73 65 20 69 ursor database i
91b80 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 28 6f 72 20 n db->aDb[] (or
91b90 2d 31 29 20 2a 2f 0a 20 20 69 6e 74 20 70 73 65 -1) */. int pse
91ba0 75 64 6f 54 61 62 6c 65 52 65 67 3b 20 20 20 2f udoTableReg; /
91bb0 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 * Register holdi
91bc0 6e 67 20 70 73 65 75 64 6f 74 61 62 6c 65 20 63 ng pseudotable c
91bd0 6f 6e 74 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 ontent. */. int
91be0 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 nField;
91bf0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
91c00 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 68 65 fields in the he
91c10 61 64 65 72 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a ader */. Bool z
91c20 65 72 6f 65 64 3b 20 20 20 20 20 20 20 20 20 20 eroed;
91c30 2f 2a 20 54 72 75 65 20 69 66 20 7a 65 72 6f 65 /* True if zeroe
91c40 64 20 6f 75 74 20 61 6e 64 20 72 65 61 64 79 20 d out and ready
91c50 66 6f 72 20 72 65 75 73 65 20 2a 2f 0a 20 20 42 for reuse */. B
91c60 6f 6f 6c 20 72 6f 77 69 64 49 73 56 61 6c 69 64 ool rowidIsValid
91c70 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 ; /* True if
91c80 6c 61 73 74 52 6f 77 69 64 20 69 73 20 76 61 6c lastRowid is val
91c90 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 61 74 46 id */. Bool atF
91ca0 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a irst; /*
91cb0 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74 69 6e True if pointin
91cc0 67 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72 79 g to first entry
91cd0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 75 73 65 52 61 */. Bool useRa
91ce0 6e 64 6f 6d 52 6f 77 69 64 3b 20 20 2f 2a 20 47 ndomRowid; /* G
91cf0 65 6e 65 72 61 74 65 20 6e 65 77 20 72 65 63 6f enerate new reco
91d00 72 64 20 6e 75 6d 62 65 72 73 20 73 65 6d 69 2d rd numbers semi-
91d10 72 61 6e 64 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f randomly */. Bo
91d20 6f 6c 20 6e 75 6c 6c 52 6f 77 3b 20 20 20 20 20 ol nullRow;
91d30 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 /* True if p
91d40 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 ointing to a row
91d50 20 77 69 74 68 20 6e 6f 20 64 61 74 61 20 2a 2f with no data */
91d60 0a 20 20 42 6f 6f 6c 20 64 65 66 65 72 72 65 64 . Bool deferred
91d70 4d 6f 76 65 74 6f 3b 20 20 2f 2a 20 41 20 63 61 Moveto; /* A ca
91d80 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 ll to sqlite3Btr
91d90 65 65 4d 6f 76 65 74 6f 28 29 20 69 73 20 6e 65 eeMoveto() is ne
91da0 65 64 65 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 eded */. Bool i
91db0 73 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 sTable;
91dc0 2f 2a 20 54 72 75 65 20 69 66 20 61 20 74 61 62 /* True if a tab
91dd0 6c 65 20 72 65 71 75 69 72 69 6e 67 20 69 6e 74 le requiring int
91de0 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a 20 20 42 eger keys */. B
91df0 6f 6f 6c 20 69 73 49 6e 64 65 78 3b 20 20 20 20 ool isIndex;
91e00 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
91e10 61 6e 20 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e an index contain
91e20 69 6e 67 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 ing keys only -
91e30 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 42 6f 6f no data */. Boo
91e40 6c 20 69 73 4f 72 64 65 72 65 64 3b 20 20 20 20 l isOrdered;
91e50 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
91e60 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 74 61 62 e underlying tab
91e70 6c 65 20 69 73 20 42 54 52 45 45 5f 55 4e 4f 52 le is BTREE_UNOR
91e80 44 45 52 45 44 20 2a 2f 0a 20 20 42 6f 6f 6c 20 DERED */. Bool
91e90 69 73 53 6f 72 74 65 72 3b 20 20 20 20 20 20 20 isSorter;
91ea0 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6e 65 /* True if a ne
91eb0 77 2d 73 74 79 6c 65 20 73 6f 72 74 65 72 20 2a w-style sorter *
91ec0 2f 0a 20 20 42 6f 6f 6c 20 6d 75 6c 74 69 50 73 /. Bool multiPs
91ed0 65 75 64 6f 3b 20 20 20 20 20 2f 2a 20 4d 75 6c eudo; /* Mul
91ee0 74 69 2d 72 65 67 69 73 74 65 72 20 70 73 65 75 ti-register pseu
91ef0 64 6f 2d 63 75 72 73 6f 72 20 2a 2f 0a 20 20 73 do-cursor */. s
91f00 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
91f10 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b or *pVtabCursor;
91f20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 /* The cursor
91f30 66 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61 for a virtual ta
91f40 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 ble */. const s
91f50 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
91f60 4d 6f 64 75 6c 65 3b 20 20 20 20 20 2f 2a 20 4d Module; /* M
91f70 6f 64 75 6c 65 20 66 6f 72 20 63 75 72 73 6f 72 odule for cursor
91f80 20 70 56 74 61 62 43 75 72 73 6f 72 20 2a 2f 0a pVtabCursor */.
91f90 20 20 69 36 34 20 73 65 71 43 6f 75 6e 74 3b 20 i64 seqCount;
91fa0 20 20 20 20 20 20 20 20 2f 2a 20 53 65 71 75 65 /* Seque
91fb0 6e 63 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 nce counter */.
91fc0 20 69 36 34 20 6d 6f 76 65 74 6f 54 61 72 67 65 i64 movetoTarge
91fd0 74 3b 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 t; /* Argume
91fe0 6e 74 20 74 6f 20 74 68 65 20 64 65 66 65 72 72 nt to the deferr
91ff0 65 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d ed sqlite3BtreeM
92000 6f 76 65 74 6f 28 29 20 2a 2f 0a 20 20 69 36 34 oveto() */. i64
92010 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 lastRowid;
92020 20 20 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64 /* Last rowid
92030 20 66 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20 from a Next or
92040 4e 65 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f NextIdx operatio
92050 6e 20 2a 2f 0a 20 20 56 64 62 65 53 6f 72 74 65 n */. VdbeSorte
92060 72 20 2a 70 53 6f 72 74 65 72 3b 20 20 2f 2a 20 r *pSorter; /*
92070 53 6f 72 74 65 72 20 6f 62 6a 65 63 74 20 66 6f Sorter object fo
92080 72 20 4f 50 5f 53 6f 72 74 65 72 4f 70 65 6e 20 r OP_SorterOpen
92090 63 75 72 73 6f 72 73 20 2a 2f 0a 0a 20 20 2f 2a cursors */.. /*
920a0 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20 Result of last
920b0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
920c0 74 6f 28 29 20 64 6f 6e 65 20 62 79 20 61 6e 20 to() done by an
920d0 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 20 OP_NotExists or
920e0 0a 20 20 2a 2a 20 4f 50 5f 49 73 55 6e 69 71 75 . ** OP_IsUniqu
920f0 65 20 6f 70 63 6f 64 65 20 6f 6e 20 74 68 69 73 e opcode on this
92100 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 69 6e cursor. */. in
92110 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 0a 0a 20 t seekResult;..
92120 20 2f 2a 20 43 61 63 68 65 64 20 69 6e 66 6f 72 /* Cached infor
92130 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 mation about the
92140 20 68 65 61 64 65 72 20 66 6f 72 20 74 68 65 20 header for the
92150 64 61 74 61 20 72 65 63 6f 72 64 20 74 68 61 74 data record that
92160 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 73 6f 72 the. ** cursor
92170 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f is currently po
92180 69 6e 74 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c 79 inting to. Only
92190 20 76 61 6c 69 64 20 69 66 20 63 61 63 68 65 53 valid if cacheS
921a0 74 61 74 75 73 20 6d 61 74 63 68 65 73 0a 20 20 tatus matches.
921b0 2a 2a 20 56 64 62 65 2e 63 61 63 68 65 43 74 72 ** Vdbe.cacheCtr
921c0 2e 20 20 56 64 62 65 2e 63 61 63 68 65 43 74 72 . Vdbe.cacheCtr
921d0 20 77 69 6c 6c 20 6e 65 76 65 72 20 74 61 6b 65 will never take
921e0 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 on the value of
921f0 0a 20 20 2a 2a 20 43 41 43 48 45 5f 53 54 41 4c . ** CACHE_STAL
92200 45 20 61 6e 64 20 73 6f 20 73 65 74 74 69 6e 67 E and so setting
92210 20 63 61 63 68 65 53 74 61 74 75 73 3d 43 41 43 cacheStatus=CAC
92220 48 45 5f 53 54 41 4c 45 20 67 75 61 72 61 6e 74 HE_STALE guarant
92230 65 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 ees that. ** th
92240 65 20 63 61 63 68 65 20 69 73 20 6f 75 74 20 6f e cache is out o
92250 66 20 64 61 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a f date.. **. *
92260 2a 20 61 52 6f 77 20 6d 69 67 68 74 20 70 6f 69 * aRow might poi
92270 6e 74 20 74 6f 20 28 65 70 68 65 6d 65 72 61 6c nt to (ephemeral
92280 29 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 ) data for the c
92290 75 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72 20 69 urrent row, or i
922a0 74 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 t might. ** be
922b0 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 NULL.. */. u32
922c0 20 63 61 63 68 65 53 74 61 74 75 73 3b 20 20 20 cacheStatus;
922d0 20 20 20 2f 2a 20 43 61 63 68 65 20 69 73 20 76 /* Cache is v
922e0 61 6c 69 64 20 69 66 20 74 68 69 73 20 6d 61 74 alid if this mat
922f0 63 68 65 73 20 56 64 62 65 2e 63 61 63 68 65 43 ches Vdbe.cacheC
92300 74 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 79 6c tr */. int payl
92310 6f 61 64 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a oadSize; /*
92320 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 Total number of
92330 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 bytes in the re
92340 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 cord */. u32 *a
92350 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 Type;
92360 2f 2a 20 54 79 70 65 20 76 61 6c 75 65 73 20 66 /* Type values f
92370 6f 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 or all entries i
92380 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a n the record */.
92390 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20 u32 *aOffset;
923a0 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 /* Cache
923b0 64 20 6f 66 66 73 65 74 73 20 74 6f 20 74 68 65 d offsets to the
923c0 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 20 63 start of each c
923d0 6f 6c 75 6d 6e 73 20 64 61 74 61 20 2a 2f 0a 20 olumns data */.
923e0 20 75 38 20 2a 61 52 6f 77 3b 20 20 20 20 20 20 u8 *aRow;
923f0 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 /* Data f
92400 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 or the current r
92410 6f 77 2c 20 69 66 20 61 6c 6c 20 6f 6e 20 6f 6e ow, if all on on
92420 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74 79 70 e page */.};.typ
92430 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 edef struct Vdbe
92440 43 75 72 73 6f 72 20 56 64 62 65 43 75 72 73 6f Cursor VdbeCurso
92450 72 3b 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 r;../*.** When a
92460 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 sub-program is
92470 65 78 65 63 75 74 65 64 20 28 4f 50 5f 50 72 6f executed (OP_Pro
92480 67 72 61 6d 29 2c 20 61 20 73 74 72 75 63 74 75 gram), a structu
92490 72 65 20 6f 66 20 74 68 69 73 20 74 79 70 65 0a re of this type.
924a0 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 ** is allocated
924b0 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 75 72 to store the cur
924c0 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 rent value of th
924d0 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 e program counte
924e0 72 2c 20 61 73 0a 2a 2a 20 77 65 6c 6c 20 61 73 r, as.** well as
924f0 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65 6d the current mem
92500 6f 72 79 20 63 65 6c 6c 20 61 72 72 61 79 20 61 ory cell array a
92510 6e 64 20 76 61 72 69 6f 75 73 20 6f 74 68 65 72 nd various other
92520 20 66 72 61 6d 65 20 73 70 65 63 69 66 69 63 0a frame specific.
92530 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 ** values stored
92540 20 69 6e 20 74 68 65 20 56 64 62 65 20 73 74 72 in the Vdbe str
92550 75 63 74 2e 20 57 68 65 6e 20 74 68 65 20 73 75 uct. When the su
92560 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 66 69 6e b-program is fin
92570 69 73 68 65 64 2c 20 0a 2a 2a 20 74 68 65 73 65 ished, .** these
92580 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 70 69 values are copi
92590 65 64 20 62 61 63 6b 20 74 6f 20 74 68 65 20 56 ed back to the V
925a0 64 62 65 20 66 72 6f 6d 20 74 68 65 20 56 64 62 dbe from the Vdb
925b0 65 46 72 61 6d 65 20 73 74 72 75 63 74 75 72 65 eFrame structure
925c0 2c 0a 2a 2a 20 72 65 73 74 6f 72 69 6e 67 20 74 ,.** restoring t
925d0 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 he state of the
925e0 56 4d 20 74 6f 20 61 73 20 69 74 20 77 61 73 20 VM to as it was
925f0 62 65 66 6f 72 65 20 74 68 65 20 73 75 62 2d 70 before the sub-p
92600 72 6f 67 72 61 6d 0a 2a 2a 20 62 65 67 61 6e 20 rogram.** began
92610 65 78 65 63 75 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a executing..**.**
92620 20 54 68 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 The memory for
92630 61 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 a VdbeFrame obje
92640 63 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 ct is allocated
92650 61 6e 64 20 6d 61 6e 61 67 65 64 20 62 79 20 61 and managed by a
92660 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 65 6c 6c 20 memory.** cell
92670 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 28 63 in the parent (c
92680 61 6c 6c 69 6e 67 29 20 66 72 61 6d 65 2e 20 57 alling) frame. W
92690 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 hen the memory c
926a0 65 6c 6c 20 69 73 20 64 65 6c 65 74 65 64 20 6f ell is deleted o
926b0 72 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e r.** overwritten
926c0 2c 20 74 68 65 20 56 64 62 65 46 72 61 6d 65 20 , the VdbeFrame
926d0 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 66 72 object is not fr
926e0 65 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e eed immediately.
926f0 20 49 6e 73 74 65 61 64 2c 20 69 74 0a 2a 2a 20 Instead, it.**
92700 69 73 20 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 is linked into t
92710 68 65 20 56 64 62 65 2e 70 44 65 6c 46 72 61 6d he Vdbe.pDelFram
92720 65 20 6c 69 73 74 2e 20 54 68 65 20 63 6f 6e 74 e list. The cont
92730 65 6e 74 73 20 6f 66 20 74 68 65 20 56 64 62 65 ents of the Vdbe
92740 2e 70 44 65 6c 46 72 61 6d 65 0a 2a 2a 20 6c 69 .pDelFrame.** li
92750 73 74 20 69 73 20 64 65 6c 65 74 65 64 20 77 68 st is deleted wh
92760 65 6e 20 74 68 65 20 56 4d 20 69 73 20 72 65 73 en the VM is res
92770 65 74 20 69 6e 20 56 64 62 65 48 61 6c 74 28 29 et in VdbeHalt()
92780 2e 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 . The reason for
92790 20 64 6f 69 6e 67 0a 2a 2a 20 74 68 69 73 20 69 doing.** this i
927a0 6e 73 74 65 61 64 20 6f 66 20 64 65 6c 65 74 69 nstead of deleti
927b0 6e 67 20 74 68 65 20 56 64 62 65 46 72 61 6d 65 ng the VdbeFrame
927c0 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 69 73 20 immediately is
927d0 74 6f 20 61 76 6f 69 64 20 72 65 63 75 72 73 69 to avoid recursi
927e0 76 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 ve.** calls to s
927f0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
92800 65 61 73 65 28 29 20 77 68 65 6e 20 74 68 65 20 ease() when the
92810 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 62 65 6c memory cells bel
92820 6f 6e 67 69 6e 67 20 74 6f 20 74 68 65 0a 2a 2a onging to the.**
92830 20 63 68 69 6c 64 20 66 72 61 6d 65 20 61 72 65 child frame are
92840 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a released..**.**
92850 20 54 68 65 20 63 75 72 72 65 6e 74 6c 79 20 65 The currently e
92860 78 65 63 75 74 69 6e 67 20 66 72 61 6d 65 20 69 xecuting frame i
92870 73 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62 65 s stored in Vdbe
92880 2e 70 46 72 61 6d 65 2e 20 56 64 62 65 2e 70 46 .pFrame. Vdbe.pF
92890 72 61 6d 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 rame is.** set t
928a0 6f 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 63 75 o NULL if the cu
928b0 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e rrently executin
928c0 67 20 66 72 61 6d 65 20 69 73 20 74 68 65 20 6d g frame is the m
928d0 61 69 6e 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a ain program..*/.
928e0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 typedef struct V
928f0 64 62 65 46 72 61 6d 65 20 56 64 62 65 46 72 61 dbeFrame VdbeFra
92900 6d 65 3b 0a 73 74 72 75 63 74 20 56 64 62 65 46 me;.struct VdbeF
92910 72 61 6d 65 20 7b 0a 20 20 56 64 62 65 20 2a 76 rame {. Vdbe *v
92920 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
92930 20 2f 2a 20 56 4d 20 74 68 69 73 20 66 72 61 6d /* VM this fram
92940 65 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a e belongs to */.
92950 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 50 61 VdbeFrame *pPa
92960 72 65 6e 74 3b 20 20 20 20 20 2f 2a 20 50 61 72 rent; /* Par
92970 65 6e 74 20 6f 66 20 74 68 69 73 20 66 72 61 6d ent of this fram
92980 65 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 70 61 e, or NULL if pa
92990 72 65 6e 74 20 69 73 20 6d 61 69 6e 20 2a 2f 0a rent is main */.
929a0 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 Op *aOp;
929b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f /* Pro
929c0 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e gram instruction
929d0 73 20 66 6f 72 20 70 61 72 65 6e 74 20 66 72 61 s for parent fra
929e0 6d 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 me */. Mem *aMe
929f0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
92a00 2f 2a 20 41 72 72 61 79 20 6f 66 20 6d 65 6d 6f /* Array of memo
92a10 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 70 61 72 ry cells for par
92a20 65 6e 74 20 66 72 61 6d 65 20 2a 2f 0a 20 20 75 ent frame */. u
92a30 38 20 2a 61 4f 6e 63 65 46 6c 61 67 3b 20 20 20 8 *aOnceFlag;
92a40 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 /* Array
92a50 6f 66 20 4f 50 5f 4f 6e 63 65 20 66 6c 61 67 73 of OP_Once flags
92a60 20 66 6f 72 20 70 61 72 65 6e 74 20 66 72 61 6d for parent fram
92a70 65 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f e */. VdbeCurso
92a80 72 20 2a 2a 61 70 43 73 72 3b 20 20 20 20 20 2f r **apCsr; /
92a90 2a 20 41 72 72 61 79 20 6f 66 20 56 64 62 65 20 * Array of Vdbe
92aa0 63 75 72 73 6f 72 73 20 66 6f 72 20 70 61 72 65 cursors for pare
92ab0 6e 74 20 66 72 61 6d 65 20 2a 2f 0a 20 20 76 6f nt frame */. vo
92ac0 69 64 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 id *token;
92ad0 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 /* Copy of
92ae0 20 53 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 SubProgram.toke
92af0 6e 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 n */. i64 lastR
92b00 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f owid; /
92b10 2a 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f * Last insert ro
92b20 77 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73 wid (sqlite3.las
92b30 74 52 6f 77 69 64 29 20 2a 2f 0a 20 20 75 31 36 tRowid) */. u16
92b40 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 nCursor;
92b50 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
92b60 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 70 43 f entries in apC
92b70 73 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 sr */. int pc;
92b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
92b90 2f 2a 20 50 72 6f 67 72 61 6d 20 43 6f 75 6e 74 /* Program Count
92ba0 65 72 20 69 6e 20 70 61 72 65 6e 74 20 28 63 61 er in parent (ca
92bb0 6c 6c 69 6e 67 29 20 66 72 61 6d 65 20 2a 2f 0a lling) frame */.
92bc0 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 int nOp;
92bd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
92be0 65 20 6f 66 20 61 4f 70 20 61 72 72 61 79 20 2a e of aOp array *
92bf0 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 /. int nMem;
92c00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
92c10 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
92c20 20 69 6e 20 61 4d 65 6d 20 2a 2f 0a 20 20 69 6e in aMem */. in
92c30 74 20 6e 4f 6e 63 65 46 6c 61 67 3b 20 20 20 20 t nOnceFlag;
92c40 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
92c50 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 4f of entries in aO
92c60 6e 63 65 46 6c 61 67 20 2a 2f 0a 20 20 69 6e 74 nceFlag */. int
92c70 20 6e 43 68 69 6c 64 4d 65 6d 3b 20 20 20 20 20 nChildMem;
92c80 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
92c90 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 f memory cells f
92ca0 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 20 2a or child frame *
92cb0 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 43 73 /. int nChildCs
92cc0 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e r; /* N
92cd0 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 umber of cursors
92ce0 20 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 for child frame
92cf0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 */. int nChang
92d00 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
92d10 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 Statement chang
92d20 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67 65 es (Vdbe.nChange
92d30 73 29 20 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 23 64 s) */.};..#d
92d40 65 66 69 6e 65 20 56 64 62 65 46 72 61 6d 65 4d efine VdbeFrameM
92d50 65 6d 28 70 29 20 28 28 4d 65 6d 20 2a 29 26 28 em(p) ((Mem *)&(
92d60 28 75 38 20 2a 29 70 29 5b 52 4f 55 4e 44 38 28 (u8 *)p)[ROUND8(
92d70 73 69 7a 65 6f 66 28 56 64 62 65 46 72 61 6d 65 sizeof(VdbeFrame
92d80 29 29 5d 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 61 ))])../*.** A va
92d90 6c 75 65 20 66 6f 72 20 56 64 62 65 43 75 72 73 lue for VdbeCurs
92da0 6f 72 2e 63 61 63 68 65 56 61 6c 69 64 20 74 68 or.cacheValid th
92db0 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 61 63 at means the cac
92dc0 68 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 76 he is always inv
92dd0 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 alid..*/.#define
92de0 20 43 41 43 48 45 5f 53 54 41 4c 45 20 30 0a 0a CACHE_STALE 0..
92df0 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 6c 79 /*.** Internally
92e00 2c 20 74 68 65 20 76 64 62 65 20 6d 61 6e 69 70 , the vdbe manip
92e10 75 6c 61 74 65 73 20 6e 65 61 72 6c 79 20 61 6c ulates nearly al
92e20 6c 20 53 51 4c 20 76 61 6c 75 65 73 20 61 73 20 l SQL values as
92e30 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 Mem.** structure
92e40 73 2e 20 45 61 63 68 20 4d 65 6d 20 73 74 72 75 s. Each Mem stru
92e50 63 74 20 6d 61 79 20 63 61 63 68 65 20 6d 75 6c ct may cache mul
92e60 74 69 70 6c 65 20 72 65 70 72 65 73 65 6e 74 61 tiple representa
92e70 74 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c 0a 2a tions (string,.*
92e80 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e 29 20 * integer etc.)
92e90 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 of the same valu
92ea0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d e..*/.struct Mem
92eb0 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 {. sqlite3 *db
92ec0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
92ed0 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 associated datab
92ee0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
92ef0 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 /. char *z;
92f00 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e /* Strin
92f10 67 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65 20 g or BLOB value
92f20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 20 */. double r;
92f30 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 6c /* Real
92f40 20 76 61 6c 75 65 20 2a 2f 0a 20 20 75 6e 69 6f value */. unio
92f50 6e 20 7b 0a 20 20 20 20 69 36 34 20 69 3b 20 20 n {. i64 i;
92f60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
92f70 6e 74 65 67 65 72 20 76 61 6c 75 65 20 75 73 65 nteger value use
92f80 64 20 77 68 65 6e 20 4d 45 4d 5f 49 6e 74 20 69 d when MEM_Int i
92f90 73 20 73 65 74 20 69 6e 20 66 6c 61 67 73 20 2a s set in flags *
92fa0 2f 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b /. int nZero;
92fb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
92fc0 64 20 77 68 65 6e 20 62 69 74 20 4d 45 4d 5f 5a d when bit MEM_Z
92fd0 65 72 6f 20 69 73 20 73 65 74 20 69 6e 20 66 6c ero is set in fl
92fe0 61 67 73 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 ags */. FuncD
92ff0 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20 20 2f ef *pDef; /
93000 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e * Used only when
93010 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67 67 20 flags==MEM_Agg
93020 2a 2f 0a 20 20 20 20 52 6f 77 53 65 74 20 2a 70 */. RowSet *p
93030 52 6f 77 53 65 74 3b 20 20 20 20 2f 2a 20 55 73 RowSet; /* Us
93040 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c 61 ed only when fla
93050 67 73 3d 3d 4d 45 4d 5f 52 6f 77 53 65 74 20 2a gs==MEM_RowSet *
93060 2f 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 /. VdbeFrame
93070 2a 70 46 72 61 6d 65 3b 20 20 2f 2a 20 55 73 65 *pFrame; /* Use
93080 64 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 d when flags==ME
93090 4d 5f 46 72 61 6d 65 20 2a 2f 0a 20 20 7d 20 75 M_Frame */. } u
930a0 3b 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 ;. int n;
930b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
930c0 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 r of characters
930d0 69 6e 20 73 74 72 69 6e 67 20 76 61 6c 75 65 2c in string value,
930e0 20 65 78 63 6c 75 64 69 6e 67 20 27 5c 30 27 20 excluding '\0'
930f0 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 */. u16 flags;
93100 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65 /* Some
93110 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 combination of
93120 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d 5f 53 74 MEM_Null, MEM_St
93130 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20 65 74 63 2e r, MEM_Dyn, etc.
93140 20 2a 2f 0a 20 20 75 38 20 20 74 79 70 65 3b 20 */. u8 type;
93150 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
93160 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c of SQLITE_NULL,
93170 20 53 51 4c 49 54 45 5f 54 45 58 54 2c 20 53 51 SQLITE_TEXT, SQ
93180 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c 20 65 74 LITE_INTEGER, et
93190 63 20 2a 2f 0a 20 20 75 38 20 20 65 6e 63 3b 20 c */. u8 enc;
931a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 /* SQ
931b0 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
931c0 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 E_UTF16BE, SQLIT
931d0 45 5f 55 54 46 31 36 4c 45 20 2a 2f 0a 23 69 66 E_UTF16LE */.#if
931e0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
931f0 0a 20 20 4d 65 6d 20 2a 70 53 63 6f 70 79 46 72 . Mem *pScopyFr
93200 6f 6d 3b 20 20 20 20 2f 2a 20 54 68 69 73 20 4d om; /* This M
93210 65 6d 20 69 73 20 61 20 73 68 61 6c 6c 6f 77 20 em is a shallow
93220 63 6f 70 79 20 6f 66 20 70 53 63 6f 70 79 46 72 copy of pScopyFr
93230 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 46 om */. void *pF
93240 69 6c 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20 53 iller; /* S
93250 6f 20 74 68 61 74 20 73 69 7a 65 6f 66 28 4d 65 o that sizeof(Me
93260 6d 29 20 69 73 20 61 20 6d 75 6c 74 69 70 6c 65 m) is a multiple
93270 20 6f 66 20 38 20 2a 2f 0a 23 65 6e 64 69 66 0a of 8 */.#endif.
93280 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
93290 6f 69 64 20 2a 29 3b 20 20 2f 2a 20 49 66 20 6e oid *); /* If n
932a0 6f 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74 68 ot null, call th
932b0 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 is function to d
932c0 65 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a 20 elete Mem.z */.
932d0 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 char *zMalloc;
932e0 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63 20 /* Dynamic
932f0 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 buffer allocated
93300 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c by sqlite3_mall
93310 6f 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 4f oc() */.};../* O
93320 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 ne or more of th
93330 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 e following flag
93340 73 20 61 72 65 20 73 65 74 20 74 6f 20 69 6e 64 s are set to ind
93350 69 63 61 74 65 20 74 68 65 20 76 61 6c 69 64 4f icate the validO
93360 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 K.** representat
93370 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 6c 75 ions of the valu
93380 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 e stored in the
93390 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a 0a 2a Mem struct..**.*
933a0 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e 75 6c * If the MEM_Nul
933b0 6c 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 l flag is set, t
933c0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 hen the value is
933d0 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c an SQL NULL val
933e0 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72 20 ue..** No other
933f0 66 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65 74 flags may be set
93400 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a in this case..*
93410 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f *.** If the MEM_
93420 53 74 72 20 66 6c 61 67 20 69 73 20 73 65 74 20 Str flag is set
93430 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 then Mem.z point
93440 73 20 61 74 20 61 20 73 74 72 69 6e 67 20 72 65 s at a string re
93450 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a presentation..**
93460 20 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 73 Usually this is
93470 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 encoded in the
93480 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e 63 same unicode enc
93490 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69 oding as the mai
934a0 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28 73 n.** database (s
934b0 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78 63 ee below for exc
934c0 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68 65 eptions). If the
934d0 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20 69 MEM_Term flag i
934e0 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c 20 74 s also.** set, t
934f0 68 65 6e 20 74 68 65 20 73 74 72 69 6e 67 20 69 hen the string i
93500 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 s nul terminated
93510 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20 61 6e . The MEM_Int an
93520 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a 20 66 d MEM_Real .** f
93530 6c 61 67 73 20 6d 61 79 20 63 6f 65 78 69 73 74 lags may coexist
93540 20 77 69 74 68 20 74 68 65 20 4d 45 4d 5f 53 74 with the MEM_St
93550 72 20 66 6c 61 67 2e 0a 2a 2f 0a 23 64 65 66 69 r flag..*/.#defi
93560 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20 ne MEM_Null
93570 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61 6c 0x0001 /* Val
93580 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 ue is NULL */.#d
93590 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20 efine MEM_Str
935a0 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a 20 0x0002 /*
935b0 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e Value is a strin
935c0 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d g */.#define MEM
935d0 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30 30 _Int 0x000
935e0 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 4 /* Value is
935f0 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64 an integer */.#d
93600 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20 efine MEM_Real
93610 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20 0x0008 /*
93620 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c 20 Value is a real
93630 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e number */.#defin
93640 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20 e MEM_Blob
93650 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 75 0x0010 /* Valu
93660 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 23 e is a BLOB */.#
93670 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53 65 define MEM_RowSe
93680 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a t 0x0020 /*
93690 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77 53 Value is a RowS
936a0 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65 et object */.#de
936b0 66 69 6e 65 20 4d 45 4d 5f 46 72 61 6d 65 20 20 fine MEM_Frame
936c0 20 20 20 30 78 30 30 34 30 20 20 20 2f 2a 20 56 0x0040 /* V
936d0 61 6c 75 65 20 69 73 20 61 20 56 64 62 65 46 72 alue is a VdbeFr
936e0 61 6d 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 ame object */.#d
936f0 65 66 69 6e 65 20 4d 45 4d 5f 49 6e 76 61 6c 69 efine MEM_Invali
93700 64 20 20 20 30 78 30 30 38 30 20 20 20 2f 2a 20 d 0x0080 /*
93710 56 61 6c 75 65 20 69 73 20 75 6e 64 65 66 69 6e Value is undefin
93720 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 ed */.#define ME
93730 4d 5f 43 6c 65 61 72 65 64 20 20 20 30 78 30 31 M_Cleared 0x01
93740 30 30 20 20 20 2f 2a 20 4e 55 4c 4c 20 73 65 74 00 /* NULL set
93750 20 62 79 20 4f 50 5f 4e 75 6c 6c 2c 20 6e 6f 74 by OP_Null, not
93760 20 66 72 6f 6d 20 64 61 74 61 20 2a 2f 0a 23 64 from data */.#d
93770 65 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d 61 efine MEM_TypeMa
93780 73 6b 20 20 30 78 30 31 66 66 20 20 20 2f 2a 20 sk 0x01ff /*
93790 4d 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69 74 Mask of type bit
937a0 73 20 2a 2f 0a 0a 0a 2f 2a 20 57 68 65 6e 65 76 s */.../* Whenev
937b0 65 72 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 er Mem contains
937c0 61 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f a valid string o
937d0 72 20 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74 r blob represent
937e0 61 74 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a ation, one of.**
937f0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 the following f
93800 6c 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 lags must be set
93810 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 to determine th
93820 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d e memory managem
93830 65 6e 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f ent.** policy fo
93840 72 20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45 r Mem.z. The ME
93850 4d 5f 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c M_Term flag tell
93860 73 20 75 73 20 77 68 65 74 68 65 72 20 6f 72 20 s us whether or
93870 6e 6f 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e not the.** strin
93880 67 20 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30 g is \000 or \u0
93890 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 000 terminated.*
938a0 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65 /.#define MEM_Te
938b0 72 6d 20 20 20 20 20 20 30 78 30 32 30 30 20 20 rm 0x0200
938c0 20 2f 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69 /* String rep i
938d0 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 s nul terminated
938e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f */.#define MEM_
938f0 44 79 6e 20 20 20 20 20 20 20 30 78 30 34 30 30 Dyn 0x0400
93900 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 /* Need to ca
93910 6c 6c 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 ll sqliteFree()
93920 6f 6e 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66 on Mem.z */.#def
93930 69 6e 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20 ine MEM_Static
93940 20 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 0x0800 /* Me
93950 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 m.z points to a
93960 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f static string */
93970 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68 .#define MEM_Eph
93980 65 6d 20 20 20 20 20 30 78 31 30 30 30 20 20 20 em 0x1000
93990 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 /* Mem.z points
939a0 74 6f 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 to an ephemeral
939b0 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e string */.#defin
939c0 65 20 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20 e MEM_Agg
939d0 30 78 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 0x2000 /* Mem.
939e0 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 z points to an a
939f0 67 67 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 gg function cont
93a00 65 78 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d ext */.#define M
93a10 45 4d 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34 EM_Zero 0x4
93a20 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63 000 /* Mem.i c
93a30 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66 ontains count of
93a40 20 30 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 0s appended to
93a50 62 6c 6f 62 20 2a 2f 0a 23 69 66 64 65 66 20 53 blob */.#ifdef S
93a60 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
93a70 4c 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45 4d LOB. #undef MEM
93a80 5f 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65 20 _Zero. #define
93a90 4d 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30 0a MEM_Zero 0x0000.
93aa0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6c #endif../*.** Cl
93ab0 65 61 72 20 61 6e 79 20 65 78 69 73 74 69 6e 67 ear any existing
93ac0 20 74 79 70 65 20 66 6c 61 67 73 20 66 72 6f 6d type flags from
93ad0 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70 6c 61 a Mem and repla
93ae0 63 65 20 74 68 65 6d 20 77 69 74 68 20 66 0a 2a ce them with f.*
93af0 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 65 74 /.#define MemSet
93b00 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 20 5c TypeFlag(p, f) \
93b10 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67 73 20 . ((p)->flags
93b20 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 7e 28 = ((p)->flags&~(
93b30 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d 45 4d MEM_TypeMask|MEM
93b40 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 2f 2a 0a 2a _Zero))|f)../*.*
93b50 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
93b60 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 a memory cell i
93b70 73 20 6e 6f 74 20 6d 61 72 6b 65 64 20 61 73 20 s not marked as
93b80 69 6e 76 61 6c 69 64 2e 20 20 54 68 69 73 20 6d invalid. This m
93b90 61 63 72 6f 0a 2a 2a 20 69 73 20 66 6f 72 20 75 acro.** is for u
93ba0 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 se inside assert
93bb0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e () statements on
93bc0 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ly..*/.#ifdef SQ
93bd0 4c 49 54 45 5f 44 45 42 55 47 0a 23 64 65 66 69 LITE_DEBUG.#defi
93be0 6e 65 20 6d 65 6d 49 73 56 61 6c 69 64 28 4d 29 ne memIsValid(M)
93bf0 20 20 28 28 4d 29 2d 3e 66 6c 61 67 73 20 26 20 ((M)->flags &
93c00 4d 45 4d 5f 49 6e 76 61 6c 69 64 29 3d 3d 30 0a MEM_Invalid)==0.
93c10 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 41 20 56 64 #endif.../* A Vd
93c20 62 65 46 75 6e 63 20 69 73 20 6a 75 73 74 20 61 beFunc is just a
93c30 20 46 75 6e 63 44 65 66 20 28 64 65 66 69 6e 65 FuncDef (define
93c40 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 d in sqliteInt.h
93c50 29 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a ) that contains.
93c60 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e ** additional in
93c70 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
93c80 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d auxiliary inform
93c90 61 74 69 6f 6e 20 62 6f 75 6e 64 20 74 6f 20 61 ation bound to a
93ca0 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 rguments.** of t
93cb0 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 he function. Th
93cc0 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d is is used to im
93cd0 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 plement the sqli
93ce0 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
93cf0 29 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 ).** and sqlite3
93d00 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 _set_auxdata() A
93d10 50 49 73 2e 20 20 54 68 65 20 22 61 75 78 64 61 PIs. The "auxda
93d20 74 61 22 20 69 73 20 73 6f 6d 65 20 61 75 78 69 ta" is some auxi
93d30 6c 69 61 72 79 20 64 61 74 61 0a 2a 2a 20 74 68 liary data.** th
93d40 61 74 20 63 61 6e 20 62 65 20 61 73 73 6f 63 69 at can be associ
93d50 61 74 65 64 20 77 69 74 68 20 61 20 63 6f 6e 73 ated with a cons
93d60 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 20 74 6f tant argument to
93d70 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 a function. Th
93d80 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 66 75 6e is.** allows fun
93d90 63 74 69 6f 6e 73 20 73 75 63 68 20 61 73 20 22 ctions such as "
93da0 72 65 67 65 78 70 22 20 74 6f 20 63 6f 6d 70 69 regexp" to compi
93db0 6c 65 20 74 68 65 69 72 20 63 6f 6e 73 74 61 6e le their constan
93dc0 74 20 72 65 67 75 6c 61 72 0a 2a 2a 20 65 78 70 t regular.** exp
93dd0 72 65 73 73 69 6f 6e 20 61 72 67 75 6d 65 6e 74 ression argument
93de0 20 6f 6e 63 65 20 61 6e 64 20 72 65 75 73 65 64 once and reused
93df0 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 63 6f the compiled co
93e00 64 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 0a de for multiple.
93e10 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a ** invocations..
93e20 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 46 75 */.struct VdbeFu
93e30 6e 63 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a nc {. FuncDef *
93e40 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 pFunc;
93e50 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 66 69 /* The defi
93e60 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 nition of the fu
93e70 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 nction */. int
93e80 6e 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 nAux;
93e90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
93ea0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 ber of entries a
93eb0 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 70 41 llocated for apA
93ec0 75 78 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 ux[] */. struct
93ed0 20 41 75 78 44 61 74 61 20 7b 0a 20 20 20 20 76 AuxData {. v
93ee0 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 oid *pAux;
93ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
93f00 41 75 78 20 64 61 74 61 20 66 6f 72 20 74 68 65 Aux data for the
93f10 20 69 2d 74 68 20 61 72 67 75 6d 65 6e 74 20 2a i-th argument *
93f20 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 44 65 /. void (*xDe
93f30 6c 65 74 65 29 28 76 6f 69 64 20 2a 29 3b 20 20 lete)(void *);
93f40 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f /* Destructo
93f50 72 20 66 6f 72 20 74 68 65 20 61 75 78 20 64 61 r for the aux da
93f60 74 61 20 2a 2f 0a 20 20 7d 20 61 70 41 75 78 5b ta */. } apAux[
93f70 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 1];
93f80 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f /* One slo
93f90 74 20 66 6f 72 20 65 61 63 68 20 66 75 6e 63 74 t for each funct
93fa0 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a ion argument */.
93fb0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 63 };../*.** The "c
93fc0 6f 6e 74 65 78 74 22 20 61 72 67 75 6d 65 6e 74 ontext" argument
93fd0 20 66 6f 72 20 61 20 69 6e 73 74 61 6c 6c 61 62 for a installab
93fe0 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 le function. A
93ff0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a pointer to an.**
94000 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
94010 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 74 s structure is t
94020 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
94030 74 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 t to the routine
94040 73 20 75 73 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d s used.** implem
94050 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 ent the SQL func
94060 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tions..**.** The
94070 72 65 20 69 73 20 61 20 74 79 70 65 64 65 66 20 re is a typedef
94080 66 6f 72 20 74 68 69 73 20 73 74 72 75 63 74 75 for this structu
94090 72 65 20 69 6e 20 73 71 6c 69 74 65 2e 68 2e 20 re in sqlite.h.
940a0 20 53 6f 20 61 6c 6c 20 72 6f 75 74 69 6e 65 73 So all routines
940b0 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 65 20 70 75 ,.** even the pu
940c0 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 blic interface t
940d0 6f 20 53 51 4c 69 74 65 2c 20 63 61 6e 20 75 73 o SQLite, can us
940e0 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 e a pointer to t
940f0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a his structure..*
94100 2a 20 42 75 74 20 74 68 69 73 20 66 69 6c 65 20 * But this file
94110 69 73 20 74 68 65 20 6f 6e 6c 79 20 70 6c 61 63 is the only plac
94120 65 20 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 e where the inte
94130 72 6e 61 6c 20 64 65 74 61 69 6c 73 20 6f 66 20 rnal details of
94140 74 68 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 this.** structur
94150 65 20 61 72 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a e are known..**.
94160 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
94170 65 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73 e is defined ins
94180 69 64 65 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 ide of vdbeInt.h
94190 20 62 65 63 61 75 73 65 20 69 74 20 75 73 65 73 because it uses
941a0 20 73 75 62 73 74 72 75 63 74 75 72 65 73 0a 2a substructures.*
941b0 2a 20 28 4d 65 6d 29 20 77 68 69 63 68 20 61 72 * (Mem) which ar
941c0 65 20 6f 6e 6c 79 20 64 65 66 69 6e 65 64 20 74 e only defined t
941d0 68 65 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 here..*/.struct
941e0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
941f0 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 {. FuncDef *pFu
94200 6e 63 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 nc; /* Poi
94210 6e 74 65 72 20 74 6f 20 66 75 6e 63 74 69 6f 6e nter to function
94220 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d information. M
94230 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a UST BE FIRST */.
94240 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 VdbeFunc *pVdb
94250 65 46 75 6e 63 3b 20 20 2f 2a 20 41 75 78 69 6c eFunc; /* Auxil
94260 61 72 79 20 64 61 74 61 2c 20 69 66 20 63 72 65 ary data, if cre
94270 61 74 65 64 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73 ated. */. Mem s
94280 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
94290 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 /* The return v
942a0 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 68 alue is stored h
942b0 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d ere */. Mem *pM
942c0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f em; /
942d0 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 * Memory cell us
942e0 65 64 20 74 6f 20 73 74 6f 72 65 20 61 67 67 72 ed to store aggr
942f0 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 2a 2f egate context */
94300 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c . CollSeq *pCol
94310 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c l; /* Coll
94320 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a ating sequence *
94330 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72 3b /. int isError;
94340 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 /* Err
94350 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 or code returned
94360 20 62 79 20 74 68 65 20 66 75 6e 63 74 69 6f 6e by the function
94370 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 46 . */. int skipF
94380 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 lag; /*
94390 53 6b 69 70 20 73 6b 69 70 20 61 63 63 75 6d 75 Skip skip accumu
943a0 6c 61 74 6f 72 20 6c 6f 61 64 69 6e 67 20 69 66 lator loading if
943b0 20 74 72 75 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a true */.};../*.
943c0 2a 2a 20 41 6e 20 45 78 70 6c 61 69 6e 20 6f 62 ** An Explain ob
943d0 6a 65 63 74 20 61 63 63 75 6d 75 6c 61 74 65 73 ject accumulates
943e0 20 69 6e 64 65 6e 74 65 64 20 6f 75 74 70 75 74 indented output
943f0 20 77 68 69 63 68 20 69 73 20 68 65 6c 70 66 75 which is helpfu
94400 6c 0a 2a 2a 20 69 6e 20 64 65 73 63 72 69 62 69 l.** in describi
94410 6e 67 20 72 65 63 75 72 73 69 76 65 20 64 61 74 ng recursive dat
94420 61 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f a structures..*/
94430 0a 73 74 72 75 63 74 20 45 78 70 6c 61 69 6e 20 .struct Explain
94440 7b 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b {. Vdbe *pVdbe;
94450 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68 /* Attach
94460 20 74 68 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e the explanation
94470 20 74 6f 20 74 68 69 73 20 56 64 62 65 20 2a 2f to this Vdbe */
94480 0a 20 20 53 74 72 41 63 63 75 6d 20 73 74 72 3b . StrAccum str;
94490 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 /* The str
944a0 69 6e 67 20 62 65 69 6e 67 20 61 63 63 75 6d 75 ing being accumu
944b0 6c 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e lated */. int n
944c0 49 6e 64 65 6e 74 3b 20 20 20 20 20 20 20 2f 2a Indent; /*
944d0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 Number of eleme
944e0 6e 74 73 20 69 6e 20 61 49 6e 64 65 6e 74 20 2a nts in aIndent *
944f0 2f 0a 20 20 75 31 36 20 61 49 6e 64 65 6e 74 5b /. u16 aIndent[
94500 31 30 30 5d 3b 20 20 2f 2a 20 4c 65 76 65 6c 73 100]; /* Levels
94510 20 6f 66 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 of indentation
94520 2a 2f 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b */. char zBase[
94530 31 30 30 5d 3b 20 20 20 2f 2a 20 49 6e 69 74 69 100]; /* Initi
94540 61 6c 20 73 70 61 63 65 20 2a 2f 0a 7d 3b 0a 0a al space */.};..
94550 2f 2a 20 41 20 62 69 74 66 69 65 6c 64 20 74 79 /* A bitfield ty
94560 70 65 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 pe for use insid
94570 65 20 6f 66 20 73 74 72 75 63 74 75 72 65 73 2e e of structures.
94580 20 20 41 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 20 Always follow
94590 77 69 74 68 20 3a 4e 20 77 68 65 72 65 0a 2a 2a with :N where.**
945a0 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 N is the number
945b0 20 6f 66 20 62 69 74 73 2e 0a 2a 2f 0a 74 79 70 of bits..*/.typ
945c0 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 62 66 edef unsigned bf
945d0 74 3b 20 20 2f 2a 20 42 69 74 20 46 69 65 6c 64 t; /* Bit Field
945e0 20 54 79 70 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Type */../*.**
945f0 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
94600 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 he virtual machi
94610 6e 65 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 ne. This struct
94620 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ure contains the
94630 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 74 61 complete.** sta
94640 74 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 te of the virtua
94650 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a l machine..**.**
94660 20 54 68 65 20 22 73 71 6c 69 74 65 33 5f 73 74 The "sqlite3_st
94670 6d 74 22 20 73 74 72 75 63 74 75 72 65 20 70 6f mt" structure po
94680 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 72 65 inter that is re
94690 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 turned by sqlite
946a0 33 5f 70 72 65 70 61 72 65 28 29 0a 2a 2a 20 69 3_prepare().** i
946b0 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 s really a point
946c0 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 er to an instanc
946d0 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 e of this struct
946e0 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 ure..**.** The V
946f0 64 62 65 2e 69 6e 56 74 61 62 4d 65 74 68 6f 64 dbe.inVtabMethod
94700 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 variable is set
94710 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 to non-zero for
94720 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 the duration of
94730 0a 2a 2a 20 61 6e 79 20 76 69 72 74 75 61 6c 20 .** any virtual
94740 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 69 6e 76 table method inv
94750 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65 20 62 79 ocations made by
94760 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 the vdbe progra
94770 6d 2e 20 49 74 20 69 73 0a 2a 2a 20 73 65 74 20 m. It is.** set
94780 74 6f 20 32 20 66 6f 72 20 78 44 65 73 74 72 6f to 2 for xDestro
94790 79 20 6d 65 74 68 6f 64 20 63 61 6c 6c 73 20 61 y method calls a
947a0 6e 64 20 31 20 66 6f 72 20 61 6c 6c 20 6f 74 68 nd 1 for all oth
947b0 65 72 20 6d 65 74 68 6f 64 73 2e 20 54 68 69 73 er methods. This
947c0 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73 20 .** variable is
947d0 75 73 65 64 20 66 6f 72 20 74 77 6f 20 70 75 72 used for two pur
947e0 70 6f 73 65 73 3a 20 74 6f 20 61 6c 6c 6f 77 20 poses: to allow
947f0 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 73 xDestroy methods
94800 20 74 6f 20 65 78 65 63 75 74 65 0a 2a 2a 20 22 to execute.** "
94810 44 52 4f 50 20 54 41 42 4c 45 22 20 73 74 61 74 DROP TABLE" stat
94820 65 6d 65 6e 74 73 20 61 6e 64 20 74 6f 20 70 72 ements and to pr
94830 65 76 65 6e 74 20 73 6f 6d 65 20 6e 61 73 74 79 event some nasty
94840 20 73 69 64 65 20 65 66 66 65 63 74 73 20 6f 66 side effects of
94850 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 .** malloc failu
94860 72 65 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 re when SQLite i
94870 73 20 69 6e 76 6f 6b 65 64 20 72 65 63 75 72 73 s invoked recurs
94880 69 76 65 6c 79 20 62 79 20 61 20 76 69 72 74 75 ively by a virtu
94890 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 6d 65 74 al table .** met
948a0 68 6f 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f hod function..*/
948b0 0a 73 74 72 75 63 74 20 56 64 62 65 20 7b 0a 20 .struct Vdbe {.
948c0 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 sqlite3 *db;
948d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
948e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
948f0 69 6f 6e 20 74 68 61 74 20 6f 77 6e 73 20 74 68 ion that owns th
94900 69 73 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a is statement */.
94910 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 Op *aOp;
94920 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 /* Spa
94930 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 76 ce to hold the v
94940 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 27 73 irtual machine's
94950 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 4d 65 program */. Me
94960 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 m *aMem;
94970 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 65 6d /* The mem
94980 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f ory locations */
94990 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 20 . Mem **apArg;
949a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
949b0 67 75 6d 65 6e 74 73 20 74 6f 20 63 75 72 72 65 guments to curre
949c0 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 75 ntly executing u
949d0 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a ser function */.
949e0 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61 6d 65 3b Mem *aColName;
949f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c /* Col
94a00 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72 65 74 umn names to ret
94a10 75 72 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 urn */. Mem *pR
94a20 65 73 75 6c 74 53 65 74 3b 20 20 20 20 20 20 20 esultSet;
94a30 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 /* Pointer to a
94a40 6e 20 61 72 72 61 79 20 6f 66 20 72 65 73 75 6c n array of resul
94a50 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d ts */. int nMem
94a60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
94a70 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d /* Number of mem
94a80 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 63 75 ory locations cu
94a90 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 rrently allocate
94aa0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 d */. int nOp;
94ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
94ac0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 * Number of inst
94ad0 72 75 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 ructions in the
94ae0 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 program */. int
94af0 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20 nOpAlloc;
94b00 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
94b10 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 f slots allocate
94b20 64 20 66 6f 72 20 61 4f 70 5b 5d 20 2a 2f 0a 20 d for aOp[] */.
94b30 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20 20 int nLabel;
94b40 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
94b50 65 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 er of labels use
94b60 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62 d */. int *aLab
94b70 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f el; /
94b80 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 * Space to hold
94b90 74 68 65 20 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 the labels */.
94ba0 75 31 36 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b 20 u16 nResColumn;
94bb0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
94bc0 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
94bd0 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72 one row of the r
94be0 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 75 esult set */. u
94bf0 31 36 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20 16 nCursor;
94c00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
94c10 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 43 of slots in apC
94c20 73 72 5b 5d 20 2a 2f 0a 20 20 75 33 32 20 6d 61 sr[] */. u32 ma
94c30 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 gic;
94c40 20 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 /* Magic numbe
94c50 72 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65 r for sanity che
94c60 63 6b 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 cking */. char
94c70 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 *zErrMsg;
94c80 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 /* Error mess
94c90 61 67 65 20 77 72 69 74 74 65 6e 20 68 65 72 65 age written here
94ca0 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 50 72 65 */. Vdbe *pPre
94cb0 76 2c 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a v,*pNext; /*
94cc0 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 Linked list of
94cd0 56 44 42 45 73 20 77 69 74 68 20 74 68 65 20 73 VDBEs with the s
94ce0 61 6d 65 20 56 64 62 65 2e 64 62 20 2a 2f 0a 20 ame Vdbe.db */.
94cf0 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 VdbeCursor **ap
94d00 43 73 72 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 Csr; /* One
94d10 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 69 73 20 element of this
94d20 61 72 72 61 79 20 66 6f 72 20 65 61 63 68 20 6f array for each o
94d30 70 65 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 pen cursor */.
94d40 4d 65 6d 20 2a 61 56 61 72 3b 20 20 20 20 20 20 Mem *aVar;
94d50 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
94d60 73 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 s for the OP_Var
94d70 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f iable opcode. */
94d80 0a 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b . char **azVar;
94d90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
94da0 6d 65 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 me of variables
94db0 2a 2f 0a 20 20 79 6e 56 61 72 20 6e 56 61 72 3b */. ynVar nVar;
94dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
94dd0 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
94de0 73 20 69 6e 20 61 56 61 72 5b 5d 20 2a 2f 0a 20 s in aVar[] */.
94df0 20 79 6e 56 61 72 20 6e 7a 56 61 72 3b 20 20 20 ynVar nzVar;
94e00 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
94e10 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e er of entries in
94e20 20 61 7a 56 61 72 5b 5d 20 2a 2f 0a 20 20 75 33 azVar[] */. u3
94e30 32 20 63 61 63 68 65 43 74 72 3b 20 20 20 20 20 2 cacheCtr;
94e40 20 20 20 20 20 20 2f 2a 20 56 64 62 65 43 75 72 /* VdbeCur
94e50 73 6f 72 20 72 6f 77 20 63 61 63 68 65 20 67 65 sor row cache ge
94e60 6e 65 72 61 74 69 6f 6e 20 63 6f 75 6e 74 65 72 neration counter
94e70 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 */. int pc;
94e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
94e90 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 The program cou
94ea0 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 nter */. int rc
94eb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
94ec0 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 /* Value to re
94ed0 74 75 72 6e 20 2a 2f 0a 20 20 75 38 20 65 72 72 turn */. u8 err
94ee0 6f 72 41 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 orAction;
94ef0 20 20 2f 2a 20 52 65 63 6f 76 65 72 79 20 61 63 /* Recovery ac
94f00 74 69 6f 6e 20 74 6f 20 64 6f 20 69 6e 20 63 61 tion to do in ca
94f10 73 65 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 2a se of an error *
94f20 2f 0a 20 20 75 38 20 6d 69 6e 57 72 69 74 65 46 /. u8 minWriteF
94f30 69 6c 65 46 6f 72 6d 61 74 3b 20 20 2f 2a 20 4d ileFormat; /* M
94f40 69 6e 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d inimum file form
94f50 61 74 20 66 6f 72 20 77 72 69 74 61 62 6c 65 20 at for writable
94f60 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 2a database files *
94f70 2f 0a 20 20 62 66 74 20 65 78 70 6c 61 69 6e 3a /. bft explain:
94f80 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 2; /* T
94f90 72 75 65 20 69 66 20 45 58 50 4c 41 49 4e 20 70 rue if EXPLAIN p
94fa0 72 65 73 65 6e 74 20 6f 6e 20 53 51 4c 20 63 6f resent on SQL co
94fb0 6d 6d 61 6e 64 20 2a 2f 0a 20 20 62 66 74 20 69 mmand */. bft i
94fc0 6e 56 74 61 62 4d 65 74 68 6f 64 3a 32 3b 20 20 nVtabMethod:2;
94fd0 20 20 20 2f 2a 20 53 65 65 20 63 6f 6d 6d 65 6e /* See commen
94fe0 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20 20 62 66 ts above */. bf
94ff0 74 20 63 68 61 6e 67 65 43 6e 74 4f 6e 3a 31 3b t changeCntOn:1;
95000 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f /* True to
95010 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e update the chan
95020 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 ge-counter */.
95030 62 66 74 20 65 78 70 69 72 65 64 3a 31 3b 20 20 bft expired:1;
95040 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
95050 69 66 20 74 68 65 20 56 4d 20 6e 65 65 64 73 20 if the VM needs
95060 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 to be recompiled
95070 20 2a 2f 0a 20 20 62 66 74 20 72 75 6e 4f 6e 6c */. bft runOnl
95080 79 4f 6e 63 65 3a 31 3b 20 20 20 20 20 20 2f 2a yOnce:1; /*
95090 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 65 Automatically e
950a0 78 70 69 72 65 20 6f 6e 20 72 65 73 65 74 20 2a xpire on reset *
950b0 2f 0a 20 20 62 66 74 20 75 73 65 73 53 74 6d 74 /. bft usesStmt
950c0 4a 6f 75 72 6e 61 6c 3a 31 3b 20 20 2f 2a 20 54 Journal:1; /* T
950d0 72 75 65 20 69 66 20 75 73 65 73 20 61 20 73 74 rue if uses a st
950e0 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 atement journal
950f0 2a 2f 0a 20 20 62 66 74 20 72 65 61 64 4f 6e 6c */. bft readOnl
95100 79 3a 31 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 y:1; /*
95110 54 72 75 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e True for read-on
95120 6c 79 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f ly statements */
95130 0a 20 20 62 66 74 20 69 73 50 72 65 70 61 72 65 . bft isPrepare
95140 56 32 3a 31 3b 20 20 20 20 20 20 2f 2a 20 54 72 V2:1; /* Tr
95150 75 65 20 69 66 20 70 72 65 70 61 72 65 64 20 77 ue if prepared w
95160 69 74 68 20 70 72 65 70 61 72 65 5f 76 32 28 29 ith prepare_v2()
95170 20 2a 2f 0a 20 20 62 66 74 20 64 6f 69 6e 67 52 */. bft doingR
95180 65 72 75 6e 3a 31 3b 20 20 20 20 20 20 20 2f 2a erun:1; /*
95190 20 54 72 75 65 20 69 66 20 72 65 72 75 6e 6e 69 True if rerunni
951a0 6e 67 20 61 66 74 65 72 20 61 6e 20 61 75 74 6f ng after an auto
951b0 2d 72 65 70 72 65 70 61 72 65 20 2a 2f 0a 20 20 -reprepare */.
951c0 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 int nChange;
951d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
951e0 72 20 6f 66 20 64 62 20 63 68 61 6e 67 65 73 20 r of db changes
951f0 6d 61 64 65 20 73 69 6e 63 65 20 6c 61 73 74 20 made since last
95200 72 65 73 65 74 20 2a 2f 0a 20 20 79 44 62 4d 61 reset */. yDbMa
95210 73 6b 20 62 74 72 65 65 4d 61 73 6b 3b 20 20 20 sk btreeMask;
95220 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 /* Bitmask of
95230 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69 db->aDb[] entri
95240 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f es referenced */
95250 0a 20 20 79 44 62 4d 61 73 6b 20 6c 6f 63 6b 4d . yDbMask lockM
95260 61 73 6b 3b 20 20 20 20 20 20 20 2f 2a 20 53 75 ask; /* Su
95270 62 73 65 74 20 6f 66 20 62 74 72 65 65 4d 61 73 bset of btreeMas
95280 6b 20 74 68 61 74 20 72 65 71 75 69 72 65 73 20 k that requires
95290 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 a lock */. int
952a0 69 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 iStatement;
952b0 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 /* Statement
952c0 20 6e 75 6d 62 65 72 20 28 6f 72 20 30 20 69 66 number (or 0 if
952d0 20 68 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 has not opened
952e0 73 74 6d 74 29 20 2a 2f 0a 20 20 69 6e 74 20 61 stmt) */. int a
952f0 43 6f 75 6e 74 65 72 5b 33 5d 3b 20 20 20 20 20 Counter[3];
95300 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 73 20 75 /* Counters u
95310 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 73 sed by sqlite3_s
95320 74 6d 74 5f 73 74 61 74 75 73 28 29 20 2a 2f 0a tmt_status() */.
95330 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
95340 4d 49 54 5f 54 52 41 43 45 0a 20 20 69 36 34 20 MIT_TRACE. i64
95350 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20 20 20 startTime;
95360 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68 65 6e /* Time when
95370 20 71 75 65 72 79 20 73 74 61 72 74 65 64 20 2d query started -
95380 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66 69 6c used for profil
95390 69 6e 67 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 ing */.#endif.
953a0 69 36 34 20 6e 46 6b 43 6f 6e 73 74 72 61 69 6e i64 nFkConstrain
953b0 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 t; /* Numbe
953c0 72 20 6f 66 20 69 6d 6d 2e 20 46 4b 20 63 6f 6e r of imm. FK con
953d0 73 74 72 61 69 6e 74 73 20 74 68 69 73 20 56 4d straints this VM
953e0 20 2a 2f 0a 20 20 69 36 34 20 6e 53 74 6d 74 44 */. i64 nStmtD
953f0 65 66 43 6f 6e 73 3b 20 20 20 20 20 20 20 2f 2a efCons; /*
95400 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 66 2e 20 Number of def.
95410 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 68 65 6e constraints when
95420 20 73 74 6d 74 20 73 74 61 72 74 65 64 20 2a 2f stmt started */
95430 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 . char *zSql;
95440 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 /* Te
95450 78 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 xt of the SQL st
95460 61 74 65 6d 65 6e 74 20 74 68 61 74 20 67 65 6e atement that gen
95470 65 72 61 74 65 64 20 74 68 69 73 20 2a 2f 0a 20 erated this */.
95480 20 76 6f 69 64 20 2a 70 46 72 65 65 3b 20 20 20 void *pFree;
95490 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 /* Free
954a0 20 74 68 69 73 20 77 68 65 6e 20 64 65 6c 65 74 this when delet
954b0 69 6e 67 20 74 68 65 20 76 64 62 65 20 2a 2f 0a ing the vdbe */.
954c0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
954d0 42 55 47 0a 20 20 46 49 4c 45 20 2a 74 72 61 63 BUG. FILE *trac
954e0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
954f0 20 57 72 69 74 65 20 61 6e 20 65 78 65 63 75 74 Write an execut
95500 69 6f 6e 20 74 72 61 63 65 20 68 65 72 65 2c 20 ion trace here,
95510 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 23 if not NULL */.#
95520 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
95530 49 54 45 5f 45 4e 41 42 4c 45 5f 54 52 45 45 5f ITE_ENABLE_TREE_
95540 45 58 50 4c 41 49 4e 0a 20 20 45 78 70 6c 61 69 EXPLAIN. Explai
95550 6e 20 2a 70 45 78 70 6c 61 69 6e 3b 20 20 20 20 n *pExplain;
95560 20 20 2f 2a 20 54 68 65 20 65 78 70 6c 61 69 6e /* The explain
95570 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 er */. char *zE
95580 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 20 20 20 xplain;
95590 2f 2a 20 45 78 70 6c 61 6e 61 74 69 6f 6e 20 6f /* Explanation o
955a0 66 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 f data structure
955b0 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 56 64 s */.#endif. Vd
955c0 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b beFrame *pFrame;
955d0 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 /* Parent
955e0 66 72 61 6d 65 20 2a 2f 0a 20 20 56 64 62 65 46 frame */. VdbeF
955f0 72 61 6d 65 20 2a 70 44 65 6c 46 72 61 6d 65 3b rame *pDelFrame;
95600 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 72 /* List of fr
95610 61 6d 65 20 6f 62 6a 65 63 74 73 20 74 6f 20 66 ame objects to f
95620 72 65 65 20 6f 6e 20 56 4d 20 72 65 73 65 74 20 ree on VM reset
95630 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 61 6d 65 3b */. int nFrame;
95640 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
95650 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73 Number of frames
95660 20 69 6e 20 70 46 72 61 6d 65 20 6c 69 73 74 20 in pFrame list
95670 2a 2f 0a 20 20 75 33 32 20 65 78 70 6d 61 73 6b */. u32 expmask
95680 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
95690 42 69 6e 64 69 6e 67 20 74 6f 20 74 68 65 73 65 Binding to these
956a0 20 76 61 72 73 20 69 6e 76 61 6c 69 64 61 74 65 vars invalidate
956b0 73 20 56 4d 20 2a 2f 0a 20 20 53 75 62 50 72 6f s VM */. SubPro
956c0 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 gram *pProgram;
956d0 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 /* Linked list
956e0 20 6f 66 20 61 6c 6c 20 73 75 62 2d 70 72 6f 67 of all sub-prog
956f0 72 61 6d 73 20 75 73 65 64 20 62 79 20 56 4d 20 rams used by VM
95700 2a 2f 0a 20 20 69 6e 74 20 6e 4f 6e 63 65 46 6c */. int nOnceFl
95710 61 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ag; /*
95720 53 69 7a 65 20 6f 66 20 61 72 72 61 79 20 61 4f Size of array aO
95730 6e 63 65 46 6c 61 67 5b 5d 20 2a 2f 0a 20 20 75 nceFlag[] */. u
95740 38 20 2a 61 4f 6e 63 65 46 6c 61 67 3b 20 20 20 8 *aOnceFlag;
95750 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
95760 66 6f 72 20 4f 50 5f 4f 6e 63 65 20 2a 2f 0a 7d for OP_Once */.}
95770 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c ;../*.** The fol
95780 6c 6f 77 69 6e 67 20 61 72 65 20 61 6c 6c 6f 77 lowing are allow
95790 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 56 64 ed values for Vd
957a0 62 65 2e 6d 61 67 69 63 0a 2a 2f 0a 23 64 65 66 be.magic.*/.#def
957b0 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 ine VDBE_MAGIC_I
957c0 4e 49 54 20 20 20 20 20 30 78 32 36 62 63 65 61 NIT 0x26bcea
957d0 61 35 20 20 20 20 2f 2a 20 42 75 69 6c 64 69 6e a5 /* Buildin
957e0 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d g a VDBE program
957f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 */.#define VDBE
95800 5f 4d 41 47 49 43 5f 52 55 4e 20 20 20 20 20 20 _MAGIC_RUN
95810 30 78 62 64 66 32 30 64 61 33 20 20 20 20 2f 2a 0xbdf20da3 /*
95820 20 56 44 42 45 20 69 73 20 72 65 61 64 79 20 74 VDBE is ready t
95830 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 23 64 65 o execute */.#de
95840 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f fine VDBE_MAGIC_
95850 48 41 4c 54 20 20 20 20 20 30 78 35 31 39 63 32 HALT 0x519c2
95860 39 37 33 20 20 20 20 2f 2a 20 56 44 42 45 20 68 973 /* VDBE h
95870 61 73 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 as completed exe
95880 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e cution */.#defin
95890 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 41 e VDBE_MAGIC_DEA
958a0 44 20 20 20 20 20 30 78 62 36 30 36 63 33 63 38 D 0xb606c3c8
958b0 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 /* The VDBE
958c0 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 has been dealloc
958d0 61 74 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 ated */../*.** F
958e0 75 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 unction prototyp
958f0 65 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 es.*/.SQLITE_PRI
95900 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
95910 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 3VdbeFreeCursor(
95920 56 64 62 65 20 2a 2c 20 56 64 62 65 43 75 72 73 Vdbe *, VdbeCurs
95930 6f 72 2a 29 3b 0a 76 6f 69 64 20 73 71 6c 69 74 or*);.void sqlit
95940 65 56 64 62 65 50 6f 70 53 74 61 63 6b 28 56 64 eVdbePopStack(Vd
95950 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 be*,int);.SQLITE
95960 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
95970 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f ite3VdbeCursorMo
95980 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 2a veto(VdbeCursor*
95990 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 );.#if defined(S
959a0 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20 QLITE_DEBUG) ||
959b0 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f defined(VDBE_PRO
959c0 46 49 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 49 FILE).SQLITE_PRI
959d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
959e0 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 3VdbePrintOp(FIL
959f0 45 2a 2c 20 69 6e 74 2c 20 4f 70 2a 29 3b 0a 23 E*, int, Op*);.#
95a00 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 endif.SQLITE_PRI
95a10 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
95a20 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
95a30 6e 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 n(u32);.SQLITE_P
95a40 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 RIVATE u32 sqlit
95a50 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
95a60 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c (Mem*, int);.SQL
95a70 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
95a80 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
95a90 6c 50 75 74 28 75 6e 73 69 67 6e 65 64 20 63 68 lPut(unsigned ch
95aa0 61 72 2a 2c 20 69 6e 74 2c 20 4d 65 6d 2a 2c 20 ar*, int, Mem*,
95ab0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
95ac0 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
95ad0 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 63 6f VdbeSerialGet(co
95ae0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
95af0 72 2a 2c 20 75 33 32 2c 20 4d 65 6d 2a 29 3b 0a r*, u32, Mem*);.
95b00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
95b10 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 oid sqlite3VdbeD
95b20 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 64 62 eleteAuxData(Vdb
95b30 65 46 75 6e 63 2a 2c 20 69 6e 74 29 3b 0a 0a 69 eFunc*, int);..i
95b40 6e 74 20 73 71 6c 69 74 65 32 42 74 72 65 65 4b nt sqlite2BtreeK
95b50 65 79 43 6f 6d 70 61 72 65 28 42 74 43 75 72 73 eyCompare(BtCurs
95b60 6f 72 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 or *, const void
95b70 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e *, int, int, in
95b80 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 t *);.SQLITE_PRI
95b90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
95ba0 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 VdbeIdxKeyCompar
95bb0 65 28 56 64 62 65 43 75 72 73 6f 72 2a 2c 55 6e e(VdbeCursor*,Un
95bc0 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 2c 69 6e packedRecord*,in
95bd0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
95be0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
95bf0 64 62 65 49 64 78 52 6f 77 69 64 28 73 71 6c 69 dbeIdxRowid(sqli
95c00 74 65 33 2a 2c 20 42 74 43 75 72 73 6f 72 20 2a te3*, BtCursor *
95c10 2c 20 69 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45 , i64 *);.SQLITE
95c20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
95c30 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 ite3MemCompare(c
95c40 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 onst Mem*, const
95c50 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 43 6f 6c Mem*, const Col
95c60 6c 53 65 71 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 lSeq*);.SQLITE_P
95c70 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
95c80 65 33 56 64 62 65 45 78 65 63 28 56 64 62 65 2a e3VdbeExec(Vdbe*
95c90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
95ca0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
95cb0 65 4c 69 73 74 28 56 64 62 65 2a 29 3b 0a 53 51 eList(Vdbe*);.SQ
95cc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
95cd0 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 sqlite3VdbeHalt
95ce0 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f (Vdbe*);.SQLITE_
95cf0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
95d00 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
95d10 6f 64 69 6e 67 28 4d 65 6d 20 2a 2c 20 69 6e 74 oding(Mem *, int
95d20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
95d30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
95d40 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 2a 29 eMemTooBig(Mem*)
95d50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
95d60 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
95d70 4d 65 6d 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f MemCopy(Mem*, co
95d80 6e 73 74 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 nst Mem*);.SQLIT
95d90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
95da0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 qlite3VdbeMemSha
95db0 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 llowCopy(Mem*, c
95dc0 6f 6e 73 74 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b onst Mem*, int);
95dd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
95de0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
95df0 4d 65 6d 4d 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65 MemMove(Mem*, Me
95e00 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
95e10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
95e20 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 dbeMemNulTermina
95e30 74 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 te(Mem*);.SQLITE
95e40 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
95e50 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
95e60 72 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 r(Mem*, const ch
95e70 61 72 2a 2c 20 69 6e 74 2c 20 75 38 2c 20 76 6f ar*, int, u8, vo
95e80 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 id(*)(void*));.S
95e90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
95ea0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
95eb0 6d 53 65 74 49 6e 74 36 34 28 4d 65 6d 2a 2c 20 mSetInt64(Mem*,
95ec0 69 36 34 29 3b 0a 23 69 66 64 65 66 20 53 51 4c i64);.#ifdef SQL
95ed0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
95ee0 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65 G_POINT.# define
95ef0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
95f00 65 74 44 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 etDouble sqlite3
95f10 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 0a VdbeMemSetInt64.
95f20 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 #else.SQLITE_PRI
95f30 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
95f40 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 te3VdbeMemSetDou
95f50 62 6c 65 28 4d 65 6d 2a 2c 20 64 6f 75 62 6c 65 ble(Mem*, double
95f60 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 );.#endif.SQLITE
95f70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
95f80 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e lite3VdbeMemSetN
95f90 75 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 ull(Mem*);.SQLIT
95fa0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
95fb0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
95fc0 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e ZeroBlob(Mem*,in
95fd0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
95fe0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
95ff0 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 dbeMemSetRowSet(
96000 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
96010 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
96020 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 3VdbeMemMakeWrit
96030 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c eable(Mem*);.SQL
96040 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
96050 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 sqlite3VdbeMemSt
96060 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c 20 69 6e ringify(Mem*, in
96070 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
96080 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64 TE i64 sqlite3Vd
96090 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 2a 29 beIntValue(Mem*)
960a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
960b0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
960c0 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 4d 65 MemIntegerify(Me
960d0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
960e0 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 ATE double sqlit
960f0 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 e3VdbeRealValue(
96100 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
96110 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
96120 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 66 e3VdbeIntegerAff
96130 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c inity(Mem*);.SQL
96140 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
96150 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
96160 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c alify(Mem*);.SQL
96170 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
96180 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 sqlite3VdbeMemNu
96190 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 merify(Mem*);.SQ
961a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
961b0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 sqlite3VdbeMemF
961c0 72 6f 6d 42 74 72 65 65 28 42 74 43 75 72 73 6f romBtree(BtCurso
961d0 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d r*,int,int,int,M
961e0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
961f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
96200 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
96210 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f Mem *p);.SQLITE_
96220 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
96230 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
96240 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a seExternal(Mem *
96250 70 29 3b 0a 23 64 65 66 69 6e 65 20 56 64 62 65 p);.#define Vdbe
96260 4d 65 6d 52 65 6c 65 61 73 65 28 58 29 20 20 5c MemRelease(X) \
96270 0a 20 20 69 66 28 28 58 29 2d 3e 66 6c 61 67 73 . if((X)->flags
96280 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 &(MEM_Agg|MEM_Dy
96290 6e 7c 4d 45 4d 5f 52 6f 77 53 65 74 7c 4d 45 4d n|MEM_RowSet|MEM
962a0 5f 46 72 61 6d 65 29 29 20 5c 0a 20 20 20 20 73 _Frame)) \. s
962b0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
962c0 65 61 73 65 45 78 74 65 72 6e 61 6c 28 58 29 3b easeExternal(X);
962d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
962e0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
962f0 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d 2a 2c emFinalize(Mem*,
96300 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49 FuncDef*);.SQLI
96310 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
96320 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 char *sqlite3Op
96330 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3b 0a 53 codeName(int);.S
96340 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
96350 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
96360 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 Grow(Mem *pMem,
96370 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 int n, int prese
96380 72 76 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 rve);.SQLITE_PRI
96390 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
963a0 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 VdbeCloseStateme
963b0 6e 74 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b nt(Vdbe *, int);
963c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
963d0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
963e0 46 72 61 6d 65 44 65 6c 65 74 65 28 56 64 62 65 FrameDelete(Vdbe
963f0 46 72 61 6d 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Frame*);.SQLITE_
96400 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
96410 74 65 33 56 64 62 65 46 72 61 6d 65 52 65 73 74 te3VdbeFrameRest
96420 6f 72 65 28 56 64 62 65 46 72 61 6d 65 20 2a 29 ore(VdbeFrame *)
96430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
96440 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
96450 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 4d 65 eMemStoreType(Me
96460 6d 20 2a 70 4d 65 6d 29 3b 0a 53 51 4c 49 54 45 m *pMem);.SQLITE
96470 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
96480 69 74 65 33 56 64 62 65 54 72 61 6e 73 66 65 72 ite3VdbeTransfer
96490 45 72 72 6f 72 28 56 64 62 65 20 2a 70 29 3b 0a Error(Vdbe *p);.
964a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
964b0 4d 49 54 5f 4d 45 52 47 45 5f 53 4f 52 54 0a 23 MIT_MERGE_SORT.#
964c0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 define sqlite3V
964d0 64 62 65 53 6f 72 74 65 72 49 6e 69 74 28 59 2c dbeSorterInit(Y,
964e0 5a 29 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f Z) SQLITE_O
964f0 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 K.# define sqlit
96500 65 33 56 64 62 65 53 6f 72 74 65 72 57 72 69 74 e3VdbeSorterWrit
96510 65 28 58 2c 59 2c 5a 29 20 20 20 53 51 4c 49 54 e(X,Y,Z) SQLIT
96520 45 5f 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 E_OK.# define sq
96530 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 43 lite3VdbeSorterC
96540 6c 6f 73 65 28 59 2c 5a 29 0a 23 20 64 65 66 69 lose(Y,Z).# defi
96550 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f ne sqlite3VdbeSo
96560 72 74 65 72 52 6f 77 6b 65 79 28 59 2c 5a 29 20 rterRowkey(Y,Z)
96570 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 SQLITE_OK.# d
96580 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 efine sqlite3Vdb
96590 65 53 6f 72 74 65 72 52 65 77 69 6e 64 28 58 2c eSorterRewind(X,
965a0 59 2c 5a 29 20 20 53 51 4c 49 54 45 5f 4f 4b 0a Y,Z) SQLITE_OK.
965b0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
965c0 56 64 62 65 53 6f 72 74 65 72 4e 65 78 74 28 58 VdbeSorterNext(X
965d0 2c 59 2c 5a 29 20 20 20 20 53 51 4c 49 54 45 5f ,Y,Z) SQLITE_
965e0 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 OK.# define sqli
965f0 74 65 33 56 64 62 65 53 6f 72 74 65 72 43 6f 6d te3VdbeSorterCom
96600 70 61 72 65 28 58 2c 59 2c 5a 29 20 53 51 4c 49 pare(X,Y,Z) SQLI
96610 54 45 5f 4f 4b 0a 23 65 6c 73 65 0a 53 51 4c 49 TE_OK.#else.SQLI
96620 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
96630 71 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 qlite3VdbeSorter
96640 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 2c 20 Init(sqlite3 *,
96650 56 64 62 65 43 75 72 73 6f 72 20 2a 29 3b 0a 53 VdbeCursor *);.S
96660 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
96670 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f id sqlite3VdbeSo
96680 72 74 65 72 43 6c 6f 73 65 28 73 71 6c 69 74 65 rterClose(sqlite
96690 33 20 2a 2c 20 56 64 62 65 43 75 72 73 6f 72 20 3 *, VdbeCursor
966a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
966b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
966c0 62 65 53 6f 72 74 65 72 52 6f 77 6b 65 79 28 63 beSorterRowkey(c
966d0 6f 6e 73 74 20 56 64 62 65 43 75 72 73 6f 72 20 onst VdbeCursor
966e0 2a 2c 20 4d 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 *, Mem *);.SQLIT
966f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
96700 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 4e lite3VdbeSorterN
96710 65 78 74 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 ext(sqlite3 *, c
96720 6f 6e 73 74 20 56 64 62 65 43 75 72 73 6f 72 20 onst VdbeCursor
96730 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 *, int *);.SQLIT
96740 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
96750 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 52 lite3VdbeSorterR
96760 65 77 69 6e 64 28 73 71 6c 69 74 65 33 20 2a 2c ewind(sqlite3 *,
96770 20 63 6f 6e 73 74 20 56 64 62 65 43 75 72 73 6f const VdbeCurso
96780 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c r *, int *);.SQL
96790 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
967a0 73 71 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 sqlite3VdbeSorte
967b0 72 57 72 69 74 65 28 73 71 6c 69 74 65 33 20 2a rWrite(sqlite3 *
967c0 2c 20 63 6f 6e 73 74 20 56 64 62 65 43 75 72 73 , const VdbeCurs
967d0 6f 72 20 2a 2c 20 4d 65 6d 20 2a 29 3b 0a 53 51 or *, Mem *);.SQ
967e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
967f0 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f 72 74 sqlite3VdbeSort
96800 65 72 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 erCompare(const
96810 56 64 62 65 43 75 72 73 6f 72 20 2a 2c 20 4d 65 VdbeCursor *, Me
96820 6d 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a 23 65 6e m *, int *);.#en
96830 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 dif..#if !define
96840 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 d(SQLITE_OMIT_SH
96850 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 53 ARED_CACHE) && S
96860 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
96870 3e 30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 >0.SQLITE_PRIVAT
96880 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
96890 56 64 62 65 45 6e 74 65 72 28 56 64 62 65 2a 29 VdbeEnter(Vdbe*)
968a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
968b0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 void sqlite3V
968c0 64 62 65 4c 65 61 76 65 28 56 64 62 65 2a 29 3b dbeLeave(Vdbe*);
968d0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
968e0 73 71 6c 69 74 65 33 56 64 62 65 45 6e 74 65 72 sqlite3VdbeEnter
968f0 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c (X).# define sql
96900 69 74 65 33 56 64 62 65 4c 65 61 76 65 28 58 29 ite3VdbeLeave(X)
96910 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
96920 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
96930 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
96940 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 41 sqlite3VdbeMemA
96950 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 56 64 62 boutToChange(Vdb
96960 65 2a 2c 4d 65 6d 2a 29 3b 0a 23 65 6e 64 69 66 e*,Mem*);.#endif
96970 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
96980 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 _OMIT_FOREIGN_KE
96990 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 Y.SQLITE_PRIVATE
969a0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
969b0 43 68 65 63 6b 46 6b 28 56 64 62 65 20 2a 2c 20 CheckFk(Vdbe *,
969c0 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 int);.#else.# de
969d0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 fine sqlite3Vdbe
969e0 43 68 65 63 6b 46 6b 28 70 2c 69 29 20 30 0a 23 CheckFk(p,i) 0.#
969f0 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
96a00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
96a10 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 3VdbeMemTranslat
96a20 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 e(Mem*, u8);.#if
96a30 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
96a40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
96a50 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 void sqlite3Vd
96a60 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65 2a bePrintSql(Vdbe*
96a70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
96a80 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
96a90 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 VdbeMemPrettyPri
96aa0 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 nt(Mem *pMem, ch
96ab0 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69 ar *zBuf);.#endi
96ac0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 f.SQLITE_PRIVATE
96ad0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
96ae0 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d MemHandleBom(Mem
96af0 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 *pMem);..#ifnde
96b00 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
96b10 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52 CRBLOB.SQLITE_PR
96b20 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
96b30 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
96b40 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a 20 20 23 Blob(Mem *);. #
96b50 64 65 66 69 6e 65 20 45 78 70 61 6e 64 42 6c 6f define ExpandBlo
96b60 62 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 b(P) (((P)->flag
96b70 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 s&MEM_Zero)?sqli
96b80 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
96b90 42 6c 6f 62 28 50 29 3a 30 29 0a 23 65 6c 73 65 Blob(P):0).#else
96ba0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
96bb0 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 e3VdbeMemExpandB
96bc0 6c 6f 62 28 78 29 20 53 51 4c 49 54 45 5f 4f 4b lob(x) SQLITE_OK
96bd0 0a 20 20 23 64 65 66 69 6e 65 20 45 78 70 61 6e . #define Expan
96be0 64 42 6c 6f 62 28 50 29 20 53 51 4c 49 54 45 5f dBlob(P) SQLITE_
96bf0 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 OK.#endif..#endi
96c00 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 56 f /* !defined(_V
96c10 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a 2f DBEINT_H_) */../
96c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
96c30 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 nd of vdbeInt.h
96c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
96c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
96c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
96c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
96c80 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
96c90 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 we left off in s
96ca0 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a tatus.c ********
96cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
96cc0 2f 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 73 20 /*.** Variables
96cd0 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 63 6f in which to reco
96ce0 72 64 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d rd status inform
96cf0 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 ation..*/.typede
96d00 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 f struct sqlite3
96d10 53 74 61 74 54 79 70 65 20 73 71 6c 69 74 65 33 StatType sqlite3
96d20 53 74 61 74 54 79 70 65 3b 0a 73 74 61 74 69 63 StatType;.static
96d30 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 SQLITE_WSD stru
96d40 63 74 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 ct sqlite3StatTy
96d50 70 65 20 7b 0a 20 20 69 6e 74 20 6e 6f 77 56 61 pe {. int nowVa
96d60 6c 75 65 5b 31 30 5d 3b 20 20 20 20 20 20 20 20 lue[10];
96d70 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 61 6c 75 /* Current valu
96d80 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 56 61 6c e */. int mxVal
96d90 75 65 5b 31 30 5d 3b 20 20 20 20 20 20 20 20 20 ue[10];
96da0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61 6c 75 /* Maximum valu
96db0 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 53 74 e */.} sqlite3St
96dc0 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b 30 2c at = { {0,}, {0,
96dd0 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20 22 77 } };.../* The "w
96de0 73 64 53 74 61 74 22 20 6d 61 63 72 6f 20 77 69 sdStat" macro wi
96df0 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 ll resolve to th
96e00 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 e status informa
96e10 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20 76 65 tion.** state ve
96e20 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62 ctor. If writab
96e30 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 le static data i
96e40 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e s unsupported on
96e50 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20 the target,.**
96e60 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 we have to locat
96e70 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 e the state vect
96e80 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 or at run-time.
96e90 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d In the more com
96ea0 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 mon.** case wher
96eb0 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 e writable stati
96ec0 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72 c data is suppor
96ed0 74 65 64 2c 20 77 73 64 53 74 61 74 20 63 61 6e ted, wsdStat can
96ee0 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a refer directly.
96ef0 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 ** to the "sqlit
96f00 65 33 53 74 61 74 22 20 73 74 61 74 65 20 76 65 e3Stat" state ve
96f10 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 ctor declared ab
96f20 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ove..*/.#ifdef S
96f30 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 QLITE_OMIT_WSD.#
96f40 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 49 define wsdStatI
96f50 6e 69 74 20 20 73 71 6c 69 74 65 33 53 74 61 74 nit sqlite3Stat
96f60 54 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f 42 41 Type *x = &GLOBA
96f70 4c 28 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 L(sqlite3StatTyp
96f80 65 2c 73 71 6c 69 74 65 33 53 74 61 74 29 0a 23 e,sqlite3Stat).#
96f90 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 20 define wsdStat
96fa0 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 x[0].#else.# def
96fb0 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69 74 0a ine wsdStatInit.
96fc0 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 # define wsdStat
96fd0 20 73 71 6c 69 74 65 33 53 74 61 74 0a 23 65 6e sqlite3Stat.#en
96fe0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 dif../*.** Retur
96ff0 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 n the current va
97000 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 lue of a status
97010 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 53 51 parameter..*/.SQ
97020 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
97030 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 sqlite3StatusVa
97040 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20 20 77 lue(int op){. w
97050 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 sdStatInit;. as
97060 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f sert( op>=0 && o
97070 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 p<ArraySize(wsdS
97080 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b tat.nowValue) );
97090 0a 20 20 72 65 74 75 72 6e 20 77 73 64 53 74 61 . return wsdSta
970a0 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a t.nowValue[op];.
970b0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e 20 74 }../*.** Add N t
970c0 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 o the value of a
970d0 20 73 74 61 74 75 73 20 72 65 63 6f 72 64 2e 20 status record.
970e0 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 It is assumed t
970f0 68 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 hat the.** calle
97100 72 20 68 6f 6c 64 73 20 61 70 70 72 6f 70 72 69 r holds appropri
97110 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 53 51 ate locks..*/.SQ
97120 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
97130 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 d sqlite3StatusA
97140 64 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 4e dd(int op, int N
97150 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 ){. wsdStatInit
97160 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d ;. assert( op>=
97170 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 0 && op<ArraySiz
97180 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c e(wsdStat.nowVal
97190 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74 61 74 ue) );. wsdStat
971a0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 2b 3d .nowValue[op] +=
971b0 20 4e 3b 0a 20 20 69 66 28 20 77 73 64 53 74 61 N;. if( wsdSta
971c0 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 t.nowValue[op]>w
971d0 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f sdStat.mxValue[o
971e0 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 p] ){. wsdSta
971f0 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 t.mxValue[op] =
97200 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
97210 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a [op];. }.}../*.
97220 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75 65 ** Set the value
97230 20 6f 66 20 61 20 73 74 61 74 75 73 20 74 6f 20 of a status to
97240 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 X..*/.SQLITE_PRI
97250 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
97260 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 20 6f 3StatusSet(int o
97270 70 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77 73 64 p, int X){. wsd
97280 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 StatInit;. asse
97290 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c rt( op>=0 && op<
972a0 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 ArraySize(wsdSta
972b0 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 t.nowValue) );.
972c0 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 wsdStat.nowValu
972d0 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69 66 28 e[op] = X;. if(
972e0 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 wsdStat.nowValu
972f0 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 e[op]>wsdStat.mx
97300 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 Value[op] ){.
97310 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 wsdStat.mxValue
97320 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e [op] = wsdStat.n
97330 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d owValue[op];. }
97340 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 .}../*.** Query
97350 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 status informati
97360 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 on..**.** This i
97370 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 mplementation as
97380 73 75 6d 65 73 20 74 68 61 74 20 72 65 61 64 69 sumes that readi
97390 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 61 6e ng or writing an
973a0 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32 2d 62 aligned.** 32-b
973b0 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 61 6e it integer is an
973c0 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 69 6f atomic operatio
973d0 6e 2e 20 20 49 66 20 74 68 61 74 20 61 73 73 75 n. If that assu
973e0 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20 74 72 mption is not tr
973f0 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 ue,.** then this
97400 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 20 routine is not
97410 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53 threadsafe..*/.S
97420 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
97430 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 6e 74 lite3_status(int
97440 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 72 65 op, int *pCurre
97450 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 77 61 nt, int *pHighwa
97460 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 46 6c ter, int resetFl
97470 61 67 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e ag){. wsdStatIn
97480 69 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30 20 7c it;. if( op<0 |
97490 7c 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a 65 28 | op>=ArraySize(
974a0 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
974b0 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
974c0 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b SQLITE_MISUSE_BK
974d0 50 54 3b 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72 PT;. }. *pCurr
974e0 65 6e 74 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f ent = wsdStat.no
974f0 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 wValue[op];. *p
97500 48 69 67 68 77 61 74 65 72 20 3d 20 77 73 64 53 Highwater = wsdS
97510 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b tat.mxValue[op];
97520 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c 61 67 . if( resetFlag
97530 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e ){. wsdStat.
97540 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 mxValue[op] = ws
97550 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f dStat.nowValue[o
97560 70 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e p];. }. return
97570 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
97580 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74 75 *.** Query statu
97590 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f s information fo
975a0 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 r a single datab
975b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a ase connection.*
975c0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
975d0 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 sqlite3_db_stat
975e0 75 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 us(. sqlite3 *d
975f0 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 b, /* T
97600 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
97610 65 63 74 69 6f 6e 20 77 68 6f 73 65 20 73 74 61 ection whose sta
97620 74 75 73 20 69 73 20 64 65 73 69 72 65 64 20 2a tus is desired *
97630 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 /. int op,
97640 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
97650 74 75 73 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e tus verb */. in
97660 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 20 20 20 t *pCurrent,
97670 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 75 72 /* Write cur
97680 72 65 6e 74 20 76 61 6c 75 65 20 68 65 72 65 20 rent value here
97690 2a 2f 0a 20 20 69 6e 74 20 2a 70 48 69 67 68 77 */. int *pHighw
976a0 61 74 65 72 2c 20 20 20 20 20 20 2f 2a 20 57 72 ater, /* Wr
976b0 69 74 65 20 68 69 67 68 2d 77 61 74 65 72 20 6d ite high-water m
976c0 61 72 6b 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e ark here */. in
976d0 74 20 72 65 73 65 74 46 6c 61 67 20 20 20 20 20 t resetFlag
976e0 20 20 20 20 2f 2a 20 52 65 73 65 74 20 68 69 67 /* Reset hig
976f0 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 66 20 h-water mark if
97700 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 true */.){. int
97710 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
97720 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
97730 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d e */. sqlite3_m
97740 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d utex_enter(db->m
97750 75 74 65 78 29 3b 0a 20 20 73 77 69 74 63 68 28 utex);. switch(
97760 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 op ){. case
97770 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f SQLITE_DBSTATUS_
97780 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a 20 LOOKASIDE_USED:
97790 7b 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65 6e {. *pCurren
977a0 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 t = db->lookasid
977b0 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a 70 e.nOut;. *p
977c0 48 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d 3e Highwater = db->
977d0 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 3b lookaside.mxOut;
977e0 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 65 74 . if( reset
977f0 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 Flag ){.
97800 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 db->lookaside.mx
97810 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 Out = db->lookas
97820 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 ide.nOut;.
97830 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
97840 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 53 }.. case S
97850 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c QLITE_DBSTATUS_L
97860 4f 4f 4b 41 53 49 44 45 5f 48 49 54 3a 0a 20 20 OOKASIDE_HIT:.
97870 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42 case SQLITE_DB
97880 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 STATUS_LOOKASIDE
97890 5f 4d 49 53 53 5f 53 49 5a 45 3a 0a 20 20 20 20 _MISS_SIZE:.
978a0 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 case SQLITE_DBST
978b0 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 4d ATUS_LOOKASIDE_M
978c0 49 53 53 5f 46 55 4c 4c 3a 20 7b 0a 20 20 20 20 ISS_FULL: {.
978d0 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
978e0 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f SQLITE_DBSTATUS_
978f0 4c 4f 4f 4b 41 53 49 44 45 5f 48 49 54 20 29 3b LOOKASIDE_HIT );
97900 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
97910 20 6f 70 3d 3d 53 51 4c 49 54 45 5f 44 42 53 54 op==SQLITE_DBST
97920 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 4d ATUS_LOOKASIDE_M
97930 49 53 53 5f 53 49 5a 45 20 29 3b 0a 20 20 20 20 ISS_SIZE );.
97940 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
97950 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f SQLITE_DBSTATUS_
97960 4c 4f 4f 4b 41 53 49 44 45 5f 4d 49 53 53 5f 46 LOOKASIDE_MISS_F
97970 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 ULL );. ass
97980 65 72 74 28 20 28 6f 70 2d 53 51 4c 49 54 45 5f ert( (op-SQLITE_
97990 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 DBSTATUS_LOOKASI
979a0 44 45 5f 48 49 54 29 3e 3d 30 20 29 3b 0a 20 20 DE_HIT)>=0 );.
979b0 20 20 20 20 61 73 73 65 72 74 28 20 28 6f 70 2d assert( (op-
979c0 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f SQLITE_DBSTATUS_
979d0 4c 4f 4f 4b 41 53 49 44 45 5f 48 49 54 29 3c 33 LOOKASIDE_HIT)<3
979e0 20 29 3b 0a 20 20 20 20 20 20 2a 70 43 75 72 72 );. *pCurr
979f0 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 2a ent = 0;. *
97a00 70 48 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d pHighwater = db-
97a10 3e 6c 6f 6f 6b 61 73 69 64 65 2e 61 6e 53 74 61 >lookaside.anSta
97a20 74 5b 6f 70 20 2d 20 53 51 4c 49 54 45 5f 44 42 t[op - SQLITE_DB
97a30 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 STATUS_LOOKASIDE
97a40 5f 48 49 54 5d 3b 0a 20 20 20 20 20 20 69 66 28 _HIT];. if(
97a50 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 resetFlag ){.
97a60 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 db->lookas
97a70 69 64 65 2e 61 6e 53 74 61 74 5b 6f 70 20 2d 20 ide.anStat[op -
97a80 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f SQLITE_DBSTATUS_
97a90 4c 4f 4f 4b 41 53 49 44 45 5f 48 49 54 5d 20 3d LOOKASIDE_HIT] =
97aa0 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
97ab0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a break;. }..
97ac0 20 20 20 20 2f 2a 20 0a 20 20 20 20 2a 2a 20 52 /* . ** R
97ad0 65 74 75 72 6e 20 61 6e 20 61 70 70 72 6f 78 69 eturn an approxi
97ae0 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 61 mation for the a
97af0 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 mount of memory
97b00 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 0a 20 currently used.
97b10 20 20 20 2a 2a 20 62 79 20 61 6c 6c 20 70 61 67 ** by all pag
97b20 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ers associated w
97b30 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 64 61 ith the given da
97b40 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
97b50 6e 2e 20 20 54 68 65 0a 20 20 20 20 2a 2a 20 68 n. The. ** h
97b60 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 69 73 ighwater mark is
97b70 20 6d 65 61 6e 69 6e 67 6c 65 73 73 20 61 6e 64 meaningless and
97b80 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 is returned as
97b90 7a 65 72 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 zero.. */.
97ba0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 case SQLITE_DBS
97bb0 54 41 54 55 53 5f 43 41 43 48 45 5f 55 53 45 44 TATUS_CACHE_USED
97bc0 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 6f : {. int to
97bd0 74 61 6c 55 73 65 64 20 3d 20 30 3b 0a 20 20 20 talUsed = 0;.
97be0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
97bf0 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
97c00 72 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 rAll(db);.
97c10 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e for(i=0; i<db->n
97c20 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 Db; i++){.
97c30 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 Btree *pBt = d
97c40 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
97c50 20 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 if( pBt )
97c60 7b 0a 20 20 20 20 20 20 20 20 20 20 50 61 67 65 {. Page
97c70 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69 r *pPager = sqli
97c80 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 42 te3BtreePager(pB
97c90 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 74 6f t);. to
97ca0 74 61 6c 55 73 65 64 20 2b 3d 20 73 71 6c 69 74 talUsed += sqlit
97cb0 65 33 50 61 67 65 72 4d 65 6d 55 73 65 64 28 70 e3PagerMemUsed(p
97cc0 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 Pager);.
97cd0 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
97ce0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
97cf0 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 eAll(db);.
97d00 2a 70 43 75 72 72 65 6e 74 20 3d 20 74 6f 74 61 *pCurrent = tota
97d10 6c 55 73 65 64 3b 0a 20 20 20 20 20 20 2a 70 48 lUsed;. *pH
97d20 69 67 68 77 61 74 65 72 20 3d 20 30 3b 0a 20 20 ighwater = 0;.
97d30 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
97d40 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 .. /*. **
97d50 2a 70 43 75 72 72 65 6e 74 20 67 65 74 73 20 61 *pCurrent gets a
97d60 6e 20 61 63 63 75 72 61 74 65 20 65 73 74 69 6d n accurate estim
97d70 61 74 65 20 6f 66 20 74 68 65 20 61 6d 6f 75 6e ate of the amoun
97d80 74 20 6f 66 20 6d 65 6d 6f 72 79 20 75 73 65 64 t of memory used
97d90 0a 20 20 20 20 2a 2a 20 74 6f 20 73 74 6f 72 65 . ** to store
97da0 20 74 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20 the schema for
97db0 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 28 6d all databases (m
97dc0 61 69 6e 2c 20 74 65 6d 70 2c 20 61 6e 64 20 61 ain, temp, and a
97dd0 6e 79 20 41 54 54 41 43 48 65 64 0a 20 20 20 20 ny ATTACHed.
97de0 2a 2a 20 64 61 74 61 62 61 73 65 73 2e 20 20 2a ** databases. *
97df0 70 48 69 67 68 77 61 74 65 72 20 69 73 20 73 65 pHighwater is se
97e00 74 20 74 6f 20 7a 65 72 6f 2e 0a 20 20 20 20 2a t to zero.. *
97e10 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 /. case SQLIT
97e20 45 5f 44 42 53 54 41 54 55 53 5f 53 43 48 45 4d E_DBSTATUS_SCHEM
97e30 41 5f 55 53 45 44 3a 20 7b 0a 20 20 20 20 20 20 A_USED: {.
97e40 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
97e50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
97e60 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 sed to iterate t
97e70 68 72 6f 75 67 68 20 73 63 68 65 6d 61 73 20 2a hrough schemas *
97e80 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74 /. int nByt
97e90 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 e = 0;
97ea0 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 61 /* Used to a
97eb0 63 63 75 6d 75 6c 61 74 65 20 72 65 74 75 72 6e ccumulate return
97ec0 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 20 20 20 value */..
97ed0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
97ee0 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 erAll(db);.
97ef0 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 65 db->pnBytesFree
97f00 64 20 3d 20 26 6e 42 79 74 65 3b 0a 20 20 20 20 d = &nByte;.
97f10 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d for(i=0; i<db-
97f20 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nDb; i++){.
97f30 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 Schema *pSch
97f40 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d ema = db->aDb[i]
97f50 2e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 .pSchema;.
97f60 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 53 63 if( ALWAYS(pSc
97f70 68 65 6d 61 21 3d 30 29 20 29 7b 0a 20 20 20 20 hema!=0) ){.
97f80 20 20 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a HashElem *
97f90 70 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 6e 42 p;.. nB
97fa0 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 47 6c yte += sqlite3Gl
97fb0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f obalConfig.m.xRo
97fc0 75 6e 64 75 70 28 73 69 7a 65 6f 66 28 48 61 73 undup(sizeof(Has
97fd0 68 45 6c 65 6d 29 29 20 2a 20 28 0a 20 20 20 20 hElem)) * (.
97fe0 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d pSchem
97ff0 61 2d 3e 74 62 6c 48 61 73 68 2e 63 6f 75 6e 74 a->tblHash.count
98000 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 . +
98010 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 pSchema->trigHas
98020 68 2e 63 6f 75 6e 74 0a 20 20 20 20 20 20 20 20 h.count.
98030 20 20 20 20 2b 20 70 53 63 68 65 6d 61 2d 3e 69 + pSchema->i
98040 64 78 48 61 73 68 2e 63 6f 75 6e 74 0a 20 20 20 dxHash.count.
98050 20 20 20 20 20 20 20 20 20 2b 20 70 53 63 68 65 + pSche
98060 6d 61 2d 3e 66 6b 65 79 48 61 73 68 2e 63 6f 75 ma->fkeyHash.cou
98070 6e 74 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a nt. );.
98080 20 20 20 20 20 20 20 20 20 20 6e 42 79 74 65 20 nByte
98090 2b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 += sqlite3Malloc
980a0 53 69 7a 65 28 70 53 63 68 65 6d 61 2d 3e 74 62 Size(pSchema->tb
980b0 6c 48 61 73 68 2e 68 74 29 3b 0a 20 20 20 20 20 lHash.ht);.
980c0 20 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 71 nByte += sq
980d0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 lite3MallocSize(
980e0 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 pSchema->trigHas
980f0 68 2e 68 74 29 3b 0a 20 20 20 20 20 20 20 20 20 h.ht);.
98100 20 6e 42 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 nByte += sqlite
98110 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 53 63 68 3MallocSize(pSch
98120 65 6d 61 2d 3e 69 64 78 48 61 73 68 2e 68 74 29 ema->idxHash.ht)
98130 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 42 79 74 ;. nByt
98140 65 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c e += sqlite3Mall
98150 6f 63 53 69 7a 65 28 70 53 63 68 65 6d 61 2d 3e ocSize(pSchema->
98160 66 6b 65 79 48 61 73 68 2e 68 74 29 3b 0a 0a 20 fkeyHash.ht);..
98170 20 20 20 20 20 20 20 20 20 66 6f 72 28 70 3d 73 for(p=s
98180 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 qliteHashFirst(&
98190 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 pSchema->trigHas
981a0 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 h); p; p=sqliteH
981b0 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20 ashNext(p)){.
981c0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
981d0 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 DeleteTrigger(db
981e0 2c 20 28 54 72 69 67 67 65 72 2a 29 73 71 6c 69 , (Trigger*)sqli
981f0 74 65 48 61 73 68 44 61 74 61 28 70 29 29 3b 0a teHashData(p));.
98200 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
98210 20 20 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 for(p=sqli
98220 74 65 48 61 73 68 46 69 72 73 74 28 26 70 53 63 teHashFirst(&pSc
98230 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 hema->tblHash);
98240 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e p; p=sqliteHashN
98250 65 78 74 28 70 29 29 7b 0a 20 20 20 20 20 20 20 ext(p)){.
98260 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 sqlite3Dele
98270 74 65 54 61 62 6c 65 28 64 62 2c 20 28 54 61 62 teTable(db, (Tab
98280 6c 65 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44 le *)sqliteHashD
98290 61 74 61 28 70 29 29 3b 0a 20 20 20 20 20 20 20 ata(p));.
982a0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
982b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 62 2d }. db-
982c0 3e 70 6e 42 79 74 65 73 46 72 65 65 64 20 3d 20 >pnBytesFreed =
982d0 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 0;. sqlite3
982e0 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 BtreeLeaveAll(db
982f0 29 3b 0a 0a 20 20 20 20 20 20 2a 70 48 69 67 68 );.. *pHigh
98300 77 61 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 water = 0;.
98310 20 2a 70 43 75 72 72 65 6e 74 20 3d 20 6e 42 79 *pCurrent = nBy
98320 74 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b te;. break;
98330 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 . }.. /*.
98340 20 20 20 2a 2a 20 2a 70 43 75 72 72 65 6e 74 20 ** *pCurrent
98350 67 65 74 73 20 61 6e 20 61 63 63 75 72 61 74 65 gets an accurate
98360 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 estimate of the
98370 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 amount of memor
98380 79 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 74 6f y used. ** to
98390 20 73 74 6f 72 65 20 61 6c 6c 20 70 72 65 70 61 store all prepa
983a0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a red statements..
983b0 20 20 20 20 2a 2a 20 2a 70 48 69 67 68 77 61 74 ** *pHighwat
983c0 65 72 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 er is set to zer
983d0 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 o.. */. ca
983e0 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 se SQLITE_DBSTAT
983f0 55 53 5f 53 54 4d 54 5f 55 53 45 44 3a 20 7b 0a US_STMT_USED: {.
98400 20 20 20 20 20 20 73 74 72 75 63 74 20 56 64 62 struct Vdb
98410 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 e *pVdbe;
98420 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 /* Used to ite
98430 72 61 74 65 20 74 68 72 6f 75 67 68 20 56 4d 73 rate through VMs
98440 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 */. int nB
98450 79 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 yte = 0;
98460 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f /* Used to
98470 20 61 63 63 75 6d 75 6c 61 74 65 20 72 65 74 75 accumulate retu
98480 72 6e 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 20 rn value */..
98490 20 20 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 db->pnBytesFr
984a0 65 65 64 20 3d 20 26 6e 42 79 74 65 3b 0a 20 20 eed = &nByte;.
984b0 20 20 20 20 66 6f 72 28 70 56 64 62 65 3d 64 62 for(pVdbe=db
984c0 2d 3e 70 56 64 62 65 3b 20 70 56 64 62 65 3b 20 ->pVdbe; pVdbe;
984d0 70 56 64 62 65 3d 70 56 64 62 65 2d 3e 70 4e 65 pVdbe=pVdbe->pNe
984e0 78 74 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c xt){. sql
984f0 69 74 65 33 56 64 62 65 43 6c 65 61 72 4f 62 6a ite3VdbeClearObj
98500 65 63 74 28 64 62 2c 20 70 56 64 62 65 29 3b 0a ect(db, pVdbe);.
98510 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
98520 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 29 bFree(db, pVdbe)
98530 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
98540 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 65 64 db->pnBytesFreed
98550 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2a 70 48 = 0;.. *pH
98560 69 67 68 77 61 74 65 72 20 3d 20 30 3b 0a 20 20 ighwater = 0;.
98570 20 20 20 20 2a 70 43 75 72 72 65 6e 74 20 3d 20 *pCurrent =
98580 6e 42 79 74 65 3b 0a 0a 20 20 20 20 20 20 62 72 nByte;.. br
98590 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 eak;. }..
985a0 2f 2a 0a 20 20 20 20 2a 2a 20 53 65 74 20 2a 70 /*. ** Set *p
985b0 43 75 72 72 65 6e 74 20 74 6f 20 74 68 65 20 74 Current to the t
985c0 6f 74 61 6c 20 63 61 63 68 65 20 68 69 74 73 20 otal cache hits
985d0 6f 72 20 6d 69 73 73 65 73 20 65 6e 63 6f 75 6e or misses encoun
985e0 74 65 72 65 64 20 62 79 20 61 6c 6c 0a 20 20 20 tered by all.
985f0 20 2a 2a 20 70 61 67 65 72 73 20 74 68 65 20 64 ** pagers the d
98600 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 69 atabase handle i
98610 73 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f 2e 20 s connected to.
98620 2a 70 48 69 67 68 77 61 74 65 72 20 69 73 20 61 *pHighwater is a
98630 6c 77 61 79 73 20 73 65 74 20 0a 20 20 20 20 2a lways set . *
98640 2a 20 74 6f 20 7a 65 72 6f 2e 0a 20 20 20 20 2a * to zero.. *
98650 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 /. case SQLIT
98660 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 E_DBSTATUS_CACHE
98670 5f 48 49 54 3a 0a 20 20 20 20 63 61 73 65 20 53 _HIT:. case S
98680 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 QLITE_DBSTATUS_C
98690 41 43 48 45 5f 4d 49 53 53 3a 0a 20 20 20 20 63 ACHE_MISS:. c
986a0 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 ase SQLITE_DBSTA
986b0 54 55 53 5f 43 41 43 48 45 5f 57 52 49 54 45 3a TUS_CACHE_WRITE:
986c0 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 {. int i;.
986d0 20 20 20 20 20 69 6e 74 20 6e 52 65 74 20 3d 20 int nRet =
986e0 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 0;. assert(
986f0 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 SQLITE_DBSTATUS
98700 5f 43 41 43 48 45 5f 4d 49 53 53 3d 3d 53 51 4c _CACHE_MISS==SQL
98710 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43 ITE_DBSTATUS_CAC
98720 48 45 5f 48 49 54 2b 31 20 29 3b 0a 20 20 20 20 HE_HIT+1 );.
98730 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 assert( SQLITE
98740 5f 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f _DBSTATUS_CACHE_
98750 57 52 49 54 45 3d 3d 53 51 4c 49 54 45 5f 44 42 WRITE==SQLITE_DB
98760 53 54 41 54 55 53 5f 43 41 43 48 45 5f 48 49 54 STATUS_CACHE_HIT
98770 2b 32 20 29 3b 0a 0a 20 20 20 20 20 20 66 6f 72 +2 );.. for
98780 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b (i=0; i<db->nDb;
98790 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 i++){. i
987a0 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 f( db->aDb[i].pB
987b0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 t ){. P
987c0 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 ager *pPager = s
987d0 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 qlite3BtreePager
987e0 28 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 (db->aDb[i].pBt)
987f0 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
98800 74 65 33 50 61 67 65 72 43 61 63 68 65 53 74 61 te3PagerCacheSta
98810 74 28 70 50 61 67 65 72 2c 20 6f 70 2c 20 72 65 t(pPager, op, re
98820 73 65 74 46 6c 61 67 2c 20 26 6e 52 65 74 29 3b setFlag, &nRet);
98830 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
98840 20 7d 0a 20 20 20 20 20 20 2a 70 48 69 67 68 77 }. *pHighw
98850 61 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 ater = 0;.
98860 2a 70 43 75 72 72 65 6e 74 20 3d 20 6e 52 65 74 *pCurrent = nRet
98870 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
98880 20 20 20 7d 0a 0a 20 20 20 20 64 65 66 61 75 6c }.. defaul
98890 74 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 t: {. rc =
988a0 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
988b0 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 }. }. sqlite
988c0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 3_mutex_leave(db
988d0 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 ->mutex);. retu
988e0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a rn rc;.}../*****
988f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
98900 20 73 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a status.c ******
98910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98930 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
98940 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
98950 66 69 6c 65 20 64 61 74 65 2e 63 20 2a 2a 2a 2a file date.c ****
98960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98980 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
98990 32 30 30 33 20 4f 63 74 6f 62 65 72 20 33 31 0a 2003 October 31.
989a0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
989b0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
989c0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
989d0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
989e0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
989f0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
98a00 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
98a10 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
98a20 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
98a30 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
98a40 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
98a50 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
98a60 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
98a70 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
98a80 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
98a90 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
98aa0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
98ab0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
98ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
98b00 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
98b10 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 ains the C funct
98b20 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d ions that implem
98b30 65 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69 6d ent date and tim
98b40 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 66 e.** functions f
98b50 6f 72 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a or SQLite. .**.
98b60 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 6c 79 ** There is only
98b70 20 6f 6e 65 20 65 78 70 6f 72 74 65 64 20 73 79 one exported sy
98b80 6d 62 6f 6c 20 69 6e 20 74 68 69 73 20 66 69 6c mbol in this fil
98b90 65 20 2d 20 74 68 65 20 66 75 6e 63 74 69 6f 6e e - the function
98ba0 0a 2a 2a 20 73 71 6c 69 74 65 33 52 65 67 69 73 .** sqlite3Regis
98bb0 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 terDateTimeFunct
98bc0 69 6f 6e 73 28 29 20 66 6f 75 6e 64 20 61 74 20 ions() found at
98bd0 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 the bottom of th
98be0 65 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f e file..** All o
98bf0 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 66 69 ther code has fi
98c00 6c 65 20 73 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a 20 le scope..**.**
98c10 53 51 4c 69 74 65 20 70 72 6f 63 65 73 73 65 73 SQLite processes
98c20 20 61 6c 6c 20 74 69 6d 65 73 20 61 6e 64 20 64 all times and d
98c30 61 74 65 73 20 61 73 20 4a 75 6c 69 61 6e 20 44 ates as Julian D
98c40 61 79 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 ay numbers. The
98c50 0a 2a 2a 20 64 61 74 65 73 20 61 6e 64 20 74 69 .** dates and ti
98c60 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 61 mes are stored a
98c70 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
98c80 64 61 79 73 20 73 69 6e 63 65 20 6e 6f 6f 6e 0a days since noon.
98c90 2a 2a 20 69 6e 20 47 72 65 65 6e 77 69 63 68 20 ** in Greenwich
98ca0 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32 34 2c 20 on November 24,
98cb0 34 37 31 34 20 42 2e 43 2e 20 61 63 63 6f 72 64 4714 B.C. accord
98cc0 69 6e 67 20 74 6f 20 74 68 65 20 47 72 65 67 6f ing to the Grego
98cd0 72 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e 64 61 72 rian.** calendar
98ce0 20 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a 2a 20 system. .**.**
98cf0 31 39 37 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 1970-01-01 00:00
98d00 3a 30 30 20 69 73 20 4a 44 20 32 34 34 30 35 38 :00 is JD 244058
98d10 37 2e 35 0a 2a 2a 20 32 30 30 30 2d 30 31 2d 30 7.5.** 2000-01-0
98d20 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 1 00:00:00 is JD
98d30 20 32 34 35 31 35 34 34 2e 35 0a 2a 2a 0a 2a 2a 2451544.5.**.**
98d40 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 69 This implementi
98d50 6f 6e 20 72 65 71 75 69 72 65 73 20 79 65 61 72 on requires year
98d60 73 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 65 s to be expresse
98d70 64 20 61 73 20 61 20 34 2d 64 69 67 69 74 20 6e d as a 4-digit n
98d80 75 6d 62 65 72 0a 2a 2a 20 77 68 69 63 68 20 6d umber.** which m
98d90 65 61 6e 73 20 74 68 61 74 20 6f 6e 6c 79 20 64 eans that only d
98da0 61 74 65 73 20 62 65 74 77 65 65 6e 20 30 30 30 ates between 000
98db0 30 2d 30 31 2d 30 31 20 61 6e 64 20 39 39 39 39 0-01-01 and 9999
98dc0 2d 31 32 2d 33 31 20 63 61 6e 0a 2a 2a 20 62 65 -12-31 can.** be
98dd0 20 72 65 70 72 65 73 65 6e 74 65 64 2c 20 65 76 represented, ev
98de0 65 6e 20 74 68 6f 75 67 68 20 6a 75 6c 69 61 6e en though julian
98df0 20 64 61 79 20 6e 75 6d 62 65 72 73 20 61 6c 6c day numbers all
98e00 6f 77 20 61 20 6d 75 63 68 20 77 69 64 65 72 0a ow a much wider.
98e10 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74 65 ** range of date
98e20 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 47 72 65 s..**.** The Gre
98e30 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 gorian calendar
98e40 73 79 73 74 65 6d 20 69 73 20 75 73 65 64 20 66 system is used f
98e50 6f 72 20 61 6c 6c 20 64 61 74 65 73 20 61 6e 64 or all dates and
98e60 20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 times,.** even
98e70 74 68 6f 73 65 20 74 68 61 74 20 70 72 65 64 61 those that preda
98e80 74 65 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e te the Gregorian
98e90 20 63 61 6c 65 6e 64 61 72 2e 20 20 48 69 73 74 calendar. Hist
98ea0 6f 72 69 61 6e 73 20 75 73 75 61 6c 6c 79 0a 2a orians usually.*
98eb0 2a 20 75 73 65 20 74 68 65 20 4a 75 6c 69 61 6e * use the Julian
98ec0 20 63 61 6c 65 6e 64 61 72 20 66 6f 72 20 64 61 calendar for da
98ed0 74 65 73 20 70 72 69 6f 72 20 74 6f 20 31 35 38 tes prior to 158
98ee0 32 2d 31 30 2d 31 35 20 61 6e 64 20 66 6f 72 20 2-10-15 and for
98ef0 73 6f 6d 65 0a 2a 2a 20 64 61 74 65 73 20 61 66 some.** dates af
98f00 74 65 72 77 61 72 64 73 2c 20 64 65 70 65 6e 64 terwards, depend
98f10 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e 20 20 ing on locale.
98f20 42 65 77 61 72 65 20 6f 66 20 74 68 69 73 20 64 Beware of this d
98f30 69 66 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a ifference..**.**
98f40 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 The conversion
98f50 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 20 69 algorithms are i
98f60 6d 70 6c 65 6d 65 6e 74 65 64 20 62 61 73 65 64 mplemented based
98f70 20 6f 6e 20 64 65 73 63 72 69 70 74 69 6f 6e 73 on descriptions
98f80 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f .** in the follo
98f90 77 69 6e 67 20 74 65 78 74 3a 0a 2a 2a 0a 2a 2a wing text:.**.**
98fa0 20 20 20 20 20 20 4a 65 61 6e 20 4d 65 65 75 73 Jean Meeus
98fb0 0a 2a 2a 20 20 20 20 20 20 41 73 74 72 6f 6e 6f .** Astrono
98fc0 6d 69 63 61 6c 20 41 6c 67 6f 72 69 74 68 6d 73 mical Algorithms
98fd0 2c 20 32 6e 64 20 45 64 69 74 69 6f 6e 2c 20 31 , 2nd Edition, 1
98fe0 39 39 38 0a 2a 2a 20 20 20 20 20 20 49 53 42 4d 998.** ISBM
98ff0 20 30 2d 39 34 33 33 39 36 2d 36 31 2d 31 0a 2a 0-943396-61-1.*
99000 2a 20 20 20 20 20 20 57 69 6c 6c 6d 61 6e 6e 2d * Willmann-
99010 42 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 20 20 20 Bell, Inc.**
99020 20 20 52 69 63 68 6d 6f 6e 64 2c 20 56 69 72 67 Richmond, Virg
99030 69 6e 69 61 20 28 55 53 41 29 0a 2a 2f 0a 2f 2a inia (USA).*/./*
99040 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 #include <stdli
99050 62 2e 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c b.h> */./* #incl
99060 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a ude <assert.h> *
99070 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65 /.#include <time
99080 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c .h>..#ifndef SQL
99090 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d ITE_OMIT_DATETIM
990a0 45 5f 46 55 4e 43 53 0a 0a 0a 2f 2a 0a 2a 2a 20 E_FUNCS.../*.**
990b0 41 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 A structure for
990c0 68 6f 6c 64 69 6e 67 20 61 20 73 69 6e 67 6c 65 holding a single
990d0 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a date and time..
990e0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
990f0 74 20 44 61 74 65 54 69 6d 65 20 44 61 74 65 54 t DateTime DateT
99100 69 6d 65 3b 0a 73 74 72 75 63 74 20 44 61 74 65 ime;.struct Date
99110 54 69 6d 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 Time {. sqlite3
99120 5f 69 6e 74 36 34 20 69 4a 44 3b 20 2f 2a 20 54 _int64 iJD; /* T
99130 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 he julian day nu
99140 6d 62 65 72 20 74 69 6d 65 73 20 38 36 34 30 30 mber times 86400
99150 30 30 30 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 000 */. int Y,
99160 4d 2c 20 44 3b 20 20 20 20 20 20 20 2f 2a 20 59 M, D; /* Y
99170 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 ear, month, and
99180 64 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 day */. int h,
99190 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 m; /* H
991a0 6f 75 72 20 61 6e 64 20 6d 69 6e 75 74 65 73 20 our and minutes
991b0 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 20 20 20 */. int tz;
991c0 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a /* Timez
991d0 6f 6e 65 20 6f 66 66 73 65 74 20 69 6e 20 6d 69 one offset in mi
991e0 6e 75 74 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c nutes */. doubl
991f0 65 20 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a e s; /*
99200 20 53 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 Seconds */. ch
99210 61 72 20 76 61 6c 69 64 59 4d 44 3b 20 20 20 20 ar validYMD;
99220 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 /* True (1) if
99230 59 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20 Y,M,D are valid
99240 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48 */. char validH
99250 4d 53 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 MS; /* True
99260 28 31 29 20 69 66 20 68 2c 6d 2c 73 20 61 72 65 (1) if h,m,s are
99270 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 valid */. char
99280 20 76 61 6c 69 64 4a 44 3b 20 20 20 20 20 20 2f validJD; /
99290 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 69 4a * True (1) if iJ
992a0 44 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 D is valid */.
992b0 63 68 61 72 20 76 61 6c 69 64 54 5a 3b 20 20 20 char validTZ;
992c0 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 /* True (1) i
992d0 66 20 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f f tz is valid */
992e0 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 .};.../*.** Conv
992f0 65 72 74 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f ert zDate into o
99300 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67 ne or more integ
99310 65 72 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c ers. Additional
99320 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f arguments.** co
99330 6d 65 20 69 6e 20 67 72 6f 75 70 73 20 6f 66 20 me in groups of
99340 35 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 5 as follows:.**
99350 0a 2a 2a 20 20 20 20 20 20 20 4e 20 20 20 20 20 .** N
99360 20 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69 number of digi
99370 74 73 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65 ts in the intege
99380 72 0a 2a 2a 20 20 20 20 20 20 20 6d 69 6e 20 20 r.** min
99390 20 20 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 minimum allow
993a0 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 ed value of the
993b0 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 integer.**
993c0 20 6d 61 78 20 20 20 20 20 6d 61 78 69 6d 75 6d max maximum
993d0 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f allowed value o
993e0 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a f the integer.**
993f0 20 20 20 20 20 20 20 6e 65 78 74 43 20 20 20 66 nextC f
99400 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 61 irst character a
99410 66 74 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 fter the integer
99420 0a 2a 2a 20 20 20 20 20 20 20 70 56 61 6c 20 20 .** pVal
99430 20 20 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 where to write
99440 20 74 68 65 20 69 6e 74 65 67 65 72 73 20 76 61 the integers va
99450 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 lue..**.** Conve
99460 72 73 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 rsions continue
99470 75 6e 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e until one with n
99480 65 78 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 extC==0 is encou
99490 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 ntered..** The f
994a0 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
994b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 the number of su
994c0 63 63 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 ccessful convers
994d0 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ions..*/.static
994e0 69 6e 74 20 67 65 74 44 69 67 69 74 73 28 63 6f int getDigits(co
994f0 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c nst char *zDate,
99500 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 ...){. va_list
99510 20 61 70 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a ap;. int val;.
99520 20 20 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d int N;. int m
99530 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20 in;. int max;.
99540 20 69 6e 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e int nextC;. in
99550 74 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63 t *pVal;. int c
99560 6e 74 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 nt = 0;. va_sta
99570 72 74 28 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20 rt(ap, zDate);.
99580 20 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f do{. N = va_
99590 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 arg(ap, int);.
995a0 20 20 6d 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 min = va_arg(a
995b0 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78 p, int);. max
995c0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e = va_arg(ap, in
995d0 74 29 3b 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 t);. nextC =
995e0 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b va_arg(ap, int);
995f0 0a 20 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61 . pVal = va_a
99600 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 rg(ap, int*);.
99610 20 20 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77 val = 0;. w
99620 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 hile( N-- ){.
99630 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 if( !sqlite3I
99640 73 64 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29 sdigit(*zDate) )
99650 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 {. goto e
99660 6e 64 5f 67 65 74 44 69 67 69 74 73 3b 0a 20 20 nd_getDigits;.
99670 20 20 20 20 7d 0a 20 20 20 20 20 20 76 61 6c 20 }. val
99680 3d 20 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 = val*10 + *zDat
99690 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a e - '0';. z
996a0 44 61 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 Date++;. }.
996b0 20 20 69 66 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c if( val<min ||
996c0 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 val>max || (nex
996d0 74 43 21 3d 30 20 26 26 20 6e 65 78 74 43 21 3d tC!=0 && nextC!=
996e0 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 *zDate) ){.
996f0 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 goto end_getDig
99700 69 74 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a its;. }. *
99710 70 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 20 20 pVal = val;.
99720 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 zDate++;. cnt
99730 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e 65 ++;. }while( ne
99740 78 74 43 20 29 3b 0a 65 6e 64 5f 67 65 74 44 69 xtC );.end_getDi
99750 67 69 74 73 3a 0a 20 20 76 61 5f 65 6e 64 28 61 gits:. va_end(a
99760 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6e 74 p);. return cnt
99770 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 ;.}../*.** Parse
99780 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 78 74 65 a timezone exte
99790 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 65 6e 64 nsion on the end
997a0 20 6f 66 20 61 20 64 61 74 65 2d 74 69 6d 65 2e of a date-time.
997b0 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e 73 69 6f .** The extensio
997c0 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d n is of the form
997d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 28 :.**.** (
997e0 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 +/-)HH:MM.**.**
997f0 4f 72 20 74 68 65 20 22 7a 75 6c 75 22 20 6e 6f Or the "zulu" no
99800 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 tation:.**.**
99810 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 Z.**.** If
99820 74 68 65 20 70 61 72 73 65 20 69 73 20 73 75 63 the parse is suc
99830 63 65 73 73 66 75 6c 2c 20 77 72 69 74 65 20 74 cessful, write t
99840 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e he number of min
99850 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e 67 utes.** of chang
99860 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 72 e in p->tz and r
99870 65 74 75 72 6e 20 30 2e 20 20 49 66 20 61 20 70 eturn 0. If a p
99880 61 72 73 65 72 20 65 72 72 6f 72 20 6f 63 63 75 arser error occu
99890 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f rs,.** return no
998a0 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 n-zero..**.** A
998b0 6d 69 73 73 69 6e 67 20 73 70 65 63 69 66 69 65 missing specifie
998c0 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 r is not conside
998d0 72 65 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f red an error..*/
998e0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 .static int pars
998f0 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 20 eTimezone(const
99900 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 char *zDate, Dat
99910 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 eTime *p){. int
99920 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 20 sgn = 0;. int
99930 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 20 nHr, nMn;. int
99940 63 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 c;. while( sqli
99950 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74 te3Isspace(*zDat
99960 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d e) ){ zDate++; }
99970 0a 20 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 20 . p->tz = 0;.
99980 63 20 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 69 66 c = *zDate;. if
99990 28 20 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 ( c=='-' ){.
999a0 73 67 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 sgn = -1;. }els
999b0 65 20 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b 0a e if( c=='+' ){.
999c0 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 20 sgn = +1;.
999d0 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a 27 }else if( c=='Z'
999e0 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a 20 20 || c=='z' ){.
999f0 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 67 zDate++;. g
99a00 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a 20 oto zulu_time;.
99a10 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
99a20 72 6e 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 20 7a rn c!=0;. }. z
99a30 44 61 74 65 2b 2b 3b 0a 20 20 69 66 28 20 67 65 Date++;. if( ge
99a40 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 tDigits(zDate, 2
99a50 2c 20 30 2c 20 31 34 2c 20 27 3a 27 2c 20 26 6e , 0, 14, ':', &n
99a60 48 72 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c Hr, 2, 0, 59, 0,
99a70 20 26 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 20 &nMn)!=2 ){.
99a80 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
99a90 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 70 zDate += 5;. p
99aa0 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e 20 ->tz = sgn*(nMn
99ab0 2b 20 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c 75 5f + nHr*60);.zulu_
99ac0 74 69 6d 65 3a 0a 20 20 77 68 69 6c 65 28 20 73 time:. while( s
99ad0 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a qlite3Isspace(*z
99ae0 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b Date) ){ zDate++
99af0 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 44 ; }. return *zD
99b00 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ate!=0;.}../*.**
99b10 20 50 61 72 73 65 20 74 69 6d 65 73 20 6f 66 20 Parse times of
99b20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d 4d 20 6f the form HH:MM o
99b30 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72 20 48 48 r HH:MM:SS or HH
99b40 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a 20 :MM:SS.FFFF..**
99b50 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61 6e 64 20 The HH, MM, and
99b60 53 53 20 6d 75 73 74 20 65 61 63 68 20 62 65 20 SS must each be
99b70 65 78 61 63 74 6c 79 20 32 20 64 69 67 69 74 73 exactly 2 digits
99b80 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61 63 74 69 . The.** fracti
99b90 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 46 46 46 onal seconds FFF
99ba0 46 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20 F can be one or
99bb0 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a 2a 2a 0a more digits..**.
99bc0 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 ** Return 1 if t
99bd0 68 65 72 65 20 69 73 20 61 20 70 61 72 73 69 6e here is a parsin
99be0 67 20 65 72 72 6f 72 20 61 6e 64 20 30 20 6f 6e g error and 0 on
99bf0 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 success..*/.sta
99c00 74 69 63 20 69 6e 74 20 70 61 72 73 65 48 68 4d tic int parseHhM
99c10 6d 53 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a mSs(const char *
99c20 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 zDate, DateTime
99c30 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c 20 6d 2c *p){. int h, m,
99c40 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20 6d 73 20 s;. double ms
99c50 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20 67 65 74 = 0.0;. if( get
99c60 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c Digits(zDate, 2,
99c70 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20 26 68 2c 0, 24, ':', &h,
99c80 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6d 2, 0, 59, 0, &m
99c90 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 )!=2 ){. retu
99ca0 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 rn 1;. }. zDat
99cb0 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28 20 2a 7a e += 5;. if( *z
99cc0 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 Date==':' ){.
99cd0 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 69 66 zDate++;. if
99ce0 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 ( getDigits(zDat
99cf0 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 e, 2, 0, 59, 0,
99d00 26 73 29 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 &s)!=1 ){.
99d10 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
99d20 20 20 20 20 7a 44 61 74 65 20 2b 3d 20 32 3b 0a zDate += 2;.
99d30 20 20 20 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d if( *zDate==
99d40 27 2e 27 20 26 26 20 73 71 6c 69 74 65 33 49 73 '.' && sqlite3Is
99d50 64 69 67 69 74 28 7a 44 61 74 65 5b 31 5d 29 20 digit(zDate[1])
99d60 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 ){. double
99d70 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 rScale = 1.0;.
99d80 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 zDate++;.
99d90 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 while( sqlite
99da0 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65 29 3Isdigit(*zDate)
99db0 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73 20 3d ){. ms =
99dc0 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 61 74 ms*10.0 + *zDat
99dd0 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 e - '0';.
99de0 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e 30 3b rScale *= 10.0;
99df0 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65 2b 2b . zDate++
99e00 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
99e10 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a 20 20 ms /= rScale;.
99e20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
99e30 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 2d s = 0;. }. p-
99e40 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 >validJD = 0;.
99e50 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b p->validHMS = 1;
99e60 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 . p->h = h;. p
99e70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73 20 ->m = m;. p->s
99e80 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66 28 20 = s + ms;. if(
99e90 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 7a 44 parseTimezone(zD
99ea0 61 74 65 2c 20 70 29 20 29 20 72 65 74 75 72 6e ate, p) ) return
99eb0 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a 1;. p->validTZ
99ec0 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29 3f 31 3a = (p->tz!=0)?1:
99ed0 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0;. return 0;.}
99ee0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 ../*.** Convert
99ef0 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 20 from YYYY-MM-DD
99f00 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c 69 HH:MM:SS to juli
99f10 61 6e 20 64 61 79 2e 20 20 57 65 20 61 6c 77 61 an day. We alwa
99f20 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68 61 ys assume.** tha
99f30 74 20 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44 44 t the YYYY-MM-DD
99f40 20 69 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f is according to
99f50 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 the Gregorian c
99f60 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 alendar..**.** R
99f70 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65 75 73 eference: Meeus
99f80 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61 74 page 61.*/.stat
99f90 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 4a ic void computeJ
99fa0 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a D(DateTime *p){.
99fb0 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 41 int Y, M, D, A
99fc0 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20 20 , B, X1, X2;..
99fd0 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20 29 if( p->validJD )
99fe0 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 return;. if( p
99ff0 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20 20 ->validYMD ){.
9a000 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20 20 Y = p->Y;.
9a010 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44 20 M = p->M;. D
9a020 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65 7b = p->D;. }else{
9a030 0a 20 20 20 20 59 20 3d 20 32 30 30 30 3b 20 20 . Y = 2000;
9a040 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 73 70 65 /* If no YMD spe
9a050 63 69 66 69 65 64 2c 20 61 73 73 75 6d 65 20 32 cified, assume 2
9a060 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20 20 000-Jan-01 */.
9a070 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20 3d M = 1;. D =
9a080 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d 3c 1;. }. if( M<
9a090 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a 20 =2 ){. Y--;.
9a0a0 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d 0a M += 12;. }.
9a0b0 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20 42 A = Y/100;. B
9a0c0 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 2f 34 29 = 2 - A + (A/4)
9a0d0 3b 0a 20 20 58 31 20 3d 20 33 36 35 32 35 2a 28 ;. X1 = 36525*(
9a0e0 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a 20 20 58 Y+4716)/100;. X
9a0f0 32 20 3d 20 33 30 36 30 30 31 2a 28 4d 2b 31 29 2 = 306001*(M+1)
9a100 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e 69 4a 44 /10000;. p->iJD
9a110 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 = (sqlite3_int6
9a120 34 29 28 28 58 31 20 2b 20 58 32 20 2b 20 44 20 4)((X1 + X2 + D
9a130 2b 20 42 20 2d 20 31 35 32 34 2e 35 20 29 20 2a + B - 1524.5 ) *
9a140 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 70 2d 86400000);. p-
9a150 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 >validJD = 1;.
9a160 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 if( p->validHMS
9a170 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d ){. p->iJD +=
9a180 20 70 2d 3e 68 2a 33 36 30 30 30 30 30 20 2b 20 p->h*3600000 +
9a190 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 28 73 71 p->m*60000 + (sq
9a1a0 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 70 2d 3e lite3_int64)(p->
9a1b0 73 2a 31 30 30 30 29 3b 0a 20 20 20 20 69 66 28 s*1000);. if(
9a1c0 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 p->validTZ ){.
9a1d0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 70 p->iJD -= p
9a1e0 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20 20 20 20 ->tz*60000;.
9a1f0 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 p->validYMD =
9a200 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 0;. p->vali
9a210 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20 20 dHMS = 0;.
9a220 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a p->validTZ = 0;.
9a230 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
9a240 2a 2a 20 50 61 72 73 65 20 64 61 74 65 73 20 6f ** Parse dates o
9a250 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a f the form.**.**
9a260 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 YYYY-MM-DD
9a270 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a 20 HH:MM:SS.FFF.**
9a280 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 YYYY-MM-DD H
9a290 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 59 H:MM:SS.** Y
9a2a0 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 0a YYY-MM-DD HH:MM.
9a2b0 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 ** YYYY-MM-D
9a2c0 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 D.**.** Write th
9a2d0 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 74 68 e result into th
9a2e0 65 20 44 61 74 65 54 69 6d 65 20 73 74 72 75 63 e DateTime struc
9a2f0 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 ture and return
9a300 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 0.** on success
9a310 61 6e 64 20 31 20 69 66 20 74 68 65 20 69 6e 70 and 1 if the inp
9a320 75 74 20 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 ut string is not
9a330 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a a well-formed.*
9a340 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 * date..*/.stati
9a350 63 20 69 6e 74 20 70 61 72 73 65 59 79 79 79 4d c int parseYyyyM
9a360 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a mDd(const char *
9a370 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 zDate, DateTime
9a380 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c *p){. int Y, M,
9a390 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28 20 D, neg;.. if(
9a3a0 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 7b zDate[0]=='-' ){
9a3b0 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 . zDate++;.
9a3c0 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65 6c neg = 1;. }el
9a3d0 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b se{. neg = 0;
9a3e0 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 74 44 69 . }. if( getDi
9a3f0 67 69 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c 39 gits(zDate,4,0,9
9a400 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 31 999,'-',&Y,2,1,1
9a410 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 2c 2,'-',&M,2,1,31,
9a420 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20 20 20 20 0,&D)!=3 ){.
9a430 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 return 1;. }.
9a440 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20 77 zDate += 10;. w
9a450 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 hile( sqlite3Iss
9a460 70 61 63 65 28 2a 7a 44 61 74 65 29 20 7c 7c 20 pace(*zDate) ||
9a470 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 65 'T'==*(u8*)zDate
9a480 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 ){ zDate++; }.
9a490 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 if( parseHhMmSs
9a4a0 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b (zDate, p)==0 ){
9a4b0 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f 74 20 74 . /* We got t
9a4c0 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c he time */. }el
9a4d0 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 30 se if( *zDate==0
9a4e0 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 ){. p->valid
9a4f0 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 HMS = 0;. }else
9a500 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a {. return 1;.
9a510 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 }. p->validJD
9a520 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 = 0;. p->valid
9a530 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d 3e 59 20 YMD = 1;. p->Y
9a540 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 59 3b 0a = neg ? -Y : Y;.
9a550 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d p->M = M;. p-
9a560 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 20 70 2d >D = D;. if( p-
9a570 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20 >validTZ ){.
9a580 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
9a590 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
9a5a0 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 ./*.** Set the t
9a5b0 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72 72 65 ime to the curre
9a5c0 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74 65 64 nt time reported
9a5d0 20 62 79 20 74 68 65 20 56 46 53 2e 0a 2a 2a 0a by the VFS..**.
9a5e0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
9a5f0 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a mber of errors..
9a600 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 */.static int se
9a610 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 tDateTimeToCurre
9a620 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 nt(sqlite3_conte
9a630 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 xt *context, Dat
9a640 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 73 71 6c eTime *p){. sql
9a650 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 ite3 *db = sqlit
9a660 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
9a670 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 ndle(context);.
9a680 20 69 66 28 20 73 71 6c 69 74 65 33 4f 73 43 75 if( sqlite3OsCu
9a690 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 64 rrentTimeInt64(d
9a6a0 62 2d 3e 70 56 66 73 2c 20 26 70 2d 3e 69 4a 44 b->pVfs, &p->iJD
9a6b0 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a )==SQLITE_OK ){.
9a6c0 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d p->validJD =
9a6d0 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 1;. return 0
9a6e0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
9a6f0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 0a eturn 1;. }.}..
9a700 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f /*.** Attempt to
9a710 20 70 61 72 73 65 20 74 68 65 20 67 69 76 65 6e parse the given
9a720 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 4a string into a J
9a730 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d 62 65 72 ulian Day Number
9a740 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 . Return.** the
9a750 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 number of error
9a760 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c s..**.** The fol
9a770 6c 6f 77 69 6e 67 20 61 72 65 20 61 63 63 65 70 lowing are accep
9a780 74 61 62 6c 65 20 66 6f 72 6d 73 20 66 6f 72 20 table forms for
9a790 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 the input string
9a7a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 59 59 :.**.** YYY
9a7b0 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 Y-MM-DD HH:MM:SS
9a7c0 2e 46 46 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a 2a .FFF +/-HH:MM.*
9a7d0 2a 20 20 20 20 20 20 44 44 44 44 2e 44 44 20 0a * DDDD.DD .
9a7e0 2a 2a 20 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a 2a ** now.**.*
9a7f0 2a 20 49 6e 20 74 68 65 20 66 69 72 73 74 20 66 * In the first f
9a800 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d 48 48 3a 4d orm, the +/-HH:M
9a810 4d 20 69 73 20 61 6c 77 61 79 73 20 6f 70 74 69 M is always opti
9a820 6f 6e 61 6c 2e 20 20 54 68 65 20 66 72 61 63 74 onal. The fract
9a830 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64 73 ional.** seconds
9a840 20 65 78 74 65 6e 73 69 6f 6e 20 28 74 68 65 20 extension (the
9a850 22 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f ".FFF") is optio
9a860 6e 61 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 nal. The second
9a870 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22 3a s portion.** (":
9a880 53 53 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 SS.FFF") is opti
9a890 6f 6e 2e 20 20 54 68 65 20 79 65 61 72 20 61 6e on. The year an
9a8a0 64 20 64 61 74 65 20 63 61 6e 20 62 65 20 6f 6d d date can be om
9a8b0 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a itted as long.**
9a8c0 20 61 73 20 74 68 65 72 65 20 69 73 20 61 20 74 as there is a t
9a8d0 69 6d 65 20 73 74 72 69 6e 67 2e 20 20 54 68 65 ime string. The
9a8e0 20 74 69 6d 65 20 73 74 72 69 6e 67 20 63 61 6e time string can
9a8f0 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c be omitted as l
9a900 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 ong.** as there
9a910 69 73 20 61 20 79 65 61 72 20 61 6e 64 20 64 61 is a year and da
9a920 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e te..*/.static in
9a930 74 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d t parseDateOrTim
9a940 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e e(. sqlite3_con
9a950 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a text *context, .
9a960 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
9a970 61 74 65 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 ate, . DateTime
9a980 20 2a 70 0a 29 7b 0a 20 20 64 6f 75 62 6c 65 20 *p.){. double
9a990 72 3b 0a 20 20 69 66 28 20 70 61 72 73 65 59 79 r;. if( parseYy
9a9a0 79 79 4d 6d 44 64 28 7a 44 61 74 65 2c 70 29 3d yyMmDd(zDate,p)=
9a9b0 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
9a9c0 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0;. }else if(
9a9d0 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61 74 parseHhMmSs(zDat
9a9e0 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 e, p)==0 ){.
9a9f0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 return 0;. }els
9aa00 65 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 e if( sqlite3Str
9aa10 49 43 6d 70 28 7a 44 61 74 65 2c 22 6e 6f 77 22 ICmp(zDate,"now"
9aa20 29 3d 3d 30 29 7b 0a 20 20 20 20 72 65 74 75 72 )==0){. retur
9aa30 6e 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f 43 n setDateTimeToC
9aa40 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c 20 urrent(context,
9aa50 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 p);. }else if(
9aa60 73 71 6c 69 74 65 33 41 74 6f 46 28 7a 44 61 74 sqlite3AtoF(zDat
9aa70 65 2c 20 26 72 2c 20 73 71 6c 69 74 65 33 53 74 e, &r, sqlite3St
9aa80 72 6c 65 6e 33 30 28 7a 44 61 74 65 29 2c 20 53 rlen30(zDate), S
9aa90 51 4c 49 54 45 5f 55 54 46 38 29 20 29 7b 0a 20 QLITE_UTF8) ){.
9aaa0 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c p->iJD = (sql
9aab0 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 ite3_int64)(r*86
9aac0 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 400000.0 + 0.5);
9aad0 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 . p->validJD
9aae0 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 1;. return
9aaf0 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
9ab00 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 1;.}../*.** Comp
9ab10 75 74 65 20 74 68 65 20 59 65 61 72 2c 20 4d 6f ute the Year, Mo
9ab20 6e 74 68 2c 20 61 6e 64 20 44 61 79 20 66 72 6f nth, and Day fro
9ab30 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 m the julian day
9ab40 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 number..*/.stat
9ab50 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 ic void computeY
9ab60 4d 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b MD(DateTime *p){
9ab70 0a 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 . int Z, A, B,
9ab80 43 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69 C, D, E, X1;. i
9ab90 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 f( p->validYMD )
9aba0 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 return;. if( !
9abb0 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 p->validJD ){.
9abc0 20 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20 p->Y = 2000;.
9abd0 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 p->M = 1;.
9abe0 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c p->D = 1;. }el
9abf0 73 65 7b 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74 se{. Z = (int
9ac00 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 )((p->iJD + 4320
9ac10 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0000)/86400000);
9ac20 0a 20 20 20 20 41 20 3d 20 28 69 6e 74 29 28 28 . A = (int)((
9ac30 5a 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29 2f Z - 1867216.25)/
9ac40 33 36 35 32 34 2e 32 35 29 3b 0a 20 20 20 20 41 36524.25);. A
9ac50 20 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28 = Z + 1 + A - (
9ac60 41 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20 41 20 A/4);. B = A
9ac70 2b 20 31 35 32 34 3b 0a 20 20 20 20 43 20 3d 20 + 1524;. C =
9ac80 28 69 6e 74 29 28 28 42 20 2d 20 31 32 32 2e 31 (int)((B - 122.1
9ac90 29 2f 33 36 35 2e 32 35 29 3b 0a 20 20 20 20 44 )/365.25);. D
9aca0 20 3d 20 28 33 36 35 32 35 2a 43 29 2f 31 30 30 = (36525*C)/100
9acb0 3b 0a 20 20 20 20 45 20 3d 20 28 69 6e 74 29 28 ;. E = (int)(
9acc0 28 42 2d 44 29 2f 33 30 2e 36 30 30 31 29 3b 0a (B-D)/30.6001);.
9acd0 20 20 20 20 58 31 20 3d 20 28 69 6e 74 29 28 33 X1 = (int)(3
9ace0 30 2e 36 30 30 31 2a 45 29 3b 0a 20 20 20 20 70 0.6001*E);. p
9acf0 2d 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20 58 31 ->D = B - D - X1
9ad00 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31 ;. p->M = E<1
9ad10 34 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a 4 ? E-1 : E-13;.
9ad20 20 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e p->Y = p->M>
9ad30 32 20 3f 20 43 20 2d 20 34 37 31 36 20 3a 20 43 2 ? C - 4716 : C
9ad40 20 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70 - 4715;. }. p
9ad50 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a ->validYMD = 1;.
9ad60 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 }../*.** Compute
9ad70 20 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74 the Hour, Minut
9ad80 65 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73 20 66 e, and Seconds f
9ad90 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 rom the julian d
9ada0 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 ay number..*/.st
9adb0 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 atic void comput
9adc0 65 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70 eHMS(DateTime *p
9add0 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 66 ){. int s;. if
9ade0 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20 ( p->validHMS )
9adf0 72 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74 return;. comput
9ae00 65 4a 44 28 70 29 3b 0a 20 20 73 20 3d 20 28 69 eJD(p);. s = (i
9ae10 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 nt)((p->iJD + 43
9ae20 32 30 30 30 30 30 29 20 25 20 38 36 34 30 30 30 200000) % 864000
9ae30 30 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f 00);. p->s = s/
9ae40 31 30 30 30 2e 30 3b 0a 20 20 73 20 3d 20 28 69 1000.0;. s = (i
9ae50 6e 74 29 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 nt)p->s;. p->s
9ae60 2d 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 -= s;. p->h = s
9ae70 2f 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d /3600;. s -= p-
9ae80 3e 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 >h*3600;. p->m
9ae90 3d 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b = s/60;. p->s +
9aea0 3d 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 = s - p->m*60;.
9aeb0 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 p->validHMS = 1
9aec0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 ;.}../*.** Compu
9aed0 74 65 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 te both YMD and
9aee0 48 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f HMS.*/.static vo
9aef0 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d id computeYMD_HM
9af00 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a S(DateTime *p){.
9af10 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b computeYMD(p);
9af20 0a 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 . computeHMS(p)
9af30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 ;.}../*.** Clear
9af40 20 74 68 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 the YMD and HMS
9af50 20 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 and the TZ.*/.s
9af60 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 tatic void clear
9af70 59 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 YMD_HMS_TZ(DateT
9af80 69 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 ime *p){. p->va
9af90 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d lidYMD = 0;. p-
9afa0 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 >validHMS = 0;.
9afb0 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b p->validTZ = 0;
9afc0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 72 65 63 .}../*.** On rec
9afd0 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 6c 61 74 ent Windows plat
9afe0 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f 63 61 6c forms, the local
9aff0 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 74 69 6f time_s() functio
9b000 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a n is available.*
9b010 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 * as part of the
9b020 20 22 53 65 63 75 72 65 20 43 52 54 22 2e 20 49 "Secure CRT". I
9b030 74 20 69 73 20 65 73 73 65 6e 74 69 61 6c 6c 79 t is essentially
9b040 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a equivalent to .
9b050 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 29 ** localtime_r()
9b060 20 61 76 61 69 6c 61 62 6c 65 20 75 6e 64 65 72 available under
9b070 20 6d 6f 73 74 20 50 4f 53 49 58 20 70 6c 61 74 most POSIX plat
9b080 66 6f 72 6d 73 2c 20 65 78 63 65 70 74 20 74 68 forms, except th
9b090 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72 64 65 72 at the .** order
9b0a0 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 of the paramete
9b0b0 72 73 20 69 73 20 72 65 76 65 72 73 65 64 2e 0a rs is reversed..
9b0c0 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74 70 3a 2f **.** See http:/
9b0d0 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f 66 74 2e /msdn.microsoft.
9b0e0 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62 72 61 72 com/en-us/librar
9b0f0 79 2f 61 34 34 32 78 33 79 65 28 56 53 2e 38 30 y/a442x3ye(VS.80
9b100 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 ).aspx..**.** If
9b110 20 74 68 65 20 75 73 65 72 20 68 61 73 20 6e 6f the user has no
9b120 74 20 69 6e 64 69 63 61 74 65 64 20 74 6f 20 75 t indicated to u
9b130 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 29 se localtime_r()
9b140 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 or localtime_s(
9b150 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c 20 63 68 ).** already, ch
9b160 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53 56 43 20 eck for an MSVC
9b170 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e 6d 65 6e build environmen
9b180 74 20 74 68 61 74 20 70 72 6f 76 69 64 65 73 20 t that provides
9b190 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 .** localtime_s(
9b1a0 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e )..*/.#if !defin
9b1b0 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d ed(HAVE_LOCALTIM
9b1c0 45 5f 52 29 20 26 26 20 21 64 65 66 69 6e 65 64 E_R) && !defined
9b1d0 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f (HAVE_LOCALTIME_
9b1e0 53 29 20 26 26 20 5c 0a 20 20 20 20 20 64 65 66 S) && \. def
9b1f0 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 20 26 ined(_MSC_VER) &
9b200 26 20 64 65 66 69 6e 65 64 28 5f 43 52 54 5f 49 & defined(_CRT_I
9b210 4e 53 45 43 55 52 45 5f 44 45 50 52 45 43 41 54 NSECURE_DEPRECAT
9b220 45 29 0a 23 64 65 66 69 6e 65 20 48 41 56 45 5f E).#define HAVE_
9b230 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31 0a 23 65 LOCALTIME_S 1.#e
9b240 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
9b250 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 LITE_OMIT_LOCALT
9b260 49 4d 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f IME./*.** The fo
9b270 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 llowing routine
9b280 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 72 implements the r
9b290 6f 75 67 68 20 65 71 75 69 76 61 6c 65 6e 74 20 ough equivalent
9b2a0 6f 66 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 29 of localtime_r()
9b2b0 0a 2a 2a 20 75 73 69 6e 67 20 77 68 61 74 65 76 .** using whatev
9b2c0 65 72 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 er operating-sys
9b2d0 74 65 6d 20 73 70 65 63 69 66 69 63 20 6c 6f 63 tem specific loc
9b2e0 61 6c 74 69 6d 65 20 66 61 63 69 6c 69 74 79 20 altime facility
9b2f0 74 68 61 74 0a 2a 2a 20 69 73 20 61 76 61 69 6c that.** is avail
9b300 61 62 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74 able. This rout
9b310 69 6e 65 20 72 65 74 75 72 6e 73 20 30 20 6f 6e ine returns 0 on
9b320 20 73 75 63 63 65 73 73 20 61 6e 64 0a 2a 2a 20 success and.**
9b330 6e 6f 6e 2d 7a 65 72 6f 20 6f 6e 20 61 6e 79 20 non-zero on any
9b340 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e 0a 2a kind of error..*
9b350 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 71 6c 69 *.** If the sqli
9b360 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b370 62 4c 6f 63 61 6c 74 69 6d 65 46 61 75 6c 74 20 bLocaltimeFault
9b380 76 61 72 69 61 62 6c 65 20 69 73 20 74 72 75 65 variable is true
9b390 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f then this.** ro
9b3a0 75 74 69 6e 65 20 77 69 6c 6c 20 61 6c 77 61 79 utine will alway
9b3b0 73 20 66 61 69 6c 2e 0a 2a 2f 0a 73 74 61 74 69 s fail..*/.stati
9b3c0 63 20 69 6e 74 20 6f 73 4c 6f 63 61 6c 74 69 6d c int osLocaltim
9b3d0 65 28 74 69 6d 65 5f 74 20 2a 74 2c 20 73 74 72 e(time_t *t, str
9b3e0 75 63 74 20 74 6d 20 2a 70 54 6d 29 7b 0a 20 20 uct tm *pTm){.
9b3f0 69 6e 74 20 72 63 3b 0a 23 69 66 20 28 21 64 65 int rc;.#if (!de
9b400 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c fined(HAVE_LOCAL
9b410 54 49 4d 45 5f 52 29 20 7c 7c 20 21 48 41 56 45 TIME_R) || !HAVE
9b420 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 29 20 5c 0a _LOCALTIME_R) \.
9b430 20 20 20 20 20 20 26 26 20 28 21 64 65 66 69 6e && (!defin
9b440 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d ed(HAVE_LOCALTIM
9b450 45 5f 53 29 20 7c 7c 20 21 48 41 56 45 5f 4c 4f E_S) || !HAVE_LO
9b460 43 41 4c 54 49 4d 45 5f 53 29 0a 20 20 73 74 72 CALTIME_S). str
9b470 75 63 74 20 74 6d 20 2a 70 58 3b 0a 23 69 66 20 uct tm *pX;.#if
9b480 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
9b490 45 3e 30 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 E>0. sqlite3_mu
9b4a0 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c tex *mutex = sql
9b4b0 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
9b4c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
9b4d0 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 IC_MASTER);.#end
9b4e0 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 if. sqlite3_mut
9b4f0 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b ex_enter(mutex);
9b500 0a 20 20 70 58 20 3d 20 6c 6f 63 61 6c 74 69 6d . pX = localtim
9b510 65 28 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 e(t);.#ifndef SQ
9b520 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 LITE_OMIT_BUILTI
9b530 4e 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c N_TEST. if( sql
9b540 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b550 2e 62 4c 6f 63 61 6c 74 69 6d 65 46 61 75 6c 74 .bLocaltimeFault
9b560 20 29 20 70 58 20 3d 20 30 3b 0a 23 65 6e 64 69 ) pX = 0;.#endi
9b570 66 0a 20 20 69 66 28 20 70 58 20 29 20 2a 70 54 f. if( pX ) *pT
9b580 6d 20 3d 20 2a 70 58 3b 0a 20 20 73 71 6c 69 74 m = *pX;. sqlit
9b590 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
9b5a0 75 74 65 78 29 3b 0a 20 20 72 63 20 3d 20 70 58 utex);. rc = pX
9b5b0 3d 3d 30 3b 0a 23 65 6c 73 65 0a 23 69 66 6e 64 ==0;.#else.#ifnd
9b5c0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 ef SQLITE_OMIT_B
9b5d0 55 49 4c 54 49 4e 5f 54 45 53 54 0a 20 20 69 66 UILTIN_TEST. if
9b5e0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9b5f0 6f 6e 66 69 67 2e 62 4c 6f 63 61 6c 74 69 6d 65 onfig.bLocaltime
9b600 46 61 75 6c 74 20 29 20 72 65 74 75 72 6e 20 31 Fault ) return 1
9b610 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 ;.#endif.#if def
9b620 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 ined(HAVE_LOCALT
9b630 49 4d 45 5f 52 29 20 26 26 20 48 41 56 45 5f 4c IME_R) && HAVE_L
9b640 4f 43 41 4c 54 49 4d 45 5f 52 0a 20 20 72 63 20 OCALTIME_R. rc
9b650 3d 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 74 2c = localtime_r(t,
9b660 20 70 54 6d 29 3d 3d 30 3b 0a 23 65 6c 73 65 0a pTm)==0;.#else.
9b670 20 20 72 63 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 rc = localtime
9b680 5f 73 28 70 54 6d 2c 20 74 29 3b 0a 23 65 6e 64 _s(pTm, t);.#end
9b690 69 66 20 2f 2a 20 48 41 56 45 5f 4c 4f 43 41 4c if /* HAVE_LOCAL
9b6a0 54 49 4d 45 5f 52 20 2a 2f 0a 23 65 6e 64 69 66 TIME_R */.#endif
9b6b0 20 2f 2a 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 /* HAVE_LOCALTI
9b6c0 4d 45 5f 52 20 7c 7c 20 48 41 56 45 5f 4c 4f 43 ME_R || HAVE_LOC
9b6d0 41 4c 54 49 4d 45 5f 53 20 2a 2f 0a 20 20 72 65 ALTIME_S */. re
9b6e0 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 turn rc;.}.#endi
9b6f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
9b700 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a 2f 0a 0a 0a _LOCALTIME */...
9b710 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
9b720 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 2f 2a MIT_LOCALTIME./*
9b730 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 .** Compute the
9b740 64 69 66 66 65 72 65 6e 63 65 20 28 69 6e 20 6d difference (in m
9b750 69 6c 6c 69 73 65 63 6f 6e 64 73 29 20 62 65 74 illiseconds) bet
9b760 77 65 65 6e 20 6c 6f 63 61 6c 74 69 6d 65 20 61 ween localtime a
9b770 6e 64 20 55 54 43 0a 2a 2a 20 28 61 2e 6b 2e 61 nd UTC.** (a.k.a
9b780 2e 20 47 4d 54 29 20 66 6f 72 20 74 68 65 20 74 . GMT) for the t
9b790 69 6d 65 20 76 61 6c 75 65 20 70 20 77 68 65 72 ime value p wher
9b7a0 65 20 70 20 69 73 20 69 6e 20 55 54 43 2e 20 49 e p is in UTC. I
9b7b0 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 f no error occur
9b7c0 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68 69 s,.** return thi
9b7d0 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 s value and set
9b7e0 2a 70 52 63 20 74 6f 20 53 51 4c 49 54 45 5f 4f *pRc to SQLITE_O
9b7f0 4b 2e 20 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 66 K. .**.** Or, if
9b800 20 61 6e 20 65 72 72 6f 72 20 64 6f 65 73 20 6f an error does o
9b810 63 63 75 72 2c 20 73 65 74 20 2a 70 52 63 20 74 ccur, set *pRc t
9b820 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 o SQLITE_ERROR.
9b830 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c The returned val
9b840 75 65 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e ue.** is undefin
9b850 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e ed in this case.
9b860 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 .*/.static sqlit
9b870 65 33 5f 69 6e 74 36 34 20 6c 6f 63 61 6c 74 69 e3_int64 localti
9b880 6d 65 4f 66 66 73 65 74 28 0a 20 20 44 61 74 65 meOffset(. Date
9b890 54 69 6d 65 20 2a 70 2c 20 20 20 20 20 20 20 20 Time *p,
9b8a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
9b8b0 61 74 65 20 61 74 20 77 68 69 63 68 20 74 6f 20 ate at which to
9b8c0 63 61 6c 63 75 6c 61 74 65 20 6f 66 66 73 65 74 calculate offset
9b8d0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f */. sqlite3_co
9b8e0 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20 20 20 ntext *pCtx,
9b8f0 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65 /* Write e
9b900 72 72 6f 72 20 68 65 72 65 20 69 66 20 6f 6e 65 rror here if one
9b910 20 6f 63 63 75 72 73 20 2a 2f 0a 20 20 69 6e 74 occurs */. int
9b920 20 2a 70 52 63 20 20 20 20 20 20 20 20 20 20 20 *pRc
9b930 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
9b940 4f 55 54 3a 20 45 72 72 6f 72 20 63 6f 64 65 2e OUT: Error code.
9b950 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 45 52 SQLITE_OK or ER
9b960 52 4f 52 20 2a 2f 0a 29 7b 0a 20 20 44 61 74 65 ROR */.){. Date
9b970 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69 6d Time x, y;. tim
9b980 65 5f 74 20 74 3b 0a 20 20 73 74 72 75 63 74 20 e_t t;. struct
9b990 74 6d 20 73 4c 6f 63 61 6c 3b 0a 0a 20 20 2f 2a tm sLocal;.. /*
9b9a0 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 Initialize the
9b9b0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 73 4c 6f 63 contents of sLoc
9b9c0 61 6c 20 74 6f 20 61 76 6f 69 64 20 61 20 63 6f al to avoid a co
9b9d0 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 2e 20 mpiler warning.
9b9e0 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 4c 6f */. memset(&sLo
9b9f0 63 61 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 cal, 0, sizeof(s
9ba00 4c 6f 63 61 6c 29 29 3b 0a 0a 20 20 78 20 3d 20 Local));.. x =
9ba10 2a 70 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 *p;. computeYMD
9ba20 5f 48 4d 53 28 26 78 29 3b 0a 20 20 69 66 28 20 _HMS(&x);. if(
9ba30 78 2e 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e x.Y<1971 || x.Y>
9ba40 3d 32 30 33 38 20 29 7b 0a 20 20 20 20 78 2e 59 =2038 ){. x.Y
9ba50 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d = 2000;. x.M
9ba60 20 3d 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20 = 1;. x.D =
9ba70 31 3b 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a 1;. x.h = 0;.
9ba80 20 20 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 x.m = 0;.
9ba90 20 78 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 x.s = 0.0;. }
9baa0 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 73 else {. int s
9bab0 20 3d 20 28 69 6e 74 29 28 78 2e 73 20 2b 20 30 = (int)(x.s + 0
9bac0 2e 35 29 3b 0a 20 20 20 20 78 2e 73 20 3d 20 73 .5);. x.s = s
9bad0 3b 0a 20 20 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 ;. }. x.tz = 0
9bae0 3b 0a 20 20 78 2e 76 61 6c 69 64 4a 44 20 3d 20 ;. x.validJD =
9baf0 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 0;. computeJD(&
9bb00 78 29 3b 0a 20 20 74 20 3d 20 28 74 69 6d 65 5f x);. t = (time_
9bb10 74 29 28 78 2e 69 4a 44 2f 31 30 30 30 20 2d 20 t)(x.iJD/1000 -
9bb20 32 31 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 21086676*(i64)10
9bb30 30 30 30 29 3b 0a 20 20 69 66 28 20 6f 73 4c 6f 000);. if( osLo
9bb40 63 61 6c 74 69 6d 65 28 26 74 2c 20 26 73 4c 6f caltime(&t, &sLo
9bb50 63 61 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 cal) ){. sqli
9bb60 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
9bb70 28 70 43 74 78 2c 20 22 6c 6f 63 61 6c 20 74 69 (pCtx, "local ti
9bb80 6d 65 20 75 6e 61 76 61 69 6c 61 62 6c 65 22 2c me unavailable",
9bb90 20 2d 31 29 3b 0a 20 20 20 20 2a 70 52 63 20 3d -1);. *pRc =
9bba0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
9bbb0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
9bbc0 0a 20 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e . y.Y = sLocal.
9bbd0 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a tm_year + 1900;.
9bbe0 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.M = sLocal.t
9bbf0 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 79 2e 44 m_mon + 1;. y.D
9bc00 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 = sLocal.tm_mda
9bc10 79 3b 0a 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 y;. y.h = sLoca
9bc20 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 79 2e 6d l.tm_hour;. y.m
9bc30 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e = sLocal.tm_min
9bc40 3b 0a 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c ;. y.s = sLocal
9bc50 2e 74 6d 5f 73 65 63 3b 0a 20 20 79 2e 76 61 6c .tm_sec;. y.val
9bc60 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e 76 idYMD = 1;. y.v
9bc70 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 79 alidHMS = 1;. y
9bc80 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 .validJD = 0;.
9bc90 79 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 y.validTZ = 0;.
9bca0 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b 0a computeJD(&y);.
9bcb0 20 20 2a 70 52 63 20 3d 20 53 51 4c 49 54 45 5f *pRc = SQLITE_
9bcc0 4f 4b 3b 0a 20 20 72 65 74 75 72 6e 20 79 2e 69 OK;. return y.i
9bcd0 4a 44 20 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 65 JD - x.iJD;.}.#e
9bce0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
9bcf0 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a 2f MIT_LOCALTIME */
9bd00 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 ../*.** Process
9bd10 61 20 6d 6f 64 69 66 69 65 72 20 74 6f 20 61 20 a modifier to a
9bd20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e date-time stamp.
9bd30 20 20 54 68 65 20 6d 6f 64 69 66 69 65 72 73 20 The modifiers
9bd40 61 72 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 are.** as follow
9bd50 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e 4e s:.**.** NNN
9bd60 20 64 61 79 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e days.** NNN
9bd70 20 68 6f 75 72 73 0a 2a 2a 20 20 20 20 20 4e 4e hours.** NN
9bd80 4e 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 20 20 20 N minutes.**
9bd90 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e 64 NNN.NNNN second
9bda0 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f 6e s.** NNN mon
9bdb0 74 68 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 79 ths.** NNN y
9bdc0 65 61 72 73 0a 2a 2a 20 20 20 20 20 73 74 61 72 ears.** star
9bdd0 74 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 t of month.**
9bde0 20 20 73 74 61 72 74 20 6f 66 20 79 65 61 72 0a start of year.
9bdf0 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 ** start of
9be00 77 65 65 6b 0a 2a 2a 20 20 20 20 20 73 74 61 72 week.** star
9be10 74 20 6f 66 20 64 61 79 0a 2a 2a 20 20 20 20 20 t of day.**
9be20 77 65 65 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 20 weekday N.**
9be30 20 75 6e 69 78 65 70 6f 63 68 0a 2a 2a 20 20 20 unixepoch.**
9be40 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 20 localtime.**
9be50 20 20 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 74 utc.**.** Ret
9be60 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 urn 0 on success
9be70 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65 20 and 1 if there
9be80 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 is any kind of e
9be90 72 72 6f 72 2e 20 49 66 20 74 68 65 20 65 72 72 rror. If the err
9bea0 6f 72 0a 2a 2a 20 69 73 20 69 6e 20 61 20 73 79 or.** is in a sy
9beb0 73 74 65 6d 20 63 61 6c 6c 20 28 69 2e 65 2e 20 stem call (i.e.
9bec0 6c 6f 63 61 6c 74 69 6d 65 28 29 29 2c 20 74 68 localtime()), th
9bed0 65 6e 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 en an error mess
9bee0 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 0a 2a age is written.*
9bef0 2a 20 74 6f 20 63 6f 6e 74 65 78 74 20 70 43 74 * to context pCt
9bf00 78 2e 20 49 66 20 74 68 65 20 65 72 72 6f 72 20 x. If the error
9bf10 69 73 20 61 6e 20 75 6e 72 65 63 6f 67 6e 69 7a is an unrecogniz
9bf20 65 64 20 6d 6f 64 69 66 69 65 72 2c 20 6e 6f 20 ed modifier, no
9bf30 65 72 72 6f 72 20 69 73 0a 2a 2a 20 77 72 69 74 error is.** writ
9bf40 74 65 6e 20 74 6f 20 70 43 74 78 2e 0a 2a 2f 0a ten to pCtx..*/.
9bf50 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 static int parse
9bf60 4d 6f 64 69 66 69 65 72 28 73 71 6c 69 74 65 33 Modifier(sqlite3
9bf70 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
9bf80 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 const char *zMod
9bf90 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a , DateTime *p){.
9bfa0 20 20 69 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20 int rc = 1;.
9bfb0 69 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20 int n;. double
9bfc0 72 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42 r;. char *z, zB
9bfd0 75 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42 uf[30];. z = zB
9bfe0 75 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e uf;. for(n=0; n
9bff0 3c 41 72 72 61 79 53 69 7a 65 28 7a 42 75 66 29 <ArraySize(zBuf)
9c000 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e -1 && zMod[n]; n
9c010 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20 ++){. z[n] =
9c020 28 63 68 61 72 29 73 71 6c 69 74 65 33 55 70 70 (char)sqlite3Upp
9c030 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29 7a 4d erToLower[(u8)zM
9c040 6f 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 20 7a 5b od[n]];. }. z[
9c050 6e 5d 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 n] = 0;. switch
9c060 28 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 ( z[0] ){.#ifnde
9c070 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
9c080 43 41 4c 54 49 4d 45 0a 20 20 20 20 63 61 73 65 CALTIME. case
9c090 20 27 6c 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 'l': {. /*
9c0a0 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20 localtime.
9c0b0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
9c0c0 41 73 73 75 6d 69 6e 67 20 74 68 65 20 63 75 72 Assuming the cur
9c0d0 72 65 6e 74 20 74 69 6d 65 20 76 61 6c 75 65 20 rent time value
9c0e0 69 73 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 is UTC (a.k.a. G
9c0f0 4d 54 29 2c 20 73 68 69 66 74 20 69 74 20 74 6f MT), shift it to
9c100 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c . ** show l
9c110 6f 63 61 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20 ocal time..
9c120 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 */. if( st
9c130 72 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69 rcmp(z, "localti
9c140 6d 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 me")==0 ){.
9c150 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b computeJD(p);
9c160 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
9c170 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 += localtimeOffs
9c180 65 74 28 70 2c 20 70 43 74 78 2c 20 26 72 63 29 et(p, pCtx, &rc)
9c190 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 ;. clearY
9c1a0 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 MD_HMS_TZ(p);.
9c1b0 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
9c1c0 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a k;. }.#endif.
9c1d0 20 20 20 20 63 61 73 65 20 27 75 27 3a 20 7b 0a case 'u': {.
9c1e0 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a /*. *
9c1f0 2a 20 20 20 20 75 6e 69 78 65 70 6f 63 68 0a 20 * unixepoch.
9c200 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
9c210 20 54 72 65 61 74 20 74 68 65 20 63 75 72 72 65 Treat the curre
9c220 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 nt value of p->i
9c230 4a 44 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 JD as the number
9c240 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 of. ** sec
9c250 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2e onds since 1970.
9c260 20 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 20 72 Convert to a r
9c270 65 61 6c 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e eal julian day n
9c280 75 6d 62 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a umber.. */.
9c290 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 if( strcmp
9c2a0 28 7a 2c 20 22 75 6e 69 78 65 70 6f 63 68 22 29 (z, "unixepoch")
9c2b0 3d 3d 30 20 26 26 20 70 2d 3e 76 61 6c 69 64 4a ==0 && p->validJ
9c2c0 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e D ){. p->
9c2d0 69 4a 44 20 3d 20 28 70 2d 3e 69 4a 44 20 2b 20 iJD = (p->iJD +
9c2e0 34 33 32 30 30 29 2f 38 36 34 30 30 20 2b 20 32 43200)/86400 + 2
9c2f0 31 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 1086676*(i64)100
9c300 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 63 00000;. c
9c310 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 learYMD_HMS_TZ(p
9c320 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
9c330 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 0;. }.#ifnd
9c340 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c ef SQLITE_OMIT_L
9c350 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 20 20 65 OCALTIME. e
9c360 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a lse if( strcmp(z
9c370 2c 20 22 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20 , "utc")==0 ){.
9c380 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 sqlite3_i
9c390 6e 74 36 34 20 63 31 3b 0a 20 20 20 20 20 20 20 nt64 c1;.
9c3a0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
9c3b0 20 20 20 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 c1 = loca
9c3c0 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 2c 20 70 ltimeOffset(p, p
9c3d0 43 74 78 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 Ctx, &rc);.
9c3e0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
9c3f0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
9c400 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 63 31 3b 0a p->iJD -= c1;.
9c410 20 20 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 clearY
9c420 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 MD_HMS_TZ(p);.
9c430 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b p->iJD +
9c440 3d 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d 65 = c1 - localtime
9c450 4f 66 66 73 65 74 28 70 2c 20 70 43 74 78 2c 20 Offset(p, pCtx,
9c460 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a &rc);. }.
9c470 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
9c480 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
9c490 7d 0a 20 20 20 20 63 61 73 65 20 27 77 27 3a 20 }. case 'w':
9c4a0 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 {. /*.
9c4b0 20 2a 2a 20 20 20 20 77 65 65 6b 64 61 79 20 4e ** weekday N
9c4c0 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
9c4d0 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74 65 ** Move the date
9c4e0 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 69 6d to the same tim
9c4f0 65 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 6f 63 e on the next oc
9c500 63 75 72 72 65 6e 63 65 20 6f 66 0a 20 20 20 20 currence of.
9c510 20 20 2a 2a 20 77 65 65 6b 64 61 79 20 4e 20 77 ** weekday N w
9c520 68 65 72 65 20 30 3d 3d 53 75 6e 64 61 79 2c 20 here 0==Sunday,
9c530 31 3d 3d 4d 6f 6e 64 61 79 2c 20 61 6e 64 20 73 1==Monday, and s
9c540 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68 65 o forth. If the
9c550 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 65 20 69 . ** date i
9c560 73 20 61 6c 72 65 61 64 79 20 6f 6e 20 74 68 65 s already on the
9c570 20 61 70 70 72 6f 70 72 69 61 74 65 20 77 65 65 appropriate wee
9c580 6b 64 61 79 2c 20 74 68 69 73 20 69 73 20 61 20 kday, this is a
9c590 6e 6f 2d 6f 70 2e 0a 20 20 20 20 20 20 2a 2f 0a no-op.. */.
9c5a0 20 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d if( strncm
9c5b0 70 28 7a 2c 20 22 77 65 65 6b 64 61 79 20 22 2c p(z, "weekday ",
9c5c0 20 38 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 8)==0.
9c5d0 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 && sqlite3
9c5e0 41 74 6f 46 28 26 7a 5b 38 5d 2c 20 26 72 2c 20 AtoF(&z[8], &r,
9c5f0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
9c600 26 7a 5b 38 5d 29 2c 20 53 51 4c 49 54 45 5f 55 &z[8]), SQLITE_U
9c610 54 46 38 29 0a 20 20 20 20 20 20 20 20 20 20 20 TF8).
9c620 20 20 20 20 26 26 20 28 6e 3d 28 69 6e 74 29 72 && (n=(int)r
9c630 29 3d 3d 72 20 26 26 20 6e 3e 3d 30 20 26 26 20 )==r && n>=0 &&
9c640 72 3c 37 20 29 7b 0a 20 20 20 20 20 20 20 20 73 r<7 ){. s
9c650 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 5a 3b 0a qlite3_int64 Z;.
9c660 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59 computeY
9c670 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 MD_HMS(p);.
9c680 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 p->validTZ =
9c690 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 0;. p->va
9c6a0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 lidJD = 0;.
9c6b0 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b computeJD(p);
9c6c0 0a 20 20 20 20 20 20 20 20 5a 20 3d 20 28 28 70 . Z = ((p
9c6d0 2d 3e 69 4a 44 20 2b 20 31 32 39 36 30 30 30 30 ->iJD + 12960000
9c6e0 30 29 2f 38 36 34 30 30 30 30 30 29 20 25 20 37 0)/86400000) % 7
9c6f0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 5a 3e ;. if( Z>
9c700 6e 20 29 20 5a 20 2d 3d 20 37 3b 0a 20 20 20 20 n ) Z -= 7;.
9c710 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 6e p->iJD += (n
9c720 20 2d 20 5a 29 2a 38 36 34 30 30 30 30 30 3b 0a - Z)*86400000;.
9c730 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 clearYMD
9c740 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 _HMS_TZ(p);.
9c750 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 rc = 0;.
9c760 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
9c770 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
9c780 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 's': {. /*.
9c790 20 20 20 20 20 20 2a 2a 20 20 20 20 73 74 61 72 ** star
9c7a0 74 20 6f 66 20 54 54 54 54 54 0a 20 20 20 20 20 t of TTTTT.
9c7b0 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d 6f 76 **. ** Mov
9c7c0 65 20 74 68 65 20 64 61 74 65 20 62 61 63 6b 77 e the date backw
9c7d0 61 72 64 73 20 74 6f 20 74 68 65 20 62 65 67 69 ards to the begi
9c7e0 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 63 75 72 nning of the cur
9c7f0 72 65 6e 74 20 64 61 79 2c 0a 20 20 20 20 20 20 rent day,.
9c800 2a 2a 20 6f 72 20 6d 6f 6e 74 68 20 6f 72 20 79 ** or month or y
9c810 65 61 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ear.. */.
9c820 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 if( strncmp(
9c830 7a 2c 20 22 73 74 61 72 74 20 6f 66 20 22 2c 20 z, "start of ",
9c840 39 29 21 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 9)!=0 ) break;.
9c850 20 20 20 20 20 7a 20 2b 3d 20 39 3b 0a 20 20 20 z += 9;.
9c860 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 computeYMD(p)
9c870 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 ;. p->valid
9c880 48 4d 53 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 HMS = 1;. p
9c890 2d 3e 68 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b 0a ->h = p->m = 0;.
9c8a0 20 20 20 20 20 20 70 2d 3e 73 20 3d 20 30 2e 30 p->s = 0.0
9c8b0 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 ;. p->valid
9c8c0 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d TZ = 0;. p-
9c8d0 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 >validJD = 0;.
9c8e0 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a if( strcmp(z
9c8f0 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a ,"month")==0 ){.
9c900 20 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 p->D = 1
9c910 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 ;. rc = 0
9c920 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
9c930 28 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 72 ( strcmp(z,"year
9c940 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
9c950 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a computeYMD(p);.
9c960 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 3d 20 31 p->M = 1
9c970 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20 3d ;. p->D =
9c980 20 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 1;. rc =
9c990 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 0;. }else
9c9a0 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 if( strcmp(z,"da
9c9b0 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 y")==0 ){.
9c9c0 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 rc = 0;.
9c9d0 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
9c9e0 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2b }. case '+
9c9f0 27 3a 0a 20 20 20 20 63 61 73 65 20 27 2d 27 3a ':. case '-':
9ca00 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 0a 20 . case '0':.
9ca10 20 20 20 63 61 73 65 20 27 31 27 3a 0a 20 20 20 case '1':.
9ca20 20 63 61 73 65 20 27 32 27 3a 0a 20 20 20 20 63 case '2':. c
9ca30 61 73 65 20 27 33 27 3a 0a 20 20 20 20 63 61 73 ase '3':. cas
9ca40 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65 20 e '4':. case
9ca50 27 35 27 3a 0a 20 20 20 20 63 61 73 65 20 27 36 '5':. case '6
9ca60 27 3a 0a 20 20 20 20 63 61 73 65 20 27 37 27 3a ':. case '7':
9ca70 0a 20 20 20 20 63 61 73 65 20 27 38 27 3a 0a 20 . case '8':.
9ca80 20 20 20 63 61 73 65 20 27 39 27 3a 20 7b 0a 20 case '9': {.
9ca90 20 20 20 20 20 64 6f 75 62 6c 65 20 72 52 6f 75 double rRou
9caa0 6e 64 65 72 3b 0a 20 20 20 20 20 20 66 6f 72 28 nder;. for(
9cab0 6e 3d 31 3b 20 7a 5b 6e 5d 20 26 26 20 7a 5b 6e n=1; z[n] && z[n
9cac0 5d 21 3d 27 3a 27 20 26 26 20 21 73 71 6c 69 74 ]!=':' && !sqlit
9cad0 65 33 49 73 73 70 61 63 65 28 7a 5b 6e 5d 29 3b e3Isspace(z[n]);
9cae0 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 69 66 n++){}. if
9caf0 28 20 21 73 71 6c 69 74 65 33 41 74 6f 46 28 7a ( !sqlite3AtoF(z
9cb00 2c 20 26 72 2c 20 6e 2c 20 53 51 4c 49 54 45 5f , &r, n, SQLITE_
9cb10 55 54 46 38 29 20 29 7b 0a 20 20 20 20 20 20 20 UTF8) ){.
9cb20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 rc = 1;.
9cb30 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
9cb40 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d if( z[n]==
9cb50 27 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ':' ){. /
9cb60 2a 20 41 20 6d 6f 64 69 66 69 65 72 20 6f 66 20 * A modifier of
9cb70 74 68 65 20 66 6f 72 6d 20 28 2b 7c 2d 29 48 48 the form (+|-)HH
9cb80 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 64 64 73 20 :MM:SS.FFF adds
9cb90 28 6f 72 20 73 75 62 74 72 61 63 74 73 29 20 74 (or subtracts) t
9cba0 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 70 he. ** sp
9cbb0 65 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f ecified number o
9cbc0 66 20 68 6f 75 72 73 2c 20 6d 69 6e 75 74 65 73 f hours, minutes
9cbd0 2c 20 73 65 63 6f 6e 64 73 2c 20 61 6e 64 20 66 , seconds, and f
9cbe0 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 ractional second
9cbf0 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 s. ** to
9cc00 74 68 65 20 74 69 6d 65 2e 20 20 54 68 65 20 22 the time. The "
9cc10 2e 46 46 46 22 20 6d 61 79 20 62 65 20 6f 6d 69 .FFF" may be omi
9cc20 74 74 65 64 2e 20 20 54 68 65 20 22 3a 53 53 2e tted. The ":SS.
9cc30 46 46 46 22 20 6d 61 79 20 62 65 0a 20 20 20 20 FFF" may be.
9cc40 20 20 20 20 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a ** omitted..
9cc50 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
9cc60 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
9cc70 32 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20 20 44 2 = z;. D
9cc80 61 74 65 54 69 6d 65 20 74 78 3b 0a 20 20 20 20 ateTime tx;.
9cc90 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 sqlite3_int6
9cca0 34 20 64 61 79 3b 0a 20 20 20 20 20 20 20 20 69 4 day;. i
9ccb0 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 f( !sqlite3Isdig
9ccc0 69 74 28 2a 7a 32 29 20 29 20 7a 32 2b 2b 3b 0a it(*z2) ) z2++;.
9ccd0 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 memset(&
9cce0 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 74 78 tx, 0, sizeof(tx
9ccf0 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ));. if(
9cd00 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 2c 20 parseHhMmSs(z2,
9cd10 26 74 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 &tx) ) break;.
9cd20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 computeJD(
9cd30 26 74 78 29 3b 0a 20 20 20 20 20 20 20 20 74 78 &tx);. tx
9cd40 2e 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30 30 .iJD -= 43200000
9cd50 3b 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d 20 ;. day =
9cd60 74 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 3b tx.iJD/86400000;
9cd70 0a 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 20 . tx.iJD
9cd80 2d 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30 3b -= day*86400000;
9cd90 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 30 . if( z[0
9cda0 5d 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a 44 20 ]=='-' ) tx.iJD
9cdb0 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 = -tx.iJD;.
9cdc0 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b computeJD(p);
9cdd0 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d . clearYM
9cde0 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 D_HMS_TZ(p);.
9cdf0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 74 p->iJD += t
9ce00 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 72 x.iJD;. r
9ce10 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 c = 0;. b
9ce20 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
9ce30 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20 20 z += n;.
9ce40 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
9ce50 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b Isspace(*z) ) z+
9ce60 2b 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c +;. n = sql
9ce70 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b ite3Strlen30(z);
9ce80 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20 . if( n>10
9ce90 7c 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a || n<3 ) break;.
9cea0 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d if( z[n-1]
9ceb0 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20 =='s' ){ z[n-1]
9cec0 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20 = 0; n--; }.
9ced0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
9cee0 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 rc = 0;.
9cef0 20 20 20 20 72 52 6f 75 6e 64 65 72 20 3d 20 72 rRounder = r
9cf00 3c 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30 2e 35 <0 ? -0.5 : +0.5
9cf10 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 33 ;. if( n==3
9cf20 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 && strcmp(z,"da
9cf30 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 y")==0 ){.
9cf40 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c p->iJD += (sql
9cf50 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 ite3_int64)(r*86
9cf60 34 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 400000.0 + rRoun
9cf70 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 der);. }els
9cf80 65 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 e if( n==4 && st
9cf90 72 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29 3d 3d rcmp(z,"hour")==
9cfa0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 0 ){. p->
9cfb0 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f iJD += (sqlite3_
9cfc0 69 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 int64)(r*(864000
9cfd0 30 30 2e 30 2f 32 34 2e 30 29 20 2b 20 72 52 6f 00.0/24.0) + rRo
9cfe0 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 under);. }e
9cff0 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 lse if( n==6 &&
9d000 73 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74 65 strcmp(z,"minute
9d010 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
9d020 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 p->iJD += (sqli
9d030 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 te3_int64)(r*(86
9d040 34 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 400000.0/(24.0*6
9d050 30 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 0.0)) + rRounder
9d060 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
9d070 66 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d f( n==6 && strcm
9d080 70 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30 p(z,"second")==0
9d090 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
9d0a0 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 JD += (sqlite3_i
9d0b0 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 nt64)(r*(8640000
9d0c0 30 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36 0.0/(24.0*60.0*6
9d0d0 30 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 0.0)) + rRounder
9d0e0 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
9d0f0 66 28 20 6e 3d 3d 35 20 26 26 20 73 74 72 63 6d f( n==5 && strcm
9d100 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 p(z,"month")==0
9d110 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 ){. int x
9d120 2c 20 79 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d , y;. com
9d130 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a puteYMD_HMS(p);.
9d140 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b 3d 20 p->M +=
9d150 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 (int)r;.
9d160 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d x = p->M>0 ? (p-
9d170 3e 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d >M-1)/12 : (p->M
9d180 2d 31 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20 -12)/12;.
9d190 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20 p->Y += x;.
9d1a0 20 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32 p->M -= x*12
9d1b0 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c ;. p->val
9d1c0 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 idJD = 0;.
9d1d0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
9d1e0 20 20 20 20 20 20 20 20 79 20 3d 20 28 69 6e 74 y = (int
9d1f0 29 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 )r;. if(
9d200 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 y!=r ){.
9d210 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c p->iJD += (sql
9d220 69 74 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d ite3_int64)((r -
9d230 20 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 30 30 y)*30.0*8640000
9d240 30 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0.0 + rRounder);
9d250 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
9d260 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 }else if( n==4
9d270 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 && strcmp(z,"yea
9d280 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 r")==0 ){.
9d290 20 20 69 6e 74 20 79 20 3d 20 28 69 6e 74 29 72 int y = (int)r
9d2a0 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 ;. comput
9d2b0 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 eYMD_HMS(p);.
9d2c0 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 79 3b 0a p->Y += y;.
9d2d0 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 p->valid
9d2e0 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 JD = 0;.
9d2f0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
9d300 20 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 if( y!=r )
9d310 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 {. p->i
9d320 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 JD += (sqlite3_i
9d330 6e 74 36 34 29 28 28 72 20 2d 20 79 29 2a 33 36 nt64)((r - y)*36
9d340 35 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 5.0*86400000.0 +
9d350 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 rRounder);.
9d360 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
9d370 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 e{. rc =
9d380 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 1;. }.
9d390 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a clearYMD_HMS_TZ
9d3a0 28 70 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b (p);. break
9d3b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
9d3c0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 ult: {. bre
9d3d0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
9d3e0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
9d3f0 0a 2a 2a 20 50 72 6f 63 65 73 73 20 74 69 6d 65 .** Process time
9d400 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
9d410 6e 74 73 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 nts. argv[0] is
9d420 20 61 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 a date-time sta
9d430 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d 20 61 mp..** argv[1] a
9d440 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 nd following are
9d450 20 6d 6f 64 69 66 69 65 72 73 2e 20 20 50 61 72 modifiers. Par
9d460 73 65 20 74 68 65 6d 20 61 6c 6c 20 61 6e 64 20 se them all and
9d470 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73 write.** the res
9d480 75 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e 74 6f ulting time into
9d490 20 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 the DateTime st
9d4a0 72 75 63 74 75 72 65 20 70 2e 20 20 52 65 74 75 ructure p. Retu
9d4b0 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 rn 0.** on succe
9d4c0 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 ss and 1 if ther
9d4d0 65 20 61 72 65 20 61 6e 79 20 65 72 72 6f 72 73 e are any errors
9d4e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 ..**.** If there
9d4f0 20 61 72 65 20 7a 65 72 6f 20 70 61 72 61 6d 65 are zero parame
9d500 74 65 72 73 20 28 69 66 20 65 76 65 6e 20 61 72 ters (if even ar
9d510 67 76 5b 30 5d 20 69 73 20 75 6e 64 65 66 69 6e gv[0] is undefin
9d520 65 64 29 0a 2a 2a 20 74 68 65 6e 20 61 73 73 75 ed).** then assu
9d530 6d 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c me a default val
9d540 75 65 20 6f 66 20 22 6e 6f 77 22 20 66 6f 72 20 ue of "now" for
9d550 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74 61 74 argv[0]..*/.stat
9d560 69 63 20 69 6e 74 20 69 73 44 61 74 65 28 0a 20 ic int isDate(.
9d570 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
9d580 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e *context, . in
9d590 74 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 t argc, . sqlit
9d5a0 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 2c e3_value **argv,
9d5b0 20 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a . DateTime *p.
9d5c0 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f ){. int i;. co
9d5d0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
9d5e0 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 54 79 70 r *z;. int eTyp
9d5f0 65 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 e;. memset(p, 0
9d600 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 , sizeof(*p));.
9d610 20 69 66 28 20 61 72 67 63 3d 3d 30 20 29 7b 0a if( argc==0 ){.
9d620 20 20 20 20 72 65 74 75 72 6e 20 73 65 74 44 61 return setDa
9d630 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 teTimeToCurrent(
9d640 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a 20 20 7d context, p);. }
9d650 0a 20 20 69 66 28 20 28 65 54 79 70 65 20 3d 20 . if( (eType =
9d660 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
9d670 70 65 28 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51 pe(argv[0]))==SQ
9d680 4c 49 54 45 5f 46 4c 4f 41 54 0a 20 20 20 20 20 LITE_FLOAT.
9d690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ||
9d6a0 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 eType==SQLITE_I
9d6b0 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 2d NTEGER ){. p-
9d6c0 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f >iJD = (sqlite3_
9d6d0 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 76 int64)(sqlite3_v
9d6e0 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 alue_double(argv
9d6f0 5b 30 5d 29 2a 38 36 34 30 30 30 30 30 2e 30 20 [0])*86400000.0
9d700 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70 2d 3e 76 + 0.5);. p->v
9d710 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 7d 65 alidJD = 1;. }e
9d720 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c lse{. z = sql
9d730 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
9d740 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 argv[0]);. if
9d750 28 20 21 7a 20 7c 7c 20 70 61 72 73 65 44 61 74 ( !z || parseDat
9d760 65 4f 72 54 69 6d 65 28 63 6f 6e 74 65 78 74 2c eOrTime(context,
9d770 20 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b (char*)z, p) ){
9d780 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
9d790 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 . }. }. for
9d7a0 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b (i=1; i<argc; i+
9d7b0 2b 29 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 +){. z = sqli
9d7c0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
9d7d0 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 rgv[i]);. if(
9d7e0 20 7a 3d 3d 30 20 7c 7c 20 70 61 72 73 65 4d 6f z==0 || parseMo
9d7f0 64 69 66 69 65 72 28 63 6f 6e 74 65 78 74 2c 20 difier(context,
9d800 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 20 72 (char*)z, p) ) r
9d810 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 eturn 1;. }. r
9d820 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a eturn 0;.}.../*.
9d830 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
9d840 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d routines implem
9d850 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 73 20 ent the various
9d860 64 61 74 65 20 61 6e 64 20 74 69 6d 65 20 66 75 date and time fu
9d870 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 nctions.** of SQ
9d880 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Lite..*/../*.**
9d890 20 20 20 6a 75 6c 69 61 6e 64 61 79 28 20 54 49 julianday( TI
9d8a0 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d MESTRING, MOD, M
9d8b0 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 OD, ...).**.** R
9d8c0 65 74 75 72 6e 20 74 68 65 20 6a 75 6c 69 61 6e eturn the julian
9d8d0 20 64 61 79 20 6e 75 6d 62 65 72 20 6f 66 20 74 day number of t
9d8e0 68 65 20 64 61 74 65 20 73 70 65 63 69 66 69 65 he date specifie
9d8f0 64 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e d in the argumen
9d900 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ts.*/.static voi
9d910 64 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 28 d juliandayFunc(
9d920 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
9d930 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
9d940 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 nt argc,. sqlit
9d950 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
9d960 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b ){. DateTime x;
9d970 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f . if( isDate(co
9d980 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 ntext, argc, arg
9d990 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 v, &x)==0 ){.
9d9a0 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a computeJD(&x);.
9d9b0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
9d9c0 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 lt_double(contex
9d9d0 74 2c 20 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 t, x.iJD/8640000
9d9e0 30 2e 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 0.0);. }.}../*.
9d9f0 2a 2a 20 20 20 20 64 61 74 65 74 69 6d 65 28 20 ** datetime(
9da00 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c TIMESTRING, MOD,
9da10 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a MOD, ...).**.**
9da20 20 52 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d Return YYYY-MM-
9da30 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 DD HH:MM:SS.*/.s
9da40 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 65 74 tatic void datet
9da50 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 imeFunc(. sqlit
9da60 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
9da70 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c ext,. int argc,
9da80 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
9da90 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 **argv.){. Dat
9daa0 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 eTime x;. if( i
9dab0 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 sDate(context, a
9dac0 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d rgc, argv, &x)==
9dad0 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 0 ){. char zB
9dae0 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d uf[100];. com
9daf0 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b puteYMD_HMS(&x);
9db00 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
9db10 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 rintf(sizeof(zBu
9db20 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d f), zBuf, "%04d-
9db30 25 30 32 64 2d 25 30 32 64 20 25 30 32 64 3a 25 %02d-%02d %02d:%
9db40 30 32 64 3a 25 30 32 64 22 2c 0a 20 20 20 20 20 02d:%02d",.
9db50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9db60 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c 20 78 x.Y, x.M, x.D, x
9db70 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 28 78 .h, x.m, (int)(x
9db80 2e 73 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 .s));. sqlite
9db90 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 3_result_text(co
9dba0 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c ntext, zBuf, -1,
9dbb0 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
9dbc0 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a T);. }.}../*.**
9dbd0 20 20 20 20 74 69 6d 65 28 20 54 49 4d 45 53 54 time( TIMEST
9dbe0 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 RING, MOD, MOD,
9dbf0 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ...).**.** Retur
9dc00 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 n HH:MM:SS.*/.st
9dc10 61 74 69 63 20 76 6f 69 64 20 74 69 6d 65 46 75 atic void timeFu
9dc20 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
9dc30 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
9dc40 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
9dc50 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
9dc60 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 gv.){. DateTime
9dc70 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 x;. if( isDate
9dc80 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 (context, argc,
9dc90 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a argv, &x)==0 ){.
9dca0 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 char zBuf[10
9dcb0 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 48 0];. computeH
9dcc0 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 MS(&x);. sqli
9dcd0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
9dce0 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c eof(zBuf), zBuf,
9dcf0 20 22 25 30 32 64 3a 25 30 32 64 3a 25 30 32 64 "%02d:%02d:%02d
9dd00 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e ", x.h, x.m, (in
9dd10 74 29 78 2e 73 29 3b 0a 20 20 20 20 73 71 6c 69 t)x.s);. sqli
9dd20 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
9dd30 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d context, zBuf, -
9dd40 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 1, SQLITE_TRANSI
9dd50 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ENT);. }.}../*.
9dd60 2a 2a 20 20 20 20 64 61 74 65 28 20 54 49 4d 45 ** date( TIME
9dd70 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 STRING, MOD, MOD
9dd80 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 , ...).**.** Ret
9dd90 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a urn YYYY-MM-DD.*
9dda0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 /.static void da
9ddb0 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 teFunc(. sqlite
9ddc0 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
9ddd0 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a xt,. int argc,.
9dde0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
9ddf0 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 **argv.){. Date
9de00 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 Time x;. if( is
9de10 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 Date(context, ar
9de20 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 gc, argv, &x)==0
9de30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 ){. char zBu
9de40 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 f[100];. comp
9de50 75 74 65 59 4d 44 28 26 78 29 3b 0a 20 20 20 20 uteYMD(&x);.
9de60 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
9de70 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a (sizeof(zBuf), z
9de80 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d Buf, "%04d-%02d-
9de90 25 30 32 64 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c %02d", x.Y, x.M,
9dea0 20 78 2e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 x.D);. sqlit
9deb0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 e3_result_text(c
9dec0 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 ontext, zBuf, -1
9ded0 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 , SQLITE_TRANSIE
9dee0 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a NT);. }.}../*.*
9def0 2a 20 20 20 20 73 74 72 66 74 69 6d 65 28 20 46 * strftime( F
9df00 4f 52 4d 41 54 2c 20 54 49 4d 45 53 54 52 49 4e ORMAT, TIMESTRIN
9df10 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e G, MOD, MOD, ...
9df20 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ).**.** Return a
9df30 20 73 74 72 69 6e 67 20 64 65 73 63 72 69 62 65 string describe
9df40 64 20 62 79 20 46 4f 52 4d 41 54 2e 20 20 43 6f d by FORMAT. Co
9df50 6e 76 65 72 73 69 6f 6e 73 20 61 73 20 66 6f 6c nversions as fol
9df60 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 25 64 lows:.**.** %d
9df70 20 20 64 61 79 20 6f 66 20 6d 6f 6e 74 68 0a 2a day of month.*
9df80 2a 20 20 20 25 66 20 20 2a 2a 20 66 72 61 63 74 * %f ** fract
9df90 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 20 53 ional seconds S
9dfa0 53 2e 53 53 53 0a 2a 2a 20 20 20 25 48 20 20 68 S.SSS.** %H h
9dfb0 6f 75 72 20 30 30 2d 32 34 0a 2a 2a 20 20 20 25 our 00-24.** %
9dfc0 6a 20 20 64 61 79 20 6f 66 20 79 65 61 72 20 30 j day of year 0
9dfd0 30 30 2d 33 36 36 0a 2a 2a 20 20 20 25 4a 20 20 00-366.** %J
9dfe0 2a 2a 20 4a 75 6c 69 61 6e 20 64 61 79 20 6e 75 ** Julian day nu
9dff0 6d 62 65 72 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f mber.** %m mo
9e000 6e 74 68 20 30 31 2d 31 32 0a 2a 2a 20 20 20 25 nth 01-12.** %
9e010 4d 20 20 6d 69 6e 75 74 65 20 30 30 2d 35 39 0a M minute 00-59.
9e020 2a 2a 20 20 20 25 73 20 20 73 65 63 6f 6e 64 73 ** %s seconds
9e030 20 73 69 6e 63 65 20 31 39 37 30 2d 30 31 2d 30 since 1970-01-0
9e040 31 0a 2a 2a 20 20 20 25 53 20 20 73 65 63 6f 6e 1.** %S secon
9e050 64 73 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 77 ds 00-59.** %w
9e060 20 20 64 61 79 20 6f 66 20 77 65 65 6b 20 30 2d day of week 0-
9e070 36 20 20 73 75 6e 64 61 79 3d 3d 30 0a 2a 2a 20 6 sunday==0.**
9e080 20 20 25 57 20 20 77 65 65 6b 20 6f 66 20 79 65 %W week of ye
9e090 61 72 20 30 30 2d 35 33 0a 2a 2a 20 20 20 25 59 ar 00-53.** %Y
9e0a0 20 20 79 65 61 72 20 30 30 30 30 2d 39 39 39 39 year 0000-9999
9e0b0 0a 2a 2a 20 20 20 25 25 20 20 25 0a 2a 2f 0a 73 .** %% %.*/.s
9e0c0 74 61 74 69 63 20 76 6f 69 64 20 73 74 72 66 74 tatic void strft
9e0d0 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 imeFunc(. sqlit
9e0e0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
9e0f0 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c ext,. int argc,
9e100 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
9e110 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 **argv.){. Dat
9e120 65 54 69 6d 65 20 78 3b 0a 20 20 75 36 34 20 6e eTime x;. u64 n
9e130 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 6a 3b 0a ;. size_t i,j;.
9e140 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71 6c char *z;. sql
9e150 69 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 ite3 *db;. cons
9e160 74 20 63 68 61 72 20 2a 7a 46 6d 74 20 3d 20 28 t char *zFmt = (
9e170 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 const char*)sqli
9e180 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
9e190 72 67 76 5b 30 5d 29 3b 0a 20 20 63 68 61 72 20 rgv[0]);. char
9e1a0 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 69 66 28 zBuf[100];. if(
9e1b0 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 44 61 zFmt==0 || isDa
9e1c0 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 te(context, argc
9e1d0 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78 29 20 -1, argv+1, &x)
9e1e0 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d ) return;. db =
9e1f0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
9e200 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 _db_handle(conte
9e210 78 74 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 xt);. for(i=0,
9e220 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b n=1; zFmt[i]; i+
9e230 2b 2c 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 66 28 +, n++){. if(
9e240 20 7a 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 29 7b zFmt[i]=='%' ){
9e250 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20 7a . switch( z
9e260 46 6d 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 20 20 Fmt[i+1] ){.
9e270 20 20 20 20 63 61 73 65 20 27 64 27 3a 0a 20 20 case 'd':.
9e280 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a 0a case 'H':.
9e290 20 20 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 case 'm'
9e2a0 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 :. case '
9e2b0 4d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 M':. case
9e2c0 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20 63 61 'S':. ca
9e2d0 73 65 20 27 57 27 3a 0a 20 20 20 20 20 20 20 20 se 'W':.
9e2e0 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 n++;.
9e2f0 20 2f 2a 20 66 61 6c 6c 20 74 68 72 75 20 2a 2f /* fall thru */
9e300 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 77 . case 'w
9e310 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 ':. case
9e320 27 25 27 3a 0a 20 20 20 20 20 20 20 20 20 20 62 '%':. b
9e330 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 reak;. ca
9e340 73 65 20 27 66 27 3a 0a 20 20 20 20 20 20 20 20 se 'f':.
9e350 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20 20 n += 8;.
9e360 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
9e370 20 20 20 63 61 73 65 20 27 6a 27 3a 0a 20 20 20 case 'j':.
9e380 20 20 20 20 20 20 20 6e 20 2b 3d 20 33 3b 0a 20 n += 3;.
9e390 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
9e3a0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 59 27 case 'Y'
9e3b0 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d :. n +=
9e3c0 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 8;. br
9e3d0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
9e3e0 65 20 27 73 27 3a 0a 20 20 20 20 20 20 20 20 63 e 's':. c
9e3f0 61 73 65 20 27 4a 27 3a 0a 20 20 20 20 20 20 20 ase 'J':.
9e400 20 20 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 20 20 n += 50;.
9e410 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
9e420 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 default:.
9e430 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 20 return;
9e440 20 2f 2a 20 45 52 52 4f 52 2e 20 20 72 65 74 75 /* ERROR. retu
9e450 72 6e 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 rn a NULL */.
9e460 20 20 20 7d 0a 20 20 20 20 20 20 69 2b 2b 3b 0a }. i++;.
9e470 20 20 20 20 7d 0a 20 20 7d 0a 20 20 74 65 73 74 }. }. test
9e480 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 case( n==sizeof(
9e490 7a 42 75 66 29 2d 31 20 29 3b 0a 20 20 74 65 73 zBuf)-1 );. tes
9e4a0 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 tcase( n==sizeof
9e4b0 28 7a 42 75 66 29 20 29 3b 0a 20 20 74 65 73 74 (zBuf) );. test
9e4c0 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29 64 62 case( n==(u64)db
9e4d0 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
9e4e0 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 LIMIT_LENGTH]+1
9e4f0 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e );. testcase( n
9e500 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 ==(u64)db->aLimi
9e510 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
9e520 45 4e 47 54 48 5d 20 29 3b 0a 20 20 69 66 28 20 ENGTH] );. if(
9e530 6e 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 29 n<sizeof(zBuf) )
9e540 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a {. z = zBuf;.
9e550 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e 28 75 }else if( n>(u
9e560 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 64)db->aLimit[SQ
9e570 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
9e580 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 H] ){. sqlite
9e590 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 3_result_error_t
9e5a0 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a oobig(context);.
9e5b0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 return;. }e
9e5c0 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c lse{. z = sql
9e5d0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
9e5e0 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 20 db, (int)n);.
9e5f0 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 if( z==0 ){.
9e600 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
9e610 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f t_error_nomem(co
9e620 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 72 65 ntext);. re
9e630 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a turn;. }. }.
9e640 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b computeJD(&x);
9e650 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d . computeYMD_HM
9e660 53 28 26 78 29 3b 0a 20 20 66 6f 72 28 69 3d 6a S(&x);. for(i=j
9e670 3d 30 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b =0; zFmt[i]; i++
9e680 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b ){. if( zFmt[
9e690 69 5d 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 i]!='%' ){.
9e6a0 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 5b 69 z[j++] = zFmt[i
9e6b0 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 ];. }else{.
9e6c0 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 73 i++;. s
9e6d0 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 5d 20 29 witch( zFmt[i] )
9e6e0 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 {. case '
9e6f0 64 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 d': sqlite3_snp
9e700 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 rintf(3, &z[j],"
9e710 25 30 32 64 22 2c 78 2e 44 29 3b 20 6a 2b 3d 32 %02d",x.D); j+=2
9e720 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ; break;.
9e730 20 63 61 73 65 20 27 66 27 3a 20 7b 0a 20 20 20 case 'f': {.
9e740 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20 73 20 double s
9e750 3d 20 78 2e 73 3b 0a 20 20 20 20 20 20 20 20 20 = x.s;.
9e760 20 69 66 28 20 73 3e 35 39 2e 39 39 39 20 29 20 if( s>59.999 )
9e770 73 20 3d 20 35 39 2e 39 39 39 3b 0a 20 20 20 20 s = 59.999;.
9e780 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
9e790 70 72 69 6e 74 66 28 37 2c 20 26 7a 5b 6a 5d 2c printf(7, &z[j],
9e7a0 22 25 30 36 2e 33 66 22 2c 20 73 29 3b 0a 20 20 "%06.3f", s);.
9e7b0 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c j += sql
9e7c0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b ite3Strlen30(&z[
9e7d0 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 j]);. b
9e7e0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a reak;. }.
9e7f0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 48 27 case 'H'
9e800 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 : sqlite3_snpri
9e810 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 ntf(3, &z[j],"%0
9e820 32 64 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 2d",x.h); j+=2;
9e830 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
9e840 61 73 65 20 27 57 27 3a 20 2f 2a 20 46 61 6c 6c ase 'W': /* Fall
9e850 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20 thru */.
9e860 20 63 61 73 65 20 27 6a 27 3a 20 7b 0a 20 20 20 case 'j': {.
9e870 20 20 20 20 20 20 20 69 6e 74 20 6e 44 61 79 3b int nDay;
9e880 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
9e890 4e 75 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73 Number of days s
9e8a0 69 6e 63 65 20 31 73 74 20 64 61 79 20 6f 66 20 ince 1st day of
9e8b0 79 65 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 year */.
9e8c0 20 20 44 61 74 65 54 69 6d 65 20 79 20 3d 20 78 DateTime y = x
9e8d0 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e 76 61 ;. y.va
9e8e0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 lidJD = 0;.
9e8f0 20 20 20 20 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 y.M = 1;.
9e900 20 20 20 20 20 20 20 20 79 2e 44 20 3d 20 31 3b y.D = 1;
9e910 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6d 70 75 . compu
9e920 74 65 4a 44 28 26 79 29 3b 0a 20 20 20 20 20 20 teJD(&y);.
9e930 20 20 20 20 6e 44 61 79 20 3d 20 28 69 6e 74 29 nDay = (int)
9e940 28 28 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b 34 33 ((x.iJD-y.iJD+43
9e950 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 200000)/86400000
9e960 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
9e970 20 7a 46 6d 74 5b 69 5d 3d 3d 27 57 27 20 29 7b zFmt[i]=='W' ){
9e980 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 . int
9e990 20 77 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f 6e 64 wd; /* 0=Mond
9e9a0 61 79 2c 20 31 3d 54 75 65 73 64 61 79 2c 20 2e ay, 1=Tuesday, .
9e9b0 2e 2e 20 36 3d 53 75 6e 64 61 79 20 2a 2f 0a 20 .. 6=Sunday */.
9e9c0 20 20 20 20 20 20 20 20 20 20 20 77 64 20 3d 20 wd =
9e9d0 28 69 6e 74 29 28 28 28 78 2e 69 4a 44 2b 34 33 (int)(((x.iJD+43
9e9e0 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 200000)/86400000
9e9f0 29 25 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 )%7);.
9ea00 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
9ea10 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 tf(3, &z[j],"%02
9ea20 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64 29 2f 37 d",(nDay+7-wd)/7
9ea30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a );. j
9ea40 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 20 += 2;.
9ea50 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
9ea60 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
9ea70 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c 22 25 intf(4, &z[j],"%
9ea80 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b 0a 20 20 03d",nDay+1);.
9ea90 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 33 j += 3
9eaa0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
9eab0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
9eac0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
9ead0 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a 20 20 20 case 'J': {.
9eae0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
9eaf0 6e 70 72 69 6e 74 66 28 32 30 2c 20 26 7a 5b 6a nprintf(20, &z[j
9eb00 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 69 4a 44 2f ],"%.16g",x.iJD/
9eb10 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 86400000.0);.
9eb20 20 20 20 20 20 20 20 6a 2b 3d 73 71 6c 69 74 65 j+=sqlite
9eb30 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3Strlen30(&z[j])
9eb40 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
9eb50 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
9eb60 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a 20 20 case 'm':
9eb70 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
9eb80 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 (3, &z[j],"%02d"
9eb90 2c 78 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 ,x.M); j+=2; bre
9eba0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
9ebb0 20 27 4d 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 'M': sqlite3_s
9ebc0 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d nprintf(3, &z[j]
9ebd0 2c 22 25 30 32 64 22 2c 78 2e 6d 29 3b 20 6a 2b ,"%02d",x.m); j+
9ebe0 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 =2; break;.
9ebf0 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 case 's': {.
9ec00 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
9ec10 5f 73 6e 70 72 69 6e 74 66 28 33 30 2c 26 7a 5b _snprintf(30,&z[
9ec20 6a 5d 2c 22 25 6c 6c 64 22 2c 0a 20 20 20 20 20 j],"%lld",.
9ec30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ec40 20 20 20 20 20 20 28 69 36 34 29 28 78 2e 69 4a (i64)(x.iJ
9ec50 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 D/1000 - 2108667
9ec60 36 2a 28 69 36 34 29 31 30 30 30 30 29 29 3b 0a 6*(i64)10000));.
9ec70 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 j += s
9ec80 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 qlite3Strlen30(&
9ec90 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 z[j]);.
9eca0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
9ecb0 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 }. case '
9ecc0 53 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 S': sqlite3_snp
9ecd0 72 69 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 rintf(3,&z[j],"%
9ece0 30 32 64 22 2c 28 69 6e 74 29 78 2e 73 29 3b 20 02d",(int)x.s);
9ecf0 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 j+=2; break;.
9ed00 20 20 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b case 'w': {
9ed10 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b . z[j++
9ed20 5d 20 3d 20 28 63 68 61 72 29 28 28 28 78 2e 69 ] = (char)(((x.i
9ed30 4a 44 2b 31 32 39 36 30 30 30 30 30 29 2f 38 36 JD+129600000)/86
9ed40 34 30 30 30 30 30 29 20 25 20 37 29 20 2b 20 27 400000) % 7) + '
9ed50 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 0';. br
9ed60 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
9ed70 20 20 20 20 20 20 20 63 61 73 65 20 27 59 27 3a case 'Y':
9ed80 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c {. sql
9ed90 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 2c ite3_snprintf(5,
9eda0 26 7a 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 2e 59 &z[j],"%04d",x.Y
9edb0 29 3b 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 ); j+=sqlite3Str
9edc0 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 len30(&z[j]);.
9edd0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
9ede0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
9edf0 20 64 65 66 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b default: z[j+
9ee00 2b 5d 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b +] = '%'; break;
9ee10 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
9ee20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 }. z[j] = 0;.
9ee30 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
9ee40 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c text(context, z,
9ee50 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 -1,.
9ee60 20 20 20 20 20 20 20 20 20 20 20 7a 3d 3d 7a 42 z==zB
9ee70 75 66 20 3f 20 53 51 4c 49 54 45 5f 54 52 41 4e uf ? SQLITE_TRAN
9ee80 53 49 45 4e 54 20 3a 20 53 51 4c 49 54 45 5f 44 SIENT : SQLITE_D
9ee90 59 4e 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a YNAMIC);.}../*.*
9eea0 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 * current_time()
9eeb0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
9eec0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
9eed0 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 same value as t
9eee0 69 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 ime('now')..*/.s
9eef0 74 61 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 tatic void ctime
9ef00 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
9ef10 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
9ef20 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c ,. int NotUsed,
9ef30 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
9ef40 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 **NotUsed2.){.
9ef50 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
9ef60 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 R2(NotUsed, NotU
9ef70 73 65 64 32 29 3b 0a 20 20 74 69 6d 65 46 75 6e sed2);. timeFun
9ef80 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 c(context, 0, 0)
9ef90 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 ;.}../*.** curre
9efa0 6e 74 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 nt_date().**.**
9efb0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
9efc0 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 turns the same v
9efd0 61 6c 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f alue as date('no
9efe0 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 w')..*/.static v
9eff0 6f 69 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20 oid cdateFunc(.
9f000 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
9f010 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
9f020 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 NotUsed,. sqli
9f030 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 te3_value **NotU
9f040 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 sed2.){. UNUSED
9f050 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 _PARAMETER2(NotU
9f060 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a sed, NotUsed2);.
9f070 20 20 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65 dateFunc(conte
9f080 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a xt, 0, 0);.}../*
9f090 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 .** current_time
9f0a0 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 stamp().**.** Th
9f0b0 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
9f0c0 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c rns the same val
9f0d0 75 65 20 61 73 20 64 61 74 65 74 69 6d 65 28 27 ue as datetime('
9f0e0 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 now')..*/.static
9f0f0 20 76 6f 69 64 20 63 74 69 6d 65 73 74 61 6d 70 void ctimestamp
9f100 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
9f110 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
9f120 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c ,. int NotUsed,
9f130 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
9f140 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 **NotUsed2.){.
9f150 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
9f160 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 R2(NotUsed, NotU
9f170 73 65 64 32 29 3b 0a 20 20 64 61 74 65 74 69 6d sed2);. datetim
9f180 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 eFunc(context, 0
9f190 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f , 0);.}.#endif /
9f1a0 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 * !defined(SQLIT
9f1b0 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f E_OMIT_DATETIME_
9f1c0 46 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64 65 FUNCS) */..#ifde
9f1d0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 f SQLITE_OMIT_DA
9f1e0 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a TETIME_FUNCS./*.
9f1f0 2a 2a 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 ** If the librar
9f200 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f y is compiled to
9f210 20 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d 73 omit the full-s
9f220 63 61 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 cale date and ti
9f230 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20 28 me.** handling (
9f240 74 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c 65 72 to get a smaller
9f250 20 62 69 6e 61 72 79 29 2c 20 74 68 65 20 66 6f binary), the fo
9f260 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 llowing minimal
9f270 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 version.** of th
9f280 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 75 72 72 e functions curr
9f290 65 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75 72 72 ent_time(), curr
9f2a0 65 6e 74 5f 64 61 74 65 28 29 20 61 6e 64 20 63 ent_date() and c
9f2b0 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 urrent_timestamp
9f2c0 28 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 ().** are includ
9f2d0 65 64 20 69 6e 73 74 65 61 64 2e 20 54 68 69 73 ed instead. This
9f2e0 20 69 73 20 74 6f 20 73 75 70 70 6f 72 74 20 63 is to support c
9f2f0 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f olumn declaratio
9f300 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 ns that.** inclu
9f310 64 65 20 22 44 45 46 41 55 4c 54 20 43 55 52 52 de "DEFAULT CURR
9f320 45 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a 2a ENT_TIME" etc..*
9f330 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
9f340 6f 6e 20 75 73 65 73 20 74 68 65 20 43 2d 6c 69 on uses the C-li
9f350 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 brary functions
9f360 74 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28 29 time(), gmtime()
9f370 0a 2a 2a 20 61 6e 64 20 73 74 72 66 74 69 6d 65 .** and strftime
9f380 28 29 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 73 (). The format s
9f390 74 72 69 6e 67 20 74 6f 20 70 61 73 73 20 74 6f tring to pass to
9f3a0 20 73 74 72 66 74 69 6d 65 28 29 20 69 73 20 73 strftime() is s
9f3b0 75 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74 68 upplied.** as th
9f3c0 65 20 75 73 65 72 2d 64 61 74 61 20 66 6f 72 20 e user-data for
9f3d0 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f the function..*/
9f3e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 75 72 .static void cur
9f3f0 72 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20 20 rentTimeFunc(.
9f400 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
9f410 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 *context,. int
9f420 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f argc,. sqlite3_
9f430 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a value **argv.){.
9f440 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63 68 time_t t;. ch
9f450 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28 63 ar *zFormat = (c
9f460 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 har *)sqlite3_us
9f470 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 er_data(context)
9f480 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b ;. sqlite3 *db;
9f490 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
9f4a0 20 69 54 3b 0a 20 20 73 74 72 75 63 74 20 74 6d iT;. struct tm
9f4b0 20 2a 70 54 6d 3b 0a 20 20 73 74 72 75 63 74 20 *pTm;. struct
9f4c0 74 6d 20 73 4e 6f 77 3b 0a 20 20 63 68 61 72 20 tm sNow;. char
9f4d0 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20 55 4e 55 zBuf[20];.. UNU
9f4e0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 SED_PARAMETER(ar
9f4f0 67 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 gc);. UNUSED_PA
9f500 52 41 4d 45 54 45 52 28 61 72 67 76 29 3b 0a 0a RAMETER(argv);..
9f510 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 db = sqlite3_c
9f520 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
9f530 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28 (context);. if(
9f540 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e sqlite3OsCurren
9f550 74 54 69 6d 65 49 6e 74 36 34 28 64 62 2d 3e 70 tTimeInt64(db->p
9f560 56 66 73 2c 20 26 69 54 29 20 29 20 72 65 74 75 Vfs, &iT) ) retu
9f570 72 6e 3b 0a 20 20 74 20 3d 20 69 54 2f 31 30 30 rn;. t = iT/100
9f580 30 20 2d 20 31 30 30 30 30 2a 28 73 71 6c 69 74 0 - 10000*(sqlit
9f590 65 33 5f 69 6e 74 36 34 29 32 31 30 38 36 36 37 e3_int64)2108667
9f5a0 36 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f 47 6;.#ifdef HAVE_G
9f5b0 4d 54 49 4d 45 5f 52 0a 20 20 70 54 6d 20 3d 20 MTIME_R. pTm =
9f5c0 67 6d 74 69 6d 65 5f 72 28 26 74 2c 20 26 73 4e gmtime_r(&t, &sN
9f5d0 6f 77 29 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c ow);.#else. sql
9f5e0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
9f5f0 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c (sqlite3MutexAll
9f600 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
9f610 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b STATIC_MASTER));
9f620 0a 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28 . pTm = gmtime(
9f630 26 74 29 3b 0a 20 20 69 66 28 20 70 54 6d 20 29 &t);. if( pTm )
9f640 20 6d 65 6d 63 70 79 28 26 73 4e 6f 77 2c 20 70 memcpy(&sNow, p
9f650 54 6d 2c 20 73 69 7a 65 6f 66 28 73 4e 6f 77 29 Tm, sizeof(sNow)
9f660 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
9f670 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 ex_leave(sqlite3
9f680 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
9f690 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
9f6a0 41 53 54 45 52 29 29 3b 0a 23 65 6e 64 69 66 0a ASTER));.#endif.
9f6b0 20 20 69 66 28 20 70 54 6d 20 29 7b 0a 20 20 20 if( pTm ){.
9f6c0 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 strftime(zBuf,
9f6d0 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e 20, zFormat, &sN
9f6e0 6f 77 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ow);. sqlite3
9f6f0 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
9f700 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 text, zBuf, -1,
9f710 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
9f720 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a );. }.}.#endif.
9f730 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
9f740 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 tion registered
9f750 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 all of the above
9f760 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 C functions as
9f770 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 SQL.** functions
9f780 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 . This should b
9f790 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 e the only routi
9f7a0 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 ne in this file
9f7b0 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 6c with.** external
9f7c0 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c linkage..*/.SQL
9f7d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
9f7e0 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 sqlite3Register
9f7f0 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e DateTimeFunction
9f800 73 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 s(void){. stati
9f810 63 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e c SQLITE_WSD Fun
9f820 63 44 65 66 20 61 44 61 74 65 54 69 6d 65 46 75 cDef aDateTimeFu
9f830 6e 63 73 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64 65 ncs[] = {.#ifnde
9f840 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 f SQLITE_OMIT_DA
9f850 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 20 20 20 TETIME_FUNCS.
9f860 20 46 55 4e 43 54 49 4f 4e 28 6a 75 6c 69 61 6e FUNCTION(julian
9f870 64 61 79 2c 20 20 20 20 20 20 20 20 2d 31 2c 20 day, -1,
9f880 30 2c 20 30 2c 20 6a 75 6c 69 61 6e 64 61 79 46 0, 0, juliandayF
9f890 75 6e 63 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 unc ),. FUNCT
9f8a0 49 4f 4e 28 64 61 74 65 2c 20 20 20 20 20 20 20 ION(date,
9f8b0 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 -1, 0, 0,
9f8c0 64 61 74 65 46 75 6e 63 20 20 20 20 20 20 29 2c dateFunc ),
9f8d0 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 69 . FUNCTION(ti
9f8e0 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 me,
9f8f0 2d 31 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46 75 -1, 0, 0, timeFu
9f900 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 nc ),. F
9f910 55 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d 65 UNCTION(datetime
9f920 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c , -1, 0,
9f930 20 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e 63 0, datetimeFunc
9f940 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
9f950 4e 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20 20 N(strftime,
9f960 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73 74 -1, 0, 0, st
9f970 72 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0a 20 rftimeFunc ),.
9f980 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 FUNCTION(curr
9f990 65 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 ent_time, 0
9f9a0 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75 6e , 0, 0, ctimeFun
9f9b0 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e c ),. FUN
9f9c0 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 CTION(current_ti
9f9d0 6d 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c 20 30 mestamp, 0, 0, 0
9f9e0 2c 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 , ctimestampFunc
9f9f0 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ),. FUNCTION(
9fa00 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 20 20 current_date,
9fa10 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 64 61 74 0, 0, 0, cdat
9fa20 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 23 65 6c eFunc ),.#el
9fa30 73 65 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54 se. STR_FUNCT
9fa40 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 ION(current_time
9fa50 2c 20 20 20 20 20 20 30 2c 20 22 25 48 3a 25 4d , 0, "%H:%M
9fa60 3a 25 53 22 2c 20 20 20 20 20 20 20 20 20 20 30 :%S", 0
9fa70 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e , currentTimeFun
9fa80 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 c),. STR_FUNC
9fa90 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 TION(current_dat
9faa0 65 2c 20 20 20 20 20 20 30 2c 20 22 25 59 2d 25 e, 0, "%Y-%
9fab0 6d 2d 25 64 22 2c 20 20 20 20 20 20 20 20 20 20 m-%d",
9fac0 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 0, currentTimeFu
9fad0 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e nc),. STR_FUN
9fae0 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 CTION(current_ti
9faf0 6d 65 73 74 61 6d 70 2c 20 30 2c 20 22 25 59 2d mestamp, 0, "%Y-
9fb00 25 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22 2c %m-%d %H:%M:%S",
9fb10 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 0, currentTimeF
9fb20 75 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d unc),.#endif. }
9fb30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75 6e ;. int i;. Fun
9fb40 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 20 cDefHash *pHash
9fb50 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 = &GLOBAL(FuncDe
9fb60 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c fHash, sqlite3Gl
9fb70 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a obalFunctions);.
9fb80 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e 63 FuncDef *aFunc
9fb90 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47 4c = (FuncDef*)&GL
9fba0 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61 44 OBAL(FuncDef, aD
9fbb0 61 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 0a 0a ateTimeFuncs);..
9fbc0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 for(i=0; i<Arr
9fbd0 61 79 53 69 7a 65 28 61 44 61 74 65 54 69 6d 65 aySize(aDateTime
9fbe0 46 75 6e 63 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 Funcs); i++){.
9fbf0 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 sqlite3FuncDef
9fc00 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26 61 Insert(pHash, &a
9fc10 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a Func[i]);. }.}.
9fc20 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
9fc30 20 45 6e 64 20 6f 66 20 64 61 74 65 2e 63 20 2a End of date.c *
9fc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
9fc70 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
9fc80 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e 63 Begin file os.c
9fc90 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
9fca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
9fcc0 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76 65 ./*.** 2005 Nove
9fcd0 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68 mber 29.**.** Th
9fce0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
9fcf0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
9fd00 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
9fd10 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
9fd20 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
9fd30 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
9fd40 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
9fd50 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
9fd60 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
9fd70 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
9fd80 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
9fd90 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
9fda0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
9fdb0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
9fdc0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
9fdd0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
9fde0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
9fdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fe00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fe10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fe20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9fe30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
9fe40 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
9fe50 61 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61 63 ains OS interfac
9fe60 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 e code that is c
9fe70 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 ommon to all.**
9fe80 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a 2a architectures..*
9fe90 2f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 /.#define _SQLIT
9fea0 45 5f 4f 53 5f 43 5f 20 31 0a 23 75 6e 64 65 66 E_OS_C_ 1.#undef
9feb0 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 0a 0a _SQLITE_OS_C_..
9fec0 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c /*.** The defaul
9fed0 74 20 53 51 4c 69 74 65 20 73 71 6c 69 74 65 33 t SQLite sqlite3
9fee0 5f 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 _vfs implementat
9fef0 69 6f 6e 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f ions do not allo
9ff00 63 61 74 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 28 cate.** memory (
9ff10 61 63 74 75 61 6c 6c 79 2c 20 6f 73 5f 75 6e 69 actually, os_uni
9ff20 78 2e 63 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 x.c allocates a
9ff30 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 small amount of
9ff40 6d 65 6d 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 77 memory.** from w
9ff50 69 74 68 69 6e 20 4f 73 4f 70 65 6e 28 29 29 2c ithin OsOpen()),
9ff60 20 62 75 74 20 73 6f 6d 65 20 74 68 69 72 64 2d but some third-
9ff70 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 party implementa
9ff80 74 69 6f 6e 73 20 6d 61 79 2e 0a 2a 2a 20 53 6f tions may..** So
9ff90 20 77 65 20 74 65 73 74 20 74 68 65 20 65 66 66 we test the eff
9ffa0 65 63 74 73 20 6f 66 20 61 20 6d 61 6c 6c 6f 63 ects of a malloc
9ffb0 28 29 20 66 61 69 6c 69 6e 67 20 61 6e 64 20 74 () failing and t
9ffc0 68 65 20 73 71 6c 69 74 65 33 4f 73 58 58 58 28 he sqlite3OsXXX(
9ffd0 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 ).** function re
9ffe0 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 49 turning SQLITE_I
9fff0 4f 45 52 52 5f 4e 4f 4d 45 4d 20 75 73 69 6e 67 OERR_NOMEM using
a0000 20 74 68 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f the DO_OS_MALLO
a0010 43 5f 54 45 53 54 20 6d 61 63 72 6f 2e 0a 2a 2a C_TEST macro..**
a0020 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
a0030 67 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 g functions are
a0040 69 6e 73 74 72 75 6d 65 6e 74 65 64 20 66 6f 72 instrumented for
a0050 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 malloc() failur
a0060 65 20 0a 2a 2a 20 74 65 73 74 69 6e 67 3a 0a 2a e .** testing:.*
a0070 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 *.** sqlite3
a0080 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 20 20 20 OsRead().**
a0090 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 sqlite3OsWrite()
a00a0 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f .** sqlite3O
a00b0 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 20 20 73 sSync().** s
a00c0 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 qlite3OsFileSize
a00d0 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 ().** sqlite
a00e0 33 4f 73 4c 6f 63 6b 28 29 0a 2a 2a 20 20 20 20 3OsLock().**
a00f0 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 sqlite3OsCheckR
a0100 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 0a 2a 2a eservedLock().**
a0110 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69 sqlite3OsFi
a0120 6c 65 43 6f 6e 74 72 6f 6c 28 29 0a 2a 2a 20 20 leControl().**
a0130 20 20 20 73 71 6c 69 74 65 33 4f 73 53 68 6d 4d sqlite3OsShmM
a0140 61 70 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 ap().** sqli
a0150 74 65 33 4f 73 4f 70 65 6e 28 29 0a 2a 2a 20 20 te3OsOpen().**
a0160 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 sqlite3OsDele
a0170 74 65 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 te().** sqli
a0180 74 65 33 4f 73 41 63 63 65 73 73 28 29 0a 2a 2a te3OsAccess().**
a0190 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 75 sqlite3OsFu
a01a0 6c 6c 50 61 74 68 6e 61 6d 65 28 29 0a 2a 2a 0a llPathname().**.
a01b0 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
a01c0 51 4c 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 QLITE_TEST).SQLI
a01d0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
a01e0 65 33 5f 6d 65 6d 64 65 62 75 67 5f 76 66 73 5f e3_memdebug_vfs_
a01f0 6f 6f 6d 5f 74 65 73 74 20 3d 20 31 3b 0a 20 20 oom_test = 1;.
a0200 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41 #define DO_OS_MA
a0210 4c 4c 4f 43 5f 54 45 53 54 28 78 29 20 20 20 20 LLOC_TEST(x)
a0220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0240 20 20 20 5c 0a 20 20 69 66 20 28 73 71 6c 69 74 \. if (sqlit
a0250 65 33 5f 6d 65 6d 64 65 62 75 67 5f 76 66 73 5f e3_memdebug_vfs_
a0260 6f 6f 6d 5f 74 65 73 74 20 26 26 20 28 21 78 20 oom_test && (!x
a0270 7c 7c 20 21 73 71 6c 69 74 65 33 49 73 4d 65 6d || !sqlite3IsMem
a0280 4a 6f 75 72 6e 61 6c 28 78 29 29 29 20 7b 20 20 Journal(x))) {
a0290 5c 0a 20 20 20 20 76 6f 69 64 20 2a 70 54 73 74 \. void *pTst
a02a0 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 4d Alloc = sqlite3M
a02b0 61 6c 6c 6f 63 28 31 30 29 3b 20 20 20 20 20 20 alloc(10);
a02c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a02d0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 20 \. if
a02e0 28 21 70 54 73 74 41 6c 6c 6f 63 29 20 72 65 74 (!pTstAlloc) ret
a02f0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
a0300 5f 4e 4f 4d 45 4d 3b 20 20 20 20 20 20 20 20 20 _NOMEM;
a0310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
a0320 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
a0330 28 70 54 73 74 41 6c 6c 6f 63 29 3b 20 20 20 20 (pTstAlloc);
a0340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0360 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6c 73 65 \. }.#else
a0370 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 . #define DO_OS
a0380 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 78 29 0a _MALLOC_TEST(x).
a0390 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
a03a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 e following rout
a03b0 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 ines are conveni
a03c0 65 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 72 ence wrappers ar
a03d0 6f 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 ound methods.**
a03e0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 of the sqlite3_f
a03f0 69 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 ile object. Thi
a0400 73 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 s is mostly just
a0410 20 73 79 6e 74 61 63 74 69 63 20 73 75 67 61 72 syntactic sugar
a0420 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73 . All.** of this
a0430 20 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 would be comple
a0440 74 65 6c 79 20 61 75 74 6f 6d 61 74 69 63 20 69 tely automatic i
a0450 66 20 53 51 4c 69 74 65 20 77 65 72 65 20 63 6f f SQLite were co
a0460 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b ded using.** C++
a0470 20 69 6e 73 74 65 61 64 20 6f 66 20 70 6c 61 69 instead of plai
a0480 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49 n old C..*/.SQLI
a0490 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a04a0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 qlite3OsClose(sq
a04b0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 29 lite3_file *pId)
a04c0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
a04d0 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49 ITE_OK;. if( pI
a04e0 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 d->pMethods ){.
a04f0 20 20 20 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65 rc = pId->pMe
a0500 74 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49 thods->xClose(pI
a0510 64 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65 d);. pId->pMe
a0520 74 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 thods = 0;. }.
a0530 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 return rc;.}.SQ
a0540 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
a0550 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73 sqlite3OsRead(s
a0560 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
a0570 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 void *pBuf, int
a0580 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 amt, i64 offset
a0590 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f ){. DO_OS_MALLO
a05a0 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 C_TEST(id);. re
a05b0 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
a05c0 73 2d 3e 78 52 65 61 64 28 69 64 2c 20 70 42 75 s->xRead(id, pBu
a05d0 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b f, amt, offset);
a05e0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
a05f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 E int sqlite3OsW
a0600 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c rite(sqlite3_fil
a0610 65 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 e *id, const voi
a0620 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 d *pBuf, int amt
a0630 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 , i64 offset){.
a0640 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
a0650 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e ST(id);. return
a0660 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
a0670 57 72 69 74 65 28 69 64 2c 20 70 42 75 66 2c 20 Write(id, pBuf,
a0680 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a amt, offset);.}.
a0690 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a06a0 6e 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e nt sqlite3OsTrun
a06b0 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c cate(sqlite3_fil
a06c0 65 20 2a 69 64 2c 20 69 36 34 20 73 69 7a 65 29 e *id, i64 size)
a06d0 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 {. return id->p
a06e0 4d 65 74 68 6f 64 73 2d 3e 78 54 72 75 6e 63 61 Methods->xTrunca
a06f0 74 65 28 69 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a te(id, size);.}.
a0700 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
a0710 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 nt sqlite3OsSync
a0720 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
a0730 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 d, int flags){.
a0740 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
a0750 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e ST(id);. return
a0760 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
a0770 53 79 6e 63 28 69 64 2c 20 66 6c 61 67 73 29 3b Sync(id, flags);
a0780 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
a0790 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 E int sqlite3OsF
a07a0 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f ileSize(sqlite3_
a07b0 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 file *id, i64 *p
a07c0 53 69 7a 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d Size){. DO_OS_M
a07d0 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a ALLOC_TEST(id);.
a07e0 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 return id->pMe
a07f0 74 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65 thods->xFileSize
a0800 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53 (id, pSize);.}.S
a0810 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
a0820 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 t sqlite3OsLock(
a0830 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
a0840 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b , int lockType){
a0850 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f . DO_OS_MALLOC_
a0860 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 TEST(id);. retu
a0870 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
a0880 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 >xLock(id, lockT
a0890 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 ype);.}.SQLITE_P
a08a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
a08b0 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 e3OsUnlock(sqlit
a08c0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
a08d0 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 lockType){. re
a08e0 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
a08f0 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c s->xUnlock(id, l
a0900 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 ockType);.}.SQLI
a0910 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
a0920 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 qlite3OsCheckRes
a0930 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
a0940 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
a0950 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f *pResOut){. DO_
a0960 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 OS_MALLOC_TEST(i
a0970 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d d);. return id-
a0980 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 68 65 63 >pMethods->xChec
a0990 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 69 64 kReservedLock(id
a09a0 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a 0a 2f , pResOut);.}../
a09b0 2a 0a 2a 2a 20 55 73 65 20 73 71 6c 69 74 65 33 *.** Use sqlite3
a09c0 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 OsFileControl()
a09d0 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f 69 6e when we are doin
a09e0 67 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 g something that
a09f0 20 6d 69 67 68 74 20 66 61 69 6c 0a 2a 2a 20 61 might fail.** a
a0a00 6e 64 20 77 65 20 6e 65 65 64 20 74 6f 20 6b 6e nd we need to kn
a0a10 6f 77 20 61 62 6f 75 74 20 74 68 65 20 66 61 69 ow about the fai
a0a20 6c 75 72 65 73 2e 20 20 55 73 65 20 73 71 6c 69 lures. Use sqli
a0a30 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c te3OsFileControl
a0a40 48 69 6e 74 28 29 0a 2a 2a 20 77 68 65 6e 20 73 Hint().** when s
a0a50 69 6d 70 6c 79 20 74 6f 73 73 69 6e 67 20 69 6e imply tossing in
a0a60 66 6f 72 6d 61 74 69 6f 6e 20 6f 76 65 72 20 74 formation over t
a0a70 68 65 20 77 61 6c 6c 20 74 6f 20 74 68 65 20 56 he wall to the V
a0a80 46 53 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74 FS and we do not
a0a90 0a 2a 2a 20 72 65 61 6c 6c 79 20 63 61 72 65 20 .** really care
a0aa0 69 66 20 74 68 65 20 56 46 53 20 72 65 63 65 69 if the VFS recei
a0ab0 76 65 73 20 61 6e 64 20 75 6e 64 65 72 73 74 61 ves and understa
a0ac0 6e 64 73 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 nds the informat
a0ad0 69 6f 6e 20 73 69 6e 63 65 20 69 74 0a 2a 2a 20 ion since it.**
a0ae0 69 73 20 6f 6e 6c 79 20 61 20 68 69 6e 74 20 61 is only a hint a
a0af0 6e 64 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79 nd can be safely
a0b00 20 69 67 6e 6f 72 65 64 2e 20 20 54 68 65 20 73 ignored. The s
a0b10 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 qlite3OsFileCont
a0b20 72 6f 6c 48 69 6e 74 28 29 0a 2a 2a 20 72 6f 75 rolHint().** rou
a0b30 74 69 6e 65 20 68 61 73 20 6e 6f 20 72 65 74 75 tine has no retu
a0b40 72 6e 20 76 61 6c 75 65 20 73 69 6e 63 65 20 74 rn value since t
a0b50 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
a0b60 77 6f 75 6c 64 20 62 65 20 6d 65 61 6e 69 6e 67 would be meaning
a0b70 6c 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f less..*/.SQLITE_
a0b80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
a0b90 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c te3OsFileControl
a0ba0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
a0bb0 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 d, int op, void
a0bc0 2a 70 41 72 67 29 7b 0a 20 20 44 4f 5f 4f 53 5f *pArg){. DO_OS_
a0bd0 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b MALLOC_TEST(id);
a0be0 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d . return id->pM
a0bf0 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 43 6f 6e ethods->xFileCon
a0c00 74 72 6f 6c 28 69 64 2c 20 6f 70 2c 20 70 41 72 trol(id, op, pAr
a0c10 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 g);.}.SQLITE_PRI
a0c20 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
a0c30 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 48 69 3OsFileControlHi
a0c40 6e 74 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 nt(sqlite3_file
a0c50 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 *id, int op, voi
a0c60 64 20 2a 70 41 72 67 29 7b 0a 20 20 28 76 6f 69 d *pArg){. (voi
a0c70 64 29 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e d)id->pMethods->
a0c80 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 2c xFileControl(id,
a0c90 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 0a 53 op, pArg);.}..S
a0ca0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
a0cb0 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f t sqlite3OsSecto
a0cc0 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 rSize(sqlite3_fi
a0cd0 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28 le *id){. int (
a0ce0 2a 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 *xSectorSize)(sq
a0cf0 6c 69 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69 lite3_file*) = i
a0d00 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65 d->pMethods->xSe
a0d10 63 74 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 75 ctorSize;. retu
a0d20 72 6e 20 28 78 53 65 63 74 6f 72 53 69 7a 65 20 rn (xSectorSize
a0d30 3f 20 78 53 65 63 74 6f 72 53 69 7a 65 28 69 64 ? xSectorSize(id
a0d40 29 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ) : SQLITE_DEFAU
a0d50 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b LT_SECTOR_SIZE);
a0d60 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
a0d70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 E int sqlite3OsD
a0d80 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
a0d90 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c tics(sqlite3_fil
a0da0 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e e *id){. return
a0db0 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
a0dc0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
a0dd0 73 74 69 63 73 28 69 64 29 3b 0a 7d 0a 53 51 4c stics(id);.}.SQL
a0de0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
a0df0 73 71 6c 69 74 65 33 4f 73 53 68 6d 4c 6f 63 6b sqlite3OsShmLock
a0e00 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
a0e10 64 2c 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 69 d, int offset, i
a0e20 6e 74 20 6e 2c 20 69 6e 74 20 66 6c 61 67 73 29 nt n, int flags)
a0e30 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 {. return id->p
a0e40 4d 65 74 68 6f 64 73 2d 3e 78 53 68 6d 4c 6f 63 Methods->xShmLoc
a0e50 6b 28 69 64 2c 20 6f 66 66 73 65 74 2c 20 6e 2c k(id, offset, n,
a0e60 20 66 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 54 flags);.}.SQLIT
a0e70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
a0e80 71 6c 69 74 65 33 4f 73 53 68 6d 42 61 72 72 69 qlite3OsShmBarri
a0e90 65 72 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 er(sqlite3_file
a0ea0 2a 69 64 29 7b 0a 20 20 69 64 2d 3e 70 4d 65 74 *id){. id->pMet
a0eb0 68 6f 64 73 2d 3e 78 53 68 6d 42 61 72 72 69 65 hods->xShmBarrie
a0ec0 72 28 69 64 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f r(id);.}.SQLITE_
a0ed0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
a0ee0 74 65 33 4f 73 53 68 6d 55 6e 6d 61 70 28 73 71 te3OsShmUnmap(sq
a0ef0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
a0f00 69 6e 74 20 64 65 6c 65 74 65 46 6c 61 67 29 7b int deleteFlag){
a0f10 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d . return id->pM
a0f20 65 74 68 6f 64 73 2d 3e 78 53 68 6d 55 6e 6d 61 ethods->xShmUnma
a0f30 70 28 69 64 2c 20 64 65 6c 65 74 65 46 6c 61 67 p(id, deleteFlag
a0f40 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
a0f50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
a0f60 73 53 68 6d 4d 61 70 28 0a 20 20 73 71 6c 69 74 sShmMap(. sqlit
a0f70 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 e3_file *id,
a0f80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 /* Da
a0f90 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 6e 64 tabase file hand
a0fa0 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 le */. int iPag
a0fb0 65 2c 0a 20 20 69 6e 74 20 70 67 73 7a 2c 0a 20 e,. int pgsz,.
a0fc0 20 69 6e 74 20 62 45 78 74 65 6e 64 2c 20 20 20 int bExtend,
a0fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0fe0 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 78 74 65 /* True to exte
a0ff0 6e 64 20 66 69 6c 65 20 69 66 20 6e 65 63 65 73 nd file if neces
a1000 73 61 72 79 20 2a 2f 0a 20 20 76 6f 69 64 20 76 sary */. void v
a1010 6f 6c 61 74 69 6c 65 20 2a 2a 70 70 20 20 20 20 olatile **pp
a1020 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
a1030 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d 61 70 : Pointer to map
a1040 70 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 44 4f 5f ping */.){. DO_
a1050 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 OS_MALLOC_TEST(i
a1060 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d d);. return id-
a1070 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 68 6d 4d >pMethods->xShmM
a1080 61 70 28 69 64 2c 20 69 50 61 67 65 2c 20 70 67 ap(id, iPage, pg
a1090 73 7a 2c 20 62 45 78 74 65 6e 64 2c 20 70 70 29 sz, bExtend, pp)
a10a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e ;.}../*.** The n
a10b0 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 ext group of rou
a10c0 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e tines are conven
a10d0 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 ience wrappers a
a10e0 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20 56 46 53 round the.** VFS
a10f0 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c methods..*/.SQL
a1100 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
a1110 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 0a 20 sqlite3OsOpen(.
a1120 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
a1130 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 fs, . const cha
a1140 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 73 71 6c r *zPath, . sql
a1150 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 ite3_file *pFile
a1160 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 , . int flags,
a1170 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67 73 4f 75 . int *pFlagsOu
a1180 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 t.){. int rc;.
a1190 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
a11a0 53 54 28 30 29 3b 0a 20 20 2f 2a 20 30 78 38 37 ST(0);. /* 0x87
a11b0 66 37 66 20 69 73 20 61 20 6d 61 73 6b 20 6f 66 f7f is a mask of
a11c0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c SQLITE_OPEN_ fl
a11d0 61 67 73 20 74 68 61 74 20 61 72 65 20 76 61 6c ags that are val
a11e0 69 64 20 74 6f 20 62 65 20 70 61 73 73 65 64 0a id to be passed.
a11f0 20 20 2a 2a 20 64 6f 77 6e 20 69 6e 74 6f 20 74 ** down into t
a1200 68 65 20 56 46 53 20 6c 61 79 65 72 2e 20 20 53 he VFS layer. S
a1210 6f 6d 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ome SQLITE_OPEN_
a1220 20 66 6c 61 67 73 20 28 66 6f 72 20 65 78 61 6d flags (for exam
a1230 70 6c 65 2c 0a 20 20 2a 2a 20 53 51 4c 49 54 45 ple,. ** SQLITE
a1240 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 _OPEN_FULLMUTEX
a1250 6f 72 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 or SQLITE_OPEN_S
a1260 48 41 52 45 44 43 41 43 48 45 29 20 61 72 65 20 HAREDCACHE) are
a1270 62 6c 6f 63 6b 65 64 20 62 65 66 6f 72 65 0a 20 blocked before.
a1280 20 2a 2a 20 72 65 61 63 68 69 6e 67 20 74 68 65 ** reaching the
a1290 20 56 46 53 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 VFS. */. rc =
a12a0 70 56 66 73 2d 3e 78 4f 70 65 6e 28 70 56 66 73 pVfs->xOpen(pVfs
a12b0 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 , zPath, pFile,
a12c0 66 6c 61 67 73 20 26 20 30 78 38 37 66 37 66 2c flags & 0x87f7f,
a12d0 20 70 46 6c 61 67 73 4f 75 74 29 3b 0a 20 20 61 pFlagsOut);. a
a12e0 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
a12f0 45 5f 4f 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 70 E_OK || pFile->p
a1300 4d 65 74 68 6f 64 73 3d 3d 30 20 29 3b 0a 20 20 Methods==0 );.
a1310 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c return rc;.}.SQL
a1320 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
a1330 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
a1340 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
a1350 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a s, const char *z
a1360 50 61 74 68 2c 20 69 6e 74 20 64 69 72 53 79 6e Path, int dirSyn
a1370 63 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c c){. DO_OS_MALL
a1380 4f 43 5f 54 45 53 54 28 30 29 3b 0a 20 20 61 73 OC_TEST(0);. as
a1390 73 65 72 74 28 20 64 69 72 53 79 6e 63 3d 3d 30 sert( dirSync==0
a13a0 20 7c 7c 20 64 69 72 53 79 6e 63 3d 3d 31 20 29 || dirSync==1 )
a13b0 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d ;. return pVfs-
a13c0 3e 78 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a >xDelete(pVfs, z
a13d0 50 61 74 68 2c 20 64 69 72 53 79 6e 63 29 3b 0a Path, dirSync);.
a13e0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
a13f0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 int sqlite3OsAc
a1400 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f cess(. sqlite3_
a1410 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f vfs *pVfs, . co
a1420 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c nst char *zPath,
a1430 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a . int flags, .
a1440 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 0a 29 int *pResOut.)
a1450 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 {. DO_OS_MALLOC
a1460 5f 54 45 53 54 28 30 29 3b 0a 20 20 72 65 74 75 _TEST(0);. retu
a1470 72 6e 20 70 56 66 73 2d 3e 78 41 63 63 65 73 73 rn pVfs->xAccess
a1480 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 66 6c (pVfs, zPath, fl
a1490 61 67 73 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d ags, pResOut);.}
a14a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a14b0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c int sqlite3OsFul
a14c0 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c lPathname(. sql
a14d0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
a14e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
a14f0 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61 Path, . int nPa
a1500 74 68 4f 75 74 2c 20 0a 20 20 63 68 61 72 20 2a thOut, . char *
a1510 7a 50 61 74 68 4f 75 74 0a 29 7b 0a 20 20 44 4f zPathOut.){. DO
a1520 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 _OS_MALLOC_TEST(
a1530 30 29 3b 0a 20 20 7a 50 61 74 68 4f 75 74 5b 30 0);. zPathOut[0
a1540 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 ] = 0;. return
a1550 70 56 66 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e pVfs->xFullPathn
a1560 61 6d 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c ame(pVfs, zPath,
a1570 20 6e 50 61 74 68 4f 75 74 2c 20 7a 50 61 74 68 nPathOut, zPath
a1580 4f 75 74 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 Out);.}.#ifndef
a1590 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 SQLITE_OMIT_LOAD
a15a0 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 _EXTENSION.SQLIT
a15b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
a15c0 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 sqlite3OsDlOpen(
a15d0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
a15e0 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a s, const char *z
a15f0 50 61 74 68 29 7b 0a 20 20 72 65 74 75 72 6e 20 Path){. return
a1600 70 56 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 pVfs->xDlOpen(pV
a1610 66 73 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 fs, zPath);.}.SQ
a1620 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
a1630 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 d sqlite3OsDlErr
a1640 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a or(sqlite3_vfs *
a1650 70 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c pVfs, int nByte,
a1660 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b char *zBufOut){
a1670 0a 20 20 70 56 66 73 2d 3e 78 44 6c 45 72 72 6f . pVfs->xDlErro
a1680 72 28 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a r(pVfs, nByte, z
a1690 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 BufOut);.}.SQLIT
a16a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 E_PRIVATE void (
a16b0 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 *sqlite3OsDlSym(
a16c0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
a16d0 73 2c 20 76 6f 69 64 20 2a 70 48 64 6c 65 2c 20 s, void *pHdle,
a16e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d const char *zSym
a16f0 29 29 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 ))(void){. retu
a1700 72 6e 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 rn pVfs->xDlSym(
a1710 70 56 66 73 2c 20 70 48 64 6c 65 2c 20 7a 53 79 pVfs, pHdle, zSy
a1720 6d 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 m);.}.SQLITE_PRI
a1730 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
a1740 33 4f 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 3OsDlClose(sqlit
a1750 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f e3_vfs *pVfs, vo
a1760 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 id *pHandle){.
a1770 70 56 66 73 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 pVfs->xDlClose(p
a1780 56 66 73 2c 20 70 48 61 6e 64 6c 65 29 3b 0a 7d Vfs, pHandle);.}
a1790 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
a17a0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
a17b0 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f NSION */.SQLITE_
a17c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
a17d0 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 te3OsRandomness(
a17e0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
a17f0 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 s, int nByte, ch
a1800 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 ar *zBufOut){.
a1810 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 52 61 return pVfs->xRa
a1820 6e 64 6f 6d 6e 65 73 73 28 70 56 66 73 2c 20 6e ndomness(pVfs, n
a1830 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a Byte, zBufOut);.
a1840 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
a1850 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c int sqlite3OsSl
a1860 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 eep(sqlite3_vfs
a1870 2a 70 56 66 73 2c 20 69 6e 74 20 6e 4d 69 63 72 *pVfs, int nMicr
a1880 6f 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 o){. return pVf
a1890 73 2d 3e 78 53 6c 65 65 70 28 70 56 66 73 2c 20 s->xSleep(pVfs,
a18a0 6e 4d 69 63 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 nMicro);.}.SQLIT
a18b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
a18c0 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 lite3OsCurrentTi
a18d0 6d 65 49 6e 74 36 34 28 73 71 6c 69 74 65 33 5f meInt64(sqlite3_
a18e0 76 66 73 20 2a 70 56 66 73 2c 20 73 71 6c 69 74 vfs *pVfs, sqlit
a18f0 65 33 5f 69 6e 74 36 34 20 2a 70 54 69 6d 65 4f e3_int64 *pTimeO
a1900 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ut){. int rc;.
a1910 20 2f 2a 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49 /* IMPLEMENTATI
a1920 4f 4e 2d 4f 46 3a 20 52 2d 34 39 30 34 35 2d 34 ON-OF: R-49045-4
a1930 32 34 39 33 20 53 51 4c 69 74 65 20 77 69 6c 6c 2493 SQLite will
a1940 20 75 73 65 20 74 68 65 20 78 43 75 72 72 65 6e use the xCurren
a1950 74 54 69 6d 65 49 6e 74 36 34 28 29 0a 20 20 2a tTimeInt64(). *
a1960 2a 20 6d 65 74 68 6f 64 20 74 6f 20 67 65 74 20 * method to get
a1970 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 65 the current date
a1980 20 61 6e 64 20 74 69 6d 65 20 69 66 20 74 68 61 and time if tha
a1990 74 20 6d 65 74 68 6f 64 20 69 73 20 61 76 61 69 t method is avai
a19a0 6c 61 62 6c 65 0a 20 20 2a 2a 20 28 69 66 20 69 lable. ** (if i
a19b0 56 65 72 73 69 6f 6e 20 69 73 20 32 20 6f 72 20 Version is 2 or
a19c0 67 72 65 61 74 65 72 20 61 6e 64 20 74 68 65 20 greater and the
a19d0 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 function pointer
a19e0 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 61 6e is not NULL) an
a19f0 64 0a 20 20 2a 2a 20 77 69 6c 6c 20 66 61 6c 6c d. ** will fall
a1a00 20 62 61 63 6b 20 74 6f 20 78 43 75 72 72 65 6e back to xCurren
a1a10 74 54 69 6d 65 28 29 20 69 66 20 78 43 75 72 72 tTime() if xCurr
a1a20 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 29 20 69 entTimeInt64() i
a1a30 73 0a 20 20 2a 2a 20 75 6e 61 76 61 69 6c 61 62 s. ** unavailab
a1a40 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 le.. */. if( p
a1a50 56 66 73 2d 3e 69 56 65 72 73 69 6f 6e 3e 3d 32 Vfs->iVersion>=2
a1a60 20 26 26 20 70 56 66 73 2d 3e 78 43 75 72 72 65 && pVfs->xCurre
a1a70 6e 74 54 69 6d 65 49 6e 74 36 34 20 29 7b 0a 20 ntTimeInt64 ){.
a1a80 20 20 20 72 63 20 3d 20 70 56 66 73 2d 3e 78 43 rc = pVfs->xC
a1a90 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 urrentTimeInt64(
a1aa0 70 56 66 73 2c 20 70 54 69 6d 65 4f 75 74 29 3b pVfs, pTimeOut);
a1ab0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f . }else{. do
a1ac0 75 62 6c 65 20 72 3b 0a 20 20 20 20 72 63 20 3d uble r;. rc =
a1ad0 20 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 pVfs->xCurrentT
a1ae0 69 6d 65 28 70 56 66 73 2c 20 26 72 29 3b 0a 20 ime(pVfs, &r);.
a1af0 20 20 20 2a 70 54 69 6d 65 4f 75 74 20 3d 20 28 *pTimeOut = (
a1b00 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 sqlite3_int64)(r
a1b10 2a 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20 20 *86400000.0);.
a1b20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
a1b30 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
a1b40 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 int sqlite3OsOp
a1b50 65 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 enMalloc(. sqli
a1b60 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a te3_vfs *pVfs, .
a1b70 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
a1b80 69 6c 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f ile, . sqlite3_
a1b90 66 69 6c 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a file **ppFile, .
a1ba0 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 int flags,. i
a1bb0 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b nt *pOutFlags.){
a1bc0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
a1bd0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 TE_NOMEM;. sqli
a1be0 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b te3_file *pFile;
a1bf0 0a 20 20 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 . pFile = (sqli
a1c00 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 te3_file *)sqlit
a1c10 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 66 e3MallocZero(pVf
a1c20 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 s->szOsFile);.
a1c30 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 if( pFile ){.
a1c40 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
a1c50 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 2c pen(pVfs, zFile,
a1c60 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 70 pFile, flags, p
a1c70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 OutFlags);. i
a1c80 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
a1c90 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
a1ca0 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 3_free(pFile);.
a1cb0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a1cc0 2a 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65 3b *ppFile = pFile;
a1cd0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
a1ce0 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 urn rc;.}.SQLITE
a1cf0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
a1d00 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 ite3OsCloseFree(
a1d10 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 sqlite3_file *pF
a1d20 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ile){. int rc =
a1d30 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 SQLITE_OK;. as
a1d40 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 sert( pFile );.
a1d50 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 rc = sqlite3OsC
a1d60 6c 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20 73 lose(pFile);. s
a1d70 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c qlite3_free(pFil
a1d80 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b e);. return rc;
a1d90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
a1da0 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72 61 unction is a wra
a1db0 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20 pper around the
a1dc0 4f 53 20 73 70 65 63 69 66 69 63 20 69 6d 70 6c OS specific impl
a1dd0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a ementation of.**
a1de0 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
a1df0 28 29 2e 20 54 68 65 20 70 75 72 70 6f 73 65 20 (). The purpose
a1e00 6f 66 20 74 68 65 20 77 72 61 70 70 65 72 20 69 of the wrapper i
a1e10 73 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 s to provide the
a1e20 0a 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f 20 73 .** ability to s
a1e30 69 6d 75 6c 61 74 65 20 61 20 6d 61 6c 6c 6f 63 imulate a malloc
a1e40 20 66 61 69 6c 75 72 65 2c 20 73 6f 20 74 68 61 failure, so tha
a1e50 74 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f t the handling o
a1e60 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 69 6e f an.** error in
a1e70 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
a1e80 28 29 20 62 79 20 74 68 65 20 75 70 70 65 72 20 () by the upper
a1e90 6c 61 79 65 72 73 20 63 61 6e 20 62 65 20 74 65 layers can be te
a1ea0 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f sted..*/.SQLITE_
a1eb0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
a1ec0 74 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 29 7b te3OsInit(void){
a1ed0 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c . void *p = sql
a1ee0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 31 30 29 3b ite3_malloc(10);
a1ef0 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
a1f00 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
a1f10 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 M;. sqlite3_fre
a1f20 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 e(p);. return s
a1f30 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 qlite3_os_init()
a1f40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6c ;.}../*.** The l
a1f50 69 73 74 20 6f 66 20 61 6c 6c 20 72 65 67 69 73 ist of all regis
a1f60 74 65 72 65 64 20 56 46 53 20 69 6d 70 6c 65 6d tered VFS implem
a1f70 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 entations..*/.st
a1f80 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 atic sqlite3_vfs
a1f90 20 2a 20 53 51 4c 49 54 45 5f 57 53 44 20 76 66 * SQLITE_WSD vf
a1fa0 73 4c 69 73 74 20 3d 20 30 3b 0a 23 64 65 66 69 sList = 0;.#defi
a1fb0 6e 65 20 76 66 73 4c 69 73 74 20 47 4c 4f 42 41 ne vfsList GLOBA
a1fc0 4c 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c L(sqlite3_vfs *,
a1fd0 20 76 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a 2a 2a vfsList)../*.**
a1fe0 20 4c 6f 63 61 74 65 20 61 20 56 46 53 20 62 79 Locate a VFS by
a1ff0 20 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20 6e 61 name. If no na
a2000 6d 65 20 69 73 20 67 69 76 65 6e 2c 20 73 69 6d me is given, sim
a2010 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 0a 2a ply return the.*
a2020 2a 20 66 69 72 73 74 20 56 46 53 20 6f 6e 20 74 * first VFS on t
a2030 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 he list..*/.SQLI
a2040 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 TE_API sqlite3_v
a2050 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f fs *sqlite3_vfs_
a2060 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20 find(const char
a2070 2a 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69 74 65 *zVfs){. sqlite
a2080 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 30 3b 3_vfs *pVfs = 0;
a2090 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
a20a0 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 ADSAFE. sqlite3
a20b0 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 23 _mutex *mutex;.#
a20c0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 endif.#ifndef SQ
a20d0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
a20e0 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 IT. int rc = sq
a20f0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
a2100 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 ();. if( rc ) r
a2110 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a eturn 0;.#endif.
a2120 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
a2130 44 53 41 46 45 0a 20 20 6d 75 74 65 78 20 3d 20 DSAFE. mutex =
a2140 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
a2150 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
a2160 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 TATIC_MASTER);.#
a2170 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f endif. sqlite3_
a2180 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
a2190 78 29 3b 0a 20 20 66 6f 72 28 70 56 66 73 20 3d x);. for(pVfs =
a21a0 20 76 66 73 4c 69 73 74 3b 20 70 56 66 73 3b 20 vfsList; pVfs;
a21b0 70 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78 74 pVfs=pVfs->pNext
a21c0 29 7b 0a 20 20 20 20 69 66 28 20 7a 56 66 73 3d ){. if( zVfs=
a21d0 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 =0 ) break;.
a21e0 69 66 28 20 73 74 72 63 6d 70 28 7a 56 66 73 2c if( strcmp(zVfs,
a21f0 20 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 pVfs->zName)==0
a2200 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 ) break;. }.
a2210 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
a2220 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 ave(mutex);. re
a2230 74 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f 2a turn pVfs;.}../*
a2240 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46 53 .** Unlink a VFS
a2250 20 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65 64 from the linked
a2260 20 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63 20 list.*/.static
a2270 76 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28 73 void vfsUnlink(s
a2280 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
a2290 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
a22a0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
a22b0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
a22c0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
a22d0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 20 29 TATIC_MASTER)) )
a22e0 3b 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20 ;. if( pVfs==0
a22f0 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 ){. /* No-op
a2300 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 */. }else if( v
a2310 66 73 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b 0a fsList==pVfs ){.
a2320 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 vfsList = pV
a2330 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c fs->pNext;. }el
a2340 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 20 29 se if( vfsList )
a2350 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 {. sqlite3_vf
a2360 73 20 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b 0a s *p = vfsList;.
a2370 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4e while( p->pN
a2380 65 78 74 20 26 26 20 70 2d 3e 70 4e 65 78 74 21 ext && p->pNext!
a2390 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 =pVfs ){. p
a23a0 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 = p->pNext;.
a23b0 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4e }. if( p->pN
a23c0 65 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 ext==pVfs ){.
a23d0 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 56 p->pNext = pV
a23e0 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d fs->pNext;. }
a23f0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
a2400 67 69 73 74 65 72 20 61 20 56 46 53 20 77 69 74 gister a VFS wit
a2410 68 20 74 68 65 20 73 79 73 74 65 6d 2e 20 20 49 h the system. I
a2420 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f t is harmless to
a2430 20 72 65 67 69 73 74 65 72 20 74 68 65 20 73 61 register the sa
a2440 6d 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69 70 me.** VFS multip
a2450 6c 65 20 74 69 6d 65 73 2e 20 20 54 68 65 20 6e le times. The n
a2460 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 ew VFS becomes t
a2470 68 65 20 64 65 66 61 75 6c 74 20 69 66 20 6d 61 he default if ma
a2480 6b 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72 75 keDflt is.** tru
a2490 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
a24a0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 int sqlite3_vfs
a24b0 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 _register(sqlite
a24c0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
a24d0 20 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20 4d 55 makeDflt){. MU
a24e0 54 45 58 5f 4c 4f 47 49 43 28 73 71 6c 69 74 65 TEX_LOGIC(sqlite
a24f0 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 29 3_mutex *mutex;)
a2500 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
a2510 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
a2520 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 int rc = sqlite3
a2530 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 _initialize();.
a2540 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
a2550 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 4d 55 rc;.#endif. MU
a2560 54 45 58 5f 4c 4f 47 49 43 28 20 6d 75 74 65 78 TEX_LOGIC( mutex
a2570 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
a2580 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
a2590 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
a25a0 3b 20 29 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 ; ). sqlite3_mu
a25b0 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 tex_enter(mutex)
a25c0 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 ;. vfsUnlink(pV
a25d0 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 6b 65 44 fs);. if( makeD
a25e0 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 74 3d 3d flt || vfsList==
a25f0 30 20 29 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70 0 ){. pVfs->p
a2600 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 3b 0a Next = vfsList;.
a2610 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 vfsList = pV
a2620 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 fs;. }else{.
a2630 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76 pVfs->pNext = v
a2640 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 fsList->pNext;.
a2650 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 vfsList->pNex
a2660 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 0a 20 20 t = pVfs;. }.
a2670 61 73 73 65 72 74 28 76 66 73 4c 69 73 74 29 3b assert(vfsList);
a2680 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
a2690 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 _leave(mutex);.
a26a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
a26b0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 72 65 K;.}../*.** Unre
a26c0 67 69 73 74 65 72 20 61 20 56 46 53 20 73 6f 20 gister a VFS so
a26d0 74 68 61 74 20 69 74 20 69 73 20 6e 6f 20 6c 6f that it is no lo
a26e0 6e 67 65 72 20 61 63 63 65 73 73 69 62 6c 65 2e nger accessible.
a26f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
a2700 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 nt sqlite3_vfs_u
a2710 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 nregister(sqlite
a2720 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 23 69 3_vfs *pVfs){.#i
a2730 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
a2740 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 AFE. sqlite3_mu
a2750 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c tex *mutex = sql
a2760 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
a2770 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
a2780 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 IC_MASTER);.#end
a2790 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 if. sqlite3_mut
a27a0 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b ex_enter(mutex);
a27b0 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66 . vfsUnlink(pVf
a27c0 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 s);. sqlite3_mu
a27d0 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 tex_leave(mutex)
a27e0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
a27f0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a E_OK;.}../******
a2800 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
a2810 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os.c ***********
a2820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a2830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a2840 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
a2850 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
a2860 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a ile fault.c ****
a2870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a2880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a2890 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
a28a0 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2a 2a 008 Jan 22.**.**
a28b0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
a28c0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
a28d0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
a28e0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
a28f0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
a2900 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
a2910 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
a2920 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
a2930 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
a2940 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
a2950 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
a2960 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
a2970 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
a2980 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
a2990 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
a29a0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
a29b0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
a29c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a29d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a29e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a29f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a2a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
a2a10 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
a2a20 6e 73 20 63 6f 64 65 20 74 6f 20 73 75 70 70 6f ns code to suppo
a2a30 72 74 20 74 68 65 20 63 6f 6e 63 65 70 74 20 6f rt the concept o
a2a40 66 20 22 62 65 6e 69 67 6e 22 20 0a 2a 2a 20 6d f "benign" .** m
a2a50 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 28 alloc failures (
a2a60 77 68 65 6e 20 74 68 65 20 78 4d 61 6c 6c 6f 63 when the xMalloc
a2a70 28 29 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28 29 () or xRealloc()
a2a80 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a method of the.*
a2a90 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 * sqlite3_mem_me
a2aa0 74 68 6f 64 73 20 73 74 72 75 63 74 75 72 65 20 thods structure
a2ab0 66 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 fails to allocat
a2ac0 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d e a block of mem
a2ad0 6f 72 79 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 ory.** and retur
a2ae0 6e 73 20 30 29 2e 20 0a 2a 2a 0a 2a 2a 20 4d 6f ns 0). .**.** Mo
a2af0 73 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 st malloc failur
a2b00 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 es are non-benig
a2b10 6e 2e 20 41 66 74 65 72 20 74 68 65 79 20 6f 63 n. After they oc
a2b20 63 75 72 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 61 cur, SQLite.** a
a2b30 62 61 6e 64 6f 6e 73 20 74 68 65 20 63 75 72 72 bandons the curr
a2b40 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e ent operation an
a2b50 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 d returns an err
a2b60 6f 72 20 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 or code (usually
a2b70 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d .** SQLITE_NOMEM
a2b80 29 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 48 ) to the user. H
a2b90 6f 77 65 76 65 72 2c 20 73 6f 6d 65 74 69 6d 65 owever, sometime
a2ba0 73 20 61 20 66 61 75 6c 74 20 69 73 20 6e 6f 74 s a fault is not
a2bb0 20 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 necessarily.**
a2bc0 66 61 74 61 6c 2e 20 46 6f 72 20 65 78 61 6d 70 fatal. For examp
a2bd0 6c 65 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 le, if a malloc
a2be0 66 61 69 6c 73 20 77 68 69 6c 65 20 72 65 73 69 fails while resi
a2bf0 7a 69 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c zing a hash tabl
a2c00 65 2c 20 74 68 69 73 20 0a 2a 2a 20 69 73 20 63 e, this .** is c
a2c10 6f 6d 70 6c 65 74 65 6c 79 20 72 65 63 6f 76 65 ompletely recove
a2c20 72 61 62 6c 65 20 73 69 6d 70 6c 79 20 62 79 20 rable simply by
a2c30 6e 6f 74 20 63 61 72 72 79 69 6e 67 20 6f 75 74 not carrying out
a2c40 20 74 68 65 20 72 65 73 69 7a 65 2e 20 54 68 65 the resize. The
a2c50 20 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 .** hash table
a2c60 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f will continue to
a2c70 20 66 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d 61 6c function normal
a2c80 6c 79 2e 20 20 53 6f 20 61 20 6d 61 6c 6c 6f 63 ly. So a malloc
a2c90 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 64 75 72 failure .** dur
a2ca0 69 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c 65 ing a hash table
a2cb0 20 72 65 73 69 7a 65 20 69 73 20 61 20 62 65 6e resize is a ben
a2cc0 69 67 6e 20 66 61 75 6c 74 2e 0a 2a 2f 0a 0a 0a ign fault..*/...
a2cd0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
a2ce0 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 MIT_BUILTIN_TEST
a2cf0 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 76 ../*.** Global v
a2d00 61 72 69 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70 ariables..*/.typ
a2d10 65 64 65 66 20 73 74 72 75 63 74 20 42 65 6e 69 edef struct Beni
a2d20 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 42 65 gnMallocHooks Be
a2d30 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 3b nignMallocHooks;
a2d40 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 .static SQLITE_W
a2d50 53 44 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e SD struct Benign
a2d60 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 7b 0a 20 20 MallocHooks {.
a2d70 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 void (*xBenignBe
a2d80 67 69 6e 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f gin)(void);. vo
a2d90 69 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 id (*xBenignEnd)
a2da0 28 76 6f 69 64 29 3b 0a 7d 20 73 71 6c 69 74 65 (void);.} sqlite
a2db0 33 48 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20 30 20 3Hooks = { 0, 0
a2dc0 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 48 };../* The "wsdH
a2dd0 6f 6f 6b 73 22 20 6d 61 63 72 6f 20 77 69 6c 6c ooks" macro will
a2de0 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 resolve to the
a2df0 61 70 70 72 6f 70 72 69 61 74 65 20 42 65 6e 69 appropriate Beni
a2e00 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0a 2a 2a gnMallocHooks.**
a2e10 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 structure. If
a2e20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 writable static
a2e30 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 data is unsuppor
a2e40 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 ted on the targe
a2e50 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f t,.** we have to
a2e60 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 locate the stat
a2e70 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d e vector at run-
a2e80 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f time. In the mo
a2e90 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 re common.** cas
a2ea0 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 e where writable
a2eb0 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 static data is
a2ec0 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 48 6f supported, wsdHo
a2ed0 6f 6b 73 20 63 61 6e 20 72 65 66 65 72 20 64 69 oks can refer di
a2ee0 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 rectly.** to the
a2ef0 20 22 73 71 6c 69 74 65 33 48 6f 6f 6b 73 22 20 "sqlite3Hooks"
a2f00 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 state vector dec
a2f10 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a lared above..*/.
a2f20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
a2f30 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 IT_WSD.# define
a2f40 77 73 64 48 6f 6f 6b 73 49 6e 69 74 20 5c 0a 20 wsdHooksInit \.
a2f50 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f BenignMallocHoo
a2f60 6b 73 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28 ks *x = &GLOBAL(
a2f70 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b BenignMallocHook
a2f80 73 2c 73 71 6c 69 74 65 33 48 6f 6f 6b 73 29 0a s,sqlite3Hooks).
a2f90 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b # define wsdHook
a2fa0 73 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 s x[0].#else.# d
a2fb0 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e efine wsdHooksIn
a2fc0 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 it.# define wsdH
a2fd0 6f 6f 6b 73 20 73 71 6c 69 74 65 33 48 6f 6f 6b ooks sqlite3Hook
a2fe0 73 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a s.#endif.../*.**
a2ff0 20 52 65 67 69 73 74 65 72 20 68 6f 6f 6b 73 20 Register hooks
a3000 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20 73 71 6c to call when sql
a3010 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
a3020 61 6c 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a 20 73 alloc() and.** s
a3030 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
a3040 61 6c 6c 6f 63 28 29 20 61 72 65 20 63 61 6c 6c alloc() are call
a3050 65 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 ed, respectively
a3060 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
a3070 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
a3080 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b BenignMallocHook
a3090 73 28 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e s(. void (*xBen
a30a0 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 2c ignBegin)(void),
a30b0 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 . void (*xBenig
a30c0 6e 45 6e 64 29 28 76 6f 69 64 29 0a 29 7b 0a 20 nEnd)(void).){.
a30d0 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 wsdHooksInit;.
a30e0 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 wsdHooks.xBenig
a30f0 6e 42 65 67 69 6e 20 3d 20 78 42 65 6e 69 67 6e nBegin = xBenign
a3100 42 65 67 69 6e 3b 0a 20 20 77 73 64 48 6f 6f 6b Begin;. wsdHook
a3110 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 78 s.xBenignEnd = x
a3120 42 65 6e 69 67 6e 45 6e 64 3b 0a 7d 0a 0a 2f 2a BenignEnd;.}../*
a3130 0a 2a 2a 20 54 68 69 73 20 28 73 71 6c 69 74 65 .** This (sqlite
a3140 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
a3150 28 29 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 ()) is called by
a3160 20 53 51 4c 69 74 65 20 63 6f 64 65 20 74 6f 20 SQLite code to
a3170 69 6e 64 69 63 61 74 65 20 74 68 61 74 0a 2a 2a indicate that.**
a3180 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c subsequent mall
a3190 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 oc failures are
a31a0 62 65 6e 69 67 6e 2e 20 41 20 63 61 6c 6c 20 74 benign. A call t
a31b0 6f 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 o sqlite3EndBeni
a31c0 67 6e 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 6e gnMalloc().** in
a31d0 64 69 63 61 74 65 73 20 74 68 61 74 20 73 75 62 dicates that sub
a31e0 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 sequent malloc f
a31f0 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d ailures are non-
a3200 62 65 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 benign..*/.SQLIT
a3210 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
a3220 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
a3230 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 nMalloc(void){.
a3240 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 wsdHooksInit;.
a3250 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 if( wsdHooks.xB
a3260 65 6e 69 67 6e 42 65 67 69 6e 20 29 7b 0a 20 20 enignBegin ){.
a3270 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 wsdHooks.xBeni
a3280 67 6e 42 65 67 69 6e 28 29 3b 0a 20 20 7d 0a 7d gnBegin();. }.}
a3290 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a32a0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42 void sqlite3EndB
a32b0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 enignMalloc(void
a32c0 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 ){. wsdHooksIni
a32d0 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b t;. if( wsdHook
a32e0 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 29 7b 0a s.xBenignEnd ){.
a32f0 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 wsdHooks.xBe
a3300 6e 69 67 6e 45 6e 64 28 29 3b 0a 20 20 7d 0a 7d nignEnd();. }.}
a3310 0a 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 23 69 ..#endif /* #i
a3320 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
a3330 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a T_BUILTIN_TEST *
a3340 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
a3350 2a 2a 20 45 6e 64 20 6f 66 20 66 61 75 6c 74 2e ** End of fault.
a3360 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
a3370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3390 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
a33a0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 ** Begin file me
a33b0 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m0.c ***********
a33c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a33d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a33e0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 */./*.** 2008 Oc
a33f0 74 6f 62 65 72 20 32 38 0a 2a 2a 0a 2a 2a 20 54 tober 28.**.** T
a3400 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
a3410 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
a3420 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
a3430 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
a3440 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
a3450 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
a3460 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
a3470 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
a3480 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
a3490 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
a34a0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
a34b0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
a34c0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
a34d0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
a34e0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
a34f0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
a3500 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
a3510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3550 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
a3560 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
a3570 20 61 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72 79 20 a no-op memory
a3580 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 allocation drive
a3590 72 73 20 66 6f 72 20 75 73 65 20 77 68 65 6e 0a rs for use when.
a35a0 2a 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d ** SQLITE_ZERO_M
a35b0 41 4c 4c 4f 43 20 69 73 20 64 65 66 69 6e 65 64 ALLOC is defined
a35c0 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f . The allocatio
a35d0 6e 20 64 72 69 76 65 72 73 20 69 6d 70 6c 65 6d n drivers implem
a35e0 65 6e 74 65 64 0a 2a 2a 20 68 65 72 65 20 61 6c ented.** here al
a35f0 77 61 79 73 20 66 61 69 6c 2e 20 20 53 51 4c 69 ways fail. SQLi
a3600 74 65 20 77 69 6c 6c 20 6e 6f 74 20 6f 70 65 72 te will not oper
a3610 61 74 65 20 77 69 74 68 20 74 68 65 73 65 20 64 ate with these d
a3620 72 69 76 65 72 73 2e 20 20 54 68 65 73 65 0a 2a rivers. These.*
a3630 2a 20 61 72 65 20 6d 65 72 65 6c 79 20 70 6c 61 * are merely pla
a3640 63 65 68 6f 6c 64 65 72 73 2e 20 20 52 65 61 6c ceholders. Real
a3650 20 64 72 69 76 65 72 73 20 6d 75 73 74 20 62 65 drivers must be
a3660 20 73 75 62 73 74 69 74 75 74 65 64 20 75 73 69 substituted usi
a3670 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f ng.** sqlite3_co
a3680 6e 66 69 67 28 29 20 62 65 66 6f 72 65 20 53 51 nfig() before SQ
a3690 4c 69 74 65 20 77 69 6c 6c 20 6f 70 65 72 61 74 Lite will operat
a36a0 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 e..*/../*.** Thi
a36b0 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 s version of the
a36c0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
a36d0 72 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 r is the default
a36e0 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 . It is.** used
a36f0 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 6d when no other m
a3700 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
a3710 69 73 20 73 70 65 63 69 66 69 65 64 20 75 73 69 is specified usi
a3720 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a ng compile-time.
a3730 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 ** macros..*/.#i
a3740 66 64 65 66 20 53 51 4c 49 54 45 5f 5a 45 52 4f fdef SQLITE_ZERO
a3750 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4e _MALLOC../*.** N
a3760 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73 20 6f 66 o-op versions of
a3770 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f all memory allo
a3780 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 0a cation routines.
a3790 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a */.static void *
a37a0 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 sqlite3MemMalloc
a37b0 28 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65 74 (int nByte){ ret
a37c0 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 urn 0; }.static
a37d0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 void sqlite3MemF
a37e0 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ree(void *pPrior
a37f0 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 ){ return; }.sta
a3800 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 tic void *sqlite
a3810 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64 3MemRealloc(void
a3820 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 *pPrior, int nB
a3830 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 yte){ return 0;
a3840 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c }.static int sql
a3850 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 ite3MemSize(void
a3860 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 75 72 *pPrior){ retur
a3870 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e n 0; }.static in
a3880 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e t sqlite3MemRoun
a3890 64 75 70 28 69 6e 74 20 6e 29 7b 20 72 65 74 75 dup(int n){ retu
a38a0 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 69 63 20 69 rn n; }.static i
a38b0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 nt sqlite3MemIni
a38c0 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
a38d0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f { return SQLITE_
a38e0 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 OK; }.static voi
a38f0 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 d sqlite3MemShut
a3900 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 down(void *NotUs
a3910 65 64 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a ed){ return; }..
a3920 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
a3930 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 ne is the only r
a3940 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 outine in this f
a3950 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 ile with externa
a3960 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a l linkage..**.**
a3970 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f Populate the lo
a3980 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
a3990 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 llocation functi
a39a0 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a on pointers in.*
a39b0 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 * sqlite3GlobalC
a39c0 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 onfig.m with poi
a39d0 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 nters to the rou
a39e0 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 tines in this fi
a39f0 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
a3a00 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
a3a10 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 e3MemSetDefault(
a3a20 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
a3a30 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 const sqlite3_me
a3a40 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c m_methods defaul
a3a50 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 tMethods = {.
a3a60 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c sqlite3MemMall
a3a70 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 oc,. sqlite3
a3a80 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 MemFree,. sq
a3a90 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c lite3MemRealloc,
a3aa0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
a3ab0 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 Size,. sqlit
a3ac0 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 e3MemRoundup,.
a3ad0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 sqlite3MemIni
a3ae0 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d t,. sqlite3M
a3af0 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 emShutdown,.
a3b00 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 0. };. sqlite
a3b10 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
a3b20 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 CONFIG_MALLOC, &
a3b30 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b defaultMethods);
a3b40 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 .}..#endif /* SQ
a3b50 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 LITE_ZERO_MALLOC
a3b60 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
a3b70 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 30 **** End of mem0
a3b80 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
a3b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3bb0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
a3bc0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
a3bd0 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mem1.c *********
a3be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3c00 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 ***/./*.** 2007
a3c10 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 August 14.**.**
a3c20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
a3c30 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
a3c40 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
a3c50 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
a3c60 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
a3c70 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
a3c80 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
a3c90 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
a3ca0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
a3cb0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
a3cc0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
a3cd0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
a3ce0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
a3cf0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
a3d00 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
a3d10 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
a3d20 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
a3d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a3d70 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
a3d80 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
a3d90 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f s low-level memo
a3da0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 ry allocation dr
a3db0 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a 2a ivers for when.*
a3dc0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 * SQLite will us
a3dd0 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 e the standard C
a3de0 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 2f -library malloc/
a3df0 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e 74 realloc/free int
a3e00 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 erface.** to obt
a3e10 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 ain the memory i
a3e20 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 t needs..**.** T
a3e30 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
a3e40 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
a3e50 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76 s of the low-lev
a3e60 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 el memory alloca
a3e70 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 tion.** routines
a3e80 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 specified in th
a3e90 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 e sqlite3_mem_me
a3ea0 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 20 20 54 thods object. T
a3eb0 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a he content of.**
a3ec0 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e this file is on
a3ed0 6c 79 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 ly used if SQLIT
a3ee0 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 E_SYSTEM_MALLOC
a3ef0 69 73 20 64 65 66 69 6e 65 64 2e 20 20 54 68 65 is defined. The
a3f00 0a 2a 2a 20 53 51 4c 49 54 45 5f 53 59 53 54 45 .** SQLITE_SYSTE
a3f10 4d 5f 4d 41 4c 4c 4f 43 20 6d 61 63 72 6f 20 69 M_MALLOC macro i
a3f20 73 20 64 65 66 69 6e 65 64 20 61 75 74 6f 6d 61 s defined automa
a3f30 74 69 63 61 6c 6c 79 20 69 66 20 6e 65 69 74 68 tically if neith
a3f40 65 72 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 er the.** SQLITE
a3f50 5f 4d 45 4d 44 45 42 55 47 20 6e 6f 72 20 74 68 _MEMDEBUG nor th
a3f60 65 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 4d e SQLITE_WIN32_M
a3f70 41 4c 4c 4f 43 20 6d 61 63 72 6f 73 20 61 72 65 ALLOC macros are
a3f80 20 64 65 66 69 6e 65 64 2e 20 20 54 68 65 0a 2a defined. The.*
a3f90 2a 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 * default config
a3fa0 75 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 75 73 uration is to us
a3fb0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
a3fc0 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 ion routines in
a3fd0 74 68 69 73 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a this.** file..**
a3fe0 0a 2a 2a 20 43 2d 70 72 65 70 72 6f 63 65 73 73 .** C-preprocess
a3ff0 6f 72 20 6d 61 63 72 6f 20 73 75 6d 6d 61 72 79 or macro summary
a4000 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 48 41 56 45 5f :.**.** HAVE_
a4010 4d 41 4c 4c 4f 43 5f 55 53 41 42 4c 45 5f 53 49 MALLOC_USABLE_SI
a4020 5a 45 20 20 20 20 20 54 68 65 20 63 6f 6e 66 69 ZE The confi
a4030 67 75 72 65 20 73 63 72 69 70 74 20 73 65 74 73 gure script sets
a4040 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 69 66 0a this symbol if.
a4050 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
a4060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4070 20 20 74 68 65 20 6d 61 6c 6c 6f 63 5f 75 73 61 the malloc_usa
a4080 62 6c 65 5f 73 69 7a 65 28 29 20 69 6e 74 65 72 ble_size() inter
a4090 66 61 63 65 20 65 78 69 73 74 73 0a 2a 2a 20 20 face exists.**
a40a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a40b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e on
a40c0 20 74 68 65 20 74 61 72 67 65 74 20 70 6c 61 74 the target plat
a40d0 66 6f 72 6d 2e 20 20 4f 72 2c 20 74 68 69 73 20 form. Or, this
a40e0 73 79 6d 62 6f 6c 0a 2a 2a 20 20 20 20 20 20 20 symbol.**
a40f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4100 20 20 20 20 20 20 20 20 20 63 61 6e 20 62 65 20 can be
a4110 73 65 74 20 6d 61 6e 75 61 6c 6c 79 2c 20 69 66 set manually, if
a4120 20 64 65 73 69 72 65 64 2e 0a 2a 2a 20 20 20 20 desired..**
a4130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4140 20 20 20 20 20 20 20 20 20 20 20 20 49 66 20 61 If a
a4150 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 n equivalent int
a4160 65 72 66 61 63 65 20 65 78 69 73 74 73 20 62 79 erface exists by
a4170 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
a4180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4190 20 20 20 61 20 64 69 66 66 65 72 65 6e 74 20 6e a different n
a41a0 61 6d 65 2c 20 75 73 69 6e 67 20 61 20 73 65 70 ame, using a sep
a41b0 61 72 61 74 65 20 2d 44 0a 2a 2a 20 20 20 20 20 arate -D.**
a41c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a41d0 20 20 20 20 20 20 20 20 20 20 20 6f 70 74 69 6f optio
a41e0 6e 20 74 6f 20 72 65 6e 61 6d 65 20 69 74 2e 0a n to rename it..
a41f0 2a 2a 0a 2a 2a 20 20 20 20 53 51 4c 49 54 45 5f **.** SQLITE_
a4200 57 49 54 48 4f 55 54 5f 5a 4f 4e 45 4d 41 4c 4c WITHOUT_ZONEMALL
a4210 4f 43 20 20 20 53 6f 6d 65 20 6f 6c 64 65 72 20 OC Some older
a4220 6d 61 63 73 20 6c 61 63 6b 20 73 75 70 70 6f 72 macs lack suppor
a4230 74 20 66 6f 72 20 74 68 65 20 7a 6f 6e 65 0a 2a t for the zone.*
a4240 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
a4250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4260 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
a4270 72 2e 20 20 53 65 74 20 74 68 69 73 20 73 79 6d r. Set this sym
a4280 62 6f 6c 20 74 6f 20 65 6e 61 62 6c 65 0a 2a 2a bol to enable.**
a4290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a42a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a42b0 62 75 69 6c 64 69 6e 67 20 6f 6e 20 6f 6c 64 65 building on olde
a42c0 72 20 6d 61 63 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 r macs..**.**
a42d0 20 53 51 4c 49 54 45 5f 57 49 54 48 4f 55 54 5f SQLITE_WITHOUT_
a42e0 4d 53 49 5a 45 20 20 20 20 20 20 20 20 53 65 74 MSIZE Set
a42f0 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 74 6f 20 this symbol to
a4300 64 69 73 61 62 6c 65 20 74 68 65 20 75 73 65 20 disable the use
a4310 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 of.**
a4320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4330 20 20 20 20 20 5f 6d 73 69 7a 65 28 29 20 6f 6e _msize() on
a4340 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 windows systems
a4350 2e 20 20 54 68 69 73 20 6d 69 67 68 74 0a 2a 2a . This might.**
a4360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a4380 62 65 20 6e 65 63 65 73 73 61 72 79 20 77 68 65 be necessary whe
a4390 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 n compiling for
a43a0 44 65 6c 70 68 69 2c 0a 2a 2a 20 20 20 20 20 20 Delphi,.**
a43b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a43c0 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 65 78 for ex
a43d0 61 6d 70 6c 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a ample..*/../*.**
a43e0 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 This version of
a43f0 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
a4400 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 cator is the def
a4410 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 ault. It is.**
a4420 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 used when no oth
a4430 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 er memory alloca
a4440 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 64 tor is specified
a4450 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 using compile-t
a4460 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a ime.** macros..*
a4470 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
a4480 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a 2f SYSTEM_MALLOC../
a4490 2a 0a 2a 2a 20 54 68 65 20 4d 53 56 43 52 54 20 *.** The MSVCRT
a44a0 68 61 73 20 6d 61 6c 6c 6f 63 5f 75 73 61 62 6c has malloc_usabl
a44b0 65 5f 73 69 7a 65 28 29 20 62 75 74 20 69 74 20 e_size() but it
a44c0 69 73 20 63 61 6c 6c 65 64 20 5f 6d 73 69 7a 65 is called _msize
a44d0 28 29 2e 0a 2a 2a 20 54 68 65 20 75 73 65 20 6f ()..** The use o
a44e0 66 20 5f 6d 73 69 7a 65 28 29 20 69 73 20 61 75 f _msize() is au
a44f0 74 6f 6d 61 74 69 63 2c 20 62 75 74 20 63 61 6e tomatic, but can
a4500 20 62 65 20 64 69 73 61 62 6c 65 64 20 62 79 20 be disabled by
a4510 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 77 69 74 compiling.** wit
a4520 68 20 2d 44 53 51 4c 49 54 45 5f 57 49 54 48 4f h -DSQLITE_WITHO
a4530 55 54 5f 4d 53 49 5a 45 0a 2a 2f 0a 23 69 66 20 UT_MSIZE.*/.#if
a4540 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 defined(_MSC_VER
a4550 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
a4560 4c 49 54 45 5f 57 49 54 48 4f 55 54 5f 4d 53 49 LITE_WITHOUT_MSI
a4570 5a 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c ZE).# define SQL
a4580 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a 45 20 5f ITE_MALLOCSIZE _
a4590 6d 73 69 7a 65 0a 23 65 6e 64 69 66 0a 0a 23 69 msize.#endif..#i
a45a0 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c f defined(__APPL
a45b0 45 5f 5f 29 20 26 26 20 21 64 65 66 69 6e 65 64 E__) && !defined
a45c0 28 53 51 4c 49 54 45 5f 57 49 54 48 4f 55 54 5f (SQLITE_WITHOUT_
a45d0 5a 4f 4e 45 4d 41 4c 4c 4f 43 29 0a 0a 2f 2a 0a ZONEMALLOC)../*.
a45e0 2a 2a 20 55 73 65 20 74 68 65 20 7a 6f 6e 65 20 ** Use the zone
a45f0 61 6c 6c 6f 63 61 74 6f 72 20 61 76 61 69 6c 61 allocator availa
a4600 62 6c 65 20 6f 6e 20 61 70 70 6c 65 20 70 72 6f ble on apple pro
a4610 64 75 63 74 73 20 75 6e 6c 65 73 73 20 74 68 65 ducts unless the
a4620 0a 2a 2a 20 53 51 4c 49 54 45 5f 57 49 54 48 4f .** SQLITE_WITHO
a4630 55 54 5f 5a 4f 4e 45 4d 41 4c 4c 4f 43 20 73 79 UT_ZONEMALLOC sy
a4640 6d 62 6f 6c 20 69 73 20 64 65 66 69 6e 65 64 2e mbol is defined.
a4650 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 .*/.#include <sy
a4660 73 2f 73 79 73 63 74 6c 2e 68 3e 0a 23 69 6e 63 s/sysctl.h>.#inc
a4670 6c 75 64 65 20 3c 6d 61 6c 6c 6f 63 2f 6d 61 6c lude <malloc/mal
a4680 6c 6f 63 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 loc.h>.#include
a4690 3c 6c 69 62 6b 65 72 6e 2f 4f 53 41 74 6f 6d 69 <libkern/OSAtomi
a46a0 63 2e 68 3e 0a 73 74 61 74 69 63 20 6d 61 6c 6c c.h>.static mall
a46b0 6f 63 5f 7a 6f 6e 65 5f 74 2a 20 5f 73 71 6c 69 oc_zone_t* _sqli
a46c0 74 65 5a 6f 6e 65 5f 3b 0a 23 64 65 66 69 6e 65 teZone_;.#define
a46d0 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 28 78 SQLITE_MALLOC(x
a46e0 29 20 6d 61 6c 6c 6f 63 5f 7a 6f 6e 65 5f 6d 61 ) malloc_zone_ma
a46f0 6c 6c 6f 63 28 5f 73 71 6c 69 74 65 5a 6f 6e 65 lloc(_sqliteZone
a4700 5f 2c 20 28 78 29 29 0a 23 64 65 66 69 6e 65 20 _, (x)).#define
a4710 53 51 4c 49 54 45 5f 46 52 45 45 28 78 29 20 6d SQLITE_FREE(x) m
a4720 61 6c 6c 6f 63 5f 7a 6f 6e 65 5f 66 72 65 65 28 alloc_zone_free(
a4730 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f 2c 20 28 78 _sqliteZone_, (x
a4740 29 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 ));.#define SQLI
a4750 54 45 5f 52 45 41 4c 4c 4f 43 28 78 2c 79 29 20 TE_REALLOC(x,y)
a4760 6d 61 6c 6c 6f 63 5f 7a 6f 6e 65 5f 72 65 61 6c malloc_zone_real
a4770 6c 6f 63 28 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f loc(_sqliteZone_
a4780 2c 20 28 78 29 2c 20 28 79 29 29 0a 23 64 65 66 , (x), (y)).#def
a4790 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f ine SQLITE_MALLO
a47a0 43 53 49 5a 45 28 78 29 20 5c 0a 20 20 20 20 20 CSIZE(x) \.
a47b0 20 20 20 28 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f (_sqliteZone_
a47c0 20 3f 20 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f 2d ? _sqliteZone_-
a47d0 3e 73 69 7a 65 28 5f 73 71 6c 69 74 65 5a 6f 6e >size(_sqliteZon
a47e0 65 5f 2c 78 29 20 3a 20 6d 61 6c 6c 6f 63 5f 73 e_,x) : malloc_s
a47f0 69 7a 65 28 78 29 29 0a 0a 23 65 6c 73 65 20 2f ize(x))..#else /
a4800 2a 20 69 66 20 6e 6f 74 20 5f 5f 41 50 50 4c 45 * if not __APPLE
a4810 5f 5f 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 55 73 65 __ */../*.** Use
a4820 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 standard C libr
a4830 61 72 79 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 66 ary malloc and f
a4840 72 65 65 20 6f 6e 20 6e 6f 6e 2d 41 70 70 6c 65 ree on non-Apple
a4850 20 73 79 73 74 65 6d 73 2e 20 20 0a 2a 2a 20 41 systems. .** A
a4860 6c 73 6f 20 75 73 65 64 20 62 79 20 41 70 70 6c lso used by Appl
a4870 65 20 73 79 73 74 65 6d 73 20 69 66 20 53 51 4c e systems if SQL
a4880 49 54 45 5f 57 49 54 48 4f 55 54 5f 5a 4f 4e 45 ITE_WITHOUT_ZONE
a4890 4d 41 4c 4c 4f 43 20 69 73 20 64 65 66 69 6e 65 MALLOC is define
a48a0 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 d..*/.#define SQ
a48b0 4c 49 54 45 5f 4d 41 4c 4c 4f 43 28 78 29 20 20 LITE_MALLOC(x)
a48c0 20 20 6d 61 6c 6c 6f 63 28 78 29 0a 23 64 65 66 malloc(x).#def
a48d0 69 6e 65 20 53 51 4c 49 54 45 5f 46 52 45 45 28 ine SQLITE_FREE(
a48e0 78 29 20 20 20 20 20 20 66 72 65 65 28 78 29 0a x) free(x).
a48f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 #define SQLITE_R
a4900 45 41 4c 4c 4f 43 28 78 2c 79 29 20 72 65 61 6c EALLOC(x,y) real
a4910 6c 6f 63 28 28 78 29 2c 28 79 29 29 0a 0a 23 69 loc((x),(y))..#i
a4920 66 20 28 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f f (defined(_MSC_
a4930 56 45 52 29 20 26 26 20 21 64 65 66 69 6e 65 64 VER) && !defined
a4940 28 53 51 4c 49 54 45 5f 57 49 54 48 4f 55 54 5f (SQLITE_WITHOUT_
a4950 4d 53 49 5a 45 29 29 20 5c 0a 20 20 20 20 20 20 MSIZE)) \.
a4960 7c 7c 20 28 64 65 66 69 6e 65 64 28 48 41 56 45 || (defined(HAVE
a4970 5f 4d 41 4c 4c 4f 43 5f 48 29 20 26 26 20 64 65 _MALLOC_H) && de
a4980 66 69 6e 65 64 28 48 41 56 45 5f 4d 41 4c 4c 4f fined(HAVE_MALLO
a4990 43 5f 55 53 41 42 4c 45 5f 53 49 5a 45 29 29 0a C_USABLE_SIZE)).
a49a0 23 20 69 6e 63 6c 75 64 65 20 3c 6d 61 6c 6c 6f # include <mallo
a49b0 63 2e 68 3e 20 20 20 20 2f 2a 20 4e 65 65 64 65 c.h> /* Neede
a49c0 64 20 66 6f 72 20 6d 61 6c 6c 6f 63 5f 75 73 61 d for malloc_usa
a49d0 62 6c 65 5f 73 69 7a 65 20 6f 6e 20 6c 69 6e 75 ble_size on linu
a49e0 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 x */.#endif.#ifd
a49f0 65 66 20 48 41 56 45 5f 4d 41 4c 4c 4f 43 5f 55 ef HAVE_MALLOC_U
a4a00 53 41 42 4c 45 5f 53 49 5a 45 0a 23 20 69 66 6e SABLE_SIZE.# ifn
a4a10 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f def SQLITE_MALLO
a4a20 43 53 49 5a 45 0a 23 20 20 64 65 66 69 6e 65 20 CSIZE.# define
a4a30 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a SQLITE_MALLOCSIZ
a4a40 45 28 78 29 20 6d 61 6c 6c 6f 63 5f 75 73 61 62 E(x) malloc_usab
a4a50 6c 65 5f 73 69 7a 65 28 78 29 0a 23 20 65 6e 64 le_size(x).# end
a4a60 69 66 0a 23 65 6c 73 65 0a 23 20 75 6e 64 65 66 if.#else.# undef
a4a70 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 SQLITE_MALLOCSI
a4a80 5a 45 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 ZE.#endif..#endi
a4a90 66 20 2f 2a 20 5f 5f 41 50 50 4c 45 5f 5f 20 6f f /* __APPLE__ o
a4aa0 72 20 6e 6f 74 20 5f 5f 41 50 50 4c 45 5f 5f 20 r not __APPLE__
a4ab0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 6d */../*.** Like m
a4ac0 61 6c 6c 6f 63 28 29 2c 20 62 75 74 20 72 65 6d alloc(), but rem
a4ad0 65 6d 62 65 72 20 74 68 65 20 73 69 7a 65 20 6f ember the size o
a4ae0 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
a4af0 0a 2a 2a 20 73 6f 20 74 68 61 74 20 77 65 20 63 .** so that we c
a4b00 61 6e 20 66 69 6e 64 20 69 74 20 6c 61 74 65 72 an find it later
a4b10 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4d 65 using sqlite3Me
a4b20 6d 53 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 mSize()..**.** F
a4b30 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 or this low-leve
a4b40 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 72 l routine, we ar
a4b50 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 e guaranteed tha
a4b60 74 20 6e 42 79 74 65 3e 30 20 62 65 63 61 75 73 t nByte>0 becaus
a4b70 65 0a 2a 2a 20 63 61 73 65 73 20 6f 66 20 6e 42 e.** cases of nB
a4b80 79 74 65 3c 3d 30 20 77 69 6c 6c 20 62 65 20 69 yte<=0 will be i
a4b90 6e 74 65 72 63 65 70 74 65 64 20 61 6e 64 20 64 ntercepted and d
a4ba0 65 61 6c 74 20 77 69 74 68 20 62 79 20 68 69 67 ealt with by hig
a4bb0 68 65 72 20 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 her level.** rou
a4bc0 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 tines..*/.static
a4bd0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 void *sqlite3Me
a4be0 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 mMalloc(int nByt
a4bf0 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 e){.#ifdef SQLIT
a4c00 45 5f 4d 41 4c 4c 4f 43 53 49 5a 45 0a 20 20 76 E_MALLOCSIZE. v
a4c10 6f 69 64 20 2a 70 20 3d 20 53 51 4c 49 54 45 5f oid *p = SQLITE_
a4c20 4d 41 4c 4c 4f 43 28 20 6e 42 79 74 65 20 29 3b MALLOC( nByte );
a4c30 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 . if( p==0 ){.
a4c40 20 20 20 74 65 73 74 63 61 73 65 28 20 73 71 6c testcase( sql
a4c50 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
a4c60 2e 78 4c 6f 67 21 3d 30 20 29 3b 0a 20 20 20 20 .xLog!=0 );.
a4c70 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 sqlite3_log(SQLI
a4c80 54 45 5f 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c 65 TE_NOMEM, "faile
a4c90 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 25 75 d to allocate %u
a4ca0 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 bytes of memory
a4cb0 22 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 0a 20 ", nByte);. }.
a4cc0 20 72 65 74 75 72 6e 20 70 3b 0a 23 65 6c 73 65 return p;.#else
a4cd0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
a4ce0 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e *p;. assert( n
a4cf0 42 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 Byte>0 );. nByt
a4d00 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 e = ROUND8(nByte
a4d10 29 3b 0a 20 20 70 20 3d 20 53 51 4c 49 54 45 5f );. p = SQLITE_
a4d20 4d 41 4c 4c 4f 43 28 20 6e 42 79 74 65 2b 38 20 MALLOC( nByte+8
a4d30 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 );. if( p ){.
a4d40 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b 0a p[0] = nByte;.
a4d50 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 65 6c 73 65 p++;. }else
a4d60 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 {. testcase(
a4d70 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
a4d80 66 69 67 2e 78 4c 6f 67 21 3d 30 20 29 3b 0a 20 fig.xLog!=0 );.
a4d90 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 sqlite3_log(S
a4da0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 22 66 61 QLITE_NOMEM, "fa
a4db0 69 6c 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 iled to allocate
a4dc0 20 25 75 20 62 79 74 65 73 20 6f 66 20 6d 65 6d %u bytes of mem
a4dd0 6f 72 79 22 2c 20 6e 42 79 74 65 29 3b 0a 20 20 ory", nByte);.
a4de0 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 }. return (void
a4df0 20 2a 29 70 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a *)p;.#endif.}..
a4e00 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 66 72 65 65 28 /*.** Like free(
a4e10 29 20 62 75 74 20 77 6f 72 6b 73 20 66 6f 72 20 ) but works for
a4e20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 62 74 61 allocations obta
a4e30 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
a4e40 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 3MemMalloc().**
a4e50 6f 72 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 or sqlite3MemRea
a4e60 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f lloc()..**.** Fo
a4e70 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c r this low-level
a4e80 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 6c 72 routine, we alr
a4e90 65 61 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 70 eady know that p
a4ea0 50 72 69 6f 72 21 3d 30 20 73 69 6e 63 65 0a 2a Prior!=0 since.*
a4eb0 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 70 50 * cases where pP
a4ec0 72 69 6f 72 3d 3d 30 20 77 69 6c 6c 20 68 61 76 rior==0 will hav
a4ed0 65 20 62 65 65 6e 20 69 6e 74 65 63 65 70 74 65 e been intecepte
a4ee0 64 20 61 6e 64 20 64 65 61 6c 74 20 77 69 74 68 d and dealt with
a4ef0 0a 2a 2a 20 62 79 20 68 69 67 68 65 72 2d 6c 65 .** by higher-le
a4f00 76 65 6c 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f vel routines..*/
a4f10 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
a4f20 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 ite3MemFree(void
a4f30 20 2a 70 50 72 69 6f 72 29 7b 0a 23 69 66 64 65 *pPrior){.#ifde
a4f40 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 f SQLITE_MALLOCS
a4f50 49 5a 45 0a 20 20 53 51 4c 49 54 45 5f 46 52 45 IZE. SQLITE_FRE
a4f60 45 28 70 50 72 69 6f 72 29 3b 0a 23 65 6c 73 65 E(pPrior);.#else
a4f70 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
a4f80 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 *p = (sqlite3_i
a4f90 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 nt64*)pPrior;.
a4fa0 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d assert( pPrior!=
a4fb0 30 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20 20 53 51 0 );. p--;. SQ
a4fc0 4c 49 54 45 5f 46 52 45 45 28 70 29 3b 0a 23 65 LITE_FREE(p);.#e
a4fd0 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ndif.}../*.** Re
a4fe0 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 port the allocat
a4ff0 65 64 20 73 69 7a 65 20 6f 66 20 61 20 70 72 69 ed size of a pri
a5000 6f 72 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 78 or return from x
a5010 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 78 Malloc().** or x
a5020 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 Realloc()..*/.st
a5030 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
a5040 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50 MemSize(void *pP
a5050 72 69 6f 72 29 7b 0a 23 69 66 64 65 66 20 53 51 rior){.#ifdef SQ
a5060 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a 45 0a LITE_MALLOCSIZE.
a5070 20 20 72 65 74 75 72 6e 20 70 50 72 69 6f 72 20 return pPrior
a5080 3f 20 28 69 6e 74 29 53 51 4c 49 54 45 5f 4d 41 ? (int)SQLITE_MA
a5090 4c 4c 4f 43 53 49 5a 45 28 70 50 72 69 6f 72 29 LLOCSIZE(pPrior)
a50a0 20 3a 20 30 3b 0a 23 65 6c 73 65 0a 20 20 73 71 : 0;.#else. sq
a50b0 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a lite3_int64 *p;.
a50c0 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 if( pPrior==0
a50d0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 ) return 0;. p
a50e0 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 = (sqlite3_int64
a50f0 2a 29 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b *)pPrior;. p--;
a5100 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 . return (int)p
a5110 5b 30 5d 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f [0];.#endif.}../
a5120 2a 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c 6f *.** Like reallo
a5130 63 28 29 2e 20 20 52 65 73 69 7a 65 20 61 6e 20 c(). Resize an
a5140 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 allocation previ
a5150 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 ously obtained f
a5160 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 65 rom.** sqlite3Me
a5170 6d 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a mMalloc()..**.**
a5180 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 For this low-le
a5190 76 65 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 77 vel interface, w
a51a0 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 e know that pPri
a51b0 6f 72 21 3d 30 2e 20 20 43 61 73 65 73 20 77 68 or!=0. Cases wh
a51c0 65 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d 30 ere.** pPrior==0
a51d0 20 77 68 69 6c 65 20 68 61 76 65 20 62 65 65 6e while have been
a51e0 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79 20 intercepted by
a51f0 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 higher-level rou
a5200 74 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64 69 tine and.** redi
a5210 72 65 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c 6f rected to xMallo
a5220 63 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 77 c. Similarly, w
a5230 65 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79 74 e know that nByt
a5240 65 3e 30 20 62 65 63 61 75 73 65 73 0a 2a 2a 20 e>0 becauses.**
a5250 63 61 73 65 73 20 77 68 65 72 65 20 6e 42 79 74 cases where nByt
a5260 65 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 e<=0 will have b
a5270 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 een intercepted
a5280 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 0a by higher-level.
a5290 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 ** routines and
a52a0 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 78 46 redirected to xF
a52b0 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ree..*/.static v
a52c0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 oid *sqlite3MemR
a52d0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 ealloc(void *pPr
a52e0 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b ior, int nByte){
a52f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d .#ifdef SQLITE_M
a5300 41 4c 4c 4f 43 53 49 5a 45 0a 20 20 76 6f 69 64 ALLOCSIZE. void
a5310 20 2a 70 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 *p = SQLITE_REA
a5320 4c 4c 4f 43 28 70 50 72 69 6f 72 2c 20 6e 42 79 LLOC(pPrior, nBy
a5330 74 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 te);. if( p==0
a5340 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 ){. testcase(
a5350 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
a5360 6e 66 69 67 2e 78 4c 6f 67 21 3d 30 20 29 3b 0a nfig.xLog!=0 );.
a5370 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 sqlite3_log(
a5380 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 0a 20 20 SQLITE_NOMEM,.
a5390 20 20 20 20 22 66 61 69 6c 65 64 20 6d 65 6d 6f "failed memo
a53a0 72 79 20 72 65 73 69 7a 65 20 25 75 20 74 6f 20 ry resize %u to
a53b0 25 75 20 62 79 74 65 73 22 2c 0a 20 20 20 20 20 %u bytes",.
a53c0 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 SQLITE_MALLOCSI
a53d0 5a 45 28 70 50 72 69 6f 72 29 2c 20 6e 42 79 74 ZE(pPrior), nByt
a53e0 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e e);. }. return
a53f0 20 70 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 p;.#else. sqli
a5400 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 te3_int64 *p = (
a5410 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 sqlite3_int64*)p
a5420 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 Prior;. assert(
a5430 20 70 50 72 69 6f 72 21 3d 30 20 26 26 20 6e 42 pPrior!=0 && nB
a5440 79 74 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 yte>0 );. asser
a5450 74 28 20 6e 42 79 74 65 3d 3d 52 4f 55 4e 44 38 t( nByte==ROUND8
a5460 28 6e 42 79 74 65 29 20 29 3b 20 2f 2a 20 45 56 (nByte) ); /* EV
a5470 3a 20 52 2d 34 36 31 39 39 2d 33 30 32 34 39 20 : R-46199-30249
a5480 2a 2f 0a 20 20 70 2d 2d 3b 0a 20 20 70 20 3d 20 */. p--;. p =
a5490 53 51 4c 49 54 45 5f 52 45 41 4c 4c 4f 43 28 70 SQLITE_REALLOC(p
a54a0 2c 20 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 , nByte+8 );. i
a54b0 66 28 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d f( p ){. p[0]
a54c0 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b = nByte;. p+
a54d0 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 +;. }else{.
a54e0 74 65 73 74 63 61 73 65 28 20 73 71 6c 69 74 65 testcase( sqlite
a54f0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 3GlobalConfig.xL
a5500 6f 67 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c og!=0 );. sql
a5510 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54 45 5f ite3_log(SQLITE_
a5520 4e 4f 4d 45 4d 2c 0a 20 20 20 20 20 20 22 66 61 NOMEM,. "fa
a5530 69 6c 65 64 20 6d 65 6d 6f 72 79 20 72 65 73 69 iled memory resi
a5540 7a 65 20 25 75 20 74 6f 20 25 75 20 62 79 74 65 ze %u to %u byte
a5550 73 22 2c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 s",. sqlite
a5560 33 4d 65 6d 53 69 7a 65 28 70 50 72 69 6f 72 29 3MemSize(pPrior)
a5570 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 0a 20 20 , nByte);. }.
a5580 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b return (void*)p;
a5590 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
a55a0 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 Round up a requ
a55b0 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 est size to the
a55c0 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 next valid alloc
a55d0 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 ation size..*/.s
a55e0 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 tatic int sqlite
a55f0 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20 3MemRoundup(int
a5600 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 n){. return ROU
a5610 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ND8(n);.}../*.**
a5620 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 Initialize this
a5630 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 module..*/.stat
a5640 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 ic int sqlite3Me
a5650 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 mInit(void *NotU
a5660 73 65 64 29 7b 0a 23 69 66 20 64 65 66 69 6e 65 sed){.#if define
a5670 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 d(__APPLE__) &&
a5680 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
a5690 57 49 54 48 4f 55 54 5f 5a 4f 4e 45 4d 41 4c 4c WITHOUT_ZONEMALL
a56a0 4f 43 29 0a 20 20 69 6e 74 20 63 70 75 43 6f 75 OC). int cpuCou
a56b0 6e 74 3b 0a 20 20 73 69 7a 65 5f 74 20 6c 65 6e nt;. size_t len
a56c0 3b 0a 20 20 69 66 28 20 5f 73 71 6c 69 74 65 5a ;. if( _sqliteZ
a56d0 6f 6e 65 5f 20 29 7b 0a 20 20 20 20 72 65 74 75 one_ ){. retu
a56e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
a56f0 7d 0a 20 20 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 }. len = sizeof
a5700 28 63 70 75 43 6f 75 6e 74 29 3b 0a 20 20 2f 2a (cpuCount);. /*
a5710 20 4f 6e 65 20 75 73 75 61 6c 6c 79 20 77 61 6e One usually wan
a5720 74 73 20 74 6f 20 75 73 65 20 68 77 2e 61 63 63 ts to use hw.acc
a5730 74 69 76 65 63 70 75 20 66 6f 72 20 4d 54 20 64 tivecpu for MT d
a5740 65 63 69 73 69 6f 6e 73 2c 20 62 75 74 20 6e 6f ecisions, but no
a5750 74 20 68 65 72 65 20 2a 2f 0a 20 20 73 79 73 63 t here */. sysc
a5760 74 6c 62 79 6e 61 6d 65 28 22 68 77 2e 6e 63 70 tlbyname("hw.ncp
a5770 75 22 2c 20 26 63 70 75 43 6f 75 6e 74 2c 20 26 u", &cpuCount, &
a5780 6c 65 6e 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20 len, NULL, 0);.
a5790 20 69 66 28 20 63 70 75 43 6f 75 6e 74 3e 31 20 if( cpuCount>1
a57a0 29 7b 0a 20 20 20 20 2f 2a 20 64 65 66 65 72 20 ){. /* defer
a57b0 4d 54 20 64 65 63 69 73 69 6f 6e 73 20 74 6f 20 MT decisions to
a57c0 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 20 2a 2f system malloc */
a57d0 0a 20 20 20 20 5f 73 71 6c 69 74 65 5a 6f 6e 65 . _sqliteZone
a57e0 5f 20 3d 20 6d 61 6c 6c 6f 63 5f 64 65 66 61 75 _ = malloc_defau
a57f0 6c 74 5f 7a 6f 6e 65 28 29 3b 0a 20 20 7d 65 6c lt_zone();. }el
a5800 73 65 7b 0a 20 20 20 20 2f 2a 20 6f 6e 6c 79 20 se{. /* only
a5810 31 20 63 6f 72 65 2c 20 75 73 65 20 6f 75 72 20 1 core, use our
a5820 6f 77 6e 20 7a 6f 6e 65 20 74 6f 20 63 6f 6e 74 own zone to cont
a5830 65 6e 74 69 6f 6e 20 6f 76 65 72 20 67 6c 6f 62 ention over glob
a5840 61 6c 20 6c 6f 63 6b 73 2c 20 0a 20 20 20 20 2a al locks, . *
a5850 2a 20 65 2e 67 2e 20 77 65 20 68 61 76 65 20 6f * e.g. we have o
a5860 75 72 20 6f 77 6e 20 64 65 64 69 63 61 74 65 64 ur own dedicated
a5870 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 62 6f locks */. bo
a5880 6f 6c 20 73 75 63 63 65 73 73 3b 0a 20 20 20 20 ol success;.
a5890 6d 61 6c 6c 6f 63 5f 7a 6f 6e 65 5f 74 2a 20 6e malloc_zone_t* n
a58a0 65 77 7a 6f 6e 65 20 3d 20 6d 61 6c 6c 6f 63 5f ewzone = malloc_
a58b0 63 72 65 61 74 65 5f 7a 6f 6e 65 28 34 30 39 36 create_zone(4096
a58c0 2c 20 30 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f 63 , 0);. malloc
a58d0 5f 73 65 74 5f 7a 6f 6e 65 5f 6e 61 6d 65 28 6e _set_zone_name(n
a58e0 65 77 7a 6f 6e 65 2c 20 22 53 71 6c 69 74 65 5f ewzone, "Sqlite_
a58f0 48 65 61 70 22 29 3b 0a 20 20 20 20 64 6f 7b 0a Heap");. do{.
a5900 20 20 20 20 20 20 73 75 63 63 65 73 73 20 3d 20 success =
a5910 4f 53 41 74 6f 6d 69 63 43 6f 6d 70 61 72 65 41 OSAtomicCompareA
a5920 6e 64 53 77 61 70 50 74 72 42 61 72 72 69 65 72 ndSwapPtrBarrier
a5930 28 4e 55 4c 4c 2c 20 6e 65 77 7a 6f 6e 65 2c 20 (NULL, newzone,
a5940 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
a5950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5960 20 20 28 76 6f 69 64 20 2a 20 76 6f 6c 61 74 69 (void * volati
a5970 6c 65 20 2a 29 26 5f 73 71 6c 69 74 65 5a 6f 6e le *)&_sqliteZon
a5980 65 5f 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 e_);. }while(
a5990 21 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f 29 3b 0a !_sqliteZone_);.
a59a0 20 20 20 20 69 66 28 20 21 73 75 63 63 65 73 73 if( !success
a59b0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 73 6f 6d ){. /* som
a59c0 65 62 6f 64 79 20 72 65 67 69 73 74 65 72 65 64 ebody registered
a59d0 20 61 20 7a 6f 6e 65 20 66 69 72 73 74 20 2a 2f a zone first */
a59e0 0a 20 20 20 20 20 20 6d 61 6c 6c 6f 63 5f 64 65 . malloc_de
a59f0 73 74 72 6f 79 5f 7a 6f 6e 65 28 6e 65 77 7a 6f stroy_zone(newzo
a5a00 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 ne);. }. }.#
a5a10 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 endif. UNUSED_P
a5a20 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
a5a30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
a5a40 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
a5a50 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 Deinitialize thi
a5a60 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 s module..*/.sta
a5a70 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 tic void sqlite3
a5a80 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 MemShutdown(void
a5a90 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e *NotUsed){. UN
a5aa0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
a5ab0 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 otUsed);. retur
a5ac0 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 n;.}../*.** This
a5ad0 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 routine is the
a5ae0 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 only routine in
a5af0 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 this file with e
a5b00 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e xternal linkage.
a5b10 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 .**.** Populate
a5b20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 the low-level me
a5b30 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
a5b40 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 function pointer
a5b50 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 s in.** sqlite3G
a5b60 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 lobalConfig.m wi
a5b70 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 th pointers to t
a5b80 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 he routines in t
a5b90 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c his file..*/.SQL
a5ba0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a5bb0 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 sqlite3MemSetDe
a5bc0 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 fault(void){. s
a5bd0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
a5be0 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 te3_mem_methods
a5bf0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d defaultMethods =
a5c00 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d {. sqlite3M
a5c10 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 emMalloc,. s
a5c20 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 qlite3MemFree,.
a5c30 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 sqlite3MemRe
a5c40 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 alloc,. sqli
a5c50 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 te3MemSize,.
a5c60 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 sqlite3MemRound
a5c70 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 up,. sqlite3
a5c80 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 MemInit,. sq
a5c90 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e lite3MemShutdown
a5ca0 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 ,. 0. };.
a5cb0 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 sqlite3_config(S
a5cc0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c QLITE_CONFIG_MAL
a5cd0 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 LOC, &defaultMet
a5ce0 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 hods);.}..#endif
a5cf0 20 2f 2a 20 53 51 4c 49 54 45 5f 53 59 53 54 45 /* SQLITE_SYSTE
a5d00 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a M_MALLOC */../**
a5d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
a5d20 20 6f 66 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a of mem1.c *****
a5d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
a5d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
a5d70 69 6e 20 66 69 6c 65 20 6d 65 6d 32 2e 63 20 2a in file mem2.c *
a5d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
a5db0 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 ** 2007 August 1
a5dc0 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
a5dd0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
a5de0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
a5df0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
a5e00 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
a5e10 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
a5e20 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
a5e30 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
a5e40 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
a5e50 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
a5e60 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
a5e70 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
a5e80 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
a5e90 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
a5ea0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
a5eb0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
a5ec0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
a5ed0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
a5ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5f20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
a5f30 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 contains low-le
a5f40 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 vel memory alloc
a5f50 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 6f ation drivers fo
a5f60 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 r when.** SQLite
a5f70 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 73 74 will use the st
a5f80 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 61 72 79 andard C-library
a5f90 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f malloc/realloc/
a5fa0 66 72 65 65 20 69 6e 74 65 72 66 61 63 65 0a 2a free interface.*
a5fb0 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 * to obtain the
a5fc0 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 20 memory it needs
a5fd0 77 68 69 6c 65 20 61 64 64 69 6e 67 20 6c 6f 74 while adding lot
a5fe0 73 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 s of additional
a5ff0 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 69 6e 66 debugging.** inf
a6000 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 65 61 63 68 ormation to each
a6010 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 6f allocation in o
a6020 72 64 65 72 20 74 6f 20 68 65 6c 70 20 64 65 74 rder to help det
a6030 65 63 74 20 61 6e 64 20 66 69 78 20 6d 65 6d 6f ect and fix memo
a6040 72 79 0a 2a 2a 20 6c 65 61 6b 73 20 61 6e 64 20 ry.** leaks and
a6050 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 65 72 72 memory usage err
a6060 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ors..**.** This
a6070 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d file contains im
a6080 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 plementations of
a6090 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d the low-level m
a60a0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
a60b0 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65 .** routines spe
a60c0 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 73 71 cified in the sq
a60d0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
a60e0 73 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 0a 2f 2a s object..*/../*
a60f0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
a6100 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
a6110 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64 llocator is used
a6120 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20 only if the.**
a6130 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 SQLITE_MEMDEBUG
a6140 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 macro is defined
a6150 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
a6160 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f 2a 0a 2a E_MEMDEBUG../*.*
a6170 2a 20 54 68 65 20 62 61 63 6b 74 72 61 63 65 20 * The backtrace
a6180 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 functionality is
a6190 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
a61a0 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f 0a 23 69 with GLIBC.*/.#i
a61b0 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f 5f 0a 20 fdef __GLIBC__.
a61c0 20 65 78 74 65 72 6e 20 69 6e 74 20 62 61 63 6b extern int back
a61d0 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c 69 6e 74 trace(void**,int
a61e0 29 3b 0a 20 20 65 78 74 65 72 6e 20 76 6f 69 64 );. extern void
a61f0 20 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f backtrace_symbo
a6200 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f 6e 73 74 ls_fd(void*const
a6210 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 65 6c 73 *,int,int);.#els
a6220 65 0a 23 20 64 65 66 69 6e 65 20 62 61 63 6b 74 e.# define backt
a6230 72 61 63 65 28 41 2c 42 29 20 31 0a 23 20 64 65 race(A,B) 1.# de
a6240 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 5f 73 fine backtrace_s
a6250 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 2c 43 29 ymbols_fd(A,B,C)
a6260 0a 23 65 6e 64 69 66 0a 2f 2a 20 23 69 6e 63 6c .#endif./* #incl
a6270 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e 20 2a 2f ude <stdio.h> */
a6280 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d 65 6d ../*.** Each mem
a6290 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c ory allocation l
a62a0 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a ooks like this:.
a62b0 2a 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d **.** ---------
a62c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
a62d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
a62e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
a62f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a ---------------.
a6300 2a 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20 20 62 ** | Title | b
a6310 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 acktrace pointer
a6320 73 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 s | MemBlockHdr
a6330 20 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 7c | allocation |
a6340 20 20 45 6e 64 47 75 61 72 64 20 7c 0a 2a 2a 20 EndGuard |.**
a6350 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
a6360 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
a6370 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
a6380 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
a6390 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 ---------.**.**
a63a0 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 The application
a63b0 63 6f 64 65 20 73 65 65 73 20 6f 6e 6c 79 20 61 code sees only a
a63c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
a63d0 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 65 20 allocation. We
a63e0 68 61 76 65 0a 2a 2a 20 74 6f 20 62 61 63 6b 20 have.** to back
a63f0 75 70 20 66 72 6f 6d 20 74 68 65 20 61 6c 6c 6f up from the allo
a6400 63 61 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 74 cation pointer t
a6410 6f 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c o find the MemBl
a6420 6f 63 6b 48 64 72 2e 20 20 54 68 65 0a 2a 2a 20 ockHdr. The.**
a6430 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 74 65 6c 6c MemBlockHdr tell
a6440 73 20 75 73 20 74 68 65 20 73 69 7a 65 20 6f 66 s us the size of
a6450 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
a6460 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f and the number o
a6470 66 0a 2a 2a 20 62 61 63 6b 74 72 61 63 65 20 70 f.** backtrace p
a6480 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 72 65 20 ointers. There
a6490 69 73 20 61 6c 73 6f 20 61 20 67 75 61 72 64 20 is also a guard
a64a0 77 6f 72 64 20 61 74 20 74 68 65 20 65 6e 64 20 word at the end
a64b0 6f 66 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f of the.** MemBlo
a64c0 63 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 ckHdr..*/.struct
a64d0 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7b 0a 20 MemBlockHdr {.
a64e0 20 69 36 34 20 69 53 69 7a 65 3b 20 20 20 20 20 i64 iSize;
a64f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6500 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
a6510 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 this allocation
a6520 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 */. struct MemB
a6530 6c 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c 20 lockHdr *pNext,
a6540 2a 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e 6b *pPrev; /* Link
a6550 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 75 ed list of all u
a6560 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a 2f nfreed memory */
a6570 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 . char nBacktra
a6580 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ce;
a6590 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
a65a0 20 6f 66 20 62 61 63 6b 74 72 61 63 65 73 20 6f of backtraces o
a65b0 6e 20 74 68 69 73 20 61 6c 6c 6f 63 20 2a 2f 0a n this alloc */.
a65c0 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63 char nBacktrac
a65d0 65 53 6c 6f 74 73 3b 20 20 20 20 20 20 20 20 20 eSlots;
a65e0 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 /* Availab
a65f0 6c 65 20 62 61 63 6b 74 72 61 63 65 20 73 6c 6f le backtrace slo
a6600 74 73 20 2a 2f 0a 20 20 75 38 20 6e 54 69 74 6c ts */. u8 nTitl
a6610 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
a6620 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
a6630 79 74 65 73 20 6f 66 20 74 69 74 6c 65 3b 20 69 ytes of title; i
a6640 6e 63 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f 0a ncludes '\0' */.
a6650 20 20 75 38 20 65 54 79 70 65 3b 20 20 20 20 20 u8 eType;
a6660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6670 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 /* Allocat
a6680 69 6f 6e 20 74 79 70 65 20 63 6f 64 65 20 2a 2f ion type code */
a6690 0a 20 20 69 6e 74 20 69 46 6f 72 65 47 75 61 72 . int iForeGuar
a66a0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
a66b0 20 20 20 20 20 20 20 2f 2a 20 47 75 61 72 64 20 /* Guard
a66c0 77 6f 72 64 20 66 6f 72 20 73 61 6e 69 74 79 20 word for sanity
a66d0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 61 */.};../*.** Gua
a66e0 72 64 20 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 66 rd words.*/.#def
a66f0 69 6e 65 20 46 4f 52 45 47 55 41 52 44 20 30 78 ine FOREGUARD 0x
a6700 38 30 46 35 45 31 35 33 0a 23 64 65 66 69 6e 65 80F5E153.#define
a6710 20 52 45 41 52 47 55 41 52 44 20 30 78 45 34 36 REARGUARD 0xE46
a6720 37 36 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 76B53../*.** Num
a6730 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 69 ber of malloc si
a6740 7a 65 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 6f ze increments to
a6750 20 74 72 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 69 track..*/.#defi
a6760 6e 65 20 4e 43 53 49 5a 45 20 20 31 30 30 30 0a ne NCSIZE 1000.
a6770 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 ./*.** All of th
a6780 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c e static variabl
a6790 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20 es used by this
a67a0 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 module are colle
a67b0 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 cted.** into a s
a67c0 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 ingle structure
a67d0 6e 61 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54 68 named "mem". Th
a67e0 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 is is to keep th
a67f0 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 e.** static vari
a6800 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 ables organized
a6810 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 and to reduce na
a6820 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f mespace pollutio
a6830 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d n.** when this m
a6840 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 odule is combine
a6850 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 d with other in
a6860 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e the amalgamation
a6870 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
a6880 63 74 20 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a ct {. . /*. *
a6890 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 * Mutex to contr
a68a0 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 ol access to the
a68b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
a68c0 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 on subsystem..
a68d0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
a68e0 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a ex *mutex;.. /*
a68f0 0a 20 20 2a 2a 20 48 65 61 64 20 61 6e 64 20 74 . ** Head and t
a6900 61 69 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 ail of a linked
a6910 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73 list of all outs
a6920 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 tanding allocati
a6930 6f 6e 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63 ons. */. struc
a6940 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 t MemBlockHdr *p
a6950 46 69 72 73 74 3b 0a 20 20 73 74 72 75 63 74 20 First;. struct
a6960 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61 MemBlockHdr *pLa
a6970 73 74 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a st;. . /*. **
a6980 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c The number of l
a6990 65 76 65 6c 73 20 6f 66 20 62 61 63 6b 74 72 61 evels of backtra
a69a0 63 65 20 74 6f 20 73 61 76 65 20 69 6e 20 6e 65 ce to save in ne
a69b0 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 w allocations..
a69c0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74 */. int nBackt
a69d0 72 61 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78 race;. void (*x
a69e0 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 Backtrace)(int,
a69f0 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a int, void **);..
a6a00 20 20 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20 /*. ** Title
a6a10 74 65 78 74 20 74 6f 20 69 6e 73 65 72 74 20 69 text to insert i
a6a20 6e 20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68 20 n front of each
a6a30 62 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74 block. */. int
a6a40 20 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 nTitle;
a6a50 2f 2a 20 42 79 74 65 73 20 6f 66 20 7a 54 69 74 /* Bytes of zTit
a6a60 6c 65 20 74 6f 20 73 61 76 65 2e 20 20 49 6e 63 le to save. Inc
a6a70 6c 75 64 65 73 20 27 5c 30 27 20 61 6e 64 20 70 ludes '\0' and p
a6a80 61 64 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 adding */. char
a6a90 20 7a 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f zTitle[100]; /
a6aa0 2a 20 54 68 65 20 74 69 74 6c 65 20 74 65 78 74 * The title text
a6ab0 20 2a 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 */.. /* . **
a6ac0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73 sqlite3MallocDis
a6ad0 61 6c 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e allow() incremen
a6ae0 74 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ts the following
a6af0 20 63 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73 counter.. ** s
a6b00 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f qlite3MallocAllo
a6b10 77 28 29 20 64 65 63 72 65 6d 65 6e 74 73 20 69 w() decrements i
a6b20 74 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69 t.. */. int di
a6b30 73 61 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f sallow; /* Do no
a6b40 74 20 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61 t allow memory a
a6b50 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 llocation */..
a6b60 2f 2a 0a 20 20 2a 2a 20 47 61 74 68 65 72 20 73 /*. ** Gather s
a6b70 74 61 74 69 73 74 69 63 73 20 6f 6e 20 74 68 65 tatistics on the
a6b80 20 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 sizes of memory
a6b90 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 allocations..
a6ba0 2a 2a 20 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 20 ** nAlloc[i] is
a6bb0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c the number of al
a6bc0 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 location attempt
a6bd0 73 20 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62 79 s of i*8. ** by
a6be0 74 65 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 20 tes. i==NCSIZE
a6bf0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
a6c00 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 allocation atte
a6c10 6d 70 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73 69 mpts for. ** si
a6c20 7a 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e 43 zes more than NC
a6c30 53 49 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20 20 SIZE*8 bytes..
a6c40 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 5b */. int nAlloc[
a6c50 4e 43 53 49 5a 45 5d 3b 20 20 20 20 20 20 2f 2a NCSIZE]; /*
a6c60 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 Total number of
a6c70 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a allocations */.
a6c80 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 5b 4e int nCurrent[N
a6c90 43 53 49 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 75 CSIZE]; /* Cu
a6ca0 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 rrent number of
a6cb0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 allocations */.
a6cc0 20 69 6e 74 20 6d 78 43 75 72 72 65 6e 74 5b 4e int mxCurrent[N
a6cd0 43 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 67 CSIZE]; /* Hig
a6ce0 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 hwater mark for
a6cf0 6e 43 75 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 6d nCurrent */..} m
a6d00 65 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 em;.../*.** Adju
a6d10 73 74 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 st memory usage
a6d20 73 74 61 74 69 73 74 69 63 73 0a 2a 2f 0a 73 74 statistics.*/.st
a6d30 61 74 69 63 20 76 6f 69 64 20 61 64 6a 75 73 74 atic void adjust
a6d40 53 74 61 74 73 28 69 6e 74 20 69 53 69 7a 65 2c Stats(int iSize,
a6d50 20 69 6e 74 20 69 6e 63 72 65 6d 65 6e 74 29 7b int increment){
a6d60 0a 20 20 69 6e 74 20 69 20 3d 20 52 4f 55 4e 44 . int i = ROUND
a6d70 38 28 69 53 69 7a 65 29 2f 38 3b 0a 20 20 69 66 8(iSize)/8;. if
a6d80 28 20 69 3e 4e 43 53 49 5a 45 2d 31 20 29 7b 0a ( i>NCSIZE-1 ){.
a6d90 20 20 20 20 69 20 3d 20 4e 43 53 49 5a 45 20 2d i = NCSIZE -
a6da0 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e 1;. }. if( in
a6db0 63 72 65 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 20 crement>0 ){.
a6dc0 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b mem.nAlloc[i]++
a6dd0 3b 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 ;. mem.nCurre
a6de0 6e 74 5b 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 28 nt[i]++;. if(
a6df0 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d mem.nCurrent[i]
a6e00 3e 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 >mem.mxCurrent[i
a6e10 5d 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 6d ] ){. mem.m
a6e20 78 43 75 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65 xCurrent[i] = me
a6e30 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3b 0a 20 m.nCurrent[i];.
a6e40 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
a6e50 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 mem.nCurrent[i
a6e60 5d 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ]--;. assert(
a6e70 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d mem.nCurrent[i]
a6e80 3e 3d 30 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a >=0 );. }.}../*
a6e90 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 61 6c 6c .** Given an all
a6ea0 6f 63 61 74 69 6f 6e 2c 20 66 69 6e 64 20 74 68 ocation, find th
a6eb0 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 66 6f e MemBlockHdr fo
a6ec0 72 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f r that allocatio
a6ed0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f n..**.** This ro
a6ee0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 65 utine checks the
a6ef0 20 67 75 61 72 64 73 20 61 74 20 65 69 74 68 65 guards at eithe
a6f00 72 20 65 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c r end of the all
a6f10 6f 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 69 ocation and.** i
a6f20 66 20 74 68 65 79 20 61 72 65 20 69 6e 63 6f 72 f they are incor
a6f30 72 65 63 74 20 69 74 20 61 73 73 65 72 74 73 2e rect it asserts.
a6f40 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 .*/.static struc
a6f50 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 73 t MemBlockHdr *s
a6f60 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 qlite3MemsysGetH
a6f70 65 61 64 65 72 28 76 6f 69 64 20 2a 70 41 6c 6c eader(void *pAll
a6f80 6f 63 61 74 69 6f 6e 29 7b 0a 20 20 73 74 72 75 ocation){. stru
a6f90 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
a6fa0 70 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a p;. int *pInt;.
a6fb0 20 20 75 38 20 2a 70 55 38 3b 0a 20 20 69 6e 74 u8 *pU8;. int
a6fc0 20 6e 52 65 73 65 72 76 65 3b 0a 0a 20 20 70 20 nReserve;.. p
a6fd0 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f = (struct MemBlo
a6fe0 63 6b 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69 ckHdr*)pAllocati
a6ff0 6f 6e 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73 on;. p--;. ass
a7000 65 72 74 28 20 70 2d 3e 69 46 6f 72 65 47 75 61 ert( p->iForeGua
a7010 72 64 3d 3d 28 69 6e 74 29 46 4f 52 45 47 55 41 rd==(int)FOREGUA
a7020 52 44 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 65 RD );. nReserve
a7030 20 3d 20 52 4f 55 4e 44 38 28 70 2d 3e 69 53 69 = ROUND8(p->iSi
a7040 7a 65 29 3b 0a 20 20 70 49 6e 74 20 3d 20 28 69 ze);. pInt = (i
a7050 6e 74 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b nt*)pAllocation;
a7060 0a 20 20 70 55 38 20 3d 20 28 75 38 2a 29 70 41 . pU8 = (u8*)pA
a7070 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73 73 llocation;. ass
a7080 65 72 74 28 20 70 49 6e 74 5b 6e 52 65 73 65 72 ert( pInt[nReser
a7090 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 3d ve/sizeof(int)]=
a70a0 3d 28 69 6e 74 29 52 45 41 52 47 55 41 52 44 20 =(int)REARGUARD
a70b0 29 3b 0a 20 20 2f 2a 20 54 68 69 73 20 63 68 65 );. /* This che
a70c0 63 6b 73 20 61 6e 79 20 6f 66 20 74 68 65 20 22 cks any of the "
a70d0 65 78 74 72 61 22 20 62 79 74 65 73 20 61 6c 6c extra" bytes all
a70e0 6f 63 61 74 65 64 20 64 75 65 0a 20 20 2a 2a 20 ocated due. **
a70f0 74 6f 20 72 6f 75 6e 64 69 6e 67 20 75 70 20 74 to rounding up t
a7100 6f 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e o an 8 byte boun
a7110 64 61 72 79 20 74 6f 20 65 6e 73 75 72 65 20 0a dary to ensure .
a7120 20 20 2a 2a 20 74 68 65 79 20 68 61 76 65 6e 27 ** they haven'
a7130 74 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74 t been overwritt
a7140 65 6e 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 en.. */. while
a7150 28 20 6e 52 65 73 65 72 76 65 2d 2d 20 3e 20 70 ( nReserve-- > p
a7160 2d 3e 69 53 69 7a 65 20 29 20 61 73 73 65 72 74 ->iSize ) assert
a7170 28 20 70 55 38 5b 6e 52 65 73 65 72 76 65 5d 3d ( pU8[nReserve]=
a7180 3d 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72 =0x65 );. retur
a7190 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 n p;.}../*.** Re
a71a0 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
a71b0 6f 66 20 62 79 74 65 73 20 63 75 72 72 65 6e 74 of bytes current
a71c0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 ly allocated at
a71d0 61 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74 address p..*/.st
a71e0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
a71f0 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 MemSize(void *p)
a7200 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c {. struct MemBl
a7210 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 ockHdr *pHdr;.
a7220 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 if( !p ){. re
a7230 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48 turn 0;. }. pH
a7240 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 dr = sqlite3Mems
a7250 79 73 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a ysGetHeader(p);.
a7260 20 20 72 65 74 75 72 6e 20 70 48 64 72 2d 3e 69 return pHdr->i
a7270 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 Size;.}../*.** I
a7280 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
a7290 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
a72a0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 subsystem..*/.st
a72b0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
a72c0 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f MemInit(void *No
a72d0 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 tUsed){. UNUSED
a72e0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
a72f0 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ed);. assert( (
a7300 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 4d 65 sizeof(struct Me
a7310 6d 42 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d 3d mBlockHdr)&7) ==
a7320 20 30 20 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 0 );. if( !sql
a7330 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
a7340 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 .bMemstat ){.
a7350 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 74 /* If memory st
a7360 61 74 75 73 20 69 73 20 65 6e 61 62 6c 65 64 2c atus is enabled,
a7370 20 74 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63 then the malloc
a7380 2e 63 20 77 72 61 70 70 65 72 20 77 69 6c 6c 20 .c wrapper will
a7390 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 68 already. ** h
a73a0 6f 6c 64 20 74 68 65 20 53 54 41 54 49 43 5f 4d old the STATIC_M
a73b0 45 4d 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68 EM mutex when th
a73c0 65 20 72 6f 75 74 69 6e 65 73 20 68 65 72 65 20 e routines here
a73d0 61 72 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a are invoked. */.
a73e0 20 20 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 mem.mutex =
a73f0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
a7400 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
a7410 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a TATIC_MEM);. }.
a7420 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
a7430 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 OK;.}../*.** Dei
a7440 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
a7450 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
a7460 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 subsystem..*/.st
a7470 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
a7480 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 3MemShutdown(voi
a7490 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 d *NotUsed){. U
a74a0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
a74b0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 2e NotUsed);. mem.
a74c0 6d 75 74 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a mutex = 0;.}../*
a74d0 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 .** Round up a r
a74e0 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 equest size to t
a74f0 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c he next valid al
a7500 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a location size..*
a7510 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c /.static int sql
a7520 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 ite3MemRoundup(i
a7530 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 nt n){. return
a7540 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a ROUND8(n);.}../*
a7550 0a 2a 2a 20 46 69 6c 6c 20 61 20 62 75 66 66 65 .** Fill a buffe
a7560 72 20 77 69 74 68 20 70 73 65 75 64 6f 2d 72 61 r with pseudo-ra
a7570 6e 64 6f 6d 20 62 79 74 65 73 2e 20 20 54 68 69 ndom bytes. Thi
a7580 73 20 69 73 20 75 73 65 64 20 74 6f 20 70 72 65 s is used to pre
a7590 73 65 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 set.** the conte
a75a0 6e 74 20 6f 66 20 61 20 6e 65 77 20 6d 65 6d 6f nt of a new memo
a75b0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f ry allocation to
a75c0 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 76 unpredictable v
a75d0 61 6c 75 65 73 20 61 6e 64 0a 2a 2a 20 74 6f 20 alues and.** to
a75e0 63 6c 65 61 72 20 74 68 65 20 63 6f 6e 74 65 6e clear the conten
a75f0 74 20 6f 66 20 61 20 66 72 65 65 64 20 61 6c 6c t of a freed all
a7600 6f 63 61 74 69 6f 6e 20 74 6f 20 75 6e 70 72 65 ocation to unpre
a7610 64 69 63 74 61 62 6c 65 20 76 61 6c 75 65 73 2e dictable values.
a7620 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
a7630 72 61 6e 64 6f 6d 46 69 6c 6c 28 63 68 61 72 20 randomFill(char
a7640 2a 70 42 75 66 2c 20 69 6e 74 20 6e 42 79 74 65 *pBuf, int nByte
a7650 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e ){. unsigned in
a7660 74 20 78 2c 20 79 2c 20 72 3b 0a 20 20 78 20 3d t x, y, r;. x =
a7670 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 SQLITE_PTR_TO_I
a7680 4e 54 28 70 42 75 66 29 3b 0a 20 20 79 20 3d 20 NT(pBuf);. y =
a7690 6e 42 79 74 65 20 7c 20 31 3b 0a 20 20 77 68 69 nByte | 1;. whi
a76a0 6c 65 28 20 6e 42 79 74 65 20 3e 3d 20 34 20 29 le( nByte >= 4 )
a76b0 7b 0a 20 20 20 20 78 20 3d 20 28 78 3e 3e 31 29 {. x = (x>>1)
a76c0 20 5e 20 28 2d 28 78 26 31 29 20 26 20 30 78 64 ^ (-(x&1) & 0xd
a76d0 30 30 30 30 30 30 31 29 3b 0a 20 20 20 20 79 20 0000001);. y
a76e0 3d 20 79 2a 31 31 30 33 35 31 35 32 34 35 20 2b = y*1103515245 +
a76f0 20 31 32 33 34 35 3b 0a 20 20 20 20 72 20 3d 20 12345;. r =
a7700 78 20 5e 20 79 3b 0a 20 20 20 20 2a 28 69 6e 74 x ^ y;. *(int
a7710 2a 29 70 42 75 66 20 3d 20 72 3b 0a 20 20 20 20 *)pBuf = r;.
a7720 70 42 75 66 20 2b 3d 20 34 3b 0a 20 20 20 20 6e pBuf += 4;. n
a7730 42 79 74 65 20 2d 3d 20 34 3b 0a 20 20 7d 0a 20 Byte -= 4;. }.
a7740 20 77 68 69 6c 65 28 20 6e 42 79 74 65 2d 2d 20 while( nByte--
a7750 3e 20 30 20 29 7b 0a 20 20 20 20 78 20 3d 20 28 > 0 ){. x = (
a7760 78 3e 3e 31 29 20 5e 20 28 2d 28 78 26 31 29 20 x>>1) ^ (-(x&1)
a7770 26 20 30 78 64 30 30 30 30 30 30 31 29 3b 0a 20 & 0xd0000001);.
a7780 20 20 20 79 20 3d 20 79 2a 31 31 30 33 35 31 35 y = y*1103515
a7790 32 34 35 20 2b 20 31 32 33 34 35 3b 0a 20 20 20 245 + 12345;.
a77a0 20 72 20 3d 20 78 20 5e 20 79 3b 0a 20 20 20 20 r = x ^ y;.
a77b0 2a 28 70 42 75 66 2b 2b 29 20 3d 20 72 20 26 20 *(pBuf++) = r &
a77c0 30 78 66 66 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 0xff;. }.}../*.
a77d0 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 ** Allocate nByt
a77e0 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 e bytes of memor
a77f0 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 y..*/.static voi
a7800 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c d *sqlite3MemMal
a7810 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a loc(int nByte){.
a7820 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 struct MemBloc
a7830 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f kHdr *pHdr;. vo
a7840 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 72 id **pBt;. char
a7850 20 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 *z;. int *pInt
a7860 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b ;. void *p = 0;
a7870 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65 . int totalSize
a7880 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65 ;. int nReserve
a7890 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
a78a0 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 x_enter(mem.mute
a78b0 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 x);. assert( me
a78c0 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b m.disallow==0 );
a78d0 0a 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f . nReserve = RO
a78e0 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 74 UND8(nByte);. t
a78f0 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73 65 otalSize = nRese
a7900 72 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 rve + sizeof(*pH
a7910 64 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 dr) + sizeof(int
a7920 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ) +.
a7930 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 mem.nBacktrac
a7940 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 e*sizeof(void*)
a7950 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20 + mem.nTitle;.
a7960 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 61 6c p = malloc(total
a7970 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 20 29 Size);. if( p )
a7980 7b 0a 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20 20 {. z = p;.
a7990 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 26 pBt = (void**)&
a79a0 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a 20 z[mem.nTitle];.
a79b0 20 20 20 70 48 64 72 20 3d 20 28 73 74 72 75 63 pHdr = (struc
a79c0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 26 t MemBlockHdr*)&
a79d0 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 pBt[mem.nBacktra
a79e0 63 65 5d 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 ce];. pHdr->p
a79f0 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 48 Next = 0;. pH
a7a00 64 72 2d 3e 70 50 72 65 76 20 3d 20 6d 65 6d 2e dr->pPrev = mem.
a7a10 70 4c 61 73 74 3b 0a 20 20 20 20 69 66 28 20 6d pLast;. if( m
a7a20 65 6d 2e 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 em.pLast ){.
a7a30 20 20 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 4e 65 mem.pLast->pNe
a7a40 78 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d xt = pHdr;. }
a7a50 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 2e else{. mem.
a7a60 70 46 69 72 73 74 20 3d 20 70 48 64 72 3b 0a 20 pFirst = pHdr;.
a7a70 20 20 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 }. mem.pLa
a7a80 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 70 st = pHdr;. p
a7a90 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 20 Hdr->iForeGuard
a7aa0 3d 20 46 4f 52 45 47 55 41 52 44 3b 0a 20 20 20 = FOREGUARD;.
a7ab0 20 70 48 64 72 2d 3e 65 54 79 70 65 20 3d 20 4d pHdr->eType = M
a7ac0 45 4d 54 59 50 45 5f 48 45 41 50 3b 0a 20 20 20 EMTYPE_HEAP;.
a7ad0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
a7ae0 65 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42 61 eSlots = mem.nBa
a7af0 63 6b 74 72 61 63 65 3b 0a 20 20 20 20 70 48 64 cktrace;. pHd
a7b00 72 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 6d 2e r->nTitle = mem.
a7b10 6e 54 69 74 6c 65 3b 0a 20 20 20 20 69 66 28 20 nTitle;. if(
a7b20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 29 mem.nBacktrace )
a7b30 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 61 41 {. void *aA
a7b40 64 64 72 5b 34 30 5d 3b 0a 20 20 20 20 20 20 70 ddr[40];. p
a7b50 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 Hdr->nBacktrace
a7b60 3d 20 62 61 63 6b 74 72 61 63 65 28 61 41 64 64 = backtrace(aAdd
a7b70 72 2c 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 r, mem.nBacktrac
a7b80 65 2b 31 29 2d 31 3b 0a 20 20 20 20 20 20 6d 65 e+1)-1;. me
a7b90 6d 63 70 79 28 70 42 74 2c 20 26 61 41 64 64 72 mcpy(pBt, &aAddr
a7ba0 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b [1], pHdr->nBack
a7bb0 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 trace*sizeof(voi
a7bc0 64 2a 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 d*));. asse
a7bd0 72 74 28 70 42 74 5b 30 5d 29 3b 0a 20 20 20 20 rt(pBt[0]);.
a7be0 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b 74 if( mem.xBackt
a7bf0 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 race ){.
a7c00 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 6e mem.xBacktrace(n
a7c10 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 Byte, pHdr->nBac
a7c20 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64 72 ktrace-1, &aAddr
a7c30 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 [1]);. }.
a7c40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
a7c50 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 Hdr->nBacktrace
a7c60 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 = 0;. }. i
a7c70 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 7b f( mem.nTitle ){
a7c80 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c . memcpy(z,
a7c90 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 6d mem.zTitle, mem
a7ca0 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d 0a .nTitle);. }.
a7cb0 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 pHdr->iSize
a7cc0 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 6a = nByte;. adj
a7cd0 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c 20 ustStats(nByte,
a7ce0 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d 20 +1);. pInt =
a7cf0 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b 0a (int*)&pHdr[1];.
a7d00 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 pInt[nReserv
a7d10 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 3d e/sizeof(int)] =
a7d20 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20 20 REARGUARD;.
a7d30 72 61 6e 64 6f 6d 46 69 6c 6c 28 28 63 68 61 72 randomFill((char
a7d40 2a 29 70 49 6e 74 2c 20 6e 42 79 74 65 29 3b 0a *)pInt, nByte);.
a7d50 20 20 20 20 6d 65 6d 73 65 74 28 28 28 63 68 61 memset(((cha
a7d60 72 2a 29 70 49 6e 74 29 2b 6e 42 79 74 65 2c 20 r*)pInt)+nByte,
a7d70 30 78 36 35 2c 20 6e 52 65 73 65 72 76 65 2d 6e 0x65, nReserve-n
a7d80 42 79 74 65 29 3b 0a 20 20 20 20 70 20 3d 20 28 Byte);. p = (
a7d90 76 6f 69 64 2a 29 70 49 6e 74 3b 0a 20 20 7d 0a void*)pInt;. }.
a7da0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
a7db0 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 leave(mem.mutex)
a7dc0 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 0a 7d ;. return p; .}
a7dd0 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d ../*.** Free mem
a7de0 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ory..*/.static v
a7df0 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 oid sqlite3MemFr
a7e00 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 ee(void *pPrior)
a7e10 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c {. struct MemBl
a7e20 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 ockHdr *pHdr;.
a7e30 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 void **pBt;. ch
a7e40 61 72 20 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28 ar *z;. assert(
a7e50 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
a7e60 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 7c 7c nfig.bMemstat ||
a7e70 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
a7e80 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 3d nfig.bCoreMutex=
a7e90 3d 30 20 0a 20 20 20 20 20 20 20 7c 7c 20 6d 65 =0 . || me
a7ea0 6d 2e 6d 75 74 65 78 21 3d 30 20 29 3b 0a 20 20 m.mutex!=0 );.
a7eb0 70 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 pHdr = sqlite3Me
a7ec0 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50 msysGetHeader(pP
a7ed0 72 69 6f 72 29 3b 0a 20 20 70 42 74 20 3d 20 28 rior);. pBt = (
a7ee0 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 70 void**)pHdr;. p
a7ef0 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63 Bt -= pHdr->nBac
a7f00 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 73 ktraceSlots;. s
a7f10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
a7f20 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 er(mem.mutex);.
a7f30 20 69 66 28 20 70 48 64 72 2d 3e 70 50 72 65 76 if( pHdr->pPrev
a7f40 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
a7f50 70 48 64 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 pHdr->pPrev->pNe
a7f60 78 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 xt==pHdr );.
a7f70 70 48 64 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 pHdr->pPrev->pNe
a7f80 78 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78 74 xt = pHdr->pNext
a7f90 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
a7fa0 73 73 65 72 74 28 20 6d 65 6d 2e 70 46 69 72 73 ssert( mem.pFirs
a7fb0 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 6d t==pHdr );. m
a7fc0 65 6d 2e 70 46 69 72 73 74 20 3d 20 70 48 64 72 em.pFirst = pHdr
a7fd0 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 ->pNext;. }. i
a7fe0 66 28 20 70 48 64 72 2d 3e 70 4e 65 78 74 20 29 f( pHdr->pNext )
a7ff0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48 {. assert( pH
a8000 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 dr->pNext->pPrev
a8010 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 70 48 ==pHdr );. pH
a8020 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 dr->pNext->pPrev
a8030 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a = pHdr->pPrev;.
a8040 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
a8050 65 72 74 28 20 6d 65 6d 2e 70 4c 61 73 74 3d 3d ert( mem.pLast==
a8060 70 48 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e pHdr );. mem.
a8070 70 4c 61 73 74 20 3d 20 70 48 64 72 2d 3e 70 50 pLast = pHdr->pP
a8080 72 65 76 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 rev;. }. z = (
a8090 63 68 61 72 2a 29 70 42 74 3b 0a 20 20 7a 20 2d char*)pBt;. z -
a80a0 3d 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a = pHdr->nTitle;.
a80b0 20 20 61 64 6a 75 73 74 53 74 61 74 73 28 70 48 adjustStats(pH
a80c0 64 72 2d 3e 69 53 69 7a 65 2c 20 2d 31 29 3b 0a dr->iSize, -1);.
a80d0 20 20 72 61 6e 64 6f 6d 46 69 6c 6c 28 7a 2c 20 randomFill(z,
a80e0 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 2a 70 48 sizeof(void*)*pH
a80f0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c dr->nBacktraceSl
a8100 6f 74 73 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 ots + sizeof(*pH
a8110 64 72 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 dr) +.
a8120 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a pHdr->iSiz
a8130 65 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 e + sizeof(int)
a8140 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b + pHdr->nTitle);
a8150 0a 20 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 . free(z);. sq
a8160 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
a8170 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a e(mem.mutex); .
a8180 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
a8190 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 the size of an e
a81a0 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 xisting memory a
a81b0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a llocation..**.**
a81c0 20 46 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 For this debugg
a81d0 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ing implementati
a81e0 6f 6e 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 on, we *always*
a81f0 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 make a copy of t
a8200 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e he.** allocation
a8210 20 69 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 into a new plac
a8220 65 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e e in memory. In
a8230 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 this way, if th
a8240 65 20 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 e .** higher lev
a8250 65 6c 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 el code is using
a8260 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
a8270 6f 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 old allocation,
a8280 69 74 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d it is .** much m
a8290 6f 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 ore likely to br
a82a0 65 61 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d eak and we are m
a82b0 75 63 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 uch more liking
a82c0 74 6f 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 to find.** the e
a82d0 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rror..*/.static
a82e0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d void *sqlite3Mem
a82f0 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 Realloc(void *pP
a8300 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 rior, int nByte)
a8310 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c {. struct MemBl
a8320 6f 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b ockHdr *pOldHdr;
a8330 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 . void *pNew;.
a8340 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 assert( mem.dis
a8350 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 allow==0 );. as
a8360 73 65 72 74 28 20 28 6e 42 79 74 65 20 26 20 37 sert( (nByte & 7
a8370 29 3d 3d 30 20 29 3b 20 20 20 20 20 2f 2a 20 45 )==0 ); /* E
a8380 56 3a 20 52 2d 34 36 31 39 39 2d 33 30 32 34 39 V: R-46199-30249
a8390 20 2a 2f 0a 20 20 70 4f 6c 64 48 64 72 20 3d 20 */. pOldHdr =
a83a0 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 sqlite3MemsysGet
a83b0 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a Header(pPrior);.
a83c0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
a83d0 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 MemMalloc(nByte)
a83e0 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a ;. if( pNew ){.
a83f0 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c memcpy(pNew,
a8400 20 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 3c 70 pPrior, nByte<p
a8410 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 3f 20 OldHdr->iSize ?
a8420 6e 42 79 74 65 20 3a 20 70 4f 6c 64 48 64 72 2d nByte : pOldHdr-
a8430 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 >iSize);. if(
a8440 20 6e 42 79 74 65 3e 70 4f 6c 64 48 64 72 2d 3e nByte>pOldHdr->
a8450 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 iSize ){. r
a8460 61 6e 64 6f 6d 46 69 6c 6c 28 26 28 28 63 68 61 andomFill(&((cha
a8470 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 48 64 72 r*)pNew)[pOldHdr
a8480 2d 3e 69 53 69 7a 65 5d 2c 20 6e 42 79 74 65 20 ->iSize], nByte
a8490 2d 20 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 - pOldHdr->iSize
a84a0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
a84b0 69 74 65 33 4d 65 6d 46 72 65 65 28 70 50 72 69 ite3MemFree(pPri
a84c0 6f 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 or);. }. retur
a84d0 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n pNew;.}../*.**
a84e0 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f Populate the lo
a84f0 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
a8500 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 llocation functi
a8510 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a on pointers in.*
a8520 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 * sqlite3GlobalC
a8530 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 onfig.m with poi
a8540 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 nters to the rou
a8550 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 tines in this fi
a8560 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
a8570 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
a8580 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 e3MemSetDefault(
a8590 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
a85a0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 const sqlite3_me
a85b0 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c m_methods defaul
a85c0 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 tMethods = {.
a85d0 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c sqlite3MemMall
a85e0 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 oc,. sqlite3
a85f0 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 MemFree,. sq
a8600 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c lite3MemRealloc,
a8610 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
a8620 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 Size,. sqlit
a8630 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 e3MemRoundup,.
a8640 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 sqlite3MemIni
a8650 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d t,. sqlite3M
a8660 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 emShutdown,.
a8670 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 0. };. sqlite
a8680 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
a8690 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 CONFIG_MALLOC, &
a86a0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b defaultMethods);
a86b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
a86c0 65 20 22 74 79 70 65 22 20 6f 66 20 61 6e 20 61 e "type" of an a
a86d0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 llocation..*/.SQ
a86e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
a86f0 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 d sqlite3Memdebu
a8700 67 53 65 74 54 79 70 65 28 76 6f 69 64 20 2a 70 gSetType(void *p
a8710 2c 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20 69 , u8 eType){. i
a8720 66 28 20 70 20 26 26 20 73 71 6c 69 74 65 33 47 f( p && sqlite3G
a8730 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d lobalConfig.m.xM
a8740 61 6c 6c 6f 63 3d 3d 73 71 6c 69 74 65 33 4d 65 alloc==sqlite3Me
a8750 6d 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 73 mMalloc ){. s
a8760 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 truct MemBlockHd
a8770 72 20 2a 70 48 64 72 3b 0a 20 20 20 20 70 48 64 r *pHdr;. pHd
a8780 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 r = sqlite3Memsy
a8790 73 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a 20 sGetHeader(p);.
a87a0 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d assert( pHdr-
a87b0 3e 69 46 6f 72 65 47 75 61 72 64 3d 3d 46 4f 52 >iForeGuard==FOR
a87c0 45 47 55 41 52 44 20 29 3b 0a 20 20 20 20 70 48 EGUARD );. pH
a87d0 64 72 2d 3e 65 54 79 70 65 20 3d 20 65 54 79 70 dr->eType = eTyp
a87e0 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 e;. }.}../*.**
a87f0 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 Return TRUE if t
a8800 68 65 20 6d 61 73 6b 20 6f 66 20 74 79 70 65 20 he mask of type
a8810 69 6e 20 65 54 79 70 65 20 6d 61 74 63 68 65 73 in eType matches
a8820 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 the type of the
a8830 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 .** allocation p
a8840 2e 20 20 41 6c 73 6f 20 72 65 74 75 72 6e 20 74 . Also return t
a8850 72 75 65 20 69 66 20 70 3d 3d 4e 55 4c 4c 2e 0a rue if p==NULL..
a8860 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
a8870 6e 65 20 69 73 20 64 65 73 69 67 6e 65 64 20 66 ne is designed f
a8880 6f 72 20 75 73 65 20 77 69 74 68 69 6e 20 61 6e or use within an
a8890 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
a88a0 65 6e 74 2c 20 74 6f 0a 2a 2a 20 76 65 72 69 66 ent, to.** verif
a88b0 79 20 74 68 65 20 74 79 70 65 20 6f 66 20 61 6e y the type of an
a88c0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 46 6f allocation. Fo
a88d0 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a r example:.**.**
a88e0 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
a88f0 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54 ite3MemdebugHasT
a8900 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 44 ype(p, MEMTYPE_D
a8910 42 29 20 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f B) );.*/.SQLITE_
a8920 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
a8930 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54 79 te3MemdebugHasTy
a8940 70 65 28 76 6f 69 64 20 2a 70 2c 20 75 38 20 65 pe(void *p, u8 e
a8950 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 Type){. int rc
a8960 3d 20 31 3b 0a 20 20 69 66 28 20 70 20 26 26 20 = 1;. if( p &&
a8970 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
a8980 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 73 fig.m.xMalloc==s
a8990 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 20 qlite3MemMalloc
a89a0 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 4d 65 ){. struct Me
a89b0 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b mBlockHdr *pHdr;
a89c0 0a 20 20 20 20 70 48 64 72 20 3d 20 73 71 6c 69 . pHdr = sqli
a89d0 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 te3MemsysGetHead
a89e0 65 72 28 70 29 3b 0a 20 20 20 20 61 73 73 65 72 er(p);. asser
a89f0 74 28 20 70 48 64 72 2d 3e 69 46 6f 72 65 47 75 t( pHdr->iForeGu
a8a00 61 72 64 3d 3d 46 4f 52 45 47 55 41 52 44 20 29 ard==FOREGUARD )
a8a10 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c ; /* All
a8a20 6f 63 61 74 69 6f 6e 20 69 73 20 76 61 6c 69 64 ocation is valid
a8a30 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 70 48 64 */. if( (pHd
a8a40 72 2d 3e 65 54 79 70 65 26 65 54 79 70 65 29 3d r->eType&eType)=
a8a50 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d =0 ){. rc =
a8a60 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0;. }. }.
a8a70 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
a8a80 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 .** Return TRUE
a8a90 69 66 20 74 68 65 20 6d 61 73 6b 20 6f 66 20 74 if the mask of t
a8aa0 79 70 65 20 69 6e 20 65 54 79 70 65 20 6d 61 74 ype in eType mat
a8ab0 63 68 65 73 20 6e 6f 20 62 69 74 73 20 6f 66 20 ches no bits of
a8ac0 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 0a the type of the.
a8ad0 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 2e ** allocation p.
a8ae0 20 20 41 6c 73 6f 20 72 65 74 75 72 6e 20 74 72 Also return tr
a8af0 75 65 20 69 66 20 70 3d 3d 4e 55 4c 4c 2e 0a 2a ue if p==NULL..*
a8b00 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
a8b10 65 20 69 73 20 64 65 73 69 67 6e 65 64 20 66 6f e is designed fo
a8b20 72 20 75 73 65 20 77 69 74 68 69 6e 20 61 6e 20 r use within an
a8b30 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
a8b40 6e 74 2c 20 74 6f 0a 2a 2a 20 76 65 72 69 66 79 nt, to.** verify
a8b50 20 74 68 65 20 74 79 70 65 20 6f 66 20 61 6e 20 the type of an
a8b60 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 46 6f 72 allocation. For
a8b70 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 example:.**.**
a8b80 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
a8b90 74 65 33 4d 65 6d 64 65 62 75 67 4e 6f 54 79 70 te3MemdebugNoTyp
a8ba0 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 44 42 29 e(p, MEMTYPE_DB)
a8bb0 20 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 );.*/.SQLITE_PR
a8bc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
a8bd0 33 4d 65 6d 64 65 62 75 67 4e 6f 54 79 70 65 28 3MemdebugNoType(
a8be0 76 6f 69 64 20 2a 70 2c 20 75 38 20 65 54 79 70 void *p, u8 eTyp
a8bf0 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 e){. int rc = 1
a8c00 3b 0a 20 20 69 66 28 20 70 20 26 26 20 73 71 6c ;. if( p && sql
a8c10 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
a8c20 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 73 71 6c 69 .m.xMalloc==sqli
a8c30 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 20 29 7b 0a te3MemMalloc ){.
a8c40 20 20 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c struct MemBl
a8c50 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 ockHdr *pHdr;.
a8c60 20 20 70 48 64 72 20 3d 20 73 71 6c 69 74 65 33 pHdr = sqlite3
a8c70 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 MemsysGetHeader(
a8c80 70 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 p);. assert(
a8c90 70 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 pHdr->iForeGuard
a8ca0 3d 3d 46 4f 52 45 47 55 41 52 44 20 29 3b 20 20 ==FOREGUARD );
a8cb0 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 /* Alloca
a8cc0 74 69 6f 6e 20 69 73 20 76 61 6c 69 64 20 2a 2f tion is valid */
a8cd0 0a 20 20 20 20 69 66 28 20 28 70 48 64 72 2d 3e . if( (pHdr->
a8ce0 65 54 79 70 65 26 65 54 79 70 65 29 21 3d 30 20 eType&eType)!=0
a8cf0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 30 3b ){. rc = 0;
a8d00 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
a8d10 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
a8d20 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 Set the number
a8d30 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c 65 76 of backtrace lev
a8d40 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 61 63 els kept for eac
a8d50 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a h allocation..**
a8d60 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f A value of zero
a8d70 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63 6b 74 turns off backt
a8d80 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e 75 6d racing. The num
a8d90 62 65 72 20 69 73 20 61 6c 77 61 79 73 20 72 6f ber is always ro
a8da0 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f 20 61 unded.** up to a
a8db0 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 2e 0a multiple of 2..
a8dc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
a8dd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 E void sqlite3Me
a8de0 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 28 mdebugBacktrace(
a8df0 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 69 66 int depth){. if
a8e00 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64 65 70 ( depth<0 ){ dep
a8e10 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 28 20 th = 0; }. if(
a8e20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65 70 74 depth>20 ){ dept
a8e30 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 70 74 h = 20; }. dept
a8e40 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26 30 78 h = (depth+1)&0x
a8e50 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 fe;. mem.nBackt
a8e60 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a 7d 0a race = depth;.}.
a8e70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a8e80 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 void sqlite3Memd
a8e90 65 62 75 67 42 61 63 6b 74 72 61 63 65 43 61 6c ebugBacktraceCal
a8ea0 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 42 61 lback(void (*xBa
a8eb0 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 6e cktrace)(int, in
a8ec0 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a 20 20 t, void **)){.
a8ed0 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 3d mem.xBacktrace =
a8ee0 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d 0a 0a xBacktrace;.}..
a8ef0 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69 /*.** Set the ti
a8f00 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 20 73 tle string for s
a8f10 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61 ubsequent alloca
a8f20 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 tions..*/.SQLITE
a8f30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
a8f40 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 lite3MemdebugSet
a8f50 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 title(const char
a8f60 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75 6e 73 *zTitle){. uns
a8f70 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20 73 71 igned int n = sq
a8f80 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 lite3Strlen30(zT
a8f90 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 71 6c itle) + 1;. sql
a8fa0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
a8fb0 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 (mem.mutex);. i
a8fc0 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d f( n>=sizeof(mem
a8fd0 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d 20 73 .zTitle) ) n = s
a8fe0 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 izeof(mem.zTitle
a8ff0 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 6d 65 )-1;. memcpy(me
a9000 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 6c 65 m.zTitle, zTitle
a9010 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74 , n);. mem.zTit
a9020 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d le[n] = 0;. mem
a9030 2e 6e 54 69 74 6c 65 20 3d 20 52 4f 55 4e 44 38 .nTitle = ROUND8
a9040 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d (n);. sqlite3_m
a9050 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d utex_leave(mem.m
a9060 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 utex);.}..SQLITE
a9070 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
a9080 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 79 6e lite3MemdebugSyn
a9090 63 28 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 c(){. struct Me
a90a0 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b mBlockHdr *pHdr;
a90b0 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d 2e . for(pHdr=mem.
a90c0 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70 48 pFirst; pHdr; pH
a90d0 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 7b dr=pHdr->pNext){
a90e0 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 74 20 . void **pBt
a90f0 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a = (void**)pHdr;.
a9100 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d pBt -= pHdr-
a9110 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 >nBacktraceSlots
a9120 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b 74 ;. mem.xBackt
a9130 72 61 63 65 28 70 48 64 72 2d 3e 69 53 69 7a 65 race(pHdr->iSize
a9140 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 , pHdr->nBacktra
a9150 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 3b 0a ce-1, &pBt[1]);.
a9160 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 }.}../*.** Ope
a9170 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 n the file indic
a9180 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 ated and write a
a9190 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 log of all unfr
a91a0 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 eed memory .** a
a91b0 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 llocations into
a91c0 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c that log..*/.SQL
a91d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a91e0 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 sqlite3Memdebug
a91f0 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 Dump(const char
a9200 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 *zFilename){. F
a9210 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 72 75 ILE *out;. stru
a9220 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
a9230 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 pHdr;. void **p
a9240 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6f Bt;. int i;. o
a9250 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 ut = fopen(zFile
a9260 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 69 66 name, "w");. if
a9270 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ( out==0 ){.
a9280 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
a9290 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 "** Unable to ou
a92a0 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 tput memory debu
a92b0 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 g output log: %s
a92c0 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 **\n",.
a92d0 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c zFil
a92e0 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 ename);. retu
a92f0 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 48 rn;. }. for(pH
a9300 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 70 dr=mem.pFirst; p
a9310 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d 3e Hdr; pHdr=pHdr->
a9320 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 61 72 pNext){. char
a9330 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 48 64 *z = (char*)pHd
a9340 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 64 72 r;. z -= pHdr
a9350 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 ->nBacktraceSlot
a9360 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 s*sizeof(void*)
a9370 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a + pHdr->nTitle;.
a9380 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
a9390 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 74 65 "**** %lld byte
a93a0 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25 73 20 s at %p from %s
a93b0 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 20 20 ****\n", .
a93c0 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a pHdr->iSiz
a93d0 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 48 64 e, &pHdr[1], pHd
a93e0 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a 20 r->nTitle ? z :
a93f0 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 28 20 "???");. if(
a9400 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
a9410 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 73 68 ){. fflush
a9420 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 42 74 (out);. pBt
a9430 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b = (void**)pHdr;
a9440 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20 70 48 . pBt -= pH
a9450 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c dr->nBacktraceSl
a9460 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63 6b 74 ots;. backt
a9470 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 race_symbols_fd(
a9480 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b pBt, pHdr->nBack
a9490 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 6f 75 trace, fileno(ou
a94a0 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72 69 6e t));. fprin
a94b0 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 tf(out, "\n");.
a94c0 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 69 6e }. }. fprin
a94d0 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 53 3a tf(out, "COUNTS:
a94e0 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b \n");. for(i=0;
a94f0 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 2b 2b i<NCSIZE-1; i++
a9500 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e ){. if( mem.n
a9510 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 20 20 Alloc[i] ){.
a9520 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
a9530 20 20 20 25 35 64 3a 20 25 31 30 64 20 25 31 30 %5d: %10d %10
a9540 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 20 20 d %10d\n", .
a9550 20 20 20 20 20 20 20 20 69 2a 38 2c 20 6d 65 6d i*8, mem
a9560 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 6d 2e .nAlloc[i], mem.
a9570 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d 65 6d nCurrent[i], mem
a9580 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 3b 0a .mxCurrent[i]);.
a9590 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
a95a0 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a mem.nAlloc[NCSIZ
a95b0 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 72 69 E-1] ){. fpri
a95c0 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 64 ntf(out, " %5d
a95d0 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30 64 : %10d %10d %10d
a95e0 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
a95f0 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d 65 NCSIZE*8-8, me
a9600 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d m.nAlloc[NCSIZE-
a9610 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 1],.
a9620 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e 43 mem.nCurrent[NC
a9630 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 43 SIZE-1], mem.mxC
a9640 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d urrent[NCSIZE-1]
a9650 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 65 28 );. }. fclose(
a9660 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 out);.}../*.** R
a9670 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
a9680 20 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 74 65 of times sqlite
a9690 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 61 73 3MemMalloc() has
a96a0 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f been called..*/
a96b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a96c0 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 int sqlite3Memde
a96d0 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 28 29 bugMallocCount()
a96e0 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 {. int i;. int
a96f0 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 66 nTotal = 0;. f
a9700 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 or(i=0; i<NCSIZE
a9710 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 6f 74 ; i++){. nTot
a9720 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 al += mem.nAlloc
a9730 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 [i];. }. retur
a9740 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 n nTotal;.}...#e
a9750 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d ndif /* SQLITE_M
a9760 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a EMDEBUG */../***
a9770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
a9780 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a of mem2.c ******
a9790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a97a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a97b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
a97c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
a97d0 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 2a 2a n file mem3.c **
a97e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a97f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
a9810 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 31 * 2007 October 1
a9820 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 4.**.** The auth
a9830 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
a9840 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
a9850 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
a9860 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
a9870 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
a9880 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
a9890 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
a98a0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
a98b0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
a98c0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
a98d0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
a98e0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
a98f0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
a9900 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
a9910 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
a9920 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
a9930 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
a9940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a9980 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
a9990 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e ntains the C fun
a99a0 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c ctions that impl
a99b0 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a ement a memory.*
a99c0 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 * allocation sub
a99d0 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62 system for use b
a99e0 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a y SQLite. .**.**
a99f0 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 This version of
a9a00 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
a9a10 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
a9a20 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 omits all.** us
a9a30 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 e of malloc(). T
a9a40 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 20 73 he SQLite user s
a9a50 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b 20 upplies a block
a9a60 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 of memory.** bef
a9a70 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 ore calling sqli
a9a80 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
a9a90 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f from which allo
a9aa0 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d cations.** are m
a9ab0 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64 ade and returned
a9ac0 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 by the xMalloc(
a9ad0 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 ) and xRealloc()
a9ae0 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 .** implementat
a9af0 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 ions. Once sqlit
a9b00 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
a9b10 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c has been called,
a9b20 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f .** the amount o
a9b30 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 f memory availab
a9b40 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20 le to SQLite is
a9b50 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 fixed and cannot
a9b60 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a .** be changed..
a9b70 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 **.** This versi
a9b80 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
a9b90 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
a9ba0 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 ystem is include
a9bb0 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c d.** in the buil
a9bc0 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 d only if SQLITE
a9bd0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 _ENABLE_MEMSYS3
a9be0 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 0a is defined..*/..
a9bf0 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 /*.** This versi
a9c00 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
a9c10 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f 6e allocator is on
a9c20 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 ly built into th
a9c30 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 4c e library.** SQL
a9c40 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
a9c50 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 20 44 S3 is defined. D
a9c60 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 79 6d efining this sym
a9c70 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 bol does not.**
a9c80 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69 mean that the li
a9c90 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 20 61 brary will use a
a9ca0 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 20 memory-pool by
a9cb0 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 68 default, just th
a9cc0 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 69 at.** it is avai
a9cd0 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 6f lable. The mempo
a9ce0 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ol allocator is
a9cf0 61 63 74 69 76 61 74 65 64 20 62 79 20 63 61 6c activated by cal
a9d00 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f ling.** sqlite3_
a9d10 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 66 config()..*/.#if
a9d20 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
a9d30 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a E_MEMSYS3../*.**
a9d40 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 69 Maximum size (i
a9d50 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f 66 n Mem3Blocks) of
a9d60 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e 6b a "small" chunk
a9d70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f ..*/.#define MX_
a9d80 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a SMALL 10.../*.**
a9d90 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c Number of freel
a9da0 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0a 2a ist hash slots.*
a9db0 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 48 /.#define N_HASH
a9dc0 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65 61../*.** A me
a9dd0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
a9de0 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 22 (also called a "
a9df0 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 73 chunk") consists
a9e00 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d of two or .** m
a9e10 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 65 ore blocks where
a9e20 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 38 each block is 8
a9e30 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 69 72 bytes. The fir
a9e40 73 74 20 38 20 62 79 74 65 73 20 61 72 65 20 0a st 8 bytes are .
a9e50 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 61 74 ** a header that
a9e60 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64 is not returned
a9e70 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a 2a to the user..**
a9e80 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 74 .** A chunk is t
a9e90 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 6b wo or more block
a9ea0 73 20 74 68 61 74 20 69 73 20 65 69 74 68 65 72 s that is either
a9eb0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 0a checked out or.
a9ec0 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 66 69 ** free. The fi
a9ed0 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 6f rst block has fo
a9ee0 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 2e 68 rmat u.hdr. u.h
a9ef0 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 20 74 dr.size4x is 4 t
a9f00 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 imes the.** size
a9f10 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 of the allocati
a9f20 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 on in blocks if
a9f30 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 the allocation i
a9f40 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 75 s free..** The u
a9f50 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 62 69 .hdr.size4x&1 bi
a9f60 74 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65 t is true if the
a9f70 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 chunk is checke
a9f80 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 6c d out and.** fal
a9f90 73 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 se if the chunk
a9fa0 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 is on the freeli
a9fb0 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73 st. The u.hdr.s
a9fc0 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 69 ize4x&2 bit.** i
a9fd0 73 20 74 72 75 65 20 69 66 20 74 68 65 20 70 72 s true if the pr
a9fe0 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 evious chunk is
a9ff0 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 checked out and
aa000 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a 20 false if the.**
aa010 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 previous chunk i
aa020 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68 s free. The u.h
aa030 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c dr.prevSize fiel
aa040 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 d is the size of
aa050 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 .** the previous
aa060 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73 chunk in blocks
aa070 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 if the previous
aa080 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65 chunk is on the
aa090 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 66 .** freelist. If
aa0a0 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 the previous ch
aa0b0 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f unk is checked o
aa0c0 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 64 ut, then.** u.hd
aa0d0 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e 20 62 r.prevSize can b
aa0e0 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 e part of the da
aa0f0 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 75 6e ta for that chun
aa100 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 k and should.**
aa110 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 not be read or w
aa120 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65 ritten..**.** We
aa130 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 79 20 often identify
aa140 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 20 69 a chunk by its i
aa150 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f ndex in mem3.aPo
aa160 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 ol[]. When.** t
aa170 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 65 his is done, the
aa180 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 66 chunk index ref
aa190 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e ers to the secon
aa1a0 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68 d block of.** th
aa1b0 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 69 e chunk. In thi
aa1c0 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 73 74 s way, the first
aa1d0 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 6e chunk has an in
aa1e0 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 63 dex of 1..** A c
aa1f0 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 20 hunk index of 0
aa200 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 63 means "no such c
aa210 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 65 hunk" and is the
aa220 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f equivalent.** o
aa230 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 f a NULL pointer
aa240 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f ..**.** The seco
aa250 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 65 nd block of free
aa260 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 68 chunks is of th
aa270 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 20 e form u.list.
aa280 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64 The.** two field
aa290 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d s form a double-
aa2a0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 63 linked list of c
aa2b0 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 64 hunks of related
aa2c0 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74 sizes..** Point
aa2d0 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 ers to the head
aa2e0 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 65 20 of the list are
aa2f0 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61 stored in mem3.a
aa300 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72 iSmall[] .** for
aa310 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20 smaller chunks
aa320 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b and mem3.aiHash[
aa330 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75 ] for larger chu
aa340 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 nks..**.** The s
aa350 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 61 econd block of a
aa360 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20 64 chunk is user d
aa370 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e 6b ata if the chunk
aa380 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a 20 is checked .**
aa390 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 6e 6b out. If a chunk
aa3a0 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c is checked out,
aa3b0 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 6d the user data m
aa3c0 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a 2a ay extend into.*
aa3d0 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 65 76 * the u.hdr.prev
aa3e0 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 68 Size value of th
aa3f0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 6e e following chun
aa400 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 k..*/.typedef st
aa410 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d ruct Mem3Block M
aa420 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 74 em3Block;.struct
aa430 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 75 Mem3Block {. u
aa440 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63 nion {. struc
aa450 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 72 t {. u32 pr
aa460 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 7a evSize; /* Siz
aa470 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 63 68 e of previous ch
aa480 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b unk in Mem3Block
aa490 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 elements */.
aa4a0 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b 20 20 u32 size4x;
aa4b0 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 69 7a /* 4x the siz
aa4c0 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 75 e of current chu
aa4d0 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 nk in Mem3Block
aa4e0 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 elements */.
aa4f0 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 75 63 } hdr;. struc
aa500 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6e 65 t {. u32 ne
aa510 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 xt; /* Ind
aa520 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c ex in mem3.aPool
aa530 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 [] of next free
aa540 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 75 chunk */. u
aa550 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 32 prev; /
aa560 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e * Index in mem3.
aa570 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69 aPool[] of previ
aa580 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a ous free chunk *
aa590 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20 /. } list;.
aa5a0 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 } u;.};../*.** A
aa5b0 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 ll of the static
aa5c0 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 variables used
aa5d0 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 by this module a
aa5e0 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 re collected.**
aa5f0 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 into a single st
aa600 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d ructure named "m
aa610 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 20 74 em3". This is t
aa620 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 o keep the.** st
aa630 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f atic variables o
aa640 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 rganized and to
aa650 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 reduce namespace
aa660 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 pollution.** wh
aa670 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 en this module i
aa680 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 s combined with
aa690 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 other in the ama
aa6a0 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 lgamation..*/.st
aa6b0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
aa6c0 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61 struct Mem3Globa
aa6d0 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 l {. /*. ** Me
aa6e0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 mory available f
aa6f0 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e or allocation. n
aa700 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 7a 65 Pool is the size
aa710 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a 20 20 of the array.
aa720 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b ** (in Mem3Block
aa730 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 s) pointed to by
aa740 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a 20 aPool less 2..
aa750 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b */. u32 nPool;
aa760 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50 . Mem3Block *aP
aa770 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 ool;.. /*. **
aa780 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20 65 True if we are e
aa790 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 74 valuating an out
aa7a0 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c 62 -of-memory callb
aa7b0 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 ack.. */. int
aa7c0 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20 alarmBusy;. .
aa7d0 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f /*. ** Mutex to
aa7e0 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 control access
aa7f0 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c to the memory al
aa800 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
aa810 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 em.. */. sqlit
aa820 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b e3_mutex *mutex;
aa830 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 . . /*. ** Th
aa840 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 e minimum amount
aa850 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 20 74 of free space t
aa860 68 61 74 20 77 65 20 68 61 76 65 20 73 65 65 6e hat we have seen
aa870 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d .. */. u32 mnM
aa880 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a aster;.. /*. *
aa890 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68 65 * iMaster is the
aa8a0 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 index of the ma
aa8b0 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f 73 ster chunk. Mos
aa8c0 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e t new allocation
aa8d0 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 66 s. ** occur off
aa8e0 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 2e 20 of this chunk.
aa8f0 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65 szMaster is the
aa900 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c size (in Mem3Bl
aa910 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 68 ocks). ** of th
aa920 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 65 72 e current master
aa930 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 30 20 . iMaster is 0
aa940 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 if there is not
aa950 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 20 master chunk..
aa960 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 63 68 ** The master ch
aa970 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 69 unk is not in ei
aa980 74 68 65 72 20 74 68 65 20 61 69 48 61 73 68 5b ther the aiHash[
aa990 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a ] or aiSmall[]..
aa9a0 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 74 */. u32 iMast
aa9b0 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 74 er;. u32 szMast
aa9c0 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 er;.. /*. ** A
aa9d0 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 6f 66 rray of lists of
aa9e0 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 63 free blocks acc
aa9f0 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 62 6c ording to the bl
aaa00 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 66 ock size . ** f
aaa10 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b or smaller chunk
aaa20 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f 6e 20 s, or a hash on
aaa30 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 66 the block size f
aaa40 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 63 or larger. ** c
aaa50 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 33 hunks.. */. u3
aaa60 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 2 aiSmall[MX_SMA
aaa70 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20 LL-1]; /* For
aaa80 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20 sizes 2 through
aaa90 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73 MX_SMALL, inclus
aaaa0 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 48 ive */. u32 aiH
aaab0 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20 ash[N_HASH];
aaac0 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 /* For sizes
aaad0 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20 MX_SMALL+1 and
aaae0 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33 larger */.} mem3
aaaf0 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b = { 97535575 };
aab00 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 47 ..#define mem3 G
aab10 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d LOBAL(struct Mem
aab20 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a 3Global, mem3)..
aab30 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 /*.** Unlink the
aab40 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 chunk at mem3.a
aab50 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 Pool[i] from lis
aab60 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c t it is currentl
aab70 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74 y.** on. *pRoot
aab80 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 68 61 is the list tha
aab90 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 72 20 t i is a member
aaba0 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f of..*/.static vo
aabb0 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b id memsys3Unlink
aabc0 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c 20 FromList(u32 i,
aabd0 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 u32 *pRoot){. u
aabe0 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 32 next = mem3.a
aabf0 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e Pool[i].u.list.n
aac00 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 76 20 ext;. u32 prev
aac10 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e = mem3.aPool[i].
aac20 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 61 u.list.prev;. a
aac30 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
aac40 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
aac50 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
aac60 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 rev==0 ){. *p
aac70 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d Root = next;. }
aac80 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 else{. mem3.a
aac90 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 Pool[prev].u.lis
aaca0 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 t.next = next;.
aacb0 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 29 7b }. if( next ){
aacc0 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b . mem3.aPool[
aacd0 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 next].u.list.pre
aace0 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 20 v = prev;. }.
aacf0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e mem3.aPool[i].u.
aad00 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a 20 list.next = 0;.
aad10 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 mem3.aPool[i].u
aad20 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a .list.prev = 0;.
aad30 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 }../*.** Unlink
aad40 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 the chunk at ind
aad50 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68 ex i from .** wh
aad60 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20 63 atever list is c
aad70 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65 urrently a membe
aad80 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 r of..*/.static
aad90 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 void memsys3Unli
aada0 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 nk(u32 i){. u32
aadb0 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 size, hash;. a
aadc0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
aadd0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
aade0 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
aadf0 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t( (mem3.aPool[i
aae00 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
aae10 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73 & 1)==0 );. as
aae20 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 sert( i>=1 );.
aae30 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f size = mem3.aPoo
aae40 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
aae50 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 e4x/4;. assert(
aae60 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f size==mem3.aPoo
aae70 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 l[i+size-1].u.hd
aae80 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 r.prevSize );.
aae90 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 assert( size>=2
aaea0 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d );. if( size <=
aaeb0 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 MX_SMALL ){.
aaec0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 memsys3UnlinkFr
aaed0 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e omList(i, &mem3.
aaee0 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 aiSmall[size-2])
aaef0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 ;. }else{. h
aaf00 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 ash = size % N_H
aaf10 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 ASH;. memsys3
aaf20 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 UnlinkFromList(i
aaf30 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 , &mem3.aiHash[h
aaf40 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ash]);. }.}../*
aaf50 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 .** Link the chu
aaf60 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c nk at mem3.aPool
aaf70 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f [i] so that is o
aaf80 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 65 n the list roote
aaf90 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0a d.** at *pRoot..
aafa0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
aafb0 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 emsys3LinkIntoLi
aafc0 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70 st(u32 i, u32 *p
aafd0 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 28 Root){. assert(
aafe0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
aaff0 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
ab000 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b );. mem3.aPool[
ab010 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d i].u.list.next =
ab020 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e *pRoot;. mem3.
ab030 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e aPool[i].u.list.
ab040 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 20 prev = 0;. if(
ab050 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d 65 *pRoot ){. me
ab060 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d m3.aPool[*pRoot]
ab070 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 69 .u.list.prev = i
ab080 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d ;. }. *pRoot =
ab090 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e i;.}../*.** Lin
ab0a0 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 k the chunk at i
ab0b0 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 74 68 ndex i into eith
ab0c0 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 er the appropria
ab0d0 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 6e te.** small chun
ab0e0 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f 20 k list, or into
ab0f0 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 the large chunk
ab100 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 hash table..*/.s
ab110 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
ab120 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 s3Link(u32 i){.
ab130 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b u32 size, hash;
ab140 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
ab150 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
ab160 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 m3.mutex) );. a
ab170 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 ssert( i>=1 );.
ab180 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 assert( (mem3.a
ab190 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
ab1a0 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29 size4x & 1)==0 )
ab1b0 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e ;. size = mem3.
ab1c0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
ab1d0 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 .size4x/4;. ass
ab1e0 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e ert( size==mem3.
ab1f0 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e aPool[i+size-1].
ab200 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 u.hdr.prevSize )
ab210 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 ;. assert( size
ab220 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a >=2 );. if( siz
ab230 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b e <= MX_SMALL ){
ab240 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b . memsys3Link
ab250 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d IntoList(i, &mem
ab260 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 3.aiSmall[size-2
ab270 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ]);. }else{.
ab280 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e hash = size % N
ab290 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 _HASH;. memsy
ab2a0 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 s3LinkIntoList(i
ab2b0 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 , &mem3.aiHash[h
ab2c0 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ash]);. }.}../*
ab2d0 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 49 .** If the STATI
ab2e0 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e C_MEM mutex is n
ab2f0 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c ot already held,
ab300 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 obtain it now.
ab310 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c The mutex.** wil
ab320 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c l already be hel
ab330 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63 d (obtained by c
ab340 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 ode in malloc.c)
ab350 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c if.** sqlite3Gl
ab360 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 obalConfig.bMemS
ab370 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a tat is true..*/.
ab380 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
ab390 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a ys3Enter(void){.
ab3a0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
ab3b0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 balConfig.bMemst
ab3c0 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d 75 at==0 && mem3.mu
ab3d0 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 tex==0 ){. me
ab3e0 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 m3.mutex = sqlit
ab3f0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
ab400 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
ab410 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c _MEM);. }. sql
ab420 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
ab430 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a (mem3.mutex);.}.
ab440 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
ab450 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a ys3Leave(void){.
ab460 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
ab470 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 leave(mem3.mutex
ab480 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c );.}../*.** Call
ab490 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20 75 ed when we are u
ab4a0 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79 nable to satisfy
ab4b0 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f an allocation o
ab4c0 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 f nBytes..*/.sta
ab4d0 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 tic void memsys3
ab4e0 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20 OutOfMemory(int
ab4f0 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 21 6d nByte){. if( !m
ab500 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 7b em3.alarmBusy ){
ab510 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 . mem3.alarmB
ab520 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 usy = 1;. ass
ab530 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
ab540 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
ab550 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ex) );. sqlit
ab560 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
ab570 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em3.mutex);.
ab580 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
ab590 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a 20 memory(nByte);.
ab5a0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
ab5b0 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65 _enter(mem3.mute
ab5c0 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 x);. mem3.ala
ab5d0 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d 0a rmBusy = 0;. }.
ab5e0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20 }.../*.** Chunk
ab5f0 69 20 69 73 20 61 20 66 72 65 65 20 63 68 75 6e i is a free chun
ab600 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 k that has been
ab610 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73 unlinked. Adjus
ab620 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 70 t its .** size p
ab630 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68 arameters for ch
ab640 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75 eck-out and retu
ab650 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
ab660 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f 72 the .** user por
ab670 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e tion of the chun
ab680 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 k..*/.static voi
ab690 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f d *memsys3Checko
ab6a0 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e 42 ut(u32 i, u32 nB
ab6b0 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b 0a lock){. u32 x;.
ab6c0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
ab6d0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 3_mutex_held(mem
ab6e0 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 3.mutex) );. as
ab6f0 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 sert( i>=1 );.
ab700 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f assert( mem3.aPo
ab710 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
ab720 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 ze4x/4==nBlock )
ab730 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 ;. assert( mem3
ab740 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d .aPool[i+nBlock-
ab750 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
ab760 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 78 e==nBlock );. x
ab770 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d = mem3.aPool[i-
ab780 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 1].u.hdr.size4x;
ab790 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d . mem3.aPool[i-
ab7a0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
ab7b0 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c = nBlock*4 | 1 |
ab7c0 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e 61 (x&2);. mem3.a
ab7d0 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d Pool[i+nBlock-1]
ab7e0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 .u.hdr.prevSize
ab7f0 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33 = nBlock;. mem3
ab800 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d .aPool[i+nBlock-
ab810 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
ab820 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26 |= 2;. return &
ab830 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d mem3.aPool[i];.}
ab840 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20 ../*.** Carve a
ab850 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65 piece off of the
ab860 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33 end of the mem3
ab870 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 63 68 .iMaster free ch
ab880 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 unk..** Return a
ab890 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
ab8a0 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 new allocation.
ab8b0 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74 Or, if the mast
ab8c0 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e er chunk.** is n
ab8d0 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c ot large enough,
ab8e0 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 return 0..*/.st
ab8f0 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 atic void *memsy
ab900 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 33 32 s3FromMaster(u32
ab910 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 65 nBlock){. asse
ab920 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
ab930 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 x_held(mem3.mute
ab940 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
ab950 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e mem3.szMaster>=n
ab960 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 6e Block );. if( n
ab970 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61 Block>=mem3.szMa
ab980 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f 2a ster-1 ){. /*
ab990 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 Use the entire
ab9a0 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 6f master */. vo
ab9b0 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 43 id *p = memsys3C
ab9c0 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d 61 heckout(mem3.iMa
ab9d0 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73 ster, mem3.szMas
ab9e0 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 69 ter);. mem3.i
ab9f0 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
aba00 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 mem3.szMaster =
aba10 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 0;. mem3.mnMa
aba20 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 65 ster = 0;. re
aba30 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 7b turn p;. }else{
aba40 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68 . /* Split th
aba50 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20 e master block.
aba60 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c Return the tail
aba70 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 77 . */. u32 new
aba80 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 3d i, x;. newi =
aba90 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20 mem3.iMaster +
abaa0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20 mem3.szMaster -
abab0 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 nBlock;. asse
abac0 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 2e rt( newi > mem3.
abad0 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 20 iMaster+1 );.
abae0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
abaf0 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
abb00 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
abb10 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 prevSize = nBloc
abb20 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f k;. mem3.aPoo
abb30 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d l[mem3.iMaster+m
abb40 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e em3.szMaster-1].
abb50 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 u.hdr.size4x |=
abb60 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 2;. mem3.aPoo
abb70 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e l[newi-1].u.hdr.
abb80 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a size4x = nBlock*
abb90 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 2e 4 + 1;. mem3.
abba0 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c 6f szMaster -= nBlo
abbb0 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f ck;. mem3.aPo
abbc0 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 ol[newi-1].u.hdr
abbd0 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 .prevSize = mem3
abbe0 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 78 .szMaster;. x
abbf0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 = mem3.aPool[me
abc00 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e m3.iMaster-1].u.
abc10 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a hdr.size4x & 2;.
abc20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d mem3.aPool[m
abc30 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
abc40 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 .hdr.size4x = me
abc50 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 m3.szMaster*4 |
abc60 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e x;. if( mem3.
abc70 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 2e szMaster < mem3.
abc80 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 mnMaster ){.
abc90 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 mem3.mnMaster
abca0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b = mem3.szMaster;
abcb0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
abcc0 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e 61 n (void*)&mem3.a
abcd0 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a Pool[newi];. }.
abce0 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20 }../*.** *pRoot
abcf0 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20 61 is the head of a
abd00 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63 68 list of free ch
abd10 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d 65 unks of the same
abd20 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65 size.** or same
abd30 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 6e 20 size hash. In
abd40 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 52 other words, *pR
abd50 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 79 20 oot is an entry
abd60 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d in either.** mem
abd70 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 3.aiSmall[] or m
abd80 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 0a em3.aiHash[]. .
abd90 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
abda0 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c 20 ne examines all
abdb0 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 20 67 entries on the g
abdc0 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 72 iven list and tr
abdd0 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 73 ies.** to coales
abde0 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 73 20 ce each entries
abdf0 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 66 72 with adjacent fr
abe00 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a ee chunks. .**.
abe10 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 61 20 ** If it sees a
abe20 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 6c 61 chunk that is la
abe30 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e 69 rger than mem3.i
abe40 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 6c 61 Master, it repla
abe50 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 72 ces .** the curr
abe60 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ent mem3.iMaster
abe70 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 6c 61 with the new la
abe80 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e 20 rger chunk. In
abe90 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 69 order for.** thi
abea0 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 72 s mem3.iMaster r
abeb0 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 6f eplacement to wo
abec0 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 20 63 rk, the master c
abed0 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a 20 hunk must be.**
abee0 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 linked into the
abef0 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 54 68 hash tables. Th
abf00 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 6e 6f at is not the no
abf10 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a 2a rmal state of.**
abf20 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 6f 75 affairs, of cou
abf30 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e rse. The callin
abf40 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6c g routine must l
abf50 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 0a 2a ink the master.*
abf60 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 69 * chunk before i
abf70 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 nvoking this rou
abf80 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 20 tine, then must
abf90 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 73 unlink the (poss
abfa0 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 29 ibly.** changed)
abfb0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e master chunk on
abfc0 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ce this routine
abfd0 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2f has finished..*/
abfe0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
abff0 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70 sys3Merge(u32 *p
ac000 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e 65 Root){. u32 iNe
ac010 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c 20 xt, prev, size,
ac020 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 28 i, x;.. assert(
ac030 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
ac040 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
ac050 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f 6f );. for(i=*pRoo
ac060 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 29 t; i>0; i=iNext)
ac070 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65 {. iNext = me
ac080 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 m3.aPool[i].u.li
ac090 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 7a st.next;. siz
ac0a0 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 e = mem3.aPool[i
ac0b0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
ac0c0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 73 ;. assert( (s
ac0d0 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 20 ize&1)==0 );.
ac0e0 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d 3d 30 if( (size&2)==0
ac0f0 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 ){. memsys
ac100 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 3UnlinkFromList(
ac110 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 i, pRoot);.
ac120 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d 65 6d assert( i > mem
ac130 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
ac140 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 dr.prevSize );.
ac150 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 2d 20 prev = i -
ac160 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e mem3.aPool[i-1].
ac170 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a u.hdr.prevSize;.
ac180 20 20 20 20 20 20 69 66 28 20 70 72 65 76 3d 3d if( prev==
ac190 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 iNext ){.
ac1a0 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 iNext = mem3.aP
ac1b0 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 ool[prev].u.list
ac1c0 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 .next;. }.
ac1d0 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
ac1e0 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20 nk(prev);.
ac1f0 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f size = i + size/
ac200 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 20 4 - prev;.
ac210 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 x = mem3.aPool[p
ac220 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a rev-1].u.hdr.siz
ac230 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d e4x & 2;. m
ac240 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 em3.aPool[prev-1
ac250 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d ].u.hdr.size4x =
ac260 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20 size*4 | x;.
ac270 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 mem3.aPool[pr
ac280 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 ev+size-1].u.hdr
ac290 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 .prevSize = size
ac2a0 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c ;. memsys3L
ac2b0 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 ink(prev);.
ac2c0 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d i = prev;. }
ac2d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65 else{. size
ac2e0 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 /= 4;. }.
ac2f0 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e 73 if( size>mem3.s
ac300 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 zMaster ){.
ac310 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 mem3.iMaster =
ac320 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a i;. mem3.sz
ac330 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a 20 Master = size;.
ac340 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
ac350 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b * Return a block
ac360 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 of memory of at
ac370 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69 6e least nBytes in
ac380 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e size..** Return
ac390 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e NULL if unable.
ac3a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
ac3b0 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 tion assumes tha
ac3c0 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 t the necessary
ac3d0 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c mutexes, if any,
ac3e0 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 are.** already
ac3f0 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c held by the call
ac400 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 er. Hence "Unsaf
ac410 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f e"..*/.static vo
ac420 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f id *memsys3Mallo
ac430 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 cUnsafe(int nByt
ac440 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 75 e){. u32 i;. u
ac450 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33 32 32 nBlock;. u32
ac460 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 65 toFree;.. asse
ac470 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
ac480 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 x_held(mem3.mute
ac490 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
ac4a0 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b sizeof(Mem3Block
ac4b0 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e 42 )==8 );. if( nB
ac4c0 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 6e yte<=12 ){. n
ac4d0 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c Block = 2;. }el
ac4e0 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d se{. nBlock =
ac4f0 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 3b (nByte + 11)/8;
ac500 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e . }. assert( n
ac510 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f Block>=2 );.. /
ac520 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 4c * STEP 1:. ** L
ac530 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 ook for an entry
ac540 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 of the correct
ac550 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 20 74 size in either t
ac560 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68 he small. ** ch
ac570 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e 20 unk table or in
ac580 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 the large chunk
ac590 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 hash table. Thi
ac5a0 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 73 s is. ** succes
ac5b0 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 65 sful most of the
ac5c0 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 20 74 time (about 9 t
ac5d0 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 29 2e imes out of 10).
ac5e0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c 6f . */. if( nBlo
ac5f0 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 ck <= MX_SMALL )
ac600 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e 61 {. i = mem3.a
ac610 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d iSmall[nBlock-2]
ac620 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b ;. if( i>0 ){
ac630 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e . memsys3Un
ac640 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 linkFromList(i,
ac650 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 &mem3.aiSmall[nB
ac660 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 20 lock-2]);.
ac670 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 return memsys3Ch
ac680 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b eckout(i, nBlock
ac690 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 );. }. }else
ac6a0 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20 3d {. int hash =
ac6b0 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 48 nBlock % N_HASH
ac6c0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d 33 ;. for(i=mem3
ac6d0 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 69 .aiHash[hash]; i
ac6e0 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c >0; i=mem3.aPool
ac6f0 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 [i].u.list.next)
ac700 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 {. if( mem3
ac710 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 .aPool[i-1].u.hd
ac720 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f r.size4x/4==nBlo
ac730 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 ck ){. me
ac740 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c msys3UnlinkFromL
ac750 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48 ist(i, &mem3.aiH
ac760 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20 ash[hash]);.
ac770 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 return memsy
ac780 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 s3Checkout(i, nB
ac790 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 lock);. }.
ac7a0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 }. }.. /* S
ac7b0 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20 TEP 2:. ** Try
ac7c0 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 61 to satisfy the a
ac7d0 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72 llocation by car
ac7e0 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66 ving a piece off
ac7f0 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a of the end. **
ac800 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 of the master c
ac810 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70 hunk. This step
ac820 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69 usually works i
ac830 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a f step 1 fails..
ac840 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e */. if( mem3.
ac850 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b szMaster>=nBlock
ac860 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d ){. return m
ac870 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 emsys3FromMaster
ac880 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a (nBlock);. }...
ac890 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a 20 /* STEP 3: .
ac8a0 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 ** Loop through
ac8b0 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f the entire memo
ac8c0 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73 ry pool. Coales
ac8d0 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 ce adjacent free
ac8e0 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52 . ** chunks. R
ac8f0 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 73 ecompute the mas
ac900 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 68 65 ter chunk as the
ac910 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 63 68 largest free ch
ac920 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74 unk.. ** Then t
ac930 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 74 69 ry again to sati
ac940 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 sfy the allocati
ac950 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 20 on by carving a
ac960 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f piece off. ** o
ac970 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 f the end of the
ac980 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 master chunk.
ac990 54 68 69 73 20 73 74 65 70 20 68 61 70 70 65 6e This step happen
ac9a0 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 s very. ** rare
ac9b0 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a 20 20 ly (we hope!).
ac9c0 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 3d */. for(toFree=
ac9d0 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65 nBlock*16; toFre
ac9e0 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36 e<(mem3.nPool*16
ac9f0 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 7b ); toFree *= 2){
aca00 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 4f . memsys3OutO
aca10 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 3b fMemory(toFree);
aca20 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d . if( mem3.iM
aca30 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d aster ){. m
aca40 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e emsys3Link(mem3.
aca50 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 iMaster);.
aca60 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30 mem3.iMaster = 0
aca70 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d ;. mem3.szM
aca80 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d aster = 0;. }
aca90 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
acaa0 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 N_HASH; i++){.
acab0 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 memsys3Merge
acac0 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d (&mem3.aiHash[i]
acad0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 );. }. for
acae0 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c (i=0; i<MX_SMALL
acaf0 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 -1; i++){.
acb00 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 memsys3Merge(&me
acb10 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a m3.aiSmall[i]);.
acb20 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 }. if( me
acb30 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 m3.szMaster ){.
acb40 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
acb50 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 nk(mem3.iMaster)
acb60 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 ;. if( mem3
acb70 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 .szMaster>=nBloc
acb80 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 k ){. ret
acb90 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d urn memsys3FromM
acba0 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 aster(nBlock);.
acbb0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
acbc0 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f .. /* If none o
acbd0 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b f the above work
acbe0 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c ed, then we fail
acbf0 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b . */. return 0;
acc00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 .}../*.** Free a
acc10 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 n outstanding me
acc20 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e mory allocation.
acc30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
acc40 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 tion assumes tha
acc50 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 t the necessary
acc60 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c mutexes, if any,
acc70 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 are.** already
acc80 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c held by the call
acc90 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 er. Hence "Unsaf
acca0 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f e"..*/.static vo
accb0 69 64 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e id memsys3FreeUn
accc0 73 61 66 65 28 76 6f 69 64 20 2a 70 4f 6c 64 29 safe(void *pOld)
accd0 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 70 {. Mem3Block *p
acce0 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70 = (Mem3Block*)p
accf0 4f 6c 64 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 Old;. int i;.
acd00 75 33 32 20 73 69 7a 65 2c 20 78 3b 0a 20 20 61 u32 size, x;. a
acd10 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
acd20 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
acd30 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
acd40 74 28 20 70 3e 6d 65 6d 33 2e 61 50 6f 6f 6c 20 t( p>mem3.aPool
acd50 26 26 20 70 3c 26 6d 65 6d 33 2e 61 50 6f 6f 6c && p<&mem3.aPool
acd60 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 20 29 3b 0a [mem3.nPool] );.
acd70 20 20 69 20 3d 20 70 20 2d 20 6d 65 6d 33 2e 61 i = p - mem3.a
acd80 50 6f 6f 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 Pool;. assert(
acd90 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d (mem3.aPool[i-1]
acda0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 29 .u.hdr.size4x&1)
acdb0 3d 3d 31 20 29 3b 0a 20 20 73 69 7a 65 20 3d 20 ==1 );. size =
acdc0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e mem3.aPool[i-1].
acdd0 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a u.hdr.size4x/4;.
acde0 20 20 61 73 73 65 72 74 28 20 69 2b 73 69 7a 65 assert( i+size
acdf0 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 2b 31 20 29 <=mem3.nPool+1 )
ace00 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ;. mem3.aPool[i
ace10 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
ace20 20 26 3d 20 7e 31 3b 0a 20 20 6d 65 6d 33 2e 61 &= ~1;. mem3.a
ace30 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 Pool[i+size-1].u
ace40 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 .hdr.prevSize =
ace50 73 69 7a 65 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f size;. mem3.aPo
ace60 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 ol[i+size-1].u.h
ace70 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 32 3b dr.size4x &= ~2;
ace80 0a 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 69 . memsys3Link(i
ace90 29 3b 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 );.. /* Try to
acea0 65 78 70 61 6e 64 20 74 68 65 20 6d 61 73 74 65 expand the maste
aceb0 72 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 6c r using the newl
acec0 79 20 66 72 65 65 64 20 63 68 75 6e 6b 20 2a 2f y freed chunk */
aced0 0a 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73 . if( mem3.iMas
acee0 74 65 72 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 ter ){. while
acef0 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 ( (mem3.aPool[me
acf00 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e m3.iMaster-1].u.
acf10 68 64 72 2e 73 69 7a 65 34 78 26 32 29 3d 3d 30 hdr.size4x&2)==0
acf20 20 29 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d ){. size =
acf30 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
acf40 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 .iMaster-1].u.hd
acf50 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 r.prevSize;.
acf60 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2d mem3.iMaster -
acf70 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 = size;. me
acf80 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2b 3d 20 73 m3.szMaster += s
acf90 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 ize;. memsy
acfa0 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d s3Unlink(mem3.iM
acfb0 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 78 20 aster);. x
acfc0 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d = mem3.aPool[mem
acfd0 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 3.iMaster-1].u.h
acfe0 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 dr.size4x & 2;.
acff0 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b mem3.aPool[
ad000 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e mem3.iMaster-1].
ad010 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d u.hdr.size4x = m
ad020 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c em3.szMaster*4 |
ad030 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 x;. mem3.a
ad040 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
ad050 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d r+mem3.szMaster-
ad060 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
ad070 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 e = mem3.szMaste
ad080 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 78 20 3d r;. }. x =
ad090 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
ad0a0 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 .iMaster-1].u.hd
ad0b0 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 20 r.size4x & 2;.
ad0c0 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 33 2e 61 while( (mem3.a
ad0d0 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
ad0e0 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d r+mem3.szMaster-
ad0f0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 1].u.hdr.size4x&
ad100 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 1)==0 ){. m
ad110 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d emsys3Unlink(mem
ad120 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 3.iMaster+mem3.s
ad130 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 zMaster);.
ad140 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2b 3d mem3.szMaster +=
ad150 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
ad160 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
ad170 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
ad180 73 69 7a 65 34 78 2f 34 3b 0a 20 20 20 20 20 20 size4x/4;.
ad190 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e mem3.aPool[mem3.
ad1a0 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 iMaster-1].u.hdr
ad1b0 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 .size4x = mem3.s
ad1c0 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 zMaster*4 | x;.
ad1d0 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b mem3.aPool[
ad1e0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d mem3.iMaster+mem
ad1f0 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 3.szMaster-1].u.
ad200 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d hdr.prevSize = m
ad210 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 em3.szMaster;.
ad220 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
ad230 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 Return the size
ad240 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 of an outstandi
ad250 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 ng allocation, i
ad260 6e 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a n bytes. The.**
ad270 20 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f size returned o
ad280 6d 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 20 mits the 8-byte
ad290 68 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e header overhead.
ad2a0 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 This only.** w
ad2b0 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20 orks for chunks
ad2c0 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 that are current
ad2d0 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a ly checked out..
ad2e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 */.static int me
ad2f0 6d 73 79 73 33 53 69 7a 65 28 76 6f 69 64 20 2a msys3Size(void *
ad300 70 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 p){. Mem3Block
ad310 2a 70 42 6c 6f 63 6b 3b 0a 20 20 69 66 28 20 70 *pBlock;. if( p
ad320 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
ad330 20 20 70 42 6c 6f 63 6b 20 3d 20 28 4d 65 6d 33 pBlock = (Mem3
ad340 42 6c 6f 63 6b 2a 29 70 3b 0a 20 20 61 73 73 65 Block*)p;. asse
ad350 72 74 28 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e rt( (pBlock[-1].
ad360 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 29 21 u.hdr.size4x&1)!
ad370 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 =0 );. return (
ad380 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 pBlock[-1].u.hdr
ad390 2e 73 69 7a 65 34 78 26 7e 33 29 2a 32 20 2d 20 .size4x&~3)*2 -
ad3a0 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 4;.}../*.** Roun
ad3b0 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73 d up a request s
ad3c0 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ize to the next
ad3d0 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e valid allocation
ad3e0 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 size..*/.static
ad3f0 20 69 6e 74 20 6d 65 6d 73 79 73 33 52 6f 75 6e int memsys3Roun
ad400 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 69 66 dup(int n){. if
ad410 28 20 6e 3c 3d 31 32 20 29 7b 0a 20 20 20 20 72 ( n<=12 ){. r
ad420 65 74 75 72 6e 20 31 32 3b 0a 20 20 7d 65 6c 73 eturn 12;. }els
ad430 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 28 e{. return ((
ad440 6e 2b 31 31 29 26 7e 37 29 20 2d 20 34 3b 0a 20 n+11)&~7) - 4;.
ad450 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f }.}../*.** Allo
ad460 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20 6d cate nBytes of m
ad470 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 emory..*/.static
ad480 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 void *memsys3Ma
ad490 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 29 lloc(int nBytes)
ad4a0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 {. sqlite3_int6
ad4b0 34 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4 *p;. assert(
ad4c0 6e 42 79 74 65 73 3e 30 20 29 3b 20 20 20 20 20 nBytes>0 );
ad4d0 20 20 20 20 20 2f 2a 20 6d 61 6c 6c 6f 63 2e 63 /* malloc.c
ad4e0 20 66 69 6c 74 65 72 73 20 6f 75 74 20 30 20 62 filters out 0 b
ad4f0 79 74 65 20 72 65 71 75 65 73 74 73 20 2a 2f 0a yte requests */.
ad500 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 memsys3Enter()
ad510 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d ;. p = memsys3M
ad520 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 allocUnsafe(nByt
ad530 65 73 29 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 65 es);. memsys3Le
ad540 61 76 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 ave();. return
ad550 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a (void*)p; .}../*
ad560 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e .** Free memory.
ad570 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
ad580 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f 69 64 memsys3Free(void
ad590 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73 *pPrior){. ass
ad5a0 65 72 74 28 20 70 50 72 69 6f 72 20 29 3b 0a 20 ert( pPrior );.
ad5b0 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b memsys3Enter();
ad5c0 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e . memsys3FreeUn
ad5d0 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 safe(pPrior);.
ad5e0 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a memsys3Leave();.
ad5f0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
ad600 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 the size of an e
ad610 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 xisting memory a
ad620 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 llocation.*/.sta
ad630 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 tic void *memsys
ad640 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 3Realloc(void *p
ad650 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 Prior, int nByte
ad660 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a s){. int nOld;.
ad670 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 void *p;. if(
ad680 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 pPrior==0 ){.
ad690 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
ad6a0 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b _malloc(nBytes);
ad6b0 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 . }. if( nByte
ad6c0 73 3c 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 s<=0 ){. sqli
ad6d0 74 65 33 5f 66 72 65 65 28 70 50 72 69 6f 72 29 te3_free(pPrior)
ad6e0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
ad6f0 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d }. nOld = mem
ad700 73 79 73 33 53 69 7a 65 28 70 50 72 69 6f 72 29 sys3Size(pPrior)
ad710 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d ;. if( nBytes<=
ad720 6e 4f 6c 64 20 26 26 20 6e 42 79 74 65 73 3e 3d nOld && nBytes>=
ad730 6e 4f 6c 64 2d 31 32 38 20 29 7b 0a 20 20 20 20 nOld-128 ){.
ad740 72 65 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 return pPrior;.
ad750 20 7d 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 }. memsys3Ente
ad760 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 r();. p = memsy
ad770 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e s3MallocUnsafe(n
ad780 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70 20 Bytes);. if( p
ad790 29 7b 0a 20 20 20 20 69 66 28 20 6e 4f 6c 64 3c ){. if( nOld<
ad7a0 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20 20 20 nBytes ){.
ad7b0 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 memcpy(p, pPrior
ad7c0 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d 65 6c , nOld);. }el
ad7d0 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 se{. memcpy
ad7e0 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 42 79 74 (p, pPrior, nByt
ad7f0 65 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d es);. }. m
ad800 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66 65 emsys3FreeUnsafe
ad810 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 (pPrior);. }.
ad820 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a memsys3Leave();.
ad830 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
ad840 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
ad850 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a this module..*/.
ad860 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 static int memsy
ad870 73 33 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 s3Init(void *Not
ad880 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
ad890 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
ad8a0 64 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 d);. if( !sqlit
ad8b0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
ad8c0 48 65 61 70 20 29 7b 0a 20 20 20 20 72 65 74 75 Heap ){. retu
ad8d0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
ad8e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 6f 72 65 . }.. /* Store
ad8f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
ad900 65 20 6d 65 6d 6f 72 79 20 62 6c 6f 63 6b 20 69 e memory block i
ad910 6e 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 n global structu
ad920 72 65 20 6d 65 6d 33 2e 20 2a 2f 0a 20 20 61 73 re mem3. */. as
ad930 73 65 72 74 28 20 73 69 7a 65 6f 66 28 4d 65 6d sert( sizeof(Mem
ad940 33 42 6c 6f 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 3Block)==8 );.
ad950 6d 65 6d 33 2e 61 50 6f 6f 6c 20 3d 20 28 4d 65 mem3.aPool = (Me
ad960 6d 33 42 6c 6f 63 6b 20 2a 29 73 71 6c 69 74 65 m3Block *)sqlite
ad970 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 3GlobalConfig.pH
ad980 65 61 70 3b 0a 20 20 6d 65 6d 33 2e 6e 50 6f 6f eap;. mem3.nPoo
ad990 6c 20 3d 20 28 73 71 6c 69 74 65 33 47 6c 6f 62 l = (sqlite3Glob
ad9a0 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 20 2f alConfig.nHeap /
ad9b0 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 sizeof(Mem3Bloc
ad9c0 6b 29 29 20 2d 20 32 3b 0a 0a 20 20 2f 2a 20 49 k)) - 2;.. /* I
ad9d0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 61 nitialize the ma
ad9e0 73 74 65 72 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 ster block. */.
ad9f0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d mem3.szMaster =
ada00 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d mem3.nPool;. m
ada10 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d em3.mnMaster = m
ada20 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 em3.szMaster;.
ada30 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 31 mem3.iMaster = 1
ada40 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 30 ;. mem3.aPool[0
ada50 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d ].u.hdr.size4x =
ada60 20 28 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3c (mem3.szMaster<
ada70 3c 32 29 20 2b 20 32 3b 0a 20 20 6d 65 6d 33 2e <2) + 2;. mem3.
ada80 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c aPool[mem3.nPool
ada90 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 ].u.hdr.prevSize
adaa0 20 3d 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 = mem3.nPool;.
adab0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
adac0 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 73 69 .nPool].u.hdr.si
adad0 7a 65 34 78 20 3d 20 31 3b 0a 0a 20 20 72 65 74 ze4x = 1;.. ret
adae0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
adaf0 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 ../*.** Deinitia
adb00 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 lize this module
adb10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
adb20 20 6d 65 6d 73 79 73 33 53 68 75 74 64 6f 77 6e memsys3Shutdown
adb30 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b (void *NotUsed){
adb40 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
adb50 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
adb60 6d 65 6d 33 2e 6d 75 74 65 78 20 3d 20 30 3b 0a mem3.mutex = 0;.
adb70 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 0a 0a 2f return;.}..../
adb80 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 *.** Open the fi
adb90 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e 64 le indicated and
adba0 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 write a log of
adbb0 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f all unfreed memo
adbc0 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f ry .** allocatio
adbd0 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 ns into that log
adbe0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
adbf0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
adc00 4d 65 6d 73 79 73 33 44 75 6d 70 28 63 6f 6e 73 Memsys3Dump(cons
adc10 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
adc20 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 e){.#ifdef SQLIT
adc30 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a E_DEBUG. FILE *
adc40 6f 75 74 3b 0a 20 20 75 33 32 20 69 2c 20 6a 3b out;. u32 i, j;
adc50 0a 20 20 75 33 32 20 73 69 7a 65 3b 0a 20 20 69 . u32 size;. i
adc60 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 f( zFilename==0
adc70 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d || zFilename[0]=
adc80 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74 20 3d 20 =0 ){. out =
adc90 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 7b stdout;. }else{
adca0 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e . out = fopen
adcb0 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29 (zFilename, "w")
adcc0 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 3d 3d 30 ;. if( out==0
adcd0 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 ){. fprint
adce0 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e f(stderr, "** Un
adcf0 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 20 6d able to output m
add00 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 74 70 emory debug outp
add10 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 ut log: %s **\n"
add20 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
add30 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d zFilenam
add40 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e e);. return
add50 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 ;. }. }. me
add60 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 msys3Enter();.
add70 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 43 48 fprintf(out, "CH
add80 55 4e 4b 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 UNKS:\n");. for
add90 28 69 3d 31 3b 20 69 3c 3d 6d 65 6d 33 2e 6e 50 (i=1; i<=mem3.nP
adda0 6f 6f 6c 3b 20 69 2b 3d 73 69 7a 65 2f 34 29 7b ool; i+=size/4){
addb0 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 . size = mem3
addc0 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 .aPool[i-1].u.hd
addd0 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 20 69 66 r.size4x;. if
adde0 28 20 73 69 7a 65 2f 34 3c 3d 31 20 29 7b 0a 20 ( size/4<=1 ){.
addf0 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
ade00 2c 20 22 25 70 20 73 69 7a 65 20 65 72 72 6f 72 , "%p size error
ade10 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c \n", &mem3.aPool
ade20 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 [i]);. asse
ade30 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 rt( 0 );. b
ade40 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
ade50 69 66 28 20 28 73 69 7a 65 26 31 29 3d 3d 30 20 if( (size&1)==0
ade60 26 26 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b && mem3.aPool[i+
ade70 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e size/4-1].u.hdr.
ade80 70 72 65 76 53 69 7a 65 21 3d 73 69 7a 65 2f 34 prevSize!=size/4
ade90 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 ){. fprint
adea0 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69 6c 20 f(out, "%p tail
adeb0 73 69 7a 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 size does not ma
adec0 74 63 68 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 tch\n", &mem3.aP
aded0 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 ool[i]);. a
adee0 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 ssert( 0 );.
adef0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
adf00 20 20 20 69 66 28 20 28 28 6d 65 6d 33 2e 61 50 if( ((mem3.aP
adf10 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e ool[i+size/4-1].
adf20 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 29 3e u.hdr.size4x&2)>
adf30 3e 31 29 21 3d 28 73 69 7a 65 26 31 29 20 29 7b >1)!=(size&1) ){
adf40 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f . fprintf(o
adf50 75 74 2c 20 22 25 70 20 74 61 69 6c 20 63 68 65 ut, "%p tail che
adf60 63 6b 6f 75 74 20 62 69 74 20 69 73 20 69 6e 63 ckout bit is inc
adf70 6f 72 72 65 63 74 5c 6e 22 2c 20 26 6d 65 6d 33 orrect\n", &mem3
adf80 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 .aPool[i]);.
adf90 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 assert( 0 );.
adfa0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
adfb0 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 26 31 }. if( size&1
adfc0 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 ){. fprint
adfd0 66 28 6f 75 74 2c 20 22 25 70 20 25 36 64 20 62 f(out, "%p %6d b
adfe0 79 74 65 73 20 63 68 65 63 6b 65 64 20 6f 75 74 ytes checked out
adff0 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c \n", &mem3.aPool
ae000 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d [i], (size/4)*8-
ae010 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 8);. }else{.
ae020 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
ae030 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73 20 , "%p %6d bytes
ae040 66 72 65 65 25 73 5c 6e 22 2c 20 26 6d 65 6d 33 free%s\n", &mem3
ae050 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 .aPool[i], (size
ae060 2f 34 29 2a 38 2d 38 2c 0a 20 20 20 20 20 20 20 /4)*8-8,.
ae070 20 20 20 20 20 20 20 20 20 20 20 69 3d 3d 6d 65 i==me
ae080 6d 33 2e 69 4d 61 73 74 65 72 20 3f 20 22 20 2a m3.iMaster ? " *
ae090 2a 6d 61 73 74 65 72 2a 2a 22 20 3a 20 22 22 29 *master**" : "")
ae0a0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f ;. }. }. fo
ae0b0 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c r(i=0; i<MX_SMAL
ae0c0 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 L-1; i++){. i
ae0d0 66 28 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b f( mem3.aiSmall[
ae0e0 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 i]==0 ) continue
ae0f0 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 ;. fprintf(ou
ae100 74 2c 20 22 73 6d 61 6c 6c 28 25 32 64 29 3a 22 t, "small(%2d):"
ae110 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20 , i);. for(j
ae120 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 = mem3.aiSmall[i
ae130 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 ]; j>0; j=mem3.a
ae140 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e Pool[j].u.list.n
ae150 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70 72 69 ext){. fpri
ae160 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28 25 64 ntf(out, " %p(%d
ae170 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b )", &mem3.aPool[
ae180 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 j],.
ae190 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d (mem3.aPool[j-
ae1a0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 1].u.hdr.size4x/
ae1b0 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 4)*8-8);. }.
ae1c0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
ae1d0 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 66 6f "\n"); . }. fo
ae1e0 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 53 48 3b r(i=0; i<N_HASH;
ae1f0 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d i++){. if( m
ae200 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 3d 3d 30 em3.aiHash[i]==0
ae210 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
ae220 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 68 fprintf(out, "h
ae230 61 73 68 28 25 32 64 29 3a 22 2c 20 69 29 3b 0a ash(%2d):", i);.
ae240 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 for(j = mem3
ae250 2e 61 69 48 61 73 68 5b 69 5d 3b 20 6a 3e 30 3b .aiHash[i]; j>0;
ae260 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d j=mem3.aPool[j]
ae270 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 .u.list.next){.
ae280 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
ae290 2c 20 22 20 25 70 28 25 64 29 22 2c 20 26 6d 65 , " %p(%d)", &me
ae2a0 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 m3.aPool[j],.
ae2b0 20 20 20 20 20 20 20 20 20 20 20 28 6d 65 6d 33 (mem3
ae2c0 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 .aPool[j-1].u.hd
ae2d0 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38 29 r.size4x/4)*8-8)
ae2e0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 72 69 ;. }. fpri
ae2f0 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 ntf(out, "\n");
ae300 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f . }. fprintf(o
ae310 75 74 2c 20 22 6d 61 73 74 65 72 3d 25 64 5c 6e ut, "master=%d\n
ae320 22 2c 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 ", mem3.iMaster)
ae330 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c ;. fprintf(out,
ae340 20 22 6e 6f 77 55 73 65 64 3d 25 64 5c 6e 22 2c "nowUsed=%d\n",
ae350 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 mem3.nPool*8 -
ae360 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 38 29 mem3.szMaster*8)
ae370 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c ;. fprintf(out,
ae380 20 22 6d 78 55 73 65 64 3d 25 64 5c 6e 22 2c 20 "mxUsed=%d\n",
ae390 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d mem3.nPool*8 - m
ae3a0 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 2a 38 29 3b em3.mnMaster*8);
ae3b0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
ae3c0 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 _leave(mem3.mute
ae3d0 78 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 x);. if( out==s
ae3e0 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c tdout ){. ffl
ae3f0 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d ush(stdout);. }
ae400 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 else{. fclose
ae410 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 (out);. }.#else
ae420 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ae430 54 45 52 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a TER(zFilename);.
ae440 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
ae450 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
ae460 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 the only routine
ae470 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 in this file wi
ae480 74 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a 20 th external .**
ae490 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 linkage..**.** P
ae4a0 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d opulate the low-
ae4b0 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c level memory all
ae4c0 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e ocation function
ae4d0 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 pointers in.**
ae4e0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
ae4f0 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74 fig.m with point
ae500 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69 ers to the routi
ae510 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 nes in this file
ae520 2e 20 54 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e . The.** argumen
ae530 74 73 20 73 70 65 63 69 66 79 20 74 68 65 20 62 ts specify the b
ae540 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 lock of memory t
ae550 6f 20 6d 61 6e 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 o manage..**.**
ae560 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
ae570 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 62 79 20 73 only called by s
ae580 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 2c qlite3_config(),
ae590 20 61 6e 64 20 74 68 65 72 65 66 6f 72 65 0a 2a and therefore.*
ae5a0 2a 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 * is not require
ae5b0 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 61 d to be threadsa
ae5c0 66 65 20 28 69 74 20 69 73 20 6e 6f 74 29 2e 0a fe (it is not)..
ae5d0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
ae5e0 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f E const sqlite3_
ae5f0 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c mem_methods *sql
ae600 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 ite3MemGetMemsys
ae610 33 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 3(void){. stati
ae620 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
ae630 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 65 6d 70 mem_methods memp
ae640 6f 6f 6c 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 oolMethods = {.
ae650 20 20 20 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f memsys3Mallo
ae660 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 46 c,. memsys3F
ae670 72 65 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 ree,. memsys
ae680 33 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 3Realloc,. m
ae690 65 6d 73 79 73 33 53 69 7a 65 2c 0a 20 20 20 20 emsys3Size,.
ae6a0 20 6d 65 6d 73 79 73 33 52 6f 75 6e 64 75 70 2c memsys3Roundup,
ae6b0 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 49 6e 69 . memsys3Ini
ae6c0 74 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53 t,. memsys3S
ae6d0 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a hutdown,. 0.
ae6e0 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d };. return &m
ae6f0 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 3b 0a 7d empoolMethods;.}
ae700 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ..#endif /* SQLI
ae710 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
ae720 33 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 3 */../*********
ae730 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d ***** End of mem
ae740 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 3.c ************
ae750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ae760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ae770 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
ae780 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
ae790 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a mem5.c ********
ae7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ae7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ae7c0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
ae7d0 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 0a 2a October 14.**.*
ae7e0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
ae7f0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
ae800 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
ae810 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
ae820 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
ae830 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
ae840 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
ae850 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
ae860 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
ae870 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
ae880 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
ae890 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
ae8a0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
ae8b0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
ae8c0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
ae8d0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
ae8e0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
ae8f0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
ae900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ae910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ae920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ae930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
ae940 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
ae950 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 the C functions
ae960 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 that implement
ae970 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f a memory.** allo
ae980 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
ae990 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c 69 for use by SQLi
ae9a0 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 te. .**.** This
ae9b0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d version of the m
ae9c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
ae9d0 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 74 73 subsystem omits
ae9e0 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 20 6d all.** use of m
ae9f0 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 61 70 70 alloc(). The app
aea00 6c 69 63 61 74 69 6f 6e 20 67 69 76 65 73 20 53 lication gives S
aea10 51 4c 69 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66 QLite a block of
aea20 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 memory.** befor
aea30 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 e calling sqlite
aea40 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 3_initialize() f
aea50 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 61 rom which alloca
aea60 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64 tions.** are mad
aea70 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 62 e and returned b
aea80 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 y the xMalloc()
aea90 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a and xRealloc() .
aeaa0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
aeab0 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 ns. Once sqlite3
aeac0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 61 _initialize() ha
aead0 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a s been called,.*
aeae0 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 * the amount of
aeaf0 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 memory available
aeb00 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20 66 69 to SQLite is fi
aeb10 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a xed and cannot.*
aeb20 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a * be changed..**
aeb30 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
aeb40 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
aeb50 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
aeb60 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 0a tem is included.
aeb70 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64 20 ** in the build
aeb80 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f 45 only if SQLITE_E
aeb90 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69 73 NABLE_MEMSYS5 is
aeba0 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 defined..**.**
aebb0 54 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f This memory allo
aebc0 63 61 74 6f 72 20 75 73 65 73 20 74 68 65 20 66 cator uses the f
aebd0 6f 6c 6c 6f 77 69 6e 67 20 61 6c 67 6f 72 69 74 ollowing algorit
aebe0 68 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 hm:.**.** 1.
aebf0 41 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 All memory alloc
aec00 61 74 69 6f 6e 73 20 73 69 7a 65 73 20 61 72 65 ations sizes are
aec10 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 61 rounded up to a
aec20 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a power of 2..**.
aec30 2a 2a 20 20 20 32 2e 20 20 49 66 20 74 77 6f 20 ** 2. If two
aec40 61 64 6a 61 63 65 6e 74 20 66 72 65 65 20 62 6c adjacent free bl
aec50 6f 63 6b 73 20 61 72 65 20 74 68 65 20 68 61 6c ocks are the hal
aec60 76 65 73 20 6f 66 20 61 20 6c 61 72 67 65 72 20 ves of a larger
aec70 62 6c 6f 63 6b 2c 0a 2a 2a 20 20 20 20 20 20 20 block,.**
aec80 74 68 65 6e 20 74 68 65 20 74 77 6f 20 62 6c 6f then the two blo
aec90 63 6b 73 20 61 72 65 20 63 6f 61 6c 65 73 65 64 cks are coalesed
aeca0 20 69 6e 74 6f 20 74 68 65 20 73 69 6e 67 6c 65 into the single
aecb0 20 6c 61 72 67 65 72 20 62 6c 6f 63 6b 2e 0a 2a larger block..*
aecc0 2a 0a 2a 2a 20 20 20 33 2e 20 20 4e 65 77 20 6d *.** 3. New m
aecd0 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 emory is allocat
aece0 65 64 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 ed from the firs
aecf0 74 20 61 76 61 69 6c 61 62 6c 65 20 66 72 65 65 t available free
aed00 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 block..**.** Th
aed10 69 73 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 is algorithm is
aed20 64 65 73 63 72 69 62 65 64 20 69 6e 3a 20 4a 2e described in: J.
aed30 20 4d 2e 20 52 6f 62 73 6f 6e 2e 20 22 42 6f 75 M. Robson. "Bou
aed40 6e 64 73 20 66 6f 72 20 53 6f 6d 65 20 46 75 6e nds for Some Fun
aed50 63 74 69 6f 6e 73 0a 2a 2a 20 43 6f 6e 63 65 72 ctions.** Concer
aed60 6e 69 6e 67 20 44 79 6e 61 6d 69 63 20 53 74 6f ning Dynamic Sto
aed70 72 61 67 65 20 41 6c 6c 6f 63 61 74 69 6f 6e 22 rage Allocation"
aed80 2e 20 4a 6f 75 72 6e 61 6c 20 6f 66 20 74 68 65 . Journal of the
aed90 20 41 73 73 6f 63 69 61 74 69 6f 6e 20 66 6f 72 Association for
aeda0 0a 2a 2a 20 43 6f 6d 70 75 74 69 6e 67 20 4d 61 .** Computing Ma
aedb0 63 68 69 6e 65 72 79 2c 20 56 6f 6c 75 6d 65 20 chinery, Volume
aedc0 32 31 2c 20 4e 75 6d 62 65 72 20 38 2c 20 4a 75 21, Number 8, Ju
aedd0 6c 79 20 31 39 37 34 2c 20 70 61 67 65 73 20 34 ly 1974, pages 4
aede0 39 31 2d 34 39 39 2e 0a 2a 2a 20 0a 2a 2a 20 4c 91-499..** .** L
aedf0 65 74 20 6e 20 62 65 20 74 68 65 20 73 69 7a 65 et n be the size
aee00 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 of the largest
aee10 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 69 76 69 64 allocation divid
aee20 65 64 20 62 79 20 74 68 65 20 6d 69 6e 69 6d 75 ed by the minimu
aee30 6d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 m.** allocation
aee40 73 69 7a 65 20 28 61 66 74 65 72 20 72 6f 75 6e size (after roun
aee50 64 69 6e 67 20 61 6c 6c 20 73 69 7a 65 73 20 75 ding all sizes u
aee60 70 20 74 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 p to a power of
aee70 32 2e 29 20 20 4c 65 74 20 4d 0a 2a 2a 20 62 65 2.) Let M.** be
aee80 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f the maximum amo
aee90 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 65 76 unt of memory ev
aeea0 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 er outstanding a
aeeb0 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 4c 65 74 t one time. Let
aeec0 0a 2a 2a 20 4e 20 62 65 20 74 68 65 20 74 6f 74 .** N be the tot
aeed0 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d al amount of mem
aeee0 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 6f ory available fo
aeef0 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 52 r allocation. R
aef00 6f 62 73 6f 6e 0a 2a 2a 20 70 72 6f 76 65 64 20 obson.** proved
aef10 74 68 61 74 20 74 68 69 73 20 6d 65 6d 6f 72 79 that this memory
aef20 20 61 6c 6c 6f 63 61 74 6f 72 20 77 69 6c 6c 20 allocator will
aef30 6e 65 76 65 72 20 62 72 65 61 6b 64 6f 77 6e 20 never breakdown
aef40 64 75 65 20 74 6f 20 0a 2a 2a 20 66 72 61 67 6d due to .** fragm
aef50 65 6e 74 61 74 69 6f 6e 20 61 73 20 6c 6f 6e 67 entation as long
aef60 20 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e as the followin
aef70 67 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 6f 6c g constraint hol
aef80 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 4e ds:.**.** N
aef90 20 3e 3d 20 20 4d 2a 28 31 20 2b 20 6c 6f 67 32 >= M*(1 + log2
aefa0 28 6e 29 2f 32 29 20 2d 20 6e 20 2b 20 31 0a 2a (n)/2) - n + 1.*
aefb0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
aefc0 5f 73 74 61 74 75 73 28 29 20 6c 6f 67 69 63 20 _status() logic
aefd0 74 72 61 63 6b 73 20 74 68 65 20 6d 61 78 69 6d tracks the maxim
aefe0 75 6d 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 61 um values of n a
aeff0 6e 64 20 4d 20 73 6f 0a 2a 2a 20 74 68 61 74 20 nd M so.** that
af000 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 an application c
af010 61 6e 2c 20 61 74 20 61 6e 79 20 74 69 6d 65 2c an, at any time,
af020 20 76 65 72 69 66 79 20 74 68 69 73 20 63 6f 6e verify this con
af030 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a straint..*/../*.
af040 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 ** This version
af050 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c of the memory al
af060 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64 20 locator is used
af070 6f 6e 6c 79 20 77 68 65 6e 20 0a 2a 2a 20 53 51 only when .** SQ
af080 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 LITE_ENABLE_MEMS
af090 59 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a YS5 is defined..
af0a0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
af0b0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a _ENABLE_MEMSYS5.
af0c0 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d ./*.** A minimum
af0d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 allocation is a
af0e0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
af0f0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
af100 63 74 75 72 65 2e 0a 2a 2a 20 4c 61 72 67 65 72 cture..** Larger
af110 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 allocations are
af120 20 61 6e 20 61 72 72 61 79 20 6f 66 20 74 68 65 an array of the
af130 73 65 20 73 74 72 75 63 74 75 72 65 73 20 77 68 se structures wh
af140 65 72 65 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 ere the.** size
af150 6f 66 20 74 68 65 20 61 72 72 61 79 20 69 73 20 of the array is
af160 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2a a power of 2..**
af170 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 .** The size of
af180 74 68 69 73 20 6f 62 6a 65 63 74 20 6d 75 73 74 this object must
af190 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 be a power of t
af1a0 77 6f 2e 20 20 54 68 61 74 20 66 61 63 74 20 69 wo. That fact i
af1b0 73 0a 2a 2a 20 76 65 72 69 66 69 65 64 20 69 6e s.** verified in
af1c0 20 6d 65 6d 73 79 73 35 49 6e 69 74 28 29 2e 0a memsys5Init()..
af1d0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
af1e0 74 20 4d 65 6d 35 4c 69 6e 6b 20 4d 65 6d 35 4c t Mem5Link Mem5L
af1f0 69 6e 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d 35 ink;.struct Mem5
af200 4c 69 6e 6b 20 7b 0a 20 20 69 6e 74 20 6e 65 78 Link {. int nex
af210 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 t; /* Inde
af220 78 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 63 x of next free c
af230 68 75 6e 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 72 hunk */. int pr
af240 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 ev; /* Ind
af250 65 78 20 6f 66 20 70 72 65 76 69 6f 75 73 20 66 ex of previous f
af260 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a ree chunk */.};.
af270 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 ./*.** Maximum s
af280 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c 6c 6f 63 ize of any alloc
af290 61 74 69 6f 6e 20 69 73 20 28 28 31 3c 3c 4c 4f ation is ((1<<LO
af2a0 47 4d 41 58 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f GMAX)*mem5.szAto
af2b0 6d 29 2e 20 53 69 6e 63 65 0a 2a 2a 20 6d 65 6d m). Since.** mem
af2c0 35 2e 73 7a 41 74 6f 6d 20 69 73 20 61 6c 77 61 5.szAtom is alwa
af2d0 79 73 20 61 74 20 6c 65 61 73 74 20 38 20 61 6e ys at least 8 an
af2e0 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 d 32-bit integer
af2f0 73 20 61 72 65 20 75 73 65 64 2c 0a 2a 2a 20 69 s are used,.** i
af300 74 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c t is not actuall
af310 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 y possible to re
af320 61 63 68 20 74 68 69 73 20 6c 69 6d 69 74 2e 0a ach this limit..
af330 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 4f 47 4d 41 */.#define LOGMA
af340 58 20 33 30 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b X 30../*.** Mask
af350 73 20 75 73 65 64 20 66 6f 72 20 6d 65 6d 35 2e s used for mem5.
af360 61 43 74 72 6c 5b 5d 20 65 6c 65 6d 65 6e 74 73 aCtrl[] elements
af370 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 ..*/.#define CTR
af380 4c 5f 4c 4f 47 53 49 5a 45 20 20 30 78 31 66 20 L_LOGSIZE 0x1f
af390 20 20 20 2f 2a 20 4c 6f 67 32 20 53 69 7a 65 20 /* Log2 Size
af3a0 6f 66 20 74 68 69 73 20 62 6c 6f 63 6b 20 2a 2f of this block */
af3b0 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 46 52 .#define CTRL_FR
af3c0 45 45 20 20 20 20 20 30 78 32 30 20 20 20 20 2f EE 0x20 /
af3d0 2a 20 54 72 75 65 20 69 66 20 6e 6f 74 20 63 68 * True if not ch
af3e0 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 0a 2f 2a ecked out */../*
af3f0 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 .** All of the s
af400 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 tatic variables
af410 75 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f 64 used by this mod
af420 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 ule are collecte
af430 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 d.** into a sing
af440 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d le structure nam
af450 65 64 20 22 6d 65 6d 35 22 2e 20 20 54 68 69 73 ed "mem5". This
af460 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65 0a is to keep the.
af470 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61 62 ** static variab
af480 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e les organized an
af490 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 d to reduce name
af4a0 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a space pollution.
af4b0 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f 64 ** when this mod
af4c0 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 20 ule is combined
af4d0 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74 68 with other in th
af4e0 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a e amalgamation..
af4f0 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 */.static SQLITE
af500 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d 35 _WSD struct Mem5
af510 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 Global {. /*.
af520 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 ** Memory availa
af530 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 ble for allocati
af540 6f 6e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 73 7a on. */. int sz
af550 41 74 6f 6d 3b 20 20 20 20 20 20 2f 2a 20 53 6d Atom; /* Sm
af560 61 6c 6c 65 73 74 20 70 6f 73 73 69 62 6c 65 20 allest possible
af570 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 62 79 allocation in by
af580 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 6c tes */. int nBl
af590 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d ock; /* Num
af5a0 62 65 72 20 6f 66 20 73 7a 41 74 6f 6d 20 73 69 ber of szAtom si
af5b0 7a 65 64 20 62 6c 6f 63 6b 73 20 69 6e 20 7a 50 zed blocks in zP
af5c0 6f 6f 6c 20 2a 2f 0a 20 20 75 38 20 2a 7a 50 6f ool */. u8 *zPo
af5d0 6f 6c 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d ol; /* Mem
af5e0 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f ory available to
af5f0 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f be allocated */
af600 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 . . /*. ** Mu
af610 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 tex to control a
af620 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65 6d ccess to the mem
af630 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
af640 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 ubsystem.. */.
af650 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
af660 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a mutex;.. /*. *
af670 2a 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 73 74 * Performance st
af680 61 74 69 73 74 69 63 73 0a 20 20 2a 2f 0a 20 20 atistics. */.
af690 75 36 34 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 u64 nAlloc;
af6a0 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
af6b0 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f 20 ber of calls to
af6c0 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 36 34 20 malloc */. u64
af6d0 74 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 totalAlloc;
af6e0 2f 2a 20 54 6f 74 61 6c 20 6f 66 20 61 6c 6c 20 /* Total of all
af6f0 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 2d 20 69 malloc calls - i
af700 6e 63 6c 75 64 65 73 20 69 6e 74 65 72 6e 61 6c ncludes internal
af710 20 66 72 61 67 20 2a 2f 0a 20 20 75 36 34 20 74 frag */. u64 t
af720 6f 74 61 6c 45 78 63 65 73 73 3b 20 20 20 20 2f otalExcess; /
af730 2a 20 54 6f 74 61 6c 20 69 6e 74 65 72 6e 61 6c * Total internal
af740 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a fragmentation *
af750 2f 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 4f /. u32 currentO
af760 75 74 3b 20 20 20 20 20 2f 2a 20 43 75 72 72 65 ut; /* Curre
af770 6e 74 20 63 68 65 63 6b 6f 75 74 2c 20 69 6e 63 nt checkout, inc
af780 6c 75 64 69 6e 67 20 69 6e 74 65 72 6e 61 6c 20 luding internal
af790 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f fragmentation */
af7a0 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 43 6f . u32 currentCo
af7b0 75 6e 74 3b 20 20 20 2f 2a 20 43 75 72 72 65 6e unt; /* Curren
af7c0 74 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 73 74 t number of dist
af7d0 69 6e 63 74 20 63 68 65 63 6b 6f 75 74 73 20 2a inct checkouts *
af7e0 2f 0a 20 20 75 33 32 20 6d 61 78 4f 75 74 3b 20 /. u32 maxOut;
af7f0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
af800 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 um instantaneous
af810 20 63 75 72 72 65 6e 74 4f 75 74 20 2a 2f 0a 20 currentOut */.
af820 20 75 33 32 20 6d 61 78 43 6f 75 6e 74 3b 20 20 u32 maxCount;
af830 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
af840 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 instantaneous cu
af850 72 72 65 6e 74 43 6f 75 6e 74 20 2a 2f 0a 20 20 rrentCount */.
af860 75 33 32 20 6d 61 78 52 65 71 75 65 73 74 3b 20 u32 maxRequest;
af870 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 61 /* Largest a
af880 6c 6c 6f 63 61 74 69 6f 6e 20 28 65 78 63 6c 75 llocation (exclu
af890 73 69 76 65 20 6f 66 20 69 6e 74 65 72 6e 61 6c sive of internal
af8a0 20 66 72 61 67 29 20 2a 2f 0a 20 20 0a 20 20 2f frag) */. . /
af8b0 2a 0a 20 20 2a 2a 20 4c 69 73 74 73 20 6f 66 20 *. ** Lists of
af8c0 66 72 65 65 20 62 6c 6f 63 6b 73 2e 20 20 61 69 free blocks. ai
af8d0 46 72 65 65 6c 69 73 74 5b 30 5d 20 69 73 20 61 Freelist[0] is a
af8e0 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 62 6c list of free bl
af8f0 6f 63 6b 73 20 6f 66 0a 20 20 2a 2a 20 73 69 7a ocks of. ** siz
af900 65 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2e 20 20 e mem5.szAtom.
af910 61 69 46 72 65 65 6c 69 73 74 5b 31 5d 20 68 6f aiFreelist[1] ho
af920 6c 64 73 20 62 6c 6f 63 6b 73 20 6f 66 20 73 69 lds blocks of si
af930 7a 65 20 73 7a 41 74 6f 6d 2a 32 2e 0a 20 20 2a ze szAtom*2.. *
af940 2a 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a * and so forth..
af950 20 20 2a 2f 0a 20 20 69 6e 74 20 61 69 46 72 65 */. int aiFre
af960 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b 31 5d 3b elist[LOGMAX+1];
af970 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 70 61 63 .. /*. ** Spac
af980 65 20 66 6f 72 20 74 72 61 63 6b 69 6e 67 20 77 e for tracking w
af990 68 69 63 68 20 62 6c 6f 63 6b 73 20 61 72 65 20 hich blocks are
af9a0 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 checked out and
af9b0 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 the size. ** of
af9c0 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20 20 4f 6e each block. On
af9d0 65 20 62 79 74 65 20 70 65 72 20 62 6c 6f 63 6b e byte per block
af9e0 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 2a 61 43 74 .. */. u8 *aCt
af9f0 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 3b 0a 0a 2f 2a rl;..} mem5;../*
afa00 0a 2a 2a 20 41 63 63 65 73 73 20 74 68 65 20 73 .** Access the s
afa10 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 20 74 tatic variable t
afa20 68 72 6f 75 67 68 20 61 20 6d 61 63 72 6f 20 66 hrough a macro f
afa30 6f 72 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 or SQLITE_OMIT_W
afa40 53 44 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 6d 65 SD.*/.#define me
afa50 6d 35 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 m5 GLOBAL(struct
afa60 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c 20 6d 65 6d Mem5Global, mem
afa70 35 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 5)../*.** Assumi
afa80 6e 67 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 69 73 ng mem5.zPool is
afa90 20 64 69 76 69 64 65 64 20 75 70 20 69 6e 74 6f divided up into
afaa0 20 61 6e 20 61 72 72 61 79 20 6f 66 20 4d 65 6d an array of Mem
afab0 35 4c 69 6e 6b 0a 2a 2a 20 73 74 72 75 63 74 75 5Link.** structu
afac0 72 65 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f res, return a po
afad0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 69 64 78 inter to the idx
afae0 2d 74 68 20 73 75 63 68 20 6c 69 6b 2e 0a 2a 2f -th such lik..*/
afaf0 0a 23 64 65 66 69 6e 65 20 4d 45 4d 35 4c 49 4e .#define MEM5LIN
afb00 4b 28 69 64 78 29 20 28 28 4d 65 6d 35 4c 69 6e K(idx) ((Mem5Lin
afb10 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a 50 6f 6f 6c k *)(&mem5.zPool
afb20 5b 28 69 64 78 29 2a 6d 65 6d 35 2e 73 7a 41 74 [(idx)*mem5.szAt
afb30 6f 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c om]))../*.** Unl
afb40 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 ink the chunk at
afb50 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 66 mem5.aPool[i] f
afb60 72 6f 6d 20 6c 69 73 74 20 69 74 20 69 73 20 63 rom list it is c
afb70 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 urrently.** on.
afb80 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20 66 6f It should be fo
afb90 75 6e 64 20 6f 6e 20 6d 65 6d 35 2e 61 69 46 72 und on mem5.aiFr
afba0 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d eelist[iLogsize]
afbb0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
afbc0 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 memsys5Unlink(i
afbd0 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67 73 69 nt i, int iLogsi
afbe0 7a 65 29 7b 0a 20 20 69 6e 74 20 6e 65 78 74 2c ze){. int next,
afbf0 20 70 72 65 76 3b 0a 20 20 61 73 73 65 72 74 28 prev;. assert(
afc00 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e i>=0 && i<mem5.
afc10 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 nBlock );. asse
afc20 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 rt( iLogsize>=0
afc30 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 && iLogsize<=LOG
afc40 4d 41 58 20 29 3b 0a 20 20 61 73 73 65 72 74 28 MAX );. assert(
afc50 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 (mem5.aCtrl[i]
afc60 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d & CTRL_LOGSIZE)=
afc70 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 =iLogsize );..
afc80 6e 65 78 74 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 next = MEM5LINK(
afc90 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 70 72 65 76 i)->next;. prev
afca0 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e = MEM5LINK(i)->
afcb0 70 72 65 76 3b 0a 20 20 69 66 28 20 70 72 65 76 prev;. if( prev
afcc0 3c 30 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 <0 ){. mem5.a
afcd0 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 iFreelist[iLogsi
afce0 7a 65 5d 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 ze] = next;. }e
afcf0 6c 73 65 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e lse{. MEM5LIN
afd00 4b 28 70 72 65 76 29 2d 3e 6e 65 78 74 20 3d 20 K(prev)->next =
afd10 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 next;. }. if(
afd20 6e 65 78 74 3e 3d 30 20 29 7b 0a 20 20 20 20 4d next>=0 ){. M
afd30 45 4d 35 4c 49 4e 4b 28 6e 65 78 74 29 2d 3e 70 EM5LINK(next)->p
afd40 72 65 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a rev = prev;. }.
afd50 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 }../*.** Link th
afd60 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 35 2e e chunk at mem5.
afd70 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20 74 68 61 74 aPool[i] so that
afd80 20 69 73 20 6f 6e 20 74 68 65 20 69 4c 6f 67 73 is on the iLogs
afd90 69 7a 65 0a 2a 2a 20 66 72 65 65 20 6c 69 73 74 ize.** free list
afda0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
afdb0 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 6e 74 memsys5Link(int
afdc0 20 69 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 i, int iLogsize
afdd0 29 7b 0a 20 20 69 6e 74 20 78 3b 0a 20 20 61 73 ){. int x;. as
afde0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
afdf0 74 65 78 5f 68 65 6c 64 28 6d 65 6d 35 2e 6d 75 tex_held(mem5.mu
afe00 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
afe10 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 6d 35 ( i>=0 && i<mem5
afe20 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 .nBlock );. ass
afe30 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 ert( iLogsize>=0
afe40 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f && iLogsize<=LO
afe50 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 65 72 74 GMAX );. assert
afe60 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d ( (mem5.aCtrl[i]
afe70 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 & CTRL_LOGSIZE)
afe80 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 ==iLogsize );..
afe90 20 78 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 x = MEM5LINK(i)
afea0 2d 3e 6e 65 78 74 20 3d 20 6d 65 6d 35 2e 61 69 ->next = mem5.ai
afeb0 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a Freelist[iLogsiz
afec0 65 5d 3b 0a 20 20 4d 45 4d 35 4c 49 4e 4b 28 69 e];. MEM5LINK(i
afed0 29 2d 3e 70 72 65 76 20 3d 20 2d 31 3b 0a 20 20 )->prev = -1;.
afee0 69 66 28 20 78 3e 3d 30 20 29 7b 0a 20 20 20 20 if( x>=0 ){.
afef0 61 73 73 65 72 74 28 20 78 3c 6d 65 6d 35 2e 6e assert( x<mem5.n
aff00 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 4d 45 4d Block );. MEM
aff10 35 4c 49 4e 4b 28 78 29 2d 3e 70 72 65 76 20 3d 5LINK(x)->prev =
aff20 20 69 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 i;. }. mem5.a
aff30 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 iFreelist[iLogsi
aff40 7a 65 5d 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a ze] = i;.}../*.*
aff50 2a 20 49 66 20 74 68 65 20 53 54 41 54 49 43 5f * If the STATIC_
aff60 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 MEM mutex is not
aff70 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f already held, o
aff80 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68 btain it now. Th
aff90 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 e mutex.** will
affa0 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c 64 20 already be held
affb0 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63 6f 64 (obtained by cod
affc0 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 e in malloc.c) i
affd0 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 f.** sqlite3Glob
affe0 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61 alConfig.bMemSta
afff0 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 t is true..*/.st
b0000 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 atic void memsys
b0010 35 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a 20 20 5Enter(void){.
b0020 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
b0030 74 65 72 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b ter(mem5.mutex);
b0040 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d .}.static void m
b0050 65 6d 73 79 73 35 4c 65 61 76 65 28 76 6f 69 64 emsys5Leave(void
b0060 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 ){. sqlite3_mut
b0070 65 78 5f 6c 65 61 76 65 28 6d 65 6d 35 2e 6d 75 ex_leave(mem5.mu
b0080 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 tex);.}../*.** R
b0090 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f eturn the size o
b00a0 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 f an outstanding
b00b0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 allocation, in
b00c0 62 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 bytes. The.** s
b00d0 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f 6d 69 ize returned omi
b00e0 74 73 20 74 68 65 20 38 2d 62 79 74 65 20 68 65 ts the 8-byte he
b00f0 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e 20 20 ader overhead.
b0100 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 This only.** wor
b0110 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20 74 68 ks for chunks th
b0120 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 at are currently
b0130 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f checked out..*/
b0140 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 .static int mems
b0150 79 73 35 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 ys5Size(void *p)
b0160 7b 0a 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 {. int iSize =
b0170 30 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 0;. if( p ){.
b0180 20 20 69 6e 74 20 69 20 3d 20 28 28 75 38 20 2a int i = ((u8 *
b0190 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d )p-mem5.zPool)/m
b01a0 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20 20 20 20 em5.szAtom;.
b01b0 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
b01c0 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b i<mem5.nBlock );
b01d0 0a 20 20 20 20 69 53 69 7a 65 20 3d 20 6d 65 6d . iSize = mem
b01e0 35 2e 73 7a 41 74 6f 6d 20 2a 20 28 31 20 3c 3c 5.szAtom * (1 <<
b01f0 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 26 (mem5.aCtrl[i]&
b0200 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 29 3b 0a CTRL_LOGSIZE));.
b0210 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 53 69 }. return iSi
b0220 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e ze;.}../*.** Fin
b0230 64 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 d the first entr
b0240 79 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 y on the freelis
b0250 74 20 69 4c 6f 67 73 69 7a 65 2e 20 20 55 6e 6c t iLogsize. Unl
b0260 69 6e 6b 20 74 68 61 74 0a 2a 2a 20 65 6e 74 72 ink that.** entr
b0270 79 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 73 y and return its
b0280 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73 74 61 74 index. .*/.stat
b0290 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 55 6e ic int memsys5Un
b02a0 6c 69 6e 6b 46 69 72 73 74 28 69 6e 74 20 69 4c linkFirst(int iL
b02b0 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 69 ogsize){. int i
b02c0 3b 0a 20 20 69 6e 74 20 69 46 69 72 73 74 3b 0a ;. int iFirst;.
b02d0 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 . assert( iLogs
b02e0 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 ize>=0 && iLogsi
b02f0 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 ze<=LOGMAX );.
b0300 69 20 3d 20 69 46 69 72 73 74 20 3d 20 6d 65 6d i = iFirst = mem
b0310 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 5.aiFreelist[iLo
b0320 67 73 69 7a 65 5d 3b 0a 20 20 61 73 73 65 72 74 gsize];. assert
b0330 28 20 69 46 69 72 73 74 3e 3d 30 20 29 3b 0a 20 ( iFirst>=0 );.
b0340 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 while( i>0 ){.
b0350 20 20 20 69 66 28 20 69 3c 69 46 69 72 73 74 20 if( i<iFirst
b0360 29 20 69 46 69 72 73 74 20 3d 20 69 3b 0a 20 20 ) iFirst = i;.
b0370 20 20 69 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 i = MEM5LINK(i
b0380 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 6d )->next;. }. m
b0390 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 46 69 emsys5Unlink(iFi
b03a0 72 73 74 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a rst, iLogsize);.
b03b0 20 20 72 65 74 75 72 6e 20 69 46 69 72 73 74 3b return iFirst;
b03c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
b03d0 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f a block of memo
b03e0 72 79 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e ry of at least n
b03f0 42 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a Bytes in size..*
b0400 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 * Return NULL if
b0410 20 75 6e 61 62 6c 65 2e 20 20 52 65 74 75 72 6e unable. Return
b0420 20 4e 55 4c 4c 20 69 66 20 6e 42 79 74 65 73 3d NULL if nBytes=
b0430 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 =0..**.** The ca
b0440 6c 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 ller guarantees
b0450 74 68 61 74 20 6e 42 79 74 65 20 70 6f 73 69 74 that nByte posit
b0460 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ive..**.** The c
b0470 61 6c 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e aller has obtain
b0480 65 64 20 61 20 6d 75 74 65 78 20 70 72 69 6f 72 ed a mutex prior
b0490 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 to invoking thi
b04a0 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 73 6f 20 s.** routine so
b04b0 74 68 65 72 65 20 69 73 20 6e 65 76 65 72 20 61 there is never a
b04c0 6e 79 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 ny chance that t
b04d0 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 74 68 wo or more.** th
b04e0 72 65 61 64 73 20 63 61 6e 20 62 65 20 69 6e 20 reads can be in
b04f0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74 20 this routine at
b0500 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a the same time..*
b0510 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d /.static void *m
b0520 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 emsys5MallocUnsa
b0530 66 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 fe(int nByte){.
b0540 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
b0550 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 /* Index of a
b0560 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 5d 20 73 6c 6f mem5.aPool[] slo
b0570 74 20 2a 2f 0a 20 20 69 6e 74 20 69 42 69 6e 3b t */. int iBin;
b0580 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
b0590 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 69 46 72 65 into mem5.aiFre
b05a0 65 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 elist[] */. int
b05b0 20 69 46 75 6c 6c 53 7a 3b 20 20 20 20 20 2f 2a iFullSz; /*
b05c0 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 Size of allocat
b05d0 69 6f 6e 20 72 6f 75 6e 64 65 64 20 75 70 20 74 ion rounded up t
b05e0 6f 20 70 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0a o power of 2 */.
b05f0 20 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 3b 20 int iLogsize;
b0600 20 20 20 2f 2a 20 4c 6f 67 32 20 6f 66 20 69 46 /* Log2 of iF
b0610 75 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20 2a ullSz/POW2_MIN *
b0620 2f 0a 0a 20 20 2f 2a 20 6e 42 79 74 65 20 6d 75 /.. /* nByte mu
b0630 73 74 20 62 65 20 61 20 70 6f 73 69 74 69 76 65 st be a positive
b0640 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e 42 */. assert( nB
b0650 79 74 65 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 4b yte>0 );.. /* K
b0660 65 65 70 20 74 72 61 63 6b 20 6f 66 20 74 68 65 eep track of the
b0670 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 maximum allocat
b0680 69 6f 6e 20 72 65 71 75 65 73 74 2e 20 20 45 76 ion request. Ev
b0690 65 6e 20 75 6e 66 75 6c 66 69 6c 6c 65 64 0a 20 en unfulfilled.
b06a0 20 2a 2a 20 72 65 71 75 65 73 74 73 20 61 72 65 ** requests are
b06b0 20 63 6f 75 6e 74 65 64 20 2a 2f 0a 20 20 69 66 counted */. if
b06c0 28 20 28 75 33 32 29 6e 42 79 74 65 3e 6d 65 6d ( (u32)nByte>mem
b06d0 35 2e 6d 61 78 52 65 71 75 65 73 74 20 29 7b 0a 5.maxRequest ){.
b06e0 20 20 20 20 6d 65 6d 35 2e 6d 61 78 52 65 71 75 mem5.maxRequ
b06f0 65 73 74 20 3d 20 6e 42 79 74 65 3b 0a 20 20 7d est = nByte;. }
b0700 0a 0a 20 20 2f 2a 20 41 62 6f 72 74 20 69 66 20 .. /* Abort if
b0710 74 68 65 20 72 65 71 75 65 73 74 65 64 20 61 6c the requested al
b0720 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 69 73 location size is
b0730 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 larger than the
b0740 20 6c 61 72 67 65 73 74 0a 20 20 2a 2a 20 70 6f largest. ** po
b0750 77 65 72 20 6f 66 20 74 77 6f 20 74 68 61 74 20 wer of two that
b0760 77 65 20 63 61 6e 20 72 65 70 72 65 73 65 6e 74 we can represent
b0770 20 75 73 69 6e 67 20 33 32 2d 62 69 74 20 73 69 using 32-bit si
b0780 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 20 gned integers..
b0790 20 2a 2f 0a 20 20 69 66 28 20 6e 42 79 74 65 20 */. if( nByte
b07a0 3e 20 30 78 34 30 30 30 30 30 30 30 20 29 7b 0a > 0x40000000 ){.
b07b0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
b07c0 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e 64 20 6e 42 }.. /* Round nB
b07d0 79 74 65 20 75 70 20 74 6f 20 74 68 65 20 6e 65 yte up to the ne
b07e0 78 74 20 76 61 6c 69 64 20 70 6f 77 65 72 20 6f xt valid power o
b07f0 66 20 74 77 6f 20 2a 2f 0a 20 20 66 6f 72 28 69 f two */. for(i
b0800 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 FullSz=mem5.szAt
b0810 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 3d 30 3b 20 om, iLogsize=0;
b0820 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 65 3b 20 69 iFullSz<nByte; i
b0830 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c 20 69 4c 6f FullSz *= 2, iLo
b0840 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a 20 20 2f 2a gsize++){}.. /*
b0850 20 4d 61 6b 65 20 73 75 72 65 20 6d 65 6d 35 2e Make sure mem5.
b0860 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 aiFreelist[iLogs
b0870 69 7a 65 5d 20 63 6f 6e 74 61 69 6e 73 20 61 74 ize] contains at
b0880 20 6c 65 61 73 74 20 6f 6e 65 20 66 72 65 65 0a least one free.
b0890 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20 ** block. If
b08a0 6e 6f 74 2c 20 74 68 65 6e 20 73 70 6c 69 74 20 not, then split
b08b0 61 20 62 6c 6f 63 6b 20 6f 66 20 74 68 65 20 6e a block of the n
b08c0 65 78 74 20 6c 61 72 67 65 72 20 70 6f 77 65 72 ext larger power
b08d0 20 6f 66 0a 20 20 2a 2a 20 74 77 6f 20 69 6e 20 of. ** two in
b08e0 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74 65 20 order to create
b08f0 61 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 6b a new free block
b0900 20 6f 66 20 73 69 7a 65 20 69 4c 6f 67 73 69 7a of size iLogsiz
b0910 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 42 e.. */. for(iB
b0920 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b 20 6d 65 6d in=iLogsize; mem
b0930 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 42 69 5.aiFreelist[iBi
b0940 6e 5d 3c 30 20 26 26 20 69 42 69 6e 3c 3d 4c 4f n]<0 && iBin<=LO
b0950 47 4d 41 58 3b 20 69 42 69 6e 2b 2b 29 7b 7d 0a GMAX; iBin++){}.
b0960 20 20 69 66 28 20 69 42 69 6e 3e 4c 4f 47 4d 41 if( iBin>LOGMA
b0970 58 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73 X ){. testcas
b0980 65 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c e( sqlite3Global
b0990 43 6f 6e 66 69 67 2e 78 4c 6f 67 21 3d 30 20 29 Config.xLog!=0 )
b09a0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f ;. sqlite3_lo
b09b0 67 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 g(SQLITE_NOMEM,
b09c0 22 66 61 69 6c 65 64 20 74 6f 20 61 6c 6c 6f 63 "failed to alloc
b09d0 61 74 65 20 25 75 20 62 79 74 65 73 22 2c 20 6e ate %u bytes", n
b09e0 42 79 74 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Byte);. retur
b09f0 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 20 3d 20 6d n 0;. }. i = m
b0a00 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73 emsys5UnlinkFirs
b0a10 74 28 69 42 69 6e 29 3b 0a 20 20 77 68 69 6c 65 t(iBin);. while
b0a20 28 20 69 42 69 6e 3e 69 4c 6f 67 73 69 7a 65 20 ( iBin>iLogsize
b0a30 29 7b 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69 ){. int newSi
b0a40 7a 65 3b 0a 0a 20 20 20 20 69 42 69 6e 2d 2d 3b ze;.. iBin--;
b0a50 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 31 . newSize = 1
b0a60 20 3c 3c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65 << iBin;. me
b0a70 6d 35 2e 61 43 74 72 6c 5b 69 2b 6e 65 77 53 69 m5.aCtrl[i+newSi
b0a80 7a 65 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 ze] = CTRL_FREE
b0a90 7c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 73 | iBin;. mems
b0aa0 79 73 35 4c 69 6e 6b 28 69 2b 6e 65 77 53 69 7a ys5Link(i+newSiz
b0ab0 65 2c 20 69 42 69 6e 29 3b 0a 20 20 7d 0a 20 20 e, iBin);. }.
b0ac0 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 3d 20 mem5.aCtrl[i] =
b0ad0 69 4c 6f 67 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 iLogsize;.. /*
b0ae0 55 70 64 61 74 65 20 61 6c 6c 6f 63 61 74 6f 72 Update allocator
b0af0 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 performance sta
b0b00 74 69 73 74 69 63 73 2e 20 2a 2f 0a 20 20 6d 65 tistics. */. me
b0b10 6d 35 2e 6e 41 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d m5.nAlloc++;. m
b0b20 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 2b em5.totalAlloc +
b0b30 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 6d 65 6d = iFullSz;. mem
b0b40 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 20 2b 3d 5.totalExcess +=
b0b50 20 69 46 75 6c 6c 53 7a 20 2d 20 6e 42 79 74 65 iFullSz - nByte
b0b60 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 ;. mem5.current
b0b70 43 6f 75 6e 74 2b 2b 3b 0a 20 20 6d 65 6d 35 2e Count++;. mem5.
b0b80 63 75 72 72 65 6e 74 4f 75 74 20 2b 3d 20 69 46 currentOut += iF
b0b90 75 6c 6c 53 7a 3b 0a 20 20 69 66 28 20 6d 65 6d ullSz;. if( mem
b0ba0 35 2e 6d 61 78 43 6f 75 6e 74 3c 6d 65 6d 35 2e 5.maxCount<mem5.
b0bb0 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 29 20 6d currentCount ) m
b0bc0 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 20 3d 20 6d em5.maxCount = m
b0bd0 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 em5.currentCount
b0be0 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 ;. if( mem5.max
b0bf0 4f 75 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 Out<mem5.current
b0c00 4f 75 74 20 29 20 6d 65 6d 35 2e 6d 61 78 4f 75 Out ) mem5.maxOu
b0c10 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 t = mem5.current
b0c20 4f 75 74 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 Out;.. /* Retur
b0c30 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
b0c40 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d he allocated mem
b0c50 6f 72 79 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e ory. */. return
b0c60 20 28 76 6f 69 64 2a 29 26 6d 65 6d 35 2e 7a 50 (void*)&mem5.zP
b0c70 6f 6f 6c 5b 69 2a 6d 65 6d 35 2e 73 7a 41 74 6f ool[i*mem5.szAto
b0c80 6d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 m];.}../*.** Fre
b0c90 65 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 e an outstanding
b0ca0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
b0cb0 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f on..*/.static vo
b0cc0 69 64 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e id memsys5FreeUn
b0cd0 73 61 66 65 28 76 6f 69 64 20 2a 70 4f 6c 64 29 safe(void *pOld)
b0ce0 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 69 4c {. u32 size, iL
b0cf0 6f 67 73 69 7a 65 3b 0a 20 20 69 6e 74 20 69 42 ogsize;. int iB
b0d00 6c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 lock;.. /* Set
b0d10 69 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20 69 6e iBlock to the in
b0d20 64 65 78 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b dex of the block
b0d30 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 pointed to by p
b0d40 4f 6c 64 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65 Old in . ** the
b0d50 20 61 72 72 61 79 20 6f 66 20 6d 65 6d 35 2e 73 array of mem5.s
b0d60 7a 41 74 6f 6d 20 62 79 74 65 20 62 6c 6f 63 6b zAtom byte block
b0d70 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 s pointed to by
b0d80 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a 20 20 2a 2f mem5.zPool.. */
b0d90 0a 20 20 69 42 6c 6f 63 6b 20 3d 20 28 28 75 38 . iBlock = ((u8
b0da0 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f *)pOld-mem5.zPo
b0db0 6f 6c 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b ol)/mem5.szAtom;
b0dc0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 .. /* Check tha
b0dd0 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 4f t the pointer pO
b0de0 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 ld points to a v
b0df0 61 6c 69 64 2c 20 6e 6f 6e 2d 66 72 65 65 20 62 alid, non-free b
b0e00 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 61 73 73 65 72 lock. */. asser
b0e10 74 28 20 69 42 6c 6f 63 6b 3e 3d 30 20 26 26 20 t( iBlock>=0 &&
b0e20 69 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42 6c 6f iBlock<mem5.nBlo
b0e30 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ck );. assert(
b0e40 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 ((u8 *)pOld-mem5
b0e50 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e 73 7a 41 .zPool)%mem5.szA
b0e60 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 tom==0 );. asse
b0e70 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b rt( (mem5.aCtrl[
b0e80 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 46 iBlock] & CTRL_F
b0e90 52 45 45 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 4c REE)==0 );.. iL
b0ea0 6f 67 73 69 7a 65 20 3d 20 6d 65 6d 35 2e 61 43 ogsize = mem5.aC
b0eb0 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 trl[iBlock] & CT
b0ec0 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 73 69 RL_LOGSIZE;. si
b0ed0 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 7a 65 ze = 1<<iLogsize
b0ee0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f ;. assert( iBlo
b0ef0 63 6b 2b 73 69 7a 65 2d 31 3c 28 75 33 32 29 6d ck+size-1<(u32)m
b0f00 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 0a 20 em5.nBlock );..
b0f10 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
b0f20 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 ck] |= CTRL_FREE
b0f30 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 ;. mem5.aCtrl[i
b0f40 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 7c 3d Block+size-1] |=
b0f50 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 61 73 CTRL_FREE;. as
b0f60 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 sert( mem5.curre
b0f70 6e 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 20 61 ntCount>0 );. a
b0f80 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 ssert( mem5.curr
b0f90 65 6e 74 4f 75 74 3e 3d 28 73 69 7a 65 2a 6d 65 entOut>=(size*me
b0fa0 6d 35 2e 73 7a 41 74 6f 6d 29 20 29 3b 0a 20 20 m5.szAtom) );.
b0fb0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e mem5.currentCoun
b0fc0 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 t--;. mem5.curr
b0fd0 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a 6d entOut -= size*m
b0fe0 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20 20 61 73 em5.szAtom;. as
b0ff0 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 sert( mem5.curre
b1000 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e ntOut>0 || mem5.
b1010 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20 currentCount==0
b1020 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d );. assert( mem
b1030 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 5.currentCount>0
b1040 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 || mem5.current
b1050 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d Out==0 );.. mem
b1060 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
b1070 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c = CTRL_FREE | iL
b1080 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c 65 28 ogsize;. while(
b1090 20 41 4c 57 41 59 53 28 69 4c 6f 67 73 69 7a 65 ALWAYS(iLogsize
b10a0 3c 4c 4f 47 4d 41 58 29 20 29 7b 0a 20 20 20 20 <LOGMAX) ){.
b10b0 69 6e 74 20 69 42 75 64 64 79 3b 0a 20 20 20 20 int iBuddy;.
b10c0 69 66 28 20 28 69 42 6c 6f 63 6b 3e 3e 69 4c 6f if( (iBlock>>iLo
b10d0 67 73 69 7a 65 29 20 26 20 31 20 29 7b 0a 20 20 gsize) & 1 ){.
b10e0 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 42 6c iBuddy = iBl
b10f0 6f 63 6b 20 2d 20 73 69 7a 65 3b 0a 20 20 20 20 ock - size;.
b1100 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 42 75 }else{. iBu
b1110 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2b 20 73 ddy = iBlock + s
b1120 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 ize;. }. a
b1130 73 73 65 72 74 28 20 69 42 75 64 64 79 3e 3d 30 ssert( iBuddy>=0
b1140 20 29 3b 0a 20 20 20 20 69 66 28 20 28 69 42 75 );. if( (iBu
b1150 64 64 79 2b 28 31 3c 3c 69 4c 6f 67 73 69 7a 65 ddy+(1<<iLogsize
b1160 29 29 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 ))>mem5.nBlock )
b1170 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 break;. if(
b1180 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 mem5.aCtrl[iBudd
b1190 79 5d 21 3d 28 43 54 52 4c 5f 46 52 45 45 20 7c y]!=(CTRL_FREE |
b11a0 20 69 4c 6f 67 73 69 7a 65 29 20 29 20 62 72 65 iLogsize) ) bre
b11b0 61 6b 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 55 ak;. memsys5U
b11c0 6e 6c 69 6e 6b 28 69 42 75 64 64 79 2c 20 69 4c nlink(iBuddy, iL
b11d0 6f 67 73 69 7a 65 29 3b 0a 20 20 20 20 69 4c 6f ogsize);. iLo
b11e0 67 73 69 7a 65 2b 2b 3b 0a 20 20 20 20 69 66 28 gsize++;. if(
b11f0 20 69 42 75 64 64 79 3c 69 42 6c 6f 63 6b 20 29 iBuddy<iBlock )
b1200 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 {. mem5.aCt
b1210 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 43 54 52 rl[iBuddy] = CTR
b1220 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a L_FREE | iLogsiz
b1230 65 3b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 e;. mem5.aC
b1240 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 30 3b trl[iBlock] = 0;
b1250 0a 20 20 20 20 20 20 69 42 6c 6f 63 6b 20 3d 20 . iBlock =
b1260 69 42 75 64 64 79 3b 0a 20 20 20 20 7d 65 6c 73 iBuddy;. }els
b1270 65 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 e{. mem5.aC
b1280 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 trl[iBlock] = CT
b1290 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 RL_FREE | iLogsi
b12a0 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 ze;. mem5.a
b12b0 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 30 Ctrl[iBuddy] = 0
b12c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 69 7a 65 ;. }. size
b12d0 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20 20 6d 65 6d *= 2;. }. mem
b12e0 73 79 73 35 4c 69 6e 6b 28 69 42 6c 6f 63 6b 2c sys5Link(iBlock,
b12f0 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 7d 0a 0a 2f iLogsize);.}../
b1300 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 *.** Allocate nB
b1310 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a ytes of memory.*
b1320 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d /.static void *m
b1330 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 28 69 6e 74 emsys5Malloc(int
b1340 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c 69 nBytes){. sqli
b1350 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 30 te3_int64 *p = 0
b1360 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3e 30 ;. if( nBytes>0
b1370 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 35 45 ){. memsys5E
b1380 6e 74 65 72 28 29 3b 0a 20 20 20 20 70 20 3d 20 nter();. p =
b1390 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 memsys5MallocUns
b13a0 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 afe(nBytes);.
b13b0 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b memsys5Leave();
b13c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 . }. return (v
b13d0 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a oid*)p; .}../*.*
b13e0 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a * Free memory..*
b13f0 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 65 72 20 6c *.** The outer l
b1400 61 79 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ayer memory allo
b1410 63 61 74 6f 72 20 70 72 65 76 65 6e 74 73 20 74 cator prevents t
b1420 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d his routine from
b1430 0a 2a 2a 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 .** being called
b1440 20 77 69 74 68 20 70 50 72 69 6f 72 3d 3d 30 2e with pPrior==0.
b1450 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
b1460 6d 65 6d 73 79 73 35 46 72 65 65 28 76 6f 69 64 memsys5Free(void
b1470 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73 *pPrior){. ass
b1480 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29 ert( pPrior!=0 )
b1490 3b 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 ;. memsys5Enter
b14a0 28 29 3b 0a 20 20 6d 65 6d 73 79 73 35 46 72 65 ();. memsys5Fre
b14b0 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b eUnsafe(pPrior);
b14c0 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 . memsys5Leave(
b14d0 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 ); .}../*.** Ch
b14e0 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 ange the size of
b14f0 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d an existing mem
b1500 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a ory allocation..
b1510 2a 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 65 72 20 **.** The outer
b1520 6c 61 79 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c layer memory all
b1530 6f 63 61 74 6f 72 20 70 72 65 76 65 6e 74 73 20 ocator prevents
b1540 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f this routine fro
b1550 6d 0a 2a 2a 20 62 65 69 6e 67 20 63 61 6c 6c 65 m.** being calle
b1560 64 20 77 69 74 68 20 70 50 72 69 6f 72 3d 3d 30 d with pPrior==0
b1570 2e 20 20 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 65 73 . .**.** nBytes
b1580 20 69 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c is always a val
b1590 75 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ue obtained from
b15a0 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f a prior call to
b15b0 0a 2a 2a 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64 .** memsys5Round
b15c0 28 29 2e 20 20 48 65 6e 63 65 20 6e 42 79 74 65 (). Hence nByte
b15d0 73 20 69 73 20 61 6c 77 61 79 73 20 61 20 6e 6f s is always a no
b15e0 6e 2d 6e 65 67 61 74 69 76 65 20 70 6f 77 65 72 n-negative power
b15f0 0a 2a 2a 20 6f 66 20 74 77 6f 2e 20 20 49 66 20 .** of two. If
b1600 6e 42 79 74 65 73 3d 3d 30 20 74 68 61 74 20 6d nBytes==0 that m
b1610 65 61 6e 73 20 74 68 61 74 20 61 6e 20 6f 76 65 eans that an ove
b1620 72 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e rsize allocation
b1630 0a 2a 2a 20 28 61 6e 20 61 6c 6c 6f 63 61 74 69 .** (an allocati
b1640 6f 6e 20 6c 61 72 67 65 72 20 74 68 61 6e 20 30 on larger than 0
b1650 78 34 30 30 30 30 30 30 30 29 20 77 61 73 20 72 x40000000) was r
b1660 65 71 75 65 73 74 65 64 20 61 6e 64 20 74 68 69 equested and thi
b1670 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 73 68 6f s.** routine sho
b1680 75 6c 64 20 72 65 74 75 72 6e 20 30 20 77 69 74 uld return 0 wit
b1690 68 6f 75 74 20 66 72 65 65 69 6e 67 20 70 50 72 hout freeing pPr
b16a0 69 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ior..*/.static v
b16b0 6f 69 64 20 2a 6d 65 6d 73 79 73 35 52 65 61 6c oid *memsys5Real
b16c0 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 loc(void *pPrior
b16d0 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 , int nBytes){.
b16e0 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 int nOld;. voi
b16f0 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 d *p;. assert(
b1700 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 61 pPrior!=0 );. a
b1710 73 73 65 72 74 28 20 28 6e 42 79 74 65 73 26 28 ssert( (nBytes&(
b1720 6e 42 79 74 65 73 2d 31 29 29 3d 3d 30 20 29 3b nBytes-1))==0 );
b1730 20 20 2f 2a 20 45 56 3a 20 52 2d 34 36 31 39 39 /* EV: R-46199
b1740 2d 33 30 32 34 39 20 2a 2f 0a 20 20 61 73 73 65 -30249 */. asse
b1750 72 74 28 20 6e 42 79 74 65 73 3e 3d 30 20 29 3b rt( nBytes>=0 );
b1760 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3d 3d 30 . if( nBytes==0
b1770 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
b1780 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d ;. }. nOld = m
b1790 65 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f emsys5Size(pPrio
b17a0 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 r);. if( nBytes
b17b0 3c 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 <=nOld ){. re
b17c0 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d turn pPrior;. }
b17d0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 . memsys5Enter(
b17e0 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 );. p = memsys5
b17f0 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 MallocUnsafe(nBy
b1800 74 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b tes);. if( p ){
b1810 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 . memcpy(p, p
b1820 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 Prior, nOld);.
b1830 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 memsys5FreeUns
b1840 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d afe(pPrior);. }
b1850 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 . memsys5Leave(
b1860 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d );. return p;.}
b1870 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 ../*.** Round up
b1880 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 a request size
b1890 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 to the next vali
b18a0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a d allocation siz
b18b0 65 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 61 6c e. If.** the al
b18c0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 74 6f 6f 20 location is too
b18d0 6c 61 72 67 65 20 74 6f 20 62 65 20 68 61 6e 64 large to be hand
b18e0 6c 65 64 20 62 79 20 74 68 69 73 20 61 6c 6c 6f led by this allo
b18f0 63 61 74 69 6f 6e 20 73 79 73 74 65 6d 2c 0a 2a cation system,.*
b1900 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a * return 0..**.*
b1910 2a 20 41 6c 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e * All allocation
b1920 73 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 s must be a powe
b1930 72 20 6f 66 20 74 77 6f 20 61 6e 64 20 6d 75 73 r of two and mus
b1940 74 20 62 65 20 65 78 70 72 65 73 73 65 64 20 62 t be expressed b
b1950 79 20 61 0a 2a 2a 20 33 32 2d 62 69 74 20 73 69 y a.** 32-bit si
b1960 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 48 gned integer. H
b1970 65 6e 63 65 20 74 68 65 20 6c 61 72 67 65 73 74 ence the largest
b1980 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 30 allocation is 0
b1990 78 34 30 30 30 30 30 30 30 0a 2a 2a 20 6f 72 20 x40000000.** or
b19a0 31 30 37 33 37 34 31 38 32 34 20 62 79 74 65 73 1073741824 bytes
b19b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
b19c0 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69 memsys5Roundup(i
b19d0 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75 nt n){. int iFu
b19e0 6c 6c 53 7a 3b 0a 20 20 69 66 28 20 6e 20 3e 20 llSz;. if( n >
b19f0 30 78 34 30 30 30 30 30 30 30 20 29 20 72 65 74 0x40000000 ) ret
b1a00 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 46 75 urn 0;. for(iFu
b1a10 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d llSz=mem5.szAtom
b1a20 3b 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 ; iFullSz<n; iFu
b1a30 6c 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 llSz *= 2);. re
b1a40 74 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a turn iFullSz;.}.
b1a50 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
b1a60 65 20 63 65 69 6c 69 6e 67 20 6f 66 20 74 68 65 e ceiling of the
b1a70 20 6c 6f 67 61 72 69 74 68 6d 20 62 61 73 65 20 logarithm base
b1a80 32 20 6f 66 20 69 56 61 6c 75 65 2e 0a 2a 2a 0a 2 of iValue..**.
b1a90 2a 2a 20 45 78 61 6d 70 6c 65 73 3a 20 20 20 6d ** Examples: m
b1aa0 65 6d 73 79 73 35 4c 6f 67 28 31 29 20 2d 3e 20 emsys5Log(1) ->
b1ab0 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 0.**
b1ac0 20 6d 65 6d 73 79 73 35 4c 6f 67 28 32 29 20 2d memsys5Log(2) -
b1ad0 3e 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 > 1.**
b1ae0 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 34 29 memsys5Log(4)
b1af0 20 2d 3e 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 -> 2.**
b1b00 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 memsys5Log(
b1b10 35 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 20 5) -> 3.**
b1b20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f memsys5Lo
b1b30 67 28 38 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 g(8) -> 3.**
b1b40 20 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 memsys5
b1b50 4c 6f 67 28 39 29 20 2d 3e 20 34 0a 2a 2f 0a 73 Log(9) -> 4.*/.s
b1b60 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 tatic int memsys
b1b70 35 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29 5Log(int iValue)
b1b80 7b 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 {. int iLog;.
b1b90 66 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 69 4c 6f for(iLog=0; (iLo
b1ba0 67 3c 28 69 6e 74 29 28 28 73 69 7a 65 6f 66 28 g<(int)((sizeof(
b1bb0 69 6e 74 29 2a 38 29 2d 31 29 29 20 26 26 20 28 int)*8)-1)) && (
b1bc0 31 3c 3c 69 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 1<<iLog)<iValue;
b1bd0 20 69 4c 6f 67 2b 2b 29 3b 0a 20 20 72 65 74 75 iLog++);. retu
b1be0 72 6e 20 69 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a rn iLog;.}../*.*
b1bf0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
b1c00 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
b1c10 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f r..**.** This ro
b1c20 75 74 69 6e 65 20 69 73 20 6e 6f 74 20 74 68 72 utine is not thr
b1c30 65 61 64 73 61 66 65 2e 20 20 54 68 65 20 63 61 eadsafe. The ca
b1c40 6c 6c 65 72 20 6d 75 73 74 20 62 65 20 68 6f 6c ller must be hol
b1c50 64 69 6e 67 20 61 20 6d 75 74 65 78 0a 2a 2a 20 ding a mutex.**
b1c60 74 6f 20 70 72 65 76 65 6e 74 20 6d 75 6c 74 69 to prevent multi
b1c70 70 6c 65 20 74 68 72 65 61 64 73 20 66 72 6f 6d ple threads from
b1c80 20 65 6e 74 65 72 69 6e 67 20 61 74 20 74 68 65 entering at the
b1c90 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 same time..*/.s
b1ca0 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 tatic int memsys
b1cb0 35 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 5Init(void *NotU
b1cc0 73 65 64 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 sed){. int ii;
b1cd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
b1ce0 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
b1cf0 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 int nByte;
b1d00 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
b1d10 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 bytes of memory
b1d20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 69 available to thi
b1d30 73 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 s allocator */.
b1d40 20 75 38 20 2a 7a 42 79 74 65 3b 20 20 20 20 20 u8 *zByte;
b1d50 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 75 73 /* Memory us
b1d60 61 62 6c 65 20 62 79 20 74 68 69 73 20 61 6c 6c able by this all
b1d70 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 ocator */. int
b1d80 6e 4d 69 6e 4c 6f 67 3b 20 20 20 20 20 20 20 2f nMinLog; /
b1d90 2a 20 4c 6f 67 20 62 61 73 65 20 32 20 6f 66 20 * Log base 2 of
b1da0 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 minimum allocati
b1db0 6f 6e 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 on size in bytes
b1dc0 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 */. int iOffse
b1dd0 74 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f t; /* An o
b1de0 66 66 73 65 74 20 69 6e 74 6f 20 6d 65 6d 35 2e ffset into mem5.
b1df0 61 43 74 72 6c 5b 5d 20 2a 2f 0a 0a 20 20 55 4e aCtrl[] */.. UN
b1e00 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
b1e10 6f 74 55 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 46 otUsed);.. /* F
b1e20 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 or the purposes
b1e30 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c of this routine,
b1e40 20 64 69 73 61 62 6c 65 20 74 68 65 20 6d 75 74 disable the mut
b1e50 65 78 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6d 75 74 ex */. mem5.mut
b1e60 65 78 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 ex = 0;.. /* Th
b1e70 65 20 73 69 7a 65 20 6f 66 20 61 20 4d 65 6d 35 e size of a Mem5
b1e80 4c 69 6e 6b 20 6f 62 6a 65 63 74 20 6d 75 73 74 Link object must
b1e90 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 be a power of t
b1ea0 77 6f 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 wo. Verify that
b1eb0 0a 20 20 2a 2a 20 74 68 69 73 20 69 73 20 63 61 . ** this is ca
b1ec0 73 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 se.. */. asser
b1ed0 74 28 20 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c t( (sizeof(Mem5L
b1ee0 69 6e 6b 29 26 28 73 69 7a 65 6f 66 28 4d 65 6d ink)&(sizeof(Mem
b1ef0 35 4c 69 6e 6b 29 2d 31 29 29 3d 3d 30 20 29 3b 5Link)-1))==0 );
b1f00 0a 0a 20 20 6e 42 79 74 65 20 3d 20 73 71 6c 69 .. nByte = sqli
b1f10 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
b1f20 6e 48 65 61 70 3b 0a 20 20 7a 42 79 74 65 20 3d nHeap;. zByte =
b1f30 20 28 75 38 2a 29 73 71 6c 69 74 65 33 47 6c 6f (u8*)sqlite3Glo
b1f40 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b balConfig.pHeap;
b1f50 0a 20 20 61 73 73 65 72 74 28 20 7a 42 79 74 65 . assert( zByte
b1f60 21 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 !=0 ); /* sqlit
b1f70 65 33 5f 63 6f 6e 66 69 67 28 29 20 64 6f 65 73 e3_config() does
b1f80 20 6e 6f 74 20 61 6c 6c 6f 77 20 6f 74 68 65 72 not allow other
b1f90 77 69 73 65 20 2a 2f 0a 0a 20 20 2f 2a 20 62 6f wise */.. /* bo
b1fa0 75 6e 64 61 72 69 65 73 20 6f 6e 20 73 71 6c 69 undaries on sqli
b1fb0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
b1fc0 6d 6e 52 65 71 20 61 72 65 20 65 6e 66 6f 72 63 mnReq are enforc
b1fd0 65 64 20 69 6e 20 73 71 6c 69 74 65 33 5f 63 6f ed in sqlite3_co
b1fe0 6e 66 69 67 28 29 20 2a 2f 0a 20 20 6e 4d 69 6e nfig() */. nMin
b1ff0 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 Log = memsys5Log
b2000 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f (sqlite3GlobalCo
b2010 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a 20 20 6d nfig.mnReq);. m
b2020 65 6d 35 2e 73 7a 41 74 6f 6d 20 3d 20 28 31 3c em5.szAtom = (1<
b2030 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 77 68 69 <nMinLog);. whi
b2040 6c 65 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 le( (int)sizeof(
b2050 4d 65 6d 35 4c 69 6e 6b 29 3e 6d 65 6d 35 2e 73 Mem5Link)>mem5.s
b2060 7a 41 74 6f 6d 20 29 7b 0a 20 20 20 20 6d 65 6d zAtom ){. mem
b2070 35 2e 73 7a 41 74 6f 6d 20 3d 20 6d 65 6d 35 2e 5.szAtom = mem5.
b2080 73 7a 41 74 6f 6d 20 3c 3c 20 31 3b 0a 20 20 7d szAtom << 1;. }
b2090 0a 0a 20 20 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 .. mem5.nBlock
b20a0 3d 20 28 6e 42 79 74 65 20 2f 20 28 6d 65 6d 35 = (nByte / (mem5
b20b0 2e 73 7a 41 74 6f 6d 2b 73 69 7a 65 6f 66 28 75 .szAtom+sizeof(u
b20c0 38 29 29 29 3b 0a 20 20 6d 65 6d 35 2e 7a 50 6f 8)));. mem5.zPo
b20d0 6f 6c 20 3d 20 7a 42 79 74 65 3b 0a 20 20 6d 65 ol = zByte;. me
b20e0 6d 35 2e 61 43 74 72 6c 20 3d 20 28 75 38 20 2a m5.aCtrl = (u8 *
b20f0 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d )&mem5.zPool[mem
b2100 35 2e 6e 42 6c 6f 63 6b 2a 6d 65 6d 35 2e 73 7a 5.nBlock*mem5.sz
b2110 41 74 6f 6d 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 Atom];.. for(ii
b2120 3d 30 3b 20 69 69 3c 3d 4c 4f 47 4d 41 58 3b 20 =0; ii<=LOGMAX;
b2130 69 69 2b 2b 29 7b 0a 20 20 20 20 6d 65 6d 35 2e ii++){. mem5.
b2140 61 69 46 72 65 65 6c 69 73 74 5b 69 69 5d 20 3d aiFreelist[ii] =
b2150 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 69 4f 66 66 -1;. }.. iOff
b2160 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 set = 0;. for(i
b2170 69 3d 4c 4f 47 4d 41 58 3b 20 69 69 3e 3d 30 3b i=LOGMAX; ii>=0;
b2180 20 69 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 ii--){. int
b2190 6e 41 6c 6c 6f 63 20 3d 20 28 31 3c 3c 69 69 29 nAlloc = (1<<ii)
b21a0 3b 0a 20 20 20 20 69 66 28 20 28 69 4f 66 66 73 ;. if( (iOffs
b21b0 65 74 2b 6e 41 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 et+nAlloc)<=mem5
b21c0 2e 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 .nBlock ){.
b21d0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 4f 66 66 mem5.aCtrl[iOff
b21e0 73 65 74 5d 20 3d 20 69 69 20 7c 20 43 54 52 4c set] = ii | CTRL
b21f0 5f 46 52 45 45 3b 0a 20 20 20 20 20 20 6d 65 6d _FREE;. mem
b2200 73 79 73 35 4c 69 6e 6b 28 69 4f 66 66 73 65 74 sys5Link(iOffset
b2210 2c 20 69 69 29 3b 0a 20 20 20 20 20 20 69 4f 66 , ii);. iOf
b2220 66 73 65 74 20 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a fset += nAlloc;.
b2230 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
b2240 28 28 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 ((iOffset+nAlloc
b2250 29 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 29 3b 0a )>mem5.nBlock);.
b2260 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 6d }.. /* If a m
b2270 75 74 65 78 20 69 73 20 72 65 71 75 69 72 65 64 utex is required
b2280 20 66 6f 72 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 for normal oper
b2290 61 74 69 6f 6e 2c 20 61 6c 6c 6f 63 61 74 65 20 ation, allocate
b22a0 6f 6e 65 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c one */. if( sql
b22b0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
b22c0 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20 29 7b 0a .bMemstat==0 ){.
b22d0 20 20 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d mem5.mutex =
b22e0 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
b22f0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
b2300 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d STATIC_MEM);. }
b2310 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
b2320 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 E_OK;.}../*.** D
b2330 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 einitialize this
b2340 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 module..*/.stat
b2350 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 53 ic void memsys5S
b2360 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f hutdown(void *No
b2370 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 tUsed){. UNUSED
b2380 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
b2390 65 64 29 3b 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 ed);. mem5.mute
b23a0 78 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 3b x = 0;. return;
b23b0 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .}..#ifdef SQLIT
b23c0 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4f 70 65 E_TEST./*.** Ope
b23d0 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 n the file indic
b23e0 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 ated and write a
b23f0 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 log of all unfr
b2400 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 eed memory .** a
b2410 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 llocations into
b2420 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c that log..*/.SQL
b2430 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b2440 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 35 44 sqlite3Memsys5D
b2450 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ump(const char *
b2460 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 49 zFilename){. FI
b2470 4c 45 20 2a 6f 75 74 3b 0a 20 20 69 6e 74 20 69 LE *out;. int i
b2480 2c 20 6a 2c 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d , j, n;. int nM
b2490 69 6e 4c 6f 67 3b 0a 0a 20 20 69 66 28 20 7a 46 inLog;.. if( zF
b24a0 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 ilename==0 || zF
b24b0 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b ilename[0]==0 ){
b24c0 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75 . out = stdou
b24d0 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 t;. }else{.
b24e0 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c out = fopen(zFil
b24f0 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20 ename, "w");.
b2500 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 if( out==0 ){.
b2510 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
b2520 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 err, "** Unable
b2530 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 to output memory
b2540 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f debug output lo
b2550 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 g: %s **\n",.
b2560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b2570 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 zFilename);.
b2580 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
b2590 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 }. }. memsys5
b25a0 45 6e 74 65 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c Enter();. nMinL
b25b0 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 og = memsys5Log(
b25c0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 3b 0a 20 20 mem5.szAtom);.
b25d0 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 4c 4f 47 4d for(i=0; i<=LOGM
b25e0 41 58 20 26 26 20 69 2b 6e 4d 69 6e 4c 6f 67 3c AX && i+nMinLog<
b25f0 33 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 32; i++){. fo
b2600 72 28 6e 3d 30 2c 20 6a 3d 6d 65 6d 35 2e 61 69 r(n=0, j=mem5.ai
b2610 46 72 65 65 6c 69 73 74 5b 69 5d 3b 20 6a 3e 3d Freelist[i]; j>=
b2620 30 3b 20 6a 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 0; j = MEM5LINK(
b2630 6a 29 2d 3e 6e 65 78 74 2c 20 6e 2b 2b 29 7b 7d j)->next, n++){}
b2640 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 . fprintf(out
b2650 2c 20 22 66 72 65 65 6c 69 73 74 20 69 74 65 6d , "freelist item
b2660 73 20 6f 66 20 73 69 7a 65 20 25 64 3a 20 25 64 s of size %d: %d
b2670 5c 6e 22 2c 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d \n", mem5.szAtom
b2680 20 3c 3c 20 69 2c 20 6e 29 3b 0a 20 20 7d 0a 20 << i, n);. }.
b2690 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d fprintf(out, "m
b26a0 65 6d 35 2e 6e 41 6c 6c 6f 63 20 20 20 20 20 20 em5.nAlloc
b26b0 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 = %llu\n", mem5
b26c0 2e 6e 41 6c 6c 6f 63 29 3b 0a 20 20 66 70 72 69 .nAlloc);. fpri
b26d0 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 74 ntf(out, "mem5.t
b26e0 6f 74 61 6c 41 6c 6c 6f 63 20 20 20 3d 20 25 6c otalAlloc = %l
b26f0 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 74 6f 74 61 lu\n", mem5.tota
b2700 6c 41 6c 6c 6f 63 29 3b 0a 20 20 66 70 72 69 6e lAlloc);. fprin
b2710 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 74 6f tf(out, "mem5.to
b2720 74 61 6c 45 78 63 65 73 73 20 20 3d 20 25 6c 6c talExcess = %ll
b2730 75 5c 6e 22 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c u\n", mem5.total
b2740 45 78 63 65 73 73 29 3b 0a 20 20 66 70 72 69 6e Excess);. fprin
b2750 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 63 75 tf(out, "mem5.cu
b2760 72 72 65 6e 74 4f 75 74 20 20 20 3d 20 25 75 5c rrentOut = %u\
b2770 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 n", mem5.current
b2780 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 Out);. fprintf(
b2790 6f 75 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 out, "mem5.curre
b27a0 6e 74 43 6f 75 6e 74 20 3d 20 25 75 5c 6e 22 2c ntCount = %u\n",
b27b0 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 mem5.currentCou
b27c0 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f nt);. fprintf(o
b27d0 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 4f 75 74 ut, "mem5.maxOut
b27e0 20 20 20 20 20 20 20 3d 20 25 75 5c 6e 22 2c 20 = %u\n",
b27f0 6d 65 6d 35 2e 6d 61 78 4f 75 74 29 3b 0a 20 20 mem5.maxOut);.
b2800 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 fprintf(out, "me
b2810 6d 35 2e 6d 61 78 43 6f 75 6e 74 20 20 20 20 20 m5.maxCount
b2820 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 = %u\n", mem5.ma
b2830 78 43 6f 75 6e 74 29 3b 0a 20 20 66 70 72 69 6e xCount);. fprin
b2840 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 tf(out, "mem5.ma
b2850 78 52 65 71 75 65 73 74 20 20 20 3d 20 25 75 5c xRequest = %u\
b2860 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 52 65 71 75 n", mem5.maxRequ
b2870 65 73 74 29 3b 0a 20 20 6d 65 6d 73 79 73 35 4c est);. memsys5L
b2880 65 61 76 65 28 29 3b 0a 20 20 69 66 28 20 6f 75 eave();. if( ou
b2890 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a 20 20 20 t==stdout ){.
b28a0 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b fflush(stdout);
b28b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 63 . }else{. fc
b28c0 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 7d 0a 7d lose(out);. }.}
b28d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
b28e0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 his routine is t
b28f0 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 he only routine
b2900 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 in this file wit
b2910 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c h external .** l
b2920 69 6e 6b 61 67 65 2e 20 49 74 20 72 65 74 75 72 inkage. It retur
b2930 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
b2940 61 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 a static sqlite3
b2950 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 _mem_methods.**
b2960 73 74 72 75 63 74 20 70 6f 70 75 6c 61 74 65 64 struct populated
b2970 20 77 69 74 68 20 74 68 65 20 6d 65 6d 73 79 73 with the memsys
b2980 35 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 5 methods..*/.SQ
b2990 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
b29a0 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d st sqlite3_mem_m
b29b0 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d ethods *sqlite3M
b29c0 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f 69 emGetMemsys5(voi
b29d0 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e d){. static con
b29e0 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d st sqlite3_mem_m
b29f0 65 74 68 6f 64 73 20 6d 65 6d 73 79 73 35 4d 65 ethods memsys5Me
b2a00 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d thods = {. m
b2a10 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c 0a 20 20 emsys5Malloc,.
b2a20 20 20 20 6d 65 6d 73 79 73 35 46 72 65 65 2c 0a memsys5Free,.
b2a30 20 20 20 20 20 6d 65 6d 73 79 73 35 52 65 61 6c memsys5Real
b2a40 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 loc,. memsys
b2a50 35 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73 5Size,. mems
b2a60 79 73 35 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 ys5Roundup,.
b2a70 20 6d 65 6d 73 79 73 35 49 6e 69 74 2c 0a 20 20 memsys5Init,.
b2a80 20 20 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f memsys5Shutdo
b2a90 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a wn,. 0. };.
b2aa0 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 73 79 73 return &memsys
b2ab0 35 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 5Methods;.}..#en
b2ac0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e dif /* SQLITE_EN
b2ad0 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 2a 2f 0a ABLE_MEMSYS5 */.
b2ae0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
b2af0 20 45 6e 64 20 6f 66 20 6d 65 6d 35 2e 63 20 2a End of mem5.c *
b2b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
b2b30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
b2b40 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 Begin file mute
b2b50 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a x.c ************
b2b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
b2b80 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
b2b90 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 14.**.** The
b2ba0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
b2bb0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
b2bc0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
b2bd0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
b2be0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
b2bf0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
b2c00 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
b2c10 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
b2c20 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
b2c30 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
b2c40 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
b2c50 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
b2c60 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
b2c70 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
b2c80 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
b2c90 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
b2ca0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
b2cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b2cf0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
b2d00 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 e contains the C
b2d10 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
b2d20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 implement mutexe
b2d30 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 s..**.** This fi
b2d40 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 le contains code
b2d50 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 that is common
b2d60 61 63 72 6f 73 73 20 61 6c 6c 20 6d 75 74 65 78 across all mutex
b2d70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
b2d80 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 ..*/..#if define
b2d90 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 d(SQLITE_DEBUG)
b2da0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
b2db0 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 0a 2f TE_MUTEX_OMIT)./
b2dc0 2a 0a 2a 2a 20 46 6f 72 20 64 65 62 75 67 67 69 *.** For debuggi
b2dd0 6e 67 20 70 75 72 70 6f 73 65 73 2c 20 72 65 63 ng purposes, rec
b2de0 6f 72 64 20 77 68 65 6e 20 74 68 65 20 6d 75 74 ord when the mut
b2df0 65 78 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 ex subsystem is
b2e00 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 61 initialized.** a
b2e10 6e 64 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 nd uninitialized
b2e20 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 so that we can
b2e30 61 73 73 65 72 74 28 29 20 69 66 20 74 68 65 72 assert() if ther
b2e40 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 e is an attempt
b2e50 74 6f 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 61 to.** allocate a
b2e60 20 6d 75 74 65 78 20 77 68 69 6c 65 20 74 68 65 mutex while the
b2e70 20 73 79 73 74 65 6d 20 69 73 20 75 6e 69 6e 69 system is unini
b2e80 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 73 74 61 tialized..*/.sta
b2e90 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 69 tic SQLITE_WSD i
b2ea0 6e 74 20 6d 75 74 65 78 49 73 49 6e 69 74 20 3d nt mutexIsInit =
b2eb0 20 30 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 0;.#endif /* SQ
b2ec0 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 0a LITE_DEBUG */...
b2ed0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
b2ee0 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 UTEX_OMIT./*.**
b2ef0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d Initialize the m
b2f00 75 74 65 78 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a utex system..*/.
b2f10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b2f20 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 nt sqlite3MutexI
b2f30 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 69 6e nit(void){ . in
b2f40 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
b2f50 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 ;. if( !sqlite3
b2f60 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
b2f70 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29 ex.xMutexAlloc )
b2f80 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 {. /* If the
b2f90 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d 65 74 68 xMutexAlloc meth
b2fa0 6f 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 od has not been
b2fb0 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 75 73 set, then the us
b2fc0 65 72 20 64 69 64 20 6e 6f 74 0a 20 20 20 20 2a er did not. *
b2fd0 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6d 75 74 65 * install a mute
b2fe0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
b2ff0 20 76 69 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e via sqlite3_con
b3000 66 69 67 28 29 20 70 72 69 6f 72 20 74 6f 20 0a fig() prior to .
b3010 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 69 ** sqlite3_i
b3020 6e 69 74 69 61 6c 69 7a 65 28 29 20 62 65 69 6e nitialize() bein
b3030 67 20 63 61 6c 6c 65 64 2e 20 54 68 69 73 20 62 g called. This b
b3040 6c 6f 63 6b 20 63 6f 70 69 65 73 20 70 6f 69 6e lock copies poin
b3050 74 65 72 73 20 74 6f 0a 20 20 20 20 2a 2a 20 74 ters to. ** t
b3060 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 he default imple
b3070 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 mentation into t
b3080 68 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c he sqlite3Global
b3090 43 6f 6e 66 69 67 20 73 74 72 75 63 74 75 72 65 Config structure
b30a0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c .. */. sql
b30b0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
b30c0 64 73 20 63 6f 6e 73 74 20 2a 70 46 72 6f 6d 3b ds const *pFrom;
b30d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
b30e0 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70 54 6f 20 ex_methods *pTo
b30f0 3d 20 26 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = &sqlite3Global
b3100 43 6f 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 0a 20 Config.mutex;..
b3110 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c if( sqlite3Gl
b3120 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 obalConfig.bCore
b3130 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 20 20 70 Mutex ){. p
b3140 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 44 65 From = sqlite3De
b3150 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a 20 20 faultMutex();.
b3160 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
b3170 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 4e 6f From = sqlite3No
b3180 6f 70 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 7d opMutex();. }
b3190 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c . memcpy(pTo,
b31a0 20 70 46 72 6f 6d 2c 20 6f 66 66 73 65 74 6f 66 pFrom, offsetof
b31b0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d (sqlite3_mutex_m
b31c0 65 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 41 6c ethods, xMutexAl
b31d0 6c 6f 63 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70 loc));. memcp
b31e0 79 28 26 70 54 6f 2d 3e 78 4d 75 74 65 78 46 72 y(&pTo->xMutexFr
b31f0 65 65 2c 20 26 70 46 72 6f 6d 2d 3e 78 4d 75 74 ee, &pFrom->xMut
b3200 65 78 46 72 65 65 2c 0a 20 20 20 20 20 20 20 20 exFree,.
b3210 20 20 20 73 69 7a 65 6f 66 28 2a 70 54 6f 29 20 sizeof(*pTo)
b3220 2d 20 6f 66 66 73 65 74 6f 66 28 73 71 6c 69 74 - offsetof(sqlit
b3230 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
b3240 2c 20 78 4d 75 74 65 78 46 72 65 65 29 29 3b 0a , xMutexFree));.
b3250 20 20 20 20 70 54 6f 2d 3e 78 4d 75 74 65 78 41 pTo->xMutexA
b3260 6c 6c 6f 63 20 3d 20 70 46 72 6f 6d 2d 3e 78 4d lloc = pFrom->xM
b3270 75 74 65 78 41 6c 6c 6f 63 3b 0a 20 20 7d 0a 20 utexAlloc;. }.
b3280 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f rc = sqlite3Glo
b3290 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
b32a0 78 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a 0a 23 xMutexInit();..#
b32b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
b32c0 55 47 0a 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c UG. GLOBAL(int,
b32d0 20 6d 75 74 65 78 49 73 49 6e 69 74 29 20 3d 20 mutexIsInit) =
b32e0 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 1;.#endif.. ret
b32f0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
b3300 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6d 75 Shutdown the mu
b3310 74 65 78 20 73 79 73 74 65 6d 2e 20 54 68 69 73 tex system. This
b3320 20 63 61 6c 6c 20 66 72 65 65 73 20 72 65 73 6f call frees reso
b3330 75 72 63 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 urces allocated
b3340 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74 by.** sqlite3Mut
b3350 65 78 49 6e 69 74 28 29 2e 0a 2a 2f 0a 53 51 4c exInit()..*/.SQL
b3360 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b3370 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 sqlite3MutexEnd(
b3380 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 void){. int rc
b3390 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
b33a0 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c f( sqlite3Global
b33b0 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
b33c0 74 65 78 45 6e 64 20 29 7b 0a 20 20 20 20 72 63 texEnd ){. rc
b33d0 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
b33e0 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
b33f0 74 65 78 45 6e 64 28 29 3b 0a 20 20 7d 0a 0a 23 texEnd();. }..#
b3400 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
b3410 55 47 0a 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c UG. GLOBAL(int,
b3420 20 6d 75 74 65 78 49 73 49 6e 69 74 29 20 3d 20 mutexIsInit) =
b3430 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 0;.#endif.. ret
b3440 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
b3450 20 52 65 74 72 69 65 76 65 20 61 20 70 6f 69 6e Retrieve a poin
b3460 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 ter to a static
b3470 6d 75 74 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74 mutex or allocat
b3480 65 20 61 20 6e 65 77 20 64 79 6e 61 6d 69 63 20 e a new dynamic
b3490 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 one..*/.SQLITE_A
b34a0 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 PI sqlite3_mutex
b34b0 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f *sqlite3_mutex_
b34c0 61 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 23 alloc(int id){.#
b34d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
b34e0 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 IT_AUTOINIT. if
b34f0 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 ( sqlite3_initia
b3500 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 lize() ) return
b3510 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 0;.#endif. retu
b3520 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c rn sqlite3Global
b3530 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
b3540 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a texAlloc(id);.}.
b3550 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b3560 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 sqlite3_mutex *s
b3570 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
b3580 28 69 6e 74 20 69 64 29 7b 0a 20 20 69 66 28 20 (int id){. if(
b3590 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f !sqlite3GlobalCo
b35a0 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 nfig.bCoreMutex
b35b0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
b35c0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 47 . }. assert( G
b35d0 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 74 65 78 LOBAL(int, mutex
b35e0 49 73 49 6e 69 74 29 20 29 3b 0a 20 20 72 65 74 IsInit) );. ret
b35f0 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 urn sqlite3Globa
b3600 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d lConfig.mutex.xM
b3610 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d utexAlloc(id);.}
b3620 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 64 ../*.** Free a d
b3630 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e 0a 2a 2f ynamic mutex..*/
b3640 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
b3650 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 sqlite3_mutex_f
b3660 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ree(sqlite3_mute
b3670 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 x *p){. if( p )
b3680 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f {. sqlite3Glo
b3690 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
b36a0 78 4d 75 74 65 78 46 72 65 65 28 70 29 3b 0a 20 xMutexFree(p);.
b36b0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 }.}../*.** Obta
b36c0 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20 in the mutex p.
b36d0 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 If some other th
b36e0 72 65 61 64 20 61 6c 72 65 61 64 79 20 68 61 73 read already has
b36f0 20 74 68 65 20 6d 75 74 65 78 2c 20 62 6c 6f 63 the mutex, bloc
b3700 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 74 20 63 61 k.** until it ca
b3710 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a n be obtained..*
b3720 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
b3730 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
b3740 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 enter(sqlite3_mu
b3750 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 tex *p){. if( p
b3760 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 ){. sqlite3G
b3770 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
b3780 78 2e 78 4d 75 74 65 78 45 6e 74 65 72 28 70 29 x.xMutexEnter(p)
b3790 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f ;. }.}../*.** O
b37a0 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20 btain the mutex
b37b0 70 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c p. If successful
b37c0 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
b37d0 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 OK. Otherwise, i
b37e0 66 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 74 68 72 f another.** thr
b37f0 65 61 64 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 ead holds the mu
b3800 74 65 78 20 61 6e 64 20 69 74 20 63 61 6e 6e 6f tex and it canno
b3810 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 72 t be obtained, r
b3820 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 eturn SQLITE_BUS
b3830 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 Y..*/.SQLITE_API
b3840 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 int sqlite3_mut
b3850 65 78 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d ex_try(sqlite3_m
b3860 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 utex *p){. int
b3870 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
b3880 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 72 if( p ){. r
b3890 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f eturn sqlite3Glo
b38a0 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
b38b0 78 4d 75 74 65 78 54 72 79 28 70 29 3b 0a 20 20 xMutexTry(p);.
b38c0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
b38d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ../*.** The sqli
b38e0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
b38f0 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 ) routine exits
b3900 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 a mutex that was
b3910 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 65 previously.** e
b3920 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 ntered by the sa
b3930 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 me thread. The
b3940 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 behavior is unde
b3950 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 fined if the mut
b3960 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 ex .** is not cu
b3970 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e rrently entered.
b3980 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 If a NULL point
b3990 65 72 20 69 73 20 70 61 73 73 65 64 20 61 73 20 er is passed as
b39a0 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 an argument.** t
b39b0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
b39c0 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 a no-op..*/.SQLI
b39d0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
b39e0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
b39f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
b3a00 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 ){. if( p ){.
b3a10 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
b3a20 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 onfig.mutex.xMut
b3a30 65 78 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a exLeave(p);. }.
b3a40 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 }..#ifndef NDEBU
b3a50 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 G./*.** The sqli
b3a60 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 te3_mutex_held()
b3a70 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 and sqlite3_mut
b3a80 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 ex_notheld() rou
b3a90 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 tine are.** inte
b3aa0 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 nded for use ins
b3ab0 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ide assert() sta
b3ac0 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 tements..*/.SQLI
b3ad0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
b3ae0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 e3_mutex_held(sq
b3af0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
b3b00 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c . return p==0 |
b3b10 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 | sqlite3GlobalC
b3b20 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 onfig.mutex.xMut
b3b30 65 78 48 65 6c 64 28 70 29 3b 0a 7d 0a 53 51 4c exHeld(p);.}.SQL
b3b40 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
b3b50 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
b3b60 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 d(sqlite3_mutex
b3b70 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d *p){. return p=
b3b80 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f =0 || sqlite3Glo
b3b90 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
b3ba0 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 xMutexNotheld(p)
b3bb0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 ;.}.#endif..#end
b3bc0 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 if /* !defined(S
b3bd0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 QLITE_MUTEX_OMIT
b3be0 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ) */../*********
b3bf0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 ***** End of mut
b3c00 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ex.c ***********
b3c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3c30 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
b3c40 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
b3c50 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 2a 2a mutex_noop.c **
b3c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3c80 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 ****/./*.** 2008
b3c90 20 4f 63 74 6f 62 65 72 20 30 37 0a 2a 2a 0a 2a October 07.**.*
b3ca0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
b3cb0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
b3cc0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
b3cd0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
b3ce0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
b3cf0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
b3d00 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
b3d10 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
b3d20 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
b3d30 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
b3d40 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
b3d50 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
b3d60 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
b3d70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
b3d80 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
b3d90 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
b3da0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
b3db0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
b3dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b3df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
b3e00 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
b3e10 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 the C functions
b3e20 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 that implement
b3e30 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 mutexes..**.** T
b3e40 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
b3e50 6f 6e 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 on in this file
b3e60 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65 does not provide
b3e70 20 61 6e 79 20 6d 75 74 75 61 6c 0a 2a 2a 20 65 any mutual.** e
b3e80 78 63 6c 75 73 69 6f 6e 20 61 6e 64 20 69 73 20 xclusion and is
b3e90 74 68 75 73 20 73 75 69 74 61 62 6c 65 20 66 6f thus suitable fo
b3ea0 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 20 61 70 r use only in ap
b3eb0 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 plications.** th
b3ec0 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 69 6e at use SQLite in
b3ed0 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 a single thread
b3ee0 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 . The routines
b3ef0 64 65 66 69 6e 65 64 0a 2a 2a 20 68 65 72 65 20 defined.** here
b3f00 61 72 65 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 are place-holder
b3f10 73 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 s. Applications
b3f20 20 63 61 6e 20 73 75 62 73 74 69 74 75 74 65 20 can substitute
b3f30 77 6f 72 6b 69 6e 67 0a 2a 2a 20 6d 75 74 65 78 working.** mutex
b3f40 20 72 6f 75 74 69 6e 65 73 20 61 74 20 73 74 61 routines at sta
b3f50 72 74 2d 74 69 6d 65 20 75 73 69 6e 67 20 74 68 rt-time using th
b3f60 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 e.**.** sqli
b3f70 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 te3_config(SQLIT
b3f80 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 2c 2e E_CONFIG_MUTEX,.
b3f90 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69 6e 74 65 72 66 ..).**.** interf
b3fa0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 6f ace..**.** If co
b3fb0 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 mpiled with SQLI
b3fc0 54 45 5f 44 45 42 55 47 2c 20 74 68 65 6e 20 61 TE_DEBUG, then a
b3fd0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 67 69 63 20 dditional logic
b3fe0 69 73 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 74 is inserted.** t
b3ff0 68 61 74 20 64 6f 65 73 20 65 72 72 6f 72 20 63 hat does error c
b4000 68 65 63 6b 69 6e 67 20 6f 6e 20 6d 75 74 65 78 hecking on mutex
b4010 65 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 es to make sure
b4020 74 68 65 79 20 61 72 65 20 62 65 69 6e 67 0a 2a they are being.*
b4030 2a 20 63 61 6c 6c 65 64 20 63 6f 72 72 65 63 74 * called correct
b4040 6c 79 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 ly..*/..#ifndef
b4050 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 SQLITE_MUTEX_OMI
b4060 54 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 T..#ifndef SQLIT
b4070 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 53 74 E_DEBUG./*.** St
b4080 75 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 ub routines for
b4090 61 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 all mutex method
b40a0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
b40b0 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e utines provide n
b40c0 6f 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 o mutual exclusi
b40d0 6f 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63 on or error chec
b40e0 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 king..*/.static
b40f0 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 int noopMutexIni
b4100 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 t(void){ return
b4110 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 SQLITE_OK; }.sta
b4120 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 tic int noopMute
b4130 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 xEnd(void){ retu
b4140 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a rn SQLITE_OK; }.
b4150 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
b4160 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 78 41 utex *noopMutexA
b4170 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20 0a 20 lloc(int id){ .
b4180 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
b4190 52 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 R(id);. return
b41a0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 (sqlite3_mutex*)
b41b0 38 3b 20 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 8; .}.static voi
b41c0 64 20 6e 6f 6f 70 4d 75 74 65 78 46 72 65 65 28 d noopMutexFree(
b41d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
b41e0 29 7b 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 ){ UNUSED_PARAME
b41f0 54 45 52 28 70 29 3b 20 72 65 74 75 72 6e 3b 20 TER(p); return;
b4200 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f }.static void no
b4210 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c opMutexEnter(sql
b4220 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 ite3_mutex *p){
b4230 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
b4240 28 70 29 3b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 (p); return; }.s
b4250 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 tatic int noopMu
b4260 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d texTry(sqlite3_m
b4270 75 74 65 78 20 2a 70 29 7b 0a 20 20 55 4e 55 53 utex *p){. UNUS
b4280 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29 3b ED_PARAMETER(p);
b4290 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
b42a0 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f _OK;.}.static vo
b42b0 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76 id noopMutexLeav
b42c0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
b42d0 2a 70 29 7b 20 55 4e 55 53 45 44 5f 50 41 52 41 *p){ UNUSED_PARA
b42e0 4d 45 54 45 52 28 70 29 3b 20 72 65 74 75 72 6e METER(p); return
b42f0 3b 20 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ; }..SQLITE_PRIV
b4300 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ATE sqlite3_mute
b4310 78 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 x_methods const
b4320 2a 73 71 6c 69 74 65 33 4e 6f 6f 70 4d 75 74 65 *sqlite3NoopMute
b4330 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 x(void){. stati
b4340 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
b4350 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d mutex_methods sM
b4360 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 6e 6f 6f utex = {. noo
b4370 70 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 pMutexInit,.
b4380 6e 6f 6f 70 4d 75 74 65 78 45 6e 64 2c 0a 20 20 noopMutexEnd,.
b4390 20 20 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63 noopMutexAlloc
b43a0 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 46 ,. noopMutexF
b43b0 72 65 65 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 ree,. noopMut
b43c0 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 6e 6f 6f exEnter,. noo
b43d0 70 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 6e pMutexTry,. n
b43e0 6f 6f 70 4d 75 74 65 78 4c 65 61 76 65 2c 0a 0a oopMutexLeave,..
b43f0 20 20 20 20 30 2c 0a 20 20 20 20 30 2c 0a 20 20 0,. 0,.
b4400 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d };.. return &sM
b4410 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f utex;.}.#endif /
b4420 2a 20 21 53 51 4c 49 54 45 5f 44 45 42 55 47 20 * !SQLITE_DEBUG
b4430 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */..#ifdef SQLIT
b4440 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 49 6e E_DEBUG./*.** In
b4450 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 this implementa
b4460 74 69 6f 6e 2c 20 65 72 72 6f 72 20 63 68 65 63 tion, error chec
b4470 6b 69 6e 67 20 69 73 20 70 72 6f 76 69 64 65 64 king is provided
b4480 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 for testing.**
b4490 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 70 75 and debugging pu
b44a0 72 70 6f 73 65 73 2e 20 20 54 68 65 20 6d 75 74 rposes. The mut
b44b0 65 78 65 73 20 73 74 69 6c 6c 20 64 6f 20 6e 6f exes still do no
b44c0 74 20 70 72 6f 76 69 64 65 20 61 6e 79 0a 2a 2a t provide any.**
b44d0 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f mutual exclusio
b44e0 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 n..*/../*.** The
b44f0 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2f mutex object.*/
b4500 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
b4510 73 71 6c 69 74 65 33 5f 64 65 62 75 67 5f 6d 75 sqlite3_debug_mu
b4520 74 65 78 20 7b 0a 20 20 69 6e 74 20 69 64 3b 20 tex {. int id;
b4530 20 20 20 20 2f 2a 20 54 68 65 20 6d 75 74 65 78 /* The mutex
b4540 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 63 type */. int c
b4550 6e 74 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 nt; /* Number
b4560 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 of entries with
b4570 6f 75 74 20 61 20 6d 61 74 63 68 69 6e 67 20 6c out a matching l
b4580 65 61 76 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 eave */.} sqlite
b4590 33 5f 64 65 62 75 67 5f 6d 75 74 65 78 3b 0a 0a 3_debug_mutex;..
b45a0 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 /*.** The sqlite
b45b0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 3_mutex_held() a
b45c0 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nd sqlite3_mutex
b45d0 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 _notheld() routi
b45e0 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 ne are.** intend
b45f0 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 ed for use insid
b4600 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 e assert() state
b4610 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 ments..*/.static
b4620 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 48 int debugMutexH
b4630 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 eld(sqlite3_mute
b4640 78 20 2a 70 58 29 7b 0a 20 20 73 71 6c 69 74 65 x *pX){. sqlite
b4650 33 5f 64 65 62 75 67 5f 6d 75 74 65 78 20 2a 70 3_debug_mutex *p
b4660 20 3d 20 28 73 71 6c 69 74 65 33 5f 64 65 62 75 = (sqlite3_debu
b4670 67 5f 6d 75 74 65 78 2a 29 70 58 3b 0a 20 20 72 g_mutex*)pX;. r
b4680 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d eturn p==0 || p-
b4690 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74 69 63 >cnt>0;.}.static
b46a0 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 4e int debugMutexN
b46b0 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d otheld(sqlite3_m
b46c0 75 74 65 78 20 2a 70 58 29 7b 0a 20 20 73 71 6c utex *pX){. sql
b46d0 69 74 65 33 5f 64 65 62 75 67 5f 6d 75 74 65 78 ite3_debug_mutex
b46e0 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 64 *p = (sqlite3_d
b46f0 65 62 75 67 5f 6d 75 74 65 78 2a 29 70 58 3b 0a ebug_mutex*)pX;.
b4700 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c return p==0 ||
b4710 20 70 2d 3e 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f p->cnt==0;.}../
b4720 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
b4730 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 and deinitialize
b4740 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 the mutex subsy
b4750 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 stem..*/.static
b4760 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 49 6e int debugMutexIn
b4770 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e it(void){ return
b4780 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 SQLITE_OK; }.st
b4790 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 atic int debugMu
b47a0 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 texEnd(void){ re
b47b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
b47c0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
b47d0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
b47e0 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 () routine alloc
b47f0 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 ates a new.** mu
b4800 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 tex and returns
b4810 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e a pointer to it.
b4820 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 If it returns
b4830 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 NULL.** that mea
b4840 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 ns that a mutex
b4850 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c could not be all
b4860 6f 63 61 74 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 ocated. .*/.stat
b4870 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
b4880 20 2a 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f *debugMutexAllo
b4890 63 28 69 6e 74 20 69 64 29 7b 0a 20 20 73 74 61 c(int id){. sta
b48a0 74 69 63 20 73 71 6c 69 74 65 33 5f 64 65 62 75 tic sqlite3_debu
b48b0 67 5f 6d 75 74 65 78 20 61 53 74 61 74 69 63 5b g_mutex aStatic[
b48c0 36 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 64 65 6];. sqlite3_de
b48d0 62 75 67 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20 bug_mutex *pNew
b48e0 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69 = 0;. switch( i
b48f0 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 d ){. case SQ
b4900 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a LITE_MUTEX_FAST:
b4910 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
b4920 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
b4930 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d : {. pNew =
b4940 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 sqlite3Malloc(s
b4950 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 izeof(*pNew));.
b4960 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b if( pNew ){
b4970 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 . pNew->i
b4980 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20 d = id;.
b4990 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 pNew->cnt = 0;.
b49a0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
b49b0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
b49c0 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 fault: {. a
b49d0 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30 ssert( id-2 >= 0
b49e0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
b49f0 28 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 73 ( id-2 < (int)(s
b4a00 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73 izeof(aStatic)/s
b4a10 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d izeof(aStatic[0]
b4a20 29 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 )) );. pNew
b4a30 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d 32 = &aStatic[id-2
b4a40 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 ];. pNew->i
b4a50 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 72 d = id;. br
b4a60 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
b4a70 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 return (sqlite3
b4a80 5f 6d 75 74 65 78 2a 29 70 4e 65 77 3b 0a 7d 0a _mutex*)pNew;.}.
b4a90 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
b4aa0 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 ine deallocates
b4ab0 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c a previously all
b4ac0 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f ocated mutex..*/
b4ad0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 .static void deb
b4ae0 75 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 ugMutexFree(sqli
b4af0 74 65 33 5f 6d 75 74 65 78 20 2a 70 58 29 7b 0a te3_mutex *pX){.
b4b00 20 20 73 71 6c 69 74 65 33 5f 64 65 62 75 67 5f sqlite3_debug_
b4b10 6d 75 74 65 78 20 2a 70 20 3d 20 28 73 71 6c 69 mutex *p = (sqli
b4b20 74 65 33 5f 64 65 62 75 67 5f 6d 75 74 65 78 2a te3_debug_mutex*
b4b30 29 70 58 3b 0a 20 20 61 73 73 65 72 74 28 20 70 )pX;. assert( p
b4b40 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 ->cnt==0 );. as
b4b50 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
b4b60 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c ITE_MUTEX_FAST |
b4b70 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f | p->id==SQLITE_
b4b80 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
b4b90 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
b4ba0 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 e(p);.}../*.** T
b4bb0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
b4bc0 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c _enter() and sql
b4bd0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
b4be0 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 routines attemp
b4bf0 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 t.** to enter a
b4c00 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 mutex. If anoth
b4c10 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 er thread is alr
b4c20 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 eady within the
b4c30 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 mutex,.** sqlite
b4c40 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 3_mutex_enter()
b4c50 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 will block and s
b4c60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
b4c70 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a () will return.*
b4c80 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 * SQLITE_BUSY.
b4c90 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
b4ca0 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 x_try() interfac
b4cb0 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 e returns SQLITE
b4cc0 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 _OK.** upon succ
b4cd0 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d essful entry. M
b4ce0 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 utexes created u
b4cf0 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 sing SQLITE_MUTE
b4d00 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a X_RECURSIVE can.
b4d10 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 ** be entered mu
b4d20 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 ltiple times by
b4d30 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e the same thread.
b4d40 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 In such cases
b4d50 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 the,.** mutex mu
b4d60 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 st be exited an
b4d70 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 equal number of
b4d80 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f times before ano
b4d90 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 ther thread.** c
b4da0 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 an enter. If th
b4db0 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 e same thread tr
b4dc0 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 ies to enter any
b4dd0 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d other kind of m
b4de0 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 utex.** more tha
b4df0 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 n once, the beha
b4e00 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 vior is undefine
b4e10 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
b4e20 64 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65 d debugMutexEnte
b4e30 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 r(sqlite3_mutex
b4e40 2a 70 58 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f *pX){. sqlite3_
b4e50 64 65 62 75 67 5f 6d 75 74 65 78 20 2a 70 20 3d debug_mutex *p =
b4e60 20 28 73 71 6c 69 74 65 33 5f 64 65 62 75 67 5f (sqlite3_debug_
b4e70 6d 75 74 65 78 2a 29 70 58 3b 0a 20 20 61 73 73 mutex*)pX;. ass
b4e80 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
b4e90 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
b4ea0 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 VE || debugMutex
b4eb0 4e 6f 74 68 65 6c 64 28 70 58 29 20 29 3b 0a 20 Notheld(pX) );.
b4ec0 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 61 p->cnt++;.}.sta
b4ed0 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 tic int debugMut
b4ee0 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 exTry(sqlite3_mu
b4ef0 74 65 78 20 2a 70 58 29 7b 0a 20 20 73 71 6c 69 tex *pX){. sqli
b4f00 74 65 33 5f 64 65 62 75 67 5f 6d 75 74 65 78 20 te3_debug_mutex
b4f10 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 64 65 *p = (sqlite3_de
b4f20 62 75 67 5f 6d 75 74 65 78 2a 29 70 58 3b 0a 20 bug_mutex*)pX;.
b4f30 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d assert( p->id==
b4f40 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
b4f50 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d URSIVE || debugM
b4f60 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 58 29 20 utexNotheld(pX)
b4f70 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 );. p->cnt++;.
b4f80 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
b4f90 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 K;.}../*.** The
b4fa0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
b4fb0 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 ave() routine ex
b4fc0 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 its a mutex that
b4fd0 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 was.** previous
b4fe0 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 ly entered by th
b4ff0 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 e same thread.
b5000 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 The behavior.**
b5010 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 is undefined if
b5020 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 the mutex is not
b5030 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 currently enter
b5040 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 ed or.** is not
b5050 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 currently alloca
b5060 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c ted. SQLite wil
b5070 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 l never do eithe
b5080 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
b5090 64 20 64 65 62 75 67 4d 75 74 65 78 4c 65 61 76 d debugMutexLeav
b50a0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
b50b0 2a 70 58 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f *pX){. sqlite3_
b50c0 64 65 62 75 67 5f 6d 75 74 65 78 20 2a 70 20 3d debug_mutex *p =
b50d0 20 28 73 71 6c 69 74 65 33 5f 64 65 62 75 67 5f (sqlite3_debug_
b50e0 6d 75 74 65 78 2a 29 70 58 3b 0a 20 20 61 73 73 mutex*)pX;. ass
b50f0 65 72 74 28 20 64 65 62 75 67 4d 75 74 65 78 48 ert( debugMutexH
b5100 65 6c 64 28 70 58 29 20 29 3b 0a 20 20 70 2d 3e eld(pX) );. p->
b5110 63 6e 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 cnt--;. assert(
b5120 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
b5130 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c UTEX_RECURSIVE |
b5140 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 | debugMutexNoth
b5150 65 6c 64 28 70 58 29 20 29 3b 0a 7d 0a 0a 53 51 eld(pX) );.}..SQ
b5160 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
b5170 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
b5180 64 73 20 63 6f 6e 73 74 20 2a 73 71 6c 69 74 65 ds const *sqlite
b5190 33 4e 6f 6f 70 4d 75 74 65 78 28 76 6f 69 64 29 3NoopMutex(void)
b51a0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 {. static const
b51b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
b51c0 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 ethods sMutex =
b51d0 7b 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 {. debugMutex
b51e0 49 6e 69 74 2c 0a 20 20 20 20 64 65 62 75 67 4d Init,. debugM
b51f0 75 74 65 78 45 6e 64 2c 0a 20 20 20 20 64 65 62 utexEnd,. deb
b5200 75 67 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 ugMutexAlloc,.
b5210 20 20 64 65 62 75 67 4d 75 74 65 78 46 72 65 65 debugMutexFree
b5220 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 ,. debugMutex
b5230 45 6e 74 65 72 2c 0a 20 20 20 20 64 65 62 75 67 Enter,. debug
b5240 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 64 65 MutexTry,. de
b5250 62 75 67 4d 75 74 65 78 4c 65 61 76 65 2c 0a 0a bugMutexLeave,..
b5260 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 48 65 debugMutexHe
b5270 6c 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 ld,. debugMut
b5280 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a exNotheld. };..
b5290 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 return &sMutex
b52a0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
b52b0 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 2f LITE_DEBUG */../
b52c0 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 *.** If compiled
b52d0 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4d 55 54 with SQLITE_MUT
b52e0 45 58 5f 4e 4f 4f 50 2c 20 74 68 65 6e 20 74 68 EX_NOOP, then th
b52f0 65 20 6e 6f 2d 6f 70 20 6d 75 74 65 78 20 69 6d e no-op mutex im
b5300 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 plementation.**
b5310 69 73 20 75 73 65 64 20 72 65 67 61 72 64 6c 65 is used regardle
b5320 73 73 20 6f 66 20 74 68 65 20 72 75 6e 2d 74 69 ss of the run-ti
b5330 6d 65 20 74 68 72 65 61 64 73 61 66 65 74 79 20 me threadsafety
b5340 73 65 74 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 setting..*/.#ifd
b5350 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ef SQLITE_MUTEX_
b5360 4e 4f 4f 50 0a 53 51 4c 49 54 45 5f 50 52 49 56 NOOP.SQLITE_PRIV
b5370 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ATE sqlite3_mute
b5380 78 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 x_methods const
b5390 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d *sqlite3DefaultM
b53a0 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 72 65 utex(void){. re
b53b0 74 75 72 6e 20 73 71 6c 69 74 65 33 4e 6f 6f 70 turn sqlite3Noop
b53c0 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69 Mutex();.}.#endi
b53d0 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c f /* defined(SQL
b53e0 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 ITE_MUTEX_NOOP)
b53f0 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 */.#endif /* !de
b5400 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 fined(SQLITE_MUT
b5410 45 58 5f 4f 4d 49 54 29 20 2a 2f 0a 0a 2f 2a 2a EX_OMIT) */../**
b5420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
b5430 20 6f 66 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 of mutex_noop.c
b5440 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
b5450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
b5470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
b5480 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e in file mutex_un
b5490 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ix.c ***********
b54a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b54b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
b54c0 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 ** 2007 August 2
b54d0 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 8.**.** The auth
b54e0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
b54f0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
b5500 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
b5510 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
b5520 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
b5530 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
b5540 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
b5550 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
b5560 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
b5570 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
b5580 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
b5590 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
b55a0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
b55b0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
b55c0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
b55d0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
b55e0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
b55f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b5630 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
b5640 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e ntains the C fun
b5650 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c ctions that impl
b5660 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f ement mutexes fo
b5670 72 20 70 74 68 72 65 61 64 73 0a 2a 2f 0a 0a 2f r pthreads.*/../
b5680 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e *.** The code in
b5690 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e this file is on
b56a0 6c 79 20 75 73 65 64 20 69 66 20 77 65 20 61 72 ly used if we ar
b56b0 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 72 65 e compiling thre
b56c0 61 64 73 61 66 65 0a 2a 2a 20 75 6e 64 65 72 20 adsafe.** under
b56d0 75 6e 69 78 20 77 69 74 68 20 70 74 68 72 65 61 unix with pthrea
b56e0 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 ds..**.** Note t
b56f0 68 61 74 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 hat this impleme
b5700 6e 74 61 74 69 6f 6e 20 72 65 71 75 69 72 65 73 ntation requires
b5710 20 61 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 74 a version of pt
b5720 68 72 65 61 64 73 20 74 68 61 74 0a 2a 2a 20 73 hreads that.** s
b5730 75 70 70 6f 72 74 73 20 72 65 63 75 72 73 69 76 upports recursiv
b5740 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 69 e mutexes..*/.#i
b5750 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 fdef SQLITE_MUTE
b5760 58 5f 50 54 48 52 45 41 44 53 0a 0a 23 69 6e 63 X_PTHREADS..#inc
b5770 6c 75 64 65 20 3c 70 74 68 72 65 61 64 2e 68 3e lude <pthread.h>
b5780 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ../*.** The sqli
b5790 74 65 33 5f 6d 75 74 65 78 2e 69 64 2c 20 73 71 te3_mutex.id, sq
b57a0 6c 69 74 65 33 5f 6d 75 74 65 78 2e 6e 52 65 66 lite3_mutex.nRef
b57b0 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 , and sqlite3_mu
b57c0 74 65 78 2e 6f 77 6e 65 72 20 66 69 65 6c 64 73 tex.owner fields
b57d0 0a 2a 2a 20 61 72 65 20 6e 65 63 65 73 73 61 72 .** are necessar
b57e0 79 20 75 6e 64 65 72 20 74 77 6f 20 63 6f 6e 64 y under two cond
b57f0 69 64 74 69 6f 6e 73 3a 20 20 28 31 29 20 44 65 idtions: (1) De
b5800 62 75 67 20 62 75 69 6c 64 73 20 61 6e 64 20 28 bug builds and (
b5810 32 29 20 75 73 69 6e 67 0a 2a 2a 20 68 6f 6d 65 2) using.** home
b5820 2d 67 72 6f 77 6e 20 6d 75 74 65 78 65 73 2e 20 -grown mutexes.
b5830 20 45 6e 63 61 70 73 75 6c 61 74 65 20 74 68 65 Encapsulate the
b5840 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 6e se conditions in
b5850 74 6f 20 61 20 73 69 6e 67 6c 65 20 23 64 65 66 to a single #def
b5860 69 6e 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 ine..*/.#if defi
b5870 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
b5880 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
b5890 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 ITE_HOMEGROWN_RE
b58a0 43 55 52 53 49 56 45 5f 4d 55 54 45 58 29 0a 23 CURSIVE_MUTEX).#
b58b0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
b58c0 55 54 45 58 5f 4e 52 45 46 20 31 0a 23 65 6c 73 UTEX_NREF 1.#els
b58d0 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 e.# define SQLIT
b58e0 45 5f 4d 55 54 45 58 5f 4e 52 45 46 20 30 0a 23 E_MUTEX_NREF 0.#
b58f0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 endif../*.** Eac
b5900 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 h recursive mute
b5910 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 x is an instance
b5920 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
b5930 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a g structure..*/.
b5940 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d struct sqlite3_m
b5950 75 74 65 78 20 7b 0a 20 20 70 74 68 72 65 61 64 utex {. pthread
b5960 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65 78 3b 20 _mutex_t mutex;
b5970 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e /* Mutex con
b5980 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 trolling the loc
b5990 6b 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f k */.#if SQLITE_
b59a0 4d 55 54 45 58 5f 4e 52 45 46 0a 20 20 69 6e 74 MUTEX_NREF. int
b59b0 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 id;
b59c0 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 /* Mutex
b59d0 20 74 79 70 65 20 2a 2f 0a 20 20 76 6f 6c 61 74 type */. volat
b59e0 69 6c 65 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 ile int nRef;
b59f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
b5a00 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f 0a of entrances */.
b5a10 20 20 76 6f 6c 61 74 69 6c 65 20 70 74 68 72 65 volatile pthre
b5a20 61 64 5f 74 20 6f 77 6e 65 72 3b 20 20 2f 2a 20 ad_t owner; /*
b5a30 54 68 72 65 61 64 20 74 68 61 74 20 69 73 20 77 Thread that is w
b5a40 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 65 78 ithin this mutex
b5a50 20 2a 2f 0a 20 20 69 6e 74 20 74 72 61 63 65 3b */. int trace;
b5a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b5a70 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 /* True to trac
b5a80 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 65 6e e changes */.#en
b5a90 64 69 66 0a 7d 3b 0a 23 69 66 20 53 51 4c 49 54 dif.};.#if SQLIT
b5aa0 45 5f 4d 55 54 45 58 5f 4e 52 45 46 0a 23 64 65 E_MUTEX_NREF.#de
b5ab0 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 fine SQLITE3_MUT
b5ac0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b EX_INITIALIZER {
b5ad0 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 PTHREAD_MUTEX_I
b5ae0 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 NITIALIZER, 0, 0
b5af0 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 2c 20 , (pthread_t)0,
b5b00 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 0 }.#else.#defin
b5b10 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f e SQLITE3_MUTEX_
b5b20 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 INITIALIZER { PT
b5b30 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 HREAD_MUTEX_INIT
b5b40 49 41 4c 49 5a 45 52 20 7d 0a 23 65 6e 64 69 66 IALIZER }.#endif
b5b50 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ../*.** The sqli
b5b60 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 te3_mutex_held()
b5b70 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 and sqlite3_mut
b5b80 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 ex_notheld() rou
b5b90 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 tine are.** inte
b5ba0 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 6c nded for use onl
b5bb0 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 y inside assert(
b5bc0 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 4f ) statements. O
b5bd0 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d 73 n some platforms
b5be0 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 69 67 68 74 ,.** there might
b5bf0 20 62 65 20 72 61 63 65 20 63 6f 6e 64 69 74 69 be race conditi
b5c00 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 63 61 75 ons that can cau
b5c10 73 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 se these routine
b5c20 73 20 74 6f 0a 2a 2a 20 64 65 6c 69 76 65 72 20 s to.** deliver
b5c30 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c 74 incorrect result
b5c40 73 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c 61 s. In particula
b5c50 72 2c 20 69 66 20 70 74 68 72 65 61 64 5f 65 71 r, if pthread_eq
b5c60 75 61 6c 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 20 ual() is.** not
b5c70 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 an atomic operat
b5c80 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 73 65 20 ion, then these
b5c90 72 6f 75 74 69 6e 65 73 20 6d 69 67 68 74 20 64 routines might d
b5ca0 65 6c 69 76 65 72 79 0a 2a 2a 20 69 6e 63 6f 72 elivery.** incor
b5cb0 72 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20 4f rect results. O
b5cc0 6e 20 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d 73 n most platforms
b5cd0 2c 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 , pthread_equal(
b5ce0 29 20 69 73 20 61 20 0a 2a 2a 20 63 6f 6d 70 61 ) is a .** compa
b5cf0 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20 69 6e 74 rison of two int
b5d00 65 67 65 72 73 20 61 6e 64 20 69 73 20 74 68 65 egers and is the
b5d10 72 65 66 6f 72 65 20 61 74 6f 6d 69 63 2e 20 20 refore atomic.
b5d20 42 75 74 20 77 65 20 61 72 65 0a 2a 2a 20 74 6f But we are.** to
b5d30 6c 64 20 74 68 61 74 20 48 50 55 58 20 69 73 20 ld that HPUX is
b5d40 6e 6f 74 20 73 75 63 68 20 61 20 70 6c 61 74 66 not such a platf
b5d50 6f 72 6d 2e 20 20 49 66 20 73 6f 2c 20 74 68 65 orm. If so, the
b5d60 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 n these routines
b5d70 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 61 6c 77 .** will not alw
b5d80 61 79 73 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 ays work correct
b5d90 6c 79 20 6f 6e 20 48 50 55 58 2e 0a 2a 2a 0a 2a ly on HPUX..**.*
b5da0 2a 20 4f 6e 20 74 68 6f 73 65 20 70 6c 61 74 66 * On those platf
b5db0 6f 72 6d 73 20 77 68 65 72 65 20 70 74 68 72 65 orms where pthre
b5dc0 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 6e 6f ad_equal() is no
b5dd0 74 20 61 74 6f 6d 69 63 2c 20 53 51 4c 69 74 65 t atomic, SQLite
b5de0 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 6f .** should be co
b5df0 6d 70 69 6c 65 64 20 77 69 74 68 6f 75 74 20 2d mpiled without -
b5e00 44 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e DSQLITE_DEBUG an
b5e10 64 20 77 69 74 68 20 2d 44 4e 44 45 42 55 47 20 d with -DNDEBUG
b5e20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65 20 to.** make sure
b5e30 6e 6f 20 61 73 73 65 72 74 28 29 20 73 74 61 74 no assert() stat
b5e40 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 ements are evalu
b5e50 61 74 65 64 20 61 6e 64 20 68 65 6e 63 65 20 74 ated and hence t
b5e60 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 hese.** routines
b5e70 20 61 72 65 20 6e 65 76 65 72 20 63 61 6c 6c 65 are never calle
b5e80 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e d..*/.#if !defin
b5e90 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 ed(NDEBUG) || de
b5ea0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
b5eb0 55 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 70 UG).static int p
b5ec0 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28 threadMutexHeld(
b5ed0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
b5ee0 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e ){. return (p->
b5ef0 6e 52 65 66 21 3d 30 20 26 26 20 70 74 68 72 65 nRef!=0 && pthre
b5f00 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 ad_equal(p->owne
b5f10 72 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 r, pthread_self(
b5f20 29 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e )));.}.static in
b5f30 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f t pthreadMutexNo
b5f40 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 theld(sqlite3_mu
b5f50 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 tex *p){. retur
b5f60 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 n p->nRef==0 ||
b5f70 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d pthread_equal(p-
b5f80 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64 5f >owner, pthread_
b5f90 73 65 6c 66 28 29 29 3d 3d 30 3b 0a 7d 0a 23 65 self())==0;.}.#e
b5fa0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 ndif../*.** Init
b5fb0 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 ialize and deini
b5fc0 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 tialize the mute
b5fd0 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a x subsystem..*/.
b5fe0 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 static int pthre
b5ff0 61 64 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 adMutexInit(void
b6000 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ){ return SQLITE
b6010 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e _OK; }.static in
b6020 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e t pthreadMutexEn
b6030 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 d(void){ return
b6040 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a SQLITE_OK; }../*
b6050 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
b6060 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f mutex_alloc() ro
b6070 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 utine allocates
b6080 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 a new.** mutex a
b6090 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 nd returns a poi
b60a0 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 nter to it. If
b60b0 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a it returns NULL.
b60c0 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 ** that means th
b60d0 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 at a mutex could
b60e0 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 not be allocate
b60f0 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 d. SQLite.** wi
b6100 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 ll unwind its st
b6110 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 ack and return a
b6120 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72 n error. The ar
b6130 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c gument.** to sql
b6140 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
b6150 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 () is one of the
b6160 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 se integer const
b6170 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e ants:.**.** <ul>
b6180 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
b6190 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 3c _MUTEX_FAST.** <
b61a0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
b61b0 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 3c X_RECURSIVE.** <
b61c0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
b61d0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a X_STATIC_MASTER.
b61e0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
b61f0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d MUTEX_STATIC_MEM
b6200 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
b6210 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 _MUTEX_STATIC_ME
b6220 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 M2.** <li> SQLI
b6230 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
b6240 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 PRNG.** <li> SQ
b6250 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
b6260 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20 53 C_LRU.** <li> S
b6270 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
b6280 49 43 5f 50 4d 45 4d 0a 2a 2a 20 3c 2f 75 6c 3e IC_PMEM.** </ul>
b6290 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
b62a0 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 two constants c
b62b0 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 ause sqlite3_mut
b62c0 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 ex_alloc() to cr
b62d0 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 eate.** a new mu
b62e0 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 tex. The new mu
b62f0 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 65 tex is recursive
b6300 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 when SQLITE_MUT
b6310 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 EX_RECURSIVE.**
b6320 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 is used but not
b6330 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 necessarily so w
b6340 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 hen SQLITE_MUTEX
b6350 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a _FAST is used..*
b6360 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c * The mutex impl
b6370 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 ementation does
b6380 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 not need to make
b6390 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a a distinction.*
b63a0 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 * between SQLITE
b63b0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
b63c0 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 and SQLITE_MUTE
b63d0 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 X_FAST if it doe
b63e0 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f s.** not want to
b63f0 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 . But SQLite wi
b6400 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 ll only request
b6410 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 a recursive mute
b6420 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 x in.** cases wh
b6430 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 ere it really ne
b6440 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 eds one. If a f
b6450 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 aster non-recurs
b6460 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 ive mutex.** imp
b6470 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 lementation is a
b6480 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 vailable on the
b6490 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 host platform, t
b64a0 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 he mutex subsyst
b64b0 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 em.** might retu
b64c0 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 rn such a mutex
b64d0 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 in response to S
b64e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
b64f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65 ..**.** The othe
b6500 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 r allowed parame
b6510 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f ters to sqlite3_
b6520 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 mutex_alloc() ea
b6530 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 ch return.** a p
b6540 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 ointer to a stat
b6550 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d ic preexisting m
b6560 75 74 65 78 2e 20 20 53 69 78 20 73 74 61 74 69 utex. Six stati
b6570 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a c mutexes are.**
b6580 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 72 used by the cur
b6590 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 rent version of
b65a0 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 SQLite. Future
b65b0 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 versions of SQLi
b65c0 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 te.** may add ad
b65d0 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 ditional static
b65e0 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 mutexes. Static
b65f0 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 mutexes are for
b6600 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 internal.** use
b6610 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e by SQLite only.
b6620 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 Applications t
b6630 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d hat use SQLite m
b6640 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a utexes should.**
b6650 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 use only the dy
b6660 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 namic mutexes re
b6670 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 turned by SQLITE
b6680 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a _MUTEX_FAST or.*
b6690 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 * SQLITE_MUTEX_R
b66a0 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 ECURSIVE..**.**
b66b0 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 Note that if one
b66c0 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 of the dynamic
b66d0 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 mutex parameters
b66e0 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 (SQLITE_MUTEX_F
b66f0 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 AST.** or SQLITE
b6700 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
b6710 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 ) is used then s
b6720 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
b6730 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 oc().** returns
b6740 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 a different mute
b6750 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e x on every call.
b6760 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 But for the st
b6770 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 74 atic .** mutex t
b6780 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d ypes, the same m
b6790 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 utex is returned
b67a0 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74 on every call t
b67b0 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 hat has.** the s
b67c0 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e ame type number.
b67d0 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 .*/.static sqlit
b67e0 65 33 5f 6d 75 74 65 78 20 2a 70 74 68 72 65 61 e3_mutex *pthrea
b67f0 64 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 dMutexAlloc(int
b6800 69 54 79 70 65 29 7b 0a 20 20 73 74 61 74 69 63 iType){. static
b6810 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 sqlite3_mutex s
b6820 74 61 74 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d taticMutexes[] =
b6830 20 7b 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d {. SQLITE3_M
b6840 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
b6850 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 ,. SQLITE3_MU
b6860 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
b6870 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 . SQLITE3_MUT
b6880 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a EX_INITIALIZER,.
b6890 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 SQLITE3_MUTE
b68a0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 X_INITIALIZER,.
b68b0 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 SQLITE3_MUTEX
b68c0 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 _INITIALIZER,.
b68d0 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f SQLITE3_MUTEX_
b68e0 49 4e 49 54 49 41 4c 49 5a 45 52 0a 20 20 7d 3b INITIALIZER. };
b68f0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
b6900 20 2a 70 3b 0a 20 20 73 77 69 74 63 68 28 20 69 *p;. switch( i
b6910 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 Type ){. case
b6920 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
b6930 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 CURSIVE: {.
b6940 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c p = sqlite3Mall
b6950 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a ocZero( sizeof(*
b6960 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 p) );. if(
b6970 70 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 p ){.#ifdef SQLI
b6980 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 TE_HOMEGROWN_REC
b6990 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 20 URSIVE_MUTEX.
b69a0 20 20 20 20 20 2f 2a 20 49 66 20 72 65 63 75 72 /* If recur
b69b0 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 sive mutexes are
b69c0 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 not available,
b69d0 77 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 0a we will have to.
b69e0 20 20 20 20 20 20 20 20 2a 2a 20 62 75 69 6c 64 ** build
b69f0 20 6f 75 72 20 6f 77 6e 2e 20 20 53 65 65 20 62 our own. See b
b6a00 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 20 elow. */.
b6a10 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 pthread_mutex_i
b6a20 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 30 nit(&p->mutex, 0
b6a30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 );.#else.
b6a40 20 2f 2a 20 55 73 65 20 61 20 72 65 63 75 72 73 /* Use a recurs
b6a50 69 76 65 20 6d 75 74 65 78 20 69 66 20 69 74 20 ive mutex if it
b6a60 69 73 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a is available */.
b6a70 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f pthread_
b6a80 6d 75 74 65 78 61 74 74 72 5f 74 20 72 65 63 75 mutexattr_t recu
b6a90 72 73 69 76 65 41 74 74 72 3b 0a 20 20 20 20 20 rsiveAttr;.
b6aa0 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 pthread_mutex
b6ab0 61 74 74 72 5f 69 6e 69 74 28 26 72 65 63 75 72 attr_init(&recur
b6ac0 73 69 76 65 41 74 74 72 29 3b 0a 20 20 20 20 20 siveAttr);.
b6ad0 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 pthread_mutex
b6ae0 61 74 74 72 5f 73 65 74 74 79 70 65 28 26 72 65 attr_settype(&re
b6af0 63 75 72 73 69 76 65 41 74 74 72 2c 20 50 54 48 cursiveAttr, PTH
b6b00 52 45 41 44 5f 4d 55 54 45 58 5f 52 45 43 55 52 READ_MUTEX_RECUR
b6b10 53 49 56 45 29 3b 0a 20 20 20 20 20 20 20 20 70 SIVE);. p
b6b20 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 thread_mutex_ini
b6b30 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 26 72 65 t(&p->mutex, &re
b6b40 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 20 20 cursiveAttr);.
b6b50 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 pthread_mu
b6b60 74 65 78 61 74 74 72 5f 64 65 73 74 72 6f 79 28 texattr_destroy(
b6b70 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 3b &recursiveAttr);
b6b80 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 .#endif.#if SQLI
b6b90 54 45 5f 4d 55 54 45 58 5f 4e 52 45 46 0a 20 20 TE_MUTEX_NREF.
b6ba0 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 p->id = iT
b6bb0 79 70 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 ype;.#endif.
b6bc0 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
b6bd0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
b6be0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
b6bf0 54 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 T: {. p = s
b6c00 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
b6c10 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a ( sizeof(*p) );.
b6c20 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a 23 if( p ){.#
b6c30 69 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f if SQLITE_MUTEX_
b6c40 4e 52 45 46 0a 20 20 20 20 20 20 20 20 70 2d 3e NREF. p->
b6c50 69 64 20 3d 20 69 54 79 70 65 3b 0a 23 65 6e 64 id = iType;.#end
b6c60 69 66 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 if. pthre
b6c70 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 ad_mutex_init(&p
b6c80 2d 3e 6d 75 74 65 78 2c 20 30 29 3b 0a 20 20 20 ->mutex, 0);.
b6c90 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
b6ca0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
b6cb0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 ult: {. ass
b6cc0 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 ert( iType-2 >=
b6cd0 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 0 );. asser
b6ce0 74 28 20 69 54 79 70 65 2d 32 20 3c 20 41 72 72 t( iType-2 < Arr
b6cf0 61 79 53 69 7a 65 28 73 74 61 74 69 63 4d 75 74 aySize(staticMut
b6d00 65 78 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70 exes) );. p
b6d10 20 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 = &staticMutexe
b6d20 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 23 69 66 20 s[iType-2];.#if
b6d30 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 52 45 SQLITE_MUTEX_NRE
b6d40 46 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 F. p->id =
b6d50 69 54 79 70 65 3b 0a 23 65 6e 64 69 66 0a 20 20 iType;.#endif.
b6d60 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
b6d70 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
b6d80 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 .}.../*.** This
b6d90 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 routine dealloca
b6da0 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 tes a previously
b6db0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 .** allocated mu
b6dc0 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20 tex. SQLite is
b6dd0 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c careful to deall
b6de0 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d ocate every.** m
b6df0 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c utex that it all
b6e00 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 ocates..*/.stati
b6e10 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 c void pthreadMu
b6e20 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f texFree(sqlite3_
b6e30 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
b6e40 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 ert( p->nRef==0
b6e50 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
b6e60 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
b6e70 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d _FAST || p->id==
b6e80 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
b6e90 55 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68 72 URSIVE );. pthr
b6ea0 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72 6f ead_mutex_destro
b6eb0 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 y(&p->mutex);.
b6ec0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
b6ed0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 .}../*.** The sq
b6ee0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
b6ef0 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f r() and sqlite3_
b6f00 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 mutex_try() rout
b6f10 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 ines attempt.**
b6f20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 to enter a mutex
b6f30 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 . If another th
b6f40 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 read is already
b6f50 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 within the mutex
b6f60 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 ,.** sqlite3_mut
b6f70 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 ex_enter() will
b6f80 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 block and sqlite
b6f90 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 3_mutex_try() wi
b6fa0 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c ll return.** SQL
b6fb0 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 ITE_BUSY. The s
b6fc0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
b6fd0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 () interface ret
b6fe0 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a urns SQLITE_OK.*
b6ff0 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 * upon successfu
b7000 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 l entry. Mutexe
b7010 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 s created using
b7020 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
b7030 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 URSIVE can.** be
b7040 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c entered multipl
b7050 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 e times by the s
b7060 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 ame thread. In
b7070 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a such cases the,.
b7080 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 ** mutex must be
b7090 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c exited an equal
b70a0 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 number of times
b70b0 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 before another
b70c0 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e thread.** can en
b70d0 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d ter. If the sam
b70e0 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 e thread tries t
b70f0 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 o enter any othe
b7100 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a r kind of mutex.
b7110 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 ** more than onc
b7120 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 e, the behavior
b7130 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f is undefined..*/
b7140 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68 .static void pth
b7150 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28 73 readMutexEnter(s
b7160 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
b7170 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 {. assert( p->i
b7180 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
b7190 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68 RECURSIVE || pth
b71a0 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 readMutexNotheld
b71b0 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 (p) );..#ifdef S
b71c0 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f QLITE_HOMEGROWN_
b71d0 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a RECURSIVE_MUTEX.
b71e0 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76 /* If recursiv
b71f0 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f e mutexes are no
b7200 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 t available, the
b7210 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f n we have to gro
b7220 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 w. ** our own.
b7230 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 This implementa
b7240 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 tion assumes tha
b7250 74 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 t pthread_equal(
b7260 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 ). ** is atomic
b7270 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f - that it canno
b7280 74 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e t be deceived in
b7290 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 to thinking self
b72a0 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e . ** and p->own
b72b0 65 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20 er are equal if
b72c0 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 p->owner changes
b72d0 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c between two val
b72e0 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 ues. ** that ar
b72f0 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 e not equal to s
b7300 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f elf while the co
b7310 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 mparison is taki
b7320 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 ng place.. ** T
b7330 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
b7340 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 on also assumes
b7350 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65 a coherent cache
b7360 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 - that . ** se
b7370 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 parate processes
b7380 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 cannot read dif
b7390 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72 ferent values fr
b73a0 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a om the same. **
b73b0 20 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20 address at the
b73c0 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65 same time. If e
b73d0 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 ither of these t
b73e0 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 wo conditions.
b73f0 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 ** are not met,
b7400 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73 then the mutexes
b7410 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 will fail and p
b7420 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 roblems will res
b7430 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 ult.. */. {.
b7440 20 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66 pthread_t self
b7450 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 = pthread_self(
b7460 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 );. if( p->nR
b7470 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f ef>0 && pthread_
b7480 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 equal(p->owner,
b7490 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70 self) ){. p
b74a0 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 65 ->nRef++;. }e
b74b0 6c 73 65 7b 0a 20 20 20 20 20 20 70 74 68 72 65 lse{. pthre
b74c0 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 ad_mutex_lock(&p
b74d0 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 ->mutex);.
b74e0 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d assert( p->nRef=
b74f0 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f =0 );. p->o
b7500 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 wner = self;.
b7510 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a p->nRef = 1;.
b7520 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a }. }.#else.
b7530 20 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 /* Use the bui
b7540 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 lt-in recursive
b7550 6d 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20 mutexes if they
b7560 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 are available..
b7570 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 */. pthread_mu
b7580 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 tex_lock(&p->mut
b7590 65 78 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f ex);.#if SQLITE_
b75a0 4d 55 54 45 58 5f 4e 52 45 46 0a 20 20 61 73 73 MUTEX_NREF. ass
b75b0 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 7c ert( p->nRef>0 |
b75c0 7c 20 70 2d 3e 6f 77 6e 65 72 3d 3d 30 20 29 3b | p->owner==0 );
b75d0 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 . p->owner = pt
b75e0 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 hread_self();.
b75f0 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 65 6e 64 69 p->nRef++;.#endi
b7600 66 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 f.#endif..#ifdef
b7610 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
b7620 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a if( p->trace ){.
b7630 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65 printf("ente
b7640 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 r mutex %p (%d)
b7650 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c with nRef=%d\n",
b7660 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d p, p->trace, p-
b7670 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 >nRef);. }.#end
b7680 69 66 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 if.}.static int
b7690 70 74 68 72 65 61 64 4d 75 74 65 78 54 72 79 28 pthreadMutexTry(
b76a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
b76b0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 ){. int rc;. a
b76c0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
b76d0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
b76e0 53 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d SIVE || pthreadM
b76f0 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 utexNotheld(p) )
b7700 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
b7710 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 _HOMEGROWN_RECUR
b7720 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 SIVE_MUTEX. /*
b7730 49 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 If recursive mut
b7740 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 exes are not ava
b7750 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 ilable, then we
b7760 68 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a have to grow. *
b7770 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 * our own. This
b7780 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
b7790 61 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68 assumes that pth
b77a0 72 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a read_equal(). *
b77b0 2a 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 * is atomic - th
b77c0 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 at it cannot be
b77d0 64 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68 deceived into th
b77e0 69 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a inking self. **
b77f0 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 and p->owner ar
b7800 65 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 e equal if p->ow
b7810 6e 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 77 ner changes betw
b7820 65 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 een two values.
b7830 20 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 ** that are not
b7840 20 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 equal to self w
b7850 68 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69 hile the compari
b7860 73 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c son is taking pl
b7870 61 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 ace.. ** This i
b7880 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c mplementation al
b7890 73 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 so assumes a coh
b78a0 65 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 erent cache - th
b78b0 61 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 at . ** separat
b78c0 65 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e e processes cann
b78d0 6f 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e ot read differen
b78e0 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 t values from th
b78f0 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 e same. ** addr
b7900 65 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 ess at the same
b7910 74 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72 time. If either
b7920 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f of these two co
b7930 6e 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 nditions. ** ar
b7940 65 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 e not met, then
b7950 74 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c the mutexes will
b7960 20 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 fail and proble
b7970 6d 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a ms will result..
b7980 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 */. {. pth
b7990 72 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 read_t self = pt
b79a0 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 hread_self();.
b79b0 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 if( p->nRef>0
b79c0 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c && pthread_equal
b79d0 28 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 (p->owner, self)
b79e0 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 ){. p->nRe
b79f0 66 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 f++;. rc =
b7a00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
b7a10 65 6c 73 65 20 69 66 28 20 70 74 68 72 65 61 64 else if( pthread
b7a20 5f 6d 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 _mutex_trylock(&
b7a30 70 2d 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a p->mutex)==0 ){.
b7a40 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d assert( p-
b7a50 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 >nRef==0 );.
b7a60 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c p->owner = sel
b7a70 66 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 f;. p->nRef
b7a80 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d = 1;. rc =
b7a90 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
b7aa0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
b7ab0 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
b7ac0 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 }. }.#else.
b7ad0 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c /* Use the buil
b7ae0 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d t-in recursive m
b7af0 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20 61 utexes if they a
b7b00 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 re available..
b7b10 2a 2f 0a 20 20 69 66 28 20 70 74 68 72 65 61 64 */. if( pthread
b7b20 5f 6d 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 _mutex_trylock(&
b7b30 70 2d 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a p->mutex)==0 ){.
b7b40 23 69 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 #if SQLITE_MUTEX
b7b50 5f 4e 52 45 46 0a 20 20 20 20 70 2d 3e 6f 77 6e _NREF. p->own
b7b60 65 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c er = pthread_sel
b7b70 66 28 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 f();. p->nRef
b7b80 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 ++;.#endif. r
b7b90 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
b7ba0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
b7bb0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
b7bc0 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 }.#endif..#ifdef
b7bd0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
b7be0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
b7bf0 4b 20 26 26 20 70 2d 3e 74 72 61 63 65 20 29 7b K && p->trace ){
b7c00 0a 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 . printf("ent
b7c10 65 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 er mutex %p (%d)
b7c20 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 with nRef=%d\n"
b7c30 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 , p, p->trace, p
b7c40 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e ->nRef);. }.#en
b7c50 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b dif. return rc;
b7c60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 .}../*.** The sq
b7c70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
b7c80 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 e() routine exit
b7c90 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 s a mutex that w
b7ca0 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 as.** previously
b7cb0 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 entered by the
b7cc0 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 same thread. Th
b7cd0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 e behavior.** is
b7ce0 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 undefined if th
b7cf0 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 e mutex is not c
b7d00 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 urrently entered
b7d10 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 or.** is not cu
b7d20 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 rrently allocate
b7d30 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 d. SQLite will
b7d40 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e never do either.
b7d50 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
b7d60 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76 pthreadMutexLeav
b7d70 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
b7d80 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
b7d90 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28 threadMutexHeld(
b7da0 70 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 p) );.#if SQLITE
b7db0 5f 4d 55 54 45 58 5f 4e 52 45 46 0a 20 20 70 2d _MUTEX_NREF. p-
b7dc0 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 >nRef--;. if( p
b7dd0 2d 3e 6e 52 65 66 3d 3d 30 20 29 20 70 2d 3e 6f ->nRef==0 ) p->o
b7de0 77 6e 65 72 20 3d 20 30 3b 0a 23 65 6e 64 69 66 wner = 0;.#endif
b7df0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 . assert( p->nR
b7e00 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d ef==0 || p->id==
b7e10 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
b7e20 55 52 53 49 56 45 20 29 3b 0a 0a 23 69 66 64 65 URSIVE );..#ifde
b7e30 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f f SQLITE_HOMEGRO
b7e40 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 WN_RECURSIVE_MUT
b7e50 45 58 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 EX. if( p->nRef
b7e60 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 68 72 65 ==0 ){. pthre
b7e70 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 ad_mutex_unlock(
b7e80 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a &p->mutex);. }.
b7e90 23 65 6c 73 65 0a 20 20 70 74 68 72 65 61 64 5f #else. pthread_
b7ea0 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d mutex_unlock(&p-
b7eb0 3e 6d 75 74 65 78 29 3b 0a 23 65 6e 64 69 66 0a >mutex);.#endif.
b7ec0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
b7ed0 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 EBUG. if( p->tr
b7ee0 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 ace ){. print
b7ef0 66 28 22 6c 65 61 76 65 20 6d 75 74 65 78 20 25 f("leave mutex %
b7f00 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66 p (%d) with nRef
b7f10 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 =%d\n", p, p->tr
b7f20 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 ace, p->nRef);.
b7f30 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53 51 4c }.#endif.}..SQL
b7f40 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
b7f50 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
b7f60 73 20 63 6f 6e 73 74 20 2a 73 71 6c 69 74 65 33 s const *sqlite3
b7f70 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 DefaultMutex(voi
b7f80 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e d){. static con
b7f90 73 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 st sqlite3_mutex
b7fa0 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 _methods sMutex
b7fb0 3d 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 4d = {. pthreadM
b7fc0 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 70 74 utexInit,. pt
b7fd0 68 72 65 61 64 4d 75 74 65 78 45 6e 64 2c 0a 20 hreadMutexEnd,.
b7fe0 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 41 pthreadMutexA
b7ff0 6c 6c 6f 63 2c 0a 20 20 20 20 70 74 68 72 65 61 lloc,. pthrea
b8000 64 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 dMutexFree,.
b8010 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65 pthreadMutexEnte
b8020 72 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 r,. pthreadMu
b8030 74 65 78 54 72 79 2c 0a 20 20 20 20 70 74 68 72 texTry,. pthr
b8040 65 61 64 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 eadMutexLeave,.#
b8050 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
b8060 55 47 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 UG. pthreadMu
b8070 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 70 74 68 texHeld,. pth
b8080 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 readMutexNotheld
b8090 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a 20 20 .#else. 0,.
b80a0 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0.#endif. };.
b80b0 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 . return &sMute
b80c0 78 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 x;.}..#endif /*
b80d0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 SQLITE_MUTEX_PTH
b80e0 52 45 41 44 53 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a READS */../*****
b80f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
b8100 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a 2a mutex_unix.c **
b8110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8130 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
b8140 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
b8150 66 69 6c 65 20 6d 75 74 65 78 5f 77 33 32 2e 63 file mutex_w32.c
b8160 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
b8170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8180 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
b8190 32 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2007 August 14.*
b81a0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
b81b0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
b81c0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
b81d0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
b81e0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
b81f0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
b8200 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
b8210 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
b8220 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
b8230 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
b8240 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
b8250 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
b8260 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
b8270 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
b8280 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
b8290 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
b82a0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
b82b0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
b82c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b82d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b82e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b82f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
b8300 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
b8310 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 ins the C functi
b8320 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ons that impleme
b8330 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 77 nt mutexes for w
b8340 69 6e 33 32 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 in32.*/../*.** T
b8350 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 he code in this
b8360 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 file is only use
b8370 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d 70 d if we are comp
b8380 69 6c 69 6e 67 20 6d 75 6c 74 69 74 68 72 65 61 iling multithrea
b8390 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 77 69 6e 33 ded.** on a win3
b83a0 32 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66 2 system..*/.#if
b83b0 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 def SQLITE_MUTEX
b83c0 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 _W32../*.** Each
b83d0 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 recursive mutex
b83e0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 is an instance
b83f0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
b8400 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 structure..*/.s
b8410 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 truct sqlite3_mu
b8420 74 65 78 20 7b 0a 20 20 43 52 49 54 49 43 41 4c tex {. CRITICAL
b8430 5f 53 45 43 54 49 4f 4e 20 6d 75 74 65 78 3b 20 _SECTION mutex;
b8440 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 /* Mutex cont
b8450 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b rolling the lock
b8460 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 */. int id;
b8470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b8480 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 20 2a /* Mutex type *
b8490 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
b84a0 44 45 42 55 47 0a 20 20 76 6f 6c 61 74 69 6c 65 DEBUG. volatile
b84b0 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
b84c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
b84d0 65 6e 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20 enterances */.
b84e0 76 6f 6c 61 74 69 6c 65 20 44 57 4f 52 44 20 6f volatile DWORD o
b84f0 77 6e 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 68 wner; /* Th
b8500 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 read holding thi
b8510 73 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74 s mutex */. int
b8520 20 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 trace;
b8530 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
b8540 74 6f 20 74 72 61 63 65 20 63 68 61 6e 67 65 73 to trace changes
b8550 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 64 */.#endif.};.#d
b8560 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 33 32 efine SQLITE_W32
b8570 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a _MUTEX_INITIALIZ
b8580 45 52 20 7b 20 30 20 7d 0a 23 69 66 64 65 66 20 ER { 0 }.#ifdef
b8590 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 64 65 SQLITE_DEBUG.#de
b85a0 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 fine SQLITE3_MUT
b85b0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b EX_INITIALIZER {
b85c0 20 53 51 4c 49 54 45 5f 57 33 32 5f 4d 55 54 45 SQLITE_W32_MUTE
b85d0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 X_INITIALIZER, 0
b85e0 2c 20 30 4c 2c 20 28 44 57 4f 52 44 29 30 2c 20 , 0L, (DWORD)0,
b85f0 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 0 }.#else.#defin
b8600 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f e SQLITE3_MUTEX_
b8610 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 53 51 INITIALIZER { SQ
b8620 4c 49 54 45 5f 57 33 32 5f 4d 55 54 45 58 5f 49 LITE_W32_MUTEX_I
b8630 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 20 7d 0a NITIALIZER, 0 }.
b8640 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 #endif../*.** Re
b8650 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a turn true (non-z
b8660 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 20 72 ero) if we are r
b8670 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 69 6e unning under Win
b8680 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50 NT, Win2K, WinXP
b8690 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20 ,.** or WinCE.
b86a0 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 7a 65 Return false (ze
b86b0 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c 20 57 ro) for Win95, W
b86c0 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a in98, or WinME..
b86d0 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 61 6e **.** Here is an
b86e0 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f 62 73 interesting obs
b86f0 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35 ervation: Win95
b8700 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e , Win98, and Win
b8710 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c ME lack.** the L
b8720 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e ockFileEx() API.
b8730 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 74 69 But we can sti
b8740 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 ll statically li
b8750 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74 0a nk against that.
b8760 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20 61 ** API as long a
b8770 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20 s we don't call
b8780 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 20 57 it win running W
b8790 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63 in95/98/ME. A c
b87a0 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72 all to.** this r
b87b0 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 outine is used t
b87c0 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 o determine if t
b87d0 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39 35 he host is Win95
b87e0 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e /98/ME or.** Win
b87f0 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61 74 NT/2K/XP so that
b8800 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 we will know wh
b8810 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 65 20 ether or not we
b8820 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a can safely call.
b8830 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 ** the LockFileE
b8840 78 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 6d x() API..**.** m
b8850 75 74 65 78 49 73 4e 54 28 29 20 69 73 20 6f 6e utexIsNT() is on
b8860 6c 79 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 ly used for the
b8870 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c TryEnterCritical
b8880 53 65 63 74 69 6f 6e 28 29 20 41 50 49 20 63 61 Section() API ca
b8890 6c 6c 2c 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 ll,.** which is
b88a0 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 only available i
b88b0 66 20 79 6f 75 72 20 61 70 70 6c 69 63 61 74 69 f your applicati
b88c0 6f 6e 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 on was compiled
b88d0 77 69 74 68 20 0a 2a 2a 20 5f 57 49 4e 33 32 5f with .** _WIN32_
b88e0 57 49 4e 4e 54 20 64 65 66 69 6e 65 64 20 74 6f WINNT defined to
b88f0 20 61 20 76 61 6c 75 65 20 3e 3d 20 30 78 30 34 a value >= 0x04
b8900 30 30 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 00. Currently,
b8910 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 63 61 6c 6c the only.** call
b8920 20 74 6f 20 54 72 79 45 6e 74 65 72 43 72 69 74 to TryEnterCrit
b8930 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 69 73 icalSection() is
b8940 20 23 69 66 64 65 66 27 65 64 20 6f 75 74 2c 20 #ifdef'ed out,
b8950 73 6f 20 23 69 66 64 65 66 20 0a 2a 2a 20 74 68 so #ifdef .** th
b8960 69 73 20 6f 75 74 20 61 73 20 77 65 6c 6c 2e 0a is out as well..
b8970 2a 2f 0a 23 69 66 20 30 0a 23 69 66 20 53 51 4c */.#if 0.#if SQL
b8980 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 7c 7c 20 ITE_OS_WINCE ||
b8990 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a SQLITE_OS_WINRT.
b89a0 23 20 64 65 66 69 6e 65 20 6d 75 74 65 78 49 73 # define mutexIs
b89b0 4e 54 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a NT() (1).#else.
b89c0 20 20 73 74 61 74 69 63 20 69 6e 74 20 6d 75 74 static int mut
b89d0 65 78 49 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 exIsNT(void){.
b89e0 20 20 73 74 61 74 69 63 20 69 6e 74 20 6f 73 54 static int osT
b89f0 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 ype = 0;. if(
b8a00 20 6f 73 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 osType==0 ){.
b8a10 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 OSVERSIONINF
b8a20 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 O sInfo;. s
b8a30 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e Info.dwOSVersion
b8a40 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f InfoSize = sizeo
b8a50 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 f(sInfo);.
b8a60 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26 73 49 GetVersionEx(&sI
b8a70 6e 66 6f 29 3b 0a 20 20 20 20 20 20 6f 73 54 79 nfo);. osTy
b8a80 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 pe = sInfo.dwPla
b8a90 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 tformId==VER_PLA
b8aa0 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f TFORM_WIN32_NT ?
b8ab0 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 2 : 1;. }.
b8ac0 20 20 72 65 74 75 72 6e 20 6f 73 54 79 70 65 3d return osType=
b8ad0 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f =2;. }.#endif /
b8ae0 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 * SQLITE_OS_WINC
b8af0 45 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 23 69 66 E */.#endif..#if
b8b00 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
b8b10 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
b8b20 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 e3_mutex_held()
b8b30 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
b8b40 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 x_notheld() rout
b8b50 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e ine are.** inten
b8b60 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 ded for use only
b8b70 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 inside assert()
b8b80 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a statements..*/.
b8b90 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 static int winMu
b8ba0 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f texHeld(sqlite3_
b8bb0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 mutex *p){. ret
b8bc0 75 72 6e 20 70 2d 3e 6e 52 65 66 21 3d 30 20 26 urn p->nRef!=0 &
b8bd0 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 & p->owner==GetC
b8be0 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 urrentThreadId()
b8bf0 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 ;.}.static int w
b8c00 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 32 28 inMutexNotheld2(
b8c10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
b8c20 2c 20 44 57 4f 52 44 20 74 69 64 29 7b 0a 20 20 , DWORD tid){.
b8c30 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d return p->nRef==
b8c40 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 3d 74 0 || p->owner!=t
b8c50 69 64 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 id;.}.static int
b8c60 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 winMutexNotheld
b8c70 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
b8c80 70 29 7b 0a 20 20 44 57 4f 52 44 20 74 69 64 20 p){. DWORD tid
b8c90 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 = GetCurrentThre
b8ca0 61 64 49 64 28 29 3b 20 0a 20 20 72 65 74 75 72 adId(); . retur
b8cb0 6e 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c n winMutexNothel
b8cc0 64 32 28 70 2c 20 74 69 64 29 3b 0a 7d 0a 23 65 d2(p, tid);.}.#e
b8cd0 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 ndif.../*.** Ini
b8ce0 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e tialize and dein
b8cf0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 itialize the mut
b8d00 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f ex subsystem..*/
b8d10 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f .static sqlite3_
b8d20 6d 75 74 65 78 20 77 69 6e 4d 75 74 65 78 5f 73 mutex winMutex_s
b8d30 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d 20 taticMutexes[6]
b8d40 3d 20 7b 0a 20 20 53 51 4c 49 54 45 33 5f 4d 55 = {. SQLITE3_MU
b8d50 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
b8d60 0a 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 . SQLITE3_MUTEX
b8d70 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 _INITIALIZER,.
b8d80 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e SQLITE3_MUTEX_IN
b8d90 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 53 51 4c ITIALIZER,. SQL
b8da0 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 ITE3_MUTEX_INITI
b8db0 41 4c 49 5a 45 52 2c 0a 20 20 53 51 4c 49 54 45 ALIZER,. SQLITE
b8dc0 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 3_MUTEX_INITIALI
b8dd0 5a 45 52 2c 0a 20 20 53 51 4c 49 54 45 33 5f 4d ZER,. SQLITE3_M
b8de0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
b8df0 0a 7d 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .};.static int w
b8e00 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d inMutex_isInit =
b8e10 20 30 3b 0a 2f 2a 20 41 73 20 77 69 6e 4d 75 74 0;./* As winMut
b8e20 65 78 49 6e 69 74 28 29 20 61 6e 64 20 77 69 6e exInit() and win
b8e30 4d 75 74 65 78 45 6e 64 28 29 20 61 72 65 20 63 MutexEnd() are c
b8e40 61 6c 6c 65 64 20 61 73 20 70 61 72 74 0a 2a 2a alled as part.**
b8e50 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
b8e60 69 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 initialize and s
b8e70 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
b8e80 29 0a 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 2c ).** processing,
b8e90 20 74 68 65 20 22 69 6e 74 65 72 6c 6f 63 6b 65 the "interlocke
b8ea0 64 22 20 6d 61 67 69 63 20 69 73 20 70 72 6f 62 d" magic is prob
b8eb0 61 62 6c 79 20 6e 6f 74 0a 2a 2a 20 73 74 72 69 ably not.** stri
b8ec0 63 74 6c 79 20 6e 65 63 65 73 73 61 72 79 2e 0a ctly necessary..
b8ed0 2a 2f 0a 73 74 61 74 69 63 20 6c 6f 6e 67 20 77 */.static long w
b8ee0 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 20 3d 20 30 inMutex_lock = 0
b8ef0 3b 0a 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f ;..SQLITE_API vo
b8f00 69 64 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 id sqlite3_win32
b8f10 5f 73 6c 65 65 70 28 44 57 4f 52 44 20 6d 69 6c _sleep(DWORD mil
b8f20 6c 69 73 65 63 6f 6e 64 73 29 3b 20 2f 2a 20 6f liseconds); /* o
b8f30 73 5f 77 69 6e 2e 63 20 2a 2f 0a 0a 73 74 61 74 s_win.c */..stat
b8f40 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 49 ic int winMutexI
b8f50 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a nit(void){ . /*
b8f60 20 54 68 65 20 66 69 72 73 74 20 74 6f 20 69 6e The first to in
b8f70 63 72 65 6d 65 6e 74 20 74 6f 20 31 20 64 6f 65 crement to 1 doe
b8f80 73 20 61 63 74 75 61 6c 20 69 6e 69 74 69 61 6c s actual initial
b8f90 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 ization */. if(
b8fa0 20 49 6e 74 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 InterlockedComp
b8fb0 61 72 65 45 78 63 68 61 6e 67 65 28 26 77 69 6e areExchange(&win
b8fc0 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20 31 2c 20 30 Mutex_lock, 1, 0
b8fd0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 )==0 ){. int
b8fe0 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 i;. for(i=0;
b8ff0 69 3c 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d i<ArraySize(winM
b9000 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 utex_staticMutex
b9010 65 73 29 3b 20 69 2b 2b 29 7b 0a 23 69 66 20 53 es); i++){.#if S
b9020 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 QLITE_OS_WINRT.
b9030 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 InitializeC
b9040 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 45 78 riticalSectionEx
b9050 28 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 (&winMutex_stati
b9060 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 cMutexes[i].mute
b9070 78 2c 20 30 2c 20 30 29 3b 0a 23 65 6c 73 65 0a x, 0, 0);.#else.
b9080 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 Initialize
b9090 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 CriticalSection(
b90a0 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 &winMutex_static
b90b0 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 Mutexes[i].mutex
b90c0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a );.#endif. }.
b90d0 20 20 20 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 winMutex_isI
b90e0 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 nit = 1;. }else
b90f0 7b 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 6f 6e 65 {. /* Someone
b9100 20 65 6c 73 65 20 69 73 20 69 6e 20 74 68 65 20 else is in the
b9110 70 72 6f 63 65 73 73 20 6f 66 20 69 6e 69 74 69 process of initi
b9120 6e 67 20 74 68 65 20 73 74 61 74 69 63 20 6d 75 ng the static mu
b9130 74 65 78 65 73 20 2a 2f 0a 20 20 20 20 77 68 69 texes */. whi
b9140 6c 65 28 20 21 77 69 6e 4d 75 74 65 78 5f 69 73 le( !winMutex_is
b9150 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 73 71 Init ){. sq
b9160 6c 69 74 65 33 5f 77 69 6e 33 32 5f 73 6c 65 65 lite3_win32_slee
b9170 70 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a p(1);. }. }.
b9180 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
b9190 4f 4b 3b 20 0a 7d 0a 0a 73 74 61 74 69 63 20 69 OK; .}..static i
b91a0 6e 74 20 77 69 6e 4d 75 74 65 78 45 6e 64 28 76 nt winMutexEnd(v
b91b0 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 54 68 65 20 oid){ . /* The
b91c0 66 69 72 73 74 20 74 6f 20 64 65 63 72 65 6d 65 first to decreme
b91d0 6e 74 20 74 6f 20 30 20 64 6f 65 73 20 61 63 74 nt to 0 does act
b91e0 75 61 6c 20 73 68 75 74 64 6f 77 6e 20 0a 20 20 ual shutdown .
b91f0 2a 2a 20 28 77 68 69 63 68 20 73 68 6f 75 6c 64 ** (which should
b9200 20 62 65 20 74 68 65 20 6c 61 73 74 20 74 6f 20 be the last to
b9210 73 68 75 74 64 6f 77 6e 2e 29 20 2a 2f 0a 20 20 shutdown.) */.
b9220 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 43 if( InterlockedC
b9230 6f 6d 70 61 72 65 45 78 63 68 61 6e 67 65 28 26 ompareExchange(&
b9240 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20 30 winMutex_lock, 0
b9250 2c 20 31 29 3d 3d 31 20 29 7b 0a 20 20 20 20 69 , 1)==1 ){. i
b9260 66 28 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e f( winMutex_isIn
b9270 69 74 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 69 it==1 ){. i
b9280 6e 74 20 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 nt i;. for(
b9290 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 i=0; i<ArraySize
b92a0 28 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 (winMutex_static
b92b0 4d 75 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a Mutexes); i++){.
b92c0 20 20 20 20 20 20 20 20 44 65 6c 65 74 65 43 72 DeleteCr
b92d0 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 77 iticalSection(&w
b92e0 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75 inMutex_staticMu
b92f0 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b texes[i].mutex);
b9300 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77 . }. w
b9310 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d inMutex_isInit =
b9320 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0;. }. }.
b9330 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
b9340 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ; .}../*.** The
b9350 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
b9360 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c loc() routine al
b9370 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a locates a new.**
b9380 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 mutex and retur
b9390 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
b93a0 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 it. If it retur
b93b0 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 ns NULL.** that
b93c0 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 means that a mut
b93d0 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ex could not be
b93e0 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 allocated. SQLi
b93f0 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e te.** will unwin
b9400 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 d its stack and
b9410 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e return an error.
b9420 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a The argument.*
b9430 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 * to sqlite3_mut
b9440 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e ex_alloc() is on
b9450 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 e of these integ
b9460 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a er constants:.**
b9470 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
b9480 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 SQLITE_MUTEX_F
b9490 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c AST.** <li> SQL
b94a0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
b94b0 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c IVE.** <li> SQL
b94c0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
b94d0 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 _MASTER.** <li>
b94e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
b94f0 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e ATIC_MEM.** <li>
b9500 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
b9510 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c TATIC_MEM2.** <l
b9520 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
b9530 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 _STATIC_PRNG.**
b9540 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
b9550 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a EX_STATIC_LRU.**
b9560 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
b9570 54 45 58 5f 53 54 41 54 49 43 5f 50 4d 45 4d 0a TEX_STATIC_PMEM.
b9580 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 ** </ul>.**.** T
b9590 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e he first two con
b95a0 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c stants cause sql
b95b0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
b95c0 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 () to create.**
b95d0 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 a new mutex. Th
b95e0 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 e new mutex is r
b95f0 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 ecursive when SQ
b9600 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
b9610 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 SIVE.** is used
b9620 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 but not necessar
b9630 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 ily so when SQLI
b9640 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 TE_MUTEX_FAST is
b9650 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 used..** The mu
b9660 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 tex implementati
b9670 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 on does not need
b9680 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 to make a disti
b9690 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 nction.** betwee
b96a0 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 n SQLITE_MUTEX_R
b96b0 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c ECURSIVE and SQL
b96c0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 ITE_MUTEX_FAST i
b96d0 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 f it does.** not
b96e0 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 want to. But S
b96f0 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 QLite will only
b9700 72 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 request a recurs
b9710 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 ive mutex in.**
b9720 63 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 cases where it r
b9730 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e eally needs one.
b9740 20 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f If a faster no
b9750 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 n-recursive mute
b9760 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 x.** implementat
b9770 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 ion is available
b9780 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 on the host pla
b9790 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 tform, the mutex
b97a0 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 subsystem.** mi
b97b0 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 ght return such
b97c0 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f a mutex in respo
b97d0 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 nse to SQLITE_MU
b97e0 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 TEX_FAST..**.**
b97f0 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 The other allowe
b9800 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 d parameters to
b9810 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
b9820 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 loc() each retur
b9830 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 n.** a pointer t
b9840 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 o a static preex
b9850 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53 isting mutex. S
b9860 69 78 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 ix static mutexe
b9870 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 s are.** used by
b9880 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 the current ver
b9890 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 sion of SQLite.
b98a0 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 Future versions
b98b0 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 of SQLite.** ma
b98c0 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c y add additional
b98d0 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e static mutexes.
b98e0 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 Static mutexes
b98f0 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 are for interna
b9900 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 l.** use by SQLi
b9910 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 te only. Applic
b9920 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 ations that use
b9930 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 SQLite mutexes s
b9940 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c hould.** use onl
b9950 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 y the dynamic mu
b9960 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 texes returned b
b9970 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 y SQLITE_MUTEX_F
b9980 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 AST or.** SQLITE
b9990 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
b99a0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
b99b0 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 t if one of the
b99c0 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 dynamic mutex pa
b99d0 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 rameters (SQLITE
b99e0 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f _MUTEX_FAST.** o
b99f0 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 r SQLITE_MUTEX_R
b9a00 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 ECURSIVE) is use
b9a10 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d d then sqlite3_m
b9a20 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 utex_alloc().**
b9a30 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 returns a differ
b9a40 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 ent mutex on eve
b9a50 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f ry call. But fo
b9a60 72 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a r the static .**
b9a70 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 mutex types, th
b9a80 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 e same mutex is
b9a90 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 returned on ever
b9aa0 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a y call that has.
b9ab0 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 ** the same type
b9ac0 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 number..*/.stat
b9ad0 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
b9ae0 20 2a 77 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 28 *winMutexAlloc(
b9af0 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 int iType){. sq
b9b00 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a lite3_mutex *p;.
b9b10 0a 20 20 73 77 69 74 63 68 28 20 69 54 79 70 65 . switch( iType
b9b20 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ){. case SQL
b9b30 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a ITE_MUTEX_FAST:.
b9b40 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
b9b50 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a MUTEX_RECURSIVE:
b9b60 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c {. p = sql
b9b70 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 ite3MallocZero(
b9b80 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 sizeof(*p) );.
b9b90 20 20 20 20 69 66 28 20 70 20 29 7b 20 20 0a 23 if( p ){ .#
b9ba0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
b9bb0 55 47 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 UG. p->id
b9bc0 20 3d 20 69 54 79 70 65 3b 0a 23 65 6e 64 69 66 = iType;.#endif
b9bd0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
b9be0 49 4e 52 54 0a 20 20 20 20 20 20 20 20 49 6e 69 INRT. Ini
b9bf0 74 69 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 tializeCriticalS
b9c00 65 63 74 69 6f 6e 45 78 28 26 70 2d 3e 6d 75 74 ectionEx(&p->mut
b9c10 65 78 2c 20 30 2c 20 30 29 3b 0a 23 65 6c 73 65 ex, 0, 0);.#else
b9c20 0a 20 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c . Initial
b9c30 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 izeCriticalSecti
b9c40 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 on(&p->mutex);.#
b9c50 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 endif. }.
b9c60 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
b9c70 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a . default: {.
b9c80 20 20 20 20 20 20 61 73 73 65 72 74 28 20 77 69 assert( wi
b9c90 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31 nMutex_isInit==1
b9ca0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
b9cb0 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 ( iType-2 >= 0 )
b9cc0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
b9cd0 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 53 iType-2 < ArrayS
b9ce0 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 74 61 ize(winMutex_sta
b9cf0 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b 0a 20 ticMutexes) );.
b9d00 20 20 20 20 20 70 20 3d 20 26 77 69 6e 4d 75 74 p = &winMut
b9d10 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 ex_staticMutexes
b9d20 5b 69 54 79 70 65 2d 32 5d 3b 0a 23 69 66 64 65 [iType-2];.#ifde
b9d30 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
b9d40 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 p->id = iTy
b9d50 70 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 pe;.#endif.
b9d60 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
b9d70 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
b9d80 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
b9d90 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 tine deallocates
b9da0 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a a previously.**
b9db0 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 allocated mutex
b9dc0 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 . SQLite is car
b9dd0 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 eful to dealloca
b9de0 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 te every.** mute
b9df0 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 x that it alloca
b9e00 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tes..*/.static v
b9e10 6f 69 64 20 77 69 6e 4d 75 74 65 78 46 72 65 65 oid winMutexFree
b9e20 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
b9e30 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 p){. assert( p
b9e40 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
b9e50 6e 52 65 66 3d 3d 30 20 26 26 20 70 2d 3e 6f 77 nRef==0 && p->ow
b9e60 6e 65 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 ner==0 );. asse
b9e70 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 rt( p->id==SQLIT
b9e80 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 E_MUTEX_FAST ||
b9e90 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
b9ea0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b TEX_RECURSIVE );
b9eb0 0a 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 . DeleteCritica
b9ec0 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 lSection(&p->mut
b9ed0 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 ex);. sqlite3_f
b9ee0 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ree(p);.}../*.**
b9ef0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
b9f00 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 ex_enter() and s
b9f10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
b9f20 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 () routines atte
b9f30 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 mpt.** to enter
b9f40 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f a mutex. If ano
b9f50 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 ther thread is a
b9f60 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 lready within th
b9f70 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 e mutex,.** sqli
b9f80 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
b9f90 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 ) will block and
b9fa0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
b9fb0 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e ry() will return
b9fc0 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e .** SQLITE_BUSY.
b9fd0 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 The sqlite3_mu
b9fe0 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 tex_try() interf
b9ff0 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ace returns SQLI
ba000 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 TE_OK.** upon su
ba010 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 ccessful entry.
ba020 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 Mutexes created
ba030 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 using SQLITE_MU
ba040 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 TEX_RECURSIVE ca
ba050 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 n.** be entered
ba060 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 multiple times b
ba070 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 y the same threa
ba080 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 d. In such case
ba090 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 s the,.** mutex
ba0a0 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 must be exited a
ba0b0 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f n equal number o
ba0c0 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 f times before a
ba0d0 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a nother thread.**
ba0e0 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 can enter. If
ba0f0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 the same thread
ba100 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 tries to enter a
ba110 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 ny other kind of
ba120 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 mutex.** more t
ba130 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 han once, the be
ba140 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 havior is undefi
ba150 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ned..*/.static v
ba160 6f 69 64 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 oid winMutexEnte
ba170 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 r(sqlite3_mutex
ba180 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 *p){.#ifdef SQLI
ba190 54 45 5f 44 45 42 55 47 0a 20 20 44 57 4f 52 44 TE_DEBUG. DWORD
ba1a0 20 74 69 64 20 3d 20 47 65 74 43 75 72 72 65 6e tid = GetCurren
ba1b0 74 54 68 72 65 61 64 49 64 28 29 3b 20 0a 20 20 tThreadId(); .
ba1c0 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 assert( p->id==S
ba1d0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
ba1e0 52 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 RSIVE || winMute
ba1f0 78 4e 6f 74 68 65 6c 64 32 28 70 2c 20 74 69 64 xNotheld2(p, tid
ba200 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 45 6e ) );.#endif. En
ba210 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 terCriticalSecti
ba220 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 on(&p->mutex);.#
ba230 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
ba240 55 47 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e UG. assert( p->
ba250 6e 52 65 66 3e 30 20 7c 7c 20 70 2d 3e 6f 77 6e nRef>0 || p->own
ba260 65 72 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 6f 77 er==0 );. p->ow
ba270 6e 65 72 20 3d 20 74 69 64 3b 20 0a 20 20 70 2d ner = tid; . p-
ba280 3e 6e 52 65 66 2b 2b 3b 0a 20 20 69 66 28 20 70 >nRef++;. if( p
ba290 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 ->trace ){. p
ba2a0 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74 rintf("enter mut
ba2b0 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20 ex %p (%d) with
ba2c0 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 nRef=%d\n", p, p
ba2d0 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 ->trace, p->nRef
ba2e0 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a );. }.#endif.}.
ba2f0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 static int winMu
ba300 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d texTry(sqlite3_m
ba310 75 74 65 78 20 2a 70 29 7b 0a 23 69 66 6e 64 65 utex *p){.#ifnde
ba320 66 20 4e 44 45 42 55 47 0a 20 20 44 57 4f 52 44 f NDEBUG. DWORD
ba330 20 74 69 64 20 3d 20 47 65 74 43 75 72 72 65 6e tid = GetCurren
ba340 74 54 68 72 65 61 64 49 64 28 29 3b 20 0a 23 65 tThreadId(); .#e
ba350 6e 64 69 66 0a 20 20 69 6e 74 20 72 63 20 3d 20 ndif. int rc =
ba360 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 SQLITE_BUSY;. a
ba370 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
ba380 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
ba390 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 SIVE || winMutex
ba3a0 4e 6f 74 68 65 6c 64 32 28 70 2c 20 74 69 64 29 Notheld2(p, tid)
ba3b0 20 29 3b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 );. /*. ** Th
ba3c0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
ba3d0 74 72 79 28 29 20 72 6f 75 74 69 6e 65 20 69 73 try() routine is
ba3e0 20 76 65 72 79 20 72 61 72 65 6c 79 20 75 73 65 very rarely use
ba3f0 64 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 0a 20 d, and when it.
ba400 20 2a 2a 20 69 73 20 75 73 65 64 20 69 74 20 69 ** is used it i
ba410 73 20 6d 65 72 65 6c 79 20 61 6e 20 6f 70 74 69 s merely an opti
ba420 6d 69 7a 61 74 69 6f 6e 2e 20 20 53 6f 20 69 74 mization. So it
ba430 20 69 73 20 4f 4b 20 66 6f 72 20 69 74 20 74 6f is OK for it to
ba440 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 66 61 69 always. ** fai
ba450 6c 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 l. . **. ** T
ba460 68 65 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 he TryEnterCriti
ba470 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 69 6e 74 calSection() int
ba480 65 72 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 61 erface is only a
ba490 76 61 69 6c 61 62 6c 65 20 6f 6e 20 57 69 6e 4e vailable on WinN
ba4a0 54 2e 0a 20 20 2a 2a 20 41 6e 64 20 73 6f 6d 65 T.. ** And some
ba4b0 20 77 69 6e 64 6f 77 73 20 63 6f 6d 70 69 6c 65 windows compile
ba4c0 72 73 20 63 6f 6d 70 6c 61 69 6e 20 69 66 20 79 rs complain if y
ba4d0 6f 75 20 74 72 79 20 74 6f 20 75 73 65 20 69 74 ou try to use it
ba4e0 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 66 69 without. ** fi
ba4f0 72 73 74 20 64 6f 69 6e 67 20 73 6f 6d 65 20 23 rst doing some #
ba500 64 65 66 69 6e 65 73 20 74 68 61 74 20 70 72 65 defines that pre
ba510 76 65 6e 74 20 53 51 4c 69 74 65 20 66 72 6f 6d vent SQLite from
ba520 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20 57 69 6e building on Win
ba530 39 38 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 61 98.. ** For tha
ba540 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 77 69 6c t reason, we wil
ba550 6c 20 6f 6d 69 74 20 74 68 69 73 20 6f 70 74 69 l omit this opti
ba560 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20 6e 6f 77 mization for now
ba570 2e 20 20 53 65 65 0a 20 20 2a 2a 20 74 69 63 6b . See. ** tick
ba580 65 74 20 23 32 36 38 35 2e 0a 20 20 2a 2f 0a 23 et #2685.. */.#
ba590 69 66 20 30 0a 20 20 69 66 28 20 6d 75 74 65 78 if 0. if( mutex
ba5a0 49 73 4e 54 28 29 20 26 26 20 54 72 79 45 6e 74 IsNT() && TryEnt
ba5b0 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f erCriticalSectio
ba5c0 6e 28 26 70 2d 3e 6d 75 74 65 78 29 20 29 7b 0a n(&p->mutex) ){.
ba5d0 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 p->owner = t
ba5e0 69 64 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b id;. p->nRef+
ba5f0 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 +;. rc = SQLI
ba600 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 23 65 6c 73 65 TE_OK;. }.#else
ba610 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ba620 54 45 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 23 TER(p);.#endif.#
ba630 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
ba640 55 47 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c UG. if( rc==SQL
ba650 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 74 72 61 ITE_OK && p->tra
ba660 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 ce ){. printf
ba670 28 22 74 72 79 20 6d 75 74 65 78 20 25 70 20 28 ("try mutex %p (
ba680 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 %d) with nRef=%d
ba690 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 \n", p, p->trace
ba6a0 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a , p->nRef);. }.
ba6b0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
ba6c0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 rc;.}../*.** The
ba6d0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
ba6e0 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 eave() routine e
ba6f0 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 xits a mutex tha
ba700 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 t was.** previou
ba710 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 sly entered by t
ba720 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 he same thread.
ba730 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a The behavior.**
ba740 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 is undefined if
ba750 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f the mutex is no
ba760 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 t currently ente
ba770 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 red or.** is not
ba780 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 currently alloc
ba790 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 ated. SQLite wi
ba7a0 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 ll never do eith
ba7b0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f er..*/.static vo
ba7c0 69 64 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 id winMutexLeave
ba7d0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
ba7e0 70 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 p){.#ifndef NDEB
ba7f0 55 47 0a 20 20 44 57 4f 52 44 20 74 69 64 20 3d UG. DWORD tid =
ba800 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 GetCurrentThrea
ba810 64 49 64 28 29 3b 0a 20 20 61 73 73 65 72 74 28 dId();. assert(
ba820 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 p->nRef>0 );.
ba830 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 assert( p->owner
ba840 3d 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52 ==tid );. p->nR
ba850 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 2d 3e 6e ef--;. if( p->n
ba860 52 65 66 3d 3d 30 20 29 20 70 2d 3e 6f 77 6e 65 Ref==0 ) p->owne
ba870 72 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 r = 0;. assert(
ba880 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 p->nRef==0 || p
ba890 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
ba8a0 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a EX_RECURSIVE );.
ba8b0 23 65 6e 64 69 66 0a 20 20 4c 65 61 76 65 43 72 #endif. LeaveCr
ba8c0 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 iticalSection(&p
ba8d0 2d 3e 6d 75 74 65 78 29 3b 0a 23 69 66 64 65 66 ->mutex);.#ifdef
ba8e0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
ba8f0 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a if( p->trace ){.
ba900 20 20 20 20 70 72 69 6e 74 66 28 22 6c 65 61 76 printf("leav
ba910 65 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 e mutex %p (%d)
ba920 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c with nRef=%d\n",
ba930 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d p, p->trace, p-
ba940 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 >nRef);. }.#end
ba950 69 66 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 if.}..SQLITE_PRI
ba960 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 VATE sqlite3_mut
ba970 65 78 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 ex_methods const
ba980 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 *sqlite3Default
ba990 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 Mutex(void){. s
ba9a0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
ba9b0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
ba9c0 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 s sMutex = {.
ba9d0 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 2c 0a 20 winMutexInit,.
ba9e0 20 20 20 77 69 6e 4d 75 74 65 78 45 6e 64 2c 0a winMutexEnd,.
ba9f0 20 20 20 20 77 69 6e 4d 75 74 65 78 41 6c 6c 6f winMutexAllo
baa00 63 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 46 c,. winMutexF
baa10 72 65 65 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 ree,. winMute
baa20 78 45 6e 74 65 72 2c 0a 20 20 20 20 77 69 6e 4d xEnter,. winM
baa30 75 74 65 78 54 72 79 2c 0a 20 20 20 20 77 69 6e utexTry,. win
baa40 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 MutexLeave,.#ifd
baa50 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
baa60 20 20 20 20 77 69 6e 4d 75 74 65 78 48 65 6c 64 winMutexHeld
baa70 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 4e 6f ,. winMutexNo
baa80 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 theld.#else.
baa90 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0,. 0.#endif.
baaa0 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 };.. return &
baab0 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 sMutex;.}.#endif
baac0 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 /* SQLITE_MUTEX
baad0 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a _W32 */../******
baae0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
baaf0 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a mutex_w32.c ****
bab00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bab10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bab20 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
bab30 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
bab40 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a ile malloc.c ***
bab50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bab60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bab70 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
bab80 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 001 September 15
bab90 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
baba0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
babb0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
babc0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
babd0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
babe0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
babf0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
bac00 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
bac10 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
bac20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
bac30 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
bac40 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
bac50 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
bac60 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
bac70 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
bac80 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
bac90 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
baca0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
bacb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bacc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bacd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bace0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
bacf0 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c **.** Memory all
bad00 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e ocation function
bad10 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 6f 75 s used throughou
bad20 74 20 73 71 6c 69 74 65 2e 0a 2a 2f 0a 2f 2a 20 t sqlite..*/./*
bad30 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 #include <stdarg
bad40 2e 68 3e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 74 .h> */../*.** At
bad50 74 65 6d 70 74 20 74 6f 20 72 65 6c 65 61 73 65 tempt to release
bad60 20 75 70 20 74 6f 20 6e 20 62 79 74 65 73 20 6f up to n bytes o
bad70 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 f non-essential
bad80 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 memory currently
bad90 0a 2a 2a 20 68 65 6c 64 20 62 79 20 53 51 4c 69 .** held by SQLi
bada0 74 65 2e 20 41 6e 20 65 78 61 6d 70 6c 65 20 6f te. An example o
badb0 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 f non-essential
badc0 6d 65 6d 6f 72 79 20 69 73 20 6d 65 6d 6f 72 79 memory is memory
badd0 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 61 63 68 used to.** cach
bade0 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 73 e database pages
badf0 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 63 75 that are not cu
bae00 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a rrently in use..
bae10 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
bae20 74 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 t sqlite3_releas
bae30 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b e_memory(int n){
bae40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
bae50 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e NABLE_MEMORY_MAN
bae60 41 47 45 4d 45 4e 54 0a 20 20 72 65 74 75 72 6e AGEMENT. return
bae70 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
bae80 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 29 3b 0a leaseMemory(n);.
bae90 23 65 6c 73 65 0a 20 20 2f 2a 20 49 4d 50 4c 45 #else. /* IMPLE
baea0 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d MENTATION-OF: R-
baeb0 33 34 33 39 31 2d 32 34 39 32 31 20 54 68 65 20 34391-24921 The
baec0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
baed0 6d 65 6d 6f 72 79 28 29 20 72 6f 75 74 69 6e 65 memory() routine
baee0 0a 20 20 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 . ** is a no-op
baef0 20 72 65 74 75 72 6e 69 6e 67 20 7a 65 72 6f 20 returning zero
baf00 69 66 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74 if SQLite is not
baf10 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 20 compiled with.
baf20 20 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c ** SQLITE_ENABL
baf30 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d E_MEMORY_MANAGEM
baf40 45 4e 54 2e 20 2a 2f 0a 20 20 55 4e 55 53 45 44 ENT. */. UNUSED
baf50 5f 50 41 52 41 4d 45 54 45 52 28 6e 29 3b 0a 20 _PARAMETER(n);.
baf60 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 return 0;.#endi
baf70 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e f.}../*.** An in
baf80 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
baf90 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74 20 72 llowing object r
bafa0 65 63 6f 72 64 73 20 74 68 65 20 6c 6f 63 61 74 ecords the locat
bafb0 69 6f 6e 20 6f 66 0a 2a 2a 20 65 61 63 68 20 75 ion of.** each u
bafc0 6e 75 73 65 64 20 73 63 72 61 74 63 68 20 62 75 nused scratch bu
bafd0 66 66 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ffer..*/.typedef
bafe0 20 73 74 72 75 63 74 20 53 63 72 61 74 63 68 46 struct ScratchF
baff0 72 65 65 73 6c 6f 74 20 7b 0a 20 20 73 74 72 75 reeslot {. stru
bb000 63 74 20 53 63 72 61 74 63 68 46 72 65 65 73 6c ct ScratchFreesl
bb010 6f 74 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 ot *pNext; /*
bb020 4e 65 78 74 20 75 6e 75 73 65 64 20 73 63 72 61 Next unused scra
bb030 74 63 68 20 62 75 66 66 65 72 20 2a 2f 0a 7d 20 tch buffer */.}
bb040 53 63 72 61 74 63 68 46 72 65 65 73 6c 6f 74 3b ScratchFreeslot;
bb050 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 74 65 20 69 6e ../*.** State in
bb060 66 6f 72 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20 formation local
bb070 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c to the memory al
bb080 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
bb090 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 em..*/.static SQ
bb0a0 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 LITE_WSD struct
bb0b0 4d 65 6d 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 73 Mem0Global {. s
bb0c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
bb0d0 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 tex; /*
bb0e0 4d 75 74 65 78 20 74 6f 20 73 65 72 69 61 6c 69 Mutex to seriali
bb0f0 7a 65 20 61 63 63 65 73 73 20 2a 2f 0a 0a 20 20 ze access */..
bb100 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 /*. ** The alar
bb110 6d 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 m callback and i
bb120 74 73 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 ts arguments. T
bb130 68 65 20 6d 65 6d 30 2e 6d 75 74 65 78 20 6c 6f he mem0.mutex lo
bb140 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 ck will. ** be
bb150 68 65 6c 64 20 77 68 69 6c 65 20 74 68 65 20 63 held while the c
bb160 61 6c 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 allback is runni
bb170 6e 67 2e 20 20 52 65 63 75 72 73 69 76 65 20 63 ng. Recursive c
bb180 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 alls into. ** t
bb190 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 he memory subsys
bb1a0 74 65 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c tem are allowed,
bb1b0 20 62 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c but no new call
bb1c0 62 61 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 backs will be.
bb1d0 2a 2a 20 69 73 73 75 65 64 2e 0a 20 20 2a 2f 0a ** issued.. */.
bb1e0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
bb1f0 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b 0a alarmThreshold;.
bb200 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43 61 void (*alarmCa
bb210 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20 73 llback)(void*, s
bb220 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 qlite3_int64,int
bb230 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72 6d );. void *alarm
bb240 41 72 67 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 Arg;.. /*. **
bb250 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 Pointers to the
bb260 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 47 6c end of sqlite3Gl
bb270 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 obalConfig.pScra
bb280 74 63 68 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 tch memory. **
bb290 28 73 6f 20 74 68 61 74 20 61 20 72 61 6e 67 65 (so that a range
bb2a0 20 74 65 73 74 20 63 61 6e 20 62 65 20 75 73 65 test can be use
bb2b0 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 d to determine i
bb2c0 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a f an allocation.
bb2d0 20 20 2a 2a 20 62 65 69 6e 67 20 66 72 65 65 64 ** being freed
bb2e0 20 63 61 6d 65 20 66 72 6f 6d 20 70 53 63 72 61 came from pScra
bb2f0 74 63 68 29 20 61 6e 64 20 61 20 70 6f 69 6e 74 tch) and a point
bb300 65 72 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f er to the list o
bb310 66 0a 20 20 2a 2a 20 75 6e 75 73 65 64 20 73 63 f. ** unused sc
bb320 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e ratch allocation
bb330 73 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20 2a s.. */. void *
bb340 70 53 63 72 61 74 63 68 45 6e 64 3b 0a 20 20 53 pScratchEnd;. S
bb350 63 72 61 74 63 68 46 72 65 65 73 6c 6f 74 20 2a cratchFreeslot *
bb360 70 53 63 72 61 74 63 68 46 72 65 65 3b 0a 20 20 pScratchFree;.
bb370 75 33 32 20 6e 53 63 72 61 74 63 68 46 72 65 65 u32 nScratchFree
bb380 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75 ;.. /*. ** Tru
bb390 65 20 69 66 20 68 65 61 70 20 69 73 20 6e 65 61 e if heap is nea
bb3a0 72 6c 79 20 22 66 75 6c 6c 22 20 77 68 65 72 65 rly "full" where
bb3b0 20 22 66 75 6c 6c 22 20 69 73 20 64 65 66 69 6e "full" is defin
bb3c0 65 64 20 62 79 20 74 68 65 0a 20 20 2a 2a 20 73 ed by the. ** s
bb3d0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 qlite3_soft_heap
bb3e0 5f 6c 69 6d 69 74 28 29 20 73 65 74 74 69 6e 67 _limit() setting
bb3f0 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 61 .. */. int nea
bb400 72 6c 79 46 75 6c 6c 3b 0a 7d 20 6d 65 6d 30 20 rlyFull;.} mem0
bb410 3d 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 = { 0, 0, 0, 0,
bb420 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23 0, 0, 0, 0 };..#
bb430 64 65 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 define mem0 GLOB
bb440 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c AL(struct Mem0Gl
bb450 6f 62 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a obal, mem0)../*.
bb460 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
bb470 72 75 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 65 runs when the me
bb480 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 73 mory allocator s
bb490 65 65 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 ees that the.**
bb4a0 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c total memory all
bb4b0 6f 63 61 74 69 6f 6e 20 69 73 20 61 62 6f 75 74 ocation is about
bb4c0 20 74 6f 20 65 78 63 65 65 64 20 74 68 65 20 73 to exceed the s
bb4d0 6f 66 74 20 68 65 61 70 0a 2a 2a 20 6c 69 6d 69 oft heap.** limi
bb4e0 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
bb4f0 64 20 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 d softHeapLimitE
bb500 6e 66 6f 72 63 65 72 28 0a 20 20 76 6f 69 64 20 nforcer(. void
bb510 2a 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73 71 6c *NotUsed, . sql
bb520 69 74 65 33 5f 69 6e 74 36 34 20 4e 6f 74 55 73 ite3_int64 NotUs
bb530 65 64 32 2c 0a 20 20 69 6e 74 20 61 6c 6c 6f 63 ed2,. int alloc
bb540 53 69 7a 65 0a 29 7b 0a 20 20 55 4e 55 53 45 44 Size.){. UNUSED
bb550 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 _PARAMETER2(NotU
bb560 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a sed, NotUsed2);.
bb570 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 sqlite3_releas
bb580 65 5f 6d 65 6d 6f 72 79 28 61 6c 6c 6f 63 53 69 e_memory(allocSi
bb590 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 ze);.}../*.** Ch
bb5a0 61 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63 ange the alarm c
bb5b0 61 6c 6c 62 61 63 6b 0a 2a 2f 0a 73 74 61 74 69 allback.*/.stati
bb5c0 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d c int sqlite3Mem
bb5d0 6f 72 79 41 6c 61 72 6d 28 0a 20 20 76 6f 69 64 oryAlarm(. void
bb5e0 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 (*xCallback)(voi
bb5f0 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65 33 d *pArg, sqlite3
bb600 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74 20 _int64 used,int
bb610 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 N),. void *pArg
bb620 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ,. sqlite3_int6
bb630 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 4 iThreshold.){.
bb640 20 20 69 6e 74 20 6e 55 73 65 64 3b 0a 20 20 73 int nUsed;. s
bb650 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
bb660 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a er(mem0.mutex);.
bb670 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c mem0.alarmCall
bb680 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b back = xCallback
bb690 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 ;. mem0.alarmAr
bb6a0 67 20 3d 20 70 41 72 67 3b 0a 20 20 6d 65 6d 30 g = pArg;. mem0
bb6b0 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 .alarmThreshold
bb6c0 3d 20 69 54 68 72 65 73 68 6f 6c 64 3b 0a 20 20 = iThreshold;.
bb6d0 6e 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33 53 nUsed = sqlite3S
bb6e0 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 tatusValue(SQLIT
bb6f0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
bb700 55 53 45 44 29 3b 0a 20 20 6d 65 6d 30 2e 6e 65 USED);. mem0.ne
bb710 61 72 6c 79 46 75 6c 6c 20 3d 20 28 69 54 68 72 arlyFull = (iThr
bb720 65 73 68 6f 6c 64 3e 30 20 26 26 20 69 54 68 72 eshold>0 && iThr
bb730 65 73 68 6f 6c 64 3c 3d 6e 55 73 65 64 29 3b 0a eshold<=nUsed);.
bb740 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
bb750 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 leave(mem0.mutex
bb760 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
bb770 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 TE_OK;.}..#ifnde
bb780 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 f SQLITE_OMIT_DE
bb790 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 PRECATED./*.** D
bb7a0 65 70 72 65 63 61 74 65 64 20 65 78 74 65 72 6e eprecated extern
bb7b0 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 al interface. I
bb7c0 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c nternal/core SQL
bb7d0 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 ite code.** shou
bb7e0 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d ld call sqlite3M
bb7f0 65 6d 6f 72 79 41 6c 61 72 6d 2e 0a 2a 2f 0a 53 emoryAlarm..*/.S
bb800 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
bb810 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 lite3_memory_ala
bb820 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c rm(. void(*xCal
bb830 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 lback)(void *pAr
bb840 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 g, sqlite3_int64
bb850 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 used,int N),.
bb860 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 void *pArg,. sq
bb870 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72 lite3_int64 iThr
bb880 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 72 65 74 75 eshold.){. retu
bb890 72 6e 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 rn sqlite3Memory
bb8a0 41 6c 61 72 6d 28 78 43 61 6c 6c 62 61 63 6b 2c Alarm(xCallback,
bb8b0 20 70 41 72 67 2c 20 69 54 68 72 65 73 68 6f 6c pArg, iThreshol
bb8c0 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a d);.}.#endif../*
bb8d0 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66 74 .** Set the soft
bb8e0 20 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74 heap-size limit
bb8f0 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72 79 for the library
bb900 2e 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72 6f . Passing a zero
bb910 20 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76 65 or .** negative
bb920 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 value indicates
bb930 20 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51 no limit..*/.SQ
bb940 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
bb950 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 73 _int64 sqlite3_s
bb960 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34 oft_heap_limit64
bb970 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e (sqlite3_int64 n
bb980 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 ){. sqlite3_int
bb990 36 34 20 70 72 69 6f 72 4c 69 6d 69 74 3b 0a 20 64 priorLimit;.
bb9a0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 65 sqlite3_int64 e
bb9b0 78 63 65 73 73 3b 0a 23 69 66 6e 64 65 66 20 53 xcess;.#ifndef S
bb9c0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
bb9d0 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 NIT. int rc = s
bb9e0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
bb9f0 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 e();. if( rc )
bba00 72 65 74 75 72 6e 20 2d 31 3b 0a 23 65 6e 64 69 return -1;.#endi
bba10 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 f. sqlite3_mute
bba20 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 x_enter(mem0.mut
bba30 65 78 29 3b 0a 20 20 70 72 69 6f 72 4c 69 6d 69 ex);. priorLimi
bba40 74 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 t = mem0.alarmTh
bba50 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 reshold;. sqlit
bba60 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
bba70 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 em0.mutex);. if
bba80 28 20 6e 3c 30 20 29 20 72 65 74 75 72 6e 20 70 ( n<0 ) return p
bba90 72 69 6f 72 4c 69 6d 69 74 3b 0a 20 20 69 66 28 riorLimit;. if(
bbaa0 20 6e 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 n>0 ){. sqli
bbab0 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73 te3MemoryAlarm(s
bbac0 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f oftHeapLimitEnfo
bbad0 72 63 65 72 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d rcer, 0, n);. }
bbae0 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 else{. sqlite
bbaf0 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 30 2c 20 3MemoryAlarm(0,
bbb00 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 65 78 63 0, 0);. }. exc
bbb10 65 73 73 20 3d 20 73 71 6c 69 74 65 33 5f 6d 65 ess = sqlite3_me
bbb20 6d 6f 72 79 5f 75 73 65 64 28 29 20 2d 20 6e 3b mory_used() - n;
bbb30 0a 20 20 69 66 28 20 65 78 63 65 73 73 3e 30 20 . if( excess>0
bbb40 29 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 ) sqlite3_releas
bbb50 65 5f 6d 65 6d 6f 72 79 28 28 69 6e 74 29 28 65 e_memory((int)(e
bbb60 78 63 65 73 73 20 26 20 30 78 37 66 66 66 66 66 xcess & 0x7fffff
bbb70 66 66 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 ff));. return p
bbb80 72 69 6f 72 4c 69 6d 69 74 3b 0a 7d 0a 53 51 4c riorLimit;.}.SQL
bbb90 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
bbba0 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c ite3_soft_heap_l
bbbb0 69 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 69 imit(int n){. i
bbbc0 66 28 20 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a f( n<0 ) n = 0;.
bbbd0 20 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 sqlite3_soft_h
bbbe0 65 61 70 5f 6c 69 6d 69 74 36 34 28 6e 29 3b 0a eap_limit64(n);.
bbbf0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c }../*.** Initial
bbc00 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 ize the memory a
bbc10 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
bbc20 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 tem..*/.SQLITE_P
bbc30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
bbc40 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 e3MallocInit(voi
bbc50 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 d){. if( sqlite
bbc60 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
bbc70 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 7b 0a 20 20 xMalloc==0 ){.
bbc80 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 sqlite3MemSetD
bbc90 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 efault();. }.
bbca0 6d 65 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c memset(&mem0, 0,
bbcb0 20 73 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a sizeof(mem0));.
bbcc0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
bbcd0 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d balConfig.bCoreM
bbce0 75 74 65 78 20 29 7b 0a 20 20 20 20 6d 65 6d 30 utex ){. mem0
bbcf0 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 .mutex = sqlite3
bbd00 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
bbd10 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
bbd20 45 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 EM);. }. if( s
bbd30 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
bbd40 69 67 2e 70 53 63 72 61 74 63 68 20 26 26 20 73 ig.pScratch && s
bbd50 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
bbd60 69 67 2e 73 7a 53 63 72 61 74 63 68 3e 3d 31 30 ig.szScratch>=10
bbd70 30 0a 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 0. && sqlit
bbd80 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
bbd90 53 63 72 61 74 63 68 3e 30 20 29 7b 0a 20 20 20 Scratch>0 ){.
bbda0 20 69 6e 74 20 69 2c 20 6e 2c 20 73 7a 3b 0a 20 int i, n, sz;.
bbdb0 20 20 20 53 63 72 61 74 63 68 46 72 65 65 73 6c ScratchFreesl
bbdc0 6f 74 20 2a 70 53 6c 6f 74 3b 0a 20 20 20 20 73 ot *pSlot;. s
bbdd0 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 z = ROUNDDOWN8(s
bbde0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
bbdf0 69 67 2e 73 7a 53 63 72 61 74 63 68 29 3b 0a 20 ig.szScratch);.
bbe00 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
bbe10 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 Config.szScratch
bbe20 20 3d 20 73 7a 3b 0a 20 20 20 20 70 53 6c 6f 74 = sz;. pSlot
bbe30 20 3d 20 28 53 63 72 61 74 63 68 46 72 65 65 73 = (ScratchFrees
bbe40 6c 6f 74 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 lot*)sqlite3Glob
bbe50 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
bbe60 68 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 h;. n = sqlit
bbe70 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
bbe80 53 63 72 61 74 63 68 3b 0a 20 20 20 20 6d 65 6d Scratch;. mem
bbe90 30 2e 70 53 63 72 61 74 63 68 46 72 65 65 20 3d 0.pScratchFree =
bbea0 20 70 53 6c 6f 74 3b 0a 20 20 20 20 6d 65 6d 30 pSlot;. mem0
bbeb0 2e 6e 53 63 72 61 74 63 68 46 72 65 65 20 3d 20 .nScratchFree =
bbec0 6e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 n;. for(i=0;
bbed0 69 3c 6e 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 i<n-1; i++){.
bbee0 20 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20 pSlot->pNext
bbef0 3d 20 28 53 63 72 61 74 63 68 46 72 65 65 73 6c = (ScratchFreesl
bbf00 6f 74 2a 29 28 73 7a 2b 28 63 68 61 72 2a 29 70 ot*)(sz+(char*)p
bbf10 53 6c 6f 74 29 3b 0a 20 20 20 20 20 20 70 53 6c Slot);. pSl
bbf20 6f 74 20 3d 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 ot = pSlot->pNex
bbf30 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 6c t;. }. pSl
bbf40 6f 74 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 ot->pNext = 0;.
bbf50 20 20 20 6d 65 6d 30 2e 70 53 63 72 61 74 63 68 mem0.pScratch
bbf60 45 6e 64 20 3d 20 28 76 6f 69 64 2a 29 26 70 53 End = (void*)&pS
bbf70 6c 6f 74 5b 31 5d 3b 0a 20 20 7d 65 6c 73 65 7b lot[1];. }else{
bbf80 0a 20 20 20 20 6d 65 6d 30 2e 70 53 63 72 61 74 . mem0.pScrat
bbf90 63 68 45 6e 64 20 3d 20 30 3b 0a 20 20 20 20 73 chEnd = 0;. s
bbfa0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
bbfb0 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20 30 3b ig.pScratch = 0;
bbfc0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
bbfd0 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 alConfig.szScrat
bbfe0 63 68 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 ch = 0;. sqli
bbff0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
bc000 6e 53 63 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 nScratch = 0;.
bc010 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 }. if( sqlite3G
bc020 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 lobalConfig.pPag
bc030 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 e==0 || sqlite3G
bc040 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 lobalConfig.szPa
bc050 67 65 3c 35 31 32 0a 20 20 20 20 20 20 7c 7c 20 ge<512. ||
bc060 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
bc070 66 69 67 2e 6e 50 61 67 65 3c 31 20 29 7b 0a 20 fig.nPage<1 ){.
bc080 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
bc090 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 30 Config.pPage = 0
bc0a0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f ;. sqlite3Glo
bc0b0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 balConfig.szPage
bc0c0 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 = 0;. sqlite
bc0d0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 3GlobalConfig.nP
bc0e0 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 age = 0;. }. r
bc0f0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f eturn sqlite3Glo
bc100 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 balConfig.m.xIni
bc110 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 t(sqlite3GlobalC
bc120 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 onfig.m.pAppData
bc130 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
bc140 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 68 rn true if the h
bc150 65 61 70 20 69 73 20 63 75 72 72 65 6e 74 6c 79 eap is currently
bc160 20 75 6e 64 65 72 20 6d 65 6d 6f 72 79 20 70 72 under memory pr
bc170 65 73 73 75 72 65 20 2d 20 69 6e 20 6f 74 68 65 essure - in othe
bc180 72 0a 2a 2a 20 77 6f 72 64 73 20 69 66 20 74 68 r.** words if th
bc190 65 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 61 70 e amount of heap
bc1a0 20 75 73 65 64 20 69 73 20 63 6c 6f 73 65 20 74 used is close t
bc1b0 6f 20 74 68 65 20 6c 69 6d 69 74 20 73 65 74 20 o the limit set
bc1c0 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 6f by.** sqlite3_so
bc1d0 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 2e ft_heap_limit().
bc1e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
bc1f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 48 65 TE int sqlite3He
bc200 61 70 4e 65 61 72 6c 79 46 75 6c 6c 28 76 6f 69 apNearlyFull(voi
bc210 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 6d 65 6d d){. return mem
bc220 30 2e 6e 65 61 72 6c 79 46 75 6c 6c 3b 0a 7d 0a 0.nearlyFull;.}.
bc230 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c ./*.** Deinitial
bc240 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 ize the memory a
bc250 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
bc260 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 tem..*/.SQLITE_P
bc270 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
bc280 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 te3MallocEnd(voi
bc290 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 d){. if( sqlite
bc2a0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
bc2b0 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 20 20 xShutdown ){.
bc2c0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
bc2d0 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64 6f 77 6e nfig.m.xShutdown
bc2e0 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f (sqlite3GlobalCo
bc2f0 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 nfig.m.pAppData)
bc300 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 ;. }. memset(&
bc310 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 mem0, 0, sizeof(
bc320 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a mem0));.}../*.**
bc330 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 Return the amou
bc340 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 75 72 nt of memory cur
bc350 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f rently checked o
bc360 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ut..*/.SQLITE_AP
bc370 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 I sqlite3_int64
bc380 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 sqlite3_memory_u
bc390 73 65 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 sed(void){. int
bc3a0 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c 69 74 65 n, mx;. sqlite
bc3b0 33 5f 69 6e 74 36 34 20 72 65 73 3b 0a 20 20 73 3_int64 res;. s
bc3c0 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 53 51 qlite3_status(SQ
bc3d0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f LITE_STATUS_MEMO
bc3e0 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78 RY_USED, &n, &mx
bc3f0 2c 20 30 29 3b 0a 20 20 72 65 73 20 3d 20 28 73 , 0);. res = (s
bc400 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6e 3b 20 qlite3_int64)n;
bc410 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e 64 20 /* Work around
bc420 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64 20 43 bug in Borland C
bc430 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36 20 2a . Ticket #3216 *
bc440 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a /. return res;.
bc450 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
bc460 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 the maximum amou
bc470 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68 61 nt of memory tha
bc480 74 20 68 61 73 20 65 76 65 72 20 62 65 65 6e 0a t has ever been.
bc490 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 20 73 ** checked out s
bc4a0 69 6e 63 65 20 65 69 74 68 65 72 20 74 68 65 20 ince either the
bc4b0 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69 beginning of thi
bc4c0 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20 6f 72 20 s process.** or
bc4d0 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 20 72 since the most r
bc4e0 65 63 65 6e 74 20 72 65 73 65 74 2e 0a 2a 2f 0a ecent reset..*/.
bc4f0 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
bc500 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e3_int64 sqlite3
bc510 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 _memory_highwate
bc520 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29 r(int resetFlag)
bc530 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 {. int n, mx;.
bc540 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 sqlite3_int64 r
bc550 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 es;. sqlite3_st
bc560 61 74 75 73 28 53 51 4c 49 54 45 5f 53 54 41 54 atus(SQLITE_STAT
bc570 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 US_MEMORY_USED,
bc580 26 6e 2c 20 26 6d 78 2c 20 72 65 73 65 74 46 6c &n, &mx, resetFl
bc590 61 67 29 3b 0a 20 20 72 65 73 20 3d 20 28 73 71 ag);. res = (sq
bc5a0 6c 69 74 65 33 5f 69 6e 74 36 34 29 6d 78 3b 20 lite3_int64)mx;
bc5b0 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e 64 20 /* Work around
bc5c0 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64 20 43 bug in Borland C
bc5d0 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36 20 2a . Ticket #3216 *
bc5e0 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a /. return res;.
bc5f0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 67 65 72 }../*.** Trigger
bc600 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a 2f 0a 73 the alarm .*/.s
bc610 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
bc620 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 69 6e e3MallocAlarm(in
bc630 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69 64 t nByte){. void
bc640 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f (*xCallback)(vo
bc650 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 id*,sqlite3_int6
bc660 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 4,int);. sqlite
bc670 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 65 64 3b 3_int64 nowUsed;
bc680 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20 . void *pArg;.
bc690 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 if( mem0.alarmC
bc6a0 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 72 65 74 allback==0 ) ret
bc6b0 75 72 6e 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b urn;. xCallback
bc6c0 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c = mem0.alarmCal
bc6d0 6c 62 61 63 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 lback;. nowUsed
bc6e0 20 3d 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 = sqlite3Status
bc6f0 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41 Value(SQLITE_STA
bc700 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 TUS_MEMORY_USED)
bc710 3b 0a 20 20 70 41 72 67 20 3d 20 6d 65 6d 30 2e ;. pArg = mem0.
bc720 61 6c 61 72 6d 41 72 67 3b 0a 20 20 6d 65 6d 30 alarmArg;. mem0
bc730 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d .alarmCallback =
bc740 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 0;. sqlite3_mu
bc750 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d tex_leave(mem0.m
bc760 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c 62 61 utex);. xCallba
bc770 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73 65 64 ck(pArg, nowUsed
bc780 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 71 6c 69 , nByte);. sqli
bc790 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
bc7a0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d mem0.mutex);. m
bc7b0 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 em0.alarmCallbac
bc7c0 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 k = xCallback;.
bc7d0 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d mem0.alarmArg =
bc7e0 20 70 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pArg;.}../*.**
bc7f0 44 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f Do a memory allo
bc800 63 61 74 69 6f 6e 20 77 69 74 68 20 73 74 61 74 cation with stat
bc810 69 73 74 69 63 73 20 61 6e 64 20 61 6c 61 72 6d istics and alarm
bc820 73 2e 20 20 41 73 73 75 6d 65 20 74 68 65 0a 2a s. Assume the.*
bc830 2a 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 * lock is alread
bc840 79 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 y held..*/.stati
bc850 63 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74 68 c int mallocWith
bc860 41 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f 69 Alarm(int n, voi
bc870 64 20 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20 6e d **pp){. int n
bc880 46 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70 3b Full;. void *p;
bc890 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
bc8a0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
bc8b0 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e m0.mutex) );. n
bc8c0 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 6c Full = sqlite3Gl
bc8d0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f obalConfig.m.xRo
bc8e0 75 6e 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c 69 undup(n);. sqli
bc8f0 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c te3StatusSet(SQL
bc900 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f ITE_STATUS_MALLO
bc910 43 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69 66 C_SIZE, n);. if
bc920 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c ( mem0.alarmCall
bc930 62 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 69 back!=0 ){. i
bc940 6e 74 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 74 nt nUsed = sqlit
bc950 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 e3StatusValue(SQ
bc960 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f LITE_STATUS_MEMO
bc970 52 59 5f 55 53 45 44 29 3b 0a 20 20 20 20 69 66 RY_USED);. if
bc980 28 20 6e 55 73 65 64 20 3e 3d 20 6d 65 6d 30 2e ( nUsed >= mem0.
bc990 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 2d alarmThreshold -
bc9a0 20 6e 46 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 nFull ){.
bc9b0 6d 65 6d 30 2e 6e 65 61 72 6c 79 46 75 6c 6c 20 mem0.nearlyFull
bc9c0 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 = 1;. sqlit
bc9d0 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 e3MallocAlarm(nF
bc9e0 75 6c 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ull);. }else{
bc9f0 0a 20 20 20 20 20 20 6d 65 6d 30 2e 6e 65 61 72 . mem0.near
bca00 6c 79 46 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 lyFull = 0;.
bca10 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 }. }. p = sqli
bca20 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
bca30 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 m.xMalloc(nFull)
bca40 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
bca50 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 ENABLE_MEMORY_MA
bca60 4e 41 47 45 4d 45 4e 54 0a 20 20 69 66 28 20 70 NAGEMENT. if( p
bca70 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 ==0 && mem0.alar
bca80 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 mCallback ){.
bca90 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c sqlite3MallocAl
bcaa0 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 arm(nFull);.
bcab0 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 p = sqlite3Globa
bcac0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f lConfig.m.xMallo
bcad0 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 23 65 c(nFull);. }.#e
bcae0 6e 64 69 66 0a 20 20 69 66 28 20 70 20 29 7b 0a ndif. if( p ){.
bcaf0 20 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 nFull = sqli
bcb00 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 te3MallocSize(p)
bcb10 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
bcb20 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
bcb30 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 ATUS_MEMORY_USED
bcb40 2c 20 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 73 71 , nFull);. sq
bcb50 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
bcb60 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c QLITE_STATUS_MAL
bcb70 4c 4f 43 5f 43 4f 55 4e 54 2c 20 31 29 3b 0a 20 LOC_COUNT, 1);.
bcb80 20 7d 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 }. *pp = p;.
bcb90 72 65 74 75 72 6e 20 6e 46 75 6c 6c 3b 0a 7d 0a return nFull;.}.
bcba0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 ./*.** Allocate
bcbb0 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 72 6f memory. This ro
bcbc0 75 74 69 6e 65 20 69 73 20 6c 69 6b 65 20 73 71 utine is like sq
bcbd0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 65 lite3_malloc() e
bcbe0 78 63 65 70 74 20 74 68 61 74 20 69 74 0a 2a 2a xcept that it.**
bcbf0 20 61 73 73 75 6d 65 73 20 74 68 65 20 6d 65 6d assumes the mem
bcc00 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 68 61 ory subsystem ha
bcc10 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 s already been i
bcc20 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 nitialized..*/.S
bcc30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
bcc40 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f id *sqlite3Mallo
bcc50 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 c(int n){. void
bcc60 20 2a 70 3b 0a 20 20 69 66 28 20 6e 3c 3d 30 20 *p;. if( n<=0
bcc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
bcc80 20 49 4d 50 3a 20 52 2d 36 35 33 31 32 2d 30 34 IMP: R-65312-04
bcc90 39 31 37 20 2a 2f 20 0a 20 20 20 7c 7c 20 6e 3e 917 */ . || n>
bcca0 3d 30 78 37 66 66 66 66 66 30 30 0a 20 20 29 7b =0x7fffff00. ){
bccb0 0a 20 20 20 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 . /* A memory
bccc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 allocation of a
bccd0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
bcce0 20 77 68 69 63 68 20 69 73 20 6e 65 61 72 20 74 which is near t
bccf0 68 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a he maximum. *
bcd00 2a 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 * signed integer
bcd10 20 76 61 6c 75 65 20 6d 69 67 68 74 20 63 61 75 value might cau
bcd20 73 65 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 76 se an integer ov
bcd30 65 72 66 6c 6f 77 20 69 6e 73 69 64 65 20 6f 66 erflow inside of
bcd40 20 74 68 65 0a 20 20 20 20 2a 2a 20 78 4d 61 6c the. ** xMal
bcd50 6c 6f 63 28 29 2e 20 20 48 65 6e 63 65 20 77 65 loc(). Hence we
bcd60 20 6c 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d limit the maxim
bcd70 75 6d 20 73 69 7a 65 20 74 6f 20 30 78 37 66 66 um size to 0x7ff
bcd80 66 66 66 30 30 2c 20 67 69 76 69 6e 67 0a 20 20 fff00, giving.
bcd90 20 20 2a 2a 20 32 35 35 20 62 79 74 65 73 20 6f ** 255 bytes o
bcda0 66 20 6f 76 65 72 68 65 61 64 2e 20 20 53 51 4c f overhead. SQL
bcdb0 69 74 65 20 69 74 73 65 6c 66 20 77 69 6c 6c 20 ite itself will
bcdc0 6e 65 76 65 72 20 75 73 65 20 61 6e 79 74 68 69 never use anythi
bcdd0 6e 67 20 6e 65 61 72 0a 20 20 20 20 2a 2a 20 74 ng near. ** t
bcde0 68 69 73 20 61 6d 6f 75 6e 74 2e 20 20 54 68 65 his amount. The
bcdf0 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 72 65 61 only way to rea
bce00 63 68 20 74 68 65 20 6c 69 6d 69 74 20 69 73 20 ch the limit is
bce10 77 69 74 68 20 73 71 6c 69 74 65 33 5f 6d 61 6c with sqlite3_mal
bce20 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 70 20 3d loc() */. p =
bce30 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0;. }else if(
bce40 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
bce50 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a fig.bMemstat ){.
bce60 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
bce70 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 x_enter(mem0.mut
bce80 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f 63 57 ex);. mallocW
bce90 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b ithAlarm(n, &p);
bcea0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
bceb0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 ex_leave(mem0.mu
bcec0 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 tex);. }else{.
bced0 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c p = sqlite3Gl
bcee0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 obalConfig.m.xMa
bcef0 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 20 61 lloc(n);. }. a
bcf00 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 ssert( EIGHT_BYT
bcf10 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 29 20 29 E_ALIGNMENT(p) )
bcf20 3b 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 30 34 36 ; /* IMP: R-046
bcf30 37 35 2d 34 34 38 35 30 20 2a 2f 0a 20 20 72 65 75-44850 */. re
bcf40 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
bcf50 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 This version of
bcf60 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
bcf70 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 20 75 73 cation is for us
bcf80 65 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 e by the applica
bcf90 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 74 20 6d tion..** First m
bcfa0 61 6b 65 20 73 75 72 65 20 74 68 65 20 6d 65 6d ake sure the mem
bcfb0 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 69 73 ory subsystem is
bcfc0 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 initialized, th
bcfd0 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c en do the.** all
bcfe0 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 ocation..*/.SQLI
bcff0 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
bd000 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 20 ite3_malloc(int
bd010 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 n){.#ifndef SQLI
bd020 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 TE_OMIT_AUTOINIT
bd030 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 . if( sqlite3_i
bd040 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 nitialize() ) re
bd050 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 turn 0;.#endif.
bd060 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d return sqlite3M
bd070 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a alloc(n);.}../*.
bd080 2a 2a 20 45 61 63 68 20 74 68 72 65 61 64 20 6d ** Each thread m
bd090 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 73 ay only have a s
bd0a0 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e 64 69 6e ingle outstandin
bd0b0 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f g allocation fro
bd0c0 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68 4d 61 6c m.** xScratchMal
bd0d0 6c 6f 63 28 29 2e 20 20 57 65 20 76 65 72 69 66 loc(). We verif
bd0e0 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e y this constrain
bd0f0 74 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65 2d t in the single-
bd100 74 68 72 65 61 64 65 64 0a 2a 2a 20 63 61 73 65 threaded.** case
bd110 20 62 79 20 73 65 74 74 69 6e 67 20 73 63 72 61 by setting scra
bd120 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 6f 20 31 tchAllocOut to 1
bd130 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 when an allocat
bd140 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 73 74 61 ion.** is outsta
bd150 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e 67 20 69 nding clearing i
bd160 74 20 77 68 65 6e 20 74 68 65 20 61 6c 6c 6f 63 t when the alloc
bd170 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e 0a ation is freed..
bd180 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 */.#if SQLITE_TH
bd190 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 READSAFE==0 && !
bd1a0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a defined(NDEBUG).
bd1b0 73 74 61 74 69 63 20 69 6e 74 20 73 63 72 61 74 static int scrat
bd1c0 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a chAllocOut = 0;.
bd1d0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 #endif.../*.** A
bd1e0 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 llocate memory t
bd1f0 68 61 74 20 69 73 20 74 6f 20 62 65 20 75 73 65 hat is to be use
bd200 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 20 72 d and released r
bd210 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a 20 54 68 ight away..** Th
bd220 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 is routine is si
bd230 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f 63 61 28 milar to alloca(
bd240 29 20 69 6e 20 74 68 61 74 20 69 74 20 69 73 20 ) in that it is
bd250 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a 2a 2a 20 not intended.**
bd260 66 6f 72 20 73 69 74 75 61 74 69 6f 6e 73 20 77 for situations w
bd270 68 65 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 here the memory
bd280 6d 69 67 68 74 20 62 65 20 68 65 6c 64 20 6c 6f might be held lo
bd290 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69 73 0a 2a ng-term. This.*
bd2a0 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 74 * routine is int
bd2b0 65 6e 64 65 64 20 74 6f 20 67 65 74 20 6d 65 6d ended to get mem
bd2c0 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61 72 67 65 ory to old large
bd2d0 20 74 72 61 6e 73 69 65 6e 74 20 64 61 74 61 0a transient data.
bd2e0 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 74 68 ** structures th
bd2f0 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f 72 at would not nor
bd300 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20 74 68 65 mally fit on the
bd310 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a 2a 2a 20 stack of an.**
bd320 65 6d 62 65 64 64 65 64 20 70 72 6f 63 65 73 73 embedded process
bd330 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 or..*/.SQLITE_PR
bd340 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
bd350 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 te3ScratchMalloc
bd360 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 (int n){. void
bd370 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e *p;. assert( n>
bd380 30 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 0 );.. sqlite3_
bd390 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 mutex_enter(mem0
bd3a0 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6d .mutex);. if( m
bd3b0 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 em0.nScratchFree
bd3c0 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 && sqlite3Globa
bd3d0 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 lConfig.szScratc
bd3e0 68 3e 3d 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20 h>=n ){. p =
bd3f0 6d 65 6d 30 2e 70 53 63 72 61 74 63 68 46 72 65 mem0.pScratchFre
bd400 65 3b 0a 20 20 20 20 6d 65 6d 30 2e 70 53 63 72 e;. mem0.pScr
bd410 61 74 63 68 46 72 65 65 20 3d 20 6d 65 6d 30 2e atchFree = mem0.
bd420 70 53 63 72 61 74 63 68 46 72 65 65 2d 3e 70 4e pScratchFree->pN
bd430 65 78 74 3b 0a 20 20 20 20 6d 65 6d 30 2e 6e 53 ext;. mem0.nS
bd440 63 72 61 74 63 68 46 72 65 65 2d 2d 3b 0a 20 20 cratchFree--;.
bd450 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 sqlite3StatusA
bd460 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dd(SQLITE_STATUS
bd470 5f 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20 31 _SCRATCH_USED, 1
bd480 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 );. sqlite3St
bd490 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 atusSet(SQLITE_S
bd4a0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 TATUS_SCRATCH_SI
bd4b0 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 ZE, n);. sqli
bd4c0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
bd4d0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d mem0.mutex);. }
bd4e0 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 73 71 else{. if( sq
bd4f0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
bd500 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 g.bMemstat ){.
bd510 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
bd520 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 sSet(SQLITE_STAT
bd530 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c US_SCRATCH_SIZE,
bd540 20 6e 29 3b 0a 20 20 20 20 20 20 6e 20 3d 20 6d n);. n = m
bd550 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e allocWithAlarm(n
bd560 2c 20 26 70 29 3b 0a 20 20 20 20 20 20 69 66 28 , &p);. if(
bd570 20 70 20 29 20 73 71 6c 69 74 65 33 53 74 61 74 p ) sqlite3Stat
bd580 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
bd590 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 TUS_SCRATCH_OVER
bd5a0 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 20 20 FLOW, n);.
bd5b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
bd5c0 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ave(mem0.mutex);
bd5d0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
bd5e0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
bd5f0 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 leave(mem0.mutex
bd600 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c );. p = sql
bd610 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
bd620 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 .m.xMalloc(n);.
bd630 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
bd640 4d 65 6d 64 65 62 75 67 53 65 74 54 79 70 65 28 MemdebugSetType(
bd650 70 2c 20 4d 45 4d 54 59 50 45 5f 53 43 52 41 54 p, MEMTYPE_SCRAT
bd660 43 48 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 CH);. }. asser
bd670 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
bd680 5f 6e 6f 74 68 65 6c 64 28 6d 65 6d 30 2e 6d 75 _notheld(mem0.mu
bd690 74 65 78 29 20 29 3b 0a 0a 0a 23 69 66 20 53 51 tex) );...#if SQ
bd6a0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d LITE_THREADSAFE=
bd6b0 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e =0 && !defined(N
bd6c0 44 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 DEBUG). /* Veri
bd6d0 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 fy that no more
bd6e0 74 68 61 6e 20 74 77 6f 20 73 63 72 61 74 63 68 than two scratch
bd6f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 70 65 72 allocations per
bd700 20 74 68 72 65 61 64 0a 20 20 2a 2a 20 61 72 65 thread. ** are
bd710 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 outstanding at
bd720 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73 one time. (This
bd730 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 is only checked
bd740 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e in the. ** sin
bd750 67 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 gle-threaded cas
bd760 65 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 e since checking
bd770 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 in the multi-th
bd780 72 65 61 64 65 64 20 63 61 73 65 0a 20 20 2a 2a readed case. **
bd790 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d would be much m
bd7a0 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e ore complicated.
bd7b0 29 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 ) */. assert( s
bd7c0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3c 3d cratchAllocOut<=
bd7d0 31 20 29 3b 0a 20 20 69 66 28 20 70 20 29 20 73 1 );. if( p ) s
bd7e0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 2b 2b cratchAllocOut++
bd7f0 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 ;.#endif.. retu
bd800 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 rn p;.}.SQLITE_P
bd810 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
bd820 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28 76 te3ScratchFree(v
bd830 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 oid *p){. if( p
bd840 20 29 7b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f ){..#if SQLITE_
bd850 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 THREADSAFE==0 &&
bd860 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
bd870 29 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 ). /* Verify
bd880 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 that no more tha
bd890 6e 20 74 77 6f 20 73 63 72 61 74 63 68 20 61 6c n two scratch al
bd8a0 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 location per thr
bd8b0 65 61 64 0a 20 20 20 20 2a 2a 20 69 73 20 6f 75 ead. ** is ou
bd8c0 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 tstanding at one
bd8d0 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 time. (This is
bd8e0 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e only checked in
bd8f0 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 69 6e 67 the. ** sing
bd900 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 le-threaded case
bd910 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 since checking
bd920 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 in the multi-thr
bd930 65 61 64 65 64 20 63 61 73 65 0a 20 20 20 20 2a eaded case. *
bd940 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 * would be much
bd950 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 more complicated
bd960 2e 29 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 .) */. assert
bd970 28 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 ( scratchAllocOu
bd980 74 3e 3d 31 20 26 26 20 73 63 72 61 74 63 68 41 t>=1 && scratchA
bd990 6c 6c 6f 63 4f 75 74 3c 3d 32 20 29 3b 0a 20 20 llocOut<=2 );.
bd9a0 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 scratchAllocOu
bd9b0 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 t--;.#endif..
bd9c0 20 69 66 28 20 70 3e 3d 73 71 6c 69 74 65 33 47 if( p>=sqlite3G
bd9d0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 lobalConfig.pScr
bd9e0 61 74 63 68 20 26 26 20 70 3c 6d 65 6d 30 2e 70 atch && p<mem0.p
bd9f0 53 63 72 61 74 63 68 45 6e 64 20 29 7b 0a 20 20 ScratchEnd ){.
bda00 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 6d /* Release m
bda10 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 53 emory from the S
bda20 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 QLITE_CONFIG_SCR
bda30 41 54 43 48 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 ATCH allocation
bda40 2a 2f 0a 20 20 20 20 20 20 53 63 72 61 74 63 68 */. Scratch
bda50 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74 3b Freeslot *pSlot;
bda60 0a 20 20 20 20 20 20 70 53 6c 6f 74 20 3d 20 28 . pSlot = (
bda70 53 63 72 61 74 63 68 46 72 65 65 73 6c 6f 74 2a ScratchFreeslot*
bda80 29 70 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 )p;. sqlite
bda90 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
bdaa0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
bdab0 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20 3d 20 pSlot->pNext =
bdac0 6d 65 6d 30 2e 70 53 63 72 61 74 63 68 46 72 65 mem0.pScratchFre
bdad0 65 3b 0a 20 20 20 20 20 20 6d 65 6d 30 2e 70 53 e;. mem0.pS
bdae0 63 72 61 74 63 68 46 72 65 65 20 3d 20 70 53 6c cratchFree = pSl
bdaf0 6f 74 3b 0a 20 20 20 20 20 20 6d 65 6d 30 2e 6e ot;. mem0.n
bdb00 53 63 72 61 74 63 68 46 72 65 65 2b 2b 3b 0a 20 ScratchFree++;.
bdb10 20 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d assert( mem
bdb20 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 20 3c 0.nScratchFree <
bdb30 3d 20 28 75 33 32 29 73 71 6c 69 74 65 33 47 6c = (u32)sqlite3Gl
bdb40 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 obalConfig.nScra
bdb50 74 63 68 20 29 3b 0a 20 20 20 20 20 20 73 71 6c tch );. sql
bdb60 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 ite3StatusAdd(SQ
bdb70 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 LITE_STATUS_SCRA
bdb80 54 43 48 5f 55 53 45 44 2c 20 2d 31 29 3b 0a 20 TCH_USED, -1);.
bdb90 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
bdba0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 ex_leave(mem0.mu
bdbb0 74 65 78 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b tex);. }else{
bdbc0 0a 20 20 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 . /* Releas
bdbd0 65 20 6d 65 6d 6f 72 79 20 62 61 63 6b 20 74 6f e memory back to
bdbe0 20 74 68 65 20 68 65 61 70 20 2a 2f 0a 20 20 20 the heap */.
bdbf0 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
bdc00 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54 79 70 e3MemdebugHasTyp
bdc10 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 53 43 52 e(p, MEMTYPE_SCR
bdc20 41 54 43 48 29 20 29 3b 0a 20 20 20 20 20 20 61 ATCH) );. a
bdc30 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 4d 65 ssert( sqlite3Me
bdc40 6d 64 65 62 75 67 4e 6f 54 79 70 65 28 70 2c 20 mdebugNoType(p,
bdc50 7e 4d 45 4d 54 59 50 45 5f 53 43 52 41 54 43 48 ~MEMTYPE_SCRATCH
bdc60 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ) );. sqlit
bdc70 65 33 4d 65 6d 64 65 62 75 67 53 65 74 54 79 70 e3MemdebugSetTyp
bdc80 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 48 45 41 e(p, MEMTYPE_HEA
bdc90 50 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 P);. if( sq
bdca0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
bdcb0 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 g.bMemstat ){.
bdcc0 20 20 20 20 20 20 69 6e 74 20 69 53 69 7a 65 20 int iSize
bdcd0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 = sqlite3MallocS
bdce0 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 ize(p);.
bdcf0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
bdd00 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ter(mem0.mutex);
bdd10 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
bdd20 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 StatusAdd(SQLITE
bdd30 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f _STATUS_SCRATCH_
bdd40 4f 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 OVERFLOW, -iSize
bdd50 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
bdd60 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 e3StatusAdd(SQLI
bdd70 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 TE_STATUS_MEMORY
bdd80 5f 55 53 45 44 2c 20 2d 69 53 69 7a 65 29 3b 0a _USED, -iSize);.
bdd90 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 sqlite3S
bdda0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f tatusAdd(SQLITE_
bddb0 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 43 4f STATUS_MALLOC_CO
bddc0 55 4e 54 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 UNT, -1);.
bddd0 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
bdde0 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 onfig.m.xFree(p)
bddf0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
bde00 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
bde10 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
bde20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
bde30 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
bde40 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a fig.m.xFree(p);.
bde50 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
bde60 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20 }.}../*.** TRUE
bde70 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b 61 73 if p is a lookas
bde80 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ide memory alloc
bde90 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f ation from db.*/
bdea0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
bdeb0 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73 OMIT_LOOKASIDE.s
bdec0 74 61 74 69 63 20 69 6e 74 20 69 73 4c 6f 6f 6b tatic int isLook
bded0 61 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64 aside(sqlite3 *d
bdee0 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 b, void *p){. r
bdef0 65 74 75 72 6e 20 70 20 26 26 20 70 3e 3d 64 62 eturn p && p>=db
bdf00 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 ->lookaside.pSta
bdf10 72 74 20 26 26 20 70 3c 64 62 2d 3e 6c 6f 6f 6b rt && p<db->look
bdf20 61 73 69 64 65 2e 70 45 6e 64 3b 0a 7d 0a 23 65 aside.pEnd;.}.#e
bdf30 6c 73 65 0a 23 64 65 66 69 6e 65 20 69 73 4c 6f lse.#define isLo
bdf40 6f 6b 61 73 69 64 65 28 41 2c 42 29 20 30 0a 23 okaside(A,B) 0.#
bdf50 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 endif../*.** Ret
bdf60 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 urn the size of
bdf70 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
bdf80 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 6f ion previously o
bdf90 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 btained from.**
bdfa0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 sqlite3Malloc()
bdfb0 6f 72 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f or sqlite3_mallo
bdfc0 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 c()..*/.SQLITE_P
bdfd0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
bdfe0 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f 69 e3MallocSize(voi
bdff0 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 d *p){. assert(
be000 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 sqlite3Memdebug
be010 48 61 73 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 HasType(p, MEMTY
be020 50 45 5f 48 45 41 50 29 20 29 3b 0a 20 20 61 73 PE_HEAP) );. as
be030 73 65 72 74 28 20 73 71 6c 69 74 65 33 4d 65 6d sert( sqlite3Mem
be040 64 65 62 75 67 4e 6f 54 79 70 65 28 70 2c 20 4d debugNoType(p, M
be050 45 4d 54 59 50 45 5f 44 42 29 20 29 3b 0a 20 20 EMTYPE_DB) );.
be060 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c return sqlite3Gl
be070 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 obalConfig.m.xSi
be080 7a 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f ze(p);.}.SQLITE_
be090 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
be0a0 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 te3DbMallocSize(
be0b0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 sqlite3 *db, voi
be0c0 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 d *p){. assert(
be0d0 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 db==0 || sqlite
be0e0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3_mutex_held(db-
be0f0 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 >mutex) );. if(
be100 20 64 62 20 26 26 20 69 73 4c 6f 6f 6b 61 73 69 db && isLookasi
be110 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 de(db, p) ){.
be120 20 72 65 74 75 72 6e 20 64 62 2d 3e 6c 6f 6f 6b return db->look
be130 61 73 69 64 65 2e 73 7a 3b 0a 20 20 7d 65 6c 73 aside.sz;. }els
be140 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 e{. assert( s
be150 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 qlite3MemdebugHa
be160 73 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 sType(p, MEMTYPE
be170 5f 44 42 29 20 29 3b 0a 20 20 20 20 61 73 73 65 _DB) );. asse
be180 72 74 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 rt( sqlite3Memde
be190 62 75 67 48 61 73 54 79 70 65 28 70 2c 20 4d 45 bugHasType(p, ME
be1a0 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 7c MTYPE_LOOKASIDE|
be1b0 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 20 29 3b MEMTYPE_HEAP) );
be1c0 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 21 . assert( db!
be1d0 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 4d 65 6d =0 || sqlite3Mem
be1e0 64 65 62 75 67 4e 6f 54 79 70 65 28 70 2c 20 4d debugNoType(p, M
be1f0 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 EMTYPE_LOOKASIDE
be200 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) );. return
be210 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
be220 66 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a fig.m.xSize(p);.
be230 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 }.}../*.** Fre
be240 65 20 6d 65 6d 6f 72 79 20 70 72 65 76 69 6f 75 e memory previou
be250 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f sly obtained fro
be260 6d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 m sqlite3Malloc(
be270 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 )..*/.SQLITE_API
be280 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72 void sqlite3_fr
be290 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 ee(void *p){. i
be2a0 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
be2b0 3b 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 34 39 30 ; /* IMP: R-490
be2c0 35 33 2d 35 34 35 35 34 20 2a 2f 0a 20 20 61 73 53-54554 */. as
be2d0 73 65 72 74 28 20 73 71 6c 69 74 65 33 4d 65 6d sert( sqlite3Mem
be2e0 64 65 62 75 67 4e 6f 54 79 70 65 28 70 2c 20 4d debugNoType(p, M
be2f0 45 4d 54 59 50 45 5f 44 42 29 20 29 3b 0a 20 20 EMTYPE_DB) );.
be300 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 4d assert( sqlite3M
be310 65 6d 64 65 62 75 67 48 61 73 54 79 70 65 28 70 emdebugHasType(p
be320 2c 20 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 20 , MEMTYPE_HEAP)
be330 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
be340 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
be350 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c mstat ){. sql
be360 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
be370 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
be380 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 sqlite3StatusA
be390 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dd(SQLITE_STATUS
be3a0 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 73 _MEMORY_USED, -s
be3b0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 qlite3MallocSize
be3c0 28 70 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 (p));. sqlite
be3d0 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
be3e0 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f E_STATUS_MALLOC_
be3f0 43 4f 55 4e 54 2c 20 2d 31 29 3b 0a 20 20 20 20 COUNT, -1);.
be400 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
be410 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a fig.m.xFree(p);.
be420 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
be430 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 x_leave(mem0.mut
be440 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ex);. }else{.
be450 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
be460 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 onfig.m.xFree(p)
be470 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 ;. }.}../*.** F
be480 72 65 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 ree memory that
be490 6d 69 67 68 74 20 62 65 20 61 73 73 6f 63 69 61 might be associa
be4a0 74 65 64 20 77 69 74 68 20 61 20 70 61 72 74 69 ted with a parti
be4b0 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a cular database.*
be4c0 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f * connection..*/
be4d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
be4e0 76 6f 69 64 20 73 71 6c 69 74 65 33 44 62 46 72 void sqlite3DbFr
be4f0 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ee(sqlite3 *db,
be500 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 void *p){. asse
be510 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c rt( db==0 || sql
be520 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
be530 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
be540 69 66 28 20 64 62 20 29 7b 0a 20 20 20 20 69 66 if( db ){. if
be550 28 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 ( db->pnBytesFre
be560 65 64 20 29 7b 0a 20 20 20 20 20 20 2a 64 62 2d ed ){. *db-
be570 3e 70 6e 42 79 74 65 73 46 72 65 65 64 20 2b 3d >pnBytesFreed +=
be580 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
be590 53 69 7a 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 Size(db, p);.
be5a0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
be5b0 0a 20 20 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 . if( isLooka
be5c0 73 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 side(db, p) ){.
be5d0 20 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c LookasideSl
be5e0 6f 74 20 2a 70 42 75 66 20 3d 20 28 4c 6f 6f 6b ot *pBuf = (Look
be5f0 61 73 69 64 65 53 6c 6f 74 2a 29 70 3b 0a 23 69 asideSlot*)p;.#i
be600 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
be610 20 20 20 20 20 2f 2a 20 54 72 61 73 68 20 61 6c /* Trash al
be620 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74 68 65 l content in the
be630 20 62 75 66 66 65 72 20 62 65 69 6e 67 20 66 72 buffer being fr
be640 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 6d 65 6d eed */. mem
be650 73 65 74 28 70 2c 20 30 78 61 61 2c 20 64 62 2d set(p, 0xaa, db-
be660 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 29 3b 0a >lookaside.sz);.
be670 23 65 6e 64 69 66 0a 20 20 20 20 20 20 70 42 75 #endif. pBu
be680 66 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c f->pNext = db->l
be690 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 3b 0a ookaside.pFree;.
be6a0 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 db->lookas
be6b0 69 64 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66 ide.pFree = pBuf
be6c0 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b ;. db->look
be6d0 61 73 69 64 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20 aside.nOut--;.
be6e0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
be6f0 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 }. }. assert(
be700 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 48 sqlite3MemdebugH
be710 61 73 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 asType(p, MEMTYP
be720 45 5f 44 42 29 20 29 3b 0a 20 20 61 73 73 65 72 E_DB) );. asser
be730 74 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 t( sqlite3Memdeb
be740 75 67 48 61 73 54 79 70 65 28 70 2c 20 4d 45 4d ugHasType(p, MEM
be750 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 7c 4d TYPE_LOOKASIDE|M
be760 45 4d 54 59 50 45 5f 48 45 41 50 29 20 29 3b 0a EMTYPE_HEAP) );.
be770 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 assert( db!=0
be780 7c 7c 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 || sqlite3Memdeb
be790 75 67 4e 6f 54 79 70 65 28 70 2c 20 4d 45 4d 54 ugNoType(p, MEMT
be7a0 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 29 20 29 YPE_LOOKASIDE) )
be7b0 3b 0a 20 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 ;. sqlite3Memde
be7c0 62 75 67 53 65 74 54 79 70 65 28 70 2c 20 4d 45 bugSetType(p, ME
be7d0 4d 54 59 50 45 5f 48 45 41 50 29 3b 0a 20 20 73 MTYPE_HEAP);. s
be7e0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
be7f0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 }../*.** Change
be800 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 the size of an e
be810 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 xisting memory a
be820 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c llocation.*/.SQL
be830 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
be840 20 2a 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 *sqlite3Realloc
be850 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 (void *pOld, int
be860 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 nBytes){. int
be870 6e 4f 6c 64 2c 20 6e 4e 65 77 2c 20 6e 44 69 66 nOld, nNew, nDif
be880 66 3b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b f;. void *pNew;
be890 0a 20 20 69 66 28 20 70 4f 6c 64 3d 3d 30 20 29 . if( pOld==0 )
be8a0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c {. return sql
be8b0 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 ite3Malloc(nByte
be8c0 73 29 3b 20 2f 2a 20 49 4d 50 3a 20 52 2d 32 38 s); /* IMP: R-28
be8d0 33 35 34 2d 32 35 37 36 39 20 2a 2f 0a 20 20 7d 354-25769 */. }
be8e0 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 . if( nBytes<=0
be8f0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
be900 66 72 65 65 28 70 4f 6c 64 29 3b 20 2f 2a 20 49 free(pOld); /* I
be910 4d 50 3a 20 52 2d 33 31 35 39 33 2d 31 30 35 37 MP: R-31593-1057
be920 34 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 4 */. return
be930 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 0;. }. if( nBy
be940 74 65 73 3e 3d 30 78 37 66 66 66 66 66 30 30 20 tes>=0x7fffff00
be950 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 30 78 ){. /* The 0x
be960 37 66 66 66 66 30 30 20 6c 69 6d 69 74 20 74 65 7ffff00 limit te
be970 72 6d 20 69 73 20 65 78 70 6c 61 69 6e 65 64 20 rm is explained
be980 69 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 73 in comments on s
be990 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a qlite3Malloc() *
be9a0 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a /. return 0;.
be9b0 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 6c }. nOld = sql
be9c0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 ite3MallocSize(p
be9d0 4f 6c 64 29 3b 0a 20 20 2f 2a 20 49 4d 50 4c 45 Old);. /* IMPLE
be9e0 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d MENTATION-OF: R-
be9f0 34 36 31 39 39 2d 33 30 32 34 39 20 53 51 4c 69 46199-30249 SQLi
bea00 74 65 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 te guarantees th
bea10 61 74 20 74 68 65 20 73 65 63 6f 6e 64 0a 20 20 at the second.
bea20 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 ** argument to x
bea30 52 65 61 6c 6c 6f 63 20 69 73 20 61 6c 77 61 79 Realloc is alway
bea40 73 20 61 20 76 61 6c 75 65 20 72 65 74 75 72 6e s a value return
bea50 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63 61 ed by a prior ca
bea60 6c 6c 20 74 6f 0a 20 20 2a 2a 20 78 52 6f 75 6e ll to. ** xRoun
bea70 64 75 70 2e 20 2a 2f 0a 20 20 6e 4e 65 77 20 3d dup. */. nNew =
bea80 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
bea90 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 nfig.m.xRoundup(
beaa0 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 6e nBytes);. if( n
beab0 4f 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 20 Old==nNew ){.
beac0 20 70 4e 65 77 20 3d 20 70 4f 6c 64 3b 0a 20 20 pNew = pOld;.
bead0 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 }else if( sqlite
beae0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 3GlobalConfig.bM
beaf0 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 emstat ){. sq
beb00 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
beb10 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 r(mem0.mutex);.
beb20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
beb30 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Set(SQLITE_STATU
beb40 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e S_MALLOC_SIZE, n
beb50 42 79 74 65 73 29 3b 0a 20 20 20 20 6e 44 69 66 Bytes);. nDif
beb60 66 20 3d 20 6e 4e 65 77 20 2d 20 6e 4f 6c 64 3b f = nNew - nOld;
beb70 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
beb80 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 StatusValue(SQLI
beb90 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 TE_STATUS_MEMORY
beba0 5f 55 53 45 44 29 20 3e 3d 20 0a 20 20 20 20 20 _USED) >= .
bebb0 20 20 20 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 mem0.alarmT
bebc0 68 72 65 73 68 6f 6c 64 2d 6e 44 69 66 66 20 29 hreshold-nDiff )
bebd0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d {. sqlite3M
bebe0 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 44 69 66 66 allocAlarm(nDiff
bebf0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 );. }. ass
bec00 65 72 74 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 ert( sqlite3Memd
bec10 65 62 75 67 48 61 73 54 79 70 65 28 70 4f 6c 64 ebugHasType(pOld
bec20 2c 20 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 20 , MEMTYPE_HEAP)
bec30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
bec40 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4e 6f qlite3MemdebugNo
bec50 54 79 70 65 28 70 4f 6c 64 2c 20 7e 4d 45 4d 54 Type(pOld, ~MEMT
bec60 59 50 45 5f 48 45 41 50 29 20 29 3b 0a 20 20 20 YPE_HEAP) );.
bec70 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 pNew = sqlite3G
bec80 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 lobalConfig.m.xR
bec90 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 ealloc(pOld, nNe
beca0 77 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 w);. if( pNew
becb0 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 ==0 && mem0.alar
becc0 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 mCallback ){.
becd0 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 sqlite3Malloc
bece0 41 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a 20 Alarm(nBytes);.
becf0 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 pNew = sqli
bed00 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
bed10 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c m.xRealloc(pOld,
bed20 20 6e 4e 65 77 29 3b 0a 20 20 20 20 7d 0a 20 20 nNew);. }.
bed30 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 if( pNew ){.
bed40 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 nNew = sqlit
bed50 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4e 65 e3MallocSize(pNe
bed60 77 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 w);. sqlite
bed70 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
bed80 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
bed90 55 53 45 44 2c 20 6e 4e 65 77 2d 6e 4f 6c 64 29 USED, nNew-nOld)
beda0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
bedb0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
bedc0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d mem0.mutex);. }
bedd0 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 20 3d else{. pNew =
bede0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
bedf0 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 nfig.m.xRealloc(
bee00 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 7d pOld, nNew);. }
bee10 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 . assert( EIGHT
bee20 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 _BYTE_ALIGNMENT(
bee30 70 4e 65 77 29 20 29 3b 20 2f 2a 20 49 4d 50 3a pNew) ); /* IMP:
bee40 20 52 2d 30 34 36 37 35 2d 34 34 38 35 30 20 2a R-04675-44850 *
bee50 2f 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b /. return pNew;
bee60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 75 .}../*.** The pu
bee70 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 blic interface t
bee80 6f 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 o sqlite3Realloc
bee90 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 61 . Make sure tha
beea0 74 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 t the memory.**
beeb0 73 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 subsystem is ini
beec0 74 69 61 6c 69 7a 65 64 20 70 72 69 6f 72 20 74 tialized prior t
beed0 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 o invoking sqlit
beee0 65 52 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 51 4c eRealloc..*/.SQL
beef0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
bef00 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f lite3_realloc(vo
bef10 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 29 id *pOld, int n)
bef20 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
bef30 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 _OMIT_AUTOINIT.
bef40 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 if( sqlite3_ini
bef50 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 tialize() ) retu
bef60 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 rn 0;.#endif. r
bef70 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 61 eturn sqlite3Rea
bef80 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b 0a 7d lloc(pOld, n);.}
bef90 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 .../*.** Allocat
befa0 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 e and zero memor
befb0 79 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 y..*/ .SQLITE_PR
befc0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
befd0 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e te3MallocZero(in
befe0 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 t n){. void *p
beff0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 = sqlite3Malloc(
bf000 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 n);. if( p ){.
bf010 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 memset(p, 0,
bf020 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e n);. }. return
bf030 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c p;.}../*.** All
bf040 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d ocate and zero m
bf050 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 emory. If the a
bf060 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
bf070 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c make.** the mal
bf080 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 locFailed flag i
bf090 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e n the connection
bf0a0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c pointer..*/.SQL
bf0b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
bf0c0 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *sqlite3DbMallo
bf0d0 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 20 2a 64 cZero(sqlite3 *d
bf0e0 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 b, int n){. voi
bf0f0 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 62 d *p = sqlite3Db
bf100 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 MallocRaw(db, n)
bf110 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
bf120 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 memset(p, 0, n)
bf130 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
bf140 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 ;.}../*.** Alloc
bf150 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d ate and zero mem
bf160 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c 6c ory. If the all
bf170 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d ocation fails, m
bf180 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f ake.** the mallo
bf190 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 cFailed flag in
bf1a0 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 the connection p
bf1b0 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 ointer..**.** If
bf1c0 20 64 62 21 3d 30 20 61 6e 64 20 64 62 2d 3e 6d db!=0 and db->m
bf1d0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 74 allocFailed is t
bf1e0 72 75 65 20 28 69 6e 64 69 63 61 74 69 6e 67 20 rue (indicating
bf1f0 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 0a 2a a prior malloc.*
bf200 2a 20 66 61 69 6c 75 72 65 20 6f 6e 20 74 68 65 * failure on the
bf210 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 same database c
bf220 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65 6e 20 onnection) then
bf230 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 30 2e always return 0.
bf240 0a 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20 61 20 .** Hence for a
bf250 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 particular datab
bf260 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 ase connection,
bf270 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74 61 72 once malloc star
bf280 74 73 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c 20 69 ts.** failing, i
bf290 74 20 66 61 69 6c 73 20 63 6f 6e 73 69 73 74 65 t fails consiste
bf2a0 6e 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c 6c 6f ntly until mallo
bf2b0 63 46 61 69 6c 65 64 20 69 73 20 72 65 73 65 74 cFailed is reset
bf2c0 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 ..** This is an
bf2d0 69 6d 70 6f 72 74 61 6e 74 20 61 73 73 75 6d 70 important assump
bf2e0 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 61 72 65 tion. There are
bf2f0 20 6d 61 6e 79 20 70 6c 61 63 65 73 20 69 6e 20 many places in
bf300 74 68 65 0a 2a 2a 20 63 6f 64 65 20 74 68 61 74 the.** code that
bf310 20 64 6f 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 do things like
bf320 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 this:.**.**
bf330 20 20 20 20 69 6e 74 20 2a 61 20 3d 20 28 69 6e int *a = (in
bf340 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c t*)sqlite3DbMall
bf350 6f 63 52 61 77 28 64 62 2c 20 31 30 30 29 3b 0a ocRaw(db, 100);.
bf360 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 20 2a ** int *
bf370 62 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 b = (int*)sqlite
bf380 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
bf390 20 32 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20 200);.**
bf3a0 20 20 69 66 28 20 62 20 29 20 61 5b 31 30 5d 20 if( b ) a[10]
bf3b0 3d 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 = 9;.**.** In ot
bf3c0 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 61 20 her words, if a
bf3d0 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f subsequent mallo
bf3e0 63 20 28 65 78 3a 20 22 62 22 29 20 77 6f 72 6b c (ex: "b") work
bf3f0 65 64 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65 ed, it is assume
bf400 64 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 70 72 d.** that all pr
bf410 69 6f 72 20 6d 61 6c 6c 6f 63 73 20 28 65 78 3a ior mallocs (ex:
bf420 20 22 61 22 29 20 77 6f 72 6b 65 64 20 74 6f 6f "a") worked too
bf430 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
bf440 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
bf450 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 3DbMallocRaw(sql
bf460 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29 ite3 *db, int n)
bf470 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 {. void *p;. a
bf480 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 ssert( db==0 ||
bf490 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
bf4a0 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b ld(db->mutex) );
bf4b0 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 . assert( db==0
bf4c0 20 7c 7c 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 || db->pnBytesF
bf4d0 72 65 65 64 3d 3d 30 20 29 3b 0a 23 69 66 6e 64 reed==0 );.#ifnd
bf4e0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c ef SQLITE_OMIT_L
bf4f0 4f 4f 4b 41 53 49 44 45 0a 20 20 69 66 28 20 64 OOKASIDE. if( d
bf500 62 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 b ){. Lookasi
bf510 64 65 53 6c 6f 74 20 2a 70 42 75 66 3b 0a 20 20 deSlot *pBuf;.
bf520 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
bf530 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 Failed ){.
bf540 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a return 0;. }.
bf550 20 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b if( db->look
bf560 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 29 aside.bEnabled )
bf570 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 64 62 {. if( n>db
bf580 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 29 ->lookaside.sz )
bf590 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f {. db->lo
bf5a0 6f 6b 61 73 69 64 65 2e 61 6e 53 74 61 74 5b 31 okaside.anStat[1
bf5b0 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ]++;. }else
bf5c0 20 69 66 28 20 28 70 42 75 66 20 3d 20 64 62 2d if( (pBuf = db-
bf5d0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 >lookaside.pFree
bf5e0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
bf5f0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 61 6e db->lookaside.an
bf600 53 74 61 74 5b 32 5d 2b 2b 3b 0a 20 20 20 20 20 Stat[2]++;.
bf610 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
bf620 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 db->lookaside.pF
bf630 72 65 65 20 3d 20 70 42 75 66 2d 3e 70 4e 65 78 ree = pBuf->pNex
bf640 74 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c t;. db->l
bf650 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b ookaside.nOut++;
bf660 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f . db->loo
bf670 6b 61 73 69 64 65 2e 61 6e 53 74 61 74 5b 30 5d kaside.anStat[0]
bf680 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ++;. if(
bf690 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f db->lookaside.nO
bf6a0 75 74 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 ut>db->lookaside
bf6b0 2e 6d 78 4f 75 74 20 29 7b 0a 20 20 20 20 20 20 .mxOut ){.
bf6c0 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 db->lookasid
bf6d0 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f e.mxOut = db->lo
bf6e0 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 okaside.nOut;.
bf6f0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
bf700 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 42 return (void*)pB
bf710 75 66 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 uf;. }.
bf720 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 }. }.#else. if
bf730 28 20 64 62 20 26 26 20 64 62 2d 3e 6d 61 6c 6c ( db && db->mall
bf740 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
bf750 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65 return 0;. }.#e
bf760 6e 64 69 66 0a 20 20 70 20 3d 20 73 71 6c 69 74 ndif. p = sqlit
bf770 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 e3Malloc(n);. i
bf780 66 28 20 21 70 20 26 26 20 64 62 20 29 7b 0a 20 f( !p && db ){.
bf790 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
bf7a0 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 led = 1;. }. s
bf7b0 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 qlite3MemdebugSe
bf7c0 74 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 tType(p, MEMTYPE
bf7d0 5f 44 42 20 7c 0a 20 20 20 20 20 20 20 20 20 28 _DB |. (
bf7e0 28 64 62 20 26 26 20 64 62 2d 3e 6c 6f 6f 6b 61 (db && db->looka
bf7f0 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 29 20 3f side.bEnabled) ?
bf800 20 4d 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 MEMTYPE_LOOKASI
bf810 44 45 20 3a 20 4d 45 4d 54 59 50 45 5f 48 45 41 DE : MEMTYPE_HEA
bf820 50 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b P));. return p;
bf830 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 .}../*.** Resize
bf840 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 the block of me
bf850 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 mory pointed to
bf860 62 79 20 70 20 74 6f 20 6e 20 62 79 74 65 73 2e by p to n bytes.
bf870 20 49 66 20 74 68 65 0a 2a 2a 20 72 65 73 69 7a If the.** resiz
bf880 65 20 66 61 69 6c 73 2c 20 73 65 74 20 74 68 65 e fails, set the
bf890 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c mallocFailed fl
bf8a0 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 ag in the connec
bf8b0 74 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a tion object..*/.
bf8c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
bf8d0 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 65 oid *sqlite3DbRe
bf8e0 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20 2a 64 alloc(sqlite3 *d
bf8f0 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 b, void *p, int
bf900 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 n){. void *pNew
bf910 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 = 0;. assert(
bf920 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 db!=0 );. asser
bf930 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
bf940 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 _held(db->mutex)
bf950 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 );. if( db->ma
bf960 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 7b llocFailed==0 ){
bf970 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 7b . if( p==0 ){
bf980 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71 . return sq
bf990 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
bf9a0 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 (db, n);. }.
bf9b0 20 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 if( isLookasi
bf9c0 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 de(db, p) ){.
bf9d0 20 20 20 69 66 28 20 6e 3c 3d 64 62 2d 3e 6c 6f if( n<=db->lo
bf9e0 6f 6b 61 73 69 64 65 2e 73 7a 20 29 7b 0a 20 20 okaside.sz ){.
bf9f0 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a return p;.
bfa00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e }. pN
bfa10 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ew = sqlite3DbMa
bfa20 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a llocRaw(db, n);.
bfa30 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 if( pNew )
bfa40 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 {. memcpy
bfa50 28 70 4e 65 77 2c 20 70 2c 20 64 62 2d 3e 6c 6f (pNew, p, db->lo
bfa60 6f 6b 61 73 69 64 65 2e 73 7a 29 3b 0a 20 20 20 okaside.sz);.
bfa70 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
bfa80 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 ee(db, p);.
bfa90 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
bfaa0 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
bfab0 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54 79 te3MemdebugHasTy
bfac0 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 44 42 pe(p, MEMTYPE_DB
bfad0 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ) );. asser
bfae0 74 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 t( sqlite3Memdeb
bfaf0 75 67 48 61 73 54 79 70 65 28 70 2c 20 4d 45 4d ugHasType(p, MEM
bfb00 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 7c 4d TYPE_LOOKASIDE|M
bfb10 45 4d 54 59 50 45 5f 48 45 41 50 29 20 29 3b 0a EMTYPE_HEAP) );.
bfb20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d sqlite3Mem
bfb30 64 65 62 75 67 53 65 74 54 79 70 65 28 70 2c 20 debugSetType(p,
bfb40 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 3b 0a 20 MEMTYPE_HEAP);.
bfb50 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 pNew = sqli
bfb60 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 2c 20 6e te3_realloc(p, n
bfb70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e );. if( !pN
bfb80 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ew ){. sq
bfb90 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 lite3MemdebugSet
bfba0 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f Type(p, MEMTYPE_
bfbb0 44 42 7c 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 DB|MEMTYPE_HEAP)
bfbc0 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 ;. db->ma
bfbd0 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a llocFailed = 1;.
bfbe0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
bfbf0 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 lite3MemdebugSet
bfc00 54 79 70 65 28 70 4e 65 77 2c 20 4d 45 4d 54 59 Type(pNew, MEMTY
bfc10 50 45 5f 44 42 20 7c 20 0a 20 20 20 20 20 20 20 PE_DB | .
bfc20 20 20 20 20 20 28 64 62 2d 3e 6c 6f 6f 6b 61 73 (db->lookas
bfc30 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3f 20 4d ide.bEnabled ? M
bfc40 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 EMTYPE_LOOKASIDE
bfc50 20 3a 20 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 : MEMTYPE_HEAP)
bfc60 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 );. }. }. r
bfc70 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f eturn pNew;.}../
bfc80 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 *.** Attempt to
bfc90 72 65 61 6c 6c 6f 63 61 74 65 20 70 2e 20 20 49 reallocate p. I
bfca0 66 20 74 68 65 20 72 65 61 6c 6c 6f 63 61 74 69 f the reallocati
bfcb0 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 66 on fails, then f
bfcc0 72 65 65 20 70 0a 2a 2a 20 61 6e 64 20 73 65 74 ree p.** and set
bfcd0 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 the mallocFaile
bfce0 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 64 61 d flag in the da
bfcf0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
bfd00 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
bfd10 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
bfd20 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 e3DbReallocOrFre
bfd30 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 e(sqlite3 *db, v
bfd40 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a oid *p, int n){.
bfd50 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 void *pNew;.
bfd60 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 pNew = sqlite3Db
bfd70 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 2c 20 6e Realloc(db, p, n
bfd80 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 );. if( !pNew )
bfd90 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 {. sqlite3DbF
bfda0 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a ree(db, p);. }.
bfdb0 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
bfdc0 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 ../*.** Make a c
bfdd0 6f 70 79 20 6f 66 20 61 20 73 74 72 69 6e 67 20 opy of a string
bfde0 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e in memory obtain
bfdf0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 ed from sqliteMa
bfe00 6c 6c 6f 63 28 29 2e 20 54 68 65 73 65 20 0a 2a lloc(). These .*
bfe10 2a 20 66 75 6e 63 74 69 6f 6e 73 20 63 61 6c 6c * functions call
bfe20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61 sqlite3MallocRa
bfe30 77 28 29 20 64 69 72 65 63 74 6c 79 20 69 6e 73 w() directly ins
bfe40 74 65 61 64 20 6f 66 20 73 71 6c 69 74 65 4d 61 tead of sqliteMa
bfe50 6c 6c 6f 63 28 29 2e 20 54 68 69 73 0a 2a 2a 20 lloc(). This.**
bfe60 69 73 20 62 65 63 61 75 73 65 20 77 68 65 6e 20 is because when
bfe70 6d 65 6d 6f 72 79 20 64 65 62 75 67 67 69 6e 67 memory debugging
bfe80 20 69 73 20 74 75 72 6e 65 64 20 6f 6e 2c 20 74 is turned on, t
bfe90 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f hese two functio
bfea0 6e 73 20 61 72 65 20 0a 2a 2a 20 63 61 6c 6c 65 ns are .** calle
bfeb0 64 20 76 69 61 20 6d 61 63 72 6f 73 20 74 68 61 d via macros tha
bfec0 74 20 72 65 63 6f 72 64 20 74 68 65 20 63 75 72 t record the cur
bfed0 72 65 6e 74 20 66 69 6c 65 20 61 6e 64 20 6c 69 rent file and li
bfee0 6e 65 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 ne number in the
bfef0 0a 2a 2a 20 54 68 72 65 61 64 44 61 74 61 20 73 .** ThreadData s
bff00 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c tructure..*/.SQL
bff10 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
bff20 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 *sqlite3DbStrDu
bff30 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 p(sqlite3 *db, c
bff40 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 onst char *z){.
bff50 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 73 char *zNew;. s
bff60 69 7a 65 5f 74 20 6e 3b 0a 20 20 69 66 28 20 7a ize_t n;. if( z
bff70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
bff80 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73 n 0;. }. n = s
bff90 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
bffa0 29 20 2b 20 31 3b 0a 20 20 61 73 73 65 72 74 28 ) + 1;. assert(
bffb0 20 28 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d (n&0x7fffffff)=
bffc0 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 =n );. zNew = s
bffd0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
bffe0 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 w(db, (int)n);.
bfff0 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 if( zNew ){.
c0000 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c memcpy(zNew, z,
c0010 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 n);. }. retur
c0020 6e 20 7a 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 n zNew;.}.SQLITE
c0030 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
c0040 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 qlite3DbStrNDup(
c0050 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e sqlite3 *db, con
c0060 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 st char *z, int
c0070 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 n){. char *zNew
c0080 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a ;. if( z==0 ){.
c0090 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
c00a0 7d 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26 30 }. assert( (n&0
c00b0 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b x7fffffff)==n );
c00c0 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 . zNew = sqlite
c00d0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
c00e0 20 6e 2b 31 29 3b 0a 20 20 69 66 28 20 7a 4e 65 n+1);. if( zNe
c00f0 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 w ){. memcpy(
c0100 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 zNew, z, n);.
c0110 20 7a 4e 65 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 zNew[n] = 0;.
c0120 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b }. return zNew;
c0130 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 .}../*.** Create
c0140 20 61 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 a string from t
c0150 68 65 20 7a 46 72 6f 6d 61 74 20 61 72 67 75 6d he zFromat argum
c0160 65 6e 74 20 61 6e 64 20 74 68 65 20 76 61 5f 6c ent and the va_l
c0170 69 73 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 ist that follows
c0180 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 73 ..** Store the s
c0190 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 tring in memory
c01a0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
c01b0 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 liteMalloc() and
c01c0 20 6d 61 6b 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 make *pz.** poi
c01d0 6e 74 20 74 6f 20 74 68 61 74 20 73 74 72 69 6e nt to that strin
c01e0 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 g..*/.SQLITE_PRI
c01f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
c0200 33 53 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 3SetString(char
c0210 2a 2a 70 7a 2c 20 73 71 6c 69 74 65 33 20 2a 64 **pz, sqlite3 *d
c0220 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
c0230 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 Format, ...){.
c0240 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 va_list ap;. ch
c0250 61 72 20 2a 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 ar *z;.. va_sta
c0260 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b rt(ap, zFormat);
c0270 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d . z = sqlite3VM
c0280 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d Printf(db, zForm
c0290 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e at, ap);. va_en
c02a0 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 d(ap);. sqlite3
c02b0 44 62 46 72 65 65 28 64 62 2c 20 2a 70 7a 29 3b DbFree(db, *pz);
c02c0 0a 20 20 2a 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a . *pz = z;.}...
c02d0 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
c02e0 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 61 6c 6c ion must be call
c02f0 65 64 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e ed before exitin
c0300 67 20 61 6e 79 20 41 50 49 20 66 75 6e 63 74 69 g any API functi
c0310 6f 6e 20 28 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 on (i.e. .** ret
c0320 75 72 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 urning control t
c0330 6f 20 74 68 65 20 75 73 65 72 29 20 74 68 61 74 o the user) that
c0340 20 68 61 73 20 63 61 6c 6c 65 64 20 73 71 6c 69 has called sqli
c0350 74 65 33 5f 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a te3_malloc or.**
c0360 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 sqlite3_realloc
c0370 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 ..**.** The retu
c0380 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6e 6f rned value is no
c0390 72 6d 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 rmally a copy of
c03a0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
c03b0 6d 65 6e 74 20 74 6f 20 74 68 69 73 0a 2a 2a 20 ment to this.**
c03c0 66 75 6e 63 74 69 6f 6e 2e 20 48 6f 77 65 76 65 function. Howeve
c03d0 72 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 r, if a malloc()
c03e0 20 66 61 69 6c 75 72 65 20 68 61 73 20 6f 63 63 failure has occ
c03f0 75 72 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 urred since the
c0400 70 72 65 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f previous.** invo
c0410 63 61 74 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f cation SQLITE_NO
c0420 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 MEM is returned
c0430 69 6e 73 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 instead. .**.**
c0440 49 66 20 74 68 65 20 66 69 72 73 74 20 61 72 67 If the first arg
c0450 75 6d 65 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f ument, db, is no
c0460 74 20 4e 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c t NULL and a mal
c0470 6c 6f 63 28 29 20 65 72 72 6f 72 20 68 61 73 20 loc() error has
c0480 6f 63 63 75 72 72 65 64 2c 0a 2a 2a 20 74 68 65 occurred,.** the
c0490 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e n the connection
c04a0 20 65 72 72 6f 72 2d 63 6f 64 65 20 28 74 68 65 error-code (the
c04b0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
c04c0 62 79 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f by sqlite3_errco
c04d0 64 65 28 29 29 0a 2a 2a 20 69 73 20 73 65 74 20 de()).** is set
c04e0 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e to SQLITE_NOMEM.
c04f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
c0500 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 TE int sqlite3Ap
c0510 69 45 78 69 74 28 73 71 6c 69 74 65 33 2a 20 64 iExit(sqlite3* d
c0520 62 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 2f 2a b, int rc){. /*
c0530 20 49 66 20 74 68 65 20 64 62 20 68 61 6e 64 6c If the db handl
c0540 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 e is not NULL, t
c0550 68 65 6e 20 77 65 20 6d 75 73 74 20 68 6f 6c 64 hen we must hold
c0560 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 the connection
c0570 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 handle. ** mute
c0580 78 20 68 65 72 65 2e 20 4f 74 68 65 72 77 69 73 x here. Otherwis
c0590 65 20 74 68 65 20 72 65 61 64 20 28 61 6e 64 20 e the read (and
c05a0 70 6f 73 73 69 62 6c 65 20 77 72 69 74 65 29 20 possible write)
c05b0 6f 66 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 of db->mallocFai
c05c0 6c 65 64 20 0a 20 20 2a 2a 20 69 73 20 75 6e 73 led . ** is uns
c05d0 61 66 65 2c 20 61 73 20 69 73 20 74 68 65 20 63 afe, as is the c
c05e0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 72 all to sqlite3Er
c05f0 72 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 ror().. */. as
c0600 73 65 72 74 28 20 21 64 62 20 7c 7c 20 73 71 6c sert( !db || sql
c0610 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
c0620 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
c0630 69 66 28 20 64 62 20 26 26 20 28 64 62 2d 3e 6d if( db && (db->m
c0640 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 72 allocFailed || r
c0650 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f c==SQLITE_IOERR_
c0660 4e 4f 4d 45 4d 29 20 29 7b 0a 20 20 20 20 73 71 NOMEM) ){. sq
c0670 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 lite3Error(db, S
c0680 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b QLITE_NOMEM, 0);
c0690 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 . db->mallocF
c06a0 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 72 ailed = 0;. r
c06b0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
c06c0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
c06d0 63 20 26 20 28 64 62 20 3f 20 64 62 2d 3e 65 72 c & (db ? db->er
c06e0 72 4d 61 73 6b 20 3a 20 30 78 66 66 29 3b 0a 7d rMask : 0xff);.}
c06f0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
c0700 2a 20 45 6e 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e * End of malloc.
c0710 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
c0720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0740 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c0750 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 72 69 * Begin file pri
c0760 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ntf.c **********
c0770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0790 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 70 72 69 /./*.** The "pri
c07a0 6e 74 66 22 20 63 6f 64 65 20 74 68 61 74 20 66 ntf" code that f
c07b0 6f 6c 6c 6f 77 73 20 64 61 74 65 73 20 66 72 6f ollows dates fro
c07c0 6d 20 74 68 65 20 31 39 38 30 27 73 2e 20 20 49 m the 1980's. I
c07d0 74 20 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20 70 t is in.** the p
c07e0 75 62 6c 69 63 20 64 6f 6d 61 69 6e 2e 20 20 54 ublic domain. T
c07f0 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6d 6d he original comm
c0800 65 6e 74 73 20 61 72 65 20 69 6e 63 6c 75 64 65 ents are include
c0810 64 20 68 65 72 65 20 66 6f 72 0a 2a 2a 20 63 6f d here for.** co
c0820 6d 70 6c 65 74 65 6e 65 73 73 2e 20 20 54 68 65 mpleteness. The
c0830 79 20 61 72 65 20 76 65 72 79 20 6f 75 74 2d 6f y are very out-o
c0840 66 2d 64 61 74 65 20 62 75 74 20 6d 69 67 68 74 f-date but might
c0850 20 62 65 20 75 73 65 66 75 6c 20 61 73 0a 2a 2a be useful as.**
c0860 20 61 6e 20 68 69 73 74 6f 72 69 63 61 6c 20 72 an historical r
c0870 65 66 65 72 65 6e 63 65 2e 20 20 4d 6f 73 74 20 eference. Most
c0880 6f 66 20 74 68 65 20 22 65 6e 68 61 6e 63 65 6d of the "enhancem
c0890 65 6e 74 73 22 20 68 61 76 65 20 62 65 65 6e 20 ents" have been
c08a0 62 61 63 6b 65 64 0a 2a 2a 20 6f 75 74 20 73 6f backed.** out so
c08b0 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 that the functi
c08c0 6f 6e 61 6c 69 74 79 20 69 73 20 6e 6f 77 20 74 onality is now t
c08d0 68 65 20 73 61 6d 65 20 61 73 20 73 74 61 6e 64 he same as stand
c08e0 61 72 64 20 70 72 69 6e 74 66 28 29 2e 0a 2a 2a ard printf()..**
c08f0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
c0900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c0930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
c0940 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
c0950 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 61 20 ains code for a
c0960 73 65 74 20 6f 66 20 22 70 72 69 6e 74 66 22 2d set of "printf"-
c0970 6c 69 6b 65 20 72 6f 75 74 69 6e 65 73 2e 20 20 like routines.
c0980 54 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 These.** routine
c0990 73 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 73 s format strings
c09a0 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65 20 70 much like the p
c09b0 72 69 6e 74 66 28 29 20 66 72 6f 6d 20 74 68 65 rintf() from the
c09c0 20 73 74 61 6e 64 61 72 64 20 43 0a 2a 2a 20 6c standard C.** l
c09d0 69 62 72 61 72 79 2c 20 74 68 6f 75 67 68 20 74 ibrary, though t
c09e0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
c09f0 6e 20 68 65 72 65 20 68 61 73 20 65 6e 68 61 6e n here has enhan
c0a00 63 65 6d 65 6e 74 73 20 74 6f 20 73 75 70 70 6f cements to suppo
c0a10 72 74 0a 2a 2a 20 53 51 4c 6c 69 74 65 2e 0a 2a rt.** SQLlite..*
c0a20 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 /../*.** Convers
c0a30 69 6f 6e 20 74 79 70 65 73 20 66 61 6c 6c 20 69 ion types fall i
c0a40 6e 74 6f 20 76 61 72 69 6f 75 73 20 63 61 74 65 nto various cate
c0a50 67 6f 72 69 65 73 20 61 73 20 64 65 66 69 6e 65 gories as define
c0a60 64 20 62 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c d by the.** foll
c0a70 6f 77 69 6e 67 20 65 6e 75 6d 65 72 61 74 69 6f owing enumeratio
c0a80 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 n..*/.#define et
c0a90 52 41 44 49 58 20 20 20 20 20 20 20 31 20 2f 2a RADIX 1 /*
c0aa0 20 49 6e 74 65 67 65 72 20 74 79 70 65 73 2e 20 Integer types.
c0ab0 20 25 64 2c 20 25 78 2c 20 25 6f 2c 20 61 6e 64 %d, %x, %o, and
c0ac0 20 73 6f 20 66 6f 72 74 68 20 2a 2f 0a 23 64 65 so forth */.#de
c0ad0 66 69 6e 65 20 65 74 46 4c 4f 41 54 20 20 20 20 fine etFLOAT
c0ae0 20 20 20 32 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 2 /* Floating
c0af0 20 70 6f 69 6e 74 2e 20 20 25 66 20 2a 2f 0a 23 point. %f */.#
c0b00 64 65 66 69 6e 65 20 65 74 45 58 50 20 20 20 20 define etEXP
c0b10 20 20 20 20 20 33 20 2f 2a 20 45 78 70 6f 6e 65 3 /* Expone
c0b20 6e 74 69 6f 6e 61 6c 20 6e 6f 74 61 74 69 6f 6e ntional notation
c0b30 2e 20 25 65 20 61 6e 64 20 25 45 20 2a 2f 0a 23 . %e and %E */.#
c0b40 64 65 66 69 6e 65 20 65 74 47 45 4e 45 52 49 43 define etGENERIC
c0b50 20 20 20 20 20 34 20 2f 2a 20 46 6c 6f 61 74 69 4 /* Floati
c0b60 6e 67 20 6f 72 20 65 78 70 6f 6e 65 6e 74 69 61 ng or exponentia
c0b70 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 l, depending on
c0b80 65 78 70 6f 6e 65 6e 74 2e 20 25 67 20 2a 2f 0a exponent. %g */.
c0b90 23 64 65 66 69 6e 65 20 65 74 53 49 5a 45 20 20 #define etSIZE
c0ba0 20 20 20 20 20 20 35 20 2f 2a 20 52 65 74 75 72 5 /* Retur
c0bb0 6e 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 n number of char
c0bc0 61 63 74 65 72 73 20 70 72 6f 63 65 73 73 65 64 acters processed
c0bd0 20 73 6f 20 66 61 72 2e 20 25 6e 20 2a 2f 0a 23 so far. %n */.#
c0be0 64 65 66 69 6e 65 20 65 74 53 54 52 49 4e 47 20 define etSTRING
c0bf0 20 20 20 20 20 36 20 2f 2a 20 53 74 72 69 6e 67 6 /* String
c0c00 73 2e 20 25 73 20 2a 2f 0a 23 64 65 66 69 6e 65 s. %s */.#define
c0c10 20 65 74 44 59 4e 53 54 52 49 4e 47 20 20 20 37 etDYNSTRING 7
c0c20 20 2f 2a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 /* Dynamically
c0c30 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e 67 allocated string
c0c40 73 2e 20 25 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 s. %z */.#define
c0c50 20 65 74 50 45 52 43 45 4e 54 20 20 20 20 20 38 etPERCENT 8
c0c60 20 2f 2a 20 50 65 72 63 65 6e 74 20 73 79 6d 62 /* Percent symb
c0c70 6f 6c 2e 20 25 25 20 2a 2f 0a 23 64 65 66 69 6e ol. %% */.#defin
c0c80 65 20 65 74 43 48 41 52 58 20 20 20 20 20 20 20 e etCHARX
c0c90 39 20 2f 2a 20 43 68 61 72 61 63 74 65 72 73 2e 9 /* Characters.
c0ca0 20 25 63 20 2a 2f 0a 2f 2a 20 54 68 65 20 72 65 %c */./* The re
c0cb0 73 74 20 61 72 65 20 65 78 74 65 6e 73 69 6f 6e st are extension
c0cc0 73 2c 20 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20 s, not normally
c0cd0 66 6f 75 6e 64 20 69 6e 20 70 72 69 6e 74 66 28 found in printf(
c0ce0 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 ) */.#define etS
c0cf0 51 4c 45 53 43 41 50 45 20 20 31 30 20 2f 2a 20 QLESCAPE 10 /*
c0d00 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c 27 Strings with '\'
c0d10 27 20 64 6f 75 62 6c 65 64 2e 20 20 25 71 20 2a ' doubled. %q *
c0d20 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 /.#define etSQLE
c0d30 53 43 41 50 45 32 20 31 31 20 2f 2a 20 53 74 72 SCAPE2 11 /* Str
c0d40 69 6e 67 73 20 77 69 74 68 20 27 5c 27 27 20 64 ings with '\'' d
c0d50 6f 75 62 6c 65 64 20 61 6e 64 20 65 6e 63 6c 6f oubled and enclo
c0d60 73 65 64 20 69 6e 20 27 27 2c 0a 20 20 20 20 20 sed in '',.
c0d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c0d80 20 20 20 20 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 NULL pointe
c0d90 72 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 53 rs replaced by S
c0da0 51 4c 20 4e 55 4c 4c 2e 20 20 25 51 20 2a 2f 0a QL NULL. %Q */.
c0db0 23 64 65 66 69 6e 65 20 65 74 54 4f 4b 45 4e 20 #define etTOKEN
c0dc0 20 20 20 20 20 31 32 20 2f 2a 20 61 20 70 6f 69 12 /* a poi
c0dd0 6e 74 65 72 20 74 6f 20 61 20 54 6f 6b 65 6e 20 nter to a Token
c0de0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 structure */.#de
c0df0 66 69 6e 65 20 65 74 53 52 43 4c 49 53 54 20 20 fine etSRCLIST
c0e00 20 20 31 33 20 2f 2a 20 61 20 70 6f 69 6e 74 65 13 /* a pointe
c0e10 72 20 74 6f 20 61 20 53 72 63 4c 69 73 74 20 2a r to a SrcList *
c0e20 2f 0a 23 64 65 66 69 6e 65 20 65 74 50 4f 49 4e /.#define etPOIN
c0e30 54 45 52 20 20 20 20 31 34 20 2f 2a 20 54 68 65 TER 14 /* The
c0e40 20 25 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a %p conversion *
c0e50 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 /.#define etSQLE
c0e60 53 43 41 50 45 33 20 31 35 20 2f 2a 20 25 77 20 SCAPE3 15 /* %w
c0e70 2d 3e 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 -> Strings with
c0e80 27 5c 22 27 20 64 6f 75 62 6c 65 64 20 2a 2f 0a '\"' doubled */.
c0e90 23 64 65 66 69 6e 65 20 65 74 4f 52 44 49 4e 41 #define etORDINA
c0ea0 4c 20 20 20 20 31 36 20 2f 2a 20 25 72 20 2d 3e L 16 /* %r ->
c0eb0 20 31 73 74 2c 20 32 6e 64 2c 20 33 72 64 2c 20 1st, 2nd, 3rd,
c0ec0 34 74 68 2c 20 65 74 63 2e 20 20 45 6e 67 6c 69 4th, etc. Engli
c0ed0 73 68 20 6f 6e 6c 79 20 2a 2f 0a 0a 23 64 65 66 sh only */..#def
c0ee0 69 6e 65 20 65 74 49 4e 56 41 4c 49 44 20 20 20 ine etINVALID
c0ef0 20 20 30 20 2f 2a 20 41 6e 79 20 75 6e 72 65 63 0 /* Any unrec
c0f00 6f 67 6e 69 7a 65 64 20 63 6f 6e 76 65 72 73 69 ognized conversi
c0f10 6f 6e 20 74 79 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a on type */.../*.
c0f20 2a 2a 20 41 6e 20 22 65 74 42 79 74 65 22 20 69 ** An "etByte" i
c0f30 73 20 61 6e 20 38 2d 62 69 74 20 75 6e 73 69 67 s an 8-bit unsig
c0f40 6e 65 64 20 76 61 6c 75 65 2e 0a 2a 2f 0a 74 79 ned value..*/.ty
c0f50 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63 pedef unsigned c
c0f60 68 61 72 20 65 74 42 79 74 65 3b 0a 0a 2f 2a 0a har etByte;../*.
c0f70 2a 2a 20 45 61 63 68 20 62 75 69 6c 74 69 6e 20 ** Each builtin
c0f80 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61 72 61 conversion chara
c0f90 63 74 65 72 20 28 65 78 3a 20 74 68 65 20 27 64 cter (ex: the 'd
c0fa0 27 20 69 6e 20 22 25 64 22 29 20 69 73 20 64 65 ' in "%d") is de
c0fb0 73 63 72 69 62 65 64 0a 2a 2a 20 62 79 20 61 6e scribed.** by an
c0fc0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
c0fd0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
c0fe0 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ture.*/.typedef
c0ff0 73 74 72 75 63 74 20 65 74 5f 69 6e 66 6f 20 7b struct et_info {
c1000 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f /* Informatio
c1010 6e 20 61 62 6f 75 74 20 65 61 63 68 20 66 6f 72 n about each for
c1020 6d 61 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20 63 mat field */. c
c1030 68 61 72 20 66 6d 74 74 79 70 65 3b 20 20 20 20 har fmttype;
c1040 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
c1050 6f 72 6d 61 74 20 66 69 65 6c 64 20 63 6f 64 65 ormat field code
c1060 20 6c 65 74 74 65 72 20 2a 2f 0a 20 20 65 74 42 letter */. etB
c1070 79 74 65 20 62 61 73 65 3b 20 20 20 20 20 20 20 yte base;
c1080 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 61 73 /* The bas
c1090 65 20 66 6f 72 20 72 61 64 69 78 20 63 6f 6e 76 e for radix conv
c10a0 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 65 74 42 79 ersion */. etBy
c10b0 74 65 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 te flags;
c10c0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d /* One or m
c10d0 6f 72 65 20 6f 66 20 46 4c 41 47 5f 20 63 6f 6e ore of FLAG_ con
c10e0 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 2a 2f 0a stants below */.
c10f0 20 20 65 74 42 79 74 65 20 74 79 70 65 3b 20 20 etByte type;
c1100 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
c1110 6e 76 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67 nversion paradig
c1120 6d 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 63 68 m */. etByte ch
c1130 61 72 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 arset;
c1140 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 /* Offset into a
c1150 44 69 67 69 74 73 5b 5d 20 6f 66 20 74 68 65 20 Digits[] of the
c1160 64 69 67 69 74 73 20 73 74 72 69 6e 67 20 2a 2f digits string */
c1170 0a 20 20 65 74 42 79 74 65 20 70 72 65 66 69 78 . etByte prefix
c1180 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f ; /* O
c1190 66 66 73 65 74 20 69 6e 74 6f 20 61 50 72 65 66 ffset into aPref
c11a0 69 78 5b 5d 20 6f 66 20 74 68 65 20 70 72 65 66 ix[] of the pref
c11b0 69 78 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 65 ix string */.} e
c11c0 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 41 t_info;../*.** A
c11d0 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f llowed values fo
c11e0 72 20 65 74 5f 69 6e 66 6f 2e 66 6c 61 67 73 0a r et_info.flags.
c11f0 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f */.#define FLAG_
c1200 53 49 47 4e 45 44 20 20 31 20 20 20 20 20 2f 2a SIGNED 1 /*
c1210 20 54 72 75 65 20 69 66 20 74 68 65 20 76 61 6c True if the val
c1220 75 65 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 73 ue to convert is
c1230 20 73 69 67 6e 65 64 20 2a 2f 0a 23 64 65 66 69 signed */.#defi
c1240 6e 65 20 46 4c 41 47 5f 49 4e 54 45 52 4e 20 20 ne FLAG_INTERN
c1250 32 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 2 /* True if
c1260 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 for internal us
c1270 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e e only */.#defin
c1280 65 20 46 4c 41 47 5f 53 54 52 49 4e 47 20 20 34 e FLAG_STRING 4
c1290 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 69 6e /* Allow in
c12a0 66 69 6e 69 74 79 20 70 72 65 63 69 73 69 6f 6e finity precision
c12b0 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 */.../*.** The
c12c0 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 following table
c12d0 69 73 20 73 65 61 72 63 68 65 64 20 6c 69 6e 65 is searched line
c12e0 61 72 6c 79 2c 20 73 6f 20 69 74 20 69 73 20 67 arly, so it is g
c12f0 6f 6f 64 20 74 6f 20 70 75 74 20 74 68 65 0a 2a ood to put the.*
c1300 2a 20 6d 6f 73 74 20 66 72 65 71 75 65 6e 74 6c * most frequentl
c1310 79 20 75 73 65 64 20 63 6f 6e 76 65 72 73 69 6f y used conversio
c1320 6e 20 74 79 70 65 73 20 66 69 72 73 74 2e 0a 2a n types first..*
c1330 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 /.static const c
c1340 68 61 72 20 61 44 69 67 69 74 73 5b 5d 20 3d 20 har aDigits[] =
c1350 22 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 "0123456789ABCDE
c1360 46 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 F0123456789abcde
c1370 66 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 f";.static const
c1380 20 63 68 61 72 20 61 50 72 65 66 69 78 5b 5d 20 char aPrefix[]
c1390 3d 20 22 2d 78 30 5c 30 30 30 58 30 22 3b 0a 73 = "-x0\000X0";.s
c13a0 74 61 74 69 63 20 63 6f 6e 73 74 20 65 74 5f 69 tatic const et_i
c13b0 6e 66 6f 20 66 6d 74 69 6e 66 6f 5b 5d 20 3d 20 nfo fmtinfo[] =
c13c0 7b 0a 20 20 7b 20 20 27 64 27 2c 20 31 30 2c 20 {. { 'd', 10,
c13d0 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 1, etRADIX,
c13e0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
c13f0 73 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 54 52 s', 0, 4, etSTR
c1400 49 4e 47 2c 20 20 20 20 20 30 2c 20 20 30 20 7d ING, 0, 0 }
c1410 2c 0a 20 20 7b 20 20 27 67 27 2c 20 20 30 2c 20 ,. { 'g', 0,
c1420 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 1, etGENERIC,
c1430 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 30, 0 },. { '
c1440 7a 27 2c 20 20 30 2c 20 34 2c 20 65 74 44 59 4e z', 0, 4, etDYN
c1450 53 54 52 49 4e 47 2c 20 20 30 2c 20 20 30 20 7d STRING, 0, 0 }
c1460 2c 0a 20 20 7b 20 20 27 71 27 2c 20 20 30 2c 20 ,. { 'q', 0,
c1470 34 2c 20 65 74 53 51 4c 45 53 43 41 50 45 2c 20 4, etSQLESCAPE,
c1480 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
c1490 51 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c Q', 0, 4, etSQL
c14a0 45 53 43 41 50 45 32 2c 20 30 2c 20 20 30 20 7d ESCAPE2, 0, 0 }
c14b0 2c 0a 20 20 7b 20 20 27 77 27 2c 20 20 30 2c 20 ,. { 'w', 0,
c14c0 34 2c 20 65 74 53 51 4c 45 53 43 41 50 45 33 2c 4, etSQLESCAPE3,
c14d0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
c14e0 63 27 2c 20 20 30 2c 20 30 2c 20 65 74 43 48 41 c', 0, 0, etCHA
c14f0 52 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d RX, 0, 0 }
c1500 2c 0a 20 20 7b 20 20 27 6f 27 2c 20 20 38 2c 20 ,. { 'o', 8,
c1510 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 0, etRADIX,
c1520 20 30 2c 20 20 32 20 7d 2c 0a 20 20 7b 20 20 27 0, 2 },. { '
c1530 75 27 2c 20 31 30 2c 20 30 2c 20 65 74 52 41 44 u', 10, 0, etRAD
c1540 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d IX, 0, 0 }
c1550 2c 0a 20 20 7b 20 20 27 78 27 2c 20 31 36 2c 20 ,. { 'x', 16,
c1560 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 0, etRADIX,
c1570 20 31 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 20 27 16, 1 },. { '
c1580 58 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44 X', 16, 0, etRAD
c1590 49 58 2c 20 20 20 20 20 20 30 2c 20 20 34 20 7d IX, 0, 4 }
c15a0 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ,.#ifndef SQLITE
c15b0 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
c15c0 4f 49 4e 54 0a 20 20 7b 20 20 27 66 27 2c 20 20 OINT. { 'f',
c15d0 30 2c 20 31 2c 20 65 74 46 4c 4f 41 54 2c 20 20 0, 1, etFLOAT,
c15e0 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 0, 0 },. {
c15f0 20 20 27 65 27 2c 20 20 30 2c 20 31 2c 20 65 74 'e', 0, 1, et
c1600 45 58 50 2c 20 20 20 20 20 20 20 20 33 30 2c 20 EXP, 30,
c1610 30 20 7d 2c 0a 20 20 7b 20 20 27 45 27 2c 20 20 0 },. { 'E',
c1620 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20 0, 1, etEXP,
c1630 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a 20 20 7b 14, 0 },. {
c1640 20 20 27 47 27 2c 20 20 30 2c 20 31 2c 20 65 74 'G', 0, 1, et
c1650 47 45 4e 45 52 49 43 2c 20 20 20 20 31 34 2c 20 GENERIC, 14,
c1660 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 7b 20 0 },.#endif. {
c1670 20 27 69 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 'i', 10, 1, etR
c1680 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 ADIX, 0, 0
c1690 20 7d 2c 0a 20 20 7b 20 20 27 6e 27 2c 20 20 30 },. { 'n', 0
c16a0 2c 20 30 2c 20 65 74 53 49 5a 45 2c 20 20 20 20 , 0, etSIZE,
c16b0 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 0, 0 },. {
c16c0 20 27 25 27 2c 20 20 30 2c 20 30 2c 20 65 74 50 '%', 0, 0, etP
c16d0 45 52 43 45 4e 54 2c 20 20 20 20 30 2c 20 20 30 ERCENT, 0, 0
c16e0 20 7d 2c 0a 20 20 7b 20 20 27 70 27 2c 20 31 36 },. { 'p', 16
c16f0 2c 20 30 2c 20 65 74 50 4f 49 4e 54 45 52 2c 20 , 0, etPOINTER,
c1700 20 20 20 30 2c 20 20 31 20 7d 2c 0a 0a 2f 2a 20 0, 1 },../*
c1710 41 6c 6c 20 74 68 65 20 72 65 73 74 20 68 61 76 All the rest hav
c1720 65 20 74 68 65 20 46 4c 41 47 5f 49 4e 54 45 52 e the FLAG_INTER
c1730 4e 20 62 69 74 20 73 65 74 20 61 6e 64 20 61 72 N bit set and ar
c1740 65 20 74 68 75 73 20 66 6f 72 20 69 6e 74 65 72 e thus for inter
c1750 6e 61 6c 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 nal.** use only
c1760 2a 2f 0a 20 20 7b 20 20 27 54 27 2c 20 20 30 2c */. { 'T', 0,
c1770 20 32 2c 20 65 74 54 4f 4b 45 4e 2c 20 20 20 20 2, etTOKEN,
c1780 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 0, 0 },. {
c1790 27 53 27 2c 20 20 30 2c 20 32 2c 20 65 74 53 52 'S', 0, 2, etSR
c17a0 43 4c 49 53 54 2c 20 20 20 20 30 2c 20 20 30 20 CLIST, 0, 0
c17b0 7d 2c 0a 20 20 7b 20 20 27 72 27 2c 20 31 30 2c },. { 'r', 10,
c17c0 20 33 2c 20 65 74 4f 52 44 49 4e 41 4c 2c 20 20 3, etORDINAL,
c17d0 20 20 30 2c 20 20 30 20 7d 2c 0a 7d 3b 0a 0a 2f 0, 0 },.};../
c17e0 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f *.** If SQLITE_O
c17f0 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
c1800 4e 54 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 NT is defined, t
c1810 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 hen none of the
c1820 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a floating point.*
c1830 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69 * conversions wi
c1840 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 6e ll work..*/.#ifn
c1850 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
c1860 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f FLOATING_POINT./
c1870 2a 0a 2a 2a 20 22 2a 76 61 6c 22 20 69 73 20 61 *.** "*val" is a
c1880 20 64 6f 75 62 6c 65 20 73 75 63 68 20 74 68 61 double such tha
c1890 74 20 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c 20 t 0.1 <= *val <
c18a0 31 30 2e 30 0a 2a 2a 20 52 65 74 75 72 6e 20 74 10.0.** Return t
c18b0 68 65 20 61 73 63 69 69 20 63 6f 64 65 20 66 6f he ascii code fo
c18c0 72 20 74 68 65 20 6c 65 61 64 69 6e 67 20 64 69 r the leading di
c18d0 67 69 74 20 6f 66 20 2a 76 61 6c 2c 20 74 68 65 git of *val, the
c18e0 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 22 2a n.** multiply "*
c18f0 76 61 6c 22 20 62 79 20 31 30 2e 30 20 74 6f 20 val" by 10.0 to
c1900 72 65 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a renormalize..**.
c1910 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 20 ** Example:.**
c1920 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20 2a 76 input: *v
c1930 61 6c 20 3d 20 33 2e 31 34 31 35 39 0a 2a 2a 20 al = 3.14159.**
c1940 20 20 20 20 6f 75 74 70 75 74 3a 20 20 20 20 2a output: *
c1950 76 61 6c 20 3d 20 31 2e 34 31 35 39 20 20 20 20 val = 1.4159
c1960 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 20 function return
c1970 3d 20 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20 = '3'.**.** The
c1980 63 6f 75 6e 74 65 72 20 2a 63 6e 74 20 69 73 20 counter *cnt is
c1990 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68 incremented each
c19a0 20 74 69 6d 65 2e 20 20 41 66 74 65 72 20 63 6f time. After co
c19b0 75 6e 74 65 72 20 65 78 63 65 65 64 73 0a 2a 2a unter exceeds.**
c19c0 20 31 36 20 28 74 68 65 20 6e 75 6d 62 65 72 20 16 (the number
c19d0 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 of significant d
c19e0 69 67 69 74 73 20 69 6e 20 61 20 36 34 2d 62 69 igits in a 64-bi
c19f0 74 20 66 6c 6f 61 74 29 20 27 30 27 20 69 73 0a t float) '0' is.
c1a00 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e ** always return
c1a10 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 ed..*/.static ch
c1a20 61 72 20 65 74 5f 67 65 74 64 69 67 69 74 28 4c ar et_getdigit(L
c1a30 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a ONGDOUBLE_TYPE *
c1a40 76 61 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a val, int *cnt){.
c1a50 20 20 69 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c int digit;. L
c1a60 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 64 ONGDOUBLE_TYPE d
c1a70 3b 0a 20 20 69 66 28 20 28 2a 63 6e 74 29 3c 3d ;. if( (*cnt)<=
c1a80 30 20 29 20 72 65 74 75 72 6e 20 27 30 27 3b 0a 0 ) return '0';.
c1a90 20 20 28 2a 63 6e 74 29 2d 2d 3b 0a 20 20 64 69 (*cnt)--;. di
c1aa0 67 69 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c 3b git = (int)*val;
c1ab0 0a 20 20 64 20 3d 20 64 69 67 69 74 3b 0a 20 20 . d = digit;.
c1ac0 64 69 67 69 74 20 2b 3d 20 27 30 27 3b 0a 20 20 digit += '0';.
c1ad0 2a 76 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20 64 *val = (*val - d
c1ae0 29 2a 31 30 2e 30 3b 0a 20 20 72 65 74 75 72 6e )*10.0;. return
c1af0 20 28 63 68 61 72 29 64 69 67 69 74 3b 0a 7d 0a (char)digit;.}.
c1b00 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
c1b10 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
c1b20 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 OINT */../*.** A
c1b30 70 70 65 6e 64 20 4e 20 73 70 61 63 65 20 63 68 ppend N space ch
c1b40 61 72 61 63 74 65 72 73 20 74 6f 20 74 68 65 20 aracters to the
c1b50 67 69 76 65 6e 20 73 74 72 69 6e 67 20 62 75 66 given string buf
c1b60 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 fer..*/.SQLITE_P
c1b70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
c1b80 74 65 33 41 70 70 65 6e 64 53 70 61 63 65 28 53 te3AppendSpace(S
c1b90 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c trAccum *pAccum,
c1ba0 20 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61 74 69 int N){. stati
c1bb0 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 70 c const char zSp
c1bc0 61 63 65 73 5b 5d 20 3d 20 22 20 20 20 20 20 20 aces[] = "
c1bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1be0 20 20 20 20 20 20 20 22 3b 0a 20 20 77 68 69 6c ";. whil
c1bf0 65 28 20 4e 3e 3d 28 69 6e 74 29 73 69 7a 65 6f e( N>=(int)sizeo
c1c00 66 28 7a 53 70 61 63 65 73 29 2d 31 20 29 7b 0a f(zSpaces)-1 ){.
c1c10 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
c1c20 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
c1c30 2c 20 7a 53 70 61 63 65 73 2c 20 73 69 7a 65 6f , zSpaces, sizeo
c1c40 66 28 7a 53 70 61 63 65 73 29 2d 31 29 3b 0a 20 f(zSpaces)-1);.
c1c50 20 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 7a N -= sizeof(z
c1c60 53 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d 0a 20 Spaces)-1;. }.
c1c70 20 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20 if( N>0 ){.
c1c80 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 sqlite3StrAccumA
c1c90 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53 ppend(pAccum, zS
c1ca0 70 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a 7d paces, N);. }.}
c1cb0 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68 69 ../*.** On machi
c1cc0 6e 65 73 20 77 69 74 68 20 61 20 73 6d 61 6c 6c nes with a small
c1cd0 20 73 74 61 63 6b 20 73 69 7a 65 2c 20 79 6f 75 stack size, you
c1ce0 20 63 61 6e 20 72 65 64 65 66 69 6e 65 20 74 68 can redefine th
c1cf0 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 49 4e e.** SQLITE_PRIN
c1d00 54 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20 62 65 T_BUF_SIZE to be
c1d10 20 73 6f 6d 65 74 68 69 6e 67 20 73 6d 61 6c 6c something small
c1d20 65 72 2c 20 69 66 20 64 65 73 69 72 65 64 2e 0a er, if desired..
c1d30 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
c1d40 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 E_PRINT_BUF_SIZE
c1d50 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
c1d60 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 _PRINT_BUF_SIZE
c1d70 37 30 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 70.#endif.#defin
c1d80 65 20 65 74 42 55 46 53 49 5a 45 20 53 51 4c 49 e etBUFSIZE SQLI
c1d90 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a TE_PRINT_BUF_SIZ
c1da0 45 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 E /* Size of th
c1db0 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 e output buffer
c1dc0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6e 64 65 72 */../*.** Render
c1dd0 20 61 20 73 74 72 69 6e 67 20 67 69 76 65 6e 20 a string given
c1de0 62 79 20 22 66 6d 74 22 20 69 6e 74 6f 20 74 68 by "fmt" into th
c1df0 65 20 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 63 e StrAccum objec
c1e00 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
c1e10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
c1e20 33 56 58 50 72 69 6e 74 66 28 0a 20 20 53 74 72 3VXPrintf(. Str
c1e30 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 20 Accum *pAccum,
c1e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1e50 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 72 65 /* Accumulate re
c1e60 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 sults here */.
c1e70 69 6e 74 20 75 73 65 45 78 74 65 6e 64 65 64 2c int useExtended,
c1e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1e90 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65 /* Allow exte
c1ea0 6e 64 65 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f nded %-conversio
c1eb0 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ns */. const ch
c1ec0 61 72 20 2a 66 6d 74 2c 20 20 20 20 20 20 20 20 ar *fmt,
c1ed0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f /* Fo
c1ee0 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 rmat string */.
c1ef0 20 76 61 5f 6c 69 73 74 20 61 70 20 20 20 20 20 va_list ap
c1f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1f10 20 20 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73 /* arguments
c1f20 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20 */.){. int c;
c1f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1f40 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 61 72 /* Next char
c1f50 61 63 74 65 72 20 69 6e 20 74 68 65 20 66 6f 72 acter in the for
c1f60 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 mat string */.
c1f70 63 68 61 72 20 2a 62 75 66 70 74 3b 20 20 20 20 char *bufpt;
c1f80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
c1f90 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e inter to the con
c1fa0 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a version buffer *
c1fb0 2f 0a 20 20 69 6e 74 20 70 72 65 63 69 73 69 6f /. int precisio
c1fc0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f n; /
c1fd0 2a 20 50 72 65 63 69 73 69 6f 6e 20 6f 66 20 74 * Precision of t
c1fe0 68 65 20 63 75 72 72 65 6e 74 20 66 69 65 6c 64 he current field
c1ff0 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68 */. int length
c2000 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
c2010 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 /* Length of th
c2020 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 e field */. int
c2030 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 idx;
c2040 20 20 20 20 20 20 20 20 2f 2a 20 41 20 67 65 6e /* A gen
c2050 65 72 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f eral purpose loo
c2060 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 p counter */. i
c2070 6e 74 20 77 69 64 74 68 3b 20 20 20 20 20 20 20 nt width;
c2080 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69 64 /* Wid
c2090 74 68 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e th of the curren
c20a0 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42 t field */. etB
c20b0 79 74 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 yte flag_leftjus
c20c0 74 69 66 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 tify; /* True
c20d0 69 66 20 22 2d 22 20 66 6c 61 67 20 69 73 20 70 if "-" flag is p
c20e0 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 resent */. etBy
c20f0 74 65 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e te flag_plussign
c2100 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 ; /* True i
c2110 66 20 22 2b 22 20 66 6c 61 67 20 69 73 20 70 72 f "+" flag is pr
c2120 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 esent */. etByt
c2130 65 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e e flag_blanksign
c2140 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 ; /* True if
c2150 20 22 20 22 20 66 6c 61 67 20 69 73 20 70 72 65 " " flag is pre
c2160 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 sent */. etByte
c2170 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 flag_alternatef
c2180 6f 72 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 orm; /* True if
c2190 22 23 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 "#" flag is pres
c21a0 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 ent */. etByte
c21b0 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 flag_altform2;
c21c0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 /* True if "
c21d0 21 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 !" flag is prese
c21e0 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 nt */. etByte f
c21f0 6c 61 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20 lag_zeropad;
c2200 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 /* True if fi
c2210 65 6c 64 20 77 69 64 74 68 20 63 6f 6e 73 74 61 eld width consta
c2220 6e 74 20 73 74 61 72 74 73 20 77 69 74 68 20 7a nt starts with z
c2230 65 72 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 ero */. etByte
c2240 66 6c 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 flag_long;
c2250 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 /* True if "
c2260 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 l" flag is prese
c2270 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 nt */. etByte f
c2280 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 lag_longlong;
c2290 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
c22a0 65 20 22 6c 6c 22 20 66 6c 61 67 20 69 73 20 70 e "ll" flag is p
c22b0 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 resent */. etBy
c22c0 74 65 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 te done;
c22d0 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 /* Loop t
c22e0 65 72 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20 ermination flag
c22f0 2a 2f 0a 20 20 65 74 42 79 74 65 20 78 74 79 70 */. etByte xtyp
c2300 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 e = 0;
c2310 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 /* Conversion pa
c2320 72 61 64 69 67 6d 20 2a 2f 0a 20 20 63 68 61 72 radigm */. char
c2330 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 prefix;
c2340 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 69 78 /* Prefix
c2350 20 63 68 61 72 61 63 74 65 72 2e 20 20 22 2b 22 character. "+"
c2360 20 6f 72 20 22 2d 22 20 6f 72 20 22 20 22 20 6f or "-" or " " o
c2370 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20 73 71 6c r '\0'. */. sql
c2380 69 74 65 5f 75 69 6e 74 36 34 20 6c 6f 6e 67 76 ite_uint64 longv
c2390 61 6c 75 65 3b 20 20 20 2f 2a 20 56 61 6c 75 65 alue; /* Value
c23a0 20 66 6f 72 20 69 6e 74 65 67 65 72 20 74 79 70 for integer typ
c23b0 65 73 20 2a 2f 0a 20 20 4c 4f 4e 47 44 4f 55 42 es */. LONGDOUB
c23c0 4c 45 5f 54 59 50 45 20 72 65 61 6c 76 61 6c 75 LE_TYPE realvalu
c23d0 65 3b 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 e; /* Value for
c23e0 72 65 61 6c 20 74 79 70 65 73 20 2a 2f 0a 20 20 real types */.
c23f0 63 6f 6e 73 74 20 65 74 5f 69 6e 66 6f 20 2a 69 const et_info *i
c2400 6e 66 6f 70 3b 20 20 20 20 20 20 2f 2a 20 50 6f nfop; /* Po
c2410 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70 inter to the app
c2420 72 6f 70 72 69 61 74 65 20 69 6e 66 6f 20 73 74 ropriate info st
c2430 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 63 68 61 ructure */. cha
c2440 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 r *zOut;
c2450 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6e 64 65 /* Rende
c2460 72 69 6e 67 20 62 75 66 66 65 72 20 2a 2f 0a 20 ring buffer */.
c2470 20 69 6e 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 int nOut;
c2480 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
c2490 69 7a 65 20 6f 66 20 74 68 65 20 72 65 6e 64 65 ize of the rende
c24a0 72 69 6e 67 20 62 75 66 66 65 72 20 2a 2f 0a 20 ring buffer */.
c24b0 20 63 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20 char *zExtra;
c24c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
c24d0 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 20 75 alloced memory u
c24e0 73 65 64 20 62 79 20 73 6f 6d 65 20 63 6f 6e 76 sed by some conv
c24f0 65 72 73 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 ersion */.#ifnde
c2500 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
c2510 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 69 OATING_POINT. i
c2520 6e 74 20 20 65 78 70 2c 20 65 32 3b 20 20 20 20 nt exp, e2;
c2530 20 20 20 20 20 20 20 20 20 20 2f 2a 20 65 78 70 /* exp
c2540 6f 6e 65 6e 74 20 6f 66 20 72 65 61 6c 20 6e 75 onent of real nu
c2550 6d 62 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e mbers */. int n
c2560 73 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 sd;
c2570 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
c2580 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 of significant d
c2590 69 67 69 74 73 20 72 65 74 75 72 6e 65 64 20 2a igits returned *
c25a0 2f 0a 20 20 64 6f 75 62 6c 65 20 72 6f 75 6e 64 /. double round
c25b0 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f er; /
c25c0 2a 20 55 73 65 64 20 66 6f 72 20 72 6f 75 6e 64 * Used for round
c25d0 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 ing floating poi
c25e0 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 65 nt values */. e
c25f0 74 42 79 74 65 20 66 6c 61 67 5f 64 70 3b 20 20 tByte flag_dp;
c2600 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
c2610 65 20 69 66 20 64 65 63 69 6d 61 6c 20 70 6f 69 e if decimal poi
c2620 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 73 68 6f nt should be sho
c2630 77 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 wn */. etByte f
c2640 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20 20 20 20 lag_rtz;
c2650 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 72 /* True if tr
c2660 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 73 68 6f ailing zeros sho
c2670 75 6c 64 20 62 65 20 72 65 6d 6f 76 65 64 20 2a uld be removed *
c2680 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68 61 72 20 /.#endif. char
c2690 62 75 66 5b 65 74 42 55 46 53 49 5a 45 5d 3b 20 buf[etBUFSIZE];
c26a0 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 /* Convers
c26b0 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 0a 20 ion buffer */..
c26c0 20 62 75 66 70 74 20 3d 20 30 3b 0a 20 20 66 6f bufpt = 0;. fo
c26d0 72 28 3b 20 28 63 3d 28 2a 66 6d 74 29 29 21 3d r(; (c=(*fmt))!=
c26e0 30 3b 20 2b 2b 66 6d 74 29 7b 0a 20 20 20 20 69 0; ++fmt){. i
c26f0 66 28 20 63 21 3d 27 25 27 20 29 7b 0a 20 20 20 f( c!='%' ){.
c2700 20 20 20 69 6e 74 20 61 6d 74 3b 0a 20 20 20 20 int amt;.
c2710 20 20 62 75 66 70 74 20 3d 20 28 63 68 61 72 20 bufpt = (char
c2720 2a 29 66 6d 74 3b 0a 20 20 20 20 20 20 61 6d 74 *)fmt;. amt
c2730 20 3d 20 31 3b 0a 20 20 20 20 20 20 77 68 69 6c = 1;. whil
c2740 65 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 e( (c=(*++fmt))!
c2750 3d 27 25 27 20 26 26 20 63 21 3d 30 20 29 20 61 ='%' && c!=0 ) a
c2760 6d 74 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 mt++;. sqli
c2770 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
c2780 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c d(pAccum, bufpt,
c2790 20 61 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 amt);. if(
c27a0 20 63 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 c==0 ) break;.
c27b0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 63 3d }. if( (c=
c27c0 28 2a 2b 2b 66 6d 74 29 29 3d 3d 30 20 29 7b 0a (*++fmt))==0 ){.
c27d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
c27e0 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 AccumAppend(pAcc
c27f0 75 6d 2c 20 22 25 22 2c 20 31 29 3b 0a 20 20 20 um, "%", 1);.
c2800 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
c2810 20 20 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 /* Find out
c2820 77 68 61 74 20 66 6c 61 67 73 20 61 72 65 20 70 what flags are p
c2830 72 65 73 65 6e 74 20 2a 2f 0a 20 20 20 20 66 6c resent */. fl
c2840 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d ag_leftjustify =
c2850 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d flag_plussign =
c2860 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 flag_blanksign
c2870 3d 20 0a 20 20 20 20 20 66 6c 61 67 5f 61 6c 74 = . flag_alt
c2880 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 66 6c 61 ernateform = fla
c2890 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 66 6c 61 g_altform2 = fla
c28a0 67 5f 7a 65 72 6f 70 61 64 20 3d 20 30 3b 0a 20 g_zeropad = 0;.
c28b0 20 20 20 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 done = 0;.
c28c0 20 64 6f 7b 0a 20 20 20 20 20 20 73 77 69 74 63 do{. switc
c28d0 68 28 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 h( c ){.
c28e0 63 61 73 65 20 27 2d 27 3a 20 20 20 66 6c 61 67 case '-': flag
c28f0 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 _leftjustify = 1
c2900 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 ; break;.
c2910 20 20 20 20 20 63 61 73 65 20 27 2b 27 3a 20 20 case '+':
c2920 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d flag_plussign =
c2930 20 31 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 1; break
c2940 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
c2950 20 27 3a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b ': flag_blank
c2960 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 sign = 1;
c2970 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
c2980 61 73 65 20 27 23 27 3a 20 20 20 66 6c 61 67 5f ase '#': flag_
c2990 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 alternateform =
c29a0 31 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 1; break;.
c29b0 20 20 20 20 63 61 73 65 20 27 21 27 3a 20 20 20 case '!':
c29c0 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 flag_altform2 =
c29d0 31 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 1; break;
c29e0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 30 . case '0
c29f0 27 3a 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 ': flag_zeropa
c2a00 64 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 62 d = 1; b
c2a10 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 reak;. de
c2a20 66 61 75 6c 74 3a 20 20 20 20 64 6f 6e 65 20 3d fault: done =
c2a30 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
c2a40 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
c2a50 20 7d 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 21 }. }while( !
c2a60 64 6f 6e 65 20 26 26 20 28 63 3d 28 2a 2b 2b 66 done && (c=(*++f
c2a70 6d 74 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 2f mt))!=0 );. /
c2a80 2a 20 47 65 74 20 74 68 65 20 66 69 65 6c 64 20 * Get the field
c2a90 77 69 64 74 68 20 2a 2f 0a 20 20 20 20 77 69 64 width */. wid
c2aa0 74 68 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 th = 0;. if(
c2ab0 63 3d 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 c=='*' ){.
c2ac0 77 69 64 74 68 20 3d 20 76 61 5f 61 72 67 28 61 width = va_arg(a
c2ad0 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 p,int);. if
c2ae0 28 20 77 69 64 74 68 3c 30 20 29 7b 0a 20 20 20 ( width<0 ){.
c2af0 20 20 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 flag_leftju
c2b00 73 74 69 66 79 20 3d 20 31 3b 0a 20 20 20 20 20 stify = 1;.
c2b10 20 20 20 77 69 64 74 68 20 3d 20 2d 77 69 64 74 width = -widt
c2b20 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 h;. }.
c2b30 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 c = *++fmt;.
c2b40 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 }else{. wh
c2b50 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63 ile( c>='0' && c
c2b60 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 <='9' ){.
c2b70 20 77 69 64 74 68 20 3d 20 77 69 64 74 68 2a 31 width = width*1
c2b80 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 0 + c - '0';.
c2b90 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b c = *++fmt;
c2ba0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
c2bb0 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 70 72 /* Get the pr
c2bc0 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 ecision */. i
c2bd0 66 28 20 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 f( c=='.' ){.
c2be0 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 30 precision = 0
c2bf0 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 ;. c = *++f
c2c00 6d 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d mt;. if( c=
c2c10 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 ='*' ){.
c2c20 70 72 65 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61 precision = va_a
c2c30 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 rg(ap,int);.
c2c40 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f if( precisio
c2c50 6e 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 n<0 ) precision
c2c60 3d 20 2d 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 = -precision;.
c2c70 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 c = *++fmt
c2c80 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
c2c90 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e while( c>
c2ca0 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 ='0' && c<='9' )
c2cb0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63 {. prec
c2cc0 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f ision = precisio
c2cd0 6e 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a n*10 + c - '0';.
c2ce0 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b c = *+
c2cf0 2b 66 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a +fmt;. }.
c2d00 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
c2d10 65 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 e{. precisi
c2d20 6f 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 on = -1;. }.
c2d30 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f /* Get the co
c2d40 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f nversion type mo
c2d50 64 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 66 difier */. if
c2d60 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 ( c=='l' ){.
c2d70 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b flag_long = 1;
c2d80 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d . c = *++fm
c2d90 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d t;. if( c==
c2da0 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66 'l' ){. f
c2db0 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31 lag_longlong = 1
c2dc0 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b ;. c = *+
c2dd0 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 +fmt;. }els
c2de0 65 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f e{. flag_
c2df0 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 longlong = 0;.
c2e00 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
c2e10 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 . flag_long
c2e20 20 3d 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 = flag_longlong
c2e30 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
c2e40 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 66 /* Fetch the inf
c2e50 6f 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 o entry for the
c2e60 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66 field */. inf
c2e70 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 30 5d op = &fmtinfo[0]
c2e80 3b 0a 20 20 20 20 78 74 79 70 65 20 3d 20 65 74 ;. xtype = et
c2e90 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 66 6f 72 INVALID;. for
c2ea0 28 69 64 78 3d 30 3b 20 69 64 78 3c 41 72 72 61 (idx=0; idx<Arra
c2eb0 79 53 69 7a 65 28 66 6d 74 69 6e 66 6f 29 3b 20 ySize(fmtinfo);
c2ec0 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 idx++){. if
c2ed0 28 20 63 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64 78 ( c==fmtinfo[idx
c2ee0 5d 2e 66 6d 74 74 79 70 65 20 29 7b 0a 20 20 20 ].fmttype ){.
c2ef0 20 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d infop = &fm
c2f00 74 69 6e 66 6f 5b 69 64 78 5d 3b 0a 20 20 20 20 tinfo[idx];.
c2f10 20 20 20 20 69 66 28 20 75 73 65 45 78 74 65 6e if( useExten
c2f20 64 65 64 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66 ded || (infop->f
c2f30 6c 61 67 73 20 26 20 46 4c 41 47 5f 49 4e 54 45 lags & FLAG_INTE
c2f40 52 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 RN)==0 ){.
c2f50 20 20 20 20 78 74 79 70 65 20 3d 20 69 6e 66 6f xtype = info
c2f60 70 2d 3e 74 79 70 65 3b 0a 20 20 20 20 20 20 20 p->type;.
c2f70 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
c2f80 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 return;.
c2f90 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 }. brea
c2fa0 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
c2fb0 0a 20 20 20 20 7a 45 78 74 72 61 20 3d 20 30 3b . zExtra = 0;
c2fc0 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 .. /*. **
c2fd0 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 76 At this point, v
c2fe0 61 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e 69 ariables are ini
c2ff0 74 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c 6c tialized as foll
c3000 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ows:. **.
c3010 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e ** flag_altern
c3020 61 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20 20 ateform
c3030 20 54 52 55 45 20 69 66 20 61 20 27 23 27 20 69 TRUE if a '#' i
c3040 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a s present.. *
c3050 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d * flag_altform
c3060 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2
c3070 54 52 55 45 20 69 66 20 61 20 27 21 27 20 69 73 TRUE if a '!' is
c3080 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a present.. **
c3090 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e flag_plussign
c30a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 T
c30b0 52 55 45 20 69 66 20 61 20 27 2b 27 20 69 73 20 RUE if a '+' is
c30c0 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 present.. **
c30d0 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 flag_leftjusti
c30e0 66 79 20 20 20 20 20 20 20 20 20 20 20 20 54 52 fy TR
c30f0 55 45 20 69 66 20 61 20 27 2d 27 20 69 73 20 70 UE if a '-' is p
c3100 72 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68 65 resent or if the
c3110 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 . **
c3120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3130 20 20 20 20 20 20 66 69 65 6c 64 20 77 69 64 74 field widt
c3140 68 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e 0a h was negative..
c3150 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a 65 ** flag_ze
c3160 72 6f 70 61 64 20 20 20 20 20 20 20 20 20 20 20 ropad
c3170 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 TRUE if the
c3180 20 77 69 64 74 68 20 62 65 67 61 6e 20 77 69 74 width began wit
c3190 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c h 0.. ** fl
c31a0 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 ag_long
c31b0 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 TRUE i
c31c0 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c 27 f the letter 'l'
c31d0 20 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a (ell) prefixed.
c31e0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
c31f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3200 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 the convers
c3210 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 ion character..
c3220 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e ** flag_lon
c3230 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20 glong
c3240 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20 TRUE if the
c3250 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c 6c letter 'll' (ell
c3260 20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20 ell) prefixed.
c3270 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **
c3280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3290 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 the conversi
c32a0 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 on character..
c32b0 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61 6e ** flag_blan
c32c0 6b 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 ksign
c32d0 20 20 20 54 52 55 45 20 69 66 20 61 20 27 20 27 TRUE if a ' '
c32e0 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 is present..
c32f0 20 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20 20 ** width
c3300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3310 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20 The specified
c3320 66 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54 68 field width. Th
c3330 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 20 is is. **
c3340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3350 20 20 20 20 20 20 20 20 20 20 20 61 6c 77 61 79 alway
c3360 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 s non-negative.
c3370 20 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65 66 Zero is the def
c3380 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20 70 ault.. ** p
c3390 72 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20 20 recision
c33a0 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 73 The s
c33b0 70 65 63 69 66 69 65 64 20 70 72 65 63 69 73 69 pecified precisi
c33c0 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 on. The default
c33d0 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 . **
c33e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c33f0 20 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20 20 is -1..
c3400 20 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20 20 ** xtype
c3410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3420 20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20 74 The class of t
c3430 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 20 he conversion..
c3440 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20 20 ** infop
c3450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3460 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 Pointer to t
c3470 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 he appropriate i
c3480 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20 20 nfo struct..
c3490 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20 78 */. switch( x
c34a0 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 type ){. ca
c34b0 73 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20 20 se etPOINTER:.
c34c0 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c flag_longl
c34d0 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 ong = sizeof(cha
c34e0 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34 29 r*)==sizeof(i64)
c34f0 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c ;. flag_l
c3500 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 ong = sizeof(cha
c3510 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e 67 r*)==sizeof(long
c3520 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 2f int);. /
c3530 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 * Fall through i
c3540 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 nto the next cas
c3550 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 e */. case
c3560 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20 20 etORDINAL:.
c3570 20 63 61 73 65 20 65 74 52 41 44 49 58 3a 0a 20 case etRADIX:.
c3580 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 70 if( infop
c3590 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 ->flags & FLAG_S
c35a0 49 47 4e 45 44 20 29 7b 0a 20 20 20 20 20 20 20 IGNED ){.
c35b0 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 20 i64 v;.
c35c0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e if( flag_lon
c35d0 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 glong ){.
c35e0 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 v = va_arg(
c35f0 61 70 2c 69 36 34 29 3b 0a 20 20 20 20 20 20 20 ap,i64);.
c3600 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 }else if( fla
c3610 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 g_long ){.
c3620 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 v = va_arg
c3630 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 (ap,long int);.
c3640 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }else{.
c3650 20 20 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 v =
c3660 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a va_arg(ap,int);.
c3670 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
c3680 20 20 20 20 20 20 69 66 28 20 76 3c 30 20 29 7b if( v<0 ){
c3690 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
c36a0 20 76 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 v==SMALLEST_INT
c36b0 36 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 ){.
c36c0 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 longvalue =
c36d0 28 28 75 36 34 29 31 29 3c 3c 36 33 3b 0a 20 20 ((u64)1)<<63;.
c36e0 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
c36f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c . l
c3700 6f 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 ongvalue = -v;.
c3710 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
c3720 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 prefix
c3730 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20 = '-';.
c3740 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
c3750 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 longvalue =
c3760 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 v;. i
c3770 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e f( flag_plussign
c3780 20 29 20 20 20 20 20 20 20 20 70 72 65 66 69 78 ) prefix
c3790 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 = '+';.
c37a0 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 else if( fla
c37b0 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70 g_blanksign ) p
c37c0 72 65 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 refix = ' ';.
c37d0 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20 else
c37e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c37f0 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a prefix = 0;.
c3800 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
c3810 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
c3820 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f if( flag_lo
c3830 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 nglong ){.
c3840 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 longvalue
c3850 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 36 34 29 = va_arg(ap,u64)
c3860 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
c3870 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 e if( flag_long
c3880 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c ){. l
c3890 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 ongvalue = va_ar
c38a0 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 6c 6f g(ap,unsigned lo
c38b0 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 ng int);.
c38c0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
c38d0 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 longvalue
c38e0 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 = va_arg(ap,unsi
c38f0 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20 20 20 20 gned int);.
c3900 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
c3910 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 prefix = 0;.
c3920 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
c3930 66 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30 20 f( longvalue==0
c3940 29 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 ) flag_alternate
c3950 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 form = 0;.
c3960 20 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 if( flag_zerop
c3970 61 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c ad && precision<
c3980 77 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 width-(prefix!=0
c3990 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
c39a0 72 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74 68 recision = width
c39b0 2d 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20 20 -(prefix!=0);.
c39c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
c39d0 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 65 74 if( precision<et
c39e0 42 55 46 53 49 5a 45 2d 31 30 20 29 7b 0a 20 20 BUFSIZE-10 ){.
c39f0 20 20 20 20 20 20 20 20 6e 4f 75 74 20 3d 20 65 nOut = e
c3a00 74 42 55 46 53 49 5a 45 3b 0a 20 20 20 20 20 20 tBUFSIZE;.
c3a10 20 20 20 20 7a 4f 75 74 20 3d 20 62 75 66 3b 0a zOut = buf;.
c3a20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
c3a30 20 20 20 20 20 20 20 20 20 6e 4f 75 74 20 3d 20 nOut =
c3a40 70 72 65 63 69 73 69 6f 6e 20 2b 20 31 30 3b 0a precision + 10;.
c3a50 20 20 20 20 20 20 20 20 20 20 7a 4f 75 74 20 3d zOut =
c3a60 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74 65 zExtra = sqlite
c3a70 33 4d 61 6c 6c 6f 63 28 20 6e 4f 75 74 20 29 3b 3Malloc( nOut );
c3a80 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a . if( z
c3a90 4f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Out==0 ){.
c3aa0 20 20 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 61 pAccum->ma
c3ab0 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a llocFailed = 1;.
c3ac0 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 retu
c3ad0 72 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a rn;. }.
c3ae0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
c3af0 20 20 62 75 66 70 74 20 3d 20 26 7a 4f 75 74 5b bufpt = &zOut[
c3b00 6e 4f 75 74 2d 31 5d 3b 0a 20 20 20 20 20 20 20 nOut-1];.
c3b10 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 4f 52 if( xtype==etOR
c3b20 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 DINAL ){.
c3b30 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
c3b40 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d 20 22 74 char zOrd[] = "t
c3b50 68 73 74 6e 64 72 64 22 3b 0a 20 20 20 20 20 20 hstndrd";.
c3b60 20 20 20 20 69 6e 74 20 78 20 3d 20 28 69 6e 74 int x = (int
c3b70 29 28 6c 6f 6e 67 76 61 6c 75 65 20 25 20 31 30 )(longvalue % 10
c3b80 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
c3b90 20 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e 67 76 61 x>=4 || (longva
c3ba0 6c 75 65 2f 31 30 29 25 31 30 3d 3d 31 20 29 7b lue/10)%10==1 ){
c3bb0 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 20 3d . x =
c3bc0 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0;. }.
c3bd0 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 *(--bu
c3be0 66 70 74 29 20 3d 20 7a 4f 72 64 5b 78 2a 32 2b fpt) = zOrd[x*2+
c3bf0 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 1];. *(
c3c00 2d 2d 62 75 66 70 74 29 20 3d 20 7a 4f 72 64 5b --bufpt) = zOrd[
c3c10 78 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a x*2];. }.
c3c20 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 {.
c3c30 20 20 20 20 72 65 67 69 73 74 65 72 20 63 6f 6e register con
c3c40 73 74 20 63 68 61 72 20 2a 63 73 65 74 3b 20 20 st char *cset;
c3c50 20 20 20 20 2f 2a 20 55 73 65 20 72 65 67 69 73 /* Use regis
c3c60 74 65 72 73 20 66 6f 72 20 73 70 65 65 64 20 2a ters for speed *
c3c70 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 69 /. regi
c3c80 73 74 65 72 20 69 6e 74 20 62 61 73 65 3b 0a 20 ster int base;.
c3c90 20 20 20 20 20 20 20 20 20 63 73 65 74 20 3d 20 cset =
c3ca0 26 61 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e &aDigits[infop->
c3cb0 63 68 61 72 73 65 74 5d 3b 0a 20 20 20 20 20 20 charset];.
c3cc0 20 20 20 20 62 61 73 65 20 3d 20 69 6e 66 6f 70 base = infop
c3cd0 2d 3e 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 ->base;.
c3ce0 20 20 64 6f 7b 20 20 20 20 20 20 20 20 20 20 20 do{
c3cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3d10 2f 2a 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 73 /* Convert to as
c3d20 63 69 69 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 cii */.
c3d30 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 *(--bufpt) =
c3d40 63 73 65 74 5b 6c 6f 6e 67 76 61 6c 75 65 25 62 cset[longvalue%b
c3d50 61 73 65 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 ase];.
c3d60 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 6c 6f longvalue = lo
c3d70 6e 67 76 61 6c 75 65 2f 62 61 73 65 3b 0a 20 20 ngvalue/base;.
c3d80 20 20 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 }while(
c3d90 6c 6f 6e 67 76 61 6c 75 65 3e 30 20 29 3b 0a 20 longvalue>0 );.
c3da0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
c3db0 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e 74 29 28 length = (int)(
c3dc0 26 7a 4f 75 74 5b 6e 4f 75 74 2d 31 5d 2d 62 75 &zOut[nOut-1]-bu
c3dd0 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 66 6f fpt);. fo
c3de0 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2d r(idx=precision-
c3df0 6c 65 6e 67 74 68 3b 20 69 64 78 3e 30 3b 20 69 length; idx>0; i
c3e00 64 78 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 dx--){.
c3e10 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 27 30 *(--bufpt) = '0
c3e20 27 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ';
c3e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c3e40 2a 20 5a 65 72 6f 20 70 61 64 20 2a 2f 0a 20 20 * Zero pad */.
c3e50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
c3e60 69 66 28 20 70 72 65 66 69 78 20 29 20 2a 28 2d if( prefix ) *(-
c3e70 2d 62 75 66 70 74 29 20 3d 20 70 72 65 66 69 78 -bufpt) = prefix
c3e80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
c3e90 2f 2a 20 41 64 64 20 73 69 67 6e 20 2a 2f 0a 20 /* Add sign */.
c3ea0 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f if( flag_
c3eb0 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 26 26 alternateform &&
c3ec0 20 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78 20 29 infop->prefix )
c3ed0 7b 20 20 20 20 20 20 2f 2a 20 41 64 64 20 22 30 { /* Add "0
c3ee0 22 20 6f 72 20 22 30 78 22 20 2a 2f 0a 20 20 20 " or "0x" */.
c3ef0 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 const cha
c3f00 72 20 2a 70 72 65 3b 0a 20 20 20 20 20 20 20 20 r *pre;.
c3f10 20 20 63 68 61 72 20 78 3b 0a 20 20 20 20 20 20 char x;.
c3f20 20 20 20 20 70 72 65 20 3d 20 26 61 50 72 65 66 pre = &aPref
c3f30 69 78 5b 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78 ix[infop->prefix
c3f40 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 ];. for
c3f50 28 3b 20 28 78 3d 28 2a 70 72 65 29 29 21 3d 30 (; (x=(*pre))!=0
c3f60 3b 20 70 72 65 2b 2b 29 20 2a 28 2d 2d 62 75 66 ; pre++) *(--buf
c3f70 70 74 29 20 3d 20 78 3b 0a 20 20 20 20 20 20 20 pt) = x;.
c3f80 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 }. lengt
c3f90 68 20 3d 20 28 69 6e 74 29 28 26 7a 4f 75 74 5b h = (int)(&zOut[
c3fa0 6e 4f 75 74 2d 31 5d 2d 62 75 66 70 74 29 3b 0a nOut-1]-bufpt);.
c3fb0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
c3fc0 20 20 20 20 20 63 61 73 65 20 65 74 46 4c 4f 41 case etFLOA
c3fd0 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 T:. case et
c3fe0 45 58 50 3a 0a 20 20 20 20 20 20 63 61 73 65 20 EXP:. case
c3ff0 65 74 47 45 4e 45 52 49 43 3a 0a 20 20 20 20 20 etGENERIC:.
c4000 20 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 76 realvalue = v
c4010 61 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c 65 29 a_arg(ap,double)
c4020 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
c4030 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
c4040 49 4e 54 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 INT. leng
c4050 74 68 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 20 20 th = 0;.#else.
c4060 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 if( precis
c4070 69 6f 6e 3c 30 20 29 20 70 72 65 63 69 73 69 6f ion<0 ) precisio
c4080 6e 20 3d 20 36 3b 20 20 20 20 20 20 20 20 20 2f n = 6; /
c4090 2a 20 53 65 74 20 64 65 66 61 75 6c 74 20 70 72 * Set default pr
c40a0 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 ecision */.
c40b0 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 if( realvalue
c40c0 3c 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 <0.0 ){.
c40d0 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 2d 72 realvalue = -r
c40e0 65 61 6c 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 ealvalue;.
c40f0 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27 prefix = '-'
c4100 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
c4110 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 . if( f
c4120 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 lag_plussign )
c4130 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d prefix =
c4140 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 '+';.
c4150 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 62 6c else if( flag_bl
c4160 61 6e 6b 73 69 67 6e 20 29 20 20 20 20 70 72 65 anksign ) pre
c4170 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 fix = ' ';.
c4180 20 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 else
c4190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c41a0 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 prefix = 0;.
c41b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
c41c0 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e if( xtype==etGEN
c41d0 45 52 49 43 20 26 26 20 70 72 65 63 69 73 69 6f ERIC && precisio
c41e0 6e 3e 30 20 29 20 70 72 65 63 69 73 69 6f 6e 2d n>0 ) precision-
c41f0 2d 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 20 -;.#if 0.
c4200 20 2f 2a 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72 /* Rounding wor
c4210 6b 73 20 6c 69 6b 65 20 42 53 44 20 77 68 65 6e ks like BSD when
c4220 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e the constant 0.
c4230 34 39 39 39 20 69 73 20 75 73 65 64 2e 20 20 57 4999 is used. W
c4240 69 65 72 64 21 20 2a 2f 0a 20 20 20 20 20 20 20 ierd! */.
c4250 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69 for(idx=precisi
c4260 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39 on, rounder=0.49
c4270 39 39 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 99; idx>0; idx--
c4280 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b , rounder*=0.1);
c4290 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 2f .#else. /
c42a0 2a 20 49 74 20 6d 61 6b 65 73 20 6d 6f 72 65 20 * It makes more
c42b0 73 65 6e 73 65 20 74 6f 20 75 73 65 20 30 2e 35 sense to use 0.5
c42c0 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 */. for(
c42d0 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 idx=precision, r
c42e0 6f 75 6e 64 65 72 3d 30 2e 35 3b 20 69 64 78 3e ounder=0.5; idx>
c42f0 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 0; idx--, rounde
c4300 72 2a 3d 30 2e 31 29 7b 7d 0a 23 65 6e 64 69 66 r*=0.1){}.#endif
c4310 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79 . if( xty
c4320 70 65 3d 3d 65 74 46 4c 4f 41 54 20 29 20 72 65 pe==etFLOAT ) re
c4330 61 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 alvalue += round
c4340 65 72 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e er;. /* N
c4350 6f 72 6d 61 6c 69 7a 65 20 72 65 61 6c 76 61 6c ormalize realval
c4360 75 65 20 74 6f 20 77 69 74 68 69 6e 20 31 30 2e ue to within 10.
c4370 30 20 3e 20 72 65 61 6c 76 61 6c 75 65 20 3e 3d 0 > realvalue >=
c4380 20 31 2e 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 1.0 */.
c4390 65 78 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 exp = 0;.
c43a0 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 if( sqlite3IsNa
c43b0 4e 28 28 64 6f 75 62 6c 65 29 72 65 61 6c 76 61 N((double)realva
c43c0 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 lue) ){.
c43d0 20 20 62 75 66 70 74 20 3d 20 22 4e 61 4e 22 3b bufpt = "NaN";
c43e0 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 . lengt
c43f0 68 20 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 h = 3;.
c4400 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
c4410 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 65 }. if( re
c4420 61 6c 76 61 6c 75 65 3e 30 2e 30 20 29 7b 0a 20 alvalue>0.0 ){.
c4430 20 20 20 20 20 20 20 20 20 4c 4f 4e 47 44 4f 55 LONGDOU
c4440 42 4c 45 5f 54 59 50 45 20 73 63 61 6c 65 20 3d BLE_TYPE scale =
c4450 20 31 2e 30 3b 0a 20 20 20 20 20 20 20 20 20 20 1.0;.
c4460 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 while( realvalue
c4470 3e 3d 31 65 31 30 30 2a 73 63 61 6c 65 20 26 26 >=1e100*scale &&
c4480 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 73 63 61 exp<=350 ){ sca
c4490 6c 65 20 2a 3d 20 31 65 31 30 30 3b 65 78 70 2b le *= 1e100;exp+
c44a0 3d 31 30 30 3b 7d 0a 20 20 20 20 20 20 20 20 20 =100;}.
c44b0 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 while( realvalu
c44c0 65 3e 3d 31 65 36 34 2a 73 63 61 6c 65 20 26 26 e>=1e64*scale &&
c44d0 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 73 63 61 exp<=350 ){ sca
c44e0 6c 65 20 2a 3d 20 31 65 36 34 3b 20 65 78 70 2b le *= 1e64; exp+
c44f0 3d 36 34 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 =64; }.
c4500 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 while( realvalu
c4510 65 3e 3d 31 65 38 2a 73 63 61 6c 65 20 26 26 20 e>=1e8*scale &&
c4520 65 78 70 3c 3d 33 35 30 20 29 7b 20 73 63 61 6c exp<=350 ){ scal
c4530 65 20 2a 3d 20 31 65 38 3b 20 65 78 70 2b 3d 38 e *= 1e8; exp+=8
c4540 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 ; }. wh
c4550 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d ile( realvalue>=
c4560 31 30 2e 30 2a 73 63 61 6c 65 20 26 26 20 65 78 10.0*scale && ex
c4570 70 3c 3d 33 35 30 20 29 7b 20 73 63 61 6c 65 20 p<=350 ){ scale
c4580 2a 3d 20 31 30 2e 30 3b 20 65 78 70 2b 2b 3b 20 *= 10.0; exp++;
c4590 7d 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c }. real
c45a0 76 61 6c 75 65 20 2f 3d 20 73 63 61 6c 65 3b 0a value /= scale;.
c45b0 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 while(
c45c0 20 72 65 61 6c 76 61 6c 75 65 3c 31 65 2d 38 20 realvalue<1e-8
c45d0 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 ){ realvalue *=
c45e0 31 65 38 3b 20 65 78 70 2d 3d 38 3b 20 7d 0a 20 1e8; exp-=8; }.
c45f0 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 while(
c4600 72 65 61 6c 76 61 6c 75 65 3c 31 2e 30 20 29 7b realvalue<1.0 ){
c4610 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 30 realvalue *= 10
c4620 2e 30 3b 20 65 78 70 2d 2d 3b 20 7d 0a 20 20 20 .0; exp--; }.
c4630 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 33 if( exp>3
c4640 35 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 ){.
c4650 20 20 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2d if( prefix=='-
c4660 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ' ){.
c4670 20 20 20 62 75 66 70 74 20 3d 20 22 2d 49 6e 66 bufpt = "-Inf
c4680 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d ";. }
c4690 65 6c 73 65 20 69 66 28 20 70 72 65 66 69 78 3d else if( prefix=
c46a0 3d 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 20 20 ='+' ){.
c46b0 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 2b bufpt = "+
c46c0 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 Inf";.
c46d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
c46e0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 bufpt = "
c46f0 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 Inf";.
c4700 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
c4710 6c 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 length = sqlite3
c4720 53 74 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b Strlen30(bufpt);
c4730 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 . bre
c4740 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a ak;. }.
c4750 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
c4760 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 bufpt = buf;.
c4770 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 /*.
c4780 20 20 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c ** If the fiel
c4790 64 20 74 79 70 65 20 69 73 20 65 74 47 45 4e 45 d type is etGENE
c47a0 52 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72 RIC, then conver
c47b0 74 20 74 6f 20 65 69 74 68 65 72 20 65 74 45 58 t to either etEX
c47c0 50 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 P. ** or
c47d0 65 74 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72 etFLOAT, as appr
c47e0 6f 70 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20 opriate..
c47f0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
c4800 78 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20 29 xtype!=etFLOAT )
c4810 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c {. real
c4820 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 value += rounder
c4830 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
c4840 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 realvalue>=10.0
c4850 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 ){ realvalue *=
c4860 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 0.1; exp++; }.
c4870 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
c4880 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e if( xtype==etGEN
c4890 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20 ERIC ){.
c48a0 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c flag_rtz = !fl
c48b0 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d ag_alternateform
c48c0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
c48d0 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72 exp<-4 || exp>pr
c48e0 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20 20 ecision ){.
c48f0 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65 xtype = e
c4900 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20 20 tEXP;.
c4910 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
c4920 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 precision = p
c4930 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b 0a recision - exp;.
c4940 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79 70 xtyp
c4950 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20 20 e = etFLOAT;.
c4960 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
c4970 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
c4980 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 66 6c 61 flag_rtz = fla
c4990 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20 20 g_altform2;.
c49a0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
c49b0 28 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 ( xtype==etEXP )
c49c0 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d {. e2 =
c49d0 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 0;. }els
c49e0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 e{. e2
c49f0 3d 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 7d = exp;. }
c4a00 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 32 2b . if( e2+
c4a10 70 72 65 63 69 73 69 6f 6e 2b 77 69 64 74 68 20 precision+width
c4a20 3e 20 65 74 42 55 46 53 49 5a 45 20 2d 20 31 35 > etBUFSIZE - 15
c4a30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 ){. bu
c4a40 66 70 74 20 3d 20 7a 45 78 74 72 61 20 3d 20 73 fpt = zExtra = s
c4a50 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 65 32 qlite3Malloc( e2
c4a60 2b 70 72 65 63 69 73 69 6f 6e 2b 77 69 64 74 68 +precision+width
c4a70 2b 31 35 20 29 3b 0a 20 20 20 20 20 20 20 20 20 +15 );.
c4a80 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20 29 7b if( bufpt==0 ){
c4a90 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 41 63 . pAc
c4aa0 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 cum->mallocFaile
c4ab0 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 d = 1;.
c4ac0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 return;.
c4ad0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
c4ae0 0a 20 20 20 20 20 20 20 20 7a 4f 75 74 20 3d 20 . zOut =
c4af0 62 75 66 70 74 3b 0a 20 20 20 20 20 20 20 20 6e bufpt;. n
c4b00 73 64 20 3d 20 31 36 20 2b 20 66 6c 61 67 5f 61 sd = 16 + flag_a
c4b10 6c 74 66 6f 72 6d 32 2a 31 30 3b 0a 20 20 20 20 ltform2*10;.
c4b20 20 20 20 20 66 6c 61 67 5f 64 70 20 3d 20 28 70 flag_dp = (p
c4b30 72 65 63 69 73 69 6f 6e 3e 30 20 3f 31 3a 30 29 recision>0 ?1:0)
c4b40 20 7c 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 | flag_alternat
c4b50 65 66 6f 72 6d 20 7c 20 66 6c 61 67 5f 61 6c 74 eform | flag_alt
c4b60 66 6f 72 6d 32 3b 0a 20 20 20 20 20 20 20 20 2f form2;. /
c4b70 2a 20 54 68 65 20 73 69 67 6e 20 69 6e 20 66 72 * The sign in fr
c4b80 6f 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 ont of the numbe
c4b90 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 r */. if(
c4ba0 20 70 72 65 66 69 78 20 29 7b 0a 20 20 20 20 20 prefix ){.
c4bb0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
c4bc0 3d 20 70 72 65 66 69 78 3b 0a 20 20 20 20 20 20 = prefix;.
c4bd0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 }. /* D
c4be0 69 67 69 74 73 20 70 72 69 6f 72 20 74 6f 20 74 igits prior to t
c4bf0 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 he decimal point
c4c00 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
c4c10 65 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 e2<0 ){.
c4c20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 *(bufpt++) = '
c4c30 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 0';. }els
c4c40 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 e{. for
c4c50 28 3b 20 65 32 3e 3d 30 3b 20 65 32 2d 2d 29 7b (; e2>=0; e2--){
c4c60 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 . *(b
c4c70 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 ufpt++) = et_get
c4c80 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65 digit(&realvalue
c4c90 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20 ,&nsd);.
c4ca0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
c4cb0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 63 /* The dec
c4cc0 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 imal point */.
c4cd0 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 64 if( flag_d
c4ce0 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a p ){. *
c4cf0 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2e 27 3b (bufpt++) = '.';
c4d00 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
c4d10 20 20 20 2f 2a 20 22 30 22 20 64 69 67 69 74 73 /* "0" digits
c4d20 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69 6d after the decim
c4d30 61 6c 20 70 6f 69 6e 74 20 62 75 74 20 62 65 66 al point but bef
c4d40 6f 72 65 20 74 68 65 20 66 69 72 73 74 0a 20 20 ore the first.
c4d50 20 20 20 20 20 20 2a 2a 20 73 69 67 6e 69 66 69 ** signifi
c4d60 63 61 6e 74 20 64 69 67 69 74 20 6f 66 20 74 68 cant digit of th
c4d70 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 e number */.
c4d80 20 20 20 20 66 6f 72 28 65 32 2b 2b 3b 20 65 32 for(e2++; e2
c4d90 3c 30 3b 20 70 72 65 63 69 73 69 6f 6e 2d 2d 2c <0; precision--,
c4da0 20 65 32 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 e2++){.
c4db0 20 20 61 73 73 65 72 74 28 20 70 72 65 63 69 73 assert( precis
c4dc0 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 ion>0 );.
c4dd0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
c4de0 27 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 '0';. }.
c4df0 20 20 20 20 20 20 20 2f 2a 20 53 69 67 6e 69 66 /* Signif
c4e00 69 63 61 6e 74 20 64 69 67 69 74 73 20 61 66 74 icant digits aft
c4e10 65 72 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 er the decimal p
c4e20 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 oint */.
c4e30 77 68 69 6c 65 28 20 28 70 72 65 63 69 73 69 6f while( (precisio
c4e40 6e 2d 2d 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 n--)>0 ){.
c4e50 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
c4e60 20 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 et_getdigit(&re
c4e70 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 alvalue,&nsd);.
c4e80 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
c4e90 20 2f 2a 20 52 65 6d 6f 76 65 20 74 72 61 69 6c /* Remove trail
c4ea0 69 6e 67 20 7a 65 72 6f 73 20 61 6e 64 20 74 68 ing zeros and th
c4eb0 65 20 22 2e 22 20 69 66 20 6e 6f 20 64 69 67 69 e "." if no digi
c4ec0 74 73 20 66 6f 6c 6c 6f 77 20 74 68 65 20 22 2e ts follow the ".
c4ed0 22 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 " */. if(
c4ee0 20 66 6c 61 67 5f 72 74 7a 20 26 26 20 66 6c 61 flag_rtz && fla
c4ef0 67 5f 64 70 20 29 7b 0a 20 20 20 20 20 20 20 20 g_dp ){.
c4f00 20 20 77 68 69 6c 65 28 20 62 75 66 70 74 5b 2d while( bufpt[-
c4f10 31 5d 3d 3d 27 30 27 20 29 20 2a 28 2d 2d 62 75 1]=='0' ) *(--bu
c4f20 66 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20 fpt) = 0;.
c4f30 20 20 20 20 61 73 73 65 72 74 28 20 62 75 66 70 assert( bufp
c4f40 74 3e 7a 4f 75 74 20 29 3b 0a 20 20 20 20 20 20 t>zOut );.
c4f50 20 20 20 20 69 66 28 20 62 75 66 70 74 5b 2d 31 if( bufpt[-1
c4f60 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 ]=='.' ){.
c4f70 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 61 if( flag_a
c4f80 6c 74 66 6f 72 6d 32 20 29 7b 0a 20 20 20 20 20 ltform2 ){.
c4f90 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
c4fa0 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 ++) = '0';.
c4fb0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
c4fc0 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d *(--
c4fd0 62 75 66 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 bufpt) = 0;.
c4fe0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
c4ff0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
c5000 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 /* Add t
c5010 68 65 20 22 65 4e 4e 4e 22 20 73 75 66 66 69 78 he "eNNN" suffix
c5020 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
c5030 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b 0a xtype==etEXP ){.
c5040 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
c5050 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 5b 69 t++) = aDigits[i
c5060 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a nfop->charset];.
c5070 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78 if( ex
c5080 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 p<0 ){.
c5090 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
c50a0 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70 3b '-'; exp = -exp;
c50b0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
c50c0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 {. *(
c50d0 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 3b 0a bufpt++) = '+';.
c50e0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
c50f0 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 3d 31 if( exp>=1
c5100 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 00 ){.
c5110 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 *(bufpt++) = (
c5120 63 68 61 72 29 28 28 65 78 70 2f 31 30 30 29 2b char)((exp/100)+
c5130 27 30 27 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 '0'); /*
c5140 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 100's digit */.
c5150 20 20 20 20 20 20 20 20 20 20 20 65 78 70 20 25 exp %
c5160 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 20 20 = 100;.
c5170 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 }. *(b
c5180 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 ufpt++) = (char)
c5190 28 65 78 70 2f 31 30 2b 27 30 27 29 3b 20 20 20 (exp/10+'0');
c51a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 27 /* 10'
c51b0 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20 s digit */.
c51c0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
c51d0 3d 20 28 63 68 61 72 29 28 65 78 70 25 31 30 2b = (char)(exp%10+
c51e0 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20 20 '0');
c51f0 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 20 2a /* 1's digit *
c5200 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 /. }.
c5210 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 3b 0a *bufpt = 0;.
c5220 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
c5230 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 65 72 converted number
c5240 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 6e 64 is in buf[] and
c5250 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 zero terminated
c5260 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 20 20 . Output it..
c5270 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 ** Note tha
c5280 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20 t the number is
c5290 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f 72 64 in the usual ord
c52a0 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 65 64 er, not reversed
c52b0 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 20 20 as with.
c52c0 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e 76 ** integer conv
c52d0 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20 ersions. */.
c52e0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e length = (in
c52f0 74 29 28 62 75 66 70 74 2d 7a 4f 75 74 29 3b 0a t)(bufpt-zOut);.
c5300 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
c5310 7a 4f 75 74 3b 0a 0a 20 20 20 20 20 20 20 20 2f zOut;.. /
c5320 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 * Special case:
c5330 20 41 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72 Add leading zer
c5340 6f 73 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a os if the flag_z
c5350 65 72 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20 eropad flag is.
c5360 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e ** set an
c5370 64 20 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66 d we are not lef
c5380 74 20 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20 t justified */.
c5390 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f if( flag_
c53a0 7a 65 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67 zeropad && !flag
c53b0 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 26 26 20 _leftjustify &&
c53c0 6c 65 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b length < width){
c53d0 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 . int i
c53e0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 ;. int
c53f0 6e 50 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c nPad = width - l
c5400 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20 ength;.
c5410 20 66 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e for(i=width; i>
c5420 3d 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20 =nPad; i--){.
c5430 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 69 bufpt[i
c5440 5d 20 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64 ] = bufpt[i-nPad
c5450 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 ];. }.
c5460 20 20 20 20 20 20 20 20 20 69 20 3d 20 70 72 65 i = pre
c5470 66 69 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 fix!=0;.
c5480 20 20 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20 while( nPad--
c5490 29 20 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27 ) bufpt[i++] = '
c54a0 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 0';. le
c54b0 6e 67 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20 ngth = width;.
c54c0 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f }.#endif /
c54d0 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 * !defined(SQLIT
c54e0 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
c54f0 50 4f 49 4e 54 29 20 2a 2f 0a 20 20 20 20 20 20 POINT) */.
c5500 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
c5510 61 73 65 20 65 74 53 49 5a 45 3a 0a 20 20 20 20 ase etSIZE:.
c5520 20 20 20 20 2a 28 76 61 5f 61 72 67 28 61 70 2c *(va_arg(ap,
c5530 69 6e 74 2a 29 29 20 3d 20 70 41 63 63 75 6d 2d int*)) = pAccum-
c5540 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20 20 20 20 >nChar;.
c5550 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d length = width =
c5560 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 0;. brea
c5570 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 k;. case et
c5580 50 45 52 43 45 4e 54 3a 0a 20 20 20 20 20 20 20 PERCENT:.
c5590 20 62 75 66 5b 30 5d 20 3d 20 27 25 27 3b 0a 20 buf[0] = '%';.
c55a0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 bufpt = b
c55b0 75 66 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 uf;. leng
c55c0 74 68 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 th = 1;.
c55d0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 break;. cas
c55e0 65 20 65 74 43 48 41 52 58 3a 0a 20 20 20 20 20 e etCHARX:.
c55f0 20 20 20 63 20 3d 20 76 61 5f 61 72 67 28 61 70 c = va_arg(ap
c5600 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 62 ,int);. b
c5610 75 66 5b 30 5d 20 3d 20 28 63 68 61 72 29 63 3b uf[0] = (char)c;
c5620 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 . if( pre
c5630 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 cision>=0 ){.
c5640 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 31 for(idx=1
c5650 3b 20 69 64 78 3c 70 72 65 63 69 73 69 6f 6e 3b ; idx<precision;
c5660 20 69 64 78 2b 2b 29 20 62 75 66 5b 69 64 78 5d idx++) buf[idx]
c5670 20 3d 20 28 63 68 61 72 29 63 3b 0a 20 20 20 20 = (char)c;.
c5680 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 70 length = p
c5690 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 recision;.
c56a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
c56b0 20 20 20 6c 65 6e 67 74 68 20 3d 31 3b 0a 20 20 length =1;.
c56c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
c56d0 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 bufpt = buf;.
c56e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
c56f0 20 20 63 61 73 65 20 65 74 53 54 52 49 4e 47 3a case etSTRING:
c5700 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 44 59 . case etDY
c5710 4e 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 20 NSTRING:.
c5720 20 62 75 66 70 74 20 3d 20 76 61 5f 61 72 67 28 bufpt = va_arg(
c5730 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 ap,char*);.
c5740 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20 if( bufpt==0
c5750 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 ){. buf
c5760 70 74 20 3d 20 22 22 3b 0a 20 20 20 20 20 20 20 pt = "";.
c5770 20 7d 65 6c 73 65 20 69 66 28 20 78 74 79 70 65 }else if( xtype
c5780 3d 3d 65 74 44 59 4e 53 54 52 49 4e 47 20 29 7b ==etDYNSTRING ){
c5790 0a 20 20 20 20 20 20 20 20 20 20 7a 45 78 74 72 . zExtr
c57a0 61 20 3d 20 62 75 66 70 74 3b 0a 20 20 20 20 20 a = bufpt;.
c57b0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
c57c0 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b precision>=0 ){
c57d0 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6c . for(l
c57e0 65 6e 67 74 68 3d 30 3b 20 6c 65 6e 67 74 68 3c ength=0; length<
c57f0 70 72 65 63 69 73 69 6f 6e 20 26 26 20 62 75 66 precision && buf
c5800 70 74 5b 6c 65 6e 67 74 68 5d 3b 20 6c 65 6e 67 pt[length]; leng
c5810 74 68 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 th++){}.
c5820 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
c5830 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 length = sqlite
c5840 33 53 74 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3Strlen30(bufpt)
c5850 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
c5860 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
c5870 20 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 case etSQLESCAP
c5880 45 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 E:. case et
c5890 53 51 4c 45 53 43 41 50 45 32 3a 0a 20 20 20 20 SQLESCAPE2:.
c58a0 20 20 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 case etSQLESCA
c58b0 50 45 33 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 PE3: {. i
c58c0 6e 74 20 69 2c 20 6a 2c 20 6b 2c 20 6e 2c 20 69 nt i, j, k, n, i
c58d0 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 snull;. i
c58e0 6e 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 nt needQuote;.
c58f0 20 20 20 20 20 20 63 68 61 72 20 63 68 3b 0a 20 char ch;.
c5900 20 20 20 20 20 20 20 63 68 61 72 20 71 20 3d 20 char q =
c5910 28 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 ((xtype==etSQLES
c5920 43 41 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 29 CAPE3)?'"':'\'')
c5930 3b 20 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 61 ; /* Quote cha
c5940 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 racter */.
c5950 20 20 63 68 61 72 20 2a 65 73 63 61 72 67 20 3d char *escarg =
c5960 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a va_arg(ap,char*
c5970 29 3b 0a 20 20 20 20 20 20 20 20 69 73 6e 75 6c );. isnul
c5980 6c 20 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20 l = escarg==0;.
c5990 20 20 20 20 20 20 20 69 66 28 20 69 73 6e 75 6c if( isnul
c59a0 6c 20 29 20 65 73 63 61 72 67 20 3d 20 28 78 74 l ) escarg = (xt
c59b0 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 ype==etSQLESCAPE
c59c0 32 20 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e 2 ? "NULL" : "(N
c59d0 55 4c 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 20 ULL)");.
c59e0 6b 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20 k = precision;.
c59f0 20 20 20 20 20 20 20 66 6f 72 28 69 3d 6e 3d 30 for(i=n=0
c5a00 3b 20 6b 21 3d 30 20 26 26 20 28 63 68 3d 65 73 ; k!=0 && (ch=es
c5a10 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b carg[i])!=0; i++
c5a20 2c 20 6b 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 , k--){.
c5a30 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 20 6e if( ch==q ) n
c5a40 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ++;. }.
c5a50 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20 needQuote
c5a60 3d 20 21 69 73 6e 75 6c 6c 20 26 26 20 78 74 79 = !isnull && xty
c5a70 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 32 pe==etSQLESCAPE2
c5a80 3b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 69 ;. n += i
c5a90 20 2b 20 31 20 2b 20 6e 65 65 64 51 75 6f 74 65 + 1 + needQuote
c5aa0 2a 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 *2;. if(
c5ab0 6e 3e 65 74 42 55 46 53 49 5a 45 20 29 7b 0a 20 n>etBUFSIZE ){.
c5ac0 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d bufpt =
c5ad0 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74 65 zExtra = sqlite
c5ae0 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 3Malloc( n );.
c5af0 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70 if( bufp
c5b00 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 t==0 ){.
c5b10 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c pAccum->mall
c5b20 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
c5b30 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
c5b40 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
c5b50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
c5b60 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 bufpt = b
c5b70 75 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 uf;. }.
c5b80 20 20 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20 20 j = 0;.
c5b90 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f if( needQuo
c5ba0 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 te ) bufpt[j++]
c5bb0 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 6b 20 3d = q;. k =
c5bc0 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 i;. for(
c5bd0 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a i=0; i<k; i++){.
c5be0 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b bufpt[
c5bf0 6a 2b 2b 5d 20 3d 20 63 68 20 3d 20 65 73 63 61 j++] = ch = esca
c5c00 72 67 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 rg[i];.
c5c10 20 69 66 28 20 63 68 3d 3d 71 20 29 20 62 75 66 if( ch==q ) buf
c5c20 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 pt[j++] = ch;.
c5c30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
c5c40 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 if( needQuote )
c5c50 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a bufpt[j++] = q;.
c5c60 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a 5d bufpt[j]
c5c70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6c 65 = 0;. le
c5c80 6e 67 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20 20 ngth = j;.
c5c90 20 20 2f 2a 20 54 68 65 20 70 72 65 63 69 73 69 /* The precisi
c5ca0 6f 6e 20 69 6e 20 25 71 20 61 6e 64 20 25 51 20 on in %q and %Q
c5cb0 6d 65 61 6e 73 20 68 6f 77 20 6d 61 6e 79 20 69 means how many i
c5cc0 6e 70 75 74 20 63 68 61 72 61 63 74 65 72 73 20 nput characters
c5cd0 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f to. ** co
c5ce0 6e 73 75 6d 65 2c 20 6e 6f 74 20 74 68 65 20 6c nsume, not the l
c5cf0 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6f 75 74 ength of the out
c5d00 70 75 74 2e 2e 2e 0a 20 20 20 20 20 20 20 20 2a put.... *
c5d10 2a 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e * if( precision>
c5d20 3d 30 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c =0 && precision<
c5d30 6c 65 6e 67 74 68 20 29 20 6c 65 6e 67 74 68 20 length ) length
c5d40 3d 20 70 72 65 63 69 73 69 6f 6e 3b 20 2a 2f 0a = precision; */.
c5d50 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
c5d60 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 }. cas
c5d70 65 20 65 74 54 4f 4b 45 4e 3a 20 7b 0a 20 20 20 e etTOKEN: {.
c5d80 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b Token *pTok
c5d90 65 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 en = va_arg(ap,
c5da0 54 6f 6b 65 6e 2a 29 3b 0a 20 20 20 20 20 20 20 Token*);.
c5db0 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 if( pToken ){.
c5dc0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
c5dd0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 StrAccumAppend(p
c5de0 41 63 63 75 6d 2c 20 28 63 6f 6e 73 74 20 63 68 Accum, (const ch
c5df0 61 72 2a 29 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 ar*)pToken->z, p
c5e00 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20 Token->n);.
c5e10 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e }. len
c5e20 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b gth = width = 0;
c5e30 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
c5e40 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 }. ca
c5e50 73 65 20 65 74 53 52 43 4c 49 53 54 3a 20 7b 0a se etSRCLIST: {.
c5e60 20 20 20 20 20 20 20 20 53 72 63 4c 69 73 74 20 SrcList
c5e70 2a 70 53 72 63 20 3d 20 76 61 5f 61 72 67 28 61 *pSrc = va_arg(a
c5e80 70 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 20 20 p, SrcList*);.
c5e90 20 20 20 20 20 20 69 6e 74 20 6b 20 3d 20 76 61 int k = va
c5ea0 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 _arg(ap, int);.
c5eb0 20 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 struct Sr
c5ec0 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 cList_item *pIte
c5ed0 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 6b 5d 3b m = &pSrc->a[k];
c5ee0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
c5ef0 20 6b 3e 3d 30 20 26 26 20 6b 3c 70 53 72 63 2d k>=0 && k<pSrc-
c5f00 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20 20 20 20 >nSrc );.
c5f10 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61 74 if( pItem->zDat
c5f20 61 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 abase ){.
c5f30 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
c5f40 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c umAppend(pAccum,
c5f50 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 pItem->zDatabas
c5f60 65 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 e, -1);.
c5f70 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
c5f80 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 mAppend(pAccum,
c5f90 22 2e 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 ".", 1);.
c5fa0 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
c5fb0 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
c5fc0 28 70 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e (pAccum, pItem->
c5fd0 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 20 20 zName, -1);.
c5fe0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 length = wid
c5ff0 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 th = 0;.
c6000 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
c6010 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a default: {.
c6020 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
c6030 78 74 79 70 65 3d 3d 65 74 49 4e 56 41 4c 49 44 xtype==etINVALID
c6040 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 );. retu
c6050 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rn;. }.
c6060 7d 2f 2a 20 45 6e 64 20 73 77 69 74 63 68 20 6f }/* End switch o
c6070 76 65 72 20 74 68 65 20 66 6f 72 6d 61 74 20 74 ver the format t
c6080 79 70 65 20 2a 2f 0a 20 20 20 20 2f 2a 0a 20 20 ype */. /*.
c6090 20 20 2a 2a 20 54 68 65 20 74 65 78 74 20 6f 66 ** The text of
c60a0 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 the conversion
c60b0 69 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 is pointed to by
c60c0 20 22 62 75 66 70 74 22 20 61 6e 64 20 69 73 0a "bufpt" and is.
c60d0 20 20 20 20 2a 2a 20 22 6c 65 6e 67 74 68 22 20 ** "length"
c60e0 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e characters long.
c60f0 20 20 54 68 65 20 66 69 65 6c 64 20 77 69 64 74 The field widt
c6100 68 20 69 73 20 22 77 69 64 74 68 22 2e 20 20 44 h is "width". D
c6110 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 75 74 o. ** the out
c6120 70 75 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 put.. */.
c6130 69 66 28 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75 if( !flag_leftju
c6140 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20 72 stify ){. r
c6150 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70 61 egister int nspa
c6160 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63 65 ce;. nspace
c6170 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68 3b = width-length;
c6180 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61 63 . if( nspac
c6190 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 e>0 ){. s
c61a0 71 6c 69 74 65 33 41 70 70 65 6e 64 53 70 61 63 qlite3AppendSpac
c61b0 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 e(pAccum, nspace
c61c0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
c61d0 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 74 68 3e . if( length>
c61e0 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
c61f0 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
c6200 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 (pAccum, bufpt,
c6210 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d 0a 20 length);. }.
c6220 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 66 74 if( flag_left
c6230 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 justify ){.
c6240 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 register int ns
c6250 70 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 pace;. nspa
c6260 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 ce = width-lengt
c6270 68 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 h;. if( nsp
c6280 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 ace>0 ){.
c6290 20 73 71 6c 69 74 65 33 41 70 70 65 6e 64 53 70 sqlite3AppendSp
c62a0 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 ace(pAccum, nspa
c62b0 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ce);. }.
c62c0 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 }. sqlite3_f
c62d0 72 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 7d ree(zExtra);. }
c62e0 2f 2a 20 45 6e 64 20 66 6f 72 20 6c 6f 6f 70 20 /* End for loop
c62f0 6f 76 65 72 20 74 68 65 20 66 6f 72 6d 61 74 20 over the format
c6300 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 2f 2a 20 45 string */.} /* E
c6310 6e 64 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 2a nd of function *
c6320 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 /../*.** Append
c6330 4e 20 62 79 74 65 73 20 6f 66 20 74 65 78 74 20 N bytes of text
c6340 66 72 6f 6d 20 7a 20 74 6f 20 74 68 65 20 53 74 from z to the St
c6350 72 41 63 63 75 6d 20 6f 62 6a 65 63 74 2e 0a 2a rAccum object..*
c6360 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
c6370 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 void sqlite3Str
c6380 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74 72 41 AccumAppend(StrA
c6390 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74 20 63 ccum *p, const c
c63a0 68 61 72 20 2a 7a 2c 20 69 6e 74 20 4e 29 7b 0a har *z, int N){.
c63b0 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 7c assert( z!=0 |
c63c0 7c 20 4e 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 | N==0 );. if(
c63d0 70 2d 3e 74 6f 6f 42 69 67 20 7c 20 70 2d 3e 6d p->tooBig | p->m
c63e0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
c63f0 20 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 74 testcase(p->t
c6400 6f 6f 42 69 67 29 3b 0a 20 20 20 20 74 65 73 74 ooBig);. test
c6410 63 61 73 65 28 70 2d 3e 6d 61 6c 6c 6f 63 46 61 case(p->mallocFa
c6420 69 6c 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72 iled);. retur
c6430 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 n;. }. assert(
c6440 20 70 2d 3e 7a 54 65 78 74 21 3d 30 20 7c 7c 20 p->zText!=0 ||
c6450 70 2d 3e 6e 43 68 61 72 3d 3d 30 20 29 3b 0a 20 p->nChar==0 );.
c6460 20 69 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20 20 if( N<0 ){.
c6470 4e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 N = sqlite3Strle
c6480 6e 33 30 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 n30(z);. }. if
c6490 28 20 4e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 ( N==0 || NEVER(
c64a0 7a 3d 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 z==0) ){. ret
c64b0 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 urn;. }. if( p
c64c0 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e ->nChar+N >= p->
c64d0 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 nAlloc ){. ch
c64e0 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66 ar *zNew;. if
c64f0 28 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 ( !p->useMalloc
c6500 29 7b 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 ){. p->tooB
c6510 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20 ig = 1;. N
c6520 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d = p->nAlloc - p-
c6530 3e 6e 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 >nChar - 1;.
c6540 20 20 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20 if( N<=0 ){.
c6550 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
c6560 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
c6570 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 6c . char *zOl
c6580 64 20 3d 20 28 70 2d 3e 7a 54 65 78 74 3d 3d 70 d = (p->zText==p
c6590 2d 3e 7a 42 61 73 65 20 3f 20 30 20 3a 20 70 2d ->zBase ? 0 : p-
c65a0 3e 7a 54 65 78 74 29 3b 0a 20 20 20 20 20 20 69 >zText);. i
c65b0 36 34 20 73 7a 4e 65 77 20 3d 20 70 2d 3e 6e 43 64 szNew = p->nC
c65c0 68 61 72 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 har;. szNew
c65d0 20 2b 3d 20 4e 20 2b 20 31 3b 0a 20 20 20 20 20 += N + 1;.
c65e0 20 69 66 28 20 73 7a 4e 65 77 20 3e 20 70 2d 3e if( szNew > p->
c65f0 6d 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 mxAlloc ){.
c6600 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
c6610 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 umReset(p);.
c6620 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 p->tooBig =
c6630 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 1;. retur
c6640 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a n;. }else{.
c6650 20 20 20 20 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f p->nAllo
c6660 63 20 3d 20 28 69 6e 74 29 73 7a 4e 65 77 3b 0a c = (int)szNew;.
c6670 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
c6680 28 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 3d 3d ( p->useMalloc==
c6690 31 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4e 65 1 ){. zNe
c66a0 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 w = sqlite3DbRea
c66b0 6c 6c 6f 63 28 70 2d 3e 64 62 2c 20 7a 4f 6c 64 lloc(p->db, zOld
c66c0 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 29 3b 0a 20 20 , p->nAlloc);.
c66d0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
c66e0 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 zNew = sqlite
c66f0 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f 6c 64 2c 20 3_realloc(zOld,
c6700 70 2d 3e 6e 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 p->nAlloc);.
c6710 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 7a 4e }. if( zN
c6720 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 ew ){. if
c6730 28 20 7a 4f 6c 64 3d 3d 30 20 26 26 20 70 2d 3e ( zOld==0 && p->
c6740 6e 43 68 61 72 3e 30 20 29 20 6d 65 6d 63 70 79 nChar>0 ) memcpy
c6750 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 78 74 2c (zNew, p->zText,
c6760 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 20 20 p->nChar);.
c6770 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 7a p->zText = z
c6780 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 New;. }else
c6790 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c {. p->mal
c67a0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
c67b0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 sqlite3St
c67c0 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a rAccumReset(p);.
c67d0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a return;.
c67e0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
c67f0 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 7a }. assert( p->z
c6800 54 65 78 74 20 29 3b 0a 20 20 6d 65 6d 63 70 79 Text );. memcpy
c6810 28 26 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 (&p->zText[p->nC
c6820 68 61 72 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 har], z, N);. p
c6830 2d 3e 6e 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a ->nChar += N;.}.
c6840 0a 2f 2a 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66 ./*.** Finish of
c6850 66 20 61 20 73 74 72 69 6e 67 20 62 79 20 6d 61 f a string by ma
c6860 6b 69 6e 67 20 73 75 72 65 20 69 74 20 69 73 20 king sure it is
c6870 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e zero-terminated.
c6880 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 .** Return a poi
c6890 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65 73 75 nter to the resu
c68a0 6c 74 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 52 lting string. R
c68b0 65 74 75 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 eturn a NULL.**
c68c0 70 6f 69 6e 74 65 72 20 69 66 20 61 6e 79 20 6b pointer if any k
c68d0 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 77 61 73 ind of error was
c68e0 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f encountered..*/
c68f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
c6900 63 68 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 char *sqlite3Str
c6910 41 63 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41 AccumFinish(StrA
c6920 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 ccum *p){. if(
c6930 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 p->zText ){.
c6940 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 p->zText[p->nCha
c6950 72 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 r] = 0;. if(
c6960 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 p->useMalloc &&
c6970 70 2d 3e 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 p->zText==p->zBa
c6980 73 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 se ){. if(
c6990 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 3d 3d 31 20 p->useMalloc==1
c69a0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 54 ){. p->zT
c69b0 65 78 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d ext = sqlite3DbM
c69c0 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 allocRaw(p->db,
c69d0 70 2d 3e 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 p->nChar+1 );.
c69e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
c69f0 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 73 71 p->zText = sq
c6a00 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70 2d 3e lite3_malloc(p->
c6a10 6e 43 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20 nChar+1);.
c6a20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 7a }. if( p->z
c6a30 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Text ){.
c6a40 6d 65 6d 63 70 79 28 70 2d 3e 7a 54 65 78 74 2c memcpy(p->zText,
c6a50 20 70 2d 3e 7a 42 61 73 65 2c 20 70 2d 3e 6e 43 p->zBase, p->nC
c6a60 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65 har+1);. }e
c6a70 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e lse{. p->
c6a80 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
c6a90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
c6aa0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e }. return p->
c6ab0 7a 54 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 zText;.}../*.**
c6ac0 52 65 73 65 74 20 61 6e 20 53 74 72 41 63 63 75 Reset an StrAccu
c6ad0 6d 20 73 74 72 69 6e 67 2e 20 20 52 65 63 6c 61 m string. Recla
c6ae0 69 6d 20 61 6c 6c 20 6d 61 6c 6c 6f 63 65 64 20 im all malloced
c6af0 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 memory..*/.SQLIT
c6b00 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
c6b10 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 qlite3StrAccumRe
c6b20 73 65 74 28 53 74 72 41 63 63 75 6d 20 2a 70 29 set(StrAccum *p)
c6b30 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 {. if( p->zText
c6b40 21 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20 20 !=p->zBase ){.
c6b50 20 20 69 66 28 20 70 2d 3e 75 73 65 4d 61 6c 6c if( p->useMall
c6b60 6f 63 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 oc==1 ){. s
c6b70 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e qlite3DbFree(p->
c6b80 64 62 2c 20 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 db, p->zText);.
c6b90 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
c6ba0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e sqlite3_free(p->
c6bb0 7a 54 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 zText);. }.
c6bc0 7d 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 }. p->zText = 0
c6bd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 ;.}../*.** Initi
c6be0 61 6c 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61 alize a string a
c6bf0 63 63 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 ccumulator.*/.SQ
c6c00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
c6c10 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 d sqlite3StrAccu
c6c20 6d 49 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a mInit(StrAccum *
c6c30 70 2c 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 p, char *zBase,
c6c40 69 6e 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a int n, int mx){.
c6c50 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e p->zText = p->
c6c60 7a 42 61 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 zBase = zBase;.
c6c70 20 70 2d 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d p->db = 0;. p-
c6c80 3e 6e 43 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d >nChar = 0;. p-
c6c90 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 >nAlloc = n;. p
c6ca0 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a ->mxAlloc = mx;.
c6cb0 20 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d p->useMalloc =
c6cc0 20 31 3b 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 1;. p->tooBig
c6cd0 3d 20 30 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 = 0;. p->malloc
c6ce0 46 61 69 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f Failed = 0;.}../
c6cf0 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 *.** Print into
c6d00 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 memory obtained
c6d10 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f from sqliteMallo
c6d20 63 28 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e c(). Use the in
c6d30 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 ternal.** %-conv
c6d40 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e ersion extension
c6d50 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
c6d60 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
c6d70 65 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 e3VMPrintf(sqlit
c6d80 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 e3 *db, const ch
c6d90 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f ar *zFormat, va_
c6da0 6c 69 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 list ap){. char
c6db0 20 2a 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 *z;. char zBas
c6dc0 65 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 e[SQLITE_PRINT_B
c6dd0 55 46 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 UF_SIZE];. StrA
c6de0 63 63 75 6d 20 61 63 63 3b 0a 20 20 61 73 73 65 ccum acc;. asse
c6df0 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 73 rt( db!=0 );. s
c6e00 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e qlite3StrAccumIn
c6e10 69 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 it(&acc, zBase,
c6e20 73 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a 20 sizeof(zBase),.
c6e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6e40 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b db->aLimit[
c6e50 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
c6e60 47 54 48 5d 29 3b 0a 20 20 61 63 63 2e 64 62 20 GTH]);. acc.db
c6e70 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 = db;. sqlite3V
c6e80 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 31 2c XPrintf(&acc, 1,
c6e90 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 zFormat, ap);.
c6ea0 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 z = sqlite3StrA
c6eb0 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 ccumFinish(&acc)
c6ec0 3b 0a 20 20 69 66 28 20 61 63 63 2e 6d 61 6c 6c ;. if( acc.mall
c6ed0 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
c6ee0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
c6ef0 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 1;. }. retu
c6f00 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 rn z;.}../*.** P
c6f10 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 rint into memory
c6f20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
c6f30 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 qliteMalloc().
c6f40 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c Use the internal
c6f50 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e .** %-conversion
c6f60 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a extensions..*/.
c6f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
c6f80 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 har *sqlite3MPri
c6f90 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ntf(sqlite3 *db,
c6fa0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
c6fb0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
c6fc0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 _list ap;. char
c6fd0 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 *z;. va_start(
c6fe0 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 ap, zFormat);.
c6ff0 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 z = sqlite3VMPri
c7000 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c ntf(db, zFormat,
c7010 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 ap);. va_end(a
c7020 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a p);. return z;.
c7030 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 }../*.** Like sq
c7040 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 lite3MPrintf(),
c7050 62 75 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 but call sqlite3
c7060 44 62 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 DbFree() on zStr
c7070 20 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e after formattin
c7080 67 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 g.** the string
c7090 61 6e 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 and before retur
c70a0 6e 6e 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 nning. This rou
c70b0 74 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 tine is intended
c70c0 20 74 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 to be used.** t
c70d0 6f 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 o modify an exis
c70e0 74 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f ting string. Fo
c70f0 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a r example:.**.**
c7100 20 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 x = sqlit
c7110 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c e3MPrintf(db, x,
c7120 20 22 70 72 65 66 69 78 20 25 73 20 73 75 66 66 "prefix %s suff
c7130 69 78 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 ix", x);.**.*/.S
c7140 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
c7150 61 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 ar *sqlite3MAppe
c7160 6e 64 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ndf(sqlite3 *db,
c7170 20 63 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e char *zStr, con
c7180 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
c7190 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 , ...){. va_lis
c71a0 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b t ap;. char *z;
c71b0 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 . va_start(ap,
c71c0 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 zFormat);. z =
c71d0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 sqlite3VMPrintf(
c71e0 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 db, zFormat, ap)
c71f0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a ;. va_end(ap);.
c7200 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
c7210 64 62 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 db, zStr);. ret
c7220 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn z;.}../*.**
c7230 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 Print into memor
c7240 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 y obtained from
c7250 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
c7260 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 . Omit the inte
c7270 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 rnal.** %-conver
c7280 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e sion extensions.
c7290 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
c72a0 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 har *sqlite3_vmp
c72b0 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 rintf(const char
c72c0 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 *zFormat, va_li
c72d0 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a st ap){. char *
c72e0 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b z;. char zBase[
c72f0 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 SQLITE_PRINT_BUF
c7300 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 _SIZE];. StrAcc
c7310 75 6d 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 um acc;.#ifndef
c7320 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
c7330 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 INIT. if( sqlit
c7340 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
c7350 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 ) return 0;.#end
c7360 69 66 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 if. sqlite3StrA
c7370 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a ccumInit(&acc, z
c7380 42 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 Base, sizeof(zBa
c7390 73 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f se), SQLITE_MAX_
c73a0 4c 45 4e 47 54 48 29 3b 0a 20 20 61 63 63 2e 75 LENGTH);. acc.u
c73b0 73 65 4d 61 6c 6c 6f 63 20 3d 20 32 3b 0a 20 20 seMalloc = 2;.
c73c0 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 sqlite3VXPrintf(
c73d0 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 &acc, 0, zFormat
c73e0 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71 6c , ap);. z = sql
c73f0 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 ite3StrAccumFini
c7400 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65 74 75 sh(&acc);. retu
c7410 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 rn z;.}../*.** P
c7420 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 rint into memory
c7430 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
c7440 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 28 qlite3_malloc()(
c7450 29 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 ). Omit the int
c7460 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 ernal.** %-conve
c7470 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 rsion extensions
c7480 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
c7490 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70 char *sqlite3_mp
c74a0 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 rintf(const char
c74b0 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
c74c0 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
c74d0 20 63 68 61 72 20 2a 7a 3b 0a 23 69 66 6e 64 65 char *z;.#ifnde
c74e0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
c74f0 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c TOINIT. if( sql
c7500 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
c7510 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 ) ) return 0;.#e
c7520 6e 64 69 66 0a 20 20 76 61 5f 73 74 61 72 74 28 ndif. va_start(
c7530 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 ap, zFormat);.
c7540 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 z = sqlite3_vmpr
c7550 69 6e 74 66 28 7a 46 6f 72 6d 61 74 2c 20 61 70 intf(zFormat, ap
c7560 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b );. va_end(ap);
c7570 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a . return z;.}..
c7580 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 6e /*.** sqlite3_sn
c7590 70 72 69 6e 74 66 28 29 20 77 6f 72 6b 73 20 6c printf() works l
c75a0 69 6b 65 20 73 6e 70 72 69 6e 74 66 28 29 20 65 ike snprintf() e
c75b0 78 63 65 70 74 20 74 68 61 74 20 69 74 20 69 67 xcept that it ig
c75c0 6e 6f 72 65 73 20 74 68 65 0a 2a 2a 20 63 75 72 nores the.** cur
c75d0 72 65 6e 74 20 6c 6f 63 61 6c 65 20 73 65 74 74 rent locale sett
c75e0 69 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20 69 ings. This is i
c75f0 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 53 51 4c mportant for SQL
c7600 69 74 65 20 62 65 63 61 75 73 65 20 77 65 0a 2a ite because we.*
c7610 2a 20 61 72 65 20 6e 6f 74 20 61 62 6c 65 20 74 * are not able t
c7620 6f 20 75 73 65 20 61 20 22 2c 22 20 61 73 20 74 o use a "," as t
c7630 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 he decimal point
c7640 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 22 2e 22 in place of "."
c7650 20 61 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 as.** specified
c7660 20 62 79 20 73 6f 6d 65 20 6c 6f 63 61 6c 65 73 by some locales
c7670 2e 0a 2a 2a 0a 2a 2a 20 4f 6f 70 73 3a 20 20 54 ..**.** Oops: T
c7680 68 65 20 66 69 72 73 74 20 74 77 6f 20 61 72 67 he first two arg
c7690 75 6d 65 6e 74 73 20 6f 66 20 73 71 6c 69 74 65 uments of sqlite
c76a0 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 61 72 65 3_snprintf() are
c76b0 20 62 61 63 6b 77 61 72 64 73 0a 2a 2a 20 66 72 backwards.** fr
c76c0 6f 6d 20 74 68 65 20 73 6e 70 72 69 6e 74 66 28 om the snprintf(
c76d0 29 20 73 74 61 6e 64 61 72 64 2e 20 20 55 6e 66 ) standard. Unf
c76e0 6f 72 74 75 6e 61 74 65 6c 79 2c 20 69 74 20 69 ortunately, it i
c76f0 73 20 74 6f 6f 20 6c 61 74 65 20 74 6f 20 63 68 s too late to ch
c7700 61 6e 67 65 0a 2a 2a 20 74 68 69 73 20 77 69 74 ange.** this wit
c7710 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 20 63 6f hout breaking co
c7720 6d 70 61 74 69 62 69 6c 69 74 79 2c 20 73 6f 20 mpatibility, so
c7730 77 65 20 6a 75 73 74 20 68 61 76 65 20 74 6f 20 we just have to
c7740 6c 69 76 65 20 77 69 74 68 20 74 68 65 0a 2a 2a live with the.**
c7750 20 6d 69 73 74 61 6b 65 2e 0a 2a 2a 0a 2a 2a 20 mistake..**.**
c7760 73 71 6c 69 74 65 33 5f 76 73 6e 70 72 69 6e 74 sqlite3_vsnprint
c7770 66 28 29 20 69 73 20 74 68 65 20 76 61 72 61 72 f() is the varar
c7780 67 73 20 76 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 53 gs version..*/.S
c7790 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
c77a0 73 71 6c 69 74 65 33 5f 76 73 6e 70 72 69 6e 74 sqlite3_vsnprint
c77b0 66 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a f(int n, char *z
c77c0 42 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Buf, const char
c77d0 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 *zFormat, va_lis
c77e0 74 20 61 70 29 7b 0a 20 20 53 74 72 41 63 63 75 t ap){. StrAccu
c77f0 6d 20 61 63 63 3b 0a 20 20 69 66 28 20 6e 3c 3d m acc;. if( n<=
c7800 30 20 29 20 72 65 74 75 72 6e 20 7a 42 75 66 3b 0 ) return zBuf;
c7810 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 . sqlite3StrAcc
c7820 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 umInit(&acc, zBu
c7830 66 2c 20 6e 2c 20 30 29 3b 0a 20 20 61 63 63 2e f, n, 0);. acc.
c7840 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 useMalloc = 0;.
c7850 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 sqlite3VXPrintf
c7860 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 (&acc, 0, zForma
c7870 74 2c 20 61 70 29 3b 0a 20 20 72 65 74 75 72 6e t, ap);. return
c7880 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
c7890 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 7d 0a Finish(&acc);.}.
c78a0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 SQLITE_API char
c78b0 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 *sqlite3_snprint
c78c0 66 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a f(int n, char *z
c78d0 42 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Buf, const char
c78e0 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a *zFormat, ...){.
c78f0 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f char *z;. va_
c7900 6c 69 73 74 20 61 70 3b 0a 20 20 76 61 5f 73 74 list ap;. va_st
c7910 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b art(ap,zFormat);
c7920 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 . z = sqlite3_v
c7930 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 42 75 66 snprintf(n, zBuf
c7940 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
c7950 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
c7960 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a return z;.}../*.
c7970 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 72 ** This is the r
c7980 6f 75 74 69 6e 65 20 74 68 61 74 20 61 63 74 75 outine that actu
c7990 61 6c 6c 79 20 66 6f 72 6d 61 74 73 20 74 68 65 ally formats the
c79a0 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 20 6d sqlite3_log() m
c79b0 65 73 73 61 67 65 2e 0a 2a 2a 20 57 65 20 68 6f essage..** We ho
c79c0 75 73 65 20 69 74 20 69 6e 20 61 20 73 65 70 61 use it in a sepa
c79d0 72 61 74 65 20 72 6f 75 74 69 6e 65 20 66 72 6f rate routine fro
c79e0 6d 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 20 m sqlite3_log()
c79f0 74 6f 20 61 76 6f 69 64 20 75 73 69 6e 67 0a 2a to avoid using.*
c7a00 2a 20 73 74 61 63 6b 20 73 70 61 63 65 20 6f 6e * stack space on
c7a10 20 73 6d 61 6c 6c 2d 73 74 61 63 6b 20 73 79 73 small-stack sys
c7a20 74 65 6d 73 20 77 68 65 6e 20 6c 6f 67 67 69 6e tems when loggin
c7a30 67 20 69 73 20 64 69 73 61 62 6c 65 64 2e 0a 2a g is disabled..*
c7a40 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 6f 67 *.** sqlite3_log
c7a50 28 29 20 6d 75 73 74 20 72 65 6e 64 65 72 20 69 () must render i
c7a60 6e 74 6f 20 61 20 73 74 61 74 69 63 20 62 75 66 nto a static buf
c7a70 66 65 72 2e 20 20 49 74 20 63 61 6e 6e 6f 74 20 fer. It cannot
c7a80 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 dynamically.** a
c7a90 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 62 llocate memory b
c7aa0 65 63 61 75 73 65 20 69 74 20 6d 69 67 68 74 20 ecause it might
c7ab0 62 65 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20 be called while
c7ac0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
c7ad0 61 74 6f 72 0a 2a 2a 20 6d 75 74 65 78 20 69 73 ator.** mutex is
c7ae0 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 held..*/.static
c7af0 20 76 6f 69 64 20 72 65 6e 64 65 72 4c 6f 67 4d void renderLogM
c7b00 73 67 28 69 6e 74 20 69 45 72 72 43 6f 64 65 2c sg(int iErrCode,
c7b10 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
c7b20 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70 rmat, va_list ap
c7b30 29 7b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 ){. StrAccum ac
c7b40 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
c7b50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
c7b60 74 72 69 6e 67 20 61 63 63 75 6d 75 6c 61 74 6f tring accumulato
c7b70 72 20 2a 2f 0a 20 20 63 68 61 72 20 7a 4d 73 67 r */. char zMsg
c7b80 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 [SQLITE_PRINT_BU
c7b90 46 5f 53 49 5a 45 2a 33 5d 3b 20 20 20 20 2f 2a F_SIZE*3]; /*
c7ba0 20 43 6f 6d 70 6c 65 74 65 20 6c 6f 67 20 6d 65 Complete log me
c7bb0 73 73 61 67 65 20 2a 2f 0a 0a 20 20 73 71 6c 69 ssage */.. sqli
c7bc0 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 te3StrAccumInit(
c7bd0 26 61 63 63 2c 20 7a 4d 73 67 2c 20 73 69 7a 65 &acc, zMsg, size
c7be0 6f 66 28 7a 4d 73 67 29 2c 20 30 29 3b 0a 20 20 of(zMsg), 0);.
c7bf0 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 acc.useMalloc =
c7c00 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 0;. sqlite3VXPr
c7c10 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 intf(&acc, 0, zF
c7c20 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 73 71 ormat, ap);. sq
c7c30 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
c7c40 67 2e 78 4c 6f 67 28 73 71 6c 69 74 65 33 47 6c g.xLog(sqlite3Gl
c7c50 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 4c 6f 67 41 obalConfig.pLogA
c7c60 72 67 2c 20 69 45 72 72 43 6f 64 65 2c 0a 20 20 rg, iErrCode,.
c7c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7c80 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
c7c90 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 StrAccumFinish(&
c7ca0 61 63 63 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 acc));.}../*.**
c7cb0 46 6f 72 6d 61 74 20 61 6e 64 20 77 72 69 74 65 Format and write
c7cc0 20 61 20 6d 65 73 73 61 67 65 20 74 6f 20 74 68 a message to th
c7cd0 65 20 6c 6f 67 20 69 66 20 6c 6f 67 67 69 6e 67 e log if logging
c7ce0 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a 2a 2f 0a is enabled..*/.
c7cf0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
c7d00 73 71 6c 69 74 65 33 5f 6c 6f 67 28 69 6e 74 20 sqlite3_log(int
c7d10 69 45 72 72 43 6f 64 65 2c 20 63 6f 6e 73 74 20 iErrCode, const
c7d20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e char *zFormat, .
c7d30 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
c7d40 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
c7d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c7d60 2a 20 56 61 72 61 72 67 20 6c 69 73 74 20 2a 2f * Vararg list */
c7d70 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
c7d80 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67 20 obalConfig.xLog
c7d90 29 7b 0a 20 20 20 20 76 61 5f 73 74 61 72 74 28 ){. va_start(
c7da0 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 ap, zFormat);.
c7db0 20 20 72 65 6e 64 65 72 4c 6f 67 4d 73 67 28 69 renderLogMsg(i
c7dc0 45 72 72 43 6f 64 65 2c 20 7a 46 6f 72 6d 61 74 ErrCode, zFormat
c7dd0 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e , ap);. va_en
c7de0 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 d(ap);. }.}..#i
c7df0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
c7e00 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 _DEBUG)./*.** A
c7e10 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 version of print
c7e20 66 28 29 20 74 68 61 74 20 75 6e 64 65 72 73 74 f() that underst
c7e30 61 6e 64 73 20 25 6c 6c 64 2e 20 20 55 73 65 64 ands %lld. Used
c7e40 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a for debugging..
c7e50 2a 2a 20 54 68 65 20 70 72 69 6e 74 66 28 29 20 ** The printf()
c7e60 62 75 69 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20 built into some
c7e70 76 65 72 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 versions of wind
c7e80 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64 ows does not und
c7e90 65 72 73 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 erstand %lld.**
c7ea0 61 6e 64 20 73 65 67 66 61 75 6c 74 73 20 69 66 and segfaults if
c7eb0 20 79 6f 75 20 67 69 76 65 20 69 74 20 61 20 6c you give it a l
c7ec0 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f ong long int..*/
c7ed0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
c7ee0 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 62 75 void sqlite3Debu
c7ef0 67 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 gPrintf(const ch
c7f00 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e ar *zFormat, ...
c7f10 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b ){. va_list ap;
c7f20 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b . StrAccum acc;
c7f30 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 35 30 30 . char zBuf[500
c7f40 5d 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 ];. sqlite3StrA
c7f50 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a ccumInit(&acc, z
c7f60 42 75 66 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66 Buf, sizeof(zBuf
c7f70 29 2c 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 ), 0);. acc.use
c7f80 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 Malloc = 0;. va
c7f90 5f 73 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 _start(ap,zForma
c7fa0 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 t);. sqlite3VXP
c7fb0 72 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a rintf(&acc, 0, z
c7fc0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 Format, ap);. v
c7fd0 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c a_end(ap);. sql
c7fe0 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 ite3StrAccumFini
c7ff0 73 68 28 26 61 63 63 29 3b 0a 20 20 66 70 72 69 sh(&acc);. fpri
c8000 6e 74 66 28 73 74 64 6f 75 74 2c 22 25 73 22 2c ntf(stdout,"%s",
c8010 20 7a 42 75 66 29 3b 0a 20 20 66 66 6c 75 73 68 zBuf);. fflush
c8020 28 73 74 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 (stdout);.}.#end
c8030 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
c8040 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a TE_OMIT_TRACE./*
c8050 0a 2a 2a 20 76 61 72 69 61 62 6c 65 2d 61 72 67 .** variable-arg
c8060 75 6d 65 6e 74 20 77 72 61 70 70 65 72 20 61 72 ument wrapper ar
c8070 6f 75 6e 64 20 73 71 6c 69 74 65 33 56 58 50 72 ound sqlite3VXPr
c8080 69 6e 74 66 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 intf()..*/.SQLIT
c8090 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
c80a0 71 6c 69 74 65 33 58 50 72 69 6e 74 66 28 53 74 qlite3XPrintf(St
c80b0 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74 rAccum *p, const
c80c0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
c80d0 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
c80e0 61 70 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 ap;. va_start(a
c80f0 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 p,zFormat);. sq
c8100 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 70 2c lite3VXPrintf(p,
c8110 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 1, zFormat, ap)
c8120 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a ;. va_end(ap);.
c8130 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a }.#endif../*****
c8140 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
c8150 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a printf.c ******
c8160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8180 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
c8190 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
c81a0 66 69 6c 65 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a file random.c **
c81b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c81c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c81d0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
c81e0 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
c81f0 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
c8200 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
c8210 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
c8220 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
c8230 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
c8240 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
c8250 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
c8260 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
c8270 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
c8280 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
c8290 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
c82a0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
c82b0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
c82c0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
c82d0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
c82e0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
c82f0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
c8300 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
c8310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8350 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
c8360 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 69 ntains code to i
c8370 6d 70 6c 65 6d 65 6e 74 20 61 20 70 73 65 75 64 mplement a pseud
c8380 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 0a o-random number.
c8390 2a 2a 20 67 65 6e 65 72 61 74 6f 72 20 28 50 52 ** generator (PR
c83a0 4e 47 29 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a NG) for SQLite..
c83b0 2a 2a 0a 2a 2a 20 52 61 6e 64 6f 6d 20 6e 75 6d **.** Random num
c83c0 62 65 72 73 20 61 72 65 20 75 73 65 64 20 62 79 bers are used by
c83d0 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 64 61 74 some of the dat
c83e0 61 62 61 73 65 20 62 61 63 6b 65 6e 64 73 20 69 abase backends i
c83f0 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 67 65 n order.** to ge
c8400 6e 65 72 61 74 65 20 72 61 6e 64 6f 6d 20 69 6e nerate random in
c8410 74 65 67 65 72 20 6b 65 79 73 20 66 6f 72 20 74 teger keys for t
c8420 61 62 6c 65 73 20 6f 72 20 72 61 6e 64 6f 6d 20 ables or random
c8430 66 69 6c 65 6e 61 6d 65 73 2e 0a 2a 2f 0a 0a 0a filenames..*/...
c8440 2f 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20 73 /* All threads s
c8450 68 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72 61 hare a single ra
c8460 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 ndom number gene
c8470 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 73 rator..** This s
c8480 74 72 75 63 74 75 72 65 20 69 73 20 74 68 65 20 tructure is the
c8490 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 current state of
c84a0 20 74 68 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a the generator..
c84b0 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 */.static SQLITE
c84c0 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 _WSD struct sqli
c84d0 74 65 33 50 72 6e 67 54 79 70 65 20 7b 0a 20 20 te3PrngType {.
c84e0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 73 unsigned char is
c84f0 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f Init; /
c8500 2a 20 54 72 75 65 20 69 66 20 69 6e 69 74 69 61 * True if initia
c8510 6c 69 7a 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 lized */. unsig
c8520 6e 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20 20 ned char i, j;
c8530 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
c8540 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a te variables */.
c8550 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
c8560 73 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20 20 s[256];
c8570 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62 /* State variab
c8580 6c 65 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 les */.} sqlite3
c8590 50 72 6e 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 Prng;../*.** Get
c85a0 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 20 a single 8-bit
c85b0 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 6f random value fro
c85c0 6d 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e 20 m the RC4 PRNG.
c85d0 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d 75 The Mutex.** mu
c85e0 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 st be held while
c85f0 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 executing this
c8600 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 routine..**.** W
c8610 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65 20 hy not just use
c8620 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f 6d a library random
c8630 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65 20 generator like
c8640 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74 68 lrand48() for th
c8650 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 is?.** Because t
c8660 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 6f he OP_NewRowid o
c8670 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44 42 pcode in the VDB
c8680 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61 76 E depends on hav
c8690 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67 6f ing a very.** go
c86a0 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61 6e od source of ran
c86b0 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 dom numbers. Th
c86c0 65 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62 72 e lrand48() libr
c86d0 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 ary function may
c86e0 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f 64 .** well be good
c86f0 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d 61 enough. But ma
c8700 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 79 ybe not. Or may
c8710 62 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61 73 be lrand48() has
c8720 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 20 some.** subtle
c8730 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65 problems on some
c8740 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 63 6f systems that co
c8750 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c 65 uld cause proble
c8760 6d 73 2e 20 20 49 74 20 69 73 20 68 61 72 64 0a ms. It is hard.
c8770 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20 ** to know. To
c8780 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69 73 minimize the ris
c8790 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64 75 k of problems du
c87a0 65 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34 38 e to bad lrand48
c87b0 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 ().** implementa
c87c0 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75 73 tions, SQLite us
c87d0 65 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20 6e es this random n
c87e0 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 umber generator
c87f0 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c based.** on RC4,
c8800 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 77 which we know w
c8810 6f 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e 0a orks very well..
c8820 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20 20 **.** (Later):
c8830 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77 Actually, OP_New
c8840 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20 64 Rowid does not d
c8850 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 20 epend on a good
c8860 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61 6e source of.** ran
c8870 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72 65 domness any more
c8880 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c . But we will l
c8890 65 61 76 65 20 74 68 69 73 20 63 6f 64 65 20 69 eave this code i
c88a0 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e 0a n all the same..
c88b0 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 72 61 6e */.static u8 ran
c88c0 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a 20 domByte(void){.
c88d0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 unsigned char t
c88e0 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 77 73 ;... /* The "ws
c88f0 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69 6c dPrng" macro wil
c8900 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 l resolve to the
c8910 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e pseudo-random n
c8920 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 0a umber generator.
c8930 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f ** state vecto
c8940 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 r. If writable
c8950 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 static data is u
c8960 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 nsupported on th
c8970 65 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a 20 77 e target,. ** w
c8980 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 e have to locate
c8990 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 6f the state vecto
c89a0 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 r at run-time.
c89b0 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d In the more comm
c89c0 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 68 65 on. ** case whe
c89d0 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 re writable stat
c89e0 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f ic data is suppo
c89f0 72 74 65 64 2c 20 77 73 64 50 72 6e 67 20 63 61 rted, wsdPrng ca
c8a00 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 n refer directly
c8a10 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 . ** to the "sq
c8a20 6c 69 74 65 33 50 72 6e 67 22 20 73 74 61 74 65 lite3Prng" state
c8a30 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 vector declared
c8a40 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69 66 above.. */.#if
c8a50 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
c8a60 57 53 44 0a 20 20 73 74 72 75 63 74 20 73 71 6c WSD. struct sql
c8a70 69 74 65 33 50 72 6e 67 54 79 70 65 20 2a 70 20 ite3PrngType *p
c8a80 3d 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 = &GLOBAL(struct
c8a90 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 sqlite3PrngType
c8aa0 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 3b 0a , sqlite3Prng);.
c8ab0 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e 67 # define wsdPrng
c8ac0 20 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 p[0].#else.# de
c8ad0 66 69 6e 65 20 77 73 64 50 72 6e 67 20 73 71 6c fine wsdPrng sql
c8ae0 69 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 66 0a ite3Prng.#endif.
c8af0 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a .. /* Initializ
c8b00 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 e the state of t
c8b10 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 he random number
c8b20 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65 2c generator once,
c8b30 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 . ** the first
c8b40 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e time this routin
c8b50 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 e is called. Th
c8b60 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f 65 e seed value doe
c8b70 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 s. ** not need
c8b80 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f 74 to contain a lot
c8b90 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 73 of randomness s
c8ba0 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74 0a ince we are not.
c8bb0 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 64 ** trying to d
c8bc0 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79 70 74 o secure encrypt
c8bd0 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67 20 ion or anything
c8be0 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20 2a like that.... *
c8bf0 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20 69 *. ** Nothing i
c8c00 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20 61 n this file or a
c8c10 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e 20 nywhere else in
c8c20 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79 20 SQLite does any
c8c30 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e 63 kind of. ** enc
c8c40 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52 43 ryption. The RC
c8c50 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 62 4 algorithm is b
c8c60 65 69 6e 67 20 75 73 65 64 20 61 73 20 61 20 50 eing used as a P
c8c70 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e 64 RNG (pseudo-rand
c8c80 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 om. ** number g
c8c90 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61 73 enerator) not as
c8ca0 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20 64 an encryption d
c8cb0 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 evice.. */. if
c8cc0 28 20 21 77 73 64 50 72 6e 67 2e 69 73 49 6e 69 ( !wsdPrng.isIni
c8cd0 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a t ){. int i;.
c8ce0 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b char k[256];
c8cf0 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 3d . wsdPrng.j =
c8d00 20 30 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 0;. wsdPrng.
c8d10 69 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 i = 0;. sqlit
c8d20 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 e3OsRandomness(s
c8d30 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 qlite3_vfs_find(
c8d40 30 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 20 20 0), 256, k);.
c8d50 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b for(i=0; i<256;
c8d60 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73 64 i++){. wsd
c8d70 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38 29 Prng.s[i] = (u8)
c8d80 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 i;. }. for
c8d90 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b (i=0; i<256; i++
c8da0 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 ){. wsdPrng
c8db0 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b .j += wsdPrng.s[
c8dc0 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 i] + k[i];.
c8dd0 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 t = wsdPrng.s[w
c8de0 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 sdPrng.j];.
c8df0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
c8e00 6e 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e ng.j] = wsdPrng.
c8e10 73 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 50 s[i];. wsdP
c8e20 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 rng.s[i] = t;.
c8e30 20 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 2e }. wsdPrng.
c8e40 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a isInit = 1;. }.
c8e50 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 . /* Generate a
c8e60 6e 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65 nd return single
c8e70 20 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a random byte. *
c8e80 2f 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b /. wsdPrng.i++;
c8e90 0a 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 . t = wsdPrng.s
c8ea0 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77 [wsdPrng.i];. w
c8eb0 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 sdPrng.j += t;.
c8ec0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
c8ed0 6e 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e ng.i] = wsdPrng.
c8ee0 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 s[wsdPrng.j];.
c8ef0 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e wsdPrng.s[wsdPrn
c8f00 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d g.j] = t;. t +=
c8f10 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 wsdPrng.s[wsdPr
c8f20 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20 ng.i];. return
c8f30 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a wsdPrng.s[t];.}.
c8f40 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20 ./*.** Return N
c8f50 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f random bytes..*/
c8f60 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
c8f70 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e sqlite3_randomn
c8f80 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 ess(int N, void
c8f90 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e *pBuf){. unsign
c8fa0 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20 ed char *zBuf =
c8fb0 70 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 45 pBuf;.#if SQLITE
c8fc0 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 _THREADSAFE. sq
c8fd0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
c8fe0 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
c8ff0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
c9000 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 29 TEX_STATIC_PRNG)
c9010 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 ;.#endif. sqlit
c9020 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
c9030 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 20 utex);. while(
c9040 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75 N-- ){. *(zBu
c9050 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74 f++) = randomByt
c9060 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 e();. }. sqlit
c9070 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
c9080 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 utex);.}..#ifnde
c9090 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 f SQLITE_OMIT_BU
c90a0 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a ILTIN_TEST./*.**
c90b0 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 For testing pur
c90c0 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 69 poses, we someti
c90d0 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65 73 mes want to pres
c90e0 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20 6f erve the state o
c90f0 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 65 f.** PRNG and re
c9100 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20 74 store the PRNG t
c9110 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61 74 o its saved stat
c9120 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d e at a later tim
c9130 65 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65 e, or.** to rese
c9140 74 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74 t the PRNG to it
c9150 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e s initial state.
c9160 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 These routines
c9170 20 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74 accomplish.** t
c9180 68 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a hose tasks..**.*
c9190 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 * The sqlite3_te
c91a0 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 st_control() int
c91b0 65 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68 65 erface calls the
c91c0 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a se routines to.*
c91d0 2a 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52 * control the PR
c91e0 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 NG..*/.static SQ
c91f0 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 LITE_WSD struct
c9200 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 sqlite3PrngType
c9210 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 sqlite3SavedPrng
c9220 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
c9230 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e void sqlite3Prn
c9240 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 29 gSaveState(void)
c9250 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 20 {. memcpy(.
c9260 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 &GLOBAL(struct s
c9270 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 qlite3PrngType,
c9280 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 sqlite3SavedPrng
c9290 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 ),. &GLOBAL(s
c92a0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
c92b0 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 gType, sqlite3Pr
c92c0 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28 ng),. sizeof(
c92d0 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 29 sqlite3Prng). )
c92e0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
c92f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
c9300 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 28 rngRestoreState(
c9310 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 28 void){. memcpy(
c9320 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 . &GLOBAL(str
c9330 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 uct sqlite3PrngT
c9340 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 ype, sqlite3Prng
c9350 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 ),. &GLOBAL(s
c9360 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
c9370 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 gType, sqlite3Sa
c9380 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 73 69 vedPrng),. si
c9390 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e 67 zeof(sqlite3Prng
c93a0 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f ). );.}.SQLITE_
c93b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
c93c0 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 61 ite3PrngResetSta
c93d0 74 65 28 76 6f 69 64 29 7b 0a 20 20 47 4c 4f 42 te(void){. GLOB
c93e0 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 AL(struct sqlite
c93f0 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 3PrngType, sqlit
c9400 65 33 50 72 6e 67 29 2e 69 73 49 6e 69 74 20 3d e3Prng).isInit =
c9410 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 0;.}.#endif /*
c9420 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
c9430 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a TIN_TEST */../**
c9440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
c9450 20 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a of random.c ***
c9460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
c9490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
c94a0 69 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a 2a in file utf.c **
c94b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c94c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c94d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
c94e0 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31 33 ** 2004 April 13
c94f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
c9500 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
c9510 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
c9520 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
c9530 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
c9540 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
c9550 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
c9560 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
c9570 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
c9580 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
c9590 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
c95a0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
c95b0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
c95c0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
c95d0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
c95e0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
c95f0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
c9600 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
c9610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
c9650 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
c9660 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 tains routines u
c9670 73 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 sed to translate
c9680 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c 20 between UTF-8,
c9690 0a 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46 2d .** UTF-16, UTF-
c96a0 31 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31 36 16BE, and UTF-16
c96b0 4c 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 LE..**.** Notes
c96c0 6f 6e 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 on UTF-8:.**.**
c96d0 20 20 42 79 74 65 2d 30 20 20 20 20 42 79 74 65 Byte-0 Byte
c96e0 2d 31 20 20 20 20 42 79 74 65 2d 32 20 20 20 20 -1 Byte-2
c96f0 42 79 74 65 2d 33 20 20 20 20 56 61 6c 75 65 0a Byte-3 Value.
c9700 2a 2a 20 20 30 78 78 78 78 78 78 78 20 20 20 20 ** 0xxxxxxx
c9710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9720 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 000
c9730 30 30 30 30 30 20 30 30 30 30 30 30 30 30 20 30 00000 00000000 0
c9740 78 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 30 79 xxxxxxx.** 110y
c9750 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20 yyyy 10xxxxxx
c9760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9770 20 20 20 20 20 30 30 30 30 30 30 30 30 20 30 30 00000000 00
c9780 30 30 30 79 79 79 20 79 79 78 78 78 78 78 78 0a 000yyy yyxxxxxx.
c9790 2a 2a 20 20 31 31 31 30 7a 7a 7a 7a 20 20 31 30 ** 1110zzzz 10
c97a0 79 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78 yyyyyy 10xxxxxx
c97b0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 000
c97c0 30 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 00000 zzzzyyyy y
c97d0 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 31 yxxxxxx.** 1111
c97e0 30 75 75 75 20 20 31 30 75 75 7a 7a 7a 7a 20 20 0uuu 10uuzzzz
c97f0 31 30 79 79 79 79 79 79 20 20 31 30 78 78 78 78 10yyyyyy 10xxxx
c9800 78 78 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a xx 000uuuuu zz
c9810 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a zzyyyy yyxxxxxx.
c9820 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f **.**.** Notes o
c9830 6e 20 55 54 46 2d 31 36 3a 20 20 28 77 69 74 68 n UTF-16: (with
c9840 20 77 77 77 77 2b 31 3d 3d 75 75 75 75 75 29 0a wwww+1==uuuuu).
c9850 2a 2a 0a 2a 2a 20 20 20 20 20 20 57 6f 72 64 2d **.** Word-
c9860 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
c9870 57 6f 72 64 2d 31 20 20 20 20 20 20 20 20 20 20 Word-1
c9880 56 61 6c 75 65 0a 2a 2a 20 20 31 31 30 31 31 30 Value.** 110110
c9890 77 77 20 77 77 7a 7a 7a 7a 79 79 20 20 20 31 31 ww wwzzzzyy 11
c98a0 30 31 31 31 79 79 20 79 79 78 78 78 78 78 78 20 0111yy yyxxxxxx
c98b0 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 000uuuuu zzzz
c98c0 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a yyyy yyxxxxxx.**
c98d0 20 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 zzzzyyyy yyxxx
c98e0 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 xxx
c98f0 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 00000
c9900 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 000 zzzzyyyy yyx
c9910 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42 xxxxx.**.**.** B
c9920 4f 4d 20 6f 72 20 42 79 74 65 20 4f 72 64 65 72 OM or Byte Order
c9930 20 4d 61 72 6b 3a 0a 2a 2a 20 20 20 20 20 30 78 Mark:.** 0x
c9940 66 66 20 30 78 66 65 20 20 20 6c 69 74 74 6c 65 ff 0xfe little
c9950 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 36 20 66 -endian utf-16 f
c9960 6f 6c 6c 6f 77 73 0a 2a 2a 20 20 20 20 20 30 78 ollows.** 0x
c9970 66 65 20 30 78 66 66 20 20 20 62 69 67 2d 65 6e fe 0xff big-en
c9980 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c dian utf-16 foll
c9990 6f 77 73 0a 2a 2a 0a 2a 2f 0a 2f 2a 20 23 69 6e ows.**.*/./* #in
c99a0 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e clude <assert.h>
c99b0 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
c99c0 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e ITE_AMALGAMATION
c99d0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
c99e0 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 76 61 wing constant va
c99f0 6c 75 65 20 69 73 20 75 73 65 64 20 62 79 20 74 lue is used by t
c9a00 68 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 he SQLITE_BIGEND
c9a10 49 41 4e 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 IAN and.** SQLIT
c9a20 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 6d E_LITTLEENDIAN m
c9a30 61 63 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 acros..*/.SQLITE
c9a40 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 _PRIVATE const i
c9a50 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 nt sqlite3one =
c9a60 31 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 1;.#endif /* SQL
c9a70 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e ITE_AMALGAMATION
c9a80 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 */../*.** This
c9a90 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20 lookup table is
c9aa0 75 73 65 64 20 74 6f 20 68 65 6c 70 20 64 65 63 used to help dec
c9ab0 6f 64 65 20 74 68 65 20 66 69 72 73 74 20 62 79 ode the first by
c9ac0 74 65 20 6f 66 0a 2a 2a 20 61 20 6d 75 6c 74 69 te of.** a multi
c9ad0 2d 62 79 74 65 20 55 54 46 38 20 63 68 61 72 61 -byte UTF8 chara
c9ae0 63 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 cter..*/.static
c9af0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
c9b00 68 61 72 20 73 71 6c 69 74 65 33 55 74 66 38 54 har sqlite3Utf8T
c9b10 72 61 6e 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 rans1[] = {. 0x
c9b20 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 00, 0x01, 0x02,
c9b30 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 0x03, 0x04, 0x05
c9b40 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 , 0x06, 0x07,.
c9b50 30 78 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61 0x08, 0x09, 0x0a
c9b60 2c 20 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78 , 0x0b, 0x0c, 0x
c9b70 30 64 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 0d, 0x0e, 0x0f,.
c9b80 20 20 30 78 31 30 2c 20 30 78 31 31 2c 20 30 78 0x10, 0x11, 0x
c9b90 31 32 2c 20 30 78 31 33 2c 20 30 78 31 34 2c 20 12, 0x13, 0x14,
c9ba0 30 78 31 35 2c 20 30 78 31 36 2c 20 30 78 31 37 0x15, 0x16, 0x17
c9bb0 2c 0a 20 20 30 78 31 38 2c 20 30 78 31 39 2c 20 ,. 0x18, 0x19,
c9bc0 30 78 31 61 2c 20 30 78 31 62 2c 20 30 78 31 63 0x1a, 0x1b, 0x1c
c9bd0 2c 20 30 78 31 64 2c 20 30 78 31 65 2c 20 30 78 , 0x1d, 0x1e, 0x
c9be0 31 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 1f,. 0x00, 0x01
c9bf0 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 , 0x02, 0x03, 0x
c9c00 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 04, 0x05, 0x06,
c9c10 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 0x07,. 0x08, 0x
c9c20 30 39 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20 09, 0x0a, 0x0b,
c9c30 30 78 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65 0x0c, 0x0d, 0x0e
c9c40 2c 20 30 78 30 66 2c 0a 20 20 30 78 30 30 2c 20 , 0x0f,. 0x00,
c9c50 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 0x01, 0x02, 0x03
c9c60 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 , 0x04, 0x05, 0x
c9c70 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 30 06, 0x07,. 0x00
c9c80 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 , 0x01, 0x02, 0x
c9c90 30 33 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 03, 0x00, 0x01,
c9ca0 30 78 30 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0x00, 0x00,.};..
c9cb0 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 .#define WRITE_U
c9cc0 54 46 38 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 TF8(zOut, c) {
c9cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9ce0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 \. if(
c9cf0 63 3c 30 78 30 30 30 38 30 20 29 7b 20 20 20 20 c<0x00080 ){
c9d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9d20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
c9d30 20 28 75 38 29 28 63 26 30 78 46 46 29 3b 20 20 (u8)(c&0xFF);
c9d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9d50 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 \. }
c9d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9d90 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 \. else if(
c9da0 63 3c 30 78 30 30 38 30 30 20 29 7b 20 20 20 20 c<0x00800 ){
c9db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9dc0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
c9dd0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 43 30 *zOut++ = 0xC0
c9de0 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 26 30 + (u8)((c>>6)&0
c9df0 78 31 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 x1F);
c9e00 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
c9e10 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 ++ = 0x80 + (u8)
c9e20 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 (c & 0x3F);
c9e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
c9e40 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 }
c9e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9e70 20 20 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 \. else
c9e80 69 66 28 20 63 3c 30 78 31 30 30 30 30 20 29 7b if( c<0x10000 ){
c9e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9eb0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
c9ec0 30 78 45 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 0xE0 + (u8)((c>>
c9ed0 31 32 29 26 30 78 30 46 29 3b 20 20 20 20 20 20 12)&0x0F);
c9ee0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
c9ef0 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 zOut++ = 0x80 +
c9f00 28 75 38 29 28 28 63 3e 3e 36 29 20 26 20 30 78 (u8)((c>>6) & 0x
c9f10 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 3F);
c9f20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
c9f30 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 = 0x80 + (u8)(c
c9f40 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 & 0x3F);
c9f50 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d \. }
c9f60 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 else{
c9f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9f90 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
c9fa0 2b 20 3d 20 30 78 46 30 20 2b 20 28 75 38 29 28 + = 0xF0 + (u8)(
c9fb0 28 63 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b (c>>18) & 0x07);
c9fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
c9fd0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 *zOut++ = 0x8
c9fe0 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 0 + (u8)((c>>12)
c9ff0 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 & 0x3F);
ca000 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
ca010 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 t++ = 0x80 + (u8
ca020 29 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 )((c>>6) & 0x3F)
ca030 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c ; \
ca040 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 . *zOut++ = 0
ca050 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 x80 + (u8)(c & 0
ca060 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 x3F);
ca070 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 \. }
ca080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca0b0 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 \.}..#define WR
ca0c0 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 4f 75 74 ITE_UTF16LE(zOut
ca0d0 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 , c) {
ca0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca0f0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 \. if
ca100 28 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 ( c<=0xFFFF ){
ca110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca140 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
ca150 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 (u8)(c&0x00FF);
ca160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca180 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
ca190 74 2b 2b 20 3d 20 28 75 38 29 28 28 63 3e 3e 38 t++ = (u8)((c>>8
ca1a0 29 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 )&0x00FF);
ca1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca1c0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ca1d0 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 }else{
ca1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca210 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
ca220 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26 = (u8)(((c>>10)&
ca230 30 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 0x003F) + (((c-0
ca240 78 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 x10000)>>10)&0x0
ca250 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 0C0)); \. *z
ca260 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 Out++ = (u8)(0x0
ca270 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31 30 30 0D8 + (((c-0x100
ca280 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 00)>>18)&0x03));
ca290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ca2a0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 *zOut++ = (u
ca2b0 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 8)(c&0x00FF);
ca2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca2e0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
ca2f0 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 43 20 + = (u8)(0x00DC
ca300 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29 + ((c>>8)&0x03))
ca310 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ca320 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 \. }
ca330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca370 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 \.}..#define WRI
ca380 54 45 5f 55 54 46 31 36 42 45 28 7a 4f 75 74 2c TE_UTF16BE(zOut,
ca390 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 c) {
ca3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca3b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 \. if(
ca3c0 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 c<=0xFFFF ){
ca3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ca400 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
ca410 75 38 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46 u8)((c>>8)&0x00F
ca420 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
ca430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca440 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
ca450 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 ++ = (u8)(c&0x00
ca460 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 FF);
ca470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca480 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d \. }
ca490 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 else{
ca4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca4d0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
ca4e0 20 28 75 38 29 28 30 78 30 30 44 38 20 2b 20 28 (u8)(0x00D8 + (
ca4f0 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 ((c-0x10000)>>18
ca500 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 )&0x03));
ca510 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
ca520 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 28 63 3e ut++ = (u8)(((c>
ca530 3e 31 30 29 26 30 78 30 30 33 46 29 20 2b 20 28 >10)&0x003F) + (
ca540 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 ((c-0x10000)>>10
ca550 29 26 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 )&0x00C0)); \.
ca560 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 *zOut++ = (u8
ca570 29 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e )(0x00DC + ((c>>
ca580 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 8)&0x03));
ca590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca5a0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
ca5b0 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46 = (u8)(c&0x00FF
ca5c0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
ca5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca5e0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 \. }
ca5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ca630 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 .}..#define READ
ca640 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 54 45 _UTF16LE(zIn, TE
ca650 52 4d 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20 RM, c){
ca660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca670 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 \. c
ca680 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 = (*zIn++);
ca690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca6c0 20 20 5c 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49 \. c += ((*zI
ca6d0 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 n++)<<8);
ca6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca700 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 \. if
ca710 28 20 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 ( c>=0xD800 && c
ca720 3c 30 78 45 30 30 30 20 26 26 20 54 45 52 4d 20 <0xE000 && TERM
ca730 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
ca740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca750 20 20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 3d \. int c2 =
ca760 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 (*zIn++);
ca770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca790 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ca7a0 63 32 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c c2 += ((*zIn++)<
ca7b0 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 <8);
ca7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca7e0 20 20 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 26 \. c = (c2&
ca7f0 30 78 30 33 46 46 29 20 2b 20 28 28 63 26 30 78 0x03FF) + ((c&0x
ca800 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 003F)<<10) + (((
ca810 63 26 30 78 30 33 43 30 29 2b 30 78 30 30 34 30 c&0x03C0)+0x0040
ca820 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20 )<<10); \. }
ca830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca870 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 \.}..#define R
ca880 45 41 44 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c EAD_UTF16BE(zIn,
ca890 20 54 45 52 4d 2c 20 63 29 7b 20 20 20 20 20 20 TERM, c){
ca8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ca8c0 20 63 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c c = ((*zIn++)<<
ca8d0 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 8);
ca8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca900 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28 2a \. c += (*
ca910 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 zIn++);
ca920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca940 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ca950 20 69 66 28 20 63 3e 3d 30 78 44 38 30 30 20 26 if( c>=0xD800 &
ca960 26 20 63 3c 30 78 45 30 30 30 20 26 26 20 54 45 & c<0xE000 && TE
ca970 52 4d 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 RM ){
ca980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca990 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63 \. int c
ca9a0 32 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 2 = ((*zIn++)<<8
ca9b0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
ca9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ca9e0 20 20 20 63 32 20 2b 3d 20 28 2a 7a 49 6e 2b 2b c2 += (*zIn++
ca9f0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
caa00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
caa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
caa20 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28 \. c = (
caa30 63 32 26 30 78 30 33 46 46 29 20 2b 20 28 28 63 c2&0x03FF) + ((c
caa40 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 &0x003F)<<10) +
caa50 28 28 28 63 26 30 78 30 33 43 30 29 2b 30 78 30 (((c&0x03C0)+0x0
caa60 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 040)<<10); \.
caa70 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
caa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
caa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
caaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
caab0 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 \.}../*.**
caac0 54 72 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 Translate a sing
caad0 6c 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 le UTF-8 charact
caae0 65 72 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 er. Return the
caaf0 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 2e 0a 2a unicode value..*
cab00 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 74 72 61 6e *.** During tran
cab10 73 6c 61 74 69 6f 6e 2c 20 61 73 73 75 6d 65 20 slation, assume
cab20 74 68 61 74 20 74 68 65 20 62 79 74 65 20 74 68 that the byte th
cab30 61 74 20 7a 54 65 72 6d 20 70 6f 69 6e 74 73 0a at zTerm points.
cab40 2a 2a 20 69 73 20 61 20 30 78 30 30 2e 0a 2a 2a ** is a 0x00..**
cab50 0a 2a 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e .** Write a poin
cab60 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ter to the next
cab70 75 6e 72 65 61 64 20 62 79 74 65 20 62 61 63 6b unread byte back
cab80 20 69 6e 74 6f 20 2a 70 7a 4e 65 78 74 2e 0a 2a into *pzNext..*
cab90 2a 0a 2a 2a 20 4e 6f 74 65 73 20 4f 6e 20 49 6e *.** Notes On In
caba0 76 61 6c 69 64 20 55 54 46 2d 38 3a 0a 2a 2a 0a valid UTF-8:.**.
cabb0 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 ** * This rout
cabc0 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 ine never allows
cabd0 20 61 20 37 2d 62 69 74 20 63 68 61 72 61 63 74 a 7-bit charact
cabe0 65 72 20 28 30 78 30 30 20 74 68 72 6f 75 67 68 er (0x00 through
cabf0 20 30 78 37 66 29 20 74 6f 0a 2a 2a 20 20 20 20 0x7f) to.**
cac00 20 62 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61 be encoded as a
cac10 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 multi-byte char
cac20 61 63 74 65 72 2e 20 20 41 6e 79 20 6d 75 6c 74 acter. Any mult
cac30 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 i-byte character
cac40 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 61 74 74 that.** att
cac50 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 empts to encode
cac60 61 20 76 61 6c 75 65 20 62 65 74 77 65 65 6e 20 a value between
cac70 30 78 30 30 20 61 6e 64 20 30 78 37 66 20 69 73 0x00 and 0x7f is
cac80 20 72 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 rendered as 0xf
cac90 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 ffd..**.** * T
caca0 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 his routine neve
cacb0 72 20 61 6c 6c 6f 77 73 20 61 20 55 54 46 31 36 r allows a UTF16
cacc0 20 73 75 72 72 6f 67 61 74 65 20 76 61 6c 75 65 surrogate value
cacd0 20 74 6f 20 62 65 20 65 6e 63 6f 64 65 64 2e 0a to be encoded..
cace0 2a 2a 20 20 20 20 20 49 66 20 61 20 6d 75 6c 74 ** If a mult
cacf0 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 i-byte character
cad00 20 61 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 attempts to enc
cad10 6f 64 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 ode a value betw
cad20 65 65 6e 0a 2a 2a 20 20 20 20 20 30 78 64 38 30 een.** 0xd80
cad30 30 20 61 6e 64 20 30 78 65 30 30 30 20 74 68 65 0 and 0xe000 the
cad40 6e 20 69 74 20 69 73 20 72 65 6e 64 65 72 65 64 n it is rendered
cad50 20 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a as 0xfffd..**.*
cad60 2a 20 20 2a 20 20 42 79 74 65 73 20 69 6e 20 74 * * Bytes in t
cad70 68 65 20 72 61 6e 67 65 20 6f 66 20 30 78 38 30 he range of 0x80
cad80 20 74 68 72 6f 75 67 68 20 30 78 62 66 20 77 68 through 0xbf wh
cad90 69 63 68 20 6f 63 63 75 72 20 61 73 20 74 68 65 ich occur as the
cada0 20 66 69 72 73 74 0a 2a 2a 20 20 20 20 20 62 79 first.** by
cadb0 74 65 20 6f 66 20 61 20 63 68 61 72 61 63 74 65 te of a characte
cadc0 72 20 61 72 65 20 69 6e 74 65 72 70 72 65 74 65 r are interprete
cadd0 64 20 61 73 20 73 69 6e 67 6c 65 2d 62 79 74 65 d as single-byte
cade0 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 characters.**
cadf0 20 20 20 61 6e 64 20 72 65 6e 64 65 72 65 64 20 and rendered
cae00 61 73 20 74 68 65 6d 73 65 6c 76 65 73 20 65 76 as themselves ev
cae10 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79 20 61 en though they a
cae20 72 65 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a re technically.*
cae30 2a 20 20 20 20 20 69 6e 76 61 6c 69 64 20 63 68 * invalid ch
cae40 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 aracters..**.**
cae50 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 * This routine
cae60 20 61 63 63 65 70 74 73 20 61 6e 20 69 6e 66 69 accepts an infi
cae70 6e 69 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 nite number of d
cae80 69 66 66 65 72 65 6e 74 20 55 54 46 38 20 65 6e ifferent UTF8 en
cae90 63 6f 64 69 6e 67 73 0a 2a 2a 20 20 20 20 20 66 codings.** f
caea0 6f 72 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 or unicode value
caeb0 73 20 30 78 38 30 20 61 6e 64 20 67 72 65 61 74 s 0x80 and great
caec0 65 72 2e 20 20 49 74 20 64 6f 20 6e 6f 74 20 63 er. It do not c
caed0 68 61 6e 67 65 20 6f 76 65 72 2d 6c 65 6e 67 74 hange over-lengt
caee0 68 0a 2a 2a 20 20 20 20 20 65 6e 63 6f 64 69 6e h.** encodin
caef0 67 73 20 74 6f 20 30 78 66 66 66 64 20 61 73 20 gs to 0xfffd as
caf00 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 72 65 63 some systems rec
caf10 6f 6d 6d 65 6e 64 2e 0a 2a 2f 0a 23 64 65 66 69 ommend..*/.#defi
caf20 6e 65 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e ne READ_UTF8(zIn
caf30 2c 20 7a 54 65 72 6d 2c 20 63 29 20 20 20 20 20 , zTerm, c)
caf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
caf50 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 2a 28 \. c = *(
caf60 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 zIn++);
caf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
caf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
caf90 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 \. if( c>=0x
cafa0 63 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 c0 ){
cafb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cafc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cafd0 5c 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 \. c = sqlite
cafe0 33 55 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 3Utf8Trans1[c-0x
caff0 63 30 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 c0];
cb000 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
cb010 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a while( zIn!=z
cb020 54 65 72 6d 20 26 26 20 28 2a 7a 49 6e 20 26 20 Term && (*zIn &
cb030 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 20 0xc0)==0x80 ){
cb040 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
cb050 20 20 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 c = (c<<6) + (
cb060 30 78 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 0x3f & *(zIn++))
cb070 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
cb080 20 20 20 20 20 20 20 5c 0a 20 20 20 20 7d 20 20 \. }
cb090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cb0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cb0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cb0c0 20 20 20 20 5c 0a 20 20 20 20 69 66 28 20 63 3c \. if( c<
cb0d0 30 78 38 30 20 20 20 20 20 20 20 20 20 20 20 20 0x80
cb0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cb0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cb100 20 5c 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 \. || (c
cb110 26 30 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 &0xFFFFF800)==0x
cb120 44 38 30 30 20 20 20 20 20 20 20 20 20 20 20 20 D800
cb130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
cb140 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 || (c&0x
cb150 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 FFFFFFFE)==0xFFF
cb160 45 20 29 7b 20 20 63 20 3d 20 30 78 46 46 46 44 E ){ c = 0xFFFD
cb170 3b 20 7d 20 20 20 20 20 20 20 20 5c 0a 20 20 7d ; } \. }
cb180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
cb190 75 33 32 20 73 71 6c 69 74 65 33 55 74 66 38 52 u32 sqlite3Utf8R
cb1a0 65 61 64 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 ead(. const uns
cb1b0 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 70 7a 20 igned char **pz
cb1c0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
cb1d0 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 77 68 69 string from whi
cb1e0 63 68 20 74 6f 20 72 65 61 64 20 63 68 61 72 20 ch to read char
cb1f0 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 */.){. unsigned
cb200 20 69 6e 74 20 63 3b 0a 0a 20 20 2f 2a 20 53 61 int c;.. /* Sa
cb210 6d 65 20 61 73 20 52 45 41 44 5f 55 54 46 38 28 me as READ_UTF8(
cb220 29 20 61 62 6f 76 65 20 62 75 74 20 77 69 74 68 ) above but with
cb230 6f 75 74 20 74 68 65 20 7a 54 65 72 6d 20 70 61 out the zTerm pa
cb240 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2a 20 46 6f rameter.. ** Fo
cb250 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 r this routine,
cb260 77 65 20 61 73 73 75 6d 65 20 74 68 65 20 55 54 we assume the UT
cb270 46 38 20 73 74 72 69 6e 67 20 69 73 20 61 6c 77 F8 string is alw
cb280 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 ays zero-termina
cb290 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 63 20 3d 20 ted.. */. c =
cb2a0 2a 28 28 2a 70 7a 29 2b 2b 29 3b 0a 20 20 69 66 *((*pz)++);. if
cb2b0 28 20 63 3e 3d 30 78 63 30 20 29 7b 0a 20 20 20 ( c>=0xc0 ){.
cb2c0 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 c = sqlite3Utf8
cb2d0 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 0a Trans1[c-0xc0];.
cb2e0 20 20 20 20 77 68 69 6c 65 28 20 28 2a 28 2a 70 while( (*(*p
cb2f0 7a 29 20 26 20 30 78 63 30 29 3d 3d 30 78 38 30 z) & 0xc0)==0x80
cb300 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 28 63 ){. c = (c
cb310 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 20 2a <<6) + (0x3f & *
cb320 28 28 2a 70 7a 29 2b 2b 29 29 3b 0a 20 20 20 20 ((*pz)++));.
cb330 7d 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 }. if( c<0x80
cb340 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 . || (c&0
cb350 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 xFFFFF800)==0xD8
cb360 30 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 00. || (c
cb370 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 &0xFFFFFFFE)==0x
cb380 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46 FFFE ){ c = 0xF
cb390 46 46 44 3b 20 7d 0a 20 20 7d 0a 20 20 72 65 74 FFD; }. }. ret
cb3a0 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a 0a urn c;.}...../*.
cb3b0 2a 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53 4c ** If the TRANSL
cb3c0 41 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f 20 ATE_TRACE macro
cb3d0 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 20 is defined, the
cb3e0 76 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d 65 value of each Me
cb3f0 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64 20 m is.** printed
cb400 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68 65 on stderr on the
cb410 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f 75 way into and ou
cb420 74 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 t of sqlite3Vdbe
cb430 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e 0a MemTranslate()..
cb440 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20 54 */ ./* #define T
cb450 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 31 RANSLATE_TRACE 1
cb460 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
cb470 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f ITE_OMIT_UTF16./
cb480 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
cb490 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68 65 e transforms the
cb4a0 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20 65 internal text e
cb4b0 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 ncoding used by
cb4c0 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69 72 pMem to.** desir
cb4d0 65 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e 20 edEnc. It is an
cb4e0 65 72 72 6f 72 20 69 66 20 74 68 65 20 73 74 72 error if the str
cb4f0 69 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20 6f ing is already o
cb500 66 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a f the desired.**
cb510 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69 66 encoding, or if
cb520 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74 20 *pMem does not
cb530 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e 67 contain a string
cb540 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 value..*/.SQLIT
cb550 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
cb560 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e lite3VdbeMemTran
cb570 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c slate(Mem *pMem,
cb580 20 75 38 20 64 65 73 69 72 65 64 45 6e 63 29 7b u8 desiredEnc){
cb590 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 . int len;
cb5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
cb5b0 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 * Maximum length
cb5c0 20 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69 6e of output strin
cb5d0 67 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 g in bytes */.
cb5e0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
cb5f0 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 Out;
cb600 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 /* Output
cb610 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73 69 buffer */. unsi
cb620 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 gned char *zIn;
cb630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cb640 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72 61 /* Input itera
cb650 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 tor */. unsigne
cb660 64 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20 d char *zTerm;
cb670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
cb680 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 20 2a * End of input *
cb690 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
cb6a0 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 r *z;
cb6b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 /* Out
cb6c0 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a put iterator */.
cb6d0 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 unsigned int c
cb6e0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 ;.. assert( pMe
cb6f0 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
cb700 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
cb710 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
cb720 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 );. assert( pMe
cb730 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 m->flags&MEM_Str
cb740 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d );. assert( pM
cb750 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 em->enc!=desired
cb760 45 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 Enc );. assert(
cb770 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 29 3b pMem->enc!=0 );
cb780 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d . assert( pMem-
cb790 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 64 65 >n>=0 );..#if de
cb7a0 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45 5f fined(TRANSLATE_
cb7b0 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65 TRACE) && define
cb7c0 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a d(SQLITE_DEBUG).
cb7d0 20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 {. char zBu
cb7e0 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 f[100];. sqli
cb7f0 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 te3VdbeMemPretty
cb800 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75 66 Print(pMem, zBuf
cb810 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 );. fprintf(s
cb820 74 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20 20 tderr, "INPUT:
cb830 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20 %s\n", zBuf);.
cb840 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 }.#endif.. /* I
cb850 66 20 74 68 65 20 74 72 61 6e 73 6c 61 74 69 6f f the translatio
cb860 6e 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 n is between UTF
cb870 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 20 62 -16 little and b
cb880 69 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 6e 20 ig endian, then
cb890 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 . ** all that i
cb8a0 73 20 72 65 71 75 69 72 65 64 20 69 73 20 74 6f s required is to
cb8b0 20 73 77 61 70 20 74 68 65 20 62 79 74 65 20 6f swap the byte o
cb8c0 72 64 65 72 2e 20 54 68 69 73 20 63 61 73 65 20 rder. This case
cb8d0 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a 2a 20 is handled. **
cb8e0 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d differently from
cb8f0 20 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 20 2a the others.. *
cb900 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e /. if( pMem->en
cb910 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 c!=SQLITE_UTF8 &
cb920 26 20 64 65 73 69 72 65 64 45 6e 63 21 3d 53 51 & desiredEnc!=SQ
cb930 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 LITE_UTF8 ){.
cb940 20 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 69 6e u8 temp;. in
cb950 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 t rc;. rc = s
cb960 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b qlite3VdbeMemMak
cb970 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29 eWriteable(pMem)
cb980 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
cb990 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
cb9a0 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
cb9b0 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 ITE_NOMEM );.
cb9c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
cb9d0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
cb9e0 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 zIn = (u8*)pMe
cb9f0 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 6d 20 m->z;. zTerm
cba00 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26 7e = &zIn[pMem->n&~
cba10 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 1];. while( z
cba20 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 In<zTerm ){.
cba30 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a 20 temp = *zIn;.
cba40 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a 49 *zIn = *(zI
cba50 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e 2b n+1);. zIn+
cba60 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b 20 +;. *zIn++
cba70 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 = temp;. }.
cba80 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 pMem->enc = de
cba90 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67 6f siredEnc;. go
cbaa0 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74 to translate_out
cbab0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 ;. }.. /* Set
cbac0 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69 6d len to the maxim
cbad0 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 um number of byt
cbae0 65 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 74 es required in t
cbaf0 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 he output buffer
cbb00 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69 72 . */. if( desir
cbb10 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 edEnc==SQLITE_UT
cbb20 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65 F8 ){. /* Whe
cbb30 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f n converting fro
cbb40 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d 61 m UTF-16, the ma
cbb50 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65 73 ximum growth res
cbb60 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a ults from. **
cbb70 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20 32 translating a 2
cbb80 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 -byte character
cbb90 74 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46 2d to a 4-byte UTF-
cbba0 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 8 character..
cbbb0 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79 74 ** A single byt
cbbc0 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f e is required fo
cbbd0 72 20 74 68 65 20 6f 75 74 70 75 74 20 73 74 72 r the output str
cbbe0 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 ing. ** nul-t
cbbf0 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a erminator.. *
cbc00 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 26 3d /. pMem->n &=
cbc10 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 ~1;. len = p
cbc20 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b 0a Mem->n * 2 + 1;.
cbc30 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
cbc40 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 When converting
cbc50 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 from UTF-8 to UT
cbc60 46 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75 6d F-16 the maximum
cbc70 20 67 72 6f 77 74 68 20 69 73 20 63 61 75 73 65 growth is cause
cbc80 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61 20 d. ** when a
cbc90 31 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 61 1-byte UTF-8 cha
cbca0 72 61 63 74 65 72 20 69 73 20 74 72 61 6e 73 6c racter is transl
cbcb0 61 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62 79 ated into a 2-by
cbcc0 74 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a 2a te UTF-16. **
cbcd0 20 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f 20 character. Two
cbce0 62 79 74 65 73 20 61 72 65 20 72 65 71 75 69 72 bytes are requir
cbcf0 65 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 ed in the output
cbd00 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 65 0a buffer for the.
cbd10 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 ** nul-termi
cbd20 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 nator.. */.
cbd30 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 len = pMem->n
cbd40 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20 20 * 2 + 2;. }..
cbd50 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70 6f /* Set zIn to po
cbd60 69 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 74 int at the start
cbd70 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62 75 of the input bu
cbd80 66 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20 74 ffer and zTerm t
cbd90 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20 62 o point 1. ** b
cbda0 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 yte past the end
cbdb0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 .. **. ** Vari
cbdc0 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65 74 able zOut is set
cbdd0 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 to point at the
cbde0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c 20 output buffer,
cbdf0 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a 20 space obtained.
cbe00 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ** from sqlite3
cbe10 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f 0a _malloc().. */.
cbe20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 zIn = (u8*)pMe
cbe30 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d 20 m->z;. zTerm =
cbe40 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 &zIn[pMem->n];.
cbe50 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 44 zOut = sqlite3D
cbe60 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d bMallocRaw(pMem-
cbe70 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 28 >db, len);. if(
cbe80 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72 65 !zOut ){. re
cbe90 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
cbea0 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f 75 M;. }. z = zOu
cbeb0 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e t;.. if( pMem->
cbec0 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 enc==SQLITE_UTF8
cbed0 20 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73 69 ){. if( desi
cbee0 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 redEnc==SQLITE_U
cbef0 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20 TF16LE ){.
cbf00 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d /* UTF-8 -> UTF-
cbf10 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 16 Little-endian
cbf20 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 */. while(
cbf30 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 zIn<zTerm ){.
cbf40 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 28 READ_UTF8(
cbf50 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a zIn, zTerm, c);.
cbf60 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 WRITE_UT
cbf70 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 F16LE(z, c);.
cbf80 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
cbf90 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 65 assert( de
cbfa0 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 siredEnc==SQLITE
cbfb0 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 20 20 _UTF16BE );.
cbfc0 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 /* UTF-8 -> UT
cbfd0 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e 20 F-16 Big-endian
cbfe0 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 */. while(
cbff0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 zIn<zTerm ){.
cc000 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 28 7a READ_UTF8(z
cc010 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 In, zTerm, c);.
cc020 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 WRITE_UTF
cc030 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 16BE(z, c);.
cc040 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d }. }. pM
cc050 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 em->n = (int)(z
cc060 2d 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2a 7a 2b - zOut);. *z+
cc070 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a + = 0;. }else{.
cc080 20 20 20 20 61 73 73 65 72 74 28 20 64 65 73 69 assert( desi
cc090 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 redEnc==SQLITE_U
cc0a0 54 46 38 20 29 3b 0a 20 20 20 20 69 66 28 20 70 TF8 );. if( p
cc0b0 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 Mem->enc==SQLITE
cc0c0 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 _UTF16LE ){.
cc0d0 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c 69 74 74 /* UTF-16 Litt
cc0e0 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 le-endian -> UTF
cc0f0 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c -8 */. whil
cc100 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a e( zIn<zTerm ){.
cc110 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 READ_UTF
cc120 31 36 4c 45 28 7a 49 6e 2c 20 7a 49 6e 3c 7a 54 16LE(zIn, zIn<zT
cc130 65 72 6d 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 erm, c); .
cc140 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 WRITE_UTF8(z,
cc150 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 c);. }.
cc160 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 }else{. /*
cc170 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 UTF-16 Big-endia
cc180 6e 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 n -> UTF-8 */.
cc190 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a while( zIn<z
cc1a0 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 Term ){.
cc1b0 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 49 6e READ_UTF16BE(zIn
cc1c0 2c 20 7a 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b , zIn<zTerm, c);
cc1d0 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f . WRITE_
cc1e0 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 UTF8(z, c);.
cc1f0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d }. }. pM
cc200 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 em->n = (int)(z
cc210 2d 20 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a - zOut);. }. *
cc220 7a 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 z = 0;. assert(
cc230 20 28 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 (pMem->n+(desir
cc240 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 edEnc==SQLITE_UT
cc250 46 38 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b F8?1:2))<=len );
cc260 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d .. sqlite3VdbeM
cc270 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b emRelease(pMem);
cc280 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 . pMem->flags &
cc290 3d 20 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d = ~(MEM_Static|M
cc2a0 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d EM_Dyn|MEM_Ephem
cc2b0 29 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d );. pMem->enc =
cc2c0 20 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 desiredEnc;. p
cc2d0 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d Mem->flags |= (M
cc2e0 45 4d 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 EM_Term|MEM_Dyn)
cc2f0 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 ;. pMem->z = (c
cc300 68 61 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 har*)zOut;. pMe
cc310 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 m->zMalloc = pMe
cc320 6d 2d 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 m->z;..translate
cc330 5f 6f 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 _out:.#if define
cc340 64 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 d(TRANSLATE_TRAC
cc350 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 E) && defined(SQ
cc360 4c 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a LITE_DEBUG). {.
cc370 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 char zBuf[10
cc380 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 0];. sqlite3V
cc390 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e dbeMemPrettyPrin
cc3a0 74 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 t(pMem, zBuf);.
cc3b0 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 fprintf(stder
cc3c0 72 2c 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e r, "OUTPUT: %s\n
cc3d0 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 ", zBuf);. }.#e
cc3e0 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 ndif. return SQ
cc3f0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
cc400 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
cc410 68 65 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65 hecks for a byte
cc420 2d 6f 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74 -order mark at t
cc430 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
cc440 74 68 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 the .** UTF-16 s
cc450 74 72 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 tring stored in
cc460 2a 70 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 *pMem. If one is
cc470 20 70 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20 present, it is
cc480 72 65 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 removed and.** t
cc490 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 he encoding of t
cc4a0 68 65 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e he Mem adjusted.
cc4b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f This routine do
cc4c0 65 73 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a es not do any.**
cc4d0 20 62 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 byte-swapping,
cc4e0 69 74 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d it just sets Mem
cc4f0 2e 65 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65 .enc appropriate
cc500 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c ly..**.** The al
cc510 6c 6f 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63 location (static
cc520 2c 20 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 , dynamic etc.)
cc530 61 6e 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 and encoding of
cc540 74 68 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a the Mem may be.*
cc550 2a 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 * changed by thi
cc560 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 s function..*/.S
cc570 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
cc580 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
cc590 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 HandleBom(Mem *p
cc5a0 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d Mem){. int rc =
cc5b0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 SQLITE_OK;. u8
cc5c0 20 62 6f 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73 bom = 0;.. ass
cc5d0 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 ert( pMem->n>=0
cc5e0 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e );. if( pMem->n
cc5f0 3e 31 20 29 7b 0a 20 20 20 20 75 38 20 62 31 20 >1 ){. u8 b1
cc600 3d 20 2a 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a = *(u8 *)pMem->z
cc610 3b 0a 20 20 20 20 75 38 20 62 32 20 3d 20 2a 28 ;. u8 b2 = *(
cc620 28 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 ((u8 *)pMem->z)
cc630 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 20 62 31 + 1);. if( b1
cc640 3d 3d 30 78 46 45 20 26 26 20 62 32 3d 3d 30 78 ==0xFE && b2==0x
cc650 46 46 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 FF ){. bom
cc660 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 = SQLITE_UTF16BE
cc670 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
cc680 62 31 3d 3d 30 78 46 46 20 26 26 20 62 32 3d 3d b1==0xFF && b2==
cc690 30 78 46 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 0xFE ){. bo
cc6a0 6d 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 m = SQLITE_UTF16
cc6b0 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 LE;. }. }.
cc6c0 0a 20 20 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 . if( bom ){.
cc6d0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
cc6e0 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
cc6f0 6c 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 le(pMem);. if
cc700 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
cc710 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e ){. pMem->n
cc720 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d -= 2;. mem
cc730 6d 6f 76 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 move(pMem->z, &p
cc740 4d 65 6d 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d Mem->z[2], pMem-
cc750 3e 6e 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d >n);. pMem-
cc760 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c >z[pMem->n] = '\
cc770 30 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 0';. pMem->
cc780 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 z[pMem->n+1] = '
cc790 5c 30 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d \0';. pMem-
cc7a0 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 >flags |= MEM_Te
cc7b0 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e rm;. pMem->
cc7c0 65 6e 63 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d enc = bom;. }
cc7d0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
cc7e0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
cc7f0 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 LITE_OMIT_UTF16
cc800 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 */../*.** pZ is
cc810 61 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 a UTF-8 encoded
cc820 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 unicode string.
cc830 49 66 20 6e 42 79 74 65 20 69 73 20 6c 65 73 73 If nByte is less
cc840 20 74 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 than zero,.** r
cc850 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
cc860 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 of unicode char
cc870 61 63 74 65 72 73 20 69 6e 20 70 5a 20 75 70 20 acters in pZ up
cc880 74 6f 20 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c to (but not incl
cc890 75 64 69 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 uding).** the fi
cc8a0 72 73 74 20 30 78 30 30 20 62 79 74 65 2e 20 49 rst 0x00 byte. I
cc8b0 66 20 6e 42 79 74 65 20 69 73 20 6e 6f 74 20 6c f nByte is not l
cc8c0 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 ess than zero, r
cc8d0 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d eturn the.** num
cc8e0 62 65 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 ber of unicode c
cc8f0 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 haracters in the
cc900 20 66 69 72 73 74 20 6e 42 79 74 65 20 6f 66 20 first nByte of
cc910 70 5a 20 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a pZ (or up to .**
cc920 20 74 68 65 20 66 69 72 73 74 20 30 78 30 30 2c the first 0x00,
cc930 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 whichever comes
cc940 20 66 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 first)..*/.SQLI
cc950 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
cc960 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 qlite3Utf8CharLe
cc970 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 n(const char *zI
cc980 6e 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 n, int nByte){.
cc990 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f int r = 0;. co
cc9a0 6e 73 74 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e nst u8 *z = (con
cc9b0 73 74 20 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f st u8*)zIn;. co
cc9c0 6e 73 74 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 nst u8 *zTerm;.
cc9d0 20 69 66 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b if( nByte>=0 ){
cc9e0 0a 20 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b . zTerm = &z[
cc9f0 6e 42 79 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b nByte];. }else{
cca00 0a 20 20 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f . zTerm = (co
cca10 6e 73 74 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 nst u8*)(-1);.
cca20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a }. assert( z<=z
cca30 54 65 72 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 Term );. while(
cca40 20 2a 7a 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 *z!=0 && z<zTer
cca50 6d 20 29 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f m ){. SQLITE_
cca60 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 SKIP_UTF8(z);.
cca70 20 20 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 r++;. }. ret
cca80 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 urn r;.}../* Thi
cca90 73 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 s test function
ccaa0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 is not currently
ccab0 20 75 73 65 64 20 62 79 20 74 68 65 20 61 75 74 used by the aut
ccac0 6f 6d 61 74 65 64 20 74 65 73 74 2d 73 75 69 74 omated test-suit
ccad0 65 2e 20 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 e. .** Hence it
ccae0 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c is only availabl
ccaf0 65 20 69 6e 20 64 65 62 75 67 20 62 75 69 6c 64 e in debug build
ccb00 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 s..*/.#if define
ccb10 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 d(SQLITE_TEST) &
ccb20 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 & defined(SQLITE
ccb30 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 _DEBUG)./*.** Tr
ccb40 61 6e 73 6c 61 74 65 20 55 54 46 2d 38 20 74 6f anslate UTF-8 to
ccb50 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 UTF-8..**.** Th
ccb60 69 73 20 68 61 73 20 74 68 65 20 65 66 66 65 63 is has the effec
ccb70 74 20 6f 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 t of making sure
ccb80 20 74 68 61 74 20 74 68 65 20 73 74 72 69 6e 67 that the string
ccb90 20 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a is well-formed.
ccba0 2a 2a 20 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f ** UTF-8. Misco
ccbb0 64 65 64 20 63 68 61 72 61 63 74 65 72 73 20 61 ded characters a
ccbc0 72 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a re removed..**.*
ccbd0 2a 20 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f * The translatio
ccbe0 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 n is done in-pla
ccbf0 63 65 20 61 6e 64 20 61 62 6f 72 74 65 64 20 69 ce and aborted i
ccc00 66 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 f the output.**
ccc10 6f 76 65 72 72 75 6e 73 20 74 68 65 20 69 6e 70 overruns the inp
ccc20 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ut..*/.SQLITE_PR
ccc30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
ccc40 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 3Utf8To8(unsigne
ccc50 64 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 d char *zIn){.
ccc60 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
ccc70 4f 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 Out = zIn;. uns
ccc80 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 61 igned char *zSta
ccc90 72 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20 rt = zIn;. u32
ccca0 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e c;.. while( zIn
cccb0 5b 30 5d 20 26 26 20 7a 4f 75 74 3c 3d 7a 49 6e [0] && zOut<=zIn
cccc0 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 ){. c = sqli
cccd0 74 65 33 55 74 66 38 52 65 61 64 28 28 63 6f 6e te3Utf8Read((con
ccce0 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 0a 20 st u8**)&zIn);.
cccf0 20 20 20 69 66 28 20 63 21 3d 30 78 66 66 66 64 if( c!=0xfffd
ccd00 20 29 7b 0a 20 20 20 20 20 20 57 52 49 54 45 5f ){. WRITE_
ccd10 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b 0a 20 UTF8(zOut, c);.
ccd20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f 75 74 }. }. *zOut
ccd30 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 = 0;. return (
ccd40 69 6e 74 29 28 7a 4f 75 74 20 2d 20 7a 53 74 61 int)(zOut - zSta
ccd50 72 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 rt);.}.#endif..#
ccd60 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
ccd70 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 43 IT_UTF16./*.** C
ccd80 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 31 36 20 onvert a UTF-16
ccd90 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 6e 61 string in the na
ccda0 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 69 6e tive encoding in
ccdb0 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e to a UTF-8 strin
ccdc0 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f 20 g..** Memory to
ccdd0 68 6f 6c 64 20 74 68 65 20 55 54 46 2d 38 20 73 hold the UTF-8 s
ccde0 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 tring is obtaine
ccdf0 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d d from sqlite3_m
cce00 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 0a 2a alloc and must.*
cce10 2a 20 62 65 20 66 72 65 65 64 20 62 79 20 74 68 * be freed by th
cce20 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
cce30 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c 20 69 on..**.** NULL i
cce40 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 s returned if th
cce50 65 72 65 20 69 73 20 61 6e 20 61 6c 6c 6f 63 61 ere is an alloca
cce60 74 69 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 tion error..*/.S
cce70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
cce80 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 31 36 ar *sqlite3Utf16
cce90 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 64 62 2c to8(sqlite3 *db,
ccea0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 const void *z,
cceb0 69 6e 74 20 6e 42 79 74 65 2c 20 75 38 20 65 6e int nByte, u8 en
ccec0 63 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d c){. Mem m;. m
cced0 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a emset(&m, 0, siz
ccee0 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 eof(m));. m.db
ccef0 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 = db;. sqlite3V
ccf00 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c dbeMemSetStr(&m,
ccf10 20 7a 2c 20 6e 42 79 74 65 2c 20 65 6e 63 2c 20 z, nByte, enc,
ccf20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a SQLITE_STATIC);.
ccf30 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
ccf40 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 ngeEncoding(&m,
ccf50 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 SQLITE_UTF8);.
ccf60 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
ccf70 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 iled ){. sqli
ccf80 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
ccf90 65 28 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d e(&m);. m.z =
ccfa0 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 0;. }. assert
ccfb0 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d ( (m.flags & MEM
ccfc0 5f 54 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d _Term)!=0 || db-
ccfd0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
ccfe0 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c . assert( (m.fl
ccff0 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d ags & MEM_Str)!=
cd000 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 0 || db->mallocF
cd010 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 72 ailed );. asser
cd020 74 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 t( (m.flags & ME
cd030 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 64 62 2d M_Dyn)!=0 || db-
cd040 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
cd050 0a 20 20 61 73 73 65 72 74 28 20 6d 2e 7a 20 7c . assert( m.z |
cd060 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
cd070 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d ed );. return m
cd080 2e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e .z;.}../*.** Con
cd090 76 65 72 74 20 61 20 55 54 46 2d 38 20 73 74 72 vert a UTF-8 str
cd0a0 69 6e 67 20 74 6f 20 74 68 65 20 55 54 46 2d 31 ing to the UTF-1
cd0b0 36 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 6 encoding speci
cd0c0 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 fied by paramete
cd0d0 72 0a 2a 2a 20 65 6e 63 2e 20 41 20 70 6f 69 6e r.** enc. A poin
cd0e0 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 ter to the new s
cd0f0 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65 tring is returne
cd100 64 2c 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 d, and the value
cd110 20 6f 66 20 2a 70 6e 4f 75 74 0a 2a 2a 20 69 73 of *pnOut.** is
cd120 20 73 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67 set to the leng
cd130 74 68 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e th of the return
cd140 65 64 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74 ed string in byt
cd150 65 73 2e 20 54 68 65 20 63 61 6c 6c 20 73 68 6f es. The call sho
cd160 75 6c 64 0a 2a 2a 20 61 72 72 61 6e 67 65 20 74 uld.** arrange t
cd170 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 o call sqlite3Db
cd180 46 72 65 65 28 29 20 6f 6e 20 74 68 65 20 72 65 Free() on the re
cd190 74 75 72 6e 65 64 20 70 6f 69 6e 74 65 72 20 77 turned pointer w
cd1a0 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 20 hen it is.** no
cd1b0 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e longer required.
cd1c0 0a 2a 2a 20 0a 2a 2a 20 49 66 20 61 20 6d 61 6c .** .** If a mal
cd1d0 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75 loc failure occu
cd1e0 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 rs, NULL is retu
cd1f0 72 6e 65 64 20 61 6e 64 20 74 68 65 20 64 62 2e rned and the db.
cd200 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 0a 2a 2a 20 mallocFailed.**
cd210 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 69 66 flag set..*/.#if
cd220 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
cd230 45 5f 53 54 41 54 33 0a 53 51 4c 49 54 45 5f 50 E_STAT3.SQLITE_P
cd240 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
cd250 69 74 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c ite3Utf8to16(sql
cd260 69 74 65 33 20 2a 64 62 2c 20 75 38 20 65 6e 63 ite3 *db, u8 enc
cd270 2c 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e , char *z, int n
cd280 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 , int *pnOut){.
cd290 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 Mem m;. memset
cd2a0 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d (&m, 0, sizeof(m
cd2b0 29 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b ));. m.db = db;
cd2c0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
cd2d0 6d 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e mSetStr(&m, z, n
cd2e0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 , SQLITE_UTF8, S
cd2f0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 QLITE_STATIC);.
cd300 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
cd310 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 26 6d 2c MemTranslate(&m,
cd320 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 61 73 73 enc) ){. ass
cd330 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 ert( db->mallocF
cd340 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 ailed );. ret
cd350 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 urn 0;. }. ass
cd360 65 72 74 28 20 6d 2e 7a 3d 3d 6d 2e 7a 4d 61 6c ert( m.z==m.zMal
cd370 6c 6f 63 20 29 3b 0a 20 20 2a 70 6e 4f 75 74 20 loc );. *pnOut
cd380 3d 20 6d 2e 6e 3b 0a 20 20 72 65 74 75 72 6e 20 = m.n;. return
cd390 6d 2e 7a 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f m.z;.}.#endif../
cd3a0 2a 0a 2a 2a 20 7a 49 6e 20 69 73 20 61 20 55 54 *.** zIn is a UT
cd3b0 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69 F-16 encoded uni
cd3c0 63 6f 64 65 20 73 74 72 69 6e 67 20 61 74 20 6c code string at l
cd3d0 65 61 73 74 20 6e 43 68 61 72 20 63 68 61 72 61 east nChar chara
cd3e0 63 74 65 72 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 cters long..** R
cd3f0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
cd400 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
cd410 20 66 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69 first nChar uni
cd420 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 0a code characters.
cd430 2a 2a 20 69 6e 20 70 5a 2e 20 20 6e 43 68 61 72 ** in pZ. nChar
cd440 20 6d 75 73 74 20 62 65 20 6e 6f 6e 2d 6e 65 67 must be non-neg
cd450 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ative..*/.SQLITE
cd460 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
cd470 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e ite3Utf16ByteLen
cd480 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e (const void *zIn
cd490 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20 , int nChar){.
cd4a0 69 6e 74 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65 int c;. unsigne
cd4b0 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20 d char const *z
cd4c0 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d = zIn;. int n =
cd4d0 20 30 3b 0a 20 20 0a 20 20 69 66 28 20 53 51 4c 0;. . if( SQL
cd4e0 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d ITE_UTF16NATIVE=
cd4f0 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 =SQLITE_UTF16BE
cd500 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 3c ){. while( n<
cd510 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 52 nChar ){. R
cd520 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 31 EAD_UTF16BE(z, 1
cd530 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b , c);. n++;
cd540 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
cd550 20 20 20 20 77 68 69 6c 65 28 20 6e 3c 6e 43 68 while( n<nCh
cd560 61 72 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 ar ){. READ
cd570 5f 55 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63 _UTF16LE(z, 1, c
cd580 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 );. n++;.
cd590 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
cd5a0 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67 6e (int)(z-(unsign
cd5b0 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 29 ed char const *)
cd5c0 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 zIn);.}..#if def
cd5d0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
cd5e0 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 )./*.** This rou
cd5f0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 tine is called f
cd600 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 74 rom the TCL test
cd610 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e 73 function "trans
cd620 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e 0a late_selftest"..
cd630 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 61 ** It checks tha
cd640 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 73 t the primitives
cd650 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 for serializing
cd660 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a 69 and deserializi
cd670 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 ng.** characters
cd680 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 6e in each encodin
cd690 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 6f g are inverses o
cd6a0 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f f each other..*/
cd6b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
cd6c0 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 53 void sqlite3UtfS
cd6d0 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a 20 elfTest(void){.
cd6e0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c unsigned int i,
cd6f0 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 t;. unsigned c
cd700 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 20 har zBuf[20];.
cd710 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
cd720 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 ;. int n;. uns
cd730 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20 igned int c;..
cd740 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 for(i=0; i<0x001
cd750 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 10000; i++){.
cd760 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 z = zBuf;. W
cd770 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b RITE_UTF8(z, i);
cd780 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a . n = (int)(z
cd790 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 -zBuf);. asse
cd7a0 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 rt( n>0 && n<=4
cd7b0 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b );. z[0] = 0;
cd7c0 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 . z = zBuf;.
cd7d0 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 c = sqlite3Ut
cd7e0 66 38 52 65 61 64 28 28 63 6f 6e 73 74 20 75 38 f8Read((const u8
cd7f0 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 20 3d 20 **)&z);. t =
cd800 69 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 i;. if( i>=0x
cd810 44 38 30 30 20 26 26 20 69 3c 3d 30 78 44 46 46 D800 && i<=0xDFF
cd820 46 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a F ) t = 0xFFFD;.
cd830 20 20 20 20 69 66 28 20 28 69 26 30 78 46 46 46 if( (i&0xFFF
cd840 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29 FFFFE)==0xFFFE )
cd850 20 74 20 3d 20 30 78 46 46 46 44 3b 0a 20 20 20 t = 0xFFFD;.
cd860 20 61 73 73 65 72 74 28 20 63 3d 3d 74 20 29 3b assert( c==t );
cd870 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d . assert( (z-
cd880 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a zBuf)==n );. }.
cd890 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 for(i=0; i<0x0
cd8a0 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 0110000; i++){.
cd8b0 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 if( i>=0xD800
cd8c0 20 26 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 && i<0xE000 ) c
cd8d0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d ontinue;. z =
cd8e0 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 zBuf;. WRITE
cd8f0 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 29 3b 0a _UTF16LE(z, i);.
cd900 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d n = (int)(z-
cd910 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72 zBuf);. asser
cd920 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 t( n>0 && n<=4 )
cd930 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a ;. z[0] = 0;.
cd940 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 z = zBuf;.
cd950 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a READ_UTF16LE(z
cd960 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 , 1, c);. ass
cd970 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 ert( c==i );.
cd980 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 assert( (z-zBuf
cd990 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 6f )==n );. }. fo
cd9a0 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 r(i=0; i<0x00110
cd9b0 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 000; i++){. i
cd9c0 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20 f( i>=0xD800 &&
cd9d0 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e 74 69 i<0xE000 ) conti
cd9e0 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 nue;. z = zBu
cd9f0 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46 f;. WRITE_UTF
cda00 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20 20 20 16BE(z, i);.
cda10 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66 n = (int)(z-zBuf
cda20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e );. assert( n
cda30 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 >0 && n<=4 );.
cda40 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 z[0] = 0;.
cda50 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 45 z = zBuf;. RE
cda60 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 31 2c AD_UTF16BE(z, 1,
cda70 20 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 c);. assert(
cda80 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 c==i );. ass
cda90 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e ert( (z-zBuf)==n
cdaa0 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 );. }.}.#endif
cdab0 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 /* SQLITE_TEST
cdac0 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c */.#endif /* SQL
cdad0 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a ITE_OMIT_UTF16 *
cdae0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
cdaf0 2a 2a 20 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 ** End of utf.c
cdb00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdb30 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
cdb40 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74 ** Begin file ut
cdb50 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a il.c ***********
cdb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdb80 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 */./*.** 2001 Se
cdb90 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a ptember 15.**.**
cdba0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
cdbb0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
cdbc0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
cdbd0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
cdbe0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
cdbf0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
cdc00 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
cdc10 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
cdc20 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
cdc30 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
cdc40 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
cdc50 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
cdc60 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
cdc70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
cdc80 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
cdc90 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
cdca0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
cdcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cdcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 *********.** Uti
cdd00 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 75 lity functions u
cdd10 73 65 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73 sed throughout s
cdd20 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 qlite..**.** Thi
cdd30 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
cdd40 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c functions for al
cdd50 6c 6f 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c locating memory,
cdd60 20 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74 comparing.** st
cdd70 72 69 6e 67 73 2c 20 61 6e 64 20 73 74 75 66 66 rings, and stuff
cdd80 20 6c 69 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a like that..**.*
cdd90 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 /./* #include <s
cdda0 74 64 61 72 67 2e 68 3e 20 2a 2f 0a 23 69 66 64 tdarg.h> */.#ifd
cddb0 65 66 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 ef SQLITE_HAVE_I
cddc0 53 4e 41 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c SNAN.# include <
cddd0 6d 61 74 68 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a math.h>.#endif..
cdde0 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 6e 65 /*.** Routine ne
cddf0 65 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 eded to support
cde00 74 68 65 20 74 65 73 74 63 61 73 65 28 29 20 6d the testcase() m
cde10 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 acro..*/.#ifdef
cde20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f SQLITE_COVERAGE_
cde30 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 TEST.SQLITE_PRIV
cde40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
cde50 43 6f 76 65 72 61 67 65 28 69 6e 74 20 78 29 7b Coverage(int x){
cde60 0a 20 20 73 74 61 74 69 63 20 75 6e 73 69 67 6e . static unsign
cde70 65 64 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20 ed dummy = 0;.
cde80 64 75 6d 6d 79 20 2b 3d 20 28 75 6e 73 69 67 6e dummy += (unsign
cde90 65 64 29 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a ed)x;.}.#endif..
cdea0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
cdeb0 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
cdec0 4e 54 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 NT./*.** Return
cded0 74 72 75 65 20 69 66 20 74 68 65 20 66 6c 6f 61 true if the floa
cdee0 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
cdef0 20 69 73 20 4e 6f 74 20 61 20 4e 75 6d 62 65 72 is Not a Number
cdf00 20 28 4e 61 4e 29 2e 0a 2a 2a 0a 2a 2a 20 55 73 (NaN)..**.** Us
cdf10 65 20 74 68 65 20 6d 61 74 68 20 6c 69 62 72 61 e the math libra
cdf20 72 79 20 69 73 6e 61 6e 28 29 20 66 75 6e 63 74 ry isnan() funct
cdf30 69 6f 6e 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 ion if compiled
cdf40 77 69 74 68 20 53 51 4c 49 54 45 5f 48 41 56 45 with SQLITE_HAVE
cdf50 5f 49 53 4e 41 4e 2e 0a 2a 2a 20 4f 74 68 65 72 _ISNAN..** Other
cdf60 77 69 73 65 2c 20 77 65 20 68 61 76 65 20 6f 75 wise, we have ou
cdf70 72 20 6f 77 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 r own implementa
cdf80 74 69 6f 6e 20 74 68 61 74 20 77 6f 72 6b 73 20 tion that works
cdf90 6f 6e 20 6d 6f 73 74 20 73 79 73 74 65 6d 73 2e on most systems.
cdfa0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
cdfb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 TE int sqlite3Is
cdfc0 4e 61 4e 28 64 6f 75 62 6c 65 20 78 29 7b 0a 20 NaN(double x){.
cdfd0 20 69 6e 74 20 72 63 3b 20 20 20 2f 2a 20 54 68 int rc; /* Th
cdfe0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 20 2a e value return *
cdff0 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 /.#if !defined(S
ce000 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e QLITE_HAVE_ISNAN
ce010 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 79 73 74 ). /*. ** Syst
ce020 65 6d 73 20 74 68 61 74 20 73 75 70 70 6f 72 74 ems that support
ce030 20 74 68 65 20 69 73 6e 61 6e 28 29 20 6c 69 62 the isnan() lib
ce040 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 73 68 rary function sh
ce050 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 0a 20 20 ould probably.
ce060 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 69 ** make use of i
ce070 74 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 t by compiling w
ce080 69 74 68 20 2d 44 53 51 4c 49 54 45 5f 48 41 56 ith -DSQLITE_HAV
ce090 45 5f 49 53 4e 41 4e 2e 20 20 42 75 74 20 77 65 E_ISNAN. But we
ce0a0 20 68 61 76 65 0a 20 20 2a 2a 20 66 6f 75 6e 64 have. ** found
ce0b0 20 74 68 61 74 20 6d 61 6e 79 20 73 79 73 74 65 that many syste
ce0c0 6d 73 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 61 ms do not have a
ce0d0 20 77 6f 72 6b 69 6e 67 20 69 73 6e 61 6e 28 29 working isnan()
ce0e0 20 66 75 6e 63 74 69 6f 6e 20 73 6f 0a 20 20 2a function so. *
ce0f0 2a 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 * this implement
ce100 61 74 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 ation is provide
ce110 64 20 61 73 20 61 6e 20 61 6c 74 65 72 6e 61 74 d as an alternat
ce120 69 76 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 ive.. **. ** T
ce130 68 69 73 20 4e 61 4e 20 74 65 73 74 20 73 6f 6d his NaN test som
ce140 65 74 69 6d 65 73 20 66 61 69 6c 73 20 69 66 20 etimes fails if
ce150 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 47 43 43 20 compiled on GCC
ce160 77 69 74 68 20 2d 66 66 61 73 74 2d 6d 61 74 68 with -ffast-math
ce170 2e 0a 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 .. ** On the ot
ce180 68 65 72 20 68 61 6e 64 2c 20 74 68 65 20 75 73 her hand, the us
ce190 65 20 6f 66 20 2d 66 66 61 73 74 2d 6d 61 74 68 e of -ffast-math
ce1a0 20 63 6f 6d 65 73 20 77 69 74 68 20 74 68 65 20 comes with the
ce1b0 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 77 following. ** w
ce1c0 61 72 6e 69 6e 67 3a 0a 20 20 2a 2a 0a 20 20 2a arning:. **. *
ce1d0 2a 20 20 20 20 20 20 54 68 69 73 20 6f 70 74 69 * This opti
ce1e0 6f 6e 20 5b 2d 66 66 61 73 74 2d 6d 61 74 68 5d on [-ffast-math]
ce1f0 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 should never be
ce200 20 74 75 72 6e 65 64 20 6f 6e 20 62 79 20 61 6e turned on by an
ce210 79 0a 20 20 2a 2a 20 20 20 20 20 20 2d 4f 20 6f y. ** -O o
ce220 70 74 69 6f 6e 20 73 69 6e 63 65 20 69 74 20 63 ption since it c
ce230 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 69 6e 63 an result in inc
ce240 6f 72 72 65 63 74 20 6f 75 74 70 75 74 20 66 6f orrect output fo
ce250 72 20 70 72 6f 67 72 61 6d 73 0a 20 20 2a 2a 20 r programs. **
ce260 20 20 20 20 20 77 68 69 63 68 20 64 65 70 65 6e which depen
ce270 64 20 6f 6e 20 61 6e 20 65 78 61 63 74 20 69 6d d on an exact im
ce280 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
ce290 49 45 45 45 20 6f 72 20 49 53 4f 20 0a 20 20 2a IEEE or ISO . *
ce2a0 2a 20 20 20 20 20 20 72 75 6c 65 73 2f 73 70 65 * rules/spe
ce2b0 63 69 66 69 63 61 74 69 6f 6e 73 20 66 6f 72 20 cifications for
ce2c0 6d 61 74 68 20 66 75 6e 63 74 69 6f 6e 73 2e 0a math functions..
ce2d0 20 20 2a 2a 0a 20 20 2a 2a 20 55 6e 64 65 72 20 **. ** Under
ce2e0 4d 53 56 43 2c 20 74 68 69 73 20 4e 61 4e 20 74 MSVC, this NaN t
ce2f0 65 73 74 20 6d 61 79 20 66 61 69 6c 20 69 66 20 est may fail if
ce300 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 61 20 compiled with a
ce310 66 6c 6f 61 74 69 6e 67 2d 0a 20 20 2a 2a 20 70 floating-. ** p
ce320 6f 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 20 6d oint precision m
ce330 6f 64 65 20 6f 74 68 65 72 20 74 68 61 6e 20 2f ode other than /
ce340 66 70 3a 70 72 65 63 69 73 65 2e 20 20 46 72 6f fp:precise. Fro
ce350 6d 20 74 68 65 20 4d 53 44 4e 20 0a 20 20 2a 2a m the MSDN . **
ce360 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 3a 0a documentation:.
ce370 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 **. ** T
ce380 68 65 20 63 6f 6d 70 69 6c 65 72 20 5b 77 69 74 he compiler [wit
ce390 68 20 2f 66 70 3a 70 72 65 63 69 73 65 5d 20 77 h /fp:precise] w
ce3a0 69 6c 6c 20 70 72 6f 70 65 72 6c 79 20 68 61 6e ill properly han
ce3b0 64 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20 dle comparisons
ce3c0 0a 20 20 2a 2a 20 20 20 20 20 20 69 6e 76 6f 6c . ** invol
ce3d0 76 69 6e 67 20 4e 61 4e 2e 20 46 6f 72 20 65 78 ving NaN. For ex
ce3e0 61 6d 70 6c 65 2c 20 78 20 21 3d 20 78 20 65 76 ample, x != x ev
ce3f0 61 6c 75 61 74 65 73 20 74 6f 20 74 72 75 65 20 aluates to true
ce400 69 66 20 78 20 69 73 20 4e 61 4e 20 0a 20 20 2a if x is NaN . *
ce410 2a 20 20 20 20 20 20 2e 2e 2e 0a 20 20 2a 2f 0a * .... */.
ce420 23 69 66 64 65 66 20 5f 5f 46 41 53 54 5f 4d 41 #ifdef __FAST_MA
ce430 54 48 5f 5f 0a 23 20 65 72 72 6f 72 20 53 51 4c TH__.# error SQL
ce440 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 ite will not wor
ce450 6b 20 63 6f 72 72 65 63 74 6c 79 20 77 69 74 68 k correctly with
ce460 20 74 68 65 20 2d 66 66 61 73 74 2d 6d 61 74 68 the -ffast-math
ce470 20 6f 70 74 69 6f 6e 20 6f 66 20 47 43 43 2e 0a option of GCC..
ce480 23 65 6e 64 69 66 0a 20 20 76 6f 6c 61 74 69 6c #endif. volatil
ce490 65 20 64 6f 75 62 6c 65 20 79 20 3d 20 78 3b 0a e double y = x;.
ce4a0 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c volatile doubl
ce4b0 65 20 7a 20 3d 20 79 3b 0a 20 20 72 63 20 3d 20 e z = y;. rc =
ce4c0 28 79 21 3d 7a 29 3b 0a 23 65 6c 73 65 20 20 2f (y!=z);.#else /
ce4d0 2a 20 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c * if defined(SQL
ce4e0 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 29 20 ITE_HAVE_ISNAN)
ce4f0 2a 2f 0a 20 20 72 63 20 3d 20 69 73 6e 61 6e 28 */. rc = isnan(
ce500 78 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 x);.#endif /* SQ
ce510 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 20 LITE_HAVE_ISNAN
ce520 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 20 72 */. testcase( r
ce530 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 c );. return rc
ce540 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
ce550 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
ce560 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a NG_POINT */../*.
ce570 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 ** Compute a str
ce580 69 6e 67 20 6c 65 6e 67 74 68 20 74 68 61 74 20 ing length that
ce590 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 77 68 is limited to wh
ce5a0 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 at can be stored
ce5b0 20 69 6e 0a 2a 2a 20 6c 6f 77 65 72 20 33 30 20 in.** lower 30
ce5c0 62 69 74 73 20 6f 66 20 61 20 33 32 2d 62 69 74 bits of a 32-bit
ce5d0 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e signed integer.
ce5e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 .**.** The value
ce5f0 20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e returned will n
ce600 65 76 65 72 20 62 65 20 6e 65 67 61 74 69 76 65 ever be negative
ce610 2e 20 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 65 . Nor will it e
ce620 76 65 72 20 62 65 20 67 72 65 61 74 65 72 0a 2a ver be greater.*
ce630 2a 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 * than the actua
ce640 6c 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 l length of the
ce650 73 74 72 69 6e 67 2e 20 20 46 6f 72 20 76 65 72 string. For ver
ce660 79 20 6c 6f 6e 67 20 73 74 72 69 6e 67 73 20 28 y long strings (
ce670 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 greater.** than
ce680 31 47 69 42 29 20 74 68 65 20 76 61 6c 75 65 20 1GiB) the value
ce690 72 65 74 75 72 6e 65 64 20 6d 69 67 68 74 20 62 returned might b
ce6a0 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 e less than the
ce6b0 74 72 75 65 20 73 74 72 69 6e 67 20 6c 65 6e 67 true string leng
ce6c0 74 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 th..*/.SQLITE_PR
ce6d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
ce6e0 33 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74 20 3Strlen30(const
ce6f0 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 6f 6e 73 char *z){. cons
ce700 74 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a t char *z2 = z;.
ce710 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 if( z==0 ) ret
ce720 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 urn 0;. while(
ce730 2a 7a 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 *z2 ){ z2++; }.
ce740 20 72 65 74 75 72 6e 20 30 78 33 66 66 66 66 66 return 0x3fffff
ce750 66 66 20 26 20 28 69 6e 74 29 28 7a 32 20 2d 20 ff & (int)(z2 -
ce760 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 z);.}../*.** Set
ce770 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
ce780 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 error code and
ce790 65 72 72 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 error string for
ce7a0 20 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 the sqlite.** h
ce7b0 61 6e 64 6c 65 20 22 64 62 22 2e 20 54 68 65 20 andle "db". The
ce7c0 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65 error code is se
ce7d0 74 20 74 6f 20 22 65 72 72 5f 63 6f 64 65 22 2e t to "err_code".
ce7e0 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 .**.** If it is
ce7f0 6e 6f 74 20 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 not NULL, string
ce800 20 7a 46 6f 72 6d 61 74 20 73 70 65 63 69 66 69 zFormat specifi
ce810 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 es the format of
ce820 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 the.** error st
ce830 72 69 6e 67 20 69 6e 20 74 68 65 20 73 74 79 6c ring in the styl
ce840 65 20 6f 66 20 74 68 65 20 70 72 69 6e 74 66 20 e of the printf
ce850 66 75 6e 63 74 69 6f 6e 73 3a 20 54 68 65 20 66 functions: The f
ce860 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d ollowing.** form
ce870 61 74 20 63 68 61 72 61 63 74 65 72 73 20 61 72 at characters ar
ce880 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a e allowed:.**.**
ce890 20 20 20 20 20 20 25 73 20 20 20 20 20 20 49 6e %s In
ce8a0 73 65 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a sert a string.**
ce8b0 20 20 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 %z A
ce8c0 73 74 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 string that shou
ce8d0 6c 64 20 62 65 20 66 72 65 65 64 20 61 66 74 65 ld be freed afte
ce8e0 72 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 r use.** %d
ce8f0 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 Insert an
ce900 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 integer.**
ce910 25 54 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 %T Insert a
ce920 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 token.** %
ce930 53 20 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 S Insert th
ce940 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 e first element
ce950 6f 66 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a of a SrcList.**.
ce960 2a 2a 20 7a 46 6f 72 6d 61 74 20 61 6e 64 20 61 ** zFormat and a
ce970 6e 79 20 73 74 72 69 6e 67 20 74 6f 6b 65 6e 73 ny string tokens
ce980 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 69 74 20 that follow it
ce990 61 72 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62 are assumed to b
ce9a0 65 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 e.** encoded in
ce9b0 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 UTF-8..**.** To
ce9c0 63 6c 65 61 72 20 74 68 65 20 6d 6f 73 74 20 72 clear the most r
ce9d0 65 63 65 6e 74 20 65 72 72 6f 72 20 66 6f 72 20 ecent error for
ce9e0 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 20 22 64 sqlite handle "d
ce9f0 62 22 2c 20 73 71 6c 69 74 65 33 45 72 72 6f 72 b", sqlite3Error
cea00 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 61 .** should be ca
cea10 6c 6c 65 64 20 77 69 74 68 20 65 72 72 5f 63 6f lled with err_co
cea20 64 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 de set to SQLITE
cea30 5f 4f 4b 20 61 6e 64 20 7a 46 6f 72 6d 61 74 20 _OK and zFormat
cea40 73 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a set.** to NULL..
cea50 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
cea60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 E void sqlite3Er
cea70 72 6f 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ror(sqlite3 *db,
cea80 20 69 6e 74 20 65 72 72 5f 63 6f 64 65 2c 20 63 int err_code, c
cea90 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
ceaa0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 at, ...){. if(
ceab0 64 62 20 26 26 20 28 64 62 2d 3e 70 45 72 72 20 db && (db->pErr
ceac0 7c 7c 20 28 64 62 2d 3e 70 45 72 72 20 3d 20 73 || (db->pErr = s
cead0 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 qlite3ValueNew(d
ceae0 62 29 29 21 3d 30 29 20 29 7b 0a 20 20 20 20 64 b))!=0) ){. d
ceaf0 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 65 72 72 b->errCode = err
ceb00 5f 63 6f 64 65 3b 0a 20 20 20 20 69 66 28 20 7a _code;. if( z
ceb10 46 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 Format ){.
ceb20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 76 char *z;. v
ceb30 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 20 20 20 a_list ap;.
ceb40 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 va_start(ap, zF
ceb50 6f 72 6d 61 74 29 3b 0a 20 20 20 20 20 20 7a 20 ormat);. z
ceb60 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 = sqlite3VMPrint
ceb70 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 f(db, zFormat, a
ceb80 70 29 3b 0a 20 20 20 20 20 20 76 61 5f 65 6e 64 p);. va_end
ceb90 28 61 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 (ap);. sqli
ceba0 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 te3ValueSetStr(d
cebb0 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 b->pErr, -1, z,
cebc0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c SQLITE_UTF8, SQL
cebd0 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 ITE_DYNAMIC);.
cebe0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 }else{. s
cebf0 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 qlite3ValueSetSt
cec00 72 28 64 62 2d 3e 70 45 72 72 2c 20 30 2c 20 30 r(db->pErr, 0, 0
cec10 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 , SQLITE_UTF8, S
cec20 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 QLITE_STATIC);.
cec30 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
cec40 2a 20 41 64 64 20 61 6e 20 65 72 72 6f 72 20 6d * Add an error m
cec50 65 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 essage to pParse
cec60 2d 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 69 6e ->zErrMsg and in
cec70 63 72 65 6d 65 6e 74 20 70 50 61 72 73 65 2d 3e crement pParse->
cec80 6e 45 72 72 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c nErr..** The fol
cec90 6c 6f 77 69 6e 67 20 66 6f 72 6d 61 74 74 69 6e lowing formattin
ceca0 67 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 g characters are
cecb0 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 allowed:.**.**
cecc0 20 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73 %s Ins
cecd0 65 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 ert a string.**
cece0 20 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73 %z A s
cecf0 74 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c tring that shoul
ced00 64 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72 d be freed after
ced10 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 use.** %d
ced20 20 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69 Insert an i
ced30 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 nteger.** %
ced40 54 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 T Insert a
ced50 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 token.** %S
ced60 20 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65 Insert the
ced70 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f first element o
ced80 66 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a f a SrcList.**.*
ced90 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
ceda0 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 74 should be used t
cedb0 6f 20 72 65 70 6f 72 74 20 61 6e 79 20 65 72 72 o report any err
cedc0 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 77 or that occurs w
cedd0 68 69 6c 73 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69 hilst.** compili
cede0 6e 67 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d ng an SQL statem
cedf0 65 6e 74 20 28 69 2e 65 2e 20 77 69 74 68 69 6e ent (i.e. within
cee00 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
cee10 28 29 29 2e 20 54 68 65 0a 2a 2a 20 6c 61 73 74 ()). The.** last
cee20 20 74 68 69 6e 67 20 74 68 65 20 73 71 6c 69 74 thing the sqlit
cee30 65 33 5f 70 72 65 70 61 72 65 28 29 20 66 75 6e e3_prepare() fun
cee40 63 74 69 6f 6e 20 64 6f 65 73 20 69 73 20 63 6f ction does is co
cee50 70 79 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 py the error.**
cee60 73 74 6f 72 65 64 20 62 79 20 74 68 69 73 20 66 stored by this f
cee70 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 unction into the
cee80 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
cee90 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 45 72 using sqlite3Er
ceea0 72 6f 72 28 29 2e 0a 2a 2a 20 46 75 6e 63 74 69 ror()..** Functi
ceeb0 6f 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 on sqlite3Error(
ceec0 29 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 ) should be used
ceed0 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e during statemen
ceee0 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 28 t execution.** (
ceef0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 65 sqlite3_step() e
cef00 74 63 2e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tc.)..*/.SQLITE_
cef10 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
cef20 69 74 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72 ite3ErrorMsg(Par
cef30 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 se *pParse, cons
cef40 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
cef50 20 2e 2e 2e 29 7b 0a 20 20 63 68 61 72 20 2a 7a ...){. char *z
cef60 4d 73 67 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61 Msg;. va_list a
cef70 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 p;. sqlite3 *db
cef80 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
cef90 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 va_start(ap, zF
cefa0 6f 72 6d 61 74 29 3b 0a 20 20 7a 4d 73 67 20 3d ormat);. zMsg =
cefb0 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 sqlite3VMPrintf
cefc0 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 (db, zFormat, ap
cefd0 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b );. va_end(ap);
cefe0 0a 20 20 69 66 28 20 64 62 2d 3e 73 75 70 70 72 . if( db->suppr
ceff0 65 73 73 45 72 72 20 29 7b 0a 20 20 20 20 73 71 essErr ){. sq
cf000 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
cf010 7a 4d 73 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a zMsg);. }else{.
cf020 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 pParse->nErr
cf030 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 ++;. sqlite3D
cf040 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 bFree(db, pParse
cf050 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 ->zErrMsg);.
cf060 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 pParse->zErrMsg
cf070 3d 20 7a 4d 73 67 3b 0a 20 20 20 20 70 50 61 72 = zMsg;. pPar
cf080 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f se->rc = SQLITE_
cf090 45 52 52 4f 52 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ERROR;. }.}../*
cf0a0 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 53 .** Convert an S
cf0b0 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74 65 64 20 QL-style quoted
cf0c0 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e 6f string into a no
cf0d0 72 6d 61 6c 20 73 74 72 69 6e 67 20 62 79 20 72 rmal string by r
cf0e0 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 20 71 emoving.** the q
cf0f0 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 73 2e uote characters.
cf100 20 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e The conversion
cf110 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 is done in-plac
cf120 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 69 6e e. If the.** in
cf130 70 75 74 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 put does not beg
cf140 69 6e 20 77 69 74 68 20 61 20 71 75 6f 74 65 20 in with a quote
cf150 63 68 61 72 61 63 74 65 72 2c 20 74 68 65 6e 20 character, then
cf160 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
cf170 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
cf180 2a 20 54 68 65 20 69 6e 70 75 74 20 73 74 72 69 * The input stri
cf190 6e 67 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d ng must be zero-
cf1a0 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 41 20 6e terminated. A n
cf1b0 65 77 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 ew zero-terminat
cf1c0 6f 72 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 or.** is added t
cf1d0 6f 20 74 68 65 20 64 65 71 75 6f 74 65 64 20 73 o the dequoted s
cf1e0 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tring..**.** The
cf1f0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 return value is
cf200 20 2d 31 20 69 66 20 6e 6f 20 64 65 71 75 6f 74 -1 if no dequot
cf210 69 6e 67 20 6f 63 63 75 72 73 20 6f 72 20 74 68 ing occurs or th
cf220 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 0a e length of the.
cf230 2a 2a 20 64 65 71 75 6f 74 65 64 20 73 74 72 69 ** dequoted stri
cf240 6e 67 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66 ng, exclusive of
cf250 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e the zero termin
cf260 61 74 6f 72 2c 20 69 66 20 64 65 71 75 6f 74 69 ator, if dequoti
cf270 6e 67 20 64 6f 65 73 0a 2a 2a 20 6f 63 63 75 72 ng does.** occur
cf280 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 32 2d 46 65 62 ..**.** 2002-Feb
cf290 2d 31 34 3a 20 54 68 69 73 20 72 6f 75 74 69 6e -14: This routin
cf2a0 65 20 69 73 20 65 78 74 65 6e 64 65 64 20 74 6f e is extended to
cf2b0 20 72 65 6d 6f 76 65 20 4d 53 2d 41 63 63 65 73 remove MS-Acces
cf2c0 73 20 73 74 79 6c 65 0a 2a 2a 20 62 72 61 63 6b s style.** brack
cf2d0 65 74 73 20 66 72 6f 6d 20 61 72 6f 75 6e 64 20 ets from around
cf2e0 69 64 65 6e 74 69 66 65 72 73 2e 20 20 46 6f 72 identifers. For
cf2f0 20 65 78 61 6d 70 6c 65 3a 20 20 22 5b 61 2d 62 example: "[a-b
cf300 2d 63 5d 22 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 -c]" becomes.**
cf310 22 61 2d 62 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49 "a-b-c"..*/.SQLI
cf320 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
cf330 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 68 qlite3Dequote(ch
cf340 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 71 ar *z){. char q
cf350 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a uote;. int i, j
cf360 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 ;. if( z==0 ) r
cf370 65 74 75 72 6e 20 2d 31 3b 0a 20 20 71 75 6f 74 eturn -1;. quot
cf380 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 e = z[0];. swit
cf390 63 68 28 20 71 75 6f 74 65 20 29 7b 0a 20 20 20 ch( quote ){.
cf3a0 20 63 61 73 65 20 27 5c 27 27 3a 20 20 62 72 65 case '\'': bre
cf3b0 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 22 27 ak;. case '"'
cf3c0 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 : break;. c
cf3d0 61 73 65 20 27 60 27 3a 20 20 20 62 72 65 61 6b ase '`': break
cf3e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
cf3f0 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 6f /* For MySQL co
cf400 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 mpatibility */.
cf410 20 20 20 63 61 73 65 20 27 5b 27 3a 20 20 20 71 case '[': q
cf420 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 65 uote = ']'; bre
cf430 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 53 ak; /* For MS S
cf440 71 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 74 69 qlServer compati
cf450 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64 65 bility */. de
cf460 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75 72 6e fault: return
cf470 20 2d 31 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 -1;. }. for(i
cf480 3d 31 2c 20 6a 3d 30 3b 20 41 4c 57 41 59 53 28 =1, j=0; ALWAYS(
cf490 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 z[i]); i++){.
cf4a0 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 if( z[i]==quote
cf4b0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b ){. if( z[
cf4c0 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 i+1]==quote ){.
cf4d0 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 z[j++] =
cf4e0 71 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 20 69 quote;. i
cf4f0 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ++;. }else{
cf500 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
cf510 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
cf520 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 e{. z[j++]
cf530 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 = z[i];. }.
cf540 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 }. z[j] = 0;.
cf550 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 20 return j;.}../*
cf560 43 6f 6e 76 65 6e 69 65 6e 74 20 73 68 6f 72 74 Convenient short
cf570 2d 68 61 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 -hand */.#define
cf580 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 20 73 71 UpperToLower sq
cf590 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 lite3UpperToLowe
cf5a0 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 r../*.** Some sy
cf5b0 73 74 65 6d 73 20 68 61 76 65 20 73 74 72 69 63 stems have stric
cf5c0 6d 70 28 29 2e 20 20 4f 74 68 65 72 73 20 68 61 mp(). Others ha
cf5d0 76 65 20 73 74 72 63 61 73 65 63 6d 70 28 29 2e ve strcasecmp().
cf5e0 20 20 42 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 Because.** the
cf5f0 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 69 73 74 re is no consist
cf600 65 6e 63 79 2c 20 77 65 20 77 69 6c 6c 20 64 65 ency, we will de
cf610 66 69 6e 65 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2a fine our own..**
cf620 0a 2a 2a 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49 .** IMPLEMENTATI
cf630 4f 4e 2d 4f 46 3a 20 52 2d 33 30 32 34 33 2d 30 ON-OF: R-30243-0
cf640 32 34 39 34 20 54 68 65 20 73 71 6c 69 74 65 33 2494 The sqlite3
cf650 5f 73 74 72 69 63 6d 70 28 29 20 61 6e 64 0a 2a _stricmp() and.*
cf660 2a 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 * sqlite3_strnic
cf670 6d 70 28 29 20 41 50 49 73 20 61 6c 6c 6f 77 20 mp() APIs allow
cf680 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 applications and
cf690 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 63 extensions to c
cf6a0 6f 6d 70 61 72 65 0a 2a 2a 20 74 68 65 20 63 6f ompare.** the co
cf6b0 6e 74 65 6e 74 73 20 6f 66 20 74 77 6f 20 62 75 ntents of two bu
cf6c0 66 66 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 ffers containing
cf6d0 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 69 UTF-8 strings i
cf6e0 6e 20 61 0a 2a 2a 20 63 61 73 65 2d 69 6e 64 65 n a.** case-inde
cf6f0 70 65 6e 64 65 6e 74 20 66 61 73 68 69 6f 6e 2c pendent fashion,
cf700 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 using the same
cf710 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 22 63 definition of "c
cf720 61 73 65 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 ase.** independe
cf730 6e 63 65 22 20 74 68 61 74 20 53 51 4c 69 74 65 nce" that SQLite
cf740 20 75 73 65 73 20 69 6e 74 65 72 6e 61 6c 6c 79 uses internally
cf750 20 77 68 65 6e 20 63 6f 6d 70 61 72 69 6e 67 20 when comparing
cf760 69 64 65 6e 74 69 66 69 65 72 73 2e 0a 2a 2f 0a identifiers..*/.
cf770 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
cf780 71 6c 69 74 65 33 5f 73 74 72 69 63 6d 70 28 63 qlite3_stricmp(c
cf790 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 onst char *zLeft
cf7a0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 , const char *zR
cf7b0 69 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65 ight){. registe
cf7c0 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 r unsigned char
cf7d0 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 *a, *b;. a = (u
cf7e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a nsigned char *)z
cf7f0 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 Left;. b = (uns
cf800 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 igned char *)zRi
cf810 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61 ght;. while( *a
cf820 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f !=0 && UpperToLo
cf830 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f wer[*a]==UpperTo
cf840 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b Lower[*b]){ a++;
cf850 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e b++; }. return
cf860 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 UpperToLower[*a
cf870 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 ] - UpperToLower
cf880 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 [*b];.}.SQLITE_A
cf890 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 PI int sqlite3_s
cf8a0 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68 trnicmp(const ch
cf8b0 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 ar *zLeft, const
cf8c0 20 63 68 61 72 20 2a 7a 52 69 67 68 74 2c 20 69 char *zRight, i
cf8d0 6e 74 20 4e 29 7b 0a 20 20 72 65 67 69 73 74 65 nt N){. registe
cf8e0 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 r unsigned char
cf8f0 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 *a, *b;. a = (u
cf900 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a nsigned char *)z
cf910 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 Left;. b = (uns
cf920 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 igned char *)zRi
cf930 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d ght;. while( N-
cf940 2d 20 3e 20 30 20 26 26 20 2a 61 21 3d 30 20 26 - > 0 && *a!=0 &
cf950 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a & UpperToLower[*
cf960 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 a]==UpperToLower
cf970 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b [*b]){ a++; b++;
cf980 20 7d 0a 20 20 72 65 74 75 72 6e 20 4e 3c 30 20 }. return N<0
cf990 3f 20 30 20 3a 20 55 70 70 65 72 54 6f 4c 6f 77 ? 0 : UpperToLow
cf9a0 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f er[*a] - UpperTo
cf9b0 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a Lower[*b];.}../*
cf9c0 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a .** The string z
cf9d0 5b 5d 20 69 73 20 61 6e 20 74 65 78 74 20 72 65 [] is an text re
cf9e0 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
cf9f0 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a a real number..*
cfa00 2a 20 43 6f 6e 76 65 72 74 20 74 68 69 73 20 73 * Convert this s
cfa10 74 72 69 6e 67 20 74 6f 20 61 20 64 6f 75 62 6c tring to a doubl
cfa20 65 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 e and write it i
cfa30 6e 74 6f 20 2a 70 52 65 73 75 6c 74 2e 0a 2a 2a nto *pResult..**
cfa40 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a .** The string z
cfa50 5b 5d 20 69 73 20 6c 65 6e 67 74 68 20 62 79 74 [] is length byt
cfa60 65 73 20 69 6e 20 6c 65 6e 67 74 68 20 28 62 79 es in length (by
cfa70 74 65 73 2c 20 6e 6f 74 20 63 68 61 72 61 63 74 tes, not charact
cfa80 65 72 73 29 20 61 6e 64 0a 2a 2a 20 75 73 65 73 ers) and.** uses
cfa90 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 65 6e the encoding en
cfaa0 63 2e 20 20 54 68 65 20 73 74 72 69 6e 67 20 69 c. The string i
cfab0 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c s not necessaril
cfac0 79 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 y zero-terminate
cfad0 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 d..**.** Return
cfae0 54 52 55 45 20 69 66 20 74 68 65 20 72 65 73 75 TRUE if the resu
cfaf0 6c 74 20 69 73 20 61 20 76 61 6c 69 64 20 72 65 lt is a valid re
cfb00 61 6c 20 6e 75 6d 62 65 72 20 28 6f 72 20 69 6e al number (or in
cfb10 74 65 67 65 72 29 20 61 6e 64 20 46 41 4c 53 45 teger) and FALSE
cfb20 0a 2a 2a 20 69 66 20 74 68 65 20 73 74 72 69 6e .** if the strin
cfb30 67 20 69 73 20 65 6d 70 74 79 20 6f 72 20 63 6f g is empty or co
cfb40 6e 74 61 69 6e 73 20 65 78 74 72 61 6e 65 6f 75 ntains extraneou
cfb50 73 20 74 65 78 74 2e 20 20 56 61 6c 69 64 20 6e s text. Valid n
cfb60 75 6d 62 65 72 73 0a 2a 2a 20 61 72 65 20 69 6e umbers.** are in
cfb70 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 66 6f one of these fo
cfb80 72 6d 61 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 rmats:.**.**
cfb90 5b 2b 2d 5d 64 69 67 69 74 73 5b 45 5b 2b 2d 5d [+-]digits[E[+-]
cfba0 64 69 67 69 74 73 5d 0a 2a 2a 20 20 20 20 5b 2b digits].** [+
cfbb0 2d 5d 64 69 67 69 74 73 2e 5b 64 69 67 69 74 73 -]digits.[digits
cfbc0 5d 5b 45 5b 2b 2d 5d 64 69 67 69 74 73 5d 0a 2a ][E[+-]digits].*
cfbd0 2a 20 20 20 20 5b 2b 2d 5d 2e 64 69 67 69 74 73 * [+-].digits
cfbe0 5b 45 5b 2b 2d 5d 64 69 67 69 74 73 5d 0a 2a 2a [E[+-]digits].**
cfbf0 0a 2a 2a 20 4c 65 61 64 69 6e 67 20 61 6e 64 20 .** Leading and
cfc00 74 72 61 69 6c 69 6e 67 20 77 68 69 74 65 73 70 trailing whitesp
cfc10 61 63 65 20 69 73 20 69 67 6e 6f 72 65 64 20 66 ace is ignored f
cfc20 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 20 6f or the purpose o
cfc30 66 20 64 65 74 65 72 6d 69 6e 69 6e 67 0a 2a 2a f determining.**
cfc40 20 76 61 6c 69 64 69 74 79 2e 0a 2a 2a 0a 2a 2a validity..**.**
cfc50 20 49 66 20 73 6f 6d 65 20 70 72 65 66 69 78 20 If some prefix
cfc60 6f 66 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 of the input str
cfc70 69 6e 67 20 69 73 20 61 20 76 61 6c 69 64 20 6e ing is a valid n
cfc80 75 6d 62 65 72 2c 20 74 68 69 73 20 72 6f 75 74 umber, this rout
cfc90 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 46 ine.** returns F
cfca0 41 4c 53 45 20 62 75 74 20 69 74 20 73 74 69 6c ALSE but it stil
cfcb0 6c 20 63 6f 6e 76 65 72 74 73 20 74 68 65 20 70 l converts the p
cfcc0 72 65 66 69 78 20 61 6e 64 20 77 72 69 74 65 73 refix and writes
cfcd0 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 the result.** i
cfce0 6e 74 6f 20 2a 70 52 65 73 75 6c 74 2e 0a 2a 2f nto *pResult..*/
cfcf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
cfd00 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28 int sqlite3AtoF(
cfd10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 const char *z, d
cfd20 6f 75 62 6c 65 20 2a 70 52 65 73 75 6c 74 2c 20 ouble *pResult,
cfd30 69 6e 74 20 6c 65 6e 67 74 68 2c 20 75 38 20 65 int length, u8 e
cfd40 6e 63 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c nc){.#ifndef SQL
cfd50 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
cfd60 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 69 6e G_POINT. int in
cfd70 63 72 20 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 54 cr = (enc==SQLIT
cfd80 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20 63 E_UTF8?1:2);. c
cfd90 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 20 onst char *zEnd
cfda0 3d 20 7a 20 2b 20 6c 65 6e 67 74 68 3b 0a 20 20 = z + length;.
cfdb0 2f 2a 20 73 69 67 6e 20 2a 20 73 69 67 6e 69 66 /* sign * signif
cfdc0 69 63 61 6e 64 20 2a 20 28 31 30 20 5e 20 28 65 icand * (10 ^ (e
cfdd0 73 69 67 6e 20 2a 20 65 78 70 6f 6e 65 6e 74 29 sign * exponent)
cfde0 29 20 2a 2f 0a 20 20 69 6e 74 20 73 69 67 6e 20 ) */. int sign
cfdf0 3d 20 31 3b 20 20 20 20 2f 2a 20 73 69 67 6e 20 = 1; /* sign
cfe00 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a of significand *
cfe10 2f 0a 20 20 69 36 34 20 73 20 3d 20 30 3b 20 20 /. i64 s = 0;
cfe20 20 20 20 20 20 2f 2a 20 73 69 67 6e 69 66 69 63 /* signific
cfe30 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 64 20 3d and */. int d =
cfe40 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 61 64 6a 0; /* adj
cfe50 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 66 6f 72 ust exponent for
cfe60 20 73 68 69 66 74 69 6e 67 20 64 65 63 69 6d 61 shifting decima
cfe70 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 l point */. int
cfe80 20 65 73 69 67 6e 20 3d 20 31 3b 20 20 20 2f 2a esign = 1; /*
cfe90 20 73 69 67 6e 20 6f 66 20 65 78 70 6f 6e 65 6e sign of exponen
cfea0 74 20 2a 2f 0a 20 20 69 6e 74 20 65 20 3d 20 30 t */. int e = 0
cfeb0 3b 20 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e ; /* expon
cfec0 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 65 56 61 ent */. int eVa
cfed0 6c 69 64 20 3d 20 31 3b 20 20 2f 2a 20 54 72 75 lid = 1; /* Tru
cfee0 65 20 65 78 70 6f 6e 65 6e 74 20 69 73 20 65 69 e exponent is ei
cfef0 74 68 65 72 20 6e 6f 74 20 75 73 65 64 20 6f 72 ther not used or
cff00 20 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 is well-formed
cff10 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 65 73 75 */. double resu
cff20 6c 74 3b 0a 20 20 69 6e 74 20 6e 44 69 67 69 74 lt;. int nDigit
cff30 73 20 3d 20 30 3b 0a 0a 20 20 2a 70 52 65 73 75 s = 0;.. *pResu
cff40 6c 74 20 3d 20 30 2e 30 3b 20 20 20 2f 2a 20 44 lt = 0.0; /* D
cff50 65 66 61 75 6c 74 20 72 65 74 75 72 6e 20 76 61 efault return va
cff60 6c 75 65 2c 20 69 6e 20 63 61 73 65 20 6f 66 20 lue, in case of
cff70 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 69 an error */.. i
cff80 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 f( enc==SQLITE_U
cff90 54 46 31 36 42 45 20 29 20 7a 2b 2b 3b 0a 0a 20 TF16BE ) z++;..
cffa0 20 2f 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 /* skip leading
cffb0 20 73 70 61 63 65 73 20 2a 2f 0a 20 20 77 68 69 spaces */. whi
cffc0 6c 65 28 20 7a 3c 7a 45 6e 64 20 26 26 20 73 71 le( z<zEnd && sq
cffd0 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 lite3Isspace(*z)
cffe0 20 29 20 7a 2b 3d 69 6e 63 72 3b 0a 20 20 69 66 ) z+=incr;. if
cfff0 28 20 7a 3e 3d 7a 45 6e 64 20 29 20 72 65 74 75 ( z>=zEnd ) retu
d0000 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 67 65 74 20 rn 0;.. /* get
d0010 73 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 sign of signific
d0020 61 6e 64 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d and */. if( *z=
d0030 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e ='-' ){. sign
d0040 20 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b 3d 69 6e = -1;. z+=in
d0050 63 72 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 cr;. }else if(
d0060 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a *z=='+' ){. z
d0070 2b 3d 69 6e 63 72 3b 0a 20 20 7d 0a 0a 20 20 2f +=incr;. }.. /
d0080 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a * skip leading z
d0090 65 72 6f 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65 eroes */. while
d00a0 28 20 7a 3c 7a 45 6e 64 20 26 26 20 7a 5b 30 5d ( z<zEnd && z[0]
d00b0 3d 3d 27 30 27 20 29 20 7a 2b 3d 69 6e 63 72 2c =='0' ) z+=incr,
d00c0 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 0a 20 20 2f nDigits++;.. /
d00d0 2a 20 63 6f 70 79 20 6d 61 78 20 73 69 67 6e 69 * copy max signi
d00e0 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 74 6f ficant digits to
d00f0 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a significand */.
d0100 20 20 77 68 69 6c 65 28 20 7a 3c 7a 45 6e 64 20 while( z<zEnd
d0110 26 26 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 && sqlite3Isdigi
d0120 74 28 2a 7a 29 20 26 26 20 73 3c 28 28 4c 41 52 t(*z) && s<((LAR
d0130 47 45 53 54 5f 49 4e 54 36 34 2d 39 29 2f 31 30 GEST_INT64-9)/10
d0140 29 20 29 7b 0a 20 20 20 20 73 20 3d 20 73 2a 31 ) ){. s = s*1
d0150 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 0 + (*z - '0');.
d0160 20 20 20 20 7a 2b 3d 69 6e 63 72 2c 20 6e 44 69 z+=incr, nDi
d0170 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f gits++;. }.. /
d0180 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e 69 * skip non-signi
d0190 66 69 63 61 6e 74 20 73 69 67 6e 69 66 69 63 61 ficant significa
d01a0 6e 64 20 64 69 67 69 74 73 0a 20 20 2a 2a 20 28 nd digits. ** (
d01b0 69 6e 63 72 65 61 73 65 20 65 78 70 6f 6e 65 6e increase exponen
d01c0 74 20 62 79 20 64 20 74 6f 20 73 68 69 66 74 20 t by d to shift
d01d0 64 65 63 69 6d 61 6c 20 6c 65 66 74 29 20 2a 2f decimal left) */
d01e0 0a 20 20 77 68 69 6c 65 28 20 7a 3c 7a 45 6e 64 . while( z<zEnd
d01f0 20 26 26 20 73 71 6c 69 74 65 33 49 73 64 69 67 && sqlite3Isdig
d0200 69 74 28 2a 7a 29 20 29 20 7a 2b 3d 69 6e 63 72 it(*z) ) z+=incr
d0210 2c 20 6e 44 69 67 69 74 73 2b 2b 2c 20 64 2b 2b , nDigits++, d++
d0220 3b 0a 20 20 69 66 28 20 7a 3e 3d 7a 45 6e 64 20 ;. if( z>=zEnd
d0230 29 20 67 6f 74 6f 20 64 6f 5f 61 74 6f 66 5f 63 ) goto do_atof_c
d0240 61 6c 63 3b 0a 0a 20 20 2f 2a 20 69 66 20 64 65 alc;.. /* if de
d0250 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 73 20 70 cimal point is p
d0260 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20 resent */. if(
d0270 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a *z=='.' ){. z
d0280 2b 3d 69 6e 63 72 3b 0a 20 20 20 20 2f 2a 20 63 +=incr;. /* c
d0290 6f 70 79 20 64 69 67 69 74 73 20 66 72 6f 6d 20 opy digits from
d02a0 61 66 74 65 72 20 64 65 63 69 6d 61 6c 20 74 6f after decimal to
d02b0 20 73 69 67 6e 69 66 69 63 61 6e 64 0a 20 20 20 significand.
d02c0 20 2a 2a 20 28 64 65 63 72 65 61 73 65 20 65 78 ** (decrease ex
d02d0 70 6f 6e 65 6e 74 20 62 79 20 64 20 74 6f 20 73 ponent by d to s
d02e0 68 69 66 74 20 64 65 63 69 6d 61 6c 20 72 69 67 hift decimal rig
d02f0 68 74 29 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 ht) */. while
d0300 28 20 7a 3c 7a 45 6e 64 20 26 26 20 73 71 6c 69 ( z<zEnd && sqli
d0310 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 te3Isdigit(*z) &
d0320 26 20 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e & s<((LARGEST_IN
d0330 54 36 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20 T64-9)/10) ){.
d0340 20 20 20 20 73 20 3d 20 73 2a 31 30 20 2b 20 28 s = s*10 + (
d0350 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 *z - '0');.
d0360 20 7a 2b 3d 69 6e 63 72 2c 20 6e 44 69 67 69 74 z+=incr, nDigit
d0370 73 2b 2b 2c 20 64 2d 2d 3b 0a 20 20 20 20 7d 0a s++, d--;. }.
d0380 20 20 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d /* skip non-
d0390 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 significant digi
d03a0 74 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 ts */. while(
d03b0 20 7a 3c 7a 45 6e 64 20 26 26 20 73 71 6c 69 74 z<zEnd && sqlit
d03c0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20 e3Isdigit(*z) )
d03d0 7a 2b 3d 69 6e 63 72 2c 20 6e 44 69 67 69 74 73 z+=incr, nDigits
d03e0 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 3e ++;. }. if( z>
d03f0 3d 7a 45 6e 64 20 29 20 67 6f 74 6f 20 64 6f 5f =zEnd ) goto do_
d0400 61 74 6f 66 5f 63 61 6c 63 3b 0a 0a 20 20 2f 2a atof_calc;.. /*
d0410 20 69 66 20 65 78 70 6f 6e 65 6e 74 20 69 73 20 if exponent is
d0420 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28 present */. if(
d0430 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d *z=='e' || *z==
d0440 27 45 27 20 29 7b 0a 20 20 20 20 7a 2b 3d 69 6e 'E' ){. z+=in
d0450 63 72 3b 0a 20 20 20 20 65 56 61 6c 69 64 20 3d cr;. eValid =
d0460 20 30 3b 0a 20 20 20 20 69 66 28 20 7a 3e 3d 7a 0;. if( z>=z
d0470 45 6e 64 20 29 20 67 6f 74 6f 20 64 6f 5f 61 74 End ) goto do_at
d0480 6f 66 5f 63 61 6c 63 3b 0a 20 20 20 20 2f 2a 20 of_calc;. /*
d0490 67 65 74 20 73 69 67 6e 20 6f 66 20 65 78 70 6f get sign of expo
d04a0 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 nent */. if(
d04b0 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 *z=='-' ){.
d04c0 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 esign = -1;.
d04d0 20 20 20 7a 2b 3d 69 6e 63 72 3b 0a 20 20 20 20 z+=incr;.
d04e0 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b }else if( *z=='+
d04f0 27 20 29 7b 0a 20 20 20 20 20 20 7a 2b 3d 69 6e ' ){. z+=in
d0500 63 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a cr;. }. /*
d0510 20 63 6f 70 79 20 64 69 67 69 74 73 20 74 6f 20 copy digits to
d0520 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 exponent */.
d0530 77 68 69 6c 65 28 20 7a 3c 7a 45 6e 64 20 26 26 while( z<zEnd &&
d0540 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 sqlite3Isdigit(
d0550 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 65 20 3d *z) ){. e =
d0560 20 65 3c 31 30 30 30 30 20 3f 20 28 65 2a 31 30 e<10000 ? (e*10
d0570 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 29 20 3a + (*z - '0')) :
d0580 20 31 30 30 30 30 3b 0a 20 20 20 20 20 20 7a 2b 10000;. z+
d0590 3d 69 6e 63 72 3b 0a 20 20 20 20 20 20 65 56 61 =incr;. eVa
d05a0 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 lid = 1;. }.
d05b0 20 7d 0a 0a 20 20 2f 2a 20 73 6b 69 70 20 74 72 }.. /* skip tr
d05c0 61 69 6c 69 6e 67 20 73 70 61 63 65 73 20 2a 2f ailing spaces */
d05d0 0a 20 20 69 66 28 20 6e 44 69 67 69 74 73 20 26 . if( nDigits &
d05e0 26 20 65 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 & eValid ){.
d05f0 77 68 69 6c 65 28 20 7a 3c 7a 45 6e 64 20 26 26 while( z<zEnd &&
d0600 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 sqlite3Isspace(
d0610 2a 7a 29 20 29 20 7a 2b 3d 69 6e 63 72 3b 0a 20 *z) ) z+=incr;.
d0620 20 7d 0a 0a 64 6f 5f 61 74 6f 66 5f 63 61 6c 63 }..do_atof_calc
d0630 3a 0a 20 20 2f 2a 20 61 64 6a 75 73 74 20 65 78 :. /* adjust ex
d0640 70 6f 6e 65 6e 74 20 62 79 20 64 2c 20 61 6e 64 ponent by d, and
d0650 20 75 70 64 61 74 65 20 73 69 67 6e 20 2a 2f 0a update sign */.
d0660 20 20 65 20 3d 20 28 65 2a 65 73 69 67 6e 29 20 e = (e*esign)
d0670 2b 20 64 3b 0a 20 20 69 66 28 20 65 3c 30 20 29 + d;. if( e<0 )
d0680 20 7b 0a 20 20 20 20 65 73 69 67 6e 20 3d 20 2d {. esign = -
d0690 31 3b 0a 20 20 20 20 65 20 2a 3d 20 2d 31 3b 0a 1;. e *= -1;.
d06a0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 65 } else {. e
d06b0 73 69 67 6e 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 sign = 1;. }..
d06c0 20 2f 2a 20 69 66 20 30 20 73 69 67 6e 69 66 69 /* if 0 signifi
d06d0 63 61 6e 64 20 2a 2f 0a 20 20 69 66 28 20 21 73 cand */. if( !s
d06e0 20 29 20 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 ) {. /* In t
d06f0 68 65 20 49 45 45 45 20 37 35 34 20 73 74 61 6e he IEEE 754 stan
d0700 64 61 72 64 2c 20 7a 65 72 6f 20 69 73 20 73 69 dard, zero is si
d0710 67 6e 65 64 2e 0a 20 20 20 20 2a 2a 20 41 64 64 gned.. ** Add
d0720 20 74 68 65 20 73 69 67 6e 20 69 66 20 77 65 27 the sign if we'
d0730 76 65 20 73 65 65 6e 20 61 74 20 6c 65 61 73 74 ve seen at least
d0740 20 6f 6e 65 20 64 69 67 69 74 20 2a 2f 0a 20 20 one digit */.
d0750 20 20 72 65 73 75 6c 74 20 3d 20 28 73 69 67 6e result = (sign
d0760 3c 30 20 26 26 20 6e 44 69 67 69 74 73 29 20 3f <0 && nDigits) ?
d0770 20 2d 28 64 6f 75 62 6c 65 29 30 20 3a 20 28 64 -(double)0 : (d
d0780 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 20 65 6c 73 ouble)0;. } els
d0790 65 20 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65 6d e {. /* attem
d07a0 70 74 20 74 6f 20 72 65 64 75 63 65 20 65 78 70 pt to reduce exp
d07b0 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 onent */. if(
d07c0 20 65 73 69 67 6e 3e 30 20 29 7b 0a 20 20 20 20 esign>0 ){.
d07d0 20 20 77 68 69 6c 65 28 20 73 3c 28 4c 41 52 47 while( s<(LARG
d07e0 45 53 54 5f 49 4e 54 36 34 2f 31 30 29 20 26 26 EST_INT64/10) &&
d07f0 20 65 3e 30 20 29 20 65 2d 2d 2c 73 2a 3d 31 30 e>0 ) e--,s*=10
d0800 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
d0810 20 20 20 77 68 69 6c 65 28 20 21 28 73 25 31 30 while( !(s%10
d0820 29 20 26 26 20 65 3e 30 20 29 20 65 2d 2d 2c 73 ) && e>0 ) e--,s
d0830 2f 3d 31 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 /=10;. }..
d0840 20 2f 2a 20 61 64 6a 75 73 74 20 74 68 65 20 73 /* adjust the s
d0850 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 61 ign of significa
d0860 6e 64 20 2a 2f 0a 20 20 20 20 73 20 3d 20 73 69 nd */. s = si
d0870 67 6e 3c 30 20 3f 20 2d 73 20 3a 20 73 3b 0a 0a gn<0 ? -s : s;..
d0880 20 20 20 20 2f 2a 20 69 66 20 65 78 70 6f 6e 65 /* if expone
d0890 6e 74 2c 20 73 63 61 6c 65 20 73 69 67 6e 69 66 nt, scale signif
d08a0 69 63 61 6e 64 20 61 73 20 61 70 70 72 6f 70 72 icand as appropr
d08b0 69 61 74 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 iate. ** and
d08c0 73 74 6f 72 65 20 69 6e 20 72 65 73 75 6c 74 2e store in result.
d08d0 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 20 29 7b */. if( e ){
d08e0 0a 20 20 20 20 20 20 4c 4f 4e 47 44 4f 55 42 4c . LONGDOUBL
d08f0 45 5f 54 59 50 45 20 73 63 61 6c 65 20 3d 20 31 E_TYPE scale = 1
d0900 2e 30 3b 0a 20 20 20 20 20 20 2f 2a 20 61 74 74 .0;. /* att
d0910 65 6d 70 74 20 74 6f 20 68 61 6e 64 6c 65 20 65 empt to handle e
d0920 78 74 72 65 6d 65 6c 79 20 73 6d 61 6c 6c 2f 6c xtremely small/l
d0930 61 72 67 65 20 6e 75 6d 62 65 72 73 20 62 65 74 arge numbers bet
d0940 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 ter */. if(
d0950 20 65 3e 33 30 37 20 26 26 20 65 3c 33 34 32 20 e>307 && e<342
d0960 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 ){. while
d0970 28 20 65 25 33 30 38 20 29 20 7b 20 73 63 61 6c ( e%308 ) { scal
d0980 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 20 2d e *= 1.0e+1; e -
d0990 3d 20 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 = 1; }. i
d09a0 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 f( esign<0 ){.
d09b0 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d result =
d09c0 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20 s / scale;.
d09d0 20 20 20 20 20 20 72 65 73 75 6c 74 20 2f 3d 20 result /=
d09e0 31 2e 30 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 1.0e+308;.
d09f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
d0a00 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 result = s *
d0a10 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 scale;.
d0a20 20 72 65 73 75 6c 74 20 2a 3d 20 31 2e 30 65 2b result *= 1.0e+
d0a30 33 30 38 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 308;. }.
d0a40 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 }else if( e
d0a50 3e 3d 33 34 32 20 29 7b 0a 20 20 20 20 20 20 20 >=342 ){.
d0a60 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a if( esign<0 ){.
d0a70 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 result
d0a80 20 3d 20 30 2e 30 2a 73 3b 0a 20 20 20 20 20 20 = 0.0*s;.
d0a90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
d0aa0 20 20 20 72 65 73 75 6c 74 20 3d 20 31 65 33 30 result = 1e30
d0ab0 38 2a 31 65 33 30 38 2a 73 3b 20 20 2f 2a 20 49 8*1e308*s; /* I
d0ac0 6e 66 69 6e 69 74 79 20 2a 2f 0a 20 20 20 20 20 nfinity */.
d0ad0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
d0ae0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 31 2e 30 {. /* 1.0
d0af0 65 2b 32 32 20 69 73 20 74 68 65 20 6c 61 72 67 e+22 is the larg
d0b00 65 73 74 20 70 6f 77 65 72 20 6f 66 20 31 30 20 est power of 10
d0b10 74 68 61 6e 20 63 61 6e 20 62 65 20 0a 20 20 20 than can be .
d0b20 20 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 6e ** represen
d0b30 74 65 64 20 65 78 61 63 74 6c 79 2e 20 2a 2f 0a ted exactly. */.
d0b40 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 65 while( e
d0b50 25 32 32 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d %22 ) { scale *=
d0b60 20 31 2e 30 65 2b 31 3b 20 65 20 2d 3d 20 31 3b 1.0e+1; e -= 1;
d0b70 20 7d 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 }. while
d0b80 28 20 65 3e 30 20 29 20 7b 20 73 63 61 6c 65 20 ( e>0 ) { scale
d0b90 2a 3d 20 31 2e 30 65 2b 32 32 3b 20 65 20 2d 3d *= 1.0e+22; e -=
d0ba0 20 32 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 22; }. i
d0bb0 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 f( esign<0 ){.
d0bc0 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d result =
d0bd0 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20 s / scale;.
d0be0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d0bf0 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20 result = s
d0c00 2a 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 * scale;.
d0c10 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
d0c20 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 72 65 else {. re
d0c30 73 75 6c 74 20 3d 20 28 64 6f 75 62 6c 65 29 73 sult = (double)s
d0c40 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
d0c50 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 * store the resu
d0c60 6c 74 20 2a 2f 0a 20 20 2a 70 52 65 73 75 6c 74 lt */. *pResult
d0c70 20 3d 20 72 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a = result;.. /*
d0c80 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 return true if
d0c90 6e 75 6d 62 65 72 20 61 6e 64 20 6e 6f 20 65 78 number and no ex
d0ca0 74 72 61 20 6e 6f 6e 2d 77 68 69 74 65 73 70 61 tra non-whitespa
d0cb0 63 65 20 63 68 72 61 63 74 65 72 73 20 61 66 74 ce chracters aft
d0cc0 65 72 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 7a er */. return z
d0cd0 3e 3d 7a 45 6e 64 20 26 26 20 6e 44 69 67 69 74 >=zEnd && nDigit
d0ce0 73 3e 30 20 26 26 20 65 56 61 6c 69 64 3b 0a 23 s>0 && eValid;.#
d0cf0 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 21 73 else. return !s
d0d00 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 qlite3Atoi64(z,
d0d10 70 52 65 73 75 6c 74 2c 20 6c 65 6e 67 74 68 2c pResult, length,
d0d20 20 65 6e 63 29 3b 0a 23 65 6e 64 69 66 20 2f 2a enc);.#endif /*
d0d30 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
d0d40 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d ATING_POINT */.}
d0d50 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 ../*.** Compare
d0d60 74 68 65 20 31 39 2d 63 68 61 72 61 63 74 65 72 the 19-character
d0d70 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 61 67 61 string zNum aga
d0d80 69 6e 73 74 20 74 68 65 20 74 65 78 74 20 72 65 inst the text re
d0d90 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 presentation.**
d0da0 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 39 32 32 value 2^63: 922
d0db0 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38 3372036854775808
d0dc0 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 61 74 69 . Return negati
d0dd0 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 ve, zero, or pos
d0de0 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d itive.** if zNum
d0df0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 is less than, e
d0e00 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 qual to, or grea
d0e10 74 65 72 20 74 68 61 6e 20 74 68 65 20 73 74 72 ter than the str
d0e20 69 6e 67 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ing..** Note tha
d0e30 74 20 7a 4e 75 6d 20 6d 75 73 74 20 63 6f 6e 74 t zNum must cont
d0e40 61 69 6e 20 65 78 61 63 74 6c 79 20 31 39 20 63 ain exactly 19 c
d0e50 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a haracters..**.**
d0e60 20 55 6e 6c 69 6b 65 20 6d 65 6d 63 6d 70 28 29 Unlike memcmp()
d0e70 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
d0e80 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 72 guaranteed to r
d0e90 65 74 75 72 6e 20 74 68 65 20 64 69 66 66 65 72 eturn the differ
d0ea0 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68 65 20 76 ence.** in the v
d0eb0 61 6c 75 65 73 20 6f 66 20 74 68 65 20 6c 61 73 alues of the las
d0ec0 74 20 64 69 67 69 74 20 69 66 20 74 68 65 20 6f t digit if the o
d0ed0 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 69 nly difference i
d0ee0 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6c 61 73 74 s in the.** last
d0ef0 20 64 69 67 69 74 2e 20 20 53 6f 2c 20 66 6f 72 digit. So, for
d0f00 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 example,.**.**
d0f10 20 20 20 20 20 63 6f 6d 70 61 72 65 32 70 6f 77 compare2pow
d0f20 36 33 28 22 39 32 32 33 33 37 32 30 33 36 38 35 63("922337203685
d0f30 34 37 37 35 38 30 30 22 2c 20 31 29 0a 2a 2a 0a 4775800", 1).**.
d0f40 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 2d ** will return -
d0f50 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 8..*/.static int
d0f60 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 63 compare2pow63(c
d0f70 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c onst char *zNum,
d0f80 20 69 6e 74 20 69 6e 63 72 29 7b 0a 20 20 69 6e int incr){. in
d0f90 74 20 63 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 t c = 0;. int i
d0fa0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
d0fb0 20 20 20 20 20 20 2f 2a 20 30 31 32 33 34 35 36 /* 0123456
d0fc0 37 38 39 30 31 32 33 34 35 36 37 20 2a 2f 0a 20 78901234567 */.
d0fd0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 6f 77 const char *pow
d0fe0 36 33 20 3d 20 22 39 32 32 33 33 37 32 30 33 36 63 = "9223372036
d0ff0 38 35 34 37 37 35 38 30 22 3b 0a 20 20 66 6f 72 85477580";. for
d1000 28 69 3d 30 3b 20 63 3d 3d 30 20 26 26 20 69 3c (i=0; c==0 && i<
d1010 31 38 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 20 18; i++){. c
d1020 3d 20 28 7a 4e 75 6d 5b 69 2a 69 6e 63 72 5d 2d = (zNum[i*incr]-
d1030 70 6f 77 36 33 5b 69 5d 29 2a 31 30 3b 0a 20 20 pow63[i])*10;.
d1040 7d 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a }. if( c==0 ){.
d1050 20 20 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 2a c = zNum[18*
d1060 69 6e 63 72 5d 20 2d 20 27 38 27 3b 0a 20 20 20 incr] - '8';.
d1070 20 74 65 73 74 63 61 73 65 28 20 63 3d 3d 28 2d testcase( c==(-
d1080 31 29 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 1) );. testca
d1090 73 65 28 20 63 3d 3d 30 20 29 3b 0a 20 20 20 20 se( c==0 );.
d10a0 74 65 73 74 63 61 73 65 28 20 63 3d 3d 28 2b 31 testcase( c==(+1
d10b0 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ) );. }. retur
d10c0 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 n c;.}.../*.** C
d10d0 6f 6e 76 65 72 74 20 7a 4e 75 6d 20 74 6f 20 61 onvert zNum to a
d10e0 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 64-bit signed i
d10f0 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 nteger..**.** If
d1100 20 74 68 65 20 7a 4e 75 6d 20 76 61 6c 75 65 20 the zNum value
d1110 69 73 20 72 65 70 72 65 73 65 6e 74 61 62 6c 65 is representable
d1120 20 61 73 20 61 20 36 34 2d 62 69 74 20 74 77 6f as a 64-bit two
d1130 73 2d 63 6f 6d 70 6c 65 6d 65 6e 74 20 0a 2a 2a s-complement .**
d1140 20 69 6e 74 65 67 65 72 2c 20 74 68 65 6e 20 77 integer, then w
d1150 72 69 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 rite that value
d1160 69 6e 74 6f 20 2a 70 4e 75 6d 20 61 6e 64 20 72 into *pNum and r
d1170 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 eturn 0..**.** I
d1180 66 20 7a 4e 75 6d 20 69 73 20 65 78 61 63 74 6c f zNum is exactl
d1190 79 20 39 32 32 33 33 37 32 30 33 36 38 35 34 36 y 92233720368546
d11a0 36 35 38 30 38 2c 20 72 65 74 75 72 6e 20 32 2e 65808, return 2.
d11b0 20 20 54 68 69 73 20 73 70 65 63 69 61 6c 0a 2a This special.*
d11c0 2a 20 63 61 73 65 20 69 73 20 62 72 6f 6b 65 6e * case is broken
d11d0 20 6f 75 74 20 62 65 63 61 75 73 65 20 77 68 69 out because whi
d11e0 6c 65 20 39 32 32 33 33 37 32 30 33 36 38 35 34 le 9223372036854
d11f0 36 36 35 38 30 38 20 63 61 6e 6e 6f 74 20 62 65 665808 cannot be
d1200 20 61 20 0a 2a 2a 20 73 69 67 6e 65 64 20 36 34 a .** signed 64
d1210 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 69 74 -bit integer, it
d1220 73 20 6e 65 67 61 74 69 76 65 20 2d 39 32 32 33 s negative -9223
d1230 33 37 32 30 33 36 38 35 34 36 36 35 38 30 38 20 372036854665808
d1240 63 61 6e 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 can be..**.** If
d1250 20 7a 4e 75 6d 20 69 73 20 74 6f 6f 20 62 69 67 zNum is too big
d1260 20 66 6f 72 20 61 20 36 34 2d 62 69 74 20 69 6e for a 64-bit in
d1270 74 65 67 65 72 20 61 6e 64 20 69 73 20 6e 6f 74 teger and is not
d1280 0a 2a 2a 20 39 32 32 33 33 37 32 30 33 36 38 35 .** 922337203685
d1290 34 36 36 35 38 30 38 20 74 68 65 6e 20 72 65 74 4665808 then ret
d12a0 75 72 6e 20 31 2e 0a 2a 2a 0a 2a 2a 20 6c 65 6e urn 1..**.** len
d12b0 67 74 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 gth is the numbe
d12c0 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
d12d0 65 20 73 74 72 69 6e 67 20 28 62 79 74 65 73 2c e string (bytes,
d12e0 20 6e 6f 74 20 63 68 61 72 61 63 74 65 72 73 29 not characters)
d12f0 2e 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 ..** The string
d1300 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 is not necessari
d1310 6c 79 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 ly zero-terminat
d1320 65 64 2e 20 20 54 68 65 20 65 6e 63 6f 64 69 6e ed. The encodin
d1330 67 20 69 73 0a 2a 2a 20 67 69 76 65 6e 20 62 79 g is.** given by
d1340 20 65 6e 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f enc..*/.SQLITE_
d1350 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
d1360 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 20 te3Atoi64(const
d1370 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 36 34 20 char *zNum, i64
d1380 2a 70 4e 75 6d 2c 20 69 6e 74 20 6c 65 6e 67 74 *pNum, int lengt
d1390 68 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69 6e h, u8 enc){. in
d13a0 74 20 69 6e 63 72 20 3d 20 28 65 6e 63 3d 3d 53 t incr = (enc==S
d13b0 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b QLITE_UTF8?1:2);
d13c0 0a 20 20 75 36 34 20 75 20 3d 20 30 3b 0a 20 20 . u64 u = 0;.
d13d0 69 6e 74 20 6e 65 67 20 3d 20 30 3b 20 2f 2a 20 int neg = 0; /*
d13e0 61 73 73 75 6d 65 20 70 6f 73 69 74 69 76 65 20 assume positive
d13f0 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e */. int i;. in
d1400 74 20 63 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 t c = 0;. const
d1410 20 63 68 61 72 20 2a 7a 53 74 61 72 74 3b 0a 20 char *zStart;.
d1420 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e const char *zEn
d1430 64 20 3d 20 7a 4e 75 6d 20 2b 20 6c 65 6e 67 74 d = zNum + lengt
d1440 68 3b 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 h;. if( enc==SQ
d1450 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 20 7a LITE_UTF16BE ) z
d1460 4e 75 6d 2b 2b 3b 0a 20 20 77 68 69 6c 65 28 20 Num++;. while(
d1470 7a 4e 75 6d 3c 7a 45 6e 64 20 26 26 20 73 71 6c zNum<zEnd && sql
d1480 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 4e 75 ite3Isspace(*zNu
d1490 6d 29 20 29 20 7a 4e 75 6d 2b 3d 69 6e 63 72 3b m) ) zNum+=incr;
d14a0 0a 20 20 69 66 28 20 7a 4e 75 6d 3c 7a 45 6e 64 . if( zNum<zEnd
d14b0 20 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a 4e 75 ){. if( *zNu
d14c0 6d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 m=='-' ){.
d14d0 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a neg = 1;. z
d14e0 4e 75 6d 2b 3d 69 6e 63 72 3b 0a 20 20 20 20 7d Num+=incr;. }
d14f0 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d else if( *zNum==
d1500 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 7a 4e 75 '+' ){. zNu
d1510 6d 2b 3d 69 6e 63 72 3b 0a 20 20 20 20 7d 0a 20 m+=incr;. }.
d1520 20 7d 0a 20 20 7a 53 74 61 72 74 20 3d 20 7a 4e }. zStart = zN
d1530 75 6d 3b 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 um;. while( zNu
d1540 6d 3c 7a 45 6e 64 20 26 26 20 7a 4e 75 6d 5b 30 m<zEnd && zNum[0
d1550 5d 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 3d ]=='0' ){ zNum+=
d1560 69 6e 63 72 3b 20 7d 20 2f 2a 20 53 6b 69 70 20 incr; } /* Skip
d1570 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 2a leading zeros. *
d1580 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 26 7a 4e /. for(i=0; &zN
d1590 75 6d 5b 69 5d 3c 7a 45 6e 64 20 26 26 20 28 63 um[i]<zEnd && (c
d15a0 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 =zNum[i])>='0' &
d15b0 26 20 63 3c 3d 27 39 27 3b 20 69 2b 3d 69 6e 63 & c<='9'; i+=inc
d15c0 72 29 7b 0a 20 20 20 20 75 20 3d 20 75 2a 31 30 r){. u = u*10
d15d0 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 7d 0a + c - '0';. }.
d15e0 20 20 69 66 28 20 75 3e 4c 41 52 47 45 53 54 5f if( u>LARGEST_
d15f0 49 4e 54 36 34 20 29 7b 0a 20 20 20 20 2a 70 4e INT64 ){. *pN
d1600 75 6d 20 3d 20 53 4d 41 4c 4c 45 53 54 5f 49 4e um = SMALLEST_IN
d1610 54 36 34 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 T64;. }else if(
d1620 20 6e 65 67 20 29 7b 0a 20 20 20 20 2a 70 4e 75 neg ){. *pNu
d1630 6d 20 3d 20 2d 28 69 36 34 29 75 3b 0a 20 20 7d m = -(i64)u;. }
d1640 65 6c 73 65 7b 0a 20 20 20 20 2a 70 4e 75 6d 20 else{. *pNum
d1650 3d 20 28 69 36 34 29 75 3b 0a 20 20 7d 0a 20 20 = (i64)u;. }.
d1660 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 38 20 testcase( i==18
d1670 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 69 );. testcase( i
d1680 3d 3d 31 39 20 29 3b 0a 20 20 74 65 73 74 63 61 ==19 );. testca
d1690 73 65 28 20 69 3d 3d 32 30 20 29 3b 0a 20 20 69 se( i==20 );. i
d16a0 66 28 20 28 63 21 3d 30 20 26 26 20 26 7a 4e 75 f( (c!=0 && &zNu
d16b0 6d 5b 69 5d 3c 7a 45 6e 64 29 20 7c 7c 20 28 69 m[i]<zEnd) || (i
d16c0 3d 3d 30 20 26 26 20 7a 53 74 61 72 74 3d 3d 7a ==0 && zStart==z
d16d0 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 2a 69 6e 63 Num) || i>19*inc
d16e0 72 20 29 7b 0a 20 20 20 20 2f 2a 20 7a 4e 75 6d r ){. /* zNum
d16f0 20 69 73 20 65 6d 70 74 79 20 6f 72 20 63 6f 6e is empty or con
d1700 74 61 69 6e 73 20 6e 6f 6e 2d 6e 75 6d 65 72 69 tains non-numeri
d1710 63 20 74 65 78 74 20 6f 72 20 69 73 20 6c 6f 6e c text or is lon
d1720 67 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 ger. ** than
d1730 31 39 20 64 69 67 69 74 73 20 28 74 68 75 73 20 19 digits (thus
d1740 67 75 61 72 61 6e 74 65 65 69 6e 67 20 74 68 61 guaranteeing tha
d1750 74 20 69 74 20 69 73 20 74 6f 6f 20 6c 61 72 67 t it is too larg
d1760 65 29 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e e) */. return
d1770 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 1;. }else if(
d1780 69 3c 31 39 2a 69 6e 63 72 20 29 7b 0a 20 20 20 i<19*incr ){.
d1790 20 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20 31 39 /* Less than 19
d17a0 20 64 69 67 69 74 73 2c 20 73 6f 20 77 65 20 6b digits, so we k
d17b0 6e 6f 77 20 74 68 61 74 20 69 74 20 66 69 74 73 now that it fits
d17c0 20 69 6e 20 36 34 20 62 69 74 73 20 2a 2f 0a 20 in 64 bits */.
d17d0 20 20 20 61 73 73 65 72 74 28 20 75 3c 3d 4c 41 assert( u<=LA
d17e0 52 47 45 53 54 5f 49 4e 54 36 34 20 29 3b 0a 20 RGEST_INT64 );.
d17f0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
d1800 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 7a 4e 75 else{. /* zNu
d1810 6d 20 69 73 20 61 20 31 39 2d 64 69 67 69 74 20 m is a 19-digit
d1820 6e 75 6d 62 65 72 73 2e 20 20 43 6f 6d 70 61 72 numbers. Compar
d1830 65 20 69 74 20 61 67 61 69 6e 73 74 20 39 32 32 e it against 922
d1840 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38 3372036854775808
d1850 2e 20 2a 2f 0a 20 20 20 20 63 20 3d 20 63 6f 6d . */. c = com
d1860 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d 2c pare2pow63(zNum,
d1870 20 69 6e 63 72 29 3b 0a 20 20 20 20 69 66 28 20 incr);. if(
d1880 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 c<0 ){. /*
d1890 7a 4e 75 6d 20 69 73 20 6c 65 73 73 20 74 68 61 zNum is less tha
d18a0 6e 20 39 32 32 33 33 37 32 30 33 36 38 35 34 37 n 92233720368547
d18b0 37 35 38 30 38 20 73 6f 20 69 74 20 66 69 74 73 75808 so it fits
d18c0 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 */. assert
d18d0 28 20 75 3c 3d 4c 41 52 47 45 53 54 5f 49 4e 54 ( u<=LARGEST_INT
d18e0 36 34 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 64 );. retu
d18f0 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 rn 0;. }else
d1900 69 66 28 20 63 3e 30 20 29 7b 0a 20 20 20 20 20 if( c>0 ){.
d1910 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 67 72 65 61 /* zNum is grea
d1920 74 65 72 20 74 68 61 6e 20 39 32 32 33 33 37 32 ter than 9223372
d1930 30 33 36 38 35 34 37 37 35 38 30 38 20 73 6f 20 036854775808 so
d1940 69 74 20 6f 76 65 72 66 6c 6f 77 73 20 2a 2f 0a it overflows */.
d1950 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
d1960 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d1970 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 78 61 63 /* zNum is exac
d1980 74 6c 79 20 39 32 32 33 33 37 32 30 33 36 38 35 tly 922337203685
d1990 34 37 37 35 38 30 38 2e 20 20 46 69 74 73 20 69 4775808. Fits i
d19a0 66 20 6e 65 67 61 74 69 76 65 2e 20 20 54 68 65 f negative. The
d19b0 0a 20 20 20 20 20 20 2a 2a 20 73 70 65 63 69 61 . ** specia
d19c0 6c 20 63 61 73 65 20 32 20 6f 76 65 72 66 6c 6f l case 2 overflo
d19d0 77 20 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f w if positive */
d19e0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 . assert( u
d19f0 2d 31 3d 3d 4c 41 52 47 45 53 54 5f 49 4e 54 36 -1==LARGEST_INT6
d1a00 34 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 4 );. asser
d1a10 74 28 20 28 2a 70 4e 75 6d 29 3d 3d 53 4d 41 4c t( (*pNum)==SMAL
d1a20 4c 45 53 54 5f 49 4e 54 36 34 20 29 3b 0a 20 20 LEST_INT64 );.
d1a30 20 20 20 20 72 65 74 75 72 6e 20 6e 65 67 20 3f return neg ?
d1a40 20 30 20 3a 20 32 3b 0a 20 20 20 20 7d 0a 20 20 0 : 2;. }.
d1a50 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e }.}../*.** If zN
d1a60 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e um represents an
d1a70 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 77 69 integer that wi
d1a80 6c 6c 20 66 69 74 20 69 6e 20 33 32 2d 62 69 74 ll fit in 32-bit
d1a90 73 2c 20 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a s, then set.** *
d1aa0 70 56 61 6c 75 65 20 74 6f 20 74 68 61 74 20 69 pValue to that i
d1ab0 6e 74 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 nteger and retur
d1ac0 6e 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 n true. Otherwi
d1ad0 73 65 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e se return false.
d1ae0 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e .**.** Any non-n
d1af0 75 6d 65 72 69 63 20 63 68 61 72 61 63 74 65 72 umeric character
d1b00 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 s that following
d1b10 20 7a 4e 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 zNum are ignore
d1b20 64 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69 d..** This is di
d1b30 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c fferent from sql
d1b40 69 74 65 33 41 74 6f 69 36 34 28 29 20 77 68 69 ite3Atoi64() whi
d1b50 63 68 20 72 65 71 75 69 72 65 73 20 74 68 65 0a ch requires the.
d1b60 2a 2a 20 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 ** input number
d1b70 74 6f 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 to be zero-termi
d1b80 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nated..*/.SQLITE
d1b90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
d1ba0 69 74 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e ite3GetInt32(con
d1bb0 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 st char *zNum, i
d1bc0 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 nt *pValue){. s
d1bd0 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 qlite_int64 v =
d1be0 30 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 0;. int i, c;.
d1bf0 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 int neg = 0;.
d1c00 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 if( zNum[0]=='-'
d1c10 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b ){. neg = 1;
d1c20 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d . zNum++;. }
d1c30 65 6c 73 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d else if( zNum[0]
d1c40 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 =='+' ){. zNu
d1c50 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 m++;. }. while
d1c60 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 ( zNum[0]=='0' )
d1c70 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 zNum++;. for(i
d1c80 3d 30 3b 20 69 3c 31 31 20 26 26 20 28 63 20 3d =0; i<11 && (c =
d1c90 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e zNum[i] - '0')>
d1ca0 3d 30 20 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 =0 && c<=9; i++)
d1cb0 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b {. v = v*10 +
d1cc0 20 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 c;. }.. /* Th
d1cd0 65 20 6c 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 e longest decima
d1ce0 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e l representation
d1cf0 20 6f 66 20 61 20 33 32 20 62 69 74 20 69 6e 74 of a 32 bit int
d1d00 65 67 65 72 20 69 73 20 31 30 20 64 69 67 69 74 eger is 10 digit
d1d10 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 s:. **. **
d1d20 20 20 20 20 20 20 20 20 20 31 32 33 34 35 36 37 1234567
d1d30 38 39 30 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 890. ** 2^3
d1d40 31 20 2d 3e 20 32 31 34 37 34 38 33 36 34 38 0a 1 -> 2147483648.
d1d50 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 */. testcase(
d1d60 20 69 3d 3d 31 30 20 29 3b 0a 20 20 69 66 28 20 i==10 );. if(
d1d70 69 3e 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75 i>10 ){. retu
d1d80 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 74 65 73 74 rn 0;. }. test
d1d90 63 61 73 65 28 20 76 2d 6e 65 67 3d 3d 32 31 34 case( v-neg==214
d1da0 37 34 38 33 36 34 37 20 29 3b 0a 20 20 69 66 28 7483647 );. if(
d1db0 20 76 2d 6e 65 67 3e 32 31 34 37 34 38 33 36 34 v-neg>214748364
d1dc0 37 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 7 ){. return
d1dd0 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 0;. }. if( neg
d1de0 20 29 7b 0a 20 20 20 20 76 20 3d 20 2d 76 3b 0a ){. v = -v;.
d1df0 20 20 7d 0a 20 20 2a 70 56 61 6c 75 65 20 3d 20 }. *pValue =
d1e00 28 69 6e 74 29 76 3b 0a 20 20 72 65 74 75 72 6e (int)v;. return
d1e10 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 1;.}../*.** Ret
d1e20 75 72 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 urn a 32-bit int
d1e30 65 67 65 72 20 76 61 6c 75 65 20 65 78 74 72 61 eger value extra
d1e40 63 74 65 64 20 66 72 6f 6d 20 61 20 73 74 72 69 cted from a stri
d1e50 6e 67 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 73 ng. If the.** s
d1e60 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61 6e 20 tring is not an
d1e70 69 6e 74 65 67 65 72 2c 20 6a 75 73 74 20 72 65 integer, just re
d1e80 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 turn 0..*/.SQLIT
d1e90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
d1ea0 6c 69 74 65 33 41 74 6f 69 28 63 6f 6e 73 74 20 lite3Atoi(const
d1eb0 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 char *z){. int
d1ec0 78 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a 20 29 x = 0;. if( z )
d1ed0 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 sqlite3GetInt32
d1ee0 28 7a 2c 20 26 78 29 3b 0a 20 20 72 65 74 75 72 (z, &x);. retur
d1ef0 6e 20 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 n x;.}../*.** Th
d1f00 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 e variable-lengt
d1f10 68 20 69 6e 74 65 67 65 72 20 65 6e 63 6f 64 69 h integer encodi
d1f20 6e 67 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 ng is as follows
d1f30 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 :.**.** KEY:.**
d1f40 20 20 20 20 20 20 20 20 41 20 3d 20 30 78 78 78 A = 0xxx
d1f50 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20 6f xxxx 7 bits o
d1f60 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 f data and one f
d1f70 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 lag bit.**
d1f80 20 20 20 42 20 3d 20 31 78 78 78 78 78 78 78 20 B = 1xxxxxxx
d1f90 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 7 bits of dat
d1fa0 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 a and one flag b
d1fb0 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 43 20 it.** C
d1fc0 3d 20 78 78 78 78 78 78 78 78 20 20 20 20 38 20 = xxxxxxxx 8
d1fd0 62 69 74 73 20 6f 66 20 64 61 74 61 0a 2a 2a 0a bits of data.**.
d1fe0 2a 2a 20 20 37 20 62 69 74 73 20 2d 20 41 0a 2a ** 7 bits - A.*
d1ff0 2a 20 31 34 20 62 69 74 73 20 2d 20 42 41 0a 2a * 14 bits - BA.*
d2000 2a 20 32 31 20 62 69 74 73 20 2d 20 42 42 41 0a * 21 bits - BBA.
d2010 2a 2a 20 32 38 20 62 69 74 73 20 2d 20 42 42 42 ** 28 bits - BBB
d2020 41 0a 2a 2a 20 33 35 20 62 69 74 73 20 2d 20 42 A.** 35 bits - B
d2030 42 42 42 41 0a 2a 2a 20 34 32 20 62 69 74 73 20 BBBA.** 42 bits
d2040 2d 20 42 42 42 42 42 41 0a 2a 2a 20 34 39 20 62 - BBBBBA.** 49 b
d2050 69 74 73 20 2d 20 42 42 42 42 42 42 41 0a 2a 2a its - BBBBBBA.**
d2060 20 35 36 20 62 69 74 73 20 2d 20 42 42 42 42 42 56 bits - BBBBB
d2070 42 42 41 0a 2a 2a 20 36 34 20 62 69 74 73 20 2d BBA.** 64 bits -
d2080 20 42 42 42 42 42 42 42 42 43 0a 2a 2f 0a 0a 2f BBBBBBBBC.*/../
d2090 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 36 34 2d *.** Write a 64-
d20a0 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e bit variable-len
d20b0 67 74 68 20 69 6e 74 65 67 65 72 20 74 6f 20 6d gth integer to m
d20c0 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 emory starting a
d20d0 74 20 70 5b 30 5d 2e 0a 2a 2a 20 54 68 65 20 6c t p[0]..** The l
d20e0 65 6e 67 74 68 20 6f 66 20 64 61 74 61 20 77 72 ength of data wr
d20f0 69 74 65 20 77 69 6c 6c 20 62 65 20 62 65 74 77 ite will be betw
d2100 65 65 6e 20 31 20 61 6e 64 20 39 20 62 79 74 65 een 1 and 9 byte
d2110 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a s. The number.*
d2120 2a 20 6f 66 20 62 79 74 65 73 20 77 72 69 74 74 * of bytes writt
d2130 65 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a en is returned..
d2140 2a 2a 0a 2a 2a 20 41 20 76 61 72 69 61 62 6c 65 **.** A variable
d2150 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 -length integer
d2160 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20 consists of the
d2170 6c 6f 77 65 72 20 37 20 62 69 74 73 20 6f 66 20 lower 7 bits of
d2180 65 61 63 68 20 62 79 74 65 0a 2a 2a 20 66 6f 72 each byte.** for
d2190 20 61 6c 6c 20 62 79 74 65 73 20 74 68 61 74 20 all bytes that
d21a0 68 61 76 65 20 74 68 65 20 38 74 68 20 62 69 74 have the 8th bit
d21b0 20 73 65 74 20 61 6e 64 20 6f 6e 65 20 62 79 74 set and one byt
d21c0 65 20 77 69 74 68 20 74 68 65 20 38 74 68 0a 2a e with the 8th.*
d21d0 2a 20 62 69 74 20 63 6c 65 61 72 2e 20 20 45 78 * bit clear. Ex
d21e0 63 65 70 74 2c 20 69 66 20 77 65 20 67 65 74 20 cept, if we get
d21f0 74 6f 20 74 68 65 20 39 74 68 20 62 79 74 65 2c to the 9th byte,
d2200 20 69 74 20 73 74 6f 72 65 73 20 74 68 65 20 66 it stores the f
d2210 75 6c 6c 0a 2a 2a 20 38 20 62 69 74 73 20 61 6e ull.** 8 bits an
d2220 64 20 69 73 20 74 68 65 20 6c 61 73 74 20 62 79 d is the last by
d2230 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 te..*/.SQLITE_PR
d2240 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
d2250 33 50 75 74 56 61 72 69 6e 74 28 75 6e 73 69 67 3PutVarint(unsig
d2260 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36 34 ned char *p, u64
d2270 20 76 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c v){. int i, j,
d2280 20 6e 3b 0a 20 20 75 38 20 62 75 66 5b 31 30 5d n;. u8 buf[10]
d2290 3b 0a 20 20 69 66 28 20 76 20 26 20 28 28 28 75 ;. if( v & (((u
d22a0 36 34 29 30 78 66 66 30 30 30 30 30 30 29 3c 3c 64)0xff000000)<<
d22b0 33 32 29 20 29 7b 0a 20 20 20 20 70 5b 38 5d 20 32) ){. p[8]
d22c0 3d 20 28 75 38 29 76 3b 0a 20 20 20 20 76 20 3e = (u8)v;. v >
d22d0 3e 3d 20 38 3b 0a 20 20 20 20 66 6f 72 28 69 3d >= 8;. for(i=
d22e0 37 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 7; i>=0; i--){.
d22f0 20 20 20 20 20 70 5b 69 5d 20 3d 20 28 75 38 29 p[i] = (u8)
d2300 28 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 78 ((v & 0x7f) | 0x
d2310 38 30 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 80);. v >>=
d2320 20 37 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 7;. }. re
d2330 74 75 72 6e 20 39 3b 0a 20 20 7d 20 20 20 20 0a turn 9;. } .
d2340 20 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 n = 0;. do{.
d2350 20 20 20 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 75 buf[n++] = (u
d2360 38 29 28 28 76 20 26 20 30 78 37 66 29 20 7c 20 8)((v & 0x7f) |
d2370 30 78 38 30 29 3b 0a 20 20 20 20 76 20 3e 3e 3d 0x80);. v >>=
d2380 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 7;. }while( v!
d2390 3d 30 20 29 3b 0a 20 20 62 75 66 5b 30 5d 20 26 =0 );. buf[0] &
d23a0 3d 20 30 78 37 66 3b 0a 20 20 61 73 73 65 72 74 = 0x7f;. assert
d23b0 28 20 6e 3c 3d 39 20 29 3b 0a 20 20 66 6f 72 28 ( n<=9 );. for(
d23c0 69 3d 30 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e 3d 30 i=0, j=n-1; j>=0
d23d0 3b 20 6a 2d 2d 2c 20 69 2b 2b 29 7b 0a 20 20 20 ; j--, i++){.
d23e0 20 70 5b 69 5d 20 3d 20 62 75 66 5b 6a 5d 3b 0a p[i] = buf[j];.
d23f0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a }. return n;.
d2400 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
d2410 75 74 69 6e 65 20 69 73 20 61 20 66 61 73 74 65 utine is a faste
d2420 72 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c r version of sql
d2430 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 29 20 ite3PutVarint()
d2440 74 68 61 74 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 that only.** wor
d2450 6b 73 20 66 6f 72 20 33 32 2d 62 69 74 20 70 6f ks for 32-bit po
d2460 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 73 20 sitive integers
d2470 61 6e 64 20 77 68 69 63 68 20 69 73 20 6f 70 74 and which is opt
d2480 69 6d 69 7a 65 64 20 66 6f 72 0a 2a 2a 20 74 68 imized for.** th
d2490 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 6f 66 e common case of
d24a0 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e small integers.
d24b0 20 20 41 20 4d 41 43 52 4f 20 76 65 72 73 69 6f A MACRO versio
d24c0 6e 2c 20 70 75 74 56 61 72 69 6e 74 33 32 2c 0a n, putVarint32,.
d24d0 2a 2a 20 69 73 20 70 72 6f 76 69 64 65 64 20 77 ** is provided w
d24e0 68 69 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 hich inlines the
d24f0 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 single-byte cas
d2500 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f e. All code sho
d2510 75 6c 64 20 75 73 65 0a 2a 2a 20 74 68 65 20 4d uld use.** the M
d2520 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 ACRO version as
d2530 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 this function as
d2540 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65 sumes the single
d2550 2d 62 79 74 65 20 63 61 73 65 20 68 61 73 0a 2a -byte case has.*
d2560 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68 * already been h
d2570 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 andled..*/.SQLIT
d2580 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
d2590 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 lite3PutVarint32
d25a0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a (unsigned char *
d25b0 70 2c 20 75 33 32 20 76 29 7b 0a 23 69 66 6e 64 p, u32 v){.#ifnd
d25c0 65 66 20 70 75 74 56 61 72 69 6e 74 33 32 0a 20 ef putVarint32.
d25d0 20 69 66 28 20 28 76 20 26 20 7e 30 78 37 66 29 if( (v & ~0x7f)
d25e0 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 ==0 ){. p[0]
d25f0 3d 20 76 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = v;. return
d2600 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 1;. }.#endif.
d2610 69 66 28 20 28 76 20 26 20 7e 30 78 33 66 66 66 if( (v & ~0x3fff
d2620 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d )==0 ){. p[0]
d2630 20 3d 20 28 75 38 29 28 28 76 3e 3e 37 29 20 7c = (u8)((v>>7) |
d2640 20 30 78 38 30 29 3b 0a 20 20 20 20 70 5b 31 5d 0x80);. p[1]
d2650 20 3d 20 28 75 38 29 28 76 20 26 20 30 78 37 66 = (u8)(v & 0x7f
d2660 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b );. return 2;
d2670 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 . }. return sq
d2680 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 70 lite3PutVarint(p
d2690 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 , v);.}../*.** B
d26a0 69 74 6d 61 73 6b 73 20 75 73 65 64 20 62 79 20 itmasks used by
d26b0 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 sqlite3GetVarint
d26c0 28 29 2e 20 20 54 68 65 73 65 20 70 72 65 63 6f (). These preco
d26d0 6d 70 75 74 65 64 20 63 6f 6e 73 74 61 6e 74 73 mputed constants
d26e0 0a 2a 2a 20 61 72 65 20 64 65 66 69 6e 65 64 20 .** are defined
d26f0 68 65 72 65 20 72 61 74 68 65 72 20 74 68 61 6e here rather than
d2700 20 73 69 6d 70 6c 79 20 70 75 74 74 69 6e 67 20 simply putting
d2710 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 the constant exp
d2720 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 69 6e 6c 69 ressions.** inli
d2730 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 77 ne in order to w
d2740 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20 ork around bugs
d2750 69 6e 20 74 68 65 20 52 56 54 20 63 6f 6d 70 69 in the RVT compi
d2760 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 53 4c 4f 54 5f ler..**.** SLOT_
d2770 32 5f 30 20 20 20 20 20 41 20 6d 61 73 6b 20 66 2_0 A mask f
d2780 6f 72 20 20 28 30 78 37 66 3c 3c 31 34 29 20 7c or (0x7f<<14) |
d2790 20 30 78 37 66 0a 2a 2a 0a 2a 2a 20 53 4c 4f 54 0x7f.**.** SLOT
d27a0 5f 34 5f 32 5f 30 20 20 20 41 20 6d 61 73 6b 20 _4_2_0 A mask
d27b0 66 6f 72 20 20 28 30 78 37 66 3c 3c 32 38 29 20 for (0x7f<<28)
d27c0 7c 20 53 4c 4f 54 5f 32 5f 30 0a 2a 2f 0a 23 64 | SLOT_2_0.*/.#d
d27d0 65 66 69 6e 65 20 53 4c 4f 54 5f 32 5f 30 20 20 efine SLOT_2_0
d27e0 20 20 20 30 78 30 30 31 66 63 30 37 66 0a 23 64 0x001fc07f.#d
d27f0 65 66 69 6e 65 20 53 4c 4f 54 5f 34 5f 32 5f 30 efine SLOT_4_2_0
d2800 20 20 20 30 78 66 30 31 66 63 30 37 66 0a 0a 0a 0xf01fc07f...
d2810 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 2d /*.** Read a 64-
d2820 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e bit variable-len
d2830 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d gth integer from
d2840 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 memory starting
d2850 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 at p[0]..** Ret
d2860 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
d2870 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 f bytes read. T
d2880 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 he value is stor
d2890 65 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c ed in *v..*/.SQL
d28a0 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 ITE_PRIVATE u8 s
d28b0 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 qlite3GetVarint(
d28c0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
d28d0 68 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 29 7b har *p, u64 *v){
d28e0 0a 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a 0a 20 . u32 a,b,s;..
d28f0 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a a = *p;. /* a:
d2900 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a p0 (unmasked) *
d2910 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30 /. if (!(a&0x80
d2920 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d 20 )). {. *v =
d2930 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b a;. return 1;
d2940 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 . }.. p++;. b
d2950 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 = *p;. /* b: p
d2960 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 1 (unmasked) */.
d2970 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 if (!(b&0x80))
d2980 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78 . {. a &= 0x
d2990 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 7f;. a = a<<7
d29a0 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 ;. a |= b;.
d29b0 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 *v = a;. re
d29c0 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f turn 2;. }.. /
d29d0 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 63 6f * Verify that co
d29e0 6e 73 74 61 6e 74 73 20 61 72 65 20 70 72 65 63 nstants are prec
d29f0 6f 6d 70 75 74 65 64 20 63 6f 72 72 65 63 74 6c omputed correctl
d2a00 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 53 y */. assert( S
d2a10 4c 4f 54 5f 32 5f 30 20 3d 3d 20 28 28 30 78 37 LOT_2_0 == ((0x7
d2a20 66 3c 3c 31 34 29 20 7c 20 28 30 78 37 66 29 29 f<<14) | (0x7f))
d2a30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 4c );. assert( SL
d2a40 4f 54 5f 34 5f 32 5f 30 20 3d 3d 20 28 28 30 78 OT_4_2_0 == ((0x
d2a50 66 55 3c 3c 32 38 29 20 7c 20 28 30 78 37 66 3c fU<<28) | (0x7f<
d2a60 3c 31 34 29 20 7c 20 28 30 78 37 66 29 29 20 29 <14) | (0x7f)) )
d2a70 3b 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 ;.. p++;. a =
d2a80 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 a<<14;. a |= *p
d2a90 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 ;. /* a: p0<<14
d2aa0 20 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29 | p2 (unmasked)
d2ab0 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 */. if (!(a&0x
d2ac0 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 80)). {. a &
d2ad0 3d 20 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 20 20 = SLOT_2_0;.
d2ae0 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 b &= 0x7f;. b
d2af0 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c = b<<7;. a |
d2b00 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b = b;. *v = a;
d2b10 0a 20 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 . return 3;.
d2b20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 31 20 66 72 }.. /* CSE1 fr
d2b30 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 om below */. a
d2b40 26 3d 20 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 70 &= SLOT_2_0;. p
d2b50 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b ++;. b = b<<14;
d2b60 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a . b |= *p;. /*
d2b70 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 b: p1<<14 | p3
d2b80 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
d2b90 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 if (!(b&0x80)).
d2ba0 20 7b 0a 20 20 20 20 62 20 26 3d 20 53 4c 4f 54 {. b &= SLOT
d2bb0 5f 32 5f 30 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 _2_0;. /* mov
d2bc0 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 20 ed CSE1 up */.
d2bd0 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c /* a &= (0x7f<
d2be0 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a <14)|(0x7f); */.
d2bf0 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 a = a<<7;.
d2c00 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 a |= b;. *v
d2c10 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e = a;. return
d2c20 20 34 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 3a 4;. }.. /* a:
d2c30 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 p0<<14 | p2 (ma
d2c40 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 62 3a sked) */. /* b:
d2c50 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e p1<<14 | p3 (un
d2c60 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 masked) */. /*
d2c70 31 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32 1:save off p0<<2
d2c80 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 1 | p1<<14 | p2<
d2c90 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 <7 | p3 (masked)
d2ca0 20 2a 2f 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 43 */. /* moved C
d2cb0 53 45 31 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 SE1 up */. /* a
d2cc0 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
d2cd0 30 78 37 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 0x7f); */. b &=
d2ce0 20 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 73 20 3d SLOT_2_0;. s =
d2cf0 20 61 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c a;. /* s: p0<<
d2d00 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 14 | p2 (masked)
d2d10 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 */.. p++;. a
d2d20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 = a<<14;. a |=
d2d30 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c *p;. /* a: p0<<
d2d40 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34 28 | p2<<14 | p4
d2d50 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
d2d60 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a if (!(a&0x80)).
d2d70 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 {. /* we ca
d2d80 6e 20 73 6b 69 70 20 74 68 65 73 65 20 63 61 75 n skip these cau
d2d90 73 65 20 74 68 65 79 20 77 65 72 65 20 28 65 66 se they were (ef
d2da0 66 65 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 fectively) done
d2db0 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e above in calc'in
d2dc0 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 g s */. /* a
d2dd0 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 &= (0x7f<<28)|(0
d2de0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
d2df0 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 */. /* b &=
d2e00 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
d2e10 29 3b 20 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c ); */. b = b<
d2e20 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a <7;. a |= b;.
d2e30 20 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 s = s>>18;.
d2e40 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 *v = ((u64)s)
d2e50 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 <<32 | a;. re
d2e60 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f turn 5;. }.. /
d2e70 2a 20 32 3a 73 61 76 65 20 6f 66 66 20 70 30 3c * 2:save off p0<
d2e80 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 <21 | p1<<14 | p
d2e90 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 2<<7 | p3 (maske
d2ea0 64 29 20 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37 d) */. s = s<<7
d2eb0 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a ;. s |= b;. /*
d2ec0 20 73 3a 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c s: p0<<21 | p1<
d2ed0 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 <14 | p2<<7 | p3
d2ee0 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 (masked) */..
d2ef0 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 p++;. b = b<<14
d2f00 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f ;. b |= *p;. /
d2f10 2a 20 62 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33 * b: p1<<28 | p3
d2f20 3c 3c 31 34 20 7c 20 70 35 20 28 75 6e 6d 61 73 <<14 | p5 (unmas
d2f30 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
d2f40 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 b&0x80)). {.
d2f50 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20 /* we can skip
d2f60 74 68 69 73 20 63 61 75 73 65 20 69 74 20 77 61 this cause it wa
d2f70 73 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 s (effectively)
d2f80 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 done above in ca
d2f90 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 lc'ing s */.
d2fa0 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 32 /* b &= (0x7f<<2
d2fb0 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 8)|(0x7f<<14)|(0
d2fc0 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26 x7f); */. a &
d2fd0 3d 20 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 20 20 = SLOT_2_0;.
d2fe0 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 a = a<<7;. a
d2ff0 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e |= b;. s = s>
d3000 3e 31 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 >18;. *v = ((
d3010 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a u64)s)<<32 | a;.
d3020 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 return 6;.
d3030 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 }.. p++;. a =
d3040 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 a<<14;. a |= *p
d3050 3b 0a 20 20 2f 2a 20 61 3a 20 70 32 3c 3c 32 38 ;. /* a: p2<<28
d3060 20 7c 20 70 34 3c 3c 31 34 20 7c 20 70 36 20 28 | p4<<14 | p6 (
d3070 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 unmasked) */. i
d3080 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 f (!(a&0x80)).
d3090 7b 0a 20 20 20 20 61 20 26 3d 20 53 4c 4f 54 5f {. a &= SLOT_
d30a0 34 5f 32 5f 30 3b 0a 20 20 20 20 62 20 26 3d 20 4_2_0;. b &=
d30b0 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 20 20 62 20 SLOT_2_0;. b
d30c0 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d = b<<7;. a |=
d30d0 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 b;. s = s>>1
d30e0 31 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 1;. *v = ((u6
d30f0 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 4)s)<<32 | a;.
d3100 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a return 7;. }.
d3110 0a 20 20 2f 2a 20 43 53 45 32 20 66 72 6f 6d 20 . /* CSE2 from
d3120 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 3d 20 below */. a &=
d3130 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 70 2b 2b 3b SLOT_2_0;. p++;
d3140 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 . b = b<<14;.
d3150 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a b |= *p;. /* b:
d3160 20 70 33 3c 3c 32 38 20 7c 20 70 35 3c 3c 31 34 p3<<28 | p5<<14
d3170 20 7c 20 70 37 20 28 75 6e 6d 61 73 6b 65 64 29 | p7 (unmasked)
d3180 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 */. if (!(b&0x
d3190 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 80)). {. b &
d31a0 3d 20 53 4c 4f 54 5f 34 5f 32 5f 30 3b 0a 20 20 = SLOT_4_2_0;.
d31b0 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 /* moved CSE2
d31c0 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 up */. /* a &
d31d0 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
d31e0 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 7f); */. a =
d31f0 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 a<<7;. a |= b
d3200 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 34 3b 0a ;. s = s>>4;.
d3210 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 *v = ((u64)s
d3220 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 )<<32 | a;. r
d3230 65 74 75 72 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 eturn 8;. }..
d3240 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 35 p++;. a = a<<15
d3250 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f ;. a |= *p;. /
d3260 2a 20 61 3a 20 70 34 3c 3c 32 39 20 7c 20 70 36 * a: p4<<29 | p6
d3270 3c 3c 31 35 20 7c 20 70 38 20 28 75 6e 6d 61 73 <<15 | p8 (unmas
d3280 6b 65 64 29 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f ked) */.. /* mo
d3290 76 65 64 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 ved CSE2 up */.
d32a0 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c /* a &= (0x7f<<
d32b0 32 39 29 7c 28 30 78 37 66 3c 3c 31 35 29 7c 28 29)|(0x7f<<15)|(
d32c0 30 78 66 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 0xff); */. b &=
d32d0 20 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 62 20 3d SLOT_2_0;. b =
d32e0 20 62 3c 3c 38 3b 0a 20 20 61 20 7c 3d 20 62 3b b<<8;. a |= b;
d32f0 0a 0a 20 20 73 20 3d 20 73 3c 3c 34 3b 0a 20 20 .. s = s<<4;.
d3300 62 20 3d 20 70 5b 2d 34 5d 3b 0a 20 20 62 20 26 b = p[-4];. b &
d3310 3d 20 30 78 37 66 3b 0a 20 20 62 20 3d 20 62 3e = 0x7f;. b = b>
d3320 3e 33 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 0a 20 >3;. s |= b;..
d3330 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c *v = ((u64)s)<<
d3340 33 32 20 7c 20 61 3b 0a 0a 20 20 72 65 74 75 72 32 | a;.. retur
d3350 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 n 9;.}../*.** Re
d3360 61 64 20 61 20 33 32 2d 62 69 74 20 76 61 72 69 ad a 32-bit vari
d3370 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 able-length inte
d3380 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 ger from memory
d3390 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d starting at p[0]
d33a0 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 ..** Return the
d33b0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
d33c0 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65 read. The value
d33d0 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 is stored in *v
d33e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 ..**.** If the v
d33f0 61 72 69 6e 74 20 73 74 6f 72 65 64 20 69 6e 20 arint stored in
d3400 70 5b 30 5d 20 69 73 20 6c 61 72 67 65 72 20 74 p[0] is larger t
d3410 68 61 6e 20 63 61 6e 20 66 69 74 20 69 6e 20 61 han can fit in a
d3420 20 33 32 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 32-bit unsigned
d3430 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 74 68 65 .** integer, the
d3440 6e 20 73 65 74 20 2a 76 20 74 6f 20 30 78 66 66 n set *v to 0xff
d3450 66 66 66 66 66 66 2e 0a 2a 2a 0a 2a 2a 20 41 20 ffffff..**.** A
d3460 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 67 MACRO version, g
d3470 65 74 56 61 72 69 6e 74 33 32 2c 20 69 73 20 70 etVarint32, is p
d3480 72 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 6e rovided which in
d3490 6c 69 6e 65 73 20 74 68 65 20 0a 2a 2a 20 73 69 lines the .** si
d34a0 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20 ngle-byte case.
d34b0 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64 All code should
d34c0 20 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20 76 use the MACRO v
d34d0 65 72 73 69 6f 6e 20 61 73 20 0a 2a 2a 20 74 68 ersion as .** th
d34e0 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 is function assu
d34f0 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 mes the single-b
d3500 79 74 65 20 63 61 73 65 20 68 61 73 20 61 6c 72 yte case has alr
d3510 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64 6c 65 eady been handle
d3520 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
d3530 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 VATE u8 sqlite3G
d3540 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 etVarint32(const
d3550 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
d3560 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20 75 33 p, u32 *v){. u3
d3570 32 20 61 2c 62 3b 0a 0a 20 20 2f 2a 20 54 68 65 2 a,b;.. /* The
d3580 20 31 2d 62 79 74 65 20 63 61 73 65 2e 20 20 4f 1-byte case. O
d3590 76 65 72 77 68 65 6c 6d 69 6e 67 6c 79 20 74 68 verwhelmingly th
d35a0 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 2e 20 20 e most common.
d35b0 48 61 6e 64 6c 65 64 20 69 6e 6c 69 6e 65 0a 20 Handled inline.
d35c0 20 2a 2a 20 62 79 20 74 68 65 20 67 65 74 56 61 ** by the getVa
d35d0 72 69 6e 33 32 28 29 20 6d 61 63 72 6f 20 2a 2f rin32() macro */
d35e0 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 . a = *p;. /*
d35f0 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 a: p0 (unmasked)
d3600 20 2a 2f 0a 23 69 66 6e 64 65 66 20 67 65 74 56 */.#ifndef getV
d3610 61 72 69 6e 74 33 32 0a 20 20 69 66 20 28 21 28 arint32. if (!(
d3620 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 a&0x80)). {.
d3630 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65 /* Values betwe
d3640 65 6e 20 30 20 61 6e 64 20 31 32 37 20 2a 2f 0a en 0 and 127 */.
d3650 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 *v = a;.
d3660 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 return 1;. }.#e
d3670 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65 20 32 ndif.. /* The 2
d3680 2d 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 -byte case */.
d3690 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 p++;. b = *p;.
d36a0 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 /* b: p1 (unmas
d36b0 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
d36c0 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 b&0x80)). {.
d36d0 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65 /* Values betwe
d36e0 65 6e 20 31 32 38 20 61 6e 64 20 31 36 33 38 33 en 128 and 16383
d36f0 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 30 78 37 */. a &= 0x7
d3700 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b f;. a = a<<7;
d3710 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b . *v = a | b;
d3720 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 . return 2;.
d3730 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 33 2d 62 }.. /* The 3-b
d3740 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 70 2b yte case */. p+
d3750 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a +;. a = a<<14;.
d3760 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 a |= *p;. /*
d3770 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 a: p0<<14 | p2 (
d3780 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 unmasked) */. i
d3790 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 f (!(a&0x80)).
d37a0 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 {. /* Values
d37b0 62 65 74 77 65 65 6e 20 31 36 33 38 34 20 61 6e between 16384 an
d37c0 64 20 32 30 39 37 31 35 31 20 2a 2f 0a 20 20 20 d 2097151 */.
d37d0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 a &= (0x7f<<14)
d37e0 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 |(0x7f);. b &
d37f0 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d 20 = 0x7f;. b =
d3800 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 b<<7;. *v = a
d3810 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e | b;. return
d3820 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 20 3;. }.. /* A
d3830 33 32 2d 62 69 74 20 76 61 72 69 6e 74 20 69 73 32-bit varint is
d3840 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 73 used to store s
d3850 69 7a 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ize information
d3860 69 6e 20 62 74 72 65 65 73 2e 0a 20 20 2a 2a 20 in btrees.. **
d3870 4f 62 6a 65 63 74 73 20 61 72 65 20 72 61 72 65 Objects are rare
d3880 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 32 ly larger than 2
d3890 4d 69 42 20 6c 69 6d 69 74 20 6f 66 20 61 20 33 MiB limit of a 3
d38a0 2d 62 79 74 65 20 76 61 72 69 6e 74 2e 0a 20 20 -byte varint..
d38b0 2a 2a 20 41 20 33 2d 62 79 74 65 20 76 61 72 69 ** A 3-byte vari
d38c0 6e 74 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 nt is sufficient
d38d0 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 , for example, t
d38e0 6f 20 72 65 63 6f 72 64 20 74 68 65 20 73 69 7a o record the siz
d38f0 65 0a 20 20 2a 2a 20 6f 66 20 61 20 31 30 34 38 e. ** of a 1048
d3900 35 36 39 2d 62 79 74 65 20 42 4c 4f 42 20 6f 72 569-byte BLOB or
d3910 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20 string.. **.
d3920 2a 2a 20 57 65 20 6f 6e 6c 79 20 75 6e 72 6f 6c ** We only unrol
d3930 6c 20 74 68 65 20 66 69 72 73 74 20 31 2d 2c 20 l the first 1-,
d3940 32 2d 2c 20 61 6e 64 20 33 2d 20 62 79 74 65 20 2-, and 3- byte
d3950 63 61 73 65 73 2e 20 20 54 68 65 20 76 65 72 79 cases. The very
d3960 0a 20 20 2a 2a 20 72 61 72 65 20 6c 61 72 67 65 . ** rare large
d3970 72 20 63 61 73 65 73 20 63 61 6e 20 62 65 20 68 r cases can be h
d3980 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 73 6c andled by the sl
d3990 6f 77 65 72 20 36 34 2d 62 69 74 20 76 61 72 69 ower 64-bit vari
d39a0 6e 74 0a 20 20 2a 2a 20 72 6f 75 74 69 6e 65 2e nt. ** routine.
d39b0 0a 20 20 2a 2f 0a 23 69 66 20 31 0a 20 20 7b 0a . */.#if 1. {.
d39c0 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20 u64 v64;.
d39d0 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d u8 n;.. p -=
d39e0 20 32 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 2;. n = sqli
d39f0 74 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 te3GetVarint(p,
d3a00 26 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 &v64);. asser
d3a10 74 28 20 6e 3e 33 20 26 26 20 6e 3c 3d 39 20 29 t( n>3 && n<=9 )
d3a20 3b 0a 20 20 20 20 69 66 28 20 28 76 36 34 20 26 ;. if( (v64 &
d3a30 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 SQLITE_MAX_U32)
d3a40 21 3d 76 36 34 20 29 7b 0a 20 20 20 20 20 20 2a !=v64 ){. *
d3a50 76 20 3d 20 30 78 66 66 66 66 66 66 66 66 3b 0a v = 0xffffffff;.
d3a60 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d3a70 20 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b 0a *v = (u32)v64;.
d3a80 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
d3a90 20 6e 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 20 n;. }..#else.
d3aa0 20 2f 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77 69 6e /* For followin
d3ab0 67 20 63 6f 64 65 20 28 6b 65 70 74 20 66 6f 72 g code (kept for
d3ac0 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 63 6f historical reco
d3ad0 72 64 20 6f 6e 6c 79 29 20 73 68 6f 77 73 20 61 rd only) shows a
d3ae0 6e 0a 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69 6e 67 n. ** unrolling
d3af0 20 66 6f 72 20 74 68 65 20 33 2d 20 61 6e 64 20 for the 3- and
d3b00 34 2d 62 79 74 65 20 76 61 72 69 6e 74 20 63 61 4-byte varint ca
d3b10 73 65 73 2e 20 20 54 68 69 73 20 63 6f 64 65 20 ses. This code
d3b20 69 73 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 is. ** slightly
d3b30 20 66 61 73 74 65 72 2c 20 62 75 74 20 69 74 20 faster, but it
d3b40 69 73 20 61 6c 73 6f 20 6c 61 72 67 65 72 20 61 is also larger a
d3b50 6e 64 20 6d 75 63 68 20 68 61 72 64 65 72 20 74 nd much harder t
d3b60 6f 20 74 65 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 o test.. */. p
d3b70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b ++;. b = b<<14;
d3b80 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a . b |= *p;. /*
d3b90 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 b: p1<<14 | p3
d3ba0 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
d3bb0 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 if (!(b&0x80)).
d3bc0 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 {. /* Values
d3bd0 20 62 65 74 77 65 65 6e 20 32 30 39 37 31 35 32 between 2097152
d3be0 20 61 6e 64 20 32 36 38 34 33 35 34 35 35 20 2a and 268435455 *
d3bf0 2f 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 /. b &= (0x7f
d3c00 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
d3c10 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 a &= (0x7f<<14
d3c20 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 )|(0x7f);. a
d3c30 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d = a<<7;. *v =
d3c40 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 a | b;. retu
d3c50 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b rn 4;. }.. p++
d3c60 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 ;. a = a<<14;.
d3c70 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 a |= *p;. /* a
d3c80 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 : p0<<28 | p2<<1
d3c90 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 4 | p4 (unmasked
d3ca0 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 ) */. if (!(a&0
d3cb0 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a x80)). {. /*
d3cc0 20 56 61 6c 75 65 73 20 20 62 65 74 77 65 65 6e Values between
d3cd0 20 32 36 38 34 33 35 34 35 36 20 61 6e 64 20 33 268435456 and 3
d3ce0 34 33 35 39 37 33 38 33 36 37 20 2a 2f 0a 20 20 4359738367 */.
d3cf0 20 20 61 20 26 3d 20 53 4c 4f 54 5f 34 5f 32 5f a &= SLOT_4_2_
d3d00 30 3b 0a 20 20 20 20 62 20 26 3d 20 53 4c 4f 54 0;. b &= SLOT
d3d10 5f 34 5f 32 5f 30 3b 0a 20 20 20 20 62 20 3d 20 _4_2_0;. b =
d3d20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 b<<7;. *v = a
d3d30 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e | b;. return
d3d40 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 5;. }.. /* We
d3d50 20 63 61 6e 20 6f 6e 6c 79 20 72 65 61 63 68 20 can only reach
d3d60 74 68 69 73 20 70 6f 69 6e 74 20 77 68 65 6e 20 this point when
d3d70 72 65 61 64 69 6e 67 20 61 20 63 6f 72 72 75 70 reading a corrup
d3d80 74 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 t database. **
d3d90 66 69 6c 65 2e 20 20 49 6e 20 74 68 61 74 20 63 file. In that c
d3da0 61 73 65 20 77 65 20 61 72 65 20 6e 6f 74 20 69 ase we are not i
d3db0 6e 20 61 6e 79 20 68 75 72 72 79 2e 20 20 55 73 n any hurry. Us
d3dc0 65 20 74 68 65 20 28 72 65 6c 61 74 69 76 65 6c e the (relativel
d3dd0 79 0a 20 20 2a 2a 20 73 6c 6f 77 29 20 67 65 6e y. ** slow) gen
d3de0 65 72 61 6c 2d 70 75 72 70 6f 73 65 20 73 71 6c eral-purpose sql
d3df0 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 29 20 ite3GetVarint()
d3e00 72 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72 61 routine to extra
d3e10 63 74 20 74 68 65 0a 20 20 2a 2a 20 76 61 6c 75 ct the. ** valu
d3e20 65 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 36 e. */. {. u6
d3e30 34 20 76 36 34 3b 0a 20 20 20 20 75 38 20 6e 3b 4 v64;. u8 n;
d3e40 0a 0a 20 20 20 20 70 20 2d 3d 20 34 3b 0a 20 20 .. p -= 4;.
d3e50 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 n = sqlite3Get
d3e60 56 61 72 69 6e 74 28 70 2c 20 26 76 36 34 29 3b Varint(p, &v64);
d3e70 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 35 . assert( n>5
d3e80 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20 20 20 && n<=9 );.
d3e90 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b 0a 20 *v = (u32)v64;.
d3ea0 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a 20 20 7d return n;. }
d3eb0 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
d3ec0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
d3ed0 65 72 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 er of bytes that
d3ee0 20 77 69 6c 6c 20 62 65 20 6e 65 65 64 65 64 20 will be needed
d3ef0 74 6f 20 73 74 6f 72 65 20 74 68 65 20 67 69 76 to store the giv
d3f00 65 6e 0a 2a 2a 20 36 34 2d 62 69 74 20 69 6e 74 en.** 64-bit int
d3f10 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f eger..*/.SQLITE_
d3f20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
d3f30 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 te3VarintLen(u64
d3f40 20 76 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 30 v){. int i = 0
d3f50 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 69 2b 2b 3b ;. do{. i++;
d3f60 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 . v >>= 7;.
d3f70 7d 77 68 69 6c 65 28 20 76 21 3d 30 20 26 26 20 }while( v!=0 &&
d3f80 41 4c 57 41 59 53 28 69 3c 39 29 20 29 3b 0a 20 ALWAYS(i<9) );.
d3f90 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 0a 2f return i;.}.../
d3fa0 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 69 *.** Read or wri
d3fb0 74 65 20 61 20 66 6f 75 72 2d 62 79 74 65 20 62 te a four-byte b
d3fc0 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 ig-endian intege
d3fd0 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 r value..*/.SQLI
d3fe0 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 TE_PRIVATE u32 s
d3ff0 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 63 qlite3Get4byte(c
d4000 6f 6e 73 74 20 75 38 20 2a 70 29 7b 0a 20 20 72 onst u8 *p){. r
d4010 65 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 32 34 29 eturn (p[0]<<24)
d4020 20 7c 20 28 70 5b 31 5d 3c 3c 31 36 29 20 7c 20 | (p[1]<<16) |
d4030 28 70 5b 32 5d 3c 3c 38 29 20 7c 20 70 5b 33 5d (p[2]<<8) | p[3]
d4040 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
d4050 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
d4060 75 74 34 62 79 74 65 28 75 6e 73 69 67 6e 65 64 ut4byte(unsigned
d4070 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29 char *p, u32 v)
d4080 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28 {. p[0] = (u8)(
d4090 76 3e 3e 32 34 29 3b 0a 20 20 70 5b 31 5d 20 3d v>>24);. p[1] =
d40a0 20 28 75 38 29 28 76 3e 3e 31 36 29 3b 0a 20 20 (u8)(v>>16);.
d40b0 70 5b 32 5d 20 3d 20 28 75 38 29 28 76 3e 3e 38 p[2] = (u8)(v>>8
d40c0 29 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 75 38 29 );. p[3] = (u8)
d40d0 76 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 v;.}..../*.** Tr
d40e0 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 anslate a single
d40f0 20 62 79 74 65 20 6f 66 20 48 65 78 20 69 6e 74 byte of Hex int
d4100 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a o an integer..**
d4110 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e This routine on
d4120 6c 79 20 77 6f 72 6b 73 20 69 66 20 68 20 72 65 ly works if h re
d4130 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20 ally is a valid
d4140 68 65 78 61 64 65 63 69 6d 61 6c 0a 2a 2a 20 63 hexadecimal.** c
d4150 68 61 72 61 63 74 65 72 3a 20 20 30 2e 2e 39 61 haracter: 0..9a
d4160 2e 2e 66 41 2e 2e 46 0a 2a 2f 0a 53 51 4c 49 54 ..fA..F.*/.SQLIT
d4170 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c E_PRIVATE u8 sql
d4180 69 74 65 33 48 65 78 54 6f 49 6e 74 28 69 6e 74 ite3HexToInt(int
d4190 20 68 29 7b 0a 20 20 61 73 73 65 72 74 28 20 28 h){. assert( (
d41a0 68 3e 3d 27 30 27 20 26 26 20 68 3c 3d 27 39 27 h>='0' && h<='9'
d41b0 29 20 7c 7c 20 20 28 68 3e 3d 27 61 27 20 26 26 ) || (h>='a' &&
d41c0 20 68 3c 3d 27 66 27 29 20 7c 7c 20 20 28 68 3e h<='f') || (h>
d41d0 3d 27 41 27 20 26 26 20 68 3c 3d 27 46 27 29 20 ='A' && h<='F')
d41e0 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 );.#ifdef SQLITE
d41f0 5f 41 53 43 49 49 0a 20 20 68 20 2b 3d 20 39 2a _ASCII. h += 9*
d4200 28 31 26 28 68 3e 3e 36 29 29 3b 0a 23 65 6e 64 (1&(h>>6));.#end
d4210 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 if.#ifdef SQLITE
d4220 5f 45 42 43 44 49 43 0a 20 20 68 20 2b 3d 20 39 _EBCDIC. h += 9
d4230 2a 28 31 26 7e 28 68 3e 3e 34 29 29 3b 0a 23 65 *(1&~(h>>4));.#e
d4240 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 28 75 ndif. return (u
d4250 38 29 28 68 20 26 20 30 78 66 29 3b 0a 7d 0a 0a 8)(h & 0xf);.}..
d4260 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
d4270 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 ITE_OMIT_BLOB_LI
d4280 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 TERAL) || define
d4290 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 d(SQLITE_HAS_COD
d42a0 45 43 29 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 EC)./*.** Conver
d42b0 74 20 61 20 42 4c 4f 42 20 6c 69 74 65 72 61 6c t a BLOB literal
d42c0 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78 27 of the form "x'
d42d0 68 68 68 68 68 68 27 22 20 69 6e 74 6f 20 69 74 hhhhhh'" into it
d42e0 73 20 62 69 6e 61 72 79 0a 2a 2a 20 76 61 6c 75 s binary.** valu
d42f0 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 e. Return a poi
d4300 6e 74 65 72 20 74 6f 20 69 74 73 20 62 69 6e 61 nter to its bina
d4310 72 79 20 76 61 6c 75 65 2e 20 20 53 70 61 63 65 ry value. Space
d4320 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 to hold the.**
d4330 62 69 6e 61 72 79 20 76 61 6c 75 65 20 68 61 73 binary value has
d4340 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 been obtained f
d4350 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d rom malloc and m
d4360 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 0a ust be freed by.
d4370 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 ** the calling r
d4380 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 outine..*/.SQLIT
d4390 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
d43a0 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 sqlite3HexToBlob
d43b0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f (sqlite3 *db, co
d43c0 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 nst char *z, int
d43d0 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 42 6c n){. char *zBl
d43e0 6f 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 ob;. int i;..
d43f0 7a 42 6c 6f 62 20 3d 20 28 63 68 61 72 20 2a 29 zBlob = (char *)
d4400 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
d4410 61 77 28 64 62 2c 20 6e 2f 32 20 2b 20 31 29 3b aw(db, n/2 + 1);
d4420 0a 20 20 6e 2d 2d 3b 0a 20 20 69 66 28 20 7a 42 . n--;. if( zB
d4430 6c 6f 62 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 lob ){. for(i
d4440 3d 30 3b 20 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0a =0; i<n; i+=2){.
d4450 20 20 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d zBlob[i/2]
d4460 20 3d 20 28 73 71 6c 69 74 65 33 48 65 78 54 6f = (sqlite3HexTo
d4470 49 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 Int(z[i])<<4) |
d4480 73 71 6c 69 74 65 33 48 65 78 54 6f 49 6e 74 28 sqlite3HexToInt(
d4490 7a 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 z[i+1]);. }.
d44a0 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 zBlob[i/2] =
d44b0 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
d44c0 7a 42 6c 6f 62 3b 0a 7d 0a 23 65 6e 64 69 66 20 zBlob;.}.#endif
d44d0 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* !SQLITE_OMIT_
d44e0 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 BLOB_LITERAL ||
d44f0 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
d4500 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 67 20 61 */../*.** Log a
d4510 6e 20 65 72 72 6f 72 20 74 68 61 74 20 69 73 20 n error that is
d4520 61 6e 20 41 50 49 20 63 61 6c 6c 20 6f 6e 20 61 an API call on a
d4530 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e connection poin
d4540 74 65 72 20 74 68 61 74 20 73 68 6f 75 6c 64 0a ter that should.
d4550 2a 2a 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e ** not have been
d4560 20 75 73 65 64 2e 20 20 54 68 65 20 22 74 79 70 used. The "typ
d4570 65 22 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e e" of connection
d4580 20 70 6f 69 6e 74 65 72 20 69 73 20 67 69 76 65 pointer is give
d4590 6e 20 61 73 20 74 68 65 0a 2a 2a 20 61 72 67 75 n as the.** argu
d45a0 6d 65 6e 74 2e 20 20 54 68 65 20 7a 54 79 70 65 ment. The zType
d45b0 20 69 73 20 61 20 77 6f 72 64 20 6c 69 6b 65 20 is a word like
d45c0 22 4e 55 4c 4c 22 20 6f 72 20 22 63 6c 6f 73 65 "NULL" or "close
d45d0 64 22 20 6f 72 20 22 69 6e 76 61 6c 69 64 22 2e d" or "invalid".
d45e0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
d45f0 6c 6f 67 42 61 64 43 6f 6e 6e 65 63 74 69 6f 6e logBadConnection
d4600 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 (const char *zTy
d4610 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6c pe){. sqlite3_l
d4620 6f 67 28 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 og(SQLITE_MISUSE
d4630 2c 20 0a 20 20 20 20 20 22 41 50 49 20 63 61 6c , . "API cal
d4640 6c 20 77 69 74 68 20 25 73 20 64 61 74 61 62 61 l with %s databa
d4650 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f se connection po
d4660 69 6e 74 65 72 22 2c 0a 20 20 20 20 20 7a 54 79 inter",. zTy
d4670 70 65 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pe. );.}../*.**
d4680 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 Check to make s
d4690 75 72 65 20 77 65 20 68 61 76 65 20 61 20 76 61 ure we have a va
d46a0 6c 69 64 20 64 62 20 70 6f 69 6e 74 65 72 2e 20 lid db pointer.
d46b0 20 54 68 69 73 20 74 65 73 74 20 69 73 20 6e 6f This test is no
d46c0 74 0a 2a 2a 20 66 6f 6f 6c 70 72 6f 6f 66 20 62 t.** foolproof b
d46d0 75 74 20 69 74 20 64 6f 65 73 20 70 72 6f 76 69 ut it does provi
d46e0 64 65 20 73 6f 6d 65 20 6d 65 61 73 75 72 65 20 de some measure
d46f0 6f 66 20 70 72 6f 74 65 63 74 69 6f 6e 20 61 67 of protection ag
d4700 61 69 6e 73 74 0a 2a 2a 20 6d 69 73 75 73 65 20 ainst.** misuse
d4710 6f 66 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 of the interface
d4720 20 73 75 63 68 20 61 73 20 70 61 73 73 69 6e 67 such as passing
d4730 20 69 6e 20 64 62 20 70 6f 69 6e 74 65 72 73 20 in db pointers
d4740 74 68 61 74 20 61 72 65 0a 2a 2a 20 4e 55 4c 4c that are.** NULL
d4750 20 6f 72 20 77 68 69 63 68 20 68 61 76 65 20 62 or which have b
d4760 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 63 een previously c
d4770 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69 73 20 losed. If this
d4780 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 0a routine returns.
d4790 2a 2a 20 31 20 69 74 20 6d 65 61 6e 73 20 74 68 ** 1 it means th
d47a0 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74 65 at the db pointe
d47b0 72 20 69 73 20 76 61 6c 69 64 20 61 6e 64 20 30 r is valid and 0
d47c0 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6e 6f if it should no
d47d0 74 20 62 65 0a 2a 2a 20 64 65 72 65 66 65 72 65 t be.** derefere
d47e0 6e 63 65 64 20 66 6f 72 20 61 6e 79 20 72 65 61 nced for any rea
d47f0 73 6f 6e 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e son. The callin
d4800 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c g function shoul
d4810 64 20 69 6e 76 6f 6b 65 0a 2a 2a 20 53 51 4c 49 d invoke.** SQLI
d4820 54 45 5f 4d 49 53 55 53 45 20 69 6d 6d 65 64 69 TE_MISUSE immedi
d4830 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c ately..**.** sql
d4840 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f ite3SafetyCheckO
d4850 6b 28 29 20 72 65 71 75 69 72 65 73 20 74 68 61 k() requires tha
d4860 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74 65 72 t the db pointer
d4870 20 62 65 20 76 61 6c 69 64 20 66 6f 72 0a 2a 2a be valid for.**
d4880 20 75 73 65 2e 20 20 73 71 6c 69 74 65 33 53 61 use. sqlite3Sa
d4890 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f fetyCheckSickOrO
d48a0 6b 28 29 20 61 6c 6c 6f 77 73 20 61 20 64 62 20 k() allows a db
d48b0 70 6f 69 6e 74 65 72 20 74 68 61 74 20 66 61 69 pointer that fai
d48c0 6c 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 6e 20 70 led to.** open p
d48d0 72 6f 70 65 72 6c 79 20 61 6e 64 20 69 73 20 6e roperly and is n
d48e0 6f 74 20 66 69 74 20 66 6f 72 20 67 65 6e 65 72 ot fit for gener
d48f0 61 6c 20 75 73 65 20 62 75 74 20 77 68 69 63 68 al use but which
d4900 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 20 can be.** used
d4910 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 as an argument t
d4920 6f 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 o sqlite3_errmsg
d4930 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6c () or sqlite3_cl
d4940 6f 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ose()..*/.SQLITE
d4950 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
d4960 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f ite3SafetyCheckO
d4970 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a k(sqlite3 *db){.
d4980 20 20 75 33 32 20 6d 61 67 69 63 3b 0a 20 20 69 u32 magic;. i
d4990 66 28 20 64 62 3d 3d 30 20 29 7b 0a 20 20 20 20 f( db==0 ){.
d49a0 6c 6f 67 42 61 64 43 6f 6e 6e 65 63 74 69 6f 6e logBadConnection
d49b0 28 22 4e 55 4c 4c 22 29 3b 0a 20 20 20 20 72 65 ("NULL");. re
d49c0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6d 61 turn 0;. }. ma
d49d0 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b gic = db->magic;
d49e0 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 . if( magic!=SQ
d49f0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 LITE_MAGIC_OPEN
d4a00 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
d4a10 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 e3SafetyCheckSic
d4a20 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 kOrOk(db) ){.
d4a30 20 20 20 74 65 73 74 63 61 73 65 28 20 73 71 6c testcase( sql
d4a40 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
d4a50 2e 78 4c 6f 67 21 3d 30 20 29 3b 0a 20 20 20 20 .xLog!=0 );.
d4a60 20 20 6c 6f 67 42 61 64 43 6f 6e 6e 65 63 74 69 logBadConnecti
d4a70 6f 6e 28 22 75 6e 6f 70 65 6e 65 64 22 29 3b 0a on("unopened");.
d4a80 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
d4a90 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
d4aa0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d return 1;. }.}
d4ab0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
d4ac0 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 int sqlite3Safet
d4ad0 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 yCheckSickOrOk(s
d4ae0 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 qlite3 *db){. u
d4af0 33 32 20 6d 61 67 69 63 3b 0a 20 20 6d 61 67 69 32 magic;. magi
d4b00 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 c = db->magic;.
d4b10 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 4c 49 if( magic!=SQLI
d4b20 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 26 26 TE_MAGIC_SICK &&
d4b30 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51 . magic!=SQ
d4b40 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 LITE_MAGIC_OPEN
d4b50 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d &&. magic!=
d4b60 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 SQLITE_MAGIC_BUS
d4b70 59 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73 Y ){. testcas
d4b80 65 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c e( sqlite3Global
d4b90 43 6f 6e 66 69 67 2e 78 4c 6f 67 21 3d 30 20 29 Config.xLog!=0 )
d4ba0 3b 0a 20 20 20 20 6c 6f 67 42 61 64 43 6f 6e 6e ;. logBadConn
d4bb0 65 63 74 69 6f 6e 28 22 69 6e 76 61 6c 69 64 22 ection("invalid"
d4bc0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
d4bd0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
d4be0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f turn 1;. }.}../
d4bf0 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 *.** Attempt to
d4c00 61 64 64 2c 20 73 75 62 73 74 72 61 63 74 2c 20 add, substract,
d4c10 6f 72 20 6d 75 6c 74 69 70 6c 79 20 74 68 65 20 or multiply the
d4c20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 76 61 64-bit signed va
d4c30 6c 75 65 20 69 42 20 61 67 61 69 6e 73 74 0a 2a lue iB against.*
d4c40 2a 20 74 68 65 20 6f 74 68 65 72 20 36 34 2d 62 * the other 64-b
d4c50 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 it signed intege
d4c60 72 20 61 74 20 2a 70 41 20 61 6e 64 20 73 74 6f r at *pA and sto
d4c70 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e re the result in
d4c80 20 2a 70 41 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 *pA..** Return
d4c90 30 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 4f 0 on success. O
d4ca0 72 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 69 r if the operati
d4cb0 6f 6e 20 77 6f 75 6c 64 20 68 61 76 65 20 72 65 on would have re
d4cc0 73 75 6c 74 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 sulted in an.**
d4cd0 6f 76 65 72 66 6c 6f 77 2c 20 6c 65 61 76 65 20 overflow, leave
d4ce0 2a 70 41 20 75 6e 63 68 61 6e 67 65 64 20 61 6e *pA unchanged an
d4cf0 64 20 72 65 74 75 72 6e 20 31 2e 0a 2a 2f 0a 53 d return 1..*/.S
d4d00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
d4d10 74 20 73 71 6c 69 74 65 33 41 64 64 49 6e 74 36 t sqlite3AddInt6
d4d20 34 28 69 36 34 20 2a 70 41 2c 20 69 36 34 20 69 4(i64 *pA, i64 i
d4d30 42 29 7b 0a 20 20 69 36 34 20 69 41 20 3d 20 2a B){. i64 iA = *
d4d40 70 41 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 pA;. testcase(
d4d50 69 41 3d 3d 30 20 29 3b 20 74 65 73 74 63 61 73 iA==0 ); testcas
d4d60 65 28 20 69 41 3d 3d 31 20 29 3b 0a 20 20 74 65 e( iA==1 );. te
d4d70 73 74 63 61 73 65 28 20 69 42 3d 3d 2d 31 20 29 stcase( iB==-1 )
d4d80 3b 20 74 65 73 74 63 61 73 65 28 20 69 42 3d 3d ; testcase( iB==
d4d90 30 20 29 3b 0a 20 20 69 66 28 20 69 42 3e 3d 30 0 );. if( iB>=0
d4da0 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65 ){. testcase
d4db0 28 20 69 41 3e 30 20 26 26 20 4c 41 52 47 45 53 ( iA>0 && LARGES
d4dc0 54 5f 49 4e 54 36 34 20 2d 20 69 41 20 3d 3d 20 T_INT64 - iA ==
d4dd0 69 42 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 iB );. testca
d4de0 73 65 28 20 69 41 3e 30 20 26 26 20 4c 41 52 47 se( iA>0 && LARG
d4df0 45 53 54 5f 49 4e 54 36 34 20 2d 20 69 41 20 3d EST_INT64 - iA =
d4e00 3d 20 69 42 20 2d 20 31 20 29 3b 0a 20 20 20 20 = iB - 1 );.
d4e10 69 66 28 20 69 41 3e 30 20 26 26 20 4c 41 52 47 if( iA>0 && LARG
d4e20 45 53 54 5f 49 4e 54 36 34 20 2d 20 69 41 20 3c EST_INT64 - iA <
d4e30 20 69 42 20 29 20 72 65 74 75 72 6e 20 31 3b 0a iB ) return 1;.
d4e40 20 20 20 20 2a 70 41 20 2b 3d 20 69 42 3b 0a 20 *pA += iB;.
d4e50 20 7d 65 6c 73 65 7b 0a 20 20 20 20 74 65 73 74 }else{. test
d4e60 63 61 73 65 28 20 69 41 3c 30 20 26 26 20 2d 28 case( iA<0 && -(
d4e70 69 41 20 2b 20 4c 41 52 47 45 53 54 5f 49 4e 54 iA + LARGEST_INT
d4e80 36 34 29 20 3d 3d 20 69 42 20 2b 20 31 20 29 3b 64) == iB + 1 );
d4e90 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 . testcase( i
d4ea0 41 3c 30 20 26 26 20 2d 28 69 41 20 2b 20 4c 41 A<0 && -(iA + LA
d4eb0 52 47 45 53 54 5f 49 4e 54 36 34 29 20 3d 3d 20 RGEST_INT64) ==
d4ec0 69 42 20 2b 20 32 20 29 3b 0a 20 20 20 20 69 66 iB + 2 );. if
d4ed0 28 20 69 41 3c 30 20 26 26 20 2d 28 69 41 20 2b ( iA<0 && -(iA +
d4ee0 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 20 LARGEST_INT64)
d4ef0 3e 20 69 42 20 2b 20 31 20 29 20 72 65 74 75 72 > iB + 1 ) retur
d4f00 6e 20 31 3b 0a 20 20 20 20 2a 70 41 20 2b 3d 20 n 1;. *pA +=
d4f10 69 42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e iB;. }. return
d4f20 20 30 3b 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 0; .}.SQLITE_PR
d4f30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
d4f40 33 53 75 62 49 6e 74 36 34 28 69 36 34 20 2a 70 3SubInt64(i64 *p
d4f50 41 2c 20 69 36 34 20 69 42 29 7b 0a 20 20 74 65 A, i64 iB){. te
d4f60 73 74 63 61 73 65 28 20 69 42 3d 3d 53 4d 41 4c stcase( iB==SMAL
d4f70 4c 45 53 54 5f 49 4e 54 36 34 2b 31 20 29 3b 0a LEST_INT64+1 );.
d4f80 20 20 69 66 28 20 69 42 3d 3d 53 4d 41 4c 4c 45 if( iB==SMALLE
d4f90 53 54 5f 49 4e 54 36 34 20 29 7b 0a 20 20 20 20 ST_INT64 ){.
d4fa0 74 65 73 74 63 61 73 65 28 20 28 2a 70 41 29 3d testcase( (*pA)=
d4fb0 3d 28 2d 31 29 20 29 3b 20 74 65 73 74 63 61 73 =(-1) ); testcas
d4fc0 65 28 20 28 2a 70 41 29 3d 3d 30 20 29 3b 0a 20 e( (*pA)==0 );.
d4fd0 20 20 20 69 66 28 20 28 2a 70 41 29 3e 3d 30 20 if( (*pA)>=0
d4fe0 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 ) return 1;.
d4ff0 2a 70 41 20 2d 3d 20 69 42 3b 0a 20 20 20 20 72 *pA -= iB;. r
d5000 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 eturn 0;. }else
d5010 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c {. return sql
d5020 69 74 65 33 41 64 64 49 6e 74 36 34 28 70 41 2c ite3AddInt64(pA,
d5030 20 2d 69 42 29 3b 0a 20 20 7d 0a 7d 0a 23 64 65 -iB);. }.}.#de
d5040 66 69 6e 65 20 54 57 4f 50 4f 57 45 52 33 32 20 fine TWOPOWER32
d5050 28 28 28 69 36 34 29 31 29 3c 3c 33 32 29 0a 23 (((i64)1)<<32).#
d5060 64 65 66 69 6e 65 20 54 57 4f 50 4f 57 45 52 33 define TWOPOWER3
d5070 31 20 28 28 28 69 36 34 29 31 29 3c 3c 33 31 29 1 (((i64)1)<<31)
d5080 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
d5090 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 6c 49 6e int sqlite3MulIn
d50a0 74 36 34 28 69 36 34 20 2a 70 41 2c 20 69 36 34 t64(i64 *pA, i64
d50b0 20 69 42 29 7b 0a 20 20 69 36 34 20 69 41 20 3d iB){. i64 iA =
d50c0 20 2a 70 41 3b 0a 20 20 69 36 34 20 69 41 31 2c *pA;. i64 iA1,
d50d0 20 69 41 30 2c 20 69 42 31 2c 20 69 42 30 2c 20 iA0, iB1, iB0,
d50e0 72 3b 0a 0a 20 20 69 41 31 20 3d 20 69 41 2f 54 r;.. iA1 = iA/T
d50f0 57 4f 50 4f 57 45 52 33 32 3b 0a 20 20 69 41 30 WOPOWER32;. iA0
d5100 20 3d 20 69 41 20 25 20 54 57 4f 50 4f 57 45 52 = iA % TWOPOWER
d5110 33 32 3b 0a 20 20 69 42 31 20 3d 20 69 42 2f 54 32;. iB1 = iB/T
d5120 57 4f 50 4f 57 45 52 33 32 3b 0a 20 20 69 42 30 WOPOWER32;. iB0
d5130 20 3d 20 69 42 20 25 20 54 57 4f 50 4f 57 45 52 = iB % TWOPOWER
d5140 33 32 3b 0a 20 20 69 66 28 20 69 41 31 2a 69 42 32;. if( iA1*iB
d5150 31 20 21 3d 20 30 20 29 20 72 65 74 75 72 6e 20 1 != 0 ) return
d5160 31 3b 0a 20 20 61 73 73 65 72 74 28 20 69 41 31 1;. assert( iA1
d5170 2a 69 42 30 3d 3d 30 20 7c 7c 20 69 41 30 2a 69 *iB0==0 || iA0*i
d5180 42 31 3d 3d 30 20 29 3b 0a 20 20 72 20 3d 20 69 B1==0 );. r = i
d5190 41 31 2a 69 42 30 20 2b 20 69 41 30 2a 69 42 31 A1*iB0 + iA0*iB1
d51a0 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 72 3d ;. testcase( r=
d51b0 3d 28 2d 54 57 4f 50 4f 57 45 52 33 31 29 2d 31 =(-TWOPOWER31)-1
d51c0 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
d51d0 72 3d 3d 28 2d 54 57 4f 50 4f 57 45 52 33 31 29 r==(-TWOPOWER31)
d51e0 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
d51f0 72 3d 3d 54 57 4f 50 4f 57 45 52 33 31 20 29 3b r==TWOPOWER31 );
d5200 0a 20 20 74 65 73 74 63 61 73 65 28 20 72 3d 3d . testcase( r==
d5210 54 57 4f 50 4f 57 45 52 33 31 2d 31 20 29 3b 0a TWOPOWER31-1 );.
d5220 20 20 69 66 28 20 72 3c 28 2d 54 57 4f 50 4f 57 if( r<(-TWOPOW
d5230 45 52 33 31 29 20 7c 7c 20 72 3e 3d 54 57 4f 50 ER31) || r>=TWOP
d5240 4f 57 45 52 33 31 20 29 20 72 65 74 75 72 6e 20 OWER31 ) return
d5250 31 3b 0a 20 20 72 20 2a 3d 20 54 57 4f 50 4f 57 1;. r *= TWOPOW
d5260 45 52 33 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 ER32;. if( sqli
d5270 74 65 33 41 64 64 49 6e 74 36 34 28 26 72 2c 20 te3AddInt64(&r,
d5280 69 41 30 2a 69 42 30 29 20 29 20 72 65 74 75 72 iA0*iB0) ) retur
d5290 6e 20 31 3b 0a 20 20 2a 70 41 20 3d 20 72 3b 0a n 1;. *pA = r;.
d52a0 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
d52b0 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 *.** Compute the
d52c0 20 61 62 73 6f 6c 75 74 65 20 76 61 6c 75 65 20 absolute value
d52d0 6f 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e of a 32-bit sign
d52e0 65 64 20 69 6e 74 65 67 65 72 2c 20 6f 66 20 70 ed integer, of p
d52f0 6f 73 73 69 62 6c 65 2e 20 20 4f 72 20 0a 2a 2a ossible. Or .**
d5300 20 69 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 if the integer
d5310 68 61 73 20 61 20 76 61 6c 75 65 20 6f 66 20 2d has a value of -
d5320 32 31 34 37 34 38 33 36 34 38 2c 20 72 65 74 75 2147483648, retu
d5330 72 6e 20 2b 32 31 34 37 34 38 33 36 34 37 0a 2a rn +2147483647.*
d5340 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
d5350 20 69 6e 74 20 73 71 6c 69 74 65 33 41 62 73 49 int sqlite3AbsI
d5360 6e 74 33 32 28 69 6e 74 20 78 29 7b 0a 20 20 69 nt32(int x){. i
d5370 66 28 20 78 3e 3d 30 20 29 20 72 65 74 75 72 6e f( x>=0 ) return
d5380 20 78 3b 0a 20 20 69 66 28 20 78 3d 3d 28 69 6e x;. if( x==(in
d5390 74 29 30 78 38 30 30 30 30 30 30 30 20 29 20 72 t)0x80000000 ) r
d53a0 65 74 75 72 6e 20 30 78 37 66 66 66 66 66 66 66 eturn 0x7fffffff
d53b0 3b 0a 20 20 72 65 74 75 72 6e 20 2d 78 3b 0a 7d ;. return -x;.}
d53c0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
d53d0 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41 4d 45 53 ENABLE_8_3_NAMES
d53e0 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 ./*.** If SQLITE
d53f0 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41 4d 45 _ENABLE_8_3_NAME
d5400 53 20 69 73 20 73 65 74 20 61 74 20 63 6f 6d 70 S is set at comp
d5410 69 6c 65 2d 74 69 6d 65 20 61 6e 64 20 69 66 20 ile-time and if
d5420 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
d5430 66 69 6c 65 6e 61 6d 65 20 69 6e 20 7a 42 61 73 filename in zBas
d5440 65 46 69 6c 65 6e 61 6d 65 20 69 73 20 61 20 55 eFilename is a U
d5450 52 49 20 77 69 74 68 20 74 68 65 20 22 38 5f 33 RI with the "8_3
d5460 5f 6e 61 6d 65 73 3d 31 22 20 70 61 72 61 6d 65 _names=1" parame
d5470 74 65 72 20 61 6e 64 0a 2a 2a 20 69 66 20 66 69 ter and.** if fi
d5480 6c 65 6e 61 6d 65 20 69 6e 20 7a 5b 5d 20 68 61 lename in z[] ha
d5490 73 20 61 20 73 75 66 66 69 78 20 28 61 2e 6b 2e s a suffix (a.k.
d54a0 61 2e 20 22 65 78 74 65 6e 73 69 6f 6e 22 29 20 a. "extension")
d54b0 74 68 61 74 20 69 73 20 6c 6f 6e 67 65 72 20 74 that is longer t
d54c0 68 61 6e 0a 2a 2a 20 74 68 72 65 65 20 63 68 61 han.** three cha
d54d0 72 61 63 74 65 72 73 2c 20 74 68 65 6e 20 73 68 racters, then sh
d54e0 6f 72 74 65 6e 20 74 68 65 20 73 75 66 66 69 78 orten the suffix
d54f0 20 6f 6e 20 7a 5b 5d 20 74 6f 20 62 65 20 74 68 on z[] to be th
d5500 65 20 6c 61 73 74 20 74 68 72 65 65 0a 2a 2a 20 e last three.**
d5510 63 68 61 72 61 63 74 65 72 73 20 6f 66 20 74 68 characters of th
d5520 65 20 6f 72 69 67 69 6e 61 6c 20 73 75 66 66 69 e original suffi
d5530 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 x..**.** If SQLI
d5540 54 45 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41 TE_ENABLE_8_3_NA
d5550 4d 45 53 20 69 73 20 73 65 74 20 74 6f 20 32 20 MES is set to 2
d5560 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2c at compile-time,
d5570 20 74 68 65 6e 20 61 6c 77 61 79 73 0a 2a 2a 20 then always.**
d5580 64 6f 20 74 68 65 20 73 75 66 66 69 78 20 73 68 do the suffix sh
d5590 6f 72 74 65 6e 69 6e 67 20 72 65 67 61 72 64 6c ortening regardl
d55a0 65 73 73 20 6f 66 20 55 52 49 20 70 61 72 61 6d ess of URI param
d55b0 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d eter..**.** Exam
d55c0 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ples:.**.**
d55d0 74 65 73 74 2e 64 62 2d 6a 6f 75 72 6e 61 6c 20 test.db-journal
d55e0 20 20 20 3d 3e 20 20 20 74 65 73 74 2e 6e 61 6c => test.nal
d55f0 0a 2a 2a 20 20 20 20 20 74 65 73 74 2e 64 62 2d .** test.db-
d5600 77 61 6c 20 20 20 20 20 20 20 20 3d 3e 20 20 20 wal =>
d5610 74 65 73 74 2e 77 61 6c 0a 2a 2a 20 20 20 20 20 test.wal.**
d5620 74 65 73 74 2e 64 62 2d 73 68 6d 20 20 20 20 20 test.db-shm
d5630 20 20 20 3d 3e 20 20 20 74 65 73 74 2e 73 68 6d => test.shm
d5640 0a 2a 2a 20 20 20 20 20 74 65 73 74 2e 64 62 2d .** test.db-
d5650 6d 6a 37 66 33 33 31 39 66 61 20 3d 3e 20 20 20 mj7f3319fa =>
d5660 74 65 73 74 2e 39 66 61 0a 2a 2f 0a 53 51 4c 49 test.9fa.*/.SQLI
d5670 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
d5680 73 71 6c 69 74 65 33 46 69 6c 65 53 75 66 66 69 sqlite3FileSuffi
d5690 78 33 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a x3(const char *z
d56a0 42 61 73 65 46 69 6c 65 6e 61 6d 65 2c 20 63 68 BaseFilename, ch
d56b0 61 72 20 2a 7a 29 7b 0a 23 69 66 20 53 51 4c 49 ar *z){.#if SQLI
d56c0 54 45 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41 TE_ENABLE_8_3_NA
d56d0 4d 45 53 3c 32 0a 20 20 69 66 28 20 73 71 6c 69 MES<2. if( sqli
d56e0 74 65 33 5f 75 72 69 5f 62 6f 6f 6c 65 61 6e 28 te3_uri_boolean(
d56f0 7a 42 61 73 65 46 69 6c 65 6e 61 6d 65 2c 20 22 zBaseFilename, "
d5700 38 5f 33 5f 6e 61 6d 65 73 22 2c 20 30 29 20 29 8_3_names", 0) )
d5710 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 .#endif. {.
d5720 69 6e 74 20 69 2c 20 73 7a 3b 0a 20 20 20 20 73 int i, sz;. s
d5730 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 z = sqlite3Strle
d5740 6e 33 30 28 7a 29 3b 0a 20 20 20 20 66 6f 72 28 n30(z);. for(
d5750 69 3d 73 7a 2d 31 3b 20 69 3e 30 20 26 26 20 7a i=sz-1; i>0 && z
d5760 5b 69 5d 21 3d 27 2f 27 20 26 26 20 7a 5b 69 5d [i]!='/' && z[i]
d5770 21 3d 27 2e 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20 !='.'; i--){}.
d5780 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2e 27 20 if( z[i]=='.'
d5790 26 26 20 41 4c 57 41 59 53 28 73 7a 3e 69 2b 34 && ALWAYS(sz>i+4
d57a0 29 20 29 20 6d 65 6d 6d 6f 76 65 28 26 7a 5b 69 ) ) memmove(&z[i
d57b0 2b 31 5d 2c 20 26 7a 5b 73 7a 2d 33 5d 2c 20 34 +1], &z[sz-3], 4
d57c0 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a );. }.}.#endif.
d57d0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
d57e0 20 45 6e 64 20 6f 66 20 75 74 69 6c 2e 63 20 2a End of util.c *
d57f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
d5820 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
d5830 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 68 Begin file hash
d5840 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
d5850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
d5870 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
d5880 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 ember 22.**.** T
d5890 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
d58a0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
d58b0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
d58c0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
d58d0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
d58e0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
d58f0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
d5900 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
d5910 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
d5920 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
d5930 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
d5940 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
d5950 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
d5960 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
d5970 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
d5980 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
d5990 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
d59a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d59b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d59c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d59d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d59e0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
d59f0 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 is the implement
d5a00 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69 63 ation of generic
d5a10 20 68 61 73 68 2d 74 61 62 6c 65 73 0a 2a 2a 20 hash-tables.**
d5a20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a used in SQLite..
d5a30 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c */./* #include <
d5a40 61 73 73 65 72 74 2e 68 3e 20 2a 2f 0a 0a 2f 2a assert.h> */../*
d5a50 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 Turn bulk memor
d5a60 79 20 69 6e 74 6f 20 61 20 68 61 73 68 20 74 61 y into a hash ta
d5a70 62 6c 65 20 6f 62 6a 65 63 74 20 62 79 20 69 6e ble object by in
d5a80 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 2a itializing the.*
d5a90 2a 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 * fields of the
d5aa0 48 61 73 68 20 73 74 72 75 63 74 75 72 65 2e 0a Hash structure..
d5ab0 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 69 73 20 **.** "pNew" is
d5ac0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
d5ad0 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 hash table that
d5ae0 20 69 73 20 74 6f 20 62 65 20 69 6e 69 74 69 61 is to be initia
d5af0 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 lized..*/.SQLITE
d5b00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
d5b10 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 48 61 lite3HashInit(Ha
d5b20 73 68 20 2a 70 4e 65 77 29 7b 0a 20 20 61 73 73 sh *pNew){. ass
d5b30 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a ert( pNew!=0 );.
d5b40 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 20 3d 20 pNew->first =
d5b50 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75 6e 74 0;. pNew->count
d5b60 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 = 0;. pNew->ht
d5b70 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e 65 77 size = 0;. pNew
d5b80 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 ->ht = 0;.}../*
d5b90 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74 72 69 Remove all entri
d5ba0 65 73 20 66 72 6f 6d 20 61 20 68 61 73 68 20 74 es from a hash t
d5bb0 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d 20 61 able. Reclaim a
d5bc0 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 43 61 ll memory..** Ca
d5bd0 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ll this routine
d5be0 74 6f 20 64 65 6c 65 74 65 20 61 20 68 61 73 68 to delete a hash
d5bf0 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 72 65 73 table or to res
d5c00 65 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a et a hash table.
d5c10 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 74 79 20 ** to the empty
d5c20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 state..*/.SQLITE
d5c30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
d5c40 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48 lite3HashClear(H
d5c50 61 73 68 20 2a 70 48 29 7b 0a 20 20 48 61 73 68 ash *pH){. Hash
d5c60 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 Elem *elem;
d5c70 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 /* For loopi
d5c80 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d ng over all elem
d5c90 65 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c ents of the tabl
d5ca0 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 e */.. assert(
d5cb0 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 pH!=0 );. elem
d5cc0 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70 = pH->first;. p
d5cd0 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 H->first = 0;.
d5ce0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d sqlite3_free(pH-
d5cf0 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d >ht);. pH->ht =
d5d00 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 0;. pH->htsize
d5d10 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65 = 0;. while( e
d5d20 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61 73 68 45 lem ){. HashE
d5d30 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d 20 3d lem *next_elem =
d5d40 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 elem->next;.
d5d50 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 65 6c sqlite3_free(el
d5d60 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 em);. elem =
d5d70 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 next_elem;. }.
d5d80 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a pH->count = 0;.
d5d90 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 61 73 }../*.** The has
d5da0 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a hing function..*
d5db0 2f 0a 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 /.static unsigne
d5dc0 64 20 69 6e 74 20 73 74 72 48 61 73 68 28 63 6f d int strHash(co
d5dd0 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 nst char *z, int
d5de0 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 20 nKey){. int h
d5df0 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 6e = 0;. assert( n
d5e00 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 77 68 69 6c Key>=0 );. whil
d5e10 65 28 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a e( nKey > 0 ){.
d5e20 20 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e h = (h<<3) ^
d5e30 20 68 20 5e 20 73 71 6c 69 74 65 33 55 70 70 65 h ^ sqlite3Uppe
d5e40 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e rToLower[(unsign
d5e50 65 64 20 63 68 61 72 29 2a 7a 2b 2b 5d 3b 0a 20 ed char)*z++];.
d5e60 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d 0a 20 nKey--;. }.
d5e70 20 72 65 74 75 72 6e 20 68 3b 0a 7d 0a 0a 0a 2f return h;.}.../
d5e80 2a 20 4c 69 6e 6b 20 70 4e 65 77 20 65 6c 65 6d * Link pNew elem
d5e90 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 ent into the has
d5ea0 68 20 74 61 62 6c 65 20 70 48 2e 20 20 49 66 20 h table pH. If
d5eb0 70 45 6e 74 72 79 21 3d 30 20 74 68 65 6e 20 61 pEntry!=0 then a
d5ec0 6c 73 6f 0a 2a 2a 20 69 6e 73 65 72 74 20 70 4e lso.** insert pN
d5ed0 65 77 20 69 6e 74 6f 20 74 68 65 20 70 45 6e 74 ew into the pEnt
d5ee0 72 79 20 68 61 73 68 20 62 75 63 6b 65 74 2e 0a ry hash bucket..
d5ef0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 */.static void i
d5f00 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a 20 20 nsertElement(.
d5f10 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 Hash *pH,
d5f20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f /* The co
d5f30 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c mplete hash tabl
d5f40 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 e */. struct _h
d5f50 74 20 2a 70 45 6e 74 72 79 2c 20 20 20 20 2f 2a t *pEntry, /*
d5f60 20 54 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20 The entry into
d5f70 77 68 69 63 68 20 70 4e 65 77 20 69 73 20 69 6e which pNew is in
d5f80 73 65 72 74 65 64 20 2a 2f 0a 20 20 48 61 73 68 serted */. Hash
d5f90 45 6c 65 6d 20 2a 70 4e 65 77 20 20 20 20 20 20 Elem *pNew
d5fa0 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e /* The elemen
d5fb0 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 t to be inserted
d5fc0 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 68 45 6c 65 */.){. HashEle
d5fd0 6d 20 2a 70 48 65 61 64 3b 20 20 20 20 20 20 20 m *pHead;
d5fe0 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 /* First element
d5ff0 20 61 6c 72 65 61 64 79 20 69 6e 20 70 45 6e 74 already in pEnt
d6000 72 79 20 2a 2f 0a 20 20 69 66 28 20 70 45 6e 74 ry */. if( pEnt
d6010 72 79 20 29 7b 0a 20 20 20 20 70 48 65 61 64 20 ry ){. pHead
d6020 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 20 = pEntry->count
d6030 3f 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 ? pEntry->chain
d6040 3a 20 30 3b 0a 20 20 20 20 70 45 6e 74 72 79 2d : 0;. pEntry-
d6050 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 20 20 70 45 >count++;. pE
d6060 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 70 4e ntry->chain = pN
d6070 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ew;. }else{.
d6080 20 70 48 65 61 64 20 3d 20 30 3b 0a 20 20 7d 0a pHead = 0;. }.
d6090 20 20 69 66 28 20 70 48 65 61 64 20 29 7b 0a 20 if( pHead ){.
d60a0 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 pNew->next =
d60b0 70 48 65 61 64 3b 0a 20 20 20 20 70 4e 65 77 2d pHead;. pNew-
d60c0 3e 70 72 65 76 20 3d 20 70 48 65 61 64 2d 3e 70 >prev = pHead->p
d60d0 72 65 76 3b 0a 20 20 20 20 69 66 28 20 70 48 65 rev;. if( pHe
d60e0 61 64 2d 3e 70 72 65 76 20 29 7b 20 70 48 65 61 ad->prev ){ pHea
d60f0 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 d->prev->next =
d6100 70 4e 65 77 3b 20 7d 0a 20 20 20 20 65 6c 73 65 pNew; }. else
d6110 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 70 { p
d6120 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b H->first = pNew;
d6130 20 7d 0a 20 20 20 20 70 48 65 61 64 2d 3e 70 72 }. pHead->pr
d6140 65 76 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c ev = pNew;. }el
d6150 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 se{. pNew->ne
d6160 78 74 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a xt = pH->first;.
d6170 20 20 20 20 69 66 28 20 70 48 2d 3e 66 69 72 73 if( pH->firs
d6180 74 20 29 7b 20 70 48 2d 3e 66 69 72 73 74 2d 3e t ){ pH->first->
d6190 70 72 65 76 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 prev = pNew; }.
d61a0 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 pNew->prev =
d61b0 30 3b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 0;. pH->first
d61c0 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a 0a = pNew;. }.}..
d61d0 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 65 20 68 ./* Resize the h
d61e0 61 73 68 20 74 61 62 6c 65 20 73 6f 20 74 68 61 ash table so tha
d61f0 74 20 69 74 20 63 61 6e 74 61 69 6e 73 20 22 6e t it cantains "n
d6200 65 77 5f 73 69 7a 65 22 20 62 75 63 6b 65 74 73 ew_size" buckets
d6210 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 73 68 ..**.** The hash
d6220 20 74 61 62 6c 65 20 6d 69 67 68 74 20 66 61 69 table might fai
d6230 6c 20 74 6f 20 72 65 73 69 7a 65 20 69 66 20 73 l to resize if s
d6240 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
d6250 66 61 69 6c 73 20 6f 72 0a 2a 2a 20 69 66 20 74 fails or.** if t
d6260 68 65 20 6e 65 77 20 73 69 7a 65 20 69 73 20 74 he new size is t
d6270 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 70 he same as the p
d6280 72 69 6f 72 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 rior size..** Re
d6290 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 turn TRUE if the
d62a0 20 72 65 73 69 7a 65 20 6f 63 63 75 72 73 20 61 resize occurs a
d62b0 6e 64 20 66 61 6c 73 65 20 69 66 20 6e 6f 74 2e nd false if not.
d62c0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
d62d0 65 68 61 73 68 28 48 61 73 68 20 2a 70 48 2c 20 ehash(Hash *pH,
d62e0 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 65 77 unsigned int new
d62f0 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 63 74 _size){. struct
d6300 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20 _ht *new_ht;
d6310 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
d6320 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65 20 2a new hash table *
d6330 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c /. HashElem *el
d6340 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20 em, *next_elem;
d6350 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e /* For loopin
d6360 67 20 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 g over existing
d6370 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a 23 69 66 elements */..#if
d6380 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 SQLITE_MALLOC_S
d6390 4f 46 54 5f 4c 49 4d 49 54 3e 30 0a 20 20 69 66 OFT_LIMIT>0. if
d63a0 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f ( new_size*sizeo
d63b0 66 28 73 74 72 75 63 74 20 5f 68 74 29 3e 53 51 f(struct _ht)>SQ
d63c0 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 LITE_MALLOC_SOFT
d63d0 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 6e 65 _LIMIT ){. ne
d63e0 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49 54 45 5f w_size = SQLITE_
d63f0 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 MALLOC_SOFT_LIMI
d6400 54 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 T/sizeof(struct
d6410 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 _ht);. }. if(
d6420 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 2d 3e 68 74 new_size==pH->ht
d6430 73 69 7a 65 20 29 20 72 65 74 75 72 6e 20 30 3b size ) return 0;
d6440 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 .#endif.. /* Th
d6450 65 20 69 6e 61 62 69 6c 69 74 79 20 74 6f 20 61 e inability to a
d6460 6c 6c 6f 63 61 74 65 73 20 73 70 61 63 65 20 66 llocates space f
d6470 6f 72 20 61 20 6c 61 72 67 65 72 20 68 61 73 68 or a larger hash
d6480 20 74 61 62 6c 65 20 69 73 0a 20 20 2a 2a 20 61 table is. ** a
d6490 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 performance hit
d64a0 20 62 75 74 20 69 74 20 69 73 20 6e 6f 74 20 61 but it is not a
d64b0 20 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 53 fatal error. S
d64c0 6f 20 6d 61 72 6b 20 74 68 65 0a 20 20 2a 2a 20 o mark the. **
d64d0 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 73 20 61 20 allocation as a
d64e0 62 65 6e 69 67 6e 2e 20 55 73 65 20 73 71 6c 69 benign. Use sqli
d64f0 74 65 33 4d 61 6c 6c 6f 63 28 29 2f 6d 65 6d 73 te3Malloc()/mems
d6500 65 74 28 30 29 20 69 6e 73 74 65 61 64 20 6f 66 et(0) instead of
d6510 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 . ** sqlite3Ma
d6520 6c 6c 6f 63 5a 65 72 6f 28 29 20 74 6f 20 6d 61 llocZero() to ma
d6530 6b 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f ke the allocatio
d6540 6e 2c 20 61 73 20 73 71 6c 69 74 65 33 4d 61 6c n, as sqlite3Mal
d6550 6c 6f 63 5a 65 72 6f 28 29 0a 20 20 2a 2a 20 6f locZero(). ** o
d6560 6e 6c 79 20 7a 65 72 6f 65 73 20 74 68 65 20 72 nly zeroes the r
d6570 65 71 75 65 73 74 65 64 20 6e 75 6d 62 65 72 20 equested number
d6580 6f 66 20 62 79 74 65 73 20 77 68 65 72 65 61 73 of bytes whereas
d6590 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 77 69 6c this module wil
d65a0 6c 0a 20 20 2a 2a 20 75 73 65 20 74 68 65 20 61 l. ** use the a
d65b0 63 74 75 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 ctual amount of
d65c0 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 space allocated
d65d0 66 6f 72 20 74 68 65 20 68 61 73 68 20 74 61 62 for the hash tab
d65e0 6c 65 20 28 77 68 69 63 68 0a 20 20 2a 2a 20 6d le (which. ** m
d65f0 61 79 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 ay be larger tha
d6600 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 n the requested
d6610 61 6d 6f 75 6e 74 29 2e 0a 20 20 2a 2f 0a 20 20 amount).. */.
d6620 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
d6630 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 6e 65 gnMalloc();. ne
d6640 77 5f 68 74 20 3d 20 28 73 74 72 75 63 74 20 5f w_ht = (struct _
d6650 68 74 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c ht *)sqlite3Mall
d6660 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a oc( new_size*siz
d6670 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 20 eof(struct _ht)
d6680 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 );. sqlite3EndB
d6690 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a enignMalloc();..
d66a0 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20 if( new_ht==0
d66b0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 73 71 ) return 0;. sq
d66c0 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d 3e 68 lite3_free(pH->h
d66d0 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e t);. pH->ht = n
d66e0 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73 ew_ht;. pH->hts
d66f0 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 20 3d ize = new_size =
d6700 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 sqlite3MallocSi
d6710 7a 65 28 6e 65 77 5f 68 74 29 2f 73 69 7a 65 6f ze(new_ht)/sizeo
d6720 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b 0a 20 f(struct _ht);.
d6730 20 6d 65 6d 73 65 74 28 6e 65 77 5f 68 74 2c 20 memset(new_ht,
d6740 30 2c 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 0, new_size*size
d6750 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 29 3b of(struct _ht));
d6760 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d 3e . for(elem=pH->
d6770 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 72 73 74 first, pH->first
d6780 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20 3d =0; elem; elem =
d6790 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20 20 next_elem){.
d67a0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 unsigned int h
d67b0 3d 20 73 74 72 48 61 73 68 28 65 6c 65 6d 2d 3e = strHash(elem->
d67c0 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79 pKey, elem->nKey
d67d0 29 20 25 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20 ) % new_size;.
d67e0 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c next_elem = el
d67f0 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69 6e em->next;. in
d6800 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 sertElement(pH,
d6810 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65 6d &new_ht[h], elem
d6820 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
d6830 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 1;.}../* This fu
d6840 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65 nction (for inte
d6850 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c rnal use only) l
d6860 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e ocates an elemen
d6870 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20 t in an.** hash
d6880 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 table that match
d6890 65 73 20 74 68 65 20 67 69 76 65 6e 20 6b 65 79 es the given key
d68a0 2e 20 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 . The hash for
d68b0 74 68 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20 this key has.**
d68c0 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d already been com
d68d0 70 75 74 65 64 20 61 6e 64 20 69 73 20 70 61 73 puted and is pas
d68e0 73 65 64 20 61 73 20 74 68 65 20 34 74 68 20 70 sed as the 4th p
d68f0 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 arameter..*/.sta
d6900 74 69 63 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 tic HashElem *fi
d6910 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 ndElementGivenHa
d6920 73 68 28 0a 20 20 63 6f 6e 73 74 20 48 61 73 68 sh(. const Hash
d6930 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20 54 68 65 *pH, /* The
d6940 20 70 48 20 74 6f 20 62 65 20 73 65 61 72 63 68 pH to be search
d6950 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ed */. const ch
d6960 61 72 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 54 ar *pKey, /* T
d6970 68 65 20 6b 65 79 20 77 65 20 61 72 65 20 73 65 he key we are se
d6980 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 arching for */.
d6990 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20 20 20 int nKey,
d69a0 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e /* Bytes in
d69b0 20 6b 65 79 20 28 6e 6f 74 20 63 6f 75 6e 74 69 key (not counti
d69c0 6e 67 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 ng zero terminat
d69d0 6f 72 29 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 or) */. unsigne
d69e0 64 20 69 6e 74 20 68 20 20 20 20 20 20 2f 2a 20 d int h /*
d69f0 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68 69 The hash for thi
d6a00 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20 48 s key. */.){. H
d6a10 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 ashElem *elem;
d6a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d6a30 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 Used to loop th
d6a40 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c ru the element l
d6a50 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 ist */. int cou
d6a60 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nt;
d6a70 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
d6a80 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 6c 65 r of elements le
d6a90 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a 0a 20 ft to test */..
d6aa0 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 if( pH->ht ){.
d6ab0 20 20 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 struct _ht *p
d6ac0 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b Entry = &pH->ht[
d6ad0 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 h];. elem = p
d6ae0 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 Entry->chain;.
d6af0 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72 79 count = pEntry
d6b00 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d 65 6c 73 65 ->count;. }else
d6b10 7b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 48 2d {. elem = pH-
d6b20 3e 66 69 72 73 74 3b 0a 20 20 20 20 63 6f 75 6e >first;. coun
d6b30 74 20 3d 20 70 48 2d 3e 63 6f 75 6e 74 3b 0a 20 t = pH->count;.
d6b40 20 7d 0a 20 20 77 68 69 6c 65 28 20 63 6f 75 6e }. while( coun
d6b50 74 2d 2d 20 26 26 20 41 4c 57 41 59 53 28 65 6c t-- && ALWAYS(el
d6b60 65 6d 29 20 29 7b 0a 20 20 20 20 69 66 28 20 65 em) ){. if( e
d6b70 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b 65 79 20 lem->nKey==nKey
d6b80 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 && sqlite3StrNIC
d6b90 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 70 4b mp(elem->pKey,pK
d6ba0 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 0a ey,nKey)==0 ){ .
d6bb0 20 20 20 20 20 20 72 65 74 75 72 6e 20 65 6c 65 return ele
d6bc0 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c 65 m;. }. ele
d6bd0 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a m = elem->next;.
d6be0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
d6bf0 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20 73 }../* Remove a s
d6c00 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d ingle entry from
d6c10 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
d6c20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 given a pointer
d6c30 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d 65 to that.** eleme
d6c40 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20 6f 6e nt and a hash on
d6c50 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73 20 6b the element's k
d6c60 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ey..*/.static vo
d6c70 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 id removeElement
d6c80 47 69 76 65 6e 48 61 73 68 28 0a 20 20 48 61 73 GivenHash(. Has
d6c90 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20 2f h *pH, /
d6ca0 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61 69 6e * The pH contain
d6cb0 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 20 ing "elem" */.
d6cc0 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20 HashElem* elem,
d6cd0 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 /* The element
d6ce0 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64 20 66 to be removed f
d6cf0 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a 20 20 rom the pH */.
d6d00 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 20 unsigned int h
d6d10 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20 /* Hash value
d6d20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 for the element
d6d30 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 5f */.){. struct _
d6d40 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66 ht *pEntry;. if
d6d50 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a ( elem->prev ){.
d6d60 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e elem->prev->
d6d70 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 next = elem->nex
d6d80 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t; . }else{.
d6d90 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65 pH->first = ele
d6da0 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 m->next;. }. i
d6db0 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b f( elem->next ){
d6dc0 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d . elem->next-
d6dd0 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 >prev = elem->pr
d6de0 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 ev;. }. if( pH
d6df0 2d 3e 68 74 20 29 7b 0a 20 20 20 20 70 45 6e 74 ->ht ){. pEnt
d6e00 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b ry = &pH->ht[h];
d6e10 0a 20 20 20 20 69 66 28 20 70 45 6e 74 72 79 2d . if( pEntry-
d6e20 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0a >chain==elem ){.
d6e30 20 20 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 pEntry->ch
d6e40 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 ain = elem->next
d6e50 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 6e 74 ;. }. pEnt
d6e60 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 20 ry->count--;.
d6e70 20 61 73 73 65 72 74 28 20 70 45 6e 74 72 79 2d assert( pEntry-
d6e80 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20 7d >count>=0 );. }
d6e90 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
d6ea0 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63 elem );. pH->c
d6eb0 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48 ount--;. if( pH
d6ec0 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a 20 20 ->count==0 ){.
d6ed0 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69 assert( pH->fi
d6ee0 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 rst==0 );. as
d6ef0 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d sert( pH->count=
d6f00 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 =0 );. sqlite
d6f10 33 48 61 73 68 43 6c 65 61 72 28 70 48 29 3b 0a 3HashClear(pH);.
d6f20 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70 }.}../* Attemp
d6f30 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20 65 t to locate an e
d6f40 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 68 61 lement of the ha
d6f50 73 68 20 74 61 62 6c 65 20 70 48 20 77 69 74 68 sh table pH with
d6f60 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20 6d a key.** that m
d6f70 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65 79 atches pKey,nKey
d6f80 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 . Return the da
d6f90 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c 65 6d ta for this elem
d6fa0 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 ent if it is.**
d6fb0 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 found, or NULL i
d6fc0 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 f there is no ma
d6fd0 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 tch..*/.SQLITE_P
d6fe0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
d6ff0 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f 6e ite3HashFind(con
d7000 73 74 20 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e st Hash *pH, con
d7010 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 st char *pKey, i
d7020 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48 61 73 68 nt nKey){. Hash
d7030 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f Elem *elem; /
d7040 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 * The element th
d7050 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a at matches key *
d7060 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
d7070 20 68 3b 20 20 20 20 2f 2a 20 41 20 68 61 73 68 h; /* A hash
d7080 20 6f 6e 20 6b 65 79 20 2a 2f 0a 0a 20 20 61 73 on key */.. as
d7090 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 sert( pH!=0 );.
d70a0 20 61 73 73 65 72 74 28 20 70 4b 65 79 21 3d 30 assert( pKey!=0
d70b0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4b );. assert( nK
d70c0 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 ey>=0 );. if( p
d70d0 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 68 20 3d H->ht ){. h =
d70e0 20 73 74 72 48 61 73 68 28 70 4b 65 79 2c 20 6e strHash(pKey, n
d70f0 4b 65 79 29 20 25 20 70 48 2d 3e 68 74 73 69 7a Key) % pH->htsiz
d7100 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 e;. }else{.
d7110 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 6c 65 h = 0;. }. ele
d7120 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47 m = findElementG
d7130 69 76 65 6e 48 61 73 68 28 70 48 2c 20 70 4b 65 ivenHash(pH, pKe
d7140 79 2c 20 6e 4b 65 79 2c 20 68 29 3b 0a 20 20 72 y, nKey, h);. r
d7150 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 eturn elem ? ele
d7160 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a m->data : 0;.}..
d7170 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 /* Insert an ele
d7180 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 ment into the ha
d7190 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68 sh table pH. Th
d71a0 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b e key is pKey,nK
d71b0 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 ey.** and the da
d71c0 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a ta is "data"..**
d71d0 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e .** If no elemen
d71e0 74 20 65 78 69 73 74 73 20 77 69 74 68 20 61 20 t exists with a
d71f0 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 68 matching key, th
d7200 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d en a new.** elem
d7210 65 6e 74 20 69 73 20 63 72 65 61 74 65 64 20 61 ent is created a
d7220 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 nd NULL is retur
d7230 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ned..**.** If an
d7240 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74 20 61 6c other element al
d7250 72 65 61 64 79 20 65 78 69 73 74 73 20 77 69 74 ready exists wit
d7260 68 20 74 68 65 20 73 61 6d 65 20 6b 65 79 2c 20 h the same key,
d7270 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e 65 77 20 then the.** new
d7280 64 61 74 61 20 72 65 70 6c 61 63 65 73 20 74 68 data replaces th
d7290 65 20 6f 6c 64 20 64 61 74 61 20 61 6e 64 20 74 e old data and t
d72a0 68 65 20 6f 6c 64 20 64 61 74 61 20 69 73 20 72 he old data is r
d72b0 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 eturned..** The
d72c0 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f 70 69 65 key is not copie
d72d0 64 20 69 6e 20 74 68 69 73 20 69 6e 73 74 61 6e d in this instan
d72e0 63 65 2e 20 20 49 66 20 61 20 6d 61 6c 6c 6f 63 ce. If a malloc
d72f0 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a 2a 2a 20 fails, then.**
d7300 74 68 65 20 6e 65 77 20 64 61 74 61 20 69 73 20 the new data is
d7310 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 returned and the
d7320 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 75 hash table is u
d7330 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 nchanged..**.**
d7340 49 66 20 74 68 65 20 22 64 61 74 61 22 20 70 61 If the "data" pa
d7350 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73 20 rameter to this
d7360 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c function is NULL
d7370 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 6c , then the.** el
d7380 65 6d 65 6e 74 20 63 6f 72 72 65 73 70 6f 6e 64 ement correspond
d7390 69 6e 67 20 74 6f 20 22 6b 65 79 22 20 69 73 20 ing to "key" is
d73a0 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 removed from the
d73b0 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a hash table..*/.
d73c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
d73d0 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 oid *sqlite3Hash
d73e0 49 6e 73 65 72 74 28 48 61 73 68 20 2a 70 48 2c Insert(Hash *pH,
d73f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 const char *pKe
d7400 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 y, int nKey, voi
d7410 64 20 2a 64 61 74 61 29 7b 0a 20 20 75 6e 73 69 d *data){. unsi
d7420 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 20 20 20 gned int h;
d7430 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 6f 66 /* the hash of
d7440 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20 the key modulo
d7450 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a 65 20 hash table size
d7460 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 */. HashElem *e
d7470 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 lem; /* Us
d7480 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 ed to loop thru
d7490 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 the element list
d74a0 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a */. HashElem *
d74b0 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e new_elem; /* N
d74c0 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 64 ew element added
d74d0 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a 0a 20 to the pH */..
d74e0 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 assert( pH!=0 )
d74f0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 ;. assert( pKey
d7500 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
d7510 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66 nKey>=0 );. if
d7520 28 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a ( pH->htsize ){.
d7530 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 h = strHash(
d7540 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 pKey, nKey) % pH
d7550 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65 6c 73 ->htsize;. }els
d7560 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a 20 20 e{. h = 0;.
d7570 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 }. elem = findE
d7580 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 lementGivenHash(
d7590 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 29 3b pH,pKey,nKey,h);
d75a0 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 7b 0a 20 . if( elem ){.
d75b0 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74 void *old_dat
d75c0 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a a = elem->data;.
d75d0 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 if( data==0
d75e0 29 7b 0a 20 20 20 20 20 20 72 65 6d 6f 76 65 45 ){. removeE
d75f0 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 lementGivenHash(
d7600 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 20 20 pH,elem,h);.
d7610 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 6c 65 }else{. ele
d7620 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a m->data = data;.
d7630 20 20 20 20 20 20 65 6c 65 6d 2d 3e 70 4b 65 79 elem->pKey
d7640 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 61 = pKey;. a
d7650 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65 6c 65 6d ssert(nKey==elem
d7660 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 7d 0a 20 ->nKey);. }.
d7670 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 return old_da
d7680 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 61 ta;. }. if( da
d7690 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 ta==0 ) return 0
d76a0 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 20 3d 20 28 ;. new_elem = (
d76b0 48 61 73 68 45 6c 65 6d 2a 29 73 71 6c 69 74 65 HashElem*)sqlite
d76c0 33 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 3Malloc( sizeof(
d76d0 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a 20 20 69 HashElem) );. i
d76e0 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20 29 f( new_elem==0 )
d76f0 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 return data;.
d7700 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d new_elem->pKey =
d7710 20 70 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 6c 65 pKey;. new_ele
d7720 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 3b 0a m->nKey = nKey;.
d7730 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 61 new_elem->data
d7740 20 3d 20 64 61 74 61 3b 0a 20 20 70 48 2d 3e 63 = data;. pH->c
d7750 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70 48 ount++;. if( pH
d7760 2d 3e 63 6f 75 6e 74 3e 3d 31 30 20 26 26 20 70 ->count>=10 && p
d7770 48 2d 3e 63 6f 75 6e 74 20 3e 20 32 2a 70 48 2d H->count > 2*pH-
d7780 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 69 >htsize ){. i
d7790 66 28 20 72 65 68 61 73 68 28 70 48 2c 20 70 48 f( rehash(pH, pH
d77a0 2d 3e 63 6f 75 6e 74 2a 32 29 20 29 7b 0a 20 20 ->count*2) ){.
d77b0 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e assert( pH->
d77c0 68 74 73 69 7a 65 3e 30 20 29 3b 0a 20 20 20 20 htsize>0 );.
d77d0 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b h = strHash(pK
d77e0 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e ey, nKey) % pH->
d77f0 68 74 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20 htsize;. }.
d7800 7d 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 }. if( pH->ht )
d7810 7b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d {. insertElem
d7820 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 74 5b ent(pH, &pH->ht[
d7830 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 h], new_elem);.
d7840 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 73 65 }else{. inse
d7850 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 30 2c rtElement(pH, 0,
d7860 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 7d 0a new_elem);. }.
d7870 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
d7880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
d7890 6e 64 20 6f 66 20 68 61 73 68 2e 63 20 2a 2a 2a nd of hash.c ***
d78a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d78b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d78c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
d78d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
d78e0 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 egin file opcode
d78f0 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.c ************
d7900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
d7920 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 * Automatically
d7930 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e generated. Do n
d7940 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 ot edit */./* Se
d7950 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 63 2e e the mkopcodec.
d7960 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 awk script for d
d7970 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 20 21 etails. */.#if !
d7980 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
d7990 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c 20 MIT_EXPLAIN) ||
d79a0 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
d79b0 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 || defined(VDBE
d79c0 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 _PROFILE) || def
d79d0 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
d79e0 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 G).SQLITE_PRIVAT
d79f0 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 E const char *sq
d7a00 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 lite3OpcodeName(
d7a10 69 6e 74 20 69 29 7b 0a 20 73 74 61 74 69 63 20 int i){. static
d7a20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 const char *cons
d7a30 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 20 22 t azName[] = { "
d7a40 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 31 20 ?",. /* 1
d7a50 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20 20 */ "Goto",.
d7a60 2f 2a 20 20 20 32 20 2a 2f 20 22 47 6f 73 75 62 /* 2 */ "Gosub
d7a70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 33 20 2a ",. /* 3 *
d7a80 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 20 20 20 / "Return",.
d7a90 20 2f 2a 20 20 20 34 20 2a 2f 20 22 59 69 65 6c /* 4 */ "Yiel
d7aa0 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 35 20 d",. /* 5
d7ab0 2a 2f 20 22 48 61 6c 74 49 66 4e 75 6c 6c 22 2c */ "HaltIfNull",
d7ac0 0a 20 20 20 20 20 2f 2a 20 20 20 36 20 2a 2f 20 . /* 6 */
d7ad0 22 48 61 6c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 "Halt",. /*
d7ae0 20 20 37 20 2a 2f 20 22 49 6e 74 65 67 65 72 22 7 */ "Integer"
d7af0 2c 0a 20 20 20 20 20 2f 2a 20 20 20 38 20 2a 2f ,. /* 8 */
d7b00 20 22 49 6e 74 36 34 22 2c 0a 20 20 20 20 20 2f "Int64",. /
d7b10 2a 20 20 20 39 20 2a 2f 20 22 53 74 72 69 6e 67 * 9 */ "String
d7b20 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 30 20 2a ",. /* 10 *
d7b30 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f / "Null",. /
d7b40 2a 20 20 31 31 20 2a 2f 20 22 42 6c 6f 62 22 2c * 11 */ "Blob",
d7b50 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 . /* 12 */
d7b60 22 56 61 72 69 61 62 6c 65 22 2c 0a 20 20 20 20 "Variable",.
d7b70 20 2f 2a 20 20 31 33 20 2a 2f 20 22 4d 6f 76 65 /* 13 */ "Move
d7b80 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34 20 2a ",. /* 14 *
d7b90 2f 20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f / "Copy",. /
d7ba0 2a 20 20 31 35 20 2a 2f 20 22 53 43 6f 70 79 22 * 15 */ "SCopy"
d7bb0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 36 20 2a 2f ,. /* 16 */
d7bc0 20 22 52 65 73 75 6c 74 52 6f 77 22 2c 0a 20 20 "ResultRow",.
d7bd0 20 20 20 2f 2a 20 20 31 37 20 2a 2f 20 22 43 6f /* 17 */ "Co
d7be0 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a 20 llSeq",. /*
d7bf0 20 31 38 20 2a 2f 20 22 46 75 6e 63 74 69 6f 6e 18 */ "Function
d7c00 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 39 20 2a ",. /* 19 *
d7c10 2f 20 22 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a / "Not",. /*
d7c20 20 20 32 30 20 2a 2f 20 22 41 64 64 49 6d 6d 22 20 */ "AddImm"
d7c30 2c 0a 20 20 20 20 20 2f 2a 20 20 32 31 20 2a 2f ,. /* 21 */
d7c40 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a 20 20 "MustBeInt",.
d7c50 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 52 65 /* 22 */ "Re
d7c60 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 alAffinity",.
d7c70 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 50 65 72 /* 23 */ "Per
d7c80 6d 75 74 61 74 69 6f 6e 22 2c 0a 20 20 20 20 20 mutation",.
d7c90 2f 2a 20 20 32 34 20 2a 2f 20 22 43 6f 6d 70 61 /* 24 */ "Compa
d7ca0 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 35 re",. /* 25
d7cb0 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a 20 20 20 20 */ "Jump",.
d7cc0 20 2f 2a 20 20 32 36 20 2a 2f 20 22 4f 6e 63 65 /* 26 */ "Once
d7cd0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 37 20 2a ",. /* 27 *
d7ce0 2f 20 22 49 66 22 2c 0a 20 20 20 20 20 2f 2a 20 / "If",. /*
d7cf0 20 32 38 20 2a 2f 20 22 49 66 4e 6f 74 22 2c 0a 28 */ "IfNot",.
d7d00 20 20 20 20 20 2f 2a 20 20 32 39 20 2a 2f 20 22 /* 29 */ "
d7d10 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a Column",. /*
d7d20 20 20 33 30 20 2a 2f 20 22 41 66 66 69 6e 69 74 30 */ "Affinit
d7d30 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 31 20 y",. /* 31
d7d40 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64 22 2c */ "MakeRecord",
d7d50 0a 20 20 20 20 20 2f 2a 20 20 33 32 20 2a 2f 20 . /* 32 */
d7d60 22 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a "Count",. /*
d7d70 20 20 33 33 20 2a 2f 20 22 53 61 76 65 70 6f 69 33 */ "Savepoi
d7d80 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 34 nt",. /* 34
d7d90 20 2a 2f 20 22 41 75 74 6f 43 6f 6d 6d 69 74 22 */ "AutoCommit"
d7da0 2c 0a 20 20 20 20 20 2f 2a 20 20 33 35 20 2a 2f ,. /* 35 */
d7db0 20 22 54 72 61 6e 73 61 63 74 69 6f 6e 22 2c 0a "Transaction",.
d7dc0 20 20 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20 22 /* 36 */ "
d7dd0 52 65 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 ReadCookie",.
d7de0 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22 53 65 74 /* 37 */ "Set
d7df0 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a Cookie",. /*
d7e00 20 20 33 38 20 2a 2f 20 22 56 65 72 69 66 79 43 38 */ "VerifyC
d7e10 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20 ookie",. /*
d7e20 20 33 39 20 2a 2f 20 22 4f 70 65 6e 52 65 61 64 39 */ "OpenRead
d7e30 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 30 20 2a ",. /* 40 *
d7e40 2f 20 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a 20 / "OpenWrite",.
d7e50 20 20 20 20 2f 2a 20 20 34 31 20 2a 2f 20 22 4f /* 41 */ "O
d7e60 70 65 6e 41 75 74 6f 69 6e 64 65 78 22 2c 0a 20 penAutoindex",.
d7e70 20 20 20 20 2f 2a 20 20 34 32 20 2a 2f 20 22 4f /* 42 */ "O
d7e80 70 65 6e 45 70 68 65 6d 65 72 61 6c 22 2c 0a 20 penEphemeral",.
d7e90 20 20 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22 53 /* 43 */ "S
d7ea0 6f 72 74 65 72 4f 70 65 6e 22 2c 0a 20 20 20 20 orterOpen",.
d7eb0 20 2f 2a 20 20 34 34 20 2a 2f 20 22 4f 70 65 6e /* 44 */ "Open
d7ec0 50 73 65 75 64 6f 22 2c 0a 20 20 20 20 20 2f 2a Pseudo",. /*
d7ed0 20 20 34 35 20 2a 2f 20 22 43 6c 6f 73 65 22 2c 45 */ "Close",
d7ee0 0a 20 20 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20 . /* 46 */
d7ef0 22 53 65 65 6b 4c 74 22 2c 0a 20 20 20 20 20 2f "SeekLt",. /
d7f00 2a 20 20 34 37 20 2a 2f 20 22 53 65 65 6b 4c 65 * 47 */ "SeekLe
d7f10 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 38 20 2a ",. /* 48 *
d7f20 2f 20 22 53 65 65 6b 47 65 22 2c 0a 20 20 20 20 / "SeekGe",.
d7f30 20 2f 2a 20 20 34 39 20 2a 2f 20 22 53 65 65 6b /* 49 */ "Seek
d7f40 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 Gt",. /* 50
d7f50 20 2a 2f 20 22 53 65 65 6b 22 2c 0a 20 20 20 20 */ "Seek",.
d7f60 20 2f 2a 20 20 35 31 20 2a 2f 20 22 4e 6f 74 46 /* 51 */ "NotF
d7f70 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ound",. /*
d7f80 35 32 20 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a 20 52 */ "Found",.
d7f90 20 20 20 20 2f 2a 20 20 35 33 20 2a 2f 20 22 49 /* 53 */ "I
d7fa0 73 55 6e 69 71 75 65 22 2c 0a 20 20 20 20 20 2f sUnique",. /
d7fb0 2a 20 20 35 34 20 2a 2f 20 22 4e 6f 74 45 78 69 * 54 */ "NotExi
d7fc0 73 74 73 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 sts",. /* 5
d7fd0 35 20 2a 2f 20 22 53 65 71 75 65 6e 63 65 22 2c 5 */ "Sequence",
d7fe0 0a 20 20 20 20 20 2f 2a 20 20 35 36 20 2a 2f 20 . /* 56 */
d7ff0 22 4e 65 77 52 6f 77 69 64 22 2c 0a 20 20 20 20 "NewRowid",.
d8000 20 2f 2a 20 20 35 37 20 2a 2f 20 22 49 6e 73 65 /* 57 */ "Inse
d8010 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 38 rt",. /* 58
d8020 20 2a 2f 20 22 49 6e 73 65 72 74 49 6e 74 22 2c */ "InsertInt",
d8030 0a 20 20 20 20 20 2f 2a 20 20 35 39 20 2a 2f 20 . /* 59 */
d8040 22 44 65 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f "Delete",. /
d8050 2a 20 20 36 30 20 2a 2f 20 22 52 65 73 65 74 43 * 60 */ "ResetC
d8060 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ount",. /*
d8070 36 31 20 2a 2f 20 22 53 6f 72 74 65 72 43 6f 6d 61 */ "SorterCom
d8080 70 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 pare",. /*
d8090 36 32 20 2a 2f 20 22 53 6f 72 74 65 72 44 61 74 62 */ "SorterDat
d80a0 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 33 20 a",. /* 63
d80b0 2a 2f 20 22 52 6f 77 4b 65 79 22 2c 0a 20 20 20 */ "RowKey",.
d80c0 20 20 2f 2a 20 20 36 34 20 2a 2f 20 22 52 6f 77 /* 64 */ "Row
d80d0 44 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Data",. /*
d80e0 36 35 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20 65 */ "Rowid",.
d80f0 20 20 20 20 2f 2a 20 20 36 36 20 2a 2f 20 22 4e /* 66 */ "N
d8100 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a ullRow",. /*
d8110 20 20 36 37 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 67 */ "Last",.
d8120 20 20 20 20 20 2f 2a 20 20 36 38 20 2a 2f 20 22 /* 68 */ "
d8130 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 39 Or",. /* 69
d8140 20 2a 2f 20 22 41 6e 64 22 2c 0a 20 20 20 20 20 */ "And",.
d8150 2f 2a 20 20 37 30 20 2a 2f 20 22 53 6f 72 74 65 /* 70 */ "Sorte
d8160 72 53 6f 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 rSort",. /*
d8170 20 37 31 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 71 */ "Sort",.
d8180 20 20 20 20 2f 2a 20 20 37 32 20 2a 2f 20 22 52 /* 72 */ "R
d8190 65 77 69 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 ewind",. /*
d81a0 20 37 33 20 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c 73 */ "IsNull",
d81b0 0a 20 20 20 20 20 2f 2a 20 20 37 34 20 2a 2f 20 . /* 74 */
d81c0 22 4e 6f 74 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 "NotNull",.
d81d0 2f 2a 20 20 37 35 20 2a 2f 20 22 4e 65 22 2c 0a /* 75 */ "Ne",.
d81e0 20 20 20 20 20 2f 2a 20 20 37 36 20 2a 2f 20 22 /* 76 */ "
d81f0 45 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 37 Eq",. /* 77
d8200 20 2a 2f 20 22 47 74 22 2c 0a 20 20 20 20 20 2f */ "Gt",. /
d8210 2a 20 20 37 38 20 2a 2f 20 22 4c 65 22 2c 0a 20 * 78 */ "Le",.
d8220 20 20 20 20 2f 2a 20 20 37 39 20 2a 2f 20 22 4c /* 79 */ "L
d8230 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 30 20 t",. /* 80
d8240 2a 2f 20 22 47 65 22 2c 0a 20 20 20 20 20 2f 2a */ "Ge",. /*
d8250 20 20 38 31 20 2a 2f 20 22 53 6f 72 74 65 72 4e 81 */ "SorterN
d8260 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 ext",. /* 8
d8270 32 20 2a 2f 20 22 42 69 74 41 6e 64 22 2c 0a 20 2 */ "BitAnd",.
d8280 20 20 20 20 2f 2a 20 20 38 33 20 2a 2f 20 22 42 /* 83 */ "B
d8290 69 74 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 itOr",. /*
d82a0 38 34 20 2a 2f 20 22 53 68 69 66 74 4c 65 66 74 84 */ "ShiftLeft
d82b0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 35 20 2a ",. /* 85 *
d82c0 2f 20 22 53 68 69 66 74 52 69 67 68 74 22 2c 0a / "ShiftRight",.
d82d0 20 20 20 20 20 2f 2a 20 20 38 36 20 2a 2f 20 22 /* 86 */ "
d82e0 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 Add",. /* 8
d82f0 37 20 2a 2f 20 22 53 75 62 74 72 61 63 74 22 2c 7 */ "Subtract",
d8300 0a 20 20 20 20 20 2f 2a 20 20 38 38 20 2a 2f 20 . /* 88 */
d8310 22 4d 75 6c 74 69 70 6c 79 22 2c 0a 20 20 20 20 "Multiply",.
d8320 20 2f 2a 20 20 38 39 20 2a 2f 20 22 44 69 76 69 /* 89 */ "Divi
d8330 64 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 30 de",. /* 90
d8340 20 2a 2f 20 22 52 65 6d 61 69 6e 64 65 72 22 2c */ "Remainder",
d8350 0a 20 20 20 20 20 2f 2a 20 20 39 31 20 2a 2f 20 . /* 91 */
d8360 22 43 6f 6e 63 61 74 22 2c 0a 20 20 20 20 20 2f "Concat",. /
d8370 2a 20 20 39 32 20 2a 2f 20 22 50 72 65 76 22 2c * 92 */ "Prev",
d8380 0a 20 20 20 20 20 2f 2a 20 20 39 33 20 2a 2f 20 . /* 93 */
d8390 22 42 69 74 4e 6f 74 22 2c 0a 20 20 20 20 20 2f "BitNot",. /
d83a0 2a 20 20 39 34 20 2a 2f 20 22 53 74 72 69 6e 67 * 94 */ "String
d83b0 38 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 35 20 8",. /* 95
d83c0 2a 2f 20 22 4e 65 78 74 22 2c 0a 20 20 20 20 20 */ "Next",.
d83d0 2f 2a 20 20 39 36 20 2a 2f 20 22 53 6f 72 74 65 /* 96 */ "Sorte
d83e0 72 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 2f rInsert",. /
d83f0 2a 20 20 39 37 20 2a 2f 20 22 49 64 78 49 6e 73 * 97 */ "IdxIns
d8400 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 ert",. /* 9
d8410 38 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 22 8 */ "IdxDelete"
d8420 2c 0a 20 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f ,. /* 99 */
d8430 20 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 "IdxRowid",.
d8440 20 20 2f 2a 20 31 30 30 20 2a 2f 20 22 49 64 78 /* 100 */ "Idx
d8450 4c 54 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 31 LT",. /* 101
d8460 20 2a 2f 20 22 49 64 78 47 45 22 2c 0a 20 20 20 */ "IdxGE",.
d8470 20 20 2f 2a 20 31 30 32 20 2a 2f 20 22 44 65 73 /* 102 */ "Des
d8480 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31 troy",. /* 1
d8490 30 33 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a 20 03 */ "Clear",.
d84a0 20 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22 43 /* 104 */ "C
d84b0 72 65 61 74 65 49 6e 64 65 78 22 2c 0a 20 20 20 reateIndex",.
d84c0 20 20 2f 2a 20 31 30 35 20 2a 2f 20 22 43 72 65 /* 105 */ "Cre
d84d0 61 74 65 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 ateTable",.
d84e0 2f 2a 20 31 30 36 20 2a 2f 20 22 50 61 72 73 65 /* 106 */ "Parse
d84f0 53 63 68 65 6d 61 22 2c 0a 20 20 20 20 20 2f 2a Schema",. /*
d8500 20 31 30 37 20 2a 2f 20 22 4c 6f 61 64 41 6e 61 107 */ "LoadAna
d8510 6c 79 73 69 73 22 2c 0a 20 20 20 20 20 2f 2a 20 lysis",. /*
d8520 31 30 38 20 2a 2f 20 22 44 72 6f 70 54 61 62 6c 108 */ "DropTabl
d8530 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 39 20 e",. /* 109
d8540 2a 2f 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a */ "DropIndex",.
d8550 20 20 20 20 20 2f 2a 20 31 31 30 20 2a 2f 20 22 /* 110 */ "
d8560 44 72 6f 70 54 72 69 67 67 65 72 22 2c 0a 20 20 DropTrigger",.
d8570 20 20 20 2f 2a 20 31 31 31 20 2a 2f 20 22 49 6e /* 111 */ "In
d8580 74 65 67 72 69 74 79 43 6b 22 2c 0a 20 20 20 20 tegrityCk",.
d8590 20 2f 2a 20 31 31 32 20 2a 2f 20 22 52 6f 77 53 /* 112 */ "RowS
d85a0 65 74 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20 etAdd",. /*
d85b0 31 31 33 20 2a 2f 20 22 52 6f 77 53 65 74 52 65 113 */ "RowSetRe
d85c0 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 34 ad",. /* 114
d85d0 20 2a 2f 20 22 52 6f 77 53 65 74 54 65 73 74 22 */ "RowSetTest"
d85e0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35 20 2a 2f ,. /* 115 */
d85f0 20 22 50 72 6f 67 72 61 6d 22 2c 0a 20 20 20 20 "Program",.
d8600 20 2f 2a 20 31 31 36 20 2a 2f 20 22 50 61 72 61 /* 116 */ "Para
d8610 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20 m",. /* 117
d8620 2a 2f 20 22 46 6b 43 6f 75 6e 74 65 72 22 2c 0a */ "FkCounter",.
d8630 20 20 20 20 20 2f 2a 20 31 31 38 20 2a 2f 20 22 /* 118 */ "
d8640 46 6b 49 66 5a 65 72 6f 22 2c 0a 20 20 20 20 20 FkIfZero",.
d8650 2f 2a 20 31 31 39 20 2a 2f 20 22 4d 65 6d 4d 61 /* 119 */ "MemMa
d8660 78 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 30 20 x",. /* 120
d8670 2a 2f 20 22 49 66 50 6f 73 22 2c 0a 20 20 20 20 */ "IfPos",.
d8680 20 2f 2a 20 31 32 31 20 2a 2f 20 22 49 66 4e 65 /* 121 */ "IfNe
d8690 67 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 32 20 g",. /* 122
d86a0 2a 2f 20 22 49 66 5a 65 72 6f 22 2c 0a 20 20 20 */ "IfZero",.
d86b0 20 20 2f 2a 20 31 32 33 20 2a 2f 20 22 41 67 67 /* 123 */ "Agg
d86c0 53 74 65 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Step",. /* 1
d86d0 32 34 20 2a 2f 20 22 41 67 67 46 69 6e 61 6c 22 24 */ "AggFinal"
d86e0 2c 0a 20 20 20 20 20 2f 2a 20 31 32 35 20 2a 2f ,. /* 125 */
d86f0 20 22 43 68 65 63 6b 70 6f 69 6e 74 22 2c 0a 20 "Checkpoint",.
d8700 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22 4a /* 126 */ "J
d8710 6f 75 72 6e 61 6c 4d 6f 64 65 22 2c 0a 20 20 20 ournalMode",.
d8720 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56 61 63 /* 127 */ "Vac
d8730 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 uum",. /* 12
d8740 38 20 2a 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 8 */ "IncrVacuum
d8750 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 39 20 2a ",. /* 129 *
d8760 2f 20 22 45 78 70 69 72 65 22 2c 0a 20 20 20 20 / "Expire",.
d8770 20 2f 2a 20 31 33 30 20 2a 2f 20 22 52 65 61 6c /* 130 */ "Real
d8780 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 31 20 2a ",. /* 131 *
d8790 2f 20 22 54 61 62 6c 65 4c 6f 63 6b 22 2c 0a 20 / "TableLock",.
d87a0 20 20 20 20 2f 2a 20 31 33 32 20 2a 2f 20 22 56 /* 132 */ "V
d87b0 42 65 67 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 Begin",. /*
d87c0 31 33 33 20 2a 2f 20 22 56 43 72 65 61 74 65 22 133 */ "VCreate"
d87d0 2c 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f ,. /* 134 */
d87e0 20 22 56 44 65 73 74 72 6f 79 22 2c 0a 20 20 20 "VDestroy",.
d87f0 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 56 4f 70 /* 135 */ "VOp
d8800 65 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 36 en",. /* 136
d8810 20 2a 2f 20 22 56 46 69 6c 74 65 72 22 2c 0a 20 */ "VFilter",.
d8820 20 20 20 20 2f 2a 20 31 33 37 20 2a 2f 20 22 56 /* 137 */ "V
d8830 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a Column",. /*
d8840 20 31 33 38 20 2a 2f 20 22 56 4e 65 78 74 22 2c 138 */ "VNext",
d8850 0a 20 20 20 20 20 2f 2a 20 31 33 39 20 2a 2f 20 . /* 139 */
d8860 22 56 52 65 6e 61 6d 65 22 2c 0a 20 20 20 20 20 "VRename",.
d8870 2f 2a 20 31 34 30 20 2a 2f 20 22 56 55 70 64 61 /* 140 */ "VUpda
d8880 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 te",. /* 141
d8890 20 2a 2f 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 */ "ToText",.
d88a0 20 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f /* 142 */ "To
d88b0 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Blob",. /* 1
d88c0 34 33 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 43 */ "ToNumeric
d88d0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 34 20 2a ",. /* 144 *
d88e0 2f 20 22 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 / "ToInt",.
d88f0 2f 2a 20 31 34 35 20 2a 2f 20 22 54 6f 52 65 61 /* 145 */ "ToRea
d8900 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 36 20 l",. /* 146
d8910 2a 2f 20 22 50 61 67 65 63 6f 75 6e 74 22 2c 0a */ "Pagecount",.
d8920 20 20 20 20 20 2f 2a 20 31 34 37 20 2a 2f 20 22 /* 147 */ "
d8930 4d 61 78 50 67 63 6e 74 22 2c 0a 20 20 20 20 20 MaxPgcnt",.
d8940 2f 2a 20 31 34 38 20 2a 2f 20 22 54 72 61 63 65 /* 148 */ "Trace
d8950 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 39 20 2a ",. /* 149 *
d8960 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 2f / "Noop",. /
d8970 2a 20 31 35 30 20 2a 2f 20 22 45 78 70 6c 61 69 * 150 */ "Explai
d8980 6e 22 2c 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 n",. };. retur
d8990 6e 20 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 n azName[i];.}.#
d89a0 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a endif../********
d89b0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 ****** End of op
d89c0 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a codes.c ********
d89d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d89e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d89f0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
d8a00 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
d8a10 65 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a e os_unix.c ****
d8a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8a40 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
d8a50 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 4 May 22.**.** T
d8a60 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
d8a70 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
d8a80 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
d8a90 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
d8aa0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
d8ab0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
d8ac0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
d8ad0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
d8ae0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
d8af0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
d8b00 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
d8b10 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
d8b20 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
d8b30 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
d8b40 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
d8b50 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
d8b60 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
d8b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
d8bc0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
d8bd0 74 61 69 6e 73 20 74 68 65 20 56 46 53 20 69 6d tains the VFS im
d8be0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 plementation for
d8bf0 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 unix-like opera
d8c00 74 69 6e 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 ting systems.**
d8c10 69 6e 63 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d include Linux, M
d8c20 61 63 4f 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 acOSX, *BSD, QNX
d8c30 2c 20 56 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 , VxWorks, AIX,
d8c40 48 50 55 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 HPUX, and others
d8c50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 ..**.** There ar
d8c60 65 20 61 63 74 75 61 6c 6c 79 20 73 65 76 65 72 e actually sever
d8c70 61 6c 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 al different VFS
d8c80 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
d8c90 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a in this file..*
d8ca0 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 * The difference
d8cb0 73 20 61 72 65 20 69 6e 20 74 68 65 20 77 61 79 s are in the way
d8cc0 20 74 68 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 that file locki
d8cd0 6e 67 20 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 ng is done. The
d8ce0 20 64 65 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c default.** impl
d8cf0 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 ementation uses
d8d00 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c Posix Advisory L
d8d10 6f 63 6b 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 ocks. Alternati
d8d20 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ve implementatio
d8d30 6e 73 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 ns.** use flock(
d8d40 29 2c 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 ), dot-files, va
d8d50 72 69 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72 rious proprietar
d8d60 79 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 y locking schema
d8d70 73 2c 20 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 s, or simply.**
d8d80 73 6b 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c skip locking all
d8d90 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a together..**.**
d8da0 20 54 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c This source fil
d8db0 65 20 69 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 e is organized i
d8dc0 6e 74 6f 20 64 69 76 69 73 69 6f 6e 73 20 77 68 nto divisions wh
d8dd0 65 72 65 20 74 68 65 20 6c 6f 67 69 63 20 66 6f ere the logic fo
d8de0 72 20 76 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 r various.** sub
d8df0 66 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e functions is con
d8e00 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 tained within th
d8e10 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 64 69 e appropriate di
d8e20 76 69 73 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a vision. PLEASE.
d8e30 2a 2a 20 4b 45 45 50 20 54 48 45 20 53 54 52 55 ** KEEP THE STRU
d8e40 43 54 55 52 45 20 4f 46 20 54 48 49 53 20 46 49 CTURE OF THIS FI
d8e50 4c 45 20 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 LE INTACT. New
d8e60 63 6f 64 65 20 73 68 6f 75 6c 64 20 62 65 20 70 code should be p
d8e70 6c 61 63 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 laced.** in the
d8e80 63 6f 72 72 65 63 74 20 64 69 76 69 73 69 6f 6e correct division
d8e90 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 63 and should be c
d8ea0 6c 65 61 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a learly labeled..
d8eb0 2a 2a 0a 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 **.** The layout
d8ec0 20 6f 66 20 64 69 76 69 73 69 6f 6e 73 20 69 73 of divisions is
d8ed0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
d8ee0 2a 2a 20 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d ** * General-
d8ef0 70 75 72 70 6f 73 65 20 64 65 63 6c 61 72 61 74 purpose declarat
d8f00 69 6f 6e 73 20 61 6e 64 20 75 74 69 6c 69 74 79 ions and utility
d8f10 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 functions..**
d8f20 20 2a 20 20 55 6e 69 71 75 65 20 66 69 6c 65 20 * Unique file
d8f30 49 44 20 6c 6f 67 69 63 20 75 73 65 64 20 62 79 ID logic used by
d8f40 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a VxWorks..** *
d8f50 20 20 56 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e Various lockin
d8f60 67 20 70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c g primitive impl
d8f70 65 6d 65 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c ementations (all
d8f80 20 65 78 63 65 70 74 20 70 72 6f 78 79 20 6c 6f except proxy lo
d8f90 63 6b 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 cking):.**
d8fa0 2b 20 66 6f 72 20 50 6f 73 69 78 20 41 64 76 69 + for Posix Advi
d8fb0 73 6f 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 sory Locks.**
d8fc0 20 20 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c + for no-op l
d8fd0 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 ocks.** + f
d8fe0 6f 72 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b or dot-file lock
d8ff0 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 s.** + for
d9000 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a flock() locking.
d9010 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 ** + for na
d9020 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f med semaphore lo
d9030 63 6b 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c cks (VxWorks onl
d9040 79 29 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 y).** + for
d9050 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20 AFP filesystem
d9060 6c 6f 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e locks (MacOSX on
d9070 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 ly).** * sqli
d9080 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 te3_file methods
d9090 20 6e 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20 not associated
d90a0 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a with locking..**
d90b0 20 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e * Definition
d90c0 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f s of sqlite3_io_
d90d0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 methods objects
d90e0 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a for all locking.
d90f0 2a 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 ** methods
d9100 70 6c 75 73 20 22 66 69 6e 64 65 72 22 20 66 75 plus "finder" fu
d9110 6e 63 74 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 nctions for each
d9120 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e locking method.
d9130 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 .** * sqlite3
d9140 5f 76 66 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c _vfs method impl
d9150 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 ementations..**
d9160 20 20 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 * Locking pri
d9170 6d 69 74 69 76 65 73 20 66 6f 72 20 74 68 65 20 mitives for the
d9180 70 72 6f 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 proxy uber-locki
d9190 6e 67 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f ng-method. (MacO
d91a0 53 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 SX only).** *
d91b0 20 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 Definitions of
d91c0 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 sqlite3_vfs obje
d91d0 63 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b cts for all lock
d91e0 69 6e 67 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 ing methods.**
d91f0 20 20 20 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 plus impleme
d9200 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 ntations of sqli
d9210 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e te3_os_init() an
d9220 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 d sqlite3_os_end
d9230 28 29 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ()..*/.#if SQLIT
d9240 45 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20 E_OS_UNIX
d9250 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 /* This f
d9260 69 6c 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75 ile is used on u
d9270 6e 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 20 nix only */../*
d9280 55 73 65 20 70 6f 73 69 78 5f 66 61 6c 6c 6f 63 Use posix_falloc
d9290 61 74 65 28 29 20 69 66 20 69 74 20 69 73 20 61 ate() if it is a
d92a0 76 61 69 6c 61 62 6c 65 0a 2a 2f 0a 23 69 66 20 vailable.*/.#if
d92b0 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 50 4f !defined(HAVE_PO
d92c0 53 49 58 5f 46 41 4c 4c 4f 43 41 54 45 29 20 5c SIX_FALLOCATE) \
d92d0 0a 20 20 20 20 20 20 26 26 20 28 5f 58 4f 50 45 . && (_XOPE
d92e0 4e 5f 53 4f 55 52 43 45 20 3e 3d 20 36 30 30 20 N_SOURCE >= 600
d92f0 7c 7c 20 5f 50 4f 53 49 58 5f 43 5f 53 4f 55 52 || _POSIX_C_SOUR
d9300 43 45 20 3e 3d 20 32 30 30 31 31 32 4c 29 0a 23 CE >= 200112L).#
d9310 20 64 65 66 69 6e 65 20 48 41 56 45 5f 50 4f 53 define HAVE_POS
d9320 49 58 5f 46 41 4c 4c 4f 43 41 54 45 20 31 0a 23 IX_FALLOCATE 1.#
d9330 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
d9340 72 65 20 61 72 65 20 76 61 72 69 6f 75 73 20 6d re are various m
d9350 65 74 68 6f 64 73 20 66 6f 72 20 66 69 6c 65 20 ethods for file
d9360 6c 6f 63 6b 69 6e 67 20 75 73 65 64 20 66 6f 72 locking used for
d9370 20 63 6f 6e 63 75 72 72 65 6e 63 79 0a 2a 2a 20 concurrency.**
d9380 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a 0a 2a 2a 20 20 control:.**.**
d9390 20 31 2e 20 50 4f 53 49 58 20 6c 6f 63 6b 69 6e 1. POSIX lockin
d93a0 67 20 28 74 68 65 20 64 65 66 61 75 6c 74 29 2c g (the default),
d93b0 0a 2a 2a 20 20 20 32 2e 20 4e 6f 20 6c 6f 63 6b .** 2. No lock
d93c0 69 6e 67 2c 0a 2a 2a 20 20 20 33 2e 20 44 6f 74 ing,.** 3. Dot
d93d0 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 0a 2a -file locking,.*
d93e0 2a 20 20 20 34 2e 20 66 6c 6f 63 6b 28 29 20 6c * 4. flock() l
d93f0 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 35 2e 20 ocking,.** 5.
d9400 41 46 50 20 6c 6f 63 6b 69 6e 67 20 28 4f 53 58 AFP locking (OSX
d9410 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 36 2e 20 only),.** 6.
d9420 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65 6d 61 Named POSIX sema
d9430 70 68 6f 72 65 73 20 28 56 58 57 6f 72 6b 73 20 phores (VXWorks
d9440 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 37 2e 20 70 only),.** 7. p
d9450 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 20 28 4f roxy locking. (O
d9460 53 58 20 6f 6e 6c 79 29 0a 2a 2a 0a 2a 2a 20 53 SX only).**.** S
d9470 74 79 6c 65 73 20 34 2c 20 35 2c 20 61 6e 64 20 tyles 4, 5, and
d9480 37 20 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 7 are only avail
d9490 61 62 6c 65 20 6f 66 20 53 51 4c 49 54 45 5f 45 able of SQLITE_E
d94a0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
d94b0 59 4c 45 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 YLE.** is define
d94c0 64 20 74 6f 20 31 2e 20 20 54 68 65 20 53 51 4c d to 1. The SQL
d94d0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
d94e0 4e 47 5f 53 54 59 4c 45 20 61 6c 73 6f 20 65 6e NG_STYLE also en
d94f0 61 62 6c 65 73 20 61 75 74 6f 6d 61 74 69 63 0a ables automatic.
d9500 2a 2a 20 73 65 6c 65 63 74 69 6f 6e 20 6f 66 20 ** selection of
d9510 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
d9520 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 62 61 locking style ba
d9530 73 65 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 73 sed on the files
d9540 79 73 74 65 6d 0a 2a 2a 20 77 68 65 72 65 20 74 ystem.** where t
d9550 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6c he database is l
d9560 6f 63 61 74 65 64 2e 20 20 0a 2a 2f 0a 23 69 66 ocated. .*/.#if
d9570 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
d9580 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
d9590 53 54 59 4c 45 29 0a 23 20 20 69 66 20 64 65 66 STYLE).# if def
d95a0 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a ined(__APPLE__).
d95b0 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 # define SQLI
d95c0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
d95d0 47 5f 53 54 59 4c 45 20 31 0a 23 20 20 65 6c 73 G_STYLE 1.# els
d95e0 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 e.# define SQ
d95f0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
d9600 49 4e 47 5f 53 54 59 4c 45 20 30 0a 23 20 20 65 ING_STYLE 0.# e
d9610 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a ndif.#endif../*.
d9620 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20 4f 53 ** Define the OS
d9630 5f 56 58 57 4f 52 4b 53 20 70 72 65 2d 70 72 6f _VXWORKS pre-pro
d9640 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 74 6f 20 cessor macro to
d9650 31 20 69 66 20 62 75 69 6c 64 69 6e 67 20 6f 6e 1 if building on
d9660 20 0a 2a 2a 20 76 78 77 6f 72 6b 73 2c 20 6f 72 .** vxworks, or
d9670 20 30 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0 otherwise..*/
d9680 0a 23 69 66 6e 64 65 66 20 4f 53 5f 56 58 57 4f .#ifndef OS_VXWO
d9690 52 4b 53 0a 23 20 20 69 66 20 64 65 66 69 6e 65 RKS.# if define
d96a0 64 28 5f 5f 52 54 50 5f 5f 29 20 7c 7c 20 64 65 d(__RTP__) || de
d96b0 66 69 6e 65 64 28 5f 57 52 53 5f 4b 45 52 4e 45 fined(_WRS_KERNE
d96c0 4c 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20 4f L).# define O
d96d0 53 5f 56 58 57 4f 52 4b 53 20 31 0a 23 20 20 65 S_VXWORKS 1.# e
d96e0 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 lse.# define
d96f0 4f 53 5f 56 58 57 4f 52 4b 53 20 30 0a 23 20 20 OS_VXWORKS 0.#
d9700 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a endif.#endif../*
d9710 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66 69 6e .** These #defin
d9720 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62 6c 65 es should enable
d9730 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70 70 6f >2GB file suppo
d9740 72 74 20 6f 6e 20 50 6f 73 69 78 20 69 66 20 74 rt on Posix if t
d9750 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 he.** underlying
d9760 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
d9770 6d 20 73 75 70 70 6f 72 74 73 20 69 74 2e 20 20 m supports it.
d9780 49 66 20 74 68 65 20 4f 53 20 6c 61 63 6b 73 0a If the OS lacks.
d9790 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 20 73 75 ** large file su
d97a0 70 70 6f 72 74 2c 20 74 68 65 73 65 20 73 68 6f pport, these sho
d97b0 75 6c 64 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a uld be no-ops..*
d97c0 2a 0a 2a 2a 20 4c 61 72 67 65 20 66 69 6c 65 20 *.** Large file
d97d0 73 75 70 70 6f 72 74 20 63 61 6e 20 62 65 20 64 support can be d
d97e0 69 73 61 62 6c 65 64 20 75 73 69 6e 67 20 74 68 isabled using th
d97f0 65 20 2d 44 53 51 4c 49 54 45 5f 44 49 53 41 42 e -DSQLITE_DISAB
d9800 4c 45 5f 4c 46 53 20 73 77 69 74 63 68 0a 2a 2a LE_LFS switch.**
d9810 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 on the compiler
d9820 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 20 20 command line.
d9830 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 This is necessar
d9840 79 20 69 66 20 79 6f 75 20 61 72 65 20 63 6f 6d y if you are com
d9850 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 61 20 72 piling.** on a r
d9860 65 63 65 6e 74 20 6d 61 63 68 69 6e 65 20 28 65 ecent machine (e
d9870 78 3a 20 52 65 64 48 61 74 20 37 2e 32 29 20 62 x: RedHat 7.2) b
d9880 75 74 20 79 6f 75 20 77 61 6e 74 20 79 6f 75 72 ut you want your
d9890 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a code to work.**
d98a0 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20 6d 61 63 on an older mac
d98b0 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74 hine (ex: RedHat
d98c0 20 36 2e 30 29 2e 20 20 49 66 20 79 6f 75 20 63 6.0). If you c
d98d0 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 64 48 61 74 ompile on RedHat
d98e0 20 37 2e 32 0a 2a 2a 20 77 69 74 68 6f 75 74 20 7.2.** without
d98f0 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 this option, LFS
d9900 20 69 73 20 65 6e 61 62 6c 65 2e 20 20 42 75 74 is enable. But
d9910 20 4c 46 53 20 64 6f 65 73 20 6e 6f 74 20 65 78 LFS does not ex
d9920 69 73 74 20 69 6e 20 74 68 65 20 6b 65 72 6e 65 ist in the kerne
d9930 6c 0a 2a 2a 20 69 6e 20 52 65 64 48 61 74 20 36 l.** in RedHat 6
d9940 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f 64 65 20 .0, so the code
d9950 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e won't work. Hen
d9960 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 ce, for maximum
d9970 62 69 6e 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 binary.** portab
d9980 69 6c 69 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 ility you should
d9990 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a omit LFS..**.**
d99a0 20 54 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 The previous pa
d99b0 72 61 67 72 61 70 68 20 77 61 73 20 77 72 69 74 ragraph was writ
d99c0 74 65 6e 20 69 6e 20 32 30 30 35 2e 20 20 28 54 ten in 2005. (T
d99d0 68 69 73 20 70 61 72 61 67 72 61 70 68 20 69 73 his paragraph is
d99e0 20 77 72 69 74 74 65 6e 0a 2a 2a 20 6f 6e 20 32 written.** on 2
d99f0 30 30 38 2d 31 31 2d 32 38 2e 29 20 54 68 65 73 008-11-28.) Thes
d9a00 65 20 64 61 79 73 2c 20 61 6c 6c 20 4c 69 6e 75 e days, all Linu
d9a10 78 20 6b 65 72 6e 65 6c 73 20 73 75 70 70 6f 72 x kernels suppor
d9a20 74 20 6c 61 72 67 65 20 66 69 6c 65 73 2c 20 73 t large files, s
d9a30 6f 0a 2a 2a 20 79 6f 75 20 73 68 6f 75 6c 64 20 o.** you should
d9a40 70 72 6f 62 61 62 6c 79 20 6c 65 61 76 65 20 4c probably leave L
d9a50 46 53 20 65 6e 61 62 6c 65 64 2e 20 20 42 75 74 FS enabled. But
d9a60 20 73 6f 6d 65 20 65 6d 62 65 64 64 65 64 20 70 some embedded p
d9a70 6c 61 74 66 6f 72 6d 73 20 6d 69 67 68 74 0a 2a latforms might.*
d9a80 2a 20 6c 61 63 6b 20 4c 46 53 20 69 6e 20 77 68 * lack LFS in wh
d9a90 69 63 68 20 63 61 73 65 20 74 68 65 20 53 51 4c ich case the SQL
d9aa0 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 ITE_DISABLE_LFS
d9ab0 6d 61 63 72 6f 20 6d 69 67 68 74 20 73 74 69 6c macro might stil
d9ac0 6c 20 62 65 20 75 73 65 66 75 6c 2e 0a 2a 2f 0a l be useful..*/.
d9ad0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 #ifndef SQLITE_D
d9ae0 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 ISABLE_LFS.# def
d9af0 69 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 ine _LARGE_FILE
d9b00 20 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 1.# ifndef
d9b10 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 _FILE_OFFSET_BI
d9b20 54 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 TS.# define _F
d9b30 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 ILE_OFFSET_BITS
d9b40 36 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 64.# endif.# def
d9b50 69 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 ine _LARGEFILE_S
d9b60 4f 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a OURCE 1.#endif..
d9b70 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61 72 64 20 69 /*.** standard i
d9b80 6e 63 6c 75 64 65 20 66 69 6c 65 73 2e 0a 2a 2f nclude files..*/
d9b90 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74 .#include <sys/t
d9ba0 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 ypes.h>.#include
d9bb0 20 3c 73 79 73 2f 73 74 61 74 2e 68 3e 0a 23 69 <sys/stat.h>.#i
d9bc0 6e 63 6c 75 64 65 20 3c 66 63 6e 74 6c 2e 68 3e nclude <fcntl.h>
d9bd0 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 73 74 .#include <unist
d9be0 64 2e 68 3e 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 d.h>./* #include
d9bf0 20 3c 74 69 6d 65 2e 68 3e 20 2a 2f 0a 23 69 6e <time.h> */.#in
d9c00 63 6c 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e clude <sys/time.
d9c10 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 h>.#include <err
d9c20 6e 6f 2e 68 3e 0a 23 69 66 6e 64 65 66 20 53 51 no.h>.#ifndef SQ
d9c30 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 23 69 LITE_OMIT_WAL.#i
d9c40 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d 6d 61 6e nclude <sys/mman
d9c50 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 .h>.#endif...#if
d9c60 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
d9c70 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 23 20 69 OCKING_STYLE.# i
d9c80 6e 63 6c 75 64 65 20 3c 73 79 73 2f 69 6f 63 74 nclude <sys/ioct
d9c90 6c 2e 68 3e 0a 23 20 69 66 20 4f 53 5f 56 58 57 l.h>.# if OS_VXW
d9ca0 4f 52 4b 53 0a 23 20 20 69 6e 63 6c 75 64 65 20 ORKS.# include
d9cb0 3c 73 65 6d 61 70 68 6f 72 65 2e 68 3e 0a 23 20 <semaphore.h>.#
d9cc0 20 69 6e 63 6c 75 64 65 20 3c 6c 69 6d 69 74 73 include <limits
d9cd0 2e 68 3e 0a 23 20 65 6c 73 65 0a 23 20 20 69 6e .h>.# else.# in
d9ce0 63 6c 75 64 65 20 3c 73 79 73 2f 66 69 6c 65 2e clude <sys/file.
d9cf0 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 h>.# include <s
d9d00 79 73 2f 70 61 72 61 6d 2e 68 3e 0a 23 20 65 6e ys/param.h>.# en
d9d10 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 dif.#endif /* SQ
d9d20 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
d9d30 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 23 69 ING_STYLE */..#i
d9d40 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c f defined(__APPL
d9d50 45 5f 5f 29 20 7c 7c 20 28 53 51 4c 49 54 45 5f E__) || (SQLITE_
d9d60 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
d9d70 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f TYLE && !OS_VXWO
d9d80 52 4b 53 29 0a 23 20 69 6e 63 6c 75 64 65 20 3c RKS).# include <
d9d90 73 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 65 6e sys/mount.h>.#en
d9da0 64 69 66 0a 0a 23 69 66 64 65 66 20 48 41 56 45 dif..#ifdef HAVE
d9db0 5f 55 54 49 4d 45 0a 23 20 69 6e 63 6c 75 64 65 _UTIME.# include
d9dc0 20 3c 75 74 69 6d 65 2e 68 3e 0a 23 65 6e 64 69 <utime.h>.#endi
d9dd0 66 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 f../*.** Allowed
d9de0 20 76 61 6c 75 65 73 20 6f 66 20 75 6e 69 78 46 values of unixF
d9df0 69 6c 65 2e 66 73 46 6c 61 67 73 0a 2a 2f 0a 23 ile.fsFlags.*/.#
d9e00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 53 define SQLITE_FS
d9e10 46 4c 41 47 53 5f 49 53 5f 4d 53 44 4f 53 20 20 FLAGS_IS_MSDOS
d9e20 20 20 20 30 78 31 0a 0a 2f 2a 0a 2a 2a 20 49 66 0x1../*.** If
d9e30 20 77 65 20 61 72 65 20 74 6f 20 62 65 20 74 68 we are to be th
d9e40 72 65 61 64 2d 73 61 66 65 2c 20 69 6e 63 6c 75 read-safe, inclu
d9e50 64 65 20 74 68 65 20 70 74 68 72 65 61 64 73 20 de the pthreads
d9e60 68 65 61 64 65 72 20 61 6e 64 20 64 65 66 69 6e header and defin
d9e70 65 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f e.** the SQLITE_
d9e80 55 4e 49 58 5f 54 48 52 45 41 44 53 20 6d 61 63 UNIX_THREADS mac
d9e90 72 6f 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ro..*/.#if SQLIT
d9ea0 45 5f 54 48 52 45 41 44 53 41 46 45 0a 2f 2a 20 E_THREADSAFE./*
d9eb0 23 20 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 65 # include <pthre
d9ec0 61 64 2e 68 3e 20 2a 2f 0a 23 20 64 65 66 69 6e ad.h> */.# defin
d9ed0 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 e SQLITE_UNIX_TH
d9ee0 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a READS 1.#endif..
d9ef0 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 /*.** Default pe
d9f00 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 rmissions when c
d9f10 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 66 69 reating a new fi
d9f20 6c 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 le.*/.#ifndef SQ
d9f30 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c LITE_DEFAULT_FIL
d9f40 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 E_PERMISSIONS.#
d9f50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
d9f60 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 FAULT_FILE_PERMI
d9f70 53 53 49 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64 SSIONS 0644.#end
d9f80 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c if../*.** Defaul
d9f90 74 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 77 68 t permissions wh
d9fa0 65 6e 20 63 72 65 61 74 69 6e 67 20 61 75 74 6f en creating auto
d9fb0 20 70 72 6f 78 79 20 64 69 72 0a 2a 2f 0a 23 69 proxy dir.*/.#i
d9fc0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 fndef SQLITE_DEF
d9fd0 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 AULT_PROXYDIR_PE
d9fe0 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 RMISSIONS.# defi
d9ff0 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ne SQLITE_DEFAUL
da000 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 T_PROXYDIR_PERMI
da010 53 53 49 4f 4e 53 20 30 37 35 35 0a 23 65 6e 64 SSIONS 0755.#end
da020 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 if../*.** Maximu
da030 6d 20 73 75 70 70 6f 72 74 65 64 20 70 61 74 68 m supported path
da040 2d 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 -length..*/.#def
da050 69 6e 65 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 ine MAX_PATHNAME
da060 20 35 31 32 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 512../*.** Only
da070 20 73 65 74 20 74 68 65 20 6c 61 73 74 45 72 72 set the lastErr
da080 6e 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 no if the error
da090 63 6f 64 65 20 69 73 20 61 20 72 65 61 6c 20 65 code is a real e
da0a0 72 72 6f 72 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a rror and not .**
da0b0 20 61 20 6e 6f 72 6d 61 6c 20 65 78 70 65 63 74 a normal expect
da0c0 65 64 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f ed return code o
da0d0 66 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 72 f SQLITE_BUSY or
da0e0 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 SQLITE_OK.*/.#d
da0f0 65 66 69 6e 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 efine IS_LOCK_ER
da100 52 4f 52 28 78 29 20 20 28 28 78 20 21 3d 20 53 ROR(x) ((x != S
da110 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28 78 20 QLITE_OK) && (x
da120 21 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 29 29 != SQLITE_BUSY))
da130 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 ../* Forward ref
da140 65 72 65 6e 63 65 73 20 2a 2f 0a 74 79 70 65 64 erences */.typed
da150 65 66 20 73 74 72 75 63 74 20 75 6e 69 78 53 68 ef struct unixSh
da160 6d 20 75 6e 69 78 53 68 6d 3b 20 20 20 20 20 20 m unixShm;
da170 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e /* Conn
da180 65 63 74 69 6f 6e 20 73 68 61 72 65 64 20 6d 65 ection shared me
da190 6d 6f 72 79 20 2a 2f 0a 74 79 70 65 64 65 66 20 mory */.typedef
da1a0 73 74 72 75 63 74 20 75 6e 69 78 53 68 6d 4e 6f struct unixShmNo
da1b0 64 65 20 75 6e 69 78 53 68 6d 4e 6f 64 65 3b 20 de unixShmNode;
da1c0 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 /* Shared
da1d0 6d 65 6d 6f 72 79 20 69 6e 73 74 61 6e 63 65 20 memory instance
da1e0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
da1f0 74 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 t unixInodeInfo
da200 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 3b 20 20 unixInodeInfo;
da210 20 2f 2a 20 41 6e 20 69 2d 6e 6f 64 65 20 2a 2f /* An i-node */
da220 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
da230 55 6e 69 78 55 6e 75 73 65 64 46 64 20 55 6e 69 UnixUnusedFd Uni
da240 78 55 6e 75 73 65 64 46 64 3b 20 20 20 20 20 2f xUnusedFd; /
da250 2a 20 41 6e 20 75 6e 75 73 65 64 20 66 69 6c 65 * An unused file
da260 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 0a descriptor */..
da270 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 2c /*.** Sometimes,
da280 20 61 66 74 65 72 20 61 20 66 69 6c 65 20 68 61 after a file ha
da290 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 62 ndle is closed b
da2a0 79 20 53 51 4c 69 74 65 2c 20 74 68 65 20 66 69 y SQLite, the fi
da2b0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a le descriptor.**
da2c0 20 63 61 6e 6e 6f 74 20 62 65 20 63 6c 6f 73 65 cannot be close
da2d0 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 d immediately. I
da2e0 6e 20 74 68 65 73 65 20 63 61 73 65 73 2c 20 69 n these cases, i
da2f0 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20 nstances of the
da300 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 following.** str
da310 75 63 74 75 72 65 20 61 72 65 20 75 73 65 64 20 ucture are used
da320 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c to store the fil
da330 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 68 69 e descriptor whi
da340 6c 65 20 77 61 69 74 69 6e 67 20 66 6f 72 20 61 le waiting for a
da350 6e 0a 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 n.** opportunity
da360 20 74 6f 20 65 69 74 68 65 72 20 63 6c 6f 73 65 to either close
da370 20 6f 72 20 72 65 75 73 65 20 69 74 2e 0a 2a 2f or reuse it..*/
da380 0a 73 74 72 75 63 74 20 55 6e 69 78 55 6e 75 73 .struct UnixUnus
da390 65 64 46 64 20 7b 0a 20 20 69 6e 74 20 66 64 3b edFd {. int fd;
da3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
da3b0 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 /* File descr
da3c0 69 70 74 6f 72 20 74 6f 20 63 6c 6f 73 65 20 2a iptor to close *
da3d0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 /. int flags;
da3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
da3f0 20 46 6c 61 67 73 20 74 68 69 73 20 66 69 6c 65 Flags this file
da400 20 64 65 73 63 72 69 70 74 6f 72 20 77 61 73 20 descriptor was
da410 6f 70 65 6e 65 64 20 77 69 74 68 20 2a 2f 0a 20 opened with */.
da420 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 UnixUnusedFd *p
da430 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 Next; /* Ne
da440 78 74 20 75 6e 75 73 65 64 20 66 69 6c 65 20 64 xt unused file d
da450 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 73 61 6d escriptor on sam
da460 65 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a e file */.};../*
da470 0a 2a 2a 20 54 68 65 20 75 6e 69 78 46 69 6c 65 .** The unixFile
da480 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73 75 structure is su
da490 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 bclass of sqlite
da4a0 33 5f 66 69 6c 65 20 73 70 65 63 69 66 69 63 20 3_file specific
da4b0 74 6f 20 74 68 65 20 75 6e 69 78 0a 2a 2a 20 56 to the unix.** V
da4c0 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f FS implementatio
da4d0 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ns..*/.typedef s
da4e0 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20 75 truct unixFile u
da4f0 6e 69 78 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 nixFile;.struct
da500 75 6e 69 78 46 69 6c 65 20 7b 0a 20 20 73 71 6c unixFile {. sql
da510 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
da520 63 6f 6e 73 74 20 2a 70 4d 65 74 68 6f 64 3b 20 const *pMethod;
da530 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 /* Always the f
da540 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 irst entry */.
da550 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
da560 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
da570 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74 /* The VFS t
da580 68 61 74 20 63 72 65 61 74 65 64 20 74 68 69 73 hat created this
da590 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 20 20 75 unixFile */. u
da5a0 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 nixInodeInfo *pI
da5b0 6e 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 node;
da5c0 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74 /* Info about
da5d0 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 69 73 20 69 locks on this i
da5e0 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 3b node */. int h;
da5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
da600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
da610 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 The file descri
da620 70 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e ptor */. unsign
da630 65 64 20 63 68 61 72 20 65 46 69 6c 65 4c 6f 63 ed char eFileLoc
da640 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a k; /*
da650 20 54 68 65 20 74 79 70 65 20 6f 66 20 6c 6f 63 The type of loc
da660 6b 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 k held on this f
da670 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 d */. unsigned
da680 73 68 6f 72 74 20 69 6e 74 20 63 74 72 6c 46 6c short int ctrlFl
da690 61 67 73 3b 20 20 20 20 20 20 20 2f 2a 20 42 65 ags; /* Be
da6a0 68 61 76 69 6f 72 61 6c 20 62 69 74 73 2e 20 20 havioral bits.
da6b0 55 4e 49 58 46 49 4c 45 5f 2a 20 66 6c 61 67 73 UNIXFILE_* flags
da6c0 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 73 74 45 72 */. int lastEr
da6d0 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 rno;
da6e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
da6f0 20 75 6e 69 78 20 65 72 72 6e 6f 20 66 72 6f 6d unix errno from
da700 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 last I/O error
da710 2a 2f 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 */. void *locki
da720 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 ngContext;
da730 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b /* Lock
da740 69 6e 67 20 73 74 79 6c 65 20 73 70 65 63 69 66 ing style specif
da750 69 63 20 73 74 61 74 65 20 2a 2f 0a 20 20 55 6e ic state */. Un
da760 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 ixUnusedFd *pUnu
da770 73 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 sed;
da780 20 20 2f 2a 20 50 72 65 2d 61 6c 6c 6f 63 61 74 /* Pre-allocat
da790 65 64 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 ed UnixUnusedFd
da7a0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
da7b0 2a 7a 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 *zPath;
da7c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
da7d0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a of the file */.
da7e0 20 20 75 6e 69 78 53 68 6d 20 2a 70 53 68 6d 3b unixShm *pShm;
da7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
da800 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 /* Shared
da810 6d 65 6d 6f 72 79 20 73 65 67 6d 65 6e 74 20 69 memory segment i
da820 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 nformation */.
da830 69 6e 74 20 73 7a 43 68 75 6e 6b 3b 20 20 20 20 int szChunk;
da840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
da850 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 /* Configure
da860 64 20 62 79 20 46 43 4e 54 4c 5f 43 48 55 4e 4b d by FCNTL_CHUNK
da870 5f 53 49 5a 45 20 2a 2f 0a 23 69 66 64 65 66 20 _SIZE */.#ifdef
da880 5f 5f 51 4e 58 4e 54 4f 5f 5f 0a 20 20 69 6e 74 __QNXNTO__. int
da890 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 sectorSize;
da8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
da8b0 20 2f 2a 20 44 65 76 69 63 65 20 73 65 63 74 6f /* Device secto
da8c0 72 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 r size */. int
da8d0 64 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 deviceCharacteri
da8e0 73 74 69 63 73 3b 20 20 20 20 20 20 20 20 20 20 stics;
da8f0 2f 2a 20 50 72 65 63 6f 6d 70 75 74 65 64 20 64 /* Precomputed d
da900 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 evice characteri
da910 73 74 69 63 73 20 2a 2f 0a 23 65 6e 64 69 66 0a stics */.#endif.
da920 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c #if SQLITE_ENABL
da930 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
da940 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 3b int openFlags;
da950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
da960 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61 /* The fla
da970 67 73 20 73 70 65 63 69 66 69 65 64 20 61 74 20 gs specified at
da980 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 6e 64 69 66 open() */.#endif
da990 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
da9a0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
da9b0 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 || defined(__AP
da9c0 50 4c 45 5f 5f 29 0a 20 20 75 6e 73 69 67 6e 65 PLE__). unsigne
da9d0 64 20 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20 d fsFlags;
da9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
da9f0 63 61 63 68 65 64 20 64 65 74 61 69 6c 73 20 66 cached details f
daa00 72 6f 6d 20 73 74 61 74 66 73 28 29 20 2a 2f 0a rom statfs() */.
daa10 23 65 6e 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 #endif.#if OS_VX
daa20 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 76 WORKS. struct v
daa30 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 xworksFileId *pI
daa40 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 d; /* U
daa50 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 2a 2f nique file ID */
daa60 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
daa70 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a QLITE_DEBUG. /*
daa80 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 The next group
daa90 6f 66 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 of variables are
daaa0 20 75 73 65 64 20 74 6f 20 74 72 61 63 6b 20 77 used to track w
daab0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
daac0 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 e. ** transacti
daad0 6f 6e 20 63 6f 75 6e 74 65 72 20 69 6e 20 62 79 on counter in by
daae0 74 65 73 20 32 34 2d 32 37 20 6f 66 20 64 61 74 tes 24-27 of dat
daaf0 61 62 61 73 65 20 66 69 6c 65 73 20 61 72 65 20 abase files are
dab00 75 70 64 61 74 65 64 0a 20 20 2a 2a 20 77 68 65 updated. ** whe
dab10 6e 65 76 65 72 20 61 6e 79 20 70 61 72 74 20 6f never any part o
dab20 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 f the database c
dab30 68 61 6e 67 65 73 2e 20 20 41 6e 20 61 73 73 65 hanges. An asse
dab40 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c rtion fault will
dab50 0a 20 20 2a 2a 20 6f 63 63 75 72 20 69 66 20 61 . ** occur if a
dab60 20 66 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 file is updated
dab70 20 77 69 74 68 6f 75 74 20 61 6c 73 6f 20 75 70 without also up
dab80 64 61 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73 dating the trans
dab90 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 6f 75 6e action. ** coun
daba0 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 74 20 ter. This test
dabb0 69 73 20 6d 61 64 65 20 74 6f 20 61 76 6f 69 64 is made to avoid
dabc0 20 6e 65 77 20 70 72 6f 62 6c 65 6d 73 20 73 69 new problems si
dabd0 6d 69 6c 61 72 20 74 6f 20 74 68 65 0a 20 20 2a milar to the. *
dabe0 2a 20 6f 6e 65 20 64 65 73 63 72 69 62 65 64 20 * one described
dabf0 62 79 20 74 69 63 6b 65 74 20 23 33 35 38 34 2e by ticket #3584.
dac00 20 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 . */. unsigne
dac10 64 20 63 68 61 72 20 74 72 61 6e 73 43 6e 74 72 d char transCntr
dac20 43 68 6e 67 3b 20 20 20 2f 2a 20 54 72 75 65 20 Chng; /* True
dac30 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 if the transacti
dac40 6f 6e 20 63 6f 75 6e 74 65 72 20 63 68 61 6e 67 on counter chang
dac50 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 ed */. unsigned
dac60 20 63 68 61 72 20 64 62 55 70 64 61 74 65 3b 20 char dbUpdate;
dac70 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
dac80 66 20 61 6e 79 20 70 61 72 74 20 6f 66 20 64 61 f any part of da
dac90 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e tabase file chan
daca0 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ged */. unsigne
dacb0 64 20 63 68 61 72 20 69 6e 4e 6f 72 6d 61 6c 57 d char inNormalW
dacc0 72 69 74 65 3b 20 20 20 2f 2a 20 54 72 75 65 20 rite; /* True
dacd0 69 66 20 69 6e 20 61 20 6e 6f 72 6d 61 6c 20 77 if in a normal w
dace0 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 2a rite operation *
dacf0 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 /.#endif.#ifdef
dad00 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a SQLITE_TEST. /*
dad10 20 49 6e 20 74 65 73 74 20 6d 6f 64 65 2c 20 69 In test mode, i
dad20 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 ncrease the size
dad30 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
dad40 72 65 20 61 20 62 69 74 20 73 6f 20 74 68 61 74 re a bit so that
dad50 20 0a 20 20 2a 2a 20 69 74 20 69 73 20 6c 61 72 . ** it is lar
dad60 67 65 72 20 74 68 61 6e 20 74 68 65 20 73 74 72 ger than the str
dad70 75 63 74 20 43 72 61 73 68 46 69 6c 65 20 64 65 uct CrashFile de
dad80 66 69 6e 65 64 20 69 6e 20 74 65 73 74 36 2e 63 fined in test6.c
dad90 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 61 50 .. */. char aP
dada0 61 64 64 69 6e 67 5b 33 32 5d 3b 0a 23 65 6e 64 adding[32];.#end
dadb0 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c if.};../*.** All
dadc0 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 owed values for
dadd0 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 63 74 72 the unixFile.ctr
dade0 6c 46 6c 61 67 73 20 62 69 74 6d 61 73 6b 3a 0a lFlags bitmask:.
dadf0 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 49 58 46 */.#define UNIXF
dae00 49 4c 45 5f 45 58 43 4c 20 20 20 20 20 20 20 20 ILE_EXCL
dae10 30 78 30 31 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 0x01 /* Conn
dae20 65 63 74 69 6f 6e 73 20 66 72 6f 6d 20 6f 6e 65 ections from one
dae30 20 70 72 6f 63 65 73 73 20 6f 6e 6c 79 20 2a 2f process only */
dae40 0a 23 64 65 66 69 6e 65 20 55 4e 49 58 46 49 4c .#define UNIXFIL
dae50 45 5f 52 44 4f 4e 4c 59 20 20 20 20 20 20 30 78 E_RDONLY 0x
dae60 30 32 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 02 /* Connec
dae70 74 69 6f 6e 20 69 73 20 72 65 61 64 20 6f 6e 6c tion is read onl
dae80 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 49 y */.#define UNI
dae90 58 46 49 4c 45 5f 50 45 52 53 49 53 54 5f 57 41 XFILE_PERSIST_WA
daea0 4c 20 30 78 30 34 20 20 20 20 20 2f 2a 20 50 65 L 0x04 /* Pe
daeb0 72 73 69 73 74 65 6e 74 20 57 41 4c 20 6d 6f 64 rsistent WAL mod
daec0 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c e */.#ifndef SQL
daed0 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 ITE_DISABLE_DIRS
daee0 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 55 4e 49 YNC.# define UNI
daef0 58 46 49 4c 45 5f 44 49 52 53 59 4e 43 20 20 20 XFILE_DIRSYNC
daf00 20 30 78 30 38 20 20 20 20 20 2f 2a 20 44 69 72 0x08 /* Dir
daf10 65 63 74 6f 72 79 20 73 79 6e 63 20 6e 65 65 64 ectory sync need
daf20 65 64 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 64 65 ed */.#else.# de
daf30 66 69 6e 65 20 55 4e 49 58 46 49 4c 45 5f 44 49 fine UNIXFILE_DI
daf40 52 53 59 4e 43 20 20 20 20 30 78 30 30 0a 23 65 RSYNC 0x00.#e
daf50 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 55 4e 49 ndif.#define UNI
daf60 58 46 49 4c 45 5f 50 53 4f 57 20 20 20 20 20 20 XFILE_PSOW
daf70 20 20 30 78 31 30 20 20 20 20 20 2f 2a 20 53 51 0x10 /* SQ
daf80 4c 49 54 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52 LITE_IOCAP_POWER
daf90 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 20 2a SAFE_OVERWRITE *
dafa0 2f 0a 23 64 65 66 69 6e 65 20 55 4e 49 58 46 49 /.#define UNIXFI
dafb0 4c 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 30 LE_DELETE 0
dafc0 78 32 30 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 x20 /* Delet
dafd0 65 20 6f 6e 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 e on close */.#d
dafe0 65 66 69 6e 65 20 55 4e 49 58 46 49 4c 45 5f 55 efine UNIXFILE_U
daff0 52 49 20 20 20 20 20 20 20 20 20 30 78 34 30 20 RI 0x40
db000 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 /* Filename
db010 6d 69 67 68 74 20 68 61 76 65 20 71 75 65 72 79 might have query
db020 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 23 parameters */.#
db030 64 65 66 69 6e 65 20 55 4e 49 58 46 49 4c 45 5f define UNIXFILE_
db040 4e 4f 4c 4f 43 4b 20 20 20 20 20 20 30 78 38 30 NOLOCK 0x80
db050 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 20 66 69 /* Do no fi
db060 6c 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 2f le locking */../
db070 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 *.** Include cod
db080 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e e that is common
db090 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 to all os_*.c f
db0a0 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a iles.*/./*******
db0b0 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
db0c0 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 os_common.h in t
db0d0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f he middle of os_
db0e0 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a unix.c *********
db0f0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
db100 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
db110 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a le os_common.h *
db120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db140 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
db150 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 04 May 22.**.**
db160 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
db170 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
db180 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
db190 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
db1a0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
db1b0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
db1c0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
db1d0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
db1e0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
db1f0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
db200 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
db210 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
db220 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
db230 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
db240 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
db250 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
db260 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
db270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
db2c0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
db2d0 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e ntains macros an
db2e0 64 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f d a little bit o
db2f0 66 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 f code that is c
db300 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 ommon to.** all
db310 6f 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d of the platform-
db320 73 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 specific files (
db330 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 os_*.c) and is #
db340 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 included into th
db350 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a ose.** files..**
db360 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 .** This file sh
db370 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 ould be #include
db380 64 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 d by the os_*.c
db390 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 files only. It
db3a0 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 is not a.** gene
db3b0 72 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 ral purpose head
db3c0 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e er file..*/.#ifn
db3d0 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 def _OS_COMMON_H
db3e0 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f _.#define _OS_CO
db3f0 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 MMON_H_../*.** A
db400 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 t least two bugs
db410 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e have slipped in
db420 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61 6e because we chan
db430 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 ged the MEMORY_D
db440 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f EBUG.** macro to
db450 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e SQLITE_DEBUG an
db460 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b d some older mak
db470 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 efiles have not
db480 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 yet made the.**
db490 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c switch. The fol
db4a0 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 lowing code shou
db4b0 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70 72 ld catch this pr
db4c0 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 oblem at compile
db4d0 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 -time..*/.#ifdef
db4e0 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 MEMORY_DEBUG.#
db4f0 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 error "The MEMOR
db500 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 Y_DEBUG macro is
db510 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 obsolete. Use
db520 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 SQLITE_DEBUG ins
db530 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 tead.".#endif..#
db540 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
db550 45 5f 54 45 53 54 29 20 26 26 20 64 65 66 69 6e E_TEST) && defin
db560 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
db570 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 .# ifndef SQLITE
db580 5f 44 45 42 55 47 5f 4f 53 5f 54 52 41 43 45 0a _DEBUG_OS_TRACE.
db590 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 # define SQLIT
db5a0 45 5f 44 45 42 55 47 5f 4f 53 5f 54 52 41 43 45 E_DEBUG_OS_TRACE
db5b0 20 30 0a 23 20 65 6e 64 69 66 0a 20 20 69 6e 74 0.# endif. int
db5c0 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 sqlite3OSTrace
db5d0 3d 20 53 51 4c 49 54 45 5f 44 45 42 55 47 5f 4f = SQLITE_DEBUG_O
db5e0 53 5f 54 52 41 43 45 3b 0a 23 20 64 65 66 69 6e S_TRACE;.# defin
db5f0 65 20 4f 53 54 52 41 43 45 28 58 29 20 20 20 20 e OSTRACE(X)
db600 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
db610 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 3OSTrace ) sqlit
db620 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 58 0a e3DebugPrintf X.
db630 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 4f #else.# define O
db640 53 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 STRACE(X).#endif
db650 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 ../*.** Macros f
db660 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 or performance t
db670 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c racing. Normall
db680 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f y turned off. O
db690 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 nly works.** on
db6a0 69 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a i486 hardware..*
db6b0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
db6c0 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 PERFORMANCE_TRAC
db6d0 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 E../* .** hwtime
db6e0 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 .h contains inli
db6f0 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 ne assembler cod
db700 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 e for implementi
db710 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 ng .** high-perf
db720 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 ormance timing r
db730 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a outines..*/./***
db740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
db750 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 ude hwtime.h in
db760 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 the middle of os
db770 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a _common.h ******
db780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
db790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
db7a0 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 n file hwtime.h
db7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
db7e0 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a * 2008 May 27.**
db7f0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
db800 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
db810 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
db820 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
db830 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
db840 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
db850 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
db860 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
db870 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
db880 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
db890 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
db8a0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
db8b0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
db8c0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
db8d0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
db8e0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
db8f0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
db900 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
db910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
db950 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
db960 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e e contains inlin
db970 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 e asm code for r
db980 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d etrieving "high-
db990 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 performance".**
db9a0 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 counters for x86
db9b0 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2f 0a class CPUs..*/.
db9c0 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f #ifndef _HWTIME_
db9d0 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 H_.#define _HWTI
db9e0 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ME_H_../*.** The
db9f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 following routi
dba00 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e ne only works on
dba10 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 pentium-class (
dba20 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 or newer) proces
dba30 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 sors..** It uses
dba40 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 the RDTSC opcod
dba50 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 e to read the cy
dba60 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 cle count value
dba70 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 out of the.** pr
dba80 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 ocessor and retu
dba90 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 rns that value.
dbaa0 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 This can be use
dbab0 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a d for high-res.*
dbac0 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a * profiling..*/.
dbad0 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 #if (defined(__G
dbae0 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 NUC__) || define
dbaf0 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 d(_MSC_VER)) &&
dbb00 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 \. (defined
dbb10 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 (i386) || define
dbb20 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 d(__i386__) || d
dbb30 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 efined(_M_IX86))
dbb40 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 .. #if defined(
dbb50 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 __GNUC__).. __i
dbb60 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
dbb70 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
dbb80 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
dbb90 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c unsigned int lo,
dbba0 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f hi;. __asm_
dbbb0 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
dbbc0 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 "rdtsc" : "=a" (
dbbd0 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b lo), "=d" (hi));
dbbe0 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 . return (sq
dbbf0 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c lite_uint64)hi <
dbc00 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a < 32 | lo;. }..
dbc10 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 #elif defined(
dbc20 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 _MSC_VER).. __d
dbc30 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f eclspec(naked) _
dbc40 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 _inline sqlite_u
dbc50 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 int64 __cdecl sq
dbc60 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
dbc70 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a ){. __asm {.
dbc80 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 rdtsc.
dbc90 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 ret
dbca0 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 ; return value a
dbcb0 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d t EDX:EAX. }
dbcc0 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a . }.. #endif..
dbcd0 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f #elif (defined(_
dbce0 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 _GNUC__) && defi
dbcf0 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 ned(__x86_64__))
dbd00 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
dbd10 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
dbd20 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
dbd30 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
dbd40 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 long val;.
dbd50 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
dbd60 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a ile__ ("rdtsc" :
dbd70 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 "=A" (val));.
dbd80 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a return val;.
dbd90 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 }. .#elif (def
dbda0 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 ined(__GNUC__) &
dbdb0 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f & defined(__ppc_
dbdc0 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f _)).. __inline_
dbdd0 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 _ sqlite_uint64
dbde0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
dbdf0 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 id){. unsig
dbe00 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 ned long long re
dbe10 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 tval;. unsi
dbe20 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a gned long junk;.
dbe30 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f __asm__ __
dbe40 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c volatile__ ("\n\
dbe50 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 . 1:
dbe60 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c mftbu %1\n\
dbe70 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
dbe80 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e mftb %L0\n
dbe90 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
dbea0 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e mftbu %0\n
dbeb0 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
dbec0 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 cmpw %0,%
dbed0 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 1\n\.
dbee0 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 bne 1
dbef0 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 b".
dbf00 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 : "=r" (ret
dbf10 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b val), "=r" (junk
dbf20 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ));. return
dbf30 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 retval;. }..#e
dbf40 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 lse.. #error Ne
dbf50 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ed implementatio
dbf60 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 n of sqlite3Hwti
dbf70 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c me() for your pl
dbf80 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 atform... /*.
dbf90 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 ** To compile wi
dbfa0 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 thout implementi
dbfb0 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 ng sqlite3Hwtime
dbfc0 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 () for your plat
dbfd0 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 form,. ** you c
dbfe0 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 an remove the ab
dbff0 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 ove #error and u
dc000 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 se the following
dc010 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 . ** stub funct
dc020 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c ion. You will l
dc030 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f ose timing suppo
dc040 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a rt for many. **
dc050 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e of the debuggin
dc060 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 g and testing ut
dc070 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 ilities, but it
dc080 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c should at. ** l
dc090 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 east compile and
dc0a0 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 run.. */.SQLIT
dc0b0 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 E_PRIVATE sqli
dc0c0 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
dc0d0 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 3Hwtime(void){ r
dc0e0 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 eturn ((sqlite_u
dc0f0 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e int64)0); }..#en
dc100 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 dif..#endif /* !
dc110 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f defined(_HWTIME_
dc120 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a H_) */../*******
dc130 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 ******* End of h
dc140 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
dc150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dc160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dc170 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
dc180 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
dc190 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
dc1a0 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f off in os_commo
dc1b0 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.h ************
dc1c0 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 ******/..static
dc1d0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f sqlite_uint64 g_
dc1e0 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 start;.static sq
dc1f0 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c lite_uint64 g_el
dc200 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 apsed;.#define T
dc210 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 IMER_START
dc220 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 g_start=sqlite3
dc230 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 Hwtime().#define
dc240 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 TIMER_END
dc250 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c g_elapsed=sql
dc260 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 ite3Hwtime()-g_s
dc270 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d tart.#define TIM
dc280 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 ER_ELAPSED g
dc290 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 _elapsed.#else.#
dc2a0 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 define TIMER_STA
dc2b0 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 RT.#define TIMER
dc2c0 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d _END.#define TIM
dc2d0 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 ER_ELAPSED (
dc2e0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 (sqlite_uint64)0
dc2f0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
dc300 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 If we compile wi
dc310 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 th the SQLITE_TE
dc320 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 ST macro set, th
dc330 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 en the following
dc340 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 block.** of cod
dc350 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 e will give us t
dc360 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 he ability to si
dc370 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f mulate a disk I/
dc380 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a O error. This.*
dc390 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 * is used for te
dc3a0 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 sting the I/O re
dc3b0 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f covery logic..*/
dc3c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
dc3d0 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
dc3e0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
dc3f0 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 ror_hit = 0;
dc400 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
dc410 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 number of I/O E
dc420 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f rrors */.SQLITE_
dc430 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
dc440 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 io_error_hardhit
dc450 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
dc460 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 Number of non-be
dc470 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 nign errors */.S
dc480 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
dc490 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
dc4a0 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 ending = 0;
dc4b0 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e /* Count down
dc4c0 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 to first I/O er
dc4d0 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ror */.SQLITE_AP
dc4e0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
dc4f0 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d _error_persist =
dc500 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
dc510 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 ue if I/O errors
dc520 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 persist */.SQLI
dc530 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
dc540 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 e3_io_error_beni
dc550 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 gn = 0;
dc560 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 /* True if error
dc570 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a s are benign */.
dc580 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
dc590 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
dc5a0 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c pending = 0;.SQL
dc5b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
dc5c0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 te3_diskfull = 0
dc5d0 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ;.#define Simula
dc5e0 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
dc5f0 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 X) sqlite3_io_er
dc600 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 ror_benign=(X).#
dc610 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 define SimulateI
dc620 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a OError(CODE) \.
dc630 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 if( (sqlite3_i
dc640 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 o_error_persist
dc650 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 && sqlite3_io_er
dc660 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 ror_hit) \.
dc670 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f || sqlite3_io_
dc680 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 error_pending--
dc690 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 == 1 ) \.
dc6a0 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f { local_
dc6b0 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d ioerr(); CODE; }
dc6c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 .static void loc
dc6d0 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f al_ioerr(){. IO
dc6e0 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 TRACE(("IOERR\n"
dc6f0 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f ));. sqlite3_io
dc700 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 _error_hit++;.
dc710 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f if( !sqlite3_io_
dc720 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 error_benign ) s
dc730 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
dc740 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 hardhit++;.}.#de
dc750 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 fine SimulateDis
dc760 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 kfullError(CODE)
dc770 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 \. if( sqlite
dc780 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
dc790 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 ng ){ \. if(
dc7a0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
dc7b0 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 l_pending == 1 )
dc7c0 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c { \. local
dc7d0 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 _ioerr(); \.
dc7e0 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 sqlite3_diskf
dc7f0 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 ull = 1; \.
dc800 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 sqlite3_io_err
dc810 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 or_hit = 1; \.
dc820 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 CODE; \.
dc830 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 }else{ \.
dc840 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 sqlite3_diskfu
dc850 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a ll_pending--; \.
dc860 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 } \. }.#e
dc870 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 lse.#define Simu
dc880 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
dc890 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d n(X).#define Sim
dc8a0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a ulateIOError(A).
dc8b0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
dc8c0 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 DiskfullError(A)
dc8d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
dc8e0 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 hen testing, kee
dc8f0 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 p a count of the
dc900 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 number of open
dc910 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 files..*/.#ifdef
dc920 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
dc930 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
dc940 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f te3_open_file_co
dc950 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 unt = 0;.#define
dc960 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 OpenCounter(X)
dc970 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 sqlite3_open_fi
dc980 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 le_count+=(X).#e
dc990 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e lse.#define Open
dc9a0 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 Counter(X).#endi
dc9b0 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 f..#endif /* !de
dc9c0 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e fined(_OS_COMMON
dc9d0 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a _H_) */../******
dc9e0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
dc9f0 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
dca00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dca10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dca20 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
dca30 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
dca40 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
dca50 74 20 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 t off in os_unix
dca60 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
dca70 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
dca80 44 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 6d Define various m
dca90 61 63 72 6f 73 20 74 68 61 74 20 61 72 65 20 6d acros that are m
dcaa0 69 73 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 issing from some
dcab0 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 systems..*/.#if
dcac0 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 ndef O_LARGEFILE
dcad0 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 .# define O_LARG
dcae0 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 EFILE 0.#endif.#
dcaf0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 ifdef SQLITE_DIS
dcb00 41 42 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 ABLE_LFS.# undef
dcb10 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 O_LARGEFILE.# d
dcb20 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c efine O_LARGEFIL
dcb30 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 E 0.#endif.#ifnd
dcb40 65 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 ef O_NOFOLLOW.#
dcb50 64 65 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f define O_NOFOLLO
dcb60 57 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 W 0.#endif.#ifnd
dcb70 65 66 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 ef O_BINARY.# de
dcb80 66 69 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a fine O_BINARY 0.
dcb90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
dcba0 65 20 74 68 72 65 61 64 69 64 20 6d 61 63 72 6f e threadid macro
dcbb0 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 74 68 65 resolves to the
dcbc0 20 74 68 72 65 61 64 2d 69 64 20 6f 72 20 74 6f thread-id or to
dcbd0 20 30 2e 20 20 55 73 65 64 20 66 6f 72 0a 2a 2a 0. Used for.**
dcbe0 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 testing and deb
dcbf0 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a ugging only..*/.
dcc00 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
dcc10 44 53 41 46 45 0a 23 64 65 66 69 6e 65 20 74 68 DSAFE.#define th
dcc20 72 65 61 64 69 64 20 70 74 68 72 65 61 64 5f 73 readid pthread_s
dcc30 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64 65 66 elf().#else.#def
dcc40 69 6e 65 20 74 68 72 65 61 64 69 64 20 30 0a 23 ine threadid 0.#
dcc50 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 69 66 endif../*.** Dif
dcc60 66 65 72 65 6e 74 20 55 6e 69 78 20 73 79 73 74 ferent Unix syst
dcc70 65 6d 73 20 64 65 63 6c 61 72 65 20 6f 70 65 6e ems declare open
dcc80 28 29 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 () in different
dcc90 77 61 79 73 2e 20 20 53 61 6d 65 20 75 73 65 0a ways. Same use.
dcca0 2a 2a 20 6f 70 65 6e 28 63 6f 6e 73 74 20 63 68 ** open(const ch
dccb0 61 72 2a 2c 69 6e 74 2c 6d 6f 64 65 5f 74 29 2e ar*,int,mode_t).
dccc0 20 20 4f 74 68 65 72 73 20 75 73 65 20 6f 70 65 Others use ope
dccd0 6e 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e n(const char*,in
dcce0 74 2c 2e 2e 2e 29 2e 0a 2a 2a 20 54 68 65 20 64 t,...)..** The d
dccf0 69 66 66 65 72 65 6e 63 65 20 69 73 20 69 6d 70 ifference is imp
dcd00 6f 72 74 61 6e 74 20 77 68 65 6e 20 75 73 69 6e ortant when usin
dcd10 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 g a pointer to t
dcd20 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a he function..**.
dcd30 2a 2a 20 54 68 65 20 73 61 66 65 73 74 20 77 61 ** The safest wa
dcd40 79 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74 y to deal with t
dcd50 68 65 20 70 72 6f 62 6c 65 6d 20 69 73 20 74 6f he problem is to
dcd60 20 61 6c 77 61 79 73 20 75 73 65 20 74 68 69 73 always use this
dcd70 20 77 72 61 70 70 65 72 0a 2a 2a 20 77 68 69 63 wrapper.** whic
dcd80 68 20 61 6c 77 61 79 73 20 68 61 73 20 74 68 65 h always has the
dcd90 20 73 61 6d 65 20 77 65 6c 6c 2d 64 65 66 69 6e same well-defin
dcda0 65 64 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f ed interface..*/
dcdb0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 73 69 .static int posi
dcdc0 78 4f 70 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 xOpen(const char
dcdd0 20 2a 7a 46 69 6c 65 2c 20 69 6e 74 20 66 6c 61 *zFile, int fla
dcde0 67 73 2c 20 69 6e 74 20 6d 6f 64 65 29 7b 0a 20 gs, int mode){.
dcdf0 20 72 65 74 75 72 6e 20 6f 70 65 6e 28 7a 46 69 return open(zFi
dce00 6c 65 2c 20 66 6c 61 67 73 2c 20 6d 6f 64 65 29 le, flags, mode)
dce10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 73 6f ;.}../*.** On so
dce20 6d 65 20 73 79 73 74 65 6d 73 2c 20 63 61 6c 6c me systems, call
dce30 73 20 74 6f 20 66 63 68 6f 77 6e 28 29 20 77 69 s to fchown() wi
dce40 6c 6c 20 74 72 69 67 67 65 72 20 61 20 6d 65 73 ll trigger a mes
dce50 73 61 67 65 20 69 6e 20 61 20 73 65 63 75 72 69 sage in a securi
dce60 74 79 0a 2a 2a 20 6c 6f 67 20 69 66 20 74 68 65 ty.** log if the
dce70 79 20 63 6f 6d 65 20 66 72 6f 6d 20 6e 6f 6e 2d y come from non-
dce80 72 6f 6f 74 20 70 72 6f 63 65 73 73 65 73 2e 20 root processes.
dce90 20 53 6f 20 61 76 6f 69 64 20 63 61 6c 6c 69 6e So avoid callin
dcea0 67 20 66 63 68 6f 77 6e 28 29 20 69 66 0a 2a 2a g fchown() if.**
dceb0 20 77 65 20 61 72 65 20 6e 6f 74 20 72 75 6e 6e we are not runn
dcec0 69 6e 67 20 61 73 20 72 6f 6f 74 2e 0a 2a 2f 0a ing as root..*/.
dced0 73 74 61 74 69 63 20 69 6e 74 20 70 6f 73 69 78 static int posix
dcee0 46 63 68 6f 77 6e 28 69 6e 74 20 66 64 2c 20 75 Fchown(int fd, u
dcef0 69 64 5f 74 20 75 69 64 2c 20 67 69 64 5f 74 20 id_t uid, gid_t
dcf00 67 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 67 gid){. return g
dcf10 65 74 65 75 69 64 28 29 20 3f 20 30 20 3a 20 66 eteuid() ? 0 : f
dcf20 63 68 6f 77 6e 28 66 64 2c 75 69 64 2c 67 69 64 chown(fd,uid,gid
dcf30 29 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 );.}../* Forward
dcf40 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74 reference */.st
dcf50 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69 72 atic int openDir
dcf60 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 61 ectory(const cha
dcf70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 2f 2a 0a 2a r*, int*);../*.*
dcf80 2a 20 4d 61 6e 79 20 73 79 73 74 65 6d 20 63 61 * Many system ca
dcf90 6c 6c 73 20 61 72 65 20 61 63 63 65 73 73 65 64 lls are accessed
dcfa0 20 74 68 72 6f 75 67 68 20 70 6f 69 6e 74 65 72 through pointer
dcfb0 2d 74 6f 2d 66 75 6e 63 74 69 6f 6e 73 20 73 6f -to-functions so
dcfc0 20 74 68 61 74 0a 2a 2a 20 74 68 65 79 20 6d 61 that.** they ma
dcfd0 79 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 y be overridden
dcfe0 61 74 20 72 75 6e 74 69 6d 65 20 74 6f 20 66 61 at runtime to fa
dcff0 63 69 6c 69 74 61 74 65 20 66 61 75 6c 74 20 69 cilitate fault i
dd000 6e 6a 65 63 74 69 6f 6e 20 64 75 72 69 6e 67 0a njection during.
dd010 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 73 ** testing and s
dd020 61 6e 64 62 6f 78 69 6e 67 2e 20 20 54 68 65 20 andboxing. The
dd030 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 20 following array
dd040 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 73 20 holds the names
dd050 61 6e 64 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20 and pointers.**
dd060 74 6f 20 61 6c 6c 20 6f 76 65 72 72 69 64 65 61 to all overridea
dd070 62 6c 65 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 ble system calls
dd080 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
dd090 63 74 20 75 6e 69 78 5f 73 79 73 63 61 6c 6c 20 ct unix_syscall
dd0a0 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a {. const char *
dd0b0 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 zName;
dd0c0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
dd0d0 20 73 79 74 65 6d 20 63 61 6c 6c 20 2a 2f 0a 20 sytem call */.
dd0e0 20 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c sqlite3_syscall
dd0f0 5f 70 74 72 20 70 43 75 72 72 65 6e 74 3b 20 2f _ptr pCurrent; /
dd100 2a 20 43 75 72 72 65 6e 74 20 76 61 6c 75 65 20 * Current value
dd110 6f 66 20 74 68 65 20 73 79 73 74 65 6d 20 63 61 of the system ca
dd120 6c 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ll */. sqlite3_
dd130 73 79 73 63 61 6c 6c 5f 70 74 72 20 70 44 65 66 syscall_ptr pDef
dd140 61 75 6c 74 3b 20 2f 2a 20 44 65 66 61 75 6c 74 ault; /* Default
dd150 20 76 61 6c 75 65 20 2a 2f 0a 7d 20 61 53 79 73 value */.} aSys
dd160 63 61 6c 6c 5b 5d 20 3d 20 7b 0a 20 20 7b 20 22 call[] = {. { "
dd170 6f 70 65 6e 22 2c 20 20 20 20 20 20 20 20 20 28 open", (
dd180 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f sqlite3_syscall_
dd190 70 74 72 29 70 6f 73 69 78 4f 70 65 6e 2c 20 20 ptr)posixOpen,
dd1a0 30 20 20 7d 2c 0a 23 64 65 66 69 6e 65 20 6f 73 0 },.#define os
dd1b0 4f 70 65 6e 20 20 20 20 20 20 28 28 69 6e 74 28 Open ((int(
dd1c0 2a 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 *)(const char*,i
dd1d0 6e 74 2c 69 6e 74 29 29 61 53 79 73 63 61 6c 6c nt,int))aSyscall
dd1e0 5b 30 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 [0].pCurrent)..
dd1f0 20 7b 20 22 63 6c 6f 73 65 22 2c 20 20 20 20 20 { "close",
dd200 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 (sqlite3_sysc
dd210 61 6c 6c 5f 70 74 72 29 63 6c 6f 73 65 2c 20 20 all_ptr)close,
dd220 20 20 20 20 30 20 20 7d 2c 0a 23 64 65 66 69 6e 0 },.#defin
dd230 65 20 6f 73 43 6c 6f 73 65 20 20 20 20 20 28 28 e osClose ((
dd240 69 6e 74 28 2a 29 28 69 6e 74 29 29 61 53 79 73 int(*)(int))aSys
dd250 63 61 6c 6c 5b 31 5d 2e 70 43 75 72 72 65 6e 74 call[1].pCurrent
dd260 29 0a 0a 20 20 7b 20 22 61 63 63 65 73 73 22 2c ).. { "access",
dd270 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f (sqlite3_
dd280 73 79 73 63 61 6c 6c 5f 70 74 72 29 61 63 63 65 syscall_ptr)acce
dd290 73 73 2c 20 20 20 20 20 30 20 20 7d 2c 0a 23 64 ss, 0 },.#d
dd2a0 65 66 69 6e 65 20 6f 73 41 63 63 65 73 73 20 20 efine osAccess
dd2b0 20 20 28 28 69 6e 74 28 2a 29 28 63 6f 6e 73 74 ((int(*)(const
dd2c0 20 63 68 61 72 2a 2c 69 6e 74 29 29 61 53 79 73 char*,int))aSys
dd2d0 63 61 6c 6c 5b 32 5d 2e 70 43 75 72 72 65 6e 74 call[2].pCurrent
dd2e0 29 0a 0a 20 20 7b 20 22 67 65 74 63 77 64 22 2c ).. { "getcwd",
dd2f0 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f (sqlite3_
dd300 73 79 73 63 61 6c 6c 5f 70 74 72 29 67 65 74 63 syscall_ptr)getc
dd310 77 64 2c 20 20 20 20 20 30 20 20 7d 2c 0a 23 64 wd, 0 },.#d
dd320 65 66 69 6e 65 20 6f 73 47 65 74 63 77 64 20 20 efine osGetcwd
dd330 20 20 28 28 63 68 61 72 2a 28 2a 29 28 63 68 61 ((char*(*)(cha
dd340 72 2a 2c 73 69 7a 65 5f 74 29 29 61 53 79 73 63 r*,size_t))aSysc
dd350 61 6c 6c 5b 33 5d 2e 70 43 75 72 72 65 6e 74 29 all[3].pCurrent)
dd360 0a 0a 20 20 7b 20 22 73 74 61 74 22 2c 20 20 20 .. { "stat",
dd370 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 (sqlite3_s
dd380 79 73 63 61 6c 6c 5f 70 74 72 29 73 74 61 74 2c yscall_ptr)stat,
dd390 20 20 20 20 20 20 20 30 20 20 7d 2c 0a 23 64 65 0 },.#de
dd3a0 66 69 6e 65 20 6f 73 53 74 61 74 20 20 20 20 20 fine osStat
dd3b0 20 28 28 69 6e 74 28 2a 29 28 63 6f 6e 73 74 20 ((int(*)(const
dd3c0 63 68 61 72 2a 2c 73 74 72 75 63 74 20 73 74 61 char*,struct sta
dd3d0 74 2a 29 29 61 53 79 73 63 61 6c 6c 5b 34 5d 2e t*))aSyscall[4].
dd3e0 70 43 75 72 72 65 6e 74 29 0a 0a 2f 2a 0a 2a 2a pCurrent)../*.**
dd3f0 20 54 68 65 20 44 4a 47 50 50 20 63 6f 6d 70 69 The DJGPP compi
dd400 6c 65 72 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 ler environment
dd410 6c 6f 6f 6b 73 20 6d 6f 73 74 6c 79 20 6c 69 6b looks mostly lik
dd420 65 20 55 6e 69 78 2c 20 62 75 74 20 69 74 0a 2a e Unix, but it.*
dd430 2a 20 6c 61 63 6b 73 20 74 68 65 20 66 63 6e 74 * lacks the fcnt
dd440 6c 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e l() system call.
dd450 20 20 53 6f 20 72 65 64 65 66 69 6e 65 20 66 63 So redefine fc
dd460 6e 74 6c 28 29 20 74 6f 20 62 65 20 73 6f 6d 65 ntl() to be some
dd470 74 68 69 6e 67 0a 2a 2a 20 74 68 61 74 20 61 6c thing.** that al
dd480 77 61 79 73 20 73 75 63 63 65 65 64 73 2e 20 20 ways succeeds.
dd490 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 This means that
dd4a0 6c 6f 63 6b 69 6e 67 20 64 6f 65 73 20 6e 6f 74 locking does not
dd4b0 20 6f 63 63 75 72 20 75 6e 64 65 72 0a 2a 2a 20 occur under.**
dd4c0 44 4a 47 50 50 2e 20 20 42 75 74 20 69 74 20 69 DJGPP. But it i
dd4d0 73 20 44 4f 53 20 2d 20 77 68 61 74 20 64 69 64 s DOS - what did
dd4e0 20 79 6f 75 20 65 78 70 65 63 74 3f 0a 2a 2f 0a you expect?.*/.
dd4f0 23 69 66 64 65 66 20 5f 5f 44 4a 47 50 50 5f 5f #ifdef __DJGPP__
dd500 0a 20 20 7b 20 22 66 73 74 61 74 22 2c 20 20 20 . { "fstat",
dd510 20 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 0,
dd520 20 20 20 20 20 20 20 20 30 20 20 7d 2c 0a 23 64 0 },.#d
dd530 65 66 69 6e 65 20 6f 73 46 73 74 61 74 28 61 2c efine osFstat(a,
dd540 62 2c 63 29 20 20 20 20 30 0a 23 65 6c 73 65 20 b,c) 0.#else
dd550 20 20 20 20 0a 20 20 7b 20 22 66 73 74 61 74 22 . { "fstat"
dd560 2c 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 , (sqlite
dd570 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 66 73 3_syscall_ptr)fs
dd580 74 61 74 2c 20 20 20 20 20 20 30 20 20 7d 2c 0a tat, 0 },.
dd590 23 64 65 66 69 6e 65 20 6f 73 46 73 74 61 74 20 #define osFstat
dd5a0 20 20 20 20 28 28 69 6e 74 28 2a 29 28 69 6e 74 ((int(*)(int
dd5b0 2c 73 74 72 75 63 74 20 73 74 61 74 2a 29 29 61 ,struct stat*))a
dd5c0 53 79 73 63 61 6c 6c 5b 35 5d 2e 70 43 75 72 72 Syscall[5].pCurr
dd5d0 65 6e 74 29 0a 23 65 6e 64 69 66 0a 0a 20 20 7b ent).#endif.. {
dd5e0 20 22 66 74 72 75 6e 63 61 74 65 22 2c 20 20 20 "ftruncate",
dd5f0 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c (sqlite3_syscal
dd600 6c 5f 70 74 72 29 66 74 72 75 6e 63 61 74 65 2c l_ptr)ftruncate,
dd610 20 20 30 20 20 7d 2c 0a 23 64 65 66 69 6e 65 20 0 },.#define
dd620 6f 73 46 74 72 75 6e 63 61 74 65 20 28 28 69 6e osFtruncate ((in
dd630 74 28 2a 29 28 69 6e 74 2c 6f 66 66 5f 74 29 29 t(*)(int,off_t))
dd640 61 53 79 73 63 61 6c 6c 5b 36 5d 2e 70 43 75 72 aSyscall[6].pCur
dd650 72 65 6e 74 29 0a 0a 20 20 7b 20 22 66 63 6e 74 rent).. { "fcnt
dd660 6c 22 2c 20 20 20 20 20 20 20 20 28 73 71 6c 69 l", (sqli
dd670 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 te3_syscall_ptr)
dd680 66 63 6e 74 6c 2c 20 20 20 20 20 20 30 20 20 7d fcntl, 0 }
dd690 2c 0a 23 64 65 66 69 6e 65 20 6f 73 46 63 6e 74 ,.#define osFcnt
dd6a0 6c 20 20 20 20 20 28 28 69 6e 74 28 2a 29 28 69 l ((int(*)(i
dd6b0 6e 74 2c 69 6e 74 2c 2e 2e 2e 29 29 61 53 79 73 nt,int,...))aSys
dd6c0 63 61 6c 6c 5b 37 5d 2e 70 43 75 72 72 65 6e 74 call[7].pCurrent
dd6d0 29 0a 0a 20 20 7b 20 22 72 65 61 64 22 2c 20 20 ).. { "read",
dd6e0 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f (sqlite3_
dd6f0 73 79 73 63 61 6c 6c 5f 70 74 72 29 72 65 61 64 syscall_ptr)read
dd700 2c 20 20 20 20 20 20 20 30 20 20 7d 2c 0a 23 64 , 0 },.#d
dd710 65 66 69 6e 65 20 6f 73 52 65 61 64 20 20 20 20 efine osRead
dd720 20 20 28 28 73 73 69 7a 65 5f 74 28 2a 29 28 69 ((ssize_t(*)(i
dd730 6e 74 2c 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 29 nt,void*,size_t)
dd740 29 61 53 79 73 63 61 6c 6c 5b 38 5d 2e 70 43 75 )aSyscall[8].pCu
dd750 72 72 65 6e 74 29 0a 0a 23 69 66 20 64 65 66 69 rrent)..#if defi
dd760 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29 20 7c ned(USE_PREAD) |
dd770 7c 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f | SQLITE_ENABLE_
dd780 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 LOCKING_STYLE.
dd790 7b 20 22 70 72 65 61 64 22 2c 20 20 20 20 20 20 { "pread",
dd7a0 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 (sqlite3_sysca
dd7b0 6c 6c 5f 70 74 72 29 70 72 65 61 64 2c 20 20 20 ll_ptr)pread,
dd7c0 20 20 20 30 20 20 7d 2c 0a 23 65 6c 73 65 0a 20 0 },.#else.
dd7d0 20 7b 20 22 70 72 65 61 64 22 2c 20 20 20 20 20 { "pread",
dd7e0 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 (sqlite3_sysc
dd7f0 61 6c 6c 5f 70 74 72 29 30 2c 20 20 20 20 20 20 all_ptr)0,
dd800 20 20 20 20 30 20 20 7d 2c 0a 23 65 6e 64 69 66 0 },.#endif
dd810 0a 23 64 65 66 69 6e 65 20 6f 73 50 72 65 61 64 .#define osPread
dd820 20 20 20 20 20 28 28 73 73 69 7a 65 5f 74 28 2a ((ssize_t(*
dd830 29 28 69 6e 74 2c 76 6f 69 64 2a 2c 73 69 7a 65 )(int,void*,size
dd840 5f 74 2c 6f 66 66 5f 74 29 29 61 53 79 73 63 61 _t,off_t))aSysca
dd850 6c 6c 5b 39 5d 2e 70 43 75 72 72 65 6e 74 29 0a ll[9].pCurrent).
dd860 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 .#if defined(USE
dd870 5f 50 52 45 41 44 36 34 29 0a 20 20 7b 20 22 70 _PREAD64). { "p
dd880 72 65 61 64 36 34 22 2c 20 20 20 20 20 20 28 73 read64", (s
dd890 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 qlite3_syscall_p
dd8a0 74 72 29 70 72 65 61 64 36 34 2c 20 20 20 20 30 tr)pread64, 0
dd8b0 20 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 },.#else. { "
dd8c0 70 72 65 61 64 36 34 22 2c 20 20 20 20 20 20 28 pread64", (
dd8d0 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f sqlite3_syscall_
dd8e0 70 74 72 29 30 2c 20 20 20 20 20 20 20 20 20 20 ptr)0,
dd8f0 30 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 64 65 0 },.#endif.#de
dd900 66 69 6e 65 20 6f 73 50 72 65 61 64 36 34 20 20 fine osPread64
dd910 20 28 28 73 73 69 7a 65 5f 74 28 2a 29 28 69 6e ((ssize_t(*)(in
dd920 74 2c 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 2c 6f t,void*,size_t,o
dd930 66 66 5f 74 29 29 61 53 79 73 63 61 6c 6c 5b 31 ff_t))aSyscall[1
dd940 30 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 0].pCurrent)..
dd950 7b 20 22 77 72 69 74 65 22 2c 20 20 20 20 20 20 { "write",
dd960 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 (sqlite3_sysca
dd970 6c 6c 5f 70 74 72 29 77 72 69 74 65 2c 20 20 20 ll_ptr)write,
dd980 20 20 20 30 20 20 7d 2c 0a 23 64 65 66 69 6e 65 0 },.#define
dd990 20 6f 73 57 72 69 74 65 20 20 20 20 20 28 28 73 osWrite ((s
dd9a0 73 69 7a 65 5f 74 28 2a 29 28 69 6e 74 2c 63 6f size_t(*)(int,co
dd9b0 6e 73 74 20 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 nst void*,size_t
dd9c0 29 29 61 53 79 73 63 61 6c 6c 5b 31 31 5d 2e 70 ))aSyscall[11].p
dd9d0 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 64 65 Current)..#if de
dd9e0 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29 fined(USE_PREAD)
dd9f0 20 7c 7c 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c || SQLITE_ENABL
dda00 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
dda10 20 20 7b 20 22 70 77 72 69 74 65 22 2c 20 20 20 { "pwrite",
dda20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 (sqlite3_sys
dda30 63 61 6c 6c 5f 70 74 72 29 70 77 72 69 74 65 2c call_ptr)pwrite,
dda40 20 20 20 20 20 30 20 20 7d 2c 0a 23 65 6c 73 65 0 },.#else
dda50 0a 20 20 7b 20 22 70 77 72 69 74 65 22 2c 20 20 . { "pwrite",
dda60 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 (sqlite3_sy
dda70 73 63 61 6c 6c 5f 70 74 72 29 30 2c 20 20 20 20 scall_ptr)0,
dda80 20 20 20 20 20 20 30 20 20 7d 2c 0a 23 65 6e 64 0 },.#end
dda90 69 66 0a 23 64 65 66 69 6e 65 20 6f 73 50 77 72 if.#define osPwr
ddaa0 69 74 65 20 20 20 20 28 28 73 73 69 7a 65 5f 74 ite ((ssize_t
ddab0 28 2a 29 28 69 6e 74 2c 63 6f 6e 73 74 20 76 6f (*)(int,const vo
ddac0 69 64 2a 2c 73 69 7a 65 5f 74 2c 6f 66 66 5f 74 id*,size_t,off_t
ddad0 29 29 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ))\.
ddae0 20 20 20 20 20 20 20 20 61 53 79 73 63 61 6c 6c aSyscall
ddaf0 5b 31 32 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a [12].pCurrent)..
ddb00 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f #if defined(USE_
ddb10 50 52 45 41 44 36 34 29 0a 20 20 7b 20 22 70 77 PREAD64). { "pw
ddb20 72 69 74 65 36 34 22 2c 20 20 20 20 20 28 73 71 rite64", (sq
ddb30 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 lite3_syscall_pt
ddb40 72 29 70 77 72 69 74 65 36 34 2c 20 20 20 30 20 r)pwrite64, 0
ddb50 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 70 },.#else. { "p
ddb60 77 72 69 74 65 36 34 22 2c 20 20 20 20 20 28 73 write64", (s
ddb70 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 qlite3_syscall_p
ddb80 74 72 29 30 2c 20 20 20 20 20 20 20 20 20 20 30 tr)0, 0
ddb90 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 64 65 66 },.#endif.#def
ddba0 69 6e 65 20 6f 73 50 77 72 69 74 65 36 34 20 20 ine osPwrite64
ddbb0 28 28 73 73 69 7a 65 5f 74 28 2a 29 28 69 6e 74 ((ssize_t(*)(int
ddbc0 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 73 69 7a ,const void*,siz
ddbd0 65 5f 74 2c 6f 66 66 5f 74 29 29 5c 0a 20 20 20 e_t,off_t))\.
ddbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ddbf0 20 61 53 79 73 63 61 6c 6c 5b 31 33 5d 2e 70 43 aSyscall[13].pC
ddc00 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 53 51 4c urrent)..#if SQL
ddc10 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
ddc20 4e 47 5f 53 54 59 4c 45 0a 20 20 7b 20 22 66 63 NG_STYLE. { "fc
ddc30 68 6d 6f 64 22 2c 20 20 20 20 20 20 20 28 73 71 hmod", (sq
ddc40 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 lite3_syscall_pt
ddc50 72 29 66 63 68 6d 6f 64 2c 20 20 20 20 20 30 20 r)fchmod, 0
ddc60 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 66 },.#else. { "f
ddc70 63 68 6d 6f 64 22 2c 20 20 20 20 20 20 20 28 73 chmod", (s
ddc80 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 qlite3_syscall_p
ddc90 74 72 29 30 2c 20 20 20 20 20 20 20 20 20 20 30 tr)0, 0
ddca0 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 64 65 66 },.#endif.#def
ddcb0 69 6e 65 20 6f 73 46 63 68 6d 6f 64 20 20 20 20 ine osFchmod
ddcc0 28 28 69 6e 74 28 2a 29 28 69 6e 74 2c 6d 6f 64 ((int(*)(int,mod
ddcd0 65 5f 74 29 29 61 53 79 73 63 61 6c 6c 5b 31 34 e_t))aSyscall[14
ddce0 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 ].pCurrent)..#if
ddcf0 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f 50 4f defined(HAVE_PO
ddd00 53 49 58 5f 46 41 4c 4c 4f 43 41 54 45 29 20 26 SIX_FALLOCATE) &
ddd10 26 20 48 41 56 45 5f 50 4f 53 49 58 5f 46 41 4c & HAVE_POSIX_FAL
ddd20 4c 4f 43 41 54 45 0a 20 20 7b 20 22 66 61 6c 6c LOCATE. { "fall
ddd30 6f 63 61 74 65 22 2c 20 20 20 20 28 73 71 6c 69 ocate", (sqli
ddd40 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 te3_syscall_ptr)
ddd50 70 6f 73 69 78 5f 66 61 6c 6c 6f 63 61 74 65 2c posix_fallocate,
ddd60 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 0 },.#else. {
ddd70 20 22 66 61 6c 6c 6f 63 61 74 65 22 2c 20 20 20 "fallocate",
ddd80 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c (sqlite3_syscal
ddd90 6c 5f 70 74 72 29 30 2c 20 20 20 20 20 20 20 20 l_ptr)0,
ddda0 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 0 },.#en
dddb0 64 69 66 0a 23 64 65 66 69 6e 65 20 6f 73 46 61 dif.#define osFa
dddc0 6c 6c 6f 63 61 74 65 20 28 28 69 6e 74 28 2a 29 llocate ((int(*)
dddd0 28 69 6e 74 2c 6f 66 66 5f 74 2c 6f 66 66 5f 74 (int,off_t,off_t
ddde0 29 29 61 53 79 73 63 61 6c 6c 5b 31 35 5d 2e 70 ))aSyscall[15].p
dddf0 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 75 Current).. { "u
dde00 6e 6c 69 6e 6b 22 2c 20 20 20 20 20 20 20 28 73 nlink", (s
dde10 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 qlite3_syscall_p
dde20 74 72 29 75 6e 6c 69 6e 6b 2c 20 20 20 20 20 20 tr)unlink,
dde30 20 20 20 20 20 30 20 7d 2c 0a 23 64 65 66 69 6e 0 },.#defin
dde40 65 20 6f 73 55 6e 6c 69 6e 6b 20 20 20 20 28 28 e osUnlink ((
dde50 69 6e 74 28 2a 29 28 63 6f 6e 73 74 20 63 68 61 int(*)(const cha
dde60 72 2a 29 29 61 53 79 73 63 61 6c 6c 5b 31 36 5d r*))aSyscall[16]
dde70 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 .pCurrent).. {
dde80 22 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 22 2c "openDirectory",
dde90 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 (sqlite3_sys
ddea0 63 61 6c 6c 5f 70 74 72 29 6f 70 65 6e 44 69 72 call_ptr)openDir
ddeb0 65 63 74 6f 72 79 2c 20 20 20 20 20 20 30 20 7d ectory, 0 }
ddec0 2c 0a 23 64 65 66 69 6e 65 20 6f 73 4f 70 65 6e ,.#define osOpen
dded0 44 69 72 65 63 74 6f 72 79 20 28 28 69 6e 74 28 Directory ((int(
ddee0 2a 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 *)(const char*,i
ddef0 6e 74 2a 29 29 61 53 79 73 63 61 6c 6c 5b 31 37 nt*))aSyscall[17
ddf00 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b ].pCurrent).. {
ddf10 20 22 6d 6b 64 69 72 22 2c 20 20 20 20 20 20 20 "mkdir",
ddf20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c (sqlite3_syscal
ddf30 6c 5f 70 74 72 29 6d 6b 64 69 72 2c 20 20 20 20 l_ptr)mkdir,
ddf40 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 64 65 66 0 },.#def
ddf50 69 6e 65 20 6f 73 4d 6b 64 69 72 20 20 20 20 20 ine osMkdir
ddf60 28 28 69 6e 74 28 2a 29 28 63 6f 6e 73 74 20 63 ((int(*)(const c
ddf70 68 61 72 2a 2c 6d 6f 64 65 5f 74 29 29 61 53 79 har*,mode_t))aSy
ddf80 73 63 61 6c 6c 5b 31 38 5d 2e 70 43 75 72 72 65 scall[18].pCurre
ddf90 6e 74 29 0a 0a 20 20 7b 20 22 72 6d 64 69 72 22 nt).. { "rmdir"
ddfa0 2c 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 , (sqlite
ddfb0 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 72 6d 3_syscall_ptr)rm
ddfc0 64 69 72 2c 20 20 20 20 20 20 20 20 20 20 20 30 dir, 0
ddfd0 20 7d 2c 0a 23 64 65 66 69 6e 65 20 6f 73 52 6d },.#define osRm
ddfe0 64 69 72 20 20 20 20 20 28 28 69 6e 74 28 2a 29 dir ((int(*)
ddff0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 61 53 (const char*))aS
de000 79 73 63 61 6c 6c 5b 31 39 5d 2e 70 43 75 72 72 yscall[19].pCurr
de010 65 6e 74 29 0a 0a 20 20 7b 20 22 66 63 68 6f 77 ent).. { "fchow
de020 6e 22 2c 20 20 20 20 20 20 20 28 73 71 6c 69 74 n", (sqlit
de030 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 70 e3_syscall_ptr)p
de040 6f 73 69 78 46 63 68 6f 77 6e 2c 20 20 20 20 20 osixFchown,
de050 30 20 7d 2c 0a 23 64 65 66 69 6e 65 20 6f 73 46 0 },.#define osF
de060 63 68 6f 77 6e 20 20 20 20 28 28 69 6e 74 28 2a chown ((int(*
de070 29 28 69 6e 74 2c 75 69 64 5f 74 2c 67 69 64 5f )(int,uid_t,gid_
de080 74 29 29 61 53 79 73 63 61 6c 6c 5b 32 30 5d 2e t))aSyscall[20].
de090 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 pCurrent).. { "
de0a0 75 6d 61 73 6b 22 2c 20 20 20 20 20 20 20 20 28 umask", (
de0b0 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f sqlite3_syscall_
de0c0 70 74 72 29 75 6d 61 73 6b 2c 20 20 20 20 20 20 ptr)umask,
de0d0 20 20 20 20 20 30 20 7d 2c 0a 23 64 65 66 69 6e 0 },.#defin
de0e0 65 20 6f 73 55 6d 61 73 6b 20 20 20 20 20 28 28 e osUmask ((
de0f0 6d 6f 64 65 5f 74 28 2a 29 28 6d 6f 64 65 5f 74 mode_t(*)(mode_t
de100 29 29 61 53 79 73 63 61 6c 6c 5b 32 31 5d 2e 70 ))aSyscall[21].p
de110 43 75 72 72 65 6e 74 29 0a 0a 7d 3b 20 2f 2a 20 Current)..}; /*
de120 45 6e 64 20 6f 66 20 74 68 65 20 6f 76 65 72 72 End of the overr
de130 69 64 65 61 62 6c 65 20 73 79 73 74 65 6d 20 63 ideable system c
de140 61 6c 6c 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 alls */../*.** T
de150 68 69 73 20 69 73 20 74 68 65 20 78 53 65 74 53 his is the xSetS
de160 79 73 74 65 6d 43 61 6c 6c 28 29 20 6d 65 74 68 ystemCall() meth
de170 6f 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66 od of sqlite3_vf
de180 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 s for all of the
de190 0a 2a 2a 20 22 75 6e 69 78 22 20 56 46 53 65 73 .** "unix" VFSes
de1a0 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
de1b0 5f 4f 4b 20 6f 70 6f 6e 20 73 75 63 63 65 73 73 _OK opon success
de1c0 66 75 6c 6c 79 20 75 70 64 61 74 69 6e 67 20 74 fully updating t
de1d0 68 65 0a 2a 2a 20 73 79 73 74 65 6d 20 63 61 6c he.** system cal
de1e0 6c 20 70 6f 69 6e 74 65 72 2c 20 6f 72 20 53 51 l pointer, or SQ
de1f0 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 69 66 LITE_NOTFOUND if
de200 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e there is no con
de210 66 69 67 75 72 61 62 6c 65 0a 2a 2a 20 73 79 73 figurable.** sys
de220 74 65 6d 20 63 61 6c 6c 20 6e 61 6d 65 64 20 7a tem call named z
de230 4e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Name..*/.static
de240 69 6e 74 20 75 6e 69 78 53 65 74 53 79 73 74 65 int unixSetSyste
de250 6d 43 61 6c 6c 28 0a 20 20 73 71 6c 69 74 65 33 mCall(. sqlite3
de260 5f 76 66 73 20 2a 70 4e 6f 74 55 73 65 64 2c 20 _vfs *pNotUsed,
de270 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 /* The VF
de280 53 20 70 6f 69 6e 74 65 72 2e 20 20 4e 6f 74 20 S pointer. Not
de290 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 used */. const
de2a0 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 char *zName,
de2b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
de2c0 6f 66 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 74 of system call t
de2d0 6f 20 6f 76 65 72 72 69 64 65 20 2a 2f 0a 20 20 o override */.
de2e0 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f sqlite3_syscall_
de2f0 70 74 72 20 70 4e 65 77 46 75 6e 63 20 20 2f 2a ptr pNewFunc /*
de300 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 Pointer to new
de310 73 79 73 74 65 6d 20 63 61 6c 6c 20 76 61 6c 75 system call valu
de320 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e e */.){. unsign
de330 65 64 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 ed int i;. int
de340 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 46 rc = SQLITE_NOTF
de350 4f 55 4e 44 3b 0a 0a 20 20 55 4e 55 53 45 44 5f OUND;.. UNUSED_
de360 50 41 52 41 4d 45 54 45 52 28 70 4e 6f 74 55 73 PARAMETER(pNotUs
de370 65 64 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 ed);. if( zName
de380 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 ==0 ){. /* If
de390 20 6e 6f 20 7a 4e 61 6d 65 20 69 73 20 67 69 76 no zName is giv
de3a0 65 6e 2c 20 72 65 73 74 6f 72 65 20 61 6c 6c 20 en, restore all
de3b0 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 74 6f 20 system calls to
de3c0 74 68 65 69 72 20 64 65 66 61 75 6c 74 0a 20 20 their default.
de3d0 20 20 2a 2a 20 73 65 74 74 69 6e 67 73 20 61 6e ** settings an
de3e0 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a 20 20 d return NULL.
de3f0 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 */. rc = SQ
de400 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 66 6f 72 LITE_OK;. for
de410 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 (i=0; i<sizeof(a
de420 53 79 73 63 61 6c 6c 29 2f 73 69 7a 65 6f 66 28 Syscall)/sizeof(
de430 61 53 79 73 63 61 6c 6c 5b 30 5d 29 3b 20 69 2b aSyscall[0]); i+
de440 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 53 +){. if( aS
de450 79 73 63 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 yscall[i].pDefau
de460 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 53 lt ){. aS
de470 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 yscall[i].pCurre
de480 6e 74 20 3d 20 61 53 79 73 63 61 6c 6c 5b 69 5d nt = aSyscall[i]
de490 2e 70 44 65 66 61 75 6c 74 3b 0a 20 20 20 20 20 .pDefault;.
de4a0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 }. }. }else
de4b0 7b 0a 20 20 20 20 2f 2a 20 49 66 20 7a 4e 61 6d {. /* If zNam
de4c0 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 e is specified,
de4d0 6f 70 65 72 61 74 65 20 6f 6e 20 6f 6e 6c 79 20 operate on only
de4e0 74 68 65 20 6f 6e 65 20 73 79 73 74 65 6d 20 63 the one system c
de4f0 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 70 65 63 69 all. ** speci
de500 66 69 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 fied.. */.
de510 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 for(i=0; i<size
de520 6f 66 28 61 53 79 73 63 61 6c 6c 29 2f 73 69 7a of(aSyscall)/siz
de530 65 6f 66 28 61 53 79 73 63 61 6c 6c 5b 30 5d 29 eof(aSyscall[0])
de540 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
de550 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 ( strcmp(zName,
de560 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d aSyscall[i].zNam
de570 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 e)==0 ){.
de580 20 69 66 28 20 61 53 79 73 63 61 6c 6c 5b 69 5d if( aSyscall[i]
de590 2e 70 44 65 66 61 75 6c 74 3d 3d 30 20 29 7b 0a .pDefault==0 ){.
de5a0 20 20 20 20 20 20 20 20 20 20 61 53 79 73 63 61 aSysca
de5b0 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 6c 74 20 3d ll[i].pDefault =
de5c0 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 aSyscall[i].pCu
de5d0 72 72 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 7d rrent;. }
de5e0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
de5f0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 LITE_OK;.
de600 20 69 66 28 20 70 4e 65 77 46 75 6e 63 3d 3d 30 if( pNewFunc==0
de610 20 29 20 70 4e 65 77 46 75 6e 63 20 3d 20 61 53 ) pNewFunc = aS
de620 79 73 63 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 yscall[i].pDefau
de630 6c 74 3b 0a 20 20 20 20 20 20 20 20 61 53 79 73 lt;. aSys
de640 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74 call[i].pCurrent
de650 20 3d 20 70 4e 65 77 46 75 6e 63 3b 0a 20 20 20 = pNewFunc;.
de660 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
de670 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
de680 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
de690 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 .** Return the v
de6a0 61 6c 75 65 20 6f 66 20 61 20 73 79 73 74 65 6d alue of a system
de6b0 20 63 61 6c 6c 2e 20 20 52 65 74 75 72 6e 20 4e call. Return N
de6c0 55 4c 4c 20 69 66 20 7a 4e 61 6d 65 20 69 73 20 ULL if zName is
de6d0 6e 6f 74 20 61 0a 2a 2a 20 72 65 63 6f 67 6e 69 not a.** recogni
de6e0 7a 65 64 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 zed system call
de6f0 6e 61 6d 65 2e 20 20 4e 55 4c 4c 20 69 73 20 61 name. NULL is a
de700 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20 lso returned if
de710 74 68 65 20 73 79 73 74 65 6d 20 63 61 6c 6c 0a the system call.
de720 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 ** is currently
de730 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 undefined..*/.st
de740 61 74 69 63 20 73 71 6c 69 74 65 33 5f 73 79 73 atic sqlite3_sys
de750 63 61 6c 6c 5f 70 74 72 20 75 6e 69 78 47 65 74 call_ptr unixGet
de760 53 79 73 74 65 6d 43 61 6c 6c 28 0a 20 20 73 71 SystemCall(. sq
de770 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e 6f 74 55 lite3_vfs *pNotU
de780 73 65 64 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 sed,. const cha
de790 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 75 6e r *zName.){. un
de7a0 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 0a 20 signed int i;..
de7b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
de7c0 52 28 70 4e 6f 74 55 73 65 64 29 3b 0a 20 20 66 R(pNotUsed);. f
de7d0 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 or(i=0; i<sizeof
de7e0 28 61 53 79 73 63 61 6c 6c 29 2f 73 69 7a 65 6f (aSyscall)/sizeo
de7f0 66 28 61 53 79 73 63 61 6c 6c 5b 30 5d 29 3b 20 f(aSyscall[0]);
de800 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 74 i++){. if( st
de810 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 61 53 79 73 rcmp(zName, aSys
de820 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d call[i].zName)==
de830 30 20 29 20 72 65 74 75 72 6e 20 61 53 79 73 63 0 ) return aSysc
de840 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74 3b all[i].pCurrent;
de850 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
de860 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
de870 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
de880 20 66 69 72 73 74 20 73 79 73 74 65 6d 20 63 61 first system ca
de890 6c 6c 20 61 66 74 65 72 20 7a 4e 61 6d 65 2e 20 ll after zName.
de8a0 20 49 66 20 7a 4e 61 6d 65 3d 3d 4e 55 4c 4c 0a If zName==NULL.
de8b0 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 ** then return t
de8c0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 he name of the f
de8d0 69 72 73 74 20 73 79 73 74 65 6d 20 63 61 6c 6c irst system call
de8e0 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 . Return NULL i
de8f0 66 20 7a 4e 61 6d 65 0a 2a 2a 20 69 73 20 74 68 f zName.** is th
de900 65 20 6c 61 73 74 20 73 79 73 74 65 6d 20 63 61 e last system ca
de910 6c 6c 20 6f 72 20 69 66 20 7a 4e 61 6d 65 20 69 ll or if zName i
de920 73 20 6e 6f 74 20 74 68 65 20 6e 61 6d 65 20 6f s not the name o
de930 66 20 61 20 76 61 6c 69 64 0a 2a 2a 20 73 79 73 f a valid.** sys
de940 74 65 6d 20 63 61 6c 6c 2e 0a 2a 2f 0a 73 74 61 tem call..*/.sta
de950 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
de960 75 6e 69 78 4e 65 78 74 53 79 73 74 65 6d 43 61 unixNextSystemCa
de970 6c 6c 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ll(sqlite3_vfs *
de980 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a p, const char *z
de990 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 20 3d Name){. int i =
de9a0 20 2d 31 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 -1;.. UNUSED_P
de9b0 41 52 41 4d 45 54 45 52 28 70 29 3b 0a 20 20 69 ARAMETER(p);. i
de9c0 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 f( zName ){.
de9d0 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 for(i=0; i<Array
de9e0 53 69 7a 65 28 61 53 79 73 63 61 6c 6c 29 2d 31 Size(aSyscall)-1
de9f0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
dea00 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 ( strcmp(zName,
dea10 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d aSyscall[i].zNam
dea20 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 e)==0 ) break;.
dea30 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 }. }. for(i
dea40 2b 2b 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 ++; i<ArraySize(
dea50 61 53 79 73 63 61 6c 6c 29 3b 20 69 2b 2b 29 7b aSyscall); i++){
dea60 0a 20 20 20 20 69 66 28 20 61 53 79 73 63 61 6c . if( aSyscal
dea70 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74 21 3d 30 l[i].pCurrent!=0
dea80 20 29 20 72 65 74 75 72 6e 20 61 53 79 73 63 61 ) return aSysca
dea90 6c 6c 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d ll[i].zName;. }
deaa0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
deab0 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 6f 70 65 /*.** Invoke ope
deac0 6e 28 29 2e 20 20 44 6f 20 73 6f 20 6d 75 6c 74 n(). Do so mult
dead0 69 70 6c 65 20 74 69 6d 65 73 2c 20 75 6e 74 69 iple times, unti
deae0 6c 20 69 74 20 65 69 74 68 65 72 20 73 75 63 63 l it either succ
deaf0 65 65 64 73 20 6f 72 0a 2a 2a 20 66 61 69 6c 73 eeds or.** fails
deb00 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e for some reason
deb10 20 6f 74 68 65 72 20 74 68 61 6e 20 45 49 4e 54 other than EINT
deb20 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 R..**.** If the
deb30 66 69 6c 65 20 63 72 65 61 74 69 6f 6e 20 6d 6f file creation mo
deb40 64 65 20 22 6d 22 20 69 73 20 30 20 74 68 65 6e de "m" is 0 then
deb50 20 73 65 74 20 69 74 20 74 6f 20 74 68 65 20 64 set it to the d
deb60 65 66 61 75 6c 74 20 66 6f 72 0a 2a 2a 20 53 51 efault for.** SQ
deb70 4c 69 74 65 2e 20 20 54 68 65 20 64 65 66 61 75 Lite. The defau
deb80 6c 74 20 69 73 20 53 51 4c 49 54 45 5f 44 45 46 lt is SQLITE_DEF
deb90 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 AULT_FILE_PERMIS
deba0 53 49 4f 4e 53 20 28 6e 6f 72 6d 61 6c 6c 79 0a SIONS (normally.
debb0 2a 2a 20 30 36 34 34 29 20 61 73 20 6d 6f 64 69 ** 0644) as modi
debc0 66 69 65 64 20 62 79 20 74 68 65 20 73 79 73 74 fied by the syst
debd0 65 6d 20 75 6d 61 73 6b 2e 20 20 49 66 20 6d 20 em umask. If m
debe0 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 0a 2a is not 0, then.*
debf0 2a 20 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 20 * make the file
dec00 63 72 65 61 74 69 6f 6e 20 6d 6f 64 65 20 62 65 creation mode be
dec10 20 65 78 61 63 74 6c 79 20 6d 20 69 67 6e 6f 72 exactly m ignor
dec20 69 6e 67 20 74 68 65 20 75 6d 61 73 6b 2e 0a 2a ing the umask..*
dec30 2a 0a 2a 2a 20 54 68 65 20 6d 20 70 61 72 61 6d *.** The m param
dec40 65 74 65 72 20 77 69 6c 6c 20 62 65 20 6e 6f 6e eter will be non
dec50 2d 7a 65 72 6f 20 6f 6e 6c 79 20 77 68 65 6e 20 -zero only when
dec60 63 72 65 61 74 69 6e 67 20 2d 77 61 6c 2c 20 2d creating -wal, -
dec70 6a 6f 75 72 6e 61 6c 2c 0a 2a 2a 20 61 6e 64 20 journal,.** and
dec80 2d 73 68 6d 20 66 69 6c 65 73 2e 20 20 57 65 20 -shm files. We
dec90 77 61 6e 74 20 74 68 6f 73 65 20 66 69 6c 65 73 want those files
deca0 20 74 6f 20 68 61 76 65 20 2a 65 78 61 63 74 6c to have *exactl
decb0 79 2a 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 70 y* the same.** p
decc0 65 72 6d 69 73 73 69 6f 6e 73 20 61 73 20 74 68 ermissions as th
decd0 65 69 72 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 eir original dat
dece0 61 62 61 73 65 2c 20 75 6e 61 64 75 6c 74 65 72 abase, unadulter
decf0 61 74 65 64 20 62 79 20 74 68 65 20 75 6d 61 73 ated by the umas
ded00 6b 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20 77 61 k..** In that wa
ded10 79 2c 20 69 66 20 61 20 64 61 74 61 62 61 73 65 y, if a database
ded20 20 66 69 6c 65 20 69 73 20 2d 72 77 2d 72 77 2d file is -rw-rw-
ded30 72 77 20 6f 72 20 2d 72 77 2d 72 77 2d 72 2d 2c rw or -rw-rw-r-,
ded40 20 61 6e 64 20 61 0a 2a 2a 20 74 72 61 6e 73 61 and a.** transa
ded50 63 74 69 6f 6e 20 63 72 61 73 68 65 73 20 61 6e ction crashes an
ded60 64 20 6c 65 61 76 65 73 20 62 65 68 69 6e 64 20 d leaves behind
ded70 68 6f 74 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 hot journals, th
ded80 65 6e 20 61 6e 79 0a 2a 2a 20 70 72 6f 63 65 73 en any.** proces
ded90 73 20 74 68 61 74 20 69 73 20 61 62 6c 65 20 74 s that is able t
deda0 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 64 o write to the d
dedb0 61 74 61 62 61 73 65 20 77 69 6c 6c 20 61 6c 73 atabase will als
dedc0 6f 20 62 65 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 o be able to.**
dedd0 72 65 63 6f 76 65 72 20 74 68 65 20 68 6f 74 20 recover the hot
dede0 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2f 0a 73 74 61 journals..*/.sta
dedf0 74 69 63 20 69 6e 74 20 72 6f 62 75 73 74 5f 6f tic int robust_o
dee00 70 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a pen(const char *
dee10 7a 2c 20 69 6e 74 20 66 2c 20 6d 6f 64 65 5f 74 z, int f, mode_t
dee20 20 6d 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 m){. int fd;.
dee30 20 6d 6f 64 65 5f 74 20 6d 32 3b 0a 20 20 6d 6f mode_t m2;. mo
dee40 64 65 5f 74 20 6f 72 69 67 4d 20 3d 20 30 3b 0a de_t origM = 0;.
dee50 20 20 69 66 28 20 6d 3d 3d 30 20 29 7b 0a 20 20 if( m==0 ){.
dee60 20 20 6d 32 20 3d 20 53 51 4c 49 54 45 5f 44 45 m2 = SQLITE_DE
dee70 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 FAULT_FILE_PERMI
dee80 53 53 49 4f 4e 53 3b 0a 20 20 7d 65 6c 73 65 7b SSIONS;. }else{
dee90 0a 20 20 20 20 6d 32 20 3d 20 6d 3b 0a 20 20 20 . m2 = m;.
deea0 20 6f 72 69 67 4d 20 3d 20 6f 73 55 6d 61 73 6b origM = osUmask
deeb0 28 30 29 3b 0a 20 20 7d 0a 20 20 64 6f 7b 0a 23 (0);. }. do{.#
deec0 69 66 20 64 65 66 69 6e 65 64 28 4f 5f 43 4c 4f if defined(O_CLO
deed0 45 58 45 43 29 0a 20 20 20 20 66 64 20 3d 20 6f EXEC). fd = o
deee0 73 4f 70 65 6e 28 7a 2c 66 7c 4f 5f 43 4c 4f 45 sOpen(z,f|O_CLOE
deef0 58 45 43 2c 6d 32 29 3b 0a 23 65 6c 73 65 0a 20 XEC,m2);.#else.
def00 20 20 20 66 64 20 3d 20 6f 73 4f 70 65 6e 28 7a fd = osOpen(z
def10 2c 66 2c 6d 32 29 3b 0a 23 65 6e 64 69 66 0a 20 ,f,m2);.#endif.
def20 20 7d 77 68 69 6c 65 28 20 66 64 3c 30 20 26 26 }while( fd<0 &&
def30 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 29 3b errno==EINTR );
def40 0a 20 20 69 66 28 20 6d 20 29 7b 0a 20 20 20 20 . if( m ){.
def50 6f 73 55 6d 61 73 6b 28 6f 72 69 67 4d 29 3b 0a osUmask(origM);.
def60 20 20 7d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 }.#if defined(
def70 46 44 5f 43 4c 4f 45 58 45 43 29 20 26 26 20 28 FD_CLOEXEC) && (
def80 21 64 65 66 69 6e 65 64 28 4f 5f 43 4c 4f 45 58 !defined(O_CLOEX
def90 45 43 29 20 7c 7c 20 4f 5f 43 4c 4f 45 58 45 43 EC) || O_CLOEXEC
defa0 3d 3d 30 29 0a 20 20 69 66 28 20 66 64 3e 3d 30 ==0). if( fd>=0
defb0 20 29 20 6f 73 46 63 6e 74 6c 28 66 64 2c 20 46 ) osFcntl(fd, F
defc0 5f 53 45 54 46 44 2c 20 6f 73 46 63 6e 74 6c 28 _SETFD, osFcntl(
defd0 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20 fd, F_GETFD, 0)
defe0 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 | FD_CLOEXEC);.#
deff0 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 66 endif. return f
df000 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 d;.}../*.** Help
df010 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 er functions to
df020 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 69 6e obtain and relin
df030 71 75 69 73 68 20 74 68 65 20 67 6c 6f 62 61 6c quish the global
df040 20 6d 75 74 65 78 2e 20 54 68 65 0a 2a 2a 20 67 mutex. The.** g
df050 6c 6f 62 61 6c 20 6d 75 74 65 78 20 69 73 20 75 lobal mutex is u
df060 73 65 64 20 74 6f 20 70 72 6f 74 65 63 74 20 74 sed to protect t
df070 68 65 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f he unixInodeInfo
df080 20 61 6e 64 0a 2a 2a 20 76 78 77 6f 72 6b 73 46 and.** vxworksF
df090 69 6c 65 49 64 20 6f 62 6a 65 63 74 73 20 75 73 ileId objects us
df0a0 65 64 20 62 79 20 74 68 69 73 20 66 69 6c 65 2c ed by this file,
df0b0 20 61 6c 6c 20 6f 66 20 77 68 69 63 68 20 6d 61 all of which ma
df0c0 79 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 y be .** shared
df0d0 62 79 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 by multiple thre
df0e0 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 ads..**.** Funct
df0f0 69 6f 6e 20 75 6e 69 78 4d 75 74 65 78 48 65 6c ion unixMutexHel
df100 64 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 d() is used to a
df110 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65 ssert() that the
df120 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 0a 2a global mutex .*
df130 2a 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20 72 * is held when r
df140 65 71 75 69 72 65 64 2e 20 54 68 69 73 20 66 75 equired. This fu
df150 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 nction is only u
df160 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 sed as part of a
df170 73 73 65 72 74 28 29 20 0a 2a 2a 20 73 74 61 74 ssert() .** stat
df180 65 6d 65 6e 74 73 2e 20 65 2e 67 2e 0a 2a 2a 0a ements. e.g..**.
df190 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 ** unixEnterMu
df1a0 74 65 78 28 29 0a 2a 2a 20 20 20 20 20 61 73 73 tex().** ass
df1b0 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65 ert( unixMutexHe
df1c0 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 20 75 6e 69 ld() );.** uni
df1d0 78 45 6e 74 65 72 4c 65 61 76 65 28 29 0a 2a 2f xEnterLeave().*/
df1e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 .static void uni
df1f0 78 45 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64 xEnterMutex(void
df200 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 ){. sqlite3_mut
df210 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 ex_enter(sqlite3
df220 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
df230 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
df240 41 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69 ASTER));.}.stati
df250 63 20 76 6f 69 64 20 75 6e 69 78 4c 65 61 76 65 c void unixLeave
df260 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 Mutex(void){. s
df270 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
df280 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 ve(sqlite3MutexA
df290 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
df2a0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
df2b0 29 3b 0a 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 );.}.#ifdef SQLI
df2c0 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 TE_DEBUG.static
df2d0 69 6e 74 20 75 6e 69 78 4d 75 74 65 78 48 65 6c int unixMutexHel
df2e0 64 28 76 6f 69 64 29 20 7b 0a 20 20 72 65 74 75 d(void) {. retu
df2f0 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 rn sqlite3_mutex
df300 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74 _held(sqlite3Mut
df310 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
df320 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
df330 45 52 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a ER));.}.#endif..
df340 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
df350 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66 ITE_TEST) && def
df360 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
df370 47 29 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 G)./*.** Helper
df380 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 72 69 function for pri
df390 6e 74 69 6e 67 20 6f 75 74 20 74 72 61 63 65 20 nting out trace
df3a0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d information from
df3b0 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 62 69 debugging.** bi
df3c0 6e 61 72 69 65 73 2e 20 54 68 69 73 20 72 65 74 naries. This ret
df3d0 75 72 6e 73 20 74 68 65 20 73 74 72 69 6e 67 20 urns the string
df3e0 72 65 70 72 65 73 65 74 61 74 69 6f 6e 20 6f 66 represetation of
df3f0 20 74 68 65 20 73 75 70 70 6c 69 65 64 0a 2a 2a the supplied.**
df400 20 69 6e 74 65 67 65 72 20 6c 6f 63 6b 2d 74 79 integer lock-ty
df410 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f pe..*/.static co
df420 6e 73 74 20 63 68 61 72 20 2a 61 7a 46 69 6c 65 nst char *azFile
df430 4c 6f 63 6b 28 69 6e 74 20 65 46 69 6c 65 4c 6f Lock(int eFileLo
df440 63 6b 29 7b 0a 20 20 73 77 69 74 63 68 28 20 65 ck){. switch( e
df450 46 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 FileLock ){.
df460 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a 20 72 65 case NO_LOCK: re
df470 74 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a 20 20 20 turn "NONE";.
df480 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c 4f 43 case SHARED_LOC
df490 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41 52 45 K: return "SHARE
df4a0 44 22 3b 0a 20 20 20 20 63 61 73 65 20 52 45 53 D";. case RES
df4b0 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 ERVED_LOCK: retu
df4c0 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a 20 rn "RESERVED";.
df4d0 20 20 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f case PENDING_
df4e0 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 LOCK: return "PE
df4f0 4e 44 49 4e 47 22 3b 0a 20 20 20 20 63 61 73 65 NDING";. case
df500 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a EXCLUSIVE_LOCK:
df510 20 72 65 74 75 72 6e 20 22 45 58 43 4c 55 53 49 return "EXCLUSI
df520 56 45 22 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 VE";. }. retur
df530 6e 20 22 45 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e n "ERROR";.}.#en
df540 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
df550 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2f 2a TE_LOCK_TRACE./*
df560 0a 2a 2a 20 50 72 69 6e 74 20 6f 75 74 20 69 6e .** Print out in
df570 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
df580 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 all locking oper
df590 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ations..**.** Th
df5a0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
df5b0 65 64 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 ed for troublesh
df5c0 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20 ooting locks on
df5d0 6d 75 6c 74 69 74 68 72 65 61 64 65 64 0a 2a 2a multithreaded.**
df5e0 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 45 6e 61 platforms. Ena
df5f0 62 6c 65 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 ble by compiling
df600 20 77 69 74 68 20 74 68 65 20 2d 44 53 51 4c 49 with the -DSQLI
df610 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2a 2a TE_LOCK_TRACE.**
df620 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70 command-line op
df630 74 69 6f 6e 20 6f 6e 20 74 68 65 20 63 6f 6d 70 tion on the comp
df640 69 6c 65 72 2e 20 20 54 68 69 73 20 63 6f 64 65 iler. This code
df650 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 is normally.**
df660 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2f 0a 73 turned off..*/.s
df670 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54 72 tatic int lockTr
df680 61 63 65 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 ace(int fd, int
df690 6f 70 2c 20 73 74 72 75 63 74 20 66 6c 6f 63 6b op, struct flock
df6a0 20 2a 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4f *p){. char *zO
df6b0 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 65 3b 0a 20 pName, *zType;.
df6c0 20 69 6e 74 20 73 3b 0a 20 20 69 6e 74 20 73 61 int s;. int sa
df6d0 76 65 64 45 72 72 6e 6f 3b 0a 20 20 69 66 28 20 vedErrno;. if(
df6e0 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20 29 7b 0a 20 op==F_GETLK ){.
df6f0 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 47 45 zOpName = "GE
df700 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 TLK";. }else if
df710 28 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 29 7b ( op==F_SETLK ){
df720 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 . zOpName = "
df730 53 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b SETLK";. }else{
df740 0a 20 20 20 20 73 20 3d 20 6f 73 46 63 6e 74 6c . s = osFcntl
df750 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 20 (fd, op, p);.
df760 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
df770 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b 6e 6f ntf("fcntl unkno
df780 77 6e 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 wn %d %d %d\n",
df790 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20 20 20 fd, op, s);.
df7a0 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a 20 20 return s;. }.
df7b0 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 if( p->l_type==F
df7c0 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 _RDLCK ){. zT
df7d0 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 ype = "RDLCK";.
df7e0 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f }else if( p->l_
df7f0 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b type==F_WRLCK ){
df800 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 . zType = "WR
df810 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 LCK";. }else if
df820 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 55 ( p->l_type==F_U
df830 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 NLCK ){. zTyp
df840 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 7d e = "UNLCK";. }
df850 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
df860 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 ( 0 );. }. ass
df870 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e 63 65 ert( p->l_whence
df880 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a 20 20 ==SEEK_SET );.
df890 73 20 3d 20 6f 73 46 63 6e 74 6c 28 66 64 2c 20 s = osFcntl(fd,
df8a0 6f 70 2c 20 70 29 3b 0a 20 20 73 61 76 65 64 45 op, p);. savedE
df8b0 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
df8c0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
df8d0 74 66 28 22 66 63 6e 74 6c 20 25 64 20 25 64 20 tf("fcntl %d %d
df8e0 25 73 20 25 73 20 25 64 20 25 64 20 25 64 20 25 %s %s %d %d %d %
df8f0 64 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72 65 61 d\n",. threa
df900 64 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65 did, fd, zOpName
df910 2c 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 70 2d , zType, (int)p-
df920 3e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 70 >l_start, (int)p
df930 2d 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69 ->l_len,. (i
df940 6e 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b nt)p->l_pid, s);
df950 0a 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 26 . if( s==(-1) &
df960 26 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 & op==F_SETLK &&
df970 20 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 (p->l_type==F_R
df980 44 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 DLCK || p->l_typ
df990 65 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20 e==F_WRLCK) ){.
df9a0 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 struct flock
df9b0 6c 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b l2;. l2 = *p;
df9c0 0a 20 20 20 20 6f 73 46 63 6e 74 6c 28 66 64 2c . osFcntl(fd,
df9d0 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a F_GETLK, &l2);.
df9e0 20 20 20 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 if( l2.l_typ
df9f0 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 e==F_RDLCK ){.
dfa00 20 20 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c zType = "RDL
dfa10 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 CK";. }else i
dfa20 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f f( l2.l_type==F_
dfa30 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a WRLCK ){. z
dfa40 54 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a Type = "WRLCK";.
dfa50 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 }else if( l2
dfa60 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b .l_type==F_UNLCK
dfa70 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 ){. zType
dfa80 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d = "UNLCK";. }
dfa90 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 else{. asse
dfaa0 72 74 28 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20 rt( 0 );. }.
dfab0 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 sqlite3DebugP
dfac0 72 69 6e 74 66 28 22 66 63 6e 74 6c 2d 66 61 69 rintf("fcntl-fai
dfad0 6c 75 72 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20 lure-reason: %s
dfae0 25 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 %d %d %d\n",.
dfaf0 20 20 20 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 zType, (int)
dfb00 6c 32 2e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 l2.l_start, (int
dfb10 29 6c 32 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 )l2.l_len, (int)
dfb20 6c 32 2e 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 l2.l_pid);. }.
dfb30 20 65 72 72 6e 6f 20 3d 20 73 61 76 65 64 45 72 errno = savedEr
dfb40 72 6e 6f 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b rno;. return s;
dfb50 0a 7d 0a 23 75 6e 64 65 66 20 6f 73 46 63 6e 74 .}.#undef osFcnt
dfb60 6c 0a 23 64 65 66 69 6e 65 20 6f 73 46 63 6e 74 l.#define osFcnt
dfb70 6c 20 6c 6f 63 6b 54 72 61 63 65 0a 23 65 6e 64 l lockTrace.#end
dfb80 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 if /* SQLITE_LOC
dfb90 4b 5f 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a K_TRACE */../*.*
dfba0 2a 20 52 65 74 72 79 20 66 74 72 75 6e 63 61 74 * Retry ftruncat
dfbb0 65 28 29 20 63 61 6c 6c 73 20 74 68 61 74 20 66 e() calls that f
dfbc0 61 69 6c 20 64 75 65 20 74 6f 20 45 49 4e 54 52 ail due to EINTR
dfbd0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
dfbe0 6f 62 75 73 74 5f 66 74 72 75 6e 63 61 74 65 28 obust_ftruncate(
dfbf0 69 6e 74 20 68 2c 20 73 71 6c 69 74 65 33 5f 69 int h, sqlite3_i
dfc00 6e 74 36 34 20 73 7a 29 7b 0a 20 20 69 6e 74 20 nt64 sz){. int
dfc10 72 63 3b 0a 20 20 64 6f 7b 20 72 63 20 3d 20 6f rc;. do{ rc = o
dfc20 73 46 74 72 75 6e 63 61 74 65 28 68 2c 73 7a 29 sFtruncate(h,sz)
dfc30 3b 20 7d 77 68 69 6c 65 28 20 72 63 3c 30 20 26 ; }while( rc<0 &
dfc40 26 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 29 & errno==EINTR )
dfc50 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
dfc60 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
dfc70 74 69 6e 65 20 74 72 61 6e 73 6c 61 74 65 73 20 tine translates
dfc80 61 20 73 74 61 6e 64 61 72 64 20 50 4f 53 49 58 a standard POSIX
dfc90 20 65 72 72 6e 6f 20 63 6f 64 65 20 69 6e 74 6f errno code into
dfca0 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 75 73 something.** us
dfcb0 65 66 75 6c 20 74 6f 20 74 68 65 20 63 6c 69 65 eful to the clie
dfcc0 6e 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 nts of the sqlit
dfcd0 65 33 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 53 e3 functions. S
dfce0 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 69 74 20 pecifically, it
dfcf0 69 73 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 74 is.** intended t
dfd00 6f 20 74 72 61 6e 73 6c 61 74 65 20 61 20 76 61 o translate a va
dfd10 72 69 65 74 79 20 6f 66 20 22 74 72 79 20 61 67 riety of "try ag
dfd20 61 69 6e 22 20 65 72 72 6f 72 73 20 69 6e 74 6f ain" errors into
dfd30 20 53 51 4c 49 54 45 5f 42 55 53 59 0a 2a 2a 20 SQLITE_BUSY.**
dfd40 61 6e 64 20 61 20 76 61 72 69 65 74 79 20 6f 66 and a variety of
dfd50 20 22 70 6c 65 61 73 65 20 63 6c 6f 73 65 20 74 "please close t
dfd60 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
dfd70 6f 72 20 4e 4f 57 22 20 65 72 72 6f 72 73 20 69 or NOW" errors i
dfd80 6e 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 nto .** SQLITE_I
dfd90 4f 45 52 52 0a 2a 2a 20 0a 2a 2a 20 45 72 72 6f OERR.** .** Erro
dfda0 72 73 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 rs during initia
dfdb0 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 6f 63 6b lization of lock
dfdc0 73 2c 20 6f 72 20 66 69 6c 65 20 73 79 73 74 65 s, or file syste
dfdd0 6d 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6c 6f m support for lo
dfde0 63 6b 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 68 cks,.** should h
dfdf0 61 6e 64 6c 65 20 45 4e 4f 4c 43 4b 2c 20 45 4e andle ENOLCK, EN
dfe00 4f 54 53 55 50 2c 20 45 4f 50 4e 4f 54 53 55 50 OTSUP, EOPNOTSUP
dfe10 50 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 2a 2f P separately..*/
dfe20 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 .static int sqli
dfe30 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
dfe40 45 72 72 6f 72 28 69 6e 74 20 70 6f 73 69 78 45 Error(int posixE
dfe50 72 72 6f 72 2c 20 69 6e 74 20 73 71 6c 69 74 65 rror, int sqlite
dfe60 49 4f 45 72 72 29 20 7b 0a 20 20 73 77 69 74 63 IOErr) {. switc
dfe70 68 20 28 70 6f 73 69 78 45 72 72 6f 72 29 20 7b h (posixError) {
dfe80 0a 23 69 66 20 30 0a 20 20 2f 2a 20 41 74 20 6f .#if 0. /* At o
dfe90 6e 65 20 70 6f 69 6e 74 20 74 68 69 73 20 63 6f ne point this co
dfea0 64 65 20 77 61 73 20 6e 6f 74 20 63 6f 6d 6d 65 de was not comme
dfeb0 6e 74 65 64 20 6f 75 74 2e 20 49 6e 20 74 68 65 nted out. In the
dfec0 6f 72 79 2c 20 74 68 69 73 20 62 72 61 6e 63 68 ory, this branch
dfed0 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 6e 65 76 . ** should nev
dfee0 65 72 20 62 65 20 68 69 74 2c 20 61 73 20 74 68 er be hit, as th
dfef0 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 is function shou
dff00 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 ld only be calle
dff10 64 20 61 66 74 65 72 0a 20 20 2a 2a 20 61 20 6c d after. ** a l
dff20 6f 63 6b 69 6e 67 2d 72 65 6c 61 74 65 64 20 66 ocking-related f
dff30 75 6e 63 74 69 6f 6e 20 28 69 2e 65 2e 20 66 63 unction (i.e. fc
dff40 6e 74 6c 28 29 29 20 68 61 73 20 72 65 74 75 72 ntl()) has retur
dff50 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 20 77 69 74 ned non-zero wit
dff60 68 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 h. ** the value
dff70 20 6f 66 20 65 72 72 6e 6f 20 61 73 20 74 68 65 of errno as the
dff80 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
dff90 20 53 69 6e 63 65 20 61 20 73 79 73 74 65 6d 20 Since a system
dffa0 63 61 6c 6c 20 68 61 73 20 66 61 69 6c 65 64 2c call has failed,
dffb0 0a 20 20 2a 2a 20 65 72 72 6e 6f 20 73 68 6f 75 . ** errno shou
dffc0 6c 64 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a ld be non-zero..
dffd0 20 20 2a 2a 0a 20 20 2a 2a 20 44 65 73 70 69 74 **. ** Despit
dffe0 65 20 74 68 69 73 2c 20 69 66 20 65 72 72 6e 6f e this, if errno
dfff0 20 72 65 61 6c 6c 79 20 69 73 20 7a 65 72 6f 2c really is zero,
e0000 20 77 65 20 73 74 69 6c 6c 20 64 6f 6e 27 74 20 we still don't
e0010 77 61 6e 74 20 74 6f 20 72 65 74 75 72 6e 0a 20 want to return.
e0020 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 ** SQLITE_OK. T
e0030 68 65 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 66 he system call f
e0040 61 69 6c 65 64 2c 20 61 6e 64 20 2a 73 6f 6d 65 ailed, and *some
e0050 2a 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 73 * SQLite error s
e0060 68 6f 75 6c 64 20 62 65 0a 20 20 2a 2a 20 70 72 hould be. ** pr
e0070 6f 70 61 67 61 74 65 64 20 62 61 63 6b 20 74 6f opagated back to
e0080 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 43 6f 6d the caller. Com
e0090 6d 65 6e 74 69 6e 67 20 74 68 69 73 20 62 72 61 menting this bra
e00a0 6e 63 68 20 6f 75 74 20 6d 65 61 6e 73 20 65 72 nch out means er
e00b0 72 6e 6f 3d 3d 30 0a 20 20 2a 2a 20 77 69 6c 6c rno==0. ** will
e00c0 20 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20 74 be handled by t
e00d0 68 65 20 22 64 65 66 61 75 6c 74 3a 22 20 63 61 he "default:" ca
e00e0 73 65 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 se below.. */.
e00f0 20 63 61 73 65 20 30 3a 20 0a 20 20 20 20 72 65 case 0: . re
e0100 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
e0110 23 65 6e 64 69 66 0a 0a 20 20 63 61 73 65 20 45 #endif.. case E
e0120 41 47 41 49 4e 3a 0a 20 20 63 61 73 65 20 45 54 AGAIN:. case ET
e0130 49 4d 45 44 4f 55 54 3a 0a 20 20 63 61 73 65 20 IMEDOUT:. case
e0140 45 42 55 53 59 3a 0a 20 20 63 61 73 65 20 45 49 EBUSY:. case EI
e0150 4e 54 52 3a 0a 20 20 63 61 73 65 20 45 4e 4f 4c NTR:. case ENOL
e0160 43 4b 3a 20 20 0a 20 20 20 20 2f 2a 20 72 61 6e CK: . /* ran
e0170 64 6f 6d 20 4e 46 53 20 72 65 74 72 79 20 65 72 dom NFS retry er
e0180 72 6f 72 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 ror, unless duri
e0190 6e 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 ng file system s
e01a0 75 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 upport . * i
e01b0 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e ntrospection, in
e01c0 20 77 68 69 63 68 20 69 74 20 61 63 74 75 61 6c which it actual
e01d0 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 ly means what it
e01e0 20 73 61 79 73 20 2a 2f 0a 20 20 20 20 72 65 74 says */. ret
e01f0 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b urn SQLITE_BUSY;
e0200 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 41 43 . . case EAC
e0210 43 45 53 3a 20 0a 20 20 20 20 2f 2a 20 45 41 43 CES: . /* EAC
e0220 43 45 53 20 69 73 20 6c 69 6b 65 20 45 41 47 41 CES is like EAGA
e0230 49 4e 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e IN during lockin
e0240 67 20 6f 70 65 72 61 74 69 6f 6e 73 2c 20 62 75 g operations, bu
e0250 74 20 6e 6f 74 20 61 6e 79 20 6f 74 68 65 72 20 t not any other
e0260 74 69 6d 65 2a 2f 0a 20 20 20 20 69 66 28 20 28 time*/. if( (
e0270 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 sqliteIOErr == S
e0280 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b QLITE_IOERR_LOCK
e0290 29 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 28 73 ) || . (s
e02a0 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 qliteIOErr == SQ
e02b0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
e02c0 4b 29 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 28 K) || . (
e02d0 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 sqliteIOErr == S
e02e0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f QLITE_IOERR_RDLO
e02f0 43 4b 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 28 CK) ||. (
e0300 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 sqliteIOErr == S
e0310 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 QLITE_IOERR_CHEC
e0320 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 29 KRESERVEDLOCK) )
e0330 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
e0340 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
e0350 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 66 61 }. /* else fa
e0360 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 ll through */.
e0370 63 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 20 20 case EPERM: .
e0380 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 return SQLITE_P
e0390 45 52 4d 3b 0a 20 20 20 20 0a 20 20 2f 2a 20 45 ERM;. . /* E
e03a0 44 45 41 44 4c 4b 20 69 73 20 6f 6e 6c 79 20 70 DEADLK is only p
e03b0 6f 73 73 69 62 6c 65 20 69 66 20 61 20 63 61 6c ossible if a cal
e03c0 6c 20 74 6f 20 66 63 6e 74 6c 28 46 5f 53 45 54 l to fcntl(F_SET
e03d0 4c 4b 57 29 20 69 73 20 6d 61 64 65 2e 20 41 6e LKW) is made. An
e03e0 64 0a 20 20 2a 2a 20 74 68 69 73 20 6d 6f 64 75 d. ** this modu
e03f0 6c 65 20 6e 65 76 65 72 20 6d 61 6b 65 73 20 73 le never makes s
e0400 75 63 68 20 61 20 63 61 6c 6c 2e 20 41 6e 64 20 uch a call. And
e0410 74 68 65 20 63 6f 64 65 20 69 6e 20 53 51 4c 69 the code in SQLi
e0420 74 65 20 69 74 73 65 6c 66 20 0a 20 20 2a 2a 20 te itself . **
e0430 61 73 73 65 72 74 73 20 74 68 61 74 20 53 51 4c asserts that SQL
e0440 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 ITE_IOERR_BLOCKE
e0450 44 20 69 73 20 6e 65 76 65 72 20 72 65 74 75 72 D is never retur
e0460 6e 65 64 2e 20 46 6f 72 20 74 68 65 73 65 20 72 ned. For these r
e0470 65 61 73 6f 6e 73 0a 20 20 2a 2a 20 74 68 69 73 easons. ** this
e0480 20 63 61 73 65 20 69 73 20 61 6c 73 6f 20 63 6f case is also co
e0490 6d 6d 65 6e 74 65 64 20 6f 75 74 2e 20 49 66 20 mmented out. If
e04a0 74 68 65 20 73 79 73 74 65 6d 20 64 6f 65 73 20 the system does
e04b0 73 65 74 20 65 72 72 6e 6f 20 74 6f 20 45 44 45 set errno to EDE
e04c0 41 44 4c 4b 2c 0a 20 20 2a 2a 20 74 68 65 20 64 ADLK,. ** the d
e04d0 65 66 61 75 6c 74 20 53 51 4c 49 54 45 5f 49 4f efault SQLITE_IO
e04e0 45 52 52 5f 58 58 58 20 63 6f 64 65 20 77 69 6c ERR_XXX code wil
e04f0 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 2a l be returned. *
e0500 2f 0a 23 69 66 20 30 0a 20 20 63 61 73 65 20 45 /.#if 0. case E
e0510 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72 65 74 75 DEADLK:. retu
e0520 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
e0530 42 4c 4f 43 4b 45 44 3b 0a 23 65 6e 64 69 66 0a BLOCKED;.#endif.
e0540 20 20 20 20 0a 23 69 66 20 45 4f 50 4e 4f 54 53 .#if EOPNOTS
e0550 55 50 50 21 3d 45 4e 4f 54 53 55 50 0a 20 20 63 UPP!=ENOTSUP. c
e0560 61 73 65 20 45 4f 50 4e 4f 54 53 55 50 50 3a 20 ase EOPNOTSUPP:
e0570 0a 20 20 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e . /* somethin
e0580 67 20 77 65 6e 74 20 74 65 72 72 69 62 6c 79 20 g went terribly
e0590 61 77 72 79 2c 20 75 6e 6c 65 73 73 20 64 75 72 awry, unless dur
e05a0 69 6e 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 ing file system
e05b0 73 75 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 support . *
e05c0 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 introspection, i
e05d0 6e 20 77 68 69 63 68 20 69 74 20 61 63 74 75 61 n which it actua
e05e0 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 lly means what i
e05f0 74 20 73 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 t says */.#endif
e0600 0a 23 69 66 64 65 66 20 45 4e 4f 54 53 55 50 0a .#ifdef ENOTSUP.
e0610 20 20 63 61 73 65 20 45 4e 4f 54 53 55 50 3a 20 case ENOTSUP:
e0620 0a 20 20 20 20 2f 2a 20 69 6e 76 61 6c 69 64 20 . /* invalid
e0630 66 64 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e fd, unless durin
e0640 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 g file system su
e0650 70 70 6f 72 74 20 69 6e 74 72 6f 73 70 65 63 74 pport introspect
e0660 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 0a 20 ion, in which .
e0670 20 20 20 20 2a 20 69 74 20 61 63 74 75 61 6c 6c * it actuall
e0680 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 y means what it
e0690 73 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 says */.#endif.
e06a0 20 63 61 73 65 20 45 49 4f 3a 0a 20 20 63 61 73 case EIO:. cas
e06b0 65 20 45 42 41 44 46 3a 0a 20 20 63 61 73 65 20 e EBADF:. case
e06c0 45 49 4e 56 41 4c 3a 0a 20 20 63 61 73 65 20 45 EINVAL:. case E
e06d0 4e 4f 54 43 4f 4e 4e 3a 0a 20 20 63 61 73 65 20 NOTCONN:. case
e06e0 45 4e 4f 44 45 56 3a 0a 20 20 63 61 73 65 20 45 ENODEV:. case E
e06f0 4e 58 49 4f 3a 0a 20 20 63 61 73 65 20 45 4e 4f NXIO:. case ENO
e0700 45 4e 54 3a 0a 23 69 66 64 65 66 20 45 53 54 41 ENT:.#ifdef ESTA
e0710 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LE
e0720 20 20 20 20 20 20 20 2f 2a 20 45 53 54 41 4c 45 /* ESTALE
e0730 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 is not defined
e0740 6f 6e 20 49 6e 74 65 72 69 78 20 73 79 73 74 65 on Interix syste
e0750 6d 73 20 2a 2f 0a 20 20 63 61 73 65 20 45 53 54 ms */. case EST
e0760 41 4c 45 3a 0a 23 65 6e 64 69 66 0a 20 20 63 61 ALE:.#endif. ca
e0770 73 65 20 45 4e 4f 53 59 53 3a 0a 20 20 20 20 2f se ENOSYS:. /
e0780 2a 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 66 * these should f
e0790 6f 72 63 65 20 74 68 65 20 63 6c 69 65 6e 74 20 orce the client
e07a0 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c to close the fil
e07b0 65 20 61 6e 64 20 72 65 63 6f 6e 6e 65 63 74 20 e and reconnect
e07c0 2a 2f 0a 20 20 20 20 0a 20 20 64 65 66 61 75 6c */. . defaul
e07d0 74 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20 73 t: . return s
e07e0 71 6c 69 74 65 49 4f 45 72 72 3b 0a 20 20 7d 0a qliteIOErr;. }.
e07f0 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }..../**********
e0800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0840 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
e0850 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 55 6e ******* Begin Un
e0860 69 71 75 65 20 46 69 6c 65 20 49 44 20 55 74 69 ique File ID Uti
e0870 6c 69 74 79 20 55 73 65 64 20 42 79 20 56 78 57 lity Used By VxW
e0880 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a orks ***********
e0890 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d 6f ****.**.** On mo
e08a0 73 74 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 75 st versions of u
e08b0 6e 69 78 2c 20 77 65 20 63 61 6e 20 67 65 74 20 nix, we can get
e08c0 61 20 75 6e 69 71 75 65 20 49 44 20 66 6f 72 20 a unique ID for
e08d0 61 20 66 69 6c 65 20 62 79 20 63 6f 6e 63 61 74 a file by concat
e08e0 65 6e 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20 64 enating.** the d
e08f0 65 76 69 63 65 20 6e 75 6d 62 65 72 20 61 6e 64 evice number and
e0900 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 the inode numbe
e0910 72 2e 20 20 42 75 74 20 74 68 69 73 20 64 6f 65 r. But this doe
e0920 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 78 s not work on Vx
e0930 57 6f 72 6b 73 2e 0a 2a 2a 20 4f 6e 20 56 78 57 Works..** On VxW
e0940 6f 72 6b 73 2c 20 61 20 75 6e 69 71 75 65 20 66 orks, a unique f
e0950 69 6c 65 20 69 64 20 6d 75 73 74 20 62 65 20 62 ile id must be b
e0960 61 73 65 64 20 6f 6e 20 74 68 65 20 63 61 6e 6f ased on the cano
e0970 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a nical filename..
e0980 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 **.** A pointer
e0990 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f to an instance o
e09a0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
e09b0 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 structure can be
e09c0 20 75 73 65 64 20 61 73 20 61 0a 2a 2a 20 75 6e used as a.** un
e09d0 69 71 75 65 20 66 69 6c 65 20 49 44 20 69 6e 20 ique file ID in
e09e0 56 78 57 6f 72 6b 73 2e 20 20 45 61 63 68 20 69 VxWorks. Each i
e09f0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
e0a00 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
e0a10 6e 73 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 ns.** a copy of
e0a20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 the canonical fi
e0a30 6c 65 6e 61 6d 65 2e 20 20 54 68 65 72 65 20 69 lename. There i
e0a40 73 20 61 6c 73 6f 20 61 20 72 65 66 65 72 65 6e s also a referen
e0a50 63 65 20 63 6f 75 6e 74 2e 20 20 0a 2a 2a 20 54 ce count. .** T
e0a60 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 he structure is
e0a70 72 65 63 6c 61 69 6d 65 64 20 77 68 65 6e 20 74 reclaimed when t
e0a80 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f 69 he number of poi
e0a90 6e 74 65 72 73 20 74 6f 20 69 74 20 64 72 6f 70 nters to it drop
e0aa0 73 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a 2a s to.** zero..**
e0ab0 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 6e 65 .** There are ne
e0ac0 76 65 72 20 76 65 72 79 20 6d 61 6e 79 20 66 69 ver very many fi
e0ad0 6c 65 73 20 6f 70 65 6e 20 61 74 20 6f 6e 65 20 les open at one
e0ae0 74 69 6d 65 20 61 6e 64 20 6c 6f 6f 6b 75 70 73 time and lookups
e0af0 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 20 70 65 are not.** a pe
e0b00 72 66 6f 72 6d 61 6e 63 65 2d 63 72 69 74 69 63 rformance-critic
e0b10 61 6c 20 70 61 74 68 2c 20 73 6f 20 69 74 20 69 al path, so it i
e0b20 73 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 s sufficient to
e0b30 70 75 74 20 74 68 65 73 65 0a 2a 2a 20 73 74 72 put these.** str
e0b40 75 63 74 75 72 65 73 20 6f 6e 20 61 20 6c 69 6e uctures on a lin
e0b50 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 ked list..*/.str
e0b60 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 uct vxworksFileI
e0b70 64 20 7b 0a 20 20 73 74 72 75 63 74 20 76 78 77 d {. struct vxw
e0b80 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 78 orksFileId *pNex
e0b90 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 61 t; /* Next in a
e0ba0 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c list of them al
e0bb0 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b l */. int nRef;
e0bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0bd0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
e0be0 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 f references to
e0bf0 74 68 69 73 20 6f 6e 65 20 2a 2f 0a 20 20 69 6e this one */. in
e0c00 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 t nName;
e0c10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
e0c20 65 6e 67 74 68 20 6f 66 20 74 68 65 20 7a 43 61 ength of the zCa
e0c30 6e 6f 6e 69 63 61 6c 4e 61 6d 65 5b 5d 20 73 74 nonicalName[] st
e0c40 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a ring */. char *
e0c50 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 3b 20 zCanonicalName;
e0c60 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e 6f 6e /* Canon
e0c70 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 2a 2f ical filename */
e0c80 0a 7d 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f .};..#if OS_VXWO
e0c90 52 4b 53 0a 2f 2a 20 0a 2a 2a 20 41 6c 6c 20 75 RKS./* .** All u
e0ca0 6e 69 71 75 65 20 66 69 6c 65 6e 61 6d 65 73 20 nique filenames
e0cb0 61 72 65 20 68 65 6c 64 20 6f 6e 20 61 20 6c 69 are held on a li
e0cc0 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 nked list headed
e0cd0 20 62 79 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 by this.** vari
e0ce0 61 62 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 able:.*/.static
e0cf0 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 struct vxworksFi
e0d00 6c 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 6c leId *vxworksFil
e0d10 65 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a eList = 0;../*.*
e0d20 2a 20 53 69 6d 70 6c 69 66 79 20 61 20 66 69 6c * Simplify a fil
e0d30 65 6e 61 6d 65 20 69 6e 74 6f 20 69 74 73 20 63 ename into its c
e0d40 61 6e 6f 6e 69 63 61 6c 20 66 6f 72 6d 0a 2a 2a anonical form.**
e0d50 20 62 79 20 6d 61 6b 69 6e 67 20 74 68 65 20 66 by making the f
e0d60 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 ollowing changes
e0d70 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 72 65 6d 6f 76 :.**.** * remov
e0d80 69 6e 67 20 61 6e 79 20 74 72 61 69 6c 69 6e 67 ing any trailing
e0d90 20 61 6e 64 20 64 75 70 6c 69 63 61 74 65 20 2f and duplicate /
e0da0 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f .** * convert /
e0db0 2e 2f 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a ./ into just /.*
e0dc0 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f 41 2f * * convert /A/
e0dd0 2e 2e 2f 20 77 68 65 72 65 20 41 20 69 73 20 61 ../ where A is a
e0de0 6e 79 20 73 69 6d 70 6c 65 20 6e 61 6d 65 20 69 ny simple name i
e0df0 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 0a 2a 2a nto just /.**.**
e0e00 20 43 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 Changes are mad
e0e10 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 52 65 74 e in-place. Ret
e0e20 75 72 6e 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 urn the new name
e0e30 20 6c 65 6e 67 74 68 2e 0a 2a 2a 0a 2a 2a 20 54 length..**.** T
e0e40 68 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 he original file
e0e50 6e 61 6d 65 20 69 73 20 69 6e 20 7a 5b 30 2e 2e name is in z[0..
e0e60 6e 2d 31 5d 2e 20 20 52 65 74 75 72 6e 20 74 68 n-1]. Return th
e0e70 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 e number of.** c
e0e80 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 haracters in the
e0e90 20 73 69 6d 70 6c 69 66 69 65 64 20 6e 61 6d 65 simplified name
e0ea0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e0eb0 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 79 4e vxworksSimplifyN
e0ec0 61 6d 65 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74 ame(char *z, int
e0ed0 20 6e 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b n){. int i, j;
e0ee0 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 . while( n>1 &&
e0ef0 20 7a 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20 z[n-1]=='/' ){
e0f00 6e 2d 2d 3b 20 7d 0a 20 20 66 6f 72 28 69 3d 6a n--; }. for(i=j
e0f10 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 =0; i<n; i++){.
e0f20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2f 27 if( z[i]=='/'
e0f30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b ){. if( z[
e0f40 69 2b 31 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 i+1]=='/' ) cont
e0f50 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20 inue;. if(
e0f60 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 z[i+1]=='.' && i
e0f70 2b 32 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d +2<n && z[i+2]==
e0f80 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 '/' ){. i
e0f90 20 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63 += 1;. c
e0fa0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d ontinue;. }
e0fb0 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 . if( z[i+1
e0fc0 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 33 3c 6e 20 ]=='.' && i+3<n
e0fd0 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2e 27 20 26 && z[i+2]=='.' &
e0fe0 26 20 7a 5b 69 2b 33 5d 3d 3d 27 2f 27 20 29 7b & z[i+3]=='/' ){
e0ff0 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
e1000 6a 3e 30 20 26 26 20 7a 5b 6a 2d 31 5d 21 3d 27 j>0 && z[j-1]!='
e1010 2f 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 /' ){ j--; }.
e1020 20 20 20 20 20 69 66 28 20 6a 3e 30 20 29 7b 20 if( j>0 ){
e1030 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 j--; }. i
e1040 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 63 += 2;. c
e1050 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d ontinue;. }
e1060 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 2b 2b . }. z[j++
e1070 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 7d 0a 20 20 ] = z[i];. }.
e1080 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 z[j] = 0;. retu
e1090 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 rn j;.}../*.** F
e10a0 69 6e 64 20 61 20 75 6e 69 71 75 65 20 66 69 6c ind a unique fil
e10b0 65 20 49 44 20 66 6f 72 20 74 68 65 20 67 69 76 e ID for the giv
e10c0 65 6e 20 61 62 73 6f 6c 75 74 65 20 70 61 74 68 en absolute path
e10d0 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a name. Return.**
e10e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
e10f0 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 e vxworksFileId
e1100 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 70 6f object. This po
e1110 69 6e 74 65 72 20 69 73 20 74 68 65 20 75 6e 69 inter is the uni
e1120 71 75 65 0a 2a 2a 20 66 69 6c 65 20 49 44 2e 0a que.** file ID..
e1130 2a 2a 0a 2a 2a 20 54 68 65 20 6e 52 65 66 20 66 **.** The nRef f
e1140 69 65 6c 64 20 6f 66 20 74 68 65 20 76 78 77 6f ield of the vxwo
e1150 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 rksFileId object
e1160 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 is incremented
e1170 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6f 62 before.** the ob
e1180 6a 65 63 74 20 69 73 20 72 65 74 75 72 6e 65 64 ject is returned
e1190 2e 20 20 41 20 6e 65 77 20 76 78 77 6f 72 6b 73 . A new vxworks
e11a0 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 73 FileId object is
e11b0 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 created.** and
e11c0 61 64 64 65 64 20 74 6f 20 74 68 65 20 67 6c 6f added to the glo
e11d0 62 61 6c 20 6c 69 73 74 20 69 66 20 6e 65 63 65 bal list if nece
e11e0 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ssary..**.** If
e11f0 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
e1200 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ion error occurs
e1210 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a , return NULL..*
e1220 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 /.static struct
e1230 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 vxworksFileId *v
e1240 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49 64 xworksFindFileId
e1250 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 62 (const char *zAb
e1260 73 6f 6c 75 74 65 4e 61 6d 65 29 7b 0a 20 20 73 soluteName){. s
e1270 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c truct vxworksFil
e1280 65 49 64 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 eId *pNew;
e1290 20 20 20 2f 2a 20 73 65 61 72 63 68 20 6b 65 79 /* search key
e12a0 20 61 6e 64 20 6e 65 77 20 66 69 6c 65 20 49 44 and new file ID
e12b0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78 77 */. struct vxw
e12c0 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 43 61 6e orksFileId *pCan
e12d0 64 69 64 61 74 65 3b 20 20 20 2f 2a 20 46 6f 72 didate; /* For
e12e0 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 looping over ex
e12f0 69 73 74 69 6e 67 20 66 69 6c 65 20 49 44 73 20 isting file IDs
e1300 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 */. int n;
e1310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1320 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 /* Leng
e1330 74 68 20 6f 66 20 7a 41 62 73 6f 6c 75 74 65 4e th of zAbsoluteN
e1340 61 6d 65 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 ame string */..
e1350 20 61 73 73 65 72 74 28 20 7a 41 62 73 6f 6c 75 assert( zAbsolu
e1360 74 65 4e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29 teName[0]=='/' )
e1370 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 29 73 74 72 ;. n = (int)str
e1380 6c 65 6e 28 7a 41 62 73 6f 6c 75 74 65 4e 61 6d len(zAbsoluteNam
e1390 65 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c e);. pNew = sql
e13a0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a ite3_malloc( siz
e13b0 65 6f 66 28 2a 70 4e 65 77 29 20 2b 20 28 6e 2b eof(*pNew) + (n+
e13c0 31 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 1) );. if( pNew
e13d0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
e13e0 20 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 pNew->zCanonic
e13f0 61 6c 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 alName = (char*)
e1400 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 &pNew[1];. memc
e1410 70 79 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 py(pNew->zCanoni
e1420 63 61 6c 4e 61 6d 65 2c 20 7a 41 62 73 6f 6c 75 calName, zAbsolu
e1430 74 65 4e 61 6d 65 2c 20 6e 2b 31 29 3b 0a 20 20 teName, n+1);.
e1440 6e 20 3d 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c n = vxworksSimpl
e1450 69 66 79 4e 61 6d 65 28 70 4e 65 77 2d 3e 7a 43 ifyName(pNew->zC
e1460 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 anonicalName, n)
e1470 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66 ;.. /* Search f
e1480 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 65 or an existing e
e1490 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 69 ntry that matchi
e14a0 6e 67 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c ng the canonical
e14b0 20 6e 61 6d 65 2e 0a 20 20 2a 2a 20 49 66 20 66 name.. ** If f
e14c0 6f 75 6e 64 2c 20 69 6e 63 72 65 6d 65 6e 74 20 ound, increment
e14d0 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
e14e0 75 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 unt and return a
e14f0 20 70 6f 69 6e 74 65 72 20 74 6f 0a 20 20 2a 2a pointer to. **
e1500 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 66 69 the existing fi
e1510 6c 65 20 49 44 2e 0a 20 20 2a 2f 0a 20 20 75 6e le ID.. */. un
e1520 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a ixEnterMutex();.
e1530 20 20 66 6f 72 28 70 43 61 6e 64 69 64 61 74 65 for(pCandidate
e1540 3d 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 =vxworksFileList
e1550 3b 20 70 43 61 6e 64 69 64 61 74 65 3b 20 70 43 ; pCandidate; pC
e1560 61 6e 64 69 64 61 74 65 3d 70 43 61 6e 64 69 64 andidate=pCandid
e1570 61 74 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ate->pNext){.
e1580 20 69 66 28 20 70 43 61 6e 64 69 64 61 74 65 2d if( pCandidate-
e1590 3e 6e 4e 61 6d 65 3d 3d 6e 20 0a 20 20 20 20 20 >nName==n .
e15a0 26 26 20 6d 65 6d 63 6d 70 28 70 43 61 6e 64 69 && memcmp(pCandi
e15b0 64 61 74 65 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c date->zCanonical
e15c0 4e 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a 43 61 6e Name, pNew->zCan
e15d0 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3d 3d onicalName, n)==
e15e0 30 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 0. ){.
e15f0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 sqlite3_free(pNe
e1600 77 29 3b 0a 20 20 20 20 20 20 20 70 43 61 6e 64 w);. pCand
e1610 69 64 61 74 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 idate->nRef++;.
e1620 20 20 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d unixLeaveM
e1630 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 20 72 utex();. r
e1640 65 74 75 72 6e 20 70 43 61 6e 64 69 64 61 74 65 eturn pCandidate
e1650 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
e1660 2a 20 4e 6f 20 6d 61 74 63 68 20 77 61 73 20 66 * No match was f
e1670 6f 75 6e 64 2e 20 20 57 65 20 77 69 6c 6c 20 6d ound. We will m
e1680 61 6b 65 20 61 20 6e 65 77 20 66 69 6c 65 20 49 ake a new file I
e1690 44 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 D */. pNew->nRe
e16a0 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e f = 1;. pNew->n
e16b0 4e 61 6d 65 20 3d 20 6e 3b 0a 20 20 70 4e 65 77 Name = n;. pNew
e16c0 2d 3e 70 4e 65 78 74 20 3d 20 76 78 77 6f 72 6b ->pNext = vxwork
e16d0 73 46 69 6c 65 4c 69 73 74 3b 0a 20 20 76 78 77 sFileList;. vxw
e16e0 6f 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 70 orksFileList = p
e16f0 4e 65 77 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65 New;. unixLeave
e1700 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 Mutex();. retur
e1710 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n pNew;.}../*.**
e1720 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 Decrement the r
e1730 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f eference count o
e1740 6e 20 61 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 n a vxworksFileI
e1750 64 20 6f 62 6a 65 63 74 2e 20 20 46 72 65 65 0a d object. Free.
e1760 2a 2a 20 74 68 65 20 6f 62 6a 65 63 74 20 77 68 ** the object wh
e1770 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 en the reference
e1780 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a count reaches z
e1790 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ero..*/.static v
e17a0 6f 69 64 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 oid vxworksRelea
e17b0 73 65 46 69 6c 65 49 64 28 73 74 72 75 63 74 20 seFileId(struct
e17c0 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 vxworksFileId *p
e17d0 49 64 29 7b 0a 20 20 75 6e 69 78 45 6e 74 65 72 Id){. unixEnter
e17e0 4d 75 74 65 78 28 29 3b 0a 20 20 61 73 73 65 72 Mutex();. asser
e17f0 74 28 20 70 49 64 2d 3e 6e 52 65 66 3e 30 20 29 t( pId->nRef>0 )
e1800 3b 0a 20 20 70 49 64 2d 3e 6e 52 65 66 2d 2d 3b ;. pId->nRef--;
e1810 0a 20 20 69 66 28 20 70 49 64 2d 3e 6e 52 65 66 . if( pId->nRef
e1820 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 ==0 ){. struc
e1830 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 t vxworksFileId
e1840 2a 2a 70 70 3b 0a 20 20 20 20 66 6f 72 28 70 70 **pp;. for(pp
e1850 3d 26 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 =&vxworksFileLis
e1860 74 3b 20 2a 70 70 20 26 26 20 2a 70 70 21 3d 70 t; *pp && *pp!=p
e1870 49 64 3b 20 70 70 20 3d 20 26 28 28 2a 70 70 29 Id; pp = &((*pp)
e1880 2d 3e 70 4e 65 78 74 29 29 7b 7d 0a 20 20 20 20 ->pNext)){}.
e1890 61 73 73 65 72 74 28 20 2a 70 70 3d 3d 70 49 64 assert( *pp==pId
e18a0 20 29 3b 0a 20 20 20 20 2a 70 70 20 3d 20 70 49 );. *pp = pI
e18b0 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 d->pNext;. sq
e18c0 6c 69 74 65 33 5f 66 72 65 65 28 70 49 64 29 3b lite3_free(pId);
e18d0 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 . }. unixLeave
e18e0 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69 Mutex();.}.#endi
e18f0 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 f /* OS_VXWORKS
e1900 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
e1910 2a 2a 2a 20 45 6e 64 20 6f 66 20 55 6e 69 71 75 *** End of Uniqu
e1920 65 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69 74 e File ID Utilit
e1930 79 20 55 73 65 64 20 42 79 20 56 78 57 6f 72 6b y Used By VxWork
e1940 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s **************
e1950 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
e1960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e19a0 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */.../**********
e19b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e19c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e19d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e19e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e19f0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
e1a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1a10 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 Posix Advisory
e1a20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a Locking ********
e1a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1a40 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58 ****.**.** POSIX
e1a50 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 advisory locks
e1a60 61 72 65 20 62 72 6f 6b 65 6e 20 62 79 20 64 65 are broken by de
e1a70 73 69 67 6e 2e 20 20 41 4e 53 49 20 53 54 44 20 sign. ANSI STD
e1a80 31 30 30 33 2e 31 20 28 31 39 39 36 29 0a 2a 2a 1003.1 (1996).**
e1a90 20 73 65 63 74 69 6f 6e 20 36 2e 35 2e 32 2e 32 section 6.5.2.2
e1aa0 20 6c 69 6e 65 73 20 34 38 33 20 74 68 72 6f 75 lines 483 throu
e1ab0 67 68 20 34 39 30 20 73 70 65 63 69 66 79 20 74 gh 490 specify t
e1ac0 68 61 74 20 77 68 65 6e 20 61 20 70 72 6f 63 65 hat when a proce
e1ad0 73 73 0a 2a 2a 20 73 65 74 73 20 6f 72 20 63 6c ss.** sets or cl
e1ae0 65 61 72 73 20 61 20 6c 6f 63 6b 2c 20 74 68 61 ears a lock, tha
e1af0 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 76 65 72 t operation over
e1b00 72 69 64 65 73 20 61 6e 79 20 70 72 69 6f 72 20 rides any prior
e1b10 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a 20 62 79 20 locks set.** by
e1b20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 the same process
e1b30 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 65 . It does not e
e1b40 78 70 6c 69 63 69 74 6c 79 20 73 61 79 20 73 6f xplicitly say so
e1b50 2c 20 62 75 74 20 74 68 69 73 20 69 6d 70 6c 69 , but this impli
e1b60 65 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 6f 76 es.** that it ov
e1b70 65 72 72 69 64 65 73 20 6c 6f 63 6b 73 20 73 65 errides locks se
e1b80 74 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 t by the same pr
e1b90 6f 63 65 73 73 20 75 73 69 6e 67 20 61 20 64 69 ocess using a di
e1ba0 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 fferent.** file
e1bb0 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 6f 6e descriptor. Con
e1bc0 73 69 64 65 72 20 74 68 69 73 20 74 65 73 74 20 sider this test
e1bd0 63 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 case:.**.**
e1be0 20 20 69 6e 74 20 66 64 31 20 3d 20 6f 70 65 6e int fd1 = open
e1bf0 28 22 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f 52 44 ("./file1", O_RD
e1c00 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 WR|O_CREAT, 0644
e1c10 29 3b 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 );.** int
e1c20 66 64 32 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 fd2 = open("./fi
e1c30 6c 65 32 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 le2", O_RDWR|O_C
e1c40 52 45 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 0a REAT, 0644);.**.
e1c50 2a 2a 20 53 75 70 70 6f 73 65 20 2e 2f 66 69 6c ** Suppose ./fil
e1c60 65 31 20 61 6e 64 20 2e 2f 66 69 6c 65 32 20 61 e1 and ./file2 a
e1c70 72 65 20 72 65 61 6c 6c 79 20 74 68 65 20 73 61 re really the sa
e1c80 6d 65 20 66 69 6c 65 20 28 62 65 63 61 75 73 65 me file (because
e1c90 0a 2a 2a 20 6f 6e 65 20 69 73 20 61 20 68 61 72 .** one is a har
e1ca0 64 20 6f 72 20 73 79 6d 62 6f 6c 69 63 20 6c 69 d or symbolic li
e1cb0 6e 6b 20 74 6f 20 74 68 65 20 6f 74 68 65 72 29 nk to the other)
e1cc0 20 74 68 65 6e 20 69 66 20 79 6f 75 20 73 65 74 then if you set
e1cd0 0a 2a 2a 20 61 6e 20 65 78 63 6c 75 73 69 76 65 .** an exclusive
e1ce0 20 6c 6f 63 6b 20 6f 6e 20 66 64 31 2c 20 74 68 lock on fd1, th
e1cf0 65 6e 20 74 72 79 20 74 6f 20 67 65 74 20 61 6e en try to get an
e1d00 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a exclusive lock.
e1d10 2a 2a 20 6f 6e 20 66 64 32 2c 20 69 74 20 77 6f ** on fd2, it wo
e1d20 72 6b 73 2e 20 20 49 20 77 6f 75 6c 64 20 68 61 rks. I would ha
e1d30 76 65 20 65 78 70 65 63 74 65 64 20 74 68 65 20 ve expected the
e1d40 73 65 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f 0a 2a second lock to.*
e1d50 2a 20 66 61 69 6c 20 73 69 6e 63 65 20 74 68 65 * fail since the
e1d60 72 65 20 77 61 73 20 61 6c 72 65 61 64 79 20 61 re was already a
e1d70 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c lock on the fil
e1d80 65 20 64 75 65 20 74 6f 20 66 64 31 2e 0a 2a 2a e due to fd1..**
e1d90 20 42 75 74 20 6e 6f 74 20 73 6f 2e 20 20 53 69 But not so. Si
e1da0 6e 63 65 20 62 6f 74 68 20 6c 6f 63 6b 73 20 63 nce both locks c
e1db0 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 73 61 6d ame from the sam
e1dc0 65 20 70 72 6f 63 65 73 73 2c 20 74 68 65 0a 2a e process, the.*
e1dd0 2a 20 73 65 63 6f 6e 64 20 6f 76 65 72 72 69 64 * second overrid
e1de0 65 73 20 74 68 65 20 66 69 72 73 74 2c 20 65 76 es the first, ev
e1df0 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79 20 77 en though they w
e1e00 65 72 65 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 ere on different
e1e10 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 .** file descrip
e1e20 74 6f 72 73 20 6f 70 65 6e 65 64 20 6f 6e 20 64 tors opened on d
e1e30 69 66 66 65 72 65 6e 74 20 66 69 6c 65 20 6e 61 ifferent file na
e1e40 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 mes..**.** This
e1e50 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 63 61 means that we ca
e1e60 6e 6e 6f 74 20 75 73 65 20 50 4f 53 49 58 20 6c nnot use POSIX l
e1e70 6f 63 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f 6e ocks to synchron
e1e80 69 7a 65 20 66 69 6c 65 20 61 63 63 65 73 73 0a ize file access.
e1e90 2a 2a 20 61 6d 6f 6e 67 20 63 6f 6d 70 65 74 69 ** among competi
e1ea0 6e 67 20 74 68 72 65 61 64 73 20 6f 66 20 74 68 ng threads of th
e1eb0 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 e same process.
e1ec0 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69 6c POSIX locks wil
e1ed0 6c 20 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20 74 l work fine.** t
e1ee0 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 61 63 o synchronize ac
e1ef0 63 65 73 73 20 66 6f 72 20 74 68 72 65 61 64 73 cess for threads
e1f00 20 69 6e 20 73 65 70 61 72 61 74 65 20 70 72 6f in separate pro
e1f10 63 65 73 73 65 73 2c 20 62 75 74 20 6e 6f 74 0a cesses, but not.
e1f20 2a 2a 20 74 68 72 65 61 64 73 20 77 69 74 68 69 ** threads withi
e1f30 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 n the same proce
e1f40 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 ss..**.** To wor
e1f50 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70 72 6f k around the pro
e1f60 62 6c 65 6d 2c 20 53 51 4c 69 74 65 20 68 61 73 blem, SQLite has
e1f70 20 74 6f 20 6d 61 6e 61 67 65 20 66 69 6c 65 20 to manage file
e1f80 6c 6f 63 6b 73 20 69 6e 74 65 72 6e 61 6c 6c 79 locks internally
e1f90 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 20 .** on its own.
e1fa0 20 57 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 Whenever a new
e1fb0 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e database is open
e1fc0 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 ed, we have to f
e1fd0 69 6e 64 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 ind the.** speci
e1fe0 66 69 63 20 69 6e 6f 64 65 20 6f 66 20 74 68 65 fic inode of the
e1ff0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 database file (
e2000 74 68 65 20 69 6e 6f 64 65 20 69 73 20 64 65 74 the inode is det
e2010 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 0a 2a ermined by the.*
e2020 2a 20 73 74 5f 64 65 76 20 61 6e 64 20 73 74 5f * st_dev and st_
e2030 69 6e 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 ino fields of th
e2040 65 20 73 74 61 74 20 73 74 72 75 63 74 75 72 65 e stat structure
e2050 20 74 68 61 74 20 66 73 74 61 74 28 29 20 66 69 that fstat() fi
e2060 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 20 63 lls in).** and c
e2070 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 20 61 heck for locks a
e2080 6c 72 65 61 64 79 20 65 78 69 73 74 69 6e 67 20 lready existing
e2090 6f 6e 20 74 68 61 74 20 69 6e 6f 64 65 2e 20 20 on that inode.
e20a0 57 68 65 6e 20 6c 6f 63 6b 73 20 61 72 65 0a 2a When locks are.*
e20b0 2a 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 6d * created or rem
e20c0 6f 76 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f oved, we have to
e20d0 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 20 6f 77 6e look at our own
e20e0 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 internal record
e20f0 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73 of the.** locks
e2100 20 74 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 68 to see if anoth
e2110 65 72 20 74 68 72 65 61 64 20 68 61 73 20 70 72 er thread has pr
e2120 65 76 69 6f 75 73 6c 79 20 73 65 74 20 61 20 6c eviously set a l
e2130 6f 63 6b 20 6f 6e 20 74 68 61 74 20 73 61 6d 65 ock on that same
e2140 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a .** inode..**.**
e2150 20 28 41 73 69 64 65 3a 20 54 68 65 20 75 73 65 (Aside: The use
e2160 20 6f 66 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 of inode number
e2170 73 20 61 73 20 75 6e 69 71 75 65 20 49 44 73 20 s as unique IDs
e2180 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e does not work on
e2190 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 46 6f 72 VxWorks..** For
e21a0 20 56 78 57 6f 72 6b 73 2c 20 77 65 20 68 61 76 VxWorks, we hav
e21b0 65 20 74 6f 20 75 73 65 20 74 68 65 20 61 6c 74 e to use the alt
e21c0 65 72 6e 61 74 69 76 65 20 75 6e 69 71 75 65 20 ernative unique
e21d0 49 44 20 73 79 73 74 65 6d 20 62 61 73 65 64 20 ID system based
e21e0 6f 6e 0a 2a 2a 20 63 61 6e 6f 6e 69 63 61 6c 20 on.** canonical
e21f0 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6d 70 filename and imp
e2200 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 68 65 20 lemented in the
e2210 70 72 65 76 69 6f 75 73 20 64 69 76 69 73 69 6f previous divisio
e2220 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 n.).**.** The sq
e2230 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 lite3_file struc
e2240 74 75 72 65 20 66 6f 72 20 50 4f 53 49 58 20 69 ture for POSIX i
e2250 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75 73 74 s no longer just
e2260 20 61 6e 20 69 6e 74 65 67 65 72 20 66 69 6c 65 an integer file
e2270 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 2e 20 .** descriptor.
e2280 20 49 74 20 69 73 20 6e 6f 77 20 61 20 73 74 72 It is now a str
e2290 75 63 74 75 72 65 20 74 68 61 74 20 68 6f 6c 64 ucture that hold
e22a0 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 66 69 s the integer fi
e22b0 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 le.** descriptor
e22c0 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 74 and a pointer t
e22d0 6f 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68 o a structure th
e22e0 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 at describes the
e22f0 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 6c 6f 63 internal.** loc
e2300 6b 73 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 ks on the corres
e2310 70 6f 6e 64 69 6e 67 20 69 6e 6f 64 65 2e 20 20 ponding inode.
e2320 54 68 65 72 65 20 69 73 20 6f 6e 65 20 6c 6f 63 There is one loc
e2330 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a king structure.*
e2340 2a 20 70 65 72 20 69 6e 6f 64 65 2c 20 73 6f 20 * per inode, so
e2350 69 66 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 if the same inod
e2360 65 20 69 73 20 6f 70 65 6e 65 64 20 74 77 69 63 e is opened twic
e2370 65 2c 20 62 6f 74 68 20 75 6e 69 78 46 69 6c 65 e, both unixFile
e2380 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 70 structures.** p
e2390 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 61 6d 65 oint to the same
e23a0 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 locking structu
e23b0 72 65 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 re. The locking
e23c0 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 structure keeps
e23d0 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 65 20 .** a reference
e23e0 63 6f 75 6e 74 20 28 73 6f 20 77 65 20 77 69 6c count (so we wil
e23f0 6c 20 6b 6e 6f 77 20 77 68 65 6e 20 74 6f 20 64 l know when to d
e2400 65 6c 65 74 65 20 69 74 29 20 61 6e 64 20 61 20 elete it) and a
e2410 22 63 6e 74 22 0a 2a 2a 20 66 69 65 6c 64 20 74 "cnt".** field t
e2420 68 61 74 20 74 65 6c 6c 73 20 75 73 20 69 74 73 hat tells us its
e2430 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 internal lock s
e2440 74 61 74 75 73 2e 20 20 63 6e 74 3d 3d 30 20 6d tatus. cnt==0 m
e2450 65 61 6e 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65 eans the.** file
e2460 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e 20 20 63 is unlocked. c
e2470 6e 74 3d 3d 2d 31 20 6d 65 61 6e 73 20 74 68 65 nt==-1 means the
e2480 20 66 69 6c 65 20 68 61 73 20 61 6e 20 65 78 63 file has an exc
e2490 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 lusive lock..**
e24a0 63 6e 74 3e 30 20 6d 65 61 6e 73 20 74 68 65 72 cnt>0 means ther
e24b0 65 20 61 72 65 20 63 6e 74 20 73 68 61 72 65 64 e are cnt shared
e24c0 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 66 69 locks on the fi
e24d0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 61 74 le..**.** Any at
e24e0 74 65 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 6f 72 tempt to lock or
e24f0 20 75 6e 6c 6f 63 6b 20 61 20 66 69 6c 65 20 66 unlock a file f
e2500 69 72 73 74 20 63 68 65 63 6b 73 20 74 68 65 20 irst checks the
e2510 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 75 63 locking.** struc
e2520 74 75 72 65 2e 20 20 54 68 65 20 66 63 6e 74 6c ture. The fcntl
e2530 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 69 () system call i
e2540 73 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64 20 74 s only invoked t
e2550 6f 20 73 65 74 20 61 20 0a 2a 2a 20 50 4f 53 49 o set a .** POSI
e2560 58 20 6c 6f 63 6b 20 69 66 20 74 68 65 20 69 6e X lock if the in
e2570 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 72 75 ternal lock stru
e2580 63 74 75 72 65 20 74 72 61 6e 73 69 74 69 6f 6e cture transition
e2590 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 20 6c s between.** a l
e25a0 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20 75 6e 6c ocked and an unl
e25b0 6f 63 6b 65 64 20 73 74 61 74 65 2e 0a 2a 2a 0a ocked state..**.
e25c0 2a 2a 20 42 75 74 20 77 61 69 74 3a 20 20 74 68 ** But wait: th
e25d0 65 72 65 20 61 72 65 20 79 65 74 20 6d 6f 72 65 ere are yet more
e25e0 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 50 problems with P
e25f0 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f OSIX advisory lo
e2600 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f cks..**.** If yo
e2610 75 20 63 6c 6f 73 65 20 61 20 66 69 6c 65 20 64 u close a file d
e2620 65 73 63 72 69 70 74 6f 72 20 74 68 61 74 20 70 escriptor that p
e2630 6f 69 6e 74 73 20 74 6f 20 61 20 66 69 6c 65 20 oints to a file
e2640 74 68 61 74 20 68 61 73 20 6c 6f 63 6b 73 2c 0a that has locks,.
e2650 2a 2a 20 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20 ** all locks on
e2660 74 68 61 74 20 66 69 6c 65 20 74 68 61 74 20 61 that file that a
e2670 72 65 20 6f 77 6e 65 64 20 62 79 20 74 68 65 20 re owned by the
e2680 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 current process
e2690 61 72 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 2e are.** released.
e26a0 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 To work around
e26b0 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 2c 20 65 this problem, e
e26c0 61 63 68 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 ach unixInodeInf
e26d0 6f 20 6f 62 6a 65 63 74 0a 2a 2a 20 6d 61 69 6e o object.** main
e26e0 74 61 69 6e 73 20 61 20 63 6f 75 6e 74 20 6f 66 tains a count of
e26f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
e2700 65 6e 64 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20 ending locks on
e2710 74 68 61 20 69 6e 6f 64 65 2e 0a 2a 2a 20 57 68 tha inode..** Wh
e2720 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 en an attempt is
e2730 20 6d 61 64 65 20 74 6f 20 63 6c 6f 73 65 20 61 made to close a
e2740 6e 20 75 6e 69 78 46 69 6c 65 2c 20 69 66 20 74 n unixFile, if t
e2750 68 65 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68 65 here are.** othe
e2760 72 20 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e 20 r unixFile open
e2770 6f 6e 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 on the same inod
e2780 65 20 74 68 61 74 20 61 72 65 20 68 6f 6c 64 69 e that are holdi
e2790 6e 67 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 61 ng locks, the ca
e27a0 6c 6c 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28 29 ll.** to close()
e27b0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
e27c0 70 74 6f 72 20 69 73 20 64 65 66 65 72 72 65 64 ptor is deferred
e27d0 20 75 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74 68 until all of th
e27e0 65 20 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a 2a e locks clear..*
e27f0 2a 20 54 68 65 20 75 6e 69 78 49 6e 6f 64 65 49 * The unixInodeI
e2800 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 6b 65 nfo structure ke
e2810 65 70 73 20 61 20 6c 69 73 74 20 6f 66 20 66 69 eps a list of fi
e2820 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 74 le descriptors t
e2830 68 61 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 hat need to.** b
e2840 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 61 e closed and tha
e2850 74 20 6c 69 73 74 20 69 73 20 77 61 6c 6b 65 64 t list is walked
e2860 20 28 61 6e 64 20 63 6c 65 61 72 65 64 29 20 77 (and cleared) w
e2870 68 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 hen the last loc
e2880 6b 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a k.** clears..**.
e2890 2a 2a 20 59 65 74 20 61 6e 6f 74 68 65 72 20 70 ** Yet another p
e28a0 72 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 68 roblem: LinuxTh
e28b0 72 65 61 64 73 20 64 6f 20 6e 6f 74 20 70 6c 61 reads do not pla
e28c0 79 20 77 65 6c 6c 20 77 69 74 68 20 70 6f 73 69 y well with posi
e28d0 78 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d x locks..**.** M
e28e0 61 6e 79 20 6f 6c 64 65 72 20 76 65 72 73 69 6f any older versio
e28f0 6e 73 20 6f 66 20 6c 69 6e 75 78 20 75 73 65 20 ns of linux use
e2900 74 68 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73 the LinuxThreads
e2910 20 6c 69 62 72 61 72 79 20 77 68 69 63 68 20 69 library which i
e2920 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69 78 20 63 s.** not posix c
e2930 6f 6d 70 6c 69 61 6e 74 2e 20 20 55 6e 64 65 72 ompliant. Under
e2940 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c 20 61 LinuxThreads, a
e2950 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 62 79 lock created by
e2960 20 74 68 72 65 61 64 0a 2a 2a 20 41 20 63 61 6e thread.** A can
e2970 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20 not be modified
e2980 6f 72 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 or overridden by
e2990 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 a different thr
e29a0 65 61 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 ead B..** Only t
e29b0 68 72 65 61 64 20 41 20 63 61 6e 20 6d 6f 64 69 hread A can modi
e29c0 66 79 20 74 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f fy the lock. Lo
e29d0 63 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 20 69 cking behavior i
e29e0 73 20 63 6f 72 72 65 63 74 0a 2a 2a 20 69 66 20 s correct.** if
e29f0 74 68 65 20 61 70 70 6c 69 61 74 69 6f 6e 20 75 the appliation u
e2a00 73 65 73 20 74 68 65 20 6e 65 77 65 72 20 4e 61 ses the newer Na
e2a10 74 69 76 65 20 50 6f 73 69 78 20 54 68 72 65 61 tive Posix Threa
e2a20 64 20 4c 69 62 72 61 72 79 20 28 4e 50 54 4c 29 d Library (NPTL)
e2a30 0a 2a 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d 20 77 .** on linux - w
e2a40 69 74 68 20 4e 50 54 4c 20 61 20 6c 6f 63 6b 20 ith NPTL a lock
e2a50 63 72 65 61 74 65 64 20 62 79 20 74 68 72 65 61 created by threa
e2a60 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 d A can override
e2a70 20 6c 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74 68 72 locks.** in thr
e2a80 65 61 64 20 42 2e 20 20 42 75 74 20 74 68 65 72 ead B. But ther
e2a90 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b e is no way to k
e2aa0 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 now at compile-t
e2ab0 69 6d 65 20 77 68 69 63 68 0a 2a 2a 20 74 68 72 ime which.** thr
e2ac0 65 61 64 69 6e 67 20 6c 69 62 72 61 72 79 20 69 eading library i
e2ad0 73 20 62 65 69 6e 67 20 75 73 65 64 2e 20 20 53 s being used. S
e2ae0 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 o there is no wa
e2af0 79 20 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20 y to know at.**
e2b00 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 65 compile-time whe
e2b10 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 ther or not thre
e2b20 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 ad A can overrid
e2b30 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 72 65 61 e locks on threa
e2b40 64 20 42 2e 0a 2a 2a 20 4f 6e 65 20 68 61 73 20 d B..** One has
e2b50 74 6f 20 64 6f 20 61 20 72 75 6e 2d 74 69 6d 65 to do a run-time
e2b60 20 63 68 65 63 6b 20 74 6f 20 64 69 73 63 6f 76 check to discov
e2b70 65 72 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 er the behavior
e2b80 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e of the.** curren
e2b90 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a t process..**.**
e2ba0 20 53 51 4c 69 74 65 20 75 73 65 64 20 74 6f 20 SQLite used to
e2bb0 73 75 70 70 6f 72 74 20 4c 69 6e 75 78 54 68 72 support LinuxThr
e2bc0 65 61 64 73 2e 20 20 42 75 74 20 73 75 70 70 6f eads. But suppo
e2bd0 72 74 20 66 6f 72 20 4c 69 6e 75 78 54 68 72 65 rt for LinuxThre
e2be0 61 64 73 0a 2a 2a 20 77 61 73 20 64 72 6f 70 70 ads.** was dropp
e2bf0 65 64 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 ed beginning wit
e2c00 68 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e 30 2e h version 3.7.0.
e2c10 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 73 74 SQLite will st
e2c20 69 6c 6c 20 77 6f 72 6b 20 77 69 74 68 0a 2a 2a ill work with.**
e2c30 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 70 72 LinuxThreads pr
e2c40 6f 76 69 64 65 64 20 74 68 61 74 20 28 31 29 20 ovided that (1)
e2c50 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 6f 72 65 there is no more
e2c60 20 74 68 61 6e 20 6f 6e 65 20 63 6f 6e 6e 65 63 than one connec
e2c70 74 69 6f 6e 20 0a 2a 2a 20 70 65 72 20 64 61 74 tion .** per dat
e2c80 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 74 68 abase file in th
e2c90 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 61 e same process a
e2ca0 6e 64 20 28 32 29 20 64 61 74 61 62 61 73 65 20 nd (2) database
e2cb0 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a 2a 20 64 connections.** d
e2cc0 6f 20 6e 6f 74 20 6d 6f 76 65 20 61 63 72 6f 73 o not move acros
e2cd0 73 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 0a 2f s threads..*/../
e2ce0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
e2cf0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
e2d00 67 20 73 74 72 75 63 74 75 72 65 20 73 65 72 76 g structure serv
e2d10 65 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 es as the key us
e2d20 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 ed.** to locate
e2d30 61 20 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69 a particular uni
e2d40 78 49 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a 65 63 xInodeInfo objec
e2d50 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 t..*/.struct uni
e2d60 78 46 69 6c 65 49 64 20 7b 0a 20 20 64 65 76 5f xFileId {. dev_
e2d70 74 20 64 65 76 3b 20 20 20 20 20 20 20 20 20 20 t dev;
e2d80 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 /* Devic
e2d90 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 69 66 20 e number */.#if
e2da0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 OS_VXWORKS. str
e2db0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 uct vxworksFileI
e2dc0 64 20 2a 70 49 64 3b 20 20 2f 2a 20 55 6e 69 71 d *pId; /* Uniq
e2dd0 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72 20 76 ue file ID for v
e2de0 78 77 6f 72 6b 73 2e 20 2a 2f 0a 23 65 6c 73 65 xworks. */.#else
e2df0 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 . ino_t ino;
e2e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e2e10 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a * Inode number *
e2e20 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a /.#endif.};../*.
e2e30 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
e2e40 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
e2e50 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c structure is all
e2e60 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 ocated for each
e2e70 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 open.** inode.
e2e80 4f 72 2c 20 6f 6e 20 4c 69 6e 75 78 54 68 72 65 Or, on LinuxThre
e2e90 61 64 73 2c 20 74 68 65 72 65 20 69 73 20 6f 6e ads, there is on
e2ea0 65 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 e of these struc
e2eb0 74 75 72 65 73 20 66 6f 72 0a 2a 2a 20 65 61 63 tures for.** eac
e2ec0 68 20 69 6e 6f 64 65 20 6f 70 65 6e 65 64 20 62 h inode opened b
e2ed0 79 20 65 61 63 68 20 74 68 72 65 61 64 2e 0a 2a y each thread..*
e2ee0 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e *.** A single in
e2ef0 6f 64 65 20 63 61 6e 20 68 61 76 65 20 6d 75 6c ode can have mul
e2f00 74 69 70 6c 65 20 66 69 6c 65 20 64 65 73 63 72 tiple file descr
e2f10 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 20 iptors, so each
e2f20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 unixFile.** stru
e2f30 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 cture contains a
e2f40 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 pointer to an i
e2f50 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
e2f60 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 69 73 0a object and this.
e2f70 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 20 ** object keeps
e2f80 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e a count of the n
e2f90 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 69 6c umber of unixFil
e2fa0 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74 e pointing to it
e2fb0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 ..*/.struct unix
e2fc0 49 6e 6f 64 65 49 6e 66 6f 20 7b 0a 20 20 73 74 InodeInfo {. st
e2fd0 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 ruct unixFileId
e2fe0 66 69 6c 65 49 64 3b 20 20 20 20 20 20 20 2f 2a fileId; /*
e2ff0 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 The lookup key
e3000 2a 2f 0a 20 20 69 6e 74 20 6e 53 68 61 72 65 64 */. int nShared
e3010 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
e3020 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
e3030 66 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 68 f SHARED locks h
e3040 65 6c 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 eld */. unsigne
e3050 64 20 63 68 61 72 20 65 46 69 6c 65 4c 6f 63 6b d char eFileLock
e3060 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 ; /* One
e3070 6f 66 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 of SHARED_LOCK,
e3080 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 65 74 RESERVED_LOCK et
e3090 63 2e 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 c. */. unsigned
e30a0 20 63 68 61 72 20 62 50 72 6f 63 65 73 73 4c 6f char bProcessLo
e30b0 63 6b 3b 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 ck; /* An ex
e30c0 63 6c 75 73 69 76 65 20 70 72 6f 63 65 73 73 20 clusive process
e30d0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 2a 2f 0a lock is held */.
e30e0 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 int nRef;
e30f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3100 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
e3110 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 ointers to this
e3120 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 75 structure */. u
e3130 6e 69 78 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d nixShmNode *pShm
e3140 4e 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f Node; /
e3150 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 * Shared memory
e3160 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
e3170 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 this inode */.
e3180 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20 20 20 20 20 int nLock;
e3190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e31a0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75 74 /* Number of out
e31b0 73 74 61 6e 64 69 6e 67 20 66 69 6c 65 20 6c 6f standing file lo
e31c0 63 6b 73 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 cks */. UnixUnu
e31d0 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 sedFd *pUnused;
e31e0 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 75 73 /* Unus
e31f0 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ed file descript
e3200 6f 72 73 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a ors to close */.
e3210 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 unixInodeInfo
e3220 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 *pNext;
e3230 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c /* List of all
e3240 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 6f unixInodeInfo o
e3250 62 6a 65 63 74 73 20 2a 2f 0a 20 20 75 6e 69 78 bjects */. unix
e3260 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 50 72 65 76 InodeInfo *pPrev
e3270 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 ; /*
e3280 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c 69 .... doubly li
e3290 6e 6b 65 64 20 2a 2f 0a 23 69 66 20 53 51 4c 49 nked */.#if SQLI
e32a0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
e32b0 47 5f 53 54 59 4c 45 0a 20 20 75 6e 73 69 67 6e G_STYLE. unsign
e32c0 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61 ed long long sha
e32d0 72 65 64 42 79 74 65 3b 20 20 2f 2a 20 66 6f 72 redByte; /* for
e32e0 20 41 46 50 20 73 69 6d 75 6c 61 74 65 64 20 73 AFP simulated s
e32f0 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 23 65 hared lock */.#e
e3300 6e 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f ndif.#if OS_VXWO
e3310 52 4b 53 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 RKS. sem_t *pSe
e3320 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
e3330 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 50 /* Named P
e3340 4f 53 49 58 20 73 65 6d 61 70 68 6f 72 65 20 2a OSIX semaphore *
e3350 2f 0a 20 20 63 68 61 72 20 61 53 65 6d 4e 61 6d /. char aSemNam
e3360 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 32 e[MAX_PATHNAME+2
e3370 5d 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 ]; /* Name of t
e3380 68 61 74 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f hat semaphore */
e3390 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a .#endif.};../*.*
e33a0 2a 20 41 20 6c 69 73 74 73 20 6f 66 20 61 6c 6c * A lists of all
e33b0 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 6f unixInodeInfo o
e33c0 62 6a 65 63 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 bjects..*/.stati
e33d0 63 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 c unixInodeInfo
e33e0 2a 69 6e 6f 64 65 4c 69 73 74 20 3d 20 30 3b 0a *inodeList = 0;.
e33f0 0a 2f 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ./*.**.** This f
e3400 75 6e 63 74 69 6f 6e 20 2d 20 75 6e 69 78 4c 6f unction - unixLo
e3410 67 45 72 72 6f 72 5f 78 28 29 2c 20 69 73 20 6f gError_x(), is o
e3420 6e 6c 79 20 65 76 65 72 20 63 61 6c 6c 65 64 20 nly ever called
e3430 76 69 61 20 74 68 65 20 6d 61 63 72 6f 0a 2a 2a via the macro.**
e3440 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 29 2e unixLogError().
e3450 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6e 76 .**.** It is inv
e3460 6f 6b 65 64 20 61 66 74 65 72 20 61 6e 20 65 72 oked after an er
e3470 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 61 6e ror occurs in an
e3480 20 4f 53 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 OS function and
e3490 20 65 72 72 6e 6f 20 68 61 73 20 62 65 65 6e 0a errno has been.
e34a0 2a 2a 20 73 65 74 2e 20 49 74 20 6c 6f 67 73 20 ** set. It logs
e34b0 61 20 6d 65 73 73 61 67 65 20 75 73 69 6e 67 20 a message using
e34c0 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 20 63 6f sqlite3_log() co
e34d0 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 75 72 ntaining the cur
e34e0 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 0a 2a 2a rent value of.**
e34f0 20 65 72 72 6e 6f 20 61 6e 64 2c 20 69 66 20 70 errno and, if p
e3500 6f 73 73 69 62 6c 65 2c 20 74 68 65 20 68 75 6d ossible, the hum
e3510 61 6e 2d 72 65 61 64 61 62 6c 65 20 65 71 75 69 an-readable equi
e3520 76 61 6c 65 6e 74 20 66 72 6f 6d 20 73 74 72 65 valent from stre
e3530 72 72 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 74 72 rror() or.** str
e3540 65 72 72 6f 72 5f 72 28 29 2e 0a 2a 2a 0a 2a 2a error_r()..**.**
e3550 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
e3560 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 74 68 ent passed to th
e3570 65 20 6d 61 63 72 6f 20 73 68 6f 75 6c 64 20 62 e macro should b
e3580 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 e the error code
e3590 20 74 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 62 65 that.** will be
e35a0 20 72 65 74 75 72 6e 65 64 20 74 6f 20 53 51 4c returned to SQL
e35b0 69 74 65 20 28 65 2e 67 2e 20 53 51 4c 49 54 45 ite (e.g. SQLITE
e35c0 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 2c 20 53 _IOERR_DELETE, S
e35d0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 29 2e QLITE_CANTOPEN).
e35e0 20 0a 2a 2a 20 54 68 65 20 74 77 6f 20 73 75 62 .** The two sub
e35f0 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 sequent argument
e3600 73 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20 s should be the
e3610 6e 61 6d 65 20 6f 66 20 74 68 65 20 4f 53 20 66 name of the OS f
e3620 75 6e 63 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 unction that.**
e3630 66 61 69 6c 65 64 20 28 65 2e 67 2e 20 22 75 6e failed (e.g. "un
e3640 6c 69 6e 6b 22 2c 20 22 6f 70 65 6e 22 29 20 61 link", "open") a
e3650 6e 64 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 nd the associate
e3660 64 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 70 61 d file-system pa
e3670 74 68 2c 0a 2a 2a 20 69 66 20 61 6e 79 2e 0a 2a th,.** if any..*
e3680 2f 0a 23 64 65 66 69 6e 65 20 75 6e 69 78 4c 6f /.#define unixLo
e3690 67 45 72 72 6f 72 28 61 2c 62 2c 63 29 20 20 20 gError(a,b,c)
e36a0 20 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 41 74 unixLogErrorAt
e36b0 4c 69 6e 65 28 61 2c 62 2c 63 2c 5f 5f 4c 49 4e Line(a,b,c,__LIN
e36c0 45 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e 74 20 E__).static int
e36d0 75 6e 69 78 4c 6f 67 45 72 72 6f 72 41 74 4c 69 unixLogErrorAtLi
e36e0 6e 65 28 0a 20 20 69 6e 74 20 65 72 72 63 6f 64 ne(. int errcod
e36f0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
e3700 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 /* SQLite
e3710 65 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a 20 20 error code */.
e3720 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e const char *zFun
e3730 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c,
e3740 2f 2a 20 4e 61 6d 65 20 6f 66 20 4f 53 20 66 75 /* Name of OS fu
e3750 6e 63 74 69 6f 6e 20 74 68 61 74 20 66 61 69 6c nction that fail
e3760 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ed */. const ch
e3770 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 ar *zPath,
e3780 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 /* File
e3790 70 61 74 68 20 61 73 73 6f 63 69 61 74 65 64 20 path associated
e37a0 77 69 74 68 20 65 72 72 6f 72 20 2a 2f 0a 20 20 with error */.
e37b0 69 6e 74 20 69 4c 69 6e 65 20 20 20 20 20 20 20 int iLine
e37c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e37d0 2f 2a 20 53 6f 75 72 63 65 20 6c 69 6e 65 20 6e /* Source line n
e37e0 75 6d 62 65 72 20 77 68 65 72 65 20 65 72 72 6f umber where erro
e37f0 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 29 7b r occurred */.){
e3800 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 20 20 . char *zErr;
e3810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3820 20 20 20 2f 2a 20 4d 65 73 73 61 67 65 20 66 72 /* Message fr
e3830 6f 6d 20 73 74 72 65 72 72 6f 72 28 29 20 6f 72 om strerror() or
e3840 20 65 71 75 69 76 61 6c 65 6e 74 20 2a 2f 0a 20 equivalent */.
e3850 20 69 6e 74 20 69 45 72 72 6e 6f 20 3d 20 65 72 int iErrno = er
e3860 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 rno;
e3870 20 2f 2a 20 53 61 76 65 64 20 73 79 73 63 61 6c /* Saved syscal
e3880 6c 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 20 2a l error number *
e3890 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 /.. /* If this
e38a0 69 73 20 6e 6f 74 20 61 20 74 68 72 65 61 64 73 is not a threads
e38b0 61 66 65 20 62 75 69 6c 64 20 28 53 51 4c 49 54 afe build (SQLIT
e38c0 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 29 E_THREADSAFE==0)
e38d0 2c 20 74 68 65 6e 20 75 73 65 0a 20 20 2a 2a 20 , then use. **
e38e0 74 68 65 20 73 74 72 65 72 72 6f 72 28 29 20 66 the strerror() f
e38f0 75 6e 63 74 69 6f 6e 20 74 6f 20 6f 62 74 61 69 unction to obtai
e3900 6e 20 74 68 65 20 68 75 6d 61 6e 2d 72 65 61 64 n the human-read
e3910 61 62 6c 65 20 65 72 72 6f 72 20 6d 65 73 73 61 able error messa
e3920 67 65 0a 20 20 2a 2a 20 65 71 75 69 76 61 6c 65 ge. ** equivale
e3930 6e 74 20 74 6f 20 65 72 72 6e 6f 2e 20 4f 74 68 nt to errno. Oth
e3940 65 72 77 69 73 65 2c 20 75 73 65 20 73 74 72 65 erwise, use stre
e3950 72 72 6f 72 5f 72 28 29 2e 0a 20 20 2a 2f 20 0a rror_r().. */ .
e3960 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
e3970 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 DSAFE && defined
e3980 28 48 41 56 45 5f 53 54 52 45 52 52 4f 52 5f 52 (HAVE_STRERROR_R
e3990 29 0a 20 20 63 68 61 72 20 61 45 72 72 5b 38 30 ). char aErr[80
e39a0 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 45 72 72 ];. memset(aErr
e39b0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 45 72 72 , 0, sizeof(aErr
e39c0 29 29 3b 0a 20 20 7a 45 72 72 20 3d 20 61 45 72 ));. zErr = aEr
e39d0 72 3b 0a 0a 20 20 2f 2a 20 49 66 20 53 54 52 45 r;.. /* If STRE
e39e0 52 52 4f 52 5f 52 5f 43 48 41 52 5f 50 20 28 73 RROR_R_CHAR_P (s
e39f0 65 74 20 62 79 20 61 75 74 6f 63 6f 6e 66 20 73 et by autoconf s
e3a00 63 72 69 70 74 73 29 20 6f 72 20 5f 5f 55 53 45 cripts) or __USE
e3a10 5f 47 4e 55 20 69 73 20 64 65 66 69 6e 65 64 2c _GNU is defined,
e3a20 0a 20 20 2a 2a 20 61 73 73 75 6d 65 20 74 68 61 . ** assume tha
e3a30 74 20 74 68 65 20 73 79 73 74 65 6d 20 70 72 6f t the system pro
e3a40 76 69 64 65 73 20 74 68 65 20 47 4e 55 20 76 65 vides the GNU ve
e3a50 72 73 69 6f 6e 20 6f 66 20 73 74 72 65 72 72 6f rsion of strerro
e3a60 72 5f 72 28 29 20 74 68 61 74 0a 20 20 2a 2a 20 r_r() that. **
e3a70 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
e3a80 72 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f r to a buffer co
e3a90 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65 72 72 ntaining the err
e3aa0 6f 72 20 6d 65 73 73 61 67 65 2e 20 54 68 61 74 or message. That
e3ab0 20 70 6f 69 6e 74 65 72 20 0a 20 20 2a 2a 20 6d pointer . ** m
e3ac0 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 45 72 72 ay point to aErr
e3ad0 5b 5d 2c 20 6f 72 20 69 74 20 6d 61 79 20 70 6f [], or it may po
e3ae0 69 6e 74 20 74 6f 20 73 6f 6d 65 20 73 74 61 74 int to some stat
e3af0 69 63 20 73 74 6f 72 61 67 65 20 73 6f 6d 65 77 ic storage somew
e3b00 68 65 72 65 2e 20 0a 20 20 2a 2a 20 4f 74 68 65 here. . ** Othe
e3b10 72 77 69 73 65 2c 20 61 73 73 75 6d 65 20 74 68 rwise, assume th
e3b20 61 74 20 74 68 65 20 73 79 73 74 65 6d 20 70 72 at the system pr
e3b30 6f 76 69 64 65 73 20 74 68 65 20 50 4f 53 49 58 ovides the POSIX
e3b40 20 76 65 72 73 69 6f 6e 20 6f 66 20 0a 20 20 2a version of . *
e3b50 2a 20 73 74 72 65 72 72 6f 72 5f 72 28 29 2c 20 * strerror_r(),
e3b60 77 68 69 63 68 20 61 6c 77 61 79 73 20 77 72 69 which always wri
e3b70 74 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 tes an error mes
e3b80 73 61 67 65 20 69 6e 74 6f 20 61 45 72 72 5b 5d sage into aErr[]
e3b90 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 .. **. ** If t
e3ba0 68 65 20 63 6f 64 65 20 69 6e 63 6f 72 72 65 63 he code incorrec
e3bb0 74 6c 79 20 61 73 73 75 6d 65 73 20 74 68 61 74 tly assumes that
e3bc0 20 69 74 20 69 73 20 74 68 65 20 50 4f 53 49 58 it is the POSIX
e3bd0 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 69 73 version that is
e3be0 0a 20 20 2a 2a 20 61 76 61 69 6c 61 62 6c 65 2c . ** available,
e3bf0 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
e3c00 67 65 20 77 69 6c 6c 20 6f 66 74 65 6e 20 62 65 ge will often be
e3c10 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 an empty string
e3c20 2e 20 4e 6f 74 20 61 0a 20 20 2a 2a 20 68 75 67 . Not a. ** hug
e3c30 65 20 70 72 6f 62 6c 65 6d 2e 20 49 6e 63 6f 72 e problem. Incor
e3c40 72 65 63 74 6c 79 20 63 6f 6e 63 6c 75 64 69 6e rectly concludin
e3c50 67 20 74 68 61 74 20 74 68 65 20 47 4e 55 20 76 g that the GNU v
e3c60 65 72 73 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 ersion is availa
e3c70 62 6c 65 20 0a 20 20 2a 2a 20 63 6f 75 6c 64 20 ble . ** could
e3c80 6c 65 61 64 20 74 6f 20 61 20 73 65 67 66 61 75 lead to a segfau
e3c90 6c 74 20 74 68 6f 75 67 68 2e 0a 20 20 2a 2f 0a lt though.. */.
e3ca0 23 69 66 20 64 65 66 69 6e 65 64 28 53 54 52 45 #if defined(STRE
e3cb0 52 52 4f 52 5f 52 5f 43 48 41 52 5f 50 29 20 7c RROR_R_CHAR_P) |
e3cc0 7c 20 64 65 66 69 6e 65 64 28 5f 5f 55 53 45 5f | defined(__USE_
e3cd0 47 4e 55 29 0a 20 20 7a 45 72 72 20 3d 20 0a 23 GNU). zErr = .#
e3ce0 20 65 6e 64 69 66 0a 20 20 73 74 72 65 72 72 6f endif. strerro
e3cf0 72 5f 72 28 69 45 72 72 6e 6f 2c 20 61 45 72 72 r_r(iErrno, aErr
e3d00 2c 20 73 69 7a 65 6f 66 28 61 45 72 72 29 2d 31 , sizeof(aErr)-1
e3d10 29 3b 0a 0a 23 65 6c 69 66 20 53 51 4c 49 54 45 );..#elif SQLITE
e3d20 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 2f 2a _THREADSAFE. /*
e3d30 20 54 68 69 73 20 69 73 20 61 20 74 68 72 65 61 This is a threa
e3d40 64 73 61 66 65 20 62 75 69 6c 64 2c 20 62 75 74 dsafe build, but
e3d50 20 73 74 72 65 72 72 6f 72 5f 72 28 29 20 69 73 strerror_r() is
e3d60 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 not available.
e3d70 2a 2f 0a 20 20 7a 45 72 72 20 3d 20 22 22 3b 0a */. zErr = "";.
e3d80 23 65 6c 73 65 0a 20 20 2f 2a 20 4e 6f 6e 2d 74 #else. /* Non-t
e3d90 68 72 65 61 64 73 61 66 65 20 62 75 69 6c 64 2c hreadsafe build,
e3da0 20 75 73 65 20 73 74 72 65 72 72 6f 72 28 29 2e use strerror().
e3db0 20 2a 2f 0a 20 20 7a 45 72 72 20 3d 20 73 74 72 */. zErr = str
e3dc0 65 72 72 6f 72 28 69 45 72 72 6e 6f 29 3b 0a 23 error(iErrno);.#
e3dd0 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 endif.. assert(
e3de0 20 65 72 72 63 6f 64 65 21 3d 53 51 4c 49 54 45 errcode!=SQLITE
e3df0 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 7a 50 61 _OK );. if( zPa
e3e00 74 68 3d 3d 30 20 29 20 7a 50 61 74 68 20 3d 20 th==0 ) zPath =
e3e10 22 22 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6c 6f "";. sqlite3_lo
e3e20 67 28 65 72 72 63 6f 64 65 2c 0a 20 20 20 20 20 g(errcode,.
e3e30 20 22 6f 73 5f 75 6e 69 78 2e 63 3a 25 64 3a 20 "os_unix.c:%d:
e3e40 28 25 64 29 20 25 73 28 25 73 29 20 2d 20 25 73 (%d) %s(%s) - %s
e3e50 22 2c 0a 20 20 20 20 20 20 69 4c 69 6e 65 2c 20 ",. iLine,
e3e60 69 45 72 72 6e 6f 2c 20 7a 46 75 6e 63 2c 20 7a iErrno, zFunc, z
e3e70 50 61 74 68 2c 20 7a 45 72 72 0a 20 20 29 3b 0a Path, zErr. );.
e3e80 0a 20 20 72 65 74 75 72 6e 20 65 72 72 63 6f 64 . return errcod
e3e90 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 e;.}../*.** Clos
e3ea0 65 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 e a file descrip
e3eb0 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 73 tor..**.** We as
e3ec0 73 75 6d 65 20 74 68 61 74 20 63 6c 6f 73 65 28 sume that close(
e3ed0 29 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 ) almost always
e3ee0 77 6f 72 6b 73 2c 20 73 69 6e 63 65 20 69 74 20 works, since it
e3ef0 69 73 20 6f 6e 6c 79 20 69 6e 20 61 0a 2a 2a 20 is only in a.**
e3f00 76 65 72 79 20 73 69 63 6b 20 61 70 70 6c 69 63 very sick applic
e3f10 61 74 69 6f 6e 20 6f 72 20 6f 6e 20 61 20 76 65 ation or on a ve
e3f20 72 79 20 73 69 63 6b 20 70 6c 61 74 66 6f 72 6d ry sick platform
e3f30 20 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 66 that it might f
e3f40 61 69 6c 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f ail..** If it do
e3f50 65 73 20 66 61 69 6c 2c 20 73 69 6d 70 6c 79 20 es fail, simply
e3f60 6c 65 61 6b 20 74 68 65 20 66 69 6c 65 20 64 65 leak the file de
e3f70 73 63 72 69 70 74 6f 72 2c 20 62 75 74 20 64 6f scriptor, but do
e3f80 20 6c 6f 67 20 74 68 65 0a 2a 2a 20 65 72 72 6f log the.** erro
e3f90 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 r..**.** Note th
e3fa0 61 74 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 at it is not saf
e3fb0 65 20 74 6f 20 72 65 74 72 79 20 63 6c 6f 73 65 e to retry close
e3fc0 28 29 20 61 66 74 65 72 20 45 49 4e 54 52 20 73 () after EINTR s
e3fd0 69 6e 63 65 20 74 68 65 0a 2a 2a 20 66 69 6c 65 ince the.** file
e3fe0 20 64 65 73 63 72 69 70 74 6f 72 20 6d 69 67 68 descriptor migh
e3ff0 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 t have already b
e4000 65 65 6e 20 72 65 75 73 65 64 20 62 79 20 61 6e een reused by an
e4010 6f 74 68 65 72 20 74 68 72 65 61 64 2e 0a 2a 2a other thread..**
e4020 20 53 6f 20 77 65 20 64 6f 6e 27 74 20 65 76 65 So we don't eve
e4030 6e 20 74 72 79 20 74 6f 20 72 65 63 6f 76 65 72 n try to recover
e4040 20 66 72 6f 6d 20 61 6e 20 45 49 4e 54 52 2e 20 from an EINTR.
e4050 20 4a 75 73 74 20 6c 6f 67 20 74 68 65 20 65 72 Just log the er
e4060 72 6f 72 0a 2a 2a 20 61 6e 64 20 6d 6f 76 65 20 ror.** and move
e4070 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f on..*/.static vo
e4080 69 64 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 id robust_close(
e4090 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c unixFile *pFile,
e40a0 20 69 6e 74 20 68 2c 20 69 6e 74 20 6c 69 6e 65 int h, int line
e40b0 6e 6f 29 7b 0a 20 20 69 66 28 20 6f 73 43 6c 6f no){. if( osClo
e40c0 73 65 28 68 29 20 29 7b 0a 20 20 20 20 75 6e 69 se(h) ){. uni
e40d0 78 4c 6f 67 45 72 72 6f 72 41 74 4c 69 6e 65 28 xLogErrorAtLine(
e40e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f SQLITE_IOERR_CLO
e40f0 53 45 2c 20 22 63 6c 6f 73 65 22 2c 0a 20 20 20 SE, "close",.
e4100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4110 20 20 20 20 70 46 69 6c 65 20 3f 20 70 46 69 6c pFile ? pFil
e4120 65 2d 3e 7a 50 61 74 68 20 3a 20 30 2c 20 6c 69 e->zPath : 0, li
e4130 6e 65 6e 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a neno);. }.}../*
e4140 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 66 69 .** Close all fi
e4150 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 61 le descriptors a
e4160 63 63 75 6d 75 61 74 65 64 20 69 6e 20 74 68 65 ccumuated in the
e4170 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 2d 3e unixInodeInfo->
e4180 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2f pUnused list..*/
e4190 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c .static void cl
e41a0 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 75 6e osePendingFds(un
e41b0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a ixFile *pFile){.
e41c0 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 unixInodeInfo
e41d0 2a 70 49 6e 6f 64 65 20 3d 20 70 46 69 6c 65 2d *pInode = pFile-
e41e0 3e 70 49 6e 6f 64 65 3b 0a 20 20 55 6e 69 78 55 >pInode;. UnixU
e41f0 6e 75 73 65 64 46 64 20 2a 70 3b 0a 20 20 55 6e nusedFd *p;. Un
e4200 69 78 55 6e 75 73 65 64 46 64 20 2a 70 4e 65 78 ixUnusedFd *pNex
e4210 74 3b 0a 20 20 66 6f 72 28 70 3d 70 49 6e 6f 64 t;. for(p=pInod
e4220 65 2d 3e 70 55 6e 75 73 65 64 3b 20 70 3b 20 70 e->pUnused; p; p
e4230 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65 =pNext){. pNe
e4240 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 xt = p->pNext;.
e4250 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 robust_close(
e4260 70 46 69 6c 65 2c 20 70 2d 3e 66 64 2c 20 5f 5f pFile, p->fd, __
e4270 4c 49 4e 45 5f 5f 29 3b 0a 20 20 20 20 73 71 6c LINE__);. sql
e4280 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
e4290 7d 0a 20 20 70 49 6e 6f 64 65 2d 3e 70 55 6e 75 }. pInode->pUnu
e42a0 73 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a sed = 0;.}../*.*
e42b0 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 * Release a unix
e42c0 49 6e 6f 64 65 49 6e 66 6f 20 73 74 72 75 63 74 InodeInfo struct
e42d0 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 ure previously a
e42e0 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 llocated by find
e42f0 49 6e 6f 64 65 49 6e 66 6f 28 29 2e 0a 2a 2a 0a InodeInfo()..**.
e4300 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 ** The mutex ent
e4310 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 ered using the u
e4320 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 nixEnterMutex()
e4330 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 function must be
e4340 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 held.** when th
e4350 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
e4360 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 alled..*/.static
e4370 20 76 6f 69 64 20 72 65 6c 65 61 73 65 49 6e 6f void releaseIno
e4380 64 65 49 6e 66 6f 28 75 6e 69 78 46 69 6c 65 20 deInfo(unixFile
e4390 2a 70 46 69 6c 65 29 7b 0a 20 20 75 6e 69 78 49 *pFile){. unixI
e43a0 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f 64 65 nodeInfo *pInode
e43b0 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 = pFile->pInode
e43c0 3b 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69 78 ;. assert( unix
e43d0 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 MutexHeld() );.
e43e0 20 69 66 28 20 41 4c 57 41 59 53 28 70 49 6e 6f if( ALWAYS(pIno
e43f0 64 65 29 20 29 7b 0a 20 20 20 20 70 49 6e 6f 64 de) ){. pInod
e4400 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 e->nRef--;. i
e4410 66 28 20 70 49 6e 6f 64 65 2d 3e 6e 52 65 66 3d f( pInode->nRef=
e4420 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 =0 ){. asse
e4430 72 74 28 20 70 49 6e 6f 64 65 2d 3e 70 53 68 6d rt( pInode->pShm
e4440 4e 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 Node==0 );.
e4450 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 closePendingFds
e4460 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 (pFile);. i
e4470 66 28 20 70 49 6e 6f 64 65 2d 3e 70 50 72 65 76 f( pInode->pPrev
e4480 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
e4490 72 74 28 20 70 49 6e 6f 64 65 2d 3e 70 50 72 65 rt( pInode->pPre
e44a0 76 2d 3e 70 4e 65 78 74 3d 3d 70 49 6e 6f 64 65 v->pNext==pInode
e44b0 20 29 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 6f );. pIno
e44c0 64 65 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 de->pPrev->pNext
e44d0 20 3d 20 70 49 6e 6f 64 65 2d 3e 70 4e 65 78 74 = pInode->pNext
e44e0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
e44f0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 assert( i
e4500 6e 6f 64 65 4c 69 73 74 3d 3d 70 49 6e 6f 64 65 nodeList==pInode
e4510 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 6f 64 );. inod
e4520 65 4c 69 73 74 20 3d 20 70 49 6e 6f 64 65 2d 3e eList = pInode->
e4530 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 pNext;. }.
e4540 20 20 20 20 20 69 66 28 20 70 49 6e 6f 64 65 2d if( pInode-
e4550 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 >pNext ){.
e4560 20 20 61 73 73 65 72 74 28 20 70 49 6e 6f 64 65 assert( pInode
e4570 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d ->pNext->pPrev==
e4580 70 49 6e 6f 64 65 20 29 3b 0a 20 20 20 20 20 20 pInode );.
e4590 20 20 70 49 6e 6f 64 65 2d 3e 70 4e 65 78 74 2d pInode->pNext-
e45a0 3e 70 50 72 65 76 20 3d 20 70 49 6e 6f 64 65 2d >pPrev = pInode-
e45b0 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a >pPrev;. }.
e45c0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
e45d0 65 65 28 70 49 6e 6f 64 65 29 3b 0a 20 20 20 20 ee(pInode);.
e45e0 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 }. }.}../*.** G
e45f0 69 76 65 6e 20 61 20 66 69 6c 65 20 64 65 73 63 iven a file desc
e4600 72 69 70 74 6f 72 2c 20 6c 6f 63 61 74 65 20 74 riptor, locate t
e4610 68 65 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f he unixInodeInfo
e4620 20 6f 62 6a 65 63 74 20 74 68 61 74 0a 2a 2a 20 object that.**
e4630 64 65 73 63 72 69 62 65 73 20 74 68 61 74 20 66 describes that f
e4640 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 ile descriptor.
e4650 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 6f 6e Create a new on
e4660 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 e if necessary.
e4670 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 The.** return v
e4680 61 6c 75 65 20 6d 69 67 68 74 20 62 65 20 75 6e alue might be un
e4690 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 66 20 61 initialized if a
e46a0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a n error occurs..
e46b0 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 **.** The mutex
e46c0 65 6e 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 entered using th
e46d0 65 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 e unixEnterMutex
e46e0 28 29 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 () function must
e46f0 20 62 65 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e be held.** when
e4700 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
e4710 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 s called..**.**
e4720 52 65 74 75 72 6e 20 61 6e 20 61 70 70 72 6f 70 Return an approp
e4730 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 riate error code
e4740 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e4750 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f 28 0a 20 findInodeInfo(.
e4760 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
e4770 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e4780 2f 2a 20 55 6e 69 78 20 66 69 6c 65 20 77 69 74 /* Unix file wit
e4790 68 20 66 69 6c 65 20 64 65 73 63 20 75 73 65 64 h file desc used
e47a0 20 69 6e 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 in the key */.
e47b0 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a unixInodeInfo *
e47c0 2a 70 70 49 6e 6f 64 65 20 20 20 20 20 20 20 20 *ppInode
e47d0 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e /* Return the un
e47e0 69 78 49 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a 65 ixInodeInfo obje
e47f0 63 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 ct here */.){.
e4800 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
e4810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e4820 2a 20 53 79 73 74 65 6d 20 63 61 6c 6c 20 72 65 * System call re
e4830 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 turn code */. i
e4840 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 nt fd;
e4850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e4860 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 The file descri
e4870 70 74 6f 72 20 66 6f 72 20 70 46 69 6c 65 20 2a ptor for pFile *
e4880 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 46 /. struct unixF
e4890 69 6c 65 49 64 20 66 69 6c 65 49 64 3b 20 20 20 ileId fileId;
e48a0 20 20 20 2f 2a 20 4c 6f 6f 6b 75 70 20 6b 65 79 /* Lookup key
e48b0 20 66 6f 72 20 74 68 65 20 75 6e 69 78 49 6e 6f for the unixIno
e48c0 64 65 49 6e 66 6f 20 2a 2f 0a 20 20 73 74 72 75 deInfo */. stru
e48d0 63 74 20 73 74 61 74 20 73 74 61 74 62 75 66 3b ct stat statbuf;
e48e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
e48f0 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e 66 w-level file inf
e4900 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 6e ormation */. un
e4910 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e ixInodeInfo *pIn
e4920 6f 64 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 ode = 0; /*
e4930 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 49 6e Candidate unixIn
e4940 6f 64 65 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a odeInfo object *
e4950 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69 /.. assert( uni
e4960 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a xMutexHeld() );.
e4970 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d 6c 65 . /* Get low-le
e4980 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 vel information
e4990 61 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 74 about the file t
e49a0 68 61 74 20 77 65 20 63 61 6e 20 75 73 65 64 20 hat we can used
e49b0 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20 61 to. ** create a
e49c0 20 75 6e 69 71 75 65 20 6e 61 6d 65 20 66 6f 72 unique name for
e49d0 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a the file.. */.
e49e0 20 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 3b fd = pFile->h;
e49f0 0a 20 20 72 63 20 3d 20 6f 73 46 73 74 61 74 28 . rc = osFstat(
e4a00 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20 fd, &statbuf);.
e4a10 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 if( rc!=0 ){.
e4a20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
e4a30 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 64 no = errno;.#ifd
e4a40 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 ef EOVERFLOW.
e4a50 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73 74 if( pFile->last
e4a60 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57 Errno==EOVERFLOW
e4a70 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
e4a80 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20 _NOLFS;.#endif.
e4a90 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e4aa0 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 69 66 _IOERR;. }..#if
e4ab0 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20 20 def __APPLE__.
e4ac0 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 61 6e /* On OS X on an
e4ad0 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 65 msdos filesyste
e4ae0 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d m, the inode num
e4af0 62 65 72 20 69 73 20 72 65 70 6f 72 74 65 64 0a ber is reported.
e4b00 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 6c 79 ** incorrectly
e4b10 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 20 66 for zero-size f
e4b20 69 6c 65 73 2e 20 20 53 65 65 20 74 69 63 6b 65 iles. See ticke
e4b30 74 20 23 33 32 36 30 2e 20 20 54 6f 20 77 6f 72 t #3260. To wor
e4b40 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 74 68 k. ** around th
e4b50 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 20 63 is problem (we c
e4b60 6f 6e 73 69 64 65 72 20 69 74 20 61 20 62 75 67 onsider it a bug
e4b70 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 53 51 in OS X, not SQ
e4b80 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 61 6c Lite). ** we al
e4b90 77 61 79 73 20 69 6e 63 72 65 61 73 65 20 74 68 ways increase th
e4ba0 65 20 66 69 6c 65 20 73 69 7a 65 20 74 6f 20 31 e file size to 1
e4bb0 20 62 79 20 77 72 69 74 69 6e 67 20 61 20 73 69 by writing a si
e4bc0 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a 20 70 ngle byte. ** p
e4bd0 72 69 6f 72 20 74 6f 20 61 63 63 65 73 73 69 6e rior to accessin
e4be0 67 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 g the inode numb
e4bf0 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 79 74 er. The one byt
e4c00 65 20 77 72 69 74 74 65 6e 20 69 73 0a 20 20 2a e written is. *
e4c10 2a 20 61 6e 20 41 53 43 49 49 20 27 53 27 20 63 * an ASCII 'S' c
e4c20 68 61 72 61 63 74 65 72 20 77 68 69 63 68 20 61 haracter which a
e4c30 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 6f 20 62 lso happens to b
e4c40 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 e the first byte
e4c50 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 65 61 . ** in the hea
e4c60 64 65 72 20 6f 66 20 65 76 65 72 79 20 53 51 4c der of every SQL
e4c70 69 74 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 ite database. I
e4c80 6e 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 n this way, if t
e4c90 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20 72 here. ** is a r
e4ca0 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 75 ace condition su
e4cb0 63 68 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 ch that another
e4cc0 74 68 72 65 61 64 20 68 61 73 20 61 6c 72 65 61 thread has alrea
e4cd0 64 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 20 2a dy populated. *
e4ce0 2a 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 * the first page
e4cf0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
e4d00 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20 64 , no damage is d
e4d10 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 one.. */. if(
e4d20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65 3d statbuf.st_size=
e4d30 3d 30 20 26 26 20 28 70 46 69 6c 65 2d 3e 66 73 =0 && (pFile->fs
e4d40 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 Flags & SQLITE_F
e4d50 53 46 4c 41 47 53 5f 49 53 5f 4d 53 44 4f 53 29 SFLAGS_IS_MSDOS)
e4d60 21 3d 30 20 29 7b 0a 20 20 20 20 64 6f 7b 20 72 !=0 ){. do{ r
e4d70 63 20 3d 20 6f 73 57 72 69 74 65 28 66 64 2c 20 c = osWrite(fd,
e4d80 22 53 22 2c 20 31 29 3b 20 7d 77 68 69 6c 65 28 "S", 1); }while(
e4d90 20 72 63 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d rc<0 && errno==
e4da0 45 49 4e 54 52 20 29 3b 0a 20 20 20 20 69 66 28 EINTR );. if(
e4db0 20 72 63 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 rc!=1 ){.
e4dc0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
e4dd0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
e4de0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
e4df0 45 52 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 ERR;. }. r
e4e00 63 20 3d 20 6f 73 46 73 74 61 74 28 66 64 2c 20 c = osFstat(fd,
e4e10 26 73 74 61 74 62 75 66 29 3b 0a 20 20 20 20 69 &statbuf);. i
e4e20 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 f( rc!=0 ){.
e4e30 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
e4e40 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
e4e50 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e4e60 49 4f 45 52 52 3b 0a 20 20 20 20 7d 0a 20 20 7d IOERR;. }. }
e4e70 0a 23 65 6e 64 69 66 0a 0a 20 20 6d 65 6d 73 65 .#endif.. memse
e4e80 74 28 26 66 69 6c 65 49 64 2c 20 30 2c 20 73 69 t(&fileId, 0, si
e4e90 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 3b 0a 20 zeof(fileId));.
e4ea0 20 66 69 6c 65 49 64 2e 64 65 76 20 3d 20 73 74 fileId.dev = st
e4eb0 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69 atbuf.st_dev;.#i
e4ec0 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 66 f OS_VXWORKS. f
e4ed0 69 6c 65 49 64 2e 70 49 64 20 3d 20 70 46 69 6c ileId.pId = pFil
e4ee0 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65 0a 20 20 e->pId;.#else.
e4ef0 66 69 6c 65 49 64 2e 69 6e 6f 20 3d 20 73 74 61 fileId.ino = sta
e4f00 74 62 75 66 2e 73 74 5f 69 6e 6f 3b 0a 23 65 6e tbuf.st_ino;.#en
e4f10 64 69 66 0a 20 20 70 49 6e 6f 64 65 20 3d 20 69 dif. pInode = i
e4f20 6e 6f 64 65 4c 69 73 74 3b 0a 20 20 77 68 69 6c nodeList;. whil
e4f30 65 28 20 70 49 6e 6f 64 65 20 26 26 20 6d 65 6d e( pInode && mem
e4f40 63 6d 70 28 26 66 69 6c 65 49 64 2c 20 26 70 49 cmp(&fileId, &pI
e4f50 6e 6f 64 65 2d 3e 66 69 6c 65 49 64 2c 20 73 69 node->fileId, si
e4f60 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 20 29 7b zeof(fileId)) ){
e4f70 0a 20 20 20 20 70 49 6e 6f 64 65 20 3d 20 70 49 . pInode = pI
e4f80 6e 6f 64 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d node->pNext;. }
e4f90 0a 20 20 69 66 28 20 70 49 6e 6f 64 65 3d 3d 30 . if( pInode==0
e4fa0 20 29 7b 0a 20 20 20 20 70 49 6e 6f 64 65 20 3d ){. pInode =
e4fb0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
e4fc0 20 73 69 7a 65 6f 66 28 2a 70 49 6e 6f 64 65 29 sizeof(*pInode)
e4fd0 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 6f );. if( pIno
e4fe0 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 de==0 ){. r
e4ff0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
e5000 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 EM;. }. me
e5010 6d 73 65 74 28 70 49 6e 6f 64 65 2c 20 30 2c 20 mset(pInode, 0,
e5020 73 69 7a 65 6f 66 28 2a 70 49 6e 6f 64 65 29 29 sizeof(*pInode))
e5030 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 49 ;. memcpy(&pI
e5040 6e 6f 64 65 2d 3e 66 69 6c 65 49 64 2c 20 26 66 node->fileId, &f
e5050 69 6c 65 49 64 2c 20 73 69 7a 65 6f 66 28 66 69 ileId, sizeof(fi
e5060 6c 65 49 64 29 29 3b 0a 20 20 20 20 70 49 6e 6f leId));. pIno
e5070 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 de->nRef = 1;.
e5080 20 20 70 49 6e 6f 64 65 2d 3e 70 4e 65 78 74 20 pInode->pNext
e5090 3d 20 69 6e 6f 64 65 4c 69 73 74 3b 0a 20 20 20 = inodeList;.
e50a0 20 70 49 6e 6f 64 65 2d 3e 70 50 72 65 76 20 3d pInode->pPrev =
e50b0 20 30 3b 0a 20 20 20 20 69 66 28 20 69 6e 6f 64 0;. if( inod
e50c0 65 4c 69 73 74 20 29 20 69 6e 6f 64 65 4c 69 73 eList ) inodeLis
e50d0 74 2d 3e 70 50 72 65 76 20 3d 20 70 49 6e 6f 64 t->pPrev = pInod
e50e0 65 3b 0a 20 20 20 20 69 6e 6f 64 65 4c 69 73 74 e;. inodeList
e50f0 20 3d 20 70 49 6e 6f 64 65 3b 0a 20 20 7d 65 6c = pInode;. }el
e5100 73 65 7b 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e se{. pInode->
e5110 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 nRef++;. }. *p
e5120 70 49 6e 6f 64 65 20 3d 20 70 49 6e 6f 64 65 3b pInode = pInode;
e5130 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
e5140 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 _OK;.}.../*.** T
e5150 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 his routine chec
e5160 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 ks if there is a
e5170 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 RESERVED lock h
e5180 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 eld on the speci
e5190 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 fied.** file by
e51a0 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 this or any othe
e51b0 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 r process. If su
e51c0 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c ch a lock is hel
e51d0 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a d, set *pResOut.
e51e0 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f ** to a non-zero
e51f0 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 value otherwise
e5200 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 *pResOut is set
e5210 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 to zero. The r
e5220 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 eturn value.** i
e5230 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f s set to SQLITE_
e5240 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f OK unless an I/O
e5250 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 error occurs du
e5260 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 ring lock checki
e5270 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
e5280 74 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 t unixCheckReser
e5290 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f vedLock(sqlite3_
e52a0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 file *id, int *p
e52b0 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 ResOut){. int r
e52c0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
e52d0 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 int reserved =
e52e0 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 0;. unixFile *p
e52f0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
e5300 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 *)id;.. Simulat
e5310 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e eIOError( return
e5320 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 SQLITE_IOERR_CH
e5330 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b ECKRESERVEDLOCK;
e5340 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 );.. assert( p
e5350 46 69 6c 65 20 29 3b 0a 20 20 75 6e 69 78 45 6e File );. unixEn
e5360 74 65 72 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42 terMutex(); /* B
e5370 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 49 ecause pFile->pI
e5380 6e 6f 64 65 20 69 73 20 73 68 61 72 65 64 20 61 node is shared a
e5390 63 72 6f 73 73 20 74 68 72 65 61 64 73 20 2a 2f cross threads */
e53a0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 .. /* Check if
e53b0 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 a thread in this
e53c0 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 process holds s
e53d0 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 uch a lock */.
e53e0 69 66 28 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 if( pFile->pInod
e53f0 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3e 53 48 41 e->eFileLock>SHA
e5400 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 RED_LOCK ){.
e5410 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 reserved = 1;.
e5420 7d 0a 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 }.. /* Otherwis
e5430 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 e see if some ot
e5440 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 her process hold
e5450 73 20 69 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 s it.. */.#ifnd
e5460 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 20 20 69 ef __DJGPP__. i
e5470 66 28 20 21 72 65 73 65 72 76 65 64 20 26 26 20 f( !reserved &&
e5480 21 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e !pFile->pInode->
e5490 62 50 72 6f 63 65 73 73 4c 6f 63 6b 20 29 7b 0a bProcessLock ){.
e54a0 20 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b struct flock
e54b0 20 6c 6f 63 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e lock;. lock.
e54c0 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f l_whence = SEEK_
e54d0 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f SET;. lock.l_
e54e0 73 74 61 72 74 20 3d 20 52 45 53 45 52 56 45 44 start = RESERVED
e54f0 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e _BYTE;. lock.
e5500 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 20 20 6c l_len = 1;. l
e5510 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 ock.l_type = F_W
e5520 52 4c 43 4b 3b 0a 20 20 20 20 69 66 28 20 6f 73 RLCK;. if( os
e5530 46 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 Fcntl(pFile->h,
e5540 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 20 F_GETLK, &lock)
e5550 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
e5560 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b LITE_IOERR_CHECK
e5570 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 0a 20 20 RESERVEDLOCK;.
e5580 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
e5590 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
e55a0 20 20 7d 20 65 6c 73 65 20 69 66 28 20 6c 6f 63 } else if( loc
e55b0 6b 2e 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c 43 k.l_type!=F_UNLC
e55c0 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 73 65 72 K ){. reser
e55d0 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 ved = 1;. }.
e55e0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 75 }.#endif. . u
e55f0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
e5600 0a 20 20 4f 53 54 52 41 43 45 28 28 22 54 45 53 . OSTRACE(("TES
e5610 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 T WR-LOCK %d %d
e5620 25 64 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 %d (unix)\n", pF
e5630 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 ile->h, rc, rese
e5640 72 76 65 64 29 29 3b 0a 0a 20 20 2a 70 52 65 73 rved));.. *pRes
e5650 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a Out = reserved;.
e5660 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
e5670 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f /*.** Attempt to
e5680 20 73 65 74 20 61 20 73 79 73 74 65 6d 2d 6c 6f set a system-lo
e5690 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 70 ck on the file p
e56a0 46 69 6c 65 2e 20 20 54 68 65 20 6c 6f 63 6b 20 File. The lock
e56b0 69 73 20 0a 2a 2a 20 64 65 73 63 72 69 62 65 64 is .** described
e56c0 20 62 79 20 70 4c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a by pLock..**.**
e56d0 20 49 66 20 74 68 65 20 70 46 69 6c 65 20 77 61 If the pFile wa
e56e0 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2f 77 72 s opened read/wr
e56f0 69 74 65 20 66 72 6f 6d 20 75 6e 69 78 2d 65 78 ite from unix-ex
e5700 63 6c 2c 20 74 68 65 6e 20 74 68 65 20 6f 6e 6c cl, then the onl
e5710 79 20 6c 6f 63 6b 0a 2a 2a 20 65 76 65 72 20 6f y lock.** ever o
e5720 62 74 61 69 6e 65 64 20 69 73 20 61 6e 20 65 78 btained is an ex
e5730 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 20 61 6e clusive lock, an
e5740 64 20 69 74 20 69 73 20 6f 62 74 61 69 6e 65 64 d it is obtained
e5750 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 0a 2a 2a exactly once.**
e5760 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 the first time
e5770 61 6e 79 20 6c 6f 63 6b 20 69 73 20 61 74 74 65 any lock is atte
e5780 6d 70 74 65 64 2e 20 20 41 6c 6c 20 73 75 62 73 mpted. All subs
e5790 65 71 75 65 6e 74 20 73 79 73 74 65 6d 20 6c 6f equent system lo
e57a0 63 6b 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69 cking.** operati
e57b0 6f 6e 73 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 ons become no-op
e57c0 73 2e 20 20 4c 6f 63 6b 69 6e 67 20 6f 70 65 72 s. Locking oper
e57d0 61 74 69 6f 6e 73 20 73 74 69 6c 6c 20 68 61 70 ations still hap
e57e0 70 65 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 0a pen internally,.
e57f0 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 ** in order to c
e5800 6f 6f 72 64 69 6e 61 74 65 20 61 63 63 65 73 73 oordinate access
e5810 20 62 65 74 77 65 65 6e 20 73 65 70 61 72 61 74 between separat
e5820 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
e5830 63 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 69 6e ctions.** within
e5840 20 74 68 69 73 20 70 72 6f 63 65 73 73 2c 20 62 this process, b
e5850 75 74 20 61 6c 6c 20 6f 66 20 74 68 61 74 20 69 ut all of that i
e5860 73 20 68 61 6e 64 6c 65 64 20 69 6e 20 6d 65 6d s handled in mem
e5870 6f 72 79 20 61 6e 64 20 74 68 65 0a 2a 2a 20 6f ory and the.** o
e5880 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
e5890 64 6f 65 73 20 6e 6f 74 20 70 61 72 74 69 63 69 does not partici
e58a0 70 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 pate..**.** This
e58b0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 70 function is a p
e58c0 61 73 73 2d 74 68 72 6f 75 67 68 20 74 6f 20 66 ass-through to f
e58d0 63 6e 74 6c 28 46 5f 53 45 54 4c 4b 29 20 69 66 cntl(F_SETLK) if
e58e0 20 70 46 69 6c 65 20 69 73 20 75 73 69 6e 67 0a pFile is using.
e58f0 2a 2a 20 61 6e 79 20 56 46 53 20 6f 74 68 65 72 ** any VFS other
e5900 20 74 68 61 6e 20 22 75 6e 69 78 2d 65 78 63 6c than "unix-excl
e5910 22 20 6f 72 20 69 66 20 70 46 69 6c 65 20 69 73 " or if pFile is
e5920 20 6f 70 65 6e 65 64 20 6f 6e 20 22 75 6e 69 78 opened on "unix
e5930 2d 65 78 63 6c 22 0a 2a 2a 20 61 6e 64 20 69 73 -excl".** and is
e5940 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2a 0a 2a read-only..**.*
e5950 2a 20 5a 65 72 6f 20 69 73 20 72 65 74 75 72 6e * Zero is return
e5960 65 64 20 69 66 20 74 68 65 20 63 61 6c 6c 20 63 ed if the call c
e5970 6f 6d 70 6c 65 74 65 73 20 73 75 63 63 65 73 73 ompletes success
e5980 66 75 6c 6c 79 2c 20 6f 72 20 2d 31 20 69 66 20 fully, or -1 if
e5990 61 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 66 63 6e a call.** to fcn
e59a0 74 6c 28 29 20 66 61 69 6c 73 2e 20 49 6e 20 74 tl() fails. In t
e59b0 68 69 73 20 63 61 73 65 2c 20 65 72 72 6e 6f 20 his case, errno
e59c0 69 73 20 73 65 74 20 61 70 70 72 6f 70 72 69 61 is set appropria
e59d0 74 65 6c 79 20 28 62 79 20 66 63 6e 74 6c 28 29 tely (by fcntl()
e59e0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
e59f0 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 75 6e unixFileLock(un
e5a00 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 73 ixFile *pFile, s
e5a10 74 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 4c 6f truct flock *pLo
e5a20 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ck){. int rc;.
e5a30 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a unixInodeInfo *
e5a40 70 49 6e 6f 64 65 20 3d 20 70 46 69 6c 65 2d 3e pInode = pFile->
e5a50 70 49 6e 6f 64 65 3b 0a 20 20 61 73 73 65 72 74 pInode;. assert
e5a60 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 ( unixMutexHeld(
e5a70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
e5a80 49 6e 6f 64 65 21 3d 30 20 29 3b 0a 20 20 69 66 Inode!=0 );. if
e5a90 28 20 28 28 70 46 69 6c 65 2d 3e 63 74 72 6c 46 ( ((pFile->ctrlF
e5aa0 6c 61 67 73 20 26 20 55 4e 49 58 46 49 4c 45 5f lags & UNIXFILE_
e5ab0 45 58 43 4c 29 21 3d 30 20 7c 7c 20 70 49 6e 6f EXCL)!=0 || pIno
e5ac0 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f 63 6b de->bProcessLock
e5ad0 29 0a 20 20 20 26 26 20 28 28 70 46 69 6c 65 2d ). && ((pFile-
e5ae0 3e 63 74 72 6c 46 6c 61 67 73 20 26 20 55 4e 49 >ctrlFlags & UNI
e5af0 58 46 49 4c 45 5f 52 44 4f 4e 4c 59 29 3d 3d 30 XFILE_RDONLY)==0
e5b00 29 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ). ){. if( p
e5b10 49 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c Inode->bProcessL
e5b20 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ock==0 ){.
e5b30 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 struct flock loc
e5b40 6b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 k;. assert(
e5b50 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 3d 3d pInode->nLock==
e5b60 30 20 29 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 0 );. lock.
e5b70 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f l_whence = SEEK_
e5b80 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e SET;. lock.
e5b90 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 l_start = SHARED
e5ba0 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 6c 6f _FIRST;. lo
e5bb0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 ck.l_len = SHARE
e5bc0 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 6c 6f D_SIZE;. lo
e5bd0 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 ck.l_type = F_WR
e5be0 4c 43 4b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 LCK;. rc =
e5bf0 6f 73 46 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 osFcntl(pFile->h
e5c00 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_SETLK, &lock
e5c10 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3c );. if( rc<
e5c20 30 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 0 ) return rc;.
e5c30 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e 62 50 72 pInode->bPr
e5c40 6f 63 65 73 73 4c 6f 63 6b 20 3d 20 31 3b 0a 20 ocessLock = 1;.
e5c50 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f pInode->nLo
e5c60 63 6b 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ck++;. }else{
e5c70 0a 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 . rc = 0;.
e5c80 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
e5c90 20 20 72 63 20 3d 20 6f 73 46 63 6e 74 6c 28 70 rc = osFcntl(p
e5ca0 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b File->h, F_SETLK
e5cb0 2c 20 70 4c 6f 63 6b 29 3b 0a 20 20 7d 0a 20 20 , pLock);. }.
e5cc0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
e5cd0 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c .** Lock the fil
e5ce0 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 e with the lock
e5cf0 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 specified by par
e5d00 61 6d 65 74 65 72 20 65 46 69 6c 65 4c 6f 63 6b ameter eFileLock
e5d10 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 - one.** of the
e5d20 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
e5d30 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 * (1) SHARED
e5d40 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 _LOCK.** (2)
e5d50 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a RESERVED_LOCK.*
e5d60 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e * (3) PENDIN
e5d70 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 G_LOCK.** (4
e5d80 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ) EXCLUSIVE_LOCK
e5d90 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 .**.** Sometimes
e5da0 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 when requesting
e5db0 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c one lock state,
e5dc0 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b additional lock
e5dd0 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 states.** are i
e5de0 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 nserted in betwe
e5df0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 en. The locking
e5e00 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f might fail on o
e5e10 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a ne of the later.
e5e20 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c ** transitions l
e5e30 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 eaving the lock
e5e40 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 state different
e5e50 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 from what it sta
e5e60 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c rted but.** stil
e5e70 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 l short of its g
e5e80 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 oal. The follow
e5e90 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 ing chart shows
e5ea0 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 the allowed.** t
e5eb0 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 ransitions and t
e5ec0 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 he inserted inte
e5ed0 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a rmediate states:
e5ee0 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b .**.** UNLOCK
e5ef0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 ED -> SHARED.**
e5f00 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 SHARED -> RES
e5f10 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 ERVED.** SHAR
e5f20 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
e5f30 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
e5f40 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 RESERVED -> (
e5f50 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
e5f60 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 USIVE.** PEND
e5f70 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 ING -> EXCLUSIVE
e5f80 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
e5f90 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e ine will only in
e5fa0 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 crease a lock.
e5fb0 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f Use the sqlite3O
e5fc0 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 sUnlock().** rou
e5fd0 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 tine to lower a
e5fe0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a locking level..*
e5ff0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
e6000 78 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 xLock(sqlite3_fi
e6010 6c 65 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c le *id, int eFil
e6020 65 4c 6f 63 6b 29 7b 0a 20 20 2f 2a 20 54 68 65 eLock){. /* The
e6030 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 65 73 63 72 following descr
e6040 69 62 65 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 ibes the impleme
e6050 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 ntation of the v
e6060 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 61 6e 64 arious locks and
e6070 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74 72 61 6e 73 . ** lock trans
e6080 69 74 69 6f 6e 73 20 69 6e 20 74 65 72 6d 73 20 itions in terms
e6090 6f 66 20 74 68 65 20 50 4f 53 49 58 20 61 64 76 of the POSIX adv
e60a0 69 73 6f 72 79 20 73 68 61 72 65 64 20 61 6e 64 isory shared and
e60b0 20 65 78 63 6c 75 73 69 76 65 0a 20 20 2a 2a 20 exclusive. **
e60c0 6c 6f 63 6b 20 70 72 69 6d 69 74 69 76 65 73 20 lock primitives
e60d0 28 63 61 6c 6c 65 64 20 72 65 61 64 2d 6c 6f 63 (called read-loc
e60e0 6b 73 20 61 6e 64 20 77 72 69 74 65 2d 6c 6f 63 ks and write-loc
e60f0 6b 73 20 62 65 6c 6f 77 2c 20 74 6f 20 61 76 6f ks below, to avo
e6100 69 64 0a 20 20 2a 2a 20 63 6f 6e 66 75 73 69 6f id. ** confusio
e6110 6e 20 77 69 74 68 20 53 51 4c 69 74 65 20 6c 6f n with SQLite lo
e6120 63 6b 20 6e 61 6d 65 73 29 2e 20 54 68 65 20 61 ck names). The a
e6130 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 20 63 6f lgorithms are co
e6140 6d 70 6c 69 63 61 74 65 64 0a 20 20 2a 2a 20 73 mplicated. ** s
e6150 6c 69 67 68 74 6c 79 20 69 6e 20 6f 72 64 65 72 lightly in order
e6160 20 74 6f 20 62 65 20 63 6f 6d 70 61 74 69 62 6c to be compatibl
e6170 65 20 77 69 74 68 20 77 69 6e 64 6f 77 73 20 73 e with windows s
e6180 79 73 74 65 6d 73 20 73 69 6d 75 6c 74 61 6e 65 ystems simultane
e6190 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 63 63 65 73 ously. ** acces
e61a0 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 sing the same da
e61b0 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 6e 20 tabase file, in
e61c0 63 61 73 65 20 74 68 61 74 20 69 73 20 65 76 65 case that is eve
e61d0 72 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a r required.. **
e61e0 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c 73 20 64 65 . ** Symbols de
e61f0 66 69 6e 65 64 20 69 6e 20 6f 73 2e 68 20 69 6e fined in os.h in
e6200 64 65 6e 74 69 66 79 20 74 68 65 20 27 70 65 6e dentify the 'pen
e6210 64 69 6e 67 20 62 79 74 65 27 20 61 6e 64 20 74 ding byte' and t
e6220 68 65 20 27 72 65 73 65 72 76 65 64 0a 20 20 2a he 'reserved. *
e6230 2a 20 62 79 74 65 27 2c 20 65 61 63 68 20 73 69 * byte', each si
e6240 6e 67 6c 65 20 62 79 74 65 73 20 61 74 20 77 65 ngle bytes at we
e6250 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 73 ll known offsets
e6260 2c 20 61 6e 64 20 74 68 65 20 27 73 68 61 72 65 , and the 'share
e6270 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 d byte. ** rang
e6280 65 27 2c 20 61 20 72 61 6e 67 65 20 6f 66 20 35 e', a range of 5
e6290 31 30 20 62 79 74 65 73 20 61 74 20 61 20 77 65 10 bytes at a we
e62a0 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 2e ll known offset.
e62b0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 6f 62 . **. ** To ob
e62c0 74 61 69 6e 20 61 20 53 48 41 52 45 44 20 6c 6f tain a SHARED lo
e62d0 63 6b 2c 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 ck, a read-lock
e62e0 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 is obtained on t
e62f0 68 65 20 27 70 65 6e 64 69 6e 67 0a 20 20 2a 2a he 'pending. **
e6300 20 62 79 74 65 27 2e 20 20 49 66 20 74 68 69 73 byte'. If this
e6310 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 is successful,
e6320 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 66 72 a random byte fr
e6330 6f 6d 20 74 68 65 20 27 73 68 61 72 65 64 20 62 om the 'shared b
e6340 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 yte. ** range'
e6350 69 73 20 72 65 61 64 2d 6c 6f 63 6b 65 64 20 61 is read-locked a
e6360 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 nd the lock on t
e6370 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 he 'pending byte
e6380 27 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2a ' released.. **
e6390 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 . ** A process
e63a0 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 may only obtain
e63b0 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
e63c0 61 66 74 65 72 20 69 74 20 68 61 73 20 61 20 53 after it has a S
e63d0 48 41 52 45 44 20 6c 6f 63 6b 2e 0a 20 20 2a 2a HARED lock.. **
e63e0 20 41 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b A RESERVED lock
e63f0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 is implemented
e6400 62 79 20 67 72 61 62 62 69 6e 67 20 61 20 77 72 by grabbing a wr
e6410 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a ite-lock on the.
e6420 20 20 2a 2a 20 27 72 65 73 65 72 76 65 64 20 62 ** 'reserved b
e6430 79 74 65 27 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a yte'. . **. **
e6440 20 41 20 70 72 6f 63 65 73 73 20 6d 61 79 20 6f A process may o
e6450 6e 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 45 4e nly obtain a PEN
e6460 44 49 4e 47 20 6c 6f 63 6b 20 61 66 74 65 72 20 DING lock after
e6470 69 74 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20 it has obtained
e6480 61 0a 20 20 2a 2a 20 53 48 41 52 45 44 20 6c 6f a. ** SHARED lo
e6490 63 6b 2e 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f ck. A PENDING lo
e64a0 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 ck is implemente
e64b0 64 20 62 79 20 6f 62 74 61 69 6e 69 6e 67 20 61 d by obtaining a
e64c0 20 77 72 69 74 65 2d 6c 6f 63 6b 0a 20 20 2a 2a write-lock. **
e64d0 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 on the 'pending
e64e0 20 62 79 74 65 27 2e 20 54 68 69 73 20 65 6e 73 byte'. This ens
e64f0 75 72 65 73 20 74 68 61 74 20 6e 6f 20 6e 65 77 ures that no new
e6500 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 63 61 SHARED locks ca
e6510 6e 20 62 65 0a 20 20 2a 2a 20 6f 62 74 61 69 6e n be. ** obtain
e6520 65 64 2c 20 62 75 74 20 65 78 69 73 74 69 6e 67 ed, but existing
e6530 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 61 72 SHARED locks ar
e6540 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 70 65 72 e allowed to per
e6550 73 69 73 74 2e 20 41 20 70 72 6f 63 65 73 73 0a sist. A process.
e6560 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 ** does not ha
e6570 76 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 52 ve to obtain a R
e6580 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 ESERVED lock on
e6590 74 68 65 20 77 61 79 20 74 6f 20 61 20 50 45 4e the way to a PEN
e65a0 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 DING lock.. **
e65b0 54 68 69 73 20 70 72 6f 70 65 72 74 79 20 69 73 This property is
e65c0 20 75 73 65 64 20 62 79 20 74 68 65 20 61 6c 67 used by the alg
e65d0 6f 72 69 74 68 6d 20 66 6f 72 20 72 6f 6c 6c 69 orithm for rolli
e65e0 6e 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 ng back a journa
e65f0 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 61 66 74 65 l file. ** afte
e6600 72 20 61 20 63 72 61 73 68 2e 0a 20 20 2a 2a 0a r a crash.. **.
e6610 20 20 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 ** An EXCLUSIV
e6620 45 20 6c 6f 63 6b 2c 20 6f 62 74 61 69 6e 65 64 E lock, obtained
e6630 20 61 66 74 65 72 20 61 20 50 45 4e 44 49 4e 47 after a PENDING
e6640 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 69 lock is held, i
e6650 73 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 s. ** implement
e6660 65 64 20 62 79 20 6f 62 74 61 69 6e 69 6e 67 20 ed by obtaining
e6670 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
e6680 74 68 65 20 65 6e 74 69 72 65 20 27 73 68 61 72 the entire 'shar
e6690 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e ed byte. ** ran
e66a0 67 65 27 2e 20 53 69 6e 63 65 20 61 6c 6c 20 6f ge'. Since all o
e66b0 74 68 65 72 20 6c 6f 63 6b 73 20 72 65 71 75 69 ther locks requi
e66c0 72 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f re a read-lock o
e66d0 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 62 79 74 n one of the byt
e66e0 65 73 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20 74 es. ** within t
e66f0 68 69 73 20 72 61 6e 67 65 2c 20 74 68 69 73 20 his range, this
e6700 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f 20 ensures that no
e6710 6f 74 68 65 72 20 6c 6f 63 6b 73 20 61 72 65 20 other locks are
e6720 68 65 6c 64 20 6f 6e 20 74 68 65 0a 20 20 2a 2a held on the. **
e6730 20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 2a 2a database. . **
e6740 0a 20 20 2a 2a 20 54 68 65 20 72 65 61 73 6f 6e . ** The reason
e6750 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 63 a single byte c
e6760 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 69 6e annot be used in
e6770 73 74 65 61 64 20 6f 66 20 74 68 65 20 27 73 68 stead of the 'sh
e6780 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 ared byte. ** r
e6790 61 6e 67 65 27 20 69 73 20 74 68 61 74 20 73 6f ange' is that so
e67a0 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 77 me versions of w
e67b0 69 6e 64 6f 77 73 20 64 6f 20 6e 6f 74 20 73 75 indows do not su
e67c0 70 70 6f 72 74 20 72 65 61 64 2d 6c 6f 63 6b 73 pport read-locks
e67d0 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e . By. ** lockin
e67e0 67 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 g a random byte
e67f0 66 72 6f 6d 20 61 20 72 61 6e 67 65 2c 20 63 6f from a range, co
e6800 6e 63 75 72 72 65 6e 74 20 53 48 41 52 45 44 20 ncurrent SHARED
e6810 6c 6f 63 6b 73 20 6d 61 79 20 65 78 69 73 74 0a locks may exist.
e6820 20 20 2a 2a 20 65 76 65 6e 20 69 66 20 74 68 65 ** even if the
e6830 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 locking primiti
e6840 76 65 20 75 73 65 64 20 69 73 20 61 6c 77 61 79 ve used is alway
e6850 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2e 0a s a write-lock..
e6860 20 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 */. int rc =
e6870 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 SQLITE_OK;. uni
e6880 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
e6890 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
e68a0 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 unixInodeInfo *p
e68b0 49 6e 6f 64 65 3b 0a 20 20 73 74 72 75 63 74 20 Inode;. struct
e68c0 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e flock lock;. in
e68d0 74 20 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 0a 20 t tErrno = 0;..
e68e0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
e68f0 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 4c 4f ;. OSTRACE(("LO
e6900 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 20 CK %d %s was
e6910 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25 64 %s(%s,%d) pid=%d
e6920 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 6c (unix)\n", pFil
e6930 65 2d 3e 68 2c 0a 20 20 20 20 20 20 61 7a 46 69 e->h,. azFi
e6940 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c 6f 63 6b leLock(eFileLock
e6950 29 2c 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 70 46 ), azFileLock(pF
e6960 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 29 2c ile->eFileLock),
e6970 0a 20 20 20 20 20 20 61 7a 46 69 6c 65 4c 6f 63 . azFileLoc
e6980 6b 28 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d k(pFile->pInode-
e6990 3e 65 46 69 6c 65 4c 6f 63 6b 29 2c 20 70 46 69 >eFileLock), pFi
e69a0 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 le->pInode->nSha
e69b0 72 65 64 20 2c 20 67 65 74 70 69 64 28 29 29 29 red , getpid()))
e69c0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 ;.. /* If there
e69d0 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f is already a lo
e69e0 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 ck of this type
e69f0 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 or more restrict
e6a00 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 ive on the. **
e6a10 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 unixFile, do not
e6a20 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 hing. Don't use
e6a30 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 the end_lock: ex
e6a40 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a it path, as. **
e6a50 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
e6a60 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 ) hasn't been ca
e6a70 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 lled yet.. */.
e6a80 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c if( pFile->eFil
e6a90 65 4c 6f 63 6b 3e 3d 65 46 69 6c 65 4c 6f 63 6b eLock>=eFileLock
e6aa0 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 28 ){. OSTRACE(
e6ab0 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 ("LOCK %d %s
e6ac0 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 ok (already held
e6ad0 29 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 ) (unix)\n", pFi
e6ae0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
e6af0 20 20 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 65 46 azFileLock(eF
e6b00 69 6c 65 4c 6f 63 6b 29 29 29 3b 0a 20 20 20 20 ileLock)));.
e6b10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e6b20 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ;. }.. /* Make
e6b30 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e sure the lockin
e6b40 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f g sequence is co
e6b50 72 72 65 63 74 2e 0a 20 20 2a 2a 20 20 28 31 29 rrect.. ** (1)
e6b60 20 57 65 20 6e 65 76 65 72 20 6d 6f 76 65 20 66 We never move f
e6b70 72 6f 6d 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 rom unlocked to
e6b80 61 6e 79 74 68 69 6e 67 20 68 69 67 68 65 72 20 anything higher
e6b90 74 68 61 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b than shared lock
e6ba0 2e 0a 20 20 2a 2a 20 20 28 32 29 20 53 51 4c 69 .. ** (2) SQLi
e6bb0 74 65 20 6e 65 76 65 72 20 65 78 70 6c 69 63 69 te never explici
e6bc0 74 6c 79 20 72 65 71 75 65 73 74 73 20 61 20 70 tly requests a p
e6bd0 65 6e 64 69 67 20 6c 6f 63 6b 2e 0a 20 20 2a 2a endig lock.. **
e6be0 20 20 28 33 29 20 41 20 73 68 61 72 65 64 20 6c (3) A shared l
e6bf0 6f 63 6b 20 69 73 20 61 6c 77 61 79 73 20 68 65 ock is always he
e6c00 6c 64 20 77 68 65 6e 20 61 20 72 65 73 65 72 76 ld when a reserv
e6c10 65 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 e lock is reques
e6c20 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ted.. */. asse
e6c30 72 74 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 rt( pFile->eFile
e6c40 4c 6f 63 6b 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c Lock!=NO_LOCK ||
e6c50 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 eFileLock==SHAR
e6c60 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 ED_LOCK );. ass
e6c70 65 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b 21 3d ert( eFileLock!=
e6c80 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a PENDING_LOCK );.
e6c90 20 20 61 73 73 65 72 74 28 20 65 46 69 6c 65 4c assert( eFileL
e6ca0 6f 63 6b 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f ock!=RESERVED_LO
e6cb0 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 65 46 69 CK || pFile->eFi
e6cc0 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c leLock==SHARED_L
e6cd0 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 OCK );.. /* Thi
e6ce0 73 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 s mutex is neede
e6cf0 64 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d d because pFile-
e6d00 3e 70 49 6e 6f 64 65 20 69 73 20 73 68 61 72 65 >pInode is share
e6d10 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 d across threads
e6d20 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 . */. unixEnte
e6d30 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 49 6e 6f rMutex();. pIno
e6d40 64 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f de = pFile->pIno
e6d50 64 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d de;.. /* If som
e6d60 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 e thread using t
e6d70 68 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f his PID has a lo
e6d80 63 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65 ck via a differe
e6d90 6e 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a nt unixFile*. *
e6da0 2a 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 * handle that pr
e6db0 65 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 ecludes the requ
e6dc0 65 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 ested lock, retu
e6dd0 72 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 rn BUSY.. */.
e6de0 69 66 28 20 28 70 46 69 6c 65 2d 3e 65 46 69 6c if( (pFile->eFil
e6df0 65 4c 6f 63 6b 21 3d 70 49 6e 6f 64 65 2d 3e 65 eLock!=pInode->e
e6e00 46 69 6c 65 4c 6f 63 6b 20 26 26 20 0a 20 20 20 FileLock && .
e6e10 20 20 20 20 20 20 20 28 70 49 6e 6f 64 65 2d 3e (pInode->
e6e20 65 46 69 6c 65 4c 6f 63 6b 3e 3d 50 45 4e 44 49 eFileLock>=PENDI
e6e30 4e 47 5f 4c 4f 43 4b 20 7c 7c 20 65 46 69 6c 65 NG_LOCK || eFile
e6e40 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b Lock>SHARED_LOCK
e6e50 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d )). ){. rc =
e6e60 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
e6e70 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b goto end_lock;
e6e80 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 . }.. /* If a
e6e90 53 48 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 72 SHARED lock is r
e6ea0 65 71 75 65 73 74 65 64 2c 20 61 6e 64 20 73 6f equested, and so
e6eb0 6d 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 me thread using
e6ec0 74 68 69 73 20 50 49 44 20 61 6c 72 65 61 64 79 this PID already
e6ed0 0a 20 20 2a 2a 20 68 61 73 20 61 20 53 48 41 52 . ** has a SHAR
e6ee0 45 44 20 6f 72 20 52 45 53 45 52 56 45 44 20 6c ED or RESERVED l
e6ef0 6f 63 6b 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d ock, then increm
e6f00 65 6e 74 20 72 65 66 65 72 65 6e 63 65 20 63 6f ent reference co
e6f10 75 6e 74 73 20 61 6e 64 0a 20 20 2a 2a 20 72 65 unts and. ** re
e6f20 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a turn SQLITE_OK..
e6f30 20 20 2a 2f 0a 20 20 69 66 28 20 65 46 69 6c 65 */. if( eFile
e6f40 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 Lock==SHARED_LOC
e6f50 4b 20 26 26 20 0a 20 20 20 20 20 20 28 70 49 6e K && . (pIn
e6f60 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d ode->eFileLock==
e6f70 53 48 41 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 SHARED_LOCK || p
e6f80 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b Inode->eFileLock
e6f90 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 ==RESERVED_LOCK)
e6fa0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
e6fb0 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 eFileLock==SHARE
e6fc0 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 D_LOCK );. as
e6fd0 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 65 46 69 sert( pFile->eFi
e6fe0 6c 65 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 20 leLock==0 );.
e6ff0 20 61 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d assert( pInode-
e7000 3e 6e 53 68 61 72 65 64 3e 30 20 29 3b 0a 20 20 >nShared>0 );.
e7010 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f pFile->eFileLo
e7020 63 6b 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b ck = SHARED_LOCK
e7030 3b 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 53 ;. pInode->nS
e7040 68 61 72 65 64 2b 2b 3b 0a 20 20 20 20 70 49 6e hared++;. pIn
e7050 6f 64 65 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 ode->nLock++;.
e7060 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b goto end_lock;
e7070 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 20 50 45 . }... /* A PE
e7080 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 NDING lock is ne
e7090 65 64 65 64 20 62 65 66 6f 72 65 20 61 63 71 75 eded before acqu
e70a0 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c iring a SHARED l
e70b0 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 ock and before.
e70c0 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e ** acquiring an
e70d0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e EXCLUSIVE lock.
e70e0 20 20 46 6f 72 20 74 68 65 20 53 48 41 52 45 44 For the SHARED
e70f0 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 lock, the PENDI
e7100 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 NG will. ** be
e7110 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 released.. */.
e7120 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c lock.l_len = 1L
e7130 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 ;. lock.l_whenc
e7140 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 e = SEEK_SET;.
e7150 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 if( eFileLock==S
e7160 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 HARED_LOCK .
e7170 20 20 7c 7c 20 28 65 46 69 6c 65 4c 6f 63 6b 3d || (eFileLock=
e7180 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
e7190 26 26 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c && pFile->eFileL
e71a0 6f 63 6b 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b ock<PENDING_LOCK
e71b0 29 0a 20 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e ). ){. lock.
e71c0 6c 5f 74 79 70 65 20 3d 20 28 65 46 69 6c 65 4c l_type = (eFileL
e71d0 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ock==SHARED_LOCK
e71e0 3f 46 5f 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b ?F_RDLCK:F_WRLCK
e71f0 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 );. lock.l_st
e7200 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 art = PENDING_BY
e7210 54 45 3b 0a 20 20 20 20 69 66 28 20 75 6e 69 78 TE;. if( unix
e7220 46 69 6c 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 FileLock(pFile,
e7230 26 6c 6f 63 6b 29 20 29 7b 0a 20 20 20 20 20 20 &lock) ){.
e7240 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
e7250 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
e7260 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
e7270 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
e7280 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b ITE_IOERR_LOCK);
e7290 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
e72a0 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 QLITE_BUSY ){.
e72b0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
e72c0 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
e72d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 . }. g
e72e0 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 oto end_lock;.
e72f0 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 49 }. }... /* I
e7300 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74 f control gets t
e7310 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 o this point, th
e7320 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f 20 61 en actually go a
e7330 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a 20 20 head and make.
e7340 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 ** operating sys
e7350 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 tem calls for th
e7360 65 20 73 70 65 63 69 66 69 65 64 20 6c 6f 63 6b e specified lock
e7370 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 46 69 .. */. if( eFi
e7380 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c leLock==SHARED_L
e7390 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 OCK ){. asser
e73a0 74 28 20 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 t( pInode->nShar
e73b0 65 64 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 ed==0 );. ass
e73c0 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 65 46 69 ert( pInode->eFi
e73d0 6c 65 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 20 leLock==0 );.
e73e0 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
e73f0 49 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 2f ITE_OK );.. /
e7400 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20 72 65 * Now get the re
e7410 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c ad-lock */. l
e7420 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 ock.l_start = SH
e7430 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 ARED_FIRST;.
e7440 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 lock.l_len = SHA
e7450 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 69 66 RED_SIZE;. if
e7460 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70 ( unixFileLock(p
e7470 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 20 29 7b 0a File, &lock) ){.
e7480 20 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 tErrno = e
e7490 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d rrno;. rc =
e74a0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
e74b0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
e74c0 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o, SQLITE_IOERR_
e74d0 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 LOCK);. }..
e74e0 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 65 /* Drop the te
e74f0 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e 47 20 mporary PENDING
e7500 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b lock */. lock
e7510 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49 .l_start = PENDI
e7520 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 NG_BYTE;. loc
e7530 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 k.l_len = 1L;.
e7540 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 lock.l_type =
e7550 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 69 66 28 F_UNLCK;. if(
e7560 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70 46 unixFileLock(pF
e7570 69 6c 65 2c 20 26 6c 6f 63 6b 29 20 26 26 20 72 ile, &lock) && r
e7580 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
e7590 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f /* This co
e75a0 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74 68 20 uld happen with
e75b0 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e 74 20 a network mount
e75c0 2a 2f 0a 20 20 20 20 20 20 74 45 72 72 6e 6f 20 */. tErrno
e75d0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 = errno;. r
e75e0 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
e75f0 5f 55 4e 4c 4f 43 4b 3b 20 0a 20 20 20 20 7d 0a _UNLOCK; . }.
e7600 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 . if( rc ){.
e7610 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
e7620 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 ITE_BUSY ){.
e7630 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
e7640 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
e7650 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 }. got
e7660 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 o end_lock;.
e7670 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 }else{. pFi
e7680 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 le->eFileLock =
e7690 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 SHARED_LOCK;.
e76a0 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b pInode->nLock
e76b0 2b 2b 3b 0a 20 20 20 20 20 20 70 49 6e 6f 64 65 ++;. pInode
e76c0 2d 3e 6e 53 68 61 72 65 64 20 3d 20 31 3b 0a 20 ->nShared = 1;.
e76d0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
e76e0 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 4c eFileLock==EXCL
e76f0 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 49 USIVE_LOCK && pI
e7700 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 3e 31 20 node->nShared>1
e7710 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 ){. /* We are
e7720 20 74 72 79 69 6e 67 20 66 6f 72 20 61 6e 20 65 trying for an e
e7730 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 62 75 xclusive lock bu
e7740 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 t another thread
e7750 20 69 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 in this. **
e7760 73 61 6d 65 20 70 72 6f 63 65 73 73 20 69 73 20 same process is
e7770 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 20 still holding a
e7780 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a shared lock. */.
e7790 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
e77a0 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 BUSY;. }else{.
e77b0 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 /* The reques
e77c0 74 20 77 61 73 20 66 6f 72 20 61 20 52 45 53 45 t was for a RESE
e77d0 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 RVED or EXCLUSIV
e77e0 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 E lock. It is.
e77f0 20 20 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 ** assumed th
e7800 61 74 20 74 68 65 72 65 20 69 73 20 61 20 53 48 at there is a SH
e7810 41 52 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 ARED or greater
e7820 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 lock on the file
e7830 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e . ** already.
e7840 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
e7850 72 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 65 46 rt( 0!=pFile->eF
e7860 69 6c 65 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 6c ileLock );. l
e7870 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 ock.l_type = F_W
e7880 52 4c 43 4b 3b 0a 0a 20 20 20 20 61 73 73 65 72 RLCK;.. asser
e7890 74 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 52 45 t( eFileLock==RE
e78a0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 65 SERVED_LOCK || e
e78b0 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 FileLock==EXCLUS
e78c0 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 IVE_LOCK );.
e78d0 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 52 if( eFileLock==R
e78e0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a ESERVED_LOCK ){.
e78f0 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 lock.l_sta
e7900 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 rt = RESERVED_BY
e7910 54 45 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c TE;. lock.l
e7920 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 20 20 7d _len = 1L;. }
e7930 65 6c 73 65 7b 0a 20 20 20 20 20 20 6c 6f 63 6b else{. lock
e7940 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 .l_start = SHARE
e7950 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 6c D_FIRST;. l
e7960 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 ock.l_len = SHAR
e7970 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 0a ED_SIZE;. }..
e7980 20 20 20 20 69 66 28 20 75 6e 69 78 46 69 6c 65 if( unixFile
e7990 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 26 6c 6f 63 Lock(pFile, &loc
e79a0 6b 29 20 29 7b 0a 20 20 20 20 20 20 74 45 72 72 k) ){. tErr
e79b0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
e79c0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
e79d0 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
e79e0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
e79f0 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 IOERR_LOCK);.
e7a00 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
e7a10 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 E_BUSY ){.
e7a20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
e7a30 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
e7a40 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
e7a50 20 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ..#ifdef SQLITE
e7a60 5f 44 45 42 55 47 0a 20 20 2f 2a 20 53 65 74 20 _DEBUG. /* Set
e7a70 75 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 up the transacti
e7a80 6f 6e 2d 63 6f 75 6e 74 65 72 20 63 68 61 6e 67 on-counter chang
e7a90 65 20 63 68 65 63 6b 69 6e 67 20 66 6c 61 67 73 e checking flags
e7aa0 20 77 68 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 when. ** trans
e7ab0 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 61 20 itioning from a
e7ac0 53 48 41 52 45 44 20 74 6f 20 61 20 52 45 53 45 SHARED to a RESE
e7ad0 52 56 45 44 20 6c 6f 63 6b 2e 20 20 54 68 65 20 RVED lock. The
e7ae0 63 68 61 6e 67 65 0a 20 20 2a 2a 20 66 72 6f 6d change. ** from
e7af0 20 53 48 41 52 45 44 20 74 6f 20 52 45 53 45 52 SHARED to RESER
e7b00 56 45 44 20 6d 61 72 6b 73 20 74 68 65 20 62 65 VED marks the be
e7b10 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f 72 ginning of a nor
e7b20 6d 61 6c 0a 20 20 2a 2a 20 77 72 69 74 65 20 6f mal. ** write o
e7b30 70 65 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61 20 peration (not a
e7b40 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c hot journal roll
e7b50 62 61 63 6b 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 back).. */. if
e7b60 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a ( rc==SQLITE_OK.
e7b70 20 20 20 26 26 20 70 46 69 6c 65 2d 3e 65 46 69 && pFile->eFi
e7b80 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 44 5f 4c leLock<=SHARED_L
e7b90 4f 43 4b 0a 20 20 20 26 26 20 65 46 69 6c 65 4c OCK. && eFileL
e7ba0 6f 63 6b 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f ock==RESERVED_LO
e7bb0 43 4b 0a 20 20 29 7b 0a 20 20 20 20 70 46 69 6c CK. ){. pFil
e7bc0 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 e->transCntrChng
e7bd0 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d = 0;. pFile-
e7be0 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b 0a 20 >dbUpdate = 0;.
e7bf0 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d pFile->inNorm
e7c00 61 6c 57 72 69 74 65 20 3d 20 31 3b 0a 20 20 7d alWrite = 1;. }
e7c10 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 69 66 28 20 .#endif... if(
e7c20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
e7c30 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c . pFile->eFil
e7c40 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 eLock = eFileLoc
e7c50 6b 3b 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 65 k;. pInode->e
e7c60 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 FileLock = eFile
e7c70 4c 6f 63 6b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Lock;. }else if
e7c80 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 ( eFileLock==EXC
e7c90 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 LUSIVE_LOCK ){.
e7ca0 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c pFile->eFileL
e7cb0 6f 63 6b 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f ock = PENDING_LO
e7cc0 43 4b 3b 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e CK;. pInode->
e7cd0 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 50 45 4e 44 eFileLock = PEND
e7ce0 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 0a 65 ING_LOCK;. }..e
e7cf0 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c nd_lock:. unixL
e7d00 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f eaveMutex();. O
e7d10 53 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 20 20 STRACE(("LOCK
e7d20 20 25 64 20 25 73 20 25 73 20 28 75 6e 69 78 29 %d %s %s (unix)
e7d30 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 61 \n", pFile->h, a
e7d40 7a 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c zFileLock(eFileL
e7d50 6f 63 6b 29 2c 20 0a 20 20 20 20 20 20 72 63 3d ock), . rc=
e7d60 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b =SQLITE_OK ? "ok
e7d70 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b 0a " : "failed"));.
e7d80 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
e7d90 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 66 69 /*.** Add the fi
e7da0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 75 73 le descriptor us
e7db0 65 64 20 62 79 20 66 69 6c 65 20 68 61 6e 64 6c ed by file handl
e7dc0 65 20 70 46 69 6c 65 20 74 6f 20 74 68 65 20 63 e pFile to the c
e7dd0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 orresponding.**
e7de0 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2f pUnused list..*/
e7df0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 .static void set
e7e00 50 65 6e 64 69 6e 67 46 64 28 75 6e 69 78 46 69 PendingFd(unixFi
e7e10 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 75 6e le *pFile){. un
e7e20 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e ixInodeInfo *pIn
e7e30 6f 64 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e ode = pFile->pIn
e7e40 6f 64 65 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 ode;. UnixUnuse
e7e50 64 46 64 20 2a 70 20 3d 20 70 46 69 6c 65 2d 3e dFd *p = pFile->
e7e60 70 55 6e 75 73 65 64 3b 0a 20 20 70 2d 3e 70 4e pUnused;. p->pN
e7e70 65 78 74 20 3d 20 70 49 6e 6f 64 65 2d 3e 70 55 ext = pInode->pU
e7e80 6e 75 73 65 64 3b 0a 20 20 70 49 6e 6f 64 65 2d nused;. pInode-
e7e90 3e 70 55 6e 75 73 65 64 20 3d 20 70 3b 0a 20 20 >pUnused = p;.
e7ea0 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 pFile->h = -1;.
e7eb0 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 20 pFile->pUnused
e7ec0 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f = 0;.}../*.** Lo
e7ed0 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 wer the locking
e7ee0 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 level on file de
e7ef0 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 scriptor pFile t
e7f00 6f 20 65 46 69 6c 65 4c 6f 63 6b 2e 20 20 65 46 o eFileLock. eF
e7f10 69 6c 65 4c 6f 63 6b 0a 2a 2a 20 6d 75 73 74 20 ileLock.** must
e7f20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 be either NO_LOC
e7f30 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b K or SHARED_LOCK
e7f40 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c ..**.** If the l
e7f50 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 ocking level of
e7f60 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
e7f70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 tor is already a
e7f80 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 t or below.** th
e7f90 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b e requested lock
e7fa0 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 ing level, this
e7fb0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
e7fc0 6f 70 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 68 61 op..** .** If ha
e7fd0 6e 64 6c 65 4e 46 53 55 6e 6c 6f 63 6b 20 69 73 ndleNFSUnlock is
e7fe0 20 74 72 75 65 2c 20 74 68 65 6e 20 6f 6e 20 64 true, then on d
e7ff0 6f 77 6e 67 72 61 64 69 6e 67 20 61 6e 20 45 58 owngrading an EX
e8000 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 74 6f 20 CLUSIVE_LOCK to
e8010 53 48 41 52 45 44 0a 2a 2a 20 74 68 65 20 62 79 SHARED.** the by
e8020 74 65 20 72 61 6e 67 65 20 69 73 20 64 69 76 69 te range is divi
e8030 64 65 64 20 69 6e 74 6f 20 32 20 70 61 72 74 73 ded into 2 parts
e8040 20 61 6e 64 20 74 68 65 20 66 69 72 73 74 20 70 and the first p
e8050 61 72 74 20 69 73 20 75 6e 6c 6f 63 6b 65 64 20 art is unlocked
e8060 74 68 65 6e 0a 2a 2a 20 73 65 74 20 74 6f 20 61 then.** set to a
e8070 20 72 65 61 64 20 6c 6f 63 6b 2c 20 74 68 65 6e read lock, then
e8080 20 74 68 65 20 6f 74 68 65 72 20 70 61 72 74 20 the other part
e8090 69 73 20 73 69 6d 70 6c 79 20 75 6e 6c 6f 63 6b is simply unlock
e80a0 65 64 2e 20 20 54 68 69 73 20 77 6f 72 6b 73 20 ed. This works
e80b0 0a 2a 2a 20 61 72 6f 75 6e 64 20 61 20 62 75 67 .** around a bug
e80c0 20 69 6e 20 42 53 44 20 4e 46 53 20 6c 6f 63 6b in BSD NFS lock
e80d0 64 20 28 61 6c 73 6f 20 73 65 65 6e 20 6f 6e 20 d (also seen on
e80e0 4d 61 63 4f 53 58 20 31 30 2e 33 2b 29 20 74 68 MacOSX 10.3+) th
e80f0 61 74 20 66 61 69 6c 73 20 74 6f 20 0a 2a 2a 20 at fails to .**
e8100 72 65 6d 6f 76 65 20 74 68 65 20 77 72 69 74 65 remove the write
e8110 20 6c 6f 63 6b 20 6f 6e 20 61 20 72 65 67 69 6f lock on a regio
e8120 6e 20 77 68 65 6e 20 61 20 72 65 61 64 20 6c 6f n when a read lo
e8130 63 6b 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 ck is set..*/.st
e8140 61 74 69 63 20 69 6e 74 20 70 6f 73 69 78 55 6e atic int posixUn
e8150 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c lock(sqlite3_fil
e8160 65 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 e *id, int eFile
e8170 4c 6f 63 6b 2c 20 69 6e 74 20 68 61 6e 64 6c 65 Lock, int handle
e8180 4e 46 53 55 6e 6c 6f 63 6b 29 7b 0a 20 20 75 6e NFSUnlock){. un
e8190 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
e81a0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
e81b0 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a unixInodeInfo *
e81c0 70 49 6e 6f 64 65 3b 0a 20 20 73 74 72 75 63 74 pInode;. struct
e81d0 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 flock lock;. i
e81e0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
e81f0 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 K;.. assert( pF
e8200 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 ile );. OSTRACE
e8210 28 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 (("UNLOCK %d %d
e8220 20 77 61 73 20 25 64 28 25 64 2c 25 64 29 20 70 was %d(%d,%d) p
e8230 69 64 3d 25 64 20 28 75 6e 69 78 29 5c 6e 22 2c id=%d (unix)\n",
e8240 20 70 46 69 6c 65 2d 3e 68 2c 20 65 46 69 6c 65 pFile->h, eFile
e8250 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 70 46 69 6c Lock,. pFil
e8260 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 2c 20 70 46 e->eFileLock, pF
e8270 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 65 46 69 ile->pInode->eFi
e8280 6c 65 4c 6f 63 6b 2c 20 70 46 69 6c 65 2d 3e 70 leLock, pFile->p
e8290 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 2c 0a Inode->nShared,.
e82a0 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29 29 getpid()))
e82b0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 65 46 69 ;.. assert( eFi
e82c0 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 44 5f 4c leLock<=SHARED_L
e82d0 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 46 69 OCK );. if( pFi
e82e0 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3c 3d 65 le->eFileLock<=e
e82f0 46 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 FileLock ){.
e8300 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e8310 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 ;. }. unixEnte
e8320 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 49 6e 6f rMutex();. pIno
e8330 64 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f de = pFile->pIno
e8340 64 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 de;. assert( pI
e8350 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 21 3d 30 node->nShared!=0
e8360 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d );. if( pFile-
e8370 3e 65 46 69 6c 65 4c 6f 63 6b 3e 53 48 41 52 45 >eFileLock>SHARE
e8380 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 D_LOCK ){. as
e8390 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 65 46 sert( pInode->eF
e83a0 69 6c 65 4c 6f 63 6b 3d 3d 70 46 69 6c 65 2d 3e ileLock==pFile->
e83b0 65 46 69 6c 65 4c 6f 63 6b 20 29 3b 0a 0a 23 69 eFileLock );..#i
e83c0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
e83d0 47 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 72 65 G. /* When re
e83e0 64 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20 73 75 ducing a lock su
e83f0 63 68 20 74 68 61 74 20 6f 74 68 65 72 20 70 72 ch that other pr
e8400 6f 63 65 73 73 65 73 20 63 61 6e 20 73 74 61 72 ocesses can star
e8410 74 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 t. ** reading
e8420 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
e8430 6c 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65 20 73 le again, make s
e8440 75 72 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 ure that the.
e8450 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** transaction
e8460 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 counter was upda
e8470 74 65 64 20 69 66 20 61 6e 79 20 70 61 72 74 20 ted if any part
e8480 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a of the database.
e8490 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 68 61 6e ** file chan
e84a0 67 65 64 2e 20 20 49 66 20 74 68 65 20 74 72 61 ged. If the tra
e84b0 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 nsaction counter
e84c0 20 69 73 20 6e 6f 74 20 75 70 64 61 74 65 64 2c is not updated,
e84d0 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f . ** other co
e84e0 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65 nnections to the
e84f0 20 73 61 6d 65 20 66 69 6c 65 20 6d 69 67 68 74 same file might
e8500 20 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74 68 61 not realize tha
e8510 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6c t. ** the fil
e8520 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 61 6e e has changed an
e8530 64 20 68 65 6e 63 65 20 6d 69 67 68 74 20 6e 6f d hence might no
e8540 74 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 68 20 t know to flush
e8550 74 68 65 69 72 0a 20 20 20 20 2a 2a 20 63 61 63 their. ** cac
e8560 68 65 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 he. The use of
e8570 61 20 73 74 61 6c 65 20 63 61 63 68 65 20 63 61 a stale cache ca
e8580 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61 62 61 n lead to databa
e8590 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 se corruption..
e85a0 20 20 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d */. pFile-
e85b0 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d >inNormalWrite =
e85c0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 0;.#endif..
e85d0 2f 2a 20 64 6f 77 6e 67 72 61 64 69 6e 67 20 74 /* downgrading t
e85e0 6f 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 o a shared lock
e85f0 6f 6e 20 4e 46 53 20 69 6e 76 6f 6c 76 65 73 20 on NFS involves
e8600 63 6c 65 61 72 69 6e 67 20 74 68 65 20 77 72 69 clearing the wri
e8610 74 65 20 6c 6f 63 6b 0a 20 20 20 20 2a 2a 20 62 te lock. ** b
e8620 65 66 6f 72 65 20 65 73 74 61 62 6c 69 73 68 69 efore establishi
e8630 6e 67 20 74 68 65 20 72 65 61 64 6c 6f 63 6b 20 ng the readlock
e8640 2d 20 74 6f 20 61 76 6f 69 64 20 61 20 72 61 63 - to avoid a rac
e8650 65 20 63 6f 6e 64 69 74 69 6f 6e 20 77 65 20 64 e condition we d
e8660 6f 77 6e 67 72 61 64 65 0a 20 20 20 20 2a 2a 20 owngrade. **
e8670 74 68 65 20 6c 6f 63 6b 20 69 6e 20 32 20 62 6c the lock in 2 bl
e8680 6f 63 6b 73 2c 20 73 6f 20 74 68 61 74 20 70 61 ocks, so that pa
e8690 72 74 20 6f 66 20 74 68 65 20 72 61 6e 67 65 20 rt of the range
e86a0 77 69 6c 6c 20 62 65 20 63 6f 76 65 72 65 64 20 will be covered
e86b0 62 79 20 61 20 0a 20 20 20 20 2a 2a 20 77 72 69 by a . ** wri
e86c0 74 65 20 6c 6f 63 6b 20 75 6e 74 69 6c 20 74 68 te lock until th
e86d0 65 20 72 65 73 74 20 69 73 20 63 6f 76 65 72 65 e rest is covere
e86e0 64 20 62 79 20 61 20 72 65 61 64 20 6c 6f 63 6b d by a read lock
e86f0 3a 0a 20 20 20 20 2a 2a 20 20 31 3a 20 20 20 5b :. ** 1: [
e8700 57 57 57 57 57 5d 0a 20 20 20 20 2a 2a 20 20 32 WWWWW]. ** 2
e8710 3a 20 20 20 5b 2e 2e 2e 2e 57 5d 0a 20 20 20 20 : [....W].
e8720 2a 2a 20 20 33 3a 20 20 20 5b 52 52 52 52 57 5d ** 3: [RRRRW]
e8730 0a 20 20 20 20 2a 2a 20 20 34 3a 20 20 20 5b 52 . ** 4: [R
e8740 52 52 52 2e 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 RRR.]. */.
e8750 20 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d if( eFileLock==
e8760 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 0a SHARED_LOCK ){..
e8770 23 69 66 20 21 64 65 66 69 6e 65 64 28 5f 5f 41 #if !defined(__A
e8780 50 50 4c 45 5f 5f 29 20 7c 7c 20 21 53 51 4c 49 PPLE__) || !SQLI
e8790 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
e87a0 47 5f 53 54 59 4c 45 0a 20 20 20 20 20 20 28 76 G_STYLE. (v
e87b0 6f 69 64 29 68 61 6e 64 6c 65 4e 46 53 55 6e 6c oid)handleNFSUnl
e87c0 6f 63 6b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ock;. asser
e87d0 74 28 20 68 61 6e 64 6c 65 4e 46 53 55 6e 6c 6f t( handleNFSUnlo
e87e0 63 6b 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a ck==0 );.#endif.
e87f0 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 #if defined(__AP
e8800 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 PLE__) && SQLITE
e8810 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
e8820 53 54 59 4c 45 0a 20 20 20 20 20 20 69 66 28 20 STYLE. if(
e8830 68 61 6e 64 6c 65 4e 46 53 55 6e 6c 6f 63 6b 20 handleNFSUnlock
e8840 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74 ){. int t
e8850 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 Errno;
e8860 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f /* Error co
e8870 64 65 20 66 72 6f 6d 20 73 79 73 74 65 6d 20 63 de from system c
e8880 61 6c 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 all errors */.
e8890 20 20 20 20 20 20 6f 66 66 5f 74 20 64 69 76 53 off_t divS
e88a0 69 7a 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a ize = SHARED_SIZ
e88b0 45 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 0a E - 1;. .
e88c0 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 lock.l_t
e88d0 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 ype = F_UNLCK;.
e88e0 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 lock.l_wh
e88f0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b ence = SEEK_SET;
e8900 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f . lock.l_
e8910 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 start = SHARED_F
e8920 49 52 53 54 3b 0a 20 20 20 20 20 20 20 20 6c 6f IRST;. lo
e8930 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 64 69 76 53 69 ck.l_len = divSi
e8940 7a 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ze;. if(
e8950 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70 46 69 unixFileLock(pFi
e8960 6c 65 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31 29 le, &lock)==(-1)
e8970 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74 45 ){. tE
e8980 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
e8990 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
e89a0 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b ITE_IOERR_UNLOCK
e89b0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
e89c0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
e89d0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
e89e0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
e89f0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
e8a00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
e8a10 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 goto end_unloc
e8a20 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
e8a30 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 lock.l_type
e8a40 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 20 20 = F_RDLCK;.
e8a50 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 lock.l_whenc
e8a60 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 e = SEEK_SET;.
e8a70 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 lock.l_sta
e8a80 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 rt = SHARED_FIRS
e8a90 54 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e T;. lock.
e8aa0 6c 5f 6c 65 6e 20 3d 20 64 69 76 53 69 7a 65 3b l_len = divSize;
e8ab0 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 6e 69 . if( uni
e8ac0 78 46 69 6c 65 4c 6f 63 6b 28 70 46 69 6c 65 2c xFileLock(pFile,
e8ad0 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31 29 20 29 7b &lock)==(-1) ){
e8ae0 0a 20 20 20 20 20 20 20 20 20 20 74 45 72 72 6e . tErrn
e8af0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 o = errno;.
e8b00 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
e8b10 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
e8b20 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
e8b30 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 TE_IOERR_RDLOCK)
e8b40 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
e8b50 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
e8b60 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
e8b70 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
e8b80 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
e8b90 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
e8ba0 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 goto end_unloc
e8bb0 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
e8bc0 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 lock.l_type
e8bd0 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 = F_UNLCK;.
e8be0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 lock.l_whenc
e8bf0 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 e = SEEK_SET;.
e8c00 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 lock.l_sta
e8c10 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 rt = SHARED_FIRS
e8c20 54 2b 64 69 76 53 69 7a 65 3b 0a 20 20 20 20 20 T+divSize;.
e8c30 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 lock.l_len =
e8c40 53 48 41 52 45 44 5f 53 49 5a 45 2d 64 69 76 53 SHARED_SIZE-divS
e8c50 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ize;. if(
e8c60 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70 46 unixFileLock(pF
e8c70 69 6c 65 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31 ile, &lock)==(-1
e8c80 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74 ) ){. t
e8c90 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
e8ca0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
e8cb0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
e8cc0 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 K;. if(
e8cd0 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 IS_LOCK_ERROR(r
e8ce0 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 c) ){.
e8cf0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
e8d00 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
e8d10 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
e8d20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f goto end_unlo
e8d30 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ck;. }.
e8d40 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 }else.#endif
e8d50 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 /* defined(__AP
e8d60 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 PLE__) && SQLITE
e8d70 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
e8d80 53 54 59 4c 45 20 2a 2f 0a 20 20 20 20 20 20 7b STYLE */. {
e8d90 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f . lock.l_
e8da0 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a type = F_RDLCK;.
e8db0 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 lock.l_w
e8dc0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 hence = SEEK_SET
e8dd0 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c ;. lock.l
e8de0 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f _start = SHARED_
e8df0 46 49 52 53 54 3b 0a 20 20 20 20 20 20 20 20 6c FIRST;. l
e8e00 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 ock.l_len = SHAR
e8e10 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 ED_SIZE;.
e8e20 20 69 66 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63 if( unixFileLoc
e8e30 6b 28 70 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 20 k(pFile, &lock)
e8e40 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
e8e50 49 6e 20 74 68 65 6f 72 79 2c 20 74 68 65 20 63 In theory, the c
e8e60 61 6c 6c 20 74 6f 20 75 6e 69 78 46 69 6c 65 4c all to unixFileL
e8e70 6f 63 6b 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 ock() cannot fai
e8e80 6c 20 62 65 63 61 75 73 65 20 61 6e 6f 74 68 65 l because anothe
e8e90 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 r. ** p
e8ea0 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e rocess is holdin
e8eb0 67 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c g an incompatibl
e8ec0 65 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20 64 6f e lock. If it do
e8ed0 65 73 2c 20 74 68 69 73 20 0a 20 20 20 20 20 20 es, this .
e8ee0 20 20 20 20 2a 2a 20 69 6e 64 69 63 61 74 65 73 ** indicates
e8ef0 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 20 that the other
e8f00 70 72 6f 63 65 73 73 20 69 73 20 6e 6f 74 20 66 process is not f
e8f10 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 6f 63 ollowing the loc
e8f20 6b 69 6e 67 0a 20 20 20 20 20 20 20 20 20 20 2a king. *
e8f30 2a 20 70 72 6f 74 6f 63 6f 6c 2e 20 49 66 20 74 * protocol. If t
e8f40 68 69 73 20 68 61 70 70 65 6e 73 2c 20 72 65 74 his happens, ret
e8f50 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
e8f60 5f 52 44 4c 4f 43 4b 2e 20 52 65 74 75 72 6e 69 _RDLOCK. Returni
e8f70 6e 67 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 ng. **
e8f80 53 51 4c 49 54 45 5f 42 55 53 59 20 77 6f 75 6c SQLITE_BUSY woul
e8f90 64 20 63 6f 6e 66 75 73 65 20 74 68 65 20 75 70 d confuse the up
e8fa0 70 65 72 20 6c 61 79 65 72 20 28 69 6e 20 70 72 per layer (in pr
e8fb0 61 63 74 69 63 65 20 69 74 20 63 61 75 73 65 73 actice it causes
e8fc0 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 . ** a
e8fd0 6e 20 61 73 73 65 72 74 20 74 6f 20 66 61 69 6c n assert to fail
e8fe0 29 2e 20 2a 2f 20 0a 20 20 20 20 20 20 20 20 20 ). */ .
e8ff0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rc = SQLITE_IOE
e9000 52 52 5f 52 44 4c 4f 43 4b 3b 0a 20 20 20 20 20 RR_RDLOCK;.
e9010 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
e9020 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
e9030 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
e9040 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 d_unlock;.
e9050 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
e9060 7d 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 }. lock.l_typ
e9070 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 e = F_UNLCK;.
e9080 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d lock.l_whence =
e9090 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c SEEK_SET;. l
e90a0 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 ock.l_start = PE
e90b0 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 NDING_BYTE;.
e90c0 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b lock.l_len = 2L;
e90d0 20 20 61 73 73 65 72 74 28 20 50 45 4e 44 49 4e assert( PENDIN
e90e0 47 5f 42 59 54 45 2b 31 3d 3d 52 45 53 45 52 56 G_BYTE+1==RESERV
e90f0 45 44 5f 42 59 54 45 20 29 3b 0a 20 20 20 20 69 ED_BYTE );. i
e9100 66 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 f( unixFileLock(
e9110 70 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 3d 3d 30 pFile, &lock)==0
e9120 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 6f 64 65 ){. pInode
e9130 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 53 48 ->eFileLock = SH
e9140 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d ARED_LOCK;. }
e9150 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
e9160 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e SQLITE_IOERR_UN
e9170 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c LOCK;. pFil
e9180 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 e->lastErrno = e
e9190 72 72 6e 6f 3b 0a 20 20 20 20 20 20 67 6f 74 6f rrno;. goto
e91a0 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 end_unlock;.
e91b0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 65 46 69 }. }. if( eFi
e91c0 6c 65 4c 6f 63 6b 3d 3d 4e 4f 5f 4c 4f 43 4b 20 leLock==NO_LOCK
e91d0 29 7b 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d ){. /* Decrem
e91e0 65 6e 74 20 74 68 65 20 73 68 61 72 65 64 20 6c ent the shared l
e91f0 6f 63 6b 20 63 6f 75 6e 74 65 72 2e 20 20 52 65 ock counter. Re
e9200 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 20 75 lease the lock u
e9210 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 4f sing an. ** O
e9220 53 20 63 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 6e S call only when
e9230 20 61 6c 6c 20 74 68 72 65 61 64 73 20 69 6e 20 all threads in
e9240 74 68 69 73 20 73 61 6d 65 20 70 72 6f 63 65 73 this same proces
e9250 73 20 68 61 76 65 20 72 65 6c 65 61 73 65 64 0a s have released.
e9260 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e ** the lock.
e9270 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 6e 6f . */. pIno
e9280 64 65 2d 3e 6e 53 68 61 72 65 64 2d 2d 3b 0a 20 de->nShared--;.
e9290 20 20 20 69 66 28 20 70 49 6e 6f 64 65 2d 3e 6e if( pInode->n
e92a0 53 68 61 72 65 64 3d 3d 30 20 29 7b 0a 20 20 20 Shared==0 ){.
e92b0 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d lock.l_type =
e92c0 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 F_UNLCK;.
e92d0 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 lock.l_whence =
e92e0 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 SEEK_SET;.
e92f0 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 6c lock.l_start = l
e9300 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b 0a ock.l_len = 0L;.
e9310 20 20 20 20 20 20 69 66 28 20 75 6e 69 78 46 69 if( unixFi
e9320 6c 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 26 6c leLock(pFile, &l
e9330 6f 63 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ock)==0 ){.
e9340 20 20 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 pInode->eFile
e9350 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a Lock = NO_LOCK;.
e9360 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
e9370 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
e9380 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 _IOERR_UNLOCK;.
e9390 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
e93a0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
e93b0 0a 20 20 20 20 20 20 20 20 70 49 6e 6f 64 65 2d . pInode-
e93c0 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 4e 4f 5f >eFileLock = NO_
e93d0 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 70 46 LOCK;. pF
e93e0 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d ile->eFileLock =
e93f0 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 NO_LOCK;.
e9400 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
e9410 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f Decrement the co
e9420 75 6e 74 20 6f 66 20 6c 6f 63 6b 73 20 61 67 61 unt of locks aga
e9430 69 6e 73 74 20 74 68 69 73 20 73 61 6d 65 20 66 inst this same f
e9440 69 6c 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 20 ile. When the.
e9450 20 20 20 2a 2a 20 63 6f 75 6e 74 20 72 65 61 63 ** count reac
e9460 68 65 73 20 7a 65 72 6f 2c 20 63 6c 6f 73 65 20 hes zero, close
e9470 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65 20 64 any other file d
e9480 65 73 63 72 69 70 74 6f 72 73 20 77 68 6f 73 65 escriptors whose
e9490 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 77 61 close. ** wa
e94a0 73 20 64 65 66 65 72 72 65 64 20 62 65 63 61 75 s deferred becau
e94b0 73 65 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e se of outstandin
e94c0 67 20 6c 6f 63 6b 73 2e 0a 20 20 20 20 2a 2f 0a g locks.. */.
e94d0 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 pInode->nLoc
e94e0 6b 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 k--;. assert(
e94f0 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 3e 3d pInode->nLock>=
e9500 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 0 );. if( pIn
e9510 6f 64 65 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b ode->nLock==0 ){
e9520 0a 20 20 20 20 20 20 63 6c 6f 73 65 50 65 6e 64 . closePend
e9530 69 6e 67 46 64 73 28 70 46 69 6c 65 29 3b 0a 20 ingFds(pFile);.
e9540 20 20 20 7d 0a 20 20 7d 0a 0a 65 6e 64 5f 75 6e }. }..end_un
e9550 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 lock:. unixLeav
e9560 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 eMutex();. if(
e9570 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc==SQLITE_OK )
e9580 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b pFile->eFileLock
e9590 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 = eFileLock;.
e95a0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
e95b0 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
e95c0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
e95d0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 ile descriptor p
e95e0 46 69 6c 65 20 74 6f 20 65 46 69 6c 65 4c 6f 63 File to eFileLoc
e95f0 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 6b 0a 2a 2a k. eFileLock.**
e9600 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
e9610 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 NO_LOCK or SHARE
e9620 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 D_LOCK..**.** If
e9630 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
e9640 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 el of the file d
e9650 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 escriptor is alr
e9660 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 eady at or below
e9670 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 .** the requeste
e9680 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c d locking level,
e9690 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
e96a0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 a no-op..*/.sta
e96b0 74 69 63 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f tic int unixUnlo
e96c0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
e96d0 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f *id, int eFileLo
e96e0 63 6b 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 6f ck){. return po
e96f0 73 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 65 46 sixUnlock(id, eF
e9700 69 6c 65 4c 6f 63 6b 2c 20 30 29 3b 0a 7d 0a 0a ileLock, 0);.}..
e9710 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
e9720 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20 74 68 65 ion performs the
e9730 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 22 63 parts of the "c
e9740 6c 6f 73 65 20 66 69 6c 65 22 20 6f 70 65 72 61 lose file" opera
e9750 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 tion .** common
e9760 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 73 to all locking s
e9770 63 68 65 6d 65 73 2e 20 49 74 20 63 6c 6f 73 65 chemes. It close
e9780 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 s the directory
e9790 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 68 61 6e 64 and file.** hand
e97a0 6c 65 73 2c 20 69 66 20 74 68 65 79 20 61 72 65 les, if they are
e97b0 20 76 61 6c 69 64 2c 20 61 6e 64 20 73 65 74 73 valid, and sets
e97c0 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 all fields of t
e97d0 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 he unixFile.** s
e97e0 74 72 75 63 74 75 72 65 20 74 6f 20 30 2e 0a 2a tructure to 0..*
e97f0 2a 0a 2a 2a 20 49 74 20 69 73 20 2a 6e 6f 74 2a *.** It is *not*
e9800 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 68 6f necessary to ho
e9810 6c 64 20 74 68 65 20 6d 75 74 65 78 20 77 68 65 ld the mutex whe
e9820 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
e9830 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 65 76 65 s called,.** eve
e9840 6e 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 20 20 41 n on VxWorks. A
e9850 20 6d 75 74 65 78 20 77 69 6c 6c 20 62 65 20 61 mutex will be a
e9860 63 71 75 69 72 65 64 20 6f 6e 20 56 78 57 6f 72 cquired on VxWor
e9870 6b 73 20 62 79 20 74 68 65 0a 2a 2a 20 76 78 77 ks by the.** vxw
e9880 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 orksReleaseFileI
e9890 64 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a d() routine..*/.
e98a0 73 74 61 74 69 63 20 69 6e 74 20 63 6c 6f 73 65 static int close
e98b0 55 6e 69 78 46 69 6c 65 28 73 71 6c 69 74 65 33 UnixFile(sqlite3
e98c0 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 75 6e _file *id){. un
e98d0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
e98e0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
e98f0 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 if( pFile->h>=0
e9900 20 29 7b 0a 20 20 20 20 72 6f 62 75 73 74 5f 63 ){. robust_c
e9910 6c 6f 73 65 28 70 46 69 6c 65 2c 20 70 46 69 6c lose(pFile, pFil
e9920 65 2d 3e 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b e->h, __LINE__);
e9930 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 . pFile->h =
e9940 2d 31 3b 0a 20 20 7d 0a 23 69 66 20 4f 53 5f 56 -1;. }.#if OS_V
e9950 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 70 46 69 XWORKS. if( pFi
e9960 6c 65 2d 3e 70 49 64 20 29 7b 0a 20 20 20 20 69 le->pId ){. i
e9970 66 28 20 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c f( pFile->ctrlFl
e9980 61 67 73 20 26 20 55 4e 49 58 46 49 4c 45 5f 44 ags & UNIXFILE_D
e9990 45 4c 45 54 45 20 29 7b 0a 20 20 20 20 20 20 6f ELETE ){. o
e99a0 73 55 6e 6c 69 6e 6b 28 70 46 69 6c 65 2d 3e 70 sUnlink(pFile->p
e99b0 49 64 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 Id->zCanonicalNa
e99c0 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 76 me);. }. v
e99d0 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 6c xworksReleaseFil
e99e0 65 49 64 28 70 46 69 6c 65 2d 3e 70 49 64 29 3b eId(pFile->pId);
e99f0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 49 64 20 . pFile->pId
e9a00 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a = 0;. }.#endif.
e9a10 20 20 4f 53 54 52 41 43 45 28 28 22 43 4c 4f 53 OSTRACE(("CLOS
e9a20 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 E %-3d\n", pFi
e9a30 6c 65 2d 3e 68 29 29 3b 0a 20 20 4f 70 65 6e 43 le->h));. OpenC
e9a40 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 73 71 ounter(-1);. sq
e9a50 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 lite3_free(pFile
e9a60 2d 3e 70 55 6e 75 73 65 64 29 3b 0a 20 20 6d 65 ->pUnused);. me
e9a70 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 mset(pFile, 0, s
e9a80 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 izeof(unixFile))
e9a90 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
e9aa0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
e9ab0 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a lose a file..*/.
e9ac0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 static int unixC
e9ad0 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c lose(sqlite3_fil
e9ae0 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 e *id){. int rc
e9af0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
e9b00 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
e9b10 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 = (unixFile *)id
e9b20 3b 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 69 ;. unixUnlock(i
e9b30 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 75 d, NO_LOCK);. u
e9b40 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
e9b50 0a 0a 20 20 2f 2a 20 75 6e 69 78 46 69 6c 65 2e .. /* unixFile.
e9b60 70 49 6e 6f 64 65 20 69 73 20 61 6c 77 61 79 73 pInode is always
e9b70 20 76 61 6c 69 64 20 68 65 72 65 2e 20 4f 74 68 valid here. Oth
e9b80 65 72 77 69 73 65 2c 20 61 20 64 69 66 66 65 72 erwise, a differ
e9b90 65 6e 74 20 63 6c 6f 73 65 0a 20 20 2a 2a 20 72 ent close. ** r
e9ba0 6f 75 74 69 6e 65 20 28 65 2e 67 2e 20 6e 6f 6c outine (e.g. nol
e9bb0 6f 63 6b 43 6c 6f 73 65 28 29 29 20 77 6f 75 6c ockClose()) woul
e9bc0 64 20 62 65 20 63 61 6c 6c 65 64 20 69 6e 73 74 d be called inst
e9bd0 65 61 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ead.. */. asse
e9be0 72 74 28 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 rt( pFile->pInod
e9bf0 65 2d 3e 6e 4c 6f 63 6b 3e 30 20 7c 7c 20 70 46 e->nLock>0 || pF
e9c00 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 62 50 72 ile->pInode->bPr
e9c10 6f 63 65 73 73 4c 6f 63 6b 3d 3d 30 20 29 3b 0a ocessLock==0 );.
e9c20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 46 69 if( ALWAYS(pFi
e9c30 6c 65 2d 3e 70 49 6e 6f 64 65 29 20 26 26 20 70 le->pInode) && p
e9c40 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 6e 4c File->pInode->nL
e9c50 6f 63 6b 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 ock ){. /* If
e9c60 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 there are outst
e9c70 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f anding locks, do
e9c80 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c not actually cl
e9c90 6f 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73 ose the file jus
e9ca0 74 0a 20 20 20 20 2a 2a 20 79 65 74 20 62 65 63 t. ** yet bec
e9cb0 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 ause that would
e9cc0 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 6b clear those lock
e9cd0 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64 64 s. Instead, add
e9ce0 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a the file. **
e9cf0 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 70 descriptor to p
e9d00 49 6e 6f 64 65 2d 3e 70 55 6e 75 73 65 64 20 6c Inode->pUnused l
e9d10 69 73 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 ist. It will be
e9d20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 automatically c
e9d30 6c 6f 73 65 64 20 0a 20 20 20 20 2a 2a 20 77 68 losed . ** wh
e9d40 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b en the last lock
e9d50 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 is cleared..
e9d60 20 2a 2f 0a 20 20 20 20 73 65 74 50 65 6e 64 69 */. setPendi
e9d70 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a 20 20 7d ngFd(pFile);. }
e9d80 0a 20 20 72 65 6c 65 61 73 65 49 6e 6f 64 65 49 . releaseInodeI
e9d90 6e 66 6f 28 70 46 69 6c 65 29 3b 0a 20 20 72 63 nfo(pFile);. rc
e9da0 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 = closeUnixFile
e9db0 28 69 64 29 3b 0a 20 20 75 6e 69 78 4c 65 61 76 (id);. unixLeav
e9dc0 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 eMutex();. retu
e9dd0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a rn rc;.}../*****
e9de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
e9df0 20 74 68 65 20 70 6f 73 69 78 20 61 64 76 69 73 the posix advis
e9e00 6f 72 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 ory lock impleme
e9e10 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a ntation ********
e9e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
e9e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9e70 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a ********/../****
e9e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
e9ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d 6f 70 20 ********* No-op
e9ef0 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a Locking ********
e9f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
e9f20 20 4f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 Of the various
e9f30 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e locking implemen
e9f40 74 61 74 69 6f 6e 73 20 61 76 61 69 6c 61 62 6c tations availabl
e9f50 65 2c 20 74 68 69 73 20 69 73 20 62 79 20 66 61 e, this is by fa
e9f60 72 20 74 68 65 0a 2a 2a 20 73 69 6d 70 6c 65 73 r the.** simples
e9f70 74 3a 20 20 6c 6f 63 6b 69 6e 67 20 69 73 20 69 t: locking is i
e9f80 67 6e 6f 72 65 64 2e 20 20 4e 6f 20 61 74 74 65 gnored. No atte
e9f90 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6c mpt is made to l
e9fa0 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 ock the database
e9fb0 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 72 65 61 .** file for rea
e9fc0 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e ding or writing.
e9fd0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 63 6b .**.** This lock
e9fe0 69 6e 67 20 6d 6f 64 65 20 69 73 20 61 70 70 72 ing mode is appr
e9ff0 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 65 20 opriate for use
ea000 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 on read-only dat
ea010 61 62 61 73 65 73 0a 2a 2a 20 28 65 78 3a 20 64 abases.** (ex: d
ea020 61 74 61 62 61 73 65 73 20 74 68 61 74 20 61 72 atabases that ar
ea030 65 20 62 75 72 6e 65 64 20 69 6e 74 6f 20 43 44 e burned into CD
ea040 2d 52 4f 4d 2c 20 66 6f 72 20 65 78 61 6d 70 6c -ROM, for exampl
ea050 65 2e 29 20 20 49 74 20 63 61 6e 0a 2a 2a 20 61 e.) It can.** a
ea060 6c 73 6f 20 62 65 20 75 73 65 64 20 69 66 20 74 lso be used if t
ea070 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 65 he application e
ea080 6d 70 6c 6f 79 73 20 73 6f 6d 65 20 65 78 74 65 mploys some exte
ea090 72 6e 61 6c 20 6d 65 63 68 61 6e 69 73 6d 20 74 rnal mechanism t
ea0a0 6f 0a 2a 2a 20 70 72 65 76 65 6e 74 20 73 69 6d o.** prevent sim
ea0b0 75 6c 74 61 6e 65 6f 75 73 20 61 63 63 65 73 73 ultaneous access
ea0c0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 64 61 74 of the same dat
ea0d0 61 62 61 73 65 20 62 79 20 74 77 6f 20 6f 72 20 abase by two or
ea0e0 6d 6f 72 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 more.** database
ea0f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 42 connections. B
ea100 75 74 20 74 68 65 72 65 20 69 73 20 61 20 73 65 ut there is a se
ea110 72 69 6f 75 73 20 72 69 73 6b 20 6f 66 20 64 61 rious risk of da
ea120 74 61 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 tabase.** corrup
ea130 74 69 6f 6e 20 69 66 20 74 68 69 73 20 6c 6f 63 tion if this loc
ea140 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 75 73 65 king mode is use
ea150 64 20 69 6e 20 73 69 74 75 61 74 69 6f 6e 73 20 d in situations
ea160 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65 0a 2a where multiple.*
ea170 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
ea180 63 74 69 6f 6e 73 20 61 72 65 20 61 63 63 65 73 ctions are acces
ea190 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 sing the same da
ea1a0 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 tabase file at t
ea1b0 68 65 20 73 61 6d 65 0a 2a 2a 20 74 69 6d 65 20 he same.** time
ea1c0 61 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 and one or more
ea1d0 6f 66 20 74 68 6f 73 65 20 63 6f 6e 6e 65 63 74 of those connect
ea1e0 69 6f 6e 73 20 61 72 65 20 77 72 69 74 69 6e 67 ions are writing
ea1f0 2e 0a 2a 2f 0a 0a 73 74 61 74 69 63 20 69 6e 74 ..*/..static int
ea200 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 nolockCheckRese
ea210 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 rvedLock(sqlite3
ea220 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 _file *NotUsed,
ea230 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 int *pResOut){.
ea240 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
ea250 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 2a 70 R(NotUsed);. *p
ea260 52 65 73 4f 75 74 20 3d 20 30 3b 0a 20 20 72 65 ResOut = 0;. re
ea270 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
ea280 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c }.static int nol
ea290 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f ockLock(sqlite3_
ea2a0 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 file *NotUsed, i
ea2b0 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 nt NotUsed2){.
ea2c0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
ea2d0 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 2(NotUsed, NotUs
ea2e0 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 ed2);. return S
ea2f0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 QLITE_OK;.}.stat
ea300 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55 6e 6c ic int nolockUnl
ea310 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
ea320 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e *NotUsed, int N
ea330 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 otUsed2){. UNUS
ea340 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f ED_PARAMETER2(No
ea350 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 tUsed, NotUsed2)
ea360 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
ea370 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
ea380 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a lose the file..*
ea390 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c /.static int nol
ea3a0 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 ockClose(sqlite3
ea3b0 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 72 _file *id) {. r
ea3c0 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 eturn closeUnixF
ea3d0 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a 2a ile(id);.}../***
ea3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea3f0 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 6f 2d 6f End of the no-o
ea400 70 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 p lock implement
ea410 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ation **********
ea420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
ea430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a **********/../**
ea480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
ea4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea4e0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 64 6f 74 ****** Begin dot
ea4f0 2d 66 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a -file Locking **
ea500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
ea520 2a 2a 20 54 68 65 20 64 6f 74 66 69 6c 65 20 6c ** The dotfile l
ea530 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 ocking implement
ea540 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 65 ation uses the e
ea550 78 69 73 74 61 6e 63 65 20 6f 66 20 73 65 70 61 xistance of sepa
ea560 72 61 74 65 20 6c 6f 63 6b 0a 2a 2a 20 66 69 6c rate lock.** fil
ea570 65 73 20 28 72 65 61 6c 6c 79 20 61 20 64 69 72 es (really a dir
ea580 65 63 74 6f 72 79 29 20 74 6f 20 63 6f 6e 74 72 ectory) to contr
ea590 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 ol access to the
ea5a0 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 database. This
ea5b0 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 6a 75 73 works.** on jus
ea5c0 74 20 61 62 6f 75 74 20 65 76 65 72 79 20 66 69 t about every fi
ea5d0 6c 65 73 79 73 74 65 6d 20 69 6d 61 67 69 6e 61 lesystem imagina
ea5e0 62 6c 65 2e 20 20 42 75 74 20 74 68 65 72 65 20 ble. But there
ea5f0 61 72 65 20 73 65 72 69 6f 75 73 20 64 6f 77 6e are serious down
ea600 73 69 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 sides:.**.**
ea610 28 31 29 20 20 54 68 65 72 65 20 69 73 20 7a 65 (1) There is ze
ea620 72 6f 20 63 6f 6e 63 75 72 72 65 6e 63 79 2e 20 ro concurrency.
ea630 20 41 20 73 69 6e 67 6c 65 20 72 65 61 64 65 72 A single reader
ea640 20 62 6c 6f 63 6b 73 20 61 6c 6c 20 6f 74 68 65 blocks all othe
ea650 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e r.** con
ea660 6e 65 63 74 69 6f 6e 73 20 66 72 6f 6d 20 72 65 nections from re
ea670 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 ading or writing
ea680 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a the database..*
ea690 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 41 6e 20 *.** (2) An
ea6a0 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 72 61 73 application cras
ea6b0 68 20 6f 72 20 70 6f 77 65 72 20 6c 6f 73 73 20 h or power loss
ea6c0 63 61 6e 20 6c 65 61 76 65 20 73 74 61 6c 65 20 can leave stale
ea6d0 6c 6f 63 6b 20 66 69 6c 65 73 0a 2a 2a 20 20 20 lock files.**
ea6e0 20 20 20 20 20 20 73 69 74 74 69 6e 67 20 61 72 sitting ar
ea6f0 6f 75 6e 64 20 74 68 61 74 20 6e 65 65 64 20 74 ound that need t
ea700 6f 20 62 65 20 63 6c 65 61 72 65 64 20 6d 61 6e o be cleared man
ea710 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 65 76 ually..**.** Nev
ea720 65 72 74 68 65 6c 65 73 73 2c 20 61 20 64 6f 74 ertheless, a dot
ea730 6c 6f 63 6b 20 69 73 20 61 6e 20 61 70 70 72 6f lock is an appro
ea740 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 6d priate locking m
ea750 6f 64 65 20 66 6f 72 20 75 73 65 20 69 66 20 6e ode for use if n
ea760 6f 0a 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69 o.** other locki
ea770 6e 67 20 73 74 72 61 74 65 67 79 20 69 73 20 61 ng strategy is a
ea780 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 vailable..**.**
ea790 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 Dotfile locking
ea7a0 77 6f 72 6b 73 20 62 79 20 63 72 65 61 74 69 6e works by creatin
ea7b0 67 20 61 20 73 75 62 64 69 72 65 63 74 6f 72 79 g a subdirectory
ea7c0 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72 in the same dir
ea7d0 65 63 74 6f 72 79 20 61 73 0a 2a 2a 20 74 68 65 ectory as.** the
ea7e0 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 69 database and wi
ea7f0 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 th the same name
ea800 20 62 75 74 20 77 69 74 68 20 61 20 22 2e 6c 6f but with a ".lo
ea810 63 6b 22 20 65 78 74 65 6e 73 69 6f 6e 20 61 64 ck" extension ad
ea820 64 65 64 2e 0a 2a 2a 20 54 68 65 20 65 78 69 73 ded..** The exis
ea830 74 61 6e 63 65 20 6f 66 20 61 20 6c 6f 63 6b 20 tance of a lock
ea840 64 69 72 65 63 74 6f 72 79 20 69 6d 70 6c 69 65 directory implie
ea850 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c s an EXCLUSIVE l
ea860 6f 63 6b 2e 20 20 41 6c 6c 20 6f 74 68 65 72 0a ock. All other.
ea870 2a 2a 20 6c 6f 63 6b 20 74 79 70 65 73 20 28 53 ** lock types (S
ea880 48 41 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c HARED, RESERVED,
ea890 20 50 45 4e 44 49 4e 47 29 20 61 72 65 20 6d 61 PENDING) are ma
ea8a0 70 70 65 64 20 69 6e 74 6f 20 45 58 43 4c 55 53 pped into EXCLUS
ea8b0 49 56 45 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 IVE..*/../*.** T
ea8c0 68 65 20 66 69 6c 65 20 73 75 66 66 69 78 20 61 he file suffix a
ea8d0 64 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 dded to the data
ea8e0 20 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 69 base filename i
ea8f0 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74 n order to creat
ea900 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 64 69 e the.** lock di
ea910 72 65 63 74 6f 72 79 2e 0a 2a 2f 0a 23 64 65 66 rectory..*/.#def
ea920 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 ine DOTLOCK_SUFF
ea930 49 58 20 22 2e 6c 6f 63 6b 22 0a 0a 2f 2a 0a 2a IX ".lock"../*.*
ea940 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
ea950 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 hecks if there i
ea960 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 s a RESERVED loc
ea970 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 k held on the sp
ea980 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 ecified.** file
ea990 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f by this or any o
ea9a0 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 ther process. If
ea9b0 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 such a lock is
ea9c0 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f held, set *pResO
ea9d0 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a ut.** to a non-z
ea9e0 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 ero value otherw
ea9f0 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 ise *pResOut is
eaa00 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 set to zero. Th
eaa10 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a e return value.*
eaa20 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 * is set to SQLI
eaa30 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 TE_OK unless an
eaa40 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 I/O error occurs
eaa50 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 during lock che
eaa60 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 cking..**.** In
eaa70 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c dotfile locking,
eaa80 20 65 69 74 68 65 72 20 61 20 6c 6f 63 6b 20 65 either a lock e
eaa90 78 69 73 74 73 20 6f 72 20 69 74 20 64 6f 65 73 xists or it does
eaaa0 20 6e 6f 74 2e 20 20 53 6f 20 69 6e 20 74 68 69 not. So in thi
eaab0 73 0a 2a 2a 20 76 61 72 69 61 74 69 6f 6e 20 6f s.** variation o
eaac0 66 20 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c f CheckReservedL
eaad0 6f 63 6b 28 29 2c 20 2a 70 52 65 73 4f 75 74 20 ock(), *pResOut
eaae0 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69 is set to true i
eaaf0 66 20 61 6e 79 20 6c 6f 63 6b 0a 2a 2a 20 69 73 f any lock.** is
eab00 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 6c held on the fil
eab10 65 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 74 e and false if t
eab20 68 65 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 he file is unloc
eab30 6b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ked..*/.static i
eab40 6e 74 20 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 nt dotlockCheckR
eab50 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 eservedLock(sqli
eab60 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
eab70 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 20 t *pResOut) {.
eab80 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
eab90 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 OK;. int reserv
eaba0 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 ed = 0;. unixFi
eabb0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
eabc0 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69 xFile*)id;.. Si
eabd0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
eabe0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
eabf0 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 RR_CHECKRESERVED
eac00 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 LOCK; );. . as
eac10 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a sert( pFile );..
eac20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 /* Check if a
eac30 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 thread in this p
eac40 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 rocess holds suc
eac50 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 h a lock */. if
eac60 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f ( pFile->eFileLo
eac70 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 ck>SHARED_LOCK )
eac80 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 {. /* Either
eac90 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 this connection
eaca0 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f or some other co
eacb0 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 nnection in the
eacc0 73 61 6d 65 20 70 72 6f 63 65 73 73 0a 20 20 20 same process.
eacd0 20 2a 2a 20 68 6f 6c 64 73 20 61 20 6c 6f 63 6b ** holds a lock
eace0 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 4e on the file. N
eacf0 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 o need to check
ead00 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20 20 20 further. */.
ead10 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 reserved = 1;.
ead20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 }else{. /* Th
ead30 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 69 e lock is held i
ead40 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 f and only if th
ead50 65 20 6c 6f 63 6b 66 69 6c 65 20 65 78 69 73 74 e lockfile exist
ead60 73 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 s */. const c
ead70 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d har *zLockFile =
ead80 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 46 (const char*)pF
ead90 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
eada0 65 78 74 3b 0a 20 20 20 20 72 65 73 65 72 76 65 ext;. reserve
eadb0 64 20 3d 20 6f 73 41 63 63 65 73 73 28 7a 4c 6f d = osAccess(zLo
eadc0 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30 3b 0a 20 ckFile, 0)==0;.
eadd0 20 7d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 54 }. OSTRACE(("T
eade0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 EST WR-LOCK %d %
eadf0 64 20 25 64 20 28 64 6f 74 6c 6f 63 6b 29 5c 6e d %d (dotlock)\n
eae00 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c ", pFile->h, rc,
eae10 20 72 65 73 65 72 76 65 64 29 29 3b 0a 20 20 2a reserved));. *
eae20 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 pResOut = reserv
eae30 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ed;. return rc;
eae40 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 .}../*.** Lock t
eae50 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 he file with the
eae60 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 lock specified
eae70 62 79 20 70 61 72 61 6d 65 74 65 72 20 65 46 69 by parameter eFi
eae80 6c 65 4c 6f 63 6b 20 2d 20 6f 6e 65 0a 2a 2a 20 leLock - one.**
eae90 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
eaea0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 :.**.** (1)
eaeb0 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 SHARED_LOCK.**
eaec0 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f (2) RESERVED_
eaed0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 LOCK.** (3)
eaee0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 PENDING_LOCK.**
eaef0 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 (4) EXCLUSIV
eaf00 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d E_LOCK.**.** Som
eaf10 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 etimes when requ
eaf20 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 esting one lock
eaf30 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 state, additiona
eaf40 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a l lock states.**
eaf50 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e are inserted in
eaf60 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c between. The l
eaf70 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 ocking might fai
eaf80 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 l on one of the
eaf90 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 later.** transit
eafa0 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 ions leaving the
eafb0 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 lock state diff
eafc0 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 erent from what
eafd0 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a it started but.*
eafe0 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 * still short of
eaff0 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 its goal. The
eb000 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 following chart
eb010 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 shows the allowe
eb020 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 d.** transitions
eb030 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 and the inserte
eb040 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 d intermediate s
eb050 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 tates:.**.**
eb060 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 UNLOCKED -> SHAR
eb070 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 ED.** SHARED
eb080 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 -> RESERVED.**
eb090 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e SHARED -> (PEN
eb0a0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 DING) -> EXCLUSI
eb0b0 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 VE.** RESERVE
eb0c0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d D -> (PENDING) -
eb0d0 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 > EXCLUSIVE.**
eb0e0 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 PENDING -> EXC
eb0f0 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 LUSIVE.**.** Thi
eb100 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f s routine will o
eb110 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c nly increase a l
eb120 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 ock. Use the sq
eb130 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a lite3OsUnlock().
eb140 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f ** routine to lo
eb150 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 wer a locking le
eb160 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 vel..**.** With
eb170 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c dotfile locking,
eb180 20 77 65 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 20 we really only
eb190 73 75 70 70 6f 72 74 20 73 74 61 74 65 20 28 34 support state (4
eb1a0 29 3a 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2a ): EXCLUSIVE..**
eb1b0 20 42 75 74 20 77 65 20 74 72 61 63 6b 20 74 68 But we track th
eb1c0 65 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20 e other locking
eb1d0 6c 65 76 65 6c 73 20 69 6e 74 65 72 6e 61 6c 6c levels internall
eb1e0 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 y..*/.static int
eb1f0 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c dotlockLock(sql
eb200 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
eb210 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 20 7b 0a nt eFileLock) {.
eb220 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
eb230 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
eb240 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b d;. char *zLock
eb250 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 70 File = (char *)p
eb260 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
eb270 74 65 78 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d text;. int rc =
eb280 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 20 SQLITE_OK;...
eb290 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 61 6e /* If we have an
eb2a0 79 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 y lock, then the
eb2b0 20 6c 6f 63 6b 20 66 69 6c 65 20 61 6c 72 65 61 lock file alrea
eb2c0 64 79 20 65 78 69 73 74 73 2e 20 20 41 6c 6c 20 dy exists. All
eb2d0 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 74 6f 20 we have. ** to
eb2e0 64 6f 20 69 73 20 61 64 6a 75 73 74 20 6f 75 72 do is adjust our
eb2f0 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 internal record
eb300 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 6c 65 76 of the lock lev
eb310 65 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 el.. */. if( p
eb320 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 File->eFileLock
eb330 3e 20 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 > NO_LOCK ){.
eb340 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 pFile->eFileLoc
eb350 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 k = eFileLock;.
eb360 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 75 70 64 /* Always upd
eb370 61 74 65 20 74 68 65 20 74 69 6d 65 73 74 61 6d ate the timestam
eb380 70 20 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69 6c p on the old fil
eb390 65 20 2a 2f 0a 23 69 66 64 65 66 20 48 41 56 45 e */.#ifdef HAVE
eb3a0 5f 55 54 49 4d 45 0a 20 20 20 20 75 74 69 6d 65 _UTIME. utime
eb3b0 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e 55 4c 4c (zLockFile, NULL
eb3c0 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 75 74 69 );.#else. uti
eb3d0 6d 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e mes(zLockFile, N
eb3e0 55 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 ULL);.#endif.
eb3f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
eb400 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 K;. }. . /* g
eb410 72 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76 65 rab an exclusive
eb420 20 6c 6f 63 6b 20 2a 2f 0a 20 20 72 63 20 3d 20 lock */. rc =
eb430 6f 73 4d 6b 64 69 72 28 7a 4c 6f 63 6b 46 69 6c osMkdir(zLockFil
eb440 65 2c 20 30 37 37 37 29 3b 0a 20 20 69 66 28 20 e, 0777);. if(
eb450 72 63 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 66 rc<0 ){. /* f
eb460 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 2f 63 72 ailed to open/cr
eb470 65 61 74 65 20 74 68 65 20 6c 6f 63 6b 20 64 69 eate the lock di
eb480 72 65 63 74 6f 72 79 20 2a 2f 0a 20 20 20 20 69 rectory */. i
eb490 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
eb4a0 6f 3b 0a 20 20 20 20 69 66 28 20 45 45 58 49 53 o;. if( EEXIS
eb4b0 54 20 3d 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 T == tErrno ){.
eb4c0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
eb4d0 5f 42 55 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73 _BUSY;. } els
eb4e0 65 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 e {. rc = s
eb4f0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
eb500 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
eb510 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
eb520 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 CK);. if( I
eb530 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
eb540 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
eb550 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
eb560 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 Errno;. }.
eb570 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
eb580 72 63 3b 0a 20 20 7d 20 0a 20 20 0a 20 20 2f 2a rc;. } . . /*
eb590 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 got it, set the
eb5a0 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e type and return
eb5b0 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e ok */. pFile->
eb5c0 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c eFileLock = eFil
eb5d0 65 4c 6f 63 6b 3b 0a 20 20 72 65 74 75 72 6e 20 eLock;. return
eb5e0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 rc;.}../*.** Low
eb5f0 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c er the locking l
eb600 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 evel on file des
eb610 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f criptor pFile to
eb620 20 65 46 69 6c 65 4c 6f 63 6b 2e 20 20 65 46 69 eFileLock. eFi
eb630 6c 65 4c 6f 63 6b 0a 2a 2a 20 6d 75 73 74 20 62 leLock.** must b
eb640 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
eb650 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
eb660 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
eb670 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
eb680 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
eb690 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
eb6a0 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
eb6b0 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
eb6c0 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
eb6d0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
eb6e0 70 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 p..**.** When th
eb6f0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
eb700 72 65 61 63 68 65 73 20 4e 4f 5f 4c 4f 43 4b 2c reaches NO_LOCK,
eb710 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b delete the lock
eb720 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
eb730 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f int dotlockUnlo
eb740 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
eb750 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f *id, int eFileLo
eb760 63 6b 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 ck) {. unixFile
eb770 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
eb780 69 6c 65 2a 29 69 64 3b 0a 20 20 63 68 61 72 20 ile*)id;. char
eb790 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 *zLockFile = (ch
eb7a0 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b ar *)pFile->lock
eb7b0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e ingContext;. in
eb7c0 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
eb7d0 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 pFile );. OSTR
eb7e0 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 20 25 64 ACE(("UNLOCK %d
eb7f0 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 %d was %d pid=%
eb800 64 20 28 64 6f 74 6c 6f 63 6b 29 5c 6e 22 2c 20 d (dotlock)\n",
eb810 70 46 69 6c 65 2d 3e 68 2c 20 65 46 69 6c 65 4c pFile->h, eFileL
eb820 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ock,.
eb830 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b pFile->eFileLock
eb840 2c 20 67 65 74 70 69 64 28 29 29 29 3b 0a 20 20 , getpid()));.
eb850 61 73 73 65 72 74 28 20 65 46 69 6c 65 4c 6f 63 assert( eFileLoc
eb860 6b 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 k<=SHARED_LOCK )
eb870 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 ;. . /* no-op
eb880 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 if possible */.
eb890 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c if( pFile->eFil
eb8a0 65 4c 6f 63 6b 3d 3d 65 46 69 6c 65 4c 6f 63 6b eLock==eFileLock
eb8b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
eb8c0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 QLITE_OK;. }..
eb8d0 20 2f 2a 20 54 6f 20 64 6f 77 6e 67 72 61 64 65 /* To downgrade
eb8e0 20 74 6f 20 73 68 61 72 65 64 2c 20 73 69 6d 70 to shared, simp
eb8f0 6c 79 20 75 70 64 61 74 65 20 6f 75 72 20 69 6e ly update our in
eb900 74 65 72 6e 61 6c 20 6e 6f 74 69 6f 6e 20 6f 66 ternal notion of
eb910 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 73 the. ** lock s
eb920 74 61 74 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74 tate. No need t
eb930 6f 20 6d 65 73 73 20 77 69 74 68 20 74 68 65 20 o mess with the
eb940 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 file on disk..
eb950 2a 2f 0a 20 20 69 66 28 20 65 46 69 6c 65 4c 6f */. if( eFileLo
eb960 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ck==SHARED_LOCK
eb970 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 ){. pFile->eF
eb980 69 6c 65 4c 6f 63 6b 20 3d 20 53 48 41 52 45 44 ileLock = SHARED
eb990 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75 72 _LOCK;. retur
eb9a0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
eb9b0 0a 20 20 0a 20 20 2f 2a 20 54 6f 20 66 75 6c 6c . . /* To full
eb9c0 79 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 y unlock the dat
eb9d0 61 62 61 73 65 2c 20 64 65 6c 65 74 65 20 74 68 abase, delete th
eb9e0 65 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 e lock file */.
eb9f0 20 61 73 73 65 72 74 28 20 65 46 69 6c 65 4c 6f assert( eFileLo
eba00 63 6b 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 ck==NO_LOCK );.
eba10 20 72 63 20 3d 20 6f 73 52 6d 64 69 72 28 7a 4c rc = osRmdir(zL
eba20 6f 63 6b 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 ockFile);. if(
eba30 72 63 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d 45 rc<0 && errno==E
eba40 4e 4f 54 44 49 52 20 29 20 72 63 20 3d 20 6f 73 NOTDIR ) rc = os
eba50 55 6e 6c 69 6e 6b 28 7a 4c 6f 63 6b 46 69 6c 65 Unlink(zLockFile
eba60 29 3b 0a 20 20 69 66 28 20 72 63 3c 30 20 29 7b );. if( rc<0 ){
eba70 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 . int tErrno
eba80 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20 = errno;. rc
eba90 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 45 4e 4f = 0;. if( ENO
ebaa0 45 4e 54 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b ENT != tErrno ){
ebab0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
ebac0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b TE_IOERR_UNLOCK;
ebad0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 49 . }. if( I
ebae0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
ebaf0 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ){. pFile-
ebb00 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
ebb10 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 rno;. }. r
ebb20 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 eturn rc; . }.
ebb30 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 pFile->eFileLoc
ebb40 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 k = NO_LOCK;. r
ebb50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
ebb60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
ebb70 61 20 66 69 6c 65 2e 20 20 4d 61 6b 65 20 73 75 a file. Make su
ebb80 72 65 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 re the lock has
ebb90 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 62 65 been released be
ebba0 66 6f 72 65 20 63 6c 6f 73 69 6e 67 2e 0a 2a 2f fore closing..*/
ebbb0 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c .static int dotl
ebbc0 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 ockClose(sqlite3
ebbd0 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 _file *id) {. i
ebbe0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
ebbf0 4b 3b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 K;. if( id ){.
ebc00 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 unixFile *pFi
ebc10 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
ebc20 69 64 3b 0a 20 20 20 20 64 6f 74 6c 6f 63 6b 55 id;. dotlockU
ebc30 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 nlock(id, NO_LOC
ebc40 4b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f K);. sqlite3_
ebc50 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b free(pFile->lock
ebc60 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 ingContext);.
ebc70 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46 rc = closeUnixF
ebc80 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a 20 20 72 ile(id);. }. r
ebc90 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 2f 2a 2a 2a eturn rc;.}./***
ebca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
ebcb0 45 6e 64 20 6f 66 20 74 68 65 20 64 6f 74 2d 66 End of the dot-f
ebcc0 69 6c 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 ile lock impleme
ebcd0 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a ntation ********
ebce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
ebcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebd10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a **********/../**
ebd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
ebd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebda0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 6c ******* Begin fl
ebdb0 6f 63 6b 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a ock Locking ****
ebdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ebdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
ebde0 2a 2a 20 55 73 65 20 74 68 65 20 66 6c 6f 63 6b ** Use the flock
ebdf0 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 74 () system call t
ebe00 6f 20 64 6f 20 66 69 6c 65 20 6c 6f 63 6b 69 6e o do file lockin
ebe10 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 g..**.** flock()
ebe20 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b 65 locking is like
ebe30 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e dot-file lockin
ebe40 67 20 69 6e 20 74 68 61 74 20 74 68 65 20 76 61 g in that the va
ebe50 72 69 6f 75 73 0a 2a 2a 20 66 69 6e 65 2d 67 72 rious.** fine-gr
ebe60 61 69 6e 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 ain locking leve
ebe70 6c 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 ls supported by
ebe80 53 51 4c 69 74 65 20 61 72 65 20 63 6f 6c 6c 61 SQLite are colla
ebe90 70 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 20 73 psed into.** a s
ebea0 69 6e 67 6c 65 20 65 78 63 6c 75 73 69 76 65 20 ingle exclusive
ebeb0 6c 6f 63 6b 2e 20 20 49 6e 20 6f 74 68 65 72 20 lock. In other
ebec0 77 6f 72 64 73 2c 20 53 48 41 52 45 44 2c 20 52 words, SHARED, R
ebed0 45 53 45 52 56 45 44 2c 20 61 6e 64 0a 2a 2a 20 ESERVED, and.**
ebee0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 73 20 61 72 PENDING locks ar
ebef0 65 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 e the same thing
ebf00 20 61 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 as an EXCLUSIVE
ebf10 20 6c 6f 63 6b 2e 20 20 53 51 4c 69 74 65 0a 2a lock. SQLite.*
ebf20 2a 20 73 74 69 6c 6c 20 77 6f 72 6b 73 20 77 68 * still works wh
ebf30 65 6e 20 79 6f 75 20 64 6f 20 74 68 69 73 2c 20 en you do this,
ebf40 62 75 74 20 63 6f 6e 63 75 72 72 65 6e 63 79 20 but concurrency
ebf50 69 73 20 72 65 64 75 63 65 64 20 73 69 6e 63 65 is reduced since
ebf60 0a 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c .** only a singl
ebf70 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20 62 65 e process can be
ebf80 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 reading the dat
ebf90 61 62 61 73 65 20 61 74 20 61 20 74 69 6d 65 2e abase at a time.
ebfa0 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74 20 74 68 69 73 .**.** Omit this
ebfb0 20 73 65 63 74 69 6f 6e 20 69 66 20 53 51 4c 49 section if SQLI
ebfc0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
ebfd0 47 5f 53 54 59 4c 45 20 69 73 20 74 75 72 6e 65 G_STYLE is turne
ebfe0 64 20 6f 66 66 20 6f 72 20 69 66 0a 2a 2a 20 63 d off or if.** c
ebff0 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 56 58 57 ompiling for VXW
ec000 4f 52 4b 53 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c ORKS..*/.#if SQL
ec010 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
ec020 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f NG_STYLE && !OS_
ec030 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 52 VXWORKS../*.** R
ec040 65 74 72 79 20 66 6c 6f 63 6b 28 29 20 63 61 6c etry flock() cal
ec050 6c 73 20 74 68 61 74 20 66 61 69 6c 20 77 69 74 ls that fail wit
ec060 68 20 45 49 4e 54 52 0a 2a 2f 0a 23 69 66 64 65 h EINTR.*/.#ifde
ec070 66 20 45 49 4e 54 52 0a 73 74 61 74 69 63 20 69 f EINTR.static i
ec080 6e 74 20 72 6f 62 75 73 74 5f 66 6c 6f 63 6b 28 nt robust_flock(
ec090 69 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 29 7b int fd, int op){
ec0a0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 64 6f 7b . int rc;. do{
ec0b0 20 72 63 20 3d 20 66 6c 6f 63 6b 28 66 64 2c 6f rc = flock(fd,o
ec0c0 70 29 3b 20 7d 77 68 69 6c 65 28 20 72 63 3c 30 p); }while( rc<0
ec0d0 20 26 26 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 && errno==EINTR
ec0e0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b );. return rc;
ec0f0 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e .}.#else.# defin
ec100 65 20 72 6f 62 75 73 74 5f 66 6c 6f 63 6b 28 61 e robust_flock(a
ec110 2c 62 29 20 66 6c 6f 63 6b 28 61 2c 62 29 0a 23 ,b) flock(a,b).#
ec120 65 6e 64 69 66 0a 20 20 20 20 20 0a 0a 2f 2a 0a endif. ../*.
ec130 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
ec140 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 checks if there
ec150 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f is a RESERVED lo
ec160 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 ck held on the s
ec170 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 pecified.** file
ec180 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 by this or any
ec190 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 other process. I
ec1a0 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 f such a lock is
ec1b0 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 held, set *pRes
ec1c0 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d Out.** to a non-
ec1d0 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 zero value other
ec1e0 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 wise *pResOut is
ec1f0 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 set to zero. T
ec200 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a he return value.
ec210 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c ** is set to SQL
ec220 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e ITE_OK unless an
ec230 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 I/O error occur
ec240 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 s during lock ch
ec250 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 ecking..*/.stati
ec260 63 20 69 6e 74 20 66 6c 6f 63 6b 43 68 65 63 6b c int flockCheck
ec270 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c ReservedLock(sql
ec280 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
ec290 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 nt *pResOut){.
ec2a0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
ec2b0 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 OK;. int reserv
ec2c0 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 ed = 0;. unixFi
ec2d0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
ec2e0 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 xFile*)id;. .
ec2f0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
ec300 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
ec310 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 OERR_CHECKRESERV
ec320 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 EDLOCK; );. .
ec330 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
ec340 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 . . /* Check i
ec350 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 f a thread in th
ec360 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 is process holds
ec370 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a such a lock */.
ec380 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 if( pFile->eFi
ec390 6c 65 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f leLock>SHARED_LO
ec3a0 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 CK ){. reserv
ec3b0 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 ed = 1;. }. .
ec3c0 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 /* Otherwise se
ec3d0 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 e if some other
ec3e0 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 process holds it
ec3f0 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 . */. if( !rese
ec400 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 61 rved ){. /* a
ec410 74 74 65 6d 70 74 20 74 6f 20 67 65 74 20 74 68 ttempt to get th
ec420 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e e lock */. in
ec430 74 20 6c 72 63 20 3d 20 72 6f 62 75 73 74 5f 66 t lrc = robust_f
ec440 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c lock(pFile->h, L
ec450 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 OCK_EX | LOCK_NB
ec460 29 3b 0a 20 20 20 20 69 66 28 20 21 6c 72 63 20 );. if( !lrc
ec470 29 7b 0a 20 20 20 20 20 20 2f 2a 20 67 6f 74 20 ){. /* got
ec480 74 68 65 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b the lock, unlock
ec490 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 it */. lrc
ec4a0 20 3d 20 72 6f 62 75 73 74 5f 66 6c 6f 63 6b 28 = robust_flock(
ec4b0 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 pFile->h, LOCK_U
ec4c0 4e 29 3b 0a 20 20 20 20 20 20 69 66 20 28 20 6c N);. if ( l
ec4d0 72 63 20 29 20 7b 0a 20 20 20 20 20 20 20 20 69 rc ) {. i
ec4e0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
ec4f0 6f 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 6e o;. /* un
ec500 6c 6f 63 6b 20 66 61 69 6c 65 64 20 77 69 74 68 lock failed with
ec510 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 an error */.
ec520 20 20 20 20 20 6c 72 63 20 3d 20 53 51 4c 49 54 lrc = SQLIT
ec530 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 20 E_IOERR_UNLOCK;
ec540 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53 5f . if( IS_
ec550 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 LOCK_ERROR(lrc)
ec560 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 ){. pFi
ec570 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
ec580 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 tErrno;.
ec590 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 rc = lrc;.
ec5a0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
ec5b0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
ec5c0 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 int tErrno = er
ec5d0 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 73 65 72 rno;. reser
ec5e0 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f ved = 1;. /
ec5f0 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d * someone else m
ec600 69 67 68 74 20 68 61 76 65 20 69 74 20 72 65 73 ight have it res
ec610 65 72 76 65 64 20 2a 2f 0a 20 20 20 20 20 20 6c erved */. l
ec620 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
ec630 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
ec640 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
ec650 45 52 52 5f 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 ERR_LOCK); .
ec660 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
ec670 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 ROR(lrc) ){.
ec680 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
ec690 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
ec6a0 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 3b rc = lrc;
ec6b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
ec6c0 20 7d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 54 }. OSTRACE(("T
ec6d0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 EST WR-LOCK %d %
ec6e0 64 20 25 64 20 28 66 6c 6f 63 6b 29 5c 6e 22 2c d %d (flock)\n",
ec6f0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 pFile->h, rc, r
ec700 65 73 65 72 76 65 64 29 29 3b 0a 0a 23 69 66 64 eserved));..#ifd
ec710 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 ef SQLITE_IGNORE
ec720 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f _FLOCK_LOCK_ERRO
ec730 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 53 RS. if( (rc & S
ec740 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 QLITE_IOERR) ==
ec750 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a SQLITE_IOERR ){.
ec760 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
ec770 4f 4b 3b 0a 20 20 20 20 72 65 73 65 72 76 65 64 OK;. reserved
ec780 3d 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f =1;. }.#endif /
ec790 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f * SQLITE_IGNORE_
ec7a0 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 FLOCK_LOCK_ERROR
ec7b0 53 20 2a 2f 0a 20 20 2a 70 52 65 73 4f 75 74 20 S */. *pResOut
ec7c0 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 = reserved;. re
ec7d0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
ec7e0 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 * Lock the file
ec7f0 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 with the lock sp
ec800 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d ecified by param
ec810 65 74 65 72 20 65 46 69 6c 65 4c 6f 63 6b 20 2d eter eFileLock -
ec820 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 one.** of the f
ec830 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
ec840 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c (1) SHARED_L
ec850 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 OCK.** (2) R
ec860 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 ESERVED_LOCK.**
ec870 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f (3) PENDING_
ec880 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 LOCK.** (4)
ec890 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a EXCLUSIVE_LOCK.*
ec8a0 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 *.** Sometimes w
ec8b0 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f hen requesting o
ec8c0 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 ne lock state, a
ec8d0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 dditional lock s
ec8e0 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 tates.** are ins
ec8f0 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e erted in between
ec900 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d . The locking m
ec910 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 ight fail on one
ec920 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a of the later.**
ec930 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 transitions lea
ec940 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 ving the lock st
ec950 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 ate different fr
ec960 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 om what it start
ec970 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 ed but.** still
ec980 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 short of its goa
ec990 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e l. The followin
ec9a0 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 g chart shows th
ec9b0 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 e allowed.** tra
ec9c0 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nsitions and the
ec9d0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d inserted interm
ec9e0 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a ediate states:.*
ec9f0 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 *.** UNLOCKED
eca00 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 -> SHARED.**
eca10 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 SHARED -> RESER
eca20 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 VED.** SHARED
eca30 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
eca40 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
eca50 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 RESERVED -> (PE
eca60 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
eca70 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e IVE.** PENDIN
eca80 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a G -> EXCLUSIVE.*
eca90 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6f 6e 6c *.** flock() onl
ecaa0 79 20 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 y really support
ecab0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 EXCLUSIVE locks
ecac0 2e 20 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65 . We track inte
ecad0 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b rmediate.** lock
ecae0 20 73 74 61 74 65 73 20 69 6e 20 74 68 65 20 73 states in the s
ecaf0 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 qlite3_file stru
ecb00 63 74 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c cture, but all l
ecb10 6f 63 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a ocks SHARED or.*
ecb20 2a 20 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c * above are real
ecb30 6c 79 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 ly EXCLUSIVE loc
ecb40 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61 ks and exclude a
ecb50 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ll other process
ecb60 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 es from.** acces
ecb70 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a s the file..**.*
ecb80 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
ecb90 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 ill only increas
ecba0 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 e a lock. Use t
ecbb0 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f he sqlite3OsUnlo
ecbc0 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 ck().** routine
ecbd0 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 to lower a locki
ecbe0 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 ng level..*/.sta
ecbf0 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63 tic int flockLoc
ecc00 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
ecc10 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63 id, int eFileLoc
ecc20 6b 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 k) {. int rc =
ecc30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 SQLITE_OK;. uni
ecc40 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
ecc50 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 unixFile*)id;..
ecc60 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
ecc70 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c ;.. /* if we al
ecc80 72 65 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 ready have a loc
ecc90 6b 2c 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 k, it is exclusi
ecca0 76 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 ve. . ** Just
eccb0 61 64 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 adjust level and
eccc0 20 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 punt on outta h
eccd0 65 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 ere. */. if (pF
ecce0 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3e ile->eFileLock >
eccf0 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 NO_LOCK) {.
ecd00 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b pFile->eFileLock
ecd10 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 = eFileLock;.
ecd20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
ecd30 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 OK;. }. . /*
ecd40 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76 grab an exclusiv
ecd50 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 0a 20 20 69 e lock */. . i
ecd60 66 20 28 72 6f 62 75 73 74 5f 66 6c 6f 63 6b 28 f (robust_flock(
ecd70 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 pFile->h, LOCK_E
ecd80 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 29 20 7b 0a X | LOCK_NB)) {.
ecd90 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d int tErrno =
ecda0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 2f 2a 20 64 errno;. /* d
ecdb0 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73 74 20 idn't get, must
ecdc0 62 65 20 62 75 73 79 20 2a 2f 0a 20 20 20 20 72 be busy */. r
ecdd0 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
ecde0 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
ecdf0 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
ece00 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 RR_LOCK);. if
ece10 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
ece20 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 rc) ){. pFi
ece30 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
ece40 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 tErrno;. }.
ece50 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 } else {. /*
ece60 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 20 got it, set the
ece70 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20 type and return
ece80 6f 6b 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d ok */. pFile-
ece90 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 >eFileLock = eFi
ecea0 6c 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 4f 53 leLock;. }. OS
eceb0 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 20 20 20 TRACE(("LOCK
ecec0 25 64 20 25 73 20 25 73 20 28 66 6c 6f 63 6b 29 %d %s %s (flock)
eced0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 61 \n", pFile->h, a
ecee0 7a 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c zFileLock(eFileL
ecef0 6f 63 6b 29 2c 20 0a 20 20 20 20 20 20 20 20 20 ock), .
ecf00 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 rc==SQLITE_OK
ecf10 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 ? "ok" : "failed
ecf20 22 29 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 "));.#ifdef SQLI
ecf30 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f TE_IGNORE_FLOCK_
ecf40 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 LOCK_ERRORS. if
ecf50 28 20 28 72 63 20 26 20 53 51 4c 49 54 45 5f 49 ( (rc & SQLITE_I
ecf60 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f OERR) == SQLITE_
ecf70 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 72 63 20 IOERR ){. rc
ecf80 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
ecf90 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }.#endif /* SQL
ecfa0 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b ITE_IGNORE_FLOCK
ecfb0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a _LOCK_ERRORS */.
ecfc0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
ecfd0 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 ./*.** Lower the
ecfe0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
ecff0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
ed000 72 20 70 46 69 6c 65 20 74 6f 20 65 46 69 6c 65 r pFile to eFile
ed010 4c 6f 63 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 6b Lock. eFileLock
ed020 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 .** must be eith
ed030 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 er NO_LOCK or SH
ed040 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a ARED_LOCK..**.**
ed050 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 If the locking
ed060 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c level of the fil
ed070 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 e descriptor is
ed080 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 already at or be
ed090 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 low.** the reque
ed0a0 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 sted locking lev
ed0b0 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 el, this routine
ed0c0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
ed0d0 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b static int flock
ed0e0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
ed0f0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 ile *id, int eFi
ed100 6c 65 4c 6f 63 6b 29 20 7b 0a 20 20 75 6e 69 78 leLock) {. unix
ed110 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
ed120 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a nixFile*)id;. .
ed130 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
ed140 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 55 );. OSTRACE(("U
ed150 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 NLOCK %d %d was
ed160 20 25 64 20 70 69 64 3d 25 64 20 28 66 6c 6f 63 %d pid=%d (floc
ed170 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c k)\n", pFile->h,
ed180 20 65 46 69 6c 65 4c 6f 63 6b 2c 0a 20 20 20 20 eFileLock,.
ed190 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 pFile->eF
ed1a0 69 6c 65 4c 6f 63 6b 2c 20 67 65 74 70 69 64 28 ileLock, getpid(
ed1b0 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 )));. assert( e
ed1c0 46 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 44 FileLock<=SHARED
ed1d0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a _LOCK );. . /*
ed1e0 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 no-op if possib
ed1f0 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c le */. if( pFil
ed200 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 65 46 e->eFileLock==eF
ed210 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 72 ileLock ){. r
ed220 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
ed230 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 . }. . /* sha
ed240 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20 red can just be
ed250 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20 61 set because we a
ed260 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65 78 lways have an ex
ed270 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 clusive */. if
ed280 28 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 (eFileLock==SHAR
ed290 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 ED_LOCK) {. p
ed2a0 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 File->eFileLock
ed2b0 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 20 = eFileLock;.
ed2c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
ed2d0 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e K;. }. . /* n
ed2e0 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63 o, really, unloc
ed2f0 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 72 6f 62 75 k. */. if( robu
ed300 73 74 5f 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e st_flock(pFile->
ed310 68 2c 20 4c 4f 43 4b 5f 55 4e 29 20 29 7b 0a 23 h, LOCK_UN) ){.#
ed320 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e ifdef SQLITE_IGN
ed330 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 ORE_FLOCK_LOCK_E
ed340 52 52 4f 52 53 0a 20 20 20 20 72 65 74 75 72 6e RRORS. return
ed350 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 SQLITE_OK;.#end
ed360 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e if /* SQLITE_IGN
ed370 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 ORE_FLOCK_LOCK_E
ed380 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 72 65 74 RRORS */. ret
ed390 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
ed3a0 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 7d 65 6c 73 65 _UNLOCK;. }else
ed3b0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 {. pFile->eFi
ed3c0 6c 65 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b leLock = NO_LOCK
ed3d0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
ed3e0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f ITE_OK;. }.}../
ed3f0 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c *.** Close a fil
ed400 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
ed410 20 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 flockClose(sqli
ed420 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a te3_file *id) {.
ed430 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
ed440 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64 20 29 E_OK;. if( id )
ed450 7b 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 {. flockUnloc
ed460 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a k(id, NO_LOCK);.
ed470 20 20 20 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e rc = closeUn
ed480 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a ixFile(id);. }.
ed490 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
ed4a0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
ed4b0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
ed4c0 53 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 STYLE && !OS_VXW
ed4d0 4f 52 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a ORK */../*******
ed4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
ed4f0 20 6f 66 20 74 68 65 20 66 6c 6f 63 6b 20 6c 6f of the flock lo
ed500 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ck implementatio
ed510 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n **************
ed520 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
ed530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed570 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a ******/../******
ed580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed5c0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
ed5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed5e0 2a 20 42 65 67 69 6e 20 4e 61 6d 65 64 20 53 65 * Begin Named Se
ed5f0 6d 61 70 68 6f 72 65 20 4c 6f 63 6b 69 6e 67 20 maphore Locking
ed600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ed610 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e ********.**.** N
ed620 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c amed semaphore l
ed630 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 73 ocking is only s
ed640 75 70 70 6f 72 74 65 64 20 6f 6e 20 56 78 57 6f upported on VxWo
ed650 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 rks..**.** Semap
ed660 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 hore locking is
ed670 6c 69 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20 61 6e like dot-lock an
ed680 64 20 66 6c 6f 63 6b 20 69 6e 20 74 68 61 74 20 d flock in that
ed690 69 74 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 0a 2a it really only.*
ed6a0 2a 20 73 75 70 70 6f 72 74 73 20 45 58 43 4c 55 * supports EXCLU
ed6b0 53 49 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 20 4f SIVE locking. O
ed6c0 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f nly a single pro
ed6d0 63 65 73 73 20 63 61 6e 20 72 65 61 64 20 6f 72 cess can read or
ed6e0 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 64 61 write.** the da
ed6f0 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 61 tabase file at a
ed700 20 74 69 6d 65 2e 20 20 54 68 69 73 20 72 65 64 time. This red
ed710 75 63 65 73 20 70 6f 74 65 6e 74 69 61 6c 20 63 uces potential c
ed720 6f 6e 63 75 72 72 65 6e 63 79 2c 20 62 75 74 0a oncurrency, but.
ed730 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 6c 6f 63 ** makes the loc
ed740 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e k implementation
ed750 20 6d 75 63 68 20 65 61 73 69 65 72 2e 0a 2a 2f much easier..*/
ed760 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
ed770 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
ed780 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 ine checks if th
ed790 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 ere is a RESERVE
ed7a0 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 D lock held on t
ed7b0 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 he specified.**
ed7c0 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 file by this or
ed7d0 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 any other proces
ed7e0 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 s. If such a loc
ed7f0 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a k is held, set *
ed800 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 pResOut.** to a
ed810 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f non-zero value o
ed820 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 therwise *pResOu
ed830 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f t is set to zero
ed840 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 . The return va
ed850 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f lue.** is set to
ed860 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 SQLITE_OK unles
ed870 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f s an I/O error o
ed880 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 ccurs during loc
ed890 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 k checking..*/.s
ed8a0 74 61 74 69 63 20 69 6e 74 20 73 65 6d 43 68 65 tatic int semChe
ed8b0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
ed8c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
ed8d0 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b int *pResOut) {
ed8e0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
ed8f0 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 TE_OK;. int res
ed900 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 erved = 0;. uni
ed910 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
ed920 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 unixFile*)id;..
ed930 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
ed940 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
ed950 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 IOERR_CHECKRESER
ed960 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 VEDLOCK; );. .
ed970 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
ed980 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 ;.. /* Check if
ed990 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 a thread in thi
ed9a0 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 s process holds
ed9b0 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 such a lock */.
ed9c0 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c if( pFile->eFil
ed9d0 65 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 eLock>SHARED_LOC
ed9e0 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 K ){. reserve
ed9f0 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 d = 1;. }. .
eda00 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 /* Otherwise see
eda10 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 if some other p
eda20 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e rocess holds it.
eda30 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 */. if( !reser
eda40 76 65 64 20 29 7b 0a 20 20 20 20 73 65 6d 5f 74 ved ){. sem_t
eda50 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e *pSem = pFile->
eda60 70 49 6e 6f 64 65 2d 3e 70 53 65 6d 3b 0a 20 20 pInode->pSem;.
eda70 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 74 struct stat st
eda80 61 74 42 75 66 3b 0a 0a 20 20 20 20 69 66 28 20 atBuf;.. if(
eda90 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53 65 6d sem_trywait(pSem
edaa0 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 69 )==-1 ){. i
edab0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
edac0 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 45 41 47 o;. if( EAG
edad0 41 49 4e 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b AIN != tErrno ){
edae0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
edaf0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
edb00 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
edb10 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
edb20 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b CKRESERVEDLOCK);
edb30 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
edb40 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
edb50 6e 6f 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 no;. } else
edb60 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 73 6f {. /* so
edb70 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61 73 20 74 meone else has t
edb80 68 65 20 6c 6f 63 6b 20 77 68 65 6e 20 77 65 20 he lock when we
edb90 61 72 65 20 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a are in NO_LOCK *
edba0 2f 0a 20 20 20 20 20 20 20 20 72 65 73 65 72 76 /. reserv
edbb0 65 64 20 3d 20 28 70 46 69 6c 65 2d 3e 65 46 69 ed = (pFile->eFi
edbc0 6c 65 4c 6f 63 6b 20 3c 20 53 48 41 52 45 44 5f leLock < SHARED_
edbd0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 LOCK);. }.
edbe0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
edbf0 2f 2a 20 77 65 20 63 6f 75 6c 64 20 68 61 76 65 /* we could have
edc00 20 69 74 20 69 66 20 77 65 20 77 61 6e 74 20 69 it if we want i
edc10 74 20 2a 2f 0a 20 20 20 20 20 20 73 65 6d 5f 70 t */. sem_p
edc20 6f 73 74 28 70 53 65 6d 29 3b 0a 20 20 20 20 7d ost(pSem);. }
edc30 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 28 28 . }. OSTRACE((
edc40 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
edc50 20 25 64 20 25 64 20 28 73 65 6d 29 5c 6e 22 2c %d %d (sem)\n",
edc60 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 pFile->h, rc, r
edc70 65 73 65 72 76 65 64 29 29 3b 0a 0a 20 20 2a 70 eserved));.. *p
edc80 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 ResOut = reserve
edc90 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a d;. return rc;.
edca0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
edcb0 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
edcc0 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
edcd0 79 20 70 61 72 61 6d 65 74 65 72 20 65 46 69 6c y parameter eFil
edce0 65 4c 6f 63 6b 20 2d 20 6f 6e 65 0a 2a 2a 20 6f eLock - one.** o
edcf0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
edd00 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
edd10 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
edd20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
edd30 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
edd40 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
edd50 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
edd60 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
edd70 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
edd80 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
edd90 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
edda0 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
eddb0 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
eddc0 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
eddd0 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
edde0 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
eddf0 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
ede00 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
ede10 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
ede20 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
ede30 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
ede40 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
ede50 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
ede60 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
ede70 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
ede80 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
ede90 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
edea0 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
edeb0 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
edec0 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
eded0 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
edee0 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
edef0 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
edf00 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
edf10 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
edf20 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
edf30 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
edf40 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
edf50 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 USIVE.**.** Sema
edf60 70 68 6f 72 65 20 6c 6f 63 6b 73 20 6f 6e 6c 79 phore locks only
edf70 20 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 really support
edf80 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e EXCLUSIVE locks.
edf90 20 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65 72 We track inter
edfa0 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 mediate.** lock
edfb0 73 74 61 74 65 73 20 69 6e 20 74 68 65 20 73 71 states in the sq
edfc0 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 lite3_file struc
edfd0 74 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f ture, but all lo
edfe0 63 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a cks SHARED or.**
edff0 20 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c 6c above are reall
ee000 79 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b y EXCLUSIVE lock
ee010 73 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61 6c s and exclude al
ee020 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 l other processe
ee030 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 s from.** access
ee040 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a the file..**.**
ee050 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 This routine wi
ee060 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 ll only increase
ee070 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 a lock. Use th
ee080 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 e sqlite3OsUnloc
ee090 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 k().** routine t
ee0a0 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e o lower a lockin
ee0b0 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 g level..*/.stat
ee0c0 69 63 20 69 6e 74 20 73 65 6d 4c 6f 63 6b 28 73 ic int semLock(s
ee0d0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
ee0e0 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 20 int eFileLock)
ee0f0 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 {. unixFile *pF
ee100 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
ee110 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 )id;. int fd;.
ee120 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 sem_t *pSem = p
ee130 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 File->pInode->pS
ee140 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 em;. int rc = S
ee150 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 QLITE_OK;.. /*
ee160 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 68 61 if we already ha
ee170 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 ve a lock, it is
ee180 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 exclusive. .
ee190 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20 6c ** Just adjust l
ee1a0 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e evel and punt on
ee1b0 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a outta here. */.
ee1c0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 65 46 69 if (pFile->eFi
ee1d0 6c 65 4c 6f 63 6b 20 3e 20 4e 4f 5f 4c 4f 43 4b leLock > NO_LOCK
ee1e0 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 ) {. pFile->e
ee1f0 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 FileLock = eFile
ee200 4c 6f 63 6b 3b 0a 20 20 20 20 72 63 20 3d 20 53 Lock;. rc = S
ee210 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f QLITE_OK;. go
ee220 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b to sem_end_lock;
ee230 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f 63 . }. . /* loc
ee240 6b 20 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 20 k semaphore now
ee250 62 75 74 20 62 61 69 6c 20 6f 75 74 20 77 68 65 but bail out whe
ee260 6e 20 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 64 n already locked
ee270 2e 20 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f 74 . */. if( sem_t
ee280 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 rywait(pSem)==-1
ee290 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
ee2a0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f ITE_BUSY;. go
ee2b0 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b to sem_end_lock;
ee2c0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69 . }.. /* got i
ee2d0 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 t, set the type
ee2e0 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f and return ok */
ee2f0 0a 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c . pFile->eFileL
ee300 6f 63 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b ock = eFileLock;
ee310 0a 0a 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a .. sem_end_lock:
ee320 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
ee330 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 ./*.** Lower the
ee340 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
ee350 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
ee360 72 20 70 46 69 6c 65 20 74 6f 20 65 46 69 6c 65 r pFile to eFile
ee370 4c 6f 63 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 6b Lock. eFileLock
ee380 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 .** must be eith
ee390 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 er NO_LOCK or SH
ee3a0 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a ARED_LOCK..**.**
ee3b0 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 If the locking
ee3c0 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c level of the fil
ee3d0 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 e descriptor is
ee3e0 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 already at or be
ee3f0 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 low.** the reque
ee400 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 sted locking lev
ee410 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 el, this routine
ee420 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
ee430 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 55 6e static int semUn
ee440 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c lock(sqlite3_fil
ee450 65 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 e *id, int eFile
ee460 4c 6f 63 6b 29 20 7b 0a 20 20 75 6e 69 78 46 69 Lock) {. unixFi
ee470 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
ee480 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73 65 6d xFile*)id;. sem
ee490 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 _t *pSem = pFile
ee4a0 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 65 6d 3b 0a ->pInode->pSem;.
ee4b0 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
ee4c0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 );. assert( pS
ee4d0 65 6d 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 em );. OSTRACE(
ee4e0 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 ("UNLOCK %d %d
ee4f0 77 61 73 20 25 64 20 70 69 64 3d 25 64 20 28 73 was %d pid=%d (s
ee500 65 6d 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 em)\n", pFile->h
ee510 2c 20 65 46 69 6c 65 4c 6f 63 6b 2c 0a 20 20 20 , eFileLock,.
ee520 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 65 pFile->e
ee530 46 69 6c 65 4c 6f 63 6b 2c 20 67 65 74 70 69 64 FileLock, getpid
ee540 28 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ()));. assert(
ee550 65 46 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 eFileLock<=SHARE
ee560 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f D_LOCK );. . /
ee570 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 * no-op if possi
ee580 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 ble */. if( pFi
ee590 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 65 le->eFileLock==e
ee5a0 46 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 FileLock ){.
ee5b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
ee5c0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 ;. }. . /* sh
ee5d0 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65 ared can just be
ee5e0 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20 set because we
ee5f0 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65 always have an e
ee600 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 xclusive */. if
ee610 20 28 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 (eFileLock==SHA
ee620 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 RED_LOCK) {.
ee630 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b pFile->eFileLock
ee640 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 = eFileLock;.
ee650 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
ee660 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 OK;. }. . /*
ee670 6e 6f 2c 20 72 65 61 6c 6c 79 20 75 6e 6c 6f 63 no, really unloc
ee680 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 20 73 65 6d k. */. if ( sem
ee690 5f 70 6f 73 74 28 70 53 65 6d 29 3d 3d 2d 31 20 _post(pSem)==-1
ee6a0 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 63 2c 20 ) {. int rc,
ee6b0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
ee6c0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
ee6d0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
ee6e0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
ee6f0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b E_IOERR_UNLOCK);
ee700 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b . if( IS_LOCK
ee710 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
ee720 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
ee730 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 rrno = tErrno;.
ee740 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
ee750 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 rc; . }. pFile
ee760 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 4e 4f ->eFileLock = NO
ee770 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 _LOCK;. return
ee780 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
ee790 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c . ** Close a fil
ee7a0 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e e.. */.static in
ee7b0 74 20 73 65 6d 43 6c 6f 73 65 28 73 71 6c 69 74 t semClose(sqlit
ee7c0 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 e3_file *id) {.
ee7d0 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 if( id ){. u
ee7e0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
ee7f0 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
ee800 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b 28 69 64 semUnlock(id
ee810 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 , NO_LOCK);.
ee820 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
ee830 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 . unixEnterMu
ee840 74 65 78 28 29 3b 0a 20 20 20 20 72 65 6c 65 61 tex();. relea
ee850 73 65 49 6e 6f 64 65 49 6e 66 6f 28 70 46 69 6c seInodeInfo(pFil
ee860 65 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 e);. unixLeav
ee870 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 63 6c eMutex();. cl
ee880 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b oseUnixFile(id);
ee890 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
ee8a0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 LITE_OK;.}..#end
ee8b0 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 if /* OS_VXWORKS
ee8c0 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 64 20 */./*.** Named
ee8d0 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e semaphore lockin
ee8e0 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 g is only availa
ee8f0 62 6c 65 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a ble on VxWorks..
ee900 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
ee910 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 61 ** End of the na
ee920 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f med semaphore lo
ee930 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ck implementatio
ee940 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n **************
ee950 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
ee960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee9a0 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */.../**********
ee9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ee9f0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
eea00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eea10 20 42 65 67 69 6e 20 41 46 50 20 4c 6f 63 6b 69 Begin AFP Locki
eea20 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ng *************
eea30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eea40 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 46 50 20 69 ****.**.** AFP i
eea50 73 20 74 68 65 20 41 70 70 6c 65 20 46 69 6c 69 s the Apple Fili
eea60 6e 67 20 50 72 6f 74 6f 63 6f 6c 2e 20 20 41 46 ng Protocol. AF
eea70 50 20 69 73 20 61 20 6e 65 74 77 6f 72 6b 20 66 P is a network f
eea80 69 6c 65 73 79 73 74 65 6d 20 66 6f 75 6e 64 0a ilesystem found.
eea90 2a 2a 20 6f 6e 20 41 70 70 6c 65 20 4d 61 63 69 ** on Apple Maci
eeaa0 6e 74 6f 73 68 20 63 6f 6d 70 75 74 65 72 73 20 ntosh computers
eeab0 2d 20 62 6f 74 68 20 4f 53 39 20 61 6e 64 20 4f - both OS9 and O
eeac0 53 58 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 72 64 2d SX..**.** Third-
eead0 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 party implementa
eeae0 74 69 6f 6e 73 20 6f 66 20 41 46 50 20 61 72 65 tions of AFP are
eeaf0 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 42 75 74 available. But
eeb00 20 74 68 69 73 20 63 6f 64 65 20 68 65 72 65 0a this code here.
eeb10 2a 2a 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e ** only works on
eeb20 20 4f 53 58 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65 OSX..*/..#if de
eeb30 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
eeb40 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
eeb50 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
eeb60 2f 2a 0a 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63 /*.** The afpLoc
eeb70 6b 69 6e 67 43 6f 6e 74 65 78 74 20 73 74 72 75 kingContext stru
eeb80 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 cture contains a
eeb90 6c 6c 20 61 66 70 20 6c 6f 63 6b 20 73 70 65 63 ll afp lock spec
eeba0 69 66 69 63 20 73 74 61 74 65 0a 2a 2f 0a 74 79 ific state.*/.ty
eebb0 70 65 64 65 66 20 73 74 72 75 63 74 20 61 66 70 pedef struct afp
eebc0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61 LockingContext a
eebd0 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
eebe0 3b 0a 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b ;.struct afpLock
eebf0 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 69 ingContext {. i
eec00 6e 74 20 72 65 73 65 72 76 65 64 3b 0a 20 20 63 nt reserved;. c
eec10 6f 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61 74 onst char *dbPat
eec20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f h; /
eec30 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 * Name of the op
eec40 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 en file */.};..s
eec50 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c truct ByteRangeL
eec60 6f 63 6b 50 42 32 0a 7b 0a 20 20 75 6e 73 69 67 ockPB2.{. unsig
eec70 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 ned long long of
eec80 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 fset; /*
eec90 6f 66 66 73 65 74 20 74 6f 20 66 69 72 73 74 20 offset to first
eeca0 62 79 74 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a byte to lock */.
eecb0 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 unsigned long
eecc0 6c 6f 6e 67 20 6c 65 6e 67 74 68 3b 20 20 20 20 long length;
eecd0 20 20 20 20 2f 2a 20 6e 62 72 20 6f 66 20 62 79 /* nbr of by
eece0 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 tes to lock */.
eecf0 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c unsigned long l
eed00 6f 6e 67 20 72 65 74 52 61 6e 67 65 53 74 61 72 ong retRangeStar
eed10 74 3b 20 2f 2a 20 6e 62 72 20 6f 66 20 31 73 74 t; /* nbr of 1st
eed20 20 62 79 74 65 20 6c 6f 63 6b 65 64 20 69 66 20 byte locked if
eed30 73 75 63 63 65 73 73 66 75 6c 20 2a 2f 0a 20 20 successful */.
eed40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75 6e unsigned char un
eed50 4c 6f 63 6b 46 6c 61 67 3b 20 20 20 20 20 20 20 LockFlag;
eed60 20 20 2f 2a 20 31 20 3d 20 75 6e 6c 6f 63 6b 2c /* 1 = unlock,
eed70 20 30 20 3d 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 0 = lock */. u
eed80 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 74 61 nsigned char sta
eed90 72 74 45 6e 64 46 6c 61 67 3b 20 20 20 20 20 20 rtEndFlag;
eeda0 20 2f 2a 20 31 3d 72 65 6c 20 74 6f 20 65 6e 64 /* 1=rel to end
eedb0 20 6f 66 20 66 6f 72 6b 2c 20 30 3d 72 65 6c 20 of fork, 0=rel
eedc0 74 6f 20 73 74 61 72 74 20 2a 2f 0a 20 20 69 6e to start */. in
eedd0 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 t fd;
eede0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eedf0 2f 2a 20 66 69 6c 65 20 64 65 73 63 20 74 6f 20 /* file desc to
eee00 61 73 73 6f 63 20 74 68 69 73 20 6c 6f 63 6b 20 assoc this lock
eee10 77 69 74 68 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 with */.};..#def
eee20 69 6e 65 20 61 66 70 66 73 42 79 74 65 52 61 6e ine afpfsByteRan
eee30 67 65 4c 6f 63 6b 32 46 53 43 54 4c 20 20 20 20 geLock2FSCTL
eee40 20 20 20 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32 _IOWR('z', 2
eee50 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52 61 3, struct ByteRa
eee60 6e 67 65 4c 6f 63 6b 50 42 32 29 0a 0a 2f 2a 0a ngeLockPB2)../*.
eee70 2a 2a 20 54 68 69 73 20 69 73 20 61 20 75 74 69 ** This is a uti
eee80 6c 69 74 79 20 66 6f 72 20 73 65 74 74 69 6e 67 lity for setting
eee90 20 6f 72 20 63 6c 65 61 72 69 6e 67 20 61 20 62 or clearing a b
eeea0 69 74 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 6f 6e it-range lock on
eeeb0 20 61 6e 0a 2a 2a 20 41 46 50 20 66 69 6c 65 73 an.** AFP files
eeec0 79 73 74 65 6d 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 ystem..** .** Re
eeed0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f turn SQLITE_OK o
eeee0 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 n success, SQLIT
eeef0 45 5f 42 55 53 59 20 6f 6e 20 66 61 69 6c 75 72 E_BUSY on failur
eef00 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
eef10 20 61 66 70 53 65 74 4c 6f 63 6b 28 0a 20 20 63 afpSetLock(. c
eef20 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c onst char *path,
eef30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
eef40 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c Name of the fil
eef50 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 6f e to be locked o
eef60 72 20 75 6e 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 r unlocked */.
eef70 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c unixFile *pFile,
eef80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
eef90 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 * Open file desc
eefa0 72 69 70 74 6f 72 20 6f 6e 20 70 61 74 68 20 2a riptor on path *
eefb0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e /. unsigned lon
eefc0 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 2c 20 20 g long offset,
eefd0 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 /* First byte
eefe0 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f to be locked */
eeff0 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 . unsigned long
ef000 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 2c 20 20 20 long length,
ef010 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
ef020 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a ytes to lock */.
ef030 20 20 69 6e 74 20 73 65 74 4c 6f 63 6b 46 6c 61 int setLockFla
ef040 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g
ef050 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 65 74 20 /* True to set
ef060 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20 74 6f 20 lock. False to
ef070 63 6c 65 61 72 20 6c 6f 63 6b 20 2a 2f 0a 29 7b clear lock */.){
ef080 0a 20 20 73 74 72 75 63 74 20 42 79 74 65 52 61 . struct ByteRa
ef090 6e 67 65 4c 6f 63 6b 50 42 32 20 70 62 3b 0a 20 ngeLockPB2 pb;.
ef0a0 20 69 6e 74 20 65 72 72 3b 0a 20 20 0a 20 20 70 int err;. . p
ef0b0 62 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20 73 b.unLockFlag = s
ef0c0 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20 3a etLockFlag ? 0 :
ef0d0 20 31 3b 0a 20 20 70 62 2e 73 74 61 72 74 45 6e 1;. pb.startEn
ef0e0 64 46 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62 2e dFlag = 0;. pb.
ef0f0 6f 66 66 73 65 74 20 3d 20 6f 66 66 73 65 74 3b offset = offset;
ef100 0a 20 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20 6c . pb.length = l
ef110 65 6e 67 74 68 3b 20 0a 20 20 70 62 2e 66 64 20 ength; . pb.fd
ef120 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 0a 20 = pFile->h;. .
ef130 20 4f 53 54 52 41 43 45 28 28 22 41 46 50 53 45 OSTRACE(("AFPSE
ef140 54 4c 4f 43 4b 20 5b 25 73 5d 20 66 6f 72 20 25 TLOCK [%s] for %
ef150 64 25 73 20 69 6e 20 72 61 6e 67 65 20 25 6c 6c d%s in range %ll
ef160 78 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20 20 20 20 x:%llx\n", .
ef170 28 73 65 74 4c 6f 63 6b 46 6c 61 67 3f 22 4f 4e (setLockFlag?"ON
ef180 22 3a 22 4f 46 46 22 29 2c 20 70 46 69 6c 65 2d ":"OFF"), pFile-
ef190 3e 68 2c 20 28 70 62 2e 66 64 3d 3d 2d 31 3f 22 >h, (pb.fd==-1?"
ef1a0 5b 74 65 73 74 76 61 6c 2d 31 5d 22 3a 22 22 29 [testval-1]":"")
ef1b0 2c 0a 20 20 20 20 6f 66 66 73 65 74 2c 20 6c 65 ,. offset, le
ef1c0 6e 67 74 68 29 29 3b 0a 20 20 65 72 72 20 3d 20 ngth));. err =
ef1d0 66 73 63 74 6c 28 70 61 74 68 2c 20 61 66 70 66 fsctl(path, afpf
ef1e0 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 sByteRangeLock2F
ef1f0 53 43 54 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20 SCTL, &pb, 0);.
ef200 20 69 66 20 28 20 65 72 72 3d 3d 2d 31 20 29 20 if ( err==-1 )
ef210 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 {. int rc;.
ef220 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 int tErrno = e
ef230 72 72 6e 6f 3b 0a 20 20 20 20 4f 53 54 52 41 43 rrno;. OSTRAC
ef240 45 28 28 22 41 46 50 53 45 54 4c 4f 43 4b 20 66 E(("AFPSETLOCK f
ef250 61 69 6c 65 64 20 74 6f 20 66 73 63 74 6c 28 29 ailed to fsctl()
ef260 20 27 25 73 27 20 25 64 20 25 73 5c 6e 22 2c 0a '%s' %d %s\n",.
ef270 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74 pat
ef280 68 2c 20 74 45 72 72 6e 6f 2c 20 73 74 72 65 72 h, tErrno, strer
ef290 72 6f 72 28 74 45 72 72 6e 6f 29 29 29 3b 0a 23 ror(tErrno)));.#
ef2a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e ifdef SQLITE_IGN
ef2b0 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 ORE_AFP_LOCK_ERR
ef2c0 4f 52 53 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ORS. rc = SQL
ef2d0 49 54 45 5f 42 55 53 59 3b 0a 23 65 6c 73 65 0a ITE_BUSY;.#else.
ef2e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
ef2f0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
ef300 6f 72 28 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20 or(tErrno,.
ef310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
ef320 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 53 51 4c etLockFlag ? SQL
ef330 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 3a ITE_IOERR_LOCK :
ef340 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e SQLITE_IOERR_UN
ef350 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 20 2f 2a LOCK);.#endif /*
ef360 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 SQLITE_IGNORE_A
ef370 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a FP_LOCK_ERRORS *
ef380 2f 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 /. if( IS_LOC
ef390 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 K_ERROR(rc) ){.
ef3a0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
ef3b0 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
ef3c0 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
ef3d0 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a rc;. } else {.
ef3e0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
ef3f0 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a E_OK;. }.}../*.
ef400 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
ef410 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 checks if there
ef420 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f is a RESERVED lo
ef430 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 ck held on the s
ef440 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 pecified.** file
ef450 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 by this or any
ef460 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 other process. I
ef470 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 f such a lock is
ef480 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 held, set *pRes
ef490 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d Out.** to a non-
ef4a0 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 zero value other
ef4b0 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 wise *pResOut is
ef4c0 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 set to zero. T
ef4d0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a he return value.
ef4e0 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c ** is set to SQL
ef4f0 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e ITE_OK unless an
ef500 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 I/O error occur
ef510 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 s during lock ch
ef520 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 ecking..*/.stati
ef530 63 20 69 6e 74 20 61 66 70 43 68 65 63 6b 52 65 c int afpCheckRe
ef540 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 servedLock(sqlit
ef550 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
ef560 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e *pResOut){. in
ef570 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
ef580 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 ;. int reserved
ef590 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 = 0;. unixFile
ef5a0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
ef5b0 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f ile*)id;. afpLo
ef5c0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f ckingContext *co
ef5d0 6e 74 65 78 74 3b 0a 20 20 0a 20 20 53 69 6d 75 ntext;. . Simu
ef5e0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 lateIOError( ret
ef5f0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
ef600 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f _CHECKRESERVEDLO
ef610 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 CK; );. . asse
ef620 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 63 rt( pFile );. c
ef630 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 ontext = (afpLoc
ef640 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 kingContext *) p
ef650 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
ef660 74 65 78 74 3b 0a 20 20 69 66 28 20 63 6f 6e 74 text;. if( cont
ef670 65 78 74 2d 3e 72 65 73 65 72 76 65 64 20 29 7b ext->reserved ){
ef680 0a 20 20 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 . *pResOut =
ef690 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 1;. return SQ
ef6a0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 75 LITE_OK;. }. u
ef6b0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
ef6c0 20 2f 2a 20 42 65 63 61 75 73 65 20 70 46 69 6c /* Because pFil
ef6d0 65 2d 3e 70 49 6e 6f 64 65 20 69 73 20 73 68 61 e->pInode is sha
ef6e0 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 red across threa
ef6f0 64 73 20 2a 2f 0a 20 20 0a 20 20 2f 2a 20 43 68 ds */. . /* Ch
ef700 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 eck if a thread
ef710 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 in this process
ef720 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 holds such a loc
ef730 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 k */. if( pFile
ef740 2d 3e 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c ->pInode->eFileL
ef750 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ock>SHARED_LOCK
ef760 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 ){. reserved
ef770 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a = 1;. }. . /*
ef780 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 Otherwise see i
ef790 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f f some other pro
ef7a0 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 cess holds it..
ef7b0 20 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 */. if( !rese
ef7c0 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 6c rved ){. /* l
ef7d0 6f 63 6b 20 74 68 65 20 52 45 53 45 52 56 45 44 ock the RESERVED
ef7e0 20 62 79 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 byte */. int
ef7f0 20 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 lrc = afpSetLoc
ef800 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
ef810 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 h, pFile, RESERV
ef820 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 20 20 ED_BYTE, 1,1);
ef830 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f . if( SQLITE_
ef840 4f 4b 3d 3d 6c 72 63 20 29 7b 0a 20 20 20 20 20 OK==lrc ){.
ef850 20 2f 2a 20 69 66 20 77 65 20 73 75 63 63 65 65 /* if we succee
ef860 64 65 64 20 69 6e 20 74 61 6b 69 6e 67 20 74 68 ded in taking th
ef870 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2c e reserved lock,
ef880 20 75 6e 6c 6f 63 6b 20 69 74 20 74 6f 20 72 65 unlock it to re
ef890 73 74 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 74 store. ** t
ef8a0 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 he original stat
ef8b0 65 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d e */. lrc =
ef8c0 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 afpSetLock(cont
ef8d0 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 ext->dbPath, pFi
ef8e0 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 le, RESERVED_BYT
ef8f0 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 E, 1, 0);. }
ef900 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 2f 2a 20 else {. /*
ef910 69 66 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 if we failed to
ef920 67 65 74 20 74 68 65 20 6c 6f 63 6b 20 74 68 65 get the lock the
ef930 6e 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d n someone else m
ef940 75 73 74 20 68 61 76 65 20 69 74 20 2a 2f 0a 20 ust have it */.
ef950 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 reserved =
ef960 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 1;. }. if(
ef970 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c IS_LOCK_ERROR(l
ef980 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 3d rc) ){. rc=
ef990 6c 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 lrc;. }. }.
ef9a0 20 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 . unixLeaveMut
ef9b0 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 ex();. OSTRACE(
ef9c0 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 ("TEST WR-LOCK %
ef9d0 64 20 25 64 20 25 64 20 28 61 66 70 29 5c 6e 22 d %d %d (afp)\n"
ef9e0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 , pFile->h, rc,
ef9f0 72 65 73 65 72 76 65 64 29 29 3b 0a 20 20 0a 20 reserved));. .
efa00 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 *pResOut = rese
efa10 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 rved;. return r
efa20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b c;.}../*.** Lock
efa30 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 the file with t
efa40 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 he lock specifie
efa50 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 65 d by parameter e
efa60 46 69 6c 65 4c 6f 63 6b 20 2d 20 6f 6e 65 0a 2a FileLock - one.*
efa70 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * of the followi
efa80 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 ng:.**.** (1
efa90 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a ) SHARED_LOCK.**
efaa0 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 (2) RESERVE
efab0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 D_LOCK.** (3
efac0 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a ) PENDING_LOCK.*
efad0 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 * (4) EXCLUS
efae0 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 IVE_LOCK.**.** S
efaf0 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 ometimes when re
efb00 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 questing one loc
efb10 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f k state, additio
efb20 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a nal lock states.
efb30 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 ** are inserted
efb40 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 in between. The
efb50 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 locking might f
efb60 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 ail on one of th
efb70 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 e later.** trans
efb80 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 itions leaving t
efb90 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 he lock state di
efba0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 fferent from wha
efbb0 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 t it started but
efbc0 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 .** still short
efbd0 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 of its goal. Th
efbe0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 e following char
efbf0 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f t shows the allo
efc00 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f wed.** transitio
efc10 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 ns and the inser
efc20 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 ted intermediate
efc30 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 states:.**.**
efc40 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 UNLOCKED -> SH
efc50 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 ARED.** SHARE
efc60 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a D -> RESERVED.**
efc70 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 SHARED -> (P
efc80 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
efc90 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 SIVE.** RESER
efca0 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 VED -> (PENDING)
efcb0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
efcc0 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 PENDING -> E
efcd0 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 XCLUSIVE.**.** T
efce0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
efcf0 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 only increase a
efd00 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 lock. Use the
efd10 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 sqlite3OsUnlock(
efd20 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 ).** routine to
efd30 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 lower a locking
efd40 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 level..*/.static
efd50 20 69 6e 74 20 61 66 70 4c 6f 63 6b 28 73 71 6c int afpLock(sql
efd60 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
efd70 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 7b 0a 20 nt eFileLock){.
efd80 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
efd90 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 _OK;. unixFile
efda0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
efdb0 6c 65 2a 29 69 64 3b 0a 20 20 75 6e 69 78 49 6e le*)id;. unixIn
efdc0 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f 64 65 20 odeInfo *pInode
efdd0 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 3b = pFile->pInode;
efde0 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e . afpLockingCon
efdf0 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 text *context =
efe00 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 (afpLockingConte
efe10 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 xt *) pFile->loc
efe20 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a kingContext;. .
efe30 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
efe40 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 4c );. OSTRACE(("L
efe50 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 OCK %d %s was
efe60 20 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25 %s(%s,%d) pid=%
efe70 64 20 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c d (afp)\n", pFil
efe80 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 e->h,.
efe90 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c azFileLock(eFil
efea0 65 4c 6f 63 6b 29 2c 20 61 7a 46 69 6c 65 4c 6f eLock), azFileLo
efeb0 63 6b 28 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c ck(pFile->eFileL
efec0 6f 63 6b 29 2c 0a 20 20 20 20 20 20 20 20 20 20 ock),.
efed0 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 70 49 6e 6f azFileLock(pIno
efee0 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 29 2c 20 de->eFileLock),
efef0 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 20 pInode->nShared
eff00 2c 20 67 65 74 70 69 64 28 29 29 29 3b 0a 0a 20 , getpid()));..
eff10 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 /* If there is
eff20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f already a lock o
eff30 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d f this type or m
eff40 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 ore restrictive
eff50 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 on the. ** unix
eff60 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 File, do nothing
eff70 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 . Don't use the
eff80 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 afp_end_lock: ex
eff90 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a it path, as. **
effa0 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
effb0 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 ) hasn't been ca
effc0 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 lled yet.. */.
effd0 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c if( pFile->eFil
effe0 65 4c 6f 63 6b 3e 3d 65 46 69 6c 65 4c 6f 63 6b eLock>=eFileLock
efff0 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 28 ){. OSTRACE(
f0000 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 ("LOCK %d %s
f0010 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 ok (already held
f0020 29 20 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c ) (afp)\n", pFil
f0030 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 e->h,.
f0040 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c azFileLock(eFil
f0050 65 4c 6f 63 6b 29 29 29 3b 0a 20 20 20 20 72 65 eLock)));. re
f0060 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
f0070 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 }.. /* Make s
f0080 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ure the locking
f0090 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 sequence is corr
f00a0 65 63 74 0a 20 20 2a 2a 20 20 28 31 29 20 57 65 ect. ** (1) We
f00b0 20 6e 65 76 65 72 20 6d 6f 76 65 20 66 72 6f 6d never move from
f00c0 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 61 6e 79 unlocked to any
f00d0 74 68 69 6e 67 20 68 69 67 68 65 72 20 74 68 61 thing higher tha
f00e0 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 0a 20 n shared lock..
f00f0 20 2a 2a 20 20 28 32 29 20 53 51 4c 69 74 65 20 ** (2) SQLite
f0100 6e 65 76 65 72 20 65 78 70 6c 69 63 69 74 6c 79 never explicitly
f0110 20 72 65 71 75 65 73 74 73 20 61 20 70 65 6e 64 requests a pend
f0120 69 67 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 20 28 ig lock.. ** (
f0130 33 29 20 41 20 73 68 61 72 65 64 20 6c 6f 63 6b 3) A shared lock
f0140 20 69 73 20 61 6c 77 61 79 73 20 68 65 6c 64 20 is always held
f0150 77 68 65 6e 20 61 20 72 65 73 65 72 76 65 20 6c when a reserve l
f0160 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 ock is requested
f0170 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
f0180 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 pFile->eFileLoc
f0190 6b 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 65 46 k!=NO_LOCK || eF
f01a0 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f ileLock==SHARED_
f01b0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
f01c0 28 20 65 46 69 6c 65 4c 6f 63 6b 21 3d 50 45 4e ( eFileLock!=PEN
f01d0 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 DING_LOCK );. a
f01e0 73 73 65 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b ssert( eFileLock
f01f0 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 !=RESERVED_LOCK
f0200 7c 7c 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c || pFile->eFileL
f0210 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ock==SHARED_LOCK
f0220 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 );. . /* This
f0230 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64 mutex is needed
f0240 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e because pFile->
f0250 70 49 6e 6f 64 65 20 69 73 20 73 68 61 72 65 64 pInode is shared
f0260 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a across threads.
f0270 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 */. unixEnter
f0280 4d 75 74 65 78 28 29 3b 0a 20 20 70 49 6e 6f 64 Mutex();. pInod
f0290 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 e = pFile->pInod
f02a0 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 e;.. /* If some
f02b0 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 thread using th
f02c0 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63 is PID has a loc
f02d0 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65 6e k via a differen
f02e0 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a 2a t unixFile*. **
f02f0 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65 handle that pre
f0300 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 65 cludes the reque
f0310 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72 sted lock, retur
f0320 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 n BUSY.. */. i
f0330 66 28 20 28 70 46 69 6c 65 2d 3e 65 46 69 6c 65 f( (pFile->eFile
f0340 4c 6f 63 6b 21 3d 70 49 6e 6f 64 65 2d 3e 65 46 Lock!=pInode->eF
f0350 69 6c 65 4c 6f 63 6b 20 26 26 20 0a 20 20 20 20 ileLock && .
f0360 20 20 20 28 70 49 6e 6f 64 65 2d 3e 65 46 69 6c (pInode->eFil
f0370 65 4c 6f 63 6b 3e 3d 50 45 4e 44 49 4e 47 5f 4c eLock>=PENDING_L
f0380 4f 43 4b 20 7c 7c 20 65 46 69 6c 65 4c 6f 63 6b OCK || eFileLock
f0390 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 >SHARED_LOCK)).
f03a0 20 20 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ){. rc =
f03b0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
f03c0 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f goto afp_end_lo
f03d0 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 ck;. }. . /*
f03e0 49 66 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b If a SHARED lock
f03f0 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 61 is requested, a
f0400 6e 64 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75 nd some thread u
f0410 73 69 6e 67 20 74 68 69 73 20 50 49 44 20 61 6c sing this PID al
f0420 72 65 61 64 79 0a 20 20 2a 2a 20 68 61 73 20 61 ready. ** has a
f0430 20 53 48 41 52 45 44 20 6f 72 20 52 45 53 45 52 SHARED or RESER
f0440 56 45 44 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 69 VED lock, then i
f0450 6e 63 72 65 6d 65 6e 74 20 72 65 66 65 72 65 6e ncrement referen
f0460 63 65 20 63 6f 75 6e 74 73 20 61 6e 64 0a 20 20 ce counts and.
f0470 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ** return SQLITE
f0480 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 _OK.. */. if(
f0490 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 eFileLock==SHARE
f04a0 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20 20 20 20 D_LOCK && .
f04b0 28 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f (pInode->eFileLo
f04c0 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ck==SHARED_LOCK
f04d0 7c 7c 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 || pInode->eFile
f04e0 4c 6f 63 6b 3d 3d 52 45 53 45 52 56 45 44 5f 4c Lock==RESERVED_L
f04f0 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 65 OCK) ){. asse
f0500 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 rt( eFileLock==S
f0510 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
f0520 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
f0530 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 30 20 29 3b >eFileLock==0 );
f0540 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e . assert( pIn
f0550 6f 64 65 2d 3e 6e 53 68 61 72 65 64 3e 30 20 29 ode->nShared>0 )
f0560 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 ;. pFile->eFi
f0570 6c 65 4c 6f 63 6b 20 3d 20 53 48 41 52 45 44 5f leLock = SHARED_
f0580 4c 4f 43 4b 3b 0a 20 20 20 20 70 49 6e 6f 64 65 LOCK;. pInode
f0590 2d 3e 6e 53 68 61 72 65 64 2b 2b 3b 0a 20 20 20 ->nShared++;.
f05a0 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 2b 2b pInode->nLock++
f05b0 3b 0a 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 ;. goto afp_e
f05c0 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 20 nd_lock;. }.
f05d0 20 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 . /* A PENDING
f05e0 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20 lock is needed
f05f0 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67 before acquiring
f0600 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 a SHARED lock a
f0610 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 nd before. ** a
f0620 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c cquiring an EXCL
f0630 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 USIVE lock. For
f0640 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b the SHARED lock
f0650 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69 , the PENDING wi
f0660 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 ll. ** be relea
f0670 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 sed.. */. if(
f0680 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 eFileLock==SHARE
f0690 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c D_LOCK . ||
f06a0 20 28 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 (eFileLock==EXC
f06b0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 LUSIVE_LOCK && p
f06c0 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3c File->eFileLock<
f06d0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 PENDING_LOCK).
f06e0 29 7b 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 ){. int faile
f06f0 64 3b 0a 20 20 20 20 66 61 69 6c 65 64 20 3d 20 d;. failed =
f0700 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 afpSetLock(conte
f0710 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c xt->dbPath, pFil
f0720 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c e, PENDING_BYTE,
f0730 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 20 28 1, 1);. if (
f0740 66 61 69 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 failed) {.
f0750 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 rc = failed;.
f0760 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f goto afp_end_
f0770 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a lock;. }. }.
f0780 20 20 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 . /* If contr
f0790 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 69 73 20 ol gets to this
f07a0 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 point, then actu
f07b0 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e ally go ahead an
f07c0 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 d make. ** oper
f07d0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c ating system cal
f07e0 6c 73 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 ls for the speci
f07f0 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a fied lock.. */.
f0800 20 20 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d if( eFileLock=
f0810 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a =SHARED_LOCK ){.
f0820 20 20 20 20 69 6e 74 20 6c 72 63 31 2c 20 6c 72 int lrc1, lr
f0830 63 32 2c 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 c2, lrc1Errno =
f0840 30 3b 0a 20 20 20 20 6c 6f 6e 67 20 6c 6b 2c 20 0;. long lk,
f0850 6d 61 73 6b 3b 0a 20 20 20 20 0a 20 20 20 20 61 mask;. . a
f0860 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 6e ssert( pInode->n
f0870 53 68 61 72 65 64 3d 3d 30 20 29 3b 0a 20 20 20 Shared==0 );.
f0880 20 61 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d assert( pInode-
f0890 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 30 20 29 3b >eFileLock==0 );
f08a0 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 6d 61 . . ma
f08b0 73 6b 20 3d 20 28 73 69 7a 65 6f 66 28 6c 6f 6e sk = (sizeof(lon
f08c0 67 29 3d 3d 38 29 20 3f 20 4c 41 52 47 45 53 54 g)==8) ? LARGEST
f08d0 5f 49 4e 54 36 34 20 3a 20 30 78 37 66 66 66 66 _INT64 : 0x7ffff
f08e0 66 66 66 3b 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 fff;. /* Now
f08f0 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 get the read-loc
f0900 6b 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f k SHARED_LOCK */
f0910 0a 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61 . /* note tha
f0920 74 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f 66 t the quality of
f0930 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 the randomness
f0940 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 74 doesn't matter t
f0950 68 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20 hat much */.
f0960 6c 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a lk = random(); .
f0970 20 20 20 20 70 49 6e 6f 64 65 2d 3e 73 68 61 72 pInode->shar
f0980 65 64 42 79 74 65 20 3d 20 28 6c 6b 20 26 20 6d edByte = (lk & m
f0990 61 73 6b 29 25 28 53 48 41 52 45 44 5f 53 49 5a ask)%(SHARED_SIZ
f09a0 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72 63 31 E - 1);. lrc1
f09b0 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f = afpSetLock(co
f09c0 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 ntext->dbPath, p
f09d0 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 20 File, .
f09e0 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 70 49 SHARED_FIRST+pI
f09f0 6e 6f 64 65 2d 3e 73 68 61 72 65 64 42 79 74 65 node->sharedByte
f0a00 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 , 1, 1);. if(
f0a10 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c IS_LOCK_ERROR(l
f0a20 72 63 31 29 20 29 7b 0a 20 20 20 20 20 20 6c 72 rc1) ){. lr
f0a30 63 31 45 72 72 6e 6f 20 3d 20 70 46 69 6c 65 2d c1Errno = pFile-
f0a40 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 >lastErrno;.
f0a50 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 }. /* Drop th
f0a60 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 e temporary PEND
f0a70 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 ING lock */.
f0a80 6c 72 63 32 20 3d 20 61 66 70 53 65 74 4c 6f 63 lrc2 = afpSetLoc
f0a90 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
f0aa0 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e h, pFile, PENDIN
f0ab0 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 G_BYTE, 1, 0);.
f0ac0 20 20 20 0a 20 20 20 20 69 66 28 20 49 53 5f 4c . if( IS_L
f0ad0 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29 20 OCK_ERROR(lrc1)
f0ae0 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ) {. pFile-
f0af0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 72 63 >lastErrno = lrc
f0b00 31 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 1Errno;. rc
f0b10 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 20 20 67 = lrc1;. g
f0b20 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b oto afp_end_lock
f0b30 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28 ;. } else if(
f0b40 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c IS_LOCK_ERROR(l
f0b50 72 63 32 29 20 29 7b 0a 20 20 20 20 20 20 72 63 rc2) ){. rc
f0b60 20 3d 20 6c 72 63 32 3b 0a 20 20 20 20 20 20 67 = lrc2;. g
f0b70 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b oto afp_end_lock
f0b80 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28 ;. } else if(
f0b90 20 6c 72 63 31 20 21 3d 20 53 51 4c 49 54 45 5f lrc1 != SQLITE_
f0ba0 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 72 63 20 OK ) {. rc
f0bb0 3d 20 6c 72 63 31 3b 0a 20 20 20 20 7d 20 65 6c = lrc1;. } el
f0bc0 73 65 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 se {. pFile
f0bd0 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 53 48 ->eFileLock = SH
f0be0 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 ARED_LOCK;.
f0bf0 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 2b 2b pInode->nLock++
f0c00 3b 0a 20 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e ;. pInode->
f0c10 6e 53 68 61 72 65 64 20 3d 20 31 3b 0a 20 20 20 nShared = 1;.
f0c20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 65 }. }else if( e
f0c30 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 FileLock==EXCLUS
f0c40 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 49 6e 6f IVE_LOCK && pIno
f0c50 64 65 2d 3e 6e 53 68 61 72 65 64 3e 31 20 29 7b de->nShared>1 ){
f0c60 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 74 . /* We are t
f0c70 72 79 69 6e 67 20 66 6f 72 20 61 6e 20 65 78 63 rying for an exc
f0c80 6c 75 73 69 76 65 20 6c 6f 63 6b 20 62 75 74 20 lusive lock but
f0c90 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 another thread i
f0ca0 6e 20 74 68 69 73 0a 20 20 20 20 20 2a 2a 20 73 n this. ** s
f0cb0 61 6d 65 20 70 72 6f 63 65 73 73 20 69 73 20 73 ame process is s
f0cc0 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 20 73 till holding a s
f0cd0 68 61 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 hared lock. */.
f0ce0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
f0cf0 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 USY;. }else{.
f0d00 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 /* The request
f0d10 20 77 61 73 20 66 6f 72 20 61 20 52 45 53 45 52 was for a RESER
f0d20 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 VED or EXCLUSIVE
f0d30 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 lock. It is.
f0d40 20 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61 ** assumed tha
f0d50 74 20 74 68 65 72 65 20 69 73 20 61 20 53 48 41 t there is a SHA
f0d60 52 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c RED or greater l
f0d70 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a ock on the file.
f0d80 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a ** already..
f0d90 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66 */. int f
f0da0 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 61 ailed = 0;. a
f0db0 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d ssert( 0!=pFile-
f0dc0 3e 65 46 69 6c 65 4c 6f 63 6b 20 29 3b 0a 20 20 >eFileLock );.
f0dd0 20 20 69 66 20 28 65 46 69 6c 65 4c 6f 63 6b 20 if (eFileLock
f0de0 3e 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b >= RESERVED_LOCK
f0df0 20 26 26 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 && pFile->eFile
f0e00 4c 6f 63 6b 20 3c 20 52 45 53 45 52 56 45 44 5f Lock < RESERVED_
f0e10 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 20 20 LOCK) {.
f0e20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53 /* Acquire a RES
f0e30 45 52 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a 20 20 ERVED lock */.
f0e40 20 20 20 20 20 20 66 61 69 6c 65 64 20 3d 20 61 failed = a
f0e50 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 fpSetLock(contex
f0e60 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 t->dbPath, pFile
f0e70 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c , RESERVED_BYTE,
f0e80 20 31 2c 31 29 3b 0a 20 20 20 20 20 20 69 66 28 1,1);. if(
f0e90 20 21 66 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 !failed ){.
f0ea0 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 72 65 73 context->res
f0eb0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 erved = 1;.
f0ec0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 }. }. if
f0ed0 28 21 66 61 69 6c 65 64 20 26 26 20 65 46 69 6c (!failed && eFil
f0ee0 65 4c 6f 63 6b 20 3d 3d 20 45 58 43 4c 55 53 49 eLock == EXCLUSI
f0ef0 56 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 VE_LOCK) {.
f0f00 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 /* Acquire an E
f0f10 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a 2f XCLUSIVE lock */
f0f20 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 . .
f0f30 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 68 /* Remove the sh
f0f40 61 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72 65 ared lock before
f0f50 20 74 72 79 69 6e 67 20 74 68 65 20 72 61 6e 67 trying the rang
f0f60 65 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64 20 74 e. we'll need t
f0f70 6f 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 65 73 o . ** rees
f0f80 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72 tablish the shar
f0f90 65 64 20 6c 6f 63 6b 20 69 66 20 77 65 20 63 61 ed lock if we ca
f0fa0 6e 27 74 20 67 65 74 20 74 68 65 20 20 61 66 70 n't get the afp
f0fb0 55 6e 6c 6f 63 6b 0a 20 20 20 20 20 20 2a 2f 0a Unlock. */.
f0fc0 20 20 20 20 20 20 69 66 28 20 21 28 66 61 69 6c if( !(fail
f0fd0 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 ed = afpSetLock(
f0fe0 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c context->dbPath,
f0ff0 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 pFile, SHARED_F
f1000 49 52 53 54 20 2b 0a 20 20 20 20 20 20 20 20 20 IRST +.
f1010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f1020 70 49 6e 6f 64 65 2d 3e 73 68 61 72 65 64 42 79 pInode->sharedBy
f1030 74 65 2c 20 31 2c 20 30 29 29 20 29 7b 0a 20 20 te, 1, 0)) ){.
f1040 20 20 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 int failed
f1050 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 2 = SQLITE_OK;.
f1060 20 20 20 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74 /* now at
f1070 74 65 6d 6d 70 74 20 74 6f 20 67 65 74 20 74 68 temmpt to get th
f1080 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b e exclusive lock
f1090 20 72 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 range */.
f10a0 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 failed = afpSe
f10b0 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 tLock(context->d
f10c0 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 bPath, pFile, SH
f10d0 41 52 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 ARED_FIRST, .
f10e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f10f0 20 20 20 20 20 20 20 20 20 20 20 20 53 48 41 52 SHAR
f1100 45 44 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 ED_SIZE, 1);.
f1110 20 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 if( failed
f1120 26 26 20 28 66 61 69 6c 65 64 32 20 3d 20 61 66 && (failed2 = af
f1130 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 pSetLock(context
f1140 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c ->dbPath, pFile,
f1150 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
f1160 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f SHARED_
f1170 46 49 52 53 54 20 2b 20 70 49 6e 6f 64 65 2d 3e FIRST + pInode->
f1180 73 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 sharedByte, 1, 1
f1190 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 )) ){.
f11a0 2f 2a 20 43 61 6e 27 74 20 72 65 65 73 74 61 62 /* Can't reestab
f11b0 6c 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 lish the shared
f11c0 6c 6f 63 6b 2e 20 20 53 71 6c 69 74 65 20 63 61 lock. Sqlite ca
f11d0 6e 27 74 20 64 65 61 6c 2c 20 74 68 69 73 20 69 n't deal, this i
f11e0 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 s. ** a
f11f0 20 63 72 69 74 69 63 61 6c 20 49 2f 4f 20 65 72 critical I/O er
f1200 72 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2f ror. */
f1210 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
f1220 28 28 66 61 69 6c 65 64 20 26 20 53 51 4c 49 54 ((failed & SQLIT
f1230 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 E_IOERR) == SQLI
f1240 54 45 5f 49 4f 45 52 52 29 20 3f 20 66 61 69 6c TE_IOERR) ? fail
f1250 65 64 32 20 3a 20 0a 20 20 20 20 20 20 20 20 20 ed2 : .
f1260 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 SQLITE_IOE
f1270 52 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 RR_LOCK;.
f1280 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f goto afp_end_
f1290 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 20 lock;. }
f12a0 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
f12b0 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 rc = faile
f12c0 64 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 d; . }.
f12d0 7d 0a 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 }. if( failed
f12e0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 ){. rc = f
f12f0 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d ailed;. }. }
f1300 0a 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 . . if( rc==SQ
f1310 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 LITE_OK ){. p
f1320 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 File->eFileLock
f1330 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 20 = eFileLock;.
f1340 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f pInode->eFileLo
f1350 63 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a ck = eFileLock;.
f1360 20 20 7d 65 6c 73 65 20 69 66 28 20 65 46 69 6c }else if( eFil
f1370 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45 eLock==EXCLUSIVE
f1380 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 _LOCK ){. pFi
f1390 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 le->eFileLock =
f13a0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 PENDING_LOCK;.
f13b0 20 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c pInode->eFileL
f13c0 6f 63 6b 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f ock = PENDING_LO
f13d0 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70 5f 65 CK;. }. .afp_e
f13e0 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c nd_lock:. unixL
f13f0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f eaveMutex();. O
f1400 53 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 20 20 STRACE(("LOCK
f1410 20 25 64 20 25 73 20 25 73 20 28 61 66 70 29 5c %d %s %s (afp)\
f1420 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 61 7a n", pFile->h, az
f1430 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c 6f FileLock(eFileLo
f1440 63 6b 29 2c 20 0a 20 20 20 20 20 20 20 20 20 72 ck), . r
f1450 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 c==SQLITE_OK ? "
f1460 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 ok" : "failed"))
f1470 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
f1480 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 ../*.** Lower th
f1490 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
f14a0 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 on file descript
f14b0 6f 72 20 70 46 69 6c 65 20 74 6f 20 65 46 69 6c or pFile to eFil
f14c0 65 4c 6f 63 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 eLock. eFileLoc
f14d0 6b 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 k.** must be eit
f14e0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 her NO_LOCK or S
f14f0 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a HARED_LOCK..**.*
f1500 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 * If the locking
f1510 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 level of the fi
f1520 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
f1530 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 already at or b
f1540 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 elow.** the requ
f1550 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 ested locking le
f1560 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e vel, this routin
f1570 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f e is a no-op..*/
f1580 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 55 .static int afpU
f1590 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 nlock(sqlite3_fi
f15a0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c le *id, int eFil
f15b0 65 4c 6f 63 6b 29 20 7b 0a 20 20 69 6e 74 20 72 eLock) {. int r
f15c0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
f15d0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
f15e0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
f15f0 3b 0a 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 ;. unixInodeInf
f1600 6f 20 2a 70 49 6e 6f 64 65 3b 0a 20 20 61 66 70 o *pInode;. afp
f1610 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
f1620 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f context = (afpLo
f1630 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 ckingContext *)
f1640 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
f1650 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20 73 6b 69 ntext;. int ski
f1660 70 53 68 61 72 65 64 20 3d 20 30 3b 0a 23 69 66 pShared = 0;.#if
f1670 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
f1680 20 20 69 6e 74 20 68 20 3d 20 70 46 69 6c 65 2d int h = pFile-
f1690 3e 68 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 >h;.#endif.. as
f16a0 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 sert( pFile );.
f16b0 20 4f 53 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 OSTRACE(("UNLOC
f16c0 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 28 K %d %d was %d(
f16d0 25 64 2c 25 64 29 20 70 69 64 3d 25 64 20 28 61 %d,%d) pid=%d (a
f16e0 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 fp)\n", pFile->h
f16f0 2c 20 65 46 69 6c 65 4c 6f 63 6b 2c 0a 20 20 20 , eFileLock,.
f1700 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 65 pFile->e
f1710 46 69 6c 65 4c 6f 63 6b 2c 20 70 46 69 6c 65 2d FileLock, pFile-
f1720 3e 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f >pInode->eFileLo
f1730 63 6b 2c 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 ck, pFile->pInod
f1740 65 2d 3e 6e 53 68 61 72 65 64 2c 0a 20 20 20 20 e->nShared,.
f1750 20 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29 getpid())
f1760 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 65 46 );.. assert( eF
f1770 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 44 5f ileLock<=SHARED_
f1780 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 46 LOCK );. if( pF
f1790 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3c 3d ile->eFileLock<=
f17a0 65 46 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 eFileLock ){.
f17b0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
f17c0 4b 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e 74 K;. }. unixEnt
f17d0 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 49 6e erMutex();. pIn
f17e0 6f 64 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e ode = pFile->pIn
f17f0 6f 64 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ode;. assert( p
f1800 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 21 3d Inode->nShared!=
f1810 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 0 );. if( pFile
f1820 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3e 53 48 41 52 ->eFileLock>SHAR
f1830 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 ED_LOCK ){. a
f1840 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 65 ssert( pInode->e
f1850 46 69 6c 65 4c 6f 63 6b 3d 3d 70 46 69 6c 65 2d FileLock==pFile-
f1860 3e 65 46 69 6c 65 4c 6f 63 6b 20 29 3b 0a 20 20 >eFileLock );.
f1870 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
f1880 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 rBenign(1);.
f1890 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
f18a0 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 53 69 h=(-1) ). Si
f18b0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
f18c0 69 67 6e 28 30 29 3b 0a 20 20 20 20 0a 23 69 66 ign(0);. .#if
f18d0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
f18e0 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 72 65 64 . /* When red
f18f0 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20 73 75 63 ucing a lock suc
f1900 68 20 74 68 61 74 20 6f 74 68 65 72 20 70 72 6f h that other pro
f1910 63 65 73 73 65 73 20 63 61 6e 20 73 74 61 72 74 cesses can start
f1920 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 . ** reading
f1930 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
f1940 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65 20 73 75 e again, make su
f1950 72 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 re that the.
f1960 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 ** transaction c
f1970 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 74 ounter was updat
f1980 65 64 20 69 66 20 61 6e 79 20 70 61 72 74 20 6f ed if any part o
f1990 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 f the database.
f19a0 20 20 20 2a 2a 20 66 69 6c 65 20 63 68 61 6e 67 ** file chang
f19b0 65 64 2e 20 20 49 66 20 74 68 65 20 74 72 61 6e ed. If the tran
f19c0 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 saction counter
f19d0 69 73 20 6e 6f 74 20 75 70 64 61 74 65 64 2c 0a is not updated,.
f19e0 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e ** other con
f19f0 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 nections to the
f1a00 73 61 6d 65 20 66 69 6c 65 20 6d 69 67 68 74 20 same file might
f1a10 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74 68 61 74 not realize that
f1a20 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6c 65 . ** the file
f1a30 20 68 61 73 20 63 68 61 6e 67 65 64 20 61 6e 64 has changed and
f1a40 20 68 65 6e 63 65 20 6d 69 67 68 74 20 6e 6f 74 hence might not
f1a50 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 68 20 74 know to flush t
f1a60 68 65 69 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 heir. ** cach
f1a70 65 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 61 e. The use of a
f1a80 20 73 74 61 6c 65 20 63 61 63 68 65 20 63 61 6e stale cache can
f1a90 20 6c 65 61 64 20 74 6f 20 64 61 74 61 62 61 73 lead to databas
f1aa0 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 e corruption..
f1ab0 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
f1ac0 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c pFile->inNormal
f1ad0 57 72 69 74 65 3d 3d 30 0a 20 20 20 20 20 20 20 Write==0.
f1ae0 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 64 62 || pFile->db
f1af0 55 70 64 61 74 65 3d 3d 30 0a 20 20 20 20 20 20 Update==0.
f1b00 20 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 74 || pFile->t
f1b10 72 61 6e 73 43 6e 74 72 43 68 6e 67 3d 3d 31 20 ransCntrChng==1
f1b20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e );. pFile->in
f1b30 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 30 3b NormalWrite = 0;
f1b40 0a 23 65 6e 64 69 66 0a 20 20 20 20 0a 20 20 20 .#endif. .
f1b50 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c if( pFile->eFil
f1b60 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45 eLock==EXCLUSIVE
f1b70 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 _LOCK ){. r
f1b80 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 c = afpSetLock(c
f1b90 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 ontext->dbPath,
f1ba0 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 pFile, SHARED_FI
f1bb0 52 53 54 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 RST, SHARED_SIZE
f1bc0 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
f1bd0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
f1be0 20 28 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 (eFileLock==SHA
f1bf0 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 6e 6f RED_LOCK || pIno
f1c00 64 65 2d 3e 6e 53 68 61 72 65 64 3e 31 29 20 29 de->nShared>1) )
f1c10 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c {. /* onl
f1c20 79 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74 y re-establish t
f1c30 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 he shared lock i
f1c40 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 f necessary */.
f1c50 20 20 20 20 20 20 20 69 6e 74 20 73 68 61 72 65 int share
f1c60 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 dLockByte = SHAR
f1c70 45 44 5f 46 49 52 53 54 2b 70 49 6e 6f 64 65 2d ED_FIRST+pInode-
f1c80 3e 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20 >sharedByte;.
f1c90 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 rc = afpSet
f1ca0 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 Lock(context->db
f1cb0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 73 68 61 Path, pFile, sha
f1cc0 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 redLockByte, 1,
f1cd0 31 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 1);. } else
f1ce0 20 7b 0a 20 20 20 20 20 20 20 20 73 6b 69 70 53 {. skipS
f1cf0 68 61 72 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 hared = 1;.
f1d00 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
f1d10 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
f1d20 26 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f & pFile->eFileLo
f1d30 63 6b 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b ck>=PENDING_LOCK
f1d40 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 ){. rc = a
f1d50 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 fpSetLock(contex
f1d60 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 t->dbPath, pFile
f1d70 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 , PENDING_BYTE,
f1d80 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20 1, 0);. } .
f1d90 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
f1da0 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 65 46 _OK && pFile->eF
f1db0 69 6c 65 4c 6f 63 6b 3e 3d 52 45 53 45 52 56 45 ileLock>=RESERVE
f1dc0 44 5f 4c 4f 43 4b 20 26 26 20 63 6f 6e 74 65 78 D_LOCK && contex
f1dd0 74 2d 3e 72 65 73 65 72 76 65 64 20 29 7b 0a 20 t->reserved ){.
f1de0 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 rc = afpSet
f1df0 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 Lock(context->db
f1e00 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 Path, pFile, RES
f1e10 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20 30 ERVED_BYTE, 1, 0
f1e20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 72 63 );. if( !rc
f1e30 20 29 7b 20 0a 20 20 20 20 20 20 20 20 63 6f 6e ){ . con
f1e40 74 65 78 74 2d 3e 72 65 73 65 72 76 65 64 20 3d text->reserved =
f1e50 20 30 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 0; . }.
f1e60 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
f1e70 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 65 46 69 QLITE_OK && (eFi
f1e80 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c leLock==SHARED_L
f1e90 4f 43 4b 20 7c 7c 20 70 49 6e 6f 64 65 2d 3e 6e OCK || pInode->n
f1ea0 53 68 61 72 65 64 3e 31 29 29 7b 0a 20 20 20 20 Shared>1)){.
f1eb0 20 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c pInode->eFileL
f1ec0 6f 63 6b 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 ock = SHARED_LOC
f1ed0 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 K;. }. }. i
f1ee0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
f1ef0 20 26 26 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 4e && eFileLock==N
f1f00 4f 5f 4c 4f 43 4b 20 29 7b 0a 0a 20 20 20 20 2f O_LOCK ){.. /
f1f10 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 * Decrement the
f1f20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f 75 6e shared lock coun
f1f30 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20 74 68 ter. Release th
f1f40 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61 6e 0a e lock using an.
f1f50 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c 6c 20 6f ** OS call o
f1f60 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 74 68 72 nly when all thr
f1f70 65 61 64 73 20 69 6e 20 74 68 69 73 20 73 61 6d eads in this sam
f1f80 65 20 70 72 6f 63 65 73 73 20 68 61 76 65 20 72 e process have r
f1f90 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a 20 74 eleased. ** t
f1fa0 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a he lock.. */.
f1fb0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e unsigned lon
f1fc0 67 20 6c 6f 6e 67 20 73 68 61 72 65 64 4c 6f 63 g long sharedLoc
f1fd0 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f 46 kByte = SHARED_F
f1fe0 49 52 53 54 2b 70 49 6e 6f 64 65 2d 3e 73 68 61 IRST+pInode->sha
f1ff0 72 65 64 42 79 74 65 3b 0a 20 20 20 20 70 49 6e redByte;. pIn
f2000 6f 64 65 2d 3e 6e 53 68 61 72 65 64 2d 2d 3b 0a ode->nShared--;.
f2010 20 20 20 20 69 66 28 20 70 49 6e 6f 64 65 2d 3e if( pInode->
f2020 6e 53 68 61 72 65 64 3d 3d 30 20 29 7b 0a 20 20 nShared==0 ){.
f2030 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 SimulateIOEr
f2040 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 rorBenign(1);.
f2050 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 SimulateIOEr
f2060 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a 20 20 ror( h=(-1) ).
f2070 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 SimulateIOEr
f2080 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20 rorBenign(0);.
f2090 20 20 20 20 69 66 28 20 21 73 6b 69 70 53 68 61 if( !skipSha
f20a0 72 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 red ){. r
f20b0 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 c = afpSetLock(c
f20c0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 ontext->dbPath,
f20d0 70 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63 pFile, sharedLoc
f20e0 6b 42 79 74 65 2c 20 31 2c 20 30 29 3b 0a 20 20 kByte, 1, 0);.
f20f0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
f2100 21 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 70 !rc ){. p
f2110 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b Inode->eFileLock
f2120 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 = NO_LOCK;.
f2130 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 pFile->eFile
f2140 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a Lock = NO_LOCK;.
f2150 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
f2160 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
f2170 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 49 6e _OK ){. pIn
f2180 6f 64 65 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 ode->nLock--;.
f2190 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 6f assert( pIno
f21a0 64 65 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b 0a de->nLock>=0 );.
f21b0 20 20 20 20 20 20 69 66 28 20 70 49 6e 6f 64 65 if( pInode
f21c0 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 ->nLock==0 ){.
f21d0 20 20 20 20 20 20 63 6c 6f 73 65 50 65 6e 64 69 closePendi
f21e0 6e 67 46 64 73 28 70 46 69 6c 65 29 3b 0a 20 20 ngFds(pFile);.
f21f0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
f2200 20 20 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 . unixLeaveMu
f2210 74 65 78 28 29 3b 0a 20 20 69 66 28 20 72 63 3d tex();. if( rc=
f2220 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 70 46 69 =SQLITE_OK ) pFi
f2230 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 le->eFileLock =
f2240 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 72 65 74 eFileLock;. ret
f2250 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
f2260 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 26 20 Close a file &
f2270 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70 65 63 cleanup AFP spec
f2280 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e ific locking con
f2290 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69 63 20 text .*/.static
f22a0 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73 71 6c int afpClose(sql
f22b0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b ite3_file *id) {
f22c0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
f22d0 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64 20 TE_OK;. if( id
f22e0 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 ){. unixFile
f22f0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
f2300 6c 65 2a 29 69 64 3b 0a 20 20 20 20 61 66 70 55 le*)id;. afpU
f2310 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 nlock(id, NO_LOC
f2320 4b 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 K);. unixEnte
f2330 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 rMutex();. if
f2340 28 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 20 ( pFile->pInode
f2350 26 26 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 && pFile->pInode
f2360 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 ->nLock ){.
f2370 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 /* If there are
f2380 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 outstanding loc
f2390 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 ks, do not actua
f23a0 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65 20 66 69 lly close the fi
f23b0 6c 65 20 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a le just. **
f23c0 20 79 65 74 20 62 65 63 61 75 73 65 20 74 68 61 yet because tha
f23d0 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 t would clear th
f23e0 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 ose locks. Inst
f23f0 65 61 64 2c 20 61 64 64 20 74 68 65 20 66 69 6c ead, add the fil
f2400 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 e. ** descr
f2410 69 70 74 6f 72 20 74 6f 20 70 49 6e 6f 64 65 2d iptor to pInode-
f2420 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49 74 20 77 >aPending. It w
f2430 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 ill be automatic
f2440 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77 68 65 6e ally closed when
f2450 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 61 . ** the la
f2460 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 st lock is clear
f2470 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ed.. */.
f2480 20 20 20 73 65 74 50 65 6e 64 69 6e 67 46 64 28 setPendingFd(
f2490 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 pFile);. }.
f24a0 20 20 72 65 6c 65 61 73 65 49 6e 6f 64 65 49 6e releaseInodeIn
f24b0 66 6f 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 73 fo(pFile);. s
f24c0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c qlite3_free(pFil
f24d0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f24e0 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 6f t);. rc = clo
f24f0 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a seUnixFile(id);.
f2500 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 unixLeaveMut
f2510 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ex();. }. retu
f2520 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 rn rc;.}..#endif
f2530 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 /* defined(__AP
f2540 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 PLE__) && SQLITE
f2550 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
f2560 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 STYLE */./*.** T
f2570 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20 69 73 he code above is
f2580 20 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d the AFP lock im
f2590 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 plementation. T
f25a0 68 65 20 63 6f 64 65 20 69 73 20 73 70 65 63 69 he code is speci
f25b0 66 69 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 fic.** to MacOSX
f25c0 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 6f and does not wo
f25d0 72 6b 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 78 rk on other unix
f25e0 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 platforms. No
f25f0 61 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 69 alternative.** i
f2600 73 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 s available. If
f2610 20 79 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 69 you don't compi
f2620 6c 65 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 68 le for a mac, th
f2630 65 6e 20 74 68 65 20 22 75 6e 69 78 2d 61 66 70 en the "unix-afp
f2640 22 0a 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 20 ".** VFS is not
f2650 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a available..**.**
f2660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2670 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 41 *** End of the A
f2680 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e FP lock implemen
f2690 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a tation *********
f26a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
f26b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f26c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f26d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f26e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f26f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
f2700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
f2750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
f2770 20 4e 46 53 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a NFS Locking ***
f2780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
f27a0 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 #if defined(__AP
f27b0 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 PLE__) && SQLITE
f27c0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
f27d0 53 54 59 4c 45 0a 2f 2a 0a 20 2a 2a 20 4c 6f 77 STYLE./*. ** Low
f27e0 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c er the locking l
f27f0 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 evel on file des
f2800 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f criptor pFile to
f2810 20 65 46 69 6c 65 4c 6f 63 6b 2e 20 20 65 46 69 eFileLock. eFi
f2820 6c 65 4c 6f 63 6b 0a 20 2a 2a 20 6d 75 73 74 20 leLock. ** must
f2830 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 be either NO_LOC
f2840 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b K or SHARED_LOCK
f2850 2e 0a 20 2a 2a 0a 20 2a 2a 20 49 66 20 74 68 65 .. **. ** If the
f2860 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
f2870 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 f the file descr
f2880 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 iptor is already
f2890 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 20 2a 2a at or below. **
f28a0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
f28b0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 ocking level, th
f28c0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
f28d0 6e 6f 2d 6f 70 2e 0a 20 2a 2f 0a 73 74 61 74 69 no-op.. */.stati
f28e0 63 20 69 6e 74 20 6e 66 73 55 6e 6c 6f 63 6b 28 c int nfsUnlock(
f28f0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
f2900 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 , int eFileLock)
f2910 7b 0a 20 20 72 65 74 75 72 6e 20 70 6f 73 69 78 {. return posix
f2920 55 6e 6c 6f 63 6b 28 69 64 2c 20 65 46 69 6c 65 Unlock(id, eFile
f2930 4c 6f 63 6b 2c 20 31 29 3b 0a 7d 0a 0a 23 65 6e Lock, 1);.}..#en
f2940 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f dif /* defined(_
f2950 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c _APPLE__) && SQL
f2960 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
f2970 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a NG_STYLE */./*.*
f2980 2a 20 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65 * The code above
f2990 20 69 73 20 74 68 65 20 4e 46 53 20 6c 6f 63 6b is the NFS lock
f29a0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e implementation.
f29b0 20 20 54 68 65 20 63 6f 64 65 20 69 73 20 73 70 The code is sp
f29c0 65 63 69 66 69 63 0a 2a 2a 20 74 6f 20 4d 61 63 ecific.** to Mac
f29d0 4f 53 58 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 OSX and does not
f29e0 20 77 6f 72 6b 20 6f 6e 20 6f 74 68 65 72 20 75 work on other u
f29f0 6e 69 78 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 nix platforms.
f2a00 4e 6f 20 61 6c 74 65 72 6e 61 74 69 76 65 0a 2a No alternative.*
f2a10 2a 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 20 * is available.
f2a20 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.***********
f2a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
f2a40 66 20 74 68 65 20 4e 46 53 20 6c 6f 63 6b 20 69 f the NFS lock i
f2a50 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a mplementation **
f2a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2a70 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
f2a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2ac0 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/../*********
f2ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2b10 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
f2b20 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c 6f 63 6b 69 ****** Non-locki
f2b30 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ng sqlite3_file
f2b40 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a methods ********
f2b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2b60 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 *****.**.** The
f2b70 6e 65 78 74 20 64 69 76 69 73 69 6f 6e 20 63 6f next division co
f2b80 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 ntains implement
f2b90 61 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 20 6d ations for all m
f2ba0 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 0a 2a ethods of the .*
f2bb0 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f * sqlite3_file o
f2bc0 62 6a 65 63 74 20 6f 74 68 65 72 20 74 68 61 6e bject other than
f2bd0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 the locking met
f2be0 68 6f 64 73 2e 20 20 54 68 65 20 6c 6f 63 6b 69 hods. The locki
f2bf0 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 77 65 ng.** methods we
f2c00 72 65 20 64 65 66 69 6e 65 64 20 69 6e 20 64 69 re defined in di
f2c10 76 69 73 69 6f 6e 73 20 61 62 6f 76 65 20 28 6f visions above (o
f2c20 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f ne locking metho
f2c30 64 20 70 65 72 0a 2a 2a 20 64 69 76 69 73 69 6f d per.** divisio
f2c40 6e 29 2e 20 20 54 68 6f 73 65 20 6d 65 74 68 6f n). Those metho
f2c50 64 73 20 74 68 61 74 20 61 72 65 20 63 6f 6d 6d ds that are comm
f2c60 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e on to all lockin
f2c70 67 20 6d 6f 64 65 73 0a 2a 2a 20 61 72 65 20 67 g modes.** are g
f2c80 61 74 68 65 72 20 74 6f 67 65 74 68 65 72 20 69 ather together i
f2c90 6e 74 6f 20 74 68 69 73 20 64 69 76 69 73 69 6f nto this divisio
f2ca0 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 n..*/../*.** See
f2cb0 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 k to the offset
f2cc0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
f2cd0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 74 cond argument, t
f2ce0 68 65 6e 20 72 65 61 64 20 63 6e 74 20 0a 2a 2a hen read cnt .**
f2cf0 20 62 79 74 65 73 20 69 6e 74 6f 20 70 42 75 66 bytes into pBuf
f2d00 2e 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d . Return the num
f2d10 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 ber of bytes act
f2d20 75 61 6c 6c 79 20 72 65 61 64 2e 0a 2a 2a 0a 2a ually read..**.*
f2d30 2a 20 4e 42 3a 20 20 49 66 20 79 6f 75 20 64 65 * NB: If you de
f2d40 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44 20 6f fine USE_PREAD o
f2d50 72 20 55 53 45 5f 50 52 45 41 44 36 34 2c 20 74 r USE_PREAD64, t
f2d60 68 65 6e 20 69 74 20 6d 69 67 68 74 20 61 6c 73 hen it might als
f2d70 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 73 73 61 72 o.** be necessar
f2d80 79 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 y to define _XOP
f2d90 45 4e 5f 53 4f 55 52 43 45 20 74 6f 20 62 65 20 EN_SOURCE to be
f2da0 35 30 30 2e 20 20 54 68 69 73 20 76 61 72 69 65 500. This varie
f2db0 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 73 79 s from.** one sy
f2dc0 73 74 65 6d 20 74 6f 20 61 6e 6f 74 68 65 72 2e stem to another.
f2dd0 20 20 53 69 6e 63 65 20 53 51 4c 69 74 65 20 64 Since SQLite d
f2de0 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 55 oes not define U
f2df0 53 45 5f 50 52 45 41 44 0a 2a 2a 20 61 6e 79 20 SE_PREAD.** any
f2e00 61 6e 79 20 66 6f 72 6d 20 62 79 20 64 65 66 61 any form by defa
f2e10 75 6c 74 2c 20 77 65 20 77 69 6c 6c 20 6e 6f 74 ult, we will not
f2e20 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 66 69 attempt to defi
f2e30 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 ne _XOPEN_SOURCE
f2e40 2e 0a 2a 2a 20 53 65 65 20 74 69 63 6b 65 74 73 ..** See tickets
f2e50 20 23 32 37 34 31 20 61 6e 64 20 23 32 36 38 31 #2741 and #2681
f2e60 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 ..**.** To avoid
f2e70 20 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 stomping the er
f2e80 72 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 rno value on a f
f2e90 61 69 6c 65 64 20 72 65 61 64 20 74 68 65 20 6c ailed read the l
f2ea0 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a astErrno value.*
f2eb0 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 20 * is set before
f2ec0 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 returning..*/.st
f2ed0 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 atic int seekAnd
f2ee0 52 65 61 64 28 75 6e 69 78 46 69 6c 65 20 2a 69 Read(unixFile *i
f2ef0 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 d, sqlite3_int64
f2f00 20 6f 66 66 73 65 74 2c 20 76 6f 69 64 20 2a 70 offset, void *p
f2f10 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 Buf, int cnt){.
f2f20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 6e 74 20 int got;. int
f2f30 70 72 69 6f 72 20 3d 20 30 3b 0a 23 69 66 20 28 prior = 0;.#if (
f2f40 21 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 !defined(USE_PRE
f2f50 41 44 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 AD) && !defined(
f2f60 55 53 45 5f 50 52 45 41 44 36 34 29 29 0a 20 20 USE_PREAD64)).
f2f70 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a 23 i64 newOffset;.#
f2f80 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 53 54 endif. TIMER_ST
f2f90 41 52 54 3b 0a 20 20 61 73 73 65 72 74 28 20 63 ART;. assert( c
f2fa0 6e 74 3d 3d 28 63 6e 74 26 30 78 31 66 66 66 66 nt==(cnt&0x1ffff
f2fb0 29 20 29 3b 0a 20 20 63 6e 74 20 26 3d 20 30 78 ) );. cnt &= 0x
f2fc0 31 66 66 66 66 3b 0a 20 20 64 6f 7b 0a 23 69 66 1ffff;. do{.#if
f2fd0 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 defined(USE_PRE
f2fe0 41 44 29 0a 20 20 20 20 67 6f 74 20 3d 20 6f 73 AD). got = os
f2ff0 50 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 Pread(id->h, pBu
f3000 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b f, cnt, offset);
f3010 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 . SimulateIOE
f3020 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 rror( got = -1 )
f3030 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 ;.#elif defined(
f3040 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 20 USE_PREAD64).
f3050 20 67 6f 74 20 3d 20 6f 73 50 72 65 61 64 36 34 got = osPread64
f3060 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e (id->h, pBuf, cn
f3070 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20 t, offset);.
f3080 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
f3090 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c got = -1 );.#el
f30a0 73 65 0a 20 20 20 20 6e 65 77 4f 66 66 73 65 74 se. newOffset
f30b0 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 = lseek(id->h,
f30c0 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 offset, SEEK_SET
f30d0 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 );. SimulateI
f30e0 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 65 OError( newOffse
f30f0 74 2d 2d 20 29 3b 0a 20 20 20 20 69 66 28 20 6e t-- );. if( n
f3100 65 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 ewOffset!=offset
f3110 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 ){. if( ne
f3120 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b wOffset == -1 ){
f3130 0a 20 20 20 20 20 20 20 20 28 28 75 6e 69 78 46 . ((unixF
f3140 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 ile*)id)->lastEr
f3150 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
f3160 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
f3170 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 ((unixFile*)id
f3180 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 )->lastErrno = 0
f3190 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
f31a0 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d return -1;. }
f31b0 0a 20 20 20 20 67 6f 74 20 3d 20 6f 73 52 65 61 . got = osRea
f31c0 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 d(id->h, pBuf, c
f31d0 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 nt);.#endif.
f31e0 69 66 28 20 67 6f 74 3d 3d 63 6e 74 20 29 20 62 if( got==cnt ) b
f31f0 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 67 6f reak;. if( go
f3200 74 3c 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 t<0 ){. if(
f3210 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 29 7b errno==EINTR ){
f3220 20 67 6f 74 20 3d 20 31 3b 20 63 6f 6e 74 69 6e got = 1; contin
f3230 75 65 3b 20 7d 0a 20 20 20 20 20 20 70 72 69 6f ue; }. prio
f3240 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 28 28 75 r = 0;. ((u
f3250 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 nixFile*)id)->la
f3260 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
f3270 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
f3280 20 20 7d 65 6c 73 65 20 69 66 28 20 67 6f 74 3e }else if( got>
f3290 30 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 20 2d 0 ){. cnt -
f32a0 3d 20 67 6f 74 3b 0a 20 20 20 20 20 20 6f 66 66 = got;. off
f32b0 73 65 74 20 2b 3d 20 67 6f 74 3b 0a 20 20 20 20 set += got;.
f32c0 20 20 70 72 69 6f 72 20 2b 3d 20 67 6f 74 3b 0a prior += got;.
f32d0 20 20 20 20 20 20 70 42 75 66 20 3d 20 28 76 6f pBuf = (vo
f32e0 69 64 2a 29 28 67 6f 74 20 2b 20 28 63 68 61 72 id*)(got + (char
f32f0 2a 29 70 42 75 66 29 3b 0a 20 20 20 20 7d 0a 20 *)pBuf);. }.
f3300 20 7d 77 68 69 6c 65 28 20 67 6f 74 3e 30 20 29 }while( got>0 )
f3310 3b 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 ;. TIMER_END;.
f3320 20 4f 53 54 52 41 43 45 28 28 22 52 45 41 44 20 OSTRACE(("READ
f3330 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c %-3d %5d %7ll
f3340 64 20 25 6c 6c 75 5c 6e 22 2c 0a 20 20 20 20 20 d %llu\n",.
f3350 20 20 20 20 20 20 20 69 64 2d 3e 68 2c 20 67 6f id->h, go
f3360 74 2b 70 72 69 6f 72 2c 20 6f 66 66 73 65 74 2d t+prior, offset-
f3370 70 72 69 6f 72 2c 20 54 49 4d 45 52 5f 45 4c 41 prior, TIMER_ELA
f3380 50 53 45 44 29 29 3b 0a 20 20 72 65 74 75 72 6e PSED));. return
f3390 20 67 6f 74 2b 70 72 69 6f 72 3b 0a 7d 0a 0a 2f got+prior;.}../
f33a0 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 *.** Read data f
f33b0 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 6f 20 rom a file into
f33c0 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 75 72 a buffer. Retur
f33d0 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 n SQLITE_OK if a
f33e0 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 72 65 ll.** bytes were
f33f0 20 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c read successful
f3400 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f ly and SQLITE_IO
f3410 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e 67 20 ERR if anything
f3420 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a goes.** wrong..*
f3430 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
f3440 78 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 xRead(. sqlite3
f3450 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 76 6f _file *id, . vo
f3460 69 64 20 2a 70 42 75 66 2c 20 0a 20 20 69 6e 74 id *pBuf, . int
f3470 20 61 6d 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f amt,. sqlite3_
f3480 69 6e 74 36 34 20 6f 66 66 73 65 74 0a 29 7b 0a int64 offset.){.
f3490 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
f34a0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 e = (unixFile *)
f34b0 69 64 3b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 id;. int got;.
f34c0 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 0a assert( id );..
f34d0 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
f34e0 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 a database file
f34f0 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 (not a journal,
f3500 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f master-journal o
f3510 72 20 74 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 r temp. ** file
f3520 29 2c 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 ), the bytes in
f3530 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 the locking rang
f3540 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 e should never b
f3550 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 e read or writte
f3560 6e 2e 20 2a 2f 0a 23 69 66 20 30 0a 20 20 61 73 n. */.#if 0. as
f3570 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e sert( pFile->pUn
f3580 75 73 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c used==0. |
f3590 7c 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e | offset>=PENDIN
f35a0 47 5f 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20 G_BYTE+512.
f35b0 20 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c || offset+amt<
f35c0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 =PENDING_BYTE .
f35d0 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 67 6f );.#endif.. go
f35e0 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28 t = seekAndRead(
f35f0 70 46 69 6c 65 2c 20 6f 66 66 73 65 74 2c 20 70 pFile, offset, p
f3600 42 75 66 2c 20 61 6d 74 29 3b 0a 20 20 69 66 28 Buf, amt);. if(
f3610 20 67 6f 74 3d 3d 61 6d 74 20 29 7b 0a 20 20 20 got==amt ){.
f3620 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
f3630 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 K;. }else if( g
f3640 6f 74 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6c ot<0 ){. /* l
f3650 61 73 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20 astErrno set by
f3660 73 65 65 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20 seekAndRead */.
f3670 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
f3680 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d _IOERR_READ;. }
f3690 65 6c 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d else{. pFile-
f36a0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 >lastErrno = 0;
f36b0 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 /* not a system
f36c0 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 error */. /*
f36d0 55 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 Unread parts of
f36e0 74 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 the buffer must
f36f0 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a be zero-filled *
f3700 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 /. memset(&((
f3710 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d char*)pBuf)[got]
f3720 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 , 0, amt-got);.
f3730 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
f3740 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
f3750 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 D;. }.}../*.**
f3760 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 Seek to the offs
f3770 65 74 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 et in id->offset
f3780 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 then read cnt b
f3790 79 74 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a ytes into pBuf..
f37a0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
f37b0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 mber of bytes ac
f37c0 74 75 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 tually read. Up
f37d0 64 61 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e date the offset.
f37e0 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 .**.** To avoid
f37f0 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72 stomping the err
f3800 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 no value on a fa
f3810 69 6c 65 64 20 77 72 69 74 65 20 74 68 65 20 6c iled write the l
f3820 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a astErrno value.*
f3830 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 20 * is set before
f3840 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 returning..*/.st
f3850 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 atic int seekAnd
f3860 57 72 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a Write(unixFile *
f3870 69 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 id, i64 offset,
f3880 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 const void *pBuf
f3890 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e , int cnt){. in
f38a0 74 20 67 6f 74 3b 0a 23 69 66 20 28 21 64 65 66 t got;.#if (!def
f38b0 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29 20 ined(USE_PREAD)
f38c0 26 26 20 21 64 65 66 69 6e 65 64 28 55 53 45 5f && !defined(USE_
f38d0 50 52 45 41 44 36 34 29 29 0a 20 20 69 36 34 20 PREAD64)). i64
f38e0 6e 65 77 4f 66 66 73 65 74 3b 0a 23 65 6e 64 69 newOffset;.#endi
f38f0 66 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 3d f. assert( cnt=
f3900 3d 28 63 6e 74 26 30 78 31 66 66 66 66 29 20 29 =(cnt&0x1ffff) )
f3910 3b 0a 20 20 63 6e 74 20 26 3d 20 30 78 31 66 66 ;. cnt &= 0x1ff
f3920 66 66 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 ff;. TIMER_STAR
f3930 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55 T;.#if defined(U
f3940 53 45 5f 50 52 45 41 44 29 0a 20 20 64 6f 7b 20 SE_PREAD). do{
f3950 67 6f 74 20 3d 20 6f 73 50 77 72 69 74 65 28 69 got = osPwrite(i
f3960 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c d->h, pBuf, cnt,
f3970 20 6f 66 66 73 65 74 29 3b 20 7d 77 68 69 6c 65 offset); }while
f3980 28 20 67 6f 74 3c 30 20 26 26 20 65 72 72 6e 6f ( got<0 && errno
f3990 3d 3d 45 49 4e 54 52 20 29 3b 0a 23 65 6c 69 66 ==EINTR );.#elif
f39a0 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 defined(USE_PRE
f39b0 41 44 36 34 29 0a 20 20 64 6f 7b 20 67 6f 74 20 AD64). do{ got
f39c0 3d 20 6f 73 50 77 72 69 74 65 36 34 28 69 64 2d = osPwrite64(id-
f39d0 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f >h, pBuf, cnt, o
f39e0 66 66 73 65 74 29 3b 7d 77 68 69 6c 65 28 20 67 ffset);}while( g
f39f0 6f 74 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d 45 ot<0 && errno==E
f3a00 49 4e 54 52 29 3b 0a 23 65 6c 73 65 0a 20 20 64 INTR);.#else. d
f3a10 6f 7b 0a 20 20 20 20 6e 65 77 4f 66 66 73 65 74 o{. newOffset
f3a20 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 = lseek(id->h,
f3a30 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 offset, SEEK_SET
f3a40 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 );. SimulateI
f3a50 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 65 OError( newOffse
f3a60 74 2d 2d 20 29 3b 0a 20 20 20 20 69 66 28 20 6e t-- );. if( n
f3a70 65 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 ewOffset!=offset
f3a80 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 ){. if( ne
f3a90 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b wOffset == -1 ){
f3aa0 0a 20 20 20 20 20 20 20 20 28 28 75 6e 69 78 46 . ((unixF
f3ab0 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 ile*)id)->lastEr
f3ac0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
f3ad0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
f3ae0 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 ((unixFile*)id
f3af0 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 )->lastErrno = 0
f3b00 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
f3b10 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d return -1;. }
f3b20 0a 20 20 20 20 67 6f 74 20 3d 20 6f 73 57 72 69 . got = osWri
f3b30 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 te(id->h, pBuf,
f3b40 63 6e 74 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 cnt);. }while(
f3b50 67 6f 74 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d got<0 && errno==
f3b60 45 49 4e 54 52 20 29 3b 0a 23 65 6e 64 69 66 0a EINTR );.#endif.
f3b70 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 69 TIMER_END;. i
f3b80 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 20 f( got<0 ){.
f3b90 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
f3ba0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
f3bb0 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 4f 53 54 52 41 no;. }.. OSTRA
f3bc0 43 45 28 28 22 57 52 49 54 45 20 20 20 25 2d 33 CE(("WRITE %-3
f3bd0 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75 d %5d %7lld %llu
f3be0 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c \n", id->h, got,
f3bf0 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45 offset, TIMER_E
f3c00 4c 41 50 53 45 44 29 29 3b 0a 20 20 72 65 74 75 LAPSED));. retu
f3c10 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a rn got;.}.../*.*
f3c20 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f * Write data fro
f3c30 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 m a buffer into
f3c40 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 a file. Return
f3c50 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
f3c60 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 cess.** or some
f3c70 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 other error code
f3c80 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a on failure..*/.
f3c90 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 57 static int unixW
f3ca0 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f rite(. sqlite3_
f3cb0 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f 6e file *id, . con
f3cc0 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a st void *pBuf, .
f3cd0 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c int amt,. sql
f3ce0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 ite3_int64 offse
f3cf0 74 20 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 t .){. unixFile
f3d00 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
f3d10 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 77 ile*)id;. int w
f3d20 72 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 rote = 0;. asse
f3d30 72 74 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65 rt( id );. asse
f3d40 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a 20 20 rt( amt>0 );..
f3d50 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 /* If this is a
f3d60 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e database file (n
f3d70 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 ot a journal, ma
f3d80 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 ster-journal or
f3d90 74 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c temp. ** file),
f3da0 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 the bytes in th
f3db0 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 e locking range
f3dc0 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 should never be
f3dd0 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e read or written.
f3de0 20 2a 2f 0a 23 69 66 20 30 0a 20 20 61 73 73 65 */.#if 0. asse
f3df0 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 rt( pFile->pUnus
f3e00 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20 ed==0. ||
f3e10 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47 5f offset>=PENDING_
f3e20 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20 20 20 BYTE+512.
f3e30 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 || offset+amt<=P
f3e40 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 20 29 ENDING_BYTE . )
f3e50 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ;.#endif..#ifdef
f3e60 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
f3e70 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 /* If we are doi
f3e80 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 ng a normal writ
f3e90 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 e to a database
f3ea0 66 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 file (as opposed
f3eb0 20 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 to. ** doing a
f3ec0 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
f3ed0 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 lback or a write
f3ee0 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 to some file ot
f3ef0 68 65 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 her than a. **
f3f00 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20 normal database
f3f10 66 69 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72 file) then recor
f3f20 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 d the fact that
f3f30 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a the database. *
f3f40 2a 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20 * has changed.
f3f50 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 If the transacti
f3f60 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f on counter is mo
f3f70 64 69 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74 dified, record t
f3f80 68 61 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f hat. ** fact to
f3f90 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 o.. */. if( pF
f3fa0 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 ile->inNormalWri
f3fb0 74 65 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d te ){. pFile-
f3fc0 3e 64 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20 >dbUpdate = 1;
f3fd0 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
f3fe0 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 has been modifie
f3ff0 64 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 d */. if( off
f4000 73 65 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 set<=24 && offse
f4010 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 t+amt>=27 ){.
f4020 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 int rc;.
f4030 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d char oldCntr[4]
f4040 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 ;. Simulate
f4050 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 IOErrorBenign(1)
f4060 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 65 ;. rc = see
f4070 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20 kAndRead(pFile,
f4080 32 34 2c 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 24, oldCntr, 4);
f4090 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 . SimulateI
f40a0 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b OErrorBenign(0);
f40b0 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 34 . if( rc!=4
f40c0 20 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e || memcmp(oldCn
f40d0 74 72 2c 20 26 28 28 63 68 61 72 2a 29 70 42 75 tr, &((char*)pBu
f40e0 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34 f)[24-offset], 4
f40f0 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )!=0 ){.
f4100 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 pFile->transCntr
f4110 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68 Chng = 1; /* Th
f4120 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f e transaction co
f4130 75 6e 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 unter has change
f4140 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 d */. }.
f4150 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 }. }.#endif..
f4160 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 20 26 26 while( amt>0 &&
f4170 20 28 77 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e (wrote = seekAn
f4180 64 57 72 69 74 65 28 70 46 69 6c 65 2c 20 6f 66 dWrite(pFile, of
f4190 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 fset, pBuf, amt)
f41a0 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d )>0 ){. amt -
f41b0 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 6f 66 66 = wrote;. off
f41c0 73 65 74 20 2b 3d 20 77 72 6f 74 65 3b 0a 20 20 set += wrote;.
f41d0 20 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 pBuf = &((char
f41e0 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a *)pBuf)[wrote];.
f41f0 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f }. SimulateIO
f4200 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d 28 2d Error(( wrote=(-
f4210 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 1), amt=1 ));.
f4220 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c SimulateDiskfull
f4230 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d 30 2c Error(( wrote=0,
f4240 20 61 6d 74 3d 31 20 29 29 3b 0a 0a 20 20 69 66 amt=1 ));.. if
f4250 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 69 ( amt>0 ){. i
f4260 66 28 20 77 72 6f 74 65 3c 30 20 26 26 20 70 46 f( wrote<0 && pF
f4270 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 21 3d ile->lastErrno!=
f4280 45 4e 4f 53 50 43 20 29 7b 0a 20 20 20 20 20 20 ENOSPC ){.
f4290 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 73 65 74 /* lastErrno set
f42a0 20 62 79 20 73 65 65 6b 41 6e 64 57 72 69 74 65 by seekAndWrite
f42b0 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e */. return
f42c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 SQLITE_IOERR_WR
f42d0 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ITE;. }else{.
f42e0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
f42f0 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e tErrno = 0; /* n
f4300 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 6f ot a system erro
f4310 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 r */. retur
f4320 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 n SQLITE_FULL;.
f4330 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 }. }.. retu
f4340 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
f4350 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
f4360 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 EST./*.** Count
f4370 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 the number of fu
f4380 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d llsyncs and norm
f4390 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 al syncs. This
f43a0 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a is used to test.
f43b0 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e ** that syncs an
f43c0 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 d fullsyncs are
f43d0 6f 63 63 75 72 72 69 6e 67 20 61 74 20 74 68 65 occurring at the
f43e0 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f right times..*/
f43f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
f4400 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 sqlite3_sync_cou
f4410 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 nt = 0;.SQLITE_A
f4420 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 PI int sqlite3_f
f4430 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 ullsync_count =
f4440 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
f4450 20 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 73 74 We do not trust
f4460 20 73 79 73 74 65 6d 73 20 74 6f 20 70 72 6f 76 systems to prov
f4470 69 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 66 64 ide a working fd
f4480 61 74 61 73 79 6e 63 28 29 2e 20 20 53 6f 6d 65 atasync(). Some
f4490 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 20 64 do..** Others d
f44a0 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 61 66 o no. To be saf
f44b0 65 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 63 6b e, we will stick
f44c0 20 77 69 74 68 20 74 68 65 20 28 73 6c 69 67 68 with the (sligh
f44d0 74 6c 79 20 73 6c 6f 77 65 72 29 0a 2a 2a 20 66 tly slower).** f
f44e0 73 79 6e 63 28 29 2e 20 49 66 20 79 6f 75 20 6b sync(). If you k
f44f0 6e 6f 77 20 74 68 61 74 20 79 6f 75 72 20 73 79 now that your sy
f4500 73 74 65 6d 20 64 6f 65 73 20 73 75 70 70 6f 72 stem does suppor
f4510 74 20 66 64 61 74 61 73 79 6e 63 28 29 20 63 6f t fdatasync() co
f4520 72 72 65 63 74 6c 79 2c 0a 2a 2a 20 74 68 65 6e rrectly,.** then
f4530 20 73 69 6d 70 6c 79 20 63 6f 6d 70 69 6c 65 20 simply compile
f4540 77 69 74 68 20 2d 44 66 64 61 74 61 73 79 6e 63 with -Dfdatasync
f4550 3d 66 64 61 74 61 73 79 6e 63 0a 2a 2f 0a 23 69 =fdatasync.*/.#i
f4560 66 20 21 64 65 66 69 6e 65 64 28 66 64 61 74 61 f !defined(fdata
f4570 73 79 6e 63 29 0a 23 20 64 65 66 69 6e 65 20 66 sync).# define f
f4580 64 61 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 datasync fsync.#
f4590 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 endif../*.** Def
f45a0 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 ine HAVE_FULLFSY
f45b0 4e 43 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70 NC to 0 or 1 dep
f45c0 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 ending on whethe
f45d0 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 r or not.** the
f45e0 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 F_FULLFSYNC macr
f45f0 6f 20 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46 o is defined. F
f4600 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 _FULLFSYNC is cu
f4610 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 rrently.** only
f4620 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 available on Mac
f4630 20 4f 53 20 58 2e 20 20 42 75 74 20 74 68 61 74 OS X. But that
f4640 20 63 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a could change..*
f4650 2f 0a 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 /.#ifdef F_FULLF
f4660 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41 SYNC.# define HA
f4670 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 VE_FULLFSYNC 1.#
f4680 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41 else.# define HA
f4690 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 VE_FULLFSYNC 0.#
f46a0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 endif.../*.** Th
f46b0 65 20 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d e fsync() system
f46c0 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 call does not w
f46d0 6f 72 6b 20 61 73 20 61 64 76 65 72 74 69 73 65 ork as advertise
f46e0 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 d on many.** uni
f46f0 78 20 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20 x systems. The
f4700 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 following proced
f4710 75 72 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 ure is an attemp
f4720 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 t to make.** it
f4730 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a work better..**.
f4740 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f ** The SQLITE_NO
f4750 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 _SYNC macro disa
f4760 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 bles all fsync()
f4770 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 66 s. This is usef
f4780 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e ul.** for testin
f4790 67 20 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 g when we want t
f47a0 6f 20 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68 o run through th
f47b0 65 20 74 65 73 74 20 73 75 69 74 65 20 71 75 69 e test suite qui
f47c0 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 ckly..** You are
f47d0 20 73 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 strongly advise
f47e0 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f d *not* to deplo
f47f0 79 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f y with SQLITE_NO
f4800 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 _SYNC.** enabled
f4810 2c 20 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 , however, since
f4820 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f with SQLITE_NO_
f4830 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e SYNC enabled, an
f4840 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 OS crash.** or
f4850 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 69 power failure wi
f4860 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 ll likely corrup
f4870 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 t the database f
f4880 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 ile..**.** SQLit
f4890 65 20 73 65 74 73 20 74 68 65 20 64 61 74 61 4f e sets the dataO
f48a0 6e 6c 79 20 66 6c 61 67 20 69 66 20 74 68 65 20 nly flag if the
f48b0 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 size of the file
f48c0 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a is unchanged..*
f48d0 2a 20 54 68 65 20 69 64 65 61 20 62 65 68 69 6e * The idea behin
f48e0 64 20 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 d dataOnly is th
f48f0 61 74 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c at it should onl
f4900 79 20 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 y write the file
f4910 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 content.** to d
f4920 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f isk, not the ino
f4930 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 de. We only set
f4940 20 64 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 dataOnly if the
f4950 20 66 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a file size is .*
f4960 2a 20 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 * unchanged sinc
f4970 65 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 e the file size
f4980 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 is part of the i
f4990 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 node. However,
f49a0 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c .** Ted Ts'o tel
f49b0 6c 73 20 75 73 20 74 68 61 74 20 66 64 61 74 61 ls us that fdata
f49c0 73 79 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f sync() will also
f49d0 20 77 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65 write the inode
f49e0 20 69 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 if the.** file
f49f0 73 69 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64 size has changed
f4a00 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c . The only real
f4a10 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 difference betw
f4a20 65 65 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a een fdatasync().
f4a30 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 ** and fsync(),
f4a40 54 65 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 Ted tells us, is
f4a50 20 74 68 61 74 20 66 64 61 74 61 73 79 6e 63 28 that fdatasync(
f4a60 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 ) will not flush
f4a70 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 the.** inode if
f4a80 20 74 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 the mtime or ow
f4a90 6e 65 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f ner or other ino
f4aa0 64 65 20 61 74 74 72 69 62 75 74 65 73 20 68 61 de attributes ha
f4ab0 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 ve changed..** W
f4ac0 65 20 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 e only care abou
f4ad0 74 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c t the file size,
f4ae0 20 6e 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66 not the other f
f4af0 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 2c 20 ile attributes,
f4b00 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20 so.** as far as
f4b10 53 51 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72 SQLite is concer
f4b20 6e 65 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e ned, an fdatasyn
f4b30 63 28 29 20 69 73 20 61 6c 77 61 79 73 20 61 64 c() is always ad
f4b40 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 equate..** So, w
f4b50 65 20 61 6c 77 61 79 73 20 75 73 65 20 66 64 61 e always use fda
f4b60 74 61 73 79 6e 63 28 29 20 69 66 20 69 74 20 69 tasync() if it i
f4b70 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 s available, reg
f4b80 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 ardless of.** th
f4b90 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 e value of the d
f4ba0 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f ataOnly flag..*/
f4bb0 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c .static int full
f4bc0 5f 66 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 _fsync(int fd, i
f4bd0 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 nt fullSync, int
f4be0 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e dataOnly){. in
f4bf0 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 t rc;.. /* The
f4c00 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 following "ifdef
f4c10 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f /elif/else/" blo
f4c20 63 6b 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 ck has the same
f4c30 73 74 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a structure as. *
f4c40 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e * the one below.
f4c50 20 49 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 It is replicate
f4c60 64 20 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f d here solely to
f4c70 20 61 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e avoid clutterin
f4c80 67 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 g . ** up the r
f4c90 65 61 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 eal code with th
f4ca0 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 e UNUSED_PARAMET
f4cb0 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a ER() macros.. *
f4cc0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
f4cd0 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 NO_SYNC. UNUSED
f4ce0 5f 50 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a _PARAMETER(fd);.
f4cf0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
f4d00 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 ER(fullSync);.
f4d10 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
f4d20 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 (dataOnly);.#eli
f4d30 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 f HAVE_FULLFSYNC
f4d40 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
f4d50 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 TER(dataOnly);.#
f4d60 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 else. UNUSED_PA
f4d70 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 RAMETER(fullSync
f4d80 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
f4d90 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b METER(dataOnly);
f4da0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 .#endif.. /* Re
f4db0 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 cord the number
f4dc0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 of times that we
f4dd0 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 do a normal fsy
f4de0 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 nc() and . ** F
f4df0 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 ULLSYNC. This i
f4e00 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 s used during te
f4e10 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 sting to verify
f4e20 74 68 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 that this proced
f4e30 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 ure. ** gets ca
f4e40 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 6f lled with the co
f4e50 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e rrect arguments.
f4e60 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c . */.#ifdef SQL
f4e70 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 ITE_TEST. if( f
f4e80 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 ullSync ) sqlite
f4e90 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 3_fullsync_count
f4ea0 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 ++;. sqlite3_sy
f4eb0 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 nc_count++;.#end
f4ec0 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 if.. /* If we c
f4ed0 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
f4ee0 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 SQLITE_NO_SYNC
f4ef0 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 flag, then synci
f4f00 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d ng is a. ** no-
f4f10 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 op. */.#ifdef S
f4f20 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 QLITE_NO_SYNC.
f4f30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
f4f40 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 #elif HAVE_FULLF
f4f50 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 SYNC. if( fullS
f4f60 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ync ){. rc =
f4f70 6f 73 46 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 osFcntl(fd, F_FU
f4f80 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d LLFSYNC, 0);. }
f4f90 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 else{. rc = 1
f4fa0 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 ;. }. /* If th
f4fb0 65 20 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c e FULLFSYNC fail
f4fc0 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f ed, fall back to
f4fd0 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 attempting an f
f4fe0 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74 20 sync().. ** It
f4ff0 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 shouldn't be pos
f5000 73 69 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 sible for fullfs
f5010 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 ync to fail on t
f5020 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 he local . ** f
f5030 69 6c 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f ile system (on O
f5040 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 SX), so failure
f5050 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 46 indicates that F
f5060 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69 73 ULLFSYNC. ** is
f5070 6e 27 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f n't supported fo
f5080 72 20 74 68 69 73 20 66 69 6c 65 20 73 79 73 74 r this file syst
f5090 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 em. So, attempt
f50a0 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20 61 an fsync . ** a
f50b0 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e nd (for now) ign
f50c0 6f 72 65 20 74 68 65 20 6f 76 65 72 68 65 61 64 ore the overhead
f50d0 20 6f 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75 of a superfluou
f50e0 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a s fcntl call. .
f50f0 20 20 2a 2a 20 49 74 27 64 20 62 65 20 62 65 74 ** It'd be bet
f5100 74 65 72 20 74 6f 20 64 65 74 65 63 74 20 66 75 ter to detect fu
f5110 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20 llfsync support
f5120 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a once and avoid .
f5130 20 20 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 ** the fcntl c
f5140 61 6c 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73 all every time s
f5150 79 6e 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 ync is called..
f5160 20 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72 */. if( rc ) r
f5170 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a c = fsync(fd);..
f5180 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 5f #elif defined(__
f5190 41 50 50 4c 45 5f 5f 29 0a 20 20 2f 2a 20 66 64 APPLE__). /* fd
f51a0 61 74 61 73 79 6e 63 28 29 20 6f 6e 20 48 46 53 atasync() on HFS
f51b0 2b 20 64 6f 65 73 6e 27 74 20 79 65 74 20 66 6c + doesn't yet fl
f51c0 75 73 68 20 74 68 65 20 66 69 6c 65 20 73 69 7a ush the file siz
f51d0 65 20 69 66 20 69 74 20 63 68 61 6e 67 65 64 20 e if it changed
f51e0 63 6f 72 72 65 63 74 6c 79 0a 20 20 2a 2a 20 73 correctly. ** s
f51f0 6f 20 63 75 72 72 65 6e 74 6c 79 20 77 65 20 64 o currently we d
f5200 65 66 61 75 6c 74 20 74 6f 20 74 68 65 20 6d 61 efault to the ma
f5210 63 72 6f 20 74 68 61 74 20 72 65 64 65 66 69 6e cro that redefin
f5220 65 73 20 66 64 61 74 61 73 79 6e 63 20 74 6f 20 es fdatasync to
f5230 66 73 79 6e 63 0a 20 20 2a 2f 0a 20 20 72 63 20 fsync. */. rc
f5240 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 23 65 6c = fsync(fd);.#el
f5250 73 65 20 0a 20 20 72 63 20 3d 20 66 64 61 74 61 se . rc = fdata
f5260 73 79 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f 53 sync(fd);.#if OS
f5270 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 _VXWORKS. if( r
f5280 63 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d 3d c==-1 && errno==
f5290 45 4e 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 72 ENOTSUP ){. r
f52a0 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 c = fsync(fd);.
f52b0 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f }.#endif /* OS_
f52c0 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 69 VXWORKS */.#endi
f52d0 66 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 54 f /* ifdef SQLIT
f52e0 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 48 E_NO_SYNC elif H
f52f0 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a 2f AVE_FULLFSYNC */
f5300 0a 0a 20 20 69 66 28 20 4f 53 5f 56 58 57 4f 52 .. if( OS_VXWOR
f5310 4b 53 20 26 26 20 72 63 21 3d 20 2d 31 20 29 7b KS && rc!= -1 ){
f5320 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d . rc = 0;. }
f5330 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
f5340 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69 ./*.** Open a fi
f5350 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f le descriptor to
f5360 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 63 the directory c
f5370 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65 20 7a ontaining file z
f5380 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49 66 20 Filename..** If
f5390 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 46 64 successful, *pFd
f53a0 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f is set to the o
f53b0 70 65 6e 65 64 20 66 69 6c 65 20 64 65 73 63 72 pened file descr
f53c0 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53 51 4c iptor and.** SQL
f53d0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
f53e0 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 ed. If an error
f53f0 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 20 53 occurs, either S
f5400 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 6f QLITE_NOMEM.** o
f5410 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 r SQLITE_CANTOPE
f5420 4e 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e N is returned an
f5430 64 20 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f d *pFd is set to
f5440 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a an undefined.**
f5450 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 value..**.** Th
f5460 65 20 64 69 72 65 63 74 6f 72 79 20 66 69 6c 65 e directory file
f5470 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 75 descriptor is u
f5480 73 65 64 20 66 6f 72 20 6f 6e 6c 79 20 6f 6e 65 sed for only one
f5490 20 74 68 69 6e 67 20 2d 20 74 6f 0a 2a 2a 20 66 thing - to.** f
f54a0 73 79 6e 63 28 29 20 61 20 64 69 72 65 63 74 6f sync() a directo
f54b0 72 79 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ry to make sure
f54c0 66 69 6c 65 20 63 72 65 61 74 69 6f 6e 20 61 6e file creation an
f54d0 64 20 64 65 6c 65 74 69 6f 6e 20 65 76 65 6e 74 d deletion event
f54e0 73 0a 2a 2a 20 61 72 65 20 66 6c 75 73 68 65 64 s.** are flushed
f54f0 20 74 6f 20 64 69 73 6b 2e 20 20 53 75 63 68 20 to disk. Such
f5500 66 73 79 6e 63 73 20 61 72 65 20 6e 6f 74 20 6e fsyncs are not n
f5510 65 65 64 65 64 20 6f 6e 20 6e 65 77 65 72 0a 2a eeded on newer.*
f5520 2a 20 6a 6f 75 72 6e 61 6c 69 6e 67 20 66 69 6c * journaling fil
f5530 65 73 79 73 74 65 6d 73 2c 20 62 75 74 20 61 72 esystems, but ar
f5540 65 20 72 65 71 75 69 72 65 64 20 6f 6e 20 6f 6c e required on ol
f5550 64 65 72 20 66 69 6c 65 73 79 73 74 65 6d 73 2e der filesystems.
f5560 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
f5570 69 6e 65 20 63 61 6e 20 62 65 20 6f 76 65 72 72 ine can be overr
f5580 69 64 64 65 6e 20 75 73 69 6e 67 20 74 68 65 20 idden using the
f5590 78 53 65 74 53 79 73 43 61 6c 6c 20 69 6e 74 65 xSetSysCall inte
f55a0 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 61 62 rface..** The ab
f55b0 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 72 69 64 ility to overrid
f55c0 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 e this routine w
f55d0 61 73 20 61 64 64 65 64 20 69 6e 20 73 75 70 70 as added in supp
f55e0 6f 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 63 68 ort of the.** ch
f55f0 72 6f 6d 69 75 6d 20 73 61 6e 64 62 6f 78 2e 20 romium sandbox.
f5600 20 4f 70 65 6e 69 6e 67 20 61 20 64 69 72 65 63 Opening a direc
f5610 74 6f 72 79 20 69 73 20 61 20 73 65 63 75 72 69 tory is a securi
f5620 74 79 20 72 69 73 6b 20 28 77 65 20 61 72 65 0a ty risk (we are.
f5630 2a 2a 20 74 6f 6c 64 29 20 73 6f 20 6d 61 6b 69 ** told) so maki
f5640 6e 67 20 69 74 20 6f 76 65 72 72 69 64 65 61 62 ng it overrideab
f5650 6c 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 68 le allows the ch
f5660 72 6f 6d 69 75 6d 20 73 61 6e 64 62 6f 78 20 74 romium sandbox t
f5670 6f 0a 2a 2a 20 72 65 70 6c 61 63 65 20 74 68 69 o.** replace thi
f5680 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 61 s routine with a
f5690 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e harmless no-op.
f56a0 20 20 54 6f 20 6d 61 6b 65 20 74 68 69 73 20 72 To make this r
f56b0 6f 75 74 69 6e 65 0a 2a 2a 20 61 20 6e 6f 2d 6f outine.** a no-o
f56c0 70 2c 20 72 65 70 6c 61 63 65 20 69 74 20 77 69 p, replace it wi
f56d0 74 68 20 61 20 73 74 75 62 20 74 68 61 74 20 72 th a stub that r
f56e0 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b eturns SQLITE_OK
f56f0 20 62 75 74 20 6c 65 61 76 65 73 0a 2a 2a 20 2a but leaves.** *
f5700 70 46 64 20 73 65 74 20 74 6f 20 61 20 6e 65 67 pFd set to a neg
f5710 61 74 69 76 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a ative number..**
f5720 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b .** If SQLITE_OK
f5730 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 is returned, th
f5740 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 e caller is resp
f5750 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73 onsible for clos
f5760 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 ing.** the file
f5770 64 65 73 63 72 69 70 74 6f 72 20 2a 70 46 64 20 descriptor *pFd
f5780 75 73 69 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a using close()..*
f5790 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 /.static int ope
f57a0 6e 44 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 nDirectory(const
f57b0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
f57c0 2c 20 69 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69 , int *pFd){. i
f57d0 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 66 64 20 nt ii;. int fd
f57e0 3d 20 2d 31 3b 0a 20 20 63 68 61 72 20 7a 44 69 = -1;. char zDi
f57f0 72 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 rname[MAX_PATHNA
f5800 4d 45 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 ME+1];.. sqlite
f5810 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 3_snprintf(MAX_P
f5820 41 54 48 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d ATHNAME, zDirnam
f5830 65 2c 20 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61 e, "%s", zFilena
f5840 6d 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 28 69 me);. for(ii=(i
f5850 6e 74 29 73 74 72 6c 65 6e 28 7a 44 69 72 6e 61 nt)strlen(zDirna
f5860 6d 65 29 3b 20 69 69 3e 31 20 26 26 20 7a 44 69 me); ii>1 && zDi
f5870 72 6e 61 6d 65 5b 69 69 5d 21 3d 27 2f 27 3b 20 rname[ii]!='/';
f5880 69 69 2d 2d 29 3b 0a 20 20 69 66 28 20 69 69 3e ii--);. if( ii>
f5890 30 20 29 7b 0a 20 20 20 20 7a 44 69 72 6e 61 6d 0 ){. zDirnam
f58a0 65 5b 69 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 e[ii] = '\0';.
f58b0 20 20 66 64 20 3d 20 72 6f 62 75 73 74 5f 6f 70 fd = robust_op
f58c0 65 6e 28 7a 44 69 72 6e 61 6d 65 2c 20 4f 5f 52 en(zDirname, O_R
f58d0 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41 52 59 2c 20 DONLY|O_BINARY,
f58e0 30 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3e 3d 0);. if( fd>=
f58f0 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 0 ){. OSTRA
f5900 43 45 28 28 22 4f 50 45 4e 44 49 52 20 25 2d 33 CE(("OPENDIR %-3
f5910 64 20 25 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 d %s\n", fd, zDi
f5920 72 6e 61 6d 65 29 29 3b 0a 20 20 20 20 7d 0a 20 rname));. }.
f5930 20 7d 0a 20 20 2a 70 46 64 20 3d 20 66 64 3b 0a }. *pFd = fd;.
f5940 20 20 72 65 74 75 72 6e 20 28 66 64 3e 3d 30 3f return (fd>=0?
f5950 53 51 4c 49 54 45 5f 4f 4b 3a 75 6e 69 78 4c 6f SQLITE_OK:unixLo
f5960 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 43 41 gError(SQLITE_CA
f5970 4e 54 4f 50 45 4e 5f 42 4b 50 54 2c 20 22 6f 70 NTOPEN_BKPT, "op
f5980 65 6e 22 2c 20 7a 44 69 72 6e 61 6d 65 29 29 3b en", zDirname));
f5990 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 .}../*.** Make s
f59a0 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 ure all writes t
f59b0 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 o a particular f
f59c0 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 ile are committe
f59d0 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a d to disk..**.**
f59e0 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 If dataOnly==0
f59f0 74 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66 69 then both the fi
f5a00 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69 74 le itself and it
f5a10 73 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c 65 s metadata (file
f5a20 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 73 .** size, access
f5a30 20 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65 20 time, etc) are
f5a40 73 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74 61 synced. If data
f5a50 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c Only!=0 then onl
f5a60 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61 y the.** file da
f5a70 74 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a ta is synced..**
f5a80 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c 20 .** Under Unix,
f5a90 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 also make sure t
f5aa0 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f 72 hat the director
f5ab0 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 y entry for the
f5ac0 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e file.** has been
f5ad0 20 63 72 65 61 74 65 64 20 62 79 20 66 73 79 6e created by fsyn
f5ae0 63 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63 74 c-ing the direct
f5af0 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ory that contain
f5b00 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 s the file..** I
f5b10 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74 f we do not do t
f5b20 68 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f 75 his and we encou
f5b30 6e 74 65 72 20 61 20 70 6f 77 65 72 20 66 61 69 nter a power fai
f5b40 6c 75 72 65 2c 20 74 68 65 20 64 69 72 65 63 74 lure, the direct
f5b50 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 ory.** entry for
f5b60 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 the journal mig
f5b70 68 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74 ht not exist aft
f5b80 65 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54 er we reboot. T
f5b90 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74 he next.** SQLit
f5ba0 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 e to access the
f5bb0 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e file will not kn
f5bc0 6f 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 ow that the jour
f5bd0 6e 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 61 nal exists (beca
f5be0 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 use.** the direc
f5bf0 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 tory entry for t
f5c00 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e he journal was n
f5c10 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61 6e ever created) an
f5c20 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f d the transactio
f5c30 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f n.** will not ro
f5c40 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 62 ll back - possib
f5c50 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 61 ly leading to da
f5c60 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f tabase corruptio
f5c70 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 n..*/.static int
f5c80 20 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 65 unixSync(sqlite
f5c90 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
f5ca0 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 flags){. int rc
f5cb0 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 ;. unixFile *pF
f5cc0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
f5cd0 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61 )id;.. int isDa
f5ce0 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26 taOnly = (flags&
f5cf0 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 SQLITE_SYNC_DATA
f5d00 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 46 ONLY);. int isF
f5d10 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 73 ullsync = (flags
f5d20 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 &0x0F)==SQLITE_S
f5d30 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 YNC_FULL;.. /*
f5d40 43 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f Check that one o
f5d50 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f f SQLITE_SYNC_NO
f5d60 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73 RMAL or FULL was
f5d70 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 73 passed */. ass
f5d80 65 72 74 28 28 66 6c 61 67 73 26 30 78 30 46 29 ert((flags&0x0F)
f5d90 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f ==SQLITE_SYNC_NO
f5da0 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66 RMAL. || (f
f5db0 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 lags&0x0F)==SQLI
f5dc0 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 TE_SYNC_FULL. )
f5dd0 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e ;.. /* Unix can
f5de0 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79 not, but some sy
f5df0 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e stems may return
f5e00 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f SQLITE_FULL fro
f5e10 6d 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20 2a m here. This. *
f5e20 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 73 * line is to tes
f5e30 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f 20 t that doing so
f5e40 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 61 does not cause a
f5e50 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a ny problems.. *
f5e60 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b /. SimulateDisk
f5e70 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72 fullError( retur
f5e80 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b n SQLITE_FULL );
f5e90 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c .. assert( pFil
f5ea0 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 e );. OSTRACE((
f5eb0 22 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22 "SYNC %-3d\n"
f5ec0 2c 20 70 46 69 6c 65 2d 3e 68 29 29 3b 0a 20 20 , pFile->h));.
f5ed0 72 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 rc = full_fsync(
f5ee0 70 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c pFile->h, isFull
f5ef0 73 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 sync, isDataOnly
f5f00 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
f5f10 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 rror( rc=1 );.
f5f20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 46 if( rc ){. pF
f5f30 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
f5f40 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 errno;. retu
f5f50 72 6e 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 rn unixLogError(
f5f60 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 SQLITE_IOERR_FSY
f5f70 4e 43 2c 20 22 66 75 6c 6c 5f 66 73 79 6e 63 22 NC, "full_fsync"
f5f80 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29 3b , pFile->zPath);
f5f90 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 73 6f 20 . }.. /* Also
f5fa0 66 73 79 6e 63 20 74 68 65 20 64 69 72 65 63 74 fsync the direct
f5fb0 6f 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ory containing t
f5fc0 68 65 20 66 69 6c 65 20 69 66 20 74 68 65 20 44 he file if the D
f5fd0 49 52 53 59 4e 43 20 66 6c 61 67 0a 20 20 2a 2a IRSYNC flag. **
f5fe0 20 69 73 20 73 65 74 2e 20 20 54 68 69 73 20 69 is set. This i
f5ff0 73 20 61 20 6f 6e 65 2d 74 69 6d 65 20 6f 63 63 s a one-time occ
f6000 75 72 72 61 6e 63 65 2e 20 20 4d 61 6e 79 20 73 urrance. Many s
f6010 79 73 74 65 6d 73 20 28 65 78 61 6d 70 6c 65 73 ystems (examples
f6020 3a 20 41 49 58 29 0a 20 20 2a 2a 20 61 72 65 20 : AIX). ** are
f6030 75 6e 61 62 6c 65 20 74 6f 20 66 73 79 6e 63 20 unable to fsync
f6040 61 20 64 69 72 65 63 74 6f 72 79 2c 20 73 6f 20 a directory, so
f6050 69 67 6e 6f 72 65 20 65 72 72 6f 72 73 20 6f 6e ignore errors on
f6060 20 74 68 65 20 66 73 79 6e 63 2e 0a 20 20 2a 2f the fsync.. */
f6070 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 63 74 . if( pFile->ct
f6080 72 6c 46 6c 61 67 73 20 26 20 55 4e 49 58 46 49 rlFlags & UNIXFI
f6090 4c 45 5f 44 49 52 53 59 4e 43 20 29 7b 0a 20 20 LE_DIRSYNC ){.
f60a0 20 20 69 6e 74 20 64 69 72 66 64 3b 0a 20 20 20 int dirfd;.
f60b0 20 4f 53 54 52 41 43 45 28 28 22 44 49 52 53 59 OSTRACE(("DIRSY
f60c0 4e 43 20 25 73 20 28 68 61 76 65 5f 66 75 6c 6c NC %s (have_full
f60d0 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 79 6e fsync=%d fullsyn
f60e0 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d c=%d)\n", pFile-
f60f0 3e 7a 50 61 74 68 2c 0a 20 20 20 20 20 20 20 20 >zPath,.
f6100 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 HAVE_FULLFSY
f6110 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 29 29 NC, isFullsync))
f6120 3b 0a 20 20 20 20 72 63 20 3d 20 6f 73 4f 70 65 ;. rc = osOpe
f6130 6e 44 69 72 65 63 74 6f 72 79 28 70 46 69 6c 65 nDirectory(pFile
f6140 2d 3e 7a 50 61 74 68 2c 20 26 64 69 72 66 64 29 ->zPath, &dirfd)
f6150 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
f6160 4c 49 54 45 5f 4f 4b 20 26 26 20 64 69 72 66 64 LITE_OK && dirfd
f6170 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 75 6c >=0 ){. ful
f6180 6c 5f 66 73 79 6e 63 28 64 69 72 66 64 2c 20 30 l_fsync(dirfd, 0
f6190 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 6f 62 75 , 0);. robu
f61a0 73 74 5f 63 6c 6f 73 65 28 70 46 69 6c 65 2c 20 st_close(pFile,
f61b0 64 69 72 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 dirfd, __LINE__)
f61c0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
f61d0 72 63 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f rc==SQLITE_CANTO
f61e0 50 45 4e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 PEN ){. rc
f61f0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
f6200 20 7d 0a 20 20 20 20 70 46 69 6c 65 2d 3e 63 74 }. pFile->ct
f6210 72 6c 46 6c 61 67 73 20 26 3d 20 7e 55 4e 49 58 rlFlags &= ~UNIX
f6220 46 49 4c 45 5f 44 49 52 53 59 4e 43 3b 0a 20 20 FILE_DIRSYNC;.
f6230 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
f6240 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 ../*.** Truncate
f6250 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f an open file to
f6260 20 61 20 73 70 65 63 69 66 69 65 64 20 73 69 7a a specified siz
f6270 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 e.*/.static int
f6280 75 6e 69 78 54 72 75 6e 63 61 74 65 28 73 71 6c unixTruncate(sql
f6290 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
f62a0 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 75 6e 69 64 nByte){. uni
f62b0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
f62c0 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20 unixFile *)id;.
f62d0 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 int rc;. asser
f62e0 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 53 69 t( pFile );. Si
f62f0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
f6300 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
f6310 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 0a RR_TRUNCATE );..
f6320 20 20 2f 2a 20 49 66 20 74 68 65 20 75 73 65 72 /* If the user
f6330 20 68 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 has configured
f6340 61 20 63 68 75 6e 6b 2d 73 69 7a 65 20 66 6f 72 a chunk-size for
f6350 20 74 68 69 73 20 66 69 6c 65 2c 20 74 72 75 6e this file, trun
f6360 63 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 66 69 cate the. ** fi
f6370 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 6f le so that it co
f6380 6e 73 69 73 74 73 20 6f 66 20 61 6e 20 69 6e 74 nsists of an int
f6390 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 63 eger number of c
f63a0 68 75 6e 6b 73 20 28 69 2e 65 2e 20 74 68 65 0a hunks (i.e. the.
f63b0 20 20 2a 2a 20 61 63 74 75 61 6c 20 66 69 6c 65 ** actual file
f63c0 20 73 69 7a 65 20 61 66 74 65 72 20 74 68 65 20 size after the
f63d0 6f 70 65 72 61 74 69 6f 6e 20 6d 61 79 20 62 65 operation may be
f63e0 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 larger than the
f63f0 20 72 65 71 75 65 73 74 65 64 0a 20 20 2a 2a 20 requested. **
f6400 73 69 7a 65 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 size).. */. if
f6410 28 20 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b ( pFile->szChunk
f6420 3e 30 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 >0 ){. nByte
f6430 3d 20 28 28 6e 42 79 74 65 20 2b 20 70 46 69 6c = ((nByte + pFil
f6440 65 2d 3e 73 7a 43 68 75 6e 6b 20 2d 20 31 29 2f e->szChunk - 1)/
f6450 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 29 20 pFile->szChunk)
f6460 2a 20 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b * pFile->szChunk
f6470 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 72 6f ;. }.. rc = ro
f6480 62 75 73 74 5f 66 74 72 75 6e 63 61 74 65 28 70 bust_ftruncate(p
f6490 46 69 6c 65 2d 3e 68 2c 20 28 6f 66 66 5f 74 29 File->h, (off_t)
f64a0 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 72 63 nByte);. if( rc
f64b0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
f64c0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
f64d0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69 ;. return uni
f64e0 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 xLogError(SQLITE
f64f0 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 2c _IOERR_TRUNCATE,
f6500 20 22 66 74 72 75 6e 63 61 74 65 22 2c 20 70 46 "ftruncate", pF
f6510 69 6c 65 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 7d ile->zPath);. }
f6520 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53 51 4c else{.#ifdef SQL
f6530 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 2f 2a ITE_DEBUG. /*
f6540 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 If we are doing
f6550 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 a normal write
f6560 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69 to a database fi
f6570 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74 le (as opposed t
f6580 6f 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 61 o. ** doing a
f6590 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
f65a0 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 lback or a write
f65b0 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 to some file ot
f65c0 68 65 72 20 74 68 61 6e 20 61 0a 20 20 20 20 2a her than a. *
f65d0 2a 20 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 73 * normal databas
f65e0 65 20 66 69 6c 65 29 20 61 6e 64 20 77 65 20 74 e file) and we t
f65f0 72 75 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 runcate the file
f6600 20 74 6f 20 7a 65 72 6f 20 6c 65 6e 67 74 68 2c to zero length,
f6610 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 65 66 66 . ** that eff
f6620 65 63 74 69 76 65 6c 79 20 75 70 64 61 74 65 73 ectively updates
f6630 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e the change coun
f6640 74 65 72 2e 20 20 54 68 69 73 20 6d 69 67 68 74 ter. This might
f6650 20 68 61 70 70 65 6e 0a 20 20 20 20 2a 2a 20 77 happen. ** w
f6660 68 65 6e 20 72 65 73 74 6f 72 69 6e 67 20 61 20 hen restoring a
f6670 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 74 database using t
f6680 68 65 20 62 61 63 6b 75 70 20 41 50 49 20 66 72 he backup API fr
f6690 6f 6d 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 om a zero-length
f66a0 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63 65 2e 0a . ** source..
f66b0 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
f66c0 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 File->inNormalWr
f66d0 69 74 65 20 26 26 20 6e 42 79 74 65 3d 3d 30 20 ite && nByte==0
f66e0 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
f66f0 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 20 3d 20 transCntrChng =
f6700 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 1;. }.#endif.
f6710 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
f6720 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a TE_OK;. }.}../*
f6730 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 .** Determine th
f6740 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f e current size o
f6750 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 f a file in byte
f6760 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 s.*/.static int
f6770 75 6e 69 78 46 69 6c 65 53 69 7a 65 28 73 71 6c unixFileSize(sql
f6780 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
f6790 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 64 *pSize){. in
f67a0 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20 73 t rc;. struct s
f67b0 74 61 74 20 62 75 66 3b 0a 20 20 61 73 73 65 72 tat buf;. asser
f67c0 74 28 20 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 t( id );. rc =
f67d0 6f 73 46 73 74 61 74 28 28 28 75 6e 69 78 46 69 osFstat(((unixFi
f67e0 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 26 62 75 66 le*)id)->h, &buf
f67f0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
f6800 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 rror( rc=1 );.
f6810 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 if( rc!=0 ){.
f6820 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
f6830 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
f6840 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 rno;. return
f6850 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 SQLITE_IOERR_FST
f6860 41 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 AT;. }. *pSize
f6870 20 3d 20 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a = buf.st_size;.
f6880 0a 20 20 2f 2a 20 57 68 65 6e 20 6f 70 65 6e 69 . /* When openi
f6890 6e 67 20 61 20 7a 65 72 6f 2d 73 69 7a 65 20 64 ng a zero-size d
f68a0 61 74 61 62 61 73 65 2c 20 74 68 65 20 66 69 6e atabase, the fin
f68b0 64 49 6e 6f 64 65 49 6e 66 6f 28 29 20 70 72 6f dInodeInfo() pro
f68c0 63 65 64 75 72 65 0a 20 20 2a 2a 20 77 72 69 74 cedure. ** writ
f68d0 65 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 es a single byte
f68e0 20 69 6e 74 6f 20 74 68 61 74 20 66 69 6c 65 20 into that file
f68f0 69 6e 20 6f 72 64 65 72 20 74 6f 20 77 6f 72 6b in order to work
f6900 20 61 72 6f 75 6e 64 20 61 20 62 75 67 0a 20 20 around a bug.
f6910 2a 2a 20 69 6e 20 74 68 65 20 4f 53 2d 58 20 6d ** in the OS-X m
f6920 73 64 6f 73 20 66 69 6c 65 73 79 73 74 65 6d 2e sdos filesystem.
f6930 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 In order to av
f6940 6f 69 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 oid problems wit
f6950 68 20 75 70 70 65 72 0a 20 20 2a 2a 20 6c 61 79 h upper. ** lay
f6960 65 72 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 ers, we need to
f6970 72 65 70 6f 72 74 20 74 68 69 73 20 66 69 6c 65 report this file
f6980 20 73 69 7a 65 20 61 73 20 7a 65 72 6f 20 65 76 size as zero ev
f6990 65 6e 20 74 68 6f 75 67 68 20 69 74 20 69 73 0a en though it is.
f69a0 20 20 2a 2a 20 72 65 61 6c 6c 79 20 31 2e 20 20 ** really 1.
f69b0 20 54 69 63 6b 65 74 20 23 33 32 36 30 2e 0a 20 Ticket #3260..
f69c0 20 2a 2f 0a 20 20 69 66 28 20 2a 70 53 69 7a 65 */. if( *pSize
f69d0 3d 3d 31 20 29 20 2a 70 53 69 7a 65 20 3d 20 30 ==1 ) *pSize = 0
f69e0 3b 0a 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c ;... return SQL
f69f0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 ITE_OK;.}..#if S
f6a00 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
f6a10 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 KING_STYLE && de
f6a20 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
f6a30 0a 2f 2a 0a 2a 2a 20 48 61 6e 64 6c 65 72 20 66 ./*.** Handler f
f6a40 6f 72 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 or proxy-locking
f6a50 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 76 65 file-control ve
f6a60 72 62 73 2e 20 20 44 65 66 69 6e 65 64 20 62 65 rbs. Defined be
f6a70 6c 6f 77 20 69 6e 20 74 68 65 0a 2a 2a 20 70 72 low in the.** pr
f6a80 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 64 oxying locking d
f6a90 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 ivision..*/.stat
f6aa0 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69 6c 65 ic int proxyFile
f6ab0 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f Control(sqlite3_
f6ac0 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 file*,int,void*)
f6ad0 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a ;.#endif../* .**
f6ae0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
f6af0 73 20 63 61 6c 6c 65 64 20 74 6f 20 68 61 6e 64 s called to hand
f6b00 6c 65 20 74 68 65 20 53 51 4c 49 54 45 5f 46 43 le the SQLITE_FC
f6b10 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e 54 20 0a 2a NTL_SIZE_HINT .*
f6b20 2a 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 6f * file-control o
f6b30 70 65 72 61 74 69 6f 6e 2e 20 20 45 6e 6c 61 72 peration. Enlar
f6b40 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ge the database
f6b50 74 6f 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a to nBytes in siz
f6b60 65 0a 2a 2a 20 28 72 6f 75 6e 64 65 64 20 75 70 e.** (rounded up
f6b70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 63 68 75 to the next chu
f6b80 6e 6b 2d 73 69 7a 65 29 2e 20 20 49 66 20 74 68 nk-size). If th
f6b90 65 20 64 61 74 61 62 61 73 65 20 69 73 20 61 6c e database is al
f6ba0 72 65 61 64 79 0a 2a 2a 20 6e 42 79 74 65 73 20 ready.** nBytes
f6bb0 6f 72 20 6c 61 72 67 65 72 2c 20 74 68 69 73 20 or larger, this
f6bc0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
f6bd0 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e op..*/.static in
f6be0 74 20 66 63 6e 74 6c 53 69 7a 65 48 69 6e 74 28 t fcntlSizeHint(
f6bf0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c unixFile *pFile,
f6c00 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 69 i64 nByte){. i
f6c10 66 28 20 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e f( pFile->szChun
f6c20 6b 3e 30 20 29 7b 0a 20 20 20 20 69 36 34 20 6e k>0 ){. i64 n
f6c30 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
f6c40 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 71 75 /* Requ
f6c50 69 72 65 64 20 66 69 6c 65 20 73 69 7a 65 20 2a ired file size *
f6c60 2f 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 61 /. struct sta
f6c70 74 20 62 75 66 3b 20 20 20 20 20 20 20 20 20 20 t buf;
f6c80 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 68 /* Used to h
f6c90 6f 6c 64 20 72 65 74 75 72 6e 20 76 61 6c 75 65 old return value
f6ca0 73 20 6f 66 20 66 73 74 61 74 28 29 20 2a 2f 0a s of fstat() */.
f6cb0 20 20 20 0a 20 20 20 20 69 66 28 20 6f 73 46 73 . if( osFs
f6cc0 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26 62 tat(pFile->h, &b
f6cd0 75 66 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c uf) ) return SQL
f6ce0 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b ITE_IOERR_FSTAT;
f6cf0 0a 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 28 28 .. nSize = ((
f6d00 6e 42 79 74 65 2b 70 46 69 6c 65 2d 3e 73 7a 43 nByte+pFile->szC
f6d10 68 75 6e 6b 2d 31 29 20 2f 20 70 46 69 6c 65 2d hunk-1) / pFile-
f6d20 3e 73 7a 43 68 75 6e 6b 29 20 2a 20 70 46 69 6c >szChunk) * pFil
f6d30 65 2d 3e 73 7a 43 68 75 6e 6b 3b 0a 20 20 20 20 e->szChunk;.
f6d40 69 66 28 20 6e 53 69 7a 65 3e 28 69 36 34 29 62 if( nSize>(i64)b
f6d50 75 66 2e 73 74 5f 73 69 7a 65 20 29 7b 0a 0a 23 uf.st_size ){..#
f6d60 69 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f if defined(HAVE_
f6d70 50 4f 53 49 58 5f 46 41 4c 4c 4f 43 41 54 45 29 POSIX_FALLOCATE)
f6d80 20 26 26 20 48 41 56 45 5f 50 4f 53 49 58 5f 46 && HAVE_POSIX_F
f6d90 41 4c 4c 4f 43 41 54 45 0a 20 20 20 20 20 20 2f ALLOCATE. /
f6da0 2a 20 54 68 65 20 63 6f 64 65 20 62 65 6c 6f 77 * The code below
f6db0 20 69 73 20 68 61 6e 64 6c 69 6e 67 20 74 68 65 is handling the
f6dc0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 return value of
f6dd0 20 6f 73 46 61 6c 6c 6f 63 61 74 65 28 29 20 0a osFallocate() .
f6de0 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 65 63 74 ** correct
f6df0 6c 79 2e 20 70 6f 73 69 78 5f 66 61 6c 6c 6f 63 ly. posix_falloc
f6e00 61 74 65 28 29 20 69 73 20 64 65 66 69 6e 65 64 ate() is defined
f6e10 20 74 6f 20 22 72 65 74 75 72 6e 73 20 7a 65 72 to "returns zer
f6e20 6f 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 0a 20 o on success, .
f6e30 20 20 20 20 20 2a 2a 20 6f 72 20 61 6e 20 65 72 ** or an er
f6e40 72 6f 72 20 6e 75 6d 62 65 72 20 6f 6e 20 20 66 ror number on f
f6e50 61 69 6c 75 72 65 22 2e 20 53 65 65 20 74 68 65 ailure". See the
f6e60 20 6d 61 6e 70 61 67 65 20 66 6f 72 20 64 65 74 manpage for det
f6e70 61 69 6c 73 2e 20 2a 2f 0a 20 20 20 20 20 20 69 ails. */. i
f6e80 6e 74 20 65 72 72 3b 0a 20 20 20 20 20 20 64 6f nt err;. do
f6e90 7b 0a 20 20 20 20 20 20 20 20 65 72 72 20 3d 20 {. err =
f6ea0 6f 73 46 61 6c 6c 6f 63 61 74 65 28 70 46 69 6c osFallocate(pFil
f6eb0 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 73 69 7a e->h, buf.st_siz
f6ec0 65 2c 20 6e 53 69 7a 65 2d 62 75 66 2e 73 74 5f e, nSize-buf.st_
f6ed0 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 77 68 size);. }wh
f6ee0 69 6c 65 28 20 65 72 72 3d 3d 45 49 4e 54 52 20 ile( err==EINTR
f6ef0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 );. if( err
f6f00 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
f6f10 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 23 65 _IOERR_WRITE;.#e
f6f20 6c 73 65 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 lse. /* If
f6f30 74 68 65 20 4f 53 20 64 6f 65 73 20 6e 6f 74 20 the OS does not
f6f40 68 61 76 65 20 70 6f 73 69 78 5f 66 61 6c 6c 6f have posix_fallo
f6f50 63 61 74 65 28 29 2c 20 66 61 6b 65 20 69 74 2e cate(), fake it.
f6f60 20 46 69 72 73 74 20 75 73 65 0a 20 20 20 20 20 First use.
f6f70 20 2a 2a 20 66 74 72 75 6e 63 61 74 65 28 29 20 ** ftruncate()
f6f80 74 6f 20 73 65 74 20 74 68 65 20 66 69 6c 65 20 to set the file
f6f90 73 69 7a 65 2c 20 74 68 65 6e 20 77 72 69 74 65 size, then write
f6fa0 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 74 a single byte t
f6fb0 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c o. ** the l
f6fc0 61 73 74 20 62 79 74 65 20 69 6e 20 65 61 63 68 ast byte in each
f6fd0 20 62 6c 6f 63 6b 20 77 69 74 68 69 6e 20 74 68 block within th
f6fe0 65 20 65 78 74 65 6e 64 65 64 20 72 65 67 69 6f e extended regio
f6ff0 6e 2e 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a n. This. **
f7000 20 69 73 20 74 68 65 20 73 61 6d 65 20 74 65 63 is the same tec
f7010 68 6e 69 71 75 65 20 75 73 65 64 20 62 79 20 67 hnique used by g
f7020 6c 69 62 63 20 74 6f 20 69 6d 70 6c 65 6d 65 6e libc to implemen
f7030 74 20 70 6f 73 69 78 5f 66 61 6c 6c 6f 63 61 74 t posix_fallocat
f7040 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 e(). ** on
f7050 73 79 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 systems that do
f7060 6e 6f 74 20 68 61 76 65 20 61 20 72 65 61 6c 20 not have a real
f7070 66 61 6c 6c 6f 63 61 74 65 28 29 20 73 79 73 74 fallocate() syst
f7080 65 6d 20 63 61 6c 6c 2e 0a 20 20 20 20 20 20 2a em call.. *
f7090 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 6c 6b /. int nBlk
f70a0 20 3d 20 62 75 66 2e 73 74 5f 62 6c 6b 73 69 7a = buf.st_blksiz
f70b0 65 3b 20 20 2f 2a 20 46 69 6c 65 2d 73 79 73 74 e; /* File-syst
f70c0 65 6d 20 62 6c 6f 63 6b 20 73 69 7a 65 20 2a 2f em block size */
f70d0 0a 20 20 20 20 20 20 69 36 34 20 69 57 72 69 74 . i64 iWrit
f70e0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
f70f0 20 20 20 2f 2a 20 4e 65 78 74 20 6f 66 66 73 65 /* Next offse
f7100 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 2a 2f t to write to */
f7110 0a 0a 20 20 20 20 20 20 69 66 28 20 72 6f 62 75 .. if( robu
f7120 73 74 5f 66 74 72 75 6e 63 61 74 65 28 70 46 69 st_ftruncate(pFi
f7130 6c 65 2d 3e 68 2c 20 6e 53 69 7a 65 29 20 29 7b le->h, nSize) ){
f7140 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
f7150 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
f7160 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 o;. retur
f7170 6e 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 53 n unixLogError(S
f7180 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e QLITE_IOERR_TRUN
f7190 43 41 54 45 2c 20 22 66 74 72 75 6e 63 61 74 65 CATE, "ftruncate
f71a0 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29 ", pFile->zPath)
f71b0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
f71c0 69 57 72 69 74 65 20 3d 20 28 28 62 75 66 2e 73 iWrite = ((buf.s
f71d0 74 5f 73 69 7a 65 20 2b 20 32 2a 6e 42 6c 6b 20 t_size + 2*nBlk
f71e0 2d 20 31 29 2f 6e 42 6c 6b 29 2a 6e 42 6c 6b 2d - 1)/nBlk)*nBlk-
f71f0 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 1;. while(
f7200 69 57 72 69 74 65 3c 6e 53 69 7a 65 20 29 7b 0a iWrite<nSize ){.
f7210 20 20 20 20 20 20 20 20 69 6e 74 20 6e 57 72 69 int nWri
f7220 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72 69 74 te = seekAndWrit
f7230 65 28 70 46 69 6c 65 2c 20 69 57 72 69 74 65 2c e(pFile, iWrite,
f7240 20 22 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 "", 1);.
f7250 20 69 66 28 20 6e 57 72 69 74 65 21 3d 31 20 29 if( nWrite!=1 )
f7260 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
f7270 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 OERR_WRITE;.
f7280 20 20 20 20 69 57 72 69 74 65 20 2b 3d 20 6e 42 iWrite += nB
f7290 6c 6b 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 lk;. }.#end
f72a0 69 66 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 if. }. }..
f72b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f72c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 2a 70 ;.}../*.** If *p
f72d0 41 72 67 20 69 73 20 69 6e 69 74 69 74 69 61 6c Arg is inititial
f72e0 6c 79 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e ly negative then
f72f0 20 74 68 69 73 20 69 73 20 61 20 71 75 65 72 79 this is a query
f7300 2e 20 20 53 65 74 20 2a 70 41 72 67 20 74 6f 0a . Set *pArg to.
f7310 2a 2a 20 31 20 6f 72 20 30 20 64 65 70 65 6e 64 ** 1 or 0 depend
f7320 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 6f ing on whether o
f7330 72 20 6e 6f 74 20 62 69 74 20 6d 61 73 6b 20 6f r not bit mask o
f7340 66 20 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 f pFile->ctrlFla
f7350 67 73 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a gs is set..**.**
f7360 20 49 66 20 2a 70 41 72 67 20 69 73 20 30 20 6f If *pArg is 0 o
f7370 72 20 31 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 r 1, then clear
f7380 6f 72 20 73 65 74 20 74 68 65 20 6d 61 73 6b 20 or set the mask
f7390 62 69 74 20 6f 66 20 70 46 69 6c 65 2d 3e 63 74 bit of pFile->ct
f73a0 72 6c 46 6c 61 67 73 2e 0a 2a 2f 0a 73 74 61 74 rlFlags..*/.stat
f73b0 69 63 20 76 6f 69 64 20 75 6e 69 78 4d 6f 64 65 ic void unixMode
f73c0 42 69 74 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 Bit(unixFile *pF
f73d0 69 6c 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 ile, unsigned ch
f73e0 61 72 20 6d 61 73 6b 2c 20 69 6e 74 20 2a 70 41 ar mask, int *pA
f73f0 72 67 29 7b 0a 20 20 69 66 28 20 2a 70 41 72 67 rg){. if( *pArg
f7400 3c 30 20 29 7b 0a 20 20 20 20 2a 70 41 72 67 20 <0 ){. *pArg
f7410 3d 20 28 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c = (pFile->ctrlFl
f7420 61 67 73 20 26 20 6d 61 73 6b 29 21 3d 30 3b 0a ags & mask)!=0;.
f7430 20 20 7d 65 6c 73 65 20 69 66 28 20 28 2a 70 41 }else if( (*pA
f7440 72 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 46 rg)==0 ){. pF
f7450 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20 26 ile->ctrlFlags &
f7460 3d 20 7e 6d 61 73 6b 3b 0a 20 20 7d 65 6c 73 65 = ~mask;. }else
f7470 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 63 74 72 {. pFile->ctr
f7480 6c 46 6c 61 67 73 20 7c 3d 20 6d 61 73 6b 3b 0a lFlags |= mask;.
f7490 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 }.}../*.** Inf
f74a0 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e ormation and con
f74b0 74 72 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 trol of an open
f74c0 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a file handle..*/.
f74d0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 static int unixF
f74e0 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 ileControl(sqlit
f74f0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
f7500 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 op, void *pArg)
f7510 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 {. unixFile *pF
f7520 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
f7530 29 69 64 3b 0a 20 20 73 77 69 74 63 68 28 20 6f )id;. switch( o
f7540 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 p ){. case SQ
f7550 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 LITE_FCNTL_LOCKS
f7560 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 TATE: {. *(
f7570 69 6e 74 2a 29 70 41 72 67 20 3d 20 70 46 69 6c int*)pArg = pFil
f7580 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 e->eFileLock;.
f7590 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
f75a0 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 E_OK;. }.
f75b0 63 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 case SQLITE_LAST
f75c0 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 _ERRNO: {.
f75d0 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 70 46 *(int*)pArg = pF
f75e0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a ile->lastErrno;.
f75f0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
f7600 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 ITE_OK;. }.
f7610 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 case SQLITE_FC
f7620 4e 54 4c 5f 43 48 55 4e 4b 5f 53 49 5a 45 3a 20 NTL_CHUNK_SIZE:
f7630 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 {. pFile->s
f7640 7a 43 68 75 6e 6b 20 3d 20 2a 28 69 6e 74 20 2a zChunk = *(int *
f7650 29 70 41 72 67 3b 0a 20 20 20 20 20 20 72 65 74 )pArg;. ret
f7660 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
f7670 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 }. case SQ
f7680 4c 49 54 45 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f LITE_FCNTL_SIZE_
f7690 48 49 4e 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e HINT: {. in
f76a0 74 20 72 63 3b 0a 20 20 20 20 20 20 53 69 6d 75 t rc;. Simu
f76b0 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
f76c0 6e 28 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d n(1);. rc =
f76d0 20 66 63 6e 74 6c 53 69 7a 65 48 69 6e 74 28 70 fcntlSizeHint(p
f76e0 46 69 6c 65 2c 20 2a 28 69 36 34 20 2a 29 70 41 File, *(i64 *)pA
f76f0 72 67 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c rg);. Simul
f7700 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e ateIOErrorBenign
f7710 28 30 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 (0);. retur
f7720 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
f7730 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 case SQLITE_FCNT
f7740 4c 5f 50 45 52 53 49 53 54 5f 57 41 4c 3a 20 7b L_PERSIST_WAL: {
f7750 0a 20 20 20 20 20 20 75 6e 69 78 4d 6f 64 65 42 . unixModeB
f7760 69 74 28 70 46 69 6c 65 2c 20 55 4e 49 58 46 49 it(pFile, UNIXFI
f7770 4c 45 5f 50 45 52 53 49 53 54 5f 57 41 4c 2c 20 LE_PERSIST_WAL,
f7780 28 69 6e 74 2a 29 70 41 72 67 29 3b 0a 20 20 20 (int*)pArg);.
f7790 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
f77a0 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 _OK;. }. c
f77b0 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ase SQLITE_FCNTL
f77c0 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 _POWERSAFE_OVERW
f77d0 52 49 54 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e RITE: {. un
f77e0 69 78 4d 6f 64 65 42 69 74 28 70 46 69 6c 65 2c ixModeBit(pFile,
f77f0 20 55 4e 49 58 46 49 4c 45 5f 50 53 4f 57 2c 20 UNIXFILE_PSOW,
f7800 28 69 6e 74 2a 29 70 41 72 67 29 3b 0a 20 20 20 (int*)pArg);.
f7810 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
f7820 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 _OK;. }. c
f7830 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ase SQLITE_FCNTL
f7840 5f 56 46 53 4e 41 4d 45 3a 20 7b 0a 20 20 20 20 _VFSNAME: {.
f7850 20 20 2a 28 63 68 61 72 2a 2a 29 70 41 72 67 20 *(char**)pArg
f7860 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 = sqlite3_mprint
f7870 66 28 22 25 73 22 2c 20 70 46 69 6c 65 2d 3e 70 f("%s", pFile->p
f7880 56 66 73 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 Vfs->zName);.
f7890 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
f78a0 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 _OK;. }.#ifde
f78b0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
f78c0 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 /* The pager
f78d0 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f calls this metho
f78e0 64 20 74 6f 20 73 69 67 6e 61 6c 20 74 68 61 74 d to signal that
f78f0 20 69 74 20 68 61 73 20 64 6f 6e 65 0a 20 20 20 it has done.
f7900 20 2a 2a 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61 ** a rollback a
f7910 6e 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61 nd that the data
f7920 62 61 73 65 20 69 73 20 74 68 65 72 65 66 6f 72 base is therefor
f7930 65 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a e unchanged and.
f7940 20 20 20 20 2a 2a 20 69 74 20 68 65 6e 63 65 20 ** it hence
f7950 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65 it is OK for the
f7960 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 68 61 transaction cha
f7970 6e 67 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 62 nge counter to b
f7980 65 0a 20 20 20 20 2a 2a 20 75 6e 63 68 61 6e 67 e. ** unchang
f7990 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 ed.. */. c
f79a0 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ase SQLITE_FCNTL
f79b0 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 3a 20 7b _DB_UNCHANGED: {
f79c0 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c . ((unixFil
f79d0 65 2a 29 69 64 29 2d 3e 64 62 55 70 64 61 74 65 e*)id)->dbUpdate
f79e0 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 = 0;. retu
f79f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
f7a00 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 }.#endif.#if S
f7a10 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
f7a20 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 KING_STYLE && de
f7a30 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
f7a40 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
f7a50 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _SET_LOCKPROXYFI
f7a60 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c LE:. case SQL
f7a70 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 ITE_GET_LOCKPROX
f7a80 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 72 YFILE: {. r
f7a90 65 74 75 72 6e 20 70 72 6f 78 79 46 69 6c 65 43 eturn proxyFileC
f7aa0 6f 6e 74 72 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 ontrol(id,op,pAr
f7ab0 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 g);. }.#endif
f7ac0 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c /* SQLITE_ENABL
f7ad0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
f7ae0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 && defined(__APP
f7af0 4c 45 5f 5f 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 LE__) */. }. r
f7b00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 54 eturn SQLITE_NOT
f7b10 46 4f 55 4e 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 FOUND;.}../*.**
f7b20 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f Return the secto
f7b30 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 r size in bytes
f7b40 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e of the underlyin
f7b50 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 g block device f
f7b60 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 or.** the specif
f7b70 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69 ied file. This i
f7b80 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 s almost always
f7b90 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d 512 bytes, but m
f7ba0 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 ay be.** larger
f7bb0 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73 for some devices
f7bc0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 ..**.** SQLite c
f7bd0 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73 ode assumes this
f7be0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 function cannot
f7bf0 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 fail. It also a
f7c00 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 ssumes that.** i
f7c10 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20 f two files are
f7c20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73 created in the s
f7c30 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 ame file-system
f7c40 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a directory (i.e..
f7c50 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e ** a database an
f7c60 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 d its journal fi
f7c70 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63 le) that the sec
f7c80 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 tor size will be
f7c90 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 the.** same for
f7ca0 20 62 6f 74 68 2e 0a 2a 2f 0a 23 69 66 6e 64 65 both..*/.#ifnde
f7cb0 66 20 5f 5f 51 4e 58 4e 54 4f 5f 5f 20 0a 73 74 f __QNXNTO__ .st
f7cc0 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 65 63 atic int unixSec
f7cd0 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f torSize(sqlite3_
f7ce0 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 29 7b 0a file *NotUsed){.
f7cf0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
f7d00 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 ER(NotUsed);. r
f7d10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 45 46 eturn SQLITE_DEF
f7d20 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 AULT_SECTOR_SIZE
f7d30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
f7d40 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
f7d50 76 65 72 73 69 6f 6e 20 6f 66 20 75 6e 69 78 53 version of unixS
f7d60 65 63 74 6f 72 53 69 7a 65 28 29 20 69 73 20 6f ectorSize() is o
f7d70 70 74 69 6d 69 7a 65 64 20 66 6f 72 20 51 4e 58 ptimized for QNX
f7d80 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 5f 51 4e ..*/.#ifdef __QN
f7d90 58 4e 54 4f 5f 5f 0a 23 69 6e 63 6c 75 64 65 20 XNTO__.#include
f7da0 3c 73 79 73 2f 64 63 6d 64 5f 62 6c 6b 2e 68 3e <sys/dcmd_blk.h>
f7db0 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 .#include <sys/s
f7dc0 74 61 74 76 66 73 2e 68 3e 0a 73 74 61 74 69 63 tatvfs.h>.static
f7dd0 20 69 6e 74 20 75 6e 69 78 53 65 63 74 6f 72 53 int unixSectorS
f7de0 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ize(sqlite3_file
f7df0 20 2a 69 64 29 7b 0a 20 20 75 6e 69 78 46 69 6c *id){. unixFil
f7e00 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
f7e10 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 66 28 20 File*)id;. if(
f7e20 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a pFile->sectorSiz
f7e30 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 73 74 e == 0 ){. st
f7e40 72 75 63 74 20 73 74 61 74 76 66 73 20 66 73 49 ruct statvfs fsI
f7e50 6e 66 6f 3b 0a 20 20 20 20 20 20 20 0a 20 20 20 nfo;. .
f7e60 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74 73 /* Set defaults
f7e70 20 66 6f 72 20 6e 6f 6e 2d 73 75 70 70 6f 72 74 for non-support
f7e80 65 64 20 66 69 6c 65 73 79 73 74 65 6d 73 20 2a ed filesystems *
f7e90 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 65 63 /. pFile->sec
f7ea0 74 6f 72 53 69 7a 65 20 3d 20 53 51 4c 49 54 45 torSize = SQLITE
f7eb0 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f _DEFAULT_SECTOR_
f7ec0 53 49 5a 45 3b 0a 20 20 20 20 70 46 69 6c 65 2d SIZE;. pFile-
f7ed0 3e 64 65 76 69 63 65 43 68 61 72 61 63 74 65 72 >deviceCharacter
f7ee0 69 73 74 69 63 73 20 3d 20 30 3b 0a 20 20 20 20 istics = 0;.
f7ef0 69 66 28 20 66 73 74 61 74 76 66 73 28 70 46 69 if( fstatvfs(pFi
f7f00 6c 65 2d 3e 68 2c 20 26 66 73 49 6e 66 6f 29 20 le->h, &fsInfo)
f7f10 3d 3d 20 2d 31 20 29 20 7b 0a 20 20 20 20 20 20 == -1 ) {.
f7f20 72 65 74 75 72 6e 20 70 46 69 6c 65 2d 3e 73 65 return pFile->se
f7f30 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 7d 0a ctorSize;. }.
f7f40 0a 20 20 20 20 69 66 28 20 21 73 74 72 63 6d 70 . if( !strcmp
f7f50 28 66 73 49 6e 66 6f 2e 66 5f 62 61 73 65 74 79 (fsInfo.f_basety
f7f60 70 65 2c 20 22 74 6d 70 22 29 20 29 20 7b 0a 20 pe, "tmp") ) {.
f7f70 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 65 63 74 pFile->sect
f7f80 6f 72 53 69 7a 65 20 3d 20 66 73 49 6e 66 6f 2e orSize = fsInfo.
f7f90 66 5f 62 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 f_bsize;. p
f7fa0 46 69 6c 65 2d 3e 64 65 76 69 63 65 43 68 61 72 File->deviceChar
f7fb0 61 63 74 65 72 69 73 74 69 63 73 20 3d 0a 20 20 acteristics =.
f7fc0 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 43 SQLITE_IOC
f7fd0 41 50 5f 41 54 4f 4d 49 43 34 4b 20 7c 20 20 20 AP_ATOMIC4K |
f7fe0 20 20 20 20 2f 2a 20 41 6c 6c 20 72 61 6d 20 66 /* All ram f
f7ff0 69 6c 65 73 79 73 74 65 6d 20 77 72 69 74 65 73 ilesystem writes
f8000 20 61 72 65 20 61 74 6f 6d 69 63 20 2a 2f 0a 20 are atomic */.
f8010 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f SQLITE_IO
f8020 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 CAP_SAFE_APPEND
f8030 7c 20 20 20 20 2f 2a 20 67 72 6f 77 69 6e 67 20 | /* growing
f8040 74 68 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f the file does no
f8050 74 20 6f 63 63 75 72 20 75 6e 74 69 6c 0a 20 20 t occur until.
f8060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8080 20 20 20 20 2a 2a 20 74 68 65 20 77 72 69 74 65 ** the write
f8090 20 73 75 63 63 65 65 64 73 20 2a 2f 0a 20 20 20 succeeds */.
f80a0 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 43 41 SQLITE_IOCA
f80b0 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 7c 20 20 P_SEQUENTIAL |
f80c0 20 20 20 2f 2a 20 54 68 65 20 72 61 6d 20 66 69 /* The ram fi
f80d0 6c 65 73 79 73 74 65 6d 20 68 61 73 20 6e 6f 20 lesystem has no
f80e0 77 72 69 74 65 20 62 65 68 69 6e 64 0a 20 20 20 write behind.
f80f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8110 20 20 20 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f ** so it is o
f8120 72 64 65 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 rdered */.
f8130 20 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 0;. }else i
f8140 66 28 20 73 74 72 73 74 72 28 66 73 49 6e 66 6f f( strstr(fsInfo
f8150 2e 66 5f 62 61 73 65 74 79 70 65 2c 20 22 65 74 .f_basetype, "et
f8160 66 73 22 29 20 29 7b 0a 20 20 20 20 20 20 70 46 fs") ){. pF
f8170 69 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 ile->sectorSize
f8180 3d 20 66 73 49 6e 66 6f 2e 66 5f 62 73 69 7a 65 = fsInfo.f_bsize
f8190 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 64 ;. pFile->d
f81a0 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
f81b0 74 69 63 73 20 3d 0a 20 20 20 20 20 20 20 20 2f tics =. /
f81c0 2a 20 65 74 66 73 20 63 6c 75 73 74 65 72 20 73 * etfs cluster s
f81d0 69 7a 65 20 77 72 69 74 65 73 20 61 72 65 20 61 ize writes are a
f81e0 74 6f 6d 69 63 20 2a 2f 0a 20 20 20 20 20 20 20 tomic */.
f81f0 20 28 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 53 (pFile->sectorS
f8200 69 7a 65 20 2f 20 35 31 32 20 2a 20 53 51 4c 49 ize / 512 * SQLI
f8210 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 TE_IOCAP_ATOMIC5
f8220 31 32 29 20 7c 0a 20 20 20 20 20 20 20 20 53 51 12) |. SQ
f8230 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f LITE_IOCAP_SAFE_
f8240 41 50 50 45 4e 44 20 7c 20 20 20 20 2f 2a 20 67 APPEND | /* g
f8250 72 6f 77 69 6e 67 20 74 68 65 20 66 69 6c 65 20 rowing the file
f8260 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 75 does not occur u
f8270 6e 74 69 6c 0a 20 20 20 20 20 20 20 20 20 20 20 ntil.
f8280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8290 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 ** th
f82a0 65 20 77 72 69 74 65 20 73 75 63 63 65 65 64 73 e write succeeds
f82b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c 49 */. SQLI
f82c0 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 TE_IOCAP_SEQUENT
f82d0 49 41 4c 20 7c 20 20 20 20 20 2f 2a 20 54 68 65 IAL | /* The
f82e0 20 72 61 6d 20 66 69 6c 65 73 79 73 74 65 6d 20 ram filesystem
f82f0 68 61 73 20 6e 6f 20 77 72 69 74 65 20 62 65 68 has no write beh
f8300 69 6e 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 ind.
f8310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8320 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 ** so
f8330 69 74 20 69 73 20 6f 72 64 65 72 65 64 20 2a 2f it is ordered */
f8340 0a 20 20 20 20 20 20 20 20 30 3b 0a 20 20 20 20 . 0;.
f8350 7d 65 6c 73 65 20 69 66 28 20 21 73 74 72 63 6d }else if( !strcm
f8360 70 28 66 73 49 6e 66 6f 2e 66 5f 62 61 73 65 74 p(fsInfo.f_baset
f8370 79 70 65 2c 20 22 71 6e 78 36 22 29 20 29 7b 0a ype, "qnx6") ){.
f8380 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 65 63 pFile->sec
f8390 74 6f 72 53 69 7a 65 20 3d 20 66 73 49 6e 66 6f torSize = fsInfo
f83a0 2e 66 5f 62 73 69 7a 65 3b 0a 20 20 20 20 20 20 .f_bsize;.
f83b0 70 46 69 6c 65 2d 3e 64 65 76 69 63 65 43 68 61 pFile->deviceCha
f83c0 72 61 63 74 65 72 69 73 74 69 63 73 20 3d 0a 20 racteristics =.
f83d0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f SQLITE_IO
f83e0 43 41 50 5f 41 54 4f 4d 49 43 20 7c 20 20 20 20 CAP_ATOMIC |
f83f0 20 20 20 20 20 2f 2a 20 41 6c 6c 20 66 69 6c 65 /* All file
f8400 73 79 73 74 65 6d 20 77 72 69 74 65 73 20 61 72 system writes ar
f8410 65 20 61 74 6f 6d 69 63 20 2a 2f 0a 20 20 20 20 e atomic */.
f8420 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 SQLITE_IOCAP
f8430 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 7c 20 20 _SAFE_APPEND |
f8440 20 20 2f 2a 20 67 72 6f 77 69 6e 67 20 74 68 65 /* growing the
f8450 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 6f file does not o
f8460 63 63 75 72 20 75 6e 74 69 6c 0a 20 20 20 20 20 ccur until.
f8470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8490 20 2a 2a 20 74 68 65 20 77 72 69 74 65 20 73 75 ** the write su
f84a0 63 63 65 65 64 73 20 2a 2f 0a 20 20 20 20 20 20 cceeds */.
f84b0 20 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 SQLITE_IOCAP_S
f84c0 45 51 55 45 4e 54 49 41 4c 20 7c 20 20 20 20 20 EQUENTIAL |
f84d0 2f 2a 20 54 68 65 20 72 61 6d 20 66 69 6c 65 73 /* The ram files
f84e0 79 73 74 65 6d 20 68 61 73 20 6e 6f 20 77 72 69 ystem has no wri
f84f0 74 65 20 62 65 68 69 6e 64 0a 20 20 20 20 20 20 te behind.
f8500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8520 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 72 64 65 ** so it is orde
f8530 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 30 red */. 0
f8540 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
f8550 21 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 !strcmp(fsInfo.f
f8560 5f 62 61 73 65 74 79 70 65 2c 20 22 71 6e 78 34 _basetype, "qnx4
f8570 22 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c ") ){. pFil
f8580 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 e->sectorSize =
f8590 66 73 49 6e 66 6f 2e 66 5f 62 73 69 7a 65 3b 0a fsInfo.f_bsize;.
f85a0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 64 65 76 pFile->dev
f85b0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
f85c0 63 73 20 3d 0a 20 20 20 20 20 20 20 20 2f 2a 20 cs =. /*
f85d0 66 75 6c 6c 20 62 69 74 73 65 74 20 6f 66 20 61 full bitset of a
f85e0 74 6f 6d 69 63 73 20 66 72 6f 6d 20 6d 61 78 20 tomics from max
f85f0 73 65 63 74 6f 72 20 73 69 7a 65 20 61 6e 64 20 sector size and
f8600 73 6d 61 6c 6c 65 72 20 2a 2f 0a 20 20 20 20 20 smaller */.
f8610 20 20 20 28 28 70 46 69 6c 65 2d 3e 73 65 63 74 ((pFile->sect
f8620 6f 72 53 69 7a 65 20 2f 20 35 31 32 20 2a 20 53 orSize / 512 * S
f8630 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
f8640 49 43 35 31 32 29 20 3c 3c 20 31 29 20 2d 20 32 IC512) << 1) - 2
f8650 20 7c 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 |. SQLIT
f8660 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 E_IOCAP_SEQUENTI
f8670 41 4c 20 7c 20 20 20 20 20 2f 2a 20 54 68 65 20 AL | /* The
f8680 72 61 6d 20 66 69 6c 65 73 79 73 74 65 6d 20 68 ram filesystem h
f8690 61 73 20 6e 6f 20 77 72 69 74 65 20 62 65 68 69 as no write behi
f86a0 6e 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 nd.
f86b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f86c0 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 69 ** so i
f86d0 74 20 69 73 20 6f 72 64 65 72 65 64 20 2a 2f 0a t is ordered */.
f86e0 20 20 20 20 20 20 20 20 30 3b 0a 20 20 20 20 7d 0;. }
f86f0 65 6c 73 65 20 69 66 28 20 73 74 72 73 74 72 28 else if( strstr(
f8700 66 73 49 6e 66 6f 2e 66 5f 62 61 73 65 74 79 70 fsInfo.f_basetyp
f8710 65 2c 20 22 64 6f 73 22 29 20 29 7b 0a 20 20 20 e, "dos") ){.
f8720 20 20 20 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 pFile->sector
f8730 53 69 7a 65 20 3d 20 66 73 49 6e 66 6f 2e 66 5f Size = fsInfo.f_
f8740 62 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 46 69 bsize;. pFi
f8750 6c 65 2d 3e 64 65 76 69 63 65 43 68 61 72 61 63 le->deviceCharac
f8760 74 65 72 69 73 74 69 63 73 20 3d 0a 20 20 20 20 teristics =.
f8770 20 20 20 20 2f 2a 20 66 75 6c 6c 20 62 69 74 73 /* full bits
f8780 65 74 20 6f 66 20 61 74 6f 6d 69 63 73 20 66 72 et of atomics fr
f8790 6f 6d 20 6d 61 78 20 73 65 63 74 6f 72 20 73 69 om max sector si
f87a0 7a 65 20 61 6e 64 20 73 6d 61 6c 6c 65 72 20 2a ze and smaller *
f87b0 2f 0a 20 20 20 20 20 20 20 20 28 28 70 46 69 6c /. ((pFil
f87c0 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 2f 20 e->sectorSize /
f87d0 35 31 32 20 2a 20 53 51 4c 49 54 45 5f 49 4f 43 512 * SQLITE_IOC
f87e0 41 50 5f 41 54 4f 4d 49 43 35 31 32 29 20 3c 3c AP_ATOMIC512) <<
f87f0 20 31 29 20 2d 20 32 20 7c 0a 20 20 20 20 20 20 1) - 2 |.
f8800 20 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 SQLITE_IOCAP_S
f8810 45 51 55 45 4e 54 49 41 4c 20 7c 20 20 20 20 20 EQUENTIAL |
f8820 2f 2a 20 54 68 65 20 72 61 6d 20 66 69 6c 65 73 /* The ram files
f8830 79 73 74 65 6d 20 68 61 73 20 6e 6f 20 77 72 69 ystem has no wri
f8840 74 65 20 62 65 68 69 6e 64 0a 20 20 20 20 20 20 te behind.
f8850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8870 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 72 64 65 ** so it is orde
f8880 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 30 red */. 0
f8890 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
f88a0 20 20 20 70 46 69 6c 65 2d 3e 64 65 76 69 63 65 pFile->device
f88b0 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 Characteristics
f88c0 3d 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 =. SQLITE
f88d0 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 _IOCAP_ATOMIC512
f88e0 20 7c 20 20 20 20 20 20 2f 2a 20 62 6c 6f 63 6b | /* block
f88f0 73 20 61 72 65 20 61 74 6f 6d 69 63 20 2a 2f 0a s are atomic */.
f8900 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 SQLITE_I
f8910 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 OCAP_SAFE_APPEND
f8920 20 7c 20 20 20 20 2f 2a 20 67 72 6f 77 69 6e 67 | /* growing
f8930 20 74 68 65 20 66 69 6c 65 20 64 6f 65 73 20 6e the file does n
f8940 6f 74 20 6f 63 63 75 72 20 75 6e 74 69 6c 0a 20 ot occur until.
f8950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8970 20 20 20 20 20 2a 2a 20 74 68 65 20 77 72 69 74 ** the writ
f8980 65 20 73 75 63 63 65 65 64 73 20 2a 2f 0a 20 20 e succeeds */.
f8990 20 20 20 20 20 20 30 3b 0a 20 20 20 20 7d 0a 20 0;. }.
f89a0 20 7d 0a 20 20 2f 2a 20 4c 61 73 74 20 63 68 61 }. /* Last cha
f89b0 6e 63 65 20 76 65 72 69 66 69 63 61 74 69 6f 6e nce verification
f89c0 2e 20 20 49 66 20 74 68 65 20 73 65 63 74 6f 72 . If the sector
f89d0 20 73 69 7a 65 20 69 73 6e 27 74 20 61 20 6d 75 size isn't a mu
f89e0 6c 74 69 70 6c 65 20 6f 66 20 35 31 32 0a 20 20 ltiple of 512.
f89f0 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 6e 27 74 ** then it isn't
f8a00 20 76 61 6c 69 64 2e 2a 2f 0a 20 20 69 66 28 20 valid.*/. if(
f8a10 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a pFile->sectorSiz
f8a20 65 20 25 20 35 31 32 20 21 3d 20 30 20 29 7b 0a e % 512 != 0 ){.
f8a30 20 20 20 20 70 46 69 6c 65 2d 3e 64 65 76 69 63 pFile->devic
f8a40 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
f8a50 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d = 0;. pFile-
f8a60 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 53 51 >sectorSize = SQ
f8a70 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 LITE_DEFAULT_SEC
f8a80 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a 20 20 TOR_SIZE;. }.
f8a90 72 65 74 75 72 6e 20 70 46 69 6c 65 2d 3e 73 65 return pFile->se
f8aa0 63 74 6f 72 53 69 7a 65 3b 0a 7d 0a 23 65 6e 64 ctorSize;.}.#end
f8ab0 69 66 20 2f 2a 20 5f 5f 51 4e 58 4e 54 4f 5f 5f if /* __QNXNTO__
f8ac0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 */../*.** Retur
f8ad0 6e 20 74 68 65 20 64 65 76 69 63 65 20 63 68 61 n the device cha
f8ae0 72 61 63 74 65 72 69 73 74 69 63 73 20 66 6f 72 racteristics for
f8af0 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a the file..**.**
f8b00 20 54 68 69 73 20 56 46 53 20 69 73 20 73 65 74 This VFS is set
f8b10 20 75 70 20 74 6f 20 72 65 74 75 72 6e 20 53 51 up to return SQ
f8b20 4c 49 54 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52 LITE_IOCAP_POWER
f8b30 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 20 62 SAFE_OVERWRITE b
f8b40 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 20 48 6f y default..** Ho
f8b50 77 65 76 65 72 2c 20 74 68 61 74 20 63 68 6f 69 wever, that choi
f8b60 63 65 20 69 73 20 63 6f 6e 74 72 61 76 65 72 73 ce is contravers
f8b70 69 61 6c 20 73 69 6e 63 65 20 74 65 63 68 6e 69 ial since techni
f8b80 63 61 6c 6c 79 20 74 68 65 20 75 6e 64 65 72 6c cally the underl
f8b90 79 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 73 79 73 ying.** file sys
f8ba0 74 65 6d 20 64 6f 65 73 20 6e 6f 74 20 61 6c 77 tem does not alw
f8bb0 61 79 73 20 70 72 6f 76 69 64 65 20 70 6f 77 65 ays provide powe
f8bc0 72 73 61 66 65 20 6f 76 65 72 77 72 69 74 65 73 rsafe overwrites
f8bd0 2e 20 20 28 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 . (In other.**
f8be0 77 6f 72 64 73 2c 20 61 66 74 65 72 20 61 20 70 words, after a p
f8bf0 6f 77 65 72 2d 6c 6f 73 73 20 65 76 65 6e 74 2c ower-loss event,
f8c00 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 66 69 parts of the fi
f8c10 6c 65 20 74 68 61 74 20 77 65 72 65 20 6e 65 76 le that were nev
f8c20 65 72 0a 2a 2a 20 77 72 69 74 74 65 6e 20 6d 69 er.** written mi
f8c30 67 68 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67 ght end up being
f8c40 20 61 6c 74 65 72 65 64 2e 29 20 20 48 6f 77 65 altered.) Howe
f8c50 76 65 72 2c 20 6e 6f 6e 2d 50 53 4f 57 20 62 65 ver, non-PSOW be
f8c60 68 61 76 69 6f 72 20 69 73 20 76 65 72 79 2c 0a havior is very,.
f8c70 2a 2a 20 76 65 72 79 20 72 61 72 65 2e 20 20 41 ** very rare. A
f8c80 6e 64 20 61 73 73 65 72 74 69 6e 67 20 50 53 4f nd asserting PSO
f8c90 57 20 6d 61 6b 65 73 20 61 20 6c 61 72 67 65 20 W makes a large
f8ca0 72 65 64 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 reduction in the
f8cb0 20 61 6d 6f 75 6e 74 0a 2a 2a 20 6f 66 20 72 65 amount.** of re
f8cc0 71 75 69 72 65 64 20 49 2f 4f 20 66 6f 72 20 6a quired I/O for j
f8cd0 6f 75 72 6e 61 6c 69 6e 67 2c 20 73 69 6e 63 65 ournaling, since
f8ce0 20 61 20 6c 6f 74 20 6f 66 20 70 61 64 64 69 6e a lot of paddin
f8cf0 67 20 69 73 20 65 6c 69 6d 69 6e 61 74 65 64 2e g is eliminated.
f8d00 0a 2a 2a 20 20 48 65 6e 63 65 2c 20 77 68 69 6c .** Hence, whil
f8d10 65 20 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 e POWERSAFE_OVER
f8d20 57 52 49 54 45 20 69 73 20 6f 6e 20 62 79 20 64 WRITE is on by d
f8d30 65 66 61 75 6c 74 2c 20 74 68 65 72 65 20 69 73 efault, there is
f8d40 20 61 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 0a a file-control.
f8d50 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 ** available to
f8d60 74 75 72 6e 20 69 74 20 6f 66 66 20 61 6e 64 20 turn it off and
f8d70 55 52 49 20 71 75 65 72 79 20 70 61 72 61 6d 65 URI query parame
f8d80 74 65 72 20 61 76 61 69 6c 61 62 6c 65 20 74 6f ter available to
f8d90 20 74 75 72 6e 20 69 74 20 6f 66 66 2e 0a 2a 2f turn it off..*/
f8da0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
f8db0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
f8dc0 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 stics(sqlite3_fi
f8dd0 6c 65 20 2a 69 64 29 7b 0a 20 20 75 6e 69 78 46 le *id){. unixF
f8de0 69 6c 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69 ile *p = (unixFi
f8df0 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 le*)id;. int rc
f8e00 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 5f 5f 51 = 0;.#ifdef __Q
f8e10 4e 58 4e 54 4f 5f 5f 0a 20 20 69 66 28 20 70 2d NXNTO__. if( p-
f8e20 3e 73 65 63 74 6f 72 53 69 7a 65 3d 3d 30 20 29 >sectorSize==0 )
f8e30 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 28 unixSectorSize(
f8e40 69 64 29 3b 0a 20 20 72 63 20 3d 20 70 2d 3e 64 id);. rc = p->d
f8e50 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
f8e60 74 69 63 73 3b 0a 23 65 6e 64 69 66 0a 20 20 69 tics;.#endif. i
f8e70 66 28 20 70 2d 3e 63 74 72 6c 46 6c 61 67 73 20 f( p->ctrlFlags
f8e80 26 20 55 4e 49 58 46 49 4c 45 5f 50 53 4f 57 20 & UNIXFILE_PSOW
f8e90 29 7b 0a 20 20 20 20 72 63 20 7c 3d 20 53 51 4c ){. rc |= SQL
f8ea0 49 54 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52 53 ITE_IOCAP_POWERS
f8eb0 41 46 45 5f 4f 56 45 52 57 52 49 54 45 3b 0a 20 AFE_OVERWRITE;.
f8ec0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
f8ed0 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
f8ee0 45 5f 4f 4d 49 54 5f 57 41 4c 0a 0a 0a 2f 2a 0a E_OMIT_WAL.../*.
f8ef0 2a 2a 20 4f 62 6a 65 63 74 20 75 73 65 64 20 74 ** Object used t
f8f00 6f 20 72 65 70 72 65 73 65 6e 74 20 61 6e 20 73 o represent an s
f8f10 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 62 75 66 hared memory buf
f8f20 66 65 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 68 65 fer. .**.** Whe
f8f30 6e 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61 n multiple threa
f8f40 64 73 20 61 6c 6c 20 72 65 66 65 72 65 6e 63 65 ds all reference
f8f50 20 74 68 65 20 73 61 6d 65 20 77 61 6c 2d 69 6e the same wal-in
f8f60 64 65 78 2c 20 65 61 63 68 20 74 68 72 65 61 64 dex, each thread
f8f70 0a 2a 2a 20 68 61 73 20 69 74 73 20 6f 77 6e 20 .** has its own
f8f80 75 6e 69 78 53 68 6d 20 6f 62 6a 65 63 74 2c 20 unixShm object,
f8f90 62 75 74 20 74 68 65 79 20 61 6c 6c 20 70 6f 69 but they all poi
f8fa0 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 69 nt to a single i
f8fb0 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 nstance.** of th
f8fc0 69 73 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 6f is unixShmNode o
f8fd0 62 6a 65 63 74 2e 20 20 49 6e 20 6f 74 68 65 72 bject. In other
f8fe0 20 77 6f 72 64 73 2c 20 65 61 63 68 20 77 61 6c words, each wal
f8ff0 2d 69 6e 64 65 78 20 69 73 20 6f 70 65 6e 65 64 -index is opened
f9000 0a 2a 2a 20 6f 6e 6c 79 20 6f 6e 63 65 20 70 65 .** only once pe
f9010 72 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a r process..**.**
f9020 20 45 61 63 68 20 75 6e 69 78 53 68 6d 4e 6f 64 Each unixShmNod
f9030 65 20 6f 62 6a 65 63 74 20 69 73 20 63 6f 6e 6e e object is conn
f9040 65 63 74 65 64 20 74 6f 20 61 20 73 69 6e 67 6c ected to a singl
f9050 65 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 e unixInodeInfo
f9060 6f 62 6a 65 63 74 2e 0a 2a 2a 20 57 65 20 63 6f object..** We co
f9070 75 6c 64 20 63 6f 61 6c 65 73 63 65 20 74 68 69 uld coalesce thi
f9080 73 20 6f 62 6a 65 63 74 20 69 6e 74 6f 20 75 6e s object into un
f9090 69 78 49 6e 6f 64 65 49 6e 66 6f 2c 20 62 75 74 ixInodeInfo, but
f90a0 20 74 68 61 74 20 77 6f 75 6c 64 20 6d 65 61 6e that would mean
f90b0 0a 2a 2a 20 65 76 65 72 79 20 6f 70 65 6e 20 66 .** every open f
f90c0 69 6c 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f ile that does no
f90d0 74 20 75 73 65 20 73 68 61 72 65 64 20 6d 65 6d t use shared mem
f90e0 6f 72 79 20 28 69 6e 20 6f 74 68 65 72 20 77 6f ory (in other wo
f90f0 72 64 73 2c 20 6d 6f 73 74 0a 2a 2a 20 6f 70 65 rds, most.** ope
f9100 6e 20 66 69 6c 65 73 29 20 77 6f 75 6c 64 20 68 n files) would h
f9110 61 76 65 20 74 6f 20 63 61 72 72 79 20 61 72 6f ave to carry aro
f9120 75 6e 64 20 74 68 69 73 20 65 78 74 72 61 20 69 und this extra i
f9130 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 53 6f 0a nformation. So.
f9140 2a 2a 20 74 68 65 20 75 6e 69 78 49 6e 6f 64 65 ** the unixInode
f9150 49 6e 66 6f 20 6f 62 6a 65 63 74 20 63 6f 6e 74 Info object cont
f9160 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 ains a pointer t
f9170 6f 20 74 68 69 73 20 75 6e 69 78 53 68 6d 4e 6f o this unixShmNo
f9180 64 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 de object.** and
f9190 20 74 68 65 20 75 6e 69 78 53 68 6d 4e 6f 64 65 the unixShmNode
f91a0 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74 object is creat
f91b0 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 6e 65 65 ed only when nee
f91c0 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 75 6e 69 78 4d ded..**.** unixM
f91d0 75 74 65 78 48 65 6c 64 28 29 20 6d 75 73 74 20 utexHeld() must
f91e0 62 65 20 74 72 75 65 20 77 68 65 6e 20 63 72 65 be true when cre
f91f0 61 74 69 6e 67 20 6f 72 20 64 65 73 74 72 6f 79 ating or destroy
f9200 69 6e 67 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65 ing.** this obje
f9210 63 74 20 6f 72 20 77 68 69 6c 65 20 72 65 61 64 ct or while read
f9220 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 74 ing or writing t
f9230 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 he following fie
f9240 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 lds:.**.**
f9250 6e 52 65 66 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 nRef.**.** The f
f9260 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 20 ollowing fields
f9270 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 61 66 are read-only af
f9280 74 65 72 20 74 68 65 20 6f 62 6a 65 63 74 20 69 ter the object i
f9290 73 20 63 72 65 61 74 65 64 3a 0a 2a 2a 20 0a 2a s created:.** .*
f92a0 2a 20 20 20 20 20 20 66 69 64 0a 2a 2a 20 20 20 * fid.**
f92b0 20 20 20 7a 46 69 6c 65 6e 61 6d 65 0a 2a 2a 0a zFilename.**.
f92c0 2a 2a 20 45 69 74 68 65 72 20 75 6e 69 78 53 68 ** Either unixSh
f92d0 6d 4e 6f 64 65 2e 6d 75 74 65 78 20 6d 75 73 74 mNode.mutex must
f92e0 20 62 65 20 68 65 6c 64 20 6f 72 20 75 6e 69 78 be held or unix
f92f0 53 68 6d 4e 6f 64 65 2e 6e 52 65 66 3d 3d 30 20 ShmNode.nRef==0
f9300 61 6e 64 0a 2a 2a 20 75 6e 69 78 4d 75 74 65 78 and.** unixMutex
f9310 48 65 6c 64 28 29 20 69 73 20 74 72 75 65 20 77 Held() is true w
f9320 68 65 6e 20 72 65 61 64 69 6e 67 20 6f 72 20 77 hen reading or w
f9330 72 69 74 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 riting any other
f9340 20 66 69 65 6c 64 0a 2a 2a 20 69 6e 20 74 68 69 field.** in thi
f9350 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a s structure..*/.
f9360 73 74 72 75 63 74 20 75 6e 69 78 53 68 6d 4e 6f struct unixShmNo
f9370 64 65 20 7b 0a 20 20 75 6e 69 78 49 6e 6f 64 65 de {. unixInode
f9380 49 6e 66 6f 20 2a 70 49 6e 6f 64 65 3b 20 20 20 Info *pInode;
f9390 20 20 2f 2a 20 75 6e 69 78 49 6e 6f 64 65 49 6e /* unixInodeIn
f93a0 66 6f 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69 fo that owns thi
f93b0 73 20 53 48 4d 20 6e 6f 64 65 20 2a 2f 0a 20 20 s SHM node */.
f93c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d sqlite3_mutex *m
f93d0 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 4d 75 utex; /* Mu
f93e0 74 65 78 20 74 6f 20 61 63 63 65 73 73 20 74 68 tex to access th
f93f0 69 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 is object */. c
f9400 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 20 har *zFilename;
f9410 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
f9420 65 20 6f 66 20 74 68 65 20 6d 6d 61 70 70 65 64 e of the mmapped
f9430 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 68 file */. int h
f9440 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
f9450 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69 /* Open fi
f9460 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f le descriptor */
f9470 0a 20 20 69 6e 74 20 73 7a 52 65 67 69 6f 6e 3b . int szRegion;
f9480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f9490 20 53 69 7a 65 20 6f 66 20 73 68 61 72 65 64 2d Size of shared-
f94a0 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 73 20 2a memory regions *
f94b0 2f 0a 20 20 75 31 36 20 6e 52 65 67 69 6f 6e 3b /. u16 nRegion;
f94c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f94d0 2a 20 53 69 7a 65 20 6f 66 20 61 72 72 61 79 20 * Size of array
f94e0 61 70 52 65 67 69 6f 6e 20 2a 2f 0a 20 20 75 38 apRegion */. u8
f94f0 20 69 73 52 65 61 64 6f 6e 6c 79 3b 20 20 20 20 isReadonly;
f9500 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
f9510 20 69 66 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f if read-only */
f9520 0a 20 20 63 68 61 72 20 2a 2a 61 70 52 65 67 69 . char **apRegi
f9530 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a on; /*
f9540 20 41 72 72 61 79 20 6f 66 20 6d 61 70 70 65 64 Array of mapped
f9550 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 72 shared-memory r
f9560 65 67 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 egions */. int
f9570 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
f9580 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
f9590 20 6f 66 20 75 6e 69 78 53 68 6d 20 6f 62 6a 65 of unixShm obje
f95a0 63 74 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 cts pointing to
f95b0 74 68 69 73 20 2a 2f 0a 20 20 75 6e 69 78 53 68 this */. unixSh
f95c0 6d 20 2a 70 46 69 72 73 74 3b 20 20 20 20 20 20 m *pFirst;
f95d0 20 20 20 20 20 2f 2a 20 41 6c 6c 20 75 6e 69 78 /* All unix
f95e0 53 68 6d 20 6f 62 6a 65 63 74 73 20 70 6f 69 6e Shm objects poin
f95f0 74 69 6e 67 20 74 6f 20 74 68 69 73 20 2a 2f 0a ting to this */.
f9600 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
f9610 42 55 47 0a 20 20 75 38 20 65 78 63 6c 4d 61 73 BUG. u8 exclMas
f9620 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k;
f9630 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 65 78 63 6c /* Mask of excl
f9640 75 73 69 76 65 20 6c 6f 63 6b 73 20 68 65 6c 64 usive locks held
f9650 20 2a 2f 0a 20 20 75 38 20 73 68 61 72 65 64 4d */. u8 sharedM
f9660 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 ask;
f9670 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 73 68 61 72 /* Mask of shar
f9680 65 64 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f ed locks held */
f9690 0a 20 20 75 38 20 6e 65 78 74 53 68 6d 49 64 3b . u8 nextShmId;
f96a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f96b0 20 4e 65 78 74 20 61 76 61 69 6c 61 62 6c 65 20 Next available
f96c0 75 6e 69 78 53 68 6d 2e 69 64 20 76 61 6c 75 65 unixShm.id value
f96d0 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f */.#endif.};../
f96e0 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 75 *.** Structure u
f96f0 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 sed internally b
f9700 79 20 74 68 69 73 20 56 46 53 20 74 6f 20 72 65 y this VFS to re
f9710 63 6f 72 64 20 74 68 65 20 73 74 61 74 65 20 6f cord the state o
f9720 66 20 61 6e 0a 2a 2a 20 6f 70 65 6e 20 73 68 61 f an.** open sha
f9730 72 65 64 20 6d 65 6d 6f 72 79 20 63 6f 6e 6e 65 red memory conne
f9740 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ction..**.** The
f9750 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 following field
f9760 73 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 s are initialize
f9770 64 20 77 68 65 6e 20 74 68 69 73 20 6f 62 6a 65 d when this obje
f9780 63 74 20 69 73 20 63 72 65 61 74 65 64 20 61 6e ct is created an
f9790 64 0a 2a 2a 20 61 72 65 20 72 65 61 64 2d 6f 6e d.** are read-on
f97a0 6c 79 20 74 68 65 72 65 61 66 74 65 72 3a 0a 2a ly thereafter:.*
f97b0 2a 0a 2a 2a 20 20 20 20 75 6e 69 78 53 68 6d 2e *.** unixShm.
f97c0 70 46 69 6c 65 0a 2a 2a 20 20 20 20 75 6e 69 78 pFile.** unix
f97d0 53 68 6d 2e 69 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c Shm.id.**.** All
f97e0 20 6f 74 68 65 72 20 66 69 65 6c 64 73 20 61 72 other fields ar
f97f0 65 20 72 65 61 64 2f 77 72 69 74 65 2e 20 20 54 e read/write. T
f9800 68 65 20 75 6e 69 78 53 68 6d 2e 70 46 69 6c 65 he unixShm.pFile
f9810 2d 3e 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 ->mutex must be
f9820 68 65 6c 64 0a 2a 2a 20 77 68 69 6c 65 20 61 63 held.** while ac
f9830 63 65 73 73 69 6e 67 20 61 6e 79 20 72 65 61 64 cessing any read
f9840 2f 77 72 69 74 65 20 66 69 65 6c 64 73 2e 0a 2a /write fields..*
f9850 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 53 68 6d /.struct unixShm
f9860 20 7b 0a 20 20 75 6e 69 78 53 68 6d 4e 6f 64 65 {. unixShmNode
f9870 20 2a 70 53 68 6d 4e 6f 64 65 3b 20 20 20 20 20 *pShmNode;
f9880 2f 2a 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e /* The underlyin
f9890 67 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 6f 62 g unixShmNode ob
f98a0 6a 65 63 74 20 2a 2f 0a 20 20 75 6e 69 78 53 68 ject */. unixSh
f98b0 6d 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 m *pNext;
f98c0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 75 6e 69 /* Next uni
f98d0 78 53 68 6d 20 77 69 74 68 20 74 68 65 20 73 61 xShm with the sa
f98e0 6d 65 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 2a me unixShmNode *
f98f0 2f 0a 20 20 75 38 20 68 61 73 4d 75 74 65 78 3b /. u8 hasMutex;
f9900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f9910 2a 20 54 72 75 65 20 69 66 20 68 6f 6c 64 69 6e * True if holdin
f9920 67 20 74 68 65 20 75 6e 69 78 53 68 6d 4e 6f 64 g the unixShmNod
f9930 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 75 38 20 e mutex */. u8
f9940 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 id;
f9950 20 20 20 20 20 20 20 20 2f 2a 20 49 64 20 6f 66 /* Id of
f9960 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e this connection
f9970 20 77 69 74 68 69 6e 20 69 74 73 20 75 6e 69 78 within its unix
f9980 53 68 6d 4e 6f 64 65 20 2a 2f 0a 20 20 75 31 36 ShmNode */. u16
f9990 20 73 68 61 72 65 64 4d 61 73 6b 3b 20 20 20 20 sharedMask;
f99a0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 /* Mask
f99b0 6f 66 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 of shared locks
f99c0 68 65 6c 64 20 2a 2f 0a 20 20 75 31 36 20 65 78 held */. u16 ex
f99d0 63 6c 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 clMask;
f99e0 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 /* Mask of
f99f0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 73 20 exclusive locks
f9a00 68 65 6c 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a held */.};../*.*
f9a10 2a 20 43 6f 6e 73 74 61 6e 74 73 20 75 73 65 64 * Constants used
f9a20 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 0a 2a 2f 0a for locking.*/.
f9a30 23 64 65 66 69 6e 65 20 55 4e 49 58 5f 53 48 4d #define UNIX_SHM
f9a40 5f 42 41 53 45 20 20 20 28 28 32 32 2b 53 51 4c _BASE ((22+SQL
f9a50 49 54 45 5f 53 48 4d 5f 4e 4c 4f 43 4b 29 2a 34 ITE_SHM_NLOCK)*4
f9a60 29 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 72 ) /* fir
f9a70 73 74 20 6c 6f 63 6b 20 62 79 74 65 20 2a 2f 0a st lock byte */.
f9a80 23 64 65 66 69 6e 65 20 55 4e 49 58 5f 53 48 4d #define UNIX_SHM
f9a90 5f 44 4d 53 20 20 20 20 28 55 4e 49 58 5f 53 48 _DMS (UNIX_SH
f9aa0 4d 5f 42 41 53 45 2b 53 51 4c 49 54 45 5f 53 48 M_BASE+SQLITE_SH
f9ab0 4d 5f 4e 4c 4f 43 4b 29 20 20 2f 2a 20 64 65 61 M_NLOCK) /* dea
f9ac0 64 6d 61 6e 20 73 77 69 74 63 68 20 2a 2f 0a 0a dman switch */..
f9ad0 2f 2a 0a 2a 2a 20 41 70 70 6c 79 20 70 6f 73 69 /*.** Apply posi
f9ae0 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 x advisory locks
f9af0 20 66 6f 72 20 61 6c 6c 20 62 79 74 65 73 20 66 for all bytes f
f9b00 72 6f 6d 20 6f 66 73 74 20 74 68 72 6f 75 67 68 rom ofst through
f9b10 20 6f 66 73 74 2b 6e 2d 31 2e 0a 2a 2a 0a 2a 2a ofst+n-1..**.**
f9b20 20 4c 6f 63 6b 73 20 62 6c 6f 63 6b 20 69 66 20 Locks block if
f9b30 74 68 65 20 6d 61 73 6b 20 69 73 20 65 78 61 63 the mask is exac
f9b40 74 6c 79 20 55 4e 49 58 5f 53 48 4d 5f 43 20 61 tly UNIX_SHM_C a
f9b50 6e 64 20 61 72 65 20 6e 6f 6e 2d 62 6c 6f 63 6b nd are non-block
f9b60 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 ing.** otherwise
f9b70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
f9b80 75 6e 69 78 53 68 6d 53 79 73 74 65 6d 4c 6f 63 unixShmSystemLoc
f9b90 6b 28 0a 20 20 75 6e 69 78 53 68 6d 4e 6f 64 65 k(. unixShmNode
f9ba0 20 2a 70 53 68 6d 4e 6f 64 65 2c 20 2f 2a 20 41 *pShmNode, /* A
f9bb0 70 70 6c 79 20 6c 6f 63 6b 73 20 74 6f 20 74 68 pply locks to th
f9bc0 69 73 20 6f 70 65 6e 20 73 68 61 72 65 64 2d 6d is open shared-m
f9bd0 65 6d 6f 72 79 20 73 65 67 6d 65 6e 74 20 2a 2f emory segment */
f9be0 0a 20 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 2c . int lockType,
f9bf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 5f 55 /* F_U
f9c00 4e 4c 43 4b 2c 20 46 5f 52 44 4c 43 4b 2c 20 6f NLCK, F_RDLCK, o
f9c10 72 20 46 5f 57 52 4c 43 4b 20 2a 2f 0a 20 20 69 r F_WRLCK */. i
f9c20 6e 74 20 6f 66 73 74 2c 20 20 20 20 20 20 20 20 nt ofst,
f9c30 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 /* First b
f9c40 79 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69 yte of the locki
f9c50 6e 67 20 72 61 6e 67 65 20 2a 2f 0a 20 20 69 6e ng range */. in
f9c60 74 20 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 t n
f9c70 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
f9c80 66 20 62 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 f bytes to lock
f9c90 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 66 */.){. struct f
f9ca0 6c 6f 63 6b 20 66 3b 20 20 20 20 20 20 20 2f 2a lock f; /*
f9cb0 20 54 68 65 20 70 6f 73 69 78 20 61 64 76 69 73 The posix advis
f9cc0 6f 72 79 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 ory locking stru
f9cd0 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 cture */. int r
f9ce0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
f9cf0 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 /* Result code
f9d00 66 6f 72 6d 20 66 63 6e 74 6c 28 29 20 2a 2f 0a form fcntl() */.
f9d10 0a 20 20 2f 2a 20 41 63 63 65 73 73 20 74 6f 20 . /* Access to
f9d20 74 68 65 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 the unixShmNode
f9d30 6f 62 6a 65 63 74 20 69 73 20 73 65 72 69 61 6c object is serial
f9d40 69 7a 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c ized by the call
f9d50 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 er */. assert(
f9d60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
f9d70 6c 64 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 ld(pShmNode->mut
f9d80 65 78 29 20 7c 7c 20 70 53 68 6d 4e 6f 64 65 2d ex) || pShmNode-
f9d90 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 0a 20 20 2f >nRef==0 );.. /
f9da0 2a 20 53 68 61 72 65 64 20 6c 6f 63 6b 73 20 6e * Shared locks n
f9db0 65 76 65 72 20 73 70 61 6e 20 6d 6f 72 65 20 74 ever span more t
f9dc0 68 61 6e 20 6f 6e 65 20 62 79 74 65 20 2a 2f 0a han one byte */.
f9dd0 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 31 20 7c assert( n==1 |
f9de0 7c 20 6c 6f 63 6b 54 79 70 65 21 3d 46 5f 52 44 | lockType!=F_RD
f9df0 4c 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 LCK );.. /* Loc
f9e00 6b 73 20 61 72 65 20 77 69 74 68 69 6e 20 72 61 ks are within ra
f9e10 6e 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 nge */. assert(
f9e20 20 6e 3e 3d 31 20 26 26 20 6e 3c 53 51 4c 49 54 n>=1 && n<SQLIT
f9e30 45 5f 53 48 4d 5f 4e 4c 4f 43 4b 20 29 3b 0a 0a E_SHM_NLOCK );..
f9e40 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e if( pShmNode->
f9e50 68 3e 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 h>=0 ){. /* I
f9e60 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6c 6f nitialize the lo
f9e70 63 6b 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 cking parameters
f9e80 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 */. memset(&
f9e90 66 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 29 29 f, 0, sizeof(f))
f9ea0 3b 0a 20 20 20 20 66 2e 6c 5f 74 79 70 65 20 3d ;. f.l_type =
f9eb0 20 6c 6f 63 6b 54 79 70 65 3b 0a 20 20 20 20 66 lockType;. f
f9ec0 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b .l_whence = SEEK
f9ed0 5f 53 45 54 3b 0a 20 20 20 20 66 2e 6c 5f 73 74 _SET;. f.l_st
f9ee0 61 72 74 20 3d 20 6f 66 73 74 3b 0a 20 20 20 20 art = ofst;.
f9ef0 66 2e 6c 5f 6c 65 6e 20 3d 20 6e 3b 0a 0a 20 20 f.l_len = n;..
f9f00 20 20 72 63 20 3d 20 6f 73 46 63 6e 74 6c 28 70 rc = osFcntl(p
f9f10 53 68 6d 4e 6f 64 65 2d 3e 68 2c 20 46 5f 53 45 ShmNode->h, F_SE
f9f20 54 4c 4b 2c 20 26 66 29 3b 0a 20 20 20 20 72 63 TLK, &f);. rc
f9f30 20 3d 20 28 72 63 21 3d 28 2d 31 29 29 20 3f 20 = (rc!=(-1)) ?
f9f40 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 SQLITE_OK : SQLI
f9f50 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 20 TE_BUSY;. }..
f9f60 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 67 6c /* Update the gl
f9f70 6f 62 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 20 obal lock state
f9f80 61 6e 64 20 64 6f 20 64 65 62 75 67 20 74 72 61 and do debug tra
f9f90 63 69 6e 67 20 2a 2f 0a 23 69 66 64 65 66 20 53 cing */.#ifdef S
f9fa0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 7b 20 QLITE_DEBUG. {
f9fb0 75 31 36 20 6d 61 73 6b 3b 0a 20 20 4f 53 54 52 u16 mask;. OSTR
f9fc0 41 43 45 28 28 22 53 48 4d 2d 4c 4f 43 4b 20 22 ACE(("SHM-LOCK "
f9fd0 29 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 28 31 3c ));. mask = (1<
f9fe0 3c 28 6f 66 73 74 2b 6e 29 29 20 2d 20 28 31 3c <(ofst+n)) - (1<
f9ff0 3c 6f 66 73 74 29 3b 0a 20 20 69 66 28 20 72 63 <ofst);. if( rc
fa000 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
fa010 20 20 20 69 66 28 20 6c 6f 63 6b 54 79 70 65 3d if( lockType=
fa020 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 =F_UNLCK ){.
fa030 20 20 4f 53 54 52 41 43 45 28 28 22 75 6e 6c 6f OSTRACE(("unlo
fa040 63 6b 20 25 64 20 6f 6b 22 2c 20 6f 66 73 74 29 ck %d ok", ofst)
fa050 29 3b 0a 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 );. pShmNod
fa060 65 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 3d 20 7e e->exclMask &= ~
fa070 6d 61 73 6b 3b 0a 20 20 20 20 20 20 70 53 68 6d mask;. pShm
fa080 4e 6f 64 65 2d 3e 73 68 61 72 65 64 4d 61 73 6b Node->sharedMask
fa090 20 26 3d 20 7e 6d 61 73 6b 3b 0a 20 20 20 20 7d &= ~mask;. }
fa0a0 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 54 79 70 else if( lockTyp
fa0b0 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 e==F_RDLCK ){.
fa0c0 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 72 65 OSTRACE(("re
fa0d0 61 64 2d 6c 6f 63 6b 20 25 64 20 6f 6b 22 2c 20 ad-lock %d ok",
fa0e0 6f 66 73 74 29 29 3b 0a 20 20 20 20 20 20 70 53 ofst));. pS
fa0f0 68 6d 4e 6f 64 65 2d 3e 65 78 63 6c 4d 61 73 6b hmNode->exclMask
fa100 20 26 3d 20 7e 6d 61 73 6b 3b 0a 20 20 20 20 20 &= ~mask;.
fa110 20 70 53 68 6d 4e 6f 64 65 2d 3e 73 68 61 72 65 pShmNode->share
fa120 64 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 dMask |= mask;.
fa130 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
fa140 61 73 73 65 72 74 28 20 6c 6f 63 6b 54 79 70 65 assert( lockType
fa150 3d 3d 46 5f 57 52 4c 43 4b 20 29 3b 0a 20 20 20 ==F_WRLCK );.
fa160 20 20 20 4f 53 54 52 41 43 45 28 28 22 77 72 69 OSTRACE(("wri
fa170 74 65 2d 6c 6f 63 6b 20 25 64 20 6f 6b 22 2c 20 te-lock %d ok",
fa180 6f 66 73 74 29 29 3b 0a 20 20 20 20 20 20 70 53 ofst));. pS
fa190 68 6d 4e 6f 64 65 2d 3e 65 78 63 6c 4d 61 73 6b hmNode->exclMask
fa1a0 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 |= mask;.
fa1b0 70 53 68 6d 4e 6f 64 65 2d 3e 73 68 61 72 65 64 pShmNode->shared
fa1c0 4d 61 73 6b 20 26 3d 20 7e 6d 61 73 6b 3b 0a 20 Mask &= ~mask;.
fa1d0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
fa1e0 20 20 69 66 28 20 6c 6f 63 6b 54 79 70 65 3d 3d if( lockType==
fa1f0 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 F_UNLCK ){.
fa200 20 4f 53 54 52 41 43 45 28 28 22 75 6e 6c 6f 63 OSTRACE(("unloc
fa210 6b 20 25 64 20 66 61 69 6c 65 64 22 2c 20 6f 66 k %d failed", of
fa220 73 74 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 st));. }else
fa230 69 66 28 20 6c 6f 63 6b 54 79 70 65 3d 3d 46 5f if( lockType==F_
fa240 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 4f RDLCK ){. O
fa250 53 54 52 41 43 45 28 28 22 72 65 61 64 2d 6c 6f STRACE(("read-lo
fa260 63 6b 20 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 ck failed"));.
fa270 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
fa280 73 73 65 72 74 28 20 6c 6f 63 6b 54 79 70 65 3d ssert( lockType=
fa290 3d 46 5f 57 52 4c 43 4b 20 29 3b 0a 20 20 20 20 =F_WRLCK );.
fa2a0 20 20 4f 53 54 52 41 43 45 28 28 22 77 72 69 74 OSTRACE(("writ
fa2b0 65 2d 6c 6f 63 6b 20 25 64 20 66 61 69 6c 65 64 e-lock %d failed
fa2c0 22 2c 20 6f 66 73 74 29 29 3b 0a 20 20 20 20 7d ", ofst));. }
fa2d0 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 28 28 . }. OSTRACE((
fa2e0 22 20 2d 20 61 66 74 65 72 77 61 72 64 73 20 25 " - afterwards %
fa2f0 30 33 78 2c 25 30 33 78 5c 6e 22 2c 0a 20 20 20 03x,%03x\n",.
fa300 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65 pShmNode
fa310 2d 3e 73 68 61 72 65 64 4d 61 73 6b 2c 20 70 53 ->sharedMask, pS
fa320 68 6d 4e 6f 64 65 2d 3e 65 78 63 6c 4d 61 73 6b hmNode->exclMask
fa330 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a ));. }.#endif..
fa340 20 20 72 65 74 75 72 6e 20 72 63 3b 20 20 20 20 return rc;
fa350 20 20 20 20 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 50 .}.../*.** P
fa360 75 72 67 65 20 74 68 65 20 75 6e 69 78 53 68 6d urge the unixShm
fa370 4e 6f 64 65 4c 69 73 74 20 6c 69 73 74 20 6f 66 NodeList list of
fa380 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 77 69 74 all entries wit
fa390 68 20 75 6e 69 78 53 68 6d 4e 6f 64 65 2e 6e 52 h unixShmNode.nR
fa3a0 65 66 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ef==0..**.** Thi
fa3b0 73 20 69 73 20 6e 6f 74 20 61 20 56 46 53 20 73 s is not a VFS s
fa3c0 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 6d 65 74 hared-memory met
fa3d0 68 6f 64 3b 20 69 74 20 69 73 20 61 20 75 74 69 hod; it is a uti
fa3e0 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 20 63 61 lity function ca
fa3f0 6c 6c 65 64 0a 2a 2a 20 62 79 20 56 46 53 20 73 lled.** by VFS s
fa400 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 6d 65 74 hared-memory met
fa410 68 6f 64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 hods..*/.static
fa420 76 6f 69 64 20 75 6e 69 78 53 68 6d 50 75 72 67 void unixShmPurg
fa430 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 64 29 e(unixFile *pFd)
fa440 7b 0a 20 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 {. unixShmNode
fa450 2a 70 20 3d 20 70 46 64 2d 3e 70 49 6e 6f 64 65 *p = pFd->pInode
fa460 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0a 20 20 61 73 ->pShmNode;. as
fa470 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 sert( unixMutexH
fa480 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 28 20 70 eld() );. if( p
fa490 20 26 26 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 && p->nRef==0 )
fa4a0 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 {. int i;.
fa4b0 20 61 73 73 65 72 74 28 20 70 2d 3e 70 49 6e 6f assert( p->pIno
fa4c0 64 65 3d 3d 70 46 64 2d 3e 70 49 6e 6f 64 65 20 de==pFd->pInode
fa4d0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
fa4e0 75 74 65 78 5f 66 72 65 65 28 70 2d 3e 6d 75 74 utex_free(p->mut
fa4f0 65 78 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 ex);. for(i=0
fa500 3b 20 69 3c 70 2d 3e 6e 52 65 67 69 6f 6e 3b 20 ; i<p->nRegion;
fa510 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 i++){. if(
fa520 70 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20 p->h>=0 ){.
fa530 20 20 20 6d 75 6e 6d 61 70 28 70 2d 3e 61 70 52 munmap(p->apR
fa540 65 67 69 6f 6e 5b 69 5d 2c 20 70 2d 3e 73 7a 52 egion[i], p->szR
fa550 65 67 69 6f 6e 29 3b 0a 20 20 20 20 20 20 7d 65 egion);. }e
fa560 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c lse{. sql
fa570 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 70 52 ite3_free(p->apR
fa580 65 67 69 6f 6e 5b 69 5d 29 3b 0a 20 20 20 20 20 egion[i]);.
fa590 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c }. }. sql
fa5a0 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 70 52 ite3_free(p->apR
fa5b0 65 67 69 6f 6e 29 3b 0a 20 20 20 20 69 66 28 20 egion);. if(
fa5c0 70 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20 p->h>=0 ){.
fa5d0 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 46 robust_close(pF
fa5e0 64 2c 20 70 2d 3e 68 2c 20 5f 5f 4c 49 4e 45 5f d, p->h, __LINE_
fa5f0 5f 29 3b 0a 20 20 20 20 20 20 70 2d 3e 68 20 3d _);. p->h =
fa600 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 -1;. }. p
fa610 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 68 6d 4e 6f ->pInode->pShmNo
fa620 64 65 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 de = 0;. sqli
fa630 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d te3_free(p);. }
fa640 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 .}../*.** Open a
fa650 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 61 shared-memory a
fa660 72 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 rea associated w
fa670 69 74 68 20 6f 70 65 6e 20 64 61 74 61 62 61 73 ith open databas
fa680 65 20 66 69 6c 65 20 70 44 62 46 64 2e 20 20 0a e file pDbFd. .
fa690 2a 2a 20 54 68 69 73 20 70 61 72 74 69 63 75 6c ** This particul
fa6a0 61 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ar implementatio
fa6b0 6e 20 75 73 65 73 20 6d 6d 61 70 70 65 64 20 66 n uses mmapped f
fa6c0 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 iles..**.** The
fa6d0 66 69 6c 65 20 75 73 65 64 20 74 6f 20 69 6d 70 file used to imp
fa6e0 6c 65 6d 65 6e 74 20 73 68 61 72 65 64 2d 6d 65 lement shared-me
fa6f0 6d 6f 72 79 20 69 73 20 69 6e 20 74 68 65 20 73 mory is in the s
fa700 61 6d 65 20 64 69 72 65 63 74 6f 72 79 0a 2a 2a ame directory.**
fa710 20 61 73 20 74 68 65 20 6f 70 65 6e 20 64 61 74 as the open dat
fa720 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 68 abase file and h
fa730 61 73 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 as the same name
fa740 20 61 73 20 74 68 65 20 6f 70 65 6e 20 64 61 74 as the open dat
fa750 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 77 69 abase.** file wi
fa760 74 68 20 74 68 65 20 22 2d 73 68 6d 22 20 73 75 th the "-shm" su
fa770 66 66 69 78 20 61 64 64 65 64 2e 20 20 46 6f 72 ffix added. For
fa780 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 example, if the
fa790 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a database file.*
fa7a0 2a 20 69 73 20 22 2f 68 6f 6d 65 2f 75 73 65 72 * is "/home/user
fa7b0 31 2f 63 6f 6e 66 69 67 2e 64 62 22 20 74 68 65 1/config.db" the
fa7c0 6e 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 n the file that
fa7d0 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 20 6d is created and m
fa7e0 6d 61 70 70 65 64 0a 2a 2a 20 66 6f 72 20 73 68 mapped.** for sh
fa7f0 61 72 65 64 20 6d 65 6d 6f 72 79 20 77 69 6c 6c ared memory will
fa800 20 62 65 20 63 61 6c 6c 65 64 20 22 2f 68 6f 6d be called "/hom
fa810 65 2f 75 73 65 72 31 2f 63 6f 6e 66 69 67 2e 64 e/user1/config.d
fa820 62 2d 73 68 6d 22 2e 20 20 0a 2a 2a 0a 2a 2a 20 b-shm". .**.**
fa830 41 6e 6f 74 68 65 72 20 61 70 70 72 6f 61 63 68 Another approach
fa840 20 74 6f 20 69 73 20 74 6f 20 75 73 65 20 66 69 to is to use fi
fa850 6c 65 73 20 69 6e 20 2f 64 65 76 2f 73 68 6d 20 les in /dev/shm
fa860 6f 72 20 2f 64 65 76 2f 74 6d 70 20 6f 72 20 61 or /dev/tmp or a
fa870 6e 0a 2a 2a 20 73 6f 6d 65 20 6f 74 68 65 72 20 n.** some other
fa880 74 6d 70 66 73 20 6d 6f 75 6e 74 2e 20 42 75 74 tmpfs mount. But
fa890 20 69 66 20 61 20 66 69 6c 65 20 69 6e 20 61 20 if a file in a
fa8a0 64 69 66 66 65 72 65 6e 74 20 64 69 72 65 63 74 different direct
fa8b0 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 ory.** from the
fa8c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
fa8d0 20 75 73 65 64 2c 20 74 68 65 6e 20 64 69 66 66 used, then diff
fa8e0 65 72 69 6e 67 20 61 63 63 65 73 73 20 70 65 72 ering access per
fa8f0 6d 69 73 73 69 6f 6e 73 0a 2a 2a 20 6f 72 20 61 missions.** or a
fa900 20 63 68 72 6f 6f 74 28 29 20 6d 69 67 68 74 20 chroot() might
fa910 63 61 75 73 65 20 74 77 6f 20 64 69 66 66 65 72 cause two differ
fa920 65 6e 74 20 70 72 6f 63 65 73 73 65 73 20 6f 6e ent processes on
fa930 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 64 61 74 the same.** dat
fa940 61 62 61 73 65 20 74 6f 20 65 6e 64 20 75 70 20 abase to end up
fa950 75 73 69 6e 67 20 64 69 66 66 65 72 65 6e 74 20 using different
fa960 66 69 6c 65 73 20 66 6f 72 20 73 68 61 72 65 64 files for shared
fa970 20 6d 65 6d 6f 72 79 20 2d 20 0a 2a 2a 20 6d 65 memory - .** me
fa980 61 6e 69 6e 67 20 74 68 61 74 20 74 68 65 69 72 aning that their
fa990 20 6d 65 6d 6f 72 79 20 77 6f 75 6c 64 20 6e 6f memory would no
fa9a0 74 20 72 65 61 6c 6c 79 20 62 65 20 73 68 61 72 t really be shar
fa9b0 65 64 20 2d 20 72 65 73 75 6c 74 69 6e 67 0a 2a ed - resulting.*
fa9c0 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f * in database co
fa9d0 72 72 75 70 74 69 6f 6e 2e 20 20 4e 65 76 65 72 rruption. Never
fa9e0 74 68 65 6c 65 73 73 2c 20 74 68 69 73 20 74 6d theless, this tm
fa9f0 70 66 73 20 66 69 6c 65 20 75 73 61 67 65 0a 2a pfs file usage.*
faa00 2a 20 63 61 6e 20 62 65 20 65 6e 61 62 6c 65 64 * can be enabled
faa10 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 at compile-time
faa20 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f using -DSQLITE_
faa30 53 48 4d 5f 44 49 52 45 43 54 4f 52 59 3d 22 2f SHM_DIRECTORY="/
faa40 64 65 76 2f 73 68 6d 22 0a 2a 2a 20 6f 72 20 74 dev/shm".** or t
faa50 68 65 20 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 he equivalent.
faa60 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 20 53 The use of the S
faa70 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52 45 43 54 QLITE_SHM_DIRECT
faa80 4f 52 59 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 ORY compile-time
faa90 0a 2a 2a 20 6f 70 74 69 6f 6e 20 72 65 73 75 6c .** option resul
faaa0 74 73 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 ts in an incompa
faab0 74 69 62 6c 65 20 62 75 69 6c 64 20 6f 66 20 53 tible build of S
faac0 51 4c 69 74 65 3b 20 20 62 75 69 6c 64 73 20 6f QLite; builds o
faad0 66 20 53 51 4c 69 74 65 0a 2a 2a 20 74 68 61 74 f SQLite.** that
faae0 20 77 69 74 68 20 64 69 66 66 65 72 69 6e 67 20 with differing
faaf0 53 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52 45 43 SQLITE_SHM_DIREC
fab00 54 4f 52 59 20 73 65 74 74 69 6e 67 73 20 61 74 TORY settings at
fab10 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 tempt to use the
fab20 0a 2a 2a 20 73 61 6d 65 20 64 61 74 61 62 61 73 .** same databas
fab30 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 73 61 e file at the sa
fab40 6d 65 20 74 69 6d 65 2c 20 64 61 74 61 62 61 73 me time, databas
fab50 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 69 6c e corruption wil
fab60 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 l likely.** resu
fab70 6c 74 2e 20 54 68 65 20 53 51 4c 49 54 45 5f 53 lt. The SQLITE_S
fab80 48 4d 5f 44 49 52 45 43 54 4f 52 59 20 63 6f 6d HM_DIRECTORY com
fab90 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e pile-time option
faba0 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a is considered.*
fabb0 2a 20 22 75 6e 73 75 70 70 6f 72 74 65 64 22 20 * "unsupported"
fabc0 61 6e 64 20 6d 61 79 20 67 6f 20 61 77 61 79 20 and may go away
fabd0 69 6e 20 61 20 66 75 74 75 72 65 20 53 51 4c 69 in a future SQLi
fabe0 74 65 20 72 65 6c 65 61 73 65 2e 0a 2a 2a 0a 2a te release..**.*
fabf0 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 * When opening a
fac00 20 6e 65 77 20 73 68 61 72 65 64 2d 6d 65 6d 6f new shared-memo
fac10 72 79 20 66 69 6c 65 2c 20 69 66 20 6e 6f 20 6f ry file, if no o
fac20 74 68 65 72 20 69 6e 73 74 61 6e 63 65 73 20 6f ther instances o
fac30 66 20 74 68 61 74 0a 2a 2a 20 66 69 6c 65 20 61 f that.** file a
fac40 72 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 re currently ope
fac50 6e 2c 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 n, in this proce
fac60 73 73 20 6f 72 20 69 6e 20 6f 74 68 65 72 20 70 ss or in other p
fac70 72 6f 63 65 73 73 65 73 2c 20 74 68 65 6e 0a 2a rocesses, then.*
fac80 2a 20 74 68 65 20 66 69 6c 65 20 6d 75 73 74 20 * the file must
fac90 62 65 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 be truncated to
faca0 7a 65 72 6f 20 6c 65 6e 67 74 68 20 6f 72 20 68 zero length or h
facb0 61 76 65 20 69 74 73 20 68 65 61 64 65 72 20 63 ave its header c
facc0 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 leared..**.** If
facd0 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 the original da
face0 74 61 62 61 73 65 20 66 69 6c 65 20 28 70 44 62 tabase file (pDb
facf0 46 64 29 20 69 73 20 75 73 69 6e 67 20 74 68 65 Fd) is using the
fad00 20 22 75 6e 69 78 2d 65 78 63 6c 22 20 56 46 53 "unix-excl" VFS
fad10 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 .** that means t
fad20 68 61 74 20 61 6e 20 65 78 63 6c 75 73 69 76 65 hat an exclusive
fad30 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e lock is held on
fad40 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
fad50 6c 65 20 61 6e 64 0a 2a 2a 20 74 68 61 74 20 6e le and.** that n
fad60 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 o other processe
fad70 73 20 61 72 65 20 61 62 6c 65 20 74 6f 20 72 65 s are able to re
fad80 61 64 20 6f 72 20 77 72 69 74 65 20 74 68 65 20 ad or write the
fad90 64 61 74 61 62 61 73 65 2e 20 20 49 6e 0a 2a 2a database. In.**
fada0 20 74 68 61 74 20 63 61 73 65 2c 20 77 65 20 64 that case, we d
fadb0 6f 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6e 65 65 o not really nee
fadc0 64 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 2e d shared memory.
fadd0 20 20 4e 6f 20 73 68 61 72 65 64 20 6d 65 6d 6f No shared memo
fade0 72 79 0a 2a 2a 20 66 69 6c 65 20 69 73 20 63 72 ry.** file is cr
fadf0 65 61 74 65 64 2e 20 20 54 68 65 20 73 68 61 72 eated. The shar
fae00 65 64 20 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 62 ed memory will b
fae10 65 20 73 69 6d 75 6c 61 74 65 64 20 77 69 74 68 e simulated with
fae20 20 68 65 61 70 20 6d 65 6d 6f 72 79 2e 0a 2a 2f heap memory..*/
fae30 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
fae40 4f 70 65 6e 53 68 61 72 65 64 4d 65 6d 6f 72 79 OpenSharedMemory
fae50 28 75 6e 69 78 46 69 6c 65 20 2a 70 44 62 46 64 (unixFile *pDbFd
fae60 29 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 ){. struct unix
fae70 53 68 6d 20 2a 70 20 3d 20 30 3b 20 20 20 20 20 Shm *p = 0;
fae80 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 6e /* The conn
fae90 65 63 74 69 6f 6e 20 74 6f 20 62 65 20 6f 70 65 ection to be ope
faea0 6e 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ned */. struct
faeb0 75 6e 69 78 53 68 6d 4e 6f 64 65 20 2a 70 53 68 unixShmNode *pSh
faec0 6d 4e 6f 64 65 3b 20 20 20 2f 2a 20 54 68 65 20 mNode; /* The
faed0 75 6e 64 65 72 6c 79 69 6e 67 20 6d 6d 61 70 70 underlying mmapp
faee0 65 64 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 ed file */. int
faef0 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
faf00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
faf10 52 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 20 Result code */.
faf20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a unixInodeInfo *
faf30 70 49 6e 6f 64 65 3b 20 20 20 20 20 20 20 20 20 pInode;
faf40 20 2f 2a 20 54 68 65 20 69 6e 6f 64 65 20 6f 66 /* The inode of
faf50 20 66 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a fd */. char *z
faf60 53 68 6d 46 69 6c 65 6e 61 6d 65 3b 20 20 20 20 ShmFilename;
faf70 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
faf80 20 6f 66 20 74 68 65 20 66 69 6c 65 20 75 73 65 of the file use
faf90 64 20 66 6f 72 20 53 48 4d 20 2a 2f 0a 20 20 69 d for SHM */. i
fafa0 6e 74 20 6e 53 68 6d 46 69 6c 65 6e 61 6d 65 3b nt nShmFilename;
fafb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
fafc0 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 53 48 * Size of the SH
fafd0 4d 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 62 79 M filename in by
fafe0 74 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c tes */.. /* All
faff0 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 ocate space for
fb000 74 68 65 20 6e 65 77 20 75 6e 69 78 53 68 6d 20 the new unixShm
fb010 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 70 20 3d object. */. p =
fb020 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
fb030 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 sizeof(*p) );.
fb040 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
fb050 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
fb060 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 . memset(p, 0,
fb070 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 61 sizeof(*p));. a
fb080 73 73 65 72 74 28 20 70 44 62 46 64 2d 3e 70 53 ssert( pDbFd->pS
fb090 68 6d 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43 hm==0 );.. /* C
fb0a0 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 61 heck to see if a
fb0b0 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 6f 62 6a unixShmNode obj
fb0c0 65 63 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 ect already exis
fb0d0 74 73 2e 20 52 65 75 73 65 20 61 6e 20 65 78 69 ts. Reuse an exi
fb0e0 73 74 69 6e 67 0a 20 20 2a 2a 20 6f 6e 65 20 69 sting. ** one i
fb0f0 66 20 70 72 65 73 65 6e 74 2e 20 43 72 65 61 74 f present. Creat
fb100 65 20 61 20 6e 65 77 20 6f 6e 65 20 69 66 20 6e e a new one if n
fb110 65 63 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 ecessary.. */.
fb120 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
fb130 29 3b 0a 20 20 70 49 6e 6f 64 65 20 3d 20 70 44 );. pInode = pD
fb140 62 46 64 2d 3e 70 49 6e 6f 64 65 3b 0a 20 20 70 bFd->pInode;. p
fb150 53 68 6d 4e 6f 64 65 20 3d 20 70 49 6e 6f 64 65 ShmNode = pInode
fb160 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0a 20 20 69 66 ->pShmNode;. if
fb170 28 20 70 53 68 6d 4e 6f 64 65 3d 3d 30 20 29 7b ( pShmNode==0 ){
fb180 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 . struct stat
fb190 20 73 53 74 61 74 3b 20 20 20 20 20 20 20 20 20 sStat;
fb1a0 20 20 20 20 20 20 20 20 2f 2a 20 66 73 74 61 74 /* fstat
fb1b0 28 29 20 69 6e 66 6f 20 66 6f 72 20 64 61 74 61 () info for data
fb1c0 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 base file */..
fb1d0 20 20 2f 2a 20 43 61 6c 6c 20 66 73 74 61 74 28 /* Call fstat(
fb1e0 29 20 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20 ) to figure out
fb1f0 74 68 65 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 the permissions
fb200 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
fb210 66 69 6c 65 2e 20 49 66 0a 20 20 20 20 2a 2a 20 file. If. **
fb220 61 20 6e 65 77 20 2a 2d 73 68 6d 20 66 69 6c 65 a new *-shm file
fb230 20 69 73 20 63 72 65 61 74 65 64 2c 20 61 6e 20 is created, an
fb240 61 74 74 65 6d 70 74 20 77 69 6c 6c 20 62 65 20 attempt will be
fb250 6d 61 64 65 20 74 6f 20 63 72 65 61 74 65 20 69 made to create i
fb260 74 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74 68 t. ** with th
fb270 65 20 73 61 6d 65 20 70 65 72 6d 69 73 73 69 6f e same permissio
fb280 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ns.. */. i
fb290 66 28 20 6f 73 46 73 74 61 74 28 70 44 62 46 64 f( osFstat(pDbFd
fb2a0 2d 3e 68 2c 20 26 73 53 74 61 74 29 20 26 26 20 ->h, &sStat) &&
fb2b0 70 49 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73 pInode->bProcess
fb2c0 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Lock==0 ){.
fb2d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rc = SQLITE_IOE
fb2e0 52 52 5f 46 53 54 41 54 3b 0a 20 20 20 20 20 20 RR_FSTAT;.
fb2f0 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65 72 goto shm_open_er
fb300 72 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 r;. }..#ifdef
fb310 20 53 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52 45 SQLITE_SHM_DIRE
fb320 43 54 4f 52 59 0a 20 20 20 20 6e 53 68 6d 46 69 CTORY. nShmFi
fb330 6c 65 6e 61 6d 65 20 3d 20 73 69 7a 65 6f 66 28 lename = sizeof(
fb340 53 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52 45 43 SQLITE_SHM_DIREC
fb350 54 4f 52 59 29 20 2b 20 33 31 3b 0a 23 65 6c 73 TORY) + 31;.#els
fb360 65 0a 20 20 20 20 6e 53 68 6d 46 69 6c 65 6e 61 e. nShmFilena
fb370 6d 65 20 3d 20 36 20 2b 20 28 69 6e 74 29 73 74 me = 6 + (int)st
fb380 72 6c 65 6e 28 70 44 62 46 64 2d 3e 7a 50 61 74 rlen(pDbFd->zPat
fb390 68 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 h);.#endif. p
fb3a0 53 68 6d 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65 ShmNode = sqlite
fb3b0 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 3_malloc( sizeof
fb3c0 28 2a 70 53 68 6d 4e 6f 64 65 29 20 2b 20 6e 53 (*pShmNode) + nS
fb3d0 68 6d 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 hmFilename );.
fb3e0 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 3d 3d if( pShmNode==
fb3f0 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
fb400 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
fb410 20 20 20 20 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 goto shm_ope
fb420 6e 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 20 20 20 n_err;. }.
fb430 20 6d 65 6d 73 65 74 28 70 53 68 6d 4e 6f 64 65 memset(pShmNode
fb440 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 53 68 , 0, sizeof(*pSh
fb450 6d 4e 6f 64 65 29 2b 6e 53 68 6d 46 69 6c 65 6e mNode)+nShmFilen
fb460 61 6d 65 29 3b 0a 20 20 20 20 7a 53 68 6d 46 69 ame);. zShmFi
fb470 6c 65 6e 61 6d 65 20 3d 20 70 53 68 6d 4e 6f 64 lename = pShmNod
fb480 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 28 e->zFilename = (
fb490 63 68 61 72 2a 29 26 70 53 68 6d 4e 6f 64 65 5b char*)&pShmNode[
fb4a0 31 5d 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 1];.#ifdef SQLIT
fb4b0 45 5f 53 48 4d 5f 44 49 52 45 43 54 4f 52 59 0a E_SHM_DIRECTORY.
fb4c0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
fb4d0 69 6e 74 66 28 6e 53 68 6d 46 69 6c 65 6e 61 6d intf(nShmFilenam
fb4e0 65 2c 20 7a 53 68 6d 46 69 6c 65 6e 61 6d 65 2c e, zShmFilename,
fb4f0 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
fb500 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 48 SQLITE_SH
fb510 4d 5f 44 49 52 45 43 54 4f 52 59 20 22 2f 73 71 M_DIRECTORY "/sq
fb520 6c 69 74 65 2d 73 68 6d 2d 25 78 2d 25 78 22 2c lite-shm-%x-%x",
fb530 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
fb540 20 20 20 20 20 20 28 75 33 32 29 73 53 74 61 74 (u32)sStat
fb550 2e 73 74 5f 69 6e 6f 2c 20 28 75 33 32 29 73 53 .st_ino, (u32)sS
fb560 74 61 74 2e 73 74 5f 64 65 76 29 3b 0a 23 65 6c tat.st_dev);.#el
fb570 73 65 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 se. sqlite3_s
fb580 6e 70 72 69 6e 74 66 28 6e 53 68 6d 46 69 6c 65 nprintf(nShmFile
fb590 6e 61 6d 65 2c 20 7a 53 68 6d 46 69 6c 65 6e 61 name, zShmFilena
fb5a0 6d 65 2c 20 22 25 73 2d 73 68 6d 22 2c 20 70 44 me, "%s-shm", pD
fb5b0 62 46 64 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 20 bFd->zPath);.
fb5c0 20 73 71 6c 69 74 65 33 46 69 6c 65 53 75 66 66 sqlite3FileSuff
fb5d0 69 78 33 28 70 44 62 46 64 2d 3e 7a 50 61 74 68 ix3(pDbFd->zPath
fb5e0 2c 20 7a 53 68 6d 46 69 6c 65 6e 61 6d 65 29 3b , zShmFilename);
fb5f0 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 53 68 6d .#endif. pShm
fb600 4e 6f 64 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 Node->h = -1;.
fb610 20 20 70 44 62 46 64 2d 3e 70 49 6e 6f 64 65 2d pDbFd->pInode-
fb620 3e 70 53 68 6d 4e 6f 64 65 20 3d 20 70 53 68 6d >pShmNode = pShm
fb630 4e 6f 64 65 3b 0a 20 20 20 20 70 53 68 6d 4e 6f Node;. pShmNo
fb640 64 65 2d 3e 70 49 6e 6f 64 65 20 3d 20 70 44 62 de->pInode = pDb
fb650 46 64 2d 3e 70 49 6e 6f 64 65 3b 0a 20 20 20 20 Fd->pInode;.
fb660 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 20 pShmNode->mutex
fb670 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f = sqlite3_mutex_
fb680 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 alloc(SQLITE_MUT
fb690 45 58 5f 46 41 53 54 29 3b 0a 20 20 20 20 69 66 EX_FAST);. if
fb6a0 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 ( pShmNode->mute
fb6b0 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 x==0 ){. rc
fb6c0 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
fb6d0 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 68 6d 5f . goto shm_
fb6e0 6f 70 65 6e 5f 65 72 72 3b 0a 20 20 20 20 7d 0a open_err;. }.
fb6f0 0a 20 20 20 20 69 66 28 20 70 49 6e 6f 64 65 2d . if( pInode-
fb700 3e 62 50 72 6f 63 65 73 73 4c 6f 63 6b 3d 3d 30 >bProcessLock==0
fb710 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 ){. int op
fb720 65 6e 46 6c 61 67 73 20 3d 20 4f 5f 52 44 57 52 enFlags = O_RDWR
fb730 20 7c 20 4f 5f 43 52 45 41 54 3b 0a 20 20 20 20 | O_CREAT;.
fb740 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 75 72 if( sqlite3_ur
fb750 69 5f 62 6f 6f 6c 65 61 6e 28 70 44 62 46 64 2d i_boolean(pDbFd-
fb760 3e 7a 50 61 74 68 2c 20 22 72 65 61 64 6f 6e 6c >zPath, "readonl
fb770 79 5f 73 68 6d 22 2c 20 30 29 20 29 7b 0a 20 20 y_shm", 0) ){.
fb780 20 20 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 openFlags
fb790 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 20 20 = O_RDONLY;.
fb7a0 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 69 73 pShmNode->is
fb7b0 52 65 61 64 6f 6e 6c 79 20 3d 20 31 3b 0a 20 20 Readonly = 1;.
fb7c0 20 20 20 20 7d 0a 20 20 20 20 20 20 70 53 68 6d }. pShm
fb7d0 4e 6f 64 65 2d 3e 68 20 3d 20 72 6f 62 75 73 74 Node->h = robust
fb7e0 5f 6f 70 65 6e 28 7a 53 68 6d 46 69 6c 65 6e 61 _open(zShmFilena
fb7f0 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 28 me, openFlags, (
fb800 73 53 74 61 74 2e 73 74 5f 6d 6f 64 65 26 30 37 sStat.st_mode&07
fb810 37 37 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 77));. if(
fb820 70 53 68 6d 4e 6f 64 65 2d 3e 68 3c 30 20 29 7b pShmNode->h<0 ){
fb830 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e . rc = un
fb840 69 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 ixLogError(SQLIT
fb850 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 2c E_CANTOPEN_BKPT,
fb860 20 22 6f 70 65 6e 22 2c 20 7a 53 68 6d 46 69 6c "open", zShmFil
fb870 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 ename);.
fb880 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65 72 goto shm_open_er
fb890 72 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 r;. }..
fb8a0 20 20 2f 2a 20 49 66 20 74 68 69 73 20 70 72 6f /* If this pro
fb8b0 63 65 73 73 20 69 73 20 72 75 6e 6e 69 6e 67 20 cess is running
fb8c0 61 73 20 72 6f 6f 74 2c 20 6d 61 6b 65 20 73 75 as root, make su
fb8d0 72 65 20 74 68 61 74 20 74 68 65 20 53 48 4d 20 re that the SHM
fb8e0 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 file. ** is
fb8f0 20 6f 77 6e 65 64 20 62 79 20 74 68 65 20 73 61 owned by the sa
fb900 6d 65 20 75 73 65 72 20 74 68 61 74 20 6f 77 6e me user that own
fb910 73 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 s the original d
fb920 61 74 61 62 61 73 65 2e 20 20 4f 74 68 65 72 77 atabase. Otherw
fb930 69 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 ise,. ** th
fb940 65 20 6f 72 69 67 69 6e 61 6c 20 6f 77 6e 65 72 e original owner
fb950 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c will not be abl
fb960 65 20 74 6f 20 63 6f 6e 6e 65 63 74 2e 0a 20 20 e to connect..
fb970 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6f 73 46 */. osF
fb980 63 68 6f 77 6e 28 70 53 68 6d 4e 6f 64 65 2d 3e chown(pShmNode->
fb990 68 2c 20 73 53 74 61 74 2e 73 74 5f 75 69 64 2c h, sStat.st_uid,
fb9a0 20 73 53 74 61 74 2e 73 74 5f 67 69 64 29 3b 0a sStat.st_gid);.
fb9b0 20 20 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 . /* Chec
fb9c0 6b 20 74 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 k to see if anot
fb9d0 68 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 68 her process is h
fb9e0 6f 6c 64 69 6e 67 20 74 68 65 20 64 65 61 64 2d olding the dead-
fb9f0 6d 61 6e 20 73 77 69 74 63 68 2e 0a 20 20 20 20 man switch..
fba00 20 20 2a 2a 20 49 66 20 6e 6f 74 2c 20 74 72 75 ** If not, tru
fba10 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 20 74 ncate the file t
fba20 6f 20 7a 65 72 6f 20 6c 65 6e 67 74 68 2e 20 0a o zero length. .
fba30 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 */. r
fba40 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
fba50 20 20 20 20 20 69 66 28 20 75 6e 69 78 53 68 6d if( unixShm
fba60 53 79 73 74 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e SystemLock(pShmN
fba70 6f 64 65 2c 20 46 5f 57 52 4c 43 4b 2c 20 55 4e ode, F_WRLCK, UN
fba80 49 58 5f 53 48 4d 5f 44 4d 53 2c 20 31 29 3d 3d IX_SHM_DMS, 1)==
fba90 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
fbaa0 20 20 20 20 20 69 66 28 20 72 6f 62 75 73 74 5f if( robust_
fbab0 66 74 72 75 6e 63 61 74 65 28 70 53 68 6d 4e 6f ftruncate(pShmNo
fbac0 64 65 2d 3e 68 2c 20 30 29 20 29 7b 0a 20 20 20 de->h, 0) ){.
fbad0 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 78 rc = unix
fbae0 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f LogError(SQLITE_
fbaf0 49 4f 45 52 52 5f 53 48 4d 4f 50 45 4e 2c 20 22 IOERR_SHMOPEN, "
fbb00 66 74 72 75 6e 63 61 74 65 22 2c 20 7a 53 68 6d ftruncate", zShm
fbb10 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Filename);.
fbb20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
fbb30 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
fbb40 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
fbb50 72 63 20 3d 20 75 6e 69 78 53 68 6d 53 79 73 74 rc = unixShmSyst
fbb60 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c emLock(pShmNode,
fbb70 20 46 5f 52 44 4c 43 4b 2c 20 55 4e 49 58 5f 53 F_RDLCK, UNIX_S
fbb80 48 4d 5f 44 4d 53 2c 20 31 29 3b 0a 20 20 20 20 HM_DMS, 1);.
fbb90 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
fbba0 20 29 20 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 6e ) goto shm_open
fbbb0 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a _err;. }. }.
fbbc0 0a 20 20 2f 2a 20 4d 61 6b 65 20 74 68 65 20 6e . /* Make the n
fbbd0 65 77 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 20 ew connection a
fbbe0 63 68 69 6c 64 20 6f 66 20 74 68 65 20 75 6e 69 child of the uni
fbbf0 78 53 68 6d 4e 6f 64 65 20 2a 2f 0a 20 20 70 2d xShmNode */. p-
fbc00 3e 70 53 68 6d 4e 6f 64 65 20 3d 20 70 53 68 6d >pShmNode = pShm
fbc10 4e 6f 64 65 3b 0a 23 69 66 64 65 66 20 53 51 4c Node;.#ifdef SQL
fbc20 49 54 45 5f 44 45 42 55 47 0a 20 20 70 2d 3e 69 ITE_DEBUG. p->i
fbc30 64 20 3d 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 65 d = pShmNode->ne
fbc40 78 74 53 68 6d 49 64 2b 2b 3b 0a 23 65 6e 64 69 xtShmId++;.#endi
fbc50 66 0a 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 f. pShmNode->nR
fbc60 65 66 2b 2b 3b 0a 20 20 70 44 62 46 64 2d 3e 70 ef++;. pDbFd->p
fbc70 53 68 6d 20 3d 20 70 3b 0a 20 20 75 6e 69 78 4c Shm = p;. unixL
fbc80 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 0a 20 20 eaveMutex();..
fbc90 2f 2a 20 54 68 65 20 72 65 66 65 72 65 6e 63 65 /* The reference
fbca0 20 63 6f 75 6e 74 20 6f 6e 20 70 53 68 6d 4e 6f count on pShmNo
fbcb0 64 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 de has already b
fbcc0 65 65 6e 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 een incremented
fbcd0 75 6e 64 65 72 0a 20 20 2a 2a 20 74 68 65 20 63 under. ** the c
fbce0 6f 76 65 72 20 6f 66 20 74 68 65 20 75 6e 69 78 over of the unix
fbcf0 45 6e 74 65 72 4d 75 74 65 78 28 29 20 6d 75 74 EnterMutex() mut
fbd00 65 78 20 61 6e 64 20 74 68 65 20 70 6f 69 6e 74 ex and the point
fbd10 65 72 20 66 72 6f 6d 20 74 68 65 0a 20 20 2a 2a er from the. **
fbd20 20 6e 65 77 20 28 73 74 72 75 63 74 20 75 6e 69 new (struct uni
fbd30 78 53 68 6d 29 20 6f 62 6a 65 63 74 20 74 6f 20 xShm) object to
fbd40 74 68 65 20 70 53 68 6d 4e 6f 64 65 20 68 61 73 the pShmNode has
fbd50 20 62 65 65 6e 20 73 65 74 2e 20 41 6c 6c 20 74 been set. All t
fbd60 68 61 74 20 69 73 0a 20 20 2a 2a 20 6c 65 66 74 hat is. ** left
fbd70 20 74 6f 20 64 6f 20 69 73 20 74 6f 20 6c 69 6e to do is to lin
fbd80 6b 20 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 k the new object
fbd90 20 69 6e 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 into the linked
fbda0 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 0a 20 list starting.
fbdb0 20 2a 2a 20 61 74 20 70 53 68 6d 4e 6f 64 65 2d ** at pShmNode-
fbdc0 3e 70 46 69 72 73 74 2e 20 54 68 69 73 20 6d 75 >pFirst. This mu
fbdd0 73 74 20 62 65 20 64 6f 6e 65 20 77 68 69 6c 65 st be done while
fbde0 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 70 53 68 holding the pSh
fbdf0 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 20 0a 20 20 mNode->mutex .
fbe00 2a 2a 20 6d 75 74 65 78 2e 0a 20 20 2a 2f 0a 20 ** mutex.. */.
fbe10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
fbe20 6e 74 65 72 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d nter(pShmNode->m
fbe30 75 74 65 78 29 3b 0a 20 20 70 2d 3e 70 4e 65 78 utex);. p->pNex
fbe40 74 20 3d 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 t = pShmNode->pF
fbe50 69 72 73 74 3b 0a 20 20 70 53 68 6d 4e 6f 64 65 irst;. pShmNode
fbe60 2d 3e 70 46 69 72 73 74 20 3d 20 70 3b 0a 20 20 ->pFirst = p;.
fbe70 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
fbe80 61 76 65 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 ave(pShmNode->mu
fbe90 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 tex);. return S
fbea0 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 QLITE_OK;.. /*
fbeb0 4a 75 6d 70 20 68 65 72 65 20 6f 6e 20 61 6e 79 Jump here on any
fbec0 20 65 72 72 6f 72 20 2a 2f 0a 73 68 6d 5f 6f 70 error */.shm_op
fbed0 65 6e 5f 65 72 72 3a 0a 20 20 75 6e 69 78 53 68 en_err:. unixSh
fbee0 6d 50 75 72 67 65 28 70 44 62 46 64 29 3b 20 20 mPurge(pDbFd);
fbef0 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c /* This cal
fbf00 6c 20 66 72 65 65 73 20 70 53 68 6d 4e 6f 64 65 l frees pShmNode
fbf10 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a if required */.
fbf20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
fbf30 29 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 );. unixLeaveMu
fbf40 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 tex();. return
fbf50 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
fbf60 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
fbf70 6c 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 lled to obtain a
fbf80 20 70 6f 69 6e 74 65 72 20 74 6f 20 72 65 67 69 pointer to regi
fbf90 6f 6e 20 69 52 65 67 69 6f 6e 20 6f 66 20 74 68 on iRegion of th
fbfa0 65 20 0a 2a 2a 20 73 68 61 72 65 64 2d 6d 65 6d e .** shared-mem
fbfb0 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 ory associated w
fbfc0 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 ith the database
fbfd0 20 66 69 6c 65 20 66 64 2e 20 53 68 61 72 65 64 file fd. Shared
fbfe0 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 73 20 -memory regions
fbff0 0a 2a 2a 20 61 72 65 20 6e 75 6d 62 65 72 65 64 .** are numbered
fc000 20 73 74 61 72 74 69 6e 67 20 66 72 6f 6d 20 7a starting from z
fc010 65 72 6f 2e 20 45 61 63 68 20 73 68 61 72 65 64 ero. Each shared
fc020 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 20 69 -memory region i
fc030 73 20 73 7a 52 65 67 69 6f 6e 20 0a 2a 2a 20 62 s szRegion .** b
fc040 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a ytes in size..**
fc050 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 .** If an error
fc060 6f 63 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72 occurs, an error
fc070 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
fc080 64 20 61 6e 64 20 2a 70 70 20 69 73 20 73 65 74 d and *pp is set
fc090 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 to NULL..**.**
fc0a0 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 Otherwise, if th
fc0b0 65 20 62 45 78 74 65 6e 64 20 70 61 72 61 6d 65 e bExtend parame
fc0c0 74 65 72 20 69 73 20 30 20 61 6e 64 20 74 68 65 ter is 0 and the
fc0d0 20 72 65 71 75 65 73 74 65 64 20 73 68 61 72 65 requested share
fc0e0 64 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 72 65 67 69 d-memory.** regi
fc0f0 6f 6e 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 on has not been
fc100 61 6c 6c 6f 63 61 74 65 64 20 28 62 79 20 61 6e allocated (by an
fc110 79 20 63 6c 69 65 6e 74 2c 20 69 6e 63 6c 75 64 y client, includ
fc120 69 6e 67 20 6f 6e 65 20 72 75 6e 6e 69 6e 67 20 ing one running
fc130 69 6e 20 61 0a 2a 2a 20 73 65 70 61 72 61 74 65 in a.** separate
fc140 20 70 72 6f 63 65 73 73 29 2c 20 74 68 65 6e 20 process), then
fc150 2a 70 70 20 69 73 20 73 65 74 20 74 6f 20 4e 55 *pp is set to NU
fc160 4c 4c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b LL and SQLITE_OK
fc170 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 0a 2a returned. If .*
fc180 2a 20 62 45 78 74 65 6e 64 20 69 73 20 6e 6f 6e * bExtend is non
fc190 2d 7a 65 72 6f 20 61 6e 64 20 74 68 65 20 72 65 -zero and the re
fc1a0 71 75 65 73 74 65 64 20 73 68 61 72 65 64 2d 6d quested shared-m
fc1b0 65 6d 6f 72 79 20 72 65 67 69 6f 6e 20 68 61 73 emory region has
fc1c0 20 6e 6f 74 20 79 65 74 20 0a 2a 2a 20 62 65 65 not yet .** bee
fc1d0 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 n allocated, it
fc1e0 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 is allocated by
fc1f0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a this function..*
fc200 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 68 61 72 *.** If the shar
fc210 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e ed-memory region
fc220 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
fc230 6e 20 61 6c 6c 6f 63 61 74 65 64 20 6f 72 20 69 n allocated or i
fc240 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a s allocated by.*
fc250 2a 20 74 68 69 73 20 63 61 6c 6c 20 61 73 20 64 * this call as d
fc260 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2c 20 escribed above,
fc270 74 68 65 6e 20 69 74 20 69 73 20 6d 61 70 70 65 then it is mappe
fc280 64 20 69 6e 74 6f 20 74 68 69 73 20 70 72 6f 63 d into this proc
fc290 65 73 73 65 73 20 0a 2a 2a 20 61 64 64 72 65 73 esses .** addres
fc2a0 73 20 73 70 61 63 65 20 28 69 66 20 69 74 20 69 s space (if it i
fc2b0 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 29 2c 20 s not already),
fc2c0 2a 70 70 20 69 73 20 73 65 74 20 74 6f 20 70 6f *pp is set to po
fc2d0 69 6e 74 20 74 6f 20 74 68 65 20 6d 61 70 70 65 int to the mappe
fc2e0 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e 64 d .** memory and
fc2f0 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 SQLITE_OK retur
fc300 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
fc310 6e 74 20 75 6e 69 78 53 68 6d 4d 61 70 28 0a 20 nt unixShmMap(.
fc320 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 sqlite3_file *f
fc330 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
fc340 20 2f 2a 20 48 61 6e 64 6c 65 20 6f 70 65 6e 20 /* Handle open
fc350 6f 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 on database file
fc360 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 69 6f */. int iRegio
fc370 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n,
fc380 20 20 20 20 20 20 2f 2a 20 52 65 67 69 6f 6e 20 /* Region
fc390 74 6f 20 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 to retrieve */.
fc3a0 20 69 6e 74 20 73 7a 52 65 67 69 6f 6e 2c 20 20 int szRegion,
fc3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fc3c0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 72 65 67 69 /* Size of regi
fc3d0 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 62 45 78 ons */. int bEx
fc3e0 74 65 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 tend,
fc3f0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
fc400 20 74 6f 20 65 78 74 65 6e 64 20 66 69 6c 65 20 to extend file
fc410 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a if necessary */.
fc420 20 20 76 6f 69 64 20 76 6f 6c 61 74 69 6c 65 20 void volatile
fc430 2a 2a 70 70 20 20 20 20 20 20 20 20 20 20 20 20 **pp
fc440 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 70 70 65 64 /* OUT: Mapped
fc450 20 6d 65 6d 6f 72 79 20 2a 2f 0a 29 7b 0a 20 20 memory */.){.
fc460 75 6e 69 78 46 69 6c 65 20 2a 70 44 62 46 64 20 unixFile *pDbFd
fc470 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 66 64 3b = (unixFile*)fd;
fc480 0a 20 20 75 6e 69 78 53 68 6d 20 2a 70 3b 0a 20 . unixShm *p;.
fc490 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 2a 70 53 unixShmNode *pS
fc4a0 68 6d 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 72 63 hmNode;. int rc
fc4b0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
fc4c0 20 2f 2a 20 49 66 20 74 68 65 20 73 68 61 72 65 /* If the share
fc4d0 64 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 68 61 d-memory file ha
fc4e0 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f s not yet been o
fc4f0 70 65 6e 65 64 2c 20 6f 70 65 6e 20 69 74 20 6e pened, open it n
fc500 6f 77 2e 20 2a 2f 0a 20 20 69 66 28 20 70 44 62 ow. */. if( pDb
fc510 46 64 2d 3e 70 53 68 6d 3d 3d 30 20 29 7b 0a 20 Fd->pShm==0 ){.
fc520 20 20 20 72 63 20 3d 20 75 6e 69 78 4f 70 65 6e rc = unixOpen
fc530 53 68 61 72 65 64 4d 65 6d 6f 72 79 28 70 44 62 SharedMemory(pDb
fc540 46 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 Fd);. if( rc!
fc550 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
fc560 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 70 urn rc;. }.. p
fc570 20 3d 20 70 44 62 46 64 2d 3e 70 53 68 6d 3b 0a = pDbFd->pShm;.
fc580 20 20 70 53 68 6d 4e 6f 64 65 20 3d 20 70 2d 3e pShmNode = p->
fc590 70 53 68 6d 4e 6f 64 65 3b 0a 20 20 73 71 6c 69 pShmNode;. sqli
fc5a0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
fc5b0 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 pShmNode->mutex)
fc5c0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 52 65 ;. assert( szRe
fc5d0 67 69 6f 6e 3d 3d 70 53 68 6d 4e 6f 64 65 2d 3e gion==pShmNode->
fc5e0 73 7a 52 65 67 69 6f 6e 20 7c 7c 20 70 53 68 6d szRegion || pShm
fc5f0 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f 6e 3d 3d 30 Node->nRegion==0
fc600 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 );. assert( pS
fc610 68 6d 4e 6f 64 65 2d 3e 70 49 6e 6f 64 65 3d 3d hmNode->pInode==
fc620 70 44 62 46 64 2d 3e 70 49 6e 6f 64 65 20 29 3b pDbFd->pInode );
fc630 0a 20 20 61 73 73 65 72 74 28 20 70 53 68 6d 4e . assert( pShmN
fc640 6f 64 65 2d 3e 68 3e 3d 30 20 7c 7c 20 70 44 62 ode->h>=0 || pDb
fc650 46 64 2d 3e 70 49 6e 6f 64 65 2d 3e 62 50 72 6f Fd->pInode->bPro
fc660 63 65 73 73 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 cessLock==1 );.
fc670 20 61 73 73 65 72 74 28 20 70 53 68 6d 4e 6f 64 assert( pShmNod
fc680 65 2d 3e 68 3c 30 20 7c 7c 20 70 44 62 46 64 2d e->h<0 || pDbFd-
fc690 3e 70 49 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73 >pInode->bProces
fc6a0 73 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 69 sLock==0 );.. i
fc6b0 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 f( pShmNode->nRe
fc6c0 67 69 6f 6e 3c 3d 69 52 65 67 69 6f 6e 20 29 7b gion<=iRegion ){
fc6d0 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 70 4e 65 . char **apNe
fc6e0 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 w;
fc6f0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 61 /* New a
fc700 70 52 65 67 69 6f 6e 5b 5d 20 61 72 72 61 79 20 pRegion[] array
fc710 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 */. int nByte
fc720 20 3d 20 28 69 52 65 67 69 6f 6e 2b 31 29 2a 73 = (iRegion+1)*s
fc730 7a 52 65 67 69 6f 6e 3b 20 20 2f 2a 20 4d 69 6e zRegion; /* Min
fc740 69 6d 75 6d 20 72 65 71 75 69 72 65 64 20 66 69 imum required fi
fc750 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 73 le size */. s
fc760 74 72 75 63 74 20 73 74 61 74 20 73 53 74 61 74 truct stat sStat
fc770 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
fc780 20 20 2f 2a 20 55 73 65 64 20 62 79 20 66 73 74 /* Used by fst
fc790 61 74 28 29 20 2a 2f 0a 0a 20 20 20 20 70 53 68 at() */.. pSh
fc7a0 6d 4e 6f 64 65 2d 3e 73 7a 52 65 67 69 6f 6e 20 mNode->szRegion
fc7b0 3d 20 73 7a 52 65 67 69 6f 6e 3b 0a 0a 20 20 20 = szRegion;..
fc7c0 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 68 if( pShmNode->h
fc7d0 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 >=0 ){. /*
fc7e0 54 68 65 20 72 65 71 75 65 73 74 65 64 20 72 65 The requested re
fc7f0 67 69 6f 6e 20 69 73 20 6e 6f 74 20 6d 61 70 70 gion is not mapp
fc800 65 64 20 69 6e 74 6f 20 74 68 69 73 20 70 72 6f ed into this pro
fc810 63 65 73 73 65 73 20 61 64 64 72 65 73 73 20 73 cesses address s
fc820 70 61 63 65 2e 0a 20 20 20 20 20 20 2a 2a 20 43 pace.. ** C
fc830 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 69 heck to see if i
fc840 74 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 t has been alloc
fc850 61 74 65 64 20 28 69 2e 65 2e 20 69 66 20 74 68 ated (i.e. if th
fc860 65 20 77 61 6c 2d 69 6e 64 65 78 20 66 69 6c 65 e wal-index file
fc870 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 6c 61 72 is. ** lar
fc880 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 63 6f 6e ge enough to con
fc890 74 61 69 6e 20 74 68 65 20 72 65 71 75 65 73 74 tain the request
fc8a0 65 64 20 72 65 67 69 6f 6e 29 2e 0a 20 20 20 20 ed region)..
fc8b0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6f */. if( o
fc8c0 73 46 73 74 61 74 28 70 53 68 6d 4e 6f 64 65 2d sFstat(pShmNode-
fc8d0 3e 68 2c 20 26 73 53 74 61 74 29 20 29 7b 0a 20 >h, &sStat) ){.
fc8e0 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
fc8f0 54 45 5f 49 4f 45 52 52 5f 53 48 4d 53 49 5a 45 TE_IOERR_SHMSIZE
fc900 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73 ;. goto s
fc910 68 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 hmpage_out;.
fc920 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 69 66 28 }. . if(
fc930 20 73 53 74 61 74 2e 73 74 5f 73 69 7a 65 3c 6e sStat.st_size<n
fc940 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Byte ){.
fc950 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 65 64 /* The requested
fc960 20 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 20 64 memory region d
fc970 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 49 oes not exist. I
fc980 66 20 62 45 78 74 65 6e 64 20 69 73 20 73 65 74 f bExtend is set
fc990 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 to. ** f
fc9a0 61 6c 73 65 2c 20 65 78 69 74 20 65 61 72 6c 79 alse, exit early
fc9b0 2e 20 2a 70 70 20 77 69 6c 6c 20 62 65 20 73 65 . *pp will be se
fc9c0 74 20 74 6f 20 4e 55 4c 4c 20 61 6e 64 20 53 51 t to NULL and SQ
fc9d0 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 LITE_OK returned
fc9e0 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 .. **.
fc9f0 20 20 20 20 20 2a 2a 20 41 6c 74 65 72 6e 61 74 ** Alternat
fca00 69 76 65 6c 79 2c 20 69 66 20 62 45 78 74 65 6e ively, if bExten
fca10 64 20 69 73 20 74 72 75 65 2c 20 75 73 65 20 66 d is true, use f
fca20 74 72 75 6e 63 61 74 65 28 29 20 74 6f 20 61 6c truncate() to al
fca30 6c 6f 63 61 74 65 0a 20 20 20 20 20 20 20 20 2a locate. *
fca40 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
fca50 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 2e 0a 20 memory region..
fca60 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
fca70 20 20 69 66 28 20 21 62 45 78 74 65 6e 64 20 29 if( !bExtend )
fca80 20 67 6f 74 6f 20 73 68 6d 70 61 67 65 5f 6f 75 goto shmpage_ou
fca90 74 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 48 t;.#if defined(H
fcaa0 41 56 45 5f 50 4f 53 49 58 5f 46 41 4c 4c 4f 43 AVE_POSIX_FALLOC
fcab0 41 54 45 29 20 26 26 20 48 41 56 45 5f 50 4f 53 ATE) && HAVE_POS
fcac0 49 58 5f 46 41 4c 4c 4f 43 41 54 45 0a 20 20 20 IX_FALLOCATE.
fcad0 20 20 20 20 20 69 66 28 20 6f 73 46 61 6c 6c 6f if( osFallo
fcae0 63 61 74 65 28 70 53 68 6d 4e 6f 64 65 2d 3e 68 cate(pShmNode->h
fcaf0 2c 20 73 53 74 61 74 2e 73 74 5f 73 69 7a 65 2c , sStat.st_size,
fcb00 20 6e 42 79 74 65 29 21 3d 30 20 29 7b 0a 20 20 nByte)!=0 ){.
fcb10 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 rc = uni
fcb20 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 xLogError(SQLITE
fcb30 5f 49 4f 45 52 52 5f 53 48 4d 53 49 5a 45 2c 20 _IOERR_SHMSIZE,
fcb40 22 66 61 6c 6c 6f 63 61 74 65 22 2c 0a 20 20 20 "fallocate",.
fcb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fcb60 20 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 pShmNod
fcb70 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 e->zFilename);.
fcb80 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73 68 goto sh
fcb90 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 20 mpage_out;.
fcba0 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 20 }.#else.
fcbb0 20 20 20 69 66 28 20 72 6f 62 75 73 74 5f 66 74 if( robust_ft
fcbc0 72 75 6e 63 61 74 65 28 70 53 68 6d 4e 6f 64 65 runcate(pShmNode
fcbd0 2d 3e 68 2c 20 6e 42 79 74 65 29 20 29 7b 0a 20 ->h, nByte) ){.
fcbe0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e rc = un
fcbf0 69 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 ixLogError(SQLIT
fcc00 45 5f 49 4f 45 52 52 5f 53 48 4d 53 49 5a 45 2c E_IOERR_SHMSIZE,
fcc10 20 22 66 74 72 75 6e 63 61 74 65 22 2c 0a 20 20 "ftruncate",.
fcc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fcc30 20 20 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f pShmNo
fcc40 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a de->zFilename);.
fcc50 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73 goto s
fcc60 68 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 hmpage_out;.
fcc70 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
fcc80 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
fcc90 2f 2a 20 4d 61 70 20 74 68 65 20 72 65 71 75 65 /* Map the reque
fcca0 73 74 65 64 20 6d 65 6d 6f 72 79 20 72 65 67 69 sted memory regi
fccb0 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 70 72 6f on into this pro
fccc0 63 65 73 73 65 73 20 61 64 64 72 65 73 73 20 73 cesses address s
fccd0 70 61 63 65 2e 20 2a 2f 0a 20 20 20 20 61 70 4e pace. */. apN
fcce0 65 77 20 3d 20 28 63 68 61 72 20 2a 2a 29 73 71 ew = (char **)sq
fccf0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 0a 20 lite3_realloc(.
fcd00 20 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d pShmNode-
fcd10 3e 61 70 52 65 67 69 6f 6e 2c 20 28 69 52 65 67 >apRegion, (iReg
fcd20 69 6f 6e 2b 31 29 2a 73 69 7a 65 6f 66 28 63 68 ion+1)*sizeof(ch
fcd30 61 72 20 2a 29 0a 20 20 20 20 29 3b 0a 20 20 20 ar *). );.
fcd40 20 69 66 28 20 21 61 70 4e 65 77 20 29 7b 0a 20 if( !apNew ){.
fcd50 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
fcd60 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 _IOERR_NOMEM;.
fcd70 20 20 20 20 67 6f 74 6f 20 73 68 6d 70 61 67 65 goto shmpage
fcd80 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _out;. }.
fcd90 70 53 68 6d 4e 6f 64 65 2d 3e 61 70 52 65 67 69 pShmNode->apRegi
fcda0 6f 6e 20 3d 20 61 70 4e 65 77 3b 0a 20 20 20 20 on = apNew;.
fcdb0 77 68 69 6c 65 28 70 53 68 6d 4e 6f 64 65 2d 3e while(pShmNode->
fcdc0 6e 52 65 67 69 6f 6e 3c 3d 69 52 65 67 69 6f 6e nRegion<=iRegion
fcdd0 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 70 ){. void *p
fcde0 4d 65 6d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 Mem;. if( p
fcdf0 53 68 6d 4e 6f 64 65 2d 3e 68 3e 3d 30 20 29 7b ShmNode->h>=0 ){
fce00 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 20 3d 20 . pMem =
fce10 6d 6d 61 70 28 30 2c 20 73 7a 52 65 67 69 6f 6e mmap(0, szRegion
fce20 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 53 ,. pS
fce30 68 6d 4e 6f 64 65 2d 3e 69 73 52 65 61 64 6f 6e hmNode->isReadon
fce40 6c 79 20 3f 20 50 52 4f 54 5f 52 45 41 44 20 3a ly ? PROT_READ :
fce50 20 50 52 4f 54 5f 52 45 41 44 7c 50 52 4f 54 5f PROT_READ|PROT_
fce60 57 52 49 54 45 2c 20 0a 20 20 20 20 20 20 20 20 WRITE, .
fce70 20 20 20 20 4d 41 50 5f 53 48 41 52 45 44 2c 20 MAP_SHARED,
fce80 70 53 68 6d 4e 6f 64 65 2d 3e 68 2c 20 73 7a 52 pShmNode->h, szR
fce90 65 67 69 6f 6e 2a 28 69 36 34 29 70 53 68 6d 4e egion*(i64)pShmN
fcea0 6f 64 65 2d 3e 6e 52 65 67 69 6f 6e 0a 20 20 20 ode->nRegion.
fceb0 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 );.
fcec0 69 66 28 20 70 4d 65 6d 3d 3d 4d 41 50 5f 46 41 if( pMem==MAP_FA
fced0 49 4c 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20 ILED ){.
fcee0 20 20 72 63 20 3d 20 75 6e 69 78 4c 6f 67 45 72 rc = unixLogEr
fcef0 72 6f 72 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 ror(SQLITE_IOERR
fcf00 5f 53 48 4d 4d 41 50 2c 20 22 6d 6d 61 70 22 2c _SHMMAP, "mmap",
fcf10 20 70 53 68 6d 4e 6f 64 65 2d 3e 7a 46 69 6c 65 pShmNode->zFile
fcf20 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 name);.
fcf30 20 67 6f 74 6f 20 73 68 6d 70 61 67 65 5f 6f 75 goto shmpage_ou
fcf40 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 t;. }.
fcf50 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
fcf60 20 20 70 4d 65 6d 20 3d 20 73 71 6c 69 74 65 33 pMem = sqlite3
fcf70 5f 6d 61 6c 6c 6f 63 28 73 7a 52 65 67 69 6f 6e _malloc(szRegion
fcf80 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
fcf90 4d 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Mem==0 ){.
fcfa0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
fcfb0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 NOMEM;.
fcfc0 20 67 6f 74 6f 20 73 68 6d 70 61 67 65 5f 6f 75 goto shmpage_ou
fcfd0 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 t;. }.
fcfe0 20 20 20 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d memset(pMem
fcff0 2c 20 30 2c 20 73 7a 52 65 67 69 6f 6e 29 3b 0a , 0, szRegion);.
fd000 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 53 }. pS
fd010 68 6d 4e 6f 64 65 2d 3e 61 70 52 65 67 69 6f 6e hmNode->apRegion
fd020 5b 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67 69 [pShmNode->nRegi
fd030 6f 6e 5d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 on] = pMem;.
fd040 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67 pShmNode->nReg
fd050 69 6f 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d ion++;. }. }
fd060 0a 0a 73 68 6d 70 61 67 65 5f 6f 75 74 3a 0a 20 ..shmpage_out:.
fd070 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e if( pShmNode->n
fd080 52 65 67 69 6f 6e 3e 69 52 65 67 69 6f 6e 20 29 Region>iRegion )
fd090 7b 0a 20 20 20 20 2a 70 70 20 3d 20 70 53 68 6d {. *pp = pShm
fd0a0 4e 6f 64 65 2d 3e 61 70 52 65 67 69 6f 6e 5b 69 Node->apRegion[i
fd0b0 52 65 67 69 6f 6e 5d 3b 0a 20 20 7d 65 6c 73 65 Region];. }else
fd0c0 7b 0a 20 20 20 20 2a 70 70 20 3d 20 30 3b 0a 20 {. *pp = 0;.
fd0d0 20 7d 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 }. if( pShmNod
fd0e0 65 2d 3e 69 73 52 65 61 64 6f 6e 6c 79 20 26 26 e->isReadonly &&
fd0f0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
fd100 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 rc = SQLITE_REA
fd110 44 4f 4e 4c 59 3b 0a 20 20 73 71 6c 69 74 65 33 DONLY;. sqlite3
fd120 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 53 68 _mutex_leave(pSh
fd130 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0a 20 mNode->mutex);.
fd140 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
fd150 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
fd160 6c 6f 63 6b 20 73 74 61 74 65 20 66 6f 72 20 61 lock state for a
fd170 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 73 shared-memory s
fd180 65 67 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f egment..**.** No
fd190 74 65 20 74 68 61 74 20 74 68 65 20 72 65 6c 61 te that the rela
fd1a0 74 69 6f 6e 73 68 69 70 20 62 65 74 77 65 65 6e tionship between
fd1b0 20 53 48 41 52 45 64 20 61 6e 64 20 45 58 43 4c SHAREd and EXCL
fd1c0 55 53 49 56 45 20 6c 6f 63 6b 73 20 69 73 20 61 USIVE locks is a
fd1d0 20 6c 69 74 74 6c 65 0a 2a 2a 20 64 69 66 66 65 little.** diffe
fd1e0 72 65 6e 74 20 68 65 72 65 20 74 68 61 6e 20 69 rent here than i
fd1f0 6e 20 70 6f 73 69 78 2e 20 20 49 6e 20 78 53 68 n posix. In xSh
fd200 6d 4c 6f 63 6b 28 29 2c 20 6f 6e 65 20 63 61 6e mLock(), one can
fd210 20 67 6f 20 66 72 6f 6d 20 75 6e 6c 6f 63 6b 65 go from unlocke
fd220 64 0a 2a 2a 20 74 6f 20 73 68 61 72 65 64 20 61 d.** to shared a
fd230 6e 64 20 62 61 63 6b 20 6f 72 20 66 72 6f 6d 20 nd back or from
fd240 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 65 78 63 6c unlocked to excl
fd250 75 73 69 76 65 20 61 6e 64 20 62 61 63 6b 2e 20 usive and back.
fd260 20 42 75 74 20 6f 6e 65 20 6d 61 79 0a 2a 2a 20 But one may.**
fd270 6e 6f 74 20 67 6f 20 66 72 6f 6d 20 73 68 61 72 not go from shar
fd280 65 64 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20 ed to exclusive
fd290 6f 72 20 66 72 6f 6d 20 65 78 63 6c 75 73 69 76 or from exclusiv
fd2a0 65 20 74 6f 20 73 68 61 72 65 64 2e 0a 2a 2f 0a e to shared..*/.
fd2b0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 static int unixS
fd2c0 68 6d 4c 6f 63 6b 28 0a 20 20 73 71 6c 69 74 65 hmLock(. sqlite
fd2d0 33 5f 66 69 6c 65 20 2a 66 64 2c 20 20 20 20 20 3_file *fd,
fd2e0 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
fd2f0 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 file holding th
fd300 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 e shared memory
fd310 2a 2f 0a 20 20 69 6e 74 20 6f 66 73 74 2c 20 20 */. int ofst,
fd320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd330 2f 2a 20 46 69 72 73 74 20 6c 6f 63 6b 20 74 6f /* First lock to
fd340 20 61 63 71 75 69 72 65 20 6f 72 20 72 65 6c 65 acquire or rele
fd350 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 ase */. int n,
fd360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd370 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
fd380 20 6c 6f 63 6b 73 20 74 6f 20 61 63 71 75 69 72 locks to acquir
fd390 65 20 6f 72 20 72 65 6c 65 61 73 65 20 2a 2f 0a e or release */.
fd3a0 20 20 69 6e 74 20 66 6c 61 67 73 20 20 20 20 20 int flags
fd3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
fd3c0 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 What to do with
fd3d0 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 the lock */.){.
fd3e0 20 75 6e 69 78 46 69 6c 65 20 2a 70 44 62 46 64 unixFile *pDbFd
fd3f0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 66 64 = (unixFile*)fd
fd400 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 ; /* Connec
fd410 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 73 68 61 tion holding sha
fd420 72 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 red memory */.
fd430 75 6e 69 78 53 68 6d 20 2a 70 20 3d 20 70 44 62 unixShm *p = pDb
fd440 46 64 2d 3e 70 53 68 6d 3b 20 20 20 20 20 20 20 Fd->pShm;
fd450 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68 61 /* The sha
fd460 72 65 64 20 6d 65 6d 6f 72 79 20 62 65 69 6e 67 red memory being
fd470 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69 locked */. uni
fd480 78 53 68 6d 20 2a 70 58 3b 20 20 20 20 20 20 20 xShm *pX;
fd490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd4a0 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e /* For loopin
fd4b0 67 20 6f 76 65 72 20 61 6c 6c 20 73 69 62 6c 69 g over all sibli
fd4c0 6e 67 73 20 2a 2f 0a 20 20 75 6e 69 78 53 68 6d ngs */. unixShm
fd4d0 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f 64 65 20 3d Node *pShmNode =
fd4e0 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 3b 20 20 2f p->pShmNode; /
fd4f0 2a 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 * The underlying
fd500 20 66 69 6c 65 20 69 4e 6f 64 65 20 2a 2f 0a 20 file iNode */.
fd510 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
fd520 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
fd530 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 /* Result
fd540 20 63 6f 64 65 20 2a 2f 0a 20 20 75 31 36 20 6d code */. u16 m
fd550 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 ask;
fd560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd570 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6c 6f 63 6b /* Mask of lock
fd580 73 20 74 6f 20 74 61 6b 65 20 6f 72 20 72 65 6c s to take or rel
fd590 65 61 73 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 ease */.. asser
fd5a0 74 28 20 70 53 68 6d 4e 6f 64 65 3d 3d 70 44 62 t( pShmNode==pDb
fd5b0 46 64 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 68 6d Fd->pInode->pShm
fd5c0 4e 6f 64 65 20 29 3b 0a 20 20 61 73 73 65 72 74 Node );. assert
fd5d0 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 49 6e 6f ( pShmNode->pIno
fd5e0 64 65 3d 3d 70 44 62 46 64 2d 3e 70 49 6e 6f 64 de==pDbFd->pInod
fd5f0 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f e );. assert( o
fd600 66 73 74 3e 3d 30 20 26 26 20 6f 66 73 74 2b 6e fst>=0 && ofst+n
fd610 3c 3d 53 51 4c 49 54 45 5f 53 48 4d 5f 4e 4c 4f <=SQLITE_SHM_NLO
fd620 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
fd630 6e 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 n>=1 );. assert
fd640 28 20 66 6c 61 67 73 3d 3d 28 53 51 4c 49 54 45 ( flags==(SQLITE
fd650 5f 53 48 4d 5f 4c 4f 43 4b 20 7c 20 53 51 4c 49 _SHM_LOCK | SQLI
fd660 54 45 5f 53 48 4d 5f 53 48 41 52 45 44 29 0a 20 TE_SHM_SHARED).
fd670 20 20 20 20 20 20 7c 7c 20 66 6c 61 67 73 3d 3d || flags==
fd680 28 53 51 4c 49 54 45 5f 53 48 4d 5f 4c 4f 43 4b (SQLITE_SHM_LOCK
fd690 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f 45 58 | SQLITE_SHM_EX
fd6a0 43 4c 55 53 49 56 45 29 0a 20 20 20 20 20 20 20 CLUSIVE).
fd6b0 7c 7c 20 66 6c 61 67 73 3d 3d 28 53 51 4c 49 54 || flags==(SQLIT
fd6c0 45 5f 53 48 4d 5f 55 4e 4c 4f 43 4b 20 7c 20 53 E_SHM_UNLOCK | S
fd6d0 51 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52 45 44 QLITE_SHM_SHARED
fd6e0 29 0a 20 20 20 20 20 20 20 7c 7c 20 66 6c 61 67 ). || flag
fd6f0 73 3d 3d 28 53 51 4c 49 54 45 5f 53 48 4d 5f 55 s==(SQLITE_SHM_U
fd700 4e 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53 NLOCK | SQLITE_S
fd710 48 4d 5f 45 58 43 4c 55 53 49 56 45 29 20 29 3b HM_EXCLUSIVE) );
fd720 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 31 20 . assert( n==1
fd730 7c 7c 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 || (flags & SQLI
fd740 54 45 5f 53 48 4d 5f 45 58 43 4c 55 53 49 56 45 TE_SHM_EXCLUSIVE
fd750 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 )!=0 );. assert
fd760 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 68 3e 3d 30 ( pShmNode->h>=0
fd770 20 7c 7c 20 70 44 62 46 64 2d 3e 70 49 6e 6f 64 || pDbFd->pInod
fd780 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f 63 6b 3d e->bProcessLock=
fd790 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =1 );. assert(
fd7a0 70 53 68 6d 4e 6f 64 65 2d 3e 68 3c 30 20 7c 7c pShmNode->h<0 ||
fd7b0 20 70 44 62 46 64 2d 3e 70 49 6e 6f 64 65 2d 3e pDbFd->pInode->
fd7c0 62 50 72 6f 63 65 73 73 4c 6f 63 6b 3d 3d 30 20 bProcessLock==0
fd7d0 29 3b 0a 0a 20 20 6d 61 73 6b 20 3d 20 28 31 3c );.. mask = (1<
fd7e0 3c 28 6f 66 73 74 2b 6e 29 29 20 2d 20 28 31 3c <(ofst+n)) - (1<
fd7f0 3c 6f 66 73 74 29 3b 0a 20 20 61 73 73 65 72 74 <ofst);. assert
fd800 28 20 6e 3e 31 20 7c 7c 20 6d 61 73 6b 3d 3d 28 ( n>1 || mask==(
fd810 31 3c 3c 6f 66 73 74 29 20 29 3b 0a 20 20 73 71 1<<ofst) );. sq
fd820 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
fd830 72 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 r(pShmNode->mute
fd840 78 29 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 x);. if( flags
fd850 26 20 53 51 4c 49 54 45 5f 53 48 4d 5f 55 4e 4c & SQLITE_SHM_UNL
fd860 4f 43 4b 20 29 7b 0a 20 20 20 20 75 31 36 20 61 OCK ){. u16 a
fd870 6c 6c 4d 61 73 6b 20 3d 20 30 3b 20 2f 2a 20 4d llMask = 0; /* M
fd880 61 73 6b 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c ask of locks hel
fd890 64 20 62 79 20 73 69 62 6c 69 6e 67 73 20 2a 2f d by siblings */
fd8a0 0a 0a 20 20 20 20 2f 2a 20 53 65 65 20 69 66 20 .. /* See if
fd8b0 61 6e 79 20 73 69 62 6c 69 6e 67 73 20 68 6f 6c any siblings hol
fd8c0 64 20 74 68 69 73 20 73 61 6d 65 20 6c 6f 63 6b d this same lock
fd8d0 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 58 3d 70 */. for(pX=p
fd8e0 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74 3b ShmNode->pFirst;
fd8f0 20 70 58 3b 20 70 58 3d 70 58 2d 3e 70 4e 65 78 pX; pX=pX->pNex
fd900 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 58 t){. if( pX
fd910 3d 3d 70 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a ==p ) continue;.
fd920 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 assert( (p
fd930 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 20 28 70 X->exclMask & (p
fd940 2d 3e 65 78 63 6c 4d 61 73 6b 7c 70 2d 3e 73 68 ->exclMask|p->sh
fd950 61 72 65 64 4d 61 73 6b 29 29 3d 3d 30 20 29 3b aredMask))==0 );
fd960 0a 20 20 20 20 20 20 61 6c 6c 4d 61 73 6b 20 7c . allMask |
fd970 3d 20 70 58 2d 3e 73 68 61 72 65 64 4d 61 73 6b = pX->sharedMask
fd980 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
fd990 55 6e 6c 6f 63 6b 20 74 68 65 20 73 79 73 74 65 Unlock the syste
fd9a0 6d 2d 6c 65 76 65 6c 20 6c 6f 63 6b 73 20 2a 2f m-level locks */
fd9b0 0a 20 20 20 20 69 66 28 20 28 6d 61 73 6b 20 26 . if( (mask &
fd9c0 20 61 6c 6c 4d 61 73 6b 29 3d 3d 30 20 29 7b 0a allMask)==0 ){.
fd9d0 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 78 53 rc = unixS
fd9e0 68 6d 53 79 73 74 65 6d 4c 6f 63 6b 28 70 53 68 hmSystemLock(pSh
fd9f0 6d 4e 6f 64 65 2c 20 46 5f 55 4e 4c 43 4b 2c 20 mNode, F_UNLCK,
fda00 6f 66 73 74 2b 55 4e 49 58 5f 53 48 4d 5f 42 41 ofst+UNIX_SHM_BA
fda10 53 45 2c 20 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 SE, n);. }els
fda20 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 e{. rc = SQ
fda30 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a LITE_OK;. }..
fda40 20 20 20 20 2f 2a 20 55 6e 64 6f 20 74 68 65 20 /* Undo the
fda50 6c 6f 63 61 6c 20 6c 6f 63 6b 73 20 2a 2f 0a 20 local locks */.
fda60 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
fda70 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d E_OK ){. p-
fda80 3e 65 78 63 6c 4d 61 73 6b 20 26 3d 20 7e 6d 61 >exclMask &= ~ma
fda90 73 6b 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 sk;. p->sha
fdaa0 72 65 64 4d 61 73 6b 20 26 3d 20 7e 6d 61 73 6b redMask &= ~mask
fdab0 3b 0a 20 20 20 20 7d 20 0a 20 20 7d 65 6c 73 65 ;. } . }else
fdac0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
fdad0 49 54 45 5f 53 48 4d 5f 53 48 41 52 45 44 20 29 ITE_SHM_SHARED )
fdae0 7b 0a 20 20 20 20 75 31 36 20 61 6c 6c 53 68 61 {. u16 allSha
fdaf0 72 65 64 20 3d 20 30 3b 20 20 2f 2a 20 55 6e 69 red = 0; /* Uni
fdb00 6f 6e 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 on of locks held
fdb10 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 by connections
fdb20 6f 74 68 65 72 20 74 68 61 6e 20 22 70 22 20 2a other than "p" *
fdb30 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 6f /.. /* Find o
fdb40 75 74 20 77 68 69 63 68 20 73 68 61 72 65 64 20 ut which shared
fdb50 6c 6f 63 6b 73 20 61 72 65 20 61 6c 72 65 61 64 locks are alread
fdb60 79 20 68 65 6c 64 20 62 79 20 73 69 62 6c 69 6e y held by siblin
fdb70 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 20 g connections..
fdb80 20 20 20 2a 2a 20 49 66 20 61 6e 79 20 73 69 62 ** If any sib
fdb90 6c 69 6e 67 20 61 6c 72 65 61 64 79 20 68 6f 6c ling already hol
fdba0 64 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 ds an exclusive
fdbb0 6c 6f 63 6b 2c 20 67 6f 20 61 68 65 61 64 20 61 lock, go ahead a
fdbc0 6e 64 20 72 65 74 75 72 6e 0a 20 20 20 20 2a 2a nd return. **
fdbd0 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 20 20 SQLITE_BUSY..
fdbe0 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 58 3d */. for(pX=
fdbf0 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74 pShmNode->pFirst
fdc00 3b 20 70 58 3b 20 70 58 3d 70 58 2d 3e 70 4e 65 ; pX; pX=pX->pNe
fdc10 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 xt){. if( (
fdc20 70 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 20 6d pX->exclMask & m
fdc30 61 73 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ask)!=0 ){.
fdc40 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
fdc50 55 53 59 3b 0a 20 20 20 20 20 20 20 20 62 72 65 USY;. bre
fdc60 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
fdc70 20 20 61 6c 6c 53 68 61 72 65 64 20 7c 3d 20 70 allShared |= p
fdc80 58 2d 3e 73 68 61 72 65 64 4d 61 73 6b 3b 0a 20 X->sharedMask;.
fdc90 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 74 }.. /* Get
fdca0 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 61 74 shared locks at
fdcb0 20 74 68 65 20 73 79 73 74 65 6d 20 6c 65 76 65 the system leve
fdcc0 6c 2c 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 l, if necessary
fdcd0 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 */. if( rc==S
fdce0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
fdcf0 20 20 69 66 28 20 28 61 6c 6c 53 68 61 72 65 64 if( (allShared
fdd00 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a 20 & mask)==0 ){.
fdd10 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 78 rc = unix
fdd20 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b 28 70 53 ShmSystemLock(pS
fdd30 68 6d 4e 6f 64 65 2c 20 46 5f 52 44 4c 43 4b 2c hmNode, F_RDLCK,
fdd40 20 6f 66 73 74 2b 55 4e 49 58 5f 53 48 4d 5f 42 ofst+UNIX_SHM_B
fdd50 41 53 45 2c 20 6e 29 3b 0a 20 20 20 20 20 20 7d ASE, n);. }
fdd60 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 else{. rc
fdd70 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
fdd80 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
fdd90 20 2f 2a 20 47 65 74 20 74 68 65 20 6c 6f 63 61 /* Get the loca
fdda0 6c 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 2a l shared locks *
fddb0 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 /. if( rc==SQ
fddc0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
fddd0 20 70 2d 3e 73 68 61 72 65 64 4d 61 73 6b 20 7c p->sharedMask |
fdde0 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 7d 0a 20 20 = mask;. }.
fddf0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4d 61 }else{. /* Ma
fde00 6b 65 20 73 75 72 65 20 6e 6f 20 73 69 62 6c 69 ke sure no sibli
fde10 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 68 ng connections h
fde20 6f 6c 64 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 old locks that w
fde30 69 6c 6c 20 62 6c 6f 63 6b 20 74 68 69 73 0a 20 ill block this.
fde40 20 20 20 2a 2a 20 6c 6f 63 6b 2e 20 20 49 66 20 ** lock. If
fde50 61 6e 79 20 64 6f 2c 20 72 65 74 75 72 6e 20 53 any do, return S
fde60 51 4c 49 54 45 5f 42 55 53 59 20 72 69 67 68 74 QLITE_BUSY right
fde70 20 61 77 61 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 away.. */.
fde80 20 20 66 6f 72 28 70 58 3d 70 53 68 6d 4e 6f 64 for(pX=pShmNod
fde90 65 2d 3e 70 46 69 72 73 74 3b 20 70 58 3b 20 70 e->pFirst; pX; p
fdea0 58 3d 70 58 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 X=pX->pNext){.
fdeb0 20 20 20 20 69 66 28 20 28 70 58 2d 3e 65 78 63 if( (pX->exc
fdec0 6c 4d 61 73 6b 20 26 20 6d 61 73 6b 29 21 3d 30 lMask & mask)!=0
fded0 20 7c 7c 20 28 70 58 2d 3e 73 68 61 72 65 64 4d || (pX->sharedM
fdee0 61 73 6b 20 26 20 6d 61 73 6b 29 21 3d 30 20 29 ask & mask)!=0 )
fdef0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 {. rc = S
fdf00 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
fdf10 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
fdf20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 }. }. .
fdf30 2f 2a 20 47 65 74 20 74 68 65 20 65 78 63 6c 75 /* Get the exclu
fdf40 73 69 76 65 20 6c 6f 63 6b 73 20 61 74 20 74 68 sive locks at th
fdf50 65 20 73 79 73 74 65 6d 20 6c 65 76 65 6c 2e 20 e system level.
fdf60 20 54 68 65 6e 20 69 66 20 73 75 63 63 65 73 73 Then if success
fdf70 66 75 6c 0a 20 20 20 20 2a 2a 20 61 6c 73 6f 20 ful. ** also
fdf80 6d 61 72 6b 20 74 68 65 20 6c 6f 63 61 6c 20 63 mark the local c
fdf90 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 62 65 69 onnection as bei
fdfa0 6e 67 20 6c 6f 63 6b 65 64 2e 0a 20 20 20 20 2a ng locked.. *
fdfb0 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 /. if( rc==SQ
fdfc0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
fdfd0 20 72 63 20 3d 20 75 6e 69 78 53 68 6d 53 79 73 rc = unixShmSys
fdfe0 74 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 temLock(pShmNode
fdff0 2c 20 46 5f 57 52 4c 43 4b 2c 20 6f 66 73 74 2b , F_WRLCK, ofst+
fe000 55 4e 49 58 5f 53 48 4d 5f 42 41 53 45 2c 20 6e UNIX_SHM_BASE, n
fe010 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
fe020 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
fe030 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 assert( (p
fe040 2d 3e 73 68 61 72 65 64 4d 61 73 6b 20 26 20 6d ->sharedMask & m
fe050 61 73 6b 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ask)==0 );.
fe060 20 20 20 70 2d 3e 65 78 63 6c 4d 61 73 6b 20 7c p->exclMask |
fe070 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 7d 0a = mask;. }.
fe080 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 }. }. sqli
fe090 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
fe0a0 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 pShmNode->mutex)
fe0b0 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 53 48 ;. OSTRACE(("SH
fe0c0 4d 2d 4c 4f 43 4b 20 73 68 6d 69 64 2d 25 64 2c M-LOCK shmid-%d,
fe0d0 20 70 69 64 2d 25 64 20 67 6f 74 20 25 30 33 78 pid-%d got %03x
fe0e0 2c 25 30 33 78 5c 6e 22 2c 0a 20 20 20 20 20 20 ,%03x\n",.
fe0f0 20 20 20 20 20 70 2d 3e 69 64 2c 20 67 65 74 70 p->id, getp
fe100 69 64 28 29 2c 20 70 2d 3e 73 68 61 72 65 64 4d id(), p->sharedM
fe110 61 73 6b 2c 20 70 2d 3e 65 78 63 6c 4d 61 73 6b ask, p->exclMask
fe120 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ));. return rc;
fe130 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
fe140 65 6e 74 20 61 20 6d 65 6d 6f 72 79 20 62 61 72 ent a memory bar
fe150 72 69 65 72 20 6f 72 20 6d 65 6d 6f 72 79 20 66 rier or memory f
fe160 65 6e 63 65 20 6f 6e 20 73 68 61 72 65 64 20 6d ence on shared m
fe170 65 6d 6f 72 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 emory. .**.** A
fe180 6c 6c 20 6c 6f 61 64 73 20 61 6e 64 20 73 74 6f ll loads and sto
fe190 72 65 73 20 62 65 67 75 6e 20 62 65 66 6f 72 65 res begun before
fe1a0 20 74 68 65 20 62 61 72 72 69 65 72 20 6d 75 73 the barrier mus
fe1b0 74 20 63 6f 6d 70 6c 65 74 65 20 62 65 66 6f 72 t complete befor
fe1c0 65 0a 2a 2a 20 61 6e 79 20 6c 6f 61 64 20 6f 72 e.** any load or
fe1d0 20 73 74 6f 72 65 20 62 65 67 75 6e 20 61 66 74 store begun aft
fe1e0 65 72 20 74 68 65 20 62 61 72 72 69 65 72 2e 0a er the barrier..
fe1f0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 */.static void u
fe200 6e 69 78 53 68 6d 42 61 72 72 69 65 72 28 0a 20 nixShmBarrier(.
fe210 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 sqlite3_file *f
fe220 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
fe230 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c /* Database fil
fe240 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 73 68 e holding the sh
fe250 61 72 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 29 ared memory */.)
fe260 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
fe270 45 54 45 52 28 66 64 29 3b 0a 20 20 75 6e 69 78 ETER(fd);. unix
fe280 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
fe290 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
fe2a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 ;.}../*.** Close
fe2b0 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f a connection to
fe2c0 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 2e 20 shared-memory.
fe2d0 20 44 65 6c 65 74 65 20 74 68 65 20 75 6e 64 65 Delete the unde
fe2e0 72 6c 79 69 6e 67 20 0a 2a 2a 20 73 74 6f 72 61 rlying .** stora
fe2f0 67 65 20 69 66 20 64 65 6c 65 74 65 46 6c 61 67 ge if deleteFlag
fe300 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 is true..**.**
fe310 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 If there is no s
fe320 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 61 73 73 hared memory ass
fe330 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
fe340 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 65 6e connection then
fe350 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
fe360 20 69 73 20 61 20 68 61 72 6d 6c 65 73 73 20 6e is a harmless n
fe370 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 o-op..*/.static
fe380 69 6e 74 20 75 6e 69 78 53 68 6d 55 6e 6d 61 70 int unixShmUnmap
fe390 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
fe3a0 20 2a 66 64 2c 20 20 20 20 20 20 20 20 20 20 20 *fd,
fe3b0 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 64 65 72 /* The under
fe3c0 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 66 lying database f
fe3d0 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 64 65 6c ile */. int del
fe3e0 65 74 65 46 6c 61 67 20 20 20 20 20 20 20 20 20 eteFlag
fe3f0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 /* Dele
fe400 74 65 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 te shared-memory
fe410 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 if true */.){.
fe420 20 75 6e 69 78 53 68 6d 20 2a 70 3b 20 20 20 20 unixShm *p;
fe430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe440 20 2f 2a 20 54 68 65 20 63 6f 6e 6e 65 63 74 69 /* The connecti
fe450 6f 6e 20 74 6f 20 62 65 20 63 6c 6f 73 65 64 20 on to be closed
fe460 2a 2f 0a 20 20 75 6e 69 78 53 68 6d 4e 6f 64 65 */. unixShmNode
fe470 20 2a 70 53 68 6d 4e 6f 64 65 3b 20 20 20 20 20 *pShmNode;
fe480 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 64 65 /* The unde
fe490 72 6c 79 69 6e 67 20 73 68 61 72 65 64 2d 6d 65 rlying shared-me
fe4a0 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 75 mory file */. u
fe4b0 6e 69 78 53 68 6d 20 2a 2a 70 70 3b 20 20 20 20 nixShm **pp;
fe4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
fe4d0 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 * For looping ov
fe4e0 65 72 20 73 69 62 6c 69 6e 67 20 63 6f 6e 6e 65 er sibling conne
fe4f0 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 6e 69 78 ctions */. unix
fe500 46 69 6c 65 20 2a 70 44 62 46 64 3b 20 20 20 20 File *pDbFd;
fe510 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
fe520 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 he underlying da
fe530 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a tabase file */..
fe540 20 20 70 44 62 46 64 20 3d 20 28 75 6e 69 78 46 pDbFd = (unixF
fe550 69 6c 65 2a 29 66 64 3b 0a 20 20 70 20 3d 20 70 ile*)fd;. p = p
fe560 44 62 46 64 2d 3e 70 53 68 6d 3b 0a 20 20 69 66 DbFd->pShm;. if
fe570 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
fe580 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 53 68 SQLITE_OK;. pSh
fe590 6d 4e 6f 64 65 20 3d 20 70 2d 3e 70 53 68 6d 4e mNode = p->pShmN
fe5a0 6f 64 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ode;.. assert(
fe5b0 70 53 68 6d 4e 6f 64 65 3d 3d 70 44 62 46 64 2d pShmNode==pDbFd-
fe5c0 3e 70 49 6e 6f 64 65 2d 3e 70 53 68 6d 4e 6f 64 >pInode->pShmNod
fe5d0 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 e );. assert( p
fe5e0 53 68 6d 4e 6f 64 65 2d 3e 70 49 6e 6f 64 65 3d ShmNode->pInode=
fe5f0 3d 70 44 62 46 64 2d 3e 70 49 6e 6f 64 65 20 29 =pDbFd->pInode )
fe600 3b 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 63 ;.. /* Remove c
fe610 6f 6e 6e 65 63 74 69 6f 6e 20 70 20 66 72 6f 6d onnection p from
fe620 20 74 68 65 20 73 65 74 20 6f 66 20 63 6f 6e 6e the set of conn
fe630 65 63 74 69 6f 6e 73 20 61 73 73 6f 63 69 61 74 ections associat
fe640 65 64 0a 20 20 2a 2a 20 77 69 74 68 20 70 53 68 ed. ** with pSh
fe650 6d 4e 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 mNode */. sqlit
fe660 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 e3_mutex_enter(p
fe670 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b ShmNode->mutex);
fe680 0a 20 20 66 6f 72 28 70 70 3d 26 70 53 68 6d 4e . for(pp=&pShmN
fe690 6f 64 65 2d 3e 70 46 69 72 73 74 3b 20 28 2a 70 ode->pFirst; (*p
fe6a0 70 29 21 3d 70 3b 20 70 70 20 3d 20 26 28 2a 70 p)!=p; pp = &(*p
fe6b0 70 29 2d 3e 70 4e 65 78 74 29 7b 7d 0a 20 20 2a p)->pNext){}. *
fe6c0 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 0a pp = p->pNext;..
fe6d0 20 20 2f 2a 20 46 72 65 65 20 74 68 65 20 63 6f /* Free the co
fe6e0 6e 6e 65 63 74 69 6f 6e 20 70 20 2a 2f 0a 20 20 nnection p */.
fe6f0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
fe700 0a 20 20 70 44 62 46 64 2d 3e 70 53 68 6d 20 3d . pDbFd->pShm =
fe710 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 0;. sqlite3_mu
fe720 74 65 78 5f 6c 65 61 76 65 28 70 53 68 6d 4e 6f tex_leave(pShmNo
fe730 64 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 2f de->mutex);.. /
fe740 2a 20 49 66 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e * If pShmNode->n
fe750 52 65 66 20 68 61 73 20 72 65 61 63 68 65 64 20 Ref has reached
fe760 30 2c 20 74 68 65 6e 20 63 6c 6f 73 65 20 74 68 0, then close th
fe770 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 20 20 2a e underlying. *
fe780 2a 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 * shared-memory
fe790 66 69 6c 65 2c 20 74 6f 6f 20 2a 2f 0a 20 20 75 file, too */. u
fe7a0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
fe7b0 0a 20 20 61 73 73 65 72 74 28 20 70 53 68 6d 4e . assert( pShmN
fe7c0 6f 64 65 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ode->nRef>0 );.
fe7d0 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 2d pShmNode->nRef-
fe7e0 2d 3b 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 -;. if( pShmNod
fe7f0 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 e->nRef==0 ){.
fe800 20 20 69 66 28 20 64 65 6c 65 74 65 46 6c 61 67 if( deleteFlag
fe810 20 26 26 20 70 53 68 6d 4e 6f 64 65 2d 3e 68 3e && pShmNode->h>
fe820 3d 30 20 29 20 6f 73 55 6e 6c 69 6e 6b 28 70 53 =0 ) osUnlink(pS
fe830 68 6d 4e 6f 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d hmNode->zFilenam
fe840 65 29 3b 0a 20 20 20 20 75 6e 69 78 53 68 6d 50 e);. unixShmP
fe850 75 72 67 65 28 70 44 62 46 64 29 3b 0a 20 20 7d urge(pDbFd);. }
fe860 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 . unixLeaveMute
fe870 78 28 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 x();.. return S
fe880 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 65 QLITE_OK;.}...#e
fe890 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 75 6e 69 lse.# define uni
fe8a0 78 53 68 6d 4d 61 70 20 20 20 20 20 30 0a 23 20 xShmMap 0.#
fe8b0 64 65 66 69 6e 65 20 75 6e 69 78 53 68 6d 4c 6f define unixShmLo
fe8c0 63 6b 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 ck 0.# define
fe8d0 20 75 6e 69 78 53 68 6d 42 61 72 72 69 65 72 20 unixShmBarrier
fe8e0 30 0a 23 20 64 65 66 69 6e 65 20 75 6e 69 78 53 0.# define unixS
fe8f0 68 6d 55 6e 6d 61 70 20 20 20 30 0a 23 65 6e 64 hmUnmap 0.#end
fe900 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 if /* #ifndef SQ
fe910 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 20 2a 2f LITE_OMIT_WAL */
fe920 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 ../*.** Here end
fe930 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 s the implementa
fe940 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 71 6c 69 tion of all sqli
fe950 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 te3_file methods
fe960 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
fe970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
fe980 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 4d 65 74 sqlite3_file Met
fe990 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a hods ***********
fe9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fe9b0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
fe9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fe9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fe9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fe9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fea00 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ***/../*.** This
fea10 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 division contai
fea20 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 6f ns definitions o
fea30 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 f sqlite3_io_met
fea40 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 74 68 61 hods objects tha
fea50 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 76 t.** implement v
fea60 61 72 69 6f 75 73 20 66 69 6c 65 20 6c 6f 63 6b arious file lock
fea70 69 6e 67 20 73 74 72 61 74 65 67 69 65 73 2e 20 ing strategies.
fea80 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e It also contain
fea90 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a s definitions.**
feaa0 20 6f 66 20 22 66 69 6e 64 65 72 22 20 66 75 6e of "finder" fun
feab0 63 74 69 6f 6e 73 2e 20 20 41 20 66 69 6e 64 65 ctions. A finde
feac0 72 2d 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 r-function is us
fead0 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 ed to locate the
feae0 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 appropriate.**
feaf0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
feb00 64 73 20 6f 62 6a 65 63 74 20 66 6f 72 20 61 20 ds object for a
feb10 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 particular datab
feb20 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 ase file. The p
feb30 41 70 70 44 61 74 61 0a 2a 2a 20 66 69 65 6c 64 AppData.** field
feb40 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
feb50 76 66 73 20 56 46 53 20 6f 62 6a 65 63 74 73 20 vfs VFS objects
feb60 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 are initialized
feb70 74 6f 20 62 65 20 70 6f 69 6e 74 65 72 73 20 74 to be pointers t
feb80 6f 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 74 o.** the correct
feb90 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e finder-function
feba0 20 66 6f 72 20 74 68 61 74 20 56 46 53 2e 0a 2a for that VFS..*
febb0 2a 0a 2a 2a 20 4d 6f 73 74 20 66 69 6e 64 65 72 *.** Most finder
febc0 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 functions retur
febd0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 n a pointer to a
febe0 20 66 69 78 65 64 20 73 71 6c 69 74 65 33 5f 69 fixed sqlite3_i
febf0 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a o_methods.** obj
fec00 65 63 74 2e 20 20 54 68 65 20 6f 6e 6c 79 20 69 ect. The only i
fec10 6e 74 65 72 65 73 74 69 6e 67 20 66 69 6e 64 65 nteresting finde
fec20 72 2d 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 75 r-function is au
fec30 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 tolockIoFinder,
fec40 77 68 69 63 68 0a 2a 2a 20 6c 6f 6f 6b 73 20 61 which.** looks a
fec50 74 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d t the filesystem
fec60 20 74 79 70 65 20 61 6e 64 20 74 72 69 65 73 20 type and tries
fec70 74 6f 20 67 75 65 73 73 20 74 68 65 20 62 65 73 to guess the bes
fec80 74 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 t locking.** str
fec90 61 74 65 67 79 20 66 72 6f 6d 20 74 68 61 74 2e ategy from that.
feca0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 69 6e 64 65 .**.** For finde
fecb0 72 2d 66 75 6e 74 69 6f 6e 20 46 2c 20 74 77 6f r-funtion F, two
fecc0 20 6f 62 6a 65 63 74 73 20 61 72 65 20 63 72 65 objects are cre
fecd0 61 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 ated:.**.** (
fece0 31 29 20 54 68 65 20 72 65 61 6c 20 66 69 6e 64 1) The real find
fecf0 65 72 2d 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 er-function name
fed00 64 20 22 46 49 6d 70 74 28 29 22 2e 0a 2a 2a 0a d "FImpt()"..**.
fed10 2a 2a 20 20 20 20 28 32 29 20 41 20 63 6f 6e 73 ** (2) A cons
fed20 74 61 6e 74 20 70 6f 69 6e 74 65 72 20 74 6f 20 tant pointer to
fed30 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6e 61 this function na
fed40 6d 65 64 20 6a 75 73 74 20 22 46 22 2e 0a 2a 2a med just "F"..**
fed50 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 .**.** A pointer
fed60 20 74 6f 20 74 68 65 20 46 20 70 6f 69 6e 74 65 to the F pointe
fed70 72 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 r is used as the
fed80 20 70 41 70 70 44 61 74 61 20 76 61 6c 75 65 20 pAppData value
fed90 66 6f 72 20 56 46 53 0a 2a 2a 20 6f 62 6a 65 63 for VFS.** objec
feda0 74 73 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 ts. We have to
fedb0 64 6f 20 74 68 69 73 20 69 6e 73 74 65 61 64 20 do this instead
fedc0 6f 66 20 6c 65 74 74 69 6e 67 20 70 41 70 70 44 of letting pAppD
fedd0 61 74 61 20 70 6f 69 6e 74 0a 2a 2a 20 64 69 72 ata point.** dir
fede0 65 63 74 6c 79 20 61 74 20 74 68 65 20 66 69 6e ectly at the fin
fedf0 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 73 69 6e der-function sin
fee00 63 65 20 43 39 30 20 72 75 6c 65 73 20 70 72 65 ce C90 rules pre
fee10 76 65 6e 74 20 61 20 76 6f 69 64 2a 0a 2a 2a 20 vent a void*.**
fee20 66 72 6f 6d 20 62 65 20 63 61 73 74 20 69 6e 74 from be cast int
fee30 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 o a function poi
fee40 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 45 nter..**.**.** E
fee50 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 ach instance of
fee60 74 68 69 73 20 6d 61 63 72 6f 20 67 65 6e 65 72 this macro gener
fee70 61 74 65 73 20 74 77 6f 20 6f 62 6a 65 63 74 73 ates two objects
fee80 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 20 63 :.**.** * A c
fee90 6f 6e 73 74 61 6e 74 20 73 71 6c 69 74 65 33 5f onstant sqlite3_
feea0 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 io_methods objec
feeb0 74 20 63 61 6c 6c 20 4d 45 54 48 4f 44 20 74 68 t call METHOD th
feec0 61 74 20 68 61 73 20 6c 6f 63 6b 69 6e 67 0a 2a at has locking.*
feed0 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 43 * methods C
feee0 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f LOSE, LOCK, UNLO
feef0 43 4b 2c 20 43 4b 52 45 53 4c 4f 43 4b 2e 0a 2a CK, CKRESLOCK..*
fef00 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e 20 49 2f 4f *.** * An I/O
fef10 20 6d 65 74 68 6f 64 20 66 69 6e 64 65 72 20 66 method finder f
fef20 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65 64 20 46 unction called F
fef30 49 4e 44 45 52 20 74 68 61 74 20 72 65 74 75 72 INDER that retur
fef40 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 ns a pointer.**
fef50 20 20 20 20 20 74 6f 20 74 68 65 20 4d 45 54 48 to the METH
fef60 4f 44 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 OD object in the
fef70 20 70 72 65 76 69 6f 75 73 20 62 75 6c 6c 65 74 previous bullet
fef80 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 49 4f 4d ..*/.#define IOM
fef90 45 54 48 4f 44 53 28 46 49 4e 44 45 52 2c 20 4d ETHODS(FINDER, M
fefa0 45 54 48 4f 44 2c 20 56 45 52 53 49 4f 4e 2c 20 ETHOD, VERSION,
fefb0 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c CLOSE, LOCK, UNL
fefc0 4f 43 4b 2c 20 43 4b 4c 4f 43 4b 29 20 20 20 20 OCK, CKLOCK)
fefd0 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 \.static const
fefe0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
feff0 6f 64 73 20 4d 45 54 48 4f 44 20 3d 20 7b 20 20 ods METHOD = {
ff000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff020 20 5c 0a 20 20 20 56 45 52 53 49 4f 4e 2c 20 20 \. VERSION,
ff030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff040 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f /* iVersion */
ff050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff070 5c 0a 20 20 20 43 4c 4f 53 45 2c 20 20 20 20 20 \. CLOSE,
ff080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff090 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 20 20 20 /* xClose */
ff0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ff0c0 0a 20 20 20 75 6e 69 78 52 65 61 64 2c 20 20 20 . unixRead,
ff0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff0e0 2f 2a 20 78 52 65 61 64 20 2a 2f 20 20 20 20 20 /* xRead */
ff0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ff110 20 20 20 75 6e 69 78 57 72 69 74 65 2c 20 20 20 unixWrite,
ff120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ff130 2a 20 78 57 72 69 74 65 20 2a 2f 20 20 20 20 20 * xWrite */
ff140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff150 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ff160 20 20 75 6e 69 78 54 72 75 6e 63 61 74 65 2c 20 unixTruncate,
ff170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ff180 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 20 20 20 xTruncate */
ff190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff1a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ff1b0 20 75 6e 69 78 53 79 6e 63 2c 20 20 20 20 20 20 unixSync,
ff1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ff1d0 78 53 79 6e 63 20 2a 2f 20 20 20 20 20 20 20 20 xSync */
ff1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff1f0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ff200 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c 20 20 20 unixFileSize,
ff210 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
ff220 46 69 6c 65 53 69 7a 65 20 2a 2f 20 20 20 20 20 FileSize */
ff230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff240 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 4c \. L
ff250 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20 OCK,
ff260 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c /* xL
ff270 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ock */
ff280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff290 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 55 4e \. UN
ff2a0 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 LOCK,
ff2b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e /* xUn
ff2c0 6c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 lock */
ff2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff2e0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 43 4b 4c \. CKL
ff2f0 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20 OCK,
ff300 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 68 65 /* xChe
ff310 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 2a ckReservedLock *
ff320 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
ff330 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 \. unix
ff340 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 FileControl,
ff350 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 /* xFile
ff360 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20 20 20 20 20 Control */
ff370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff380 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 \. unixS
ff390 65 63 74 6f 72 53 69 7a 65 2c 20 20 20 20 20 20 ectorSize,
ff3a0 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f /* xSecto
ff3b0 72 53 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 20 rSize */
ff3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff3d0 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 44 65 \. unixDe
ff3e0 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
ff3f0 69 63 73 2c 20 20 2f 2a 20 78 44 65 76 69 63 65 ics, /* xDevice
ff400 43 61 70 61 62 69 6c 69 74 69 65 73 20 2a 2f 20 Capabilities */
ff410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff420 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 68 6d \. unixShm
ff430 4d 61 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 Map,
ff440 20 20 20 20 20 2f 2a 20 78 53 68 6d 4d 61 70 20 /* xShmMap
ff450 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
ff460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff470 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 68 6d 4c \. unixShmL
ff480 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 ock,
ff490 20 20 20 20 2f 2a 20 78 53 68 6d 4c 6f 63 6b 20 /* xShmLock
ff4a0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
ff4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff4c0 20 20 5c 0a 20 20 20 75 6e 69 78 53 68 6d 42 61 \. unixShmBa
ff4d0 72 72 69 65 72 2c 20 20 20 20 20 20 20 20 20 20 rrier,
ff4e0 20 20 20 2f 2a 20 78 53 68 6d 42 61 72 72 69 65 /* xShmBarrie
ff4f0 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 r */
ff500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff510 20 5c 0a 20 20 20 75 6e 69 78 53 68 6d 55 6e 6d \. unixShmUnm
ff520 61 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ap
ff530 20 20 2f 2a 20 78 53 68 6d 55 6e 6d 61 70 20 2a /* xShmUnmap *
ff540 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
ff550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff560 5c 0a 7d 3b 20 20 20 20 20 20 20 20 20 20 20 20 \.};
ff570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ff5b0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 .static const sq
ff5c0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
ff5d0 20 2a 46 49 4e 44 45 52 23 23 49 6d 70 6c 28 63 *FINDER##Impl(c
ff5e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 75 6e onst char *z, un
ff5f0 69 78 46 69 6c 65 20 2a 70 29 7b 20 20 20 5c 0a ixFile *p){ \.
ff600 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
ff610 45 52 28 7a 29 3b 20 55 4e 55 53 45 44 5f 50 41 ER(z); UNUSED_PA
ff620 52 41 4d 45 54 45 52 28 70 29 3b 20 20 20 20 20 RAMETER(p);
ff630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff640 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ff650 20 72 65 74 75 72 6e 20 26 4d 45 54 48 4f 44 3b return &METHOD;
ff660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff690 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 20 \.}
ff6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff6e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61 \.sta
ff6f0 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
ff700 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 3_io_methods *(*
ff710 63 6f 6e 73 74 20 46 49 4e 44 45 52 29 28 63 6f const FINDER)(co
ff720 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 nst char*,unixFi
ff730 6c 65 20 2a 70 29 20 20 20 20 5c 0a 20 20 20 20 le *p) \.
ff740 3d 20 46 49 4e 44 45 52 23 23 49 6d 70 6c 3b 0a = FINDER##Impl;.
ff750 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20 61 72 65 20 ./*.** Here are
ff760 61 6c 6c 20 6f 66 20 74 68 65 20 73 71 6c 69 74 all of the sqlit
ff770 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
ff780 6a 65 63 74 73 20 66 6f 72 20 65 61 63 68 20 6f jects for each o
ff790 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 f the.** locking
ff7a0 20 73 74 72 61 74 65 67 69 65 73 2e 20 20 46 75 strategies. Fu
ff7b0 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 72 65 74 nctions that ret
ff7c0 75 72 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 urn pointers to
ff7d0 74 68 65 73 65 20 6d 65 74 68 6f 64 73 0a 2a 2a these methods.**
ff7e0 20 61 72 65 20 61 6c 73 6f 20 63 72 65 61 74 65 are also create
ff7f0 64 2e 0a 2a 2f 0a 49 4f 4d 45 54 48 4f 44 53 28 d..*/.IOMETHODS(
ff800 0a 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 . posixIoFinder
ff810 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
ff820 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 Finder function
ff830 6e 61 6d 65 20 2a 2f 0a 20 20 70 6f 73 69 78 49 name */. posixI
ff840 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 oMethods,
ff850 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 /* sqlite3_i
ff860 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
ff870 20 6e 61 6d 65 20 2a 2f 0a 20 20 32 2c 20 20 20 name */. 2,
ff880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff890 20 20 20 20 20 2f 2a 20 73 68 61 72 65 64 20 6d /* shared m
ff8a0 65 6d 6f 72 79 20 69 73 20 65 6e 61 62 6c 65 64 emory is enabled
ff8b0 20 2a 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c */. unixClose,
ff8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff8d0 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 /* xClose method
ff8e0 20 2a 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 */. unixLock,
ff8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff900 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 /* xLock method
ff910 2a 2f 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c */. unixUnlock,
ff920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ff930 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 * xUnlock method
ff940 20 2a 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 */. unixCheckR
ff950 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 eservedLock
ff960 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
ff970 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a dLock method */.
ff980 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e ).IOMETHODS(. n
ff990 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 olockIoFinder,
ff9a0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 /* Find
ff9b0 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 er function name
ff9c0 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 */. nolockIoMe
ff9d0 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 thods,
ff9e0 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 /* sqlite3_io_me
ff9f0 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d thods object nam
ffa00 65 20 2a 2f 0a 20 20 31 2c 20 20 20 20 20 20 20 e */. 1,
ffa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ffa20 20 2f 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 /* shared memor
ffa30 79 20 69 73 20 64 69 73 61 62 6c 65 64 20 2a 2f y is disabled */
ffa40 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 2c 20 . nolockClose,
ffa50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ffa60 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f xClose method */
ffa70 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 . nolockLock,
ffa80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ffa90 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a xLock method */.
ffaa0 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 nolockUnlock,
ffab0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
ffac0 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f Unlock method */
ffad0 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 . nolockCheckRe
ffae0 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f 2a 20 servedLock /*
ffaf0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
ffb00 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 49 ck method */.).I
ffb10 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f 74 6c OMETHODS(. dotl
ffb20 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 ockIoFinder,
ffb30 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 /* Finder
ffb40 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f function name */
ffb50 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 . dotlockIoMeth
ffb60 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ods, /*
ffb70 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
ffb80 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a ds object name *
ffb90 2f 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 /. 1,
ffba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ffbb0 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 69 shared memory i
ffbc0 73 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20 20 s disabled */.
ffbd0 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 dotlockClose,
ffbe0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c /* xCl
ffbf0 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ose method */.
ffc00 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 dotlockLock,
ffc10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f /* xLo
ffc20 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64 ck method */. d
ffc30 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 otlockUnlock,
ffc40 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c /* xUnl
ffc50 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ock method */.
ffc60 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 dotlockCheckRese
ffc70 72 76 65 64 4c 6f 63 6b 20 20 2f 2a 20 78 43 68 rvedLock /* xCh
ffc80 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 eckReservedLock
ffc90 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 0a 23 69 66 method */.)..#if
ffca0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
ffcb0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 OCKING_STYLE &&
ffcc0 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 !OS_VXWORKS.IOME
ffcd0 54 48 4f 44 53 28 0a 20 20 66 6c 6f 63 6b 49 6f THODS(. flockIo
ffce0 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 Finder,
ffcf0 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e /* Finder fun
ffd00 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 ction name */.
ffd10 66 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 flockIoMethods,
ffd20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c /* sql
ffd30 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
ffd40 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 object name */.
ffd50 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 1,
ffd60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 /* sh
ffd70 61 72 65 64 20 6d 65 6d 6f 72 79 20 69 73 20 64 ared memory is d
ffd80 69 73 61 62 6c 65 64 20 2a 2f 0a 20 20 66 6c 6f isabled */. flo
ffd90 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 ckClose,
ffda0 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 /* xClose
ffdb0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f method */. flo
ffdc0 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 ckLock,
ffdd0 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 /* xLock
ffde0 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 method */. floc
ffdf0 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 kUnlock,
ffe00 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b /* xUnlock
ffe10 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f method */. flo
ffe20 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c ckCheckReservedL
ffe30 6f 63 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 6b ock /* xCheck
ffe40 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 ReservedLock met
ffe50 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a hod */.).#endif.
ffe60 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
ffe70 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 73 65 6d IOMETHODS(. sem
ffe80 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 IoFinder,
ffe90 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 /* Finder
ffea0 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a function name *
ffeb0 2f 0a 20 20 73 65 6d 49 6f 4d 65 74 68 6f 64 73 /. semIoMethods
ffec0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
ffed0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
ffee0 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 ods object name
ffef0 2a 2f 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 */. 1,
fff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
fff10 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 * shared memory
fff20 69 73 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20 is disabled */.
fff30 20 73 65 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 semClose,
fff40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 /* xC
fff50 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 lose method */.
fff60 20 73 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 semLock,
fff70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c /* xL
fff80 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ock method */.
fff90 73 65 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 semUnlock,
fffa0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e /* xUn
fffb0 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 lock method */.
fffc0 20 73 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65 semCheckReserve
fffd0 64 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 dLock /* xC
fffe0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
ffff0 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e method */.).#en
10000 64 69 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 dif..#if defined
10001 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 (__APPLE__) && S
10002 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
10003 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 KING_STYLE.IOMET
10004 48 4f 44 53 28 0a 20 20 61 66 70 49 6f 46 69 6e HODS(. afpIoFin
10005 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 der,
10006 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 /* Finder func
10007 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 tion name */. a
10008 66 70 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 fpIoMethods,
10009 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 /* sqli
1000a 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f te3_io_methods o
1000b 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 bject name */.
1000c 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1,
1000d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 /* sha
1000e 72 65 64 20 6d 65 6d 6f 72 79 20 69 73 20 64 69 red memory is di
1000f 73 61 62 6c 65 64 20 2a 2f 0a 20 20 61 66 70 43 sabled */. afpC
10010 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 lose,
10011 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 /* xClose
10012 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 4c method */. afpL
10013 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 ock,
10014 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d /* xLock m
10015 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 55 6e ethod */. afpUn
10016 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 lock,
10017 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 /* xUnlock
10018 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 43 method */. afpC
10019 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
1001a 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 /* xCheckR
1001b 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 eservedLock meth
1001c 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a od */.).#endif..
1001d 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 /*.** The proxy
1001e 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 69 locking method i
1001f 73 20 61 20 22 73 75 70 65 72 2d 6d 65 74 68 6f s a "super-metho
10020 64 22 20 69 6e 20 74 68 65 20 73 65 6e 73 65 20 d" in the sense
10021 74 68 61 74 20 69 74 0a 2a 2a 20 6f 70 65 6e 73 that it.** opens
10022 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 20 secondary file
10023 64 65 73 63 72 69 70 74 6f 72 73 20 66 6f 72 20 descriptors for
10024 74 68 65 20 63 6f 6e 63 68 20 61 6e 64 20 6c 6f the conch and lo
10025 63 6b 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 ck files and.**
10026 69 74 20 75 73 65 73 20 70 72 6f 78 79 2c 20 64 it uses proxy, d
10027 6f 74 2d 66 69 6c 65 2c 20 41 46 50 2c 20 61 6e ot-file, AFP, an
10028 64 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e d flock() lockin
10029 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 6f g methods on tho
1002a 73 65 0a 2a 2a 20 73 65 63 6f 6e 64 61 72 79 20 se.** secondary
1002b 66 69 6c 65 73 2e 20 20 46 6f 72 20 74 68 69 73 files. For this
1002c 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 reason, the div
1002d 69 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 ision that imple
1002e 6d 65 6e 74 73 0a 2a 2a 20 70 72 6f 78 79 20 6c ments.** proxy l
1002f 6f 63 6b 69 6e 67 20 69 73 20 6c 6f 63 61 74 65 ocking is locate
10030 64 20 6d 75 63 68 20 66 75 72 74 68 65 72 20 64 d much further d
10031 6f 77 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e own in the file.
10032 20 20 42 75 74 20 77 65 20 6e 65 65 64 0a 2a 2a But we need.**
10033 20 74 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 to go ahead and
10034 20 64 65 66 69 6e 65 20 74 68 65 20 73 71 6c 69 define the sqli
10035 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 61 te3_io_methods a
10036 6e 64 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 nd finder functi
10037 6f 6e 0a 2a 2a 20 66 6f 72 20 70 72 6f 78 79 20 on.** for proxy
10038 6c 6f 63 6b 69 6e 67 20 68 65 72 65 2e 20 20 53 locking here. S
10039 6f 20 77 65 20 66 6f 72 77 61 72 64 20 64 65 63 o we forward dec
1003a 6c 61 72 65 20 74 68 65 20 49 2f 4f 20 6d 65 74 lare the I/O met
1003b 68 6f 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 hods..*/.#if def
1003c 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 ined(__APPLE__)
1003d 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
1003e 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 _LOCKING_STYLE.s
1003f 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 tatic int proxyC
10040 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c lose(sqlite3_fil
10041 65 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 e*);.static int
10042 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65 proxyLock(sqlite
10043 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 3_file*, int);.s
10044 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 55 tatic int proxyU
10045 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 nlock(sqlite3_fi
10046 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 le*, int);.stati
10047 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b c int proxyCheck
10048 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c ReservedLock(sql
10049 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a ite3_file*, int*
1004a 29 3b 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 );.IOMETHODS(.
1004b 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 2c 20 20 proxyIoFinder,
1004c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e /* Fin
1004d 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d der function nam
1004e 65 20 2a 2f 0a 20 20 70 72 6f 78 79 49 6f 4d 65 e */. proxyIoMe
1004f 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 thods,
10050 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d /* sqlite3_io_m
10051 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 ethods object na
10052 6d 65 20 2a 2f 0a 20 20 31 2c 20 20 20 20 20 20 me */. 1,
10053 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10054 20 20 2f 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f /* shared memo
10055 72 79 20 69 73 20 64 69 73 61 62 6c 65 64 20 2a ry is disabled *
10056 2f 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 2c 20 /. proxyClose,
10057 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10058 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a xClose method *
10059 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c 20 20 /. proxyLock,
1005a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1005b 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f xLock method */
1005c 0a 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 2c 20 . proxyUnlock,
1005d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1005e 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a xUnlock method *
1005f 2f 0a 20 20 70 72 6f 78 79 43 68 65 63 6b 52 65 /. proxyCheckRe
10060 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 2f 2a servedLock /*
10061 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
10062 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a ock method */.).
10063 23 65 6e 64 69 66 0a 0a 2f 2a 20 6e 66 73 20 6c #endif../* nfs l
10064 6f 63 6b 64 20 6f 6e 20 4f 53 58 20 31 30 2e 33 ockd on OSX 10.3
10065 2b 20 64 6f 65 73 6e 27 74 20 63 6c 65 61 72 20 + doesn't clear
10066 77 72 69 74 65 20 6c 6f 63 6b 73 20 77 68 65 6e write locks when
10067 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 a read lock is
10068 73 65 74 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e set */.#if defin
10069 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 ed(__APPLE__) &&
1006a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
1006b 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d OCKING_STYLE.IOM
1006c 45 54 48 4f 44 53 28 0a 20 20 6e 66 73 49 6f 46 ETHODS(. nfsIoF
1006d 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 inder,
1006e 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 /* Finder f
1006f 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a unction name */.
10070 20 20 6e 66 73 49 6f 4d 65 74 68 6f 64 73 2c 20 nfsIoMethods,
10071 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10072 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
10073 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a ds object name *
10074 2f 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 /. 1,
10075 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10076 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 * shared memory
10077 69 73 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20 is disabled */.
10078 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 20 20 20 unixClose,
10079 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1007a 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a Close method */.
1007b 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 20 20 unixLock,
1007c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1007d 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a xLock method */.
1007e 20 20 6e 66 73 55 6e 6c 6f 63 6b 2c 20 20 20 20 nfsUnlock,
1007f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10080 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a xUnlock method *
10081 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 /. unixCheckRes
10082 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 20 2f ervedLock /
10083 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
10084 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 Lock method */.)
10085 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 64 65 66 .#endif..#if def
10086 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 ined(__APPLE__)
10087 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
10088 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f _LOCKING_STYLE./
10089 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e 64 * .** This "find
1008a 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 er" function att
1008b 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d 69 empts to determi
1008c 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b ne the best lock
1008d 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a 2a 2a ing strategy .**
1008e 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 for the databas
1008f 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74 68 e file "filePath
10090 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74 75 ". It then retu
10091 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f rns the sqlite3_
10092 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 io_methods.** ob
10093 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d ject that implem
10094 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 74 65 ents that strate
10095 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 gy..**.** This i
10096 73 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f 6e 6c s for MacOSX onl
10097 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e y..*/.static con
10098 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
10099 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 6b 49 thods *autolockI
1009a 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 20 63 oFinderImpl(. c
1009b 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 50 onst char *fileP
1009c 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d 65 20 ath, /* name
1009d 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1009e 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 file */. unixFi
1009f 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 le *pNew
100a0 20 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c 65 20 /* open file
100a1 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 64 object for the d
100a2 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
100a3 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 ){. static cons
100a4 74 20 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67 t struct Mapping
100a5 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 {. const cha
100a6 72 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 r *zFilesystem;
100a7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
100a8 46 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 Filesystem type
100a9 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 name */. cons
100aa 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
100ab 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20 hods *pMethods;
100ac 20 20 2f 2a 20 41 70 70 72 6f 70 72 69 61 74 65 /* Appropriate
100ad 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 locking method
100ae 2a 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 3d 20 */. } aMap[] =
100af 7b 0a 20 20 20 20 7b 20 22 68 66 73 22 2c 20 20 {. { "hfs",
100b0 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 &posixIoMethod
100b1 73 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66 73 22 s },. { "ufs"
100b2 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 , &posixIoMet
100b3 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 61 hods },. { "a
100b4 66 70 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 fpfs", &afpIoMe
100b5 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 thods },. { "
100b6 73 6d 62 66 73 22 2c 20 20 26 61 66 70 49 6f 4d smbfs", &afpIoM
100b7 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 ethods },. {
100b8 22 77 65 62 64 61 76 22 2c 20 26 6e 6f 6c 6f 63 "webdav", &noloc
100b9 6b 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 kIoMethods },.
100ba 20 20 7b 20 30 2c 20 30 20 7d 0a 20 20 7d 3b 0a { 0, 0 }. };.
100bb 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 int i;. struc
100bc 74 20 73 74 61 74 66 73 20 66 73 49 6e 66 6f 3b t statfs fsInfo;
100bd 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 . struct flock
100be 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 lockInfo;.. if(
100bf 20 21 66 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 !filePath ){.
100c0 20 20 2f 2a 20 49 66 20 66 69 6c 65 50 61 74 68 /* If filePath
100c1 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e ==NULL that mean
100c2 73 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 s we are dealing
100c3 20 77 69 74 68 20 61 20 74 72 61 6e 73 69 65 6e with a transien
100c4 74 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 t file. ** th
100c5 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 at does not need
100c6 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 2e 20 2a to be locked. *
100c7 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 26 6e 6f /. return &no
100c8 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 lockIoMethods;.
100c9 20 7d 0a 20 20 69 66 28 20 73 74 61 74 66 73 28 }. if( statfs(
100ca 66 69 6c 65 50 61 74 68 2c 20 26 66 73 49 6e 66 filePath, &fsInf
100cb 6f 29 20 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20 o) != -1 ){.
100cc 69 66 28 20 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 if( fsInfo.f_fla
100cd 67 73 20 26 20 4d 4e 54 5f 52 44 4f 4e 4c 59 20 gs & MNT_RDONLY
100ce 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
100cf 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 &nolockIoMethods
100d0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 ;. }. for(
100d1 69 3d 30 3b 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 i=0; aMap[i].zFi
100d2 6c 65 73 79 73 74 65 6d 3b 20 69 2b 2b 29 7b 0a lesystem; i++){.
100d3 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 if( strcmp
100d4 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 65 (fsInfo.f_fstype
100d5 6e 61 6d 65 2c 20 61 4d 61 70 5b 69 5d 2e 7a 46 name, aMap[i].zF
100d6 69 6c 65 73 79 73 74 65 6d 29 3d 3d 30 20 29 7b ilesystem)==0 ){
100d7 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
100d8 61 4d 61 70 5b 69 5d 2e 70 4d 65 74 68 6f 64 73 aMap[i].pMethods
100d9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
100da 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 66 61 75 6c }.. /* Defaul
100db 74 20 63 61 73 65 2e 20 48 61 6e 64 6c 65 73 2c t case. Handles,
100dc 20 61 6d 6f 6e 67 73 74 20 6f 74 68 65 72 73 2c amongst others,
100dd 20 22 6e 66 73 22 2e 0a 20 20 2a 2a 20 54 65 73 "nfs".. ** Tes
100de 74 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 t byte-range loc
100df 6b 20 75 73 69 6e 67 20 66 63 6e 74 6c 28 29 2e k using fcntl().
100e0 20 49 66 20 74 68 65 20 63 61 6c 6c 20 73 75 63 If the call suc
100e1 63 65 65 64 73 2c 20 0a 20 20 2a 2a 20 61 73 73 ceeds, . ** ass
100e2 75 6d 65 20 74 68 61 74 20 74 68 65 20 66 69 6c ume that the fil
100e3 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 e-system support
100e4 73 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f s POSIX style lo
100e5 63 6b 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c 6f 63 cks. . */. loc
100e6 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b kInfo.l_len = 1;
100e7 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 . lockInfo.l_st
100e8 61 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 art = 0;. lockI
100e9 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 nfo.l_whence = S
100ea 45 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 EEK_SET;. lockI
100eb 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 nfo.l_type = F_R
100ec 44 4c 43 4b 3b 0a 20 20 69 66 28 20 6f 73 46 63 DLCK;. if( osFc
100ed 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c 20 46 5f 47 ntl(pNew->h, F_G
100ee 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e 66 6f 29 ETLK, &lockInfo)
100ef 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 66 28 !=-1 ) {. if(
100f0 20 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 strcmp(fsInfo.f
100f1 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 22 6e 66 _fstypename, "nf
100f2 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 s")==0 ){.
100f3 72 65 74 75 72 6e 20 26 6e 66 73 49 6f 4d 65 74 return &nfsIoMet
100f4 68 6f 64 73 3b 0a 20 20 20 20 7d 20 65 6c 73 65 hods;. } else
100f5 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 {. return
100f6 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b &posixIoMethods;
100f7 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
100f8 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f 74 6c return &dotl
100f9 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 ockIoMethods;.
100fa 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 }.}.static const
100fb 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
100fc 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 ods . *(*const
100fd 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 autolockIoFinder
100fe 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e )(const char*,un
100ff 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c ixFile*) = autol
10100 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b ockIoFinderImpl;
10101 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 ..#endif /* defi
10102 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 ned(__APPLE__) &
10103 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f & SQLITE_ENABLE_
10104 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f LOCKING_STYLE */
10105 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 ..#if OS_VXWORKS
10106 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
10107 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
10108 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e /* .** This "fin
10109 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74 der" function at
1010a 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d tempts to determ
1010b 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63 ine the best loc
1010c 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a 2a king strategy .*
1010d 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 * for the databa
1010e 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74 se file "filePat
1010f 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74 h". It then ret
10110 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 urns the sqlite3
10111 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f _io_methods.** o
10112 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 bject that imple
10113 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 74 ments that strat
10114 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 egy..**.** This
10115 69 73 20 66 6f 72 20 56 58 57 6f 72 6b 73 20 6f is for VXWorks o
10116 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 nly..*/.static c
10117 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
10118 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 methods *autoloc
10119 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 kIoFinderImpl(.
1011a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c const char *fil
1011b 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d ePath, /* nam
1011c 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
1011d 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 e file */. unix
1011e 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 20 File *pNew
1011f 20 20 20 20 20 2f 2a 20 74 68 65 20 6f 70 65 6e /* the open
10120 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 2a 2f 0a file object */.
10121 29 7b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 ){. struct floc
10122 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69 k lockInfo;.. i
10123 66 28 20 21 66 69 6c 65 50 61 74 68 20 29 7b 0a f( !filePath ){.
10124 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 50 61 /* If filePa
10125 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d 65 th==NULL that me
10126 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 6c 69 ans we are deali
10127 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e 73 69 ng with a transi
10128 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 ent file. **
10129 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 that does not ne
1012a 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 2e ed to be locked.
1012b 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 26 */. return &
1012c 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b nolockIoMethods;
1012d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 . }.. /* Test
1012e 69 66 20 66 63 6e 74 6c 28 29 20 69 73 20 73 75 if fcntl() is su
1012f 70 70 6f 72 74 65 64 20 61 6e 64 20 75 73 65 20 pported and use
10130 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63 6b POSIX style lock
10131 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 s.. ** Otherwis
10132 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 74 e fall back to t
10133 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f he named semapho
10134 72 65 20 6d 65 74 68 6f 64 2e 0a 20 20 2a 2f 0a re method.. */.
10135 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e lockInfo.l_len
10136 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f = 1;. lockInfo
10137 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 .l_start = 0;.
10138 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 lockInfo.l_whenc
10139 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 e = SEEK_SET;.
1013a 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 lockInfo.l_type
1013b 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28 = F_RDLCK;. if(
1013c 20 6f 73 46 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 osFcntl(pNew->h
1013d 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_GETLK, &lock
1013e 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 Info)!=-1 ) {.
1013f 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 return &posixI
10140 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 oMethods;. }els
10141 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 73 e{. return &s
10142 65 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d emIoMethods;. }
10143 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 .}.static const
10144 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
10145 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 61 ds . *(*const a
10146 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29 utolockIoFinder)
10147 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 (const char*,uni
10148 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c 6f xFile*) = autolo
10149 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a ckIoFinderImpl;.
1014a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 .#endif /* OS_VX
1014b 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f WORKS && SQLITE_
1014c 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
1014d 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 TYLE */../*.** A
1014e 6e 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20 n abstract type
1014f 66 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f for a pointer to
10150 20 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e a IO method fin
10151 64 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f der function:.*/
10152 0a 74 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73 .typedef const s
10153 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
10154 73 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65 s *(*finder_type
10155 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e )(const char*,un
10156 69 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f 2a 2a 2a ixFile*);.../***
10157 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10158 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10159 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1015a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1015b 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
1015c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1015d 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 ****** sqlite3_v
1015e 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a fs methods *****
1015f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10160 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
10161 69 73 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 is division cont
10162 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 ains the impleme
10163 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f ntation of metho
10164 64 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c ds on the.** sql
10165 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e ite3_vfs object.
10166 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 .*/../*.** Initi
10167 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e alize the conten
10168 74 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 ts of the unixFi
10169 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 le structure poi
1016a 6e 74 65 64 20 74 6f 20 62 79 20 70 49 64 2e 0a nted to by pId..
1016b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 */.static int fi
1016c 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20 llInUnixFile(.
1016d 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
1016e 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 s, /* Point
1016f 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 er to vfs object
10170 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 */. int h,
10171 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10172 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 Open file descr
10173 69 70 74 6f 72 20 6f 66 20 66 69 6c 65 20 62 65 iptor of file be
10174 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 ing opened */.
10175 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 sqlite3_file *pI
10176 64 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 d, /* Write
10177 20 74 6f 20 74 68 65 20 75 6e 69 78 46 69 6c 65 to the unixFile
10178 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20 structure here
10179 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1017a 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 *zFilename, /*
1017b 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 Name of the file
1017c 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f being opened */
1017d 0a 20 20 69 6e 74 20 63 74 72 6c 46 6c 61 67 73 . int ctrlFlags
1017e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 5a 65 /* Ze
1017f 72 6f 20 6f 72 20 6d 6f 72 65 20 55 4e 49 58 46 ro or more UNIXF
10180 49 4c 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a ILE_* values */.
10181 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 ){. const sqlit
10182 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 e3_io_methods *p
10183 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 LockingStyle;.
10184 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 3d unixFile *pNew =
10185 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 70 49 64 (unixFile *)pId
10186 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
10187 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 ITE_OK;.. asser
10188 74 28 20 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 3d t( pNew->pInode=
10189 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 20 2f 2a 20 55 =NULL );.. /* U
1018a 73 75 61 6c 6c 79 20 74 68 65 20 70 61 74 68 20 sually the path
1018b 7a 46 69 6c 65 6e 61 6d 65 20 73 68 6f 75 6c 64 zFilename should
1018c 20 6e 6f 74 20 62 65 20 61 20 72 65 6c 61 74 69 not be a relati
1018d 76 65 20 70 61 74 68 6e 61 6d 65 2e 20 54 68 65 ve pathname. The
1018e 0a 20 20 2a 2a 20 65 78 63 65 70 74 69 6f 6e 20 . ** exception
1018f 69 73 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 is when opening
10190 74 68 65 20 70 72 6f 78 79 20 22 63 6f 6e 63 68 the proxy "conch
10191 22 20 66 69 6c 65 20 69 6e 20 62 75 69 6c 64 73 " file in builds
10192 20 74 68 61 74 0a 20 20 2a 2a 20 69 6e 63 6c 75 that. ** inclu
10193 64 65 20 74 68 65 20 73 70 65 63 69 61 6c 20 41 de the special A
10194 70 70 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 pple locking sty
10195 6c 65 73 2e 0a 20 20 2a 2f 0a 23 69 66 20 64 65 les.. */.#if de
10196 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
10197 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
10198 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
10199 20 20 61 73 73 65 72 74 28 20 7a 46 69 6c 65 6e assert( zFilen
1019a 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e ame==0 || zFilen
1019b 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 0a 20 20 20 ame[0]=='/' .
1019c 20 7c 7c 20 70 56 66 73 2d 3e 70 41 70 70 44 61 || pVfs->pAppDa
1019d 74 61 3d 3d 28 76 6f 69 64 2a 29 26 61 75 74 6f ta==(void*)&auto
1019e 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 3b 0a lockIoFinder );.
1019f 23 65 6c 73 65 0a 20 20 61 73 73 65 72 74 28 20 #else. assert(
101a0 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 zFilename==0 ||
101a1 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 27 2f zFilename[0]=='/
101a2 27 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f ' );.#endif.. /
101a3 2a 20 4e 6f 20 6c 6f 63 6b 69 6e 67 20 6f 63 63 * No locking occ
101a4 75 72 73 20 69 6e 20 74 65 6d 70 6f 72 61 72 79 urs in temporary
101a5 20 66 69 6c 65 73 20 2a 2f 0a 20 20 61 73 73 65 files */. asse
101a6 72 74 28 20 7a 46 69 6c 65 6e 61 6d 65 21 3d 30 rt( zFilename!=0
101a7 20 7c 7c 20 28 63 74 72 6c 46 6c 61 67 73 20 26 || (ctrlFlags &
101a8 20 55 4e 49 58 46 49 4c 45 5f 4e 4f 4c 4f 43 4b UNIXFILE_NOLOCK
101a9 29 21 3d 30 20 29 3b 0a 0a 20 20 4f 53 54 52 41 )!=0 );.. OSTRA
101aa 43 45 28 28 22 4f 50 45 4e 20 20 20 20 25 2d 33 CE(("OPEN %-3
101ab 64 20 25 73 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c d %s\n", h, zFil
101ac 65 6e 61 6d 65 29 29 3b 0a 20 20 70 4e 65 77 2d ename));. pNew-
101ad 3e 68 20 3d 20 68 3b 0a 20 20 70 4e 65 77 2d 3e >h = h;. pNew->
101ae 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 70 pVfs = pVfs;. p
101af 4e 65 77 2d 3e 7a 50 61 74 68 20 3d 20 7a 46 69 New->zPath = zFi
101b0 6c 65 6e 61 6d 65 3b 0a 20 20 70 4e 65 77 2d 3e lename;. pNew->
101b1 63 74 72 6c 46 6c 61 67 73 20 3d 20 28 75 38 29 ctrlFlags = (u8)
101b2 63 74 72 6c 46 6c 61 67 73 3b 0a 20 20 69 66 28 ctrlFlags;. if(
101b3 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 62 6f 6f sqlite3_uri_boo
101b4 6c 65 61 6e 28 28 28 63 74 72 6c 46 6c 61 67 73 lean(((ctrlFlags
101b5 20 26 20 55 4e 49 58 46 49 4c 45 5f 55 52 49 29 & UNIXFILE_URI)
101b6 20 3f 20 7a 46 69 6c 65 6e 61 6d 65 20 3a 20 30 ? zFilename : 0
101b7 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ),.
101b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 70 "p
101b9 73 6f 77 22 2c 20 53 51 4c 49 54 45 5f 50 4f 57 sow", SQLITE_POW
101ba 45 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 ERSAFE_OVERWRITE
101bb 29 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 63 ) ){. pNew->c
101bc 74 72 6c 46 6c 61 67 73 20 7c 3d 20 55 4e 49 58 trlFlags |= UNIX
101bd 46 49 4c 45 5f 50 53 4f 57 3b 0a 20 20 7d 0a 20 FILE_PSOW;. }.
101be 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 56 66 73 if( memcmp(pVfs
101bf 2d 3e 7a 4e 61 6d 65 2c 22 75 6e 69 78 2d 65 78 ->zName,"unix-ex
101c0 63 6c 22 2c 31 30 29 3d 3d 30 20 29 7b 0a 20 20 cl",10)==0 ){.
101c1 20 20 70 4e 65 77 2d 3e 63 74 72 6c 46 6c 61 67 pNew->ctrlFlag
101c2 73 20 7c 3d 20 55 4e 49 58 46 49 4c 45 5f 45 58 s |= UNIXFILE_EX
101c3 43 4c 3b 0a 20 20 7d 0a 0a 23 69 66 20 4f 53 5f CL;. }..#if OS_
101c4 56 58 57 4f 52 4b 53 0a 20 20 70 4e 65 77 2d 3e VXWORKS. pNew->
101c5 70 49 64 20 3d 20 76 78 77 6f 72 6b 73 46 69 6e pId = vxworksFin
101c6 64 46 69 6c 65 49 64 28 7a 46 69 6c 65 6e 61 6d dFileId(zFilenam
101c7 65 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e e);. if( pNew->
101c8 70 49 64 3d 3d 30 20 29 7b 0a 20 20 20 20 63 74 pId==0 ){. ct
101c9 72 6c 46 6c 61 67 73 20 7c 3d 20 55 4e 49 58 46 rlFlags |= UNIXF
101ca 49 4c 45 5f 4e 4f 4c 4f 43 4b 3b 0a 20 20 20 20 ILE_NOLOCK;.
101cb 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
101cc 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 M;. }.#endif..
101cd 20 69 66 28 20 63 74 72 6c 46 6c 61 67 73 20 26 if( ctrlFlags &
101ce 20 55 4e 49 58 46 49 4c 45 5f 4e 4f 4c 4f 43 4b UNIXFILE_NOLOCK
101cf 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 69 6e 67 ){. pLocking
101d0 53 74 79 6c 65 20 3d 20 26 6e 6f 6c 6f 63 6b 49 Style = &nolockI
101d1 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 oMethods;. }els
101d2 65 7b 0a 20 20 20 20 70 4c 6f 63 6b 69 6e 67 53 e{. pLockingS
101d3 74 79 6c 65 20 3d 20 28 2a 2a 28 66 69 6e 64 65 tyle = (**(finde
101d4 72 5f 74 79 70 65 2a 29 70 56 66 73 2d 3e 70 41 r_type*)pVfs->pA
101d5 70 70 44 61 74 61 29 28 7a 46 69 6c 65 6e 61 6d ppData)(zFilenam
101d6 65 2c 20 70 4e 65 77 29 3b 0a 23 69 66 20 53 51 e, pNew);.#if SQ
101d7 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
101d8 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 2f 2a ING_STYLE. /*
101d9 20 43 61 63 68 65 20 7a 46 69 6c 65 6e 61 6d 65 Cache zFilename
101da 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 in the locking
101db 63 6f 6e 74 65 78 74 20 28 41 46 50 20 61 6e 64 context (AFP and
101dc 20 64 6f 74 6c 6f 63 6b 20 6f 76 65 72 72 69 64 dotlock overrid
101dd 65 29 20 66 6f 72 0a 20 20 20 20 2a 2a 20 70 72 e) for. ** pr
101de 6f 78 79 4c 6f 63 6b 20 61 63 74 69 76 61 74 69 oxyLock activati
101df 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 20 28 on is possible (
101e0 72 65 6d 6f 74 65 20 70 72 6f 78 79 20 69 73 20 remote proxy is
101e1 62 61 73 65 64 20 6f 6e 20 64 62 20 6e 61 6d 65 based on db name
101e2 29 0a 20 20 20 20 2a 2a 20 7a 46 69 6c 65 6e 61 ). ** zFilena
101e3 6d 65 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 me remains valid
101e4 20 75 6e 74 69 6c 20 66 69 6c 65 20 69 73 20 63 until file is c
101e5 6c 6f 73 65 64 2c 20 74 6f 20 73 75 70 70 6f 72 losed, to suppor
101e6 74 20 2a 2f 0a 20 20 20 20 70 4e 65 77 2d 3e 6c t */. pNew->l
101e7 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 ockingContext =
101e8 28 76 6f 69 64 2a 29 7a 46 69 6c 65 6e 61 6d 65 (void*)zFilename
101e9 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 ;.#endif. }..
101ea 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c if( pLockingStyl
101eb 65 20 3d 3d 20 26 70 6f 73 69 78 49 6f 4d 65 74 e == &posixIoMet
101ec 68 6f 64 73 0a 23 69 66 20 64 65 66 69 6e 65 64 hods.#if defined
101ed 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 (__APPLE__) && S
101ee 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
101ef 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 7c KING_STYLE. |
101f0 7c 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 | pLockingStyle
101f1 3d 3d 20 26 6e 66 73 49 6f 4d 65 74 68 6f 64 73 == &nfsIoMethods
101f2 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20 .#endif. ){.
101f3 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
101f4 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 );. rc = find
101f5 49 6e 6f 64 65 49 6e 66 6f 28 70 4e 65 77 2c 20 InodeInfo(pNew,
101f6 26 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 29 3b 0a &pNew->pInode);.
101f7 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
101f8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f TE_OK ){. /
101f9 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 * If an error oc
101fa 63 75 72 65 64 20 69 6e 20 66 69 6e 64 49 6e 6f cured in findIno
101fb 64 65 49 6e 66 6f 28 29 2c 20 63 6c 6f 73 65 20 deInfo(), close
101fc 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
101fd 74 6f 72 0a 20 20 20 20 20 20 2a 2a 20 69 6d 6d tor. ** imm
101fe 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 ediately, before
101ff 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 20 6d releasing the m
10200 75 74 65 78 2e 20 66 69 6e 64 49 6e 6f 64 65 49 utex. findInodeI
10201 6e 66 6f 28 29 20 6d 61 79 20 66 61 69 6c 0a 20 nfo() may fail.
10202 20 20 20 20 20 2a 2a 20 69 6e 20 74 77 6f 20 73 ** in two s
10203 63 65 6e 61 72 69 6f 73 3a 0a 20 20 20 20 20 20 cenarios:.
10204 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 28 61 **. ** (a
10205 29 20 41 20 63 61 6c 6c 20 74 6f 20 66 73 74 61 ) A call to fsta
10206 74 28 29 20 66 61 69 6c 65 64 2e 0a 20 20 20 20 t() failed..
10207 20 20 2a 2a 20 20 20 28 62 29 20 41 20 6d 61 6c ** (b) A mal
10208 6c 6f 63 20 66 61 69 6c 65 64 2e 0a 20 20 20 20 loc failed..
10209 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 63 **. ** Sc
1020a 65 6e 61 72 69 6f 20 28 62 29 20 6d 61 79 20 6f enario (b) may o
1020b 6e 6c 79 20 6f 63 63 75 72 20 69 66 20 74 68 65 nly occur if the
1020c 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 process is hold
1020d 69 6e 67 20 6e 6f 20 6f 74 68 65 72 0a 20 20 20 ing no other.
1020e 20 20 20 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 ** file descr
1020f 69 70 74 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 iptors open on t
10210 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 20 49 66 he same file. If
10211 20 74 68 65 72 65 20 77 65 72 65 20 6f 74 68 65 there were othe
10212 72 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 r file. **
10213 64 65 73 63 72 69 70 74 6f 72 73 20 6f 6e 20 74 descriptors on t
10214 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20 6e his file, then n
10215 6f 20 6d 61 6c 6c 6f 63 20 77 6f 75 6c 64 20 62 o malloc would b
10216 65 20 72 65 71 75 69 72 65 64 20 62 79 0a 20 20 e required by.
10217 20 20 20 20 2a 2a 20 66 69 6e 64 49 6e 6f 64 65 ** findInode
10218 49 6e 66 6f 28 29 2e 20 49 66 20 74 68 69 73 20 Info(). If this
10219 69 73 20 74 68 65 20 63 61 73 65 2c 20 69 74 20 is the case, it
1021a 69 73 20 71 75 69 74 65 20 73 61 66 65 20 74 6f is quite safe to
1021b 20 63 6c 6f 73 65 0a 20 20 20 20 20 20 2a 2a 20 close. **
1021c 68 61 6e 64 6c 65 20 68 20 2d 20 61 73 20 69 74 handle h - as it
1021d 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
1021e 68 61 74 20 6e 6f 20 70 6f 73 69 78 20 6c 6f 63 hat no posix loc
1021f 6b 73 20 77 69 6c 6c 20 62 65 20 72 65 6c 65 61 ks will be relea
10220 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 sed. ** by
10221 64 6f 69 6e 67 20 73 6f 2e 0a 20 20 20 20 20 20 doing so..
10222 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 73 **. ** If s
10223 63 65 6e 61 72 69 6f 20 28 61 29 20 63 61 75 73 cenario (a) caus
10224 65 64 20 74 68 65 20 65 72 72 6f 72 20 74 68 65 ed the error the
10225 6e 20 74 68 69 6e 67 73 20 61 72 65 20 6e 6f 74 n things are not
10226 20 73 6f 20 73 61 66 65 2e 20 54 68 65 0a 20 20 so safe. The.
10227 20 20 20 20 2a 2a 20 69 6d 70 6c 69 63 69 74 20 ** implicit
10228 61 73 73 75 6d 70 74 69 6f 6e 20 68 65 72 65 20 assumption here
10229 69 73 20 74 68 61 74 20 69 66 20 66 73 74 61 74 is that if fstat
1022a 28 29 20 66 61 69 6c 73 2c 20 74 68 69 6e 67 73 () fails, things
1022b 20 61 72 65 20 69 6e 0a 20 20 20 20 20 20 2a 2a are in. **
1022c 20 73 75 63 68 20 62 61 64 20 73 68 61 70 65 20 such bad shape
1022d 74 68 61 74 20 64 72 6f 70 70 69 6e 67 20 61 20 that dropping a
1022e 6c 6f 63 6b 20 6f 72 20 74 77 6f 20 64 6f 65 73 lock or two does
1022f 6e 27 74 20 6d 61 74 74 65 72 20 6d 75 63 68 2e n't matter much.
10230 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
10231 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65 robust_close(pNe
10232 77 2c 20 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b w, h, __LINE__);
10233 0a 20 20 20 20 20 20 68 20 3d 20 2d 31 3b 0a 20 . h = -1;.
10234 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65 61 }. unixLea
10235 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 0a veMutex();. }..
10236 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c #if SQLITE_ENABL
10237 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
10238 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 && defined(__APP
10239 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69 66 28 LE__). else if(
1023a 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d pLockingStyle =
1023b 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 = &afpIoMethods
1023c 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20 6c 6f ){. /* AFP lo
1023d 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 cking uses the f
1023e 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e ile path so it n
1023f 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 eeds to be inclu
10240 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 ded in. ** th
10241 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 e afpLockingCont
10242 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ext.. */.
10243 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 afpLockingContex
10244 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70 4e 65 t *pCtx;. pNe
10245 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 w->lockingContex
10246 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c 69 74 t = pCtx = sqlit
10247 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f e3_malloc( sizeo
10248 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 20 20 f(*pCtx) );.
10249 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b 0a 20 if( pCtx==0 ){.
1024a 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1024b 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 _NOMEM;. }els
1024c 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42 3a 20 e{. /* NB:
1024d 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 74 73 zFilename exists
1024e 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 61 6c and remains val
1024f 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69 6c id until the fil
10250 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 20 20 e is closed.
10251 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 74 ** according t
10252 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 46 31 o requirement F1
10253 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 6f 20 1141. So we do
10254 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 not need to make
10255 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 a. ** copy
10256 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 of the filename
10257 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74 78 2d . */. pCtx-
10258 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c 65 6e >dbPath = zFilen
10259 61 6d 65 3b 0a 20 20 20 20 20 20 70 43 74 78 2d ame;. pCtx-
1025a 3e 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 >reserved = 0;.
1025b 20 20 20 20 20 73 72 61 6e 64 6f 6d 64 65 76 28 srandomdev(
1025c 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 45 6e 74 );. unixEnt
1025d 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 erMutex();.
1025e 20 72 63 20 3d 20 66 69 6e 64 49 6e 6f 64 65 49 rc = findInodeI
1025f 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d nfo(pNew, &pNew-
10260 3e 70 49 6e 6f 64 65 29 3b 0a 20 20 20 20 20 20 >pInode);.
10261 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
10262 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c K ){. sql
10263 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 2d 3e ite3_free(pNew->
10264 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b lockingContext);
10265 0a 20 20 20 20 20 20 20 20 72 6f 62 75 73 74 5f . robust_
10266 63 6c 6f 73 65 28 70 4e 65 77 2c 20 68 2c 20 5f close(pNew, h, _
10267 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 20 20 20 20 20 _LINE__);.
10268 20 20 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 h = -1;.
10269 7d 0a 20 20 20 20 20 20 75 6e 69 78 4c 65 61 76 }. unixLeav
1026a 65 4d 75 74 65 78 28 29 3b 20 20 20 20 20 20 20 eMutex();
1026b 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 . }. }.#end
1026c 69 66 0a 0a 20 20 65 6c 73 65 20 69 66 28 20 70 if.. else if( p
1026d 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 LockingStyle ==
1026e 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 &dotlockIoMethod
1026f 73 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 74 66 s ){. /* Dotf
10270 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 ile locking uses
10271 20 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 the file path s
10272 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 o it needs to be
10273 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20 20 included in.
10274 20 2a 2a 20 74 68 65 20 64 6f 74 6c 6f 63 6b 4c ** the dotlockL
10275 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 0a 20 ockingContext .
10276 20 20 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a */. char *
10277 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 20 20 69 zLockFile;. i
10278 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 nt nFilename;.
10279 20 20 61 73 73 65 72 74 28 20 7a 46 69 6c 65 6e assert( zFilen
1027a 61 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20 6e 46 ame!=0 );. nF
1027b 69 6c 65 6e 61 6d 65 20 3d 20 28 69 6e 74 29 73 ilename = (int)s
1027c 74 72 6c 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 29 trlen(zFilename)
1027d 20 2b 20 36 3b 0a 20 20 20 20 7a 4c 6f 63 6b 46 + 6;. zLockF
1027e 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 73 71 ile = (char *)sq
1027f 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 46 69 lite3_malloc(nFi
10280 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 lename);. if(
10281 20 7a 4c 6f 63 6b 46 69 6c 65 3d 3d 30 20 29 7b zLockFile==0 ){
10282 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
10283 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 TE_NOMEM;. }e
10284 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
10285 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46 69 6c e3_snprintf(nFil
10286 65 6e 61 6d 65 2c 20 7a 4c 6f 63 6b 46 69 6c 65 ename, zLockFile
10287 2c 20 22 25 73 22 20 44 4f 54 4c 4f 43 4b 5f 53 , "%s" DOTLOCK_S
10288 55 46 46 49 58 2c 20 7a 46 69 6c 65 6e 61 6d 65 UFFIX, zFilename
10289 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 );. }. pNe
1028a 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 w->lockingContex
1028b 74 20 3d 20 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 t = zLockFile;.
1028c 20 7d 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 }..#if OS_VXWOR
1028d 4b 53 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c KS. else if( pL
1028e 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 ockingStyle == &
1028f 73 65 6d 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a semIoMethods ){.
10290 20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 73 65 6d /* Named sem
10291 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 75 aphore locking u
10292 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74 ses the file pat
10293 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f h so it needs to
10294 20 62 65 0a 20 20 20 20 2a 2a 20 69 6e 63 6c 75 be. ** inclu
10295 64 65 64 20 69 6e 20 74 68 65 20 73 65 6d 4c 6f ded in the semLo
10296 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 20 20 20 ckingContext.
10297 20 2a 2f 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 */. unixEnte
10298 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 63 rMutex();. rc
10299 20 3d 20 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f = findInodeInfo
1029a 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 49 (pNew, &pNew->pI
1029b 6e 6f 64 65 29 3b 0a 20 20 20 20 69 66 28 20 28 node);. if( (
1029c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 26 rc==SQLITE_OK) &
1029d 26 20 28 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 2d & (pNew->pInode-
1029e 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20 29 7b 0a >pSem==NULL) ){.
1029f 20 20 20 20 20 20 63 68 61 72 20 2a 7a 53 65 6d char *zSem
102a0 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e 70 49 6e Name = pNew->pIn
102a1 6f 64 65 2d 3e 61 53 65 6d 4e 61 6d 65 3b 0a 20 ode->aSemName;.
102a2 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 int n;.
102a3 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
102a4 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c tf(MAX_PATHNAME,
102a5 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 2f 25 73 2e zSemName, "/%s.
102a6 73 65 6d 22 2c 0a 20 20 20 20 20 20 20 20 20 20 sem",.
102a7 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65 pNe
102a8 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 63 w->pId->zCanonic
102a9 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 66 alName);. f
102aa 6f 72 28 20 6e 3d 31 3b 20 7a 53 65 6d 4e 61 6d or( n=1; zSemNam
102ab 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20 20 20 e[n]; n++ ).
102ac 20 20 20 20 69 66 28 20 7a 53 65 6d 4e 61 6d 65 if( zSemName
102ad 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 6d 4e [n]=='/' ) zSemN
102ae 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a 20 20 ame[n] = '_';.
102af 20 20 20 20 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 pNew->pInode
102b0 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 65 ->pSem = sem_ope
102b1 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52 n(zSemName, O_CR
102b2 45 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a 20 EAT, 0666, 1);.
102b3 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 if( pNew->p
102b4 49 6e 6f 64 65 2d 3e 70 53 65 6d 20 3d 3d 20 53 Inode->pSem == S
102b5 45 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20 EM_FAILED ){.
102b6 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
102b7 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 _NOMEM;.
102b8 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 2d 3e 61 53 pNew->pInode->aS
102b9 65 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 emName[0] = '\0'
102ba 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
102bb 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 unixLeaveMut
102bc 65 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 ex();. }.#endif
102bd 0a 20 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74 . . pNew->last
102be 45 72 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 4f Errno = 0;.#if O
102bf 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 S_VXWORKS. if(
102c0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
102c1 0a 20 20 20 20 69 66 28 20 68 3e 3d 30 20 29 20 . if( h>=0 )
102c2 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65 robust_close(pNe
102c3 77 2c 20 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b w, h, __LINE__);
102c4 0a 20 20 20 20 68 20 3d 20 2d 31 3b 0a 20 20 20 . h = -1;.
102c5 20 6f 73 55 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e osUnlink(zFilen
102c6 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 65 6c 65 ame);. isDele
102c7 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 te = 0;. }. if
102c8 28 20 69 73 44 65 6c 65 74 65 20 29 20 70 4e 65 ( isDelete ) pNe
102c9 77 2d 3e 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20 w->ctrlFlags |=
102ca 55 4e 49 58 46 49 4c 45 5f 44 45 4c 45 54 45 3b UNIXFILE_DELETE;
102cb 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 72 63 .#endif. if( rc
102cc 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
102cd 20 20 20 69 66 28 20 68 3e 3d 30 20 29 20 72 6f if( h>=0 ) ro
102ce 62 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65 77 2c bust_close(pNew,
102cf 20 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 h, __LINE__);.
102d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 }else{. pNew
102d1 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f 63 ->pMethod = pLoc
102d2 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20 4f kingStyle;. O
102d3 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a penCounter(+1);.
102d4 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
102d5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
102d6 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 the name of a d
102d7 69 72 65 63 74 6f 72 79 20 69 6e 20 77 68 69 63 irectory in whic
102d8 68 20 74 6f 20 70 75 74 20 74 65 6d 70 6f 72 61 h to put tempora
102d9 72 79 20 66 69 6c 65 73 2e 0a 2a 2a 20 49 66 20 ry files..** If
102da 6e 6f 20 73 75 69 74 61 62 6c 65 20 74 65 6d 70 no suitable temp
102db 6f 72 61 72 79 20 66 69 6c 65 20 64 69 72 65 63 orary file direc
102dc 74 6f 72 79 20 63 61 6e 20 62 65 20 66 6f 75 6e tory can be foun
102dd 64 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a d, return NULL..
102de 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
102df 63 68 61 72 20 2a 75 6e 69 78 54 65 6d 70 46 69 char *unixTempFi
102e0 6c 65 44 69 72 28 76 6f 69 64 29 7b 0a 20 20 73 leDir(void){. s
102e1 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
102e2 20 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 *azDirs[] = {.
102e3 20 20 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a 20 0,. 0,.
102e4 20 20 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c 0a "/var/tmp",.
102e5 20 20 20 20 20 22 2f 75 73 72 2f 74 6d 70 22 2c "/usr/tmp",
102e6 0a 20 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20 20 . "/tmp",.
102e7 20 20 20 30 20 20 20 20 20 20 20 20 2f 2a 20 4c 0 /* L
102e8 69 73 74 20 74 65 72 6d 69 6e 61 74 6f 72 20 2a ist terminator *
102e9 2f 0a 20 20 7d 3b 0a 20 20 75 6e 73 69 67 6e 65 /. };. unsigne
102ea 64 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 d int i;. struc
102eb 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 63 6f t stat buf;. co
102ec 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 72 20 3d nst char *zDir =
102ed 20 30 3b 0a 0a 20 20 61 7a 44 69 72 73 5b 30 5d 0;.. azDirs[0]
102ee 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f = sqlite3_temp_
102ef 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 69 66 28 directory;. if(
102f0 20 21 61 7a 44 69 72 73 5b 31 5d 20 29 20 61 7a !azDirs[1] ) az
102f1 44 69 72 73 5b 31 5d 20 3d 20 67 65 74 65 6e 76 Dirs[1] = getenv
102f2 28 22 54 4d 50 44 49 52 22 29 3b 0a 20 20 66 6f ("TMPDIR");. fo
102f3 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 r(i=0; i<sizeof(
102f4 61 7a 44 69 72 73 29 2f 73 69 7a 65 6f 66 28 61 azDirs)/sizeof(a
102f5 7a 44 69 72 73 5b 30 5d 29 3b 20 7a 44 69 72 3d zDirs[0]); zDir=
102f6 61 7a 44 69 72 73 5b 69 2b 2b 5d 29 7b 0a 20 20 azDirs[i++]){.
102f7 20 20 69 66 28 20 7a 44 69 72 3d 3d 30 20 29 20 if( zDir==0 )
102f8 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 continue;. if
102f9 28 20 6f 73 53 74 61 74 28 7a 44 69 72 2c 20 26 ( osStat(zDir, &
102fa 62 75 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b buf) ) continue;
102fb 0a 20 20 20 20 69 66 28 20 21 53 5f 49 53 44 49 . if( !S_ISDI
102fc 52 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29 20 29 R(buf.st_mode) )
102fd 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 continue;. i
102fe 66 28 20 6f 73 41 63 63 65 73 73 28 7a 44 69 72 f( osAccess(zDir
102ff 2c 20 30 37 29 20 29 20 63 6f 6e 74 69 6e 75 65 , 07) ) continue
10300 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d ;. break;. }
10301 0a 20 20 72 65 74 75 72 6e 20 7a 44 69 72 3b 0a . return zDir;.
10302 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 }../*.** Create
10303 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
10304 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 name in zBuf.
10305 7a 42 75 66 20 6d 75 73 74 20 62 65 20 61 6c 6c zBuf must be all
10306 6f 63 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 ocated.** by the
10307 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 73 73 calling process
10308 20 61 6e 64 20 6d 75 73 74 20 62 65 20 62 69 67 and must be big
10309 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 enough to hold
1030a 61 74 20 6c 65 61 73 74 0a 2a 2a 20 70 56 66 73 at least.** pVfs
1030b 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 62 79 74 ->mxPathname byt
1030c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
1030d 74 20 75 6e 69 78 47 65 74 54 65 6d 70 6e 61 6d t unixGetTempnam
1030e 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 e(int nBuf, char
1030f 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69 *zBuf){. stati
10310 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 c const unsigned
10311 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d char zChars[] =
10312 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 69 6a . "abcdefghij
10313 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a klmnopqrstuvwxyz
10314 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 48 49 ". "ABCDEFGHI
10315 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 JKLMNOPQRSTUVWXY
10316 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 Z". "01234567
10317 38 39 22 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 89";. unsigned
10318 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 6f 6e 73 int i, j;. cons
10319 74 20 63 68 61 72 20 2a 7a 44 69 72 3b 0a 0a 20 t char *zDir;..
1031a 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 /* It's odd to
1031b 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 simulate an io-e
1031c 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 rror here, but r
1031d 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 eally this is ju
1031e 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 st. ** using th
1031f 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 e io-error infra
10320 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 structure to tes
10321 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 t that SQLite ha
10322 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 ndles this. **
10323 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 function failing
10324 2e 20 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 . . */. Simula
10325 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
10326 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 n SQLITE_IOERR )
10327 3b 0a 0a 20 20 7a 44 69 72 20 3d 20 75 6e 69 78 ;.. zDir = unix
10328 54 65 6d 70 46 69 6c 65 44 69 72 28 29 3b 0a 20 TempFileDir();.
10329 20 69 66 28 20 7a 44 69 72 3d 3d 30 20 29 20 7a if( zDir==0 ) z
1032a 44 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a Dir = ".";.. /*
1032b 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 Check that the
1032c 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73 output buffer is
1032d 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 66 6f large enough fo
1032e 72 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 r the temporary
1032f 66 69 6c 65 20 0a 20 20 2a 2a 20 6e 61 6d 65 2e file . ** name.
10330 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 72 If it is not, r
10331 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
10332 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 OR.. */. if( (
10333 73 74 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20 73 strlen(zDir) + s
10334 74 72 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45 4d trlen(SQLITE_TEM
10335 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 29 20 2b P_FILE_PREFIX) +
10336 20 31 38 29 20 3e 3d 20 28 73 69 7a 65 5f 74 29 18) >= (size_t)
10337 6e 42 75 66 20 29 7b 0a 20 20 20 20 72 65 74 75 nBuf ){. retu
10338 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
10339 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 . }.. do{.
1033a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1033b 28 6e 42 75 66 2d 31 38 2c 20 7a 42 75 66 2c 20 (nBuf-18, zBuf,
1033c 22 25 73 2f 22 53 51 4c 49 54 45 5f 54 45 4d 50 "%s/"SQLITE_TEMP
1033d 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 44 _FILE_PREFIX, zD
1033e 69 72 29 3b 0a 20 20 20 20 6a 20 3d 20 28 69 6e ir);. j = (in
1033f 74 29 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a t)strlen(zBuf);.
10340 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 sqlite3_rand
10341 6f 6d 6e 65 73 73 28 31 35 2c 20 26 7a 42 75 66 omness(15, &zBuf
10342 5b 6a 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d [j]);. for(i=
10343 30 3b 20 69 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b 0; i<15; i++, j+
10344 2b 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 6a +){. zBuf[j
10345 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 ] = (char)zChars
10346 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 [ ((unsigned cha
10347 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 r)zBuf[j])%(size
10348 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b of(zChars)-1) ];
10349 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b . }. zBuf[
1034a 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 42 75 66 j] = 0;. zBuf
1034b 5b 6a 2b 31 5d 20 3d 20 30 3b 0a 20 20 7d 77 68 [j+1] = 0;. }wh
1034c 69 6c 65 28 20 6f 73 41 63 63 65 73 73 28 7a 42 ile( osAccess(zB
1034d 75 66 2c 30 29 3d 3d 30 20 29 3b 0a 20 20 72 65 uf,0)==0 );. re
1034e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1034f 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e }..#if SQLITE_EN
10350 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
10351 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f LE && defined(__
10352 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20 52 APPLE__)./*.** R
10353 6f 75 74 69 6e 65 20 74 6f 20 74 72 61 6e 73 66 outine to transf
10354 6f 72 6d 20 61 20 75 6e 69 78 46 69 6c 65 20 69 orm a unixFile i
10355 6e 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f 63 6b nto a proxy-lock
10356 69 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0a 2a 2a ing unixFile..**
10357 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
10358 69 6e 20 74 68 65 20 70 72 6f 78 79 2d 6c 6f 63 in the proxy-loc
10359 6b 20 64 69 76 69 73 69 6f 6e 2c 20 62 75 74 20 k division, but
1035a 75 73 65 64 20 62 79 20 75 6e 69 78 4f 70 65 6e used by unixOpen
1035b 28 29 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f ().** if SQLITE_
1035c 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 PREFER_PROXY_LOC
1035d 4b 49 4e 47 20 69 73 20 64 65 66 69 6e 65 64 2e KING is defined.
1035e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
1035f 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 roxyTransformUni
10360 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 2a 2c xFile(unixFile*,
10361 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 const char*);.#
10362 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 endif../*.** Sea
10363 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73 65 rch for an unuse
10364 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f d file descripto
10365 72 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e 65 r that was opene
10366 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 d on the databas
10367 65 20 0a 2a 2a 20 66 69 6c 65 20 28 6e 6f 74 20 e .** file (not
10368 61 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 6d 61 73 a journal or mas
10369 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter-journal file
1036a 29 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 ) identified by
1036b 70 61 74 68 6e 61 6d 65 0a 2a 2a 20 7a 50 61 74 pathname.** zPat
1036c 68 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f 50 h with SQLITE_OP
1036d 45 4e 5f 58 58 58 20 66 6c 61 67 73 20 6d 61 74 EN_XXX flags mat
1036e 63 68 69 6e 67 20 74 68 6f 73 65 20 70 61 73 73 ching those pass
1036f 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
10370 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 .** argument to
10371 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a this function..*
10372 2a 0a 2a 2a 20 53 75 63 68 20 61 20 66 69 6c 65 *.** Such a file
10373 20 64 65 73 63 72 69 70 74 6f 72 20 6d 61 79 20 descriptor may
10374 65 78 69 73 74 20 69 66 20 61 20 64 61 74 61 62 exist if a datab
10375 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 ase connection w
10376 61 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 62 75 74 as closed.** but
10377 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 the associated
10378 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
10379 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6c 6f could not be clo
1037a 73 65 64 20 62 65 63 61 75 73 65 20 73 6f 6d 65 sed because some
1037b 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 20 64 .** other file d
1037c 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e 20 6f escriptor open o
1037d 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 n the same file
1037e 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 66 69 6c is holding a fil
1037f 65 2d 6c 6f 63 6b 2e 0a 2a 2a 20 52 65 66 65 72 e-lock..** Refer
10380 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 to comments in
10381 74 68 65 20 75 6e 69 78 43 6c 6f 73 65 28 29 20 the unixClose()
10382 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 74 68 65 function and the
10383 20 6c 65 6e 67 74 68 79 20 63 6f 6d 6d 65 6e 74 lengthy comment
10384 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 20 22 .** describing "
10385 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c Posix Advisory L
10386 6f 63 6b 69 6e 67 22 20 61 74 20 74 68 65 20 73 ocking" at the s
10387 74 61 72 74 20 6f 66 20 74 68 69 73 20 66 69 6c tart of this fil
10388 65 20 66 6f 72 20 0a 2a 2a 20 66 75 72 74 68 65 e for .** furthe
10389 72 20 64 65 74 61 69 6c 73 2e 20 41 6c 73 6f 2c r details. Also,
1038a 20 74 69 63 6b 65 74 20 23 34 30 31 38 2e 0a 2a ticket #4018..*
1038b 2a 0a 2a 2a 20 49 66 20 61 20 73 75 69 74 61 62 *.** If a suitab
1038c 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 le file descript
1038d 6f 72 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 or is found, the
1038e 6e 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 n it is returned
1038f 2e 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 63 68 20 . If no.** such
10390 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
10391 69 73 20 6c 6f 63 61 74 65 64 2c 20 2d 31 20 69 is located, -1 i
10392 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 s returned..*/.s
10393 74 61 74 69 63 20 55 6e 69 78 55 6e 75 73 65 64 tatic UnixUnused
10394 46 64 20 2a 66 69 6e 64 52 65 75 73 61 62 6c 65 Fd *findReusable
10395 46 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a Fd(const char *z
10396 50 61 74 68 2c 20 69 6e 74 20 66 6c 61 67 73 29 Path, int flags)
10397 7b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 {. UnixUnusedFd
10398 20 2a 70 55 6e 75 73 65 64 20 3d 20 30 3b 0a 0a *pUnused = 0;..
10399 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 65 61 72 /* Do not sear
1039a 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73 65 64 ch for an unused
1039b 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
1039c 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 4e 6f 74 on vxworks. Not
1039d 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 76 78 because. ** vx
1039e 77 6f 72 6b 73 20 77 6f 75 6c 64 20 6e 6f 74 20 works would not
1039f 62 65 6e 65 66 69 74 20 66 72 6f 6d 20 74 68 65 benefit from the
103a0 20 63 68 61 6e 67 65 20 28 69 74 20 6d 69 67 68 change (it migh
103a1 74 2c 20 77 65 27 72 65 20 6e 6f 74 20 73 75 72 t, we're not sur
103a2 65 29 2c 0a 20 20 2a 2a 20 62 75 74 20 62 65 63 e),. ** but bec
103a3 61 75 73 65 20 6e 6f 20 77 61 79 20 74 6f 20 74 ause no way to t
103a4 65 73 74 20 69 74 20 69 73 20 63 75 72 72 65 6e est it is curren
103a5 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e 20 49 tly available. I
103a6 74 20 69 73 20 62 65 74 74 65 72 20 0a 20 20 2a t is better . *
103a7 2a 20 6e 6f 74 20 74 6f 20 72 69 73 6b 20 62 72 * not to risk br
103a8 65 61 6b 69 6e 67 20 76 78 77 6f 72 6b 73 20 73 eaking vxworks s
103a9 75 70 70 6f 72 74 20 66 6f 72 20 74 68 65 20 73 upport for the s
103aa 61 6b 65 20 6f 66 20 73 75 63 68 20 61 6e 20 6f ake of such an o
103ab 62 73 63 75 72 65 20 0a 20 20 2a 2a 20 66 65 61 bscure . ** fea
103ac 74 75 72 65 2e 20 20 2a 2f 0a 23 69 66 20 21 4f ture. */.#if !O
103ad 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 S_VXWORKS. stru
103ae 63 74 20 73 74 61 74 20 73 53 74 61 74 3b 20 20 ct stat sStat;
103af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
103b0 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 73 /* Results of s
103b1 74 61 74 28 29 20 63 61 6c 6c 20 2a 2f 0a 0a 20 tat() call */..
103b2 20 2f 2a 20 41 20 73 74 61 74 28 29 20 63 61 6c /* A stat() cal
103b3 6c 20 6d 61 79 20 66 61 69 6c 20 66 6f 72 20 76 l may fail for v
103b4 61 72 69 6f 75 73 20 72 65 61 73 6f 6e 73 2e 20 arious reasons.
103b5 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c If this happens,
103b6 20 69 74 20 69 73 0a 20 20 2a 2a 20 61 6c 6d 6f it is. ** almo
103b7 73 74 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 st certain that
103b8 61 6e 20 6f 70 65 6e 28 29 20 63 61 6c 6c 20 6f an open() call o
103b9 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74 68 20 n the same path
103ba 77 69 6c 6c 20 61 6c 73 6f 20 66 61 69 6c 2e 0a will also fail..
103bb 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 65 ** For this re
103bc 61 73 6f 6e 2c 20 69 66 20 61 6e 20 65 72 72 6f ason, if an erro
103bd 72 20 6f 63 63 75 72 73 20 69 6e 20 74 68 65 20 r occurs in the
103be 73 74 61 74 28 29 20 63 61 6c 6c 20 68 65 72 65 stat() call here
103bf 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 69 67 6e , it is. ** ign
103c0 6f 72 65 64 20 61 6e 64 20 2d 31 20 69 73 20 72 ored and -1 is r
103c1 65 74 75 72 6e 65 64 2e 20 54 68 65 20 63 61 6c eturned. The cal
103c2 6c 65 72 20 77 69 6c 6c 20 74 72 79 20 74 6f 20 ler will try to
103c3 6f 70 65 6e 20 61 20 6e 65 77 20 66 69 6c 65 0a open a new file.
103c4 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 ** descriptor
103c5 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74 68 on the same path
103c6 2c 20 66 61 69 6c 2c 20 61 6e 64 20 72 65 74 75 , fail, and retu
103c7 72 6e 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 53 rn an error to S
103c8 51 4c 69 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a QLite.. **. **
103c9 20 45 76 65 6e 20 69 66 20 61 20 73 75 62 73 65 Even if a subse
103ca 71 75 65 6e 74 20 6f 70 65 6e 28 29 20 63 61 6c quent open() cal
103cb 6c 20 64 6f 65 73 20 73 75 63 63 65 65 64 2c 20 l does succeed,
103cc 74 68 65 20 63 6f 6e 73 65 71 75 65 6e 63 65 73 the consequences
103cd 20 6f 66 0a 20 20 2a 2a 20 6e 6f 74 20 73 65 61 of. ** not sea
103ce 72 63 68 69 6e 67 20 66 6f 72 20 61 20 72 65 73 rching for a res
103cf 75 73 61 62 6c 65 20 66 69 6c 65 20 64 65 73 63 usable file desc
103d0 72 69 70 74 6f 72 20 61 72 65 20 6e 6f 74 20 64 riptor are not d
103d1 69 72 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 30 ire. */. if( 0
103d2 3d 3d 6f 73 53 74 61 74 28 7a 50 61 74 68 2c 20 ==osStat(zPath,
103d3 26 73 53 74 61 74 29 20 29 7b 0a 20 20 20 20 75 &sStat) ){. u
103d4 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 nixInodeInfo *pI
103d5 6e 6f 64 65 3b 0a 0a 20 20 20 20 75 6e 69 78 45 node;.. unixE
103d6 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
103d7 20 70 49 6e 6f 64 65 20 3d 20 69 6e 6f 64 65 4c pInode = inodeL
103d8 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 ist;. while(
103d9 70 49 6e 6f 64 65 20 26 26 20 28 70 49 6e 6f 64 pInode && (pInod
103da 65 2d 3e 66 69 6c 65 49 64 2e 64 65 76 21 3d 73 e->fileId.dev!=s
103db 53 74 61 74 2e 73 74 5f 64 65 76 0a 20 20 20 20 Stat.st_dev.
103dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
103dd 20 7c 7c 20 70 49 6e 6f 64 65 2d 3e 66 69 6c 65 || pInode->file
103de 49 64 2e 69 6e 6f 21 3d 73 53 74 61 74 2e 73 74 Id.ino!=sStat.st
103df 5f 69 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20 _ino) ){.
103e0 70 49 6e 6f 64 65 20 3d 20 70 49 6e 6f 64 65 2d pInode = pInode-
103e1 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 >pNext;. }.
103e2 20 20 69 66 28 20 70 49 6e 6f 64 65 20 29 7b 0a if( pInode ){.
103e3 20 20 20 20 20 20 55 6e 69 78 55 6e 75 73 65 64 UnixUnused
103e4 46 64 20 2a 2a 70 70 3b 0a 20 20 20 20 20 20 66 Fd **pp;. f
103e5 6f 72 28 70 70 3d 26 70 49 6e 6f 64 65 2d 3e 70 or(pp=&pInode->p
103e6 55 6e 75 73 65 64 3b 20 2a 70 70 20 26 26 20 28 Unused; *pp && (
103e7 2a 70 70 29 2d 3e 66 6c 61 67 73 21 3d 66 6c 61 *pp)->flags!=fla
103e8 67 73 3b 20 70 70 3d 26 28 28 2a 70 70 29 2d 3e gs; pp=&((*pp)->
103e9 70 4e 65 78 74 29 29 3b 0a 20 20 20 20 20 20 70 pNext));. p
103ea 55 6e 75 73 65 64 20 3d 20 2a 70 70 3b 0a 20 20 Unused = *pp;.
103eb 20 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 20 if( pUnused
103ec 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 70 20 3d ){. *pp =
103ed 20 70 55 6e 75 73 65 64 2d 3e 70 4e 65 78 74 3b pUnused->pNext;
103ee 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
103ef 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 unixLeaveMute
103f0 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 x();. }.#endif
103f1 20 20 20 2f 2a 20 69 66 20 21 4f 53 5f 56 58 57 /* if !OS_VXW
103f2 4f 52 4b 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e ORKS */. return
103f3 20 70 55 6e 75 73 65 64 3b 0a 7d 0a 0a 2f 2a 0a pUnused;.}../*.
103f4 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
103f5 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 75 6e is called by un
103f6 69 78 4f 70 65 6e 28 29 20 74 6f 20 64 65 74 65 ixOpen() to dete
103f7 72 6d 69 6e 65 20 74 68 65 20 75 6e 69 78 20 70 rmine the unix p
103f8 65 72 6d 69 73 73 69 6f 6e 73 0a 2a 2a 20 74 6f ermissions.** to
103f9 20 63 72 65 61 74 65 20 6e 65 77 20 66 69 6c 65 create new file
103fa 73 20 77 69 74 68 2e 20 49 66 20 6e 6f 20 65 72 s with. If no er
103fb 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e ror occurs, then
103fc 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
103fd 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 61 20 turned.** and a
103fe 76 61 6c 75 65 20 73 75 69 74 61 62 6c 65 20 66 value suitable f
103ff 6f 72 20 70 61 73 73 69 6e 67 20 61 73 20 74 68 or passing as th
10400 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 e third argument
10401 20 74 6f 20 6f 70 65 6e 28 32 29 20 69 73 0a 2a to open(2) is.*
10402 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 4d * written to *pM
10403 6f 64 65 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 ode. If an IO er
10404 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 53 ror occurs, an S
10405 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 QLite error code
10406 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 is .** returned
10407 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f and the value o
10408 66 20 2a 70 4d 6f 64 65 20 69 73 20 6e 6f 74 20 f *pMode is not
10409 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 modified..**.**
1040a 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 20 63 61 In most cases ca
1040b 73 65 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e ses, this routin
1040c 65 20 73 65 74 73 20 2a 70 4d 6f 64 65 20 74 6f e sets *pMode to
1040d 20 30 2c 20 77 68 69 63 68 20 77 69 6c 6c 20 62 0, which will b
1040e 65 63 6f 6d 65 0a 2a 2a 20 61 6e 20 69 6e 64 69 ecome.** an indi
1040f 63 61 74 69 6f 6e 20 74 6f 20 72 6f 62 75 73 74 cation to robust
10410 5f 6f 70 65 6e 28 29 20 74 6f 20 63 72 65 61 74 _open() to creat
10411 65 20 74 68 65 20 66 69 6c 65 20 75 73 69 6e 67 e the file using
10412 0a 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 .** SQLITE_DEFAU
10413 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 LT_FILE_PERMISSI
10414 4f 4e 53 20 61 64 6a 75 73 74 65 64 20 62 79 20 ONS adjusted by
10415 74 68 65 20 75 6d 61 73 6b 2e 0a 2a 2a 20 42 75 the umask..** Bu
10416 74 20 69 66 20 74 68 65 20 66 69 6c 65 20 62 65 t if the file be
10417 69 6e 67 20 6f 70 65 6e 65 64 20 69 73 20 61 20 ing opened is a
10418 57 41 4c 20 6f 72 20 72 65 67 75 6c 61 72 20 6a WAL or regular j
10419 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 ournal file, the
1041a 6e 20 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 n .** this funct
1041b 69 6f 6e 20 71 75 65 72 69 65 73 20 74 68 65 20 ion queries the
1041c 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 6f 72 20 file-system for
1041d 74 68 65 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 the permissions
1041e 6f 6e 20 74 68 65 20 0a 2a 2a 20 63 6f 72 72 65 on the .** corre
1041f 73 70 6f 6e 64 69 6e 67 20 64 61 74 61 62 61 73 sponding databas
10420 65 20 66 69 6c 65 20 61 6e 64 20 73 65 74 73 20 e file and sets
10421 2a 70 4d 6f 64 65 20 74 6f 20 74 68 69 73 20 76 *pMode to this v
10422 61 6c 75 65 2e 20 57 68 65 6e 65 76 65 72 20 0a alue. Whenever .
10423 2a 2a 20 70 6f 73 73 69 62 6c 65 2c 20 57 41 4c ** possible, WAL
10424 20 61 6e 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c and journal fil
10425 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20 75 es are created u
10426 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 70 65 sing the same pe
10427 72 6d 69 73 73 69 6f 6e 73 20 0a 2a 2a 20 61 73 rmissions .** as
10428 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 the associated
10429 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
1042a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 *.** If the SQLI
1042b 54 45 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41 TE_ENABLE_8_3_NA
1042c 4d 45 53 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e MES option is en
1042d 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 0a abled, then the.
1042e 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 ** original file
1042f 6e 61 6d 65 20 69 73 20 75 6e 61 76 61 69 6c 61 name is unavaila
10430 62 6c 65 2e 20 20 42 75 74 20 38 5f 33 5f 4e 41 ble. But 8_3_NA
10431 4d 45 53 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 MES is only used
10432 20 66 6f 72 0a 2a 2a 20 46 41 54 20 66 69 6c 65 for.** FAT file
10433 73 79 73 74 65 6d 73 20 61 6e 64 20 70 65 72 6d systems and perm
10434 69 73 73 69 6f 6e 73 20 64 6f 20 6e 6f 74 20 6d issions do not m
10435 61 74 74 65 72 20 74 68 65 72 65 2c 20 73 6f 20 atter there, so
10436 6a 75 73 74 20 75 73 65 0a 2a 2a 20 74 68 65 20 just use.** the
10437 64 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 default permissi
10438 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ons..*/.static i
10439 6e 74 20 66 69 6e 64 43 72 65 61 74 65 46 69 6c nt findCreateFil
1043a 65 4d 6f 64 65 28 0a 20 20 63 6f 6e 73 74 20 63 eMode(. const c
1043b 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 har *zPath,
1043c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 74 68 /* Path
1043d 20 6f 66 20 66 69 6c 65 20 28 70 6f 73 73 69 62 of file (possib
1043e 6c 79 29 20 62 65 69 6e 67 20 63 72 65 61 74 65 ly) being create
1043f 64 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 d */. int flags
10440 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
10441 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
10442 70 61 73 73 65 64 20 61 73 20 34 74 68 20 61 72 passed as 4th ar
10443 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e 28 gument to xOpen(
10444 29 20 2a 2f 0a 20 20 6d 6f 64 65 5f 74 20 2a 70 ) */. mode_t *p
10445 4d 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 Mode,
10446 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 /* OUT: P
10447 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 6f 70 ermissions to op
10448 65 6e 20 66 69 6c 65 20 77 69 74 68 20 2a 2f 0a en file with */.
10449 20 20 75 69 64 5f 74 20 2a 70 55 69 64 2c 20 20 uid_t *pUid,
1044a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1044b 20 20 2f 2a 20 4f 55 54 3a 20 75 69 64 20 74 6f /* OUT: uid to
1044c 20 73 65 74 20 6f 6e 20 74 68 65 20 66 69 6c 65 set on the file
1044d 20 2a 2f 0a 20 20 67 69 64 5f 74 20 2a 70 47 69 */. gid_t *pGi
1044e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
1044f 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 67 69 /* OUT: gi
10450 64 20 74 6f 20 73 65 74 20 6f 6e 20 74 68 65 20 d to set on the
10451 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 file */.){. int
10452 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
10453 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10454 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 Return Code */.
10455 20 2a 70 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 2a *pMode = 0;. *
10456 70 55 69 64 20 3d 20 30 3b 0a 20 20 2a 70 47 69 pUid = 0;. *pGi
10457 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 66 6c 61 d = 0;. if( fla
10458 67 73 20 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 gs & (SQLITE_OPE
10459 4e 5f 57 41 4c 7c 53 51 4c 49 54 45 5f 4f 50 45 N_WAL|SQLITE_OPE
1045a 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 20 N_MAIN_JOURNAL)
1045b 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 44 62 5b ){. char zDb[
1045c 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b MAX_PATHNAME+1];
1045d 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
1045e 20 66 69 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20 file path */.
1045f 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20 20 20 int nDb;
10460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10461 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 61 6c /* Number of val
10462 69 64 20 62 79 74 65 73 20 69 6e 20 7a 44 62 20 id bytes in zDb
10463 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 */. struct st
10464 61 74 20 73 53 74 61 74 3b 20 20 20 20 20 20 20 at sStat;
10465 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 6f /* Output o
10466 66 20 73 74 61 74 28 29 20 6f 6e 20 64 61 74 61 f stat() on data
10467 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 base file */..
10468 20 20 2f 2a 20 7a 50 61 74 68 20 69 73 20 61 20 /* zPath is a
10469 70 61 74 68 20 74 6f 20 61 20 57 41 4c 20 6f 72 path to a WAL or
1046a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 journal file. T
1046b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
1046c 63 6b 20 64 65 72 69 76 65 73 0a 20 20 20 20 2a ck derives. *
1046d 2a 20 74 68 65 20 70 61 74 68 20 74 6f 20 74 68 * the path to th
1046e 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 e associated dat
1046f 61 62 61 73 65 20 66 69 6c 65 20 66 72 6f 6d 20 abase file from
10470 7a 50 61 74 68 2e 20 54 68 69 73 20 62 6c 6f 63 zPath. This bloc
10471 6b 20 68 61 6e 64 6c 65 73 0a 20 20 20 20 2a 2a k handles. **
10472 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e the following n
10473 61 6d 69 6e 67 20 63 6f 6e 76 65 6e 74 69 6f 6e aming convention
10474 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a s:. **. **
10475 20 20 20 22 3c 70 61 74 68 20 74 6f 20 64 62 3e "<path to db>
10476 2d 6a 6f 75 72 6e 61 6c 22 0a 20 20 20 20 2a 2a -journal". **
10477 20 20 20 22 3c 70 61 74 68 20 74 6f 20 64 62 3e "<path to db>
10478 2d 77 61 6c 22 0a 20 20 20 20 2a 2a 20 20 20 22 -wal". ** "
10479 3c 70 61 74 68 20 74 6f 20 64 62 3e 2d 6a 6f 75 <path to db>-jou
1047a 72 6e 61 6c 4e 4e 22 0a 20 20 20 20 2a 2a 20 20 rnalNN". **
1047b 20 22 3c 70 61 74 68 20 74 6f 20 64 62 3e 2d 77 "<path to db>-w
1047c 61 6c 4e 4e 22 0a 20 20 20 20 2a 2a 0a 20 20 20 alNN". **.
1047d 20 2a 2a 20 77 68 65 72 65 20 4e 4e 20 69 73 20 ** where NN is
1047e 61 20 64 65 63 69 6d 61 6c 20 6e 75 6d 62 65 72 a decimal number
1047f 2e 20 54 68 65 20 4e 4e 20 6e 61 6d 69 6e 67 20 . The NN naming
10480 73 63 68 65 6d 65 73 20 61 72 65 20 0a 20 20 20 schemes are .
10481 20 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 ** used by the
10482 74 65 73 74 5f 6d 75 6c 74 69 70 6c 65 78 2e 63 test_multiplex.c
10483 20 6d 6f 64 75 6c 65 2e 0a 20 20 20 20 2a 2f 0a module.. */.
10484 20 20 20 20 6e 44 62 20 3d 20 73 71 6c 69 74 65 nDb = sqlite
10485 33 53 74 72 6c 65 6e 33 30 28 7a 50 61 74 68 29 3Strlen30(zPath)
10486 20 2d 20 31 3b 20 0a 23 69 66 64 65 66 20 53 51 - 1; .#ifdef SQ
10487 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f LITE_ENABLE_8_3_
10488 4e 41 4d 45 53 0a 20 20 20 20 77 68 69 6c 65 28 NAMES. while(
10489 20 6e 44 62 3e 30 20 26 26 20 73 71 6c 69 74 65 nDb>0 && sqlite
1048a 33 49 73 61 6c 6e 75 6d 28 7a 50 61 74 68 5b 6e 3Isalnum(zPath[n
1048b 44 62 5d 29 20 29 20 6e 44 62 2d 2d 3b 0a 20 20 Db]) ) nDb--;.
1048c 20 20 69 66 28 20 6e 44 62 3d 3d 30 20 7c 7c 20 if( nDb==0 ||
1048d 7a 50 61 74 68 5b 6e 44 62 5d 21 3d 27 2d 27 20 zPath[nDb]!='-'
1048e 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
1048f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 20 20 77 68 OK;.#else. wh
10490 69 6c 65 28 20 7a 50 61 74 68 5b 6e 44 62 5d 21 ile( zPath[nDb]!
10491 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 61 73 ='-' ){. as
10492 73 65 72 74 28 20 6e 44 62 3e 30 20 29 3b 0a 20 sert( nDb>0 );.
10493 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 50 61 assert( zPa
10494 74 68 5b 6e 44 62 5d 21 3d 27 5c 6e 27 20 29 3b th[nDb]!='\n' );
10495 0a 20 20 20 20 20 20 6e 44 62 2d 2d 3b 0a 20 20 . nDb--;.
10496 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 6d }.#endif. m
10497 65 6d 63 70 79 28 7a 44 62 2c 20 7a 50 61 74 68 emcpy(zDb, zPath
10498 2c 20 6e 44 62 29 3b 0a 20 20 20 20 7a 44 62 5b , nDb);. zDb[
10499 6e 44 62 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 nDb] = '\0';..
1049a 20 20 69 66 28 20 30 3d 3d 6f 73 53 74 61 74 28 if( 0==osStat(
1049b 7a 44 62 2c 20 26 73 53 74 61 74 29 20 29 7b 0a zDb, &sStat) ){.
1049c 20 20 20 20 20 20 2a 70 4d 6f 64 65 20 3d 20 73 *pMode = s
1049d 53 74 61 74 2e 73 74 5f 6d 6f 64 65 20 26 20 30 Stat.st_mode & 0
1049e 37 37 37 3b 0a 20 20 20 20 20 20 2a 70 55 69 64 777;. *pUid
1049f 20 3d 20 73 53 74 61 74 2e 73 74 5f 75 69 64 3b = sStat.st_uid;
104a0 0a 20 20 20 20 20 20 2a 70 47 69 64 20 3d 20 73 . *pGid = s
104a1 53 74 61 74 2e 73 74 5f 67 69 64 3b 0a 20 20 20 Stat.st_gid;.
104a2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
104a3 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
104a4 46 53 54 41 54 3b 0a 20 20 20 20 7d 0a 20 20 7d FSTAT;. }. }
104a5 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 else if( flags &
104a6 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c SQLITE_OPEN_DEL
104a7 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 20 20 ETEONCLOSE ){.
104a8 20 20 2a 70 4d 6f 64 65 20 3d 20 30 36 30 30 3b *pMode = 0600;
104a9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
104aa 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 ;.}../*.** Open
104ab 74 68 65 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a the file zPath..
104ac 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c ** .** Previousl
104ad 79 2c 20 74 68 65 20 53 51 4c 69 74 65 20 4f 53 y, the SQLite OS
104ae 20 6c 61 79 65 72 20 75 73 65 64 20 74 68 72 65 layer used thre
104af 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70 e functions in p
104b0 6c 61 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 lace of this.**
104b1 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 one:.**.** s
104b2 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 qlite3OsOpenRead
104b3 57 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20 Write();.**
104b4 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 sqlite3OsOpenRea
104b5 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20 dOnly();.**
104b6 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 sqlite3OsOpenExc
104b7 6c 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20 lusive();.**.**
104b8 54 68 65 73 65 20 63 61 6c 6c 73 20 63 6f 72 72 These calls corr
104b9 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f espond to the fo
104ba 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 llowing combinat
104bb 69 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a ions of flags:.*
104bc 2a 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 72 69 *.** ReadWri
104bd 74 65 28 29 20 2d 3e 20 20 20 20 20 28 52 45 41 te() -> (REA
104be 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 29 DWRITE | CREATE)
104bf 0a 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e 6c 79 .** ReadOnly
104c0 28 29 20 20 2d 3e 20 20 20 20 20 28 52 45 41 44 () -> (READ
104c1 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70 ONLY) .** Op
104c2 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 2d 3e enExclusive() ->
104c3 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52 (READWRITE | CR
104c4 45 41 54 45 20 7c 20 45 58 43 4c 55 53 49 56 45 EATE | EXCLUSIVE
104c5 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 ).**.** The old
104c6 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 OpenExclusive()
104c7 61 63 63 65 70 74 65 64 20 61 20 62 6f 6f 6c 65 accepted a boole
104c8 61 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 22 64 an argument - "d
104c9 65 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74 elFlag". If.** t
104ca 72 75 65 2c 20 74 68 65 20 66 69 6c 65 20 77 61 rue, the file wa
104cb 73 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 s configured to
104cc 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 be automatically
104cd 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 deleted when th
104ce 65 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65 e.** file handle
104cf 20 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 68 69 closed. To achi
104d0 65 76 65 20 74 68 65 20 73 61 6d 65 20 65 66 66 eve the same eff
104d1 65 63 74 20 75 73 69 6e 67 20 74 68 69 73 20 6e ect using this n
104d2 65 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 ew .** interface
104d3 2c 20 61 64 64 20 74 68 65 20 44 45 4c 45 54 45 , add the DELETE
104d4 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20 ONCLOSE flag to
104d5 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 20 those specified
104d6 61 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70 above for .** Op
104d7 65 6e 45 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a enExclusive()..*
104d8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
104d9 78 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 xOpen(. sqlite3
104da 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
104db 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 /* The VFS
104dc 20 66 6f 72 20 77 68 69 63 68 20 74 68 69 73 20 for which this
104dd 69 73 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 is the xOpen met
104de 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 hod */. const c
104df 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 har *zPath,
104e0 20 20 20 20 20 20 2f 2a 20 50 61 74 68 6e 61 6d /* Pathnam
104e1 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 e of file to be
104e2 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 opened */. sqli
104e3 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c te3_file *pFile,
104e4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
104e5 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
104e6 74 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20 to be filled in
104e7 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 */. int flags,
104e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
104e9 20 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61 67 73 /* Input flags
104ea 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 to control the
104eb 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 opening */. int
104ec 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 *pOutFlags
104ed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 /* Out
104ee 70 75 74 20 66 6c 61 67 73 20 72 65 74 75 72 6e put flags return
104ef 65 64 20 74 6f 20 53 51 4c 69 74 65 20 63 6f 72 ed to SQLite cor
104f0 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 46 69 e */.){. unixFi
104f1 6c 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69 6c le *p = (unixFil
104f2 65 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69 6e 74 e *)pFile;. int
104f3 20 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 fd = -1;
104f4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
104f5 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72 ile descriptor r
104f6 65 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28 eturned by open(
104f7 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46 ) */. int openF
104f8 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20 lags = 0;
104f9 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 /* Flags t
104fa 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28 29 o pass to open()
104fb 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65 20 */. int eType
104fc 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46 46 = flags&0xFFFFFF
104fd 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 00; /* Type of
104fe 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a file to open */.
104ff 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20 20 int noLock;
10500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10501 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 /* True to omit
10502 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 locking primiti
10503 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 ves */. int rc
10504 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
10505 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 /* Funct
10506 69 6f 6e 20 52 65 74 75 72 6e 20 43 6f 64 65 20 ion Return Code
10507 2a 2f 0a 20 20 69 6e 74 20 63 74 72 6c 46 6c 61 */. int ctrlFla
10508 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 gs = 0;
10509 20 20 20 20 2f 2a 20 55 4e 49 58 46 49 4c 45 5f /* UNIXFILE_
1050a 2a 20 66 6c 61 67 73 20 2a 2f 0a 0a 20 20 69 6e * flags */.. in
1050b 74 20 69 73 45 78 63 6c 75 73 69 76 65 20 20 3d t isExclusive =
1050c 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 (flags & SQLITE
1050d 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 29 _OPEN_EXCLUSIVE)
1050e 3b 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 ;. int isDelete
1050f 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20 = (flags &
10510 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 SQLITE_OPEN_DELE
10511 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a 20 20 69 6e TEONCLOSE);. in
10512 74 20 69 73 43 72 65 61 74 65 20 20 20 20 20 3d t isCreate =
10513 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 (flags & SQLITE
10514 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 _OPEN_CREATE);.
10515 20 69 6e 74 20 69 73 52 65 61 64 6f 6e 6c 79 20 int isReadonly
10516 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c = (flags & SQL
10517 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
10518 59 29 3b 0a 20 20 69 6e 74 20 69 73 52 65 61 64 Y);. int isRead
10519 57 72 69 74 65 20 20 3d 20 28 66 6c 61 67 73 20 Write = (flags
1051a 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 & SQLITE_OPEN_RE
1051b 41 44 57 52 49 54 45 29 3b 0a 23 69 66 20 53 51 ADWRITE);.#if SQ
1051c 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
1051d 49 4e 47 5f 53 54 59 4c 45 0a 20 20 69 6e 74 20 ING_STYLE. int
1051e 69 73 41 75 74 6f 50 72 6f 78 79 20 20 3d 20 28 isAutoProxy = (
1051f 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
10520 50 45 4e 5f 41 55 54 4f 50 52 4f 58 59 29 3b 0a PEN_AUTOPROXY);.
10521 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e #endif.#if defin
10522 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 7c 7c ed(__APPLE__) ||
10523 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
10524 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 73 OCKING_STYLE. s
10525 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73 49 truct statfs fsI
10526 6e 66 6f 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f nfo;.#endif.. /
10527 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61 20 * If creating a
10528 6d 61 73 74 65 72 20 6f 72 20 6d 61 69 6e 2d 66 master or main-f
10529 69 6c 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 69 ile journal, thi
1052a 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 s function will
1052b 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 66 69 6c 65 open. ** a file
1052c 2d 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 74 -descriptor on t
1052d 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 6f 6f he directory too
1052e 2e 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 . The first time
1052f 20 75 6e 69 78 53 79 6e 63 28 29 0a 20 20 2a 2a unixSync(). **
10530 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 64 is called the d
10531 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 irectory file de
10532 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 scriptor will be
10533 20 66 73 79 6e 63 28 29 65 64 20 61 6e 64 20 63 fsync()ed and c
10534 6c 6f 73 65 28 29 64 2e 0a 20 20 2a 2f 0a 20 20 lose()d.. */.
10535 69 6e 74 20 73 79 6e 63 44 69 72 20 3d 20 28 69 int syncDir = (i
10536 73 43 72 65 61 74 65 20 26 26 20 28 0a 20 20 20 sCreate && (.
10537 20 20 20 20 20 65 54 79 70 65 3d 3d 53 51 4c 49 eType==SQLI
10538 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a TE_OPEN_MASTER_J
10539 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 7c 7c 20 OURNAL . ||
1053a 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 eType==SQLITE_OP
1053b 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 EN_MAIN_JOURNAL
1053c 0a 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d . || eType==
1053d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 57 41 4c 0a SQLITE_OPEN_WAL.
1053e 20 20 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 ));.. /* If a
1053f 72 67 75 6d 65 6e 74 20 7a 50 61 74 68 20 69 73 rgument zPath is
10540 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
10541 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
10542 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 6f 70 s required to op
10543 65 6e 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f 72 en. ** a tempor
10544 61 72 79 20 66 69 6c 65 2e 20 55 73 65 20 74 68 ary file. Use th
10545 69 73 20 62 75 66 66 65 72 20 74 6f 20 73 74 6f is buffer to sto
10546 72 65 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 re the file name
10547 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 in.. */. char
10548 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 zTmpname[MAX_PA
10549 54 48 4e 41 4d 45 2b 32 5d 3b 0a 20 20 63 6f 6e THNAME+2];. con
1054a 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d st char *zName =
1054b 20 7a 50 61 74 68 3b 0a 0a 20 20 2f 2a 20 43 68 zPath;.. /* Ch
1054c 65 63 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e eck the followin
1054d 67 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 g statements are
1054e 20 74 72 75 65 3a 20 0a 20 20 2a 2a 0a 20 20 2a true: . **. *
1054f 2a 20 20 20 28 61 29 20 45 78 61 63 74 6c 79 20 * (a) Exactly
10550 6f 6e 65 20 6f 66 20 74 68 65 20 52 45 41 44 57 one of the READW
10551 52 49 54 45 20 61 6e 64 20 52 45 41 44 4f 4e 4c RITE and READONL
10552 59 20 66 6c 61 67 73 20 6d 75 73 74 20 62 65 20 Y flags must be
10553 73 65 74 2c 20 61 6e 64 20 0a 20 20 2a 2a 20 20 set, and . **
10554 20 28 62 29 20 69 66 20 43 52 45 41 54 45 20 69 (b) if CREATE i
10555 73 20 73 65 74 2c 20 74 68 65 6e 20 52 45 41 44 s set, then READ
10556 57 52 49 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 WRITE must also
10557 62 65 20 73 65 74 2c 20 61 6e 64 0a 20 20 2a 2a be set, and. **
10558 20 20 20 28 63 29 20 69 66 20 45 58 43 4c 55 53 (c) if EXCLUS
10559 49 56 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e IVE is set, then
1055a 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73 CREATE must als
1055b 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2a 20 20 o be set.. **
1055c 20 28 64 29 20 69 66 20 44 45 4c 45 54 45 4f 4e (d) if DELETEON
1055d 43 4c 4f 53 45 20 69 73 20 73 65 74 2c 20 74 68 CLOSE is set, th
1055e 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 en CREATE must a
1055f 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2f lso be set.. */
10560 0a 20 20 61 73 73 65 72 74 28 28 69 73 52 65 61 . assert((isRea
10561 64 6f 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73 52 65 donly==0 || isRe
10562 61 64 57 72 69 74 65 3d 3d 30 29 20 26 26 20 28 adWrite==0) && (
10563 69 73 52 65 61 64 57 72 69 74 65 20 7c 7c 20 69 isReadWrite || i
10564 73 52 65 61 64 6f 6e 6c 79 29 29 3b 0a 20 20 61 sReadonly));. a
10565 73 73 65 72 74 28 69 73 43 72 65 61 74 65 3d 3d ssert(isCreate==
10566 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 0 || isReadWrite
10567 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 45 78 );. assert(isEx
10568 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20 69 73 clusive==0 || is
10569 43 72 65 61 74 65 29 3b 0a 20 20 61 73 73 65 72 Create);. asser
1056a 74 28 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c 7c t(isDelete==0 ||
1056b 20 69 73 43 72 65 61 74 65 29 3b 0a 0a 20 20 2f isCreate);.. /
1056c 2a 20 54 68 65 20 6d 61 69 6e 20 44 42 2c 20 6d * The main DB, m
1056d 61 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 57 41 4c ain journal, WAL
1056e 20 66 69 6c 65 20 61 6e 64 20 6d 61 73 74 65 72 file and master
1056f 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 journal are nev
10570 65 72 20 0a 20 20 2a 2a 20 61 75 74 6f 6d 61 74 er . ** automat
10571 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 2e 20 ically deleted.
10572 4e 6f 72 20 61 72 65 20 74 68 65 79 20 65 76 65 Nor are they eve
10573 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 r temporary file
10574 73 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 s. */. assert(
10575 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a (!isDelete && z
10576 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d Name) || eType!=
10577 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
10578 5f 44 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 _DB );. assert(
10579 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a (!isDelete && z
1057a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d Name) || eType!=
1057b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
1057c 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a 20 20 61 73 _JOURNAL );. as
1057d 73 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 65 sert( (!isDelete
1057e 20 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 && zName) || eT
1057f 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype!=SQLITE_OPEN
10580 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 _MASTER_JOURNAL
10581 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 21 69 );. assert( (!i
10582 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 sDelete && zName
10583 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 ) || eType!=SQLI
10584 54 45 5f 4f 50 45 4e 5f 57 41 4c 20 29 3b 0a 0a TE_OPEN_WAL );..
10585 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 /* Assert that
10586 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 the upper layer
10587 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 20 has set one of
10588 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65 22 20 the "file-type"
10589 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65 flags. */. asse
1058a 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 rt( eType==SQLIT
1058b 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 E_OPEN_MAIN_DB
1058c 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 || eType==SQ
1058d 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 LITE_OPEN_TEMP_D
1058e 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 B . || eTy
1058f 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe==SQLITE_OPEN_
10590 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 MAIN_JOURNAL ||
10591 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 eType==SQLITE_OP
10592 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 EN_TEMP_JOURNAL
10593 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 . || eType
10594 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 ==SQLITE_OPEN_SU
10595 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54 BJOURNAL || eT
10596 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
10597 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 _MASTER_JOURNAL
10598 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 . || eType
10599 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 ==SQLITE_OPEN_TR
1059a 41 4e 53 49 45 4e 54 5f 44 42 20 7c 7c 20 65 54 ANSIENT_DB || eT
1059b 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
1059c 5f 57 41 4c 0a 20 20 29 3b 0a 0a 20 20 6d 65 6d _WAL. );.. mem
1059d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 set(p, 0, sizeof
1059e 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 (unixFile));..
1059f 69 66 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 if( eType==SQLIT
105a0 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 E_OPEN_MAIN_DB )
105a1 7b 0a 20 20 20 20 55 6e 69 78 55 6e 75 73 65 64 {. UnixUnused
105a2 46 64 20 2a 70 55 6e 75 73 65 64 3b 0a 20 20 20 Fd *pUnused;.
105a3 20 70 55 6e 75 73 65 64 20 3d 20 66 69 6e 64 52 pUnused = findR
105a4 65 75 73 61 62 6c 65 46 64 28 7a 4e 61 6d 65 2c eusableFd(zName,
105a5 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 flags);. if(
105a6 20 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 pUnused ){.
105a7 20 20 66 64 20 3d 20 70 55 6e 75 73 65 64 2d 3e fd = pUnused->
105a8 66 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 fd;. }else{.
105a9 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d 20 73 pUnused = s
105aa 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 qlite3_malloc(si
105ab 7a 65 6f 66 28 2a 70 55 6e 75 73 65 64 29 29 3b zeof(*pUnused));
105ac 0a 20 20 20 20 20 20 69 66 28 20 21 70 55 6e 75 . if( !pUnu
105ad 73 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 sed ){. r
105ae 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
105af 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 EM;. }.
105b0 7d 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73 65 64 }. p->pUnused
105b1 20 3d 20 70 55 6e 75 73 65 64 3b 0a 0a 20 20 20 = pUnused;..
105b2 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c /* Database fil
105b3 65 6e 61 6d 65 73 20 61 72 65 20 64 6f 75 62 6c enames are doubl
105b4 65 2d 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 e-zero terminate
105b5 64 20 69 66 20 74 68 65 79 20 61 72 65 20 6e 6f d if they are no
105b6 74 0a 20 20 20 20 2a 2a 20 55 52 49 73 20 77 69 t. ** URIs wi
105b7 74 68 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 th parameters.
105b8 48 65 6e 63 65 2c 20 74 68 65 79 20 63 61 6e 20 Hence, they can
105b9 61 6c 77 61 79 73 20 62 65 20 70 61 73 73 65 64 always be passed
105ba 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c into. ** sql
105bb 69 74 65 33 5f 75 72 69 5f 70 61 72 61 6d 65 74 ite3_uri_paramet
105bc 65 72 28 29 2e 20 2a 2f 0a 20 20 20 20 61 73 73 er(). */. ass
105bd 65 72 74 28 20 28 66 6c 61 67 73 20 26 20 53 51 ert( (flags & SQ
105be 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49 29 20 7c LITE_OPEN_URI) |
105bf 7c 20 7a 4e 61 6d 65 5b 73 74 72 6c 65 6e 28 7a | zName[strlen(z
105c0 4e 61 6d 65 29 2b 31 5d 3d 3d 30 20 29 3b 0a 0a Name)+1]==0 );..
105c1 20 20 7d 65 6c 73 65 20 69 66 28 20 21 7a 4e 61 }else if( !zNa
105c2 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 me ){. /* If
105c3 7a 4e 61 6d 65 20 69 73 20 4e 55 4c 4c 2c 20 74 zName is NULL, t
105c4 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 20 69 he upper layer i
105c5 73 20 72 65 71 75 65 73 74 69 6e 67 20 61 20 74 s requesting a t
105c6 65 6d 70 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 emp file. */.
105c7 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 65 assert(isDelete
105c8 20 26 26 20 21 73 79 6e 63 44 69 72 29 3b 0a 20 && !syncDir);.
105c9 20 20 20 72 63 20 3d 20 75 6e 69 78 47 65 74 54 rc = unixGetT
105ca 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 empname(MAX_PATH
105cb 4e 41 4d 45 2b 32 2c 20 7a 54 6d 70 6e 61 6d 65 NAME+2, zTmpname
105cc 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
105cd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
105ce 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
105cf 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a }. zName = z
105d0 54 6d 70 6e 61 6d 65 3b 0a 0a 20 20 20 20 2f 2a Tmpname;.. /*
105d1 20 47 65 6e 65 72 61 74 65 64 20 74 65 6d 70 6f Generated tempo
105d2 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 73 20 61 rary filenames a
105d3 72 65 20 61 6c 77 61 79 73 20 64 6f 75 62 6c 65 re always double
105d4 2d 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 -zero terminated
105d5 0a 20 20 20 20 2a 2a 20 66 6f 72 20 75 73 65 20 . ** for use
105d6 62 79 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 70 by sqlite3_uri_p
105d7 61 72 61 6d 65 74 65 72 28 29 2e 20 2a 2f 0a 20 arameter(). */.
105d8 20 20 20 61 73 73 65 72 74 28 20 7a 4e 61 6d 65 assert( zName
105d9 5b 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 2b 31 [strlen(zName)+1
105da 5d 3d 3d 30 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f ]==0 );. }.. /
105db 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 * Determine the
105dc 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6c 61 value of the fla
105dd 67 73 20 70 61 72 61 6d 65 74 65 72 20 70 61 73 gs parameter pas
105de 73 65 64 20 74 6f 20 50 4f 53 49 58 20 66 75 6e sed to POSIX fun
105df 63 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 28 ction. ** open(
105e0 29 2e 20 54 68 65 73 65 20 6d 75 73 74 20 62 65 ). These must be
105e1 20 63 61 6c 63 75 6c 61 74 65 64 20 65 76 65 6e calculated even
105e2 20 69 66 20 6f 70 65 6e 28 29 20 69 73 20 6e 6f if open() is no
105e3 74 20 63 61 6c 6c 65 64 2c 20 61 73 0a 20 20 2a t called, as. *
105e4 2a 20 74 68 65 79 20 6d 61 79 20 62 65 20 73 74 * they may be st
105e5 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 ored as part of
105e6 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 the file handle
105e7 61 6e 64 20 75 73 65 64 20 62 79 20 74 68 65 20 and used by the
105e8 0a 20 20 2a 2a 20 27 63 6f 6e 63 68 20 66 69 6c . ** 'conch fil
105e9 65 27 20 6c 6f 63 6b 69 6e 67 20 66 75 6e 63 74 e' locking funct
105ea 69 6f 6e 73 20 6c 61 74 65 72 20 6f 6e 2e 20 20 ions later on.
105eb 2a 2f 0a 20 20 69 66 28 20 69 73 52 65 61 64 6f */. if( isReado
105ec 6e 6c 79 20 29 20 20 6f 70 65 6e 46 6c 61 67 73 nly ) openFlags
105ed 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 |= O_RDONLY;.
105ee 69 66 28 20 69 73 52 65 61 64 57 72 69 74 65 20 if( isReadWrite
105ef 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f ) openFlags |= O
105f0 5f 52 44 57 52 3b 0a 20 20 69 66 28 20 69 73 43 _RDWR;. if( isC
105f1 72 65 61 74 65 20 29 20 20 20 20 6f 70 65 6e 46 reate ) openF
105f2 6c 61 67 73 20 7c 3d 20 4f 5f 43 52 45 41 54 3b lags |= O_CREAT;
105f3 0a 20 20 69 66 28 20 69 73 45 78 63 6c 75 73 69 . if( isExclusi
105f4 76 65 20 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c ve ) openFlags |
105f5 3d 20 28 4f 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 4f = (O_EXCL|O_NOFO
105f6 4c 4c 4f 57 29 3b 0a 20 20 6f 70 65 6e 46 6c 61 LLOW);. openFla
105f7 67 73 20 7c 3d 20 28 4f 5f 4c 41 52 47 45 46 49 gs |= (O_LARGEFI
105f8 4c 45 7c 4f 5f 42 49 4e 41 52 59 29 3b 0a 0a 20 LE|O_BINARY);..
105f9 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 if( fd<0 ){.
105fa 20 6d 6f 64 65 5f 74 20 6f 70 65 6e 4d 6f 64 65 mode_t openMode
105fb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
105fc 2a 20 50 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f * Permissions to
105fd 20 63 72 65 61 74 65 20 66 69 6c 65 20 77 69 74 create file wit
105fe 68 20 2a 2f 0a 20 20 20 20 75 69 64 5f 74 20 75 h */. uid_t u
105ff 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 id;
10600 20 20 20 20 20 20 20 2f 2a 20 55 73 65 72 69 64 /* Userid
10601 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 2a 2f for the file */
10602 0a 20 20 20 20 67 69 64 5f 74 20 67 69 64 3b 20 . gid_t gid;
10603 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10604 20 20 20 2f 2a 20 47 72 6f 75 70 69 64 20 66 6f /* Groupid fo
10605 72 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 r the file */.
10606 20 20 72 63 20 3d 20 66 69 6e 64 43 72 65 61 74 rc = findCreat
10607 65 46 69 6c 65 4d 6f 64 65 28 7a 4e 61 6d 65 2c eFileMode(zName,
10608 20 66 6c 61 67 73 2c 20 26 6f 70 65 6e 4d 6f 64 flags, &openMod
10609 65 2c 20 26 75 69 64 2c 20 26 67 69 64 29 3b 0a e, &uid, &gid);.
1060a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1060b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 TE_OK ){. a
1060c 73 73 65 72 74 28 20 21 70 2d 3e 70 55 6e 75 73 ssert( !p->pUnus
1060d 65 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 ed );. asse
1060e 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 rt( eType==SQLIT
1060f 45 5f 4f 50 45 4e 5f 57 41 4c 20 7c 7c 20 65 54 E_OPEN_WAL || eT
10610 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
10611 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 29 3b _MAIN_JOURNAL );
10612 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
10613 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 64 20 3d ;. }. fd =
10614 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 7a 4e 61 robust_open(zNa
10615 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 6f me, openFlags, o
10616 70 65 6e 4d 6f 64 65 29 3b 0a 20 20 20 20 4f 53 penMode);. OS
10617 54 52 41 43 45 28 28 22 4f 50 45 4e 58 20 20 20 TRACE(("OPENX
10618 25 2d 33 64 20 25 73 20 30 25 6f 5c 6e 22 2c 20 %-3d %s 0%o\n",
10619 66 64 2c 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 fd, zName, openF
1061a 6c 61 67 73 29 29 3b 0a 20 20 20 20 69 66 28 20 lags));. if(
1061b 66 64 3c 30 20 26 26 20 65 72 72 6e 6f 21 3d 45 fd<0 && errno!=E
1061c 49 53 44 49 52 20 26 26 20 69 73 52 65 61 64 57 ISDIR && isReadW
1061d 72 69 74 65 20 26 26 20 21 69 73 45 78 63 6c 75 rite && !isExclu
1061e 73 69 76 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a sive ){. /*
1061f 20 46 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 20 Failed to open
10620 74 68 65 20 66 69 6c 65 20 66 6f 72 20 72 65 61 the file for rea
10621 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 d/write access.
10622 54 72 79 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a Try read-only. *
10623 2f 0a 20 20 20 20 20 20 66 6c 61 67 73 20 26 3d /. flags &=
10624 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 ~(SQLITE_OPEN_R
10625 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f EADWRITE|SQLITE_
10626 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20 OPEN_CREATE);.
10627 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 26 3d openFlags &=
10628 20 7e 28 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 ~(O_RDWR|O_CREA
10629 54 29 3b 0a 20 20 20 20 20 20 66 6c 61 67 73 20 T);. flags
1062a 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 |= SQLITE_OPEN_R
1062b 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 6f EADONLY;. o
1062c 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 penFlags |= O_RD
1062d 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 69 73 52 65 ONLY;. isRe
1062e 61 64 6f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20 adonly = 1;.
1062f 20 20 66 64 20 3d 20 72 6f 62 75 73 74 5f 6f 70 fd = robust_op
10630 65 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c en(zName, openFl
10631 61 67 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a ags, openMode);.
10632 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66 64 }. if( fd
10633 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d <0 ){. rc =
10634 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 53 51 unixLogError(SQ
10635 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b LITE_CANTOPEN_BK
10636 50 54 2c 20 22 6f 70 65 6e 22 2c 20 7a 4e 61 6d PT, "open", zNam
10637 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f e);. goto o
10638 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 pen_finished;.
10639 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 }.. /* If t
1063a 68 69 73 20 70 72 6f 63 65 73 73 20 69 73 20 72 his process is r
1063b 75 6e 6e 69 6e 67 20 61 73 20 72 6f 6f 74 20 61 unning as root a
1063c 6e 64 20 69 66 20 63 72 65 61 74 69 6e 67 20 61 nd if creating a
1063d 20 6e 65 77 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 new rollback.
1063e 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 ** journal or
1063f 57 41 4c 20 66 69 6c 65 2c 20 73 65 74 20 74 68 WAL file, set th
10640 65 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 74 e ownership of t
10641 68 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 57 41 he journal or WA
10642 4c 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a 20 74 L to be. ** t
10643 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 6f he same as the o
10644 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 riginal database
10645 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
10646 20 66 6c 61 67 73 20 26 20 28 53 51 4c 49 54 45 flags & (SQLITE
10647 5f 4f 50 45 4e 5f 57 41 4c 7c 53 51 4c 49 54 45 _OPEN_WAL|SQLITE
10648 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e _OPEN_MAIN_JOURN
10649 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 6f 73 46 AL) ){. osF
1064a 63 68 6f 77 6e 28 66 64 2c 20 75 69 64 2c 20 67 chown(fd, uid, g
1064b 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 id);. }. }.
1064c 20 61 73 73 65 72 74 28 20 66 64 3e 3d 30 20 29 assert( fd>=0 )
1064d 3b 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 ;. if( pOutFlag
1064e 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c s ){. *pOutFl
1064f 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d ags = flags;. }
10650 0a 0a 20 20 69 66 28 20 70 2d 3e 70 55 6e 75 73 .. if( p->pUnus
10651 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 70 55 6e ed ){. p->pUn
10652 75 73 65 64 2d 3e 66 64 20 3d 20 66 64 3b 0a 20 used->fd = fd;.
10653 20 20 20 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 p->pUnused->f
10654 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 lags = flags;.
10655 7d 0a 0a 20 20 69 66 28 20 69 73 44 65 6c 65 74 }.. if( isDelet
10656 65 20 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f e ){.#if OS_VXWO
10657 52 4b 53 0a 20 20 20 20 7a 50 61 74 68 20 3d 20 RKS. zPath =
10658 7a 4e 61 6d 65 3b 0a 23 65 6c 73 65 0a 20 20 20 zName;.#else.
10659 20 6f 73 55 6e 6c 69 6e 6b 28 7a 4e 61 6d 65 29 osUnlink(zName)
1065a 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 69 66 ;.#endif. }.#if
1065b 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
1065c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 65 OCKING_STYLE. e
1065d 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 6f 70 65 6e lse{. p->open
1065e 46 6c 61 67 73 20 3d 20 6f 70 65 6e 46 6c 61 67 Flags = openFlag
1065f 73 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 s;. }.#endif..
10660 20 6e 6f 4c 6f 63 6b 20 3d 20 65 54 79 70 65 21 noLock = eType!
10661 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 =SQLITE_OPEN_MAI
10662 4e 5f 44 42 3b 0a 0a 20 20 0a 23 69 66 20 64 65 N_DB;.. .#if de
10663 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
10664 20 7c 7c 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c || SQLITE_ENABL
10665 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
10666 20 20 69 66 28 20 66 73 74 61 74 66 73 28 66 64 if( fstatfs(fd
10667 2c 20 26 66 73 49 6e 66 6f 29 20 3d 3d 20 2d 31 , &fsInfo) == -1
10668 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 ){. ((unixFi
10669 6c 65 2a 29 70 46 69 6c 65 29 2d 3e 6c 61 73 74 le*)pFile)->last
1066a 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
1066b 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 robust_close(
1066c 70 2c 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 p, fd, __LINE__)
1066d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
1066e 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 ITE_IOERR_ACCESS
1066f 3b 0a 20 20 7d 0a 20 20 69 66 20 28 30 20 3d 3d ;. }. if (0 ==
10670 20 73 74 72 6e 63 6d 70 28 22 6d 73 64 6f 73 22 strncmp("msdos"
10671 2c 20 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 , fsInfo.f_fstyp
10672 65 6e 61 6d 65 2c 20 35 29 29 20 7b 0a 20 20 20 ename, 5)) {.
10673 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 ((unixFile*)pFi
10674 6c 65 29 2d 3e 66 73 46 6c 61 67 73 20 7c 3d 20 le)->fsFlags |=
10675 53 51 4c 49 54 45 5f 46 53 46 4c 41 47 53 5f 49 SQLITE_FSFLAGS_I
10676 53 5f 4d 53 44 4f 53 3b 0a 20 20 7d 0a 23 65 6e S_MSDOS;. }.#en
10677 64 69 66 0a 0a 20 20 2f 2a 20 53 65 74 20 75 70 dif.. /* Set up
10678 20 61 70 70 72 6f 70 72 69 61 74 65 20 63 74 72 appropriate ctr
10679 6c 46 6c 61 67 73 20 2a 2f 0a 20 20 69 66 28 20 lFlags */. if(
1067a 69 73 44 65 6c 65 74 65 20 29 20 20 20 20 20 20 isDelete )
1067b 20 20 20 20 20 20 20 20 20 20 63 74 72 6c 46 6c ctrlFl
1067c 61 67 73 20 7c 3d 20 55 4e 49 58 46 49 4c 45 5f ags |= UNIXFILE_
1067d 44 45 4c 45 54 45 3b 0a 20 20 69 66 28 20 69 73 DELETE;. if( is
1067e 52 65 61 64 6f 6e 6c 79 20 29 20 20 20 20 20 20 Readonly )
1067f 20 20 20 20 20 20 20 20 63 74 72 6c 46 6c 61 67 ctrlFlag
10680 73 20 7c 3d 20 55 4e 49 58 46 49 4c 45 5f 52 44 s |= UNIXFILE_RD
10681 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 6e 6f 4c 6f ONLY;. if( noLo
10682 63 6b 20 29 20 20 20 20 20 20 20 20 20 20 20 20 ck )
10683 20 20 20 20 20 20 63 74 72 6c 46 6c 61 67 73 20 ctrlFlags
10684 7c 3d 20 55 4e 49 58 46 49 4c 45 5f 4e 4f 4c 4f |= UNIXFILE_NOLO
10685 43 4b 3b 0a 20 20 69 66 28 20 73 79 6e 63 44 69 CK;. if( syncDi
10686 72 20 29 20 20 20 20 20 20 20 20 20 20 20 20 20 r )
10687 20 20 20 20 63 74 72 6c 46 6c 61 67 73 20 7c 3d ctrlFlags |=
10688 20 55 4e 49 58 46 49 4c 45 5f 44 49 52 53 59 4e UNIXFILE_DIRSYN
10689 43 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 C;. if( flags &
1068a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49 SQLITE_OPEN_URI
1068b 20 29 20 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20 ) ctrlFlags |=
1068c 55 4e 49 58 46 49 4c 45 5f 55 52 49 3b 0a 0a 23 UNIXFILE_URI;..#
1068d 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 if SQLITE_ENABLE
1068e 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 23 _LOCKING_STYLE.#
1068f 69 66 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52 if SQLITE_PREFER
10690 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 0a 20 _PROXY_LOCKING.
10691 20 69 73 41 75 74 6f 50 72 6f 78 79 20 3d 20 31 isAutoProxy = 1
10692 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 69 ;.#endif. if( i
10693 73 41 75 74 6f 50 72 6f 78 79 20 26 26 20 28 7a sAutoProxy && (z
10694 50 61 74 68 21 3d 4e 55 4c 4c 29 20 26 26 20 28 Path!=NULL) && (
10695 21 6e 6f 4c 6f 63 6b 29 20 26 26 20 70 56 66 73 !noLock) && pVfs
10696 2d 3e 78 4f 70 65 6e 20 29 7b 0a 20 20 20 20 63 ->xOpen ){. c
10697 68 61 72 20 2a 65 6e 76 66 6f 72 63 65 20 3d 20 har *envforce =
10698 67 65 74 65 6e 76 28 22 53 51 4c 49 54 45 5f 46 getenv("SQLITE_F
10699 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 ORCE_PROXY_LOCKI
1069a 4e 47 22 29 3b 0a 20 20 20 20 69 6e 74 20 75 73 NG");. int us
1069b 65 50 72 6f 78 79 20 3d 20 30 3b 0a 0a 20 20 20 eProxy = 0;..
1069c 20 2f 2a 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 /* SQLITE_FORCE
1069d 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 3d 3d _PROXY_LOCKING==
1069e 31 20 6d 65 61 6e 73 20 66 6f 72 63 65 20 61 6c 1 means force al
1069f 77 61 79 73 20 75 73 65 20 70 72 6f 78 79 2c 20 ways use proxy,
106a0 30 20 6d 65 61 6e 73 20 0a 20 20 20 20 2a 2a 20 0 means . **
106a1 6e 65 76 65 72 20 75 73 65 20 70 72 6f 78 79 2c never use proxy,
106a2 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 75 73 65 20 NULL means use
106a3 70 72 6f 78 79 20 66 6f 72 20 6e 6f 6e 2d 6c 6f proxy for non-lo
106a4 63 61 6c 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 cal files only.
106a5 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 6e 76 66 */. if( envf
106a6 6f 72 63 65 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 orce!=NULL ){.
106a7 20 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 61 useProxy = a
106a8 74 6f 69 28 65 6e 76 66 6f 72 63 65 29 3e 30 3b toi(envforce)>0;
106a9 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
106aa 20 20 69 66 28 20 73 74 61 74 66 73 28 7a 50 61 if( statfs(zPa
106ab 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 3d 3d 20 th, &fsInfo) ==
106ac 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a -1 ){. /*
106ad 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68 65 20 In theory, the
106ae 63 6c 6f 73 65 28 66 64 29 20 63 61 6c 6c 20 69 close(fd) call i
106af 73 20 73 75 62 2d 6f 70 74 69 6d 61 6c 2e 20 49 s sub-optimal. I
106b0 66 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e 65 f the file opene
106b1 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 69 74 d. ** wit
106b2 68 20 66 64 20 69 73 20 61 20 64 61 74 61 62 61 h fd is a databa
106b3 73 65 20 66 69 6c 65 2c 20 61 6e 64 20 74 68 65 se file, and the
106b4 72 65 20 61 72 65 20 6f 74 68 65 72 20 63 6f 6e re are other con
106b5 6e 65 63 74 69 6f 6e 73 20 6f 70 65 6e 0a 20 20 nections open.
106b6 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 61 74 ** on that
106b7 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20 63 file that are c
106b8 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 urrently holding
106b9 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 advisory locks
106ba 6f 6e 20 69 74 2c 0a 20 20 20 20 20 20 20 20 2a on it,. *
106bb 2a 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 20 * then the call
106bc 74 6f 20 63 6c 6f 73 65 28 29 20 77 69 6c 6c 20 to close() will
106bd 63 61 6e 63 65 6c 20 74 68 6f 73 65 20 6c 6f 63 cancel those loc
106be 6b 73 2e 20 49 6e 20 70 72 61 63 74 69 63 65 2c ks. In practice,
106bf 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 27 72 . ** we'r
106c0 65 20 61 73 73 75 6d 69 6e 67 20 74 68 61 74 20 e assuming that
106c1 73 74 61 74 66 73 28 29 20 64 6f 65 73 6e 27 74 statfs() doesn't
106c2 20 66 61 69 6c 20 76 65 72 79 20 6f 66 74 65 6e fail very often
106c3 2e 20 41 74 20 6c 65 61 73 74 0a 20 20 20 20 20 . At least.
106c4 20 20 20 2a 2a 20 6e 6f 74 20 77 68 69 6c 65 20 ** not while
106c5 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 63 72 other file descr
106c6 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 62 79 iptors opened by
106c7 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 the same proces
106c8 73 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 s on. **
106c9 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 61 72 the same file ar
106ca 65 20 77 6f 72 6b 69 6e 67 2e 20 20 2a 2f 0a 20 e working. */.
106cb 20 20 20 20 20 20 20 70 2d 3e 6c 61 73 74 45 72 p->lastEr
106cc 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
106cd 20 20 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 robust_clos
106ce 65 28 70 2c 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f e(p, fd, __LINE_
106cf 5f 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d _);. rc =
106d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 SQLITE_IOERR_AC
106d1 43 45 53 53 3b 0a 20 20 20 20 20 20 20 20 67 6f CESS;. go
106d2 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 to open_finished
106d3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
106d4 75 73 65 50 72 6f 78 79 20 3d 20 21 28 66 73 49 useProxy = !(fsI
106d5 6e 66 6f 2e 66 5f 66 6c 61 67 73 26 4d 4e 54 5f nfo.f_flags&MNT_
106d6 4c 4f 43 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 LOCAL);. }.
106d7 20 20 69 66 28 20 75 73 65 50 72 6f 78 79 20 29 if( useProxy )
106d8 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 6c {. rc = fil
106d9 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73 lInUnixFile(pVfs
106da 2c 20 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 , fd, pFile, zPa
106db 74 68 2c 20 63 74 72 6c 46 6c 61 67 73 29 3b 0a th, ctrlFlags);.
106dc 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
106dd 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
106de 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54 72 61 rc = proxyTra
106df 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 28 nsformUnixFile((
106e0 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 6c 65 2c unixFile*)pFile,
106e1 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 20 20 20 20 ":auto:");.
106e2 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
106e3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
106e4 20 20 20 2f 2a 20 55 73 65 20 75 6e 69 78 43 6c /* Use unixCl
106e5 6f 73 65 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 ose to clean up
106e6 74 68 65 20 72 65 73 6f 75 72 63 65 73 20 61 64 the resources ad
106e7 64 65 64 20 69 6e 20 66 69 6c 6c 49 6e 55 6e 69 ded in fillInUni
106e8 78 46 69 6c 65 20 0a 20 20 20 20 20 20 20 20 20 xFile .
106e9 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 20 61 6c ** and clear al
106ea 6c 20 74 68 65 20 73 74 72 75 63 74 75 72 65 27 l the structure'
106eb 73 20 72 65 66 65 72 65 6e 63 65 73 2e 20 20 53 s references. S
106ec 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 0a 20 20 pecifically, .
106ed 20 20 20 20 20 20 20 20 2a 2a 20 70 46 69 6c 65 ** pFile
106ee 2d 3e 70 4d 65 74 68 6f 64 73 20 77 69 6c 6c 20 ->pMethods will
106ef 62 65 20 4e 55 4c 4c 20 73 6f 20 73 71 6c 69 74 be NULL so sqlit
106f0 65 33 4f 73 43 6c 6f 73 65 20 77 69 6c 6c 20 62 e3OsClose will b
106f1 65 20 61 20 6e 6f 2d 6f 70 20 0a 20 20 20 20 20 e a no-op .
106f2 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
106f3 20 20 75 6e 69 78 43 6c 6f 73 65 28 70 46 69 6c unixClose(pFil
106f4 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 e);. re
106f5 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
106f6 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
106f7 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 goto open_finis
106f8 68 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 hed;. }. }.#
106f9 65 6e 64 69 66 0a 20 20 0a 20 20 72 63 20 3d 20 endif. . rc =
106fa 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 fillInUnixFile(p
106fb 56 66 73 2c 20 66 64 2c 20 70 46 69 6c 65 2c 20 Vfs, fd, pFile,
106fc 7a 50 61 74 68 2c 20 63 74 72 6c 46 6c 61 67 73 zPath, ctrlFlags
106fd 29 3b 0a 0a 6f 70 65 6e 5f 66 69 6e 69 73 68 65 );..open_finishe
106fe 64 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c d:. if( rc!=SQL
106ff 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 ITE_OK ){. sq
10700 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 70 55 lite3_free(p->pU
10701 6e 75 73 65 64 29 3b 0a 20 20 7d 0a 20 20 72 65 nused);. }. re
10702 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
10703 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 66 69 ** Delete the fi
10704 6c 65 20 61 74 20 7a 50 61 74 68 2e 20 49 66 20 le at zPath. If
10705 74 68 65 20 64 69 72 53 79 6e 63 20 61 72 67 75 the dirSync argu
10706 6d 65 6e 74 20 69 73 20 74 72 75 65 2c 20 66 73 ment is true, fs
10707 79 6e 63 28 29 0a 2a 2a 20 74 68 65 20 64 69 72 ync().** the dir
10708 65 63 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c ectory after del
10709 65 74 69 6e 67 20 74 68 65 20 66 69 6c 65 2e 0a eting the file..
1070a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
1070b 69 78 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 ixDelete(. sqli
1070c 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 te3_vfs *NotUsed
1070d 2c 20 20 20 20 20 2f 2a 20 56 46 53 20 63 6f 6e , /* VFS con
1070e 74 61 69 6e 69 6e 67 20 74 68 69 73 20 61 73 20 taining this as
1070f 74 68 65 20 78 44 65 6c 65 74 65 20 6d 65 74 68 the xDelete meth
10710 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 od */. const ch
10711 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 ar *zPath,
10712 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c /* Name of fil
10713 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 e to be deleted
10714 2a 2f 0a 20 20 69 6e 74 20 64 69 72 53 79 6e 63 */. int dirSync
10715 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10716 2a 20 49 66 20 74 72 75 65 2c 20 66 73 79 6e 63 * If true, fsync
10717 28 29 20 64 69 72 65 63 74 6f 72 79 20 61 66 74 () directory aft
10718 65 72 20 64 65 6c 65 74 69 6e 67 20 66 69 6c 65 er deleting file
10719 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 */.){. int rc
1071a 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 55 = SQLITE_OK;. U
1071b 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
1071c 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 6d 75 NotUsed);. Simu
1071d 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 lateIOError(retu
1071e 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
1071f 44 45 4c 45 54 45 29 3b 0a 20 20 69 66 28 20 6f DELETE);. if( o
10720 73 55 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 3d 3d sUnlink(zPath)==
10721 28 2d 31 29 20 29 7b 0a 20 20 20 20 69 66 28 20 (-1) ){. if(
10722 65 72 72 6e 6f 3d 3d 45 4e 4f 45 4e 54 20 29 7b errno==ENOENT ){
10723 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
10724 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 5f TE_IOERR_DELETE_
10725 4e 4f 45 4e 54 3b 0a 20 20 20 20 7d 65 6c 73 65 NOENT;. }else
10726 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 {. rc = uni
10727 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 xLogError(SQLITE
10728 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 2c 20 22 _IOERR_DELETE, "
10729 75 6e 6c 69 6e 6b 22 2c 20 7a 50 61 74 68 29 3b unlink", zPath);
1072a 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
1072b 6e 20 72 63 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 n rc;. }.#ifnde
1072c 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 f SQLITE_DISABLE
1072d 5f 44 49 52 53 59 4e 43 0a 20 20 69 66 28 20 28 _DIRSYNC. if( (
1072e 64 69 72 53 79 6e 63 20 26 20 31 29 21 3d 30 20 dirSync & 1)!=0
1072f 29 7b 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 ){. int fd;.
10730 20 20 20 72 63 20 3d 20 6f 73 4f 70 65 6e 44 69 rc = osOpenDi
10731 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 rectory(zPath, &
10732 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d fd);. if( rc=
10733 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 =SQLITE_OK ){.#i
10734 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 f OS_VXWORKS.
10735 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 29 if( fsync(fd)
10736 3d 3d 2d 31 20 29 0a 23 65 6c 73 65 0a 20 20 20 ==-1 ).#else.
10737 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 29 if( fsync(fd)
10738 20 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 ).#endif.
10739 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 {. rc = u
1073a 6e 69 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 nixLogError(SQLI
1073b 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 TE_IOERR_DIR_FSY
1073c 4e 43 2c 20 22 66 73 79 6e 63 22 2c 20 7a 50 61 NC, "fsync", zPa
1073d 74 68 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 th);. }.
1073e 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 robust_close(
1073f 30 2c 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 0, fd, __LINE__)
10740 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
10741 72 63 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f rc==SQLITE_CANTO
10742 50 45 4e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 PEN ){. rc
10743 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
10744 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }. }.#endif.
10745 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
10746 0a 2a 2a 20 54 65 73 74 20 74 68 65 20 65 78 69 .** Test the exi
10747 73 74 61 6e 63 65 20 6f 66 20 6f 72 20 61 63 63 stance of or acc
10748 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 ess permissions
10749 6f 66 20 66 69 6c 65 20 7a 50 61 74 68 2e 20 54 of file zPath. T
1074a 68 65 0a 2a 2a 20 74 65 73 74 20 70 65 72 66 6f he.** test perfo
1074b 72 6d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e 20 rmed depends on
1074c 74 68 65 20 76 61 6c 75 65 20 6f 66 20 66 6c 61 the value of fla
1074d 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 gs:.**.** SQ
1074e 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 LITE_ACCESS_EXIS
1074f 54 53 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 TS: Return 1 if
10750 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 0a the file exists.
10751 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 ** SQLITE_AC
10752 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 20 CESS_READWRITE:
10753 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
10754 66 69 6c 65 20 69 73 20 72 65 61 64 20 61 6e 64 file is read and
10755 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 writable..**
10756 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f SQLITE_ACCESS_
10757 52 45 41 44 4f 4e 4c 59 3a 20 52 65 74 75 72 6e READONLY: Return
10758 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 1 if the file i
10759 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a s readable..**.*
1075a 2a 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 * Otherwise retu
1075b 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rn 0..*/.static
1075c 69 6e 74 20 75 6e 69 78 41 63 63 65 73 73 28 0a int unixAccess(.
1075d 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e sqlite3_vfs *N
1075e 6f 74 55 73 65 64 2c 20 20 20 2f 2a 20 54 68 65 otUsed, /* The
1075f 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 VFS containing
10760 74 68 69 73 20 78 41 63 63 65 73 73 20 6d 65 74 this xAccess met
10761 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 hod */. const c
10762 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 har *zPath,
10763 20 2f 2a 20 50 61 74 68 20 6f 66 20 74 68 65 20 /* Path of the
10764 66 69 6c 65 20 74 6f 20 65 78 61 6d 69 6e 65 20 file to examine
10765 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 */. int flags,
10766 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10767 57 68 61 74 20 64 6f 20 77 65 20 77 61 6e 74 20 What do we want
10768 74 6f 20 6c 65 61 72 6e 20 61 62 6f 75 74 20 74 to learn about t
10769 68 65 20 7a 50 61 74 68 20 66 69 6c 65 3f 20 2a he zPath file? *
1076a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 /. int *pResOut
1076b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
1076c 72 69 74 65 20 72 65 73 75 6c 74 20 62 6f 6f 6c rite result bool
1076d 65 61 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 ean here */.){.
1076e 20 69 6e 74 20 61 6d 6f 64 65 20 3d 20 30 3b 0a int amode = 0;.
1076f 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
10770 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53 ER(NotUsed);. S
10771 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
10772 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
10773 45 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0a 20 ERR_ACCESS; );.
10774 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 switch( flags )
10775 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
10776 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a E_ACCESS_EXISTS:
10777 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 46 . amode = F
10778 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b _OK;. break
10779 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 ;. case SQLIT
1077a 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 E_ACCESS_READWRI
1077b 54 45 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 TE:. amode
1077c 3d 20 57 5f 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 20 = W_OK|R_OK;.
1077d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 break;. ca
1077e 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 se SQLITE_ACCESS
1077f 5f 52 45 41 44 3a 0a 20 20 20 20 20 20 61 6d 6f _READ:. amo
10780 64 65 20 3d 20 52 5f 4f 4b 3b 0a 20 20 20 20 20 de = R_OK;.
10781 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66 break;.. def
10782 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 ault:. asse
10783 72 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 rt(!"Invalid fla
10784 67 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 gs argument");.
10785 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 }. *pResOut =
10786 28 6f 73 41 63 63 65 73 73 28 7a 50 61 74 68 2c (osAccess(zPath,
10787 20 61 6d 6f 64 65 29 3d 3d 30 29 3b 0a 20 20 69 amode)==0);. i
10788 66 28 20 66 6c 61 67 73 3d 3d 53 51 4c 49 54 45 f( flags==SQLITE
10789 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 20 26 _ACCESS_EXISTS &
1078a 26 20 2a 70 52 65 73 4f 75 74 20 29 7b 0a 20 20 & *pResOut ){.
1078b 20 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75 struct stat bu
1078c 66 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 6f 73 f;. if( 0==os
1078d 53 74 61 74 28 7a 50 61 74 68 2c 20 26 62 75 66 Stat(zPath, &buf
1078e 29 20 26 26 20 62 75 66 2e 73 74 5f 73 69 7a 65 ) && buf.st_size
1078f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 ==0 ){. *pR
10790 65 73 4f 75 74 20 3d 20 30 3b 0a 20 20 20 20 7d esOut = 0;. }
10791 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
10792 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a LITE_OK;.}.../*.
10793 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69 ** Turn a relati
10794 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f ve pathname into
10795 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 a full pathname
10796 2e 20 54 68 65 20 72 65 6c 61 74 69 76 65 20 70 . The relative p
10797 61 74 68 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 ath.** is stored
10798 20 61 73 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e as a nul-termin
10799 61 74 65 64 20 73 74 72 69 6e 67 20 69 6e 20 74 ated string in t
1079a 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 he buffer pointe
1079b 64 20 74 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 d to by.** zPath
1079c 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f . .**.** zOut po
1079d 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72 ints to a buffer
1079e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 71 6c of at least sql
1079f 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e ite3_vfs.mxPathn
107a0 61 6d 65 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 ame bytes .** (i
107a1 6e 20 74 68 69 73 20 63 61 73 65 2c 20 4d 41 58 n this case, MAX
107a2 5f 50 41 54 48 4e 41 4d 45 20 62 79 74 65 73 29 _PATHNAME bytes)
107a3 2e 20 54 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 . The full-path
107a4 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a is written to.**
107a5 20 74 68 69 73 20 62 75 66 66 65 72 20 62 65 66 this buffer bef
107a6 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
107a7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
107a8 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 xFullPathname(.
107a9 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
107aa 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f fs, /
107ab 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 * Pointer to vfs
107ac 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e object */. con
107ad 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 st char *zPath,
107ae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
107af 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 ssibly relative
107b0 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 input path */.
107b1 69 6e 74 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 int nOut,
107b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
107b3 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 Size of output
107b4 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 buffer in bytes
107b5 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 */. char *zOut
107b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
107b7 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 /* Output buf
107b8 66 65 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 fer */.){.. /*
107b9 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 It's odd to simu
107ba 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 late an io-error
107bb 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c here, but reall
107bc 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20 y this is just.
107bd 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f ** using the io
107be 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75 -error infrastru
107bf 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68 cture to test th
107c0 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 at SQLite handle
107c1 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 s this. ** func
107c2 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 tion failing. Th
107c3 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c is function coul
107c4 64 20 66 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 d fail if, for e
107c5 78 61 6d 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a xample, the. **
107c6 20 63 75 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 current working
107c7 20 64 69 72 65 63 74 6f 72 79 20 68 61 73 20 62 directory has b
107c8 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 een unlinked..
107c9 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 */. SimulateIOE
107ca 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
107cb 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 ITE_ERROR );..
107cc 61 73 73 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 assert( pVfs->mx
107cd 50 61 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 Pathname==MAX_PA
107ce 54 48 4e 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53 THNAME );. UNUS
107cf 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 ED_PARAMETER(pVf
107d0 73 29 3b 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74 s);.. zOut[nOut
107d1 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 -1] = '\0';. if
107d2 28 20 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 ( zPath[0]=='/'
107d3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ){. sqlite3_s
107d4 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f nprintf(nOut, zO
107d5 75 74 2c 20 22 25 73 22 2c 20 7a 50 61 74 68 29 ut, "%s", zPath)
107d6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
107d7 6e 74 20 6e 43 77 64 3b 0a 20 20 20 20 69 66 28 nt nCwd;. if(
107d8 20 6f 73 47 65 74 63 77 64 28 7a 4f 75 74 2c 20 osGetcwd(zOut,
107d9 6e 4f 75 74 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 nOut-1)==0 ){.
107da 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69 78 4c return unixL
107db 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 43 ogError(SQLITE_C
107dc 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 2c 20 22 67 ANTOPEN_BKPT, "g
107dd 65 74 63 77 64 22 2c 20 7a 50 61 74 68 29 3b 0a etcwd", zPath);.
107de 20 20 20 20 7d 0a 20 20 20 20 6e 43 77 64 20 3d }. nCwd =
107df 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 4f 75 (int)strlen(zOu
107e0 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f t);. sqlite3_
107e1 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2d 6e 43 snprintf(nOut-nC
107e2 77 64 2c 20 26 7a 4f 75 74 5b 6e 43 77 64 5d 2c wd, &zOut[nCwd],
107e3 20 22 2f 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a "/%s", zPath);.
107e4 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
107e5 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e ITE_OK;.}...#ifn
107e6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
107e7 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f LOAD_EXTENSION./
107e8 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 *.** Interfaces
107e9 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 for opening a sh
107ea 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 ared library, fi
107eb 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e nding entry poin
107ec 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 ts.** within the
107ed 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c shared library,
107ee 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 and closing the
107ef 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e shared library.
107f0 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 64 6c .*/.#include <dl
107f1 66 63 6e 2e 68 3e 0a 73 74 61 74 69 63 20 76 6f fcn.h>.static vo
107f2 69 64 20 2a 75 6e 69 78 44 6c 4f 70 65 6e 28 73 id *unixDlOpen(s
107f3 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 qlite3_vfs *NotU
107f4 73 65 64 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 sed, const char
107f5 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 *zFilename){. U
107f6 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
107f7 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 NotUsed);. retu
107f8 72 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e rn dlopen(zFilen
107f9 61 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 7c 20 ame, RTLD_NOW |
107fa 52 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a RTLD_GLOBAL);.}.
107fb 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61 ./*.** SQLite ca
107fc 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f lls this functio
107fd 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 n immediately af
107fe 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 75 6e ter a call to un
107ff 69 78 44 6c 53 79 6d 28 29 20 6f 72 0a 2a 2a 20 ixDlSym() or.**
10800 75 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66 61 69 unixDlOpen() fai
10801 6c 73 20 28 72 65 74 75 72 6e 73 20 61 20 6e 75 ls (returns a nu
10802 6c 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49 66 20 ll pointer). If
10803 61 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20 a more detailed
10804 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 error.** message
10805 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 69 is available, i
10806 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 t is written to
10807 7a 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f 20 65 zBufOut. If no e
10808 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 rror message.**
10809 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 7a 42 is available, zB
1080a 75 66 4f 75 74 20 69 73 20 6c 65 66 74 20 75 6e ufOut is left un
1080b 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 53 51 4c modified and SQL
1080c 69 74 65 20 75 73 65 73 20 61 20 64 65 66 61 75 ite uses a defau
1080d 6c 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 lt.** error mess
1080e 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 age..*/.static v
1080f 6f 69 64 20 75 6e 69 78 44 6c 45 72 72 6f 72 28 oid unixDlError(
10810 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 sqlite3_vfs *Not
10811 55 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20 Used, int nBuf,
10812 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a char *zBufOut){.
10813 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 const char *zE
10814 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 rr;. UNUSED_PAR
10815 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
10816 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 . unixEnterMute
10817 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64 6c x();. zErr = dl
10818 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20 7a error();. if( z
10819 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Err ){. sqlit
1081a 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 e3_snprintf(nBuf
1081b 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22 2c , zBufOut, "%s",
1081c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 75 6e zErr);. }. un
1081d 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
1081e 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 28 2a }.static void (*
1081f 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74 65 unixDlSym(sqlite
10820 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 3_vfs *NotUsed,
10821 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 20 63 void *p, const c
10822 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 har*zSym))(void)
10823 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 43 43 {. /* . ** GCC
10824 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d with -pedantic-
10825 65 72 72 6f 72 73 20 73 61 79 73 20 74 68 61 74 errors says that
10826 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 61 6c C90 does not al
10827 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f 20 62 low a void* to b
10828 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e 74 6f e. ** cast into
10829 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
1082a 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 20 79 function. And y
1082b 65 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 64 et the library d
1082c 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 0a 20 lsym() routine.
1082d 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76 6f ** returns a vo
1082e 69 64 2a 20 77 68 69 63 68 20 69 73 20 72 65 61 id* which is rea
1082f 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f lly a pointer to
10830 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 6f a function. So
10831 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a 2a 20 how do we. **
10832 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69 74 68 use dlsym() with
10833 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72 -pedantic-error
10834 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 s?. **. ** Var
10835 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 69 73 iable x below is
10836 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 61 defined to be a
10837 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 pointer to a fu
10838 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a 20 20 nction taking.
10839 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 76 6f ** parameters vo
1083a 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 63 68 id* and const ch
1083b 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e 69 6e ar* and returnin
1083c 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 g a pointer to a
1083d 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 function.. **
1083e 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 78 20 We initialize x
1083f 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69 74 20 by assigning it
10840 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
10841 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 69 6f dlsym() functio
10842 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 61 73 n.. ** (That as
10843 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65 signment require
10844 73 20 61 20 63 61 73 74 2e 29 20 20 54 68 65 6e s a cast.) Then
10845 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66 75 6e we call the fun
10846 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a 2a 20 ction that. **
10847 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 0a 20 x points to. .
10848 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 77 6f **. ** This wo
10849 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 6e 6c rk-around is unl
1084a 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 63 6f ikely to work co
1084b 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 20 73 rrectly on any s
1084c 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20 2a 2a ystem where. **
1084d 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 6e 6e you really cann
1084e 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63 74 69 ot cast a functi
1084f 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 on pointer into
10850 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 65 6e void*. But then
10851 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 74 , on the. ** ot
10852 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 6d 28 her hand, dlsym(
10853 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 ) will not work
10854 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74 65 6d on such a system
10855 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65 20 68 either, so we h
10856 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 61 ave. ** not rea
10857 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 69 6e lly lost anythin
10858 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20 28 g.. */. void (
10859 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 *(*x)(void*,cons
1085a 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 3b t char*))(void);
1085b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
1085c 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
1085d 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 28 76 x = (void(*(*)(v
1085e 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a oid*,const char*
1085f 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d 3b 0a ))(void))dlsym;.
10860 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28 70 2c return (*x)(p,
10861 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 69 63 zSym);.}.static
10862 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f 73 void unixDlClos
10863 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e e(sqlite3_vfs *N
10864 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 48 otUsed, void *pH
10865 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44 andle){. UNUSED
10866 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
10867 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28 70 ed);. dlclose(p
10868 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 Handle);.}.#else
10869 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d /* if SQLITE_OM
1086a 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
1086b 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f N is defined: */
1086c 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 . #define unixD
1086d 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66 69 lOpen 0. #defi
1086e 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 20 30 ne unixDlError 0
1086f 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 . #define unixD
10870 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66 69 lSym 0. #defi
10871 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20 30 ne unixDlClose 0
10872 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
10873 72 69 74 65 20 6e 42 75 66 20 62 79 74 65 73 20 rite nBuf bytes
10874 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 20 74 of random data t
10875 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 62 o the supplied b
10876 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a 73 uffer zBuf..*/.s
10877 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52 61 tatic int unixRa
10878 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 ndomness(sqlite3
10879 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 _vfs *NotUsed, i
1087a 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
1087b 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 Buf){. UNUSED_P
1087c 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
1087d 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73 69 7a );. assert((siz
1087e 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65 6f e_t)nBuf>=(sizeo
1087f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f 66 f(time_t)+sizeof
10880 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20 57 (int)));.. /* W
10881 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69 61 e have to initia
10882 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72 65 lize zBuf to pre
10883 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66 72 vent valgrind fr
10884 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20 2a om reporting. *
10885 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20 72 * errors. The r
10886 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 62 79 eports issued by
10887 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69 6e valgrind are in
10888 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f 75 correct - we wou
10889 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20 74 ld. ** prefer t
1088a 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 hat the randomne
1088b 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 64 20 ss be increased
1088c 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f 66 by making use of
1088d 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69 74 the. ** uninit
1088e 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69 6e ialized space in
1088f 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c 67 zBuf - but valg
10890 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e 64 rind errors tend
10891 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20 73 to worry. ** s
10892 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74 68 ome users. Rath
10893 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20 69 er than argue, i
10894 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 20 6a t seems easier j
10895 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a ust to initializ
10896 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c 65 e. ** the whole
10897 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65 6e array and silen
10898 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76 65 ce valgrind, eve
10899 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20 n if that means
1089a 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73 0a less randomness.
1089b 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e 64 ** in the rand
1089c 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 om seed.. **.
1089d 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c ** When testing,
1089e 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a 42 initializing zB
1089f 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73 20 uf[] to zero is
108a0 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61 74 all we do. That
108a1 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61 74 means. ** that
108a2 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 74 we always use t
108a3 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20 6e he same random n
108a4 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e 20 umber sequence.
108a5 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 0a This makes the.
108a6 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65 61 ** tests repea
108a7 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65 table.. */. me
108a8 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 mset(zBuf, 0, nB
108a9 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 uf);.#if !define
108aa 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 d(SQLITE_TEST).
108ab 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 2c 20 {. int pid,
108ac 66 64 2c 20 67 6f 74 3b 0a 20 20 20 20 66 64 20 fd, got;. fd
108ad 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 22 2f = robust_open("/
108ae 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f dev/urandom", O_
108af 52 44 4f 4e 4c 59 2c 20 30 29 3b 0a 20 20 20 20 RDONLY, 0);.
108b0 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 if( fd<0 ){.
108b1 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 20 time_t t;.
108b2 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 time(&t);.
108b3 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20 26 memcpy(zBuf, &
108b4 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 t, sizeof(t));.
108b5 20 20 20 20 20 70 69 64 20 3d 20 67 65 74 70 69 pid = getpi
108b6 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 d();. memcp
108b7 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66 28 74 y(&zBuf[sizeof(t
108b8 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66 )], &pid, sizeof
108b9 28 70 69 64 29 29 3b 0a 20 20 20 20 20 20 61 73 (pid));. as
108ba 73 65 72 74 28 20 73 69 7a 65 6f 66 28 74 29 2b sert( sizeof(t)+
108bb 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d 28 73 69 sizeof(pid)<=(si
108bc 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a 20 20 20 ze_t)nBuf );.
108bd 20 20 20 6e 42 75 66 20 3d 20 73 69 7a 65 6f 66 nBuf = sizeof
108be 28 74 29 20 2b 20 73 69 7a 65 6f 66 28 70 69 64 (t) + sizeof(pid
108bf 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
108c0 20 20 20 20 64 6f 7b 20 67 6f 74 20 3d 20 6f 73 do{ got = os
108c1 52 65 61 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e Read(fd, zBuf, n
108c2 42 75 66 29 3b 20 7d 77 68 69 6c 65 28 20 67 6f Buf); }while( go
108c3 74 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d 45 49 t<0 && errno==EI
108c4 4e 54 52 20 29 3b 0a 20 20 20 20 20 20 72 6f 62 NTR );. rob
108c5 75 73 74 5f 63 6c 6f 73 65 28 30 2c 20 66 64 2c ust_close(0, fd,
108c6 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 20 20 20 __LINE__);.
108c7 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 }. }.#endif. r
108c8 65 74 75 72 6e 20 6e 42 75 66 3b 0a 7d 0a 0a 0a eturn nBuf;.}...
108c9 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 /*.** Sleep for
108ca 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 a little while.
108cb 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 Return the amou
108cc 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 nt of time slept
108cd 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e ..** The argumen
108ce 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 t is the number
108cf 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 of microseconds
108d0 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 we want to sleep
108d1 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 ..** The return
108d2 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d value is the num
108d3 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f ber of microseco
108d4 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 nds of sleep act
108d5 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 ually.** request
108d6 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 ed from the unde
108d7 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 rlying operating
108d8 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 system, a numbe
108d9 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 r which.** might
108da 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e be greater than
108db 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
108dc 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e argument, but n
108dd 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 ot less.** than
108de 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f the argument..*/
108df 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
108e0 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 Sleep(sqlite3_vf
108e1 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 s *NotUsed, int
108e2 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 7b 0a 23 microseconds){.#
108e3 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
108e4 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63 20 struct timespec
108e5 73 70 3b 0a 0a 20 20 73 70 2e 74 76 5f 73 65 63 sp;.. sp.tv_sec
108e6 20 3d 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 = microseconds
108e7 2f 20 31 30 30 30 30 30 30 3b 0a 20 20 73 70 2e / 1000000;. sp.
108e8 74 76 5f 6e 73 65 63 20 3d 20 28 6d 69 63 72 6f tv_nsec = (micro
108e9 73 65 63 6f 6e 64 73 20 25 20 31 30 30 30 30 30 seconds % 100000
108ea 30 29 20 2a 20 31 30 30 30 3b 0a 20 20 6e 61 6e 0) * 1000;. nan
108eb 6f 73 6c 65 65 70 28 26 73 70 2c 20 4e 55 4c 4c osleep(&sp, NULL
108ec 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
108ed 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
108ee 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 return microse
108ef 63 6f 6e 64 73 3b 0a 23 65 6c 69 66 20 64 65 66 conds;.#elif def
108f0 69 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 45 50 ined(HAVE_USLEEP
108f1 29 20 26 26 20 48 41 56 45 5f 55 53 4c 45 45 50 ) && HAVE_USLEEP
108f2 0a 20 20 75 73 6c 65 65 70 28 6d 69 63 72 6f 73 . usleep(micros
108f3 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e 55 53 45 econds);. UNUSE
108f4 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
108f5 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d sed);. return m
108f6 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23 65 6c icroseconds;.#el
108f7 73 65 0a 20 20 69 6e 74 20 73 65 63 6f 6e 64 73 se. int seconds
108f8 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 = (microseconds
108f9 2b 39 39 39 39 39 39 29 2f 31 30 30 30 30 30 30 +999999)/1000000
108fa 3b 0a 20 20 73 6c 65 65 70 28 73 65 63 6f 6e 64 ;. sleep(second
108fb 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 s);. UNUSED_PAR
108fc 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
108fd 0a 20 20 72 65 74 75 72 6e 20 73 65 63 6f 6e 64 . return second
108fe 73 2a 31 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 s*1000000;.#endi
108ff 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 f.}../*.** The f
10900 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c ollowing variabl
10901 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e e, if set to a n
10902 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 69 on-zero value, i
10903 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 s interpreted as
10904 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f .** the number o
10905 66 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 f seconds since
10906 31 39 37 30 20 61 6e 64 20 69 73 20 75 73 65 64 1970 and is used
10907 20 74 6f 20 73 65 74 20 74 68 65 20 72 65 73 75 to set the resu
10908 6c 74 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 lt of.** sqlite3
10909 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29 20 OsCurrentTime()
1090a 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2e 0a during testing..
1090b 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
1090c 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
1090d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 int sqlite3_cur
1090e 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 20 20 rent_time = 0;
1090f 2f 2a 20 46 61 6b 65 20 73 79 73 74 65 6d 20 74 /* Fake system t
10910 69 6d 65 20 69 6e 20 73 65 63 6f 6e 64 73 20 73 ime in seconds s
10911 69 6e 63 65 20 31 39 37 30 2e 20 2a 2f 0a 23 65 ince 1970. */.#e
10912 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 ndif../*.** Find
10913 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d the current tim
10914 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 e (in Universal
10915 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 Coordinated Time
10916 29 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a ). Write into *
10917 70 69 4e 6f 77 0a 2a 2a 20 74 68 65 20 63 75 72 piNow.** the cur
10918 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 rent time and da
10919 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 te as a Julian D
1091a 61 79 20 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 ay number times
1091b 38 36 5f 34 30 30 5f 30 30 30 2e 20 20 49 6e 0a 86_400_000. In.
1091c 2a 2a 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 ** other words,
1091d 77 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 4e 6f write into *piNo
1091e 77 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 w the number of
1091f 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73 69 6e milliseconds sin
10920 63 65 20 74 68 65 20 4a 75 6c 69 61 6e 0a 2a 2a ce the Julian.**
10921 20 65 70 6f 63 68 20 6f 66 20 6e 6f 6f 6e 20 69 epoch of noon i
10922 6e 20 47 72 65 65 6e 77 69 63 68 20 6f 6e 20 4e n Greenwich on N
10923 6f 76 65 6d 62 65 72 20 32 34 2c 20 34 37 31 34 ovember 24, 4714
10924 20 42 2e 43 20 61 63 63 6f 72 64 69 6e 67 20 74 B.C according t
10925 6f 20 74 68 65 0a 2a 2a 20 70 72 6f 6c 65 70 74 o the.** prolept
10926 69 63 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c ic Gregorian cal
10927 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 endar..**.** On
10928 73 75 63 63 65 73 73 2c 20 72 65 74 75 72 6e 20 success, return
10929 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 52 65 74 75 SQLITE_OK. Retu
1092a 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 rn SQLITE_ERROR
1092b 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 if the time and
1092c 64 61 74 65 20 0a 2a 2a 20 63 61 6e 6e 6f 74 20 date .** cannot
1092d 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 be found..*/.sta
1092e 74 69 63 20 69 6e 74 20 75 6e 69 78 43 75 72 72 tic int unixCurr
1092f 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 73 71 6c entTimeInt64(sql
10930 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 ite3_vfs *NotUse
10931 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 d, sqlite3_int64
10932 20 2a 70 69 4e 6f 77 29 7b 0a 20 20 73 74 61 74 *piNow){. stat
10933 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
10934 5f 69 6e 74 36 34 20 75 6e 69 78 45 70 6f 63 68 _int64 unixEpoch
10935 20 3d 20 32 34 34 30 35 38 37 35 2a 28 73 71 6c = 24405875*(sql
10936 69 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30 30 ite3_int64)86400
10937 30 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 00;. int rc = S
10938 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 20 64 65 QLITE_OK;.#if de
10939 66 69 6e 65 64 28 4e 4f 5f 47 45 54 54 4f 44 29 fined(NO_GETTOD)
1093a 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 . time_t t;. t
1093b 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70 69 4e 6f ime(&t);. *piNo
1093c 77 20 3d 20 28 28 73 71 6c 69 74 65 33 5f 69 6e w = ((sqlite3_in
1093d 74 36 34 29 74 29 2a 31 30 30 30 20 2b 20 75 6e t64)t)*1000 + un
1093e 69 78 45 70 6f 63 68 3b 0a 23 65 6c 69 66 20 4f ixEpoch;.#elif O
1093f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 S_VXWORKS. stru
10940 63 74 20 74 69 6d 65 73 70 65 63 20 73 4e 6f 77 ct timespec sNow
10941 3b 0a 20 20 63 6c 6f 63 6b 5f 67 65 74 74 69 6d ;. clock_gettim
10942 65 28 43 4c 4f 43 4b 5f 52 45 41 4c 54 49 4d 45 e(CLOCK_REALTIME
10943 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 2a 70 69 4e , &sNow);. *piN
10944 6f 77 20 3d 20 75 6e 69 78 45 70 6f 63 68 20 2b ow = unixEpoch +
10945 20 31 30 30 30 2a 28 73 71 6c 69 74 65 33 5f 69 1000*(sqlite3_i
10946 6e 74 36 34 29 73 4e 6f 77 2e 74 76 5f 73 65 63 nt64)sNow.tv_sec
10947 20 2b 20 73 4e 6f 77 2e 74 76 5f 6e 73 65 63 2f + sNow.tv_nsec/
10948 31 30 30 30 30 30 30 3b 0a 23 65 6c 73 65 0a 20 1000000;.#else.
10949 20 73 74 72 75 63 74 20 74 69 6d 65 76 61 6c 20 struct timeval
1094a 73 4e 6f 77 3b 0a 20 20 69 66 28 20 67 65 74 74 sNow;. if( gett
1094b 69 6d 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c 20 imeofday(&sNow,
1094c 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 69 0)==0 ){. *pi
1094d 4e 6f 77 20 3d 20 75 6e 69 78 45 70 6f 63 68 20 Now = unixEpoch
1094e 2b 20 31 30 30 30 2a 28 73 71 6c 69 74 65 33 5f + 1000*(sqlite3_
1094f 69 6e 74 36 34 29 73 4e 6f 77 2e 74 76 5f 73 65 int64)sNow.tv_se
10950 63 20 2b 20 73 4e 6f 77 2e 74 76 5f 75 73 65 63 c + sNow.tv_usec
10951 2f 31 30 30 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a /1000;. }else{.
10952 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
10953 45 52 52 4f 52 3b 0a 20 20 7d 0a 23 65 6e 64 69 ERROR;. }.#endi
10954 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
10955 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 _TEST. if( sqli
10956 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 te3_current_time
10957 20 29 7b 0a 20 20 20 20 2a 70 69 4e 6f 77 20 3d ){. *piNow =
10958 20 31 30 30 30 2a 28 73 71 6c 69 74 65 33 5f 69 1000*(sqlite3_i
10959 6e 74 36 34 29 73 71 6c 69 74 65 33 5f 63 75 72 nt64)sqlite3_cur
1095a 72 65 6e 74 5f 74 69 6d 65 20 2b 20 75 6e 69 78 rent_time + unix
1095b 45 70 6f 63 68 3b 0a 20 20 7d 0a 23 65 6e 64 69 Epoch;. }.#endi
1095c 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d f. UNUSED_PARAM
1095d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
1095e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1095f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 *.** Find the cu
10960 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 rrent time (in U
10961 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e niversal Coordin
10962 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 ated Time). Wri
10963 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e te the.** curren
10964 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 t time and date
10965 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 as a Julian Day
10966 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e number into *prN
10967 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e ow and.** return
10968 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 0. Return 1 if
10969 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 the time and da
1096a 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 te cannot be fou
1096b 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e nd..*/.static in
1096c 74 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d t unixCurrentTim
1096d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e e(sqlite3_vfs *N
1096e 6f 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 20 2a otUsed, double *
1096f 70 72 4e 6f 77 29 7b 0a 20 20 73 71 6c 69 74 65 prNow){. sqlite
10970 33 5f 69 6e 74 36 34 20 69 20 3d 20 30 3b 0a 20 3_int64 i = 0;.
10971 20 69 6e 74 20 72 63 3b 0a 20 20 55 4e 55 53 45 int rc;. UNUSE
10972 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
10973 73 65 64 29 3b 0a 20 20 72 63 20 3d 20 75 6e 69 sed);. rc = uni
10974 78 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 xCurrentTimeInt6
10975 34 28 30 2c 20 26 69 29 3b 0a 20 20 2a 70 72 4e 4(0, &i);. *prN
10976 6f 77 20 3d 20 69 2f 38 36 34 30 30 30 30 30 2e ow = i/86400000.
10977 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0;. return rc;.
10978 7d 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65 }../*.** We adde
10979 64 20 74 68 65 20 78 47 65 74 4c 61 73 74 45 72 d the xGetLastEr
1097a 72 6f 72 28 29 20 6d 65 74 68 6f 64 20 77 69 74 ror() method wit
1097b 68 20 74 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20 h the intention
1097c 6f 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20 of providing.**
1097d 62 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c better low-level
1097e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 error messages
1097f 77 68 65 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73 when operating-s
10980 79 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63 ystem problems c
10981 6f 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67 ome up.** during
10982 20 53 51 4c 69 74 65 20 6f 70 65 72 61 74 69 6f SQLite operatio
10983 6e 2e 20 20 42 75 74 20 73 6f 20 66 61 72 2c 20 n. But so far,
10984 6e 6f 6e 65 20 6f 66 20 74 68 61 74 20 68 61 73 none of that has
10985 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 been implemente
10986 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 65 d.** in the core
10987 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 . So this routi
10988 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c ne is never call
10989 65 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74 ed. For now, it
1098a 20 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20 is merely.** a
1098b 70 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f place-holder..*/
1098c 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
1098d 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c GetLastError(sql
1098e 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 ite3_vfs *NotUse
1098f 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c d, int NotUsed2,
10990 20 63 68 61 72 20 2a 4e 6f 74 55 73 65 64 33 29 char *NotUsed3)
10991 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
10992 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
10993 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
10994 52 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 55 R(NotUsed2);. U
10995 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
10996 4e 6f 74 55 73 65 64 33 29 3b 0a 20 20 72 65 74 NotUsed3);. ret
10997 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a urn 0;.}.../*.**
10998 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10999 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 ****** End of sq
1099a 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 lite3_vfs method
1099b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s **************
1099c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
1099d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1099e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1099f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
109a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
109a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
109a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
109a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
109a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
109a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
109a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
109a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
109a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
109a9 50 72 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a Proxy Locking **
109aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
109ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
109ac 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 *.** Proxy locki
109ad 6e 67 20 69 73 20 61 20 22 75 62 65 72 2d 6c 6f ng is a "uber-lo
109ae 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69 6e cking-method" in
109af 20 74 68 69 73 20 73 65 6e 73 65 3a 20 20 49 74 this sense: It
109b0 20 75 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74 68 uses the.** oth
109b1 65 72 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f er locking metho
109b2 64 73 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79 20 ds on secondary
109b3 6c 6f 63 6b 20 66 69 6c 65 73 2e 20 20 50 72 6f lock files. Pro
109b4 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 0a xy locking is a.
109b5 2a 2a 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f 76 ** meta-layer ov
109b6 65 72 20 74 6f 70 20 6f 66 20 74 68 65 20 70 72 er top of the pr
109b7 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 imitive locking
109b8 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f 76 implemented abov
109b9 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73 20 e. For.** this
109ba 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 reason, the divi
109bb 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d sion that implem
109bc 65 6e 74 73 20 6f 66 20 70 72 6f 78 79 20 6c 6f ents of proxy lo
109bd 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 cking is deferre
109be 64 0a 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65 20 d.** until late
109bf 69 6e 20 74 68 65 20 66 69 6c 65 20 28 68 65 72 in the file (her
109c0 65 29 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 e) after all of
109c1 74 68 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d 65 the other I/O me
109c2 74 68 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62 65 thods have.** be
109c3 65 6e 20 64 65 66 69 6e 65 64 20 2d 20 73 6f 20 en defined - so
109c4 74 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 that the primiti
109c5 76 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f ve locking metho
109c6 64 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 ds are available
109c7 0a 2a 2a 20 61 73 20 73 65 72 76 69 63 65 73 20 .** as services
109c8 74 6f 20 68 65 6c 70 20 77 69 74 68 20 74 68 65 to help with the
109c9 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
109ca 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 of proxy locking
109cb 2e 0a 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 ..**.****.**.**
109cc 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b The default lock
109cd 69 6e 67 20 73 63 68 65 6d 65 73 20 69 6e 20 53 ing schemes in S
109ce 51 4c 69 74 65 20 75 73 65 20 62 79 74 65 2d 72 QLite use byte-r
109cf 61 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 ange locks on th
109d0 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 e.** database fi
109d1 6c 65 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 le to coordinate
109d2 20 73 61 66 65 2c 20 63 6f 6e 63 75 72 72 65 6e safe, concurren
109d3 74 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c 74 t access by mult
109d4 69 70 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a 20 iple readers.**
109d5 61 6e 64 20 77 72 69 74 65 72 73 20 5b 68 74 74 and writers [htt
109d6 70 3a 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f 6c p://sqlite.org/l
109d7 6f 63 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 ockingv3.html].
109d8 20 54 68 65 20 66 69 76 65 20 66 69 6c 65 20 6c The five file l
109d9 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 ocking.** states
109da 20 28 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 (UNLOCKED, PEND
109db 49 4e 47 2c 20 53 48 41 52 45 44 2c 20 52 45 53 ING, SHARED, RES
109dc 45 52 56 45 44 2c 20 45 58 43 4c 55 53 49 56 45 ERVED, EXCLUSIVE
109dd 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 ) are implemente
109de 64 0a 2a 2a 20 61 73 20 50 4f 53 49 58 20 72 65 d.** as POSIX re
109df 61 64 20 26 20 77 72 69 74 65 20 6c 6f 63 6b 73 ad & write locks
109e0 20 6f 76 65 72 20 66 69 78 65 64 20 73 65 74 20 over fixed set
109e1 6f 66 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76 69 of locations (vi
109e2 61 20 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 a fsctl),.** on
109e3 41 46 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 AFP and SMB only
109e4 20 65 78 63 6c 75 73 69 76 65 20 62 79 74 65 2d exclusive byte-
109e5 72 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 range locks are
109e6 61 76 61 69 6c 61 62 6c 65 20 76 69 61 20 66 73 available via fs
109e7 63 74 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f 57 ctl.** with _IOW
109e8 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 R('z', 23, struc
109e9 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 t ByteRangeLockP
109ea 42 32 29 20 74 6f 20 74 72 61 63 6b 20 74 68 65 B2) to track the
109eb 20 73 61 6d 65 20 35 20 73 74 61 74 65 73 2e 0a same 5 states..
109ec 2a 2a 20 54 6f 20 73 69 6d 75 6c 61 74 65 20 61 ** To simulate a
109ed 20 46 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65 20 F_RDLCK on the
109ee 73 68 61 72 65 64 20 72 61 6e 67 65 2c 20 6f 6e shared range, on
109ef 20 41 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79 20 AFP a randomly
109f0 73 65 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64 72 selected.** addr
109f1 65 73 73 20 69 6e 20 74 68 65 20 73 68 61 72 65 ess in the share
109f2 64 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e d range is taken
109f3 20 66 6f 72 20 61 20 53 48 41 52 45 44 20 6c 6f for a SHARED lo
109f4 63 6b 2c 20 74 68 65 20 65 6e 74 69 72 65 0a 2a ck, the entire.*
109f5 2a 20 73 68 61 72 65 64 20 72 61 6e 67 65 20 69 * shared range i
109f6 73 20 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20 45 s taken for an E
109f7 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a 0a XCLUSIVE lock):.
109f8 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44 49 **.** PENDI
109f9 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20 20 30 NG_BYTE 0
109fa 78 34 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20 x40000000.**
109fb 20 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 RESERVED_BYTE
109fc 20 20 20 20 20 20 30 78 34 30 30 30 30 30 30 31 0x40000001
109fd 0a 2a 2a 20 20 20 20 20 20 53 48 41 52 45 44 5f .** SHARED_
109fe 52 41 4e 47 45 20 20 20 20 20 20 20 20 30 78 34 RANGE 0x4
109ff 30 30 30 30 30 30 32 20 2d 3e 20 30 78 34 30 30 0000002 -> 0x400
10a00 30 30 32 30 30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 00200.**.** This
10a01 20 77 6f 72 6b 73 20 77 65 6c 6c 20 6f 6e 20 74 works well on t
10a02 68 65 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 he local file sy
10a03 73 74 65 6d 2c 20 62 75 74 20 73 68 6f 77 73 20 stem, but shows
10a04 61 20 6e 65 61 72 6c 79 20 31 30 30 78 0a 2a 2a a nearly 100x.**
10a05 20 73 6c 6f 77 64 6f 77 6e 20 69 6e 20 72 65 61 slowdown in rea
10a06 64 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 6e d performance on
10a07 20 41 46 50 20 62 65 63 61 75 73 65 20 74 68 65 AFP because the
10a08 20 41 46 50 20 63 6c 69 65 6e 74 20 64 69 73 61 AFP client disa
10a09 62 6c 65 73 0a 2a 2a 20 74 68 65 20 72 65 61 64 bles.** the read
10a0a 20 63 61 63 68 65 20 77 68 65 6e 20 62 79 74 65 cache when byte
10a0b 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 -range locks are
10a0c 20 70 72 65 73 65 6e 74 2e 20 20 45 6e 61 62 6c present. Enabl
10a0d 69 6e 67 20 74 68 65 20 72 65 61 64 0a 2a 2a 20 ing the read.**
10a0e 63 61 63 68 65 20 65 78 70 6f 73 65 73 20 61 20 cache exposes a
10a0f 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 20 cache coherency
10a10 70 72 6f 62 6c 65 6d 20 74 68 61 74 20 69 73 20 problem that is
10a11 70 72 65 73 65 6e 74 20 6f 6e 20 61 6c 6c 20 4f present on all O
10a12 53 20 58 0a 2a 2a 20 73 75 70 70 6f 72 74 65 64 S X.** supported
10a13 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 20 73 79 network file sy
10a14 73 74 65 6d 73 2e 20 20 4e 46 53 20 61 6e 64 20 stems. NFS and
10a15 41 46 50 20 62 6f 74 68 20 6f 62 73 65 72 76 65 AFP both observe
10a16 20 74 68 65 0a 2a 2a 20 63 6c 6f 73 65 2d 74 6f the.** close-to
10a17 2d 6f 70 65 6e 20 73 65 6d 61 6e 74 69 63 73 20 -open semantics
10a18 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 63 61 63 for ensuring cac
10a19 68 65 20 63 6f 68 65 72 65 6e 63 79 0a 2a 2a 20 he coherency.**
10a1a 5b 68 74 74 70 3a 2f 2f 6e 66 73 2e 73 6f 75 72 [http://nfs.sour
10a1b 63 65 66 6f 72 67 65 2e 6e 65 74 2f 23 66 61 71 ceforge.net/#faq
10a1c 5f 61 38 5d 2c 20 77 68 69 63 68 20 64 6f 65 73 _a8], which does
10a1d 20 6e 6f 74 20 65 66 66 65 63 74 69 76 65 6c 79 not effectively
10a1e 0a 2a 2a 20 61 64 64 72 65 73 73 20 74 68 65 20 .** address the
10a1f 72 65 71 75 69 72 65 6d 65 6e 74 73 20 66 6f 72 requirements for
10a20 20 63 6f 6e 63 75 72 72 65 6e 74 20 64 61 74 61 concurrent data
10a21 62 61 73 65 20 61 63 63 65 73 73 20 62 79 20 6d base access by m
10a22 75 6c 74 69 70 6c 65 0a 2a 2a 20 72 65 61 64 65 ultiple.** reade
10a23 72 73 20 61 6e 64 20 77 72 69 74 65 72 73 0a 2a rs and writers.*
10a24 2a 20 5b 68 74 74 70 3a 2f 2f 77 77 77 2e 6e 61 * [http://www.na
10a25 62 62 6c 65 2e 63 6f 6d 2f 53 51 4c 69 74 65 2d bble.com/SQLite-
10a26 6f 6e 2d 4e 46 53 2d 63 61 63 68 65 2d 63 6f 68 on-NFS-cache-coh
10a27 65 72 65 6e 63 79 2d 74 64 31 35 36 35 35 37 30 erency-td1565570
10a28 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a 0a 2a 2a 20 54 1.html]..**.** T
10a29 6f 20 61 64 64 72 65 73 73 20 74 68 65 20 70 65 o address the pe
10a2a 72 66 6f 72 6d 61 6e 63 65 20 61 6e 64 20 63 61 rformance and ca
10a2b 63 68 65 20 63 6f 68 65 72 65 6e 63 79 20 69 73 che coherency is
10a2c 73 75 65 73 2c 20 70 72 6f 78 79 20 66 69 6c 65 sues, proxy file
10a2d 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 63 68 61 6e locking.** chan
10a2e 67 65 73 20 74 68 65 20 77 61 79 20 64 61 74 61 ges the way data
10a2f 62 61 73 65 20 61 63 63 65 73 73 20 69 73 20 63 base access is c
10a30 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 6c 69 6d ontrolled by lim
10a31 69 74 69 6e 67 20 61 63 63 65 73 73 20 74 6f 20 iting access to
10a32 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 68 6f 73 74 a.** single host
10a33 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 20 6d at a time and m
10a34 6f 76 69 6e 67 20 66 69 6c 65 20 6c 6f 63 6b 73 oving file locks
10a35 20 6f 66 66 20 6f 66 20 74 68 65 20 64 61 74 61 off of the data
10a36 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 base file.** and
10a37 20 6f 6e 74 6f 20 61 20 70 72 6f 78 79 20 66 69 onto a proxy fi
10a38 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 le on the local
10a39 66 69 6c 65 20 73 79 73 74 65 6d 2e 20 20 0a 2a file system. .*
10a3a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69 6e 67 20 70 72 *.**.** Using pr
10a3b 6f 78 79 20 6c 6f 63 6b 73 0a 2a 2a 20 2d 2d 2d oxy locks.** ---
10a3c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a --------------.*
10a3d 2a 0a 2a 2a 20 43 20 41 50 49 73 0a 2a 2a 0a 2a *.** C APIs.**.*
10a3e 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f * sqlite3_file_
10a3f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61 control(db, dbna
10a40 6d 65 2c 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c me, SQLITE_SET_L
10a41 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 0a 2a 2a OCKPROXYFILE,.**
10a42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10a43 20 20 20 20 20 20 20 3c 70 72 6f 78 79 5f 70 61 <proxy_pa
10a44 74 68 3e 20 7c 20 22 3a 61 75 74 6f 3a 22 29 3b th> | ":auto:");
10a45 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c .** sqlite3_fil
10a46 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62 e_control(db, db
10a47 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 47 45 54 name, SQLITE_GET
10a48 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 _LOCKPROXYFILE,
10a49 26 3c 70 72 6f 78 79 5f 70 61 74 68 3e 29 3b 0a &<proxy_path>);.
10a4a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51 4c 20 70 72 61 **.**.** SQL pra
10a4b 67 6d 61 73 0a 2a 2a 0a 2a 2a 20 20 50 52 41 47 gmas.**.** PRAG
10a4c 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f MA [database.]lo
10a4d 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 3d 3c 70 ck_proxy_file=<p
10a4e 72 6f 78 79 5f 70 61 74 68 3e 20 7c 20 3a 61 75 roxy_path> | :au
10a4f 74 6f 3a 0a 2a 2a 20 20 50 52 41 47 4d 41 20 5b to:.** PRAGMA [
10a50 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 database.]lock_p
10a51 72 6f 78 79 5f 66 69 6c 65 0a 2a 2a 0a 2a 2a 20 roxy_file.**.**
10a52 53 70 65 63 69 66 79 69 6e 67 20 22 3a 61 75 74 Specifying ":aut
10a53 6f 3a 22 20 6d 65 61 6e 73 20 74 68 61 74 20 69 o:" means that i
10a54 66 20 74 68 65 72 65 20 69 73 20 61 20 63 6f 6e f there is a con
10a55 63 68 20 66 69 6c 65 20 77 69 74 68 20 61 20 6d ch file with a m
10a56 61 74 63 68 69 6e 67 0a 2a 2a 20 68 6f 73 74 20 atching.** host
10a57 49 44 20 69 6e 20 69 74 2c 20 74 68 65 20 70 72 ID in it, the pr
10a58 6f 78 79 20 70 61 74 68 20 69 6e 20 74 68 65 20 oxy path in the
10a59 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 6c 6c 20 conch file will
10a5a 62 65 20 75 73 65 64 2c 20 6f 74 68 65 72 77 69 be used, otherwi
10a5b 73 65 0a 2a 2a 20 61 20 70 72 6f 78 79 20 70 61 se.** a proxy pa
10a5c 74 68 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 th based on the
10a5d 75 73 65 72 27 73 20 74 65 6d 70 20 64 69 72 0a user's temp dir.
10a5e 2a 2a 20 28 76 69 61 20 63 6f 6e 66 73 74 72 28 ** (via confstr(
10a5f 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f _CS_DARWIN_USER_
10a60 54 45 4d 50 5f 44 49 52 2c 2e 2e 2e 29 29 20 77 TEMP_DIR,...)) w
10a61 69 6c 6c 20 62 65 20 75 73 65 64 20 61 6e 64 20 ill be used and
10a62 74 68 65 0a 2a 2a 20 61 63 74 75 61 6c 20 70 72 the.** actual pr
10a63 6f 78 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 oxy file name is
10a64 20 67 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 generated from
10a65 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 70 61 74 the name and pat
10a66 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 h of the.** data
10a67 62 61 73 65 20 66 69 6c 65 2e 20 20 46 6f 72 20 base file. For
10a68 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 example:.**.**
10a69 20 20 20 20 20 46 6f 72 20 64 61 74 61 62 61 73 For databas
10a6a 65 20 70 61 74 68 20 22 2f 55 73 65 72 73 2f 6d e path "/Users/m
10a6b 65 2f 66 6f 6f 2e 64 62 22 20 0a 2a 2a 20 20 20 e/foo.db" .**
10a6c 20 20 20 20 54 68 65 20 6c 6f 63 6b 20 70 61 74 The lock pat
10a6d 68 20 77 69 6c 6c 20 62 65 20 22 3c 74 6d 70 64 h will be "<tmpd
10a6e 69 72 3e 2f 73 71 6c 69 74 65 70 6c 6f 63 6b 73 ir>/sqliteplocks
10a6f 2f 5f 55 73 65 72 73 5f 6d 65 5f 66 6f 6f 2e 64 /_Users_me_foo.d
10a70 62 3a 61 75 74 6f 3a 22 29 0a 2a 2a 0a 2a 2a 20 b:auto:").**.**
10a71 4f 6e 63 65 20 61 20 6c 6f 63 6b 20 70 72 6f 78 Once a lock prox
10a72 79 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 20 y is configured
10a73 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 63 for a database c
10a74 6f 6e 6e 65 63 74 69 6f 6e 2c 20 69 74 20 63 61 onnection, it ca
10a75 6e 20 6e 6f 74 0a 2a 2a 20 62 65 20 72 65 6d 6f n not.** be remo
10a76 76 65 64 2c 20 68 6f 77 65 76 65 72 20 69 74 20 ved, however it
10a77 6d 61 79 20 62 65 20 73 77 69 74 63 68 65 64 20 may be switched
10a78 74 6f 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 to a different p
10a79 72 6f 78 79 20 70 61 74 68 20 76 69 61 0a 2a 2a roxy path via.**
10a7a 20 74 68 65 20 61 62 6f 76 65 20 41 50 49 73 20 the above APIs
10a7b 28 61 73 73 75 6d 69 6e 67 20 74 68 65 20 63 6f (assuming the co
10a7c 6e 63 68 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 nch file is not
10a7d 62 65 69 6e 67 20 68 65 6c 64 20 62 79 20 61 6e being held by an
10a7e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 other.** connect
10a7f 69 6f 6e 20 6f 72 20 70 72 6f 63 65 73 73 29 2e ion or process).
10a80 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 48 6f 77 20 70 .**.**.** How p
10a81 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 roxy locking wor
10a82 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ks.** ----------
10a83 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a -------------.**
10a84 0a 2a 2a 20 50 72 6f 78 79 20 66 69 6c 65 20 6c .** Proxy file l
10a85 6f 63 6b 69 6e 67 20 72 65 6c 69 65 73 20 70 72 ocking relies pr
10a86 69 6d 61 72 69 6c 79 20 6f 6e 20 74 77 6f 20 6e imarily on two n
10a87 65 77 20 73 75 70 70 6f 72 74 69 6e 67 20 66 69 ew supporting fi
10a88 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 les: .**.** *
10a89 20 63 6f 6e 63 68 20 66 69 6c 65 20 74 6f 20 6c conch file to l
10a8a 69 6d 69 74 20 61 63 63 65 73 73 20 74 6f 20 74 imit access to t
10a8b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
10a8c 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 68 6f 73 to a single hos
10a8d 74 0a 2a 2a 20 20 20 20 20 20 61 74 20 61 20 74 t.** at a t
10a8e 69 6d 65 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 70 ime.**.** * p
10a8f 72 6f 78 79 20 66 69 6c 65 20 74 6f 20 61 63 74 roxy file to act
10a90 20 61 73 20 61 20 70 72 6f 78 79 20 66 6f 72 20 as a proxy for
10a91 74 68 65 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 the advisory loc
10a92 6b 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 20 ks normally.**
10a93 20 20 20 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 taken on the
10a94 20 64 61 74 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20 database.**.**
10a95 54 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2d The conch file -
10a96 20 74 6f 20 75 73 65 20 61 20 70 72 6f 78 79 20 to use a proxy
10a97 66 69 6c 65 2c 20 73 71 6c 69 74 65 20 6d 75 73 file, sqlite mus
10a98 74 20 66 69 72 73 74 20 22 68 6f 6c 64 20 74 68 t first "hold th
10a99 65 20 63 6f 6e 63 68 22 0a 2a 2a 20 62 79 20 74 e conch".** by t
10a9a 61 6b 69 6e 67 20 61 6e 20 73 71 6c 69 74 65 2d aking an sqlite-
10a9b 73 74 79 6c 65 20 73 68 61 72 65 64 20 6c 6f 63 style shared loc
10a9c 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 k on the conch f
10a9d 69 6c 65 2c 20 72 65 61 64 69 6e 67 20 74 68 65 ile, reading the
10a9e 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 61 6e 64 .** contents and
10a9f 20 63 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 68 comparing the h
10aa0 6f 73 74 27 73 20 75 6e 69 71 75 65 20 68 6f 73 ost's unique hos
10aa1 74 20 49 44 20 28 73 65 65 20 62 65 6c 6f 77 29 t ID (see below)
10aa2 20 61 6e 64 20 6c 6f 63 6b 0a 2a 2a 20 70 72 6f and lock.** pro
10aa3 78 79 20 70 61 74 68 20 61 67 61 69 6e 73 74 20 xy path against
10aa4 74 68 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65 the values store
10aa5 64 20 69 6e 20 74 68 65 20 63 6f 6e 63 68 2e 20 d in the conch.
10aa6 20 54 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 The conch file
10aa7 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 is.** stored in
10aa8 74 68 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f the same directo
10aa9 72 79 20 61 73 20 74 68 65 20 64 61 74 61 62 61 ry as the databa
10aaa 73 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 se file and the
10aab 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 file name.** is
10aac 70 61 74 74 65 72 6e 65 64 20 61 66 74 65 72 20 patterned after
10aad 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
10aae 65 20 6e 61 6d 65 20 61 73 20 22 2e 3c 64 61 74 e name as ".<dat
10aaf 61 62 61 73 65 6e 61 6d 65 3e 2d 63 6f 6e 63 68 abasename>-conch
10ab0 22 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6e "..** If the con
10ab1 63 68 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 ch file does not
10ab2 20 65 78 69 73 74 2c 20 6f 72 20 69 74 27 73 20 exist, or it's
10ab3 63 6f 6e 74 65 6e 74 73 20 64 6f 20 6e 6f 74 20 contents do not
10ab4 6d 61 74 63 68 20 74 68 65 0a 2a 2a 20 68 6f 73 match the.** hos
10ab5 74 20 49 44 20 61 6e 64 2f 6f 72 20 70 72 6f 78 t ID and/or prox
10ab6 79 20 70 61 74 68 2c 20 74 68 65 6e 20 74 68 65 y path, then the
10ab7 20 6c 6f 63 6b 20 69 73 20 65 73 63 61 6c 61 74 lock is escalat
10ab8 65 64 20 74 6f 20 61 6e 20 65 78 63 6c 75 73 69 ed to an exclusi
10ab9 76 65 0a 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 74 ve.** lock and t
10aba 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 63 6f he conch file co
10abb 6e 74 65 6e 74 73 20 69 73 20 75 70 64 61 74 65 ntents is update
10abc 64 20 77 69 74 68 20 74 68 65 20 68 6f 73 74 20 d with the host
10abd 49 44 20 61 6e 64 20 70 72 6f 78 79 0a 2a 2a 20 ID and proxy.**
10abe 70 61 74 68 20 61 6e 64 20 74 68 65 20 6c 6f 63 path and the loc
10abf 6b 20 69 73 20 64 6f 77 6e 67 72 61 64 65 64 20 k is downgraded
10ac0 74 6f 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b to a shared lock
10ac1 20 61 67 61 69 6e 2e 20 20 49 66 20 74 68 65 20 again. If the
10ac2 63 6f 6e 63 68 0a 2a 2a 20 69 73 20 68 65 6c 64 conch.** is held
10ac3 20 62 79 20 61 6e 6f 74 68 65 72 20 70 72 6f 63 by another proc
10ac4 65 73 73 20 28 77 69 74 68 20 61 20 73 68 61 72 ess (with a shar
10ac5 65 64 20 6c 6f 63 6b 29 2c 20 74 68 65 20 65 78 ed lock), the ex
10ac6 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 clusive lock.**
10ac7 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 53 51 will fail and SQ
10ac8 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 LITE_BUSY is ret
10ac9 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 urned..**.** The
10aca 20 70 72 6f 78 79 20 66 69 6c 65 20 2d 20 61 20 proxy file - a
10acb 73 69 6e 67 6c 65 2d 62 79 74 65 20 66 69 6c 65 single-byte file
10acc 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 61 64 used for all ad
10acd 76 69 73 6f 72 79 20 66 69 6c 65 20 6c 6f 63 6b visory file lock
10ace 73 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 74 61 s.** normally ta
10acf 6b 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ken on the datab
10ad0 61 73 65 20 66 69 6c 65 2e 20 20 20 54 68 69 73 ase file. This
10ad1 20 61 6c 6c 6f 77 73 20 66 6f 72 20 73 61 66 65 allows for safe
10ad2 20 73 68 61 72 69 6e 67 0a 2a 2a 20 6f 66 20 74 sharing.** of t
10ad3 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
10ad4 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 20 72 65 for multiple re
10ad5 61 64 65 72 73 20 61 6e 64 20 77 72 69 74 65 72 aders and writer
10ad6 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a s on the same.**
10ad7 20 68 6f 73 74 20 28 74 68 65 20 63 6f 6e 63 68 host (the conch
10ad8 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 74 68 ensures that th
10ad9 65 79 20 61 6c 6c 20 75 73 65 20 74 68 65 20 73 ey all use the s
10ada 61 6d 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 ame local lock f
10adb 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 ile)..**.** Requ
10adc 65 73 74 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 esting the lock
10add 70 72 6f 78 79 20 64 6f 65 73 20 6e 6f 74 20 69 proxy does not i
10ade 6d 6d 65 64 69 61 74 65 6c 79 20 74 61 6b 65 20 mmediately take
10adf 74 68 65 20 63 6f 6e 63 68 2c 20 69 74 20 69 73 the conch, it is
10ae0 0a 2a 2a 20 6f 6e 6c 79 20 74 61 6b 65 6e 20 77 .** only taken w
10ae1 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 72 65 hen the first re
10ae2 71 75 65 73 74 20 74 6f 20 6c 6f 63 6b 20 64 61 quest to lock da
10ae3 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d tabase file is m
10ae4 61 64 65 2e 20 20 0a 2a 2a 20 54 68 69 73 20 6d ade. .** This m
10ae5 61 74 63 68 65 73 20 74 68 65 20 73 65 6d 61 6e atches the seman
10ae6 74 69 63 73 20 6f 66 20 74 68 65 20 74 72 61 64 tics of the trad
10ae7 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 itional locking
10ae8 62 65 68 61 76 69 6f 72 2c 20 77 68 65 72 65 0a behavior, where.
10ae9 2a 2a 20 6f 70 65 6e 69 6e 67 20 61 20 63 6f 6e ** opening a con
10aea 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 64 61 74 nection to a dat
10aeb 61 62 61 73 65 20 66 69 6c 65 20 64 6f 65 73 20 abase file does
10aec 6e 6f 74 20 74 61 6b 65 20 61 20 6c 6f 63 6b 20 not take a lock
10aed 6f 6e 20 69 74 2e 0a 2a 2a 20 54 68 65 20 73 68 on it..** The sh
10aee 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6e ared lock and an
10aef 20 6f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 open file descr
10af0 69 70 74 6f 72 20 61 72 65 20 6d 61 69 6e 74 61 iptor are mainta
10af1 69 6e 65 64 20 75 6e 74 69 6c 20 0a 2a 2a 20 74 ined until .** t
10af2 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f he connection to
10af3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
10af4 20 63 6c 6f 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 closed. .**.**
10af5 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65 20 61 The proxy file a
10af6 6e 64 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 nd the lock file
10af7 20 61 72 65 20 6e 65 76 65 72 20 64 65 6c 65 74 are never delet
10af8 65 64 20 73 6f 20 74 68 65 79 20 6f 6e 6c 79 20 ed so they only
10af9 6e 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 63 72 need.** to be cr
10afa 65 61 74 65 64 20 74 68 65 20 66 69 72 73 74 20 eated the first
10afb 74 69 6d 65 20 74 68 65 79 20 61 72 65 20 75 73 time they are us
10afc 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 ed..**.** Config
10afd 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a uration options.
10afe 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ** -------------
10aff 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 --------.**.**
10b00 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 SQLITE_PREFER_PR
10b01 4f 58 59 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a OXY_LOCKING.**.*
10b02 2a 20 20 20 20 20 20 20 44 61 74 61 62 61 73 65 * Database
10b03 20 66 69 6c 65 73 20 61 63 63 65 73 73 65 64 20 files accessed
10b04 6f 6e 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c on non-local fil
10b05 65 20 73 79 73 74 65 6d 73 20 61 72 65 0a 2a 2a e systems are.**
10b06 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 automatic
10b07 61 6c 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20 ally configured
10b08 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e for proxy lockin
10b09 67 2c 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 72 g, lock files ar
10b0a 65 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 64 e.** named
10b0b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 75 automatically u
10b0c 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f sing the same lo
10b0d 67 69 63 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 gic as.**
10b0e 50 52 41 47 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78 PRAGMA lock_prox
10b0f 79 5f 66 69 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a y_file=":auto:".
10b10 2a 2a 20 20 20 20 0a 2a 2a 20 20 53 51 4c 49 54 ** .** SQLIT
10b11 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 0a 2a 2a E_PROXY_DEBUG.**
10b12 0a 2a 2a 20 20 20 20 20 20 20 45 6e 61 62 6c 65 .** Enable
10b13 73 20 74 68 65 20 6c 6f 67 67 69 6e 67 20 6f 66 s the logging of
10b14 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 error messages
10b15 64 75 72 69 6e 67 20 68 6f 73 74 20 69 64 20 66 during host id f
10b16 69 6c 65 0a 2a 2a 20 20 20 20 20 20 20 72 65 74 ile.** ret
10b17 72 69 65 76 61 6c 20 61 6e 64 20 63 72 65 61 74 rieval and creat
10b18 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 4c 4f 43 4b 50 ion.**.** LOCKP
10b19 52 4f 58 59 44 49 52 0a 2a 2a 0a 2a 2a 20 20 20 ROXYDIR.**.**
10b1a 20 20 20 20 4f 76 65 72 72 69 64 65 73 20 74 68 Overrides th
10b1b 65 20 64 65 66 61 75 6c 74 20 64 69 72 65 63 74 e default direct
10b1c 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 ory used for loc
10b1d 6b 20 70 72 6f 78 79 20 66 69 6c 65 73 20 74 68 k proxy files th
10b1e 61 74 0a 2a 2a 20 20 20 20 20 20 20 61 72 65 20 at.** are
10b1f 6e 61 6d 65 64 20 61 75 74 6f 6d 61 74 69 63 61 named automatica
10b20 6c 6c 79 20 76 69 61 20 74 68 65 20 22 3a 61 75 lly via the ":au
10b21 74 6f 3a 22 20 73 65 74 74 69 6e 67 0a 2a 2a 0a to:" setting.**.
10b22 2a 2a 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ** SQLITE_DEFAU
10b23 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d LT_PROXYDIR_PERM
10b24 49 53 53 49 4f 4e 53 0a 2a 2a 0a 2a 2a 20 20 20 ISSIONS.**.**
10b25 20 20 20 20 50 65 72 6d 69 73 73 69 6f 6e 73 20 Permissions
10b26 74 6f 20 75 73 65 20 77 68 65 6e 20 63 72 65 61 to use when crea
10b27 74 69 6e 67 20 61 20 64 69 72 65 63 74 6f 72 79 ting a directory
10b28 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 for storing the
10b29 0a 2a 2a 20 20 20 20 20 20 20 6c 6f 63 6b 20 70 .** lock p
10b2a 72 6f 78 79 20 66 69 6c 65 73 2c 20 6f 6e 6c 79 roxy files, only
10b2b 20 75 73 65 64 20 77 68 65 6e 20 4c 4f 43 4b 50 used when LOCKP
10b2c 52 4f 58 59 44 49 52 20 69 73 20 6e 6f 74 20 73 ROXYDIR is not s
10b2d 65 74 2e 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 20 et..** .**
10b2e 20 0a 2a 2a 20 41 73 20 6d 65 6e 74 69 6f 6e 65 .** As mentione
10b2f 64 20 61 62 6f 76 65 2c 20 77 68 65 6e 20 63 6f d above, when co
10b30 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 mpiled with SQLI
10b31 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f TE_PREFER_PROXY_
10b32 4c 4f 43 4b 49 4e 47 2c 0a 2a 2a 20 73 65 74 74 LOCKING,.** sett
10b33 69 6e 67 20 74 68 65 20 65 6e 76 69 72 6f 6e 6d ing the environm
10b34 65 6e 74 20 76 61 72 69 61 62 6c 65 20 53 51 4c ent variable SQL
10b35 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f ITE_FORCE_PROXY_
10b36 4c 4f 43 4b 49 4e 47 20 74 6f 20 31 20 77 69 6c LOCKING to 1 wil
10b37 6c 0a 2a 2a 20 66 6f 72 63 65 20 70 72 6f 78 79 l.** force proxy
10b38 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 20 75 locking to be u
10b39 73 65 64 20 66 6f 72 20 65 76 65 72 79 20 64 61 sed for every da
10b3a 74 61 62 61 73 65 20 66 69 6c 65 20 6f 70 65 6e tabase file open
10b3b 65 64 2c 20 61 6e 64 20 30 0a 2a 2a 20 77 69 6c ed, and 0.** wil
10b3c 6c 20 66 6f 72 63 65 20 61 75 74 6f 6d 61 74 69 l force automati
10b3d 63 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 c proxy locking
10b3e 74 6f 20 62 65 20 64 69 73 61 62 6c 65 64 20 66 to be disabled f
10b3f 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 0a or all database.
10b40 2a 2a 20 66 69 6c 65 73 20 28 65 78 70 6c 69 63 ** files (explic
10b41 69 74 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 ity calling the
10b42 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 SQLITE_SET_LOCKP
10b43 52 4f 58 59 46 49 4c 45 20 70 72 61 67 6d 61 20 ROXYFILE pragma
10b44 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 5f 66 69 6c or.** sqlite_fil
10b45 65 5f 63 6f 6e 74 72 6f 6c 20 41 50 49 20 69 73 e_control API is
10b46 20 6e 6f 74 20 61 66 66 65 63 74 65 64 20 62 79 not affected by
10b47 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 SQLITE_FORCE_PR
10b48 4f 58 59 5f 4c 4f 43 4b 49 4e 47 29 2e 0a 2a 2f OXY_LOCKING)..*/
10b49 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f ../*.** Proxy lo
10b4a 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61 76 cking is only av
10b4b 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 ailable on MacOS
10b4c 58 20 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 X .*/.#if define
10b4d 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 d(__APPLE__) &&
10b4e 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
10b4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 0a 2f 2a 0a CKING_STYLE../*.
10b50 2a 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b ** The proxyLock
10b51 69 6e 67 43 6f 6e 74 65 78 74 20 68 61 73 20 74 ingContext has t
10b52 68 65 20 70 61 74 68 20 61 6e 64 20 66 69 6c 65 he path and file
10b53 20 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 structures for
10b54 74 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61 the remote .** a
10b55 6e 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66 nd local proxy f
10b56 69 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79 iles in it.*/.ty
10b57 70 65 64 65 66 20 73 74 72 75 63 74 20 70 72 6f pedef struct pro
10b58 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
10b59 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
10b5a 74 65 78 74 3b 0a 73 74 72 75 63 74 20 70 72 6f text;.struct pro
10b5b 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
10b5c 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 {. unixFile *c
10b5d 6f 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 onchFile;
10b5e 20 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20 /* Open conch
10b5f 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a file */. char *
10b60 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b 20 20 conchFilePath;
10b61 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
10b62 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 f the conch file
10b63 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a */. unixFile *
10b64 6c 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20 20 20 lockProxy;
10b65 20 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79 /* Open proxy
10b66 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 lock file */.
10b67 63 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50 char *lockProxyP
10b68 61 74 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ath; /*
10b69 4e 61 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78 Name of the prox
10b6a 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 y lock file */.
10b6b 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 char *dbPath;
10b6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10b6d 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 Name of the ope
10b6e 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 n file */. int
10b6f 63 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20 conchHeld;
10b70 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 69 66 /* 1 if
10b71 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 68 65 the conch is he
10b72 6c 64 2c 20 2d 31 20 69 66 20 6c 6f 63 6b 6c 65 ld, -1 if lockle
10b73 73 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c ss */. void *ol
10b74 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b dLockingContext;
10b75 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c /* Original
10b76 20 6c 6f 63 6b 69 6e 67 63 6f 6e 74 65 78 74 20 lockingcontext
10b77 74 6f 20 72 65 73 74 6f 72 65 20 6f 6e 20 63 6c to restore on cl
10b78 6f 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ose */. sqlite3
10b79 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 _io_methods cons
10b7a 74 20 2a 70 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 t *pOldMethod;
10b7b 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 49 /* Original I
10b7c 2f 4f 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 63 /O methods for c
10b7d 6c 6f 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a lose */.};../* .
10b7e 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 ** The proxy loc
10b7f 6b 20 66 69 6c 65 20 70 61 74 68 20 66 6f 72 20 k file path for
10b80 74 68 65 20 64 61 74 61 62 61 73 65 20 61 74 20 the database at
10b81 64 62 50 61 74 68 20 69 73 20 77 72 69 74 74 65 dbPath is writte
10b82 6e 20 69 6e 74 6f 20 6c 50 61 74 68 2c 20 0a 2a n into lPath, .*
10b83 2a 20 77 68 69 63 68 20 6d 75 73 74 20 70 6f 69 * which must poi
10b84 6e 74 20 74 6f 20 76 61 6c 69 64 2c 20 77 72 69 nt to valid, wri
10b85 74 61 62 6c 65 20 6d 65 6d 6f 72 79 20 6c 61 72 table memory lar
10b86 67 65 20 65 6e 6f 75 67 68 20 66 6f 72 20 61 20 ge enough for a
10b87 6d 61 78 4c 65 6e 20 6c 65 6e 67 74 68 0a 2a 2a maxLen length.**
10b88 20 66 69 6c 65 20 70 61 74 68 2e 20 0a 2a 2f 0a file path. .*/.
10b89 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
10b8a 47 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73 GetLockPath(cons
10b8b 74 20 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 t char *dbPath,
10b8c 63 68 61 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a char *lPath, siz
10b8d 65 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69 e_t maxLen){. i
10b8e 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62 nt len;. int db
10b8f 4c 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23 Len;. int i;..#
10b90 69 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44 ifdef LOCKPROXYD
10b91 49 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 IR. len = strlc
10b92 70 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52 py(lPath, LOCKPR
10b93 4f 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b OXYDIR, maxLen);
10b94 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 5f .#else.# ifdef _
10b95 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 CS_DARWIN_USER_T
10b96 45 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20 EMP_DIR. {.
10b97 69 66 28 20 21 63 6f 6e 66 73 74 72 28 5f 43 53 if( !confstr(_CS
10b98 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d _DARWIN_USER_TEM
10b99 50 5f 44 49 52 2c 20 6c 50 61 74 68 2c 20 6d 61 P_DIR, lPath, ma
10b9a 78 4c 65 6e 29 20 29 7b 0a 20 20 20 20 20 20 4f xLen) ){. O
10b9b 53 54 52 41 43 45 28 28 22 47 45 54 4c 4f 43 4b STRACE(("GETLOCK
10b9c 50 41 54 48 20 20 66 61 69 6c 65 64 20 25 73 20 PATH failed %s
10b9d 65 72 72 6e 6f 3d 25 64 20 70 69 64 3d 25 64 5c errno=%d pid=%d\
10b9e 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
10b9f 20 20 20 6c 50 61 74 68 2c 20 65 72 72 6e 6f 2c lPath, errno,
10ba0 20 67 65 74 70 69 64 28 29 29 29 3b 0a 20 20 20 getpid()));.
10ba1 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
10ba2 5f 49 4f 45 52 52 5f 4c 4f 43 4b 3b 0a 20 20 20 _IOERR_LOCK;.
10ba3 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 }. len = str
10ba4 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 73 71 6c lcat(lPath, "sql
10ba5 69 74 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c iteplocks", maxL
10ba6 65 6e 29 3b 20 20 20 20 0a 20 20 7d 0a 23 20 65 en); . }.# e
10ba7 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c lse. len = strl
10ba8 63 70 79 28 6c 50 61 74 68 2c 20 22 2f 74 6d 70 cpy(lPath, "/tmp
10ba9 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23 20 65 /", maxLen);.# e
10baa 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20 69 ndif.#endif.. i
10bab 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d 31 5d 21 f( lPath[len-1]!
10bac 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c 65 6e 20 ='/' ){. len
10bad 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c = strlcat(lPath,
10bae 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 "/", maxLen);.
10baf 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72 61 6e 73 }. . /* trans
10bb0 66 6f 72 6d 20 74 68 65 20 64 62 20 70 61 74 68 form the db path
10bb1 20 74 6f 20 61 20 75 6e 69 71 75 65 20 63 61 63 to a unique cac
10bb2 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 62 4c he name */. dbL
10bb3 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e en = (int)strlen
10bb4 28 64 62 50 61 74 68 29 3b 0a 20 20 66 6f 72 28 (dbPath);. for(
10bb5 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e 20 26 26 i=0; i<dbLen &&
10bb6 20 28 69 2b 6c 65 6e 2b 37 29 3c 28 69 6e 74 29 (i+len+7)<(int)
10bb7 6d 61 78 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 maxLen; i++){.
10bb8 20 20 63 68 61 72 20 63 20 3d 20 64 62 50 61 74 char c = dbPat
10bb9 68 5b 69 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b h[i];. lPath[
10bba 69 2b 6c 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 i+len] = (c=='/'
10bbb 29 3f 27 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c )?'_':c;. }. l
10bbc 50 61 74 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 Path[i+len]='\0'
10bbd 3b 0a 20 20 73 74 72 6c 63 61 74 28 6c 50 61 74 ;. strlcat(lPat
10bbe 68 2c 20 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 h, ":auto:", max
10bbf 4c 65 6e 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 Len);. OSTRACE(
10bc0 28 22 47 45 54 4c 4f 43 4b 50 41 54 48 20 20 70 ("GETLOCKPATH p
10bc1 72 6f 78 79 20 6c 6f 63 6b 20 70 61 74 68 3d 25 roxy lock path=%
10bc2 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c 50 61 s pid=%d\n", lPa
10bc3 74 68 2c 20 67 65 74 70 69 64 28 29 29 29 3b 0a th, getpid()));.
10bc4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
10bc5 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 20 2a 2a 20 43 OK;.}../* . ** C
10bc6 72 65 61 74 65 73 20 74 68 65 20 6c 6f 63 6b 20 reates the lock
10bc7 66 69 6c 65 20 61 6e 64 20 61 6e 79 20 6d 69 73 file and any mis
10bc8 73 69 6e 67 20 64 69 72 65 63 74 6f 72 69 65 73 sing directories
10bc9 20 69 6e 20 6c 6f 63 6b 50 61 74 68 0a 20 2a 2f in lockPath. */
10bca 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 .static int prox
10bcb 79 43 72 65 61 74 65 4c 6f 63 6b 50 61 74 68 28 yCreateLockPath(
10bcc 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f 63 6b const char *lock
10bcd 50 61 74 68 29 7b 0a 20 20 69 6e 74 20 69 2c 20 Path){. int i,
10bce 6c 65 6e 3b 0a 20 20 63 68 61 72 20 62 75 66 5b len;. char buf[
10bcf 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 20 69 MAXPATHLEN];. i
10bd0 6e 74 20 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 nt start = 0;.
10bd1 0a 20 20 61 73 73 65 72 74 28 6c 6f 63 6b 50 61 . assert(lockPa
10bd2 74 68 21 3d 4e 55 4c 4c 29 3b 0a 20 20 2f 2a 20 th!=NULL);. /*
10bd3 74 72 79 20 74 6f 20 63 72 65 61 74 65 20 61 6c try to create al
10bd4 6c 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 l the intermedia
10bd5 74 65 20 64 69 72 65 63 74 6f 72 69 65 73 20 2a te directories *
10bd6 2f 0a 20 20 6c 65 6e 20 3d 20 28 69 6e 74 29 73 /. len = (int)s
10bd7 74 72 6c 65 6e 28 6c 6f 63 6b 50 61 74 68 29 3b trlen(lockPath);
10bd8 0a 20 20 62 75 66 5b 30 5d 20 3d 20 6c 6f 63 6b . buf[0] = lock
10bd9 50 61 74 68 5b 30 5d 3b 0a 20 20 66 6f 72 28 20 Path[0];. for(
10bda 69 3d 31 3b 20 69 3c 6c 65 6e 3b 20 69 2b 2b 20 i=1; i<len; i++
10bdb 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 ){. if( lockP
10bdc 61 74 68 5b 69 5d 20 3d 3d 20 27 2f 27 20 26 26 ath[i] == '/' &&
10bdd 20 28 69 20 2d 20 73 74 61 72 74 20 3e 20 30 29 (i - start > 0)
10bde 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 6f 6e 6c ){. /* onl
10bdf 79 20 6d 6b 64 69 72 20 69 66 20 6c 65 61 66 20 y mkdir if leaf
10be0 64 69 72 20 21 3d 20 22 2e 22 20 6f 72 20 22 2f dir != "." or "/
10be1 22 20 6f 72 20 22 2e 2e 22 20 2a 2f 0a 20 20 20 " or ".." */.
10be2 20 20 20 69 66 28 20 69 2d 73 74 61 72 74 3e 32 if( i-start>2
10be3 20 7c 7c 20 28 69 2d 73 74 61 72 74 3d 3d 31 20 || (i-start==1
10be4 26 26 20 62 75 66 5b 73 74 61 72 74 5d 20 21 3d && buf[start] !=
10be5 20 27 2e 27 20 26 26 20 62 75 66 5b 73 74 61 72 '.' && buf[star
10be6 74 5d 20 21 3d 20 27 2f 27 29 20 0a 20 20 20 20 t] != '/') .
10be7 20 20 20 20 20 7c 7c 20 28 69 2d 73 74 61 72 74 || (i-start
10be8 3d 3d 32 20 26 26 20 62 75 66 5b 73 74 61 72 74 ==2 && buf[start
10be9 5d 20 21 3d 20 27 2e 27 20 26 26 20 62 75 66 5b ] != '.' && buf[
10bea 73 74 61 72 74 2b 31 5d 20 21 3d 20 27 2e 27 29 start+1] != '.')
10beb 20 29 7b 0a 20 20 20 20 20 20 20 20 62 75 66 5b ){. buf[
10bec 69 5d 3d 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 i]='\0';.
10bed 20 69 66 28 20 6f 73 4d 6b 64 69 72 28 62 75 66 if( osMkdir(buf
10bee 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 , SQLITE_DEFAULT
10bef 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 _PROXYDIR_PERMIS
10bf0 53 49 4f 4e 53 29 20 29 7b 0a 20 20 20 20 20 20 SIONS) ){.
10bf1 20 20 20 20 69 6e 74 20 65 72 72 3d 65 72 72 6e int err=errn
10bf2 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 o;. if(
10bf3 20 65 72 72 21 3d 45 45 58 49 53 54 20 29 20 7b err!=EEXIST ) {
10bf4 0a 20 20 20 20 20 20 20 20 20 20 20 20 4f 53 54 . OST
10bf5 52 41 43 45 28 28 22 43 52 45 41 54 45 4c 4f 43 RACE(("CREATELOC
10bf6 4b 50 41 54 48 20 20 46 41 49 4c 45 44 20 63 72 KPATH FAILED cr
10bf7 65 61 74 69 6e 67 20 25 73 2c 20 22 0a 20 20 20 eating %s, ".
10bf8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10bf9 20 20 22 27 25 73 27 20 70 72 6f 78 79 20 6c 6f "'%s' proxy lo
10bfa 63 6b 20 70 61 74 68 3d 25 73 20 70 69 64 3d 25 ck path=%s pid=%
10bfb 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
10bfc 20 20 20 20 20 20 20 20 20 20 20 62 75 66 2c 20 buf,
10bfd 73 74 72 65 72 72 6f 72 28 65 72 72 29 2c 20 6c strerror(err), l
10bfe 6f 63 6b 50 61 74 68 2c 20 67 65 74 70 69 64 28 ockPath, getpid(
10bff 29 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 )));.
10c00 20 72 65 74 75 72 6e 20 65 72 72 3b 0a 20 20 20 return err;.
10c01 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
10c02 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
10c03 20 73 74 61 72 74 3d 69 2b 31 3b 0a 20 20 20 20 start=i+1;.
10c04 7d 0a 20 20 20 20 62 75 66 5b 69 5d 20 3d 20 6c }. buf[i] = l
10c05 6f 63 6b 50 61 74 68 5b 69 5d 3b 0a 20 20 7d 0a ockPath[i];. }.
10c06 20 20 4f 53 54 52 41 43 45 28 28 22 43 52 45 41 OSTRACE(("CREA
10c07 54 45 4c 4f 43 4b 50 41 54 48 20 20 70 72 6f 78 TELOCKPATH prox
10c08 79 20 6c 6f 63 6b 20 70 61 74 68 3d 25 73 20 70 y lock path=%s p
10c09 69 64 3d 25 64 5c 6e 22 2c 20 6c 6f 63 6b 50 61 id=%d\n", lockPa
10c0a 74 68 2c 20 67 65 74 70 69 64 28 29 29 29 3b 0a th, getpid()));.
10c0b 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
10c0c 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 *.** Create a ne
10c0d 77 20 56 46 53 20 66 69 6c 65 20 64 65 73 63 72 w VFS file descr
10c0e 69 70 74 6f 72 20 28 73 74 6f 72 65 64 20 69 6e iptor (stored in
10c0f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
10c10 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 from.** sqlite3
10c11 5f 6d 61 6c 6c 6f 63 29 20 61 6e 64 20 6f 70 65 _malloc) and ope
10c12 6e 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 64 n the file named
10c13 20 22 70 61 74 68 22 20 69 6e 20 74 68 65 20 66 "path" in the f
10c14 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a ile descriptor..
10c15 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 **.** The caller
10c16 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 is responsible
10c17 6e 6f 74 20 6f 6e 6c 79 20 66 6f 72 20 63 6c 6f not only for clo
10c18 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 64 65 sing the file de
10c19 73 63 72 69 70 74 6f 72 0a 2a 2a 20 62 75 74 20 scriptor.** but
10c1a 61 6c 73 6f 20 66 6f 72 20 66 72 65 65 69 6e 67 also for freeing
10c1b 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 73 73 6f the memory asso
10c1c 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
10c1d 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e file descriptor.
10c1e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
10c1f 72 6f 78 79 43 72 65 61 74 65 55 6e 69 78 46 69 roxyCreateUnixFi
10c20 6c 65 28 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 le(. const ch
10c21 61 72 20 2a 70 61 74 68 2c 20 20 20 20 20 20 20 ar *path,
10c22 20 2f 2a 20 70 61 74 68 20 66 6f 72 20 74 68 65 /* path for the
10c23 20 6e 65 77 20 75 6e 69 78 46 69 6c 65 20 2a 2f new unixFile */
10c24 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 2a . unixFile **
10c25 70 70 46 69 6c 65 2c 20 20 20 20 20 20 20 2f 2a ppFile, /*
10c26 20 75 6e 69 78 46 69 6c 65 20 63 72 65 61 74 65 unixFile create
10c27 64 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 62 d and returned b
10c28 79 20 72 65 66 20 2a 2f 0a 20 20 20 20 69 6e 74 y ref */. int
10c29 20 69 73 6c 6f 63 6b 66 69 6c 65 20 20 20 20 20 islockfile
10c2a 20 20 20 20 20 20 2f 2a 20 69 66 20 6e 6f 6e 20 /* if non
10c2b 7a 65 72 6f 20 6d 69 73 73 69 6e 67 20 64 69 72 zero missing dir
10c2c 73 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 s will be create
10c2d 64 20 2a 2f 0a 29 20 7b 0a 20 20 69 6e 74 20 66 d */.) {. int f
10c2e 64 20 3d 20 2d 31 3b 0a 20 20 75 6e 69 78 46 69 d = -1;. unixFi
10c2f 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 le *pNew;. int
10c30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
10c31 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 20 int openFlags
10c32 3d 20 4f 5f 52 44 57 52 20 7c 20 4f 5f 43 52 45 = O_RDWR | O_CRE
10c33 41 54 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 AT;. sqlite3_vf
10c34 73 20 64 75 6d 6d 79 56 66 73 3b 0a 20 20 69 6e s dummyVfs;. in
10c35 74 20 74 65 72 72 6e 6f 20 3d 20 30 3b 0a 20 20 t terrno = 0;.
10c36 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 UnixUnusedFd *pU
10c37 6e 75 73 65 64 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 nused = NULL;..
10c38 20 2f 2a 20 31 2e 20 66 69 72 73 74 20 74 72 79 /* 1. first try
10c39 20 74 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20 to open/create
10c3a 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 32 2e the file. ** 2.
10c3b 20 69 66 20 74 68 61 74 20 66 61 69 6c 73 2c 20 if that fails,
10c3c 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 6c 6f and this is a lo
10c3d 63 6b 20 66 69 6c 65 20 28 6e 6f 74 2d 63 6f 6e ck file (not-con
10c3e 63 68 29 2c 20 74 72 79 20 63 72 65 61 74 69 6e ch), try creatin
10c3f 67 0a 20 20 2a 2a 20 74 68 65 20 70 61 72 65 6e g. ** the paren
10c40 74 20 64 69 72 65 63 74 6f 72 69 65 73 20 61 6e t directories an
10c41 64 20 74 68 65 6e 20 74 72 79 20 61 67 61 69 6e d then try again
10c42 2e 0a 20 20 2a 2a 20 33 2e 20 69 66 20 74 68 61 .. ** 3. if tha
10c43 74 20 66 61 69 6c 73 2c 20 74 72 79 20 74 6f 20 t fails, try to
10c44 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 72 65 open the file re
10c45 61 64 2d 6f 6e 6c 79 0a 20 20 2a 2a 20 6f 74 68 ad-only. ** oth
10c46 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 42 55 erwise return BU
10c47 53 59 20 28 69 66 20 6c 6f 63 6b 20 66 69 6c 65 SY (if lock file
10c48 29 20 6f 72 20 43 41 4e 54 4f 50 45 4e 20 66 6f ) or CANTOPEN fo
10c49 72 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 r the conch file
10c4a 0a 20 20 2a 2f 0a 20 20 70 55 6e 75 73 65 64 20 . */. pUnused
10c4b 3d 20 66 69 6e 64 52 65 75 73 61 62 6c 65 46 64 = findReusableFd
10c4c 28 70 61 74 68 2c 20 6f 70 65 6e 46 6c 61 67 73 (path, openFlags
10c4d 29 3b 0a 20 20 69 66 28 20 70 55 6e 75 73 65 64 );. if( pUnused
10c4e 20 29 7b 0a 20 20 20 20 66 64 20 3d 20 70 55 6e ){. fd = pUn
10c4f 75 73 65 64 2d 3e 66 64 3b 0a 20 20 7d 65 6c 73 used->fd;. }els
10c50 65 7b 0a 20 20 20 20 70 55 6e 75 73 65 64 20 3d e{. pUnused =
10c51 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
10c52 73 69 7a 65 6f 66 28 2a 70 55 6e 75 73 65 64 29 sizeof(*pUnused)
10c53 29 3b 0a 20 20 20 20 69 66 28 20 21 70 55 6e 75 );. if( !pUnu
10c54 73 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74 sed ){. ret
10c55 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
10c56 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
10c57 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 66 64 ( fd<0 ){. fd
10c58 20 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 70 = robust_open(p
10c59 61 74 68 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 ath, openFlags,
10c5a 30 29 3b 0a 20 20 20 20 74 65 72 72 6e 6f 20 3d 0);. terrno =
10c5b 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 errno;. if(
10c5c 66 64 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d 45 fd<0 && errno==E
10c5d 4e 4f 45 4e 54 20 26 26 20 69 73 6c 6f 63 6b 66 NOENT && islockf
10c5e 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ile ){. if(
10c5f 20 70 72 6f 78 79 43 72 65 61 74 65 4c 6f 63 6b proxyCreateLock
10c60 50 61 74 68 28 70 61 74 68 29 20 3d 3d 20 53 51 Path(path) == SQ
10c61 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
10c62 20 20 20 66 64 20 3d 20 72 6f 62 75 73 74 5f 6f fd = robust_o
10c63 70 65 6e 28 70 61 74 68 2c 20 6f 70 65 6e 46 6c pen(path, openFl
10c64 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d ags, 0);. }
10c65 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
10c66 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 6f 70 65 fd<0 ){. ope
10c67 6e 46 6c 61 67 73 20 3d 20 4f 5f 52 44 4f 4e 4c nFlags = O_RDONL
10c68 59 3b 0a 20 20 20 20 66 64 20 3d 20 72 6f 62 75 Y;. fd = robu
10c69 73 74 5f 6f 70 65 6e 28 70 61 74 68 2c 20 6f 70 st_open(path, op
10c6a 65 6e 46 6c 61 67 73 2c 20 30 29 3b 0a 20 20 20 enFlags, 0);.
10c6b 20 74 65 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b terrno = errno;
10c6c 0a 20 20 7d 0a 20 20 69 66 28 20 66 64 3c 30 20 . }. if( fd<0
10c6d 29 7b 0a 20 20 20 20 69 66 28 20 69 73 6c 6f 63 ){. if( isloc
10c6e 6b 66 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 72 kfile ){. r
10c6f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 eturn SQLITE_BUS
10c70 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 77 69 Y;. }. swi
10c71 74 63 68 20 28 74 65 72 72 6e 6f 29 20 7b 0a 20 tch (terrno) {.
10c72 20 20 20 20 20 63 61 73 65 20 45 41 43 43 45 53 case EACCES
10c73 3a 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e :. return
10c74 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 SQLITE_PERM;.
10c75 20 20 20 20 63 61 73 65 20 45 49 4f 3a 20 0a 20 case EIO: .
10c76 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
10c77 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 3b LITE_IOERR_LOCK;
10c78 20 2f 2a 20 65 76 65 6e 20 74 68 6f 75 67 68 20 /* even though
10c79 69 74 20 69 73 20 74 68 65 20 63 6f 6e 63 68 20 it is the conch
10c7a 2a 2f 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 */. default
10c7b 3a 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e :. return
10c7c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
10c7d 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d _BKPT;. }. }
10c7e 0a 20 20 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e . . pNew = (un
10c7f 69 78 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 ixFile *)sqlite3
10c80 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a _malloc(sizeof(*
10c81 70 4e 65 77 29 29 3b 0a 20 20 69 66 28 20 70 4e pNew));. if( pN
10c82 65 77 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 ew==NULL ){.
10c83 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
10c84 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f M;. goto end_
10c85 63 72 65 61 74 65 5f 70 72 6f 78 79 3b 0a 20 20 create_proxy;.
10c86 7d 0a 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c }. memset(pNew,
10c87 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 0, sizeof(unixF
10c88 69 6c 65 29 29 3b 0a 20 20 70 4e 65 77 2d 3e 6f ile));. pNew->o
10c89 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e 46 penFlags = openF
10c8a 6c 61 67 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26 lags;. memset(&
10c8b 64 75 6d 6d 79 56 66 73 2c 20 30 2c 20 73 69 7a dummyVfs, 0, siz
10c8c 65 6f 66 28 64 75 6d 6d 79 56 66 73 29 29 3b 0a eof(dummyVfs));.
10c8d 20 20 64 75 6d 6d 79 56 66 73 2e 70 41 70 70 44 dummyVfs.pAppD
10c8e 61 74 61 20 3d 20 28 76 6f 69 64 2a 29 26 61 75 ata = (void*)&au
10c8f 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 3b 0a tolockIoFinder;.
10c90 20 20 64 75 6d 6d 79 56 66 73 2e 7a 4e 61 6d 65 dummyVfs.zName
10c91 20 3d 20 22 64 75 6d 6d 79 22 3b 0a 20 20 70 55 = "dummy";. pU
10c92 6e 75 73 65 64 2d 3e 66 64 20 3d 20 66 64 3b 0a nused->fd = fd;.
10c93 20 20 70 55 6e 75 73 65 64 2d 3e 66 6c 61 67 73 pUnused->flags
10c94 20 3d 20 6f 70 65 6e 46 6c 61 67 73 3b 0a 20 20 = openFlags;.
10c95 70 4e 65 77 2d 3e 70 55 6e 75 73 65 64 20 3d 20 pNew->pUnused =
10c96 70 55 6e 75 73 65 64 3b 0a 20 20 0a 20 20 72 63 pUnused;. . rc
10c97 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c = fillInUnixFil
10c98 65 28 26 64 75 6d 6d 79 56 66 73 2c 20 66 64 2c e(&dummyVfs, fd,
10c99 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 (sqlite3_file*)
10c9a 70 4e 65 77 2c 20 70 61 74 68 2c 20 30 29 3b 0a pNew, path, 0);.
10c9b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
10c9c 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 70 46 69 _OK ){. *ppFi
10c9d 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 72 le = pNew;. r
10c9e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
10c9f 0a 20 20 7d 0a 65 6e 64 5f 63 72 65 61 74 65 5f . }.end_create_
10ca0 70 72 6f 78 79 3a 20 20 20 20 0a 20 20 72 6f 62 proxy: . rob
10ca1 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65 77 2c 20 ust_close(pNew,
10ca2 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 fd, __LINE__);.
10ca3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e sqlite3_free(pN
10ca4 65 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 ew);. sqlite3_f
10ca5 72 65 65 28 70 55 6e 75 73 65 64 29 3b 0a 20 20 ree(pUnused);.
10ca6 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 return rc;.}..#i
10ca7 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
10ca8 0a 2f 2a 20 73 69 6d 75 6c 61 74 65 20 6d 75 6c ./* simulate mul
10ca9 74 69 70 6c 65 20 68 6f 73 74 73 20 62 79 20 63 tiple hosts by c
10caa 72 65 61 74 69 6e 67 20 75 6e 69 71 75 65 20 68 reating unique h
10cab 6f 73 74 69 64 20 66 69 6c 65 20 70 61 74 68 73 ostid file paths
10cac 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
10cad 6e 74 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 nt sqlite3_hosti
10cae 64 5f 6e 75 6d 20 3d 20 30 3b 0a 23 65 6e 64 69 d_num = 0;.#endi
10caf 66 0a 0a 23 64 65 66 69 6e 65 20 50 52 4f 58 59 f..#define PROXY
10cb0 5f 48 4f 53 54 49 44 4c 45 4e 20 20 20 20 31 36 _HOSTIDLEN 16
10cb1 20 20 2f 2a 20 63 6f 6e 63 68 20 66 69 6c 65 20 /* conch file
10cb2 68 6f 73 74 20 69 64 20 6c 65 6e 67 74 68 20 2a host id length *
10cb3 2f 0a 0a 2f 2a 20 4e 6f 74 20 61 6c 77 61 79 73 /../* Not always
10cb4 20 64 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 defined in the
10cb5 68 65 61 64 65 72 73 20 61 73 20 69 74 20 6f 75 headers as it ou
10cb6 67 68 74 20 74 6f 20 62 65 20 2a 2f 0a 65 78 74 ght to be */.ext
10cb7 65 72 6e 20 69 6e 74 20 67 65 74 68 6f 73 74 75 ern int gethostu
10cb8 75 69 64 28 75 75 69 64 5f 74 20 69 64 2c 20 63 uid(uuid_t id, c
10cb9 6f 6e 73 74 20 73 74 72 75 63 74 20 74 69 6d 65 onst struct time
10cba 73 70 65 63 20 2a 77 61 69 74 29 3b 0a 0a 2f 2a spec *wait);../*
10cbb 20 67 65 74 20 74 68 65 20 68 6f 73 74 20 49 44 get the host ID
10cbc 20 76 69 61 20 67 65 74 68 6f 73 74 75 75 69 64 via gethostuuid
10cbd 28 29 2c 20 70 48 6f 73 74 49 44 20 6d 75 73 74 (), pHostID must
10cbe 20 70 6f 69 6e 74 20 74 6f 20 50 52 4f 58 59 5f point to PROXY_
10cbf 48 4f 53 54 49 44 4c 45 4e 20 0a 2a 2a 20 62 79 HOSTIDLEN .** by
10cc0 74 65 73 20 6f 66 20 77 72 69 74 61 62 6c 65 20 tes of writable
10cc1 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 memory..*/.stati
10cc2 63 20 69 6e 74 20 70 72 6f 78 79 47 65 74 48 6f c int proxyGetHo
10cc3 73 74 49 44 28 75 6e 73 69 67 6e 65 64 20 63 68 stID(unsigned ch
10cc4 61 72 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e 74 ar *pHostID, int
10cc5 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20 61 73 73 *pError){. ass
10cc6 65 72 74 28 50 52 4f 58 59 5f 48 4f 53 54 49 44 ert(PROXY_HOSTID
10cc7 4c 45 4e 20 3d 3d 20 73 69 7a 65 6f 66 28 75 75 LEN == sizeof(uu
10cc8 69 64 5f 74 29 29 3b 0a 20 20 6d 65 6d 73 65 74 id_t));. memset
10cc9 28 70 48 6f 73 74 49 44 2c 20 30 2c 20 50 52 4f (pHostID, 0, PRO
10cca 58 59 5f 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 23 XY_HOSTIDLEN);.#
10ccb 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 4d 41 58 if defined(__MAX
10ccc 5f 4f 53 5f 58 5f 56 45 52 53 49 4f 4e 5f 4d 49 _OS_X_VERSION_MI
10ccd 4e 5f 52 45 51 55 49 52 45 44 29 5c 0a 20 20 20 N_REQUIRED)\.
10cce 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 5f && _
10ccf 5f 4d 41 43 5f 4f 53 5f 58 5f 56 45 52 53 49 4f _MAC_OS_X_VERSIO
10cd0 4e 5f 4d 49 4e 5f 52 45 51 55 49 52 45 44 3c 31 N_MIN_REQUIRED<1
10cd1 30 35 30 0a 20 20 7b 0a 20 20 20 20 73 74 61 74 050. {. stat
10cd2 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 ic const struct
10cd3 74 69 6d 65 73 70 65 63 20 74 69 6d 65 6f 75 74 timespec timeout
10cd4 20 3d 20 7b 31 2c 20 30 7d 3b 20 2f 2a 20 31 20 = {1, 0}; /* 1
10cd5 73 65 63 20 74 69 6d 65 6f 75 74 20 2a 2f 0a 20 sec timeout */.
10cd6 20 20 20 69 66 28 20 67 65 74 68 6f 73 74 75 75 if( gethostuu
10cd7 69 64 28 70 48 6f 73 74 49 44 2c 20 26 74 69 6d id(pHostID, &tim
10cd8 65 6f 75 74 29 20 29 7b 0a 20 20 20 20 20 20 69 eout) ){. i
10cd9 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a nt err = errno;.
10cda 20 20 20 20 20 20 69 66 28 20 70 45 72 72 6f 72 if( pError
10cdb 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 45 72 ){. *pEr
10cdc 72 6f 72 20 3d 20 65 72 72 3b 0a 20 20 20 20 20 ror = err;.
10cdd 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 }. return
10cde 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 SQLITE_IOERR;.
10cdf 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 }. }.#else.
10ce0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
10ce1 28 70 45 72 72 6f 72 29 3b 0a 23 65 6e 64 69 66 (pError);.#endif
10ce2 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
10ce3 45 53 54 0a 20 20 2f 2a 20 73 69 6d 75 6c 61 74 EST. /* simulat
10ce4 65 20 6d 75 6c 74 69 70 6c 65 20 68 6f 73 74 73 e multiple hosts
10ce5 20 62 79 20 63 72 65 61 74 69 6e 67 20 75 6e 69 by creating uni
10ce6 71 75 65 20 68 6f 73 74 69 64 20 66 69 6c 65 20 que hostid file
10ce7 70 61 74 68 73 20 2a 2f 0a 20 20 69 66 28 20 73 paths */. if( s
10ce8 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 qlite3_hostid_nu
10ce9 6d 20 21 3d 20 30 29 7b 0a 20 20 20 20 70 48 6f m != 0){. pHo
10cea 73 74 49 44 5b 30 5d 20 3d 20 28 63 68 61 72 29 stID[0] = (char)
10ceb 28 70 48 6f 73 74 49 44 5b 30 5d 20 2b 20 28 63 (pHostID[0] + (c
10cec 68 61 72 29 28 73 71 6c 69 74 65 33 5f 68 6f 73 har)(sqlite3_hos
10ced 74 69 64 5f 6e 75 6d 20 26 20 30 78 46 46 29 29 tid_num & 0xFF))
10cee 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a ;. }.#endif. .
10cef 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
10cf0 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 63 6f OK;.}../* The co
10cf1 6e 63 68 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e nch file contain
10cf2 73 20 74 68 65 20 68 65 61 64 65 72 2c 20 68 6f s the header, ho
10cf3 73 74 20 69 64 20 61 6e 64 20 6c 6f 63 6b 20 66 st id and lock f
10cf4 69 6c 65 20 70 61 74 68 0a 20 2a 2f 0a 23 64 65 ile path. */.#de
10cf5 66 69 6e 65 20 50 52 4f 58 59 5f 43 4f 4e 43 48 fine PROXY_CONCH
10cf6 56 45 52 53 49 4f 4e 20 32 20 20 20 2f 2a 20 31 VERSION 2 /* 1
10cf7 2d 62 79 74 65 20 68 65 61 64 65 72 2c 20 31 36 -byte header, 16
10cf8 2d 62 79 74 65 20 68 6f 73 74 20 69 64 2c 20 70 -byte host id, p
10cf9 61 74 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ath */.#define P
10cfa 52 4f 58 59 5f 48 45 41 44 45 52 4c 45 4e 20 20 ROXY_HEADERLEN
10cfb 20 20 31 20 20 20 2f 2a 20 63 6f 6e 63 68 20 66 1 /* conch f
10cfc 69 6c 65 20 68 65 61 64 65 72 20 6c 65 6e 67 74 ile header lengt
10cfd 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 52 4f h */.#define PRO
10cfe 58 59 5f 50 41 54 48 49 4e 44 45 58 20 20 20 20 XY_PATHINDEX
10cff 28 50 52 4f 58 59 5f 48 45 41 44 45 52 4c 45 4e (PROXY_HEADERLEN
10d00 2b 50 52 4f 58 59 5f 48 4f 53 54 49 44 4c 45 4e +PROXY_HOSTIDLEN
10d01 29 0a 23 64 65 66 69 6e 65 20 50 52 4f 58 59 5f ).#define PROXY_
10d02 4d 41 58 43 4f 4e 43 48 4c 45 4e 20 20 28 50 52 MAXCONCHLEN (PR
10d03 4f 58 59 5f 48 45 41 44 45 52 4c 45 4e 2b 50 52 OXY_HEADERLEN+PR
10d04 4f 58 59 5f 48 4f 53 54 49 44 4c 45 4e 2b 4d 41 OXY_HOSTIDLEN+MA
10d05 58 50 41 54 48 4c 45 4e 29 0a 0a 2f 2a 20 0a 2a XPATHLEN)../* .*
10d06 2a 20 54 61 6b 65 73 20 61 6e 20 6f 70 65 6e 20 * Takes an open
10d07 63 6f 6e 63 68 20 66 69 6c 65 2c 20 63 6f 70 69 conch file, copi
10d08 65 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 es the contents
10d09 74 6f 20 61 20 6e 65 77 20 70 61 74 68 20 61 6e to a new path an
10d0a 64 20 74 68 65 6e 20 6d 6f 76 65 73 20 0a 2a 2a d then moves .**
10d0b 20 69 74 20 62 61 63 6b 2e 20 20 54 68 65 20 6e it back. The n
10d0c 65 77 6c 79 20 63 72 65 61 74 65 64 20 66 69 6c ewly created fil
10d0d 65 27 73 20 66 69 6c 65 20 64 65 73 63 72 69 70 e's file descrip
10d0e 74 6f 72 20 69 73 20 61 73 73 69 67 6e 65 64 20 tor is assigned
10d0f 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e 63 68 20 to the.** conch
10d10 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 61 file structure a
10d11 6e 64 20 66 69 6e 61 6c 6c 79 20 74 68 65 20 6f nd finally the o
10d12 72 69 67 69 6e 61 6c 20 63 6f 6e 63 68 20 66 69 riginal conch fi
10d13 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
10d14 20 0a 2a 2a 20 63 6c 6f 73 65 64 2e 20 20 52 65 .** closed. Re
10d15 74 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 73 75 turns zero if su
10d16 63 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a 73 74 61 ccessful..*/.sta
10d17 74 69 63 20 69 6e 74 20 70 72 6f 78 79 42 72 65 tic int proxyBre
10d18 61 6b 43 6f 6e 63 68 4c 6f 63 6b 28 75 6e 69 78 akConchLock(unix
10d19 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 75 75 69 File *pFile, uui
10d1a 64 5f 74 20 6d 79 48 6f 73 74 49 44 29 7b 0a 20 d_t myHostID){.
10d1b 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
10d1c 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 text *pCtx = (pr
10d1d 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
10d1e 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 t *)pFile->locki
10d1f 6e 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 75 6e ngContext; . un
10d20 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c ixFile *conchFil
10d21 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 e = pCtx->conchF
10d22 69 6c 65 3b 0a 20 20 63 68 61 72 20 74 50 61 74 ile;. char tPat
10d23 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 h[MAXPATHLEN];.
10d24 20 63 68 61 72 20 62 75 66 5b 50 52 4f 58 59 5f char buf[PROXY_
10d25 4d 41 58 43 4f 4e 43 48 4c 45 4e 5d 3b 0a 20 20 MAXCONCHLEN];.
10d26 63 68 61 72 20 2a 63 50 61 74 68 20 3d 20 70 43 char *cPath = pC
10d27 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 tx->conchFilePat
10d28 68 3b 0a 20 20 73 69 7a 65 5f 74 20 72 65 61 64 h;. size_t read
10d29 4c 65 6e 20 3d 20 30 3b 0a 20 20 73 69 7a 65 5f Len = 0;. size_
10d2a 74 20 70 61 74 68 4c 65 6e 20 3d 20 30 3b 0a 20 t pathLen = 0;.
10d2b 20 63 68 61 72 20 65 72 72 6d 73 67 5b 36 34 5d char errmsg[64]
10d2c 20 3d 20 22 22 3b 0a 20 20 69 6e 74 20 66 64 20 = "";. int fd
10d2d 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 72 63 20 3d = -1;. int rc =
10d2e 20 2d 31 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 -1;. UNUSED_PA
10d2f 52 41 4d 45 54 45 52 28 6d 79 48 6f 73 74 49 44 RAMETER(myHostID
10d30 29 3b 0a 0a 20 20 2f 2a 20 63 72 65 61 74 65 20 );.. /* create
10d31 61 20 6e 65 77 20 70 61 74 68 20 62 79 20 72 65 a new path by re
10d32 70 6c 61 63 65 20 74 68 65 20 74 72 61 69 6c 69 place the traili
10d33 6e 67 20 27 2d 63 6f 6e 63 68 27 20 77 69 74 68 ng '-conch' with
10d34 20 27 2d 62 72 65 61 6b 27 20 2a 2f 0a 20 20 70 '-break' */. p
10d35 61 74 68 4c 65 6e 20 3d 20 73 74 72 6c 63 70 79 athLen = strlcpy
10d36 28 74 50 61 74 68 2c 20 63 50 61 74 68 2c 20 4d (tPath, cPath, M
10d37 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 69 66 AXPATHLEN);. if
10d38 28 20 70 61 74 68 4c 65 6e 3e 4d 41 58 50 41 54 ( pathLen>MAXPAT
10d39 48 4c 45 4e 20 7c 7c 20 70 61 74 68 4c 65 6e 3c HLEN || pathLen<
10d3a 36 20 7c 7c 20 0a 20 20 20 20 20 28 73 74 72 6c 6 || . (strl
10d3b 63 70 79 28 26 74 50 61 74 68 5b 70 61 74 68 4c cpy(&tPath[pathL
10d3c 65 6e 2d 35 5d 2c 20 22 62 72 65 61 6b 22 2c 20 en-5], "break",
10d3d 36 29 20 21 3d 20 35 29 20 29 7b 0a 20 20 20 20 6) != 5) ){.
10d3e 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
10d3f 28 73 69 7a 65 6f 66 28 65 72 72 6d 73 67 29 2c (sizeof(errmsg),
10d40 65 72 72 6d 73 67 2c 22 70 61 74 68 20 65 72 72 errmsg,"path err
10d41 6f 72 20 28 6c 65 6e 20 25 64 29 22 2c 28 69 6e or (len %d)",(in
10d42 74 29 70 61 74 68 4c 65 6e 29 3b 0a 20 20 20 20 t)pathLen);.
10d43 67 6f 74 6f 20 65 6e 64 5f 62 72 65 61 6b 6c 6f goto end_breaklo
10d44 63 6b 3b 0a 20 20 7d 0a 20 20 2f 2a 20 72 65 61 ck;. }. /* rea
10d45 64 20 74 68 65 20 63 6f 6e 63 68 20 63 6f 6e 74 d the conch cont
10d46 65 6e 74 20 2a 2f 0a 20 20 72 65 61 64 4c 65 6e ent */. readLen
10d47 20 3d 20 6f 73 50 72 65 61 64 28 63 6f 6e 63 68 = osPread(conch
10d48 46 69 6c 65 2d 3e 68 2c 20 62 75 66 2c 20 50 52 File->h, buf, PR
10d49 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e 2c OXY_MAXCONCHLEN,
10d4a 20 30 29 3b 0a 20 20 69 66 28 20 72 65 61 64 4c 0);. if( readL
10d4b 65 6e 3c 50 52 4f 58 59 5f 50 41 54 48 49 4e 44 en<PROXY_PATHIND
10d4c 45 58 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 EX ){. sqlite
10d4d 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
10d4e 66 28 65 72 72 6d 73 67 29 2c 65 72 72 6d 73 67 f(errmsg),errmsg
10d4f 2c 22 72 65 61 64 20 65 72 72 6f 72 20 28 6c 65 ,"read error (le
10d50 6e 20 25 64 29 22 2c 28 69 6e 74 29 72 65 61 64 n %d)",(int)read
10d51 4c 65 6e 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 Len);. goto e
10d52 6e 64 5f 62 72 65 61 6b 6c 6f 63 6b 3b 0a 20 20 nd_breaklock;.
10d53 7d 0a 20 20 2f 2a 20 77 72 69 74 65 20 69 74 20 }. /* write it
10d54 6f 75 74 20 74 6f 20 74 68 65 20 74 65 6d 70 6f out to the tempo
10d55 72 61 72 79 20 62 72 65 61 6b 20 66 69 6c 65 20 rary break file
10d56 2a 2f 0a 20 20 66 64 20 3d 20 72 6f 62 75 73 74 */. fd = robust
10d57 5f 6f 70 65 6e 28 74 50 61 74 68 2c 20 28 4f 5f _open(tPath, (O_
10d58 52 44 57 52 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45 RDWR|O_CREAT|O_E
10d59 58 43 4c 29 2c 20 30 29 3b 0a 20 20 69 66 28 20 XCL), 0);. if(
10d5a 66 64 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 fd<0 ){. sqli
10d5b 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
10d5c 65 6f 66 28 65 72 72 6d 73 67 29 2c 20 65 72 72 eof(errmsg), err
10d5d 6d 73 67 2c 20 22 63 72 65 61 74 65 20 66 61 69 msg, "create fai
10d5e 6c 65 64 20 28 25 64 29 22 2c 20 65 72 72 6e 6f led (%d)", errno
10d5f 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f );. goto end_
10d60 62 72 65 61 6b 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 breaklock;. }.
10d61 20 69 66 28 20 6f 73 50 77 72 69 74 65 28 66 64 if( osPwrite(fd
10d62 2c 20 62 75 66 2c 20 72 65 61 64 4c 65 6e 2c 20 , buf, readLen,
10d63 30 29 20 21 3d 20 28 73 73 69 7a 65 5f 74 29 72 0) != (ssize_t)r
10d64 65 61 64 4c 65 6e 20 29 7b 0a 20 20 20 20 73 71 eadLen ){. sq
10d65 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
10d66 69 7a 65 6f 66 28 65 72 72 6d 73 67 29 2c 20 65 izeof(errmsg), e
10d67 72 72 6d 73 67 2c 20 22 77 72 69 74 65 20 66 61 rrmsg, "write fa
10d68 69 6c 65 64 20 28 25 64 29 22 2c 20 65 72 72 6e iled (%d)", errn
10d69 6f 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 o);. goto end
10d6a 5f 62 72 65 61 6b 6c 6f 63 6b 3b 0a 20 20 7d 0a _breaklock;. }.
10d6b 20 20 69 66 28 20 72 65 6e 61 6d 65 28 74 50 61 if( rename(tPa
10d6c 74 68 2c 20 63 50 61 74 68 29 20 29 7b 0a 20 20 th, cPath) ){.
10d6d 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
10d6e 74 66 28 73 69 7a 65 6f 66 28 65 72 72 6d 73 67 tf(sizeof(errmsg
10d6f 29 2c 20 65 72 72 6d 73 67 2c 20 22 72 65 6e 61 ), errmsg, "rena
10d70 6d 65 20 66 61 69 6c 65 64 20 28 25 64 29 22 2c me failed (%d)",
10d71 20 65 72 72 6e 6f 29 3b 0a 20 20 20 20 67 6f 74 errno);. got
10d72 6f 20 65 6e 64 5f 62 72 65 61 6b 6c 6f 63 6b 3b o end_breaklock;
10d73 0a 20 20 7d 0a 20 20 72 63 20 3d 20 30 3b 0a 20 . }. rc = 0;.
10d74 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
10d75 20 22 62 72 6f 6b 65 20 73 74 61 6c 65 20 6c 6f "broke stale lo
10d76 63 6b 20 6f 6e 20 25 73 5c 6e 22 2c 20 63 50 61 ck on %s\n", cPa
10d77 74 68 29 3b 0a 20 20 72 6f 62 75 73 74 5f 63 6c th);. robust_cl
10d78 6f 73 65 28 70 46 69 6c 65 2c 20 63 6f 6e 63 68 ose(pFile, conch
10d79 46 69 6c 65 2d 3e 68 2c 20 5f 5f 4c 49 4e 45 5f File->h, __LINE_
10d7a 5f 29 3b 0a 20 20 63 6f 6e 63 68 46 69 6c 65 2d _);. conchFile-
10d7b 3e 68 20 3d 20 66 64 3b 0a 20 20 63 6f 6e 63 68 >h = fd;. conch
10d7c 46 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 File->openFlags
10d7d 3d 20 4f 5f 52 44 57 52 20 7c 20 4f 5f 43 52 45 = O_RDWR | O_CRE
10d7e 41 54 3b 0a 0a 65 6e 64 5f 62 72 65 61 6b 6c 6f AT;..end_breaklo
10d7f 63 6b 3a 0a 20 20 69 66 28 20 72 63 20 29 7b 0a ck:. if( rc ){.
10d80 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20 29 7b if( fd>=0 ){
10d81 0a 20 20 20 20 20 20 6f 73 55 6e 6c 69 6e 6b 28 . osUnlink(
10d82 74 50 61 74 68 29 3b 0a 20 20 20 20 20 20 72 6f tPath);. ro
10d83 62 75 73 74 5f 63 6c 6f 73 65 28 70 46 69 6c 65 bust_close(pFile
10d84 2c 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b , fd, __LINE__);
10d85 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 72 69 6e . }. fprin
10d86 74 66 28 73 74 64 65 72 72 2c 20 22 66 61 69 6c tf(stderr, "fail
10d87 65 64 20 74 6f 20 62 72 65 61 6b 20 73 74 61 6c ed to break stal
10d88 65 20 6c 6f 63 6b 20 6f 6e 20 25 73 2c 20 25 73 e lock on %s, %s
10d89 5c 6e 22 2c 20 63 50 61 74 68 2c 20 65 72 72 6d \n", cPath, errm
10d8a 73 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 sg);. }. retur
10d8b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 61 6b 65 n rc;.}../* Take
10d8c 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
10d8d 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 68 ock on the conch
10d8e 20 66 69 6c 65 20 61 6e 64 20 62 72 65 61 6b 20 file and break
10d8f 61 20 73 74 61 6c 65 20 6c 6f 63 6b 20 69 66 20 a stale lock if
10d90 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 69 64 20 the .** host id
10d91 6d 61 74 63 68 65 73 2e 0a 2a 2f 0a 73 74 61 74 matches..*/.stat
10d92 69 63 20 69 6e 74 20 70 72 6f 78 79 43 6f 6e 63 ic int proxyConc
10d93 68 4c 6f 63 6b 28 75 6e 69 78 46 69 6c 65 20 2a hLock(unixFile *
10d94 70 46 69 6c 65 2c 20 75 75 69 64 5f 74 20 6d 79 pFile, uuid_t my
10d95 48 6f 73 74 49 44 2c 20 69 6e 74 20 6c 6f 63 6b HostID, int lock
10d96 54 79 70 65 29 7b 0a 20 20 70 72 6f 78 79 4c 6f Type){. proxyLo
10d97 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
10d98 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 tx = (proxyLocki
10d99 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c ngContext *)pFil
10d9a 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
10d9b 74 3b 20 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a t; . unixFile *
10d9c 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 conchFile = pCtx
10d9d 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 69 ->conchFile;. i
10d9e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
10d9f 4b 3b 0a 20 20 69 6e 74 20 6e 54 72 69 65 73 20 K;. int nTries
10da0 3d 20 30 3b 0a 20 20 73 74 72 75 63 74 20 74 69 = 0;. struct ti
10da1 6d 65 73 70 65 63 20 63 6f 6e 63 68 4d 6f 64 54 mespec conchModT
10da2 69 6d 65 3b 0a 20 20 0a 20 20 6d 65 6d 73 65 74 ime;. . memset
10da3 28 26 63 6f 6e 63 68 4d 6f 64 54 69 6d 65 2c 20 (&conchModTime,
10da4 30 2c 20 73 69 7a 65 6f 66 28 63 6f 6e 63 68 4d 0, sizeof(conchM
10da5 6f 64 54 69 6d 65 29 29 3b 0a 20 20 64 6f 20 7b odTime));. do {
10da6 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 . rc = conchF
10da7 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c ile->pMethod->xL
10da8 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ock((sqlite3_fil
10da9 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 6c 6f e*)conchFile, lo
10daa 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 6e 54 72 ckType);. nTr
10dab 69 65 73 20 2b 2b 3b 0a 20 20 20 20 69 66 28 20 ies ++;. if(
10dac 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 rc==SQLITE_BUSY
10dad 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 ){. /* If t
10dae 68 65 20 6c 6f 63 6b 20 66 61 69 6c 65 64 20 28 he lock failed (
10daf 62 75 73 79 29 3a 0a 20 20 20 20 20 20 20 2a 20 busy):. *
10db0 31 73 74 20 74 72 79 3a 20 67 65 74 20 74 68 65 1st try: get the
10db1 20 6d 6f 64 20 74 69 6d 65 20 6f 66 20 74 68 65 mod time of the
10db2 20 63 6f 6e 63 68 2c 20 77 61 69 74 20 30 2e 35 conch, wait 0.5
10db3 73 20 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e s and try again.
10db4 20 0a 20 20 20 20 20 20 20 2a 20 32 6e 64 20 74 . * 2nd t
10db5 72 79 3a 20 66 61 69 6c 20 69 66 20 74 68 65 20 ry: fail if the
10db6 6d 6f 64 20 74 69 6d 65 20 63 68 61 6e 67 65 64 mod time changed
10db7 20 6f 72 20 68 6f 73 74 20 69 64 20 69 73 20 64 or host id is d
10db8 69 66 66 65 72 65 6e 74 2c 20 77 61 69 74 20 0a ifferent, wait .
10db9 20 20 20 20 20 20 20 2a 20 20 20 20 20 20 20 20 *
10dba 20 20 20 31 30 20 73 65 63 20 61 6e 64 20 74 72 10 sec and tr
10dbb 79 20 61 67 61 69 6e 0a 20 20 20 20 20 20 20 2a y again. *
10dbc 20 33 72 64 20 74 72 79 3a 20 62 72 65 61 6b 20 3rd try: break
10dbd 74 68 65 20 6c 6f 63 6b 20 75 6e 6c 65 73 73 20 the lock unless
10dbe 74 68 65 20 6d 6f 64 20 74 69 6d 65 20 68 61 73 the mod time has
10dbf 20 63 68 61 6e 67 65 64 2e 0a 20 20 20 20 20 20 changed..
10dc0 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 */. struct
10dc1 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20 20 20 stat buf;.
10dc2 20 69 66 28 20 6f 73 46 73 74 61 74 28 63 6f 6e if( osFstat(con
10dc3 63 68 46 69 6c 65 2d 3e 68 2c 20 26 62 75 66 29 chFile->h, &buf)
10dc4 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
10dc5 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 e->lastErrno = e
10dc6 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 rrno;. re
10dc7 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
10dc8 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 0a R_LOCK;. }.
10dc9 20 20 20 20 20 20 0a 20 20 20 20 20 20 69 66 28 . if(
10dca 20 6e 54 72 69 65 73 3d 3d 31 20 29 7b 0a 20 20 nTries==1 ){.
10dcb 20 20 20 20 20 20 63 6f 6e 63 68 4d 6f 64 54 69 conchModTi
10dcc 6d 65 20 3d 20 62 75 66 2e 73 74 5f 6d 74 69 6d me = buf.st_mtim
10dcd 65 73 70 65 63 3b 0a 20 20 20 20 20 20 20 20 75 espec;. u
10dce 73 6c 65 65 70 28 35 30 30 30 30 30 29 3b 20 2f sleep(500000); /
10dcf 2a 20 77 61 69 74 20 30 2e 35 20 73 65 63 20 61 * wait 0.5 sec a
10dd0 6e 64 20 74 72 79 20 74 68 65 20 6c 6f 63 6b 20 nd try the lock
10dd1 61 67 61 69 6e 2a 2f 0a 20 20 20 20 20 20 20 20 again*/.
10dd2 63 6f 6e 74 69 6e 75 65 3b 20 20 0a 20 20 20 20 continue; .
10dd3 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 }.. asser
10dd4 74 28 20 6e 54 72 69 65 73 3e 31 20 29 3b 0a 20 t( nTries>1 );.
10dd5 20 20 20 20 20 69 66 28 20 63 6f 6e 63 68 4d 6f if( conchMo
10dd6 64 54 69 6d 65 2e 74 76 5f 73 65 63 20 21 3d 20 dTime.tv_sec !=
10dd7 62 75 66 2e 73 74 5f 6d 74 69 6d 65 73 70 65 63 buf.st_mtimespec
10dd8 2e 74 76 5f 73 65 63 20 7c 7c 20 0a 20 20 20 20 .tv_sec || .
10dd9 20 20 20 20 20 63 6f 6e 63 68 4d 6f 64 54 69 6d conchModTim
10dda 65 2e 74 76 5f 6e 73 65 63 20 21 3d 20 62 75 66 e.tv_nsec != buf
10ddb 2e 73 74 5f 6d 74 69 6d 65 73 70 65 63 2e 74 76 .st_mtimespec.tv
10ddc 5f 6e 73 65 63 20 29 7b 0a 20 20 20 20 20 20 20 _nsec ){.
10ddd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
10dde 55 53 59 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 USY;. }.
10ddf 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 6e 54 . if( nT
10de0 72 69 65 73 3d 3d 32 20 29 7b 20 20 0a 20 20 20 ries==2 ){ .
10de1 20 20 20 20 20 63 68 61 72 20 74 42 75 66 5b 50 char tBuf[P
10de2 52 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e ROXY_MAXCONCHLEN
10de3 5d 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c ];. int l
10de4 65 6e 20 3d 20 6f 73 50 72 65 61 64 28 63 6f 6e en = osPread(con
10de5 63 68 46 69 6c 65 2d 3e 68 2c 20 74 42 75 66 2c chFile->h, tBuf,
10de6 20 50 52 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c PROXY_MAXCONCHL
10de7 45 4e 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 EN, 0);.
10de8 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20 if( len<0 ){.
10de9 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
10dea 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
10deb 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
10dec 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c n SQLITE_IOERR_L
10ded 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 OCK;. }.
10dee 20 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3e 50 if( len>P
10def 52 4f 58 59 5f 50 41 54 48 49 4e 44 45 58 20 26 ROXY_PATHINDEX &
10df0 26 20 74 42 75 66 5b 30 5d 3d 3d 28 63 68 61 72 & tBuf[0]==(char
10df1 29 50 52 4f 58 59 5f 43 4f 4e 43 48 56 45 52 53 )PROXY_CONCHVERS
10df2 49 4f 4e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ION){.
10df3 2f 2a 20 64 6f 6e 27 74 20 62 72 65 61 6b 20 74 /* don't break t
10df4 68 65 20 6c 6f 63 6b 20 69 66 20 74 68 65 20 68 he lock if the h
10df5 6f 73 74 20 69 64 20 64 6f 65 73 6e 27 74 20 6d ost id doesn't m
10df6 61 74 63 68 20 2a 2f 0a 20 20 20 20 20 20 20 20 atch */.
10df7 20 20 69 66 28 20 30 21 3d 6d 65 6d 63 6d 70 28 if( 0!=memcmp(
10df8 26 74 42 75 66 5b 50 52 4f 58 59 5f 48 45 41 44 &tBuf[PROXY_HEAD
10df9 45 52 4c 45 4e 5d 2c 20 6d 79 48 6f 73 74 49 44 ERLEN], myHostID
10dfa 2c 20 50 52 4f 58 59 5f 48 4f 53 54 49 44 4c 45 , PROXY_HOSTIDLE
10dfb 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 N) ){.
10dfc 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
10dfd 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 20 20 BUSY;.
10dfe 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
10dff 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 6f . /* do
10e00 6e 27 74 20 62 72 65 61 6b 20 74 68 65 20 6c 6f n't break the lo
10e01 63 6b 20 6f 6e 20 73 68 6f 72 74 20 72 65 61 64 ck on short read
10e02 20 6f 72 20 61 20 76 65 72 73 69 6f 6e 20 6d 69 or a version mi
10e03 73 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 20 20 smatch */.
10e04 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
10e05 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 E_BUSY;.
10e06 7d 0a 20 20 20 20 20 20 20 20 75 73 6c 65 65 70 }. usleep
10e07 28 31 30 30 30 30 30 30 30 29 3b 20 2f 2a 20 77 (10000000); /* w
10e08 61 69 74 20 31 30 20 73 65 63 20 61 6e 64 20 74 ait 10 sec and t
10e09 72 79 20 74 68 65 20 6c 6f 63 6b 20 61 67 61 69 ry the lock agai
10e0a 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e n */. con
10e0b 74 69 6e 75 65 3b 20 0a 20 20 20 20 20 20 7d 0a tinue; . }.
10e0c 20 20 20 20 20 20 0a 20 20 20 20 20 20 61 73 73 . ass
10e0d 65 72 74 28 20 6e 54 72 69 65 73 3d 3d 33 20 29 ert( nTries==3 )
10e0e 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 70 ;. if( 0==p
10e0f 72 6f 78 79 42 72 65 61 6b 43 6f 6e 63 68 4c 6f roxyBreakConchLo
10e10 63 6b 28 70 46 69 6c 65 2c 20 6d 79 48 6f 73 74 ck(pFile, myHost
10e11 49 44 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ID) ){. r
10e12 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
10e13 20 20 20 20 20 20 20 69 66 28 20 6c 6f 63 6b 54 if( lockT
10e14 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype==EXCLUSIVE_L
10e15 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 OCK ){.
10e16 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d rc = conchFile-
10e17 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 >pMethod->xLock(
10e18 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 (sqlite3_file*)c
10e19 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 45 44 onchFile, SHARED
10e1a 5f 4c 4f 43 4b 29 3b 20 20 20 20 20 20 20 20 20 _LOCK);
10e1b 20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 . }.
10e1c 20 20 20 20 69 66 28 20 21 72 63 20 29 7b 0a 20 if( !rc ){.
10e1d 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6f rc = co
10e1e 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 nchFile->pMethod
10e1f 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 ->xLock((sqlite3
10e20 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 _file*)conchFile
10e21 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 , lockType);.
10e22 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
10e23 20 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65 28 20 }. } while(
10e24 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 rc==SQLITE_BUSY
10e25 26 26 20 6e 54 72 69 65 73 3c 33 20 29 3b 0a 20 && nTries<3 );.
10e26 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d . return rc;.}
10e27 0a 0a 2f 2a 20 54 61 6b 65 73 20 74 68 65 20 63 ../* Takes the c
10e28 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 61 onch by taking a
10e29 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 shared lock and
10e2a 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e read the conten
10e2b 74 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a ts conch, if .**
10e2c 20 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f 6e lockPath is non
10e2d 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 20 -NULL, the host
10e2e 49 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 ID and lock file
10e2f 20 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 68 path must match
10e30 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f . A NULL .** lo
10e31 63 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 61 ckPath means tha
10e32 74 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 69 t the lockPath i
10e33 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 n the conch file
10e34 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 66 will be used if
10e35 20 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 44 the .** host ID
10e36 73 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e 65 s match, or a ne
10e37 77 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c w lock path will
10e38 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 75 be generated au
10e39 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 tomatically .**
10e3a 61 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74 and written to t
10e3b 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a he conch file..*
10e3c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
10e3d 78 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 78 xyTakeConch(unix
10e3e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 File *pFile){.
10e3f 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
10e40 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f ext *pCtx = (pro
10e41 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
10e42 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
10e43 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 20 gContext; . .
10e44 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 if( pCtx->conchH
10e45 65 6c 64 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 eld!=0 ){. re
10e46 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
10e47 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 }else{. uni
10e48 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 xFile *conchFile
10e49 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 = pCtx->conchFi
10e4a 6c 65 3b 0a 20 20 20 20 75 75 69 64 5f 74 20 6d le;. uuid_t m
10e4b 79 48 6f 73 74 49 44 3b 0a 20 20 20 20 69 6e 74 yHostID;. int
10e4c 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 pError = 0;.
10e4d 20 63 68 61 72 20 72 65 61 64 42 75 66 5b 50 52 char readBuf[PR
10e4e 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e 5d OXY_MAXCONCHLEN]
10e4f 3b 0a 20 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 ;. char lockP
10e50 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b ath[MAXPATHLEN];
10e51 0a 20 20 20 20 63 68 61 72 20 2a 74 65 6d 70 4c . char *tempL
10e52 6f 63 6b 50 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a ockPath = NULL;.
10e53 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c int rc = SQL
10e54 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 ITE_OK;. int
10e55 63 72 65 61 74 65 43 6f 6e 63 68 20 3d 20 30 3b createConch = 0;
10e56 0a 20 20 20 20 69 6e 74 20 68 6f 73 74 49 64 4d . int hostIdM
10e57 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 69 6e atch = 0;. in
10e58 74 20 72 65 61 64 4c 65 6e 20 3d 20 30 3b 0a 20 t readLen = 0;.
10e59 20 20 20 69 6e 74 20 74 72 79 4f 6c 64 4c 6f 63 int tryOldLoc
10e5a 6b 50 61 74 68 20 3d 20 30 3b 0a 20 20 20 20 69 kPath = 0;. i
10e5b 6e 74 20 66 6f 72 63 65 4e 65 77 4c 6f 63 6b 50 nt forceNewLockP
10e5c 61 74 68 20 3d 20 30 3b 0a 20 20 20 20 0a 20 20 ath = 0;. .
10e5d 20 20 4f 53 54 52 41 43 45 28 28 22 54 41 4b 45 OSTRACE(("TAKE
10e5e 43 4f 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 CONCH %d for %s
10e5f 20 70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 pid=%d\n", conc
10e60 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 hFile->h,.
10e61 20 20 20 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f (pCtx->lo
10e62 63 6b 50 72 6f 78 79 50 61 74 68 20 3f 20 70 43 ckProxyPath ? pC
10e63 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
10e64 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 h : ":auto:"), g
10e65 65 74 70 69 64 28 29 29 29 3b 0a 0a 20 20 20 20 etpid()));..
10e66 72 63 20 3d 20 70 72 6f 78 79 47 65 74 48 6f 73 rc = proxyGetHos
10e67 74 49 44 28 6d 79 48 6f 73 74 49 44 2c 20 26 70 tID(myHostID, &p
10e68 45 72 72 6f 72 29 3b 0a 20 20 20 20 69 66 28 20 Error);. if(
10e69 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 (rc&0xff)==SQLIT
10e6a 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 20 E_IOERR ){.
10e6b 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
10e6c 6f 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 20 20 o = pError;.
10e6d 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 goto end_takec
10e6e 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 onch;. }.
10e6f 72 63 20 3d 20 70 72 6f 78 79 43 6f 6e 63 68 4c rc = proxyConchL
10e70 6f 63 6b 28 70 46 69 6c 65 2c 20 6d 79 48 6f 73 ock(pFile, myHos
10e71 74 49 44 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b tID, SHARED_LOCK
10e72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
10e73 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
10e74 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 goto end_takec
10e75 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 onch;. }.
10e76 2f 2a 20 72 65 61 64 20 74 68 65 20 65 78 69 73 /* read the exis
10e77 74 69 6e 67 20 63 6f 6e 63 68 20 66 69 6c 65 20 ting conch file
10e78 2a 2f 0a 20 20 20 20 72 65 61 64 4c 65 6e 20 3d */. readLen =
10e79 20 73 65 65 6b 41 6e 64 52 65 61 64 28 28 75 6e seekAndRead((un
10e7a 69 78 46 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c ixFile*)conchFil
10e7b 65 2c 20 30 2c 20 72 65 61 64 42 75 66 2c 20 50 e, 0, readBuf, P
10e7c 52 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e ROXY_MAXCONCHLEN
10e7d 29 3b 0a 20 20 20 20 69 66 28 20 72 65 61 64 4c );. if( readL
10e7e 65 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a en<0 ){. /*
10e7f 20 49 2f 4f 20 65 72 72 6f 72 3a 20 6c 61 73 74 I/O error: last
10e80 45 72 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65 Errno set by see
10e81 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20 20 20 20 kAndRead */.
10e82 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
10e83 6e 6f 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e no = conchFile->
10e84 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 lastErrno;.
10e85 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rc = SQLITE_IOE
10e86 52 52 5f 52 45 41 44 3b 0a 20 20 20 20 20 20 67 RR_READ;. g
10e87 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 oto end_takeconc
10e88 68 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 h;. }else if(
10e89 20 72 65 61 64 4c 65 6e 3c 3d 28 50 52 4f 58 59 readLen<=(PROXY
10e8a 5f 48 45 41 44 45 52 4c 45 4e 2b 50 52 4f 58 59 _HEADERLEN+PROXY
10e8b 5f 48 4f 53 54 49 44 4c 45 4e 29 20 7c 7c 20 0a _HOSTIDLEN) || .
10e8c 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 61 rea
10e8d 64 42 75 66 5b 30 5d 21 3d 28 63 68 61 72 29 50 dBuf[0]!=(char)P
10e8e 52 4f 58 59 5f 43 4f 4e 43 48 56 45 52 53 49 4f ROXY_CONCHVERSIO
10e8f 4e 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 61 20 N ){. /* a
10e90 73 68 6f 72 74 20 72 65 61 64 20 6f 72 20 76 65 short read or ve
10e91 72 73 69 6f 6e 20 66 6f 72 6d 61 74 20 6d 69 73 rsion format mis
10e92 6d 61 74 63 68 20 6d 65 61 6e 73 20 77 65 20 6e match means we n
10e93 65 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 eed to create a
10e94 6e 65 77 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f new . ** co
10e95 6e 63 68 20 66 69 6c 65 2e 20 0a 20 20 20 20 20 nch file. .
10e96 20 2a 2f 0a 20 20 20 20 20 20 63 72 65 61 74 65 */. create
10e97 43 6f 6e 63 68 20 3d 20 31 3b 0a 20 20 20 20 7d Conch = 1;. }
10e98 0a 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 68 . /* if the h
10e99 6f 73 74 20 69 64 20 6d 61 74 63 68 65 73 20 61 ost id matches a
10e9a 6e 64 20 74 68 65 20 6c 6f 63 6b 20 70 61 74 68 nd the lock path
10e9b 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 already exists
10e9c 69 6e 20 74 68 65 20 63 6f 6e 63 68 0a 20 20 20 in the conch.
10e9d 20 2a 2a 20 77 65 27 6c 6c 20 74 72 79 20 74 6f ** we'll try to
10e9e 20 75 73 65 20 74 68 65 20 70 61 74 68 20 74 68 use the path th
10e9f 65 72 65 2c 20 69 66 20 77 65 20 63 61 6e 27 74 ere, if we can't
10ea0 20 6f 70 65 6e 20 74 68 61 74 20 70 61 74 68 2c open that path,
10ea1 20 77 65 27 6c 6c 20 0a 20 20 20 20 2a 2a 20 72 we'll . ** r
10ea2 65 74 72 79 20 77 69 74 68 20 61 20 6e 65 77 20 etry with a new
10ea3 61 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 70 auto-generated p
10ea4 61 74 68 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ath . */.
10ea5 64 6f 20 7b 20 2f 2a 20 69 6e 20 63 61 73 65 20 do { /* in case
10ea6 77 65 20 6e 65 65 64 20 74 6f 20 74 72 79 20 61 we need to try a
10ea7 67 61 69 6e 20 66 6f 72 20 61 6e 20 3a 61 75 74 gain for an :aut
10ea8 6f 3a 20 6e 61 6d 65 64 20 6c 6f 63 6b 20 66 69 o: named lock fi
10ea9 6c 65 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28 le */.. if(
10eaa 20 21 63 72 65 61 74 65 43 6f 6e 63 68 20 26 26 !createConch &&
10eab 20 21 66 6f 72 63 65 4e 65 77 4c 6f 63 6b 50 61 !forceNewLockPa
10eac 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 68 6f th ){. ho
10ead 73 74 49 64 4d 61 74 63 68 20 3d 20 21 6d 65 6d stIdMatch = !mem
10eae 63 6d 70 28 26 72 65 61 64 42 75 66 5b 50 52 4f cmp(&readBuf[PRO
10eaf 58 59 5f 48 45 41 44 45 52 4c 45 4e 5d 2c 20 6d XY_HEADERLEN], m
10eb0 79 48 6f 73 74 49 44 2c 20 0a 20 20 20 20 20 20 yHostID, .
10eb1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10eb2 20 20 20 20 20 20 20 20 20 20 20 20 50 52 4f 58 PROX
10eb3 59 5f 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 Y_HOSTIDLEN);.
10eb4 20 20 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 /* if the
10eb5 63 6f 6e 63 68 20 68 61 73 20 64 61 74 61 20 63 conch has data c
10eb6 6f 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74 65 ompare the conte
10eb7 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 nts */. i
10eb8 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 f( !pCtx->lockPr
10eb9 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20 20 20 oxyPath ){.
10eba 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74 6f /* for auto
10ebb 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f 63 -named local loc
10ebc 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68 65 k file, just che
10ebd 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20 61 ck the host ID a
10ebe 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 20 20 nd we'll.
10ebf 20 20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c ** use the l
10ec0 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 ocal lock file p
10ec1 61 74 68 20 74 68 61 74 27 73 20 61 6c 72 65 61 ath that's alrea
10ec2 64 79 20 69 6e 20 74 68 65 72 65 0a 20 20 20 20 dy in there.
10ec3 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
10ec4 20 20 20 20 69 66 28 20 68 6f 73 74 49 64 4d 61 if( hostIdMa
10ec5 74 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20 tch ){.
10ec6 20 20 20 73 69 7a 65 5f 74 20 70 61 74 68 4c 65 size_t pathLe
10ec7 6e 20 3d 20 28 72 65 61 64 4c 65 6e 20 2d 20 50 n = (readLen - P
10ec8 52 4f 58 59 5f 50 41 54 48 49 4e 44 45 58 29 3b ROXY_PATHINDEX);
10ec9 0a 20 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 . .
10eca 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 61 if( pa
10ecb 74 68 4c 65 6e 3e 3d 4d 41 58 50 41 54 48 4c 45 thLen>=MAXPATHLE
10ecc 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 N ){.
10ecd 20 20 20 70 61 74 68 4c 65 6e 3d 4d 41 58 50 41 pathLen=MAXPA
10ece 54 48 4c 45 4e 2d 31 3b 0a 20 20 20 20 20 20 20 THLEN-1;.
10ecf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
10ed0 20 20 20 6d 65 6d 63 70 79 28 6c 6f 63 6b 50 61 memcpy(lockPa
10ed1 74 68 2c 20 26 72 65 61 64 42 75 66 5b 50 52 4f th, &readBuf[PRO
10ed2 58 59 5f 50 41 54 48 49 4e 44 45 58 5d 2c 20 70 XY_PATHINDEX], p
10ed3 61 74 68 4c 65 6e 29 3b 0a 20 20 20 20 20 20 20 athLen);.
10ed4 20 20 20 20 20 6c 6f 63 6b 50 61 74 68 5b 70 61 lockPath[pa
10ed5 74 68 4c 65 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 thLen] = 0;.
10ed6 20 20 20 20 20 20 20 20 74 65 6d 70 4c 6f 63 6b tempLock
10ed7 50 61 74 68 20 3d 20 6c 6f 63 6b 50 61 74 68 3b Path = lockPath;
10ed8 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 72 79 . try
10ed9 4f 6c 64 4c 6f 63 6b 50 61 74 68 20 3d 20 31 3b OldLockPath = 1;
10eda 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 . /*
10edb 63 72 65 61 74 65 20 61 20 63 6f 70 79 20 6f 66 create a copy of
10edc 20 74 68 65 20 6c 6f 63 6b 20 70 61 74 68 20 69 the lock path i
10edd 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 74 f the conch is t
10ede 61 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 aken */.
10edf 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b goto end_tak
10ee0 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 econch;.
10ee1 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 }. }els
10ee2 65 20 69 66 28 20 68 6f 73 74 49 64 4d 61 74 63 e if( hostIdMatc
10ee3 68 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h.
10ee4 20 26 26 20 21 73 74 72 6e 63 6d 70 28 70 43 74 && !strncmp(pCt
10ee5 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
10ee6 2c 20 26 72 65 61 64 42 75 66 5b 50 52 4f 58 59 , &readBuf[PROXY
10ee7 5f 50 41 54 48 49 4e 44 45 58 5d 2c 0a 20 20 20 _PATHINDEX],.
10ee8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ee9 20 20 20 20 20 20 20 20 72 65 61 64 4c 65 6e 2d readLen-
10eea 50 52 4f 58 59 5f 50 41 54 48 49 4e 44 45 58 29 PROXY_PATHINDEX)
10eeb 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 . ){.
10eec 20 20 20 20 20 20 2f 2a 20 63 6f 6e 63 68 20 68 /* conch h
10eed 6f 73 74 20 61 6e 64 20 6c 6f 63 6b 20 70 61 74 ost and lock pat
10eee 68 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 20 h match */.
10eef 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 goto end_ta
10ef0 6b 65 63 6f 6e 63 68 3b 20 0a 20 20 20 20 20 20 keconch; .
10ef1 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
10ef2 20 20 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 74 . /* if t
10ef3 68 65 20 63 6f 6e 63 68 20 69 73 6e 27 74 20 77 he conch isn't w
10ef4 72 69 74 61 62 6c 65 20 61 6e 64 20 64 6f 65 73 ritable and does
10ef5 6e 27 74 20 6d 61 74 63 68 2c 20 77 65 20 63 61 n't match, we ca
10ef6 6e 27 74 20 74 61 6b 65 20 69 74 20 2a 2f 0a 20 n't take it */.
10ef7 20 20 20 20 20 69 66 28 20 28 63 6f 6e 63 68 46 if( (conchF
10ef8 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 26 4f ile->openFlags&O
10ef9 5f 52 44 57 52 29 20 3d 3d 20 30 20 29 7b 0a 20 _RDWR) == 0 ){.
10efa 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
10efb 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 TE_BUSY;.
10efc 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f goto end_takeco
10efd 6e 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nch;. }.
10efe 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 65 69 74 . /* eit
10eff 68 65 72 20 74 68 65 20 63 6f 6e 63 68 20 64 69 her the conch di
10f00 64 6e 27 74 20 6d 61 74 63 68 20 6f 72 20 77 65 dn't match or we
10f01 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74 65 20 need to create
10f02 61 20 6e 65 77 20 6f 6e 65 20 2a 2f 0a 20 20 20 a new one */.
10f03 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f if( !pCtx->lo
10f04 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 ckProxyPath ){.
10f05 20 20 20 20 20 20 20 70 72 6f 78 79 47 65 74 4c proxyGetL
10f06 6f 63 6b 50 61 74 68 28 70 43 74 78 2d 3e 64 62 ockPath(pCtx->db
10f07 50 61 74 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 Path, lockPath,
10f08 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 MAXPATHLEN);.
10f09 20 20 20 20 20 74 65 6d 70 4c 6f 63 6b 50 61 74 tempLockPat
10f0a 68 20 3d 20 6c 6f 63 6b 50 61 74 68 3b 0a 20 20 h = lockPath;.
10f0b 20 20 20 20 20 20 2f 2a 20 63 72 65 61 74 65 20 /* create
10f0c 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6c 6f a copy of the lo
10f0d 63 6b 20 70 61 74 68 20 5f 6f 6e 6c 79 5f 20 69 ck path _only_ i
10f0e 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 74 f the conch is t
10f0f 61 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 7d 0a aken */. }.
10f10 20 20 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 . /*
10f11 75 70 64 61 74 65 20 63 6f 6e 63 68 20 77 69 74 update conch wit
10f12 68 20 68 6f 73 74 20 61 6e 64 20 70 61 74 68 20 h host and path
10f13 28 74 68 69 73 20 77 69 6c 6c 20 66 61 69 6c 20 (this will fail
10f14 69 66 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 if other process
10f15 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20 61 20 . ** has a
10f16 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6c 72 65 shared lock alre
10f17 61 64 79 29 2c 20 69 66 20 74 68 65 20 68 6f 73 ady), if the hos
10f18 74 20 69 64 20 6d 61 74 63 68 65 73 2c 20 75 73 t id matches, us
10f19 65 20 74 68 65 20 62 69 67 0a 20 20 20 20 20 20 e the big.
10f1a 2a 2a 20 73 74 69 63 6b 2e 0a 20 20 20 20 20 20 ** stick..
10f1b 2a 2f 0a 20 20 20 20 20 20 66 75 74 69 6d 65 73 */. futimes
10f1c 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 4e (conchFile->h, N
10f1d 55 4c 4c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ULL);. if(
10f1e 68 6f 73 74 49 64 4d 61 74 63 68 20 26 26 20 21 hostIdMatch && !
10f1f 63 72 65 61 74 65 43 6f 6e 63 68 20 29 7b 0a 20 createConch ){.
10f20 20 20 20 20 20 20 20 69 66 28 20 63 6f 6e 63 68 if( conch
10f21 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 20 26 26 20 File->pInode &&
10f22 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 49 6e 6f 64 conchFile->pInod
10f23 65 2d 3e 6e 53 68 61 72 65 64 3e 31 20 29 7b 0a e->nShared>1 ){.
10f24 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 /* We
10f25 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72 20 61 are trying for a
10f26 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
10f27 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74 68 72 but another thr
10f28 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20 20 20 ead in this.
10f29 20 20 20 20 20 20 20 2a 2a 20 73 61 6d 65 20 70 ** same p
10f2a 72 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 rocess is still
10f2b 68 6f 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64 holding a shared
10f2c 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 20 20 lock. */.
10f2d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
10f2e 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 20 BUSY;. }
10f2f 65 6c 73 65 20 7b 20 20 20 20 20 20 20 20 20 20 else {
10f30 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
10f31 70 72 6f 78 79 43 6f 6e 63 68 4c 6f 63 6b 28 70 proxyConchLock(p
10f32 46 69 6c 65 2c 20 6d 79 48 6f 73 74 49 44 2c 20 File, myHostID,
10f33 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b EXCLUSIVE_LOCK);
10f34 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
10f35 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
10f36 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e rc = conchFile->
10f37 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 pMethod->xLock((
10f38 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f sqlite3_file*)co
10f39 6e 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 49 nchFile, EXCLUSI
10f3a 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 VE_LOCK);.
10f3b 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
10f3c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
10f3d 20 20 20 20 20 63 68 61 72 20 77 72 69 74 65 42 char writeB
10f3e 75 66 66 65 72 5b 50 52 4f 58 59 5f 4d 41 58 43 uffer[PROXY_MAXC
10f3f 4f 4e 43 48 4c 45 4e 5d 3b 0a 20 20 20 20 20 20 ONCHLEN];.
10f40 20 20 69 6e 74 20 77 72 69 74 65 53 69 7a 65 20 int writeSize
10f41 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 0a 20 20 = 0;. .
10f42 20 20 20 20 20 20 77 72 69 74 65 42 75 66 66 65 writeBuffe
10f43 72 5b 30 5d 20 3d 20 28 63 68 61 72 29 50 52 4f r[0] = (char)PRO
10f44 58 59 5f 43 4f 4e 43 48 56 45 52 53 49 4f 4e 3b XY_CONCHVERSION;
10f45 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
10f46 26 77 72 69 74 65 42 75 66 66 65 72 5b 50 52 4f &writeBuffer[PRO
10f47 58 59 5f 48 45 41 44 45 52 4c 45 4e 5d 2c 20 6d XY_HEADERLEN], m
10f48 79 48 6f 73 74 49 44 2c 20 50 52 4f 58 59 5f 48 yHostID, PROXY_H
10f49 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20 20 20 OSTIDLEN);.
10f4a 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 if( pCtx->loc
10f4b 6b 50 72 6f 78 79 50 61 74 68 21 3d 4e 55 4c 4c kProxyPath!=NULL
10f4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74 ){. st
10f4d 72 6c 63 70 79 28 26 77 72 69 74 65 42 75 66 66 rlcpy(&writeBuff
10f4e 65 72 5b 50 52 4f 58 59 5f 50 41 54 48 49 4e 44 er[PROXY_PATHIND
10f4f 45 58 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 EX], pCtx->lockP
10f50 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 roxyPath, MAXPAT
10f51 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 20 20 7d HLEN);. }
10f52 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
10f53 73 74 72 6c 63 70 79 28 26 77 72 69 74 65 42 75 strlcpy(&writeBu
10f54 66 66 65 72 5b 50 52 4f 58 59 5f 50 41 54 48 49 ffer[PROXY_PATHI
10f55 4e 44 45 58 5d 2c 20 74 65 6d 70 4c 6f 63 6b 50 NDEX], tempLockP
10f56 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 ath, MAXPATHLEN)
10f57 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
10f58 20 20 20 20 77 72 69 74 65 53 69 7a 65 20 3d 20 writeSize =
10f59 50 52 4f 58 59 5f 50 41 54 48 49 4e 44 45 58 20 PROXY_PATHINDEX
10f5a 2b 20 73 74 72 6c 65 6e 28 26 77 72 69 74 65 42 + strlen(&writeB
10f5b 75 66 66 65 72 5b 50 52 4f 58 59 5f 50 41 54 48 uffer[PROXY_PATH
10f5c 49 4e 44 45 58 5d 29 3b 0a 20 20 20 20 20 20 20 INDEX]);.
10f5d 20 72 6f 62 75 73 74 5f 66 74 72 75 6e 63 61 74 robust_ftruncat
10f5e 65 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 e(conchFile->h,
10f5f 77 72 69 74 65 53 69 7a 65 29 3b 0a 20 20 20 20 writeSize);.
10f60 20 20 20 20 72 63 20 3d 20 75 6e 69 78 57 72 69 rc = unixWri
10f61 74 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 te((sqlite3_file
10f62 20 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 77 72 *)conchFile, wr
10f63 69 74 65 42 75 66 66 65 72 2c 20 77 72 69 74 65 iteBuffer, write
10f64 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Size, 0);.
10f65 20 20 66 73 79 6e 63 28 63 6f 6e 63 68 46 69 6c fsync(conchFil
10f66 65 2d 3e 68 29 3b 0a 20 20 20 20 20 20 20 20 2f e->h);. /
10f67 2a 20 49 66 20 77 65 20 63 72 65 61 74 65 64 20 * If we created
10f68 61 20 6e 65 77 20 63 6f 6e 63 68 20 66 69 6c 65 a new conch file
10f69 20 28 6e 6f 74 20 6a 75 73 74 20 75 70 64 61 74 (not just updat
10f6a 65 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 ed the contents
10f6b 6f 66 20 61 20 0a 20 20 20 20 20 20 20 20 20 2a of a . *
10f6c 2a 20 76 61 6c 69 64 20 63 6f 6e 63 68 20 66 69 * valid conch fi
10f6d 6c 65 29 2c 20 74 72 79 20 74 6f 20 6d 61 74 63 le), try to matc
10f6e 68 20 74 68 65 20 70 65 72 6d 69 73 73 69 6f 6e h the permission
10f6f 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 s of the databas
10f70 65 20 0a 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 e . */.
10f71 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
10f72 51 4c 49 54 45 5f 4f 4b 20 26 26 20 63 72 65 61 QLITE_OK && crea
10f73 74 65 43 6f 6e 63 68 20 29 7b 0a 20 20 20 20 20 teConch ){.
10f74 20 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 struct stat
10f75 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 20 20 buf;.
10f76 69 6e 74 20 65 72 72 20 3d 20 6f 73 46 73 74 61 int err = osFsta
10f77 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26 62 75 66 t(pFile->h, &buf
10f78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
10f79 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 err==0 ){.
10f7a 20 20 20 20 20 20 20 6d 6f 64 65 5f 74 20 63 6d mode_t cm
10f7b 6f 64 65 20 3d 20 62 75 66 2e 73 74 5f 6d 6f 64 ode = buf.st_mod
10f7c 65 26 28 53 5f 49 52 55 53 52 7c 53 5f 49 57 55 e&(S_IRUSR|S_IWU
10f7d 53 52 20 7c 20 53 5f 49 52 47 52 50 7c 53 5f 49 SR | S_IRGRP|S_I
10f7e 57 47 52 50 20 7c 0a 20 20 20 20 20 20 20 20 20 WGRP |.
10f7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 S
10f81 5f 49 52 4f 54 48 7c 53 5f 49 57 4f 54 48 29 3b _IROTH|S_IWOTH);
10f82 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 . /*
10f83 74 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65 try to match the
10f84 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 52 database file R
10f85 2f 57 20 70 65 72 6d 69 73 73 69 6f 6e 73 2c 20 /W permissions,
10f86 69 67 6e 6f 72 65 20 66 61 69 6c 75 72 65 20 2a ignore failure *
10f87 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
10f88 5f 50 52 4f 58 59 5f 44 45 42 55 47 0a 20 20 20 _PROXY_DEBUG.
10f89 20 20 20 20 20 20 20 20 20 6f 73 46 63 68 6d 6f osFchmo
10f8a 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 d(conchFile->h,
10f8b 63 6d 6f 64 65 29 3b 0a 23 65 6c 73 65 0a 20 20 cmode);.#else.
10f8c 20 20 20 20 20 20 20 20 20 20 64 6f 7b 0a 20 20 do{.
10f8d 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
10f8e 20 6f 73 46 63 68 6d 6f 64 28 63 6f 6e 63 68 46 osFchmod(conchF
10f8f 69 6c 65 2d 3e 68 2c 20 63 6d 6f 64 65 29 3b 0a ile->h, cmode);.
10f90 20 20 20 20 20 20 20 20 20 20 20 20 7d 77 68 69 }whi
10f91 6c 65 28 20 72 63 3d 3d 28 2d 31 29 20 26 26 20 le( rc==(-1) &&
10f92 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 29 3b 0a errno==EINTR );.
10f93 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
10f94 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 rc!=0 ){.
10f95 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 int code
10f96 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 = errno;.
10f97 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 fprintf(s
10f98 74 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 tderr, "fchmod %
10f99 6f 20 46 41 49 4c 45 44 20 77 69 74 68 20 25 64 o FAILED with %d
10f9a 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 %s\n",.
10f9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d cm
10f9c 6f 64 65 2c 20 63 6f 64 65 2c 20 73 74 72 65 72 ode, code, strer
10f9d 72 6f 72 28 63 6f 64 65 29 29 3b 0a 20 20 20 20 ror(code));.
10f9e 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b } else {
10f9f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 . f
10fa0 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 printf(stderr, "
10fa1 66 63 68 6d 6f 64 20 25 6f 20 53 55 43 43 45 44 fchmod %o SUCCED
10fa2 45 44 5c 6e 22 2c 63 6d 6f 64 65 29 3b 0a 20 20 ED\n",cmode);.
10fa3 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
10fa4 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
10fa5 20 20 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 int cod
10fa6 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 e = errno;.
10fa7 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 fprintf(s
10fa8 74 64 65 72 72 2c 20 22 53 54 41 54 20 46 41 49 tderr, "STAT FAI
10fa9 4c 45 44 5b 25 64 5d 20 77 69 74 68 20 25 64 20 LED[%d] with %d
10faa 25 73 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 %s\n", .
10fab 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72 2c err,
10fac 20 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 code, strerror(
10fad 63 6f 64 65 29 29 3b 0a 23 65 6e 64 69 66 0a 20 code));.#endif.
10fae 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
10faf 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
10fb0 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d conchFile->pM
10fb1 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 ethod->xUnlock((
10fb2 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f sqlite3_file*)co
10fb3 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f nchFile, SHARED_
10fb4 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 0a 20 20 LOCK);. .
10fb5 20 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3a end_takeconch:
10fb6 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 28 28 . OSTRACE((
10fb7 22 54 52 41 4e 53 50 52 4f 58 59 3a 20 43 4c 4f "TRANSPROXY: CLO
10fb8 53 45 20 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 SE %d\n", pFile
10fb9 2d 3e 68 29 29 3b 0a 20 20 20 20 20 20 69 66 28 ->h));. if(
10fba 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
10fbb 26 20 70 46 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 & pFile->openFla
10fbc 67 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e gs ){. in
10fbd 74 20 66 64 3b 0a 20 20 20 20 20 20 20 20 69 66 t fd;. if
10fbe 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 7b ( pFile->h>=0 ){
10fbf 0a 20 20 20 20 20 20 20 20 20 20 72 6f 62 75 73 . robus
10fc0 74 5f 63 6c 6f 73 65 28 70 46 69 6c 65 2c 20 70 t_close(pFile, p
10fc1 46 69 6c 65 2d 3e 68 2c 20 5f 5f 4c 49 4e 45 5f File->h, __LINE_
10fc2 5f 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 _);. }.
10fc3 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d pFile->h =
10fc4 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 66 64 20 -1;. fd
10fc5 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 70 43 = robust_open(pC
10fc6 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c tx->dbPath, pFil
10fc7 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 2c 20 30 29 e->openFlags, 0)
10fc8 3b 0a 20 20 20 20 20 20 20 20 4f 53 54 52 41 43 ;. OSTRAC
10fc9 45 28 28 22 54 52 41 4e 53 50 52 4f 58 59 3a 20 E(("TRANSPROXY:
10fca 4f 50 45 4e 20 20 25 64 5c 6e 22 2c 20 66 64 29 OPEN %d\n", fd)
10fcb 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 );. if( f
10fcc 64 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 d>=0 ){.
10fcd 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 66 64 3b pFile->h = fd;
10fce 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
10fcf 20 20 20 20 20 20 20 20 20 20 72 63 3d 53 51 4c rc=SQL
10fd0 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 ITE_CANTOPEN_BKP
10fd1 54 3b 20 2f 2a 20 53 51 4c 49 54 45 5f 42 55 53 T; /* SQLITE_BUS
10fd2 59 3f 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 Y? proxyTakeConc
10fd3 68 20 63 61 6c 6c 65 64 0a 20 20 20 20 20 20 20 h called.
10fd4 20 20 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 during locki
10fd5 6e 67 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a ng */. }.
10fd6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
10fd7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
10fd8 26 26 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 && !pCtx->lockPr
10fd9 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 20 20 63 oxy ){. c
10fda 68 61 72 20 2a 70 61 74 68 20 3d 20 74 65 6d 70 har *path = temp
10fdb 4c 6f 63 6b 50 61 74 68 20 3f 20 74 65 6d 70 4c LockPath ? tempL
10fdc 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 2d 3e ockPath : pCtx->
10fdd 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 lockProxyPath;.
10fde 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 rc = prox
10fdf 79 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 yCreateUnixFile(
10fe0 70 61 74 68 2c 20 26 70 43 74 78 2d 3e 6c 6f 63 path, &pCtx->loc
10fe1 6b 50 72 6f 78 79 2c 20 31 29 3b 0a 20 20 20 20 kProxy, 1);.
10fe2 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
10fe3 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c TE_OK && rc!=SQL
10fe4 49 54 45 5f 4e 4f 4d 45 4d 20 26 26 20 74 72 79 ITE_NOMEM && try
10fe5 4f 6c 64 4c 6f 63 6b 50 61 74 68 20 29 7b 0a 20 OldLockPath ){.
10fe6 20 20 20 20 20 20 20 20 20 2f 2a 20 77 65 20 63 /* we c
10fe7 6f 75 6c 64 6e 27 74 20 63 72 65 61 74 65 20 74 ouldn't create t
10fe8 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 he proxy lock fi
10fe9 6c 65 20 77 69 74 68 20 74 68 65 20 6f 6c 64 20 le with the old
10fea 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 68 0a 20 lock file path.
10feb 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 ** so
10fec 74 72 79 20 61 67 61 69 6e 20 76 69 61 20 61 75 try again via au
10fed 74 6f 2d 6e 61 6d 69 6e 67 20 0a 20 20 20 20 20 to-naming .
10fee 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
10fef 20 20 20 66 6f 72 63 65 4e 65 77 4c 6f 63 6b 50 forceNewLockP
10ff0 61 74 68 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 ath = 1;.
10ff1 20 20 20 74 72 79 4f 6c 64 4c 6f 63 6b 50 61 74 tryOldLockPat
10ff2 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 h = 0;.
10ff3 20 63 6f 6e 74 69 6e 75 65 3b 20 2f 2a 20 67 6f continue; /* go
10ff4 20 62 61 63 6b 20 74 6f 20 74 68 65 20 64 6f 20 back to the do
10ff5 7b 7d 20 77 68 69 6c 65 20 73 74 61 72 74 20 70 {} while start p
10ff6 6f 69 6e 74 2c 20 74 72 79 20 61 67 61 69 6e 20 oint, try again
10ff7 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 */. }.
10ff8 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 }. if( r
10ff9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
10ffa 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 65 64 20 /* Need
10ffb 74 6f 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f to make a copy o
10ffc 66 20 70 61 74 68 20 69 66 20 77 65 20 65 78 74 f path if we ext
10ffd 72 61 63 74 65 64 20 74 68 65 20 76 61 6c 75 65 racted the value
10ffe 0a 20 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f . ** fro
10fff 6d 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 m the conch file
11000 20 6f 72 20 74 68 65 20 70 61 74 68 20 77 61 73 or the path was
11001 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 allocated on th
11002 65 20 73 74 61 63 6b 0a 20 20 20 20 20 20 20 20 e stack.
11003 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
11004 74 65 6d 70 4c 6f 63 6b 50 61 74 68 20 29 7b 0a tempLockPath ){.
11005 20 20 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e pCtx->
11006 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 lockProxyPath =
11007 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
11008 30 2c 20 74 65 6d 70 4c 6f 63 6b 50 61 74 68 29 0, tempLockPath)
11009 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
1100a 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 !pCtx->lockProxy
1100b 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 Path ){.
1100c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1100d 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 NOMEM;.
1100e 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1100f 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 }. if( r
11010 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
11011 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 63 6f pCtx->co
11012 6e 63 68 48 65 6c 64 20 3d 20 31 3b 0a 20 20 20 nchHeld = 1;.
11013 20 20 20 20 20 0a 20 20 20 20 20 20 20 20 69 66 . if
11014 28 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 ( pCtx->lockProx
11015 79 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 y->pMethod == &a
11016 66 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 fpIoMethods ){.
11017 20 20 20 20 20 20 20 20 20 61 66 70 4c 6f 63 6b afpLock
11018 69 6e 67 43 6f 6e 74 65 78 74 20 2a 61 66 70 43 ingContext *afpC
11019 74 78 3b 0a 20 20 20 20 20 20 20 20 20 20 61 66 tx;. af
1101a 70 43 74 78 20 3d 20 28 61 66 70 4c 6f 63 6b 69 pCtx = (afpLocki
1101b 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 43 74 78 ngContext *)pCtx
1101c 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 6c 6f 63 ->lockProxy->loc
1101d 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 kingContext;.
1101e 20 20 20 20 20 20 20 61 66 70 43 74 78 2d 3e 64 afpCtx->d
1101f 62 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f bPath = pCtx->lo
11020 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 ckProxyPath;.
11021 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 65 }. } e
11022 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 63 6f lse {. co
11023 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 nchFile->pMethod
11024 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 ->xUnlock((sqlit
11025 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 e3_file*)conchFi
11026 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 le, NO_LOCK);.
11027 20 20 20 20 7d 0a 20 20 20 20 20 20 4f 53 54 52 }. OSTR
11028 41 43 45 28 28 22 54 41 4b 45 43 4f 4e 43 48 20 ACE(("TAKECONCH
11029 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 %d %s\n", conch
1102a 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
1102b 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 rc==SQLI
1102c 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c TE_OK?"ok":"fail
1102d 65 64 22 29 29 3b 0a 20 20 20 20 20 20 72 65 74 ed"));. ret
1102e 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 20 77 68 urn rc;. } wh
1102f 69 6c 65 20 28 31 29 3b 20 2f 2a 20 69 6e 20 63 ile (1); /* in c
11030 61 73 65 20 77 65 20 6e 65 65 64 20 74 6f 20 72 ase we need to r
11031 65 74 72 79 20 74 68 65 20 3a 61 75 74 6f 3a 20 etry the :auto:
11032 6c 6f 63 6b 20 66 69 6c 65 20 2d 20 0a 20 20 20 lock file - .
11033 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
11034 20 77 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 we should never
11035 20 67 65 74 20 68 65 72 65 20 65 78 63 65 70 74 get here except
11036 20 76 69 61 20 74 68 65 20 27 63 6f 6e 74 69 6e via the 'contin
11037 75 65 27 20 63 61 6c 6c 2e 20 2a 2f 0a 20 20 7d ue' call. */. }
11038 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 .}../*.** If pFi
11039 6c 65 20 68 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 le holds a lock
1103a 6f 6e 20 61 20 63 6f 6e 63 68 20 66 69 6c 65 2c on a conch file,
1103b 20 74 68 65 6e 20 72 65 6c 65 61 73 65 20 74 68 then release th
1103c 61 74 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 at lock..*/.stat
1103d 69 63 20 69 6e 74 20 70 72 6f 78 79 52 65 6c 65 ic int proxyRele
1103e 61 73 65 43 6f 6e 63 68 28 75 6e 69 78 46 69 6c aseConch(unixFil
1103f 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 e *pFile){. int
11040 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
11041 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 72 /* Subr
11042 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 20 63 6f outine return co
11043 64 65 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 de */. proxyLoc
11044 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 kingContext *pCt
11045 78 3b 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 x; /* The locki
11046 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 74 ng context for t
11047 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 2a 2f he proxy lock */
11048 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e . unixFile *con
11049 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20 2f chFile; /
1104a 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f * Name of the co
1104b 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 70 nch file */.. p
1104c 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b Ctx = (proxyLock
1104d 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 ingContext *)pFi
1104e 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
1104f 78 74 3b 0a 20 20 63 6f 6e 63 68 46 69 6c 65 20 xt;. conchFile
11050 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c = pCtx->conchFil
11051 65 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 52 e;. OSTRACE(("R
11052 45 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 64 20 ELEASECONCH %d
11053 66 6f 72 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 for %s pid=%d\n"
11054 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a , conchFile->h,.
11055 20 20 20 20 20 20 20 20 20 20 20 28 70 43 74 78 (pCtx
11056 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 ->lockProxyPath
11057 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 ? pCtx->lockProx
11058 79 50 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22 yPath : ":auto:"
11059 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 67 ), . g
1105a 65 74 70 69 64 28 29 29 29 3b 0a 20 20 69 66 28 etpid()));. if(
1105b 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 pCtx->conchHeld
1105c 3e 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 >0 ){. rc = c
1105d 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f onchFile->pMetho
1105e 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 d->xUnlock((sqli
1105f 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 te3_file*)conchF
11060 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 ile, NO_LOCK);.
11061 20 7d 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 }. pCtx->conch
11062 48 65 6c 64 20 3d 20 30 3b 0a 20 20 4f 53 54 52 Held = 0;. OSTR
11063 41 43 45 28 28 22 52 45 4c 45 41 53 45 43 4f 4e ACE(("RELEASECON
11064 43 48 20 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f CH %d %s\n", co
11065 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 nchFile->h,.
11066 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49 (rc==SQLI
11067 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 TE_OK ? "ok" : "
11068 66 61 69 6c 65 64 22 29 29 29 3b 0a 20 20 72 65 failed")));. re
11069 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1106a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 * Given the name
1106b 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66 of a database f
1106c 69 6c 65 2c 20 63 6f 6d 70 75 74 65 20 74 68 65 ile, compute the
1106d 20 6e 61 6d 65 20 6f 66 20 69 74 73 20 63 6f 6e name of its con
1106e 63 68 20 66 69 6c 65 2e 0a 2a 2a 20 53 74 6f 72 ch file..** Stor
1106f 65 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 e the conch file
11070 6e 61 6d 65 20 69 6e 20 6d 65 6d 6f 72 79 20 6f name in memory o
11071 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
11072 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a ite3_malloc()..*
11073 2a 20 4d 61 6b 65 20 2a 70 43 6f 6e 63 68 50 61 * Make *pConchPa
11074 74 68 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 th point to the
11075 6e 65 77 20 6e 61 6d 65 2e 20 20 52 65 74 75 72 new name. Retur
11076 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
11077 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 53 51 4c uccess.** or SQL
11078 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 75 6e 61 ITE_NOMEM if una
11079 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20 6d 65 ble to obtain me
1107a 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 mory..**.** The
1107b 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e caller is respon
1107c 73 69 62 6c 65 20 66 6f 72 20 65 6e 73 75 72 69 sible for ensuri
1107d 6e 67 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f ng that the allo
1107e 63 61 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 cated memory.**
1107f 73 70 61 63 65 20 69 73 20 65 76 65 6e 74 75 61 space is eventua
11080 6c 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a lly freed..**.**
11081 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 69 73 20 *pConchPath is
11082 73 65 74 20 74 6f 20 4e 55 4c 4c 20 69 66 20 61 set to NULL if a
11083 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
11084 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e on error occurs.
11085 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
11086 72 6f 78 79 43 72 65 61 74 65 43 6f 6e 63 68 50 roxyCreateConchP
11087 61 74 68 6e 61 6d 65 28 63 68 61 72 20 2a 64 62 athname(char *db
11088 50 61 74 68 2c 20 63 68 61 72 20 2a 2a 70 43 6f Path, char **pCo
11089 6e 63 68 50 61 74 68 29 7b 0a 20 20 69 6e 74 20 nchPath){. int
1108a 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1108b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
1108c 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 p counter */. i
1108d 6e 74 20 6c 65 6e 20 3d 20 28 69 6e 74 29 73 74 nt len = (int)st
1108e 72 6c 65 6e 28 64 62 50 61 74 68 29 3b 20 2f 2a rlen(dbPath); /*
1108f 20 4c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 62 Length of datab
11090 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 2d 20 64 ase filename - d
11091 62 50 61 74 68 20 2a 2f 0a 20 20 63 68 61 72 20 bPath */. char
11092 2a 63 6f 6e 63 68 50 61 74 68 3b 20 20 20 20 20 *conchPath;
11093 20 20 20 20 20 20 20 20 20 2f 2a 20 62 75 66 66 /* buff
11094 65 72 20 69 6e 20 77 68 69 63 68 20 74 6f 20 63 er in which to c
11095 6f 6e 73 74 72 75 63 74 20 63 6f 6e 63 68 20 6e onstruct conch n
11096 61 6d 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c ame */.. /* All
11097 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 ocate space for
11098 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 6e 61 the conch filena
11099 6d 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a me and initializ
1109a 65 20 74 68 65 20 6e 61 6d 65 20 74 6f 0a 20 20 e the name to.
1109b 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 ** the name of t
1109c 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 he original data
1109d 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 20 20 0a base file. */ .
1109e 20 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 3d 20 *pConchPath =
1109f 63 6f 6e 63 68 50 61 74 68 20 3d 20 28 63 68 61 conchPath = (cha
110a0 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c r *)sqlite3_mall
110a1 6f 63 28 6c 65 6e 20 2b 20 38 29 3b 0a 20 20 69 oc(len + 8);. i
110a2 66 28 20 63 6f 6e 63 68 50 61 74 68 3d 3d 30 20 f( conchPath==0
110a3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
110a4 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
110a5 20 20 6d 65 6d 63 70 79 28 63 6f 6e 63 68 50 61 memcpy(conchPa
110a6 74 68 2c 20 64 62 50 61 74 68 2c 20 6c 65 6e 2b th, dbPath, len+
110a7 31 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 77 20 1);. . /* now
110a8 69 6e 73 65 72 74 20 61 20 22 2e 22 20 62 65 66 insert a "." bef
110a9 6f 72 65 20 74 68 65 20 6c 61 73 74 20 2f 20 63 ore the last / c
110aa 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 66 6f haracter */. fo
110ab 72 28 20 69 3d 28 6c 65 6e 2d 31 29 3b 20 69 3e r( i=(len-1); i>
110ac 3d 30 3b 20 69 2d 2d 20 29 7b 0a 20 20 20 20 69 =0; i-- ){. i
110ad 66 28 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d f( conchPath[i]=
110ae 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 69 2b ='/' ){. i+
110af 2b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a +;. break;.
110b0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6e 63 }. }. conc
110b1 68 50 61 74 68 5b 69 5d 3d 27 2e 27 3b 0a 20 20 hPath[i]='.';.
110b2 77 68 69 6c 65 20 28 20 69 3c 6c 65 6e 20 29 7b while ( i<len ){
110b3 0a 20 20 20 20 63 6f 6e 63 68 50 61 74 68 5b 69 . conchPath[i
110b4 2b 31 5d 3d 64 62 50 61 74 68 5b 69 5d 3b 0a 20 +1]=dbPath[i];.
110b5 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f i++;. }.. /
110b6 2a 20 61 70 70 65 6e 64 20 74 68 65 20 22 2d 63 * append the "-c
110b7 6f 6e 63 68 22 20 73 75 66 66 69 78 20 74 6f 20 onch" suffix to
110b8 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 6d 65 the file */. me
110b9 6d 63 70 79 28 26 63 6f 6e 63 68 50 61 74 68 5b mcpy(&conchPath[
110ba 69 2b 31 5d 2c 20 22 2d 63 6f 6e 63 68 22 2c 20 i+1], "-conch",
110bb 37 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 69 7);. assert( (i
110bc 6e 74 29 73 74 72 6c 65 6e 28 63 6f 6e 63 68 50 nt)strlen(conchP
110bd 61 74 68 29 20 3d 3d 20 6c 65 6e 2b 37 20 29 3b ath) == len+7 );
110be 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
110bf 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 20 54 61 6b E_OK;.}.../* Tak
110c0 65 73 20 61 20 66 75 6c 6c 79 20 63 6f 6e 66 69 es a fully confi
110c1 67 75 72 65 64 20 70 72 6f 78 79 20 6c 6f 63 6b gured proxy lock
110c2 69 6e 67 2d 73 74 79 6c 65 20 75 6e 69 78 20 66 ing-style unix f
110c3 69 6c 65 20 61 6e 64 20 73 77 69 74 63 68 65 73 ile and switches
110c4 0a 2a 2a 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f .** the local lo
110c5 63 6b 20 66 69 6c 65 20 70 61 74 68 20 0a 2a 2f ck file path .*/
110c6 0a 73 74 61 74 69 63 20 69 6e 74 20 73 77 69 74 .static int swit
110c7 63 68 4c 6f 63 6b 50 72 6f 78 79 50 61 74 68 28 chLockProxyPath(
110c8 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c unixFile *pFile,
110c9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 const char *pat
110ca 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b h) {. proxyLock
110cb 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 ingContext *pCtx
110cc 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 = (proxyLocking
110cd 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e Context*)pFile->
110ce 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a lockingContext;.
110cf 20 20 63 68 61 72 20 2a 6f 6c 64 50 61 74 68 20 char *oldPath
110d0 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 = pCtx->lockProx
110d1 79 50 61 74 68 3b 0a 20 20 69 6e 74 20 72 63 20 yPath;. int rc
110d2 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
110d3 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 if( pFile->eFile
110d4 4c 6f 63 6b 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b Lock!=NO_LOCK ){
110d5 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
110d6 54 45 5f 42 55 53 59 3b 0a 20 20 7d 20 20 0a 0a TE_BUSY;. } ..
110d7 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 /* nothing to
110d8 64 6f 20 69 66 20 74 68 65 20 70 61 74 68 20 69 do if the path i
110d9 73 20 4e 55 4c 4c 2c 20 3a 61 75 74 6f 3a 20 6f s NULL, :auto: o
110da 72 20 6d 61 74 63 68 65 73 20 74 68 65 20 65 78 r matches the ex
110db 69 73 74 69 6e 67 20 70 61 74 68 20 2a 2f 0a 20 isting path */.
110dc 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20 70 61 if( !path || pa
110dd 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c 20 21 th[0]=='\0' || !
110de 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22 3a 61 strcmp(path, ":a
110df 75 74 6f 3a 22 29 20 7c 7c 0a 20 20 20 20 28 6f uto:") ||. (o
110e0 6c 64 50 61 74 68 20 26 26 20 21 73 74 72 6e 63 ldPath && !strnc
110e1 6d 70 28 6f 6c 64 50 61 74 68 2c 20 70 61 74 68 mp(oldPath, path
110e2 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 29 20 29 , MAXPATHLEN)) )
110e3 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
110e4 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b ITE_OK;. }else{
110e5 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c . unixFile *l
110e6 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 74 78 2d ockProxy = pCtx-
110e7 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20 >lockProxy;.
110e8 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3d pCtx->lockProxy=
110e9 4e 55 4c 4c 3b 0a 20 20 20 20 70 43 74 78 2d 3e NULL;. pCtx->
110ea 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30 3b 0a 20 conchHeld = 0;.
110eb 20 20 20 69 66 28 20 6c 6f 63 6b 50 72 6f 78 79 if( lockProxy
110ec 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 !=NULL ){.
110ed 72 63 3d 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d rc=lockProxy->pM
110ee 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 ethod->xClose((s
110ef 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 6c 6f qlite3_file *)lo
110f0 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 ckProxy);.
110f1 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
110f2 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 rc;. sqlite
110f3 33 5f 66 72 65 65 28 6c 6f 63 6b 50 72 6f 78 79 3_free(lockProxy
110f4 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
110f5 69 74 65 33 5f 66 72 65 65 28 6f 6c 64 50 61 74 ite3_free(oldPat
110f6 68 29 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f h);. pCtx->lo
110f7 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 71 ckProxyPath = sq
110f8 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c lite3DbStrDup(0,
110f9 20 70 61 74 68 29 3b 0a 20 20 7d 0a 20 20 0a 20 path);. }. .
110fa 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
110fb 2a 0a 2a 2a 20 70 46 69 6c 65 20 69 73 20 61 20 *.** pFile is a
110fc 66 69 6c 65 20 74 68 61 74 20 68 61 73 20 62 65 file that has be
110fd 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 61 20 70 en opened by a p
110fe 72 69 6f 72 20 78 4f 70 65 6e 20 63 61 6c 6c 2e rior xOpen call.
110ff 20 20 64 62 50 61 74 68 0a 2a 2a 20 69 73 20 61 dbPath.** is a
11100 20 73 74 72 69 6e 67 20 62 75 66 66 65 72 20 61 string buffer a
11101 74 20 6c 65 61 73 74 20 4d 41 58 50 41 54 48 4c t least MAXPATHL
11102 45 4e 2b 31 20 63 68 61 72 61 63 74 65 72 73 20 EN+1 characters
11103 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 in size..**.** T
11104 68 69 73 20 72 6f 75 74 69 6e 65 20 66 69 6e 64 his routine find
11105 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 73 the filename as
11106 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 46 sociated with pF
11107 69 6c 65 20 61 6e 64 20 77 72 69 74 65 73 20 69 ile and writes i
11108 74 0a 2a 2a 20 69 6e 74 20 64 62 50 61 74 68 2e t.** int dbPath.
11109 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
1110a 72 6f 78 79 47 65 74 44 62 50 61 74 68 46 6f 72 roxyGetDbPathFor
1110b 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c UnixFile(unixFil
1110c 65 20 2a 70 46 69 6c 65 2c 20 63 68 61 72 20 2a e *pFile, char *
1110d 64 62 50 61 74 68 29 7b 0a 23 69 66 20 64 65 66 dbPath){.#if def
1110e 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a ined(__APPLE__).
1110f 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 if( pFile->pMe
11110 74 68 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 thod == &afpIoMe
11111 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 thods ){. /*
11112 61 66 70 20 73 74 79 6c 65 20 6b 65 65 70 73 20 afp style keeps
11113 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 a reference to t
11114 68 65 20 64 62 20 70 61 74 68 20 69 6e 20 74 68 he db path in th
11115 65 20 66 69 6c 65 50 61 74 68 20 66 69 65 6c 64 e filePath field
11116 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 . ** of the
11117 73 74 72 75 63 74 20 2a 2f 0a 20 20 20 20 61 73 struct */. as
11118 73 65 72 74 28 20 28 69 6e 74 29 73 74 72 6c 65 sert( (int)strle
11119 6e 28 28 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e n((char*)pFile->
1111a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3c lockingContext)<
1111b 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 3b 0a 20 =MAXPATHLEN );.
1111c 20 20 20 73 74 72 6c 63 70 79 28 64 62 50 61 74 strlcpy(dbPat
1111d 68 2c 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 h, ((afpLockingC
1111e 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e ontext *)pFile->
1111f 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 2d lockingContext)-
11120 3e 64 62 50 61 74 68 2c 20 4d 41 58 50 41 54 48 >dbPath, MAXPATH
11121 4c 45 4e 29 3b 0a 20 20 7d 20 65 6c 73 65 0a 23 LEN);. } else.#
11122 65 6e 64 69 66 0a 20 20 69 66 28 20 70 46 69 6c endif. if( pFil
11123 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 64 e->pMethod == &d
11124 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 otlockIoMethods
11125 29 7b 0a 20 20 20 20 2f 2a 20 64 6f 74 20 6c 6f ){. /* dot lo
11126 63 6b 20 73 74 79 6c 65 20 75 73 65 73 20 74 68 ck style uses th
11127 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 e locking contex
11128 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 64 t to store the d
11129 6f 74 20 6c 6f 63 6b 0a 20 20 20 20 2a 2a 20 66 ot lock. ** f
1112a 69 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20 20 20 ile path */.
1112b 69 6e 74 20 6c 65 6e 20 3d 20 73 74 72 6c 65 6e int len = strlen
1112c 28 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e ((char *)pFile->
1112d 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 20 lockingContext)
1112e 2d 20 73 74 72 6c 65 6e 28 44 4f 54 4c 4f 43 4b - strlen(DOTLOCK
1112f 5f 53 55 46 46 49 58 29 3b 0a 20 20 20 20 6d 65 _SUFFIX);. me
11130 6d 63 70 79 28 64 62 50 61 74 68 2c 20 28 63 68 mcpy(dbPath, (ch
11131 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b ar *)pFile->lock
11132 69 6e 67 43 6f 6e 74 65 78 74 2c 20 6c 65 6e 20 ingContext, len
11133 2b 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 + 1);. }else{.
11134 20 20 20 2f 2a 20 61 6c 6c 20 6f 74 68 65 72 20 /* all other
11135 73 74 79 6c 65 73 20 75 73 65 20 74 68 65 20 6c styles use the l
11136 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 ocking context t
11137 6f 20 73 74 6f 72 65 20 74 68 65 20 64 62 20 66 o store the db f
11138 69 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20 20 20 ile path */.
11139 61 73 73 65 72 74 28 20 73 74 72 6c 65 6e 28 28 assert( strlen((
1113a 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 char*)pFile->loc
1113b 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3c 3d 4d 41 kingContext)<=MA
1113c 58 50 41 54 48 4c 45 4e 20 29 3b 0a 20 20 20 20 XPATHLEN );.
1113d 73 74 72 6c 63 70 79 28 64 62 50 61 74 68 2c 20 strlcpy(dbPath,
1113e 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c (char *)pFile->l
1113f 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2c 20 4d ockingContext, M
11140 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 7d 0a AXPATHLEN);. }.
11141 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
11142 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 6b OK;.}../*.** Tak
11143 65 73 20 61 6e 20 61 6c 72 65 61 64 79 20 66 69 es an already fi
11144 6c 6c 65 64 20 69 6e 20 75 6e 69 78 20 66 69 6c lled in unix fil
11145 65 20 61 6e 64 20 61 6c 74 65 72 73 20 69 74 20 e and alters it
11146 73 6f 20 61 6c 6c 20 66 69 6c 65 20 6c 6f 63 6b so all file lock
11147 69 6e 67 20 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 ing .** will be
11148 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 performed on the
11149 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 6c 6f 63 local proxy loc
1114a 6b 20 66 69 6c 65 2e 20 20 54 68 65 20 66 6f 6c k file. The fol
1114b 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 0a 2a 2a lowing fields.**
1114c 20 61 72 65 20 70 72 65 73 65 72 76 65 64 20 69 are preserved i
1114d 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f n the locking co
1114e 6e 74 65 78 74 20 73 6f 20 74 68 61 74 20 74 68 ntext so that th
1114f 65 79 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 ey can be restor
11150 65 64 20 61 6e 64 20 0a 2a 2a 20 74 68 65 20 75 ed and .** the u
11151 6e 69 78 20 73 74 72 75 63 74 75 72 65 20 70 72 nix structure pr
11152 6f 70 65 72 6c 79 20 63 6c 65 61 6e 65 64 20 75 operly cleaned u
11153 70 20 61 74 20 63 6c 6f 73 65 20 74 69 6d 65 3a p at close time:
11154 0a 2a 2a 20 20 2d 3e 6c 6f 63 6b 69 6e 67 43 6f .** ->lockingCo
11155 6e 74 65 78 74 0a 2a 2a 20 20 2d 3e 70 4d 65 74 ntext.** ->pMet
11156 68 6f 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e hod.*/.static in
11157 74 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d t proxyTransform
11158 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c UnixFile(unixFil
11159 65 20 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20 e *pFile, const
1115a 63 68 61 72 20 2a 70 61 74 68 29 20 7b 0a 20 20 char *path) {.
1115b 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
1115c 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 63 68 61 ext *pCtx;. cha
1115d 72 20 64 62 50 61 74 68 5b 4d 41 58 50 41 54 48 r dbPath[MAXPATH
1115e 4c 45 4e 2b 31 5d 3b 20 20 20 20 20 20 20 2f 2a LEN+1]; /*
1115f 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 Name of the dat
11160 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
11161 63 68 61 72 20 2a 6c 6f 63 6b 50 61 74 68 3d 4e char *lockPath=N
11162 55 4c 4c 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ULL;. int rc =
11163 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 0a 20 20 SQLITE_OK;. .
11164 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 if( pFile->eFile
11165 4c 6f 63 6b 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b Lock!=NO_LOCK ){
11166 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
11167 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 TE_BUSY;. }. p
11168 72 6f 78 79 47 65 74 44 62 50 61 74 68 46 6f 72 roxyGetDbPathFor
11169 55 6e 69 78 46 69 6c 65 28 70 46 69 6c 65 2c 20 UnixFile(pFile,
1116a 64 62 50 61 74 68 29 3b 0a 20 20 69 66 28 20 21 dbPath);. if( !
1116b 70 61 74 68 20 7c 7c 20 70 61 74 68 5b 30 5d 3d path || path[0]=
1116c 3d 27 5c 30 27 20 7c 7c 20 21 73 74 72 63 6d 70 ='\0' || !strcmp
1116d 28 70 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 29 (path, ":auto:")
1116e 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 50 61 74 68 ){. lockPath
1116f 3d 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a =NULL;. }else{.
11170 20 20 20 20 6c 6f 63 6b 50 61 74 68 3d 28 63 68 lockPath=(ch
11171 61 72 20 2a 29 70 61 74 68 3b 0a 20 20 7d 0a 20 ar *)path;. }.
11172 20 0a 20 20 4f 53 54 52 41 43 45 28 28 22 54 52 . OSTRACE(("TR
11173 41 4e 53 50 52 4f 58 59 20 20 25 64 20 66 6f 72 ANSPROXY %d for
11174 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 %s pid=%d\n", p
11175 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
11176 20 20 20 20 28 6c 6f 63 6b 50 61 74 68 20 3f 20 (lockPath ?
11177 6c 6f 63 6b 50 61 74 68 20 3a 20 22 3a 61 75 74 lockPath : ":aut
11178 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29 29 29 o:"), getpid()))
11179 3b 0a 0a 20 20 70 43 74 78 20 3d 20 73 71 6c 69 ;.. pCtx = sqli
1117a 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 te3_malloc( size
1117b 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 69 of(*pCtx) );. i
1117c 66 28 20 70 43 74 78 3d 3d 30 20 29 7b 0a 20 20 f( pCtx==0 ){.
1117d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1117e 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d NOMEM;. }. mem
1117f 73 65 74 28 70 43 74 78 2c 20 30 2c 20 73 69 7a set(pCtx, 0, siz
11180 65 6f 66 28 2a 70 43 74 78 29 29 3b 0a 0a 20 20 eof(*pCtx));..
11181 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 rc = proxyCreate
11182 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 64 62 ConchPathname(db
11183 50 61 74 68 2c 20 26 70 43 74 78 2d 3e 63 6f 6e Path, &pCtx->con
11184 63 68 46 69 6c 65 50 61 74 68 29 3b 0a 20 20 69 chFilePath);. i
11185 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
11186 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f ){. rc = pro
11187 78 79 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 xyCreateUnixFile
11188 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 (pCtx->conchFile
11189 50 61 74 68 2c 20 26 70 43 74 78 2d 3e 63 6f 6e Path, &pCtx->con
1118a 63 68 46 69 6c 65 2c 20 30 29 3b 0a 20 20 20 20 chFile, 0);.
1118b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 if( rc==SQLITE_C
1118c 41 4e 54 4f 50 45 4e 20 26 26 20 28 28 70 46 69 ANTOPEN && ((pFi
1118d 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 26 4f 5f le->openFlags&O_
1118e 52 44 57 52 29 20 3d 3d 20 30 29 20 29 7b 0a 20 RDWR) == 0) ){.
1118f 20 20 20 20 20 2f 2a 20 69 66 20 28 61 29 20 74 /* if (a) t
11190 68 65 20 6f 70 65 6e 20 66 6c 61 67 73 20 61 72 he open flags ar
11191 65 20 6e 6f 74 20 4f 5f 52 44 57 52 2c 20 28 62 e not O_RDWR, (b
11192 29 20 74 68 65 20 63 6f 6e 63 68 20 69 73 6e 27 ) the conch isn'
11193 74 20 74 68 65 72 65 2c 20 61 6e 64 0a 20 20 20 t there, and.
11194 20 20 20 2a 2a 20 28 63 29 20 74 68 65 20 66 69 ** (c) the fi
11195 6c 65 20 73 79 73 74 65 6d 20 69 73 20 72 65 61 le system is rea
11196 64 2d 6f 6e 6c 79 2c 20 74 68 65 6e 20 65 6e 61 d-only, then ena
11197 62 6c 65 20 6e 6f 2d 6c 6f 63 6b 69 6e 67 20 61 ble no-locking a
11198 63 63 65 73 73 2e 0a 20 20 20 20 20 20 2a 2a 20 ccess.. **
11199 55 67 68 2c 20 73 69 6e 63 65 20 4f 5f 52 44 4f Ugh, since O_RDO
1119a 4e 4c 59 3d 3d 30 78 30 30 30 30 20 77 65 20 74 NLY==0x0000 we t
1119b 65 73 74 20 66 6f 72 20 21 4f 5f 52 44 57 52 20 est for !O_RDWR
1119c 73 69 6e 63 65 20 75 6e 69 78 4f 70 65 6e 20 61 since unixOpen a
1119d 73 73 65 72 74 73 0a 20 20 20 20 20 20 2a 2a 20 sserts. **
1119e 74 68 61 74 20 6f 70 65 6e 46 6c 61 67 73 20 77 that openFlags w
1119f 69 6c 6c 20 68 61 76 65 20 6f 6e 6c 79 20 6f 6e ill have only on
111a0 65 20 6f 66 20 4f 5f 52 44 4f 4e 4c 59 20 6f 72 e of O_RDONLY or
111a1 20 4f 5f 52 44 57 52 2e 0a 20 20 20 20 20 20 2a O_RDWR.. *
111a2 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73 /. struct s
111a3 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 tatfs fsInfo;.
111a4 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20 struct stat
111a5 63 6f 6e 63 68 49 6e 66 6f 3b 0a 20 20 20 20 20 conchInfo;.
111a6 20 69 6e 74 20 67 6f 4c 6f 63 6b 6c 65 73 73 20 int goLockless
111a7 3d 20 30 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 = 0;.. if(
111a8 6f 73 53 74 61 74 28 70 43 74 78 2d 3e 63 6f 6e osStat(pCtx->con
111a9 63 68 46 69 6c 65 50 61 74 68 2c 20 26 63 6f 6e chFilePath, &con
111aa 63 68 49 6e 66 6f 29 20 3d 3d 20 2d 31 20 29 20 chInfo) == -1 )
111ab 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 65 72 {. int er
111ac 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 r = errno;.
111ad 20 20 20 69 66 28 20 28 65 72 72 3d 3d 45 4e 4f if( (err==ENO
111ae 45 4e 54 29 20 26 26 20 28 73 74 61 74 66 73 28 ENT) && (statfs(
111af 64 62 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 dbPath, &fsInfo)
111b0 20 21 3d 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 != -1) ){.
111b1 20 20 20 20 20 67 6f 4c 6f 63 6b 6c 65 73 73 20 goLockless
111b2 3d 20 28 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 = (fsInfo.f_flag
111b3 73 26 4d 4e 54 5f 52 44 4f 4e 4c 59 29 20 3d 3d s&MNT_RDONLY) ==
111b4 20 4d 4e 54 5f 52 44 4f 4e 4c 59 3b 0a 20 20 20 MNT_RDONLY;.
111b5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
111b6 20 20 20 20 20 69 66 28 20 67 6f 4c 6f 63 6b 6c if( goLockl
111b7 65 73 73 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ess ){. p
111b8 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d Ctx->conchHeld =
111b9 20 2d 31 3b 20 2f 2a 20 72 65 61 64 20 6f 6e 6c -1; /* read onl
111ba 79 20 46 53 2f 20 6c 6f 63 6b 6c 65 73 73 20 2a y FS/ lockless *
111bb 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 /. rc = S
111bc 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
111bd 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 20 0a 20 20 }. }. } .
111be 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
111bf 4b 20 26 26 20 6c 6f 63 6b 50 61 74 68 20 29 7b K && lockPath ){
111c0 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 . pCtx->lockP
111c1 72 6f 78 79 50 61 74 68 20 3d 20 73 71 6c 69 74 roxyPath = sqlit
111c2 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 6c 6f e3DbStrDup(0, lo
111c3 63 6b 50 61 74 68 29 3b 0a 20 20 7d 0a 0a 20 20 ckPath);. }..
111c4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
111c5 4b 20 29 7b 0a 20 20 20 20 70 43 74 78 2d 3e 64 K ){. pCtx->d
111c6 62 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 bPath = sqlite3D
111c7 62 53 74 72 44 75 70 28 30 2c 20 64 62 50 61 74 bStrDup(0, dbPat
111c8 68 29 3b 0a 20 20 20 20 69 66 28 20 70 43 74 78 h);. if( pCtx
111c9 2d 3e 64 62 50 61 74 68 3d 3d 4e 55 4c 4c 20 29 ->dbPath==NULL )
111ca 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
111cb 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
111cc 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 . }. if( rc==S
111cd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
111ce 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 69 73 /* all memory is
111cf 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 6f 78 allocated, prox
111d0 79 73 20 61 72 65 20 63 72 65 61 74 65 64 20 61 ys are created a
111d1 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a 20 20 nd assigned, .
111d2 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 65 20 ** switch the
111d3 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 locking context
111d4 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65 6e and pMethod then
111d5 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a 2f 0a return.. */.
111d6 20 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 pCtx->oldLoc
111d7 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46 kingContext = pF
111d8 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
111d9 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ext;. pFile->
111da 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d lockingContext =
111db 20 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 2d pCtx;. pCtx-
111dc 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 46 >pOldMethod = pF
111dd 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 20 ile->pMethod;.
111de 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 pFile->pMethod
111df 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f = &proxyIoMetho
111e0 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ds;. }else{.
111e1 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 if( pCtx->conch
111e2 46 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 70 File ){ . p
111e3 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 2d 3e Ctx->conchFile->
111e4 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 pMethod->xClose(
111e5 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
111e6 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 pCtx->conchFile)
111e7 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
111e8 66 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 free(pCtx->conch
111e9 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 File);. }.
111ea 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 30 sqlite3DbFree(0
111eb 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 , pCtx->lockProx
111ec 79 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 yPath);. sqli
111ed 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 te3_free(pCtx->c
111ee 6f 6e 63 68 46 69 6c 65 50 61 74 68 29 3b 20 0a onchFilePath); .
111ef 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
111f0 28 70 43 74 78 29 3b 0a 20 20 7d 0a 20 20 4f 53 (pCtx);. }. OS
111f1 54 52 41 43 45 28 28 22 54 52 41 4e 53 50 52 4f TRACE(("TRANSPRO
111f2 58 59 20 20 25 64 20 25 73 5c 6e 22 2c 20 70 46 XY %d %s\n", pF
111f3 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 ile->h,.
111f4 20 20 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f (rc==SQLITE_O
111f5 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c K ? "ok" : "fail
111f6 65 64 22 29 29 29 3b 0a 20 20 72 65 74 75 72 6e ed")));. return
111f7 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 rc;.}.../*.** T
111f8 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 6e 64 his routine hand
111f9 6c 65 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 les sqlite3_file
111fa 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61 6c 6c 73 _control() calls
111fb 20 74 68 61 74 20 61 72 65 20 73 70 65 63 69 66 that are specif
111fc 69 63 0a 2a 2a 20 74 6f 20 70 72 6f 78 79 20 6c ic.** to proxy l
111fd 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 ocking..*/.stati
111fe 63 20 69 6e 74 20 70 72 6f 78 79 46 69 6c 65 43 c int proxyFileC
111ff 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 ontrol(sqlite3_f
11200 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c ile *id, int op,
11201 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 void *pArg){.
11202 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 switch( op ){.
11203 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 47 45 case SQLITE_GE
11204 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a T_LOCKPROXYFILE:
11205 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c {. unixFil
11206 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
11207 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20 File*)id;.
11208 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 if( pFile->pMeth
11209 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f 4d 65 od == &proxyIoMe
1120a 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 thods ){.
1120b 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
1120c 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 text *pCtx = (pr
1120d 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
1120e 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e t*)pFile->lockin
1120f 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 gContext;.
11210 20 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 proxyTakeConch
11211 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 20 (pFile);.
11212 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 if( pCtx->lockP
11213 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20 20 roxyPath ){.
11214 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 63 68 *(const ch
11215 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 70 43 74 ar **)pArg = pCt
11216 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
11217 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
11218 0a 20 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e . *(con
11219 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 st char **)pArg
1121a 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f 74 20 68 = ":auto: (not h
1121b 65 6c 64 29 22 3b 0a 20 20 20 20 20 20 20 20 7d eld)";. }
1121c 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a . } else {.
1121d 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 *(const
1121e 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 4e char **)pArg = N
1121f 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ULL;. }.
11220 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
11221 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 _OK;. }. c
11222 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c ase SQLITE_SET_L
11223 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 7b 0a OCKPROXYFILE: {.
11224 20 20 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a unixFile *
11225 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
11226 65 2a 29 69 64 3b 0a 20 20 20 20 20 20 69 6e 74 e*)id;. int
11227 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
11228 0a 20 20 20 20 20 20 69 6e 74 20 69 73 50 72 6f . int isPro
11229 78 79 53 74 79 6c 65 20 3d 20 28 70 46 69 6c 65 xyStyle = (pFile
1122a 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 70 72 ->pMethod == &pr
1122b 6f 78 79 49 6f 4d 65 74 68 6f 64 73 29 3b 0a 20 oxyIoMethods);.
1122c 20 20 20 20 20 69 66 28 20 70 41 72 67 3d 3d 4e if( pArg==N
1122d 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74 20 63 68 ULL || (const ch
1122e 61 72 20 2a 29 70 41 72 67 3d 3d 30 20 29 7b 0a ar *)pArg==0 ){.
1122f 20 20 20 20 20 20 20 20 69 66 28 20 69 73 50 72 if( isPr
11230 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20 20 20 20 oxyStyle ){.
11231 20 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 6f 66 /* turn of
11232 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 f proxy locking
11233 2d 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 64 20 - not supported
11234 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 */. rc
11235 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 2f = SQLITE_ERROR /
11236 2a 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c *SQLITE_PROTOCOL
11237 3f 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3f ? SQLITE_MISUSE?
11238 2a 2f 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 */;. }els
11239 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e{. /*
1123a 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 79 20 6c turn off proxy l
1123b 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65 61 64 79 ocking - already
1123c 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a 2f 0a 20 off - NOOP */.
1123d 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
1123e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 LITE_OK;.
1123f 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
11240 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 const ch
11241 61 72 20 2a 70 72 6f 78 79 50 61 74 68 20 3d 20 ar *proxyPath =
11242 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 41 (const char *)pA
11243 72 67 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 rg;. if(
11244 69 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a isProxyStyle ){.
11245 20 20 20 20 20 20 20 20 20 20 70 72 6f 78 79 4c proxyL
11246 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 ockingContext *p
11247 43 74 78 20 3d 20 0a 20 20 20 20 20 20 20 20 20 Ctx = .
11248 20 20 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 (proxyLocking
11249 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e Context*)pFile->
1124a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a lockingContext;.
1124b 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 73 if( !s
1124c 74 72 63 6d 70 28 70 41 72 67 2c 20 22 3a 61 75 trcmp(pArg, ":au
1124d 74 6f 3a 22 29 20 0a 20 20 20 20 20 20 20 20 20 to:") .
1124e 20 20 7c 7c 20 28 70 43 74 78 2d 3e 6c 6f 63 6b || (pCtx->lock
1124f 50 72 6f 78 79 50 61 74 68 20 26 26 0a 20 20 20 ProxyPath &&.
11250 20 20 20 20 20 20 20 20 20 20 20 20 21 73 74 72 !str
11251 6e 63 6d 70 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 ncmp(pCtx->lockP
11252 72 6f 78 79 50 61 74 68 2c 20 70 72 6f 78 79 50 roxyPath, proxyP
11253 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 ath, MAXPATHLEN)
11254 29 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 ). ){.
11255 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
11256 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
11257 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
11258 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 77 69 rc = swi
11259 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 61 74 68 tchLockProxyPath
1125a 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 61 74 (pFile, proxyPat
1125b 68 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a h);. }.
1125c 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1125d 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e /* turn
1125e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c 65 20 6c on proxy file l
1125f 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 ocking */.
11260 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54 72 rc = proxyTr
11261 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 ansformUnixFile(
11262 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 61 74 68 pFile, proxyPath
11263 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
11264 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 }. retur
11265 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
11266 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
11267 20 61 73 73 65 72 74 28 20 30 20 29 3b 20 20 2f assert( 0 ); /
11268 2a 20 54 68 65 20 63 61 6c 6c 20 61 73 73 75 72 * The call assur
11269 65 73 20 74 68 61 74 20 6f 6e 6c 79 20 76 61 6c es that only val
1126a 69 64 20 6f 70 63 6f 64 65 73 20 61 72 65 20 73 id opcodes are s
1126b 65 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d ent */. }. }
1126c 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44 2a . /*NOTREACHED*
1126d 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 /. return SQLIT
1126e 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a E_ERROR;.}../*.*
1126f 2a 20 57 69 74 68 69 6e 20 74 68 69 73 20 64 69 * Within this di
11270 76 69 73 69 6f 6e 20 28 74 68 65 20 70 72 6f 78 vision (the prox
11271 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 ying locking imp
11272 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20 74 68 65 lementation) the
11273 20 70 72 6f 63 65 64 75 72 65 73 0a 2a 2a 20 61 procedures.** a
11274 62 6f 76 65 20 74 68 69 73 20 70 6f 69 6e 74 20 bove this point
11275 61 72 65 20 61 6c 6c 20 75 74 69 6c 69 74 69 65 are all utilitie
11276 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d 72 65 6c s. The lock-rel
11277 61 74 65 64 20 6d 65 74 68 6f 64 73 20 6f 66 20 ated methods of
11278 74 68 65 0a 2a 2a 20 70 72 6f 78 79 2d 6c 6f 63 the.** proxy-loc
11279 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6f 5f king sqlite3_io_
1127a 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74 20 66 6f method object fo
1127b 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a llow..*/.../*.**
1127c 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
1127d 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 ecks if there is
1127e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
1127f 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 held on the spe
11280 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 cified.** file b
11281 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 y this or any ot
11282 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 her process. If
11283 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 such a lock is h
11284 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 eld, set *pResOu
11285 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 t.** to a non-ze
11286 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 ro value otherwi
11287 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 se *pResOut is s
11288 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 et to zero. The
11289 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a return value.**
1128a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 is set to SQLIT
1128b 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 E_OK unless an I
1128c 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 /O error occurs
1128d 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 during lock chec
1128e 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 king..*/.static
1128f 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b 52 65 int proxyCheckRe
11290 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 servedLock(sqlit
11291 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
11292 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 20 75 *pResOut) {. u
11293 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
11294 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
11295 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 int rc = proxy
11296 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 TakeConch(pFile)
11297 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
11298 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f TE_OK ){. pro
11299 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
1129a 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c *pCtx = (proxyL
1129b 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 ockingContext *)
1129c 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
1129d 6e 74 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 ntext;. if( p
1129e 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 3e 30 Ctx->conchHeld>0
1129f 20 29 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 ){. unixFi
112a0 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 78 le *proxy = pCtx
112a1 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 ->lockProxy;.
112a2 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78 79 2d return proxy-
112a3 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65 63 6b >pMethod->xCheck
112a4 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 28 73 71 ReservedLock((sq
112a5 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 lite3_file*)prox
112a6 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a 20 20 20 y, pResOut);.
112a7 20 7d 65 6c 73 65 7b 20 2f 2a 20 63 6f 6e 63 68 }else{ /* conch
112a8 48 65 6c 64 20 3c 20 30 20 69 73 20 6c 6f 63 6b Held < 0 is lock
112a9 6c 65 73 73 20 2a 2f 0a 20 20 20 20 20 20 70 52 less */. pR
112aa 65 73 4f 75 74 3d 30 3b 0a 20 20 20 20 7d 0a 20 esOut=0;. }.
112ab 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
112ac 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
112ad 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
112ae 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
112af 79 20 70 61 72 61 6d 65 74 65 72 20 65 46 69 6c y parameter eFil
112b0 65 4c 6f 63 6b 20 2d 20 6f 6e 65 0a 2a 2a 20 6f eLock - one.** o
112b1 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
112b2 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
112b3 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
112b4 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
112b5 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
112b6 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
112b7 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
112b8 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
112b9 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
112ba 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
112bb 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
112bc 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
112bd 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
112be 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
112bf 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
112c0 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
112c1 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
112c2 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
112c3 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
112c4 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
112c5 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
112c6 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
112c7 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
112c8 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
112c9 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
112ca 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
112cb 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
112cc 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
112cd 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
112ce 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
112cf 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
112d0 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
112d1 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
112d2 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
112d3 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
112d4 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
112d5 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
112d6 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
112d7 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 USIVE.**.** This
112d8 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e routine will on
112d9 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f ly increase a lo
112da 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c ck. Use the sql
112db 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a ite3OsUnlock().*
112dc 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 * routine to low
112dd 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 er a locking lev
112de 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e el..*/.static in
112df 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 t proxyLock(sqli
112e0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
112e1 74 20 65 46 69 6c 65 4c 6f 63 6b 29 20 7b 0a 20 t eFileLock) {.
112e2 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
112e3 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
112e4 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f ;. int rc = pro
112e5 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c xyTakeConch(pFil
112e6 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 e);. if( rc==SQ
112e7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 LITE_OK ){. p
112e8 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
112e9 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 xt *pCtx = (prox
112ea 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
112eb 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
112ec 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 69 66 28 Context;. if(
112ed 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 pCtx->conchHeld
112ee 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 6e 69 78 >0 ){. unix
112ef 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 File *proxy = pC
112f0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 tx->lockProxy;.
112f1 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d rc = proxy-
112f2 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 >pMethod->xLock(
112f3 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 (sqlite3_file*)p
112f4 72 6f 78 79 2c 20 65 46 69 6c 65 4c 6f 63 6b 29 roxy, eFileLock)
112f5 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 65 ;. pFile->e
112f6 46 69 6c 65 4c 6f 63 6b 20 3d 20 70 72 6f 78 79 FileLock = proxy
112f7 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 20 ->eFileLock;.
112f8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a }else{. /*
112f9 20 63 6f 6e 63 68 48 65 6c 64 20 3c 20 30 20 69 conchHeld < 0 i
112fa 73 20 6c 6f 63 6b 6c 65 73 73 20 2a 2f 0a 20 20 s lockless */.
112fb 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
112fc 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c rc;.}.../*.** L
112fd 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
112fe 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 level on file d
112ff 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 escriptor pFile
11300 74 6f 20 65 46 69 6c 65 4c 6f 63 6b 2e 20 20 65 to eFileLock. e
11301 46 69 6c 65 4c 6f 63 6b 0a 2a 2a 20 6d 75 73 74 FileLock.** must
11302 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f be either NO_LO
11303 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 CK or SHARED_LOC
11304 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 K..**.** If the
11305 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 locking level of
11306 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
11307 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 ptor is already
11308 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 at or below.** t
11309 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
1130a 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 king level, this
1130b 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
1130c 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 -op..*/.static i
1130d 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 nt proxyUnlock(s
1130e 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
1130f 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 20 int eFileLock)
11310 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 {. unixFile *pF
11311 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
11312 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 )id;. int rc =
11313 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 proxyTakeConch(p
11314 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d File);. if( rc=
11315 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
11316 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f proxyLockingCo
11317 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 ntext *pCtx = (p
11318 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
11319 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b xt *)pFile->lock
1131a 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 ingContext;.
1131b 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 if( pCtx->conchH
1131c 65 6c 64 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 eld>0 ){. u
1131d 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d nixFile *proxy =
1131e 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
1131f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f ;. rc = pro
11320 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e xy->pMethod->xUn
11321 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 lock((sqlite3_fi
11322 6c 65 2a 29 70 72 6f 78 79 2c 20 65 46 69 6c 65 le*)proxy, eFile
11323 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 70 46 69 Lock);. pFi
11324 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 le->eFileLock =
11325 70 72 6f 78 79 2d 3e 65 46 69 6c 65 4c 6f 63 6b proxy->eFileLock
11326 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
11327 20 20 20 2f 2a 20 63 6f 6e 63 68 48 65 6c 64 20 /* conchHeld
11328 3c 20 30 20 69 73 20 6c 6f 63 6b 6c 65 73 73 20 < 0 is lockless
11329 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 */. }. }. r
1132a 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1132b 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 ** Close a file
1132c 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 79 20 that uses proxy
1132d 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 locks..*/.static
1132e 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 28 int proxyClose(
1132f 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
11330 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a ) {. if( id ){.
11331 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 unixFile *pF
11332 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
11333 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 4c 6f )id;. proxyLo
11334 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
11335 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 tx = (proxyLocki
11336 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c ngContext *)pFil
11337 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
11338 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 t;. unixFile
11339 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 74 *lockProxy = pCt
1133a 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 x->lockProxy;.
1133b 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 unixFile *conc
1133c 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f hFile = pCtx->co
1133d 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74 nchFile;. int
1133e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1133f 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 6c 6f . . if( lo
11340 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 ckProxy ){.
11341 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d rc = lockProxy-
11342 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 >pMethod->xUnloc
11343 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a k((sqlite3_file*
11344 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f 4c )lockProxy, NO_L
11345 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 OCK);. if(
11346 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
11347 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50 rc = lockP
11348 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 roxy->pMethod->x
11349 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 Close((sqlite3_f
1134a 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b ile*)lockProxy);
1134b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 . if( rc )
1134c 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1134d 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f sqlite3_free(lo
1134e 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 ckProxy);.
1134f 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 pCtx->lockProxy
11350 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 = 0;. }. i
11351 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b 0a f( conchFile ){.
11352 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e if( pCtx->
11353 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 20 20 conchHeld ){.
11354 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 52 rc = proxyR
11355 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69 6c eleaseConch(pFil
11356 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 e);. if(
11357 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
11358 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
11359 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d = conchFile->pM
1135a 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 ethod->xClose((s
1135b 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e qlite3_file*)con
1135c 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 chFile);. i
1135d 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1135e 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 c;. sqlite3
1135f 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c 65 29 _free(conchFile)
11360 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
11361 74 65 33 44 62 46 72 65 65 28 30 2c 20 70 43 74 te3DbFree(0, pCt
11362 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
11363 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 );. sqlite3_f
11364 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 ree(pCtx->conchF
11365 69 6c 65 50 61 74 68 29 3b 0a 20 20 20 20 73 71 ilePath);. sq
11366 6c 69 74 65 33 44 62 46 72 65 65 28 30 2c 20 70 lite3DbFree(0, p
11367 43 74 78 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 Ctx->dbPath);.
11368 20 20 2f 2a 20 72 65 73 74 6f 72 65 20 74 68 65 /* restore the
11369 20 6f 72 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e original lockin
1136a 67 20 63 6f 6e 74 65 78 74 20 61 6e 64 20 70 4d g context and pM
1136b 65 74 68 6f 64 20 74 68 65 6e 20 63 6c 6f 73 65 ethod then close
1136c 20 69 74 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 it */. pFile
1136d 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
1136e 20 3d 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b = pCtx->oldLock
1136f 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 ingContext;.
11370 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d pFile->pMethod =
11371 20 70 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f pCtx->pOldMetho
11372 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 d;. sqlite3_f
11373 72 65 65 28 70 43 74 78 29 3b 0a 20 20 20 20 72 ree(pCtx);. r
11374 65 74 75 72 6e 20 70 46 69 6c 65 2d 3e 70 4d 65 eturn pFile->pMe
11375 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 69 64 29 thod->xClose(id)
11376 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
11377 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 23 QLITE_OK;.}....#
11378 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 endif /* defined
11379 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 (__APPLE__) && S
1137a 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
1137b 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a KING_STYLE */./*
1137c 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f .** The proxy lo
1137d 63 6b 69 6e 67 20 73 74 79 6c 65 20 69 73 20 69 cking style is i
1137e 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
1137f 77 69 74 68 20 41 46 50 20 66 69 6c 65 73 79 73 with AFP filesys
11380 74 65 6d 73 2e 0a 2a 2a 20 41 6e 64 20 73 69 6e tems..** And sin
11381 63 65 20 41 46 50 20 69 73 20 6f 6e 6c 79 20 73 ce AFP is only s
11382 75 70 70 6f 72 74 65 64 20 6f 6e 20 4d 61 63 4f upported on MacO
11383 53 58 2c 20 74 68 65 20 70 72 6f 78 79 20 6c 6f SX, the proxy lo
11384 63 6b 69 6e 67 20 69 73 20 61 6c 73 6f 0a 2a 2a cking is also.**
11385 20 72 65 73 74 72 69 63 74 65 64 20 74 6f 20 4d restricted to M
11386 61 63 4f 53 58 2e 0a 2a 2a 20 0a 2a 2a 0a 2a 2a acOSX..** .**.**
11387 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11388 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 70 72 6f * End of the pro
11389 78 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e xy lock implemen
1138a 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a tation *********
1138b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
1138c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1138d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1138e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1138f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
11391 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
11392 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
11393 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a stem interface..
11394 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
11395 6e 65 20 72 65 67 69 73 74 65 72 73 20 61 6c 6c ne registers all
11396 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 VFS implementat
11397 69 6f 6e 73 20 66 6f 72 20 75 6e 69 78 2d 6c 69 ions for unix-li
11398 6b 65 20 6f 70 65 72 61 74 69 6e 67 0a 2a 2a 20 ke operating.**
11399 73 79 73 74 65 6d 73 2e 20 20 54 68 69 73 20 72 systems. This r
1139a 6f 75 74 69 6e 65 2c 20 61 6e 64 20 74 68 65 20 outine, and the
1139b 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 sqlite3_os_end()
1139c 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 66 6f routine that fo
1139d 6c 6c 6f 77 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 llows,.** should
1139e 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 be the only rou
1139f 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 tines in this fi
113a0 6c 65 20 74 68 61 74 20 61 72 65 20 76 69 73 69 le that are visi
113a1 62 6c 65 20 66 72 6f 6d 20 6f 74 68 65 72 0a 2a ble from other.*
113a2 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 * files..**.** T
113a3 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
113a4 61 6c 6c 65 64 20 6f 6e 63 65 20 64 75 72 69 6e alled once durin
113a5 67 20 53 51 4c 69 74 65 20 69 6e 69 74 69 61 6c g SQLite initial
113a6 69 7a 61 74 69 6f 6e 20 61 6e 64 20 62 79 20 61 ization and by a
113a7 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 68 72 65 61 .** single threa
113a8 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 d. The memory a
113a9 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 6d 75 llocation and mu
113aa 74 65 78 20 73 75 62 73 79 73 74 65 6d 73 20 68 tex subsystems h
113ab 61 76 65 20 6e 6f 74 0a 2a 2a 20 6e 65 63 65 73 ave not.** neces
113ac 73 61 72 69 6c 79 20 62 65 65 6e 20 69 6e 69 74 sarily been init
113ad 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 74 68 69 ialized when thi
113ae 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
113af 6c 65 64 2c 20 61 6e 64 20 73 6f 20 74 68 65 79 led, and so they
113b0 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 .** should not b
113b1 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 e used..*/.SQLIT
113b2 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
113b3 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 3_os_init(void){
113b4 20 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 54 68 65 . /* . ** The
113b5 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f following macro
113b6 20 64 65 66 69 6e 65 73 20 61 6e 20 69 6e 69 74 defines an init
113b7 69 61 6c 69 7a 65 72 20 66 6f 72 20 61 6e 20 73 ializer for an s
113b8 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 qlite3_vfs objec
113b9 74 2e 0a 20 20 2a 2a 20 54 68 65 20 6e 61 6d 65 t.. ** The name
113ba 20 6f 66 20 74 68 65 20 56 46 53 20 69 73 20 4e of the VFS is N
113bb 41 4d 45 2e 20 20 54 68 65 20 70 41 70 70 44 61 AME. The pAppDa
113bc 74 61 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 ta is a pointer
113bd 74 6f 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 2a to a pointer. *
113be 2a 20 74 6f 20 74 68 65 20 22 66 69 6e 64 65 72 * to the "finder
113bf 22 20 66 75 6e 63 74 69 6f 6e 2e 20 20 28 70 41 " function. (pA
113c0 70 70 44 61 74 61 20 69 73 20 61 20 70 6f 69 6e ppData is a poin
113c1 74 65 72 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 ter to a pointer
113c2 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 73 69 because. ** si
113c3 6c 6c 79 20 43 39 30 20 72 75 6c 65 73 20 70 72 lly C90 rules pr
113c4 6f 68 69 62 69 74 20 61 20 76 6f 69 64 2a 20 66 ohibit a void* f
113c5 72 6f 6d 20 62 65 69 6e 67 20 63 61 73 74 20 74 rom being cast t
113c6 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 o a function poi
113c7 6e 74 65 72 0a 20 20 2a 2a 20 61 6e 64 20 73 6f nter. ** and so
113c8 20 77 65 20 68 61 76 65 20 74 6f 20 67 6f 20 74 we have to go t
113c9 68 72 6f 75 67 68 20 74 68 65 20 69 6e 74 65 72 hrough the inter
113ca 6d 65 64 69 61 74 65 20 70 6f 69 6e 74 65 72 20 mediate pointer
113cb 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d to avoid problem
113cc 73 0a 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 s. ** when comp
113cd 69 6c 69 6e 67 20 77 69 74 68 20 2d 70 65 64 61 iling with -peda
113ce 6e 74 69 63 2d 65 72 72 6f 72 73 20 6f 6e 20 47 ntic-errors on G
113cf 43 43 2e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 CC.). **. ** T
113d0 68 65 20 46 49 4e 44 45 52 20 70 61 72 61 6d 65 he FINDER parame
113d1 74 65 72 20 74 6f 20 74 68 69 73 20 6d 61 63 72 ter to this macr
113d2 6f 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 o is the name of
113d3 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 the pointer to
113d4 74 68 65 0a 20 20 2a 2a 20 66 69 6e 64 65 72 2d the. ** finder-
113d5 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 function. The f
113d6 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 inder-function r
113d7 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
113d8 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c to the. ** sql
113d9 69 74 65 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f ite_io_methods o
113da 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 bject that imple
113db 6d 65 6e 74 73 20 74 68 65 20 64 65 73 69 72 65 ments the desire
113dc 64 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 62 d locking. ** b
113dd 65 68 61 76 69 6f 72 73 2e 20 20 53 65 65 20 74 ehaviors. See t
113de 68 65 20 64 69 76 69 73 69 6f 6e 20 61 62 6f 76 he division abov
113df 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 e that contains
113e0 74 68 65 20 49 4f 4d 45 54 48 4f 44 53 0a 20 20 the IOMETHODS.
113e1 2a 2a 20 6d 61 63 72 6f 20 66 6f 72 20 61 64 64 ** macro for add
113e2 69 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f ition informatio
113e3 6e 20 6f 6e 20 66 69 6e 64 65 72 2d 66 75 6e 63 n on finder-func
113e4 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a tions.. **. **
113e5 20 4d 6f 73 74 20 66 69 6e 64 65 72 73 20 73 69 Most finders si
113e6 6d 70 6c 79 20 72 65 74 75 72 6e 20 61 20 70 6f mply return a po
113e7 69 6e 74 65 72 20 74 6f 20 61 20 66 69 78 65 64 inter to a fixed
113e8 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
113e9 6f 64 73 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 2e ods. ** object.
113ea 20 20 42 75 74 20 74 68 65 20 22 61 75 74 6f 6c But the "autol
113eb 6f 63 6b 49 6f 46 69 6e 64 65 72 22 20 61 76 61 ockIoFinder" ava
113ec 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 ilable on MacOSX
113ed 20 64 6f 65 73 20 61 20 6c 69 74 74 6c 65 0a 20 does a little.
113ee 20 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 74 68 ** more than th
113ef 61 74 3b 20 69 74 20 6c 6f 6f 6b 73 20 61 74 20 at; it looks at
113f0 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 74 the filesystem t
113f1 79 70 65 20 74 68 61 74 20 68 6f 73 74 73 20 74 ype that hosts t
113f2 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 he . ** databas
113f3 65 20 66 69 6c 65 20 61 6e 64 20 74 72 69 65 73 e file and tries
113f4 20 74 6f 20 63 68 6f 6f 73 65 20 61 6e 20 6c 6f to choose an lo
113f5 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 61 70 70 cking method app
113f6 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 20 20 2a ropriate for. *
113f7 2a 20 74 68 61 74 20 66 69 6c 65 73 79 73 74 65 * that filesyste
113f8 6d 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 23 m time.. */. #
113f9 64 65 66 69 6e 65 20 55 4e 49 58 56 46 53 28 56 define UNIXVFS(V
113fa 46 53 4e 41 4d 45 2c 20 46 49 4e 44 45 52 29 20 FSNAME, FINDER)
113fb 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
113fc 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 33 \. 3
113fd 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
113fe 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e /* iVersion
113ff 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
11400 20 20 20 20 20 20 20 5c 0a 20 20 20 20 73 69 7a \. siz
11401 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 2c 20 20 eof(unixFile),
11402 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a /* szOsFile *
11403 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
11404 20 20 20 20 20 5c 0a 20 20 20 20 4d 41 58 5f 50 \. MAX_P
11405 41 54 48 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 ATHNAME,
11406 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a /* mxPathname *
11407 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
11408 20 20 20 5c 0a 20 20 20 20 30 2c 20 20 20 20 20 \. 0,
11409 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1140a 2a 20 70 4e 65 78 74 20 2a 2f 20 20 20 20 20 20 * pNext */
1140b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1140c 20 5c 0a 20 20 20 20 56 46 53 4e 41 4d 45 2c 20 \. VFSNAME,
1140d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1140e 7a 4e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 zName */
1140f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
11410 0a 20 20 20 20 28 76 6f 69 64 2a 29 26 46 49 4e . (void*)&FIN
11411 44 45 52 2c 20 20 20 20 20 20 20 2f 2a 20 70 41 DER, /* pA
11412 70 70 44 61 74 61 20 2a 2f 20 20 20 20 20 20 20 ppData */
11413 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
11414 20 20 20 75 6e 69 78 4f 70 65 6e 2c 20 20 20 20 unixOpen,
11415 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 /* xOpe
11416 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 n */
11417 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
11418 20 75 6e 69 78 44 65 6c 65 74 65 2c 20 20 20 20 unixDelete,
11419 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 /* xDelet
1141a 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 e */
1141b 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 \. u
1141c 6e 69 78 41 63 63 65 73 73 2c 20 20 20 20 20 20 nixAccess,
1141d 20 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 /* xAccess
1141e 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
1141f 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 \. uni
11420 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 xFullPathname,
11421 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e /* xFullPathn
11422 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ame */
11423 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 \. unixD
11424 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 lOpen,
11425 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 20 20 /* xDlOpen */
11426 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11427 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 45 \. unixDlE
11428 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f rror, /
11429 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 20 20 20 * xDlError */
1142a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1142b 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 53 79 6d \. unixDlSym
1142c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
1142d 78 44 6c 53 79 6d 20 2a 2f 20 20 20 20 20 20 20 xDlSym */
1142e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
1142f 0a 20 20 20 20 75 6e 69 78 44 6c 43 6c 6f 73 65 . unixDlClose
11430 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 , /* xD
11431 6c 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 lClose */
11432 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
11433 20 20 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 unixRandomnes
11434 73 2c 20 20 20 20 20 20 20 2f 2a 20 78 52 61 6e s, /* xRan
11435 64 6f 6d 6e 65 73 73 20 2a 2f 20 20 20 20 20 20 domness */
11436 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
11437 20 75 6e 69 78 53 6c 65 65 70 2c 20 20 20 20 20 unixSleep,
11438 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 /* xSleep
11439 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
1143a 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 \. u
1143b 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 nixCurrentTime,
1143c 20 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 /* xCurrent
1143d 54 69 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 Time */
1143e 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 \. uni
1143f 78 47 65 74 4c 61 73 74 45 72 72 6f 72 2c 20 20 xGetLastError,
11440 20 20 20 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 /* xGetLastEr
11441 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ror */
11442 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 43 \. unixC
11443 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 2c urrentTimeInt64,
11444 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 /* xCurrentTime
11445 49 6e 74 36 34 20 2a 2f 20 20 20 20 20 20 20 20 Int64 */
11446 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 65 74 \. unixSet
11447 53 79 73 74 65 6d 43 61 6c 6c 2c 20 20 20 20 2f SystemCall, /
11448 2a 20 78 53 65 74 53 79 73 74 65 6d 43 61 6c 6c * xSetSystemCall
11449 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
1144a 20 5c 0a 20 20 20 20 75 6e 69 78 47 65 74 53 79 \. unixGetSy
1144b 73 74 65 6d 43 61 6c 6c 2c 20 20 20 20 2f 2a 20 stemCall, /*
1144c 78 47 65 74 53 79 73 74 65 6d 43 61 6c 6c 20 2a xGetSystemCall *
1144d 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c / \
1144e 0a 20 20 20 20 75 6e 69 78 4e 65 78 74 53 79 73 . unixNextSys
1144f 74 65 6d 43 61 6c 6c 2c 20 20 20 2f 2a 20 78 4e temCall, /* xN
11450 65 78 74 53 79 73 74 65 6d 43 61 6c 6c 20 2a 2f extSystemCall */
11451 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
11452 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c }.. /*. ** Al
11453 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 73 20 l default VFSes
11454 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 6f 6e for unix are con
11455 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 6f tained in the fo
11456 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e 0a 20 llowing array..
11457 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 **. ** Note th
11458 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 at the sqlite3_v
11459 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 20 6f fs.pNext field o
1145a 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63 74 f the VFS object
1145b 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 20 2a is modified. *
1145c 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 * by the SQLite
1145d 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 56 46 core when the VF
1145e 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e S is registered.
1145f 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 So the followi
11460 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 63 61 ng. ** array ca
11461 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0a 20 nnot be const..
11462 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 71 6c */. static sql
11463 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d 20 ite3_vfs aVfs[]
11464 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 = {.#if SQLITE_E
11465 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
11466 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57 4f 52 YLE && (OS_VXWOR
11467 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f KS || defined(__
11468 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 20 55 4e APPLE__)). UN
11469 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20 20 IXVFS("unix",
1146a 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 6b 49 autolockI
1146b 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c 73 65 oFinder ),.#else
1146c 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e . UNIXVFS("un
1146d 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 70 6f ix", po
1146e 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 sixIoFinder ),.#
1146f 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 56 46 endif. UNIXVF
11470 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 20 S("unix-none",
11471 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 nolockIoFinde
11472 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 r ),. UNIXVFS
11473 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22 2c ("unix-dotfile",
11474 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 dotlockIoFinde
11475 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 r ),. UNIXVFS
11476 28 22 75 6e 69 78 2d 65 78 63 6c 22 2c 20 20 20 ("unix-excl",
11477 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 posixIoFinder
11478 29 2c 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b ),.#if OS_VXWORK
11479 53 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 S. UNIXVFS("u
1147a 6e 69 78 2d 6e 61 6d 65 64 73 65 6d 22 2c 20 73 nix-namedsem", s
1147b 65 6d 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 emIoFinder ),.#e
1147c 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f ndif.#if SQLITE_
1147d 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
1147e 54 59 4c 45 0a 20 20 20 20 55 4e 49 58 56 46 53 TYLE. UNIXVFS
1147f 28 22 75 6e 69 78 2d 70 6f 73 69 78 22 2c 20 20 ("unix-posix",
11480 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 posixIoFinder
11481 29 2c 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 ),.#if !OS_VXWOR
11482 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 KS. UNIXVFS("
11483 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c 20 20 20 20 unix-flock",
11484 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c flockIoFinder ),
11485 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 .#endif.#endif.#
11486 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 if SQLITE_ENABLE
11487 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 _LOCKING_STYLE &
11488 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c & defined(__APPL
11489 45 5f 5f 29 0a 20 20 20 20 55 4e 49 58 56 46 53 E__). UNIXVFS
1148a 28 22 75 6e 69 78 2d 61 66 70 22 2c 20 20 20 20 ("unix-afp",
1148b 20 20 61 66 70 49 6f 46 69 6e 64 65 72 20 29 2c afpIoFinder ),
1148c 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e . UNIXVFS("un
1148d 69 78 2d 6e 66 73 22 2c 20 20 20 20 20 20 6e 66 ix-nfs", nf
1148e 73 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 20 sIoFinder ),.
1148f 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70 UNIXVFS("unix-p
11490 72 6f 78 79 22 2c 20 20 20 20 70 72 6f 78 79 49 roxy", proxyI
11491 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 oFinder ),.#endi
11492 66 0a 20 20 7d 3b 0a 20 20 75 6e 73 69 67 6e 65 f. };. unsigne
11493 64 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 d int i;
11494 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
11495 72 20 2a 2f 0a 0a 20 20 2f 2a 20 44 6f 75 62 6c r */.. /* Doubl
11496 65 2d 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 e-check that the
11497 20 61 53 79 73 63 61 6c 6c 5b 5d 20 61 72 72 61 aSyscall[] arra
11498 79 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 73 74 y has been const
11499 72 75 63 74 65 64 0a 20 20 2a 2a 20 63 6f 72 72 ructed. ** corr
1149a 65 63 74 6c 79 2e 20 20 53 65 65 20 74 69 63 6b ectly. See tick
1149b 65 74 20 5b 62 62 33 61 38 36 65 38 39 30 63 38 et [bb3a86e890c8
1149c 65 39 36 61 62 5d 20 2a 2f 0a 20 20 61 73 73 65 e96ab] */. asse
1149d 72 74 28 20 41 72 72 61 79 53 69 7a 65 28 61 53 rt( ArraySize(aS
1149e 79 73 63 61 6c 6c 29 3d 3d 32 32 20 29 3b 0a 0a yscall)==22 );..
1149f 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c /* Register al
114a0 6c 20 56 46 53 65 73 20 64 65 66 69 6e 65 64 20 l VFSes defined
114a1 69 6e 20 74 68 65 20 61 56 66 73 5b 5d 20 61 72 in the aVfs[] ar
114a2 72 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 ray */. for(i=0
114a3 3b 20 69 3c 28 73 69 7a 65 6f 66 28 61 56 66 73 ; i<(sizeof(aVfs
114a4 29 2f 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 )/sizeof(sqlite3
114a5 5f 76 66 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 _vfs)); i++){.
114a6 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 sqlite3_vfs_re
114a7 67 69 73 74 65 72 28 26 61 56 66 73 5b 69 5d 2c gister(&aVfs[i],
114a8 20 69 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65 i==0);. }. re
114a9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
114aa 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f .}../*.** Shutdo
114ab 77 6e 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 wn the operating
114ac 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 system interfac
114ad 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70 e..**.** Some op
114ae 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 erating systems
114af 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20 64 6f might need to do
114b0 20 73 6f 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e some cleanup in
114b1 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a this routine,.*
114b2 2a 20 74 6f 20 72 65 6c 65 61 73 65 20 64 79 6e * to release dyn
114b3 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 amically allocat
114b4 65 64 20 6f 62 6a 65 63 74 73 2e 20 20 42 75 74 ed objects. But
114b5 20 6e 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a not on unix..**
114b6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
114b7 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69 a no-op for uni
114b8 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 x..*/.SQLITE_API
114b9 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f int sqlite3_os_
114ba 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 end(void){ . re
114bb 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
114bc 0a 7d 0a 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 .}. .#endif /* S
114bd 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f QLITE_OS_UNIX */
114be 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
114bf 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 * End of os_unix
114c0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
114c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
114c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
114c3 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
114c4 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f * Begin file os_
114c5 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a win.c **********
114c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
114c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
114c8 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 /./*.** 2004 May
114c9 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 22.**.** The au
114ca 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
114cb 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
114cc 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
114cd 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
114ce 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
114cf 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
114d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
114d1 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
114d2 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
114d3 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
114d4 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
114d5 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
114d6 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
114d7 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
114d8 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
114d9 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
114da 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
114db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
114dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
114dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
114de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
114df 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
114e0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
114e1 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 code that is sp
114e2 65 63 69 66 69 63 20 74 6f 20 57 69 6e 64 6f 77 ecific to Window
114e3 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 s..*/.#if SQLITE
114e4 5f 4f 53 5f 57 49 4e 20 20 20 20 20 20 20 20 20 _OS_WIN
114e5 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 /* This fi
114e6 6c 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 57 le is used for W
114e7 69 6e 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a indows only */..
114e8 23 69 66 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f #ifdef __CYGWIN_
114e9 5f 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 _.# include <sys
114ea 2f 63 79 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 /cygwin.h>.#endi
114eb 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 f../*.** Include
114ec 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f code that is co
114ed 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a mmon to all os_*
114ee 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a .c files.*/./***
114ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
114f0 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 ude os_common.h
114f1 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
114f2 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a os_win.c ******
114f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
114f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
114f5 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e n file os_common
114f6 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
114f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
114f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
114f9 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a * 2004 May 22.**
114fa 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
114fb 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
114fc 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
114fd 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
114fe 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
114ff 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
11500 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
11501 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
11502 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
11503 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
11504 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
11505 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
11506 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
11507 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
11508 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
11509 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
1150a 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
1150b 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1150c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1150d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1150e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1150f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11510 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
11511 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f e contains macro
11512 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 s and a little b
11513 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 it of code that
11514 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 is common to.**
11515 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 all of the platf
11516 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c orm-specific fil
11517 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 es (os_*.c) and
11518 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 is #included int
11519 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 o those.** files
1151a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ..**.** This fil
1151b 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 e should be #inc
1151c 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f luded by the os_
1151d 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 *.c files only.
1151e 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 It is not a.**
1151f 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 general purpose
11520 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a header file..*/.
11521 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d #ifndef _OS_COMM
11522 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f ON_H_.#define _O
11523 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a S_COMMON_H_../*.
11524 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 ** At least two
11525 62 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 bugs have slippe
11526 64 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 d in because we
11527 63 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f changed the MEMO
11528 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 RY_DEBUG.** macr
11529 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 o to SQLITE_DEBU
1152a 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 G and some older
1152b 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 makefiles have
1152c 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 not yet made the
1152d 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 .** switch. The
1152e 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 following code
1152f 73 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 should catch thi
11530 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d s problem at com
11531 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 pile-time..*/.#i
11532 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 fdef MEMORY_DEBU
11533 47 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d G.# error "The M
11534 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 EMORY_DEBUG macr
11535 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 o is obsolete.
11536 55 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 Use SQLITE_DEBUG
11537 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 instead.".#endi
11538 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 f..#if defined(S
11539 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 QLITE_TEST) && d
1153a 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
1153b 42 55 47 29 0a 23 20 69 66 6e 64 65 66 20 53 51 BUG).# ifndef SQ
1153c 4c 49 54 45 5f 44 45 42 55 47 5f 4f 53 5f 54 52 LITE_DEBUG_OS_TR
1153d 41 43 45 0a 23 20 20 20 64 65 66 69 6e 65 20 53 ACE.# define S
1153e 51 4c 49 54 45 5f 44 45 42 55 47 5f 4f 53 5f 54 QLITE_DEBUG_OS_T
1153f 52 41 43 45 20 30 0a 23 20 65 6e 64 69 66 0a 20 RACE 0.# endif.
11540 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 int sqlite3OSTr
11541 61 63 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 42 ace = SQLITE_DEB
11542 55 47 5f 4f 53 5f 54 52 41 43 45 3b 0a 23 20 64 UG_OS_TRACE;.# d
11543 65 66 69 6e 65 20 4f 53 54 52 41 43 45 28 58 29 efine OSTRACE(X)
11544 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 if( sq
11545 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 lite3OSTrace ) s
11546 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
11547 66 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 f X.#else.# defi
11548 6e 65 20 4f 53 54 52 41 43 45 28 58 29 0a 23 65 ne OSTRACE(X).#e
11549 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 ndif../*.** Macr
1154a 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e os for performan
1154b 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 ce tracing. Nor
1154c 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 mally turned off
1154d 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a . Only works.**
1154e 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 on i486 hardwar
1154f 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c e..*/.#ifdef SQL
11550 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f ITE_PERFORMANCE_
11551 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 TRACE../* .** hw
11552 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 time.h contains
11553 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 inline assembler
11554 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d code for implem
11555 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d enting .** high-
11556 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 performance timi
11557 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a ng routines..*/.
11558 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
11559 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 Include hwtime.h
1155a 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
1155b 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a f os_common.h **
1155c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1155d 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1155e 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d Begin file hwtim
1155f 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
11560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11561 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
11562 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 /*.** 2008 May 2
11563 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 7.**.** The auth
11564 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
11565 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
11566 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
11567 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
11568 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
11569 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
1156a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
1156b 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
1156c 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
1156d 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
1156e 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
1156f 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
11570 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
11571 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
11572 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
11573 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
11574 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
11575 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11576 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11577 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11578 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11579 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
1157a 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 file contains i
1157b 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 nline asm code f
1157c 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 or retrieving "h
1157d 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 igh-performance"
1157e 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 .** counters for
1157f 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e x86 class CPUs.
11580 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 .*/.#ifndef _HWT
11581 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f IME_H_.#define _
11582 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a HWTIME_H_../*.**
11583 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
11584 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b outine only work
11585 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 s on pentium-cla
11586 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 ss (or newer) pr
11587 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 ocessors..** It
11588 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f uses the RDTSC o
11589 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 pcode to read th
1158a 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 e cycle count va
1158b 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a lue out of the.*
1158c 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 * processor and
1158d 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c returns that val
1158e 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 ue. This can be
1158f 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 used for high-r
11590 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e es.** profiling.
11591 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 .*/.#if (defined
11592 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 (__GNUC__) || de
11593 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 fined(_MSC_VER))
11594 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 && \. (def
11595 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 ined(i386) || de
11596 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 fined(__i386__)
11597 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 || defined(_M_IX
11598 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 86)).. #if defi
11599 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 ned(__GNUC__)..
1159a 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 __inline__ sqli
1159b 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
1159c 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
1159d 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
1159e 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f lo, hi;. __
1159f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 asm__ __volatile
115a0 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d __ ("rdtsc" : "=
115a1 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 a" (lo), "=d" (h
115a2 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e i));. return
115a3 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 (sqlite_uint64)
115a4 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 hi << 32 | lo;.
115a5 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 }.. #elif defi
115a6 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 ned(_MSC_VER)..
115a7 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 __declspec(nake
115a8 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 d) __inline sqli
115a9 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 te_uint64 __cdec
115aa 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 l sqlite3Hwtime(
115ab 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 void){. __as
115ac 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 m {. rdts
115ad 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20 c. ret
115ae 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c ; return val
115af 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 ue at EDX:EAX.
115b0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 }. }.. #end
115b1 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e if..#elif (defin
115b2 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 ed(__GNUC__) &&
115b3 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 defined(__x86_64
115b4 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 __)).. __inline
115b5 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 __ sqlite_uint64
115b6 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
115b7 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 oid){. unsi
115b8 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 gned long val;.
115b9 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 __asm__ __v
115ba 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 olatile__ ("rdts
115bb 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 c" : "=A" (val))
115bc 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 ;. return v
115bd 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 al;. }. .#elif
115be 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f (defined(__GNUC_
115bf 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f _) && defined(__
115c0 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c ppc__)).. __inl
115c1 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
115c2 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
115c3 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 e(void){. u
115c4 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
115c5 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 g retval;.
115c6 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 unsigned long ju
115c7 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f nk;. __asm_
115c8 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
115c9 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 "\n\. 1
115ca 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 : mftbu %
115cb 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 1\n\.
115cc 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 mftb %
115cd 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 L0\n\.
115ce 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 mftbu
115cf 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 %0\n\.
115d0 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 cmpw
115d1 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 %0,%1\n\.
115d2 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 bne
115d3 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 1b".
115d4 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 : "=r"
115d5 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 (retval), "=r" (
115d6 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 junk));. re
115d7 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d turn retval;. }
115d8 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f ..#else.. #erro
115d9 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 r Need implement
115da 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 ation of sqlite3
115db 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 Hwtime() for you
115dc 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f r platform... /
115dd 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c *. ** To compil
115de 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d e without implem
115df 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 enting sqlite3Hw
115e0 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 time() for your
115e1 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 platform,. ** y
115e2 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 ou can remove th
115e3 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 e above #error a
115e4 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f nd use the follo
115e5 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 wing. ** stub f
115e6 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 unction. You wi
115e7 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 ll lose timing s
115e8 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a upport for many.
115e9 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 ** of the debu
115ea 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e gging and testin
115eb 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 g utilities, but
115ec 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 it should at.
115ed 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 ** least compile
115ee 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 and run.. */.S
115ef 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
115f0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
115f1 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
115f2 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 ){ return ((sqli
115f3 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a te_uint64)0); }.
115f4 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 .#endif..#endif
115f5 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 /* !defined(_HWT
115f6 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a IME_H_) */../***
115f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
115f8 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a of hwtime.h ****
115f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
115fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
115fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
115fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
115fd 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
115fe 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 left off in os_c
115ff 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
11600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 **********/..sta
11601 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 tic sqlite_uint6
11602 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 4 g_start;.stati
11603 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 c sqlite_uint64
11604 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 g_elapsed;.#defi
11605 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 ne TIMER_START
11606 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c g_start=sql
11607 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 ite3Hwtime().#de
11608 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 fine TIMER_END
11609 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 g_elapsed
1160a 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 =sqlite3Hwtime()
1160b 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 -g_start.#define
1160c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 TIMER_ELAPSED
1160d 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c g_elapsed.#el
1160e 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 se.#define TIMER
1160f 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 _START.#define T
11610 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 IMER_END.#define
11611 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 TIMER_ELAPSED
11612 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 ((sqlite_uint
11613 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 64)0).#endif../*
11614 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c .** If we compil
11615 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 e with the SQLIT
11616 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 E_TEST macro set
11617 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f , then the follo
11618 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 wing block.** of
11619 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 code will give
1161a 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 us the ability t
1161b 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 o simulate a dis
1161c 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 k I/O error. Th
1161d 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f is.** is used fo
1161e 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f r testing the I/
1161f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 O recovery logic
11620 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
11621 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
11622 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
11623 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b o_error_hit = 0;
11624 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
11625 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 otal number of I
11626 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c /O Errors */.SQL
11627 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
11628 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 te3_io_error_har
11629 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 dhit = 0;
1162a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f /* Number of no
1162b 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 n-benign errors
1162c 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1162d 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
1162e 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 or_pending = 0;
1162f 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 /* Count
11630 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f down to first I/
11631 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 O error */.SQLIT
11632 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
11633 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 3_io_error_persi
11634 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f st = 0; /
11635 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 * True if I/O er
11636 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a rors persist */.
11637 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
11638 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
11639 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 benign = 0;
1163a 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 /* True if e
1163b 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e rrors are benign
1163c 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
1163d 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 nt sqlite3_diskf
1163e 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b ull_pending = 0;
1163f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
11640 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
11641 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 = 0;.#define Si
11642 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
11643 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 ign(X) sqlite3_i
11644 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 o_error_benign=(
11645 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c X).#define Simul
11646 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 ateIOError(CODE)
11647 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 \. if( (sqlit
11648 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 e3_io_error_pers
11649 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 ist && sqlite3_i
1164a 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 o_error_hit) \.
1164b 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 || sqlite3
1164c 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e _io_error_pendin
1164d 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 g-- == 1 ) \.
1164e 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f { lo
1164f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 cal_ioerr(); COD
11650 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 E; }.static void
11651 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a local_ioerr(){.
11652 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 IOTRACE(("IOER
11653 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 R\n"));. sqlite
11654 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3_io_error_hit++
11655 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 ;. if( !sqlite3
11656 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e _io_error_benign
11657 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 ) sqlite3_io_er
11658 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d ror_hardhit++;.}
11659 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
1165a 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 eDiskfullError(C
1165b 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 ODE) \. if( sq
1165c 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 lite3_diskfull_p
1165d 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 ending ){ \.
1165e 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 if( sqlite3_dis
1165f 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d kfull_pending ==
11660 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 1 ){ \. l
11661 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a ocal_ioerr(); \.
11662 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 sqlite3_d
11663 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 iskfull = 1; \.
11664 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f sqlite3_io
11665 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 _error_hit = 1;
11666 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c \. CODE; \
11667 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 . }else{ \.
11668 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 sqlite3_di
11669 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d skfull_pending--
1166a 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 ; \. } \.
1166b 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 }.#else.#define
1166c 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 SimulateIOErrorB
1166d 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 enign(X).#define
1166e 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
1166f 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 (A).#define Simu
11670 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f lateDiskfullErro
11671 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a r(A).#endif../*.
11672 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c ** When testing,
11673 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 keep a count of
11674 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f the number of o
11675 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 pen files..*/.#i
11676 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
11677 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
11678 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c sqlite3_open_fil
11679 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 e_count = 0;.#de
1167a 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 fine OpenCounter
1167b 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 (X) sqlite3_ope
1167c 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 n_file_count+=(X
1167d 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 ).#else.#define
1167e 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 OpenCounter(X).#
1167f 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a endif..#endif /*
11680 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f !defined(_OS_CO
11681 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a MMON_H_) */../**
11682 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
11683 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 of os_common.h
11684 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11685 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11686 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
11687 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
11688 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
11689 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f left off in os_
1168a 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a win.c **********
1168b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
1168c 0a 2a 2a 20 43 6f 6d 70 69 6c 69 6e 67 20 61 6e .** Compiling an
1168d 64 20 75 73 69 6e 67 20 57 41 4c 20 6d 6f 64 65 d using WAL mode
1168e 20 72 65 71 75 69 72 65 73 20 73 65 76 65 72 61 requires severa
1168f 6c 20 41 50 49 73 20 74 68 61 74 20 61 72 65 20 l APIs that are
11690 6f 6e 6c 79 0a 2a 2a 20 61 76 61 69 6c 61 62 6c only.** availabl
11691 65 20 69 6e 20 57 69 6e 64 6f 77 73 20 70 6c 61 e in Windows pla
11692 74 66 6f 72 6d 73 20 62 61 73 65 64 20 6f 6e 20 tforms based on
11693 74 68 65 20 4e 54 20 6b 65 72 6e 65 6c 2e 0a 2a the NT kernel..*
11694 2f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 /.#if !SQLITE_OS
11695 5f 57 49 4e 4e 54 20 26 26 20 21 64 65 66 69 6e _WINNT && !defin
11696 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 ed(SQLITE_OMIT_W
11697 41 4c 29 0a 23 20 65 72 72 6f 72 20 22 57 41 4c AL).# error "WAL
11698 20 6d 6f 64 65 20 72 65 71 75 69 72 65 73 20 73 mode requires s
11699 75 70 70 6f 72 74 20 66 72 6f 6d 20 74 68 65 20 upport from the
1169a 57 69 6e 64 6f 77 73 20 4e 54 20 6b 65 72 6e 65 Windows NT kerne
1169b 6c 2c 20 63 6f 6d 70 69 6c 65 5c 0a 20 77 69 74 l, compile\. wit
1169c 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 h SQLITE_OMIT_WA
1169d 4c 2e 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a L.".#endif../*.*
1169e 2a 20 41 72 65 20 6d 6f 73 74 20 6f 66 20 74 68 * Are most of th
1169f 65 20 57 69 6e 33 32 20 41 4e 53 49 20 41 50 49 e Win32 ANSI API
116a0 73 20 61 76 61 69 6c 61 62 6c 65 20 28 69 2e 65 s available (i.e
116a1 2e 20 77 69 74 68 20 63 65 72 74 61 69 6e 20 65 . with certain e
116a2 78 63 65 70 74 69 6f 6e 73 0a 2a 2a 20 62 61 73 xceptions.** bas
116a3 65 64 20 6f 6e 20 74 68 65 20 73 75 62 2d 70 6c ed on the sub-pl
116a4 61 74 66 6f 72 6d 29 3f 0a 2a 2f 0a 23 69 66 20 atform)?.*/.#if
116a5 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 !SQLITE_OS_WINCE
116a6 20 26 26 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 && !SQLITE_OS_W
116a7 49 4e 52 54 0a 23 20 20 64 65 66 69 6e 65 20 53 INRT.# define S
116a8 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f QLITE_WIN32_HAS_
116a9 41 4e 53 49 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a ANSI.#endif../*.
116aa 2a 2a 20 41 72 65 20 6d 6f 73 74 20 6f 66 20 74 ** Are most of t
116ab 68 65 20 57 69 6e 33 32 20 55 6e 69 63 6f 64 65 he Win32 Unicode
116ac 20 41 50 49 73 20 61 76 61 69 6c 61 62 6c 65 20 APIs available
116ad 28 69 2e 65 2e 20 77 69 74 68 20 63 65 72 74 61 (i.e. with certa
116ae 69 6e 20 65 78 63 65 70 74 69 6f 6e 73 0a 2a 2a in exceptions.**
116af 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 73 75 based on the su
116b0 62 2d 70 6c 61 74 66 6f 72 6d 29 3f 0a 2a 2f 0a b-platform)?.*/.
116b1 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
116b2 4e 43 45 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 53 NCE || SQLITE_OS
116b3 5f 57 49 4e 4e 54 20 7c 7c 20 53 51 4c 49 54 45 _WINNT || SQLITE
116b4 5f 4f 53 5f 57 49 4e 52 54 0a 23 20 20 64 65 66 _OS_WINRT.# def
116b5 69 6e 65 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 ine SQLITE_WIN32
116b6 5f 48 41 53 5f 57 49 44 45 0a 23 65 6e 64 69 66 _HAS_WIDE.#endif
116b7 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 77 65 20 6e 65 ../*.** Do we ne
116b8 65 64 20 74 6f 20 6d 61 6e 75 61 6c 6c 79 20 64 ed to manually d
116b9 65 66 69 6e 65 20 74 68 65 20 57 69 6e 33 32 20 efine the Win32
116ba 66 69 6c 65 20 6d 61 70 70 69 6e 67 20 41 50 49 file mapping API
116bb 73 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 57 s for use with W
116bc 41 4c 0a 2a 2a 20 6d 6f 64 65 20 28 65 2e 67 2e AL.** mode (e.g.
116bd 20 74 68 65 73 65 20 41 50 49 73 20 61 72 65 20 these APIs are
116be 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 available in the
116bf 20 57 69 6e 64 6f 77 73 20 43 45 20 53 44 4b 3b Windows CE SDK;
116c0 20 68 6f 77 65 76 65 72 2c 20 74 68 65 79 0a 2a however, they.*
116c1 2a 20 61 72 65 20 6e 6f 74 20 70 72 65 73 65 6e * are not presen
116c2 74 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 t in the header
116c3 66 69 6c 65 29 3f 0a 2a 2f 0a 23 69 66 20 53 51 file)?.*/.#if SQ
116c4 4c 49 54 45 5f 57 49 4e 33 32 5f 46 49 4c 45 4d LITE_WIN32_FILEM
116c5 41 50 50 49 4e 47 5f 41 50 49 20 26 26 20 21 64 APPING_API && !d
116c6 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
116c7 49 54 5f 57 41 4c 29 0a 2f 2a 0a 2a 2a 20 54 77 IT_WAL)./*.** Tw
116c8 6f 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6d 61 o of the file ma
116c9 70 70 69 6e 67 20 41 50 49 73 20 61 72 65 20 64 pping APIs are d
116ca 69 66 66 65 72 65 6e 74 20 75 6e 64 65 72 20 57 ifferent under W
116cb 69 6e 52 54 2e 20 20 46 69 67 75 72 65 20 6f 75 inRT. Figure ou
116cc 74 20 77 68 69 63 68 0a 2a 2a 20 73 65 74 20 77 t which.** set w
116cd 65 20 6e 65 65 64 2e 0a 2a 2f 0a 23 69 66 20 53 e need..*/.#if S
116ce 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 57 QLITE_OS_WINRT.W
116cf 49 4e 42 41 53 45 41 50 49 20 48 41 4e 44 4c 45 INBASEAPI HANDLE
116d0 20 57 49 4e 41 50 49 20 43 72 65 61 74 65 46 69 WINAPI CreateFi
116d1 6c 65 4d 61 70 70 69 6e 67 46 72 6f 6d 41 70 70 leMappingFromApp
116d2 28 48 41 4e 44 4c 45 2c 20 5c 0a 20 20 20 20 20 (HANDLE, \.
116d3 20 20 20 4c 50 53 45 43 55 52 49 54 59 5f 41 54 LPSECURITY_AT
116d4 54 52 49 42 55 54 45 53 2c 20 55 4c 4f 4e 47 2c TRIBUTES, ULONG,
116d5 20 55 4c 4f 4e 47 36 34 2c 20 4c 50 43 57 53 54 ULONG64, LPCWST
116d6 52 29 3b 0a 0a 57 49 4e 42 41 53 45 41 50 49 20 R);..WINBASEAPI
116d7 4c 50 56 4f 49 44 20 57 49 4e 41 50 49 20 4d 61 LPVOID WINAPI Ma
116d8 70 56 69 65 77 4f 66 46 69 6c 65 46 72 6f 6d 41 pViewOfFileFromA
116d9 70 70 28 48 41 4e 44 4c 45 2c 20 55 4c 4f 4e 47 pp(HANDLE, ULONG
116da 2c 20 55 4c 4f 4e 47 36 34 2c 20 53 49 5a 45 5f , ULONG64, SIZE_
116db 54 29 3b 0a 23 65 6c 73 65 0a 23 69 66 20 64 65 T);.#else.#if de
116dc 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e fined(SQLITE_WIN
116dd 33 32 5f 48 41 53 5f 41 4e 53 49 29 0a 57 49 4e 32_HAS_ANSI).WIN
116de 42 41 53 45 41 50 49 20 48 41 4e 44 4c 45 20 57 BASEAPI HANDLE W
116df 49 4e 41 50 49 20 43 72 65 61 74 65 46 69 6c 65 INAPI CreateFile
116e0 4d 61 70 70 69 6e 67 41 28 48 41 4e 44 4c 45 2c MappingA(HANDLE,
116e1 20 4c 50 53 45 43 55 52 49 54 59 5f 41 54 54 52 LPSECURITY_ATTR
116e2 49 42 55 54 45 53 2c 20 5c 0a 20 20 20 20 20 20 IBUTES, \.
116e3 20 20 44 57 4f 52 44 2c 20 44 57 4f 52 44 2c 20 DWORD, DWORD,
116e4 44 57 4f 52 44 2c 20 4c 50 43 53 54 52 29 3b 0a DWORD, LPCSTR);.
116e5 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 #endif /* define
116e6 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 d(SQLITE_WIN32_H
116e7 41 53 5f 41 4e 53 49 29 20 2a 2f 0a 0a 23 69 66 AS_ANSI) */..#if
116e8 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
116e9 57 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a WIN32_HAS_WIDE).
116ea 57 49 4e 42 41 53 45 41 50 49 20 48 41 4e 44 4c WINBASEAPI HANDL
116eb 45 20 57 49 4e 41 50 49 20 43 72 65 61 74 65 46 E WINAPI CreateF
116ec 69 6c 65 4d 61 70 70 69 6e 67 57 28 48 41 4e 44 ileMappingW(HAND
116ed 4c 45 2c 20 4c 50 53 45 43 55 52 49 54 59 5f 41 LE, LPSECURITY_A
116ee 54 54 52 49 42 55 54 45 53 2c 20 5c 0a 20 20 20 TTRIBUTES, \.
116ef 20 20 20 20 20 44 57 4f 52 44 2c 20 44 57 4f 52 DWORD, DWOR
116f0 44 2c 20 44 57 4f 52 44 2c 20 4c 50 43 57 53 54 D, DWORD, LPCWST
116f1 52 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 R);.#endif /* de
116f2 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e fined(SQLITE_WIN
116f3 33 32 5f 48 41 53 5f 57 49 44 45 29 20 2a 2f 0a 32_HAS_WIDE) */.
116f4 0a 57 49 4e 42 41 53 45 41 50 49 20 4c 50 56 4f .WINBASEAPI LPVO
116f5 49 44 20 57 49 4e 41 50 49 20 4d 61 70 56 69 65 ID WINAPI MapVie
116f6 77 4f 66 46 69 6c 65 28 48 41 4e 44 4c 45 2c 20 wOfFile(HANDLE,
116f7 44 57 4f 52 44 2c 20 44 57 4f 52 44 2c 20 44 57 DWORD, DWORD, DW
116f8 4f 52 44 2c 20 53 49 5a 45 5f 54 29 3b 0a 23 65 ORD, SIZE_T);.#e
116f9 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
116fa 53 5f 57 49 4e 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a S_WINRT */../*.*
116fb 2a 20 54 68 69 73 20 66 69 6c 65 20 6d 61 70 70 * This file mapp
116fc 69 6e 67 20 41 50 49 20 69 73 20 63 6f 6d 6d 6f ing API is commo
116fd 6e 20 74 6f 20 62 6f 74 68 20 57 69 6e 33 32 20 n to both Win32
116fe 61 6e 64 20 57 69 6e 52 54 2e 0a 2a 2f 0a 57 49 and WinRT..*/.WI
116ff 4e 42 41 53 45 41 50 49 20 42 4f 4f 4c 20 57 49 NBASEAPI BOOL WI
11700 4e 41 50 49 20 55 6e 6d 61 70 56 69 65 77 4f 66 NAPI UnmapViewOf
11701 46 69 6c 65 28 4c 50 43 56 4f 49 44 29 3b 0a 23 File(LPCVOID);.#
11702 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
11703 57 49 4e 33 32 5f 46 49 4c 45 4d 41 50 50 49 4e WIN32_FILEMAPPIN
11704 47 5f 41 50 49 20 26 26 20 21 64 65 66 69 6e 65 G_API && !define
11705 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 d(SQLITE_OMIT_WA
11706 4c 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 L) */../*.** Mac
11707 72 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d ro to find the m
11708 69 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 inimum of two nu
11709 6d 65 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f meric values..*/
1170a 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 .#ifndef MIN.# d
1170b 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 efine MIN(x,y) (
1170c 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 (x)<(y)?(x):(y))
1170d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 .#endif../*.** S
1170e 6f 6d 65 20 4d 69 63 72 6f 73 6f 66 74 20 63 6f ome Microsoft co
1170f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69 mpilers lack thi
11710 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f s definition..*/
11711 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44 .#ifndef INVALID
11712 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 _FILE_ATTRIBUTES
11713 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c 49 .# define INVALI
11714 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 D_FILE_ATTRIBUTE
11715 53 20 28 28 44 57 4f 52 44 29 2d 31 29 20 0a 23 S ((DWORD)-1) .#
11716 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 46 endif..#ifndef F
11717 49 4c 45 5f 46 4c 41 47 5f 4d 41 53 4b 0a 23 20 ILE_FLAG_MASK.#
11718 64 65 66 69 6e 65 20 46 49 4c 45 5f 46 4c 41 47 define FILE_FLAG
11719 5f 4d 41 53 4b 20 20 20 20 20 20 20 20 20 20 28 _MASK (
1171a 30 78 46 46 33 43 30 30 30 30 29 0a 23 65 6e 64 0xFF3C0000).#end
1171b 69 66 0a 0a 23 69 66 6e 64 65 66 20 46 49 4c 45 if..#ifndef FILE
1171c 5f 41 54 54 52 49 42 55 54 45 5f 4d 41 53 4b 0a _ATTRIBUTE_MASK.
1171d 23 20 64 65 66 69 6e 65 20 46 49 4c 45 5f 41 54 # define FILE_AT
1171e 54 52 49 42 55 54 45 5f 4d 41 53 4b 20 20 20 20 TRIBUTE_MASK
1171f 20 28 30 78 30 30 30 33 46 46 46 37 29 0a 23 65 (0x0003FFF7).#e
11720 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
11721 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 2f 2a LITE_OMIT_WAL./*
11722 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e Forward referen
11723 63 65 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 ces */.typedef s
11724 74 72 75 63 74 20 77 69 6e 53 68 6d 20 77 69 6e truct winShm win
11725 53 68 6d 3b 20 20 20 20 20 20 20 20 20 20 20 2f Shm; /
11726 2a 20 41 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 * A connection t
11727 6f 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 o shared-memory
11728 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
11729 74 20 77 69 6e 53 68 6d 4e 6f 64 65 20 77 69 6e t winShmNode win
1172a 53 68 6d 4e 6f 64 65 3b 20 20 20 2f 2a 20 41 20 ShmNode; /* A
1172b 72 65 67 69 6f 6e 20 6f 66 20 73 68 61 72 65 64 region of shared
1172c 2d 6d 65 6d 6f 72 79 20 2a 2f 0a 23 65 6e 64 69 -memory */.#endi
1172d 66 0a 0a 2f 2a 0a 2a 2a 20 57 69 6e 43 45 20 6c f../*.** WinCE l
1172e 61 63 6b 73 20 6e 61 74 69 76 65 20 73 75 70 70 acks native supp
1172f 6f 72 74 20 66 6f 72 20 66 69 6c 65 20 6c 6f 63 ort for file loc
11730 6b 69 6e 67 20 73 6f 20 77 65 20 68 61 76 65 20 king so we have
11731 74 6f 20 66 61 6b 65 20 69 74 0a 2a 2a 20 77 69 to fake it.** wi
11732 74 68 20 73 6f 6d 65 20 63 6f 64 65 20 6f 66 20 th some code of
11733 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 20 our own..*/.#if
11734 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
11735 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 77 typedef struct w
11736 69 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e 74 inceLock {. int
11737 20 6e 52 65 61 64 65 72 73 3b 20 20 20 20 20 20 nReaders;
11738 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 /* Number of re
11739 61 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74 61 69 ader locks obtai
1173a 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 50 ned */. BOOL bP
1173b 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f 2a 20 ending; /*
1173c 49 6e 64 69 63 61 74 65 73 20 61 20 70 65 6e 64 Indicates a pend
1173d 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 ing lock has bee
1173e 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 n obtained */.
1173f 42 4f 4f 4c 20 62 52 65 73 65 72 76 65 64 3b 20 BOOL bReserved;
11740 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 /* Indicates
11741 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b a reserved lock
11742 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e has been obtain
11743 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 45 78 ed */. BOOL bEx
11744 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a 20 49 clusive; /* I
11745 6e 64 69 63 61 74 65 73 20 61 6e 20 65 78 63 6c ndicates an excl
11746 75 73 69 76 65 20 6c 6f 63 6b 20 68 61 73 20 62 usive lock has b
11747 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a een obtained */.
11748 7d 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23 65 6e } winceLock;.#en
11749 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 77 dif../*.** The w
1174a 69 6e 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 inFile structure
1174b 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f is a subclass o
1174c 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 20 f sqlite3_file*
1174d 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 specific to the
1174e 77 69 6e 33 32 0a 2a 2a 20 70 6f 72 74 61 62 69 win32.** portabi
1174f 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 lity layer..*/.t
11750 79 70 65 64 65 66 20 73 74 72 75 63 74 20 77 69 ypedef struct wi
11751 6e 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b 0a 73 nFile winFile;.s
11752 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20 7b 0a truct winFile {.
11753 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f const sqlite3_
11754 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 io_methods *pMet
11755 68 6f 64 3b 20 2f 2a 2a 2a 20 4d 75 73 74 20 62 hod; /*** Must b
11756 65 20 66 69 72 73 74 20 2a 2a 2a 2f 0a 20 20 73 e first ***/. s
11757 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
11758 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 ; /* The VF
11759 53 20 75 73 65 64 20 74 6f 20 6f 70 65 6e 20 74 S used to open t
1175a 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 48 41 his file */. HA
1175b 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20 NDLE h;
1175c 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 /* Handle
1175d 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 for accessing th
1175e 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6c e file */. u8 l
1175f 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 20 ocktype;
11760 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c /* Type of l
11761 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 68 65 ock currently he
11762 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 ld on this file
11763 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 61 72 65 */. short share
11764 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 2f 2a 20 dLockByte; /*
11765 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 Randomly chosen
11766 62 79 74 65 20 75 73 65 64 20 61 73 20 61 20 73 byte used as a s
11767 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 hared lock */.
11768 75 38 20 63 74 72 6c 46 6c 61 67 73 3b 20 20 20 u8 ctrlFlags;
11769 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 /* Flags
1176a 2e 20 20 53 65 65 20 57 49 4e 46 49 4c 45 5f 2a . See WINFILE_*
1176b 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 44 57 4f 52 below */. DWOR
1176c 44 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20 20 D lastErrno;
1176d 20 20 20 20 2f 2a 20 54 68 65 20 57 69 6e 64 6f /* The Windo
1176e 77 73 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 ws errno from th
1176f 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 e last I/O error
11770 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
11771 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20 20 77 69 TE_OMIT_WAL. wi
11772 6e 53 68 6d 20 2a 70 53 68 6d 3b 20 20 20 20 20 nShm *pShm;
11773 20 20 20 20 20 20 2f 2a 20 49 6e 73 74 61 6e 63 /* Instanc
11774 65 20 6f 66 20 73 68 61 72 65 64 20 6d 65 6d 6f e of shared memo
11775 72 79 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 ry on this file
11776 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 6f 6e 73 */.#endif. cons
11777 74 20 63 68 61 72 20 2a 7a 50 61 74 68 3b 20 20 t char *zPath;
11778 20 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68 /* Full path
11779 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66 69 6c name of this fil
1177a 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 43 68 75 e */. int szChu
1177b 6e 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f nk; /
1177c 2a 20 43 68 75 6e 6b 20 73 69 7a 65 20 63 6f 6e * Chunk size con
1177d 66 69 67 75 72 65 64 20 62 79 20 46 43 4e 54 4c figured by FCNTL
1177e 5f 43 48 55 4e 4b 5f 53 49 5a 45 20 2a 2f 0a 23 _CHUNK_SIZE */.#
1177f 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
11780 43 45 0a 20 20 4c 50 57 53 54 52 20 7a 44 65 6c CE. LPWSTR zDel
11781 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20 eteOnClose; /*
11782 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Name of file to
11783 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73 delete when clos
11784 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 ing */. HANDLE
11785 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 hMutex;
11786 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74 /* Mutex used t
11787 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 o control access
11788 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 to shared lock
11789 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 53 */ . HANDLE hS
1178a 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 2f hared; /
1178b 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 * Shared memory
1178c 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f 72 segment used for
1178d 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 69 locking */. wi
1178e 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20 nceLock local;
1178f 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f /* Locks o
11790 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20 btained by this
11791 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e 46 instance of winF
11792 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f ile */. winceLo
11793 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 20 ck *shared;
11794 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 65 /* Global share
11795 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 6f d lock memory fo
11796 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a 23 r the file */.#
11797 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
11798 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 Allowed values f
11799 6f 72 20 77 69 6e 46 69 6c 65 2e 63 74 72 6c 46 or winFile.ctrlF
1179a 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 lags.*/.#define
1179b 57 49 4e 46 49 4c 45 5f 50 45 52 53 49 53 54 5f WINFILE_PERSIST_
1179c 57 41 4c 20 20 20 20 20 30 78 30 34 20 20 20 2f WAL 0x04 /
1179d 2a 20 50 65 72 73 69 73 74 65 6e 74 20 57 41 4c * Persistent WAL
1179e 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 mode */.#define
1179f 20 57 49 4e 46 49 4c 45 5f 50 53 4f 57 20 20 20 WINFILE_PSOW
117a0 20 20 20 20 20 20 20 20 20 30 78 31 30 20 20 20 0x10
117a1 2f 2a 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f /* SQLITE_IOCAP_
117a2 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 52 POWERSAFE_OVERWR
117a3 49 54 45 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 54 68 ITE */../*. * Th
117a4 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 e size of the bu
117a5 66 66 65 72 20 75 73 65 64 20 62 79 20 73 71 6c ffer used by sql
117a6 69 74 65 33 5f 77 69 6e 33 32 5f 77 72 69 74 65 ite3_win32_write
117a7 5f 64 65 62 75 67 28 29 2e 0a 20 2a 2f 0a 23 69 _debug().. */.#i
117a8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e fndef SQLITE_WIN
117a9 33 32 5f 44 42 47 5f 42 55 46 5f 53 49 5a 45 0a 32_DBG_BUF_SIZE.
117aa 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 # define SQLITE
117ab 5f 57 49 4e 33 32 5f 44 42 47 5f 42 55 46 5f 53 _WIN32_DBG_BUF_S
117ac 49 5a 45 20 20 20 28 28 69 6e 74 29 28 34 30 39 IZE ((int)(409
117ad 36 2d 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 29 6-sizeof(DWORD))
117ae 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 ).#endif../*. *
117af 54 68 65 20 76 61 6c 75 65 20 75 73 65 64 20 77 The value used w
117b0 69 74 68 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 ith sqlite3_win3
117b1 32 5f 73 65 74 5f 64 69 72 65 63 74 6f 72 79 28 2_set_directory(
117b2 29 20 74 6f 20 73 70 65 63 69 66 79 20 74 68 61 ) to specify tha
117b3 74 0a 20 2a 20 74 68 65 20 64 61 74 61 20 64 69 t. * the data di
117b4 72 65 63 74 6f 72 79 20 73 68 6f 75 6c 64 20 62 rectory should b
117b5 65 20 63 68 61 6e 67 65 64 2e 0a 20 2a 2f 0a 23 e changed.. */.#
117b6 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 57 49 ifndef SQLITE_WI
117b7 4e 33 32 5f 44 41 54 41 5f 44 49 52 45 43 54 4f N32_DATA_DIRECTO
117b8 52 59 5f 54 59 50 45 0a 23 20 20 64 65 66 69 6e RY_TYPE.# defin
117b9 65 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 44 e SQLITE_WIN32_D
117ba 41 54 41 5f 44 49 52 45 43 54 4f 52 59 5f 54 59 ATA_DIRECTORY_TY
117bb 50 45 20 28 31 29 0a 23 65 6e 64 69 66 0a 0a 2f PE (1).#endif../
117bc 2a 0a 20 2a 20 54 68 65 20 76 61 6c 75 65 20 75 *. * The value u
117bd 73 65 64 20 77 69 74 68 20 73 71 6c 69 74 65 33 sed with sqlite3
117be 5f 77 69 6e 33 32 5f 73 65 74 5f 64 69 72 65 63 _win32_set_direc
117bf 74 6f 72 79 28 29 20 74 6f 20 73 70 65 63 69 66 tory() to specif
117c0 79 20 74 68 61 74 0a 20 2a 20 74 68 65 20 74 65 y that. * the te
117c1 6d 70 6f 72 61 72 79 20 64 69 72 65 63 74 6f 72 mporary director
117c2 79 20 73 68 6f 75 6c 64 20 62 65 20 63 68 61 6e y should be chan
117c3 67 65 64 2e 0a 20 2a 2f 0a 23 69 66 6e 64 65 66 ged.. */.#ifndef
117c4 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 54 45 SQLITE_WIN32_TE
117c5 4d 50 5f 44 49 52 45 43 54 4f 52 59 5f 54 59 50 MP_DIRECTORY_TYP
117c6 45 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 E.# define SQLI
117c7 54 45 5f 57 49 4e 33 32 5f 54 45 4d 50 5f 44 49 TE_WIN32_TEMP_DI
117c8 52 45 43 54 4f 52 59 5f 54 59 50 45 20 28 32 29 RECTORY_TYPE (2)
117c9 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 49 .#endif../*. * I
117ca 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 f compiled with
117cb 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 4d 41 4c SQLITE_WIN32_MAL
117cc 4c 4f 43 20 6f 6e 20 57 69 6e 64 6f 77 73 2c 20 LOC on Windows,
117cd 77 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 0a we will use the.
117ce 20 2a 20 76 61 72 69 6f 75 73 20 57 69 6e 33 32 * various Win32
117cf 20 41 50 49 20 68 65 61 70 20 66 75 6e 63 74 69 API heap functi
117d0 6f 6e 73 20 69 6e 73 74 65 61 64 20 6f 66 20 6f ons instead of o
117d1 75 72 20 6f 77 6e 2e 0a 20 2a 2f 0a 23 69 66 64 ur own.. */.#ifd
117d2 65 66 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f ef SQLITE_WIN32_
117d3 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 20 2a 20 49 66 MALLOC../*. * If
117d4 20 74 68 69 73 20 69 73 20 6e 6f 6e 2d 7a 65 72 this is non-zer
117d5 6f 2c 20 61 6e 20 69 73 6f 6c 61 74 65 64 20 68 o, an isolated h
117d6 65 61 70 20 77 69 6c 6c 20 62 65 20 63 72 65 61 eap will be crea
117d7 74 65 64 20 62 79 20 74 68 65 20 6e 61 74 69 76 ted by the nativ
117d8 65 20 57 69 6e 33 32 0a 20 2a 20 61 6c 6c 6f 63 e Win32. * alloc
117d9 61 74 6f 72 20 73 75 62 73 79 73 74 65 6d 3b 20 ator subsystem;
117da 6f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 64 otherwise, the d
117db 65 66 61 75 6c 74 20 70 72 6f 63 65 73 73 20 68 efault process h
117dc 65 61 70 20 77 69 6c 6c 20 62 65 20 75 73 65 64 eap will be used
117dd 2e 20 20 54 68 69 73 0a 20 2a 20 73 65 74 74 69 . This. * setti
117de 6e 67 20 68 61 73 20 6e 6f 20 65 66 66 65 63 74 ng has no effect
117df 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 when compiling
117e0 66 6f 72 20 57 69 6e 52 54 2e 20 20 42 79 20 64 for WinRT. By d
117e1 65 66 61 75 6c 74 2c 20 74 68 69 73 20 69 73 20 efault, this is
117e2 65 6e 61 62 6c 65 64 0a 20 2a 20 61 6e 64 20 61 enabled. * and a
117e3 6e 20 69 73 6f 6c 61 74 65 64 20 68 65 61 70 20 n isolated heap
117e4 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 20 will be created
117e5 74 6f 20 73 74 6f 72 65 20 61 6c 6c 20 61 6c 6c to store all all
117e6 6f 63 61 74 65 64 20 64 61 74 61 2e 0a 20 2a 0a ocated data.. *.
117e7 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
117e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
117e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
117ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
117eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
117ec 20 2a 20 57 41 52 4e 49 4e 47 3a 20 49 74 20 69 * WARNING: It i
117ed 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e s important to n
117ee 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 74 68 ote that when th
117ef 69 73 20 73 65 74 74 69 6e 67 20 69 73 20 6e 6f is setting is no
117f0 6e 2d 7a 65 72 6f 20 61 6e 64 20 74 68 65 0a 20 n-zero and the.
117f1 2a 20 20 20 20 20 20 20 20 20 20 77 69 6e 4d 65 * winMe
117f2 6d 53 68 75 74 64 6f 77 6e 20 66 75 6e 63 74 69 mShutdown functi
117f3 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 28 65 2e on is called (e.
117f4 67 2e 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 g. by the sqlite
117f5 33 5f 73 68 75 74 64 6f 77 6e 0a 20 2a 20 20 20 3_shutdown. *
117f6 20 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 29 function)
117f7 2c 20 61 6c 6c 20 64 61 74 61 20 74 68 61 74 20 , all data that
117f8 77 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 was allocated us
117f9 69 6e 67 20 74 68 65 20 69 73 6f 6c 61 74 65 64 ing the isolated
117fa 20 68 65 61 70 20 77 69 6c 6c 0a 20 2a 20 20 20 heap will. *
117fb 20 20 20 20 20 20 20 62 65 20 66 72 65 65 64 20 be freed
117fc 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 6e 64 20 immediately and
117fd 61 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 61 any attempt to a
117fe 63 63 65 73 73 20 61 6e 79 20 6f 66 20 74 68 61 ccess any of tha
117ff 74 20 66 72 65 65 64 0a 20 2a 20 20 20 20 20 20 t freed. *
11800 20 20 20 20 64 61 74 61 20 77 69 6c 6c 20 61 6c data will al
11801 6d 6f 73 74 20 63 65 72 74 61 69 6e 6c 79 20 72 most certainly r
11802 65 73 75 6c 74 20 69 6e 20 61 6e 20 69 6d 6d 65 esult in an imme
11803 64 69 61 74 65 20 61 63 63 65 73 73 20 76 69 6f diate access vio
11804 6c 61 74 69 6f 6e 2e 0a 20 2a 2a 2a 2a 2a 2a 2a lation.. *******
11805 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11806 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11807 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11808 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11809 2a 2a 2a 2a 2a 2a 2a 0a 20 2a 2f 0a 23 69 66 6e *******. */.#ifn
1180a 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 def SQLITE_WIN32
1180b 5f 48 45 41 50 5f 43 52 45 41 54 45 0a 23 20 20 _HEAP_CREATE.#
1180c 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 49 define SQLITE_WI
1180d 4e 33 32 5f 48 45 41 50 5f 43 52 45 41 54 45 20 N32_HEAP_CREATE
1180e 20 20 20 28 54 52 55 45 29 0a 23 65 6e 64 69 66 (TRUE).#endif
1180f 0a 0a 2f 2a 0a 20 2a 20 54 68 65 20 69 6e 69 74 ../*. * The init
11810 69 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 ial size of the
11811 57 69 6e 33 32 2d 73 70 65 63 69 66 69 63 20 68 Win32-specific h
11812 65 61 70 2e 20 20 54 68 69 73 20 76 61 6c 75 65 eap. This value
11813 20 6d 61 79 20 62 65 20 7a 65 72 6f 2e 0a 20 2a may be zero.. *
11814 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
11815 5f 57 49 4e 33 32 5f 48 45 41 50 5f 49 4e 49 54 _WIN32_HEAP_INIT
11816 5f 53 49 5a 45 0a 23 20 20 64 65 66 69 6e 65 20 _SIZE.# define
11817 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41 SQLITE_WIN32_HEA
11818 50 5f 49 4e 49 54 5f 53 49 5a 45 20 28 28 53 51 P_INIT_SIZE ((SQ
11819 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 LITE_DEFAULT_CAC
1181a 48 45 5f 53 49 5a 45 29 20 2a 20 5c 0a 20 20 20 HE_SIZE) * \.
1181b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1181c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1181d 20 20 20 20 28 53 51 4c 49 54 45 5f 44 45 46 41 (SQLITE_DEFA
1181e 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 29 20 2b ULT_PAGE_SIZE) +
1181f 20 34 31 39 34 33 30 34 29 0a 23 65 6e 64 69 66 4194304).#endif
11820 0a 0a 2f 2a 0a 20 2a 20 54 68 65 20 6d 61 78 69 ../*. * The maxi
11821 6d 75 6d 20 73 69 7a 65 20 6f 66 20 74 68 65 20 mum size of the
11822 57 69 6e 33 32 2d 73 70 65 63 69 66 69 63 20 68 Win32-specific h
11823 65 61 70 2e 20 20 54 68 69 73 20 76 61 6c 75 65 eap. This value
11824 20 6d 61 79 20 62 65 20 7a 65 72 6f 2e 0a 20 2a may be zero.. *
11825 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
11826 5f 57 49 4e 33 32 5f 48 45 41 50 5f 4d 41 58 5f _WIN32_HEAP_MAX_
11827 53 49 5a 45 0a 23 20 20 64 65 66 69 6e 65 20 53 SIZE.# define S
11828 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41 50 QLITE_WIN32_HEAP
11829 5f 4d 41 58 5f 53 49 5a 45 20 20 28 30 29 0a 23 _MAX_SIZE (0).#
1182a 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 54 68 65 endif../*. * The
1182b 20 65 78 74 72 61 20 66 6c 61 67 73 20 74 6f 20 extra flags to
1182c 75 73 65 20 69 6e 20 63 61 6c 6c 73 20 74 6f 20 use in calls to
1182d 74 68 65 20 57 69 6e 33 32 20 68 65 61 70 20 41 the Win32 heap A
1182e 50 49 73 2e 20 20 54 68 69 73 20 76 61 6c 75 65 PIs. This value
1182f 20 6d 61 79 20 62 65 0a 20 2a 20 7a 65 72 6f 20 may be. * zero
11830 66 6f 72 20 74 68 65 20 64 65 66 61 75 6c 74 20 for the default
11831 62 65 68 61 76 69 6f 72 2e 0a 20 2a 2f 0a 23 69 behavior.. */.#i
11832 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e fndef SQLITE_WIN
11833 33 32 5f 48 45 41 50 5f 46 4c 41 47 53 0a 23 20 32_HEAP_FLAGS.#
11834 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 define SQLITE_W
11835 49 4e 33 32 5f 48 45 41 50 5f 46 4c 41 47 53 20 IN32_HEAP_FLAGS
11836 20 20 20 20 28 30 29 0a 23 65 6e 64 69 66 0a 0a (0).#endif..
11837 2f 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 4d 65 6d /*.** The winMem
11838 44 61 74 61 20 73 74 72 75 63 74 75 72 65 20 73 Data structure s
11839 74 6f 72 65 73 20 69 6e 66 6f 72 6d 61 74 69 6f tores informatio
1183a 6e 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 n required by th
1183b 65 20 57 69 6e 33 32 2d 73 70 65 63 69 66 69 63 e Win32-specific
1183c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f .** sqlite3_mem_
1183d 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e methods implemen
1183e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 tation..*/.typed
1183f 65 66 20 73 74 72 75 63 74 20 77 69 6e 4d 65 6d ef struct winMem
11840 44 61 74 61 20 77 69 6e 4d 65 6d 44 61 74 61 3b Data winMemData;
11841 0a 73 74 72 75 63 74 20 77 69 6e 4d 65 6d 44 61 .struct winMemDa
11842 74 61 20 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 ta {.#ifndef NDE
11843 42 55 47 0a 20 20 75 33 32 20 6d 61 67 69 63 3b BUG. u32 magic;
11844 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d /* Magic num
11845 62 65 72 20 74 6f 20 64 65 74 65 63 74 20 73 74 ber to detect st
11846 72 75 63 74 75 72 65 20 63 6f 72 72 75 70 74 69 ructure corrupti
11847 6f 6e 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 on. */.#endif.
11848 48 41 4e 44 4c 45 20 68 48 65 61 70 3b 20 2f 2a HANDLE hHeap; /*
11849 20 54 68 65 20 68 61 6e 64 6c 65 20 74 6f 20 6f The handle to o
1184a 75 72 20 68 65 61 70 2e 20 2a 2f 0a 20 20 42 4f ur heap. */. BO
1184b 4f 4c 20 62 4f 77 6e 65 64 3b 20 20 2f 2a 20 44 OL bOwned; /* D
1184c 6f 20 77 65 20 6f 77 6e 20 74 68 65 20 68 65 61 o we own the hea
1184d 70 20 28 69 2e 65 2e 20 64 65 73 74 72 6f 79 20 p (i.e. destroy
1184e 69 74 20 6f 6e 20 73 68 75 74 64 6f 77 6e 29 3f it on shutdown)?
1184f 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 6e 64 65 66 20 */.};..#ifndef
11850 4e 44 45 42 55 47 0a 23 64 65 66 69 6e 65 20 57 NDEBUG.#define W
11851 49 4e 4d 45 4d 5f 4d 41 47 49 43 20 20 20 20 20 INMEM_MAGIC
11852 30 78 34 32 62 32 38 33 30 62 0a 23 65 6e 64 69 0x42b2830b.#endi
11853 66 0a 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 f..static struct
11854 20 77 69 6e 4d 65 6d 44 61 74 61 20 77 69 6e 5f winMemData win_
11855 6d 65 6d 5f 64 61 74 61 20 3d 20 7b 0a 23 69 66 mem_data = {.#if
11856 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 57 49 ndef NDEBUG. WI
11857 4e 4d 45 4d 5f 4d 41 47 49 43 2c 0a 23 65 6e 64 NMEM_MAGIC,.#end
11858 69 66 0a 20 20 4e 55 4c 4c 2c 20 46 41 4c 53 45 if. NULL, FALSE
11859 0a 7d 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 .};..#ifndef NDE
1185a 42 55 47 0a 23 64 65 66 69 6e 65 20 77 69 6e 4d BUG.#define winM
1185b 65 6d 41 73 73 65 72 74 4d 61 67 69 63 28 29 20 emAssertMagic()
1185c 61 73 73 65 72 74 28 20 77 69 6e 5f 6d 65 6d 5f assert( win_mem_
1185d 64 61 74 61 2e 6d 61 67 69 63 3d 3d 57 49 4e 4d data.magic==WINM
1185e 45 4d 5f 4d 41 47 49 43 20 29 0a 23 65 6c 73 65 EM_MAGIC ).#else
1185f 0a 23 64 65 66 69 6e 65 20 77 69 6e 4d 65 6d 41 .#define winMemA
11860 73 73 65 72 74 4d 61 67 69 63 28 29 0a 23 65 6e ssertMagic().#en
11861 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 77 69 6e dif..#define win
11862 4d 65 6d 47 65 74 48 65 61 70 28 29 20 77 69 6e MemGetHeap() win
11863 5f 6d 65 6d 5f 64 61 74 61 2e 68 48 65 61 70 0a _mem_data.hHeap.
11864 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 77 69 .static void *wi
11865 6e 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e nMemMalloc(int n
11866 42 79 74 65 73 29 3b 0a 73 74 61 74 69 63 20 76 Bytes);.static v
11867 6f 69 64 20 77 69 6e 4d 65 6d 46 72 65 65 28 76 oid winMemFree(v
11868 6f 69 64 20 2a 70 50 72 69 6f 72 29 3b 0a 73 74 oid *pPrior);.st
11869 61 74 69 63 20 76 6f 69 64 20 2a 77 69 6e 4d 65 atic void *winMe
1186a 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 mRealloc(void *p
1186b 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 Prior, int nByte
1186c 73 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 77 s);.static int w
1186d 69 6e 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a inMemSize(void *
1186e 70 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 77 p);.static int w
1186f 69 6e 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74 inMemRoundup(int
11870 20 6e 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 n);.static int
11871 77 69 6e 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 winMemInit(void
11872 2a 70 41 70 70 44 61 74 61 29 3b 0a 73 74 61 74 *pAppData);.stat
11873 69 63 20 76 6f 69 64 20 77 69 6e 4d 65 6d 53 68 ic void winMemSh
11874 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 70 41 70 utdown(void *pAp
11875 70 44 61 74 61 29 3b 0a 0a 53 51 4c 49 54 45 5f pData);..SQLITE_
11876 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 PRIVATE const sq
11877 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
11878 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 s *sqlite3MemGet
11879 57 69 6e 33 32 28 76 6f 69 64 29 3b 0a 23 65 6e Win32(void);.#en
1187a 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 57 49 dif /* SQLITE_WI
1187b 4e 33 32 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f N32_MALLOC */../
1187c 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
1187d 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 20 28 ng variable is (
1187e 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f 6e normally) set on
1187f 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68 61 ce and never cha
11880 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66 74 nges.** thereaft
11881 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73 20 er. It records
11882 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 65 72 whether the oper
11883 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 ating system is
11884 57 69 6e 39 78 0a 2a 2a 20 6f 72 20 57 69 6e 4e Win9x.** or WinN
11885 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70 T..**.** 0: Op
11886 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 75 erating system u
11887 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20 nknown..** 1:
11888 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d Operating system
11889 20 69 73 20 57 69 6e 39 78 2e 0a 2a 2a 20 32 3a is Win9x..** 2:
1188a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 Operating sys
1188b 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a tem is WinNT..**
1188c 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 .** In order to
1188d 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74 69 facilitate testi
1188e 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73 79 ng on a WinNT sy
1188f 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20 66 stem, the test f
11890 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d 61 ixture.** can ma
11891 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73 20 nually set this
11892 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65 6d value to 1 to em
11893 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68 61 ulate Win98 beha
11894 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 vior..*/.#ifdef
11895 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
11896 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
11897 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b 0a e3_os_type = 0;.
11898 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e 74 #else.static int
11899 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 sqlite3_os_type
1189a 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 = 0;.#endif..#i
1189b 66 6e 64 65 66 20 53 59 53 43 41 4c 4c 0a 23 20 fndef SYSCALL.#
1189c 20 64 65 66 69 6e 65 20 53 59 53 43 41 4c 4c 20 define SYSCALL
1189d 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f sqlite3_syscall_
1189e 70 74 72 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ptr.#endif../*.*
1189f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
118a0 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 is not available
118a1 20 6f 6e 20 57 69 6e 64 6f 77 73 20 43 45 20 6f on Windows CE o
118a2 72 20 57 69 6e 52 54 2e 0a 20 2a 2f 0a 0a 23 69 r WinRT.. */..#i
118a3 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
118a4 45 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 E || SQLITE_OS_W
118a5 49 4e 52 54 0a 23 20 20 64 65 66 69 6e 65 20 6f INRT.# define o
118a6 73 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 sAreFileApisANSI
118a7 28 29 20 20 20 20 20 20 20 31 0a 23 65 6e 64 69 () 1.#endi
118a8 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 73 79 f../*.** Many sy
118a9 73 74 65 6d 20 63 61 6c 6c 73 20 61 72 65 20 61 stem calls are a
118aa 63 63 65 73 73 65 64 20 74 68 72 6f 75 67 68 20 ccessed through
118ab 70 6f 69 6e 74 65 72 2d 74 6f 2d 66 75 6e 63 74 pointer-to-funct
118ac 69 6f 6e 73 20 73 6f 20 74 68 61 74 0a 2a 2a 20 ions so that.**
118ad 74 68 65 79 20 6d 61 79 20 62 65 20 6f 76 65 72 they may be over
118ae 72 69 64 64 65 6e 20 61 74 20 72 75 6e 74 69 6d ridden at runtim
118af 65 20 74 6f 20 66 61 63 69 6c 69 74 61 74 65 20 e to facilitate
118b0 66 61 75 6c 74 20 69 6e 6a 65 63 74 69 6f 6e 20 fault injection
118b1 64 75 72 69 6e 67 0a 2a 2a 20 74 65 73 74 69 6e during.** testin
118b2 67 20 61 6e 64 20 73 61 6e 64 62 6f 78 69 6e 67 g and sandboxing
118b3 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
118b4 20 61 72 72 61 79 20 68 6f 6c 64 73 20 74 68 65 array holds the
118b5 20 6e 61 6d 65 73 20 61 6e 64 20 70 6f 69 6e 74 names and point
118b6 65 72 73 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6f 76 ers.** to all ov
118b7 65 72 72 69 64 65 61 62 6c 65 20 73 79 73 74 65 errideable syste
118b8 6d 20 63 61 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 m calls..*/.stat
118b9 69 63 20 73 74 72 75 63 74 20 77 69 6e 5f 73 79 ic struct win_sy
118ba 73 63 61 6c 6c 20 7b 0a 20 20 63 6f 6e 73 74 20 scall {. const
118bb 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
118bc 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
118bd 6f 66 20 74 68 65 20 73 79 74 65 6d 20 63 61 6c of the sytem cal
118be 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 l */. sqlite3_s
118bf 79 73 63 61 6c 6c 5f 70 74 72 20 70 43 75 72 72 yscall_ptr pCurr
118c0 65 6e 74 3b 20 2f 2a 20 43 75 72 72 65 6e 74 20 ent; /* Current
118c1 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73 79 73 value of the sys
118c2 74 65 6d 20 63 61 6c 6c 20 2a 2f 0a 20 20 73 71 tem call */. sq
118c3 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 lite3_syscall_pt
118c4 72 20 70 44 65 66 61 75 6c 74 3b 20 2f 2a 20 44 r pDefault; /* D
118c5 65 66 61 75 6c 74 20 76 61 6c 75 65 20 2a 2f 0a efault value */.
118c6 7d 20 61 53 79 73 63 61 6c 6c 5b 5d 20 3d 20 7b } aSyscall[] = {
118c7 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f .#if !SQLITE_OS_
118c8 57 49 4e 43 45 20 26 26 20 21 53 51 4c 49 54 45 WINCE && !SQLITE
118c9 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 41 _OS_WINRT. { "A
118ca 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 22 2c reFileApisANSI",
118cb 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c (SYSCAL
118cc 4c 29 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 L)AreFileApisANS
118cd 49 2c 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a I, 0 },.
118ce 23 65 6c 73 65 0a 20 20 7b 20 22 41 72 65 46 69 #else. { "AreFi
118cf 6c 65 41 70 69 73 41 4e 53 49 22 2c 20 20 20 20 leApisANSI",
118d0 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c (SYSCALL)0,
118d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118d2 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 0 },.#end
118d3 69 66 0a 0a 23 69 66 6e 64 65 66 20 6f 73 41 72 if..#ifndef osAr
118d4 65 46 69 6c 65 41 70 69 73 41 4e 53 49 0a 23 64 eFileApisANSI.#d
118d5 65 66 69 6e 65 20 6f 73 41 72 65 46 69 6c 65 41 efine osAreFileA
118d6 70 69 73 41 4e 53 49 20 28 28 42 4f 4f 4c 28 57 pisANSI ((BOOL(W
118d7 49 4e 41 50 49 2a 29 28 56 4f 49 44 29 29 61 53 INAPI*)(VOID))aS
118d8 79 73 63 61 6c 6c 5b 30 5d 2e 70 43 75 72 72 65 yscall[0].pCurre
118d9 6e 74 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 nt).#endif..#if
118da 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 SQLITE_OS_WINCE
118db 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 && defined(SQLIT
118dc 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 E_WIN32_HAS_WIDE
118dd 29 0a 20 20 7b 20 22 43 68 61 72 4c 6f 77 65 72 ). { "CharLower
118de 57 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 W",
118df 20 28 53 59 53 43 41 4c 4c 29 43 68 61 72 4c 6f (SYSCALL)CharLo
118e0 77 65 72 57 2c 20 20 20 20 20 20 20 20 20 20 20 werW,
118e1 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 0 },.#else.
118e2 7b 20 22 43 68 61 72 4c 6f 77 65 72 57 22 2c 20 { "CharLowerW",
118e3 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 (SY
118e4 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 SCALL)0,
118e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
118e6 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 },.#endif..#def
118e7 69 6e 65 20 6f 73 43 68 61 72 4c 6f 77 65 72 57 ine osCharLowerW
118e8 20 28 28 4c 50 57 53 54 52 28 57 49 4e 41 50 49 ((LPWSTR(WINAPI
118e9 2a 29 28 4c 50 57 53 54 52 29 29 61 53 79 73 63 *)(LPWSTR))aSysc
118ea 61 6c 6c 5b 31 5d 2e 70 43 75 72 72 65 6e 74 29 all[1].pCurrent)
118eb 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ..#if SQLITE_OS_
118ec 57 49 4e 43 45 20 26 26 20 64 65 66 69 6e 65 64 WINCE && defined
118ed 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 (SQLITE_WIN32_HA
118ee 53 5f 57 49 44 45 29 0a 20 20 7b 20 22 43 68 61 S_WIDE). { "Cha
118ef 72 55 70 70 65 72 57 22 2c 20 20 20 20 20 20 20 rUpperW",
118f0 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 (SYSCALL)
118f1 43 68 61 72 55 70 70 65 72 57 2c 20 20 20 20 20 CharUpperW,
118f2 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 0 },.#e
118f3 6c 73 65 0a 20 20 7b 20 22 43 68 61 72 55 70 70 lse. { "CharUpp
118f4 65 72 57 22 2c 20 20 20 20 20 20 20 20 20 20 20 erW",
118f5 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 (SYSCALL)0,
118f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118f7 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0 },.#endif
118f8 0a 0a 23 64 65 66 69 6e 65 20 6f 73 43 68 61 72 ..#define osChar
118f9 55 70 70 65 72 57 20 28 28 4c 50 57 53 54 52 28 UpperW ((LPWSTR(
118fa 57 49 4e 41 50 49 2a 29 28 4c 50 57 53 54 52 29 WINAPI*)(LPWSTR)
118fb 29 61 53 79 73 63 61 6c 6c 5b 32 5d 2e 70 43 75 )aSyscall[2].pCu
118fc 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 43 6c 6f rrent).. { "Clo
118fd 73 65 48 61 6e 64 6c 65 22 2c 20 20 20 20 20 20 seHandle",
118fe 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 (SYSCALL)
118ff 43 6c 6f 73 65 48 61 6e 64 6c 65 2c 20 20 20 20 CloseHandle,
11900 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 0a 23 0 },..#
11901 64 65 66 69 6e 65 20 6f 73 43 6c 6f 73 65 48 61 define osCloseHa
11902 6e 64 6c 65 20 28 28 42 4f 4f 4c 28 57 49 4e 41 ndle ((BOOL(WINA
11903 50 49 2a 29 28 48 41 4e 44 4c 45 29 29 61 53 79 PI*)(HANDLE))aSy
11904 73 63 61 6c 6c 5b 33 5d 2e 70 43 75 72 72 65 6e scall[3].pCurren
11905 74 29 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 t)..#if defined(
11906 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 SQLITE_WIN32_HAS
11907 5f 41 4e 53 49 29 0a 20 20 7b 20 22 43 72 65 61 _ANSI). { "Crea
11908 74 65 46 69 6c 65 41 22 2c 20 20 20 20 20 20 20 teFileA",
11909 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 43 (SYSCALL)C
1190a 72 65 61 74 65 46 69 6c 65 41 2c 20 20 20 20 20 reateFileA,
1190b 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6c 0 },.#el
1190c 73 65 0a 20 20 7b 20 22 43 72 65 61 74 65 46 69 se. { "CreateFi
1190d 6c 65 41 22 2c 20 20 20 20 20 20 20 20 20 20 20 leA",
1190e 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 (SYSCALL)0,
1190f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11910 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0 },.#endif.
11911 0a 23 64 65 66 69 6e 65 20 6f 73 43 72 65 61 74 .#define osCreat
11912 65 46 69 6c 65 41 20 28 28 48 41 4e 44 4c 45 28 eFileA ((HANDLE(
11913 57 49 4e 41 50 49 2a 29 28 4c 50 43 53 54 52 2c WINAPI*)(LPCSTR,
11914 44 57 4f 52 44 2c 44 57 4f 52 44 2c 20 5c 0a 20 DWORD,DWORD, \.
11915 20 20 20 20 20 20 20 4c 50 53 45 43 55 52 49 54 LPSECURIT
11916 59 5f 41 54 54 52 49 42 55 54 45 53 2c 44 57 4f Y_ATTRIBUTES,DWO
11917 52 44 2c 44 57 4f 52 44 2c 48 41 4e 44 4c 45 29 RD,DWORD,HANDLE)
11918 29 61 53 79 73 63 61 6c 6c 5b 34 5d 2e 70 43 75 )aSyscall[4].pCu
11919 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c rrent)..#if !SQL
1191a 49 54 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26 20 ITE_OS_WINRT &&
1191b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 defined(SQLITE_W
1191c 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a 20 IN32_HAS_WIDE).
1191d 20 7b 20 22 43 72 65 61 74 65 46 69 6c 65 57 22 { "CreateFileW"
1191e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 , (S
1191f 59 53 43 41 4c 4c 29 43 72 65 61 74 65 46 69 6c YSCALL)CreateFil
11920 65 57 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 eW,
11921 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 0 },.#else. { "
11922 43 72 65 61 74 65 46 69 6c 65 57 22 2c 20 20 20 CreateFileW",
11923 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 (SYSCA
11924 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 LL)0,
11925 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0 },
11926 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 .#endif..#define
11927 20 6f 73 43 72 65 61 74 65 46 69 6c 65 57 20 28 osCreateFileW (
11928 28 48 41 4e 44 4c 45 28 57 49 4e 41 50 49 2a 29 (HANDLE(WINAPI*)
11929 28 4c 50 43 57 53 54 52 2c 44 57 4f 52 44 2c 44 (LPCWSTR,DWORD,D
1192a 57 4f 52 44 2c 20 5c 0a 20 20 20 20 20 20 20 20 WORD, \.
1192b 4c 50 53 45 43 55 52 49 54 59 5f 41 54 54 52 49 LPSECURITY_ATTRI
1192c 42 55 54 45 53 2c 44 57 4f 52 44 2c 44 57 4f 52 BUTES,DWORD,DWOR
1192d 44 2c 48 41 4e 44 4c 45 29 29 61 53 79 73 63 61 D,HANDLE))aSysca
1192e 6c 6c 5b 35 5d 2e 70 43 75 72 72 65 6e 74 29 0a ll[5].pCurrent).
1192f 0a 23 69 66 20 28 21 53 51 4c 49 54 45 5f 4f 53 .#if (!SQLITE_OS
11930 5f 57 49 4e 52 54 20 26 26 20 64 65 66 69 6e 65 _WINRT && define
11931 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 d(SQLITE_WIN32_H
11932 41 53 5f 41 4e 53 49 29 20 26 26 20 5c 0a 20 20 AS_ANSI) && \.
11933 20 20 20 20 20 20 21 64 65 66 69 6e 65 64 28 53 !defined(S
11934 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 29 29 QLITE_OMIT_WAL))
11935 0a 20 20 7b 20 22 43 72 65 61 74 65 46 69 6c 65 . { "CreateFile
11936 4d 61 70 70 69 6e 67 41 22 2c 20 20 20 20 20 20 MappingA",
11937 28 53 59 53 43 41 4c 4c 29 43 72 65 61 74 65 46 (SYSCALL)CreateF
11938 69 6c 65 4d 61 70 70 69 6e 67 41 2c 20 20 20 20 ileMappingA,
11939 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 0 },.#else. {
1193a 20 22 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 "CreateFileMapp
1193b 69 6e 67 41 22 2c 20 20 20 20 20 20 28 53 59 53 ingA", (SYS
1193c 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 CALL)0,
1193d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 0
1193e 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 },.#endif..#defi
1193f 6e 65 20 6f 73 43 72 65 61 74 65 46 69 6c 65 4d ne osCreateFileM
11940 61 70 70 69 6e 67 41 20 28 28 48 41 4e 44 4c 45 appingA ((HANDLE
11941 28 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 (WINAPI*)(HANDLE
11942 2c 4c 50 53 45 43 55 52 49 54 59 5f 41 54 54 52 ,LPSECURITY_ATTR
11943 49 42 55 54 45 53 2c 20 5c 0a 20 20 20 20 20 20 IBUTES, \.
11944 20 20 44 57 4f 52 44 2c 44 57 4f 52 44 2c 44 57 DWORD,DWORD,DW
11945 4f 52 44 2c 4c 50 43 53 54 52 29 29 61 53 79 73 ORD,LPCSTR))aSys
11946 63 61 6c 6c 5b 36 5d 2e 70 43 75 72 72 65 6e 74 call[6].pCurrent
11947 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 )..#if SQLITE_OS
11948 5f 57 49 4e 43 45 20 7c 7c 20 28 21 53 51 4c 49 _WINCE || (!SQLI
11949 54 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26 20 64 TE_OS_WINRT && d
1194a 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 efined(SQLITE_WI
1194b 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 20 26 26 N32_HAS_WIDE) &&
1194c 20 5c 0a 20 20 20 20 20 20 20 20 21 64 65 66 69 \. !defi
1194d 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
1194e 57 41 4c 29 29 0a 20 20 7b 20 22 43 72 65 61 74 WAL)). { "Creat
1194f 65 46 69 6c 65 4d 61 70 70 69 6e 67 57 22 2c 20 eFileMappingW",
11950 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 43 72 (SYSCALL)Cr
11951 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 57 eateFileMappingW
11952 2c 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 , 0 },.#els
11953 65 0a 20 20 7b 20 22 43 72 65 61 74 65 46 69 6c e. { "CreateFil
11954 65 4d 61 70 70 69 6e 67 57 22 2c 20 20 20 20 20 eMappingW",
11955 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 (SYSCALL)0,
11956 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11957 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 0 },.#endif..
11958 23 64 65 66 69 6e 65 20 6f 73 43 72 65 61 74 65 #define osCreate
11959 46 69 6c 65 4d 61 70 70 69 6e 67 57 20 28 28 48 FileMappingW ((H
1195a 41 4e 44 4c 45 28 57 49 4e 41 50 49 2a 29 28 48 ANDLE(WINAPI*)(H
1195b 41 4e 44 4c 45 2c 4c 50 53 45 43 55 52 49 54 59 ANDLE,LPSECURITY
1195c 5f 41 54 54 52 49 42 55 54 45 53 2c 20 5c 0a 20 _ATTRIBUTES, \.
1195d 20 20 20 20 20 20 20 44 57 4f 52 44 2c 44 57 4f DWORD,DWO
1195e 52 44 2c 44 57 4f 52 44 2c 4c 50 43 57 53 54 52 RD,DWORD,LPCWSTR
1195f 29 29 61 53 79 73 63 61 6c 6c 5b 37 5d 2e 70 43 ))aSyscall[7].pC
11960 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 urrent)..#if !SQ
11961 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26 LITE_OS_WINRT &&
11962 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
11963 57 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a WIN32_HAS_WIDE).
11964 20 20 7b 20 22 43 72 65 61 74 65 4d 75 74 65 78 { "CreateMutex
11965 57 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 28 W", (
11966 53 59 53 43 41 4c 4c 29 43 72 65 61 74 65 4d 75 SYSCALL)CreateMu
11967 74 65 78 57 2c 20 20 20 20 20 20 20 20 20 20 20 texW,
11968 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 0 },.#else. {
11969 22 43 72 65 61 74 65 4d 75 74 65 78 57 22 2c 20 "CreateMutexW",
1196a 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 (SYSC
1196b 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 ALL)0,
1196c 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 0 }
1196d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e ,.#endif..#defin
1196e 65 20 6f 73 43 72 65 61 74 65 4d 75 74 65 78 57 e osCreateMutexW
1196f 20 28 28 48 41 4e 44 4c 45 28 57 49 4e 41 50 49 ((HANDLE(WINAPI
11970 2a 29 28 4c 50 53 45 43 55 52 49 54 59 5f 41 54 *)(LPSECURITY_AT
11971 54 52 49 42 55 54 45 53 2c 42 4f 4f 4c 2c 20 5c TRIBUTES,BOOL, \
11972 0a 20 20 20 20 20 20 20 20 4c 50 43 57 53 54 52 . LPCWSTR
11973 29 29 61 53 79 73 63 61 6c 6c 5b 38 5d 2e 70 43 ))aSyscall[8].pC
11974 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 64 65 66 urrent)..#if def
11975 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 ined(SQLITE_WIN3
11976 32 5f 48 41 53 5f 41 4e 53 49 29 0a 20 20 7b 20 2_HAS_ANSI). {
11977 22 44 65 6c 65 74 65 46 69 6c 65 41 22 2c 20 20 "DeleteFileA",
11978 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 (SYSC
11979 41 4c 4c 29 44 65 6c 65 74 65 46 69 6c 65 41 2c ALL)DeleteFileA,
1197a 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 0 }
1197b 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 44 65 6c ,.#else. { "Del
1197c 65 74 65 46 69 6c 65 41 22 2c 20 20 20 20 20 20 eteFileA",
1197d 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 (SYSCALL)
1197e 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1197f 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 0 },.#e
11980 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 ndif..#define os
11981 44 65 6c 65 74 65 46 69 6c 65 41 20 28 28 42 4f DeleteFileA ((BO
11982 4f 4c 28 57 49 4e 41 50 49 2a 29 28 4c 50 43 53 OL(WINAPI*)(LPCS
11983 54 52 29 29 61 53 79 73 63 61 6c 6c 5b 39 5d 2e TR))aSyscall[9].
11984 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 64 pCurrent)..#if d
11985 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 efined(SQLITE_WI
11986 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a 20 20 N32_HAS_WIDE).
11987 7b 20 22 44 65 6c 65 74 65 46 69 6c 65 57 22 2c { "DeleteFileW",
11988 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 (SY
11989 53 43 41 4c 4c 29 44 65 6c 65 74 65 46 69 6c 65 SCALL)DeleteFile
1198a 57 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 30 W, 0
1198b 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 44 },.#else. { "D
1198c 65 6c 65 74 65 46 69 6c 65 57 22 2c 20 20 20 20 eleteFileW",
1198d 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c (SYSCAL
1198e 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 L)0,
1198f 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 0 },.
11990 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 #endif..#define
11991 6f 73 44 65 6c 65 74 65 46 69 6c 65 57 20 28 28 osDeleteFileW ((
11992 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 4c 50 BOOL(WINAPI*)(LP
11993 43 57 53 54 52 29 29 61 53 79 73 63 61 6c 6c 5b CWSTR))aSyscall[
11994 31 30 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 10].pCurrent)..#
11995 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
11996 43 45 0a 20 20 7b 20 22 46 69 6c 65 54 69 6d 65 CE. { "FileTime
11997 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 22 ToLocalFileTime"
11998 2c 20 28 53 59 53 43 41 4c 4c 29 46 69 6c 65 54 , (SYSCALL)FileT
11999 69 6d 65 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 imeToLocalFileTi
1199a 6d 65 2c 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 me, 0 },.#else.
1199b 20 7b 20 22 46 69 6c 65 54 69 6d 65 54 6f 4c 6f { "FileTimeToLo
1199c 63 61 6c 46 69 6c 65 54 69 6d 65 22 2c 20 28 53 calFileTime", (S
1199d 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 YSCALL)0,
1199e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1199f 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 0 },.#endif..#de
119a0 66 69 6e 65 20 6f 73 46 69 6c 65 54 69 6d 65 54 fine osFileTimeT
119a1 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 20 28 oLocalFileTime (
119a2 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 43 (BOOL(WINAPI*)(C
119a3 4f 4e 53 54 20 46 49 4c 45 54 49 4d 45 2a 2c 20 ONST FILETIME*,
119a4 5c 0a 20 20 20 20 20 20 20 20 4c 50 46 49 4c 45 \. LPFILE
119a5 54 49 4d 45 29 29 61 53 79 73 63 61 6c 6c 5b 31 TIME))aSyscall[1
119a6 31 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 1].pCurrent)..#i
119a7 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
119a8 45 0a 20 20 7b 20 22 46 69 6c 65 54 69 6d 65 54 E. { "FileTimeT
119a9 6f 53 79 73 74 65 6d 54 69 6d 65 22 2c 20 20 20 oSystemTime",
119aa 20 28 53 59 53 43 41 4c 4c 29 46 69 6c 65 54 69 (SYSCALL)FileTi
119ab 6d 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 2c 20 meToSystemTime,
119ac 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 0 },.#else.
119ad 7b 20 22 46 69 6c 65 54 69 6d 65 54 6f 53 79 73 { "FileTimeToSys
119ae 74 65 6d 54 69 6d 65 22 2c 20 20 20 20 28 53 59 temTime", (SY
119af 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 SCALL)0,
119b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
119b1 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 },.#endif..#def
119b2 69 6e 65 20 6f 73 46 69 6c 65 54 69 6d 65 54 6f ine osFileTimeTo
119b3 53 79 73 74 65 6d 54 69 6d 65 20 28 28 42 4f 4f SystemTime ((BOO
119b4 4c 28 57 49 4e 41 50 49 2a 29 28 43 4f 4e 53 54 L(WINAPI*)(CONST
119b5 20 46 49 4c 45 54 49 4d 45 2a 2c 20 5c 0a 20 20 FILETIME*, \.
119b6 20 20 20 20 20 20 4c 50 53 59 53 54 45 4d 54 49 LPSYSTEMTI
119b7 4d 45 29 29 61 53 79 73 63 61 6c 6c 5b 31 32 5d ME))aSyscall[12]
119b8 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 .pCurrent).. {
119b9 22 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72 "FlushFileBuffer
119ba 73 22 2c 20 20 20 20 20 20 20 20 28 53 59 53 43 s", (SYSC
119bb 41 4c 4c 29 46 6c 75 73 68 46 69 6c 65 42 75 66 ALL)FlushFileBuf
119bc 66 65 72 73 2c 20 20 20 20 20 20 20 20 30 20 7d fers, 0 }
119bd 2c 0a 0a 23 64 65 66 69 6e 65 20 6f 73 46 6c 75 ,..#define osFlu
119be 73 68 46 69 6c 65 42 75 66 66 65 72 73 20 28 28 shFileBuffers ((
119bf 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41 BOOL(WINAPI*)(HA
119c0 4e 44 4c 45 29 29 61 53 79 73 63 61 6c 6c 5b 31 NDLE))aSyscall[1
119c1 33 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 3].pCurrent)..#i
119c2 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
119c3 5f 57 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 29 _WIN32_HAS_ANSI)
119c4 0a 20 20 7b 20 22 46 6f 72 6d 61 74 4d 65 73 73 . { "FormatMess
119c5 61 67 65 41 22 2c 20 20 20 20 20 20 20 20 20 20 ageA",
119c6 28 53 59 53 43 41 4c 4c 29 46 6f 72 6d 61 74 4d (SYSCALL)FormatM
119c7 65 73 73 61 67 65 41 2c 20 20 20 20 20 20 20 20 essageA,
119c8 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 0 },.#else. {
119c9 20 22 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 41 "FormatMessageA
119ca 22 2c 20 20 20 20 20 20 20 20 20 20 28 53 59 53 ", (SYS
119cb 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 CALL)0,
119cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 0
119cd 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 },.#endif..#defi
119ce 6e 65 20 6f 73 46 6f 72 6d 61 74 4d 65 73 73 61 ne osFormatMessa
119cf 67 65 41 20 28 28 44 57 4f 52 44 28 57 49 4e 41 geA ((DWORD(WINA
119d0 50 49 2a 29 28 44 57 4f 52 44 2c 4c 50 43 56 4f PI*)(DWORD,LPCVO
119d1 49 44 2c 44 57 4f 52 44 2c 44 57 4f 52 44 2c 4c ID,DWORD,DWORD,L
119d2 50 53 54 52 2c 20 5c 0a 20 20 20 20 20 20 20 20 PSTR, \.
119d3 44 57 4f 52 44 2c 76 61 5f 6c 69 73 74 2a 29 29 DWORD,va_list*))
119d4 61 53 79 73 63 61 6c 6c 5b 31 34 5d 2e 70 43 75 aSyscall[14].pCu
119d5 72 72 65 6e 74 29 0a 0a 23 69 66 20 64 65 66 69 rrent)..#if defi
119d6 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 ned(SQLITE_WIN32
119d7 5f 48 41 53 5f 57 49 44 45 29 0a 20 20 7b 20 22 _HAS_WIDE). { "
119d8 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 57 22 2c FormatMessageW",
119d9 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 (SYSCA
119da 4c 4c 29 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 LL)FormatMessage
119db 57 2c 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c W, 0 },
119dc 0a 23 65 6c 73 65 0a 20 20 7b 20 22 46 6f 72 6d .#else. { "Form
119dd 61 74 4d 65 73 73 61 67 65 57 22 2c 20 20 20 20 atMessageW",
119de 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 (SYSCALL)0
119df 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
119e0 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 0 },.#en
119e1 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 46 dif..#define osF
119e2 6f 72 6d 61 74 4d 65 73 73 61 67 65 57 20 28 28 ormatMessageW ((
119e3 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 28 44 DWORD(WINAPI*)(D
119e4 57 4f 52 44 2c 4c 50 43 56 4f 49 44 2c 44 57 4f WORD,LPCVOID,DWO
119e5 52 44 2c 44 57 4f 52 44 2c 4c 50 57 53 54 52 2c RD,DWORD,LPWSTR,
119e6 20 5c 0a 20 20 20 20 20 20 20 20 44 57 4f 52 44 \. DWORD
119e7 2c 76 61 5f 6c 69 73 74 2a 29 29 61 53 79 73 63 ,va_list*))aSysc
119e8 61 6c 6c 5b 31 35 5d 2e 70 43 75 72 72 65 6e 74 all[15].pCurrent
119e9 29 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 )..#if !defined(
119ea 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 SQLITE_OMIT_LOAD
119eb 5f 45 58 54 45 4e 53 49 4f 4e 29 0a 20 20 7b 20 _EXTENSION). {
119ec 22 46 72 65 65 4c 69 62 72 61 72 79 22 2c 20 20 "FreeLibrary",
119ed 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 (SYSC
119ee 41 4c 4c 29 46 72 65 65 4c 69 62 72 61 72 79 2c ALL)FreeLibrary,
119ef 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 0 }
119f0 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 46 72 65 ,.#else. { "Fre
119f1 65 4c 69 62 72 61 72 79 22 2c 20 20 20 20 20 20 eLibrary",
119f2 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 (SYSCALL)
119f3 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
119f4 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 0 },.#e
119f5 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 ndif..#define os
119f6 46 72 65 65 4c 69 62 72 61 72 79 20 28 28 42 4f FreeLibrary ((BO
119f7 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 4d 4f 44 OL(WINAPI*)(HMOD
119f8 55 4c 45 29 29 61 53 79 73 63 61 6c 6c 5b 31 36 ULE))aSyscall[16
119f9 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b ].pCurrent).. {
119fa 20 22 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63 "GetCurrentProc
119fb 65 73 73 49 64 22 2c 20 20 20 20 20 28 53 59 53 essId", (SYS
119fc 43 41 4c 4c 29 47 65 74 43 75 72 72 65 6e 74 50 CALL)GetCurrentP
119fd 72 6f 63 65 73 73 49 64 2c 20 20 20 20 20 30 20 rocessId, 0
119fe 7d 2c 0a 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 },..#define osGe
119ff 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73 73 49 tCurrentProcessI
11a00 64 20 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 d ((DWORD(WINAPI
11a01 2a 29 28 56 4f 49 44 29 29 61 53 79 73 63 61 6c *)(VOID))aSyscal
11a02 6c 5b 31 37 5d 2e 70 43 75 72 72 65 6e 74 29 0a l[17].pCurrent).
11a03 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f .#if !SQLITE_OS_
11a04 57 49 4e 43 45 20 26 26 20 64 65 66 69 6e 65 64 WINCE && defined
11a05 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 (SQLITE_WIN32_HA
11a06 53 5f 41 4e 53 49 29 0a 20 20 7b 20 22 47 65 74 S_ANSI). { "Get
11a07 44 69 73 6b 46 72 65 65 53 70 61 63 65 41 22 2c DiskFreeSpaceA",
11a08 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 (SYSCALL)
11a09 47 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 GetDiskFreeSpace
11a0a 41 2c 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 A, 0 },.#e
11a0b 6c 73 65 0a 20 20 7b 20 22 47 65 74 44 69 73 6b lse. { "GetDisk
11a0c 46 72 65 65 53 70 61 63 65 41 22 2c 20 20 20 20 FreeSpaceA",
11a0d 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 (SYSCALL)0,
11a0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a0f 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0 },.#endif
11a10 0a 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 74 44 ..#define osGetD
11a11 69 73 6b 46 72 65 65 53 70 61 63 65 41 20 28 28 iskFreeSpaceA ((
11a12 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 4c 50 BOOL(WINAPI*)(LP
11a13 43 53 54 52 2c 4c 50 44 57 4f 52 44 2c 4c 50 44 CSTR,LPDWORD,LPD
11a14 57 4f 52 44 2c 4c 50 44 57 4f 52 44 2c 20 5c 0a WORD,LPDWORD, \.
11a15 20 20 20 20 20 20 20 20 4c 50 44 57 4f 52 44 29 LPDWORD)
11a16 29 61 53 79 73 63 61 6c 6c 5b 31 38 5d 2e 70 43 )aSyscall[18].pC
11a17 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 urrent)..#if !SQ
11a18 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26 LITE_OS_WINCE &&
11a19 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 !SQLITE_OS_WINR
11a1a 54 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c T && defined(SQL
11a1b 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49 ITE_WIN32_HAS_WI
11a1c 44 45 29 0a 20 20 7b 20 22 47 65 74 44 69 73 6b DE). { "GetDisk
11a1d 46 72 65 65 53 70 61 63 65 57 22 2c 20 20 20 20 FreeSpaceW",
11a1e 20 20 20 28 53 59 53 43 41 4c 4c 29 47 65 74 44 (SYSCALL)GetD
11a1f 69 73 6b 46 72 65 65 53 70 61 63 65 57 2c 20 20 iskFreeSpaceW,
11a20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 0 },.#else.
11a21 20 20 7b 20 22 47 65 74 44 69 73 6b 46 72 65 65 { "GetDiskFree
11a22 53 70 61 63 65 57 22 2c 20 20 20 20 20 20 20 28 SpaceW", (
11a23 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 SYSCALL)0,
11a24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a25 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 0 },.#endif..#d
11a26 65 66 69 6e 65 20 6f 73 47 65 74 44 69 73 6b 46 efine osGetDiskF
11a27 72 65 65 53 70 61 63 65 57 20 28 28 42 4f 4f 4c reeSpaceW ((BOOL
11a28 28 57 49 4e 41 50 49 2a 29 28 4c 50 43 57 53 54 (WINAPI*)(LPCWST
11a29 52 2c 4c 50 44 57 4f 52 44 2c 4c 50 44 57 4f 52 R,LPDWORD,LPDWOR
11a2a 44 2c 4c 50 44 57 4f 52 44 2c 20 5c 0a 20 20 20 D,LPDWORD, \.
11a2b 20 20 20 20 20 4c 50 44 57 4f 52 44 29 29 61 53 LPDWORD))aS
11a2c 79 73 63 61 6c 6c 5b 31 39 5d 2e 70 43 75 72 72 yscall[19].pCurr
11a2d 65 6e 74 29 0a 0a 23 69 66 20 64 65 66 69 6e 65 ent)..#if define
11a2e 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 d(SQLITE_WIN32_H
11a2f 41 53 5f 41 4e 53 49 29 0a 20 20 7b 20 22 47 65 AS_ANSI). { "Ge
11a30 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 41 tFileAttributesA
11a31 22 2c 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c ", (SYSCALL
11a32 29 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 )GetFileAttribut
11a33 65 73 41 2c 20 20 20 20 20 20 30 20 7d 2c 0a 23 esA, 0 },.#
11a34 65 6c 73 65 0a 20 20 7b 20 22 47 65 74 46 69 6c else. { "GetFil
11a35 65 41 74 74 72 69 62 75 74 65 73 41 22 2c 20 20 eAttributesA",
11a36 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 (SYSCALL)0,
11a37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a38 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 0 },.#endi
11a39 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 74 f..#define osGet
11a3a 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 41 20 FileAttributesA
11a3b 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 ((DWORD(WINAPI*)
11a3c 28 4c 50 43 53 54 52 29 29 61 53 79 73 63 61 6c (LPCSTR))aSyscal
11a3d 6c 5b 32 30 5d 2e 70 43 75 72 72 65 6e 74 29 0a l[20].pCurrent).
11a3e 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f .#if !SQLITE_OS_
11a3f 57 49 4e 52 54 20 26 26 20 64 65 66 69 6e 65 64 WINRT && defined
11a40 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 (SQLITE_WIN32_HA
11a41 53 5f 57 49 44 45 29 0a 20 20 7b 20 22 47 65 74 S_WIDE). { "Get
11a42 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 22 FileAttributesW"
11a43 2c 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 , (SYSCALL)
11a44 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 GetFileAttribute
11a45 73 57 2c 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 sW, 0 },.#e
11a46 6c 73 65 0a 20 20 7b 20 22 47 65 74 46 69 6c 65 lse. { "GetFile
11a47 41 74 74 72 69 62 75 74 65 73 57 22 2c 20 20 20 AttributesW",
11a48 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 (SYSCALL)0,
11a49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a4a 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0 },.#endif
11a4b 0a 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 74 46 ..#define osGetF
11a4c 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 20 28 ileAttributesW (
11a4d 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 28 (DWORD(WINAPI*)(
11a4e 4c 50 43 57 53 54 52 29 29 61 53 79 73 63 61 6c LPCWSTR))aSyscal
11a4f 6c 5b 32 31 5d 2e 70 43 75 72 72 65 6e 74 29 0a l[21].pCurrent).
11a50 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
11a51 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49 ITE_WIN32_HAS_WI
11a52 44 45 29 0a 20 20 7b 20 22 47 65 74 46 69 6c 65 DE). { "GetFile
11a53 41 74 74 72 69 62 75 74 65 73 45 78 57 22 2c 20 AttributesExW",
11a54 20 20 20 28 53 59 53 43 41 4c 4c 29 47 65 74 46 (SYSCALL)GetF
11a55 69 6c 65 41 74 74 72 69 62 75 74 65 73 45 78 57 ileAttributesExW
11a56 2c 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a , 0 },.#else.
11a57 20 20 7b 20 22 47 65 74 46 69 6c 65 41 74 74 72 { "GetFileAttr
11a58 69 62 75 74 65 73 45 78 57 22 2c 20 20 20 20 28 ibutesExW", (
11a59 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 SYSCALL)0,
11a5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a5b 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 0 },.#endif..#d
11a5c 65 66 69 6e 65 20 6f 73 47 65 74 46 69 6c 65 41 efine osGetFileA
11a5d 74 74 72 69 62 75 74 65 73 45 78 57 20 28 28 42 ttributesExW ((B
11a5e 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 4c 50 43 OOL(WINAPI*)(LPC
11a5f 57 53 54 52 2c 47 45 54 5f 46 49 4c 45 45 58 5f WSTR,GET_FILEEX_
11a60 49 4e 46 4f 5f 4c 45 56 45 4c 53 2c 20 5c 0a 20 INFO_LEVELS, \.
11a61 20 20 20 20 20 20 20 4c 50 56 4f 49 44 29 29 61 LPVOID))a
11a62 53 79 73 63 61 6c 6c 5b 32 32 5d 2e 70 43 75 72 Syscall[22].pCur
11a63 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c 49 rent)..#if !SQLI
11a64 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 7b 20 TE_OS_WINRT. {
11a65 22 47 65 74 46 69 6c 65 53 69 7a 65 22 2c 20 20 "GetFileSize",
11a66 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 (SYSC
11a67 41 4c 4c 29 47 65 74 46 69 6c 65 53 69 7a 65 2c ALL)GetFileSize,
11a68 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 0 }
11a69 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 47 65 74 ,.#else. { "Get
11a6a 46 69 6c 65 53 69 7a 65 22 2c 20 20 20 20 20 20 FileSize",
11a6b 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 (SYSCALL)
11a6c 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
11a6d 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 0 },.#e
11a6e 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 ndif..#define os
11a6f 47 65 74 46 69 6c 65 53 69 7a 65 20 28 28 44 57 GetFileSize ((DW
11a70 4f 52 44 28 57 49 4e 41 50 49 2a 29 28 48 41 4e ORD(WINAPI*)(HAN
11a71 44 4c 45 2c 4c 50 44 57 4f 52 44 29 29 61 53 79 DLE,LPDWORD))aSy
11a72 73 63 61 6c 6c 5b 32 33 5d 2e 70 43 75 72 72 65 scall[23].pCurre
11a73 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 nt)..#if !SQLITE
11a74 5f 4f 53 5f 57 49 4e 43 45 20 26 26 20 64 65 66 _OS_WINCE && def
11a75 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 ined(SQLITE_WIN3
11a76 32 5f 48 41 53 5f 41 4e 53 49 29 0a 20 20 7b 20 2_HAS_ANSI). {
11a77 22 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 "GetFullPathName
11a78 41 22 2c 20 20 20 20 20 20 20 20 28 53 59 53 43 A", (SYSC
11a79 41 4c 4c 29 47 65 74 46 75 6c 6c 50 61 74 68 4e ALL)GetFullPathN
11a7a 61 6d 65 41 2c 20 20 20 20 20 20 20 20 30 20 7d ameA, 0 }
11a7b 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 47 65 74 ,.#else. { "Get
11a7c 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 22 2c 20 FullPathNameA",
11a7d 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 (SYSCALL)
11a7e 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
11a7f 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 0 },.#e
11a80 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 ndif..#define os
11a81 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 GetFullPathNameA
11a82 20 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a ((DWORD(WINAPI*
11a83 29 28 4c 50 43 53 54 52 2c 44 57 4f 52 44 2c 4c )(LPCSTR,DWORD,L
11a84 50 53 54 52 2c 20 5c 0a 20 20 20 20 20 20 20 20 PSTR, \.
11a85 4c 50 53 54 52 2a 29 29 61 53 79 73 63 61 6c 6c LPSTR*))aSyscall
11a86 5b 32 34 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a [24].pCurrent)..
11a87 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 #if !SQLITE_OS_W
11a88 49 4e 43 45 20 26 26 20 21 53 51 4c 49 54 45 5f INCE && !SQLITE_
11a89 4f 53 5f 57 49 4e 52 54 20 26 26 20 64 65 66 69 OS_WINRT && defi
11a8a 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 ned(SQLITE_WIN32
11a8b 5f 48 41 53 5f 57 49 44 45 29 0a 20 20 7b 20 22 _HAS_WIDE). { "
11a8c 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 GetFullPathNameW
11a8d 22 2c 20 20 20 20 20 20 20 20 28 53 59 53 43 41 ", (SYSCA
11a8e 4c 4c 29 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 LL)GetFullPathNa
11a8f 6d 65 57 2c 20 20 20 20 20 20 20 20 30 20 7d 2c meW, 0 },
11a90 0a 23 65 6c 73 65 0a 20 20 7b 20 22 47 65 74 46 .#else. { "GetF
11a91 75 6c 6c 50 61 74 68 4e 61 6d 65 57 22 2c 20 20 ullPathNameW",
11a92 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 (SYSCALL)0
11a93 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
11a94 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 0 },.#en
11a95 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 47 dif..#define osG
11a96 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 20 etFullPathNameW
11a97 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 ((DWORD(WINAPI*)
11a98 28 4c 50 43 57 53 54 52 2c 44 57 4f 52 44 2c 4c (LPCWSTR,DWORD,L
11a99 50 57 53 54 52 2c 20 5c 0a 20 20 20 20 20 20 20 PWSTR, \.
11a9a 20 4c 50 57 53 54 52 2a 29 29 61 53 79 73 63 61 LPWSTR*))aSysca
11a9b 6c 6c 5b 32 35 5d 2e 70 43 75 72 72 65 6e 74 29 ll[25].pCurrent)
11a9c 0a 0a 20 20 7b 20 22 47 65 74 4c 61 73 74 45 72 .. { "GetLastEr
11a9d 72 6f 72 22 2c 20 20 20 20 20 20 20 20 20 20 20 ror",
11a9e 20 28 53 59 53 43 41 4c 4c 29 47 65 74 4c 61 73 (SYSCALL)GetLas
11a9f 74 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 tError,
11aa0 20 20 20 30 20 7d 2c 0a 0a 23 64 65 66 69 6e 65 0 },..#define
11aa1 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 20 osGetLastError
11aa2 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 ((DWORD(WINAPI*)
11aa3 28 56 4f 49 44 29 29 61 53 79 73 63 61 6c 6c 5b (VOID))aSyscall[
11aa4 32 36 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 26].pCurrent)..#
11aa5 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
11aa6 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
11aa7 45 4e 53 49 4f 4e 29 0a 23 69 66 20 53 51 4c 49 ENSION).#if SQLI
11aa8 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 2f 2a TE_OS_WINCE. /*
11aa9 20 54 68 65 20 47 65 74 50 72 6f 63 41 64 64 72 The GetProcAddr
11aaa 65 73 73 41 28 29 20 72 6f 75 74 69 6e 65 20 69 essA() routine i
11aab 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 s only available
11aac 20 6f 6e 20 57 69 6e 64 6f 77 73 20 43 45 2e 20 on Windows CE.
11aad 2a 2f 0a 20 20 7b 20 22 47 65 74 50 72 6f 63 41 */. { "GetProcA
11aae 64 64 72 65 73 73 41 22 2c 20 20 20 20 20 20 20 ddressA",
11aaf 20 20 28 53 59 53 43 41 4c 4c 29 47 65 74 50 72 (SYSCALL)GetPr
11ab0 6f 63 41 64 64 72 65 73 73 41 2c 20 20 20 20 20 ocAddressA,
11ab1 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 0 },.#else.
11ab2 20 2f 2a 20 41 6c 6c 20 6f 74 68 65 72 20 57 69 /* All other Wi
11ab3 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 20 ndows platforms
11ab4 65 78 70 65 63 74 20 47 65 74 50 72 6f 63 41 64 expect GetProcAd
11ab5 64 72 65 73 73 28 29 20 74 6f 20 74 61 6b 65 0a dress() to take.
11ab6 20 20 2a 2a 20 61 6e 20 41 4e 53 49 20 73 74 72 ** an ANSI str
11ab7 69 6e 67 20 72 65 67 61 72 64 6c 65 73 73 20 6f ing regardless o
11ab8 66 20 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 73 f the _UNICODE s
11ab9 65 74 74 69 6e 67 20 2a 2f 0a 20 20 7b 20 22 47 etting */. { "G
11aba 65 74 50 72 6f 63 41 64 64 72 65 73 73 41 22 2c etProcAddressA",
11abb 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c (SYSCAL
11abc 4c 29 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 L)GetProcAddress
11abd 2c 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a , 0 },.
11abe 23 65 6e 64 69 66 0a 23 65 6c 73 65 0a 20 20 7b #endif.#else. {
11abf 20 22 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 "GetProcAddress
11ac0 41 22 2c 20 20 20 20 20 20 20 20 20 28 53 59 53 A", (SYS
11ac1 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 CALL)0,
11ac2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 0
11ac3 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 },.#endif..#defi
11ac4 6e 65 20 6f 73 47 65 74 50 72 6f 63 41 64 64 72 ne osGetProcAddr
11ac5 65 73 73 41 20 28 28 46 41 52 50 52 4f 43 28 57 essA ((FARPROC(W
11ac6 49 4e 41 50 49 2a 29 28 48 4d 4f 44 55 4c 45 2c INAPI*)(HMODULE,
11ac7 20 5c 0a 20 20 20 20 20 20 20 20 4c 50 43 53 54 \. LPCST
11ac8 52 29 29 61 53 79 73 63 61 6c 6c 5b 32 37 5d 2e R))aSyscall[27].
11ac9 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 pCurrent)..#if !
11aca 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a SQLITE_OS_WINRT.
11acb 20 20 7b 20 22 47 65 74 53 79 73 74 65 6d 49 6e { "GetSystemIn
11acc 66 6f 22 2c 20 20 20 20 20 20 20 20 20 20 20 28 fo", (
11acd 53 59 53 43 41 4c 4c 29 47 65 74 53 79 73 74 65 SYSCALL)GetSyste
11ace 6d 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 mInfo,
11acf 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 0 },.#else. {
11ad0 22 47 65 74 53 79 73 74 65 6d 49 6e 66 6f 22 2c "GetSystemInfo",
11ad1 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 (SYSC
11ad2 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 ALL)0,
11ad3 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 0 }
11ad4 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e ,.#endif..#defin
11ad5 65 20 6f 73 47 65 74 53 79 73 74 65 6d 49 6e 66 e osGetSystemInf
11ad6 6f 20 28 28 56 4f 49 44 28 57 49 4e 41 50 49 2a o ((VOID(WINAPI*
11ad7 29 28 4c 50 53 59 53 54 45 4d 5f 49 4e 46 4f 29 )(LPSYSTEM_INFO)
11ad8 29 61 53 79 73 63 61 6c 6c 5b 32 38 5d 2e 70 43 )aSyscall[28].pC
11ad9 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 47 65 urrent).. { "Ge
11ada 74 53 79 73 74 65 6d 54 69 6d 65 22 2c 20 20 20 tSystemTime",
11adb 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c (SYSCALL
11adc 29 47 65 74 53 79 73 74 65 6d 54 69 6d 65 2c 20 )GetSystemTime,
11add 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 0a 0 },..
11ade 23 64 65 66 69 6e 65 20 6f 73 47 65 74 53 79 73 #define osGetSys
11adf 74 65 6d 54 69 6d 65 20 28 28 56 4f 49 44 28 57 temTime ((VOID(W
11ae0 49 4e 41 50 49 2a 29 28 4c 50 53 59 53 54 45 4d INAPI*)(LPSYSTEM
11ae1 54 49 4d 45 29 29 61 53 79 73 63 61 6c 6c 5b 32 TIME))aSyscall[2
11ae2 39 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 9].pCurrent)..#i
11ae3 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e f !SQLITE_OS_WIN
11ae4 43 45 0a 20 20 7b 20 22 47 65 74 53 79 73 74 65 CE. { "GetSyste
11ae5 6d 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 22 mTimeAsFileTime"
11ae6 2c 20 28 53 59 53 43 41 4c 4c 29 47 65 74 53 79 , (SYSCALL)GetSy
11ae7 73 74 65 6d 54 69 6d 65 41 73 46 69 6c 65 54 69 stemTimeAsFileTi
11ae8 6d 65 2c 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 me, 0 },.#else.
11ae9 20 7b 20 22 47 65 74 53 79 73 74 65 6d 54 69 6d { "GetSystemTim
11aea 65 41 73 46 69 6c 65 54 69 6d 65 22 2c 20 28 53 eAsFileTime", (S
11aeb 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 YSCALL)0,
11aec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11aed 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 0 },.#endif..#de
11aee 66 69 6e 65 20 6f 73 47 65 74 53 79 73 74 65 6d fine osGetSystem
11aef 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 20 28 TimeAsFileTime (
11af0 28 56 4f 49 44 28 57 49 4e 41 50 49 2a 29 28 20 (VOID(WINAPI*)(
11af1 5c 0a 20 20 20 20 20 20 20 20 4c 50 46 49 4c 45 \. LPFILE
11af2 54 49 4d 45 29 29 61 53 79 73 63 61 6c 6c 5b 33 TIME))aSyscall[3
11af3 30 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 0].pCurrent)..#i
11af4 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
11af5 5f 57 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 29 _WIN32_HAS_ANSI)
11af6 0a 20 20 7b 20 22 47 65 74 54 65 6d 70 50 61 74 . { "GetTempPat
11af7 68 41 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 hA",
11af8 28 53 59 53 43 41 4c 4c 29 47 65 74 54 65 6d 70 (SYSCALL)GetTemp
11af9 50 61 74 68 41 2c 20 20 20 20 20 20 20 20 20 20 PathA,
11afa 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 0 },.#else. {
11afb 20 22 47 65 74 54 65 6d 70 50 61 74 68 41 22 2c "GetTempPathA",
11afc 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 (SYS
11afd 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 CALL)0,
11afe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 0
11aff 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 },.#endif..#defi
11b00 6e 65 20 6f 73 47 65 74 54 65 6d 70 50 61 74 68 ne osGetTempPath
11b01 41 20 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 A ((DWORD(WINAPI
11b02 2a 29 28 44 57 4f 52 44 2c 4c 50 53 54 52 29 29 *)(DWORD,LPSTR))
11b03 61 53 79 73 63 61 6c 6c 5b 33 31 5d 2e 70 43 75 aSyscall[31].pCu
11b04 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c rrent)..#if !SQL
11b05 49 54 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26 20 ITE_OS_WINRT &&
11b06 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 defined(SQLITE_W
11b07 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a 20 IN32_HAS_WIDE).
11b08 20 7b 20 22 47 65 74 54 65 6d 70 50 61 74 68 57 { "GetTempPathW
11b09 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 28 53 ", (S
11b0a 59 53 43 41 4c 4c 29 47 65 74 54 65 6d 70 50 61 YSCALL)GetTempPa
11b0b 74 68 57 2c 20 20 20 20 20 20 20 20 20 20 20 20 thW,
11b0c 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 0 },.#else. { "
11b0d 47 65 74 54 65 6d 70 50 61 74 68 57 22 2c 20 20 GetTempPathW",
11b0e 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 (SYSCA
11b0f 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 LL)0,
11b10 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0 },
11b11 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 .#endif..#define
11b12 20 6f 73 47 65 74 54 65 6d 70 50 61 74 68 57 20 osGetTempPathW
11b13 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 ((DWORD(WINAPI*)
11b14 28 44 57 4f 52 44 2c 4c 50 57 53 54 52 29 29 61 (DWORD,LPWSTR))a
11b15 53 79 73 63 61 6c 6c 5b 33 32 5d 2e 70 43 75 72 Syscall[32].pCur
11b16 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c 49 rent)..#if !SQLI
11b17 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 7b 20 TE_OS_WINRT. {
11b18 22 47 65 74 54 69 63 6b 43 6f 75 6e 74 22 2c 20 "GetTickCount",
11b19 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 (SYSC
11b1a 41 4c 4c 29 47 65 74 54 69 63 6b 43 6f 75 6e 74 ALL)GetTickCount
11b1b 2c 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d , 0 }
11b1c 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 47 65 74 ,.#else. { "Get
11b1d 54 69 63 6b 43 6f 75 6e 74 22 2c 20 20 20 20 20 TickCount",
11b1e 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 (SYSCALL)
11b1f 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
11b20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 0 },.#e
11b21 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 ndif..#define os
11b22 47 65 74 54 69 63 6b 43 6f 75 6e 74 20 28 28 44 GetTickCount ((D
11b23 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 28 56 4f WORD(WINAPI*)(VO
11b24 49 44 29 29 61 53 79 73 63 61 6c 6c 5b 33 33 5d ID))aSyscall[33]
11b25 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 .pCurrent)..#if
11b26 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 defined(SQLITE_W
11b27 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 29 0a 20 IN32_HAS_ANSI).
11b28 20 7b 20 22 47 65 74 56 65 72 73 69 6f 6e 45 78 { "GetVersionEx
11b29 41 22 2c 20 20 20 20 20 20 20 20 20 20 20 28 53 A", (S
11b2a 59 53 43 41 4c 4c 29 47 65 74 56 65 72 73 69 6f YSCALL)GetVersio
11b2b 6e 45 78 41 2c 20 20 20 20 20 20 20 20 20 20 20 nExA,
11b2c 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 0 },.#else. { "
11b2d 47 65 74 56 65 72 73 69 6f 6e 45 78 41 22 2c 20 GetVersionExA",
11b2e 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 (SYSCA
11b2f 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 LL)0,
11b30 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0 },
11b31 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 .#endif..#define
11b32 20 6f 73 47 65 74 56 65 72 73 69 6f 6e 45 78 41 osGetVersionExA
11b33 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 ((BOOL(WINAPI*)
11b34 28 20 5c 0a 20 20 20 20 20 20 20 20 4c 50 4f 53 ( \. LPOS
11b35 56 45 52 53 49 4f 4e 49 4e 46 4f 41 29 29 61 53 VERSIONINFOA))aS
11b36 79 73 63 61 6c 6c 5b 33 34 5d 2e 70 43 75 72 72 yscall[34].pCurr
11b37 65 6e 74 29 0a 0a 20 20 7b 20 22 48 65 61 70 41 ent).. { "HeapA
11b38 6c 6c 6f 63 22 2c 20 20 20 20 20 20 20 20 20 20 lloc",
11b39 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 48 65 (SYSCALL)He
11b3a 61 70 41 6c 6c 6f 63 2c 20 20 20 20 20 20 20 20 apAlloc,
11b3b 20 20 20 20 20 20 20 30 20 7d 2c 0a 0a 23 64 65 0 },..#de
11b3c 66 69 6e 65 20 6f 73 48 65 61 70 41 6c 6c 6f 63 fine osHeapAlloc
11b3d 20 28 28 4c 50 56 4f 49 44 28 57 49 4e 41 50 49 ((LPVOID(WINAPI
11b3e 2a 29 28 48 41 4e 44 4c 45 2c 44 57 4f 52 44 2c *)(HANDLE,DWORD,
11b3f 20 5c 0a 20 20 20 20 20 20 20 20 53 49 5a 45 5f \. SIZE_
11b40 54 29 29 61 53 79 73 63 61 6c 6c 5b 33 35 5d 2e T))aSyscall[35].
11b41 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 pCurrent)..#if !
11b42 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a SQLITE_OS_WINRT.
11b43 20 20 7b 20 22 48 65 61 70 43 72 65 61 74 65 22 { "HeapCreate"
11b44 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 , (
11b45 53 59 53 43 41 4c 4c 29 48 65 61 70 43 72 65 61 SYSCALL)HeapCrea
11b46 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 te,
11b47 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 0 },.#else. {
11b48 22 48 65 61 70 43 72 65 61 74 65 22 2c 20 20 20 "HeapCreate",
11b49 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 (SYSC
11b4a 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 ALL)0,
11b4b 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 0 }
11b4c 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e ,.#endif..#defin
11b4d 65 20 6f 73 48 65 61 70 43 72 65 61 74 65 20 28 e osHeapCreate (
11b4e 28 48 41 4e 44 4c 45 28 57 49 4e 41 50 49 2a 29 (HANDLE(WINAPI*)
11b4f 28 44 57 4f 52 44 2c 53 49 5a 45 5f 54 2c 20 5c (DWORD,SIZE_T, \
11b50 0a 20 20 20 20 20 20 20 20 53 49 5a 45 5f 54 29 . SIZE_T)
11b51 29 61 53 79 73 63 61 6c 6c 5b 33 36 5d 2e 70 43 )aSyscall[36].pC
11b52 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 urrent)..#if !SQ
11b53 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 LITE_OS_WINRT.
11b54 7b 20 22 48 65 61 70 44 65 73 74 72 6f 79 22 2c { "HeapDestroy",
11b55 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 (SY
11b56 53 43 41 4c 4c 29 48 65 61 70 44 65 73 74 72 6f SCALL)HeapDestro
11b57 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 30 y, 0
11b58 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 48 },.#else. { "H
11b59 65 61 70 44 65 73 74 72 6f 79 22 2c 20 20 20 20 eapDestroy",
11b5a 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c (SYSCAL
11b5b 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 L)0,
11b5c 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 0 },.
11b5d 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 #endif..#define
11b5e 6f 73 48 65 61 70 44 65 73 74 72 6f 79 20 28 28 osHeapDestroy ((
11b5f 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41 BOOL(WINAPI*)(HA
11b60 4e 44 4c 45 29 29 61 53 79 73 63 61 6c 6c 5b 33 NDLE))aSyscall[3
11b61 37 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7].pCurrent)..
11b62 7b 20 22 48 65 61 70 46 72 65 65 22 2c 20 20 20 { "HeapFree",
11b63 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 (SY
11b64 53 43 41 4c 4c 29 48 65 61 70 46 72 65 65 2c 20 SCALL)HeapFree,
11b65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
11b66 20 7d 2c 0a 0a 23 64 65 66 69 6e 65 20 6f 73 48 },..#define osH
11b67 65 61 70 46 72 65 65 20 28 28 42 4f 4f 4c 28 57 eapFree ((BOOL(W
11b68 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 2c 44 INAPI*)(HANDLE,D
11b69 57 4f 52 44 2c 4c 50 56 4f 49 44 29 29 61 53 79 WORD,LPVOID))aSy
11b6a 73 63 61 6c 6c 5b 33 38 5d 2e 70 43 75 72 72 65 scall[38].pCurre
11b6b 6e 74 29 0a 0a 20 20 7b 20 22 48 65 61 70 52 65 nt).. { "HeapRe
11b6c 41 6c 6c 6f 63 22 2c 20 20 20 20 20 20 20 20 20 Alloc",
11b6d 20 20 20 20 28 53 59 53 43 41 4c 4c 29 48 65 61 (SYSCALL)Hea
11b6e 70 52 65 41 6c 6c 6f 63 2c 20 20 20 20 20 20 20 pReAlloc,
11b6f 20 20 20 20 20 20 30 20 7d 2c 0a 0a 23 64 65 66 0 },..#def
11b70 69 6e 65 20 6f 73 48 65 61 70 52 65 41 6c 6c 6f ine osHeapReAllo
11b71 63 20 28 28 4c 50 56 4f 49 44 28 57 49 4e 41 50 c ((LPVOID(WINAP
11b72 49 2a 29 28 48 41 4e 44 4c 45 2c 44 57 4f 52 44 I*)(HANDLE,DWORD
11b73 2c 4c 50 56 4f 49 44 2c 20 5c 0a 20 20 20 20 20 ,LPVOID, \.
11b74 20 20 20 53 49 5a 45 5f 54 29 29 61 53 79 73 63 SIZE_T))aSysc
11b75 61 6c 6c 5b 33 39 5d 2e 70 43 75 72 72 65 6e 74 all[39].pCurrent
11b76 29 0a 0a 20 20 7b 20 22 48 65 61 70 53 69 7a 65 ).. { "HeapSize
11b77 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ",
11b78 20 20 28 53 59 53 43 41 4c 4c 29 48 65 61 70 53 (SYSCALL)HeapS
11b79 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ize,
11b7a 20 20 20 20 30 20 7d 2c 0a 0a 23 64 65 66 69 6e 0 },..#defin
11b7b 65 20 6f 73 48 65 61 70 53 69 7a 65 20 28 28 53 e osHeapSize ((S
11b7c 49 5a 45 5f 54 28 57 49 4e 41 50 49 2a 29 28 48 IZE_T(WINAPI*)(H
11b7d 41 4e 44 4c 45 2c 44 57 4f 52 44 2c 20 5c 0a 20 ANDLE,DWORD, \.
11b7e 20 20 20 20 20 20 20 4c 50 43 56 4f 49 44 29 29 LPCVOID))
11b7f 61 53 79 73 63 61 6c 6c 5b 34 30 5d 2e 70 43 75 aSyscall[40].pCu
11b80 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c rrent)..#if !SQL
11b81 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 7b ITE_OS_WINRT. {
11b82 20 22 48 65 61 70 56 61 6c 69 64 61 74 65 22 2c "HeapValidate",
11b83 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 (SYS
11b84 43 41 4c 4c 29 48 65 61 70 56 61 6c 69 64 61 74 CALL)HeapValidat
11b85 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 30 20 e, 0
11b86 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 48 65 },.#else. { "He
11b87 61 70 56 61 6c 69 64 61 74 65 22 2c 20 20 20 20 apValidate",
11b88 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c (SYSCALL
11b89 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 )0,
11b8a 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 0 },.#
11b8b 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f endif..#define o
11b8c 73 48 65 61 70 56 61 6c 69 64 61 74 65 20 28 28 sHeapValidate ((
11b8d 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41 BOOL(WINAPI*)(HA
11b8e 4e 44 4c 45 2c 44 57 4f 52 44 2c 20 5c 0a 20 20 NDLE,DWORD, \.
11b8f 20 20 20 20 20 20 4c 50 43 56 4f 49 44 29 29 61 LPCVOID))a
11b90 53 79 73 63 61 6c 6c 5b 34 31 5d 2e 70 43 75 72 Syscall[41].pCur
11b91 72 65 6e 74 29 0a 0a 23 69 66 20 64 65 66 69 6e rent)..#if defin
11b92 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f ed(SQLITE_WIN32_
11b93 48 41 53 5f 41 4e 53 49 29 20 26 26 20 21 64 65 HAS_ANSI) && !de
11b94 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
11b95 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e T_LOAD_EXTENSION
11b96 29 0a 20 20 7b 20 22 4c 6f 61 64 4c 69 62 72 61 ). { "LoadLibra
11b97 72 79 41 22 2c 20 20 20 20 20 20 20 20 20 20 20 ryA",
11b98 20 28 53 59 53 43 41 4c 4c 29 4c 6f 61 64 4c 69 (SYSCALL)LoadLi
11b99 62 72 61 72 79 41 2c 20 20 20 20 20 20 20 20 20 braryA,
11b9a 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 0 },.#else.
11b9b 7b 20 22 4c 6f 61 64 4c 69 62 72 61 72 79 41 22 { "LoadLibraryA"
11b9c 2c 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 , (SY
11b9d 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 SCALL)0,
11b9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
11b9f 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 },.#endif..#def
11ba0 69 6e 65 20 6f 73 4c 6f 61 64 4c 69 62 72 61 72 ine osLoadLibrar
11ba1 79 41 20 28 28 48 4d 4f 44 55 4c 45 28 57 49 4e yA ((HMODULE(WIN
11ba2 41 50 49 2a 29 28 4c 50 43 53 54 52 29 29 61 53 API*)(LPCSTR))aS
11ba3 79 73 63 61 6c 6c 5b 34 32 5d 2e 70 43 75 72 72 yscall[42].pCurr
11ba4 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c 49 54 ent)..#if !SQLIT
11ba5 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26 20 64 65 E_OS_WINRT && de
11ba6 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e fined(SQLITE_WIN
11ba7 33 32 5f 48 41 53 5f 57 49 44 45 29 20 26 26 20 32_HAS_WIDE) &&
11ba8 5c 0a 20 20 20 20 20 20 20 20 21 64 65 66 69 6e \. !defin
11ba9 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c ed(SQLITE_OMIT_L
11baa 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 29 0a 20 OAD_EXTENSION).
11bab 20 7b 20 22 4c 6f 61 64 4c 69 62 72 61 72 79 57 { "LoadLibraryW
11bac 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 28 53 ", (S
11bad 59 53 43 41 4c 4c 29 4c 6f 61 64 4c 69 62 72 61 YSCALL)LoadLibra
11bae 72 79 57 2c 20 20 20 20 20 20 20 20 20 20 20 20 ryW,
11baf 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 0 },.#else. { "
11bb0 4c 6f 61 64 4c 69 62 72 61 72 79 57 22 2c 20 20 LoadLibraryW",
11bb1 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 (SYSCA
11bb2 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 LL)0,
11bb3 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0 },
11bb4 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 .#endif..#define
11bb5 20 6f 73 4c 6f 61 64 4c 69 62 72 61 72 79 57 20 osLoadLibraryW
11bb6 28 28 48 4d 4f 44 55 4c 45 28 57 49 4e 41 50 49 ((HMODULE(WINAPI
11bb7 2a 29 28 4c 50 43 57 53 54 52 29 29 61 53 79 73 *)(LPCWSTR))aSys
11bb8 63 61 6c 6c 5b 34 33 5d 2e 70 43 75 72 72 65 6e call[43].pCurren
11bb9 74 29 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f t)..#if !SQLITE_
11bba 4f 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 4c 6f OS_WINRT. { "Lo
11bbb 63 61 6c 46 72 65 65 22 2c 20 20 20 20 20 20 20 calFree",
11bbc 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c (SYSCALL
11bbd 29 4c 6f 63 61 6c 46 72 65 65 2c 20 20 20 20 20 )LocalFree,
11bbe 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 0 },.#
11bbf 65 6c 73 65 0a 20 20 7b 20 22 4c 6f 63 61 6c 46 else. { "LocalF
11bc0 72 65 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 ree",
11bc1 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 (SYSCALL)0,
11bc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11bc3 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 0 },.#endi
11bc4 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 4c 6f 63 f..#define osLoc
11bc5 61 6c 46 72 65 65 20 28 28 48 4c 4f 43 41 4c 28 alFree ((HLOCAL(
11bc6 57 49 4e 41 50 49 2a 29 28 48 4c 4f 43 41 4c 29 WINAPI*)(HLOCAL)
11bc7 29 61 53 79 73 63 61 6c 6c 5b 34 34 5d 2e 70 43 )aSyscall[44].pC
11bc8 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 urrent)..#if !SQ
11bc9 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26 LITE_OS_WINCE &&
11bca 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 !SQLITE_OS_WINR
11bcb 54 0a 20 20 7b 20 22 4c 6f 63 6b 46 69 6c 65 22 T. { "LockFile"
11bcc 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
11bcd 20 28 53 59 53 43 41 4c 4c 29 4c 6f 63 6b 46 69 (SYSCALL)LockFi
11bce 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 le,
11bcf 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 0 },.#else.
11bd0 7b 20 22 4c 6f 63 6b 46 69 6c 65 22 2c 20 20 20 { "LockFile",
11bd1 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 (SY
11bd2 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 SCALL)0,
11bd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
11bd4 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e },.#endif..#ifn
11bd5 64 65 66 20 6f 73 4c 6f 63 6b 46 69 6c 65 0a 23 def osLockFile.#
11bd6 64 65 66 69 6e 65 20 6f 73 4c 6f 63 6b 46 69 6c define osLockFil
11bd7 65 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a e ((BOOL(WINAPI*
11bd8 29 28 48 41 4e 44 4c 45 2c 44 57 4f 52 44 2c 44 )(HANDLE,DWORD,D
11bd9 57 4f 52 44 2c 44 57 4f 52 44 2c 20 5c 0a 20 20 WORD,DWORD, \.
11bda 20 20 20 20 20 20 44 57 4f 52 44 29 29 61 53 79 DWORD))aSy
11bdb 73 63 61 6c 6c 5b 34 35 5d 2e 70 43 75 72 72 65 scall[45].pCurre
11bdc 6e 74 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 nt).#endif..#if
11bdd 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 !SQLITE_OS_WINCE
11bde 0a 20 20 7b 20 22 4c 6f 63 6b 46 69 6c 65 45 78 . { "LockFileEx
11bdf 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ",
11be0 28 53 59 53 43 41 4c 4c 29 4c 6f 63 6b 46 69 6c (SYSCALL)LockFil
11be1 65 45 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 eEx,
11be2 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 0 },.#else. {
11be3 20 22 4c 6f 63 6b 46 69 6c 65 45 78 22 2c 20 20 "LockFileEx",
11be4 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 (SYS
11be5 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 CALL)0,
11be6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 0
11be7 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 },.#endif..#ifnd
11be8 65 66 20 6f 73 4c 6f 63 6b 46 69 6c 65 45 78 0a ef osLockFileEx.
11be9 23 64 65 66 69 6e 65 20 6f 73 4c 6f 63 6b 46 69 #define osLockFi
11bea 6c 65 45 78 20 28 28 42 4f 4f 4c 28 57 49 4e 41 leEx ((BOOL(WINA
11beb 50 49 2a 29 28 48 41 4e 44 4c 45 2c 44 57 4f 52 PI*)(HANDLE,DWOR
11bec 44 2c 44 57 4f 52 44 2c 44 57 4f 52 44 2c 44 57 D,DWORD,DWORD,DW
11bed 4f 52 44 2c 20 5c 0a 20 20 20 20 20 20 20 20 4c ORD, \. L
11bee 50 4f 56 45 52 4c 41 50 50 45 44 29 29 61 53 79 POVERLAPPED))aSy
11bef 73 63 61 6c 6c 5b 34 36 5d 2e 70 43 75 72 72 65 scall[46].pCurre
11bf0 6e 74 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 nt).#endif..#if
11bf1 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 SQLITE_OS_WINCE
11bf2 7c 7c 20 28 21 53 51 4c 49 54 45 5f 4f 53 5f 57 || (!SQLITE_OS_W
11bf3 49 4e 52 54 20 26 26 20 21 64 65 66 69 6e 65 64 INRT && !defined
11bf4 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c (SQLITE_OMIT_WAL
11bf5 29 29 0a 20 20 7b 20 22 4d 61 70 56 69 65 77 4f )). { "MapViewO
11bf6 66 46 69 6c 65 22 2c 20 20 20 20 20 20 20 20 20 fFile",
11bf7 20 20 28 53 59 53 43 41 4c 4c 29 4d 61 70 56 69 (SYSCALL)MapVi
11bf8 65 77 4f 66 46 69 6c 65 2c 20 20 20 20 20 20 20 ewOfFile,
11bf9 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 0 },.#else.
11bfa 20 7b 20 22 4d 61 70 56 69 65 77 4f 66 46 69 6c { "MapViewOfFil
11bfb 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 28 53 e", (S
11bfc 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 YSCALL)0,
11bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11bfe 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 0 },.#endif..#de
11bff 66 69 6e 65 20 6f 73 4d 61 70 56 69 65 77 4f 66 fine osMapViewOf
11c00 46 69 6c 65 20 28 28 4c 50 56 4f 49 44 28 57 49 File ((LPVOID(WI
11c01 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 2c 44 57 NAPI*)(HANDLE,DW
11c02 4f 52 44 2c 44 57 4f 52 44 2c 44 57 4f 52 44 2c ORD,DWORD,DWORD,
11c03 20 5c 0a 20 20 20 20 20 20 20 20 53 49 5a 45 5f \. SIZE_
11c04 54 29 29 61 53 79 73 63 61 6c 6c 5b 34 37 5d 2e T))aSyscall[47].
11c05 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 pCurrent).. { "
11c06 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 MultiByteToWideC
11c07 68 61 72 22 2c 20 20 20 20 20 28 53 59 53 43 41 har", (SYSCA
11c08 4c 4c 29 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 LL)MultiByteToWi
11c09 64 65 43 68 61 72 2c 20 20 20 20 20 30 20 7d 2c deChar, 0 },
11c0a 0a 0a 23 64 65 66 69 6e 65 20 6f 73 4d 75 6c 74 ..#define osMult
11c0b 69 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 20 iByteToWideChar
11c0c 28 28 69 6e 74 28 57 49 4e 41 50 49 2a 29 28 55 ((int(WINAPI*)(U
11c0d 49 4e 54 2c 44 57 4f 52 44 2c 4c 50 43 53 54 52 INT,DWORD,LPCSTR
11c0e 2c 69 6e 74 2c 4c 50 57 53 54 52 2c 20 5c 0a 20 ,int,LPWSTR, \.
11c0f 20 20 20 20 20 20 20 69 6e 74 29 29 61 53 79 73 int))aSys
11c10 63 61 6c 6c 5b 34 38 5d 2e 70 43 75 72 72 65 6e call[48].pCurren
11c11 74 29 0a 0a 20 20 7b 20 22 51 75 65 72 79 50 65 t).. { "QueryPe
11c12 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 rformanceCounter
11c13 22 2c 20 28 53 59 53 43 41 4c 4c 29 51 75 65 72 ", (SYSCALL)Quer
11c14 79 50 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e yPerformanceCoun
11c15 74 65 72 2c 20 30 20 7d 2c 0a 0a 23 64 65 66 69 ter, 0 },..#defi
11c16 6e 65 20 6f 73 51 75 65 72 79 50 65 72 66 6f 72 ne osQueryPerfor
11c17 6d 61 6e 63 65 43 6f 75 6e 74 65 72 20 28 28 42 manceCounter ((B
11c18 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 20 5c 0a OOL(WINAPI*)( \.
11c19 20 20 20 20 20 20 20 20 4c 41 52 47 45 5f 49 4e LARGE_IN
11c1a 54 45 47 45 52 2a 29 29 61 53 79 73 63 61 6c 6c TEGER*))aSyscall
11c1b 5b 34 39 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a [49].pCurrent)..
11c1c 20 20 7b 20 22 52 65 61 64 46 69 6c 65 22 2c 20 { "ReadFile",
11c1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
11c1e 53 59 53 43 41 4c 4c 29 52 65 61 64 46 69 6c 65 SYSCALL)ReadFile
11c1f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
11c20 20 30 20 7d 2c 0a 0a 23 64 65 66 69 6e 65 20 6f 0 },..#define o
11c21 73 52 65 61 64 46 69 6c 65 20 28 28 42 4f 4f 4c sReadFile ((BOOL
11c22 28 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 (WINAPI*)(HANDLE
11c23 2c 4c 50 56 4f 49 44 2c 44 57 4f 52 44 2c 4c 50 ,LPVOID,DWORD,LP
11c24 44 57 4f 52 44 2c 20 5c 0a 20 20 20 20 20 20 20 DWORD, \.
11c25 20 4c 50 4f 56 45 52 4c 41 50 50 45 44 29 29 61 LPOVERLAPPED))a
11c26 53 79 73 63 61 6c 6c 5b 35 30 5d 2e 70 43 75 72 Syscall[50].pCur
11c27 72 65 6e 74 29 0a 0a 20 20 7b 20 22 53 65 74 45 rent).. { "SetE
11c28 6e 64 4f 66 46 69 6c 65 22 2c 20 20 20 20 20 20 ndOfFile",
11c29 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 53 (SYSCALL)S
11c2a 65 74 45 6e 64 4f 66 46 69 6c 65 2c 20 20 20 20 etEndOfFile,
11c2b 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 0a 23 64 0 },..#d
11c2c 65 66 69 6e 65 20 6f 73 53 65 74 45 6e 64 4f 66 efine osSetEndOf
11c2d 46 69 6c 65 20 28 28 42 4f 4f 4c 28 57 49 4e 41 File ((BOOL(WINA
11c2e 50 49 2a 29 28 48 41 4e 44 4c 45 29 29 61 53 79 PI*)(HANDLE))aSy
11c2f 73 63 61 6c 6c 5b 35 31 5d 2e 70 43 75 72 72 65 scall[51].pCurre
11c30 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 nt)..#if !SQLITE
11c31 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 53 _OS_WINRT. { "S
11c32 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 22 2c 20 etFilePointer",
11c33 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c (SYSCAL
11c34 4c 29 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 L)SetFilePointer
11c35 2c 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a , 0 },.
11c36 23 65 6c 73 65 0a 20 20 7b 20 22 53 65 74 46 69 #else. { "SetFi
11c37 6c 65 50 6f 69 6e 74 65 72 22 2c 20 20 20 20 20 lePointer",
11c38 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c (SYSCALL)0,
11c39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11c3a 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 0 },.#end
11c3b 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 53 65 if..#define osSe
11c3c 74 46 69 6c 65 50 6f 69 6e 74 65 72 20 28 28 44 tFilePointer ((D
11c3d 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 28 48 41 WORD(WINAPI*)(HA
11c3e 4e 44 4c 45 2c 4c 4f 4e 47 2c 50 4c 4f 4e 47 2c NDLE,LONG,PLONG,
11c3f 20 5c 0a 20 20 20 20 20 20 20 20 44 57 4f 52 44 \. DWORD
11c40 29 29 61 53 79 73 63 61 6c 6c 5b 35 32 5d 2e 70 ))aSyscall[52].p
11c41 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 Current)..#if !S
11c42 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 QLITE_OS_WINRT.
11c43 20 7b 20 22 53 6c 65 65 70 22 2c 20 20 20 20 20 { "Sleep",
11c44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 (S
11c45 59 53 43 41 4c 4c 29 53 6c 65 65 70 2c 20 20 20 YSCALL)Sleep,
11c46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11c47 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 0 },.#else. { "
11c48 53 6c 65 65 70 22 2c 20 20 20 20 20 20 20 20 20 Sleep",
11c49 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 (SYSCA
11c4a 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 LL)0,
11c4b 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0 },
11c4c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 .#endif..#define
11c4d 20 6f 73 53 6c 65 65 70 20 28 28 56 4f 49 44 28 osSleep ((VOID(
11c4e 57 49 4e 41 50 49 2a 29 28 44 57 4f 52 44 29 29 WINAPI*)(DWORD))
11c4f 61 53 79 73 63 61 6c 6c 5b 35 33 5d 2e 70 43 75 aSyscall[53].pCu
11c50 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 53 79 73 rrent).. { "Sys
11c51 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d temTimeToFileTim
11c52 65 22 2c 20 20 20 20 28 53 59 53 43 41 4c 4c 29 e", (SYSCALL)
11c53 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 SystemTimeToFile
11c54 54 69 6d 65 2c 20 20 20 20 30 20 7d 2c 0a 0a 23 Time, 0 },..#
11c55 64 65 66 69 6e 65 20 6f 73 53 79 73 74 65 6d 54 define osSystemT
11c56 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65 20 28 28 imeToFileTime ((
11c57 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 43 4f BOOL(WINAPI*)(CO
11c58 4e 53 54 20 53 59 53 54 45 4d 54 49 4d 45 2a 2c NST SYSTEMTIME*,
11c59 20 5c 0a 20 20 20 20 20 20 20 20 4c 50 46 49 4c \. LPFIL
11c5a 45 54 49 4d 45 29 29 61 53 79 73 63 61 6c 6c 5b ETIME))aSyscall[
11c5b 35 34 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 54].pCurrent)..#
11c5c 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 if !SQLITE_OS_WI
11c5d 4e 43 45 20 26 26 20 21 53 51 4c 49 54 45 5f 4f NCE && !SQLITE_O
11c5e 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 55 6e 6c S_WINRT. { "Unl
11c5f 6f 63 6b 46 69 6c 65 22 2c 20 20 20 20 20 20 20 ockFile",
11c60 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 (SYSCALL)
11c61 55 6e 6c 6f 63 6b 46 69 6c 65 2c 20 20 20 20 20 UnlockFile,
11c62 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 0 },.#e
11c63 6c 73 65 0a 20 20 7b 20 22 55 6e 6c 6f 63 6b 46 lse. { "UnlockF
11c64 69 6c 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 ile",
11c65 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 (SYSCALL)0,
11c66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11c67 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0 },.#endif
11c68 0a 0a 23 69 66 6e 64 65 66 20 6f 73 55 6e 6c 6f ..#ifndef osUnlo
11c69 63 6b 46 69 6c 65 0a 23 64 65 66 69 6e 65 20 6f ckFile.#define o
11c6a 73 55 6e 6c 6f 63 6b 46 69 6c 65 20 28 28 42 4f sUnlockFile ((BO
11c6b 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 OL(WINAPI*)(HAND
11c6c 4c 45 2c 44 57 4f 52 44 2c 44 57 4f 52 44 2c 44 LE,DWORD,DWORD,D
11c6d 57 4f 52 44 2c 20 5c 0a 20 20 20 20 20 20 20 20 WORD, \.
11c6e 44 57 4f 52 44 29 29 61 53 79 73 63 61 6c 6c 5b DWORD))aSyscall[
11c6f 35 35 5d 2e 70 43 75 72 72 65 6e 74 29 0a 23 65 55].pCurrent).#e
11c70 6e 64 69 66 0a 0a 23 69 66 20 21 53 51 4c 49 54 ndif..#if !SQLIT
11c71 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 7b 20 22 E_OS_WINCE. { "
11c72 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 22 2c 20 20 UnlockFileEx",
11c73 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 (SYSCA
11c74 4c 4c 29 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 2c LL)UnlockFileEx,
11c75 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0 },
11c76 0a 23 65 6c 73 65 0a 20 20 7b 20 22 55 6e 6c 6f .#else. { "Unlo
11c77 63 6b 46 69 6c 65 45 78 22 2c 20 20 20 20 20 20 ckFileEx",
11c78 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 (SYSCALL)0
11c79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
11c7a 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 0 },.#en
11c7b 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 55 dif..#define osU
11c7c 6e 6c 6f 63 6b 46 69 6c 65 45 78 20 28 28 42 4f nlockFileEx ((BO
11c7d 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 OL(WINAPI*)(HAND
11c7e 4c 45 2c 44 57 4f 52 44 2c 44 57 4f 52 44 2c 44 LE,DWORD,DWORD,D
11c7f 57 4f 52 44 2c 20 5c 0a 20 20 20 20 20 20 20 20 WORD, \.
11c80 4c 50 4f 56 45 52 4c 41 50 50 45 44 29 29 61 53 LPOVERLAPPED))aS
11c81 79 73 63 61 6c 6c 5b 35 36 5d 2e 70 43 75 72 72 yscall[56].pCurr
11c82 65 6e 74 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 ent)..#if SQLITE
11c83 5f 4f 53 5f 57 49 4e 43 45 20 7c 7c 20 21 64 65 _OS_WINCE || !de
11c84 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
11c85 54 5f 57 41 4c 29 0a 20 20 7b 20 22 55 6e 6d 61 T_WAL). { "Unma
11c86 70 56 69 65 77 4f 66 46 69 6c 65 22 2c 20 20 20 pViewOfFile",
11c87 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 55 (SYSCALL)U
11c88 6e 6d 61 70 56 69 65 77 4f 66 46 69 6c 65 2c 20 nmapViewOfFile,
11c89 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6c 0 },.#el
11c8a 73 65 0a 20 20 7b 20 22 55 6e 6d 61 70 56 69 65 se. { "UnmapVie
11c8b 77 4f 66 46 69 6c 65 22 2c 20 20 20 20 20 20 20 wOfFile",
11c8c 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 (SYSCALL)0,
11c8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11c8e 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0 },.#endif.
11c8f 0a 23 64 65 66 69 6e 65 20 6f 73 55 6e 6d 61 70 .#define osUnmap
11c90 56 69 65 77 4f 66 46 69 6c 65 20 28 28 42 4f 4f ViewOfFile ((BOO
11c91 4c 28 57 49 4e 41 50 49 2a 29 28 4c 50 43 56 4f L(WINAPI*)(LPCVO
11c92 49 44 29 29 61 53 79 73 63 61 6c 6c 5b 35 37 5d ID))aSyscall[57]
11c93 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 .pCurrent).. {
11c94 22 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 "WideCharToMulti
11c95 42 79 74 65 22 2c 20 20 20 20 20 28 53 59 53 43 Byte", (SYSC
11c96 41 4c 4c 29 57 69 64 65 43 68 61 72 54 6f 4d 75 ALL)WideCharToMu
11c97 6c 74 69 42 79 74 65 2c 20 20 20 20 20 30 20 7d ltiByte, 0 }
11c98 2c 0a 0a 23 64 65 66 69 6e 65 20 6f 73 57 69 64 ,..#define osWid
11c99 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 eCharToMultiByte
11c9a 20 28 28 69 6e 74 28 57 49 4e 41 50 49 2a 29 28 ((int(WINAPI*)(
11c9b 55 49 4e 54 2c 44 57 4f 52 44 2c 4c 50 43 57 53 UINT,DWORD,LPCWS
11c9c 54 52 2c 69 6e 74 2c 4c 50 53 54 52 2c 69 6e 74 TR,int,LPSTR,int
11c9d 2c 20 5c 0a 20 20 20 20 20 20 20 20 4c 50 43 53 , \. LPCS
11c9e 54 52 2c 4c 50 42 4f 4f 4c 29 29 61 53 79 73 63 TR,LPBOOL))aSysc
11c9f 61 6c 6c 5b 35 38 5d 2e 70 43 75 72 72 65 6e 74 all[58].pCurrent
11ca0 29 0a 0a 20 20 7b 20 22 57 72 69 74 65 46 69 6c ).. { "WriteFil
11ca1 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 e",
11ca2 20 20 28 53 59 53 43 41 4c 4c 29 57 72 69 74 65 (SYSCALL)Write
11ca3 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 File,
11ca4 20 20 20 20 30 20 7d 2c 0a 0a 23 64 65 66 69 6e 0 },..#defin
11ca5 65 20 6f 73 57 72 69 74 65 46 69 6c 65 20 28 28 e osWriteFile ((
11ca6 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41 BOOL(WINAPI*)(HA
11ca7 4e 44 4c 45 2c 4c 50 43 56 4f 49 44 2c 44 57 4f NDLE,LPCVOID,DWO
11ca8 52 44 2c 4c 50 44 57 4f 52 44 2c 20 5c 0a 20 20 RD,LPDWORD, \.
11ca9 20 20 20 20 20 20 4c 50 4f 56 45 52 4c 41 50 50 LPOVERLAPP
11caa 45 44 29 29 61 53 79 73 63 61 6c 6c 5b 35 39 5d ED))aSyscall[59]
11cab 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 .pCurrent)..#if
11cac 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a SQLITE_OS_WINRT.
11cad 20 20 7b 20 22 43 72 65 61 74 65 45 76 65 6e 74 { "CreateEvent
11cae 45 78 57 22 2c 20 20 20 20 20 20 20 20 20 20 28 ExW", (
11caf 53 59 53 43 41 4c 4c 29 43 72 65 61 74 65 45 76 SYSCALL)CreateEv
11cb0 65 6e 74 45 78 57 2c 20 20 20 20 20 20 20 20 20 entExW,
11cb1 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 0 },.#else. {
11cb2 22 43 72 65 61 74 65 45 76 65 6e 74 45 78 57 22 "CreateEventExW"
11cb3 2c 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 , (SYSC
11cb4 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 ALL)0,
11cb5 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 0 }
11cb6 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e ,.#endif..#defin
11cb7 65 20 6f 73 43 72 65 61 74 65 45 76 65 6e 74 45 e osCreateEventE
11cb8 78 57 20 28 28 48 41 4e 44 4c 45 28 57 49 4e 41 xW ((HANDLE(WINA
11cb9 50 49 2a 29 28 4c 50 53 45 43 55 52 49 54 59 5f PI*)(LPSECURITY_
11cba 41 54 54 52 49 42 55 54 45 53 2c 4c 50 43 57 53 ATTRIBUTES,LPCWS
11cbb 54 52 2c 20 5c 0a 20 20 20 20 20 20 20 20 44 57 TR, \. DW
11cbc 4f 52 44 2c 44 57 4f 52 44 29 29 61 53 79 73 63 ORD,DWORD))aSysc
11cbd 61 6c 6c 5b 36 30 5d 2e 70 43 75 72 72 65 6e 74 all[60].pCurrent
11cbe 29 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f )..#if !SQLITE_O
11cbf 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 57 61 69 S_WINRT. { "Wai
11cc0 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74 tForSingleObject
11cc1 22 2c 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 ", (SYSCALL)
11cc2 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a WaitForSingleObj
11cc3 65 63 74 2c 20 20 20 20 20 30 20 7d 2c 0a 23 65 ect, 0 },.#e
11cc4 6c 73 65 0a 20 20 7b 20 22 57 61 69 74 46 6f 72 lse. { "WaitFor
11cc5 53 69 6e 67 6c 65 4f 62 6a 65 63 74 22 2c 20 20 SingleObject",
11cc6 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 (SYSCALL)0,
11cc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11cc8 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0 },.#endif
11cc9 0a 0a 23 64 65 66 69 6e 65 20 6f 73 57 61 69 74 ..#define osWait
11cca 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74 20 ForSingleObject
11ccb 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 ((DWORD(WINAPI*)
11ccc 28 48 41 4e 44 4c 45 2c 20 5c 0a 20 20 20 20 20 (HANDLE, \.
11ccd 20 20 20 44 57 4f 52 44 29 29 61 53 79 73 63 61 DWORD))aSysca
11cce 6c 6c 5b 36 31 5d 2e 70 43 75 72 72 65 6e 74 29 ll[61].pCurrent)
11ccf 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ..#if SQLITE_OS_
11cd0 57 49 4e 52 54 0a 20 20 7b 20 22 57 61 69 74 46 WINRT. { "WaitF
11cd1 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74 45 78 orSingleObjectEx
11cd2 22 2c 20 20 20 28 53 59 53 43 41 4c 4c 29 57 61 ", (SYSCALL)Wa
11cd3 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 itForSingleObjec
11cd4 74 45 78 2c 20 20 20 30 20 7d 2c 0a 23 65 6c 73 tEx, 0 },.#els
11cd5 65 0a 20 20 7b 20 22 57 61 69 74 46 6f 72 53 69 e. { "WaitForSi
11cd6 6e 67 6c 65 4f 62 6a 65 63 74 45 78 22 2c 20 20 ngleObjectEx",
11cd7 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 (SYSCALL)0,
11cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11cd9 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 0 },.#endif..
11cda 23 64 65 66 69 6e 65 20 6f 73 57 61 69 74 46 6f #define osWaitFo
11cdb 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74 45 78 20 rSingleObjectEx
11cdc 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 ((DWORD(WINAPI*)
11cdd 28 48 41 4e 44 4c 45 2c 44 57 4f 52 44 2c 20 5c (HANDLE,DWORD, \
11cde 0a 20 20 20 20 20 20 20 20 42 4f 4f 4c 29 29 61 . BOOL))a
11cdf 53 79 73 63 61 6c 6c 5b 36 32 5d 2e 70 43 75 72 Syscall[62].pCur
11ce0 72 65 6e 74 29 0a 0a 23 69 66 20 53 51 4c 49 54 rent)..#if SQLIT
11ce1 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 E_OS_WINRT. { "
11ce2 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 45 78 SetFilePointerEx
11ce3 22 2c 20 20 20 20 20 20 20 20 28 53 59 53 43 41 ", (SYSCA
11ce4 4c 4c 29 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 LL)SetFilePointe
11ce5 72 45 78 2c 20 20 20 20 20 20 20 20 30 20 7d 2c rEx, 0 },
11ce6 0a 23 65 6c 73 65 0a 20 20 7b 20 22 53 65 74 46 .#else. { "SetF
11ce7 69 6c 65 50 6f 69 6e 74 65 72 45 78 22 2c 20 20 ilePointerEx",
11ce8 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 (SYSCALL)0
11ce9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
11cea 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 0 },.#en
11ceb 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 53 dif..#define osS
11cec 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 45 78 20 etFilePointerEx
11ced 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 ((BOOL(WINAPI*)(
11cee 48 41 4e 44 4c 45 2c 4c 41 52 47 45 5f 49 4e 54 HANDLE,LARGE_INT
11cef 45 47 45 52 2c 20 5c 0a 20 20 20 20 20 20 20 20 EGER, \.
11cf0 50 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 2c 44 PLARGE_INTEGER,D
11cf1 57 4f 52 44 29 29 61 53 79 73 63 61 6c 6c 5b 36 WORD))aSyscall[6
11cf2 33 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 3].pCurrent)..#i
11cf3 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 f SQLITE_OS_WINR
11cf4 54 0a 20 20 7b 20 22 47 65 74 46 69 6c 65 49 6e T. { "GetFileIn
11cf5 66 6f 72 6d 61 74 69 6f 6e 42 79 48 61 6e 64 6c formationByHandl
11cf6 65 45 78 22 2c 20 28 53 59 53 43 41 4c 4c 29 47 eEx", (SYSCALL)G
11cf7 65 74 46 69 6c 65 49 6e 66 6f 72 6d 61 74 69 6f etFileInformatio
11cf8 6e 42 79 48 61 6e 64 6c 65 45 78 2c 20 30 20 7d nByHandleEx, 0 }
11cf9 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 47 65 74 ,.#else. { "Get
11cfa 46 69 6c 65 49 6e 66 6f 72 6d 61 74 69 6f 6e 42 FileInformationB
11cfb 79 48 61 6e 64 6c 65 45 78 22 2c 20 28 53 59 53 yHandleEx", (SYS
11cfc 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 CALL)0,
11cfd 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 0 },.#e
11cfe 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 ndif..#define os
11cff 47 65 74 46 69 6c 65 49 6e 66 6f 72 6d 61 74 69 GetFileInformati
11d00 6f 6e 42 79 48 61 6e 64 6c 65 45 78 20 28 28 42 onByHandleEx ((B
11d01 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41 4e OOL(WINAPI*)(HAN
11d02 44 4c 45 2c 20 5c 0a 20 20 20 20 20 20 20 20 46 DLE, \. F
11d03 49 4c 45 5f 49 4e 46 4f 5f 42 59 5f 48 41 4e 44 ILE_INFO_BY_HAND
11d04 4c 45 5f 43 4c 41 53 53 2c 4c 50 56 4f 49 44 2c LE_CLASS,LPVOID,
11d05 44 57 4f 52 44 29 29 61 53 79 73 63 61 6c 6c 5b DWORD))aSyscall[
11d06 36 34 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 64].pCurrent)..#
11d07 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
11d08 52 54 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 RT && !defined(S
11d09 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 29 0a QLITE_OMIT_WAL).
11d0a 20 20 7b 20 22 4d 61 70 56 69 65 77 4f 66 46 69 { "MapViewOfFi
11d0b 6c 65 46 72 6f 6d 41 70 70 22 2c 20 20 20 20 28 leFromApp", (
11d0c 53 59 53 43 41 4c 4c 29 4d 61 70 56 69 65 77 4f SYSCALL)MapViewO
11d0d 66 46 69 6c 65 46 72 6f 6d 41 70 70 2c 20 20 20 fFileFromApp,
11d0e 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 0 },.#else. {
11d0f 22 4d 61 70 56 69 65 77 4f 66 46 69 6c 65 46 72 "MapViewOfFileFr
11d10 6f 6d 41 70 70 22 2c 20 20 20 20 28 53 59 53 43 omApp", (SYSC
11d11 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 ALL)0,
11d12 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 0 }
11d13 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e ,.#endif..#defin
11d14 65 20 6f 73 4d 61 70 56 69 65 77 4f 66 46 69 6c e osMapViewOfFil
11d15 65 46 72 6f 6d 41 70 70 20 28 28 4c 50 56 4f 49 eFromApp ((LPVOI
11d16 44 28 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c D(WINAPI*)(HANDL
11d17 45 2c 55 4c 4f 4e 47 2c 55 4c 4f 4e 47 36 34 2c E,ULONG,ULONG64,
11d18 20 5c 0a 20 20 20 20 20 20 20 20 53 49 5a 45 5f \. SIZE_
11d19 54 29 29 61 53 79 73 63 61 6c 6c 5b 36 35 5d 2e T))aSyscall[65].
11d1a 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 53 pCurrent)..#if S
11d1b 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 QLITE_OS_WINRT.
11d1c 20 7b 20 22 43 72 65 61 74 65 46 69 6c 65 32 22 { "CreateFile2"
11d1d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 , (S
11d1e 59 53 43 41 4c 4c 29 43 72 65 61 74 65 46 69 6c YSCALL)CreateFil
11d1f 65 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 e2,
11d20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 0 },.#else. { "
11d21 43 72 65 61 74 65 46 69 6c 65 32 22 2c 20 20 20 CreateFile2",
11d22 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 (SYSCA
11d23 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 LL)0,
11d24 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0 },
11d25 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 .#endif..#define
11d26 20 6f 73 43 72 65 61 74 65 46 69 6c 65 32 20 28 osCreateFile2 (
11d27 28 48 41 4e 44 4c 45 28 57 49 4e 41 50 49 2a 29 (HANDLE(WINAPI*)
11d28 28 4c 50 43 57 53 54 52 2c 44 57 4f 52 44 2c 44 (LPCWSTR,DWORD,D
11d29 57 4f 52 44 2c 44 57 4f 52 44 2c 20 5c 0a 20 20 WORD,DWORD, \.
11d2a 20 20 20 20 20 20 4c 50 43 52 45 41 54 45 46 49 LPCREATEFI
11d2b 4c 45 32 5f 45 58 54 45 4e 44 45 44 5f 50 41 52 LE2_EXTENDED_PAR
11d2c 41 4d 45 54 45 52 53 29 29 61 53 79 73 63 61 6c AMETERS))aSyscal
11d2d 6c 5b 36 36 5d 2e 70 43 75 72 72 65 6e 74 29 0a l[66].pCurrent).
11d2e 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
11d2f 49 4e 52 54 20 26 26 20 21 64 65 66 69 6e 65 64 INRT && !defined
11d30 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 (SQLITE_OMIT_LOA
11d31 44 5f 45 58 54 45 4e 53 49 4f 4e 29 0a 20 20 7b D_EXTENSION). {
11d32 20 22 4c 6f 61 64 50 61 63 6b 61 67 65 64 4c 69 "LoadPackagedLi
11d33 62 72 61 72 79 22 2c 20 20 20 20 20 28 53 59 53 brary", (SYS
11d34 43 41 4c 4c 29 4c 6f 61 64 50 61 63 6b 61 67 65 CALL)LoadPackage
11d35 64 4c 69 62 72 61 72 79 2c 20 20 20 20 20 30 20 dLibrary, 0
11d36 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 4c 6f },.#else. { "Lo
11d37 61 64 50 61 63 6b 61 67 65 64 4c 69 62 72 61 72 adPackagedLibrar
11d38 79 22 2c 20 20 20 20 20 28 53 59 53 43 41 4c 4c y", (SYSCALL
11d39 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 )0,
11d3a 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 0 },.#
11d3b 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f endif..#define o
11d3c 73 4c 6f 61 64 50 61 63 6b 61 67 65 64 4c 69 62 sLoadPackagedLib
11d3d 72 61 72 79 20 28 28 48 4d 4f 44 55 4c 45 28 57 rary ((HMODULE(W
11d3e 49 4e 41 50 49 2a 29 28 4c 50 43 57 53 54 52 2c INAPI*)(LPCWSTR,
11d3f 20 5c 0a 20 20 20 20 20 20 20 20 44 57 4f 52 44 \. DWORD
11d40 29 29 61 53 79 73 63 61 6c 6c 5b 36 37 5d 2e 70 ))aSyscall[67].p
11d41 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 53 51 Current)..#if SQ
11d42 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 LITE_OS_WINRT.
11d43 7b 20 22 47 65 74 54 69 63 6b 43 6f 75 6e 74 36 { "GetTickCount6
11d44 34 22 2c 20 20 20 20 20 20 20 20 20 20 28 53 59 4", (SY
11d45 53 43 41 4c 4c 29 47 65 74 54 69 63 6b 43 6f 75 SCALL)GetTickCou
11d46 6e 74 36 34 2c 20 20 20 20 20 20 20 20 20 20 30 nt64, 0
11d47 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 47 },.#else. { "G
11d48 65 74 54 69 63 6b 43 6f 75 6e 74 36 34 22 2c 20 etTickCount64",
11d49 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c (SYSCAL
11d4a 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 L)0,
11d4b 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 0 },.
11d4c 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 #endif..#define
11d4d 6f 73 47 65 74 54 69 63 6b 43 6f 75 6e 74 36 34 osGetTickCount64
11d4e 20 28 28 55 4c 4f 4e 47 4c 4f 4e 47 28 57 49 4e ((ULONGLONG(WIN
11d4f 41 50 49 2a 29 28 56 4f 49 44 29 29 61 53 79 73 API*)(VOID))aSys
11d50 63 61 6c 6c 5b 36 38 5d 2e 70 43 75 72 72 65 6e call[68].pCurren
11d51 74 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f t)..#if SQLITE_O
11d52 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 47 65 74 S_WINRT. { "Get
11d53 4e 61 74 69 76 65 53 79 73 74 65 6d 49 6e 66 6f NativeSystemInfo
11d54 22 2c 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 ", (SYSCALL)
11d55 47 65 74 4e 61 74 69 76 65 53 79 73 74 65 6d 49 GetNativeSystemI
11d56 6e 66 6f 2c 20 20 20 20 20 30 20 7d 2c 0a 23 65 nfo, 0 },.#e
11d57 6c 73 65 0a 20 20 7b 20 22 47 65 74 4e 61 74 69 lse. { "GetNati
11d58 76 65 53 79 73 74 65 6d 49 6e 66 6f 22 2c 20 20 veSystemInfo",
11d59 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 (SYSCALL)0,
11d5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d5b 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0 },.#endif
11d5c 0a 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 74 4e ..#define osGetN
11d5d 61 74 69 76 65 53 79 73 74 65 6d 49 6e 66 6f 20 ativeSystemInfo
11d5e 28 28 56 4f 49 44 28 57 49 4e 41 50 49 2a 29 28 ((VOID(WINAPI*)(
11d5f 20 5c 0a 20 20 20 20 20 20 20 20 4c 50 53 59 53 \. LPSYS
11d60 54 45 4d 5f 49 4e 46 4f 29 29 61 53 79 73 63 61 TEM_INFO))aSysca
11d61 6c 6c 5b 36 39 5d 2e 70 43 75 72 72 65 6e 74 29 ll[69].pCurrent)
11d62 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 ..#if defined(SQ
11d63 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 41 LITE_WIN32_HAS_A
11d64 4e 53 49 29 0a 20 20 7b 20 22 4f 75 74 70 75 74 NSI). { "Output
11d65 44 65 62 75 67 53 74 72 69 6e 67 41 22 2c 20 20 DebugStringA",
11d66 20 20 20 20 28 53 59 53 43 41 4c 4c 29 4f 75 74 (SYSCALL)Out
11d67 70 75 74 44 65 62 75 67 53 74 72 69 6e 67 41 2c putDebugStringA,
11d68 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0 },.#else
11d69 0a 20 20 7b 20 22 4f 75 74 70 75 74 44 65 62 75 . { "OutputDebu
11d6a 67 53 74 72 69 6e 67 41 22 2c 20 20 20 20 20 20 gStringA",
11d6b 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 (SYSCALL)0,
11d6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d6d 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 0 },.#endif..#
11d6e 64 65 66 69 6e 65 20 6f 73 4f 75 74 70 75 74 44 define osOutputD
11d6f 65 62 75 67 53 74 72 69 6e 67 41 20 28 28 56 4f ebugStringA ((VO
11d70 49 44 28 57 49 4e 41 50 49 2a 29 28 4c 50 43 53 ID(WINAPI*)(LPCS
11d71 54 52 29 29 61 53 79 73 63 61 6c 6c 5b 37 30 5d TR))aSyscall[70]
11d72 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 .pCurrent)..#if
11d73 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 defined(SQLITE_W
11d74 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a 20 IN32_HAS_WIDE).
11d75 20 7b 20 22 4f 75 74 70 75 74 44 65 62 75 67 53 { "OutputDebugS
11d76 74 72 69 6e 67 57 22 2c 20 20 20 20 20 20 28 53 tringW", (S
11d77 59 53 43 41 4c 4c 29 4f 75 74 70 75 74 44 65 62 YSCALL)OutputDeb
11d78 75 67 53 74 72 69 6e 67 57 2c 20 20 20 20 20 20 ugStringW,
11d79 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 0 },.#else. { "
11d7a 4f 75 74 70 75 74 44 65 62 75 67 53 74 72 69 6e OutputDebugStrin
11d7b 67 57 22 2c 20 20 20 20 20 20 28 53 59 53 43 41 gW", (SYSCA
11d7c 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 LL)0,
11d7d 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0 },
11d7e 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 .#endif..#define
11d7f 20 6f 73 4f 75 74 70 75 74 44 65 62 75 67 53 74 osOutputDebugSt
11d80 72 69 6e 67 57 20 28 28 56 4f 49 44 28 57 49 4e ringW ((VOID(WIN
11d81 41 50 49 2a 29 28 4c 50 43 57 53 54 52 29 29 61 API*)(LPCWSTR))a
11d82 53 79 73 63 61 6c 6c 5b 37 31 5d 2e 70 43 75 72 Syscall[71].pCur
11d83 72 65 6e 74 29 0a 0a 20 20 7b 20 22 47 65 74 50 rent).. { "GetP
11d84 72 6f 63 65 73 73 48 65 61 70 22 2c 20 20 20 20 rocessHeap",
11d85 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 47 (SYSCALL)G
11d86 65 74 50 72 6f 63 65 73 73 48 65 61 70 2c 20 20 etProcessHeap,
11d87 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 0a 23 64 0 },..#d
11d88 65 66 69 6e 65 20 6f 73 47 65 74 50 72 6f 63 65 efine osGetProce
11d89 73 73 48 65 61 70 20 28 28 48 41 4e 44 4c 45 28 ssHeap ((HANDLE(
11d8a 57 49 4e 41 50 49 2a 29 28 56 4f 49 44 29 29 61 WINAPI*)(VOID))a
11d8b 53 79 73 63 61 6c 6c 5b 37 32 5d 2e 70 43 75 72 Syscall[72].pCur
11d8c 72 65 6e 74 29 0a 0a 23 69 66 20 53 51 4c 49 54 rent)..#if SQLIT
11d8d 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26 20 21 64 E_OS_WINRT && !d
11d8e 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
11d8f 49 54 5f 57 41 4c 29 0a 20 20 7b 20 22 43 72 65 IT_WAL). { "Cre
11d90 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 46 72 ateFileMappingFr
11d91 6f 6d 41 70 70 22 2c 20 28 53 59 53 43 41 4c 4c omApp", (SYSCALL
11d92 29 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 )CreateFileMappi
11d93 6e 67 46 72 6f 6d 41 70 70 2c 20 30 20 7d 2c 0a ngFromApp, 0 },.
11d94 23 65 6c 73 65 0a 20 20 7b 20 22 43 72 65 61 74 #else. { "Creat
11d95 65 46 69 6c 65 4d 61 70 70 69 6e 67 46 72 6f 6d eFileMappingFrom
11d96 41 70 70 22 2c 20 28 53 59 53 43 41 4c 4c 29 30 App", (SYSCALL)0
11d97 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
11d98 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 0 },.#end
11d99 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 43 72 if..#define osCr
11d9a 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 46 eateFileMappingF
11d9b 72 6f 6d 41 70 70 20 28 28 48 41 4e 44 4c 45 28 romApp ((HANDLE(
11d9c 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 2c WINAPI*)(HANDLE,
11d9d 20 5c 0a 20 20 20 20 20 20 20 20 4c 50 53 45 43 \. LPSEC
11d9e 55 52 49 54 59 5f 41 54 54 52 49 42 55 54 45 53 URITY_ATTRIBUTES
11d9f 2c 55 4c 4f 4e 47 2c 55 4c 4f 4e 47 36 34 2c 4c ,ULONG,ULONG64,L
11da0 50 43 57 53 54 52 29 29 61 53 79 73 63 61 6c 6c PCWSTR))aSyscall
11da1 5b 37 33 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a [73].pCurrent)..
11da2 7d 3b 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 }; /* End of the
11da3 20 6f 76 65 72 72 69 64 65 61 62 6c 65 20 73 79 overrideable sy
11da4 73 74 65 6d 20 63 61 6c 6c 73 20 2a 2f 0a 0a 2f stem calls */../
11da5 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 *.** This is the
11da6 20 78 53 65 74 53 79 73 74 65 6d 43 61 6c 6c 28 xSetSystemCall(
11da7 29 20 6d 65 74 68 6f 64 20 6f 66 20 73 71 6c 69 ) method of sqli
11da8 74 65 33 5f 76 66 73 20 66 6f 72 20 61 6c 6c 20 te3_vfs for all
11da9 6f 66 20 74 68 65 0a 2a 2a 20 22 77 69 6e 33 32 of the.** "win32
11daa 22 20 56 46 53 65 73 2e 20 20 52 65 74 75 72 6e " VFSes. Return
11dab 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 70 6f 6e 20 SQLITE_OK opon
11dac 73 75 63 63 65 73 73 66 75 6c 6c 79 20 75 70 64 successfully upd
11dad 61 74 69 6e 67 20 74 68 65 0a 2a 2a 20 73 79 73 ating the.** sys
11dae 74 65 6d 20 63 61 6c 6c 20 70 6f 69 6e 74 65 72 tem call pointer
11daf 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 54 46 , or SQLITE_NOTF
11db0 4f 55 4e 44 20 69 66 20 74 68 65 72 65 20 69 73 OUND if there is
11db1 20 6e 6f 20 63 6f 6e 66 69 67 75 72 61 62 6c 65 no configurable
11db2 0a 2a 2a 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 .** system call
11db3 6e 61 6d 65 64 20 7a 4e 61 6d 65 2e 0a 2a 2f 0a named zName..*/.
11db4 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 65 static int winSe
11db5 74 53 79 73 74 65 6d 43 61 6c 6c 28 0a 20 20 73 tSystemCall(. s
11db6 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e 6f 74 qlite3_vfs *pNot
11db7 55 73 65 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 Used, /*
11db8 54 68 65 20 56 46 53 20 70 6f 69 6e 74 65 72 2e The VFS pointer.
11db9 20 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 Not used */.
11dba 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
11dbb 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e, /*
11dbc 20 4e 61 6d 65 20 6f 66 20 73 79 73 74 65 6d 20 Name of system
11dbd 63 61 6c 6c 20 74 6f 20 6f 76 65 72 72 69 64 65 call to override
11dbe 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 */. sqlite3_sy
11dbf 73 63 61 6c 6c 5f 70 74 72 20 70 4e 65 77 46 75 scall_ptr pNewFu
11dc0 6e 63 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 nc /* Pointer t
11dc1 6f 20 6e 65 77 20 73 79 73 74 65 6d 20 63 61 6c o new system cal
11dc2 6c 20 76 61 6c 75 65 20 2a 2f 0a 29 7b 0a 20 20 l value */.){.
11dc3 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a unsigned int i;.
11dc4 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
11dc5 45 5f 4e 4f 54 46 4f 55 4e 44 3b 0a 0a 20 20 55 E_NOTFOUND;.. U
11dc6 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
11dc7 70 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 pNotUsed);. if(
11dc8 20 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 zName==0 ){.
11dc9 20 2f 2a 20 49 66 20 6e 6f 20 7a 4e 61 6d 65 20 /* If no zName
11dca 69 73 20 67 69 76 65 6e 2c 20 72 65 73 74 6f 72 is given, restor
11dcb 65 20 61 6c 6c 20 73 79 73 74 65 6d 20 63 61 6c e all system cal
11dcc 6c 73 20 74 6f 20 74 68 65 69 72 20 64 65 66 61 ls to their defa
11dcd 75 6c 74 0a 20 20 20 20 2a 2a 20 73 65 74 74 69 ult. ** setti
11dce 6e 67 73 20 61 6e 64 20 72 65 74 75 72 6e 20 4e ngs and return N
11dcf 55 4c 4c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 ULL. */. r
11dd0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
11dd1 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 for(i=0; i<si
11dd2 7a 65 6f 66 28 61 53 79 73 63 61 6c 6c 29 2f 73 zeof(aSyscall)/s
11dd3 69 7a 65 6f 66 28 61 53 79 73 63 61 6c 6c 5b 30 izeof(aSyscall[0
11dd4 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ]); i++){.
11dd5 69 66 28 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e if( aSyscall[i].
11dd6 70 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 pDefault ){.
11dd7 20 20 20 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e aSyscall[i].
11dd8 70 43 75 72 72 65 6e 74 20 3d 20 61 53 79 73 63 pCurrent = aSysc
11dd9 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 6c 74 3b all[i].pDefault;
11dda 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
11ddb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 }else{. /* I
11ddc 66 20 7a 4e 61 6d 65 20 69 73 20 73 70 65 63 69 f zName is speci
11ddd 66 69 65 64 2c 20 6f 70 65 72 61 74 65 20 6f 6e fied, operate on
11dde 20 6f 6e 6c 79 20 74 68 65 20 6f 6e 65 20 73 79 only the one sy
11ddf 73 74 65 6d 20 63 61 6c 6c 0a 20 20 20 20 2a 2a stem call. **
11de0 20 73 70 65 63 69 66 69 65 64 2e 0a 20 20 20 20 specified..
11de1 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 */. for(i=0;
11de2 69 3c 73 69 7a 65 6f 66 28 61 53 79 73 63 61 6c i<sizeof(aSyscal
11de3 6c 29 2f 73 69 7a 65 6f 66 28 61 53 79 73 63 61 l)/sizeof(aSysca
11de4 6c 6c 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 ll[0]); i++){.
11de5 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a if( strcmp(z
11de6 4e 61 6d 65 2c 20 61 53 79 73 63 61 6c 6c 5b 69 Name, aSyscall[i
11de7 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 ].zName)==0 ){.
11de8 20 20 20 20 20 20 20 69 66 28 20 61 53 79 73 63 if( aSysc
11de9 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 6c 74 3d all[i].pDefault=
11dea 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
11deb 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 44 65 66 aSyscall[i].pDef
11dec 61 75 6c 74 20 3d 20 61 53 79 73 63 61 6c 6c 5b ault = aSyscall[
11ded 69 5d 2e 70 43 75 72 72 65 6e 74 3b 0a 20 20 20 i].pCurrent;.
11dee 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 }. r
11def 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
11df0 20 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 46 if( pNewF
11df1 75 6e 63 3d 3d 30 20 29 20 70 4e 65 77 46 75 6e unc==0 ) pNewFun
11df2 63 20 3d 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e c = aSyscall[i].
11df3 70 44 65 66 61 75 6c 74 3b 0a 20 20 20 20 20 20 pDefault;.
11df4 20 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 aSyscall[i].pC
11df5 75 72 72 65 6e 74 20 3d 20 70 4e 65 77 46 75 6e urrent = pNewFun
11df6 63 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b c;. break
11df7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
11df8 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
11df9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
11dfa 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 the value of a
11dfb 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 52 65 system call. Re
11dfc 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 7a 4e 61 turn NULL if zNa
11dfd 6d 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 72 me is not a.** r
11dfe 65 63 6f 67 6e 69 7a 65 64 20 73 79 73 74 65 6d ecognized system
11dff 20 63 61 6c 6c 20 6e 61 6d 65 2e 20 20 4e 55 4c call name. NUL
11e00 4c 20 69 73 20 61 6c 73 6f 20 72 65 74 75 72 6e L is also return
11e01 65 64 20 69 66 20 74 68 65 20 73 79 73 74 65 6d ed if the system
11e02 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 63 75 72 72 call.** is curr
11e03 65 6e 74 6c 79 20 75 6e 64 65 66 69 6e 65 64 2e ently undefined.
11e04 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 .*/.static sqlit
11e05 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 20 77 e3_syscall_ptr w
11e06 69 6e 47 65 74 53 79 73 74 65 6d 43 61 6c 6c 28 inGetSystemCall(
11e07 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
11e08 70 4e 6f 74 55 73 65 64 2c 0a 20 20 63 6f 6e 73 pNotUsed,. cons
11e09 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 0a 29 7b t char *zName.){
11e0a 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
11e0b 69 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 i;.. UNUSED_PAR
11e0c 41 4d 45 54 45 52 28 70 4e 6f 74 55 73 65 64 29 AMETER(pNotUsed)
11e0d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 ;. for(i=0; i<s
11e0e 69 7a 65 6f 66 28 61 53 79 73 63 61 6c 6c 29 2f izeof(aSyscall)/
11e0f 73 69 7a 65 6f 66 28 61 53 79 73 63 61 6c 6c 5b sizeof(aSyscall[
11e10 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 0]); i++){. i
11e11 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c f( strcmp(zName,
11e12 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 aSyscall[i].zNa
11e13 6d 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 me)==0 ) return
11e14 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 aSyscall[i].pCur
11e15 72 65 6e 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 rent;. }. retu
11e16 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn 0;.}../*.** R
11e17 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f eturn the name o
11e18 66 20 74 68 65 20 66 69 72 73 74 20 73 79 73 74 f the first syst
11e19 65 6d 20 63 61 6c 6c 20 61 66 74 65 72 20 7a 4e em call after zN
11e1a 61 6d 65 2e 20 20 49 66 20 7a 4e 61 6d 65 3d 3d ame. If zName==
11e1b 4e 55 4c 4c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 NULL.** then ret
11e1c 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 urn the name of
11e1d 74 68 65 20 66 69 72 73 74 20 73 79 73 74 65 6d the first system
11e1e 20 63 61 6c 6c 2e 20 20 52 65 74 75 72 6e 20 4e call. Return N
11e1f 55 4c 4c 20 69 66 20 7a 4e 61 6d 65 0a 2a 2a 20 ULL if zName.**
11e20 69 73 20 74 68 65 20 6c 61 73 74 20 73 79 73 74 is the last syst
11e21 65 6d 20 63 61 6c 6c 20 6f 72 20 69 66 20 7a 4e em call or if zN
11e22 61 6d 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6e ame is not the n
11e23 61 6d 65 20 6f 66 20 61 20 76 61 6c 69 64 0a 2a ame of a valid.*
11e24 2a 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e 0a 2a * system call..*
11e25 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 /.static const c
11e26 68 61 72 20 2a 77 69 6e 4e 65 78 74 53 79 73 74 har *winNextSyst
11e27 65 6d 43 61 6c 6c 28 73 71 6c 69 74 65 33 5f 76 emCall(sqlite3_v
11e28 66 73 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 fs *p, const cha
11e29 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 r *zName){. int
11e2a 20 69 20 3d 20 2d 31 3b 0a 0a 20 20 55 4e 55 53 i = -1;.. UNUS
11e2b 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29 3b ED_PARAMETER(p);
11e2c 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a . if( zName ){.
11e2d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 for(i=0; i<A
11e2e 72 72 61 79 53 69 7a 65 28 61 53 79 73 63 61 6c rraySize(aSyscal
11e2f 6c 29 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 l)-1; i++){.
11e30 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 if( strcmp(zNa
11e31 6d 65 2c 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e me, aSyscall[i].
11e32 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 zName)==0 ) brea
11e33 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 k;. }. }. f
11e34 6f 72 28 69 2b 2b 3b 20 69 3c 41 72 72 61 79 53 or(i++; i<ArrayS
11e35 69 7a 65 28 61 53 79 73 63 61 6c 6c 29 3b 20 69 ize(aSyscall); i
11e36 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 53 79 ++){. if( aSy
11e37 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 6e scall[i].pCurren
11e38 74 21 3d 30 20 29 20 72 65 74 75 72 6e 20 61 53 t!=0 ) return aS
11e39 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d 65 3b yscall[i].zName;
11e3a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
11e3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
11e3c 75 6e 63 74 69 6f 6e 20 6f 75 74 70 75 74 73 20 unction outputs
11e3d 74 68 65 20 73 70 65 63 69 66 69 65 64 20 28 41 the specified (A
11e3e 4e 53 49 29 20 73 74 72 69 6e 67 20 74 6f 20 74 NSI) string to t
11e3f 68 65 20 57 69 6e 33 32 20 64 65 62 75 67 67 65 he Win32 debugge
11e40 72 0a 2a 2a 20 28 69 66 20 61 76 61 69 6c 61 62 r.** (if availab
11e41 6c 65 29 2e 0a 2a 2f 0a 0a 53 51 4c 49 54 45 5f le)..*/..SQLITE_
11e42 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
11e43 5f 77 69 6e 33 32 5f 77 72 69 74 65 5f 64 65 62 _win32_write_deb
11e44 75 67 28 63 68 61 72 20 2a 7a 42 75 66 2c 20 69 ug(char *zBuf, i
11e45 6e 74 20 6e 42 75 66 29 7b 0a 20 20 63 68 61 72 nt nBuf){. char
11e46 20 7a 44 62 67 42 75 66 5b 53 51 4c 49 54 45 5f zDbgBuf[SQLITE_
11e47 57 49 4e 33 32 5f 44 42 47 5f 42 55 46 5f 53 49 WIN32_DBG_BUF_SI
11e48 5a 45 5d 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 20 ZE];. int nMin
11e49 3d 20 4d 49 4e 28 6e 42 75 66 2c 20 28 53 51 4c = MIN(nBuf, (SQL
11e4a 49 54 45 5f 57 49 4e 33 32 5f 44 42 47 5f 42 55 ITE_WIN32_DBG_BU
11e4b 46 5f 53 49 5a 45 20 2d 20 31 29 29 3b 20 2f 2a F_SIZE - 1)); /*
11e4c 20 6d 61 79 20 62 65 20 6e 65 67 61 74 69 76 65 may be negative
11e4d 2e 20 2a 2f 0a 20 20 69 66 28 20 6e 4d 69 6e 3c . */. if( nMin<
11e4e 2d 31 20 29 20 6e 4d 69 6e 20 3d 20 2d 31 3b 20 -1 ) nMin = -1;
11e4f 2f 2a 20 61 6c 6c 20 6e 65 67 61 74 69 76 65 20 /* all negative
11e50 76 61 6c 75 65 73 20 62 65 63 6f 6d 65 20 2d 31 values become -1
11e51 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e . */. assert( n
11e52 4d 69 6e 3d 3d 2d 31 20 7c 7c 20 6e 4d 69 6e 3d Min==-1 || nMin=
11e53 3d 30 20 7c 7c 20 6e 4d 69 6e 3c 53 51 4c 49 54 =0 || nMin<SQLIT
11e54 45 5f 57 49 4e 33 32 5f 44 42 47 5f 42 55 46 5f E_WIN32_DBG_BUF_
11e55 53 49 5a 45 20 29 3b 0a 23 69 66 20 64 65 66 69 SIZE );.#if defi
11e56 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 ned(SQLITE_WIN32
11e57 5f 48 41 53 5f 41 4e 53 49 29 0a 20 20 69 66 28 _HAS_ANSI). if(
11e58 20 6e 4d 69 6e 3e 30 20 29 7b 0a 20 20 20 20 6d nMin>0 ){. m
11e59 65 6d 73 65 74 28 7a 44 62 67 42 75 66 2c 20 30 emset(zDbgBuf, 0
11e5a 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 44 , SQLITE_WIN32_D
11e5b 42 47 5f 42 55 46 5f 53 49 5a 45 29 3b 0a 20 20 BG_BUF_SIZE);.
11e5c 20 20 6d 65 6d 63 70 79 28 7a 44 62 67 42 75 66 memcpy(zDbgBuf
11e5d 2c 20 7a 42 75 66 2c 20 6e 4d 69 6e 29 3b 0a 20 , zBuf, nMin);.
11e5e 20 20 20 6f 73 4f 75 74 70 75 74 44 65 62 75 67 osOutputDebug
11e5f 53 74 72 69 6e 67 41 28 7a 44 62 67 42 75 66 29 StringA(zDbgBuf)
11e60 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f ;. }else{. o
11e61 73 4f 75 74 70 75 74 44 65 62 75 67 53 74 72 69 sOutputDebugStri
11e62 6e 67 41 28 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 ngA(zBuf);. }.#
11e63 65 6c 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c elif defined(SQL
11e64 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49 ITE_WIN32_HAS_WI
11e65 44 45 29 0a 20 20 6d 65 6d 73 65 74 28 7a 44 62 DE). memset(zDb
11e66 67 42 75 66 2c 20 30 2c 20 53 51 4c 49 54 45 5f gBuf, 0, SQLITE_
11e67 57 49 4e 33 32 5f 44 42 47 5f 42 55 46 5f 53 49 WIN32_DBG_BUF_SI
11e68 5a 45 29 3b 0a 20 20 69 66 20 28 20 6f 73 4d 75 ZE);. if ( osMu
11e69 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61 ltiByteToWideCha
11e6a 72 28 0a 20 20 20 20 20 20 20 20 20 20 6f 73 41 r(. osA
11e6b 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 reFileApisANSI()
11e6c 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f ? CP_ACP : CP_O
11e6d 45 4d 43 50 2c 20 30 2c 20 7a 42 75 66 2c 0a 20 EMCP, 0, zBuf,.
11e6e 20 20 20 20 20 20 20 20 20 6e 4d 69 6e 2c 20 28 nMin, (
11e6f 4c 50 57 53 54 52 29 7a 44 62 67 42 75 66 2c 20 LPWSTR)zDbgBuf,
11e70 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 44 42 47 SQLITE_WIN32_DBG
11e71 5f 42 55 46 5f 53 49 5a 45 2f 73 69 7a 65 6f 66 _BUF_SIZE/sizeof
11e72 28 57 43 48 41 52 29 29 3c 3d 30 20 29 7b 0a 20 (WCHAR))<=0 ){.
11e73 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
11e74 20 6f 73 4f 75 74 70 75 74 44 65 62 75 67 53 74 osOutputDebugSt
11e75 72 69 6e 67 57 28 28 4c 50 43 57 53 54 52 29 7a ringW((LPCWSTR)z
11e76 44 62 67 42 75 66 29 3b 0a 23 65 6c 73 65 0a 20 DbgBuf);.#else.
11e77 20 69 66 28 20 6e 4d 69 6e 3e 30 20 29 7b 0a 20 if( nMin>0 ){.
11e78 20 20 20 6d 65 6d 73 65 74 28 7a 44 62 67 42 75 memset(zDbgBu
11e79 66 2c 20 30 2c 20 53 51 4c 49 54 45 5f 57 49 4e f, 0, SQLITE_WIN
11e7a 33 32 5f 44 42 47 5f 42 55 46 5f 53 49 5a 45 29 32_DBG_BUF_SIZE)
11e7b 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 44 62 ;. memcpy(zDb
11e7c 67 42 75 66 2c 20 7a 42 75 66 2c 20 6e 4d 69 6e gBuf, zBuf, nMin
11e7d 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 );. fprintf(s
11e7e 74 64 65 72 72 2c 20 22 25 73 22 2c 20 7a 44 62 tderr, "%s", zDb
11e7f 67 42 75 66 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a gBuf);. }else{.
11e80 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 fprintf(stde
11e81 72 72 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b rr, "%s", zBuf);
11e82 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f . }.#endif.}../
11e83 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
11e84 6e 67 20 72 6f 75 74 69 6e 65 20 73 75 73 70 65 ng routine suspe
11e85 6e 64 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 nds the current
11e86 74 68 72 65 61 64 20 66 6f 72 20 61 74 20 6c 65 thread for at le
11e87 61 73 74 20 6d 73 0a 2a 2a 20 6d 69 6c 6c 69 73 ast ms.** millis
11e88 65 63 6f 6e 64 73 2e 20 20 54 68 69 73 20 69 73 econds. This is
11e89 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 equivalent to t
11e8a 68 65 20 57 69 6e 33 32 20 53 6c 65 65 70 28 29 he Win32 Sleep()
11e8b 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 interface..*/.#
11e8c 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
11e8d 52 54 0a 73 74 61 74 69 63 20 48 41 4e 44 4c 45 RT.static HANDLE
11e8e 20 73 6c 65 65 70 4f 62 6a 20 3d 20 4e 55 4c 4c sleepObj = NULL
11e8f 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 ;.#endif..SQLITE
11e90 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
11e91 33 5f 77 69 6e 33 32 5f 73 6c 65 65 70 28 44 57 3_win32_sleep(DW
11e92 4f 52 44 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 ORD milliseconds
11e93 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 ){.#if SQLITE_OS
11e94 5f 57 49 4e 52 54 0a 20 20 69 66 20 28 20 73 6c _WINRT. if ( sl
11e95 65 65 70 4f 62 6a 3d 3d 4e 55 4c 4c 20 29 7b 0a eepObj==NULL ){.
11e96 20 20 20 20 73 6c 65 65 70 4f 62 6a 20 3d 20 6f sleepObj = o
11e97 73 43 72 65 61 74 65 45 76 65 6e 74 45 78 57 28 sCreateEventExW(
11e98 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 43 52 45 41 NULL, NULL, CREA
11e99 54 45 5f 45 56 45 4e 54 5f 4d 41 4e 55 41 4c 5f TE_EVENT_MANUAL_
11e9a 52 45 53 45 54 2c 0a 20 20 20 20 20 20 20 20 20 RESET,.
11e9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11e9c 20 20 20 20 20 20 20 53 59 4e 43 48 52 4f 4e 49 SYNCHRONI
11e9d 5a 45 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 ZE);. }. asser
11e9e 74 28 20 73 6c 65 65 70 4f 62 6a 21 3d 4e 55 4c t( sleepObj!=NUL
11e9f 4c 20 29 3b 0a 20 20 6f 73 57 61 69 74 46 6f 72 L );. osWaitFor
11ea0 53 69 6e 67 6c 65 4f 62 6a 65 63 74 45 78 28 73 SingleObjectEx(s
11ea1 6c 65 65 70 4f 62 6a 2c 20 6d 69 6c 6c 69 73 65 leepObj, millise
11ea2 63 6f 6e 64 73 2c 20 46 41 4c 53 45 29 3b 0a 23 conds, FALSE);.#
11ea3 65 6c 73 65 0a 20 20 6f 73 53 6c 65 65 70 28 6d else. osSleep(m
11ea4 69 6c 6c 69 73 65 63 6f 6e 64 73 29 3b 0a 23 65 illiseconds);.#e
11ea5 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ndif.}../*.** Re
11ea6 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a turn true (non-z
11ea7 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 20 72 ero) if we are r
11ea8 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 69 6e unning under Win
11ea9 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50 NT, Win2K, WinXP
11eaa 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20 ,.** or WinCE.
11eab 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 7a 65 Return false (ze
11eac 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c 20 57 ro) for Win95, W
11ead 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a in98, or WinME..
11eae 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 61 6e **.** Here is an
11eaf 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f 62 73 interesting obs
11eb0 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35 ervation: Win95
11eb1 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e , Win98, and Win
11eb2 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c ME lack.** the L
11eb3 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e ockFileEx() API.
11eb4 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 74 69 But we can sti
11eb5 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 ll statically li
11eb6 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74 0a nk against that.
11eb7 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20 61 ** API as long a
11eb8 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20 s we don't call
11eb9 69 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 it when running
11eba 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20 Win95/98/ME. A
11ebb 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 20 call to.** this
11ebc 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
11ebd 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 to determine if
11ebe 74 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39 the host is Win9
11ebf 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 5/98/ME or.** Wi
11ec0 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61 nNT/2K/XP so tha
11ec1 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 t we will know w
11ec2 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 65 hether or not we
11ec3 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c can safely call
11ec4 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 .** the LockFile
11ec5 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 Ex() API..*/.#if
11ec6 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
11ec7 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 || SQLITE_OS_WI
11ec8 4e 52 54 0a 23 20 64 65 66 69 6e 65 20 69 73 4e NRT.# define isN
11ec9 54 28 29 20 20 28 31 29 0a 23 65 6c 69 66 20 21 T() (1).#elif !
11eca 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 defined(SQLITE_W
11ecb 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a 23 IN32_HAS_WIDE).#
11ecc 20 64 65 66 69 6e 65 20 69 73 4e 54 28 29 20 20 define isNT()
11ecd 28 30 29 0a 23 65 6c 73 65 0a 20 20 73 74 61 74 (0).#else. stat
11ece 69 63 20 69 6e 74 20 69 73 4e 54 28 76 6f 69 64 ic int isNT(void
11ecf 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
11ed0 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 30 20 29 7b e3_os_type==0 ){
11ed1 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e . OSVERSION
11ed2 49 4e 46 4f 41 20 73 49 6e 66 6f 3b 0a 20 20 20 INFOA sInfo;.
11ed3 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 sInfo.dwOSVer
11ed4 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 sionInfoSize = s
11ed5 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 izeof(sInfo);.
11ed6 20 20 20 20 6f 73 47 65 74 56 65 72 73 69 6f 6e osGetVersion
11ed7 45 78 41 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20 ExA(&sInfo);.
11ed8 20 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 sqlite3_os_ty
11ed9 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 pe = sInfo.dwPla
11eda 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 tformId==VER_PLA
11edb 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f TFORM_WIN32_NT ?
11edc 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 2 : 1;. }.
11edd 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
11ede 5f 6f 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20 7d _os_type==2;. }
11edf 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
11ee0 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 4d 41 4c SQLITE_WIN32_MAL
11ee1 4c 4f 43 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 LOC./*.** Alloca
11ee2 74 65 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d te nBytes of mem
11ee3 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ory..*/.static v
11ee4 6f 69 64 20 2a 77 69 6e 4d 65 6d 4d 61 6c 6c 6f oid *winMemMallo
11ee5 63 28 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 c(int nBytes){.
11ee6 20 48 41 4e 44 4c 45 20 68 48 65 61 70 3b 0a 20 HANDLE hHeap;.
11ee7 20 76 6f 69 64 20 2a 70 3b 0a 0a 20 20 77 69 6e void *p;.. win
11ee8 4d 65 6d 41 73 73 65 72 74 4d 61 67 69 63 28 29 MemAssertMagic()
11ee9 3b 0a 20 20 68 48 65 61 70 20 3d 20 77 69 6e 4d ;. hHeap = winM
11eea 65 6d 47 65 74 48 65 61 70 28 29 3b 0a 20 20 61 emGetHeap();. a
11eeb 73 73 65 72 74 28 20 68 48 65 61 70 21 3d 30 20 ssert( hHeap!=0
11eec 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 48 65 );. assert( hHe
11eed 61 70 21 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 ap!=INVALID_HAND
11eee 4c 45 5f 56 41 4c 55 45 20 29 3b 0a 23 69 66 20 LE_VALUE );.#if
11eef 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 !SQLITE_OS_WINRT
11ef0 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 && defined(SQLI
11ef1 54 45 5f 57 49 4e 33 32 5f 4d 41 4c 4c 4f 43 5f TE_WIN32_MALLOC_
11ef2 56 41 4c 49 44 41 54 45 29 0a 20 20 61 73 73 65 VALIDATE). asse
11ef3 72 74 20 28 20 6f 73 48 65 61 70 56 61 6c 69 64 rt ( osHeapValid
11ef4 61 74 65 28 68 48 65 61 70 2c 20 53 51 4c 49 54 ate(hHeap, SQLIT
11ef5 45 5f 57 49 4e 33 32 5f 48 45 41 50 5f 46 4c 41 E_WIN32_HEAP_FLA
11ef6 47 53 2c 20 4e 55 4c 4c 29 20 29 3b 0a 23 65 6e GS, NULL) );.#en
11ef7 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 6e 42 dif. assert( nB
11ef8 79 74 65 73 3e 3d 30 20 29 3b 0a 20 20 70 20 3d ytes>=0 );. p =
11ef9 20 6f 73 48 65 61 70 41 6c 6c 6f 63 28 68 48 65 osHeapAlloc(hHe
11efa 61 70 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 ap, SQLITE_WIN32
11efb 5f 48 45 41 50 5f 46 4c 41 47 53 2c 20 28 53 49 _HEAP_FLAGS, (SI
11efc 5a 45 5f 54 29 6e 42 79 74 65 73 29 3b 0a 20 20 ZE_T)nBytes);.
11efd 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 73 71 if( !p ){. sq
11efe 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54 45 lite3_log(SQLITE
11eff 5f 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c 65 64 20 _NOMEM, "failed
11f00 74 6f 20 48 65 61 70 41 6c 6c 6f 63 20 25 75 20 to HeapAlloc %u
11f01 62 79 74 65 73 20 28 25 64 29 2c 20 68 65 61 70 bytes (%d), heap
11f02 3d 25 70 22 2c 0a 20 20 20 20 20 20 20 20 20 20 =%p",.
11f03 20 20 20 20 20 20 6e 42 79 74 65 73 2c 20 6f 73 nBytes, os
11f04 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 2c 20 GetLastError(),
11f05 28 76 6f 69 64 2a 29 68 48 65 61 70 29 3b 0a 20 (void*)hHeap);.
11f06 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
11f07 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d ../*.** Free mem
11f08 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ory..*/.static v
11f09 6f 69 64 20 77 69 6e 4d 65 6d 46 72 65 65 28 76 oid winMemFree(v
11f0a 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 oid *pPrior){.
11f0b 48 41 4e 44 4c 45 20 68 48 65 61 70 3b 0a 0a 20 HANDLE hHeap;..
11f0c 20 77 69 6e 4d 65 6d 41 73 73 65 72 74 4d 61 67 winMemAssertMag
11f0d 69 63 28 29 3b 0a 20 20 68 48 65 61 70 20 3d 20 ic();. hHeap =
11f0e 77 69 6e 4d 65 6d 47 65 74 48 65 61 70 28 29 3b winMemGetHeap();
11f0f 0a 20 20 61 73 73 65 72 74 28 20 68 48 65 61 70 . assert( hHeap
11f10 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
11f11 20 68 48 65 61 70 21 3d 49 4e 56 41 4c 49 44 5f hHeap!=INVALID_
11f12 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 3b 0a HANDLE_VALUE );.
11f13 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 #if !SQLITE_OS_W
11f14 49 4e 52 54 20 26 26 20 64 65 66 69 6e 65 64 28 INRT && defined(
11f15 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 4d 41 4c SQLITE_WIN32_MAL
11f16 4c 4f 43 5f 56 41 4c 49 44 41 54 45 29 0a 20 20 LOC_VALIDATE).
11f17 61 73 73 65 72 74 20 28 20 6f 73 48 65 61 70 56 assert ( osHeapV
11f18 61 6c 69 64 61 74 65 28 68 48 65 61 70 2c 20 53 alidate(hHeap, S
11f19 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41 50 QLITE_WIN32_HEAP
11f1a 5f 46 4c 41 47 53 2c 20 70 50 72 69 6f 72 29 20 _FLAGS, pPrior)
11f1b 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 );.#endif. if(
11f1c 21 70 50 72 69 6f 72 20 29 20 72 65 74 75 72 6e !pPrior ) return
11f1d 3b 20 2f 2a 20 50 61 73 73 69 6e 67 20 4e 55 4c ; /* Passing NUL
11f1e 4c 20 74 6f 20 48 65 61 70 46 72 65 65 20 69 73 L to HeapFree is
11f1f 20 75 6e 64 65 66 69 6e 65 64 2e 20 2a 2f 0a 20 undefined. */.
11f20 20 69 66 28 20 21 6f 73 48 65 61 70 46 72 65 65 if( !osHeapFree
11f21 28 68 48 65 61 70 2c 20 53 51 4c 49 54 45 5f 57 (hHeap, SQLITE_W
11f22 49 4e 33 32 5f 48 45 41 50 5f 46 4c 41 47 53 2c IN32_HEAP_FLAGS,
11f23 20 70 50 72 69 6f 72 29 20 29 7b 0a 20 20 20 20 pPrior) ){.
11f24 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 sqlite3_log(SQLI
11f25 54 45 5f 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c 65 TE_NOMEM, "faile
11f26 64 20 74 6f 20 48 65 61 70 46 72 65 65 20 62 6c d to HeapFree bl
11f27 6f 63 6b 20 25 70 20 28 25 64 29 2c 20 68 65 61 ock %p (%d), hea
11f28 70 3d 25 70 22 2c 0a 20 20 20 20 20 20 20 20 20 p=%p",.
11f29 20 20 20 20 20 20 20 70 50 72 69 6f 72 2c 20 6f pPrior, o
11f2a 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 2c sGetLastError(),
11f2b 20 28 76 6f 69 64 2a 29 68 48 65 61 70 29 3b 0a (void*)hHeap);.
11f2c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 }.}../*.** Cha
11f2d 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 nge the size of
11f2e 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f an existing memo
11f2f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f ry allocation.*/
11f30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 77 69 .static void *wi
11f31 6e 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64 nMemRealloc(void
11f32 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 *pPrior, int nB
11f33 79 74 65 73 29 7b 0a 20 20 48 41 4e 44 4c 45 20 ytes){. HANDLE
11f34 68 48 65 61 70 3b 0a 20 20 76 6f 69 64 20 2a 70 hHeap;. void *p
11f35 3b 0a 0a 20 20 77 69 6e 4d 65 6d 41 73 73 65 72 ;.. winMemAsser
11f36 74 4d 61 67 69 63 28 29 3b 0a 20 20 68 48 65 61 tMagic();. hHea
11f37 70 20 3d 20 77 69 6e 4d 65 6d 47 65 74 48 65 61 p = winMemGetHea
11f38 70 28 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 p();. assert( h
11f39 48 65 61 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 Heap!=0 );. ass
11f3a 65 72 74 28 20 68 48 65 61 70 21 3d 49 4e 56 41 ert( hHeap!=INVA
11f3b 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 LID_HANDLE_VALUE
11f3c 20 29 3b 0a 23 69 66 20 21 53 51 4c 49 54 45 5f );.#if !SQLITE_
11f3d 4f 53 5f 57 49 4e 52 54 20 26 26 20 64 65 66 69 OS_WINRT && defi
11f3e 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 ned(SQLITE_WIN32
11f3f 5f 4d 41 4c 4c 4f 43 5f 56 41 4c 49 44 41 54 45 _MALLOC_VALIDATE
11f40 29 0a 20 20 61 73 73 65 72 74 20 28 20 6f 73 48 ). assert ( osH
11f41 65 61 70 56 61 6c 69 64 61 74 65 28 68 48 65 61 eapValidate(hHea
11f42 70 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f p, SQLITE_WIN32_
11f43 48 45 41 50 5f 46 4c 41 47 53 2c 20 70 50 72 69 HEAP_FLAGS, pPri
11f44 6f 72 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 or) );.#endif.
11f45 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e 3d assert( nBytes>=
11f46 30 20 29 3b 0a 20 20 69 66 28 20 21 70 50 72 69 0 );. if( !pPri
11f47 6f 72 20 29 7b 0a 20 20 20 20 70 20 3d 20 6f 73 or ){. p = os
11f48 48 65 61 70 41 6c 6c 6f 63 28 68 48 65 61 70 2c HeapAlloc(hHeap,
11f49 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 SQLITE_WIN32_HE
11f4a 41 50 5f 46 4c 41 47 53 2c 20 28 53 49 5a 45 5f AP_FLAGS, (SIZE_
11f4b 54 29 6e 42 79 74 65 73 29 3b 0a 20 20 7d 65 6c T)nBytes);. }el
11f4c 73 65 7b 0a 20 20 20 20 70 20 3d 20 6f 73 48 65 se{. p = osHe
11f4d 61 70 52 65 41 6c 6c 6f 63 28 68 48 65 61 70 2c apReAlloc(hHeap,
11f4e 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 SQLITE_WIN32_HE
11f4f 41 50 5f 46 4c 41 47 53 2c 20 70 50 72 69 6f 72 AP_FLAGS, pPrior
11f50 2c 20 28 53 49 5a 45 5f 54 29 6e 42 79 74 65 73 , (SIZE_T)nBytes
11f51 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 20 );. }. if( !p
11f52 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c ){. sqlite3_l
11f53 6f 67 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c og(SQLITE_NOMEM,
11f54 20 22 66 61 69 6c 65 64 20 74 6f 20 25 73 20 25 "failed to %s %
11f55 75 20 62 79 74 65 73 20 28 25 64 29 2c 20 68 65 u bytes (%d), he
11f56 61 70 3d 25 70 22 2c 0a 20 20 20 20 20 20 20 20 ap=%p",.
11f57 20 20 20 20 20 20 20 20 70 50 72 69 6f 72 20 3f pPrior ?
11f58 20 22 48 65 61 70 52 65 41 6c 6c 6f 63 22 20 3a "HeapReAlloc" :
11f59 20 22 48 65 61 70 41 6c 6c 6f 63 22 2c 20 6e 42 "HeapAlloc", nB
11f5a 79 74 65 73 2c 20 6f 73 47 65 74 4c 61 73 74 45 ytes, osGetLastE
11f5b 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20 20 rror(),.
11f5c 20 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 68 (void*)h
11f5d 48 65 61 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Heap);. }. ret
11f5e 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
11f5f 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
11f60 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e of an outstandin
11f61 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e g allocation, in
11f62 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 bytes..*/.stati
11f63 63 20 69 6e 74 20 77 69 6e 4d 65 6d 53 69 7a 65 c int winMemSize
11f64 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 48 41 4e (void *p){. HAN
11f65 44 4c 45 20 68 48 65 61 70 3b 0a 20 20 53 49 5a DLE hHeap;. SIZ
11f66 45 5f 54 20 6e 3b 0a 0a 20 20 77 69 6e 4d 65 6d E_T n;.. winMem
11f67 41 73 73 65 72 74 4d 61 67 69 63 28 29 3b 0a 20 AssertMagic();.
11f68 20 68 48 65 61 70 20 3d 20 77 69 6e 4d 65 6d 47 hHeap = winMemG
11f69 65 74 48 65 61 70 28 29 3b 0a 20 20 61 73 73 65 etHeap();. asse
11f6a 72 74 28 20 68 48 65 61 70 21 3d 30 20 29 3b 0a rt( hHeap!=0 );.
11f6b 20 20 61 73 73 65 72 74 28 20 68 48 65 61 70 21 assert( hHeap!
11f6c 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f =INVALID_HANDLE_
11f6d 56 41 4c 55 45 20 29 3b 0a 23 69 66 20 21 53 51 VALUE );.#if !SQ
11f6e 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26 LITE_OS_WINRT &&
11f6f 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
11f70 57 49 4e 33 32 5f 4d 41 4c 4c 4f 43 5f 56 41 4c WIN32_MALLOC_VAL
11f71 49 44 41 54 45 29 0a 20 20 61 73 73 65 72 74 20 IDATE). assert
11f72 28 20 6f 73 48 65 61 70 56 61 6c 69 64 61 74 65 ( osHeapValidate
11f73 28 68 48 65 61 70 2c 20 53 51 4c 49 54 45 5f 57 (hHeap, SQLITE_W
11f74 49 4e 33 32 5f 48 45 41 50 5f 46 4c 41 47 53 2c IN32_HEAP_FLAGS,
11f75 20 4e 55 4c 4c 29 20 29 3b 0a 23 65 6e 64 69 66 NULL) );.#endif
11f76 0a 20 20 69 66 28 20 21 70 20 29 20 72 65 74 75 . if( !p ) retu
11f77 72 6e 20 30 3b 0a 20 20 6e 20 3d 20 6f 73 48 65 rn 0;. n = osHe
11f78 61 70 53 69 7a 65 28 68 48 65 61 70 2c 20 53 51 apSize(hHeap, SQ
11f79 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41 50 5f LITE_WIN32_HEAP_
11f7a 46 4c 41 47 53 2c 20 70 29 3b 0a 20 20 69 66 28 FLAGS, p);. if(
11f7b 20 6e 3d 3d 28 53 49 5a 45 5f 54 29 2d 31 20 29 n==(SIZE_T)-1 )
11f7c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f {. sqlite3_lo
11f7d 67 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 g(SQLITE_NOMEM,
11f7e 22 66 61 69 6c 65 64 20 74 6f 20 48 65 61 70 53 "failed to HeapS
11f7f 69 7a 65 20 62 6c 6f 63 6b 20 25 70 20 28 25 64 ize block %p (%d
11f80 29 2c 20 68 65 61 70 3d 25 70 22 2c 0a 20 20 20 ), heap=%p",.
11f81 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2c 20 p,
11f82 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 osGetLastError()
11f83 2c 20 28 76 6f 69 64 2a 29 68 48 65 61 70 29 3b , (void*)hHeap);
11f84 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
11f85 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 }. return (int
11f86 29 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 )n;.}../*.** Rou
11f87 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 nd up a request
11f88 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 size to the next
11f89 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f valid allocatio
11f8a 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 n size..*/.stati
11f8b 63 20 69 6e 74 20 77 69 6e 4d 65 6d 52 6f 75 6e c int winMemRoun
11f8c 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65 dup(int n){. re
11f8d 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn n;.}../*.**
11f8e 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 Initialize this
11f8f 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 module..*/.stat
11f90 69 63 20 69 6e 74 20 77 69 6e 4d 65 6d 49 6e 69 ic int winMemIni
11f91 74 28 76 6f 69 64 20 2a 70 41 70 70 44 61 74 61 t(void *pAppData
11f92 29 7b 0a 20 20 77 69 6e 4d 65 6d 44 61 74 61 20 ){. winMemData
11f93 2a 70 57 69 6e 4d 65 6d 44 61 74 61 20 3d 20 28 *pWinMemData = (
11f94 77 69 6e 4d 65 6d 44 61 74 61 20 2a 29 70 41 70 winMemData *)pAp
11f95 70 44 61 74 61 3b 0a 0a 20 20 69 66 28 20 21 70 pData;.. if( !p
11f96 57 69 6e 4d 65 6d 44 61 74 61 20 29 20 72 65 74 WinMemData ) ret
11f97 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
11f98 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 69 6e ;. assert( pWin
11f99 4d 65 6d 44 61 74 61 2d 3e 6d 61 67 69 63 3d 3d MemData->magic==
11f9a 57 49 4e 4d 45 4d 5f 4d 41 47 49 43 20 29 3b 0a WINMEM_MAGIC );.
11f9b 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f .#if !SQLITE_OS_
11f9c 57 49 4e 52 54 20 26 26 20 53 51 4c 49 54 45 5f WINRT && SQLITE_
11f9d 57 49 4e 33 32 5f 48 45 41 50 5f 43 52 45 41 54 WIN32_HEAP_CREAT
11f9e 45 0a 20 20 69 66 28 20 21 70 57 69 6e 4d 65 6d E. if( !pWinMem
11f9f 44 61 74 61 2d 3e 68 48 65 61 70 20 29 7b 0a 20 Data->hHeap ){.
11fa0 20 20 20 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e pWinMemData->
11fa1 68 48 65 61 70 20 3d 20 6f 73 48 65 61 70 43 72 hHeap = osHeapCr
11fa2 65 61 74 65 28 53 51 4c 49 54 45 5f 57 49 4e 33 eate(SQLITE_WIN3
11fa3 32 5f 48 45 41 50 5f 46 4c 41 47 53 2c 0a 20 20 2_HEAP_FLAGS,.
11fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11fa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11fa6 20 20 20 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 SQLITE_WIN32
11fa7 5f 48 45 41 50 5f 49 4e 49 54 5f 53 49 5a 45 2c _HEAP_INIT_SIZE,
11fa8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
11fa9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11faa 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 57 49 SQLITE_WI
11fab 4e 33 32 5f 48 45 41 50 5f 4d 41 58 5f 53 49 5a N32_HEAP_MAX_SIZ
11fac 45 29 3b 0a 20 20 20 20 69 66 28 20 21 70 57 69 E);. if( !pWi
11fad 6e 4d 65 6d 44 61 74 61 2d 3e 68 48 65 61 70 20 nMemData->hHeap
11fae 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
11faf 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 _log(SQLITE_NOME
11fb0 4d 2c 0a 20 20 20 20 20 20 20 20 20 20 22 66 61 M,. "fa
11fb1 69 6c 65 64 20 74 6f 20 48 65 61 70 43 72 65 61 iled to HeapCrea
11fb2 74 65 20 28 25 64 29 2c 20 66 6c 61 67 73 3d 25 te (%d), flags=%
11fb3 75 2c 20 69 6e 69 74 53 69 7a 65 3d 25 75 2c 20 u, initSize=%u,
11fb4 6d 61 78 53 69 7a 65 3d 25 75 22 2c 0a 20 20 20 maxSize=%u",.
11fb5 20 20 20 20 20 20 20 6f 73 47 65 74 4c 61 73 74 osGetLast
11fb6 45 72 72 6f 72 28 29 2c 20 53 51 4c 49 54 45 5f Error(), SQLITE_
11fb7 57 49 4e 33 32 5f 48 45 41 50 5f 46 4c 41 47 53 WIN32_HEAP_FLAGS
11fb8 2c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 ,. SQLI
11fb9 54 45 5f 57 49 4e 33 32 5f 48 45 41 50 5f 49 4e TE_WIN32_HEAP_IN
11fba 49 54 5f 53 49 5a 45 2c 20 53 51 4c 49 54 45 5f IT_SIZE, SQLITE_
11fbb 57 49 4e 33 32 5f 48 45 41 50 5f 4d 41 58 5f 53 WIN32_HEAP_MAX_S
11fbc 49 5a 45 29 3b 0a 20 20 20 20 20 20 72 65 74 75 IZE);. retu
11fbd 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
11fbe 0a 20 20 20 20 7d 0a 20 20 20 20 70 57 69 6e 4d . }. pWinM
11fbf 65 6d 44 61 74 61 2d 3e 62 4f 77 6e 65 64 20 3d emData->bOwned =
11fc0 20 54 52 55 45 3b 0a 20 20 20 20 61 73 73 65 72 TRUE;. asser
11fc1 74 28 20 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e t( pWinMemData->
11fc2 62 4f 77 6e 65 64 20 29 3b 0a 20 20 7d 0a 23 65 bOwned );. }.#e
11fc3 6c 73 65 0a 20 20 70 57 69 6e 4d 65 6d 44 61 74 lse. pWinMemDat
11fc4 61 2d 3e 68 48 65 61 70 20 3d 20 6f 73 47 65 74 a->hHeap = osGet
11fc5 50 72 6f 63 65 73 73 48 65 61 70 28 29 3b 0a 20 ProcessHeap();.
11fc6 20 69 66 28 20 21 70 57 69 6e 4d 65 6d 44 61 74 if( !pWinMemDat
11fc7 61 2d 3e 68 48 65 61 70 20 29 7b 0a 20 20 20 20 a->hHeap ){.
11fc8 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 sqlite3_log(SQLI
11fc9 54 45 5f 4e 4f 4d 45 4d 2c 0a 20 20 20 20 20 20 TE_NOMEM,.
11fca 20 20 22 66 61 69 6c 65 64 20 74 6f 20 47 65 74 "failed to Get
11fcb 50 72 6f 63 65 73 73 48 65 61 70 20 28 25 64 29 ProcessHeap (%d)
11fcc 22 2c 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f ", osGetLastErro
11fcd 72 28 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e r());. return
11fce 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
11fcf 20 7d 0a 20 20 70 57 69 6e 4d 65 6d 44 61 74 61 }. pWinMemData
11fd0 2d 3e 62 4f 77 6e 65 64 20 3d 20 46 41 4c 53 45 ->bOwned = FALSE
11fd1 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 57 69 ;. assert( !pWi
11fd2 6e 4d 65 6d 44 61 74 61 2d 3e 62 4f 77 6e 65 64 nMemData->bOwned
11fd3 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 );.#endif. ass
11fd4 65 72 74 28 20 70 57 69 6e 4d 65 6d 44 61 74 61 ert( pWinMemData
11fd5 2d 3e 68 48 65 61 70 21 3d 30 20 29 3b 0a 20 20 ->hHeap!=0 );.
11fd6 61 73 73 65 72 74 28 20 70 57 69 6e 4d 65 6d 44 assert( pWinMemD
11fd7 61 74 61 2d 3e 68 48 65 61 70 21 3d 49 4e 56 41 ata->hHeap!=INVA
11fd8 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 LID_HANDLE_VALUE
11fd9 20 29 3b 0a 23 69 66 20 21 53 51 4c 49 54 45 5f );.#if !SQLITE_
11fda 4f 53 5f 57 49 4e 52 54 20 26 26 20 64 65 66 69 OS_WINRT && defi
11fdb 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 ned(SQLITE_WIN32
11fdc 5f 4d 41 4c 4c 4f 43 5f 56 41 4c 49 44 41 54 45 _MALLOC_VALIDATE
11fdd 29 0a 20 20 61 73 73 65 72 74 28 20 6f 73 48 65 ). assert( osHe
11fde 61 70 56 61 6c 69 64 61 74 65 28 70 57 69 6e 4d apValidate(pWinM
11fdf 65 6d 44 61 74 61 2d 3e 68 48 65 61 70 2c 20 53 emData->hHeap, S
11fe0 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41 50 QLITE_WIN32_HEAP
11fe1 5f 46 4c 41 47 53 2c 20 4e 55 4c 4c 29 20 29 3b _FLAGS, NULL) );
11fe2 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
11fe3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
11fe4 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a *.** Deinitializ
11fe5 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a e this module..*
11fe6 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 /.static void wi
11fe7 6e 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 nMemShutdown(voi
11fe8 64 20 2a 70 41 70 70 44 61 74 61 29 7b 0a 20 20 d *pAppData){.
11fe9 77 69 6e 4d 65 6d 44 61 74 61 20 2a 70 57 69 6e winMemData *pWin
11fea 4d 65 6d 44 61 74 61 20 3d 20 28 77 69 6e 4d 65 MemData = (winMe
11feb 6d 44 61 74 61 20 2a 29 70 41 70 70 44 61 74 61 mData *)pAppData
11fec 3b 0a 0a 20 20 69 66 28 20 21 70 57 69 6e 4d 65 ;.. if( !pWinMe
11fed 6d 44 61 74 61 20 29 20 72 65 74 75 72 6e 3b 0a mData ) return;.
11fee 20 20 69 66 28 20 70 57 69 6e 4d 65 6d 44 61 74 if( pWinMemDat
11fef 61 2d 3e 68 48 65 61 70 20 29 7b 0a 20 20 20 20 a->hHeap ){.
11ff0 61 73 73 65 72 74 28 20 70 57 69 6e 4d 65 6d 44 assert( pWinMemD
11ff1 61 74 61 2d 3e 68 48 65 61 70 21 3d 49 4e 56 41 ata->hHeap!=INVA
11ff2 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 LID_HANDLE_VALUE
11ff3 20 29 3b 0a 23 69 66 20 21 53 51 4c 49 54 45 5f );.#if !SQLITE_
11ff4 4f 53 5f 57 49 4e 52 54 20 26 26 20 64 65 66 69 OS_WINRT && defi
11ff5 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 ned(SQLITE_WIN32
11ff6 5f 4d 41 4c 4c 4f 43 5f 56 41 4c 49 44 41 54 45 _MALLOC_VALIDATE
11ff7 29 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 73 ). assert( os
11ff8 48 65 61 70 56 61 6c 69 64 61 74 65 28 70 57 69 HeapValidate(pWi
11ff9 6e 4d 65 6d 44 61 74 61 2d 3e 68 48 65 61 70 2c nMemData->hHeap,
11ffa 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 SQLITE_WIN32_HE
11ffb 41 50 5f 46 4c 41 47 53 2c 20 4e 55 4c 4c 29 20 AP_FLAGS, NULL)
11ffc 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 );.#endif. if
11ffd 28 20 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e 62 ( pWinMemData->b
11ffe 4f 77 6e 65 64 20 29 7b 0a 20 20 20 20 20 20 69 Owned ){. i
11fff 66 28 20 21 6f 73 48 65 61 70 44 65 73 74 72 6f f( !osHeapDestro
12000 79 28 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e 68 y(pWinMemData->h
12001 48 65 61 70 29 20 29 7b 0a 20 20 20 20 20 20 20 Heap) ){.
12002 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c sqlite3_log(SQL
12003 49 54 45 5f 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c ITE_NOMEM, "fail
12004 65 64 20 74 6f 20 48 65 61 70 44 65 73 74 72 6f ed to HeapDestro
12005 79 20 28 25 64 29 2c 20 68 65 61 70 3d 25 70 22 y (%d), heap=%p"
12006 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
12007 20 20 20 20 20 20 6f 73 47 65 74 4c 61 73 74 45 osGetLastE
12008 72 72 6f 72 28 29 2c 20 28 76 6f 69 64 2a 29 70 rror(), (void*)p
12009 57 69 6e 4d 65 6d 44 61 74 61 2d 3e 68 48 65 61 WinMemData->hHea
1200a 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p);. }.
1200b 20 20 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e 62 pWinMemData->b
1200c 4f 77 6e 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 Owned = FALSE;.
1200d 20 20 20 7d 0a 20 20 20 20 70 57 69 6e 4d 65 6d }. pWinMem
1200e 44 61 74 61 2d 3e 68 48 65 61 70 20 3d 20 4e 55 Data->hHeap = NU
1200f 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a LL;. }.}../*.**
12010 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f Populate the lo
12011 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
12012 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 llocation functi
12013 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a on pointers in.*
12014 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 * sqlite3GlobalC
12015 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 onfig.m with poi
12016 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 nters to the rou
12017 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 tines in this fi
12018 6c 65 2e 20 54 68 65 0a 2a 2a 20 61 72 67 75 6d le. The.** argum
12019 65 6e 74 73 20 73 70 65 63 69 66 79 20 74 68 65 ents specify the
1201a 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 block of memory
1201b 20 74 6f 20 6d 61 6e 61 67 65 2e 0a 2a 2a 0a 2a to manage..**.*
1201c 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1201d 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 62 79 s only called by
1201e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
1201f 29 2c 20 61 6e 64 20 74 68 65 72 65 66 6f 72 65 ), and therefore
12020 0a 2a 2a 20 69 73 20 6e 6f 74 20 72 65 71 75 69 .** is not requi
12021 72 65 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 red to be thread
12022 73 61 66 65 20 28 69 74 20 69 73 20 6e 6f 74 29 safe (it is not)
12023 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
12024 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ATE const sqlite
12025 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 3_mem_methods *s
12026 71 6c 69 74 65 33 4d 65 6d 47 65 74 57 69 6e 33 qlite3MemGetWin3
12027 32 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 2(void){. stati
12028 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
12029 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 77 69 6e 4d mem_methods winM
1202a 65 6d 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 emMethods = {.
1202b 20 20 77 69 6e 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a winMemMalloc,.
1202c 20 20 20 20 77 69 6e 4d 65 6d 46 72 65 65 2c 0a winMemFree,.
1202d 20 20 20 20 77 69 6e 4d 65 6d 52 65 61 6c 6c 6f winMemReallo
1202e 63 2c 0a 20 20 20 20 77 69 6e 4d 65 6d 53 69 7a c,. winMemSiz
1202f 65 2c 0a 20 20 20 20 77 69 6e 4d 65 6d 52 6f 75 e,. winMemRou
12030 6e 64 75 70 2c 0a 20 20 20 20 77 69 6e 4d 65 6d ndup,. winMem
12031 49 6e 69 74 2c 0a 20 20 20 20 77 69 6e 4d 65 6d Init,. winMem
12032 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 26 77 Shutdown,. &w
12033 69 6e 5f 6d 65 6d 5f 64 61 74 61 0a 20 20 7d 3b in_mem_data. };
12034 0a 20 20 72 65 74 75 72 6e 20 26 77 69 6e 4d 65 . return &winMe
12035 6d 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 53 51 4c mMethods;.}..SQL
12036 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
12037 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 sqlite3MemSetDe
12038 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 fault(void){. s
12039 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 qlite3_config(SQ
1203a 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c LITE_CONFIG_MALL
1203b 4f 43 2c 20 73 71 6c 69 74 65 33 4d 65 6d 47 65 OC, sqlite3MemGe
1203c 74 57 69 6e 33 32 28 29 29 3b 0a 7d 0a 23 65 6e tWin32());.}.#en
1203d 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 57 49 dif /* SQLITE_WI
1203e 4e 33 32 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f N32_MALLOC */../
1203f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 *.** Convert a U
12040 54 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 4d TF-8 string to M
12041 69 63 72 6f 73 6f 66 74 20 55 6e 69 63 6f 64 65 icrosoft Unicode
12042 20 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a 0a (UTF-16?). .**.
12043 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 ** Space to hold
12044 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 the returned st
12045 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 ring is obtained
12046 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f from malloc..*/
12047 0a 73 74 61 74 69 63 20 4c 50 57 53 54 52 20 75 .static LPWSTR u
12048 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e tf8ToUnicode(con
12049 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
1204a 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 43 68 61 72 me){. int nChar
1204b 3b 0a 20 20 4c 50 57 53 54 52 20 7a 57 69 64 65 ;. LPWSTR zWide
1204c 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 43 68 Filename;.. nCh
1204d 61 72 20 3d 20 6f 73 4d 75 6c 74 69 42 79 74 65 ar = osMultiByte
1204e 54 6f 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 ToWideChar(CP_UT
1204f 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 F8, 0, zFilename
12050 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a , -1, NULL, 0);.
12051 20 20 69 66 28 20 6e 43 68 61 72 3d 3d 30 20 29 if( nChar==0 )
12052 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
12053 20 20 7d 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e }. zWideFilen
12054 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c ame = sqlite3Mal
12055 6c 6f 63 5a 65 72 6f 28 20 6e 43 68 61 72 2a 73 locZero( nChar*s
12056 69 7a 65 6f 66 28 7a 57 69 64 65 46 69 6c 65 6e izeof(zWideFilen
12057 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 ame[0]) );. if(
12058 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3d 3d zWideFilename==
12059 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
1205a 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 0;. }. nChar =
1205b 20 6f 73 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 osMultiByteToWi
1205c 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 2c 20 deChar(CP_UTF8,
1205d 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 0, zFilename, -1
1205e 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c , zWideFilename,
1205f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
12060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12061 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28 20 6e nChar);. if( n
12062 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 73 Char==0 ){. s
12063 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 57 69 64 qlite3_free(zWid
12064 65 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 eFilename);.
12065 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20 zWideFilename =
12066 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
12067 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 7d zWideFilename;.}
12068 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 ../*.** Convert
12069 4d 69 63 72 6f 73 6f 66 74 20 55 6e 69 63 6f 64 Microsoft Unicod
1206a 65 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 e to UTF-8. Spa
1206b 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ce to hold the r
1206c 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 eturned string i
1206d 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 s.** obtained fr
1206e 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f om sqlite3_mallo
1206f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 c()..*/.static c
12070 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f 55 74 har *unicodeToUt
12071 66 38 28 4c 50 43 57 53 54 52 20 7a 57 69 64 65 f8(LPCWSTR zWide
12072 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 Filename){. int
12073 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 72 20 2a nByte;. char *
12074 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 42 zFilename;.. nB
12075 79 74 65 20 3d 20 6f 73 57 69 64 65 43 68 61 72 yte = osWideChar
12076 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55 ToMultiByte(CP_U
12077 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c TF8, 0, zWideFil
12078 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c ename, -1, 0, 0,
12079 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 0, 0);. if( nB
1207a 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 yte == 0 ){.
1207b 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
1207c 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 73 71 6c 69 zFilename = sqli
1207d 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 6e te3MallocZero( n
1207e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 Byte );. if( zF
1207f 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 ilename==0 ){.
12080 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
12081 20 20 6e 42 79 74 65 20 3d 20 6f 73 57 69 64 65 nByte = osWide
12082 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 CharToMultiByte(
12083 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 CP_UTF8, 0, zWid
12084 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a eFilename, -1, z
12085 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c Filename, nByte,
12086 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
12087 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12088 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 0, 0);. if( nB
12089 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 yte == 0 ){.
1208a 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 69 sqlite3_free(zFi
1208b 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 46 69 lename);. zFi
1208c 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a lename = 0;. }.
1208d 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 return zFilena
1208e 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e me;.}../*.** Con
1208f 76 65 72 74 20 61 6e 20 41 4e 53 49 20 73 74 72 vert an ANSI str
12090 69 6e 67 20 74 6f 20 4d 69 63 72 6f 73 6f 66 74 ing to Microsoft
12091 20 55 6e 69 63 6f 64 65 2c 20 62 61 73 65 64 20 Unicode, based
12092 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e on the.** curren
12093 74 20 63 6f 64 65 70 61 67 65 20 73 65 74 74 69 t codepage setti
12094 6e 67 73 20 66 6f 72 20 66 69 6c 65 20 61 70 69 ngs for file api
12095 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70 61 63 65 20 s..** .** Space
12096 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 to hold the retu
12097 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f rned string is o
12098 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 btained.** from
12099 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e 0a sqlite3_malloc..
1209a 2a 2f 0a 73 74 61 74 69 63 20 4c 50 57 53 54 52 */.static LPWSTR
1209b 20 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63 mbcsToUnicode(c
1209c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
1209d 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 name){. int nBy
1209e 74 65 3b 0a 20 20 4c 50 57 53 54 52 20 7a 4d 62 te;. LPWSTR zMb
1209f 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e csFilename;. in
120a0 74 20 63 6f 64 65 70 61 67 65 20 3d 20 6f 73 41 t codepage = osA
120a1 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 reFileApisANSI()
120a2 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f ? CP_ACP : CP_O
120a3 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d EMCP;.. nByte =
120a4 20 6f 73 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 osMultiByteToWi
120a5 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c deChar(codepage,
120a6 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 0, zFilename, -
120a7 31 2c 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 1, NULL,.
120a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
120a9 20 20 20 20 20 20 20 20 20 30 29 2a 73 69 7a 65 0)*size
120aa 6f 66 28 57 43 48 41 52 29 3b 0a 20 20 69 66 28 of(WCHAR);. if(
120ab 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20 nByte==0 ){.
120ac 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
120ad 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d zMbcsFilename =
120ae 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 sqlite3MallocZe
120af 72 6f 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 ro( nByte*sizeof
120b0 28 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 5b 30 (zMbcsFilename[0
120b1 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 4d 62 63 ]) );. if( zMbc
120b2 73 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a sFilename==0 ){.
120b3 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
120b4 7d 0a 20 20 6e 42 79 74 65 20 3d 20 6f 73 4d 75 }. nByte = osMu
120b5 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61 ltiByteToWideCha
120b6 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a r(codepage, 0, z
120b7 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 4d Filename, -1, zM
120b8 62 63 73 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 bcsFilename,.
120b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
120ba 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 42 79 nBy
120bb 74 65 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 te);. if( nByte
120bc 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
120bd 65 33 5f 66 72 65 65 28 7a 4d 62 63 73 46 69 6c e3_free(zMbcsFil
120be 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d 62 63 ename);. zMbc
120bf 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 sFilename = 0;.
120c0 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d 62 63 }. return zMbc
120c1 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a sFilename;.}../*
120c2 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 4d 69 63 72 .** Convert Micr
120c3 6f 73 6f 66 74 20 55 6e 69 63 6f 64 65 20 74 6f osoft Unicode to
120c4 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 multi-byte char
120c5 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62 61 acter string, ba
120c6 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 73 sed on the.** us
120c7 65 72 27 73 20 41 4e 53 49 20 63 6f 64 65 70 61 er's ANSI codepa
120c8 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 ge..**.** Space
120c9 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 to hold the retu
120ca 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f rned string is o
120cb 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 btained from.**
120cc 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
120cd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
120ce 20 2a 75 6e 69 63 6f 64 65 54 6f 4d 62 63 73 28 *unicodeToMbcs(
120cf 4c 50 43 57 53 54 52 20 7a 57 69 64 65 46 69 6c LPCWSTR zWideFil
120d0 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 ename){. int nB
120d1 79 74 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 yte;. char *zFi
120d2 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f lename;. int co
120d3 64 65 70 61 67 65 20 3d 20 6f 73 41 72 65 46 69 depage = osAreFi
120d4 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f 20 43 leApisANSI() ? C
120d5 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 P_ACP : CP_OEMCP
120d6 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 6f 73 57 ;.. nByte = osW
120d7 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 ideCharToMultiBy
120d8 74 65 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 te(codepage, 0,
120d9 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d zWideFilename, -
120da 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 1, 0, 0, 0, 0);.
120db 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d 20 30 if( nByte == 0
120dc 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
120dd 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d ;. }. zFilenam
120de 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f e = sqlite3Mallo
120df 63 5a 65 72 6f 28 20 6e 42 79 74 65 20 29 3b 0a cZero( nByte );.
120e0 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d if( zFilename=
120e1 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
120e2 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 0;. }. nByte
120e3 3d 20 6f 73 57 69 64 65 43 68 61 72 54 6f 4d 75 = osWideCharToMu
120e4 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 67 65 ltiByte(codepage
120e5 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 , 0, zWideFilena
120e6 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e 61 6d me, -1, zFilenam
120e7 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
120e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
120e9 20 20 20 6e 42 79 74 65 2c 20 30 2c 20 30 29 3b nByte, 0, 0);
120ea 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d 20 . if( nByte ==
120eb 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
120ec 5f 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 _free(zFilename)
120ed 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 ;. zFilename
120ee 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
120ef 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a n zFilename;.}..
120f0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 /*.** Convert mu
120f1 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65 ltibyte characte
120f2 72 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d r string to UTF-
120f3 38 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 8. Space to hol
120f4 64 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 d the.** returne
120f5 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 d string is obta
120f6 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
120f7 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 3_malloc()..*/.S
120f8 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
120f9 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 sqlite3_win32_mb
120fa 63 73 5f 74 6f 5f 75 74 66 38 28 63 6f 6e 73 74 cs_to_utf8(const
120fb 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
120fc 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 ){. char *zFile
120fd 6e 61 6d 65 55 74 66 38 3b 0a 20 20 4c 50 57 53 nameUtf8;. LPWS
120fe 54 52 20 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 TR zTmpWide;..
120ff 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73 54 zTmpWide = mbcsT
12100 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 oUnicode(zFilena
12101 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 me);. if( zTmpW
12102 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ide==0 ){. re
12103 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 turn 0;. }. zF
12104 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75 6e ilenameUtf8 = un
12105 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d 70 icodeToUtf8(zTmp
12106 57 69 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 Wide);. sqlite3
12107 5f 66 72 65 65 28 7a 54 6d 70 57 69 64 65 29 3b _free(zTmpWide);
12108 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e . return zFilen
12109 61 6d 65 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a ameUtf8;.}../*.*
1210a 2a 20 43 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 * Convert UTF-8
1210b 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61 to multibyte cha
1210c 72 61 63 74 65 72 20 73 74 72 69 6e 67 2e 20 20 racter string.
1210d 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 Space to hold th
1210e 65 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 e .** returned s
1210f 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 tring is obtaine
12110 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d d from sqlite3_m
12111 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 alloc()..*/.SQLI
12112 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c TE_API char *sql
12113 69 74 65 33 5f 77 69 6e 33 32 5f 75 74 66 38 5f ite3_win32_utf8_
12114 74 6f 5f 6d 62 63 73 28 63 6f 6e 73 74 20 63 68 to_mbcs(const ch
12115 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a ar *zFilename){.
12116 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d char *zFilenam
12117 65 4d 62 63 73 3b 0a 20 20 4c 50 57 53 54 52 20 eMbcs;. LPWSTR
12118 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d zTmpWide;.. zTm
12119 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55 6e pWide = utf8ToUn
1211a 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 icode(zFilename)
1211b 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64 65 ;. if( zTmpWide
1211c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
1211d 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 n 0;. }. zFile
1211e 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63 6f nameMbcs = unico
1211f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69 64 deToMbcs(zTmpWid
12120 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 e);. sqlite3_fr
12121 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 ee(zTmpWide);.
12122 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 return zFilename
12123 4d 62 63 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 Mbcs;.}../*.** T
12124 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 his function set
12125 73 20 74 68 65 20 64 61 74 61 20 64 69 72 65 63 s the data direc
12126 74 6f 72 79 20 6f 72 20 74 68 65 20 74 65 6d 70 tory or the temp
12127 6f 72 61 72 79 20 64 69 72 65 63 74 6f 72 79 20 orary directory
12128 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 based on.** the
12129 70 72 6f 76 69 64 65 64 20 61 72 67 75 6d 65 6e provided argumen
1212a 74 73 2e 20 20 54 68 65 20 74 79 70 65 20 61 72 ts. The type ar
1212b 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 31 gument must be 1
1212c 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 65 74 in order to set
1212d 20 74 68 65 0a 2a 2a 20 64 61 74 61 20 64 69 72 the.** data dir
1212e 65 63 74 6f 72 79 20 6f 72 20 32 20 69 6e 20 6f ectory or 2 in o
1212f 72 64 65 72 20 74 6f 20 73 65 74 20 74 68 65 20 rder to set the
12130 74 65 6d 70 6f 72 61 72 79 20 64 69 72 65 63 74 temporary direct
12131 6f 72 79 2e 20 20 54 68 65 20 7a 56 61 6c 75 65 ory. The zValue
12132 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 .** argument is
12133 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
12134 64 69 72 65 63 74 6f 72 79 20 74 6f 20 75 73 65 directory to use
12135 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 . The return va
12136 6c 75 65 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 53 lue will be.** S
12137 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 QLITE_OK if succ
12138 65 73 73 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 essful..*/.SQLIT
12139 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1213a 33 5f 77 69 6e 33 32 5f 73 65 74 5f 64 69 72 65 3_win32_set_dire
1213b 63 74 6f 72 79 28 44 57 4f 52 44 20 74 79 70 65 ctory(DWORD type
1213c 2c 20 4c 50 43 57 53 54 52 20 7a 56 61 6c 75 65 , LPCWSTR zValue
1213d 29 7b 0a 20 20 63 68 61 72 20 2a 2a 70 70 44 69 ){. char **ppDi
1213e 72 65 63 74 6f 72 79 20 3d 20 30 3b 0a 23 69 66 rectory = 0;.#if
1213f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
12140 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74 20 _AUTOINIT. int
12141 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 rc = sqlite3_ini
12142 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 tialize();. if(
12143 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
12144 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 74 79 .#endif. if( ty
12145 70 65 3d 3d 53 51 4c 49 54 45 5f 57 49 4e 33 32 pe==SQLITE_WIN32
12146 5f 44 41 54 41 5f 44 49 52 45 43 54 4f 52 59 5f _DATA_DIRECTORY_
12147 54 59 50 45 20 29 7b 0a 20 20 20 20 70 70 44 69 TYPE ){. ppDi
12148 72 65 63 74 6f 72 79 20 3d 20 26 73 71 6c 69 74 rectory = &sqlit
12149 65 33 5f 64 61 74 61 5f 64 69 72 65 63 74 6f 72 e3_data_director
1214a 79 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 74 y;. }else if( t
1214b 79 70 65 3d 3d 53 51 4c 49 54 45 5f 57 49 4e 33 ype==SQLITE_WIN3
1214c 32 5f 54 45 4d 50 5f 44 49 52 45 43 54 4f 52 59 2_TEMP_DIRECTORY
1214d 5f 54 59 50 45 20 29 7b 0a 20 20 20 20 70 70 44 _TYPE ){. ppD
1214e 69 72 65 63 74 6f 72 79 20 3d 20 26 73 71 6c 69 irectory = &sqli
1214f 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f te3_temp_directo
12150 72 79 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 ry;. }. assert
12151 28 20 21 70 70 44 69 72 65 63 74 6f 72 79 20 7c ( !ppDirectory |
12152 7c 20 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 57 | type==SQLITE_W
12153 49 4e 33 32 5f 44 41 54 41 5f 44 49 52 45 43 54 IN32_DATA_DIRECT
12154 4f 52 59 5f 54 59 50 45 0a 20 20 20 20 20 20 20 ORY_TYPE.
12155 20 20 20 7c 7c 20 74 79 70 65 3d 3d 53 51 4c 49 || type==SQLI
12156 54 45 5f 57 49 4e 33 32 5f 54 45 4d 50 5f 44 49 TE_WIN32_TEMP_DI
12157 52 45 43 54 4f 52 59 5f 54 59 50 45 0a 20 20 29 RECTORY_TYPE. )
12158 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 70 44 ;. assert( !ppD
12159 69 72 65 63 74 6f 72 79 20 7c 7c 20 73 71 6c 69 irectory || sqli
1215a 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54 79 te3MemdebugHasTy
1215b 70 65 28 2a 70 70 44 69 72 65 63 74 6f 72 79 2c pe(*ppDirectory,
1215c 20 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 20 29 MEMTYPE_HEAP) )
1215d 3b 0a 20 20 69 66 28 20 70 70 44 69 72 65 63 74 ;. if( ppDirect
1215e 6f 72 79 20 29 7b 0a 20 20 20 20 63 68 61 72 20 ory ){. char
1215f 2a 7a 56 61 6c 75 65 55 74 66 38 20 3d 20 30 3b *zValueUtf8 = 0;
12160 0a 20 20 20 20 69 66 28 20 7a 56 61 6c 75 65 20 . if( zValue
12161 26 26 20 7a 56 61 6c 75 65 5b 30 5d 20 29 7b 0a && zValue[0] ){.
12162 20 20 20 20 20 20 7a 56 61 6c 75 65 55 74 66 38 zValueUtf8
12163 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 = unicodeToUtf8
12164 28 7a 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 (zValue);.
12165 69 66 20 28 20 7a 56 61 6c 75 65 55 74 66 38 3d if ( zValueUtf8=
12166 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
12167 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
12168 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d M;. }. }
12169 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
1216a 65 28 2a 70 70 44 69 72 65 63 74 6f 72 79 29 3b e(*ppDirectory);
1216b 0a 20 20 20 20 2a 70 70 44 69 72 65 63 74 6f 72 . *ppDirector
1216c 79 20 3d 20 7a 56 61 6c 75 65 55 74 66 38 3b 0a y = zValueUtf8;.
1216d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1216e 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 E_OK;. }. retu
1216f 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
12170 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 .}../*.** The re
12171 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 67 65 turn value of ge
12172 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 0a 2a 2a tLastErrorMsg.**
12173 20 69 73 20 7a 65 72 6f 20 69 66 20 74 68 65 20 is zero if the
12174 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 69 error message fi
12175 74 73 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 ts in the buffer
12176 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a , or non-zero.**
12177 20 6f 74 68 65 72 77 69 73 65 20 28 69 66 20 74 otherwise (if t
12178 68 65 20 6d 65 73 73 61 67 65 20 77 61 73 20 74 he message was t
12179 72 75 6e 63 61 74 65 64 29 2e 0a 2a 2f 0a 73 74 runcated)..*/.st
1217a 61 74 69 63 20 69 6e 74 20 67 65 74 4c 61 73 74 atic int getLast
1217b 45 72 72 6f 72 4d 73 67 28 44 57 4f 52 44 20 6c ErrorMsg(DWORD l
1217c 61 73 74 45 72 72 6e 6f 2c 20 69 6e 74 20 6e 42 astErrno, int nB
1217d 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b uf, char *zBuf){
1217e 0a 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65 73 73 . /* FormatMess
1217f 61 67 65 20 72 65 74 75 72 6e 73 20 30 20 6f 6e age returns 0 on
12180 20 66 61 69 6c 75 72 65 2e 20 20 4f 74 68 65 72 failure. Other
12181 77 69 73 65 20 69 74 0a 20 20 2a 2a 20 72 65 74 wise it. ** ret
12182 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
12183 6f 66 20 54 43 48 41 52 73 20 77 72 69 74 74 65 of TCHARs writte
12184 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 0a n to the output.
12185 20 20 2a 2a 20 62 75 66 66 65 72 2c 20 65 78 63 ** buffer, exc
12186 6c 75 64 69 6e 67 20 74 68 65 20 74 65 72 6d 69 luding the termi
12187 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68 61 72 nating null char
12188 2e 0a 20 20 2a 2f 0a 20 20 44 57 4f 52 44 20 64 .. */. DWORD d
12189 77 4c 65 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 wLen = 0;. char
1218a 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 0a 20 20 69 *zOut = 0;.. i
1218b 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 23 69 66 f( isNT() ){.#if
1218c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 SQLITE_OS_WINRT
1218d 0a 20 20 20 20 57 43 48 41 52 20 7a 54 65 6d 70 . WCHAR zTemp
1218e 57 69 64 65 5b 4d 41 58 5f 50 41 54 48 2b 31 5d Wide[MAX_PATH+1]
1218f 3b 20 2f 2a 20 4e 4f 54 45 3a 20 53 6f 6d 65 77 ; /* NOTE: Somew
12190 68 61 74 20 61 72 62 69 74 72 61 72 79 2e 20 2a hat arbitrary. *
12191 2f 0a 20 20 20 20 64 77 4c 65 6e 20 3d 20 6f 73 /. dwLen = os
12192 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 57 28 46 FormatMessageW(F
12193 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 52 ORMAT_MESSAGE_FR
12194 4f 4d 5f 53 59 53 54 45 4d 20 7c 0a 20 20 20 20 OM_SYSTEM |.
12195 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12196 20 20 20 20 20 20 20 20 20 46 4f 52 4d 41 54 5f FORMAT_
12197 4d 45 53 53 41 47 45 5f 49 47 4e 4f 52 45 5f 49 MESSAGE_IGNORE_I
12198 4e 53 45 52 54 53 2c 0a 20 20 20 20 20 20 20 20 NSERTS,.
12199 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1219a 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 NULL,.
1219b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1219c 20 20 20 20 20 20 20 20 6c 61 73 74 45 72 72 6e lastErrn
1219d 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 o,.
1219e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1219f 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 0,.
121a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121a1 7a 54 65 6d 70 57 69 64 65 2c 0a 20 20 20 20 20 zTempWide,.
121a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121a3 20 20 20 20 20 20 20 20 4d 41 58 5f 50 41 54 48 MAX_PATH
121a4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
121a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
121a6 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 4c 50 57 );.#else. LPW
121a7 53 54 52 20 7a 54 65 6d 70 57 69 64 65 20 3d 20 STR zTempWide =
121a8 4e 55 4c 4c 3b 0a 20 20 20 20 64 77 4c 65 6e 20 NULL;. dwLen
121a9 3d 20 6f 73 46 6f 72 6d 61 74 4d 65 73 73 61 67 = osFormatMessag
121aa 65 57 28 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 eW(FORMAT_MESSAG
121ab 45 5f 41 4c 4c 4f 43 41 54 45 5f 42 55 46 46 45 E_ALLOCATE_BUFFE
121ac 52 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 R |.
121ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121ae 20 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f FORMAT_MESSAGE_
121af 46 52 4f 4d 5f 53 59 53 54 45 4d 20 7c 0a 20 20 FROM_SYSTEM |.
121b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121b1 20 20 20 20 20 20 20 20 20 20 20 46 4f 52 4d 41 FORMA
121b2 54 5f 4d 45 53 53 41 47 45 5f 49 47 4e 4f 52 45 T_MESSAGE_IGNORE
121b3 5f 49 4e 53 45 52 54 53 2c 0a 20 20 20 20 20 20 _INSERTS,.
121b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121b5 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 NULL,.
121b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121b7 20 20 20 20 20 20 20 20 20 20 6c 61 73 74 45 72 lastEr
121b8 72 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rno,.
121b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121ba 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 0,.
121bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121bc 20 20 28 4c 50 57 53 54 52 29 20 26 7a 54 65 6d (LPWSTR) &zTem
121bd 70 57 69 64 65 2c 0a 20 20 20 20 20 20 20 20 20 pWide,.
121be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121bf 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 0,.
121c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121c1 20 20 20 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 0);.#endif.
121c2 20 20 20 69 66 28 20 64 77 4c 65 6e 20 3e 20 30 if( dwLen > 0
121c3 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 61 6c 6c ){. /* all
121c4 6f 63 61 74 65 20 61 20 62 75 66 66 65 72 20 61 ocate a buffer a
121c5 6e 64 20 63 6f 6e 76 65 72 74 20 74 6f 20 55 54 nd convert to UT
121c6 46 38 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 F8 */. sqli
121c7 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
121c8 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 7a 4f lloc();. zO
121c9 75 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 ut = unicodeToUt
121ca 66 38 28 7a 54 65 6d 70 57 69 64 65 29 3b 0a 20 f8(zTempWide);.
121cb 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 sqlite3EndB
121cc 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23 enignMalloc();.#
121cd 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 if !SQLITE_OS_WI
121ce 4e 52 54 0a 20 20 20 20 20 20 2f 2a 20 66 72 65 NRT. /* fre
121cf 65 20 74 68 65 20 73 79 73 74 65 6d 20 62 75 66 e the system buf
121d0 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 fer allocated by
121d1 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 20 2a FormatMessage *
121d2 2f 0a 20 20 20 20 20 20 6f 73 4c 6f 63 61 6c 46 /. osLocalF
121d3 72 65 65 28 7a 54 65 6d 70 57 69 64 65 29 3b 0a ree(zTempWide);.
121d4 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d #endif. }. }
121d5 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 57 .#ifdef SQLITE_W
121d6 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20 20 IN32_HAS_ANSI.
121d7 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a else{. char *
121d8 7a 54 65 6d 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 zTemp = NULL;.
121d9 20 20 64 77 4c 65 6e 20 3d 20 6f 73 46 6f 72 6d dwLen = osForm
121da 61 74 4d 65 73 73 61 67 65 41 28 46 4f 52 4d 41 atMessageA(FORMA
121db 54 5f 4d 45 53 53 41 47 45 5f 41 4c 4c 4f 43 41 T_MESSAGE_ALLOCA
121dc 54 45 5f 42 55 46 46 45 52 20 7c 0a 20 20 20 20 TE_BUFFER |.
121dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121de 20 20 20 20 20 20 20 20 20 46 4f 52 4d 41 54 5f FORMAT_
121df 4d 45 53 53 41 47 45 5f 46 52 4f 4d 5f 53 59 53 MESSAGE_FROM_SYS
121e0 54 45 4d 20 7c 0a 20 20 20 20 20 20 20 20 20 20 TEM |.
121e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121e2 20 20 20 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 FORMAT_MESSAG
121e3 45 5f 49 47 4e 4f 52 45 5f 49 4e 53 45 52 54 53 E_IGNORE_INSERTS
121e4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
121e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e N
121e6 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ULL,.
121e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121e8 20 20 6c 61 73 74 45 72 72 6e 6f 2c 0a 20 20 20 lastErrno,.
121e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121ea 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 0,.
121eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121ec 20 20 20 20 20 20 20 20 20 20 28 4c 50 53 54 52 (LPSTR
121ed 29 20 26 7a 54 65 6d 70 2c 0a 20 20 20 20 20 20 ) &zTemp,.
121ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121ef 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 0,.
121f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
121f1 20 20 20 20 20 20 20 30 29 3b 0a 20 20 20 20 69 0);. i
121f2 66 28 20 64 77 4c 65 6e 20 3e 20 30 20 29 7b 0a f( dwLen > 0 ){.
121f3 20 20 20 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 74 /* allocat
121f4 65 20 61 20 62 75 66 66 65 72 20 61 6e 64 20 63 e a buffer and c
121f5 6f 6e 76 65 72 74 20 74 6f 20 55 54 46 38 20 2a onvert to UTF8 *
121f6 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 /. sqlite3B
121f7 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
121f8 28 29 3b 0a 20 20 20 20 20 20 7a 4f 75 74 20 3d ();. zOut =
121f9 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d sqlite3_win32_m
121fa 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65 6d bcs_to_utf8(zTem
121fb 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 p);. sqlite
121fc 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
121fd 28 29 3b 0a 20 20 20 20 20 20 2f 2a 20 66 72 65 ();. /* fre
121fe 65 20 74 68 65 20 73 79 73 74 65 6d 20 62 75 66 e the system buf
121ff 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 fer allocated by
12200 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 20 2a FormatMessage *
12201 2f 0a 20 20 20 20 20 20 6f 73 4c 6f 63 61 6c 46 /. osLocalF
12202 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 ree(zTemp);.
12203 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 }. }.#endif. i
12204 66 28 20 30 20 3d 3d 20 64 77 4c 65 6e 20 29 7b f( 0 == dwLen ){
12205 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
12206 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 rintf(nBuf, zBuf
12207 2c 20 22 4f 73 45 72 72 6f 72 20 30 78 25 78 20 , "OsError 0x%x
12208 28 25 75 29 22 2c 20 6c 61 73 74 45 72 72 6e 6f (%u)", lastErrno
12209 2c 20 6c 61 73 74 45 72 72 6e 6f 29 3b 0a 20 20 , lastErrno);.
1220a 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 63 6f }else{. /* co
1220b 70 79 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20 py a maximum of
1220c 6e 42 75 66 20 63 68 61 72 73 20 74 6f 20 6f 75 nBuf chars to ou
1220d 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 tput buffer */.
1220e 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1220f 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 ntf(nBuf, zBuf,
12210 22 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 "%s", zOut);.
12211 20 2f 2a 20 66 72 65 65 20 74 68 65 20 55 54 46 /* free the UTF
12212 38 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 8 buffer */.
12213 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4f 75 sqlite3_free(zOu
12214 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e t);. }. return
12215 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 0a 2a 2a 20 0;.}../*.**.**
12216 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2d 20 This function -
12217 77 69 6e 4c 6f 67 45 72 72 6f 72 41 74 4c 69 6e winLogErrorAtLin
12218 65 28 29 20 2d 20 69 73 20 6f 6e 6c 79 20 65 76 e() - is only ev
12219 65 72 20 63 61 6c 6c 65 64 20 76 69 61 20 74 68 er called via th
1221a 65 20 6d 61 63 72 6f 0a 2a 2a 20 77 69 6e 4c 6f e macro.** winLo
1221b 67 45 72 72 6f 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 gError()..**.**
1221c 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
1221d 69 6e 76 6f 6b 65 64 20 61 66 74 65 72 20 61 6e invoked after an
1221e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69 6e error occurs in
1221f 20 61 6e 20 4f 53 20 66 75 6e 63 74 69 6f 6e 2e an OS function.
12220 0a 2a 2a 20 49 74 20 6c 6f 67 73 20 61 20 6d 65 .** It logs a me
12221 73 73 61 67 65 20 75 73 69 6e 67 20 73 71 6c 69 ssage using sqli
12222 74 65 33 5f 6c 6f 67 28 29 20 63 6f 6e 74 61 69 te3_log() contai
12223 6e 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 ning the current
12224 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 65 72 72 value of.** err
12225 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 69 66 20 or code and, if
12226 70 6f 73 73 69 62 6c 65 2c 20 74 68 65 20 68 75 possible, the hu
12227 6d 61 6e 2d 72 65 61 64 61 62 6c 65 20 65 71 75 man-readable equ
12228 69 76 61 6c 65 6e 74 20 66 72 6f 6d 20 0a 2a 2a ivalent from .**
12229 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 2e 0a FormatMessage..
1222a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
1222b 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 argument passed
1222c 74 6f 20 74 68 65 20 6d 61 63 72 6f 20 73 68 6f to the macro sho
1222d 75 6c 64 20 62 65 20 74 68 65 20 65 72 72 6f 72 uld be the error
1222e 20 63 6f 64 65 20 74 68 61 74 0a 2a 2a 20 77 69 code that.** wi
1222f 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 74 ll be returned t
12230 6f 20 53 51 4c 69 74 65 20 28 65 2e 67 2e 20 53 o SQLite (e.g. S
12231 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 QLITE_IOERR_DELE
12232 54 45 2c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f TE, SQLITE_CANTO
12233 50 45 4e 29 2e 20 0a 2a 2a 20 54 68 65 20 74 77 PEN). .** The tw
12234 6f 20 73 75 62 73 65 71 75 65 6e 74 20 61 72 67 o subsequent arg
12235 75 6d 65 6e 74 73 20 73 68 6f 75 6c 64 20 62 65 uments should be
12236 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
12237 20 4f 53 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 OS function tha
12238 74 0a 2a 2a 20 66 61 69 6c 65 64 20 61 6e 64 20 t.** failed and
12239 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 66 the associated f
1223a 69 6c 65 2d 73 79 73 74 65 6d 20 70 61 74 68 2c ile-system path,
1223b 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a 23 64 65 66 if any..*/.#def
1223c 69 6e 65 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 ine winLogError(
1223d 61 2c 62 2c 63 2c 64 29 20 20 20 77 69 6e 4c 6f a,b,c,d) winLo
1223e 67 45 72 72 6f 72 41 74 4c 69 6e 65 28 61 2c 62 gErrorAtLine(a,b
1223f 2c 63 2c 64 2c 5f 5f 4c 49 4e 45 5f 5f 29 0a 73 ,c,d,__LINE__).s
12240 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4c 6f 67 tatic int winLog
12241 45 72 72 6f 72 41 74 4c 69 6e 65 28 0a 20 20 69 ErrorAtLine(. i
12242 6e 74 20 65 72 72 63 6f 64 65 2c 20 20 20 20 20 nt errcode,
12243 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
12244 2a 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 * SQLite error c
12245 6f 64 65 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6c ode */. DWORD l
12246 61 73 74 45 72 72 6e 6f 2c 20 20 20 20 20 20 20 astErrno,
12247 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69 6e 33 /* Win3
12248 32 20 6c 61 73 74 20 65 72 72 6f 72 20 2a 2f 0a 2 last error */.
12249 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
1224a 75 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 unc,
1224b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 4f 53 20 /* Name of OS
1224c 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 66 61 function that fa
1224d 69 6c 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 iled */. const
1224e 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 char *zPath,
1224f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c /* Fil
12250 65 20 70 61 74 68 20 61 73 73 6f 63 69 61 74 65 e path associate
12251 64 20 77 69 74 68 20 65 72 72 6f 72 20 2a 2f 0a d with error */.
12252 20 20 69 6e 74 20 69 4c 69 6e 65 20 20 20 20 20 int iLine
12253 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12254 20 20 2f 2a 20 53 6f 75 72 63 65 20 6c 69 6e 65 /* Source line
12255 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 65 72 number where er
12256 72 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a ror occurred */.
12257 29 7b 0a 20 20 63 68 61 72 20 7a 4d 73 67 5b 35 ){. char zMsg[5
12258 30 30 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 00];
12259 20 20 20 20 20 2f 2a 20 48 75 6d 61 6e 20 72 65 /* Human re
1225a 61 64 61 62 6c 65 20 65 72 72 6f 72 20 74 65 78 adable error tex
1225b 74 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 t */. int i;
1225c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1225d 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
1225e 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 7a 4d 73 ounter */.. zMs
1225f 67 5b 30 5d 20 3d 20 30 3b 0a 20 20 67 65 74 4c g[0] = 0;. getL
12260 61 73 74 45 72 72 6f 72 4d 73 67 28 6c 61 73 74 astErrorMsg(last
12261 45 72 72 6e 6f 2c 20 73 69 7a 65 6f 66 28 7a 4d Errno, sizeof(zM
12262 73 67 29 2c 20 7a 4d 73 67 29 3b 0a 20 20 61 73 sg), zMsg);. as
12263 73 65 72 74 28 20 65 72 72 63 6f 64 65 21 3d 53 sert( errcode!=S
12264 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 QLITE_OK );. if
12265 28 20 7a 50 61 74 68 3d 3d 30 20 29 20 7a 50 61 ( zPath==0 ) zPa
12266 74 68 20 3d 20 22 22 3b 0a 20 20 66 6f 72 28 69 th = "";. for(i
12267 3d 30 3b 20 7a 4d 73 67 5b 69 5d 20 26 26 20 7a =0; zMsg[i] && z
12268 4d 73 67 5b 69 5d 21 3d 27 5c 72 27 20 26 26 20 Msg[i]!='\r' &&
12269 7a 4d 73 67 5b 69 5d 21 3d 27 5c 6e 27 3b 20 69 zMsg[i]!='\n'; i
1226a 2b 2b 29 7b 7d 0a 20 20 7a 4d 73 67 5b 69 5d 20 ++){}. zMsg[i]
1226b 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6c = 0;. sqlite3_l
1226c 6f 67 28 65 72 72 63 6f 64 65 2c 0a 20 20 20 20 og(errcode,.
1226d 20 20 22 6f 73 5f 77 69 6e 2e 63 3a 25 64 3a 20 "os_win.c:%d:
1226e 28 25 64 29 20 25 73 28 25 73 29 20 2d 20 25 73 (%d) %s(%s) - %s
1226f 22 2c 0a 20 20 20 20 20 20 69 4c 69 6e 65 2c 20 ",. iLine,
12270 6c 61 73 74 45 72 72 6e 6f 2c 20 7a 46 75 6e 63 lastErrno, zFunc
12271 2c 20 7a 50 61 74 68 2c 20 7a 4d 73 67 0a 20 20 , zPath, zMsg.
12272 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 65 72 72 );.. return err
12273 63 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 code;.}../*.** T
12274 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d he number of tim
12275 65 73 20 74 68 61 74 20 61 20 52 65 61 64 46 69 es that a ReadFi
12276 6c 65 28 29 2c 20 57 72 69 74 65 46 69 6c 65 28 le(), WriteFile(
12277 29 2c 20 61 6e 64 20 44 65 6c 65 74 65 46 69 6c ), and DeleteFil
12278 65 28 29 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72 e().** will be r
12279 65 74 72 69 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 etried following
1227a 20 61 20 6c 6f 63 6b 69 6e 67 20 65 72 72 6f 72 a locking error
1227b 20 2d 20 70 72 6f 62 61 62 6c 79 20 63 61 75 73 - probably caus
1227c 65 64 20 62 79 20 0a 2a 2a 20 61 6e 74 69 76 69 ed by .** antivi
1227d 72 75 73 20 73 6f 66 74 77 61 72 65 2e 20 20 41 rus software. A
1227e 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 20 lso the initial
1227f 64 65 6c 61 79 20 62 65 66 6f 72 65 20 74 68 65 delay before the
12280 20 66 69 72 73 74 20 72 65 74 72 79 2e 0a 2a 2a first retry..**
12281 20 54 68 65 20 64 65 6c 61 79 20 69 6e 63 72 65 The delay incre
12282 61 73 65 73 20 6c 69 6e 65 61 72 6c 79 20 77 69 ases linearly wi
12283 74 68 20 65 61 63 68 20 72 65 74 72 79 2e 0a 2a th each retry..*
12284 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
12285 5f 57 49 4e 33 32 5f 49 4f 45 52 52 5f 52 45 54 _WIN32_IOERR_RET
12286 52 59 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 RY.# define SQLI
12287 54 45 5f 57 49 4e 33 32 5f 49 4f 45 52 52 5f 52 TE_WIN32_IOERR_R
12288 45 54 52 59 20 31 30 0a 23 65 6e 64 69 66 0a 23 ETRY 10.#endif.#
12289 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 57 49 ifndef SQLITE_WI
1228a 4e 33 32 5f 49 4f 45 52 52 5f 52 45 54 52 59 5f N32_IOERR_RETRY_
1228b 44 45 4c 41 59 0a 23 20 64 65 66 69 6e 65 20 53 DELAY.# define S
1228c 51 4c 49 54 45 5f 57 49 4e 33 32 5f 49 4f 45 52 QLITE_WIN32_IOER
1228d 52 5f 52 45 54 52 59 5f 44 45 4c 41 59 20 32 35 R_RETRY_DELAY 25
1228e 0a 23 65 6e 64 69 66 0a 73 74 61 74 69 63 20 69 .#endif.static i
1228f 6e 74 20 77 69 6e 33 32 49 6f 65 72 72 52 65 74 nt win32IoerrRet
12290 72 79 20 3d 20 53 51 4c 49 54 45 5f 57 49 4e 33 ry = SQLITE_WIN3
12291 32 5f 49 4f 45 52 52 5f 52 45 54 52 59 3b 0a 73 2_IOERR_RETRY;.s
12292 74 61 74 69 63 20 69 6e 74 20 77 69 6e 33 32 49 tatic int win32I
12293 6f 65 72 72 52 65 74 72 79 44 65 6c 61 79 20 3d oerrRetryDelay =
12294 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 49 4f SQLITE_WIN32_IO
12295 45 52 52 5f 52 45 54 52 59 5f 44 45 4c 41 59 3b ERR_RETRY_DELAY;
12296 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 61 20 52 65 61 ../*.** If a Rea
12297 64 46 69 6c 65 28 29 20 6f 72 20 57 72 69 74 65 dFile() or Write
12298 46 69 6c 65 28 29 20 65 72 72 6f 72 20 6f 63 63 File() error occ
12299 75 72 73 2c 20 69 6e 76 6f 6b 65 20 74 68 69 73 urs, invoke this
1229a 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 74 6f 20 73 routine.** to s
1229b 65 65 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20 ee if it should
1229c 62 65 20 72 65 74 72 69 65 64 2e 20 20 52 65 74 be retried. Ret
1229d 75 72 6e 20 54 52 55 45 20 74 6f 20 72 65 74 72 urn TRUE to retr
1229e 79 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 y. Return FALSE
1229f 0a 2a 2a 20 74 6f 20 67 69 76 65 20 75 70 20 77 .** to give up w
122a0 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f ith an error..*/
122a1 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 74 72 .static int retr
122a2 79 49 6f 65 72 72 28 69 6e 74 20 2a 70 6e 52 65 yIoerr(int *pnRe
122a3 74 72 79 2c 20 44 57 4f 52 44 20 2a 70 45 72 72 try, DWORD *pErr
122a4 6f 72 29 7b 0a 20 20 44 57 4f 52 44 20 65 20 3d or){. DWORD e =
122a5 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 osGetLastError(
122a6 29 3b 0a 20 20 69 66 28 20 2a 70 6e 52 65 74 72 );. if( *pnRetr
122a7 79 3e 3d 77 69 6e 33 32 49 6f 65 72 72 52 65 74 y>=win32IoerrRet
122a8 72 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 45 ry ){. if( pE
122a9 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 2a 70 rror ){. *p
122aa 45 72 72 6f 72 20 3d 20 65 3b 0a 20 20 20 20 7d Error = e;. }
122ab 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
122ac 20 7d 0a 20 20 69 66 28 20 65 3d 3d 45 52 52 4f }. if( e==ERRO
122ad 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 45 44 20 R_ACCESS_DENIED
122ae 7c 7c 0a 20 20 20 20 20 20 65 3d 3d 45 52 52 4f ||. e==ERRO
122af 52 5f 4c 4f 43 4b 5f 56 49 4f 4c 41 54 49 4f 4e R_LOCK_VIOLATION
122b0 20 7c 7c 0a 20 20 20 20 20 20 65 3d 3d 45 52 52 ||. e==ERR
122b1 4f 52 5f 53 48 41 52 49 4e 47 5f 56 49 4f 4c 41 OR_SHARING_VIOLA
122b2 54 49 4f 4e 20 29 7b 0a 20 20 20 20 73 71 6c 69 TION ){. sqli
122b3 74 65 33 5f 77 69 6e 33 32 5f 73 6c 65 65 70 28 te3_win32_sleep(
122b4 77 69 6e 33 32 49 6f 65 72 72 52 65 74 72 79 44 win32IoerrRetryD
122b5 65 6c 61 79 2a 28 31 2b 2a 70 6e 52 65 74 72 79 elay*(1+*pnRetry
122b6 29 29 3b 0a 20 20 20 20 2b 2b 2a 70 6e 52 65 74 ));. ++*pnRet
122b7 72 79 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 ry;. return 1
122b8 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 72 72 ;. }. if( pErr
122b9 6f 72 20 29 7b 0a 20 20 20 20 2a 70 45 72 72 6f or ){. *pErro
122ba 72 20 3d 20 65 3b 0a 20 20 7d 0a 20 20 72 65 74 r = e;. }. ret
122bb 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
122bc 4c 6f 67 20 61 20 49 2f 4f 20 65 72 72 6f 72 20 Log a I/O error
122bd 72 65 74 72 79 20 65 70 69 73 6f 64 65 2e 0a 2a retry episode..*
122be 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f /.static void lo
122bf 67 49 6f 65 72 72 28 69 6e 74 20 6e 52 65 74 72 gIoerr(int nRetr
122c0 79 29 7b 0a 20 20 69 66 28 20 6e 52 65 74 72 79 y){. if( nRetry
122c1 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
122c2 6c 6f 67 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 log(SQLITE_IOERR
122c3 2c 20 0a 20 20 20 20 20 20 22 64 65 6c 61 79 65 , . "delaye
122c4 64 20 25 64 6d 73 20 66 6f 72 20 6c 6f 63 6b 2f d %dms for lock/
122c5 73 68 61 72 69 6e 67 20 63 6f 6e 66 6c 69 63 74 sharing conflict
122c6 22 2c 0a 20 20 20 20 20 20 77 69 6e 33 32 49 6f ",. win32Io
122c7 65 72 72 52 65 74 72 79 44 65 6c 61 79 2a 6e 52 errRetryDelay*nR
122c8 65 74 72 79 2a 28 6e 52 65 74 72 79 2b 31 29 2f etry*(nRetry+1)/
122c9 32 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2. );. }.}..
122ca 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
122cb 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a NCE./***********
122cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
122cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
122ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
122cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
122d0 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20 63 * This section c
122d1 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 ontains code for
122d2 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a WinCE only..*/.
122d3 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 20 43 45 /*.** Windows CE
122d4 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 does not have a
122d5 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75 6e localtime() fun
122d6 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61 74 ction. So creat
122d7 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 e a.** substitut
122d8 65 2e 0a 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 e..*/./* #includ
122d9 65 20 3c 74 69 6d 65 2e 68 3e 20 2a 2f 0a 73 74 e <time.h> */.st
122da 72 75 63 74 20 74 6d 20 2a 5f 5f 63 64 65 63 6c ruct tm *__cdecl
122db 20 6c 6f 63 61 6c 74 69 6d 65 28 63 6f 6e 73 74 localtime(const
122dc 20 74 69 6d 65 5f 74 20 2a 74 29 0a 7b 0a 20 20 time_t *t).{.
122dd 73 74 61 74 69 63 20 73 74 72 75 63 74 20 74 6d static struct tm
122de 20 79 3b 0a 20 20 46 49 4c 45 54 49 4d 45 20 75 y;. FILETIME u
122df 54 6d 2c 20 6c 54 6d 3b 0a 20 20 53 59 53 54 45 Tm, lTm;. SYSTE
122e0 4d 54 49 4d 45 20 70 54 6d 3b 0a 20 20 73 71 6c MTIME pTm;. sql
122e1 69 74 65 33 5f 69 6e 74 36 34 20 74 36 34 3b 0a ite3_int64 t64;.
122e2 20 20 74 36 34 20 3d 20 2a 74 3b 0a 20 20 74 36 t64 = *t;. t6
122e3 34 20 3d 20 28 74 36 34 20 2b 20 31 31 36 34 34 4 = (t64 + 11644
122e4 34 37 33 36 30 30 29 2a 31 30 30 30 30 30 30 30 473600)*10000000
122e5 3b 0a 20 20 75 54 6d 2e 64 77 4c 6f 77 44 61 74 ;. uTm.dwLowDat
122e6 65 54 69 6d 65 20 3d 20 28 44 57 4f 52 44 29 28 eTime = (DWORD)(
122e7 74 36 34 20 26 20 30 78 46 46 46 46 46 46 46 46 t64 & 0xFFFFFFFF
122e8 29 3b 0a 20 20 75 54 6d 2e 64 77 48 69 67 68 44 );. uTm.dwHighD
122e9 61 74 65 54 69 6d 65 3d 20 28 44 57 4f 52 44 29 ateTime= (DWORD)
122ea 28 74 36 34 20 3e 3e 20 33 32 29 3b 0a 20 20 6f (t64 >> 32);. o
122eb 73 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63 61 6c sFileTimeToLocal
122ec 46 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c 26 6c FileTime(&uTm,&l
122ed 54 6d 29 3b 0a 20 20 6f 73 46 69 6c 65 54 69 6d Tm);. osFileTim
122ee 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 26 6c eToSystemTime(&l
122ef 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d Tm,&pTm);. y.tm
122f0 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 65 61 _year = pTm.wYea
122f1 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d r - 1900;. y.tm
122f2 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74 _mon = pTm.wMont
122f3 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64 h - 1;. y.tm_wd
122f4 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57 ay = pTm.wDayOfW
122f5 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79 eek;. y.tm_mday
122f6 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79 = pTm.wDay;. y
122f7 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77 .tm_hour = pTm.w
122f8 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e Hour;. y.tm_min
122f9 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a = pTm.wMinute;.
122fa 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d y.tm_sec = pTm
122fb 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75 .wSecond;. retu
122fc 72 6e 20 26 79 3b 0a 7d 0a 0a 23 64 65 66 69 6e rn &y;.}..#defin
122fd 65 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 e HANDLE_TO_WINF
122fe 49 4c 45 28 61 29 20 28 77 69 6e 46 69 6c 65 2a ILE(a) (winFile*
122ff 29 26 28 28 63 68 61 72 2a 29 61 29 5b 2d 28 69 )&((char*)a)[-(i
12300 6e 74 29 6f 66 66 73 65 74 6f 66 28 77 69 6e 46 nt)offsetof(winF
12301 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20 41 ile,h)]../*.** A
12302 63 71 75 69 72 65 20 61 20 6c 6f 63 6b 20 6f 6e cquire a lock on
12303 20 74 68 65 20 68 61 6e 64 6c 65 20 68 0a 2a 2f the handle h.*/
12304 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e .static void win
12305 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 48 ceMutexAcquire(H
12306 41 4e 44 4c 45 20 68 29 7b 0a 20 20 20 44 57 4f ANDLE h){. DWO
12307 52 44 20 64 77 45 72 72 3b 0a 20 20 20 64 6f 20 RD dwErr;. do
12308 7b 0a 20 20 20 20 20 64 77 45 72 72 20 3d 20 6f {. dwErr = o
12309 73 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62 sWaitForSingleOb
1230a 6a 65 63 74 28 68 2c 20 49 4e 46 49 4e 49 54 45 ject(h, INFINITE
1230b 29 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20 28 64 );. } while (d
1230c 77 45 72 72 20 21 3d 20 57 41 49 54 5f 4f 42 4a wErr != WAIT_OBJ
1230d 45 43 54 5f 30 20 26 26 20 64 77 45 72 72 20 21 ECT_0 && dwErr !
1230e 3d 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e 45 44 = WAIT_ABANDONED
1230f 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 );.}./*.** Relea
12310 73 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 69 72 se a lock acquir
12311 65 64 20 62 79 20 77 69 6e 63 65 4d 75 74 65 78 ed by winceMutex
12312 41 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23 64 65 Acquire().*/.#de
12313 66 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 78 52 fine winceMutexR
12314 65 6c 65 61 73 65 28 68 29 20 52 65 6c 65 61 73 elease(h) Releas
12315 65 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a eMutex(h)../*.**
12316 20 43 72 65 61 74 65 20 74 68 65 20 6d 75 74 65 Create the mute
12317 78 20 61 6e 64 20 73 68 61 72 65 64 20 6d 65 6d x and shared mem
12318 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 ory used for loc
12319 6b 69 6e 67 20 69 6e 20 74 68 65 20 66 69 6c 65 king in the file
1231a 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 70 .** descriptor p
1231b 46 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 File.*/.static B
1231c 4f 4f 4c 20 77 69 6e 63 65 43 72 65 61 74 65 4c OOL winceCreateL
1231d 6f 63 6b 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ock(const char *
1231e 7a 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e 46 69 zFilename, winFi
1231f 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 4c 50 le *pFile){. LP
12320 57 53 54 52 20 7a 54 6f 6b 3b 0a 20 20 4c 50 57 WSTR zTok;. LPW
12321 53 54 52 20 7a 4e 61 6d 65 3b 0a 20 20 42 4f 4f STR zName;. BOO
12322 4c 20 62 49 6e 69 74 20 3d 20 54 52 55 45 3b 0a L bInit = TRUE;.
12323 0a 20 20 7a 4e 61 6d 65 20 3d 20 75 74 66 38 54 . zName = utf8T
12324 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 oUnicode(zFilena
12325 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 me);. if( zName
12326 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6f 75 ==0 ){. /* ou
12327 74 20 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 t of memory */.
12328 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b return FALSE;
12329 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 . }.. /* Initi
1232a 61 6c 69 7a 65 20 74 68 65 20 6c 6f 63 61 6c 20 alize the local
1232b 6c 6f 63 6b 64 61 74 61 20 2a 2f 0a 20 20 6d 65 lockdata */. me
1232c 6d 73 65 74 28 26 70 46 69 6c 65 2d 3e 6c 6f 63 mset(&pFile->loc
1232d 61 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 46 al, 0, sizeof(pF
1232e 69 6c 65 2d 3e 6c 6f 63 61 6c 29 29 3b 0a 0a 20 ile->local));..
1232f 20 2f 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20 /* Replace the
12330 62 61 63 6b 73 6c 61 73 68 65 73 20 66 72 6f 6d backslashes from
12331 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 6e the filename an
12332 64 20 6c 6f 77 65 72 63 61 73 65 20 69 74 0a 20 d lowercase it.
12333 20 2a 2a 20 74 6f 20 64 65 72 69 76 65 20 61 20 ** to derive a
12334 6d 75 74 65 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20 mutex name. */.
12335 20 7a 54 6f 6b 20 3d 20 6f 73 43 68 61 72 4c 6f zTok = osCharLo
12336 77 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 werW(zName);. f
12337 6f 72 20 28 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b 2b or (;*zTok;zTok+
12338 2b 29 7b 0a 20 20 20 20 69 66 20 28 2a 7a 54 6f +){. if (*zTo
12339 6b 20 3d 3d 20 27 5c 5c 27 29 20 2a 7a 54 6f 6b k == '\\') *zTok
1233a 20 3d 20 27 5f 27 3b 0a 20 20 7d 0a 0a 20 20 2f = '_';. }.. /
1233b 2a 20 43 72 65 61 74 65 2f 6f 70 65 6e 20 74 68 * Create/open th
1233c 65 20 6e 61 6d 65 64 20 6d 75 74 65 78 20 2a 2f e named mutex */
1233d 0a 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 . pFile->hMutex
1233e 20 3d 20 6f 73 43 72 65 61 74 65 4d 75 74 65 78 = osCreateMutex
1233f 57 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 2c 20 7a W(NULL, FALSE, z
12340 4e 61 6d 65 29 3b 0a 20 20 69 66 20 28 21 70 46 Name);. if (!pF
12341 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 ile->hMutex){.
12342 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
12343 6e 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 no = osGetLastEr
12344 72 6f 72 28 29 3b 0a 20 20 20 20 77 69 6e 4c 6f ror();. winLo
12345 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 45 52 gError(SQLITE_ER
12346 52 4f 52 2c 20 70 46 69 6c 65 2d 3e 6c 61 73 74 ROR, pFile->last
12347 45 72 72 6e 6f 2c 20 22 77 69 6e 63 65 43 72 65 Errno, "winceCre
12348 61 74 65 4c 6f 63 6b 31 22 2c 20 7a 46 69 6c 65 ateLock1", zFile
12349 6e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 name);. sqlit
1234a 65 33 5f 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a e3_free(zName);.
1234b 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 return FALSE
1234c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 ;. }.. /* Acqu
1234d 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 62 65 ire the mutex be
1234e 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 20 fore continuing
1234f 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 */. winceMutexA
12350 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d cquire(pFile->hM
12351 75 74 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 53 utex);. . /* S
12352 69 6e 63 65 20 74 68 65 20 6e 61 6d 65 73 20 6f ince the names o
12353 66 20 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 2c f named mutexes,
12354 20 73 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 6c semaphores, fil
12355 65 20 6d 61 70 70 69 6e 67 73 20 65 74 63 20 61 e mappings etc a
12356 72 65 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 65 re . ** case-se
12357 6e 73 69 74 69 76 65 2c 20 74 61 6b 65 20 61 64 nsitive, take ad
12358 76 61 6e 74 61 67 65 20 6f 66 20 74 68 61 74 20 vantage of that
12359 62 79 20 75 70 70 65 72 63 61 73 69 6e 67 20 74 by uppercasing t
1235a 68 65 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 20 he mutex name.
1235b 2a 2a 20 61 6e 64 20 75 73 69 6e 67 20 74 68 61 ** and using tha
1235c 74 20 61 73 20 74 68 65 20 73 68 61 72 65 64 20 t as the shared
1235d 66 69 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d 65 filemapping name
1235e 2e 0a 20 20 2a 2f 0a 20 20 6f 73 43 68 61 72 55 .. */. osCharU
1235f 70 70 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 pperW(zName);.
12360 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d pFile->hShared =
12361 20 6f 73 43 72 65 61 74 65 46 69 6c 65 4d 61 70 osCreateFileMap
12362 70 69 6e 67 57 28 49 4e 56 41 4c 49 44 5f 48 41 pingW(INVALID_HA
12363 4e 44 4c 45 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c NDLE_VALUE, NULL
12364 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
12365 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12366 20 20 20 20 20 20 20 20 20 20 50 41 47 45 5f 52 PAGE_R
12367 45 41 44 57 52 49 54 45 2c 20 30 2c 20 73 69 7a EADWRITE, 0, siz
12368 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a eof(winceLock),.
12369 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1236a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1236b 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20 zName);
1236c 20 0a 0a 20 20 2f 2a 20 53 65 74 20 61 20 66 6c .. /* Set a fl
1236d 61 67 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 ag that indicate
1236e 73 20 77 65 27 72 65 20 74 68 65 20 66 69 72 73 s we're the firs
1236f 74 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 t to create the
12370 6d 65 6d 6f 72 79 20 73 6f 20 69 74 20 0a 20 20 memory so it .
12371 2a 2a 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d ** must be zero-
12372 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 initialized */.
12373 20 69 66 20 28 6f 73 47 65 74 4c 61 73 74 45 72 if (osGetLastEr
12374 72 6f 72 28 29 20 3d 3d 20 45 52 52 4f 52 5f 41 ror() == ERROR_A
12375 4c 52 45 41 44 59 5f 45 58 49 53 54 53 29 7b 0a LREADY_EXISTS){.
12376 20 20 20 20 62 49 6e 69 74 20 3d 20 46 41 4c 53 bInit = FALS
12377 45 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 E;. }.. sqlite
12378 33 5f 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 0a 3_free(zName);..
12379 20 20 2f 2a 20 49 66 20 77 65 20 73 75 63 63 65 /* If we succe
1237a 65 64 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 74 eded in making t
1237b 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 he shared memory
1237c 20 68 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 2e handle, map it.
1237d 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d */. if (pFile-
1237e 3e 68 53 68 61 72 65 64 29 7b 0a 20 20 20 20 70 >hShared){. p
1237f 46 69 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 28 File->shared = (
12380 77 69 6e 63 65 4c 6f 63 6b 2a 29 6f 73 4d 61 70 winceLock*)osMap
12381 56 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 ViewOfFile(pFile
12382 2d 3e 68 53 68 61 72 65 64 2c 20 0a 20 20 20 20 ->hShared, .
12383 20 20 20 20 20 20 20 20 20 46 49 4c 45 5f 4d 41 FILE_MA
12384 50 5f 52 45 41 44 7c 46 49 4c 45 5f 4d 41 50 5f P_READ|FILE_MAP_
12385 57 52 49 54 45 2c 20 30 2c 20 30 2c 20 73 69 7a WRITE, 0, 0, siz
12386 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b eof(winceLock));
12387 0a 20 20 20 20 2f 2a 20 49 66 20 6d 61 70 70 69 . /* If mappi
12388 6e 67 20 66 61 69 6c 65 64 2c 20 63 6c 6f 73 65 ng failed, close
12389 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f the shared memo
1238a 72 79 20 68 61 6e 64 6c 65 20 61 6e 64 20 65 72 ry handle and er
1238b 61 73 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 ase it */. if
1238c 20 28 21 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 (!pFile->shared
1238d 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
1238e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73 47 65 lastErrno = osGe
1238f 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 tLastError();.
12390 20 20 20 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 winLogError(
12391 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 70 46 SQLITE_ERROR, pF
12392 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c 0a ile->lastErrno,.
12393 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
12394 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b 32 winceCreateLock2
12395 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 ", zFilename);.
12396 20 20 20 20 20 6f 73 43 6c 6f 73 65 48 61 6e 64 osCloseHand
12397 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 le(pFile->hShare
12398 64 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d d);. pFile-
12399 3e 68 53 68 61 72 65 64 20 3d 20 4e 55 4c 4c 3b >hShared = NULL;
1239a 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
1239b 20 49 66 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 If shared memor
1239c 79 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 y could not be c
1239d 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 6c 6f reated, then clo
1239e 73 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e 64 se the mutex and
1239f 20 66 61 69 6c 20 2a 2f 0a 20 20 69 66 20 28 70 fail */. if (p
123a0 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 3d File->hShared ==
123a1 20 4e 55 4c 4c 29 7b 0a 20 20 20 20 77 69 6e 63 NULL){. winc
123a2 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 eMutexRelease(pF
123a3 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 ile->hMutex);.
123a4 20 20 6f 73 43 6c 6f 73 65 48 61 6e 64 6c 65 28 osCloseHandle(
123a5 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a pFile->hMutex);.
123a6 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 pFile->hMute
123a7 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 x = NULL;. re
123a8 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a turn FALSE;. }.
123a9 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 . /* Initiali
123aa 7a 65 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 ze the shared me
123ab 6d 6f 72 79 20 69 66 20 77 65 27 72 65 20 73 75 mory if we're su
123ac 70 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20 20 69 pposed to */. i
123ad 66 20 28 62 49 6e 69 74 29 20 7b 0a 20 20 20 20 f (bInit) {.
123ae 6d 65 6d 73 65 74 28 70 46 69 6c 65 2d 3e 73 68 memset(pFile->sh
123af 61 72 65 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 ared, 0, sizeof(
123b0 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 7d winceLock));. }
123b1 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 .. winceMutexRe
123b2 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 lease(pFile->hMu
123b3 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 54 tex);. return T
123b4 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 RUE;.}../*.** De
123b5 73 74 72 6f 79 20 74 68 65 20 70 61 72 74 20 6f stroy the part o
123b6 66 20 77 69 6e 46 69 6c 65 20 74 68 61 74 20 64 f winFile that d
123b7 65 61 6c 73 20 77 69 74 68 20 77 69 6e 63 65 20 eals with wince
123b8 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 20 locks.*/.static
123b9 76 6f 69 64 20 77 69 6e 63 65 44 65 73 74 72 6f void winceDestro
123ba 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 yLock(winFile *p
123bb 46 69 6c 65 29 7b 0a 20 20 69 66 20 28 70 46 69 File){. if (pFi
123bc 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20 le->hMutex){.
123bd 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 65 20 /* Acquire the
123be 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e mutex */. win
123bf 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 ceMutexAcquire(p
123c0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a File->hMutex);..
123c1 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f /* The follo
123c2 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75 wing blocks shou
123c3 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 73 73 65 ld probably asse
123c4 72 74 20 69 6e 20 64 65 62 75 67 20 6d 6f 64 65 rt in debug mode
123c5 2c 20 62 75 74 20 74 68 65 79 0a 20 20 20 20 20 , but they.
123c6 20 20 61 72 65 20 74 6f 20 63 6c 65 61 6e 75 70 are to cleanup
123c7 20 69 6e 20 63 61 73 65 20 61 6e 79 20 6c 6f 63 in case any loc
123c8 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f 70 65 6e ks remained open
123c9 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c */. if (pFil
123ca 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 e->local.nReader
123cb 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d s){. pFile-
123cc 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 >shared->nReader
123cd 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 s --;. }.
123ce 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c if (pFile->local
123cf 2e 62 52 65 73 65 72 76 65 64 29 7b 0a 20 20 20 .bReserved){.
123d0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
123d1 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 ->bReserved = FA
123d2 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 LSE;. }. i
123d3 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
123d4 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 bPending){.
123d5 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
123d6 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 bPending = FALSE
123d7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 ;. }. if (
123d8 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 pFile->local.bEx
123d9 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20 clusive){.
123da 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
123db 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 Exclusive = FALS
123dc 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a E;. }.. /*
123dd 20 44 65 2d 72 65 66 65 72 65 6e 63 65 20 61 6e De-reference an
123de 64 20 63 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79 d close our copy
123df 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6d of the shared m
123e0 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0a emory handle */.
123e1 20 20 20 20 6f 73 55 6e 6d 61 70 56 69 65 77 4f osUnmapViewO
123e2 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 61 fFile(pFile->sha
123e3 72 65 64 29 3b 0a 20 20 20 20 6f 73 43 6c 6f 73 red);. osClos
123e4 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 eHandle(pFile->h
123e5 53 68 61 72 65 64 29 3b 0a 0a 20 20 20 20 2f 2a Shared);.. /*
123e6 20 44 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6d Done with the m
123e7 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 utex */. winc
123e8 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 eMutexRelease(pF
123e9 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 20 20 20 ile->hMutex);
123ea 20 0a 20 20 20 20 6f 73 43 6c 6f 73 65 48 61 6e . osCloseHan
123eb 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 dle(pFile->hMute
123ec 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 x);. pFile->h
123ed 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 Mutex = NULL;.
123ee 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69 }.}../* .** An i
123ef 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
123f0 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28 29 20 the LockFile()
123f1 41 50 49 20 6f 66 20 57 69 6e 64 6f 77 73 20 66 API of Windows f
123f2 6f 72 20 43 45 0a 2a 2f 0a 73 74 61 74 69 63 20 or CE.*/.static
123f3 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 BOOL winceLockFi
123f4 6c 65 28 0a 20 20 4c 50 48 41 4e 44 4c 45 20 70 le(. LPHANDLE p
123f5 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 hFile,. DWORD d
123f6 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a wFileOffsetLow,.
123f7 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 DWORD dwFileOf
123f8 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 fsetHigh,. DWOR
123f9 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 D nNumberOfBytes
123fa 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f ToLockLow,. DWO
123fb 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 RD nNumberOfByte
123fc 73 54 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 sToLockHigh.){.
123fd 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
123fe 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 = HANDLE_TO_WINF
123ff 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 ILE(phFile);. B
12400 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 OOL bReturn = FA
12401 4c 53 45 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 LSE;.. UNUSED_P
12402 41 52 41 4d 45 54 45 52 28 64 77 46 69 6c 65 4f ARAMETER(dwFileO
12403 66 66 73 65 74 48 69 67 68 29 3b 0a 20 20 55 4e ffsetHigh);. UN
12404 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e USED_PARAMETER(n
12405 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
12406 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 69 66 20 ockHigh);.. if
12407 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 (!pFile->hMutex)
12408 20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 return TRUE;.
12409 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 winceMutexAcquir
1240a 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 e(pFile->hMutex)
1240b 3b 0a 0a 20 20 2f 2a 20 57 61 6e 74 69 6e 67 20 ;.. /* Wanting
1240c 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
1240d 6b 3f 20 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 k? */. if (dwFi
1240e 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 leOffsetLow == (
1240f 44 57 4f 52 44 29 53 48 41 52 45 44 5f 46 49 52 DWORD)SHARED_FIR
12410 53 54 0a 20 20 20 20 20 20 20 26 26 20 6e 4e 75 ST. && nNu
12411 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 mberOfBytesToLoc
12412 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 kLow == (DWORD)S
12413 48 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 HARED_SIZE){.
12414 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 if (pFile->shar
12415 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 ed->nReaders ==
12416 30 20 26 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 0 && pFile->shar
12417 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d ed->bExclusive =
12418 3d 20 30 29 7b 0a 20 20 20 20 20 20 20 70 46 69 = 0){. pFi
12419 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 le->shared->bExc
1241a 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 lusive = TRUE;.
1241b 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
1241c 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 al.bExclusive =
1241d 54 52 55 45 3b 0a 20 20 20 20 20 20 20 62 52 65 TRUE;. bRe
1241e 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 turn = TRUE;.
1241f 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e }. }.. /* Wan
12420 74 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f t a read-only lo
12421 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 ck? */. else if
12422 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f (dwFileOffsetLo
12423 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 w == (DWORD)SHAR
12424 45 44 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 ED_FIRST &&.
12425 20 20 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 nNumberOf
12426 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d BytesToLockLow =
12427 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 = 1){. if (pF
12428 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 ile->shared->bEx
12429 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a 20 clusive == 0){.
1242a 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 pFile->loca
1242b 6c 2e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 l.nReaders ++;.
1242c 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e if (pFile->
1242d 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d local.nReaders =
1242e 3d 20 31 29 7b 0a 20 20 20 20 20 20 20 20 70 46 = 1){. pF
1242f 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 ile->shared->nRe
12430 61 64 65 72 73 20 2b 2b 3b 0a 20 20 20 20 20 20 aders ++;.
12431 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 }. bReturn
12432 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 = TRUE;. }.
12433 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 70 }.. /* Want a p
12434 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f 20 2a 2f 0a ending lock? */.
12435 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c else if (dwFil
12436 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 eOffsetLow == (D
12437 57 4f 52 44 29 50 45 4e 44 49 4e 47 5f 42 59 54 WORD)PENDING_BYT
12438 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 E && nNumberOfBy
12439 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 tesToLockLow ==
1243a 31 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 1){. /* If no
1243b 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 pending lock ha
1243c 73 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 2c s been acquired,
1243d 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 69 74 then acquire it
1243e 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c */. if (pFil
1243f 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 e->shared->bPend
12440 69 6e 67 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 ing == 0) {.
12441 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
12442 3e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 >bPending = TRUE
12443 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c ;. pFile->l
12444 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 ocal.bPending =
12445 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 TRUE;. bRet
12446 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
12447 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 }. }.. /* Want
12448 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b a reserved lock
12449 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 ? */. else if (
1244a 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
1244b 3d 3d 20 28 44 57 4f 52 44 29 52 45 53 45 52 56 == (DWORD)RESERV
1244c 45 44 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 ED_BYTE && nNumb
1244d 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c erOfBytesToLockL
1244e 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 ow == 1){. if
1244f 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d (pFile->shared-
12450 3e 62 52 65 73 65 72 76 65 64 20 3d 3d 20 30 29 >bReserved == 0)
12451 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e {. pFile->
12452 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 shared->bReserve
12453 64 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 d = TRUE;.
12454 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 pFile->local.bRe
12455 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 served = TRUE;.
12456 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 bReturn = T
12457 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a RUE;. }. }..
12458 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 winceMutexRele
12459 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 ase(pFile->hMute
1245a 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 52 65 x);. return bRe
1245b 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 turn;.}../*.** A
1245c 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e n implementation
1245d 20 6f 66 20 74 68 65 20 55 6e 6c 6f 63 6b 46 69 of the UnlockFi
1245e 6c 65 20 41 50 49 20 6f 66 20 57 69 6e 64 6f 77 le API of Window
1245f 73 20 66 6f 72 20 43 45 0a 2a 2f 0a 73 74 61 74 s for CE.*/.stat
12460 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 55 6e 6c ic BOOL winceUnl
12461 6f 63 6b 46 69 6c 65 28 0a 20 20 4c 50 48 41 4e ockFile(. LPHAN
12462 44 4c 45 20 70 68 46 69 6c 65 2c 0a 20 20 44 57 DLE phFile,. DW
12463 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 ORD dwFileOffset
12464 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 Low,. DWORD dwF
12465 69 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 ileOffsetHigh,.
12466 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 DWORD nNumberOf
12467 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 BytesToUnlockLow
12468 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 ,. DWORD nNumbe
12469 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b rOfBytesToUnlock
1246a 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69 6c High.){. winFil
1246b 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44 4c e *pFile = HANDL
1246c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68 46 E_TO_WINFILE(phF
1246d 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52 65 ile);. BOOL bRe
1246e 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a 20 turn = FALSE;..
1246f 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
12470 52 28 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 R(dwFileOffsetHi
12471 67 68 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 gh);. UNUSED_PA
12472 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f RAMETER(nNumberO
12473 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 fBytesToUnlockHi
12474 67 68 29 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 gh);.. if (!pFi
12475 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 le->hMutex) retu
12476 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 rn TRUE;. wince
12477 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 MutexAcquire(pFi
12478 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 le->hMutex);..
12479 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 /* Releasing a r
1247a 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e eader lock or an
1247b 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
1247c 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f */. if (dwFileO
1247d 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f ffsetLow == (DWO
1247e 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 29 RD)SHARED_FIRST)
1247f 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20 {. /* Did we
12480 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 have an exclusiv
12481 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69 e lock? */. i
12482 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
12483 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 bExclusive){.
12484 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62 65 assert(nNumbe
12485 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b rOfBytesToUnlock
12486 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 Low == (DWORD)SH
12487 41 52 45 44 5f 53 49 5a 45 29 3b 0a 20 20 20 20 ARED_SIZE);.
12488 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 pFile->local.b
12489 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 Exclusive = FALS
1248a 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e E;. pFile->
1248b 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 shared->bExclusi
1248c 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 ve = FALSE;.
1248d 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 bReturn = TRUE
1248e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
1248f 44 69 64 20 77 65 20 6a 75 73 74 20 68 61 76 65 Did we just have
12490 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f 20 a reader lock?
12491 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 */. else if (
12492 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 pFile->local.nRe
12493 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 61 73 aders){. as
12494 73 65 72 74 28 6e 4e 75 6d 62 65 72 4f 66 42 79 sert(nNumberOfBy
12495 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d tesToUnlockLow =
12496 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f = (DWORD)SHARED_
12497 53 49 5a 45 20 7c 7c 20 6e 4e 75 6d 62 65 72 4f SIZE || nNumberO
12498 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f fBytesToUnlockLo
12499 77 20 3d 3d 20 31 29 3b 0a 20 20 20 20 20 20 70 w == 1);. p
1249a 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 File->local.nRea
1249b 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69 ders --;. i
1249c 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
1249d 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 29 0a 20 nReaders == 0).
1249e 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 {. p
1249f 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 File->shared->nR
124a0 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 eaders --;.
124a1 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e }. bReturn
124a2 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
124a3 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 }.. /* Releasi
124a4 6e 67 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 ng a pending loc
124a5 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 k */. else if (
124a6 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
124a7 3d 3d 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e == (DWORD)PENDIN
124a8 47 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 G_BYTE && nNumbe
124a9 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b rOfBytesToUnlock
124aa 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 Low == 1){. i
124ab 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
124ac 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 bPending){.
124ad 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 pFile->local.bP
124ae 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a ending = FALSE;.
124af 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
124b0 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 red->bPending =
124b1 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 FALSE;. bRe
124b2 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 turn = TRUE;.
124b3 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 6c 65 }. }. /* Rele
124b4 61 73 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 asing a reserved
124b5 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 lock */. else
124b6 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 if (dwFileOffset
124b7 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 52 45 Low == (DWORD)RE
124b8 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e SERVED_BYTE && n
124b9 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 NumberOfBytesToU
124ba 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a nlockLow == 1){.
124bb 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c if (pFile->l
124bc 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29 20 ocal.bReserved)
124bd 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
124be 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d ocal.bReserved =
124bf 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 FALSE;. pF
124c0 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 ile->shared->bRe
124c1 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a served = FALSE;.
124c2 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 bReturn =
124c3 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a TRUE;. }. }.
124c4 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c . winceMutexRel
124c5 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 ease(pFile->hMut
124c6 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 52 ex);. return bR
124c7 65 74 75 72 6e 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 eturn;.}./*.** E
124c8 6e 64 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 nd of the specia
124c9 6c 20 63 6f 64 65 20 66 6f 72 20 77 69 6e 63 65 l code for wince
124ca 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
124cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
124cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
124cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
124ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
124cf 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
124d0 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a _OS_WINCE */../*
124d1 0a 2a 2a 20 4c 6f 63 6b 20 61 20 66 69 6c 65 20 .** Lock a file
124d2 72 65 67 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 region..*/.stati
124d3 63 20 42 4f 4f 4c 20 77 69 6e 4c 6f 63 6b 46 69 c BOOL winLockFi
124d4 6c 65 28 0a 20 20 4c 50 48 41 4e 44 4c 45 20 70 le(. LPHANDLE p
124d5 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 66 hFile,. DWORD f
124d6 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20 6f 66 lags,. DWORD of
124d7 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 fsetLow,. DWORD
124d8 20 6f 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44 offsetHigh,. D
124d9 57 4f 52 44 20 6e 75 6d 42 79 74 65 73 4c 6f 77 WORD numBytesLow
124da 2c 0a 20 20 44 57 4f 52 44 20 6e 75 6d 42 79 74 ,. DWORD numByt
124db 65 73 48 69 67 68 0a 29 7b 0a 23 69 66 20 53 51 esHigh.){.#if SQ
124dc 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 LITE_OS_WINCE.
124dd 2f 2a 0a 20 20 2a 2a 20 4e 4f 54 45 3a 20 57 69 /*. ** NOTE: Wi
124de 6e 64 6f 77 73 20 43 45 20 69 73 20 68 61 6e 64 ndows CE is hand
124df 6c 65 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20 led differently
124e0 68 65 72 65 20 64 75 65 20 69 74 73 20 6c 61 63 here due its lac
124e1 6b 20 6f 66 20 74 68 65 20 57 69 6e 33 32 0a 20 k of the Win32.
124e2 20 2a 2a 20 20 20 20 20 20 20 41 50 49 20 4c 6f ** API Lo
124e3 63 6b 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 ckFile.. */. r
124e4 65 74 75 72 6e 20 77 69 6e 63 65 4c 6f 63 6b 46 eturn winceLockF
124e5 69 6c 65 28 70 68 46 69 6c 65 2c 20 6f 66 66 73 ile(phFile, offs
124e6 65 74 4c 6f 77 2c 20 6f 66 66 73 65 74 48 69 67 etLow, offsetHig
124e7 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 h,.
124e8 20 20 20 20 20 20 20 20 20 20 6e 75 6d 42 79 74 numByt
124e9 65 73 4c 6f 77 2c 20 6e 75 6d 42 79 74 65 73 48 esLow, numBytesH
124ea 69 67 68 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 igh);.#else. if
124eb 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 ( isNT() ){.
124ec 4f 56 45 52 4c 41 50 50 45 44 20 6f 76 6c 70 3b OVERLAPPED ovlp;
124ed 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 6f 76 6c . memset(&ovl
124ee 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4f 56 45 p, 0, sizeof(OVE
124ef 52 4c 41 50 50 45 44 29 29 3b 0a 20 20 20 20 6f RLAPPED));. o
124f0 76 6c 70 2e 4f 66 66 73 65 74 20 3d 20 6f 66 66 vlp.Offset = off
124f1 73 65 74 4c 6f 77 3b 0a 20 20 20 20 6f 76 6c 70 setLow;. ovlp
124f2 2e 4f 66 66 73 65 74 48 69 67 68 20 3d 20 6f 66 .OffsetHigh = of
124f3 66 73 65 74 48 69 67 68 3b 0a 20 20 20 20 72 65 fsetHigh;. re
124f4 74 75 72 6e 20 6f 73 4c 6f 63 6b 46 69 6c 65 45 turn osLockFileE
124f5 78 28 2a 70 68 46 69 6c 65 2c 20 66 6c 61 67 73 x(*phFile, flags
124f6 2c 20 30 2c 20 6e 75 6d 42 79 74 65 73 4c 6f 77 , 0, numBytesLow
124f7 2c 20 6e 75 6d 42 79 74 65 73 48 69 67 68 2c 20 , numBytesHigh,
124f8 26 6f 76 6c 70 29 3b 0a 20 20 7d 65 6c 73 65 7b &ovlp);. }else{
124f9 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 4c 6f . return osLo
124fa 63 6b 46 69 6c 65 28 2a 70 68 46 69 6c 65 2c 20 ckFile(*phFile,
124fb 6f 66 66 73 65 74 4c 6f 77 2c 20 6f 66 66 73 65 offsetLow, offse
124fc 74 48 69 67 68 2c 20 6e 75 6d 42 79 74 65 73 4c tHigh, numBytesL
124fd 6f 77 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ow,.
124fe 20 20 20 20 20 20 20 20 20 20 6e 75 6d 42 79 74 numByt
124ff 65 73 48 69 67 68 29 3b 0a 20 20 7d 0a 23 65 6e esHigh);. }.#en
12500 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c dif.}../*.** Unl
12501 6f 63 6b 20 61 20 66 69 6c 65 20 72 65 67 69 6f ock a file regio
12502 6e 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 42 4f n.. */.static BO
12503 4f 4c 20 77 69 6e 55 6e 6c 6f 63 6b 46 69 6c 65 OL winUnlockFile
12504 28 0a 20 20 4c 50 48 41 4e 44 4c 45 20 70 68 46 (. LPHANDLE phF
12505 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 6f 66 66 ile,. DWORD off
12506 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 setLow,. DWORD
12507 6f 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57 offsetHigh,. DW
12508 4f 52 44 20 6e 75 6d 42 79 74 65 73 4c 6f 77 2c ORD numBytesLow,
12509 0a 20 20 44 57 4f 52 44 20 6e 75 6d 42 79 74 65 . DWORD numByte
1250a 73 48 69 67 68 0a 29 7b 0a 23 69 66 20 53 51 4c sHigh.){.#if SQL
1250b 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 2f ITE_OS_WINCE. /
1250c 2a 0a 20 20 2a 2a 20 4e 4f 54 45 3a 20 57 69 6e *. ** NOTE: Win
1250d 64 6f 77 73 20 43 45 20 69 73 20 68 61 6e 64 6c dows CE is handl
1250e 65 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20 68 ed differently h
1250f 65 72 65 20 64 75 65 20 69 74 73 20 6c 61 63 6b ere due its lack
12510 20 6f 66 20 74 68 65 20 57 69 6e 33 32 0a 20 20 of the Win32.
12511 2a 2a 20 20 20 20 20 20 20 41 50 49 20 55 6e 6c ** API Unl
12512 6f 63 6b 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 ockFile.. */.
12513 72 65 74 75 72 6e 20 77 69 6e 63 65 55 6e 6c 6f return winceUnlo
12514 63 6b 46 69 6c 65 28 70 68 46 69 6c 65 2c 20 6f ckFile(phFile, o
12515 66 66 73 65 74 4c 6f 77 2c 20 6f 66 66 73 65 74 ffsetLow, offset
12516 48 69 67 68 2c 0a 20 20 20 20 20 20 20 20 20 20 High,.
12517 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e n
12518 75 6d 42 79 74 65 73 4c 6f 77 2c 20 6e 75 6d 42 umBytesLow, numB
12519 79 74 65 73 48 69 67 68 29 3b 0a 23 65 6c 73 65 ytesHigh);.#else
1251a 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b . if( isNT() ){
1251b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50 45 44 20 . OVERLAPPED
1251c 6f 76 6c 70 3b 0a 20 20 20 20 6d 65 6d 73 65 74 ovlp;. memset
1251d 28 26 6f 76 6c 70 2c 20 30 2c 20 73 69 7a 65 6f (&ovlp, 0, sizeo
1251e 66 28 4f 56 45 52 4c 41 50 50 45 44 29 29 3b 0a f(OVERLAPPED));.
1251f 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 20 ovlp.Offset
12520 3d 20 6f 66 66 73 65 74 4c 6f 77 3b 0a 20 20 20 = offsetLow;.
12521 20 6f 76 6c 70 2e 4f 66 66 73 65 74 48 69 67 68 ovlp.OffsetHigh
12522 20 3d 20 6f 66 66 73 65 74 48 69 67 68 3b 0a 20 = offsetHigh;.
12523 20 20 20 72 65 74 75 72 6e 20 6f 73 55 6e 6c 6f return osUnlo
12524 63 6b 46 69 6c 65 45 78 28 2a 70 68 46 69 6c 65 ckFileEx(*phFile
12525 2c 20 30 2c 20 6e 75 6d 42 79 74 65 73 4c 6f 77 , 0, numBytesLow
12526 2c 20 6e 75 6d 42 79 74 65 73 48 69 67 68 2c 20 , numBytesHigh,
12527 26 6f 76 6c 70 29 3b 0a 20 20 7d 65 6c 73 65 7b &ovlp);. }else{
12528 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 55 6e . return osUn
12529 6c 6f 63 6b 46 69 6c 65 28 2a 70 68 46 69 6c 65 lockFile(*phFile
1252a 2c 20 6f 66 66 73 65 74 4c 6f 77 2c 20 6f 66 66 , offsetLow, off
1252b 73 65 74 48 69 67 68 2c 20 6e 75 6d 42 79 74 65 setHigh, numByte
1252c 73 4c 6f 77 2c 0a 20 20 20 20 20 20 20 20 20 20 sLow,.
1252d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 75 nu
1252e 6d 42 79 74 65 73 48 69 67 68 29 3b 0a 20 20 7d mBytesHigh);. }
1252f 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a 2a 2a .#endif.}../****
12530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12531 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12532 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12533 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
12534 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 *********.** The
12535 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 next group of r
12536 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e outines implemen
12537 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 t the I/O method
12538 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 s specified.** b
12539 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f y the sqlite3_io
1253a 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e _methods object.
1253b 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1253c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1253d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1253e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1253f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
12540 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 4d 69 63 ../*.** Some Mic
12541 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 rosoft compilers
12542 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e lack this defin
12543 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ition..*/.#ifnde
12544 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 f INVALID_SET_FI
12545 4c 45 5f 50 4f 49 4e 54 45 52 0a 23 20 64 65 66 LE_POINTER.# def
12546 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f ine INVALID_SET_
12547 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28 28 44 FILE_POINTER ((D
12548 57 4f 52 44 29 2d 31 29 0a 23 65 6e 64 69 66 0a WORD)-1).#endif.
12549 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 ./*.** Move the
1254a 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e current position
1254b 20 6f 66 20 74 68 65 20 66 69 6c 65 20 68 61 6e of the file han
1254c 64 6c 65 20 70 61 73 73 65 64 20 61 73 20 74 68 dle passed as th
1254d 65 20 66 69 72 73 74 20 0a 2a 2a 20 61 72 67 75 e first .** argu
1254e 6d 65 6e 74 20 74 6f 20 6f 66 66 73 65 74 20 69 ment to offset i
1254f 4f 66 66 73 65 74 20 77 69 74 68 69 6e 20 74 68 Offset within th
12550 65 20 66 69 6c 65 2e 20 49 66 20 73 75 63 63 65 e file. If succe
12551 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 30 2e ssful, return 0.
12552 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 .** Otherwise,
12553 73 65 74 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 set pFile->lastE
12554 72 72 6e 6f 20 61 6e 64 20 72 65 74 75 72 6e 20 rrno and return
12555 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 non-zero..*/.sta
12556 74 69 63 20 69 6e 74 20 73 65 65 6b 57 69 6e 46 tic int seekWinF
12557 69 6c 65 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 ile(winFile *pFi
12558 6c 65 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 le, sqlite3_int6
12559 34 20 69 4f 66 66 73 65 74 29 7b 0a 23 69 66 20 4 iOffset){.#if
1255a 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 !SQLITE_OS_WINRT
1255b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 . LONG upperBit
1255c 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
1255d 20 20 20 2f 2a 20 4d 6f 73 74 20 73 69 67 2e 20 /* Most sig.
1255e 33 32 20 62 69 74 73 20 6f 66 20 6e 65 77 20 6f 32 bits of new o
1255f 66 66 73 65 74 20 2a 2f 0a 20 20 4c 4f 4e 47 20 ffset */. LONG
12560 6c 6f 77 65 72 42 69 74 73 3b 20 20 20 20 20 20 lowerBits;
12561 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 /* Le
12562 61 73 74 20 73 69 67 2e 20 33 32 20 62 69 74 73 ast sig. 32 bits
12563 20 6f 66 20 6e 65 77 20 6f 66 66 73 65 74 20 2a of new offset *
12564 2f 0a 20 20 44 57 4f 52 44 20 64 77 52 65 74 3b /. DWORD dwRet;
12565 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12566 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 /* Value ret
12567 75 72 6e 65 64 20 62 79 20 53 65 74 46 69 6c 65 urned by SetFile
12568 50 6f 69 6e 74 65 72 28 29 20 2a 2f 0a 20 20 44 Pointer() */. D
12569 57 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 20 WORD lastErrno;
1256a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1256b 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 * Value returned
1256c 20 62 79 20 47 65 74 4c 61 73 74 45 72 72 6f 72 by GetLastError
1256d 28 29 20 2a 2f 0a 0a 20 20 75 70 70 65 72 42 69 () */.. upperBi
1256e 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 69 4f 66 ts = (LONG)((iOf
1256f 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66 fset>>32) & 0x7f
12570 66 66 66 66 66 66 29 3b 0a 20 20 6c 6f 77 65 72 ffffff);. lower
12571 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 69 4f Bits = (LONG)(iO
12572 66 66 73 65 74 20 26 20 30 78 66 66 66 66 66 66 ffset & 0xffffff
12573 66 66 29 3b 0a 0a 20 20 2f 2a 20 41 50 49 20 6f ff);.. /* API o
12574 64 64 69 74 79 3a 20 49 66 20 73 75 63 63 65 73 ddity: If succes
12575 73 66 75 6c 2c 20 53 65 74 46 69 6c 65 50 6f 69 sful, SetFilePoi
12576 6e 74 65 72 28 29 20 72 65 74 75 72 6e 73 20 61 nter() returns a
12577 20 64 77 6f 72 64 20 0a 20 20 2a 2a 20 63 6f 6e dword . ** con
12578 74 61 69 6e 69 6e 67 20 74 68 65 20 6c 6f 77 65 taining the lowe
12579 72 20 33 32 2d 62 69 74 73 20 6f 66 20 74 68 65 r 32-bits of the
1257a 20 6e 65 77 20 66 69 6c 65 2d 6f 66 66 73 65 74 new file-offset
1257b 2e 20 4f 72 2c 20 69 66 20 69 74 20 66 61 69 6c . Or, if it fail
1257c 73 2c 0a 20 20 2a 2a 20 69 74 20 72 65 74 75 72 s,. ** it retur
1257d 6e 73 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 ns INVALID_SET_F
1257e 49 4c 45 5f 50 4f 49 4e 54 45 52 2e 20 48 6f 77 ILE_POINTER. How
1257f 65 76 65 72 20 61 63 63 6f 72 64 69 6e 67 20 74 ever according t
12580 6f 20 4d 53 44 4e 2c 20 0a 20 20 2a 2a 20 49 4e o MSDN, . ** IN
12581 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 VALID_SET_FILE_P
12582 4f 49 4e 54 45 52 20 6d 61 79 20 61 6c 73 6f 20 OINTER may also
12583 62 65 20 61 20 76 61 6c 69 64 20 6e 65 77 20 6f be a valid new o
12584 66 66 73 65 74 2e 20 53 6f 20 74 6f 20 64 65 74 ffset. So to det
12585 65 72 6d 69 6e 65 20 0a 20 20 2a 2a 20 77 68 65 ermine . ** whe
12586 74 68 65 72 20 61 6e 20 65 72 72 6f 72 20 68 61 ther an error ha
12587 73 20 61 63 74 75 61 6c 6c 79 20 6f 63 63 75 72 s actually occur
12588 65 64 2c 20 69 74 20 69 73 20 61 6c 73 6f 20 6e ed, it is also n
12589 65 63 65 73 73 61 72 79 20 74 6f 20 63 61 6c 6c ecessary to call
1258a 20 0a 20 20 2a 2a 20 47 65 74 4c 61 73 74 45 72 . ** GetLastEr
1258b 72 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 64 77 ror().. */. dw
1258c 52 65 74 20 3d 20 6f 73 53 65 74 46 69 6c 65 50 Ret = osSetFileP
1258d 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c ointer(pFile->h,
1258e 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 lowerBits, &upp
1258f 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 erBits, FILE_BEG
12590 49 4e 29 3b 0a 0a 20 20 69 66 28 20 28 64 77 52 IN);.. if( (dwR
12591 65 74 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f et==INVALID_SET_
12592 46 49 4c 45 5f 50 4f 49 4e 54 45 52 0a 20 20 20 FILE_POINTER.
12593 20 20 20 26 26 20 28 28 6c 61 73 74 45 72 72 6e && ((lastErrn
12594 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 72 o = osGetLastErr
12595 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 29 or())!=NO_ERROR)
12596 29 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ) ){. pFile->
12597 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 61 73 74 lastErrno = last
12598 45 72 72 6e 6f 3b 0a 20 20 20 20 77 69 6e 4c 6f Errno;. winLo
12599 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 49 4f gError(SQLITE_IO
1259a 45 52 52 5f 53 45 45 4b 2c 20 70 46 69 6c 65 2d ERR_SEEK, pFile-
1259b 3e 6c 61 73 74 45 72 72 6e 6f 2c 0a 20 20 20 20 >lastErrno,.
1259c 20 20 20 20 20 20 20 20 20 22 73 65 65 6b 57 69 "seekWi
1259d 6e 46 69 6c 65 22 2c 20 70 46 69 6c 65 2d 3e 7a nFile", pFile->z
1259e 50 61 74 68 29 3b 0a 20 20 20 20 72 65 74 75 72 Path);. retur
1259f 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 n 1;. }.. retu
125a0 72 6e 20 30 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a rn 0;.#else. /*
125a1 0a 20 20 2a 2a 20 53 61 6d 65 20 61 73 20 61 62 . ** Same as ab
125a2 6f 76 65 2c 20 65 78 63 65 70 74 20 74 68 61 74 ove, except that
125a3 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 this implementa
125a4 74 69 6f 6e 20 77 6f 72 6b 73 20 66 6f 72 20 57 tion works for W
125a5 69 6e 52 54 2e 0a 20 20 2a 2f 0a 0a 20 20 4c 41 inRT.. */.. LA
125a6 52 47 45 5f 49 4e 54 45 47 45 52 20 78 3b 20 20 RGE_INTEGER x;
125a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
125a8 20 54 68 65 20 6e 65 77 20 6f 66 66 73 65 74 20 The new offset
125a9 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 74 3b 20 */. BOOL bRet;
125aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
125ab 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 /* Value re
125ac 74 75 72 6e 65 64 20 62 79 20 53 65 74 46 69 6c turned by SetFil
125ad 65 50 6f 69 6e 74 65 72 45 78 28 29 20 2a 2f 0a ePointerEx() */.
125ae 0a 20 20 78 2e 51 75 61 64 50 61 72 74 20 3d 20 . x.QuadPart =
125af 69 4f 66 66 73 65 74 3b 0a 20 20 62 52 65 74 20 iOffset;. bRet
125b0 3d 20 6f 73 53 65 74 46 69 6c 65 50 6f 69 6e 74 = osSetFilePoint
125b1 65 72 45 78 28 70 46 69 6c 65 2d 3e 68 2c 20 78 erEx(pFile->h, x
125b2 2c 20 30 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 , 0, FILE_BEGIN)
125b3 3b 0a 0a 20 20 69 66 28 21 62 52 65 74 29 7b 0a ;.. if(!bRet){.
125b4 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
125b5 72 72 6e 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74 rrno = osGetLast
125b6 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 77 69 6e Error();. win
125b7 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f LogError(SQLITE_
125b8 49 4f 45 52 52 5f 53 45 45 4b 2c 20 70 46 69 6c IOERR_SEEK, pFil
125b9 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c 0a 20 20 e->lastErrno,.
125ba 20 20 20 20 20 20 20 20 20 20 20 22 73 65 65 6b "seek
125bb 57 69 6e 46 69 6c 65 22 2c 20 70 46 69 6c 65 2d WinFile", pFile-
125bc 3e 7a 50 61 74 68 29 3b 0a 20 20 20 20 72 65 74 >zPath);. ret
125bd 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65 urn 1;. }.. re
125be 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 7d turn 0;.#endif.}
125bf 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 ../*.** Close a
125c0 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 file..**.** It i
125c1 73 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 20 s reported that
125c2 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6c an attempt to cl
125c3 6f 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 67 ose a handle mig
125c4 68 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 ht sometimes.**
125c5 66 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20 61 fail. This is a
125c6 20 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 62 very unreasonab
125c7 6c 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20 57 le result, but W
125c8 69 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69 indows is notori
125c9 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67 ous.** for being
125ca 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f unreasonable so
125cb 20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20 I do not doubt
125cc 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 68 61 that it might ha
125cd 70 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 ppen. If.** the
125ce 20 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65 close fails, we
125cf 20 70 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d pause for 100 m
125d0 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 illiseconds and
125d1 74 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a try again. As.*
125d2 2a 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f * many as MX_CLO
125d3 53 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d SE_ATTEMPT attem
125d4 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 pts to close the
125d5 20 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65 handle are made
125d6 20 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e before.** givin
125d7 67 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 g up and returni
125d8 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a ng an error..*/.
125d9 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45 #define MX_CLOSE
125da 5f 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74 69 _ATTEMPT 3.stati
125db 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73 c int winClose(s
125dc 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
125dd 7b 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 20 {. int rc, cnt
125de 3d 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a = 0;. winFile *
125df 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
125e0 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 *)id;.. assert(
125e1 20 69 64 21 3d 30 20 29 3b 0a 23 69 66 6e 64 65 id!=0 );.#ifnde
125e2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 f SQLITE_OMIT_WA
125e3 4c 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c L. assert( pFil
125e4 65 2d 3e 70 53 68 6d 3d 3d 30 20 29 3b 0a 23 65 e->pShm==0 );.#e
125e5 6e 64 69 66 0a 20 20 4f 53 54 52 41 43 45 28 28 ndif. OSTRACE((
125e6 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 "CLOSE %d\n", pF
125e7 69 6c 65 2d 3e 68 29 29 3b 0a 20 20 64 6f 7b 0a ile->h));. do{.
125e8 20 20 20 20 72 63 20 3d 20 6f 73 43 6c 6f 73 65 rc = osClose
125e9 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 29 Handle(pFile->h)
125ea 3b 0a 20 20 20 20 2f 2a 20 53 69 6d 75 6c 61 74 ;. /* Simulat
125eb 65 49 4f 45 72 72 6f 72 28 20 72 63 3d 30 3b 20 eIOError( rc=0;
125ec 63 6e 74 3d 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 cnt=MX_CLOSE_ATT
125ed 45 4d 50 54 3b 20 29 3b 20 2a 2f 0a 20 20 7d 77 EMPT; ); */. }w
125ee 68 69 6c 65 28 20 72 63 3d 3d 30 20 26 26 20 2b hile( rc==0 && +
125ef 2b 63 6e 74 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f +cnt < MX_CLOSE_
125f0 41 54 54 45 4d 50 54 20 26 26 20 28 73 71 6c 69 ATTEMPT && (sqli
125f1 74 65 33 5f 77 69 6e 33 32 5f 73 6c 65 65 70 28 te3_win32_sleep(
125f2 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 69 66 20 100), 1) );.#if
125f3 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
125f4 23 64 65 66 69 6e 65 20 57 49 4e 43 45 5f 44 45 #define WINCE_DE
125f5 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 LETION_ATTEMPTS
125f6 33 0a 20 20 77 69 6e 63 65 44 65 73 74 72 6f 79 3. winceDestroy
125f7 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 69 Lock(pFile);. i
125f8 66 28 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 f( pFile->zDelet
125f9 65 4f 6e 43 6c 6f 73 65 20 29 7b 0a 20 20 20 20 eOnClose ){.
125fa 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 int cnt = 0;.
125fb 20 77 68 69 6c 65 28 0a 20 20 20 20 20 20 20 20 while(.
125fc 20 20 20 6f 73 44 65 6c 65 74 65 46 69 6c 65 57 osDeleteFileW
125fd 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f (pFile->zDeleteO
125fe 6e 43 6c 6f 73 65 29 3d 3d 30 0a 20 20 20 20 20 nClose)==0.
125ff 20 20 20 26 26 20 6f 73 47 65 74 46 69 6c 65 41 && osGetFileA
12600 74 74 72 69 62 75 74 65 73 57 28 70 46 69 6c 65 ttributesW(pFile
12601 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 ->zDeleteOnClose
12602 29 21 3d 30 78 66 66 66 66 66 66 66 66 20 0a 20 )!=0xffffffff .
12603 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 && cnt++
12604 3c 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e < WINCE_DELETION
12605 5f 41 54 54 45 4d 50 54 53 0a 20 20 20 20 29 7b _ATTEMPTS. ){
12606 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f . sqlite3_
12607 77 69 6e 33 32 5f 73 6c 65 65 70 28 31 30 30 29 win32_sleep(100)
12608 3b 20 20 2f 2a 20 57 61 69 74 20 61 20 6c 69 74 ; /* Wait a lit
12609 74 6c 65 20 62 65 66 6f 72 65 20 74 72 79 69 6e tle before tryin
1260a 67 20 61 67 61 69 6e 20 2a 2f 0a 20 20 20 20 7d g again */. }
1260b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
1260c 65 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 e(pFile->zDelete
1260d 4f 6e 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23 65 OnClose);. }.#e
1260e 6e 64 69 66 0a 20 20 4f 53 54 52 41 43 45 28 28 ndif. OSTRACE((
1260f 22 43 4c 4f 53 45 20 25 64 20 25 73 5c 6e 22 2c "CLOSE %d %s\n",
12610 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 3f 20 pFile->h, rc ?
12611 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 "ok" : "failed")
12612 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 );. if( rc ){.
12613 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 4e 55 pFile->h = NU
12614 4c 4c 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e 43 6f LL;. }. OpenCo
12615 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 72 65 74 unter(-1);. ret
12616 75 72 6e 20 72 63 20 3f 20 53 51 4c 49 54 45 5f urn rc ? SQLITE_
12617 4f 4b 0a 20 20 20 20 20 20 20 20 20 20 20 20 3a OK. :
12618 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c winLogError(SQL
12619 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 2c ITE_IOERR_CLOSE,
1261a 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 osGetLastError(
1261b 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ),.
1261c 20 20 20 20 20 20 20 20 20 20 20 20 20 22 77 69 "wi
1261d 6e 43 6c 6f 73 65 22 2c 20 70 46 69 6c 65 2d 3e nClose", pFile->
1261e 7a 50 61 74 68 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a zPath);.}../*.**
1261f 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 Read data from
12620 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 a file into a bu
12621 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 ffer. Return SQ
12622 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a LITE_OK if all.*
12623 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61 * bytes were rea
12624 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 d successfully a
12625 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 nd SQLITE_IOERR
12626 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 if anything goes
12627 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 .** wrong..*/.st
12628 61 74 69 63 20 69 6e 74 20 77 69 6e 52 65 61 64 atic int winRead
12629 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
1262a 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 2f *id, /
1262b 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 * File to read f
1262c 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 rom */. void *p
1262d 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 Buf,
1262e 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e /* Write con
1262f 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 62 tent into this b
12630 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 uffer */. int a
12631 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 mt,
12632 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
12633 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 of bytes to read
12634 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e */. sqlite3_in
12635 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 t64 offset
12636 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e /* Begin readin
12637 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 g at this offset
12638 20 2a 2f 0a 29 7b 0a 23 69 66 20 21 53 51 4c 49 */.){.#if !SQLI
12639 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 4f 56 TE_OS_WINCE. OV
1263a 45 52 4c 41 50 50 45 44 20 6f 76 65 72 6c 61 70 ERLAPPED overlap
1263b 70 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ped; /*
1263c 20 54 68 65 20 6f 66 66 73 65 74 20 66 6f 72 20 The offset for
1263d 52 65 61 64 46 69 6c 65 2e 20 2a 2f 0a 23 65 6e ReadFile. */.#en
1263e 64 69 66 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 dif. winFile *p
1263f 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
12640 29 69 64 3b 20 20 2f 2a 20 66 69 6c 65 20 68 61 )id; /* file ha
12641 6e 64 6c 65 20 2a 2f 0a 20 20 44 57 4f 52 44 20 ndle */. DWORD
12642 6e 52 65 61 64 3b 20 20 20 20 20 20 20 20 20 20 nRead;
12643 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
12644 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 ber of bytes act
12645 75 61 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 ually read from
12646 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 file */. int nR
12647 65 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20 etry = 0;
12648 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
12649 62 65 72 20 6f 66 20 72 65 74 72 79 73 20 2a 2f ber of retrys */
1264a 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d .. assert( id!=
1264b 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 0 );. SimulateI
1264c 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 OError(return SQ
1264d 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 29 LITE_IOERR_READ)
1264e 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 52 45 ;. OSTRACE(("RE
1264f 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 AD %d lock=%d\n"
12650 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c , pFile->h, pFil
12651 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 0a e->locktype));..
12652 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
12653 4e 43 45 0a 20 20 69 66 28 20 73 65 65 6b 57 69 NCE. if( seekWi
12654 6e 46 69 6c 65 28 70 46 69 6c 65 2c 20 6f 66 66 nFile(pFile, off
12655 73 65 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 set) ){. retu
12656 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a rn SQLITE_FULL;.
12657 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 21 6f 73 }. while( !os
12658 52 65 61 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e ReadFile(pFile->
12659 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 6e h, pBuf, amt, &n
1265a 52 65 61 64 2c 20 30 29 20 29 7b 0a 23 65 6c 73 Read, 0) ){.#els
1265b 65 0a 20 20 6d 65 6d 73 65 74 28 26 6f 76 65 72 e. memset(&over
1265c 6c 61 70 70 65 64 2c 20 30 2c 20 73 69 7a 65 6f lapped, 0, sizeo
1265d 66 28 4f 56 45 52 4c 41 50 50 45 44 29 29 3b 0a f(OVERLAPPED));.
1265e 20 20 6f 76 65 72 6c 61 70 70 65 64 2e 4f 66 66 overlapped.Off
1265f 73 65 74 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 set = (LONG)(off
12660 73 65 74 20 26 20 30 78 66 66 66 66 66 66 66 66 set & 0xffffffff
12661 29 3b 0a 20 20 6f 76 65 72 6c 61 70 70 65 64 2e );. overlapped.
12662 4f 66 66 73 65 74 48 69 67 68 20 3d 20 28 4c 4f OffsetHigh = (LO
12663 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e 33 32 29 NG)((offset>>32)
12664 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a & 0x7fffffff);.
12665 20 20 77 68 69 6c 65 28 20 21 6f 73 52 65 61 64 while( !osRead
12666 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 File(pFile->h, p
12667 42 75 66 2c 20 61 6d 74 2c 20 26 6e 52 65 61 64 Buf, amt, &nRead
12668 2c 20 26 6f 76 65 72 6c 61 70 70 65 64 29 20 26 , &overlapped) &
12669 26 0a 20 20 20 20 20 20 20 20 20 6f 73 47 65 74 &. osGet
1266a 4c 61 73 74 45 72 72 6f 72 28 29 21 3d 45 52 52 LastError()!=ERR
1266b 4f 52 5f 48 41 4e 44 4c 45 5f 45 4f 46 20 29 7b OR_HANDLE_EOF ){
1266c 0a 23 65 6e 64 69 66 0a 20 20 20 20 44 57 4f 52 .#endif. DWOR
1266d 44 20 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 D lastErrno;.
1266e 20 69 66 28 20 72 65 74 72 79 49 6f 65 72 72 28 if( retryIoerr(
1266f 26 6e 52 65 74 72 79 2c 20 26 6c 61 73 74 45 72 &nRetry, &lastEr
12670 72 6e 6f 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b rno) ) continue;
12671 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
12672 45 72 72 6e 6f 20 3d 20 6c 61 73 74 45 72 72 6e Errno = lastErrn
12673 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 77 69 o;. return wi
12674 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 nLogError(SQLITE
12675 5f 49 4f 45 52 52 5f 52 45 41 44 2c 20 70 46 69 _IOERR_READ, pFi
12676 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c 0a 20 le->lastErrno,.
12677 20 20 20 20 20 20 20 20 20 20 20 20 22 77 69 6e "win
12678 52 65 61 64 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 Read", pFile->zP
12679 61 74 68 29 3b 0a 20 20 7d 0a 20 20 6c 6f 67 49 ath);. }. logI
1267a 6f 65 72 72 28 6e 52 65 74 72 79 29 3b 0a 20 20 oerr(nRetry);.
1267b 69 66 28 20 6e 52 65 61 64 3c 28 44 57 4f 52 44 if( nRead<(DWORD
1267c 29 61 6d 74 20 29 7b 0a 20 20 20 20 2f 2a 20 55 )amt ){. /* U
1267d 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 nread parts of t
1267e 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 he buffer must b
1267f 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f e zero-filled */
12680 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 . memset(&((c
12681 68 61 72 2a 29 70 42 75 66 29 5b 6e 52 65 61 64 har*)pBuf)[nRead
12682 5d 2c 20 30 2c 20 61 6d 74 2d 6e 52 65 61 64 29 ], 0, amt-nRead)
12683 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
12684 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f ITE_IOERR_SHORT_
12685 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 READ;. }.. ret
12686 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
12687 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 ../*.** Write da
12688 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 ta from a buffer
12689 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 into a file. R
1268a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
1268b 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 on success.** or
1268c 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f some other erro
1268d 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 r code on failur
1268e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1268f 20 77 69 6e 57 72 69 74 65 28 0a 20 20 73 71 6c winWrite(. sql
12690 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 ite3_file *id,
12691 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12692 46 69 6c 65 20 74 6f 20 77 72 69 74 65 20 69 6e File to write in
12693 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f to */. const vo
12694 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 id *pBuf,
12695 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 /* The b
12696 79 74 65 73 20 74 6f 20 62 65 20 77 72 69 74 74 ytes to be writt
12697 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c en */. int amt,
12698 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12699 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1269a 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 r of bytes to wr
1269b 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ite */. sqlite3
1269c 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 _int64 offset
1269d 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
1269e 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 et into the file
1269f 20 74 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e to begin writin
126a0 67 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 g at */.){. int
126a1 20 72 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 rc = 0;
126a2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
126a3 54 72 75 65 20 69 66 20 65 72 72 6f 72 20 68 61 True if error ha
126a4 73 20 6f 63 63 75 72 65 64 2c 20 65 6c 73 65 20 s occured, else
126a5 66 61 6c 73 65 20 2a 2f 0a 20 20 77 69 6e 46 69 false */. winFi
126a6 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
126a7 46 69 6c 65 2a 29 69 64 3b 20 20 2f 2a 20 46 69 File*)id; /* Fi
126a8 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 le handle */. i
126a9 6e 74 20 6e 52 65 74 72 79 20 3d 20 30 3b 20 20 nt nRetry = 0;
126aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
126ab 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 74 72 * Number of retr
126ac 69 65 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ies */.. assert
126ad 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 61 73 73 ( amt>0 );. ass
126ae 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
126af 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
126b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
126b1 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20 53 69 ERR_WRITE);. Si
126b2 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
126b3 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 ror(return SQLIT
126b4 45 5f 46 55 4c 4c 29 3b 0a 0a 20 20 4f 53 54 52 E_FULL);.. OSTR
126b5 41 43 45 28 28 22 57 52 49 54 45 20 25 64 20 6c ACE(("WRITE %d l
126b6 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 ock=%d\n", pFile
126b7 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ->h, pFile->lock
126b8 74 79 70 65 29 29 3b 0a 0a 23 69 66 20 53 51 4c type));..#if SQL
126b9 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 72 ITE_OS_WINCE. r
126ba 63 20 3d 20 73 65 65 6b 57 69 6e 46 69 6c 65 28 c = seekWinFile(
126bb 70 46 69 6c 65 2c 20 6f 66 66 73 65 74 29 3b 0a pFile, offset);.
126bc 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 23 if( rc==0 ){.#
126bd 65 6c 73 65 0a 20 20 7b 0a 23 65 6e 64 69 66 0a else. {.#endif.
126be 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 #if !SQLITE_OS_W
126bf 49 4e 43 45 0a 20 20 20 20 4f 56 45 52 4c 41 50 INCE. OVERLAP
126c0 50 45 44 20 6f 76 65 72 6c 61 70 70 65 64 3b 20 PED overlapped;
126c1 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 66 /* The of
126c2 66 73 65 74 20 66 6f 72 20 57 72 69 74 65 46 69 fset for WriteFi
126c3 6c 65 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 le. */.#endif.
126c4 20 20 75 38 20 2a 61 52 65 6d 20 3d 20 28 75 38 u8 *aRem = (u8
126c5 20 2a 29 70 42 75 66 3b 20 20 20 20 20 20 20 20 *)pBuf;
126c6 2f 2a 20 44 61 74 61 20 79 65 74 20 74 6f 20 62 /* Data yet to b
126c7 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 20 e written */.
126c8 20 69 6e 74 20 6e 52 65 6d 20 3d 20 61 6d 74 3b int nRem = amt;
126c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
126ca 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
126cb 73 20 79 65 74 20 74 6f 20 62 65 20 77 72 69 74 s yet to be writ
126cc 74 65 6e 20 2a 2f 0a 20 20 20 20 44 57 4f 52 44 ten */. DWORD
126cd 20 6e 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 nWrite;
126ce 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 /* Byte
126cf 73 20 77 72 69 74 74 65 6e 20 62 79 20 65 61 63 s written by eac
126d0 68 20 57 72 69 74 65 46 69 6c 65 28 29 20 63 61 h WriteFile() ca
126d1 6c 6c 20 2a 2f 0a 20 20 20 20 44 57 4f 52 44 20 ll */. DWORD
126d2 6c 61 73 74 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 lastErrno = NO_E
126d3 52 52 4f 52 3b 20 20 20 2f 2a 20 56 61 6c 75 65 RROR; /* Value
126d4 20 72 65 74 75 72 6e 65 64 20 62 79 20 47 65 74 returned by Get
126d5 4c 61 73 74 45 72 72 6f 72 28 29 20 2a 2f 0a 0a LastError() */..
126d6 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 #if !SQLITE_OS_W
126d7 49 4e 43 45 0a 20 20 20 20 6d 65 6d 73 65 74 28 INCE. memset(
126d8 26 6f 76 65 72 6c 61 70 70 65 64 2c 20 30 2c 20 &overlapped, 0,
126d9 73 69 7a 65 6f 66 28 4f 56 45 52 4c 41 50 50 45 sizeof(OVERLAPPE
126da 44 29 29 3b 0a 20 20 20 20 6f 76 65 72 6c 61 70 D));. overlap
126db 70 65 64 2e 4f 66 66 73 65 74 20 3d 20 28 4c 4f ped.Offset = (LO
126dc 4e 47 29 28 6f 66 66 73 65 74 20 26 20 30 78 66 NG)(offset & 0xf
126dd 66 66 66 66 66 66 66 29 3b 0a 20 20 20 20 6f 76 fffffff);. ov
126de 65 72 6c 61 70 70 65 64 2e 4f 66 66 73 65 74 48 erlapped.OffsetH
126df 69 67 68 20 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 igh = (LONG)((of
126e0 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66 fset>>32) & 0x7f
126e1 66 66 66 66 66 66 29 3b 0a 23 65 6e 64 69 66 0a ffffff);.#endif.
126e2 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 52 65 6d . while( nRem
126e3 3e 30 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 >0 ){.#if SQLITE
126e4 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 20 20 _OS_WINCE.
126e5 69 66 28 20 21 6f 73 57 72 69 74 65 46 69 6c 65 if( !osWriteFile
126e6 28 70 46 69 6c 65 2d 3e 68 2c 20 61 52 65 6d 2c (pFile->h, aRem,
126e7 20 6e 52 65 6d 2c 20 26 6e 57 72 69 74 65 2c 20 nRem, &nWrite,
126e8 30 29 20 29 7b 0a 23 65 6c 73 65 0a 20 20 20 20 0) ){.#else.
126e9 20 20 69 66 28 20 21 6f 73 57 72 69 74 65 46 69 if( !osWriteFi
126ea 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 61 52 65 le(pFile->h, aRe
126eb 6d 2c 20 6e 52 65 6d 2c 20 26 6e 57 72 69 74 65 m, nRem, &nWrite
126ec 2c 20 26 6f 76 65 72 6c 61 70 70 65 64 29 20 29 , &overlapped) )
126ed 7b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 {.#endif.
126ee 20 69 66 28 20 72 65 74 72 79 49 6f 65 72 72 28 if( retryIoerr(
126ef 26 6e 52 65 74 72 79 2c 20 26 6c 61 73 74 45 72 &nRetry, &lastEr
126f0 72 6e 6f 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b rno) ) continue;
126f1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
126f2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 }. as
126f3 73 65 72 74 28 20 6e 57 72 69 74 65 3d 3d 30 20 sert( nWrite==0
126f4 7c 7c 20 6e 57 72 69 74 65 3c 3d 28 44 57 4f 52 || nWrite<=(DWOR
126f5 44 29 6e 52 65 6d 20 29 3b 0a 20 20 20 20 20 20 D)nRem );.
126f6 69 66 28 20 6e 57 72 69 74 65 3d 3d 30 20 7c 7c if( nWrite==0 ||
126f7 20 6e 57 72 69 74 65 3e 28 44 57 4f 52 44 29 6e nWrite>(DWORD)n
126f8 52 65 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 6c Rem ){. l
126f9 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73 47 65 74 astErrno = osGet
126fa 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 LastError();.
126fb 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
126fc 20 20 7d 0a 23 69 66 20 21 53 51 4c 49 54 45 5f }.#if !SQLITE_
126fd 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 20 20 6f OS_WINCE. o
126fe 66 66 73 65 74 20 2b 3d 20 6e 57 72 69 74 65 3b ffset += nWrite;
126ff 0a 20 20 20 20 20 20 6f 76 65 72 6c 61 70 70 65 . overlappe
12700 64 2e 4f 66 66 73 65 74 20 3d 20 28 4c 4f 4e 47 d.Offset = (LONG
12701 29 28 6f 66 66 73 65 74 20 26 20 30 78 66 66 66 )(offset & 0xfff
12702 66 66 66 66 66 29 3b 0a 20 20 20 20 20 20 6f 76 fffff);. ov
12703 65 72 6c 61 70 70 65 64 2e 4f 66 66 73 65 74 48 erlapped.OffsetH
12704 69 67 68 20 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 igh = (LONG)((of
12705 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66 fset>>32) & 0x7f
12706 66 66 66 66 66 66 29 3b 0a 23 65 6e 64 69 66 0a ffffff);.#endif.
12707 20 20 20 20 20 20 61 52 65 6d 20 2b 3d 20 6e 57 aRem += nW
12708 72 69 74 65 3b 0a 20 20 20 20 20 20 6e 52 65 6d rite;. nRem
12709 20 2d 3d 20 6e 57 72 69 74 65 3b 0a 20 20 20 20 -= nWrite;.
1270a 7d 0a 20 20 20 20 69 66 28 20 6e 52 65 6d 3e 30 }. if( nRem>0
1270b 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ){. pFile-
1270c 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 61 73 >lastErrno = las
1270d 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 tErrno;. rc
1270e 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 1;. }. }.
1270f 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
12710 20 69 66 28 20 20 20 28 20 70 46 69 6c 65 2d 3e if( ( pFile->
12711 6c 61 73 74 45 72 72 6e 6f 3d 3d 45 52 52 4f 52 lastErrno==ERROR
12712 5f 48 41 4e 44 4c 45 5f 44 49 53 4b 5f 46 55 4c _HANDLE_DISK_FUL
12713 4c 20 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 20 L ). || (
12714 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
12715 3d 3d 45 52 52 4f 52 5f 44 49 53 4b 5f 46 55 4c ==ERROR_DISK_FUL
12716 4c 20 29 29 7b 0a 20 20 20 20 20 20 72 65 74 75 L )){. retu
12717 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a rn SQLITE_FULL;.
12718 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
12719 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c winLogError(SQL
1271a 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 2c ITE_IOERR_WRITE,
1271b 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
1271c 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 o,.
1271d 22 77 69 6e 57 72 69 74 65 22 2c 20 70 46 69 6c "winWrite", pFil
1271e 65 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 7d 65 6c e->zPath);. }el
1271f 73 65 7b 0a 20 20 20 20 6c 6f 67 49 6f 65 72 72 se{. logIoerr
12720 28 6e 52 65 74 72 79 29 3b 0a 20 20 7d 0a 20 20 (nRetry);. }.
12721 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
12722 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 ;.}../*.** Trunc
12723 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 ate an open file
12724 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 to a specified
12725 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 size.*/.static i
12726 6e 74 20 77 69 6e 54 72 75 6e 63 61 74 65 28 73 nt winTruncate(s
12727 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
12728 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e sqlite3_int64 n
12729 42 79 74 65 29 7b 0a 20 20 77 69 6e 46 69 6c 65 Byte){. winFile
1272a 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 *pFile = (winFi
1272b 6c 65 2a 29 69 64 3b 20 20 2f 2a 20 46 69 6c 65 le*)id; /* File
1272c 20 68 61 6e 64 6c 65 20 6f 62 6a 65 63 74 20 2a handle object *
1272d 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
1272e 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 ITE_OK;
1272f 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
12730 64 65 20 66 6f 72 20 74 68 69 73 20 66 75 6e 63 de for this func
12731 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 tion */.. asser
12732 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 4f t( pFile );.. O
12733 53 54 52 41 43 45 28 28 22 54 52 55 4e 43 41 54 STRACE(("TRUNCAT
12734 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 E %d %lld\n", pF
12735 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 29 3b ile->h, nByte));
12736 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
12737 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 or(return SQLITE
12738 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 29 _IOERR_TRUNCATE)
12739 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 75 ;.. /* If the u
1273a 73 65 72 20 68 61 73 20 63 6f 6e 66 69 67 75 72 ser has configur
1273b 65 64 20 61 20 63 68 75 6e 6b 2d 73 69 7a 65 20 ed a chunk-size
1273c 66 6f 72 20 74 68 69 73 20 66 69 6c 65 2c 20 74 for this file, t
1273d 72 75 6e 63 61 74 65 20 74 68 65 0a 20 20 2a 2a runcate the. **
1273e 20 66 69 6c 65 20 73 6f 20 74 68 61 74 20 69 74 file so that it
1273f 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 6e 20 consists of an
12740 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f integer number o
12741 66 20 63 68 75 6e 6b 73 20 28 69 2e 65 2e 20 74 f chunks (i.e. t
12742 68 65 0a 20 20 2a 2a 20 61 63 74 75 61 6c 20 66 he. ** actual f
12743 69 6c 65 20 73 69 7a 65 20 61 66 74 65 72 20 74 ile size after t
12744 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 6d 61 79 he operation may
12745 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 be larger than
12746 74 68 65 20 72 65 71 75 65 73 74 65 64 0a 20 20 the requested.
12747 2a 2a 20 73 69 7a 65 29 2e 0a 20 20 2a 2f 0a 20 ** size).. */.
12748 20 69 66 28 20 70 46 69 6c 65 2d 3e 73 7a 43 68 if( pFile->szCh
12749 75 6e 6b 3e 30 20 29 7b 0a 20 20 20 20 6e 42 79 unk>0 ){. nBy
1274a 74 65 20 3d 20 28 28 6e 42 79 74 65 20 2b 20 70 te = ((nByte + p
1274b 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 20 2d 20 File->szChunk -
1274c 31 29 2f 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 1)/pFile->szChun
1274d 6b 29 20 2a 20 70 46 69 6c 65 2d 3e 73 7a 43 68 k) * pFile->szCh
1274e 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 unk;. }.. /* S
1274f 65 74 45 6e 64 4f 66 46 69 6c 65 28 29 20 72 65 etEndOfFile() re
12750 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 77 turns non-zero w
12751 68 65 6e 20 73 75 63 63 65 73 73 66 75 6c 2c 20 hen successful,
12752 6f 72 20 7a 65 72 6f 20 77 68 65 6e 20 69 74 20 or zero when it
12753 66 61 69 6c 73 2e 20 2a 2f 0a 20 20 69 66 28 20 fails. */. if(
12754 73 65 65 6b 57 69 6e 46 69 6c 65 28 70 46 69 6c seekWinFile(pFil
12755 65 2c 20 6e 42 79 74 65 29 20 29 7b 0a 20 20 20 e, nByte) ){.
12756 20 72 63 20 3d 20 77 69 6e 4c 6f 67 45 72 72 6f rc = winLogErro
12757 72 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 r(SQLITE_IOERR_T
12758 52 55 4e 43 41 54 45 2c 20 70 46 69 6c 65 2d 3e RUNCATE, pFile->
12759 6c 61 73 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20 lastErrno,.
1275a 20 20 20 20 20 20 20 20 22 77 69 6e 54 72 75 6e "winTrun
1275b 63 61 74 65 31 22 2c 20 70 46 69 6c 65 2d 3e 7a cate1", pFile->z
1275c 50 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65 20 69 Path);. }else i
1275d 66 28 20 30 3d 3d 6f 73 53 65 74 45 6e 64 4f 66 f( 0==osSetEndOf
1275e 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 29 20 29 File(pFile->h) )
1275f 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
12760 74 45 72 72 6e 6f 20 3d 20 6f 73 47 65 74 4c 61 tErrno = osGetLa
12761 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 stError();. r
12762 63 20 3d 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 c = winLogError(
12763 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 SQLITE_IOERR_TRU
12764 4e 43 41 54 45 2c 20 70 46 69 6c 65 2d 3e 6c 61 NCATE, pFile->la
12765 73 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20 20 stErrno,.
12766 20 20 20 20 20 20 22 77 69 6e 54 72 75 6e 63 61 "winTrunca
12767 74 65 32 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 te2", pFile->zPa
12768 74 68 29 3b 0a 20 20 7d 0a 0a 20 20 4f 53 54 52 th);. }.. OSTR
12769 41 43 45 28 28 22 54 52 55 4e 43 41 54 45 20 25 ACE(("TRUNCATE %
1276a 64 20 25 6c 6c 64 20 25 73 5c 6e 22 2c 20 70 46 d %lld %s\n", pF
1276b 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 2c 20 72 ile->h, nByte, r
1276c 63 20 3f 20 22 66 61 69 6c 65 64 22 20 3a 20 22 c ? "failed" : "
1276d 6f 6b 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 ok"));. return
1276e 72 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 rc;.}..#ifdef SQ
1276f 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 LITE_TEST./*.**
12770 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 Count the number
12771 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e of fullsyncs an
12772 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 d normal syncs.
12773 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f This is used to
12774 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 test.** that sy
12775 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 ncs and fullsync
12776 73 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61 s are occuring a
12777 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 t the right time
12778 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
12779 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e int sqlite3_syn
1277a 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c c_count = 0;.SQL
1277b 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1277c 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 te3_fullsync_cou
1277d 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a nt = 0;.#endif..
1277e 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
1277f 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 all writes to a
12780 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 particular file
12781 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f are committed to
12782 20 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 disk..*/.static
12783 20 69 6e 74 20 77 69 6e 53 79 6e 63 28 73 71 6c int winSync(sql
12784 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
12785 6e 74 20 66 6c 61 67 73 29 7b 0a 23 69 66 6e 64 nt flags){.#ifnd
12786 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e ef SQLITE_NO_SYN
12787 43 0a 20 20 2f 2a 0a 20 20 2a 2a 20 55 73 65 64 C. /*. ** Used
12788 20 6f 6e 6c 79 20 77 68 65 6e 20 53 51 4c 49 54 only when SQLIT
12789 45 5f 4e 4f 5f 53 59 4e 43 20 69 73 20 6e 6f 74 E_NO_SYNC is not
1278a 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 2a 2f 0a defined.. */.
1278b 20 20 42 4f 4f 4c 20 72 63 3b 0a 23 65 6e 64 69 BOOL rc;.#endi
1278c 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e f.#if !defined(N
1278d 44 45 42 55 47 29 20 7c 7c 20 21 64 65 66 69 6e DEBUG) || !defin
1278e 65 64 28 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e ed(SQLITE_NO_SYN
1278f 43 29 20 7c 7c 20 5c 0a 20 20 20 20 28 64 65 66 C) || \. (def
12790 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
12791 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c ) && defined(SQL
12792 49 54 45 5f 44 45 42 55 47 29 29 0a 20 20 2f 2a ITE_DEBUG)). /*
12793 0a 20 20 2a 2a 20 55 73 65 64 20 77 68 65 6e 20 . ** Used when
12794 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 69 SQLITE_NO_SYNC i
12795 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 6e s not defined an
12796 64 20 62 79 20 74 68 65 20 61 73 73 65 72 74 28 d by the assert(
12797 29 20 61 6e 64 2f 6f 72 0a 20 20 2a 2a 20 4f 53 ) and/or. ** OS
12798 54 52 41 43 45 28 29 20 6d 61 63 72 6f 73 2e 0a TRACE() macros..
12799 20 20 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 */. winFile
1279a 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c *pFile = (winFil
1279b 65 2a 29 69 64 3b 0a 23 65 6c 73 65 0a 20 20 55 e*)id;.#else. U
1279c 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
1279d 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 61 id);.#endif.. a
1279e 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
1279f 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 /* Check that
127a0 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 53 59 one of SQLITE_SY
127a1 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 46 55 4c NC_NORMAL or FUL
127a2 4c 20 77 61 73 20 70 61 73 73 65 64 20 2a 2f 0a L was passed */.
127a3 20 20 61 73 73 65 72 74 28 28 66 6c 61 67 73 26 assert((flags&
127a4 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 0x0F)==SQLITE_SY
127a5 4e 43 5f 4e 4f 52 4d 41 4c 0a 20 20 20 20 20 20 NC_NORMAL.
127a6 7c 7c 20 28 66 6c 61 67 73 26 30 78 30 46 29 3d || (flags&0x0F)=
127a7 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c =SQLITE_SYNC_FUL
127a8 4c 0a 20 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43 L. );.. OSTRAC
127a9 45 28 28 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b E(("SYNC %d lock
127aa 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
127ab 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 , pFile->locktyp
127ac 65 29 29 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 e));.. /* Unix
127ad 63 61 6e 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 cannot, but some
127ae 20 73 79 73 74 65 6d 73 20 6d 61 79 20 72 65 74 systems may ret
127af 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 urn SQLITE_FULL
127b0 66 72 6f 6d 20 68 65 72 65 2e 20 54 68 69 73 0a from here. This.
127b1 20 20 2a 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 ** line is to
127b2 74 65 73 74 20 74 68 61 74 20 64 6f 69 6e 67 20 test that doing
127b3 73 6f 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 so does not caus
127b4 65 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a e any problems..
127b5 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 */. SimulateD
127b6 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 iskfullError( re
127b7 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c turn SQLITE_FULL
127b8 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c );..#ifndef SQL
127b9 49 54 45 5f 54 45 53 54 0a 20 20 55 4e 55 53 45 ITE_TEST. UNUSE
127ba 44 5f 50 41 52 41 4d 45 54 45 52 28 66 6c 61 67 D_PARAMETER(flag
127bb 73 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 s);.#else. if(
127bc 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 (flags&0x0F)==SQ
127bd 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 29 LITE_SYNC_FULL )
127be 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 75 {. sqlite3_fu
127bf 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a llsync_count++;.
127c0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 }. sqlite3_sy
127c1 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 nc_count++;.#end
127c2 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 if.. /* If we c
127c3 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
127c4 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 SQLITE_NO_SYNC
127c5 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 flag, then synci
127c6 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d ng is a. ** no-
127c7 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 op. */.#ifdef S
127c8 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 QLITE_NO_SYNC.
127c9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
127ca 3b 0a 23 65 6c 73 65 0a 20 20 72 63 20 3d 20 6f ;.#else. rc = o
127cb 73 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72 sFlushFileBuffer
127cc 73 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 53 s(pFile->h);. S
127cd 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
127ce 72 63 3d 46 41 4c 53 45 20 29 3b 0a 20 20 69 66 rc=FALSE );. if
127cf 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 ( rc ){. retu
127d0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
127d1 7d 65 6c 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 }else{. pFile
127d2 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73 ->lastErrno = os
127d3 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a GetLastError();.
127d4 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e 4c 6f return winLo
127d5 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 49 4f gError(SQLITE_IO
127d6 45 52 52 5f 46 53 59 4e 43 2c 20 70 46 69 6c 65 ERR_FSYNC, pFile
127d7 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c 0a 20 20 20 ->lastErrno,.
127d8 20 20 20 20 20 20 20 20 20 20 22 77 69 6e 53 79 "winSy
127d9 6e 63 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 nc", pFile->zPat
127da 68 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d h);. }.#endif.}
127db 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e ../*.** Determin
127dc 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 e the current si
127dd 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 ze of a file in
127de 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 bytes.*/.static
127df 69 6e 74 20 77 69 6e 46 69 6c 65 53 69 7a 65 28 int winFileSize(
127e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
127e1 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
127e2 2a 70 53 69 7a 65 29 7b 0a 20 20 77 69 6e 46 69 *pSize){. winFi
127e3 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
127e4 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 File*)id;. int
127e5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
127e6 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 . assert( id!=0
127e7 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f );. SimulateIO
127e8 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c Error(return SQL
127e9 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 29 ITE_IOERR_FSTAT)
127ea 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ;.#if SQLITE_OS_
127eb 57 49 4e 52 54 0a 20 20 7b 0a 20 20 20 20 46 49 WINRT. {. FI
127ec 4c 45 5f 53 54 41 4e 44 41 52 44 5f 49 4e 46 4f LE_STANDARD_INFO
127ed 20 69 6e 66 6f 3b 0a 20 20 20 20 69 66 28 20 6f info;. if( o
127ee 73 47 65 74 46 69 6c 65 49 6e 66 6f 72 6d 61 74 sGetFileInformat
127ef 69 6f 6e 42 79 48 61 6e 64 6c 65 45 78 28 70 46 ionByHandleEx(pF
127f0 69 6c 65 2d 3e 68 2c 20 46 69 6c 65 53 74 61 6e ile->h, FileStan
127f1 64 61 72 64 49 6e 66 6f 2c 0a 20 20 20 20 20 20 dardInfo,.
127f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
127f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
127f4 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69 6e 66 info, sizeof(inf
127f5 6f 29 29 20 29 7b 0a 20 20 20 20 20 20 2a 70 53 o)) ){. *pS
127f6 69 7a 65 20 3d 20 69 6e 66 6f 2e 45 6e 64 4f 66 ize = info.EndOf
127f7 46 69 6c 65 2e 51 75 61 64 50 61 72 74 3b 0a 20 File.QuadPart;.
127f8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
127f9 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
127fa 20 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f = osGetLastErro
127fb 72 28 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 r();. rc =
127fc 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 winLogError(SQLI
127fd 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 2c 20 TE_IOERR_FSTAT,
127fe 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
127ff 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
12800 20 20 20 20 20 20 20 20 20 22 77 69 6e 46 69 6c "winFil
12801 65 53 69 7a 65 22 2c 20 70 46 69 6c 65 2d 3e 7a eSize", pFile->z
12802 50 61 74 68 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Path);. }. }
12803 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 44 .#else. {. D
12804 57 4f 52 44 20 75 70 70 65 72 42 69 74 73 3b 0a WORD upperBits;.
12805 20 20 20 20 44 57 4f 52 44 20 6c 6f 77 65 72 42 DWORD lowerB
12806 69 74 73 3b 0a 20 20 20 20 44 57 4f 52 44 20 6c its;. DWORD l
12807 61 73 74 45 72 72 6e 6f 3b 0a 0a 20 20 20 20 6c astErrno;.. l
12808 6f 77 65 72 42 69 74 73 20 3d 20 6f 73 47 65 74 owerBits = osGet
12809 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2d 3e FileSize(pFile->
1280a 68 2c 20 26 75 70 70 65 72 42 69 74 73 29 3b 0a h, &upperBits);.
1280b 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 28 28 28 *pSize = (((
1280c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 75 70 sqlite3_int64)up
1280d 70 65 72 42 69 74 73 29 3c 3c 33 32 29 20 2b 20 perBits)<<32) +
1280e 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 20 20 69 lowerBits;. i
1280f 66 28 20 20 20 28 6c 6f 77 65 72 42 69 74 73 20 f( (lowerBits
12810 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f == INVALID_FILE_
12811 53 49 5a 45 29 0a 20 20 20 20 20 20 20 26 26 20 SIZE). &&
12812 28 28 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73 ((lastErrno = os
12813 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21 GetLastError())!
12814 3d 4e 4f 5f 45 52 52 4f 52 29 20 29 7b 0a 20 20 =NO_ERROR) ){.
12815 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
12816 72 72 6e 6f 20 3d 20 6c 61 73 74 45 72 72 6e 6f rrno = lastErrno
12817 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 69 6e ;. rc = win
12818 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f LogError(SQLITE_
12819 49 4f 45 52 52 5f 46 53 54 41 54 2c 20 70 46 69 IOERR_FSTAT, pFi
1281a 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c 0a 20 le->lastErrno,.
1281b 20 20 20 20 20 20 20 20 20 20 20 20 22 77 69 6e "win
1281c 46 69 6c 65 53 69 7a 65 22 2c 20 70 46 69 6c 65 FileSize", pFile
1281d 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 20 20 7d 0a ->zPath);. }.
1281e 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 }.#endif. ret
1281f 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
12820 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 LOCKFILE_FAIL_I
12821 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20 75 6e MMEDIATELY is un
12822 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 defined on some
12823 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e Windows systems.
12824 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f 43 4b .*/.#ifndef LOCK
12825 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 FILE_FAIL_IMMEDI
12826 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65 20 4c ATELY.# define L
12827 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d OCKFILE_FAIL_IMM
12828 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e 64 69 EDIATELY 1.#endi
12829 66 0a 0a 23 69 66 6e 64 65 66 20 4c 4f 43 4b 46 f..#ifndef LOCKF
1282a 49 4c 45 5f 45 58 43 4c 55 53 49 56 45 5f 4c 4f ILE_EXCLUSIVE_LO
1282b 43 4b 0a 23 20 64 65 66 69 6e 65 20 4c 4f 43 4b CK.# define LOCK
1282c 46 49 4c 45 5f 45 58 43 4c 55 53 49 56 45 5f 4c FILE_EXCLUSIVE_L
1282d 4f 43 4b 20 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a OCK 2.#endif../*
1282e 0a 2a 2a 20 48 69 73 74 6f 72 69 63 61 6c 6c 79 .** Historically
1282f 2c 20 53 51 4c 69 74 65 20 68 61 73 20 75 73 65 , SQLite has use
12830 64 20 62 6f 74 68 20 74 68 65 20 4c 6f 63 6b 46 d both the LockF
12831 69 6c 65 20 61 6e 64 20 4c 6f 63 6b 46 69 6c 65 ile and LockFile
12832 45 78 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a Ex functions..**
12833 20 57 68 65 6e 20 74 68 65 20 4c 6f 63 6b 46 69 When the LockFi
12834 6c 65 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 le function was
12835 75 73 65 64 2c 20 69 74 20 77 61 73 20 61 6c 77 used, it was alw
12836 61 79 73 20 65 78 70 65 63 74 65 64 20 74 6f 20 ays expected to
12837 66 61 69 6c 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 fail.** immediat
12838 65 6c 79 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 ely if the lock
12839 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 6f 62 74 could not be obt
1283a 61 69 6e 65 64 2e 20 20 41 6c 73 6f 2c 20 69 74 ained. Also, it
1283b 20 61 6c 77 61 79 73 20 65 78 70 65 63 74 65 64 always expected
1283c 20 74 6f 0a 2a 2a 20 6f 62 74 61 69 6e 20 61 6e to.** obtain an
1283d 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e exclusive lock.
1283e 20 20 54 68 65 73 65 20 66 6c 61 67 73 20 61 72 These flags ar
1283f 65 20 75 73 65 64 20 77 69 74 68 20 74 68 65 20 e used with the
12840 4c 6f 63 6b 46 69 6c 65 45 78 20 66 75 6e 63 74 LockFileEx funct
12841 69 6f 6e 0a 2a 2a 20 61 6e 64 20 72 65 66 6c 65 ion.** and refle
12842 63 74 20 74 68 6f 73 65 20 65 78 70 65 63 74 61 ct those expecta
12843 74 69 6f 6e 73 3b 20 74 68 65 72 65 66 6f 72 65 tions; therefore
12844 2c 20 74 68 65 79 20 73 68 6f 75 6c 64 20 6e 6f , they should no
12845 74 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f t be changed..*/
12846 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
12847 4c 4f 43 4b 46 49 4c 45 5f 46 4c 41 47 53 0a 23 LOCKFILE_FLAGS.#
12848 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c define SQLITE_L
12849 4f 43 4b 46 49 4c 45 5f 46 4c 41 47 53 20 20 20 OCKFILE_FLAGS
1284a 28 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 (LOCKFILE_FAIL_I
1284b 4d 4d 45 44 49 41 54 45 4c 59 20 7c 20 5c 0a 20 MMEDIATELY | \.
1284c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1284d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1284e 20 4c 4f 43 4b 46 49 4c 45 5f 45 58 43 4c 55 53 LOCKFILE_EXCLUS
1284f 49 56 45 5f 4c 4f 43 4b 29 0a 23 65 6e 64 69 66 IVE_LOCK).#endif
12850 0a 0a 2f 2a 0a 2a 2a 20 43 75 72 72 65 6e 74 6c ../*.** Currentl
12851 79 2c 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20 y, SQLite never
12852 63 61 6c 6c 73 20 74 68 65 20 4c 6f 63 6b 46 69 calls the LockFi
12853 6c 65 45 78 20 66 75 6e 63 74 69 6f 6e 20 77 69 leEx function wi
12854 74 68 6f 75 74 20 77 61 6e 74 69 6e 67 20 74 68 thout wanting th
12855 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 66 61 69 e.** call to fai
12856 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 69 66 l immediately if
12857 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 the lock cannot
12858 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f be obtained..*/
12859 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1285a 4c 4f 43 4b 46 49 4c 45 45 58 5f 46 4c 41 47 53 LOCKFILEEX_FLAGS
1285b 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
1285c 5f 4c 4f 43 4b 46 49 4c 45 45 58 5f 46 4c 41 47 _LOCKFILEEX_FLAG
1285d 53 20 28 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c S (LOCKFILE_FAIL
1285e 5f 49 4d 4d 45 44 49 41 54 45 4c 59 29 0a 23 65 _IMMEDIATELY).#e
1285f 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 ndif../*.** Acqu
12860 69 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 ire a reader loc
12861 6b 2e 0a 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 k..** Different
12862 41 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 API routines are
12863 20 63 61 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e called dependin
12864 67 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 g on whether or
12865 6e 6f 74 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 not this.** is W
12866 69 6e 39 78 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a in9x or WinNT..*
12867 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 /.static int get
12868 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 ReadLock(winFile
12869 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 *pFile){. int
1286a 72 65 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 res;. if( isNT(
1286b 29 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f ) ){.#if SQLITE_
1286c 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 2f 2a 0a OS_WINCE. /*.
1286d 20 20 20 20 2a 2a 20 4e 4f 54 45 3a 20 57 69 6e ** NOTE: Win
1286e 64 6f 77 73 20 43 45 20 69 73 20 68 61 6e 64 6c dows CE is handl
1286f 65 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20 68 ed differently h
12870 65 72 65 20 64 75 65 20 69 74 73 20 6c 61 63 6b ere due its lack
12871 20 6f 66 20 74 68 65 20 57 69 6e 33 32 0a 20 20 of the Win32.
12872 20 20 2a 2a 20 20 20 20 20 20 20 41 50 49 20 4c ** API L
12873 6f 63 6b 46 69 6c 65 45 78 2e 0a 20 20 20 20 2a ockFileEx.. *
12874 2f 0a 20 20 20 20 72 65 73 20 3d 20 77 69 6e 63 /. res = winc
12875 65 4c 6f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65 eLockFile(&pFile
12876 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 ->h, SHARED_FIRS
12877 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6c T, 0, 1, 0);.#el
12878 73 65 0a 20 20 20 20 72 65 73 20 3d 20 77 69 6e se. res = win
12879 4c 6f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65 2d LockFile(&pFile-
1287a 3e 68 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 46 >h, SQLITE_LOCKF
1287b 49 4c 45 45 58 5f 46 4c 41 47 53 2c 20 53 48 41 ILEEX_FLAGS, SHA
1287c 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 0a 20 20 RED_FIRST, 0,.
1287d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1287e 20 20 20 20 53 48 41 52 45 44 5f 53 49 5a 45 2c SHARED_SIZE,
1287f 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0);.#endif. }.
12880 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 57 49 #ifdef SQLITE_WI
12881 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20 20 65 N32_HAS_ANSI. e
12882 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 3b lse{. int lk;
12883 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e . sqlite3_ran
12884 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 6c domness(sizeof(l
12885 6b 29 2c 20 26 6c 6b 29 3b 0a 20 20 20 20 70 46 k), &lk);. pF
12886 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 ile->sharedLockB
12887 79 74 65 20 3d 20 28 73 68 6f 72 74 29 28 28 6c yte = (short)((l
12888 6b 20 26 20 30 78 37 66 66 66 66 66 66 66 29 25 k & 0x7fffffff)%
12889 28 53 48 41 52 45 44 5f 53 49 5a 45 20 2d 20 31 (SHARED_SIZE - 1
1288a 29 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 77 69 ));. res = wi
1288b 6e 4c 6f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65 nLockFile(&pFile
1288c 2d 3e 68 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 4b ->h, SQLITE_LOCK
1288d 46 49 4c 45 5f 46 4c 41 47 53 2c 0a 20 20 20 20 FILE_FLAGS,.
1288e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1288f 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 70 SHARED_FIRST+p
12890 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b File->sharedLock
12891 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b 0a Byte, 0, 1, 0);.
12892 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 }.#endif. if(
12893 20 72 65 73 20 3d 3d 20 30 20 29 7b 0a 20 20 20 res == 0 ){.
12894 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
12895 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 72 o = osGetLastErr
12896 6f 72 28 29 3b 0a 20 20 20 20 2f 2a 20 4e 6f 20 or();. /* No
12897 6e 65 65 64 20 74 6f 20 6c 6f 67 20 61 20 66 61 need to log a fa
12898 69 6c 75 72 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f ilure to lock */
12899 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 . }. return re
1289a 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f s;.}../*.** Undo
1289b 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 a readlock.*/.s
1289c 74 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b tatic int unlock
1289d 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 ReadLock(winFile
1289e 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 *pFile){. int
1289f 72 65 73 3b 0a 20 20 44 57 4f 52 44 20 6c 61 73 res;. DWORD las
128a0 74 45 72 72 6e 6f 3b 0a 20 20 69 66 28 20 69 73 tErrno;. if( is
128a1 4e 54 28 29 20 29 7b 0a 20 20 20 20 72 65 73 20 NT() ){. res
128a2 3d 20 77 69 6e 55 6e 6c 6f 63 6b 46 69 6c 65 28 = winUnlockFile(
128a3 26 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 &pFile->h, SHARE
128a4 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 D_FIRST, 0, SHAR
128a5 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 7d ED_SIZE, 0);. }
128a6 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 57 .#ifdef SQLITE_W
128a7 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20 20 IN32_HAS_ANSI.
128a8 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20 3d 20 else{. res =
128a9 77 69 6e 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 70 winUnlockFile(&p
128aa 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f File->h, SHARED_
128ab 46 49 52 53 54 2b 70 46 69 6c 65 2d 3e 73 68 61 FIRST+pFile->sha
128ac 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 redLockByte, 0,
128ad 31 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 1, 0);. }.#endi
128ae 66 0a 20 20 69 66 28 20 72 65 73 3d 3d 30 20 26 f. if( res==0 &
128af 26 20 28 28 6c 61 73 74 45 72 72 6e 6f 20 3d 20 & ((lastErrno =
128b0 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 osGetLastError()
128b1 29 21 3d 45 52 52 4f 52 5f 4e 4f 54 5f 4c 4f 43 )!=ERROR_NOT_LOC
128b2 4b 45 44 29 20 29 7b 0a 20 20 20 20 70 46 69 6c KED) ){. pFil
128b3 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c e->lastErrno = l
128b4 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 77 69 astErrno;. wi
128b5 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 nLogError(SQLITE
128b6 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 2c 20 70 _IOERR_UNLOCK, p
128b7 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c File->lastErrno,
128b8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 22 75 . "u
128b9 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 22 2c 20 nlockReadLock",
128ba 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29 3b 0a 20 pFile->zPath);.
128bb 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b }. return res;
128bc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 .}../*.** Lock t
128bd 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 he file with the
128be 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 lock specified
128bf 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 by parameter loc
128c0 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f ktype - one.** o
128c1 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
128c2 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
128c3 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
128c4 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
128c5 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
128c6 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
128c7 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
128c8 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
128c9 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
128ca 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
128cb 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
128cc 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
128cd 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
128ce 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
128cf 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
128d0 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
128d1 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
128d2 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
128d3 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
128d4 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
128d5 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
128d6 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
128d7 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
128d8 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
128d9 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
128da 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
128db 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
128dc 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
128dd 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
128de 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
128df 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
128e0 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
128e1 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
128e2 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
128e3 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
128e4 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
128e5 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
128e6 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
128e7 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 USIVE.**.** This
128e8 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e routine will on
128e9 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f ly increase a lo
128ea 63 6b 2e 20 20 54 68 65 20 77 69 6e 55 6e 6c 6f ck. The winUnlo
128eb 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 ck() routine.**
128ec 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 erases all locks
128ed 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 at once and ret
128ee 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 urns us immediat
128ef 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c ely to locking l
128f0 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 evel 0..** It is
128f1 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
128f2 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 lower the locki
128f3 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 ng level one ste
128f4 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f p at a time. Yo
128f5 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 u.** must go str
128f6 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 aight to locking
128f7 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 level 0..*/.sta
128f8 74 69 63 20 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 tic int winLock(
128f9 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
128fa 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b , int locktype){
128fb 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
128fc 54 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 74 TE_OK; /* Ret
128fd 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 urn code from su
128fe 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 69 broutines */. i
128ff 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20 nt res = 1;
12900 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 /* Result
12901 6f 66 20 61 20 57 69 6e 64 6f 77 73 20 6c 6f 63 of a Windows loc
12902 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 k call */. int
12903 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20 newLocktype;
12904 20 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d /* Set pFile-
12905 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69 >locktype to thi
12906 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65 s value before e
12907 78 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 xiting */. int
12908 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d gotPendingLock =
12909 20 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65 0;/* True if we
1290a 20 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44 acquired a PEND
1290b 49 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 ING lock this ti
1290c 6d 65 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 me */. winFile
1290d 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c *pFile = (winFil
1290e 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 6c e*)id;. DWORD l
1290f 61 73 74 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 52 astErrno = NO_ER
12910 52 4f 52 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ROR;.. assert(
12911 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 id!=0 );. OSTRA
12912 43 45 28 28 22 4c 4f 43 4b 20 25 64 20 25 64 20 CE(("LOCK %d %d
12913 77 61 73 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 was %d(%d)\n",.
12914 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d pFile-
12915 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 >h, locktype, pF
12916 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 ile->locktype, p
12917 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b File->sharedLock
12918 42 79 74 65 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 Byte));.. /* If
12919 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 there is alread
1291a 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 y a lock of this
1291b 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 type or more re
1291c 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 strictive on the
1291d 0a 20 20 2a 2a 20 4f 73 46 69 6c 65 2c 20 64 6f . ** OsFile, do
1291e 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 nothing. Don't
1291f 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b use the end_lock
12920 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a : exit path, as.
12921 20 20 2a 2a 20 73 71 6c 69 74 65 33 4f 73 45 6e ** sqlite3OsEn
12922 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 terMutex() hasn'
12923 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 t been called ye
12924 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 t.. */. if( pF
12925 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c ile->locktype>=l
12926 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 ocktype ){. r
12927 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
12928 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 . }.. /* Make
12929 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 sure the locking
1292a 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 sequence is cor
1292b 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 rect. */. asse
1292c 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 rt( pFile->lockt
1292d 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 ype!=NO_LOCK ||
1292e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
1292f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 _LOCK );. asser
12930 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e t( locktype!=PEN
12931 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 DING_LOCK );. a
12932 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 ssert( locktype!
12933 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c =RESERVED_LOCK |
12934 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 | pFile->locktyp
12935 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
12936 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 ;.. /* Lock the
12937 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 PENDING_LOCK by
12938 74 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f te if we need to
12939 20 61 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 acquire a PENDI
1293a 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 NG lock or. **
1293b 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20 a SHARED lock.
1293c 49 66 20 77 65 20 61 72 65 20 61 63 71 75 69 72 If we are acquir
1293d 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 ing a SHARED loc
1293e 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69 74 69 k, the acquisiti
1293f 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 50 on of. ** the P
12940 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 ENDING_LOCK byte
12941 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a 20 is temporary..
12942 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70 */. newLocktyp
12943 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 e = pFile->lockt
12944 79 70 65 3b 0a 20 20 69 66 28 20 20 20 28 70 46 ype;. if( (pF
12945 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e ile->locktype==N
12946 4f 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 7c 7c 20 O_LOCK). ||
12947 28 20 20 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 ( (locktype==E
12948 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 0a 20 XCLUSIVE_LOCK).
12949 20 20 20 20 20 20 20 20 26 26 20 28 70 46 69 6c && (pFil
1294a 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 e->locktype==RES
1294b 45 52 56 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 ERVED_LOCK)). )
1294c 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 {. int cnt =
1294d 33 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 6e 3;. while( cn
1294e 74 2d 2d 3e 30 20 26 26 20 28 72 65 73 20 3d 20 t-->0 && (res =
1294f 77 69 6e 4c 6f 63 6b 46 69 6c 65 28 26 70 46 69 winLockFile(&pFi
12950 6c 65 2d 3e 68 2c 20 53 51 4c 49 54 45 5f 4c 4f le->h, SQLITE_LO
12951 43 4b 46 49 4c 45 5f 46 4c 41 47 53 2c 0a 20 20 CKFILE_FLAGS,.
12952 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12953 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12954 20 20 20 20 20 20 20 50 45 4e 44 49 4e 47 5f 42 PENDING_B
12955 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29 3d 3d YTE, 0, 1, 0))==
12956 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 72 0 ){. /* Tr
12957 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 65 74 y 3 times to get
12958 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 the pending loc
12959 6b 2e 20 20 54 68 69 73 20 69 73 20 6e 65 65 64 k. This is need
1295a 65 64 20 74 6f 20 77 6f 72 6b 0a 20 20 20 20 20 ed to work.
1295b 20 2a 2a 20 61 72 6f 75 6e 64 20 70 72 6f 62 6c ** around probl
1295c 65 6d 73 20 63 61 75 73 65 64 20 62 79 20 69 6e ems caused by in
1295d 64 65 78 69 6e 67 20 61 6e 64 2f 6f 72 20 61 6e dexing and/or an
1295e 74 69 2d 76 69 72 75 73 20 73 6f 66 74 77 61 72 ti-virus softwar
1295f 65 20 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 57 69 e on. ** Wi
12960 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 20 ndows systems..
12961 20 20 20 20 20 2a 2a 20 49 66 20 79 6f 75 20 61 ** If you a
12962 72 65 20 75 73 69 6e 67 20 74 68 69 73 20 63 6f re using this co
12963 64 65 20 61 73 20 61 20 6d 6f 64 65 6c 20 66 6f de as a model fo
12964 72 20 61 6c 74 65 72 6e 61 74 69 76 65 20 56 46 r alternative VF
12965 53 65 73 2c 20 64 6f 20 6e 6f 74 0a 20 20 20 20 Ses, do not.
12966 20 20 2a 2a 20 63 6f 70 79 20 74 68 69 73 20 72 ** copy this r
12967 65 74 72 79 20 6c 6f 67 69 63 2e 20 20 49 74 20 etry logic. It
12968 69 73 20 61 20 68 61 63 6b 20 69 6e 74 65 6e 64 is a hack intend
12969 65 64 20 66 6f 72 20 57 69 6e 64 6f 77 73 20 6f ed for Windows o
1296a 6e 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 nly.. */.
1296b 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 63 6f OSTRACE(("co
1296c 75 6c 64 20 6e 6f 74 20 67 65 74 20 61 20 50 45 uld not get a PE
1296d 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 63 6e 74 3d NDING lock. cnt=
1296e 25 64 5c 6e 22 2c 20 63 6e 74 29 29 3b 0a 20 20 %d\n", cnt));.
1296f 20 20 20 20 69 66 28 20 63 6e 74 20 29 20 73 71 if( cnt ) sq
12970 6c 69 74 65 33 5f 77 69 6e 33 32 5f 73 6c 65 65 lite3_win32_slee
12971 70 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 p(1);. }.
12972 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d gotPendingLock =
12973 20 72 65 73 3b 0a 20 20 20 20 69 66 28 20 21 72 res;. if( !r
12974 65 73 20 29 7b 0a 20 20 20 20 20 20 6c 61 73 74 es ){. last
12975 45 72 72 6e 6f 20 3d 20 6f 73 47 65 74 4c 61 73 Errno = osGetLas
12976 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 7d 0a tError();. }.
12977 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 }.. /* Acquir
12978 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a e a shared lock.
12979 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 */. if( lockt
1297a 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
1297b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 61 && res ){. a
1297c 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f ssert( pFile->lo
1297d 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 cktype==NO_LOCK
1297e 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 67 65 74 );. res = get
1297f 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b ReadLock(pFile);
12980 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a . if( res ){.
12981 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 newLocktyp
12982 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b e = SHARED_LOCK;
12983 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
12984 20 20 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73 lastErrno = os
12985 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a GetLastError();.
12986 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
12987 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 Acquire a RESERV
12988 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 ED lock. */. i
12989 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 f( locktype==RES
1298a 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 ERVED_LOCK && re
1298b 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 s ){. assert(
1298c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
1298d 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b ==SHARED_LOCK );
1298e 0a 20 20 20 20 72 65 73 20 3d 20 77 69 6e 4c 6f . res = winLo
1298f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65 2d 3e 68 ckFile(&pFile->h
12990 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 46 49 4c , SQLITE_LOCKFIL
12991 45 5f 46 4c 41 47 53 2c 20 52 45 53 45 52 56 45 E_FLAGS, RESERVE
12992 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 D_BYTE, 0, 1, 0)
12993 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b ;. if( res ){
12994 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 . newLockty
12995 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f pe = RESERVED_LO
12996 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 CK;. }else{.
12997 20 20 20 20 20 6c 61 73 74 45 72 72 6e 6f 20 3d lastErrno =
12998 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 osGetLastError(
12999 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
1299a 2f 2a 20 41 63 71 75 69 72 65 20 61 20 50 45 4e /* Acquire a PEN
1299b 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 DING lock. */.
1299c 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 if( locktype==E
1299d 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 XCLUSIVE_LOCK &&
1299e 20 72 65 73 20 29 7b 0a 20 20 20 20 6e 65 77 4c res ){. newL
1299f 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e ocktype = PENDIN
129a0 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 G_LOCK;. gotP
129a1 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a endingLock = 0;.
129a2 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 }.. /* Acquir
129a3 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c e an EXCLUSIVE l
129a4 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c ock. */. if( l
129a5 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
129a6 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 VE_LOCK && res )
129a7 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 {. assert( pF
129a8 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 ile->locktype>=S
129a9 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
129aa 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 res = unlockRe
129ab 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 adLock(pFile);.
129ac 20 20 20 4f 53 54 52 41 43 45 28 28 22 75 6e 72 OSTRACE(("unr
129ad 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c eadlock = %d\n",
129ae 20 72 65 73 29 29 3b 0a 20 20 20 20 72 65 73 20 res));. res
129af 3d 20 77 69 6e 4c 6f 63 6b 46 69 6c 65 28 26 70 = winLockFile(&p
129b0 46 69 6c 65 2d 3e 68 2c 20 53 51 4c 49 54 45 5f File->h, SQLITE_
129b1 4c 4f 43 4b 46 49 4c 45 5f 46 4c 41 47 53 2c 20 LOCKFILE_FLAGS,
129b2 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c SHARED_FIRST, 0,
129b3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
129b4 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 53 49 SHARED_SI
129b5 5a 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 ZE, 0);. if(
129b6 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 res ){. new
129b7 4c 6f 63 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 Locktype = EXCLU
129b8 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d SIVE_LOCK;. }
129b9 65 6c 73 65 7b 0a 20 20 20 20 20 20 6c 61 73 74 else{. last
129ba 45 72 72 6e 6f 20 3d 20 6f 73 47 65 74 4c 61 73 Errno = osGetLas
129bb 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 tError();.
129bc 4f 53 54 52 41 43 45 28 28 22 65 72 72 6f 72 2d OSTRACE(("error-
129bd 63 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20 6c 61 code = %d\n", la
129be 73 74 45 72 72 6e 6f 29 29 3b 0a 20 20 20 20 20 stErrno));.
129bf 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 getReadLock(pFi
129c0 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a le);. }. }..
129c1 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68 /* If we are h
129c2 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 olding a PENDING
129c3 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74 lock that ought
129c4 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c to be released,
129c5 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 then. ** relea
129c6 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a se it now.. */.
129c7 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 if( gotPending
129c8 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 Lock && locktype
129c9 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b ==SHARED_LOCK ){
129ca 0a 20 20 20 20 77 69 6e 55 6e 6c 6f 63 6b 46 69 . winUnlockFi
129cb 6c 65 28 26 70 46 69 6c 65 2d 3e 68 2c 20 50 45 le(&pFile->h, PE
129cc 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 NDING_BYTE, 0, 1
129cd 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 , 0);. }.. /*
129ce 55 70 64 61 74 65 20 74 68 65 20 73 74 61 74 65 Update the state
129cf 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 of the lock has
129d0 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66 69 6c held in the fil
129d1 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 65 e descriptor the
129d2 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 n. ** return th
129d3 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 e appropriate re
129d4 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a sult code.. */.
129d5 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
129d6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
129d7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53 . }else{. OS
129d8 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 46 41 49 TRACE(("LOCK FAI
129d9 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 66 6f LED %d trying fo
129da 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 64 5c r %d but got %d\
129db 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 n", pFile->h,.
129dc 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 locktyp
129dd 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65 29 29 e, newLocktype))
129de 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 ;. pFile->las
129df 74 45 72 72 6e 6f 20 3d 20 6c 61 73 74 45 72 72 tErrno = lastErr
129e0 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c no;. rc = SQL
129e1 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 ITE_BUSY;. }.
129e2 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
129e3 3d 20 28 75 38 29 6e 65 77 4c 6f 63 6b 74 79 70 = (u8)newLocktyp
129e4 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a e;. return rc;.
129e5 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
129e6 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 utine checks if
129e7 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 there is a RESER
129e8 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e VED lock held on
129e9 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a the specified.*
129ea 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f * file by this o
129eb 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 r any other proc
129ec 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c ess. If such a l
129ed 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74 ock is held, ret
129ee 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c urn.** non-zero,
129ef 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e otherwise zero.
129f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
129f1 69 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c inCheckReservedL
129f2 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
129f3 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f *id, int *pResO
129f4 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ut){. int rc;.
129f5 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
129f6 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a = (winFile*)id;.
129f7 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
129f8 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
129f9 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 E_IOERR_CHECKRES
129fa 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 0a 20 ERVEDLOCK; );..
129fb 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
129fc 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c ;. if( pFile->l
129fd 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 ocktype>=RESERVE
129fe 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 63 D_LOCK ){. rc
129ff 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52 41 43 = 1;. OSTRAC
12a00 45 28 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b E(("TEST WR-LOCK
12a01 20 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e %d %d (local)\n
12a02 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 ", pFile->h, rc)
12a03 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
12a04 72 63 20 3d 20 77 69 6e 4c 6f 63 6b 46 69 6c 65 rc = winLockFile
12a05 28 26 70 46 69 6c 65 2d 3e 68 2c 20 53 51 4c 49 (&pFile->h, SQLI
12a06 54 45 5f 4c 4f 43 4b 46 49 4c 45 5f 46 4c 41 47 TE_LOCKFILE_FLAG
12a07 53 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 S, RESERVED_BYTE
12a08 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 , 0, 1, 0);.
12a09 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
12a0a 77 69 6e 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 70 winUnlockFile(&p
12a0b 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 File->h, RESERVE
12a0c 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 D_BYTE, 0, 1, 0)
12a0d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
12a0e 20 21 72 63 3b 0a 20 20 20 20 4f 53 54 52 41 43 !rc;. OSTRAC
12a0f 45 28 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b E(("TEST WR-LOCK
12a10 20 25 64 20 25 64 20 28 72 65 6d 6f 74 65 29 5c %d %d (remote)\
12a11 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 n", pFile->h, rc
12a12 29 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f ));. }. *pResO
12a13 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 ut = rc;. retur
12a14 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
12a15 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 /*.** Lower the
12a16 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e locking level on
12a17 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
12a18 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e id to locktype.
12a19 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 locktype.** mu
12a1a 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f st be either NO_
12a1b 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c LOCK or SHARED_L
12a1c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 OCK..**.** If th
12a1d 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
12a1e 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 of the file desc
12a1f 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 riptor is alread
12a20 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a y at or below.**
12a21 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
12a22 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 ocking level, th
12a23 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
12a24 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 no-op..**.** It
12a25 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
12a26 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 for this routine
12a27 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65 20 to fail if the
12a28 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a second argument.
12a29 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 ** is NO_LOCK.
12a2a 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 If the second ar
12a2b 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 44 gument is SHARED
12a2c 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 20 _LOCK then this
12a2d 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 routine.** might
12a2e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
12a2f 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 OERR;.*/.static
12a30 69 6e 74 20 77 69 6e 55 6e 6c 6f 63 6b 28 73 71 int winUnlock(sq
12a31 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
12a32 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 int locktype){.
12a33 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 77 69 6e int type;. win
12a34 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 File *pFile = (w
12a35 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e inFile*)id;. in
12a36 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
12a37 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c ;. assert( pFil
12a38 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 e!=0 );. assert
12a39 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 ( locktype<=SHAR
12a3a 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 ED_LOCK );. OST
12a3b 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 25 64 RACE(("UNLOCK %d
12a3c 20 74 6f 20 25 64 20 77 61 73 20 25 64 28 25 64 to %d was %d(%d
12a3d 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 )\n", pFile->h,
12a3e 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 locktype,.
12a3f 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
12a40 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 ype, pFile->shar
12a41 65 64 4c 6f 63 6b 42 79 74 65 29 29 3b 0a 20 20 edLockByte));.
12a42 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f type = pFile->lo
12a43 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79 cktype;. if( ty
12a44 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe>=EXCLUSIVE_LO
12a45 43 4b 20 29 7b 0a 20 20 20 20 77 69 6e 55 6e 6c CK ){. winUnl
12a46 6f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65 2d 3e ockFile(&pFile->
12a47 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c h, SHARED_FIRST,
12a48 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 0, SHARED_SIZE,
12a49 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 0);. if( loc
12a4a 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
12a4b 43 4b 20 26 26 20 21 67 65 74 52 65 61 64 4c 6f CK && !getReadLo
12a4c 63 6b 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 ck(pFile) ){.
12a4d 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c /* This shoul
12a4e 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e 2e 20 d never happen.
12a4f 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 We should alway
12a50 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20 20 20 s be able to.
12a51 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72 65 20 ** reacquire
12a52 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f the read lock */
12a53 0a 20 20 20 20 20 20 72 63 20 3d 20 77 69 6e 4c . rc = winL
12a54 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 49 ogError(SQLITE_I
12a55 4f 45 52 52 5f 55 4e 4c 4f 43 4b 2c 20 6f 73 47 OERR_UNLOCK, osG
12a56 65 74 4c 61 73 74 45 72 72 6f 72 28 29 2c 0a 20 etLastError(),.
12a57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 77 "w
12a58 69 6e 55 6e 6c 6f 63 6b 22 2c 20 70 46 69 6c 65 inUnlock", pFile
12a59 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 20 20 7d 0a ->zPath);. }.
12a5a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d }. if( type>=
12a5b 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b RESERVED_LOCK ){
12a5c 0a 20 20 20 20 77 69 6e 55 6e 6c 6f 63 6b 46 69 . winUnlockFi
12a5d 6c 65 28 26 70 46 69 6c 65 2d 3e 68 2c 20 52 45 le(&pFile->h, RE
12a5e 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 SERVED_BYTE, 0,
12a5f 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 1, 0);. }. if(
12a60 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f locktype==NO_LO
12a61 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 CK && type>=SHAR
12a62 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75 ED_LOCK ){. u
12a63 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 nlockReadLock(pF
12a64 69 6c 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ile);. }. if(
12a65 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f type>=PENDING_LO
12a66 43 4b 20 29 7b 0a 20 20 20 20 77 69 6e 55 6e 6c CK ){. winUnl
12a67 6f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65 2d 3e ockFile(&pFile->
12a68 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c h, PENDING_BYTE,
12a69 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 0, 1, 0);. }.
12a6a 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
12a6b 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65 3b = (u8)locktype;
12a6c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
12a6d 0a 2f 2a 0a 2a 2a 20 49 66 20 2a 70 41 72 67 20 ./*.** If *pArg
12a6e 69 73 20 69 6e 69 74 69 74 69 61 6c 6c 79 20 6e is inititially n
12a6f 65 67 61 74 69 76 65 20 74 68 65 6e 20 74 68 69 egative then thi
12a70 73 20 69 73 20 61 20 71 75 65 72 79 2e 20 20 53 s is a query. S
12a71 65 74 20 2a 70 41 72 67 20 74 6f 0a 2a 2a 20 31 et *pArg to.** 1
12a72 20 6f 72 20 30 20 64 65 70 65 6e 64 69 6e 67 20 or 0 depending
12a73 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f on whether or no
12a74 74 20 62 69 74 20 6d 61 73 6b 20 6f 66 20 70 46 t bit mask of pF
12a75 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20 69 ile->ctrlFlags i
12a76 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 s set..**.** If
12a77 2a 70 41 72 67 20 69 73 20 30 20 6f 72 20 31 2c *pArg is 0 or 1,
12a78 20 74 68 65 6e 20 63 6c 65 61 72 20 6f 72 20 73 then clear or s
12a79 65 74 20 74 68 65 20 6d 61 73 6b 20 62 69 74 20 et the mask bit
12a7a 6f 66 20 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c of pFile->ctrlFl
12a7b 61 67 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ags..*/.static v
12a7c 6f 69 64 20 77 69 6e 4d 6f 64 65 42 69 74 28 77 oid winModeBit(w
12a7d 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 75 inFile *pFile, u
12a7e 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6d 61 73 nsigned char mas
12a7f 6b 2c 20 69 6e 74 20 2a 70 41 72 67 29 7b 0a 20 k, int *pArg){.
12a80 20 69 66 28 20 2a 70 41 72 67 3c 30 20 29 7b 0a if( *pArg<0 ){.
12a81 20 20 20 20 2a 70 41 72 67 20 3d 20 28 70 46 69 *pArg = (pFi
12a82 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20 26 20 le->ctrlFlags &
12a83 6d 61 73 6b 29 21 3d 30 3b 0a 20 20 7d 65 6c 73 mask)!=0;. }els
12a84 65 20 69 66 28 20 28 2a 70 41 72 67 29 3d 3d 30 e if( (*pArg)==0
12a85 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 63 ){. pFile->c
12a86 74 72 6c 46 6c 61 67 73 20 26 3d 20 7e 6d 61 73 trlFlags &= ~mas
12a87 6b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 k;. }else{.
12a88 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 pFile->ctrlFlags
12a89 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 7d 0a 7d 0a |= mask;. }.}.
12a8a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61 ./*.** Control a
12a8b 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 nd query of the
12a8c 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 open file handle
12a8d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
12a8e 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 winFileControl(s
12a8f 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
12a90 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 int op, void *p
12a91 41 72 67 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 Arg){. winFile
12a92 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c *pFile = (winFil
12a93 65 2a 29 69 64 3b 0a 20 20 73 77 69 74 63 68 28 e*)id;. switch(
12a94 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 op ){. case
12a95 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 SQLITE_FCNTL_LOC
12a96 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 KSTATE: {.
12a97 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 70 46 *(int*)pArg = pF
12a98 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 ile->locktype;.
12a99 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
12a9a 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 TE_OK;. }.
12a9b 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53 case SQLITE_LAS
12a9c 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 T_ERRNO: {.
12a9d 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 *(int*)pArg = (
12a9e 69 6e 74 29 70 46 69 6c 65 2d 3e 6c 61 73 74 45 int)pFile->lastE
12a9f 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 rrno;. retu
12aa0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
12aa1 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c }. case SQL
12aa2 49 54 45 5f 46 43 4e 54 4c 5f 43 48 55 4e 4b 5f ITE_FCNTL_CHUNK_
12aa3 53 49 5a 45 3a 20 7b 0a 20 20 20 20 20 20 70 46 SIZE: {. pF
12aa4 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 20 3d 20 2a ile->szChunk = *
12aa5 28 69 6e 74 20 2a 29 70 41 72 67 3b 0a 20 20 20 (int *)pArg;.
12aa6 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
12aa7 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 _OK;. }. c
12aa8 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ase SQLITE_FCNTL
12aa9 5f 53 49 5a 45 5f 48 49 4e 54 3a 20 7b 0a 20 20 _SIZE_HINT: {.
12aaa 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 73 if( pFile->s
12aab 7a 43 68 75 6e 6b 3e 30 20 29 7b 0a 20 20 20 20 zChunk>0 ){.
12aac 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 sqlite3_int6
12aad 34 20 6f 6c 64 53 7a 3b 0a 20 20 20 20 20 20 20 4 oldSz;.
12aae 20 69 6e 74 20 72 63 20 3d 20 77 69 6e 46 69 6c int rc = winFil
12aaf 65 53 69 7a 65 28 69 64 2c 20 26 6f 6c 64 53 7a eSize(id, &oldSz
12ab0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
12ab1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
12ab2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
12ab3 33 5f 69 6e 74 36 34 20 6e 65 77 53 7a 20 3d 20 3_int64 newSz =
12ab4 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a *(sqlite3_int64*
12ab5 29 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 20 )pArg;.
12ab6 20 69 66 28 20 6e 65 77 53 7a 3e 6f 6c 64 53 7a if( newSz>oldSz
12ab7 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
12ab8 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 SimulateIOErrorB
12ab9 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 20 20 enign(1);.
12aba 20 20 20 20 20 20 72 63 20 3d 20 77 69 6e 54 72 rc = winTr
12abb 75 6e 63 61 74 65 28 69 64 2c 20 6e 65 77 53 7a uncate(id, newSz
12abc 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 53 );. S
12abd 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 imulateIOErrorBe
12abe 6e 69 67 6e 28 30 29 3b 0a 20 20 20 20 20 20 20 nign(0);.
12abf 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
12ac0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
12ac1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
12ac2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
12ac3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
12ac4 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 45 SQLITE_FCNTL_PE
12ac5 52 53 49 53 54 5f 57 41 4c 3a 20 7b 0a 20 20 20 RSIST_WAL: {.
12ac6 20 20 20 77 69 6e 4d 6f 64 65 42 69 74 28 70 46 winModeBit(pF
12ac7 69 6c 65 2c 20 57 49 4e 46 49 4c 45 5f 50 45 52 ile, WINFILE_PER
12ac8 53 49 53 54 5f 57 41 4c 2c 20 28 69 6e 74 2a 29 SIST_WAL, (int*)
12ac9 70 41 72 67 29 3b 0a 20 20 20 20 20 20 72 65 74 pArg);. ret
12aca 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
12acb 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 }. case SQ
12acc 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 4f 57 45 52 LITE_FCNTL_POWER
12acd 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 3a 20 SAFE_OVERWRITE:
12ace 7b 0a 20 20 20 20 20 20 77 69 6e 4d 6f 64 65 42 {. winModeB
12acf 69 74 28 70 46 69 6c 65 2c 20 57 49 4e 46 49 4c it(pFile, WINFIL
12ad0 45 5f 50 53 4f 57 2c 20 28 69 6e 74 2a 29 70 41 E_PSOW, (int*)pA
12ad1 72 67 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 rg);. retur
12ad2 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
12ad3 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 }. case SQLI
12ad4 54 45 5f 46 43 4e 54 4c 5f 56 46 53 4e 41 4d 45 TE_FCNTL_VFSNAME
12ad5 3a 20 7b 0a 20 20 20 20 20 20 2a 28 63 68 61 72 : {. *(char
12ad6 2a 2a 29 70 41 72 67 20 3d 20 73 71 6c 69 74 65 **)pArg = sqlite
12ad7 33 5f 6d 70 72 69 6e 74 66 28 22 77 69 6e 33 32 3_mprintf("win32
12ad8 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ");. return
12ad9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
12ada 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 }. case SQLIT
12adb 45 5f 46 43 4e 54 4c 5f 57 49 4e 33 32 5f 41 56 E_FCNTL_WIN32_AV
12adc 5f 52 45 54 52 59 3a 20 7b 0a 20 20 20 20 20 20 _RETRY: {.
12add 69 6e 74 20 2a 61 20 3d 20 28 69 6e 74 2a 29 70 int *a = (int*)p
12ade 41 72 67 3b 0a 20 20 20 20 20 20 69 66 28 20 61 Arg;. if( a
12adf 5b 30 5d 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 [0]>0 ){.
12ae0 20 77 69 6e 33 32 49 6f 65 72 72 52 65 74 72 79 win32IoerrRetry
12ae1 20 3d 20 61 5b 30 5d 3b 0a 20 20 20 20 20 20 7d = a[0];. }
12ae2 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 5b else{. a[
12ae3 30 5d 20 3d 20 77 69 6e 33 32 49 6f 65 72 72 52 0] = win32IoerrR
12ae4 65 74 72 79 3b 0a 20 20 20 20 20 20 7d 0a 20 20 etry;. }.
12ae5 20 20 20 20 69 66 28 20 61 5b 31 5d 3e 30 20 29 if( a[1]>0 )
12ae6 7b 0a 20 20 20 20 20 20 20 20 77 69 6e 33 32 49 {. win32I
12ae7 6f 65 72 72 52 65 74 72 79 44 65 6c 61 79 20 3d oerrRetryDelay =
12ae8 20 61 5b 31 5d 3b 0a 20 20 20 20 20 20 7d 65 6c a[1];. }el
12ae9 73 65 7b 0a 20 20 20 20 20 20 20 20 61 5b 31 5d se{. a[1]
12aea 20 3d 20 77 69 6e 33 32 49 6f 65 72 72 52 65 74 = win32IoerrRet
12aeb 72 79 44 65 6c 61 79 3b 0a 20 20 20 20 20 20 7d ryDelay;. }
12aec 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
12aed 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 LITE_OK;. }.
12aee 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
12aef 54 45 5f 4e 4f 54 46 4f 55 4e 44 3b 0a 7d 0a 0a TE_NOTFOUND;.}..
12af0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
12af1 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 sector size in
12af2 62 79 74 65 73 20 6f 66 20 74 68 65 20 75 6e 64 bytes of the und
12af3 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 erlying block de
12af4 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 vice for.** the
12af5 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 2e 20 specified file.
12af6 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 This is almost a
12af7 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 73 2c lways 512 bytes,
12af8 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c but may be.** l
12af9 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 arger for some d
12afa 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 evices..**.** SQ
12afb 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75 6d 65 Lite code assume
12afc 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 s this function
12afd 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 cannot fail. It
12afe 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 68 61 also assumes tha
12aff 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 t.** if two file
12b00 73 20 61 72 65 20 63 72 65 61 74 65 64 20 69 6e s are created in
12b01 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 the same file-s
12b02 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72 79 20 ystem directory
12b03 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 (i.e..** a datab
12b04 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 ase and its jour
12b05 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74 20 74 nal file) that t
12b06 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 he sector size w
12b07 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 ill be the.** sa
12b08 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a me for both..*/.
12b09 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 65 static int winSe
12b0a 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 ctorSize(sqlite3
12b0b 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 28 76 _file *id){. (v
12b0c 6f 69 64 29 69 64 3b 0a 20 20 72 65 74 75 72 6e oid)id;. return
12b0d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
12b0e 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a 0a SECTOR_SIZE;.}..
12b0f 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 76 /*.** Return a v
12b10 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63 65 20 ector of device
12b11 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 2e characteristics.
12b12 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
12b13 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 65 inDeviceCharacte
12b14 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f ristics(sqlite3_
12b15 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 77 69 6e file *id){. win
12b16 46 69 6c 65 20 2a 70 20 3d 20 28 77 69 6e 46 69 File *p = (winFi
12b17 6c 65 2a 29 69 64 3b 0a 20 20 72 65 74 75 72 6e le*)id;. return
12b18 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 55 4e SQLITE_IOCAP_UN
12b19 44 45 4c 45 54 41 42 4c 45 5f 57 48 45 4e 5f 4f DELETABLE_WHEN_O
12b1a 50 45 4e 20 7c 0a 20 20 20 20 20 20 20 20 20 28 PEN |. (
12b1b 28 70 2d 3e 63 74 72 6c 46 6c 61 67 73 20 26 20 (p->ctrlFlags &
12b1c 57 49 4e 46 49 4c 45 5f 50 53 4f 57 29 3f 53 51 WINFILE_PSOW)?SQ
12b1d 4c 49 54 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52 LITE_IOCAP_POWER
12b1e 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 3a 30 SAFE_OVERWRITE:0
12b1f 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 );.}..#ifndef SQ
12b20 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 0a 2f LITE_OMIT_WAL../
12b21 2a 20 0a 2a 2a 20 57 69 6e 64 6f 77 73 20 77 69 * .** Windows wi
12b22 6c 6c 20 6f 6e 6c 79 20 6c 65 74 20 79 6f 75 20 ll only let you
12b23 63 72 65 61 74 65 20 66 69 6c 65 20 76 69 65 77 create file view
12b24 20 6d 61 70 70 69 6e 67 73 0a 2a 2a 20 6f 6e 20 mappings.** on
12b25 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 allocation size
12b26 67 72 61 6e 75 6c 61 72 69 74 79 20 62 6f 75 6e granularity boun
12b27 64 61 72 69 65 73 2e 0a 2a 2a 20 44 75 72 69 6e daries..** Durin
12b28 67 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 g sqlite3_os_ini
12b29 74 28 29 20 77 65 20 64 6f 20 61 20 47 65 74 53 t() we do a GetS
12b2a 79 73 74 65 6d 49 6e 66 6f 28 29 0a 2a 2a 20 74 ystemInfo().** t
12b2b 6f 20 67 65 74 20 74 68 65 20 67 72 61 6e 75 6c o get the granul
12b2c 61 72 69 74 79 20 73 69 7a 65 2e 0a 2a 2f 0a 53 arity size..*/.S
12b2d 59 53 54 45 4d 5f 49 4e 46 4f 20 77 69 6e 53 79 YSTEM_INFO winSy
12b2e 73 49 6e 66 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 48 65 sInfo;../*.** He
12b2f 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 lper functions t
12b30 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c o obtain and rel
12b31 69 6e 71 75 69 73 68 20 74 68 65 20 67 6c 6f 62 inquish the glob
12b32 61 6c 20 6d 75 74 65 78 2e 20 54 68 65 0a 2a 2a al mutex. The.**
12b33 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 69 73 global mutex is
12b34 20 75 73 65 64 20 74 6f 20 70 72 6f 74 65 63 74 used to protect
12b35 20 74 68 65 20 77 69 6e 4c 6f 63 6b 49 6e 66 6f the winLockInfo
12b36 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20 62 79 objects used by
12b37 20 0a 2a 2a 20 74 68 69 73 20 66 69 6c 65 2c 20 .** this file,
12b38 61 6c 6c 20 6f 66 20 77 68 69 63 68 20 6d 61 79 all of which may
12b39 20 62 65 20 73 68 61 72 65 64 20 62 79 20 6d 75 be shared by mu
12b3a 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73 2e 0a ltiple threads..
12b3b 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 77 **.** Function w
12b3c 69 6e 53 68 6d 4d 75 74 65 78 48 65 6c 64 28 29 inShmMutexHeld()
12b3d 20 69 73 20 75 73 65 64 20 74 6f 20 61 73 73 65 is used to asse
12b3e 72 74 28 29 20 74 68 61 74 20 74 68 65 20 67 6c rt() that the gl
12b3f 6f 62 61 6c 20 6d 75 74 65 78 20 0a 2a 2a 20 69 obal mutex .** i
12b40 73 20 68 65 6c 64 20 77 68 65 6e 20 72 65 71 75 s held when requ
12b41 69 72 65 64 2e 20 54 68 69 73 20 66 75 6e 63 74 ired. This funct
12b42 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 ion is only used
12b43 20 61 73 20 70 61 72 74 20 6f 66 20 61 73 73 65 as part of asse
12b44 72 74 28 29 20 0a 2a 2a 20 73 74 61 74 65 6d 65 rt() .** stateme
12b45 6e 74 73 2e 20 65 2e 67 2e 0a 2a 2a 0a 2a 2a 20 nts. e.g..**.**
12b46 20 20 77 69 6e 53 68 6d 45 6e 74 65 72 4d 75 74 winShmEnterMut
12b47 65 78 28 29 0a 2a 2a 20 20 20 20 20 61 73 73 65 ex().** asse
12b48 72 74 28 20 77 69 6e 53 68 6d 4d 75 74 65 78 48 rt( winShmMutexH
12b49 65 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 20 77 69 eld() );.** wi
12b4a 6e 53 68 6d 4c 65 61 76 65 4d 75 74 65 78 28 29 nShmLeaveMutex()
12b4b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
12b4c 77 69 6e 53 68 6d 45 6e 74 65 72 4d 75 74 65 78 winShmEnterMutex
12b4d 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 (void){. sqlite
12b4e 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 3_mutex_enter(sq
12b4f 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
12b50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
12b51 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a TIC_MASTER));.}.
12b52 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 53 static void winS
12b53 68 6d 4c 65 61 76 65 4d 75 74 65 78 28 76 6f 69 hmLeaveMutex(voi
12b54 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 d){. sqlite3_mu
12b55 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 tex_leave(sqlite
12b56 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
12b57 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
12b58 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 23 69 66 64 MASTER));.}.#ifd
12b59 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
12b5a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 68 static int winSh
12b5b 6d 4d 75 74 65 78 48 65 6c 64 28 76 6f 69 64 29 mMutexHeld(void)
12b5c 20 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 {. return sqli
12b5d 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 te3_mutex_held(s
12b5e 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
12b5f 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
12b60 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d ATIC_MASTER));.}
12b61 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f .#endif../*.** O
12b62 62 6a 65 63 74 20 75 73 65 64 20 74 6f 20 72 65 bject used to re
12b63 70 72 65 73 65 6e 74 20 61 20 73 69 6e 67 6c 65 present a single
12b64 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 61 6e 64 file opened and
12b65 20 6d 6d 61 70 70 65 64 20 74 6f 20 70 72 6f 76 mmapped to prov
12b66 69 64 65 0a 2a 2a 20 73 68 61 72 65 64 20 6d 65 ide.** shared me
12b67 6d 6f 72 79 2e 20 20 57 68 65 6e 20 6d 75 6c 74 mory. When mult
12b68 69 70 6c 65 20 74 68 72 65 61 64 73 20 61 6c 6c iple threads all
12b69 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 73 reference the s
12b6a 61 6d 65 0a 2a 2a 20 6c 6f 67 2d 73 75 6d 6d 61 ame.** log-summa
12b6b 72 79 2c 20 65 61 63 68 20 74 68 72 65 61 64 20 ry, each thread
12b6c 68 61 73 20 69 74 73 20 6f 77 6e 20 77 69 6e 46 has its own winF
12b6d 69 6c 65 20 6f 62 6a 65 63 74 2c 20 62 75 74 20 ile object, but
12b6e 74 68 65 79 20 61 6c 6c 0a 2a 2a 20 70 6f 69 6e they all.** poin
12b6f 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 69 6e t to a single in
12b70 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f stance of this o
12b71 62 6a 65 63 74 2e 20 20 49 6e 20 6f 74 68 65 72 bject. In other
12b72 20 77 6f 72 64 73 2c 20 65 61 63 68 0a 2a 2a 20 words, each.**
12b73 6c 6f 67 2d 73 75 6d 6d 61 72 79 20 69 73 20 6f log-summary is o
12b74 70 65 6e 65 64 20 6f 6e 6c 79 20 6f 6e 63 65 20 pened only once
12b75 70 65 72 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a per process..**.
12b76 2a 2a 20 77 69 6e 53 68 6d 4d 75 74 65 78 48 65 ** winShmMutexHe
12b77 6c 64 28 29 20 6d 75 73 74 20 62 65 20 74 72 75 ld() must be tru
12b78 65 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 e when creating
12b79 6f 72 20 64 65 73 74 72 6f 79 69 6e 67 0a 2a 2a or destroying.**
12b7a 20 74 68 69 73 20 6f 62 6a 65 63 74 20 6f 72 20 this object or
12b7b 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 6f 72 while reading or
12b7c 20 77 72 69 74 69 6e 67 20 74 68 65 20 66 6f 6c writing the fol
12b7d 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 3a 0a 2a lowing fields:.*
12b7e 2a 0a 2a 2a 20 20 20 20 20 20 6e 52 65 66 0a 2a *.** nRef.*
12b7f 2a 20 20 20 20 20 20 70 4e 65 78 74 20 0a 2a 2a * pNext .**
12b80 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
12b81 67 20 66 69 65 6c 64 73 20 61 72 65 20 72 65 61 g fields are rea
12b82 64 2d 6f 6e 6c 79 20 61 66 74 65 72 20 74 68 65 d-only after the
12b83 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74 object is creat
12b84 65 64 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 20 ed:.** .**
12b85 66 69 64 0a 2a 2a 20 20 20 20 20 20 7a 46 69 6c fid.** zFil
12b86 65 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 45 69 74 68 ename.**.** Eith
12b87 65 72 20 77 69 6e 53 68 6d 4e 6f 64 65 2e 6d 75 er winShmNode.mu
12b88 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 tex must be held
12b89 20 6f 72 20 77 69 6e 53 68 6d 4e 6f 64 65 2e 6e or winShmNode.n
12b8a 52 65 66 3d 3d 30 20 61 6e 64 0a 2a 2a 20 77 69 Ref==0 and.** wi
12b8b 6e 53 68 6d 4d 75 74 65 78 48 65 6c 64 28 29 20 nShmMutexHeld()
12b8c 69 73 20 74 72 75 65 20 77 68 65 6e 20 72 65 61 is true when rea
12b8d 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 ding or writing
12b8e 61 6e 79 20 6f 74 68 65 72 20 66 69 65 6c 64 0a any other field.
12b8f 2a 2a 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 ** in this struc
12b90 74 75 72 65 2e 0a 2a 2a 0a 2a 2f 0a 73 74 72 75 ture..**.*/.stru
12b91 63 74 20 77 69 6e 53 68 6d 4e 6f 64 65 20 7b 0a ct winShmNode {.
12b92 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
12b93 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 *mutex; /*
12b94 4d 75 74 65 78 20 74 6f 20 61 63 63 65 73 73 20 Mutex to access
12b95 74 68 69 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 this object */.
12b96 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
12b97 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
12b98 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ame of the file
12b99 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 68 46 69 */. winFile hFi
12b9a 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 le;
12b9b 2f 2a 20 46 69 6c 65 20 68 61 6e 64 6c 65 20 66 /* File handle f
12b9c 72 6f 6d 20 77 69 6e 4f 70 65 6e 20 2a 2f 0a 0a rom winOpen */..
12b9d 20 20 69 6e 74 20 73 7a 52 65 67 69 6f 6e 3b 20 int szRegion;
12b9e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12b9f 53 69 7a 65 20 6f 66 20 73 68 61 72 65 64 2d 6d Size of shared-m
12ba0 65 6d 6f 72 79 20 72 65 67 69 6f 6e 73 20 2a 2f emory regions */
12ba1 0a 20 20 69 6e 74 20 6e 52 65 67 69 6f 6e 3b 20 . int nRegion;
12ba2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12ba3 20 53 69 7a 65 20 6f 66 20 61 72 72 61 79 20 61 Size of array a
12ba4 70 52 65 67 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 pRegion */. str
12ba5 75 63 74 20 53 68 6d 52 65 67 69 6f 6e 20 7b 0a uct ShmRegion {.
12ba6 20 20 20 20 48 41 4e 44 4c 45 20 68 4d 61 70 3b HANDLE hMap;
12ba7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12ba8 46 69 6c 65 20 68 61 6e 64 6c 65 20 66 72 6f 6d File handle from
12ba9 20 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 CreateFileMappi
12baa 6e 67 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a ng */. void *
12bab 70 4d 61 70 3b 0a 20 20 7d 20 2a 61 52 65 67 69 pMap;. } *aRegi
12bac 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 6c 61 73 74 on;. DWORD last
12bad 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 Errno;
12bae 20 2f 2a 20 54 68 65 20 57 69 6e 64 6f 77 73 20 /* The Windows
12baf 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c errno from the l
12bb0 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f ast I/O error */
12bb1 0a 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 .. int nRef;
12bb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
12bb3 2a 20 4e 75 6d 62 65 72 20 6f 66 20 77 69 6e 53 * Number of winS
12bb4 68 6d 20 6f 62 6a 65 63 74 73 20 70 6f 69 6e 74 hm objects point
12bb5 69 6e 67 20 74 6f 20 74 68 69 73 20 2a 2f 0a 20 ing to this */.
12bb6 20 77 69 6e 53 68 6d 20 2a 70 46 69 72 73 74 3b winShm *pFirst;
12bb7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
12bb8 6c 6c 20 77 69 6e 53 68 6d 20 6f 62 6a 65 63 74 ll winShm object
12bb9 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 s pointing to th
12bba 69 73 20 2a 2f 0a 20 20 77 69 6e 53 68 6d 4e 6f is */. winShmNo
12bbb 64 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 de *pNext;
12bbc 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69 /* Next in li
12bbd 73 74 20 6f 66 20 61 6c 6c 20 77 69 6e 53 68 6d st of all winShm
12bbe 4e 6f 64 65 20 6f 62 6a 65 63 74 73 20 2a 2f 0a Node objects */.
12bbf 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
12bc0 42 55 47 0a 20 20 75 38 20 6e 65 78 74 53 68 6d BUG. u8 nextShm
12bc1 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Id;
12bc2 20 2f 2a 20 4e 65 78 74 20 61 76 61 69 6c 61 62 /* Next availab
12bc3 6c 65 20 77 69 6e 53 68 6d 2e 69 64 20 76 61 6c le winShm.id val
12bc4 75 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a ue */.#endif.};.
12bc5 0a 2f 2a 0a 2a 2a 20 41 20 67 6c 6f 62 61 6c 20 ./*.** A global
12bc6 61 72 72 61 79 20 6f 66 20 61 6c 6c 20 77 69 6e array of all win
12bc7 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74 73 2e ShmNode objects.
12bc8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 53 68 .**.** The winSh
12bc9 6d 4d 75 74 65 78 48 65 6c 64 28 29 20 6d 75 73 mMutexHeld() mus
12bca 74 20 62 65 20 74 72 75 65 20 77 68 69 6c 65 20 t be true while
12bcb 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 reading or writi
12bcc 6e 67 20 74 68 69 73 20 6c 69 73 74 2e 0a 2a 2f ng this list..*/
12bcd 0a 73 74 61 74 69 63 20 77 69 6e 53 68 6d 4e 6f .static winShmNo
12bce 64 65 20 2a 77 69 6e 53 68 6d 4e 6f 64 65 4c 69 de *winShmNodeLi
12bcf 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 53 st = 0;../*.** S
12bd0 74 72 75 63 74 75 72 65 20 75 73 65 64 20 69 6e tructure used in
12bd1 74 65 72 6e 61 6c 6c 79 20 62 79 20 74 68 69 73 ternally by this
12bd2 20 56 46 53 20 74 6f 20 72 65 63 6f 72 64 20 74 VFS to record t
12bd3 68 65 20 73 74 61 74 65 20 6f 66 20 61 6e 0a 2a he state of an.*
12bd4 2a 20 6f 70 65 6e 20 73 68 61 72 65 64 20 6d 65 * open shared me
12bd5 6d 6f 72 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e mory connection.
12bd6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f .**.** The follo
12bd7 77 69 6e 67 20 66 69 65 6c 64 73 20 61 72 65 20 wing fields are
12bd8 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65 6e initialized when
12bd9 20 74 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 this object is
12bda 63 72 65 61 74 65 64 20 61 6e 64 0a 2a 2a 20 61 created and.** a
12bdb 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 74 68 65 re read-only the
12bdc 72 65 61 66 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 reafter:.**.**
12bdd 20 20 77 69 6e 53 68 6d 2e 70 53 68 6d 4e 6f 64 winShm.pShmNod
12bde 65 0a 2a 2a 20 20 20 20 77 69 6e 53 68 6d 2e 69 e.** winShm.i
12bdf 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 d.**.** All othe
12be0 72 20 66 69 65 6c 64 73 20 61 72 65 20 72 65 61 r fields are rea
12be1 64 2f 77 72 69 74 65 2e 20 20 54 68 65 20 77 69 d/write. The wi
12be2 6e 53 68 6d 2e 70 53 68 6d 4e 6f 64 65 2d 3e 6d nShm.pShmNode->m
12be3 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c utex must be hel
12be4 64 0a 2a 2a 20 77 68 69 6c 65 20 61 63 63 65 73 d.** while acces
12be5 73 69 6e 67 20 61 6e 79 20 72 65 61 64 2f 77 72 sing any read/wr
12be6 69 74 65 20 66 69 65 6c 64 73 2e 0a 2a 2f 0a 73 ite fields..*/.s
12be7 74 72 75 63 74 20 77 69 6e 53 68 6d 20 7b 0a 20 truct winShm {.
12be8 20 77 69 6e 53 68 6d 4e 6f 64 65 20 2a 70 53 68 winShmNode *pSh
12be9 6d 4e 6f 64 65 3b 20 20 20 20 20 20 2f 2a 20 54 mNode; /* T
12bea 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 77 69 he underlying wi
12beb 6e 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74 20 nShmNode object
12bec 2a 2f 0a 20 20 77 69 6e 53 68 6d 20 2a 70 4e 65 */. winShm *pNe
12bed 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 xt;
12bee 2f 2a 20 4e 65 78 74 20 77 69 6e 53 68 6d 20 77 /* Next winShm w
12bef 69 74 68 20 74 68 65 20 73 61 6d 65 20 77 69 6e ith the same win
12bf0 53 68 6d 4e 6f 64 65 20 2a 2f 0a 20 20 75 38 20 ShmNode */. u8
12bf1 68 61 73 4d 75 74 65 78 3b 20 20 20 20 20 20 20 hasMutex;
12bf2 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
12bf3 69 66 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 77 if holding the w
12bf4 69 6e 53 68 6d 4e 6f 64 65 20 6d 75 74 65 78 20 inShmNode mutex
12bf5 2a 2f 0a 20 20 75 31 36 20 73 68 61 72 65 64 4d */. u16 sharedM
12bf6 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 ask;
12bf7 2f 2a 20 4d 61 73 6b 20 6f 66 20 73 68 61 72 65 /* Mask of share
12bf8 64 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a d locks held */.
12bf9 20 20 75 31 36 20 65 78 63 6c 4d 61 73 6b 3b 20 u16 exclMask;
12bfa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12bfb 4d 61 73 6b 20 6f 66 20 65 78 63 6c 75 73 69 76 Mask of exclusiv
12bfc 65 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a e locks held */.
12bfd 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
12bfe 42 55 47 0a 20 20 75 38 20 69 64 3b 20 20 20 20 BUG. u8 id;
12bff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12c00 20 2f 2a 20 49 64 20 6f 66 20 74 68 69 73 20 63 /* Id of this c
12c01 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 69 onnection with i
12c02 74 73 20 77 69 6e 53 68 6d 4e 6f 64 65 20 2a 2f ts winShmNode */
12c03 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a .#endif.};../*.*
12c04 2a 20 43 6f 6e 73 74 61 6e 74 73 20 75 73 65 64 * Constants used
12c05 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 0a 2a 2f 0a for locking.*/.
12c06 23 64 65 66 69 6e 65 20 57 49 4e 5f 53 48 4d 5f #define WIN_SHM_
12c07 42 41 53 45 20 20 20 28 28 32 32 2b 53 51 4c 49 BASE ((22+SQLI
12c08 54 45 5f 53 48 4d 5f 4e 4c 4f 43 4b 29 2a 34 29 TE_SHM_NLOCK)*4)
12c09 20 20 20 20 20 20 20 20 2f 2a 20 66 69 72 73 74 /* first
12c0a 20 6c 6f 63 6b 20 62 79 74 65 20 2a 2f 0a 23 64 lock byte */.#d
12c0b 65 66 69 6e 65 20 57 49 4e 5f 53 48 4d 5f 44 4d efine WIN_SHM_DM
12c0c 53 20 20 20 20 28 57 49 4e 5f 53 48 4d 5f 42 41 S (WIN_SHM_BA
12c0d 53 45 2b 53 51 4c 49 54 45 5f 53 48 4d 5f 4e 4c SE+SQLITE_SHM_NL
12c0e 4f 43 4b 29 20 20 2f 2a 20 64 65 61 64 6d 61 6e OCK) /* deadman
12c0f 20 73 77 69 74 63 68 20 2a 2f 0a 0a 2f 2a 0a 2a switch */../*.*
12c10 2a 20 41 70 70 6c 79 20 61 64 76 69 73 6f 72 79 * Apply advisory
12c11 20 6c 6f 63 6b 73 20 66 6f 72 20 61 6c 6c 20 6e locks for all n
12c12 20 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 bytes beginning
12c13 20 61 74 20 6f 66 73 74 2e 0a 2a 2f 0a 23 64 65 at ofst..*/.#de
12c14 66 69 6e 65 20 5f 53 48 4d 5f 55 4e 4c 43 4b 20 fine _SHM_UNLCK
12c15 20 31 0a 23 64 65 66 69 6e 65 20 5f 53 48 4d 5f 1.#define _SHM_
12c16 52 44 4c 43 4b 20 20 32 0a 23 64 65 66 69 6e 65 RDLCK 2.#define
12c17 20 5f 53 48 4d 5f 57 52 4c 43 4b 20 20 33 0a 73 _SHM_WRLCK 3.s
12c18 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 68 6d tatic int winShm
12c19 53 79 73 74 65 6d 4c 6f 63 6b 28 0a 20 20 77 69 SystemLock(. wi
12c1a 6e 53 68 6d 4e 6f 64 65 20 2a 70 46 69 6c 65 2c nShmNode *pFile,
12c1b 20 20 20 20 2f 2a 20 41 70 70 6c 79 20 6c 6f 63 /* Apply loc
12c1c 6b 73 20 74 6f 20 74 68 69 73 20 6f 70 65 6e 20 ks to this open
12c1d 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 73 65 shared-memory se
12c1e 67 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6c gment */. int l
12c1f 6f 63 6b 54 79 70 65 2c 20 20 20 20 20 20 20 20 ockType,
12c20 20 2f 2a 20 5f 53 48 4d 5f 55 4e 4c 43 4b 2c 20 /* _SHM_UNLCK,
12c21 5f 53 48 4d 5f 52 44 4c 43 4b 2c 20 6f 72 20 5f _SHM_RDLCK, or _
12c22 53 48 4d 5f 57 52 4c 43 4b 20 2a 2f 0a 20 20 69 SHM_WRLCK */. i
12c23 6e 74 20 6f 66 73 74 2c 20 20 20 20 20 20 20 20 nt ofst,
12c24 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 /* Offset t
12c25 6f 20 66 69 72 73 74 20 62 79 74 65 20 74 6f 20 o first byte to
12c26 62 65 20 6c 6f 63 6b 65 64 2f 75 6e 6c 6f 63 6b be locked/unlock
12c27 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 ed */. int nByt
12c28 65 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e /*
12c29 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
12c2a 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 75 6e 6c 6f to lock or unlo
12c2b 63 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 ck */.){. int r
12c2c 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 c = 0;
12c2d 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 /* Result code
12c2e 66 6f 72 6d 20 4c 6f 63 6b 2f 55 6e 6c 6f 63 6b form Lock/Unlock
12c2f 46 69 6c 65 45 78 28 29 20 2a 2f 0a 0a 20 20 2f FileEx() */.. /
12c30 2a 20 41 63 63 65 73 73 20 74 6f 20 74 68 65 20 * Access to the
12c31 77 69 6e 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 winShmNode objec
12c32 74 20 69 73 20 73 65 72 69 61 6c 69 7a 65 64 20 t is serialized
12c33 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 2a 2f by the caller */
12c34 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
12c35 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 46 e3_mutex_held(pF
12c36 69 6c 65 2d 3e 6d 75 74 65 78 29 20 7c 7c 20 70 ile->mutex) || p
12c37 46 69 6c 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b File->nRef==0 );
12c38 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 65 2f 41 .. /* Release/A
12c39 63 71 75 69 72 65 20 74 68 65 20 73 79 73 74 65 cquire the syste
12c3a 6d 2d 6c 65 76 65 6c 20 6c 6f 63 6b 20 2a 2f 0a m-level lock */.
12c3b 20 20 69 66 28 20 6c 6f 63 6b 54 79 70 65 3d 3d if( lockType==
12c3c 5f 53 48 4d 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 _SHM_UNLCK ){.
12c3d 20 20 72 63 20 3d 20 77 69 6e 55 6e 6c 6f 63 6b rc = winUnlock
12c3e 46 69 6c 65 28 26 70 46 69 6c 65 2d 3e 68 46 69 File(&pFile->hFi
12c3f 6c 65 2e 68 2c 20 6f 66 73 74 2c 20 30 2c 20 6e le.h, ofst, 0, n
12c40 42 79 74 65 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 Byte, 0);. }els
12c41 65 7b 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 e{. /* Initia
12c42 6c 69 7a 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 lize the locking
12c43 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20 parameters */.
12c44 20 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 DWORD dwFlags
12c45 20 3d 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c = LOCKFILE_FAIL
12c46 5f 49 4d 4d 45 44 49 41 54 45 4c 59 3b 0a 20 20 _IMMEDIATELY;.
12c47 20 20 69 66 28 20 6c 6f 63 6b 54 79 70 65 20 3d if( lockType =
12c48 3d 20 5f 53 48 4d 5f 57 52 4c 43 4b 20 29 20 64 = _SHM_WRLCK ) d
12c49 77 46 6c 61 67 73 20 7c 3d 20 4c 4f 43 4b 46 49 wFlags |= LOCKFI
12c4a 4c 45 5f 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 LE_EXCLUSIVE_LOC
12c4b 4b 3b 0a 20 20 20 20 72 63 20 3d 20 77 69 6e 4c K;. rc = winL
12c4c 6f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65 2d 3e ockFile(&pFile->
12c4d 68 46 69 6c 65 2e 68 2c 20 64 77 46 6c 61 67 73 hFile.h, dwFlags
12c4e 2c 20 6f 66 73 74 2c 20 30 2c 20 6e 42 79 74 65 , ofst, 0, nByte
12c4f 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 69 , 0);. }. . i
12c50 66 28 20 72 63 21 3d 20 30 20 29 7b 0a 20 20 20 f( rc!= 0 ){.
12c51 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
12c52 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 46 . }else{. pF
12c53 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
12c54 20 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 osGetLastError
12c55 28 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ();. rc = SQL
12c56 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 ITE_BUSY;. }..
12c57 20 4f 53 54 52 41 43 45 28 28 22 53 48 4d 2d 4c OSTRACE(("SHM-L
12c58 4f 43 4b 20 25 64 20 25 73 20 25 73 20 30 78 25 OCK %d %s %s 0x%
12c59 30 38 6c 78 5c 6e 22 2c 20 0a 20 20 20 20 20 20 08lx\n", .
12c5a 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 46 69 6c pFile->hFil
12c5b 65 2e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 e.h,.
12c5c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 rc==SQLITE_OK ?
12c5d 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 2c "ok" : "failed",
12c5e 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b . lock
12c5f 54 79 70 65 3d 3d 5f 53 48 4d 5f 55 4e 4c 43 4b Type==_SHM_UNLCK
12c60 20 3f 20 22 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 ? "UnlockFileEx
12c61 22 20 3a 20 22 4c 6f 63 6b 46 69 6c 65 45 78 22 " : "LockFileEx"
12c62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 46 69 ,. pFi
12c63 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 29 29 3b le->lastErrno));
12c64 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
12c65 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 ../* Forward ref
12c66 65 72 65 6e 63 65 73 20 74 6f 20 56 46 53 20 6d erences to VFS m
12c67 65 74 68 6f 64 73 20 2a 2f 0a 73 74 61 74 69 63 ethods */.static
12c68 20 69 6e 74 20 77 69 6e 4f 70 65 6e 28 73 71 6c int winOpen(sql
12c69 69 74 65 33 5f 76 66 73 2a 2c 63 6f 6e 73 74 20 ite3_vfs*,const
12c6a 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 66 69 char*,sqlite3_fi
12c6b 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2a 29 3b 0a 73 le*,int,int*);.s
12c6c 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 6c tatic int winDel
12c6d 65 74 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ete(sqlite3_vfs
12c6e 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e *,const char*,in
12c6f 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 75 72 67 65 t);../*.** Purge
12c70 20 74 68 65 20 77 69 6e 53 68 6d 4e 6f 64 65 4c the winShmNodeL
12c71 69 73 74 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 ist list of all
12c72 65 6e 74 72 69 65 73 20 77 69 74 68 20 77 69 6e entries with win
12c73 53 68 6d 4e 6f 64 65 2e 6e 52 65 66 3d 3d 30 2e ShmNode.nRef==0.
12c74 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e .**.** This is n
12c75 6f 74 20 61 20 56 46 53 20 73 68 61 72 65 64 2d ot a VFS shared-
12c76 6d 65 6d 6f 72 79 20 6d 65 74 68 6f 64 3b 20 69 memory method; i
12c77 74 20 69 73 20 61 20 75 74 69 6c 69 74 79 20 66 t is a utility f
12c78 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65 64 0a 2a unction called.*
12c79 2a 20 62 79 20 56 46 53 20 73 68 61 72 65 64 2d * by VFS shared-
12c7a 6d 65 6d 6f 72 79 20 6d 65 74 68 6f 64 73 2e 0a memory methods..
12c7b 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 */.static void w
12c7c 69 6e 53 68 6d 50 75 72 67 65 28 73 71 6c 69 74 inShmPurge(sqlit
12c7d 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
12c7e 74 20 64 65 6c 65 74 65 46 6c 61 67 29 7b 0a 20 t deleteFlag){.
12c7f 20 77 69 6e 53 68 6d 4e 6f 64 65 20 2a 2a 70 70 winShmNode **pp
12c80 3b 0a 20 20 77 69 6e 53 68 6d 4e 6f 64 65 20 2a ;. winShmNode *
12c81 70 3b 0a 20 20 42 4f 4f 4c 20 62 52 63 3b 0a 20 p;. BOOL bRc;.
12c82 20 61 73 73 65 72 74 28 20 77 69 6e 53 68 6d 4d assert( winShmM
12c83 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 utexHeld() );.
12c84 70 70 20 3d 20 26 77 69 6e 53 68 6d 4e 6f 64 65 pp = &winShmNode
12c85 4c 69 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 28 List;. while( (
12c86 70 20 3d 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20 p = *pp)!=0 ){.
12c87 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d if( p->nRef==
12c88 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 0 ){. int i
12c89 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6d ;. if( p->m
12c8a 75 74 65 78 20 29 20 73 71 6c 69 74 65 33 5f 6d utex ) sqlite3_m
12c8b 75 74 65 78 5f 66 72 65 65 28 70 2d 3e 6d 75 74 utex_free(p->mut
12c8c 65 78 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 ex);. for(i
12c8d 3d 30 3b 20 69 3c 70 2d 3e 6e 52 65 67 69 6f 6e =0; i<p->nRegion
12c8e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; i++){.
12c8f 62 52 63 20 3d 20 6f 73 55 6e 6d 61 70 56 69 65 bRc = osUnmapVie
12c90 77 4f 66 46 69 6c 65 28 70 2d 3e 61 52 65 67 69 wOfFile(p->aRegi
12c91 6f 6e 5b 69 5d 2e 70 4d 61 70 29 3b 0a 20 20 20 on[i].pMap);.
12c92 20 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 53 OSTRACE(("S
12c93 48 4d 2d 50 55 52 47 45 20 70 69 64 2d 25 64 20 HM-PURGE pid-%d
12c94 75 6e 6d 61 70 20 72 65 67 69 6f 6e 3d 25 64 20 unmap region=%d
12c95 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 %s\n",.
12c96 20 20 20 20 20 20 20 20 28 69 6e 74 29 6f 73 47 (int)osG
12c97 65 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73 73 etCurrentProcess
12c98 49 64 28 29 2c 20 69 2c 0a 20 20 20 20 20 20 20 Id(), i,.
12c99 20 20 20 20 20 20 20 20 20 20 62 52 63 20 3f 20 bRc ?
12c9a 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 "ok" : "failed")
12c9b 29 3b 0a 20 20 20 20 20 20 20 20 62 52 63 20 3d );. bRc =
12c9c 20 6f 73 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 osCloseHandle(p
12c9d 2d 3e 61 52 65 67 69 6f 6e 5b 69 5d 2e 68 4d 61 ->aRegion[i].hMa
12c9e 70 29 3b 0a 20 20 20 20 20 20 20 20 4f 53 54 52 p);. OSTR
12c9f 41 43 45 28 28 22 53 48 4d 2d 50 55 52 47 45 20 ACE(("SHM-PURGE
12ca0 70 69 64 2d 25 64 20 63 6c 6f 73 65 20 72 65 67 pid-%d close reg
12ca1 69 6f 6e 3d 25 64 20 25 73 5c 6e 22 2c 0a 20 20 ion=%d %s\n",.
12ca2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
12ca3 69 6e 74 29 6f 73 47 65 74 43 75 72 72 65 6e 74 int)osGetCurrent
12ca4 50 72 6f 63 65 73 73 49 64 28 29 2c 20 69 2c 0a ProcessId(), i,.
12ca5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12ca6 20 62 52 63 20 3f 20 22 6f 6b 22 20 3a 20 22 66 bRc ? "ok" : "f
12ca7 61 69 6c 65 64 22 29 29 3b 0a 20 20 20 20 20 20 ailed"));.
12ca8 7d 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 68 }. if( p->h
12ca9 46 69 6c 65 2e 68 20 21 3d 20 49 4e 56 41 4c 49 File.h != INVALI
12caa 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 D_HANDLE_VALUE )
12cab 7b 0a 20 20 20 20 20 20 20 20 53 69 6d 75 6c 61 {. Simula
12cac 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
12cad 31 29 3b 0a 20 20 20 20 20 20 20 20 77 69 6e 43 1);. winC
12cae 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 lose((sqlite3_fi
12caf 6c 65 20 2a 29 26 70 2d 3e 68 46 69 6c 65 29 3b le *)&p->hFile);
12cb0 0a 20 20 20 20 20 20 20 20 53 69 6d 75 6c 61 74 . Simulat
12cb1 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 eIOErrorBenign(0
12cb2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
12cb3 20 69 66 28 20 64 65 6c 65 74 65 46 6c 61 67 20 if( deleteFlag
12cb4 29 7b 0a 20 20 20 20 20 20 20 20 53 69 6d 75 6c ){. Simul
12cb5 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e ateIOErrorBenign
12cb6 28 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c (1);. sql
12cb7 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
12cb8 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 alloc();.
12cb9 20 77 69 6e 44 65 6c 65 74 65 28 70 56 66 73 2c winDelete(pVfs,
12cba 20 70 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 30 p->zFilename, 0
12cbb 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
12cbc 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f e3EndBenignMallo
12cbd 63 28 29 3b 0a 20 20 20 20 20 20 20 20 53 69 6d c();. Sim
12cbe 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 ulateIOErrorBeni
12cbf 67 6e 28 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 gn(0);. }.
12cc0 20 20 20 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e *pp = p->pN
12cc1 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ext;. sqlit
12cc2 65 33 5f 66 72 65 65 28 70 2d 3e 61 52 65 67 69 e3_free(p->aRegi
12cc3 6f 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 on);. sqlit
12cc4 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 e3_free(p);.
12cc5 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 70 20 }else{. pp
12cc6 3d 20 26 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 = &p->pNext;.
12cc7 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
12cc8 4f 70 65 6e 20 74 68 65 20 73 68 61 72 65 64 2d Open the shared-
12cc9 6d 65 6d 6f 72 79 20 61 72 65 61 20 61 73 73 6f memory area asso
12cca 63 69 61 74 65 64 20 77 69 74 68 20 64 61 74 61 ciated with data
12ccb 62 61 73 65 20 66 69 6c 65 20 70 44 62 46 64 2e base file pDbFd.
12ccc 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 6f 70 65 6e .**.** When open
12ccd 69 6e 67 20 61 20 6e 65 77 20 73 68 61 72 65 64 ing a new shared
12cce 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2c 20 69 66 -memory file, if
12ccf 20 6e 6f 20 6f 74 68 65 72 20 69 6e 73 74 61 6e no other instan
12cd0 63 65 73 20 6f 66 20 74 68 61 74 0a 2a 2a 20 66 ces of that.** f
12cd1 69 6c 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c ile are currentl
12cd2 79 20 6f 70 65 6e 2c 20 69 6e 20 74 68 69 73 20 y open, in this
12cd3 70 72 6f 63 65 73 73 20 6f 72 20 69 6e 20 6f 74 process or in ot
12cd4 68 65 72 20 70 72 6f 63 65 73 73 65 73 2c 20 74 her processes, t
12cd5 68 65 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 hen.** the file
12cd6 6d 75 73 74 20 62 65 20 74 72 75 6e 63 61 74 65 must be truncate
12cd7 64 20 74 6f 20 7a 65 72 6f 20 6c 65 6e 67 74 68 d to zero length
12cd8 20 6f 72 20 68 61 76 65 20 69 74 73 20 68 65 61 or have its hea
12cd9 64 65 72 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a der cleared..*/.
12cda 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70 static int winOp
12cdb 65 6e 53 68 61 72 65 64 4d 65 6d 6f 72 79 28 77 enSharedMemory(w
12cdc 69 6e 46 69 6c 65 20 2a 70 44 62 46 64 29 7b 0a inFile *pDbFd){.
12cdd 20 20 73 74 72 75 63 74 20 77 69 6e 53 68 6d 20 struct winShm
12cde 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 *p;
12cdf 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 6e /* The conn
12ce0 65 63 74 69 6f 6e 20 74 6f 20 62 65 20 6f 70 65 ection to be ope
12ce1 6e 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ned */. struct
12ce2 77 69 6e 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d winShmNode *pShm
12ce3 4e 6f 64 65 20 3d 20 30 3b 20 20 20 2f 2a 20 54 Node = 0; /* T
12ce4 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 6d he underlying mm
12ce5 61 70 70 65 64 20 66 69 6c 65 20 2a 2f 0a 20 20 apped file */.
12ce6 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
12ce7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12ce8 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 /* Result cod
12ce9 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 77 69 e */. struct wi
12cea 6e 53 68 6d 4e 6f 64 65 20 2a 70 4e 65 77 3b 20 nShmNode *pNew;
12ceb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 /* New
12cec 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 77 69 6e ly allocated win
12ced 53 68 6d 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 ShmNode */. int
12cee 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 nName;
12cef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12cf0 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 4e 61 6d 65 /* Size of zName
12cf1 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 0a 20 20 in bytes */..
12cf2 61 73 73 65 72 74 28 20 70 44 62 46 64 2d 3e 70 assert( pDbFd->p
12cf3 53 68 6d 3d 3d 30 20 29 3b 20 20 20 20 2f 2a 20 Shm==0 ); /*
12cf4 4e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 6f Not previously o
12cf5 70 65 6e 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 41 pened */.. /* A
12cf6 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f llocate space fo
12cf7 72 20 74 68 65 20 6e 65 77 20 73 71 6c 69 74 65 r the new sqlite
12cf8 33 5f 73 68 6d 20 6f 62 6a 65 63 74 2e 20 20 41 3_shm object. A
12cf9 6c 73 6f 20 73 70 65 63 75 6c 61 74 69 76 65 6c lso speculativel
12cfa 79 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 y. ** allocate
12cfb 73 70 61 63 65 20 66 6f 72 20 61 20 6e 65 77 20 space for a new
12cfc 77 69 6e 53 68 6d 4e 6f 64 65 20 61 6e 64 20 66 winShmNode and f
12cfd 69 6c 65 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 ilename.. */.
12cfe 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f p = sqlite3Mallo
12cff 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 cZero( sizeof(*p
12d00 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 ) );. if( p==0
12d01 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
12d02 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 6e IOERR_NOMEM;. n
12d03 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 Name = sqlite3St
12d04 72 6c 65 6e 33 30 28 70 44 62 46 64 2d 3e 7a 50 rlen30(pDbFd->zP
12d05 61 74 68 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 ath);. pNew = s
12d06 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
12d07 28 20 73 69 7a 65 6f 66 28 2a 70 53 68 6d 4e 6f ( sizeof(*pShmNo
12d08 64 65 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 37 de) + nName + 17
12d09 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d );. if( pNew==
12d0a 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
12d0b 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 72 65 _free(p);. re
12d0c 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
12d0d 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 R_NOMEM;. }. p
12d0e 4e 65 77 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20 3d New->zFilename =
12d0f 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d (char*)&pNew[1]
12d10 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 ;. sqlite3_snpr
12d11 69 6e 74 66 28 6e 4e 61 6d 65 2b 31 35 2c 20 70 intf(nName+15, p
12d12 4e 65 77 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 New->zFilename,
12d13 22 25 73 2d 73 68 6d 22 2c 20 70 44 62 46 64 2d "%s-shm", pDbFd-
12d14 3e 7a 50 61 74 68 29 3b 0a 20 20 73 71 6c 69 74 >zPath);. sqlit
12d15 65 33 46 69 6c 65 53 75 66 66 69 78 33 28 70 44 e3FileSuffix3(pD
12d16 62 46 64 2d 3e 7a 50 61 74 68 2c 20 70 4e 65 77 bFd->zPath, pNew
12d17 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 0a 0a ->zFilename); ..
12d18 20 20 2f 2a 20 4c 6f 6f 6b 20 74 6f 20 73 65 65 /* Look to see
12d19 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 if there is an
12d1a 65 78 69 73 74 69 6e 67 20 77 69 6e 53 68 6d 4e existing winShmN
12d1b 6f 64 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 ode that can be
12d1c 75 73 65 64 2e 0a 20 20 2a 2a 20 49 66 20 6e 6f used.. ** If no
12d1d 20 6d 61 74 63 68 69 6e 67 20 77 69 6e 53 68 6d matching winShm
12d1e 4e 6f 64 65 20 63 75 72 72 65 6e 74 6c 79 20 65 Node currently e
12d1f 78 69 73 74 73 2c 20 63 72 65 61 74 65 20 61 20 xists, create a
12d20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 new one.. */.
12d21 77 69 6e 53 68 6d 45 6e 74 65 72 4d 75 74 65 78 winShmEnterMutex
12d22 28 29 3b 0a 20 20 66 6f 72 28 70 53 68 6d 4e 6f ();. for(pShmNo
12d23 64 65 20 3d 20 77 69 6e 53 68 6d 4e 6f 64 65 4c de = winShmNodeL
12d24 69 73 74 3b 20 70 53 68 6d 4e 6f 64 65 3b 20 70 ist; pShmNode; p
12d25 53 68 6d 4e 6f 64 65 3d 70 53 68 6d 4e 6f 64 65 ShmNode=pShmNode
12d26 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 2f 2a ->pNext){. /*
12d27 20 54 42 44 20 6e 65 65 64 20 74 6f 20 63 6f 6d TBD need to com
12d28 65 20 75 70 20 77 69 74 68 20 62 65 74 74 65 72 e up with better
12d29 20 6d 61 74 63 68 20 68 65 72 65 2e 20 20 50 65 match here. Pe
12d2a 72 68 61 70 73 0a 20 20 20 20 2a 2a 20 75 73 65 rhaps. ** use
12d2b 20 46 49 4c 45 5f 49 44 5f 42 4f 54 48 5f 44 49 FILE_ID_BOTH_DI
12d2c 52 5f 49 4e 46 4f 20 53 74 72 75 63 74 75 72 65 R_INFO Structure
12d2d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
12d2e 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
12d2f 70 53 68 6d 4e 6f 64 65 2d 3e 7a 46 69 6c 65 6e pShmNode->zFilen
12d30 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a 46 69 6c 65 ame, pNew->zFile
12d31 6e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b name)==0 ) break
12d32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 68 6d ;. }. if( pShm
12d33 4e 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 Node ){. sqli
12d34 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a te3_free(pNew);.
12d35 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 53 68 }else{. pSh
12d36 6d 4e 6f 64 65 20 3d 20 70 4e 65 77 3b 0a 20 20 mNode = pNew;.
12d37 20 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20 pNew = 0;.
12d38 28 28 77 69 6e 46 69 6c 65 2a 29 28 26 70 53 68 ((winFile*)(&pSh
12d39 6d 4e 6f 64 65 2d 3e 68 46 69 6c 65 29 29 2d 3e mNode->hFile))->
12d3a 68 20 3d 20 49 4e 56 41 4c 49 44 5f 48 41 4e 44 h = INVALID_HAND
12d3b 4c 45 5f 56 41 4c 55 45 3b 0a 20 20 20 20 70 53 LE_VALUE;. pS
12d3c 68 6d 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 hmNode->pNext =
12d3d 77 69 6e 53 68 6d 4e 6f 64 65 4c 69 73 74 3b 0a winShmNodeList;.
12d3e 20 20 20 20 77 69 6e 53 68 6d 4e 6f 64 65 4c 69 winShmNodeLi
12d3f 73 74 20 3d 20 70 53 68 6d 4e 6f 64 65 3b 0a 0a st = pShmNode;..
12d40 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 pShmNode->mu
12d41 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 tex = sqlite3_mu
12d42 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 tex_alloc(SQLITE
12d43 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20 _MUTEX_FAST);.
12d44 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e if( pShmNode->
12d45 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 mutex==0 ){.
12d46 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
12d47 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ERR_NOMEM;.
12d48 20 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65 goto shm_open_e
12d49 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 rr;. }.. r
12d4a 63 20 3d 20 77 69 6e 4f 70 65 6e 28 70 44 62 46 c = winOpen(pDbF
12d4b 64 2d 3e 70 56 66 73 2c 0a 20 20 20 20 20 20 20 d->pVfs,.
12d4c 20 20 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f pShmNo
12d4d 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 de->zFilename,
12d4e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
12d4f 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28 me of the file (
12d50 55 54 46 2d 38 29 20 2a 2f 0a 20 20 20 20 20 20 UTF-8) */.
12d51 20 20 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 (sqli
12d52 74 65 33 5f 66 69 6c 65 2a 29 26 70 53 68 6d 4e te3_file*)&pShmN
12d53 6f 64 65 2d 3e 68 46 69 6c 65 2c 20 20 2f 2a 20 ode->hFile, /*
12d54 46 69 6c 65 20 68 61 6e 64 6c 65 20 68 65 72 65 File handle here
12d55 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
12d56 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
12d57 5f 57 41 4c 20 7c 20 53 51 4c 49 54 45 5f 4f 50 _WAL | SQLITE_OP
12d58 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53 EN_READWRITE | S
12d59 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
12d5a 45 2c 20 2f 2a 20 4d 6f 64 65 20 66 6c 61 67 73 E, /* Mode flags
12d5b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
12d5c 20 20 20 20 20 30 29 3b 0a 20 20 20 20 69 66 28 0);. if(
12d5d 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 SQLITE_OK!=rc )
12d5e 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 68 6d {. goto shm
12d5f 5f 6f 70 65 6e 5f 65 72 72 3b 0a 20 20 20 20 7d _open_err;. }
12d60 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 .. /* Check t
12d61 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 68 65 72 o see if another
12d62 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 process is hold
12d63 69 6e 67 20 74 68 65 20 64 65 61 64 2d 6d 61 6e ing the dead-man
12d64 20 73 77 69 74 63 68 2e 0a 20 20 20 20 2a 2a 20 switch.. **
12d65 49 66 20 6e 6f 74 2c 20 74 72 75 6e 63 61 74 65 If not, truncate
12d66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 7a 65 72 the file to zer
12d67 6f 20 6c 65 6e 67 74 68 2e 20 0a 20 20 20 20 2a o length. . *
12d68 2f 0a 20 20 20 20 69 66 28 20 77 69 6e 53 68 6d /. if( winShm
12d69 53 79 73 74 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e SystemLock(pShmN
12d6a 6f 64 65 2c 20 5f 53 48 4d 5f 57 52 4c 43 4b 2c ode, _SHM_WRLCK,
12d6b 20 57 49 4e 5f 53 48 4d 5f 44 4d 53 2c 20 31 29 WIN_SHM_DMS, 1)
12d6c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
12d6d 20 20 20 20 20 72 63 20 3d 20 77 69 6e 54 72 75 rc = winTru
12d6e 6e 63 61 74 65 28 28 73 71 6c 69 74 65 33 5f 66 ncate((sqlite3_f
12d6f 69 6c 65 20 2a 29 26 70 53 68 6d 4e 6f 64 65 2d ile *)&pShmNode-
12d70 3e 68 46 69 6c 65 2c 20 30 29 3b 0a 20 20 20 20 >hFile, 0);.
12d71 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12d72 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
12d73 63 20 3d 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 c = winLogError(
12d74 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d SQLITE_IOERR_SHM
12d75 4f 50 45 4e 2c 20 6f 73 47 65 74 4c 61 73 74 45 OPEN, osGetLastE
12d76 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20 20 rror(),.
12d77 20 20 20 20 20 20 20 20 20 22 77 69 6e 4f 70 65 "winOpe
12d78 6e 53 68 6d 22 2c 20 70 44 62 46 64 2d 3e 7a 50 nShm", pDbFd->zP
12d79 61 74 68 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ath);. }.
12d7a 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
12d7b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
12d7c 20 20 20 77 69 6e 53 68 6d 53 79 73 74 65 6d 4c winShmSystemL
12d7d 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c 20 5f 53 ock(pShmNode, _S
12d7e 48 4d 5f 55 4e 4c 43 4b 2c 20 57 49 4e 5f 53 48 HM_UNLCK, WIN_SH
12d7f 4d 5f 44 4d 53 2c 20 31 29 3b 0a 20 20 20 20 20 M_DMS, 1);.
12d80 20 72 63 20 3d 20 77 69 6e 53 68 6d 53 79 73 74 rc = winShmSyst
12d81 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c emLock(pShmNode,
12d82 20 5f 53 48 4d 5f 52 44 4c 43 4b 2c 20 57 49 4e _SHM_RDLCK, WIN
12d83 5f 53 48 4d 5f 44 4d 53 2c 20 31 29 3b 0a 20 20 _SHM_DMS, 1);.
12d84 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29 }. if( rc )
12d85 20 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65 goto shm_open_e
12d86 72 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 rr;. }.. /* Ma
12d87 6b 65 20 74 68 65 20 6e 65 77 20 63 6f 6e 6e 65 ke the new conne
12d88 63 74 69 6f 6e 20 61 20 63 68 69 6c 64 20 6f 66 ction a child of
12d89 20 74 68 65 20 77 69 6e 53 68 6d 4e 6f 64 65 20 the winShmNode
12d8a 2a 2f 0a 20 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 */. p->pShmNode
12d8b 20 3d 20 70 53 68 6d 4e 6f 64 65 3b 0a 23 69 66 = pShmNode;.#if
12d8c 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
12d8d 0a 20 20 70 2d 3e 69 64 20 3d 20 70 53 68 6d 4e . p->id = pShmN
12d8e 6f 64 65 2d 3e 6e 65 78 74 53 68 6d 49 64 2b 2b ode->nextShmId++
12d8f 3b 0a 23 65 6e 64 69 66 0a 20 20 70 53 68 6d 4e ;.#endif. pShmN
12d90 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 70 ode->nRef++;. p
12d91 44 62 46 64 2d 3e 70 53 68 6d 20 3d 20 70 3b 0a DbFd->pShm = p;.
12d92 20 20 77 69 6e 53 68 6d 4c 65 61 76 65 4d 75 74 winShmLeaveMut
12d93 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 ex();.. /* The
12d94 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 reference count
12d95 6f 6e 20 70 53 68 6d 4e 6f 64 65 20 68 61 73 20 on pShmNode has
12d96 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 63 already been inc
12d97 72 65 6d 65 6e 74 65 64 20 75 6e 64 65 72 0a 20 remented under.
12d98 20 2a 2a 20 74 68 65 20 63 6f 76 65 72 20 6f 66 ** the cover of
12d99 20 74 68 65 20 77 69 6e 53 68 6d 45 6e 74 65 72 the winShmEnter
12d9a 4d 75 74 65 78 28 29 20 6d 75 74 65 78 20 61 6e Mutex() mutex an
12d9b 64 20 74 68 65 20 70 6f 69 6e 74 65 72 20 66 72 d the pointer fr
12d9c 6f 6d 20 74 68 65 0a 20 20 2a 2a 20 6e 65 77 20 om the. ** new
12d9d 28 73 74 72 75 63 74 20 77 69 6e 53 68 6d 29 20 (struct winShm)
12d9e 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 20 70 53 object to the pS
12d9f 68 6d 4e 6f 64 65 20 68 61 73 20 62 65 65 6e 20 hmNode has been
12da0 73 65 74 2e 20 41 6c 6c 20 74 68 61 74 20 69 73 set. All that is
12da1 0a 20 20 2a 2a 20 6c 65 66 74 20 74 6f 20 64 6f . ** left to do
12da2 20 69 73 20 74 6f 20 6c 69 6e 6b 20 74 68 65 20 is to link the
12da3 6e 65 77 20 6f 62 6a 65 63 74 20 69 6e 74 6f 20 new object into
12da4 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 the linked list
12da5 73 74 61 72 74 69 6e 67 0a 20 20 2a 2a 20 61 74 starting. ** at
12da6 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 pShmNode->pFirs
12da7 74 2e 20 54 68 69 73 20 6d 75 73 74 20 62 65 20 t. This must be
12da8 64 6f 6e 65 20 77 68 69 6c 65 20 68 6f 6c 64 69 done while holdi
12da9 6e 67 20 74 68 65 20 70 53 68 6d 4e 6f 64 65 2d ng the pShmNode-
12daa 3e 6d 75 74 65 78 20 0a 20 20 2a 2a 20 6d 75 74 >mutex . ** mut
12dab 65 78 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 ex.. */. sqlit
12dac 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 e3_mutex_enter(p
12dad 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b ShmNode->mutex);
12dae 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53 . p->pNext = pS
12daf 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74 3b 0a hmNode->pFirst;.
12db0 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 pShmNode->pFir
12db1 73 74 20 3d 20 70 3b 0a 20 20 73 71 6c 69 74 65 st = p;. sqlite
12db2 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 53 3_mutex_leave(pS
12db3 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0a hmNode->mutex);.
12db4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
12db5 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 OK;.. /* Jump h
12db6 65 72 65 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72 ere on any error
12db7 20 2a 2f 0a 73 68 6d 5f 6f 70 65 6e 5f 65 72 72 */.shm_open_err
12db8 3a 0a 20 20 77 69 6e 53 68 6d 53 79 73 74 65 6d :. winShmSystem
12db9 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c 20 5f Lock(pShmNode, _
12dba 53 48 4d 5f 55 4e 4c 43 4b 2c 20 57 49 4e 5f 53 SHM_UNLCK, WIN_S
12dbb 48 4d 5f 44 4d 53 2c 20 31 29 3b 0a 20 20 77 69 HM_DMS, 1);. wi
12dbc 6e 53 68 6d 50 75 72 67 65 28 70 44 62 46 64 2d nShmPurge(pDbFd-
12dbd 3e 70 56 66 73 2c 20 30 29 3b 20 20 20 20 20 20 >pVfs, 0);
12dbe 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 66 72 65 /* This call fre
12dbf 65 73 20 70 53 68 6d 4e 6f 64 65 20 69 66 20 72 es pShmNode if r
12dc0 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 73 71 6c equired */. sql
12dc1 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
12dc2 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 sqlite3_free(pNe
12dc3 77 29 3b 0a 20 20 77 69 6e 53 68 6d 4c 65 61 76 w);. winShmLeav
12dc4 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 eMutex();. retu
12dc5 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
12dc6 43 6c 6f 73 65 20 61 20 63 6f 6e 6e 65 63 74 69 Close a connecti
12dc7 6f 6e 20 74 6f 20 73 68 61 72 65 64 2d 6d 65 6d on to shared-mem
12dc8 6f 72 79 2e 20 20 44 65 6c 65 74 65 20 74 68 65 ory. Delete the
12dc9 20 75 6e 64 65 72 6c 79 69 6e 67 20 0a 2a 2a 20 underlying .**
12dca 73 74 6f 72 61 67 65 20 69 66 20 64 65 6c 65 74 storage if delet
12dcb 65 46 6c 61 67 20 69 73 20 74 72 75 65 2e 0a 2a eFlag is true..*
12dcc 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e /.static int win
12dcd 53 68 6d 55 6e 6d 61 70 28 0a 20 20 73 71 6c 69 ShmUnmap(. sqli
12dce 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 20 20 te3_file *fd,
12dcf 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
12dd0 73 65 20 68 6f 6c 64 69 6e 67 20 73 68 61 72 65 se holding share
12dd1 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e d memory */. in
12dd2 74 20 64 65 6c 65 74 65 46 6c 61 67 20 20 20 20 t deleteFlag
12dd3 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 /* Dele
12dd4 74 65 20 61 66 74 65 72 20 63 6c 6f 73 69 6e 67 te after closing
12dd5 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 if true */.){.
12dd6 20 77 69 6e 46 69 6c 65 20 2a 70 44 62 46 64 3b winFile *pDbFd;
12dd7 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
12dd8 73 65 20 68 6f 6c 64 69 6e 67 20 73 68 61 72 65 se holding share
12dd9 64 2d 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 77 69 d-memory */. wi
12dda 6e 53 68 6d 20 2a 70 3b 20 20 20 20 20 20 20 20 nShm *p;
12ddb 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 6e 65 /* The conne
12ddc 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 6c 6f 73 ction to be clos
12ddd 65 64 20 2a 2f 0a 20 20 77 69 6e 53 68 6d 4e 6f ed */. winShmNo
12dde 64 65 20 2a 70 53 68 6d 4e 6f 64 65 3b 20 2f 2a de *pShmNode; /*
12ddf 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 The underlying
12de0 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 66 69 shared-memory fi
12de1 6c 65 20 2a 2f 0a 20 20 77 69 6e 53 68 6d 20 2a le */. winShm *
12de2 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20 2f 2a *pp; /*
12de3 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 For looping ove
12de4 72 20 73 69 62 6c 69 6e 67 20 63 6f 6e 6e 65 63 r sibling connec
12de5 74 69 6f 6e 73 20 2a 2f 0a 0a 20 20 70 44 62 46 tions */.. pDbF
12de6 64 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 66 64 d = (winFile*)fd
12de7 3b 0a 20 20 70 20 3d 20 70 44 62 46 64 2d 3e 70 ;. p = pDbFd->p
12de8 53 68 6d 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 Shm;. if( p==0
12de9 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
12dea 4f 4b 3b 0a 20 20 70 53 68 6d 4e 6f 64 65 20 3d OK;. pShmNode =
12deb 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0a 0a 20 p->pShmNode;..
12dec 20 2f 2a 20 52 65 6d 6f 76 65 20 63 6f 6e 6e 65 /* Remove conne
12ded 63 74 69 6f 6e 20 70 20 66 72 6f 6d 20 74 68 65 ction p from the
12dee 20 73 65 74 20 6f 66 20 63 6f 6e 6e 65 63 74 69 set of connecti
12def 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64 0a 20 ons associated.
12df0 20 2a 2a 20 77 69 74 68 20 70 53 68 6d 4e 6f 64 ** with pShmNod
12df1 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d e */. sqlite3_m
12df2 75 74 65 78 5f 65 6e 74 65 72 28 70 53 68 6d 4e utex_enter(pShmN
12df3 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 66 ode->mutex);. f
12df4 6f 72 28 70 70 3d 26 70 53 68 6d 4e 6f 64 65 2d or(pp=&pShmNode-
12df5 3e 70 46 69 72 73 74 3b 20 28 2a 70 70 29 21 3d >pFirst; (*pp)!=
12df6 70 3b 20 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e p; pp = &(*pp)->
12df7 70 4e 65 78 74 29 7b 7d 0a 20 20 2a 70 70 20 3d pNext){}. *pp =
12df8 20 70 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 2f 2a p->pNext;.. /*
12df9 20 46 72 65 65 20 74 68 65 20 63 6f 6e 6e 65 63 Free the connec
12dfa 74 69 6f 6e 20 70 20 2a 2f 0a 20 20 73 71 6c 69 tion p */. sqli
12dfb 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 70 te3_free(p);. p
12dfc 44 62 46 64 2d 3e 70 53 68 6d 20 3d 20 30 3b 0a DbFd->pShm = 0;.
12dfd 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
12dfe 6c 65 61 76 65 28 70 53 68 6d 4e 6f 64 65 2d 3e leave(pShmNode->
12dff 6d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 49 66 mutex);.. /* If
12e00 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 20 pShmNode->nRef
12e01 68 61 73 20 72 65 61 63 68 65 64 20 30 2c 20 74 has reached 0, t
12e02 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 75 6e hen close the un
12e03 64 65 72 6c 79 69 6e 67 0a 20 20 2a 2a 20 73 68 derlying. ** sh
12e04 61 72 65 64 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 ared-memory file
12e05 2c 20 74 6f 6f 20 2a 2f 0a 20 20 77 69 6e 53 68 , too */. winSh
12e06 6d 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 mEnterMutex();.
12e07 20 61 73 73 65 72 74 28 20 70 53 68 6d 4e 6f 64 assert( pShmNod
12e08 65 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 70 e->nRef>0 );. p
12e09 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 2d 2d 3b ShmNode->nRef--;
12e0a 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d . if( pShmNode-
12e0b 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 >nRef==0 ){.
12e0c 77 69 6e 53 68 6d 50 75 72 67 65 28 70 44 62 46 winShmPurge(pDbF
12e0d 64 2d 3e 70 56 66 73 2c 20 64 65 6c 65 74 65 46 d->pVfs, deleteF
12e0e 6c 61 67 29 3b 0a 20 20 7d 0a 20 20 77 69 6e 53 lag);. }. winS
12e0f 68 6d 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a hmLeaveMutex();.
12e10 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
12e11 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 _OK;.}../*.** Ch
12e12 61 6e 67 65 20 74 68 65 20 6c 6f 63 6b 20 73 74 ange the lock st
12e13 61 74 65 20 66 6f 72 20 61 20 73 68 61 72 65 64 ate for a shared
12e14 2d 6d 65 6d 6f 72 79 20 73 65 67 6d 65 6e 74 2e -memory segment.
12e15 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
12e16 69 6e 53 68 6d 4c 6f 63 6b 28 0a 20 20 73 71 6c inShmLock(. sql
12e17 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 20 ite3_file *fd,
12e18 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
12e19 61 73 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 ase file holding
12e1a 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f the shared memo
12e1b 72 79 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 73 74 ry */. int ofst
12e1c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
12e1d 20 20 20 2f 2a 20 46 69 72 73 74 20 6c 6f 63 6b /* First lock
12e1e 20 74 6f 20 61 63 71 75 69 72 65 20 6f 72 20 72 to acquire or r
12e1f 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 elease */. int
12e20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n,
12e21 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
12e22 20 6f 66 20 6c 6f 63 6b 73 20 74 6f 20 61 63 71 of locks to acq
12e23 75 69 72 65 20 6f 72 20 72 65 6c 65 61 73 65 20 uire or release
12e24 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 20 */. int flags
12e25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12e26 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 /* What to do wi
12e27 74 68 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 29 th the lock */.)
12e28 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 44 62 {. winFile *pDb
12e29 46 64 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 66 Fd = (winFile*)f
12e2a 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e d; /* Con
12e2b 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 nection holding
12e2c 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 2a 2f shared memory */
12e2d 0a 20 20 77 69 6e 53 68 6d 20 2a 70 20 3d 20 70 . winShm *p = p
12e2e 44 62 46 64 2d 3e 70 53 68 6d 3b 20 20 20 20 20 DbFd->pShm;
12e2f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
12e30 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 62 65 shared memory be
12e31 69 6e 67 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 ing locked */.
12e32 77 69 6e 53 68 6d 20 2a 70 58 3b 20 20 20 20 20 winShm *pX;
12e33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12e34 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f /* For loo
12e35 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 73 69 ping over all si
12e36 62 6c 69 6e 67 73 20 2a 2f 0a 20 20 77 69 6e 53 blings */. winS
12e37 68 6d 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f 64 65 hmNode *pShmNode
12e38 20 3d 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0a = p->pShmNode;.
12e39 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
12e3a 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 E_OK;
12e3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c /* Resul
12e3c 74 20 63 6f 64 65 20 2a 2f 0a 20 20 75 31 36 20 t code */. u16
12e3d 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 mask;
12e3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12e3f 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6c 6f 63 /* Mask of loc
12e40 6b 73 20 74 6f 20 74 61 6b 65 20 6f 72 20 72 65 ks to take or re
12e41 6c 65 61 73 65 20 2a 2f 0a 0a 20 20 61 73 73 65 lease */.. asse
12e42 72 74 28 20 6f 66 73 74 3e 3d 30 20 26 26 20 6f rt( ofst>=0 && o
12e43 66 73 74 2b 6e 3c 3d 53 51 4c 49 54 45 5f 53 48 fst+n<=SQLITE_SH
12e44 4d 5f 4e 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 M_NLOCK );. ass
12e45 65 72 74 28 20 6e 3e 3d 31 20 29 3b 0a 20 20 61 ert( n>=1 );. a
12e46 73 73 65 72 74 28 20 66 6c 61 67 73 3d 3d 28 53 ssert( flags==(S
12e47 51 4c 49 54 45 5f 53 48 4d 5f 4c 4f 43 4b 20 7c QLITE_SHM_LOCK |
12e48 20 53 51 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52 SQLITE_SHM_SHAR
12e49 45 44 29 0a 20 20 20 20 20 20 20 7c 7c 20 66 6c ED). || fl
12e4a 61 67 73 3d 3d 28 53 51 4c 49 54 45 5f 53 48 4d ags==(SQLITE_SHM
12e4b 5f 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53 _LOCK | SQLITE_S
12e4c 48 4d 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20 HM_EXCLUSIVE).
12e4d 20 20 20 20 20 7c 7c 20 66 6c 61 67 73 3d 3d 28 || flags==(
12e4e 53 51 4c 49 54 45 5f 53 48 4d 5f 55 4e 4c 4f 43 SQLITE_SHM_UNLOC
12e4f 4b 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f 53 K | SQLITE_SHM_S
12e50 48 41 52 45 44 29 0a 20 20 20 20 20 20 20 7c 7c HARED). ||
12e51 20 66 6c 61 67 73 3d 3d 28 53 51 4c 49 54 45 5f flags==(SQLITE_
12e52 53 48 4d 5f 55 4e 4c 4f 43 4b 20 7c 20 53 51 4c SHM_UNLOCK | SQL
12e53 49 54 45 5f 53 48 4d 5f 45 58 43 4c 55 53 49 56 ITE_SHM_EXCLUSIV
12e54 45 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 E) );. assert(
12e55 6e 3d 3d 31 20 7c 7c 20 28 66 6c 61 67 73 20 26 n==1 || (flags &
12e56 20 53 51 4c 49 54 45 5f 53 48 4d 5f 45 58 43 4c SQLITE_SHM_EXCL
12e57 55 53 49 56 45 29 21 3d 30 20 29 3b 0a 0a 20 20 USIVE)!=0 );..
12e58 6d 61 73 6b 20 3d 20 28 75 31 36 29 28 28 31 55 mask = (u16)((1U
12e59 3c 3c 28 6f 66 73 74 2b 6e 29 29 20 2d 20 28 31 <<(ofst+n)) - (1
12e5a 55 3c 3c 6f 66 73 74 29 29 3b 0a 20 20 61 73 73 U<<ofst));. ass
12e5b 65 72 74 28 20 6e 3e 31 20 7c 7c 20 6d 61 73 6b ert( n>1 || mask
12e5c 3d 3d 28 31 3c 3c 6f 66 73 74 29 20 29 3b 0a 20 ==(1<<ofst) );.
12e5d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
12e5e 6e 74 65 72 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d nter(pShmNode->m
12e5f 75 74 65 78 29 3b 0a 20 20 69 66 28 20 66 6c 61 utex);. if( fla
12e60 67 73 20 26 20 53 51 4c 49 54 45 5f 53 48 4d 5f gs & SQLITE_SHM_
12e61 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75 31 UNLOCK ){. u1
12e62 36 20 61 6c 6c 4d 61 73 6b 20 3d 20 30 3b 20 2f 6 allMask = 0; /
12e63 2a 20 4d 61 73 6b 20 6f 66 20 6c 6f 63 6b 73 20 * Mask of locks
12e64 68 65 6c 64 20 62 79 20 73 69 62 6c 69 6e 67 73 held by siblings
12e65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 65 20 */.. /* See
12e66 69 66 20 61 6e 79 20 73 69 62 6c 69 6e 67 73 20 if any siblings
12e67 68 6f 6c 64 20 74 68 69 73 20 73 61 6d 65 20 6c hold this same l
12e68 6f 63 6b 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 ock */. for(p
12e69 58 3d 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 X=pShmNode->pFir
12e6a 73 74 3b 20 70 58 3b 20 70 58 3d 70 58 2d 3e 70 st; pX; pX=pX->p
12e6b 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 Next){. if(
12e6c 20 70 58 3d 3d 70 20 29 20 63 6f 6e 74 69 6e 75 pX==p ) continu
12e6d 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 e;. assert(
12e6e 20 28 70 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 (pX->exclMask &
12e6f 20 28 70 2d 3e 65 78 63 6c 4d 61 73 6b 7c 70 2d (p->exclMask|p-
12e70 3e 73 68 61 72 65 64 4d 61 73 6b 29 29 3d 3d 30 >sharedMask))==0
12e71 20 29 3b 0a 20 20 20 20 20 20 61 6c 6c 4d 61 73 );. allMas
12e72 6b 20 7c 3d 20 70 58 2d 3e 73 68 61 72 65 64 4d k |= pX->sharedM
12e73 61 73 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 ask;. }..
12e74 2f 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 73 79 /* Unlock the sy
12e75 73 74 65 6d 2d 6c 65 76 65 6c 20 6c 6f 63 6b 73 stem-level locks
12e76 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 6d 61 73 */. if( (mas
12e77 6b 20 26 20 61 6c 6c 4d 61 73 6b 29 3d 3d 30 20 k & allMask)==0
12e78 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 69 ){. rc = wi
12e79 6e 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b 28 70 nShmSystemLock(p
12e7a 53 68 6d 4e 6f 64 65 2c 20 5f 53 48 4d 5f 55 4e ShmNode, _SHM_UN
12e7b 4c 43 4b 2c 20 6f 66 73 74 2b 57 49 4e 5f 53 48 LCK, ofst+WIN_SH
12e7c 4d 5f 42 41 53 45 2c 20 6e 29 3b 0a 20 20 20 20 M_BASE, n);.
12e7d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
12e7e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
12e7f 20 7d 0a 0a 20 20 20 20 2f 2a 20 55 6e 64 6f 20 }.. /* Undo
12e80 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 73 20 the local locks
12e81 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 */. if( rc==S
12e82 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
12e83 20 20 70 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 3d p->exclMask &=
12e84 20 7e 6d 61 73 6b 3b 0a 20 20 20 20 20 20 70 2d ~mask;. p-
12e85 3e 73 68 61 72 65 64 4d 61 73 6b 20 26 3d 20 7e >sharedMask &= ~
12e86 6d 61 73 6b 3b 0a 20 20 20 20 7d 20 0a 20 20 7d mask;. } . }
12e87 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 else if( flags &
12e88 20 53 51 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52 SQLITE_SHM_SHAR
12e89 45 44 20 29 7b 0a 20 20 20 20 75 31 36 20 61 6c ED ){. u16 al
12e8a 6c 53 68 61 72 65 64 20 3d 20 30 3b 20 20 2f 2a lShared = 0; /*
12e8b 20 55 6e 69 6f 6e 20 6f 66 20 6c 6f 63 6b 73 20 Union of locks
12e8c 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 held by connecti
12e8d 6f 6e 73 20 6f 74 68 65 72 20 74 68 61 6e 20 22 ons other than "
12e8e 70 22 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 p" */.. /* Fi
12e8f 6e 64 20 6f 75 74 20 77 68 69 63 68 20 73 68 61 nd out which sha
12e90 72 65 64 20 6c 6f 63 6b 73 20 61 72 65 20 61 6c red locks are al
12e91 72 65 61 64 79 20 68 65 6c 64 20 62 79 20 73 69 ready held by si
12e92 62 6c 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e bling connection
12e93 73 2e 0a 20 20 20 20 2a 2a 20 49 66 20 61 6e 79 s.. ** If any
12e94 20 73 69 62 6c 69 6e 67 20 61 6c 72 65 61 64 79 sibling already
12e95 20 68 6f 6c 64 73 20 61 6e 20 65 78 63 6c 75 73 holds an exclus
12e96 69 76 65 20 6c 6f 63 6b 2c 20 67 6f 20 61 68 65 ive lock, go ahe
12e97 61 64 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 ad and return.
12e98 20 20 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 ** SQLITE_BUSY
12e99 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 .. */. for
12e9a 28 70 58 3d 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 (pX=pShmNode->pF
12e9b 69 72 73 74 3b 20 70 58 3b 20 70 58 3d 70 58 2d irst; pX; pX=pX-
12e9c 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 >pNext){. i
12e9d 66 28 20 28 70 58 2d 3e 65 78 63 6c 4d 61 73 6b f( (pX->exclMask
12e9e 20 26 20 6d 61 73 6b 29 21 3d 30 20 29 7b 0a 20 & mask)!=0 ){.
12e9f 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
12ea0 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 TE_BUSY;.
12ea1 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
12ea2 20 20 20 20 20 20 61 6c 6c 53 68 61 72 65 64 20 allShared
12ea3 7c 3d 20 70 58 2d 3e 73 68 61 72 65 64 4d 61 73 |= pX->sharedMas
12ea4 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a k;. }.. /*
12ea5 20 47 65 74 20 73 68 61 72 65 64 20 6c 6f 63 6b Get shared lock
12ea6 73 20 61 74 20 74 68 65 20 73 79 73 74 65 6d 20 s at the system
12ea7 6c 65 76 65 6c 2c 20 69 66 20 6e 65 63 65 73 73 level, if necess
12ea8 61 72 79 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 ary */. if( r
12ea9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
12eaa 20 20 20 20 20 20 69 66 28 20 28 61 6c 6c 53 68 if( (allSh
12eab 61 72 65 64 20 26 20 6d 61 73 6b 29 3d 3d 30 20 ared & mask)==0
12eac 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
12ead 77 69 6e 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b winShmSystemLock
12eae 28 70 53 68 6d 4e 6f 64 65 2c 20 5f 53 48 4d 5f (pShmNode, _SHM_
12eaf 52 44 4c 43 4b 2c 20 6f 66 73 74 2b 57 49 4e 5f RDLCK, ofst+WIN_
12eb0 53 48 4d 5f 42 41 53 45 2c 20 6e 29 3b 0a 20 20 SHM_BASE, n);.
12eb1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
12eb2 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
12eb3 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d K;. }. }
12eb4 0a 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 .. /* Get the
12eb5 20 6c 6f 63 61 6c 20 73 68 61 72 65 64 20 6c 6f local shared lo
12eb6 63 6b 73 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 cks */. if( r
12eb7 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
12eb8 20 20 20 20 20 20 70 2d 3e 73 68 61 72 65 64 4d p->sharedM
12eb9 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 ask |= mask;.
12eba 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
12ebb 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 /* Make sure no
12ebc 73 69 62 6c 69 6e 67 20 63 6f 6e 6e 65 63 74 69 sibling connecti
12ebd 6f 6e 73 20 68 6f 6c 64 20 6c 6f 63 6b 73 20 74 ons hold locks t
12ebe 68 61 74 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 74 hat will block t
12ebf 68 69 73 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 2e his. ** lock.
12ec0 20 20 49 66 20 61 6e 79 20 64 6f 2c 20 72 65 74 If any do, ret
12ec1 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 urn SQLITE_BUSY
12ec2 72 69 67 68 74 20 61 77 61 79 2e 0a 20 20 20 20 right away..
12ec3 2a 2f 0a 20 20 20 20 66 6f 72 28 70 58 3d 70 53 */. for(pX=pS
12ec4 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74 3b 20 hmNode->pFirst;
12ec5 70 58 3b 20 70 58 3d 70 58 2d 3e 70 4e 65 78 74 pX; pX=pX->pNext
12ec6 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 58 ){. if( (pX
12ec7 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 20 6d 61 73 ->exclMask & mas
12ec8 6b 29 21 3d 30 20 7c 7c 20 28 70 58 2d 3e 73 68 k)!=0 || (pX->sh
12ec9 61 72 65 64 4d 61 73 6b 20 26 20 6d 61 73 6b 29 aredMask & mask)
12eca 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 !=0 ){. r
12ecb 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
12ecc 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
12ecd 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
12ece 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 . /* Get the
12ecf 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 73 20 exclusive locks
12ed0 61 74 20 74 68 65 20 73 79 73 74 65 6d 20 6c 65 at the system le
12ed1 76 65 6c 2e 20 20 54 68 65 6e 20 69 66 20 73 75 vel. Then if su
12ed2 63 63 65 73 73 66 75 6c 0a 20 20 20 20 2a 2a 20 ccessful. **
12ed3 61 6c 73 6f 20 6d 61 72 6b 20 74 68 65 20 6c 6f also mark the lo
12ed4 63 61 6c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 cal connection a
12ed5 73 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 0a s being locked..
12ed6 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 */. if( r
12ed7 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
12ed8 20 20 20 20 20 20 72 63 20 3d 20 77 69 6e 53 68 rc = winSh
12ed9 6d 53 79 73 74 65 6d 4c 6f 63 6b 28 70 53 68 6d mSystemLock(pShm
12eda 4e 6f 64 65 2c 20 5f 53 48 4d 5f 57 52 4c 43 4b Node, _SHM_WRLCK
12edb 2c 20 6f 66 73 74 2b 57 49 4e 5f 53 48 4d 5f 42 , ofst+WIN_SHM_B
12edc 41 53 45 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 ASE, n);. i
12edd 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
12ede 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
12edf 72 74 28 20 28 70 2d 3e 73 68 61 72 65 64 4d 61 rt( (p->sharedMa
12ee0 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 3b sk & mask)==0 );
12ee1 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 78 63 6c . p->excl
12ee2 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 Mask |= mask;.
12ee3 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
12ee4 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
12ee5 6c 65 61 76 65 28 70 53 68 6d 4e 6f 64 65 2d 3e leave(pShmNode->
12ee6 6d 75 74 65 78 29 3b 0a 20 20 4f 53 54 52 41 43 mutex);. OSTRAC
12ee7 45 28 28 22 53 48 4d 2d 4c 4f 43 4b 20 73 68 6d E(("SHM-LOCK shm
12ee8 69 64 2d 25 64 2c 20 70 69 64 2d 25 64 20 67 6f id-%d, pid-%d go
12ee9 74 20 25 30 33 78 2c 25 30 33 78 20 25 73 5c 6e t %03x,%03x %s\n
12eea 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d ",. p-
12eeb 3e 69 64 2c 20 28 69 6e 74 29 6f 73 47 65 74 43 >id, (int)osGetC
12eec 75 72 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 urrentProcessId(
12eed 29 2c 20 70 2d 3e 73 68 61 72 65 64 4d 61 73 6b ), p->sharedMask
12eee 2c 20 70 2d 3e 65 78 63 6c 4d 61 73 6b 2c 0a 20 , p->exclMask,.
12eef 20 20 20 20 20 20 20 20 20 20 72 63 20 3f 20 22 rc ? "
12ef0 66 61 69 6c 65 64 22 20 3a 20 22 6f 6b 22 29 29 failed" : "ok"))
12ef1 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
12ef2 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
12ef3 74 20 61 20 6d 65 6d 6f 72 79 20 62 61 72 72 69 t a memory barri
12ef4 65 72 20 6f 72 20 6d 65 6d 6f 72 79 20 66 65 6e er or memory fen
12ef5 63 65 20 6f 6e 20 73 68 61 72 65 64 20 6d 65 6d ce on shared mem
12ef6 6f 72 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 6c 6c ory. .**.** All
12ef7 20 6c 6f 61 64 73 20 61 6e 64 20 73 74 6f 72 65 loads and store
12ef8 73 20 62 65 67 75 6e 20 62 65 66 6f 72 65 20 74 s begun before t
12ef9 68 65 20 62 61 72 72 69 65 72 20 6d 75 73 74 20 he barrier must
12efa 63 6f 6d 70 6c 65 74 65 20 62 65 66 6f 72 65 0a complete before.
12efb 2a 2a 20 61 6e 79 20 6c 6f 61 64 20 6f 72 20 73 ** any load or s
12efc 74 6f 72 65 20 62 65 67 75 6e 20 61 66 74 65 72 tore begun after
12efd 20 74 68 65 20 62 61 72 72 69 65 72 2e 0a 2a 2f the barrier..*/
12efe 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e .static void win
12eff 53 68 6d 42 61 72 72 69 65 72 28 0a 20 20 73 71 ShmBarrier(. sq
12f00 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 20 20 lite3_file *fd
12f01 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
12f02 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 ase holding the
12f03 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 2a 2f shared memory */
12f04 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 .){. UNUSED_PAR
12f05 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20 2f 2a AMETER(fd);. /*
12f06 20 4d 65 6d 6f 72 79 42 61 72 72 69 65 72 28 29 MemoryBarrier()
12f07 3b 20 2f 2f 20 64 6f 65 73 20 6e 6f 74 20 77 6f ; // does not wo
12f08 72 6b 20 2d 2d 20 64 6f 20 6e 6f 74 20 6b 6e 6f rk -- do not kno
12f09 77 20 77 68 79 20 6e 6f 74 20 2a 2f 0a 20 20 77 w why not */. w
12f0a 69 6e 53 68 6d 45 6e 74 65 72 4d 75 74 65 78 28 inShmEnterMutex(
12f0b 29 3b 0a 20 20 77 69 6e 53 68 6d 4c 65 61 76 65 );. winShmLeave
12f0c 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a Mutex();.}../*.*
12f0d 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
12f0e 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 6f 62 74 is called to obt
12f0f 61 69 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ain a pointer to
12f10 20 72 65 67 69 6f 6e 20 69 52 65 67 69 6f 6e 20 region iRegion
12f11 6f 66 20 74 68 65 20 0a 2a 2a 20 73 68 61 72 65 of the .** share
12f12 64 2d 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 d-memory associa
12f13 74 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 ted with the dat
12f14 61 62 61 73 65 20 66 69 6c 65 20 66 64 2e 20 53 abase file fd. S
12f15 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 hared-memory reg
12f16 69 6f 6e 73 20 0a 2a 2a 20 61 72 65 20 6e 75 6d ions .** are num
12f17 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20 66 bered starting f
12f18 72 6f 6d 20 7a 65 72 6f 2e 20 45 61 63 68 20 73 rom zero. Each s
12f19 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 hared-memory reg
12f1a 69 6f 6e 20 69 73 20 73 7a 52 65 67 69 6f 6e 20 ion is szRegion
12f1b 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a .** bytes in siz
12f1c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 e..**.** If an e
12f1d 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 rror occurs, an
12f1e 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
12f1f 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 20 69 turned and *pp i
12f20 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a s set to NULL..*
12f21 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 *.** Otherwise,
12f22 69 66 20 74 68 65 20 69 73 57 72 69 74 65 20 70 if the isWrite p
12f23 61 72 61 6d 65 74 65 72 20 69 73 20 30 20 61 6e arameter is 0 an
12f24 64 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 d the requested
12f25 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 0a 2a 2a shared-memory.**
12f26 20 72 65 67 69 6f 6e 20 68 61 73 20 6e 6f 74 20 region has not
12f27 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 28 been allocated (
12f28 62 79 20 61 6e 79 20 63 6c 69 65 6e 74 2c 20 69 by any client, i
12f29 6e 63 6c 75 64 69 6e 67 20 6f 6e 65 20 72 75 6e ncluding one run
12f2a 6e 69 6e 67 20 69 6e 20 61 0a 2a 2a 20 73 65 70 ning in a.** sep
12f2b 61 72 61 74 65 20 70 72 6f 63 65 73 73 29 2c 20 arate process),
12f2c 74 68 65 6e 20 2a 70 70 20 69 73 20 73 65 74 20 then *pp is set
12f2d 74 6f 20 4e 55 4c 4c 20 61 6e 64 20 53 51 4c 49 to NULL and SQLI
12f2e 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 TE_OK returned.
12f2f 49 66 20 0a 2a 2a 20 69 73 57 72 69 74 65 20 69 If .** isWrite i
12f30 73 20 6e 6f 6e 2d 7a 65 72 6f 20 61 6e 64 20 74 s non-zero and t
12f31 68 65 20 72 65 71 75 65 73 74 65 64 20 73 68 61 he requested sha
12f32 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f red-memory regio
12f33 6e 20 68 61 73 20 6e 6f 74 20 79 65 74 20 0a 2a n has not yet .*
12f34 2a 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 * been allocated
12f35 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 , it is allocate
12f36 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 d by this functi
12f37 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 on..**.** If the
12f38 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 72 shared-memory r
12f39 65 67 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 egion has alread
12f3a 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 y been allocated
12f3b 20 6f 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 or is allocated
12f3c 20 62 79 0a 2a 2a 20 74 68 69 73 20 63 61 6c 6c by.** this call
12f3d 20 61 73 20 64 65 73 63 72 69 62 65 64 20 61 62 as described ab
12f3e 6f 76 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20 ove, then it is
12f3f 6d 61 70 70 65 64 20 69 6e 74 6f 20 74 68 69 73 mapped into this
12f40 20 70 72 6f 63 65 73 73 65 73 20 0a 2a 2a 20 61 processes .** a
12f41 64 64 72 65 73 73 20 73 70 61 63 65 20 28 69 66 ddress space (if
12f42 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 it is not alrea
12f43 64 79 29 2c 20 2a 70 70 20 69 73 20 73 65 74 20 dy), *pp is set
12f44 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 to point to the
12f45 6d 61 70 70 65 64 20 0a 2a 2a 20 6d 65 6d 6f 72 mapped .** memor
12f46 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 y and SQLITE_OK
12f47 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 returned..*/.sta
12f48 74 69 63 20 69 6e 74 20 77 69 6e 53 68 6d 4d 61 tic int winShmMa
12f49 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c p(. sqlite3_fil
12f4a 65 20 2a 66 64 2c 20 20 20 20 20 20 20 20 20 20 e *fd,
12f4b 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 6f /* Handle o
12f4c 70 65 6e 20 6f 6e 20 64 61 74 61 62 61 73 65 20 pen on database
12f4d 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 file */. int iR
12f4e 65 67 69 6f 6e 2c 20 20 20 20 20 20 20 20 20 20 egion,
12f4f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
12f50 69 6f 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 ion to retrieve
12f51 2a 2f 0a 20 20 69 6e 74 20 73 7a 52 65 67 69 6f */. int szRegio
12f52 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n,
12f53 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
12f54 72 65 67 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 regions */. int
12f55 20 69 73 57 72 69 74 65 2c 20 20 20 20 20 20 20 isWrite,
12f56 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12f57 54 72 75 65 20 74 6f 20 65 78 74 65 6e 64 20 66 True to extend f
12f58 69 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 ile if necessary
12f59 20 2a 2f 0a 20 20 76 6f 69 64 20 76 6f 6c 61 74 */. void volat
12f5a 69 6c 65 20 2a 2a 70 70 20 20 20 20 20 20 20 20 ile **pp
12f5b 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 /* OUT: Ma
12f5c 70 70 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 29 pped memory */.)
12f5d 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 44 62 {. winFile *pDb
12f5e 46 64 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 66 Fd = (winFile*)f
12f5f 64 3b 0a 20 20 77 69 6e 53 68 6d 20 2a 70 20 3d d;. winShm *p =
12f60 20 70 44 62 46 64 2d 3e 70 53 68 6d 3b 0a 20 20 pDbFd->pShm;.
12f61 77 69 6e 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d winShmNode *pShm
12f62 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 72 63 20 3d Node;. int rc =
12f63 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 SQLITE_OK;.. i
12f64 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 63 20 f( !p ){. rc
12f65 3d 20 77 69 6e 4f 70 65 6e 53 68 61 72 65 64 4d = winOpenSharedM
12f66 65 6d 6f 72 79 28 70 44 62 46 64 29 3b 0a 20 20 emory(pDbFd);.
12f67 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
12f68 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b _OK ) return rc;
12f69 0a 20 20 20 20 70 20 3d 20 70 44 62 46 64 2d 3e . p = pDbFd->
12f6a 70 53 68 6d 3b 0a 20 20 7d 0a 20 20 70 53 68 6d pShm;. }. pShm
12f6b 4e 6f 64 65 20 3d 20 70 2d 3e 70 53 68 6d 4e 6f Node = p->pShmNo
12f6c 64 65 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d de;.. sqlite3_m
12f6d 75 74 65 78 5f 65 6e 74 65 72 28 70 53 68 6d 4e utex_enter(pShmN
12f6e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 ode->mutex);. a
12f6f 73 73 65 72 74 28 20 73 7a 52 65 67 69 6f 6e 3d ssert( szRegion=
12f70 3d 70 53 68 6d 4e 6f 64 65 2d 3e 73 7a 52 65 67 =pShmNode->szReg
12f71 69 6f 6e 20 7c 7c 20 70 53 68 6d 4e 6f 64 65 2d ion || pShmNode-
12f72 3e 6e 52 65 67 69 6f 6e 3d 3d 30 20 29 3b 0a 0a >nRegion==0 );..
12f73 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e if( pShmNode->
12f74 6e 52 65 67 69 6f 6e 3c 3d 69 52 65 67 69 6f 6e nRegion<=iRegion
12f75 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 ){. struct S
12f76 68 6d 52 65 67 69 6f 6e 20 2a 61 70 4e 65 77 3b hmRegion *apNew;
12f77 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 /* Ne
12f78 77 20 61 52 65 67 69 6f 6e 5b 5d 20 61 72 72 61 w aRegion[] arra
12f79 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 42 79 y */. int nBy
12f7a 74 65 20 3d 20 28 69 52 65 67 69 6f 6e 2b 31 29 te = (iRegion+1)
12f7b 2a 73 7a 52 65 67 69 6f 6e 3b 20 20 2f 2a 20 4d *szRegion; /* M
12f7c 69 6e 69 6d 75 6d 20 72 65 71 75 69 72 65 64 20 inimum required
12f7d 66 69 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 file size */.
12f7e 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
12f7f 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 z;
12f80 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 /* Current s
12f81 69 7a 65 20 6f 66 20 77 61 6c 2d 69 6e 64 65 78 ize of wal-index
12f82 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 20 20 70 53 file */.. pS
12f83 68 6d 4e 6f 64 65 2d 3e 73 7a 52 65 67 69 6f 6e hmNode->szRegion
12f84 20 3d 20 73 7a 52 65 67 69 6f 6e 3b 0a 0a 20 20 = szRegion;..
12f85 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 /* The request
12f86 65 64 20 72 65 67 69 6f 6e 20 69 73 20 6e 6f 74 ed region is not
12f87 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 74 68 69 mapped into thi
12f88 73 20 70 72 6f 63 65 73 73 65 73 20 61 64 64 72 s processes addr
12f89 65 73 73 20 73 70 61 63 65 2e 0a 20 20 20 20 2a ess space.. *
12f8a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 * Check to see i
12f8b 66 20 69 74 20 68 61 73 20 62 65 65 6e 20 61 6c f it has been al
12f8c 6c 6f 63 61 74 65 64 20 28 69 2e 65 2e 20 69 66 located (i.e. if
12f8d 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 66 the wal-index f
12f8e 69 6c 65 20 69 73 0a 20 20 20 20 2a 2a 20 6c 61 ile is. ** la
12f8f 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 63 6f rge enough to co
12f90 6e 74 61 69 6e 20 74 68 65 20 72 65 71 75 65 73 ntain the reques
12f91 74 65 64 20 72 65 67 69 6f 6e 29 2e 0a 20 20 20 ted region)..
12f92 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77 69 6e */. rc = win
12f93 46 69 6c 65 53 69 7a 65 28 28 73 71 6c 69 74 65 FileSize((sqlite
12f94 33 5f 66 69 6c 65 20 2a 29 26 70 53 68 6d 4e 6f 3_file *)&pShmNo
12f95 64 65 2d 3e 68 46 69 6c 65 2c 20 26 73 7a 29 3b de->hFile, &sz);
12f96 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
12f97 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
12f98 72 63 20 3d 20 77 69 6e 4c 6f 67 45 72 72 6f 72 rc = winLogError
12f99 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 (SQLITE_IOERR_SH
12f9a 4d 53 49 5a 45 2c 20 6f 73 47 65 74 4c 61 73 74 MSIZE, osGetLast
12f9b 45 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20 Error(),.
12f9c 20 20 20 20 20 20 20 20 22 77 69 6e 53 68 6d 4d "winShmM
12f9d 61 70 31 22 2c 20 70 44 62 46 64 2d 3e 7a 50 61 ap1", pDbFd->zPa
12f9e 74 68 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 th);. goto
12f9f 73 68 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 shmpage_out;.
12fa0 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 7a 3c 6e }.. if( sz<n
12fa1 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a Byte ){. /*
12fa2 20 54 68 65 20 72 65 71 75 65 73 74 65 64 20 6d The requested m
12fa3 65 6d 6f 72 79 20 72 65 67 69 6f 6e 20 64 6f 65 emory region doe
12fa4 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 49 66 20 s not exist. If
12fa5 69 73 57 72 69 74 65 20 69 73 20 73 65 74 20 74 isWrite is set t
12fa6 6f 0a 20 20 20 20 20 20 2a 2a 20 7a 65 72 6f 2c o. ** zero,
12fa7 20 65 78 69 74 20 65 61 72 6c 79 2e 20 2a 70 70 exit early. *pp
12fa8 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 will be set to
12fa9 4e 55 4c 4c 20 61 6e 64 20 53 51 4c 49 54 45 5f NULL and SQLITE_
12faa 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 20 20 20 OK returned..
12fab 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 **. ** A
12fac 6c 74 65 72 6e 61 74 69 76 65 6c 79 2c 20 69 66 lternatively, if
12fad 20 69 73 57 72 69 74 65 20 69 73 20 6e 6f 6e 2d isWrite is non-
12fae 7a 65 72 6f 2c 20 75 73 65 20 66 74 72 75 6e 63 zero, use ftrunc
12faf 61 74 65 28 29 20 74 6f 20 61 6c 6c 6f 63 61 74 ate() to allocat
12fb0 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 e. ** the r
12fb1 65 71 75 65 73 74 65 64 20 6d 65 6d 6f 72 79 20 equested memory
12fb2 72 65 67 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f region.. */
12fb3 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 57 72 . if( !isWr
12fb4 69 74 65 20 29 20 67 6f 74 6f 20 73 68 6d 70 61 ite ) goto shmpa
12fb5 67 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 72 63 ge_out;. rc
12fb6 20 3d 20 77 69 6e 54 72 75 6e 63 61 74 65 28 28 = winTruncate((
12fb7 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 sqlite3_file *)&
12fb8 70 53 68 6d 4e 6f 64 65 2d 3e 68 46 69 6c 65 2c pShmNode->hFile,
12fb9 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 69 nByte);. i
12fba 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
12fbb 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
12fbc 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c winLogError(SQL
12fbd 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d 53 49 5a ITE_IOERR_SHMSIZ
12fbe 45 2c 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f E, osGetLastErro
12fbf 72 28 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 r(),.
12fc0 20 20 20 20 20 20 22 77 69 6e 53 68 6d 4d 61 70 "winShmMap
12fc1 32 22 2c 20 70 44 62 46 64 2d 3e 7a 50 61 74 68 2", pDbFd->zPath
12fc2 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 );. goto
12fc3 73 68 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 shmpage_out;.
12fc4 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
12fc5 2f 2a 20 4d 61 70 20 74 68 65 20 72 65 71 75 65 /* Map the reque
12fc6 73 74 65 64 20 6d 65 6d 6f 72 79 20 72 65 67 69 sted memory regi
12fc7 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 70 72 6f on into this pro
12fc8 63 65 73 73 65 73 20 61 64 64 72 65 73 73 20 73 cesses address s
12fc9 70 61 63 65 2e 20 2a 2f 0a 20 20 20 20 61 70 4e pace. */. apN
12fca 65 77 20 3d 20 28 73 74 72 75 63 74 20 53 68 6d ew = (struct Shm
12fcb 52 65 67 69 6f 6e 20 2a 29 73 71 6c 69 74 65 33 Region *)sqlite3
12fcc 5f 72 65 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 _realloc(.
12fcd 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 61 52 65 67 pShmNode->aReg
12fce 69 6f 6e 2c 20 28 69 52 65 67 69 6f 6e 2b 31 29 ion, (iRegion+1)
12fcf 2a 73 69 7a 65 6f 66 28 61 70 4e 65 77 5b 30 5d *sizeof(apNew[0]
12fd0 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 ). );. if(
12fd1 20 21 61 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 !apNew ){.
12fd2 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rc = SQLITE_IOE
12fd3 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 RR_NOMEM;.
12fd4 67 6f 74 6f 20 73 68 6d 70 61 67 65 5f 6f 75 74 goto shmpage_out
12fd5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 68 6d ;. }. pShm
12fd6 4e 6f 64 65 2d 3e 61 52 65 67 69 6f 6e 20 3d 20 Node->aRegion =
12fd7 61 70 4e 65 77 3b 0a 0a 20 20 20 20 77 68 69 6c apNew;.. whil
12fd8 65 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 e( pShmNode->nRe
12fd9 67 69 6f 6e 3c 3d 69 52 65 67 69 6f 6e 20 29 7b gion<=iRegion ){
12fda 0a 20 20 20 20 20 20 48 41 4e 44 4c 45 20 68 4d . HANDLE hM
12fdb 61 70 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 ap = NULL;
12fdc 20 20 20 2f 2a 20 66 69 6c 65 2d 6d 61 70 70 69 /* file-mappi
12fdd 6e 67 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 20 ng handle */.
12fde 20 20 20 76 6f 69 64 20 2a 70 4d 61 70 20 3d 20 void *pMap =
12fdf 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
12fe0 2a 20 4d 61 70 70 65 64 20 6d 65 6d 6f 72 79 20 * Mapped memory
12fe1 72 65 67 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 0a region */. .
12fe2 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
12fe3 4e 52 54 0a 20 20 20 20 20 20 68 4d 61 70 20 3d NRT. hMap =
12fe4 20 6f 73 43 72 65 61 74 65 46 69 6c 65 4d 61 70 osCreateFileMap
12fe5 70 69 6e 67 46 72 6f 6d 41 70 70 28 70 53 68 6d pingFromApp(pShm
12fe6 4e 6f 64 65 2d 3e 68 46 69 6c 65 2e 68 2c 0a 20 Node->hFile.h,.
12fe7 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 20 50 NULL, P
12fe8 41 47 45 5f 52 45 41 44 57 52 49 54 45 2c 20 6e AGE_READWRITE, n
12fe9 42 79 74 65 2c 20 4e 55 4c 4c 0a 20 20 20 20 20 Byte, NULL.
12fea 20 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 );.#elif define
12feb 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 d(SQLITE_WIN32_H
12fec 41 53 5f 57 49 44 45 29 0a 20 20 20 20 20 20 68 AS_WIDE). h
12fed 4d 61 70 20 3d 20 6f 73 43 72 65 61 74 65 46 69 Map = osCreateFi
12fee 6c 65 4d 61 70 70 69 6e 67 57 28 70 53 68 6d 4e leMappingW(pShmN
12fef 6f 64 65 2d 3e 68 46 69 6c 65 2e 68 2c 20 0a 20 ode->hFile.h, .
12ff0 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 20 50 NULL, P
12ff1 41 47 45 5f 52 45 41 44 57 52 49 54 45 2c 20 30 AGE_READWRITE, 0
12ff2 2c 20 6e 42 79 74 65 2c 20 4e 55 4c 4c 0a 20 20 , nByte, NULL.
12ff3 20 20 20 20 29 3b 0a 23 65 6c 69 66 20 64 65 66 );.#elif def
12ff4 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 ined(SQLITE_WIN3
12ff5 32 5f 48 41 53 5f 41 4e 53 49 29 0a 20 20 20 20 2_HAS_ANSI).
12ff6 20 20 68 4d 61 70 20 3d 20 6f 73 43 72 65 61 74 hMap = osCreat
12ff7 65 46 69 6c 65 4d 61 70 70 69 6e 67 41 28 70 53 eFileMappingA(pS
12ff8 68 6d 4e 6f 64 65 2d 3e 68 46 69 6c 65 2e 68 2c hmNode->hFile.h,
12ff9 20 0a 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c . NULL
12ffa 2c 20 50 41 47 45 5f 52 45 41 44 57 52 49 54 45 , PAGE_READWRITE
12ffb 2c 20 30 2c 20 6e 42 79 74 65 2c 20 4e 55 4c 4c , 0, nByte, NULL
12ffc 0a 20 20 20 20 20 20 29 3b 0a 23 65 6e 64 69 66 . );.#endif
12ffd 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 28 28 . OSTRACE((
12ffe 22 53 48 4d 2d 4d 41 50 20 70 69 64 2d 25 64 20 "SHM-MAP pid-%d
12fff 63 72 65 61 74 65 20 72 65 67 69 6f 6e 3d 25 64 create region=%d
13000 20 6e 62 79 74 65 3d 25 64 20 25 73 5c 6e 22 2c nbyte=%d %s\n",
13001 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
13002 28 69 6e 74 29 6f 73 47 65 74 43 75 72 72 65 6e (int)osGetCurren
13003 74 50 72 6f 63 65 73 73 49 64 28 29 2c 20 70 53 tProcessId(), pS
13004 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f 6e 2c hmNode->nRegion,
13005 20 6e 42 79 74 65 2c 0a 20 20 20 20 20 20 20 20 nByte,.
13006 20 20 20 20 20 20 20 68 4d 61 70 20 3f 20 22 6f hMap ? "o
13007 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b k" : "failed"));
13008 0a 20 20 20 20 20 20 69 66 28 20 68 4d 61 70 20 . if( hMap
13009 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 ){. int i
1300a 4f 66 66 73 65 74 20 3d 20 70 53 68 6d 4e 6f 64 Offset = pShmNod
1300b 65 2d 3e 6e 52 65 67 69 6f 6e 2a 73 7a 52 65 67 e->nRegion*szReg
1300c 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 ion;. int
1300d 20 69 4f 66 66 73 65 74 53 68 69 66 74 20 3d 20 iOffsetShift =
1300e 69 4f 66 66 73 65 74 20 25 20 77 69 6e 53 79 73 iOffset % winSys
1300f 49 6e 66 6f 2e 64 77 41 6c 6c 6f 63 61 74 69 6f Info.dwAllocatio
13010 6e 47 72 61 6e 75 6c 61 72 69 74 79 3b 0a 23 69 nGranularity;.#i
13011 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 f SQLITE_OS_WINR
13012 54 0a 20 20 20 20 20 20 20 20 70 4d 61 70 20 3d T. pMap =
13013 20 6f 73 4d 61 70 56 69 65 77 4f 66 46 69 6c 65 osMapViewOfFile
13014 46 72 6f 6d 41 70 70 28 68 4d 61 70 2c 20 46 49 FromApp(hMap, FI
13015 4c 45 5f 4d 41 50 5f 57 52 49 54 45 20 7c 20 46 LE_MAP_WRITE | F
13016 49 4c 45 5f 4d 41 50 5f 52 45 41 44 2c 0a 20 20 ILE_MAP_READ,.
13017 20 20 20 20 20 20 20 20 20 20 69 4f 66 66 73 65 iOffse
13018 74 20 2d 20 69 4f 66 66 73 65 74 53 68 69 66 74 t - iOffsetShift
13019 2c 20 73 7a 52 65 67 69 6f 6e 20 2b 20 69 4f 66 , szRegion + iOf
1301a 66 73 65 74 53 68 69 66 74 0a 20 20 20 20 20 20 fsetShift.
1301b 20 20 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 );.#else.
1301c 20 20 20 70 4d 61 70 20 3d 20 6f 73 4d 61 70 56 pMap = osMapV
1301d 69 65 77 4f 66 46 69 6c 65 28 68 4d 61 70 2c 20 iewOfFile(hMap,
1301e 46 49 4c 45 5f 4d 41 50 5f 57 52 49 54 45 20 7c FILE_MAP_WRITE |
1301f 20 46 49 4c 45 5f 4d 41 50 5f 52 45 41 44 2c 0a FILE_MAP_READ,.
13020 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 69 0, i
13021 4f 66 66 73 65 74 20 2d 20 69 4f 66 66 73 65 74 Offset - iOffset
13022 53 68 69 66 74 2c 20 73 7a 52 65 67 69 6f 6e 20 Shift, szRegion
13023 2b 20 69 4f 66 66 73 65 74 53 68 69 66 74 0a 20 + iOffsetShift.
13024 20 20 20 20 20 20 20 29 3b 0a 23 65 6e 64 69 66 );.#endif
13025 0a 20 20 20 20 20 20 20 20 4f 53 54 52 41 43 45 . OSTRACE
13026 28 28 22 53 48 4d 2d 4d 41 50 20 70 69 64 2d 25 (("SHM-MAP pid-%
13027 64 20 6d 61 70 20 72 65 67 69 6f 6e 3d 25 64 20 d map region=%d
13028 6f 66 66 73 65 74 3d 25 64 20 73 69 7a 65 3d 25 offset=%d size=%
13029 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 d %s\n",.
1302a 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29 6f (int)o
1302b 73 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63 65 sGetCurrentProce
1302c 73 73 49 64 28 29 2c 20 70 53 68 6d 4e 6f 64 65 ssId(), pShmNode
1302d 2d 3e 6e 52 65 67 69 6f 6e 2c 20 69 4f 66 66 73 ->nRegion, iOffs
1302e 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 et,.
1302f 20 20 20 20 20 73 7a 52 65 67 69 6f 6e 2c 20 70 szRegion, p
13030 4d 61 70 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 Map ? "ok" : "fa
13031 69 6c 65 64 22 29 29 3b 0a 20 20 20 20 20 20 7d iled"));. }
13032 0a 20 20 20 20 20 20 69 66 28 20 21 70 4d 61 70 . if( !pMap
13033 20 29 7b 0a 20 20 20 20 20 20 20 20 70 53 68 6d ){. pShm
13034 4e 6f 64 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 Node->lastErrno
13035 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 = osGetLastError
13036 28 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ();. rc =
13037 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c winLogError(SQL
13038 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d 4d 41 50 ITE_IOERR_SHMMAP
13039 2c 20 70 53 68 6d 4e 6f 64 65 2d 3e 6c 61 73 74 , pShmNode->last
1303a 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 Errno,.
1303b 20 20 20 20 20 20 20 20 22 77 69 6e 53 68 6d 4d "winShmM
1303c 61 70 33 22 2c 20 70 44 62 46 64 2d 3e 7a 50 61 ap3", pDbFd->zPa
1303d 74 68 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 th);. if(
1303e 20 68 4d 61 70 20 29 20 6f 73 43 6c 6f 73 65 48 hMap ) osCloseH
1303f 61 6e 64 6c 65 28 68 4d 61 70 29 3b 0a 20 20 20 andle(hMap);.
13040 20 20 20 20 20 67 6f 74 6f 20 73 68 6d 70 61 67 goto shmpag
13041 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 0a e_out;. }..
13042 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e pShmNode->
13043 61 52 65 67 69 6f 6e 5b 70 53 68 6d 4e 6f 64 65 aRegion[pShmNode
13044 2d 3e 6e 52 65 67 69 6f 6e 5d 2e 70 4d 61 70 20 ->nRegion].pMap
13045 3d 20 70 4d 61 70 3b 0a 20 20 20 20 20 20 70 53 = pMap;. pS
13046 68 6d 4e 6f 64 65 2d 3e 61 52 65 67 69 6f 6e 5b hmNode->aRegion[
13047 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f pShmNode->nRegio
13048 6e 5d 2e 68 4d 61 70 20 3d 20 68 4d 61 70 3b 0a n].hMap = hMap;.
13049 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e pShmNode->
1304a 6e 52 65 67 69 6f 6e 2b 2b 3b 0a 20 20 20 20 7d nRegion++;. }
1304b 0a 20 20 7d 0a 0a 73 68 6d 70 61 67 65 5f 6f 75 . }..shmpage_ou
1304c 74 3a 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 t:. if( pShmNod
1304d 65 2d 3e 6e 52 65 67 69 6f 6e 3e 69 52 65 67 69 e->nRegion>iRegi
1304e 6f 6e 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 4f on ){. int iO
1304f 66 66 73 65 74 20 3d 20 69 52 65 67 69 6f 6e 2a ffset = iRegion*
13050 73 7a 52 65 67 69 6f 6e 3b 0a 20 20 20 20 69 6e szRegion;. in
13051 74 20 69 4f 66 66 73 65 74 53 68 69 66 74 20 3d t iOffsetShift =
13052 20 69 4f 66 66 73 65 74 20 25 20 77 69 6e 53 79 iOffset % winSy
13053 73 49 6e 66 6f 2e 64 77 41 6c 6c 6f 63 61 74 69 sInfo.dwAllocati
13054 6f 6e 47 72 61 6e 75 6c 61 72 69 74 79 3b 0a 20 onGranularity;.
13055 20 20 20 63 68 61 72 20 2a 70 20 3d 20 28 63 68 char *p = (ch
13056 61 72 20 2a 29 70 53 68 6d 4e 6f 64 65 2d 3e 61 ar *)pShmNode->a
13057 52 65 67 69 6f 6e 5b 69 52 65 67 69 6f 6e 5d 2e Region[iRegion].
13058 70 4d 61 70 3b 0a 20 20 20 20 2a 70 70 20 3d 20 pMap;. *pp =
13059 28 76 6f 69 64 20 2a 29 26 70 5b 69 4f 66 66 73 (void *)&p[iOffs
1305a 65 74 53 68 69 66 74 5d 3b 0a 20 20 7d 65 6c 73 etShift];. }els
1305b 65 7b 0a 20 20 20 20 2a 70 70 20 3d 20 30 3b 0a e{. *pp = 0;.
1305c 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 }. sqlite3_mu
1305d 74 65 78 5f 6c 65 61 76 65 28 70 53 68 6d 4e 6f tex_leave(pShmNo
1305e 64 65 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 de->mutex);. re
1305f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6c 73 turn rc;.}..#els
13060 65 0a 23 20 64 65 66 69 6e 65 20 77 69 6e 53 68 e.# define winSh
13061 6d 4d 61 70 20 20 20 20 20 30 0a 23 20 64 65 66 mMap 0.# def
13062 69 6e 65 20 77 69 6e 53 68 6d 4c 6f 63 6b 20 20 ine winShmLock
13063 20 20 30 0a 23 20 64 65 66 69 6e 65 20 77 69 6e 0.# define win
13064 53 68 6d 42 61 72 72 69 65 72 20 30 0a 23 20 64 ShmBarrier 0.# d
13065 65 66 69 6e 65 20 77 69 6e 53 68 6d 55 6e 6d 61 efine winShmUnma
13066 70 20 20 20 30 0a 23 65 6e 64 69 66 20 2f 2a 20 p 0.#endif /*
13067 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
13068 4d 49 54 5f 57 41 4c 20 2a 2f 0a 0a 2f 2a 0a 2a MIT_WAL */../*.*
13069 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 * Here ends the
1306a 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
1306b 66 20 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 69 f all sqlite3_fi
1306c 6c 65 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a le methods..**.*
1306d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1306e 2a 2a 2a 2a 2a 20 45 6e 64 20 73 71 6c 69 74 65 ***** End sqlite
1306f 33 5f 66 69 6c 65 20 4d 65 74 68 6f 64 73 20 2a 3_file Methods *
13070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13071 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
13072 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13073 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13074 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13075 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13076 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
13077 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 6f /*.** This vecto
13078 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 r defines all th
13079 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63 e methods that c
1307a 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e an operate on an
1307b 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 .** sqlite3_file
1307c 20 66 6f 72 20 77 69 6e 33 32 2e 0a 2a 2f 0a 73 for win32..*/.s
1307d 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
1307e 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 77 te3_io_methods w
1307f 69 6e 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 inIoMethod = {.
13080 20 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2,
13081 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13082 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a /* iVersion */.
13083 20 20 77 69 6e 43 6c 6f 73 65 2c 20 20 20 20 20 winClose,
13084 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13085 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 /* xClose */.
13086 20 77 69 6e 52 65 61 64 2c 20 20 20 20 20 20 20 winRead,
13087 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13088 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 77 /* xRead */. w
13089 69 6e 57 72 69 74 65 2c 20 20 20 20 20 20 20 20 inWrite,
1308a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1308b 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 20 77 69 * xWrite */. wi
1308c 6e 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 nTruncate,
1308d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1308e 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20 xTruncate */.
1308f 77 69 6e 53 79 6e 63 2c 20 20 20 20 20 20 20 20 winSync,
13090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13091 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 77 69 /* xSync */. wi
13092 6e 46 69 6c 65 53 69 7a 65 2c 20 20 20 20 20 20 nFileSize,
13093 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
13094 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20 xFileSize */.
13095 77 69 6e 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 winLock,
13096 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13097 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 77 69 /* xLock */. wi
13098 6e 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 nUnlock,
13099 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1309a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 77 69 xUnlock */. wi
1309b 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f nCheckReservedLo
1309c 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ck, /*
1309d 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
1309e 6f 63 6b 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 ock */. winFile
1309f 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 20 20 20 20 Control,
130a0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c /* xFil
130a1 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 77 69 eControl */. wi
130a2 6e 53 65 63 74 6f 72 53 69 7a 65 2c 20 20 20 20 nSectorSize,
130a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
130a4 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 0a xSectorSize */.
130a5 20 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 winDeviceChara
130a6 63 74 65 72 69 73 74 69 63 73 2c 20 20 20 20 20 cteristics,
130a7 20 20 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72 /* xDeviceChar
130a8 61 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 acteristics */.
130a9 20 77 69 6e 53 68 6d 4d 61 70 2c 20 20 20 20 20 winShmMap,
130aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
130ab 20 2f 2a 20 78 53 68 6d 4d 61 70 20 2a 2f 0a 20 /* xShmMap */.
130ac 20 77 69 6e 53 68 6d 4c 6f 63 6b 2c 20 20 20 20 winShmLock,
130ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
130ae 20 2f 2a 20 78 53 68 6d 4c 6f 63 6b 20 2a 2f 0a /* xShmLock */.
130af 20 20 77 69 6e 53 68 6d 42 61 72 72 69 65 72 2c winShmBarrier,
130b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
130b1 20 20 2f 2a 20 78 53 68 6d 42 61 72 72 69 65 72 /* xShmBarrier
130b2 20 2a 2f 0a 20 20 77 69 6e 53 68 6d 55 6e 6d 61 */. winShmUnma
130b3 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p
130b4 20 20 20 20 20 20 2f 2a 20 78 53 68 6d 55 6e 6d /* xShmUnm
130b5 61 70 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a ap */.};../*****
130b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
130b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
130b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
130b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
130ba 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
130bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
130bc 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66 73 **** sqlite3_vfs
130bd 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a methods *******
130be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
130bf 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
130c0 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 division contai
130c1 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 ns the implement
130c2 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f 64 73 ation of methods
130c3 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 on the.** sqlit
130c4 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a 2a e3_vfs object..*
130c5 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 /../*.** Convert
130c6 20 61 20 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d a UTF-8 filenam
130c7 65 20 69 6e 74 6f 20 77 68 61 74 65 76 65 72 20 e into whatever
130c8 66 6f 72 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 form the underly
130c9 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 ing.** operating
130ca 20 73 79 73 74 65 6d 20 77 61 6e 74 73 20 66 69 system wants fi
130cb 6c 65 6e 61 6d 65 73 20 69 6e 2e 20 20 53 70 61 lenames in. Spa
130cc 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ce to hold the r
130cd 65 73 75 6c 74 0a 2a 2a 20 69 73 20 6f 62 74 61 esult.** is obta
130ce 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 ined from malloc
130cf 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 and must be fre
130d0 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e ed by the callin
130d1 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a g.** function..*
130d2 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63 /.static void *c
130d3 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 onvertUtf8Filena
130d4 6d 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a me(const char *z
130d5 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 76 6f 69 Filename){. voi
130d6 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 d *zConverted =
130d7 30 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 0;. if( isNT()
130d8 29 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 ){. zConverte
130d9 64 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 d = utf8ToUnicod
130da 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 e(zFilename);.
130db 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f }.#ifdef SQLITE_
130dc 57 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20 WIN32_HAS_ANSI.
130dd 20 65 6c 73 65 7b 0a 20 20 20 20 7a 43 6f 6e 76 else{. zConv
130de 65 72 74 65 64 20 3d 20 73 71 6c 69 74 65 33 5f erted = sqlite3_
130df 77 69 6e 33 32 5f 75 74 66 38 5f 74 6f 5f 6d 62 win32_utf8_to_mb
130e0 63 73 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 cs(zFilename);.
130e1 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 63 }.#endif. /* c
130e2 61 6c 6c 65 72 20 77 69 6c 6c 20 68 61 6e 64 6c aller will handl
130e3 65 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 e out of memory
130e4 2a 2f 0a 20 20 72 65 74 75 72 6e 20 7a 43 6f 6e */. return zCon
130e5 76 65 72 74 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a verted;.}../*.**
130e6 20 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 Create a tempor
130e7 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e ary file name in
130e8 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 zBuf. zBuf mus
130e9 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 t be big enough
130ea 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56 to.** hold at pV
130eb 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63 fs->mxPathname c
130ec 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 haracters..*/.st
130ed 61 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 atic int getTemp
130ee 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 name(int nBuf, c
130ef 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 har *zBuf){. st
130f0 61 74 69 63 20 63 68 61 72 20 7a 43 68 61 72 73 atic char zChars
130f1 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 [] =. "abcdef
130f2 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 ghijklmnopqrstuv
130f3 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 wxyz". "ABCDE
130f4 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 FGHIJKLMNOPQRSTU
130f5 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 VWXYZ". "0123
130f6 34 35 36 37 38 39 22 3b 0a 20 20 73 69 7a 65 5f 456789";. size_
130f7 74 20 69 2c 20 6a 3b 0a 20 20 69 6e 74 20 6e 54 t i, j;. int nT
130f8 65 6d 70 50 61 74 68 3b 0a 20 20 63 68 61 72 20 empPath;. char
130f9 7a 54 65 6d 70 50 61 74 68 5b 4d 41 58 5f 50 41 zTempPath[MAX_PA
130fa 54 48 2b 32 5d 3b 0a 0a 20 20 2f 2a 20 49 74 27 TH+2];.. /* It'
130fb 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 s odd to simulat
130fc 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 e an io-error he
130fd 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 re, but really t
130fe 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a his is just. **
130ff 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 using the io-er
13100 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 ror infrastructu
13101 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 re to test that
13102 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 SQLite handles t
13103 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f his. ** functio
13104 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20 2a 2f n failing. . */
13105 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
13106 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
13107 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 6d 65 E_IOERR );.. me
13108 6d 73 65 74 28 7a 54 65 6d 70 50 61 74 68 2c 20 mset(zTempPath,
13109 30 2c 20 4d 41 58 5f 50 41 54 48 2b 32 29 3b 0a 0, MAX_PATH+2);.
1310a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 . if( sqlite3_t
1310b 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b emp_directory ){
1310c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
1310d 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 rintf(MAX_PATH-3
1310e 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 0, zTempPath, "%
1310f 73 22 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 s", sqlite3_temp
13110 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 7d _directory);. }
13111 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f .#if !SQLITE_OS_
13112 57 49 4e 52 54 0a 20 20 65 6c 73 65 20 69 66 28 WINRT. else if(
13113 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 63 isNT() ){. c
13114 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0a 20 20 20 har *zMulti;.
13115 20 57 43 48 41 52 20 7a 57 69 64 65 50 61 74 68 WCHAR zWidePath
13116 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 [MAX_PATH];.
13117 6f 73 47 65 74 54 65 6d 70 50 61 74 68 57 28 4d osGetTempPathW(M
13118 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57 69 64 AX_PATH-30, zWid
13119 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75 6c ePath);. zMul
1311a 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 ti = unicodeToUt
1311b 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 f8(zWidePath);.
1311c 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 29 7b if( zMulti ){
1311d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
1311e 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 nprintf(MAX_PATH
1311f 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 -30, zTempPath,
13120 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b 0a 20 "%s", zMulti);.
13121 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
13122 65 28 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20 7d e(zMulti);. }
13123 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 else{. retu
13124 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
13125 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d NOMEM;. }. }
13126 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 57 .#ifdef SQLITE_W
13127 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20 20 IN32_HAS_ANSI.
13128 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a else{. char *
13129 7a 55 74 66 38 3b 0a 20 20 20 20 63 68 61 72 20 zUtf8;. char
1312a 7a 4d 62 63 73 50 61 74 68 5b 4d 41 58 5f 50 41 zMbcsPath[MAX_PA
1312b 54 48 5d 3b 0a 20 20 20 20 6f 73 47 65 74 54 65 TH];. osGetTe
1312c 6d 70 50 61 74 68 41 28 4d 41 58 5f 50 41 54 48 mpPathA(MAX_PATH
1312d 2d 33 30 2c 20 7a 4d 62 63 73 50 61 74 68 29 3b -30, zMbcsPath);
1312e 0a 20 20 20 20 7a 55 74 66 38 20 3d 20 73 71 6c . zUtf8 = sql
1312f 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63 73 5f ite3_win32_mbcs_
13130 74 6f 5f 75 74 66 38 28 7a 4d 62 63 73 50 61 74 to_utf8(zMbcsPat
13131 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 55 74 66 h);. if( zUtf
13132 38 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 8 ){. sqlit
13133 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f e3_snprintf(MAX_
13134 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 PATH-30, zTempPa
13135 74 68 2c 20 22 25 73 22 2c 20 7a 55 74 66 38 29 th, "%s", zUtf8)
13136 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
13137 66 72 65 65 28 7a 55 74 66 38 29 3b 0a 20 20 20 free(zUtf8);.
13138 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 }else{. re
13139 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
1313a 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 R_NOMEM;. }.
1313b 20 7d 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 }.#endif.#endif
1313c 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 .. /* Check tha
1313d 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 t the output buf
1313e 66 65 72 20 69 73 20 6c 61 72 67 65 20 65 6e 6f fer is large eno
1313f 75 67 68 20 66 6f 72 20 74 68 65 20 74 65 6d 70 ugh for the temp
13140 6f 72 61 72 79 20 66 69 6c 65 20 0a 20 20 2a 2a orary file . **
13141 20 6e 61 6d 65 2e 20 49 66 20 69 74 20 69 73 20 name. If it is
13142 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 not, return SQLI
13143 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a 2f 0a 20 TE_ERROR.. */.
13144 20 6e 54 65 6d 70 50 61 74 68 20 3d 20 73 71 6c nTempPath = sql
13145 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 ite3Strlen30(zTe
13146 6d 70 50 61 74 68 29 3b 0a 0a 20 20 69 66 28 20 mpPath);.. if(
13147 28 6e 54 65 6d 70 50 61 74 68 20 2b 20 73 71 6c (nTempPath + sql
13148 69 74 65 33 53 74 72 6c 65 6e 33 30 28 53 51 4c ite3Strlen30(SQL
13149 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 ITE_TEMP_FILE_PR
1314a 45 46 49 58 29 20 2b 20 31 38 29 20 3e 3d 20 6e EFIX) + 18) >= n
1314b 42 75 66 20 29 7b 0a 20 20 20 20 72 65 74 75 72 Buf ){. retur
1314c 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
1314d 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 6e 54 65 }.. for(i=nTe
1314e 6d 70 50 61 74 68 3b 20 69 3e 30 20 26 26 20 7a mpPath; i>0 && z
1314f 54 65 6d 70 50 61 74 68 5b 69 2d 31 5d 3d 3d 27 TempPath[i-1]=='
13150 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a 54 \\'; i--){}. zT
13151 65 6d 70 50 61 74 68 5b 69 5d 20 3d 20 30 3b 0a empPath[i] = 0;.
13152 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
13153 6e 74 66 28 6e 42 75 66 2d 31 38 2c 20 7a 42 75 ntf(nBuf-18, zBu
13154 66 2c 20 28 6e 54 65 6d 70 50 61 74 68 20 3e 20 f, (nTempPath >
13155 30 29 20 3f 0a 20 20 20 20 20 20 20 20 20 20 20 0) ?.
13156 20 20 20 20 20 20 20 20 20 20 20 20 22 25 73 5c "%s\
13157 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 \"SQLITE_TEMP_FI
13158 4c 45 5f 50 52 45 46 49 58 20 3a 20 53 51 4c 49 LE_PREFIX : SQLI
13159 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 TE_TEMP_FILE_PRE
1315a 46 49 58 2c 0a 20 20 20 20 20 20 20 20 20 20 20 FIX,.
1315b 20 20 20 20 20 20 20 20 7a 54 65 6d 70 50 61 74 zTempPat
1315c 68 29 3b 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65 h);. j = sqlite
1315d 33 53 74 72 6c 65 6e 33 30 28 7a 42 75 66 29 3b 3Strlen30(zBuf);
1315e 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f . sqlite3_rando
1315f 6d 6e 65 73 73 28 31 35 2c 20 26 7a 42 75 66 5b mness(15, &zBuf[
13160 6a 5d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 j]);. for(i=0;
13161 69 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b i<15; i++, j++){
13162 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 . zBuf[j] = (
13163 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 char)zChars[ ((u
13164 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 nsigned char)zBu
13165 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 f[j])%(sizeof(zC
13166 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a hars)-1) ];. }.
13167 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 zBuf[j] = 0;.
13168 20 7a 42 75 66 5b 6a 2b 31 5d 20 3d 20 30 3b 0a zBuf[j+1] = 0;.
13169 0a 20 20 4f 53 54 52 41 43 45 28 28 22 54 45 4d . OSTRACE(("TEM
1316a 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e P FILENAME: %s\n
1316b 22 2c 20 7a 42 75 66 29 29 3b 0a 20 20 72 65 74 ", zBuf));. ret
1316c 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a urn SQLITE_OK; .
1316d 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1316e 54 52 55 45 20 69 66 20 74 68 65 20 6e 61 6d 65 TRUE if the name
1316f 64 20 66 69 6c 65 20 69 73 20 72 65 61 6c 6c 79 d file is really
13170 20 61 20 64 69 72 65 63 74 6f 72 79 2e 20 20 52 a directory. R
13171 65 74 75 72 6e 20 66 61 6c 73 65 20 69 66 0a 2a eturn false if.*
13172 2a 20 69 74 20 69 73 20 73 6f 6d 65 74 68 69 6e * it is somethin
13173 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20 64 g other than a d
13174 69 72 65 63 74 6f 72 79 2c 20 6f 72 20 69 66 20 irectory, or if
13175 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b 69 6e there is any kin
13176 64 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 d of memory.** a
13177 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 75 72 llocation failur
13178 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
13179 20 77 69 6e 49 73 44 69 72 28 63 6f 6e 73 74 20 winIsDir(const
1317a 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 void *zConverted
1317b 29 7b 0a 20 20 44 57 4f 52 44 20 61 74 74 72 3b ){. DWORD attr;
1317c 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 . int rc = 0;.
1317d 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f DWORD lastErrno
1317e 3b 0a 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 ;.. if( isNT()
1317f 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d ){. int cnt =
13180 20 30 3b 0a 20 20 20 20 57 49 4e 33 32 5f 46 49 0;. WIN32_FI
13181 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 44 41 54 LE_ATTRIBUTE_DAT
13182 41 20 73 41 74 74 72 44 61 74 61 3b 0a 20 20 20 A sAttrData;.
13183 20 6d 65 6d 73 65 74 28 26 73 41 74 74 72 44 61 memset(&sAttrDa
13184 74 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 41 ta, 0, sizeof(sA
13185 74 74 72 44 61 74 61 29 29 3b 0a 20 20 20 20 77 ttrData));. w
13186 68 69 6c 65 28 20 21 28 72 63 20 3d 20 6f 73 47 hile( !(rc = osG
13187 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 etFileAttributes
13188 45 78 57 28 28 4c 50 43 57 53 54 52 29 7a 43 6f ExW((LPCWSTR)zCo
13189 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 nverted,.
1318a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1318b 20 20 20 20 20 20 47 65 74 46 69 6c 65 45 78 49 GetFileExI
1318c 6e 66 6f 53 74 61 6e 64 61 72 64 2c 0a 20 20 20 nfoStandard,.
1318d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1318e 20 20 20 20 20 20 20 20 20 20 26 73 41 74 74 72 &sAttr
1318f 44 61 74 61 29 29 20 26 26 20 72 65 74 72 79 49 Data)) && retryI
13190 6f 65 72 72 28 26 63 6e 74 2c 20 26 6c 61 73 74 oerr(&cnt, &last
13191 45 72 72 6e 6f 29 20 29 7b 7d 0a 20 20 20 20 69 Errno) ){}. i
13192 66 28 20 21 72 63 20 29 7b 0a 20 20 20 20 20 20 f( !rc ){.
13193 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 49 6e 76 return 0; /* Inv
13194 61 6c 69 64 20 6e 61 6d 65 3f 20 2a 2f 0a 20 20 alid name? */.
13195 20 20 7d 0a 20 20 20 20 61 74 74 72 20 3d 20 73 }. attr = s
13196 41 74 74 72 44 61 74 61 2e 64 77 46 69 6c 65 41 AttrData.dwFileA
13197 74 74 72 69 62 75 74 65 73 3b 0a 23 69 66 20 53 ttributes;.#if S
13198 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
13199 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 0. }else{. a
1319a 74 74 72 20 3d 20 6f 73 47 65 74 46 69 6c 65 41 ttr = osGetFileA
1319b 74 74 72 69 62 75 74 65 73 41 28 28 63 68 61 72 ttributesA((char
1319c 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 *)zConverted);.#
1319d 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 endif. }. retu
1319e 72 6e 20 28 61 74 74 72 21 3d 49 4e 56 41 4c 49 rn (attr!=INVALI
1319f 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 D_FILE_ATTRIBUTE
131a0 53 29 20 26 26 20 28 61 74 74 72 26 46 49 4c 45 S) && (attr&FILE
131a1 5f 41 54 54 52 49 42 55 54 45 5f 44 49 52 45 43 _ATTRIBUTE_DIREC
131a2 54 4f 52 59 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TORY);.}../*.**
131a3 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a Open a file..*/.
131a4 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70 static int winOp
131a5 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 en(. sqlite3_vf
131a6 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 s *pVfs,
131a7 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 /* Not used */.
131a8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
131a9 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 me, /* Na
131aa 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28 me of the file (
131ab 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 UTF-8) */. sqli
131ac 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 te3_file *id,
131ad 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 /* Write t
131ae 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 he SQLite file h
131af 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 andle here */.
131b0 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 int flags,
131b1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 /* Ope
131b2 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a n mode flags */.
131b3 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 int *pOutFlags
131b4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
131b5 74 61 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61 tatus return fla
131b6 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c gs */.){. HANDL
131b7 45 20 68 3b 0a 20 20 44 57 4f 52 44 20 6c 61 73 E h;. DWORD las
131b8 74 45 72 72 6e 6f 3b 0a 20 20 44 57 4f 52 44 20 tErrno;. DWORD
131b9 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 3b dwDesiredAccess;
131ba 0a 20 20 44 57 4f 52 44 20 64 77 53 68 61 72 65 . DWORD dwShare
131bb 4d 6f 64 65 3b 0a 20 20 44 57 4f 52 44 20 64 77 Mode;. DWORD dw
131bc 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 CreationDisposit
131bd 69 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 64 77 46 ion;. DWORD dwF
131be 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 lagsAndAttribute
131bf 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 s = 0;.#if SQLIT
131c0 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74 E_OS_WINCE. int
131c1 20 69 73 54 65 6d 70 20 3d 20 30 3b 0a 23 65 6e isTemp = 0;.#en
131c2 64 69 66 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 dif. winFile *p
131c3 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
131c4 29 69 64 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f )id;. void *zCo
131c5 6e 76 65 72 74 65 64 3b 20 20 20 20 20 20 20 20 nverted;
131c6 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d /* Filenam
131c7 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64 69 6e 67 e in OS encoding
131c8 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
131c9 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 4e *zUtf8Name = zN
131ca 61 6d 65 3b 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 ame; /* Filename
131cb 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 in UTF-8 encodi
131cc 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 20 ng */. int cnt
131cd 3d 20 30 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 72 = 0;.. /* If ar
131ce 67 75 6d 65 6e 74 20 7a 50 61 74 68 20 69 73 20 gument zPath is
131cf 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 a NULL pointer,
131d0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
131d1 20 72 65 71 75 69 72 65 64 20 74 6f 20 6f 70 65 required to ope
131d2 6e 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f 72 61 n. ** a tempora
131d3 72 79 20 66 69 6c 65 2e 20 55 73 65 20 74 68 69 ry file. Use thi
131d4 73 20 62 75 66 66 65 72 20 74 6f 20 73 74 6f 72 s buffer to stor
131d5 65 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 20 e the file name
131d6 69 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 in.. */. char
131d7 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 zTmpname[MAX_PAT
131d8 48 2b 32 5d 3b 20 20 20 20 20 2f 2a 20 42 75 66 H+2]; /* Buf
131d9 66 65 72 20 75 73 65 64 20 74 6f 20 63 72 65 61 fer used to crea
131da 74 65 20 74 65 6d 70 20 66 69 6c 65 6e 61 6d 65 te temp filename
131db 20 2a 2f 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 */.. int rc =
131dc 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
131dd 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f /* Functio
131de 6e 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f n Return Code */
131df 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 .#if !defined(ND
131e0 45 42 55 47 29 20 7c 7c 20 53 51 4c 49 54 45 5f EBUG) || SQLITE_
131e1 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74 20 65 OS_WINCE. int e
131e2 54 79 70 65 20 3d 20 66 6c 61 67 73 26 30 78 46 Type = flags&0xF
131e3 46 46 46 46 46 30 30 3b 20 20 2f 2a 20 54 79 70 FFFFF00; /* Typ
131e4 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 6f 70 65 e of file to ope
131e5 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 69 n */.#endif.. i
131e6 6e 74 20 69 73 45 78 63 6c 75 73 69 76 65 20 20 nt isExclusive
131e7 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 = (flags & SQLIT
131e8 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
131e9 29 3b 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 );. int isDelet
131ea 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 26 e = (flags &
131eb 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c SQLITE_OPEN_DEL
131ec 45 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a 20 20 69 ETEONCLOSE);. i
131ed 6e 74 20 69 73 43 72 65 61 74 65 20 20 20 20 20 nt isCreate
131ee 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 = (flags & SQLIT
131ef 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a E_OPEN_CREATE);.
131f0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
131f1 20 69 6e 74 20 69 73 52 65 61 64 6f 6e 6c 79 20 int isReadonly
131f2 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c = (flags & SQL
131f3 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
131f4 59 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 Y);.#endif. int
131f5 20 69 73 52 65 61 64 57 72 69 74 65 20 20 3d 20 isReadWrite =
131f6 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f (flags & SQLITE_
131f7 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 3b OPEN_READWRITE);
131f8 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
131f9 0a 20 20 69 6e 74 20 69 73 4f 70 65 6e 4a 6f 75 . int isOpenJou
131fa 72 6e 61 6c 20 3d 20 28 69 73 43 72 65 61 74 65 rnal = (isCreate
131fb 20 26 26 20 28 0a 20 20 20 20 20 20 20 20 65 54 && (. eT
131fc 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
131fd 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 _MASTER_JOURNAL
131fe 0a 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d . || eType==
131ff 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
13200 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 7c _JOURNAL . |
13201 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f | eType==SQLITE_
13202 4f 50 45 4e 5f 57 41 4c 0a 20 20 29 29 3b 0a 23 OPEN_WAL. ));.#
13203 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 43 68 65 63 endif.. /* Chec
13204 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 k the following
13205 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 74 statements are t
13206 72 75 65 3a 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 rue: . **. **
13207 20 20 28 61 29 20 45 78 61 63 74 6c 79 20 6f 6e (a) Exactly on
13208 65 20 6f 66 20 74 68 65 20 52 45 41 44 57 52 49 e of the READWRI
13209 54 45 20 61 6e 64 20 52 45 41 44 4f 4e 4c 59 20 TE and READONLY
1320a 66 6c 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 flags must be se
1320b 74 2c 20 61 6e 64 20 0a 20 20 2a 2a 20 20 20 28 t, and . ** (
1320c 62 29 20 69 66 20 43 52 45 41 54 45 20 69 73 20 b) if CREATE is
1320d 73 65 74 2c 20 74 68 65 6e 20 52 45 41 44 57 52 set, then READWR
1320e 49 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 ITE must also be
1320f 20 73 65 74 2c 20 61 6e 64 0a 20 20 2a 2a 20 20 set, and. **
13210 20 28 63 29 20 69 66 20 45 58 43 4c 55 53 49 56 (c) if EXCLUSIV
13211 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 43 E is set, then C
13212 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 REATE must also
13213 62 65 20 73 65 74 2e 0a 20 20 2a 2a 20 20 20 28 be set.. ** (
13214 64 29 20 69 66 20 44 45 4c 45 54 45 4f 4e 43 4c d) if DELETEONCL
13215 4f 53 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e OSE is set, then
13216 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73 CREATE must als
13217 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 o be set.. */.
13218 20 61 73 73 65 72 74 28 28 69 73 52 65 61 64 6f assert((isReado
13219 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64 nly==0 || isRead
1321a 57 72 69 74 65 3d 3d 30 29 20 26 26 20 28 69 73 Write==0) && (is
1321b 52 65 61 64 57 72 69 74 65 20 7c 7c 20 69 73 52 ReadWrite || isR
1321c 65 61 64 6f 6e 6c 79 29 29 3b 0a 20 20 61 73 73 eadonly));. ass
1321d 65 72 74 28 69 73 43 72 65 61 74 65 3d 3d 30 20 ert(isCreate==0
1321e 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 29 3b || isReadWrite);
1321f 0a 20 20 61 73 73 65 72 74 28 69 73 45 78 63 6c . assert(isExcl
13220 75 73 69 76 65 3d 3d 30 20 7c 7c 20 69 73 43 72 usive==0 || isCr
13221 65 61 74 65 29 3b 0a 20 20 61 73 73 65 72 74 28 eate);. assert(
13222 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c 7c 20 69 isDelete==0 || i
13223 73 43 72 65 61 74 65 29 3b 0a 0a 20 20 2f 2a 20 sCreate);.. /*
13224 54 68 65 20 6d 61 69 6e 20 44 42 2c 20 6d 61 69 The main DB, mai
13225 6e 20 6a 6f 75 72 6e 61 6c 2c 20 57 41 4c 20 66 n journal, WAL f
13226 69 6c 65 20 61 6e 64 20 6d 61 73 74 65 72 20 6a ile and master j
13227 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72 ournal are never
13228 20 0a 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 . ** automatic
13229 61 6c 6c 79 20 64 65 6c 65 74 65 64 2e 20 4e 6f ally deleted. No
1322a 72 20 61 72 65 20 74 68 65 79 20 65 76 65 72 20 r are they ever
1322b 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e temporary files.
1322c 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 */. assert( (
1322d 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 !isDelete && zNa
1322e 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 me) || eType!=SQ
1322f 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 LITE_OPEN_MAIN_D
13230 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 B );. assert( (
13231 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 !isDelete && zNa
13232 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 me) || eType!=SQ
13233 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
13234 4f 55 52 4e 41 4c 20 29 3b 0a 20 20 61 73 73 65 OURNAL );. asse
13235 72 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 rt( (!isDelete &
13236 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 & zName) || eTyp
13237 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d e!=SQLITE_OPEN_M
13238 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29 3b ASTER_JOURNAL );
13239 0a 20 20 61 73 73 65 72 74 28 20 28 21 69 73 44 . assert( (!isD
1323a 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 29 20 elete && zName)
1323b 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 || eType!=SQLITE
1323c 5f 4f 50 45 4e 5f 57 41 4c 20 29 3b 0a 0a 20 20 _OPEN_WAL );..
1323d 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 /* Assert that t
1323e 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 20 68 he upper layer h
1323f 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 20 74 68 as set one of th
13240 65 20 22 66 69 6c 65 2d 74 79 70 65 22 20 66 6c e "file-type" fl
13241 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 ags. */. assert
13242 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f ( eType==SQLITE_
13243 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20 OPEN_MAIN_DB
13244 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 || eType==SQLI
13245 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 TE_OPEN_TEMP_DB
13246 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 . || eType
13247 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 ==SQLITE_OPEN_MA
13248 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 IN_JOURNAL || eT
13249 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
1324a 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 0a 20 _TEMP_JOURNAL .
1324b 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d || eType==
1324c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a SQLITE_OPEN_SUBJ
1324d 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54 79 70 OURNAL || eTyp
1324e 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d e==SQLITE_OPEN_M
1324f 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 0a 20 ASTER_JOURNAL .
13250 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d || eType==
13251 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e SQLITE_OPEN_TRAN
13252 53 49 45 4e 54 5f 44 42 20 7c 7c 20 65 54 79 70 SIENT_DB || eTyp
13253 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 57 e==SQLITE_OPEN_W
13254 41 4c 0a 20 20 29 3b 0a 0a 20 20 61 73 73 65 72 AL. );.. asser
13255 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 55 4e t( id!=0 );. UN
13256 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
13257 56 66 73 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 Vfs);..#if SQLIT
13258 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 69 66 28 E_OS_WINRT. if(
13259 20 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 !sqlite3_temp_d
1325a 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 irectory ){.
1325b 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 sqlite3_log(SQLI
1325c 54 45 5f 45 52 52 4f 52 2c 0a 20 20 20 20 20 20 TE_ERROR,.
1325d 20 20 22 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f "sqlite3_temp_
1325e 64 69 72 65 63 74 6f 72 79 20 76 61 72 69 61 62 directory variab
1325f 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 le should be set
13260 20 66 6f 72 20 57 69 6e 52 54 22 29 3b 0a 20 20 for WinRT");.
13261 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 70 46 69 6c }.#endif.. pFil
13262 65 2d 3e 68 20 3d 20 49 4e 56 41 4c 49 44 5f 48 e->h = INVALID_H
13263 41 4e 44 4c 45 5f 56 41 4c 55 45 3b 0a 0a 20 20 ANDLE_VALUE;..
13264 2f 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 /* If the second
13265 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 argument to thi
13266 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 s function is NU
13267 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20 61 20 0a LL, generate a .
13268 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 ** temporary f
13269 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75 73 65 20 ile name to use
1326a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 7a 55 74 . */. if( !zUt
1326b 66 38 4e 61 6d 65 20 29 7b 0a 20 20 20 20 61 73 f8Name ){. as
1326c 73 65 72 74 28 69 73 44 65 6c 65 74 65 20 26 26 sert(isDelete &&
1326d 20 21 69 73 4f 70 65 6e 4a 6f 75 72 6e 61 6c 29 !isOpenJournal)
1326e 3b 0a 20 20 20 20 72 63 20 3d 20 67 65 74 54 65 ;. rc = getTe
1326f 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 2b mpname(MAX_PATH+
13270 32 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 2, zTmpname);.
13271 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13272 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
13273 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
13274 20 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 54 zUtf8Name = zT
13275 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f mpname;. }.. /
13276 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e * Database filen
13277 61 6d 65 73 20 61 72 65 20 64 6f 75 62 6c 65 2d ames are double-
13278 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 20 zero terminated
13279 69 66 20 74 68 65 79 20 61 72 65 20 6e 6f 74 0a if they are not.
1327a 20 20 2a 2a 20 55 52 49 73 20 77 69 74 68 20 70 ** URIs with p
1327b 61 72 61 6d 65 74 65 72 73 2e 20 20 48 65 6e 63 arameters. Henc
1327c 65 2c 20 74 68 65 79 20 63 61 6e 20 61 6c 77 61 e, they can alwa
1327d 79 73 20 62 65 20 70 61 73 73 65 64 20 69 6e 74 ys be passed int
1327e 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 75 o. ** sqlite3_u
1327f 72 69 5f 70 61 72 61 6d 65 74 65 72 28 29 2e 0a ri_parameter()..
13280 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 */. assert( (
13281 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 eType!=SQLITE_OP
13282 45 4e 5f 4d 41 49 4e 5f 44 42 29 20 7c 7c 20 28 EN_MAIN_DB) || (
13283 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
13284 50 45 4e 5f 55 52 49 29 20 7c 7c 0a 20 20 20 20 PEN_URI) ||.
13285 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 5b 73 74 zUtf8Name[st
13286 72 6c 65 6e 28 7a 55 74 66 38 4e 61 6d 65 29 2b rlen(zUtf8Name)+
13287 31 5d 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43 1]==0 );.. /* C
13288 6f 6e 76 65 72 74 20 74 68 65 20 66 69 6c 65 6e onvert the filen
13289 61 6d 65 20 74 6f 20 74 68 65 20 73 79 73 74 65 ame to the syste
1328a 6d 20 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 m encoding. */.
1328b 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f zConverted = co
1328c 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d nvertUtf8Filenam
1328d 65 28 7a 55 74 66 38 4e 61 6d 65 29 3b 0a 20 20 e(zUtf8Name);.
1328e 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d if( zConverted==
1328f 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
13290 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d SQLITE_IOERR_NOM
13291 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 77 EM;. }.. if( w
13292 69 6e 49 73 44 69 72 28 7a 43 6f 6e 76 65 72 74 inIsDir(zConvert
13293 65 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ed) ){. sqlit
13294 65 33 5f 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 e3_free(zConvert
13295 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ed);. return
13296 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f SQLITE_CANTOPEN_
13297 49 53 44 49 52 3b 0a 20 20 7d 0a 0a 20 20 69 66 ISDIR;. }.. if
13298 28 20 69 73 52 65 61 64 57 72 69 74 65 20 29 7b ( isReadWrite ){
13299 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 . dwDesiredAc
1329a 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 cess = GENERIC_R
1329b 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52 EAD | GENERIC_WR
1329c 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ITE;. }else{.
1329d 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73 dwDesiredAcces
1329e 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 s = GENERIC_READ
1329f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 51 4c 49 ;. }.. /* SQLI
132a0 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 TE_OPEN_EXCLUSIV
132a1 45 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b E is used to mak
132a2 65 20 73 75 72 65 20 74 68 61 74 20 61 20 6e 65 e sure that a ne
132a3 77 20 66 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 w file is . **
132a4 63 72 65 61 74 65 64 2e 20 53 51 4c 69 74 65 20 created. SQLite
132a5 64 6f 65 73 6e 27 74 20 75 73 65 20 69 74 20 74 doesn't use it t
132a6 6f 20 69 6e 64 69 63 61 74 65 20 22 65 78 63 6c o indicate "excl
132a7 75 73 69 76 65 20 61 63 63 65 73 73 22 20 0a 20 usive access" .
132a8 20 2a 2a 20 61 73 20 69 74 20 69 73 20 75 73 75 ** as it is usu
132a9 61 6c 6c 79 20 75 6e 64 65 72 73 74 6f 6f 64 2e ally understood.
132aa 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 45 78 . */. if( isEx
132ab 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20 2f clusive ){. /
132ac 2a 20 43 72 65 61 74 65 73 20 61 20 6e 65 77 20 * Creates a new
132ad 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 file, only if it
132ae 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 does not alread
132af 79 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20 y exist. */.
132b0 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 65 /* If the file e
132b1 78 69 73 74 73 2c 20 69 74 20 66 61 69 6c 73 2e xists, it fails.
132b2 20 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69 */. dwCreati
132b3 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 onDisposition =
132b4 43 52 45 41 54 45 5f 4e 45 57 3b 0a 20 20 7d 65 CREATE_NEW;. }e
132b5 6c 73 65 20 69 66 28 20 69 73 43 72 65 61 74 65 lse if( isCreate
132b6 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 ){. /* Open
132b7 65 78 69 73 74 69 6e 67 20 66 69 6c 65 2c 20 6f existing file, o
132b8 72 20 63 72 65 61 74 65 20 69 66 20 69 74 20 64 r create if it d
132b9 6f 65 73 6e 27 74 20 65 78 69 73 74 20 2a 2f 0a oesn't exist */.
132ba 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 dwCreationDi
132bb 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e sposition = OPEN
132bc 5f 41 4c 57 41 59 53 3b 0a 20 20 7d 65 6c 73 65 _ALWAYS;. }else
132bd 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 73 20 61 {. /* Opens a
132be 20 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 file, only if i
132bf 74 20 65 78 69 73 74 73 2e 20 2a 2f 0a 20 20 20 t exists. */.
132c0 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f dwCreationDispo
132c1 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58 sition = OPEN_EX
132c2 49 53 54 49 4e 47 3b 0a 20 20 7d 0a 0a 20 20 64 ISTING;. }.. d
132c3 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49 4c wShareMode = FIL
132c4 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20 46 E_SHARE_READ | F
132c5 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45 3b ILE_SHARE_WRITE;
132c6 0a 0a 20 20 69 66 28 20 69 73 44 65 6c 65 74 65 .. if( isDelete
132c7 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f ){.#if SQLITE_O
132c8 53 5f 57 49 4e 43 45 0a 20 20 20 20 64 77 46 6c S_WINCE. dwFl
132c9 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 agsAndAttributes
132ca 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 = FILE_ATTRIBUT
132cb 45 5f 48 49 44 44 45 4e 3b 0a 20 20 20 20 69 73 E_HIDDEN;. is
132cc 54 65 6d 70 20 3d 20 31 3b 0a 23 65 6c 73 65 0a Temp = 1;.#else.
132cd 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 dwFlagsAndAt
132ce 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f tributes = FILE_
132cf 41 54 54 52 49 42 55 54 45 5f 54 45 4d 50 4f 52 ATTRIBUTE_TEMPOR
132d0 41 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 ARY.
132d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132d2 20 20 20 7c 20 46 49 4c 45 5f 41 54 54 52 49 42 | FILE_ATTRIB
132d3 55 54 45 5f 48 49 44 44 45 4e 0a 20 20 20 20 20 UTE_HIDDEN.
132d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132d5 20 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c 45 | FILE
132d6 5f 46 4c 41 47 5f 44 45 4c 45 54 45 5f 4f 4e 5f _FLAG_DELETE_ON_
132d7 43 4c 4f 53 45 3b 0a 23 65 6e 64 69 66 0a 20 20 CLOSE;.#endif.
132d8 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 46 6c 61 }else{. dwFla
132d9 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 gsAndAttributes
132da 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 = FILE_ATTRIBUTE
132db 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 2f _NORMAL;. }. /
132dc 2a 20 52 65 70 6f 72 74 73 20 66 72 6f 6d 20 74 * Reports from t
132dd 68 65 20 69 6e 74 65 72 6e 65 74 20 61 72 65 20 he internet are
132de 74 68 61 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 that performance
132df 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 is always. **
132e0 62 65 74 74 65 72 20 69 66 20 46 49 4c 45 5f 46 better if FILE_F
132e1 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53 LAG_RANDOM_ACCES
132e2 53 20 69 73 20 75 73 65 64 2e 20 20 54 69 63 6b S is used. Tick
132e3 65 74 20 23 32 36 39 39 2e 20 2a 2f 0a 23 69 66 et #2699. */.#if
132e4 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
132e5 0a 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 . dwFlagsAndAtt
132e6 72 69 62 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f ributes |= FILE_
132e7 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 FLAG_RANDOM_ACCE
132e8 53 53 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 SS;.#endif.. if
132e9 28 20 69 73 4e 54 28 29 20 29 7b 0a 23 69 66 20 ( isNT() ){.#if
132ea 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a SQLITE_OS_WINRT.
132eb 20 20 20 20 43 52 45 41 54 45 46 49 4c 45 32 5f CREATEFILE2_
132ec 45 58 54 45 4e 44 45 44 5f 50 41 52 41 4d 45 54 EXTENDED_PARAMET
132ed 45 52 53 20 65 78 74 65 6e 64 65 64 50 61 72 61 ERS extendedPara
132ee 6d 65 74 65 72 73 3b 0a 20 20 20 20 65 78 74 65 meters;. exte
132ef 6e 64 65 64 50 61 72 61 6d 65 74 65 72 73 2e 64 ndedParameters.d
132f0 77 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 43 wSize = sizeof(C
132f1 52 45 41 54 45 46 49 4c 45 32 5f 45 58 54 45 4e REATEFILE2_EXTEN
132f2 44 45 44 5f 50 41 52 41 4d 45 54 45 52 53 29 3b DED_PARAMETERS);
132f3 0a 20 20 20 20 65 78 74 65 6e 64 65 64 50 61 72 . extendedPar
132f4 61 6d 65 74 65 72 73 2e 64 77 46 69 6c 65 41 74 ameters.dwFileAt
132f5 74 72 69 62 75 74 65 73 20 3d 0a 20 20 20 20 20 tributes =.
132f6 20 20 20 20 20 20 20 64 77 46 6c 61 67 73 41 6e dwFlagsAn
132f7 64 41 74 74 72 69 62 75 74 65 73 20 26 20 46 49 dAttributes & FI
132f8 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 4d 41 53 LE_ATTRIBUTE_MAS
132f9 4b 3b 0a 20 20 20 20 65 78 74 65 6e 64 65 64 50 K;. extendedP
132fa 61 72 61 6d 65 74 65 72 73 2e 64 77 46 69 6c 65 arameters.dwFile
132fb 46 6c 61 67 73 20 3d 20 64 77 46 6c 61 67 73 41 Flags = dwFlagsA
132fc 6e 64 41 74 74 72 69 62 75 74 65 73 20 26 20 46 ndAttributes & F
132fd 49 4c 45 5f 46 4c 41 47 5f 4d 41 53 4b 3b 0a 20 ILE_FLAG_MASK;.
132fe 20 20 20 65 78 74 65 6e 64 65 64 50 61 72 61 6d extendedParam
132ff 65 74 65 72 73 2e 64 77 53 65 63 75 72 69 74 79 eters.dwSecurity
13300 51 6f 73 46 6c 61 67 73 20 3d 20 53 45 43 55 52 QosFlags = SECUR
13301 49 54 59 5f 41 4e 4f 4e 59 4d 4f 55 53 3b 0a 20 ITY_ANONYMOUS;.
13302 20 20 20 65 78 74 65 6e 64 65 64 50 61 72 61 6d extendedParam
13303 65 74 65 72 73 2e 6c 70 53 65 63 75 72 69 74 79 eters.lpSecurity
13304 41 74 74 72 69 62 75 74 65 73 20 3d 20 4e 55 4c Attributes = NUL
13305 4c 3b 0a 20 20 20 20 65 78 74 65 6e 64 65 64 50 L;. extendedP
13306 61 72 61 6d 65 74 65 72 73 2e 68 54 65 6d 70 6c arameters.hTempl
13307 61 74 65 46 69 6c 65 20 3d 20 4e 55 4c 4c 3b 0a ateFile = NULL;.
13308 20 20 20 20 77 68 69 6c 65 28 20 28 68 20 3d 20 while( (h =
13309 6f 73 43 72 65 61 74 65 46 69 6c 65 32 28 28 4c osCreateFile2((L
1330a 50 43 57 53 54 52 29 7a 43 6f 6e 76 65 72 74 65 PCWSTR)zConverte
1330b 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 d,.
1330c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1330d 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 dwDesiredAccess
1330e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1330f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13310 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20 dwShareMode,.
13311 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13312 20 20 20 20 20 20 20 20 20 20 20 64 77 43 72 65 dwCre
13313 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e ationDisposition
13314 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
13315 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13316 26 65 78 74 65 6e 64 65 64 50 61 72 61 6d 65 74 &extendedParamet
13317 65 72 73 29 29 3d 3d 49 4e 56 41 4c 49 44 5f 48 ers))==INVALID_H
13318 41 4e 44 4c 45 5f 56 41 4c 55 45 20 26 26 0a 20 ANDLE_VALUE &&.
13319 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1331a 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 ret
1331b 72 79 49 6f 65 72 72 28 26 63 6e 74 2c 20 26 6c ryIoerr(&cnt, &l
1331c 61 73 74 45 72 72 6e 6f 29 20 29 7b 0a 20 20 20 astErrno) ){.
1331d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1331e 6f 6f 70 20 2a 2f 0a 20 20 20 20 7d 0a 23 65 6c oop */. }.#el
1331f 73 65 0a 20 20 20 20 77 68 69 6c 65 28 20 28 68 se. while( (h
13320 20 3d 20 6f 73 43 72 65 61 74 65 46 69 6c 65 57 = osCreateFileW
13321 28 28 4c 50 43 57 53 54 52 29 7a 43 6f 6e 76 65 ((LPCWSTR)zConve
13322 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20 20 rted,.
13323 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13324 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 dwDesiredAcc
13325 65 73 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ess,.
13326 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13327 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 20 dwShareMode,
13328 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 NULL,.
13329 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1332a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 dwCreationDi
1332b 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 sposition,.
1332c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1332d 20 20 20 20 20 20 20 20 20 64 77 46 6c 61 67 73 dwFlags
1332e 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a 20 AndAttributes,.
1332f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13330 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c NUL
13331 4c 29 29 3d 3d 49 4e 56 41 4c 49 44 5f 48 41 4e L))==INVALID_HAN
13332 44 4c 45 5f 56 41 4c 55 45 20 26 26 0a 20 20 20 DLE_VALUE &&.
13333 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13334 20 20 20 20 20 20 20 20 20 20 20 72 65 74 72 79 retry
13335 49 6f 65 72 72 28 26 63 6e 74 2c 20 26 6c 61 73 Ioerr(&cnt, &las
13336 74 45 72 72 6e 6f 29 20 29 7b 0a 20 20 20 20 20 tErrno) ){.
13337 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 6f /* Noo
13338 70 20 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 p */. }.#endi
13339 66 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c f. }.#ifdef SQL
1333a 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 41 4e ITE_WIN32_HAS_AN
1333b 53 49 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 77 SI. else{. w
1333c 68 69 6c 65 28 20 28 68 20 3d 20 6f 73 43 72 65 hile( (h = osCre
1333d 61 74 65 46 69 6c 65 41 28 28 4c 50 43 53 54 52 ateFileA((LPCSTR
1333e 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 )zConverted,.
1333f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13340 20 20 20 20 20 20 20 20 20 20 20 64 77 44 65 73 dwDes
13341 69 72 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20 iredAccess,.
13342 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13343 20 20 20 20 20 20 20 20 20 20 64 77 53 68 61 72 dwShar
13344 65 4d 6f 64 65 2c 20 4e 55 4c 4c 2c 0a 20 20 20 eMode, NULL,.
13345 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13346 20 20 20 20 20 20 20 20 20 20 20 64 77 43 72 65 dwCre
13347 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e ationDisposition
13348 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
13349 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1334a 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 dwFlagsAndAttrib
1334b 75 74 65 73 2c 0a 20 20 20 20 20 20 20 20 20 20 utes,.
1334c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1334d 20 20 20 20 4e 55 4c 4c 29 29 3d 3d 49 4e 56 41 NULL))==INVA
1334e 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 LID_HANDLE_VALUE
1334f 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 &&.
13350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13351 20 20 72 65 74 72 79 49 6f 65 72 72 28 26 63 6e retryIoerr(&cn
13352 74 2c 20 26 6c 61 73 74 45 72 72 6e 6f 29 20 29 t, &lastErrno) )
13353 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
13354 20 2f 2a 20 4e 6f 6f 70 20 2a 2f 0a 20 20 20 20 /* Noop */.
13355 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 6c }. }.#endif. l
13356 6f 67 49 6f 65 72 72 28 63 6e 74 29 3b 0a 0a 20 ogIoerr(cnt);..
13357 20 4f 53 54 52 41 43 45 28 28 22 4f 50 45 4e 20 OSTRACE(("OPEN
13358 25 64 20 25 73 20 30 78 25 6c 78 20 25 73 5c 6e %d %s 0x%lx %s\n
13359 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 68 ", . h
1335a 2c 20 7a 4e 61 6d 65 2c 20 64 77 44 65 73 69 72 , zName, dwDesir
1335b 65 64 41 63 63 65 73 73 2c 20 0a 20 20 20 20 20 edAccess, .
1335c 20 20 20 20 20 20 68 3d 3d 49 4e 56 41 4c 49 44 h==INVALID
1335d 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 3f 20 _HANDLE_VALUE ?
1335e 22 66 61 69 6c 65 64 22 20 3a 20 22 6f 6b 22 29 "failed" : "ok")
1335f 29 3b 0a 0a 20 20 69 66 28 20 68 3d 3d 49 4e 56 );.. if( h==INV
13360 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 ALID_HANDLE_VALU
13361 45 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e E ){. pFile->
13362 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 61 73 74 lastErrno = last
13363 45 72 72 6e 6f 3b 0a 20 20 20 20 77 69 6e 4c 6f Errno;. winLo
13364 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 43 41 gError(SQLITE_CA
13365 4e 54 4f 50 45 4e 2c 20 70 46 69 6c 65 2d 3e 6c NTOPEN, pFile->l
13366 61 73 74 45 72 72 6e 6f 2c 20 22 77 69 6e 4f 70 astErrno, "winOp
13367 65 6e 22 2c 20 7a 55 74 66 38 4e 61 6d 65 29 3b en", zUtf8Name);
13368 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
13369 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
1336a 20 20 20 69 66 28 20 69 73 52 65 61 64 57 72 69 if( isReadWri
1336b 74 65 20 26 26 20 21 69 73 45 78 63 6c 75 73 69 te && !isExclusi
1336c 76 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ve ){. retu
1336d 72 6e 20 77 69 6e 4f 70 65 6e 28 70 56 66 73 2c rn winOpen(pVfs,
1336e 20 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a 20 20 20 zName, id, .
1336f 20 20 20 20 20 20 20 20 20 20 28 28 66 6c 61 67 ((flag
13370 73 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 s|SQLITE_OPEN_RE
13371 41 44 4f 4e 4c 59 29 26 7e 28 53 51 4c 49 54 45 ADONLY)&~(SQLITE
13372 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c _OPEN_CREATE|SQL
13373 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
13374 54 45 29 29 2c 20 70 4f 75 74 46 6c 61 67 73 29 TE)), pOutFlags)
13375 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
13376 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
13377 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 3b 0a _CANTOPEN_BKPT;.
13378 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
13379 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 pOutFlags ){.
1337a 20 20 69 66 28 20 69 73 52 65 61 64 57 72 69 74 if( isReadWrit
1337b 65 20 29 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 e ){. *pOut
1337c 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f Flags = SQLITE_O
1337d 50 45 4e 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 PEN_READWRITE;.
1337e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1337f 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 4c *pOutFlags = SQL
13380 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
13381 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 Y;. }. }..
13382 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c memset(pFile, 0,
13383 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 29 sizeof(*pFile))
13384 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 ;. pFile->pMeth
13385 6f 64 20 3d 20 26 77 69 6e 49 6f 4d 65 74 68 6f od = &winIoMetho
13386 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 d;. pFile->h =
13387 68 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 h;. pFile->last
13388 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 52 52 4f 52 Errno = NO_ERROR
13389 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 56 66 73 20 ;. pFile->pVfs
1338a 3d 20 70 56 66 73 3b 0a 23 69 66 6e 64 65 66 20 = pVfs;.#ifndef
1338b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a SQLITE_OMIT_WAL.
1338c 20 20 70 46 69 6c 65 2d 3e 70 53 68 6d 20 3d 20 pFile->pShm =
1338d 30 3b 0a 23 65 6e 64 69 66 0a 20 20 70 46 69 6c 0;.#endif. pFil
1338e 65 2d 3e 7a 50 61 74 68 20 3d 20 7a 4e 61 6d 65 e->zPath = zName
1338f 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f ;. if( sqlite3_
13390 75 72 69 5f 62 6f 6f 6c 65 61 6e 28 7a 4e 61 6d uri_boolean(zNam
13391 65 2c 20 22 70 73 6f 77 22 2c 20 53 51 4c 49 54 e, "psow", SQLIT
13392 45 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 E_POWERSAFE_OVER
13393 57 52 49 54 45 29 20 29 7b 0a 20 20 20 20 70 46 WRITE) ){. pF
13394 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20 7c ile->ctrlFlags |
13395 3d 20 57 49 4e 46 49 4c 45 5f 50 53 4f 57 3b 0a = WINFILE_PSOW;.
13396 20 20 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f }..#if SQLITE_
13397 4f 53 5f 57 49 4e 43 45 0a 20 20 69 66 28 20 69 OS_WINCE. if( i
13398 73 52 65 61 64 57 72 69 74 65 20 26 26 20 65 54 sReadWrite && eT
13399 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
1339a 5f 4d 41 49 4e 5f 44 42 0a 20 20 20 20 20 20 20 _MAIN_DB.
1339b 26 26 20 21 77 69 6e 63 65 43 72 65 61 74 65 4c && !winceCreateL
1339c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65 ock(zName, pFile
1339d 29 0a 20 20 29 7b 0a 20 20 20 20 6f 73 43 6c 6f ). ){. osClo
1339e 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a 20 20 20 seHandle(h);.
1339f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 sqlite3_free(zC
133a0 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 72 onverted);. r
133a1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e eturn SQLITE_CAN
133a2 54 4f 50 45 4e 5f 42 4b 50 54 3b 0a 20 20 7d 0a TOPEN_BKPT;. }.
133a3 20 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b 0a if( isTemp ){.
133a4 20 20 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 pFile->zDele
133a5 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f 6e teOnClose = zCon
133a6 76 65 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65 0a verted;. }else.
133a7 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 73 #endif. {. s
133a8 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e qlite3_free(zCon
133a9 76 65 72 74 65 64 29 3b 0a 20 20 7d 0a 0a 20 20 verted);. }..
133aa 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b OpenCounter(+1);
133ab 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
133ac 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 ./*.** Delete th
133ad 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a e named file..**
133ae 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 57 69 .** Note that Wi
133af 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61 ndows does not a
133b0 6c 6c 6f 77 20 61 20 66 69 6c 65 20 74 6f 20 62 llow a file to b
133b1 65 20 64 65 6c 65 74 65 64 20 69 66 20 73 6f 6d e deleted if som
133b2 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65 e other.** proce
133b3 73 73 20 68 61 73 20 69 74 20 6f 70 65 6e 2e 20 ss has it open.
133b4 20 53 6f 6d 65 74 69 6d 65 73 20 61 20 76 69 72 Sometimes a vir
133b5 75 73 20 73 63 61 6e 6e 65 72 20 6f 72 20 69 6e us scanner or in
133b6 64 65 78 69 6e 67 20 70 72 6f 67 72 61 6d 0a 2a dexing program.*
133b7 2a 20 77 69 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f * will open a jo
133b8 75 72 6e 61 6c 20 66 69 6c 65 20 73 68 6f 72 74 urnal file short
133b9 6c 79 20 61 66 74 65 72 20 69 74 20 69 73 20 63 ly after it is c
133ba 72 65 61 74 65 64 20 69 6e 20 6f 72 64 65 72 20 reated in order
133bb 74 6f 20 64 6f 0a 2a 2a 20 77 68 61 74 65 76 65 to do.** whateve
133bc 72 20 69 74 20 64 6f 65 73 2e 20 20 57 68 69 6c r it does. Whil
133bd 65 20 74 68 69 73 20 6f 74 68 65 72 20 70 72 6f e this other pro
133be 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 cess is holding
133bf 74 68 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 6e the.** file open
133c0 2c 20 77 65 20 77 69 6c 6c 20 62 65 20 75 6e 61 , we will be una
133c1 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74 ble to delete it
133c2 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e . To work aroun
133c3 64 20 74 68 69 73 0a 2a 2a 20 70 72 6f 62 6c 65 d this.** proble
133c4 6d 2c 20 77 65 20 64 65 6c 61 79 20 31 30 30 20 m, we delay 100
133c5 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 milliseconds and
133c6 20 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20 61 try to delete a
133c7 67 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f 20 gain. Up.** to
133c8 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 MX_DELETION_ATTE
133c9 4d 50 54 73 20 64 65 6c 65 74 69 6f 6e 20 61 74 MPTs deletion at
133ca 74 65 6d 70 74 73 20 61 72 65 20 72 75 6e 20 62 tempts are run b
133cb 65 66 6f 72 65 20 67 69 76 69 6e 67 0a 2a 2a 20 efore giving.**
133cc 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 up and returning
133cd 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 an error..*/.st
133ce 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 6c 65 atic int winDele
133cf 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 te(. sqlite3_vf
133d0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 s *pVfs,
133d1 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e /* Not used on
133d2 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 win32 */. cons
133d3 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
133d4 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 e, /* Name
133d5 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 of file to delet
133d6 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 e */. int syncD
133d7 69 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ir
133d8 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f /* Not used o
133d9 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20 n win32 */.){.
133da 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69 int cnt = 0;. i
133db 6e 74 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 61 nt rc;. DWORD a
133dc 74 74 72 3b 0a 20 20 44 57 4f 52 44 20 6c 61 73 ttr;. DWORD las
133dd 74 45 72 72 6e 6f 3b 0a 20 20 76 6f 69 64 20 2a tErrno;. void *
133de 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 55 4e zConverted;. UN
133df 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
133e0 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 Vfs);. UNUSED_P
133e1 41 52 41 4d 45 54 45 52 28 73 79 6e 63 44 69 72 ARAMETER(syncDir
133e2 29 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f );.. SimulateIO
133e3 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c Error(return SQL
133e4 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 ITE_IOERR_DELETE
133e5 29 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 );. zConverted
133e6 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c = convertUtf8Fil
133e7 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 ename(zFilename)
133e8 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 ;. if( zConvert
133e9 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ed==0 ){. ret
133ea 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
133eb 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 _NOMEM;. }. if
133ec 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 ( isNT() ){.
133ed 64 6f 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f do {.#if SQLITE_
133ee 4f 53 5f 57 49 4e 52 54 0a 20 20 20 20 20 20 57 OS_WINRT. W
133ef 49 4e 33 32 5f 46 49 4c 45 5f 41 54 54 52 49 42 IN32_FILE_ATTRIB
133f0 55 54 45 5f 44 41 54 41 20 73 41 74 74 72 44 61 UTE_DATA sAttrDa
133f1 74 61 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 ta;. memset
133f2 28 26 73 41 74 74 72 44 61 74 61 2c 20 30 2c 20 (&sAttrData, 0,
133f3 73 69 7a 65 6f 66 28 73 41 74 74 72 44 61 74 61 sizeof(sAttrData
133f4 29 29 3b 0a 20 20 20 20 20 20 69 66 20 28 20 6f ));. if ( o
133f5 73 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 sGetFileAttribut
133f6 65 73 45 78 57 28 7a 43 6f 6e 76 65 72 74 65 64 esExW(zConverted
133f7 2c 20 47 65 74 46 69 6c 65 45 78 49 6e 66 6f 53 , GetFileExInfoS
133f8 74 61 6e 64 61 72 64 2c 0a 20 20 20 20 20 20 20 tandard,.
133f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
133fa 20 20 20 20 20 20 20 20 20 20 20 26 73 41 74 74 &sAtt
133fb 72 44 61 74 61 29 20 29 7b 0a 20 20 20 20 20 20 rData) ){.
133fc 20 20 61 74 74 72 20 3d 20 73 41 74 74 72 44 61 attr = sAttrDa
133fd 74 61 2e 64 77 46 69 6c 65 41 74 74 72 69 62 75 ta.dwFileAttribu
133fe 74 65 73 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 tes;. }else
133ff 7b 0a 20 20 20 20 20 20 20 20 6c 61 73 74 45 72 {. lastEr
13400 72 6e 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74 45 rno = osGetLastE
13401 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 20 20 rror();.
13402 69 66 28 20 6c 61 73 74 45 72 72 6e 6f 3d 3d 45 if( lastErrno==E
13403 52 52 4f 52 5f 46 49 4c 45 5f 4e 4f 54 5f 46 4f RROR_FILE_NOT_FO
13404 55 4e 44 20 7c 7c 20 6c 61 73 74 45 72 72 6e 6f UND || lastErrno
13405 3d 3d 45 52 52 4f 52 5f 50 41 54 48 5f 4e 4f 54 ==ERROR_PATH_NOT
13406 5f 46 4f 55 4e 44 20 29 7b 0a 20 20 20 20 20 20 _FOUND ){.
13407 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
13408 49 4f 45 52 52 5f 44 45 4c 45 54 45 5f 4e 4f 45 IOERR_DELETE_NOE
13409 4e 54 3b 20 2f 2a 20 41 6c 72 65 61 64 79 20 67 NT; /* Already g
1340a 6f 6e 65 3f 20 2a 2f 0a 20 20 20 20 20 20 20 20 one? */.
1340b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
1340c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
1340d 4f 52 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 OR;. }.
1340e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1340f 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 20 }.#else.
13410 20 61 74 74 72 20 3d 20 6f 73 47 65 74 46 69 6c attr = osGetFil
13411 65 41 74 74 72 69 62 75 74 65 73 57 28 7a 43 6f eAttributesW(zCo
13412 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 69 66 nverted);.#endif
13413 0a 20 20 20 20 20 20 69 66 20 28 20 61 74 74 72 . if ( attr
13414 3d 3d 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 ==INVALID_FILE_A
13415 54 54 52 49 42 55 54 45 53 20 29 7b 0a 20 20 20 TTRIBUTES ){.
13416 20 20 20 20 20 6c 61 73 74 45 72 72 6e 6f 20 3d lastErrno =
13417 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 osGetLastError(
13418 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6c );. if( l
13419 61 73 74 45 72 72 6e 6f 3d 3d 45 52 52 4f 52 5f astErrno==ERROR_
1341a 46 49 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 20 7c FILE_NOT_FOUND |
1341b 7c 20 6c 61 73 74 45 72 72 6e 6f 3d 3d 45 52 52 | lastErrno==ERR
1341c 4f 52 5f 50 41 54 48 5f 4e 4f 54 5f 46 4f 55 4e OR_PATH_NOT_FOUN
1341d 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 D ){. r
1341e 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
1341f 5f 44 45 4c 45 54 45 5f 4e 4f 45 4e 54 3b 20 2f _DELETE_NOENT; /
13420 2a 20 41 6c 72 65 61 64 79 20 67 6f 6e 65 3f 20 * Already gone?
13421 2a 2f 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 */. }else
13422 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
13423 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
13424 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
13425 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
13426 20 20 20 20 20 20 69 66 20 28 20 61 74 74 72 26 if ( attr&
13427 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 44 FILE_ATTRIBUTE_D
13428 49 52 45 43 54 4f 52 59 20 29 7b 0a 20 20 20 20 IRECTORY ){.
13429 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1342a 45 52 52 4f 52 3b 20 2f 2a 20 46 69 6c 65 73 20 ERROR; /* Files
1342b 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 only. */.
1342c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
1342d 20 20 20 20 20 20 69 66 20 28 20 6f 73 44 65 6c if ( osDel
1342e 65 74 65 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 eteFileW(zConver
1342f 74 65 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ted) ){.
13430 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
13431 2f 2a 20 44 65 6c 65 74 65 64 20 4f 4b 2e 20 2a /* Deleted OK. *
13432 2f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b /. break;
13433 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
13434 66 20 28 20 21 72 65 74 72 79 49 6f 65 72 72 28 f ( !retryIoerr(
13435 26 63 6e 74 2c 20 26 6c 61 73 74 45 72 72 6e 6f &cnt, &lastErrno
13436 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 ) ){. rc
13437 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 = SQLITE_ERROR;
13438 2f 2a 20 4e 6f 20 6d 6f 72 65 20 72 65 74 72 69 /* No more retri
13439 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 es. */. b
1343a 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1343b 20 20 7d 20 77 68 69 6c 65 28 31 29 3b 0a 20 20 } while(1);.
1343c 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f }.#ifdef SQLITE_
1343d 57 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20 WIN32_HAS_ANSI.
1343e 20 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20 7b 0a else{. do {.
1343f 20 20 20 20 20 20 61 74 74 72 20 3d 20 6f 73 47 attr = osG
13440 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 etFileAttributes
13441 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 A(zConverted);.
13442 20 20 20 20 20 69 66 20 28 20 61 74 74 72 3d 3d if ( attr==
13443 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 INVALID_FILE_ATT
13444 52 49 42 55 54 45 53 20 29 7b 0a 20 20 20 20 20 RIBUTES ){.
13445 20 20 20 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f lastErrno = o
13446 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b sGetLastError();
13447 0a 20 20 20 20 20 20 20 20 69 66 28 20 6c 61 73 . if( las
13448 74 45 72 72 6e 6f 3d 3d 45 52 52 4f 52 5f 46 49 tErrno==ERROR_FI
13449 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 20 7c 7c 20 LE_NOT_FOUND ||
1344a 6c 61 73 74 45 72 72 6e 6f 3d 3d 45 52 52 4f 52 lastErrno==ERROR
1344b 5f 50 41 54 48 5f 4e 4f 54 5f 46 4f 55 4e 44 20 _PATH_NOT_FOUND
1344c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
1344d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 = SQLITE_IOERR_D
1344e 45 4c 45 54 45 5f 4e 4f 45 4e 54 3b 20 2f 2a 20 ELETE_NOENT; /*
1344f 41 6c 72 65 61 64 79 20 67 6f 6e 65 3f 20 2a 2f Already gone? */
13450 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
13451 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
13452 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
13453 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 }. b
13454 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
13455 20 20 20 20 69 66 20 28 20 61 74 74 72 26 46 49 if ( attr&FI
13456 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 44 49 52 LE_ATTRIBUTE_DIR
13457 45 43 54 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 ECTORY ){.
13458 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
13459 52 4f 52 3b 20 2f 2a 20 46 69 6c 65 73 20 6f 6e ROR; /* Files on
1345a 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 ly. */. b
1345b 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1345c 20 20 20 20 69 66 20 28 20 6f 73 44 65 6c 65 74 if ( osDelet
1345d 65 46 69 6c 65 41 28 7a 43 6f 6e 76 65 72 74 65 eFileA(zConverte
1345e 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 d) ){. rc
1345f 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 2f 2a = SQLITE_OK; /*
13460 20 44 65 6c 65 74 65 64 20 4f 4b 2e 20 2a 2f 0a Deleted OK. */.
13461 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
13462 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 20 }. if
13463 28 20 21 72 65 74 72 79 49 6f 65 72 72 28 26 63 ( !retryIoerr(&c
13464 6e 74 2c 20 26 6c 61 73 74 45 72 72 6e 6f 29 20 nt, &lastErrno)
13465 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
13466 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 2f 2a SQLITE_ERROR; /*
13467 20 4e 6f 20 6d 6f 72 65 20 72 65 74 72 69 65 73 No more retries
13468 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65 . */. bre
13469 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
1346a 7d 20 77 68 69 6c 65 28 31 29 3b 0a 20 20 7d 0a } while(1);. }.
1346b 23 65 6e 64 69 66 0a 20 20 69 66 28 20 72 63 20 #endif. if( rc
1346c 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f && rc!=SQLITE_IO
1346d 45 52 52 5f 44 45 4c 45 54 45 5f 4e 4f 45 4e 54 ERR_DELETE_NOENT
1346e 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 77 69 6e ){. rc = win
1346f 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f LogError(SQLITE_
13470 49 4f 45 52 52 5f 44 45 4c 45 54 45 2c 20 6c 61 IOERR_DELETE, la
13471 73 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20 20 stErrno,.
13472 20 20 20 20 20 20 22 77 69 6e 44 65 6c 65 74 65 "winDelete
13473 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 ", zFilename);.
13474 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 67 49 }else{. logI
13475 6f 65 72 72 28 63 6e 74 29 3b 0a 20 20 7d 0a 20 oerr(cnt);. }.
13476 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 sqlite3_free(zC
13477 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 4f 53 54 onverted);. OST
13478 52 41 43 45 28 28 22 44 45 4c 45 54 45 20 5c 22 RACE(("DELETE \"
13479 25 73 5c 22 20 25 73 5c 6e 22 2c 20 7a 46 69 6c %s\" %s\n", zFil
1347a 65 6e 61 6d 65 2c 20 28 72 63 20 3f 20 22 66 61 ename, (rc ? "fa
1347b 69 6c 65 64 22 20 3a 20 22 6f 6b 22 20 29 29 29 iled" : "ok" )))
1347c 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1347d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 ../*.** Check th
1347e 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20 e existance and
1347f 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65 status of a file
13480 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
13481 77 69 6e 41 63 63 65 73 73 28 0a 20 20 73 71 6c winAccess(. sql
13482 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
13483 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 /* Not u
13484 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a sed on win32 */.
13485 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
13486 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 ilename, /*
13487 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Name of file to
13488 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 check */. int f
13489 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
1348a 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 /* Type of
1348b 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e test to make on
1348c 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 this file */.
1348d 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 int *pResOut
1348e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 /* OU
1348f 54 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a T: Result */.){.
13490 20 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 20 DWORD attr;.
13491 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 44 57 int rc = 0;. DW
13492 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 ORD lastErrno;.
13493 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 void *zConverte
13494 64 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 d;. UNUSED_PARA
13495 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 0a 20 20 METER(pVfs);..
13496 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
13497 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
13498 4f 45 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0a OERR_ACCESS; );.
13499 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 zConverted = c
1349a 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 onvertUtf8Filena
1349b 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 me(zFilename);.
1349c 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d if( zConverted=
1349d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
1349e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f SQLITE_IOERR_NO
1349f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 MEM;. }. if( i
134a0 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 69 6e 74 sNT() ){. int
134a1 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 57 49 cnt = 0;. WI
134a2 4e 33 32 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 N32_FILE_ATTRIBU
134a3 54 45 5f 44 41 54 41 20 73 41 74 74 72 44 61 74 TE_DATA sAttrDat
134a4 61 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 a;. memset(&s
134a5 41 74 74 72 44 61 74 61 2c 20 30 2c 20 73 69 7a AttrData, 0, siz
134a6 65 6f 66 28 73 41 74 74 72 44 61 74 61 29 29 3b eof(sAttrData));
134a7 0a 20 20 20 20 77 68 69 6c 65 28 20 21 28 72 63 . while( !(rc
134a8 20 3d 20 6f 73 47 65 74 46 69 6c 65 41 74 74 72 = osGetFileAttr
134a9 69 62 75 74 65 73 45 78 57 28 28 4c 50 43 57 53 ibutesExW((LPCWS
134aa 54 52 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 TR)zConverted,.
134ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
134ac 20 20 20 20 20 20 20 20 20 20 20 20 47 65 74 46 GetF
134ad 69 6c 65 45 78 49 6e 66 6f 53 74 61 6e 64 61 72 ileExInfoStandar
134ae 64 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 d, .
134af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
134b0 20 26 73 41 74 74 72 44 61 74 61 29 29 20 26 26 &sAttrData)) &&
134b1 20 72 65 74 72 79 49 6f 65 72 72 28 26 63 6e 74 retryIoerr(&cnt
134b2 2c 20 26 6c 61 73 74 45 72 72 6e 6f 29 20 29 7b , &lastErrno) ){
134b3 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a }. if( rc ){.
134b4 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 6e 20 /* For an
134b5 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
134b6 49 53 54 53 20 71 75 65 72 79 2c 20 74 72 65 61 ISTS query, trea
134b7 74 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 t a zero-length
134b8 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 73 file. ** as
134b9 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 if it does not
134ba 65 78 69 73 74 2e 0a 20 20 20 20 20 20 2a 2f 0a exist.. */.
134bb 20 20 20 20 20 20 69 66 28 20 20 20 20 66 6c 61 if( fla
134bc 67 73 3d 3d 53 51 4c 49 54 45 5f 41 43 43 45 53 gs==SQLITE_ACCES
134bd 53 5f 45 58 49 53 54 53 0a 20 20 20 20 20 20 20 S_EXISTS.
134be 20 20 20 26 26 20 73 41 74 74 72 44 61 74 61 2e && sAttrData.
134bf 6e 46 69 6c 65 53 69 7a 65 48 69 67 68 3d 3d 30 nFileSizeHigh==0
134c0 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 73 . && s
134c1 41 74 74 72 44 61 74 61 2e 6e 46 69 6c 65 53 69 AttrData.nFileSi
134c2 7a 65 4c 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 zeLow==0 ){.
134c3 20 20 20 20 61 74 74 72 20 3d 20 49 4e 56 41 4c attr = INVAL
134c4 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 ID_FILE_ATTRIBUT
134c5 45 53 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ES;. }else{
134c6 0a 20 20 20 20 20 20 20 20 61 74 74 72 20 3d 20 . attr =
134c7 73 41 74 74 72 44 61 74 61 2e 64 77 46 69 6c 65 sAttrData.dwFile
134c8 41 74 74 72 69 62 75 74 65 73 3b 0a 20 20 20 20 Attributes;.
134c9 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
134ca 20 20 20 20 20 6c 6f 67 49 6f 65 72 72 28 63 6e logIoerr(cn
134cb 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 61 t);. if( la
134cc 73 74 45 72 72 6e 6f 21 3d 45 52 52 4f 52 5f 46 stErrno!=ERROR_F
134cd 49 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 20 26 26 ILE_NOT_FOUND &&
134ce 20 6c 61 73 74 45 72 72 6e 6f 21 3d 45 52 52 4f lastErrno!=ERRO
134cf 52 5f 50 41 54 48 5f 4e 4f 54 5f 46 4f 55 4e 44 R_PATH_NOT_FOUND
134d0 20 29 7b 0a 20 20 20 20 20 20 20 20 77 69 6e 4c ){. winL
134d1 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 49 ogError(SQLITE_I
134d2 4f 45 52 52 5f 41 43 43 45 53 53 2c 20 6c 61 73 OERR_ACCESS, las
134d3 74 45 72 72 6e 6f 2c 20 22 77 69 6e 41 63 63 65 tErrno, "winAcce
134d4 73 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b ss", zFilename);
134d5 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
134d6 5f 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 _free(zConverted
134d7 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
134d8 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 n SQLITE_IOERR_A
134d9 43 43 45 53 53 3b 0a 20 20 20 20 20 20 7d 65 6c CCESS;. }el
134da 73 65 7b 0a 20 20 20 20 20 20 20 20 61 74 74 72 se{. attr
134db 20 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f = INVALID_FILE_
134dc 41 54 54 52 49 42 55 54 45 53 3b 0a 20 20 20 20 ATTRIBUTES;.
134dd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 }. }. }.#i
134de 66 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e 33 fdef SQLITE_WIN3
134df 32 5f 48 41 53 5f 41 4e 53 49 0a 20 20 65 6c 73 2_HAS_ANSI. els
134e0 65 7b 0a 20 20 20 20 61 74 74 72 20 3d 20 6f 73 e{. attr = os
134e1 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 GetFileAttribute
134e2 73 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 sA((char*)zConve
134e3 72 74 65 64 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 rted);. }.#endi
134e4 66 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 f. sqlite3_free
134e5 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 (zConverted);.
134e6 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b switch( flags ){
134e7 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
134e8 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 _ACCESS_READ:.
134e9 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 case SQLITE_AC
134ea 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 CESS_EXISTS:.
134eb 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d 49 4e rc = attr!=IN
134ec 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 VALID_FILE_ATTRI
134ed 42 55 54 45 53 3b 0a 20 20 20 20 20 20 62 72 65 BUTES;. bre
134ee 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ak;. case SQL
134ef 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 ITE_ACCESS_READW
134f0 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d RITE:. rc =
134f1 20 61 74 74 72 21 3d 49 4e 56 41 4c 49 44 5f 46 attr!=INVALID_F
134f2 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 20 26 ILE_ATTRIBUTES &
134f3 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28 &. (
134f4 61 74 74 72 20 26 20 46 49 4c 45 5f 41 54 54 52 attr & FILE_ATTR
134f5 49 42 55 54 45 5f 52 45 41 44 4f 4e 4c 59 29 3d IBUTE_READONLY)=
134f6 3d 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b =0;. break;
134f7 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 . default:.
134f8 20 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 assert(!"Inv
134f9 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d alid flags argum
134fa 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 ent");. }. *pR
134fb 65 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 esOut = rc;. re
134fc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
134fd 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e }.../*.** Return
134fe 73 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 s non-zero if th
134ff 65 20 73 70 65 63 69 66 69 65 64 20 70 61 74 68 e specified path
13500 20 6e 61 6d 65 20 73 68 6f 75 6c 64 20 62 65 20 name should be
13501 75 73 65 64 20 76 65 72 62 61 74 69 6d 2e 20 20 used verbatim.
13502 49 66 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 69 If.** non-zero i
13503 73 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 s returned from
13504 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 this function, t
13505 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
13506 69 6f 6e 20 6d 75 73 74 20 73 69 6d 70 6c 79 0a ion must simply.
13507 2a 2a 20 75 73 65 20 74 68 65 20 70 72 6f 76 69 ** use the provi
13508 64 65 64 20 70 61 74 68 20 6e 61 6d 65 20 76 65 ded path name ve
13509 72 62 61 74 69 6d 20 2d 4f 52 2d 20 72 65 73 6f rbatim -OR- reso
1350a 6c 76 65 20 69 74 20 69 6e 74 6f 20 61 20 66 75 lve it into a fu
1350b 6c 6c 20 70 61 74 68 20 6e 61 6d 65 0a 2a 2a 20 ll path name.**
1350c 75 73 69 6e 67 20 74 68 65 20 47 65 74 46 75 6c using the GetFul
1350d 6c 50 61 74 68 4e 61 6d 65 20 57 69 6e 33 32 20 lPathName Win32
1350e 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 28 69 66 API function (if
1350f 20 61 76 61 69 6c 61 62 6c 65 29 2e 0a 2a 2f 0a available)..*/.
13510 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 49 static BOOL winI
13511 73 56 65 72 62 61 74 69 6d 50 61 74 68 6e 61 6d sVerbatimPathnam
13512 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 e(. const char
13513 2a 7a 50 61 74 68 6e 61 6d 65 0a 29 7b 0a 20 20 *zPathname.){.
13514 2f 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 70 /*. ** If the p
13515 61 74 68 20 6e 61 6d 65 20 73 74 61 72 74 73 20 ath name starts
13516 77 69 74 68 20 61 20 66 6f 72 77 61 72 64 20 73 with a forward s
13517 6c 61 73 68 20 6f 72 20 61 20 62 61 63 6b 73 6c lash or a backsl
13518 61 73 68 2c 20 69 74 20 69 73 20 65 69 74 68 65 ash, it is eithe
13519 72 0a 20 20 2a 2a 20 61 20 6c 65 67 61 6c 20 55 r. ** a legal U
1351a 4e 43 20 6e 61 6d 65 2c 20 61 20 76 6f 6c 75 6d NC name, a volum
1351b 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 2c e relative path,
1351c 20 6f 72 20 61 6e 20 61 62 73 6f 6c 75 74 65 20 or an absolute
1351d 70 61 74 68 20 6e 61 6d 65 20 69 6e 20 74 68 65 path name in the
1351e 0a 20 20 2a 2a 20 22 55 6e 69 78 22 20 66 6f 72 . ** "Unix" for
1351f 6d 61 74 20 6f 6e 20 57 69 6e 64 6f 77 73 2e 20 mat on Windows.
13520 20 54 68 65 72 65 20 69 73 20 6e 6f 20 65 61 73 There is no eas
13521 79 20 77 61 79 20 74 6f 20 64 69 66 66 65 72 65 y way to differe
13522 6e 74 69 61 74 65 20 62 65 74 77 65 65 6e 0a 20 ntiate between.
13523 20 2a 2a 20 74 68 65 20 66 69 6e 61 6c 20 74 77 ** the final tw
13524 6f 20 63 61 73 65 73 3b 20 74 68 65 72 65 66 6f o cases; therefo
13525 72 65 2c 20 77 65 20 72 65 74 75 72 6e 20 74 68 re, we return th
13526 65 20 73 61 66 65 72 20 72 65 74 75 72 6e 20 76 e safer return v
13527 61 6c 75 65 20 6f 66 20 54 52 55 45 0a 20 20 2a alue of TRUE. *
13528 2a 20 73 6f 20 74 68 61 74 20 63 61 6c 6c 65 72 * so that caller
13529 73 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 s of this functi
1352a 6f 6e 20 77 69 6c 6c 20 73 69 6d 70 6c 79 20 75 on will simply u
1352b 73 65 20 69 74 20 76 65 72 62 61 74 69 6d 2e 0a se it verbatim..
1352c 20 20 2a 2f 0a 20 20 69 66 20 28 20 7a 50 61 74 */. if ( zPat
1352d 68 6e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 7c 7c hname[0]=='/' ||
1352e 20 7a 50 61 74 68 6e 61 6d 65 5b 30 5d 3d 3d 27 zPathname[0]=='
1352f 5c 5c 27 20 29 7b 0a 20 20 20 20 72 65 74 75 72 \\' ){. retur
13530 6e 20 54 52 55 45 3b 0a 20 20 7d 0a 0a 20 20 2f n TRUE;. }.. /
13531 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 70 61 *. ** If the pa
13532 74 68 20 6e 61 6d 65 20 73 74 61 72 74 73 20 77 th name starts w
13533 69 74 68 20 61 20 6c 65 74 74 65 72 20 61 6e 64 ith a letter and
13534 20 61 20 63 6f 6c 6f 6e 20 69 74 20 69 73 20 65 a colon it is e
13535 69 74 68 65 72 20 61 20 76 6f 6c 75 6d 65 0a 20 ither a volume.
13536 20 2a 2a 20 72 65 6c 61 74 69 76 65 20 70 61 74 ** relative pat
13537 68 20 6f 72 20 61 6e 20 61 62 73 6f 6c 75 74 65 h or an absolute
13538 20 70 61 74 68 2e 20 20 43 61 6c 6c 65 72 73 20 path. Callers
13539 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e of this function
1353a 20 6d 75 73 74 20 6e 6f 74 0a 20 20 2a 2a 20 61 must not. ** a
1353b 74 74 65 6d 70 74 20 74 6f 20 74 72 65 61 74 20 ttempt to treat
1353c 69 74 20 61 73 20 61 20 72 65 6c 61 74 69 76 65 it as a relative
1353d 20 70 61 74 68 20 6e 61 6d 65 20 28 69 2e 65 2e path name (i.e.
1353e 20 74 68 65 79 20 73 68 6f 75 6c 64 20 73 69 6d they should sim
1353f 70 6c 79 20 75 73 65 0a 20 20 2a 2a 20 69 74 20 ply use. ** it
13540 76 65 72 62 61 74 69 6d 29 2e 0a 20 20 2a 2f 0a verbatim).. */.
13541 20 20 69 66 20 28 20 73 71 6c 69 74 65 33 49 73 if ( sqlite3Is
13542 61 6c 70 68 61 28 7a 50 61 74 68 6e 61 6d 65 5b alpha(zPathname[
13543 30 5d 29 20 26 26 20 7a 50 61 74 68 6e 61 6d 65 0]) && zPathname
13544 5b 31 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 [1]==':' ){.
13545 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 7d return TRUE;. }
13546 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 77 .. /*. ** If w
13547 65 20 67 65 74 20 74 6f 20 74 68 69 73 20 70 6f e get to this po
13548 69 6e 74 2c 20 74 68 65 20 70 61 74 68 20 6e 61 int, the path na
13549 6d 65 20 73 68 6f 75 6c 64 20 61 6c 6d 6f 73 74 me should almost
1354a 20 63 65 72 74 61 69 6e 6c 79 20 62 65 20 61 20 certainly be a
1354b 70 75 72 65 6c 79 0a 20 20 2a 2a 20 72 65 6c 61 purely. ** rela
1354c 74 69 76 65 20 6f 6e 65 20 28 69 2e 65 2e 20 6e tive one (i.e. n
1354d 6f 74 20 61 20 55 4e 43 20 6e 61 6d 65 2c 20 6e ot a UNC name, n
1354e 6f 74 20 61 62 73 6f 6c 75 74 65 2c 20 61 6e 64 ot absolute, and
1354f 20 6e 6f 74 20 76 6f 6c 75 6d 65 20 72 65 6c 61 not volume rela
13550 74 69 76 65 29 2e 0a 20 20 2a 2f 0a 20 20 72 65 tive).. */. re
13551 74 75 72 6e 20 46 41 4c 53 45 3b 0a 7d 0a 0a 2f turn FALSE;.}../
13552 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 *.** Turn a rela
13553 74 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e tive pathname in
13554 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 to a full pathna
13555 6d 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 66 me. Write the f
13556 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 ull.** pathname
13557 69 6e 74 6f 20 7a 4f 75 74 5b 5d 2e 20 20 7a 4f into zOut[]. zO
13558 75 74 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74 20 ut[] will be at
13559 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50 61 least pVfs->mxPa
1355a 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20 thname.** bytes
1355b 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 in size..*/.stat
1355c 69 63 20 69 6e 74 20 77 69 6e 46 75 6c 6c 50 61 ic int winFullPa
1355d 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 thname(. sqlite
1355e 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 3_vfs *pVfs,
1355f 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
13560 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 er to vfs object
13561 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
13562 20 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20 *zRelative,
13563 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 /* Possibly
13564 72 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70 relative input p
13565 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 ath */. int nFu
13566 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ll,
13567 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
13568 66 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 f output buffer
13569 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 in bytes */. ch
1356a 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 ar *zFull
1356b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1356c 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a utput buffer */.
1356d 29 7b 0a 20 20 0a 23 69 66 20 64 65 66 69 6e 65 ){. .#if define
1356e 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 d(__CYGWIN__).
1356f 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
13570 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
13571 52 52 4f 52 20 29 3b 0a 20 20 55 4e 55 53 45 44 RROR );. UNUSED
13572 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c _PARAMETER(nFull
13573 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 66 );. assert( pVf
13574 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 3e 3d 4d s->mxPathname>=M
13575 41 58 5f 50 41 54 48 20 29 3b 0a 20 20 61 73 73 AX_PATH );. ass
13576 65 72 74 28 20 6e 46 75 6c 6c 3e 3d 70 56 66 73 ert( nFull>=pVfs
13577 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 29 3b 0a ->mxPathname );.
13578 20 20 69 66 20 28 20 73 71 6c 69 74 65 33 5f 64 if ( sqlite3_d
13579 61 74 61 5f 64 69 72 65 63 74 6f 72 79 20 26 26 ata_directory &&
1357a 20 21 77 69 6e 49 73 56 65 72 62 61 74 69 6d 50 !winIsVerbatimP
1357b 61 74 68 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76 athname(zRelativ
1357c 65 29 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 e) ){. /*.
1357d 20 2a 2a 20 4e 4f 54 45 3a 20 57 65 20 61 72 65 ** NOTE: We are
1357e 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 dealing with a
1357f 72 65 6c 61 74 69 76 65 20 70 61 74 68 20 6e 61 relative path na
13580 6d 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 0a me and the data.
13581 20 20 20 20 2a 2a 20 20 20 20 20 20 20 64 69 72 ** dir
13582 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e 20 ectory has been
13583 73 65 74 2e 20 20 54 68 65 72 65 66 6f 72 65 2c set. Therefore,
13584 20 75 73 65 20 69 74 20 61 73 20 74 68 65 20 62 use it as the b
13585 61 73 69 73 0a 20 20 20 20 2a 2a 20 20 20 20 20 asis. **
13586 20 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 for converting
13587 20 74 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 the relative pa
13588 74 68 20 6e 61 6d 65 20 74 6f 20 61 6e 20 61 62 th name to an ab
13589 73 6f 6c 75 74 65 0a 20 20 20 20 2a 2a 20 20 20 solute. **
1358a 20 20 20 20 6f 6e 65 20 62 79 20 70 72 65 70 65 one by prepe
1358b 6e 64 69 6e 67 20 74 68 65 20 64 61 74 61 20 64 nding the data d
1358c 69 72 65 63 74 6f 72 79 20 61 6e 64 20 61 20 73 irectory and a s
1358d 6c 61 73 68 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 lash.. */.
1358e 20 63 68 61 72 20 7a 4f 75 74 5b 4d 41 58 5f 50 char zOut[MAX_P
1358f 41 54 48 2b 31 5d 3b 0a 20 20 20 20 6d 65 6d 73 ATH+1];. mems
13590 65 74 28 7a 4f 75 74 2c 20 30 2c 20 4d 41 58 5f et(zOut, 0, MAX_
13591 50 41 54 48 2b 31 29 3b 0a 20 20 20 20 63 79 67 PATH+1);. cyg
13592 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 77 69 6e 33 win_conv_to_win3
13593 32 5f 70 61 74 68 28 7a 52 65 6c 61 74 69 76 65 2_path(zRelative
13594 2c 20 7a 4f 75 74 29 3b 20 2f 2a 20 50 4f 53 49 , zOut); /* POSI
13595 58 20 74 6f 20 57 69 6e 33 32 20 2a 2f 0a 20 20 X to Win32 */.
13596 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
13597 74 66 28 4d 49 4e 28 6e 46 75 6c 6c 2c 20 70 56 tf(MIN(nFull, pV
13598 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 29 2c fs->mxPathname),
13599 20 7a 46 75 6c 6c 2c 20 22 25 73 5c 5c 25 73 22 zFull, "%s\\%s"
1359a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1359b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 sqlite3_d
1359c 61 74 61 5f 64 69 72 65 63 74 6f 72 79 2c 20 7a ata_directory, z
1359d 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Out);. }else{.
1359e 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 4e 4f 54 /*. ** NOT
1359f 45 3a 20 54 68 65 20 43 79 67 77 69 6e 20 64 6f E: The Cygwin do
135a0 63 73 20 73 74 61 74 65 20 74 68 61 74 20 74 68 cs state that th
135a1 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 e maximum length
135a2 20 6e 65 65 64 65 64 0a 20 20 20 20 2a 2a 20 20 needed. **
135a3 20 20 20 20 20 66 6f 72 20 74 68 65 20 62 75 66 for the buf
135a4 66 65 72 20 70 61 73 73 65 64 20 74 6f 20 63 79 fer passed to cy
135a5 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75 6c gwin_conv_to_ful
135a6 6c 5f 77 69 6e 33 32 5f 70 61 74 68 0a 20 20 20 l_win32_path.
135a7 20 2a 2a 20 20 20 20 20 20 20 69 73 20 4d 41 58 ** is MAX
135a8 5f 50 41 54 48 2e 0a 20 20 20 20 2a 2f 0a 20 20 _PATH.. */.
135a9 20 20 63 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f cygwin_conv_to
135aa 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 68 _full_win32_path
135ab 28 7a 52 65 6c 61 74 69 76 65 2c 20 7a 46 75 6c (zRelative, zFul
135ac 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e l);. }. return
135ad 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 SQLITE_OK;.#end
135ae 69 66 0a 0a 23 69 66 20 28 53 51 4c 49 54 45 5f if..#if (SQLITE_
135af 4f 53 5f 57 49 4e 43 45 20 7c 7c 20 53 51 4c 49 OS_WINCE || SQLI
135b0 54 45 5f 4f 53 5f 57 49 4e 52 54 29 20 26 26 20 TE_OS_WINRT) &&
135b1 21 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 !defined(__CYGWI
135b2 4e 5f 5f 29 0a 20 20 53 69 6d 75 6c 61 74 65 49 N__). SimulateI
135b3 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
135b4 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 20 QLITE_ERROR );.
135b5 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 20 6e 6f /* WinCE has no
135b6 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 20 72 65 concept of a re
135b7 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 2c lative pathname,
135b8 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f 6c 64 or so I am told
135b9 2e 20 2a 2f 0a 20 20 2f 2a 20 57 69 6e 52 54 20 . */. /* WinRT
135ba 68 61 73 20 6e 6f 20 77 61 79 20 74 6f 20 63 6f has no way to co
135bb 6e 76 65 72 74 20 61 20 72 65 6c 61 74 69 76 65 nvert a relative
135bc 20 70 61 74 68 20 74 6f 20 61 6e 20 61 62 73 6f path to an abso
135bd 6c 75 74 65 20 6f 6e 65 2e 20 2a 2f 0a 20 20 69 lute one. */. i
135be 66 20 28 20 73 71 6c 69 74 65 33 5f 64 61 74 61 f ( sqlite3_data
135bf 5f 64 69 72 65 63 74 6f 72 79 20 26 26 20 21 77 _directory && !w
135c0 69 6e 49 73 56 65 72 62 61 74 69 6d 50 61 74 68 inIsVerbatimPath
135c1 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76 65 29 20 name(zRelative)
135c2 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a ){. /*. **
135c3 20 4e 4f 54 45 3a 20 57 65 20 61 72 65 20 64 65 NOTE: We are de
135c4 61 6c 69 6e 67 20 77 69 74 68 20 61 20 72 65 6c aling with a rel
135c5 61 74 69 76 65 20 70 61 74 68 20 6e 61 6d 65 20 ative path name
135c6 61 6e 64 20 74 68 65 20 64 61 74 61 0a 20 20 20 and the data.
135c7 20 2a 2a 20 20 20 20 20 20 20 64 69 72 65 63 74 ** direct
135c8 6f 72 79 20 68 61 73 20 62 65 65 6e 20 73 65 74 ory has been set
135c9 2e 20 20 54 68 65 72 65 66 6f 72 65 2c 20 75 73 . Therefore, us
135ca 65 20 69 74 20 61 73 20 74 68 65 20 62 61 73 69 e it as the basi
135cb 73 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 66 s. ** f
135cc 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 74 68 or converting th
135cd 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 20 e relative path
135ce 6e 61 6d 65 20 74 6f 20 61 6e 20 61 62 73 6f 6c name to an absol
135cf 75 74 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 ute. **
135d0 20 6f 6e 65 20 62 79 20 70 72 65 70 65 6e 64 69 one by prependi
135d1 6e 67 20 74 68 65 20 64 61 74 61 20 64 69 72 65 ng the data dire
135d2 63 74 6f 72 79 20 61 6e 64 20 61 20 62 61 63 6b ctory and a back
135d3 73 6c 61 73 68 2e 0a 20 20 20 20 2a 2f 0a 20 20 slash.. */.
135d4 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
135d5 74 66 28 4d 49 4e 28 6e 46 75 6c 6c 2c 20 70 56 tf(MIN(nFull, pV
135d6 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 29 2c fs->mxPathname),
135d7 20 7a 46 75 6c 6c 2c 20 22 25 73 5c 5c 25 73 22 zFull, "%s\\%s"
135d8 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
135d9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 sqlite3_d
135da 61 74 61 5f 64 69 72 65 63 74 6f 72 79 2c 20 7a ata_directory, z
135db 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 7d 65 6c Relative);. }el
135dc 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f se{. sqlite3_
135dd 73 6e 70 72 69 6e 74 66 28 4d 49 4e 28 6e 46 75 snprintf(MIN(nFu
135de 6c 6c 2c 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 ll, pVfs->mxPath
135df 6e 61 6d 65 29 2c 20 7a 46 75 6c 6c 2c 20 22 25 name), zFull, "%
135e0 73 22 2c 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a s", zRelative);.
135e1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
135e2 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a ITE_OK;.#endif..
135e3 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 #if !SQLITE_OS_W
135e4 49 4e 43 45 20 26 26 20 21 53 51 4c 49 54 45 5f INCE && !SQLITE_
135e5 4f 53 5f 57 49 4e 52 54 20 26 26 20 21 64 65 66 OS_WINRT && !def
135e6 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 ined(__CYGWIN__)
135e7 0a 20 20 44 57 4f 52 44 20 6e 42 79 74 65 3b 0a . DWORD nByte;.
135e8 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 void *zConvert
135e9 65 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 ed;. char *zOut
135ea 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 ;.. /* If this
135eb 70 61 74 68 20 6e 61 6d 65 20 62 65 67 69 6e 73 path name begins
135ec 20 77 69 74 68 20 22 2f 58 3a 22 2c 20 77 68 65 with "/X:", whe
135ed 72 65 20 22 58 22 20 69 73 20 61 6e 79 20 61 6c re "X" is any al
135ee 70 68 61 62 65 74 69 63 0a 20 20 2a 2a 20 63 68 phabetic. ** ch
135ef 61 72 61 63 74 65 72 2c 20 64 69 73 63 61 72 64 aracter, discard
135f0 20 74 68 65 20 69 6e 69 74 69 61 6c 20 22 2f 22 the initial "/"
135f1 20 66 72 6f 6d 20 74 68 65 20 70 61 74 68 6e 61 from the pathna
135f2 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a me.. */. if( z
135f3 52 65 6c 61 74 69 76 65 5b 30 5d 3d 3d 27 2f 27 Relative[0]=='/'
135f4 20 26 26 20 73 71 6c 69 74 65 33 49 73 61 6c 70 && sqlite3Isalp
135f5 68 61 28 7a 52 65 6c 61 74 69 76 65 5b 31 5d 29 ha(zRelative[1])
135f6 20 26 26 20 7a 52 65 6c 61 74 69 76 65 5b 32 5d && zRelative[2]
135f7 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 7a 52 65 ==':' ){. zRe
135f8 6c 61 74 69 76 65 2b 2b 3b 0a 20 20 7d 0a 0a 20 lative++;. }..
135f9 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 /* It's odd to
135fa 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 simulate an io-e
135fb 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 rror here, but r
135fc 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 eally this is ju
135fd 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 st. ** using th
135fe 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 e io-error infra
135ff 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 structure to tes
13600 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 t that SQLite ha
13601 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 ndles this. **
13602 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 function failing
13603 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
13604 63 6f 75 6c 64 20 66 61 69 6c 20 69 66 2c 20 66 could fail if, f
13605 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 0a or example, the.
13606 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 77 6f 72 ** current wor
13607 6b 69 6e 67 20 64 69 72 65 63 74 6f 72 79 20 68 king directory h
13608 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 as been unlinked
13609 2e 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 .. */. Simulat
1360a 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e eIOError( return
1360b 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b SQLITE_ERROR );
1360c 0a 20 20 69 66 20 28 20 73 71 6c 69 74 65 33 5f . if ( sqlite3_
1360d 64 61 74 61 5f 64 69 72 65 63 74 6f 72 79 20 26 data_directory &
1360e 26 20 21 77 69 6e 49 73 56 65 72 62 61 74 69 6d & !winIsVerbatim
1360f 50 61 74 68 6e 61 6d 65 28 7a 52 65 6c 61 74 69 Pathname(zRelati
13610 76 65 29 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 ve) ){. /*.
13611 20 20 2a 2a 20 4e 4f 54 45 3a 20 57 65 20 61 72 ** NOTE: We ar
13612 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 e dealing with a
13613 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 20 6e relative path n
13614 61 6d 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 ame and the data
13615 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 64 69 . ** di
13616 72 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e rectory has been
13617 20 73 65 74 2e 20 20 54 68 65 72 65 66 6f 72 65 set. Therefore
13618 2c 20 75 73 65 20 69 74 20 61 73 20 74 68 65 20 , use it as the
13619 62 61 73 69 73 0a 20 20 20 20 2a 2a 20 20 20 20 basis. **
1361a 20 20 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e for convertin
1361b 67 20 74 68 65 20 72 65 6c 61 74 69 76 65 20 70 g the relative p
1361c 61 74 68 20 6e 61 6d 65 20 74 6f 20 61 6e 20 61 ath name to an a
1361d 62 73 6f 6c 75 74 65 0a 20 20 20 20 2a 2a 20 20 bsolute. **
1361e 20 20 20 20 20 6f 6e 65 20 62 79 20 70 72 65 70 one by prep
1361f 65 6e 64 69 6e 67 20 74 68 65 20 64 61 74 61 20 ending the data
13620 64 69 72 65 63 74 6f 72 79 20 61 6e 64 20 61 20 directory and a
13621 62 61 63 6b 73 6c 61 73 68 2e 0a 20 20 20 20 2a backslash.. *
13622 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e /. sqlite3_sn
13623 70 72 69 6e 74 66 28 4d 49 4e 28 6e 46 75 6c 6c printf(MIN(nFull
13624 2c 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 , pVfs->mxPathna
13625 6d 65 29 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 5c me), zFull, "%s\
13626 5c 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 \%s",.
13627 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
13628 65 33 5f 64 61 74 61 5f 64 69 72 65 63 74 6f 72 e3_data_director
13629 79 2c 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20 y, zRelative);.
1362a 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1362b 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 7a 43 6f 6e 76 _OK;. }. zConv
1362c 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 erted = convertU
1362d 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 52 65 6c tf8Filename(zRel
1362e 61 74 69 76 65 29 3b 0a 20 20 69 66 28 20 7a 43 ative);. if( zC
1362f 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 onverted==0 ){.
13630 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
13631 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 _IOERR_NOMEM;.
13632 7d 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 }. if( isNT() )
13633 7b 0a 20 20 20 20 4c 50 57 53 54 52 20 7a 54 65 {. LPWSTR zTe
13634 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 mp;. nByte =
13635 6f 73 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d osGetFullPathNam
13636 65 57 28 28 4c 50 43 57 53 54 52 29 7a 43 6f 6e eW((LPCWSTR)zCon
13637 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 verted, 0, 0, 0)
13638 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d ;. if( nByte=
13639 3d 30 20 29 7b 0a 20 20 20 20 20 20 77 69 6e 4c =0 ){. winL
1363a 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 45 ogError(SQLITE_E
1363b 52 52 4f 52 2c 20 6f 73 47 65 74 4c 61 73 74 45 RROR, osGetLastE
1363c 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20 20 rror(),.
1363d 20 20 20 20 20 20 20 20 20 20 22 47 65 74 46 75 "GetFu
1363e 6c 6c 50 61 74 68 4e 61 6d 65 57 31 22 2c 20 7a llPathNameW1", z
1363f 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
13640 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
13641 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
13642 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
13643 43 41 4e 54 4f 50 45 4e 5f 46 55 4c 4c 50 41 54 CANTOPEN_FULLPAT
13644 48 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 42 79 H;. }. nBy
13645 74 65 20 2b 3d 20 33 3b 0a 20 20 20 20 7a 54 65 te += 3;. zTe
13646 6d 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c mp = sqlite3Mall
13647 6f 63 5a 65 72 6f 28 20 6e 42 79 74 65 2a 73 69 ocZero( nByte*si
13648 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 zeof(zTemp[0]) )
13649 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d ;. if( zTemp=
1364a 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
1364b 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e 76 65 72 te3_free(zConver
1364c 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ted);. retu
1364d 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
1364e 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 NOMEM;. }.
1364f 20 6e 42 79 74 65 20 3d 20 6f 73 47 65 74 46 75 nByte = osGetFu
13650 6c 6c 50 61 74 68 4e 61 6d 65 57 28 28 4c 50 43 llPathNameW((LPC
13651 57 53 54 52 29 7a 43 6f 6e 76 65 72 74 65 64 2c WSTR)zConverted,
13652 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 nByte, zTemp, 0
13653 29 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 );. if( nByte
13654 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 77 69 6e ==0 ){. win
13655 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f LogError(SQLITE_
13656 45 52 52 4f 52 2c 20 6f 73 47 65 74 4c 61 73 74 ERROR, osGetLast
13657 45 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20 Error(),.
13658 20 20 20 20 20 20 20 20 20 20 20 22 47 65 74 46 "GetF
13659 75 6c 6c 50 61 74 68 4e 61 6d 65 57 32 22 2c 20 ullPathNameW2",
1365a 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 zConverted);.
1365b 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
1365c 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 zConverted);.
1365d 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
1365e 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 72 65 zTemp);. re
1365f 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 turn SQLITE_CANT
13660 4f 50 45 4e 5f 46 55 4c 4c 50 41 54 48 3b 0a 20 OPEN_FULLPATH;.
13661 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
13662 5f 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 _free(zConverted
13663 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75 6e );. zOut = un
13664 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 6d icodeToUtf8(zTem
13665 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f p);. sqlite3_
13666 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20 7d free(zTemp);. }
13667 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 57 .#ifdef SQLITE_W
13668 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20 20 IN32_HAS_ANSI.
13669 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a else{. char *
1366a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 zTemp;. nByte
1366b 20 3d 20 6f 73 47 65 74 46 75 6c 6c 50 61 74 68 = osGetFullPath
1366c 4e 61 6d 65 41 28 28 63 68 61 72 2a 29 7a 43 6f NameA((char*)zCo
1366d 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 nverted, 0, 0, 0
1366e 29 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 );. if( nByte
1366f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 77 69 6e ==0 ){. win
13670 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f LogError(SQLITE_
13671 45 52 52 4f 52 2c 20 6f 73 47 65 74 4c 61 73 74 ERROR, osGetLast
13672 45 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20 Error(),.
13673 20 20 20 20 20 20 20 20 20 20 20 22 47 65 74 46 "GetF
13674 75 6c 6c 50 61 74 68 4e 61 6d 65 41 31 22 2c 20 ullPathNameA1",
13675 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 zConverted);.
13676 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
13677 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 zConverted);.
13678 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
13679 5f 43 41 4e 54 4f 50 45 4e 5f 46 55 4c 4c 50 41 _CANTOPEN_FULLPA
1367a 54 48 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 42 TH;. }. nB
1367b 79 74 65 20 2b 3d 20 33 3b 0a 20 20 20 20 7a 54 yte += 3;. zT
1367c 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c emp = sqlite3Mal
1367d 6c 6f 63 5a 65 72 6f 28 20 6e 42 79 74 65 2a 73 locZero( nByte*s
1367e 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 izeof(zTemp[0])
1367f 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 );. if( zTemp
13680 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
13681 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e 76 65 ite3_free(zConve
13682 72 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 rted);. ret
13683 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
13684 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
13685 20 20 6e 42 79 74 65 20 3d 20 6f 73 47 65 74 46 nByte = osGetF
13686 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 ullPathNameA((ch
13687 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 ar*)zConverted,
13688 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 nByte, zTemp, 0)
13689 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d ;. if( nByte=
1368a 3d 30 20 29 7b 0a 20 20 20 20 20 20 77 69 6e 4c =0 ){. winL
1368b 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 45 ogError(SQLITE_E
1368c 52 52 4f 52 2c 20 6f 73 47 65 74 4c 61 73 74 45 RROR, osGetLastE
1368d 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20 20 rror(),.
1368e 20 20 20 20 20 20 20 20 20 20 22 47 65 74 46 75 "GetFu
1368f 6c 6c 50 61 74 68 4e 61 6d 65 41 32 22 2c 20 7a llPathNameA2", z
13690 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
13691 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
13692 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
13693 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
13694 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 72 65 74 Temp);. ret
13695 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f urn SQLITE_CANTO
13696 50 45 4e 5f 46 55 4c 4c 50 41 54 48 3b 0a 20 20 PEN_FULLPATH;.
13697 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
13698 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 free(zConverted)
13699 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c ;. zOut = sql
1369a 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63 73 5f ite3_win32_mbcs_
1369b 74 6f 5f 75 74 66 38 28 7a 54 65 6d 70 29 3b 0a to_utf8(zTemp);.
1369c 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
1369d 28 7a 54 65 6d 70 29 3b 0a 20 20 7d 0a 23 65 6e (zTemp);. }.#en
1369e 64 69 66 0a 20 20 69 66 28 20 7a 4f 75 74 20 29 dif. if( zOut )
1369f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e {. sqlite3_sn
136a0 70 72 69 6e 74 66 28 4d 49 4e 28 6e 46 75 6c 6c printf(MIN(nFull
136a1 2c 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 , pVfs->mxPathna
136a2 6d 65 29 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22 me), zFull, "%s"
136a3 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 73 71 6c , zOut);. sql
136a4 69 74 65 33 5f 66 72 65 65 28 7a 4f 75 74 29 3b ite3_free(zOut);
136a5 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
136a6 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
136a7 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
136a8 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 E_IOERR_NOMEM;.
136a9 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 23 69 66 }.#endif.}..#if
136aa 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
136ab 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a _LOAD_EXTENSION.
136ac 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 /*.** Interfaces
136ad 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 for opening a s
136ae 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 hared library, f
136af 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 inding entry poi
136b0 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 nts.** within th
136b1 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 e shared library
136b2 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 , and closing th
136b3 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 e shared library
136b4 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 ..*/./*.** Inter
136b5 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e faces for openin
136b6 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 g a shared libra
136b7 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 ry, finding entr
136b8 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 y points.** with
136b9 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 in the shared li
136ba 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 brary, and closi
136bb 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 ng the shared li
136bc 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 brary..*/.static
136bd 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f 70 65 6e void *winDlOpen
136be 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
136bf 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a fs, const char *
136c0 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 41 zFilename){. HA
136c1 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a NDLE h;. void *
136c2 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e zConverted = con
136c3 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 vertUtf8Filename
136c4 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 (zFilename);. U
136c5 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
136c6 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f pVfs);. if( zCo
136c7 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 nverted==0 ){.
136c8 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
136c9 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a if( isNT() ){.
136ca 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
136cb 4e 52 54 0a 20 20 20 20 68 20 3d 20 6f 73 4c 6f NRT. h = osLo
136cc 61 64 50 61 63 6b 61 67 65 64 4c 69 62 72 61 72 adPackagedLibrar
136cd 79 28 28 4c 50 43 57 53 54 52 29 7a 43 6f 6e 76 y((LPCWSTR)zConv
136ce 65 72 74 65 64 2c 20 30 29 3b 0a 23 65 6c 73 65 erted, 0);.#else
136cf 0a 20 20 20 20 68 20 3d 20 6f 73 4c 6f 61 64 4c . h = osLoadL
136d0 69 62 72 61 72 79 57 28 28 4c 50 43 57 53 54 52 ibraryW((LPCWSTR
136d1 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 )zConverted);.#e
136d2 6e 64 69 66 0a 20 20 7d 0a 23 69 66 64 65 66 20 ndif. }.#ifdef
136d3 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 SQLITE_WIN32_HAS
136d4 5f 41 4e 53 49 0a 20 20 65 6c 73 65 7b 0a 20 20 _ANSI. else{.
136d5 20 20 68 20 3d 20 6f 73 4c 6f 61 64 4c 69 62 72 h = osLoadLibr
136d6 61 72 79 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e aryA((char*)zCon
136d7 76 65 72 74 65 64 29 3b 0a 20 20 7d 0a 23 65 6e verted);. }.#en
136d8 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 dif. sqlite3_fr
136d9 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a ee(zConverted);.
136da 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 return (void*)
136db 68 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 h;.}.static void
136dc 20 77 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c 69 winDlError(sqli
136dd 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
136de 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
136df 42 75 66 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45 BufOut){. UNUSE
136e0 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 D_PARAMETER(pVfs
136e1 29 3b 0a 20 20 67 65 74 4c 61 73 74 45 72 72 6f );. getLastErro
136e2 72 4d 73 67 28 6f 73 47 65 74 4c 61 73 74 45 72 rMsg(osGetLastEr
136e3 72 6f 72 28 29 2c 20 6e 42 75 66 2c 20 7a 42 75 ror(), nBuf, zBu
136e4 66 4f 75 74 29 3b 0a 7d 0a 73 74 61 74 69 63 20 fOut);.}.static
136e5 76 6f 69 64 20 28 2a 77 69 6e 44 6c 53 79 6d 28 void (*winDlSym(
136e6 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
136e7 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 s, void *pHandle
136e8 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 , const char *zS
136e9 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 7b 0a 20 ymbol))(void){.
136ea 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
136eb 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 72 R(pVfs);. retur
136ec 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 n (void(*)(void)
136ed 29 6f 73 47 65 74 50 72 6f 63 41 64 64 72 65 73 )osGetProcAddres
136ee 73 41 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 sA((HANDLE)pHand
136ef 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 7d 0a le, zSymbol);.}.
136f0 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 44 static void winD
136f1 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 lClose(sqlite3_v
136f2 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a fs *pVfs, void *
136f3 70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 pHandle){. UNUS
136f4 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 ED_PARAMETER(pVf
136f5 73 29 3b 0a 20 20 6f 73 46 72 65 65 4c 69 62 72 s);. osFreeLibr
136f6 61 72 79 28 28 48 41 4e 44 4c 45 29 70 48 61 6e ary((HANDLE)pHan
136f7 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a dle);.}.#else /*
136f8 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f if SQLITE_OMIT_
136f9 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 LOAD_EXTENSION i
136fa 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 s defined: */.
136fb 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 4f 70 65 #define winDlOpe
136fc 6e 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 n 0. #define w
136fd 69 6e 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 inDlError 0. #d
136fe 65 66 69 6e 65 20 77 69 6e 44 6c 53 79 6d 20 20 efine winDlSym
136ff 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 0. #define win
13700 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 DlClose 0.#endif
13701 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75 .../*.** Write u
13702 70 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73 20 p to nBuf bytes
13703 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e of randomness in
13704 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 to zBuf..*/.stat
13705 69 63 20 69 6e 74 20 77 69 6e 52 61 6e 64 6f 6d ic int winRandom
13706 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 ness(sqlite3_vfs
13707 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 *pVfs, int nBuf
13708 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 , char *zBuf){.
13709 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 55 4e int n = 0;. UN
1370a 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
1370b 56 66 73 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 Vfs);.#if define
1370c 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 d(SQLITE_TEST).
1370d 20 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d n = nBuf;. mem
1370e 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 set(zBuf, 0, nBu
1370f 66 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 f);.#else. if(
13710 73 69 7a 65 6f 66 28 53 59 53 54 45 4d 54 49 4d sizeof(SYSTEMTIM
13711 45 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 E)<=nBuf-n ){.
13712 20 20 53 59 53 54 45 4d 54 49 4d 45 20 78 3b 0a SYSTEMTIME x;.
13713 20 20 20 20 6f 73 47 65 74 53 79 73 74 65 6d 54 osGetSystemT
13714 69 6d 65 28 26 78 29 3b 0a 20 20 20 20 6d 65 6d ime(&x);. mem
13715 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 cpy(&zBuf[n], &x
13716 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 , sizeof(x));.
13717 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78 29 n += sizeof(x)
13718 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 ;. }. if( size
13719 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d of(DWORD)<=nBuf-
1371a 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 70 n ){. DWORD p
1371b 69 64 20 3d 20 6f 73 47 65 74 43 75 72 72 65 6e id = osGetCurren
1371c 74 50 72 6f 63 65 73 73 49 64 28 29 3b 0a 20 20 tProcessId();.
1371d 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
1371e 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 ], &pid, sizeof(
1371f 70 69 64 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 pid));. n +=
13720 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20 20 7d sizeof(pid);. }
13721 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
13722 49 4e 52 54 0a 20 20 69 66 28 20 73 69 7a 65 6f INRT. if( sizeo
13723 66 28 55 4c 4f 4e 47 4c 4f 4e 47 29 3c 3d 6e 42 f(ULONGLONG)<=nB
13724 75 66 2d 6e 20 29 7b 0a 20 20 20 20 55 4c 4f 4e uf-n ){. ULON
13725 47 4c 4f 4e 47 20 63 6e 74 20 3d 20 6f 73 47 65 GLONG cnt = osGe
13726 74 54 69 63 6b 43 6f 75 6e 74 36 34 28 29 3b 0a tTickCount64();.
13727 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 memcpy(&zBuf
13728 5b 6e 5d 2c 20 26 63 6e 74 2c 20 73 69 7a 65 6f [n], &cnt, sizeo
13729 66 28 63 6e 74 29 29 3b 0a 20 20 20 20 6e 20 2b f(cnt));. n +
1372a 3d 20 73 69 7a 65 6f 66 28 63 6e 74 29 3b 0a 20 = sizeof(cnt);.
1372b 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 73 }.#else. if( s
1372c 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42 izeof(DWORD)<=nB
1372d 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52 uf-n ){. DWOR
1372e 44 20 63 6e 74 20 3d 20 6f 73 47 65 74 54 69 63 D cnt = osGetTic
1372f 6b 43 6f 75 6e 74 28 29 3b 0a 20 20 20 20 6d 65 kCount();. me
13730 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 mcpy(&zBuf[n], &
13731 63 6e 74 2c 20 73 69 7a 65 6f 66 28 63 6e 74 29 cnt, sizeof(cnt)
13732 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 );. n += size
13733 6f 66 28 63 6e 74 29 3b 0a 20 20 7d 0a 23 65 6e of(cnt);. }.#en
13734 64 69 66 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 dif. if( sizeof
13735 28 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 29 3c (LARGE_INTEGER)<
13736 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 4c =nBuf-n ){. L
13737 41 52 47 45 5f 49 4e 54 45 47 45 52 20 69 3b 0a ARGE_INTEGER i;.
13738 20 20 20 20 6f 73 51 75 65 72 79 50 65 72 66 6f osQueryPerfo
13739 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 28 26 69 rmanceCounter(&i
1373a 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a );. memcpy(&z
1373b 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 69 7a 65 Buf[n], &i, size
1373c 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e 20 2b 3d of(i));. n +=
1373d 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 20 7d 0a sizeof(i);. }.
1373e 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
1373f 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 n;.}.../*.** Sle
13740 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 ep for a little
13741 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 while. Return t
13742 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d he amount of tim
13743 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73 74 61 74 e slept..*/.stat
13744 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 65 70 28 ic int winSleep(
13745 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
13746 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 29 s, int microsec)
13747 7b 0a 20 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 {. sqlite3_win3
13748 32 5f 73 6c 65 65 70 28 28 6d 69 63 72 6f 73 65 2_sleep((microse
13749 63 2b 39 39 39 29 2f 31 30 30 30 29 3b 0a 20 20 c+999)/1000);.
1374a 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1374b 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 72 6e (pVfs);. return
1374c 20 28 28 6d 69 63 72 6f 73 65 63 2b 39 39 39 29 ((microsec+999)
1374d 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a 7d 0a 0a /1000)*1000;.}..
1374e 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
1374f 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 ing variable, if
13750 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 set to a non-ze
13751 72 6f 20 76 61 6c 75 65 2c 20 69 73 20 69 6e 74 ro value, is int
13752 65 72 70 72 65 74 65 64 20 61 73 0a 2a 2a 20 74 erpreted as.** t
13753 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 he number of sec
13754 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 20 onds since 1970
13755 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 73 and is used to s
13756 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 et the result of
13757 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 43 75 72 .** sqlite3OsCur
13758 72 65 6e 74 54 69 6d 65 28 29 20 64 75 72 69 6e rentTime() durin
13759 67 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 g testing..*/.#i
1375a 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
1375b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1375c 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f sqlite3_current_
1375d 74 69 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 time = 0; /* Fa
1375e 6b 65 20 73 79 73 74 65 6d 20 74 69 6d 65 20 69 ke system time i
1375f 6e 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 n seconds since
13760 31 39 37 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 1970. */.#endif.
13761 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 ./*.** Find the
13762 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e current time (in
13763 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 Universal Coord
13764 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 inated Time). W
13765 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 4e 6f 77 rite into *piNow
13766 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 .** the current
13767 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73 time and date as
13768 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 a Julian Day nu
13769 6d 62 65 72 20 74 69 6d 65 73 20 38 36 5f 34 30 mber times 86_40
1376a 30 5f 30 30 30 2e 20 20 49 6e 0a 2a 2a 20 6f 74 0_000. In.** ot
1376b 68 65 72 20 77 6f 72 64 73 2c 20 77 72 69 74 65 her words, write
1376c 20 69 6e 74 6f 20 2a 70 69 4e 6f 77 20 74 68 65 into *piNow the
1376d 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 number of milli
1376e 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 74 68 seconds since th
1376f 65 20 4a 75 6c 69 61 6e 0a 2a 2a 20 65 70 6f 63 e Julian.** epoc
13770 68 20 6f 66 20 6e 6f 6f 6e 20 69 6e 20 47 72 65 h of noon in Gre
13771 65 6e 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 enwich on Novemb
13772 65 72 20 32 34 2c 20 34 37 31 34 20 42 2e 43 20 er 24, 4714 B.C
13773 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 according to the
13774 0a 2a 2a 20 70 72 6f 6c 65 70 74 69 63 20 47 72 .** proleptic Gr
13775 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 egorian calendar
13776 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 ..**.** On succe
13777 73 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ss, return SQLIT
13778 45 5f 4f 4b 2e 20 20 52 65 74 75 72 6e 20 53 51 E_OK. Return SQ
13779 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68 LITE_ERROR if th
1377a 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 e time and date
1377b 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f .** cannot be fo
1377c 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 und..*/.static i
1377d 6e 74 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d nt winCurrentTim
1377e 65 49 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 eInt64(sqlite3_v
1377f 66 73 20 2a 70 56 66 73 2c 20 73 71 6c 69 74 65 fs *pVfs, sqlite
13780 33 5f 69 6e 74 36 34 20 2a 70 69 4e 6f 77 29 7b 3_int64 *piNow){
13781 0a 20 20 2f 2a 20 46 49 4c 45 54 49 4d 45 20 73 . /* FILETIME s
13782 74 72 75 63 74 75 72 65 20 69 73 20 61 20 36 34 tructure is a 64
13783 2d 62 69 74 20 76 61 6c 75 65 20 72 65 70 72 65 -bit value repre
13784 73 65 6e 74 69 6e 67 20 74 68 65 20 6e 75 6d 62 senting the numb
13785 65 72 20 6f 66 20 0a 20 20 20 20 20 31 30 30 2d er of . 100-
13786 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65 72 nanosecond inter
13787 76 61 6c 73 20 73 69 6e 63 65 20 4a 61 6e 75 61 vals since Janua
13788 72 79 20 31 2c 20 31 36 30 31 20 28 3d 20 4a 44 ry 1, 1601 (= JD
13789 20 32 33 30 35 38 31 33 2e 35 29 2e 20 0a 20 20 2305813.5). .
1378a 2a 2f 0a 20 20 46 49 4c 45 54 49 4d 45 20 66 74 */. FILETIME ft
1378b 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 ;. static const
1378c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 77 sqlite3_int64 w
1378d 69 6e 46 69 6c 65 74 69 6d 65 45 70 6f 63 68 20 inFiletimeEpoch
1378e 3d 20 32 33 30 35 38 31 33 35 2a 28 73 71 6c 69 = 23058135*(sqli
1378f 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30 30 30 te3_int64)864000
13790 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 0;.#ifdef SQLITE
13791 5f 54 45 53 54 0a 20 20 73 74 61 74 69 63 20 63 _TEST. static c
13792 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 onst sqlite3_int
13793 36 34 20 75 6e 69 78 45 70 6f 63 68 20 3d 20 32 64 unixEpoch = 2
13794 34 34 30 35 38 37 35 2a 28 73 71 6c 69 74 65 33 4405875*(sqlite3
13795 5f 69 6e 74 36 34 29 38 36 34 30 30 30 30 3b 0a _int64)8640000;.
13796 23 65 6e 64 69 66 0a 20 20 2f 2a 20 32 5e 33 32 #endif. /* 2^32
13797 20 2d 20 74 6f 20 61 76 6f 69 64 20 75 73 65 20 - to avoid use
13798 6f 66 20 4c 4c 20 61 6e 64 20 77 61 72 6e 69 6e of LL and warnin
13799 67 73 20 69 6e 20 67 63 63 20 2a 2f 0a 20 20 73 gs in gcc */. s
1379a 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
1379b 74 65 33 5f 69 6e 74 36 34 20 6d 61 78 33 32 42 te3_int64 max32B
1379c 69 74 56 61 6c 75 65 20 3d 20 0a 20 20 20 20 20 itValue = .
1379d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
1379e 32 30 30 30 30 30 30 30 30 30 20 2b 20 28 73 71 2000000000 + (sq
1379f 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 30 30 30 lite3_int64)2000
137a0 30 30 30 30 30 30 20 2b 20 28 73 71 6c 69 74 65 000000 + (sqlite
137a1 33 5f 69 6e 74 36 34 29 32 39 34 39 36 37 32 39 3_int64)29496729
137a2 36 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 6;..#if SQLITE_O
137a3 53 5f 57 49 4e 43 45 0a 20 20 53 59 53 54 45 4d S_WINCE. SYSTEM
137a4 54 49 4d 45 20 74 69 6d 65 3b 0a 20 20 6f 73 47 TIME time;. osG
137a5 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 74 69 etSystemTime(&ti
137a6 6d 65 29 3b 0a 20 20 2f 2a 20 69 66 20 53 79 73 me);. /* if Sys
137a7 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d temTimeToFileTim
137a8 65 28 29 20 66 61 69 6c 73 2c 20 69 74 20 72 65 e() fails, it re
137a9 74 75 72 6e 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20 turns zero. */.
137aa 20 69 66 20 28 21 6f 73 53 79 73 74 65 6d 54 69 if (!osSystemTi
137ab 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28 26 74 69 meToFileTime(&ti
137ac 6d 65 2c 26 66 74 29 29 7b 0a 20 20 20 20 72 65 me,&ft)){. re
137ad 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
137ae 52 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 6f R;. }.#else. o
137af 73 47 65 74 53 79 73 74 65 6d 54 69 6d 65 41 73 sGetSystemTimeAs
137b0 46 69 6c 65 54 69 6d 65 28 20 26 66 74 20 29 3b FileTime( &ft );
137b1 0a 23 65 6e 64 69 66 0a 0a 20 20 2a 70 69 4e 6f .#endif.. *piNo
137b2 77 20 3d 20 77 69 6e 46 69 6c 65 74 69 6d 65 45 w = winFiletimeE
137b3 70 6f 63 68 20 2b 0a 20 20 20 20 20 20 20 20 20 poch +.
137b4 20 20 20 28 28 28 28 73 71 6c 69 74 65 33 5f 69 ((((sqlite3_i
137b5 6e 74 36 34 29 66 74 2e 64 77 48 69 67 68 44 61 nt64)ft.dwHighDa
137b6 74 65 54 69 6d 65 29 2a 6d 61 78 33 32 42 69 74 teTime)*max32Bit
137b7 56 61 6c 75 65 29 20 2b 20 0a 20 20 20 20 20 20 Value) + .
137b8 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 (sqlite
137b9 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 4c 6f 77 3_int64)ft.dwLow
137ba 44 61 74 65 54 69 6d 65 29 2f 28 73 71 6c 69 74 DateTime)/(sqlit
137bb 65 33 5f 69 6e 74 36 34 29 31 30 30 30 30 3b 0a e3_int64)10000;.
137bc 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
137bd 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 EST. if( sqlite
137be 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 3_current_time )
137bf 7b 0a 20 20 20 20 2a 70 69 4e 6f 77 20 3d 20 31 {. *piNow = 1
137c0 30 30 30 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 000*(sqlite3_int
137c1 36 34 29 73 71 6c 69 74 65 33 5f 63 75 72 72 65 64)sqlite3_curre
137c2 6e 74 5f 74 69 6d 65 20 2b 20 75 6e 69 78 45 70 nt_time + unixEp
137c3 6f 63 68 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a och;. }.#endif.
137c4 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
137c5 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 ER(pVfs);. retu
137c6 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
137c7 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 ./*.** Find the
137c8 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e current time (in
137c9 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 Universal Coord
137ca 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 inated Time). W
137cb 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 rite the.** curr
137cc 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 ent time and dat
137cd 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 e as a Julian Da
137ce 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 y number into *p
137cf 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 rNow and.** retu
137d0 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 rn 0. Return 1
137d1 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 if the time and
137d2 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 date cannot be f
137d3 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ound..*/.static
137d4 69 6e 74 20 77 69 6e 43 75 72 72 65 6e 74 54 69 int winCurrentTi
137d5 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a me(sqlite3_vfs *
137d6 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72 pVfs, double *pr
137d7 4e 6f 77 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Now){. int rc;.
137d8 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
137d9 69 3b 0a 20 20 72 63 20 3d 20 77 69 6e 43 75 72 i;. rc = winCur
137da 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 70 56 rentTimeInt64(pV
137db 66 73 2c 20 26 69 29 3b 0a 20 20 69 66 28 20 21 fs, &i);. if( !
137dc 72 63 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 rc ){. *prNow
137dd 20 3d 20 69 2f 38 36 34 30 30 30 30 30 2e 30 3b = i/86400000.0;
137de 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
137df 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 ;.}../*.** The i
137e0 64 65 61 20 69 73 20 74 68 61 74 20 74 68 69 73 dea is that this
137e1 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20 function works
137e2 6c 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61 74 69 like a combinati
137e3 6f 6e 20 6f 66 0a 2a 2a 20 47 65 74 4c 61 73 74 on of.** GetLast
137e4 45 72 72 6f 72 28 29 20 61 6e 64 20 46 6f 72 6d Error() and Form
137e5 61 74 4d 65 73 73 61 67 65 28 29 20 6f 6e 20 57 atMessage() on W
137e6 69 6e 64 6f 77 73 20 28 6f 72 20 65 72 72 6e 6f indows (or errno
137e7 20 61 6e 64 0a 2a 2a 20 73 74 72 65 72 72 6f 72 and.** strerror
137e8 5f 72 28 29 20 6f 6e 20 55 6e 69 78 29 2e 20 41 _r() on Unix). A
137e9 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 69 73 fter an error is
137ea 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 6e 20 returned by an
137eb 4f 53 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c 20 OS.** function,
137ec 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 SQLite calls thi
137ed 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 s function with
137ee 7a 42 75 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f zBuf pointing to
137ef 0a 2a 2a 20 61 20 62 75 66 66 65 72 20 6f 66 20 .** a buffer of
137f0 6e 42 75 66 20 62 79 74 65 73 2e 20 54 68 65 20 nBuf bytes. The
137f1 4f 53 20 6c 61 79 65 72 20 73 68 6f 75 6c 64 20 OS layer should
137f2 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a 2a 20 populate the.**
137f3 62 75 66 66 65 72 20 77 69 74 68 20 61 20 6e 75 buffer with a nu
137f4 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 l-terminated UTF
137f5 2d 38 20 65 6e 63 6f 64 65 64 20 65 72 72 6f 72 -8 encoded error
137f6 20 6d 65 73 73 61 67 65 0a 2a 2a 20 64 65 73 63 message.** desc
137f7 72 69 62 69 6e 67 20 74 68 65 20 6c 61 73 74 20 ribing the last
137f8 49 4f 20 65 72 72 6f 72 20 74 6f 20 68 61 76 65 IO error to have
137f9 20 6f 63 63 75 72 72 65 64 20 77 69 74 68 69 6e occurred within
137fa 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 the calling.**
137fb 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 thread..**.** If
137fc 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
137fd 67 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 ge is too large
137fe 66 6f 72 20 74 68 65 20 73 75 70 70 6c 69 65 64 for the supplied
137ff 20 62 75 66 66 65 72 2c 0a 2a 2a 20 69 74 20 73 buffer,.** it s
13800 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61 74 hould be truncat
13801 65 64 2e 20 54 68 65 20 72 65 74 75 72 6e 20 76 ed. The return v
13802 61 6c 75 65 20 6f 66 20 78 47 65 74 4c 61 73 74 alue of xGetLast
13803 45 72 72 6f 72 0a 2a 2a 20 69 73 20 7a 65 72 6f Error.** is zero
13804 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 if the error me
13805 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74 68 ssage fits in th
13806 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e e buffer, or non
13807 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 -zero.** otherwi
13808 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73 61 se (if the messa
13809 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65 64 ge was truncated
1380a 29 2e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 69 ). If non-zero i
1380b 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20 74 s returned,.** t
1380c 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 hen it is not ne
1380d 63 65 73 73 61 72 79 20 74 6f 20 69 6e 63 6c 75 cessary to inclu
1380e 64 65 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69 de the nul-termi
1380f 6e 61 74 6f 72 20 63 68 61 72 61 63 74 65 72 0a nator character.
13810 2a 2a 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 ** in the output
13811 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e buffer..**.** N
13812 6f 74 20 73 75 70 70 6c 79 69 6e 67 20 61 6e 20 ot supplying an
13813 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 69 error message wi
13814 6c 6c 20 68 61 76 65 20 6e 6f 20 61 64 76 65 72 ll have no adver
13815 73 65 20 65 66 66 65 63 74 0a 2a 2a 20 6f 6e 20 se effect.** on
13816 53 51 4c 69 74 65 2e 20 49 74 20 69 73 20 66 69 SQLite. It is fi
13817 6e 65 20 74 6f 20 68 61 76 65 20 61 6e 20 69 6d ne to have an im
13818 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 plementation tha
13819 74 20 6e 65 76 65 72 0a 2a 2a 20 72 65 74 75 72 t never.** retur
1381a 6e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 ns an error mess
1381b 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 6e 74 age:.**.** int
1381c 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 xGetLastError(s
1381d 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
1381e 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 , int nBuf, char
1381f 20 2a 7a 42 75 66 29 7b 0a 2a 2a 20 20 20 20 20 *zBuf){.**
13820 61 73 73 65 72 74 28 7a 42 75 66 5b 30 5d 3d 3d assert(zBuf[0]==
13821 27 5c 30 27 29 3b 0a 2a 2a 20 20 20 20 20 72 65 '\0');.** re
13822 74 75 72 6e 20 30 3b 0a 2a 2a 20 20 20 7d 0a 2a turn 0;.** }.*
13823 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 20 69 66 20 *.** However if
13824 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
13825 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20 69 74 is supplied, it
13826 20 77 69 6c 6c 20 62 65 20 69 6e 63 6f 72 70 6f will be incorpo
13827 72 61 74 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 rated.** by sqli
13828 74 65 20 69 6e 74 6f 20 74 68 65 20 65 72 72 6f te into the erro
13829 72 20 6d 65 73 73 61 67 65 20 61 76 61 69 6c 61 r message availa
1382a 62 6c 65 20 74 6f 20 74 68 65 20 75 73 65 72 20 ble to the user
1382b 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 using.** sqlite3
1382c 5f 65 72 72 6d 73 67 28 29 2c 20 70 6f 73 73 69 _errmsg(), possi
1382d 62 6c 79 20 6d 61 6b 69 6e 67 20 49 4f 20 65 72 bly making IO er
1382e 72 6f 72 73 20 65 61 73 69 65 72 20 74 6f 20 64 rors easier to d
1382f 65 62 75 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ebug..*/.static
13830 69 6e 74 20 77 69 6e 47 65 74 4c 61 73 74 45 72 int winGetLastEr
13831 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ror(sqlite3_vfs
13832 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c *pVfs, int nBuf,
13833 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 char *zBuf){.
13834 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
13835 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 72 6e (pVfs);. return
13836 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 getLastErrorMsg
13837 28 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 (osGetLastError(
13838 29 2c 20 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a ), nBuf, zBuf);.
13839 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c }../*.** Initial
1383a 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 ize and deinitia
1383b 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 lize the operati
1383c 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 ng system interf
1383d 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ace..*/.SQLITE_A
1383e 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
1383f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 s_init(void){.
13840 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 static sqlite3_v
13841 66 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20 fs winVfs = {.
13842 20 20 33 2c 20 20 20 20 20 20 20 20 20 20 20 20 3,
13843 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 /* iVersi
13844 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 on */. sizeof
13845 28 77 69 6e 46 69 6c 65 29 2c 20 20 20 20 20 2f (winFile), /
13846 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 * szOsFile */.
13847 20 20 4d 41 58 5f 50 41 54 48 2c 20 20 20 20 20 MAX_PATH,
13848 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 /* mxPath
13849 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 name */. 0,
1384a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1384b 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 /* pNext */.
1384c 20 22 77 69 6e 33 32 22 2c 20 20 20 20 20 20 20 "win32",
1384d 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a /* zName *
1384e 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 /. 0,
1384f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 /* pA
13850 70 70 44 61 74 61 20 2a 2f 0a 20 20 20 20 77 69 ppData */. wi
13851 6e 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 nOpen,
13852 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20 /* xOpen */.
13853 20 20 20 77 69 6e 44 65 6c 65 74 65 2c 20 20 20 winDelete,
13854 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 /* xDele
13855 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41 63 63 te */. winAcc
13856 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f ess, /
13857 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 * xAccess */.
13858 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 winFullPathname
13859 2c 20 20 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 , /* xFullPa
1385a 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 thname */. wi
1385b 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 nDlOpen,
1385c 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f /* xDlOpen */
1385d 0a 20 20 20 20 77 69 6e 44 6c 45 72 72 6f 72 2c . winDlError,
1385e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c /* xDl
1385f 45 72 72 6f 72 20 2a 2f 0a 20 20 20 20 77 69 6e Error */. win
13860 44 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 DlSym,
13861 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 /* xDlSym */.
13862 20 20 20 77 69 6e 44 6c 43 6c 6f 73 65 2c 20 20 winDlClose,
13863 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c /* xDlCl
13864 6f 73 65 20 2a 2f 0a 20 20 20 20 77 69 6e 52 61 ose */. winRa
13865 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 20 20 ndomness,
13866 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a /* xRandomness *
13867 2f 0a 20 20 20 20 77 69 6e 53 6c 65 65 70 2c 20 /. winSleep,
13868 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 /* xS
13869 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69 6e 43 leep */. winC
1386a 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 20 urrentTime,
1386b 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 /* xCurrentTime
1386c 20 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61 */. winGetLa
1386d 73 74 45 72 72 6f 72 2c 20 20 20 20 20 2f 2a 20 stError, /*
1386e 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f xGetLastError */
1386f 0a 20 20 20 20 77 69 6e 43 75 72 72 65 6e 74 54 . winCurrentT
13870 69 6d 65 49 6e 74 36 34 2c 20 2f 2a 20 78 43 75 imeInt64, /* xCu
13871 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 20 2a rrentTimeInt64 *
13872 2f 0a 20 20 20 20 77 69 6e 53 65 74 53 79 73 74 /. winSetSyst
13873 65 6d 43 61 6c 6c 2c 20 20 20 20 2f 2a 20 78 53 emCall, /* xS
13874 65 74 53 79 73 74 65 6d 43 61 6c 6c 20 2a 2f 0a etSystemCall */.
13875 20 20 20 20 77 69 6e 47 65 74 53 79 73 74 65 6d winGetSystem
13876 43 61 6c 6c 2c 20 20 20 20 2f 2a 20 78 47 65 74 Call, /* xGet
13877 53 79 73 74 65 6d 43 61 6c 6c 20 2a 2f 0a 20 20 SystemCall */.
13878 20 20 77 69 6e 4e 65 78 74 53 79 73 74 65 6d 43 winNextSystemC
13879 61 6c 6c 2c 20 20 20 2f 2a 20 78 4e 65 78 74 53 all, /* xNextS
1387a 79 73 74 65 6d 43 61 6c 6c 20 2a 2f 0a 20 20 7d ystemCall */. }
1387b 3b 0a 0a 20 20 2f 2a 20 44 6f 75 62 6c 65 2d 63 ;.. /* Double-c
1387c 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 61 53 heck that the aS
1387d 79 73 63 61 6c 6c 5b 5d 20 61 72 72 61 79 20 68 yscall[] array h
1387e 61 73 20 62 65 65 6e 20 63 6f 6e 73 74 72 75 63 as been construc
1387f 74 65 64 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 ted. ** correct
13880 6c 79 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 ly. See ticket
13881 5b 62 62 33 61 38 36 65 38 39 30 63 38 65 39 36 [bb3a86e890c8e96
13882 61 62 5d 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ab] */. assert(
13883 20 41 72 72 61 79 53 69 7a 65 28 61 53 79 73 63 ArraySize(aSysc
13884 61 6c 6c 29 3d 3d 37 34 20 29 3b 0a 0a 23 69 66 all)==74 );..#if
13885 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
13886 5f 57 41 4c 0a 20 20 2f 2a 20 67 65 74 20 6d 65 _WAL. /* get me
13887 6d 6f 72 79 20 6d 61 70 20 61 6c 6c 6f 63 61 74 mory map allocat
13888 69 6f 6e 20 67 72 61 6e 75 6c 61 72 69 74 79 20 ion granularity
13889 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 77 69 6e */. memset(&win
1388a 53 79 73 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 SysInfo, 0, size
1388b 6f 66 28 53 59 53 54 45 4d 5f 49 4e 46 4f 29 29 of(SYSTEM_INFO))
1388c 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ;.#if SQLITE_OS_
1388d 57 49 4e 52 54 0a 20 20 6f 73 47 65 74 4e 61 74 WINRT. osGetNat
1388e 69 76 65 53 79 73 74 65 6d 49 6e 66 6f 28 26 77 iveSystemInfo(&w
1388f 69 6e 53 79 73 49 6e 66 6f 29 3b 0a 23 65 6c 73 inSysInfo);.#els
13890 65 0a 20 20 6f 73 47 65 74 53 79 73 74 65 6d 49 e. osGetSystemI
13891 6e 66 6f 28 26 77 69 6e 53 79 73 49 6e 66 6f 29 nfo(&winSysInfo)
13892 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 ;.#endif. asser
13893 74 28 77 69 6e 53 79 73 49 6e 66 6f 2e 64 77 41 t(winSysInfo.dwA
13894 6c 6c 6f 63 61 74 69 6f 6e 47 72 61 6e 75 6c 61 llocationGranula
13895 72 69 74 79 20 3e 20 30 29 3b 0a 23 65 6e 64 69 rity > 0);.#endi
13896 66 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 f.. sqlite3_vfs
13897 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e 56 66 _register(&winVf
13898 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 s, 1);. return
13899 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 53 SQLITE_OK; .}..S
1389a 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1389b 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 lite3_os_end(voi
1389c 64 29 7b 20 0a 23 69 66 20 53 51 4c 49 54 45 5f d){ .#if SQLITE_
1389d 4f 53 5f 57 49 4e 52 54 0a 20 20 69 66 28 20 73 OS_WINRT. if( s
1389e 6c 65 65 70 4f 62 6a 21 3d 4e 55 4c 4c 20 29 7b leepObj!=NULL ){
1389f 0a 20 20 20 20 6f 73 43 6c 6f 73 65 48 61 6e 64 . osCloseHand
138a0 6c 65 28 73 6c 65 65 70 4f 62 6a 29 3b 0a 20 20 le(sleepObj);.
138a1 20 20 73 6c 65 65 70 4f 62 6a 20 3d 20 4e 55 4c sleepObj = NUL
138a2 4c 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 L;. }.#endif.
138a3 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
138a4 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
138a5 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 2a 2f 0a QLITE_OS_WIN */.
138a6 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
138a7 20 45 6e 64 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 End of os_win.c
138a8 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
138a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
138aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
138ab 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
138ac 20 42 65 67 69 6e 20 66 69 6c 65 20 62 69 74 76 Begin file bitv
138ad 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ec.c ***********
138ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
138af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
138b0 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 46 65 62 72 ./*.** 2008 Febr
138b1 75 61 72 79 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 uary 16.**.** Th
138b2 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
138b3 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
138b4 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
138b5 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
138b6 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
138b7 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
138b8 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
138b9 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
138ba 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
138bb 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
138bc 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
138bd 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
138be 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
138bf 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
138c0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
138c1 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
138c2 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
138c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
138c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
138c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
138c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
138c7 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
138c8 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 ile implements a
138c9 6e 20 6f 62 6a 65 63 74 20 74 68 61 74 20 72 65 n object that re
138ca 70 72 65 73 65 6e 74 73 20 61 20 66 69 78 65 64 presents a fixed
138cb 2d 6c 65 6e 67 74 68 0a 2a 2a 20 62 69 74 6d 61 -length.** bitma
138cc 70 2e 20 20 42 69 74 73 20 61 72 65 20 6e 75 6d p. Bits are num
138cd 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20 77 bered starting w
138ce 69 74 68 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 20 62 ith 1..**.** A b
138cf 69 74 6d 61 70 20 69 73 20 75 73 65 64 20 74 6f itmap is used to
138d0 20 72 65 63 6f 72 64 20 77 68 69 63 68 20 70 61 record which pa
138d1 67 65 73 20 6f 66 20 61 20 64 61 74 61 62 61 73 ges of a databas
138d2 65 20 66 69 6c 65 20 68 61 76 65 20 62 65 65 6e e file have been
138d3 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 64 .** journalled d
138d4 75 72 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 uring a transact
138d5 69 6f 6e 2c 20 6f 72 20 77 68 69 63 68 20 70 61 ion, or which pa
138d6 67 65 73 20 68 61 76 65 20 74 68 65 20 22 64 6f ges have the "do
138d7 6e 74 2d 77 72 69 74 65 22 0a 2a 2a 20 70 72 6f nt-write".** pro
138d8 70 65 72 74 79 2e 20 20 55 73 75 61 6c 6c 79 20 perty. Usually
138d9 6f 6e 6c 79 20 61 20 66 65 77 20 70 61 67 65 73 only a few pages
138da 20 61 72 65 20 6d 65 65 74 20 65 69 74 68 65 72 are meet either
138db 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 20 53 condition..** S
138dc 6f 20 74 68 65 20 62 69 74 6d 61 70 20 69 73 20 o the bitmap is
138dd 75 73 75 61 6c 6c 79 20 73 70 61 72 73 65 20 61 usually sparse a
138de 6e 64 20 68 61 73 20 6c 6f 77 20 63 61 72 64 69 nd has low cardi
138df 6e 61 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20 73 nality..** But s
138e0 6f 6d 65 74 69 6d 65 73 20 28 66 6f 72 20 65 78 ometimes (for ex
138e1 61 6d 70 6c 65 20 77 68 65 6e 20 64 75 72 69 6e ample when durin
138e2 67 20 61 20 44 52 4f 50 20 6f 66 20 61 20 6c 61 g a DROP of a la
138e3 72 67 65 20 74 61 62 6c 65 29 20 6d 6f 73 74 0a rge table) most.
138e4 2a 2a 20 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 ** or all of the
138e5 20 70 61 67 65 73 20 69 6e 20 61 20 64 61 74 61 pages in a data
138e6 62 61 73 65 20 63 61 6e 20 67 65 74 20 6a 6f 75 base can get jou
138e7 72 6e 61 6c 6c 65 64 2e 20 20 49 6e 20 74 68 6f rnalled. In tho
138e8 73 65 20 63 61 73 65 73 2c 20 0a 2a 2a 20 74 68 se cases, .** th
138e9 65 20 62 69 74 6d 61 70 20 62 65 63 6f 6d 65 73 e bitmap becomes
138ea 20 64 65 6e 73 65 20 77 69 74 68 20 68 69 67 68 dense with high
138eb 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 20 20 54 cardinality. T
138ec 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 6e 65 65 he algorithm nee
138ed 64 73 20 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 ds .** to handle
138ee 20 62 6f 74 68 20 63 61 73 65 73 20 77 65 6c 6c both cases well
138ef 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 ..**.** The size
138f0 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70 20 69 of the bitmap i
138f1 73 20 66 69 78 65 64 20 77 68 65 6e 20 74 68 65 s fixed when the
138f2 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74 object is creat
138f3 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 62 69 ed..**.** All bi
138f4 74 73 20 61 72 65 20 63 6c 65 61 72 20 77 68 65 ts are clear whe
138f5 6e 20 74 68 65 20 62 69 74 6d 61 70 20 69 73 20 n the bitmap is
138f6 63 72 65 61 74 65 64 2e 20 20 49 6e 64 69 76 69 created. Indivi
138f7 64 75 61 6c 20 62 69 74 73 0a 2a 2a 20 6d 61 79 dual bits.** may
138f8 20 62 65 20 73 65 74 20 6f 72 20 63 6c 65 61 72 be set or clear
138f9 65 64 20 6f 6e 65 20 61 74 20 61 20 74 69 6d 65 ed one at a time
138fa 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 74 20 6f 70 65 ..**.** Test ope
138fb 72 61 74 69 6f 6e 73 20 61 72 65 20 61 62 6f 75 rations are abou
138fc 74 20 31 30 30 20 74 69 6d 65 73 20 6d 6f 72 65 t 100 times more
138fd 20 63 6f 6d 6d 6f 6e 20 74 68 61 74 20 73 65 74 common that set
138fe 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 20 operations..**
138ff 43 6c 65 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 Clear operations
13900 20 61 72 65 20 65 78 63 65 65 64 69 6e 67 6c 79 are exceedingly
13901 20 72 61 72 65 2e 20 20 54 68 65 72 65 20 61 72 rare. There ar
13902 65 20 75 73 75 61 6c 6c 79 20 62 65 74 77 65 65 e usually betwee
13903 6e 0a 2a 2a 20 35 20 61 6e 64 20 35 30 30 20 73 n.** 5 and 500 s
13904 65 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 70 65 et operations pe
13905 72 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2c r Bitvec object,
13906 20 74 68 6f 75 67 68 20 74 68 65 20 6e 75 6d 62 though the numb
13907 65 72 20 6f 66 20 73 65 74 73 20 63 61 6e 0a 2a er of sets can.*
13908 2a 20 73 6f 6d 65 74 69 6d 65 73 20 67 72 6f 77 * sometimes grow
13909 20 69 6e 74 6f 20 74 65 6e 73 20 6f 66 20 74 68 into tens of th
1390a 6f 75 73 61 6e 64 73 20 6f 72 20 6c 61 72 67 65 ousands or large
1390b 72 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 r. The size of
1390c 74 68 65 0a 2a 2a 20 42 69 74 76 65 63 20 6f 62 the.** Bitvec ob
1390d 6a 65 63 74 20 69 73 20 74 68 65 20 6e 75 6d 62 ject is the numb
1390e 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
1390f 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13910 20 61 74 20 74 68 65 0a 2a 2a 20 73 74 61 72 74 at the.** start
13911 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f of a transactio
13912 6e 2c 20 61 6e 64 20 69 73 20 74 68 75 73 20 75 n, and is thus u
13913 73 75 61 6c 6c 79 20 6c 65 73 73 20 74 68 61 6e sually less than
13914 20 61 20 66 65 77 20 74 68 6f 75 73 61 6e 64 2c a few thousand,
13915 0a 2a 2a 20 62 75 74 20 63 61 6e 20 62 65 20 61 .** but can be a
13916 73 20 6c 61 72 67 65 20 61 73 20 32 20 62 69 6c s large as 2 bil
13917 6c 69 6f 6e 20 66 6f 72 20 61 20 72 65 61 6c 6c lion for a reall
13918 79 20 62 69 67 20 64 61 74 61 62 61 73 65 2e 0a y big database..
13919 2a 2f 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 74 */../* Size of t
1391a 68 65 20 42 69 74 76 65 63 20 73 74 72 75 63 74 he Bitvec struct
1391b 75 72 65 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f ure in bytes. */
1391c 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f .#define BITVEC_
1391d 53 5a 20 20 20 20 20 20 20 20 35 31 32 0a 0a 2f SZ 512../
1391e 2a 20 52 6f 75 6e 64 20 74 68 65 20 75 6e 69 6f * Round the unio
1391f 6e 20 73 69 7a 65 20 64 6f 77 6e 20 74 6f 20 74 n size down to t
13920 68 65 20 6e 65 61 72 65 73 74 20 70 6f 69 6e 74 he nearest point
13921 65 72 20 62 6f 75 6e 64 61 72 79 2c 20 73 69 6e er boundary, sin
13922 63 65 20 74 68 61 74 27 73 20 68 6f 77 20 0a 2a ce that's how .*
13923 2a 20 69 74 20 77 69 6c 6c 20 62 65 20 61 6c 69 * it will be ali
13924 67 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 gned within the
13925 42 69 74 76 65 63 20 73 74 72 75 63 74 2e 20 2a Bitvec struct. *
13926 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 /.#define BITVEC
13927 5f 55 53 49 5a 45 20 20 20 20 20 28 28 28 42 49 _USIZE (((BI
13928 54 56 45 43 5f 53 5a 2d 28 33 2a 73 69 7a 65 6f TVEC_SZ-(3*sizeo
13929 66 28 75 33 32 29 29 29 2f 73 69 7a 65 6f 66 28 f(u32)))/sizeof(
1392a 42 69 74 76 65 63 2a 29 29 2a 73 69 7a 65 6f 66 Bitvec*))*sizeof
1392b 28 42 69 74 76 65 63 2a 29 29 0a 0a 2f 2a 20 54 (Bitvec*))../* T
1392c 79 70 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 ype of the array
1392d 20 22 65 6c 65 6d 65 6e 74 22 20 66 6f 72 20 74 "element" for t
1392e 68 65 20 62 69 74 6d 61 70 20 72 65 70 72 65 73 he bitmap repres
1392f 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 20 53 68 entation. .** Sh
13930 6f 75 6c 64 20 62 65 20 61 20 70 6f 77 65 72 20 ould be a power
13931 6f 66 20 32 2c 20 61 6e 64 20 69 64 65 61 6c 6c of 2, and ideall
13932 79 2c 20 65 76 65 6e 6c 79 20 64 69 76 69 64 65 y, evenly divide
13933 20 69 6e 74 6f 20 42 49 54 56 45 43 5f 55 53 49 into BITVEC_USI
13934 5a 45 2e 20 0a 2a 2a 20 53 65 74 74 69 6e 67 20 ZE. .** Setting
13935 74 68 69 73 20 74 6f 20 74 68 65 20 22 6e 61 74 this to the "nat
13936 75 72 61 6c 20 77 6f 72 64 22 20 73 69 7a 65 20 ural word" size
13937 6f 66 20 79 6f 75 72 20 43 50 55 20 6d 61 79 20 of your CPU may
13938 69 6d 70 72 6f 76 65 0a 2a 2a 20 70 65 72 66 6f improve.** perfo
13939 72 6d 61 6e 63 65 2e 20 2a 2f 0a 23 64 65 66 69 rmance. */.#defi
1393a 6e 65 20 42 49 54 56 45 43 5f 54 45 4c 45 4d 20 ne BITVEC_TELEM
1393b 20 20 20 20 75 38 0a 2f 2a 20 53 69 7a 65 2c 20 u8./* Size,
1393c 69 6e 20 62 69 74 73 2c 20 6f 66 20 74 68 65 20 in bits, of the
1393d 62 69 74 6d 61 70 20 65 6c 65 6d 65 6e 74 2e 20 bitmap element.
1393e 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 */.#define BITVE
1393f 43 5f 53 5a 45 4c 45 4d 20 20 20 20 38 0a 2f 2a C_SZELEM 8./*
13940 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 Number of eleme
13941 6e 74 73 20 69 6e 20 61 20 62 69 74 6d 61 70 20 nts in a bitmap
13942 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69 6e array. */.#defin
13943 65 20 42 49 54 56 45 43 5f 4e 45 4c 45 4d 20 20 e BITVEC_NELEM
13944 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45 (BITVEC_USIZE
13945 2f 73 69 7a 65 6f 66 28 42 49 54 56 45 43 5f 54 /sizeof(BITVEC_T
13946 45 4c 45 4d 29 29 0a 2f 2a 20 4e 75 6d 62 65 72 ELEM))./* Number
13947 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 of bits in the
13948 62 69 74 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f bitmap array. */
13949 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f .#define BITVEC_
1394a 4e 42 49 54 20 20 20 20 20 20 28 42 49 54 56 45 NBIT (BITVE
1394b 43 5f 4e 45 4c 45 4d 2a 42 49 54 56 45 43 5f 53 C_NELEM*BITVEC_S
1394c 5a 45 4c 45 4d 29 0a 0a 2f 2a 20 4e 75 6d 62 65 ZELEM)../* Numbe
1394d 72 20 6f 66 20 75 33 32 20 76 61 6c 75 65 73 20 r of u32 values
1394e 69 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 20 2a in hash table. *
1394f 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 /.#define BITVEC
13950 5f 4e 49 4e 54 20 20 20 20 20 20 28 42 49 54 56 _NINT (BITV
13951 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 EC_USIZE/sizeof(
13952 75 33 32 29 29 0a 2f 2a 20 4d 61 78 69 6d 75 6d u32))./* Maximum
13953 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 number of entri
13954 65 73 20 69 6e 20 68 61 73 68 20 74 61 62 6c 65 es in hash table
13955 20 62 65 66 6f 72 65 20 0a 2a 2a 20 73 75 62 2d before .** sub-
13956 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72 65 2d dividing and re-
13957 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 23 64 65 66 hashing. */.#def
13958 69 6e 65 20 42 49 54 56 45 43 5f 4d 58 48 41 53 ine BITVEC_MXHAS
13959 48 20 20 20 20 28 42 49 54 56 45 43 5f 4e 49 4e H (BITVEC_NIN
1395a 54 2f 32 29 0a 2f 2a 20 48 61 73 68 69 6e 67 20 T/2)./* Hashing
1395b 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 65 function for the
1395c 20 61 48 61 73 68 20 72 65 70 72 65 73 65 6e 74 aHash represent
1395d 61 74 69 6f 6e 2e 0a 2a 2a 20 45 6d 70 69 72 69 ation..** Empiri
1395e 63 61 6c 20 74 65 73 74 69 6e 67 20 73 68 6f 77 cal testing show
1395f 65 64 20 74 68 61 74 20 74 68 65 20 2a 33 37 20 ed that the *37
13960 6d 75 6c 74 69 70 6c 69 65 72 20 0a 2a 2a 20 28 multiplier .** (
13961 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 72 69 an arbitrary pri
13962 6d 65 29 69 6e 20 74 68 65 20 68 61 73 68 20 66 me)in the hash f
13963 75 6e 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 64 unction provided
13964 20 0a 2a 2a 20 6e 6f 20 66 65 77 65 72 20 63 6f .** no fewer co
13965 6c 6c 69 73 69 6f 6e 73 20 74 68 61 6e 20 74 68 llisions than th
13966 65 20 6e 6f 2d 6f 70 20 2a 31 2e 20 2a 2f 0a 23 e no-op *1. */.#
13967 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 48 41 define BITVEC_HA
13968 53 48 28 58 29 20 20 20 28 28 28 58 29 2a 31 29 SH(X) (((X)*1)
13969 25 42 49 54 56 45 43 5f 4e 49 4e 54 29 0a 0a 23 %BITVEC_NINT)..#
1396a 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 50 define BITVEC_NP
1396b 54 52 20 20 20 20 20 20 28 42 49 54 56 45 43 5f TR (BITVEC_
1396c 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 69 74 USIZE/sizeof(Bit
1396d 76 65 63 20 2a 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 vec *)).../*.**
1396e 41 20 62 69 74 6d 61 70 20 69 73 20 61 6e 20 69 A bitmap is an i
1396f 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
13970 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
13971 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 62 re..**.** This b
13972 69 74 6d 61 70 20 72 65 63 6f 72 64 73 20 74 68 itmap records th
13973 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20 7a e existance of z
13974 65 72 6f 20 6f 72 20 6d 6f 72 65 20 62 69 74 73 ero or more bits
13975 0a 2a 2a 20 77 69 74 68 20 76 61 6c 75 65 73 20 .** with values
13976 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69 53 between 1 and iS
13977 69 7a 65 2c 20 69 6e 63 6c 75 73 69 76 65 2e 0a ize, inclusive..
13978 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 **.** There are
13979 74 68 72 65 65 20 70 6f 73 73 69 62 6c 65 20 72 three possible r
1397a 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 6f epresentations o
1397b 66 20 74 68 65 20 62 69 74 6d 61 70 2e 0a 2a 2a f the bitmap..**
1397c 20 49 66 20 69 53 69 7a 65 3c 3d 42 49 54 56 45 If iSize<=BITVE
1397d 43 5f 4e 42 49 54 2c 20 74 68 65 6e 20 42 69 74 C_NBIT, then Bit
1397e 76 65 63 2e 75 2e 61 42 69 74 6d 61 70 5b 5d 20 vec.u.aBitmap[]
1397f 69 73 20 61 20 73 74 72 61 69 67 68 74 0a 2a 2a is a straight.**
13980 20 62 69 74 6d 61 70 2e 20 20 54 68 65 20 6c 65 bitmap. The le
13981 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 ast significant
13982 62 69 74 20 69 73 20 62 69 74 20 31 2e 0a 2a 2a bit is bit 1..**
13983 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3e 42 49 54 .** If iSize>BIT
13984 56 45 43 5f 4e 42 49 54 20 61 6e 64 20 69 44 69 VEC_NBIT and iDi
13985 76 69 73 6f 72 3d 3d 30 20 74 68 65 6e 20 42 69 visor==0 then Bi
13986 74 76 65 63 2e 75 2e 61 48 61 73 68 5b 5d 20 69 tvec.u.aHash[] i
13987 73 0a 2a 2a 20 61 20 68 61 73 68 20 74 61 62 6c s.** a hash tabl
13988 65 20 74 68 61 74 20 77 69 6c 6c 20 68 6f 6c 64 e that will hold
13989 20 75 70 20 74 6f 20 42 49 54 56 45 43 5f 4d 58 up to BITVEC_MX
1398a 48 41 53 48 20 64 69 73 74 69 6e 63 74 20 76 61 HASH distinct va
1398b 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 lues..**.** Othe
1398c 72 77 69 73 65 2c 20 74 68 65 20 76 61 6c 75 65 rwise, the value
1398d 20 69 20 69 73 20 72 65 64 69 72 65 63 74 65 64 i is redirected
1398e 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 42 49 54 into one of BIT
1398f 56 45 43 5f 4e 50 54 52 0a 2a 2a 20 73 75 62 2d VEC_NPTR.** sub-
13990 62 69 74 6d 61 70 73 20 70 6f 69 6e 74 65 64 20 bitmaps pointed
13991 74 6f 20 62 79 20 42 69 74 76 65 63 2e 75 2e 61 to by Bitvec.u.a
13992 70 53 75 62 5b 5d 2e 20 20 45 61 63 68 20 73 75 pSub[]. Each su
13993 62 62 69 74 6d 61 70 0a 2a 2a 20 68 61 6e 64 6c bbitmap.** handl
13994 65 73 20 75 70 20 74 6f 20 69 44 69 76 69 73 6f es up to iDiviso
13995 72 20 73 65 70 61 72 61 74 65 20 76 61 6c 75 65 r separate value
13996 73 20 6f 66 20 69 2e 20 20 61 70 53 75 62 5b 30 s of i. apSub[0
13997 5d 20 68 6f 6c 64 73 0a 2a 2a 20 76 61 6c 75 65 ] holds.** value
13998 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 s between 1 and
13999 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53 75 62 iDivisor. apSub
1399a 5b 31 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 [1] holds values
1399b 20 62 65 74 77 65 65 6e 0a 2a 2a 20 69 44 69 76 between.** iDiv
1399c 69 73 6f 72 2b 31 20 61 6e 64 20 32 2a 69 44 69 isor+1 and 2*iDi
1399d 76 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 4e 5d visor. apSub[N]
1399e 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 65 holds values be
1399f 74 77 65 65 6e 0a 2a 2a 20 4e 2a 69 44 69 76 69 tween.** N*iDivi
139a0 73 6f 72 2b 31 20 61 6e 64 20 28 4e 2b 31 29 2a sor+1 and (N+1)*
139a1 69 44 69 76 69 73 6f 72 2e 20 20 45 61 63 68 20 iDivisor. Each
139a2 73 75 62 62 69 74 6d 61 70 20 69 73 20 6e 6f 72 subbitmap is nor
139a3 6d 61 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 68 6f malized.** to ho
139a4 6c 64 20 64 65 61 6c 20 77 69 74 68 20 76 61 6c ld deal with val
139a5 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e ues between 1 an
139a6 64 20 69 44 69 76 69 73 6f 72 2e 0a 2a 2f 0a 73 d iDivisor..*/.s
139a7 74 72 75 63 74 20 42 69 74 76 65 63 20 7b 0a 20 truct Bitvec {.
139a8 20 75 33 32 20 69 53 69 7a 65 3b 20 20 20 20 20 u32 iSize;
139a9 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 62 69 74 20 /* Maximum bit
139aa 69 6e 64 65 78 2e 20 20 4d 61 78 20 69 53 69 7a index. Max iSiz
139ab 65 20 69 73 20 34 2c 32 39 34 2c 39 36 37 2c 32 e is 4,294,967,2
139ac 39 36 2e 20 2a 2f 0a 20 20 75 33 32 20 6e 53 65 96. */. u32 nSe
139ad 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 t; /* Numb
139ae 65 72 20 6f 66 20 62 69 74 73 20 74 68 61 74 20 er of bits that
139af 61 72 65 20 73 65 74 20 2d 20 6f 6e 6c 79 20 76 are set - only v
139b0 61 6c 69 64 20 66 6f 72 20 61 48 61 73 68 0a 20 alid for aHash.
139b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139b2 20 2a 2a 20 65 6c 65 6d 65 6e 74 2e 20 20 4d 61 ** element. Ma
139b3 78 20 69 73 20 42 49 54 56 45 43 5f 4e 49 4e 54 x is BITVEC_NINT
139b4 2e 20 20 46 6f 72 20 42 49 54 56 45 43 5f 53 5a . For BITVEC_SZ
139b5 20 6f 66 20 35 31 32 2c 0a 20 20 20 20 20 20 20 of 512,.
139b6 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 ** th
139b7 69 73 20 77 6f 75 6c 64 20 62 65 20 31 32 35 2e is would be 125.
139b8 20 2a 2f 0a 20 20 75 33 32 20 69 44 69 76 69 73 */. u32 iDivis
139b9 6f 72 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 or; /* Number
139ba 6f 66 20 62 69 74 73 20 68 61 6e 64 6c 65 64 20 of bits handled
139bb 62 79 20 65 61 63 68 20 61 70 53 75 62 5b 5d 20 by each apSub[]
139bc 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20 20 20 entry. */.
139bd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
139be 68 6f 75 6c 64 20 3e 3d 30 20 66 6f 72 20 61 70 hould >=0 for ap
139bf 53 75 62 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a Sub element. */.
139c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139c1 20 20 2f 2a 20 4d 61 78 20 69 44 69 76 69 73 6f /* Max iDiviso
139c2 72 20 69 73 20 6d 61 78 28 75 33 32 29 20 2f 20 r is max(u32) /
139c3 42 49 54 56 45 43 5f 4e 50 54 52 20 2b 20 31 2e BITVEC_NPTR + 1.
139c4 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 */.
139c5 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 20 /* For a
139c6 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 32 BITVEC_SZ of 512
139c7 2c 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 , this would be
139c8 33 34 2c 33 35 39 2c 37 33 39 2e 20 2a 2f 0a 20 34,359,739. */.
139c9 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 42 49 54 union {. BIT
139ca 56 45 43 5f 54 45 4c 45 4d 20 61 42 69 74 6d 61 VEC_TELEM aBitma
139cb 70 5b 42 49 54 56 45 43 5f 4e 45 4c 45 4d 5d 3b p[BITVEC_NELEM];
139cc 20 20 20 20 2f 2a 20 42 69 74 6d 61 70 20 72 65 /* Bitmap re
139cd 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a presentation */.
139ce 20 20 20 20 75 33 32 20 61 48 61 73 68 5b 42 49 u32 aHash[BI
139cf 54 56 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 20 20 TVEC_NINT];
139d0 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 72 /* Hash table r
139d1 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f epresentation */
139d2 0a 20 20 20 20 42 69 74 76 65 63 20 2a 61 70 53 . Bitvec *apS
139d3 75 62 5b 42 49 54 56 45 43 5f 4e 50 54 52 5d 3b ub[BITVEC_NPTR];
139d4 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 20 72 /* Recursive r
139d5 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f epresentation */
139d6 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a . } u;.};../*.*
139d7 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 62 * Create a new b
139d8 69 74 6d 61 70 20 6f 62 6a 65 63 74 20 61 62 6c itmap object abl
139d9 65 20 74 6f 20 68 61 6e 64 6c 65 20 62 69 74 73 e to handle bits
139da 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 69 between 0 and i
139db 53 69 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75 73 69 Size,.** inclusi
139dc 76 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f ve. Return a po
139dd 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 inter to the new
139de 20 6f 62 6a 65 63 74 2e 20 20 52 65 74 75 72 6e object. Return
139df 20 4e 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d 61 6c NULL if .** mal
139e0 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 loc fails..*/.SQ
139e1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 LITE_PRIVATE Bit
139e2 76 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 vec *sqlite3Bitv
139e3 65 63 43 72 65 61 74 65 28 75 33 32 20 69 53 69 ecCreate(u32 iSi
139e4 7a 65 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 ze){. Bitvec *p
139e5 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 ;. assert( size
139e6 6f 66 28 2a 70 29 3d 3d 42 49 54 56 45 43 5f 53 of(*p)==BITVEC_S
139e7 5a 20 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 Z );. p = sqlit
139e8 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 e3MallocZero( si
139e9 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 66 zeof(*p) );. if
139ea 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 69 53 ( p ){. p->iS
139eb 69 7a 65 20 3d 20 69 53 69 7a 65 3b 0a 20 20 7d ize = iSize;. }
139ec 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
139ed 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 /*.** Check to s
139ee 65 65 20 69 66 20 74 68 65 20 69 2d 74 68 20 62 ee if the i-th b
139ef 69 74 20 69 73 20 73 65 74 2e 20 20 52 65 74 75 it is set. Retu
139f0 72 6e 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 rn true or false
139f1 2e 0a 2a 2a 20 49 66 20 70 20 69 73 20 4e 55 4c ..** If p is NUL
139f2 4c 20 28 69 66 20 74 68 65 20 62 69 74 6d 61 70 L (if the bitmap
139f3 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 63 72 has not been cr
139f4 65 61 74 65 64 29 20 6f 72 20 69 66 0a 2a 2a 20 eated) or if.**
139f5 69 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 i is out of rang
139f6 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 66 e, then return f
139f7 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f alse..*/.SQLITE_
139f8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
139f9 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 69 te3BitvecTest(Bi
139fa 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b tvec *p, u32 i){
139fb 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
139fc 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 69 3e turn 0;. if( i>
139fd 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 69 3d 3d 30 p->iSize || i==0
139fe 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 ) return 0;. i
139ff 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e --;. while( p->
13a00 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 iDivisor ){.
13a01 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 u32 bin = i/p->i
13a02 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d Divisor;. i =
13a03 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a i%p->iDivisor;.
13a04 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 p = p->u.apS
13a05 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 20 ub[bin];. if
13a06 28 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65 74 (!p) {. ret
13a07 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 0;. }. }
13a08 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c . if( p->iSize<
13a09 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a =BITVEC_NBIT ){.
13a0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 75 return (p->u
13a0b 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 .aBitmap[i/BITVE
13a0c 43 5f 53 5a 45 4c 45 4d 5d 20 26 20 28 31 3c 3c C_SZELEM] & (1<<
13a0d 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 (i&(BITVEC_SZELE
13a0e 4d 2d 31 29 29 29 29 21 3d 30 3b 0a 20 20 7d 20 M-1))))!=0;. }
13a0f 65 6c 73 65 7b 0a 20 20 20 20 75 33 32 20 68 20 else{. u32 h
13a10 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 69 2b = BITVEC_HASH(i+
13a11 2b 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 +);. while( p
13a12 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a ->u.aHash[h] ){.
13a13 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 if( p->u.a
13a14 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65 74 Hash[h]==i ) ret
13a15 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 68 20 3d urn 1;. h =
13a16 20 28 68 2b 31 29 20 25 20 42 49 54 56 45 43 5f (h+1) % BITVEC_
13a17 4e 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 NINT;. }.
13a18 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a return 0;. }.}.
13a19 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 69 ./*.** Set the i
13a1a 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75 72 6e -th bit. Return
13a1b 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 0 on success an
13a1c 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 d an error code
13a1d 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 67 if.** anything g
13a1e 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a oes wrong..**.**
13a1f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 This routine mi
13a20 67 68 74 20 63 61 75 73 65 20 73 75 62 2d 62 69 ght cause sub-bi
13a21 74 6d 61 70 73 20 74 6f 20 62 65 20 61 6c 6c 6f tmaps to be allo
13a22 63 61 74 65 64 2e 20 20 46 61 69 6c 69 6e 67 0a cated. Failing.
13a23 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20 6d 65 ** to get the me
13a24 6d 6f 72 79 20 6e 65 65 64 65 64 20 74 6f 20 68 mory needed to h
13a25 6f 6c 64 20 74 68 65 20 73 75 62 2d 62 69 74 6d old the sub-bitm
13a26 61 70 20 69 73 20 74 68 65 20 6f 6e 6c 79 0a 2a ap is the only.*
13a27 2a 20 74 68 61 74 20 63 61 6e 20 67 6f 20 77 72 * that can go wr
13a28 6f 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 73 65 ong with an inse
13a29 72 74 2c 20 61 73 73 75 6d 69 6e 67 20 70 20 61 rt, assuming p a
13a2a 6e 64 20 69 20 61 72 65 20 76 61 6c 69 64 2e 0a nd i are valid..
13a2b 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e **.** The callin
13a2c 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 g function must
13a2d 65 6e 73 75 72 65 20 74 68 61 74 20 70 20 69 73 ensure that p is
13a2e 20 61 20 76 61 6c 69 64 20 42 69 74 76 65 63 20 a valid Bitvec
13a2f 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 74 68 object.** and th
13a30 61 74 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 at the value for
13a31 20 22 69 22 20 69 73 20 77 69 74 68 69 6e 20 72 "i" is within r
13a32 61 6e 67 65 20 6f 66 20 74 68 65 20 42 69 74 76 ange of the Bitv
13a33 65 63 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f 74 ec object..** Ot
13a34 68 65 72 77 69 73 65 20 74 68 65 20 62 65 68 61 herwise the beha
13a35 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 vior is undefine
13a36 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
13a37 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
13a38 42 69 74 76 65 63 53 65 74 28 42 69 74 76 65 63 BitvecSet(Bitvec
13a39 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 75 *p, u32 i){. u
13a3a 33 32 20 68 3b 0a 20 20 69 66 28 20 70 3d 3d 30 32 h;. if( p==0
13a3b 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
13a3c 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 _OK;. assert( i
13a3d 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
13a3e 69 3c 3d 70 2d 3e 69 53 69 7a 65 20 29 3b 0a 20 i<=p->iSize );.
13a3f 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 28 70 i--;. while((p
13a40 2d 3e 69 53 69 7a 65 20 3e 20 42 49 54 56 45 43 ->iSize > BITVEC
13a41 5f 4e 42 49 54 29 20 26 26 20 70 2d 3e 69 44 69 _NBIT) && p->iDi
13a42 76 69 73 6f 72 29 20 7b 0a 20 20 20 20 75 33 32 visor) {. u32
13a43 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 bin = i/p->iDiv
13a44 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 isor;. i = i%
13a45 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 p->iDivisor;.
13a46 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 5b if( p->u.apSub[
13a47 62 69 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 bin]==0 ){.
13a48 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d p->u.apSub[bin]
13a49 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 = sqlite3Bitvec
13a4a 43 72 65 61 74 65 28 20 70 2d 3e 69 44 69 76 69 Create( p->iDivi
13a4b 73 6f 72 20 29 3b 0a 20 20 20 20 20 20 69 66 28 sor );. if(
13a4c 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d p->u.apSub[bin]
13a4d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c ==0 ) return SQL
13a4e 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
13a4f 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 . p = p->u.ap
13a50 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 7d 0a 20 20 Sub[bin];. }.
13a51 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 if( p->iSize<=BI
13a52 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 TVEC_NBIT ){.
13a53 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f p->u.aBitmap[i/
13a54 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 7c BITVEC_SZELEM] |
13a55 3d 20 31 20 3c 3c 20 28 69 26 28 42 49 54 56 45 = 1 << (i&(BITVE
13a56 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 3b 0a 20 20 C_SZELEM-1));.
13a57 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
13a58 4f 4b 3b 0a 20 20 7d 0a 20 20 68 20 3d 20 42 49 OK;. }. h = BI
13a59 54 56 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0a TVEC_HASH(i++);.
13a5a 20 20 2f 2a 20 69 66 20 74 68 65 72 65 20 77 61 /* if there wa
13a5b 73 6e 27 74 20 61 20 68 61 73 68 20 63 6f 6c 6c sn't a hash coll
13a5c 69 73 69 6f 6e 2c 20 61 6e 64 20 74 68 69 73 20 ision, and this
13a5d 64 6f 65 73 6e 27 74 20 2a 2f 0a 20 20 2f 2a 20 doesn't */. /*
13a5e 63 6f 6d 70 6c 65 74 65 6c 79 20 66 69 6c 6c 20 completely fill
13a5f 74 68 65 20 68 61 73 68 2c 20 74 68 65 6e 20 6a the hash, then j
13a60 75 73 74 20 61 64 64 20 69 74 20 77 69 74 68 6f ust add it witho
13a61 75 74 20 2a 2f 0a 20 20 2f 2a 20 77 6f 72 72 69 ut */. /* worri
13a62 6e 67 20 61 62 6f 75 74 20 73 75 62 2d 64 69 76 ng about sub-div
13a63 69 64 69 6e 67 20 61 6e 64 20 72 65 2d 68 61 73 iding and re-has
13a64 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 20 21 hing. */. if( !
13a65 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b p->u.aHash[h] ){
13a66 0a 20 20 20 20 69 66 20 28 70 2d 3e 6e 53 65 74 . if (p->nSet
13a67 3c 28 42 49 54 56 45 43 5f 4e 49 4e 54 2d 31 29 <(BITVEC_NINT-1)
13a68 29 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 ) {. goto b
13a69 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3b 0a 20 itvec_set_end;.
13a6a 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
13a6b 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 73 65 goto bitvec_se
13a6c 74 5f 72 65 68 61 73 68 3b 0a 20 20 20 20 7d 0a t_rehash;. }.
13a6d 20 20 7d 0a 20 20 2f 2a 20 74 68 65 72 65 20 77 }. /* there w
13a6e 61 73 20 61 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 as a collision,
13a6f 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 check to see if
13a70 69 74 27 73 20 61 6c 72 65 61 64 79 20 2a 2f 0a it's already */.
13a71 20 20 2f 2a 20 69 6e 20 68 61 73 68 2c 20 69 66 /* in hash, if
13a72 20 6e 6f 74 2c 20 74 72 79 20 74 6f 20 66 69 6e not, try to fin
13a73 64 20 61 20 73 70 6f 74 20 66 6f 72 20 69 74 20 d a spot for it
13a74 2a 2f 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 66 */. do {. if
13a75 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d ( p->u.aHash[h]=
13a76 3d 69 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 =i ) return SQLI
13a77 54 45 5f 4f 4b 3b 0a 20 20 20 20 68 2b 2b 3b 0a TE_OK;. h++;.
13a78 20 20 20 20 69 66 28 20 68 3e 3d 42 49 54 56 45 if( h>=BITVE
13a79 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b 0a C_NINT ) h = 0;.
13a7a 20 20 7d 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e } while( p->u.
13a7b 61 48 61 73 68 5b 68 5d 20 29 3b 0a 20 20 2f 2a aHash[h] );. /*
13a7c 20 77 65 20 64 69 64 6e 27 74 20 66 69 6e 64 20 we didn't find
13a7d 69 74 20 69 6e 20 74 68 65 20 68 61 73 68 2e 20 it in the hash.
13a7e 20 68 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 h points to the
13a7f 20 66 69 72 73 74 20 2a 2f 0a 20 20 2f 2a 20 61 first */. /* a
13a80 76 61 69 6c 61 62 6c 65 20 66 72 65 65 20 73 70 vailable free sp
13a81 6f 74 2e 20 63 68 65 63 6b 20 74 6f 20 73 65 65 ot. check to see
13a82 20 69 66 20 74 68 69 73 20 69 73 20 67 6f 69 6e if this is goin
13a83 67 20 74 6f 20 2a 2f 0a 20 20 2f 2a 20 6d 61 6b g to */. /* mak
13a84 65 20 6f 75 72 20 68 61 73 68 20 74 6f 6f 20 22 e our hash too "
13a85 66 75 6c 6c 22 2e 20 20 2a 2f 0a 62 69 74 76 65 full". */.bitve
13a86 63 5f 73 65 74 5f 72 65 68 61 73 68 3a 0a 20 20 c_set_rehash:.
13a87 69 66 28 20 70 2d 3e 6e 53 65 74 3e 3d 42 49 54 if( p->nSet>=BIT
13a88 56 45 43 5f 4d 58 48 41 53 48 20 29 7b 0a 20 20 VEC_MXHASH ){.
13a89 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6a unsigned int j
13a8a 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 ;. int rc;.
13a8b 20 20 75 33 32 20 2a 61 69 56 61 6c 75 65 73 20 u32 *aiValues
13a8c 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c = sqlite3StackAl
13a8d 6c 6f 63 52 61 77 28 30 2c 20 73 69 7a 65 6f 66 locRaw(0, sizeof
13a8e 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 (p->u.aHash));.
13a8f 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 3d if( aiValues=
13a90 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 =0 ){. retu
13a91 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
13a92 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
13a93 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c 75 65 memcpy(aiValue
13a94 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20 73 s, p->u.aHash, s
13a95 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68 izeof(p->u.aHash
13a96 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 ));. memset
13a97 28 70 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c 20 (p->u.apSub, 0,
13a98 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 70 53 75 sizeof(p->u.apSu
13a99 62 29 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 44 b));. p->iD
13a9a 69 76 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 69 ivisor = (p->iSi
13a9b 7a 65 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 52 ze + BITVEC_NPTR
13a9c 20 2d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 54 - 1)/BITVEC_NPT
13a9d 52 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 R;. rc = sq
13a9e 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 lite3BitvecSet(p
13a9f 2c 20 69 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 , i);. for(
13aa0 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49 j=0; j<BITVEC_NI
13aa1 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 NT; j++){.
13aa2 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 5b 6a if( aiValues[j
13aa3 5d 20 29 20 72 63 20 7c 3d 20 73 71 6c 69 74 65 ] ) rc |= sqlite
13aa4 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 61 69 3BitvecSet(p, ai
13aa5 56 61 6c 75 65 73 5b 6a 5d 29 3b 0a 20 20 20 20 Values[j]);.
13aa6 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
13aa7 33 53 74 61 63 6b 46 72 65 65 28 30 2c 20 61 69 3StackFree(0, ai
13aa8 56 61 6c 75 65 73 29 3b 0a 20 20 20 20 20 20 72 Values);. r
13aa9 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
13aaa 20 20 7d 0a 62 69 74 76 65 63 5f 73 65 74 5f 65 }.bitvec_set_e
13aab 6e 64 3a 0a 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b nd:. p->nSet++;
13aac 0a 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d . p->u.aHash[h]
13aad 20 3d 20 69 3b 0a 20 20 72 65 74 75 72 6e 20 53 = i;. return S
13aae 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
13aaf 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 69 2d 74 ** Clear the i-t
13ab0 68 20 62 69 74 2e 0a 2a 2a 0a 2a 2a 20 70 42 75 h bit..**.** pBu
13ab1 66 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e f must be a poin
13ab2 74 65 72 20 74 6f 20 61 74 20 6c 65 61 73 74 20 ter to at least
13ab3 42 49 54 56 45 43 5f 53 5a 20 62 79 74 65 73 20 BITVEC_SZ bytes
13ab4 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 73 74 6f of temporary sto
13ab5 72 61 67 65 0a 2a 2a 20 74 68 61 74 20 42 69 74 rage.** that Bit
13ab6 76 65 63 43 6c 65 61 72 20 63 61 6e 20 75 73 65 vecClear can use
13ab7 20 74 6f 20 72 65 62 75 69 6c 74 20 69 74 73 20 to rebuilt its
13ab8 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 hash table..*/.S
13ab9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
13aba 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 id sqlite3Bitvec
13abb 43 6c 65 61 72 28 42 69 74 76 65 63 20 2a 70 2c Clear(Bitvec *p,
13abc 20 75 33 32 20 69 2c 20 76 6f 69 64 20 2a 70 42 u32 i, void *pB
13abd 75 66 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 uf){. if( p==0
13abe 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 ) return;. asse
13abf 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 69 2d 2d rt( i>0 );. i--
13ac0 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 69 44 ;. while( p->iD
13ac1 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 33 ivisor ){. u3
13ac2 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 2 bin = i/p->iDi
13ac3 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 visor;. i = i
13ac4 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 %p->iDivisor;.
13ac5 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 p = p->u.apSub
13ac6 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28 21 [bin];. if (!
13ac7 70 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 p) {. retur
13ac8 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 n;. }. }. i
13ac9 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 f( p->iSize<=BIT
13aca 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 VEC_NBIT ){.
13acb 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 p->u.aBitmap[i/B
13acc 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 3d ITVEC_SZELEM] &=
13acd 20 7e 28 31 20 3c 3c 20 28 69 26 28 42 49 54 56 ~(1 << (i&(BITV
13ace 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 3b 0a EC_SZELEM-1)));.
13acf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 73 }else{. uns
13ad0 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 igned int j;.
13ad1 20 75 33 32 20 2a 61 69 56 61 6c 75 65 73 20 3d u32 *aiValues =
13ad2 20 70 42 75 66 3b 0a 20 20 20 20 6d 65 6d 63 70 pBuf;. memcp
13ad3 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e 75 y(aiValues, p->u
13ad4 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 70 .aHash, sizeof(p
13ad5 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 ->u.aHash));.
13ad6 20 6d 65 6d 73 65 74 28 70 2d 3e 75 2e 61 48 61 memset(p->u.aHa
13ad7 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d sh, 0, sizeof(p-
13ad8 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 20 >u.aHash));.
13ad9 70 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20 20 p->nSet = 0;.
13ada 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54 56 for(j=0; j<BITV
13adb 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 EC_NINT; j++){.
13adc 20 20 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 if( aiValue
13add 73 5b 6a 5d 20 26 26 20 61 69 56 61 6c 75 65 73 s[j] && aiValues
13ade 5b 6a 5d 21 3d 28 69 2b 31 29 20 29 7b 0a 20 20 [j]!=(i+1) ){.
13adf 20 20 20 20 20 20 75 33 32 20 68 20 3d 20 42 49 u32 h = BI
13ae0 54 56 45 43 5f 48 41 53 48 28 61 69 56 61 6c 75 TVEC_HASH(aiValu
13ae1 65 73 5b 6a 5d 2d 31 29 3b 0a 20 20 20 20 20 20 es[j]-1);.
13ae2 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 20 p->nSet++;.
13ae3 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 while( p->u
13ae4 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 .aHash[h] ){.
13ae5 20 20 20 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 h++;.
13ae6 20 20 20 20 20 20 69 66 28 20 68 3e 3d 42 49 54 if( h>=BIT
13ae7 56 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 VEC_NINT ) h = 0
13ae8 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
13ae9 20 20 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 p->u.aHash[h
13aea 5d 20 3d 20 61 69 56 61 6c 75 65 73 5b 6a 5d 3b ] = aiValues[j];
13aeb 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
13aec 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 }.}../*.** Dest
13aed 72 6f 79 20 61 20 62 69 74 6d 61 70 20 6f 62 6a roy a bitmap obj
13aee 65 63 74 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c ect. Reclaim al
13aef 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 2e 0a 2a l memory used..*
13af0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13af1 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 void sqlite3Bit
13af2 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76 65 vecDestroy(Bitve
13af3 63 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d c *p){. if( p==
13af4 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 0 ) return;. if
13af5 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b ( p->iDivisor ){
13af6 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e . unsigned in
13af7 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 t i;. for(i=0
13af8 3b 20 69 3c 42 49 54 56 45 43 5f 4e 50 54 52 3b ; i<BITVEC_NPTR;
13af9 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c i++){. sql
13afa 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f ite3BitvecDestro
13afb 79 28 70 2d 3e 75 2e 61 70 53 75 62 5b 69 5d 29 y(p->u.apSub[i])
13afc 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 ;. }. }. sq
13afd 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d lite3_free(p);.}
13afe 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
13aff 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
13b00 69 53 69 7a 65 20 70 61 72 61 6d 65 74 65 72 20 iSize parameter
13b01 73 70 65 63 69 66 69 65 64 20 77 68 65 6e 20 42 specified when B
13b02 69 74 76 65 63 20 2a 70 0a 2a 2a 20 77 61 73 20 itvec *p.** was
13b03 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 created..*/.SQLI
13b04 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 TE_PRIVATE u32 s
13b05 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 qlite3BitvecSize
13b06 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 20 72 (Bitvec *p){. r
13b07 65 74 75 72 6e 20 70 2d 3e 69 53 69 7a 65 3b 0a eturn p->iSize;.
13b08 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
13b09 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 E_OMIT_BUILTIN_T
13b0a 45 53 54 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 56 5b EST./*.** Let V[
13b0b 5d 20 62 65 20 61 6e 20 61 72 72 61 79 20 6f 66 ] be an array of
13b0c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 unsigned charac
13b0d 74 65 72 73 20 73 75 66 66 69 63 69 65 6e 74 20 ters sufficient
13b0e 74 6f 20 68 6f 6c 64 0a 2a 2a 20 75 70 20 74 6f to hold.** up to
13b0f 20 4e 20 62 69 74 73 2e 20 20 4c 65 74 20 49 20 N bits. Let I
13b10 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 be an integer be
13b11 74 77 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20 20 tween 0 and N.
13b12 30 3c 3d 49 3c 4e 2e 0a 2a 2a 20 54 68 65 6e 20 0<=I<N..** Then
13b13 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 the following ma
13b14 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 cros can be used
13b15 20 74 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c 20 to set, clear,
13b16 6f 72 20 74 65 73 74 0a 2a 2a 20 69 6e 64 69 76 or test.** indiv
13b17 69 64 75 61 6c 20 62 69 74 73 20 77 69 74 68 69 idual bits withi
13b18 6e 20 56 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 n V..*/.#define
13b19 53 45 54 42 49 54 28 56 2c 49 29 20 20 20 20 20 SETBIT(V,I)
13b1a 20 56 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31 3c 3c V[I>>3] |= (1<<
13b1b 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20 43 (I&7)).#define C
13b1c 4c 45 41 52 42 49 54 28 56 2c 49 29 20 20 20 20 LEARBIT(V,I)
13b1d 56 5b 49 3e 3e 33 5d 20 26 3d 20 7e 28 31 3c 3c V[I>>3] &= ~(1<<
13b1e 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20 54 (I&7)).#define T
13b1f 45 53 54 42 49 54 28 56 2c 49 29 20 20 20 20 20 ESTBIT(V,I)
13b20 28 56 5b 49 3e 3e 33 5d 26 28 31 3c 3c 28 49 26 (V[I>>3]&(1<<(I&
13b21 37 29 29 29 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20 54 7)))!=0../*.** T
13b22 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 his routine runs
13b23 20 61 6e 20 65 78 74 65 6e 73 69 76 65 20 74 65 an extensive te
13b24 73 74 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 st of the Bitvec
13b25 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 code..**.** The
13b26 20 69 6e 70 75 74 20 69 73 20 61 6e 20 61 72 72 input is an arr
13b27 61 79 20 6f 66 20 69 6e 74 65 67 65 72 73 20 74 ay of integers t
13b28 68 61 74 20 61 63 74 73 20 61 73 20 61 20 70 72 hat acts as a pr
13b29 6f 67 72 61 6d 0a 2a 2a 20 74 6f 20 74 65 73 74 ogram.** to test
13b2a 20 74 68 65 20 42 69 74 76 65 63 2e 20 20 54 68 the Bitvec. Th
13b2b 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 6f e integers are o
13b2c 70 63 6f 64 65 73 20 66 6f 6c 6c 6f 77 65 64 0a pcodes followed.
13b2d 2a 2a 20 62 79 20 30 2c 20 31 2c 20 6f 72 20 33 ** by 0, 1, or 3
13b2e 20 6f 70 65 72 61 6e 64 73 2c 20 64 65 70 65 6e operands, depen
13b2f 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 63 6f ding on the opco
13b30 64 65 2e 20 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 de. Another.**
13b31 6f 70 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 69 opcode follows i
13b32 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 mmediately after
13b33 20 74 68 65 20 6c 61 73 74 20 6f 70 65 72 61 6e the last operan
13b34 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 d..**.** There a
13b35 72 65 20 36 20 6f 70 63 6f 64 65 73 20 6e 75 6d re 6 opcodes num
13b36 62 65 72 65 64 20 66 72 6f 6d 20 30 20 74 68 72 bered from 0 thr
13b37 6f 75 67 68 20 35 2e 20 20 30 20 69 73 20 74 68 ough 5. 0 is th
13b38 65 0a 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63 6f e.** "halt" opco
13b39 64 65 20 61 6e 64 20 63 61 75 73 65 73 20 74 68 de and causes th
13b3a 65 20 74 65 73 74 20 74 6f 20 65 6e 64 2e 0a 2a e test to end..*
13b3b 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20 20 *.** 0
13b3c 20 20 20 48 61 6c 74 20 61 6e 64 20 72 65 74 75 Halt and retu
13b3d 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
13b3e 20 65 72 72 6f 72 73 0a 2a 2a 20 20 20 20 31 20 errors.** 1
13b3f 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 20 62 N S X Set N b
13b40 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 its beginning wi
13b41 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d 65 th S and increme
13b42 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20 nting by X.**
13b43 20 32 20 4e 20 53 20 58 20 20 20 20 43 6c 65 61 2 N S X Clea
13b44 72 20 4e 20 62 69 74 73 20 62 65 67 69 6e 6e 69 r N bits beginni
13b45 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20 69 6e ng with S and in
13b46 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 0a crementing by X.
13b47 2a 2a 20 20 20 20 33 20 4e 20 20 20 20 20 20 20 ** 3 N
13b48 20 53 65 74 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 Set N randomly
13b49 63 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 chosen bits.**
13b4a 20 20 34 20 4e 20 20 20 20 20 20 20 20 43 6c 65 4 N Cle
13b4b 61 72 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68 ar N randomly ch
13b4c 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20 osen bits.**
13b4d 35 20 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 5 N S X Set N
13b4e 20 62 69 74 73 20 66 72 6f 6d 20 53 20 69 6e 63 bits from S inc
13b4f 72 65 6d 65 6e 74 20 58 20 69 6e 20 61 72 72 61 rement X in arra
13b50 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 69 6e 20 62 y only, not in b
13b51 69 74 76 65 63 0a 2a 2a 0a 2a 2a 20 54 68 65 20 itvec.**.** The
13b52 6f 70 63 6f 64 65 73 20 31 20 74 68 72 6f 75 67 opcodes 1 throug
13b53 68 20 34 20 70 65 72 66 6f 72 6d 20 73 65 74 20 h 4 perform set
13b54 61 6e 64 20 63 6c 65 61 72 20 6f 70 65 72 61 74 and clear operat
13b55 69 6f 6e 73 20 61 72 65 20 70 65 72 66 6f 72 6d ions are perform
13b56 65 64 0a 2a 2a 20 6f 6e 20 62 6f 74 68 20 61 20 ed.** on both a
13b57 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 61 6e Bitvec object an
13b58 64 20 6f 6e 20 61 20 6c 69 6e 65 61 72 20 61 72 d on a linear ar
13b59 72 61 79 20 6f 66 20 62 69 74 73 20 6f 62 74 61 ray of bits obta
13b5a 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 ined from malloc
13b5b 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 77 6f ..** Opcode 5 wo
13b5c 72 6b 73 20 6f 6e 20 74 68 65 20 6c 69 6e 65 61 rks on the linea
13b5d 72 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f r array only, no
13b5e 74 20 6f 6e 20 74 68 65 20 42 69 74 76 65 63 2e t on the Bitvec.
13b5f 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 69 73 20 .** Opcode 5 is
13b60 75 73 65 64 20 74 6f 20 64 65 6c 69 62 65 72 61 used to delibera
13b61 74 65 6c 79 20 69 6e 64 75 63 65 20 61 20 66 61 tely induce a fa
13b62 75 6c 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a ult in order to.
13b63 2a 2a 20 63 6f 6e 66 69 72 6d 20 74 68 61 74 20 ** confirm that
13b64 65 72 72 6f 72 20 64 65 74 65 63 74 69 6f 6e 20 error detection
13b65 77 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 works..**.** At
13b66 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f the conclusion o
13b67 66 20 74 68 65 20 74 65 73 74 20 74 68 65 20 6c f the test the l
13b68 69 6e 65 61 72 20 61 72 72 61 79 20 69 73 20 63 inear array is c
13b69 6f 6d 70 61 72 65 64 0a 2a 2a 20 61 67 61 69 6e ompared.** again
13b6a 73 74 20 74 68 65 20 42 69 74 76 65 63 20 6f 62 st the Bitvec ob
13b6b 6a 65 63 74 2e 20 20 49 66 20 74 68 65 72 65 20 ject. If there
13b6c 61 72 65 20 61 6e 79 20 64 69 66 66 65 72 65 6e are any differen
13b6d 63 65 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 ces,.** an error
13b6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 is returned. I
13b6f 66 20 74 68 65 79 20 61 72 65 20 74 68 65 20 73 f they are the s
13b70 61 6d 65 2c 20 7a 65 72 6f 20 69 73 20 72 65 74 ame, zero is ret
13b71 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
13b72 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
13b73 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ion error occurs
13b74 2c 20 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a , return -1..*/.
13b75 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
13b76 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 nt sqlite3Bitvec
13b77 42 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 20 BuiltinTest(int
13b78 73 7a 2c 20 69 6e 74 20 2a 61 4f 70 29 7b 0a 20 sz, int *aOp){.
13b79 20 42 69 74 76 65 63 20 2a 70 42 69 74 76 65 63 Bitvec *pBitvec
13b7a 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 = 0;. unsigned
13b7b 20 63 68 61 72 20 2a 70 56 20 3d 20 30 3b 0a 20 char *pV = 0;.
13b7c 20 69 6e 74 20 72 63 20 3d 20 2d 31 3b 0a 20 20 int rc = -1;.
13b7d 69 6e 74 20 69 2c 20 6e 78 2c 20 70 63 2c 20 6f int i, nx, pc, o
13b7e 70 3b 0a 20 20 76 6f 69 64 20 2a 70 54 6d 70 53 p;. void *pTmpS
13b7f 70 61 63 65 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f pace;.. /* Allo
13b80 63 61 74 65 20 74 68 65 20 42 69 74 76 65 63 20 cate the Bitvec
13b81 74 6f 20 62 65 20 74 65 73 74 65 64 20 61 6e 64 to be tested and
13b82 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 a linear array
13b83 6f 66 0a 20 20 2a 2a 20 62 69 74 73 20 74 6f 20 of. ** bits to
13b84 61 63 74 20 61 73 20 74 68 65 20 72 65 66 65 72 act as the refer
13b85 65 6e 63 65 20 2a 2f 0a 20 20 70 42 69 74 76 65 ence */. pBitve
13b86 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 c = sqlite3Bitve
13b87 63 43 72 65 61 74 65 28 20 73 7a 20 29 3b 0a 20 cCreate( sz );.
13b88 20 70 56 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c pV = sqlite3Mal
13b89 6c 6f 63 5a 65 72 6f 28 20 28 73 7a 2b 37 29 2f locZero( (sz+7)/
13b8a 38 20 2b 20 31 20 29 3b 0a 20 20 70 54 6d 70 53 8 + 1 );. pTmpS
13b8b 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d pace = sqlite3_m
13b8c 61 6c 6c 6f 63 28 42 49 54 56 45 43 5f 53 5a 29 alloc(BITVEC_SZ)
13b8d 3b 0a 20 20 69 66 28 20 70 42 69 74 76 65 63 3d ;. if( pBitvec=
13b8e 3d 30 20 7c 7c 20 70 56 3d 3d 30 20 7c 7c 20 70 =0 || pV==0 || p
13b8f 54 6d 70 53 70 61 63 65 3d 3d 30 20 20 29 20 67 TmpSpace==0 ) g
13b90 6f 74 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a oto bitvec_end;.
13b91 0a 20 20 2f 2a 20 4e 55 4c 4c 20 70 42 69 74 76 . /* NULL pBitv
13b92 65 63 20 74 65 73 74 73 20 2a 2f 0a 20 20 73 71 ec tests */. sq
13b93 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 30 lite3BitvecSet(0
13b94 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 , 1);. sqlite3B
13b95 69 74 76 65 63 43 6c 65 61 72 28 30 2c 20 31 2c itvecClear(0, 1,
13b96 20 70 54 6d 70 53 70 61 63 65 29 3b 0a 0a 20 20 pTmpSpace);..
13b97 2f 2a 20 52 75 6e 20 74 68 65 20 70 72 6f 67 72 /* Run the progr
13b98 61 6d 20 2a 2f 0a 20 20 70 63 20 3d 20 30 3b 0a am */. pc = 0;.
13b99 20 20 77 68 69 6c 65 28 20 28 6f 70 20 3d 20 61 while( (op = a
13b9a 4f 70 5b 70 63 5d 29 21 3d 30 20 29 7b 0a 20 20 Op[pc])!=0 ){.
13b9b 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
13b9c 20 20 20 20 20 20 63 61 73 65 20 31 3a 0a 20 20 case 1:.
13b9d 20 20 20 20 63 61 73 65 20 32 3a 0a 20 20 20 20 case 2:.
13b9e 20 20 63 61 73 65 20 35 3a 20 7b 0a 20 20 20 20 case 5: {.
13b9f 20 20 20 20 6e 78 20 3d 20 34 3b 0a 20 20 20 20 nx = 4;.
13ba0 20 20 20 20 69 20 3d 20 61 4f 70 5b 70 63 2b 32 i = aOp[pc+2
13ba1 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 61 ] - 1;. a
13ba2 4f 70 5b 70 63 2b 32 5d 20 2b 3d 20 61 4f 70 5b Op[pc+2] += aOp[
13ba3 70 63 2b 33 5d 3b 0a 20 20 20 20 20 20 20 20 62 pc+3];. b
13ba4 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
13ba5 20 20 20 20 63 61 73 65 20 33 3a 0a 20 20 20 20 case 3:.
13ba6 20 20 63 61 73 65 20 34 3a 20 0a 20 20 20 20 20 case 4: .
13ba7 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 default: {.
13ba8 20 20 20 20 6e 78 20 3d 20 32 3b 0a 20 20 20 20 nx = 2;.
13ba9 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 sqlite3_rand
13baa 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 29 omness(sizeof(i)
13bab 2c 20 26 69 29 3b 0a 20 20 20 20 20 20 20 20 62 , &i);. b
13bac 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
13bad 20 20 7d 0a 20 20 20 20 69 66 28 20 28 2d 2d 61 }. if( (--a
13bae 4f 70 5b 70 63 2b 31 5d 29 20 3e 20 30 20 29 20 Op[pc+1]) > 0 )
13baf 6e 78 20 3d 20 30 3b 0a 20 20 20 20 70 63 20 2b nx = 0;. pc +
13bb0 3d 20 6e 78 3b 0a 20 20 20 20 69 20 3d 20 28 69 = nx;. i = (i
13bb1 20 26 20 30 78 37 66 66 66 66 66 66 66 29 25 73 & 0x7fffffff)%s
13bb2 7a 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 20 26 z;. if( (op &
13bb3 20 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 1)!=0 ){.
13bb4 53 45 54 42 49 54 28 70 56 2c 20 28 69 2b 31 29 SETBIT(pV, (i+1)
13bb5 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 21 );. if( op!
13bb6 3d 35 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 =5 ){. if
13bb7 28 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 ( sqlite3BitvecS
13bb8 65 74 28 70 42 69 74 76 65 63 2c 20 69 2b 31 29 et(pBitvec, i+1)
13bb9 20 29 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 65 ) goto bitvec_e
13bba 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 nd;. }.
13bbb 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 43 4c 45 }else{. CLE
13bbc 41 52 42 49 54 28 70 56 2c 20 28 69 2b 31 29 29 ARBIT(pV, (i+1))
13bbd 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ;. sqlite3B
13bbe 69 74 76 65 63 43 6c 65 61 72 28 70 42 69 74 76 itvecClear(pBitv
13bbf 65 63 2c 20 69 2b 31 2c 20 70 54 6d 70 53 70 61 ec, i+1, pTmpSpa
13bc0 63 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ce);. }. }..
13bc1 20 20 2f 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b /* Test to mak
13bc2 65 20 73 75 72 65 20 74 68 65 20 6c 69 6e 65 61 e sure the linea
13bc3 72 20 61 72 72 61 79 20 65 78 61 63 74 6c 79 20 r array exactly
13bc4 6d 61 74 63 68 65 73 20 74 68 65 0a 20 20 2a 2a matches the. **
13bc5 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 Bitvec object.
13bc6 20 53 74 61 72 74 20 77 69 74 68 20 74 68 65 20 Start with the
13bc7 61 73 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 assumption that
13bc8 74 68 65 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74 they do. ** mat
13bc9 63 68 20 28 72 63 3d 3d 30 29 2e 20 20 43 68 61 ch (rc==0). Cha
13bca 6e 67 65 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65 nge rc to non-ze
13bcb 72 6f 20 69 66 20 61 20 64 69 73 63 72 65 70 61 ro if a discrepa
13bcc 6e 63 79 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e ncy. ** is foun
13bcd 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 d.. */. rc = s
13bce 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
13bcf 28 30 2c 30 29 20 2b 20 73 71 6c 69 74 65 33 42 (0,0) + sqlite3B
13bd0 69 74 76 65 63 54 65 73 74 28 70 42 69 74 76 65 itvecTest(pBitve
13bd1 63 2c 20 73 7a 2b 31 29 0a 20 20 20 20 20 20 20 c, sz+1).
13bd2 20 20 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76 + sqlite3Bitv
13bd3 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 20 ecTest(pBitvec,
13bd4 30 29 0a 20 20 20 20 20 20 20 20 20 20 2b 20 28 0). + (
13bd5 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a sqlite3BitvecSiz
13bd6 65 28 70 42 69 74 76 65 63 29 20 2d 20 73 7a 29 e(pBitvec) - sz)
13bd7 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d ;. for(i=1; i<=
13bd8 73 7a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 sz; i++){. if
13bd9 28 20 20 28 54 45 53 54 42 49 54 28 70 56 2c 69 ( (TESTBIT(pV,i
13bda 29 29 21 3d 73 71 6c 69 74 65 33 42 69 74 76 65 ))!=sqlite3Bitve
13bdb 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 69 29 cTest(pBitvec,i)
13bdc 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 ){. rc = i
13bdd 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
13bde 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 }. }.. /* F
13bdf 72 65 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 ree allocated st
13be0 72 75 63 74 75 72 65 20 2a 2f 0a 62 69 74 76 65 ructure */.bitve
13be1 63 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 c_end:. sqlite3
13be2 5f 66 72 65 65 28 70 54 6d 70 53 70 61 63 65 29 _free(pTmpSpace)
13be3 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
13be4 28 70 56 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 (pV);. sqlite3B
13be5 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 42 69 itvecDestroy(pBi
13be6 74 76 65 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 tvec);. return
13be7 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 rc;.}.#endif /*
13be8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
13be9 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a TIN_TEST */../**
13bea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
13beb 20 6f 66 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a of bitvec.c ***
13bec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13bed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13bee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
13bef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
13bf0 69 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e 63 in file pcache.c
13bf1 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
13bf2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13bf3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
13bf4 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30 ** 2008 August 0
13bf5 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
13bf6 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
13bf7 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
13bf8 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
13bf9 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
13bfa 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
13bfb 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
13bfc 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
13bfd 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
13bfe 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
13bff 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
13c00 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
13c01 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
13c02 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
13c03 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
13c04 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
13c05 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
13c06 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
13c07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13c08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13c09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13c0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13c0b 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d .** This file im
13c0c 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 70 61 plements that pa
13c0d 67 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 0a 2f 2a ge cache..*/../*
13c0e 0a 2a 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 .** A complete p
13c0f 61 67 65 20 63 61 63 68 65 20 69 73 20 61 6e 20 age cache is an
13c10 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
13c11 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 structure..*/.s
13c12 74 72 75 63 74 20 50 43 61 63 68 65 20 7b 0a 20 truct PCache {.
13c13 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 2c 20 PgHdr *pDirty,
13c14 2a 70 44 69 72 74 79 54 61 69 6c 3b 20 20 20 20 *pDirtyTail;
13c15 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 /* List of
13c16 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 4c dirty pages in L
13c17 52 55 20 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 RU order */. Pg
13c18 48 64 72 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 Hdr *pSynced;
13c19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13c1a 20 20 2f 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 /* Last synced
13c1b 20 70 61 67 65 20 69 6e 20 64 69 72 74 79 20 70 page in dirty p
13c1c 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e age list */. in
13c1d 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 t nRef;
13c1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13c1f 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
13c20 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 eferenced pages
13c21 2a 2f 0a 20 20 69 6e 74 20 73 7a 43 61 63 68 65 */. int szCache
13c22 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
13c23 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 /* Conf
13c24 69 67 75 72 65 64 20 63 61 63 68 65 20 73 69 7a igured cache siz
13c25 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 e */. int szPag
13c26 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
13c27 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
13c28 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 ze of every page
13c29 20 69 6e 20 74 68 69 73 20 63 61 63 68 65 20 2a in this cache *
13c2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61 3b /. int szExtra;
13c2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13c2c 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
13c2d 6f 66 20 65 78 74 72 61 20 73 70 61 63 65 20 66 of extra space f
13c2e 6f 72 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a or each page */.
13c2f 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 int bPurgeable
13c30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
13c31 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
13c32 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61 pages are on ba
13c33 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 cking store */.
13c34 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28 int (*xStress)(
13c35 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 3b 20 20 void*,PgHdr*);
13c36 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 /* Call to
13c37 74 72 79 20 6d 61 6b 65 20 61 20 70 61 67 65 20 try make a page
13c38 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 clean */. void
13c39 2a 70 53 74 72 65 73 73 3b 20 20 20 20 20 20 20 *pStress;
13c3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
13c3b 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 * Argument to xS
13c3c 74 72 65 73 73 20 2a 2f 0a 20 20 73 71 6c 69 74 tress */. sqlit
13c3d 65 33 5f 70 63 61 63 68 65 20 2a 70 43 61 63 68 e3_pcache *pCach
13c3e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e; /
13c3f 2a 20 50 6c 75 67 67 61 62 6c 65 20 63 61 63 68 * Pluggable cach
13c40 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 50 67 e module */. Pg
13c41 48 64 72 20 2a 70 50 61 67 65 31 3b 20 20 20 20 Hdr *pPage1;
13c42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13c43 20 20 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 /* Reference t
13c44 6f 20 70 61 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a o page 1 */.};..
13c45 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 /*.** Some of th
13c46 65 20 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f e assert() macro
13c47 73 20 69 6e 20 74 68 69 73 20 63 6f 64 65 20 61 s in this code a
13c48 72 65 20 74 6f 6f 20 65 78 70 65 6e 73 69 76 65 re too expensive
13c49 20 74 6f 20 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 to run.** even
13c4a 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 during normal de
13c4b 62 75 67 67 69 6e 67 2e 20 20 55 73 65 20 74 68 bugging. Use th
13c4c 65 6d 20 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f em only rarely o
13c4d 6e 20 6c 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a n long-running.*
13c4e 2a 20 74 65 73 74 73 2e 20 20 45 6e 61 62 6c 65 * tests. Enable
13c4f 20 74 68 65 20 65 78 70 65 6e 73 69 76 65 20 61 the expensive a
13c50 73 73 65 72 74 73 20 75 73 69 6e 67 20 74 68 65 sserts using the
13c51 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f 45 4e 41 .** -DSQLITE_ENA
13c52 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 BLE_EXPENSIVE_AS
13c53 53 45 52 54 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 SERT=1 compile-t
13c54 69 6d 65 20 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 ime option..*/.#
13c55 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
13c56 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 BLE_EXPENSIVE_AS
13c57 53 45 52 54 0a 23 20 64 65 66 69 6e 65 20 65 78 SERT.# define ex
13c58 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58 pensive_assert(X
13c59 29 20 20 61 73 73 65 72 74 28 58 29 0a 23 65 6c ) assert(X).#el
13c5a 73 65 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 se.# define expe
13c5b 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58 29 0a nsive_assert(X).
13c5c 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a #endif../*******
13c5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13c5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b *********** Link
13c5f 65 64 20 4c 69 73 74 20 4d 61 6e 61 67 65 6d 65 ed List Manageme
13c60 6e 74 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt *************
13c61 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 *******/..#if !d
13c62 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 efined(NDEBUG) &
13c63 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 & defined(SQLITE
13c64 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 _ENABLE_EXPENSIV
13c65 45 5f 41 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 E_ASSERT)./*.**
13c66 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 Check that the p
13c67 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 76 Cache->pSynced v
13c68 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 63 ariable is set c
13c69 6f 72 72 65 63 74 6c 79 2e 20 49 66 20 69 74 0a orrectly. If it.
13c6a 2a 2a 20 69 73 20 6e 6f 74 2c 20 65 69 74 68 65 ** is not, eithe
13c6b 72 20 66 61 69 6c 20 61 6e 20 61 73 73 65 72 74 r fail an assert
13c6c 20 6f 72 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e or return zero.
13c6d 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 Otherwise, retu
13c6e 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 rn.** non-zero.
13c6f 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 This is only use
13c70 64 20 69 6e 20 64 65 62 75 67 67 69 6e 67 20 62 d in debugging b
13c71 75 69 6c 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 uilds, as follow
13c72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 65 78 70 65 6e s:.**.** expen
13c73 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 sive_assert( pca
13c74 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 cheCheckSynced(p
13c75 43 61 63 68 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 Cache) );.*/.sta
13c76 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 43 68 tic int pcacheCh
13c77 65 63 6b 53 79 6e 63 65 64 28 50 43 61 63 68 65 eckSynced(PCache
13c78 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 *pCache){. PgH
13c79 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 dr *p;. for(p=p
13c7a 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 Cache->pDirtyTai
13c7b 6c 3b 20 70 21 3d 70 43 61 63 68 65 2d 3e 70 53 l; p!=pCache->pS
13c7c 79 6e 63 65 64 3b 20 70 3d 70 2d 3e 70 44 69 72 ynced; p=p->pDir
13c7d 74 79 50 72 65 76 29 7b 0a 20 20 20 20 61 73 73 tyPrev){. ass
13c7e 65 72 74 28 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 ert( p->nRef ||
13c7f 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f (p->flags&PGHDR_
13c80 4e 45 45 44 5f 53 59 4e 43 29 20 29 3b 0a 20 20 NEED_SYNC) );.
13c81 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 3d 3d 30 }. return (p==0
13c82 20 7c 7c 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 || p->nRef || (
13c83 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e p->flags&PGHDR_N
13c84 45 45 44 5f 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d EED_SYNC)==0);.}
13c85 0a 23 65 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42 .#endif /* !NDEB
13c86 55 47 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 UG && SQLITE_ENA
13c87 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 BLE_EXPENSIVE_AS
13c88 53 45 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 SERT */../*.** R
13c89 65 6d 6f 76 65 20 70 61 67 65 20 70 50 61 67 65 emove page pPage
13c8a 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f from the list o
13c8b 66 20 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a f dirty pages..*
13c8c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
13c8d 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 acheRemoveFromDi
13c8e 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 rtyList(PgHdr *p
13c8f 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 Page){. PCache
13c90 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 *p = pPage->pCac
13c91 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 he;.. assert( p
13c92 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 Page->pDirtyNext
13c93 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 || pPage==p->pD
13c94 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 61 73 irtyTail );. as
13c95 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 sert( pPage->pDi
13c96 72 74 79 50 72 65 76 20 7c 7c 20 70 50 61 67 65 rtyPrev || pPage
13c97 3d 3d 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 0a ==p->pDirty );..
13c98 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 /* Update the
13c99 50 43 61 63 68 65 31 2e 70 53 79 6e 63 65 64 20 PCache1.pSynced
13c9a 76 61 72 69 61 62 6c 65 20 69 66 20 6e 65 63 65 variable if nece
13c9b 73 73 61 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 ssary. */. if(
13c9c 70 2d 3e 70 53 79 6e 63 65 64 3d 3d 70 50 61 67 p->pSynced==pPag
13c9d 65 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a e ){. PgHdr *
13c9e 70 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 2d pSynced = pPage-
13c9f 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 20 >pDirtyPrev;.
13ca0 20 77 68 69 6c 65 28 20 70 53 79 6e 63 65 64 20 while( pSynced
13ca1 26 26 20 28 70 53 79 6e 63 65 64 2d 3e 66 6c 61 && (pSynced->fla
13ca2 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
13ca3 4e 43 29 20 29 7b 0a 20 20 20 20 20 20 70 53 79 NC) ){. pSy
13ca4 6e 63 65 64 20 3d 20 70 53 79 6e 63 65 64 2d 3e nced = pSynced->
13ca5 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 pDirtyPrev;.
13ca6 7d 0a 20 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 }. p->pSynced
13ca7 20 3d 20 70 53 79 6e 63 65 64 3b 0a 20 20 7d 0a = pSynced;. }.
13ca8 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 . if( pPage->pD
13ca9 69 72 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 irtyNext ){.
13caa 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 pPage->pDirtyNex
13cab 74 2d 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 t->pDirtyPrev =
13cac 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 pPage->pDirtyPre
13cad 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 v;. }else{.
13cae 61 73 73 65 72 74 28 20 70 50 61 67 65 3d 3d 70 assert( pPage==p
13caf 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0a ->pDirtyTail );.
13cb0 20 20 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69 p->pDirtyTai
13cb1 6c 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 l = pPage->pDirt
13cb2 79 50 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 yPrev;. }. if(
13cb3 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 pPage->pDirtyPr
13cb4 65 76 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d ev ){. pPage-
13cb5 3e 70 44 69 72 74 79 50 72 65 76 2d 3e 70 44 69 >pDirtyPrev->pDi
13cb6 72 74 79 4e 65 78 74 20 3d 20 70 50 61 67 65 2d rtyNext = pPage-
13cb7 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d >pDirtyNext;. }
13cb8 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
13cb9 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 ( pPage==p->pDir
13cba 74 79 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 ty );. p->pDi
13cbb 72 74 79 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 rty = pPage->pDi
13cbc 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 rtyNext;. }. p
13cbd 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 Page->pDirtyNext
13cbe 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 70 = 0;. pPage->p
13cbf 44 69 72 74 79 50 72 65 76 20 3d 20 30 3b 0a 0a DirtyPrev = 0;..
13cc0 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 expensive_asse
13cc1 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 rt( pcacheCheckS
13cc2 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f ynced(p) );.}../
13cc3 2a 0a 2a 2a 20 41 64 64 20 70 61 67 65 20 70 50 *.** Add page pP
13cc4 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 age to the head
13cc5 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 of the dirty lis
13cc6 74 20 28 50 43 61 63 68 65 31 2e 70 44 69 72 74 t (PCache1.pDirt
13cc7 79 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70 y is set to.** p
13cc8 50 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 Page)..*/.static
13cc9 20 76 6f 69 64 20 70 63 61 63 68 65 41 64 64 54 void pcacheAddT
13cca 6f 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 oDirtyList(PgHdr
13ccb 20 2a 70 50 61 67 65 29 7b 0a 20 20 50 43 61 63 *pPage){. PCac
13ccc 68 65 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 he *p = pPage->p
13ccd 43 61 63 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 Cache;.. assert
13cce 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e ( pPage->pDirtyN
13ccf 65 78 74 3d 3d 30 20 26 26 20 70 50 61 67 65 2d ext==0 && pPage-
13cd0 3e 70 44 69 72 74 79 50 72 65 76 3d 3d 30 20 26 >pDirtyPrev==0 &
13cd1 26 20 70 2d 3e 70 44 69 72 74 79 21 3d 70 50 61 & p->pDirty!=pPa
13cd2 67 65 20 29 3b 0a 0a 20 20 70 50 61 67 65 2d 3e ge );.. pPage->
13cd3 70 44 69 72 74 79 4e 65 78 74 20 3d 20 70 2d 3e pDirtyNext = p->
13cd4 70 44 69 72 74 79 3b 0a 20 20 69 66 28 20 70 50 pDirty;. if( pP
13cd5 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 age->pDirtyNext
13cd6 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
13cd7 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 Page->pDirtyNext
13cd8 2d 3e 70 44 69 72 74 79 50 72 65 76 3d 3d 30 20 ->pDirtyPrev==0
13cd9 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 );. pPage->pD
13cda 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 irtyNext->pDirty
13cdb 50 72 65 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 Prev = pPage;.
13cdc 7d 0a 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 }. p->pDirty =
13cdd 70 50 61 67 65 3b 0a 20 20 69 66 28 20 21 70 2d pPage;. if( !p-
13cde 3e 70 44 69 72 74 79 54 61 69 6c 20 29 7b 0a 20 >pDirtyTail ){.
13cdf 20 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c p->pDirtyTail
13ce0 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 = pPage;. }.
13ce1 69 66 28 20 21 70 2d 3e 70 53 79 6e 63 65 64 20 if( !p->pSynced
13ce2 26 26 20 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c && 0==(pPage->fl
13ce3 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
13ce4 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70 YNC) ){. p->p
13ce5 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 3b 0a Synced = pPage;.
13ce6 20 20 7d 0a 20 20 65 78 70 65 6e 73 69 76 65 5f }. expensive_
13ce7 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 68 assert( pcacheCh
13ce8 65 63 6b 53 79 6e 63 65 64 28 70 29 20 29 3b 0a eckSynced(p) );.
13ce9 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 }../*.** Wrapper
13cea 20 61 72 6f 75 6e 64 20 74 68 65 20 70 6c 75 67 around the plug
13ceb 67 61 62 6c 65 20 63 61 63 68 65 73 20 78 55 6e gable caches xUn
13cec 70 69 6e 20 6d 65 74 68 6f 64 2e 20 49 66 20 74 pin method. If t
13ced 68 65 20 63 61 63 68 65 20 69 73 0a 2a 2a 20 62 he cache is.** b
13cee 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 61 6e eing used for an
13cef 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
13cf0 61 73 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 ase, this functi
13cf1 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a on is a no-op..*
13cf2 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
13cf3 61 63 68 65 55 6e 70 69 6e 28 50 67 48 64 72 20 acheUnpin(PgHdr
13cf4 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 *p){. PCache *p
13cf5 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 Cache = p->pCach
13cf6 65 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d e;. if( pCache-
13cf7 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 >bPurgeable ){.
13cf8 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d if( p->pgno==
13cf9 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 1 ){. pCach
13cfa 65 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 e->pPage1 = 0;.
13cfb 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
13cfc 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
13cfd 63 68 65 32 2e 78 55 6e 70 69 6e 28 70 43 61 63 che2.xUnpin(pCac
13cfe 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2d 3e 70 he->pCache, p->p
13cff 50 61 67 65 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a Page, 0);. }.}.
13d00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
13d01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13d02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
13d03 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6e ***** General In
13d04 74 65 72 66 61 63 65 73 20 2a 2a 2a 2a 2a 2a 0a terfaces ******.
13d05 2a 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 **.** Initialize
13d06 20 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 and shutdown th
13d07 65 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 e page cache sub
13d08 73 79 73 74 65 6d 2e 20 4e 65 69 74 68 65 72 20 system. Neither
13d09 6f 66 20 74 68 65 73 65 20 0a 2a 2a 20 66 75 6e of these .** fun
13d0a 63 74 69 6f 6e 73 20 61 72 65 20 74 68 72 65 61 ctions are threa
13d0b 64 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 dsafe..*/.SQLITE
13d0c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
13d0d 69 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 ite3PcacheInitia
13d0e 6c 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 69 66 lize(void){. if
13d0f 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
13d10 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 49 onfig.pcache2.xI
13d11 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a nit==0 ){. /*
13d12 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d IMPLEMENTATION-
13d13 4f 46 3a 20 52 2d 32 36 38 30 31 2d 36 34 31 33 OF: R-26801-6413
13d14 37 20 49 66 20 74 68 65 20 78 49 6e 69 74 28 29 7 If the xInit()
13d15 20 6d 65 74 68 6f 64 20 69 73 20 4e 55 4c 4c 2c method is NULL,
13d16 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a then the. **
13d17 20 62 75 69 6c 74 2d 69 6e 20 64 65 66 61 75 6c built-in defaul
13d18 74 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 t page cache is
13d19 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 used instead of
13d1a 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
13d1b 64 65 66 69 6e 65 64 0a 20 20 20 20 2a 2a 20 70 defined. ** p
13d1c 61 67 65 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 age cache. */.
13d1d 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 sqlite3PCacheS
13d1e 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d etDefault();. }
13d1f 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
13d20 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
13d21 61 63 68 65 32 2e 78 49 6e 69 74 28 73 71 6c 69 ache2.xInit(sqli
13d22 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
13d23 70 63 61 63 68 65 32 2e 70 41 72 67 29 3b 0a 7d pcache2.pArg);.}
13d24 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13d25 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
13d26 68 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 heShutdown(void)
13d27 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 {. if( sqlite3G
13d28 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
13d29 68 65 32 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b he2.xShutdown ){
13d2a 0a 20 20 20 20 2f 2a 20 49 4d 50 4c 45 4d 45 4e . /* IMPLEMEN
13d2b 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d 32 36 30 TATION-OF: R-260
13d2c 30 30 2d 35 36 35 38 39 20 54 68 65 20 78 53 68 00-56589 The xSh
13d2d 75 74 64 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 utdown() method
13d2e 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a may be NULL. */.
13d2f 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
13d30 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e lConfig.pcache2.
13d31 78 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 xShutdown(sqlite
13d32 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
13d33 61 63 68 65 32 2e 70 41 72 67 29 3b 0a 20 20 7d ache2.pArg);. }
13d34 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
13d35 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 the size in byt
13d36 65 73 20 6f 66 20 61 20 50 43 61 63 68 65 20 6f es of a PCache o
13d37 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 bject..*/.SQLITE
13d38 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
13d39 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 ite3PcacheSize(v
13d3a 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 73 69 7a oid){ return siz
13d3b 65 6f 66 28 50 43 61 63 68 65 29 3b 20 7d 0a 0a eof(PCache); }..
13d3c 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e /*.** Create a n
13d3d 65 77 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 ew PCache object
13d3e 2e 20 53 74 6f 72 61 67 65 20 73 70 61 63 65 20 . Storage space
13d3f 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f 62 6a 65 to hold the obje
13d40 63 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 ct.** has alread
13d41 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 y been allocated
13d42 20 61 6e 64 20 69 73 20 70 61 73 73 65 64 20 69 and is passed i
13d43 6e 20 61 73 20 74 68 65 20 70 20 70 6f 69 6e 74 n as the p point
13d44 65 72 2e 20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c er. .** The call
13d45 65 72 20 64 69 73 63 6f 76 65 72 73 20 68 6f 77 er discovers how
13d46 20 6d 75 63 68 20 73 70 61 63 65 20 6e 65 65 64 much space need
13d47 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 s to be allocate
13d48 64 20 62 79 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67 d by .** calling
13d49 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 sqlite3PcacheSi
13d4a 7a 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ze()..*/.SQLITE_
13d4b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
13d4c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 0a ite3PcacheOpen(.
13d4d 20 20 69 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 int szPage,
13d4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
13d4f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20 * Size of every
13d50 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a page */. int sz
13d51 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 20 Extra,
13d52 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 /* Extra
13d53 73 70 61 63 65 20 61 73 73 6f 63 69 61 74 65 64 space associated
13d54 20 77 69 74 68 20 65 61 63 68 20 70 61 67 65 20 with each page
13d55 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61 */. int bPurgea
13d56 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ble,
13d57 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 /* True if pag
13d58 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e es are on backin
13d59 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 g store */. int
13d5a 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64 (*xStress)(void
13d5b 2a 2c 50 67 48 64 72 2a 29 2c 2f 2a 20 43 61 6c *,PgHdr*),/* Cal
13d5c 6c 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 l to try to make
13d5d 20 70 61 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a pages clean */.
13d5e 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 2c void *pStress,
13d5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
13d60 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 * Argument to xS
13d61 74 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 tress */. PCach
13d62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 e *p
13d63 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c /* Preal
13d64 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f located space fo
13d65 72 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a r the PCache */.
13d66 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 ){. memset(p, 0
13d67 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 29 , sizeof(PCache)
13d68 29 3b 0a 20 20 70 2d 3e 73 7a 50 61 67 65 20 3d );. p->szPage =
13d69 20 73 7a 50 61 67 65 3b 0a 20 20 70 2d 3e 73 7a szPage;. p->sz
13d6a 45 78 74 72 61 20 3d 20 73 7a 45 78 74 72 61 3b Extra = szExtra;
13d6b 0a 20 20 70 2d 3e 62 50 75 72 67 65 61 62 6c 65 . p->bPurgeable
13d6c 20 3d 20 62 50 75 72 67 65 61 62 6c 65 3b 0a 20 = bPurgeable;.
13d6d 20 70 2d 3e 78 53 74 72 65 73 73 20 3d 20 78 53 p->xStress = xS
13d6e 74 72 65 73 73 3b 0a 20 20 70 2d 3e 70 53 74 72 tress;. p->pStr
13d6f 65 73 73 20 3d 20 70 53 74 72 65 73 73 3b 0a 20 ess = pStress;.
13d70 20 70 2d 3e 73 7a 43 61 63 68 65 20 3d 20 31 30 p->szCache = 10
13d71 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 0;.}../*.** Chan
13d72 67 65 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 ge the page size
13d73 20 66 6f 72 20 50 43 61 63 68 65 20 6f 62 6a 65 for PCache obje
13d74 63 74 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d ct. The caller m
13d75 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 ust ensure that
13d76 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 there.** are no
13d77 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 outstanding page
13d78 20 72 65 66 65 72 65 6e 63 65 73 20 77 68 65 6e references when
13d79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
13d7a 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c s called..*/.SQL
13d7b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
13d7c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 sqlite3PcacheSe
13d7d 74 50 61 67 65 53 69 7a 65 28 50 43 61 63 68 65 tPageSize(PCache
13d7e 20 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 73 7a *pCache, int sz
13d7f 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 Page){. assert(
13d80 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 3d 3d 30 pCache->nRef==0
13d81 20 26 26 20 70 43 61 63 68 65 2d 3e 70 44 69 72 && pCache->pDir
13d82 74 79 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 ty==0 );. if( p
13d83 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b Cache->pCache ){
13d84 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
13d85 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 alConfig.pcache2
13d86 2e 78 44 65 73 74 72 6f 79 28 70 43 61 63 68 65 .xDestroy(pCache
13d87 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 20 20 70 ->pCache);. p
13d88 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 3d 20 Cache->pCache =
13d89 30 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 0;. pCache->p
13d8a 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 20 Page1 = 0;. }.
13d8b 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 pCache->szPage
13d8c 3d 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a = szPage;.}../*.
13d8d 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6e ** Compute the n
13d8e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f umber of pages o
13d8f 66 20 63 61 63 68 65 20 72 65 71 75 65 73 74 65 f cache requeste
13d90 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
13d91 20 6e 75 6d 62 65 72 4f 66 43 61 63 68 65 50 61 numberOfCachePa
13d92 67 65 73 28 50 43 61 63 68 65 20 2a 70 29 7b 0a ges(PCache *p){.
13d93 20 20 69 66 28 20 70 2d 3e 73 7a 43 61 63 68 65 if( p->szCache
13d94 3e 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 >=0 ){. retur
13d95 6e 20 70 2d 3e 73 7a 43 61 63 68 65 3b 0a 20 20 n p->szCache;.
13d96 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 }else{. retur
13d97 6e 20 28 69 6e 74 29 28 28 2d 31 30 32 34 2a 28 n (int)((-1024*(
13d98 69 36 34 29 70 2d 3e 73 7a 43 61 63 68 65 29 2f i64)p->szCache)/
13d99 28 70 2d 3e 73 7a 50 61 67 65 2b 70 2d 3e 73 7a (p->szPage+p->sz
13d9a 45 78 74 72 61 29 29 3b 0a 20 20 7d 0a 7d 0a 0a Extra));. }.}..
13d9b 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 /*.** Try to obt
13d9c 61 69 6e 20 61 20 70 61 67 65 20 66 72 6f 6d 20 ain a page from
13d9d 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 the cache..*/.SQ
13d9e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
13d9f 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 sqlite3PcacheFe
13da0 74 63 68 28 0a 20 20 50 43 61 63 68 65 20 2a 70 tch(. PCache *p
13da1 43 61 63 68 65 2c 20 20 20 20 20 20 20 2f 2a 20 Cache, /*
13da2 4f 62 74 61 69 6e 20 74 68 65 20 70 61 67 65 20 Obtain the page
13da3 66 72 6f 6d 20 74 68 69 73 20 63 61 63 68 65 20 from this cache
13da4 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 */. Pgno pgno,
13da5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
13da6 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6f 62 74 ge number to obt
13da7 61 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 ain */. int cre
13da8 61 74 65 46 6c 61 67 2c 20 20 20 20 20 20 20 2f ateFlag, /
13da9 2a 20 49 66 20 74 72 75 65 2c 20 63 72 65 61 74 * If true, creat
13daa 65 20 70 61 67 65 20 69 66 20 69 74 20 64 6f 65 e page if it doe
13dab 73 20 6e 6f 74 20 65 78 69 73 74 20 61 6c 72 65 s not exist alre
13dac 61 64 79 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a ady */. PgHdr *
13dad 2a 70 70 50 61 67 65 20 20 20 20 20 20 20 20 2f *ppPage /
13dae 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67 65 * Write the page
13daf 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 here */.){. sq
13db0 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67 lite3_pcache_pag
13db1 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 e *pPage = 0;.
13db2 50 67 48 64 72 20 2a 70 50 67 48 64 72 20 3d 20 PgHdr *pPgHdr =
13db3 30 3b 0a 20 20 69 6e 74 20 65 43 72 65 61 74 65 0;. int eCreate
13db4 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 ;.. assert( pCa
13db5 63 68 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 che!=0 );. asse
13db6 72 74 28 20 63 72 65 61 74 65 46 6c 61 67 3d 3d rt( createFlag==
13db7 31 20 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 3d 1 || createFlag=
13db8 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
13db9 70 67 6e 6f 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 pgno>0 );.. /*
13dba 49 66 20 74 68 65 20 70 6c 75 67 67 61 62 6c 65 If the pluggable
13dbb 20 63 61 63 68 65 20 28 73 71 6c 69 74 65 33 5f cache (sqlite3_
13dbc 70 63 61 63 68 65 2a 29 20 68 61 73 20 6e 6f 74 pcache*) has not
13dbd 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c been allocated,
13dbe 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 69 . ** allocate i
13dbf 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 t now.. */. if
13dc0 28 20 21 70 43 61 63 68 65 2d 3e 70 43 61 63 68 ( !pCache->pCach
13dc1 65 20 26 26 20 63 72 65 61 74 65 46 6c 61 67 20 e && createFlag
13dc2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 70 ){. sqlite3_p
13dc3 63 61 63 68 65 20 2a 70 3b 0a 20 20 20 20 70 20 cache *p;. p
13dc4 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
13dc5 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 43 onfig.pcache2.xC
13dc6 72 65 61 74 65 28 0a 20 20 20 20 20 20 20 20 70 reate(. p
13dc7 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 2c 20 70 Cache->szPage, p
13dc8 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 2b Cache->szExtra +
13dc9 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 2c 20 sizeof(PgHdr),
13dca 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
13dcb 6c 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 le. );. if
13dcc 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20 72 65 ( !p ){. re
13dcd 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
13dce 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c M;. }. sql
13dcf 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
13dd0 2e 70 63 61 63 68 65 32 2e 78 43 61 63 68 65 73 .pcache2.xCaches
13dd1 69 7a 65 28 70 2c 20 6e 75 6d 62 65 72 4f 66 43 ize(p, numberOfC
13dd2 61 63 68 65 50 61 67 65 73 28 70 43 61 63 68 65 achePages(pCache
13dd3 29 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e ));. pCache->
13dd4 70 43 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a pCache = p;. }.
13dd5 0a 20 20 65 43 72 65 61 74 65 20 3d 20 63 72 65 . eCreate = cre
13dd6 61 74 65 46 6c 61 67 20 2a 20 28 31 20 2b 20 28 ateFlag * (1 + (
13dd7 21 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 !pCache->bPurgea
13dd8 62 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e ble || !pCache->
13dd9 70 44 69 72 74 79 29 29 3b 0a 20 20 69 66 28 20 pDirty));. if(
13dda 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 pCache->pCache )
13ddb 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 {. pPage = sq
13ddc 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
13ddd 67 2e 70 63 61 63 68 65 32 2e 78 46 65 74 63 68 g.pcache2.xFetch
13dde 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c (pCache->pCache,
13ddf 20 70 67 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b pgno, eCreate);
13de0 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 . }.. if( !pPa
13de1 67 65 20 26 26 20 65 43 72 65 61 74 65 3d 3d 31 ge && eCreate==1
13de2 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 ){. PgHdr *p
13de3 50 67 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 Pg;.. /* Find
13de4 20 61 20 64 69 72 74 79 20 70 61 67 65 20 74 6f a dirty page to
13de5 20 77 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 72 write-out and r
13de6 65 63 79 63 6c 65 2e 20 46 69 72 73 74 20 74 72 ecycle. First tr
13de7 79 20 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 20 y to find a .
13de8 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f ** page that do
13de9 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61 es not require a
13dea 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f journal-sync (o
13deb 6e 65 20 77 69 74 68 20 50 47 48 44 52 5f 4e 45 ne with PGHDR_NE
13dec 45 44 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63 ED_SYNC. ** c
13ded 6c 65 61 72 65 64 29 2c 20 62 75 74 20 69 66 20 leared), but if
13dee 74 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 that is not poss
13def 69 62 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 20 ible settle for
13df0 61 6e 79 20 6f 74 68 65 72 20 0a 20 20 20 20 2a any other . *
13df1 2a 20 75 6e 72 65 66 65 72 65 6e 63 65 64 20 64 * unreferenced d
13df2 69 72 74 79 20 70 61 67 65 2e 0a 20 20 20 20 2a irty page.. *
13df3 2f 0a 20 20 20 20 65 78 70 65 6e 73 69 76 65 5f /. expensive_
13df4 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 68 assert( pcacheCh
13df5 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68 65 eckSynced(pCache
13df6 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 ) );. for(pPg
13df7 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 =pCache->pSynced
13df8 3b 20 0a 20 20 20 20 20 20 20 20 70 50 67 20 26 ; . pPg &
13df9 26 20 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 & (pPg->nRef ||
13dfa 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 (pPg->flags&PGHD
13dfb 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a R_NEED_SYNC)); .
13dfc 20 20 20 20 20 20 20 20 70 50 67 3d 70 50 67 2d pPg=pPg-
13dfd 3e 70 44 69 72 74 79 50 72 65 76 0a 20 20 20 20 >pDirtyPrev.
13dfe 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 );. pCache->p
13dff 53 79 6e 63 65 64 20 3d 20 70 50 67 3b 0a 20 20 Synced = pPg;.
13e00 20 20 69 66 28 20 21 70 50 67 20 29 7b 0a 20 20 if( !pPg ){.
13e01 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43 61 63 for(pPg=pCac
13e02 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 he->pDirtyTail;
13e03 70 50 67 20 26 26 20 70 50 67 2d 3e 6e 52 65 66 pPg && pPg->nRef
13e04 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44 69 72 74 ; pPg=pPg->pDirt
13e05 79 50 72 65 76 29 3b 0a 20 20 20 20 7d 0a 20 20 yPrev);. }.
13e06 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 if( pPg ){.
13e07 20 20 20 69 6e 74 20 72 63 3b 0a 23 69 66 64 65 int rc;.#ifde
13e08 66 20 53 51 4c 49 54 45 5f 4c 4f 47 5f 43 41 43 f SQLITE_LOG_CAC
13e09 48 45 5f 53 50 49 4c 4c 0a 20 20 20 20 20 20 73 HE_SPILL. s
13e0a 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54 qlite3_log(SQLIT
13e0b 45 5f 46 55 4c 4c 2c 20 0a 20 20 20 20 20 20 20 E_FULL, .
13e0c 20 20 20 20 20 20 20 20 20 20 20 22 73 70 69 6c "spil
13e0d 6c 20 70 61 67 65 20 25 64 20 6d 61 6b 69 6e 67 l page %d making
13e0e 20 72 6f 6f 6d 20 66 6f 72 20 25 64 20 2d 20 63 room for %d - c
13e0f 61 63 68 65 20 75 73 65 64 3a 20 25 64 2f 25 64 ache used: %d/%d
13e10 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
13e11 20 20 20 20 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 pPg->pgno,
13e12 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 pgno,.
13e13 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 sqlite3G
13e14 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
13e15 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 28 70 43 he.xPagecount(pC
13e16 61 63 68 65 2d 3e 70 43 61 63 68 65 29 2c 0a 20 ache->pCache),.
13e17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13e18 20 6e 75 6d 62 65 72 4f 66 43 61 63 68 65 50 61 numberOfCachePa
13e19 67 65 73 28 70 43 61 63 68 65 29 29 3b 0a 23 65 ges(pCache));.#e
13e1a 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20 ndif. rc =
13e1b 70 43 61 63 68 65 2d 3e 78 53 74 72 65 73 73 28 pCache->xStress(
13e1c 70 43 61 63 68 65 2d 3e 70 53 74 72 65 73 73 2c pCache->pStress,
13e1d 20 70 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 pPg);. if(
13e1e 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc!=SQLITE_OK &
13e1f 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 & rc!=SQLITE_BUS
13e20 59 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 Y ){. ret
13e21 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
13e22 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 }.. pPage
13e23 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
13e24 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 Config.pcache2.x
13e25 46 65 74 63 68 28 70 43 61 63 68 65 2d 3e 70 43 Fetch(pCache->pC
13e26 61 63 68 65 2c 20 70 67 6e 6f 2c 20 32 29 3b 0a ache, pgno, 2);.
13e27 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 }.. if( pPage
13e28 20 29 7b 0a 20 20 20 20 70 50 67 48 64 72 20 3d ){. pPgHdr =
13e29 20 28 50 67 48 64 72 20 2a 29 70 50 61 67 65 2d (PgHdr *)pPage-
13e2a 3e 70 45 78 74 72 61 3b 0a 0a 20 20 20 20 69 66 >pExtra;.. if
13e2b 28 20 21 70 50 67 48 64 72 2d 3e 70 50 61 67 65 ( !pPgHdr->pPage
13e2c 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 ){. memset
13e2d 28 70 50 67 48 64 72 2c 20 30 2c 20 73 69 7a 65 (pPgHdr, 0, size
13e2e 6f 66 28 50 67 48 64 72 29 29 3b 0a 20 20 20 20 of(PgHdr));.
13e2f 20 20 70 50 67 48 64 72 2d 3e 70 50 61 67 65 20 pPgHdr->pPage
13e30 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 70 = pPage;. p
13e31 50 67 48 64 72 2d 3e 70 44 61 74 61 20 3d 20 70 PgHdr->pData = p
13e32 50 61 67 65 2d 3e 70 42 75 66 3b 0a 20 20 20 20 Page->pBuf;.
13e33 20 20 70 50 67 48 64 72 2d 3e 70 45 78 74 72 61 pPgHdr->pExtra
13e34 20 3d 20 28 76 6f 69 64 20 2a 29 26 70 50 67 48 = (void *)&pPgH
13e35 64 72 5b 31 5d 3b 0a 20 20 20 20 20 20 6d 65 6d dr[1];. mem
13e36 73 65 74 28 70 50 67 48 64 72 2d 3e 70 45 78 74 set(pPgHdr->pExt
13e37 72 61 2c 20 30 2c 20 70 43 61 63 68 65 2d 3e 73 ra, 0, pCache->s
13e38 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 20 20 70 zExtra);. p
13e39 50 67 48 64 72 2d 3e 70 43 61 63 68 65 20 3d 20 PgHdr->pCache =
13e3a 70 43 61 63 68 65 3b 0a 20 20 20 20 20 20 70 50 pCache;. pP
13e3b 67 48 64 72 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e gHdr->pgno = pgn
13e3c 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 o;. }. ass
13e3d 65 72 74 28 20 70 50 67 48 64 72 2d 3e 70 43 61 ert( pPgHdr->pCa
13e3e 63 68 65 3d 3d 70 43 61 63 68 65 20 29 3b 0a 20 che==pCache );.
13e3f 20 20 20 61 73 73 65 72 74 28 20 70 50 67 48 64 assert( pPgHd
13e40 72 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 20 29 3b r->pgno==pgno );
13e41 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 . assert( pPg
13e42 48 64 72 2d 3e 70 44 61 74 61 3d 3d 70 50 61 67 Hdr->pData==pPag
13e43 65 2d 3e 70 42 75 66 20 29 3b 0a 20 20 20 20 61 e->pBuf );. a
13e44 73 73 65 72 74 28 20 70 50 67 48 64 72 2d 3e 70 ssert( pPgHdr->p
13e45 45 78 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26 Extra==(void *)&
13e46 70 50 67 48 64 72 5b 31 5d 20 29 3b 0a 0a 20 20 pPgHdr[1] );..
13e47 20 20 69 66 28 20 30 3d 3d 70 50 67 48 64 72 2d if( 0==pPgHdr-
13e48 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20 70 >nRef ){. p
13e49 43 61 63 68 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 Cache->nRef++;.
13e4a 20 20 20 7d 0a 20 20 20 20 70 50 67 48 64 72 2d }. pPgHdr-
13e4b 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 69 66 28 >nRef++;. if(
13e4c 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 pgno==1 ){.
13e4d 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 pCache->pPage1
13e4e 20 3d 20 70 50 67 48 64 72 3b 0a 20 20 20 20 7d = pPgHdr;. }
13e4f 0a 20 20 7d 0a 20 20 2a 70 70 50 61 67 65 20 3d . }. *ppPage =
13e50 20 70 50 67 48 64 72 3b 0a 20 20 72 65 74 75 72 pPgHdr;. retur
13e51 6e 20 28 70 50 67 48 64 72 3d 3d 30 20 26 26 20 n (pPgHdr==0 &&
13e52 65 43 72 65 61 74 65 29 20 3f 20 53 51 4c 49 54 eCreate) ? SQLIT
13e53 45 5f 4e 4f 4d 45 4d 20 3a 20 53 51 4c 49 54 45 E_NOMEM : SQLITE
13e54 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 _OK;.}../*.** De
13e55 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 crement the refe
13e56 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e 20 61 rence count on a
13e57 20 70 61 67 65 2e 20 49 66 20 74 68 65 20 70 61 page. If the pa
13e58 67 65 20 69 73 20 63 6c 65 61 6e 20 61 6e 64 20 ge is clean and
13e59 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 the.** reference
13e5a 20 63 6f 75 6e 74 20 64 72 6f 70 73 20 74 6f 20 count drops to
13e5b 30 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6d 61 0, then it is ma
13e5c 64 65 20 65 6c 69 62 6c 65 20 66 6f 72 20 72 65 de elible for re
13e5d 63 79 63 6c 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 cycling..*/.SQLI
13e5e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
13e5f 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c sqlite3PcacheRel
13e60 65 61 73 65 28 50 67 48 64 72 20 2a 70 29 7b 0a ease(PgHdr *p){.
13e61 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
13e62 66 3e 30 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 f>0 );. p->nRef
13e63 2d 2d 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 --;. if( p->nRe
13e64 66 3d 3d 30 20 29 7b 0a 20 20 20 20 50 43 61 63 f==0 ){. PCac
13e65 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e he *pCache = p->
13e66 70 43 61 63 68 65 3b 0a 20 20 20 20 70 43 61 63 pCache;. pCac
13e67 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 he->nRef--;.
13e68 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 if( (p->flags&PG
13e69 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20 29 7b HDR_DIRTY)==0 ){
13e6a 0a 20 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 . pcacheUnp
13e6b 69 6e 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 in(p);. }else
13e6c 7b 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 {. /* Move
13e6d 74 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 the page to the
13e6e 68 65 61 64 20 6f 66 20 74 68 65 20 64 69 72 74 head of the dirt
13e6f 79 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 y list. */.
13e70 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f pcacheRemoveFro
13e71 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 mDirtyList(p);.
13e72 20 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f pcacheAddTo
13e73 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 DirtyList(p);.
13e74 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
13e75 20 49 6e 63 72 65 61 73 65 20 74 68 65 20 72 65 Increase the re
13e76 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 ference count of
13e77 20 61 20 73 75 70 70 6c 69 65 64 20 70 61 67 65 a supplied page
13e78 20 62 79 20 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 by 1..*/.SQLITE
13e79 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
13e7a 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 28 50 lite3PcacheRef(P
13e7b 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 gHdr *p){. asse
13e7c 72 74 28 70 2d 3e 6e 52 65 66 3e 30 29 3b 0a 20 rt(p->nRef>0);.
13e7d 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f p->nRef++;.}../
13e7e 2a 0a 2a 2a 20 44 72 6f 70 20 61 20 70 61 67 65 *.** Drop a page
13e7f 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 2e from the cache.
13e80 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 65 There must be e
13e81 78 61 63 74 6c 79 20 6f 6e 65 20 72 65 66 65 72 xactly one refer
13e82 65 6e 63 65 20 74 6f 20 74 68 65 0a 2a 2a 20 70 ence to the.** p
13e83 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 age. This functi
13e84 6f 6e 20 64 65 6c 65 74 65 73 20 74 68 61 74 20 on deletes that
13e85 72 65 66 65 72 65 6e 63 65 2c 20 73 6f 20 61 66 reference, so af
13e86 74 65 72 20 69 74 20 72 65 74 75 72 6e 73 20 74 ter it returns t
13e87 68 65 0a 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 he.** page point
13e88 65 64 20 74 6f 20 62 79 20 70 20 69 73 20 69 6e ed to by p is in
13e89 76 61 6c 69 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 valid..*/.SQLITE
13e8a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
13e8b 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 lite3PcacheDrop(
13e8c 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 43 61 PgHdr *p){. PCa
13e8d 63 68 65 20 2a 70 43 61 63 68 65 3b 0a 20 20 61 che *pCache;. a
13e8e 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d ssert( p->nRef==
13e8f 31 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 1 );. if( p->fl
13e90 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 ags&PGHDR_DIRTY
13e91 29 7b 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d ){. pcacheRem
13e92 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 oveFromDirtyList
13e93 28 70 29 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 (p);. }. pCach
13e94 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 e = p->pCache;.
13e95 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2d 2d 3b pCache->nRef--;
13e96 0a 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d . if( p->pgno==
13e97 31 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 1 ){. pCache-
13e98 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 7d >pPage1 = 0;. }
13e99 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c . sqlite3Global
13e9a 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 Config.pcache2.x
13e9b 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e 70 43 Unpin(pCache->pC
13e9c 61 63 68 65 2c 20 70 2d 3e 70 50 61 67 65 2c 20 ache, p->pPage,
13e9d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 1);.}../*.** Mak
13e9e 65 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 e sure the page
13e9f 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 is marked as dir
13ea0 74 79 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 ty. If it isn't
13ea1 64 69 72 74 79 20 61 6c 72 65 61 64 79 2c 0a 2a dirty already,.*
13ea2 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f * make it so..*/
13ea3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13ea4 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
13ea5 68 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 heMakeDirty(PgHd
13ea6 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 67 r *p){. p->flag
13ea7 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54 s &= ~PGHDR_DONT
13ea8 5f 57 52 49 54 45 3b 0a 20 20 61 73 73 65 72 74 _WRITE;. assert
13ea9 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ( p->nRef>0 );.
13eaa 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 67 if( 0==(p->flag
13eab 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 s & PGHDR_DIRTY)
13eac 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 ){. p->flags
13ead 20 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b |= PGHDR_DIRTY;
13eae 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f . pcacheAddTo
13eaf 44 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 DirtyList( p);.
13eb0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 }.}../*.** Make
13eb1 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 sure the page i
13eb2 73 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61 s marked as clea
13eb3 6e 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 63 n. If it isn't c
13eb4 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a lean already,.**
13eb5 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a make it so..*/.
13eb6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
13eb7 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
13eb8 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 eMakeClean(PgHdr
13eb9 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e *p){. if( (p->
13eba 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 flags & PGHDR_DI
13ebb 52 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63 RTY) ){. pcac
13ebc 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 heRemoveFromDirt
13ebd 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d yList(p);. p-
13ebe 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44 >flags &= ~(PGHD
13ebf 52 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 R_DIRTY|PGHDR_NE
13ec0 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 ED_SYNC);. if
13ec1 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a ( p->nRef==0 ){.
13ec2 20 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 pcacheUnpi
13ec3 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a n(p);. }. }.
13ec4 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 }../*.** Make ev
13ec5 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 ery page in the
13ec6 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a cache clean..*/.
13ec7 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
13ec8 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
13ec9 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 eCleanAll(PCache
13eca 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 *pCache){. PgH
13ecb 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 dr *p;. while(
13ecc 28 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 (p = pCache->pDi
13ecd 72 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 rty)!=0 ){. s
13ece 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
13ecf 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a Clean(p);. }.}.
13ed0 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 ./*.** Clear the
13ed1 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
13ed2 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 flag from all d
13ed3 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 irty pages..*/.S
13ed4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
13ed5 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
13ed6 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50 ClearSyncFlags(P
13ed7 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a Cache *pCache){.
13ed8 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f PgHdr *p;. fo
13ed9 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 r(p=pCache->pDir
13eda 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 ty; p; p=p->pDir
13edb 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e tyNext){. p->
13edc 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f flags &= ~PGHDR_
13edd 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 NEED_SYNC;. }.
13ede 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 pCache->pSynced
13edf 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 = pCache->pDirt
13ee0 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 yTail;.}../*.**
13ee1 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 Change the page
13ee2 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70 number of page p
13ee3 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f to newPgno. .*/
13ee4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13ee5 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
13ee6 68 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c heMove(PgHdr *p,
13ee7 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a Pgno newPgno){.
13ee8 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 PCache *pCache
13ee9 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 = p->pCache;.
13eea 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e assert( p->nRef>
13eeb 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 0 );. assert( n
13eec 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 ewPgno>0 );. sq
13eed 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
13eee 67 2e 70 63 61 63 68 65 32 2e 78 52 65 6b 65 79 g.pcache2.xRekey
13eef 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c (pCache->pCache,
13ef0 20 70 2d 3e 70 50 61 67 65 2c 20 70 2d 3e 70 67 p->pPage, p->pg
13ef1 6e 6f 2c 6e 65 77 50 67 6e 6f 29 3b 0a 20 20 70 no,newPgno);. p
13ef2 2d 3e 70 67 6e 6f 20 3d 20 6e 65 77 50 67 6e 6f ->pgno = newPgno
13ef3 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 ;. if( (p->flag
13ef4 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 26 s&PGHDR_DIRTY) &
13ef5 26 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 & (p->flags&PGHD
13ef6 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a R_NEED_SYNC) ){.
13ef7 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 pcacheRemove
13ef8 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 FromDirtyList(p)
13ef9 3b 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54 ;. pcacheAddT
13efa 6f 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 oDirtyList(p);.
13efb 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 }.}../*.** Drop
13efc 20 65 76 65 72 79 20 63 61 63 68 65 20 65 6e 74 every cache ent
13efd 72 79 20 77 68 6f 73 65 20 70 61 67 65 20 6e 75 ry whose page nu
13efe 6d 62 65 72 20 69 73 20 67 72 65 61 74 65 72 20 mber is greater
13eff 74 68 61 6e 20 22 70 67 6e 6f 22 2e 20 54 68 65 than "pgno". The
13f00 0a 2a 2a 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 .** caller must
13f01 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 72 ensure that ther
13f02 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e e are no outstan
13f03 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20 ding references
13f04 74 6f 20 61 6e 79 20 70 61 67 65 73 0a 2a 2a 20 to any pages.**
13f05 6f 74 68 65 72 20 74 68 61 6e 20 70 61 67 65 20 other than page
13f06 31 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 1 with a page nu
13f07 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 68 61 mber greater tha
13f08 6e 20 70 67 6e 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 n pgno..**.** If
13f09 20 74 68 65 72 65 20 69 73 20 61 20 72 65 66 65 there is a refe
13f0a 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 rence to page 1
13f0b 61 6e 64 20 74 68 65 20 70 67 6e 6f 20 70 61 72 and the pgno par
13f0c 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 6f ameter passed to
13f0d 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f this.** functio
13f0e 6e 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65 n is 0, then the
13f0f 20 64 61 74 61 20 61 72 65 61 20 61 73 73 6f 63 data area assoc
13f10 69 61 74 65 64 20 77 69 74 68 20 70 61 67 65 20 iated with page
13f11 31 20 69 73 20 7a 65 72 6f 65 64 2c 20 62 75 74 1 is zeroed, but
13f12 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6f 62 6a .** the page obj
13f13 65 63 74 20 69 73 20 6e 6f 74 20 64 72 6f 70 70 ect is not dropp
13f14 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
13f15 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
13f16 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 e3PcacheTruncate
13f17 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c (PCache *pCache,
13f18 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 Pgno pgno){. i
13f19 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 f( pCache->pCach
13f1a 65 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a e ){. PgHdr *
13f1b 70 3b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 4e p;. PgHdr *pN
13f1c 65 78 74 3b 0a 20 20 20 20 66 6f 72 28 70 3d 70 ext;. for(p=p
13f1d 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70 Cache->pDirty; p
13f1e 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 ; p=pNext){.
13f1f 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 44 69 pNext = p->pDi
13f20 72 74 79 4e 65 78 74 3b 0a 20 20 20 20 20 20 2f rtyNext;. /
13f21 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e * This routine n
13f22 65 76 65 72 20 67 65 74 73 20 63 61 6c 6c 20 77 ever gets call w
13f23 69 74 68 20 61 20 70 6f 73 69 74 69 76 65 20 70 ith a positive p
13f24 67 6e 6f 20 65 78 63 65 70 74 20 72 69 67 68 74 gno except right
13f25 0a 20 20 20 20 20 20 2a 2a 20 61 66 74 65 72 20 . ** after
13f26 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 sqlite3PcacheCle
13f27 61 6e 41 6c 6c 28 29 2e 20 20 53 6f 20 69 66 20 anAll(). So if
13f28 74 68 65 72 65 20 61 72 65 20 64 69 72 74 79 20 there are dirty
13f29 70 61 67 65 73 2c 0a 20 20 20 20 20 20 2a 2a 20 pages,. **
13f2a 69 74 20 6d 75 73 74 20 62 65 20 74 68 61 74 20 it must be that
13f2b 70 67 6e 6f 3d 3d 30 2e 0a 20 20 20 20 20 20 2a pgno==0.. *
13f2c 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 /. assert(
13f2d 70 2d 3e 70 67 6e 6f 3e 30 20 29 3b 0a 20 20 20 p->pgno>0 );.
13f2e 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d if( ALWAYS(p-
13f2f 3e 70 67 6e 6f 3e 70 67 6e 6f 29 20 29 7b 0a 20 >pgno>pgno) ){.
13f30 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
13f31 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
13f32 52 54 59 20 29 3b 0a 20 20 20 20 20 20 20 20 73 RTY );. s
13f33 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
13f34 43 6c 65 61 6e 28 70 29 3b 0a 20 20 20 20 20 20 Clean(p);.
13f35 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
13f36 70 67 6e 6f 3d 3d 30 20 26 26 20 70 43 61 63 68 pgno==0 && pCach
13f37 65 2d 3e 70 50 61 67 65 31 20 29 7b 0a 20 20 20 e->pPage1 ){.
13f38 20 20 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 memset(pCache
13f39 2d 3e 70 50 61 67 65 31 2d 3e 70 44 61 74 61 2c ->pPage1->pData,
13f3a 20 30 2c 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 0, pCache->szPa
13f3b 67 65 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 ge);. pgno
13f3c 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 = 1;. }. s
13f3d 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
13f3e 69 67 2e 70 63 61 63 68 65 32 2e 78 54 72 75 6e ig.pcache2.xTrun
13f3f 63 61 74 65 28 70 43 61 63 68 65 2d 3e 70 43 61 cate(pCache->pCa
13f40 63 68 65 2c 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 che, pgno+1);.
13f41 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 }.}../*.** Close
13f42 20 61 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c a cache..*/.SQL
13f43 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
13f44 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c sqlite3PcacheCl
13f45 6f 73 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 ose(PCache *pCac
13f46 68 65 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 he){. if( pCach
13f47 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 e->pCache ){.
13f48 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
13f49 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 44 65 nfig.pcache2.xDe
13f4a 73 74 72 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 stroy(pCache->pC
13f4b 61 63 68 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ache);. }.}../*
13f4c 20 0a 2a 2a 20 44 69 73 63 61 72 64 20 74 68 65 .** Discard the
13f4d 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
13f4e 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 cache..*/.SQLIT
13f4f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
13f50 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 qlite3PcacheClea
13f51 72 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 r(PCache *pCache
13f52 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 ){. sqlite3Pcac
13f53 68 65 54 72 75 6e 63 61 74 65 28 70 43 61 63 68 heTruncate(pCach
13f54 65 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 e, 0);.}../*.**
13f55 4d 65 72 67 65 20 74 77 6f 20 6c 69 73 74 73 20 Merge two lists
13f56 6f 66 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 of pages connect
13f57 65 64 20 62 79 20 70 44 69 72 74 79 20 61 6e 64 ed by pDirty and
13f58 20 69 6e 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a in pgno order..
13f59 2a 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 20 66 ** Do not both f
13f5a 69 78 69 6e 67 20 74 68 65 20 70 44 69 72 74 79 ixing the pDirty
13f5b 50 72 65 76 20 70 6f 69 6e 74 65 72 73 2e 0a 2a Prev pointers..*
13f5c 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a /.static PgHdr *
13f5d 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 pcacheMergeDirty
13f5e 4c 69 73 74 28 50 67 48 64 72 20 2a 70 41 2c 20 List(PgHdr *pA,
13f5f 50 67 48 64 72 20 2a 70 42 29 7b 0a 20 20 50 67 PgHdr *pB){. Pg
13f60 48 64 72 20 72 65 73 75 6c 74 2c 20 2a 70 54 61 Hdr result, *pTa
13f61 69 6c 3b 0a 20 20 70 54 61 69 6c 20 3d 20 26 72 il;. pTail = &r
13f62 65 73 75 6c 74 3b 0a 20 20 77 68 69 6c 65 28 20 esult;. while(
13f63 70 41 20 26 26 20 70 42 20 29 7b 0a 20 20 20 20 pA && pB ){.
13f64 69 66 28 20 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d if( pA->pgno<pB-
13f65 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 70 >pgno ){. p
13f66 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 Tail->pDirty = p
13f67 41 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d A;. pTail =
13f68 20 70 41 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 pA;. pA =
13f69 70 41 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 pA->pDirty;.
13f6a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61 }else{. pTa
13f6b 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b il->pDirty = pB;
13f6c 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 . pTail = p
13f6d 42 3b 0a 20 20 20 20 20 20 70 42 20 3d 20 70 42 B;. pB = pB
13f6e 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 0a ->pDirty;. }.
13f6f 20 20 7d 0a 20 20 69 66 28 20 70 41 20 29 7b 0a }. if( pA ){.
13f70 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 pTail->pDirt
13f71 79 20 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 65 20 y = pA;. }else
13f72 69 66 28 20 70 42 20 29 7b 0a 20 20 20 20 70 54 if( pB ){. pT
13f73 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 ail->pDirty = pB
13f74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
13f75 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 30 Tail->pDirty = 0
13f76 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
13f77 65 73 75 6c 74 2e 70 44 69 72 74 79 3b 0a 7d 0a esult.pDirty;.}.
13f78 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 20 74 68 65 20 ./*.** Sort the
13f79 6c 69 73 74 20 6f 66 20 70 61 67 65 73 20 69 6e list of pages in
13f7a 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 accending order
13f7b 20 62 79 20 70 67 6e 6f 2e 20 20 50 61 67 65 73 by pgno. Pages
13f7c 20 61 72 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 are.** connecte
13f7d 64 20 62 79 20 70 44 69 72 74 79 20 70 6f 69 6e d by pDirty poin
13f7e 74 65 72 73 2e 20 20 54 68 65 20 70 44 69 72 74 ters. The pDirt
13f7f 79 50 72 65 76 20 70 6f 69 6e 74 65 72 73 20 61 yPrev pointers a
13f80 72 65 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 20 re.** corrupted
13f81 62 79 20 74 68 69 73 20 73 6f 72 74 2e 0a 2a 2a by this sort..**
13f82 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 72 65 20 .** Since there
13f83 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 cannot be more t
13f84 68 61 6e 20 32 5e 33 31 20 64 69 73 74 69 6e 63 han 2^31 distinc
13f85 74 20 70 61 67 65 73 20 69 6e 20 61 20 64 61 74 t pages in a dat
13f86 61 62 61 73 65 2c 0a 2a 2a 20 74 68 65 72 65 20 abase,.** there
13f87 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 cannot be more t
13f88 68 61 6e 20 33 31 20 62 75 63 6b 65 74 73 20 72 han 31 buckets r
13f89 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 6d equired by the m
13f8a 65 72 67 65 20 73 6f 72 74 65 72 2e 0a 2a 2a 20 erge sorter..**
13f8b 4f 6e 65 20 65 78 74 72 61 20 62 75 63 6b 65 74 One extra bucket
13f8c 20 69 73 20 61 64 64 65 64 20 74 6f 20 63 61 74 is added to cat
13f8d 63 68 20 6f 76 65 72 66 6c 6f 77 20 69 6e 20 63 ch overflow in c
13f8e 61 73 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a ase something.**
13f8f 20 65 76 65 72 20 63 68 61 6e 67 65 73 20 74 6f ever changes to
13f90 20 6d 61 6b 65 20 74 68 65 20 70 72 65 76 69 6f make the previo
13f91 75 73 20 73 65 6e 74 65 6e 63 65 20 69 6e 63 6f us sentence inco
13f92 72 72 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e rrect..*/.#defin
13f93 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 20 e N_SORT_BUCKET
13f94 20 33 32 0a 73 74 61 74 69 63 20 50 67 48 64 72 32.static PgHdr
13f95 20 2a 70 63 61 63 68 65 53 6f 72 74 44 69 72 74 *pcacheSortDirt
13f96 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 49 6e yList(PgHdr *pIn
13f97 29 7b 0a 20 20 50 67 48 64 72 20 2a 61 5b 4e 5f ){. PgHdr *a[N_
13f98 53 4f 52 54 5f 42 55 43 4b 45 54 5d 2c 20 2a 70 SORT_BUCKET], *p
13f99 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d 65 6d ;. int i;. mem
13f9a 73 65 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f 66 set(a, 0, sizeof
13f9b 28 61 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 (a));. while( p
13f9c 49 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20 70 49 In ){. p = pI
13f9d 6e 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 2d 3e n;. pIn = p->
13f9e 70 44 69 72 74 79 3b 0a 20 20 20 20 70 2d 3e 70 pDirty;. p->p
13f9f 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 66 Dirty = 0;. f
13fa0 6f 72 28 69 3d 30 3b 20 41 4c 57 41 59 53 28 69 or(i=0; ALWAYS(i
13fa1 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 2d 31 <N_SORT_BUCKET-1
13fa2 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 ); i++){. i
13fa3 66 28 20 61 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 f( a[i]==0 ){.
13fa4 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70 3b 0a a[i] = p;.
13fa5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
13fa6 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
13fa7 20 20 20 20 70 20 3d 20 70 63 61 63 68 65 4d 65 p = pcacheMe
13fa8 72 67 65 44 69 72 74 79 4c 69 73 74 28 61 5b 69 rgeDirtyList(a[i
13fa9 5d 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 61 ], p);. a
13faa 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d [i] = 0;. }
13fab 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 4e . }. if( N
13fac 45 56 45 52 28 69 3d 3d 4e 5f 53 4f 52 54 5f 42 EVER(i==N_SORT_B
13fad 55 43 4b 45 54 2d 31 29 20 29 7b 0a 20 20 20 20 UCKET-1) ){.
13fae 20 20 2f 2a 20 54 6f 20 67 65 74 20 68 65 72 65 /* To get here
13faf 2c 20 74 68 65 72 65 20 6e 65 65 64 20 74 6f 20 , there need to
13fb0 62 65 20 32 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 be 2^(N_SORT_BUC
13fb1 4b 45 54 29 20 65 6c 65 6d 65 6e 74 73 20 69 6e KET) elements in
13fb2 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 69 6e . ** the in
13fb3 70 75 74 20 6c 69 73 74 2e 20 20 42 75 74 20 74 put list. But t
13fb4 68 61 74 20 69 73 20 69 6d 70 6f 73 73 69 62 6c hat is impossibl
13fb5 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
13fb6 20 20 61 5b 69 5d 20 3d 20 70 63 61 63 68 65 4d a[i] = pcacheM
13fb7 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 61 5b ergeDirtyList(a[
13fb8 69 5d 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 i], p);. }.
13fb9 7d 0a 20 20 70 20 3d 20 61 5b 30 5d 3b 0a 20 20 }. p = a[0];.
13fba 66 6f 72 28 69 3d 31 3b 20 69 3c 4e 5f 53 4f 52 for(i=1; i<N_SOR
13fbb 54 5f 42 55 43 4b 45 54 3b 20 69 2b 2b 29 7b 0a T_BUCKET; i++){.
13fbc 20 20 20 20 70 20 3d 20 70 63 61 63 68 65 4d 65 p = pcacheMe
13fbd 72 67 65 44 69 72 74 79 4c 69 73 74 28 70 2c 20 rgeDirtyList(p,
13fbe 61 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 a[i]);. }. ret
13fbf 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
13fc0 52 65 74 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 Return a list of
13fc1 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 all dirty pages
13fc2 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 73 in the cache, s
13fc3 6f 72 74 65 64 20 62 79 20 70 61 67 65 20 6e 75 orted by page nu
13fc4 6d 62 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f mber..*/.SQLITE_
13fc5 50 52 49 56 41 54 45 20 50 67 48 64 72 20 2a 73 PRIVATE PgHdr *s
13fc6 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 qlite3PcacheDirt
13fc7 79 4c 69 73 74 28 50 43 61 63 68 65 20 2a 70 43 yList(PCache *pC
13fc8 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 20 2a ache){. PgHdr *
13fc9 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68 p;. for(p=pCach
13fca 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d e->pDirty; p; p=
13fcb 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 29 7b 0a p->pDirtyNext){.
13fcc 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 p->pDirty =
13fcd 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 p->pDirtyNext;.
13fce 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 63 61 63 }. return pcac
13fcf 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 28 heSortDirtyList(
13fd0 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29 3b pCache->pDirty);
13fd1 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 .}../* .** Retur
13fd2 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 n the total numb
13fd3 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 64 er of referenced
13fd4 20 70 61 67 65 73 20 68 65 6c 64 20 62 79 20 74 pages held by t
13fd5 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c he cache..*/.SQL
13fd6 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
13fd7 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
13fd8 43 6f 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43 Count(PCache *pC
13fd9 61 63 68 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 ache){. return
13fda 70 43 61 63 68 65 2d 3e 6e 52 65 66 3b 0a 7d 0a pCache->nRef;.}.
13fdb 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
13fdc 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 e number of refe
13fdd 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 rences to the pa
13fde 67 65 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 ge supplied as a
13fdf 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 n argument..*/.S
13fe0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
13fe1 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 t sqlite3PcacheP
13fe2 61 67 65 52 65 66 63 6f 75 6e 74 28 50 67 48 64 ageRefcount(PgHd
13fe3 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 r *p){. return
13fe4 70 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 20 0a p->nRef;.}../* .
13fe5 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f ** Return the to
13fe6 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 tal number of pa
13fe7 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 ges in the cache
13fe8 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
13fe9 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
13fea 63 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28 50 cachePagecount(P
13feb 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a Cache *pCache){.
13fec 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 30 3b int nPage = 0;
13fed 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 . if( pCache->p
13fee 43 61 63 68 65 20 29 7b 0a 20 20 20 20 6e 50 61 Cache ){. nPa
13fef 67 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 ge = sqlite3Glob
13ff0 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 alConfig.pcache2
13ff1 2e 78 50 61 67 65 63 6f 75 6e 74 28 70 43 61 63 .xPagecount(pCac
13ff2 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 7d he->pCache);. }
13ff3 0a 20 20 72 65 74 75 72 6e 20 6e 50 61 67 65 3b . return nPage;
13ff4 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .}..#ifdef SQLIT
13ff5 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 47 65 74 E_TEST./*.** Get
13ff6 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63 the suggested c
13ff7 61 63 68 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e ache-size value.
13ff8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
13ff9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
13ffa 61 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 acheGetCachesize
13ffb 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 (PCache *pCache)
13ffc 7b 0a 20 20 72 65 74 75 72 6e 20 6e 75 6d 62 65 {. return numbe
13ffd 72 4f 66 43 61 63 68 65 50 61 67 65 73 28 70 43 rOfCachePages(pC
13ffe 61 63 68 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a ache);.}.#endif.
13fff 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 ./*.** Set the s
14000 75 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 uggested cache-s
14001 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 ize value..*/.SQ
14002 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
14003 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 d sqlite3PcacheS
14004 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 etCachesize(PCac
14005 68 65 20 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 he *pCache, int
14006 6d 78 50 61 67 65 29 7b 0a 20 20 70 43 61 63 68 mxPage){. pCach
14007 65 2d 3e 73 7a 43 61 63 68 65 20 3d 20 6d 78 50 e->szCache = mxP
14008 61 67 65 3b 0a 20 20 69 66 28 20 70 43 61 63 68 age;. if( pCach
14009 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 e->pCache ){.
1400a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
1400b 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 43 61 nfig.pcache2.xCa
1400c 63 68 65 73 69 7a 65 28 70 43 61 63 68 65 2d 3e chesize(pCache->
1400d 70 43 61 63 68 65 2c 0a 20 20 20 20 20 20 20 20 pCache,.
1400e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1400f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14010 20 20 20 6e 75 6d 62 65 72 4f 66 43 61 63 68 65 numberOfCache
14011 50 61 67 65 73 28 70 43 61 63 68 65 29 29 3b 0a Pages(pCache));.
14012 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 }.}../*.** Fre
14013 65 20 75 70 20 61 73 20 6d 75 63 68 20 6d 65 6d e up as much mem
14014 6f 72 79 20 61 73 20 70 6f 73 73 69 62 6c 65 20 ory as possible
14015 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20 63 61 from the page ca
14016 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 che..*/.SQLITE_P
14017 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
14018 74 65 33 50 63 61 63 68 65 53 68 72 69 6e 6b 28 te3PcacheShrink(
14019 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
1401a 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 . if( pCache->p
1401b 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c Cache ){. sql
1401c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
1401d 2e 70 63 61 63 68 65 32 2e 78 53 68 72 69 6e 6b .pcache2.xShrink
1401e 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 (pCache->pCache)
1401f 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 64 65 66 ;. }.}..#if def
14020 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 48 45 43 ined(SQLITE_CHEC
14021 4b 5f 50 41 47 45 53 29 20 7c 7c 20 64 65 66 69 K_PAGES) || defi
14022 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
14023 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 61 6c 6c 20 )./*.** For all
14024 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72 dirty pages curr
14025 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 63 61 63 ently in the cac
14026 68 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 73 he, invoke the s
14027 70 65 63 69 66 69 65 64 0a 2a 2a 20 63 61 6c 6c pecified.** call
14028 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 6f 6e back. This is on
14029 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 20 53 ly used if the S
1402a 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
1402b 53 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 64 65 S macro is.** de
1402c 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 fined..*/.SQLITE
1402d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1402e 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 61 lite3PcacheItera
1402f 74 65 44 69 72 74 79 28 50 43 61 63 68 65 20 2a teDirty(PCache *
14030 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a 78 pCache, void (*x
14031 49 74 65 72 29 28 50 67 48 64 72 20 2a 29 29 7b Iter)(PgHdr *)){
14032 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 . PgHdr *pDirty
14033 3b 0a 20 20 66 6f 72 28 70 44 69 72 74 79 3d 70 ;. for(pDirty=p
14034 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70 Cache->pDirty; p
14035 44 69 72 74 79 3b 20 70 44 69 72 74 79 3d 70 44 Dirty; pDirty=pD
14036 69 72 74 79 2d 3e 70 44 69 72 74 79 4e 65 78 74 irty->pDirtyNext
14037 29 7b 0a 20 20 20 20 78 49 74 65 72 28 70 44 69 ){. xIter(pDi
14038 72 74 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 rty);. }.}.#end
14039 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a if../***********
1403a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 68 *** End of pcach
1403b 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.c ************
1403c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1403d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1403e 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1403f 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 *** Begin file p
14040 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a cache1.c *******
14041 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14042 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14043 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4e **/./*.** 2008 N
14044 6f 76 65 6d 62 65 72 20 30 35 0a 2a 2a 0a 2a 2a ovember 05.**.**
14045 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
14046 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
14047 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
14048 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
14049 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1404a 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1404b 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1404c 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1404d 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1404e 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1404f 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
14050 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
14051 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
14052 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
14053 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
14054 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
14055 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
14056 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14057 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14058 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14059 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1405a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1405b 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d This file implem
1405c 65 6e 74 73 20 74 68 65 20 64 65 66 61 75 6c 74 ents the default
1405d 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c page cache impl
1405e 65 6d 65 6e 74 61 74 69 6f 6e 20 28 74 68 65 0a ementation (the.
1405f 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 ** sqlite3_pcach
14060 65 20 69 6e 74 65 72 66 61 63 65 29 2e 20 49 74 e interface). It
14061 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 70 also contains p
14062 61 72 74 20 6f 66 20 74 68 65 20 69 6d 70 6c 65 art of the imple
14063 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 mentation.** of
14064 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 the SQLITE_CONFI
14065 47 5f 50 41 47 45 43 41 43 48 45 20 61 6e 64 20 G_PAGECACHE and
14066 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
14067 6d 65 6d 6f 72 79 28 29 20 66 65 61 74 75 72 65 memory() feature
14068 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 66 s..** If the def
14069 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 20 ault page cache
1406a 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 implementation i
1406b 73 20 6f 76 65 72 72 69 64 65 6e 2c 20 74 68 65 s overriden, the
1406c 6e 20 6e 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20 n neither of.**
1406d 74 68 65 73 65 20 74 77 6f 20 66 65 61 74 75 72 these two featur
1406e 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 es are available
1406f 2e 0a 2a 2f 0a 0a 0a 74 79 70 65 64 65 66 20 73 ..*/...typedef s
14070 74 72 75 63 74 20 50 43 61 63 68 65 31 20 50 43 truct PCache1 PC
14071 61 63 68 65 31 3b 0a 74 79 70 65 64 65 66 20 73 ache1;.typedef s
14072 74 72 75 63 74 20 50 67 48 64 72 31 20 50 67 48 truct PgHdr1 PgH
14073 64 72 31 3b 0a 74 79 70 65 64 65 66 20 73 74 72 dr1;.typedef str
14074 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 20 50 uct PgFreeslot P
14075 67 46 72 65 65 73 6c 6f 74 3b 0a 74 79 70 65 64 gFreeslot;.typed
14076 65 66 20 73 74 72 75 63 74 20 50 47 72 6f 75 70 ef struct PGroup
14077 20 50 47 72 6f 75 70 3b 0a 0a 2f 2a 20 45 61 63 PGroup;../* Eac
14078 68 20 70 61 67 65 20 63 61 63 68 65 20 28 6f 72 h page cache (or
14079 20 50 43 61 63 68 65 29 20 62 65 6c 6f 6e 67 73 PCache) belongs
1407a 20 74 6f 20 61 20 50 47 72 6f 75 70 2e 20 20 41 to a PGroup. A
1407b 20 50 47 72 6f 75 70 20 69 73 20 61 20 73 65 74 PGroup is a set
1407c 20 0a 2a 2a 20 6f 66 20 6f 6e 65 20 6f 72 20 6d .** of one or m
1407d 6f 72 65 20 50 43 61 63 68 65 73 20 74 68 61 74 ore PCaches that
1407e 20 61 72 65 20 61 62 6c 65 20 74 6f 20 72 65 63 are able to rec
1407f 79 63 6c 65 20 65 61 63 68 20 6f 74 68 65 72 73 ycle each others
14080 20 75 6e 70 69 6e 6e 65 64 0a 2a 2a 20 70 61 67 unpinned.** pag
14081 65 73 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 es when they are
14082 20 75 6e 64 65 72 20 6d 65 6d 6f 72 79 20 70 72 under memory pr
14083 65 73 73 75 72 65 2e 20 20 41 20 50 47 72 6f 75 essure. A PGrou
14084 70 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 p is an instance
14085 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f of.** the follo
14086 77 69 6e 67 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a wing object..**.
14087 2a 2a 20 54 68 69 73 20 70 61 67 65 20 63 61 63 ** This page cac
14088 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
14089 6e 20 77 6f 72 6b 73 20 69 6e 20 6f 6e 65 20 6f n works in one o
1408a 66 20 74 77 6f 20 6d 6f 64 65 73 3a 0a 2a 2a 0a f two modes:.**.
1408b 2a 2a 20 20 20 28 31 29 20 20 45 76 65 72 79 20 ** (1) Every
1408c 50 43 61 63 68 65 20 69 73 20 74 68 65 20 73 6f PCache is the so
1408d 6c 65 20 6d 65 6d 62 65 72 20 6f 66 20 69 74 73 le member of its
1408e 20 6f 77 6e 20 50 47 72 6f 75 70 2e 20 20 54 68 own PGroup. Th
1408f 65 72 65 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 ere is.**
14090 20 6f 6e 65 20 50 47 72 6f 75 70 20 70 65 72 20 one PGroup per
14091 50 43 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 PCache..**.**
14092 28 32 29 20 20 54 68 65 72 65 20 69 73 20 61 20 (2) There is a
14093 73 69 6e 67 6c 65 20 67 6c 6f 62 61 6c 20 50 47 single global PG
14094 72 6f 75 70 20 74 68 61 74 20 61 6c 6c 20 50 43 roup that all PC
14095 61 63 68 65 73 20 61 72 65 20 61 20 6d 65 6d 62 aches are a memb
14096 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 6f 66 2e er.** of.
14097 0a 2a 2a 0a 2a 2a 20 4d 6f 64 65 20 31 20 75 73 .**.** Mode 1 us
14098 65 73 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 28 es more memory (
14099 73 69 6e 63 65 20 50 43 61 63 68 65 20 69 6e 73 since PCache ins
1409a 74 61 6e 63 65 73 20 61 72 65 20 6e 6f 74 20 61 tances are not a
1409b 62 6c 65 20 74 6f 20 72 6f 62 0a 2a 2a 20 75 6e ble to rob.** un
1409c 75 73 65 64 20 70 61 67 65 73 20 66 72 6f 6d 20 used pages from
1409d 6f 74 68 65 72 20 50 43 61 63 68 65 73 29 20 62 other PCaches) b
1409e 75 74 20 69 74 20 61 6c 73 6f 20 6f 70 65 72 61 ut it also opera
1409f 74 65 73 20 77 69 74 68 6f 75 74 20 61 20 6d 75 tes without a mu
140a0 74 65 78 2c 0a 2a 2a 20 61 6e 64 20 69 73 20 74 tex,.** and is t
140a1 68 65 72 65 66 6f 72 65 20 6f 66 74 65 6e 20 66 herefore often f
140a2 61 73 74 65 72 2e 20 20 4d 6f 64 65 20 32 20 72 aster. Mode 2 r
140a3 65 71 75 69 72 65 73 20 61 20 6d 75 74 65 78 20 equires a mutex
140a4 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 0a 2a in order to be.*
140a5 2a 20 74 68 72 65 61 64 73 61 66 65 2c 20 62 75 * threadsafe, bu
140a6 74 20 72 65 63 79 63 6c 65 73 20 70 61 67 65 73 t recycles pages
140a7 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 6c more efficientl
140a8 79 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 64 y..**.** For mod
140a9 65 20 28 31 29 2c 20 50 47 72 6f 75 70 2e 6d 75 e (1), PGroup.mu
140aa 74 65 78 20 69 73 20 4e 55 4c 4c 2e 20 20 46 6f tex is NULL. Fo
140ab 72 20 6d 6f 64 65 20 28 32 29 20 74 68 65 72 65 r mode (2) there
140ac 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c is only a singl
140ad 65 0a 2a 2a 20 50 47 72 6f 75 70 20 77 68 69 63 e.** PGroup whic
140ae 68 20 69 73 20 74 68 65 20 70 63 61 63 68 65 31 h is the pcache1
140af 2e 67 72 70 20 67 6c 6f 62 61 6c 20 76 61 72 69 .grp global vari
140b0 61 62 6c 65 20 61 6e 64 20 69 74 73 20 6d 75 74 able and its mut
140b1 65 78 20 69 73 0a 2a 2a 20 53 51 4c 49 54 45 5f ex is.** SQLITE_
140b2 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 MUTEX_STATIC_LRU
140b3 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 47 72 6f ..*/.struct PGro
140b4 75 70 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d up {. sqlite3_m
140b5 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 utex *mutex;
140b6 20 20 20 20 20 20 2f 2a 20 4d 55 54 45 58 5f 53 /* MUTEX_S
140b7 54 41 54 49 43 5f 4c 52 55 20 6f 72 20 4e 55 4c TATIC_LRU or NUL
140b8 4c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 L */. unsigned
140b9 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20 int nMaxPage;
140ba 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 /* Sum of
140bb 6e 4d 61 78 20 66 6f 72 20 70 75 72 67 65 61 62 nMax for purgeab
140bc 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 75 le caches */. u
140bd 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d 69 6e nsigned int nMin
140be 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 2f 2a Page; /*
140bf 20 53 75 6d 20 6f 66 20 6e 4d 69 6e 20 66 6f 72 Sum of nMin for
140c0 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 purgeable cache
140c1 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 s */. unsigned
140c2 69 6e 74 20 6d 78 50 69 6e 6e 65 64 3b 20 20 20 int mxPinned;
140c3 20 20 20 20 20 20 2f 2a 20 6e 4d 61 78 70 61 67 /* nMaxpag
140c4 65 20 2b 20 31 30 20 2d 20 6e 4d 69 6e 50 61 67 e + 10 - nMinPag
140c5 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 e */. unsigned
140c6 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61 67 65 int nCurrentPage
140c7 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 ; /* Number
140c8 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 of purgeable pag
140c9 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a es allocated */.
140ca 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65 PgHdr1 *pLruHe
140cb 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20 ad, *pLruTail;
140cc 20 2f 2a 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 /* LRU list of
140cd 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a unpinned pages *
140ce 2f 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 70 61 /.};../* Each pa
140cf 67 65 20 63 61 63 68 65 20 69 73 20 61 6e 20 69 ge cache is an i
140d0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
140d1 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74 2e ollowing object.
140d2 20 20 45 76 65 72 79 0a 2a 2a 20 6f 70 65 6e 20 Every.** open
140d3 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 69 database file (i
140d4 6e 63 6c 75 64 69 6e 67 20 65 61 63 68 20 69 6e ncluding each in
140d5 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
140d6 20 61 6e 64 20 65 61 63 68 0a 2a 2a 20 74 65 6d and each.** tem
140d7 70 6f 72 61 72 79 20 6f 72 20 74 72 61 6e 73 69 porary or transi
140d8 65 6e 74 20 64 61 74 61 62 61 73 65 29 20 68 61 ent database) ha
140d9 73 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 s a single page
140da 63 61 63 68 65 20 77 68 69 63 68 0a 2a 2a 20 69 cache which.** i
140db 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
140dc 20 74 68 69 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a this object..**
140dd 0a 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 .** Pointers to
140de 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 74 68 structures of th
140df 69 73 20 74 79 70 65 20 61 72 65 20 63 61 73 74 is type are cast
140e0 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 61 73 and returned as
140e1 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 71 6c 69 .** opaque sqli
140e2 74 65 33 5f 70 63 61 63 68 65 2a 20 68 61 6e 64 te3_pcache* hand
140e3 6c 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 les..*/.struct P
140e4 43 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 43 61 Cache1 {. /* Ca
140e5 63 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f che configuratio
140e6 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20 50 61 n parameters. Pa
140e7 67 65 20 73 69 7a 65 20 28 73 7a 50 61 67 65 29 ge size (szPage)
140e8 20 61 6e 64 20 74 68 65 20 70 75 72 67 65 61 62 and the purgeab
140e9 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62 50 le. ** flag (bP
140ea 75 72 67 65 61 62 6c 65 29 20 61 72 65 20 73 65 urgeable) are se
140eb 74 20 77 68 65 6e 20 74 68 65 20 63 61 63 68 65 t when the cache
140ec 20 69 73 20 63 72 65 61 74 65 64 2e 20 6e 4d 61 is created. nMa
140ed 78 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a 20 6d x may be . ** m
140ee 6f 64 69 66 69 65 64 20 61 74 20 61 6e 79 20 74 odified at any t
140ef 69 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74 6f ime by a call to
140f0 20 74 68 65 20 70 63 61 63 68 65 31 43 61 63 68 the pcache1Cach
140f1 65 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e 0a esize() method..
140f2 20 20 2a 2a 20 54 68 65 20 50 47 72 6f 75 70 20 ** The PGroup
140f3 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 mutex must be he
140f4 6c 64 20 77 68 65 6e 20 61 63 63 65 73 73 69 6e ld when accessin
140f5 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20 50 g nMax.. */. P
140f6 47 72 6f 75 70 20 2a 70 47 72 6f 75 70 3b 20 20 Group *pGroup;
140f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
140f8 20 20 20 2f 2a 20 50 47 72 6f 75 70 20 74 68 69 /* PGroup thi
140f9 73 20 63 61 63 68 65 20 62 65 6c 6f 6e 67 73 20 s cache belongs
140fa 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 to */. int szPa
140fb 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
140fc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
140fd 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 ize of allocated
140fe 20 70 61 67 65 73 20 69 6e 20 62 79 74 65 73 20 pages in bytes
140ff 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61 */. int szExtra
14100 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
14101 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
14102 20 6f 66 20 65 78 74 72 61 20 73 70 61 63 65 20 of extra space
14103 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e in bytes */. in
14104 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20 t bPurgeable;
14105 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14106 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63 /* True if cac
14107 68 65 20 69 73 20 70 75 72 67 65 61 62 6c 65 20 he is purgeable
14108 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e */. unsigned in
14109 74 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20 20 t nMin;
1410a 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 /* Mini
1410b 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 mum number of pa
1410c 67 65 73 20 72 65 73 65 72 76 65 64 20 2a 2f 0a ges reserved */.
1410d 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e unsigned int n
1410e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 Max;
1410f 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 /* Configu
14110 72 65 64 20 22 63 61 63 68 65 5f 73 69 7a 65 22 red "cache_size"
14111 20 76 61 6c 75 65 20 2a 2f 0a 20 20 75 6e 73 69 value */. unsi
14112 67 6e 65 64 20 69 6e 74 20 6e 39 30 70 63 74 3b gned int n90pct;
14113 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14114 2f 2a 20 6e 4d 61 78 2a 39 2f 31 30 20 2a 2f 0a /* nMax*9/10 */.
14115 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 unsigned int i
14116 4d 61 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20 MaxKey;
14117 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 /* Largest
14118 20 6b 65 79 20 73 65 65 6e 20 73 69 6e 63 65 20 key seen since
14119 78 54 72 75 6e 63 61 74 65 28 29 20 2a 2f 0a 0a xTruncate() */..
1411a 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 /* Hash table
1411b 6f 66 20 61 6c 6c 20 70 61 67 65 73 2e 20 54 68 of all pages. Th
1411c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 e following vari
1411d 61 62 6c 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62 ables may only b
1411e 65 20 61 63 63 65 73 73 65 64 0a 20 20 2a 2a 20 e accessed. **
1411f 77 68 65 6e 20 74 68 65 20 61 63 63 65 73 73 6f when the accesso
14120 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 r is holding the
14121 20 50 47 72 6f 75 70 20 6d 75 74 65 78 2e 0a 20 PGroup mutex..
14122 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 */. unsigned i
14123 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 20 nt nRecyclable;
14124 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
14125 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
14126 74 68 65 20 4c 52 55 20 6c 69 73 74 20 2a 2f 0a the LRU list */.
14127 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e unsigned int n
14128 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
14129 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e /* Total n
1412a 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
1412b 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 20 75 6e n apHash */. un
1412c 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61 73 68 signed int nHash
1412d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1412e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
1412f 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68 5b 5d lots in apHash[]
14130 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 2a 61 */. PgHdr1 **a
14131 70 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 pHash;
14132 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 /* Has
14133 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61 73 74 h table for fast
14134 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79 20 2a lookup by key *
14135 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 /.};../*.** Each
14136 20 63 61 63 68 65 20 65 6e 74 72 79 20 69 73 20 cache entry is
14137 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 represented by a
14138 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
14139 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 e following .**
1413a 73 74 72 75 63 74 75 72 65 2e 20 55 6e 6c 65 73 structure. Unles
1413b 73 20 53 51 4c 49 54 45 5f 50 43 41 43 48 45 5f s SQLITE_PCACHE_
1413c 53 45 50 41 52 41 54 45 5f 48 45 41 44 45 52 20 SEPARATE_HEADER
1413d 69 73 20 64 65 66 69 6e 65 64 2c 20 61 20 62 75 is defined, a bu
1413e 66 66 65 72 20 6f 66 0a 2a 2a 20 50 67 48 64 72 ffer of.** PgHdr
1413f 31 2e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 1.pCache->szPage
14140 20 62 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 61 bytes is alloca
14141 74 65 64 20 64 69 72 65 63 74 6c 79 20 62 65 66 ted directly bef
14142 6f 72 65 20 74 68 69 73 20 73 74 72 75 63 74 75 ore this structu
14143 72 65 20 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 re .** in memory
14144 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 64 ..*/.struct PgHd
14145 72 31 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 70 r1 {. sqlite3_p
14146 63 61 63 68 65 5f 70 61 67 65 20 70 61 67 65 3b cache_page page;
14147 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
14148 69 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 iKey;
14149 20 20 2f 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 /* Key value (
1414a 70 61 67 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a page number) */.
1414b 20 20 50 67 48 64 72 31 20 2a 70 4e 65 78 74 3b PgHdr1 *pNext;
1414c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1414d 20 2f 2a 20 4e 65 78 74 20 69 6e 20 68 61 73 68 /* Next in hash
1414e 20 74 61 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a table chain */.
1414f 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 PCache1 *pCach
14150 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
14151 20 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 63 /* Cache that c
14152 75 72 72 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 urrently owns th
14153 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 is page */. PgH
14154 64 72 31 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 dr1 *pLruNext;
14155 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
14156 65 78 74 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 ext in LRU list
14157 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 of unpinned page
14158 73 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 s */. PgHdr1 *p
14159 4c 72 75 50 72 65 76 3b 20 20 20 20 20 20 20 20 LruPrev;
1415a 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 /* Previou
1415b 73 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 s in LRU list of
1415c 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 unpinned pages
1415d 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 */.};../*.** Fre
1415e 65 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 e slots in the a
1415f 6c 6c 6f 63 61 74 6f 72 20 75 73 65 64 20 74 6f llocator used to
14160 20 64 69 76 69 64 65 20 75 70 20 74 68 65 20 62 divide up the b
14161 75 66 66 65 72 20 70 72 6f 76 69 64 65 64 20 75 uffer provided u
14162 73 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 sing.** the SQLI
14163 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 TE_CONFIG_PAGECA
14164 43 48 45 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a CHE mechanism..*
14165 2f 0a 73 74 72 75 63 74 20 50 67 46 72 65 65 73 /.struct PgFrees
14166 6c 6f 74 20 7b 0a 20 20 50 67 46 72 65 65 73 6c lot {. PgFreesl
14167 6f 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e ot *pNext; /* N
14168 65 78 74 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f ext free slot */
14169 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 .};../*.** Globa
1416a 6c 20 64 61 74 61 20 75 73 65 64 20 62 79 20 74 l data used by t
1416b 68 69 73 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 his cache..*/.st
1416c 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
1416d 73 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f struct PCacheGlo
1416e 62 61 6c 20 7b 0a 20 20 50 47 72 6f 75 70 20 67 bal {. PGroup g
1416f 72 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rp;
14170 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 67 6c /* The gl
14171 6f 62 61 6c 20 50 47 72 6f 75 70 20 66 6f 72 20 obal PGroup for
14172 6d 6f 64 65 20 28 32 29 20 2a 2f 0a 0a 20 20 2f mode (2) */.. /
14173 2a 20 56 61 72 69 61 62 6c 65 73 20 72 65 6c 61 * Variables rela
14174 74 65 64 20 74 6f 20 53 51 4c 49 54 45 5f 43 4f ted to SQLITE_CO
14175 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 73 NFIG_PAGECACHE s
14176 65 74 74 69 6e 67 73 2e 20 20 54 68 65 0a 20 20 ettings. The.
14177 2a 2a 20 73 7a 53 6c 6f 74 2c 20 6e 53 6c 6f 74 ** szSlot, nSlot
14178 2c 20 70 53 74 61 72 74 2c 20 70 45 6e 64 2c 20 , pStart, pEnd,
14179 6e 52 65 73 65 72 76 65 2c 20 61 6e 64 20 69 73 nReserve, and is
1417a 49 6e 69 74 20 76 61 6c 75 65 73 20 61 72 65 20 Init values are
1417b 61 6c 6c 0a 20 20 2a 2a 20 66 69 78 65 64 20 61 all. ** fixed a
1417c 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 t sqlite3_initia
1417d 6c 69 7a 65 28 29 20 74 69 6d 65 20 61 6e 64 20 lize() time and
1417e 64 6f 20 6e 6f 74 20 72 65 71 75 69 72 65 20 6d do not require m
1417f 75 74 65 78 20 70 72 6f 74 65 63 74 69 6f 6e 2e utex protection.
14180 0a 20 20 2a 2a 20 54 68 65 20 6e 46 72 65 65 53 . ** The nFreeS
14181 6c 6f 74 20 61 6e 64 20 70 46 72 65 65 20 76 61 lot and pFree va
14182 6c 75 65 73 20 64 6f 20 72 65 71 75 69 72 65 20 lues do require
14183 6d 75 74 65 78 20 70 72 6f 74 65 63 74 69 6f 6e mutex protection
14184 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 .. */. int isI
14185 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 nit;
14186 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
14187 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a if initialized *
14188 2f 0a 20 20 69 6e 74 20 73 7a 53 6c 6f 74 3b 20 /. int szSlot;
14189 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1418a 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 /* Size of ea
1418b 63 68 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a ch free slot */.
1418c 20 20 69 6e 74 20 6e 53 6c 6f 74 3b 20 20 20 20 int nSlot;
1418d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1418e 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f /* The number o
1418f 66 20 70 63 61 63 68 65 20 73 6c 6f 74 73 20 2a f pcache slots *
14190 2f 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65 /. int nReserve
14191 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
14192 20 20 20 2f 2a 20 54 72 79 20 74 6f 20 6b 65 65 /* Try to kee
14193 70 20 6e 46 72 65 65 53 6c 6f 74 20 61 62 6f 76 p nFreeSlot abov
14194 65 20 74 68 69 73 20 2a 2f 0a 20 20 76 6f 69 64 e this */. void
14195 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b *pStart, *pEnd;
14196 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f /* Bo
14197 75 6e 64 73 20 6f 66 20 70 61 67 65 63 61 63 68 unds of pagecach
14198 65 20 6d 61 6c 6c 6f 63 20 72 61 6e 67 65 20 2a e malloc range *
14199 2f 0a 20 20 2f 2a 20 41 62 6f 76 65 20 72 65 71 /. /* Above req
1419a 75 69 72 65 73 20 6e 6f 20 6d 75 74 65 78 2e 20 uires no mutex.
1419b 20 55 73 65 20 6d 75 74 65 78 20 62 65 6c 6f 77 Use mutex below
1419c 20 66 6f 72 20 76 61 72 69 61 62 6c 65 20 74 68 for variable th
1419d 61 74 20 66 6f 6c 6c 6f 77 2e 20 2a 2f 0a 20 20 at follow. */.
1419e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d sqlite3_mutex *m
1419f 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 2f utex; /
141a0 2a 20 4d 75 74 65 78 20 66 6f 72 20 61 63 63 65 * Mutex for acce
141a1 73 73 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 ssing the follow
141a2 69 6e 67 3a 20 2a 2f 0a 20 20 50 67 46 72 65 65 ing: */. PgFree
141a3 73 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 20 slot *pFree;
141a4 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 /* Free
141a5 20 70 61 67 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a page blocks */.
141a6 20 20 69 6e 74 20 6e 46 72 65 65 53 6c 6f 74 3b int nFreeSlot;
141a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
141a8 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e /* Number of un
141a9 75 73 65 64 20 70 63 61 63 68 65 20 73 6c 6f 74 used pcache slot
141aa 73 20 2a 2f 0a 20 20 2f 2a 20 54 68 65 20 66 6f s */. /* The fo
141ab 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 72 65 llowing value re
141ac 71 75 69 72 65 73 20 61 20 6d 75 74 65 78 20 74 quires a mutex t
141ad 6f 20 63 68 61 6e 67 65 2e 20 20 57 65 20 73 6b o change. We sk
141ae 69 70 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 0a ip the mutex on.
141af 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 62 65 63 ** reading bec
141b0 61 75 73 65 20 28 31 29 20 6d 6f 73 74 20 70 6c ause (1) most pl
141b1 61 74 66 6f 72 6d 73 20 72 65 61 64 20 61 20 33 atforms read a 3
141b2 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 61 74 2-bit integer at
141b3 6f 6d 69 63 61 6c 6c 79 20 61 6e 64 0a 20 20 2a omically and. *
141b4 2a 20 28 32 29 20 65 76 65 6e 20 69 66 20 61 6e * (2) even if an
141b5 20 69 6e 63 6f 72 72 65 63 74 20 76 61 6c 75 65 incorrect value
141b6 20 69 73 20 72 65 61 64 2c 20 6e 6f 20 67 72 65 is read, no gre
141b7 61 74 20 68 61 72 6d 20 69 73 20 64 6f 6e 65 20 at harm is done
141b8 73 69 6e 63 65 20 74 68 69 73 0a 20 20 2a 2a 20 since this. **
141b9 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61 is really just a
141ba 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 n optimization.
141bb 2a 2f 0a 20 20 69 6e 74 20 62 55 6e 64 65 72 50 */. int bUnderP
141bc 72 65 73 73 75 72 65 3b 20 20 20 20 20 20 20 20 ressure;
141bd 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c /* True if l
141be 6f 77 20 6f 6e 20 50 41 47 45 43 41 43 48 45 20 ow on PAGECACHE
141bf 6d 65 6d 6f 72 79 20 2a 2f 0a 7d 20 70 63 61 63 memory */.} pcac
141c0 68 65 31 5f 67 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c he1_g;../*.** Al
141c1 6c 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 l code in this f
141c2 69 6c 65 20 73 68 6f 75 6c 64 20 61 63 63 65 73 ile should acces
141c3 73 20 74 68 65 20 67 6c 6f 62 61 6c 20 73 74 72 s the global str
141c4 75 63 74 75 72 65 20 61 62 6f 76 65 20 76 69 61 ucture above via
141c5 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73 20 22 70 the.** alias "p
141c6 63 61 63 68 65 31 22 2e 20 54 68 69 73 20 65 6e cache1". This en
141c7 73 75 72 65 73 20 74 68 61 74 20 74 68 65 20 57 sures that the W
141c8 53 44 20 65 6d 75 6c 61 74 69 6f 6e 20 69 73 20 SD emulation is
141c9 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20 63 6f 6d used when.** com
141ca 70 69 6c 69 6e 67 20 66 6f 72 20 73 79 73 74 65 piling for syste
141cb 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 73 ms that do not s
141cc 75 70 70 6f 72 74 20 72 65 61 6c 20 57 53 44 2e upport real WSD.
141cd 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70 63 61 63 .*/.#define pcac
141ce 68 65 31 20 28 47 4c 4f 42 41 4c 28 73 74 72 75 he1 (GLOBAL(stru
141cf 63 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c 2c ct PCacheGlobal,
141d0 20 70 63 61 63 68 65 31 5f 67 29 29 0a 0a 2f 2a pcache1_g))../*
141d1 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 65 6e .** Macros to en
141d2 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 74 68 ter and leave th
141d3 65 20 50 43 61 63 68 65 20 4c 52 55 20 6d 75 74 e PCache LRU mut
141d4 65 78 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70 ex..*/.#define p
141d5 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 cache1EnterMutex
141d6 28 58 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 (X) sqlite3_mute
141d7 78 5f 65 6e 74 65 72 28 28 58 29 2d 3e 6d 75 74 x_enter((X)->mut
141d8 65 78 29 0a 23 64 65 66 69 6e 65 20 70 63 61 63 ex).#define pcac
141d9 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 58 29 he1LeaveMutex(X)
141da 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
141db 65 61 76 65 28 28 58 29 2d 3e 6d 75 74 65 78 29 eave((X)->mutex)
141dc 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
141dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
141de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
141df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
141e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
141e1 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 */./******** Pag
141e2 65 20 41 6c 6c 6f 63 61 74 69 6f 6e 2f 53 51 4c e Allocation/SQL
141e3 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 ITE_CONFIG_PCACH
141e4 45 20 52 65 6c 61 74 65 64 20 46 75 6e 63 74 69 E Related Functi
141e5 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ons ************
141e6 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 **/../*.** This
141e7 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
141e8 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 ed during initia
141e9 6c 69 7a 61 74 69 6f 6e 20 69 66 20 61 20 73 74 lization if a st
141ea 61 74 69 63 20 62 75 66 66 65 72 20 69 73 20 0a atic buffer is .
141eb 2a 2a 20 73 75 70 70 6c 69 65 64 20 74 6f 20 75 ** supplied to u
141ec 73 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 2d se for the page-
141ed 63 61 63 68 65 20 62 79 20 70 61 73 73 69 6e 67 cache by passing
141ee 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 the SQLITE_CONF
141ef 49 47 5f 50 41 47 45 43 41 43 48 45 0a 2a 2a 20 IG_PAGECACHE.**
141f0 76 65 72 62 20 74 6f 20 73 71 6c 69 74 65 33 5f verb to sqlite3_
141f1 63 6f 6e 66 69 67 28 29 2e 20 50 61 72 61 6d 65 config(). Parame
141f2 74 65 72 20 70 42 75 66 20 70 6f 69 6e 74 73 20 ter pBuf points
141f3 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e to an allocation
141f4 20 6c 61 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68 large.** enough
141f5 20 74 6f 20 63 6f 6e 74 61 69 6e 20 27 6e 27 20 to contain 'n'
141f6 62 75 66 66 65 72 73 20 6f 66 20 27 73 7a 27 20 buffers of 'sz'
141f7 62 79 74 65 73 20 65 61 63 68 2e 0a 2a 2a 0a 2a bytes each..**.*
141f8 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
141f9 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71 s called from sq
141fa 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
141fb 28 29 20 61 6e 64 20 73 6f 20 69 74 20 69 73 20 () and so it is
141fc 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f guaranteed.** to
141fd 20 62 65 20 73 65 72 69 61 6c 69 7a 65 64 20 61 be serialized a
141fe 6c 72 65 61 64 79 2e 20 20 54 68 65 72 65 20 69 lready. There i
141ff 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 66 75 s no need for fu
14200 72 74 68 65 72 20 6d 75 74 65 78 69 6e 67 2e 0a rther mutexing..
14201 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14202 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 E void sqlite3PC
14203 61 63 68 65 42 75 66 66 65 72 53 65 74 75 70 28 acheBufferSetup(
14204 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 void *pBuf, int
14205 73 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 66 sz, int n){. if
14206 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74 ( pcache1.isInit
14207 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73 6c ){. PgFreesl
14208 6f 74 20 2a 70 3b 0a 20 20 20 20 73 7a 20 3d 20 ot *p;. sz =
14209 52 4f 55 4e 44 44 4f 57 4e 38 28 73 7a 29 3b 0a ROUNDDOWN8(sz);.
1420a 20 20 20 20 70 63 61 63 68 65 31 2e 73 7a 53 6c pcache1.szSl
1420b 6f 74 20 3d 20 73 7a 3b 0a 20 20 20 20 70 63 61 ot = sz;. pca
1420c 63 68 65 31 2e 6e 53 6c 6f 74 20 3d 20 70 63 61 che1.nSlot = pca
1420d 63 68 65 31 2e 6e 46 72 65 65 53 6c 6f 74 20 3d che1.nFreeSlot =
1420e 20 6e 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e n;. pcache1.
1420f 6e 52 65 73 65 72 76 65 20 3d 20 6e 3e 39 30 20 nReserve = n>90
14210 3f 20 31 30 20 3a 20 28 6e 2f 31 30 20 2b 20 31 ? 10 : (n/10 + 1
14211 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 );. pcache1.p
14212 53 74 61 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 Start = pBuf;.
14213 20 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 pcache1.pFree
14214 3d 20 30 3b 0a 20 20 20 20 70 63 61 63 68 65 31 = 0;. pcache1
14215 2e 62 55 6e 64 65 72 50 72 65 73 73 75 72 65 20 .bUnderPressure
14216 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 = 0;. while(
14217 6e 2d 2d 20 29 7b 0a 20 20 20 20 20 20 70 20 3d n-- ){. p =
14218 20 28 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 42 (PgFreeslot*)pB
14219 75 66 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 uf;. p->pNe
1421a 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72 xt = pcache1.pFr
1421b 65 65 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 ee;. pcache
1421c 31 2e 70 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 1.pFree = p;.
1421d 20 20 20 70 42 75 66 20 3d 20 28 76 6f 69 64 2a pBuf = (void*
1421e 29 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b )&((char*)pBuf)[
1421f 73 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 sz];. }. p
14220 63 61 63 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 cache1.pEnd = pB
14221 75 66 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a uf;. }.}../*.**
14222 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e Malloc function
14223 20 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 69 used within thi
14224 73 20 66 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 s file to alloca
14225 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68 te space from th
14226 65 20 62 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 66 e buffer.** conf
14227 69 67 75 72 65 64 20 75 73 69 6e 67 20 73 71 6c igured using sql
14228 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 ite3_config(SQLI
14229 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 TE_CONFIG_PAGECA
1422a 43 48 45 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 CHE) option. If
1422b 6e 6f 20 0a 2a 2a 20 73 75 63 68 20 62 75 66 66 no .** such buff
1422c 65 72 20 65 78 69 73 74 73 20 6f 72 20 74 68 65 er exists or the
1422d 72 65 20 69 73 20 6e 6f 20 73 70 61 63 65 20 6c re is no space l
1422e 65 66 74 20 69 6e 20 69 74 2c 20 74 68 69 73 20 eft in it, this
1422f 66 75 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 20 0a function falls .
14230 2a 2a 20 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 ** back to sqlit
14231 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a e3Malloc()..**.*
14232 2a 20 4d 75 6c 74 69 70 6c 65 20 74 68 72 65 61 * Multiple threa
14233 64 73 20 63 61 6e 20 72 75 6e 20 74 68 69 73 20 ds can run this
14234 72 6f 75 74 69 6e 65 20 61 74 20 74 68 65 20 73 routine at the s
14235 61 6d 65 20 74 69 6d 65 2e 20 20 47 6c 6f 62 61 ame time. Globa
14236 6c 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 69 l variables.** i
14237 6e 20 70 63 61 63 68 65 31 20 6e 65 65 64 20 74 n pcache1 need t
14238 6f 20 62 65 20 70 72 6f 74 65 63 74 65 64 20 76 o be protected v
14239 69 61 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 ia mutex..*/.sta
1423a 74 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65 tic void *pcache
1423b 31 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 1Alloc(int nByte
1423c 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 ){. void *p = 0
1423d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
1423e 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
1423f 64 28 70 63 61 63 68 65 31 2e 67 72 70 2e 6d 75 d(pcache1.grp.mu
14240 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
14241 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 3StatusSet(SQLIT
14242 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 E_STATUS_PAGECAC
14243 48 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b HE_SIZE, nByte);
14244 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 70 63 . if( nByte<=pc
14245 61 63 68 65 31 2e 73 7a 53 6c 6f 74 20 29 7b 0a ache1.szSlot ){.
14246 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
14247 78 5f 65 6e 74 65 72 28 70 63 61 63 68 65 31 2e x_enter(pcache1.
14248 6d 75 74 65 78 29 3b 0a 20 20 20 20 70 20 3d 20 mutex);. p =
14249 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 68 65 (PgHdr1 *)pcache
1424a 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 69 66 28 1.pFree;. if(
1424b 20 70 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 p ){. pcac
1424c 68 65 31 2e 70 46 72 65 65 20 3d 20 70 63 61 63 he1.pFree = pcac
1424d 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e 65 78 74 he1.pFree->pNext
1424e 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e ;. pcache1.
1424f 6e 46 72 65 65 53 6c 6f 74 2d 2d 3b 0a 20 20 20 nFreeSlot--;.
14250 20 20 20 70 63 61 63 68 65 31 2e 62 55 6e 64 65 pcache1.bUnde
14251 72 50 72 65 73 73 75 72 65 20 3d 20 70 63 61 63 rPressure = pcac
14252 68 65 31 2e 6e 46 72 65 65 53 6c 6f 74 3c 70 63 he1.nFreeSlot<pc
14253 61 63 68 65 31 2e 6e 52 65 73 65 72 76 65 3b 0a ache1.nReserve;.
14254 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 63 assert( pc
14255 61 63 68 65 31 2e 6e 46 72 65 65 53 6c 6f 74 3e ache1.nFreeSlot>
14256 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 =0 );. sqli
14257 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
14258 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 ITE_STATUS_PAGEC
14259 41 43 48 45 5f 55 53 45 44 2c 20 31 29 3b 0a 20 ACHE_USED, 1);.
1425a 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1425b 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61 _mutex_leave(pca
1425c 63 68 65 31 2e 6d 75 74 65 78 29 3b 0a 20 20 7d che1.mutex);. }
1425d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 . if( p==0 ){.
1425e 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 69 73 20 /* Memory is
1425f 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e not available in
14260 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 the SQLITE_CONF
14261 49 47 5f 50 41 47 45 43 41 43 48 45 20 70 6f 6f IG_PAGECACHE poo
14262 6c 2e 20 20 47 65 74 0a 20 20 20 20 2a 2a 20 69 l. Get. ** i
14263 74 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61 t from sqlite3Ma
14264 6c 6c 6f 63 20 69 6e 73 74 65 61 64 2e 0a 20 20 lloc instead..
14265 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 73 71 6c */. p = sql
14266 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 ite3Malloc(nByte
14267 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
14268 45 5f 44 49 53 41 42 4c 45 5f 50 41 47 45 43 41 E_DISABLE_PAGECA
14269 43 48 45 5f 4f 56 45 52 46 4c 4f 57 5f 53 54 41 CHE_OVERFLOW_STA
1426a 54 53 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a TS. if( p ){.
1426b 20 20 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 73 int sz = s
1426c 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 qlite3MallocSize
1426d 28 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 (p);. sqlit
1426e 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 e3_mutex_enter(p
1426f 63 61 63 68 65 31 2e 6d 75 74 65 78 29 3b 0a 20 cache1.mutex);.
14270 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
14271 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
14272 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 TUS_PAGECACHE_OV
14273 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20 20 20 ERFLOW, sz);.
14274 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
14275 5f 6c 65 61 76 65 28 70 63 61 63 68 65 31 2e 6d _leave(pcache1.m
14276 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 23 65 6e utex);. }.#en
14277 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 4d dif. sqlite3M
14278 65 6d 64 65 62 75 67 53 65 74 54 79 70 65 28 70 emdebugSetType(p
14279 2c 20 4d 45 4d 54 59 50 45 5f 50 43 41 43 48 45 , MEMTYPE_PCACHE
1427a 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1427b 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 p;.}../*.** Free
1427c 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 an allocated bu
1427d 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72 ffer obtained fr
1427e 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 om pcache1Alloc(
1427f 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
14280 20 70 63 61 63 68 65 31 46 72 65 65 28 76 6f 69 pcache1Free(voi
14281 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 46 72 d *p){. int nFr
14282 65 65 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 eed = 0;. if( p
14283 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
14284 20 20 69 66 28 20 70 3e 3d 70 63 61 63 68 65 31 if( p>=pcache1
14285 2e 70 53 74 61 72 74 20 26 26 20 70 3c 70 63 61 .pStart && p<pca
14286 63 68 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 20 che1.pEnd ){.
14287 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c PgFreeslot *pSl
14288 6f 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ot;. sqlite3_
14289 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63 61 63 mutex_enter(pcac
1428a 68 65 31 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 he1.mutex);.
1428b 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
1428c 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 (SQLITE_STATUS_P
1428d 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c 20 2d AGECACHE_USED, -
1428e 31 29 3b 0a 20 20 20 20 70 53 6c 6f 74 20 3d 20 1);. pSlot =
1428f 28 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 3b 0a (PgFreeslot*)p;.
14290 20 20 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 pSlot->pNext
14291 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 = pcache1.pFree
14292 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 ;. pcache1.pF
14293 72 65 65 20 3d 20 70 53 6c 6f 74 3b 0a 20 20 20 ree = pSlot;.
14294 20 70 63 61 63 68 65 31 2e 6e 46 72 65 65 53 6c pcache1.nFreeSl
14295 6f 74 2b 2b 3b 0a 20 20 20 20 70 63 61 63 68 65 ot++;. pcache
14296 31 2e 62 55 6e 64 65 72 50 72 65 73 73 75 72 65 1.bUnderPressure
14297 20 3d 20 70 63 61 63 68 65 31 2e 6e 46 72 65 65 = pcache1.nFree
14298 53 6c 6f 74 3c 70 63 61 63 68 65 31 2e 6e 52 65 Slot<pcache1.nRe
14299 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73 65 72 serve;. asser
1429a 74 28 20 70 63 61 63 68 65 31 2e 6e 46 72 65 65 t( pcache1.nFree
1429b 53 6c 6f 74 3c 3d 70 63 61 63 68 65 31 2e 6e 53 Slot<=pcache1.nS
1429c 6c 6f 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 lot );. sqlit
1429d 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
1429e 63 61 63 68 65 31 2e 6d 75 74 65 78 29 3b 0a 20 cache1.mutex);.
1429f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
142a0 72 74 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 rt( sqlite3Memde
142a1 62 75 67 48 61 73 54 79 70 65 28 70 2c 20 4d 45 bugHasType(p, ME
142a2 4d 54 59 50 45 5f 50 43 41 43 48 45 29 20 29 3b MTYPE_PCACHE) );
142a3 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 64 . sqlite3Memd
142a4 65 62 75 67 53 65 74 54 79 70 65 28 70 2c 20 4d ebugSetType(p, M
142a5 45 4d 54 59 50 45 5f 48 45 41 50 29 3b 0a 20 20 EMTYPE_HEAP);.
142a6 20 20 6e 46 72 65 65 64 20 3d 20 73 71 6c 69 74 nFreed = sqlit
142a7 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b e3MallocSize(p);
142a8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
142a9 44 49 53 41 42 4c 45 5f 50 41 47 45 43 41 43 48 DISABLE_PAGECACH
142aa 45 5f 4f 56 45 52 46 4c 4f 57 5f 53 54 41 54 53 E_OVERFLOW_STATS
142ab 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
142ac 65 78 5f 65 6e 74 65 72 28 70 63 61 63 68 65 31 ex_enter(pcache1
142ad 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c .mutex);. sql
142ae 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 ite3StatusAdd(SQ
142af 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
142b0 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 CACHE_OVERFLOW,
142b1 2d 6e 46 72 65 65 64 29 3b 0a 20 20 20 20 73 71 -nFreed);. sq
142b2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
142b3 65 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 e(pcache1.mutex)
142b4 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c ;.#endif. sql
142b5 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
142b6 7d 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65 }. return nFree
142b7 64 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c d;.}..#ifdef SQL
142b8 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 ITE_ENABLE_MEMOR
142b9 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a Y_MANAGEMENT./*.
142ba 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 ** Return the si
142bb 7a 65 20 6f 66 20 61 20 70 63 61 63 68 65 20 61 ze of a pcache a
142bc 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 llocation.*/.sta
142bd 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 4d tic int pcache1M
142be 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b emSize(void *p){
142bf 0a 20 20 69 66 28 20 70 3e 3d 70 63 61 63 68 65 . if( p>=pcache
142c0 31 2e 70 53 74 61 72 74 20 26 26 20 70 3c 70 63 1.pStart && p<pc
142c1 61 63 68 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 ache1.pEnd ){.
142c2 20 20 72 65 74 75 72 6e 20 70 63 61 63 68 65 31 return pcache1
142c3 2e 73 7a 53 6c 6f 74 3b 0a 20 20 7d 65 6c 73 65 .szSlot;. }else
142c4 7b 0a 20 20 20 20 69 6e 74 20 69 53 69 7a 65 3b {. int iSize;
142c5 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
142c6 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54 ite3MemdebugHasT
142c7 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 50 ype(p, MEMTYPE_P
142c8 43 41 43 48 45 29 20 29 3b 0a 20 20 20 20 73 71 CACHE) );. sq
142c9 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 lite3MemdebugSet
142ca 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f Type(p, MEMTYPE_
142cb 48 45 41 50 29 3b 0a 20 20 20 20 69 53 69 7a 65 HEAP);. iSize
142cc 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
142cd 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 73 71 6c Size(p);. sql
142ce 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 54 ite3MemdebugSetT
142cf 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 50 ype(p, MEMTYPE_P
142d0 43 41 43 48 45 29 3b 0a 20 20 20 20 72 65 74 75 CACHE);. retu
142d1 72 6e 20 69 53 69 7a 65 3b 0a 20 20 7d 0a 7d 0a rn iSize;. }.}.
142d2 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
142d3 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d _ENABLE_MEMORY_M
142d4 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a 2f 2a ANAGEMENT */../*
142d5 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e .** Allocate a n
142d6 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 ew page object i
142d7 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61 nitially associa
142d8 74 65 64 20 77 69 74 68 20 63 61 63 68 65 20 70 ted with cache p
142d9 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 Cache..*/.static
142da 20 50 67 48 64 72 31 20 2a 70 63 61 63 68 65 31 PgHdr1 *pcache1
142db 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65 AllocPage(PCache
142dc 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 1 *pCache){. Pg
142dd 48 64 72 31 20 2a 70 20 3d 20 30 3b 0a 20 20 76 Hdr1 *p = 0;. v
142de 6f 69 64 20 2a 70 50 67 3b 0a 0a 20 20 2f 2a 20 oid *pPg;.. /*
142df 54 68 65 20 67 72 6f 75 70 20 6d 75 74 65 78 20 The group mutex
142e0 6d 75 73 74 20 62 65 20 72 65 6c 65 61 73 65 64 must be released
142e1 20 62 65 66 6f 72 65 20 70 63 61 63 68 65 31 41 before pcache1A
142e2 6c 6c 6f 63 28 29 20 69 73 20 63 61 6c 6c 65 64 lloc() is called
142e3 2e 20 54 68 69 73 0a 20 20 2a 2a 20 69 73 20 62 . This. ** is b
142e4 65 63 61 75 73 65 20 69 74 20 6d 61 79 20 63 61 ecause it may ca
142e5 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 ll sqlite3_relea
142e6 73 65 5f 6d 65 6d 6f 72 79 28 29 2c 20 77 68 69 se_memory(), whi
142e7 63 68 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 ch assumes that
142e8 0a 20 20 2a 2a 20 74 68 69 73 20 6d 75 74 65 78 . ** this mutex
142e9 20 69 73 20 6e 6f 74 20 68 65 6c 64 2e 20 2a 2f is not held. */
142ea 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
142eb 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
142ec 61 63 68 65 2d 3e 70 47 72 6f 75 70 2d 3e 6d 75 ache->pGroup->mu
142ed 74 65 78 29 20 29 3b 0a 20 20 70 63 61 63 68 65 tex) );. pcache
142ee 31 4c 65 61 76 65 4d 75 74 65 78 28 70 43 61 63 1LeaveMutex(pCac
142ef 68 65 2d 3e 70 47 72 6f 75 70 29 3b 0a 23 69 66 he->pGroup);.#if
142f0 64 65 66 20 53 51 4c 49 54 45 5f 50 43 41 43 48 def SQLITE_PCACH
142f1 45 5f 53 45 50 41 52 41 54 45 5f 48 45 41 44 45 E_SEPARATE_HEADE
142f2 52 0a 20 20 70 50 67 20 3d 20 70 63 61 63 68 65 R. pPg = pcache
142f3 31 41 6c 6c 6f 63 28 70 43 61 63 68 65 2d 3e 73 1Alloc(pCache->s
142f4 7a 50 61 67 65 29 3b 0a 20 20 70 20 3d 20 73 71 zPage);. p = sq
142f5 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 69 7a 65 lite3Malloc(size
142f6 6f 66 28 50 67 48 64 72 31 29 20 2b 20 70 43 61 of(PgHdr1) + pCa
142f7 63 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 che->szExtra);.
142f8 20 69 66 28 20 21 70 50 67 20 7c 7c 20 21 70 20 if( !pPg || !p
142f9 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 46 72 ){. pcache1Fr
142fa 65 65 28 70 50 67 29 3b 0a 20 20 20 20 73 71 6c ee(pPg);. sql
142fb 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
142fc 20 20 70 50 67 20 3d 20 30 3b 0a 20 20 7d 0a 23 pPg = 0;. }.#
142fd 65 6c 73 65 0a 20 20 70 50 67 20 3d 20 70 63 61 else. pPg = pca
142fe 63 68 65 31 41 6c 6c 6f 63 28 73 69 7a 65 6f 66 che1Alloc(sizeof
142ff 28 50 67 48 64 72 31 29 20 2b 20 70 43 61 63 68 (PgHdr1) + pCach
14300 65 2d 3e 73 7a 50 61 67 65 20 2b 20 70 43 61 63 e->szPage + pCac
14301 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 20 he->szExtra);.
14302 70 20 3d 20 28 50 67 48 64 72 31 20 2a 29 26 28 p = (PgHdr1 *)&(
14303 28 75 38 20 2a 29 70 50 67 29 5b 70 43 61 63 68 (u8 *)pPg)[pCach
14304 65 2d 3e 73 7a 50 61 67 65 5d 3b 0a 23 65 6e 64 e->szPage];.#end
14305 69 66 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 if. pcache1Ente
14306 72 4d 75 74 65 78 28 70 43 61 63 68 65 2d 3e 70 rMutex(pCache->p
14307 47 72 6f 75 70 29 3b 0a 0a 20 20 69 66 28 20 70 Group);.. if( p
14308 50 67 20 29 7b 0a 20 20 20 20 70 2d 3e 70 61 67 Pg ){. p->pag
14309 65 2e 70 42 75 66 20 3d 20 70 50 67 3b 0a 20 20 e.pBuf = pPg;.
1430a 20 20 70 2d 3e 70 61 67 65 2e 70 45 78 74 72 61 p->page.pExtra
1430b 20 3d 20 26 70 5b 31 5d 3b 0a 20 20 20 20 69 66 = &p[1];. if
1430c 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 ( pCache->bPurge
1430d 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43 able ){. pC
1430e 61 63 68 65 2d 3e 70 47 72 6f 75 70 2d 3e 6e 43 ache->pGroup->nC
1430f 75 72 72 65 6e 74 50 61 67 65 2b 2b 3b 0a 20 20 urrentPage++;.
14310 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 70 }. return p
14311 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 ;. }. return 0
14312 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
14313 61 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 6c a page object al
14314 6c 6f 63 61 74 65 64 20 62 79 20 70 63 61 63 68 located by pcach
14315 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a 2a e1AllocPage()..*
14316 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 *.** The pointer
14317 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 is allowed to b
14318 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 e NULL, which is
14319 20 70 72 75 64 65 6e 74 2e 20 20 42 75 74 20 69 prudent. But i
1431a 74 20 74 75 72 6e 73 20 6f 75 74 0a 2a 2a 20 74 t turns out.** t
1431b 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 hat the current
1431c 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68 implementation h
1431d 61 70 70 65 6e 73 20 74 6f 20 6e 65 76 65 72 20 appens to never
1431e 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e call this routin
1431f 65 0a 2a 2a 20 77 69 74 68 20 61 20 4e 55 4c 4c e.** with a NULL
14320 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 77 65 20 pointer, so we
14321 6d 61 72 6b 20 74 68 65 20 4e 55 4c 4c 20 74 65 mark the NULL te
14322 73 74 20 77 69 74 68 20 41 4c 57 41 59 53 28 29 st with ALWAYS()
14323 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
14324 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 pcache1FreePage
14325 28 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 69 (PgHdr1 *p){. i
14326 66 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b 0a f( ALWAYS(p) ){.
14327 20 20 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 PCache1 *pCa
14328 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b che = p->pCache;
14329 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
1432a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1432b 70 2d 3e 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 p->pCache->pGrou
1432c 70 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 p->mutex) );.
1432d 20 70 63 61 63 68 65 31 46 72 65 65 28 70 2d 3e pcache1Free(p->
1432e 70 61 67 65 2e 70 42 75 66 29 3b 0a 23 69 66 64 page.pBuf);.#ifd
1432f 65 66 20 53 51 4c 49 54 45 5f 50 43 41 43 48 45 ef SQLITE_PCACHE
14330 5f 53 45 50 41 52 41 54 45 5f 48 45 41 44 45 52 _SEPARATE_HEADER
14331 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
14332 65 28 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 e(p);.#endif.
14333 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 if( pCache->bPu
14334 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 rgeable ){.
14335 20 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70 2d pCache->pGroup-
14336 3e 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b >nCurrentPage--;
14337 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
14338 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 .** Malloc funct
14339 69 6f 6e 20 75 73 65 64 20 62 79 20 53 51 4c 69 ion used by SQLi
1433a 74 65 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 te to obtain spa
1433b 63 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 ce from the buff
1433c 65 72 20 63 6f 6e 66 69 67 75 72 65 64 0a 2a 2a er configured.**
1433d 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 using sqlite3_c
1433e 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e onfig(SQLITE_CON
1433f 46 49 47 5f 50 41 47 45 43 41 43 48 45 29 20 6f FIG_PAGECACHE) o
14340 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 73 75 63 ption. If no suc
14341 68 20 62 75 66 66 65 72 0a 2a 2a 20 65 78 69 73 h buffer.** exis
14342 74 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f ts, this functio
14343 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f 20 n falls back to
14344 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e sqlite3Malloc().
14345 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14346 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
14347 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 20 73 PageMalloc(int s
14348 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 63 61 z){. return pca
14349 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b 0a 7d che1Alloc(sz);.}
1434a 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 ../*.** Free an
1434b 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 allocated buffer
1434c 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
1434d 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 qlite3PageMalloc
1434e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ()..*/.SQLITE_PR
1434f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
14350 65 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 20 e3PageFree(void
14351 2a 70 29 7b 0a 20 20 70 63 61 63 68 65 31 46 72 *p){. pcache1Fr
14352 65 65 28 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a ee(p);.}.../*.**
14353 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
14354 69 74 20 64 65 73 69 72 61 62 6c 65 20 74 6f 20 it desirable to
14355 61 76 6f 69 64 20 61 6c 6c 6f 63 61 74 69 6e 67 avoid allocating
14356 20 61 20 6e 65 77 20 70 61 67 65 20 63 61 63 68 a new page cach
14357 65 0a 2a 2a 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a e.** entry..**.*
14358 2a 20 49 66 20 6d 65 6d 6f 72 79 20 77 61 73 20 * If memory was
14359 61 6c 6c 6f 63 61 74 65 64 20 73 70 65 63 69 66 allocated specif
1435a 69 63 61 6c 6c 79 20 74 6f 20 74 68 65 20 70 61 ically to the pa
1435b 67 65 20 63 61 63 68 65 20 75 73 69 6e 67 0a 2a ge cache using.*
1435c 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f * SQLITE_CONFIG_
1435d 50 41 47 45 43 41 43 48 45 20 62 75 74 20 74 68 PAGECACHE but th
1435e 61 74 20 6d 65 6d 6f 72 79 20 68 61 73 20 61 6c at memory has al
1435f 6c 20 62 65 65 6e 20 75 73 65 64 2c 20 74 68 65 l been used, the
14360 6e 0a 2a 2a 20 69 74 20 69 73 20 64 65 73 69 72 n.** it is desir
14361 61 62 6c 65 20 74 6f 20 61 76 6f 69 64 20 61 6c able to avoid al
14362 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 70 locating a new p
14363 61 67 65 20 63 61 63 68 65 20 65 6e 74 72 79 20 age cache entry
14364 62 65 63 61 75 73 65 0a 2a 2a 20 70 72 65 73 75 because.** presu
14365 6d 61 62 6c 79 20 53 51 4c 49 54 45 5f 43 4f 4e mably SQLITE_CON
14366 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 77 61 FIG_PAGECACHE wa
14367 73 20 73 75 70 70 6f 73 65 20 74 6f 20 62 65 20 s suppose to be
14368 73 75 66 66 69 63 69 65 6e 74 0a 2a 2a 20 66 6f sufficient.** fo
14369 72 20 61 6c 6c 20 70 61 67 65 20 63 61 63 68 65 r all page cache
1436a 20 6e 65 65 64 73 20 61 6e 64 20 77 65 20 73 68 needs and we sh
1436b 6f 75 6c 64 20 6e 6f 74 20 6e 65 65 64 20 74 6f ould not need to
1436c 20 73 70 69 6c 6c 20 74 68 65 0a 2a 2a 20 61 6c spill the.** al
1436d 6c 6f 63 61 74 69 6f 6e 20 6f 6e 74 6f 20 74 68 location onto th
1436e 65 20 68 65 61 70 2e 0a 2a 2a 0a 2a 2a 20 4f 72 e heap..**.** Or
1436f 2c 20 74 68 65 20 68 65 61 70 20 69 73 20 75 73 , the heap is us
14370 65 64 20 66 6f 72 20 61 6c 6c 20 70 61 67 65 20 ed for all page
14371 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 62 75 74 cache memory but
14372 20 74 68 65 20 68 65 61 70 20 69 73 0a 2a 2a 20 the heap is.**
14373 75 6e 64 65 72 20 6d 65 6d 6f 72 79 20 70 72 65 under memory pre
14374 73 73 75 72 65 2c 20 74 68 65 6e 20 61 67 61 69 ssure, then agai
14375 6e 20 69 74 20 69 73 20 64 65 73 69 72 61 62 6c n it is desirabl
14376 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a 20 61 6c e to avoid.** al
14377 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 70 locating a new p
14378 61 67 65 20 63 61 63 68 65 20 65 6e 74 72 79 20 age cache entry
14379 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 in order to avoi
1437a 64 20 73 74 72 65 73 73 69 6e 67 0a 2a 2a 20 74 d stressing.** t
1437b 68 65 20 68 65 61 70 20 65 76 65 6e 20 66 75 72 he heap even fur
1437c 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ther..*/.static
1437d 69 6e 74 20 70 63 61 63 68 65 31 55 6e 64 65 72 int pcache1Under
1437e 4d 65 6d 6f 72 79 50 72 65 73 73 75 72 65 28 50 MemoryPressure(P
1437f 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 29 7b Cache1 *pCache){
14380 0a 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 6e . if( pcache1.n
14381 53 6c 6f 74 20 26 26 20 28 70 43 61 63 68 65 2d Slot && (pCache-
14382 3e 73 7a 50 61 67 65 2b 70 43 61 63 68 65 2d 3e >szPage+pCache->
14383 73 7a 45 78 74 72 61 29 3c 3d 70 63 61 63 68 65 szExtra)<=pcache
14384 31 2e 73 7a 53 6c 6f 74 20 29 7b 0a 20 20 20 20 1.szSlot ){.
14385 72 65 74 75 72 6e 20 70 63 61 63 68 65 31 2e 62 return pcache1.b
14386 55 6e 64 65 72 50 72 65 73 73 75 72 65 3b 0a 20 UnderPressure;.
14387 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
14388 72 6e 20 73 71 6c 69 74 65 33 48 65 61 70 4e 65 rn sqlite3HeapNe
14389 61 72 6c 79 46 75 6c 6c 28 29 3b 0a 20 20 7d 0a arlyFull();. }.
1438a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
1438b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1438c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1438d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1438e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1438f 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 **/./******** Ge
14390 6e 65 72 61 6c 20 49 6d 70 6c 65 6d 65 6e 74 61 neral Implementa
14391 74 69 6f 6e 20 46 75 6e 63 74 69 6f 6e 73 20 2a tion Functions *
14392 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14393 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14394 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ***/../*.** This
14395 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
14396 64 20 74 6f 20 72 65 73 69 7a 65 20 74 68 65 20 d to resize the
14397 68 61 73 68 20 74 61 62 6c 65 20 75 73 65 64 20 hash table used
14398 62 79 20 74 68 65 20 63 61 63 68 65 20 70 61 73 by the cache pas
14399 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 sed.** as the fi
1439a 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a rst argument..**
1439b 0a 2a 2a 20 54 68 65 20 50 43 61 63 68 65 20 6d .** The PCache m
1439c 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c utex must be hel
1439d 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 d when this func
1439e 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a tion is called..
1439f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63 */.static int pc
143a0 61 63 68 65 31 52 65 73 69 7a 65 48 61 73 68 28 ache1ResizeHash(
143a1 50 43 61 63 68 65 31 20 2a 70 29 7b 0a 20 20 50 PCache1 *p){. P
143a2 67 48 64 72 31 20 2a 2a 61 70 4e 65 77 3b 0a 20 gHdr1 **apNew;.
143a3 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4e unsigned int nN
143a4 65 77 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 ew;. unsigned i
143a5 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 nt i;.. assert(
143a6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
143a7 65 6c 64 28 70 2d 3e 70 47 72 6f 75 70 2d 3e 6d eld(p->pGroup->m
143a8 75 74 65 78 29 20 29 3b 0a 0a 20 20 6e 4e 65 77 utex) );.. nNew
143a9 20 3d 20 70 2d 3e 6e 48 61 73 68 2a 32 3b 0a 20 = p->nHash*2;.
143aa 20 69 66 28 20 6e 4e 65 77 3c 32 35 36 20 29 7b if( nNew<256 ){
143ab 0a 20 20 20 20 6e 4e 65 77 20 3d 20 32 35 36 3b . nNew = 256;
143ac 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c . }.. pcache1L
143ad 65 61 76 65 4d 75 74 65 78 28 70 2d 3e 70 47 72 eaveMutex(p->pGr
143ae 6f 75 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e oup);. if( p->n
143af 48 61 73 68 20 29 7b 20 73 71 6c 69 74 65 33 42 Hash ){ sqlite3B
143b0 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
143b1 28 29 3b 20 7d 0a 20 20 61 70 4e 65 77 20 3d 20 (); }. apNew =
143b2 28 50 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74 (PgHdr1 **)sqlit
143b3 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a e3MallocZero(siz
143b4 65 6f 66 28 50 67 48 64 72 31 20 2a 29 2a 6e 4e eof(PgHdr1 *)*nN
143b5 65 77 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 ew);. if( p->nH
143b6 61 73 68 20 29 7b 20 73 71 6c 69 74 65 33 45 6e ash ){ sqlite3En
143b7 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
143b8 20 7d 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 }. pcache1Ente
143b9 72 4d 75 74 65 78 28 70 2d 3e 70 47 72 6f 75 70 rMutex(p->pGroup
143ba 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77 20 29 );. if( apNew )
143bb 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 {. for(i=0; i
143bc 3c 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b <p->nHash; i++){
143bd 0a 20 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 . PgHdr1 *p
143be 50 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48 64 Page;. PgHd
143bf 72 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 r1 *pNext = p->a
143c0 70 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 pHash[i];.
143c1 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 while( (pPage =
143c2 70 4e 65 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 pNext)!=0 ){.
143c3 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e unsigned in
143c4 74 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 t h = pPage->iKe
143c5 79 20 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 y % nNew;.
143c6 20 20 70 4e 65 78 74 20 3d 20 70 50 61 67 65 2d pNext = pPage-
143c7 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 >pNext;.
143c8 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 pPage->pNext = a
143c9 70 4e 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 pNew[h];.
143ca 20 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 apNew[h] = pPag
143cb 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d e;. }. }
143cc 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
143cd 65 28 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 e(p->apHash);.
143ce 20 20 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 p->apHash = ap
143cf 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 New;. p->nHas
143d0 68 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 h = nNew;. }..
143d1 20 72 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 return (p->apHa
143d2 73 68 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a sh ? SQLITE_OK :
143d3 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a SQLITE_NOMEM);.
143d4 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
143d5 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 nction is used i
143d6 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d nternally to rem
143d7 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 50 61 ove the page pPa
143d8 67 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 ge from the .**
143d9 50 47 72 6f 75 70 20 4c 52 55 20 6c 69 73 74 2c PGroup LRU list,
143da 20 69 66 20 69 73 20 70 61 72 74 20 6f 66 20 69 if is part of i
143db 74 2e 20 49 66 20 70 50 61 67 65 20 69 73 20 6e t. If pPage is n
143dc 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 50 ot part of the P
143dd 47 72 6f 75 70 0a 2a 2a 20 4c 52 55 20 6c 69 73 Group.** LRU lis
143de 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e t, then this fun
143df 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
143e0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 47 72 6f ..**.** The PGro
143e1 75 70 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 up mutex must be
143e2 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 held when this
143e3 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
143e4 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 50 61 ed..**.** If pPa
143e5 67 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 ge is NULL then
143e6 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
143e7 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 a no-op..*/.stat
143e8 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 50 ic void pcache1P
143e9 69 6e 50 61 67 65 28 50 67 48 64 72 31 20 2a 70 inPage(PgHdr1 *p
143ea 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 31 Page){. PCache1
143eb 20 2a 70 43 61 63 68 65 3b 0a 20 20 50 47 72 6f *pCache;. PGro
143ec 75 70 20 2a 70 47 72 6f 75 70 3b 0a 0a 20 20 69 up *pGroup;.. i
143ed 66 28 20 70 50 61 67 65 3d 3d 30 20 29 20 72 65 f( pPage==0 ) re
143ee 74 75 72 6e 3b 0a 20 20 70 43 61 63 68 65 20 3d turn;. pCache =
143ef 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a pPage->pCache;.
143f0 20 20 70 47 72 6f 75 70 20 3d 20 70 43 61 63 68 pGroup = pCach
143f1 65 2d 3e 70 47 72 6f 75 70 3b 0a 20 20 61 73 73 e->pGroup;. ass
143f2 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
143f3 65 78 5f 68 65 6c 64 28 70 47 72 6f 75 70 2d 3e ex_held(pGroup->
143f4 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
143f5 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 pPage->pLruNext
143f6 7c 7c 20 70 50 61 67 65 3d 3d 70 47 72 6f 75 70 || pPage==pGroup
143f7 2d 3e 70 4c 72 75 54 61 69 6c 20 29 7b 0a 20 20 ->pLruTail ){.
143f8 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72 if( pPage->pLr
143f9 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 uPrev ){. p
143fa 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d 3e Page->pLruPrev->
143fb 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67 65 pLruNext = pPage
143fc 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 ->pLruNext;.
143fd 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d }. if( pPage-
143fe 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 20 >pLruNext ){.
143ff 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 pPage->pLruNe
14400 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 xt->pLruPrev = p
14401 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0a Page->pLruPrev;.
14402 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 47 }. if( pG
14403 72 6f 75 70 2d 3e 70 4c 72 75 48 65 61 64 3d 3d roup->pLruHead==
14404 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 pPage ){. p
14405 47 72 6f 75 70 2d 3e 70 4c 72 75 48 65 61 64 20 Group->pLruHead
14406 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 = pPage->pLruNex
14407 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 t;. }. if(
14408 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 54 61 69 pGroup->pLruTai
14409 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 l==pPage ){.
1440a 20 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 54 61 pGroup->pLruTa
1440b 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 il = pPage->pLru
1440c 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Prev;. }.
1440d 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 pPage->pLruNext
1440e 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e = 0;. pPage->
1440f 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 pLruPrev = 0;.
14410 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d pPage->pCache-
14411 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b 0a >nRecyclable--;.
14412 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 }.}.../*.** Re
14413 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 75 move the page su
14414 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 pplied as an arg
14415 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 68 ument from the h
14416 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28 50 ash table .** (P
14417 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73 74 Cache1.apHash st
14418 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69 74 ructure) that it
14419 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74 is currently st
1441a 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 ored in..**.** T
1441b 68 65 20 50 47 72 6f 75 70 20 6d 75 74 65 78 20 he PGroup mutex
1441c 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 must be held whe
1441d 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
1441e 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 is called..*/.st
1441f 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
14420 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 1RemoveFromHash(
14421 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a PgHdr1 *pPage){.
14422 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
14423 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 ;. PCache1 *pCa
14424 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 che = pPage->pCa
14425 63 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a che;. PgHdr1 **
14426 70 70 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 pp;.. assert( s
14427 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14428 64 28 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70 d(pCache->pGroup
14429 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 68 20 ->mutex) );. h
1442a 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 25 20 = pPage->iKey %
1442b 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 pCache->nHash;.
1442c 20 66 6f 72 28 70 70 3d 26 70 43 61 63 68 65 2d for(pp=&pCache-
1442d 3e 61 70 48 61 73 68 5b 68 5d 3b 20 28 2a 70 70 >apHash[h]; (*pp
1442e 29 21 3d 70 50 61 67 65 3b 20 70 70 3d 26 28 2a )!=pPage; pp=&(*
1442f 70 70 29 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 2a pp)->pNext);. *
14430 70 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78 pp = (*pp)->pNex
14431 74 3b 0a 0a 20 20 70 43 61 63 68 65 2d 3e 6e 50 t;.. pCache->nP
14432 61 67 65 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 age--;.}../*.**
14433 49 66 20 74 68 65 72 65 20 61 72 65 20 63 75 72 If there are cur
14434 72 65 6e 74 6c 79 20 6d 6f 72 65 20 74 68 61 6e rently more than
14435 20 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 20 nMaxPage pages
14436 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a 2a allocated, try.*
14437 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 67 * to recycle pag
14438 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 es to reduce the
14439 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 65 number allocate
1443a 64 20 74 6f 20 6e 4d 61 78 50 61 67 65 2e 0a 2a d to nMaxPage..*
1443b 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
1443c 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 ache1EnforceMaxP
1443d 61 67 65 28 50 47 72 6f 75 70 20 2a 70 47 72 6f age(PGroup *pGro
1443e 75 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 up){. assert( s
1443f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14440 64 28 70 47 72 6f 75 70 2d 3e 6d 75 74 65 78 29 d(pGroup->mutex)
14441 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 47 72 );. while( pGr
14442 6f 75 70 2d 3e 6e 43 75 72 72 65 6e 74 50 61 67 oup->nCurrentPag
14443 65 3e 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61 e>pGroup->nMaxPa
14444 67 65 20 26 26 20 70 47 72 6f 75 70 2d 3e 70 4c ge && pGroup->pL
14445 72 75 54 61 69 6c 20 29 7b 0a 20 20 20 20 50 67 ruTail ){. Pg
14446 48 64 72 31 20 2a 70 20 3d 20 70 47 72 6f 75 70 Hdr1 *p = pGroup
14447 2d 3e 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 ->pLruTail;.
14448 61 73 73 65 72 74 28 20 70 2d 3e 70 43 61 63 68 assert( p->pCach
14449 65 2d 3e 70 47 72 6f 75 70 3d 3d 70 47 72 6f 75 e->pGroup==pGrou
1444a 70 20 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 p );. pcache1
1444b 50 69 6e 50 61 67 65 28 70 29 3b 0a 20 20 20 20 PinPage(p);.
1444c 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f pcache1RemoveFro
1444d 6d 48 61 73 68 28 70 29 3b 0a 20 20 20 20 70 63 mHash(p);. pc
1444e 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 29 ache1FreePage(p)
1444f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ;. }.}../*.** D
14450 69 73 63 61 72 64 20 61 6c 6c 20 70 61 67 65 73 iscard all pages
14451 20 66 72 6f 6d 20 63 61 63 68 65 20 70 43 61 63 from cache pCac
14452 68 65 20 77 69 74 68 20 61 20 70 61 67 65 20 6e he with a page n
14453 75 6d 62 65 72 20 28 6b 65 79 20 76 61 6c 75 65 umber (key value
14454 29 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 ) .** greater th
14455 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 69 an or equal to i
14456 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e 65 Limit. Any pinne
14457 64 20 70 61 67 65 73 20 74 68 61 74 20 6d 65 65 d pages that mee
14458 74 20 74 68 69 73 20 0a 2a 2a 20 63 72 69 74 65 t this .** crite
14459 72 69 61 20 61 72 65 20 75 6e 70 69 6e 6e 65 64 ria are unpinned
1445a 20 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 before they are
1445b 20 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a discarded..**.*
1445c 2a 20 54 68 65 20 50 43 61 63 68 65 20 6d 75 74 * The PCache mut
1445d 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 ex must be held
1445e 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
1445f 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f on is called..*/
14460 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
14461 63 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 che1TruncateUnsa
14462 66 65 28 0a 20 20 50 43 61 63 68 65 31 20 2a 70 fe(. PCache1 *p
14463 43 61 63 68 65 2c 20 20 20 20 20 20 20 20 20 20 Cache,
14464 20 20 20 2f 2a 20 54 68 65 20 63 61 63 68 65 20 /* The cache
14465 74 6f 20 74 72 75 6e 63 61 74 65 20 2a 2f 0a 20 to truncate */.
14466 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c unsigned int iL
14467 69 6d 69 74 20 20 20 20 20 20 20 20 20 20 2f 2a imit /*
14468 20 44 72 6f 70 20 70 61 67 65 73 20 77 69 74 68 Drop pages with
14469 20 74 68 69 73 20 70 67 6e 6f 20 6f 72 20 6c 61 this pgno or la
1446a 72 67 65 72 20 2a 2f 0a 29 7b 0a 20 20 54 45 53 rger */.){. TES
1446b 54 4f 4e 4c 59 28 20 75 6e 73 69 67 6e 65 64 20 TONLY( unsigned
1446c 69 6e 74 20 6e 50 61 67 65 20 3d 20 30 3b 20 29 int nPage = 0; )
1446d 20 20 2f 2a 20 54 6f 20 61 73 73 65 72 74 20 70 /* To assert p
1446e 43 61 63 68 65 2d 3e 6e 50 61 67 65 20 69 73 20 Cache->nPage is
1446f 63 6f 72 72 65 63 74 20 2a 2f 0a 20 20 75 6e 73 correct */. uns
14470 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 61 igned int h;. a
14471 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
14472 75 74 65 78 5f 68 65 6c 64 28 70 43 61 63 68 65 utex_held(pCache
14473 2d 3e 70 47 72 6f 75 70 2d 3e 6d 75 74 65 78 29 ->pGroup->mutex)
14474 20 29 3b 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68 );. for(h=0; h
14475 3c 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20 <pCache->nHash;
14476 68 2b 2b 29 7b 0a 20 20 20 20 50 67 48 64 72 31 h++){. PgHdr1
14477 20 2a 2a 70 70 20 3d 20 26 70 43 61 63 68 65 2d **pp = &pCache-
14478 3e 61 70 48 61 73 68 5b 68 5d 3b 20 0a 20 20 20 >apHash[h]; .
14479 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0a PgHdr1 *pPage;.
1447a 20 20 20 20 77 68 69 6c 65 28 20 28 70 50 61 67 while( (pPag
1447b 65 20 3d 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20 e = *pp)!=0 ){.
1447c 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e if( pPage->
1447d 69 4b 65 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a iKey>=iLimit ){.
1447e 20 20 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e pCache->
1447f 6e 50 61 67 65 2d 2d 3b 0a 20 20 20 20 20 20 20 nPage--;.
14480 20 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e *pp = pPage->pN
14481 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 63 61 ext;. pca
14482 63 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67 che1PinPage(pPag
14483 65 29 3b 0a 20 20 20 20 20 20 20 20 70 63 61 63 e);. pcac
14484 68 65 31 46 72 65 65 50 61 67 65 28 70 50 61 67 he1FreePage(pPag
14485 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b e);. }else{
14486 0a 20 20 20 20 20 20 20 20 70 70 20 3d 20 26 70 . pp = &p
14487 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Page->pNext;.
14488 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 6e TESTONLY( n
14489 50 61 67 65 2b 2b 3b 20 29 0a 20 20 20 20 20 20 Page++; ).
1448a 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 }. }. }. as
1448b 73 65 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 sert( pCache->nP
1448c 61 67 65 3d 3d 6e 50 61 67 65 20 29 3b 0a 7d 0a age==nPage );.}.
1448d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1448e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1448f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14491 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14492 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 /./******** sqli
14493 74 65 33 5f 70 63 61 63 68 65 20 4d 65 74 68 6f te3_pcache Metho
14494 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ds *************
14495 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14496 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14497 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d */../*.** Implem
14498 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
14499 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 sqlite3_pcache.x
1449a 49 6e 69 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a Init method..*/.
1449b 73 74 61 74 69 63 20 69 6e 74 20 70 63 61 63 68 static int pcach
1449c 65 31 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 e1Init(void *Not
1449d 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
1449e 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
1449f 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 d);. assert( pc
144a0 61 63 68 65 31 2e 69 73 49 6e 69 74 3d 3d 30 20 ache1.isInit==0
144a1 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 63 61 );. memset(&pca
144a2 63 68 65 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 che1, 0, sizeof(
144a3 70 63 61 63 68 65 31 29 29 3b 0a 20 20 69 66 28 pcache1));. if(
144a4 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
144a5 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 nfig.bCoreMutex
144a6 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 67 ){. pcache1.g
144a7 72 70 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 rp.mutex = sqlit
144a8 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 e3_mutex_alloc(S
144a9 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
144aa 49 43 5f 4c 52 55 29 3b 0a 20 20 20 20 70 63 61 IC_LRU);. pca
144ab 63 68 65 31 2e 6d 75 74 65 78 20 3d 20 73 71 6c che1.mutex = sql
144ac 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
144ad 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
144ae 41 54 49 43 5f 50 4d 45 4d 29 3b 0a 20 20 7d 0a ATIC_PMEM);. }.
144af 20 20 70 63 61 63 68 65 31 2e 67 72 70 2e 6d 78 pcache1.grp.mx
144b0 50 69 6e 6e 65 64 20 3d 20 31 30 3b 0a 20 20 70 Pinned = 10;. p
144b1 63 61 63 68 65 31 2e 69 73 49 6e 69 74 20 3d 20 cache1.isInit =
144b2 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 1;. return SQLI
144b3 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
144b4 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
144b5 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 f the sqlite3_pc
144b6 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e 20 6d ache.xShutdown m
144b7 65 74 68 6f 64 2e 0a 2a 2a 20 4e 6f 74 65 20 74 ethod..** Note t
144b8 68 61 74 20 74 68 65 20 73 74 61 74 69 63 20 6d hat the static m
144b9 75 74 65 78 20 61 6c 6c 6f 63 61 74 65 64 20 69 utex allocated i
144ba 6e 20 78 49 6e 69 74 20 64 6f 65 73 20 0a 2a 2a n xInit does .**
144bb 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 not need to be
144bc 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 freed..*/.static
144bd 20 76 6f 69 64 20 70 63 61 63 68 65 31 53 68 75 void pcache1Shu
144be 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 tdown(void *NotU
144bf 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
144c0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
144c1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 );. assert( pca
144c2 63 68 65 31 2e 69 73 49 6e 69 74 21 3d 30 20 29 che1.isInit!=0 )
144c3 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 63 61 63 ;. memset(&pcac
144c4 68 65 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 he1, 0, sizeof(p
144c5 63 61 63 68 65 31 29 29 3b 0a 7d 0a 0a 2f 2a 0a cache1));.}../*.
144c6 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
144c7 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 n of the sqlite3
144c8 5f 70 63 61 63 68 65 2e 78 43 72 65 61 74 65 20 _pcache.xCreate
144c9 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c method..**.** Al
144ca 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 63 61 63 locate a new cac
144cb 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 he..*/.static sq
144cc 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 63 lite3_pcache *pc
144cd 61 63 68 65 31 43 72 65 61 74 65 28 69 6e 74 20 ache1Create(int
144ce 73 7a 50 61 67 65 2c 20 69 6e 74 20 73 7a 45 78 szPage, int szEx
144cf 74 72 61 2c 20 69 6e 74 20 62 50 75 72 67 65 61 tra, int bPurgea
144d0 62 6c 65 29 7b 0a 20 20 50 43 61 63 68 65 31 20 ble){. PCache1
144d1 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 2f 2a *pCache; /*
144d2 20 54 68 65 20 6e 65 77 6c 79 20 63 72 65 61 74 The newly creat
144d3 65 64 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f ed page cache */
144d4 0a 20 20 50 47 72 6f 75 70 20 2a 70 47 72 6f 75 . PGroup *pGrou
144d5 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 p; /* The
144d6 67 72 6f 75 70 20 74 68 65 20 6e 65 77 20 70 61 group the new pa
144d7 67 65 20 63 61 63 68 65 20 77 69 6c 6c 20 62 65 ge cache will be
144d8 6c 6f 6e 67 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 long to */. int
144d9 20 73 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 sz;
144da 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 6d /* Bytes of m
144db 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 20 74 emory required t
144dc 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 6e o allocate the n
144dd 65 77 20 63 61 63 68 65 20 2a 2f 0a 0a 20 20 2f ew cache */.. /
144de 2a 0a 20 20 2a 2a 20 54 68 65 20 73 65 70 65 72 *. ** The seper
144df 61 74 65 43 61 63 68 65 20 76 61 72 69 61 62 6c ateCache variabl
144e0 65 20 69 73 20 74 72 75 65 20 69 66 20 65 61 63 e is true if eac
144e1 68 20 50 43 61 63 68 65 20 68 61 73 20 69 74 73 h PCache has its
144e2 20 6f 77 6e 20 70 72 69 76 61 74 65 0a 20 20 2a own private. *
144e3 2a 20 50 47 72 6f 75 70 2e 20 20 49 6e 20 6f 74 * PGroup. In ot
144e4 68 65 72 20 77 6f 72 64 73 2c 20 73 65 70 61 72 her words, separ
144e5 61 74 65 43 61 63 68 65 20 69 73 20 74 72 75 65 ateCache is true
144e6 20 66 6f 72 20 6d 6f 64 65 20 28 31 29 20 77 68 for mode (1) wh
144e7 65 72 65 20 6e 6f 0a 20 20 2a 2a 20 6d 75 74 65 ere no. ** mute
144e8 78 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64 xing is required
144e9 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 .. **. ** *
144ea 20 41 6c 77 61 79 73 20 75 73 65 20 61 20 75 6e Always use a un
144eb 69 66 69 65 64 20 63 61 63 68 65 20 28 6d 6f 64 ified cache (mod
144ec 65 2d 32 29 20 69 66 20 45 4e 41 42 4c 45 5f 4d e-2) if ENABLE_M
144ed 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 EMORY_MANAGEMENT
144ee 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 20 . **. ** *
144ef 41 6c 77 61 79 73 20 75 73 65 20 61 20 75 6e 69 Always use a uni
144f0 66 69 65 64 20 63 61 63 68 65 20 69 6e 20 73 69 fied cache in si
144f1 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70 ngle-threaded ap
144f2 70 6c 69 63 61 74 69 6f 6e 73 0a 20 20 2a 2a 0a plications. **.
144f3 20 20 2a 2a 20 20 20 2a 20 20 4f 74 68 65 72 77 ** * Otherw
144f4 69 73 65 20 28 69 66 20 6d 75 6c 74 69 2d 74 68 ise (if multi-th
144f5 72 65 61 64 65 64 20 61 6e 64 20 45 4e 41 42 4c readed and ENABL
144f6 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d E_MEMORY_MANAGEM
144f7 45 4e 54 20 69 73 20 6f 66 66 29 0a 20 20 2a 2a ENT is off). **
144f8 20 20 20 20 20 20 75 73 65 20 73 65 70 61 72 61 use separa
144f9 74 65 20 63 61 63 68 65 73 20 28 6d 6f 64 65 2d te caches (mode-
144fa 31 29 0a 20 20 2a 2f 0a 23 69 66 20 64 65 66 69 1). */.#if defi
144fb 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c ned(SQLITE_ENABL
144fc 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d E_MEMORY_MANAGEM
144fd 45 4e 54 29 20 7c 7c 20 53 51 4c 49 54 45 5f 54 ENT) || SQLITE_T
144fe 48 52 45 41 44 53 41 46 45 3d 3d 30 0a 20 20 63 HREADSAFE==0. c
144ff 6f 6e 73 74 20 69 6e 74 20 73 65 70 61 72 61 74 onst int separat
14500 65 43 61 63 68 65 20 3d 20 30 3b 0a 23 65 6c 73 eCache = 0;.#els
14501 65 0a 20 20 69 6e 74 20 73 65 70 61 72 61 74 65 e. int separate
14502 43 61 63 68 65 20 3d 20 73 71 6c 69 74 65 33 47 Cache = sqlite3G
14503 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 lobalConfig.bCor
14504 65 4d 75 74 65 78 3e 30 3b 0a 23 65 6e 64 69 66 eMutex>0;.#endif
14505 0a 0a 20 20 61 73 73 65 72 74 28 20 28 73 7a 50 .. assert( (szP
14506 61 67 65 20 26 20 28 73 7a 50 61 67 65 2d 31 29 age & (szPage-1)
14507 29 3d 3d 30 20 26 26 20 73 7a 50 61 67 65 3e 3d )==0 && szPage>=
14508 35 31 32 20 26 26 20 73 7a 50 61 67 65 3c 3d 36 512 && szPage<=6
14509 35 35 33 36 20 29 3b 0a 20 20 61 73 73 65 72 74 5536 );. assert
1450a 28 20 73 7a 45 78 74 72 61 20 3c 20 33 30 30 20 ( szExtra < 300
1450b 29 3b 0a 0a 20 20 73 7a 20 3d 20 73 69 7a 65 6f );.. sz = sizeo
1450c 66 28 50 43 61 63 68 65 31 29 20 2b 20 73 69 7a f(PCache1) + siz
1450d 65 6f 66 28 50 47 72 6f 75 70 29 2a 73 65 70 61 eof(PGroup)*sepa
1450e 72 61 74 65 43 61 63 68 65 3b 0a 20 20 70 43 61 rateCache;. pCa
1450f 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a che = (PCache1 *
14510 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 )sqlite3MallocZe
14511 72 6f 28 73 7a 29 3b 0a 20 20 69 66 28 20 70 43 ro(sz);. if( pC
14512 61 63 68 65 20 29 7b 0a 20 20 20 20 69 66 28 20 ache ){. if(
14513 73 65 70 61 72 61 74 65 43 61 63 68 65 20 29 7b separateCache ){
14514 0a 20 20 20 20 20 20 70 47 72 6f 75 70 20 3d 20 . pGroup =
14515 28 50 47 72 6f 75 70 2a 29 26 70 43 61 63 68 65 (PGroup*)&pCache
14516 5b 31 5d 3b 0a 20 20 20 20 20 20 70 47 72 6f 75 [1];. pGrou
14517 70 2d 3e 6d 78 50 69 6e 6e 65 64 20 3d 20 31 30 p->mxPinned = 10
14518 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
14519 20 20 20 70 47 72 6f 75 70 20 3d 20 26 70 63 61 pGroup = &pca
1451a 63 68 65 31 2e 67 72 70 3b 0a 20 20 20 20 7d 0a che1.grp;. }.
1451b 20 20 20 20 70 43 61 63 68 65 2d 3e 70 47 72 6f pCache->pGro
1451c 75 70 20 3d 20 70 47 72 6f 75 70 3b 0a 20 20 20 up = pGroup;.
1451d 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 pCache->szPage
1451e 3d 20 73 7a 50 61 67 65 3b 0a 20 20 20 20 70 43 = szPage;. pC
1451f 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 3d 20 ache->szExtra =
14520 73 7a 45 78 74 72 61 3b 0a 20 20 20 20 70 43 61 szExtra;. pCa
14521 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 che->bPurgeable
14522 3d 20 28 62 50 75 72 67 65 61 62 6c 65 20 3f 20 = (bPurgeable ?
14523 31 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20 1 : 0);. if(
14524 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 bPurgeable ){.
14525 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e pCache->nMin
14526 20 3d 20 31 30 3b 0a 20 20 20 20 20 20 70 63 61 = 10;. pca
14527 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 70 che1EnterMutex(p
14528 47 72 6f 75 70 29 3b 0a 20 20 20 20 20 20 70 47 Group);. pG
14529 72 6f 75 70 2d 3e 6e 4d 69 6e 50 61 67 65 20 2b roup->nMinPage +
1452a 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a = pCache->nMin;.
1452b 20 20 20 20 20 20 70 47 72 6f 75 70 2d 3e 6d 78 pGroup->mx
1452c 50 69 6e 6e 65 64 20 3d 20 70 47 72 6f 75 70 2d Pinned = pGroup-
1452d 3e 6e 4d 61 78 50 61 67 65 20 2b 20 31 30 20 2d >nMaxPage + 10 -
1452e 20 70 47 72 6f 75 70 2d 3e 6e 4d 69 6e 50 61 67 pGroup->nMinPag
1452f 65 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 e;. pcache1
14530 4c 65 61 76 65 4d 75 74 65 78 28 70 47 72 6f 75 LeaveMutex(pGrou
14531 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 p);. }. }.
14532 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f return (sqlite3_
14533 70 63 61 63 68 65 20 2a 29 70 43 61 63 68 65 3b pcache *)pCache;
14534 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
14535 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
14536 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 sqlite3_pcache.x
14537 43 61 63 68 65 73 69 7a 65 20 6d 65 74 68 6f 64 Cachesize method
14538 2e 20 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 . .**.** Configu
14539 72 65 20 74 68 65 20 63 61 63 68 65 5f 73 69 7a re the cache_siz
1453a 65 20 6c 69 6d 69 74 20 66 6f 72 20 61 20 63 61 e limit for a ca
1453b 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 che..*/.static v
1453c 6f 69 64 20 70 63 61 63 68 65 31 43 61 63 68 65 oid pcache1Cache
1453d 73 69 7a 65 28 73 71 6c 69 74 65 33 5f 70 63 61 size(sqlite3_pca
1453e 63 68 65 20 2a 70 2c 20 69 6e 74 20 6e 4d 61 78 che *p, int nMax
1453f 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 ){. PCache1 *pC
14540 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 ache = (PCache1
14541 2a 29 70 3b 0a 20 20 69 66 28 20 70 43 61 63 68 *)p;. if( pCach
14542 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b e->bPurgeable ){
14543 0a 20 20 20 20 50 47 72 6f 75 70 20 2a 70 47 72 . PGroup *pGr
14544 6f 75 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 47 oup = pCache->pG
14545 72 6f 75 70 3b 0a 20 20 20 20 70 63 61 63 68 65 roup;. pcache
14546 31 45 6e 74 65 72 4d 75 74 65 78 28 70 47 72 6f 1EnterMutex(pGro
14547 75 70 29 3b 0a 20 20 20 20 70 47 72 6f 75 70 2d up);. pGroup-
14548 3e 6e 4d 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d >nMaxPage += (nM
14549 61 78 20 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 ax - pCache->nMa
1454a 78 29 3b 0a 20 20 20 20 70 47 72 6f 75 70 2d 3e x);. pGroup->
1454b 6d 78 50 69 6e 6e 65 64 20 3d 20 70 47 72 6f 75 mxPinned = pGrou
1454c 70 2d 3e 6e 4d 61 78 50 61 67 65 20 2b 20 31 30 p->nMaxPage + 10
1454d 20 2d 20 70 47 72 6f 75 70 2d 3e 6e 4d 69 6e 50 - pGroup->nMinP
1454e 61 67 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d age;. pCache-
1454f 3e 6e 4d 61 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 >nMax = nMax;.
14550 20 20 70 43 61 63 68 65 2d 3e 6e 39 30 70 63 74 pCache->n90pct
14551 20 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 2a = pCache->nMax*
14552 39 2f 31 30 3b 0a 20 20 20 20 70 63 61 63 68 65 9/10;. pcache
14553 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 1EnforceMaxPage(
14554 70 47 72 6f 75 70 29 3b 0a 20 20 20 20 70 63 61 pGroup);. pca
14555 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 70 che1LeaveMutex(p
14556 47 72 6f 75 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f Group);. }.}../
14557 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
14558 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
14559 65 33 5f 70 63 61 63 68 65 2e 78 53 68 72 69 6e e3_pcache.xShrin
1455a 6b 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a k method. .**.**
1455b 20 46 72 65 65 20 75 70 20 61 73 20 6d 75 63 68 Free up as much
1455c 20 6d 65 6d 6f 72 79 20 61 73 20 70 6f 73 73 69 memory as possi
1455d 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ble..*/.static v
1455e 6f 69 64 20 70 63 61 63 68 65 31 53 68 72 69 6e oid pcache1Shrin
1455f 6b 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 k(sqlite3_pcache
14560 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 31 20 *p){. PCache1
14561 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 *pCache = (PCach
14562 65 31 2a 29 70 3b 0a 20 20 69 66 28 20 70 43 61 e1*)p;. if( pCa
14563 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 che->bPurgeable
14564 29 7b 0a 20 20 20 20 50 47 72 6f 75 70 20 2a 70 ){. PGroup *p
14565 47 72 6f 75 70 20 3d 20 70 43 61 63 68 65 2d 3e Group = pCache->
14566 70 47 72 6f 75 70 3b 0a 20 20 20 20 69 6e 74 20 pGroup;. int
14567 73 61 76 65 64 4d 61 78 50 61 67 65 3b 0a 20 20 savedMaxPage;.
14568 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 pcache1EnterMu
14569 74 65 78 28 70 47 72 6f 75 70 29 3b 0a 20 20 20 tex(pGroup);.
1456a 20 73 61 76 65 64 4d 61 78 50 61 67 65 20 3d 20 savedMaxPage =
1456b 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61 67 65 pGroup->nMaxPage
1456c 3b 0a 20 20 20 20 70 47 72 6f 75 70 2d 3e 6e 4d ;. pGroup->nM
1456d 61 78 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 axPage = 0;.
1456e 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 pcache1EnforceMa
1456f 78 50 61 67 65 28 70 47 72 6f 75 70 29 3b 0a 20 xPage(pGroup);.
14570 20 20 20 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 pGroup->nMaxP
14571 61 67 65 20 3d 20 73 61 76 65 64 4d 61 78 50 61 age = savedMaxPa
14572 67 65 3b 0a 20 20 20 20 70 63 61 63 68 65 31 4c ge;. pcache1L
14573 65 61 76 65 4d 75 74 65 78 28 70 47 72 6f 75 70 eaveMutex(pGroup
14574 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
14575 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
14576 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 f the sqlite3_pc
14577 61 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 ache.xPagecount
14578 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 method. .*/.stat
14579 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 50 61 ic int pcache1Pa
1457a 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f gecount(sqlite3_
1457b 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e pcache *p){. in
1457c 74 20 6e 3b 0a 20 20 50 43 61 63 68 65 31 20 2a t n;. PCache1 *
1457d 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 pCache = (PCache
1457e 31 2a 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 1*)p;. pcache1E
1457f 6e 74 65 72 4d 75 74 65 78 28 70 43 61 63 68 65 nterMutex(pCache
14580 2d 3e 70 47 72 6f 75 70 29 3b 0a 20 20 6e 20 3d ->pGroup);. n =
14581 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 3b 0a pCache->nPage;.
14582 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 pcache1LeaveMu
14583 74 65 78 28 70 43 61 63 68 65 2d 3e 70 47 72 6f tex(pCache->pGro
14584 75 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b up);. return n;
14585 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
14586 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
14587 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 sqlite3_pcache.x
14588 46 65 74 63 68 20 6d 65 74 68 6f 64 2e 20 0a 2a Fetch method. .*
14589 2a 0a 2a 2a 20 46 65 74 63 68 20 61 20 70 61 67 *.** Fetch a pag
1458a 65 20 62 79 20 6b 65 79 20 76 61 6c 75 65 2e 0a e by key value..
1458b 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f 72 **.** Whether or
1458c 20 6e 6f 74 20 61 20 6e 65 77 20 70 61 67 65 20 not a new page
1458d 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 may be allocated
1458e 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
1458f 6e 20 64 65 70 65 6e 64 73 20 6f 6e 0a 2a 2a 20 n depends on.**
14590 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
14591 20 63 72 65 61 74 65 46 6c 61 67 20 61 72 67 75 createFlag argu
14592 6d 65 6e 74 2e 20 20 30 20 6d 65 61 6e 73 20 64 ment. 0 means d
14593 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 20 61 o not allocate a
14594 20 6e 65 77 0a 2a 2a 20 70 61 67 65 2e 20 20 31 new.** page. 1
14595 20 6d 65 61 6e 73 20 61 6c 6c 6f 63 61 74 65 20 means allocate
14596 61 20 6e 65 77 20 70 61 67 65 20 69 66 20 73 70 a new page if sp
14597 61 63 65 20 69 73 20 65 61 73 69 6c 79 20 61 76 ace is easily av
14598 61 69 6c 61 62 6c 65 2e 20 20 32 20 0a 2a 2a 20 ailable. 2 .**
14599 6d 65 61 6e 73 20 74 6f 20 74 72 79 20 72 65 61 means to try rea
1459a 6c 6c 79 20 68 61 72 64 20 74 6f 20 61 6c 6c 6f lly hard to allo
1459b 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e cate a new page.
1459c 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 6e 6f 6e .**.** For a non
1459d 2d 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 -purgeable cache
1459e 20 28 61 20 63 61 63 68 65 20 75 73 65 64 20 61 (a cache used a
1459f 73 20 74 68 65 20 73 74 6f 72 61 67 65 20 66 6f s the storage fo
145a0 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a r an in-memory.*
145a1 2a 20 64 61 74 61 62 61 73 65 29 20 74 68 65 72 * database) ther
145a2 65 20 69 73 20 72 65 61 6c 6c 79 20 6e 6f 20 64 e is really no d
145a3 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 ifference betwee
145a4 6e 20 63 72 65 61 74 65 46 6c 61 67 20 31 20 61 n createFlag 1 a
145a5 6e 64 20 32 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 nd 2. So.** the
145a6 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f calling functio
145a7 6e 20 28 70 63 61 63 68 65 2e 63 29 20 77 69 6c n (pcache.c) wil
145a8 6c 20 6e 65 76 65 72 20 68 61 76 65 20 61 20 63 l never have a c
145a9 72 65 61 74 65 46 6c 61 67 20 6f 66 20 31 20 6f reateFlag of 1 o
145aa 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 70 75 72 67 65 n.** a non-purge
145ab 61 62 6c 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a able cache..**.*
145ac 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 * There are thre
145ad 65 20 64 69 66 66 65 72 65 6e 74 20 61 70 70 72 e different appr
145ae 6f 61 63 68 65 73 20 74 6f 20 6f 62 74 61 69 6e oaches to obtain
145af 69 6e 67 20 73 70 61 63 65 20 66 6f 72 20 61 20 ing space for a
145b0 70 61 67 65 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 page,.** dependi
145b1 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 ng on the value
145b2 6f 66 20 70 61 72 61 6d 65 74 65 72 20 63 72 65 of parameter cre
145b3 61 74 65 46 6c 61 67 20 28 77 68 69 63 68 20 6d ateFlag (which m
145b4 61 79 20 62 65 20 30 2c 20 31 20 6f 72 20 32 29 ay be 0, 1 or 2)
145b5 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 52 65 67 ..**.** 1. Reg
145b6 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 76 ardless of the v
145b7 61 6c 75 65 20 6f 66 20 63 72 65 61 74 65 46 6c alue of createFl
145b8 61 67 2c 20 74 68 65 20 63 61 63 68 65 20 69 73 ag, the cache is
145b9 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 61 20 searched for a
145ba 0a 2a 2a 20 20 20 20 20 20 63 6f 70 79 20 6f 66 .** copy of
145bb 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 the requested p
145bc 61 67 65 2e 20 49 66 20 6f 6e 65 20 69 73 20 66 age. If one is f
145bd 6f 75 6e 64 2c 20 69 74 20 69 73 20 72 65 74 75 ound, it is retu
145be 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e rned..**.** 2.
145bf 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 3d 3d If createFlag==
145c0 30 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 0 and the page i
145c1 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e s not already in
145c2 20 74 68 65 20 63 61 63 68 65 2c 20 4e 55 4c 4c the cache, NULL
145c3 20 69 73 0a 2a 2a 20 20 20 20 20 20 72 65 74 75 is.** retu
145c4 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e rned..**.** 3.
145c5 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 If createFlag i
145c6 73 20 31 2c 20 61 6e 64 20 74 68 65 20 70 61 67 s 1, and the pag
145c7 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 e is not already
145c8 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 in the cache, t
145c9 68 65 6e 0a 2a 2a 20 20 20 20 20 20 72 65 74 75 hen.** retu
145ca 72 6e 20 4e 55 4c 4c 20 28 64 6f 20 6e 6f 74 20 rn NULL (do not
145cb 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 allocate a new p
145cc 61 67 65 29 20 69 66 20 61 6e 79 20 6f 66 20 74 age) if any of t
145cd 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 he following.**
145ce 20 20 20 20 20 63 6f 6e 64 69 74 69 6f 6e 73 20 conditions
145cf 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 are true:.**.**
145d0 20 20 20 20 20 20 28 61 29 20 74 68 65 20 6e 75 (a) the nu
145d1 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 mber of pages pi
145d2 6e 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68 nned by the cach
145d3 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 e is greater tha
145d4 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50 n.** P
145d5 43 61 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a Cache1.nMax, or.
145d6 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28 62 29 20 **.** (b)
145d7 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
145d8 67 65 73 20 70 69 6e 6e 65 64 20 62 79 20 74 68 ges pinned by th
145d9 65 20 63 61 63 68 65 20 69 73 20 67 72 65 61 74 e cache is great
145da 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 er than.**
145db 20 20 20 20 20 74 68 65 20 73 75 6d 20 6f 66 20 the sum of
145dc 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 20 70 75 72 nMax for all pur
145dd 67 65 61 62 6c 65 20 63 61 63 68 65 73 2c 20 6c geable caches, l
145de 65 73 73 20 74 68 65 20 73 75 6d 20 6f 66 20 0a ess the sum of .
145df 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6e 4d 69 ** nMi
145e0 6e 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 n for all other
145e1 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 purgeable caches
145e2 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 34 2e 20 , or.**.** 4.
145e3 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 If none of the f
145e4 69 72 73 74 20 74 68 72 65 65 20 63 6f 6e 64 69 irst three condi
145e5 74 69 6f 6e 73 20 61 70 70 6c 79 20 61 6e 64 20 tions apply and
145e6 74 68 65 20 63 61 63 68 65 20 69 73 20 6d 61 72 the cache is mar
145e7 6b 65 64 0a 2a 2a 20 20 20 20 20 20 61 73 20 70 ked.** as p
145e8 75 72 67 65 61 62 6c 65 2c 20 61 6e 64 20 69 66 urgeable, and if
145e9 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
145ea 6f 77 69 6e 67 20 69 73 20 74 72 75 65 3a 0a 2a owing is true:.*
145eb 2a 0a 2a 2a 20 20 20 20 20 20 20 28 61 29 20 54 *.** (a) T
145ec 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
145ed 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 es allocated for
145ee 20 74 68 65 20 63 61 63 68 65 20 69 73 20 61 6c the cache is al
145ef 72 65 61 64 79 20 0a 2a 2a 20 20 20 20 20 20 20 ready .**
145f0 20 20 20 20 50 43 61 63 68 65 31 2e 6e 4d 61 78 PCache1.nMax
145f1 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 , or.**.**
145f2 20 28 62 29 20 54 68 65 20 6e 75 6d 62 65 72 20 (b) The number
145f3 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 of pages allocat
145f4 65 64 20 66 6f 72 20 61 6c 6c 20 70 75 72 67 65 ed for all purge
145f5 61 62 6c 65 20 63 61 63 68 65 73 20 69 73 0a 2a able caches is.*
145f6 2a 20 20 20 20 20 20 20 20 20 20 20 61 6c 72 65 * alre
145f7 61 64 79 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 ady equal to or
145f8 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 greater than the
145f9 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f 72 sum of nMax for
145fa 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 all.**
145fb 20 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 purgeable cach
145fc 65 73 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 es,.**.**
145fd 28 63 29 20 54 68 65 20 73 79 73 74 65 6d 20 69 (c) The system i
145fe 73 20 75 6e 64 65 72 20 6d 65 6d 6f 72 79 20 70 s under memory p
145ff 72 65 73 73 75 72 65 20 61 6e 64 20 77 61 6e 74 ressure and want
14600 73 20 74 6f 20 61 76 6f 69 64 0a 2a 2a 20 20 20 s to avoid.**
14601 20 20 20 20 20 20 20 20 75 6e 6e 65 63 65 73 73 unnecess
14602 61 72 79 20 70 61 67 65 73 20 63 61 63 68 65 20 ary pages cache
14603 65 6e 74 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e entry allocation
14604 73 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 74 68 65 s.**.** the
14605 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 63 n attempt to rec
14606 79 63 6c 65 20 61 20 70 61 67 65 20 66 72 6f 6d ycle a page from
14607 20 74 68 65 20 4c 52 55 20 6c 69 73 74 2e 20 49 the LRU list. I
14608 66 20 69 74 20 69 73 20 74 68 65 20 72 69 67 68 f it is the righ
14609 74 0a 2a 2a 20 20 20 20 20 20 73 69 7a 65 2c 20 t.** size,
1460a 72 65 74 75 72 6e 20 74 68 65 20 72 65 63 79 63 return the recyc
1460b 6c 65 64 20 62 75 66 66 65 72 2e 20 4f 74 68 65 led buffer. Othe
1460c 72 77 69 73 65 2c 20 66 72 65 65 20 74 68 65 20 rwise, free the
1460d 62 75 66 66 65 72 20 61 6e 64 0a 2a 2a 20 20 20 buffer and.**
1460e 20 20 20 70 72 6f 63 65 65 64 20 74 6f 20 73 74 proceed to st
1460f 65 70 20 35 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 35 ep 5. .**.** 5
14610 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6c 6c . Otherwise, all
14611 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e ocate and return
14612 20 61 20 6e 65 77 20 70 61 67 65 20 62 75 66 66 a new page buff
14613 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 er..*/.static sq
14614 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67 lite3_pcache_pag
14615 65 20 2a 70 63 61 63 68 65 31 46 65 74 63 68 28 e *pcache1Fetch(
14616 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 . sqlite3_pcach
14617 65 20 2a 70 2c 20 0a 20 20 75 6e 73 69 67 6e 65 e *p, . unsigne
14618 64 20 69 6e 74 20 69 4b 65 79 2c 20 0a 20 20 69 d int iKey, . i
14619 6e 74 20 63 72 65 61 74 65 46 6c 61 67 0a 29 7b nt createFlag.){
1461a 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
1461b 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68 nPinned;. PCach
1461c 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 e1 *pCache = (PC
1461d 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 47 72 ache1 *)p;. PGr
1461e 6f 75 70 20 2a 70 47 72 6f 75 70 3b 0a 20 20 50 oup *pGroup;. P
1461f 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30 gHdr1 *pPage = 0
14620 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 ;.. assert( pCa
14621 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 che->bPurgeable
14622 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 21 3d 31 || createFlag!=1
14623 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
14624 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 ache->bPurgeable
14625 20 7c 7c 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e || pCache->nMin
14626 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
14627 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 pCache->bPurgea
14628 62 6c 65 3d 3d 30 20 7c 7c 20 70 43 61 63 68 65 ble==0 || pCache
14629 2d 3e 6e 4d 69 6e 3d 3d 31 30 20 29 3b 0a 20 20 ->nMin==10 );.
1462a 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d 3e assert( pCache->
1462b 6e 4d 69 6e 3d 3d 30 20 7c 7c 20 70 43 61 63 68 nMin==0 || pCach
1462c 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 3b e->bPurgeable );
1462d 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d . pcache1EnterM
1462e 75 74 65 78 28 70 47 72 6f 75 70 20 3d 20 70 43 utex(pGroup = pC
1462f 61 63 68 65 2d 3e 70 47 72 6f 75 70 29 3b 0a 0a ache->pGroup);..
14630 20 20 2f 2a 20 53 74 65 70 20 31 3a 20 53 65 61 /* Step 1: Sea
14631 72 63 68 20 74 68 65 20 68 61 73 68 20 74 61 62 rch the hash tab
14632 6c 65 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 le for an existi
14633 6e 67 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 69 ng entry. */. i
14634 66 28 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 f( pCache->nHash
14635 3e 30 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e >0 ){. unsign
14636 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 79 20 ed int h = iKey
14637 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b % pCache->nHash;
14638 0a 20 20 20 20 66 6f 72 28 70 50 61 67 65 3d 70 . for(pPage=p
14639 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d Cache->apHash[h]
1463a 3b 20 70 50 61 67 65 26 26 70 50 61 67 65 2d 3e ; pPage&&pPage->
1463b 69 4b 65 79 21 3d 69 4b 65 79 3b 20 70 50 61 67 iKey!=iKey; pPag
1463c 65 3d 70 50 61 67 65 2d 3e 70 4e 65 78 74 29 3b e=pPage->pNext);
1463d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 . }.. /* Step
1463e 32 3a 20 41 62 6f 72 74 20 69 66 20 6e 6f 20 65 2: Abort if no e
1463f 78 69 73 74 69 6e 67 20 70 61 67 65 20 69 73 20 xisting page is
14640 66 6f 75 6e 64 20 61 6e 64 20 63 72 65 61 74 65 found and create
14641 46 6c 61 67 20 69 73 20 30 20 2a 2f 0a 20 20 69 Flag is 0 */. i
14642 66 28 20 70 50 61 67 65 20 7c 7c 20 63 72 65 61 f( pPage || crea
14643 74 65 46 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 teFlag==0 ){.
14644 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 pcache1PinPage(
14645 70 50 61 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f pPage);. goto
14646 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a fetch_out;. }.
14647 0a 20 20 2f 2a 20 54 68 65 20 70 47 72 6f 75 70 . /* The pGroup
14648 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20 local variable
14649 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 20 62 65 will normally be
1464a 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 79 20 initialized by
1464b 74 68 65 0a 20 20 2a 2a 20 70 63 61 63 68 65 31 the. ** pcache1
1464c 45 6e 74 65 72 4d 75 74 65 78 28 29 20 6d 61 63 EnterMutex() mac
1464d 72 6f 20 61 62 6f 76 65 2e 20 20 42 75 74 20 69 ro above. But i
1464e 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f f SQLITE_MUTEX_O
1464f 4d 49 54 20 69 73 20 64 65 66 69 6e 65 64 2c 0a MIT is defined,.
14650 20 20 2a 2a 20 74 68 65 6e 20 70 63 61 63 68 65 ** then pcache
14651 31 45 6e 74 65 72 4d 75 74 65 78 28 29 20 69 73 1EnterMutex() is
14652 20 61 20 6e 6f 2d 6f 70 2c 20 73 6f 20 77 65 20 a no-op, so we
14653 68 61 76 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 have to initiali
14654 7a 65 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 63 61 ze the. ** loca
14655 6c 20 76 61 72 69 61 62 6c 65 20 68 65 72 65 2e l variable here.
14656 20 20 44 65 6c 61 79 69 6e 67 20 74 68 65 20 69 Delaying the i
14657 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 nitialization of
14658 20 70 47 72 6f 75 70 20 69 73 20 61 6e 0a 20 20 pGroup is an.
14659 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 3a ** optimization:
1465a 20 20 54 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 The common cas
1465b 65 20 69 73 20 74 6f 20 65 78 69 74 20 74 68 65 e is to exit the
1465c 20 6d 6f 64 75 6c 65 20 62 65 66 6f 72 65 20 72 module before r
1465d 65 61 63 68 69 6e 67 0a 20 20 2a 2a 20 74 68 69 eaching. ** thi
1465e 73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2f 0a 23 69 s point.. */.#i
1465f 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 fdef SQLITE_MUTE
14660 58 5f 4f 4d 49 54 0a 20 20 70 47 72 6f 75 70 20 X_OMIT. pGroup
14661 3d 20 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70 = pCache->pGroup
14662 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 53 ;.#endif.. /* S
14663 74 65 70 20 33 3a 20 41 62 6f 72 74 20 69 66 20 tep 3: Abort if
14664 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 31 20 createFlag is 1
14665 62 75 74 20 74 68 65 20 63 61 63 68 65 20 69 73 but the cache is
14666 20 6e 65 61 72 6c 79 20 66 75 6c 6c 20 2a 2f 0a nearly full */.
14667 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 assert( pCache
14668 2d 3e 6e 50 61 67 65 20 3e 3d 20 70 43 61 63 68 ->nPage >= pCach
14669 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 20 29 e->nRecyclable )
1466a 3b 0a 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 ;. nPinned = pC
1466b 61 63 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43 ache->nPage - pC
1466c 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c ache->nRecyclabl
1466d 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 47 72 e;. assert( pGr
1466e 6f 75 70 2d 3e 6d 78 50 69 6e 6e 65 64 20 3d 3d oup->mxPinned ==
1466f 20 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61 67 pGroup->nMaxPag
14670 65 20 2b 20 31 30 20 2d 20 70 47 72 6f 75 70 2d e + 10 - pGroup-
14671 3e 6e 4d 69 6e 50 61 67 65 20 29 3b 0a 20 20 61 >nMinPage );. a
14672 73 73 65 72 74 28 20 70 43 61 63 68 65 2d 3e 6e ssert( pCache->n
14673 39 30 70 63 74 20 3d 3d 20 70 43 61 63 68 65 2d 90pct == pCache-
14674 3e 6e 4d 61 78 2a 39 2f 31 30 20 29 3b 0a 20 20 >nMax*9/10 );.
14675 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d 3d if( createFlag==
14676 31 20 26 26 20 28 0a 20 20 20 20 20 20 20 20 6e 1 && (. n
14677 50 69 6e 6e 65 64 3e 3d 70 47 72 6f 75 70 2d 3e Pinned>=pGroup->
14678 6d 78 50 69 6e 6e 65 64 0a 20 20 20 20 20 7c 7c mxPinned. ||
14679 20 6e 50 69 6e 6e 65 64 3e 3d 70 43 61 63 68 65 nPinned>=pCache
1467a 2d 3e 6e 39 30 70 63 74 0a 20 20 20 20 20 7c 7c ->n90pct. ||
1467b 20 70 63 61 63 68 65 31 55 6e 64 65 72 4d 65 6d pcache1UnderMem
1467c 6f 72 79 50 72 65 73 73 75 72 65 28 70 43 61 63 oryPressure(pCac
1467d 68 65 29 0a 20 20 29 29 7b 0a 20 20 20 20 67 6f he). )){. go
1467e 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 to fetch_out;.
1467f 7d 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d }.. if( pCache-
14680 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d 3e >nPage>=pCache->
14681 6e 48 61 73 68 20 26 26 20 70 63 61 63 68 65 31 nHash && pcache1
14682 52 65 73 69 7a 65 48 61 73 68 28 70 43 61 63 68 ResizeHash(pCach
14683 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 e) ){. goto f
14684 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 etch_out;. }..
14685 20 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79 20 /* Step 4. Try
14686 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 67 to recycle a pag
14687 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 e. */. if( pCac
14688 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26 he->bPurgeable &
14689 26 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 54 61 & pGroup->pLruTa
1468a 69 6c 20 26 26 20 28 0a 20 20 20 20 20 20 20 20 il && (.
1468b 20 28 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b (pCache->nPage+
1468c 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 1>=pCache->nMax)
1468d 0a 20 20 20 20 20 20 7c 7c 20 70 47 72 6f 75 70 . || pGroup
1468e 2d 3e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 3d ->nCurrentPage>=
1468f 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61 67 65 pGroup->nMaxPage
14690 0a 20 20 20 20 20 20 7c 7c 20 70 63 61 63 68 65 . || pcache
14691 31 55 6e 64 65 72 4d 65 6d 6f 72 79 50 72 65 73 1UnderMemoryPres
14692 73 75 72 65 28 70 43 61 63 68 65 29 0a 20 20 29 sure(pCache). )
14693 29 7b 0a 20 20 20 20 50 43 61 63 68 65 31 20 2a ){. PCache1 *
14694 70 4f 74 68 65 72 3b 0a 20 20 20 20 70 50 61 67 pOther;. pPag
14695 65 20 3d 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 e = pGroup->pLru
14696 54 61 69 6c 3b 0a 20 20 20 20 70 63 61 63 68 65 Tail;. pcache
14697 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 1RemoveFromHash(
14698 70 50 61 67 65 29 3b 0a 20 20 20 20 70 63 61 63 pPage);. pcac
14699 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 he1PinPage(pPage
1469a 29 3b 0a 20 20 20 20 70 4f 74 68 65 72 20 3d 20 );. pOther =
1469b 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a 0a pPage->pCache;..
1469c 20 20 20 20 2f 2a 20 57 65 20 77 61 6e 74 20 74 /* We want t
1469d 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 73 7a o verify that sz
1469e 50 61 67 65 20 61 6e 64 20 73 7a 45 78 74 72 61 Page and szExtra
1469f 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 66 6f are the same fo
146a0 72 20 70 4f 74 68 65 72 0a 20 20 20 20 2a 2a 20 r pOther. **
146a1 61 6e 64 20 70 43 61 63 68 65 2e 20 20 41 73 73 and pCache. Ass
146a2 65 72 74 20 74 68 61 74 20 77 65 20 63 61 6e 20 ert that we can
146a3 76 65 72 69 66 79 20 74 68 69 73 20 62 79 20 63 verify this by c
146a4 6f 6d 70 61 72 69 6e 67 20 73 75 6d 73 2e 20 2a omparing sums. *
146a5 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 /. assert( (p
146a6 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 26 20 Cache->szPage &
146a7 28 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 2d (pCache->szPage-
146a8 31 29 29 3d 3d 30 20 26 26 20 70 43 61 63 68 65 1))==0 && pCache
146a9 2d 3e 73 7a 50 61 67 65 3e 3d 35 31 32 20 29 3b ->szPage>=512 );
146aa 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 61 . assert( pCa
146ab 63 68 65 2d 3e 73 7a 45 78 74 72 61 3c 35 31 32 che->szExtra<512
146ac 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
146ad 28 70 4f 74 68 65 72 2d 3e 73 7a 50 61 67 65 20 (pOther->szPage
146ae 26 20 28 70 4f 74 68 65 72 2d 3e 73 7a 50 61 67 & (pOther->szPag
146af 65 2d 31 29 29 3d 3d 30 20 26 26 20 70 4f 74 68 e-1))==0 && pOth
146b0 65 72 2d 3e 73 7a 50 61 67 65 3e 3d 35 31 32 20 er->szPage>=512
146b1 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
146b2 4f 74 68 65 72 2d 3e 73 7a 45 78 74 72 61 3c 35 Other->szExtra<5
146b3 31 32 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 12 );.. if( p
146b4 4f 74 68 65 72 2d 3e 73 7a 50 61 67 65 2b 70 4f Other->szPage+pO
146b5 74 68 65 72 2d 3e 73 7a 45 78 74 72 61 20 21 3d ther->szExtra !=
146b6 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 2b pCache->szPage+
146b7 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 pCache->szExtra
146b8 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 ){. pcache1
146b9 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b FreePage(pPage);
146ba 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 30 . pPage = 0
146bb 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
146bc 20 20 20 70 47 72 6f 75 70 2d 3e 6e 43 75 72 72 pGroup->nCurr
146bd 65 6e 74 50 61 67 65 20 2d 3d 20 28 70 4f 74 68 entPage -= (pOth
146be 65 72 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 2d er->bPurgeable -
146bf 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 pCache->bPurgea
146c0 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ble);. }. }.
146c1 0a 20 20 2f 2a 20 53 74 65 70 20 35 2e 20 49 66 . /* Step 5. If
146c2 20 61 20 75 73 61 62 6c 65 20 70 61 67 65 20 62 a usable page b
146c3 75 66 66 65 72 20 68 61 73 20 73 74 69 6c 6c 20 uffer has still
146c4 6e 6f 74 20 62 65 65 6e 20 66 6f 75 6e 64 2c 20 not been found,
146c5 0a 20 20 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f . ** attempt to
146c6 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 allocate a new
146c7 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 one. . */. if(
146c8 20 21 70 50 61 67 65 20 29 7b 0a 20 20 20 20 69 !pPage ){. i
146c9 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 f( createFlag==1
146ca 20 29 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 ) sqlite3BeginB
146cb 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
146cc 20 20 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 pPage = pcach
146cd 65 31 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63 e1AllocPage(pCac
146ce 68 65 29 3b 0a 20 20 20 20 69 66 28 20 63 72 65 he);. if( cre
146cf 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c ateFlag==1 ) sql
146d0 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
146d1 6c 6f 63 28 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 loc();. }.. if
146d2 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 75 ( pPage ){. u
146d3 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 nsigned int h =
146d4 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e iKey % pCache->n
146d5 48 61 73 68 3b 0a 20 20 20 20 70 43 61 63 68 65 Hash;. pCache
146d6 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70 ->nPage++;. p
146d7 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 Page->iKey = iKe
146d8 79 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e y;. pPage->pN
146d9 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 ext = pCache->ap
146da 48 61 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 Hash[h];. pPa
146db 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 ge->pCache = pCa
146dc 63 68 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e che;. pPage->
146dd 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 pLruPrev = 0;.
146de 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 pPage->pLruNex
146df 74 20 3d 20 30 3b 0a 20 20 20 20 2a 28 76 6f 69 t = 0;. *(voi
146e0 64 20 2a 2a 29 70 50 61 67 65 2d 3e 70 61 67 65 d **)pPage->page
146e1 2e 70 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 20 .pExtra = 0;.
146e2 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b pCache->apHash[
146e3 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a h] = pPage;. }.
146e4 0a 66 65 74 63 68 5f 6f 75 74 3a 0a 20 20 69 66 .fetch_out:. if
146e5 28 20 70 50 61 67 65 20 26 26 20 69 4b 65 79 3e ( pPage && iKey>
146e6 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 pCache->iMaxKey
146e7 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 69 ){. pCache->i
146e8 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20 MaxKey = iKey;.
146e9 20 7d 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 }. pcache1Leav
146ea 65 4d 75 74 65 78 28 70 47 72 6f 75 70 29 3b 0a eMutex(pGroup);.
146eb 20 20 72 65 74 75 72 6e 20 26 70 50 61 67 65 2d return &pPage-
146ec 3e 70 61 67 65 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a >page;.}.../*.**
146ed 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
146ee 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 of the sqlite3_p
146ef 63 61 63 68 65 2e 78 55 6e 70 69 6e 20 6d 65 74 cache.xUnpin met
146f0 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72 6b 20 hod..**.** Mark
146f1 61 20 70 61 67 65 20 61 73 20 75 6e 70 69 6e 6e a page as unpinn
146f2 65 64 20 28 65 6c 69 67 69 62 6c 65 20 66 6f 72 ed (eligible for
146f3 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 72 65 asynchronous re
146f4 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f 0a 73 74 61 cycling)..*/.sta
146f5 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 tic void pcache1
146f6 55 6e 70 69 6e 28 0a 20 20 73 71 6c 69 74 65 33 Unpin(. sqlite3
146f7 5f 70 63 61 63 68 65 20 2a 70 2c 20 0a 20 20 73 _pcache *p, . s
146f8 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 qlite3_pcache_pa
146f9 67 65 20 2a 70 50 67 2c 20 0a 20 20 69 6e 74 20 ge *pPg, . int
146fa 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 0a 29 7b reuseUnlikely.){
146fb 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 . PCache1 *pCac
146fc 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 he = (PCache1 *)
146fd 70 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 50 61 p;. PgHdr1 *pPa
146fe 67 65 20 3d 20 28 50 67 48 64 72 31 20 2a 29 70 ge = (PgHdr1 *)p
146ff 50 67 3b 0a 20 20 50 47 72 6f 75 70 20 2a 70 47 Pg;. PGroup *pG
14700 72 6f 75 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 roup = pCache->p
14701 47 72 6f 75 70 3b 0a 20 0a 20 20 61 73 73 65 72 Group;. . asser
14702 74 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 t( pPage->pCache
14703 3d 3d 70 43 61 63 68 65 20 29 3b 0a 20 20 70 63 ==pCache );. pc
14704 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
14705 70 47 72 6f 75 70 29 3b 0a 0a 20 20 2f 2a 20 49 pGroup);.. /* I
14706 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f t is an error to
14707 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 call this funct
14708 69 6f 6e 20 69 66 20 74 68 65 20 70 61 67 65 20 ion if the page
14709 69 73 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a is already . **
1470a 20 70 61 72 74 20 6f 66 20 74 68 65 20 50 47 72 part of the PGr
1470b 6f 75 70 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20 oup LRU list..
1470c 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
1470d 67 65 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20 ge->pLruPrev==0
1470e 26 26 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 && pPage->pLruNe
1470f 78 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 xt==0 );. asser
14710 74 28 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 48 t( pGroup->pLruH
14711 65 61 64 21 3d 70 50 61 67 65 20 26 26 20 70 47 ead!=pPage && pG
14712 72 6f 75 70 2d 3e 70 4c 72 75 54 61 69 6c 21 3d roup->pLruTail!=
14713 70 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20 pPage );.. if(
14714 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c reuseUnlikely ||
14715 20 70 47 72 6f 75 70 2d 3e 6e 43 75 72 72 65 6e pGroup->nCurren
14716 74 50 61 67 65 3e 70 47 72 6f 75 70 2d 3e 6e 4d tPage>pGroup->nM
14717 61 78 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63 axPage ){. pc
14718 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 ache1RemoveFromH
14719 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20 ash(pPage);.
1471a 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 pcache1FreePage(
1471b 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b pPage);. }else{
1471c 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 . /* Add the
1471d 70 61 67 65 20 74 6f 20 74 68 65 20 50 47 72 6f page to the PGro
1471e 75 70 20 4c 52 55 20 6c 69 73 74 2e 20 2a 2f 0a up LRU list. */.
1471f 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 2d 3e if( pGroup->
14720 70 4c 72 75 48 65 61 64 20 29 7b 0a 20 20 20 20 pLruHead ){.
14721 20 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 48 65 pGroup->pLruHe
14722 61 64 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 ad->pLruPrev = p
14723 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 Page;. pPag
14724 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 47 e->pLruNext = pG
14725 72 6f 75 70 2d 3e 70 4c 72 75 48 65 61 64 3b 0a roup->pLruHead;.
14726 20 20 20 20 20 20 70 47 72 6f 75 70 2d 3e 70 4c pGroup->pL
14727 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a ruHead = pPage;.
14728 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
14729 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 54 61 69 pGroup->pLruTai
1472a 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 l = pPage;.
1472b 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 48 65 61 pGroup->pLruHea
1472c 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d d = pPage;. }
1472d 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 . pCache->nRe
1472e 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a cyclable++;. }.
1472f 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
14730 75 74 65 78 28 70 43 61 63 68 65 2d 3e 70 47 72 utex(pCache->pGr
14731 6f 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 oup);.}../*.** I
14732 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
14733 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
14734 63 68 65 2e 78 52 65 6b 65 79 20 6d 65 74 68 6f che.xRekey metho
14735 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f d. .*/.static vo
14736 69 64 20 70 63 61 63 68 65 31 52 65 6b 65 79 28 id pcache1Rekey(
14737 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 . sqlite3_pcach
14738 65 20 2a 70 2c 0a 20 20 73 71 6c 69 74 65 33 5f e *p,. sqlite3_
14739 70 63 61 63 68 65 5f 70 61 67 65 20 2a 70 50 67 pcache_page *pPg
1473a 2c 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 ,. unsigned int
1473b 20 69 4f 6c 64 2c 0a 20 20 75 6e 73 69 67 6e 65 iOld,. unsigne
1473c 64 20 69 6e 74 20 69 4e 65 77 0a 29 7b 0a 20 20 d int iNew.){.
1473d 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 PCache1 *pCache
1473e 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a = (PCache1 *)p;.
1473f 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 PgHdr1 *pPage
14740 3d 20 28 50 67 48 64 72 31 20 2a 29 70 50 67 3b = (PgHdr1 *)pPg;
14741 0a 20 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a . PgHdr1 **pp;.
14742 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
14743 3b 20 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ; . assert( pPa
14744 67 65 2d 3e 69 4b 65 79 3d 3d 69 4f 6c 64 20 29 ge->iKey==iOld )
14745 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
14746 65 2d 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68 e->pCache==pCach
14747 65 20 29 3b 0a 0a 20 20 70 63 61 63 68 65 31 45 e );.. pcache1E
14748 6e 74 65 72 4d 75 74 65 78 28 70 43 61 63 68 65 nterMutex(pCache
14749 2d 3e 70 47 72 6f 75 70 29 3b 0a 0a 20 20 68 20 ->pGroup);.. h
1474a 3d 20 69 4f 6c 64 25 70 43 61 63 68 65 2d 3e 6e = iOld%pCache->n
1474b 48 61 73 68 3b 0a 20 20 70 70 20 3d 20 26 70 43 Hash;. pp = &pC
1474c 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b ache->apHash[h];
1474d 0a 20 20 77 68 69 6c 65 28 20 28 2a 70 70 29 21 . while( (*pp)!
1474e 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 70 =pPage ){. pp
1474f 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 = &(*pp)->pNext
14750 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20 70 50 ;. }. *pp = pP
14751 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 68 age->pNext;.. h
14752 20 3d 20 69 4e 65 77 25 70 43 61 63 68 65 2d 3e = iNew%pCache->
14753 6e 48 61 73 68 3b 0a 20 20 70 50 61 67 65 2d 3e nHash;. pPage->
14754 69 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20 20 70 iKey = iNew;. p
14755 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 70 43 Page->pNext = pC
14756 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b ache->apHash[h];
14757 0a 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 . pCache->apHas
14758 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 h[h] = pPage;.
14759 69 66 28 20 69 4e 65 77 3e 70 43 61 63 68 65 2d if( iNew>pCache-
1475a 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 20 >iMaxKey ){.
1475b 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 pCache->iMaxKey
1475c 3d 20 69 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 70 = iNew;. }.. p
1475d 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
1475e 28 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70 29 (pCache->pGroup)
1475f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 ;.}../*.** Imple
14760 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
14761 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e sqlite3_pcache.
14762 78 54 72 75 6e 63 61 74 65 20 6d 65 74 68 6f 64 xTruncate method
14763 2e 20 0a 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64 . .**.** Discard
14764 20 61 6c 6c 20 75 6e 70 69 6e 6e 65 64 20 70 61 all unpinned pa
14765 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 ges in the cache
14766 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d with a page num
14767 62 65 72 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20 ber equal to.**
14768 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 or greater than
14769 70 61 72 61 6d 65 74 65 72 20 69 4c 69 6d 69 74 parameter iLimit
1476a 2e 20 41 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 . Any pinned pag
1476b 65 73 20 77 69 74 68 20 61 20 70 61 67 65 20 6e es with a page n
1476c 75 6d 62 65 72 0a 2a 2a 20 65 71 75 61 6c 20 74 umber.** equal t
1476d 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 o or greater tha
1476e 6e 20 69 4c 69 6d 69 74 20 61 72 65 20 69 6d 70 n iLimit are imp
1476f 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64 licitly unpinned
14770 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
14771 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 pcache1Truncate
14772 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 (sqlite3_pcache
14773 2a 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 *p, unsigned int
14774 20 69 4c 69 6d 69 74 29 7b 0a 20 20 50 43 61 63 iLimit){. PCac
14775 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 he1 *pCache = (P
14776 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 Cache1 *)p;. pc
14777 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
14778 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70 29 3b pCache->pGroup);
14779 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3c 3d 70 . if( iLimit<=p
1477a 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 Cache->iMaxKey )
1477b 7b 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 75 {. pcache1Tru
1477c 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43 61 63 ncateUnsafe(pCac
1477d 68 65 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 he, iLimit);.
1477e 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 pCache->iMaxKey
1477f 20 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a 20 20 7d = iLimit-1;. }
14780 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
14781 75 74 65 78 28 70 43 61 63 68 65 2d 3e 70 47 72 utex(pCache->pGr
14782 6f 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 oup);.}../*.** I
14783 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
14784 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
14785 63 68 65 2e 78 44 65 73 74 72 6f 79 20 6d 65 74 che.xDestroy met
14786 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73 74 hod. .**.** Dest
14787 72 6f 79 20 61 20 63 61 63 68 65 20 61 6c 6c 6f roy a cache allo
14788 63 61 74 65 64 20 75 73 69 6e 67 20 70 63 61 63 cated using pcac
14789 68 65 31 43 72 65 61 74 65 28 29 2e 0a 2a 2f 0a he1Create()..*/.
1478a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
1478b 68 65 31 44 65 73 74 72 6f 79 28 73 71 6c 69 74 he1Destroy(sqlit
1478c 65 33 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 e3_pcache *p){.
1478d 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 PCache1 *pCache
1478e 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b = (PCache1 *)p;
1478f 0a 20 20 50 47 72 6f 75 70 20 2a 70 47 72 6f 75 . PGroup *pGrou
14790 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 47 72 6f p = pCache->pGro
14791 75 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 up;. assert( pC
14792 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 ache->bPurgeable
14793 20 7c 7c 20 28 70 43 61 63 68 65 2d 3e 6e 4d 61 || (pCache->nMa
14794 78 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e x==0 && pCache->
14795 6e 4d 69 6e 3d 3d 30 29 20 29 3b 0a 20 20 70 63 nMin==0) );. pc
14796 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
14797 70 47 72 6f 75 70 29 3b 0a 20 20 70 63 61 63 68 pGroup);. pcach
14798 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65 e1TruncateUnsafe
14799 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20 20 61 (pCache, 0);. a
1479a 73 73 65 72 74 28 20 70 47 72 6f 75 70 2d 3e 6e ssert( pGroup->n
1479b 4d 61 78 50 61 67 65 20 3e 3d 20 70 43 61 63 68 MaxPage >= pCach
1479c 65 2d 3e 6e 4d 61 78 20 29 3b 0a 20 20 70 47 72 e->nMax );. pGr
1479d 6f 75 70 2d 3e 6e 4d 61 78 50 61 67 65 20 2d 3d oup->nMaxPage -=
1479e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20 pCache->nMax;.
1479f 20 61 73 73 65 72 74 28 20 70 47 72 6f 75 70 2d assert( pGroup-
147a0 3e 6e 4d 69 6e 50 61 67 65 20 3e 3d 20 70 43 61 >nMinPage >= pCa
147a1 63 68 65 2d 3e 6e 4d 69 6e 20 29 3b 0a 20 20 70 che->nMin );. p
147a2 47 72 6f 75 70 2d 3e 6e 4d 69 6e 50 61 67 65 20 Group->nMinPage
147a3 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b -= pCache->nMin;
147a4 0a 20 20 70 47 72 6f 75 70 2d 3e 6d 78 50 69 6e . pGroup->mxPin
147a5 6e 65 64 20 3d 20 70 47 72 6f 75 70 2d 3e 6e 4d ned = pGroup->nM
147a6 61 78 50 61 67 65 20 2b 20 31 30 20 2d 20 70 47 axPage + 10 - pG
147a7 72 6f 75 70 2d 3e 6e 4d 69 6e 50 61 67 65 3b 0a roup->nMinPage;.
147a8 20 20 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 pcache1Enforce
147a9 4d 61 78 50 61 67 65 28 70 47 72 6f 75 70 29 3b MaxPage(pGroup);
147aa 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
147ab 75 74 65 78 28 70 47 72 6f 75 70 29 3b 0a 20 20 utex(pGroup);.
147ac 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 61 sqlite3_free(pCa
147ad 63 68 65 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 che->apHash);.
147ae 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 61 sqlite3_free(pCa
147af 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 che);.}../*.** T
147b0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
147b1 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 69 6e called during in
147b2 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 28 73 71 itialization (sq
147b3 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
147b4 28 29 29 20 74 6f 0a 2a 2a 20 69 6e 73 74 61 6c ()) to.** instal
147b5 6c 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 6c l the default pl
147b6 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d 6f uggable cache mo
147b7 64 75 6c 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 dule, assuming t
147b8 68 65 20 75 73 65 72 20 68 61 73 20 6e 6f 74 0a he user has not.
147b9 2a 2a 20 61 6c 72 65 61 64 79 20 70 72 6f 76 69 ** already provi
147ba 64 65 64 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 ded an alternati
147bb 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ve..*/.SQLITE_PR
147bc 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
147bd 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61 75 e3PCacheSetDefau
147be 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 lt(void){. stat
147bf 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
147c0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 _pcache_methods2
147c1 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 defaultMethods
147c2 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 = {. 1,
147c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
147c4 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a /* iVersion */.
147c5 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 0,
147c6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
147c7 70 41 72 67 20 2a 2f 0a 20 20 20 20 70 63 61 63 pArg */. pcac
147c8 68 65 31 49 6e 69 74 2c 20 20 20 20 20 20 20 20 he1Init,
147c9 20 20 20 20 20 2f 2a 20 78 49 6e 69 74 20 2a 2f /* xInit */
147ca 0a 20 20 20 20 70 63 61 63 68 65 31 53 68 75 74 . pcache1Shut
147cb 64 6f 77 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a down, /*
147cc 20 78 53 68 75 74 64 6f 77 6e 20 2a 2f 0a 20 20 xShutdown */.
147cd 20 20 70 63 61 63 68 65 31 43 72 65 61 74 65 2c pcache1Create,
147ce 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 /* xC
147cf 72 65 61 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 reate */. pca
147d0 63 68 65 31 43 61 63 68 65 73 69 7a 65 2c 20 20 che1Cachesize,
147d1 20 20 20 20 20 20 2f 2a 20 78 43 61 63 68 65 73 /* xCaches
147d2 69 7a 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 ize */. pcach
147d3 65 31 50 61 67 65 63 6f 75 6e 74 2c 20 20 20 20 e1Pagecount,
147d4 20 20 20 20 2f 2a 20 78 50 61 67 65 63 6f 75 6e /* xPagecoun
147d5 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 t */. pcache1
147d6 46 65 74 63 68 2c 20 20 20 20 20 20 20 20 20 20 Fetch,
147d7 20 20 2f 2a 20 78 46 65 74 63 68 20 2a 2f 0a 20 /* xFetch */.
147d8 20 20 20 70 63 61 63 68 65 31 55 6e 70 69 6e 2c pcache1Unpin,
147d9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
147da 55 6e 70 69 6e 20 2a 2f 0a 20 20 20 20 70 63 61 Unpin */. pca
147db 63 68 65 31 52 65 6b 65 79 2c 20 20 20 20 20 20 che1Rekey,
147dc 20 20 20 20 20 20 2f 2a 20 78 52 65 6b 65 79 20 /* xRekey
147dd 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 */. pcache1Tr
147de 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 uncate,
147df 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a /* xTruncate */.
147e0 20 20 20 20 70 63 61 63 68 65 31 44 65 73 74 72 pcache1Destr
147e1 6f 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 oy, /*
147e2 78 44 65 73 74 72 6f 79 20 2a 2f 0a 20 20 20 20 xDestroy */.
147e3 70 63 61 63 68 65 31 53 68 72 69 6e 6b 20 20 20 pcache1Shrink
147e4 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68 72 /* xShr
147e5 69 6e 6b 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 ink */. };. sq
147e6 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c lite3_config(SQL
147e7 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 ITE_CONFIG_PCACH
147e8 45 32 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 E2, &defaultMeth
147e9 6f 64 73 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 ods);.}..#ifdef
147ea 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 SQLITE_ENABLE_ME
147eb 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a MORY_MANAGEMENT.
147ec 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
147ed 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f ion is called to
147ee 20 66 72 65 65 20 73 75 70 65 72 66 6c 75 6f 75 free superfluou
147ef 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c s dynamically al
147f0 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 0a 2a located memory.*
147f1 2a 20 68 65 6c 64 20 62 79 20 74 68 65 20 70 61 * held by the pa
147f2 67 65 72 20 73 79 73 74 65 6d 2e 20 4d 65 6d 6f ger system. Memo
147f3 72 79 20 69 6e 20 75 73 65 20 62 79 20 61 6e 79 ry in use by any
147f4 20 53 51 4c 69 74 65 20 70 61 67 65 72 20 61 6c SQLite pager al
147f5 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 located.** by th
147f6 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 e current thread
147f7 20 6d 61 79 20 62 65 20 73 71 6c 69 74 65 33 5f may be sqlite3_
147f8 66 72 65 65 28 29 65 64 2e 0a 2a 2a 0a 2a 2a 20 free()ed..**.**
147f9 6e 52 65 71 20 69 73 20 74 68 65 20 6e 75 6d 62 nReq is the numb
147fa 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d er of bytes of m
147fb 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 2e 20 emory required.
147fc 4f 6e 63 65 20 74 68 69 73 20 6d 75 63 68 20 68 Once this much h
147fd 61 73 0a 2a 2a 20 62 65 65 6e 20 72 65 6c 65 61 as.** been relea
147fe 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f sed, the functio
147ff 6e 20 72 65 74 75 72 6e 73 2e 20 54 68 65 20 72 n returns. The r
14800 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 eturn value is t
14801 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
14802 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 6f 66 20 .** of bytes of
14803 6d 65 6d 6f 72 79 20 72 65 6c 65 61 73 65 64 2e memory released.
14804 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14805 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
14806 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 acheReleaseMemor
14807 79 28 69 6e 74 20 6e 52 65 71 29 7b 0a 20 20 69 y(int nReq){. i
14808 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 nt nFree = 0;.
14809 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1480a 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 63 mutex_notheld(pc
1480b 61 63 68 65 31 2e 67 72 70 2e 6d 75 74 65 78 29 ache1.grp.mutex)
1480c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
1480d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 lite3_mutex_noth
1480e 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65 eld(pcache1.mute
1480f 78 29 20 29 3b 0a 20 20 69 66 28 20 70 63 61 63 x) );. if( pcac
14810 68 65 31 2e 70 53 74 61 72 74 3d 3d 30 20 29 7b he1.pStart==0 ){
14811 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 3b 0a . PgHdr1 *p;.
14812 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 pcache1Enter
14813 4d 75 74 65 78 28 26 70 63 61 63 68 65 31 2e 67 Mutex(&pcache1.g
14814 72 70 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 rp);. while(
14815 28 6e 52 65 71 3c 30 20 7c 7c 20 6e 46 72 65 65 (nReq<0 || nFree
14816 3c 6e 52 65 71 29 20 26 26 20 28 28 70 3d 70 63 <nReq) && ((p=pc
14817 61 63 68 65 31 2e 67 72 70 2e 70 4c 72 75 54 61 ache1.grp.pLruTa
14818 69 6c 29 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 il)!=0) ){.
14819 20 6e 46 72 65 65 20 2b 3d 20 70 63 61 63 68 65 nFree += pcache
1481a 31 4d 65 6d 53 69 7a 65 28 70 2d 3e 70 61 67 65 1MemSize(p->page
1481b 2e 70 42 75 66 29 3b 0a 23 69 66 64 65 66 20 53 .pBuf);.#ifdef S
1481c 51 4c 49 54 45 5f 50 43 41 43 48 45 5f 53 45 50 QLITE_PCACHE_SEP
1481d 41 52 41 54 45 5f 48 45 41 44 45 52 0a 20 20 20 ARATE_HEADER.
1481e 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 nFree += sqli
1481f 74 65 33 4d 65 6d 53 69 7a 65 28 70 29 3b 0a 23 te3MemSize(p);.#
14820 65 6e 64 69 66 0a 20 20 20 20 20 20 70 63 61 63 endif. pcac
14821 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b 0a 20 he1PinPage(p);.
14822 20 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f pcache1Remo
14823 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 veFromHash(p);.
14824 20 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 pcache1Free
14825 50 61 67 65 28 70 29 3b 0a 20 20 20 20 7d 0a 20 Page(p);. }.
14826 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d pcache1LeaveM
14827 75 74 65 78 28 26 70 63 61 63 68 65 31 2e 67 72 utex(&pcache1.gr
14828 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e p);. }. return
14829 20 6e 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 nFree;.}.#endif
1482a 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c /* SQLITE_ENABL
1482b 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d E_MEMORY_MANAGEM
1482c 45 4e 54 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 ENT */..#ifdef S
1482d 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a QLITE_TEST./*.**
1482e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
1482f 73 20 75 73 65 64 20 62 79 20 74 65 73 74 20 70 s used by test p
14830 72 6f 63 65 64 75 72 65 73 20 74 6f 20 69 6e 73 rocedures to ins
14831 70 65 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61 pect the interna
14832 6c 20 73 74 61 74 65 0a 2a 2a 20 6f 66 20 74 68 l state.** of th
14833 65 20 67 6c 6f 62 61 6c 20 63 61 63 68 65 2e 0a e global cache..
14834 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14835 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
14836 61 63 68 65 53 74 61 74 73 28 0a 20 20 69 6e 74 acheStats(. int
14837 20 2a 70 6e 43 75 72 72 65 6e 74 2c 20 20 20 20 *pnCurrent,
14838 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 /* OUT: Total
14839 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
1483a 63 61 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 cached */. int
1483b 2a 70 6e 4d 61 78 2c 20 20 20 20 20 20 20 20 20 *pnMax,
1483c 20 2f 2a 20 4f 55 54 3a 20 47 6c 6f 62 61 6c 20 /* OUT: Global
1483d 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 20 73 69 maximum cache si
1483e 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d ze */. int *pnM
1483f 69 6e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 in, /*
14840 4f 55 54 3a 20 53 75 6d 20 6f 66 20 50 43 61 63 OUT: Sum of PCac
14841 68 65 31 2e 6e 4d 69 6e 20 66 6f 72 20 70 75 72 he1.nMin for pur
14842 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f geable caches */
14843 0a 20 20 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c . int *pnRecycl
14844 61 62 6c 65 20 20 20 20 2f 2a 20 4f 55 54 3a 20 able /* OUT:
14845 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 Total number of
14846 70 61 67 65 73 20 61 76 61 69 6c 61 62 6c 65 20 pages available
14847 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 20 2a 2f for recycling */
14848 0a 29 7b 0a 20 20 50 67 48 64 72 31 20 2a 70 3b .){. PgHdr1 *p;
14849 0a 20 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 . int nRecyclab
1484a 6c 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d le = 0;. for(p=
1484b 70 63 61 63 68 65 31 2e 67 72 70 2e 70 4c 72 75 pcache1.grp.pLru
1484c 48 65 61 64 3b 20 70 3b 20 70 3d 70 2d 3e 70 4c Head; p; p=p->pL
1484d 72 75 4e 65 78 74 29 7b 0a 20 20 20 20 6e 52 65 ruNext){. nRe
1484e 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a cyclable++;. }.
1484f 20 20 2a 70 6e 43 75 72 72 65 6e 74 20 3d 20 70 *pnCurrent = p
14850 63 61 63 68 65 31 2e 67 72 70 2e 6e 43 75 72 72 cache1.grp.nCurr
14851 65 6e 74 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 61 entPage;. *pnMa
14852 78 20 3d 20 28 69 6e 74 29 70 63 61 63 68 65 31 x = (int)pcache1
14853 2e 67 72 70 2e 6e 4d 61 78 50 61 67 65 3b 0a 20 .grp.nMaxPage;.
14854 20 2a 70 6e 4d 69 6e 20 3d 20 28 69 6e 74 29 70 *pnMin = (int)p
14855 63 61 63 68 65 31 2e 67 72 70 2e 6e 4d 69 6e 50 cache1.grp.nMinP
14856 61 67 65 3b 0a 20 20 2a 70 6e 52 65 63 79 63 6c age;. *pnRecycl
14857 61 62 6c 65 20 3d 20 6e 52 65 63 79 63 6c 61 62 able = nRecyclab
14858 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a le;.}.#endif../*
14859 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
1485a 64 20 6f 66 20 70 63 61 63 68 65 31 2e 63 20 2a d of pcache1.c *
1485b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1485c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1485d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1485e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
1485f 67 69 6e 20 66 69 6c 65 20 72 6f 77 73 65 74 2e gin file rowset.
14860 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
14861 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14862 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
14863 0a 2a 2a 20 32 30 30 38 20 44 65 63 65 6d 62 65 .** 2008 Decembe
14864 72 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 r 3.**.** The au
14865 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
14866 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
14867 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
14868 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
14869 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
1486a 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
1486b 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
1486c 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
1486d 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
1486e 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
1486f 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
14870 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
14871 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
14872 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
14873 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
14874 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
14875 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
14876 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14877 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14878 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14879 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1487a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f **.**.** This mo
1487b 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 dule implements
1487c 61 6e 20 6f 62 6a 65 63 74 20 77 65 20 63 61 6c an object we cal
1487d 6c 20 61 20 22 52 6f 77 53 65 74 22 2e 0a 2a 2a l a "RowSet"..**
1487e 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 20 6f .** The RowSet o
1487f 62 6a 65 63 74 20 69 73 20 61 20 63 6f 6c 6c 65 bject is a colle
14880 63 74 69 6f 6e 20 6f 66 20 72 6f 77 69 64 73 2e ction of rowids.
14881 20 20 52 6f 77 69 64 73 0a 2a 2a 20 61 72 65 20 Rowids.** are
14882 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 inserted into th
14883 65 20 52 6f 77 53 65 74 20 69 6e 20 61 6e 20 61 e RowSet in an a
14884 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 2e 20 rbitrary order.
14885 20 49 6e 73 65 72 74 73 0a 2a 2a 20 63 61 6e 20 Inserts.** can
14886 62 65 20 69 6e 74 65 72 6d 69 78 65 64 20 77 69 be intermixed wi
14887 74 68 20 74 65 73 74 73 20 74 6f 20 73 65 65 20 th tests to see
14888 69 66 20 61 20 67 69 76 65 6e 20 72 6f 77 69 64 if a given rowid
14889 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 has been.** pre
1488a 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 65 64 viously inserted
1488b 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 into the RowSet
1488c 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 6c ..**.** After al
1488d 6c 20 69 6e 73 65 72 74 73 20 61 72 65 20 66 69 l inserts are fi
1488e 6e 69 73 68 65 64 2c 20 69 74 20 69 73 20 70 6f nished, it is po
1488f 73 73 69 62 6c 65 20 74 6f 20 65 78 74 72 61 63 ssible to extrac
14890 74 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 t the.** element
14891 73 20 6f 66 20 74 68 65 20 52 6f 77 53 65 74 20 s of the RowSet
14892 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e in sorted order.
14893 20 20 4f 6e 63 65 20 74 68 69 73 20 65 78 74 72 Once this extr
14894 61 63 74 69 6f 6e 0a 2a 2a 20 70 72 6f 63 65 73 action.** proces
14895 73 20 68 61 73 20 73 74 61 72 74 65 64 2c 20 6e s has started, n
14896 6f 20 6e 65 77 20 65 6c 65 6d 65 6e 74 73 20 6d o new elements m
14897 61 79 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a ay be inserted..
14898 2a 2a 0a 2a 2a 20 48 65 6e 63 65 2c 20 74 68 65 **.** Hence, the
14899 20 70 72 69 6d 69 74 69 76 65 20 6f 70 65 72 61 primitive opera
1489a 74 69 6f 6e 73 20 66 6f 72 20 61 20 52 6f 77 53 tions for a RowS
1489b 65 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 et are:.**.**
1489c 20 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 49 4e CREATE.** IN
1489d 53 45 52 54 0a 2a 2a 20 20 20 20 54 45 53 54 0a SERT.** TEST.
1489e 2a 2a 20 20 20 20 53 4d 41 4c 4c 45 53 54 0a 2a ** SMALLEST.*
1489f 2a 20 20 20 20 44 45 53 54 52 4f 59 0a 2a 2a 0a * DESTROY.**.
148a0 2a 2a 20 54 68 65 20 43 52 45 41 54 45 20 61 6e ** The CREATE an
148a1 64 20 44 45 53 54 52 4f 59 20 70 72 69 6d 69 74 d DESTROY primit
148a2 69 76 65 73 20 61 72 65 20 74 68 65 20 63 6f 6e ives are the con
148a3 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 65 73 structor and des
148a4 74 72 75 63 74 6f 72 2c 0a 2a 2a 20 6f 62 76 69 tructor,.** obvi
148a5 6f 75 73 6c 79 2e 20 20 54 68 65 20 49 4e 53 45 ously. The INSE
148a6 52 54 20 70 72 69 6d 69 74 69 76 65 20 61 64 64 RT primitive add
148a7 73 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 s a new element
148a8 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a to the RowSet..*
148a9 2a 20 54 45 53 54 20 63 68 65 63 6b 73 20 74 6f * TEST checks to
148aa 20 73 65 65 20 69 66 20 61 6e 20 65 6c 65 6d 65 see if an eleme
148ab 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e nt is already in
148ac 20 74 68 65 20 52 6f 77 53 65 74 2e 20 20 53 4d the RowSet. SM
148ad 41 4c 4c 45 53 54 0a 2a 2a 20 65 78 74 72 61 63 ALLEST.** extrac
148ae 74 73 20 74 68 65 20 6c 65 61 73 74 20 76 61 6c ts the least val
148af 75 65 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53 ue from the RowS
148b0 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 4e et..**.** The IN
148b1 53 45 52 54 20 70 72 69 6d 69 74 69 76 65 20 6d SERT primitive m
148b2 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 64 ight allocate ad
148b3 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 2e ditional memory.
148b4 20 20 4d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20 61 Memory is.** a
148b5 6c 6c 6f 63 61 74 65 64 20 69 6e 20 63 68 75 6e llocated in chun
148b6 6b 73 20 73 6f 20 6d 6f 73 74 20 49 4e 53 45 52 ks so most INSER
148b7 54 73 20 64 6f 20 6e 6f 20 61 6c 6c 6f 63 61 74 Ts do no allocat
148b8 69 6f 6e 2e 20 20 54 68 65 72 65 20 69 73 20 61 ion. There is a
148b9 6e 20 0a 2a 2a 20 75 70 70 65 72 20 62 6f 75 6e n .** upper boun
148ba 64 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 d on the size of
148bb 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 allocated memor
148bc 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72 79 20 69 73 y. No memory is
148bd 20 66 72 65 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 freed.** until
148be 44 45 53 54 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20 54 DESTROY..**.** T
148bf 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 76 he TEST primitiv
148c0 65 20 69 6e 63 6c 75 64 65 73 20 61 20 22 62 61 e includes a "ba
148c1 74 63 68 22 20 6e 75 6d 62 65 72 2e 20 20 54 68 tch" number. Th
148c2 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 76 65 e TEST primitive
148c3 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 73 65 .** will only se
148c4 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 e elements that
148c5 77 65 72 65 20 69 6e 73 65 72 74 65 64 20 62 65 were inserted be
148c6 66 6f 72 65 20 74 68 65 20 6c 61 73 74 20 63 68 fore the last ch
148c7 61 6e 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 62 ange.** in the b
148c8 61 74 63 68 20 6e 75 6d 62 65 72 2e 20 20 49 6e atch number. In
148c9 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 other words, if
148ca 20 61 6e 20 49 4e 53 45 52 54 20 6f 63 63 75 72 an INSERT occur
148cb 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 77 6f s between.** two
148cc 20 54 45 53 54 73 20 77 68 65 72 65 20 74 68 65 TESTs where the
148cd 20 54 45 53 54 73 20 68 61 76 65 20 74 68 65 20 TESTs have the
148ce 73 61 6d 65 20 62 61 74 63 68 20 6e 75 62 6d 65 same batch nubme
148cf 72 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 76 r, then the.** v
148d0 61 6c 75 65 20 61 64 64 65 64 20 62 79 20 74 68 alue added by th
148d1 65 20 49 4e 53 45 52 54 20 77 69 6c 6c 20 6e 6f e INSERT will no
148d2 74 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f 20 t be visible to
148d3 74 68 65 20 73 65 63 6f 6e 64 20 54 45 53 54 2e the second TEST.
148d4 0a 2a 2a 20 54 68 65 20 69 6e 69 74 69 61 6c 20 .** The initial
148d5 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 batch number is
148d6 7a 65 72 6f 2c 20 73 6f 20 69 66 20 74 68 65 20 zero, so if the
148d7 76 65 72 79 20 66 69 72 73 74 20 54 45 53 54 20 very first TEST
148d8 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 6e 6f contains.** a no
148d9 6e 2d 7a 65 72 6f 20 62 61 74 63 68 20 6e 75 6d n-zero batch num
148da 62 65 72 2c 20 69 74 20 77 69 6c 6c 20 73 65 65 ber, it will see
148db 20 61 6c 6c 20 70 72 69 6f 72 20 49 4e 53 45 52 all prior INSER
148dc 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e 53 Ts..**.** No INS
148dd 45 52 54 73 20 6d 61 79 20 6f 63 63 75 72 73 20 ERTs may occurs
148de 61 66 74 65 72 20 61 20 53 4d 41 4c 4c 45 53 54 after a SMALLEST
148df 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 . An assertion
148e0 77 69 6c 6c 20 66 61 69 6c 20 69 66 0a 2a 2a 20 will fail if.**
148e1 74 68 61 74 20 69 73 20 61 74 74 65 6d 70 74 65 that is attempte
148e2 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 73 d..**.** The cos
148e3 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 69 t of an INSERT i
148e4 73 20 72 6f 75 67 68 6c 79 20 63 6f 6e 73 74 61 s roughly consta
148e5 6e 74 2e 20 20 28 53 6f 6d 65 74 69 6d 65 20 6e nt. (Sometime n
148e6 65 77 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 61 73 ew memory.** has
148e7 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 to be allocated
148e8 20 6f 6e 20 61 6e 20 49 4e 53 45 52 54 2e 29 20 on an INSERT.)
148e9 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 The cost of a T
148ea 45 53 54 20 77 69 74 68 20 61 20 6e 65 77 0a 2a EST with a new.*
148eb 2a 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 * batch number i
148ec 73 20 4f 28 4e 6c 6f 67 4e 29 20 77 68 65 72 65 s O(NlogN) where
148ed 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 N is the number
148ee 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 of elements in
148ef 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 54 the RowSet..** T
148f0 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 45 53 he cost of a TES
148f1 54 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 T using the same
148f2 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 batch number is
148f3 20 4f 28 6c 6f 67 4e 29 2e 20 20 54 68 65 20 63 O(logN). The c
148f4 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 ost.** of the fi
148f5 72 73 74 20 53 4d 41 4c 4c 45 53 54 20 69 73 20 rst SMALLEST is
148f6 4f 28 4e 6c 6f 67 4e 29 2e 20 20 53 65 63 6f 6e O(NlogN). Secon
148f7 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 d and subsequent
148f8 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 70 72 69 SMALLEST.** pri
148f9 6d 69 74 69 76 65 73 20 61 72 65 20 63 6f 6e 73 mitives are cons
148fa 74 61 6e 74 20 74 69 6d 65 2e 20 20 54 68 65 20 tant time. The
148fb 63 6f 73 74 20 6f 66 20 44 45 53 54 52 4f 59 20 cost of DESTROY
148fc 69 73 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 54 is O(N)..**.** T
148fd 68 65 72 65 20 69 73 20 61 6e 20 61 64 64 65 64 here is an added
148fe 20 63 6f 73 74 20 6f 66 20 4f 28 4e 29 20 77 68 cost of O(N) wh
148ff 65 6e 20 73 77 69 74 63 68 69 6e 67 20 62 65 74 en switching bet
14900 77 65 65 6e 20 54 45 53 54 20 61 6e 64 0a 2a 2a ween TEST and.**
14901 20 53 4d 41 4c 4c 45 53 54 20 70 72 69 6d 69 74 SMALLEST primit
14902 69 76 65 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a ives..*/.../*.**
14903 20 54 61 72 67 65 74 20 73 69 7a 65 20 66 6f 72 Target size for
14904 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e allocation chun
14905 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 ks..*/.#define R
14906 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e OWSET_ALLOCATION
14907 5f 53 49 5a 45 20 31 30 32 34 0a 0a 2f 2a 0a 2a _SIZE 1024../*.*
14908 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * The number of
14909 72 6f 77 73 65 74 20 65 6e 74 72 69 65 73 20 70 rowset entries p
1490a 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 er allocation ch
1490b 75 6e 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 unk..*/.#define
1490c 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 ROWSET_ENTRY_PER
1490d 5f 43 48 55 4e 4b 20 20 5c 0a 20 20 20 20 20 20 _CHUNK \.
1490e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1490f 20 28 28 52 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 ((ROWSET_ALLOCA
14910 54 49 4f 4e 5f 53 49 5a 45 2d 38 29 2f 73 69 7a TION_SIZE-8)/siz
14911 65 6f 66 28 73 74 72 75 63 74 20 52 6f 77 53 65 eof(struct RowSe
14912 74 45 6e 74 72 79 29 29 0a 0a 2f 2a 0a 2a 2a 20 tEntry))../*.**
14913 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 61 20 Each entry in a
14914 52 6f 77 53 65 74 20 69 73 20 61 6e 20 69 6e 73 RowSet is an ins
14915 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
14916 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74 2e 0a 2a lowing object..*
14917 2a 0a 2a 2a 20 54 68 69 73 20 73 61 6d 65 20 6f *.** This same o
14918 62 6a 65 63 74 20 69 73 20 72 65 75 73 65 64 20 bject is reused
14919 74 6f 20 73 74 6f 72 65 20 61 20 6c 69 6e 6b 65 to store a linke
1491a 64 20 6c 69 73 74 20 6f 66 20 74 72 65 65 73 20 d list of trees
1491b 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 79 0a 2a of RowSetEntry.*
1491c 2a 20 6f 62 6a 65 63 74 73 2e 20 20 49 6e 20 74 * objects. In t
1491d 68 61 74 20 61 6c 74 65 72 6e 61 74 69 76 65 20 hat alternative
1491e 75 73 65 2c 20 70 52 69 67 68 74 20 70 6f 69 6e use, pRight poin
1491f 74 73 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 ts to the next e
14920 6e 74 72 79 0a 2a 2a 20 69 6e 20 74 68 65 20 6c ntry.** in the l
14921 69 73 74 2c 20 70 4c 65 66 74 20 70 6f 69 6e 74 ist, pLeft point
14922 73 20 74 6f 20 74 68 65 20 74 72 65 65 2c 20 61 s to the tree, a
14923 6e 64 20 76 20 69 73 20 75 6e 75 73 65 64 2e 20 nd v is unused.
14924 20 54 68 65 0a 2a 2a 20 52 6f 77 53 65 74 2e 70 The.** RowSet.p
14925 46 6f 72 65 73 74 20 76 61 6c 75 65 20 70 6f 69 Forest value poi
14926 6e 74 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 nts to the head
14927 6f 66 20 74 68 69 73 20 66 6f 72 65 73 74 20 6c of this forest l
14928 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 ist..*/.struct R
14929 6f 77 53 65 74 45 6e 74 72 79 20 7b 20 20 20 20 owSetEntry {
1492a 20 20 20 20 20 20 20 20 0a 20 20 69 36 34 20 76 . i64 v
1492b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1492c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 4f 57 49 /* ROWI
1492d 44 20 76 61 6c 75 65 20 66 6f 72 20 74 68 69 73 D value for this
1492e 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 entry */. stru
1492f 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
14930 70 52 69 67 68 74 3b 20 20 20 2f 2a 20 52 69 67 pRight; /* Rig
14931 68 74 20 73 75 62 74 72 65 65 20 28 6c 61 72 67 ht subtree (larg
14932 65 72 20 65 6e 74 72 69 65 73 29 20 6f 72 20 6c er entries) or l
14933 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ist */. struct
14934 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65 RowSetEntry *pLe
14935 66 74 3b 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 ft; /* Left s
14936 75 62 74 72 65 65 20 28 73 6d 61 6c 6c 65 72 20 ubtree (smaller
14937 65 6e 74 72 69 65 73 29 20 2a 2f 0a 7d 3b 0a 0a entries) */.};..
14938 2f 2a 0a 2a 2a 20 52 6f 77 53 65 74 45 6e 74 72 /*.** RowSetEntr
14939 79 20 6f 62 6a 65 63 74 73 20 61 72 65 20 61 6c y objects are al
1493a 6c 6f 63 61 74 65 64 20 69 6e 20 6c 61 72 67 65 located in large
1493b 20 63 68 75 6e 6b 73 20 28 69 6e 73 74 61 6e 63 chunks (instanc
1493c 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c es of the.** fol
1493d 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
1493e 29 20 74 6f 20 72 65 64 75 63 65 20 6d 65 6d 6f ) to reduce memo
1493f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 76 ry allocation ov
14940 65 72 68 65 61 64 2e 20 20 54 68 65 0a 2a 2a 20 erhead. The.**
14941 63 68 75 6e 6b 73 20 61 72 65 20 6b 65 70 74 20 chunks are kept
14942 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 on a linked list
14943 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 so that they ca
14944 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 n be deallocated
14945 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 52 6f 77 .** when the Row
14946 53 65 74 20 69 73 20 64 65 73 74 72 6f 79 65 64 Set is destroyed
14947 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 ..*/.struct RowS
14948 65 74 43 68 75 6e 6b 20 7b 0a 20 20 73 74 72 75 etChunk {. stru
14949 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a ct RowSetChunk *
1494a 70 4e 65 78 74 43 68 75 6e 6b 3b 20 20 20 20 20 pNextChunk;
1494b 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b /* Next chunk
1494c 20 6f 6e 20 6c 69 73 74 20 6f 66 20 74 68 65 6d on list of them
1494d 20 61 6c 6c 20 2a 2f 0a 20 20 73 74 72 75 63 74 all */. struct
1494e 20 52 6f 77 53 65 74 45 6e 74 72 79 20 61 45 6e RowSetEntry aEn
1494f 74 72 79 5b 52 4f 57 53 45 54 5f 45 4e 54 52 59 try[ROWSET_ENTRY
14950 5f 50 45 52 5f 43 48 55 4e 4b 5d 3b 20 2f 2a 20 _PER_CHUNK]; /*
14951 41 6c 6c 6f 63 61 74 65 64 20 65 6e 74 72 69 65 Allocated entrie
14952 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 s */.};../*.** A
14953 20 52 6f 77 53 65 74 20 69 6e 20 61 6e 20 69 6e RowSet in an in
14954 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
14955 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
14956 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 79 70 65 64 e..**.** A typed
14957 65 66 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ef of this struc
14958 74 75 72 65 20 69 66 20 66 6f 75 6e 64 20 69 6e ture if found in
14959 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2f sqliteInt.h..*/
1495a 0a 73 74 72 75 63 74 20 52 6f 77 53 65 74 20 7b .struct RowSet {
1495b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
1495c 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20 Chunk *pChunk;
1495d 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c /* List of all
1495e 20 63 68 75 6e 6b 20 61 6c 6c 6f 63 61 74 69 6f chunk allocatio
1495f 6e 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 ns */. sqlite3
14960 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 *db;
14961 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
14962 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14963 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f n */. struct Ro
14964 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 wSetEntry *pEntr
14965 79 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 y; /* List of
14966 20 65 6e 74 72 69 65 73 20 75 73 69 6e 67 20 70 entries using p
14967 52 69 67 68 74 20 2a 2f 0a 20 20 73 74 72 75 63 Right */. struc
14968 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
14969 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 73 Last; /* Las
1496a 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 t entry on the p
1496b 45 6e 74 72 79 20 6c 69 73 74 20 2a 2f 0a 20 20 Entry list */.
1496c 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
1496d 72 79 20 2a 70 46 72 65 73 68 3b 20 20 20 20 2f ry *pFresh; /
1496e 2a 20 53 6f 75 72 63 65 20 6f 66 20 6e 65 77 20 * Source of new
1496f 65 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 2a 2f entry objects */
14970 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
14971 45 6e 74 72 79 20 2a 70 46 6f 72 65 73 74 3b 20 Entry *pForest;
14972 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 62 69 6e /* List of bin
14973 61 72 79 20 74 72 65 65 73 20 6f 66 20 65 6e 74 ary trees of ent
14974 72 69 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 46 ries */. u16 nF
14975 72 65 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 resh;
14976 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
14977 65 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 6f 6e er of objects on
14978 20 70 46 72 65 73 68 20 2a 2f 0a 20 20 75 38 20 pFresh */. u8
14979 72 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 rsFlags;
1497a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
1497b 61 72 69 6f 75 73 20 66 6c 61 67 73 20 2a 2f 0a arious flags */.
1497c 20 20 75 38 20 69 42 61 74 63 68 3b 20 20 20 20 u8 iBatch;
1497d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1497e 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 73 65 /* Current inse
1497f 72 74 20 62 61 74 63 68 20 2a 2f 0a 7d 3b 0a 0a rt batch */.};..
14980 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 /*.** Allowed va
14981 6c 75 65 73 20 66 6f 72 20 52 6f 77 53 65 74 2e lues for RowSet.
14982 72 73 46 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 rsFlags.*/.#defi
14983 6e 65 20 52 4f 57 53 45 54 5f 53 4f 52 54 45 44 ne ROWSET_SORTED
14984 20 20 30 78 30 31 20 20 20 2f 2a 20 54 72 75 65 0x01 /* True
14985 20 69 66 20 52 6f 77 53 65 74 2e 70 45 6e 74 72 if RowSet.pEntr
14986 79 20 69 73 20 73 6f 72 74 65 64 20 2a 2f 0a 23 y is sorted */.#
14987 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 4e 45 define ROWSET_NE
14988 58 54 20 20 20 20 30 78 30 32 20 20 20 2f 2a 20 XT 0x02 /*
14989 54 72 75 65 20 69 66 20 73 71 6c 69 74 65 33 52 True if sqlite3R
1498a 6f 77 53 65 74 4e 65 78 74 28 29 20 68 61 73 20 owSetNext() has
1498b 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a 0a been called */..
1498c 2f 2a 0a 2a 2a 20 54 75 72 6e 20 62 75 6c 6b 20 /*.** Turn bulk
1498d 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 52 6f memory into a Ro
1498e 77 53 65 74 20 6f 62 6a 65 63 74 2e 20 20 4e 20 wSet object. N
1498f 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a bytes of memory.
14990 2a 2a 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 ** are available
14991 20 61 74 20 70 53 70 61 63 65 2e 20 20 54 68 65 at pSpace. The
14992 20 64 62 20 70 6f 69 6e 74 65 72 20 69 73 20 75 db pointer is u
14993 73 65 64 20 61 73 20 61 20 6d 65 6d 6f 72 79 20 sed as a memory
14994 63 6f 6e 74 65 78 74 0a 2a 2a 20 66 6f 72 20 61 context.** for a
14995 6e 79 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c ny subsequent al
14996 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6e locations that n
14997 65 65 64 20 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a eed to occur..**
14998 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
14999 72 20 74 6f 20 74 68 65 20 6e 65 77 20 52 6f 77 r to the new Row
1499a 53 65 74 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a Set object..**.*
1499b 2a 20 49 74 20 6d 75 73 74 20 62 65 20 74 68 65 * It must be the
1499c 20 63 61 73 65 20 74 68 61 74 20 4e 20 69 73 20 case that N is
1499d 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 6d 61 sufficient to ma
1499e 6b 65 20 61 20 52 6f 77 73 65 74 2e 20 20 49 66 ke a Rowset. If
1499f 20 6e 6f 74 0a 2a 2a 20 61 6e 20 61 73 73 65 72 not.** an asser
149a0 74 69 6f 6e 20 66 61 75 6c 74 20 6f 63 63 75 72 tion fault occur
149a1 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 4e 20 69 s..** .** If N i
149a2 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 s larger than th
149a3 65 20 6d 69 6e 69 6d 75 6d 2c 20 75 73 65 20 74 e minimum, use t
149a4 68 65 20 73 75 72 70 6c 75 73 20 61 73 20 61 6e he surplus as an
149a5 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 61 6c 6c 6f initial.** allo
149a6 63 61 74 69 6f 6e 20 6f 66 20 65 6e 74 72 69 65 cation of entrie
149a7 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 62 s available to b
149a8 65 20 66 69 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c e filled..*/.SQL
149a9 49 54 45 5f 50 52 49 56 41 54 45 20 52 6f 77 53 ITE_PRIVATE RowS
149aa 65 74 20 2a 73 71 6c 69 74 65 33 52 6f 77 53 65 et *sqlite3RowSe
149ab 74 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 tInit(sqlite3 *d
149ac 62 2c 20 76 6f 69 64 20 2a 70 53 70 61 63 65 2c b, void *pSpace,
149ad 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 4e 29 unsigned int N)
149ae 7b 0a 20 20 52 6f 77 53 65 74 20 2a 70 3b 0a 20 {. RowSet *p;.
149af 20 61 73 73 65 72 74 28 20 4e 20 3e 3d 20 52 4f assert( N >= RO
149b0 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 29 29 UND8(sizeof(*p))
149b1 20 29 3b 0a 20 20 70 20 3d 20 70 53 70 61 63 65 );. p = pSpace
149b2 3b 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 ;. p->pChunk =
149b3 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0;. p->db = db;
149b4 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30 . p->pEntry = 0
149b5 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 ;. p->pLast = 0
149b6 3b 0a 20 20 70 2d 3e 70 46 6f 72 65 73 74 20 3d ;. p->pForest =
149b7 20 30 3b 0a 20 20 70 2d 3e 70 46 72 65 73 68 20 0;. p->pFresh
149b8 3d 20 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 = (struct RowSet
149b9 45 6e 74 72 79 2a 29 28 52 4f 55 4e 44 38 28 73 Entry*)(ROUND8(s
149ba 69 7a 65 6f 66 28 2a 70 29 29 20 2b 20 28 63 68 izeof(*p)) + (ch
149bb 61 72 2a 29 70 29 3b 0a 20 20 70 2d 3e 6e 46 72 ar*)p);. p->nFr
149bc 65 73 68 20 3d 20 28 75 31 36 29 28 28 4e 20 2d esh = (u16)((N -
149bd 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a ROUND8(sizeof(*
149be 70 29 29 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 p)))/sizeof(stru
149bf 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 29 29 ct RowSetEntry))
149c0 3b 0a 20 20 70 2d 3e 72 73 46 6c 61 67 73 20 3d ;. p->rsFlags =
149c1 20 52 4f 57 53 45 54 5f 53 4f 52 54 45 44 3b 0a ROWSET_SORTED;.
149c2 20 20 70 2d 3e 69 42 61 74 63 68 20 3d 20 30 3b p->iBatch = 0;
149c3 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
149c4 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 /*.** Deallocate
149c5 20 61 6c 6c 20 63 68 75 6e 6b 73 20 66 72 6f 6d all chunks from
149c6 20 61 20 52 6f 77 53 65 74 2e 20 20 54 68 69 73 a RowSet. This
149c7 20 66 72 65 65 73 20 61 6c 6c 20 6d 65 6d 6f 72 frees all memor
149c8 79 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 52 6f y that.** the Ro
149c9 77 53 65 74 20 68 61 73 20 61 6c 6c 6f 63 61 74 wSet has allocat
149ca 65 64 20 6f 76 65 72 20 69 74 73 20 6c 69 66 65 ed over its life
149cb 74 69 6d 65 2e 20 20 54 68 69 73 20 72 6f 75 74 time. This rout
149cc 69 6e 65 20 69 73 0a 2a 2a 20 74 68 65 20 64 65 ine is.** the de
149cd 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 structor for the
149ce 20 52 6f 77 53 65 74 2e 0a 2a 2f 0a 53 51 4c 49 RowSet..*/.SQLI
149cf 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
149d0 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 sqlite3RowSetCle
149d1 61 72 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 ar(RowSet *p){.
149d2 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 struct RowSetCh
149d3 75 6e 6b 20 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e unk *pChunk, *pN
149d4 65 78 74 43 68 75 6e 6b 3b 0a 20 20 66 6f 72 28 extChunk;. for(
149d5 70 43 68 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b pChunk=p->pChunk
149d6 3b 20 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b ; pChunk; pChunk
149d7 20 3d 20 70 4e 65 78 74 43 68 75 6e 6b 29 7b 0a = pNextChunk){.
149d8 20 20 20 20 70 4e 65 78 74 43 68 75 6e 6b 20 3d pNextChunk =
149d9 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 43 68 pChunk->pNextCh
149da 75 6e 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 unk;. sqlite3
149db 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 DbFree(p->db, pC
149dc 68 75 6e 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e hunk);. }. p->
149dd 70 43 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d pChunk = 0;. p-
149de 3e 6e 46 72 65 73 68 20 3d 20 30 3b 0a 20 20 70 >nFresh = 0;. p
149df 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 ->pEntry = 0;.
149e0 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 p->pLast = 0;.
149e1 70 2d 3e 70 46 6f 72 65 73 74 20 3d 20 30 3b 0a p->pForest = 0;.
149e2 20 20 70 2d 3e 72 73 46 6c 61 67 73 20 3d 20 52 p->rsFlags = R
149e3 4f 57 53 45 54 5f 53 4f 52 54 45 44 3b 0a 7d 0a OWSET_SORTED;.}.
149e4 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 ./*.** Allocate
149e5 61 20 6e 65 77 20 52 6f 77 53 65 74 45 6e 74 72 a new RowSetEntr
149e6 79 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 73 y object that is
149e7 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
149e8 20 74 68 65 0a 2a 2a 20 67 69 76 65 6e 20 52 6f the.** given Ro
149e9 77 53 65 74 2e 20 20 52 65 74 75 72 6e 20 61 20 wSet. Return a
149ea 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e pointer to the n
149eb 65 77 20 61 6e 64 20 63 6f 6d 70 6c 65 74 65 6c ew and completel
149ec 79 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 0a y uninitialized.
149ed 2a 2a 20 6f 62 6a 65 63 74 65 64 2e 0a 2a 2a 0a ** objected..**.
149ee 2a 2a 20 49 6e 20 61 6e 20 4f 4f 4d 20 73 69 74 ** In an OOM sit
149ef 75 61 74 69 6f 6e 2c 20 74 68 65 20 52 6f 77 53 uation, the RowS
149f0 65 74 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 et.db->mallocFai
149f1 6c 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 led flag is set
149f2 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 and this.** rout
149f3 69 6e 65 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c ine returns NULL
149f4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
149f5 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
149f6 72 6f 77 53 65 74 45 6e 74 72 79 41 6c 6c 6f 63 rowSetEntryAlloc
149f7 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 61 (RowSet *p){. a
149f8 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
149f9 20 69 66 28 20 70 2d 3e 6e 46 72 65 73 68 3d 3d if( p->nFresh==
149fa 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 0 ){. struct
149fb 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e 65 RowSetChunk *pNe
149fc 77 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 w;. pNew = sq
149fd 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
149fe 28 70 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a (p->db, sizeof(*
149ff 70 4e 65 77 29 29 3b 0a 20 20 20 20 69 66 28 20 pNew));. if(
14a00 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 pNew==0 ){.
14a01 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d return 0;. }
14a02 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 . pNew->pNext
14a03 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 43 68 75 6e Chunk = p->pChun
14a04 6b 3b 0a 20 20 20 20 70 2d 3e 70 43 68 75 6e 6b k;. p->pChunk
14a05 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e = pNew;. p->
14a06 70 46 72 65 73 68 20 3d 20 70 4e 65 77 2d 3e 61 pFresh = pNew->a
14a07 45 6e 74 72 79 3b 0a 20 20 20 20 70 2d 3e 6e 46 Entry;. p->nF
14a08 72 65 73 68 20 3d 20 52 4f 57 53 45 54 5f 45 4e resh = ROWSET_EN
14a09 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 3b 0a 20 TRY_PER_CHUNK;.
14a0a 20 7d 0a 20 20 70 2d 3e 6e 46 72 65 73 68 2d 2d }. p->nFresh--
14a0b 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 46 ;. return p->pF
14a0c 72 65 73 68 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a resh++;.}../*.**
14a0d 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 76 61 Insert a new va
14a0e 6c 75 65 20 69 6e 74 6f 20 61 20 52 6f 77 53 65 lue into a RowSe
14a0f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 6c t..**.** The mal
14a10 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 6f locFailed flag o
14a11 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 f the database c
14a12 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 73 65 74 onnection is set
14a13 20 69 66 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20 if a.** memory
14a14 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 allocation fails
14a15 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14a16 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
14a17 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f 77 RowSetInsert(Row
14a18 53 65 74 20 2a 70 2c 20 69 36 34 20 72 6f 77 69 Set *p, i64 rowi
14a19 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 d){. struct Row
14a1a 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 SetEntry *pEntry
14a1b 3b 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 65 6e ; /* The new en
14a1c 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 try */. struct
14a1d 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 61 RowSetEntry *pLa
14a1e 73 74 3b 20 20 20 2f 2a 20 54 68 65 20 6c 61 73 st; /* The las
14a1f 74 20 70 72 69 6f 72 20 65 6e 74 72 79 20 2a 2f t prior entry */
14a20 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 .. /* This rout
14a21 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c ine is never cal
14a22 6c 65 64 20 61 66 74 65 72 20 73 71 6c 69 74 65 led after sqlite
14a23 33 52 6f 77 53 65 74 4e 65 78 74 28 29 20 2a 2f 3RowSetNext() */
14a24 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 . assert( p!=0
14a25 26 26 20 28 70 2d 3e 72 73 46 6c 61 67 73 20 26 && (p->rsFlags &
14a26 20 52 4f 57 53 45 54 5f 4e 45 58 54 29 3d 3d 30 ROWSET_NEXT)==0
14a27 20 29 3b 0a 0a 20 20 70 45 6e 74 72 79 20 3d 20 );.. pEntry =
14a28 72 6f 77 53 65 74 45 6e 74 72 79 41 6c 6c 6f 63 rowSetEntryAlloc
14a29 28 70 29 3b 0a 20 20 69 66 28 20 70 45 6e 74 72 (p);. if( pEntr
14a2a 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 y==0 ) return;.
14a2b 20 70 45 6e 74 72 79 2d 3e 76 20 3d 20 72 6f 77 pEntry->v = row
14a2c 69 64 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 70 52 id;. pEntry->pR
14a2d 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 4c 61 73 ight = 0;. pLas
14a2e 74 20 3d 20 70 2d 3e 70 4c 61 73 74 3b 0a 20 20 t = p->pLast;.
14a2f 69 66 28 20 70 4c 61 73 74 20 29 7b 0a 20 20 20 if( pLast ){.
14a30 20 69 66 28 20 28 70 2d 3e 72 73 46 6c 61 67 73 if( (p->rsFlags
14a31 20 26 20 52 4f 57 53 45 54 5f 53 4f 52 54 45 44 & ROWSET_SORTED
14a32 29 21 3d 30 20 26 26 20 72 6f 77 69 64 3c 3d 70 )!=0 && rowid<=p
14a33 4c 61 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 20 Last->v ){.
14a34 20 70 2d 3e 72 73 46 6c 61 67 73 20 26 3d 20 7e p->rsFlags &= ~
14a35 52 4f 57 53 45 54 5f 53 4f 52 54 45 44 3b 0a 20 ROWSET_SORTED;.
14a36 20 20 20 7d 0a 20 20 20 20 70 4c 61 73 74 2d 3e }. pLast->
14a37 70 52 69 67 68 74 20 3d 20 70 45 6e 74 72 79 3b pRight = pEntry;
14a38 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d . }else{. p-
14a39 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 >pEntry = pEntry
14a3a 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 61 73 74 ;. }. p->pLast
14a3b 20 3d 20 70 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a = pEntry;.}../*
14a3c 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c 69 .** Merge two li
14a3d 73 74 73 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 sts of RowSetEnt
14a3e 72 79 20 6f 62 6a 65 63 74 73 2e 20 20 52 65 6d ry objects. Rem
14a3f 6f 76 65 20 64 75 70 6c 69 63 61 74 65 73 2e 0a ove duplicates..
14a40 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 **.** The input
14a41 6c 69 73 74 73 20 61 72 65 20 63 6f 6e 6e 65 63 lists are connec
14a42 74 65 64 20 76 69 61 20 70 52 69 67 68 74 20 70 ted via pRight p
14a43 6f 69 6e 74 65 72 73 20 61 6e 64 20 61 72 65 20 ointers and are
14a44 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 6f 20 65 .** assumed to e
14a45 61 63 68 20 61 6c 72 65 61 64 79 20 62 65 20 69 ach already be i
14a46 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a n sorted order..
14a47 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 */.static struct
14a48 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f RowSetEntry *ro
14a49 77 53 65 74 45 6e 74 72 79 4d 65 72 67 65 28 0a wSetEntryMerge(.
14a4a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
14a4b 6e 74 72 79 20 2a 70 41 2c 20 20 20 20 2f 2a 20 ntry *pA, /*
14a4c 46 69 72 73 74 20 73 6f 72 74 65 64 20 6c 69 73 First sorted lis
14a4d 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a t to be merged *
14a4e 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 /. struct RowSe
14a4f 74 45 6e 74 72 79 20 2a 70 42 20 20 20 20 20 2f tEntry *pB /
14a50 2a 20 53 65 63 6f 6e 64 20 73 6f 72 74 65 64 20 * Second sorted
14a51 6c 69 73 74 20 74 6f 20 62 65 20 6d 65 72 67 65 list to be merge
14a52 64 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 d */.){. struct
14a53 20 52 6f 77 53 65 74 45 6e 74 72 79 20 68 65 61 RowSetEntry hea
14a54 64 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 d;. struct RowS
14a55 65 74 45 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0a etEntry *pTail;.
14a56 0a 20 20 70 54 61 69 6c 20 3d 20 26 68 65 61 64 . pTail = &head
14a57 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 ;. while( pA &&
14a58 20 70 42 20 29 7b 0a 20 20 20 20 61 73 73 65 72 pB ){. asser
14a59 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 t( pA->pRight==0
14a5a 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 || pA->v<=pA->p
14a5b 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 Right->v );.
14a5c 61 73 73 65 72 74 28 20 70 42 2d 3e 70 52 69 67 assert( pB->pRig
14a5d 68 74 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d ht==0 || pB->v<=
14a5e 70 42 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b pB->pRight->v );
14a5f 0a 20 20 20 20 69 66 28 20 70 41 2d 3e 76 3c 70 . if( pA->v<p
14a60 42 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 54 B->v ){. pT
14a61 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 ail->pRight = pA
14a62 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d ;. pA = pA-
14a63 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 70 >pRight;. p
14a64 54 61 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 Tail = pTail->pR
14a65 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 20 ight;. }else
14a66 69 66 28 20 70 42 2d 3e 76 3c 70 41 2d 3e 76 20 if( pB->v<pA->v
14a67 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e ){. pTail->
14a68 70 52 69 67 68 74 20 3d 20 70 42 3b 0a 20 20 20 pRight = pB;.
14a69 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 52 69 67 pB = pB->pRig
14a6a 68 74 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 ht;. pTail
14a6b 3d 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b = pTail->pRight;
14a6c 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
14a6d 20 20 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 pA = pA->pRigh
14a6e 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 t;. }. }. i
14a6f 66 28 20 70 41 20 29 7b 0a 20 20 20 20 61 73 73 f( pA ){. ass
14a70 65 72 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d ert( pA->pRight=
14a71 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d =0 || pA->v<=pA-
14a72 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 >pRight->v );.
14a73 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 pTail->pRight
14a74 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = pA;. }else{.
14a75 20 20 20 61 73 73 65 72 74 28 20 70 42 3d 3d 30 assert( pB==0
14a76 20 7c 7c 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d || pB->pRight==
14a77 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 0 || pB->v<=pB->
14a78 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 pRight->v );.
14a79 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d pTail->pRight =
14a7a 20 70 42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 pB;. }. retur
14a7b 6e 20 68 65 61 64 2e 70 52 69 67 68 74 3b 0a 7d n head.pRight;.}
14a7c 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 20 61 6c 6c ../*.** Sort all
14a7d 20 65 6c 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 elements on the
14a7e 20 6c 69 73 74 20 6f 66 20 52 6f 77 53 65 74 45 list of RowSetE
14a7f 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 69 6e 74 ntry objects int
14a80 6f 20 6f 72 64 65 72 20 6f 66 0a 2a 2a 20 69 6e o order of.** in
14a81 63 72 65 61 73 69 6e 67 20 76 2e 0a 2a 2f 20 0a creasing v..*/ .
14a82 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52 6f static struct Ro
14a83 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 65 wSetEntry *rowSe
14a84 74 45 6e 74 72 79 53 6f 72 74 28 73 74 72 75 63 tEntrySort(struc
14a85 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
14a86 49 6e 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 In){. unsigned
14a87 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
14a88 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4e 65 RowSetEntry *pNe
14a89 78 74 2c 20 2a 61 42 75 63 6b 65 74 5b 34 30 5d xt, *aBucket[40]
14a8a 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 61 42 75 63 ;.. memset(aBuc
14a8b 6b 65 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 ket, 0, sizeof(a
14a8c 42 75 63 6b 65 74 29 29 3b 0a 20 20 77 68 69 6c Bucket));. whil
14a8d 65 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 70 4e e( pIn ){. pN
14a8e 65 78 74 20 3d 20 70 49 6e 2d 3e 70 52 69 67 68 ext = pIn->pRigh
14a8f 74 3b 0a 20 20 20 20 70 49 6e 2d 3e 70 52 69 67 t;. pIn->pRig
14a90 68 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 ht = 0;. for(
14a91 69 3d 30 3b 20 61 42 75 63 6b 65 74 5b 69 5d 3b i=0; aBucket[i];
14a92 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 49 6e i++){. pIn
14a93 20 3d 20 72 6f 77 53 65 74 45 6e 74 72 79 4d 65 = rowSetEntryMe
14a94 72 67 65 28 61 42 75 63 6b 65 74 5b 69 5d 2c 20 rge(aBucket[i],
14a95 70 49 6e 29 3b 0a 20 20 20 20 20 20 61 42 75 63 pIn);. aBuc
14a96 6b 65 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 ket[i] = 0;.
14a97 7d 0a 20 20 20 20 61 42 75 63 6b 65 74 5b 69 5d }. aBucket[i]
14a98 20 3d 20 70 49 6e 3b 0a 20 20 20 20 70 49 6e 20 = pIn;. pIn
14a99 3d 20 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 = pNext;. }. p
14a9a 49 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d In = 0;. for(i=
14a9b 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42 75 63 0; i<sizeof(aBuc
14a9c 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42 75 63 ket)/sizeof(aBuc
14a9d 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 ket[0]); i++){.
14a9e 20 20 20 70 49 6e 20 3d 20 72 6f 77 53 65 74 45 pIn = rowSetE
14a9f 6e 74 72 79 4d 65 72 67 65 28 70 49 6e 2c 20 61 ntryMerge(pIn, a
14aa0 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 20 7d 0a Bucket[i]);. }.
14aa1 20 20 72 65 74 75 72 6e 20 70 49 6e 3b 0a 7d 0a return pIn;.}.
14aa2 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 ../*.** The inpu
14aa3 74 2c 20 70 49 6e 2c 20 69 73 20 61 20 62 69 6e t, pIn, is a bin
14aa4 61 72 79 20 74 72 65 65 20 28 6f 72 20 73 75 62 ary tree (or sub
14aa5 74 72 65 65 29 20 6f 66 20 52 6f 77 53 65 74 45 tree) of RowSetE
14aa6 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a ntry objects..**
14aa7 20 43 6f 6e 76 65 72 74 20 74 68 69 73 20 74 72 Convert this tr
14aa8 65 65 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 ee into a linked
14aa9 20 6c 69 73 74 20 63 6f 6e 6e 65 63 74 65 64 20 list connected
14aaa 62 79 20 74 68 65 20 70 52 69 67 68 74 20 70 6f by the pRight po
14aab 69 6e 74 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65 inters.** and re
14aac 74 75 72 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f turn pointers to
14aad 20 74 68 65 20 66 69 72 73 74 20 61 6e 64 20 6c the first and l
14aae 61 73 74 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 ast elements of
14aaf 74 68 65 20 6e 65 77 20 6c 69 73 74 2e 0a 2a 2f the new list..*/
14ab0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 .static void row
14ab1 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 0a 20 SetTreeToList(.
14ab2 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
14ab3 74 72 79 20 2a 70 49 6e 2c 20 20 20 20 20 20 20 try *pIn,
14ab4 20 20 2f 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 /* Root of the
14ab5 20 69 6e 70 75 74 20 74 72 65 65 20 2a 2f 0a 20 input tree */.
14ab6 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
14ab7 74 72 79 20 2a 2a 70 70 46 69 72 73 74 2c 20 20 try **ppFirst,
14ab8 20 20 2f 2a 20 57 72 69 74 65 20 68 65 61 64 20 /* Write head
14ab9 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 6c 69 of the output li
14aba 73 74 20 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 st here */. str
14abb 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
14abc 2a 2a 70 70 4c 61 73 74 20 20 20 20 20 20 2f 2a **ppLast /*
14abd 20 57 72 69 74 65 20 74 61 69 6c 20 6f 66 20 74 Write tail of t
14abe 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74 20 68 he output list h
14abf 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 ere */.){. asse
14ac0 72 74 28 20 70 49 6e 21 3d 30 20 29 3b 0a 20 20 rt( pIn!=0 );.
14ac1 69 66 28 20 70 49 6e 2d 3e 70 4c 65 66 74 20 29 if( pIn->pLeft )
14ac2 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52 6f 77 {. struct Row
14ac3 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20 20 SetEntry *p;.
14ac4 20 72 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 rowSetTreeToLis
14ac5 74 28 70 49 6e 2d 3e 70 4c 65 66 74 2c 20 70 70 t(pIn->pLeft, pp
14ac6 46 69 72 73 74 2c 20 26 70 29 3b 0a 20 20 20 20 First, &p);.
14ac7 70 2d 3e 70 52 69 67 68 74 20 3d 20 70 49 6e 3b p->pRight = pIn;
14ac8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 . }else{. *p
14ac9 70 46 69 72 73 74 20 3d 20 70 49 6e 3b 0a 20 20 pFirst = pIn;.
14aca 7d 0a 20 20 69 66 28 20 70 49 6e 2d 3e 70 52 69 }. if( pIn->pRi
14acb 67 68 74 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 ght ){. rowSe
14acc 74 54 72 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d tTreeToList(pIn-
14acd 3e 70 52 69 67 68 74 2c 20 26 70 49 6e 2d 3e 70 >pRight, &pIn->p
14ace 52 69 67 68 74 2c 20 70 70 4c 61 73 74 29 3b 0a Right, ppLast);.
14acf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 }else{. *pp
14ad0 4c 61 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a Last = pIn;. }.
14ad1 20 20 61 73 73 65 72 74 28 20 28 2a 70 70 4c 61 assert( (*ppLa
14ad2 73 74 29 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 st)->pRight==0 )
14ad3 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 ;.}.../*.** Conv
14ad4 65 72 74 20 61 20 73 6f 72 74 65 64 20 6c 69 73 ert a sorted lis
14ad5 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 28 63 t of elements (c
14ad6 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 52 69 67 onnected by pRig
14ad7 68 74 29 20 69 6e 74 6f 20 61 20 62 69 6e 61 72 ht) into a binar
14ad8 79 0a 2a 2a 20 74 72 65 65 20 77 69 74 68 20 64 y.** tree with d
14ad9 65 70 74 68 20 6f 66 20 69 44 65 70 74 68 2e 20 epth of iDepth.
14ada 20 41 20 64 65 70 74 68 20 6f 66 20 31 20 6d 65 A depth of 1 me
14adb 61 6e 73 20 74 68 65 20 74 72 65 65 20 63 6f 6e ans the tree con
14adc 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a tains a single.*
14add 2a 20 6e 6f 64 65 20 74 61 6b 65 6e 20 66 72 6f * node taken fro
14ade 6d 20 74 68 65 20 68 65 61 64 20 6f 66 20 2a 70 m the head of *p
14adf 70 4c 69 73 74 2e 20 20 41 20 64 65 70 74 68 20 pList. A depth
14ae0 6f 66 20 32 20 6d 65 61 6e 73 20 61 20 74 72 65 of 2 means a tre
14ae1 65 20 77 69 74 68 0a 2a 2a 20 74 68 72 65 65 20 e with.** three
14ae2 6e 6f 64 65 73 2e 20 20 41 6e 64 20 73 6f 20 66 nodes. And so f
14ae3 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 orth..**.** Use
14ae4 61 73 20 6d 61 6e 79 20 65 6e 74 72 69 65 73 20 as many entries
14ae5 66 72 6f 6d 20 74 68 65 20 69 6e 70 75 74 20 6c from the input l
14ae6 69 73 74 20 61 73 20 72 65 71 75 69 72 65 64 20 ist as required
14ae7 61 6e 64 20 75 70 64 61 74 65 20 74 68 65 0a 2a and update the.*
14ae8 2a 20 2a 70 70 4c 69 73 74 20 74 6f 20 70 6f 69 * *ppList to poi
14ae9 6e 74 20 74 6f 20 74 68 65 20 75 6e 75 73 65 64 nt to the unused
14aea 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 elements of the
14aeb 20 6c 69 73 74 2e 20 20 49 66 20 74 68 65 20 69 list. If the i
14aec 6e 70 75 74 0a 2a 2a 20 6c 69 73 74 20 63 6f 6e nput.** list con
14aed 74 61 69 6e 73 20 74 6f 6f 20 66 65 77 20 65 6c tains too few el
14aee 65 6d 65 6e 74 73 2c 20 74 68 65 6e 20 63 6f 6e ements, then con
14aef 73 74 72 75 63 74 20 61 6e 20 69 6e 63 6f 6d 70 struct an incomp
14af0 6c 65 74 65 20 74 72 65 65 0a 2a 2a 20 61 6e 64 lete tree.** and
14af1 20 6c 65 61 76 65 20 2a 70 70 4c 69 73 74 20 73 leave *ppList s
14af2 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a et to NULL..**.*
14af3 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 * Return a point
14af4 65 72 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6f er to the root o
14af5 66 20 74 68 65 20 63 6f 6e 73 74 72 75 63 74 65 f the constructe
14af6 64 20 62 69 6e 61 72 79 20 74 72 65 65 2e 0a 2a d binary tree..*
14af7 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 /.static struct
14af8 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 RowSetEntry *row
14af9 53 65 74 4e 44 65 65 70 54 72 65 65 28 0a 20 20 SetNDeepTree(.
14afa 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
14afb 72 79 20 2a 2a 70 70 4c 69 73 74 2c 0a 20 20 69 ry **ppList,. i
14afc 6e 74 20 69 44 65 70 74 68 0a 29 7b 0a 20 20 73 nt iDepth.){. s
14afd 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
14afe 79 20 2a 70 3b 20 20 20 20 20 20 20 20 20 2f 2a y *p; /*
14aff 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 6e 65 77 Root of the new
14b00 20 74 72 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 tree */. struc
14b01 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
14b02 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66 Left; /* Lef
14b03 74 20 73 75 62 74 72 65 65 20 2a 2f 0a 20 20 69 t subtree */. i
14b04 66 28 20 2a 70 70 4c 69 73 74 3d 3d 30 20 29 7b f( *ppList==0 ){
14b05 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
14b06 20 7d 0a 20 20 69 66 28 20 69 44 65 70 74 68 3d }. if( iDepth=
14b07 3d 31 20 29 7b 0a 20 20 20 20 70 20 3d 20 2a 70 =1 ){. p = *p
14b08 70 4c 69 73 74 3b 0a 20 20 20 20 2a 70 70 4c 69 pList;. *ppLi
14b09 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a st = p->pRight;.
14b0a 20 20 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 p->pLeft = p
14b0b 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 ->pRight = 0;.
14b0c 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d 0a return p;. }.
14b0d 20 20 70 4c 65 66 74 20 3d 20 72 6f 77 53 65 74 pLeft = rowSet
14b0e 4e 44 65 65 70 54 72 65 65 28 70 70 4c 69 73 74 NDeepTree(ppList
14b0f 2c 20 69 44 65 70 74 68 2d 31 29 3b 0a 20 20 70 , iDepth-1);. p
14b10 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20 20 69 66 = *ppList;. if
14b11 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ( p==0 ){. re
14b12 74 75 72 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 0a turn pLeft;. }.
14b13 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 p->pLeft = pLe
14b14 66 74 3b 0a 20 20 2a 70 70 4c 69 73 74 20 3d 20 ft;. *ppList =
14b15 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e p->pRight;. p->
14b16 70 52 69 67 68 74 20 3d 20 72 6f 77 53 65 74 4e pRight = rowSetN
14b17 44 65 65 70 54 72 65 65 28 70 70 4c 69 73 74 2c DeepTree(ppList,
14b18 20 69 44 65 70 74 68 2d 31 29 3b 0a 20 20 72 65 iDepth-1);. re
14b19 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
14b1a 20 43 6f 6e 76 65 72 74 20 61 20 73 6f 72 74 65 Convert a sorte
14b1b 64 20 6c 69 73 74 20 6f 66 20 65 6c 65 6d 65 6e d list of elemen
14b1c 74 73 20 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 ts into a binary
14b1d 20 74 72 65 65 2e 20 4d 61 6b 65 20 74 68 65 20 tree. Make the
14b1e 74 72 65 65 0a 2a 2a 20 61 73 20 64 65 65 70 20 tree.** as deep
14b1f 61 73 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 as it needs to b
14b20 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f e in order to co
14b21 6e 74 61 69 6e 20 74 68 65 20 65 6e 74 69 72 65 ntain the entire
14b22 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 list..*/.static
14b23 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
14b24 74 72 79 20 2a 72 6f 77 53 65 74 4c 69 73 74 54 try *rowSetListT
14b25 6f 54 72 65 65 28 73 74 72 75 63 74 20 52 6f 77 oTree(struct Row
14b26 53 65 74 45 6e 74 72 79 20 2a 70 4c 69 73 74 29 SetEntry *pList)
14b27 7b 0a 20 20 69 6e 74 20 69 44 65 70 74 68 3b 20 {. int iDepth;
14b28 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70 /* Dep
14b29 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 73 th of the tree s
14b2a 6f 20 66 61 72 20 2a 2f 0a 20 20 73 74 72 75 63 o far */. struc
14b2b 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
14b2c 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 ; /* Curre
14b2d 6e 74 20 74 72 65 65 20 72 6f 6f 74 20 2a 2f 0a nt tree root */.
14b2e 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
14b2f 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 2f ntry *pLeft; /
14b30 2a 20 4c 65 66 74 20 73 75 62 74 72 65 65 20 2a * Left subtree *
14b31 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 /.. assert( pLi
14b32 73 74 21 3d 30 20 29 3b 0a 20 20 70 20 3d 20 70 st!=0 );. p = p
14b33 4c 69 73 74 3b 0a 20 20 70 4c 69 73 74 20 3d 20 List;. pList =
14b34 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e p->pRight;. p->
14b35 70 4c 65 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 pLeft = p->pRigh
14b36 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 44 65 t = 0;. for(iDe
14b37 70 74 68 3d 31 3b 20 70 4c 69 73 74 3b 20 69 44 pth=1; pList; iD
14b38 65 70 74 68 2b 2b 29 7b 0a 20 20 20 20 70 4c 65 epth++){. pLe
14b39 66 74 20 3d 20 70 3b 0a 20 20 20 20 70 20 3d 20 ft = p;. p =
14b3a 70 4c 69 73 74 3b 0a 20 20 20 20 70 4c 69 73 74 pList;. pList
14b3b 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 = p->pRight;.
14b3c 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 p->pLeft = pLe
14b3d 66 74 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 ft;. p->pRigh
14b3e 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 54 t = rowSetNDeepT
14b3f 72 65 65 28 26 70 4c 69 73 74 2c 20 69 44 65 70 ree(&pList, iDep
14b40 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 th);. }. retur
14b41 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 n p;.}../*.** Ta
14b42 6b 65 20 61 6c 6c 20 74 68 65 20 65 6e 74 72 69 ke all the entri
14b43 65 73 20 6f 6e 20 70 2d 3e 70 45 6e 74 72 79 20 es on p->pEntry
14b44 61 6e 64 20 6f 6e 20 74 68 65 20 74 72 65 65 73 and on the trees
14b45 20 69 6e 20 70 2d 3e 70 46 6f 72 65 73 74 20 61 in p->pForest a
14b46 6e 64 0a 2a 2a 20 73 6f 72 74 20 74 68 65 6d 20 nd.** sort them
14b47 61 6c 6c 20 74 6f 67 65 74 68 65 72 20 69 6e 74 all together int
14b48 6f 20 6f 6e 65 20 62 69 67 20 6f 72 64 65 72 65 o one big ordere
14b49 64 20 6c 69 73 74 20 6f 6e 20 70 2d 3e 70 45 6e d list on p->pEn
14b4a 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 try..**.** This
14b4b 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f routine should o
14b4c 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e nly be called on
14b4d 63 65 20 69 6e 20 74 68 65 20 6c 69 66 65 20 6f ce in the life o
14b4e 66 20 61 20 52 6f 77 53 65 74 2e 0a 2a 2f 0a 73 f a RowSet..*/.s
14b4f 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 tatic void rowSe
14b50 74 54 6f 4c 69 73 74 28 52 6f 77 53 65 74 20 2a tToList(RowSet *
14b51 70 29 7b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 p){.. /* This r
14b52 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
14b53 20 6f 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 20 20 only once */.
14b54 61 73 73 65 72 74 28 20 70 21 3d 30 20 26 26 20 assert( p!=0 &&
14b55 28 70 2d 3e 72 73 46 6c 61 67 73 20 26 20 52 4f (p->rsFlags & RO
14b56 57 53 45 54 5f 4e 45 58 54 29 3d 3d 30 20 29 3b WSET_NEXT)==0 );
14b57 0a 0a 20 20 69 66 28 20 28 70 2d 3e 72 73 46 6c .. if( (p->rsFl
14b58 61 67 73 20 26 20 52 4f 57 53 45 54 5f 53 4f 52 ags & ROWSET_SOR
14b59 54 45 44 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 TED)==0 ){. p
14b5a 2d 3e 70 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 ->pEntry = rowSe
14b5b 74 45 6e 74 72 79 53 6f 72 74 28 70 2d 3e 70 45 tEntrySort(p->pE
14b5c 6e 74 72 79 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a ntry);. }.. /*
14b5d 20 57 68 69 6c 65 20 74 68 69 73 20 6d 6f 64 75 While this modu
14b5e 6c 65 20 63 6f 75 6c 64 20 74 68 65 6f 72 65 74 le could theoret
14b5f 69 63 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 69 ically support i
14b60 74 2c 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 t, sqlite3RowSet
14b61 4e 65 78 74 28 29 0a 20 20 2a 2a 20 69 73 20 6e Next(). ** is n
14b62 65 76 65 72 20 63 61 6c 6c 65 64 20 61 66 74 65 ever called afte
14b63 72 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 r sqlite3RowSetT
14b64 65 78 74 28 29 20 66 6f 72 20 74 68 65 20 73 61 ext() for the sa
14b65 6d 65 20 52 6f 77 53 65 74 2e 20 20 53 6f 0a 20 me RowSet. So.
14b66 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 65 76 ** there is nev
14b67 65 72 20 61 20 66 6f 72 65 73 74 20 74 6f 20 64 er a forest to d
14b68 65 61 6c 20 77 69 74 68 2e 20 20 53 68 6f 75 6c eal with. Shoul
14b69 64 20 74 68 69 73 20 63 68 61 6e 67 65 2c 20 73 d this change, s
14b6a 69 6d 70 6c 79 0a 20 20 2a 2a 20 72 65 6d 6f 76 imply. ** remov
14b6b 65 20 74 68 65 20 61 73 73 65 72 74 28 29 20 61 e the assert() a
14b6c 6e 64 20 74 68 65 20 23 69 66 20 30 2e 20 2a 2f nd the #if 0. */
14b6d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 46 . assert( p->pF
14b6e 6f 72 65 73 74 3d 3d 30 20 29 3b 0a 23 69 66 20 orest==0 );.#if
14b6f 30 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 46 0. while( p->pF
14b70 6f 72 65 73 74 20 29 7b 0a 20 20 20 20 73 74 72 orest ){. str
14b71 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
14b72 2a 70 54 72 65 65 20 3d 20 70 2d 3e 70 46 6f 72 *pTree = p->pFor
14b73 65 73 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 est->pLeft;.
14b74 69 66 28 20 70 54 72 65 65 20 29 7b 0a 20 20 20 if( pTree ){.
14b75 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 struct RowSet
14b76 45 6e 74 72 79 20 2a 70 48 65 61 64 2c 20 2a 70 Entry *pHead, *p
14b77 54 61 69 6c 3b 0a 20 20 20 20 20 20 72 6f 77 53 Tail;. rowS
14b78 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 54 72 etTreeToList(pTr
14b79 65 65 2c 20 26 70 48 65 61 64 2c 20 26 70 54 61 ee, &pHead, &pTa
14b7a 69 6c 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 45 il);. p->pE
14b7b 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 45 6e 74 ntry = rowSetEnt
14b7c 72 79 4d 65 72 67 65 28 70 2d 3e 70 45 6e 74 72 ryMerge(p->pEntr
14b7d 79 2c 20 70 48 65 61 64 29 3b 0a 20 20 20 20 7d y, pHead);. }
14b7e 0a 20 20 20 20 70 2d 3e 70 46 6f 72 65 73 74 20 . p->pForest
14b7f 3d 20 70 2d 3e 70 46 6f 72 65 73 74 2d 3e 70 52 = p->pForest->pR
14b80 69 67 68 74 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 ight;. }.#endif
14b81 0a 20 20 70 2d 3e 72 73 46 6c 61 67 73 20 7c 3d . p->rsFlags |=
14b82 20 52 4f 57 53 45 54 5f 4e 45 58 54 3b 20 20 2f ROWSET_NEXT; /
14b83 2a 20 56 65 72 69 66 79 20 74 68 69 73 20 72 6f * Verify this ro
14b84 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 utine is never c
14b85 61 6c 6c 65 64 20 61 67 61 69 6e 20 2a 2f 0a 7d alled again */.}
14b86 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 ../*.** Extract
14b87 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6c 65 the smallest ele
14b88 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 52 6f ment from the Ro
14b89 77 53 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 74 wSet..** Write t
14b8a 68 65 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 he element into
14b8b 2a 70 52 6f 77 69 64 2e 20 20 52 65 74 75 72 6e *pRowid. Return
14b8c 20 31 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 1 on success.
14b8d 52 65 74 75 72 6e 0a 2a 2a 20 30 20 69 66 20 74 Return.** 0 if t
14b8e 68 65 20 52 6f 77 53 65 74 20 69 73 20 61 6c 72 he RowSet is alr
14b8f 65 61 64 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a eady empty..**.*
14b90 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 * After this rou
14b91 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61 tine has been ca
14b92 6c 6c 65 64 2c 20 74 68 65 20 73 71 6c 69 74 65 lled, the sqlite
14b93 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 29 0a 3RowSetInsert().
14b94 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 6e ** routine may n
14b95 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 ot be called aga
14b96 69 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f in. .*/.SQLITE_
14b97 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14b98 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f te3RowSetNext(Ro
14b99 77 53 65 74 20 2a 70 2c 20 69 36 34 20 2a 70 52 wSet *p, i64 *pR
14b9a 6f 77 69 64 29 7b 0a 20 20 61 73 73 65 72 74 28 owid){. assert(
14b9b 20 70 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 4d p!=0 );.. /* M
14b9c 65 72 67 65 20 74 68 65 20 66 6f 72 65 73 74 20 erge the forest
14b9d 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 6f into a single so
14b9e 72 74 65 64 20 6c 69 73 74 20 6f 6e 20 66 69 72 rted list on fir
14b9f 73 74 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 66 28 st call */. if(
14ba0 20 28 70 2d 3e 72 73 46 6c 61 67 73 20 26 20 52 (p->rsFlags & R
14ba1 4f 57 53 45 54 5f 4e 45 58 54 29 3d 3d 30 20 29 OWSET_NEXT)==0 )
14ba2 20 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 70 29 rowSetToList(p)
14ba3 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 ;.. /* Return t
14ba4 68 65 20 6e 65 78 74 20 65 6e 74 72 79 20 6f 6e he next entry on
14ba5 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 the list */. i
14ba6 66 28 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a f( p->pEntry ){.
14ba7 20 20 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 2d *pRowid = p-
14ba8 3e 70 45 6e 74 72 79 2d 3e 76 3b 0a 20 20 20 20 >pEntry->v;.
14ba9 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 p->pEntry = p->p
14baa 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20 Entry->pRight;.
14bab 20 20 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 if( p->pEntry
14bac 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
14bad 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 ite3RowSetClear(
14bae 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 p);. }. re
14baf 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b turn 1;. }else{
14bb0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
14bb1 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 }.}../*.** Chec
14bb2 6b 20 74 6f 20 73 65 65 20 69 66 20 65 6c 65 6d k to see if elem
14bb3 65 6e 74 20 69 52 6f 77 69 64 20 77 61 73 20 69 ent iRowid was i
14bb4 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 nserted into the
14bb5 20 72 6f 77 73 65 74 20 61 73 0a 2a 2a 20 70 61 rowset as.** pa
14bb6 72 74 20 6f 66 20 61 6e 79 20 69 6e 73 65 72 74 rt of any insert
14bb7 20 62 61 74 63 68 20 70 72 69 6f 72 20 74 6f 20 batch prior to
14bb8 69 42 61 74 63 68 2e 20 20 52 65 74 75 72 6e 20 iBatch. Return
14bb9 31 20 6f 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 1 or 0..**.** If
14bba 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 this is the fir
14bbb 73 74 20 74 65 73 74 20 6f 66 20 61 20 6e 65 77 st test of a new
14bbc 20 62 61 74 63 68 20 61 6e 64 20 69 66 20 74 68 batch and if th
14bbd 65 72 65 20 65 78 69 73 74 20 65 6e 74 69 72 65 ere exist entire
14bbe 73 0a 2a 2a 20 6f 6e 20 70 52 6f 77 53 65 74 2d s.** on pRowSet-
14bbf 3e 70 45 6e 74 72 79 2c 20 74 68 65 6e 20 73 6f >pEntry, then so
14bc0 72 74 20 74 68 6f 73 65 20 65 6e 74 69 72 65 73 rt those entires
14bc1 20 69 6e 74 6f 20 74 68 65 20 66 6f 72 65 73 74 into the forest
14bc2 20 61 74 0a 2a 2a 20 70 52 6f 77 53 65 74 2d 3e at.** pRowSet->
14bc3 70 46 6f 72 65 73 74 20 73 6f 20 74 68 61 74 20 pForest so that
14bc4 74 68 65 79 20 63 61 6e 20 62 65 20 74 65 73 74 they can be test
14bc5 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
14bc6 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
14bc7 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77 53 3RowSetTest(RowS
14bc8 65 74 20 2a 70 52 6f 77 53 65 74 2c 20 75 38 20 et *pRowSet, u8
14bc9 69 42 61 74 63 68 2c 20 73 71 6c 69 74 65 33 5f iBatch, sqlite3_
14bca 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 0a 20 int64 iRowid){.
14bcb 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
14bcc 74 72 79 20 2a 70 2c 20 2a 70 54 72 65 65 3b 0a try *p, *pTree;.
14bcd 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 . /* This routi
14bce 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c ne is never call
14bcf 65 64 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 ed after sqlite3
14bd0 52 6f 77 53 65 74 4e 65 78 74 28 29 20 2a 2f 0a RowSetNext() */.
14bd1 20 20 61 73 73 65 72 74 28 20 70 52 6f 77 53 65 assert( pRowSe
14bd2 74 21 3d 30 20 26 26 20 28 70 52 6f 77 53 65 74 t!=0 && (pRowSet
14bd3 2d 3e 72 73 46 6c 61 67 73 20 26 20 52 4f 57 53 ->rsFlags & ROWS
14bd4 45 54 5f 4e 45 58 54 29 3d 3d 30 20 29 3b 0a 0a ET_NEXT)==0 );..
14bd5 20 20 2f 2a 20 53 6f 72 74 20 65 6e 74 72 69 65 /* Sort entrie
14bd6 73 20 69 6e 74 6f 20 74 68 65 20 66 6f 72 65 73 s into the fores
14bd7 74 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 74 t on the first t
14bd8 65 73 74 20 6f 66 20 61 20 6e 65 77 20 62 61 74 est of a new bat
14bd9 63 68 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 ch . */. if( i
14bda 42 61 74 63 68 21 3d 70 52 6f 77 53 65 74 2d 3e Batch!=pRowSet->
14bdb 69 42 61 74 63 68 20 29 7b 0a 20 20 20 20 70 20 iBatch ){. p
14bdc 3d 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 = pRowSet->pEntr
14bdd 79 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a y;. if( p ){.
14bde 20 20 20 20 20 20 73 74 72 75 63 74 20 52 6f 77 struct Row
14bdf 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 50 72 65 SetEntry **ppPre
14be0 76 54 72 65 65 20 3d 20 26 70 52 6f 77 53 65 74 vTree = &pRowSet
14be1 2d 3e 70 46 6f 72 65 73 74 3b 0a 20 20 20 20 20 ->pForest;.
14be2 20 69 66 28 20 28 70 52 6f 77 53 65 74 2d 3e 72 if( (pRowSet->r
14be3 73 46 6c 61 67 73 20 26 20 52 4f 57 53 45 54 5f sFlags & ROWSET_
14be4 53 4f 52 54 45 44 29 3d 3d 30 20 29 7b 0a 20 20 SORTED)==0 ){.
14be5 20 20 20 20 20 20 70 20 3d 20 72 6f 77 53 65 74 p = rowSet
14be6 45 6e 74 72 79 53 6f 72 74 28 70 29 3b 0a 20 20 EntrySort(p);.
14be7 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 }. for(
14be8 70 54 72 65 65 20 3d 20 70 52 6f 77 53 65 74 2d pTree = pRowSet-
14be9 3e 70 46 6f 72 65 73 74 3b 20 70 54 72 65 65 3b >pForest; pTree;
14bea 20 70 54 72 65 65 3d 70 54 72 65 65 2d 3e 70 52 pTree=pTree->pR
14beb 69 67 68 74 29 7b 0a 20 20 20 20 20 20 20 20 70 ight){. p
14bec 70 50 72 65 76 54 72 65 65 20 3d 20 26 70 54 72 pPrevTree = &pTr
14bed 65 65 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 ee->pRight;.
14bee 20 20 20 20 69 66 28 20 70 54 72 65 65 2d 3e 70 if( pTree->p
14bef 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Left==0 ){.
14bf0 20 20 20 20 20 70 54 72 65 65 2d 3e 70 4c 65 66 pTree->pLef
14bf1 74 20 3d 20 72 6f 77 53 65 74 4c 69 73 74 54 6f t = rowSetListTo
14bf2 54 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 Tree(p);.
14bf3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
14bf4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
14bf5 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 struct RowSet
14bf6 45 6e 74 72 79 20 2a 70 41 75 78 2c 20 2a 70 54 Entry *pAux, *pT
14bf7 61 69 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 ail;. r
14bf8 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 owSetTreeToList(
14bf9 70 54 72 65 65 2d 3e 70 4c 65 66 74 2c 20 26 70 pTree->pLeft, &p
14bfa 41 75 78 2c 20 26 70 54 61 69 6c 29 3b 0a 20 20 Aux, &pTail);.
14bfb 20 20 20 20 20 20 20 20 70 54 72 65 65 2d 3e 70 pTree->p
14bfc 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 Left = 0;.
14bfd 20 20 20 20 70 20 3d 20 72 6f 77 53 65 74 45 6e p = rowSetEn
14bfe 74 72 79 4d 65 72 67 65 28 70 41 75 78 2c 20 70 tryMerge(pAux, p
14bff 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
14c00 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 }. if( p
14c01 54 72 65 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Tree==0 ){.
14c02 20 20 20 2a 70 70 50 72 65 76 54 72 65 65 20 3d *ppPrevTree =
14c03 20 70 54 72 65 65 20 3d 20 72 6f 77 53 65 74 45 pTree = rowSetE
14c04 6e 74 72 79 41 6c 6c 6f 63 28 70 52 6f 77 53 65 ntryAlloc(pRowSe
14c05 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 t);. if(
14c06 70 54 72 65 65 20 29 7b 0a 20 20 20 20 20 20 20 pTree ){.
14c07 20 20 20 70 54 72 65 65 2d 3e 76 20 3d 20 30 3b pTree->v = 0;
14c08 0a 20 20 20 20 20 20 20 20 20 20 70 54 72 65 65 . pTree
14c09 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 ->pRight = 0;.
14c0a 20 20 20 20 20 20 20 20 70 54 72 65 65 2d 3e 70 pTree->p
14c0b 4c 65 66 74 20 3d 20 72 6f 77 53 65 74 4c 69 73 Left = rowSetLis
14c0c 74 54 6f 54 72 65 65 28 70 29 3b 0a 20 20 20 20 tToTree(p);.
14c0d 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
14c0e 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e pRowSet->pEn
14c0f 74 72 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 try = 0;. p
14c10 52 6f 77 53 65 74 2d 3e 70 4c 61 73 74 20 3d 20 RowSet->pLast =
14c11 30 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 0;. pRowSet
14c12 2d 3e 72 73 46 6c 61 67 73 20 7c 3d 20 52 4f 57 ->rsFlags |= ROW
14c13 53 45 54 5f 53 4f 52 54 45 44 3b 0a 20 20 20 20 SET_SORTED;.
14c14 7d 0a 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 69 }. pRowSet->i
14c15 42 61 74 63 68 20 3d 20 69 42 61 74 63 68 3b 0a Batch = iBatch;.
14c16 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 74 }.. /* Test t
14c17 6f 20 73 65 65 20 69 66 20 74 68 65 20 69 52 6f o see if the iRo
14c18 77 69 64 20 76 61 6c 75 65 20 61 70 70 65 61 72 wid value appear
14c19 73 20 61 6e 79 77 68 65 72 65 20 69 6e 20 74 68 s anywhere in th
14c1a 65 20 66 6f 72 65 73 74 2e 0a 20 20 2a 2a 20 52 e forest.. ** R
14c1b 65 74 75 72 6e 20 31 20 69 66 20 69 74 20 64 6f eturn 1 if it do
14c1c 65 73 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e es and 0 if not.
14c1d 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 54 72 65 . */. for(pTre
14c1e 65 20 3d 20 70 52 6f 77 53 65 74 2d 3e 70 46 6f e = pRowSet->pFo
14c1f 72 65 73 74 3b 20 70 54 72 65 65 3b 20 70 54 72 rest; pTree; pTr
14c20 65 65 3d 70 54 72 65 65 2d 3e 70 52 69 67 68 74 ee=pTree->pRight
14c21 29 7b 0a 20 20 20 20 70 20 3d 20 70 54 72 65 65 ){. p = pTree
14c22 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 77 68 69 ->pLeft;. whi
14c23 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 20 20 69 le( p ){. i
14c24 66 28 20 70 2d 3e 76 3c 69 52 6f 77 69 64 20 29 f( p->v<iRowid )
14c25 7b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d {. p = p-
14c26 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 7d >pRight;. }
14c27 65 6c 73 65 20 69 66 28 20 70 2d 3e 76 3e 69 52 else if( p->v>iR
14c28 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 owid ){.
14c29 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 p = p->pLeft;.
14c2a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
14c2b 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
14c2c 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
14c2d 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
14c2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
14c2f 64 20 6f 66 20 72 6f 77 73 65 74 2e 63 20 2a 2a d of rowset.c **
14c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
14c33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
14c34 67 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 63 gin file pager.c
14c35 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
14c36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
14c38 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
14c39 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 15.**.** The
14c3a 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
14c3b 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
14c3c 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
14c3d 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
14c3e 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
14c3f 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
14c40 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
14c41 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
14c42 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
14c43 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
14c44 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
14c45 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
14c46 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
14c47 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
14c48 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
14c49 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
14c4a 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
14c4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c4f 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ****.** This is
14c50 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
14c51 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 on of the page c
14c52 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 6f ache subsystem o
14c53 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a r "pager"..** .*
14c54 2a 20 54 68 65 20 70 61 67 65 72 20 69 73 20 75 * The pager is u
14c55 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 61 20 sed to access a
14c56 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 66 69 database disk fi
14c57 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e le. It implemen
14c58 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 6f 6d ts.** atomic com
14c59 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b mit and rollback
14c5a 20 74 68 72 6f 75 67 68 20 74 68 65 20 75 73 65 through the use
14c5b 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 of a journal fi
14c5c 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 73 65 le that.** is se
14c5d 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20 parate from the
14c5e 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
14c5f 54 68 65 20 70 61 67 65 72 20 61 6c 73 6f 20 69 The pager also i
14c60 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 0a 2a mplements file.*
14c61 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 72 65 * locking to pre
14c62 76 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 73 73 vent two process
14c63 65 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20 es from writing
14c64 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
14c65 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 6c 74 e.** file simult
14c66 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 aneously, or one
14c67 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 72 65 process from re
14c68 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 ading the databa
14c69 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74 se while.** anot
14c6a 68 65 72 20 69 73 20 77 72 69 74 69 6e 67 2e 0a her is writing..
14c6b 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
14c6c 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 2f 2a E_OMIT_DISKIO./*
14c6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
14c6e 63 6c 75 64 65 20 77 61 6c 2e 68 20 69 6e 20 74 clude wal.h in t
14c6f 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 70 61 67 he middle of pag
14c70 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.c ***********
14c71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
14c72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
14c73 67 69 6e 20 66 69 6c 65 20 77 61 6c 2e 68 20 2a gin file wal.h *
14c74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
14c77 0a 2a 2a 20 32 30 31 30 20 46 65 62 72 75 61 72 .** 2010 Februar
14c78 79 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 y 1.**.** The au
14c79 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
14c7a 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
14c7b 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
14c7c 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
14c7d 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
14c7e 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
14c7f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
14c80 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
14c81 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
14c82 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
14c83 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
14c84 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
14c85 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
14c86 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
14c87 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
14c88 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
14c89 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
14c8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c8e 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 **.** This heade
14c8f 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 r file defines t
14c90 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 he interface to
14c91 74 68 65 20 77 72 69 74 65 2d 61 68 65 61 64 20 the write-ahead
14c92 6c 6f 67 67 69 6e 67 20 0a 2a 2a 20 73 79 73 74 logging .** syst
14c93 65 6d 2e 20 52 65 66 65 72 20 74 6f 20 74 68 65 em. Refer to the
14c94 20 63 6f 6d 6d 65 6e 74 73 20 62 65 6c 6f 77 20 comments below
14c95 61 6e 64 20 74 68 65 20 68 65 61 64 65 72 20 63 and the header c
14c96 6f 6d 6d 65 6e 74 20 61 74 74 61 63 68 65 64 20 omment attached
14c97 74 6f 20 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 to .** the imple
14c98 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 65 61 63 mentation of eac
14c99 68 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 6c 6f h function in lo
14c9a 67 2e 63 20 66 6f 72 20 66 75 72 74 68 65 72 20 g.c for further
14c9b 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 0a 23 69 66 details..*/..#if
14c9c 6e 64 65 66 20 5f 57 41 4c 5f 48 5f 0a 23 64 65 ndef _WAL_H_.#de
14c9d 66 69 6e 65 20 5f 57 41 4c 5f 48 5f 0a 0a 0a 2f fine _WAL_H_.../
14c9e 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 76 61 6c * Additional val
14c9f 75 65 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 ues that can be
14ca0 61 64 64 65 64 20 74 6f 20 74 68 65 20 73 79 6e added to the syn
14ca1 63 5f 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 c_flags argument
14ca2 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 57 61 of.** sqlite3Wa
14ca3 6c 46 72 61 6d 65 73 28 29 3a 0a 2a 2f 0a 23 64 lFrames():.*/.#d
14ca4 65 66 69 6e 65 20 57 41 4c 5f 53 59 4e 43 5f 54 efine WAL_SYNC_T
14ca5 52 41 4e 53 41 43 54 49 4f 4e 53 20 20 30 78 32 RANSACTIONS 0x2
14ca6 30 20 20 20 2f 2a 20 53 79 6e 63 20 61 74 20 74 0 /* Sync at t
14ca7 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 74 he end of each t
14ca8 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 ransaction */.#d
14ca9 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e efine SQLITE_SYN
14caa 43 5f 4d 41 53 4b 20 20 20 20 20 20 20 30 78 31 C_MASK 0x1
14cab 33 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 66 20 3 /* Mask off
14cac 74 68 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f the SQLITE_SYNC_
14cad 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 0a 23 69 66 * values */..#if
14cae 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14caf 57 41 4c 0a 23 20 64 65 66 69 6e 65 20 73 71 6c WAL.# define sql
14cb0 69 74 65 33 57 61 6c 4f 70 65 6e 28 78 2c 79 2c ite3WalOpen(x,y,
14cb1 7a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 z)
14cb2 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 0.# define
14cb3 73 71 6c 69 74 65 33 57 61 6c 4c 69 6d 69 74 28 sqlite3WalLimit(
14cb4 78 2c 79 29 0a 23 20 64 65 66 69 6e 65 20 73 71 x,y).# define sq
14cb5 6c 69 74 65 33 57 61 6c 43 6c 6f 73 65 28 77 2c lite3WalClose(w,
14cb6 78 2c 79 2c 7a 29 20 20 20 20 20 20 20 20 20 20 x,y,z)
14cb7 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 0.# define
14cb8 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67 69 6e sqlite3WalBegin
14cb9 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 ReadTransaction(
14cba 79 2c 7a 29 20 20 20 20 20 30 0a 23 20 64 65 66 y,z) 0.# def
14cbb 69 6e 65 20 73 71 6c 69 74 65 33 57 61 6c 45 6e ine sqlite3WalEn
14cbc 64 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e dReadTransaction
14cbd 28 7a 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c (z).# define sql
14cbe 69 74 65 33 57 61 6c 52 65 61 64 28 76 2c 77 2c ite3WalRead(v,w,
14cbf 78 2c 79 2c 7a 29 20 20 20 20 20 20 20 20 20 20 x,y,z)
14cc0 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 0.# define
14cc1 73 71 6c 69 74 65 33 57 61 6c 44 62 73 69 7a 65 sqlite3WalDbsize
14cc2 28 79 29 20 20 20 20 20 20 20 20 20 20 20 20 20 (y)
14cc3 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 0.# defi
14cc4 6e 65 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67 ne sqlite3WalBeg
14cc5 69 6e 57 72 69 74 65 54 72 61 6e 73 61 63 74 69 inWriteTransacti
14cc6 6f 6e 28 79 29 20 20 20 20 20 20 30 0a 23 20 64 on(y) 0.# d
14cc7 65 66 69 6e 65 20 73 71 6c 69 74 65 33 57 61 6c efine sqlite3Wal
14cc8 45 6e 64 57 72 69 74 65 54 72 61 6e 73 61 63 74 EndWriteTransact
14cc9 69 6f 6e 28 78 29 20 20 20 20 20 20 20 20 30 0a ion(x) 0.
14cca 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
14ccb 57 61 6c 55 6e 64 6f 28 78 2c 79 2c 7a 29 20 20 WalUndo(x,y,z)
14ccc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14ccd 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 0.# define sqli
14cce 74 65 33 57 61 6c 53 61 76 65 70 6f 69 6e 74 28 te3WalSavepoint(
14ccf 79 2c 7a 29 0a 23 20 64 65 66 69 6e 65 20 73 71 y,z).# define sq
14cd0 6c 69 74 65 33 57 61 6c 53 61 76 65 70 6f 69 6e lite3WalSavepoin
14cd1 74 55 6e 64 6f 28 79 2c 7a 29 20 20 20 20 20 20 tUndo(y,z)
14cd2 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 0.# define
14cd3 20 73 71 6c 69 74 65 33 57 61 6c 46 72 61 6d 65 sqlite3WalFrame
14cd4 73 28 75 2c 76 2c 77 2c 78 2c 79 2c 7a 29 20 20 s(u,v,w,x,y,z)
14cd5 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 0.# def
14cd6 69 6e 65 20 73 71 6c 69 74 65 33 57 61 6c 43 68 ine sqlite3WalCh
14cd7 65 63 6b 70 6f 69 6e 74 28 72 2c 73 2c 74 2c 75 eckpoint(r,s,t,u
14cd8 2c 76 2c 77 2c 78 2c 79 2c 7a 29 20 30 0a 23 20 ,v,w,x,y,z) 0.#
14cd9 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 57 61 define sqlite3Wa
14cda 6c 43 61 6c 6c 62 61 63 6b 28 7a 29 20 20 20 20 lCallback(z)
14cdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
14cdc 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
14cdd 33 57 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f 64 3WalExclusiveMod
14cde 65 28 79 2c 7a 29 20 20 20 20 20 20 20 20 20 20 e(y,z)
14cdf 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 0.# define sql
14ce0 69 74 65 33 57 61 6c 48 65 61 70 4d 65 6d 6f 72 ite3WalHeapMemor
14ce1 79 28 7a 29 20 20 20 20 20 20 20 20 20 20 20 20 y(z)
14ce2 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 0.# define
14ce3 73 71 6c 69 74 65 33 57 61 6c 46 72 61 6d 65 73 sqlite3WalFrames
14ce4 69 7a 65 28 7a 29 20 20 20 20 20 20 20 20 20 20 ize(z)
14ce5 20 20 20 20 20 20 20 20 30 0a 23 65 6c 73 65 0a 0.#else.
14ce6 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 53 41 56 .#define WAL_SAV
14ce7 45 50 4f 49 4e 54 5f 4e 44 41 54 41 20 34 0a 0a EPOINT_NDATA 4..
14ce8 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f /* Connection to
14ce9 20 61 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c a write-ahead l
14cea 6f 67 20 28 57 41 4c 29 20 66 69 6c 65 2e 20 0a og (WAL) file. .
14ceb 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 ** There is one
14cec 6f 62 6a 65 63 74 20 6f 66 20 74 68 69 73 20 74 object of this t
14ced 79 70 65 20 66 6f 72 20 65 61 63 68 20 70 61 67 ype for each pag
14cee 65 72 2e 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 er. .*/.typedef
14cef 73 74 72 75 63 74 20 57 61 6c 20 57 61 6c 3b 0a struct Wal Wal;.
14cf0 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20 63 6c 6f ./* Open and clo
14cf1 73 65 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 se a connection
14cf2 74 6f 20 61 20 77 72 69 74 65 2d 61 68 65 61 64 to a write-ahead
14cf3 20 6c 6f 67 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f log. */.SQLITE_
14cf4 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14cf5 74 65 33 57 61 6c 4f 70 65 6e 28 73 71 6c 69 74 te3WalOpen(sqlit
14cf6 65 33 5f 76 66 73 2a 2c 20 73 71 6c 69 74 65 33 e3_vfs*, sqlite3
14cf7 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 63 68 _file*, const ch
14cf8 61 72 20 2a 2c 20 69 6e 74 2c 20 69 36 34 2c 20 ar *, int, i64,
14cf9 57 61 6c 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Wal**);.SQLITE_P
14cfa 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
14cfb 65 33 57 61 6c 43 6c 6f 73 65 28 57 61 6c 20 2a e3WalClose(Wal *
14cfc 70 57 61 6c 2c 20 69 6e 74 20 73 79 6e 63 5f 66 pWal, int sync_f
14cfd 6c 61 67 73 2c 20 69 6e 74 2c 20 75 38 20 2a 29 lags, int, u8 *)
14cfe 3b 0a 0a 2f 2a 20 53 65 74 20 74 68 65 20 6c 69 ;../* Set the li
14cff 6d 69 74 69 6e 67 20 73 69 7a 65 20 6f 66 20 61 miting size of a
14d00 20 57 41 4c 20 66 69 6c 65 2e 20 2a 2f 0a 53 51 WAL file. */.SQ
14d01 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
14d02 64 20 73 71 6c 69 74 65 33 57 61 6c 4c 69 6d 69 d sqlite3WalLimi
14d03 74 28 57 61 6c 2a 2c 20 69 36 34 29 3b 0a 0a 2f t(Wal*, i64);../
14d04 2a 20 55 73 65 64 20 62 79 20 72 65 61 64 65 72 * Used by reader
14d05 73 20 74 6f 20 6f 70 65 6e 20 28 6c 6f 63 6b 29 s to open (lock)
14d06 20 61 6e 64 20 63 6c 6f 73 65 20 28 75 6e 6c 6f and close (unlo
14d07 63 6b 29 20 61 20 73 6e 61 70 73 68 6f 74 2e 20 ck) a snapshot.
14d08 20 41 20 0a 2a 2a 20 73 6e 61 70 73 68 6f 74 20 A .** snapshot
14d09 69 73 20 6c 69 6b 65 20 61 20 72 65 61 64 2d 74 is like a read-t
14d0a 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 74 20 ransaction. It
14d0b 69 73 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 is the state of
14d0c 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
14d0d 61 74 20 61 6e 20 69 6e 73 74 61 6e 74 20 69 6e at an instant in
14d0e 20 74 69 6d 65 2e 20 20 73 71 6c 69 74 65 33 57 time. sqlite3W
14d0f 61 6c 4f 70 65 6e 53 6e 61 70 73 68 6f 74 20 67 alOpenSnapshot g
14d10 65 74 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 ets a read lock
14d11 61 6e 64 0a 2a 2a 20 70 72 65 73 65 72 76 65 73 and.** preserves
14d12 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 the current sta
14d13 74 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f te even if the o
14d14 74 68 65 72 20 74 68 72 65 61 64 73 20 6f 72 20 ther threads or
14d15 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 77 72 69 processes.** wri
14d16 74 65 20 74 6f 20 6f 72 20 63 68 65 63 6b 70 6f te to or checkpo
14d17 69 6e 74 20 74 68 65 20 57 41 4c 2e 20 20 73 71 int the WAL. sq
14d18 6c 69 74 65 33 57 61 6c 43 6c 6f 73 65 53 6e 61 lite3WalCloseSna
14d19 70 73 68 6f 74 28 29 20 63 6c 6f 73 65 73 20 74 pshot() closes t
14d1a 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f he.** transactio
14d1b 6e 20 61 6e 64 20 72 65 6c 65 61 73 65 73 20 74 n and releases t
14d1c 68 65 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 he lock..*/.SQLI
14d1d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
14d1e 71 6c 69 74 65 33 57 61 6c 42 65 67 69 6e 52 65 qlite3WalBeginRe
14d1f 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 57 61 adTransaction(Wa
14d20 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 20 2a 29 3b l *pWal, int *);
14d21 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14d22 76 6f 69 64 20 73 71 6c 69 74 65 33 57 61 6c 45 void sqlite3WalE
14d23 6e 64 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f ndReadTransactio
14d24 6e 28 57 61 6c 20 2a 70 57 61 6c 29 3b 0a 0a 2f n(Wal *pWal);../
14d25 2a 20 52 65 61 64 20 61 20 70 61 67 65 20 66 72 * Read a page fr
14d26 6f 6d 20 74 68 65 20 77 72 69 74 65 2d 61 68 65 om the write-ahe
14d27 61 64 20 6c 6f 67 2c 20 69 66 20 69 74 20 69 73 ad log, if it is
14d28 20 70 72 65 73 65 6e 74 2e 20 2a 2f 0a 53 51 4c present. */.SQL
14d29 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
14d2a 73 71 6c 69 74 65 33 57 61 6c 52 65 61 64 28 57 sqlite3WalRead(W
14d2b 61 6c 20 2a 70 57 61 6c 2c 20 50 67 6e 6f 20 70 al *pWal, Pgno p
14d2c 67 6e 6f 2c 20 69 6e 74 20 2a 70 49 6e 57 61 6c gno, int *pInWal
14d2d 2c 20 69 6e 74 20 6e 4f 75 74 2c 20 75 38 20 2a , int nOut, u8 *
14d2e 70 4f 75 74 29 3b 0a 0a 2f 2a 20 49 66 20 74 68 pOut);../* If th
14d2f 65 20 57 41 4c 20 69 73 20 6e 6f 74 20 65 6d 70 e WAL is not emp
14d30 74 79 2c 20 72 65 74 75 72 6e 20 74 68 65 20 73 ty, return the s
14d31 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
14d32 61 73 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 ase. */.SQLITE_P
14d33 52 49 56 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 RIVATE Pgno sqli
14d34 74 65 33 57 61 6c 44 62 73 69 7a 65 28 57 61 6c te3WalDbsize(Wal
14d35 20 2a 70 57 61 6c 29 3b 0a 0a 2f 2a 20 4f 62 74 *pWal);../* Obt
14d36 61 69 6e 20 6f 72 20 72 65 6c 65 61 73 65 20 74 ain or release t
14d37 68 65 20 57 52 49 54 45 52 20 6c 6f 63 6b 2e 20 he WRITER lock.
14d38 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14d39 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c E int sqlite3Wal
14d3a 42 65 67 69 6e 57 72 69 74 65 54 72 61 6e 73 61 BeginWriteTransa
14d3b 63 74 69 6f 6e 28 57 61 6c 20 2a 70 57 61 6c 29 ction(Wal *pWal)
14d3c 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
14d3d 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 45 int sqlite3WalE
14d3e 6e 64 57 72 69 74 65 54 72 61 6e 73 61 63 74 69 ndWriteTransacti
14d3f 6f 6e 28 57 61 6c 20 2a 70 57 61 6c 29 3b 0a 0a on(Wal *pWal);..
14d40 2f 2a 20 55 6e 64 6f 20 61 6e 79 20 66 72 61 6d /* Undo any fram
14d41 65 73 20 77 72 69 74 74 65 6e 20 28 62 75 74 20 es written (but
14d42 6e 6f 74 20 63 6f 6d 6d 69 74 74 65 64 29 20 74 not committed) t
14d43 6f 20 74 68 65 20 6c 6f 67 20 2a 2f 0a 53 51 4c o the log */.SQL
14d44 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
14d45 73 71 6c 69 74 65 33 57 61 6c 55 6e 64 6f 28 57 sqlite3WalUndo(W
14d46 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 20 28 2a al *pWal, int (*
14d47 78 55 6e 64 6f 29 28 76 6f 69 64 20 2a 2c 20 50 xUndo)(void *, P
14d48 67 6e 6f 29 2c 20 76 6f 69 64 20 2a 70 55 6e 64 gno), void *pUnd
14d49 6f 43 74 78 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 oCtx);../* Retur
14d4a 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 n an integer tha
14d4b 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 63 75 t records the cu
14d4c 72 72 65 6e 74 20 28 75 6e 63 6f 6d 6d 69 74 74 rrent (uncommitt
14d4d 65 64 29 20 77 72 69 74 65 0a 2a 2a 20 70 6f 73 ed) write.** pos
14d4e 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 57 41 4c ition in the WAL
14d4f 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
14d50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 57 TE void sqlite3W
14d51 61 6c 53 61 76 65 70 6f 69 6e 74 28 57 61 6c 20 alSavepoint(Wal
14d52 2a 70 57 61 6c 2c 20 75 33 32 20 2a 61 57 61 6c *pWal, u32 *aWal
14d53 44 61 74 61 29 3b 0a 0a 2f 2a 20 4d 6f 76 65 20 Data);../* Move
14d54 74 68 65 20 77 72 69 74 65 20 70 6f 73 69 74 69 the write positi
14d55 6f 6e 20 6f 66 20 74 68 65 20 57 41 4c 20 62 61 on of the WAL ba
14d56 63 6b 20 74 6f 20 69 46 72 61 6d 65 2e 20 20 43 ck to iFrame. C
14d57 61 6c 6c 65 64 20 69 6e 0a 2a 2a 20 72 65 73 70 alled in.** resp
14d58 6f 6e 73 65 20 74 6f 20 61 20 52 4f 4c 4c 42 41 onse to a ROLLBA
14d59 43 4b 20 54 4f 20 63 6f 6d 6d 61 6e 64 2e 20 2a CK TO command. *
14d5a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
14d5b 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 53 int sqlite3WalS
14d5c 61 76 65 70 6f 69 6e 74 55 6e 64 6f 28 57 61 6c avepointUndo(Wal
14d5d 20 2a 70 57 61 6c 2c 20 75 33 32 20 2a 61 57 61 *pWal, u32 *aWa
14d5e 6c 44 61 74 61 29 3b 0a 0a 2f 2a 20 57 72 69 74 lData);../* Writ
14d5f 65 20 61 20 66 72 61 6d 65 20 6f 72 20 66 72 61 e a frame or fra
14d60 6d 65 73 20 74 6f 20 74 68 65 20 6c 6f 67 2e 20 mes to the log.
14d61 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14d62 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c E int sqlite3Wal
14d63 46 72 61 6d 65 73 28 57 61 6c 20 2a 70 57 61 6c Frames(Wal *pWal
14d64 2c 20 69 6e 74 2c 20 50 67 48 64 72 20 2a 2c 20 , int, PgHdr *,
14d65 50 67 6e 6f 2c 20 69 6e 74 2c 20 69 6e 74 29 3b Pgno, int, int);
14d66 0a 0a 2f 2a 20 43 6f 70 79 20 70 61 67 65 73 20 ../* Copy pages
14d67 66 72 6f 6d 20 74 68 65 20 6c 6f 67 20 74 6f 20 from the log to
14d68 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
14d69 65 20 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 e */ .SQLITE_PRI
14d6a 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
14d6b 57 61 6c 43 68 65 63 6b 70 6f 69 6e 74 28 0a 20 WalCheckpoint(.
14d6c 20 57 61 6c 20 2a 70 57 61 6c 2c 20 20 20 20 20 Wal *pWal,
14d6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14d6e 20 2f 2a 20 57 72 69 74 65 2d 61 68 65 61 64 20 /* Write-ahead
14d6f 6c 6f 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a log connection *
14d70 2f 0a 20 20 69 6e 74 20 65 4d 6f 64 65 2c 20 20 /. int eMode,
14d71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14d72 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 50 41 /* One of PA
14d73 53 53 49 56 45 2c 20 46 55 4c 4c 20 61 6e 64 20 SSIVE, FULL and
14d74 52 45 53 54 41 52 54 20 2a 2f 0a 20 20 69 6e 74 RESTART */. int
14d75 20 28 2a 78 42 75 73 79 29 28 76 6f 69 64 2a 29 (*xBusy)(void*)
14d76 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
14d77 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c Function to call
14d78 20 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20 when busy */.
14d79 76 6f 69 64 20 2a 70 42 75 73 79 41 72 67 2c 20 void *pBusyArg,
14d7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14d7b 2f 2a 20 43 6f 6e 74 65 78 74 20 61 72 67 75 6d /* Context argum
14d7c 65 6e 74 20 66 6f 72 20 78 42 75 73 79 48 61 6e ent for xBusyHan
14d7d 64 6c 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 79 dler */. int sy
14d7e 6e 63 5f 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nc_flags,
14d7f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
14d80 67 73 20 74 6f 20 73 79 6e 63 20 64 62 20 66 69 gs to sync db fi
14d81 6c 65 20 77 69 74 68 20 28 6f 72 20 30 29 20 2a le with (or 0) *
14d82 2f 0a 20 20 69 6e 74 20 6e 42 75 66 2c 20 20 20 /. int nBuf,
14d83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14d84 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 /* Size of b
14d85 75 66 66 65 72 20 6e 42 75 66 20 2a 2f 0a 20 20 uffer nBuf */.
14d86 75 38 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 u8 *zBuf,
14d87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14d88 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 62 75 66 /* Temporary buf
14d89 66 65 72 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 fer to use */.
14d8a 69 6e 74 20 2a 70 6e 4c 6f 67 2c 20 20 20 20 20 int *pnLog,
14d8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14d8c 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f /* OUT: Number o
14d8d 66 20 66 72 61 6d 65 73 20 69 6e 20 57 41 4c 20 f frames in WAL
14d8e 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6b 70 74 */. int *pnCkpt
14d8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14d90 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d /* OUT: Num
14d91 62 65 72 20 6f 66 20 62 61 63 6b 66 69 6c 6c 65 ber of backfille
14d92 64 20 66 72 61 6d 65 73 20 69 6e 20 57 41 4c 20 d frames in WAL
14d93 2a 2f 0a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e */.);../* Return
14d94 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 70 61 the value to pa
14d95 73 73 20 74 6f 20 61 20 73 71 6c 69 74 65 33 5f ss to a sqlite3_
14d96 77 61 6c 5f 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 wal_hook callbac
14d97 6b 2c 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 k, the.** number
14d98 20 6f 66 20 66 72 61 6d 65 73 20 69 6e 20 74 68 of frames in th
14d99 65 20 57 41 4c 20 61 74 20 74 68 65 20 70 6f 69 e WAL at the poi
14d9a 6e 74 20 6f 66 20 74 68 65 20 6c 61 73 74 20 63 nt of the last c
14d9b 6f 6d 6d 69 74 20 73 69 6e 63 65 0a 2a 2a 20 73 ommit since.** s
14d9c 71 6c 69 74 65 33 57 61 6c 43 61 6c 6c 62 61 63 qlite3WalCallbac
14d9d 6b 28 29 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 k() was called.
14d9e 20 49 66 20 6e 6f 20 63 6f 6d 6d 69 74 73 20 68 If no commits h
14d9f 61 76 65 20 6f 63 63 75 72 72 65 64 20 73 69 6e ave occurred sin
14da0 63 65 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 63 ce.** the last c
14da1 61 6c 6c 2c 20 74 68 65 6e 20 72 65 74 75 72 6e all, then return
14da2 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 0..*/.SQLITE_PR
14da3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
14da4 33 57 61 6c 43 61 6c 6c 62 61 63 6b 28 57 61 6c 3WalCallback(Wal
14da5 20 2a 70 57 61 6c 29 3b 0a 0a 2f 2a 20 54 65 6c *pWal);../* Tel
14da6 6c 20 74 68 65 20 77 61 6c 20 6c 61 79 65 72 20 l the wal layer
14da7 74 68 61 74 20 61 6e 20 45 58 43 4c 55 53 49 56 that an EXCLUSIV
14da8 45 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 E lock has been
14da9 6f 62 74 61 69 6e 65 64 20 28 6f 72 20 72 65 6c obtained (or rel
14daa 65 61 73 65 64 29 0a 2a 2a 20 62 79 20 74 68 65 eased).** by the
14dab 20 70 61 67 65 72 20 6c 61 79 65 72 20 6f 6e 20 pager layer on
14dac 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
14dad 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
14dae 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
14daf 57 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f 64 65 WalExclusiveMode
14db0 28 57 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 20 (Wal *pWal, int
14db1 6f 70 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 op);../* Return
14db2 74 72 75 65 20 69 66 20 74 68 65 20 61 72 67 75 true if the argu
14db3 6d 65 6e 74 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c ment is non-NULL
14db4 20 61 6e 64 20 74 68 65 20 57 41 4c 20 6d 6f 64 and the WAL mod
14db5 75 6c 65 20 69 73 20 75 73 69 6e 67 0a 2a 2a 20 ule is using.**
14db6 68 65 61 70 2d 6d 65 6d 6f 72 79 20 66 6f 72 20 heap-memory for
14db7 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 2e 20 4f the wal-index. O
14db8 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 therwise, if the
14db9 20 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c argument is NUL
14dba 4c 20 6f 72 20 74 68 65 0a 2a 2a 20 57 41 4c 20 L or the.** WAL
14dbb 6d 6f 64 75 6c 65 20 69 73 20 75 73 69 6e 67 20 module is using
14dbc 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 2c 20 72 shared-memory, r
14dbd 65 74 75 72 6e 20 66 61 6c 73 65 2e 20 0a 2a 2f eturn false. .*/
14dbe 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14dbf 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 48 65 int sqlite3WalHe
14dc0 61 70 4d 65 6d 6f 72 79 28 57 61 6c 20 2a 70 57 apMemory(Wal *pW
14dc1 61 6c 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c al);..#ifdef SQL
14dc2 49 54 45 5f 45 4e 41 42 4c 45 5f 5a 49 50 56 46 ITE_ENABLE_ZIPVF
14dc3 53 0a 2f 2a 20 49 66 20 74 68 65 20 57 41 4c 20 S./* If the WAL
14dc4 66 69 6c 65 20 69 73 20 6e 6f 74 20 65 6d 70 74 file is not empt
14dc5 79 2c 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 y, return the nu
14dc6 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
14dc7 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 73 74 6f 72 content.** stor
14dc8 65 64 20 69 6e 20 65 61 63 68 20 66 72 61 6d 65 ed in each frame
14dc9 20 28 69 2e 65 2e 20 74 68 65 20 64 62 20 70 61 (i.e. the db pa
14dca 67 65 2d 73 69 7a 65 20 77 68 65 6e 20 74 68 65 ge-size when the
14dcb 20 57 41 4c 20 77 61 73 20 63 72 65 61 74 65 64 WAL was created
14dcc 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
14dcd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
14dce 57 61 6c 46 72 61 6d 65 73 69 7a 65 28 57 61 6c WalFramesize(Wal
14dcf 20 2a 70 57 61 6c 29 3b 0a 23 65 6e 64 69 66 0a *pWal);.#endif.
14dd0 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64 65 .#endif /* ifnde
14dd1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 f SQLITE_OMIT_WA
14dd2 4c 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 5f L */.#endif /* _
14dd3 57 41 4c 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a WAL_H_ */../****
14dd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
14dd5 66 20 77 61 6c 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a f wal.h ********
14dd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14dd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14dd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
14dd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
14dda 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
14ddb 65 66 74 20 6f 66 66 20 69 6e 20 70 61 67 65 72 eft off in pager
14ddc 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
14ddd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a *********/.../**
14dde 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14ddf 2a 20 4e 4f 54 45 53 20 4f 4e 20 54 48 45 20 44 * NOTES ON THE D
14de0 45 53 49 47 4e 20 4f 46 20 54 48 45 20 50 41 47 ESIGN OF THE PAG
14de1 45 52 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ER *************
14de2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
14de3 2a 20 54 68 69 73 20 63 6f 6d 6d 65 6e 74 20 62 * This comment b
14de4 6c 6f 63 6b 20 64 65 73 63 72 69 62 65 73 20 69 lock describes i
14de5 6e 76 61 72 69 61 6e 74 73 20 74 68 61 74 20 68 nvariants that h
14de6 6f 6c 64 20 77 68 65 6e 20 75 73 69 6e 67 20 61 old when using a
14de7 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6a 6f 75 rollback.** jou
14de8 72 6e 61 6c 2e 20 20 54 68 65 73 65 20 69 6e 76 rnal. These inv
14de9 61 72 69 61 6e 74 73 20 64 6f 20 6e 6f 74 20 61 ariants do not a
14dea 70 70 6c 79 20 66 6f 72 20 6a 6f 75 72 6e 61 6c pply for journal
14deb 5f 6d 6f 64 65 3d 57 41 4c 2c 0a 2a 2a 20 6a 6f _mode=WAL,.** jo
14dec 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f 52 urnal_mode=MEMOR
14ded 59 2c 20 6f 72 20 6a 6f 75 72 6e 61 6c 5f 6d 6f Y, or journal_mo
14dee 64 65 3d 4f 46 46 2e 0a 2a 2a 0a 2a 2a 20 57 69 de=OFF..**.** Wi
14def 74 68 69 6e 20 74 68 69 73 20 63 6f 6d 6d 65 6e thin this commen
14df0 74 20 62 6c 6f 63 6b 2c 20 61 20 70 61 67 65 20 t block, a page
14df1 69 73 20 64 65 65 6d 65 64 20 74 6f 20 68 61 76 is deemed to hav
14df2 65 20 62 65 65 6e 20 73 79 6e 63 65 64 0a 2a 2a e been synced.**
14df3 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61 automatically a
14df4 73 20 73 6f 6f 6e 20 61 73 20 69 74 20 69 73 20 s soon as it is
14df5 77 72 69 74 74 65 6e 20 77 68 65 6e 20 50 52 41 written when PRA
14df6 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d GMA synchronous=
14df7 4f 46 46 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 OFF..** Otherwis
14df8 65 2c 20 74 68 65 20 70 61 67 65 20 69 73 20 6e e, the page is n
14df9 6f 74 20 73 79 6e 63 65 64 20 75 6e 74 69 6c 20 ot synced until
14dfa 74 68 65 20 78 53 79 6e 63 20 6d 65 74 68 6f 64 the xSync method
14dfb 20 6f 66 20 74 68 65 20 56 46 53 0a 2a 2a 20 69 of the VFS.** i
14dfc 73 20 63 61 6c 6c 65 64 20 73 75 63 63 65 73 73 s called success
14dfd 66 75 6c 6c 79 20 6f 6e 20 74 68 65 20 66 69 6c fully on the fil
14dfe 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
14dff 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 page..**.** Def
14e00 69 6e 69 74 69 6f 6e 3a 20 20 41 20 70 61 67 65 inition: A page
14e01 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
14e02 20 66 69 6c 65 20 69 73 20 73 61 69 64 20 74 6f file is said to
14e03 20 62 65 20 22 6f 76 65 72 77 72 69 74 65 61 62 be "overwriteab
14e04 6c 65 22 20 69 66 0a 2a 2a 20 6f 6e 65 20 6f 72 le" if.** one or
14e05 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 66 6f 6c more of the fol
14e06 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 20 lowing are true
14e07 61 62 6f 75 74 20 74 68 65 20 70 61 67 65 3a 0a about the page:.
14e08 2a 2a 20 0a 2a 2a 20 20 20 20 20 28 61 29 20 20 ** .** (a)
14e09 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e The original con
14e0a 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 tent of the page
14e0b 20 61 73 20 69 74 20 77 61 73 20 61 74 20 74 68 as it was at th
14e0c 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a e beginning of.*
14e0d 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74 * the t
14e0e 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 62 ransaction has b
14e0f 65 65 6e 20 77 72 69 74 74 65 6e 20 69 6e 74 6f een written into
14e10 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
14e11 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 20 20 20 urnal and.**
14e12 20 20 20 20 20 20 73 79 6e 63 65 64 2e 0a 2a 2a synced..**
14e13 20 0a 2a 2a 20 20 20 20 20 28 62 29 20 20 54 68 .** (b) Th
14e14 65 20 70 61 67 65 20 77 61 73 20 61 20 66 72 65 e page was a fre
14e15 65 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 elist leaf page
14e16 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 at the start of
14e17 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
14e18 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 28 63 29 20 .** .** (c)
14e19 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 The page number
14e1a 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
14e1b 20 74 68 65 20 6c 61 72 67 65 73 74 20 70 61 67 the largest pag
14e1c 65 20 74 68 61 74 20 65 78 69 73 74 65 64 20 69 e that existed i
14e1d 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 n.** th
14e1e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
14e1f 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 at the start of
14e20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
14e21 0a 2a 2a 20 0a 2a 2a 20 28 31 29 20 41 20 70 61 .** .** (1) A pa
14e22 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ge of the databa
14e23 73 65 20 66 69 6c 65 20 69 73 20 6e 65 76 65 72 se file is never
14e24 20 6f 76 65 72 77 72 69 74 74 65 6e 20 75 6e 6c overwritten unl
14e25 65 73 73 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a ess one of the.*
14e26 2a 20 20 20 20 20 66 6f 6c 6c 6f 77 69 6e 67 20 * following
14e27 61 72 65 20 74 72 75 65 3a 0a 2a 2a 20 0a 2a 2a are true:.** .**
14e28 20 20 20 20 20 28 61 29 20 54 68 65 20 70 61 67 (a) The pag
14e29 65 20 61 6e 64 20 61 6c 6c 20 6f 74 68 65 72 20 e and all other
14e2a 70 61 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d pages on the sam
14e2b 65 20 73 65 63 74 6f 72 20 61 72 65 20 6f 76 65 e sector are ove
14e2c 72 77 72 69 74 65 61 62 6c 65 2e 0a 2a 2a 20 0a rwriteable..** .
14e2d 2a 2a 20 20 20 20 20 28 62 29 20 54 68 65 20 61 ** (b) The a
14e2e 74 6f 6d 69 63 20 70 61 67 65 20 77 72 69 74 65 tomic page write
14e2f 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 optimization is
14e30 20 65 6e 61 62 6c 65 64 2c 20 61 6e 64 20 74 68 enabled, and th
14e31 65 20 65 6e 74 69 72 65 0a 2a 2a 20 20 20 20 20 e entire.**
14e32 20 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 transaction
14e33 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 75 other than the u
14e34 70 64 61 74 65 20 6f 66 20 74 68 65 20 74 72 61 pdate of the tra
14e35 6e 73 61 63 74 69 6f 6e 20 73 65 71 75 65 6e 63 nsaction sequenc
14e36 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 6e 75 6d e.** num
14e37 62 65 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 ber consists of
14e38 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 63 68 a single page ch
14e39 61 6e 67 65 2e 0a 2a 2a 20 0a 2a 2a 20 28 32 29 ange..** .** (2)
14e3a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 The content of
14e3b 61 20 70 61 67 65 20 77 72 69 74 74 65 6e 20 69 a page written i
14e3c 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b nto the rollback
14e3d 20 6a 6f 75 72 6e 61 6c 20 65 78 61 63 74 6c 79 journal exactly
14e3e 20 6d 61 74 63 68 65 73 0a 2a 2a 20 20 20 20 20 matches.**
14e3f 62 6f 74 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 both the content
14e40 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
14e41 20 77 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 when the rollba
14e42 63 6b 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 77 ck journal was w
14e43 72 69 74 74 65 6e 0a 2a 2a 20 20 20 20 20 61 6e ritten.** an
14e44 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 69 6e d the content in
14e45 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 74 the database at
14e46 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
14e47 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a f the current.**
14e48 20 20 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e transaction
14e49 2e 0a 2a 2a 20 0a 2a 2a 20 28 33 29 20 57 72 69 ..** .** (3) Wri
14e4a 74 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 tes to the datab
14e4b 61 73 65 20 66 69 6c 65 20 61 72 65 20 61 6e 20 ase file are an
14e4c 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 integer multiple
14e4d 20 6f 66 20 74 68 65 20 70 61 67 65 20 73 69 7a of the page siz
14e4e 65 0a 2a 2a 20 20 20 20 20 69 6e 20 6c 65 6e 67 e.** in leng
14e4f 74 68 20 61 6e 64 20 61 72 65 20 61 6c 69 67 6e th and are align
14e50 65 64 20 6f 6e 20 61 20 70 61 67 65 20 62 6f 75 ed on a page bou
14e51 6e 64 61 72 79 2e 0a 2a 2a 20 0a 2a 2a 20 28 34 ndary..** .** (4
14e52 29 20 52 65 61 64 73 20 66 72 6f 6d 20 74 68 65 ) Reads from the
14e53 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
14e54 72 65 20 65 69 74 68 65 72 20 61 6c 69 67 6e 65 re either aligne
14e55 64 20 6f 6e 20 61 20 70 61 67 65 20 62 6f 75 6e d on a page boun
14e56 64 61 72 79 20 61 6e 64 0a 2a 2a 20 20 20 20 20 dary and.**
14e57 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 an integer multi
14e58 70 6c 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 ple of the page
14e59 73 69 7a 65 20 69 6e 20 6c 65 6e 67 74 68 20 6f size in length o
14e5a 72 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d r are taken from
14e5b 20 74 68 65 0a 2a 2a 20 20 20 20 20 66 69 72 73 the.** firs
14e5c 74 20 31 30 30 20 62 79 74 65 73 20 6f 66 20 74 t 100 bytes of t
14e5d 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
14e5e 2e 0a 2a 2a 20 0a 2a 2a 20 28 35 29 20 41 6c 6c ..** .** (5) All
14e5f 20 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 64 writes to the d
14e60 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 atabase file are
14e61 20 73 79 6e 63 65 64 20 70 72 69 6f 72 20 74 6f synced prior to
14e62 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
14e63 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 62 65 69 urnal.** bei
14e64 6e 67 20 64 65 6c 65 74 65 64 2c 20 74 72 75 6e ng deleted, trun
14e65 63 61 74 65 64 2c 20 6f 72 20 7a 65 72 6f 65 64 cated, or zeroed
14e66 2e 0a 2a 2a 20 0a 2a 2a 20 28 36 29 20 49 66 20 ..** .** (6) If
14e67 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
14e68 20 66 69 6c 65 20 69 73 20 75 73 65 64 2c 20 74 file is used, t
14e69 68 65 6e 20 61 6c 6c 20 77 72 69 74 65 73 20 74 hen all writes t
14e6a 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
14e6b 69 6c 65 0a 2a 2a 20 20 20 20 20 61 72 65 20 73 ile.** are s
14e6c 79 6e 63 65 64 20 70 72 69 6f 72 20 74 6f 20 74 ynced prior to t
14e6d 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
14e6e 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e l being deleted.
14e6f 0a 2a 2a 20 0a 2a 2a 20 44 65 66 69 6e 69 74 69 .** .** Definiti
14e70 6f 6e 3a 20 54 77 6f 20 64 61 74 61 62 61 73 65 on: Two database
14e71 73 20 28 6f 72 20 74 68 65 20 73 61 6d 65 20 64 s (or the same d
14e72 61 74 61 62 61 73 65 20 61 74 20 74 77 6f 20 70 atabase at two p
14e73 6f 69 6e 74 73 20 69 74 20 74 69 6d 65 29 0a 2a oints it time).*
14e74 2a 20 61 72 65 20 73 61 69 64 20 74 6f 20 62 65 * are said to be
14e75 20 22 6c 6f 67 69 63 61 6c 6c 79 20 65 71 75 69 "logically equi
14e76 76 61 6c 65 6e 74 22 20 69 66 20 74 68 65 79 20 valent" if they
14e77 67 69 76 65 20 74 68 65 20 73 61 6d 65 20 61 6e give the same an
14e78 73 77 65 72 20 74 6f 0a 2a 2a 20 61 6c 6c 20 71 swer to.** all q
14e79 75 65 72 69 65 73 2e 20 20 4e 6f 74 65 20 69 6e ueries. Note in
14e7a 20 70 61 72 74 69 63 75 6c 61 72 20 74 68 65 20 particular the
14e7b 63 6f 6e 74 65 6e 74 20 6f 66 20 66 72 65 65 6c content of freel
14e7c 69 73 74 20 6c 65 61 66 0a 2a 2a 20 70 61 67 65 ist leaf.** page
14e7d 73 20 63 61 6e 20 62 65 20 63 68 61 6e 67 65 64 s can be changed
14e7e 20 61 72 62 69 74 61 72 69 6c 79 20 77 69 74 68 arbitarily with
14e7f 6f 75 74 20 65 66 66 65 63 74 69 6e 67 20 74 68 out effecting th
14e80 65 20 6c 6f 67 69 63 61 6c 20 65 71 75 69 76 61 e logical equiva
14e81 6c 65 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 lence.** of the
14e82 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 0a 2a 2a database..** .**
14e83 20 28 37 29 20 41 74 20 61 6e 79 20 74 69 6d 65 (7) At any time
14e84 2c 20 69 66 20 61 6e 79 20 73 75 62 73 65 74 2c , if any subset,
14e85 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 65 including the e
14e86 6d 70 74 79 20 73 65 74 20 61 6e 64 20 74 68 65 mpty set and the
14e87 20 74 6f 74 61 6c 20 73 65 74 2c 0a 2a 2a 20 20 total set,.**
14e88 20 20 20 6f 66 20 74 68 65 20 75 6e 73 79 6e 63 of the unsync
14e89 65 64 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 ed changes to a
14e8a 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
14e8b 20 61 72 65 20 72 65 6d 6f 76 65 64 20 61 6e 64 are removed and
14e8c 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 the .** jou
14e8d 72 6e 61 6c 20 69 73 20 72 6f 6c 6c 65 64 20 62 rnal is rolled b
14e8e 61 63 6b 2c 20 74 68 65 20 72 65 73 75 6c 74 69 ack, the resulti
14e8f 6e 67 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ng database file
14e90 20 77 69 6c 6c 20 62 65 20 6c 6f 67 69 63 61 6c will be logical
14e91 0a 2a 2a 20 20 20 20 20 65 71 75 69 76 61 6c 65 .** equivale
14e92 6e 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 nt to the databa
14e93 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 62 se file at the b
14e94 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
14e95 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 transaction..**
14e96 0a 2a 2a 20 28 38 29 20 57 68 65 6e 20 61 20 74 .** (8) When a t
14e97 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f ransaction is ro
14e98 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 20 78 lled back, the x
14e99 54 72 75 6e 63 61 74 65 20 6d 65 74 68 6f 64 20 Truncate method
14e9a 6f 66 20 74 68 65 20 56 46 53 0a 2a 2a 20 20 20 of the VFS.**
14e9b 20 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 is called to r
14e9c 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 estore the datab
14e9d 61 73 65 20 66 69 6c 65 20 74 6f 20 74 68 65 20 ase file to the
14e9e 73 61 6d 65 20 73 69 7a 65 20 69 74 20 77 61 73 same size it was
14e9f 20 61 74 0a 2a 2a 20 20 20 20 20 74 68 65 20 62 at.** the b
14ea0 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
14ea1 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 28 49 transaction. (I
14ea2 6e 20 73 6f 6d 65 20 56 46 53 65 73 2c 20 74 68 n some VFSes, th
14ea3 65 20 78 54 72 75 6e 63 61 74 65 0a 2a 2a 20 20 e xTruncate.**
14ea4 20 20 20 6d 65 74 68 6f 64 20 69 73 20 61 20 6e method is a n
14ea5 6f 2d 6f 70 2c 20 62 75 74 20 74 68 61 74 20 64 o-op, but that d
14ea6 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 oes not change t
14ea7 68 65 20 66 61 63 74 20 74 68 65 20 53 51 4c 69 he fact the SQLi
14ea8 74 65 20 77 69 6c 6c 0a 2a 2a 20 20 20 20 20 69 te will.** i
14ea9 6e 76 6f 6b 65 20 69 74 2e 29 0a 2a 2a 20 0a 2a nvoke it.).** .*
14eaa 2a 20 28 39 29 20 57 68 65 6e 65 76 65 72 20 74 * (9) Whenever t
14eab 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
14eac 20 69 73 20 6d 6f 64 69 66 69 65 64 2c 20 61 74 is modified, at
14ead 20 6c 65 61 73 74 20 6f 6e 65 20 62 69 74 20 69 least one bit i
14eae 6e 20 74 68 65 20 72 61 6e 67 65 0a 2a 2a 20 20 n the range.**
14eaf 20 20 20 6f 66 20 62 79 74 65 73 20 66 72 6f 6d of bytes from
14eb0 20 32 34 20 74 68 72 6f 75 67 68 20 33 39 20 69 24 through 39 i
14eb1 6e 63 6c 75 73 69 76 65 20 77 69 6c 6c 20 62 65 nclusive will be
14eb2 20 63 68 61 6e 67 65 64 20 70 72 69 6f 72 20 74 changed prior t
14eb3 6f 20 72 65 6c 65 61 73 69 6e 67 0a 2a 2a 20 20 o releasing.**
14eb4 20 20 20 74 68 65 20 45 58 43 4c 55 53 49 56 45 the EXCLUSIVE
14eb5 20 6c 6f 63 6b 2c 20 74 68 75 73 20 73 69 67 6e lock, thus sign
14eb6 61 6c 69 6e 67 20 6f 74 68 65 72 20 63 6f 6e 6e aling other conn
14eb7 65 63 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 73 ections on the s
14eb8 61 6d 65 0a 2a 2a 20 20 20 20 20 64 61 74 61 62 ame.** datab
14eb9 61 73 65 20 74 6f 20 66 6c 75 73 68 20 74 68 65 ase to flush the
14eba 69 72 20 63 61 63 68 65 73 2e 0a 2a 2a 0a 2a 2a ir caches..**.**
14ebb 20 28 31 30 29 20 54 68 65 20 70 61 74 74 65 72 (10) The patter
14ebc 6e 20 6f 66 20 62 69 74 73 20 69 6e 20 62 79 74 n of bits in byt
14ebd 65 73 20 32 34 20 74 68 72 6f 75 67 68 20 33 39 es 24 through 39
14ebe 20 73 68 61 6c 6c 20 6e 6f 74 20 72 65 70 65 61 shall not repea
14ebf 74 20 69 6e 20 6c 65 73 73 0a 2a 2a 20 20 20 20 t in less.**
14ec0 20 20 74 68 61 6e 20 6f 6e 65 20 62 69 6c 6c 69 than one billi
14ec1 6f 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e on transactions.
14ec2 0a 2a 2a 0a 2a 2a 20 28 31 31 29 20 41 20 64 61 .**.** (11) A da
14ec3 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 77 tabase file is w
14ec4 65 6c 6c 2d 66 6f 72 6d 65 64 20 61 74 20 74 68 ell-formed at th
14ec5 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 e beginning and
14ec6 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f at the conclusio
14ec7 6e 0a 2a 2a 20 20 20 20 20 20 6f 66 20 65 76 65 n.** of eve
14ec8 72 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a ry transaction..
14ec9 2a 2a 0a 2a 2a 20 28 31 32 29 20 41 6e 20 45 58 **.** (12) An EX
14eca 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 CLUSIVE lock is
14ecb 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 held on the data
14ecc 62 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 77 base file when w
14ecd 72 69 74 69 6e 67 20 74 6f 0a 2a 2a 20 20 20 20 riting to.**
14ece 20 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 the database f
14ecf 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 28 31 33 29 20 ile..**.** (13)
14ed0 41 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 69 73 A SHARED lock is
14ed1 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 held on the dat
14ed2 61 62 61 73 65 20 66 69 6c 65 20 77 68 69 6c 65 abase file while
14ed3 20 72 65 61 64 69 6e 67 20 61 6e 79 0a 2a 2a 20 reading any.**
14ed4 20 20 20 20 20 63 6f 6e 74 65 6e 74 20 6f 75 74 content out
14ed5 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
14ed6 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a file..**.******
14ed7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14ed8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14ed9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14eda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14edb 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a ********/../*.**
14edc 20 4d 61 63 72 6f 73 20 66 6f 72 20 74 72 6f 75 Macros for trou
14edd 62 6c 65 73 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f bleshooting. No
14ede 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 rmally turned of
14edf 66 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 f.*/.#if 0.int s
14ee0 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 63 65 qlite3PagerTrace
14ee1 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 =1; /* True to
14ee2 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a enable tracing *
14ee3 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 /.#define sqlite
14ee4 33 44 65 62 75 67 50 72 69 6e 74 66 20 70 72 69 3DebugPrintf pri
14ee5 6e 74 66 0a 23 64 65 66 69 6e 65 20 50 41 47 45 ntf.#define PAGE
14ee6 52 54 52 41 43 45 28 58 29 20 20 20 20 20 69 66 RTRACE(X) if
14ee7 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 ( sqlite3PagerTr
14ee8 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33 44 65 ace ){ sqlite3De
14ee9 62 75 67 50 72 69 6e 74 66 20 58 3b 20 7d 0a 23 bugPrintf X; }.#
14eea 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 50 41 47 else.#define PAG
14eeb 45 52 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 ERTRACE(X).#endi
14eec 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c f../*.** The fol
14eed 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 63 72 6f lowing two macro
14eee 73 20 61 72 65 20 75 73 65 64 20 77 69 74 68 69 s are used withi
14eef 6e 20 74 68 65 20 50 41 47 45 52 54 52 41 43 45 n the PAGERTRACE
14ef0 28 29 20 6d 61 63 72 6f 73 20 61 62 6f 76 65 0a () macros above.
14ef1 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f 75 74 20 ** to print out
14ef2 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 73 file-descriptors
14ef3 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45 52 49 44 . .**.** PAGERID
14ef4 28 29 20 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 () takes a point
14ef5 65 72 20 74 6f 20 61 20 50 61 67 65 72 20 73 74 er to a Pager st
14ef6 72 75 63 74 20 61 73 20 69 74 73 20 61 72 67 75 ruct as its argu
14ef7 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 61 73 73 ment. The.** ass
14ef8 6f 63 69 61 74 65 64 20 66 69 6c 65 2d 64 65 73 ociated file-des
14ef9 63 72 69 70 74 6f 72 20 69 73 20 72 65 74 75 72 criptor is retur
14efa 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44 4c 45 49 ned. FILEHANDLEI
14efb 44 28 29 20 74 61 6b 65 73 20 61 6e 20 73 71 6c D() takes an sql
14efc 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 73 74 72 ite3_file.** str
14efd 75 63 74 20 61 73 20 69 74 73 20 61 72 67 75 6d uct as its argum
14efe 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ent..*/.#define
14eff 50 41 47 45 52 49 44 28 70 29 20 28 28 69 6e 74 PAGERID(p) ((int
14f00 29 28 70 2d 3e 66 64 29 29 0a 23 64 65 66 69 6e )(p->fd)).#defin
14f01 65 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 66 e FILEHANDLEID(f
14f02 64 29 20 28 28 69 6e 74 29 66 64 29 0a 0a 2f 2a d) ((int)fd)../*
14f03 0a 2a 2a 20 54 68 65 20 50 61 67 65 72 2e 65 53 .** The Pager.eS
14f04 74 61 74 65 20 76 61 72 69 61 62 6c 65 20 73 74 tate variable st
14f05 6f 72 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 ores the current
14f06 20 27 73 74 61 74 65 27 20 6f 66 20 61 20 70 61 'state' of a pa
14f07 67 65 72 2e 20 41 0a 2a 2a 20 70 61 67 65 72 20 ger. A.** pager
14f08 6d 61 79 20 62 65 20 69 6e 20 61 6e 79 20 6f 6e may be in any on
14f09 65 20 6f 66 20 74 68 65 20 73 65 76 65 6e 20 73 e of the seven s
14f0a 74 61 74 65 73 20 73 68 6f 77 6e 20 69 6e 20 74 tates shown in t
14f0b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 he following.**
14f0c 73 74 61 74 65 20 64 69 61 67 72 61 6d 2e 0a 2a state diagram..*
14f0d 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 *.**
14f0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14f0f 4f 50 45 4e 20 3c 2d 2d 2d 2d 2d 2d 2b 2d 2d 2d OPEN <------+---
14f10 2d 2d 2d 2b 0a 2a 2a 20 20 20 20 20 20 20 20 20 ---+.**
14f11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14f12 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 20 7c | |
14f13 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 |.**
14f14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14f15 20 20 20 20 20 20 20 20 56 20 20 20 20 20 20 20 V
14f16 20 20 7c 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 | |.**
14f17 20 20 20 20 20 20 20 20 20 20 20 20 2b 2d 2d 2d +---
14f18 2d 2d 2d 2d 2d 2d 3e 20 52 45 41 44 45 52 2d 2d ------> READER--
14f19 2d 2d 2d 2d 2d 2b 20 20 20 20 20 20 7c 0a 2a 2a -----+ |.**
14f1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
14f1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 |
14f1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
14f1d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
14f1e 20 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 |
14f1f 20 56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 V
14f20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 |.**
14f21 20 20 20 20 20 7c 3c 2d 2d 2d 2d 2d 2d 2d 57 52 |<-------WR
14f22 49 54 45 52 5f 4c 4f 43 4b 45 44 2d 2d 2d 2d 2d ITER_LOCKED-----
14f23 2d 3e 20 45 52 52 4f 52 0a 2a 2a 20 20 20 20 20 -> ERROR.**
14f24 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 20 20 |
14f25 20 20 20 20 20 20 20 20 20 7c 20 20 20 20 20 20 |
14f26 20 20 20 20 20 20 20 20 20 20 5e 20 20 0a 2a 2a ^ .**
14f27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
14f28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 56 20 V
14f29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
14f2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
14f2b 20 20 7c 3c 2d 2d 2d 2d 2d 2d 57 52 49 54 45 52 |<------WRITER
14f2c 5f 43 41 43 48 45 4d 4f 44 2d 2d 2d 2d 2d 2d 2d _CACHEMOD-------
14f2d 2d 3e 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ->|.**
14f2e 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 20 20 |
14f2f 20 20 20 20 7c 20 20 20 20 20 20 20 20 20 20 20 |
14f30 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 20 |.**
14f31 20 20 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 |
14f32 20 20 20 20 20 20 20 56 20 20 20 20 20 20 20 20 V
14f33 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 |.**
14f34 20 20 20 20 20 20 20 20 20 20 20 7c 3c 2d 2d 2d |<---
14f35 2d 2d 2d 2d 57 52 49 54 45 52 5f 44 42 4d 4f 44 ----WRITER_DBMOD
14f36 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 3e 7c 0a 2a 2a 20 ---------->|.**
14f37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 |
14f38 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 |
14f39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 0a |.
14f3a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
14f3b 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |
14f3c 56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 V
14f3d 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 |.**
14f3e 20 20 20 20 2b 3c 2d 2d 2d 2d 2d 2d 57 52 49 54 +<------WRIT
14f3f 45 52 5f 46 49 4e 49 53 48 45 44 2d 2d 2d 2d 2d ER_FINISHED-----
14f40 2d 2d 2d 3e 2b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4c --->+.**.**.** L
14f41 69 73 74 20 6f 66 20 73 74 61 74 65 20 74 72 61 ist of state tra
14f42 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nsitions and the
14f43 20 43 20 5b 66 75 6e 63 74 69 6f 6e 5d 20 74 68 C [function] th
14f44 61 74 20 70 65 72 66 6f 72 6d 73 20 65 61 63 68 at performs each
14f45 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 4f 50 45 4e 20 :.** .** OPEN
14f46 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 3e 20 ->
14f47 52 45 41 44 45 52 20 20 20 20 20 20 20 20 20 20 READER
14f48 20 20 20 20 5b 73 71 6c 69 74 65 33 50 61 67 65 [sqlite3Page
14f49 72 53 68 61 72 65 64 4c 6f 63 6b 5d 0a 2a 2a 20 rSharedLock].**
14f4a 20 20 52 45 41 44 45 52 20 20 20 20 20 20 20 20 READER
14f4b 20 20 20 20 2d 3e 20 4f 50 45 4e 20 20 20 20 20 -> OPEN
14f4c 20 20 20 20 20 20 20 20 20 20 20 5b 70 61 67 65 [page
14f4d 72 5f 75 6e 6c 6f 63 6b 5d 0a 2a 2a 0a 2a 2a 20 r_unlock].**.**
14f4e 20 20 52 45 41 44 45 52 20 20 20 20 20 20 20 20 READER
14f4f 20 20 20 20 2d 3e 20 57 52 49 54 45 52 5f 4c 4f -> WRITER_LO
14f50 43 4b 45 44 20 20 20 20 20 20 20 5b 73 71 6c 69 CKED [sqli
14f51 74 65 33 50 61 67 65 72 42 65 67 69 6e 5d 0a 2a te3PagerBegin].*
14f52 2a 20 20 20 57 52 49 54 45 52 5f 4c 4f 43 4b 45 * WRITER_LOCKE
14f53 44 20 20 20 20 20 2d 3e 20 57 52 49 54 45 52 5f D -> WRITER_
14f54 43 41 43 48 45 4d 4f 44 20 20 20 20 20 5b 70 61 CACHEMOD [pa
14f55 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c ger_open_journal
14f56 5d 0a 2a 2a 20 20 20 57 52 49 54 45 52 5f 43 41 ].** WRITER_CA
14f57 43 48 45 4d 4f 44 20 20 20 2d 3e 20 57 52 49 54 CHEMOD -> WRIT
14f58 45 52 5f 44 42 4d 4f 44 20 20 20 20 20 20 20 20 ER_DBMOD
14f59 5b 73 79 6e 63 4a 6f 75 72 6e 61 6c 5d 0a 2a 2a [syncJournal].**
14f5a 20 20 20 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 WRITER_DBMOD
14f5b 20 20 20 20 20 2d 3e 20 57 52 49 54 45 52 5f 46 -> WRITER_F
14f5c 49 4e 49 53 48 45 44 20 20 20 20 20 5b 73 71 6c INISHED [sql
14f5d 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 ite3PagerCommitP
14f5e 68 61 73 65 4f 6e 65 5d 0a 2a 2a 20 20 20 57 52 haseOne].** WR
14f5f 49 54 45 52 5f 2a 2a 2a 20 20 20 20 20 20 20 20 ITER_***
14f60 2d 3e 20 52 45 41 44 45 52 20 20 20 20 20 20 20 -> READER
14f61 20 20 20 20 20 20 20 5b 70 61 67 65 72 5f 65 6e [pager_en
14f62 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 5d 0a 2a d_transaction].*
14f63 2a 0a 2a 2a 20 20 20 57 52 49 54 45 52 5f 2a 2a *.** WRITER_**
14f64 2a 20 20 20 20 20 20 20 20 2d 3e 20 45 52 52 4f * -> ERRO
14f65 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 R
14f66 5b 70 61 67 65 72 5f 65 72 72 6f 72 5d 0a 2a 2a [pager_error].**
14f67 20 20 20 45 52 52 4f 52 20 20 20 20 20 20 20 20 ERROR
14f68 20 20 20 20 20 2d 3e 20 4f 50 45 4e 20 20 20 20 -> OPEN
14f69 20 20 20 20 20 20 20 20 20 20 20 20 5b 70 61 67 [pag
14f6a 65 72 5f 75 6e 6c 6f 63 6b 5d 0a 2a 2a 20 0a 2a er_unlock].** .*
14f6b 2a 0a 2a 2a 20 20 4f 50 45 4e 3a 0a 2a 2a 0a 2a *.** OPEN:.**.*
14f6c 2a 20 20 20 20 54 68 65 20 70 61 67 65 72 20 73 * The pager s
14f6d 74 61 72 74 73 20 75 70 20 69 6e 20 74 68 69 73 tarts up in this
14f6e 20 73 74 61 74 65 2e 20 4e 6f 74 68 69 6e 67 20 state. Nothing
14f6f 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 69 6e is guaranteed in
14f70 20 74 68 69 73 0a 2a 2a 20 20 20 20 73 74 61 74 this.** stat
14f71 65 20 2d 20 74 68 65 20 66 69 6c 65 20 6d 61 79 e - the file may
14f72 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 6c or may not be l
14f73 6f 63 6b 65 64 20 61 6e 64 20 74 68 65 20 64 61 ocked and the da
14f74 74 61 62 61 73 65 20 73 69 7a 65 20 69 73 0a 2a tabase size is.*
14f75 2a 20 20 20 20 75 6e 6b 6e 6f 77 6e 2e 20 54 68 * unknown. Th
14f76 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 6e e database may n
14f77 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 ot be read or wr
14f78 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 itten..**.**
14f79 2a 20 4e 6f 20 72 65 61 64 20 6f 72 20 77 72 69 * No read or wri
14f7a 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 te transaction i
14f7b 73 20 61 63 74 69 76 65 2e 0a 2a 2a 20 20 20 20 s active..**
14f7c 2a 20 41 6e 79 20 6c 6f 63 6b 2c 20 6f 72 20 6e * Any lock, or n
14f7d 6f 20 6c 6f 63 6b 20 61 74 20 61 6c 6c 2c 20 6d o lock at all, m
14f7e 61 79 20 62 65 20 68 65 6c 64 20 6f 6e 20 74 68 ay be held on th
14f7f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
14f80 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 64 62 53 .** * The dbS
14f81 69 7a 65 2c 20 64 62 4f 72 69 67 53 69 7a 65 20 ize, dbOrigSize
14f82 61 6e 64 20 64 62 46 69 6c 65 53 69 7a 65 20 76 and dbFileSize v
14f83 61 72 69 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 ariables may not
14f84 20 62 65 20 74 72 75 73 74 65 64 2e 0a 2a 2a 0a be trusted..**.
14f85 2a 2a 20 20 52 45 41 44 45 52 3a 0a 2a 2a 0a 2a ** READER:.**.*
14f86 2a 20 20 20 20 49 6e 20 74 68 69 73 20 73 74 61 * In this sta
14f87 74 65 20 61 6c 6c 20 74 68 65 20 72 65 71 75 69 te all the requi
14f88 72 65 6d 65 6e 74 73 20 66 6f 72 20 72 65 61 64 rements for read
14f89 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 ing the database
14f8a 20 69 6e 20 0a 2a 2a 20 20 20 20 72 6f 6c 6c 62 in .** rollb
14f8b 61 63 6b 20 28 6e 6f 6e 2d 57 41 4c 29 20 6d 6f ack (non-WAL) mo
14f8c 64 65 20 61 72 65 20 6d 65 74 2e 20 55 6e 6c 65 de are met. Unle
14f8d 73 73 20 74 68 65 20 70 61 67 65 72 20 69 73 20 ss the pager is
14f8e 28 6f 72 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 (or recently.**
14f8f 20 20 20 77 61 73 29 20 69 6e 20 65 78 63 6c 75 was) in exclu
14f90 73 69 76 65 2d 6c 6f 63 6b 69 6e 67 20 6d 6f 64 sive-locking mod
14f91 65 2c 20 61 20 75 73 65 72 2d 6c 65 76 65 6c 20 e, a user-level
14f92 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e read transaction
14f93 20 69 73 20 0a 2a 2a 20 20 20 20 6f 70 65 6e 2e is .** open.
14f94 20 54 68 65 20 64 61 74 61 62 61 73 65 20 73 69 The database si
14f95 7a 65 20 69 73 20 6b 6e 6f 77 6e 20 69 6e 20 74 ze is known in t
14f96 68 69 73 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a his state..**.**
14f97 20 20 20 20 41 20 63 6f 6e 6e 65 63 74 69 6f 6e A connection
14f98 20 72 75 6e 6e 69 6e 67 20 77 69 74 68 20 6c 6f running with lo
14f99 63 6b 69 6e 67 5f 6d 6f 64 65 3d 6e 6f 72 6d 61 cking_mode=norma
14f9a 6c 20 65 6e 74 65 72 73 20 74 68 69 73 20 73 74 l enters this st
14f9b 61 74 65 20 77 68 65 6e 0a 2a 2a 20 20 20 20 69 ate when.** i
14f9c 74 20 6f 70 65 6e 73 20 61 20 72 65 61 64 2d 74 t opens a read-t
14f9d 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 ransaction on th
14f9e 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 72 e database and r
14f9f 65 74 75 72 6e 73 20 74 6f 20 73 74 61 74 65 0a eturns to state.
14fa0 2a 2a 20 20 20 20 4f 50 45 4e 20 61 66 74 65 72 ** OPEN after
14fa1 20 74 68 65 20 72 65 61 64 2d 74 72 61 6e 73 61 the read-transa
14fa2 63 74 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74 ction is complet
14fa3 65 64 2e 20 48 6f 77 65 76 65 72 20 61 20 63 6f ed. However a co
14fa4 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 72 nnection.** r
14fa5 75 6e 6e 69 6e 67 20 69 6e 20 6c 6f 63 6b 69 6e unning in lockin
14fa6 67 5f 6d 6f 64 65 3d 65 78 63 6c 75 73 69 76 65 g_mode=exclusive
14fa7 20 28 69 6e 63 6c 75 64 69 6e 67 20 74 65 6d 70 (including temp
14fa8 20 64 61 74 61 62 61 73 65 73 29 20 72 65 6d 61 databases) rema
14fa9 69 6e 73 20 69 6e 0a 2a 2a 20 20 20 20 74 68 69 ins in.** thi
14faa 73 20 73 74 61 74 65 20 65 76 65 6e 20 61 66 74 s state even aft
14fab 65 72 20 74 68 65 20 72 65 61 64 2d 74 72 61 6e er the read-tran
14fac 73 61 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 saction is close
14fad 64 2e 20 54 68 65 20 6f 6e 6c 79 20 77 61 79 0a d. The only way.
14fae 2a 2a 20 20 20 20 61 20 6c 6f 63 6b 69 6e 67 5f ** a locking_
14faf 6d 6f 64 65 3d 65 78 63 6c 75 73 69 76 65 20 63 mode=exclusive c
14fb0 6f 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 20 74 72 onnection can tr
14fb1 61 6e 73 69 74 69 6f 6e 20 66 72 6f 6d 20 52 45 ansition from RE
14fb2 41 44 45 52 20 74 6f 20 4f 50 45 4e 0a 2a 2a 20 ADER to OPEN.**
14fb3 20 20 20 69 73 20 76 69 61 20 74 68 65 20 45 52 is via the ER
14fb4 52 4f 52 20 73 74 61 74 65 20 28 73 65 65 20 62 ROR state (see b
14fb5 65 6c 6f 77 29 2e 0a 2a 2a 20 0a 2a 2a 20 20 20 elow)..** .**
14fb6 20 2a 20 41 20 72 65 61 64 20 74 72 61 6e 73 61 * A read transa
14fb7 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 61 63 74 ction may be act
14fb8 69 76 65 20 28 62 75 74 20 61 20 77 72 69 74 65 ive (but a write
14fb9 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e -transaction can
14fba 6e 6f 74 29 2e 0a 2a 2a 20 20 20 20 2a 20 41 20 not)..** * A
14fbb 53 48 41 52 45 44 20 6f 72 20 67 72 65 61 74 65 SHARED or greate
14fbc 72 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f r lock is held o
14fbd 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
14fbe 69 6c 65 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 65 ile..** * The
14fbf 20 64 62 53 69 7a 65 20 76 61 72 69 61 62 6c 65 dbSize variable
14fc0 20 6d 61 79 20 62 65 20 74 72 75 73 74 65 64 20 may be trusted
14fc1 28 65 76 65 6e 20 69 66 20 61 20 75 73 65 72 2d (even if a user-
14fc2 6c 65 76 65 6c 20 72 65 61 64 20 0a 2a 2a 20 20 level read .**
14fc3 20 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 transaction
14fc4 69 73 20 6e 6f 74 20 61 63 74 69 76 65 29 2e 20 is not active).
14fc5 54 68 65 20 64 62 4f 72 69 67 53 69 7a 65 20 61 The dbOrigSize a
14fc6 6e 64 20 64 62 46 69 6c 65 53 69 7a 65 20 76 61 nd dbFileSize va
14fc7 72 69 61 62 6c 65 73 0a 2a 2a 20 20 20 20 20 20 riables.**
14fc8 6d 61 79 20 6e 6f 74 20 62 65 20 74 72 75 73 74 may not be trust
14fc9 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 ed at this point
14fca 2e 0a 2a 2a 20 20 20 20 2a 20 49 66 20 74 68 65 ..** * If the
14fcb 20 64 61 74 61 62 61 73 65 20 69 73 20 61 20 57 database is a W
14fcc 41 4c 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 AL database, the
14fcd 6e 20 74 68 65 20 57 41 4c 20 63 6f 6e 6e 65 63 n the WAL connec
14fce 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2e 0a 2a 2a tion is open..**
14fcf 20 20 20 20 2a 20 45 76 65 6e 20 69 66 20 61 20 * Even if a
14fd0 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e read-transaction
14fd1 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 20 69 74 is not open, it
14fd2 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
14fd3 68 61 74 20 0a 2a 2a 20 20 20 20 20 20 74 68 65 hat .** the
14fd4 72 65 20 69 73 20 6e 6f 20 68 6f 74 2d 6a 6f 75 re is no hot-jou
14fd5 72 6e 61 6c 20 69 6e 20 74 68 65 20 66 69 6c 65 rnal in the file
14fd6 2d 73 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 20 -system..**.**
14fd7 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 3a 0a 2a WRITER_LOCKED:.*
14fd8 2a 0a 2a 2a 20 20 20 20 54 68 65 20 70 61 67 65 *.** The page
14fd9 72 20 6d 6f 76 65 73 20 74 6f 20 74 68 69 73 20 r moves to this
14fda 73 74 61 74 65 20 66 72 6f 6d 20 52 45 41 44 45 state from READE
14fdb 52 20 77 68 65 6e 20 61 20 77 72 69 74 65 2d 74 R when a write-t
14fdc 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 ransaction.**
14fdd 20 69 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 is first opened
14fde 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
14fdf 2e 20 49 6e 20 57 52 49 54 45 52 5f 4c 4f 43 4b . In WRITER_LOCK
14fe0 45 44 20 73 74 61 74 65 2c 20 61 6c 6c 20 6c 6f ED state, all lo
14fe1 63 6b 73 20 0a 2a 2a 20 20 20 20 72 65 71 75 69 cks .** requi
14fe2 72 65 64 20 74 6f 20 73 74 61 72 74 20 61 20 77 red to start a w
14fe3 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
14fe4 20 61 72 65 20 68 65 6c 64 2c 20 62 75 74 20 6e are held, but n
14fe5 6f 20 61 63 74 75 61 6c 20 0a 2a 2a 20 20 20 20 o actual .**
14fe6 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 74 6f modifications to
14fe7 20 74 68 65 20 63 61 63 68 65 20 6f 72 20 64 61 the cache or da
14fe8 74 61 62 61 73 65 20 68 61 76 65 20 74 61 6b 65 tabase have take
14fe9 6e 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 n place..**.**
14fea 20 20 49 6e 20 72 6f 6c 6c 62 61 63 6b 20 6d 6f In rollback mo
14feb 64 65 2c 20 61 20 52 45 53 45 52 56 45 44 20 6f de, a RESERVED o
14fec 72 20 28 69 66 20 74 68 65 20 74 72 61 6e 73 61 r (if the transa
14fed 63 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 ction was opened
14fee 20 77 69 74 68 20 0a 2a 2a 20 20 20 20 42 45 47 with .** BEG
14fef 49 4e 20 45 58 43 4c 55 53 49 56 45 29 20 45 58 IN EXCLUSIVE) EX
14ff0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 CLUSIVE lock is
14ff1 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 obtained on the
14ff2 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 database file wh
14ff3 65 6e 0a 2a 2a 20 20 20 20 6d 6f 76 69 6e 67 20 en.** moving
14ff4 74 6f 20 74 68 69 73 20 73 74 61 74 65 2c 20 62 to this state, b
14ff5 75 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ut the journal f
14ff6 69 6c 65 20 69 73 20 6e 6f 74 20 77 72 69 74 74 ile is not writt
14ff7 65 6e 20 74 6f 20 6f 72 20 6f 70 65 6e 65 64 20 en to or opened
14ff8 0a 2a 2a 20 20 20 20 74 6f 20 69 6e 20 74 68 69 .** to in thi
14ff9 73 20 73 74 61 74 65 2e 20 49 66 20 74 68 65 20 s state. If the
14ffa 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 transaction is c
14ffb 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c ommitted or roll
14ffc 65 64 20 62 61 63 6b 20 77 68 69 6c 65 20 0a 2a ed back while .*
14ffd 2a 20 20 20 20 69 6e 20 57 52 49 54 45 52 5f 4c * in WRITER_L
14ffe 4f 43 4b 45 44 20 73 74 61 74 65 2c 20 61 6c 6c OCKED state, all
14fff 20 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 that is require
15000 64 20 69 73 20 74 6f 20 75 6e 6c 6f 63 6b 20 74 d is to unlock t
15001 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 he database .**
15002 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 file..**.**
15003 20 20 49 4e 20 57 41 4c 20 6d 6f 64 65 2c 20 57 IN WAL mode, W
15004 61 6c 42 65 67 69 6e 57 72 69 74 65 54 72 61 6e alBeginWriteTran
15005 73 61 63 74 69 6f 6e 28 29 20 69 73 20 63 61 6c saction() is cal
15006 6c 65 64 20 74 6f 20 6c 6f 63 6b 20 74 68 65 20 led to lock the
15007 6c 6f 67 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 log file..**
15008 49 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f If the connectio
15009 6e 20 69 73 20 72 75 6e 6e 69 6e 67 20 77 69 74 n is running wit
1500a 68 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 h locking_mode=e
1500b 78 63 6c 75 73 69 76 65 2c 20 61 6e 20 61 74 74 xclusive, an att
1500c 65 6d 70 74 0a 2a 2a 20 20 20 20 69 73 20 6d 61 empt.** is ma
1500d 64 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 20 de to obtain an
1500e 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f EXCLUSIVE lock o
1500f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
15010 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 ile..**.** *
15011 41 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 A write transact
15012 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a ion is active..*
15013 2a 20 20 20 20 2a 20 49 66 20 74 68 65 20 63 6f * * If the co
15014 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e nnection is open
15015 20 69 6e 20 72 6f 6c 6c 62 61 63 6b 2d 6d 6f 64 in rollback-mod
15016 65 2c 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 e, a RESERVED or
15017 20 67 72 65 61 74 65 72 20 0a 2a 2a 20 20 20 20 greater .**
15018 20 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f lock is held o
15019 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
1501a 69 6c 65 2e 0a 2a 2a 20 20 20 20 2a 20 49 66 20 ile..** * If
1501b 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 the connection i
1501c 73 20 6f 70 65 6e 20 69 6e 20 57 41 4c 2d 6d 6f s open in WAL-mo
1501d 64 65 2c 20 61 20 57 41 4c 20 77 72 69 74 65 20 de, a WAL write
1501e 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 transaction.**
1501f 20 20 20 20 69 73 20 6f 70 65 6e 20 28 69 2e 65 is open (i.e
15020 2e 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67 69 . sqlite3WalBegi
15021 6e 57 72 69 74 65 54 72 61 6e 73 61 63 74 69 6f nWriteTransactio
15022 6e 28 29 20 68 61 73 20 62 65 65 6e 20 73 75 63 n() has been suc
15023 63 65 73 73 66 75 6c 6c 79 0a 2a 2a 20 20 20 20 cessfully.**
15024 20 20 63 61 6c 6c 65 64 29 2e 0a 2a 2a 20 20 20 called)..**
15025 20 2a 20 54 68 65 20 64 62 53 69 7a 65 2c 20 64 * The dbSize, d
15026 62 4f 72 69 67 53 69 7a 65 20 61 6e 64 20 64 62 bOrigSize and db
15027 46 69 6c 65 53 69 7a 65 20 76 61 72 69 61 62 6c FileSize variabl
15028 65 73 20 61 72 65 20 61 6c 6c 20 76 61 6c 69 64 es are all valid
15029 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 63 6f ..** * The co
1502a 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
1502b 67 65 72 20 63 61 63 68 65 20 68 61 76 65 20 6e ger cache have n
1502c 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 ot been modified
1502d 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 6a 6f ..** * The jo
1502e 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f urnal file may o
1502f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f 70 65 r may not be ope
15030 6e 2e 0a 2a 2a 20 20 20 20 2a 20 4e 6f 74 68 69 n..** * Nothi
15031 6e 67 20 28 6e 6f 74 20 65 76 65 6e 20 74 68 65 ng (not even the
15032 20 66 69 72 73 74 20 68 65 61 64 65 72 29 20 68 first header) h
15033 61 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 as been written
15034 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a to the journal..
15035 2a 2a 0a 2a 2a 20 20 57 52 49 54 45 52 5f 43 41 **.** WRITER_CA
15036 43 48 45 4d 4f 44 3a 0a 2a 2a 0a 2a 2a 20 20 20 CHEMOD:.**.**
15037 20 41 20 70 61 67 65 72 20 6d 6f 76 65 73 20 66 A pager moves f
15038 72 6f 6d 20 57 52 49 54 45 52 5f 4c 4f 43 4b 45 rom WRITER_LOCKE
15039 44 20 73 74 61 74 65 20 74 6f 20 74 68 69 73 20 D state to this
1503a 73 74 61 74 65 20 77 68 65 6e 20 61 20 70 61 67 state when a pag
1503b 65 20 69 73 0a 2a 2a 20 20 20 20 66 69 72 73 74 e is.** first
1503c 20 6d 6f 64 69 66 69 65 64 20 62 79 20 74 68 65 modified by the
1503d 20 75 70 70 65 72 20 6c 61 79 65 72 2e 20 49 6e upper layer. In
1503e 20 72 6f 6c 6c 62 61 63 6b 20 6d 6f 64 65 20 74 rollback mode t
1503f 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a he journal file.
15040 2a 2a 20 20 20 20 69 73 20 6f 70 65 6e 65 64 20 ** is opened
15041 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c (if it is not al
15042 72 65 61 64 79 20 6f 70 65 6e 29 20 61 6e 64 20 ready open) and
15043 61 20 68 65 61 64 65 72 20 77 72 69 74 74 65 6e a header written
15044 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 73 74 to the.** st
15045 61 72 74 20 6f 66 20 69 74 2e 20 54 68 65 20 64 art of it. The d
15046 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 atabase file on
15047 64 69 73 6b 20 68 61 73 20 6e 6f 74 20 62 65 65 disk has not bee
15048 6e 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a n modified..**.*
15049 2a 20 20 20 20 2a 20 41 20 77 72 69 74 65 20 74 * * A write t
1504a 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 ransaction is ac
1504b 74 69 76 65 2e 0a 2a 2a 20 20 20 20 2a 20 41 20 tive..** * A
1504c 52 45 53 45 52 56 45 44 20 6f 72 20 67 72 65 61 RESERVED or grea
1504d 74 65 72 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 ter lock is held
1504e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
1504f 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 2a 20 54 file..** * T
15050 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
15051 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 is open and the
15052 66 69 72 73 74 20 68 65 61 64 65 72 20 68 61 73 first header has
15053 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 0a 2a been written .*
15054 2a 20 20 20 20 20 20 74 6f 20 69 74 2c 20 62 75 * to it, bu
15055 74 20 74 68 65 20 68 65 61 64 65 72 20 68 61 73 t the header has
15056 20 6e 6f 74 20 62 65 65 6e 20 73 79 6e 63 65 64 not been synced
15057 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 20 20 20 20 to disk..**
15058 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f * The contents o
15059 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 f the page cache
1505a 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 have been modif
1505b 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 57 52 49 54 ied..**.** WRIT
1505c 45 52 5f 44 42 4d 4f 44 3a 0a 2a 2a 0a 2a 2a 20 ER_DBMOD:.**.**
1505d 20 20 20 54 68 65 20 70 61 67 65 72 20 74 72 61 The pager tra
1505e 6e 73 69 74 69 6f 6e 73 20 66 72 6f 6d 20 57 52 nsitions from WR
1505f 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 20 69 6e ITER_CACHEMOD in
15060 74 6f 20 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 to WRITER_DBMOD
15061 73 74 61 74 65 0a 2a 2a 20 20 20 20 77 68 65 6e state.** when
15062 20 69 74 20 6d 6f 64 69 66 69 65 73 20 74 68 65 it modifies the
15063 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
15064 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
15065 57 41 4c 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a WAL connections.
15066 2a 2a 20 20 20 20 6e 65 76 65 72 20 65 6e 74 65 ** never ente
15067 72 20 74 68 69 73 20 73 74 61 74 65 20 28 73 69 r this state (si
15068 6e 63 65 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 nce they do not
15069 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 modify the datab
1506a 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 20 20 20 ase file,.**
1506b 6a 75 73 74 20 74 68 65 20 6c 6f 67 20 66 69 6c just the log fil
1506c 65 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 41 e)..**.** * A
1506d 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
1506e 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2a on is active..**
1506f 20 20 20 20 2a 20 41 6e 20 45 58 43 4c 55 53 49 * An EXCLUSI
15070 56 45 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f VE or greater lo
15071 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 ck is held on th
15072 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
15073 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 6a 6f 75 .** * The jou
15074 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 rnal file is ope
15075 6e 20 61 6e 64 20 74 68 65 20 66 69 72 73 74 20 n and the first
15076 68 65 61 64 65 72 20 68 61 73 20 62 65 65 6e 20 header has been
15077 77 72 69 74 74 65 6e 20 0a 2a 2a 20 20 20 20 20 written .**
15078 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f 20 64 and synced to d
15079 69 73 6b 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 65 isk..** * The
1507a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
1507b 20 70 61 67 65 20 63 61 63 68 65 20 68 61 76 65 page cache have
1507c 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 28 been modified (
1507d 61 6e 64 20 70 6f 73 73 69 62 6c 79 0a 2a 2a 20 and possibly.**
1507e 20 20 20 20 20 77 72 69 74 74 65 6e 20 74 6f 20 written to
1507f 64 69 73 6b 29 2e 0a 2a 2a 0a 2a 2a 20 20 57 52 disk)..**.** WR
15080 49 54 45 52 5f 46 49 4e 49 53 48 45 44 3a 0a 2a ITER_FINISHED:.*
15081 2a 0a 2a 2a 20 20 20 20 49 74 20 69 73 20 6e 6f *.** It is no
15082 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 t possible for a
15083 20 57 41 4c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 WAL connection
15084 74 6f 20 65 6e 74 65 72 20 74 68 69 73 20 73 74 to enter this st
15085 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 41 20 ate..**.** A
15086 72 6f 6c 6c 62 61 63 6b 2d 6d 6f 64 65 20 70 61 rollback-mode pa
15087 67 65 72 20 63 68 61 6e 67 65 73 20 74 6f 20 57 ger changes to W
15088 52 49 54 45 52 5f 46 49 4e 49 53 48 45 44 20 73 RITER_FINISHED s
15089 74 61 74 65 20 66 72 6f 6d 20 57 52 49 54 45 52 tate from WRITER
1508a 5f 44 42 4d 4f 44 0a 2a 2a 20 20 20 20 73 74 61 _DBMOD.** sta
1508b 74 65 20 61 66 74 65 72 20 74 68 65 20 65 6e 74 te after the ent
1508c 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ire transaction
1508d 68 61 73 20 62 65 65 6e 20 73 75 63 63 65 73 73 has been success
1508e 66 75 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e fully written in
1508f 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 64 61 74 to the.** dat
15090 61 62 61 73 65 20 66 69 6c 65 2e 20 49 6e 20 74 abase file. In t
15091 68 69 73 20 73 74 61 74 65 20 74 68 65 20 74 72 his state the tr
15092 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 62 65 ansaction may be
15093 20 63 6f 6d 6d 69 74 74 65 64 20 73 69 6d 70 6c committed simpl
15094 79 0a 2a 2a 20 20 20 20 62 79 20 66 69 6e 61 6c y.** by final
15095 69 7a 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 izing the journa
15096 6c 20 66 69 6c 65 2e 20 4f 6e 63 65 20 69 6e 20 l file. Once in
15097 57 52 49 54 45 52 5f 46 49 4e 49 53 48 45 44 20 WRITER_FINISHED
15098 73 74 61 74 65 2c 20 69 74 20 69 73 20 0a 2a 2a state, it is .**
15099 20 20 20 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 not possible
1509a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64 to modify the d
1509b 61 74 61 62 61 73 65 20 66 75 72 74 68 65 72 2e atabase further.
1509c 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 At this point,
1509d 74 68 65 20 75 70 70 65 72 20 0a 2a 2a 20 20 20 the upper .**
1509e 20 6c 61 79 65 72 20 6d 75 73 74 20 65 69 74 68 layer must eith
1509f 65 72 20 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c er commit or rol
150a0 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 lback the transa
150a1 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 ction..**.**
150a2 2a 20 41 20 77 72 69 74 65 20 74 72 61 6e 73 61 * A write transa
150a3 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e ction is active.
150a4 0a 2a 2a 20 20 20 20 2a 20 41 6e 20 45 58 43 4c .** * An EXCL
150a5 55 53 49 56 45 20 6f 72 20 67 72 65 61 74 65 72 USIVE or greater
150a6 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e lock is held on
150a7 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
150a8 6c 65 2e 0a 2a 2a 20 20 20 20 2a 20 41 6c 6c 20 le..** * All
150a9 77 72 69 74 69 6e 67 20 61 6e 64 20 73 79 6e 63 writing and sync
150aa 69 6e 67 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 61 ing of journal a
150ab 6e 64 20 64 61 74 61 62 61 73 65 20 64 61 74 61 nd database data
150ac 20 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a has finished..*
150ad 2a 20 20 20 20 20 20 49 66 20 6e 6f 20 65 72 72 * If no err
150ae 6f 72 20 6f 63 63 75 72 65 64 2c 20 61 6c 6c 20 or occured, all
150af 74 68 61 74 20 72 65 6d 61 69 6e 73 20 69 73 20 that remains is
150b0 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 to finalize the
150b1 6a 6f 75 72 6e 61 6c 20 74 6f 0a 2a 2a 20 20 20 journal to.**
150b2 20 20 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 commit the tr
150b3 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 61 6e ansaction. If an
150b4 20 65 72 72 6f 72 20 64 69 64 20 6f 63 63 75 72 error did occur
150b5 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 77 69 6c , the caller wil
150b6 6c 20 6e 65 65 64 0a 2a 2a 20 20 20 20 20 20 74 l need.** t
150b7 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 o rollback the t
150b8 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a ransaction. .**.
150b9 2a 2a 20 20 45 52 52 4f 52 3a 0a 2a 2a 0a 2a 2a ** ERROR:.**.**
150ba 20 20 20 20 54 68 65 20 45 52 52 4f 52 20 73 74 The ERROR st
150bb 61 74 65 20 69 73 20 65 6e 74 65 72 65 64 20 77 ate is entered w
150bc 68 65 6e 20 61 6e 20 49 4f 20 6f 72 20 64 69 73 hen an IO or dis
150bd 6b 2d 66 75 6c 6c 20 65 72 72 6f 72 20 28 69 6e k-full error (in
150be 63 6c 75 64 69 6e 67 0a 2a 2a 20 20 20 20 53 51 cluding.** SQ
150bf 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d LITE_IOERR_NOMEM
150c0 29 20 6f 63 63 75 72 73 20 61 74 20 61 20 70 6f ) occurs at a po
150c1 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 int in the code
150c2 74 68 61 74 20 6d 61 6b 65 73 20 69 74 20 0a 2a that makes it .*
150c3 2a 20 20 20 20 64 69 66 66 69 63 75 6c 74 20 74 * difficult t
150c4 6f 20 62 65 20 73 75 72 65 20 74 68 61 74 20 74 o be sure that t
150c5 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 he in-memory pag
150c6 65 72 20 73 74 61 74 65 20 28 63 61 63 68 65 20 er state (cache
150c7 63 6f 6e 74 65 6e 74 73 2c 20 0a 2a 2a 20 20 20 contents, .**
150c8 20 64 62 20 73 69 7a 65 20 65 74 63 2e 29 20 61 db size etc.) a
150c9 72 65 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 re consistent wi
150ca 74 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 th the contents
150cb 6f 66 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 of the file-syst
150cc 65 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 54 65 6d em..**.** Tem
150cd 70 6f 72 61 72 79 20 70 61 67 65 72 20 66 69 6c porary pager fil
150ce 65 73 20 6d 61 79 20 65 6e 74 65 72 20 74 68 65 es may enter the
150cf 20 45 52 52 4f 52 20 73 74 61 74 65 2c 20 62 75 ERROR state, bu
150d0 74 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 t in-memory page
150d1 72 73 0a 2a 2a 20 20 20 20 63 61 6e 6e 6f 74 2e rs.** cannot.
150d2 0a 2a 2a 0a 2a 2a 20 20 20 20 46 6f 72 20 65 78 .**.** For ex
150d3 61 6d 70 6c 65 2c 20 69 66 20 61 6e 20 49 4f 20 ample, if an IO
150d4 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 error occurs whi
150d5 6c 65 20 70 65 72 66 6f 72 6d 69 6e 67 20 61 20 le performing a
150d6 72 6f 6c 6c 62 61 63 6b 2c 20 0a 2a 2a 20 20 20 rollback, .**
150d7 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
150d8 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 the page-cache
150d9 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e 20 61 may be left in a
150da 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 n inconsistent s
150db 74 61 74 65 2e 0a 2a 2a 20 20 20 20 41 74 20 74 tate..** At t
150dc 68 69 73 20 70 6f 69 6e 74 20 69 74 20 77 6f 75 his point it wou
150dd 6c 64 20 62 65 20 64 61 6e 67 65 72 6f 75 73 20 ld be dangerous
150de 74 6f 20 63 68 61 6e 67 65 20 62 61 63 6b 20 74 to change back t
150df 6f 20 52 45 41 44 45 52 20 73 74 61 74 65 0a 2a o READER state.*
150e0 2a 20 20 20 20 28 61 73 20 75 73 75 61 6c 6c 79 * (as usually
150e1 20 68 61 70 70 65 6e 73 20 61 66 74 65 72 20 61 happens after a
150e2 20 72 6f 6c 6c 62 61 63 6b 29 2e 20 41 6e 79 20 rollback). Any
150e3 73 75 62 73 65 71 75 65 6e 74 20 72 65 61 64 65 subsequent reade
150e4 72 73 20 6d 69 67 68 74 0a 2a 2a 20 20 20 20 72 rs might.** r
150e5 65 70 6f 72 74 20 64 61 74 61 62 61 73 65 20 63 eport database c
150e6 6f 72 72 75 70 74 69 6f 6e 20 28 64 75 65 20 74 orruption (due t
150e7 6f 20 74 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 o the inconsiste
150e8 6e 74 20 63 61 63 68 65 29 2c 20 61 6e 64 20 69 nt cache), and i
150e9 66 0a 2a 2a 20 20 20 20 74 68 65 79 20 75 70 67 f.** they upg
150ea 72 61 64 65 20 74 6f 20 77 72 69 74 65 72 73 2c rade to writers,
150eb 20 74 68 65 79 20 6d 61 79 20 69 6e 61 64 76 65 they may inadve
150ec 72 74 65 6e 74 6c 79 20 63 6f 72 72 75 70 74 20 rtently corrupt
150ed 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
150ee 20 20 20 66 69 6c 65 2e 20 54 6f 20 61 76 6f 69 file. To avoi
150ef 64 20 74 68 69 73 20 68 61 7a 61 72 64 2c 20 74 d this hazard, t
150f0 68 65 20 70 61 67 65 72 20 73 77 69 74 63 68 65 he pager switche
150f1 73 20 69 6e 74 6f 20 74 68 65 20 45 52 52 4f 52 s into the ERROR
150f2 20 73 74 61 74 65 0a 2a 2a 20 20 20 20 69 6e 73 state.** ins
150f3 74 65 61 64 20 6f 66 20 52 45 41 44 45 52 20 66 tead of READER f
150f4 6f 6c 6c 6f 77 69 6e 67 20 73 75 63 68 20 61 6e ollowing such an
150f5 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 error..**.**
150f6 20 4f 6e 63 65 20 69 74 20 68 61 73 20 65 6e 74 Once it has ent
150f7 65 72 65 64 20 74 68 65 20 45 52 52 4f 52 20 73 ered the ERROR s
150f8 74 61 74 65 2c 20 61 6e 79 20 61 74 74 65 6d 70 tate, any attemp
150f9 74 20 74 6f 20 75 73 65 20 74 68 65 20 70 61 67 t to use the pag
150fa 65 72 0a 2a 2a 20 20 20 20 74 6f 20 72 65 61 64 er.** to read
150fb 20 6f 72 20 77 72 69 74 65 20 64 61 74 61 20 72 or write data r
150fc 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2e eturns an error.
150fd 20 45 76 65 6e 74 75 61 6c 6c 79 2c 20 6f 6e 63 Eventually, onc
150fe 65 20 61 6c 6c 20 0a 2a 2a 20 20 20 20 6f 75 74 e all .** out
150ff 73 74 61 6e 64 69 6e 67 20 74 72 61 6e 73 61 63 standing transac
15100 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 tions have been
15101 61 62 61 6e 64 6f 6e 65 64 2c 20 74 68 65 20 70 abandoned, the p
15102 61 67 65 72 20 69 73 20 61 62 6c 65 20 74 6f 0a ager is able to.
15103 2a 2a 20 20 20 20 74 72 61 6e 73 69 74 69 6f 6e ** transition
15104 20 62 61 63 6b 20 74 6f 20 4f 50 45 4e 20 73 74 back to OPEN st
15105 61 74 65 2c 20 64 69 73 63 61 72 64 69 6e 67 20 ate, discarding
15106 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
15107 74 68 65 20 0a 2a 2a 20 20 20 20 70 61 67 65 2d the .** page-
15108 63 61 63 68 65 20 61 6e 64 20 61 6e 79 20 6f 74 cache and any ot
15109 68 65 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 her in-memory st
1510a 61 74 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 ate at the same
1510b 74 69 6d 65 2e 20 45 76 65 72 79 74 68 69 6e 67 time. Everything
1510c 0a 2a 2a 20 20 20 20 69 73 20 72 65 6c 6f 61 64 .** is reload
1510d 65 64 20 66 72 6f 6d 20 64 69 73 6b 20 28 61 6e ed from disk (an
1510e 64 2c 20 69 66 20 6e 65 63 65 73 73 61 72 79 2c d, if necessary,
1510f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
15110 6c 62 61 63 6b 20 70 65 66 6f 72 6d 65 64 29 0a lback peformed).
15111 2a 2a 20 20 20 20 77 68 65 6e 20 61 20 72 65 61 ** when a rea
15112 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 d-transaction is
15113 20 6e 65 78 74 20 6f 70 65 6e 65 64 20 6f 6e 20 next opened on
15114 74 68 65 20 70 61 67 65 72 20 28 74 72 61 6e 73 the pager (trans
15115 69 74 69 6f 6e 69 6e 67 0a 2a 2a 20 20 20 20 74 itioning.** t
15116 68 65 20 70 61 67 65 72 20 69 6e 74 6f 20 52 45 he pager into RE
15117 41 44 45 52 20 73 74 61 74 65 29 2e 20 41 74 20 ADER state). At
15118 74 68 61 74 20 70 6f 69 6e 74 20 74 68 65 20 73 that point the s
15119 79 73 74 65 6d 20 68 61 73 20 72 65 63 6f 76 65 ystem has recove
1511a 72 65 64 20 0a 2a 2a 20 20 20 20 66 72 6f 6d 20 red .** from
1511b 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a the error..**.**
1511c 20 20 20 20 53 70 65 63 69 66 69 63 61 6c 6c 79 Specifically
1511d 2c 20 74 68 65 20 70 61 67 65 72 20 6a 75 6d 70 , the pager jump
1511e 73 20 69 6e 74 6f 20 74 68 65 20 45 52 52 4f 52 s into the ERROR
1511f 20 73 74 61 74 65 20 69 66 3a 0a 2a 2a 0a 2a 2a state if:.**.**
15120 20 20 20 20 20 20 31 2e 20 41 6e 20 65 72 72 6f 1. An erro
15121 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 61 r occurs while a
15122 74 74 65 6d 70 74 69 6e 67 20 61 20 72 6f 6c 6c ttempting a roll
15123 62 61 63 6b 2e 20 54 68 69 73 20 68 61 70 70 65 back. This happe
15124 6e 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 ns in.**
15125 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 function sqlite
15126 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 3PagerRollback()
15127 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 32 2e 20 ..**.** 2.
15128 41 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 An error occurs
15129 77 68 69 6c 65 20 61 74 74 65 6d 70 74 69 6e 67 while attempting
1512a 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 61 20 6a to finalize a j
1512b 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 ournal file.**
1512c 20 20 20 20 20 20 20 66 6f 6c 6c 6f 77 69 6e 67 following
1512d 20 61 20 63 6f 6d 6d 69 74 20 69 6e 20 66 75 6e a commit in fun
1512e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 50 61 67 ction sqlite3Pag
1512f 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f erCommitPhaseTwo
15130 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 33 ()..**.** 3
15131 2e 20 41 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 . An error occur
15132 73 20 77 68 69 6c 65 20 61 74 74 65 6d 70 74 69 s while attempti
15133 6e 67 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 ng to write to t
15134 68 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 0a 2a 2a he journal or.**
15135 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 databas
15136 65 20 66 69 6c 65 20 69 6e 20 66 75 6e 63 74 69 e file in functi
15137 6f 6e 20 70 61 67 65 72 53 74 72 65 73 73 28 29 on pagerStress()
15138 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66 72 65 in order to fre
15139 65 20 75 70 0a 2a 2a 20 20 20 20 20 20 20 20 20 e up.**
1513a 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 memory..**.**
1513b 20 49 6e 20 6f 74 68 65 72 20 63 61 73 65 73 2c In other cases,
1513c 20 74 68 65 20 65 72 72 6f 72 20 69 73 20 72 65 the error is re
1513d 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 62 2d turned to the b-
1513e 74 72 65 65 20 6c 61 79 65 72 2e 20 54 68 65 20 tree layer. The
1513f 62 2d 74 72 65 65 0a 2a 2a 20 20 20 20 6c 61 79 b-tree.** lay
15140 65 72 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73 er then attempts
15141 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65 72 a rollback oper
15142 61 74 69 6f 6e 2e 20 49 66 20 74 68 65 20 65 72 ation. If the er
15143 72 6f 72 20 63 6f 6e 64 69 74 69 6f 6e 20 0a 2a ror condition .*
15144 2a 20 20 20 20 70 65 72 73 69 73 74 73 2c 20 74 * persists, t
15145 68 65 20 70 61 67 65 72 20 65 6e 74 65 72 73 20 he pager enters
15146 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 20 the ERROR state
15147 76 69 61 20 63 6f 6e 64 69 74 69 6f 6e 20 28 31 via condition (1
15148 29 20 61 62 6f 76 65 2e 0a 2a 2a 0a 2a 2a 20 20 ) above..**.**
15149 20 20 43 6f 6e 64 69 74 69 6f 6e 20 28 33 29 20 Condition (3)
1514a 69 73 20 6e 65 63 65 73 73 61 72 79 20 62 65 63 is necessary bec
1514b 61 75 73 65 20 69 74 20 63 61 6e 20 62 65 20 74 ause it can be t
1514c 72 69 67 67 65 72 65 64 20 62 79 20 61 20 72 65 riggered by a re
1514d 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 20 20 20 73 74 ad-only.** st
1514e 61 74 65 6d 65 6e 74 20 65 78 65 63 75 74 65 64 atement executed
1514f 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61 within a transa
15150 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 ction. In this c
15151 61 73 65 2c 20 69 66 20 74 68 65 20 65 72 72 6f ase, if the erro
15152 72 0a 2a 2a 20 20 20 20 63 6f 64 65 20 77 65 72 r.** code wer
15153 65 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e 65 e simply returne
15154 64 20 74 6f 20 74 68 65 20 75 73 65 72 2c 20 74 d to the user, t
15155 68 65 20 62 2d 74 72 65 65 20 6c 61 79 65 72 20 he b-tree layer
15156 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 20 20 20 would not.**
15157 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61 74 automatically at
15158 74 65 6d 70 74 20 61 20 72 6f 6c 6c 62 61 63 6b tempt a rollback
15159 2c 20 61 73 20 69 74 20 61 73 73 75 6d 65 73 20 , as it assumes
1515a 74 68 61 74 20 61 6e 20 65 72 72 6f 72 20 69 6e that an error in
1515b 20 61 0a 2a 2a 20 20 20 20 72 65 61 64 2d 6f 6e a.** read-on
1515c 6c 79 20 73 74 61 74 65 6d 65 6e 74 20 63 61 6e ly statement can
1515d 6e 6f 74 20 6c 65 61 76 65 20 74 68 65 20 70 61 not leave the pa
1515e 67 65 72 20 69 6e 20 61 6e 20 69 6e 74 65 72 6e ger in an intern
1515f 61 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e ally inconsisten
15160 74 20 0a 2a 2a 20 20 20 20 73 74 61 74 65 2e 0a t .** state..
15161 2a 2a 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 50 **.** * The P
15162 61 67 65 72 2e 65 72 72 43 6f 64 65 20 76 61 72 ager.errCode var
15163 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 iable is set to
15164 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 something other
15165 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a than SQLITE_OK..
15166 2a 2a 20 20 20 20 2a 20 54 68 65 72 65 20 61 72 ** * There ar
15167 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 75 e one or more ou
15168 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 tstanding refere
15169 6e 63 65 73 20 74 6f 20 70 61 67 65 73 20 28 61 nces to pages (a
1516a 66 74 65 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 fter the.**
1516b 20 6c 61 73 74 20 72 65 66 65 72 65 6e 63 65 20 last reference
1516c 69 73 20 64 72 6f 70 70 65 64 20 74 68 65 20 70 is dropped the p
1516d 61 67 65 72 20 73 68 6f 75 6c 64 20 6d 6f 76 65 ager should move
1516e 20 62 61 63 6b 20 74 6f 20 4f 50 45 4e 20 73 74 back to OPEN st
1516f 61 74 65 29 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 ate)..** * Th
15170 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 61 e pager is not a
15171 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 n in-memory page
15172 72 2e 0a 2a 2a 20 20 20 20 0a 2a 2a 0a 2a 2a 20 r..** .**.**
15173 4e 6f 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a Notes:.**.** *
15174 20 41 20 70 61 67 65 72 20 69 73 20 6e 65 76 65 A pager is neve
15175 72 20 69 6e 20 57 52 49 54 45 52 5f 44 42 4d 4f r in WRITER_DBMO
15176 44 20 6f 72 20 57 52 49 54 45 52 5f 46 49 4e 49 D or WRITER_FINI
15177 53 48 45 44 20 73 74 61 74 65 20 69 66 20 74 68 SHED state if th
15178 65 0a 2a 2a 20 20 20 20 20 63 6f 6e 6e 65 63 74 e.** connect
15179 69 6f 6e 20 69 73 20 6f 70 65 6e 20 69 6e 20 57 ion is open in W
1517a 41 4c 20 6d 6f 64 65 2e 20 41 20 57 41 4c 20 63 AL mode. A WAL c
1517b 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 61 6c 77 onnection is alw
1517c 61 79 73 20 69 6e 20 6f 6e 65 0a 2a 2a 20 20 20 ays in one.**
1517d 20 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 66 of the first f
1517e 6f 75 72 20 73 74 61 74 65 73 2e 0a 2a 2a 0a 2a our states..**.*
1517f 2a 20 20 20 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 * * Normally,
15180 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 a connection ope
15181 6e 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d n in exclusive m
15182 6f 64 65 20 69 73 20 6e 65 76 65 72 20 69 6e 20 ode is never in
15183 50 41 47 45 52 5f 4f 50 45 4e 0a 2a 2a 20 20 20 PAGER_OPEN.**
15184 20 20 73 74 61 74 65 2e 20 54 68 65 72 65 20 61 state. There a
15185 72 65 20 74 77 6f 20 65 78 63 65 70 74 69 6f 6e re two exception
15186 73 3a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 s: immediately a
15187 66 74 65 72 20 65 78 63 6c 75 73 69 76 65 2d 6d fter exclusive-m
15188 6f 64 65 20 68 61 73 0a 2a 2a 20 20 20 20 20 62 ode has.** b
15189 65 65 6e 20 74 75 72 6e 65 64 20 6f 6e 20 28 61 een turned on (a
1518a 6e 64 20 62 65 66 6f 72 65 20 61 6e 79 20 72 65 nd before any re
1518b 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e ad or write tran
1518c 73 61 63 74 69 6f 6e 73 20 61 72 65 20 0a 2a 2a sactions are .**
1518d 20 20 20 20 20 65 78 65 63 75 74 65 64 29 2c 20 executed),
1518e 61 6e 64 20 77 68 65 6e 20 74 68 65 20 70 61 67 and when the pag
1518f 65 72 20 69 73 20 6c 65 61 76 69 6e 67 20 74 68 er is leaving th
15190 65 20 22 65 72 72 6f 72 20 73 74 61 74 65 22 2e e "error state".
15191 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 53 65 65 20 61 .**.** * See a
15192 6c 73 6f 3a 20 61 73 73 65 72 74 5f 70 61 67 65 lso: assert_page
15193 72 5f 73 74 61 74 65 28 29 2e 0a 2a 2f 0a 23 64 r_state()..*/.#d
15194 65 66 69 6e 65 20 50 41 47 45 52 5f 4f 50 45 4e efine PAGER_OPEN
15195 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15196 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 45 0.#define PAGE
15197 52 5f 52 45 41 44 45 52 20 20 20 20 20 20 20 20 R_READER
15198 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
15199 65 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c e PAGER_WRITER_L
1519a 4f 43 4b 45 44 20 20 20 20 20 20 20 20 20 32 0a OCKED 2.
1519b 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 57 52 #define PAGER_WR
1519c 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 20 20 20 ITER_CACHEMOD
1519d 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 50 41 3.#define PA
1519e 47 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 GER_WRITER_DBMOD
1519f 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 4.#def
151a0 69 6e 65 20 50 41 47 45 52 5f 57 52 49 54 45 52 ine PAGER_WRITER
151a1 5f 46 49 4e 49 53 48 45 44 20 20 20 20 20 20 20 _FINISHED
151a2 35 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 5.#define PAGER_
151a3 45 52 52 4f 52 20 20 20 20 20 20 20 20 20 20 20 ERROR
151a4 20 20 20 20 20 20 36 0a 0a 2f 2a 0a 2a 2a 20 54 6../*.** T
151a5 68 65 20 50 61 67 65 72 2e 65 4c 6f 63 6b 20 76 he Pager.eLock v
151a6 61 72 69 61 62 6c 65 20 69 73 20 61 6c 6d 6f 73 ariable is almos
151a7 74 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 t always set to
151a8 6f 6e 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 one of the .** f
151a9 6f 6c 6c 6f 77 69 6e 67 20 6c 6f 63 6b 69 6e 67 ollowing locking
151aa 2d 73 74 61 74 65 73 2c 20 61 63 63 6f 72 64 69 -states, accordi
151ab 6e 67 20 74 6f 20 74 68 65 20 6c 6f 63 6b 20 63 ng to the lock c
151ac 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e urrently held on
151ad 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
151ae 20 66 69 6c 65 3a 20 4e 4f 5f 4c 4f 43 4b 2c 20 file: NO_LOCK,
151af 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 SHARED_LOCK, RES
151b0 45 52 56 45 44 5f 4c 4f 43 4b 20 6f 72 20 45 58 ERVED_LOCK or EX
151b1 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 2e 0a 2a 2a CLUSIVE_LOCK..**
151b2 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 This variable i
151b3 73 20 6b 65 70 74 20 75 70 20 74 6f 20 64 61 74 s kept up to dat
151b4 65 20 61 73 20 6c 6f 63 6b 73 20 61 72 65 20 74 e as locks are t
151b5 61 6b 65 6e 20 61 6e 64 20 72 65 6c 65 61 73 65 aken and release
151b6 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 61 67 65 d by.** the page
151b7 72 4c 6f 63 6b 44 62 28 29 20 61 6e 64 20 70 61 rLockDb() and pa
151b8 67 65 72 55 6e 6c 6f 63 6b 44 62 28 29 20 77 72 gerUnlockDb() wr
151b9 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 appers..**.** If
151ba 20 74 68 65 20 56 46 53 20 78 4c 6f 63 6b 28 29 the VFS xLock()
151bb 20 6f 72 20 78 55 6e 6c 6f 63 6b 28 29 20 72 65 or xUnlock() re
151bc 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 6f turns an error o
151bd 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 ther than SQLITE
151be 5f 42 55 53 59 0a 2a 2a 20 28 69 2e 65 2e 20 6f _BUSY.** (i.e. o
151bf 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 ne of the SQLITE
151c0 5f 49 4f 45 52 52 20 73 75 62 74 79 70 65 73 29 _IOERR subtypes)
151c1 2c 20 69 74 20 69 73 20 6e 6f 74 20 63 6c 65 61 , it is not clea
151c2 72 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 r whether or not
151c3 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f .** the operatio
151c4 6e 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c n was successful
151c5 2e 20 49 6e 20 74 68 65 73 65 20 63 69 72 63 75 . In these circu
151c6 6d 73 74 61 6e 63 65 73 20 70 61 67 65 72 4c 6f mstances pagerLo
151c7 63 6b 44 62 28 29 20 61 6e 64 0a 2a 2a 20 70 61 ckDb() and.** pa
151c8 67 65 72 55 6e 6c 6f 63 6b 44 62 28 29 20 74 61 gerUnlockDb() ta
151c9 6b 65 20 61 20 63 6f 6e 73 65 72 76 61 74 69 76 ke a conservativ
151ca 65 20 61 70 70 72 6f 61 63 68 20 2d 20 65 4c 6f e approach - eLo
151cb 63 6b 20 69 73 20 61 6c 77 61 79 73 20 75 70 64 ck is always upd
151cc 61 74 65 64 0a 2a 2a 20 77 68 65 6e 20 75 6e 6c ated.** when unl
151cd 6f 63 6b 69 6e 67 20 74 68 65 20 66 69 6c 65 2c ocking the file,
151ce 20 61 6e 64 20 6f 6e 6c 79 20 75 70 64 61 74 65 and only update
151cf 64 20 77 68 65 6e 20 6c 6f 63 6b 69 6e 67 20 74 d when locking t
151d0 68 65 20 66 69 6c 65 20 69 66 20 74 68 65 0a 2a he file if the.*
151d1 2a 20 56 46 53 20 63 61 6c 6c 20 69 73 20 73 75 * VFS call is su
151d2 63 63 65 73 73 66 75 6c 2e 20 54 68 69 73 20 77 ccessful. This w
151d3 61 79 2c 20 74 68 65 20 50 61 67 65 72 2e 65 4c ay, the Pager.eL
151d4 6f 63 6b 20 76 61 72 69 61 62 6c 65 20 6d 61 79 ock variable may
151d5 20 62 65 20 73 65 74 0a 2a 2a 20 74 6f 20 61 20 be set.** to a
151d6 6c 65 73 73 20 65 78 63 6c 75 73 69 76 65 20 28 less exclusive (
151d7 6c 6f 77 65 72 29 20 76 61 6c 75 65 20 74 68 61 lower) value tha
151d8 6e 20 74 68 65 20 6c 6f 63 6b 20 74 68 61 74 20 n the lock that
151d9 69 73 20 61 63 74 75 61 6c 6c 79 20 68 65 6c 64 is actually held
151da 0a 2a 2a 20 61 74 20 74 68 65 20 73 79 73 74 65 .** at the syste
151db 6d 20 6c 65 76 65 6c 2c 20 62 75 74 20 69 74 20 m level, but it
151dc 69 73 20 6e 65 76 65 72 20 73 65 74 20 74 6f 20 is never set to
151dd 61 20 6d 6f 72 65 20 65 78 63 6c 75 73 69 76 65 a more exclusive
151de 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 value..**.** Th
151df 69 73 20 69 73 20 75 73 75 61 6c 6c 79 20 73 61 is is usually sa
151e0 66 65 2e 20 49 66 20 61 6e 20 78 55 6e 6c 6f 63 fe. If an xUnloc
151e1 6b 20 66 61 69 6c 73 20 6f 72 20 61 70 70 65 61 k fails or appea
151e2 72 73 20 74 6f 20 66 61 69 6c 2c 20 74 68 65 72 rs to fail, ther
151e3 65 20 6d 61 79 20 0a 2a 2a 20 62 65 20 61 20 66 e may .** be a f
151e4 65 77 20 72 65 64 75 6e 64 61 6e 74 20 78 4c 6f ew redundant xLo
151e5 63 6b 28 29 20 63 61 6c 6c 73 20 6f 72 20 61 20 ck() calls or a
151e6 6c 6f 63 6b 20 6d 61 79 20 62 65 20 68 65 6c 64 lock may be held
151e7 20 66 6f 72 20 6c 6f 6e 67 65 72 20 74 68 61 6e for longer than
151e8 0a 2a 2a 20 72 65 71 75 69 72 65 64 2c 20 62 75 .** required, bu
151e9 74 20 6e 6f 74 68 69 6e 67 20 72 65 61 6c 6c 79 t nothing really
151ea 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a goes wrong..**.
151eb 2a 2a 20 54 68 65 20 65 78 63 65 70 74 69 6f 6e ** The exception
151ec 20 69 73 20 77 68 65 6e 20 74 68 65 20 64 61 74 is when the dat
151ed 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 75 6e abase file is un
151ee 6c 6f 63 6b 65 64 20 61 73 20 74 68 65 20 70 61 locked as the pa
151ef 67 65 72 20 6d 6f 76 65 73 0a 2a 2a 20 66 72 6f ger moves.** fro
151f0 6d 20 45 52 52 4f 52 20 74 6f 20 4f 50 45 4e 20 m ERROR to OPEN
151f1 73 74 61 74 65 2e 20 41 74 20 74 68 69 73 20 70 state. At this p
151f2 6f 69 6e 74 20 74 68 65 72 65 20 6d 61 79 20 62 oint there may b
151f3 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 e a hot-journal
151f4 66 69 6c 65 20 0a 2a 2a 20 69 6e 20 74 68 65 20 file .** in the
151f5 66 69 6c 65 2d 73 79 73 74 65 6d 20 74 68 61 74 file-system that
151f6 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c needs to be rol
151f7 6c 65 64 20 62 61 63 6b 20 28 61 73 20 70 61 72 led back (as par
151f8 74 20 6f 66 20 61 20 4f 50 45 4e 2d 3e 53 48 41 t of a OPEN->SHA
151f9 52 45 44 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f RED.** transitio
151fa 6e 2c 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 n, by the same p
151fb 61 67 65 72 20 6f 72 20 61 6e 79 20 6f 74 68 65 ager or any othe
151fc 72 29 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 20 r). If the call
151fd 74 6f 20 78 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 to xUnlock().**
151fe 66 61 69 6c 73 20 61 74 20 74 68 69 73 20 70 6f fails at this po
151ff 69 6e 74 20 61 6e 64 20 74 68 65 20 70 61 67 65 int and the page
15200 72 20 69 73 20 6c 65 66 74 20 68 6f 6c 64 69 6e r is left holdin
15201 67 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c g an EXCLUSIVE l
15202 6f 63 6b 2c 20 74 68 69 73 0a 2a 2a 20 63 61 6e ock, this.** can
15203 20 63 6f 6e 66 75 73 65 20 74 68 65 20 63 61 6c confuse the cal
15204 6c 20 74 6f 20 78 43 68 65 63 6b 52 65 73 65 72 l to xCheckReser
15205 76 65 64 4c 6f 63 6b 28 29 20 63 61 6c 6c 20 6d vedLock() call m
15206 61 64 65 20 6c 61 74 65 72 20 61 73 20 70 61 72 ade later as par
15207 74 0a 2a 2a 20 6f 66 20 68 6f 74 2d 6a 6f 75 72 t.** of hot-jour
15208 6e 61 6c 20 64 65 74 65 63 74 69 6f 6e 2e 0a 2a nal detection..*
15209 2a 0a 2a 2a 20 78 43 68 65 63 6b 52 65 73 65 72 *.** xCheckReser
1520a 76 65 64 4c 6f 63 6b 28 29 20 69 73 20 64 65 66 vedLock() is def
1520b 69 6e 65 64 20 61 73 20 72 65 74 75 72 6e 69 6e ined as returnin
1520c 67 20 74 72 75 65 20 22 69 66 20 74 68 65 72 65 g true "if there
1520d 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 0a is a RESERVED .
1520e 2a 2a 20 6c 6f 63 6b 20 68 65 6c 64 20 62 79 20 ** lock held by
1520f 74 68 69 73 20 70 72 6f 63 65 73 73 20 6f 72 20 this process or
15210 61 6e 79 20 6f 74 68 65 72 73 22 2e 20 53 6f 20 any others". So
15211 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
15212 63 6b 20 6d 61 79 20 0a 2a 2a 20 72 65 74 75 72 ck may .** retur
15213 6e 20 74 72 75 65 20 62 65 63 61 75 73 65 20 74 n true because t
15214 68 65 20 63 61 6c 6c 65 72 20 69 74 73 65 6c 66 he caller itself
15215 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 6e 20 45 is holding an E
15216 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 28 62 XCLUSIVE lock (b
15217 75 74 0a 2a 2a 20 64 6f 65 73 6e 27 74 20 6b 6e ut.** doesn't kn
15218 6f 77 20 69 74 20 62 65 63 61 75 73 65 20 6f 66 ow it because of
15219 20 61 20 70 72 65 76 69 6f 75 73 20 65 72 72 6f a previous erro
1521a 72 20 69 6e 20 78 55 6e 6c 6f 63 6b 29 2e 20 49 r in xUnlock). I
1521b 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 0a 2a f this happens.*
1521c 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 * a hot-journal
1521d 6d 61 79 20 62 65 20 6d 69 73 74 61 6b 65 6e 20 may be mistaken
1521e 66 6f 72 20 61 20 6a 6f 75 72 6e 61 6c 20 62 65 for a journal be
1521f 69 6e 67 20 63 72 65 61 74 65 64 20 62 79 20 61 ing created by a
15220 6e 20 61 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e n active.** tran
15221 73 61 63 74 69 6f 6e 20 69 6e 20 61 6e 6f 74 68 saction in anoth
15222 65 72 20 70 72 6f 63 65 73 73 2c 20 63 61 75 73 er process, caus
15223 69 6e 67 20 53 51 4c 69 74 65 20 74 6f 20 72 65 ing SQLite to re
15224 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ad from the data
15225 62 61 73 65 0a 2a 2a 20 77 69 74 68 6f 75 74 20 base.** without
15226 72 6f 6c 6c 69 6e 67 20 69 74 20 62 61 63 6b 2e rolling it back.
15227 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 .**.** To work a
15228 72 6f 75 6e 64 20 74 68 69 73 2c 20 69 66 20 61 round this, if a
15229 20 63 61 6c 6c 20 74 6f 20 78 55 6e 6c 6f 63 6b call to xUnlock
1522a 28 29 20 66 61 69 6c 73 20 77 68 65 6e 20 75 6e () fails when un
1522b 6c 6f 63 6b 69 6e 67 20 74 68 65 0a 2a 2a 20 64 locking the.** d
1522c 61 74 61 62 61 73 65 20 69 6e 20 74 68 65 20 45 atabase in the E
1522d 52 52 4f 52 20 73 74 61 74 65 2c 20 50 61 67 65 RROR state, Page
1522e 72 2e 65 4c 6f 63 6b 20 69 73 20 73 65 74 20 74 r.eLock is set t
1522f 6f 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 2e 20 o UNKNOWN_LOCK.
15230 49 74 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 63 68 It.** is only ch
15231 61 6e 67 65 64 20 62 61 63 6b 20 74 6f 20 61 20 anged back to a
15232 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 73 74 61 real locking sta
15233 74 65 20 61 66 74 65 72 20 61 20 73 75 63 63 65 te after a succe
15234 73 73 66 75 6c 20 63 61 6c 6c 0a 2a 2a 20 74 6f ssful call.** to
15235 20 78 4c 6f 63 6b 28 45 58 43 4c 55 53 49 56 45 xLock(EXCLUSIVE
15236 29 2e 20 41 6c 73 6f 2c 20 74 68 65 20 63 6f 64 ). Also, the cod
15237 65 20 74 6f 20 64 6f 20 74 68 65 20 4f 50 45 4e e to do the OPEN
15238 2d 3e 53 48 41 52 45 44 20 73 74 61 74 65 20 74 ->SHARED state t
15239 72 61 6e 73 69 74 69 6f 6e 0a 2a 2a 20 6f 6d 69 ransition.** omi
1523a 74 73 20 74 68 65 20 63 68 65 63 6b 20 66 6f 72 ts the check for
1523b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 69 a hot-journal i
1523c 66 20 50 61 67 65 72 2e 65 4c 6f 63 6b 20 69 73 f Pager.eLock is
1523d 20 73 65 74 20 74 6f 20 55 4e 4b 4e 4f 57 4e 5f set to UNKNOWN_
1523e 4c 4f 43 4b 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 LOCK .** lock. I
1523f 6e 73 74 65 61 64 2c 20 69 74 20 61 73 73 75 6d nstead, it assum
15240 65 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c es a hot-journal
15241 20 65 78 69 73 74 73 20 61 6e 64 20 6f 62 74 61 exists and obta
15242 69 6e 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 ins an EXCLUSIVE
15243 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 .** lock on the
15244 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65 database file be
15245 66 6f 72 65 20 61 74 74 65 6d 70 74 69 6e 67 20 fore attempting
15246 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e to roll it back.
15247 20 53 65 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a See function.**
15248 20 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b PagerSharedLock
15249 28 29 20 66 6f 72 20 6d 6f 72 65 20 64 65 74 61 () for more deta
1524a 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 72 2e il..**.** Pager.
1524b 65 4c 6f 63 6b 20 6d 61 79 20 6f 6e 6c 79 20 62 eLock may only b
1524c 65 20 73 65 74 20 74 6f 20 55 4e 4b 4e 4f 57 4e e set to UNKNOWN
1524d 5f 4c 4f 43 4b 20 77 68 65 6e 20 74 68 65 20 70 _LOCK when the p
1524e 61 67 65 72 20 69 73 20 69 6e 20 0a 2a 2a 20 50 ager is in .** P
1524f 41 47 45 52 5f 4f 50 45 4e 20 73 74 61 74 65 2e AGER_OPEN state.
15250 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 4b 4e .*/.#define UNKN
15251 4f 57 4e 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 OWN_LOCK
15252 20 20 20 20 20 20 20 20 28 45 58 43 4c 55 53 49 (EXCLUSI
15253 56 45 5f 4c 4f 43 4b 2b 31 29 0a 0a 2f 2a 0a 2a VE_LOCK+1)../*.*
15254 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 64 20 66 * A macro used f
15255 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 or invoking the
15256 63 6f 64 65 63 20 69 66 20 74 68 65 72 65 20 69 codec if there i
15257 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20 s one.*/.#ifdef
15258 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
15259 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 31 .# define CODEC1
1525a 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 5c 0a 20 20 (P,D,N,X,E) \.
1525b 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 65 63 20 if( P->xCodec
1525c 26 26 20 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e && P->xCodec(P->
1525d 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 3d 3d 30 pCodec,D,N,X)==0
1525e 20 29 7b 20 45 3b 20 7d 0a 23 20 64 65 66 69 6e ){ E; }.# defin
1525f 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 e CODEC2(P,D,N,X
15260 2c 45 2c 4f 29 20 5c 0a 20 20 20 20 69 66 28 20 ,E,O) \. if(
15261 50 2d 3e 78 43 6f 64 65 63 3d 3d 30 20 29 7b 20 P->xCodec==0 ){
15262 4f 3d 28 63 68 61 72 2a 29 44 3b 20 7d 65 6c 73 O=(char*)D; }els
15263 65 20 5c 0a 20 20 20 20 69 66 28 20 28 4f 3d 28 e \. if( (O=(
15264 63 68 61 72 2a 29 28 50 2d 3e 78 43 6f 64 65 63 char*)(P->xCodec
15265 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 (P->pCodec,D,N,X
15266 29 29 29 3d 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 )))==0 ){ E; }.#
15267 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 43 4f else.# define CO
15268 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 DEC1(P,D,N,X,E)
15269 20 20 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a 23 20 /* NO-OP */.#
1526a 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c define CODEC2(P,
1526b 44 2c 4e 2c 58 2c 45 2c 4f 29 20 4f 3d 28 63 68 D,N,X,E,O) O=(ch
1526c 61 72 2a 29 44 0a 23 65 6e 64 69 66 0a 0a 2f 2a ar*)D.#endif../*
1526d 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 .** The maximum
1526e 61 6c 6c 6f 77 65 64 20 73 65 63 74 6f 72 20 73 allowed sector s
1526f 69 7a 65 2e 20 36 34 4b 69 42 2e 20 49 66 20 74 ize. 64KiB. If t
15270 68 65 20 78 53 65 63 74 6f 72 73 69 7a 65 28 29 he xSectorsize()
15271 20 6d 65 74 68 6f 64 20 0a 2a 2a 20 72 65 74 75 method .** retu
15272 72 6e 73 20 61 20 76 61 6c 75 65 20 6c 61 72 67 rns a value larg
15273 65 72 20 74 68 61 6e 20 74 68 69 73 2c 20 74 68 er than this, th
15274 65 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 en MAX_SECTOR_SI
15275 5a 45 20 69 73 20 75 73 65 64 20 69 6e 73 74 65 ZE is used inste
15276 61 64 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c ad..** This coul
15277 64 20 63 6f 6e 63 65 69 76 61 62 6c 79 20 63 61 d conceivably ca
15278 75 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 66 use corruption f
15279 6f 6c 6c 6f 77 69 6e 67 20 61 20 70 6f 77 65 72 ollowing a power
1527a 20 66 61 69 6c 75 72 65 20 6f 6e 0a 2a 2a 20 73 failure on.** s
1527b 75 63 68 20 61 20 73 79 73 74 65 6d 2e 20 54 68 uch a system. Th
1527c 69 73 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 is is currently
1527d 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 20 an undocumented
1527e 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e limit..*/.#defin
1527f 65 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a e MAX_SECTOR_SIZ
15280 45 20 30 78 31 30 30 30 30 0a 0a 2f 2a 0a 2a 2a E 0x10000../*.**
15281 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
15282 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
15283 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 ructure is alloc
15284 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 61 63 ated for each ac
15285 74 69 76 65 0a 2a 2a 20 73 61 76 65 70 6f 69 6e tive.** savepoin
15286 74 20 61 6e 64 20 73 74 61 74 65 6d 65 6e 74 20 t and statement
15287 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 74 transaction in t
15288 68 65 20 73 79 73 74 65 6d 2e 20 41 6c 6c 20 73 he system. All s
15289 75 63 68 20 73 74 72 75 63 74 75 72 65 73 0a 2a uch structures.*
1528a 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 * are stored in
1528b 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 70 the Pager.aSavep
1528c 6f 69 6e 74 5b 5d 20 61 72 72 61 79 2c 20 77 68 oint[] array, wh
1528d 69 63 68 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ich is allocated
1528e 20 61 6e 64 0a 2a 2a 20 72 65 73 69 7a 65 64 20 and.** resized
1528f 75 73 69 6e 67 20 73 71 6c 69 74 65 33 52 65 61 using sqlite3Rea
15290 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 lloc()..**.** Wh
15291 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69 en a savepoint i
15292 73 20 63 72 65 61 74 65 64 2c 20 74 68 65 20 50 s created, the P
15293 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 agerSavepoint.iH
15294 64 72 4f 66 66 73 65 74 20 66 69 65 6c 64 20 69 drOffset field i
15295 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2e 20 49 s.** set to 0. I
15296 66 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 f a journal-head
15297 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e er is written in
15298 74 6f 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 to the main jour
15299 6e 61 6c 20 77 68 69 6c 65 0a 2a 2a 20 74 68 65 nal while.** the
1529a 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 63 savepoint is ac
1529b 74 69 76 65 2c 20 74 68 65 6e 20 69 48 64 72 4f tive, then iHdrO
1529c 66 66 73 65 74 20 69 73 20 73 65 74 20 74 6f 20 ffset is set to
1529d 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 20 the byte offset
1529e 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 .** immediately
1529f 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61 following the la
152a0 73 74 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 st journal recor
152a1 64 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 d written into t
152a2 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e he main.** journ
152a3 61 6c 20 62 65 66 6f 72 65 20 74 68 65 20 6a 6f al before the jo
152a4 75 72 6e 61 6c 2d 68 65 61 64 65 72 2e 20 54 68 urnal-header. Th
152a5 69 73 20 69 73 20 72 65 71 75 69 72 65 64 20 64 is is required d
152a6 75 72 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 0a uring savepoint.
152a7 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 28 73 65 65 ** rollback (see
152a8 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 pagerPlaybackSa
152a9 76 65 70 6f 69 6e 74 28 29 29 2e 0a 2a 2f 0a 74 vepoint())..*/.t
152aa 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 ypedef struct Pa
152ab 67 65 72 53 61 76 65 70 6f 69 6e 74 20 50 61 67 gerSavepoint Pag
152ac 65 72 53 61 76 65 70 6f 69 6e 74 3b 0a 73 74 72 erSavepoint;.str
152ad 75 63 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 uct PagerSavepoi
152ae 6e 74 20 7b 0a 20 20 69 36 34 20 69 4f 66 66 73 nt {. i64 iOffs
152af 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 et;
152b0 20 20 20 20 2f 2a 20 53 74 61 72 74 69 6e 67 20 /* Starting
152b1 6f 66 66 73 65 74 20 69 6e 20 6d 61 69 6e 20 6a offset in main j
152b2 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 ournal */. i64
152b3 69 48 64 72 4f 66 66 73 65 74 3b 20 20 20 20 20 iHdrOffset;
152b4 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 65 20 /* See
152b5 61 62 6f 76 65 20 2a 2f 0a 20 20 42 69 74 76 65 above */. Bitve
152b6 63 20 2a 70 49 6e 53 61 76 65 70 6f 69 6e 74 3b c *pInSavepoint;
152b7 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6f /* Set o
152b8 66 20 70 61 67 65 73 20 69 6e 20 74 68 69 73 20 f pages in this
152b9 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 50 savepoint */. P
152ba 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 20 20 20 gno nOrig;
152bb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
152bc 72 69 67 69 6e 61 6c 20 6e 75 6d 62 65 72 20 6f riginal number o
152bd 66 20 70 61 67 65 73 20 69 6e 20 66 69 6c 65 20 f pages in file
152be 2a 2f 0a 20 20 50 67 6e 6f 20 69 53 75 62 52 65 */. Pgno iSubRe
152bf 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
152c0 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 /* Index of fi
152c1 72 73 74 20 72 65 63 6f 72 64 20 69 6e 20 73 75 rst record in su
152c2 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 69 66 b-journal */.#if
152c3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
152c4 5f 57 41 4c 0a 20 20 75 33 32 20 61 57 61 6c 44 _WAL. u32 aWalD
152c5 61 74 61 5b 57 41 4c 5f 53 41 56 45 50 4f 49 4e ata[WAL_SAVEPOIN
152c6 54 5f 4e 44 41 54 41 5d 3b 20 20 20 20 20 20 20 T_NDATA];
152c7 20 2f 2a 20 57 41 4c 20 73 61 76 65 70 6f 69 6e /* WAL savepoin
152c8 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 23 65 6e t context */.#en
152c9 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 dif.};../*.** A
152ca 6f 70 65 6e 20 70 61 67 65 20 63 61 63 68 65 20 open page cache
152cb 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
152cc 66 20 73 74 72 75 63 74 20 50 61 67 65 72 2e 20 f struct Pager.
152cd 41 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 A description of
152ce 0a 2a 2a 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 .** some of the
152cf 6d 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74 20 6d more important m
152d0 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 73 20 ember variables
152d1 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 65 follows:.**.** e
152d2 53 74 61 74 65 0a 2a 2a 0a 2a 2a 20 20 20 54 68 State.**.** Th
152d3 65 20 63 75 72 72 65 6e 74 20 27 73 74 61 74 65 e current 'state
152d4 27 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 6f ' of the pager o
152d5 62 6a 65 63 74 2e 20 53 65 65 20 74 68 65 20 63 bject. See the c
152d6 6f 6d 6d 65 6e 74 20 61 6e 64 20 73 74 61 74 65 omment and state
152d7 0a 2a 2a 20 20 20 64 69 61 67 72 61 6d 20 61 62 .** diagram ab
152d8 6f 76 65 20 66 6f 72 20 61 20 64 65 73 63 72 69 ove for a descri
152d9 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 ption of the pag
152da 65 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 er state..**.**
152db 65 4c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 20 20 46 6f eLock.**.** Fo
152dc 72 20 61 20 72 65 61 6c 20 6f 6e 2d 64 69 73 6b r a real on-disk
152dd 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 63 database, the c
152de 75 72 72 65 6e 74 20 6c 6f 63 6b 20 68 65 6c 64 urrent lock held
152df 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
152e0 20 66 69 6c 65 20 2d 0a 2a 2a 20 20 20 4e 4f 5f file -.** NO_
152e1 4c 4f 43 4b 2c 20 53 48 41 52 45 44 5f 4c 4f 43 LOCK, SHARED_LOC
152e2 4b 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b K, RESERVED_LOCK
152e3 20 6f 72 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f or EXCLUSIVE_LO
152e4 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 6f 72 20 CK..**.** For
152e5 61 20 74 65 6d 70 6f 72 61 72 79 20 6f 72 20 69 a temporary or i
152e6 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
152e7 65 20 28 6e 65 69 74 68 65 72 20 6f 66 20 77 68 e (neither of wh
152e8 69 63 68 20 72 65 71 75 69 72 65 20 61 6e 79 0a ich require any.
152e9 2a 2a 20 20 20 6c 6f 63 6b 73 29 2c 20 74 68 69 ** locks), thi
152ea 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 61 6c s variable is al
152eb 77 61 79 73 20 73 65 74 20 74 6f 20 45 58 43 4c ways set to EXCL
152ec 55 53 49 56 45 5f 4c 4f 43 4b 2e 20 53 69 6e 63 USIVE_LOCK. Sinc
152ed 65 20 73 75 63 68 0a 2a 2a 20 20 20 64 61 74 61 e such.** data
152ee 62 61 73 65 73 20 61 6c 77 61 79 73 20 68 61 76 bases always hav
152ef 65 20 50 61 67 65 72 2e 65 78 63 6c 75 73 69 76 e Pager.exclusiv
152f0 65 4d 6f 64 65 3d 3d 31 2c 20 74 68 69 73 20 74 eMode==1, this t
152f1 72 69 63 6b 73 20 74 68 65 20 70 61 67 65 72 0a ricks the pager.
152f2 2a 2a 20 20 20 6c 6f 67 69 63 20 69 6e 74 6f 20 ** logic into
152f3 74 68 69 6e 6b 69 6e 67 20 74 68 61 74 20 69 74 thinking that it
152f4 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 6c 6c already has all
152f5 20 74 68 65 20 6c 6f 63 6b 73 20 69 74 20 77 69 the locks it wi
152f6 6c 6c 20 65 76 65 72 0a 2a 2a 20 20 20 6e 65 65 ll ever.** nee
152f7 64 20 28 61 6e 64 20 6e 6f 20 72 65 61 73 6f 6e d (and no reason
152f8 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 6d to release them
152f9 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 73 6f )..**.** In so
152fa 6d 65 20 28 6f 62 73 63 75 72 65 29 20 63 69 72 me (obscure) cir
152fb 63 75 6d 73 74 61 6e 63 65 73 2c 20 74 68 69 73 cumstances, this
152fc 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20 61 6c variable may al
152fd 73 6f 20 62 65 20 73 65 74 20 74 6f 0a 2a 2a 20 so be set to.**
152fe 20 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 2e 20 UNKNOWN_LOCK.
152ff 53 65 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 See the comment
15300 61 62 6f 76 65 20 74 68 65 20 23 64 65 66 69 6e above the #defin
15301 65 20 6f 66 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 e of UNKNOWN_LOC
15302 4b 20 66 6f 72 0a 2a 2a 20 20 20 64 65 74 61 69 K for.** detai
15303 6c 73 2e 0a 2a 2a 0a 2a 2a 20 63 68 61 6e 67 65 ls..**.** change
15304 43 6f 75 6e 74 44 6f 6e 65 0a 2a 2a 0a 2a 2a 20 CountDone.**.**
15305 20 20 54 68 69 73 20 62 6f 6f 6c 65 61 6e 20 76 This boolean v
15306 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20 ariable is used
15307 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
15308 74 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 t the change-cou
15309 6e 74 65 72 20 0a 2a 2a 20 20 20 28 74 68 65 20 nter .** (the
1530a 34 2d 62 79 74 65 20 68 65 61 64 65 72 20 66 69 4-byte header fi
1530b 65 6c 64 20 61 74 20 62 79 74 65 20 6f 66 66 73 eld at byte offs
1530c 65 74 20 32 34 20 6f 66 20 74 68 65 20 64 61 74 et 24 of the dat
1530d 61 62 61 73 65 20 66 69 6c 65 29 20 69 73 20 0a abase file) is .
1530e 2a 2a 20 20 20 6e 6f 74 20 75 70 64 61 74 65 64 ** not updated
1530f 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e more often than
15310 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 2a 2a 0a necessary. .**.
15311 2a 2a 20 20 20 49 74 20 69 73 20 73 65 74 20 74 ** It is set t
15312 6f 20 74 72 75 65 20 77 68 65 6e 20 74 68 65 20 o true when the
15313 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 66 change-counter f
15314 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64 2c ield is updated,
15315 20 77 68 69 63 68 20 0a 2a 2a 20 20 20 63 61 6e which .** can
15316 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20 only happen if
15317 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
15318 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 k is held on the
15319 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
1531a 2a 2a 20 20 20 49 74 20 69 73 20 63 6c 65 61 72 ** It is clear
1531b 65 64 20 28 73 65 74 20 74 6f 20 66 61 6c 73 65 ed (set to false
1531c 29 20 77 68 65 6e 65 76 65 72 20 61 6e 20 65 78 ) whenever an ex
1531d 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 clusive lock is
1531e 0a 2a 2a 20 20 20 72 65 6c 69 6e 71 75 69 73 68 .** relinquish
1531f 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ed on the databa
15320 73 65 20 66 69 6c 65 2e 20 45 61 63 68 20 74 69 se file. Each ti
15321 6d 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e me a transaction
15322 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2c 0a 2a is committed,.*
15323 2a 20 20 20 54 68 65 20 63 68 61 6e 67 65 43 6f * The changeCo
15324 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 69 73 20 untDone flag is
15325 69 6e 73 70 65 63 74 65 64 2e 20 49 66 20 69 74 inspected. If it
15326 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 77 6f is true, the wo
15327 72 6b 20 6f 66 0a 2a 2a 20 20 20 75 70 64 61 74 rk of.** updat
15328 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65 2d 63 ing the change-c
15329 6f 75 6e 74 65 72 20 69 73 20 6f 6d 69 74 74 65 ounter is omitte
1532a 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e d for the curren
1532b 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a t transaction..*
1532c 2a 0a 2a 2a 20 20 20 54 68 69 73 20 6d 65 63 68 *.** This mech
1532d 61 6e 69 73 6d 20 6d 65 61 6e 73 20 74 68 61 74 anism means that
1532e 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e when running in
1532f 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c exclusive mode,
15330 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a a connection .*
15331 2a 20 20 20 6e 65 65 64 20 6f 6e 6c 79 20 75 70 * need only up
15332 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d date the change-
15333 63 6f 75 6e 74 65 72 20 6f 6e 63 65 2c 20 66 6f counter once, fo
15334 72 20 74 68 65 20 66 69 72 73 74 20 74 72 61 6e r the first tran
15335 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 63 6f 6d saction.** com
15336 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 73 65 mitted..**.** se
15337 74 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20 tMaster.**.**
15338 57 68 65 6e 20 50 61 67 65 72 43 6f 6d 6d 69 74 When PagerCommit
15339 50 68 61 73 65 4f 6e 65 28 29 20 69 73 20 63 61 PhaseOne() is ca
1533a 6c 6c 65 64 20 74 6f 20 63 6f 6d 6d 69 74 20 61 lled to commit a
1533b 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 transaction, it
1533c 20 6d 61 79 0a 2a 2a 20 20 20 28 6f 72 20 6d 61 may.** (or ma
1533d 79 20 6e 6f 74 29 20 73 70 65 63 69 66 79 20 61 y not) specify a
1533e 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 master-journal
1533f 6e 61 6d 65 20 74 6f 20 62 65 20 77 72 69 74 74 name to be writt
15340 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 en into the .**
15341 20 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 journal file b
15342 65 66 6f 72 65 20 69 74 20 69 73 20 73 79 6e 63 efore it is sync
15343 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a ed to disk..**.*
15344 2a 20 20 20 57 68 65 74 68 65 72 20 6f 72 20 6e * Whether or n
15345 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ot a journal fil
15346 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 6d 61 73 e contains a mas
15347 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e ter-journal poin
15348 74 65 72 20 61 66 66 65 63 74 73 20 0a 2a 2a 20 ter affects .**
15349 20 20 74 68 65 20 77 61 79 20 69 6e 20 77 68 69 the way in whi
1534a 63 68 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ch the journal f
1534b 69 6c 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 ile is finalized
1534c 20 61 66 74 65 72 20 74 68 65 20 74 72 61 6e 73 after the trans
1534d 61 63 74 69 6f 6e 20 69 73 20 0a 2a 2a 20 20 20 action is .**
1534e 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c committed or rol
1534f 6c 65 64 20 62 61 63 6b 20 77 68 65 6e 20 72 75 led back when ru
15350 6e 6e 69 6e 67 20 69 6e 20 22 6a 6f 75 72 6e 61 nning in "journa
15351 6c 5f 6d 6f 64 65 3d 50 45 52 53 49 53 54 22 20 l_mode=PERSIST"
15352 6d 6f 64 65 2e 0a 2a 2a 20 20 20 49 66 20 61 20 mode..** If a
15353 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 journal file doe
15354 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 s not contain a
15355 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 master-journal p
15356 6f 69 6e 74 65 72 2c 20 69 74 20 69 73 0a 2a 2a ointer, it is.**
15357 20 20 20 66 69 6e 61 6c 69 7a 65 64 20 62 79 20 finalized by
15358 6f 76 65 72 77 72 69 74 69 6e 67 20 74 68 65 20 overwriting the
15359 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 first journal he
1535a 61 64 65 72 20 77 69 74 68 20 7a 65 72 6f 65 73 ader with zeroes
1535b 2e 20 49 66 0a 2a 2a 20 20 20 69 74 20 64 6f 65 . If.** it doe
1535c 73 20 63 6f 6e 74 61 69 6e 20 61 20 6d 61 73 74 s contain a mast
1535d 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 er-journal point
1535e 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 er the journal f
1535f 69 6c 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 ile is finalized
15360 20 0a 2a 2a 20 20 20 62 79 20 74 72 75 6e 63 61 .** by trunca
15361 74 69 6e 67 20 69 74 20 74 6f 20 7a 65 72 6f 20 ting it to zero
15362 62 79 74 65 73 2c 20 6a 75 73 74 20 61 73 20 69 bytes, just as i
15363 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e f the connection
15364 20 77 65 72 65 20 0a 2a 2a 20 20 20 72 75 6e 6e were .** runn
15365 69 6e 67 20 69 6e 20 22 6a 6f 75 72 6e 61 6c 5f ing in "journal_
15366 6d 6f 64 65 3d 74 72 75 6e 63 61 74 65 22 20 6d mode=truncate" m
15367 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 4a 6f 75 ode..**.** Jou
15368 72 6e 61 6c 20 66 69 6c 65 73 20 74 68 61 74 20 rnal files that
15369 63 6f 6e 74 61 69 6e 20 6d 61 73 74 65 72 20 6a contain master j
1536a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 73 20 ournal pointers
1536b 63 61 6e 6e 6f 74 20 62 65 20 66 69 6e 61 6c 69 cannot be finali
1536c 7a 65 64 0a 2a 2a 20 20 20 73 69 6d 70 6c 79 20 zed.** simply
1536d 62 79 20 6f 76 65 72 77 72 69 74 69 6e 67 20 74 by overwriting t
1536e 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c he first journal
1536f 2d 68 65 61 64 65 72 20 77 69 74 68 20 7a 65 72 -header with zer
15370 6f 65 73 2c 20 61 73 20 74 68 65 0a 2a 2a 20 20 oes, as the.**
15371 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
15372 70 6f 69 6e 74 65 72 20 63 6f 75 6c 64 20 69 6e pointer could in
15373 74 65 72 66 65 72 65 20 77 69 74 68 20 68 6f 74 terfere with hot
15374 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 -journal rollbac
15375 6b 20 6f 66 20 61 6e 79 0a 2a 2a 20 20 20 73 75 k of any.** su
15376 62 73 65 71 75 65 6e 74 6c 79 20 69 6e 74 65 72 bsequently inter
15377 72 75 70 74 65 64 20 74 72 61 6e 73 61 63 74 69 rupted transacti
15378 6f 6e 20 74 68 61 74 20 72 65 75 73 65 73 20 74 on that reuses t
15379 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
1537a 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 66 6c 61 .**.** The fla
1537b 67 20 69 73 20 63 6c 65 61 72 65 64 20 61 73 20 g is cleared as
1537c 73 6f 6f 6e 20 61 73 20 74 68 65 20 6a 6f 75 72 soon as the jour
1537d 6e 61 6c 20 66 69 6c 65 20 69 73 20 66 69 6e 61 nal file is fina
1537e 6c 69 7a 65 64 20 28 65 69 74 68 65 72 0a 2a 2a lized (either.**
1537f 20 20 20 62 79 20 50 61 67 65 72 43 6f 6d 6d 69 by PagerCommi
15380 74 50 68 61 73 65 54 77 6f 20 6f 72 20 50 61 67 tPhaseTwo or Pag
15381 65 72 52 6f 6c 6c 62 61 63 6b 29 2e 20 49 66 20 erRollback). If
15382 61 6e 20 49 4f 20 65 72 72 6f 72 20 70 72 65 76 an IO error prev
15383 65 6e 74 73 20 74 68 65 0a 2a 2a 20 20 20 6a 6f ents the.** jo
15384 75 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20 urnal file from
15385 62 65 69 6e 67 20 73 75 63 63 65 73 73 66 75 6c being successful
15386 6c 79 20 66 69 6e 61 6c 69 7a 65 64 2c 20 74 68 ly finalized, th
15387 65 20 73 65 74 4d 61 73 74 65 72 20 66 6c 61 67 e setMaster flag
15388 0a 2a 2a 20 20 20 69 73 20 63 6c 65 61 72 65 64 .** is cleared
15389 20 61 6e 79 77 61 79 20 28 61 6e 64 20 74 68 65 anyway (and the
1538a 20 70 61 67 65 72 20 77 69 6c 6c 20 6d 6f 76 65 pager will move
1538b 20 74 6f 20 45 52 52 4f 52 20 73 74 61 74 65 29 to ERROR state)
1538c 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e 6f 74 53 70 69 ..**.** doNotSpi
1538d 6c 6c 2c 20 64 6f 4e 6f 74 53 79 6e 63 53 70 69 ll, doNotSyncSpi
1538e 6c 6c 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 73 65 ll.**.** These
1538f 20 74 77 6f 20 62 6f 6f 6c 65 61 6e 20 76 61 72 two boolean var
15390 69 61 62 6c 65 73 20 63 6f 6e 74 72 6f 6c 20 74 iables control t
15391 68 65 20 62 65 68 61 76 69 6f 75 72 20 6f 66 20 he behaviour of
15392 63 61 63 68 65 2d 73 70 69 6c 6c 73 0a 2a 2a 20 cache-spills.**
15393 20 20 28 63 61 6c 6c 73 20 6d 61 64 65 20 62 79 (calls made by
15394 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75 the pcache modu
15395 6c 65 20 74 6f 20 74 68 65 20 70 61 67 65 72 53 le to the pagerS
15396 74 72 65 73 73 28 29 20 72 6f 75 74 69 6e 65 20 tress() routine
15397 74 6f 0a 2a 2a 20 20 20 77 72 69 74 65 20 63 61 to.** write ca
15398 63 68 65 64 20 64 61 74 61 20 74 6f 20 74 68 65 ched data to the
15399 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 69 6e 20 file-system in
1539a 6f 72 64 65 72 20 74 6f 20 66 72 65 65 20 75 70 order to free up
1539b 20 6d 65 6d 6f 72 79 29 2e 0a 2a 2a 0a 2a 2a 20 memory)..**.**
1539c 20 20 57 68 65 6e 20 64 6f 4e 6f 74 53 70 69 6c When doNotSpil
1539d 6c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 77 l is non-zero, w
1539e 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 riting to the da
1539f 74 61 62 61 73 65 20 66 72 6f 6d 20 70 61 67 65 tabase from page
153a0 72 53 74 72 65 73 73 28 29 0a 2a 2a 20 20 20 69 rStress().** i
153a1 73 20 64 69 73 61 62 6c 65 64 20 61 6c 74 6f 67 s disabled altog
153a2 65 74 68 65 72 2e 20 54 68 69 73 20 69 73 20 64 ether. This is d
153a3 6f 6e 65 20 69 6e 20 61 20 76 65 72 79 20 6f 62 one in a very ob
153a4 73 63 75 72 65 20 63 61 73 65 20 74 68 61 74 0a scure case that.
153a5 2a 2a 20 20 20 63 6f 6d 65 73 20 75 70 20 64 75 ** comes up du
153a6 72 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 20 72 ring savepoint r
153a7 6f 6c 6c 62 61 63 6b 20 74 68 61 74 20 72 65 71 ollback that req
153a8 75 69 72 65 73 20 74 68 65 20 70 63 61 63 68 65 uires the pcache
153a9 20 6d 6f 64 75 6c 65 0a 2a 2a 20 20 20 74 6f 20 module.** to
153aa 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 allocate a new p
153ab 61 67 65 20 74 6f 20 70 72 65 76 65 6e 74 20 74 age to prevent t
153ac 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
153ad 66 72 6f 6d 20 62 65 69 6e 67 20 77 72 69 74 74 from being writt
153ae 65 6e 0a 2a 2a 20 20 20 77 68 69 6c 65 20 69 74 en.** while it
153af 20 69 73 20 62 65 69 6e 67 20 74 72 61 76 65 72 is being traver
153b0 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 70 sed by code in p
153b1 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 2e ager_playback().
153b2 0a 2a 2a 20 0a 2a 2a 20 20 20 49 66 20 64 6f 4e .** .** If doN
153b3 6f 74 53 79 6e 63 53 70 69 6c 6c 20 69 73 20 6e otSyncSpill is n
153b4 6f 6e 2d 7a 65 72 6f 2c 20 77 72 69 74 69 6e 67 on-zero, writing
153b5 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
153b6 20 66 72 6f 6d 20 70 61 67 65 72 53 74 72 65 73 from pagerStres
153b7 73 28 29 0a 2a 2a 20 20 20 69 73 20 70 65 72 6d s().** is perm
153b8 69 74 74 65 64 2c 20 62 75 74 20 73 79 6e 63 69 itted, but synci
153b9 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ng the journal f
153ba 69 6c 65 20 69 73 20 6e 6f 74 2e 20 54 68 69 73 ile is not. This
153bb 20 66 6c 61 67 20 69 73 20 73 65 74 0a 2a 2a 20 flag is set.**
153bc 20 20 62 79 20 73 71 6c 69 74 65 33 50 61 67 65 by sqlite3Page
153bd 72 57 72 69 74 65 28 29 20 77 68 65 6e 20 74 68 rWrite() when th
153be 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 65 e file-system se
153bf 63 74 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61 72 ctor-size is lar
153c0 67 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 74 68 ger than.** th
153c1 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2d e database page-
153c2 73 69 7a 65 20 69 6e 20 6f 72 64 65 72 20 74 6f size in order to
153c3 20 70 72 65 76 65 6e 74 20 61 20 6a 6f 75 72 6e prevent a journ
153c4 61 6c 20 73 79 6e 63 20 66 72 6f 6d 20 68 61 70 al sync from hap
153c5 70 65 6e 69 6e 67 20 0a 2a 2a 20 20 20 69 6e 20 pening .** in
153c6 62 65 74 77 65 65 6e 20 74 68 65 20 6a 6f 75 72 between the jour
153c7 6e 61 6c 6c 69 6e 67 20 6f 66 20 74 77 6f 20 70 nalling of two p
153c8 61 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 ages on the same
153c9 20 73 65 63 74 6f 72 2e 20 0a 2a 2a 0a 2a 2a 20 sector. .**.**
153ca 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 0a 2a 2a 0a subjInMemory.**.
153cb 2a 2a 20 20 20 54 68 69 73 20 69 73 20 61 20 62 ** This is a b
153cc 6f 6f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 2e oolean variable.
153cd 20 49 66 20 74 72 75 65 2c 20 74 68 65 6e 20 61 If true, then a
153ce 6e 79 20 72 65 71 75 69 72 65 64 20 73 75 62 2d ny required sub-
153cf 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 69 73 20 journal.** is
153d0 6f 70 65 6e 65 64 20 61 73 20 61 6e 20 69 6e 2d opened as an in-
153d1 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 memory journal f
153d2 69 6c 65 2e 20 49 66 20 66 61 6c 73 65 2c 20 74 ile. If false, t
153d3 68 65 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a hen in-memory.**
153d4 20 20 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 sub-journals
153d5 61 72 65 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f are only used fo
153d6 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 r in-memory page
153d7 72 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 r files..**.**
153d8 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 This variable i
153d9 73 20 75 70 64 61 74 65 64 20 62 79 20 74 68 65 s updated by the
153da 20 75 70 70 65 72 20 6c 61 79 65 72 20 65 61 63 upper layer eac
153db 68 20 74 69 6d 65 20 61 20 6e 65 77 20 0a 2a 2a h time a new .**
153dc 20 20 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 write-transac
153dd 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2e 0a tion is opened..
153de 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65 2c 20 64 62 **.** dbSize, db
153df 4f 72 69 67 53 69 7a 65 2c 20 64 62 46 69 6c 65 OrigSize, dbFile
153e0 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 56 61 72 Size.**.** Var
153e1 69 61 62 6c 65 20 64 62 53 69 7a 65 20 69 73 20 iable dbSize is
153e2 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 set to the numbe
153e3 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
153e4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
153e5 0a 2a 2a 20 20 20 49 74 20 69 73 20 76 61 6c 69 .** It is vali
153e6 64 20 69 6e 20 50 41 47 45 52 5f 52 45 41 44 45 d in PAGER_READE
153e7 52 20 61 6e 64 20 68 69 67 68 65 72 20 73 74 61 R and higher sta
153e8 74 65 73 20 28 61 6c 6c 20 73 74 61 74 65 73 20 tes (all states
153e9 65 78 63 65 70 74 20 66 6f 72 0a 2a 2a 20 20 20 except for.**
153ea 4f 50 45 4e 20 61 6e 64 20 45 52 52 4f 52 29 2e OPEN and ERROR).
153eb 20 0a 2a 2a 0a 2a 2a 20 20 20 64 62 53 69 7a 65 .**.** dbSize
153ec 20 69 73 20 73 65 74 20 62 61 73 65 64 20 6f 6e is set based on
153ed 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
153ee 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
153ef 77 68 69 63 68 20 6d 61 79 20 62 65 20 0a 2a 2a which may be .**
153f0 20 20 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 larger than t
153f1 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 he size of the d
153f2 61 74 61 62 61 73 65 20 28 74 68 65 20 76 61 6c atabase (the val
153f3 75 65 20 73 74 6f 72 65 64 20 61 74 20 6f 66 66 ue stored at off
153f4 73 65 74 0a 2a 2a 20 20 20 32 38 20 6f 66 20 74 set.** 28 of t
153f5 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61 64 he database head
153f6 65 72 20 62 79 20 74 68 65 20 62 74 72 65 65 29 er by the btree)
153f7 2e 20 49 66 20 74 68 65 20 73 69 7a 65 20 6f 66 . If the size of
153f8 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 20 20 69 the file.** i
153f9 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 s not an integer
153fa 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 multiple of the
153fb 20 70 61 67 65 2d 73 69 7a 65 2c 20 74 68 65 20 page-size, the
153fc 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 0a value stored in.
153fd 2a 2a 20 20 20 64 62 53 69 7a 65 20 69 73 20 72 ** dbSize is r
153fe 6f 75 6e 64 65 64 20 64 6f 77 6e 20 28 69 2e 65 ounded down (i.e
153ff 2e 20 61 20 35 4b 42 20 66 69 6c 65 20 77 69 74 . a 5KB file wit
15400 68 20 32 4b 20 70 61 67 65 2d 73 69 7a 65 20 68 h 2K page-size h
15401 61 73 20 64 62 53 69 7a 65 3d 3d 32 29 2e 0a 2a as dbSize==2)..*
15402 2a 20 20 20 45 78 63 65 70 74 2c 20 61 6e 79 20 * Except, any
15403 66 69 6c 65 20 74 68 61 74 20 69 73 20 67 72 65 file that is gre
15404 61 74 65 72 20 74 68 61 6e 20 30 20 62 79 74 65 ater than 0 byte
15405 73 20 69 6e 20 73 69 7a 65 20 69 73 20 63 6f 6e s in size is con
15406 73 69 64 65 72 65 64 0a 2a 2a 20 20 20 74 6f 20 sidered.** to
15407 68 61 76 65 20 61 74 20 6c 65 61 73 74 20 6f 6e have at least on
15408 65 20 70 61 67 65 2e 20 28 69 2e 65 2e 20 61 20 e page. (i.e. a
15409 31 4b 42 20 66 69 6c 65 20 77 69 74 68 20 32 4b 1KB file with 2K
1540a 20 70 61 67 65 2d 73 69 7a 65 20 6c 65 61 64 73 page-size leads
1540b 0a 2a 2a 20 20 20 74 6f 20 64 62 53 69 7a 65 3d .** to dbSize=
1540c 3d 31 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 75 72 =1)..**.** Dur
1540d 69 6e 67 20 61 20 77 72 69 74 65 2d 74 72 61 6e ing a write-tran
1540e 73 61 63 74 69 6f 6e 2c 20 69 66 20 70 61 67 65 saction, if page
1540f 73 20 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 s with page-numb
15410 65 72 73 20 67 72 65 61 74 65 72 20 74 68 61 6e ers greater than
15411 0a 2a 2a 20 20 20 64 62 53 69 7a 65 20 61 72 65 .** dbSize are
15412 20 6d 6f 64 69 66 69 65 64 20 69 6e 20 74 68 65 modified in the
15413 20 63 61 63 68 65 2c 20 64 62 53 69 7a 65 20 69 cache, dbSize i
15414 73 20 75 70 64 61 74 65 64 20 61 63 63 6f 72 64 s updated accord
15415 69 6e 67 6c 79 2e 0a 2a 2a 20 20 20 53 69 6d 69 ingly..** Simi
15416 6c 61 72 6c 79 2c 20 69 66 20 74 68 65 20 64 61 larly, if the da
15417 74 61 62 61 73 65 20 69 73 20 74 72 75 6e 63 61 tabase is trunca
15418 74 65 64 20 75 73 69 6e 67 20 50 61 67 65 72 54 ted using PagerT
15419 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 2c 20 runcateImage(),
1541a 0a 2a 2a 20 20 20 64 62 53 69 7a 65 20 69 73 20 .** dbSize is
1541b 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 updated..**.**
1541c 20 56 61 72 69 61 62 6c 65 73 20 64 62 4f 72 69 Variables dbOri
1541d 67 53 69 7a 65 20 61 6e 64 20 64 62 46 69 6c 65 gSize and dbFile
1541e 53 69 7a 65 20 61 72 65 20 76 61 6c 69 64 20 69 Size are valid i
1541f 6e 20 73 74 61 74 65 73 20 0a 2a 2a 20 20 20 50 n states .** P
15420 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b AGER_WRITER_LOCK
15421 45 44 20 61 6e 64 20 68 69 67 68 65 72 2e 20 64 ED and higher. d
15422 62 4f 72 69 67 53 69 7a 65 20 69 73 20 61 20 63 bOrigSize is a c
15423 6f 70 79 20 6f 66 20 74 68 65 20 64 62 53 69 7a opy of the dbSiz
15424 65 0a 2a 2a 20 20 20 76 61 72 69 61 62 6c 65 20 e.** variable
15425 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 at the start of
15426 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
15427 20 49 74 20 69 73 20 75 73 65 64 20 64 75 72 69 It is used duri
15428 6e 67 20 72 6f 6c 6c 62 61 63 6b 2c 0a 2a 2a 20 ng rollback,.**
15429 20 20 61 6e 64 20 74 6f 20 64 65 74 65 72 6d 69 and to determi
1542a 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ne whether or no
1542b 74 20 70 61 67 65 73 20 6e 65 65 64 20 74 6f 20 t pages need to
1542c 62 65 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 62 65 be journalled be
1542d 66 6f 72 65 0a 2a 2a 20 20 20 62 65 69 6e 67 20 fore.** being
1542e 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 modified..**.**
1542f 20 20 54 68 72 6f 75 67 68 6f 75 74 20 61 20 77 Throughout a w
15430 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
15431 2c 20 64 62 46 69 6c 65 53 69 7a 65 20 63 6f 6e , dbFileSize con
15432 74 61 69 6e 73 20 74 68 65 20 73 69 7a 65 20 6f tains the size o
15433 66 0a 2a 2a 20 20 20 74 68 65 20 66 69 6c 65 20 f.** the file
15434 6f 6e 20 64 69 73 6b 20 69 6e 20 70 61 67 65 73 on disk in pages
15435 2e 20 49 74 20 69 73 20 73 65 74 20 74 6f 20 61 . It is set to a
15436 20 63 6f 70 79 20 6f 66 20 64 62 53 69 7a 65 20 copy of dbSize
15437 77 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 77 72 when the.** wr
15438 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite-transaction
15439 69 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2c is first opened,
1543a 20 61 6e 64 20 75 70 64 61 74 65 64 20 77 68 65 and updated whe
1543b 6e 20 56 46 53 20 63 61 6c 6c 73 20 61 72 65 20 n VFS calls are
1543c 6d 61 64 65 0a 2a 2a 20 20 20 74 6f 20 77 72 69 made.** to wri
1543d 74 65 20 6f 72 20 74 72 75 6e 63 61 74 65 20 74 te or truncate t
1543e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1543f 20 6f 6e 20 64 69 73 6b 2e 20 0a 2a 2a 0a 2a 2a on disk. .**.**
15440 20 20 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 73 The only reas
15441 6f 6e 20 74 68 65 20 64 62 46 69 6c 65 53 69 7a on the dbFileSiz
15442 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 72 65 e variable is re
15443 71 75 69 72 65 64 20 69 73 20 74 6f 20 73 75 70 quired is to sup
15444 70 72 65 73 73 20 0a 2a 2a 20 20 20 75 6e 6e 65 press .** unne
15445 63 65 73 73 61 72 79 20 63 61 6c 6c 73 20 74 6f cessary calls to
15446 20 78 54 72 75 6e 63 61 74 65 28 29 20 61 66 74 xTruncate() aft
15447 65 72 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 er committing a
15448 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 2c transaction. If,
15449 20 0a 2a 2a 20 20 20 77 68 65 6e 20 61 20 74 72 .** when a tr
1544a 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d ansaction is com
1544b 6d 69 74 74 65 64 2c 20 74 68 65 20 64 62 46 69 mitted, the dbFi
1544c 6c 65 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 leSize variable
1544d 69 6e 64 69 63 61 74 65 73 20 0a 2a 2a 20 20 20 indicates .**
1544e 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 that the databas
1544f 65 20 66 69 6c 65 20 69 73 20 6c 61 72 67 65 72 e file is larger
15450 20 74 68 61 6e 20 74 68 65 20 64 61 74 61 62 61 than the databa
15451 73 65 20 69 6d 61 67 65 20 28 50 61 67 65 72 2e se image (Pager.
15452 64 62 53 69 7a 65 29 2c 20 0a 2a 2a 20 20 20 70 dbSize), .** p
15453 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 29 20 ager_truncate()
15454 69 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 20 70 is called. The p
15455 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 29 20 ager_truncate()
15456 63 61 6c 6c 20 75 73 65 73 20 78 46 69 6c 65 73 call uses xFiles
15457 69 7a 65 28 29 0a 2a 2a 20 20 20 74 6f 20 6d 65 ize().** to me
15458 61 73 75 72 65 20 74 68 65 20 64 61 74 61 62 61 asure the databa
15459 73 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2c se file on disk,
1545a 20 61 6e 64 20 74 68 65 6e 20 74 72 75 6e 63 61 and then trunca
1545b 74 65 73 20 69 74 20 69 66 20 72 65 71 75 69 72 tes it if requir
1545c 65 64 2e 0a 2a 2a 20 20 20 64 62 46 69 6c 65 53 ed..** dbFileS
1545d 69 7a 65 20 69 73 20 6e 6f 74 20 75 73 65 64 20 ize is not used
1545e 77 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 when rolling bac
1545f 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e k a transaction.
15460 20 49 6e 20 74 68 69 73 20 63 61 73 65 0a 2a 2a In this case.**
15461 20 20 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 pager_truncat
15462 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 75 6e e() is called un
15463 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 28 77 conditionally (w
15464 68 69 63 68 20 6d 65 61 6e 73 20 74 68 65 72 65 hich means there
15465 20 6d 61 79 20 62 65 0a 2a 2a 20 20 20 61 20 63 may be.** a c
15466 61 6c 6c 20 74 6f 20 78 46 69 6c 65 73 69 7a 65 all to xFilesize
15467 28 29 20 74 68 61 74 20 69 73 20 6e 6f 74 20 73 () that is not s
15468 74 72 69 63 74 6c 79 20 72 65 71 75 69 72 65 64 trictly required
15469 29 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 ). In either cas
1546a 65 2c 0a 2a 2a 20 20 20 70 61 67 65 72 5f 74 72 e,.** pager_tr
1546b 75 6e 63 61 74 65 28 29 20 6d 61 79 20 63 61 75 uncate() may cau
1546c 73 65 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 se the file to b
1546d 65 63 6f 6d 65 20 73 6d 61 6c 6c 65 72 20 6f 72 ecome smaller or
1546e 20 6c 61 72 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 64 larger..**.** d
1546f 62 48 69 6e 74 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 bHintSize.**.**
15470 20 20 54 68 65 20 64 62 48 69 6e 74 53 69 7a 65 The dbHintSize
15471 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 variable is use
15472 64 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 6e d to limit the n
15473 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 6d umber of calls m
15474 61 64 65 20 74 6f 0a 2a 2a 20 20 20 74 68 65 20 ade to.** the
15475 56 46 53 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c VFS xFileControl
15476 28 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e 54 (FCNTL_SIZE_HINT
15477 29 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a ) method. .**.**
15478 20 20 20 64 62 48 69 6e 74 53 69 7a 65 20 69 73 dbHintSize is
15479 20 73 65 74 20 74 6f 20 61 20 63 6f 70 79 20 6f set to a copy o
1547a 66 20 74 68 65 20 64 62 53 69 7a 65 20 76 61 72 f the dbSize var
1547b 69 61 62 6c 65 20 77 68 65 6e 20 61 0a 2a 2a 20 iable when a.**
1547c 20 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 write-transact
1547d 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 20 28 61 ion is opened (a
1547e 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 t the same time
1547f 61 73 20 64 62 46 69 6c 65 53 69 7a 65 20 61 6e as dbFileSize an
15480 64 0a 2a 2a 20 20 20 64 62 4f 72 69 67 53 69 7a d.** dbOrigSiz
15481 65 29 2e 20 49 66 20 74 68 65 20 78 46 69 6c 65 e). If the xFile
15482 43 6f 6e 74 72 6f 6c 28 46 43 4e 54 4c 5f 53 49 Control(FCNTL_SI
15483 5a 45 5f 48 49 4e 54 29 20 6d 65 74 68 6f 64 20 ZE_HINT) method
15484 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20 20 is called,.**
15485 64 62 48 69 6e 74 53 69 7a 65 20 69 73 20 69 6e dbHintSize is in
15486 63 72 65 61 73 65 64 20 74 6f 20 74 68 65 20 6e creased to the n
15487 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 umber of pages t
15488 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 hat correspond t
15489 6f 20 74 68 65 0a 2a 2a 20 20 20 73 69 7a 65 2d o the.** size-
1548a 68 69 6e 74 20 70 61 73 73 65 64 20 74 6f 20 74 hint passed to t
1548b 68 65 20 6d 65 74 68 6f 64 20 63 61 6c 6c 2e 20 he method call.
1548c 53 65 65 20 70 61 67 65 72 5f 77 72 69 74 65 5f See pager_write_
1548d 70 61 67 65 6c 69 73 74 28 29 20 66 6f 72 20 0a pagelist() for .
1548e 2a 2a 20 20 20 64 65 74 61 69 6c 73 2e 0a 2a 2a ** details..**
1548f 0a 2a 2a 20 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a .** errCode.**.*
15490 2a 20 20 20 54 68 65 20 50 61 67 65 72 2e 65 72 * The Pager.er
15491 72 43 6f 64 65 20 76 61 72 69 61 62 6c 65 20 69 rCode variable i
15492 73 20 6f 6e 6c 79 20 65 76 65 72 20 75 73 65 64 s only ever used
15493 20 69 6e 20 50 41 47 45 52 5f 45 52 52 4f 52 20 in PAGER_ERROR
15494 73 74 61 74 65 2e 20 49 74 0a 2a 2a 20 20 20 69 state. It.** i
15495 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 20 69 6e s set to zero in
15496 20 61 6c 6c 20 6f 74 68 65 72 20 73 74 61 74 65 all other state
15497 73 2e 20 49 6e 20 50 41 47 45 52 5f 45 52 52 4f s. In PAGER_ERRO
15498 52 20 73 74 61 74 65 2c 20 50 61 67 65 72 2e 65 R state, Pager.e
15499 72 72 43 6f 64 65 20 0a 2a 2a 20 20 20 69 73 20 rrCode .** is
1549a 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 53 51 always set to SQ
1549b 4c 49 54 45 5f 46 55 4c 4c 2c 20 53 51 4c 49 54 LITE_FULL, SQLIT
1549c 45 5f 49 4f 45 52 52 20 6f 72 20 6f 6e 65 20 6f E_IOERR or one o
1549d 66 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f 45 f the SQLITE_IOE
1549e 52 52 5f 58 58 58 20 0a 2a 2a 20 20 20 73 75 62 RR_XXX .** sub
1549f 2d 63 6f 64 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 -codes..*/.struc
154a0 74 20 50 61 67 65 72 20 7b 0a 20 20 73 71 6c 69 t Pager {. sqli
154a1 74 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 te3_vfs *pVfs;
154a2 20 20 20 20 20 20 20 20 2f 2a 20 4f 53 20 66 75 /* OS fu
154a3 6e 63 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 66 nctions to use f
154a4 6f 72 20 49 4f 20 2a 2f 0a 20 20 75 38 20 65 78 or IO */. u8 ex
154a5 63 6c 75 73 69 76 65 4d 6f 64 65 3b 20 20 20 20 clusiveMode;
154a6 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 /* Boolea
154a7 6e 2e 20 54 72 75 65 20 69 66 20 6c 6f 63 6b 69 n. True if locki
154a8 6e 67 5f 6d 6f 64 65 3d 3d 45 58 43 4c 55 53 49 ng_mode==EXCLUSI
154a9 56 45 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e VE */. u8 journ
154aa 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 alMode;
154ab 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 /* One of th
154ac 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d e PAGER_JOURNALM
154ad 4f 44 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a ODE_* values */.
154ae 20 20 75 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b u8 useJournal;
154af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
154b0 20 55 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 Use a rollback
154b1 6a 6f 75 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20 journal on this
154b2 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 53 file */. u8 noS
154b3 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 ync;
154b4 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 /* Do not
154b5 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c sync the journal
154b6 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38 if true */. u8
154b7 20 66 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 20 fullSync;
154b8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 /* Do
154b9 65 78 74 72 61 20 73 79 6e 63 73 20 6f 66 20 74 extra syncs of t
154ba 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 he journal for r
154bb 6f 62 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 75 obustness */. u
154bc 38 20 63 6b 70 74 53 79 6e 63 46 6c 61 67 73 3b 8 ckptSyncFlags;
154bd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 59 /* SY
154be 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 53 59 4e NC_NORMAL or SYN
154bf 43 5f 46 55 4c 4c 20 66 6f 72 20 63 68 65 63 6b C_FULL for check
154c0 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20 77 61 point */. u8 wa
154c1 6c 53 79 6e 63 46 6c 61 67 73 3b 20 20 20 20 20 lSyncFlags;
154c2 20 20 20 20 20 20 20 2f 2a 20 53 59 4e 43 5f 4e /* SYNC_N
154c3 4f 52 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46 55 ORMAL or SYNC_FU
154c4 4c 4c 20 66 6f 72 20 77 61 6c 20 77 72 69 74 65 LL for wal write
154c5 73 20 2a 2f 0a 20 20 75 38 20 73 79 6e 63 46 6c s */. u8 syncFl
154c6 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
154c7 20 20 20 2f 2a 20 53 59 4e 43 5f 4e 4f 52 4d 41 /* SYNC_NORMA
154c8 4c 20 6f 72 20 53 59 4e 43 5f 46 55 4c 4c 20 6f L or SYNC_FULL o
154c9 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20 75 38 therwise */. u8
154ca 20 74 65 6d 70 46 69 6c 65 3b 20 20 20 20 20 20 tempFile;
154cb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 /* zFi
154cc 6c 65 6e 61 6d 65 20 69 73 20 61 20 74 65 6d 70 lename is a temp
154cd 6f 72 61 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 orary file */.
154ce 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 u8 readOnly;
154cf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
154d0 72 75 65 20 66 6f 72 20 61 20 72 65 61 64 2d 6f rue for a read-o
154d1 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a nly database */.
154d2 20 20 75 38 20 6d 65 6d 44 62 3b 20 20 20 20 20 u8 memDb;
154d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
154d4 20 54 72 75 65 20 74 6f 20 69 6e 68 69 62 69 74 True to inhibit
154d5 20 61 6c 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f all file I/O */
154d6 0a 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .. /***********
154d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
154d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
154d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
154da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
154db 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ** The followi
154dc 6e 67 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e ng block contain
154dd 73 20 74 68 6f 73 65 20 63 6c 61 73 73 20 6d 65 s those class me
154de 6d 62 65 72 73 20 74 68 61 74 20 63 68 61 6e 67 mbers that chang
154df 65 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 72 6f e during. ** ro
154e0 75 74 69 6e 65 20 6f 70 65 72 74 69 6f 6e 2e 20 utine opertion.
154e1 20 43 6c 61 73 73 20 6d 65 6d 62 65 72 73 20 6e Class members n
154e2 6f 74 20 69 6e 20 74 68 69 73 20 62 6c 6f 63 6b ot in this block
154e3 20 61 72 65 20 65 69 74 68 65 72 20 66 69 78 65 are either fixe
154e4 64 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 d. ** when the
154e5 70 61 67 65 72 20 69 73 20 66 69 72 73 74 20 63 pager is first c
154e6 72 65 61 74 65 64 20 6f 72 20 65 6c 73 65 20 6f reated or else o
154e7 6e 6c 79 20 63 68 61 6e 67 65 20 77 68 65 6e 20 nly change when
154e8 74 68 65 72 65 20 69 73 20 61 0a 20 20 2a 2a 20 there is a. **
154e9 73 69 67 6e 69 66 69 63 61 6e 74 20 6d 6f 64 65 significant mode
154ea 20 63 68 61 6e 67 65 20 28 73 75 63 68 20 61 73 change (such as
154eb 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 70 61 changing the pa
154ec 67 65 5f 73 69 7a 65 2c 20 6c 6f 63 6b 69 6e 67 ge_size, locking
154ed 5f 6d 6f 64 65 2c 0a 20 20 2a 2a 20 6f 72 20 74 _mode,. ** or t
154ee 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 29 he journal_mode)
154ef 2e 20 20 46 72 6f 6d 20 61 6e 6f 74 68 65 72 20 . From another
154f0 76 69 65 77 2c 20 74 68 65 73 65 20 63 6c 61 73 view, these clas
154f1 73 20 6d 65 6d 62 65 72 73 20 64 65 73 63 72 69 s members descri
154f2 62 65 0a 20 20 2a 2a 20 74 68 65 20 22 73 74 61 be. ** the "sta
154f3 74 65 22 20 6f 66 20 74 68 65 20 70 61 67 65 72 te" of the pager
154f4 2c 20 77 68 69 6c 65 20 6f 74 68 65 72 20 63 6c , while other cl
154f5 61 73 73 20 6d 65 6d 62 65 72 73 20 64 65 73 63 ass members desc
154f6 72 69 62 65 20 74 68 65 0a 20 20 2a 2a 20 22 63 ribe the. ** "c
154f7 6f 6e 66 69 67 75 72 61 74 69 6f 6e 22 20 6f 66 onfiguration" of
154f8 20 74 68 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f the pager.. */
154f9 0a 20 20 75 38 20 65 53 74 61 74 65 3b 20 20 20 . u8 eState;
154fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
154fb 2a 20 50 61 67 65 72 20 73 74 61 74 65 20 28 4f * Pager state (O
154fc 50 45 4e 2c 20 52 45 41 44 45 52 2c 20 57 52 49 PEN, READER, WRI
154fd 54 45 52 5f 4c 4f 43 4b 45 44 2e 2e 29 20 2a 2f TER_LOCKED..) */
154fe 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20 20 20 20 . u8 eLock;
154ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15500 2a 20 43 75 72 72 65 6e 74 20 6c 6f 63 6b 20 68 * Current lock h
15501 65 6c 64 20 6f 6e 20 64 61 74 61 62 61 73 65 20 eld on database
15502 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 63 68 61 file */. u8 cha
15503 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20 ngeCountDone;
15504 20 20 20 20 20 20 2f 2a 20 53 65 74 20 61 66 74 /* Set aft
15505 65 72 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 er incrementing
15506 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
15507 65 72 20 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61 er */. u8 setMa
15508 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 ster;
15509 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
1550a 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 73 20 62 65 m-j name has be
1550b 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 72 en written to jr
1550c 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74 nl */. u8 doNot
1550d 53 70 69 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 Spill;
1550e 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 70 /* Do not sp
1550f 69 6c 6c 20 74 68 65 20 63 61 63 68 65 20 77 68 ill the cache wh
15510 65 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 2a 2f 0a 20 en non-zero */.
15511 20 75 38 20 64 6f 4e 6f 74 53 79 6e 63 53 70 69 u8 doNotSyncSpi
15512 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ll; /*
15513 44 6f 20 6e 6f 74 20 64 6f 20 61 20 73 70 69 6c Do not do a spil
15514 6c 20 74 68 61 74 20 72 65 71 75 69 72 65 73 20 l that requires
15515 6a 72 6e 6c 20 73 79 6e 63 20 2a 2f 0a 20 20 75 jrnl sync */. u
15516 38 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b 20 8 subjInMemory;
15517 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
15518 75 65 20 74 6f 20 75 73 65 20 69 6e 2d 6d 65 6d ue to use in-mem
15519 6f 72 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 ory sub-journals
1551a 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 53 69 7a */. Pgno dbSiz
1551b 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1551c 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
1551d 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 ages in the data
1551e 62 61 73 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 base */. Pgno d
1551f 62 4f 72 69 67 53 69 7a 65 3b 20 20 20 20 20 20 bOrigSize;
15520 20 20 20 20 20 20 2f 2a 20 64 62 53 69 7a 65 20 /* dbSize
15521 62 65 66 6f 72 65 20 74 68 65 20 63 75 72 72 65 before the curre
15522 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a nt transaction *
15523 2f 0a 20 20 50 67 6e 6f 20 64 62 46 69 6c 65 53 /. Pgno dbFileS
15524 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
15525 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
15526 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 es in the databa
15527 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e se file */. Pgn
15528 6f 20 64 62 48 69 6e 74 53 69 7a 65 3b 20 20 20 o dbHintSize;
15529 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
1552a 65 20 70 61 73 73 65 64 20 74 6f 20 46 43 4e 54 e passed to FCNT
1552b 4c 5f 53 49 5a 45 5f 48 49 4e 54 20 63 61 6c 6c L_SIZE_HINT call
1552c 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 */. int errCod
1552d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1552e 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 /* One of seve
1552f 72 61 6c 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 ral kinds of err
15530 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 ors */. int nRe
15531 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
15532 20 20 20 20 20 2f 2a 20 50 61 67 65 73 20 6a 6f /* Pages jo
15533 75 72 6e 61 6c 6c 65 64 20 73 69 6e 63 65 20 6c urnalled since l
15534 61 73 74 20 6a 2d 68 65 61 64 65 72 20 77 72 69 ast j-header wri
15535 74 74 65 6e 20 2a 2f 0a 20 20 75 33 32 20 63 6b tten */. u32 ck
15536 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 20 20 sumInit;
15537 20 20 20 20 20 20 2f 2a 20 51 75 61 73 69 2d 72 /* Quasi-r
15538 61 6e 64 6f 6d 20 76 61 6c 75 65 20 61 64 64 65 andom value adde
15539 64 20 74 6f 20 65 76 65 72 79 20 63 68 65 63 6b d to every check
1553a 73 75 6d 20 2a 2f 0a 20 20 75 33 32 20 6e 53 75 sum */. u32 nSu
1553b 62 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 bRec;
1553c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1553d 66 20 72 65 63 6f 72 64 73 20 77 72 69 74 74 65 f records writte
1553e 6e 20 74 6f 20 73 75 62 2d 6a 6f 75 72 6e 61 6c n to sub-journal
1553f 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 */. Bitvec *pI
15540 6e 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 nJournal;
15541 20 20 2f 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 /* One bit for
15542 20 65 61 63 68 20 70 61 67 65 20 69 6e 20 74 68 each page in th
15543 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
15544 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
15545 65 20 2a 66 64 3b 20 20 20 20 20 20 20 20 20 20 e *fd;
15546 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 /* File descrip
15547 74 6f 72 20 66 6f 72 20 64 61 74 61 62 61 73 65 tor for database
15548 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
15549 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 le *jfd;
1554a 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 /* File descri
1554b 70 74 6f 72 20 66 6f 72 20 6d 61 69 6e 20 6a 6f ptor for main jo
1554c 75 72 6e 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 urnal */. sqlit
1554d 65 33 5f 66 69 6c 65 20 2a 73 6a 66 64 3b 20 20 e3_file *sjfd;
1554e 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 /* File d
1554f 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 73 75 escriptor for su
15550 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 b-journal */. i
15551 36 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 3b 20 20 64 journalOff;
15552 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 /* Cu
15553 72 72 65 6e 74 20 77 72 69 74 65 20 6f 66 66 73 rrent write offs
15554 65 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 et in the journa
15555 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 l file */. i64
15556 6a 6f 75 72 6e 61 6c 48 64 72 3b 20 20 20 20 20 journalHdr;
15557 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 /* Byte
15558 6f 66 66 73 65 74 20 74 6f 20 70 72 65 76 69 6f offset to previo
15559 75 73 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 us journal heade
1555a 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 r */. sqlite3_b
1555b 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 3b 20 ackup *pBackup;
1555c 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1555d 20 6c 69 73 74 20 6f 66 20 6f 6e 67 6f 69 6e 67 list of ongoing
1555e 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 65 backup processe
1555f 73 20 2a 2f 0a 20 20 50 61 67 65 72 53 61 76 65 s */. PagerSave
15560 70 6f 69 6e 74 20 2a 61 53 61 76 65 70 6f 69 6e point *aSavepoin
15561 74 3b 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 61 t; /* Array of a
15562 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 ctive savepoints
15563 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 */. int nSavep
15564 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 oint;
15565 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
15566 6c 65 6d 65 6e 74 73 20 69 6e 20 61 53 61 76 65 lements in aSave
15567 70 6f 69 6e 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 point[] */. cha
15568 72 20 64 62 46 69 6c 65 56 65 72 73 5b 31 36 5d r dbFileVers[16]
15569 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e ; /* Chan
1556a 67 65 73 20 77 68 65 6e 65 76 65 72 20 64 61 74 ges whenever dat
1556b 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 abase file chang
1556c 65 73 20 2a 2f 0a 20 20 2f 2a 0a 20 20 2a 2a 20 es */. /*. **
1556d 45 6e 64 20 6f 66 20 74 68 65 20 72 6f 75 74 69 End of the routi
1556e 6e 65 6c 79 2d 63 68 61 6e 67 69 6e 67 20 63 6c nely-changing cl
1556f 61 73 73 20 6d 65 6d 62 65 72 73 0a 20 20 2a 2a ass members. **
15570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15571 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15572 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15573 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15574 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 75 31 *********/.. u1
15575 36 20 6e 45 78 74 72 61 3b 20 20 20 20 20 20 20 6 nExtra;
15576 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 /* Add
15577 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 this many bytes
15578 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f to each in-memo
15579 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 31 36 ry page */. i16
1557a 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20 nReserve;
1557b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1557c 65 72 20 6f 66 20 75 6e 75 73 65 64 20 62 79 74 er of unused byt
1557d 65 73 20 61 74 20 65 6e 64 20 6f 66 20 65 61 63 es at end of eac
1557e 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 32 20 h page */. u32
1557f 76 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20 vfsFlags;
15580 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 /* Flags
15581 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 66 73 for sqlite3_vfs
15582 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 75 33 .xOpen() */. u3
15583 32 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 2 sectorSize;
15584 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 /* Ass
15585 75 6d 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 umed sector size
15586 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b during rollback
15587 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65 53 69 */. int pageSi
15588 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
15589 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1558a 79 74 65 73 20 69 6e 20 61 20 70 61 67 65 20 2a ytes in a page *
1558b 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67 6e 6f 3b /. Pgno mxPgno;
1558c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1558d 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 /* Maximum allow
1558e 65 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 ed size of the d
1558f 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 36 34 atabase */. i64
15590 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 journalSizeLimi
15591 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 t; /* Size
15592 20 6c 69 6d 69 74 20 66 6f 72 20 70 65 72 73 69 limit for persi
15593 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 stent journal fi
15594 6c 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a les */. char *z
15595 46 69 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20 20 Filename;
15596 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
15597 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
15598 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4a 6f e */. char *zJo
15599 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 urnal;
1559a 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
1559b 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a e journal file *
1559c 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48 /. int (*xBusyH
1559d 61 6e 64 6c 65 72 29 28 76 6f 69 64 2a 29 3b 20 andler)(void*);
1559e 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63 /* Function to c
1559f 61 6c 6c 20 77 68 65 6e 20 62 75 73 79 20 2a 2f all when busy */
155a0 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61 . void *pBusyHa
155a1 6e 64 6c 65 72 41 72 67 3b 20 20 20 20 20 20 2f ndlerArg; /
155a2 2a 20 43 6f 6e 74 65 78 74 20 61 72 67 75 6d 65 * Context argume
155a3 6e 74 20 66 6f 72 20 78 42 75 73 79 48 61 6e 64 nt for xBusyHand
155a4 6c 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 53 74 ler */. int aSt
155a5 61 74 5b 33 5d 3b 20 20 20 20 20 20 20 20 20 20 at[3];
155a6 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 63 61 /* Total ca
155a7 63 68 65 20 68 69 74 73 2c 20 6d 69 73 73 65 73 che hits, misses
155a8 20 61 6e 64 20 77 72 69 74 65 73 20 2a 2f 0a 23 and writes */.#
155a9 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
155aa 54 0a 20 20 69 6e 74 20 6e 52 65 61 64 3b 20 20 T. int nRead;
155ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
155ac 2f 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 65 /* Database page
155ad 73 20 72 65 61 64 20 2a 2f 0a 23 65 6e 64 69 66 s read */.#endif
155ae 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 . void (*xReini
155af 74 65 72 29 28 44 62 50 61 67 65 2a 29 3b 20 2f ter)(DbPage*); /
155b0 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 * Call this rout
155b1 69 6e 65 20 77 68 65 6e 20 72 65 6c 6f 61 64 69 ine when reloadi
155b2 6e 67 20 70 61 67 65 73 20 2a 2f 0a 23 69 66 64 ng pages */.#ifd
155b3 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f ef SQLITE_HAS_CO
155b4 44 45 43 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 DEC. void *(*xC
155b5 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 odec)(void*,void
155b6 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20 2f 2a 20 *,Pgno,int); /*
155b7 52 6f 75 74 69 6e 65 20 66 6f 72 20 65 6e 2f 64 Routine for en/d
155b8 65 63 6f 64 69 6e 67 20 64 61 74 61 20 2a 2f 0a ecoding data */.
155b9 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 53 void (*xCodecS
155ba 69 7a 65 43 68 6e 67 29 28 76 6f 69 64 2a 2c 69 izeChng)(void*,i
155bb 6e 74 2c 69 6e 74 29 3b 20 2f 2a 20 4e 6f 74 69 nt,int); /* Noti
155bc 66 79 20 6f 66 20 70 61 67 65 20 73 69 7a 65 20 fy of page size
155bd 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 76 6f 69 changes */. voi
155be 64 20 28 2a 78 43 6f 64 65 63 46 72 65 65 29 28 d (*xCodecFree)(
155bf 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20 void*);
155c0 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f /* Destructo
155c1 72 20 66 6f 72 20 74 68 65 20 63 6f 64 65 63 20 r for the codec
155c2 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 */. void *pCode
155c3 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
155c4 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 /* First argume
155c5 6e 74 20 74 6f 20 78 43 6f 64 65 63 2e 2e 2e 20 nt to xCodec...
155c6 6d 65 74 68 6f 64 73 20 2a 2f 0a 23 65 6e 64 69 methods */.#endi
155c7 66 0a 20 20 63 68 61 72 20 2a 70 54 6d 70 53 70 f. char *pTmpSp
155c8 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ace;
155c9 2f 2a 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a /* Pager.pageSiz
155ca 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 e bytes of space
155cb 20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a for tmp use */.
155cc 20 20 50 43 61 63 68 65 20 2a 70 50 43 61 63 68 PCache *pPCach
155cd 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
155ce 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 Pointer to page
155cf 20 63 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f cache object */
155d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
155d1 4f 4d 49 54 5f 57 41 4c 0a 20 20 57 61 6c 20 2a OMIT_WAL. Wal *
155d2 70 57 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 pWal;
155d3 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 2d /* Write-
155d4 61 68 65 61 64 20 6c 6f 67 20 75 73 65 64 20 62 ahead log used b
155d5 79 20 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d y "journal_mode=
155d6 77 61 6c 22 20 2a 2f 0a 20 20 63 68 61 72 20 2a wal" */. char *
155d7 7a 57 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 zWal;
155d8 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 6e 61 /* File na
155d9 6d 65 20 66 6f 72 20 77 72 69 74 65 2d 61 68 65 me for write-ahe
155da 61 64 20 6c 6f 67 20 2a 2f 0a 23 65 6e 64 69 66 ad log */.#endif
155db 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 65 78 .};../*.** Index
155dc 65 73 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 es for use with
155dd 50 61 67 65 72 2e 61 53 74 61 74 5b 5d 2e 20 54 Pager.aStat[]. T
155de 68 65 20 50 61 67 65 72 2e 61 53 74 61 74 5b 5d he Pager.aStat[]
155df 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 0a array contains.
155e0 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 61 63 ** the values ac
155e1 63 65 73 73 65 64 20 62 79 20 70 61 73 73 69 6e cessed by passin
155e2 67 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 g SQLITE_DBSTATU
155e3 53 5f 43 41 43 48 45 5f 48 49 54 2c 20 43 41 43 S_CACHE_HIT, CAC
155e4 48 45 5f 4d 49 53 53 20 0a 2a 2a 20 6f 72 20 43 HE_MISS .** or C
155e5 41 43 48 45 5f 57 52 49 54 45 20 74 6f 20 73 71 ACHE_WRITE to sq
155e6 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 lite3_db_status(
155e7 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 )..*/.#define PA
155e8 47 45 52 5f 53 54 41 54 5f 48 49 54 20 20 20 30 GER_STAT_HIT 0
155e9 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 .#define PAGER_S
155ea 54 41 54 5f 4d 49 53 53 20 20 31 0a 23 64 65 66 TAT_MISS 1.#def
155eb 69 6e 65 20 50 41 47 45 52 5f 53 54 41 54 5f 57 ine PAGER_STAT_W
155ec 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 54 68 RITE 2../*.** Th
155ed 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 e following glob
155ee 61 6c 20 76 61 72 69 61 62 6c 65 73 20 68 6f 6c al variables hol
155ef 64 20 63 6f 75 6e 74 65 72 73 20 75 73 65 64 20 d counters used
155f0 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 70 for.** testing p
155f1 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 54 urposes only. T
155f2 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 64 hese variables d
155f3 6f 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 0a 2a o not exist in.*
155f4 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 69 6e 67 20 * a non-testing
155f5 62 75 69 6c 64 2e 20 20 54 68 65 73 65 20 76 61 build. These va
155f6 72 69 61 62 6c 65 73 20 61 72 65 20 6e 6f 74 20 riables are not
155f7 74 68 72 65 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a thread-safe..*/.
155f8 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
155f9 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
155fa 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f t sqlite3_pager_
155fb 72 65 61 64 64 62 5f 63 6f 75 6e 74 20 3d 20 30 readdb_count = 0
155fc 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f ; /* Number o
155fd 66 20 66 75 6c 6c 20 70 61 67 65 73 20 72 65 61 f full pages rea
155fe 64 20 66 72 6f 6d 20 44 42 20 2a 2f 0a 53 51 4c d from DB */.SQL
155ff 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
15600 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 te3_pager_writed
15601 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f b_count = 0; /
15602 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c * Number of full
15603 20 70 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 pages written t
15604 6f 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 o DB */.SQLITE_A
15605 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 PI int sqlite3_p
15606 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e ager_writej_coun
15607 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d t = 0; /* Num
15608 62 65 72 20 6f 66 20 70 61 67 65 73 20 77 72 69 ber of pages wri
15609 74 74 65 6e 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 tten to journal
1560a 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45 */.# define PAGE
1560b 52 5f 49 4e 43 52 28 76 29 20 20 76 2b 2b 0a 23 R_INCR(v) v++.#
1560c 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 50 41 else.# define PA
1560d 47 45 52 5f 49 4e 43 52 28 76 29 0a 23 65 6e 64 GER_INCR(v).#end
1560e 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 if..../*.** Jour
1560f 6e 61 6c 20 66 69 6c 65 73 20 62 65 67 69 6e 20 nal files begin
15610 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 with the followi
15611 6e 67 20 6d 61 67 69 63 20 73 74 72 69 6e 67 2e ng magic string.
15612 20 20 54 68 65 20 64 61 74 61 0a 2a 2a 20 77 61 The data.** wa
15613 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
15614 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e 20 20 49 74 /dev/random. It
15615 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 61 73 is used only as
15616 20 61 20 73 61 6e 69 74 79 20 63 68 65 63 6b 2e a sanity check.
15617 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72 .**.** Since ver
15618 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 68 65 20 sion 2.8.0, the
15619 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 journal format c
1561a 6f 6e 74 61 69 6e 73 20 61 64 64 69 74 69 6f 6e ontains addition
1561b 61 6c 20 73 61 6e 69 74 79 0a 2a 2a 20 63 68 65 al sanity.** che
1561c 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f cking informatio
1561d 6e 2e 20 20 49 66 20 74 68 65 20 70 6f 77 65 72 n. If the power
1561e 20 66 61 69 6c 73 20 77 68 69 6c 65 20 74 68 65 fails while the
1561f 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e journal is bein
15620 67 0a 2a 2a 20 77 72 69 74 74 65 6e 2c 20 73 65 g.** written, se
15621 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72 62 61 67 mi-random garbag
15622 65 20 64 61 74 61 20 6d 69 67 68 74 20 61 70 70 e data might app
15623 65 61 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e ear in the journ
15624 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 66 74 65 72 al.** file after
15625 20 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72 power is restor
15626 65 64 2e 20 20 49 66 20 61 6e 20 61 74 74 65 6d ed. If an attem
15627 70 74 20 69 73 20 74 68 65 6e 20 6d 61 64 65 0a pt is then made.
15628 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 68 65 20 6a ** to roll the j
15629 6f 75 72 6e 61 6c 20 62 61 63 6b 2c 20 74 68 65 ournal back, the
1562a 20 64 61 74 61 62 61 73 65 20 63 6f 75 6c 64 20 database could
1562b 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20 20 54 be corrupted. T
1562c 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a he additional.**
1562d 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 sanity checking
1562e 20 64 61 74 61 20 69 73 20 61 6e 20 61 74 74 65 data is an atte
1562f 6d 70 74 20 74 6f 20 64 69 73 63 6f 76 65 72 20 mpt to discover
15630 74 68 65 20 67 61 72 62 61 67 65 20 69 6e 20 74 the garbage in t
15631 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e he.** journal an
15632 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 2a 2a 0a d ignore it..**.
15633 2a 2a 20 54 68 65 20 73 61 6e 69 74 79 20 63 68 ** The sanity ch
15634 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 ecking informati
15635 6f 6e 20 66 6f 72 20 74 68 65 20 6e 65 77 20 6a on for the new j
15636 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f ournal format co
15637 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 20 61 20 33 nsists.** of a 3
15638 32 2d 62 69 74 20 63 68 65 63 6b 73 75 6d 20 6f 2-bit checksum o
15639 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 64 n each page of d
1563a 61 74 61 2e 20 20 54 68 65 20 63 68 65 63 6b 73 ata. The checks
1563b 75 6d 20 63 6f 76 65 72 73 20 62 6f 74 68 0a 2a um covers both.*
1563c 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 * the page numbe
1563d 72 20 61 6e 64 20 74 68 65 20 70 50 61 67 65 72 r and the pPager
1563e 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 ->pageSize bytes
1563f 20 6f 66 20 64 61 74 61 20 66 6f 72 20 74 68 65 of data for the
15640 20 70 61 67 65 2e 0a 2a 2a 20 54 68 69 73 20 63 page..** This c
15641 6b 73 75 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 ksum is initiali
15642 7a 65 64 20 74 6f 20 61 20 33 32 2d 62 69 74 20 zed to a 32-bit
15643 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 74 68 61 random value tha
15644 74 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 65 t appears in the
15645 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
15646 20 72 69 67 68 74 20 61 66 74 65 72 20 74 68 65 right after the
15647 20 68 65 61 64 65 72 2e 20 20 54 68 65 20 72 61 header. The ra
15648 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 69 7a 65 72 ndom initializer
15649 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2c 0a 2a is important,.*
1564a 2a 20 62 65 63 61 75 73 65 20 67 61 72 62 61 67 * because garbag
1564b 65 20 64 61 74 61 20 74 68 61 74 20 61 70 70 65 e data that appe
1564c 61 72 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f ars at the end o
1564d 66 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c f a journal is l
1564e 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 61 20 74 68 ikely.** data th
1564f 61 74 20 77 61 73 20 6f 6e 63 65 20 69 6e 20 6f at was once in o
15650 74 68 65 72 20 66 69 6c 65 73 20 74 68 61 74 20 ther files that
15651 68 61 76 65 20 6e 6f 77 20 62 65 65 6e 20 64 65 have now been de
15652 6c 65 74 65 64 2e 20 20 49 66 20 74 68 65 0a 2a leted. If the.*
15653 2a 20 67 61 72 62 61 67 65 20 64 61 74 61 20 63 * garbage data c
15654 61 6d 65 20 66 72 6f 6d 20 61 6e 20 6f 62 73 6f ame from an obso
15655 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c lete journal fil
15656 65 2c 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 e, the checksums
15657 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 63 6f 72 might.** be cor
15658 72 65 63 74 2e 20 20 42 75 74 20 62 79 20 69 6e rect. But by in
15659 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 63 itializing the c
1565a 68 65 63 6b 73 75 6d 20 74 6f 20 72 61 6e 64 6f hecksum to rando
1565b 6d 20 76 61 6c 75 65 20 77 68 69 63 68 0a 2a 2a m value which.**
1565c 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 6f is different fo
1565d 72 20 65 76 65 72 79 20 6a 6f 75 72 6e 61 6c 2c r every journal,
1565e 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20 74 68 61 we minimize tha
1565f 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 t risk..*/.stati
15660 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 c const unsigned
15661 20 63 68 61 72 20 61 4a 6f 75 72 6e 61 6c 4d 61 char aJournalMa
15662 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 30 78 64 39 gic[] = {. 0xd9
15663 2c 20 30 78 64 35 2c 20 30 78 30 35 2c 20 30 78 , 0xd5, 0x05, 0x
15664 66 39 2c 20 30 78 32 30 2c 20 30 78 61 31 2c 20 f9, 0x20, 0xa1,
15665 30 78 36 33 2c 20 30 78 64 37 2c 0a 7d 3b 0a 0a 0x63, 0xd7,.};..
15666 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f /*.** The size o
15667 66 20 74 68 65 20 6f 66 20 65 61 63 68 20 70 61 f the of each pa
15668 67 65 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 ge record in the
15669 20 6a 6f 75 72 6e 61 6c 20 69 73 20 67 69 76 65 journal is give
1566a 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c n by.** the foll
1566b 6f 77 69 6e 67 20 6d 61 63 72 6f 2e 0a 2a 2f 0a owing macro..*/.
1566c 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f #define JOURNAL_
1566d 50 47 5f 53 5a 28 70 50 61 67 65 72 29 20 20 28 PG_SZ(pPager) (
1566e 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a (pPager->pageSiz
1566f 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 e) + 8)../*.** T
15670 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
15671 72 20 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20 r size for this
15672 70 61 67 65 72 2e 20 54 68 69 73 20 69 73 20 75 pager. This is u
15673 73 75 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 20 sually the same
15674 0a 2a 2a 20 73 69 7a 65 20 61 73 20 61 20 73 69 .** size as a si
15675 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 ngle disk sector
15676 2e 20 53 65 65 20 61 6c 73 6f 20 73 65 74 53 65 . See also setSe
15677 63 74 6f 72 53 69 7a 65 28 29 2e 0a 2a 2f 0a 23 ctorSize()..*/.#
15678 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 48 define JOURNAL_H
15679 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 28 70 DR_SZ(pPager) (p
1567a 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a Pager->sectorSiz
1567b 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 e)../*.** The ma
1567c 63 72 6f 20 4d 45 4d 44 42 20 69 73 20 74 72 75 cro MEMDB is tru
1567d 65 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c e if we are deal
1567e 69 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 2d 6d ing with an in-m
1567f 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e 0a emory database..
15680 2a 2a 20 57 65 20 64 6f 20 74 68 69 73 20 61 73 ** We do this as
15681 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 a macro so that
15682 20 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4f if the SQLITE_O
15683 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 6d 61 63 MIT_MEMORYDB mac
15684 72 6f 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 ro is set,.** th
15685 65 20 76 61 6c 75 65 20 6f 66 20 4d 45 4d 44 42 e value of MEMDB
15686 20 77 69 6c 6c 20 62 65 20 61 20 63 6f 6e 73 74 will be a const
15687 61 6e 74 20 61 6e 64 20 74 68 65 20 63 6f 6d 70 ant and the comp
15688 69 6c 65 72 20 77 69 6c 6c 20 6f 70 74 69 6d 69 iler will optimi
15689 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 65 20 74 ze.** out code t
1568a 68 61 74 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 hat would never
1568b 65 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 69 66 64 execute..*/.#ifd
1568c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d ef SQLITE_OMIT_M
1568d 45 4d 4f 52 59 44 42 0a 23 20 64 65 66 69 6e 65 EMORYDB.# define
1568e 20 4d 45 4d 44 42 20 30 0a 23 65 6c 73 65 0a 23 MEMDB 0.#else.#
1568f 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 70 50 define MEMDB pP
15690 61 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 65 6e 64 ager->memDb.#end
15691 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 if../*.** The ma
15692 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61 67 65 ximum legal page
15693 20 6e 75 6d 62 65 72 20 69 73 20 28 32 5e 33 31 number is (2^31
15694 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e - 1)..*/.#defin
15695 65 20 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f e PAGER_MAX_PGNO
15696 20 32 31 34 37 34 38 33 36 34 37 0a 0a 2f 2a 0a 2147483647../*.
15697 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 ** The argument
15698 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73 to this macro is
15699 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 a file descript
1569a 6f 72 20 28 74 79 70 65 20 73 71 6c 69 74 65 33 or (type sqlite3
1569b 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 52 65 74 75 _file*)..** Retu
1569c 72 6e 20 30 20 69 66 20 69 74 20 69 73 20 6e 6f rn 0 if it is no
1569d 74 20 6f 70 65 6e 2c 20 6f 72 20 6e 6f 6e 2d 7a t open, or non-z
1569e 65 72 6f 20 28 62 75 74 20 6e 6f 74 20 31 29 20 ero (but not 1)
1569f 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 if it is..**.**
156a0 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 20 This is so that
156a1 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e 20 expressions can
156a2 62 65 20 77 72 69 74 74 65 6e 20 61 73 3a 0a 2a be written as:.*
156a3 2a 0a 2a 2a 20 20 20 69 66 28 20 69 73 4f 70 65 *.** if( isOpe
156a4 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 n(pPager->jfd) )
156a5 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 6e 73 74 { ....**.** inst
156a6 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 69 ead of.**.** i
156a7 66 28 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d 3e f( pPager->jfd->
156a8 70 4d 65 74 68 6f 64 73 20 29 7b 20 2e 2e 2e 0a pMethods ){ ....
156a9 2a 2f 0a 23 64 65 66 69 6e 65 20 69 73 4f 70 65 */.#define isOpe
156aa 6e 28 70 46 64 29 20 28 28 70 46 64 29 2d 3e 70 n(pFd) ((pFd)->p
156ab 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a 0a 2a 2a 20 Methods)../*.**
156ac 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 Return true if t
156ad 68 69 73 20 70 61 67 65 72 20 75 73 65 73 20 61 his pager uses a
156ae 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 write-ahead log
156af 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 instead of the
156b0 75 73 75 61 6c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 usual.** rollbac
156b1 6b 20 6a 6f 75 72 6e 61 6c 2e 20 4f 74 68 65 72 k journal. Other
156b2 77 69 73 65 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23 wise false..*/.#
156b3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
156b4 49 54 5f 57 41 4c 0a 73 74 61 74 69 63 20 69 6e IT_WAL.static in
156b5 74 20 70 61 67 65 72 55 73 65 57 61 6c 28 50 61 t pagerUseWal(Pa
156b6 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
156b7 72 65 74 75 72 6e 20 28 70 50 61 67 65 72 2d 3e return (pPager->
156b8 70 57 61 6c 21 3d 30 29 3b 0a 7d 0a 23 65 6c 73 pWal!=0);.}.#els
156b9 65 0a 23 20 64 65 66 69 6e 65 20 70 61 67 65 72 e.# define pager
156ba 55 73 65 57 61 6c 28 78 29 20 30 0a 23 20 64 65 UseWal(x) 0.# de
156bb 66 69 6e 65 20 70 61 67 65 72 52 6f 6c 6c 62 61 fine pagerRollba
156bc 63 6b 57 61 6c 28 78 29 20 30 0a 23 20 64 65 66 ckWal(x) 0.# def
156bd 69 6e 65 20 70 61 67 65 72 57 61 6c 46 72 61 6d ine pagerWalFram
156be 65 73 28 76 2c 77 2c 78 2c 79 29 20 30 0a 23 20 es(v,w,x,y) 0.#
156bf 64 65 66 69 6e 65 20 70 61 67 65 72 4f 70 65 6e define pagerOpen
156c0 57 61 6c 49 66 50 72 65 73 65 6e 74 28 7a 29 20 WalIfPresent(z)
156c1 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 65 66 69 SQLITE_OK.# defi
156c2 6e 65 20 70 61 67 65 72 42 65 67 69 6e 52 65 61 ne pagerBeginRea
156c3 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 7a 29 20 dTransaction(z)
156c4 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 SQLITE_OK.#endif
156c5 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
156c6 20 0a 2f 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a ./*.** Usage:.*
156c7 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 61 *.** assert( a
156c8 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 ssert_pager_stat
156c9 65 28 70 50 61 67 65 72 29 20 29 3b 0a 2a 2a 0a e(pPager) );.**.
156ca 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
156cb 20 72 75 6e 73 20 6d 61 6e 79 20 61 73 73 65 72 runs many asser
156cc 74 73 20 74 6f 20 74 72 79 20 74 6f 20 66 69 6e ts to try to fin
156cd 64 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63 69 65 d inconsistencie
156ce 73 20 69 6e 0a 2a 2a 20 74 68 65 20 69 6e 74 65 s in.** the inte
156cf 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 74 68 rnal state of th
156d0 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 0a e Pager object..
156d1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 73 */.static int as
156d2 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 sert_pager_state
156d3 28 50 61 67 65 72 20 2a 70 29 7b 0a 20 20 50 61 (Pager *p){. Pa
156d4 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 3b ger *pPager = p;
156d5 0a 0a 20 20 2f 2a 20 53 74 61 74 65 20 6d 75 73 .. /* State mus
156d6 74 20 62 65 20 76 61 6c 69 64 2e 20 2a 2f 0a 20 t be valid. */.
156d7 20 61 73 73 65 72 74 28 20 70 2d 3e 65 53 74 61 assert( p->eSta
156d8 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 0a 20 te==PAGER_OPEN.
156d9 20 20 20 20 20 20 7c 7c 20 70 2d 3e 65 53 74 61 || p->eSta
156da 74 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 45 52 te==PAGER_READER
156db 0a 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 65 53 . || p->eS
156dc 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 tate==PAGER_WRIT
156dd 45 52 5f 4c 4f 43 4b 45 44 0a 20 20 20 20 20 20 ER_LOCKED.
156de 20 7c 7c 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50 || p->eState==P
156df 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 AGER_WRITER_CACH
156e0 45 4d 4f 44 0a 20 20 20 20 20 20 20 7c 7c 20 70 EMOD. || p
156e1 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f ->eState==PAGER_
156e2 57 52 49 54 45 52 5f 44 42 4d 4f 44 0a 20 20 20 WRITER_DBMOD.
156e3 20 20 20 20 7c 7c 20 70 2d 3e 65 53 74 61 74 65 || p->eState
156e4 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 46 ==PAGER_WRITER_F
156e5 49 4e 49 53 48 45 44 0a 20 20 20 20 20 20 20 7c INISHED. |
156e6 7c 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 | p->eState==PAG
156e7 45 52 5f 45 52 52 4f 52 0a 20 20 29 3b 0a 0a 20 ER_ERROR. );..
156e8 20 2f 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f /* Regardless o
156e9 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 f the current st
156ea 61 74 65 2c 20 61 20 74 65 6d 70 2d 66 69 6c 65 ate, a temp-file
156eb 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c 77 61 connection alwa
156ec 79 73 20 62 65 68 61 76 65 73 0a 20 20 2a 2a 20 ys behaves. **
156ed 61 73 20 69 66 20 69 74 20 68 61 73 20 61 6e 20 as if it has an
156ee 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f exclusive lock o
156ef 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
156f0 69 6c 65 2e 20 49 74 20 6e 65 76 65 72 20 75 70 ile. It never up
156f1 64 61 74 65 73 0a 20 20 2a 2a 20 74 68 65 20 63 dates. ** the c
156f2 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 66 69 hange-counter fi
156f3 65 6c 64 2c 20 73 6f 20 74 68 65 20 63 68 61 6e eld, so the chan
156f4 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 geCountDone flag
156f5 20 69 73 20 61 6c 77 61 79 73 20 73 65 74 2e 0a is always set..
156f6 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
156f7 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c ->tempFile==0 ||
156f8 20 70 2d 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 p->eLock==EXCLU
156f9 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 SIVE_LOCK );. a
156fa 73 73 65 72 74 28 20 70 2d 3e 74 65 6d 70 46 69 ssert( p->tempFi
156fb 6c 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d le==0 || pPager-
156fc 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 >changeCountDone
156fd 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 );.. /* If the
156fe 20 75 73 65 4a 6f 75 72 6e 61 6c 20 66 6c 61 67 useJournal flag
156ff 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 20 6a is clear, the j
15700 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 6d 75 73 74 ournal-mode must
15701 20 62 65 20 22 4f 46 46 22 2e 20 0a 20 20 2a 2a be "OFF". . **
15702 20 41 6e 64 20 69 66 20 74 68 65 20 6a 6f 75 72 And if the jour
15703 6e 61 6c 2d 6d 6f 64 65 20 69 73 20 22 4f 46 46 nal-mode is "OFF
15704 22 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ", the journal f
15705 69 6c 65 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 ile must not be
15706 6f 70 65 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 open.. */. ass
15707 65 72 74 28 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d ert( p->journalM
15708 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
15709 41 4c 4d 4f 44 45 5f 4f 46 46 20 7c 7c 20 70 2d ALMODE_OFF || p-
1570a 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 >useJournal );.
1570b 20 61 73 73 65 72 74 28 20 70 2d 3e 6a 6f 75 72 assert( p->jour
1570c 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a nalMode!=PAGER_J
1570d 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 7c OURNALMODE_OFF |
1570e 7c 20 21 69 73 4f 70 65 6e 28 70 2d 3e 6a 66 64 | !isOpen(p->jfd
1570f 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ) );.. /* Check
15710 20 74 68 61 74 20 4d 45 4d 44 42 20 69 6d 70 6c that MEMDB impl
15711 69 65 73 20 6e 6f 53 79 6e 63 2e 20 41 6e 64 20 ies noSync. And
15712 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 an in-memory jou
15713 72 6e 61 6c 2e 20 53 69 6e 63 65 20 0a 20 20 2a rnal. Since . *
15714 2a 20 74 68 69 73 20 6d 65 61 6e 73 20 61 6e 20 * this means an
15715 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 20 in-memory pager
15716 70 65 72 66 6f 72 6d 73 20 6e 6f 20 49 4f 20 61 performs no IO a
15717 74 20 61 6c 6c 2c 20 69 74 20 63 61 6e 6e 6f 74 t all, it cannot
15718 20 65 6e 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2a encounter . **
15719 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 49 either SQLITE_I
1571a 4f 45 52 52 20 6f 72 20 53 51 4c 49 54 45 5f 46 OERR or SQLITE_F
1571b 55 4c 4c 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 ULL during rollb
1571c 61 63 6b 20 6f 72 20 77 68 69 6c 65 20 66 69 6e ack or while fin
1571d 61 6c 69 7a 69 6e 67 20 0a 20 20 2a 2a 20 61 20 alizing . ** a
1571e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 28 61 journal file. (a
1571f 6c 74 68 6f 75 67 68 20 74 68 65 20 69 6e 2d 6d lthough the in-m
15720 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 69 6d emory journal im
15721 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 61 79 plementation may
15722 20 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51 . ** return SQ
15723 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d LITE_IOERR_NOMEM
15724 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e while the journ
15725 61 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e 67 al file is being
15726 20 77 72 69 74 74 65 6e 29 2e 20 49 74 20 0a 20 written). It .
15727 20 2a 2a 20 69 73 20 74 68 65 72 65 66 6f 72 65 ** is therefore
15728 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f not possible fo
15729 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 r an in-memory p
1572a 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68 ager to enter th
1572b 65 20 45 52 52 4f 52 20 0a 20 20 2a 2a 20 73 74 e ERROR . ** st
1572c 61 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ate.. */. if(
1572d 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 61 73 73 MEMDB ){. ass
1572e 65 72 74 28 20 70 2d 3e 6e 6f 53 79 6e 63 20 29 ert( p->noSync )
1572f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d ;. assert( p-
15730 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
15731 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
15732 4f 46 46 20 0a 20 20 20 20 20 20 20 20 20 7c 7c OFF . ||
15733 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d p->journalMode=
15734 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
15735 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 29 DE_MEMORY . )
15736 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d ;. assert( p-
15737 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 5f 45 >eState!=PAGER_E
15738 52 52 4f 52 20 26 26 20 70 2d 3e 65 53 74 61 74 RROR && p->eStat
15739 65 21 3d 50 41 47 45 52 5f 4f 50 45 4e 20 29 3b e!=PAGER_OPEN );
1573a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67 . assert( pag
1573b 65 72 55 73 65 57 61 6c 28 70 29 3d 3d 30 20 29 erUseWal(p)==0 )
1573c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63 ;. }.. /* If c
1573d 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 69 hangeCountDone i
1573e 73 20 73 65 74 2c 20 61 20 52 45 53 45 52 56 45 s set, a RESERVE
1573f 44 20 6c 6f 63 6b 20 6f 72 20 67 72 65 61 74 65 D lock or greate
15740 72 20 6d 75 73 74 20 62 65 20 68 65 6c 64 0a 20 r must be held.
15741 20 2a 2a 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e ** on the file.
15742 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
15743 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
15744 75 6e 74 44 6f 6e 65 3d 3d 30 20 7c 7c 20 70 50 untDone==0 || pP
15745 61 67 65 72 2d 3e 65 4c 6f 63 6b 3e 3d 52 45 53 ager->eLock>=RES
15746 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 ERVED_LOCK );.
15747 61 73 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b assert( p->eLock
15748 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 !=PENDING_LOCK )
15749 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e ;.. switch( p->
1574a 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 63 61 eState ){. ca
1574b 73 65 20 50 41 47 45 52 5f 4f 50 45 4e 3a 0a 20 se PAGER_OPEN:.
1574c 20 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 assert( !ME
1574d 4d 44 42 20 29 3b 0a 20 20 20 20 20 20 61 73 73 MDB );. ass
1574e 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72 72 ert( pPager->err
1574f 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 Code==SQLITE_OK
15750 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
15751 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
15752 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 fCount(pPager->p
15753 50 43 61 63 68 65 29 3d 3d 30 20 7c 7c 20 70 50 PCache)==0 || pP
15754 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 ager->tempFile )
15755 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a ;. break;..
15756 20 20 20 20 63 61 73 65 20 50 41 47 45 52 5f 52 case PAGER_R
15757 45 41 44 45 52 3a 0a 20 20 20 20 20 20 61 73 73 EADER:. ass
15758 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72 72 ert( pPager->err
15759 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 Code==SQLITE_OK
1575a 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1575b 20 70 2d 3e 65 4c 6f 63 6b 21 3d 55 4e 4b 4e 4f p->eLock!=UNKNO
1575c 57 4e 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 WN_LOCK );.
1575d 20 61 73 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63 assert( p->eLoc
1575e 6b 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 k>=SHARED_LOCK )
1575f 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a ;. break;..
15760 20 20 20 20 63 61 73 65 20 50 41 47 45 52 5f 57 case PAGER_W
15761 52 49 54 45 52 5f 4c 4f 43 4b 45 44 3a 0a 20 20 RITER_LOCKED:.
15762 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 assert( p->e
15763 4c 6f 63 6b 21 3d 55 4e 4b 4e 4f 57 4e 5f 4c 4f Lock!=UNKNOWN_LO
15764 43 4b 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 CK );. asse
15765 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 rt( pPager->errC
15766 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 ode==SQLITE_OK )
15767 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 61 67 ;. if( !pag
15768 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 erUseWal(pPager)
15769 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
1576a 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 3e 3d 52 45 rt( p->eLock>=RE
1576b 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SERVED_LOCK );.
1576c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 }. ass
1576d 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 ert( pPager->dbS
1576e 69 7a 65 3d 3d 70 50 61 67 65 72 2d 3e 64 62 4f ize==pPager->dbO
1576f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 rigSize );.
15770 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
15771 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d 70 50 61 >dbOrigSize==pPa
15772 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 ger->dbFileSize
15773 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
15774 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 pPager->dbOrigS
15775 69 7a 65 3d 3d 70 50 61 67 65 72 2d 3e 64 62 48 ize==pPager->dbH
15776 69 6e 74 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 intSize );.
15777 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
15778 3e 73 65 74 4d 61 73 74 65 72 3d 3d 30 20 29 3b >setMaster==0 );
15779 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 . break;..
1577a 20 20 20 63 61 73 65 20 50 41 47 45 52 5f 57 52 case PAGER_WR
1577b 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 3a 0a 20 ITER_CACHEMOD:.
1577c 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
1577d 65 4c 6f 63 6b 21 3d 55 4e 4b 4e 4f 57 4e 5f 4c eLock!=UNKNOWN_L
1577e 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 61 73 73 OCK );. ass
1577f 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72 72 ert( pPager->err
15780 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 Code==SQLITE_OK
15781 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 61 );. if( !pa
15782 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 gerUseWal(pPager
15783 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ) ){. /*
15784 49 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 It is possible t
15785 68 61 74 20 69 66 20 6a 6f 75 72 6e 61 6c 5f 6d hat if journal_m
15786 6f 64 65 3d 77 61 6c 20 68 65 72 65 20 74 68 61 ode=wal here tha
15787 74 20 6e 65 69 74 68 65 72 20 74 68 65 0a 20 20 t neither the.
15788 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c ** journal
15789 20 66 69 6c 65 20 6e 6f 72 20 74 68 65 20 57 41 file nor the WA
1578a 4c 20 66 69 6c 65 20 61 72 65 20 6f 70 65 6e 2e L file are open.
1578b 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 64 75 This happens du
1578c 72 69 6e 67 0a 20 20 20 20 20 20 20 20 2a 2a 20 ring. **
1578d 61 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 a rollback trans
1578e 61 63 74 69 6f 6e 20 74 68 61 74 20 73 77 69 74 action that swit
1578f 63 68 65 73 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 ches from journa
15790 6c 5f 6d 6f 64 65 3d 6f 66 66 0a 20 20 20 20 20 l_mode=off.
15791 20 20 20 2a 2a 20 74 6f 20 6a 6f 75 72 6e 61 6c ** to journal
15792 5f 6d 6f 64 65 3d 77 61 6c 2e 0a 20 20 20 20 20 _mode=wal..
15793 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 */. as
15794 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 3e 3d sert( p->eLock>=
15795 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b RESERVED_LOCK );
15796 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
15797 20 69 73 4f 70 65 6e 28 70 2d 3e 6a 66 64 29 20 isOpen(p->jfd)
15798 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c . ||
15799 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d p->journalMode=
1579a 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
1579b 44 45 5f 4f 46 46 20 0a 20 20 20 20 20 20 20 20 DE_OFF .
1579c 20 20 20 20 20 7c 7c 20 70 2d 3e 6a 6f 75 72 6e || p->journ
1579d 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
1579e 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 0a 20 URNALMODE_WAL .
1579f 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 );.
157a0 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
157a1 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 pPager->dbOrigSi
157a2 7a 65 3d 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 ze==pPager->dbFi
157a3 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 leSize );.
157a4 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
157a5 64 62 4f 72 69 67 53 69 7a 65 3d 3d 70 50 61 67 dbOrigSize==pPag
157a6 65 72 2d 3e 64 62 48 69 6e 74 53 69 7a 65 20 29 er->dbHintSize )
157a7 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a ;. break;..
157a8 20 20 20 20 63 61 73 65 20 50 41 47 45 52 5f 57 case PAGER_W
157a9 52 49 54 45 52 5f 44 42 4d 4f 44 3a 0a 20 20 20 RITER_DBMOD:.
157aa 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 4c assert( p->eL
157ab 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ock==EXCLUSIVE_L
157ac 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 61 73 73 OCK );. ass
157ad 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72 72 ert( pPager->err
157ae 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 Code==SQLITE_OK
157af 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
157b0 20 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 !pagerUseWal(pP
157b1 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 61 ager) );. a
157b2 73 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 3e ssert( p->eLock>
157b3 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
157b4 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
157b5 20 69 73 4f 70 65 6e 28 70 2d 3e 6a 66 64 29 20 isOpen(p->jfd)
157b6 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 . || p
157b7 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
157b8 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
157b9 5f 4f 46 46 20 0a 20 20 20 20 20 20 20 20 20 20 _OFF .
157ba 20 7c 7c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f || p->journalMo
157bb 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
157bc 4c 4d 4f 44 45 5f 57 41 4c 20 0a 20 20 20 20 20 LMODE_WAL .
157bd 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
157be 28 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 ( pPager->dbOrig
157bf 53 69 7a 65 3c 3d 70 50 61 67 65 72 2d 3e 64 62 Size<=pPager->db
157c0 48 69 6e 74 53 69 7a 65 20 29 3b 0a 20 20 20 20 HintSize );.
157c1 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 63 61 break;.. ca
157c2 73 65 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f se PAGER_WRITER_
157c3 46 49 4e 49 53 48 45 44 3a 0a 20 20 20 20 20 20 FINISHED:.
157c4 61 73 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b assert( p->eLock
157c5 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
157c6 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
157c7 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
157c8 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a e==SQLITE_OK );.
157c9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 assert( !p
157ca 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 agerUseWal(pPage
157cb 72 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 r) );. asse
157cc 72 74 28 20 69 73 4f 70 65 6e 28 70 2d 3e 6a 66 rt( isOpen(p->jf
157cd 64 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 7c d) . |
157ce 7c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 | p->journalMode
157cf 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
157d0 4f 44 45 5f 4f 46 46 20 0a 20 20 20 20 20 20 20 ODE_OFF .
157d1 20 20 20 20 7c 7c 20 70 2d 3e 6a 6f 75 72 6e 61 || p->journa
157d2 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
157d3 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 0a 20 20 RNALMODE_WAL .
157d4 20 20 20 20 29 3b 0a 20 20 20 20 20 20 62 72 65 );. bre
157d5 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 20 50 41 ak;.. case PA
157d6 47 45 52 5f 45 52 52 4f 52 3a 0a 20 20 20 20 20 GER_ERROR:.
157d7 20 2f 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 /* There must b
157d8 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f e at least one o
157d9 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 utstanding refer
157da 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67 65 ence to the page
157db 72 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 69 6e r if. ** in
157dc 20 45 52 52 4f 52 20 73 74 61 74 65 2e 20 4f 74 ERROR state. Ot
157dd 68 65 72 77 69 73 65 20 74 68 65 20 70 61 67 65 herwise the page
157de 72 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c r should have al
157df 72 65 61 64 79 20 64 72 6f 70 70 65 64 0a 20 20 ready dropped.
157e0 20 20 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 4f ** back to O
157e1 50 45 4e 20 73 74 61 74 65 2e 0a 20 20 20 20 20 PEN state..
157e2 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 */. assert
157e3 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
157e4 65 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a e!=SQLITE_OK );.
157e5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 assert( sq
157e6 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
157e7 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
157e8 63 68 65 29 3e 30 20 29 3b 0a 20 20 20 20 20 20 che)>0 );.
157e9 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 break;. }.. re
157ea 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 turn 1;.}.#endif
157eb 20 2f 2a 20 69 66 6e 64 65 66 20 4e 44 45 42 55 /* ifndef NDEBU
157ec 47 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c G */..#ifdef SQL
157ed 49 54 45 5f 44 45 42 55 47 20 0a 2f 2a 0a 2a 2a ITE_DEBUG ./*.**
157ee 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
157ef 72 20 74 6f 20 61 20 68 75 6d 61 6e 20 72 65 61 r to a human rea
157f0 64 61 62 6c 65 20 73 74 72 69 6e 67 20 69 6e 20 dable string in
157f1 61 20 73 74 61 74 69 63 20 62 75 66 66 65 72 0a a static buffer.
157f2 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ** containing th
157f3 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 50 e state of the P
157f4 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61 73 73 ager object pass
157f5 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e ed as an argumen
157f6 74 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 69 6e t. This.** is in
157f7 74 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65 tended to be use
157f8 64 20 77 69 74 68 69 6e 20 64 65 62 75 67 67 65 d within debugge
157f9 72 73 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c rs. For example,
157fa 20 61 73 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 as an alternati
157fb 76 65 0a 2a 2a 20 74 6f 20 22 70 72 69 6e 74 20 ve.** to "print
157fc 2a 70 50 61 67 65 72 22 20 69 6e 20 67 64 62 3a *pPager" in gdb:
157fd 0a 2a 2a 0a 2a 2a 20 28 67 64 62 29 20 70 72 69 .**.** (gdb) pri
157fe 6e 74 66 20 22 25 73 22 2c 20 70 72 69 6e 74 5f ntf "%s", print_
157ff 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 pager_state(pPag
15800 65 72 29 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 er).*/.static ch
15801 61 72 20 2a 70 72 69 6e 74 5f 70 61 67 65 72 5f ar *print_pager_
15802 73 74 61 74 65 28 50 61 67 65 72 20 2a 70 29 7b state(Pager *p){
15803 0a 20 20 73 74 61 74 69 63 20 63 68 61 72 20 7a . static char z
15804 52 65 74 5b 31 30 32 34 5d 3b 0a 0a 20 20 73 71 Ret[1024];.. sq
15805 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 lite3_snprintf(1
15806 30 32 34 2c 20 7a 52 65 74 2c 0a 20 20 20 20 20 024, zRet,.
15807 20 22 46 69 6c 65 6e 61 6d 65 3a 20 20 20 20 20 "Filename:
15808 20 25 73 5c 6e 22 0a 20 20 20 20 20 20 22 53 74 %s\n". "St
15809 61 74 65 3a 20 20 20 20 20 20 20 20 20 25 73 20 ate: %s
1580a 65 72 72 43 6f 64 65 3d 25 64 5c 6e 22 0a 20 20 errCode=%d\n".
1580b 20 20 20 20 22 4c 6f 63 6b 3a 20 20 20 20 20 20 "Lock:
1580c 20 20 20 20 25 73 5c 6e 22 0a 20 20 20 20 20 20 %s\n".
1580d 22 4c 6f 63 6b 69 6e 67 20 6d 6f 64 65 3a 20 20 "Locking mode:
1580e 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 25 73 5c locking_mode=%s\
1580f 6e 22 0a 20 20 20 20 20 20 22 4a 6f 75 72 6e 61 n". "Journa
15810 6c 20 6d 6f 64 65 3a 20 20 6a 6f 75 72 6e 61 6c l mode: journal
15811 5f 6d 6f 64 65 3d 25 73 5c 6e 22 0a 20 20 20 20 _mode=%s\n".
15812 20 20 22 42 61 63 6b 69 6e 67 20 73 74 6f 72 65 "Backing store
15813 3a 20 74 65 6d 70 46 69 6c 65 3d 25 64 20 6d 65 : tempFile=%d me
15814 6d 44 62 3d 25 64 20 75 73 65 4a 6f 75 72 6e 61 mDb=%d useJourna
15815 6c 3d 25 64 5c 6e 22 0a 20 20 20 20 20 20 22 4a l=%d\n". "J
15816 6f 75 72 6e 61 6c 3a 20 20 20 20 20 20 20 6a 6f ournal: jo
15817 75 72 6e 61 6c 4f 66 66 3d 25 6c 6c 64 20 6a 6f urnalOff=%lld jo
15818 75 72 6e 61 6c 48 64 72 3d 25 6c 6c 64 5c 6e 22 urnalHdr=%lld\n"
15819 0a 20 20 20 20 20 20 22 53 69 7a 65 3a 20 20 20 . "Size:
1581a 20 20 20 20 20 20 20 64 62 73 69 7a 65 3d 25 64 dbsize=%d
1581b 20 64 62 4f 72 69 67 53 69 7a 65 3d 25 64 20 64 dbOrigSize=%d d
1581c 62 46 69 6c 65 53 69 7a 65 3d 25 64 5c 6e 22 0a bFileSize=%d\n".
1581d 20 20 20 20 20 20 2c 20 70 2d 3e 7a 46 69 6c 65 , p->zFile
1581e 6e 61 6d 65 0a 20 20 20 20 20 20 2c 20 70 2d 3e name. , p->
1581f 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 eState==PAGER_OP
15820 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 3f 20 EN ?
15821 22 4f 50 45 4e 22 20 3a 0a 20 20 20 20 20 20 20 "OPEN" :.
15822 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 p->eState==PAGE
15823 52 5f 52 45 41 44 45 52 20 20 20 20 20 20 20 20 R_READER
15824 20 20 3f 20 22 52 45 41 44 45 52 22 20 3a 0a 20 ? "READER" :.
15825 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65 p->eState
15826 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c ==PAGER_WRITER_L
15827 4f 43 4b 45 44 20 20 20 3f 20 22 57 52 49 54 45 OCKED ? "WRITE
15828 52 5f 4c 4f 43 4b 45 44 22 20 3a 0a 20 20 20 20 R_LOCKED" :.
15829 20 20 20 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50 p->eState==P
1582a 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 AGER_WRITER_CACH
1582b 45 4d 4f 44 20 3f 20 22 57 52 49 54 45 52 5f 43 EMOD ? "WRITER_C
1582c 41 43 48 45 4d 4f 44 22 20 3a 0a 20 20 20 20 20 ACHEMOD" :.
1582d 20 20 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50 41 p->eState==PA
1582e 47 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 GER_WRITER_DBMOD
1582f 20 20 20 20 3f 20 22 57 52 49 54 45 52 5f 44 42 ? "WRITER_DB
15830 4d 4f 44 22 20 3a 0a 20 20 20 20 20 20 20 20 70 MOD" :. p
15831 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f ->eState==PAGER_
15832 57 52 49 54 45 52 5f 46 49 4e 49 53 48 45 44 20 WRITER_FINISHED
15833 3f 20 22 57 52 49 54 45 52 5f 46 49 4e 49 53 48 ? "WRITER_FINISH
15834 45 44 22 20 3a 0a 20 20 20 20 20 20 20 20 70 2d ED" :. p-
15835 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 45 >eState==PAGER_E
15836 52 52 4f 52 20 20 20 20 20 20 20 20 20 20 20 3f RROR ?
15837 20 22 45 52 52 4f 52 22 20 3a 20 22 3f 65 72 72 "ERROR" : "?err
15838 6f 72 3f 22 0a 20 20 20 20 20 20 2c 20 28 69 6e or?". , (in
15839 74 29 70 2d 3e 65 72 72 43 6f 64 65 0a 20 20 20 t)p->errCode.
1583a 20 20 20 2c 20 70 2d 3e 65 4c 6f 63 6b 3d 3d 4e , p->eLock==N
1583b 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 3f O_LOCK ?
1583c 20 22 4e 4f 5f 4c 4f 43 4b 22 20 3a 0a 20 20 20 "NO_LOCK" :.
1583d 20 20 20 20 20 70 2d 3e 65 4c 6f 63 6b 3d 3d 52 p->eLock==R
1583e 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 3f ESERVED_LOCK ?
1583f 20 22 52 45 53 45 52 56 45 44 22 20 3a 0a 20 20 "RESERVED" :.
15840 20 20 20 20 20 20 70 2d 3e 65 4c 6f 63 6b 3d 3d p->eLock==
15841 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 EXCLUSIVE_LOCK
15842 3f 20 22 45 58 43 4c 55 53 49 56 45 22 20 3a 0a ? "EXCLUSIVE" :.
15843 20 20 20 20 20 20 20 20 70 2d 3e 65 4c 6f 63 6b p->eLock
15844 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 ==SHARED_LOCK
15845 20 20 3f 20 22 53 48 41 52 45 44 22 20 3a 0a 20 ? "SHARED" :.
15846 20 20 20 20 20 20 20 70 2d 3e 65 4c 6f 63 6b 3d p->eLock=
15847 3d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 20 20 =UNKNOWN_LOCK
15848 20 3f 20 22 55 4e 4b 4e 4f 57 4e 22 20 3a 20 22 ? "UNKNOWN" : "
15849 3f 65 72 72 6f 72 3f 22 0a 20 20 20 20 20 20 2c ?error?". ,
1584a 20 70 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 p->exclusiveMod
1584b 65 20 3f 20 22 65 78 63 6c 75 73 69 76 65 22 20 e ? "exclusive"
1584c 3a 20 22 6e 6f 72 6d 61 6c 22 0a 20 20 20 20 20 : "normal".
1584d 20 2c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 , p->journalMod
1584e 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
1584f 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 3f 20 MODE_MEMORY ?
15850 22 6d 65 6d 6f 72 79 22 20 3a 0a 20 20 20 20 20 "memory" :.
15851 20 20 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 p->journalMod
15852 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
15853 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 3f 20 MODE_OFF ?
15854 22 6f 66 66 22 20 3a 0a 20 20 20 20 20 20 20 20 "off" :.
15855 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d p->journalMode==
15856 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
15857 45 5f 44 45 4c 45 54 45 20 20 20 3f 20 22 64 65 E_DELETE ? "de
15858 6c 65 74 65 22 20 3a 0a 20 20 20 20 20 20 20 20 lete" :.
15859 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d p->journalMode==
1585a 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
1585b 45 5f 50 45 52 53 49 53 54 20 20 3f 20 22 70 65 E_PERSIST ? "pe
1585c 72 73 69 73 74 22 20 3a 0a 20 20 20 20 20 20 20 rsist" :.
1585d 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d p->journalMode=
1585e 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
1585f 44 45 5f 54 52 55 4e 43 41 54 45 20 3f 20 22 74 DE_TRUNCATE ? "t
15860 72 75 6e 63 61 74 65 22 20 3a 0a 20 20 20 20 20 runcate" :.
15861 20 20 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 p->journalMod
15862 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
15863 4d 4f 44 45 5f 57 41 4c 20 20 20 20 20 20 3f 20 MODE_WAL ?
15864 22 77 61 6c 22 20 3a 20 22 3f 65 72 72 6f 72 3f "wal" : "?error?
15865 22 0a 20 20 20 20 20 20 2c 20 28 69 6e 74 29 70 ". , (int)p
15866 2d 3e 74 65 6d 70 46 69 6c 65 2c 20 28 69 6e 74 ->tempFile, (int
15867 29 70 2d 3e 6d 65 6d 44 62 2c 20 28 69 6e 74 29 )p->memDb, (int)
15868 70 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 0a 20 20 p->useJournal.
15869 20 20 20 20 2c 20 70 2d 3e 6a 6f 75 72 6e 61 6c , p->journal
1586a 4f 66 66 2c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 48 Off, p->journalH
1586b 64 72 0a 20 20 20 20 20 20 2c 20 28 69 6e 74 29 dr. , (int)
1586c 70 2d 3e 64 62 53 69 7a 65 2c 20 28 69 6e 74 29 p->dbSize, (int)
1586d 70 2d 3e 64 62 4f 72 69 67 53 69 7a 65 2c 20 28 p->dbOrigSize, (
1586e 69 6e 74 29 70 2d 3e 64 62 46 69 6c 65 53 69 7a int)p->dbFileSiz
1586f 65 0a 20 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e e. );.. return
15870 20 7a 52 65 74 3b 0a 7d 0a 23 65 6e 64 69 66 0a zRet;.}.#endif.
15871 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 ./*.** Return tr
15872 75 65 20 69 66 20 69 74 20 69 73 20 6e 65 63 65 ue if it is nece
15873 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20 70 ssary to write p
15874 61 67 65 20 2a 70 50 67 20 69 6e 74 6f 20 74 68 age *pPg into th
15875 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 2a e sub-journal..*
15876 2a 20 41 20 70 61 67 65 20 6e 65 65 64 73 20 74 * A page needs t
15877 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 o be written int
15878 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 o the sub-journa
15879 6c 20 69 66 20 74 68 65 72 65 20 65 78 69 73 74 l if there exist
1587a 73 20 6f 6e 65 0a 2a 2a 20 6f 72 20 6d 6f 72 65 s one.** or more
1587b 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 open savepoints
1587c 20 66 6f 72 20 77 68 69 63 68 3a 0a 2a 2a 0a 2a for which:.**.*
1587d 2a 20 20 20 2a 20 54 68 65 20 70 61 67 65 2d 6e * * The page-n
1587e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20 74 68 umber is less th
1587f 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 50 an or equal to P
15880 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 6e 4f agerSavepoint.nO
15881 72 69 67 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 rig, and.** *
15882 54 68 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f The bit correspo
15883 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70 61 67 nding to the pag
15884 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 e-number is not
15885 73 65 74 20 69 6e 0a 2a 2a 20 20 20 20 20 50 61 set in.** Pa
15886 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e gerSavepoint.pIn
15887 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 Savepoint..*/.st
15888 61 74 69 63 20 69 6e 74 20 73 75 62 6a 52 65 71 atic int subjReq
15889 75 69 72 65 73 50 61 67 65 28 50 67 48 64 72 20 uiresPage(PgHdr
1588a 2a 70 50 67 29 7b 0a 20 20 50 67 6e 6f 20 70 67 *pPg){. Pgno pg
1588b 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a no = pPg->pgno;.
1588c 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 Pager *pPager
1588d 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 = pPg->pPager;.
1588e 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
1588f 30 3b 20 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 0; i<pPager->nSa
15890 76 65 70 6f 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 vepoint; i++){.
15891 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e PagerSavepoin
15892 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72 2d 3e t *p = &pPager->
15893 61 53 61 76 65 70 6f 69 6e 74 5b 69 5d 3b 0a 20 aSavepoint[i];.
15894 20 20 20 69 66 28 20 70 2d 3e 6e 4f 72 69 67 3e if( p->nOrig>
15895 3d 70 67 6e 6f 20 26 26 20 30 3d 3d 73 71 6c 69 =pgno && 0==sqli
15896 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 2d te3BitvecTest(p-
15897 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 70 >pInSavepoint, p
15898 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 72 65 gno) ){. re
15899 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
1589a 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
1589b 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 ./*.** Return tr
1589c 75 65 20 69 66 20 74 68 65 20 70 61 67 65 20 69 ue if the page i
1589d 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 s already in the
1589e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a journal file..*
1589f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
158a0 65 49 6e 4a 6f 75 72 6e 61 6c 28 50 67 48 64 72 eInJournal(PgHdr
158a1 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e *pPg){. return
158a2 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 sqlite3BitvecTe
158a3 73 74 28 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e st(pPg->pPager->
158a4 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d pInJournal, pPg-
158a5 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a >pgno);.}../*.**
158a6 20 52 65 61 64 20 61 20 33 32 2d 62 69 74 20 69 Read a 32-bit i
158a7 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 68 65 20 nteger from the
158a8 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63 72 given file descr
158a9 69 70 74 6f 72 2e 20 20 53 74 6f 72 65 20 74 68 iptor. Store th
158aa 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 68 61 e integer.** tha
158ab 74 20 69 73 20 72 65 61 64 20 69 6e 20 2a 70 52 t is read in *pR
158ac 65 73 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 es. Return SQLI
158ad 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 TE_OK if everyth
158ae 69 6e 67 20 77 6f 72 6b 65 64 2c 20 6f 72 20 61 ing worked, or a
158af 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 n.** error code
158b0 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 is something goe
158b1 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 s wrong..**.** A
158b2 6c 6c 20 76 61 6c 75 65 73 20 61 72 65 20 73 74 ll values are st
158b3 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20 61 73 20 ored on disk as
158b4 62 69 67 2d 65 6e 64 69 61 6e 2e 0a 2a 2f 0a 73 big-endian..*/.s
158b5 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 33 32 tatic int read32
158b6 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c bits(sqlite3_fil
158b7 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 e *fd, i64 offse
158b8 74 2c 20 75 33 32 20 2a 70 52 65 73 29 7b 0a 20 t, u32 *pRes){.
158b9 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 unsigned char a
158ba 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 72 63 20 3d c[4];. int rc =
158bb 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 66 sqlite3OsRead(f
158bc 64 2c 20 61 63 2c 20 73 69 7a 65 6f 66 28 61 63 d, ac, sizeof(ac
158bd 29 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 69 66 ), offset);. if
158be 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
158bf 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 73 ){. *pRes = s
158c0 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 61 qlite3Get4byte(a
158c1 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e c);. }. return
158c2 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 rc;.}../*.** Wr
158c3 69 74 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 ite a 32-bit int
158c4 65 67 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69 eger into a stri
158c5 6e 67 20 62 75 66 66 65 72 20 69 6e 20 62 69 67 ng buffer in big
158c6 2d 65 6e 64 69 61 6e 20 62 79 74 65 20 6f 72 64 -endian byte ord
158c7 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70 er..*/.#define p
158c8 75 74 33 32 62 69 74 73 28 41 2c 42 29 20 20 73 ut32bits(A,B) s
158c9 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 28 qlite3Put4byte((
158ca 75 38 2a 29 41 2c 42 29 0a 0a 0a 2f 2a 0a 2a 2a u8*)A,B).../*.**
158cb 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20 Write a 32-bit
158cc 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 74 68 65 integer into the
158cd 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63 given file desc
158ce 72 69 70 74 6f 72 2e 20 20 52 65 74 75 72 6e 20 riptor. Return
158cf 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 SQLITE_OK.** on
158d0 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 success or an er
158d1 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 ror code is some
158d2 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
158d3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
158d4 77 72 69 74 65 33 32 62 69 74 73 28 73 71 6c 69 write32bits(sqli
158d5 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36 te3_file *fd, i6
158d6 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 76 61 4 offset, u32 va
158d7 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 5b 34 5d l){. char ac[4]
158d8 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 61 63 ;. put32bits(ac
158d9 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e , val);. return
158da 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
158db 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 66 73 65 fd, ac, 4, offse
158dc 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c t);.}../*.** Unl
158dd 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 ock the database
158de 20 66 69 6c 65 20 74 6f 20 6c 65 76 65 6c 20 65 file to level e
158df 4c 6f 63 6b 2c 20 77 68 69 63 68 20 6d 75 73 74 Lock, which must
158e0 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f be either NO_LO
158e1 43 4b 0a 2a 2a 20 6f 72 20 53 48 41 52 45 44 5f CK.** or SHARED_
158e2 4c 4f 43 4b 2e 20 52 65 67 61 72 64 6c 65 73 73 LOCK. Regardless
158e3 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e of whether or n
158e4 6f 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 78 ot the call to x
158e5 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 73 75 63 63 Unlock().** succ
158e6 65 65 64 73 2c 20 73 65 74 20 74 68 65 20 50 61 eeds, set the Pa
158e7 67 65 72 2e 65 4c 6f 63 6b 20 76 61 72 69 61 62 ger.eLock variab
158e8 6c 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 le to match the
158e9 28 61 74 74 65 6d 70 74 65 64 29 20 6e 65 77 20 (attempted) new
158ea 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65 lock..**.** Exce
158eb 70 74 2c 20 69 66 20 50 61 67 65 72 2e 65 4c 6f pt, if Pager.eLo
158ec 63 6b 20 69 73 20 73 65 74 20 74 6f 20 55 4e 4b ck is set to UNK
158ed 4e 4f 57 4e 5f 4c 4f 43 4b 20 77 68 65 6e 20 74 NOWN_LOCK when t
158ee 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a his function is.
158ef 2a 2a 20 63 61 6c 6c 65 64 2c 20 64 6f 20 6e 6f ** called, do no
158f0 74 20 6d 6f 64 69 66 79 20 69 74 2e 20 53 65 65 t modify it. See
158f1 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f the comment abo
158f2 76 65 20 74 68 65 20 23 64 65 66 69 6e 65 20 6f ve the #define o
158f3 66 20 0a 2a 2a 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f f .** UNKNOWN_LO
158f4 43 4b 20 66 6f 72 20 61 6e 20 65 78 70 6c 61 6e CK for an explan
158f5 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 2e 0a 2a ation of this..*
158f6 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
158f7 65 72 55 6e 6c 6f 63 6b 44 62 28 50 61 67 65 72 erUnlockDb(Pager
158f8 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4c *pPager, int eL
158f9 6f 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ock){. int rc =
158fa 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 SQLITE_OK;.. a
158fb 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e ssert( !pPager->
158fc 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c exclusiveMode ||
158fd 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d pPager->eLock==
158fe 65 4c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 eLock );. asser
158ff 74 28 20 65 4c 6f 63 6b 3d 3d 4e 4f 5f 4c 4f 43 t( eLock==NO_LOC
15900 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 53 48 41 52 K || eLock==SHAR
15901 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 ED_LOCK );. ass
15902 65 72 74 28 20 65 4c 6f 63 6b 21 3d 4e 4f 5f 4c ert( eLock!=NO_L
15903 4f 43 4b 20 7c 7c 20 70 61 67 65 72 55 73 65 57 OCK || pagerUseW
15904 61 6c 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b al(pPager)==0 );
15905 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 . if( isOpen(pP
15906 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 ager->fd) ){.
15907 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
15908 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b 20 29 3b >eLock>=eLock );
15909 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1590a 33 4f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 3OsUnlock(pPager
1590b 2d 3e 66 64 2c 20 65 4c 6f 63 6b 29 3b 0a 20 20 ->fd, eLock);.
1590c 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 4c if( pPager->eL
1590d 6f 63 6b 21 3d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 ock!=UNKNOWN_LOC
1590e 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 K ){. pPage
1590f 72 2d 3e 65 4c 6f 63 6b 20 3d 20 28 75 38 29 65 r->eLock = (u8)e
15910 4c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Lock;. }.
15911 49 4f 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b IOTRACE(("UNLOCK
15912 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 %p %d\n", pPage
15913 72 2c 20 65 4c 6f 63 6b 29 29 0a 20 20 7d 0a 20 r, eLock)). }.
15914 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
15915 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 64 61 *.** Lock the da
15916 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6c tabase file to l
15917 65 76 65 6c 20 65 4c 6f 63 6b 2c 20 77 68 69 63 evel eLock, whic
15918 68 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 h must be either
15919 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 0a 2a 2a SHARED_LOCK,.**
1591a 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 6f RESERVED_LOCK o
1591b 72 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b r EXCLUSIVE_LOCK
1591c 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 . If the caller
1591d 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 is successful, s
1591e 65 74 20 74 68 65 0a 2a 2a 20 50 61 67 65 72 2e et the.** Pager.
1591f 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 20 74 eLock variable t
15920 6f 20 74 68 65 20 6e 65 77 20 6c 6f 63 6b 69 6e o the new lockin
15921 67 20 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 g state. .**.**
15922 45 78 63 65 70 74 2c 20 69 66 20 50 61 67 65 72 Except, if Pager
15923 2e 65 4c 6f 63 6b 20 69 73 20 73 65 74 20 74 6f .eLock is set to
15924 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 77 68 UNKNOWN_LOCK wh
15925 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
15926 20 69 73 20 0a 2a 2a 20 63 61 6c 6c 65 64 2c 20 is .** called,
15927 64 6f 20 6e 6f 74 20 6d 6f 64 69 66 79 20 69 74 do not modify it
15928 20 75 6e 6c 65 73 73 20 74 68 65 20 6e 65 77 20 unless the new
15929 6c 6f 63 6b 69 6e 67 20 73 74 61 74 65 20 69 73 locking state is
1592a 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 2e EXCLUSIVE_LOCK.
1592b 20 0a 2a 2a 20 53 65 65 20 74 68 65 20 63 6f 6d .** See the com
1592c 6d 65 6e 74 20 61 62 6f 76 65 20 74 68 65 20 23 ment above the #
1592d 64 65 66 69 6e 65 20 6f 66 20 55 4e 4b 4e 4f 57 define of UNKNOW
1592e 4e 5f 4c 4f 43 4b 20 66 6f 72 20 61 6e 20 65 78 N_LOCK for an ex
1592f 70 6c 61 6e 61 74 69 6f 6e 20 0a 2a 2a 20 6f 66 planation .** of
15930 20 74 68 69 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 this..*/.static
15931 20 69 6e 74 20 70 61 67 65 72 4c 6f 63 6b 44 62 int pagerLockDb
15932 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
15933 69 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 6e int eLock){. in
15934 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
15935 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f ;.. assert( eLo
15936 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ck==SHARED_LOCK
15937 7c 7c 20 65 4c 6f 63 6b 3d 3d 52 45 53 45 52 56 || eLock==RESERV
15938 45 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b ED_LOCK || eLock
15939 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
1593a 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 );. if( pPager
1593b 2d 3e 65 4c 6f 63 6b 3c 65 4c 6f 63 6b 20 7c 7c ->eLock<eLock ||
1593c 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d pPager->eLock==
1593d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 29 7b 0a UNKNOWN_LOCK ){.
1593e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1593f 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 OsLock(pPager->f
15940 64 2c 20 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 69 d, eLock);. i
15941 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
15942 20 26 26 20 28 70 50 61 67 65 72 2d 3e 65 4c 6f && (pPager->eLo
15943 63 6b 21 3d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b ck!=UNKNOWN_LOCK
15944 7c 7c 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 ||eLock==EXCLUSI
15945 56 45 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 VE_LOCK) ){.
15946 20 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 20 pPager->eLock
15947 3d 20 28 75 38 29 65 4c 6f 63 6b 3b 0a 20 20 20 = (u8)eLock;.
15948 20 20 20 49 4f 54 52 41 43 45 28 28 22 4c 4f 43 IOTRACE(("LOC
15949 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 K %p %d\n", pPag
1594a 65 72 2c 20 65 4c 6f 63 6b 29 29 0a 20 20 20 20 er, eLock)).
1594b 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
1594c 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
1594d 20 66 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d function determ
1594e 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 ines whether or
1594f 6e 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 not the atomic-w
15950 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f rite optimizatio
15951 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 n.** can be used
15952 20 77 69 74 68 20 74 68 69 73 20 70 61 67 65 72 with this pager
15953 2e 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 . The optimizati
15954 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 on can be used i
15955 66 3a 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 f:.**.** (a) th
15956 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
15957 20 62 79 20 4f 73 44 65 76 69 63 65 43 68 61 72 by OsDeviceChar
15958 61 63 74 65 72 69 73 74 69 63 73 28 29 20 69 6e acteristics() in
15959 64 69 63 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 dicates that.**
1595a 20 20 20 20 20 61 20 64 61 74 61 62 61 73 65 20 a database
1595b 70 61 67 65 20 6d 61 79 20 62 65 20 77 72 69 74 page may be writ
1595c 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 ten atomically,
1595d 61 6e 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 and.** (b) the
1595e 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 value returned b
1595f 79 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 y OsSectorSize()
15960 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 is less than or
15961 20 65 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 equal.** t
15962 6f 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e o the page size.
15963 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d .**.** The optim
15964 69 7a 61 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 ization is also
15965 61 6c 77 61 79 73 20 65 6e 61 62 6c 65 64 20 66 always enabled f
15966 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c or temporary fil
15967 65 73 2e 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20 es. It is.** an
15968 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 error to call th
15969 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 70 is function if p
1596a 50 61 67 65 72 20 69 73 20 6f 70 65 6e 65 64 20 Pager is opened
1596b 6f 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a on an in-memory.
1596c 2a 2a 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a ** database..**.
1596d 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 ** If the optimi
1596e 7a 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 zation cannot be
1596f 20 75 73 65 64 2c 20 30 20 69 73 20 72 65 74 75 used, 0 is retu
15970 72 6e 65 64 2e 20 49 66 20 69 74 20 63 61 6e 20 rned. If it can
15971 62 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e be used,.** then
15972 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
15973 6e 65 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 ned is the size
15974 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
15975 69 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63 ile when it.** c
15976 6f 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b ontains rollback
15977 20 64 61 74 61 20 66 6f 72 20 65 78 61 63 74 6c data for exactl
15978 79 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 y one page..*/.#
15979 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
1597a 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 BLE_ATOMIC_WRITE
1597b 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c .static int jrnl
1597c 42 75 66 66 65 72 53 69 7a 65 28 50 61 67 65 72 BufferSize(Pager
1597d 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73 *pPager){. ass
1597e 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 ert( !MEMDB );.
1597f 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 if( !pPager->te
15980 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e mpFile ){. in
15981 74 20 64 63 3b 20 20 20 20 20 20 20 20 20 20 20 t dc;
15982 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15983 2f 2a 20 44 65 76 69 63 65 20 63 68 61 72 61 63 /* Device charac
15984 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 20 teristics */.
15985 20 69 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 int nSector;
15986 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15987 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a /* Sector siz
15988 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 e */. int szP
15989 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
1598a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
1598b 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 ge size */..
1598c 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
1598d 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 Pager->fd) );.
1598e 20 20 64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 dc = sqlite3Os
1598f 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
15990 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 stics(pPager->fd
15991 29 3b 0a 20 20 20 20 6e 53 65 63 74 6f 72 20 3d );. nSector =
15992 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 pPager->sectorS
15993 69 7a 65 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 ize;. szPage
15994 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 = pPager->pageSi
15995 7a 65 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 ze;.. assert(
15996 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
15997 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 MIC512==(512>>8)
15998 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 );. assert(SQ
15999 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
1599a 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 C64K==(65536>>8)
1599b 29 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 64 );. if( 0==(d
1599c 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f c&(SQLITE_IOCAP_
1599d 41 54 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e ATOMIC|(szPage>>
1599e 38 29 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 8)) || nSector>s
1599f 7a 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 zPage) ){.
159a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a return 0;. }.
159a1 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f }.. return JO
159a2 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
159a3 67 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 ger) + JOURNAL_P
159a4 47 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a G_SZ(pPager);.}.
159a5 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 #endif../*.** If
159a6 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 SQLITE_CHECK_PA
159a7 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 74 GES is defined t
159a8 68 65 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 hen we do some s
159a9 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a anity checking.*
159aa 2a 20 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75 * on the cache u
159ab 73 69 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63 sing a hash func
159ac 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 tion. This is u
159ad 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a sed for testing.
159ae 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 ** and debugging
159af 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 only..*/.#ifdef
159b0 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 SQLITE_CHECK_PA
159b1 47 45 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e GES./*.** Return
159b2 20 61 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f a 32-bit hash o
159b3 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 f the page data
159b4 66 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 for pPage..*/.st
159b5 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 64 atic u32 pager_d
159b6 61 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74 atahash(int nByt
159b7 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 e, unsigned char
159b8 20 2a 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20 *pData){. u32
159b9 68 61 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 hash = 0;. int
159ba 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c i;. for(i=0; i<
159bb 6e 42 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 nByte; i++){.
159bc 20 68 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30 hash = (hash*10
159bd 33 39 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 39) + pData[i];.
159be 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73 }. return has
159bf 68 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20 h;.}.static u32
159c0 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 50 pager_pagehash(P
159c1 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 gHdr *pPage){.
159c2 72 65 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74 return pager_dat
159c3 61 68 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61 ahash(pPage->pPa
159c4 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 ger->pageSize, (
159c5 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
159c6 70 50 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d pPage->pData);.}
159c7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 .static void pag
159c8 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28 er_set_pagehash(
159c9 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 PgHdr *pPage){.
159ca 20 70 50 61 67 65 2d 3e 70 61 67 65 48 61 73 68 pPage->pageHash
159cb 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 = pager_pagehas
159cc 68 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a h(pPage);.}../*.
159cd 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47 ** The CHECK_PAG
159ce 45 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20 E macro takes a
159cf 50 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67 PgHdr* as an arg
159d0 75 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 ument. If SQLITE
159d1 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 _CHECK_PAGES.**
159d2 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 is defined, and
159d3 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65 NDEBUG is not de
159d4 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 fined, an assert
159d5 28 29 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65 () statement che
159d6 63 6b 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 cks.** that the
159d7 70 61 67 65 20 69 73 20 65 69 74 68 65 72 20 64 page is either d
159d8 69 72 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 irty or still ma
159d9 74 63 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c tches the calcul
159da 61 74 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a ated page-hash..
159db 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b */.#define CHECK
159dc 5f 50 41 47 45 28 78 29 20 63 68 65 63 6b 50 61 _PAGE(x) checkPa
159dd 67 65 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69 ge(x).static voi
159de 64 20 63 68 65 63 6b 50 61 67 65 28 50 67 48 64 d checkPage(PgHd
159df 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 r *pPg){. Pager
159e0 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e *pPager = pPg->
159e1 70 50 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74 pPager;. assert
159e2 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 ( pPager->eState
159e3 21 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 29 3b !=PAGER_ERROR );
159e4 0a 20 20 61 73 73 65 72 74 28 20 28 70 50 67 2d . assert( (pPg-
159e5 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
159e6 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 67 65 TY) || pPg->page
159e7 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 67 65 Hash==pager_page
159e8 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d 0a 0a hash(pPg) );.}..
159e9 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 70 61 #else.#define pa
159ea 67 65 72 5f 64 61 74 61 68 61 73 68 28 58 2c 59 ger_datahash(X,Y
159eb 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70 61 67 ) 0.#define pag
159ec 65 72 5f 70 61 67 65 68 61 73 68 28 58 29 20 20 er_pagehash(X)
159ed 30 0a 23 64 65 66 69 6e 65 20 70 61 67 65 72 5f 0.#define pager_
159ee 73 65 74 5f 70 61 67 65 68 61 73 68 28 58 29 0a set_pagehash(X).
159ef 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 41 #define CHECK_PA
159f0 47 45 28 78 29 0a 23 65 6e 64 69 66 20 20 2f 2a GE(x).#endif /*
159f1 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 SQLITE_CHECK_PA
159f2 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 68 GES */../*.** Wh
159f3 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 en this is calle
159f4 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 d the journal fi
159f5 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 61 le for pager pPa
159f6 67 65 72 20 6d 75 73 74 20 62 65 20 6f 70 65 6e ger must be open
159f7 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 ..** This functi
159f8 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 on attempts to r
159f9 65 61 64 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 ead a master jou
159fa 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 66 rnal file name f
159fb 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 65 6e 64 20 rom the .** end
159fc 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64 2c of the file and,
159fd 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 if successful,
159fe 63 6f 70 69 65 73 20 69 74 20 69 6e 74 6f 20 6d copies it into m
159ff 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 64 20 0a emory supplied .
15a00 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 ** by the caller
15a01 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 . See comments a
15a02 62 6f 76 65 20 77 72 69 74 65 4d 61 73 74 65 72 bove writeMaster
15a03 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 20 74 68 Journal() for th
15a04 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 75 73 65 64 e format.** used
15a05 20 74 6f 20 73 74 6f 72 65 20 61 20 6d 61 73 74 to store a mast
15a06 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
15a07 6e 61 6d 65 20 61 74 20 74 68 65 20 65 6e 64 20 name at the end
15a08 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c of a journal fil
15a09 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65 72 e..**.** zMaster
15a0a 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 must point to a
15a0b 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65 buffer of at le
15a0c 61 73 74 20 6e 4d 61 73 74 65 72 20 62 79 74 65 ast nMaster byte
15a0d 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a s allocated by.*
15a0e 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 54 68 * the caller. Th
15a0f 69 73 20 73 68 6f 75 6c 64 20 62 65 20 73 71 6c is should be sql
15a10 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e ite3_vfs.mxPathn
15a11 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 75 72 65 ame+1 (to ensure
15a12 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e 6f there is.** eno
15a13 75 67 68 20 73 70 61 63 65 20 74 6f 20 77 72 69 ugh space to wri
15a14 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f te the master jo
15a15 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66 20 urnal name). If
15a16 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
15a17 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74 68 al.** name in th
15a18 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f 6e e journal is lon
15a19 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 74 65 72 ger than nMaster
15a1a 20 62 79 74 65 73 20 28 69 6e 63 6c 75 64 69 6e bytes (includin
15a1b 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 g a.** nul-termi
15a1c 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 74 68 69 nator), then thi
15a1d 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61 73 20 s is handled as
15a1e 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 if no master jou
15a1f 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65 72 rnal name.** wer
15a20 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 e present in the
15a21 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 journal..**.**
15a22 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 If a master jour
15a23 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 nal file name is
15a24 20 70 72 65 73 65 6e 74 20 61 74 20 74 68 65 20 present at the
15a25 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e end of the journ
15a26 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 65 6e al.** file, then
15a27 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 69 6e it is copied in
15a28 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 70 6f to the buffer po
15a29 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 inted to by zMas
15a2a 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 6c 2d 74 65 ter. A.** nul-te
15a2b 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 69 73 rminator byte is
15a2c 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 appended to the
15a2d 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f 77 69 6e buffer followin
15a2e 67 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 g the master.**
15a2f 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d journal file nam
15a30 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 e..**.** If it i
15a31 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68 61 s determined tha
15a32 74 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 t no master jour
15a33 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 nal file name is
15a34 20 70 72 65 73 65 6e 74 20 0a 2a 2a 20 7a 4d 61 present .** zMa
15a35 73 74 65 72 5b 30 5d 20 69 73 20 73 65 74 20 74 ster[0] is set t
15a36 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f o 0 and SQLITE_O
15a37 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a K returned..**.*
15a38 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 * If an error oc
15a39 63 75 72 73 20 77 68 69 6c 65 20 72 65 61 64 69 curs while readi
15a3a 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 ng from the jour
15a3b 6e 61 6c 20 66 69 6c 65 2c 20 61 6e 20 53 51 4c nal file, an SQL
15a3c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 ite.** error cod
15a3d 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a e is returned..*
15a3e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 /.static int rea
15a3f 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 73 dMasterJournal(s
15a40 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 72 qlite3_file *pJr
15a41 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 nl, char *zMaste
15a42 72 2c 20 75 33 32 20 6e 4d 61 73 74 65 72 29 7b r, u32 nMaster){
15a43 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
15a44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15a45 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
15a46 20 20 75 33 32 20 6c 65 6e 3b 20 20 20 20 20 20 u32 len;
15a47 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15a48 4c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20 Length in bytes
15a49 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 of master journa
15a4a 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 36 34 20 l name */. i64
15a4b 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 szJ;
15a4c 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 /* Total
15a4d 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 size in bytes of
15a4e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 70 4a journal file pJ
15a4f 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 rnl */. u32 cks
15a50 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 um;
15a51 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65 63 6b 73 /* MJ checks
15a52 75 6d 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 um value read fr
15a53 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 om journal */.
15a54 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 20 u32 u;
15a55 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e /* Un
15a56 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e signed loop coun
15a57 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ter */. unsigne
15a58 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38 5d d char aMagic[8]
15a59 3b 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20 ; /* A buffer
15a5a 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 to hold the magi
15a5b 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 7a 4d c header */. zM
15a5c 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b aster[0] = '\0';
15a5d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f .. if( SQLITE_O
15a5e 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 K!=(rc = sqlite3
15a5f 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a 72 6e 6c OsFileSize(pJrnl
15a60 2c 20 26 73 7a 4a 29 29 0a 20 20 20 7c 7c 20 73 , &szJ)). || s
15a61 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 53 51 4c 49 zJ<16. || SQLI
15a62 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 TE_OK!=(rc = rea
15a63 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 d32bits(pJrnl, s
15a64 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 29 0a 20 20 zJ-16, &len)).
15a65 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 74 65 72 || len>=nMaster
15a66 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f . || SQLITE_O
15a67 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 K!=(rc = read32b
15a68 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 its(pJrnl, szJ-1
15a69 32 2c 20 26 63 6b 73 75 6d 29 29 0a 20 20 20 7c 2, &cksum)). |
15a6a 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 | SQLITE_OK!=(rc
15a6b 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
15a6c 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69 63 2c 20 (pJrnl, aMagic,
15a6d 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 20 20 7c 7c 8, szJ-8)). ||
15a6e 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 memcmp(aMagic,
15a6f 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 aJournalMagic, 8
15a70 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f ). || SQLITE_O
15a71 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 K!=(rc = sqlite3
15a72 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 7a 4d OsRead(pJrnl, zM
15a73 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 7a 4a 2d aster, len, szJ-
15a74 31 36 2d 6c 65 6e 29 29 0a 20 20 29 7b 0a 20 20 16-len)). ){.
15a75 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
15a76 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 74 68 .. /* See if th
15a77 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 74 63 68 e checksum match
15a78 65 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f es the master jo
15a79 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 urnal name */.
15a7a 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 6e 3b 20 for(u=0; u<len;
15a7b 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 u++){. cksum
15a7c 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d 3b 0a 20 -= zMaster[u];.
15a7d 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d 20 29 }. if( cksum )
15a7e 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 {. /* If the
15a7f 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e 27 74 checksum doesn't
15a80 20 61 64 64 20 75 70 2c 20 74 68 65 6e 20 6f 6e add up, then on
15a81 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 e or more of the
15a82 20 64 69 73 6b 20 73 65 63 74 6f 72 73 0a 20 20 disk sectors.
15a83 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ** containing
15a84 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
15a85 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 63 al filename is c
15a86 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 20 6d orrupted. This m
15a87 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65 66 69 eans. ** defi
15a88 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61 63 6b nitely roll back
15a89 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 75 72 6e , so just return
15a8a 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 72 SQLITE_OK and r
15a8b 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a 20 20 eport a (nul).
15a8c 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72 ** master-jour
15a8d 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 20 20 nal filename..
15a8e 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 30 */. len = 0
15a8f 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 5b ;. }. zMaster[
15a90 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 len] = '\0';.
15a91 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
15a92 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 _OK;.}../*.** Re
15a93 74 75 72 6e 20 74 68 65 20 6f 66 66 73 65 74 20 turn the offset
15a94 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 62 6f of the sector bo
15a95 75 6e 64 61 72 79 20 61 74 20 6f 72 20 69 6d 6d undary at or imm
15a96 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 66 6f 6c ediately .** fol
15a97 6c 6f 77 69 6e 67 20 74 68 65 20 76 61 6c 75 65 lowing the value
15a98 20 69 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 in pPager->jour
15a99 6e 61 6c 4f 66 66 2c 20 61 73 73 75 6d 69 6e 67 nalOff, assuming
15a9a 20 61 20 73 65 63 74 6f 72 20 0a 2a 2a 20 73 69 a sector .** si
15a9b 7a 65 20 6f 66 20 70 50 61 67 65 72 2d 3e 73 65 ze of pPager->se
15a9c 63 74 6f 72 53 69 7a 65 20 62 79 74 65 73 2e 0a ctorSize bytes..
15a9d 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f 72 20 61 20 **.** i.e for a
15a9e 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 35 sector size of 5
15a9f 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67 65 12:.**.** Page
15aa0 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 20 20 20 r.journalOff
15aa1 20 20 20 20 20 20 52 65 74 75 72 6e 20 76 61 6c Return val
15aa2 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d ue.** --------
15aa3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
15aa4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a ---------------.
15aa5 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 20 20 ** 0
15aa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
15aa7 0a 2a 2a 20 20 20 35 31 32 20 20 20 20 20 20 20 .** 512
15aa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15aa9 35 31 32 0a 2a 2a 20 20 20 31 30 30 20 20 20 20 512.** 100
15aaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15aab 20 20 20 35 31 32 0a 2a 2a 20 20 20 32 30 30 30 512.** 2000
15aac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15aad 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 20 0a 2a 2048.** .*
15aae 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6a 6f 75 /.static i64 jou
15aaf 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 50 61 rnalHdrOffset(Pa
15ab0 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
15ab1 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a i64 offset = 0;.
15ab2 20 20 69 36 34 20 63 20 3d 20 70 50 61 67 65 72 i64 c = pPager
15ab3 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 ->journalOff;.
15ab4 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6f 66 66 if( c ){. off
15ab5 73 65 74 20 3d 20 28 28 63 2d 31 29 2f 4a 4f 55 set = ((c-1)/JOU
15ab6 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
15ab7 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f 55 52 4e er) + 1) * JOURN
15ab8 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
15ab9 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 );. }. assert(
15aba 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f offset%JOURNAL_
15abb 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d HDR_SZ(pPager)==
15abc 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 0 );. assert( o
15abd 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 20 61 73 ffset>=c );. as
15abe 73 65 72 74 28 20 28 6f 66 66 73 65 74 2d 63 29 sert( (offset-c)
15abf 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 <JOURNAL_HDR_SZ(
15ac0 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 pPager) );. ret
15ac1 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a 0a 2f urn offset;.}../
15ac2 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c *.** The journal
15ac3 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 file must be op
15ac4 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e en when this fun
15ac5 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e ction is called.
15ac6 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
15ac7 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20 tion is a no-op
15ac8 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 if the journal f
15ac9 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e ile has not been
15aca 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 77 written to.** w
15acb 69 74 68 69 6e 20 74 68 65 20 63 75 72 72 65 6e ithin the curren
15acc 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69 t transaction (i
15acd 2e 65 2e 20 69 66 20 50 61 67 65 72 2e 6a 6f 75 .e. if Pager.jou
15ace 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0a 2a 2a 0a rnalOff==0)..**.
15acf 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63 61 74 65 ** If doTruncate
15ad0 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 20 is non-zero or
15ad1 74 68 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 the Pager.journa
15ad2 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61 72 69 61 lSizeLimit varia
15ad3 62 6c 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f ble is.** set to
15ad4 20 30 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 0, then truncat
15ad5 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
15ad6 6c 65 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 le to zero bytes
15ad7 20 69 6e 20 73 69 7a 65 2e 20 4f 74 68 65 72 77 in size. Otherw
15ad8 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f 20 74 68 65 ise,.** zero the
15ad9 20 32 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 28-byte header
15ada 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 at the start of
15adb 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
15adc 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 . In either case
15add 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20 70 61 67 , .** if the pag
15ade 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 6e 6f 2d er is not in no-
15adf 73 79 6e 63 20 6d 6f 64 65 2c 20 73 79 6e 63 20 sync mode, sync
15ae0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
15ae1 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a immediately .**
15ae2 20 61 66 74 65 72 20 77 72 69 74 69 6e 67 20 6f after writing o
15ae3 72 20 74 72 75 6e 63 61 74 69 6e 67 20 69 74 2e r truncating it.
15ae4 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 67 65 72 2e .**.** If Pager.
15ae5 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 journalSizeLimit
15ae6 20 69 73 20 73 65 74 20 74 6f 20 61 20 70 6f 73 is set to a pos
15ae7 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 itive, non-zero
15ae8 76 61 6c 75 65 2c 20 61 6e 64 0a 2a 2a 20 66 6f value, and.** fo
15ae9 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 72 75 6e llowing the trun
15aea 63 61 74 69 6f 6e 20 6f 72 20 7a 65 72 6f 69 6e cation or zeroin
15aeb 67 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 g described abov
15aec 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 e the size of th
15aed 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 e .** journal fi
15aee 6c 65 20 69 6e 20 62 79 74 65 73 20 69 73 20 6c le in bytes is l
15aef 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 20 arger than this
15af0 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 72 75 6e value, then trun
15af1 63 61 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 cate the.** jour
15af2 6e 61 6c 20 66 69 6c 65 20 74 6f 20 50 61 67 65 nal file to Page
15af3 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d r.journalSizeLim
15af4 69 74 20 62 79 74 65 73 2e 20 54 68 65 20 6a 6f it bytes. The jo
15af5 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 0a urnal file does.
15af6 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 ** not need to b
15af7 65 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f 77 69 e synced followi
15af8 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f ng this operatio
15af9 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 n..**.** If an I
15afa 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 O error occurs,
15afb 61 62 61 6e 64 6f 6e 20 70 72 6f 63 65 73 73 69 abandon processi
15afc 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 ng and return th
15afd 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 2e e IO error code.
15afe 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 .** Otherwise, r
15aff 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e eturn SQLITE_OK.
15b00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 7a .*/.static int z
15b01 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61 eroJournalHdr(Pa
15b02 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
15b03 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0a 20 20 doTruncate){.
15b04 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
15b05 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 OK;
15b06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15b07 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
15b08 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 */. assert( is
15b09 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
15b0a 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 ) );. if( pPage
15b0b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b r->journalOff ){
15b0c 0a 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20 69 . const i64 i
15b0d 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 72 2d 3e Limit = pPager->
15b0e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 journalSizeLimit
15b0f 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 ; /* Local ca
15b10 63 68 65 20 6f 66 20 6a 73 6c 20 2a 2f 0a 0a 20 che of jsl */..
15b11 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 5a 45 IOTRACE(("JZE
15b12 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 70 50 61 ROHDR %p\n", pPa
15b13 67 65 72 29 29 0a 20 20 20 20 69 66 28 20 64 6f ger)). if( do
15b14 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 4c 69 6d Truncate || iLim
15b15 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 it==0 ){. r
15b16 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 c = sqlite3OsTru
15b17 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 ncate(pPager->jf
15b18 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 d, 0);. }else
15b19 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 63 {. static c
15b1a 6f 6e 73 74 20 63 68 61 72 20 7a 65 72 6f 48 64 onst char zeroHd
15b1b 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a 20 20 20 r[28] = {0};.
15b1c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
15b1d 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a sWrite(pPager->j
15b1e 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 73 69 7a fd, zeroHdr, siz
15b1f 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c 20 30 29 eof(zeroHdr), 0)
15b20 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
15b21 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
15b22 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 !pPager->noSync
15b23 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
15b24 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 qlite3OsSync(pPa
15b25 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54 45 ger->jfd, SQLITE
15b26 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 7c 70 _SYNC_DATAONLY|p
15b27 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 Pager->syncFlags
15b28 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a );. }.. /*
15b29 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 At this point t
15b2a 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
15b2b 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 75 74 20 s committed but
15b2c 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 0a the write lock .
15b2d 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 ** is still
15b2e 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 held on the file
15b2f 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 . If there is a
15b30 73 69 7a 65 20 6c 69 6d 69 74 20 63 6f 6e 66 69 size limit confi
15b31 67 75 72 65 64 20 66 6f 72 20 0a 20 20 20 20 2a gured for . *
15b32 2a 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 * the persistent
15b33 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 journal and the
15b34 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 75 journal file cu
15b35 72 72 65 6e 74 6c 79 20 63 6f 6e 73 75 6d 65 73 rrently consumes
15b36 20 6d 6f 72 65 0a 20 20 20 20 2a 2a 20 73 70 61 more. ** spa
15b37 63 65 20 74 68 61 6e 20 74 68 61 74 20 6c 69 6d ce than that lim
15b38 69 74 20 61 6c 6c 6f 77 73 20 66 6f 72 2c 20 74 it allows for, t
15b39 72 75 6e 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 runcate it now.
15b3a 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 There is no need
15b3b 0a 20 20 20 20 2a 2a 20 74 6f 20 73 79 6e 63 20 . ** to sync
15b3c 74 68 65 20 66 69 6c 65 20 66 6f 6c 6c 6f 77 69 the file followi
15b3d 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f ng this operatio
15b3e 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 n.. */. if
15b3f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
15b40 26 26 20 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 && iLimit>0 ){.
15b41 20 20 20 20 20 69 36 34 20 73 7a 3b 0a 20 20 20 i64 sz;.
15b42 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
15b43 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 sFileSize(pPager
15b44 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a 20 20 20 ->jfd, &sz);.
15b45 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
15b46 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c 69 6d 69 E_OK && sz>iLimi
15b47 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 t ){. rc
15b48 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
15b49 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ate(pPager->jfd,
15b4a 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 iLimit);.
15b4b 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
15b4c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
15b4d 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * The journal fi
15b4e 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 le must be open
15b4f 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e when this routin
15b50 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 20 6a e is called. A j
15b51 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 61 64 65 72 ournal.** header
15b52 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a (JOURNAL_HDR_SZ
15b53 20 62 79 74 65 73 29 20 69 73 20 77 72 69 74 74 bytes) is writt
15b54 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 en into the jour
15b55 6e 61 6c 20 66 69 6c 65 20 61 74 20 74 68 65 0a nal file at the.
15b56 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 ** current locat
15b57 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ion..**.** The f
15b58 6f 72 6d 61 74 20 66 6f 72 20 74 68 65 20 6a 6f ormat for the jo
15b59 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 urnal header is
15b5a 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d as follows:.** -
15b5b 20 38 20 62 79 74 65 73 3a 20 4d 61 67 69 63 20 8 bytes: Magic
15b5c 69 64 65 6e 74 69 66 79 69 6e 67 20 6a 6f 75 72 identifying jour
15b5d 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 2d nal format..** -
15b5e 20 34 20 62 79 74 65 73 3a 20 4e 75 6d 62 65 72 4 bytes: Number
15b5f 20 6f 66 20 72 65 63 6f 72 64 73 20 69 6e 20 6a of records in j
15b60 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d 31 20 6e 6f ournal, or -1 no
15b61 2d 73 79 6e 63 20 6d 6f 64 65 20 69 73 20 6f 6e -sync mode is on
15b62 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 ..** - 4 bytes:
15b63 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 75 73 Random number us
15b64 65 64 20 66 6f 72 20 70 61 67 65 20 68 61 73 68 ed for page hash
15b65 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 ..** - 4 bytes:
15b66 49 6e 69 74 69 61 6c 20 64 61 74 61 62 61 73 65 Initial database
15b67 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2a 20 page count..**
15b68 2d 20 34 20 62 79 74 65 73 3a 20 53 65 63 74 6f - 4 bytes: Secto
15b69 72 20 73 69 7a 65 20 75 73 65 64 20 62 79 20 74 r size used by t
15b6a 68 65 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 he process that
15b6b 77 72 6f 74 65 20 74 68 69 73 20 6a 6f 75 72 6e wrote this journ
15b6c 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 al..** - 4 bytes
15b6d 3a 20 44 61 74 61 62 61 73 65 20 70 61 67 65 20 : Database page
15b6e 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c size..** .** Fol
15b6f 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55 52 4e 41 lowed by (JOURNA
15b70 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29 20 62 L_HDR_SZ - 28) b
15b71 79 74 65 73 20 6f 66 20 75 6e 75 73 65 64 20 73 ytes of unused s
15b72 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 pace..*/.static
15b73 69 6e 74 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c int writeJournal
15b74 48 64 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 Hdr(Pager *pPage
15b75 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 r){. int rc = S
15b76 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 QLITE_OK;
15b77 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
15b78 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 63 68 urn code */. ch
15b79 61 72 20 2a 7a 48 65 61 64 65 72 20 3d 20 70 50 ar *zHeader = pP
15b7a 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b ager->pTmpSpace;
15b7b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 /* Temporary s
15b7c 70 61 63 65 20 75 73 65 64 20 74 6f 20 62 75 69 pace used to bui
15b7d 6c 64 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 ld header */. u
15b7e 33 32 20 6e 48 65 61 64 65 72 20 3d 20 28 75 33 32 nHeader = (u3
15b7f 32 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 2)pPager->pageSi
15b80 7a 65 3b 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 ze;/* Size of bu
15b81 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 ffer pointed to
15b82 62 79 20 7a 48 65 61 64 65 72 20 2a 2f 0a 20 20 by zHeader */.
15b83 75 33 32 20 6e 57 72 69 74 65 3b 20 20 20 20 20 u32 nWrite;
15b84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15b85 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 /* Bytes of
15b86 68 65 61 64 65 72 20 73 65 63 74 6f 72 20 77 72 header sector wr
15b87 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 itten */. int i
15b88 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
15b89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15b8a 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
15b8b 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f /.. assert( isO
15b8c 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
15b8d 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72 ); /* Jour
15b8e 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 nal file must be
15b8f 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 69 66 28 open. */.. if(
15b90 20 6e 48 65 61 64 65 72 3e 4a 4f 55 52 4e 41 4c nHeader>JOURNAL
15b91 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 _HDR_SZ(pPager)
15b92 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 3d ){. nHeader =
15b93 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 JOURNAL_HDR_SZ(
15b94 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 pPager);. }..
15b95 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 /* If there are
15b96 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 active savepoint
15b97 73 20 61 6e 64 20 61 6e 79 20 6f 66 20 74 68 65 s and any of the
15b98 6d 20 77 65 72 65 20 63 72 65 61 74 65 64 20 0a m were created .
15b99 20 20 2a 2a 20 73 69 6e 63 65 20 74 68 65 20 6d ** since the m
15b9a 6f 73 74 20 72 65 63 65 6e 74 20 6a 6f 75 72 6e ost recent journ
15b9b 61 6c 20 68 65 61 64 65 72 20 77 61 73 20 77 72 al header was wr
15b9c 69 74 74 65 6e 2c 20 75 70 64 61 74 65 20 74 68 itten, update th
15b9d 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 53 61 76 e . ** PagerSav
15b9e 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 epoint.iHdrOffse
15b9f 74 20 66 69 65 6c 64 73 20 6e 6f 77 2e 0a 20 20 t fields now..
15ba0 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 */. for(ii=0; i
15ba1 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 i<pPager->nSavep
15ba2 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 oint; ii++){.
15ba3 20 69 66 28 20 70 50 61 67 65 72 2d 3e 61 53 61 if( pPager->aSa
15ba4 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72 vepoint[ii].iHdr
15ba5 4f 66 66 73 65 74 3d 3d 30 20 29 7b 0a 20 20 20 Offset==0 ){.
15ba6 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 pPager->aSave
15ba7 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 point[ii].iHdrOf
15ba8 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a fset = pPager->j
15ba9 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 7d ournalOff;. }
15baa 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e . }.. pPager->
15bab 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70 50 61 journalHdr = pPa
15bac 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
15bad 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 = journalHdrOffs
15bae 65 74 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 2f et(pPager);.. /
15baf 2a 20 0a 20 20 2a 2a 20 57 72 69 74 65 20 74 68 * . ** Write th
15bb0 65 20 6e 52 65 63 20 46 69 65 6c 64 20 2d 20 74 e nRec Field - t
15bb1 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
15bb2 65 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 66 e records that f
15bb3 6f 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 ollow this. **
15bb4 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 journal header.
15bb5 4e 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 Normally, zero i
15bb6 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 69 s written to thi
15bb7 73 20 76 61 6c 75 65 20 61 74 20 74 68 69 73 20 s value at this
15bb8 74 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 time.. ** After
15bb9 20 74 68 65 20 72 65 63 6f 72 64 73 20 61 72 65 the records are
15bba 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6a 6f added to the jo
15bbb 75 72 6e 61 6c 20 28 61 6e 64 20 74 68 65 20 6a urnal (and the j
15bbc 6f 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a ournal synced, .
15bbd 20 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d ** if in full-
15bbe 73 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 sync mode), the
15bbf 7a 65 72 6f 20 69 73 20 6f 76 65 72 77 72 69 74 zero is overwrit
15bc0 74 65 6e 20 77 69 74 68 20 74 68 65 20 74 72 75 ten with the tru
15bc1 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 e number. ** of
15bc2 20 72 65 63 6f 72 64 73 20 28 73 65 65 20 73 79 records (see sy
15bc3 6e 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 ncJournal())..
15bc4 2a 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74 65 72 **. ** A faster
15bc5 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 73 20 alternative is
15bc6 74 6f 20 77 72 69 74 65 20 30 78 46 46 46 46 46 to write 0xFFFFF
15bc7 46 46 46 20 74 6f 20 74 68 65 20 6e 52 65 63 20 FFF to the nRec
15bc8 66 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a field. When. **
15bc9 20 72 65 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 reading the jou
15bca 72 6e 61 6c 20 74 68 69 73 20 76 61 6c 75 65 20 rnal this value
15bcb 74 65 6c 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 tells SQLite to
15bcc 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 0a assume that the.
15bcd 20 20 2a 2a 20 72 65 73 74 20 6f 66 20 74 68 65 ** rest of the
15bce 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f journal file co
15bcf 6e 74 61 69 6e 73 20 76 61 6c 69 64 20 70 61 67 ntains valid pag
15bd0 65 20 72 65 63 6f 72 64 73 2e 20 54 68 69 73 20 e records. This
15bd1 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 assumption. **
15bd2 69 73 20 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 is dangerous, as
15bd3 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 if a failure oc
15bd4 63 75 72 72 65 64 20 77 68 69 6c 73 74 20 77 72 curred whilst wr
15bd5 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 iting to the jou
15bd6 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 rnal. ** file i
15bd7 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f t may contain so
15bd8 6d 65 20 67 61 72 62 61 67 65 20 64 61 74 61 2e me garbage data.
15bd9 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 20 73 There are two s
15bda 63 65 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 cenarios. ** wh
15bdb 65 72 65 20 74 68 69 73 20 72 69 73 6b 20 63 61 ere this risk ca
15bdc 6e 20 62 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 n be ignored:.
15bdd 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e **. ** * When
15bde 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
15bdf 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 no-sync mode. C
15be0 6f 72 72 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f orruption can fo
15be1 6c 6c 6f 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 llow a. **
15be2 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 69 6e power failure in
15be3 20 74 68 69 73 20 63 61 73 65 20 61 6e 79 77 61 this case anywa
15be4 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a y.. **. ** *
15be5 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 When the SQLITE
15be6 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 _IOCAP_SAFE_APPE
15be7 4e 44 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 ND flag is set.
15be8 54 68 69 73 20 67 75 61 72 61 6e 74 65 65 73 0a This guarantees.
15be9 20 20 2a 2a 20 20 20 20 20 74 68 61 74 20 67 61 ** that ga
15bea 72 62 61 67 65 20 64 61 74 61 20 69 73 20 6e 65 rbage data is ne
15beb 76 65 72 20 61 70 70 65 6e 64 65 64 20 74 6f 20 ver appended to
15bec 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
15bed 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
15bee 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
15bef 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e fd) || pPager->n
15bf0 6f 53 79 6e 63 20 29 3b 0a 20 20 69 66 28 20 70 oSync );. if( p
15bf1 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 7c 7c Pager->noSync ||
15bf2 20 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 (pPager->journa
15bf3 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
15bf4 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 RNALMODE_MEMORY)
15bf5 0a 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f . || (sqlite3O
15bf6 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 sDeviceCharacter
15bf7 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 istics(pPager->f
15bf8 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f d)&SQLITE_IOCAP_
15bf9 53 41 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 SAFE_APPEND) .
15bfa 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 48 ){. memcpy(zH
15bfb 65 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d eader, aJournalM
15bfc 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f agic, sizeof(aJo
15bfd 75 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 urnalMagic));.
15bfe 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 put32bits(&zHe
15bff 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 ader[sizeof(aJou
15c00 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 30 78 66 rnalMagic)], 0xf
15c01 66 66 66 66 66 66 66 29 3b 0a 20 20 7d 65 6c 73 fffffff);. }els
15c02 65 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 7a 48 e{. memset(zH
15c03 65 61 64 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 eader, 0, sizeof
15c04 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b (aJournalMagic)+
15c05 34 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 4);. }.. /* Th
15c06 65 20 72 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 e random check-h
15c07 61 73 68 20 69 6e 69 74 69 61 6c 69 73 65 72 20 ash initialiser
15c08 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 */ . sqlite3_ra
15c09 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 ndomness(sizeof(
15c0a 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 pPager->cksumIni
15c0b 74 29 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 t), &pPager->cks
15c0c 75 6d 49 6e 69 74 29 3b 0a 20 20 70 75 74 33 32 umInit);. put32
15c0d 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 bits(&zHeader[si
15c0e 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
15c0f 69 63 29 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e ic)+4], pPager->
15c10 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a cksumInit);. /*
15c11 20 54 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 The initial dat
15c12 61 62 61 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20 abase size */.
15c13 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 put32bits(&zHead
15c14 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e er[sizeof(aJourn
15c15 61 6c 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 alMagic)+8], pPa
15c16 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 ger->dbOrigSize)
15c17 3b 0a 20 20 2f 2a 20 54 68 65 20 61 73 73 75 6d ;. /* The assum
15c18 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 66 ed sector size f
15c19 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 or this process
15c1a 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 */. put32bits(&
15c1b 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
15c1c 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 32 JournalMagic)+12
15c1d 5d 2c 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f ], pPager->secto
15c1e 72 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 rSize);.. /* Th
15c1f 65 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 e page size */.
15c20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 put32bits(&zHea
15c21 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 der[sizeof(aJour
15c22 6e 61 6c 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70 nalMagic)+16], p
15c23 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 Pager->pageSize)
15c24 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 ;.. /* Initiali
15c25 7a 69 6e 67 20 74 68 65 20 74 61 69 6c 20 6f 66 zing the tail of
15c26 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20 6e the buffer is n
15c27 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 20 45 ot necessary. E
15c28 76 65 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 verything. ** w
15c29 6f 72 6b 73 20 66 69 6e 64 20 69 66 20 74 68 65 orks find if the
15c2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73 65 following memse
15c2b 74 28 29 20 69 73 20 6f 6d 69 74 74 65 64 2e 20 t() is omitted.
15c2c 20 42 75 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e But initializin
15c2d 67 0a 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 g. ** the memor
15c2e 79 20 70 72 65 76 65 6e 74 73 20 76 61 6c 67 72 y prevents valgr
15c2f 69 6e 64 20 66 72 6f 6d 20 63 6f 6d 70 6c 61 69 ind from complai
15c30 6e 69 6e 67 2c 20 73 6f 20 77 65 20 61 72 65 20 ning, so we are
15c31 77 69 6c 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 willing to. **
15c32 74 61 6b 65 20 74 68 65 20 70 65 72 66 6f 72 6d take the perform
15c33 61 6e 63 65 20 68 69 74 2e 0a 20 20 2a 2f 0a 20 ance hit.. */.
15c34 20 6d 65 6d 73 65 74 28 26 7a 48 65 61 64 65 72 memset(&zHeader
15c35 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c [sizeof(aJournal
15c36 4d 61 67 69 63 29 2b 32 30 5d 2c 20 30 2c 0a 20 Magic)+20], 0,.
15c37 20 20 20 20 20 20 20 20 6e 48 65 61 64 65 72 2d nHeader-
15c38 28 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c (sizeof(aJournal
15c39 4d 61 67 69 63 29 2b 32 30 29 29 3b 0a 0a 20 20 Magic)+20));..
15c3a 2f 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 69 74 /* In theory, it
15c3b 20 69 73 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61 is only necessa
15c3c 72 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 ry to write the
15c3d 32 38 20 62 79 74 65 73 20 74 68 61 74 20 74 68 28 bytes that th
15c3e 65 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 e . ** journal
15c3f 68 65 61 64 65 72 20 63 6f 6e 73 75 6d 65 73 20 header consumes
15c40 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
15c41 69 6c 65 20 68 65 72 65 2e 20 54 68 65 6e 20 69 ile here. Then i
15c42 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 ncrement the .
15c43 2a 2a 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c ** Pager.journal
15c44 4f 66 66 20 76 61 72 69 61 62 6c 65 20 62 79 20 Off variable by
15c45 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 73 JOURNAL_HDR_SZ s
15c46 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 o that the next
15c47 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 69 73 20 . ** record is
15c48 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66 written to the f
15c49 6f 6c 6c 6f 77 69 6e 67 20 73 65 63 74 6f 72 20 ollowing sector
15c4a 28 6c 65 61 76 69 6e 67 20 61 20 67 61 70 20 69 (leaving a gap i
15c4b 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 n the file. **
15c4c 74 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6d 70 that will be imp
15c4d 6c 69 63 69 74 6c 79 20 66 69 6c 6c 65 64 20 69 licitly filled i
15c4e 6e 20 62 79 20 74 68 65 20 4f 53 29 2e 0a 20 20 n by the OS)..
15c4f 2a 2a 0a 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 **. ** However
15c50 69 74 20 68 61 73 20 62 65 65 6e 20 64 69 73 63 it has been disc
15c51 6f 76 65 72 65 64 20 74 68 61 74 20 6f 6e 20 73 overed that on s
15c52 6f 6d 65 20 73 79 73 74 65 6d 73 20 74 68 69 73 ome systems this
15c53 20 70 61 74 74 65 72 6e 20 63 61 6e 20 0a 20 20 pattern can .
15c54 2a 2a 20 62 65 20 73 69 67 6e 69 66 69 63 61 6e ** be significan
15c55 74 6c 79 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 tly slower than
15c56 63 6f 6e 74 69 67 75 6f 75 73 6c 79 20 77 72 69 contiguously wri
15c57 74 69 6e 67 20 64 61 74 61 20 74 6f 20 74 68 65 ting data to the
15c58 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 65 76 65 6e file,. ** even
15c59 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20 65 if that means e
15c5a 78 70 6c 69 63 69 74 6c 79 20 77 72 69 74 69 6e xplicitly writin
15c5b 67 20 64 61 74 61 20 74 6f 20 74 68 65 20 62 6c g data to the bl
15c5c 6f 63 6b 20 6f 66 20 0a 20 20 2a 2a 20 28 4a 4f ock of . ** (JO
15c5d 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 URNAL_HDR_SZ - 2
15c5e 38 29 20 62 79 74 65 73 20 74 68 61 74 20 77 69 8) bytes that wi
15c5f 6c 6c 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 20 ll not be used.
15c60 53 6f 20 74 68 61 74 20 69 73 20 77 68 61 74 0a So that is what.
15c61 20 20 2a 2a 20 69 73 20 64 6f 6e 65 2e 20 0a 20 ** is done. .
15c62 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6c 6f 6f **. ** The loo
15c63 70 20 69 73 20 72 65 71 75 69 72 65 64 20 68 65 p is required he
15c64 72 65 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 re in case the s
15c65 65 63 74 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61 ector-size is la
15c66 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 rger than the .
15c67 20 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 ** database pag
15c68 65 20 73 69 7a 65 2e 20 53 69 6e 63 65 20 74 68 e size. Since th
15c69 65 20 7a 48 65 61 64 65 72 20 62 75 66 66 65 72 e zHeader buffer
15c6a 20 69 73 20 6f 6e 6c 79 20 50 61 67 65 72 2e 70 is only Pager.p
15c6b 61 67 65 53 69 7a 65 0a 20 20 2a 2a 20 62 79 74 ageSize. ** byt
15c6c 65 73 20 69 6e 20 73 69 7a 65 2c 20 6d 6f 72 65 es in size, more
15c6d 20 74 68 61 6e 20 6f 6e 65 20 63 61 6c 6c 20 74 than one call t
15c6e 6f 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 o sqlite3OsWrite
15c6f 28 29 20 6d 61 79 20 62 65 20 72 65 71 75 69 72 () may be requir
15c70 65 64 0a 20 20 2a 2a 20 74 6f 20 70 6f 70 75 6c ed. ** to popul
15c71 61 74 65 20 74 68 65 20 65 6e 74 69 72 65 20 6a ate the entire j
15c72 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65 ournal header se
15c73 63 74 6f 72 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f ctor.. */ . fo
15c74 72 28 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d r(nWrite=0; rc==
15c75 53 51 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 SQLITE_OK&&nWrit
15c76 65 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a e<JOURNAL_HDR_SZ
15c77 28 70 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65 (pPager); nWrite
15c78 2b 3d 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 20 +=nHeader){.
15c79 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 IOTRACE(("JHDR %
15c7a 70 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 p %lld %d\n", pP
15c7b 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f ager, pPager->jo
15c7c 75 72 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 urnalHdr, nHeade
15c7d 72 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c r)). rc = sql
15c7e 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
15c7f 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 er->jfd, zHeader
15c80 2c 20 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65 , nHeader, pPage
15c81 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a r->journalOff);.
15c82 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
15c83 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3c er->journalHdr <
15c84 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 = pPager->journa
15c85 6c 4f 66 66 20 29 3b 0a 20 20 20 20 70 50 61 67 lOff );. pPag
15c86 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b er->journalOff +
15c87 3d 20 6e 48 65 61 64 65 72 3b 0a 20 20 7d 0a 0a = nHeader;. }..
15c88 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
15c89 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 /*.** The journa
15c8a 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f l file must be o
15c8b 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 69 73 pen when this is
15c8c 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e called. A journ
15c8d 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 0a 2a al header file.*
15c8e 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 * (JOURNAL_HDR_S
15c8f 5a 20 62 79 74 65 73 29 20 69 73 20 72 65 61 64 Z bytes) is read
15c90 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e from the curren
15c91 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68 t location in th
15c92 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c e journal.** fil
15c93 65 2e 20 54 68 65 20 63 75 72 72 65 6e 74 20 6c e. The current l
15c94 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a ocation in the j
15c95 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 67 ournal file is g
15c96 69 76 65 6e 20 62 79 0a 2a 2a 20 70 50 61 67 65 iven by.** pPage
15c97 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 r->journalOff. S
15c98 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 ee comments abov
15c99 65 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 e function write
15c9a 4a 6f 75 72 6e 61 6c 48 64 72 28 29 20 66 6f 72 JournalHdr() for
15c9b 0a 2a 2a 20 61 20 64 65 73 63 72 69 70 74 69 6f .** a descriptio
15c9c 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c n of the journal
15c9d 20 68 65 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a header format..
15c9e 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 68 65 61 **.** If the hea
15c9f 64 65 72 20 69 73 20 72 65 61 64 20 73 75 63 63 der is read succ
15ca0 65 73 73 66 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 essfully, *pNRec
15ca1 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e is set to the n
15ca2 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 70 61 67 65 umber of.** page
15ca3 20 72 65 63 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 records followi
15ca4 6e 67 20 74 68 69 73 20 68 65 61 64 65 72 20 61 ng this header a
15ca5 6e 64 20 2a 70 44 62 53 69 7a 65 20 69 73 20 73 nd *pDbSize is s
15ca6 65 74 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f et to the size o
15ca7 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 f the.** databas
15ca8 65 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 e before the tra
15ca9 6e 73 61 63 74 69 6f 6e 20 62 65 67 61 6e 2c 20 nsaction began,
15caa 69 6e 20 70 61 67 65 73 2e 20 41 6c 73 6f 2c 20 in pages. Also,
15cab 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 pPager->cksumIni
15cac 74 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 t.** is set to t
15cad 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 he value read fr
15cae 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 om the journal h
15caf 65 61 64 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b eader. SQLITE_OK
15cb0 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 is returned.**
15cb1 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a in this case..**
15cb2 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e .** If the journ
15cb3 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 20 61 al header file a
15cb4 70 70 65 61 72 73 20 74 6f 20 62 65 20 63 6f 72 ppears to be cor
15cb5 72 75 70 74 65 64 2c 20 53 51 4c 49 54 45 5f 44 rupted, SQLITE_D
15cb6 4f 4e 45 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e ONE is.** return
15cb7 65 64 20 61 6e 64 20 2a 70 4e 52 65 63 20 61 6e ed and *pNRec an
15cb8 64 20 2a 50 44 62 53 69 7a 65 20 61 72 65 20 75 d *PDbSize are u
15cb9 6e 64 65 66 69 6e 65 64 2e 20 20 49 66 20 4a 4f ndefined. If JO
15cba 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 URNAL_HDR_SZ byt
15cbb 65 73 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 es.** cannot be
15cbc 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f read from the jo
15cbd 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 20 65 72 urnal file an er
15cbe 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
15cbf 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rned..*/.static
15cc0 69 6e 74 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 int readJournalH
15cc1 64 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 dr(. Pager *pPa
15cc2 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 ger,
15cc3 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 /* Pager obje
15cc4 63 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 48 6f ct */. int isHo
15cc5 74 2c 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c t,. i64 journal
15cc6 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 Size,
15cc7 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
15cc8 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 open journal fi
15cc9 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 le in bytes */.
15cca 20 75 33 32 20 2a 70 4e 52 65 63 2c 20 20 20 20 u32 *pNRec,
15ccb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15ccc 20 4f 55 54 3a 20 56 61 6c 75 65 20 72 65 61 64 OUT: Value read
15ccd 20 66 72 6f 6d 20 74 68 65 20 6e 52 65 63 20 66 from the nRec f
15cce 69 65 6c 64 20 2a 2f 0a 20 20 75 33 32 20 2a 70 ield */. u32 *p
15ccf 44 62 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 DbSize
15cd0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56 /* OUT: V
15cd1 61 6c 75 65 20 6f 66 20 6f 72 69 67 69 6e 61 6c alue of original
15cd2 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 66 database size f
15cd3 69 65 6c 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 ield */.){. int
15cd4 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
15cd5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
15cd6 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 6e urn code */. un
15cd7 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67 signed char aMag
15cd8 69 63 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 41 20 ic[8]; /* A
15cd9 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 buffer to hold t
15cda 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 72 20 he magic header
15cdb 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 */. i64 iHdrOff
15cdc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
15cdd 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 6a /* Offset of j
15cde 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 62 65 ournal header be
15cdf 69 6e 67 20 72 65 61 64 20 2a 2f 0a 0a 20 20 61 ing read */.. a
15ce0 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
15ce1 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20 20 ager->jfd) );
15ce2 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 /* Journal fi
15ce3 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e le must be open.
15ce4 20 2a 2f 0a 0a 20 20 2f 2a 20 41 64 76 61 6e 63 */.. /* Advanc
15ce5 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f e Pager.journalO
15ce6 66 66 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 ff to the start
15ce7 6f 66 20 74 68 65 20 6e 65 78 74 20 73 65 63 74 of the next sect
15ce8 6f 72 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 or. If the. **
15ce9 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
15cea 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20 74 68 too small for th
15ceb 65 72 65 20 74 6f 20 62 65 20 61 20 68 65 61 64 ere to be a head
15cec 65 72 20 73 74 6f 72 65 64 20 61 74 20 74 68 69 er stored at thi
15ced 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 2c 20 72 65 s. ** point, re
15cee 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 turn SQLITE_DONE
15cef 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d .. */. pPager-
15cf0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f >journalOff = jo
15cf1 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 urnalHdrOffset(p
15cf2 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50 Pager);. if( pP
15cf3 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
15cf4 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 +JOURNAL_HDR_SZ(
15cf5 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e 61 pPager) > journa
15cf6 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74 lSize ){. ret
15cf7 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b urn SQLITE_DONE;
15cf8 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20 3d . }. iHdrOff =
15cf9 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
15cfa 4f 66 66 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 Off;.. /* Read
15cfb 69 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 62 in the first 8 b
15cfc 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 ytes of the jour
15cfd 6e 61 6c 20 68 65 61 64 65 72 2e 20 49 66 20 74 nal header. If t
15cfe 68 65 79 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68 hey do not match
15cff 0a 20 20 2a 2a 20 74 68 65 20 20 6d 61 67 69 63 . ** the magic
15d00 20 73 74 72 69 6e 67 20 66 6f 75 6e 64 20 61 74 string found at
15d01 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 61 the start of ea
15d02 63 68 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 ch journal heade
15d03 72 2c 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 53 r, return. ** S
15d04 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 49 66 20 61 QLITE_DONE. If a
15d05 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
15d06 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 s, return an err
15d07 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 77 69 or code. Otherwi
15d08 73 65 2c 0a 20 20 2a 2a 20 70 72 6f 63 65 65 64 se,. ** proceed
15d09 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 48 .. */. if( isH
15d0a 6f 74 20 7c 7c 20 69 48 64 72 4f 66 66 21 3d 70 ot || iHdrOff!=p
15d0b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
15d0c 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 r ){. rc = sq
15d0d 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 lite3OsRead(pPag
15d0e 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c er->jfd, aMagic,
15d0f 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 2c sizeof(aMagic),
15d10 20 69 48 64 72 4f 66 66 29 3b 0a 20 20 20 20 69 iHdrOff);. i
15d11 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 f( rc ){. r
15d12 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
15d13 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 61 if( memcmp(a
15d14 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d Magic, aJournalM
15d15 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61 agic, sizeof(aMa
15d16 67 69 63 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 gic))!=0 ){.
15d17 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15d18 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a DONE;. }. }.
15d19 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 66 . /* Read the f
15d1a 69 72 73 74 20 74 68 72 65 65 20 33 32 2d 62 69 irst three 32-bi
15d1b 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 t fields of the
15d1c 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 3a 20 journal header:
15d1d 54 68 65 20 6e 52 65 63 0a 20 20 2a 2a 20 66 69 The nRec. ** fi
15d1e 65 6c 64 2c 20 74 68 65 20 63 68 65 63 6b 73 75 eld, the checksu
15d1f 6d 2d 69 6e 69 74 69 61 6c 69 7a 65 72 20 61 6e m-initializer an
15d20 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 d the database s
15d21 69 7a 65 20 61 74 20 74 68 65 20 73 74 61 72 74 ize at the start
15d22 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 72 61 . ** of the tra
15d23 6e 73 61 63 74 69 6f 6e 2e 20 52 65 74 75 72 6e nsaction. Return
15d24 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 an error code i
15d25 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 f anything goes
15d26 77 72 6f 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 wrong.. */. if
15d27 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 ( SQLITE_OK!=(rc
15d28 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50 = read32bits(pP
15d29 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f ager->jfd, iHdrO
15d2a 66 66 2b 38 2c 20 70 4e 52 65 63 29 29 0a 20 20 ff+8, pNRec)).
15d2b 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
15d2c 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
15d2d 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
15d2e 72 4f 66 66 2b 31 32 2c 20 26 70 50 61 67 65 72 rOff+12, &pPager
15d2f 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 29 0a 20 20 ->cksumInit)).
15d30 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 || SQLITE_OK!=(
15d31 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
15d32 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
15d33 72 4f 66 66 2b 31 36 2c 20 70 44 62 53 69 7a 65 rOff+16, pDbSize
15d34 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 )). ){. retu
15d35 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 rn rc;. }.. if
15d36 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
15d37 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 75 lOff==0 ){. u
15d38 33 32 20 69 50 61 67 65 53 69 7a 65 3b 20 20 20 32 iPageSize;
15d39 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
15d3a 61 67 65 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f age-size field o
15d3b 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 f journal header
15d3c 20 2a 2f 0a 20 20 20 20 75 33 32 20 69 53 65 63 */. u32 iSec
15d3d 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20 torSize;
15d3e 20 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 2d 73 /* Sector-s
15d3f 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f 75 ize field of jou
15d40 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 0a rnal header */..
15d41 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 /* Read the
15d42 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 page-size and se
15d43 63 74 6f 72 2d 73 69 7a 65 20 6a 6f 75 72 6e 61 ctor-size journa
15d44 6c 20 68 65 61 64 65 72 20 66 69 65 6c 64 73 2e l header fields.
15d45 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 */. if( SQLI
15d46 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 TE_OK!=(rc = rea
15d47 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e d32bits(pPager->
15d48 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 30 2c jfd, iHdrOff+20,
15d49 20 26 69 53 65 63 74 6f 72 53 69 7a 65 29 29 0a &iSectorSize)).
15d4a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f || SQLITE_O
15d4b 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 K!=(rc = read32b
15d4c 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c its(pPager->jfd,
15d4d 20 69 48 64 72 4f 66 66 2b 32 34 2c 20 26 69 50 iHdrOff+24, &iP
15d4e 61 67 65 53 69 7a 65 29 29 0a 20 20 20 20 29 7b ageSize)). ){
15d4f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
15d50 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
15d51 56 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 Versions of SQLi
15d52 74 65 20 70 72 69 6f 72 20 74 6f 20 33 2e 35 2e te prior to 3.5.
15d53 38 20 73 65 74 20 74 68 65 20 70 61 67 65 2d 73 8 set the page-s
15d54 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 74 68 65 ize field of the
15d55 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 . ** journal
15d56 68 65 61 64 65 72 20 74 6f 20 7a 65 72 6f 2e 20 header to zero.
15d57 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 61 73 In this case, as
15d58 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 50 61 sume that the Pa
15d59 67 65 72 2e 70 61 67 65 53 69 7a 65 0a 20 20 20 ger.pageSize.
15d5a 20 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73 20 ** variable is
15d5b 61 6c 72 65 61 64 79 20 73 65 74 20 74 6f 20 74 already set to t
15d5c 68 65 20 63 6f 72 72 65 63 74 20 70 61 67 65 20 he correct page
15d5d 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 size.. */.
15d5e 20 69 66 28 20 69 50 61 67 65 53 69 7a 65 3d 3d if( iPageSize==
15d5f 30 20 29 7b 0a 20 20 20 20 20 20 69 50 61 67 65 0 ){. iPage
15d60 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 Size = pPager->p
15d61 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 0a ageSize;. }..
15d62 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 /* Check tha
15d63 74 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 61 t the values rea
15d64 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 2d d from the page-
15d65 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 2d size and sector-
15d66 73 69 7a 65 20 66 69 65 6c 64 73 0a 20 20 20 20 size fields.
15d67 2a 2a 20 61 72 65 20 77 69 74 68 69 6e 20 72 61 ** are within ra
15d68 6e 67 65 2e 20 54 6f 20 62 65 20 27 69 6e 20 72 nge. To be 'in r
15d69 61 6e 67 65 27 2c 20 62 6f 74 68 20 76 61 6c 75 ange', both valu
15d6a 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 61 20 es need to be a
15d6b 70 6f 77 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 power. ** of
15d6c 74 77 6f 20 67 72 65 61 74 65 72 20 74 68 61 6e two greater than
15d6d 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 35 31 32 or equal to 512
15d6e 20 6f 72 20 33 32 2c 20 61 6e 64 20 6e 6f 74 20 or 32, and not
15d6f 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 greater than the
15d70 69 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70 65 ir . ** respe
15d71 63 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74 69 ctive compile ti
15d72 6d 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69 74 me maximum limit
15d73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 s.. */. if
15d74 28 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32 20 ( iPageSize<512
15d75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d76 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3c || iSectorSize<
15d77 33 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 67 65 32. || iPage
15d78 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f Size>SQLITE_MAX_
15d79 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 53 65 PAGE_SIZE || iSe
15d7a 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 43 ctorSize>MAX_SEC
15d7b 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 7c 7c TOR_SIZE. ||
15d7c 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29 26 ((iPageSize-1)&
15d7d 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 20 20 iPageSize)!=0
15d7e 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 7a 65 || ((iSectorSize
15d7f 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a 65 29 -1)&iSectorSize)
15d80 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 !=0 . ){.
15d81 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 74 68 /* If the eith
15d82 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 er the page-size
15d83 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 or sector-size
15d84 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 in the journal-h
15d85 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 20 20 eader is .
15d86 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 65 6e ** invalid, then
15d87 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61 the process tha
15d88 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f 75 72 t wrote the jour
15d89 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 74 20 nal-header must
15d8a 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 have . ** c
15d8b 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 74 68 rashed before th
15d8c 65 20 68 65 61 64 65 72 20 77 61 73 20 73 79 6e e header was syn
15d8d 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ced. In this cas
15d8e 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20 0a e stop reading .
15d8f 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 ** the jou
15d90 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 0a rnal file here..
15d91 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 */. r
15d92 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e eturn SQLITE_DON
15d93 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a E;. }.. /*
15d94 20 55 70 64 61 74 65 20 74 68 65 20 70 61 67 65 Update the page
15d95 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 -size to match t
15d96 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 he value read fr
15d97 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 om the journal.
15d98 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74 65 . ** Use a te
15d99 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 74 stcase() macro t
15d9a 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 o make sure that
15d9b 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 malloc failure
15d9c 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20 50 within . ** P
15d9d 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 agerSetPagesize(
15d9e 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 20 20 ) is tested..
15d9f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
15da0 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 ite3PagerSetPage
15da1 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26 69 50 size(pPager, &iP
15da2 61 67 65 53 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 ageSize, -1);.
15da3 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d testcase( rc!=
15da4 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 SQLITE_OK );..
15da5 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 /* Update the
15da6 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 2d 73 assumed sector-s
15da7 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65 ize to match the
15da8 20 76 61 6c 75 65 20 75 73 65 64 20 62 79 20 0a value used by .
15da9 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 ** the proce
15daa 73 73 20 74 68 61 74 20 63 72 65 61 74 65 64 20 ss that created
15dab 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 this journal. If
15dac 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 this journal wa
15dad 73 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 s. ** created
15dae 20 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f 74 by a process ot
15daf 68 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e her than this on
15db0 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 e, then this rou
15db1 74 69 6e 65 0a 20 20 20 20 2a 2a 20 69 73 20 62 tine. ** is b
15db2 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d eing called from
15db3 20 77 69 74 68 69 6e 20 70 61 67 65 72 5f 70 6c within pager_pl
15db4 61 79 62 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f ayback(). The lo
15db5 63 61 6c 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a cal value. **
15db6 20 6f 66 20 50 61 67 65 72 2e 73 65 63 74 6f 72 of Pager.sector
15db7 53 69 7a 65 20 69 73 20 72 65 73 74 6f 72 65 64 Size is restored
15db8 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
15db9 68 61 74 20 72 6f 75 74 69 6e 65 2e 0a 20 20 20 hat routine..
15dba 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e */. pPager->
15dbb 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 69 53 65 sectorSize = iSe
15dbc 63 74 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 ctorSize;. }..
15dbd 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
15dbe 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 Off += JOURNAL_H
15dbf 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 DR_SZ(pPager);.
15dc0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a return rc;.}...
15dc1 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 /*.** Write the
15dc2 73 75 70 70 6c 69 65 64 20 6d 61 73 74 65 72 20 supplied master
15dc3 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 journal name int
15dc4 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
15dc5 6c 65 20 66 6f 72 20 70 61 67 65 72 0a 2a 2a 20 le for pager.**
15dc6 70 50 61 67 65 72 20 61 74 20 74 68 65 20 63 75 pPager at the cu
15dc7 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 20 rrent location.
15dc8 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e The master journ
15dc9 61 6c 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 al name must be
15dca 74 68 65 20 6c 61 73 74 0a 2a 2a 20 74 68 69 6e the last.** thin
15dcb 67 20 77 72 69 74 74 65 6e 20 74 6f 20 61 20 6a g written to a j
15dcc 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 ournal file. If
15dcd 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
15dce 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 full-sync mode,
15dcf 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 the.** journal f
15dd0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 ile descriptor i
15dd1 73 20 61 64 76 61 6e 63 65 64 20 74 6f 20 74 68 s advanced to th
15dd2 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 62 6f e next sector bo
15dd3 75 6e 64 61 72 79 20 62 65 66 6f 72 65 0a 2a 2a undary before.**
15dd4 20 61 6e 79 74 68 69 6e 67 20 69 73 20 77 72 69 anything is wri
15dd5 74 74 65 6e 2e 20 54 68 65 20 66 6f 72 6d 61 74 tten. The format
15dd6 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 34 is:.**.** + 4
15dd7 20 62 79 74 65 73 3a 20 50 41 47 45 52 5f 4d 4a bytes: PAGER_MJ
15dd8 5f 50 47 4e 4f 2e 0a 2a 2a 20 20 20 2b 20 4e 20 _PGNO..** + N
15dd9 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f bytes: Master jo
15dda 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 urnal filename i
15ddb 6e 20 75 74 66 2d 38 2e 0a 2a 2a 20 20 20 2b 20 n utf-8..** +
15ddc 34 20 62 79 74 65 73 3a 20 4e 20 28 6c 65 6e 67 4 bytes: N (leng
15ddd 74 68 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 th of master jou
15dde 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 20 62 79 74 rnal name in byt
15ddf 65 73 2c 20 6e 6f 20 6e 75 6c 2d 74 65 72 6d 69 es, no nul-termi
15de0 6e 61 74 6f 72 29 2e 0a 2a 2a 20 20 20 2b 20 34 nator)..** + 4
15de1 20 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a bytes: Master j
15de2 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63 ournal name chec
15de3 6b 73 75 6d 2e 0a 2a 2a 20 20 20 2b 20 38 20 62 ksum..** + 8 b
15de4 79 74 65 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61 ytes: aJournalMa
15de5 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 gic[]..**.** The
15de6 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
15de7 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 69 73 page checksum is
15de8 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 the sum of the
15de9 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d 61 73 bytes in the mas
15dea 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6e ter.** journal n
15deb 61 6d 65 2c 20 77 68 65 72 65 20 65 61 63 68 20 ame, where each
15dec 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 65 byte is interpre
15ded 74 65 64 20 61 73 20 61 20 73 69 67 6e 65 64 20 ted as a signed
15dee 38 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a 8-bit integer..*
15def 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72 20 *.** If zMaster
15df0 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 is a NULL pointe
15df1 72 20 28 6f 63 63 75 72 73 20 66 6f 72 20 61 20 r (occurs for a
15df2 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 single database
15df3 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a 2a transaction), .*
15df4 2a 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 * this call is a
15df5 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 no-op..*/.stati
15df6 63 20 69 6e 74 20 77 72 69 74 65 4d 61 73 74 65 c int writeMaste
15df7 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a rJournal(Pager *
15df8 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 pPager, const ch
15df9 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 ar *zMaster){.
15dfa 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
15dfb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15dfc 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
15dfd 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 */. int nMaster
15dfe 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
15dff 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 /* Length
15e00 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 of string zMaste
15e01 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f r */. i64 iHdrO
15e02 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ff;
15e03 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
15e04 74 20 6f 66 20 68 65 61 64 65 72 20 69 6e 20 6a t of header in j
15e05 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 ournal file */.
15e06 20 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 20 20 i64 jrnlSize;
15e07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15e08 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6a 6f 75 /* Size of jou
15e09 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 rnal file on dis
15e0a 6b 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d k */. u32 cksum
15e0b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
15e0c 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b /* Check
15e0d 73 75 6d 20 6f 66 20 73 74 72 69 6e 67 20 7a 4d sum of string zM
15e0e 61 73 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 aster */.. asse
15e0f 72 74 28 20 70 50 61 67 65 72 2d 3e 73 65 74 4d rt( pPager->setM
15e10 61 73 74 65 72 3d 3d 30 20 29 3b 0a 20 20 61 73 aster==0 );. as
15e11 73 65 72 74 28 20 21 70 61 67 65 72 55 73 65 57 sert( !pagerUseW
15e12 61 6c 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 al(pPager) );..
15e13 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 0a 20 if( !zMaster .
15e14 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 || pPager->jou
15e15 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
15e16 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
15e17 52 59 20 0a 20 20 20 7c 7c 20 70 50 61 67 65 72 RY . || pPager
15e18 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
15e19 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
15e1a 5f 4f 46 46 20 0a 20 20 29 7b 0a 20 20 20 20 72 _OFF . ){. r
15e1b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
15e1c 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 . }. pPager->s
15e1d 65 74 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 etMaster = 1;.
15e1e 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
15e1f 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 Pager->jfd) );.
15e20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
15e21 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3c 3d 20 70 >journalHdr <= p
15e22 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
15e23 66 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 f );.. /* Calcu
15e24 6c 61 74 65 20 74 68 65 20 6c 65 6e 67 74 68 20 late the length
15e25 69 6e 20 62 79 74 65 73 20 61 6e 64 20 74 68 65 in bytes and the
15e26 20 63 68 65 63 6b 73 75 6d 20 6f 66 20 7a 4d 61 checksum of zMa
15e27 73 74 65 72 20 2a 2f 0a 20 20 66 6f 72 28 6e 4d ster */. for(nM
15e28 61 73 74 65 72 3d 30 3b 20 7a 4d 61 73 74 65 72 aster=0; zMaster
15e29 5b 6e 4d 61 73 74 65 72 5d 3b 20 6e 4d 61 73 74 [nMaster]; nMast
15e2a 65 72 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d er++){. cksum
15e2b 20 2b 3d 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 += zMaster[nMas
15e2c 74 65 72 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ter];. }.. /*
15e2d 49 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 If in full-sync
15e2e 6d 6f 64 65 2c 20 61 64 76 61 6e 63 65 20 74 6f mode, advance to
15e2f 20 74 68 65 20 6e 65 78 74 20 64 69 73 6b 20 73 the next disk s
15e30 65 63 74 6f 72 20 62 65 66 6f 72 65 20 77 72 69 ector before wri
15e31 74 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61 ting. ** the ma
15e32 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
15e33 65 2e 20 54 68 69 73 20 69 73 20 69 6e 20 63 61 e. This is in ca
15e34 73 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 se the previous
15e35 70 61 67 65 20 77 72 69 74 74 65 6e 20 74 6f 0a page written to.
15e36 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c ** the journal
15e37 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
15e38 6e 20 73 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20 n synced.. */.
15e39 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c if( pPager->ful
15e3a 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 70 50 61 lSync ){. pPa
15e3b 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
15e3c 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 = journalHdrOffs
15e3d 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a et(pPager);. }.
15e3e 20 20 69 48 64 72 4f 66 66 20 3d 20 70 50 61 67 iHdrOff = pPag
15e3f 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a er->journalOff;.
15e40 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 . /* Write the
15e41 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 64 master journal d
15e42 61 74 61 20 74 6f 20 74 68 65 20 65 6e 64 20 6f ata to the end o
15e43 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
15e44 6c 65 2e 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65 le. If. ** an e
15e45 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 rror occurs, ret
15e46 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f urn the error co
15e47 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 de to the caller
15e48 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 30 20 .. */. if( (0
15e49 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33 32 != (rc = write32
15e4a 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
15e4b 2c 20 69 48 64 72 4f 66 66 2c 20 50 41 47 45 52 , iHdrOff, PAGER
15e4c 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 _MJ_PGNO(pPager)
15e4d 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 ))). || (0 !=
15e4e 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 (rc = sqlite3OsW
15e4f 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 rite(pPager->jfd
15e50 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 , zMaster, nMast
15e51 65 72 2c 20 69 48 64 72 4f 66 66 2b 34 29 29 29 er, iHdrOff+4)))
15e52 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 . || (0 != (rc
15e53 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 = write32bits(p
15e54 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 Pager->jfd, iHdr
15e55 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2c 20 6e Off+4+nMaster, n
15e56 4d 61 73 74 65 72 29 29 29 0a 20 20 20 7c 7c 20 Master))). ||
15e57 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 (0 != (rc = writ
15e58 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e e32bits(pPager->
15e59 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e jfd, iHdrOff+4+n
15e5a 4d 61 73 74 65 72 2b 34 2c 20 63 6b 73 75 6d 29 Master+4, cksum)
15e5b 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 )). || (0 != (
15e5c 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
15e5d 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ite(pPager->jfd,
15e5e 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 aJournalMagic,
15e5f 38 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 8, iHdrOff+4+nMa
15e60 73 74 65 72 2b 38 29 29 29 0a 20 20 29 7b 0a 20 ster+8))). ){.
15e61 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
15e62 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 }. pPager->jour
15e63 6e 61 6c 4f 66 66 20 2b 3d 20 28 6e 4d 61 73 74 nalOff += (nMast
15e64 65 72 2b 32 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 er+20);.. /* If
15e65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
15e66 20 70 65 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e peristent-journ
15e67 61 6c 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 al mode, then th
15e68 65 20 70 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a e physical . **
15e69 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61 journal-file ma
15e6a 79 20 65 78 74 65 6e 64 20 70 61 73 74 20 74 68 y extend past th
15e6b 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 e end of the mas
15e6c 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter-journal name
15e6d 0a 20 20 2a 2a 20 61 6e 64 20 38 20 62 79 74 65 . ** and 8 byte
15e6e 73 20 6f 66 20 6d 61 67 69 63 20 64 61 74 61 20 s of magic data
15e6f 6a 75 73 74 20 77 72 69 74 74 65 6e 20 74 6f 20 just written to
15e70 74 68 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69 the file. This i
15e71 73 20 0a 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75 s . ** dangerou
15e72 73 20 62 65 63 61 75 73 65 20 74 68 65 20 63 6f s because the co
15e73 64 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 de to rollback a
15e74 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c hot-journal fil
15e75 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 e. ** will not
15e76 62 65 20 61 62 6c 65 20 74 6f 20 66 69 6e 64 20 be able to find
15e77 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e the master-journ
15e78 61 6c 20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 al name to deter
15e79 6d 69 6e 65 20 0a 20 20 2a 2a 20 77 68 65 74 68 mine . ** wheth
15e7a 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f er or not the jo
15e7b 75 72 6e 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20 urnal is hot. .
15e7c 20 2a 2a 0a 20 20 2a 2a 20 45 61 73 69 65 73 74 **. ** Easiest
15e7d 20 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20 thing to do in
15e7e 74 68 69 73 20 73 63 65 6e 61 72 69 6f 20 69 73 this scenario is
15e7f 20 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 to truncate the
15e80 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 journal . ** f
15e81 69 6c 65 20 74 6f 20 74 68 65 20 72 65 71 75 69 ile to the requi
15e82 72 65 64 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a red size.. */ .
15e83 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
15e84 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 =(rc = sqlite3Os
15e85 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d FileSize(pPager-
15e86 3e 6a 66 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 29 >jfd, &jrnlSize)
15e87 29 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a 65 ). && jrnlSize
15e88 3e 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c >pPager->journal
15e89 4f 66 66 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 Off. ){. rc
15e8a 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
15e8b 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ate(pPager->jfd,
15e8c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
15e8d 4f 66 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Off);. }. retu
15e8e 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
15e8f 46 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74 Find a page in t
15e90 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 he hash table gi
15e91 76 65 6e 20 69 74 73 20 70 61 67 65 20 6e 75 6d ven its page num
15e92 62 65 72 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 61 ber. Return.** a
15e93 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
15e94 70 61 67 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 page or NULL if
15e95 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 the requested pa
15e96 67 65 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c ge is not .** al
15e97 72 65 61 64 79 20 69 6e 20 6d 65 6d 6f 72 79 2e ready in memory.
15e98 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 .*/.static PgHdr
15e99 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 *pager_lookup(P
15e9a 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 ager *pPager, Pg
15e9b 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 no pgno){. PgHd
15e9c 72 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 r *p;
15e9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15e9e 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f Return value */
15e9f 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 .. /* It is not
15ea0 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 20 possible for a
15ea1 63 61 6c 6c 20 74 6f 20 50 63 61 63 68 65 46 65 call to PcacheFe
15ea2 74 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74 tch() with creat
15ea3 65 46 6c 61 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a eFlag==0 to. **
15ea4 20 66 61 69 6c 2c 20 73 69 6e 63 65 20 6e 6f 20 fail, since no
15ea5 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 attempt to alloc
15ea6 61 74 65 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f ate dynamic memo
15ea7 72 79 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 2e ry will be made.
15ea8 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 . */. (void)sq
15ea9 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 lite3PcacheFetch
15eaa 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
15eab 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a , pgno, 0, &p);.
15eac 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
15ead 2a 0a 2a 2a 20 44 69 73 63 61 72 64 20 74 68 65 *.** Discard the
15eae 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 entire contents
15eaf 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 of the in-memor
15eb0 79 20 70 61 67 65 2d 63 61 63 68 65 2e 0a 2a 2f y page-cache..*/
15eb1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 .static void pag
15eb2 65 72 5f 72 65 73 65 74 28 50 61 67 65 72 20 2a er_reset(Pager *
15eb3 70 50 61 67 65 72 29 7b 0a 20 20 73 71 6c 69 74 pPager){. sqlit
15eb4 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 e3BackupRestart(
15eb5 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 pPager->pBackup)
15eb6 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 ;. sqlite3Pcach
15eb7 65 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 eClear(pPager->p
15eb8 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a PCache);.}../*.*
15eb9 2a 20 46 72 65 65 20 61 6c 6c 20 73 74 72 75 63 * Free all struc
15eba 74 75 72 65 73 20 69 6e 20 74 68 65 20 50 61 67 tures in the Pag
15ebb 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 er.aSavepoint[]
15ebc 61 72 72 61 79 20 61 6e 64 20 73 65 74 20 62 6f array and set bo
15ebd 74 68 0a 2a 2a 20 50 61 67 65 72 2e 61 53 61 76 th.** Pager.aSav
15ebe 65 70 6f 69 6e 74 20 61 6e 64 20 50 61 67 65 72 epoint and Pager
15ebf 2e 6e 53 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a .nSavepoint to z
15ec0 65 72 6f 2e 20 43 6c 6f 73 65 20 74 68 65 20 73 ero. Close the s
15ec1 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66 ub-journal.** if
15ec2 20 69 74 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 it is open and
15ec3 74 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 the pager is not
15ec4 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f in exclusive mo
15ec5 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f de..*/.static vo
15ec6 69 64 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 id releaseAllSav
15ec7 65 70 6f 69 6e 74 73 28 50 61 67 65 72 20 2a 70 epoints(Pager *p
15ec8 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 69 69 Pager){. int ii
15ec9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
15eca 2f 2a 20 49 74 65 72 61 74 6f 72 20 66 6f 72 20 /* Iterator for
15ecb 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 looping through
15ecc 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 Pager.aSavepoint
15ecd 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 */. for(ii=0;
15ece 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 ii<pPager->nSave
15ecf 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 point; ii++){.
15ed0 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
15ed1 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 estroy(pPager->a
15ed2 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 Savepoint[ii].pI
15ed3 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d nSavepoint);. }
15ed4 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e . if( !pPager->
15ed5 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c exclusiveMode ||
15ed6 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 sqlite3IsMemJou
15ed7 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 73 6a 66 rnal(pPager->sjf
15ed8 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 d) ){. sqlite
15ed9 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
15eda 3e 73 6a 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71 >sjfd);. }. sq
15edb 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 lite3_free(pPage
15edc 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 29 3b 0a r->aSavepoint);.
15edd 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 pPager->aSavep
15ede 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 oint = 0;. pPag
15edf 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d er->nSavepoint =
15ee0 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 0;. pPager->nS
15ee1 75 62 52 65 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a ubRec = 0;.}../*
15ee2 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 69 74 20 .** Set the bit
15ee3 6e 75 6d 62 65 72 20 70 67 6e 6f 20 69 6e 20 74 number pgno in t
15ee4 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e he PagerSavepoin
15ee5 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 0a t.pInSavepoint .
15ee6 2a 2a 20 62 69 74 76 65 63 73 20 6f 66 20 61 6c ** bitvecs of al
15ee7 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 l open savepoint
15ee8 73 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 s. Return SQLITE
15ee9 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 _OK if successfu
15eea 6c 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e l.** or SQLITE_N
15eeb 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63 OMEM if a malloc
15eec 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e failure occurs.
15eed 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
15eee 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 ddToSavepointBit
15eef 76 65 63 73 28 50 61 67 65 72 20 2a 70 50 61 67 vecs(Pager *pPag
15ef0 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a er, Pgno pgno){.
15ef1 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 int ii;
15ef2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
15ef3 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
15ef4 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
15ef5 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 _OK; /* Re
15ef6 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 sult code */..
15ef7 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 for(ii=0; ii<pPa
15ef8 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b ger->nSavepoint;
15ef9 20 69 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 ii++){. Page
15efa 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 rSavepoint *p =
15efb 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f &pPager->aSavepo
15efc 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 int[ii];. if(
15efd 20 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 pgno<=p->nOrig
15efe 29 7b 0a 20 20 20 20 20 20 72 63 20 7c 3d 20 73 ){. rc |= s
15eff 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
15f00 70 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c p->pInSavepoint,
15f01 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 74 65 pgno);. te
15f02 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 stcase( rc==SQLI
15f03 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 TE_NOMEM );.
15f04 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
15f05 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 LITE_OK || rc==S
15f06 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
15f07 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
15f08 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
15f09 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
15f0a 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 70 a no-op if the p
15f0b 61 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c 75 ager is in exclu
15f0c 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 6e 6f sive mode and no
15f0d 74 0a 2a 2a 20 69 6e 20 74 68 65 20 45 52 52 4f t.** in the ERRO
15f0e 52 20 73 74 61 74 65 2e 20 4f 74 68 65 72 77 69 R state. Otherwi
15f0f 73 65 2c 20 69 74 20 73 77 69 74 63 68 65 73 20 se, it switches
15f10 74 68 65 20 70 61 67 65 72 20 74 6f 20 50 41 47 the pager to PAG
15f11 45 52 5f 4f 50 45 4e 0a 2a 2a 20 73 74 61 74 65 ER_OPEN.** state
15f12 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
15f13 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 65 ager is not in e
15f14 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 xclusive-access
15f15 6d 6f 64 65 2c 20 74 68 65 20 64 61 74 61 62 61 mode, the databa
15f16 73 65 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 6f se file is.** co
15f17 6d 70 6c 65 74 65 6c 79 20 75 6e 6c 6f 63 6b 65 mpletely unlocke
15f18 64 2e 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 d. If the file i
15f19 73 20 75 6e 6c 6f 63 6b 65 64 20 61 6e 64 20 74 s unlocked and t
15f1a 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 he file-system d
15f1b 6f 65 73 0a 2a 2a 20 6e 6f 74 20 65 78 68 69 62 oes.** not exhib
15f1c 69 74 20 74 68 65 20 55 4e 44 45 4c 45 54 41 42 it the UNDELETAB
15f1d 4c 45 5f 57 48 45 4e 5f 4f 50 45 4e 20 70 72 6f LE_WHEN_OPEN pro
15f1e 70 65 72 74 79 2c 20 74 68 65 20 6a 6f 75 72 6e perty, the journ
15f1f 61 6c 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 6c al file is.** cl
15f20 6f 73 65 64 20 28 69 66 20 69 74 20 69 73 20 6f osed (if it is o
15f21 70 65 6e 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 pen)..**.** If t
15f22 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 45 he pager is in E
15f23 52 52 4f 52 20 73 74 61 74 65 20 77 68 65 6e 20 RROR state when
15f24 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
15f25 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 0a 2a 2a called, the .**
15f26 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
15f27 20 70 61 67 65 72 20 63 61 63 68 65 20 61 72 65 pager cache are
15f28 20 64 69 73 63 61 72 64 65 64 20 62 65 66 6f 72 discarded befor
15f29 65 20 73 77 69 74 63 68 69 6e 67 20 62 61 63 6b e switching back
15f2a 20 74 6f 20 0a 2a 2a 20 74 68 65 20 4f 50 45 4e to .** the OPEN
15f2b 20 73 74 61 74 65 2e 20 52 65 67 61 72 64 6c 65 state. Regardle
15f2c 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 74 68 ss of whether th
15f2d 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 78 e pager is in ex
15f2e 63 6c 75 73 69 76 65 2d 6d 6f 64 65 0a 2a 2a 20 clusive-mode.**
15f2f 6f 72 20 6e 6f 74 2c 20 61 6e 79 20 6a 6f 75 72 or not, any jour
15f30 6e 61 6c 20 66 69 6c 65 20 6c 65 66 74 20 69 6e nal file left in
15f31 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d the file-system
15f32 20 77 69 6c 6c 20 62 65 20 74 72 65 61 74 65 64 will be treated
15f33 0a 2a 2a 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 .** as a hot-jou
15f34 72 6e 61 6c 20 61 6e 64 20 72 6f 6c 6c 65 64 20 rnal and rolled
15f35 62 61 63 6b 20 74 68 65 20 6e 65 78 74 20 74 69 back the next ti
15f36 6d 65 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 me a read-transa
15f37 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 70 65 6e ction.** is open
15f38 65 64 20 28 62 79 20 74 68 69 73 20 6f 72 20 62 ed (by this or b
15f39 79 20 61 6e 79 20 6f 74 68 65 72 20 63 6f 6e 6e y any other conn
15f3a 65 63 74 69 6f 6e 29 2e 0a 2a 2f 0a 73 74 61 74 ection)..*/.stat
15f3b 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 75 6e ic void pager_un
15f3c 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 lock(Pager *pPag
15f3d 65 72 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 er){.. assert(
15f3e 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d pPager->eState==
15f3f 50 41 47 45 52 5f 52 45 41 44 45 52 20 0a 20 20 PAGER_READER .
15f40 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e || pPager->
15f41 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 eState==PAGER_OP
15f42 45 4e 20 0a 20 20 20 20 20 20 20 7c 7c 20 70 50 EN . || pP
15f43 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 ager->eState==PA
15f44 47 45 52 5f 45 52 52 4f 52 20 0a 20 20 29 3b 0a GER_ERROR . );.
15f45 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 . sqlite3Bitvec
15f46 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e Destroy(pPager->
15f47 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 70 pInJournal);. p
15f48 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
15f49 6c 20 3d 20 30 3b 0a 20 20 72 65 6c 65 61 73 65 l = 0;. release
15f4a 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 AllSavepoints(pP
15f4b 61 67 65 72 29 3b 0a 0a 20 20 69 66 28 20 70 61 ager);.. if( pa
15f4c 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 gerUseWal(pPager
15f4d 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 ) ){. assert(
15f4e 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d !isOpen(pPager-
15f4f 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20 73 71 6c >jfd) );. sql
15f50 69 74 65 33 57 61 6c 45 6e 64 52 65 61 64 54 72 ite3WalEndReadTr
15f51 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 ansaction(pPager
15f52 2d 3e 70 57 61 6c 29 3b 0a 20 20 20 20 70 50 61 ->pWal);. pPa
15f53 67 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 ger->eState = PA
15f54 47 45 52 5f 4f 50 45 4e 3b 0a 20 20 7d 65 6c 73 GER_OPEN;. }els
15f55 65 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 e if( !pPager->e
15f56 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a xclusiveMode ){.
15f57 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 int rc;
15f58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15f59 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 /* Error code
15f5a 72 65 74 75 72 6e 65 64 20 62 79 20 70 61 67 65 returned by page
15f5b 72 55 6e 6c 6f 63 6b 44 62 28 29 20 2a 2f 0a 20 rUnlockDb() */.
15f5c 20 20 20 69 6e 74 20 69 44 63 20 3d 20 69 73 4f int iDc = isO
15f5d 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 3f pen(pPager->fd)?
15f5e 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 sqlite3OsDeviceC
15f5f 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 haracteristics(p
15f60 50 61 67 65 72 2d 3e 66 64 29 3a 30 3b 0a 0a 20 Pager->fd):0;..
15f61 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 70 65 /* If the ope
15f62 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 75 rating system su
15f63 70 70 6f 72 74 20 64 65 6c 65 74 69 6f 6e 20 6f pport deletion o
15f64 66 20 6f 70 65 6e 20 66 69 6c 65 73 2c 20 74 68 f open files, th
15f65 65 6e 0a 20 20 20 20 2a 2a 20 63 6c 6f 73 65 20 en. ** close
15f66 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
15f67 20 77 68 65 6e 20 64 72 6f 70 70 69 6e 67 20 74 when dropping t
15f68 68 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b he database lock
15f69 2e 20 20 4f 74 68 65 72 77 69 73 65 0a 20 20 20 . Otherwise.
15f6a 20 2a 2a 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e ** another conn
15f6b 65 63 74 69 6f 6e 20 77 69 74 68 20 6a 6f 75 72 ection with jour
15f6c 6e 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65 74 65 20 nal_mode=delete
15f6d 6d 69 67 68 74 20 64 65 6c 65 74 65 20 74 68 65 might delete the
15f6e 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 6f 75 74 file. ** out
15f6f 20 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a from under us..
15f70 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 */. asser
15f71 74 28 20 28 50 41 47 45 52 5f 4a 4f 55 52 4e 41 t( (PAGER_JOURNA
15f72 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 26 LMODE_MEMORY &
15f73 20 35 29 21 3d 31 20 29 3b 0a 20 20 20 20 61 73 5)!=1 );. as
15f74 73 65 72 74 28 20 28 50 41 47 45 52 5f 4a 4f 55 sert( (PAGER_JOU
15f75 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20 RNALMODE_OFF
15f76 20 20 26 20 35 29 21 3d 31 20 29 3b 0a 20 20 20 & 5)!=1 );.
15f77 20 61 73 73 65 72 74 28 20 28 50 41 47 45 52 5f assert( (PAGER_
15f78 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 JOURNALMODE_WAL
15f79 20 20 20 20 20 26 20 35 29 21 3d 31 20 29 3b 0a & 5)!=1 );.
15f7a 20 20 20 20 61 73 73 65 72 74 28 20 28 50 41 47 assert( (PAG
15f7b 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 ER_JOURNALMODE_D
15f7c 45 4c 45 54 45 20 20 20 26 20 35 29 21 3d 31 20 ELETE & 5)!=1
15f7d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
15f7e 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
15f7f 45 5f 54 52 55 4e 43 41 54 45 20 26 20 35 29 3d E_TRUNCATE & 5)=
15f80 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =1 );. assert
15f81 28 20 28 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ( (PAGER_JOURNAL
15f82 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 20 26 20 MODE_PERSIST &
15f83 35 29 3d 3d 31 20 29 3b 0a 20 20 20 20 69 66 28 5)==1 );. if(
15f84 20 30 3d 3d 28 69 44 63 20 26 20 53 51 4c 49 54 0==(iDc & SQLIT
15f85 45 5f 49 4f 43 41 50 5f 55 4e 44 45 4c 45 54 41 E_IOCAP_UNDELETA
15f86 42 4c 45 5f 57 48 45 4e 5f 4f 50 45 4e 29 0a 20 BLE_WHEN_OPEN).
15f87 20 20 20 20 7c 7c 20 31 21 3d 28 70 50 61 67 65 || 1!=(pPage
15f88 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 26 r->journalMode &
15f89 20 35 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 5). ){.
15f8a 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
15f8b 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 pPager->jfd);.
15f8c 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 }.. /* If t
15f8d 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 he pager is in t
15f8e 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 20 61 he ERROR state a
15f8f 6e 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 75 nd the call to u
15f90 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 nlock the databa
15f91 73 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 66 se. ** file f
15f92 61 69 6c 73 2c 20 73 65 74 20 74 68 65 20 63 75 ails, set the cu
15f93 72 72 65 6e 74 20 6c 6f 63 6b 20 74 6f 20 55 4e rrent lock to UN
15f94 4b 4e 4f 57 4e 5f 4c 4f 43 4b 2e 20 53 65 65 20 KNOWN_LOCK. See
15f95 74 68 65 20 63 6f 6d 6d 65 6e 74 0a 20 20 20 20 the comment.
15f96 2a 2a 20 61 62 6f 76 65 20 74 68 65 20 23 64 65 ** above the #de
15f97 66 69 6e 65 20 66 6f 72 20 55 4e 4b 4e 4f 57 4e fine for UNKNOWN
15f98 5f 4c 4f 43 4b 20 66 6f 72 20 61 6e 20 65 78 70 _LOCK for an exp
15f99 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 77 68 79 20 lanation of why
15f9a 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 6e this. ** is n
15f9b 65 63 65 73 73 61 72 79 2e 0a 20 20 20 20 2a 2f ecessary.. */
15f9c 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 55 . rc = pagerU
15f9d 6e 6c 6f 63 6b 44 62 28 70 50 61 67 65 72 2c 20 nlockDb(pPager,
15f9e 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 NO_LOCK);. if
15f9f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
15fa0 26 26 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 && pPager->eStat
15fa1 65 3d 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 29 e==PAGER_ERROR )
15fa2 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e {. pPager->
15fa3 65 4c 6f 63 6b 20 3d 20 55 4e 4b 4e 4f 57 4e 5f eLock = UNKNOWN_
15fa4 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 LOCK;. }..
15fa5 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 73 74 /* The pager st
15fa6 61 74 65 20 6d 61 79 20 62 65 20 63 68 61 6e 67 ate may be chang
15fa7 65 64 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 52 ed from PAGER_ER
15fa8 52 4f 52 20 74 6f 20 50 41 47 45 52 5f 4f 50 45 ROR to PAGER_OPE
15fa9 4e 20 68 65 72 65 0a 20 20 20 20 2a 2a 20 77 69 N here. ** wi
15faa 74 68 6f 75 74 20 63 6c 65 61 72 69 6e 67 20 74 thout clearing t
15fab 68 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 54 he error code. T
15fac 68 69 73 20 69 73 20 69 6e 74 65 6e 74 69 6f 6e his is intention
15fad 61 6c 20 2d 20 74 68 65 20 65 72 72 6f 72 0a 20 al - the error.
15fae 20 20 20 2a 2a 20 63 6f 64 65 20 69 73 20 63 6c ** code is cl
15faf 65 61 72 65 64 20 61 6e 64 20 74 68 65 20 63 61 eared and the ca
15fb0 63 68 65 20 72 65 73 65 74 20 69 6e 20 74 68 65 che reset in the
15fb1 20 62 6c 6f 63 6b 20 62 65 6c 6f 77 2e 0a 20 20 block below..
15fb2 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
15fb3 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
15fb4 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 61 || pPager->eSta
15fb5 74 65 21 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 te!=PAGER_ERROR
15fb6 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 );. pPager->c
15fb7 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d hangeCountDone =
15fb8 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 0;. pPager->
15fb9 65 53 74 61 74 65 20 3d 20 50 41 47 45 52 5f 4f eState = PAGER_O
15fba 50 45 4e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 PEN;. }.. /* I
15fbb 66 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 f Pager.errCode
15fbc 69 73 20 73 65 74 2c 20 74 68 65 20 63 6f 6e 74 is set, the cont
15fbd 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 ents of the page
15fbe 72 20 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 r cache cannot b
15fbf 65 0a 20 20 2a 2a 20 74 72 75 73 74 65 64 2e 20 e. ** trusted.
15fc0 4e 6f 77 20 74 68 61 74 20 74 68 65 72 65 20 61 Now that there a
15fc1 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e re no outstandin
15fc2 67 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 g references to
15fc3 74 68 65 20 70 61 67 65 72 2c 0a 20 20 2a 2a 20 the pager,. **
15fc4 69 74 20 63 61 6e 20 73 61 66 65 6c 79 20 6d 6f it can safely mo
15fc5 76 65 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 ve back to PAGER
15fc6 5f 4f 50 45 4e 20 73 74 61 74 65 2e 20 54 68 69 _OPEN state. Thi
15fc7 73 20 68 61 70 70 65 6e 73 20 69 6e 20 62 6f 74 s happens in bot
15fc8 68 0a 20 20 2a 2a 20 6e 6f 72 6d 61 6c 20 61 6e h. ** normal an
15fc9 64 20 65 78 63 6c 75 73 69 76 65 2d 6c 6f 63 6b d exclusive-lock
15fca 69 6e 67 20 6d 6f 64 65 2e 0a 20 20 2a 2f 0a 20 ing mode.. */.
15fcb 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 if( pPager->err
15fcc 43 6f 64 65 20 29 7b 0a 20 20 20 20 61 73 73 65 Code ){. asse
15fcd 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 rt( !MEMDB );.
15fce 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 pager_reset(pP
15fcf 61 67 65 72 29 3b 0a 20 20 20 20 70 50 61 67 65 ager);. pPage
15fd0 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f r->changeCountDo
15fd1 6e 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d ne = pPager->tem
15fd2 70 46 69 6c 65 3b 0a 20 20 20 20 70 50 61 67 65 pFile;. pPage
15fd3 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 47 45 r->eState = PAGE
15fd4 52 5f 4f 50 45 4e 3b 0a 20 20 20 20 70 50 61 67 R_OPEN;. pPag
15fd5 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 er->errCode = SQ
15fd6 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
15fd7 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
15fd8 66 66 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 ff = 0;. pPager
15fd9 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 ->journalHdr = 0
15fda 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d ;. pPager->setM
15fdb 61 73 74 65 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a aster = 0;.}../*
15fdc 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
15fdd 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e n is called when
15fde 65 76 65 72 20 61 6e 20 49 4f 45 52 52 20 6f 72 ever an IOERR or
15fdf 20 46 55 4c 4c 20 65 72 72 6f 72 20 74 68 61 74 FULL error that
15fe0 20 72 65 71 75 69 72 65 73 0a 2a 2a 20 74 68 65 requires.** the
15fe1 20 70 61 67 65 72 20 74 6f 20 74 72 61 6e 73 69 pager to transi
15fe2 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 45 52 tion into the ER
15fe3 52 4f 52 20 73 74 61 74 65 20 6d 61 79 20 61 68 ROR state may ah
15fe4 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 ve occurred..**
15fe5 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 The first argume
15fe6 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 nt is a pointer
15fe7 74 6f 20 74 68 65 20 70 61 67 65 72 20 73 74 72 to the pager str
15fe8 75 63 74 75 72 65 2c 20 74 68 65 20 73 65 63 6f ucture, the seco
15fe9 6e 64 20 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 nd .** the error
15fea 2d 63 6f 64 65 20 61 62 6f 75 74 20 74 6f 20 62 -code about to b
15feb 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 20 e returned by a
15fec 70 61 67 65 72 20 41 50 49 20 66 75 6e 63 74 69 pager API functi
15fed 6f 6e 2e 20 54 68 65 20 0a 2a 2a 20 76 61 6c 75 on. The .** valu
15fee 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 20 e returned is a
15fef 63 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 6f copy of the seco
15ff0 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 nd argument to t
15ff1 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a his function. .*
15ff2 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 63 6f *.** If the seco
15ff3 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 nd argument is S
15ff4 51 4c 49 54 45 5f 46 55 4c 4c 2c 20 53 51 4c 49 QLITE_FULL, SQLI
15ff5 54 45 5f 49 4f 45 52 52 20 6f 72 20 6f 6e 65 20 TE_IOERR or one
15ff6 6f 66 20 74 68 65 0a 2a 2a 20 49 4f 45 52 52 20 of the.** IOERR
15ff7 73 75 62 2d 63 6f 64 65 73 2c 20 74 68 65 20 70 sub-codes, the p
15ff8 61 67 65 72 20 65 6e 74 65 72 73 20 74 68 65 20 ager enters the
15ff9 45 52 52 4f 52 20 73 74 61 74 65 20 61 6e 64 20 ERROR state and
15ffa 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a the error code.*
15ffb 2a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 50 * is stored in P
15ffc 61 67 65 72 2e 65 72 72 43 6f 64 65 2e 20 57 68 ager.errCode. Wh
15ffd 69 6c 65 20 74 68 65 20 70 61 67 65 72 20 72 65 ile the pager re
15ffe 6d 61 69 6e 73 20 69 6e 20 74 68 65 20 45 52 52 mains in the ERR
15fff 4f 52 20 73 74 61 74 65 2c 0a 2a 2a 20 61 6c 6c OR state,.** all
16000 20 6d 61 6a 6f 72 20 41 50 49 20 63 61 6c 6c 73 major API calls
16001 20 6f 6e 20 74 68 65 20 50 61 67 65 72 20 77 69 on the Pager wi
16002 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72 ll immediately r
16003 65 74 75 72 6e 20 50 61 67 65 72 2e 65 72 72 43 eturn Pager.errC
16004 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 ode..**.** The E
16005 52 52 4f 52 20 73 74 61 74 65 20 69 6e 64 69 63 RROR state indic
16006 61 74 65 73 20 74 68 61 74 20 74 68 65 20 63 6f ates that the co
16007 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
16008 67 65 72 2d 63 61 63 68 65 20 0a 2a 2a 20 63 61 ger-cache .** ca
16009 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e nnot be trusted.
1600a 20 54 68 69 73 20 73 74 61 74 65 20 63 61 6e 20 This state can
1600b 62 65 20 63 6c 65 61 72 65 64 20 62 79 20 63 6f be cleared by co
1600c 6d 70 6c 65 74 65 6c 79 20 64 69 73 63 61 72 64 mpletely discard
1600d 69 6e 67 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 ing .** the cont
1600e 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 ents of the page
1600f 72 2d 63 61 63 68 65 2e 20 49 66 20 61 20 74 72 r-cache. If a tr
16010 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 61 63 ansaction was ac
16011 74 69 76 65 20 77 68 65 6e 0a 2a 2a 20 74 68 65 tive when.** the
16012 20 70 65 72 73 69 73 74 65 6e 74 20 65 72 72 6f persistent erro
16013 72 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e r occurred, then
16014 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
16015 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65 64 0a 2a urnal may need.*
16016 2a 20 74 6f 20 62 65 20 72 65 70 6c 61 79 65 64 * to be replayed
16017 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 to restore the
16018 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
16019 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 61 database file (a
1601a 73 20 69 66 0a 2a 2a 20 69 74 20 77 65 72 65 20 s if.** it were
1601b 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a a hot-journal)..
1601c 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
1601d 67 65 72 5f 65 72 72 6f 72 28 50 61 67 65 72 20 ger_error(Pager
1601e 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 72 63 29 *pPager, int rc)
1601f 7b 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 72 63 {. int rc2 = rc
16020 20 26 20 30 78 66 66 3b 0a 20 20 61 73 73 65 72 & 0xff;. asser
16021 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
16022 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 || !MEMDB );.
16023 61 73 73 65 72 74 28 0a 20 20 20 20 20 20 20 70 assert(. p
16024 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d Pager->errCode==
16025 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 SQLITE_FULL ||.
16026 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 pPager->er
16027 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b rCode==SQLITE_OK
16028 20 7c 7c 0a 20 20 20 20 20 20 20 28 70 50 61 67 ||. (pPag
16029 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 20 30 78 er->errCode & 0x
1602a 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 ff)==SQLITE_IOER
1602b 52 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 32 R. );. if( rc2
1602c 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c ==SQLITE_FULL ||
1602d 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 rc2==SQLITE_IOE
1602e 52 52 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 RR ){. pPager
1602f 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a ->errCode = rc;.
16030 20 20 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 pPager->eSta
16031 74 65 20 3d 20 50 41 47 45 52 5f 45 52 52 4f 52 te = PAGER_ERROR
16032 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
16033 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
16034 20 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20 routine ends a
16035 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74 transaction. A t
16036 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 75 73 ransaction is us
16037 75 61 6c 6c 79 20 65 6e 64 65 64 20 62 79 20 0a ually ended by .
16038 2a 2a 20 65 69 74 68 65 72 20 61 20 43 4f 4d 4d ** either a COMM
16039 49 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b IT or a ROLLBACK
1603a 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 operation. This
1603b 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 62 65 20 routine may be
1603c 63 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72 called .** after
1603d 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68 rollback of a h
1603e 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69 ot-journal, or i
1603f 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
16040 73 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a s while opening.
16041 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ** the journal f
16042 69 6c 65 20 6f 72 20 77 72 69 74 69 6e 67 20 74 ile or writing t
16043 68 65 20 76 65 72 79 20 66 69 72 73 74 20 6a 6f he very first jo
16044 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 6f 66 20 urnal-header of
16045 61 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 72 a.** database tr
16046 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a ansaction..** .*
16047 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
16048 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 69 s never called i
16049 6e 20 50 41 47 45 52 5f 45 52 52 4f 52 20 73 74 n PAGER_ERROR st
1604a 61 74 65 2e 20 49 66 20 69 74 20 69 73 20 63 61 ate. If it is ca
1604b 6c 6c 65 64 0a 2a 2a 20 69 6e 20 50 41 47 45 52 lled.** in PAGER
1604c 5f 4e 4f 4e 45 20 6f 72 20 50 41 47 45 52 5f 53 _NONE or PAGER_S
1604d 48 41 52 45 44 20 73 74 61 74 65 20 61 6e 64 20 HARED state and
1604e 74 68 65 20 6c 6f 63 6b 20 68 65 6c 64 20 69 73 the lock held is
1604f 20 6c 65 73 73 0a 2a 2a 20 65 78 63 6c 75 73 69 less.** exclusi
16050 76 65 20 74 68 61 6e 20 61 20 52 45 53 45 52 56 ve than a RESERV
16051 45 44 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 61 ED lock, it is a
16052 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 4f 74 no-op..**.** Ot
16053 68 65 72 77 69 73 65 2c 20 61 6e 79 20 61 63 74 herwise, any act
16054 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 ive savepoints a
16055 72 65 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a re released..**.
16056 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 ** If the journa
16057 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 2c 20 l file is open,
16058 74 68 65 6e 20 69 74 20 69 73 20 22 66 69 6e 61 then it is "fina
16059 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20 61 20 6a lized". Once a j
1605a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 6c 65 20 ournal .** file
1605b 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a has been finaliz
1605c 65 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 ed it is not pos
1605d 73 69 62 6c 65 20 74 6f 20 75 73 65 20 69 74 20 sible to use it
1605e 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 20 0a to roll back a .
1605f 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ** transaction.
16060 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 65 20 63 Nor will it be c
16061 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 onsidered to be
16062 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 79 a hot-journal by
16063 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61 6e 79 20 this.** or any
16064 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 other database c
16065 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61 63 74 onnection. Exact
16066 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e 61 6c ly how a journal
16067 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 0a 2a 2a is finalized.**
16068 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 depends on whet
16069 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 70 her or not the p
1606a 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 ager is running
1606b 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 in exclusive mod
1606c 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 75 72 e and.** the cur
1606d 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 rent journal-mod
1606e 65 20 28 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c e (Pager.journal
1606f 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20 61 73 20 Mode value), as
16070 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
16071 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 journalMode==ME
16072 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 MORY.** Jour
16073 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 nal file descrip
16074 74 6f 72 20 69 73 20 73 69 6d 70 6c 79 20 63 6c tor is simply cl
16075 6f 73 65 64 2e 20 54 68 69 73 20 64 65 73 74 72 osed. This destr
16076 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20 20 20 69 oys an .** i
16077 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c n-memory journal
16078 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 ..**.** journa
16079 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 54 45 0a lMode==TRUNCATE.
1607a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66 ** Journal f
1607b 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64 ile is truncated
1607c 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 to zero bytes i
1607d 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 n size..**.**
1607e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52 journalMode==PER
1607f 53 49 53 54 0a 2a 2a 20 20 20 20 20 54 68 65 20 SIST.** The
16080 66 69 72 73 74 20 32 38 20 62 79 74 65 73 20 6f first 28 bytes o
16081 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
16082 6c 65 20 61 72 65 20 7a 65 72 6f 65 64 2e 20 54 le are zeroed. T
16083 68 69 73 20 69 6e 76 61 6c 69 64 61 74 65 73 0a his invalidates.
16084 2a 2a 20 20 20 20 20 74 68 65 20 66 69 72 73 74 ** the first
16085 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
16086 69 6e 20 74 68 65 20 66 69 6c 65 2c 20 61 6e 64 in the file, and
16087 20 68 65 6e 63 65 20 74 68 65 20 65 6e 74 69 72 hence the entir
16088 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 e journal.**
16089 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61 6c 69 file. An invali
1608a 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 d journal file c
1608b 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20 annot be rolled
1608c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f back..**.** jo
1608d 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c 45 54 urnalMode==DELET
1608e 45 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a 6f 75 E.** The jou
1608f 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f rnal file is clo
16090 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 64 20 sed and deleted
16091 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44 using sqlite3OsD
16092 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 elete()..**.**
16093 20 20 20 49 66 20 74 68 65 20 70 61 67 65 72 20 If the pager
16094 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 is running in ex
16095 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 clusive mode, th
16096 69 73 20 6d 65 74 68 6f 64 20 6f 66 20 66 69 6e is method of fin
16097 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 20 20 74 alizing.** t
16098 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
16099 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e 20 49 is never used. I
1609a 6e 73 74 65 61 64 2c 20 69 66 20 74 68 65 20 6a nstead, if the j
1609b 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73 0a 2a 2a ournalMode is.**
1609c 20 20 20 20 20 44 45 4c 45 54 45 20 61 6e 64 20 DELETE and
1609d 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
1609e 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 exclusive mode,
1609f 74 68 65 20 6d 65 74 68 6f 64 20 64 65 73 63 72 the method descr
160a0 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a 20 20 20 ibed under.**
160a1 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 journalMode==P
160a2 45 52 53 49 53 54 20 69 73 20 75 73 65 64 20 69 ERSIST is used i
160a3 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 66 nstead..**.** Af
160a4 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ter the journal
160a5 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c 20 74 68 is finalized, th
160a6 65 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74 6f e pager moves to
160a7 20 50 41 47 45 52 5f 52 45 41 44 45 52 20 73 74 PAGER_READER st
160a8 61 74 65 2e 0a 2a 2a 20 49 66 20 72 75 6e 6e 69 ate..** If runni
160a9 6e 67 20 69 6e 20 6e 6f 6e 2d 65 78 63 6c 75 73 ng in non-exclus
160aa 69 76 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 6f 64 ive rollback mod
160ab 65 2c 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 e, the lock on t
160ac 68 65 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20 64 he file is .** d
160ad 6f 77 6e 67 72 61 64 65 64 20 74 6f 20 61 20 53 owngraded to a S
160ae 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a HARED_LOCK..**.*
160af 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 * SQLITE_OK is r
160b0 65 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 65 72 eturned if no er
160b1 72 6f 72 20 6f 63 63 75 72 73 2e 20 49 66 20 61 ror occurs. If a
160b2 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 n error occurs d
160b3 75 72 69 6e 67 0a 2a 2a 20 61 6e 79 20 6f 66 20 uring.** any of
160b4 74 68 65 20 49 4f 20 6f 70 65 72 61 74 69 6f 6e the IO operation
160b5 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 s to finalize th
160b6 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f e journal file o
160b7 72 20 75 6e 6c 6f 63 6b 20 74 68 65 0a 2a 2a 20 r unlock the.**
160b8 64 61 74 61 62 61 73 65 20 74 68 65 6e 20 74 68 database then th
160b9 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 e IO error code
160ba 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 is returned to t
160bb 68 65 20 75 73 65 72 2e 20 49 66 20 74 68 65 20 he user. If the
160bc 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f .** operation to
160bd 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f finalize the jo
160be 75 72 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c 73 urnal file fails
160bf 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 64 65 20 , then the code
160c0 73 74 69 6c 6c 0a 2a 2a 20 74 72 69 65 73 20 74 still.** tries t
160c1 6f 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 o unlock the dat
160c2 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 6e 6f abase file if no
160c3 74 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d t in exclusive m
160c4 6f 64 65 2e 20 49 66 20 74 68 65 0a 2a 2a 20 75 ode. If the.** u
160c5 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 nlock operation
160c6 66 61 69 6c 73 20 61 73 20 77 65 6c 6c 2c 20 74 fails as well, t
160c7 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 65 72 hen the first er
160c8 72 6f 72 20 63 6f 64 65 20 72 65 6c 61 74 65 64 ror code related
160c9 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 .** to the first
160ca 20 65 72 72 6f 72 20 65 6e 63 6f 75 6e 74 65 72 error encounter
160cb 65 64 20 28 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ed (the journal
160cc 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f 6e 65 finalization one
160cd 29 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 ) is.** returned
160ce 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
160cf 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 pager_end_transa
160d0 63 74 69 6f 6e 28 50 61 67 65 72 20 2a 70 50 61 ction(Pager *pPa
160d1 67 65 72 2c 20 69 6e 74 20 68 61 73 4d 61 73 74 ger, int hasMast
160d2 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 er){. int rc =
160d3 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
160d4 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 72 /* Error code fr
160d5 6f 6d 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 6c om journal final
160d6 69 7a 61 74 69 6f 6e 20 6f 70 65 72 61 74 69 6f ization operatio
160d7 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 32 20 3d n */. int rc2 =
160d8 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
160d9 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 72 /* Error code fr
160da 6f 6d 20 64 62 20 66 69 6c 65 20 75 6e 6c 6f 63 om db file unloc
160db 6b 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 0a k operation */..
160dc 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 /* Do nothing
160dd 69 66 20 74 68 65 20 70 61 67 65 72 20 64 6f 65 if the pager doe
160de 73 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 6f 70 s not have an op
160df 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 en write transac
160e0 74 69 6f 6e 0a 20 20 2a 2a 20 6f 72 20 61 74 20 tion. ** or at
160e1 6c 65 61 73 74 20 61 20 52 45 53 45 52 56 45 44 least a RESERVED
160e2 20 6c 6f 63 6b 2e 20 54 68 69 73 20 66 75 6e 63 lock. This func
160e3 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c tion may be call
160e4 65 64 20 77 68 65 6e 20 74 68 65 72 65 0a 20 20 ed when there.
160e5 2a 2a 20 69 73 20 6e 6f 20 77 72 69 74 65 2d 74 ** is no write-t
160e6 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76 ransaction activ
160e7 65 20 62 75 74 20 61 20 52 45 53 45 52 56 45 44 e but a RESERVED
160e8 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b or greater lock
160e9 20 69 73 0a 20 20 2a 2a 20 68 65 6c 64 20 75 6e is. ** held un
160ea 64 65 72 20 74 77 6f 20 63 69 72 63 75 6d 73 74 der two circumst
160eb 61 6e 63 65 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a ances:. **. **
160ec 20 20 20 31 2e 20 41 66 74 65 72 20 61 20 73 75 1. After a su
160ed 63 63 65 73 73 66 75 6c 20 68 6f 74 2d 6a 6f 75 ccessful hot-jou
160ee 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 rnal rollback, i
160ef 74 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 t is called with
160f0 0a 20 20 2a 2a 20 20 20 20 20 20 65 53 74 61 74 . ** eStat
160f1 65 3d 3d 50 41 47 45 52 5f 4e 4f 4e 45 20 61 6e e==PAGER_NONE an
160f2 64 20 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 d eLock==EXCLUSI
160f3 56 45 5f 4c 4f 43 4b 2e 0a 20 20 2a 2a 0a 20 20 VE_LOCK.. **.
160f4 2a 2a 20 20 20 32 2e 20 49 66 20 61 20 63 6f 6e ** 2. If a con
160f5 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 6c 6f 63 nection with loc
160f6 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78 63 6c 75 73 king_mode=exclus
160f7 69 76 65 20 68 6f 6c 64 69 6e 67 20 61 6e 20 45 ive holding an E
160f8 58 43 4c 55 53 49 56 45 20 0a 20 20 2a 2a 20 20 XCLUSIVE . **
160f9 20 20 20 20 6c 6f 63 6b 20 73 77 69 74 63 68 65 lock switche
160fa 73 20 62 61 63 6b 20 74 6f 20 6c 6f 63 6b 69 6e s back to lockin
160fb 67 5f 6d 6f 64 65 3d 6e 6f 72 6d 61 6c 20 61 6e g_mode=normal an
160fc 64 20 74 68 65 6e 20 65 78 65 63 75 74 65 73 20 d then executes
160fd 61 0a 20 20 2a 2a 20 20 20 20 20 20 72 65 61 64 a. ** read
160fe 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 -transaction, th
160ff 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
16100 61 6c 6c 65 64 20 77 69 74 68 20 65 53 74 61 74 alled with eStat
16101 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20 e==PAGER_READER
16102 0a 20 20 2a 2a 20 20 20 20 20 20 61 6e 64 20 65 . ** and e
16103 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f Lock==EXCLUSIVE_
16104 4c 4f 43 4b 20 77 68 65 6e 20 74 68 65 20 72 65 LOCK when the re
16105 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 ad-transaction i
16106 73 20 63 6c 6f 73 65 64 2e 0a 20 20 2a 2f 0a 20 s closed.. */.
16107 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f assert( assert_
16108 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 pager_state(pPag
16109 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 er) );. assert(
1610a 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21 pPager->eState!
1610b 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 29 3b 0a =PAGER_ERROR );.
1610c 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53 if( pPager->eS
1610d 74 61 74 65 3c 50 41 47 45 52 5f 57 52 49 54 45 tate<PAGER_WRITE
1610e 52 5f 4c 4f 43 4b 45 44 20 26 26 20 70 50 61 67 R_LOCKED && pPag
1610f 65 72 2d 3e 65 4c 6f 63 6b 3c 52 45 53 45 52 56 er->eLock<RESERV
16110 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 ED_LOCK ){. r
16111 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
16112 0a 20 20 7d 0a 0a 20 20 72 65 6c 65 61 73 65 41 . }.. releaseA
16113 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 61 llSavepoints(pPa
16114 67 65 72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ger);. assert(
16115 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
16116 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 70 fd) || pPager->p
16117 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a InJournal==0 );.
16118 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 if( isOpen(pPa
16119 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 ger->jfd) ){.
1611a 20 61 73 73 65 72 74 28 20 21 70 61 67 65 72 55 assert( !pagerU
1611b 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 3b seWal(pPager) );
1611c 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61 6c 69 7a .. /* Finaliz
1611d 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
1611e 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 le. */. if( s
1611f 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e qlite3IsMemJourn
16120 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 al(pPager->jfd)
16121 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
16122 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
16123 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
16124 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 NALMODE_MEMORY )
16125 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f ;. sqlite3O
16126 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a sClose(pPager->j
16127 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 fd);. }else i
16128 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
16129 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
1612a 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 URNALMODE_TRUNCA
1612b 54 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 TE ){. if(
1612c 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
1612d 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ff==0 ){.
1612e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1612f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
16130 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
16131 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 e3OsTruncate(pPa
16132 67 65 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 ger->jfd, 0);.
16133 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 }. pPag
16134 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
16135 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 0;. }else if
16136 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
16137 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
16138 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 RNALMODE_PERSIST
16139 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65 . || (pPage
1613a 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
1613b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 && pPager->jour
1613c 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a nalMode!=PAGER_J
1613d 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 29 0a OURNALMODE_WAL).
1613e 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ){. rc
1613f 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 = zeroJournalHdr
16140 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74 (pPager, hasMast
16141 65 72 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 er);. pPage
16142 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
16143 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 0;. }else{.
16144 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e /* This bran
16145 63 68 20 6d 61 79 20 62 65 20 65 78 65 63 75 74 ch may be execut
16146 65 64 20 77 69 74 68 20 50 61 67 65 72 2e 6a 6f ed with Pager.jo
16147 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 urnalMode==MEMOR
16148 59 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 61 20 Y if. ** a
16149 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77 61 73 20 hot-journal was
1614a 6a 75 73 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b just rolled back
1614b 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
1614c 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 he journal.
1614d 20 2a 2a 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 ** file should
1614e 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 65 be closed and de
1614f 6c 65 74 65 64 2e 20 49 66 20 74 68 69 73 20 63 leted. If this c
16150 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73 onnection writes
16151 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 to. ** the
16152 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
16153 69 74 20 77 69 6c 6c 20 64 6f 20 73 6f 20 75 73 it will do so us
16154 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 ing an in-memory
16155 20 6a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 20 journal. .
16156 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 62 44 */. int bD
16157 65 6c 65 74 65 20 3d 20 28 21 70 50 61 67 65 72 elete = (!pPager
16158 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26 20 73 71 ->tempFile && sq
16159 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 45 78 69 73 lite3JournalExis
1615a 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 29 ts(pPager->jfd))
1615b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1615c 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
1615d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
1615e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 0a 20 ALMODE_DELETE .
1615f 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 50 61 || pPa
16160 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
16161 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
16162 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 ODE_MEMORY .
16163 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 || pPager
16164 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
16165 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
16166 5f 57 41 4c 20 0a 20 20 20 20 20 20 29 3b 0a 20 _WAL . );.
16167 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c sqlite3OsCl
16168 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 ose(pPager->jfd)
16169 3b 0a 20 20 20 20 20 20 69 66 28 20 62 44 65 6c ;. if( bDel
1616a 65 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ete ){. r
1616b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c c = sqlite3OsDel
1616c 65 74 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73 ete(pPager->pVfs
1616d 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e , pPager->zJourn
1616e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a al, 0);. }.
1616f 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 }. }..#ifde
16170 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
16171 41 47 45 53 0a 20 20 73 71 6c 69 74 65 33 50 63 AGES. sqlite3Pc
16172 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74 79 acheIterateDirty
16173 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
16174 2c 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67 65 , pager_set_page
16175 68 61 73 68 29 3b 0a 20 20 69 66 28 20 70 50 61 hash);. if( pPa
16176 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 30 20 26 ger->dbSize==0 &
16177 26 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 & sqlite3PcacheR
16178 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e efCount(pPager->
16179 70 50 43 61 63 68 65 29 3e 30 20 29 7b 0a 20 20 pPCache)>0 ){.
1617a 20 20 50 67 48 64 72 20 2a 70 20 3d 20 70 61 67 PgHdr *p = pag
1617b 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 er_lookup(pPager
1617c 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20 70 20 , 1);. if( p
1617d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 61 67 65 ){. p->page
1617e 48 61 73 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 Hash = 0;.
1617f 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
16180 66 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a f(p);. }. }.
16181 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 #endif.. sqlite
16182 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
16183 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
16184 6c 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 49 l);. pPager->pI
16185 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 nJournal = 0;.
16186 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 pPager->nRec = 0
16187 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 ;. sqlite3Pcach
16188 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 72 eCleanAll(pPager
16189 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 73 71 ->pPCache);. sq
1618a 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e 63 lite3PcacheTrunc
1618b 61 74 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ate(pPager->pPCa
1618c 63 68 65 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 che, pPager->dbS
1618d 69 7a 65 29 3b 0a 0a 20 20 69 66 28 20 70 61 67 ize);.. if( pag
1618e 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 erUseWal(pPager)
1618f 20 29 7b 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 ){. /* Drop
16190 74 68 65 20 57 41 4c 20 77 72 69 74 65 2d 6c 6f the WAL write-lo
16191 63 6b 2c 20 69 66 20 61 6e 79 2e 20 41 6c 73 6f ck, if any. Also
16192 2c 20 69 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 , if the connect
16193 69 6f 6e 20 77 61 73 20 69 6e 20 0a 20 20 20 20 ion was in .
16194 2a 2a 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d ** locking_mode=
16195 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 62 exclusive mode b
16196 75 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 2c ut is no longer,
16197 20 64 72 6f 70 20 74 68 65 20 45 58 43 4c 55 53 drop the EXCLUS
16198 49 56 45 20 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b IVE . ** lock
16199 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 held on the dat
1619a 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 abase file..
1619b 2a 2f 0a 20 20 20 20 72 63 32 20 3d 20 73 71 6c */. rc2 = sql
1619c 69 74 65 33 57 61 6c 45 6e 64 57 72 69 74 65 54 ite3WalEndWriteT
1619d 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 ransaction(pPage
1619e 72 2d 3e 70 57 61 6c 29 3b 0a 20 20 20 20 61 73 r->pWal);. as
1619f 73 65 72 74 28 20 72 63 32 3d 3d 53 51 4c 49 54 sert( rc2==SQLIT
161a0 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 E_OK );. }. if
161a1 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 ( !pPager->exclu
161a2 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 siveMode . &&
161a3 28 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 (!pagerUseWal(pP
161a4 61 67 65 72 29 20 7c 7c 20 73 71 6c 69 74 65 33 ager) || sqlite3
161a5 57 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f 64 65 WalExclusiveMode
161a6 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 30 (pPager->pWal, 0
161a7 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 63 32 20 )). ){. rc2
161a8 3d 20 70 61 67 65 72 55 6e 6c 6f 63 6b 44 62 28 = pagerUnlockDb(
161a9 70 50 61 67 65 72 2c 20 53 48 41 52 45 44 5f 4c pPager, SHARED_L
161aa 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72 OCK);. pPager
161ab 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e ->changeCountDon
161ac 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 50 61 e = 0;. }. pPa
161ad 67 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 ger->eState = PA
161ae 47 45 52 5f 52 45 41 44 45 52 3b 0a 20 20 70 50 GER_READER;. pP
161af 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 ager->setMaster
161b0 3d 20 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 28 = 0;.. return (
161b1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 72 63 rc==SQLITE_OK?rc
161b2 32 3a 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 2:rc);.}../*.**
161b3 45 78 65 63 75 74 65 20 61 20 72 6f 6c 6c 62 61 Execute a rollba
161b4 63 6b 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 ck if a transact
161b5 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e ion is active an
161b6 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 2a 2a d unlock the .**
161b7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
161b8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
161b9 67 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 ger has already
161ba 65 6e 74 65 72 65 64 20 74 68 65 20 45 52 52 4f entered the ERRO
161bb 52 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74 20 R state, do not
161bc 61 74 74 65 6d 70 74 20 0a 2a 2a 20 74 68 65 20 attempt .** the
161bd 72 6f 6c 6c 62 61 63 6b 20 61 74 20 74 68 69 73 rollback at this
161be 20 74 69 6d 65 2e 20 49 6e 73 74 65 61 64 2c 20 time. Instead,
161bf 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 pager_unlock() i
161c0 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 0a 2a 2a s called. The.**
161c1 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 call to pager_u
161c2 6e 6c 6f 63 6b 28 29 20 77 69 6c 6c 20 64 69 73 nlock() will dis
161c3 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f card all in-memo
161c4 72 79 20 70 61 67 65 73 2c 20 75 6e 6c 6f 63 6b ry pages, unlock
161c5 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
161c6 20 66 69 6c 65 20 61 6e 64 20 6d 6f 76 65 20 74 file and move t
161c7 68 65 20 70 61 67 65 72 20 62 61 63 6b 20 74 6f he pager back to
161c8 20 4f 50 45 4e 20 73 74 61 74 65 2e 20 49 66 20 OPEN state. If
161c9 74 68 69 73 20 0a 2a 2a 20 6d 65 61 6e 73 20 74 this .** means t
161ca 68 61 74 20 74 68 65 72 65 20 69 73 20 61 20 68 hat there is a h
161cb 6f 74 2d 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 ot-journal left
161cc 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 in the file-syst
161cd 65 6d 2c 20 74 68 65 20 6e 65 78 74 20 0a 2a 2a em, the next .**
161ce 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6f connection to o
161cf 62 74 61 69 6e 20 61 20 73 68 61 72 65 64 20 6c btain a shared l
161d0 6f 63 6b 20 6f 6e 20 74 68 65 20 70 61 67 65 72 ock on the pager
161d1 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 20 74 (which may be t
161d2 68 69 73 20 6f 6e 65 29 20 0a 2a 2a 20 77 69 6c his one) .** wil
161d3 6c 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a l roll it back..
161d4 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 **.** If the pag
161d5 65 72 20 68 61 73 20 6e 6f 74 20 61 6c 72 65 61 er has not alrea
161d6 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 20 45 dy entered the E
161d7 52 52 4f 52 20 73 74 61 74 65 2c 20 62 75 74 20 RROR state, but
161d8 61 6e 20 49 4f 20 6f 72 0a 2a 2a 20 6d 61 6c 6c an IO or.** mall
161d9 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 oc error occurs
161da 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 during a rollbac
161db 6b 2c 20 74 68 65 6e 20 74 68 69 73 20 77 69 6c k, then this wil
161dc 6c 20 69 74 73 65 6c 66 20 63 61 75 73 65 20 0a l itself cause .
161dd 2a 2a 20 74 68 65 20 70 61 67 65 72 20 74 6f 20 ** the pager to
161de 65 6e 74 65 72 20 74 68 65 20 45 52 52 4f 52 20 enter the ERROR
161df 73 74 61 74 65 2e 20 57 68 69 63 68 20 77 69 6c state. Which wil
161e0 6c 20 62 65 20 63 6c 65 61 72 65 64 20 62 79 20 l be cleared by
161e1 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 70 the.** call to p
161e2 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 2c 20 61 ager_unlock(), a
161e3 73 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 s described abov
161e4 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
161e5 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 d pagerUnlockAnd
161e6 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a Rollback(Pager *
161e7 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 70 pPager){. if( p
161e8 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d 50 Pager->eState!=P
161e9 41 47 45 52 5f 45 52 52 4f 52 20 26 26 20 70 50 AGER_ERROR && pP
161ea 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d 50 41 ager->eState!=PA
161eb 47 45 52 5f 4f 50 45 4e 20 29 7b 0a 20 20 20 20 GER_OPEN ){.
161ec 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 assert( assert_p
161ed 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 ager_state(pPage
161ee 72 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 r) );. if( pP
161ef 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41 ager->eState>=PA
161f0 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 GER_WRITER_LOCKE
161f1 44 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 D ){. sqlit
161f2 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
161f3 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73 71 6c loc();. sql
161f4 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 ite3PagerRollbac
161f5 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 k(pPager);.
161f6 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
161f7 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 7d nMalloc();. }
161f8 65 6c 73 65 20 69 66 28 20 21 70 50 61 67 65 72 else if( !pPager
161f9 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 ->exclusiveMode
161fa 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
161fb 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d pPager->eState=
161fc 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20 29 3b =PAGER_READER );
161fd 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65 6e 64 . pager_end
161fe 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 _transaction(pPa
161ff 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 ger, 0);. }.
16200 20 7d 0a 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 }. pager_unloc
16201 6b 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a k(pPager);.}../*
16202 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 61 44 .** Parameter aD
16203 61 74 61 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 ata must point t
16204 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 70 50 o a buffer of pP
16205 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 ager->pageSize b
16206 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 2e ytes.** of data.
16207 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 72 65 74 Compute and ret
16208 75 72 6e 20 61 20 63 68 65 63 6b 73 75 6d 20 62 urn a checksum b
16209 61 73 65 64 20 6f 6e 74 20 74 68 65 20 63 6f 6e ased ont the con
1620a 74 65 6e 74 73 20 6f 66 20 74 68 65 20 0a 2a 2a tents of the .**
1620b 20 70 61 67 65 20 6f 66 20 64 61 74 61 20 61 6e page of data an
1620c 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 d the current va
1620d 6c 75 65 20 6f 66 20 70 50 61 67 65 72 2d 3e 63 lue of pPager->c
1620e 6b 73 75 6d 49 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 ksumInit..**.**
1620f 54 68 69 73 20 69 73 20 6e 6f 74 20 61 20 72 65 This is not a re
16210 61 6c 20 63 68 65 63 6b 73 75 6d 2e 20 49 74 20 al checksum. It
16211 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74 is really just t
16212 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 0a 2a he sum of the .*
16213 2a 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c * random initial
16214 20 76 61 6c 75 65 20 28 70 50 61 67 65 72 2d 3e value (pPager->
16215 63 6b 73 75 6d 49 6e 69 74 29 20 61 6e 64 20 65 cksumInit) and e
16216 76 65 72 79 20 32 30 30 74 68 20 62 79 74 65 0a very 200th byte.
16217 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 64 ** of the page d
16218 61 74 61 2c 20 73 74 61 72 74 69 6e 67 20 77 69 ata, starting wi
16219 74 68 20 62 79 74 65 20 6f 66 66 73 65 74 20 28 th byte offset (
1621a 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
1621b 25 32 30 30 29 2e 0a 2a 2a 20 45 61 63 68 20 62 %200)..** Each b
1621c 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 65 74 yte is interpret
1621d 65 64 20 61 73 20 61 6e 20 38 2d 62 69 74 20 75 ed as an 8-bit u
1621e 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e nsigned integer.
1621f 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 .**.** Changing
16220 74 68 65 20 66 6f 72 6d 75 6c 61 20 75 73 65 64 the formula used
16221 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 69 73 to compute this
16222 20 63 68 65 63 6b 73 75 6d 20 72 65 73 75 6c 74 checksum result
16223 73 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6d s in an.** incom
16224 70 61 74 69 62 6c 65 20 6a 6f 75 72 6e 61 6c 20 patible journal
16225 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a file format..**.
16226 2a 2a 20 49 66 20 6a 6f 75 72 6e 61 6c 20 63 6f ** If journal co
16227 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 73 20 rruption occurs
16228 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72 20 66 due to a power f
16229 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73 74 ailure, the most
1622a 20 6c 69 6b 65 6c 79 20 0a 2a 2a 20 73 63 65 6e likely .** scen
1622b 61 72 69 6f 20 69 73 20 74 68 61 74 20 6f 6e 65 ario is that one
1622c 20 65 6e 64 20 6f 72 20 74 68 65 20 6f 74 68 65 end or the othe
1622d 72 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 r of the record
1622e 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 2e will be changed.
1622f 20 0a 2a 2a 20 49 74 20 69 73 20 6d 75 63 68 20 .** It is much
16230 6c 65 73 73 20 6c 69 6b 65 6c 79 20 74 68 61 74 less likely that
16231 20 74 68 65 20 74 77 6f 20 65 6e 64 73 20 6f 66 the two ends of
16232 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 63 the journal rec
16233 6f 72 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 63 ord will be.** c
16234 6f 72 72 65 63 74 20 61 6e 64 20 74 68 65 20 6d orrect and the m
16235 69 64 64 6c 65 20 62 65 20 63 6f 72 72 75 70 74 iddle be corrupt
16236 2e 20 20 54 68 75 73 2c 20 74 68 69 73 20 22 63 . Thus, this "c
16237 68 65 63 6b 73 75 6d 22 20 73 63 68 65 6d 65 2c hecksum" scheme,
16238 0a 2a 2a 20 74 68 6f 75 67 68 20 66 61 73 74 20 .** though fast
16239 61 6e 64 20 73 69 6d 70 6c 65 2c 20 63 61 74 63 and simple, catc
1623a 68 65 73 20 74 68 65 20 6d 6f 73 74 6c 79 20 6c hes the mostly l
1623b 69 6b 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63 6f ikely kind of co
1623c 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 rruption..*/.sta
1623d 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 63 6b tic u32 pager_ck
1623e 73 75 6d 28 50 61 67 65 72 20 2a 70 50 61 67 65 sum(Pager *pPage
1623f 72 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 r, const u8 *aDa
16240 74 61 29 7b 0a 20 20 75 33 32 20 63 6b 73 75 6d ta){. u32 cksum
16241 20 3d 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d = pPager->cksum
16242 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Init; /*
16243 20 43 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 20 Checksum value
16244 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 to return */. i
16245 6e 74 20 69 20 3d 20 70 50 61 67 65 72 2d 3e 70 nt i = pPager->p
16246 61 67 65 53 69 7a 65 2d 32 30 30 3b 20 20 20 20 ageSize-200;
16247 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
16248 75 6e 74 65 72 20 2a 2f 0a 20 20 77 68 69 6c 65 unter */. while
16249 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 63 6b 73 ( i>0 ){. cks
1624a 75 6d 20 2b 3d 20 61 44 61 74 61 5b 69 5d 3b 0a um += aData[i];.
1624b 20 20 20 20 69 20 2d 3d 20 32 30 30 3b 0a 20 20 i -= 200;.
1624c 7d 0a 20 20 72 65 74 75 72 6e 20 63 6b 73 75 6d }. return cksum
1624d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f 72 ;.}../*.** Repor
1624e 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 t the current pa
1624f 67 65 20 73 69 7a 65 20 61 6e 64 20 6e 75 6d 62 ge size and numb
16250 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 62 er of reserved b
16251 79 74 65 73 20 62 61 63 6b 0a 2a 2a 20 74 6f 20 ytes back.** to
16252 74 68 65 20 63 6f 64 65 63 2e 0a 2a 2f 0a 23 69 the codec..*/.#i
16253 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f fdef SQLITE_HAS_
16254 43 4f 44 45 43 0a 73 74 61 74 69 63 20 76 6f 69 CODEC.static voi
16255 64 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a d pagerReportSiz
16256 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 e(Pager *pPager)
16257 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e {. if( pPager->
16258 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 20 29 xCodecSizeChng )
16259 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 78 43 {. pPager->xC
1625a 6f 64 65 63 53 69 7a 65 43 68 6e 67 28 70 50 61 odecSizeChng(pPa
1625b 67 65 72 2d 3e 70 43 6f 64 65 63 2c 20 70 50 61 ger->pCodec, pPa
1625c 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20 ger->pageSize,.
1625d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1625e 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29 70 (int)p
1625f 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 29 Pager->nReserve)
16260 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 ;. }.}.#else.#
16261 64 65 66 69 6e 65 20 70 61 67 65 72 52 65 70 6f define pagerRepo
16262 72 74 53 69 7a 65 28 58 29 20 20 20 20 20 2f 2a rtSize(X) /*
16263 20 4e 6f 2d 6f 70 20 69 66 20 77 65 20 64 6f 20 No-op if we do
16264 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 20 63 6f not support a co
16265 64 65 63 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f dec */.#endif../
16266 2a 0a 2a 2a 20 52 65 61 64 20 61 20 73 69 6e 67 *.** Read a sing
16267 6c 65 20 70 61 67 65 20 66 72 6f 6d 20 65 69 74 le page from eit
16268 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 her the journal
16269 66 69 6c 65 20 28 69 66 20 69 73 4d 61 69 6e 4a file (if isMainJ
1626a 72 6e 6c 3d 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 rnl==1) or.** fr
1626b 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e om the sub-journ
1626c 61 6c 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e al (if isMainJrn
1626d 6c 3d 3d 30 29 20 61 6e 64 20 70 6c 61 79 62 61 l==0) and playba
1626e 63 6b 20 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a ck that page..**
1626f 20 54 68 65 20 70 61 67 65 20 62 65 67 69 6e 73 The page begins
16270 20 61 74 20 6f 66 66 73 65 74 20 2a 70 4f 66 66 at offset *pOff
16271 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c set into the fil
16272 65 2e 20 54 68 65 20 2a 70 4f 66 66 73 65 74 0a e. The *pOffset.
16273 2a 2a 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 ** value is incr
16274 65 61 73 65 64 20 74 6f 20 74 68 65 20 73 74 61 eased to the sta
16275 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70 rt of the next p
16276 61 67 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e age in the journ
16277 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 al..**.** The ma
16278 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 in rollback jour
16279 6e 61 6c 20 75 73 65 73 20 63 68 65 63 6b 73 75 nal uses checksu
1627a 6d 73 20 2d 20 74 68 65 20 73 74 61 74 65 6d 65 ms - the stateme
1627b 6e 74 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 nt journal does
1627c 0a 2a 2a 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 .** not..**.** I
1627d 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 f the page numbe
1627e 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 72 65 r of the page re
1627f 63 6f 72 64 20 72 65 61 64 20 66 72 6f 6d 20 74 cord read from t
16280 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c he (sub-)journal
16281 20 66 69 6c 65 0a 2a 2a 20 69 73 20 67 72 65 61 file.** is grea
16282 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 ter than the cur
16283 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 61 rent value of Pa
16284 67 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 65 6e ger.dbSize, then
16285 20 70 6c 61 79 62 61 63 6b 20 69 73 0a 2a 2a 20 playback is.**
16286 73 6b 69 70 70 65 64 20 61 6e 64 20 53 51 4c 49 skipped and SQLI
16287 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
16288 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 6f 6e d..**.** If pDon
16289 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 e is not NULL, t
1628a 68 65 6e 20 69 74 20 69 73 20 61 20 72 65 63 6f hen it is a reco
1628b 72 64 20 6f 66 20 70 61 67 65 73 20 74 68 61 74 rd of pages that
1628c 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a have already.**
1628d 20 62 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63 been played bac
1628e 6b 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 k. If the page
1628f 61 74 20 2a 70 4f 66 66 73 65 74 20 68 61 73 20 at *pOffset has
16290 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6c 61 already been pla
16291 79 65 64 20 62 61 63 6b 0a 2a 2a 20 28 69 66 20 yed back.** (if
16292 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
16293 67 20 70 44 6f 6e 65 20 62 69 74 20 69 73 20 73 g pDone bit is s
16294 65 74 29 20 74 68 65 6e 20 73 6b 69 70 20 74 68 et) then skip th
16295 65 20 70 6c 61 79 62 61 63 6b 2e 0a 2a 2a 20 4d e playback..** M
16296 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 44 6f ake sure the pDo
16297 6e 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e ne bit correspon
16298 64 69 6e 67 20 74 6f 20 74 68 65 20 2a 70 4f 66 ding to the *pOf
16299 66 73 65 74 20 70 61 67 65 20 69 73 20 73 65 74 fset page is set
1629a 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74 .** prior to ret
1629b 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 urning..**.** If
1629c 20 74 68 65 20 70 61 67 65 20 72 65 63 6f 72 64 the page record
1629d 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 is successfully
1629e 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28 read from the (
1629f 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c sub-)journal fil
162a0 65 0a 2a 2a 20 61 6e 64 20 70 6c 61 79 65 64 20 e.** and played
162a1 62 61 63 6b 2c 20 74 68 65 6e 20 53 51 4c 49 54 back, then SQLIT
162a2 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
162a3 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 . If an IO error
162a4 20 6f 63 63 75 72 73 0a 2a 2a 20 77 68 69 6c 65 occurs.** while
162a5 20 72 65 61 64 69 6e 67 20 74 68 65 20 72 65 63 reading the rec
162a6 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 ord from the (su
162a7 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 b-)journal file
162a8 6f 72 20 77 68 69 6c 65 20 77 72 69 74 69 6e 67 or while writing
162a9 0a 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62 .** to the datab
162aa 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 74 ase file, then t
162ab 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 he IO error code
162ac 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 is returned. If
162ad 20 64 61 74 61 0a 2a 2a 20 69 73 20 73 75 63 63 data.** is succ
162ae 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66 72 essfully read fr
162af 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 om the (sub-)jou
162b0 72 6e 61 6c 20 66 69 6c 65 20 62 75 74 20 61 70 rnal file but ap
162b1 70 65 61 72 73 20 74 6f 20 62 65 0a 2a 2a 20 63 pears to be.** c
162b2 6f 72 72 75 70 74 65 64 2c 20 53 51 4c 49 54 45 orrupted, SQLITE
162b3 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65 _DONE is returne
162b4 64 2e 20 44 61 74 61 20 69 73 20 63 6f 6e 73 69 d. Data is consi
162b5 64 65 72 65 64 20 63 6f 72 72 75 70 74 65 64 20 dered corrupted
162b6 69 6e 0a 2a 2a 20 74 77 6f 20 63 69 72 63 75 6d in.** two circum
162b7 73 74 61 6e 63 65 73 3a 0a 2a 2a 20 0a 2a 2a 20 stances:.** .**
162b8 20 20 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 * If the recor
162b9 64 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 d page-number is
162ba 20 69 6c 6c 65 67 61 6c 20 28 30 20 6f 72 20 50 illegal (0 or P
162bb 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 29 2c 20 6f AGER_MJ_PGNO), o
162bc 72 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 r.** * If the
162bd 72 65 63 6f 72 64 20 69 73 20 62 65 69 6e 67 20 record is being
162be 72 6f 6c 6c 65 64 20 62 61 63 6b 20 66 72 6f 6d rolled back from
162bf 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
162c0 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 61 6e l file.** an
162c1 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 66 d the checksum f
162c2 69 65 6c 64 20 64 6f 65 73 20 6e 6f 74 20 6d 61 ield does not ma
162c3 74 63 68 20 74 68 65 20 72 65 63 6f 72 64 20 63 tch the record c
162c4 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 65 ontent..**.** Ne
162c5 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 ither of these t
162c6 77 6f 20 73 63 65 6e 61 72 69 6f 73 20 61 72 65 wo scenarios are
162c7 20 70 6f 73 73 69 62 6c 65 20 64 75 72 69 6e 67 possible during
162c8 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c a savepoint rol
162c9 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 lback..**.** If
162ca 74 68 69 73 20 69 73 20 61 20 73 61 76 65 70 6f this is a savepo
162cb 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 int rollback, th
162cc 65 6e 20 6d 65 6d 6f 72 79 20 6d 61 79 20 68 61 en memory may ha
162cd 76 65 20 74 6f 20 62 65 20 64 79 6e 61 6d 69 63 ve to be dynamic
162ce 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 ally.** allocate
162cf 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 d by this functi
162d0 6f 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 on. If this is t
162d1 68 65 20 63 61 73 65 20 61 6e 64 20 61 6e 20 61 he case and an a
162d2 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
162d3 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d .** SQLITE_NOMEM
162d4 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
162d5 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
162d6 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 r_playback_one_p
162d7 61 67 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 age(. Pager *pP
162d8 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 ager,
162d9 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 /* The page
162da 72 20 62 65 69 6e 67 20 70 6c 61 79 65 64 20 62 r being played b
162db 61 63 6b 20 2a 2f 0a 20 20 69 36 34 20 2a 70 4f ack */. i64 *pO
162dc 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 ffset,
162dd 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
162de 20 6f 66 20 72 65 63 6f 72 64 20 74 6f 20 70 6c of record to pl
162df 61 79 62 61 63 6b 20 2a 2f 0a 20 20 42 69 74 76 ayback */. Bitv
162e0 65 63 20 2a 70 44 6f 6e 65 2c 20 20 20 20 20 20 ec *pDone,
162e1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 74 /* Bit
162e2 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72 vec of pages alr
162e3 65 61 64 79 20 70 6c 61 79 65 64 20 62 61 63 6b eady played back
162e4 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 61 69 6e */. int isMain
162e5 4a 72 6e 6c 2c 20 20 20 20 20 20 20 20 20 20 20 Jrnl,
162e6 20 20 20 20 2f 2a 20 31 20 2d 3e 20 6d 61 69 6e /* 1 -> main
162e7 20 6a 6f 75 72 6e 61 6c 2e 20 30 20 2d 3e 20 73 journal. 0 -> s
162e8 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 ub-journal. */.
162e9 20 69 6e 74 20 69 73 53 61 76 65 70 6e 74 20 20 int isSavepnt
162ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
162eb 2a 20 54 72 75 65 20 66 6f 72 20 61 20 73 61 76 * True for a sav
162ec 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 epoint rollback
162ed 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a */.){. int rc;.
162ee 20 20 50 67 48 64 72 20 2a 70 50 67 3b 20 20 20 PgHdr *pPg;
162ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
162f0 2f 2a 20 41 6e 20 65 78 69 73 74 69 6e 67 20 70 /* An existing p
162f1 61 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 age in the cache
162f2 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b */. Pgno pgno;
162f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
162f4 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 /* The page
162f5 6e 75 6d 62 65 72 20 6f 66 20 61 20 70 61 67 65 number of a page
162f6 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 in journal */.
162f7 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 20 20 u32 cksum;
162f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
162f9 2a 20 43 68 65 63 6b 73 75 6d 20 75 73 65 64 20 * Checksum used
162fa 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65 63 6b for sanity check
162fb 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 ing */. char *a
162fc 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 Data;
162fd 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 /* Tempor
162fe 61 72 79 20 73 74 6f 72 61 67 65 20 66 6f 72 20 ary storage for
162ff 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 73 71 the page */. sq
16300 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64 3b lite3_file *jfd;
16301 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
16302 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
16303 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e or for the journ
16304 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 al file */. int
16305 20 69 73 53 79 6e 63 65 64 3b 20 20 20 20 20 20 isSynced;
16306 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
16307 75 65 20 69 66 20 6a 6f 75 72 6e 61 6c 20 70 61 ue if journal pa
16308 67 65 20 69 73 20 73 79 6e 63 65 64 20 2a 2f 0a ge is synced */.
16309 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 4d 61 . assert( (isMa
1630a 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 20 29 3b inJrnl&~1)==0 );
1630b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 69 6e 4a /* isMainJ
1630c 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 20 2a 2f rnl is 0 or 1 */
1630d 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 53 61 . assert( (isSa
1630e 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 29 3b 20 vepnt&~1)==0 );
1630f 20 20 20 20 20 20 2f 2a 20 69 73 53 61 76 65 70 /* isSavep
16310 6e 74 20 69 73 20 30 20 6f 72 20 31 20 2a 2f 0a nt is 0 or 1 */.
16311 20 20 61 73 73 65 72 74 28 20 69 73 4d 61 69 6e assert( isMain
16312 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 20 29 3b Jrnl || pDone );
16313 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 20 61 6c /* pDone al
16314 77 61 79 73 20 75 73 65 64 20 6f 6e 20 73 75 62 ways used on sub
16315 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 61 -journals */. a
16316 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e 74 ssert( isSavepnt
16317 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 29 3b 20 || pDone==0 );
16318 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 76 65 72 /* pDone never
16319 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d 73 61 76 used on non-sav
1631a 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 61 44 61 epoint */.. aDa
1631b 74 61 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d ta = pPager->pTm
1631c 70 53 70 61 63 65 3b 0a 20 20 61 73 73 65 72 74 pSpace;. assert
1631d 28 20 61 44 61 74 61 20 29 3b 20 20 20 20 20 20 ( aData );
1631e 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 /* Temp stora
1631f 67 65 20 6d 75 73 74 20 68 61 76 65 20 61 6c 72 ge must have alr
16320 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 eady been alloca
16321 74 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ted */. assert(
16322 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 pagerUseWal(pPa
16323 67 65 72 29 3d 3d 30 20 7c 7c 20 28 21 69 73 4d ger)==0 || (!isM
16324 61 69 6e 4a 72 6e 6c 20 26 26 20 69 73 53 61 76 ainJrnl && isSav
16325 65 70 6e 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 45 epnt) );.. /* E
16326 69 74 68 65 72 20 74 68 65 20 73 74 61 74 65 20 ither the state
16327 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
16328 50 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 PAGER_WRITER_CAC
16329 48 45 4d 4f 44 20 28 61 20 74 72 61 6e 73 61 63 HEMOD (a transac
1632a 74 69 6f 6e 20 0a 20 20 2a 2a 20 6f 72 20 73 61 tion . ** or sa
1632b 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b vepoint rollback
1632c 20 64 6f 6e 65 20 61 74 20 74 68 65 20 72 65 71 done at the req
1632d 75 65 73 74 20 6f 66 20 74 68 65 20 63 61 6c 6c uest of the call
1632e 65 72 29 20 6f 72 20 74 68 69 73 20 69 73 0a 20 er) or this is.
1632f 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 ** a hot-journa
16330 6c 20 72 6f 6c 6c 62 61 63 6b 2e 20 49 66 20 69 l rollback. If i
16331 74 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e t is a hot-journ
16332 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 al rollback, the
16333 20 70 61 67 65 72 0a 20 20 2a 2a 20 69 73 20 69 pager. ** is i
16334 6e 20 73 74 61 74 65 20 4f 50 45 4e 20 61 6e 64 n state OPEN and
16335 20 68 6f 6c 64 73 20 61 6e 20 45 58 43 4c 55 53 holds an EXCLUS
16336 49 56 45 20 6c 6f 63 6b 2e 20 48 6f 74 2d 6a 6f IVE lock. Hot-jo
16337 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 0a 20 urnal rollback.
16338 20 2a 2a 20 6f 6e 6c 79 20 72 65 61 64 73 20 66 ** only reads f
16339 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 rom the main jou
1633a 72 6e 61 6c 2c 20 6e 6f 74 20 74 68 65 20 73 75 rnal, not the su
1633b 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2f 0a b-journal.. */.
1633c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
1633d 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f ->eState>=PAGER_
1633e 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 0a WRITER_CACHEMOD.
1633f 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65 || (pPage
16340 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 r->eState==PAGER
16341 5f 4f 50 45 4e 20 26 26 20 70 50 61 67 65 72 2d _OPEN && pPager-
16342 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 >eLock==EXCLUSIV
16343 45 5f 4c 4f 43 4b 29 0a 20 20 29 3b 0a 20 20 61 E_LOCK). );. a
16344 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 ssert( pPager->e
16345 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 57 52 49 State>=PAGER_WRI
16346 54 45 52 5f 43 41 43 48 45 4d 4f 44 20 7c 7c 20 TER_CACHEMOD ||
16347 69 73 4d 61 69 6e 4a 72 6e 6c 20 29 3b 0a 0a 20 isMainJrnl );..
16348 20 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 /* Read the pag
16349 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 e number and pag
1634a 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 e data from the
1634b 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a journal or sub-j
1634c 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 ournal. ** file
1634d 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f . Return an erro
1634e 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 r code to the ca
1634f 6c 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 65 72 ller if an IO er
16350 72 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a 2f ror occurs.. */
16351 0a 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e 4a . jfd = isMainJ
16352 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 rnl ? pPager->jf
16353 64 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 d : pPager->sjfd
16354 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 ;. rc = read32b
16355 69 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 65 its(jfd, *pOffse
16356 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 t, &pgno);. if(
16357 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
16358 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 return rc;. rc
16359 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
1635a 28 6a 66 64 2c 20 28 75 38 2a 29 61 44 61 74 61 (jfd, (u8*)aData
1635b 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
1635c 7a 65 2c 20 28 2a 70 4f 66 66 73 65 74 29 2b 34 ze, (*pOffset)+4
1635d 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
1635e 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 ITE_OK ) return
1635f 72 63 3b 0a 20 20 2a 70 4f 66 66 73 65 74 20 2b rc;. *pOffset +
16360 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 = pPager->pageSi
16361 7a 65 20 2b 20 34 20 2b 20 69 73 4d 61 69 6e 4a ze + 4 + isMainJ
16362 72 6e 6c 2a 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e rnl*4;.. /* San
16363 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 ity checking on
16364 74 68 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 the page. This
16365 69 73 20 6d 6f 72 65 20 69 6d 70 6f 72 74 61 6e is more importan
16366 74 20 74 68 61 74 20 49 20 6f 72 69 67 69 6e 61 t that I origina
16367 6c 6c 79 0a 20 20 2a 2a 20 74 68 6f 75 67 68 74 lly. ** thought
16368 2e 20 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 . If a power fa
16369 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 ilure occurs whi
1636a 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 le the journal i
1636b 73 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c s being written,
1636c 0a 20 20 2a 2a 20 69 74 20 63 6f 75 6c 64 20 63 . ** it could c
1636d 61 75 73 65 20 69 6e 76 61 6c 69 64 20 64 61 74 ause invalid dat
1636e 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 a to be written
1636f 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c into the journal
16370 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 0a 20 20 . We need to.
16371 2a 2a 20 64 65 74 65 63 74 20 74 68 69 73 20 69 ** detect this i
16372 6e 76 61 6c 69 64 20 64 61 74 61 20 28 77 69 74 nvalid data (wit
16373 68 20 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 h high probabili
16374 74 79 29 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 ty) and ignore i
16375 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 67 t.. */. if( pg
16376 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 no==0 || pgno==P
16377 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
16378 67 65 72 29 20 29 7b 0a 20 20 20 20 61 73 73 65 ger) ){. asse
16379 72 74 28 20 21 69 73 53 61 76 65 70 6e 74 20 29 rt( !isSavepnt )
1637a 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
1637b 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 ITE_DONE;. }.
1637c 69 66 28 20 70 67 6e 6f 3e 28 50 67 6e 6f 29 70 if( pgno>(Pgno)p
1637d 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 7c 7c Pager->dbSize ||
1637e 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 sqlite3BitvecTe
1637f 73 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 20 st(pDone, pgno)
16380 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
16381 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 LITE_OK;. }. i
16382 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 29 7b f( isMainJrnl ){
16383 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 33 32 . rc = read32
16384 62 69 74 73 28 6a 66 64 2c 20 28 2a 70 4f 66 66 bits(jfd, (*pOff
16385 73 65 74 29 2d 34 2c 20 26 63 6b 73 75 6d 29 3b set)-4, &cksum);
16386 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
16387 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 turn rc;. if(
16388 20 21 69 73 53 61 76 65 70 6e 74 20 26 26 20 70 !isSavepnt && p
16389 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 ager_cksum(pPage
1638a 72 2c 20 28 75 38 2a 29 61 44 61 74 61 29 21 3d r, (u8*)aData)!=
1638b 63 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 cksum ){. r
1638c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e eturn SQLITE_DON
1638d 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 E;. }. }..
1638e 2f 2a 20 49 66 20 74 68 69 73 20 70 61 67 65 20 /* If this page
1638f 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
16390 20 70 6c 61 79 65 64 20 62 79 20 62 65 66 6f 72 played by befor
16391 65 20 64 75 72 69 6e 67 20 74 68 65 20 63 75 72 e during the cur
16392 72 65 6e 74 0a 20 20 2a 2a 20 72 6f 6c 6c 62 61 rent. ** rollba
16393 63 6b 2c 20 74 68 65 6e 20 64 6f 6e 27 74 20 62 ck, then don't b
16394 6f 74 68 65 72 20 74 6f 20 70 6c 61 79 20 69 74 other to play it
16395 20 62 61 63 6b 20 61 67 61 69 6e 2e 0a 20 20 2a back again.. *
16396 2f 0a 20 20 69 66 28 20 70 44 6f 6e 65 20 26 26 /. if( pDone &&
16397 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 (rc = sqlite3Bi
16398 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c 20 70 tvecSet(pDone, p
16399 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b gno))!=SQLITE_OK
1639a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
1639b 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 68 65 c;. }.. /* Whe
1639c 6e 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 70 n playing back p
1639d 61 67 65 20 31 2c 20 72 65 73 74 6f 72 65 20 74 age 1, restore t
1639e 68 65 20 6e 52 65 73 65 72 76 65 20 73 65 74 74 he nReserve sett
1639f 69 6e 67 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 ing. */. if( p
163a0 67 6e 6f 3d 3d 31 20 26 26 20 70 50 61 67 65 72 gno==1 && pPager
163a1 2d 3e 6e 52 65 73 65 72 76 65 21 3d 28 28 75 38 ->nReserve!=((u8
163a2 2a 29 61 44 61 74 61 29 5b 32 30 5d 20 29 7b 0a *)aData)[20] ){.
163a3 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 73 pPager->nRes
163a4 65 72 76 65 20 3d 20 28 28 75 38 2a 29 61 44 61 erve = ((u8*)aDa
163a5 74 61 29 5b 32 30 5d 3b 0a 20 20 20 20 70 61 67 ta)[20];. pag
163a6 65 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 erReportSize(pPa
163a7 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ger);. }.. /*
163a8 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
163a9 69 6e 20 43 41 43 48 45 4d 4f 44 20 73 74 61 74 in CACHEMOD stat
163aa 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 e, then there mu
163ab 73 74 20 62 65 20 61 20 63 6f 70 79 20 6f 66 20 st be a copy of
163ac 74 68 69 73 0a 20 20 2a 2a 20 70 61 67 65 20 69 this. ** page i
163ad 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 n the pager cach
163ae 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 e. In this case
163af 6a 75 73 74 20 75 70 64 61 74 65 20 74 68 65 20 just update the
163b0 70 61 67 65 72 20 63 61 63 68 65 2c 0a 20 20 2a pager cache,. *
163b1 2a 20 6e 6f 74 20 74 68 65 20 64 61 74 61 62 61 * not the databa
163b2 73 65 20 66 69 6c 65 2e 20 54 68 65 20 70 61 67 se file. The pag
163b3 65 20 69 73 20 6c 65 66 74 20 6d 61 72 6b 65 64 e is left marked
163b4 20 64 69 72 74 79 20 69 6e 20 74 68 69 73 20 63 dirty in this c
163b5 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 ase.. **. ** A
163b6 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 n exception to t
163b7 68 65 20 61 62 6f 76 65 20 72 75 6c 65 3a 20 49 he above rule: I
163b8 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 f the database i
163b9 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 s in no-sync mod
163ba 65 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70 61 67 e. ** and a pag
163bb 65 20 69 73 20 6d 6f 76 65 64 20 64 75 72 69 6e e is moved durin
163bc 67 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c g an incremental
163bd 20 76 61 63 75 75 6d 20 74 68 65 6e 20 74 68 65 vacuum then the
163be 20 70 61 67 65 20 6d 61 79 0a 20 20 2a 2a 20 6e page may. ** n
163bf 6f 74 20 62 65 20 69 6e 20 74 68 65 20 70 61 67 ot be in the pag
163c0 65 72 20 63 61 63 68 65 2e 20 4c 61 74 65 72 3a er cache. Later:
163c1 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f if a malloc() o
163c2 72 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 r IO error occur
163c3 73 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 61 20 s. ** during a
163c4 4d 6f 76 65 70 61 67 65 28 29 20 63 61 6c 6c 2c Movepage() call,
163c5 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d then the page m
163c6 61 79 20 6e 6f 74 20 62 65 20 69 6e 20 74 68 65 ay not be in the
163c7 20 63 61 63 68 65 0a 20 20 2a 2a 20 65 69 74 68 cache. ** eith
163c8 65 72 2e 20 53 6f 20 74 68 65 20 63 6f 6e 64 69 er. So the condi
163c9 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 69 tion described i
163ca 6e 20 74 68 65 20 61 62 6f 76 65 20 70 61 72 61 n the above para
163cb 67 72 61 70 68 20 69 73 20 6e 6f 74 0a 20 20 2a graph is not. *
163cc 2a 20 61 73 73 65 72 74 28 29 61 62 6c 65 2e 0a * assert()able..
163cd 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 6e 20 **. ** If in
163ce 57 52 49 54 45 52 5f 44 42 4d 4f 44 2c 20 57 52 WRITER_DBMOD, WR
163cf 49 54 45 52 5f 46 49 4e 49 53 48 45 44 20 6f 72 ITER_FINISHED or
163d0 20 4f 50 45 4e 20 73 74 61 74 65 2c 20 74 68 65 OPEN state, the
163d1 6e 20 77 65 20 75 70 64 61 74 65 20 74 68 65 0a n we update the.
163d2 20 20 2a 2a 20 70 61 67 65 72 20 63 61 63 68 65 ** pager cache
163d3 20 69 66 20 69 74 20 65 78 69 73 74 73 20 61 6e if it exists an
163d4 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e d the main file.
163d5 20 54 68 65 20 70 61 67 65 20 69 73 20 74 68 65 The page is the
163d6 6e 20 6d 61 72 6b 65 64 20 0a 20 20 2a 2a 20 6e n marked . ** n
163d7 6f 74 20 64 69 72 74 79 2e 20 53 69 6e 63 65 20 ot dirty. Since
163d8 74 68 69 73 20 63 6f 64 65 20 69 73 20 6f 6e 6c this code is onl
163d9 79 20 65 78 65 63 75 74 65 64 20 69 6e 20 50 41 y executed in PA
163da 47 45 52 5f 4f 50 45 4e 20 73 74 61 74 65 20 66 GER_OPEN state f
163db 6f 72 0a 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f or. ** a hot-jo
163dc 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 urnal rollback,
163dd 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 it is guaranteed
163de 20 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 63 that the page-c
163df 61 63 68 65 20 69 73 20 65 6d 70 74 79 0a 20 20 ache is empty.
163e0 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65 72 20 ** if the pager
163e1 69 73 20 69 6e 20 4f 50 45 4e 20 73 74 61 74 65 is in OPEN state
163e2 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b .. **. ** Tick
163e3 65 74 20 23 31 31 37 31 3a 20 20 54 68 65 20 73 et #1171: The s
163e4 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c tatement journal
163e5 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 70 might contain p
163e6 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 61 74 age content that
163e7 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 is. ** differe
163e8 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 nt from the page
163e9 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 65 20 content at the
163ea 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 start of the tra
163eb 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 nsaction.. ** T
163ec 68 69 73 20 6f 63 63 75 72 73 20 77 68 65 6e 20 his occurs when
163ed 61 20 70 61 67 65 20 69 73 20 63 68 61 6e 67 65 a page is change
163ee 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 d prior to the s
163ef 74 61 72 74 20 6f 66 20 61 20 73 74 61 74 65 6d tart of a statem
163f0 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 63 68 ent. ** then ch
163f1 61 6e 67 65 64 20 61 67 61 69 6e 20 77 69 74 68 anged again with
163f2 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 in the statement
163f3 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 . When rolling
163f4 62 61 63 6b 20 73 75 63 68 20 61 0a 20 20 2a 2a back such a. **
163f5 20 73 74 61 74 65 6d 65 6e 74 20 77 65 20 6d 75 statement we mu
163f6 73 74 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 st not write to
163f7 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 the original dat
163f8 61 62 61 73 65 20 75 6e 6c 65 73 73 20 77 65 20 abase unless we
163f9 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 63 65 know. ** for ce
163fa 72 74 61 69 6e 20 74 68 61 74 20 6f 72 69 67 69 rtain that origi
163fb 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 nal page content
163fc 73 20 61 72 65 20 73 79 6e 63 65 64 20 69 6e 74 s are synced int
163fd 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 o the main rollb
163fe 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c ack. ** journal
163ff 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 . Otherwise, a
16400 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 68 74 power loss might
16401 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 64 20 leave modified
16402 64 61 74 61 20 69 6e 20 74 68 65 0a 20 20 2a 2a data in the. **
16403 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
16404 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 79 20 ithout an entry
16405 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 in the rollback
16406 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 61 6e journal that can
16407 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 74 68 . ** restore th
16408 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 e database to it
16409 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 6d 2e s original form.
1640a 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 Two conditions
1640b 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6d 65 must be. ** me
1640c 74 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 t before writing
1640d 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
1640e 20 66 69 6c 65 73 2e 20 28 31 29 20 74 68 65 20 files. (1) the
1640f 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 database must be
16410 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20 28 . ** locked. (
16411 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 2) we know that
16412 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 the original pag
16413 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 75 6c e content is ful
16414 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a 20 69 ly synced. ** i
16415 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e n the main journ
16416 61 6c 20 65 69 74 68 65 72 20 62 65 63 61 75 73 al either becaus
16417 65 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f e the page is no
16418 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 65 6c t in cache or el
16419 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 se. ** the page
1641a 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 6e 65 is marked as ne
1641b 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a edSync==0.. **.
1641c 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 34 3a ** 2008-04-14:
1641d 20 20 57 68 65 6e 20 61 74 74 65 6d 70 74 69 6e When attemptin
1641e 67 20 74 6f 20 76 61 63 75 75 6d 20 61 20 63 6f g to vacuum a co
1641f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 rrupt database f
16420 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 73 20 ile, it. ** is
16421 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 69 6c possible to fail
16422 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20 a statement on
16423 61 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 a database that
16424 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 78 69 does not yet exi
16425 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 st.. ** Do not
16426 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 attempt to write
16427 20 69 66 20 64 61 74 61 62 61 73 65 20 66 69 6c if database fil
16428 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 6e e has never been
16429 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 opened.. */.
1642a 69 66 28 20 70 61 67 65 72 55 73 65 57 61 6c 28 if( pagerUseWal(
1642b 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 70 pPager) ){. p
1642c 50 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b Pg = 0;. }else{
1642d 0a 20 20 20 20 70 50 67 20 3d 20 70 61 67 65 72 . pPg = pager
1642e 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 _lookup(pPager,
1642f 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 61 73 73 pgno);. }. ass
16430 65 72 74 28 20 70 50 67 20 7c 7c 20 21 4d 45 4d ert( pPg || !MEM
16431 44 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 DB );. assert(
16432 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d pPager->eState!=
16433 50 41 47 45 52 5f 4f 50 45 4e 20 7c 7c 20 70 50 PAGER_OPEN || pP
16434 67 3d 3d 30 20 29 3b 0a 20 20 50 41 47 45 52 54 g==0 );. PAGERT
16435 52 41 43 45 28 28 22 50 4c 41 59 42 41 43 4b 20 RACE(("PLAYBACK
16436 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68 28 %d page %d hash(
16437 25 30 38 78 29 20 25 73 5c 6e 22 2c 0a 20 20 20 %08x) %s\n",.
16438 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 PAGERID(
16439 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 pPager), pgno, p
1643a 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 ager_datahash(pP
1643b 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
1643c 28 75 38 2a 29 61 44 61 74 61 29 2c 0a 20 20 20 (u8*)aData),.
1643d 20 20 20 20 20 20 20 20 28 69 73 4d 61 69 6e 4a (isMainJ
1643e 72 6e 6c 3f 22 6d 61 69 6e 2d 6a 6f 75 72 6e 61 rnl?"main-journa
1643f 6c 22 3a 22 73 75 62 2d 6a 6f 75 72 6e 61 6c 22 l":"sub-journal"
16440 29 0a 20 20 29 29 3b 0a 20 20 69 66 28 20 69 73 ). ));. if( is
16441 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 MainJrnl ){.
16442 69 73 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 isSynced = pPage
16443 72 2d 3e 6e 6f 53 79 6e 63 20 7c 7c 20 28 2a 70 r->noSync || (*p
16444 4f 66 66 73 65 74 20 3c 3d 20 70 50 61 67 65 72 Offset <= pPager
16445 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 3b 0a 20 ->journalHdr);.
16446 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 73 53 79 }else{. isSy
16447 6e 63 65 64 20 3d 20 28 70 50 67 3d 3d 30 20 7c nced = (pPg==0 |
16448 7c 20 30 3d 3d 28 70 50 67 2d 3e 66 6c 61 67 73 | 0==(pPg->flags
16449 20 26 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 & PGHDR_NEED_SY
1644a 4e 43 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 NC));. }. if(
1644b 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
1644c 64 29 0a 20 20 20 26 26 20 28 70 50 61 67 65 72 d). && (pPager
1644d 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f ->eState>=PAGER_
1644e 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 7c 7c 20 WRITER_DBMOD ||
1644f 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d pPager->eState==
16450 50 41 47 45 52 5f 4f 50 45 4e 29 0a 20 20 20 26 PAGER_OPEN). &
16451 26 20 69 73 53 79 6e 63 65 64 0a 20 20 29 7b 0a & isSynced. ){.
16452 20 20 20 20 69 36 34 20 6f 66 73 74 20 3d 20 28 i64 ofst = (
16453 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 pgno-1)*(i64)pPa
16454 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 ger->pageSize;.
16455 20 20 20 74 65 73 74 63 61 73 65 28 20 21 69 73 testcase( !is
16456 53 61 76 65 70 6e 74 20 26 26 20 70 50 67 21 3d Savepnt && pPg!=
16457 30 20 26 26 20 28 70 50 67 2d 3e 66 6c 61 67 73 0 && (pPg->flags
16458 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
16459 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 )!=0 );. asse
1645a 72 74 28 20 21 70 61 67 65 72 55 73 65 57 61 6c rt( !pagerUseWal
1645b 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 (pPager) );.
1645c 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
1645d 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ite(pPager->fd,
1645e 28 75 38 2a 29 61 44 61 74 61 2c 20 70 50 61 67 (u8*)aData, pPag
1645f 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 er->pageSize, of
16460 73 74 29 3b 0a 20 20 20 20 69 66 28 20 70 67 6e st);. if( pgn
16461 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 o>pPager->dbFile
16462 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 70 50 Size ){. pP
16463 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 ager->dbFileSize
16464 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 7d 0a 20 = pgno;. }.
16465 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 if( pPager->p
16466 42 61 63 6b 75 70 20 29 7b 0a 20 20 20 20 20 20 Backup ){.
16467 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 61 CODEC1(pPager, a
16468 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33 2c 20 72 Data, pgno, 3, r
16469 63 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b c=SQLITE_NOMEM);
1646a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 61 . sqlite3Ba
1646b 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 67 65 ckupUpdate(pPage
1646c 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e 6f r->pBackup, pgno
1646d 2c 20 28 75 38 2a 29 61 44 61 74 61 29 3b 0a 20 , (u8*)aData);.
1646e 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 CODEC2(pPag
1646f 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e 6f 2c er, aData, pgno,
16470 20 37 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f 7, rc=SQLITE_NO
16471 4d 45 4d 2c 20 61 44 61 74 61 29 3b 0a 20 20 20 MEM, aData);.
16472 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 }. }else if( !
16473 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 26 20 70 50 isMainJrnl && pP
16474 67 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 g==0 ){. /* I
16475 66 20 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c f this is a roll
16476 62 61 63 6b 20 6f 66 20 61 20 73 61 76 65 70 6f back of a savepo
16477 69 6e 74 20 61 6e 64 20 64 61 74 61 20 77 61 73 int and data was
16478 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 0a not written to.
16479 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 ** the datab
1647a 61 73 65 20 61 6e 64 20 74 68 65 20 70 61 67 65 ase and the page
1647b 20 69 73 20 6e 6f 74 20 69 6e 2d 6d 65 6d 6f 72 is not in-memor
1647c 79 2c 20 74 68 65 72 65 20 69 73 20 61 20 70 6f y, there is a po
1647d 74 65 6e 74 69 61 6c 0a 20 20 20 20 2a 2a 20 70 tential. ** p
1647e 72 6f 62 6c 65 6d 2e 20 57 68 65 6e 20 74 68 65 roblem. When the
1647f 20 70 61 67 65 20 69 73 20 6e 65 78 74 20 66 65 page is next fe
16480 74 63 68 65 64 20 62 79 20 74 68 65 20 62 2d 74 tched by the b-t
16481 72 65 65 20 6c 61 79 65 72 2c 20 69 74 20 0a 20 ree layer, it .
16482 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65 ** will be re
16483 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ad from the data
16484 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68 base file, which
16485 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 may or may not
16486 62 65 20 0a 20 20 20 20 2a 2a 20 63 75 72 72 65 be . ** curre
16487 6e 74 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 nt. . **.
16488 2a 2a 20 54 68 65 72 65 20 61 72 65 20 61 20 63 ** There are a c
16489 6f 75 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65 ouple of differe
1648a 6e 74 20 77 61 79 73 20 74 68 69 73 20 63 61 6e nt ways this can
1648b 20 68 61 70 70 65 6e 2e 20 41 6c 6c 20 61 72 65 happen. All are
1648c 20 71 75 69 74 65 0a 20 20 20 20 2a 2a 20 6f 62 quite. ** ob
1648d 73 63 75 72 65 2e 20 57 68 65 6e 20 72 75 6e 6e scure. When runn
1648e 69 6e 67 20 69 6e 20 73 79 6e 63 68 72 6f 6e 6f ing in synchrono
1648f 75 73 20 6d 6f 64 65 2c 20 74 68 69 73 20 63 61 us mode, this ca
16490 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 0a 20 n only happen .
16491 20 20 20 2a 2a 20 69 66 20 74 68 65 20 70 61 67 ** if the pag
16492 65 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 e is on the free
16493 2d 6c 69 73 74 20 61 74 20 74 68 65 20 73 74 61 -list at the sta
16494 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 rt of the transa
16495 63 74 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 ction, then.
16496 2a 2a 20 70 6f 70 75 6c 61 74 65 64 2c 20 74 68 ** populated, th
16497 65 6e 20 6d 6f 76 65 64 20 75 73 69 6e 67 20 73 en moved using s
16498 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 qlite3PagerMovep
16499 61 67 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 age().. **.
1649a 20 20 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f ** The solutio
1649b 6e 20 69 73 20 74 6f 20 61 64 64 20 61 6e 20 69 n is to add an i
1649c 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 74 6f n-memory page to
1649d 20 74 68 65 20 63 61 63 68 65 20 63 6f 6e 74 61 the cache conta
1649e 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68 65 ining. ** the
1649f 20 64 61 74 61 20 6a 75 73 74 20 72 65 61 64 20 data just read
164a0 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 from the sub-jou
164a1 72 6e 61 6c 2e 20 4d 61 72 6b 20 74 68 65 20 70 rnal. Mark the p
164a2 61 67 65 20 61 73 20 64 69 72 74 79 20 0a 20 20 age as dirty .
164a3 20 20 2a 2a 20 61 6e 64 20 69 66 20 74 68 65 20 ** and if the
164a4 70 61 67 65 72 20 72 65 71 75 69 72 65 73 20 61 pager requires a
164a5 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 2c 20 74 journal-sync, t
164a6 68 65 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 hen mark the pag
164a7 65 20 61 73 20 0a 20 20 20 20 2a 2a 20 72 65 71 e as . ** req
164a8 75 69 72 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c uiring a journal
164a9 2d 73 79 6e 63 20 62 65 66 6f 72 65 20 69 74 20 -sync before it
164aa 69 73 20 77 72 69 74 74 65 6e 2e 0a 20 20 20 20 is written..
164ab 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 */. assert( i
164ac 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20 sSavepnt );.
164ad 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
164ae 64 6f 4e 6f 74 53 70 69 6c 6c 3d 3d 30 20 29 3b doNotSpill==0 );
164af 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e . pPager->doN
164b0 6f 74 53 70 69 6c 6c 2b 2b 3b 0a 20 20 20 20 72 otSpill++;. r
164b1 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
164b2 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20 Acquire(pPager,
164b3 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 3b 0a pgno, &pPg, 1);.
164b4 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
164b5 65 72 2d 3e 64 6f 4e 6f 74 53 70 69 6c 6c 3d 3d er->doNotSpill==
164b6 31 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 1 );. pPager-
164b7 3e 64 6f 4e 6f 74 53 70 69 6c 6c 2d 2d 3b 0a 20 >doNotSpill--;.
164b8 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
164b9 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 E_OK ) return rc
164ba 3b 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 ;. pPg->flags
164bb 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f &= ~PGHDR_NEED_
164bc 52 45 41 44 3b 0a 20 20 20 20 73 71 6c 69 74 65 READ;. sqlite
164bd 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 3PcacheMakeDirty
164be 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 (pPg);. }. if(
164bf 20 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a 20 4e pPg ){. /* N
164c0 6f 20 70 61 67 65 20 73 68 6f 75 6c 64 20 65 76 o page should ev
164c1 65 72 20 62 65 20 65 78 70 6c 69 63 69 74 6c 79 er be explicitly
164c2 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 61 rolled back tha
164c3 74 20 69 73 20 69 6e 20 75 73 65 2c 20 65 78 63 t is in use, exc
164c4 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 70 ept. ** for p
164c5 61 67 65 20 31 20 77 68 69 63 68 20 69 73 20 68 age 1 which is h
164c6 65 6c 64 20 69 6e 20 75 73 65 20 69 6e 20 6f 72 eld in use in or
164c7 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 der to keep the
164c8 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 lock on the.
164c9 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63 74 69 ** database acti
164ca 76 65 2e 20 48 6f 77 65 76 65 72 20 73 75 63 68 ve. However such
164cb 20 61 20 70 61 67 65 20 6d 61 79 20 62 65 20 72 a page may be r
164cc 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61 20 olled back as a
164cd 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 6f 66 result. ** of
164ce 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 72 72 an internal err
164cf 6f 72 20 72 65 73 75 6c 74 69 6e 67 20 69 6e 20 or resulting in
164d0 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c an automatic cal
164d1 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 l to. ** sqli
164d2 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
164d3 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 76 ().. */. v
164d4 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20 20 20 20 oid *pData;.
164d5 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 pData = pPg->pDa
164d6 74 61 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 ta;. memcpy(p
164d7 44 61 74 61 2c 20 28 75 38 2a 29 61 44 61 74 61 Data, (u8*)aData
164d8 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
164d9 7a 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d ze);. pPager-
164da 3e 78 52 65 69 6e 69 74 65 72 28 70 50 67 29 3b >xReiniter(pPg);
164db 0a 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e 4a . if( isMainJ
164dc 72 6e 6c 20 26 26 20 28 21 69 73 53 61 76 65 70 rnl && (!isSavep
164dd 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 3c 3d nt || *pOffset<=
164de 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 pPager->journalH
164df 64 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 dr) ){. /*
164e0 49 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 If the contents
164e1 6f 66 20 74 68 69 73 20 70 61 67 65 20 77 65 72 of this page wer
164e2 65 20 6a 75 73 74 20 72 65 73 74 6f 72 65 64 20 e just restored
164e3 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0a 20 from the main .
164e4 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 ** journal
164e5 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20 63 file, then its c
164e6 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 61 ontent must be a
164e7 73 20 74 68 65 79 20 77 65 72 65 20 77 68 65 6e s they were when
164e8 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 the . ** t
164e9 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 66 ransaction was f
164ea 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e 20 irst opened. In
164eb 74 68 69 73 20 63 61 73 65 20 77 65 20 63 61 6e this case we can
164ec 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0a 20 mark the page.
164ed 20 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61 6e ** as clean
164ee 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 69 , since there wi
164ef 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74 6f ll be no need to
164f0 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f write it out to
164f1 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 the. ** da
164f2 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 2a 2a tabase.. **
164f3 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20 . ** There
164f4 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e is one exception
164f5 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 49 to this rule. I
164f6 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62 65 f the page is be
164f7 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20 ing rolled.
164f8 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72 74 ** back as part
164f9 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 of a savepoint
164fa 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20 72 (or statement) r
164fb 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20 ollback from an
164fc 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63 . ** unsync
164fd 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 ed portion of th
164fe 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 e main journal f
164ff 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20 ile, then it is
16500 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20 2a not safe. *
16501 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 61 * to mark the pa
16502 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 69 ge as clean. Thi
16503 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61 72 s is because mar
16504 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 73 king the page as
16505 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20 . ** clean
16506 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 50 will clear the P
16507 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 GHDR_NEED_SYNC f
16508 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20 70 lag. Since the p
16509 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 age is. **
1650a 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 6a already in the j
1650b 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 63 ournal file (rec
1650c 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e 70 orded in Pager.p
1650d 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20 InJournal) and.
1650e 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 ** the PGHD
1650f 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
16510 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66 20 is cleared, if
16511 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 the page is writ
16512 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 ten to. **
16513 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69 again within thi
16514 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 s transaction, i
16515 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64 t will be marked
16516 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20 20 as dirty but.
16517 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 ** the PGHDR
16518 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 _NEED_SYNC flag
16519 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e will not be set.
1651a 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 70 It could then p
1651b 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20 otentially.
1651c 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 6f ** be written o
1651d 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 ut into the data
1651e 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 base file before
1651f 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c its journal fil
16520 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 e. ** segme
16521 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49 66 nt is synced. If
16522 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73 20 a crash occurs
16523 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 during or follow
16524 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20 20 ing this,.
16525 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 ** database corr
16526 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 uption may ensue
16527 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
16528 20 61 73 73 65 72 74 28 20 21 70 61 67 65 72 55 assert( !pagerU
16529 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 3b seWal(pPager) );
1652a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 . sqlite3Pc
1652b 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 50 acheMakeClean(pP
1652c 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 g);. }. pa
1652d 67 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 ger_set_pagehash
1652e 28 70 50 67 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 (pPg);.. /* I
1652f 66 20 74 68 69 73 20 77 61 73 20 70 61 67 65 20 f this was page
16530 31 2c 20 74 68 65 6e 20 72 65 73 74 6f 72 65 20 1, then restore
16531 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 67 the value of Pag
16532 65 72 2e 64 62 46 69 6c 65 56 65 72 73 2e 0a 20 er.dbFileVers..
16533 20 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 62 65 ** Do this be
16534 66 6f 72 65 20 61 6e 79 20 64 65 63 6f 64 69 6e fore any decodin
16535 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 g. */. if( pg
16536 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 6d no==1 ){. m
16537 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 emcpy(&pPager->d
16538 62 46 69 6c 65 56 65 72 73 2c 20 26 28 28 75 38 bFileVers, &((u8
16539 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c 73 69 7a *)pData)[24],siz
1653a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 eof(pPager->dbFi
1653b 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 7d 0a leVers));. }.
1653c 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64 65 20 74 . /* Decode t
1653d 68 65 20 70 61 67 65 20 6a 75 73 74 20 72 65 61 he page just rea
1653e 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a 2f 0a 20 d from disk */.
1653f 20 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 72 CODEC1(pPager
16540 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 , pData, pPg->pg
16541 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 no, 3, rc=SQLITE
16542 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 73 71 6c _NOMEM);. sql
16543 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 ite3PcacheReleas
16544 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 e(pPg);. }. re
16545 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
16546 2a 20 50 61 72 61 6d 65 74 65 72 20 7a 4d 61 73 * Parameter zMas
16547 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ter is the name
16548 6f 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 of a master jour
16549 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73 69 6e 67 nal file. A sing
1654a 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 le journal.** fi
1654b 6c 65 20 74 68 61 74 20 72 65 66 65 72 72 65 64 le that referred
1654c 20 74 6f 20 74 68 65 20 6d 61 73 74 65 72 20 6a to the master j
1654d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 ournal file has
1654e 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c 6c 65 64 just been rolled
1654f 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69 73 20 72 back..** This r
16550 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 outine checks if
16551 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 it is possible
16552 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 to delete the ma
16553 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
16554 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65 73 20 73 e,.** and does s
16555 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a o if it is..**.*
16556 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d 61 73 74 * Argument zMast
16557 65 72 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 er may point to
16558 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 2e Pager.pTmpSpace.
16559 20 53 6f 20 74 68 61 74 20 62 75 66 66 65 72 20 So that buffer
1655a 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76 61 69 6c is not .** avail
1655b 61 62 6c 65 20 66 6f 72 20 75 73 65 20 77 69 74 able for use wit
1655c 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hin this functio
1655d 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 n..**.** When a
1655e 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
1655f 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c 20 ile is created,
16560 69 74 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 it is populated
16561 77 69 74 68 20 74 68 65 20 6e 61 6d 65 73 20 0a with the names .
16562 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 69 74 73 ** of all of its
16563 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 73 2c child journals,
16564 20 6f 6e 65 20 61 66 74 65 72 20 61 6e 6f 74 68 one after anoth
16565 65 72 2c 20 66 6f 72 6d 61 74 74 65 64 20 61 73 er, formatted as
16566 20 75 74 66 2d 38 20 0a 2a 2a 20 65 6e 63 6f 64 utf-8 .** encod
16567 65 64 20 74 65 78 74 2e 20 54 68 65 20 65 6e 64 ed text. The end
16568 20 6f 66 20 65 61 63 68 20 63 68 69 6c 64 20 6a of each child j
16569 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6d ournal file is m
1656a 61 72 6b 65 64 20 77 69 74 68 20 61 20 0a 2a 2a arked with a .**
1656b 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 nul-terminator
1656c 62 79 74 65 20 28 30 78 30 30 29 2e 20 69 2e 65 byte (0x00). i.e
1656d 2e 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e . the entire con
1656e 74 65 6e 74 73 20 6f 66 20 61 20 6d 61 73 74 65 tents of a maste
1656f 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c r journal.** fil
16570 65 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74 e for a transact
16571 69 6f 6e 20 69 6e 76 6f 6c 76 69 6e 67 20 74 77 ion involving tw
16572 6f 20 64 61 74 61 62 61 73 65 73 20 6d 69 67 68 o databases migh
16573 74 20 62 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 2f t be:.**.** "/
16574 68 6f 6d 65 2f 62 69 6c 6c 2f 61 2e 64 62 2d 6a home/bill/a.db-j
16575 6f 75 72 6e 61 6c 5c 78 30 30 2f 68 6f 6d 65 2f ournal\x00/home/
16576 62 69 6c 6c 2f 62 2e 64 62 2d 6a 6f 75 72 6e 61 bill/b.db-journa
16577 6c 5c 78 30 30 22 0a 2a 2a 0a 2a 2a 20 41 20 6d l\x00".**.** A m
16578 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
16579 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 64 le may only be d
1657a 65 6c 65 74 65 64 20 6f 6e 63 65 20 61 6c 6c 20 eleted once all
1657b 6f 66 20 69 74 73 20 63 68 69 6c 64 20 0a 2a 2a of its child .**
1657c 20 6a 6f 75 72 6e 61 6c 73 20 68 61 76 65 20 62 journals have b
1657d 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e een rolled back.
1657e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
1657f 74 69 6f 6e 20 72 65 61 64 73 20 74 68 65 20 63 tion reads the c
16580 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6d ontents of the m
16581 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 aster-journal fi
16582 6c 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d 65 6d 6f le into .** memo
16583 72 79 20 61 6e 64 20 6c 6f 6f 70 73 20 74 68 72 ry and loops thr
16584 6f 75 67 68 20 65 61 63 68 20 6f 66 20 74 68 65 ough each of the
16585 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 6e child journal n
16586 61 6d 65 73 2e 20 46 6f 72 0a 2a 2a 20 65 61 63 ames. For.** eac
16587 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 2c h child journal,
16588 20 69 74 20 63 68 65 63 6b 73 20 69 66 3a 0a 2a it checks if:.*
16589 2a 0a 2a 2a 20 20 20 2a 20 69 66 20 74 68 65 20 *.** * if the
1658a 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 65 78 child journal ex
1658b 69 73 74 73 2c 20 61 6e 64 20 69 66 20 73 6f 0a ists, and if so.
1658c 2a 2a 20 20 20 2a 20 69 66 20 74 68 65 20 63 68 ** * if the ch
1658d 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 ild journal cont
1658e 61 69 6e 73 20 61 20 72 65 66 65 72 65 6e 63 65 ains a reference
1658f 20 74 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e to master journ
16590 61 6c 20 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20 al .** file
16591 7a 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a 20 49 66 zMaster.**.** If
16592 20 61 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c a child journal
16593 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 20 74 68 can be found th
16594 61 74 20 6d 61 74 63 68 65 73 20 62 6f 74 68 20 at matches both
16595 6f 66 20 74 68 65 20 63 72 69 74 65 72 69 61 0a of the criteria.
16596 2a 2a 20 61 62 6f 76 65 2c 20 74 68 69 73 20 66 ** above, this f
16597 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
16598 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e without doing an
16599 79 74 68 69 6e 67 2e 20 4f 74 68 65 72 77 69 73 ything. Otherwis
1659a 65 2c 20 69 66 0a 2a 2a 20 6e 6f 20 73 75 63 68 e, if.** no such
1659b 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 child journal c
1659c 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 66 69 6c an be found, fil
1659d 65 20 7a 4d 61 73 74 65 72 20 69 73 20 64 65 6c e zMaster is del
1659e 65 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 eted from.** the
1659f 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 75 73 69 file-system usi
165a0 6e 67 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 ng sqlite3OsDele
165a1 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 te()..**.** If a
165a2 6e 20 49 4f 20 65 72 72 6f 72 20 77 69 74 68 69 n IO error withi
165a3 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c n this function,
165a4 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 an error code i
165a5 73 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 s returned. This
165a6 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c .** function all
165a7 6f 63 61 74 65 73 20 6d 65 6d 6f 72 79 20 62 79 ocates memory by
165a8 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 calling sqlite3
165a9 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 20 61 6e 20 Malloc(). If an
165aa 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 66 61 allocation.** fa
165ab 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 ils, SQLITE_NOME
165ac 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f M is returned. O
165ad 74 68 65 72 77 69 73 65 2c 20 69 66 20 6e 6f 20 therwise, if no
165ae 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 20 65 72 72 IO or malloc err
165af 6f 72 73 20 0a 2a 2a 20 6f 63 63 75 72 2c 20 53 ors .** occur, S
165b0 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
165b1 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f rned..**.** TODO
165b2 3a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 : This function
165b3 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 69 6e 67 allocates a sing
165b4 6c 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f le block of memo
165b5 72 79 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 74 68 ry to load.** th
165b6 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 e entire content
165b7 73 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 s of the master
165b8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 journal file. Th
165b9 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a 2a 20 61 is could be.** a
165ba 20 63 6f 75 70 6c 65 20 6f 66 20 6b 69 6c 6f 62 couple of kilob
165bb 79 74 65 73 20 6f 72 20 73 6f 20 2d 20 70 6f 74 ytes or so - pot
165bc 65 6e 74 69 61 6c 6c 79 20 6c 61 72 67 65 72 20 entially larger
165bd 74 68 61 6e 20 74 68 65 20 70 61 67 65 20 0a 2a than the page .*
165be 2a 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 * size..*/.stati
165bf 63 20 69 6e 74 20 70 61 67 65 72 5f 64 65 6c 6d c int pager_delm
165c0 61 73 74 65 72 28 50 61 67 65 72 20 2a 70 50 61 aster(Pager *pPa
165c1 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 ger, const char
165c2 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 73 71 6c *zMaster){. sql
165c3 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d ite3_vfs *pVfs =
165c4 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 pPager->pVfs;.
165c5 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
165c6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
165c7 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 turn code */. s
165c8 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 qlite3_file *pMa
165c9 73 74 65 72 3b 20 20 20 20 2f 2a 20 4d 61 6c 6c ster; /* Mall
165ca 6f 63 27 64 20 6d 61 73 74 65 72 2d 6a 6f 75 72 oc'd master-jour
165cb 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 nal file descrip
165cc 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 tor */. sqlite3
165cd 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e 61 6c 3b _file *pJournal;
165ce 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 63 /* Malloc'd c
165cf 68 69 6c 64 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c hild-journal fil
165d0 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a e descriptor */.
165d1 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 4a char *zMasterJ
165d2 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f 2a 20 43 ournal = 0; /* C
165d3 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61 73 74 65 ontents of maste
165d4 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a r journal file *
165d5 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74 65 72 4a /. i64 nMasterJ
165d6 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 2f 2a ournal; /*
165d7 20 53 69 7a 65 20 6f 66 20 6d 61 73 74 65 72 20 Size of master
165d8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
165d9 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c char *zJournal
165da 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 ; /* P
165db 6f 69 6e 74 65 72 20 74 6f 20 6f 6e 65 20 6a 6f ointer to one jo
165dc 75 72 6e 61 6c 20 77 69 74 68 69 6e 20 4d 4a 20 urnal within MJ
165dd 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a file */. char *
165de 7a 4d 61 73 74 65 72 50 74 72 3b 20 20 20 20 20 zMasterPtr;
165df 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 /* Space to
165e0 68 6f 6c 64 20 4d 4a 20 66 69 6c 65 6e 61 6d 65 hold MJ filename
165e1 20 66 72 6f 6d 20 61 20 6a 6f 75 72 6e 61 6c 20 from a journal
165e2 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d file */. int nM
165e3 61 73 74 65 72 50 74 72 3b 20 20 20 20 20 20 20 asterPtr;
165e4 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 /* Amount of
165e5 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 space allocated
165e6 20 74 6f 20 7a 4d 61 73 74 65 72 50 74 72 5b 5d to zMasterPtr[]
165e7 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 */.. /* Alloca
165e8 74 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f 74 te space for bot
165e9 68 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20 61 h the pJournal a
165ea 6e 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65 20 nd pMaster file
165eb 64 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20 2a descriptors.. *
165ec 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c * If successful,
165ed 20 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 open the master
165ee 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f journal file fo
165ef 72 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f 0a r reading.. */.
165f0 20 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71 6c pMaster = (sql
165f1 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 ite3_file *)sqli
165f2 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 te3MallocZero(pV
165f3 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 fs->szOsFile * 2
165f4 29 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 );. pJournal =
165f5 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
165f6 28 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 (((u8 *)pMaster)
165f7 20 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c + pVfs->szOsFil
165f8 65 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 e);. if( !pMast
165f9 65 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 er ){. rc = S
165fa 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
165fb 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 else{. const
165fc 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c int flags = (SQL
165fd 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
165fe 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 Y|SQLITE_OPEN_MA
165ff 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 STER_JOURNAL);.
16600 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
16601 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 sOpen(pVfs, zMas
16602 74 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c ter, pMaster, fl
16603 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 ags, 0);. }. i
16604 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
16605 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 ) goto delmaste
16606 72 5f 6f 75 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 61 r_out;.. /* Loa
16607 64 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73 d the entire mas
16608 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
16609 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 into space obta
1660a 69 6e 65 64 20 66 72 6f 6d 0a 20 20 2a 2a 20 73 ined from. ** s
1660b 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
1660c 61 6e 64 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 and pointed to b
1660d 79 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c y zMasterJournal
1660e 2e 20 20 20 41 6c 73 6f 20 6f 62 74 61 69 6e 0a . Also obtain.
1660f 20 20 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 ** sufficient
16610 73 70 61 63 65 20 28 69 6e 20 7a 4d 61 73 74 65 space (in zMaste
16611 72 50 74 72 29 20 74 6f 20 68 6f 6c 64 20 74 68 rPtr) to hold th
16612 65 20 6e 61 6d 65 73 20 6f 66 20 6d 61 73 74 65 e names of maste
16613 72 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 r. ** journal f
16614 69 6c 65 73 20 65 78 74 72 61 63 74 65 64 20 66 iles extracted f
16615 72 6f 6d 20 72 65 67 75 6c 61 72 20 72 6f 6c 6c rom regular roll
16616 62 61 63 6b 2d 6a 6f 75 72 6e 61 6c 73 2e 0a 20 back-journals..
16617 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 */. rc = sqlit
16618 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4d 61 e3OsFileSize(pMa
16619 73 74 65 72 2c 20 26 6e 4d 61 73 74 65 72 4a 6f ster, &nMasterJo
1661a 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28 20 72 63 urnal);. if( rc
1661b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
1661c 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 to delmaster_out
1661d 3b 0a 20 20 6e 4d 61 73 74 65 72 50 74 72 20 3d ;. nMasterPtr =
1661e 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d pVfs->mxPathnam
1661f 65 2b 31 3b 0a 20 20 7a 4d 61 73 74 65 72 4a 6f e+1;. zMasterJo
16620 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 4d urnal = sqlite3M
16621 61 6c 6c 6f 63 28 28 69 6e 74 29 6e 4d 61 73 74 alloc((int)nMast
16622 65 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e 4d 61 73 erJournal + nMas
16623 74 65 72 50 74 72 20 2b 20 31 29 3b 0a 20 20 69 terPtr + 1);. i
16624 66 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e f( !zMasterJourn
16625 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 al ){. rc = S
16626 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
16627 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
16628 6f 75 74 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 out;. }. zMast
16629 65 72 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72 erPtr = &zMaster
1662a 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a Journal[nMasterJ
1662b 6f 75 72 6e 61 6c 2b 31 5d 3b 0a 20 20 72 63 20 ournal+1];. rc
1662c 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
1662d 70 4d 61 73 74 65 72 2c 20 7a 4d 61 73 74 65 72 pMaster, zMaster
1662e 4a 6f 75 72 6e 61 6c 2c 20 28 69 6e 74 29 6e 4d Journal, (int)nM
1662f 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 30 29 asterJournal, 0)
16630 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
16631 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c TE_OK ) goto del
16632 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 7a 4d master_out;. zM
16633 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 asterJournal[nMa
16634 73 74 65 72 4a 6f 75 72 6e 61 6c 5d 20 3d 20 30 sterJournal] = 0
16635 3b 0a 0a 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 ;.. zJournal =
16636 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a zMasterJournal;.
16637 20 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e while( (zJourn
16638 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 al-zMasterJourna
16639 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 l)<nMasterJourna
1663a 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 78 69 l ){. int exi
1663b 73 74 73 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 sts;. rc = sq
1663c 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 lite3OsAccess(pV
1663d 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 fs, zJournal, SQ
1663e 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 LITE_ACCESS_EXIS
1663f 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a 20 20 TS, &exists);.
16640 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16641 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 _OK ){. got
16642 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b o delmaster_out;
16643 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 . }. if( e
16644 78 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 2f xists ){. /
16645 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 * One of the jou
16646 72 6e 61 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f rnals pointed to
16647 20 62 79 20 74 68 65 20 6d 61 73 74 65 72 20 6a by the master j
16648 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20 ournal exists..
16649 20 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20 ** Open it
1664a 61 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74 20 and check if it
1664b 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61 points at the ma
1664c 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 ster journal. If
1664d 0a 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72 65 . ** so, re
1664e 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 65 6c turn without del
1664f 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 eting the master
16650 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 journal file..
16651 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e */. in
16652 74 20 63 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 t c;. int f
16653 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f lags = (SQLITE_O
16654 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c PEN_READONLY|SQL
16655 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
16656 55 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20 72 63 URNAL);. rc
16657 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e = sqlite3OsOpen
16658 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c (pVfs, zJournal,
16659 20 70 4a 6f 75 72 6e 61 6c 2c 20 66 6c 61 67 73 pJournal, flags
1665a 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
1665b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1665c 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 . goto de
1665d 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 lmaster_out;.
1665e 20 20 20 7d 0a 0a 20 20 20 20 20 20 72 63 20 3d }.. rc =
1665f 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e readMasterJourn
16660 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a 4d 61 al(pJournal, zMa
16661 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 65 72 sterPtr, nMaster
16662 50 74 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 Ptr);. sqli
16663 74 65 33 4f 73 43 6c 6f 73 65 28 70 4a 6f 75 72 te3OsClose(pJour
16664 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 nal);. if(
16665 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
16666 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 . goto de
16667 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 lmaster_out;.
16668 20 20 20 7d 0a 0a 20 20 20 20 20 20 63 20 3d 20 }.. c =
16669 7a 4d 61 73 74 65 72 50 74 72 5b 30 5d 21 3d 30 zMasterPtr[0]!=0
1666a 20 26 26 20 73 74 72 63 6d 70 28 7a 4d 61 73 74 && strcmp(zMast
1666b 65 72 50 74 72 2c 20 7a 4d 61 73 74 65 72 29 3d erPtr, zMaster)=
1666c 3d 30 3b 0a 20 20 20 20 20 20 69 66 28 20 63 20 =0;. if( c
1666d 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 ){. /* We
1666e 20 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44 have a match. D
1666f 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 o not delete the
16670 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
16671 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 file. */.
16672 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
16673 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 out;. }.
16674 20 7d 0a 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 }. zJournal
16675 2b 3d 20 28 73 71 6c 69 74 65 33 53 74 72 6c 65 += (sqlite3Strle
16676 6e 33 30 28 7a 4a 6f 75 72 6e 61 6c 29 2b 31 29 n30(zJournal)+1)
16677 3b 0a 20 20 7d 0a 20 0a 20 20 73 71 6c 69 74 65 ;. }. . sqlite
16678 33 4f 73 43 6c 6f 73 65 28 70 4d 61 73 74 65 72 3OsClose(pMaster
16679 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1667a 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 3OsDelete(pVfs,
1667b 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 0a 64 65 zMaster, 0);..de
1667c 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a 20 20 73 lmaster_out:. s
1667d 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 qlite3_free(zMas
1667e 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69 terJournal);. i
1667f 66 28 20 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 f( pMaster ){.
16680 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
16681 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 61 (pMaster);. a
16682 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 ssert( !isOpen(p
16683 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a 20 20 20 20 Journal) );.
16684 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 61 sqlite3_free(pMa
16685 73 74 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ster);. }. ret
16686 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
16687 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
16688 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e 67 is used to chang
16689 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 69 7a e the actual siz
1668a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
1668b 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 74 68 e .** file in th
1668c 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 20 54 e file-system. T
1668d 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 his only happens
1668e 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 when committing
1668f 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a a transaction,.
16690 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 ** or rolling ba
16691 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ck a transaction
16692 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f 6c 6c (including roll
16693 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 2d 6a ing back a hot-j
16694 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49 ournal)..**.** I
16695 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 f the main datab
16696 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 ase file is not
16697 6f 70 65 6e 2c 20 6f 72 20 74 68 65 20 70 61 67 open, or the pag
16698 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 65 69 74 er is not in eit
16699 68 65 72 0a 2a 2a 20 44 42 4d 4f 44 20 6f 72 20 her.** DBMOD or
1669a 4f 50 45 4e 20 73 74 61 74 65 2c 20 74 68 69 73 OPEN state, this
1669b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
1669c 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c o-op. Otherwise,
1669d 20 74 68 65 20 73 69 7a 65 20 0a 2a 2a 20 6f 66 the size .** of
1669e 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 68 61 the file is cha
1669f 6e 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 nged to nPage pa
166a0 67 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 ges (nPage*pPage
166a1 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 r->pageSize byte
166a2 73 29 2e 20 0a 2a 2a 20 49 66 20 74 68 65 20 66 s). .** If the f
166a3 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 ile on disk is c
166a4 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 urrently larger
166a5 74 68 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73 than nPage pages
166a6 2c 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 56 , then use the V
166a7 46 53 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 FS.** xTruncate(
166a8 29 20 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e ) method to trun
166a9 63 61 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f cate it..**.** O
166aa 72 2c 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68 r, it might migh
166ab 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 t be the case th
166ac 61 74 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 at the file on d
166ad 69 73 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 isk is smaller t
166ae 68 61 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 han .** nPage pa
166af 67 65 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 ges. Some operat
166b0 69 6e 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 ing system imple
166b1 6d 65 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 mentations can g
166b2 65 74 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a et confused if .
166b3 2a 2a 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72 ** you try to tr
166b4 75 6e 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f uncate a file to
166b5 20 73 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20 some size that
166b6 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 is larger than i
166b7 74 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 t .** currently
166b8 69 73 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68 is, so detect th
166b9 69 73 20 63 61 73 65 20 61 6e 64 20 77 72 69 74 is case and writ
166ba 65 20 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 e a single zero
166bb 62 79 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 byte to .** the
166bc 65 6e 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66 end of the new f
166bd 69 6c 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a ile instead..**.
166be 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c ** If successful
166bf 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
166c0 4f 4b 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 OK. If an IO err
166c1 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
166c2 6d 6f 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 modifying.** the
166c3 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
166c4 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
166c5 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c code to the cal
166c6 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ler..*/.static i
166c7 6e 74 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 nt pager_truncat
166c8 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c e(Pager *pPager,
166c9 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 Pgno nPage){.
166ca 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
166cb 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 OK;. assert( pP
166cc 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d 50 41 ager->eState!=PA
166cd 47 45 52 5f 45 52 52 4f 52 20 29 3b 0a 20 20 61 GER_ERROR );. a
166ce 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 ssert( pPager->e
166cf 53 74 61 74 65 21 3d 50 41 47 45 52 5f 52 45 41 State!=PAGER_REA
166d0 44 45 52 20 29 3b 0a 20 20 0a 20 20 69 66 28 20 DER );. . if(
166d1 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
166d2 64 29 20 0a 20 20 20 26 26 20 28 70 50 61 67 65 d) . && (pPage
166d3 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52 r->eState>=PAGER
166d4 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 7c 7c _WRITER_DBMOD ||
166d5 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d pPager->eState=
166d6 3d 50 41 47 45 52 5f 4f 50 45 4e 29 20 0a 20 20 =PAGER_OPEN) .
166d7 29 7b 0a 20 20 20 20 69 36 34 20 63 75 72 72 65 ){. i64 curre
166d8 6e 74 53 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b ntSize, newSize;
166d9 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 67 65 20 . int szPage
166da 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 = pPager->pageSi
166db 7a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ze;. assert(
166dc 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 45 pPager->eLock==E
166dd 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b XCLUSIVE_LOCK );
166de 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 . /* TODO: Is
166df 20 69 74 20 73 61 66 65 20 74 6f 20 75 73 65 20 it safe to use
166e0 50 61 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 Pager.dbFileSize
166e1 20 68 65 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63 here? */. rc
166e2 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 = sqlite3OsFile
166e3 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c Size(pPager->fd,
166e4 20 26 63 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a ¤tSize);.
166e5 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 73 7a newSize = sz
166e6 50 61 67 65 2a 28 69 36 34 29 6e 50 61 67 65 3b Page*(i64)nPage;
166e7 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
166e8 49 54 45 5f 4f 4b 20 26 26 20 63 75 72 72 65 6e ITE_OK && curren
166e9 74 53 69 7a 65 21 3d 6e 65 77 53 69 7a 65 20 29 tSize!=newSize )
166ea 7b 0a 20 20 20 20 20 20 69 66 28 20 63 75 72 72 {. if( curr
166eb 65 6e 74 53 69 7a 65 3e 6e 65 77 53 69 7a 65 20 entSize>newSize
166ec 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
166ed 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 sqlite3OsTruncat
166ee 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6e 65 e(pPager->fd, ne
166ef 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 65 wSize);. }e
166f0 6c 73 65 20 69 66 28 20 28 63 75 72 72 65 6e 74 lse if( (current
166f1 53 69 7a 65 2b 73 7a 50 61 67 65 29 3c 3d 6e 65 Size+szPage)<=ne
166f2 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 wSize ){.
166f3 20 63 68 61 72 20 2a 70 54 6d 70 20 3d 20 70 50 char *pTmp = pP
166f4 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b ager->pTmpSpace;
166f5 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 . memset(
166f6 70 54 6d 70 2c 20 30 2c 20 73 7a 50 61 67 65 29 pTmp, 0, szPage)
166f7 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
166f8 73 65 28 20 28 6e 65 77 53 69 7a 65 2d 73 7a 50 se( (newSize-szP
166f9 61 67 65 29 20 3d 3d 20 63 75 72 72 65 6e 74 53 age) == currentS
166fa 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 20 20 74 ize );. t
166fb 65 73 74 63 61 73 65 28 20 28 6e 65 77 53 69 7a estcase( (newSiz
166fc 65 2d 73 7a 50 61 67 65 29 20 3e 20 20 63 75 72 e-szPage) > cur
166fd 72 65 6e 74 53 69 7a 65 20 29 3b 0a 20 20 20 20 rentSize );.
166fe 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
166ff 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
16700 66 64 2c 20 70 54 6d 70 2c 20 73 7a 50 61 67 65 fd, pTmp, szPage
16701 2c 20 6e 65 77 53 69 7a 65 2d 73 7a 50 61 67 65 , newSize-szPage
16702 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
16703 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
16704 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 OK ){. pP
16705 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 ager->dbFileSize
16706 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 = nPage;.
16707 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
16708 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
16709 2a 20 52 65 74 75 72 6e 20 61 20 73 61 6e 69 74 * Return a sanit
1670a 69 7a 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 ized version of
1670b 74 68 65 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 the sector-size
1670c 6f 66 20 4f 53 20 66 69 6c 65 20 70 46 69 6c 65 of OS file pFile
1670d 2e 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 . The.** return
1670e 76 61 6c 75 65 20 69 73 20 67 75 61 72 61 6e 74 value is guarant
1670f 65 65 64 20 74 6f 20 6c 69 65 20 62 65 74 77 65 eed to lie betwe
16710 65 6e 20 33 32 20 61 6e 64 20 4d 41 58 5f 53 45 en 32 and MAX_SE
16711 43 54 4f 52 5f 53 49 5a 45 2e 0a 2a 2f 0a 53 51 CTOR_SIZE..*/.SQ
16712 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
16713 20 73 71 6c 69 74 65 33 53 65 63 74 6f 72 53 69 sqlite3SectorSi
16714 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ze(sqlite3_file
16715 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 69 *pFile){. int i
16716 52 65 74 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 Ret = sqlite3OsS
16717 65 63 74 6f 72 53 69 7a 65 28 70 46 69 6c 65 29 ectorSize(pFile)
16718 3b 0a 20 20 69 66 28 20 69 52 65 74 3c 33 32 20 ;. if( iRet<32
16719 29 7b 0a 20 20 20 20 69 52 65 74 20 3d 20 35 31 ){. iRet = 51
1671a 32 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 2;. }else if( i
1671b 52 65 74 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53 Ret>MAX_SECTOR_S
1671c 49 5a 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72 IZE ){. asser
1671d 74 28 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 t( MAX_SECTOR_SI
1671e 5a 45 3e 3d 35 31 32 20 29 3b 0a 20 20 20 20 69 ZE>=512 );. i
1671f 52 65 74 20 3d 20 4d 41 58 5f 53 45 43 54 4f 52 Ret = MAX_SECTOR
16720 5f 53 49 5a 45 3b 0a 20 20 7d 0a 20 20 72 65 74 _SIZE;. }. ret
16721 75 72 6e 20 69 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a urn iRet;.}../*.
16722 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75 65 ** Set the value
16723 20 6f 66 20 74 68 65 20 50 61 67 65 72 2e 73 65 of the Pager.se
16724 63 74 6f 72 53 69 7a 65 20 76 61 72 69 61 62 6c ctorSize variabl
16725 65 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 0a e for the given.
16726 2a 2a 20 70 61 67 65 72 20 62 61 73 65 64 20 6f ** pager based o
16727 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 n the value retu
16728 72 6e 65 64 20 62 79 20 74 68 65 20 78 53 65 63 rned by the xSec
16729 74 6f 72 53 69 7a 65 20 6d 65 74 68 6f 64 0a 2a torSize method.*
1672a 2a 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 64 61 * of the open da
1672b 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 tabase file. The
1672c 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c sector size wil
1672d 6c 20 62 65 20 75 73 65 64 20 75 73 65 64 20 0a l be used used .
1672e 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ** to determine
1672f 74 68 65 20 73 69 7a 65 20 61 6e 64 20 61 6c 69 the size and ali
16730 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75 72 6e 61 gnment of journa
16731 6c 20 68 65 61 64 65 72 20 61 6e 64 20 0a 2a 2a l header and .**
16732 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
16733 70 6f 69 6e 74 65 72 73 20 77 69 74 68 69 6e 20 pointers within
16734 63 72 65 61 74 65 64 20 6a 6f 75 72 6e 61 6c 20 created journal
16735 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 files..**.** For
16736 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
16737 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 73 the effective s
16738 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20 61 6c ector size is al
16739 77 61 79 73 20 35 31 32 20 62 79 74 65 73 2e 0a ways 512 bytes..
1673a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
1673b 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d 70 6f 72 61 for non-tempora
1673c 72 79 20 66 69 6c 65 73 2c 20 74 68 65 20 65 66 ry files, the ef
1673d 66 65 63 74 69 76 65 20 73 65 63 74 6f 72 20 73 fective sector s
1673e 69 7a 65 20 69 73 0a 2a 2a 20 74 68 65 20 76 61 ize is.** the va
1673f 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
16740 74 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 28 the xSectorSize(
16741 29 20 6d 65 74 68 6f 64 20 72 6f 75 6e 64 65 64 ) method rounded
16742 20 75 70 20 74 6f 20 33 32 20 69 66 0a 2a 2a 20 up to 32 if.**
16743 69 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 it is less than
16744 33 32 2c 20 6f 72 20 72 6f 75 6e 64 65 64 20 64 32, or rounded d
16745 6f 77 6e 20 74 6f 20 4d 41 58 5f 53 45 43 54 4f own to MAX_SECTO
16746 52 5f 53 49 5a 45 20 69 66 20 69 74 0a 2a 2a 20 R_SIZE if it.**
16747 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
16748 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 2e MAX_SECTOR_SIZE.
16749 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
1674a 6c 65 20 68 61 73 20 74 68 65 20 53 51 4c 49 54 le has the SQLIT
1674b 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52 53 41 46 E_IOCAP_POWERSAF
1674c 45 5f 4f 56 45 52 57 52 49 54 45 20 70 72 6f 70 E_OVERWRITE prop
1674d 65 72 74 79 2c 20 74 68 65 6e 20 73 65 74 0a 2a erty, then set.*
1674e 2a 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 * the effective
1674f 73 65 63 74 6f 72 20 73 69 7a 65 20 74 6f 20 69 sector size to i
16750 74 73 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 ts minimum value
16751 20 28 35 31 32 29 2e 20 20 54 68 65 20 70 75 72 (512). The pur
16752 70 6f 73 65 20 6f 66 0a 2a 2a 20 70 50 61 67 65 pose of.** pPage
16753 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 69 73 r->sectorSize is
16754 20 74 6f 20 64 65 66 69 6e 65 20 74 68 65 20 22 to define the "
16755 62 6c 61 73 74 20 72 61 64 69 75 73 22 20 6f 66 blast radius" of
16756 20 62 79 74 65 73 20 74 68 61 74 0a 2a 2a 20 6d bytes that.** m
16757 69 67 68 74 20 63 68 61 6e 67 65 20 69 66 20 61 ight change if a
16758 20 63 72 61 73 68 20 6f 63 63 75 72 73 20 77 68 crash occurs wh
16759 69 6c 65 20 77 72 69 74 69 6e 67 20 74 6f 20 61 ile writing to a
1675a 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 0a single byte in.
1675b 2a 2a 20 74 68 61 74 20 72 61 6e 67 65 2e 20 20 ** that range.
1675c 42 75 74 20 77 69 74 68 20 50 4f 57 45 52 53 41 But with POWERSA
1675d 46 45 5f 4f 56 45 52 57 52 49 54 45 2c 20 74 68 FE_OVERWRITE, th
1675e 65 20 62 6c 61 73 74 20 72 61 64 69 75 73 20 69 e blast radius i
1675f 73 20 7a 65 72 6f 0a 2a 2a 20 28 74 68 61 74 20 s zero.** (that
16760 69 73 20 77 68 61 74 20 50 4f 57 45 52 53 41 46 is what POWERSAF
16761 45 5f 4f 56 45 52 57 52 49 54 45 20 6d 65 61 6e E_OVERWRITE mean
16762 73 29 2c 20 73 6f 20 77 65 20 6d 69 6e 69 6d 69 s), so we minimi
16763 7a 65 20 74 68 65 20 73 65 63 74 6f 72 0a 2a 2a ze the sector.**
16764 20 73 69 7a 65 2e 20 20 46 6f 72 20 62 61 63 6b size. For back
16765 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c wards compatibil
16766 69 74 79 20 6f 66 20 74 68 65 20 72 6f 6c 6c 62 ity of the rollb
16767 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ack journal file
16768 20 66 6f 72 6d 61 74 2c 0a 2a 2a 20 77 65 20 63 format,.** we c
16769 61 6e 6e 6f 74 20 72 65 64 75 63 65 20 74 68 65 annot reduce the
1676a 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f effective secto
1676b 72 20 73 69 7a 65 20 62 65 6c 6f 77 20 35 31 32 r size below 512
1676c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1676d 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 50 setSectorSize(P
1676e 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
1676f 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
16770 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 pPager->fd) || p
16771 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
16772 29 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 );.. if( pPager
16773 2d 3e 74 65 6d 70 46 69 6c 65 0a 20 20 20 7c 7c ->tempFile. ||
16774 20 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 (sqlite3OsDevic
16775 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
16776 28 70 50 61 67 65 72 2d 3e 66 64 29 20 26 20 0a (pPager->fd) & .
16777 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 SQ
16778 4c 49 54 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52 LITE_IOCAP_POWER
16779 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 29 21 SAFE_OVERWRITE)!
1677a 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 53 =0. ){. /* S
1677b 65 63 74 6f 72 20 73 69 7a 65 20 64 6f 65 73 6e ector size doesn
1677c 27 74 20 6d 61 74 74 65 72 20 66 6f 72 20 74 65 't matter for te
1677d 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 41 mporary files. A
1677e 6c 73 6f 2c 20 74 68 65 20 66 69 6c 65 0a 20 20 lso, the file.
1677f 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 68 61 76 ** may not hav
16780 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 79 65 e been opened ye
16781 74 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 t, in which case
16782 20 74 68 65 20 4f 73 53 65 63 74 6f 72 53 69 7a the OsSectorSiz
16783 65 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 e(). ** call
16784 77 69 6c 6c 20 73 65 67 66 61 75 6c 74 2e 20 2a will segfault. *
16785 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 /. pPager->se
16786 63 74 6f 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a ctorSize = 512;.
16787 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 }else{. pPa
16788 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 ger->sectorSize
16789 3d 20 73 71 6c 69 74 65 33 53 65 63 74 6f 72 53 = sqlite3SectorS
1678a 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b ize(pPager->fd);
1678b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c . }.}../*.** Pl
1678c 61 79 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e ayback the journ
1678d 61 6c 20 61 6e 64 20 74 68 75 73 20 72 65 73 74 al and thus rest
1678e 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ore the database
1678f 20 66 69 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 file to.** the
16790 73 74 61 74 65 20 69 74 20 77 61 73 20 69 6e 20 state it was in
16791 62 65 66 6f 72 65 20 77 65 20 73 74 61 72 74 65 before we starte
16792 64 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 73 d making changes
16793 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f . .**.** The jo
16794 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61 urnal file forma
16795 74 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a t is as follows:
16796 20 0a 2a 2a 0a 2a 2a 20 20 28 31 29 20 20 38 20 .**.** (1) 8
16797 62 79 74 65 20 70 72 65 66 69 78 2e 20 20 41 20 byte prefix. A
16798 63 6f 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61 6c copy of aJournal
16799 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 20 20 28 32 29 Magic[]..** (2)
1679a 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 4 byte big-end
1679b 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 ian integer whic
1679c 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 h is the number
1679d 6f 66 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 of valid page re
1679e 63 6f 72 64 73 0a 2a 2a 20 20 20 20 20 20 20 69 cords.** i
1679f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 n the journal.
167a0 49 66 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 If this value is
167a1 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 0xffffffff, the
167a2 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a n compute the.**
167a3 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 number of
167a4 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 72 page records fr
167a5 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 73 om the journal s
167a6 69 7a 65 2e 0a 2a 2a 20 20 28 33 29 20 20 34 20 ize..** (3) 4
167a7 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 byte big-endian
167a8 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 integer which is
167a9 20 74 68 65 20 69 6e 69 74 69 61 6c 20 76 61 6c the initial val
167aa 75 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 20 ue for the .**
167ab 20 20 20 20 20 73 61 6e 69 74 79 20 63 68 65 63 sanity chec
167ac 6b 73 75 6d 2e 0a 2a 2a 20 20 28 34 29 20 20 34 ksum..** (4) 4
167ad 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 68 byte integer wh
167ae 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 ich is the numbe
167af 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 74 72 r of pages to tr
167b0 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 20 20 uncate the.**
167b1 20 20 20 20 64 61 74 61 62 61 73 65 20 74 6f 20 database to
167b2 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 during a rollbac
167b3 6b 2e 0a 2a 2a 20 20 28 35 29 20 20 34 20 62 79 k..** (5) 4 by
167b4 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e te big-endian in
167b5 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 teger which is t
167b6 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 he sector size.
167b7 20 54 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 20 The header.**
167b8 20 20 20 20 20 69 73 20 74 68 69 73 20 6d 61 6e is this man
167b9 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e y bytes in size.
167ba 0a 2a 2a 20 20 28 36 29 20 20 34 20 62 79 74 65 .** (6) 4 byte
167bb 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 big-endian inte
167bc 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 ger which is the
167bd 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 20 page size..**
167be 28 37 29 20 20 7a 65 72 6f 20 70 61 64 64 69 6e (7) zero paddin
167bf 67 20 6f 75 74 20 74 6f 20 74 68 65 20 6e 65 78 g out to the nex
167c0 74 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 0a 2a t sector size..*
167c1 2a 20 20 28 38 29 20 20 5a 65 72 6f 20 6f 72 20 * (8) Zero or
167c2 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73 74 61 more pages insta
167c3 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f nces, each as fo
167c4 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20 llows:.**
167c5 20 2b 20 20 34 20 62 79 74 65 20 70 61 67 65 20 + 4 byte page
167c6 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20 number..**
167c7 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67 + pPager->pag
167c8 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 eSize bytes of d
167c9 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b ata..** +
167ca 20 20 34 20 62 79 74 65 20 63 68 65 63 6b 73 75 4 byte checksu
167cb 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20 m.**.** When we
167cc 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 speak of the jou
167cd 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20 rnal header, we
167ce 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74 20 37 mean the first 7
167cf 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a items above..**
167d0 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 Each entry in t
167d1 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e he journal is an
167d2 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
167d3 20 38 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 8th item..**.**
167d4 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20 Call the value
167d5 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 from the second
167d6 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20 bullet "nRec".
167d7 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62 nRec is the numb
167d8 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70 er of.** valid p
167d9 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 age entries in t
167da 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 he journal. In
167db 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20 most cases, you
167dc 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a can compute the.
167dd 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 ** value of nRec
167de 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f from the size o
167df 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
167e0 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f le. But if a po
167e1 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f wer.** failure o
167e2 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 74 68 ccurred while th
167e3 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 e journal was be
167e4 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20 ing written, it
167e5 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20 could be the.**
167e6 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 69 case that the si
167e7 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ze of the journa
167e8 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61 l file had alrea
167e9 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65 dy been increase
167ea 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74 d but.** the ext
167eb 72 61 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e ra entries had n
167ec 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74 20 73 ot yet made it s
167ed 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20 afely to disk.
167ee 49 6e 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a In such a case,.
167ef 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
167f0 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72 nRec computed fr
167f1 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 om the file size
167f2 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61 would be too la
167f3 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61 rge. For.** tha
167f4 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77 t reason, we alw
167f5 61 79 73 20 75 73 65 20 74 68 65 20 6e 52 65 63 ays use the nRec
167f6 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65 value in the he
167f7 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ader..**.** If t
167f8 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73 he nRec value is
167f9 20 30 78 66 66 66 66 66 66 66 66 20 69 74 20 6d 0xffffffff it m
167fa 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73 eans that nRec s
167fb 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 hould be compute
167fc 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 d.** from the fi
167fd 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76 le size. This v
167fe 61 6c 75 65 20 69 73 20 75 73 65 64 20 77 68 65 alue is used whe
167ff 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c 65 63 n the user selec
16800 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e ts the.** no-syn
16801 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 c option for the
16802 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 journal. A pow
16803 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 er failure could
16804 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74 lead to corrupt
16805 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 ion.** in this c
16806 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68 ase. But for th
16807 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 ings like tempor
16808 61 72 79 20 74 61 62 6c 65 20 28 77 68 69 63 68 ary table (which
16809 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 will be.** dele
1680a 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77 ted when the pow
1680b 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 29 20 er is restored)
1680c 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 we don't care.
1680d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
1680e 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65 le opened as the
1680f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
16810 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d not a well-form
16811 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 ed.** journal fi
16812 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 le then all page
16813 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 s up to the firs
16814 74 20 63 6f 72 72 75 70 74 65 64 20 70 61 67 65 t corrupted page
16815 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 are rolled.** b
16816 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73 ack (or no pages
16817 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 if the journal
16818 68 65 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 header is corrup
16819 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61 ted). The journa
1681a 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65 l file.** is the
1681b 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 n deleted and SQ
1681c 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 LITE_OK returned
1681d 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20 , just as if no
1681e 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a corruption had.*
1681f 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 * been encounter
16820 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ed..**.** If an
16821 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 I/O or malloc()
16822 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 error occurs, th
16823 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 e journal-file i
16824 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a s not deleted.**
16825 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f and an error co
16826 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a de is returned..
16827 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 20 **.** The isHot
16828 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 parameter indica
16829 74 65 73 20 74 68 61 74 20 77 65 20 61 72 65 20 tes that we are
1682a 74 72 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 trying to rollba
1682b 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 ck a journal.**
1682c 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 20 that might be a
1682d 68 6f 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72 hot journal. Or
1682e 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 , it could be th
1682f 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 at the journal i
16830 73 20 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 20 s .** preserved
16831 62 65 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e because of JOURN
16832 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 6f ALMODE_PERSIST o
16833 72 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 r JOURNALMODE_TR
16834 55 4e 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68 UNCATE..** If th
16835 65 20 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79 e journal really
16836 20 69 73 20 68 6f 74 2c 20 72 65 73 65 74 20 74 is hot, reset t
16837 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 70 he pager cache p
16838 72 69 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 rior rolling.**
16839 62 61 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74 back any content
1683a 2e 20 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 . If the journa
1683b 6c 20 69 73 20 6d 65 72 65 6c 79 20 70 65 72 73 l is merely pers
1683c 69 73 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74 istent, no reset
1683d 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a is.** needed..*
1683e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
1683f 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65 er_playback(Page
16840 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 r *pPager, int i
16841 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 sHot){. sqlite3
16842 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 _vfs *pVfs = pPa
16843 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34 ger->pVfs;. i64
16844 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 szJ;
16845 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
16846 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
16847 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 e in bytes */.
16848 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20 20 20 u32 nRec;
16849 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1684a 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20 69 6e er of Records in
1684b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a the journal */.
1684c 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 u32 u;
1684d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e /* Un
1684e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e signed loop coun
1684f 74 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 ter */. Pgno mx
16850 50 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 Pg = 0;
16851 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
16852 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 69 original file i
16853 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 n pages */. int
16854 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
16855 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 /* Result
16856 63 6f 64 65 20 6f 66 20 61 20 73 75 62 72 6f 75 code of a subrou
16857 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 tine */. int re
16858 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 s = 1;
16859 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 /* Value retu
1685a 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f rned by sqlite3O
1685b 73 41 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 63 sAccess() */. c
1685c 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 har *zMaster = 0
1685d 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 ; /* Name
1685e 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 of master journa
1685f 6c 20 66 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f l file if any */
16860 0a 20 20 69 6e 74 20 6e 65 65 64 50 61 67 65 72 . int needPager
16861 52 65 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 Reset; /* T
16862 72 75 65 20 74 6f 20 72 65 73 65 74 20 70 61 67 rue to reset pag
16863 65 20 70 72 69 6f 72 20 74 6f 20 66 69 72 73 74 e prior to first
16864 20 70 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a page rollback *
16865 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f /.. /* Figure o
16866 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f ut how many reco
16867 72 64 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a rds are in the j
16868 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65 ournal. Abort e
16869 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65 arly if. ** the
1686a 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 journal is empt
1686b 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 y.. */. assert
1686c 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
1686d 3e 6a 66 64 29 20 29 3b 0a 20 20 72 63 20 3d 20 >jfd) );. rc =
1686e 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
1686f 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 e(pPager->jfd, &
16870 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d szJ);. if( rc!=
16871 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
16872 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 goto end_playba
16873 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 ck;. }.. /* Re
16874 61 64 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f ad the master jo
16875 75 72 6e 61 6c 20 6e 61 6d 65 20 66 72 6f 6d 20 urnal name from
16876 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 the journal, if
16877 69 74 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 it is present..
16878 20 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 ** If a master
16879 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d journal file nam
1687a 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 e is specified,
1687b 62 75 74 20 74 68 65 20 66 69 6c 65 20 69 73 20 but the file is
1687c 6e 6f 74 0a 20 20 2a 2a 20 70 72 65 73 65 6e 74 not. ** present
1687d 20 6f 6e 20 64 69 73 6b 2c 20 74 68 65 6e 20 74 on disk, then t
1687e 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f he journal is no
1687f 74 20 68 6f 74 20 61 6e 64 20 64 6f 65 73 20 6e t hot and does n
16880 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 ot need to be.
16881 2a 2a 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a ** played back..
16882 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 **. ** TODO:
16883 54 65 63 68 6e 69 63 61 6c 6c 79 20 74 68 65 20 Technically the
16884 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 6e 20 following is an
16885 65 72 72 6f 72 20 62 65 63 61 75 73 65 20 69 74 error because it
16886 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 20 20 assumes that.
16887 2a 2a 20 62 75 66 66 65 72 20 50 61 67 65 72 2e ** buffer Pager.
16888 70 54 6d 70 53 70 61 63 65 20 69 73 20 28 6d 78 pTmpSpace is (mx
16889 50 61 74 68 6e 61 6d 65 2b 31 29 20 62 79 74 65 Pathname+1) byte
1688a 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 69 2e 65 s or larger. i.e
1688b 2e 20 74 68 61 74 0a 20 20 2a 2a 20 28 70 50 61 . that. ** (pPa
1688c 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3e 3d ger->pageSize >=
1688d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d pPager->pVfs->m
1688e 78 50 61 74 68 6e 61 6d 65 2b 31 29 2e 20 55 73 xPathname+1). Us
1688f 69 6e 67 20 6f 73 5f 75 6e 69 78 2e 63 2c 0a 20 ing os_unix.c,.
16890 20 2a 2a 20 20 6d 78 50 61 74 68 6e 61 6d 65 20 ** mxPathname
16891 69 73 20 35 31 32 2c 20 77 68 69 63 68 20 69 73 is 512, which is
16892 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 the same as the
16893 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 61 62 minimum allowab
16894 6c 65 20 76 61 6c 75 65 0a 20 20 2a 2a 20 66 6f le value. ** fo
16895 72 20 70 61 67 65 53 69 7a 65 2e 0a 20 20 2a 2f r pageSize.. */
16896 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61 . zMaster = pPa
16897 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a ger->pTmpSpace;.
16898 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 rc = readMaste
16899 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d rJournal(pPager-
1689a 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 >jfd, zMaster, p
1689b 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 Pager->pVfs->mxP
1689c 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 69 66 athname+1);. if
1689d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1689e 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 29 7b && zMaster[0] ){
1689f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
168a0 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 3OsAccess(pVfs,
168a1 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f zMaster, SQLITE_
168a2 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 ACCESS_EXISTS, &
168a3 72 65 73 29 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 res);. }. zMas
168a4 74 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 72 ter = 0;. if( r
168a5 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c!=SQLITE_OK ||
168a6 21 72 65 73 20 29 7b 0a 20 20 20 20 67 6f 74 6f !res ){. goto
168a7 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 end_playback;.
168a8 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 }. pPager->jou
168a9 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 6e rnalOff = 0;. n
168aa 65 65 64 50 61 67 65 72 52 65 73 65 74 20 3d 20 eedPagerReset =
168ab 69 73 48 6f 74 3b 0a 0a 20 20 2f 2a 20 54 68 69 isHot;.. /* Thi
168ac 73 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 74 65 s loop terminate
168ad 73 20 65 69 74 68 65 72 20 77 68 65 6e 20 61 20 s either when a
168ae 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 29 readJournalHdr()
168af 20 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 72 5f or . ** pager_
168b0 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 playback_one_pag
168b1 65 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 e() call returns
168b2 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 SQLITE_DONE or
168b3 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a 20 20 2a an IO error . *
168b4 2a 20 6f 63 63 75 72 73 2e 20 0a 20 20 2a 2f 0a * occurs. . */.
168b5 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 while( 1 ){.
168b6 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65 /* Read the ne
168b7 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 xt journal heade
168b8 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e r from the journ
168b9 61 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 al file. If the
168ba 72 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f re are. ** no
168bb 74 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c t enough bytes l
168bc 65 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e eft in the journ
168bd 61 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f al file for a co
168be 6d 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f mplete header, o
168bf 72 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63 r. ** it is c
168c0 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61 orrupted, then a
168c1 20 70 72 6f 63 65 73 73 20 6d 75 73 74 20 68 61 process must ha
168c2 76 65 20 66 61 69 6c 65 64 20 77 68 69 6c 65 20 ve failed while
168c3 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 writing it..
168c4 2a 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 65 ** This indicate
168c5 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e s nothing more n
168c6 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 eeds to be rolle
168c7 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 d back.. */.
168c8 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 rc = readJour
168c9 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 69 nalHdr(pPager, i
168ca 73 48 6f 74 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 sHot, szJ, &nRec
168cb 2c 20 26 6d 78 50 67 29 3b 0a 20 20 20 20 69 66 , &mxPg);. if
168cc 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
168cd 29 7b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63 ){ . if( rc
168ce 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b ==SQLITE_DONE ){
168cf 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
168d0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d LITE_OK;. }
168d1 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f . goto end_
168d2 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a playback;. }.
168d3 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 . /* If nRec
168d4 69 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 is 0xffffffff, t
168d5 68 65 6e 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c hen this journal
168d6 20 77 61 73 20 63 72 65 61 74 65 64 20 62 79 20 was created by
168d7 61 20 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a a process. **
168d8 20 77 6f 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 working in no-s
168d9 79 6e 63 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d ync mode. This m
168da 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 72 65 eans that the re
168db 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 st of the journa
168dc 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f l. ** file co
168dd 6e 73 69 73 74 73 20 6f 66 20 70 61 67 65 73 2c nsists of pages,
168de 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f there are no mo
168df 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 re journal heade
168e0 72 73 2e 20 43 6f 6d 70 75 74 65 0a 20 20 20 20 rs. Compute.
168e1 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
168e2 6e 52 65 63 20 62 61 73 65 64 20 6f 6e 20 74 68 nRec based on th
168e3 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 is assumption..
168e4 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 */. if( nR
168e5 65 63 3d 3d 30 78 66 66 66 66 66 66 66 66 20 29 ec==0xffffffff )
168e6 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
168e7 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
168e8 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f ff==JOURNAL_HDR_
168e9 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 SZ(pPager) );.
168ea 20 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 nRec = (int)
168eb 28 28 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f ((szJ - JOURNAL_
168ec 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 29 2f HDR_SZ(pPager))/
168ed 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 JOURNAL_PG_SZ(pP
168ee 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 ager));. }..
168ef 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 /* If nRec is
168f0 20 30 20 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c 0 and this roll
168f1 62 61 63 6b 20 69 73 20 6f 66 20 61 20 74 72 61 back is of a tra
168f2 6e 73 61 63 74 69 6f 6e 20 63 72 65 61 74 65 64 nsaction created
168f3 20 62 79 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 by this. **
168f4 70 72 6f 63 65 73 73 20 61 6e 64 20 69 66 20 74 process and if t
168f5 68 69 73 20 69 73 20 74 68 65 20 66 69 6e 61 6c his is the final
168f6 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 6a header in the j
168f7 6f 75 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20 ournal, then it
168f8 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61 means. ** tha
168f9 74 20 74 68 69 73 20 70 61 72 74 20 6f 66 20 74 t this part of t
168fa 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 he journal was b
168fb 65 69 6e 67 20 66 69 6c 6c 65 64 20 62 75 74 20 eing filled but
168fc 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e has not yet been
168fd 0a 20 20 20 20 2a 2a 20 73 79 6e 63 65 64 20 74 . ** synced t
168fe 6f 20 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65 o disk. Compute
168ff 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
16900 61 67 65 73 20 62 61 73 65 64 20 6f 6e 20 74 68 ages based on th
16901 65 20 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20 e remaining.
16902 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 ** size of the f
16903 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ile.. **.
16904 2a 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 72 ** The third ter
16905 6d 20 6f 66 20 74 68 65 20 74 65 73 74 20 77 61 m of the test wa
16906 73 20 61 64 64 65 64 20 74 6f 20 66 69 78 20 74 s added to fix t
16907 69 63 6b 65 74 20 23 32 35 36 35 2e 0a 20 20 20 icket #2565..
16908 20 2a 2a 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 ** When rolling
16909 20 62 61 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72 back a hot jour
1690a 6e 61 6c 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77 nal, nRec==0 alw
1690b 61 79 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 ays means that t
1690c 68 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63 he next. ** c
1690d 68 75 6e 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 hunk of the jour
1690e 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 nal contains zer
1690f 6f 20 70 61 67 65 73 20 74 6f 20 62 65 20 72 6f o pages to be ro
16910 6c 6c 65 64 20 62 61 63 6b 2e 20 20 42 75 74 0a lled back. But.
16911 20 20 20 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e ** when doin
16912 67 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 g a ROLLBACK and
16913 20 74 68 65 20 6e 52 65 63 3d 3d 30 20 63 68 75 the nRec==0 chu
16914 6e 6b 20 69 73 20 74 68 65 20 6c 61 73 74 20 63 nk is the last c
16915 68 75 6e 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74 hunk in. ** t
16916 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d he journal, it m
16917 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6a 6f eans that the jo
16918 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 urnal might cont
16919 61 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20 ain additional.
1691a 20 20 20 2a 2a 20 70 61 67 65 73 20 74 68 61 74 ** pages that
1691b 20 6e 65 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c need to be roll
1691c 65 64 20 62 61 63 6b 20 61 6e 64 20 74 68 61 74 ed back and that
1691d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
1691e 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f ages . ** sho
1691f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 20 uld be computed
16920 62 61 73 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 based on the jou
16921 72 6e 61 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a rnal file size..
16922 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e */. if( n
16923 52 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 Rec==0 && !isHot
16924 20 26 26 0a 20 20 20 20 20 20 20 20 70 50 61 67 &&. pPag
16925 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a er->journalHdr+J
16926 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
16927 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a ager)==pPager->j
16928 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 ournalOff ){.
16929 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 nRec = (int)(
1692a 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a (szJ - pPager->j
1692b 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 ournalOff) / JOU
1692c 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 RNAL_PG_SZ(pPage
1692d 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 r));. }..
1692e 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68 /* If this is th
1692f 65 20 66 69 72 73 74 20 68 65 61 64 65 72 20 72 e first header r
16930 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 ead from the jou
16931 72 6e 61 6c 2c 20 74 72 75 6e 63 61 74 65 20 74 rnal, truncate t
16932 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 he. ** databa
16933 73 65 20 66 69 6c 65 20 62 61 63 6b 20 74 6f 20 se file back to
16934 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a its original siz
16935 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 e.. */. if
16936 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
16937 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 lOff==JOURNAL_HD
16938 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a R_SZ(pPager) ){.
16939 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
1693a 5f 74 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 _truncate(pPager
1693b 2c 20 6d 78 50 67 29 3b 0a 20 20 20 20 20 20 69 , mxPg);. i
1693c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1693d 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f ){. goto
1693e 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 end_playback;.
1693f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 }. pPa
16940 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 78 ger->dbSize = mx
16941 50 67 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f Pg;. }.. /
16942 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 * Copy original
16943 70 61 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65 pages out of the
16944 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 journal and bac
16945 6b 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 k into the .
16946 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
16947 20 61 6e 64 2f 6f 72 20 70 61 67 65 20 63 61 63 and/or page cac
16948 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 he.. */. f
16949 6f 72 28 75 3d 30 3b 20 75 3c 6e 52 65 63 3b 20 or(u=0; u<nRec;
1694a 75 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 u++){. if(
1694b 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20 29 needPagerReset )
1694c 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 5f {. pager_
1694d 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 reset(pPager);.
1694e 20 20 20 20 20 20 20 6e 65 65 64 50 61 67 65 72 needPager
1694f 52 65 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 Reset = 0;.
16950 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 }. rc = pa
16951 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 ger_playback_one
16952 5f 70 61 67 65 28 70 50 61 67 65 72 2c 26 70 50 _page(pPager,&pP
16953 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
16954 2c 30 2c 31 2c 30 29 3b 0a 20 20 20 20 20 20 69 ,0,1,0);. i
16955 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
16956 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
16957 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc==SQLITE_DONE
16958 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 ){. pPa
16959 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
1695a 3d 20 73 7a 4a 3b 0a 20 20 20 20 20 20 20 20 20 = szJ;.
1695b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1695c 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 }else if( rc==SQ
1695d 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
1695e 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20 _READ ){.
1695f 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 /* If the jou
16960 72 6e 61 6c 20 68 61 73 20 62 65 65 6e 20 74 72 rnal has been tr
16961 75 6e 63 61 74 65 64 2c 20 73 69 6d 70 6c 79 20 uncated, simply
16962 73 74 6f 70 20 72 65 61 64 69 6e 67 20 61 6e 64 stop reading and
16963 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 72 . ** pr
16964 6f 63 65 73 73 69 6e 67 20 74 68 65 20 6a 6f 75 ocessing the jou
16965 72 6e 61 6c 2e 20 54 68 69 73 20 6d 69 67 68 74 rnal. This might
16966 20 68 61 70 70 65 6e 20 69 66 20 74 68 65 20 6a happen if the j
16967 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20 20 20 20 ournal was.
16968 20 20 20 20 20 2a 2a 20 6e 6f 74 20 63 6f 6d 70 ** not comp
16969 6c 65 74 65 6c 79 20 77 72 69 74 74 65 6e 20 61 letely written a
1696a 6e 64 20 73 79 6e 63 65 64 20 70 72 69 6f 72 20 nd synced prior
1696b 74 6f 20 61 20 63 72 61 73 68 2e 20 20 49 6e 20 to a crash. In
1696c 74 68 61 74 0a 20 20 20 20 20 20 20 20 20 20 2a that. *
1696d 2a 20 63 61 73 65 2c 20 74 68 65 20 64 61 74 61 * case, the data
1696e 62 61 73 65 20 73 68 6f 75 6c 64 20 68 61 76 65 base should have
1696f 20 6e 65 76 65 72 20 62 65 65 6e 20 77 72 69 74 never been writ
16970 74 65 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 20 ten in the.
16971 20 20 20 20 20 2a 2a 20 66 69 72 73 74 20 70 6c ** first pl
16972 61 63 65 20 73 6f 20 69 74 20 69 73 20 4f 4b 20 ace so it is OK
16973 74 6f 20 73 69 6d 70 6c 79 20 61 62 61 6e 64 6f to simply abando
16974 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 n the rollback.
16975 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 */. rc
16976 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
16977 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
16978 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 playback;.
16979 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1697a 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 /* If we are
1697b 75 6e 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 unable to rollba
1697c 63 6b 2c 20 71 75 69 74 20 61 6e 64 20 72 65 74 ck, quit and ret
1697d 75 72 6e 20 74 68 65 20 65 72 72 6f 72 0a 20 20 urn the error.
1697e 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 2e ** code.
1697f 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 This will caus
16980 65 20 74 68 65 20 70 61 67 65 72 20 74 6f 20 65 e the pager to e
16981 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 nter the error s
16982 74 61 74 65 0a 20 20 20 20 20 20 20 20 20 20 2a tate. *
16983 2a 20 73 6f 20 74 68 61 74 20 6e 6f 20 66 75 72 * so that no fur
16984 74 68 65 72 20 68 61 72 6d 20 77 69 6c 6c 20 62 ther harm will b
16985 65 20 64 6f 6e 65 2e 20 20 50 65 72 68 61 70 73 e done. Perhaps
16986 20 74 68 65 20 6e 65 78 74 0a 20 20 20 20 20 20 the next.
16987 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 74 ** process t
16988 6f 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 77 69 6c o come along wil
16989 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c l be able to rol
1698a 6c 62 61 63 6b 20 74 68 65 20 64 61 74 61 62 61 lback the databa
1698b 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f se.. */
1698c 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
1698d 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 end_playback;.
1698e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1698f 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f }. }. /*NO
16990 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 61 73 73 TREACHED*/. ass
16991 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64 5f 70 ert( 0 );..end_p
16992 6c 61 79 62 61 63 6b 3a 0a 20 20 2f 2a 20 46 6f layback:. /* Fo
16993 6c 6c 6f 77 69 6e 67 20 61 20 72 6f 6c 6c 62 61 llowing a rollba
16994 63 6b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 ck, the database
16995 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 file should be
16996 62 61 63 6b 20 69 6e 20 69 74 73 20 6f 72 69 67 back in its orig
16997 69 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 74 65 20 inal. ** state
16998 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61 prior to the sta
16999 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 rt of the transa
1699a 63 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f 6b 65 ction, so invoke
1699b 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 the. ** SQLITE
1699c 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e _FCNTL_DB_UNCHAN
1699d 47 45 44 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c GED file-control
1699e 20 6d 65 74 68 6f 64 20 74 6f 20 64 69 73 61 62 method to disab
1699f 6c 65 20 74 68 65 0a 20 20 2a 2a 20 61 73 73 65 le the. ** asse
169a0 72 74 69 6f 6e 20 74 68 61 74 20 74 68 65 20 74 rtion that the t
169a1 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 ransaction count
169a2 65 72 20 77 61 73 20 6d 6f 64 69 66 69 65 64 2e er was modified.
169a3 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c . */.#ifdef SQL
169a4 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 ITE_DEBUG. if(
169a5 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 pPager->fd->pMet
169a6 68 6f 64 73 20 29 7b 0a 20 20 20 20 73 71 6c 69 hods ){. sqli
169a7 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c te3OsFileControl
169a8 48 69 6e 74 28 70 50 61 67 65 72 2d 3e 66 64 2c Hint(pPager->fd,
169a9 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f SQLITE_FCNTL_DB_
169aa 55 4e 43 48 41 4e 47 45 44 2c 30 29 3b 0a 20 20 UNCHANGED,0);.
169ab 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 }.#endif.. /* I
169ac 66 20 74 68 69 73 20 70 6c 61 79 62 61 63 6b 20 f this playback
169ad 69 73 20 68 61 70 70 65 6e 69 6e 67 20 61 75 74 is happening aut
169ae 6f 6d 61 74 69 63 61 6c 6c 79 20 61 73 20 61 20 omatically as a
169af 72 65 73 75 6c 74 20 6f 66 20 61 6e 20 49 4f 20 result of an IO
169b0 6f 72 20 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 or . ** malloc
169b1 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 error that occur
169b2 72 65 64 20 61 66 74 65 72 20 74 68 65 20 63 68 red after the ch
169b3 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 77 61 73 ange-counter was
169b4 20 75 70 64 61 74 65 64 20 62 75 74 20 0a 20 20 updated but .
169b5 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 ** before the tr
169b6 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 63 6f ansaction was co
169b7 6d 6d 69 74 74 65 64 2c 20 74 68 65 6e 20 74 68 mmitted, then th
169b8 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
169b9 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63 61 74 . ** modificat
169ba 69 6f 6e 20 6d 61 79 20 6a 75 73 74 20 68 61 76 ion may just hav
169bb 65 20 62 65 65 6e 20 72 65 76 65 72 74 65 64 2e e been reverted.
169bc 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 If this happens
169bd 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 0a 20 in exclusive .
169be 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e 20 73 ** mode, then s
169bf 75 62 73 65 71 75 65 6e 74 20 74 72 61 6e 73 61 ubsequent transa
169c0 63 74 69 6f 6e 73 20 70 65 72 66 6f 72 6d 65 64 ctions performed
169c1 20 62 79 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 by the connecti
169c2 6f 6e 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a on will not. **
169c3 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e update the chan
169c4 67 65 2d 63 6f 75 6e 74 65 72 20 61 74 20 61 6c ge-counter at al
169c5 6c 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 64 l. This may lead
169c6 20 74 6f 20 63 61 63 68 65 20 69 6e 63 6f 6e 73 to cache incons
169c7 69 73 74 65 6e 63 79 0a 20 20 2a 2a 20 70 72 6f istency. ** pro
169c8 62 6c 65 6d 73 20 66 6f 72 20 6f 74 68 65 72 20 blems for other
169c9 70 72 6f 63 65 73 73 65 73 20 61 74 20 73 6f 6d processes at som
169ca 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 e point in the f
169cb 75 74 75 72 65 2e 20 53 6f 2c 20 6a 75 73 74 0a uture. So, just.
169cc 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 69 ** in case thi
169cd 73 20 68 61 73 20 68 61 70 70 65 6e 65 64 2c 20 s has happened,
169ce 63 6c 65 61 72 20 74 68 65 20 63 68 61 6e 67 65 clear the change
169cf 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 6e CountDone flag n
169d0 6f 77 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 ow.. */. pPage
169d1 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f r->changeCountDo
169d2 6e 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d ne = pPager->tem
169d3 70 46 69 6c 65 3b 0a 0a 20 20 69 66 28 20 72 63 pFile;.. if( rc
169d4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
169d5 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61 zMaster = pPa
169d6 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a ger->pTmpSpace;.
169d7 20 20 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 rc = readMas
169d8 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 terJournal(pPage
169d9 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c r->jfd, zMaster,
169da 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d pPager->pVfs->m
169db 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 xPathname+1);.
169dc 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d testcase( rc!=
169dd 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d SQLITE_OK );. }
169de 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
169df 45 5f 4f 4b 0a 20 20 20 26 26 20 28 70 50 61 67 E_OK. && (pPag
169e0 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 er->eState>=PAGE
169e1 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 7c R_WRITER_DBMOD |
169e2 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 | pPager->eState
169e3 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 29 0a 20 20 ==PAGER_OPEN).
169e4 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
169e5 74 65 33 50 61 67 65 72 53 79 6e 63 28 70 50 61 te3PagerSync(pPa
169e6 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ger);. }. if(
169e7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
169e8 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f . rc = pager_
169e9 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 end_transaction(
169ea 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 5b pPager, zMaster[
169eb 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 20 20 74 0]!='\0');. t
169ec 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c estcase( rc!=SQL
169ed 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 ITE_OK );. }.
169ee 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
169ef 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 K && zMaster[0]
169f0 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a && res ){. /*
169f1 20 49 66 20 74 68 65 72 65 20 77 61 73 20 61 20 If there was a
169f2 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 master journal a
169f3 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 nd this routine
169f4 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 75 63 63 will return succ
169f5 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20 ess,. ** see
169f6 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c if it is possibl
169f7 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 e to delete the
169f8 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a master journal..
169f9 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
169fa 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 pager_delmaster(
169fb 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 pPager, zMaster)
169fc 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
169fd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc!=SQLITE_OK );
169fe 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50 . }.. /* The P
169ff 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 ager.sectorSize
16a00 76 61 72 69 61 62 6c 65 20 6d 61 79 20 68 61 76 variable may hav
16a01 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 20 77 e been updated w
16a02 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a hile rolling. *
16a03 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c * back a journal
16a04 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 created by a pr
16a05 6f 63 65 73 73 20 77 69 74 68 20 61 20 64 69 66 ocess with a dif
16a06 66 65 72 65 6e 74 20 73 65 63 74 6f 72 20 73 69 ferent sector si
16a07 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52 ze. ** value. R
16a08 65 73 65 74 20 69 74 20 74 6f 20 74 68 65 20 63 eset it to the c
16a09 6f 72 72 65 63 74 20 76 61 6c 75 65 20 66 6f 72 orrect value for
16a0a 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e 0a 20 this process..
16a0b 20 2a 2f 0a 20 20 73 65 74 53 65 63 74 6f 72 53 */. setSectorS
16a0c 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 ize(pPager);. r
16a0d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
16a0e 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 63 6f 6e .** Read the con
16a0f 74 65 6e 74 20 66 6f 72 20 70 61 67 65 20 70 50 tent for page pP
16a10 67 20 6f 75 74 20 6f 66 20 74 68 65 20 64 61 74 g out of the dat
16a11 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 69 abase file and i
16a12 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d 3e 70 44 61 nto .** pPg->pDa
16a13 74 61 2e 20 41 20 73 68 61 72 65 64 20 6c 6f 63 ta. A shared loc
16a14 6b 20 6f 72 20 67 72 65 61 74 65 72 20 6d 75 73 k or greater mus
16a15 74 20 62 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 t be held on the
16a16 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c database.** fil
16a17 65 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 e before this fu
16a18 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
16a19 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 67 65 20 ..**.** If page
16a1a 31 20 69 73 20 72 65 61 64 2c 20 74 68 65 6e 20 1 is read, then
16a1b 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 67 the value of Pag
16a1c 65 72 2e 64 62 46 69 6c 65 56 65 72 73 5b 5d 20 er.dbFileVers[]
16a1d 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 74 68 65 is set to.** the
16a1e 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d value read from
16a1f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
16a20 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 le..**.** If an
16a21 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c IO error occurs,
16a22 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 then the IO err
16a23 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 or is returned t
16a24 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a o the caller..**
16a25 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 Otherwise, SQLI
16a26 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
16a27 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
16a28 20 72 65 61 64 44 62 50 61 67 65 28 50 67 48 64 readDbPage(PgHd
16a29 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 r *pPg){. Pager
16a2a 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e *pPager = pPg->
16a2b 70 50 61 67 65 72 3b 20 2f 2a 20 50 61 67 65 72 pPager; /* Pager
16a2c 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 object associat
16a2d 65 64 20 77 69 74 68 20 70 61 67 65 20 70 50 67 ed with page pPg
16a2e 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 */. Pgno pgno
16a2f 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 20 20 20 20 = pPg->pgno;
16a30 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 /* Page numbe
16a31 72 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 69 r to read */. i
16a32 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
16a33 4b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 K; /* R
16a34 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
16a35 69 6e 74 20 69 73 49 6e 57 61 6c 20 3d 20 30 3b int isInWal = 0;
16a36 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16a37 54 72 75 65 20 69 66 20 70 61 67 65 20 69 73 20 True if page is
16a38 69 6e 20 6c 6f 67 20 66 69 6c 65 20 2a 2f 0a 20 in log file */.
16a39 20 69 6e 74 20 70 67 73 7a 20 3d 20 70 50 61 67 int pgsz = pPag
16a3a 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 20 2f 2a er->pageSize; /*
16a3b 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
16a3c 20 74 6f 20 72 65 61 64 20 2a 2f 0a 0a 20 20 61 to read */.. a
16a3d 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 ssert( pPager->e
16a3e 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 41 State>=PAGER_REA
16a3f 44 45 52 20 26 26 20 21 4d 45 4d 44 42 20 29 3b DER && !MEMDB );
16a40 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 . assert( isOpe
16a41 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b n(pPager->fd) );
16a42 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 21 69 .. if( NEVER(!i
16a43 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
16a44 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 )) ){. assert
16a45 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 ( pPager->tempFi
16a46 6c 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 le );. memset
16a47 28 70 50 67 2d 3e 70 44 61 74 61 2c 20 30 2c 20 (pPg->pData, 0,
16a48 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
16a49 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
16a4a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
16a4b 69 66 28 20 70 61 67 65 72 55 73 65 57 61 6c 28 if( pagerUseWal(
16a4c 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 2f pPager) ){. /
16a4d 2a 20 54 72 79 20 74 6f 20 70 75 6c 6c 20 74 68 * Try to pull th
16a4e 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 e page from the
16a4f 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 2e write-ahead log.
16a50 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
16a51 69 74 65 33 57 61 6c 52 65 61 64 28 70 50 61 67 ite3WalRead(pPag
16a52 65 72 2d 3e 70 57 61 6c 2c 20 70 67 6e 6f 2c 20 er->pWal, pgno,
16a53 26 69 73 49 6e 57 61 6c 2c 20 70 67 73 7a 2c 20 &isInWal, pgsz,
16a54 70 50 67 2d 3e 70 44 61 74 61 29 3b 0a 20 20 7d pPg->pData);. }
16a55 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
16a56 45 5f 4f 4b 20 26 26 20 21 69 73 49 6e 57 61 6c E_OK && !isInWal
16a57 20 29 7b 0a 20 20 20 20 69 36 34 20 69 4f 66 66 ){. i64 iOff
16a58 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 set = (pgno-1)*(
16a59 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 i64)pPager->page
16a5a 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 Size;. rc = s
16a5b 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 qlite3OsRead(pPa
16a5c 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d 3e 70 44 ger->fd, pPg->pD
16a5d 61 74 61 2c 20 70 67 73 7a 2c 20 69 4f 66 66 73 ata, pgsz, iOffs
16a5e 65 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d et);. if( rc=
16a5f 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 =SQLITE_IOERR_SH
16a60 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 ORT_READ ){.
16a61 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
16a62 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 ;. }. }.. i
16a63 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 f( pgno==1 ){.
16a64 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
16a65 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 61 64 /* If the read
16a66 20 69 73 20 75 6e 73 75 63 63 65 73 73 66 75 6c is unsuccessful
16a67 2c 20 73 65 74 20 74 68 65 20 64 62 46 69 6c 65 , set the dbFile
16a68 56 65 72 73 5b 5d 20 74 6f 20 73 6f 6d 65 74 68 Vers[] to someth
16a69 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 ing. ** tha
16a6a 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 t will never be
16a6b 61 20 76 61 6c 69 64 20 66 69 6c 65 20 76 65 72 a valid file ver
16a6c 73 69 6f 6e 2e 20 20 64 62 46 69 6c 65 56 65 72 sion. dbFileVer
16a6d 73 5b 5d 20 69 73 20 61 20 63 6f 70 79 0a 20 20 s[] is a copy.
16a6e 20 20 20 20 2a 2a 20 6f 66 20 62 79 74 65 73 20 ** of bytes
16a6f 32 34 2e 2e 33 39 20 6f 66 20 74 68 65 20 64 61 24..39 of the da
16a70 74 61 62 61 73 65 2e 20 20 42 79 74 65 73 20 32 tabase. Bytes 2
16a71 38 2e 2e 33 31 20 73 68 6f 75 6c 64 20 61 6c 77 8..31 should alw
16a72 61 79 73 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 ays be. **
16a73 7a 65 72 6f 20 6f 72 20 74 68 65 20 73 69 7a 65 zero or the size
16a74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
16a75 20 69 6e 20 70 61 67 65 2e 20 42 79 74 65 73 20 in page. Bytes
16a76 33 32 2e 2e 33 35 20 61 6e 64 20 33 35 2e 2e 33 32..35 and 35..3
16a77 39 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f 75 6c 9. ** shoul
16a78 64 20 62 65 20 70 61 67 65 20 6e 75 6d 62 65 72 d be page number
16a79 73 20 77 68 69 63 68 20 61 72 65 20 6e 65 76 65 s which are neve
16a7a 72 20 30 78 66 66 66 66 66 66 66 66 2e 20 20 53 r 0xffffffff. S
16a7b 6f 20 66 69 6c 6c 69 6e 67 0a 20 20 20 20 20 20 o filling.
16a7c 2a 2a 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c ** pPager->dbFil
16a7d 65 56 65 72 73 5b 5d 20 77 69 74 68 20 61 6c 6c eVers[] with all
16a7e 20 30 78 66 66 20 62 79 74 65 73 20 73 68 6f 75 0xff bytes shou
16a7f 6c 64 20 73 75 66 66 69 63 65 2e 0a 20 20 20 20 ld suffice..
16a80 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f **. ** Fo
16a81 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64 r an encrypted d
16a82 61 74 61 62 61 73 65 2c 20 74 68 65 20 73 69 74 atabase, the sit
16a83 75 61 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 63 uation is more c
16a84 6f 6d 70 6c 65 78 3a 20 20 62 79 74 65 73 0a 20 omplex: bytes.
16a85 20 20 20 20 20 2a 2a 20 32 34 2e 2e 33 39 20 6f ** 24..39 o
16a86 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 f the database a
16a87 72 65 20 77 68 69 74 65 20 6e 6f 69 73 65 2e 20 re white noise.
16a88 20 42 75 74 20 74 68 65 20 70 72 6f 62 61 62 69 But the probabi
16a89 6c 69 74 79 20 6f 66 0a 20 20 20 20 20 20 2a 2a lity of. **
16a8a 20 77 68 69 74 65 20 6e 6f 69 73 69 6e 67 20 65 white noising e
16a8b 71 75 61 6c 69 6e 67 20 31 36 20 62 79 74 65 73 qualing 16 bytes
16a8c 20 6f 66 20 30 78 66 66 20 69 73 20 76 61 6e 69 of 0xff is vani
16a8d 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 73 6f shingly small so
16a8e 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 73 68 6f . ** we sho
16a8f 75 6c 64 20 73 74 69 6c 6c 20 62 65 20 6f 6b 2e uld still be ok.
16a90 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
16a91 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 64 memset(pPager->d
16a92 62 46 69 6c 65 56 65 72 73 2c 20 30 78 66 66 2c bFileVers, 0xff,
16a93 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e sizeof(pPager->
16a94 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 dbFileVers));.
16a95 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 }else{. u
16a96 38 20 2a 64 62 46 69 6c 65 56 65 72 73 20 3d 20 8 *dbFileVers =
16a97 26 28 28 75 38 2a 29 70 50 67 2d 3e 70 44 61 74 &((u8*)pPg->pDat
16a98 61 29 5b 32 34 5d 3b 0a 20 20 20 20 20 20 6d 65 a)[24];. me
16a99 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 mcpy(&pPager->db
16a9a 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 FileVers, dbFile
16a9b 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 70 50 61 Vers, sizeof(pPa
16a9c 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 ger->dbFileVers)
16a9d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 43 );. }. }. C
16a9e 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50 ODEC1(pPager, pP
16a9f 67 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 g->pData, pgno,
16aa0 33 2c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 3, rc = SQLITE_N
16aa1 4f 4d 45 4d 29 3b 0a 0a 20 20 50 41 47 45 52 5f OMEM);.. PAGER_
16aa2 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 INCR(sqlite3_pag
16aa3 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 29 er_readdb_count)
16aa4 3b 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 ;. PAGER_INCR(p
16aa5 50 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 Pager->nRead);.
16aa6 20 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e 20 IOTRACE(("PGIN
16aa7 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 %p %d\n", pPager
16aa8 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 50 41 47 45 , pgno));. PAGE
16aa9 52 54 52 41 43 45 28 28 22 46 45 54 43 48 20 25 RTRACE(("FETCH %
16aaa 64 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 d page %d hash(%
16aab 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 08x)\n",.
16aac 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 PAGERID(
16aad 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 pPager), pgno, p
16aae 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 ager_pagehash(pP
16aaf 67 29 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 g)));.. return
16ab0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 rc;.}../*.** Upd
16ab1 61 74 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ate the value of
16ab2 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e the change-coun
16ab3 74 65 72 20 61 74 20 6f 66 66 73 65 74 73 20 32 ter at offsets 2
16ab4 34 20 61 6e 64 20 39 32 20 69 6e 0a 2a 2a 20 74 4 and 92 in.** t
16ab5 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 74 68 he header and th
16ab6 65 20 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e e sqlite version
16ab7 20 6e 75 6d 62 65 72 20 61 74 20 6f 66 66 73 65 number at offse
16ab8 74 20 39 36 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 t 96..**.** This
16ab9 20 69 73 20 61 6e 20 75 6e 63 6f 6e 64 69 74 69 is an unconditi
16aba 6f 6e 61 6c 20 75 70 64 61 74 65 2e 20 20 53 65 onal update. Se
16abb 65 20 61 6c 73 6f 20 74 68 65 20 70 61 67 65 72 e also the pager
16abc 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e _incr_changecoun
16abd 74 65 72 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 ter().** routine
16abe 20 77 68 69 63 68 20 6f 6e 6c 79 20 75 70 64 61 which only upda
16abf 74 65 73 20 74 68 65 20 63 68 61 6e 67 65 2d 63 tes the change-c
16ac0 6f 75 6e 74 65 72 20 69 66 20 74 68 65 20 75 70 ounter if the up
16ac1 64 61 74 65 20 69 73 20 61 63 74 75 61 6c 6c 79 date is actually
16ac2 0a 2a 2a 20 6e 65 65 64 65 64 2c 20 61 73 20 64 .** needed, as d
16ac3 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 etermined by the
16ac4 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 pPager->changeC
16ac5 6f 75 6e 74 44 6f 6e 65 20 73 74 61 74 65 20 76 ountDone state v
16ac6 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 ariable..*/.stat
16ac7 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 77 72 ic void pager_wr
16ac8 69 74 65 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 ite_changecounte
16ac9 72 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 r(PgHdr *pPg){.
16aca 20 75 33 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e u32 change_coun
16acb 74 65 72 3b 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 ter;.. /* Incre
16acc 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6a ment the value j
16acd 75 73 74 20 72 65 61 64 20 61 6e 64 20 77 72 69 ust read and wri
16ace 74 65 20 69 74 20 62 61 63 6b 20 74 6f 20 62 79 te it back to by
16acf 74 65 20 32 34 2e 20 2a 2f 0a 20 20 63 68 61 6e te 24. */. chan
16ad0 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73 71 6c ge_counter = sql
16ad1 69 74 65 33 47 65 74 34 62 79 74 65 28 28 75 38 ite3Get4byte((u8
16ad2 2a 29 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 64 *)pPg->pPager->d
16ad3 62 46 69 6c 65 56 65 72 73 29 2b 31 3b 0a 20 20 bFileVers)+1;.
16ad4 70 75 74 33 32 62 69 74 73 28 28 28 63 68 61 72 put32bits(((char
16ad5 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 2b 32 34 *)pPg->pData)+24
16ad6 2c 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 , change_counter
16ad7 29 3b 0a 0a 20 20 2f 2a 20 41 6c 73 6f 20 73 74 );.. /* Also st
16ad8 6f 72 65 20 74 68 65 20 53 51 4c 69 74 65 20 76 ore the SQLite v
16ad9 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 69 6e ersion number in
16ada 20 62 79 74 65 73 20 39 36 2e 2e 39 39 20 61 6e bytes 96..99 an
16adb 64 20 69 6e 0a 20 20 2a 2a 20 62 79 74 65 73 20 d in. ** bytes
16adc 39 32 2e 2e 39 35 20 73 74 6f 72 65 20 74 68 65 92..95 store the
16add 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
16ade 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 76 65 for which the ve
16adf 72 73 69 6f 6e 20 6e 75 6d 62 65 72 0a 20 20 2a rsion number. *
16ae0 2a 20 69 73 20 76 61 6c 69 64 2e 20 2a 2f 0a 20 * is valid. */.
16ae1 20 70 75 74 33 32 62 69 74 73 28 28 28 63 68 61 put32bits(((cha
16ae2 72 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 2b 39 r*)pPg->pData)+9
16ae3 32 2c 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 2, change_counte
16ae4 72 29 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 r);. put32bits(
16ae5 28 28 63 68 61 72 2a 29 70 50 67 2d 3e 70 44 61 ((char*)pPg->pDa
16ae6 74 61 29 2b 39 36 2c 20 53 51 4c 49 54 45 5f 56 ta)+96, SQLITE_V
16ae7 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 29 3b 0a ERSION_NUMBER);.
16ae8 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
16ae9 45 5f 4f 4d 49 54 5f 57 41 4c 0a 2f 2a 0a 2a 2a E_OMIT_WAL./*.**
16aea 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
16aeb 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 s invoked once f
16aec 6f 72 20 65 61 63 68 20 70 61 67 65 20 74 68 61 or each page tha
16aed 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 t has already be
16aee 65 6e 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 en .** written i
16aef 6e 74 6f 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 nto the log file
16af0 20 77 68 65 6e 20 61 20 57 41 4c 20 74 72 61 6e when a WAL tran
16af1 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 saction is rolle
16af2 64 20 62 61 63 6b 2e 0a 2a 2a 20 50 61 72 61 6d d back..** Param
16af3 65 74 65 72 20 69 50 67 20 69 73 20 74 68 65 20 eter iPg is the
16af4 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 page number of s
16af5 61 69 64 20 70 61 67 65 2e 20 54 68 65 20 70 43 aid page. The pC
16af6 74 78 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 tx argument .**
16af7 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 70 6f is actually a po
16af8 69 6e 74 65 72 20 74 6f 20 74 68 65 20 50 61 67 inter to the Pag
16af9 65 72 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a er structure..**
16afa 0a 2a 2a 20 49 66 20 70 61 67 65 20 69 50 67 20 .** If page iPg
16afb 69 73 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 is present in th
16afc 65 20 63 61 63 68 65 2c 20 61 6e 64 20 68 61 73 e cache, and has
16afd 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 no outstanding
16afe 72 65 66 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 69 references,.** i
16aff 74 20 69 73 20 64 69 73 63 61 72 64 65 64 2e 20 t is discarded.
16b00 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 Otherwise, if th
16b01 65 72 65 20 61 72 65 20 6f 6e 65 20 6f 72 20 6d ere are one or m
16b02 6f 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a ore outstanding.
16b03 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 2c 20 74 ** references, t
16b04 68 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 he page content
16b05 69 73 20 72 65 6c 6f 61 64 65 64 20 66 72 6f 6d is reloaded from
16b06 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 the database. I
16b07 66 20 74 68 65 0a 2a 2a 20 61 74 74 65 6d 70 74 f the.** attempt
16b08 20 74 6f 20 72 65 6c 6f 61 64 20 63 6f 6e 74 65 to reload conte
16b09 6e 74 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 nt from the data
16b0a 62 61 73 65 20 69 73 20 72 65 71 75 69 72 65 64 base is required
16b0b 20 61 6e 64 20 66 61 69 6c 73 2c 20 0a 2a 2a 20 and fails, .**
16b0c 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65 return an SQLite
16b0d 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74 68 error code. Oth
16b0e 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f erwise, SQLITE_O
16b0f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 K..*/.static int
16b10 20 70 61 67 65 72 55 6e 64 6f 43 61 6c 6c 62 61 pagerUndoCallba
16b11 63 6b 28 76 6f 69 64 20 2a 70 43 74 78 2c 20 50 ck(void *pCtx, P
16b12 67 6e 6f 20 69 50 67 29 7b 0a 20 20 69 6e 74 20 gno iPg){. int
16b13 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
16b14 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 Pager *pPager
16b15 3d 20 28 50 61 67 65 72 20 2a 29 70 43 74 78 3b = (Pager *)pCtx;
16b16 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a . PgHdr *pPg;..
16b17 20 20 70 50 67 20 3d 20 73 71 6c 69 74 65 33 50 pPg = sqlite3P
16b18 61 67 65 72 4c 6f 6f 6b 75 70 28 70 50 61 67 65 agerLookup(pPage
16b19 72 2c 20 69 50 67 29 3b 0a 20 20 69 66 28 20 70 r, iPg);. if( p
16b1a 50 67 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 Pg ){. if( sq
16b1b 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 lite3PcachePageR
16b1c 65 66 63 6f 75 6e 74 28 70 50 67 29 3d 3d 31 20 efcount(pPg)==1
16b1d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
16b1e 50 63 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b PcacheDrop(pPg);
16b1f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
16b20 20 20 72 63 20 3d 20 72 65 61 64 44 62 50 61 67 rc = readDbPag
16b21 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20 69 66 e(pPg);. if
16b22 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
16b23 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 ){. pPage
16b24 72 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50 67 r->xReiniter(pPg
16b25 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
16b26 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
16b27 65 66 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 ef(pPg);. }.
16b28 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c }.. /* Normall
16b29 79 2c 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 y, if a transact
16b2a 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 ion is rolled ba
16b2b 63 6b 2c 20 61 6e 79 20 62 61 63 6b 75 70 20 70 ck, any backup p
16b2c 72 6f 63 65 73 73 65 73 20 61 72 65 0a 20 20 2a rocesses are. *
16b2d 2a 20 75 70 64 61 74 65 64 20 61 73 20 64 61 74 * updated as dat
16b2e 61 20 69 73 20 63 6f 70 69 65 64 20 6f 75 74 20 a is copied out
16b2f 6f 66 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 of the rollback
16b30 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 6e 74 6f journal and into
16b31 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 the. ** databa
16b32 73 65 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20 se. This is not
16b33 67 65 6e 65 72 61 6c 6c 79 20 70 6f 73 73 69 62 generally possib
16b34 6c 65 20 77 69 74 68 20 61 20 57 41 4c 20 64 61 le with a WAL da
16b35 74 61 62 61 73 65 2c 20 61 73 0a 20 20 2a 2a 20 tabase, as. **
16b36 72 6f 6c 6c 62 61 63 6b 20 69 6e 76 6f 6c 76 65 rollback involve
16b37 73 20 73 69 6d 70 6c 79 20 74 72 75 6e 63 61 74 s simply truncat
16b38 69 6e 67 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 ing the log file
16b39 2e 20 54 68 65 72 65 66 6f 72 65 2c 20 69 66 20 . Therefore, if
16b3a 6f 6e 65 0a 20 20 2a 2a 20 6f 72 20 6d 6f 72 65 one. ** or more
16b3b 20 66 72 61 6d 65 73 20 68 61 76 65 20 61 6c 72 frames have alr
16b3c 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 eady been writte
16b3d 6e 20 74 6f 20 74 68 65 20 6c 6f 67 20 28 61 6e n to the log (an
16b3e 64 20 74 68 65 72 65 66 6f 72 65 20 0a 20 20 2a d therefore . *
16b3f 2a 20 61 6c 73 6f 20 63 6f 70 69 65 64 20 69 6e * also copied in
16b40 74 6f 20 74 68 65 20 62 61 63 6b 75 70 20 64 61 to the backup da
16b41 74 61 62 61 73 65 73 29 20 61 73 20 70 61 72 74 tabases) as part
16b42 20 6f 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 of this transac
16b43 74 69 6f 6e 2c 0a 20 20 2a 2a 20 74 68 65 20 62 tion,. ** the b
16b44 61 63 6b 75 70 73 20 6d 75 73 74 20 62 65 20 72 ackups must be r
16b45 65 73 74 61 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 estarted.. */.
16b46 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 sqlite3BackupRe
16b47 73 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42 start(pPager->pB
16b48 61 63 6b 75 70 29 3b 0a 0a 20 20 72 65 74 75 72 ackup);.. retur
16b49 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
16b4a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
16b4b 63 61 6c 6c 65 64 20 74 6f 20 72 6f 6c 6c 62 61 called to rollba
16b4c 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ck a transaction
16b4d 20 6f 6e 20 61 20 57 41 4c 20 64 61 74 61 62 61 on a WAL databa
16b4e 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e se..*/.static in
16b4f 74 20 70 61 67 65 72 52 6f 6c 6c 62 61 63 6b 57 t pagerRollbackW
16b50 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 al(Pager *pPager
16b51 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
16b52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b53 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 43 /* Return C
16b54 6f 64 65 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a ode */. PgHdr *
16b55 70 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 pList;
16b56 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 /* List
16b57 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20 of dirty pages
16b58 74 6f 20 72 65 76 65 72 74 20 2a 2f 0a 0a 20 20 to revert */..
16b59 2f 2a 20 46 6f 72 20 61 6c 6c 20 70 61 67 65 73 /* For all pages
16b5a 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 74 68 in the cache th
16b5b 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 at are currently
16b5c 20 64 69 72 74 79 20 6f 72 20 68 61 76 65 20 61 dirty or have a
16b5d 6c 72 65 61 64 79 0a 20 20 2a 2a 20 62 65 65 6e lready. ** been
16b5e 20 77 72 69 74 74 65 6e 20 28 62 75 74 20 6e 6f written (but no
16b5f 74 20 63 6f 6d 6d 69 74 74 65 64 29 20 74 6f 20 t committed) to
16b60 74 68 65 20 6c 6f 67 20 66 69 6c 65 2c 20 64 6f the log file, do
16b61 20 6f 6e 65 20 6f 66 20 74 68 65 20 0a 20 20 2a one of the . *
16b62 2a 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 2a * following:. *
16b63 2a 0a 20 20 2a 2a 20 20 20 2b 20 44 69 73 63 61 *. ** + Disca
16b64 72 64 20 74 68 65 20 63 61 63 68 65 64 20 70 61 rd the cached pa
16b65 67 65 20 28 69 66 20 72 65 66 63 6f 75 6e 74 3d ge (if refcount=
16b66 3d 30 29 2c 20 6f 72 0a 20 20 2a 2a 20 20 20 2b =0), or. ** +
16b67 20 52 65 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e Reload page con
16b68 74 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 64 61 tent from the da
16b69 74 61 62 61 73 65 20 28 69 66 20 72 65 66 63 6f tabase (if refco
16b6a 75 6e 74 3e 30 29 2e 0a 20 20 2a 2f 0a 20 20 70 unt>0).. */. p
16b6b 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 Pager->dbSize =
16b6c 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 pPager->dbOrigSi
16b6d 7a 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 ze;. rc = sqlit
16b6e 65 33 57 61 6c 55 6e 64 6f 28 70 50 61 67 65 72 e3WalUndo(pPager
16b6f 2d 3e 70 57 61 6c 2c 20 70 61 67 65 72 55 6e 64 ->pWal, pagerUnd
16b70 6f 43 61 6c 6c 62 61 63 6b 2c 20 28 76 6f 69 64 oCallback, (void
16b71 20 2a 29 70 50 61 67 65 72 29 3b 0a 20 20 70 4c *)pPager);. pL
16b72 69 73 74 20 3d 20 73 71 6c 69 74 65 33 50 63 61 ist = sqlite3Pca
16b73 63 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 cheDirtyList(pPa
16b74 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 ger->pPCache);.
16b75 20 77 68 69 6c 65 28 20 70 4c 69 73 74 20 26 26 while( pList &&
16b76 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
16b77 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 4e 65 {. PgHdr *pNe
16b78 78 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72 xt = pList->pDir
16b79 74 79 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 ty;. rc = pag
16b7a 65 72 55 6e 64 6f 43 61 6c 6c 62 61 63 6b 28 28 erUndoCallback((
16b7b 76 6f 69 64 20 2a 29 70 50 61 67 65 72 2c 20 70 void *)pPager, p
16b7c 4c 69 73 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 List->pgno);.
16b7d 20 70 4c 69 73 74 20 3d 20 70 4e 65 78 74 3b 0a pList = pNext;.
16b7e 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
16b7f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
16b80 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72 function is a wr
16b81 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71 6c apper around sql
16b82 69 74 65 33 57 61 6c 46 72 61 6d 65 73 28 29 2e ite3WalFrames().
16b83 20 41 73 20 77 65 6c 6c 20 61 73 20 6c 6f 67 67 As well as logg
16b84 69 6e 67 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 ing.** the conte
16b85 6e 74 73 20 6f 66 20 74 68 65 20 6c 69 73 74 20 nts of the list
16b86 6f 66 20 70 61 67 65 73 20 68 65 61 64 65 64 20 of pages headed
16b87 62 79 20 70 4c 69 73 74 20 28 63 6f 6e 6e 65 63 by pList (connec
16b88 74 65 64 20 62 79 20 70 44 69 72 74 79 29 2c 0a ted by pDirty),.
16b89 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ** this function
16b8a 20 6e 6f 74 69 66 69 65 73 20 61 6e 79 20 61 63 notifies any ac
16b8b 74 69 76 65 20 62 61 63 6b 75 70 20 70 72 6f 63 tive backup proc
16b8c 65 73 73 65 73 20 74 68 61 74 20 74 68 65 20 70 esses that the p
16b8d 61 67 65 73 20 68 61 76 65 0a 2a 2a 20 63 68 61 ages have.** cha
16b8e 6e 67 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 nged. .**.** The
16b8f 20 6c 69 73 74 20 6f 66 20 70 61 67 65 73 20 70 list of pages p
16b90 61 73 73 65 64 20 69 6e 74 6f 20 74 68 69 73 20 assed into this
16b91 72 6f 75 74 69 6e 65 20 69 73 20 61 6c 77 61 79 routine is alway
16b92 73 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 65 s sorted by page
16b93 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 48 65 6e 63 number..** Henc
16b94 65 2c 20 69 66 20 70 61 67 65 20 31 20 61 70 70 e, if page 1 app
16b95 65 61 72 73 20 61 6e 79 77 68 65 72 65 20 6f 6e ears anywhere on
16b96 20 74 68 65 20 6c 69 73 74 2c 20 69 74 20 77 69 the list, it wi
16b97 6c 6c 20 62 65 20 74 68 65 20 66 69 72 73 74 20 ll be the first
16b98 70 61 67 65 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 page..*/ .static
16b99 20 69 6e 74 20 70 61 67 65 72 57 61 6c 46 72 61 int pagerWalFra
16b9a 6d 65 73 28 0a 20 20 50 61 67 65 72 20 2a 70 50 mes(. Pager *pP
16b9b 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 ager,
16b9c 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 /* Pager
16b9d 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 50 67 48 64 object */. PgHd
16b9e 72 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20 r *pList,
16b9f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
16ba0 69 73 74 20 6f 66 20 66 72 61 6d 65 73 20 74 6f ist of frames to
16ba1 20 6c 6f 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e log */. Pgno n
16ba2 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 Truncate,
16ba3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
16ba4 61 62 61 73 65 20 73 69 7a 65 20 61 66 74 65 72 abase size after
16ba5 20 74 68 69 73 20 63 6f 6d 6d 69 74 20 2a 2f 0a this commit */.
16ba6 20 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74 20 20 int isCommit
16ba7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ba8 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 /* True if thi
16ba9 73 20 69 73 20 61 20 63 6f 6d 6d 69 74 20 2a 2f s is a commit */
16baa 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 .){. int rc;
16bab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bac 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
16bad 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c code */. int nL
16bae 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ist;
16baf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
16bb0 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
16bb1 70 4c 69 73 74 20 2a 2f 0a 23 69 66 20 64 65 66 pList */.#if def
16bb2 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
16bb3 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 G) || defined(SQ
16bb4 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 LITE_CHECK_PAGES
16bb5 29 0a 20 20 50 67 48 64 72 20 2a 70 3b 20 20 20 ). PgHdr *p;
16bb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bb7 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 /* For loopi
16bb8 6e 67 20 6f 76 65 72 20 70 61 67 65 73 20 2a 2f ng over pages */
16bb9 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 .#endif.. asser
16bba 74 28 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 20 t( pPager->pWal
16bbb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 );. assert( pLi
16bbc 73 74 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c st );.#ifdef SQL
16bbd 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 56 ITE_DEBUG. /* V
16bbe 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 70 erify that the p
16bbf 61 67 65 20 6c 69 73 74 20 69 73 20 69 6e 20 61 age list is in a
16bc0 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a ccending order *
16bc1 2f 0a 20 20 66 6f 72 28 70 3d 70 4c 69 73 74 3b /. for(p=pList;
16bc2 20 70 20 26 26 20 70 2d 3e 70 44 69 72 74 79 3b p && p->pDirty;
16bc3 20 70 3d 70 2d 3e 70 44 69 72 74 79 29 7b 0a 20 p=p->pDirty){.
16bc4 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 67 assert( p->pg
16bc5 6e 6f 20 3c 20 70 2d 3e 70 44 69 72 74 79 2d 3e no < p->pDirty->
16bc6 70 67 6e 6f 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 pgno );. }.#end
16bc7 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4c if.. assert( pL
16bc8 69 73 74 2d 3e 70 44 69 72 74 79 3d 3d 30 20 7c ist->pDirty==0 |
16bc9 7c 20 69 73 43 6f 6d 6d 69 74 20 29 3b 0a 20 20 | isCommit );.
16bca 69 66 28 20 69 73 43 6f 6d 6d 69 74 20 29 7b 0a if( isCommit ){.
16bcb 20 20 20 20 2f 2a 20 49 66 20 61 20 57 41 4c 20 /* If a WAL
16bcc 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 62 transaction is b
16bcd 65 69 6e 67 20 63 6f 6d 6d 69 74 74 65 64 2c 20 eing committed,
16bce 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e there is no poin
16bcf 74 20 69 6e 20 77 72 69 74 69 6e 67 0a 20 20 20 t in writing.
16bd0 20 2a 2a 20 61 6e 79 20 70 61 67 65 73 20 77 69 ** any pages wi
16bd1 74 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 th page numbers
16bd2 67 72 65 61 74 65 72 20 74 68 61 6e 20 6e 54 72 greater than nTr
16bd3 75 6e 63 61 74 65 20 69 6e 74 6f 20 74 68 65 20 uncate into the
16bd4 57 41 4c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a WAL file.. **
16bd5 20 54 68 65 79 20 77 69 6c 6c 20 6e 65 76 65 72 They will never
16bd6 20 62 65 20 72 65 61 64 20 62 79 20 61 6e 79 20 be read by any
16bd7 63 6c 69 65 6e 74 2e 20 53 6f 20 72 65 6d 6f 76 client. So remov
16bd8 65 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20 e them from the
16bd9 70 44 69 72 74 79 0a 20 20 20 20 2a 2a 20 6c 69 pDirty. ** li
16bda 73 74 20 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 st here. */.
16bdb 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50 67 PgHdr *p;. Pg
16bdc 48 64 72 20 2a 2a 70 70 4e 65 78 74 20 3d 20 26 Hdr **ppNext = &
16bdd 70 4c 69 73 74 3b 0a 20 20 20 20 6e 4c 69 73 74 pList;. nList
16bde 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 3d = 0;. for(p=
16bdf 70 4c 69 73 74 3b 20 28 2a 70 70 4e 65 78 74 20 pList; (*ppNext
16be0 3d 20 70 29 21 3d 30 3b 20 70 3d 70 2d 3e 70 44 = p)!=0; p=p->pD
16be1 69 72 74 79 29 7b 0a 20 20 20 20 20 20 69 66 28 irty){. if(
16be2 20 70 2d 3e 70 67 6e 6f 3c 3d 6e 54 72 75 6e 63 p->pgno<=nTrunc
16be3 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ate ){. p
16be4 70 4e 65 78 74 20 3d 20 26 70 2d 3e 70 44 69 72 pNext = &p->pDir
16be5 74 79 3b 0a 20 20 20 20 20 20 20 20 6e 4c 69 73 ty;. nLis
16be6 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 t++;. }.
16be7 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
16be8 4c 69 73 74 20 29 3b 0a 20 20 7d 65 6c 73 65 7b List );. }else{
16be9 0a 20 20 20 20 6e 4c 69 73 74 20 3d 20 31 3b 0a . nList = 1;.
16bea 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 61 53 }. pPager->aS
16beb 74 61 74 5b 50 41 47 45 52 5f 53 54 41 54 5f 57 tat[PAGER_STAT_W
16bec 52 49 54 45 5d 20 2b 3d 20 6e 4c 69 73 74 3b 0a RITE] += nList;.
16bed 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 70 67 . if( pList->pg
16bee 6e 6f 3d 3d 31 20 29 20 70 61 67 65 72 5f 77 72 no==1 ) pager_wr
16bef 69 74 65 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 ite_changecounte
16bf0 72 28 70 4c 69 73 74 29 3b 0a 20 20 72 63 20 3d r(pList);. rc =
16bf1 20 73 71 6c 69 74 65 33 57 61 6c 46 72 61 6d 65 sqlite3WalFrame
16bf2 73 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 s(pPager->pWal,
16bf3 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 . pPager->p
16bf4 61 67 65 53 69 7a 65 2c 20 70 4c 69 73 74 2c 20 ageSize, pList,
16bf5 6e 54 72 75 6e 63 61 74 65 2c 20 69 73 43 6f 6d nTruncate, isCom
16bf6 6d 69 74 2c 20 70 50 61 67 65 72 2d 3e 77 61 6c mit, pPager->wal
16bf7 53 79 6e 63 46 6c 61 67 73 0a 20 20 29 3b 0a 20 SyncFlags. );.
16bf8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
16bf9 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 70 42 OK && pPager->pB
16bfa 61 63 6b 75 70 20 29 7b 0a 20 20 20 20 50 67 48 ackup ){. PgH
16bfb 64 72 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 dr *p;. for(p
16bfc 3d 70 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e =pList; p; p=p->
16bfd 70 44 69 72 74 79 29 7b 0a 20 20 20 20 20 20 73 pDirty){. s
16bfe 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 qlite3BackupUpda
16bff 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b te(pPager->pBack
16c00 75 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 28 75 38 up, p->pgno, (u8
16c01 20 2a 29 70 2d 3e 70 44 61 74 61 29 3b 0a 20 20 *)p->pData);.
16c02 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 }. }..#ifdef
16c03 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
16c04 45 53 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c ES. pList = sql
16c05 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c ite3PcacheDirtyL
16c06 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ist(pPager->pPCa
16c07 63 68 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 4c che);. for(p=pL
16c08 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 ist; p; p=p->pDi
16c09 72 74 79 29 7b 0a 20 20 20 20 70 61 67 65 72 5f rty){. pager_
16c0a 73 65 74 5f 70 61 67 65 68 61 73 68 28 70 29 3b set_pagehash(p);
16c0b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 . }.#endif.. r
16c0c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
16c0d 2a 2a 20 42 65 67 69 6e 20 61 20 72 65 61 64 20 ** Begin a read
16c0e 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 transaction on t
16c0f 68 65 20 57 41 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 he WAL..**.** Th
16c10 69 73 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 is routine used
16c11 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 22 70 61 to be called "pa
16c12 67 65 72 4f 70 65 6e 53 6e 61 70 73 68 6f 74 28 gerOpenSnapshot(
16c13 29 22 20 62 65 63 61 75 73 65 20 69 74 20 65 73 )" because it es
16c14 73 65 6e 74 69 61 6c 6c 79 0a 2a 2a 20 6d 61 6b sentially.** mak
16c15 65 73 20 61 20 73 6e 61 70 73 68 6f 74 20 6f 66 es a snapshot of
16c16 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 74 the database at
16c17 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 6f 69 the current poi
16c18 6e 74 20 69 6e 20 74 69 6d 65 20 61 6e 64 20 70 nt in time and p
16c19 72 65 73 65 72 76 65 73 0a 2a 2a 20 74 68 61 74 reserves.** that
16c1a 20 73 6e 61 70 73 68 6f 74 20 66 6f 72 20 75 73 snapshot for us
16c1b 65 20 62 79 20 74 68 65 20 72 65 61 64 65 72 20 e by the reader
16c1c 69 6e 20 73 70 69 74 65 20 6f 66 20 63 6f 6e 63 in spite of conc
16c1d 75 72 72 65 6e 74 6c 79 20 63 68 61 6e 67 65 73 urrently changes
16c1e 20 62 79 0a 2a 2a 20 6f 74 68 65 72 20 77 72 69 by.** other wri
16c1f 74 65 72 73 20 6f 72 20 63 68 65 63 6b 70 6f 69 ters or checkpoi
16c20 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 nters..*/.static
16c21 20 69 6e 74 20 70 61 67 65 72 42 65 67 69 6e 52 int pagerBeginR
16c22 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 eadTransaction(P
16c23 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
16c24 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
16c25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c26 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
16c27 2a 2f 0a 20 20 69 6e 74 20 63 68 61 6e 67 65 64 */. int changed
16c28 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
16c29 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
16c2a 63 61 63 68 65 20 6d 75 73 74 20 62 65 20 72 65 cache must be re
16c2b 73 65 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 set */.. assert
16c2c 28 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 ( pagerUseWal(pP
16c2d 61 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 ager) );. asser
16c2e 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 t( pPager->eStat
16c2f 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 7c 7c e==PAGER_OPEN ||
16c30 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d pPager->eState=
16c31 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20 29 3b =PAGER_READER );
16c32 0a 0a 20 20 2f 2a 20 73 71 6c 69 74 65 33 57 61 .. /* sqlite3Wa
16c33 6c 45 6e 64 52 65 61 64 54 72 61 6e 73 61 63 74 lEndReadTransact
16c34 69 6f 6e 28 29 20 77 61 73 20 6e 6f 74 20 63 61 ion() was not ca
16c35 6c 6c 65 64 20 66 6f 72 20 74 68 65 20 70 72 65 lled for the pre
16c36 76 69 6f 75 73 0a 20 20 2a 2a 20 74 72 61 6e 73 vious. ** trans
16c37 61 63 74 69 6f 6e 20 69 6e 20 6c 6f 63 6b 69 6e action in lockin
16c38 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 49 56 45 g_mode=EXCLUSIVE
16c39 2e 20 20 53 6f 20 63 61 6c 6c 20 69 74 20 6e 6f . So call it no
16c3a 77 2e 20 20 49 66 20 77 65 0a 20 20 2a 2a 20 61 w. If we. ** a
16c3b 72 65 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f re in locking_mo
16c3c 64 65 3d 4e 4f 52 4d 41 4c 20 61 6e 64 20 45 6e de=NORMAL and En
16c3d 64 52 65 61 64 28 29 20 77 61 73 20 70 72 65 76 dRead() was prev
16c3e 69 6f 75 73 6c 79 20 63 61 6c 6c 65 64 2c 0a 20 iously called,.
16c3f 20 2a 2a 20 74 68 65 20 64 75 70 6c 69 63 61 74 ** the duplicat
16c40 65 20 63 61 6c 6c 20 69 73 20 68 61 72 6d 6c 65 e call is harmle
16c41 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 ss.. */. sqlit
16c42 65 33 57 61 6c 45 6e 64 52 65 61 64 54 72 61 6e e3WalEndReadTran
16c43 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2d 3e saction(pPager->
16c44 70 57 61 6c 29 3b 0a 0a 20 20 72 63 20 3d 20 73 pWal);.. rc = s
16c45 71 6c 69 74 65 33 57 61 6c 42 65 67 69 6e 52 65 qlite3WalBeginRe
16c46 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 adTransaction(pP
16c47 61 67 65 72 2d 3e 70 57 61 6c 2c 20 26 63 68 61 ager->pWal, &cha
16c48 6e 67 65 64 29 3b 0a 20 20 69 66 28 20 72 63 21 nged);. if( rc!
16c49 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 63 68 =SQLITE_OK || ch
16c4a 61 6e 67 65 64 20 29 7b 0a 20 20 20 20 70 61 67 anged ){. pag
16c4b 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 er_reset(pPager)
16c4c 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
16c4d 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a rc;.}.#endif../*
16c4e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
16c4f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 73 20 70 n is called as p
16c50 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 art of the trans
16c51 69 74 69 6f 6e 20 66 72 6f 6d 20 50 41 47 45 52 ition from PAGER
16c52 5f 4f 50 45 4e 0a 2a 2a 20 74 6f 20 50 41 47 45 _OPEN.** to PAGE
16c53 52 5f 52 45 41 44 45 52 20 73 74 61 74 65 20 74 R_READER state t
16c54 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
16c55 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
16c56 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 69 6e 20 base file.** in
16c57 70 61 67 65 73 20 28 61 73 73 75 6d 69 6e 67 20 pages (assuming
16c58 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 75 the page size cu
16c59 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 rrently stored i
16c5a 6e 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 n Pager.pageSize
16c5b 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 )..**.** If no e
16c5c 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 53 51 4c rror occurs, SQL
16c5d 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
16c5e 65 64 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 ed and the size
16c5f 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a of the database.
16c60 2a 2a 20 69 6e 20 70 61 67 65 73 20 69 73 20 73 ** in pages is s
16c61 74 6f 72 65 64 20 69 6e 20 2a 70 6e 50 61 67 65 tored in *pnPage
16c62 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 . Otherwise, an
16c63 65 72 72 6f 72 20 63 6f 64 65 20 28 70 65 72 68 error code (perh
16c64 61 70 73 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f aps.** SQLITE_IO
16c65 45 52 52 5f 46 53 54 41 54 29 20 69 73 20 72 65 ERR_FSTAT) is re
16c66 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 6e 50 61 turned and *pnPa
16c67 67 65 20 69 73 20 6c 65 66 74 20 75 6e 6d 6f 64 ge is left unmod
16c68 69 66 69 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 ified..*/.static
16c69 20 69 6e 74 20 70 61 67 65 72 50 61 67 65 63 6f int pagerPageco
16c6a 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 unt(Pager *pPage
16c6b 72 2c 20 50 67 6e 6f 20 2a 70 6e 50 61 67 65 29 r, Pgno *pnPage)
16c6c 7b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 {. Pgno nPage;
16c6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c6e 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 /* Value to
16c6f 72 65 74 75 72 6e 20 76 69 61 20 2a 70 6e 50 61 return via *pnPa
16c70 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20 51 75 65 72 ge */.. /* Quer
16c71 79 20 74 68 65 20 57 41 4c 20 73 75 62 2d 73 79 y the WAL sub-sy
16c72 73 74 65 6d 20 66 6f 72 20 74 68 65 20 64 61 74 stem for the dat
16c73 61 62 61 73 65 20 73 69 7a 65 2e 20 54 68 65 20 abase size. The
16c74 57 61 6c 44 62 73 69 7a 65 28 29 0a 20 20 2a 2a WalDbsize(). **
16c75 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
16c76 73 20 7a 65 72 6f 20 69 66 20 74 68 65 20 57 41 s zero if the WA
16c77 4c 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20 28 69 L is not open (i
16c78 2e 65 2e 20 50 61 67 65 72 2e 70 57 61 6c 3d 3d .e. Pager.pWal==
16c79 30 29 2c 20 6f 72 0a 20 20 2a 2a 20 69 66 20 74 0), or. ** if t
16c7a 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 he database size
16c7b 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c is not availabl
16c7c 65 2e 20 54 68 65 20 64 61 74 61 62 61 73 65 20 e. The database
16c7d 73 69 7a 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a size is not. **
16c7e 20 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 available from
16c7f 74 68 65 20 57 41 4c 20 73 75 62 2d 73 79 73 74 the WAL sub-syst
16c80 65 6d 20 69 66 20 74 68 65 20 6c 6f 67 20 66 69 em if the log fi
16c81 6c 65 20 69 73 20 65 6d 70 74 79 20 6f 72 0a 20 le is empty or.
16c82 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 ** contains no
16c83 76 61 6c 69 64 20 63 6f 6d 6d 69 74 74 65 64 20 valid committed
16c84 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 20 transactions..
16c85 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
16c86 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 ger->eState==PAG
16c87 45 52 5f 4f 50 45 4e 20 29 3b 0a 20 20 61 73 73 ER_OPEN );. ass
16c88 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f ert( pPager->eLo
16c89 63 6b 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ck>=SHARED_LOCK
16c8a 29 3b 0a 20 20 6e 50 61 67 65 20 3d 20 73 71 6c );. nPage = sql
16c8b 69 74 65 33 57 61 6c 44 62 73 69 7a 65 28 70 50 ite3WalDbsize(pP
16c8c 61 67 65 72 2d 3e 70 57 61 6c 29 3b 0a 0a 20 20 ager->pWal);..
16c8d 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 /* If the databa
16c8e 73 65 20 73 69 7a 65 20 77 61 73 20 6e 6f 74 20 se size was not
16c8f 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 available from t
16c90 68 65 20 57 41 4c 20 73 75 62 2d 73 79 73 74 65 he WAL sub-syste
16c91 6d 2c 0a 20 20 2a 2a 20 64 65 74 65 72 6d 69 6e m,. ** determin
16c92 65 20 69 74 20 62 61 73 65 64 20 6f 6e 20 74 68 e it based on th
16c93 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 e size of the da
16c94 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 tabase file. If
16c95 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 the size. ** of
16c96 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
16c97 6c 65 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 le is not an int
16c98 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 eger multiple of
16c99 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 2c 0a the page-size,.
16c9a 20 20 2a 2a 20 72 6f 75 6e 64 20 64 6f 77 6e 20 ** round down
16c9b 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 70 to the nearest p
16c9c 61 67 65 2e 20 45 78 63 65 70 74 2c 20 61 6e 79 age. Except, any
16c9d 20 66 69 6c 65 20 6c 61 72 67 65 72 20 74 68 61 file larger tha
16c9e 6e 20 30 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 n 0. ** bytes i
16c9f 6e 20 73 69 7a 65 20 69 73 20 63 6f 6e 73 69 64 n size is consid
16ca0 65 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 ered to contain
16ca1 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 70 61 67 at least one pag
16ca2 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 50 e.. */. if( nP
16ca3 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 36 age==0 ){. i6
16ca4 34 20 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 4 n = 0;
16ca5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
16ca6 69 7a 65 20 6f 66 20 64 62 20 66 69 6c 65 20 69 ize of db file i
16ca7 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 61 n bytes */. a
16ca8 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
16ca9 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 ager->fd) || pPa
16caa 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b ger->tempFile );
16cab 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 . if( isOpen(
16cac 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 pPager->fd) ){.
16cad 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 int rc = sq
16cae 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
16caf 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 6e 29 3b pPager->fd, &n);
16cb0 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
16cb1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16cb2 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
16cb3 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
16cb4 20 6e 50 61 67 65 20 3d 20 28 50 67 6e 6f 29 28 nPage = (Pgno)(
16cb5 28 6e 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 (n+pPager->pageS
16cb6 69 7a 65 2d 31 29 20 2f 20 70 50 61 67 65 72 2d ize-1) / pPager-
16cb7 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 7d 0a >pageSize);. }.
16cb8 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 . /* If the cur
16cb9 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 70 rent number of p
16cba 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 ages in the file
16cbb 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
16cbc 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6e 66 69 67 the. ** config
16cbd 75 72 65 64 20 6d 61 78 69 6d 75 6d 20 70 61 67 ured maximum pag
16cbe 65 72 20 6e 75 6d 62 65 72 2c 20 69 6e 63 72 65 er number, incre
16cbf 61 73 65 20 74 68 65 20 61 6c 6c 6f 77 65 64 20 ase the allowed
16cc0 6c 69 6d 69 74 20 73 6f 0a 20 20 2a 2a 20 74 68 limit so. ** th
16cc1 61 74 20 74 68 65 20 66 69 6c 65 20 63 61 6e 20 at the file can
16cc2 62 65 20 72 65 61 64 2e 0a 20 20 2a 2f 0a 20 20 be read.. */.
16cc3 69 66 28 20 6e 50 61 67 65 3e 70 50 61 67 65 72 if( nPage>pPager
16cc4 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20 20 ->mxPgno ){.
16cc5 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d pPager->mxPgno =
16cc6 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 20 20 (Pgno)nPage;.
16cc7 7d 0a 0a 20 20 2a 70 6e 50 61 67 65 20 3d 20 6e }.. *pnPage = n
16cc8 50 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 Page;. return S
16cc9 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 QLITE_OK;.}..#if
16cca 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16ccb 5f 57 41 4c 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b _WAL./*.** Check
16ccc 20 69 66 20 74 68 65 20 2a 2d 77 61 6c 20 66 69 if the *-wal fi
16ccd 6c 65 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f le that correspo
16cce 6e 64 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 nds to the datab
16ccf 61 73 65 20 6f 70 65 6e 65 64 20 62 79 20 70 50 ase opened by pP
16cd0 61 67 65 72 0a 2a 2a 20 65 78 69 73 74 73 20 69 ager.** exists i
16cd1 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 f the database i
16cd2 73 20 6e 6f 74 20 65 6d 70 79 2c 20 6f 72 20 76 s not empy, or v
16cd3 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 2a erify that the *
16cd4 2d 77 61 6c 20 66 69 6c 65 20 64 6f 65 73 0a 2a -wal file does.*
16cd5 2a 20 6e 6f 74 20 65 78 69 73 74 20 28 62 79 20 * not exist (by
16cd6 64 65 6c 65 74 69 6e 67 20 69 74 29 20 69 66 20 deleting it) if
16cd7 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
16cd8 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a e is empty..**.*
16cd9 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
16cda 65 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 61 e is not empty a
16cdb 6e 64 20 74 68 65 20 2a 2d 77 61 6c 20 66 69 6c nd the *-wal fil
16cdc 65 20 65 78 69 73 74 73 2c 20 6f 70 65 6e 20 74 e exists, open t
16cdd 68 65 20 70 61 67 65 72 0a 2a 2a 20 69 6e 20 57 he pager.** in W
16cde 41 4c 20 6d 6f 64 65 2e 20 20 49 66 20 74 68 65 AL mode. If the
16cdf 20 64 61 74 61 62 61 73 65 20 69 73 20 65 6d 70 database is emp
16ce0 74 79 20 6f 72 20 69 66 20 6e 6f 20 2a 2d 77 61 ty or if no *-wa
16ce1 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e l file exists an
16ce2 64 0a 2a 2a 20 69 66 20 6e 6f 20 65 72 72 6f 72 d.** if no error
16ce3 20 6f 63 63 75 72 73 2c 20 6d 61 6b 65 20 73 75 occurs, make su
16ce4 72 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c re Pager.journal
16ce5 4d 6f 64 65 20 69 73 20 6e 6f 74 20 73 65 74 20 Mode is not set
16ce6 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 4a 4f 55 52 to.** PAGER_JOUR
16ce7 4e 41 4c 4d 4f 44 45 5f 57 41 4c 2e 0a 2a 2a 0a NALMODE_WAL..**.
16ce8 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ** Return SQLITE
16ce9 5f 4f 4b 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 _OK or an error
16cea 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 code..**.** The
16ceb 63 61 6c 6c 65 72 20 6d 75 73 74 20 68 6f 6c 64 caller must hold
16cec 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 6f a SHARED lock o
16ced 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
16cee 69 6c 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 ile to call this
16cef 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 42 65 .** function. Be
16cf0 63 61 75 73 65 20 61 6e 20 45 58 43 4c 55 53 49 cause an EXCLUSI
16cf1 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 VE lock on the d
16cf2 62 20 66 69 6c 65 20 69 73 20 72 65 71 75 69 72 b file is requir
16cf3 65 64 20 74 6f 20 64 65 6c 65 74 65 20 0a 2a 2a ed to delete .**
16cf4 20 61 20 57 41 4c 20 6f 6e 20 61 20 6e 6f 6e 65 a WAL on a none
16cf5 2d 65 6d 70 74 79 20 64 61 74 61 62 61 73 65 2c -empty database,
16cf6 20 74 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 this ensures th
16cf7 65 72 65 20 69 73 20 6e 6f 20 72 61 63 65 20 63 ere is no race c
16cf8 6f 6e 64 69 74 69 6f 6e 20 0a 2a 2a 20 62 65 74 ondition .** bet
16cf9 77 65 65 6e 20 74 68 65 20 78 41 63 63 65 73 73 ween the xAccess
16cfa 28 29 20 62 65 6c 6f 77 20 61 6e 64 20 61 6e 20 () below and an
16cfb 78 44 65 6c 65 74 65 28 29 20 62 65 69 6e 67 20 xDelete() being
16cfc 65 78 65 63 75 74 65 64 20 62 79 20 73 6f 6d 65 executed by some
16cfd 20 0a 2a 2a 20 6f 74 68 65 72 20 63 6f 6e 6e 65 .** other conne
16cfe 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ction..*/.static
16cff 20 69 6e 74 20 70 61 67 65 72 4f 70 65 6e 57 61 int pagerOpenWa
16d00 6c 49 66 50 72 65 73 65 6e 74 28 50 61 67 65 72 lIfPresent(Pager
16d01 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 *pPager){. int
16d02 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
16d03 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
16d04 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 r->eState==PAGER
16d05 5f 4f 50 45 4e 20 29 3b 0a 20 20 61 73 73 65 72 _OPEN );. asser
16d06 74 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b t( pPager->eLock
16d07 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b >=SHARED_LOCK );
16d08 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d .. if( !pPager-
16d09 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 >tempFile ){.
16d0a 20 69 6e 74 20 69 73 57 61 6c 3b 20 20 20 20 20 int isWal;
16d0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16d0c 2a 20 54 72 75 65 20 69 66 20 57 41 4c 20 66 69 * True if WAL fi
16d0d 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 le exists */.
16d0e 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20 Pgno nPage;
16d0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16d10 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 64 61 * Size of the da
16d11 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a tabase file */..
16d12 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50 61 rc = pagerPa
16d13 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 gecount(pPager,
16d14 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 &nPage);. if(
16d15 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
16d16 0a 20 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d . if( nPage==
16d17 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
16d18 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
16d19 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 pPager->pVfs, pP
16d1a 61 67 65 72 2d 3e 7a 57 61 6c 2c 20 30 29 3b 0a ager->zWal, 0);.
16d1b 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
16d1c 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 LITE_IOERR_DELET
16d1d 45 5f 4e 4f 45 4e 54 20 29 20 72 63 20 3d 20 53 E_NOENT ) rc = S
16d1e 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
16d1f 69 73 57 61 6c 20 3d 20 30 3b 0a 20 20 20 20 7d isWal = 0;. }
16d20 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
16d21 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 sqlite3OsAccess
16d22 28 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 (. pPag
16d23 65 72 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 er->pVfs, pPager
16d24 2d 3e 7a 57 61 6c 2c 20 53 51 4c 49 54 45 5f 41 ->zWal, SQLITE_A
16d25 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 69 CCESS_EXISTS, &i
16d26 73 57 61 6c 0a 20 20 20 20 20 20 29 3b 0a 20 20 sWal. );.
16d27 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
16d28 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
16d29 20 20 20 69 66 28 20 69 73 57 61 6c 20 29 7b 0a if( isWal ){.
16d2a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
16d2b 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 ( sqlite3PcacheP
16d2c 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d agecount(pPager-
16d2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a >pPCache)==0 );.
16d2e 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
16d2f 69 74 65 33 50 61 67 65 72 4f 70 65 6e 57 61 6c ite3PagerOpenWal
16d30 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 (pPager, 0);.
16d31 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 }else if( pPa
16d32 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
16d33 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
16d34 4f 44 45 5f 57 41 4c 20 29 7b 0a 20 20 20 20 20 ODE_WAL ){.
16d35 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
16d36 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a alMode = PAGER_J
16d37 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 OURNALMODE_DELET
16d38 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d E;. }. }
16d39 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
16d3a 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
16d3b 2a 20 50 6c 61 79 62 61 63 6b 20 73 61 76 65 70 * Playback savep
16d3c 6f 69 6e 74 20 70 53 61 76 65 70 6f 69 6e 74 2e oint pSavepoint.
16d3d 20 4f 72 2c 20 69 66 20 70 53 61 76 65 70 6f 69 Or, if pSavepoi
16d3e 6e 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 nt==NULL, then p
16d3f 6c 61 79 62 61 63 6b 0a 2a 2a 20 74 68 65 20 65 layback.** the e
16d40 6e 74 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 ntire master jou
16d41 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 63 rnal file. The c
16d42 61 73 65 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d ase pSavepoint==
16d43 4e 55 4c 4c 20 6f 63 63 75 72 73 20 77 68 65 6e NULL occurs when
16d44 20 0a 2a 2a 20 61 20 52 4f 4c 4c 42 41 43 4b 20 .** a ROLLBACK
16d45 54 4f 20 63 6f 6d 6d 61 6e 64 20 69 73 20 69 6e TO command is in
16d46 76 6f 6b 65 64 20 6f 6e 20 61 20 53 41 56 45 50 voked on a SAVEP
16d47 4f 49 4e 54 20 74 68 61 74 20 69 73 20 61 20 74 OINT that is a t
16d48 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 73 ransaction .** s
16d49 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 avepoint..**.**
16d4a 57 68 65 6e 20 70 53 61 76 65 70 6f 69 6e 74 20 When pSavepoint
16d4b 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65 61 is not NULL (mea
16d4c 6e 69 6e 67 20 61 20 6e 6f 6e 2d 74 72 61 6e 73 ning a non-trans
16d4d 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 action savepoint
16d4e 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 72 6f is .** being ro
16d4f 6c 6c 65 64 20 62 61 63 6b 29 2c 20 74 68 65 6e lled back), then
16d50 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 6f the rollback co
16d51 6e 73 69 73 74 73 20 6f 66 20 75 70 20 74 6f 20 nsists of up to
16d52 74 68 72 65 65 20 73 74 61 67 65 73 2c 0a 2a 2a three stages,.**
16d53 20 70 65 72 66 6f 72 6d 65 64 20 69 6e 20 74 68 performed in th
16d54 65 20 6f 72 64 65 72 20 73 70 65 63 69 66 69 65 e order specifie
16d55 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 d:.**.** * Pag
16d56 65 73 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 es are played ba
16d57 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e ck from the main
16d58 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e journal startin
16d59 67 20 61 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 g at byte.**
16d5a 20 6f 66 66 73 65 74 20 50 61 67 65 72 53 61 76 offset PagerSav
16d5b 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 epoint.iOffset a
16d5c 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f nd continuing to
16d5d 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 .** PagerSa
16d5e 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 vepoint.iHdrOffs
16d5f 65 74 2c 20 6f 72 20 74 6f 20 74 68 65 20 65 6e et, or to the en
16d60 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f d of the main jo
16d61 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c urnal.** fil
16d62 65 20 69 66 20 50 61 67 65 72 53 61 76 65 70 6f e if PagerSavepo
16d63 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69 int.iHdrOffset i
16d64 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 s zero..**.**
16d65 2a 20 49 66 20 50 61 67 65 72 53 61 76 65 70 6f * If PagerSavepo
16d66 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69 int.iHdrOffset i
16d67 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e s not zero, then
16d68 20 70 61 67 65 73 20 61 72 65 20 70 6c 61 79 65 pages are playe
16d69 64 0a 2a 2a 20 20 20 20 20 62 61 63 6b 20 73 74 d.** back st
16d6a 61 72 74 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 arting from the
16d6b 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 journal header i
16d6c 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f mmediately follo
16d6d 77 69 6e 67 20 0a 2a 2a 20 20 20 20 20 50 61 67 wing .** Pag
16d6e 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 erSavepoint.iHdr
16d6f 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 65 6e Offset to the en
16d70 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f d of the main jo
16d71 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a urnal file..**.*
16d72 2a 20 20 20 2a 20 50 61 67 65 73 20 61 72 65 20 * * Pages are
16d73 74 68 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b then played back
16d74 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f from the sub-jo
16d75 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 74 61 72 urnal file, star
16d76 74 69 6e 67 0a 2a 2a 20 20 20 20 20 77 69 74 68 ting.** with
16d77 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f the PagerSavepo
16d78 69 6e 74 2e 69 53 75 62 52 65 63 20 61 6e 64 20 int.iSubRec and
16d79 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 continuing to th
16d7a 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 20 20 20 20 e end of.**
16d7b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
16d7c 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68 6f ..**.** Througho
16d7d 75 74 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 ut the rollback
16d7e 70 72 6f 63 65 73 73 2c 20 65 61 63 68 20 74 69 process, each ti
16d7f 6d 65 20 61 20 70 61 67 65 20 69 73 20 72 6f 6c me a page is rol
16d80 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 0a 2a 2a led back, the.**
16d81 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 corresponding b
16d82 69 74 20 69 73 20 73 65 74 20 69 6e 20 61 20 62 it is set in a b
16d83 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65 20 itvec structure
16d84 28 76 61 72 69 61 62 6c 65 20 70 44 6f 6e 65 20 (variable pDone
16d85 69 6e 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d in the.** implem
16d86 65 6e 74 61 74 69 6f 6e 20 62 65 6c 6f 77 29 2e entation below).
16d87 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f This is used to
16d88 20 65 6e 73 75 72 65 20 74 68 61 74 20 61 20 70 ensure that a p
16d89 61 67 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 age is only.** r
16d8a 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 66 olled back the f
16d8b 69 72 73 74 20 74 69 6d 65 20 69 74 20 69 73 20 irst time it is
16d8c 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 6e 20 65 encountered in e
16d8d 69 74 68 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 2a ither journal..*
16d8e 2a 0a 2a 2a 20 49 66 20 70 53 61 76 65 70 6f 69 *.** If pSavepoi
16d8f 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e nt is NULL, then
16d90 20 70 61 67 65 73 20 61 72 65 20 6f 6e 6c 79 20 pages are only
16d91 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d played back from
16d92 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 the main.** jou
16d93 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 rnal file. There
16d94 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 is no need for
16d95 61 20 62 69 74 76 65 63 20 69 6e 20 74 68 69 73 a bitvec in this
16d96 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 case..**.** In
16d97 65 69 74 68 65 72 20 63 61 73 65 2c 20 62 65 66 either case, bef
16d98 6f 72 65 20 70 6c 61 79 62 61 63 6b 20 63 6f 6d ore playback com
16d99 6d 65 6e 63 65 73 20 74 68 65 20 50 61 67 65 72 mences the Pager
16d9a 2e 64 62 53 69 7a 65 20 76 61 72 69 61 62 6c 65 .dbSize variable
16d9b 0a 2a 2a 20 69 73 20 72 65 73 65 74 20 74 6f 20 .** is reset to
16d9c 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 69 the value that i
16d9d 74 20 68 65 6c 64 20 61 74 20 74 68 65 20 73 74 t held at the st
16d9e 61 72 74 20 6f 66 20 74 68 65 20 73 61 76 65 70 art of the savep
16d9f 6f 69 6e 74 20 0a 2a 2a 20 28 6f 72 20 74 72 61 oint .** (or tra
16da0 6e 73 61 63 74 69 6f 6e 29 2e 20 4e 6f 20 70 61 nsaction). No pa
16da1 67 65 20 77 69 74 68 20 61 20 70 61 67 65 2d 6e ge with a page-n
16da2 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 68 umber greater th
16da3 61 6e 20 74 68 69 73 20 76 61 6c 75 65 0a 2a 2a an this value.**
16da4 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b 2e is played back.
16da5 20 49 66 20 6f 6e 65 20 69 73 20 65 6e 63 6f 75 If one is encou
16da6 6e 74 65 72 65 64 20 69 74 20 69 73 20 73 69 6d ntered it is sim
16da7 70 6c 79 20 73 6b 69 70 70 65 64 2e 0a 2a 2f 0a ply skipped..*/.
16da8 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
16da9 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e PlaybackSavepoin
16daa 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c t(Pager *pPager,
16dab 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 PagerSavepoint
16dac 2a 70 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 *pSavepoint){.
16dad 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 i64 szJ;
16dae 20 20 20 20 20 20 20 20 20 2f 2a 20 45 66 66 65 /* Effe
16daf 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68 ctive size of th
16db0 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a e main journal *
16db1 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b /. i64 iHdrOff;
16db2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16db3 45 6e 64 20 6f 66 20 66 69 72 73 74 20 73 65 67 End of first seg
16db4 6d 65 6e 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f 75 ment of main-jou
16db5 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 2a 2f 0a rnal records */.
16db6 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
16db7 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65 E_OK; /* Re
16db8 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 turn code */. B
16db9 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 3d 20 30 itvec *pDone = 0
16dba 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 74 76 65 ; /* Bitve
16dbb 63 20 74 6f 20 65 6e 73 75 72 65 20 70 61 67 65 c to ensure page
16dbc 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 6e s played back on
16dbd 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61 73 ly once */.. as
16dbe 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 sert( pPager->eS
16dbf 74 61 74 65 21 3d 50 41 47 45 52 5f 45 52 52 4f tate!=PAGER_ERRO
16dc0 52 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 R );. assert( p
16dc1 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 Pager->eState>=P
16dc2 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b AGER_WRITER_LOCK
16dc3 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f ED );.. /* Allo
16dc4 63 61 74 65 20 61 20 62 69 74 76 65 63 20 74 6f cate a bitvec to
16dc5 20 75 73 65 20 74 6f 20 73 74 6f 72 65 20 74 68 use to store th
16dc6 65 20 73 65 74 20 6f 66 20 70 61 67 65 73 20 72 e set of pages r
16dc7 6f 6c 6c 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 olled back */.
16dc8 69 66 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29 if( pSavepoint )
16dc9 7b 0a 20 20 20 20 70 44 6f 6e 65 20 3d 20 73 71 {. pDone = sq
16dca 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 lite3BitvecCreat
16dcb 65 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f e(pSavepoint->nO
16dcc 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20 21 70 rig);. if( !p
16dcd 44 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 72 65 Done ){. re
16dce 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
16dcf 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 M;. }. }..
16dd0 2f 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62 /* Set the datab
16dd1 61 73 65 20 73 69 7a 65 20 62 61 63 6b 20 74 6f ase size back to
16dd2 20 74 68 65 20 76 61 6c 75 65 20 69 74 20 77 61 the value it wa
16dd3 73 20 62 65 66 6f 72 65 20 74 68 65 20 73 61 76 s before the sav
16dd4 65 70 6f 69 6e 74 20 0a 20 20 2a 2a 20 62 65 69 epoint . ** bei
16dd5 6e 67 20 72 65 76 65 72 74 65 64 20 77 61 73 20 ng reverted was
16dd6 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 opened.. */. p
16dd7 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 Pager->dbSize =
16dd8 70 53 61 76 65 70 6f 69 6e 74 20 3f 20 70 53 61 pSavepoint ? pSa
16dd9 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a vepoint->nOrig :
16dda 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 pPager->dbOrigS
16ddb 69 7a 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 63 ize;. pPager->c
16ddc 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d hangeCountDone =
16ddd 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
16dde 65 3b 0a 0a 20 20 69 66 28 20 21 70 53 61 76 65 e;.. if( !pSave
16ddf 70 6f 69 6e 74 20 26 26 20 70 61 67 65 72 55 73 point && pagerUs
16de0 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a eWal(pPager) ){.
16de1 20 20 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 return pager
16de2 52 6f 6c 6c 62 61 63 6b 57 61 6c 28 70 50 61 67 RollbackWal(pPag
16de3 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 er);. }.. /* U
16de4 73 65 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e se pPager->journ
16de5 61 6c 4f 66 66 20 61 73 20 74 68 65 20 65 66 66 alOff as the eff
16de6 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 ective size of t
16de7 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b he main rollback
16de8 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 . ** journal.
16de9 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 The actual file
16dea 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 might be larger
16deb 74 68 61 6e 20 74 68 69 73 20 69 6e 0a 20 20 2a than this in. *
16dec 2a 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d * PAGER_JOURNALM
16ded 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 6f 72 20 ODE_TRUNCATE or
16dee 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
16def 45 5f 50 45 52 53 49 53 54 2e 20 20 42 75 74 20 E_PERSIST. But
16df0 61 6e 79 74 68 69 6e 67 0a 20 20 2a 2a 20 70 61 anything. ** pa
16df1 73 74 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e st pPager->journ
16df2 61 6c 4f 66 66 20 69 73 20 6f 66 66 2d 6c 69 6d alOff is off-lim
16df3 69 74 73 20 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a its to us.. */.
16df4 20 20 73 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e szJ = pPager->
16df5 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 61 73 journalOff;. as
16df6 73 65 72 74 28 20 70 61 67 65 72 55 73 65 57 61 sert( pagerUseWa
16df7 6c 28 70 50 61 67 65 72 29 3d 3d 30 20 7c 7c 20 l(pPager)==0 ||
16df8 73 7a 4a 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 szJ==0 );.. /*
16df9 42 65 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67 Begin by rolling
16dfa 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72 back records fr
16dfb 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 om the main jour
16dfc 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a nal starting at.
16dfd 20 20 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f ** PagerSavepo
16dfe 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 int.iOffset and
16dff 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 continuing to th
16e00 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 e next journal h
16e01 65 61 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72 eader.. ** Ther
16e02 65 20 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72 e might be recor
16e03 64 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a ds in the main j
16e04 6f 75 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65 ournal that have
16e05 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 a page number.
16e06 20 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e ** greater than
16e07 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 the current dat
16e08 61 62 61 73 65 20 73 69 7a 65 20 28 70 50 61 67 abase size (pPag
16e09 65 72 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20 er->dbSize) but
16e0a 74 68 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 those. ** will
16e0b 62 65 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d be skipped autom
16e0c 61 74 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73 atically. Pages
16e0d 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 70 44 are added to pD
16e0e 6f 6e 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a one as they. **
16e0f 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b are played back
16e10 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 .. */. if( pSa
16e11 76 65 70 6f 69 6e 74 20 26 26 20 21 70 61 67 65 vepoint && !page
16e12 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 rUseWal(pPager)
16e13 29 7b 0a 20 20 20 20 69 48 64 72 4f 66 66 20 3d ){. iHdrOff =
16e14 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 pSavepoint->iHd
16e15 72 4f 66 66 73 65 74 20 3f 20 70 53 61 76 65 70 rOffset ? pSavep
16e16 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 oint->iHdrOffset
16e17 20 3a 20 73 7a 4a 3b 0a 20 20 20 20 70 50 61 67 : szJ;. pPag
16e18 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
16e19 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 4f 66 pSavepoint->iOf
16e1a 66 73 65 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 fset;. while(
16e1b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
16e1c 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 & pPager->journa
16e1d 6c 4f 66 66 3c 69 48 64 72 4f 66 66 20 29 7b 0a lOff<iHdrOff ){.
16e1e 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
16e1f 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 _playback_one_pa
16e20 67 65 28 70 50 61 67 65 72 2c 20 26 70 50 61 67 ge(pPager, &pPag
16e21 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 er->journalOff,
16e22 70 44 6f 6e 65 2c 20 31 2c 20 31 29 3b 0a 20 20 pDone, 1, 1);.
16e23 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
16e24 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc!=SQLITE_DONE
16e25 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
16e26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
16e27 66 66 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f ff = 0;. }.. /
16e28 2a 20 43 6f 6e 74 69 6e 75 65 20 72 6f 6c 6c 69 * Continue rolli
16e29 6e 67 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 ng back records
16e2a 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 out of the main
16e2b 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 journal starting
16e2c 20 61 74 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 at. ** the fir
16e2d 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 st journal heade
16e2e 72 20 73 65 65 6e 20 61 6e 64 20 63 6f 6e 74 69 r seen and conti
16e2f 6e 75 69 6e 67 20 75 6e 74 69 6c 20 74 68 65 20 nuing until the
16e30 65 66 66 65 63 74 69 76 65 20 65 6e 64 0a 20 20 effective end.
16e31 2a 2a 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a ** of the main j
16e32 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 43 6f ournal file. Co
16e33 6e 74 69 6e 75 65 20 74 6f 20 73 6b 69 70 20 6f ntinue to skip o
16e34 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 ut-of-range page
16e35 73 20 61 6e 64 0a 20 20 2a 2a 20 63 6f 6e 74 69 s and. ** conti
16e36 6e 75 65 20 61 64 64 69 6e 67 20 70 61 67 65 73 nue adding pages
16e37 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 6f 20 rolled back to
16e38 70 44 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 77 68 pDone.. */. wh
16e39 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ile( rc==SQLITE_
16e3a 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f OK && pPager->jo
16e3b 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 20 29 7b 0a urnalOff<szJ ){.
16e3c 20 20 20 20 75 33 32 20 69 69 3b 20 20 20 20 20 u32 ii;
16e3d 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
16e3e 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 33 ounter */. u3
16e3f 32 20 6e 4a 52 65 63 20 3d 20 30 3b 20 20 20 20 2 nJRec = 0;
16e40 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4a 6f /* Number of Jo
16e41 75 72 6e 61 6c 20 52 65 63 6f 72 64 73 20 2a 2f urnal Records */
16e42 0a 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a . u32 dummy;.
16e43 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 rc = readJou
16e44 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 rnalHdr(pPager,
16e45 30 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20 0, szJ, &nJRec,
16e46 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 &dummy);. ass
16e47 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
16e48 44 4f 4e 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 0a DONE );.. /*.
16e49 20 20 20 20 2a 2a 20 54 68 65 20 22 70 50 61 67 ** The "pPag
16e4a 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a er->journalHdr+J
16e4b 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
16e4c 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a ager)==pPager->j
16e4d 6f 75 72 6e 61 6c 4f 66 66 22 0a 20 20 20 20 2a ournalOff". *
16e4e 2a 20 74 65 73 74 20 69 73 20 72 65 6c 61 74 65 * test is relate
16e4f 64 20 74 6f 20 74 69 63 6b 65 74 20 23 32 35 36 d to ticket #256
16e50 35 2e 20 20 53 65 65 20 74 68 65 20 64 69 73 63 5. See the disc
16e51 75 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 20 20 ussion in the.
16e52 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 ** pager_playb
16e53 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 ack() function f
16e54 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
16e55 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a formation.. *
16e56 2f 0a 20 20 20 20 69 66 28 20 6e 4a 52 65 63 3d /. if( nJRec=
16e57 3d 30 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 =0 . && pPag
16e58 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a er->journalHdr+J
16e59 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
16e5a 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a ager)==pPager->j
16e5b 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 20 20 29 7b ournalOff. ){
16e5c 0a 20 20 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 . nJRec = (
16e5d 75 33 32 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 u32)((szJ - pPag
16e5e 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f er->journalOff)/
16e5f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 JOURNAL_PG_SZ(pP
16e60 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 ager));. }.
16e61 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d for(ii=0; rc==
16e62 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c SQLITE_OK && ii<
16e63 6e 4a 52 65 63 20 26 26 20 70 50 61 67 65 72 2d nJRec && pPager-
16e64 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b >journalOff<szJ;
16e65 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 ii++){. rc
16e66 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 = pager_playbac
16e67 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 k_one_page(pPage
16e68 72 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 r, &pPager->jour
16e69 6e 61 6c 4f 66 66 2c 20 70 44 6f 6e 65 2c 20 31 nalOff, pDone, 1
16e6a 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , 1);. }.
16e6b 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 assert( rc!=SQLI
16e6c 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 TE_DONE );. }.
16e6d 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
16e6e 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 ITE_OK || pPager
16e6f 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 3d 73 7a ->journalOff>=sz
16e70 4a 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c J );.. /* Final
16e71 6c 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 70 61 ly, rollback pa
16e72 67 65 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62 ges from the sub
16e73 2d 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67 65 20 -journal. Page
16e74 74 68 61 74 20 77 65 72 65 0a 20 20 2a 2a 20 70 that were. ** p
16e75 72 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 reviously rolled
16e76 20 62 61 63 6b 20 6f 75 74 20 6f 66 20 74 68 65 back out of the
16e77 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 main journal (a
16e78 6e 64 20 61 72 65 20 68 65 6e 63 65 20 69 6e 20 nd are hence in
16e79 70 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 6c 6c pDone). ** will
16e7a 20 62 65 20 73 6b 69 70 70 65 64 2e 20 20 4f 75 be skipped. Ou
16e7b 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 t-of-range pages
16e7c 20 61 72 65 20 61 6c 73 6f 20 73 6b 69 70 70 65 are also skippe
16e7d 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 d.. */. if( pS
16e7e 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 avepoint ){.
16e7f 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 20 u32 ii;
16e80 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
16e81 65 72 20 2a 2f 0a 20 20 20 20 69 36 34 20 6f 66 er */. i64 of
16e82 66 73 65 74 20 3d 20 28 69 36 34 29 70 53 61 76 fset = (i64)pSav
16e83 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 2a epoint->iSubRec*
16e84 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 (4+pPager->pageS
16e85 69 7a 65 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 ize);.. if( p
16e86 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 agerUseWal(pPage
16e87 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d r) ){. rc =
16e88 20 73 71 6c 69 74 65 33 57 61 6c 53 61 76 65 70 sqlite3WalSavep
16e89 6f 69 6e 74 55 6e 64 6f 28 70 50 61 67 65 72 2d ointUndo(pPager-
16e8a 3e 70 57 61 6c 2c 20 70 53 61 76 65 70 6f 69 6e >pWal, pSavepoin
16e8b 74 2d 3e 61 57 61 6c 44 61 74 61 29 3b 0a 20 20 t->aWalData);.
16e8c 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d 70 }. for(ii=p
16e8d 53 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 Savepoint->iSubR
16e8e 65 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f ec; rc==SQLITE_O
16e8f 4b 20 26 26 20 69 69 3c 70 50 61 67 65 72 2d 3e K && ii<pPager->
16e90 6e 53 75 62 52 65 63 3b 20 69 69 2b 2b 29 7b 0a nSubRec; ii++){.
16e91 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 66 assert( of
16e92 66 73 65 74 3d 3d 28 69 36 34 29 69 69 2a 28 34 fset==(i64)ii*(4
16e93 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a +pPager->pageSiz
16e94 65 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d e) );. rc =
16e95 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f pager_playback_
16e96 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c one_page(pPager,
16e97 20 26 6f 66 66 73 65 74 2c 20 70 44 6f 6e 65 2c &offset, pDone,
16e98 20 30 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 0, 1);. }.
16e99 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 assert( rc!=SQ
16e9a 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d LITE_DONE );. }
16e9b 0a 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 .. sqlite3Bitve
16e9c 63 44 65 73 74 72 6f 79 28 70 44 6f 6e 65 29 3b cDestroy(pDone);
16e9d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
16e9e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 E_OK ){. pPag
16e9f 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
16ea0 20 73 7a 4a 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 szJ;. }.. ret
16ea1 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
16ea2 20 43 68 61 6e 67 65 20 74 68 65 20 6d 61 78 69 Change the maxi
16ea3 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e mum number of in
16ea4 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 68 -memory pages th
16ea5 61 74 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a at are allowed..
16ea6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
16ea7 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
16ea8 67 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 gerSetCachesize(
16ea9 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
16eaa 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 73 71 nt mxPage){. sq
16eab 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 lite3PcacheSetCa
16eac 63 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e chesize(pPager->
16ead 70 50 43 61 63 68 65 2c 20 6d 78 50 61 67 65 29 pPCache, mxPage)
16eae 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
16eaf 61 73 20 6d 75 63 68 20 6d 65 6d 6f 72 79 20 61 as much memory a
16eb0 73 20 70 6f 73 73 69 62 6c 65 20 66 72 6f 6d 20 s possible from
16eb1 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 the pager..*/.SQ
16eb2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
16eb3 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 d sqlite3PagerSh
16eb4 72 69 6e 6b 28 50 61 67 65 72 20 2a 70 50 61 67 rink(Pager *pPag
16eb5 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 er){. sqlite3Pc
16eb6 61 63 68 65 53 68 72 69 6e 6b 28 70 50 61 67 65 acheShrink(pPage
16eb7 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a r->pPCache);.}..
16eb8 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 74 68 65 /*.** Adjust the
16eb9 20 72 6f 62 75 73 74 6e 65 73 73 20 6f 66 20 74 robustness of t
16eba 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 he database to d
16ebb 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 amage due to OS
16ebc 63 72 61 73 68 65 73 0a 2a 2a 20 6f 72 20 70 6f crashes.** or po
16ebd 77 65 72 20 66 61 69 6c 75 72 65 73 20 62 79 20 wer failures by
16ebe 63 68 61 6e 67 69 6e 67 20 74 68 65 20 6e 75 6d changing the num
16ebf 62 65 72 20 6f 66 20 73 79 6e 63 73 28 29 73 20 ber of syncs()s
16ec0 77 68 65 6e 20 77 72 69 74 69 6e 67 0a 2a 2a 20 when writing.**
16ec1 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
16ec2 72 6e 61 6c 2e 20 20 54 68 65 72 65 20 61 72 65 rnal. There are
16ec3 20 74 68 72 65 65 20 6c 65 76 65 6c 73 3a 0a 2a three levels:.*
16ec4 2a 0a 2a 2a 20 20 20 20 4f 46 46 20 20 20 20 20 *.** OFF
16ec5 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 sqlite3OsSync(
16ec6 29 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 ) is never calle
16ec7 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 d. This is the
16ec8 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 default.**
16ec9 20 20 20 20 20 20 20 20 66 6f 72 20 74 65 6d 70 for temp
16eca 6f 72 61 72 79 20 61 6e 64 20 74 72 61 6e 73 69 orary and transi
16ecb 65 6e 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a ent files..**.**
16ecc 20 20 20 20 4e 4f 52 4d 41 4c 20 20 20 20 54 68 NORMAL Th
16ecd 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e e journal is syn
16ece 63 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 20 ced once before
16ecf 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e 20 writes begin on
16ed0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
16ed1 20 20 20 20 64 61 74 61 62 61 73 65 2e 20 20 54 database. T
16ed2 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 his is normally
16ed3 61 64 65 71 75 61 74 65 20 70 72 6f 74 65 63 74 adequate protect
16ed4 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20 20 20 20 20 ion, but.**
16ed5 20 20 20 20 20 20 20 20 20 69 74 20 69 73 20 74 it is t
16ed6 68 65 6f 72 65 74 69 63 61 6c 6c 79 20 70 6f 73 heoretically pos
16ed7 73 69 62 6c 65 2c 20 74 68 6f 75 67 68 20 76 65 sible, though ve
16ed8 72 79 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 ry unlikely,.**
16ed9 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 61 tha
16eda 74 20 61 6e 20 69 6e 6f 70 65 72 74 75 6e 65 20 t an inopertune
16edb 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f power failure co
16edc 75 6c 64 20 6c 65 61 76 65 20 74 68 65 20 6a 6f uld leave the jo
16edd 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 urnal.**
16ede 20 20 20 20 20 20 69 6e 20 61 20 73 74 61 74 65 in a state
16edf 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61 75 which would cau
16ee0 73 65 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 se damage to the
16ee1 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 database.**
16ee2 20 20 20 20 20 20 20 20 20 20 77 68 65 6e 20 69 when i
16ee3 74 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b t is rolled back
16ee4 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c 20 ..**.** FULL
16ee5 20 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c The journal
16ee6 20 69 73 20 73 79 6e 63 65 64 20 74 77 69 63 65 is synced twice
16ee7 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 20 62 before writes b
16ee8 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 egin on the.**
16ee9 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 data
16eea 62 61 73 65 20 28 77 69 74 68 20 73 6f 6d 65 20 base (with some
16eeb 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
16eec 6d 61 74 69 6f 6e 20 2d 20 74 68 65 20 6e 52 65 mation - the nRe
16eed 63 20 66 69 65 6c 64 0a 2a 2a 20 20 20 20 20 20 c field.**
16eee 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 6a of the j
16eef 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2d 20 ournal header -
16ef0 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 69 6e being written in
16ef1 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f between the two
16ef2 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
16ef3 20 73 79 6e 63 73 29 2e 20 20 49 66 20 77 65 20 syncs). If we
16ef4 61 73 73 75 6d 65 20 74 68 61 74 20 77 72 69 74 assume that writ
16ef5 69 6e 67 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 ing a.**
16ef6 20 20 20 20 20 20 73 69 6e 67 6c 65 20 64 69 73 single dis
16ef7 6b 20 73 65 63 74 6f 72 20 69 73 20 61 74 6f 6d k sector is atom
16ef8 69 63 2c 20 74 68 65 6e 20 74 68 69 73 20 6d 6f ic, then this mo
16ef9 64 65 20 70 72 6f 76 69 64 65 73 0a 2a 2a 20 20 de provides.**
16efa 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 75 assu
16efb 72 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 6a rance that the j
16efc 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 ournal will not
16efd 62 65 20 63 6f 72 72 75 70 74 65 64 20 74 6f 20 be corrupted to
16efe 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
16eff 20 20 20 20 70 6f 69 6e 74 20 6f 66 20 63 61 75 point of cau
16f00 73 69 6e 67 20 64 61 6d 61 67 65 20 74 6f 20 74 sing damage to t
16f01 68 65 20 64 61 74 61 62 61 73 65 20 64 75 72 69 he database duri
16f02 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a ng rollback..**.
16f03 2a 2a 20 54 68 65 20 61 62 6f 76 65 20 69 73 20 ** The above is
16f04 66 6f 72 20 61 20 72 6f 6c 6c 62 61 63 6b 2d 6a for a rollback-j
16f05 6f 75 72 6e 61 6c 20 6d 6f 64 65 2e 20 20 46 6f ournal mode. Fo
16f06 72 20 57 41 4c 20 6d 6f 64 65 2c 20 4f 46 46 20 r WAL mode, OFF
16f07 63 6f 6e 74 69 6e 75 65 73 0a 2a 2a 20 74 6f 20 continues.** to
16f08 6d 65 61 6e 20 74 68 61 74 20 6e 6f 20 73 79 6e mean that no syn
16f09 63 73 20 65 76 65 72 20 6f 63 63 75 72 2e 20 20 cs ever occur.
16f0a 4e 4f 52 4d 41 4c 20 6d 65 61 6e 73 20 74 68 61 NORMAL means tha
16f0b 74 20 74 68 65 20 57 41 4c 20 69 73 20 73 79 6e t the WAL is syn
16f0c 63 65 64 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 ced.** prior to
16f0d 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 68 65 the start of che
16f0e 63 6b 70 6f 69 6e 74 20 61 6e 64 20 74 68 61 74 ckpoint and that
16f0f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
16f10 6c 65 20 69 73 20 73 79 6e 63 65 64 0a 2a 2a 20 le is synced.**
16f11 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f at the conclusio
16f12 6e 20 6f 66 20 74 68 65 20 63 68 65 63 6b 70 6f n of the checkpo
16f13 69 6e 74 20 69 66 20 74 68 65 20 65 6e 74 69 72 int if the entir
16f14 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 e content of the
16f15 20 57 41 4c 0a 2a 2a 20 77 61 73 20 77 72 69 74 WAL.** was writ
16f16 74 65 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 ten back into th
16f17 65 20 64 61 74 61 62 61 73 65 2e 20 20 42 75 74 e database. But
16f18 20 6e 6f 20 73 79 6e 63 20 6f 70 65 72 61 74 69 no sync operati
16f19 6f 6e 73 20 6f 63 63 75 72 20 66 6f 72 0a 2a 2a ons occur for.**
16f1a 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 63 6f 6d an ordinary com
16f1b 6d 69 74 20 69 6e 20 4e 4f 52 4d 41 4c 20 6d 6f mit in NORMAL mo
16f1c 64 65 20 77 69 74 68 20 57 41 4c 2e 20 20 46 55 de with WAL. FU
16f1d 4c 4c 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 LL means that th
16f1e 65 20 57 41 4c 0a 2a 2a 20 66 69 6c 65 20 69 73 e WAL.** file is
16f1f 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f 77 69 6e synced followin
16f20 67 20 65 61 63 68 20 63 6f 6d 6d 69 74 20 6f 70 g each commit op
16f21 65 72 61 74 69 6f 6e 2c 20 69 6e 20 61 64 64 69 eration, in addi
16f22 74 69 6f 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 73 tion to the.** s
16f23 79 6e 63 73 20 61 73 73 6f 63 69 61 74 65 64 20 yncs associated
16f24 77 69 74 68 20 4e 4f 52 4d 41 4c 2e 0a 2a 2a 0a with NORMAL..**.
16f25 2a 2a 20 44 6f 20 6e 6f 74 20 63 6f 6e 66 75 73 ** Do not confus
16f26 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 46 55 e synchronous=FU
16f27 4c 4c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 53 LL with SQLITE_S
16f28 59 4e 43 5f 46 55 4c 4c 2e 20 20 54 68 65 0a 2a YNC_FULL. The.*
16f29 2a 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 * SQLITE_SYNC_FU
16f2a 4c 4c 20 6d 61 63 72 6f 20 6d 65 61 6e 73 20 74 LL macro means t
16f2b 6f 20 75 73 65 20 74 68 65 20 4d 61 63 4f 53 58 o use the MacOSX
16f2c 2d 73 74 79 6c 65 20 66 75 6c 6c 2d 66 73 79 6e -style full-fsyn
16f2d 63 0a 2a 2a 20 75 73 69 6e 67 20 66 63 6e 74 6c c.** using fcntl
16f2e 28 46 5f 46 55 4c 4c 46 53 59 4e 43 29 2e 20 20 (F_FULLFSYNC).
16f2f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d SQLITE_SYNC_NORM
16f30 41 4c 20 6d 65 61 6e 73 20 74 6f 20 64 6f 20 61 AL means to do a
16f31 6e 0a 2a 2a 20 6f 72 64 69 6e 61 72 79 20 66 73 n.** ordinary fs
16f32 79 6e 63 28 29 20 63 61 6c 6c 2e 20 20 54 68 65 ync() call. The
16f33 72 65 20 69 73 20 6e 6f 20 64 69 66 66 65 72 65 re is no differe
16f34 6e 63 65 20 62 65 74 77 65 65 6e 20 53 51 4c 49 nce between SQLI
16f35 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 2a 2a 20 TE_SYNC_FULL.**
16f36 61 6e 64 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f and SQLITE_SYNC_
16f37 4e 4f 52 4d 41 4c 20 6f 6e 20 70 6c 61 74 66 6f NORMAL on platfo
16f38 72 6d 73 20 6f 74 68 65 72 20 74 68 61 6e 20 4d rms other than M
16f39 61 63 4f 53 58 2e 20 20 42 75 74 20 74 68 65 0a acOSX. But the.
16f3a 2a 2a 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 46 ** synchronous=F
16f3b 55 4c 4c 20 76 65 72 73 75 73 20 73 79 6e 63 68 ULL versus synch
16f3c 72 6f 6e 6f 75 73 3d 4e 4f 52 4d 41 4c 20 73 65 ronous=NORMAL se
16f3d 74 74 69 6e 67 20 64 65 74 65 72 6d 69 6e 65 73 tting determines
16f3e 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 78 53 79 when.** the xSy
16f3f 6e 63 20 70 72 69 6d 69 74 69 76 65 20 69 73 20 nc primitive is
16f40 63 61 6c 6c 65 64 20 61 6e 64 20 69 73 20 72 65 called and is re
16f41 6c 65 76 61 6e 74 20 74 6f 20 61 6c 6c 20 70 6c levant to all pl
16f42 61 74 66 6f 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 4e atforms..**.** N
16f43 75 6d 65 72 69 63 20 76 61 6c 75 65 73 20 61 73 umeric values as
16f44 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
16f45 65 73 65 20 73 74 61 74 65 73 20 61 72 65 20 4f ese states are O
16f46 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c FF==1, NORMAL=2,
16f47 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a .** and FULL=3..
16f48 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
16f49 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 E_OMIT_PAGER_PRA
16f4a 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 GMAS.SQLITE_PRIV
16f4b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
16f4c 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 PagerSetSafetyLe
16f4d 76 65 6c 28 0a 20 20 50 61 67 65 72 20 2a 70 50 vel(. Pager *pP
16f4e 61 67 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 ager, /*
16f4f 54 68 65 20 70 61 67 65 72 20 74 6f 20 73 65 74 The pager to set
16f50 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 66 6f safety level fo
16f51 72 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 76 65 6c r */. int level
16f52 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
16f53 50 52 41 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f PRAGMA synchrono
16f54 75 73 2e 20 20 31 3d 4f 46 46 2c 20 32 3d 4e 4f us. 1=OFF, 2=NO
16f55 52 4d 41 4c 2c 20 33 3d 46 55 4c 4c 20 2a 2f 20 RMAL, 3=FULL */
16f56 20 0a 20 20 69 6e 74 20 62 46 75 6c 6c 46 73 79 . int bFullFsy
16f57 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 50 52 41 nc, /* PRA
16f58 47 4d 41 20 66 75 6c 6c 66 73 79 6e 63 20 2a 2f GMA fullfsync */
16f59 0a 20 20 69 6e 74 20 62 43 6b 70 74 46 75 6c 6c . int bCkptFull
16f5a 46 73 79 6e 63 20 20 20 20 2f 2a 20 50 52 41 47 Fsync /* PRAG
16f5b 4d 41 20 63 68 65 63 6b 70 6f 69 6e 74 5f 66 75 MA checkpoint_fu
16f5c 6c 6c 66 73 79 6e 63 20 2a 2f 0a 29 7b 0a 20 20 llfsync */.){.
16f5d 61 73 73 65 72 74 28 20 6c 65 76 65 6c 3e 3d 31 assert( level>=1
16f5e 20 26 26 20 6c 65 76 65 6c 3c 3d 33 20 29 3b 0a && level<=3 );.
16f5f 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 pPager->noSync
16f60 20 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c = (level==1 ||
16f61 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
16f62 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 e) ?1:0;. pPage
16f63 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c r->fullSync = (l
16f64 65 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 evel==3 && !pPag
16f65 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31 er->tempFile) ?1
16f66 3a 30 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 :0;. if( pPager
16f67 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 ->noSync ){.
16f68 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 pPager->syncFlag
16f69 73 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 s = 0;. pPage
16f6a 72 2d 3e 63 6b 70 74 53 79 6e 63 46 6c 61 67 73 r->ckptSyncFlags
16f6b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = 0;. }else if
16f6c 28 20 62 46 75 6c 6c 46 73 79 6e 63 20 29 7b 0a ( bFullFsync ){.
16f6d 20 20 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 pPager->sync
16f6e 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 Flags = SQLITE_S
16f6f 59 4e 43 5f 46 55 4c 4c 3b 0a 20 20 20 20 70 50 YNC_FULL;. pP
16f70 61 67 65 72 2d 3e 63 6b 70 74 53 79 6e 63 46 6c ager->ckptSyncFl
16f71 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e ags = SQLITE_SYN
16f72 43 5f 46 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 20 C_FULL;. }else
16f73 69 66 28 20 62 43 6b 70 74 46 75 6c 6c 46 73 79 if( bCkptFullFsy
16f74 6e 63 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 nc ){. pPager
16f75 2d 3e 73 79 6e 63 46 6c 61 67 73 20 3d 20 53 51 ->syncFlags = SQ
16f76 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
16f77 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 6b ;. pPager->ck
16f78 70 74 53 79 6e 63 46 6c 61 67 73 20 3d 20 53 51 ptSyncFlags = SQ
16f79 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3b 0a LITE_SYNC_FULL;.
16f7a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 }else{. pPa
16f7b 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 20 3d ger->syncFlags =
16f7c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 SQLITE_SYNC_NOR
16f7d 4d 41 4c 3b 0a 20 20 20 20 70 50 61 67 65 72 2d MAL;. pPager-
16f7e 3e 63 6b 70 74 53 79 6e 63 46 6c 61 67 73 20 3d >ckptSyncFlags =
16f7f 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 SQLITE_SYNC_NOR
16f80 4d 41 4c 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 MAL;. }. pPage
16f81 72 2d 3e 77 61 6c 53 79 6e 63 46 6c 61 67 73 20 r->walSyncFlags
16f82 3d 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c = pPager->syncFl
16f83 61 67 73 3b 0a 20 20 69 66 28 20 70 50 61 67 65 ags;. if( pPage
16f84 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20 r->fullSync ){.
16f85 20 20 20 70 50 61 67 65 72 2d 3e 77 61 6c 53 79 pPager->walSy
16f86 6e 63 46 6c 61 67 73 20 7c 3d 20 57 41 4c 5f 53 ncFlags |= WAL_S
16f87 59 4e 43 5f 54 52 41 4e 53 41 43 54 49 4f 4e 53 YNC_TRANSACTIONS
16f88 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a ;. }.}.#endif..
16f89 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
16f8a 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 ing global varia
16f8b 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 ble is increment
16f8c 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 ed whenever the
16f8d 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74 65 6d library.** attem
16f8e 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20 74 65 pts to open a te
16f8f 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 20 54 mporary file. T
16f90 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 his information
16f91 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 is used for.** t
16f92 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 esting and analy
16f93 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f 0a 23 sis only. .*/.#
16f94 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
16f95 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
16f96 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d sqlite3_opentem
16f97 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e p_count = 0;.#en
16f98 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 dif../*.** Open
16f99 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
16f9a 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 ..**.** Write th
16f9b 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
16f9c 72 20 69 6e 74 6f 20 2a 70 46 69 6c 65 2e 20 52 r into *pFile. R
16f9d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
16f9e 6f 6e 20 73 75 63 63 65 73 73 20 0a 2a 2a 20 6f on success .** o
16f9f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 r some other err
16fa0 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 66 61 or code if we fa
16fa1 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c 6c 20 il. The OS will
16fa2 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a automatically .*
16fa3 2a 20 64 65 6c 65 74 65 20 74 68 65 20 74 65 6d * delete the tem
16fa4 70 6f 72 61 72 79 20 66 69 6c 65 20 77 68 65 6e porary file when
16fa5 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a it is closed..*
16fa6 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 70 *.** The flags p
16fa7 61 73 73 65 64 20 74 6f 20 74 68 65 20 56 46 53 assed to the VFS
16fa8 20 6c 61 79 65 72 20 78 4f 70 65 6e 28 29 20 63 layer xOpen() c
16fa9 61 6c 6c 20 61 72 65 20 74 68 6f 73 65 20 73 70 all are those sp
16faa 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 70 61 ecified.** by pa
16fab 72 61 6d 65 74 65 72 20 76 66 73 46 6c 61 67 73 rameter vfsFlags
16fac 20 4f 52 65 64 20 77 69 74 68 20 74 68 65 20 66 ORed with the f
16fad 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
16fae 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f SQLITE_OPEN_
16faf 52 45 41 44 57 52 49 54 45 0a 2a 2a 20 20 20 20 READWRITE.**
16fb0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 SQLITE_OPEN_CRE
16fb1 41 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 ATE.** SQLIT
16fb2 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
16fb3 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f .** SQLITE_O
16fb4 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
16fb5 45 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 E.*/.static int
16fb6 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 0a 20 pagerOpentemp(.
16fb7 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 Pager *pPager,
16fb8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
16fb9 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 ger object */.
16fba 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 sqlite3_file *pF
16fbb 69 6c 65 2c 20 20 2f 2a 20 57 72 69 74 65 20 74 ile, /* Write t
16fbc 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
16fbd 6f 72 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 or here */. int
16fbe 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 vfsFlags
16fbf 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 /* Flags pass
16fc0 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 ed through to th
16fc1 65 20 56 46 53 20 2a 2f 0a 29 7b 0a 20 20 69 6e e VFS */.){. in
16fc2 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
16fc3 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
16fc4 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 de */..#ifdef SQ
16fc5 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 LITE_TEST. sqli
16fc6 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 te3_opentemp_cou
16fc7 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73 65 64 20 66 nt++; /* Used f
16fc8 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 or testing and a
16fc9 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 20 2a 2f 0a nalysis only */.
16fca 23 65 6e 64 69 66 0a 0a 20 20 76 66 73 46 6c 61 #endif.. vfsFla
16fcb 67 73 20 7c 3d 20 20 53 51 4c 49 54 45 5f 4f 50 gs |= SQLITE_OP
16fcc 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53 EN_READWRITE | S
16fcd 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
16fce 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 E |.
16fcf 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c SQLITE_OPEN_EXCL
16fd0 55 53 49 56 45 20 7c 20 53 51 4c 49 54 45 5f 4f USIVE | SQLITE_O
16fd1 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
16fd2 45 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 E;. rc = sqlite
16fd3 33 4f 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 3OsOpen(pPager->
16fd4 70 56 66 73 2c 20 30 2c 20 70 46 69 6c 65 2c 20 pVfs, 0, pFile,
16fd5 76 66 73 46 6c 61 67 73 2c 20 30 29 3b 0a 20 20 vfsFlags, 0);.
16fd6 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 assert( rc!=SQLI
16fd7 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 TE_OK || isOpen(
16fd8 70 46 69 6c 65 29 20 29 3b 0a 20 20 72 65 74 75 pFile) );. retu
16fd9 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
16fda 53 65 74 20 74 68 65 20 62 75 73 79 20 68 61 6e Set the busy han
16fdb 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a dler function..*
16fdc 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 69 *.** The pager i
16fdd 6e 76 6f 6b 65 73 20 74 68 65 20 62 75 73 79 2d nvokes the busy-
16fde 68 61 6e 64 6c 65 72 20 69 66 20 73 71 6c 69 74 handler if sqlit
16fdf 65 33 4f 73 4c 6f 63 6b 28 29 20 72 65 74 75 72 e3OsLock() retur
16fe0 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 ns .** SQLITE_BU
16fe1 53 59 20 77 68 65 6e 20 74 72 79 69 6e 67 20 74 SY when trying t
16fe2 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d 20 6e o upgrade from n
16fe3 6f 2d 6c 6f 63 6b 20 74 6f 20 61 20 53 48 41 52 o-lock to a SHAR
16fe4 45 44 20 6c 6f 63 6b 2c 0a 2a 2a 20 6f 72 20 77 ED lock,.** or w
16fe5 68 65 6e 20 74 72 79 69 6e 67 20 74 6f 20 75 70 hen trying to up
16fe6 67 72 61 64 65 20 66 72 6f 6d 20 61 20 52 45 53 grade from a RES
16fe7 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 61 6e ERVED lock to an
16fe8 20 45 58 43 4c 55 53 49 56 45 20 0a 2a 2a 20 6c EXCLUSIVE .** l
16fe9 6f 63 6b 2e 20 49 74 20 64 6f 65 73 20 2a 6e 6f ock. It does *no
16fea 74 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 t* invoke the bu
16feb 73 79 20 68 61 6e 64 6c 65 72 20 77 68 65 6e 20 sy handler when
16fec 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d 0a 2a upgrading from.*
16fed 2a 20 53 48 41 52 45 44 20 74 6f 20 52 45 53 45 * SHARED to RESE
16fee 52 56 45 44 2c 20 6f 72 20 77 68 65 6e 20 75 70 RVED, or when up
16fef 67 72 61 64 69 6e 67 20 66 72 6f 6d 20 53 48 41 grading from SHA
16ff0 52 45 44 20 74 6f 20 45 58 43 4c 55 53 49 56 45 RED to EXCLUSIVE
16ff1 0a 2a 2a 20 28 77 68 69 63 68 20 6f 63 63 75 72 .** (which occur
16ff2 73 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 s during hot-jou
16ff3 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 20 rnal rollback).
16ff4 53 75 6d 6d 61 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 Summary:.**.**
16ff5 20 54 72 61 6e 73 69 74 69 6f 6e 20 20 20 20 20 Transition
16ff6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ff7 20 20 20 7c 20 49 6e 76 6f 6b 65 73 20 78 42 75 | Invokes xBu
16ff8 73 79 48 61 6e 64 6c 65 72 0a 2a 2a 20 20 20 2d syHandler.** -
16ff9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
16ffa 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
16ffb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
16ffc 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 4e 4f 5f -------.** NO_
16ffd 4c 4f 43 4b 20 20 20 20 20 20 20 2d 3e 20 53 48 LOCK -> SH
16ffe 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 20 7c ARED_LOCK |
16fff 20 59 65 73 0a 2a 2a 20 20 20 53 48 41 52 45 44 Yes.** SHARED
17000 5f 4c 4f 43 4b 20 20 20 2d 3e 20 52 45 53 45 52 _LOCK -> RESER
17001 56 45 44 5f 4c 4f 43 4b 20 20 20 20 7c 20 4e 6f VED_LOCK | No
17002 0a 2a 2a 20 20 20 53 48 41 52 45 44 5f 4c 4f 43 .** SHARED_LOC
17003 4b 20 20 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 K -> EXCLUSIVE
17004 5f 4c 4f 43 4b 20 20 20 7c 20 4e 6f 0a 2a 2a 20 _LOCK | No.**
17005 20 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 RESERVED_LOCK
17006 2d 3e 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 -> EXCLUSIVE_LOC
17007 4b 20 20 20 7c 20 59 65 73 0a 2a 2a 0a 2a 2a 20 K | Yes.**.**
17008 49 66 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 If the busy-hand
17009 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 ler callback ret
1700a 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 urns non-zero, t
1700b 68 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 72 he lock is .** r
1700c 65 74 72 69 65 64 2e 20 49 66 20 69 74 20 72 65 etried. If it re
1700d 74 75 72 6e 73 20 7a 65 72 6f 2c 20 74 68 65 6e turns zero, then
1700e 20 74 68 65 20 53 51 4c 49 54 45 5f 42 55 53 59 the SQLITE_BUSY
1700f 20 65 72 72 6f 72 20 69 73 0a 2a 2a 20 72 65 74 error is.** ret
17010 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c urned to the cal
17011 6c 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 72 ler of the pager
17012 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a API function..*
17013 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
17014 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
17015 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 erSetBusyhandler
17016 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 (. Pager *pPage
17017 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
17018 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
17019 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e r object */. in
1701a 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 t (*xBusyHandler
1701b 29 28 76 6f 69 64 20 2a 29 2c 20 20 20 20 20 20 )(void *),
1701c 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1701d 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 66 75 busy-handler fu
1701e 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 nction */. void
1701f 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 *pBusyHandlerAr
17020 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g
17021 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 /* Argument to
17022 70 61 73 73 20 74 6f 20 78 42 75 73 79 48 61 6e pass to xBusyHan
17023 64 6c 65 72 20 2a 2f 0a 29 7b 0a 20 20 70 50 61 dler */.){. pPa
17024 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 ger->xBusyHandle
17025 72 20 3d 20 78 42 75 73 79 48 61 6e 64 6c 65 72 r = xBusyHandler
17026 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 42 75 73 ;. pPager->pBus
17027 79 48 61 6e 64 6c 65 72 41 72 67 20 3d 20 70 42 yHandlerArg = pB
17028 75 73 79 48 61 6e 64 6c 65 72 41 72 67 3b 0a 0a usyHandlerArg;..
17029 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 if( isOpen(pPa
1702a 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 ger->fd) ){.
1702b 76 6f 69 64 20 2a 2a 61 70 20 3d 20 28 76 6f 69 void **ap = (voi
1702c 64 20 2a 2a 29 26 70 50 61 67 65 72 2d 3e 78 42 d **)&pPager->xB
1702d 75 73 79 48 61 6e 64 6c 65 72 3b 0a 20 20 20 20 usyHandler;.
1702e 61 73 73 65 72 74 28 20 28 28 69 6e 74 28 2a 29 assert( ((int(*)
1702f 28 76 6f 69 64 20 2a 29 29 28 61 70 5b 30 5d 29 (void *))(ap[0])
17030 29 3d 3d 78 42 75 73 79 48 61 6e 64 6c 65 72 20 )==xBusyHandler
17031 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 );. assert( a
17032 70 5b 31 5d 3d 3d 70 42 75 73 79 48 61 6e 64 6c p[1]==pBusyHandl
17033 65 72 41 72 67 20 29 3b 0a 20 20 20 20 73 71 6c erArg );. sql
17034 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f ite3OsFileContro
17035 6c 48 69 6e 74 28 70 50 61 67 65 72 2d 3e 66 64 lHint(pPager->fd
17036 2c 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 42 , SQLITE_FCNTL_B
17037 55 53 59 48 41 4e 44 4c 45 52 2c 20 28 76 6f 69 USYHANDLER, (voi
17038 64 20 2a 29 61 70 29 3b 0a 20 20 7d 0a 7d 0a 0a d *)ap);. }.}..
17039 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
1703a 20 70 61 67 65 20 73 69 7a 65 20 75 73 65 64 20 page size used
1703b 62 79 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a by the Pager obj
1703c 65 63 74 2e 20 54 68 65 20 6e 65 77 20 70 61 67 ect. The new pag
1703d 65 20 73 69 7a 65 20 0a 2a 2a 20 69 73 20 70 61 e size .** is pa
1703e 73 73 65 64 20 69 6e 20 2a 70 50 61 67 65 53 69 ssed in *pPageSi
1703f 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ze..**.** If the
17040 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 pager is in the
17041 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68 65 error state whe
17042 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
17043 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 0a 2a 2a is called, it.**
17044 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 54 68 65 is a no-op. The
17045 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
17046 69 73 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 is the error sta
17047 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 28 69 te error code (i
17048 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 .e. .** one of S
17049 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 61 6e 20 QLITE_IOERR, an
1704a 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 78 78 78 SQLITE_IOERR_xxx
1704b 20 73 75 62 2d 63 6f 64 65 20 6f 72 20 53 51 4c sub-code or SQL
1704c 49 54 45 5f 46 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a ITE_FULL)..**.**
1704d 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 Otherwise, if a
1704e 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ll of the follow
1704f 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a ing are true:.**
17050 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6e 65 77 20 .** * the new
17051 70 61 67 65 20 73 69 7a 65 20 28 76 61 6c 75 65 page size (value
17052 20 6f 66 20 2a 70 50 61 67 65 53 69 7a 65 29 20 of *pPageSize)
17053 69 73 20 76 61 6c 69 64 20 28 61 20 70 6f 77 65 is valid (a powe
17054 72 20 0a 2a 2a 20 20 20 20 20 6f 66 20 74 77 6f r .** of two
17055 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 between 512 and
17056 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 SQLITE_MAX_PAGE
17057 5f 53 49 5a 45 2c 20 69 6e 63 6c 75 73 69 76 65 _SIZE, inclusive
17058 29 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a ), and.**.** *
17059 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 there are no ou
1705a 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 tstanding page r
1705b 65 66 65 72 65 6e 63 65 73 2c 20 61 6e 64 0a 2a eferences, and.*
1705c 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74 *.** * the dat
1705d 61 62 61 73 65 20 69 73 20 65 69 74 68 65 72 20 abase is either
1705e 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 not an in-memory
1705f 20 64 61 74 61 62 61 73 65 20 6f 72 20 69 74 20 database or it
17060 69 73 0a 2a 2a 20 20 20 20 20 61 6e 20 69 6e 2d is.** an in-
17061 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 memory database
17062 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 63 that currently c
17063 6f 6e 73 69 73 74 73 20 6f 66 20 7a 65 72 6f 20 onsists of zero
17064 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 pages..**.** the
17065 6e 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 n the pager obje
17066 63 74 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 ct page size is
17067 73 65 74 20 74 6f 20 2a 70 50 61 67 65 53 69 7a set to *pPageSiz
17068 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
17069 70 61 67 65 20 73 69 7a 65 20 69 73 20 63 68 61 page size is cha
1706a 6e 67 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 nged, then this
1706b 66 75 6e 63 74 69 6f 6e 20 75 73 65 73 20 73 71 function uses sq
1706c 6c 69 74 65 33 50 61 67 65 72 4d 61 6c 6c 6f 63 lite3PagerMalloc
1706d 28 29 20 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e () .** to obtain
1706e 20 61 20 6e 65 77 20 50 61 67 65 72 2e 70 54 6d a new Pager.pTm
1706f 70 53 70 61 63 65 20 62 75 66 66 65 72 2e 20 49 pSpace buffer. I
17070 66 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f f this allocatio
17071 6e 20 61 74 74 65 6d 70 74 20 0a 2a 2a 20 66 61 n attempt .** fa
17072 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 ils, SQLITE_NOME
17073 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e M is returned an
17074 64 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 d the page size
17075 72 65 6d 61 69 6e 73 20 75 6e 63 68 61 6e 67 65 remains unchange
17076 64 2e 20 0a 2a 2a 20 49 6e 20 61 6c 6c 20 6f 74 d. .** In all ot
17077 68 65 72 20 63 61 73 65 73 2c 20 53 51 4c 49 54 her cases, SQLIT
17078 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
17079 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
1707a 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 20 age size is not
1707b 63 68 61 6e 67 65 64 2c 20 65 69 74 68 65 72 20 changed, either
1707c 62 65 63 61 75 73 65 20 6f 6e 65 20 6f 66 20 74 because one of t
1707d 68 65 20 65 6e 75 6d 65 72 61 74 65 64 0a 2a 2a he enumerated.**
1707e 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 conditions abov
1707f 65 20 69 73 20 6e 6f 74 20 74 72 75 65 2c 20 74 e is not true, t
17080 68 65 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 he pager was in
17081 65 72 72 6f 72 20 73 74 61 74 65 20 77 68 65 6e error state when
17082 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f this.** functio
17083 6e 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 6f 72 n was called, or
17084 20 62 65 63 61 75 73 65 20 74 68 65 20 6d 65 6d because the mem
17085 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 ory allocation a
17086 74 74 65 6d 70 74 20 66 61 69 6c 65 64 2c 20 0a ttempt failed, .
17087 2a 2a 20 74 68 65 6e 20 2a 70 50 61 67 65 53 69 ** then *pPageSi
17088 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 ze is set to the
17089 20 6f 6c 64 2c 20 72 65 74 61 69 6e 65 64 20 70 old, retained p
1708a 61 67 65 20 73 69 7a 65 20 62 65 66 6f 72 65 20 age size before
1708b 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 returning..*/.SQ
1708c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1708d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
1708e 50 61 67 65 73 69 7a 65 28 50 61 67 65 72 20 2a Pagesize(Pager *
1708f 70 50 61 67 65 72 2c 20 75 33 32 20 2a 70 50 61 pPager, u32 *pPa
17090 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 geSize, int nRes
17091 65 72 76 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 erve){. int rc
17092 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
17093 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 /* It is not pos
17094 73 69 62 6c 65 20 74 6f 20 64 6f 20 61 20 66 75 sible to do a fu
17095 6c 6c 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f ll assert_pager_
17096 73 74 61 74 65 28 29 20 68 65 72 65 2c 20 61 73 state() here, as
17097 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 this. ** funct
17098 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 ion may be calle
17099 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 50 61 d from within Pa
1709a 67 65 72 4f 70 65 6e 28 29 2c 20 62 65 66 6f 72 gerOpen(), befor
1709b 65 20 74 68 65 20 73 74 61 74 65 0a 20 20 2a 2a e the state. **
1709c 20 6f 66 20 74 68 65 20 50 61 67 65 72 20 6f 62 of the Pager ob
1709d 6a 65 63 74 20 69 73 20 69 6e 74 65 72 6e 61 6c ject is internal
1709e 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 20 ly consistent..
1709f 20 2a 2a 0a 20 20 2a 2a 20 41 74 20 6f 6e 65 20 **. ** At one
170a0 70 6f 69 6e 74 20 74 68 69 73 20 66 75 6e 63 74 point this funct
170a1 69 6f 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 ion returned an
170a2 65 72 72 6f 72 20 69 66 20 74 68 65 20 70 61 67 error if the pag
170a3 65 72 20 77 61 73 20 69 6e 20 0a 20 20 2a 2a 20 er was in . **
170a4 50 41 47 45 52 5f 45 52 52 4f 52 20 73 74 61 74 PAGER_ERROR stat
170a5 65 2e 20 42 75 74 20 73 69 6e 63 65 20 50 41 47 e. But since PAG
170a6 45 52 5f 45 52 52 4f 52 20 73 74 61 74 65 20 67 ER_ERROR state g
170a7 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 20 uarantees that.
170a8 20 2a 2a 20 74 68 65 72 65 20 69 73 20 61 74 20 ** there is at
170a9 6c 65 61 73 74 20 6f 6e 65 20 6f 75 74 73 74 61 least one outsta
170aa 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 nding page refer
170ab 65 6e 63 65 2c 20 74 68 69 73 20 66 75 6e 63 74 ence, this funct
170ac 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 61 20 6e 6f ion. ** is a no
170ad 2d 6f 70 20 66 6f 72 20 74 68 61 74 20 63 61 73 -op for that cas
170ae 65 20 61 6e 79 68 6f 77 2e 0a 20 20 2a 2f 0a 0a e anyhow.. */..
170af 20 20 75 33 32 20 70 61 67 65 53 69 7a 65 20 3d u32 pageSize =
170b0 20 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 20 61 *pPageSize;. a
170b1 73 73 65 72 74 28 20 70 61 67 65 53 69 7a 65 3d ssert( pageSize=
170b2 3d 30 20 7c 7c 20 28 70 61 67 65 53 69 7a 65 3e =0 || (pageSize>
170b3 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a 65 =512 && pageSize
170b4 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 <=SQLITE_MAX_PAG
170b5 45 5f 53 49 5a 45 29 20 29 3b 0a 20 20 69 66 28 E_SIZE) );. if(
170b6 20 28 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d (pPager->memDb=
170b7 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 =0 || pPager->db
170b8 53 69 7a 65 3d 3d 30 29 0a 20 20 20 26 26 20 73 Size==0). && s
170b9 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
170ba 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 ount(pPager->pPC
170bb 61 63 68 65 29 3d 3d 30 20 0a 20 20 20 26 26 20 ache)==0 . &&
170bc 70 61 67 65 53 69 7a 65 20 26 26 20 70 61 67 65 pageSize && page
170bd 53 69 7a 65 21 3d 28 75 33 32 29 70 50 61 67 65 Size!=(u32)pPage
170be 72 2d 3e 70 61 67 65 53 69 7a 65 20 0a 20 20 29 r->pageSize . )
170bf 7b 0a 20 20 20 20 63 68 61 72 20 2a 70 4e 65 77 {. char *pNew
170c0 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20 = NULL;
170c1 20 20 20 20 20 2f 2a 20 4e 65 77 20 74 65 6d 70 /* New temp
170c2 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 69 36 space */. i6
170c3 34 20 6e 42 79 74 65 20 3d 20 30 3b 0a 0a 20 20 4 nByte = 0;..
170c4 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53 if( pPager->eS
170c5 74 61 74 65 3e 50 41 47 45 52 5f 4f 50 45 4e 20 tate>PAGER_OPEN
170c6 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 && isOpen(pPager
170c7 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 20 20 72 ->fd) ){. r
170c8 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
170c9 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 eSize(pPager->fd
170ca 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 7d , &nByte);. }
170cb 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
170cc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
170cd 70 4e 65 77 20 3d 20 28 63 68 61 72 20 2a 29 73 pNew = (char *)s
170ce 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 qlite3PageMalloc
170cf 28 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 (pageSize);.
170d0 20 20 69 66 28 20 21 70 4e 65 77 20 29 20 72 63 if( !pNew ) rc
170d1 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
170d2 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
170d3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
170d4 0a 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 . pager_res
170d5 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 et(pPager);.
170d6 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
170d7 20 3d 20 28 50 67 6e 6f 29 28 28 6e 42 79 74 65 = (Pgno)((nByte
170d8 2b 70 61 67 65 53 69 7a 65 2d 31 29 2f 70 61 67 +pageSize-1)/pag
170d9 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 70 50 eSize);. pP
170da 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3d ager->pageSize =
170db 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 pageSize;.
170dc 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 sqlite3PageFree
170dd 28 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 (pPager->pTmpSpa
170de 63 65 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 ce);. pPage
170df 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 70 r->pTmpSpace = p
170e0 4e 65 77 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 New;. sqlit
170e1 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 e3PcacheSetPageS
170e2 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ize(pPager->pPCa
170e3 63 68 65 2c 20 70 61 67 65 53 69 7a 65 29 3b 0a che, pageSize);.
170e4 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 50 }. }.. *pP
170e5 61 67 65 53 69 7a 65 20 3d 20 70 50 61 67 65 72 ageSize = pPager
170e6 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 69 66 ->pageSize;. if
170e7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
170e8 29 7b 0a 20 20 20 20 69 66 28 20 6e 52 65 73 65 ){. if( nRese
170e9 72 76 65 3c 30 20 29 20 6e 52 65 73 65 72 76 65 rve<0 ) nReserve
170ea 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 = pPager->nRese
170eb 72 76 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 rve;. assert(
170ec 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26 26 20 nReserve>=0 &&
170ed 6e 52 65 73 65 72 76 65 3c 31 30 30 30 20 29 3b nReserve<1000 );
170ee 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 . pPager->nRe
170ef 73 65 72 76 65 20 3d 20 28 69 31 36 29 6e 52 65 serve = (i16)nRe
170f0 73 65 72 76 65 3b 0a 20 20 20 20 70 61 67 65 72 serve;. pager
170f1 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 65 ReportSize(pPage
170f2 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e r);. }. return
170f3 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
170f4 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
170f5 6f 20 74 68 65 20 22 74 65 6d 70 6f 72 61 72 79 o the "temporary
170f6 20 70 61 67 65 22 20 62 75 66 66 65 72 20 68 65 page" buffer he
170f7 6c 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a ld internally.**
170f8 20 62 79 20 74 68 65 20 70 61 67 65 72 2e 20 20 by the pager.
170f9 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 This is a buffer
170fa 20 74 68 61 74 20 69 73 20 62 69 67 20 65 6e 6f that is big eno
170fb 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a ugh to hold the.
170fc 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e ** entire conten
170fd 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 t of a database
170fe 70 61 67 65 2e 20 20 54 68 69 73 20 62 75 66 66 page. This buff
170ff 65 72 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 er is used inter
17100 6e 61 6c 6c 79 0a 2a 2a 20 64 75 72 69 6e 67 20 nally.** during
17101 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 77 69 6c rollback and wil
17102 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e l be overwritten
17103 20 77 68 65 6e 65 76 65 72 20 61 20 72 6f 6c 6c whenever a roll
17104 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 back.** occurs.
17105 20 42 75 74 20 6f 74 68 65 72 20 6d 6f 64 75 6c But other modul
17106 65 73 20 61 72 65 20 66 72 65 65 20 74 6f 20 75 es are free to u
17107 73 65 20 69 74 20 74 6f 6f 2c 20 61 73 20 6c 6f se it too, as lo
17108 6e 67 20 61 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c ng as.** no roll
17109 62 61 63 6b 73 20 61 72 65 20 68 61 70 70 65 6e backs are happen
1710a 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ing..*/.SQLITE_P
1710b 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
1710c 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 ite3PagerTempSpa
1710d 63 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ce(Pager *pPager
1710e 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 ){. return pPag
1710f 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 7d er->pTmpSpace;.}
17110 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 ../*.** Attempt
17111 74 6f 20 73 65 74 20 74 68 65 20 6d 61 78 69 6d to set the maxim
17112 75 6d 20 64 61 74 61 62 61 73 65 20 70 61 67 65 um database page
17113 20 63 6f 75 6e 74 20 69 66 20 6d 78 50 61 67 65 count if mxPage
17114 20 69 73 20 70 6f 73 69 74 69 76 65 2e 20 0a 2a is positive. .*
17115 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 * Make no change
17116 73 20 69 66 20 6d 78 50 61 67 65 20 69 73 20 7a s if mxPage is z
17117 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 2e ero or negative.
17118 20 20 41 6e 64 20 6e 65 76 65 72 20 72 65 64 75 And never redu
17119 63 65 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 ce the.** maximu
1711a 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 62 65 6c m page count bel
1711b 6f 77 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 ow the current s
1711c 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
1711d 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72 ase..**.** Regar
1711e 64 6c 65 73 73 20 6f 66 20 6d 78 50 61 67 65 2c dless of mxPage,
1711f 20 72 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 return the curr
17120 65 6e 74 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 ent maximum page
17121 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 count..*/.SQLIT
17122 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
17123 6c 69 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 lite3PagerMaxPag
17124 65 43 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 eCount(Pager *pP
17125 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 ager, int mxPage
17126 29 7b 0a 20 20 69 66 28 20 6d 78 50 61 67 65 3e ){. if( mxPage>
17127 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 0 ){. pPager-
17128 3e 6d 78 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65 >mxPgno = mxPage
17129 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
1712a 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d pPager->eState!=
1712b 50 41 47 45 52 5f 4f 50 45 4e 20 29 3b 20 20 20 PAGER_OPEN );
1712c 20 20 20 2f 2a 20 43 61 6c 6c 65 64 20 6f 6e 6c /* Called onl
1712d 79 20 62 79 20 4f 50 5f 4d 61 78 50 67 63 6e 74 y by OP_MaxPgcnt
1712e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
1712f 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3e 3d 70 50 ager->mxPgno>=pP
17130 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 3b 20 ager->dbSize );
17131 20 2f 2a 20 4f 50 5f 4d 61 78 50 67 63 6e 74 20 /* OP_MaxPgcnt
17132 65 6e 66 6f 72 63 65 73 20 74 68 69 73 20 2a 2f enforces this */
17133 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 . return pPager
17134 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a ->mxPgno;.}../*.
17135 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
17136 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 set of routines
17137 20 61 72 65 20 75 73 65 64 20 74 6f 20 64 69 73 are used to dis
17138 61 62 6c 65 20 74 68 65 20 73 69 6d 75 6c 61 74 able the simulat
17139 65 64 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 ed.** I/O error
1713a 6d 65 63 68 61 6e 69 73 6d 2e 20 20 54 68 65 73 mechanism. Thes
1713b 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 e routines are u
1713c 73 65 64 20 74 6f 20 61 76 6f 69 64 20 73 69 6d sed to avoid sim
1713d 75 6c 61 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73 ulated.** errors
1713e 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 in places where
1713f 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 we do not care
17140 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a about errors..**
17141 0a 2a 2a 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c .** Unless -DSQL
17142 49 54 45 5f 54 45 53 54 3d 31 20 69 73 20 75 73 ITE_TEST=1 is us
17143 65 64 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e ed, these routin
17144 65 73 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 es are all no-op
17145 73 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 s.** and generat
17146 65 20 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 e no code..*/.#i
17147 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
17148 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 .SQLITE_API exte
17149 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 rn int sqlite3_i
1714a 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b o_error_pending;
1714b 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 .SQLITE_API exte
1714c 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 rn int sqlite3_i
1714d 6f 5f 65 72 72 6f 72 5f 68 69 74 3b 0a 73 74 61 o_error_hit;.sta
1714e 74 69 63 20 69 6e 74 20 73 61 76 65 64 5f 63 6e tic int saved_cn
1714f 74 3b 0a 76 6f 69 64 20 64 69 73 61 62 6c 65 5f t;.void disable_
17150 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
17151 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 61 76 ors(void){. sav
17152 65 64 5f 63 6e 74 20 3d 20 73 71 6c 69 74 65 33 ed_cnt = sqlite3
17153 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e _io_error_pendin
17154 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f g;. sqlite3_io_
17155 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 error_pending =
17156 2d 31 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61 62 6c -1;.}.void enabl
17157 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
17158 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 rrors(void){. s
17159 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
1715a 70 65 6e 64 69 6e 67 20 3d 20 73 61 76 65 64 5f pending = saved_
1715b 63 6e 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 cnt;.}.#else.# d
1715c 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 efine disable_si
1715d 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
1715e 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 s().# define ena
1715f 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f ble_simulated_io
17160 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 _errors().#endif
17161 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 ../*.** Read the
17162 20 66 69 72 73 74 20 4e 20 62 79 74 65 73 20 66 first N bytes f
17163 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e rom the beginnin
17164 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e g of the file in
17165 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74 68 61 to memory.** tha
17166 74 20 70 44 65 73 74 20 70 6f 69 6e 74 73 20 74 t pDest points t
17167 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 o. .**.** If the
17168 20 70 61 67 65 72 20 77 61 73 20 6f 70 65 6e 65 pager was opene
17169 64 20 6f 6e 20 61 20 74 72 61 6e 73 69 65 6e 74 d on a transient
1716a 20 66 69 6c 65 20 28 7a 46 69 6c 65 6e 61 6d 65 file (zFilename
1716b 3d 3d 22 22 29 2c 20 6f 72 0a 2a 2a 20 6f 70 65 ==""), or.** ope
1716c 6e 65 64 20 6f 6e 20 61 20 66 69 6c 65 20 6c 65 ned on a file le
1716d 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20 ss than N bytes
1716e 69 6e 20 73 69 7a 65 2c 20 74 68 65 20 6f 75 74 in size, the out
1716f 70 75 74 20 62 75 66 66 65 72 20 69 73 0a 2a 2a put buffer is.**
17170 20 7a 65 72 6f 65 64 20 61 6e 64 20 53 51 4c 49 zeroed and SQLI
17171 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 TE_OK returned.
17172 54 68 65 20 72 61 74 69 6f 6e 61 6c 65 20 66 6f The rationale fo
17173 72 20 74 68 69 73 20 69 73 20 74 68 61 74 20 74 r this is that t
17174 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e his .** function
17175 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 is used to read
17176 20 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 database header
17177 73 2c 20 61 6e 64 20 61 20 6e 65 77 20 74 72 61 s, and a new tra
17178 6e 73 69 65 6e 74 20 6f 72 0a 2a 2a 20 7a 65 72 nsient or.** zer
17179 6f 20 73 69 7a 65 64 20 64 61 74 61 62 61 73 65 o sized database
1717a 20 68 61 73 20 61 20 68 65 61 64 65 72 20 74 68 has a header th
1717b 61 6e 20 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 an consists enti
1717c 72 65 6c 79 20 6f 66 20 7a 65 72 6f 65 73 2e 0a rely of zeroes..
1717d 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 49 4f 20 **.** If any IO
1717e 65 72 72 6f 72 20 61 70 61 72 74 20 66 72 6f 6d error apart from
1717f 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 SQLITE_IOERR_SH
17180 4f 52 54 5f 52 45 41 44 20 69 73 20 65 6e 63 6f ORT_READ is enco
17181 75 6e 74 65 72 65 64 2c 0a 2a 2a 20 74 68 65 20 untered,.** the
17182 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
17183 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 turned to the ca
17184 6c 6c 65 72 20 61 6e 64 20 74 68 65 20 63 6f 6e ller and the con
17185 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 tents of the.**
17186 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 75 6e output buffer un
17187 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 defined..*/.SQLI
17188 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
17189 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 46 qlite3PagerReadF
1718a 69 6c 65 68 65 61 64 65 72 28 50 61 67 65 72 20 ileheader(Pager
1718b 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 4e 2c 20 *pPager, int N,
1718c 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
1718d 44 65 73 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 Dest){. int rc
1718e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 6d = SQLITE_OK;. m
1718f 65 6d 73 65 74 28 70 44 65 73 74 2c 20 30 2c 20 emset(pDest, 0,
17190 4e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 N);. assert( is
17191 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
17192 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 || pPager->temp
17193 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 File );.. /* Th
17194 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e is routine is on
17195 6c 79 20 63 61 6c 6c 65 64 20 62 79 20 62 74 72 ly called by btr
17196 65 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 ee immediately a
17197 66 74 65 72 20 63 72 65 61 74 69 6e 67 0a 20 20 fter creating.
17198 2a 2a 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a ** the Pager obj
17199 65 63 74 2e 20 20 54 68 65 72 65 20 68 61 73 20 ect. There has
1719a 6e 6f 74 20 62 65 65 6e 20 61 6e 20 6f 70 70 6f not been an oppo
1719b 72 74 75 6e 69 74 79 20 74 6f 20 74 72 61 6e 73 rtunity to trans
1719c 69 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 57 41 ition. ** to WA
1719d 4c 20 6d 6f 64 65 20 79 65 74 2e 0a 20 20 2a 2f L mode yet.. */
1719e 0a 20 20 61 73 73 65 72 74 28 20 21 70 61 67 65 . assert( !page
1719f 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 rUseWal(pPager)
171a0 29 3b 0a 0a 20 20 69 66 28 20 69 73 4f 70 65 6e );.. if( isOpen
171a1 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a (pPager->fd) ){.
171a2 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 44 42 IOTRACE(("DB
171a3 48 44 52 20 25 70 20 30 20 25 64 5c 6e 22 2c 20 HDR %p 0 %d\n",
171a4 70 50 61 67 65 72 2c 20 4e 29 29 0a 20 20 20 20 pPager, N)).
171a5 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 rc = sqlite3OsRe
171a6 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 ad(pPager->fd, p
171a7 44 65 73 74 2c 20 4e 2c 20 30 29 3b 0a 20 20 20 Dest, N, 0);.
171a8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
171a9 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
171aa 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
171ab 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
171ac 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
171ad 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
171ae 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 unction may only
171af 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 be called when
171b0 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 a read-transacti
171b1 6f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e 0a 2a 2a on is open on.**
171b2 20 74 68 65 20 70 61 67 65 72 2e 20 49 74 20 72 the pager. It r
171b3 65 74 75 72 6e 73 20 74 68 65 20 74 6f 74 61 6c eturns the total
171b4 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
171b5 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
171b6 2e 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 2c ..**.** However,
171b7 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 if the file is
171b8 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 3c 70 between 1 and <p
171b9 61 67 65 2d 73 69 7a 65 3e 20 62 79 74 65 73 20 age-size> bytes
171ba 69 6e 20 73 69 7a 65 2c 20 74 68 65 6e 20 0a 2a in size, then .*
171bb 2a 20 74 68 69 73 20 69 73 20 63 6f 6e 73 69 64 * this is consid
171bc 65 72 65 64 20 61 20 31 20 70 61 67 65 20 66 69 ered a 1 page fi
171bd 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
171be 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
171bf 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
171c0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
171c1 69 6e 74 20 2a 70 6e 50 61 67 65 29 7b 0a 20 20 int *pnPage){.
171c2 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
171c3 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 eState>=PAGER_RE
171c4 41 44 45 52 20 29 3b 0a 20 20 61 73 73 65 72 74 ADER );. assert
171c5 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 ( pPager->eState
171c6 21 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 46 !=PAGER_WRITER_F
171c7 49 4e 49 53 48 45 44 20 29 3b 0a 20 20 2a 70 6e INISHED );. *pn
171c8 50 61 67 65 20 3d 20 28 69 6e 74 29 70 50 61 67 Page = (int)pPag
171c9 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 7d 0a 0a 0a er->dbSize;.}...
171ca 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 /*.** Try to obt
171cb 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 ain a lock of ty
171cc 70 65 20 6c 6f 63 6b 74 79 70 65 20 6f 6e 20 74 pe locktype on t
171cd 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
171ce 2e 20 49 66 0a 2a 2a 20 61 20 73 69 6d 69 6c 61 . If.** a simila
171cf 72 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 r or greater loc
171d0 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c k is already hel
171d1 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e d, this function
171d2 20 69 73 20 61 20 6e 6f 2d 6f 70 0a 2a 2a 20 28 is a no-op.** (
171d3 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 returning SQLITE
171d4 5f 4f 4b 20 69 6d 6d 65 64 69 61 74 65 6c 79 29 _OK immediately)
171d5 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ..**.** Otherwis
171d6 65 2c 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 62 e, attempt to ob
171d7 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20 75 73 tain the lock us
171d8 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 ing sqlite3OsLoc
171d9 6b 28 29 2e 20 49 6e 76 6f 6b 65 20 0a 2a 2a 20 k(). Invoke .**
171da 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 the busy callbac
171db 6b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 k if the lock is
171dc 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f 74 20 61 currently not a
171dd 76 61 69 6c 61 62 6c 65 2e 20 52 65 70 65 61 74 vailable. Repeat
171de 20 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 62 .** until the b
171df 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 usy callback ret
171e0 75 72 6e 73 20 66 61 6c 73 65 20 6f 72 20 75 6e urns false or un
171e1 74 69 6c 20 74 68 65 20 61 74 74 65 6d 70 74 20 til the attempt
171e2 74 6f 20 0a 2a 2a 20 6f 62 74 61 69 6e 20 74 68 to .** obtain th
171e3 65 20 6c 6f 63 6b 20 73 75 63 63 65 65 64 73 2e e lock succeeds.
171e4 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 .**.** Return SQ
171e5 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
171e6 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 ss and an error
171e7 63 6f 64 65 20 69 66 20 77 65 20 63 61 6e 6e 6f code if we canno
171e8 74 20 6f 62 74 61 69 6e 0a 2a 2a 20 74 68 65 20 t obtain.** the
171e9 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 6c 6f 63 lock. If the loc
171ea 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 73 75 k is obtained su
171eb 63 63 65 73 73 66 75 6c 6c 79 2c 20 73 65 74 20 ccessfully, set
171ec 74 68 65 20 50 61 67 65 72 2e 73 74 61 74 65 20 the Pager.state
171ed 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 74 6f 20 .** variable to
171ee 6c 6f 63 6b 74 79 70 65 20 62 65 66 6f 72 65 20 locktype before
171ef 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 returning..*/.st
171f0 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 atic int pager_w
171f1 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65 ait_on_lock(Page
171f2 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6c r *pPager, int l
171f3 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 ocktype){. int
171f4 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
171f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
171f6 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
171f7 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 */.. /* Check t
171f8 68 61 74 20 74 68 69 73 20 69 73 20 65 69 74 68 hat this is eith
171f9 65 72 20 61 20 6e 6f 2d 6f 70 20 28 62 65 63 61 er a no-op (beca
171fa 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65 use the requeste
171fb 64 20 6c 6f 63 6b 20 69 73 20 0a 20 20 2a 2a 20 d lock is . **
171fc 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 72 already held, or
171fd 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 72 61 6e one of the tran
171fe 73 69 73 74 69 6f 6e 73 20 74 68 61 74 20 74 68 sistions that th
171ff 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 20 e busy-handler.
17200 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76 6f 6b ** may be invok
17201 65 64 20 64 75 72 69 6e 67 2c 20 61 63 63 6f 72 ed during, accor
17202 64 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 6d 6d ding to the comm
17203 65 6e 74 20 61 62 6f 76 65 0a 20 20 2a 2a 20 73 ent above. ** s
17204 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75 qlite3PagerSetBu
17205 73 79 68 61 6e 64 6c 65 72 28 29 2e 0a 20 20 2a syhandler().. *
17206 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 50 61 /. assert( (pPa
17207 67 65 72 2d 3e 65 4c 6f 63 6b 3e 3d 6c 6f 63 6b ger->eLock>=lock
17208 74 79 70 65 29 0a 20 20 20 20 20 20 20 7c 7c 20 type). ||
17209 28 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d (pPager->eLock==
1720a 4e 4f 5f 4c 4f 43 4b 20 26 26 20 6c 6f 63 6b 74 NO_LOCK && lockt
1720b 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
1720c 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 ). || (pPa
1720d 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 53 45 ger->eLock==RESE
1720e 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 6c 6f 63 RVED_LOCK && loc
1720f 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 ktype==EXCLUSIVE
17210 5f 4c 4f 43 4b 29 0a 20 20 29 3b 0a 0a 20 20 64 _LOCK). );.. d
17211 6f 20 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 o {. rc = pag
17212 65 72 4c 6f 63 6b 44 62 28 70 50 61 67 65 72 2c erLockDb(pPager,
17213 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 7d 77 locktype);. }w
17214 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 hile( rc==SQLITE
17215 5f 42 55 53 59 20 26 26 20 70 50 61 67 65 72 2d _BUSY && pPager-
17216 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 28 70 50 >xBusyHandler(pP
17217 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c ager->pBusyHandl
17218 65 72 41 72 67 29 20 29 3b 0a 20 20 72 65 74 75 erArg) );. retu
17219 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1721a 46 75 6e 63 74 69 6f 6e 20 61 73 73 65 72 74 54 Function assertT
1721b 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e runcateConstrain
1721c 74 28 70 50 61 67 65 72 29 20 63 68 65 63 6b 73 t(pPager) checks
1721d 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 that one of the
1721e 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 .** following i
1721f 73 20 74 72 75 65 20 66 6f 72 20 61 6c 6c 20 64 s true for all d
17220 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65 irty pages curre
17221 6e 74 6c 79 20 69 6e 20 74 68 65 20 70 61 67 65 ntly in the page
17222 2d 63 61 63 68 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 -cache:.**.**
17223 61 29 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 a) The page numb
17224 65 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 er is less than
17225 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 or equal to the
17226 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 size of the .**
17227 20 20 20 20 20 63 75 72 72 65 6e 74 20 64 61 74 current dat
17228 61 62 61 73 65 20 69 6d 61 67 65 2c 20 69 6e 20 abase image, in
17229 70 61 67 65 73 2c 20 4f 52 0a 2a 2a 0a 2a 2a 20 pages, OR.**.**
1722a 20 20 62 29 20 69 66 20 74 68 65 20 70 61 67 65 b) if the page
1722b 20 63 6f 6e 74 65 6e 74 20 77 65 72 65 20 77 72 content were wr
1722c 69 74 74 65 6e 20 61 74 20 74 68 69 73 20 74 69 itten at this ti
1722d 6d 65 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 me, it would not
1722e 0a 2a 2a 20 20 20 20 20 20 62 65 20 6e 65 63 65 .** be nece
1722f 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20 74 ssary to write t
17230 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 he current conte
17231 6e 74 20 6f 75 74 20 74 6f 20 74 68 65 20 73 75 nt out to the su
17232 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 b-journal.**
17233 20 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64 (as determined
17234 20 62 79 20 66 75 6e 63 74 69 6f 6e 20 73 75 62 by function sub
17235 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 29 29 jRequiresPage())
17236 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 ..**.** If the c
17237 6f 6e 64 69 74 69 6f 6e 20 61 73 73 65 72 74 65 ondition asserte
17238 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 d by this functi
17239 6f 6e 20 77 65 72 65 20 6e 6f 74 20 74 72 75 65 on were not true
1723a 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 69 72 , and the.** dir
1723b 74 79 20 70 61 67 65 20 77 65 72 65 20 74 6f 20 ty page were to
1723c 62 65 20 64 69 73 63 61 72 64 65 64 20 66 72 6f be discarded fro
1723d 6d 20 74 68 65 20 63 61 63 68 65 20 76 69 61 20 m the cache via
1723e 74 68 65 20 70 61 67 65 72 53 74 72 65 73 73 28 the pagerStress(
1723f 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 70 61 ).** routine, pa
17240 67 65 72 53 74 72 65 73 73 28 29 20 77 6f 75 6c gerStress() woul
17241 64 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65 20 d not write the
17242 63 75 72 72 65 6e 74 20 70 61 67 65 20 63 6f 6e current page con
17243 74 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 tent to.** the d
17244 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 atabase file. If
17245 20 61 20 73 61 76 65 70 6f 69 6e 74 20 74 72 61 a savepoint tra
17246 6e 73 61 63 74 69 6f 6e 20 77 65 72 65 20 72 6f nsaction were ro
17247 6c 6c 65 64 20 62 61 63 6b 20 61 66 74 65 72 0a lled back after.
17248 2a 2a 20 74 68 69 73 20 68 61 70 70 65 6e 65 64 ** this happened
17249 2c 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 65 , the correct be
1724a 68 61 76 69 6f 75 72 20 77 6f 75 6c 64 20 62 65 haviour would be
1724b 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 to restore the
1724c 63 75 72 72 65 6e 74 0a 2a 2a 20 63 6f 6e 74 65 current.** conte
1724d 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 nt of the page.
1724e 48 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 74 However, since t
1724f 68 69 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e his content is n
17250 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 20 65 69 ot present in ei
17251 74 68 65 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 ther.** the data
17252 62 61 73 65 20 66 69 6c 65 20 6f 72 20 74 68 65 base file or the
17253 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 portion of the
17254 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
17255 20 61 6e 64 20 0a 2a 2a 20 73 75 62 2d 6a 6f 75 and .** sub-jou
17256 72 6e 61 6c 20 72 6f 6c 6c 65 64 20 62 61 63 6b rnal rolled back
17257 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 6f 75 the content cou
17258 6c 64 20 6e 6f 74 20 62 65 20 72 65 73 74 6f 72 ld not be restor
17259 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61 ed and the.** da
1725a 74 61 62 61 73 65 20 69 6d 61 67 65 20 77 6f 75 tabase image wou
1725b 6c 64 20 62 65 63 6f 6d 65 20 63 6f 72 72 75 70 ld become corrup
1725c 74 2e 20 49 74 20 69 73 20 74 68 65 72 65 66 6f t. It is therefo
1725d 72 65 20 66 6f 72 74 75 6e 61 74 65 20 74 68 61 re fortunate tha
1725e 74 20 0a 2a 2a 20 74 68 69 73 20 63 69 72 63 75 t .** this circu
1725f 6d 73 74 61 6e 63 65 20 63 61 6e 6e 6f 74 20 61 mstance cannot a
17260 72 69 73 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 rise..*/.#if def
17261 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
17262 47 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 G).static void a
17263 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e ssertTruncateCon
17264 73 74 72 61 69 6e 74 43 62 28 50 67 48 64 72 20 straintCb(PgHdr
17265 2a 70 50 67 29 7b 0a 20 20 61 73 73 65 72 74 28 *pPg){. assert(
17266 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 pPg->flags&PGHD
17267 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 61 73 73 R_DIRTY );. ass
17268 65 72 74 28 20 21 73 75 62 6a 52 65 71 75 69 72 ert( !subjRequir
17269 65 73 50 61 67 65 28 70 50 67 29 20 7c 7c 20 70 esPage(pPg) || p
1726a 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 67 2d 3e 70 Pg->pgno<=pPg->p
1726b 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 3b Pager->dbSize );
1726c 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 .}.static void a
1726d 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e ssertTruncateCon
1726e 73 74 72 61 69 6e 74 28 50 61 67 65 72 20 2a 70 straint(Pager *p
1726f 50 61 67 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 Pager){. sqlite
17270 33 50 63 61 63 68 65 49 74 65 72 61 74 65 44 69 3PcacheIterateDi
17271 72 74 79 28 70 50 61 67 65 72 2d 3e 70 50 43 61 rty(pPager->pPCa
17272 63 68 65 2c 20 61 73 73 65 72 74 54 72 75 6e 63 che, assertTrunc
17273 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 29 ateConstraintCb)
17274 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 ;.}.#else.# defi
17275 6e 65 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 ne assertTruncat
17276 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67 eConstraint(pPag
17277 65 72 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a er).#endif../*.*
17278 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 69 * Truncate the i
17279 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
1727a 65 20 66 69 6c 65 20 69 6d 61 67 65 20 74 6f 20 e file image to
1727b 6e 50 61 67 65 20 70 61 67 65 73 2e 20 54 68 69 nPage pages. Thi
1727c 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 64 s .** function d
1727d 6f 65 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 oes not actually
1727e 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 modify the data
1727f 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 base file on dis
17280 6b 2e 20 49 74 20 0a 2a 2a 20 6a 75 73 74 20 73 k. It .** just s
17281 65 74 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c ets the internal
17282 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 state of the pa
17283 67 65 72 20 6f 62 6a 65 63 74 20 73 6f 20 74 68 ger object so th
17284 61 74 20 74 68 65 20 0a 2a 2a 20 74 72 75 6e 63 at the .** trunc
17285 61 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f ation will be do
17286 6e 65 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 ne when the curr
17287 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
17288 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2f is committed..*/
17289 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1728a 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
1728b 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 50 rTruncateImage(P
1728c 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 ager *pPager, Pg
1728d 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 61 73 73 no nPage){. ass
1728e 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 ert( pPager->dbS
1728f 69 7a 65 3e 3d 6e 50 61 67 65 20 29 3b 0a 20 20 ize>=nPage );.
17290 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
17291 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 57 52 eState>=PAGER_WR
17292 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 20 29 3b ITER_CACHEMOD );
17293 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a . pPager->dbSiz
17294 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 61 73 73 e = nPage;. ass
17295 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 ertTruncateConst
17296 72 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a 7d raint(pPager);.}
17297 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 .../*.** This fu
17298 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
17299 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69 before attempti
1729a 6e 67 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c ng a hot-journal
1729b 20 72 6f 6c 6c 62 61 63 6b 2e 20 49 74 0a 2a 2a rollback. It.**
1729c 20 73 79 6e 63 73 20 74 68 65 20 6a 6f 75 72 6e syncs the journ
1729d 61 6c 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2c al file to disk,
1729e 20 74 68 65 6e 20 73 65 74 73 20 70 50 61 67 65 then sets pPage
1729f 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 74 6f r->journalHdr to
172a0 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 the.** size of
172a1 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
172a2 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 67 so that the pag
172a3 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 72 6f er_playback() ro
172a4 75 74 69 6e 65 20 6b 6e 6f 77 73 0a 2a 2a 20 74 utine knows.** t
172a5 68 61 74 20 74 68 65 20 65 6e 74 69 72 65 20 6a hat the entire j
172a6 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 ournal file has
172a7 62 65 65 6e 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a been synced..**.
172a8 2a 2a 20 53 79 6e 63 69 6e 67 20 61 20 68 6f 74 ** Syncing a hot
172a9 2d 6a 6f 75 72 6e 61 6c 20 74 6f 20 64 69 73 6b -journal to disk
172aa 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69 before attempti
172ab 6e 67 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 ng to roll it ba
172ac 63 6b 20 65 6e 73 75 72 65 73 20 0a 2a 2a 20 74 ck ensures .** t
172ad 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 2d 66 hat if a power-f
172ae 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 64 75 ailure occurs du
172af 72 69 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61 63 ring the rollbac
172b0 6b 2c 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 k, the process t
172b1 68 61 74 0a 2a 2a 20 61 74 74 65 6d 70 74 73 20 hat.** attempts
172b2 72 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c 6f 77 69 rollback followi
172b3 6e 67 20 73 79 73 74 65 6d 20 72 65 63 6f 76 65 ng system recove
172b4 72 79 20 73 65 65 73 20 74 68 65 20 73 61 6d 65 ry sees the same
172b5 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 63 6f 6e 74 journal.** cont
172b6 65 6e 74 20 61 73 20 74 68 69 73 20 70 72 6f 63 ent as this proc
172b7 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 76 ess..**.** If ev
172b8 65 72 79 74 68 69 6e 67 20 67 6f 65 73 20 61 73 erything goes as
172b9 20 70 6c 61 6e 6e 65 64 2c 20 53 51 4c 49 54 45 planned, SQLITE
172ba 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
172bb 20 4f 74 68 65 72 77 69 73 65 2c 20 0a 2a 2a 20 Otherwise, .**
172bc 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 an SQLite error
172bd 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 code..*/.static
172be 69 6e 74 20 70 61 67 65 72 53 79 6e 63 48 6f 74 int pagerSyncHot
172bf 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 Journal(Pager *p
172c0 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 Pager){. int rc
172c1 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
172c2 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 if( !pPager->noS
172c3 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ync ){. rc =
172c4 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 sqlite3OsSync(pP
172c5 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54 ager->jfd, SQLIT
172c6 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a E_SYNC_NORMAL);.
172c7 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
172c8 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
172c9 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
172ca 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 eSize(pPager->jf
172cb 64 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 d, &pPager->jour
172cc 6e 61 6c 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72 nalHdr);. }. r
172cd 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
172ce 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 ** Shutdown the
172cf 70 61 67 65 20 63 61 63 68 65 2e 20 20 46 72 65 page cache. Fre
172d0 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e 64 e all memory and
172d1 20 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 73 close all files
172d2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 61 ..**.** If a tra
172d3 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 69 6e 20 nsaction was in
172d4 70 72 6f 67 72 65 73 73 20 77 68 65 6e 20 74 68 progress when th
172d5 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
172d6 6c 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74 72 lled, that.** tr
172d7 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c ansaction is rol
172d8 6c 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20 6f led back. All o
172d9 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 utstanding pages
172da 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 are invalidated
172db 0a 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 6d 65 .** and their me
172dc 6d 6f 72 79 20 69 73 20 66 72 65 65 64 2e 20 20 mory is freed.
172dd 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 Any attempt to u
172de 73 65 20 61 20 70 61 67 65 20 61 73 73 6f 63 69 se a page associ
172df 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 69 ated.** with thi
172e0 73 20 70 61 67 65 20 63 61 63 68 65 20 61 66 74 s page cache aft
172e1 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e er this function
172e2 20 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69 returns will li
172e3 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20 69 kely.** result i
172e4 6e 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2a n a coredump..**
172e5 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
172e6 6e 20 61 6c 77 61 79 73 20 73 75 63 63 65 65 64 n always succeed
172e7 73 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 s. If a transact
172e8 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e ion is active an
172e9 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d attempt.** is m
172ea 61 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 ade to roll it b
172eb 61 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 ack. If an error
172ec 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 occurs during t
172ed 68 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a 20 he rollback .**
172ee 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d 61 a hot journal ma
172ef 79 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65 y be left in the
172f0 20 66 69 6c 65 73 79 73 74 65 6d 20 62 75 74 20 filesystem but
172f1 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 no error is retu
172f2 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 63 rned.** to the c
172f3 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 aller..*/.SQLITE
172f4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
172f5 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 ite3PagerClose(P
172f6 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
172f7 20 75 38 20 2a 70 54 6d 70 20 3d 20 28 75 38 20 u8 *pTmp = (u8
172f8 2a 29 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 *)pPager->pTmpSp
172f9 61 63 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ace;.. assert(
172fa 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 assert_pager_sta
172fb 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 te(pPager) );.
172fc 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 disable_simulate
172fd 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 d_io_errors();.
172fe 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e sqlite3BeginBen
172ff 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 2f ignMalloc();. /
17300 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 * pPager->errCod
17301 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 e = 0; */. pPag
17302 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 er->exclusiveMod
17303 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 e = 0;.#ifndef S
17304 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20 QLITE_OMIT_WAL.
17305 20 73 71 6c 69 74 65 33 57 61 6c 43 6c 6f 73 65 sqlite3WalClose
17306 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 70 (pPager->pWal, p
17307 50 61 67 65 72 2d 3e 63 6b 70 74 53 79 6e 63 46 Pager->ckptSyncF
17308 6c 61 67 73 2c 20 70 50 61 67 65 72 2d 3e 70 61 lags, pPager->pa
17309 67 65 53 69 7a 65 2c 20 70 54 6d 70 29 3b 0a 20 geSize, pTmp);.
1730a 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 20 3d 20 pPager->pWal =
1730b 30 3b 0a 23 65 6e 64 69 66 0a 20 20 70 61 67 65 0;.#endif. page
1730c 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b r_reset(pPager);
1730d 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a . if( MEMDB ){.
1730e 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b pager_unlock
1730f 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c 73 (pPager);. }els
17310 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 69 74 20 e{. /* If it
17311 69 73 20 6f 70 65 6e 2c 20 73 79 6e 63 20 74 68 is open, sync th
17312 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 e journal file b
17313 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 55 6e efore calling Un
17314 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 2e lockAndRollback.
17315 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 . ** If this
17316 69 73 20 6e 6f 74 20 64 6f 6e 65 2c 20 74 68 65 is not done, the
17317 6e 20 61 6e 20 75 6e 73 79 6e 63 65 64 20 70 6f n an unsynced po
17318 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 65 rtion of the ope
17319 6e 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 20 20 2a n journal . *
1731a 2a 20 66 69 6c 65 20 6d 61 79 20 62 65 20 70 6c * file may be pl
1731b 61 79 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 74 ayed back into t
1731c 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 he database. If
1731d 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 a power failure
1731e 6f 63 63 75 72 73 20 0a 20 20 20 20 2a 2a 20 77 occurs . ** w
1731f 68 69 6c 65 20 74 68 69 73 20 69 73 20 68 61 70 hile this is hap
17320 70 65 6e 69 6e 67 2c 20 74 68 65 20 64 61 74 61 pening, the data
17321 62 61 73 65 20 63 6f 75 6c 64 20 62 65 63 6f 6d base could becom
17322 65 20 63 6f 72 72 75 70 74 2e 0a 20 20 20 20 2a e corrupt.. *
17323 2a 0a 20 20 20 20 2a 2a 20 49 66 20 61 6e 20 65 *. ** If an e
17324 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
17325 65 20 74 72 79 69 6e 67 20 74 6f 20 73 79 6e 63 e trying to sync
17326 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 73 68 the journal, sh
17327 69 66 74 20 74 68 65 20 70 61 67 65 72 0a 20 20 ift the pager.
17328 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 45 52 ** into the ER
17329 52 4f 52 20 73 74 61 74 65 2e 20 54 68 69 73 20 ROR state. This
1732a 63 61 75 73 65 73 20 55 6e 6c 6f 63 6b 41 6e 64 causes UnlockAnd
1732b 52 6f 6c 6c 62 61 63 6b 20 74 6f 20 75 6e 6c 6f Rollback to unlo
1732c 63 6b 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 ck the. ** da
1732d 74 61 62 61 73 65 20 61 6e 64 20 63 6c 6f 73 65 tabase and close
1732e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1732f 65 20 77 69 74 68 6f 75 74 20 61 74 74 65 6d 70 e without attemp
17330 74 69 6e 67 20 74 6f 20 72 6f 6c 6c 20 69 74 0a ting to roll it.
17331 20 20 20 20 2a 2a 20 62 61 63 6b 20 6f 72 20 66 ** back or f
17332 69 6e 61 6c 69 7a 65 20 69 74 2e 20 54 68 65 20 inalize it. The
17333 6e 65 78 74 20 64 61 74 61 62 61 73 65 20 75 73 next database us
17334 65 72 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 20 er will have to
17335 64 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 20 do hot-journal.
17336 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 62 ** rollback b
17337 65 66 6f 72 65 20 61 63 63 65 73 73 69 6e 67 20 efore accessing
17338 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
17339 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 e.. */. if
1733a 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
1733b 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 70 >jfd) ){. p
1733c 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 ager_error(pPage
1733d 72 2c 20 70 61 67 65 72 53 79 6e 63 48 6f 74 4a r, pagerSyncHotJ
1733e 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 29 3b ournal(pPager));
1733f 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67 65 72 . }. pager
17340 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 UnlockAndRollbac
17341 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 k(pPager);. }.
17342 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
17343 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 65 6e 61 nMalloc();. ena
17344 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f ble_simulated_io
17345 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 50 41 47 _errors();. PAG
17346 45 52 54 52 41 43 45 28 28 22 43 4c 4f 53 45 20 ERTRACE(("CLOSE
17347 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 %d\n", PAGERID(p
17348 50 61 67 65 72 29 29 29 3b 0a 20 20 49 4f 54 52 Pager)));. IOTR
17349 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70 5c 6e ACE(("CLOSE %p\n
1734a 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 73 71 ", pPager)). sq
1734b 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
1734c 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 73 71 6c ger->jfd);. sql
1734d 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 ite3OsClose(pPag
1734e 65 72 2d 3e 66 64 29 3b 0a 20 20 73 71 6c 69 74 er->fd);. sqlit
1734f 65 33 50 61 67 65 46 72 65 65 28 70 54 6d 70 29 e3PageFree(pTmp)
17350 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 ;. sqlite3Pcach
17351 65 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 70 eClose(pPager->p
17352 50 43 61 63 68 65 29 3b 0a 0a 23 69 66 64 65 66 PCache);..#ifdef
17353 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 SQLITE_HAS_CODE
17354 43 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e C. if( pPager->
17355 78 43 6f 64 65 63 46 72 65 65 20 29 20 70 50 61 xCodecFree ) pPa
17356 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 28 ger->xCodecFree(
17357 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 29 3b pPager->pCodec);
17358 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 .#endif.. asser
17359 74 28 20 21 70 50 61 67 65 72 2d 3e 61 53 61 76 t( !pPager->aSav
1735a 65 70 6f 69 6e 74 20 26 26 20 21 70 50 61 67 65 epoint && !pPage
1735b 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 3b r->pInJournal );
1735c 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 . assert( !isOp
1735d 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
1735e 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 && !isOpen(pPage
1735f 72 2d 3e 73 6a 66 64 29 20 29 3b 0a 0a 20 20 73 r->sjfd) );.. s
17360 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 qlite3_free(pPag
17361 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 er);. return SQ
17362 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 LITE_OK;.}..#if
17363 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
17364 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
17365 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52 TE_TEST)./*.** R
17366 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 6e eturn the page n
17367 75 6d 62 65 72 20 66 6f 72 20 70 61 67 65 20 70 umber for page p
17368 50 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 Pg..*/.SQLITE_PR
17369 49 56 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 74 IVATE Pgno sqlit
1736a 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 e3PagerPagenumbe
1736b 72 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a r(DbPage *pPg){.
1736c 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 67 return pPg->pg
1736d 6e 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a no;.}.#endif../*
1736e 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 .** Increment th
1736f 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e e reference coun
17370 74 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a t for page pPg..
17371 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
17372 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
17373 67 65 72 52 65 66 28 44 62 50 61 67 65 20 2a 70 gerRef(DbPage *p
17374 50 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 Pg){. sqlite3Pc
17375 61 63 68 65 52 65 66 28 70 50 67 29 3b 0a 7d 0a acheRef(pPg);.}.
17376 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 ./*.** Sync the
17377 6a 6f 75 72 6e 61 6c 2e 20 49 6e 20 6f 74 68 65 journal. In othe
17378 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 73 75 r words, make su
17379 72 65 20 61 6c 6c 20 74 68 65 20 70 61 67 65 73 re all the pages
1737a 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 62 65 that have.** be
1737b 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 en written to th
1737c 65 20 6a 6f 75 72 6e 61 6c 20 68 61 76 65 20 61 e journal have a
1737d 63 74 75 61 6c 6c 79 20 72 65 61 63 68 65 64 20 ctually reached
1737e 74 68 65 20 73 75 72 66 61 63 65 20 6f 66 20 74 the surface of t
1737f 68 65 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 63 he.** disk and c
17380 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 69 an be restored i
17381 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 n the event of a
17382 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
17383 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 lback..**.** If
17384 74 68 65 20 50 61 67 65 72 2e 6e 6f 53 79 6e 63 the Pager.noSync
17385 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 flag is set, th
17386 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
17387 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20 is a no-op..**
17388 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 Otherwise, the a
17389 63 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 ctions required
1738a 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a 6f depend on the jo
1738b 75 72 6e 61 6c 2d 6d 6f 64 65 20 61 6e 64 20 74 urnal-mode and t
1738c 68 65 20 0a 2a 2a 20 64 65 76 69 63 65 20 63 68 he .** device ch
1738d 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 aracteristics of
1738e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d the file-system
1738f 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a , as follows:.**
17390 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 6a .** * If the j
17391 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 61 ournal file is a
17392 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 n in-memory jour
17393 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f 20 61 63 74 nal file, no act
17394 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20 20 20 20 ion need.**
17395 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 be taken..**.**
17396 20 20 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 * Otherwise, i
17397 66 20 74 68 65 20 64 65 76 69 63 65 20 64 6f 65 f the device doe
17398 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74 68 s not support th
17399 65 20 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72 e SAFE_APPEND pr
1739a 6f 70 65 72 74 79 2c 0a 2a 2a 20 20 20 20 20 74 operty,.** t
1739b 68 65 6e 20 74 68 65 20 6e 52 65 63 20 66 69 65 hen the nRec fie
1739c 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 ld of the most r
1739d 65 63 65 6e 74 6c 79 20 77 72 69 74 74 65 6e 20 ecently written
1739e 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a journal header.*
1739f 2a 20 20 20 20 20 69 73 20 75 70 64 61 74 65 64 * is updated
173a0 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 to contain the
173a1 6e 75 6d 62 65 72 20 6f 66 20 6a 6f 75 72 6e 61 number of journa
173a2 6c 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 68 l records that h
173a3 61 76 65 0a 2a 2a 20 20 20 20 20 62 65 65 6e 20 ave.** been
173a4 77 72 69 74 74 65 6e 20 66 6f 6c 6c 6f 77 69 6e written followin
173a5 67 20 69 74 2e 20 49 66 20 74 68 65 20 70 61 67 g it. If the pag
173a6 65 72 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20 er is operating
173a7 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a 2a 2a 20 in full-sync.**
173a8 20 20 20 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 mode, then t
173a9 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
173aa 69 73 20 73 79 6e 63 65 64 20 62 65 66 6f 72 65 is synced before
173ab 20 74 68 69 73 20 66 69 65 6c 64 20 69 73 20 75 this field is u
173ac 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 pdated..**.**
173ad 2a 20 49 66 20 74 68 65 20 64 65 76 69 63 65 20 * If the device
173ae 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 does not support
173af 20 74 68 65 20 53 45 51 55 45 4e 54 49 41 4c 20 the SEQUENTIAL
173b0 70 72 6f 70 65 72 74 79 2c 20 74 68 65 6e 20 0a property, then .
173b1 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c 20 66 ** journal f
173b2 69 6c 65 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a ile is synced..*
173b3 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 73 65 75 *.** Or, in pseu
173b4 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 do-code:.**.**
173b5 20 69 66 28 20 4e 4f 54 20 3c 69 6e 2d 6d 65 6d if( NOT <in-mem
173b6 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e 20 29 7b 0a ory journal> ){.
173b7 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 ** if( NOT S
173b8 41 46 45 5f 41 50 50 45 4e 44 20 29 7b 0a 2a 2a AFE_APPEND ){.**
173b9 20 20 20 20 20 20 20 69 66 28 20 3c 66 75 6c 6c if( <full
173ba 2d 73 79 6e 63 20 6d 6f 64 65 3e 20 29 20 78 53 -sync mode> ) xS
173bb 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c ync(<journal fil
173bc 65 3e 29 3b 0a 2a 2a 20 20 20 20 20 20 20 3c 75 e>);.** <u
173bd 70 64 61 74 65 20 6e 52 65 63 20 66 69 65 6c 64 pdate nRec field
173be 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a 2a 20 20 >.** } .**
173bf 20 20 20 69 66 28 20 4e 4f 54 20 53 45 51 55 45 if( NOT SEQUE
173c0 4e 54 49 41 4c 20 29 20 78 53 79 6e 63 28 3c 6a NTIAL ) xSync(<j
173c1 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a ournal file>);.*
173c2 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 73 * }.**.** If s
173c3 75 63 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20 uccessful, this
173c4 72 6f 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74 routine clears t
173c5 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 he PGHDR_NEED_SY
173c6 4e 43 20 66 6c 61 67 20 6f 66 20 65 76 65 72 79 NC flag of every
173c7 20 0a 2a 2a 20 70 61 67 65 20 63 75 72 72 65 6e .** page curren
173c8 74 6c 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f tly held in memo
173c9 72 79 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e ry before return
173ca 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 ing SQLITE_OK. I
173cb 66 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 f an IO.** error
173cc 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c is encountered,
173cd 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 then the IO err
173ce 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
173cf 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ned to the calle
173d0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
173d1 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 syncJournal(Pag
173d2 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
173d3 6e 65 77 48 64 72 29 7b 0a 20 20 69 6e 74 20 72 newHdr){. int r
173d4 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
173d5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
173d6 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 turn code */..
173d7 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
173d8 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 eState==PAGER_WR
173d9 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 0a 20 20 ITER_CACHEMOD.
173da 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e || pPager->
173db 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 eState==PAGER_WR
173dc 49 54 45 52 5f 44 42 4d 4f 44 0a 20 20 29 3b 0a ITER_DBMOD. );.
173dd 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 assert( assert
173de 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 _pager_state(pPa
173df 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 ger) );. assert
173e0 28 20 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 ( !pagerUseWal(p
173e1 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 72 63 20 Pager) );.. rc
173e2 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 45 78 = sqlite3PagerEx
173e3 63 6c 75 73 69 76 65 4c 6f 63 6b 28 70 50 61 67 clusiveLock(pPag
173e4 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 er);. if( rc!=S
173e5 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
173e6 6e 20 72 63 3b 0a 0a 20 20 69 66 28 20 21 70 50 n rc;.. if( !pP
173e7 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a ager->noSync ){.
173e8 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 assert( !pPa
173e9 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b ger->tempFile );
173ea 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 . if( isOpen(
173eb 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 pPager->jfd) &&
173ec 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
173ed 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode!=PAGER_JOURN
173ee 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b ALMODE_MEMORY ){
173ef 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 . const int
173f0 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 iDc = sqlite3Os
173f1 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
173f2 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 stics(pPager->fd
173f3 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
173f4 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
173f5 6a 66 64 29 20 29 3b 0a 0a 20 20 20 20 20 20 69 jfd) );.. i
173f6 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 f( 0==(iDc&SQLIT
173f7 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 E_IOCAP_SAFE_APP
173f8 45 4e 44 29 20 29 7b 0a 20 20 20 20 20 20 20 20 END) ){.
173f9 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 64 65 /* This block de
173fa 61 6c 73 20 77 69 74 68 20 61 6e 20 6f 62 73 63 als with an obsc
173fb 75 72 65 20 70 72 6f 62 6c 65 6d 2e 20 49 66 20 ure problem. If
173fc 74 68 65 20 6c 61 73 74 20 63 6f 6e 6e 65 63 74 the last connect
173fd 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 ion. ** t
173fe 68 61 74 20 77 72 6f 74 65 20 74 6f 20 74 68 69 hat wrote to thi
173ff 73 20 64 61 74 61 62 61 73 65 20 77 61 73 20 6f s database was o
17400 70 65 72 61 74 69 6e 67 20 69 6e 20 70 65 72 73 perating in pers
17401 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20 istent-journal.
17402 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64 65 2c 20 ** mode,
17403 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c then the journal
17404 20 66 69 6c 65 20 6d 61 79 20 61 74 20 74 68 69 file may at thi
17405 73 20 70 6f 69 6e 74 20 61 63 74 75 61 6c 6c 79 s point actually
17406 20 62 65 20 6c 61 72 67 65 72 0a 20 20 20 20 20 be larger.
17407 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 ** than Pager
17408 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 62 79 74 65 .journalOff byte
17409 73 2e 20 49 66 20 74 68 65 20 6e 65 78 74 20 74 s. If the next t
1740a 68 69 6e 67 20 69 6e 20 74 68 65 20 6a 6f 75 72 hing in the jour
1740b 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 nal. ** f
1740c 69 6c 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62 ile happens to b
1740d 65 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 e a journal-head
1740e 65 72 20 28 77 72 69 74 74 65 6e 20 61 73 20 70 er (written as p
1740f 61 72 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 art of the.
17410 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 ** previous c
17411 6f 6e 6e 65 63 74 69 6f 6e 27 73 20 74 72 61 6e onnection's tran
17412 73 61 63 74 69 6f 6e 29 2c 20 61 6e 64 20 61 20 saction), and a
17413 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 2d 66 crash or power-f
17414 61 69 6c 75 72 65 20 0a 20 20 20 20 20 20 20 20 ailure .
17415 2a 2a 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 ** occurs after
17416 6e 52 65 63 20 69 73 20 75 70 64 61 74 65 64 20 nRec is updated
17417 62 75 74 20 62 65 66 6f 72 65 20 74 68 69 73 20 but before this
17418 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 connection write
17419 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e s . ** an
1741a 79 74 68 69 6e 67 20 65 6c 73 65 20 74 6f 20 74 ything else to t
1741b 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1741c 28 6f 72 20 63 6f 6d 6d 69 74 73 2f 72 6f 6c 6c (or commits/roll
1741d 73 20 62 61 63 6b 20 69 74 73 20 0a 20 20 20 20 s back its .
1741e 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 ** transacti
1741f 6f 6e 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 on), then SQLite
17420 20 6d 61 79 20 62 65 63 6f 6d 65 20 63 6f 6e 66 may become conf
17421 75 73 65 64 20 77 68 65 6e 20 64 6f 69 6e 67 20 used when doing
17422 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 the . **
17423 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c hot-journal roll
17424 62 61 63 6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 back following r
17425 65 63 6f 76 65 72 79 2e 20 49 74 20 6d 61 79 20 ecovery. It may
17426 72 6f 6c 6c 20 62 61 63 6b 20 61 6c 6c 0a 20 20 roll back all.
17427 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 69 73 ** of this
17428 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 64 61 74 connections dat
17429 61 2c 20 74 68 65 6e 20 70 72 6f 63 65 65 64 20 a, then proceed
1742a 74 6f 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 to rolling back
1742b 74 68 65 20 6f 6c 64 2c 0a 20 20 20 20 20 20 20 the old,.
1742c 20 2a 2a 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 ** out-of-date
1742d 64 61 74 61 20 74 68 61 74 20 66 6f 6c 6c 6f 77 data that follow
1742e 73 20 69 74 2e 20 44 61 74 61 62 61 73 65 20 63 s it. Database c
1742f 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20 orruption..
17430 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a **. **
17431 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 To work around
17432 74 68 69 73 2c 20 69 66 20 74 68 65 20 6a 6f 75 this, if the jou
17433 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 61 rnal file does a
17434 70 70 65 61 72 20 74 6f 20 63 6f 6e 74 61 69 6e ppear to contain
17435 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 76 61 . ** a va
17436 6c 69 64 20 68 65 61 64 65 72 20 66 6f 6c 6c 6f lid header follo
17437 77 69 6e 67 20 50 61 67 65 72 2e 6a 6f 75 72 6e wing Pager.journ
17438 61 6c 4f 66 66 2c 20 74 68 65 6e 20 77 72 69 74 alOff, then writ
17439 65 20 61 20 30 78 30 30 0a 20 20 20 20 20 20 20 e a 0x00.
1743a 20 2a 2a 20 62 79 74 65 20 74 6f 20 74 68 65 20 ** byte to the
1743b 73 74 61 72 74 20 6f 66 20 69 74 20 74 6f 20 70 start of it to p
1743c 72 65 76 65 6e 74 20 69 74 20 66 72 6f 6d 20 62 revent it from b
1743d 65 69 6e 67 20 72 65 63 6f 67 6e 69 7a 65 64 2e eing recognized.
1743e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
1743f 20 20 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 20 ** Variable
17440 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 20 69 iNextHdrOffset i
17441 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f 66 66 s set to the off
17442 73 65 74 20 61 74 20 77 68 69 63 68 20 74 68 69 set at which thi
17443 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f s. ** pro
17444 62 6c 65 6d 61 74 69 63 20 68 65 61 64 65 72 20 blematic header
17445 77 69 6c 6c 20 6f 63 63 75 72 2c 20 69 66 20 69 will occur, if i
17446 74 20 65 78 69 73 74 73 2e 20 61 4d 61 67 69 63 t exists. aMagic
17447 20 69 73 20 75 73 65 64 20 0a 20 20 20 20 20 20 is used .
17448 20 20 2a 2a 20 61 73 20 61 20 74 65 6d 70 6f 72 ** as a tempor
17449 61 72 79 20 62 75 66 66 65 72 20 74 6f 20 69 6e ary buffer to in
1744a 73 70 65 63 74 20 74 68 65 20 66 69 72 73 74 20 spect the first
1744b 63 6f 75 70 6c 65 20 6f 66 20 62 79 74 65 73 20 couple of bytes
1744c 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 of. ** th
1744d 65 20 70 6f 74 65 6e 74 69 61 6c 20 6a 6f 75 72 e potential jour
1744e 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 20 20 nal header..
1744f 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 */. i
17450 36 34 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 64 iNextHdrOffse
17451 74 3b 0a 20 20 20 20 20 20 20 20 75 38 20 61 4d t;. u8 aM
17452 61 67 69 63 5b 38 5d 3b 0a 20 20 20 20 20 20 20 agic[8];.
17453 20 75 38 20 7a 48 65 61 64 65 72 5b 73 69 7a 65 u8 zHeader[size
17454 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of(aJournalMagic
17455 29 2b 34 5d 3b 0a 0a 20 20 20 20 20 20 20 20 6d )+4];.. m
17456 65 6d 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61 emcpy(zHeader, a
17457 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 JournalMagic, si
17458 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
17459 69 63 29 29 3b 0a 20 20 20 20 20 20 20 20 70 75 ic));. pu
1745a 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 t32bits(&zHeader
1745b 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c [sizeof(aJournal
1745c 4d 61 67 69 63 29 5d 2c 20 70 50 61 67 65 72 2d Magic)], pPager-
1745d 3e 6e 52 65 63 29 3b 0a 0a 20 20 20 20 20 20 20 >nRec);..
1745e 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 20 iNextHdrOffset
1745f 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 = journalHdrOffs
17460 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 et(pPager);.
17461 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
17462 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 6a OsRead(pPager->j
17463 66 64 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 69 fd, aMagic, 8, i
17464 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29 3b 0a NextHdrOffset);.
17465 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
17466 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d SQLITE_OK && 0==
17467 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 memcmp(aMagic, a
17468 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 29 JournalMagic, 8)
17469 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74 ){. st
1746a 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 7a 65 atic const u8 ze
1746b 72 6f 62 79 74 65 20 3d 20 30 3b 0a 20 20 20 20 robyte = 0;.
1746c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1746d 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
1746e 2d 3e 6a 66 64 2c 20 26 7a 65 72 6f 62 79 74 65 ->jfd, &zerobyte
1746f 2c 20 31 2c 20 69 4e 65 78 74 48 64 72 4f 66 66 , 1, iNextHdrOff
17470 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a set);. }.
17471 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
17472 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 SQLITE_OK && rc!
17473 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 =SQLITE_IOERR_SH
17474 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 ORT_READ ){.
17475 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
17476 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 . }..
17477 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
17478 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e 74 6f nRec value into
17479 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1747a 65 20 68 65 61 64 65 72 2e 20 49 66 20 69 6e 0a e header. If in.
1747b 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6c 6c 2d ** full-
1747c 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 synchronous mode
1747d 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e , sync the journ
1747e 61 6c 20 66 69 72 73 74 2e 20 54 68 69 73 20 65 al first. This e
1747f 6e 73 75 72 65 73 20 74 68 61 74 0a 20 20 20 20 nsures that.
17480 20 20 20 20 2a 2a 20 61 6c 6c 20 64 61 74 61 20 ** all data
17481 68 61 73 20 72 65 61 6c 6c 79 20 68 69 74 20 74 has really hit t
17482 68 65 20 64 69 73 6b 20 62 65 66 6f 72 65 20 6e he disk before n
17483 52 65 63 20 69 73 20 75 70 64 61 74 65 64 20 74 Rec is updated t
17484 6f 20 6d 61 72 6b 0a 20 20 20 20 20 20 20 20 2a o mark. *
17485 2a 20 69 74 20 61 73 20 61 20 63 61 6e 64 69 64 * it as a candid
17486 61 74 65 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b ate for rollback
17487 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 .. **.
17488 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 ** This is
17489 6e 6f 74 20 72 65 71 75 69 72 65 64 20 69 66 20 not required if
1748a 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d the persistent m
1748b 65 64 69 61 20 73 75 70 70 6f 72 74 73 20 74 68 edia supports th
1748c 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 41 46 e. ** SAF
1748d 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74 E_APPEND propert
1748e 79 2e 20 42 65 63 61 75 73 65 20 69 6e 20 74 68 y. Because in th
1748f 69 73 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f is case it is no
17490 74 20 70 6f 73 73 69 62 6c 65 20 0a 20 20 20 20 t possible .
17491 20 20 20 20 2a 2a 20 66 6f 72 20 67 61 72 62 61 ** for garba
17492 67 65 20 64 61 74 61 20 74 6f 20 62 65 20 61 70 ge data to be ap
17493 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 66 69 pended to the fi
17494 6c 65 2c 20 74 68 65 20 6e 52 65 63 20 66 69 65 le, the nRec fie
17495 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 ld. ** is
17496 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 populated with
17497 30 78 46 46 46 46 46 46 46 46 20 77 68 65 6e 20 0xFFFFFFFF when
17498 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 the journal head
17499 65 72 20 69 73 20 77 72 69 74 74 65 6e 0a 20 20 er is written.
1749a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6e 65 76 ** and nev
1749b 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 er needs to be u
1749c 70 64 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20 pdated..
1749d 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 */. if( p
1749e 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 Pager->fullSync
1749f 26 26 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 && 0==(iDc&SQLIT
174a0 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 E_IOCAP_SEQUENTI
174a1 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 AL) ){.
174a2 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 59 PAGERTRACE(("SY
174a3 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64 NC journal of %d
174a4 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 \n", PAGERID(pPa
174a5 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20 20 20 ger)));.
174a6 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53 59 4e IOTRACE(("JSYN
174a7 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 C %p\n", pPager)
174a8 29 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ). rc =
174a9 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
174aa 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 Pager->jfd, pPag
174ab 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 29 3b 0a er->syncFlags);.
174ac 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
174ad 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
174ae 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
174af 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 }. IOTRA
174b0 43 45 28 28 22 4a 48 44 52 20 25 70 20 25 6c 6c CE(("JHDR %p %ll
174b1 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 d\n", pPager, pP
174b2 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
174b3 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ));. rc =
174b4 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
174b5 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 . pPa
174b6 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 ger->jfd, zHeade
174b7 72 2c 20 73 69 7a 65 6f 66 28 7a 48 65 61 64 65 r, sizeof(zHeade
174b8 72 29 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 r), pPager->jour
174b9 6e 61 6c 48 64 72 0a 20 20 20 20 20 20 20 20 29 nalHdr. )
174ba 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
174bb 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
174bc 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
174bd 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 . if( 0==(i
174be 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f Dc&SQLITE_IOCAP_
174bf 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 SEQUENTIAL) ){.
174c0 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 PAGERTRAC
174c1 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c E(("SYNC journal
174c2 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 of %d\n", PAGER
174c3 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 ID(pPager)));.
174c4 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 IOTRACE(("
174c5 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 JSYNC %p\n", pPa
174c6 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63 ger)). rc
174c7 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 = sqlite3OsSync
174c8 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 (pPager->jfd, pP
174c9 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 7c ager->syncFlags|
174ca 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 61 . (pPa
174cb 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 3d 3d ger->syncFlags==
174cc 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c SQLITE_SYNC_FULL
174cd 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 ?SQLITE_SYNC_DAT
174ce 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20 20 AONLY:0).
174cf 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
174d0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
174d1 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
174d2 20 7d 0a 0a 20 20 20 20 20 20 70 50 61 67 65 72 }.. pPager
174d3 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70 ->journalHdr = p
174d4 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
174d5 66 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 77 f;. if( new
174d6 48 64 72 20 26 26 20 30 3d 3d 28 69 44 63 26 53 Hdr && 0==(iDc&S
174d7 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 QLITE_IOCAP_SAFE
174d8 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20 20 20 20 _APPEND) ){.
174d9 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 pPager->nRec
174da 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63 = 0;. rc
174db 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 = writeJournalH
174dc 64 72 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 dr(pPager);.
174dd 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
174de 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 TE_OK ) return r
174df 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
174e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 67 else{. pPag
174e1 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d er->journalHdr =
174e2 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
174e3 4f 66 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a Off;. }. }..
174e4 20 20 2f 2a 20 55 6e 6c 65 73 73 20 74 68 65 20 /* Unless the
174e5 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f 53 79 pager is in noSy
174e6 6e 63 20 6d 6f 64 65 2c 20 74 68 65 20 6a 6f 75 nc mode, the jou
174e7 72 6e 61 6c 20 66 69 6c 65 20 77 61 73 20 6a 75 rnal file was ju
174e8 73 74 20 0a 20 20 2a 2a 20 73 75 63 63 65 73 73 st . ** success
174e9 66 75 6c 6c 79 20 73 79 6e 63 65 64 2e 20 45 69 fully synced. Ei
174ea 74 68 65 72 20 77 61 79 2c 20 63 6c 65 61 72 20 ther way, clear
174eb 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 the PGHDR_NEED_S
174ec 59 4e 43 20 66 6c 61 67 20 6f 6e 20 0a 20 20 2a YNC flag on . *
174ed 2a 20 61 6c 6c 20 70 61 67 65 73 2e 0a 20 20 2a * all pages.. *
174ee 2f 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 /. sqlite3Pcach
174ef 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 eClearSyncFlags(
174f0 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
174f1 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 ;. pPager->eSta
174f2 74 65 20 3d 20 50 41 47 45 52 5f 57 52 49 54 45 te = PAGER_WRITE
174f3 52 5f 44 42 4d 4f 44 3b 0a 20 20 61 73 73 65 72 R_DBMOD;. asser
174f4 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f t( assert_pager_
174f5 73 74 61 74 65 28 70 50 61 67 65 72 29 20 29 3b state(pPager) );
174f6 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
174f7 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 _OK;.}../*.** Th
174f8 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 e argument is th
174f9 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e e first in a lin
174fa 6b 65 64 20 6c 69 73 74 20 6f 66 20 64 69 72 74 ked list of dirt
174fb 79 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 y pages connecte
174fc 64 0a 2a 2a 20 62 79 20 74 68 65 20 50 67 48 64 d.** by the PgHd
174fd 72 2e 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 r.pDirty pointer
174fe 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
174ff 77 72 69 74 65 73 20 65 61 63 68 20 6f 6e 65 20 writes each one
17500 6f 66 20 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d of the.** in-mem
17501 6f 72 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 ory pages in the
17502 20 6c 69 73 74 20 74 6f 20 74 68 65 20 64 61 74 list to the dat
17503 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 abase file. The
17504 61 72 67 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 argument may.**
17505 62 65 20 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 be NULL, represe
17506 6e 74 69 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c nting an empty l
17507 69 73 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ist. In this cas
17508 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 e this function
17509 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a is.** a no-op..*
1750a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 6d *.** The pager m
1750b 75 73 74 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 ust hold at leas
1750c 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 t a RESERVED loc
1750d 6b 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 k when this func
1750e 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 tion.** is calle
1750f 64 2e 20 42 65 66 6f 72 65 20 77 72 69 74 69 6e d. Before writin
17510 67 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 g anything to th
17511 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
17512 20 74 68 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 this lock.** is
17513 20 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 upgraded to an
17514 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 EXCLUSIVE lock.
17515 49 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e If the lock cann
17516 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a ot be obtained,.
17517 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 ** SQLITE_BUSY i
17518 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e s returned and n
17519 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 o data is writte
1751a 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 n to the databas
1751b 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 e file..** .** I
1751c 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 61 f the pager is a
1751d 20 74 65 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 temp-file pager
1751e 20 61 6e 64 20 74 68 65 20 61 63 74 75 61 6c 20 and the actual
1751f 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 file-system file
17520 0a 2a 2a 20 69 73 20 6e 6f 74 20 79 65 74 20 6f .** is not yet o
17521 70 65 6e 2c 20 69 74 20 69 73 20 63 72 65 61 74 pen, it is creat
17522 65 64 20 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 ed and opened be
17523 66 6f 72 65 20 61 6e 79 20 64 61 74 61 20 69 73 fore any data is
17524 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 .** written out
17525 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 ..**.** Once the
17526 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 75 lock has been u
17527 70 67 72 61 64 65 64 20 61 6e 64 2c 20 69 66 20 pgraded and, if
17528 6e 65 63 65 73 73 61 72 79 2c 20 74 68 65 20 66 necessary, the f
17529 69 6c 65 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 ile opened,.** t
1752a 68 65 20 70 61 67 65 73 20 61 72 65 20 77 72 69 he pages are wri
1752b 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 tten out to the
1752c 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e database file in
1752d 20 6c 69 73 74 20 6f 72 64 65 72 2e 20 57 72 69 list order. Wri
1752e 74 69 6e 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 ting.** a page i
1752f 73 20 73 6b 69 70 70 65 64 20 69 66 20 69 74 20 s skipped if it
17530 6d 65 65 74 73 20 65 69 74 68 65 72 20 6f 66 20 meets either of
17531 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 the following cr
17532 69 74 65 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 iteria:.**.**
17533 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 * The page numbe
17534 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 r is greater tha
17535 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 n Pager.dbSize,
17536 6f 72 0a 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 or.** * The PG
17537 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 HDR_DONT_WRITE f
17538 6c 61 67 20 69 73 20 73 65 74 20 6f 6e 20 74 68 lag is set on th
17539 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 e page..**.** If
1753a 20 77 72 69 74 69 6e 67 20 6f 75 74 20 61 20 70 writing out a p
1753b 61 67 65 20 63 61 75 73 65 73 20 74 68 65 20 64 age causes the d
1753c 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 atabase file to
1753d 67 72 6f 77 2c 20 50 61 67 65 72 2e 64 62 46 69 grow, Pager.dbFi
1753e 6c 65 53 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 leSize.** is upd
1753f 61 74 65 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 ated accordingly
17540 2e 20 49 66 20 70 61 67 65 20 31 20 69 73 20 77 . If page 1 is w
17541 72 69 74 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e ritten out, then
17542 20 74 68 65 20 76 61 6c 75 65 20 63 61 63 68 65 the value cache
17543 64 0a 2a 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 d.** in Pager.db
17544 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20 75 70 FileVers[] is up
17545 64 61 74 65 64 20 74 6f 20 6d 61 74 63 68 20 74 dated to match t
17546 68 65 20 6e 65 77 20 76 61 6c 75 65 20 73 74 6f he new value sto
17547 72 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 red in.** the da
17548 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
17549 2a 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 ** If everything
1754a 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 is successful,
1754b 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
1754c 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 urned. If an IO
1754d 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 error .** occurs
1754e 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f , an IO error co
1754f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 de is returned.
17550 4f 72 2c 20 69 66 20 74 68 65 20 45 58 43 4c 55 Or, if the EXCLU
17551 53 49 56 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 SIVE lock cannot
17552 0a 2a 2a 20 62 65 20 6f 62 74 61 69 6e 65 64 2c .** be obtained,
17553 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 SQLITE_BUSY is
17554 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 returned..*/.sta
17555 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 tic int pager_wr
17556 69 74 65 5f 70 61 67 65 6c 69 73 74 28 50 61 67 ite_pagelist(Pag
17557 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 48 64 er *pPager, PgHd
17558 72 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 r *pList){. int
17559 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1755a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1755b 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
1755c 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 */.. /* This f
1755d 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 unction is only
1755e 63 61 6c 6c 65 64 20 66 6f 72 20 72 6f 6c 6c 62 called for rollb
1755f 61 63 6b 20 70 61 67 65 72 73 20 69 6e 20 57 52 ack pagers in WR
17560 49 54 45 52 5f 44 42 4d 4f 44 20 73 74 61 74 65 ITER_DBMOD state
17561 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 . */. assert( !
17562 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 pagerUseWal(pPag
17563 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 er) );. assert(
17564 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d pPager->eState=
17565 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 44 42 =PAGER_WRITER_DB
17566 4d 4f 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 MOD );. assert(
17567 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d pPager->eLock==
17568 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 EXCLUSIVE_LOCK )
17569 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 ;.. /* If the f
1756a 69 6c 65 20 69 73 20 61 20 74 65 6d 70 2d 66 69 ile is a temp-fi
1756b 6c 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 le has not yet b
1756c 65 65 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e een opened, open
1756d 20 69 74 20 6e 6f 77 2e 20 49 74 0a 20 20 2a 2a it now. It. **
1756e 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
1756f 20 66 6f 72 20 72 63 20 74 6f 20 62 65 20 6f 74 for rc to be ot
17570 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f her than SQLITE_
17571 4f 4b 20 69 66 20 74 68 69 73 20 62 72 61 6e 63 OK if this branc
17572 68 0a 20 20 2a 2a 20 69 73 20 74 61 6b 65 6e 2c h. ** is taken,
17573 20 61 73 20 70 61 67 65 72 5f 77 61 69 74 5f 6f as pager_wait_o
17574 6e 5f 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f n_lock() is a no
17575 2d 6f 70 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c -op for temp-fil
17576 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 es.. */. if( !
17577 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
17578 64 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 d) ){. assert
17579 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 ( pPager->tempFi
1757a 6c 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 le && rc==SQLITE
1757b 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 _OK );. rc =
1757c 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 pagerOpentemp(pP
1757d 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 ager, pPager->fd
1757e 2c 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 , pPager->vfsFla
1757f 67 73 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 gs);. }.. /* B
17580 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 efore the first
17581 77 72 69 74 65 2c 20 67 69 76 65 20 74 68 65 20 write, give the
17582 56 46 53 20 61 20 68 69 6e 74 20 6f 66 20 77 68 VFS a hint of wh
17583 61 74 20 74 68 65 20 66 69 6e 61 6c 0a 20 20 2a at the final. *
17584 2a 20 66 69 6c 65 20 73 69 7a 65 20 77 69 6c 6c * file size will
17585 20 62 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 be.. */. asse
17586 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f rt( rc!=SQLITE_O
17587 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 K || isOpen(pPag
17588 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 69 66 28 er->fd) );. if(
17589 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
1758a 26 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 & pPager->dbSize
1758b 3e 70 50 61 67 65 72 2d 3e 64 62 48 69 6e 74 53 >pPager->dbHintS
1758c 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ize ){. sqlit
1758d 65 33 5f 69 6e 74 36 34 20 73 7a 46 69 6c 65 20 e3_int64 szFile
1758e 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 = pPager->pageSi
1758f 7a 65 20 2a 20 28 73 71 6c 69 74 65 33 5f 69 6e ze * (sqlite3_in
17590 74 36 34 29 70 50 61 67 65 72 2d 3e 64 62 53 69 t64)pPager->dbSi
17591 7a 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f ze;. sqlite3O
17592 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 48 69 6e 74 sFileControlHint
17593 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 53 51 4c (pPager->fd, SQL
17594 49 54 45 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 ITE_FCNTL_SIZE_H
17595 49 4e 54 2c 20 26 73 7a 46 69 6c 65 29 3b 0a 20 INT, &szFile);.
17596 20 20 20 70 50 61 67 65 72 2d 3e 64 62 48 69 6e pPager->dbHin
17597 74 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e tSize = pPager->
17598 64 62 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 77 dbSize;. }.. w
17599 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 hile( rc==SQLITE
1759a 5f 4f 4b 20 26 26 20 70 4c 69 73 74 20 29 7b 0a _OK && pList ){.
1759b 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 Pgno pgno =
1759c 70 4c 69 73 74 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 pList->pgno;..
1759d 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
1759e 65 20 64 69 72 74 79 20 70 61 67 65 73 20 69 6e e dirty pages in
1759f 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20 the page cache
175a0 77 69 74 68 20 70 61 67 65 20 6e 75 6d 62 65 72 with page number
175a1 73 20 67 72 65 61 74 65 72 0a 20 20 20 20 2a 2a s greater. **
175a2 20 74 68 61 6e 20 50 61 67 65 72 2e 64 62 53 69 than Pager.dbSi
175a3 7a 65 2c 20 74 68 69 73 20 6d 65 61 6e 73 20 73 ze, this means s
175a4 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 qlite3PagerTrunc
175a5 61 74 65 49 6d 61 67 65 28 29 20 77 61 73 20 63 ateImage() was c
175a6 61 6c 6c 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 alled to. **
175a7 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 20 73 6d make the file sm
175a8 61 6c 6c 65 72 20 28 70 72 65 73 75 6d 61 62 6c aller (presumabl
175a9 79 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d y by auto-vacuum
175aa 20 63 6f 64 65 29 2e 20 44 6f 20 6e 6f 74 20 77 code). Do not w
175ab 72 69 74 65 0a 20 20 20 20 2a 2a 20 61 6e 79 20 rite. ** any
175ac 73 75 63 68 20 70 61 67 65 73 20 74 6f 20 74 68 such pages to th
175ad 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 e file.. **.
175ae 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 64 6f 20 6e ** Also, do n
175af 6f 74 20 77 72 69 74 65 20 6f 75 74 20 61 6e 79 ot write out any
175b0 20 70 61 67 65 20 74 68 61 74 20 68 61 73 20 74 page that has t
175b1 68 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 he PGHDR_DONT_WR
175b2 49 54 45 20 66 6c 61 67 0a 20 20 20 20 2a 2a 20 ITE flag. **
175b3 73 65 74 20 28 73 65 74 20 62 79 20 73 71 6c 69 set (set by sqli
175b4 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 te3PagerDontWrit
175b5 65 28 29 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 e()).. */.
175b6 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65 if( pgno<=pPage
175b7 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 30 3d 3d r->dbSize && 0==
175b8 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50 47 (pList->flags&PG
175b9 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29 20 HDR_DONT_WRITE)
175ba 29 7b 0a 20 20 20 20 20 20 69 36 34 20 6f 66 66 ){. i64 off
175bb 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 set = (pgno-1)*(
175bc 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 i64)pPager->page
175bd 53 69 7a 65 3b 20 20 20 2f 2a 20 4f 66 66 73 65 Size; /* Offse
175be 74 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 t to write */.
175bf 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b char *pData;
175c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
175c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
175c2 20 20 20 2f 2a 20 44 61 74 61 20 74 6f 20 77 72 /* Data to wr
175c3 69 74 65 20 2a 2f 20 20 20 20 0a 0a 20 20 20 20 ite */ ..
175c4 20 20 61 73 73 65 72 74 28 20 28 70 4c 69 73 74 assert( (pList
175c5 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
175c6 45 44 5f 53 59 4e 43 29 3d 3d 30 20 29 3b 0a 20 ED_SYNC)==0 );.
175c7 20 20 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e if( pList->
175c8 70 67 6e 6f 3d 3d 31 20 29 20 70 61 67 65 72 5f pgno==1 ) pager_
175c9 77 72 69 74 65 5f 63 68 61 6e 67 65 63 6f 75 6e write_changecoun
175ca 74 65 72 28 70 4c 69 73 74 29 3b 0a 0a 20 20 20 ter(pList);..
175cb 20 20 20 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65 /* Encode the
175cc 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 database */.
175cd 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 CODEC2(pPager
175ce 2c 20 70 4c 69 73 74 2d 3e 70 44 61 74 61 2c 20 , pList->pData,
175cf 70 67 6e 6f 2c 20 36 2c 20 72 65 74 75 72 6e 20 pgno, 6, return
175d0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 SQLITE_NOMEM, pD
175d1 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ata);.. /*
175d2 57 72 69 74 65 20 6f 75 74 20 74 68 65 20 70 61 Write out the pa
175d3 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 ge data. */.
175d4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
175d5 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 Write(pPager->fd
175d6 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d , pData, pPager-
175d7 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 >pageSize, offse
175d8 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 t);.. /* If
175d9 20 70 61 67 65 20 31 20 77 61 73 20 6a 75 73 74 page 1 was just
175da 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65 written, update
175db 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 Pager.dbFileVer
175dc 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 20 20 20 s to match.
175dd 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6e 6f ** the value no
175de 77 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 w stored in the
175df 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 database file. I
175e0 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 0a f writing this .
175e1 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 63 61 ** page ca
175e2 75 73 65 64 20 74 68 65 20 64 61 74 61 62 61 73 used the databas
175e3 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 e file to grow,
175e4 75 70 64 61 74 65 20 64 62 46 69 6c 65 53 69 7a update dbFileSiz
175e5 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 e. . */.
175e6 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 if( pgno==1 )
175e7 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 {. memcpy
175e8 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 (&pPager->dbFile
175e9 56 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34 5d Vers, &pData[24]
175ea 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d , sizeof(pPager-
175eb 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 >dbFileVers));.
175ec 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
175ed 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 pgno>pPager->db
175ee 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 FileSize ){.
175ef 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 pPager->dbFi
175f0 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20 leSize = pgno;.
175f1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 }. pPa
175f2 67 65 72 2d 3e 61 53 74 61 74 5b 50 41 47 45 52 ger->aStat[PAGER
175f3 5f 53 54 41 54 5f 57 52 49 54 45 5d 2b 2b 3b 0a _STAT_WRITE]++;.
175f4 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 . /* Update
175f5 20 61 6e 79 20 62 61 63 6b 75 70 20 6f 62 6a 65 any backup obje
175f6 63 74 73 20 63 6f 70 79 69 6e 67 20 74 68 65 20 cts copying the
175f7 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 73 contents of this
175f8 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20 pager. */.
175f9 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 sqlite3BackupUp
175fa 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61 date(pPager->pBa
175fb 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75 38 2a ckup, pgno, (u8*
175fc 29 70 4c 69 73 74 2d 3e 70 44 61 74 61 29 3b 0a )pList->pData);.
175fd 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 . PAGERTRAC
175fe 45 28 28 22 53 54 4f 52 45 20 25 64 20 70 61 67 E(("STORE %d pag
175ff 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 5c e %d hash(%08x)\
17600 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
17601 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 PAGERID(p
17602 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 Pager), pgno, pa
17603 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 ger_pagehash(pLi
17604 73 74 29 29 29 3b 0a 20 20 20 20 20 20 49 4f 54 st)));. IOT
17605 52 41 43 45 28 28 22 50 47 4f 55 54 20 25 70 20 RACE(("PGOUT %p
17606 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 %d\n", pPager, p
17607 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50 41 47 gno));. PAG
17608 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f ER_INCR(sqlite3_
17609 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f pager_writedb_co
1760a 75 6e 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b unt);. }else{
1760b 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 . PAGERTRAC
1760c 45 28 28 22 4e 4f 53 54 4f 52 45 20 25 64 20 70 E(("NOSTORE %d p
1760d 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 age %d\n", PAGER
1760e 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f ID(pPager), pgno
1760f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 ));. }. pa
17610 67 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 ger_set_pagehash
17611 28 70 4c 69 73 74 29 3b 0a 20 20 20 20 70 4c 69 (pList);. pLi
17612 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72 st = pList->pDir
17613 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 ty;. }.. retur
17614 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 n rc;.}../*.** E
17615 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73 nsure that the s
17616 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ub-journal file
17617 69 73 20 6f 70 65 6e 2e 20 49 66 20 69 74 20 69 is open. If it i
17618 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2c 20 s already open,
17619 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f this .** functio
1761a 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a n is a no-op..**
1761b 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 .** SQLITE_OK is
1761c 20 72 65 74 75 72 6e 65 64 20 69 66 20 65 76 65 returned if eve
1761d 72 79 74 68 69 6e 67 20 67 6f 65 73 20 61 63 63 rything goes acc
1761e 6f 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2e 20 ording to plan.
1761f 41 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f An .** SQLITE_IO
17620 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f ERR_XXX error co
17621 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 de is returned i
17622 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 f a call to sqli
17623 74 65 33 4f 73 4f 70 65 6e 28 29 20 0a 2a 2a 20 te3OsOpen() .**
17624 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 fails..*/.static
17625 20 69 6e 74 20 6f 70 65 6e 53 75 62 4a 6f 75 72 int openSubJour
17626 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 nal(Pager *pPage
17627 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 r){. int rc = S
17628 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
17629 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
1762a 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 69 66 28 sjfd) ){. if(
1762b 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
1762c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
1762d 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 7c NALMODE_MEMORY |
1762e 7c 20 70 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e | pPager->subjIn
1762f 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 20 20 20 20 Memory ){.
17630 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
17631 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a lOpen(pPager->sj
17632 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a fd);. }else{.
17633 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
17634 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65 72 2c Opentemp(pPager,
17635 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 53 pPager->sjfd, S
17636 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f QLITE_OPEN_SUBJO
17637 55 52 4e 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 URNAL);. }.
17638 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
17639 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 ../*.** Append a
1763a 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 63 record of the c
1763b 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 urrent state of
1763c 70 61 67 65 20 70 50 67 20 74 6f 20 74 68 65 20 page pPg to the
1763d 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 0a 2a 2a sub-journal. .**
1763e 20 49 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65 It is the calle
1763f 72 73 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 rs responsibilit
17640 79 20 74 6f 20 75 73 65 20 73 75 62 6a 52 65 71 y to use subjReq
17641 75 69 72 65 73 50 61 67 65 28 29 20 74 6f 20 63 uiresPage() to c
17642 68 65 63 6b 20 0a 2a 2a 20 74 68 61 74 20 69 74 heck .** that it
17643 20 69 73 20 72 65 61 6c 6c 79 20 72 65 71 75 69 is really requi
17644 72 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 red before calli
17645 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ng this function
17646 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 ..**.** If succe
17647 73 73 66 75 6c 2c 20 73 65 74 20 74 68 65 20 62 ssful, set the b
17648 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 it corresponding
17649 20 74 6f 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e to pPg->pgno in
1764a 20 74 68 65 20 62 69 74 76 65 63 73 0a 2a 2a 20 the bitvecs.**
1764b 66 6f 72 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 for all open sav
1764c 65 70 6f 69 6e 74 73 20 62 65 66 6f 72 65 20 72 epoints before r
1764d 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 eturning..**.**
1764e 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
1764f 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 turns SQLITE_OK
17650 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 if everything is
17651 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 6e 20 successful, an
17652 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 IO.** error code
17653 20 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 if the attempt
17654 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 to write to the
17655 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 61 69 6c sub-journal fail
17656 73 2c 20 6f 72 20 0a 2a 2a 20 53 51 4c 49 54 45 s, or .** SQLITE
17657 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c _NOMEM if a mall
17658 6f 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20 73 oc fails while s
17659 65 74 74 69 6e 67 20 61 20 62 69 74 20 69 6e 20 etting a bit in
1765a 61 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 62 a savepoint.** b
1765b 69 74 76 65 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 itvec..*/.static
1765c 20 69 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c 50 int subjournalP
1765d 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b age(PgHdr *pPg){
1765e 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1765f 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a TE_OK;. Pager *
17660 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 pPager = pPg->pP
17661 61 67 65 72 3b 0a 20 20 69 66 28 20 70 50 61 67 ager;. if( pPag
17662 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 er->journalMode!
17663 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
17664 44 45 5f 4f 46 46 20 29 7b 0a 0a 20 20 20 20 2f DE_OFF ){.. /
17665 2a 20 4f 70 65 6e 20 74 68 65 20 73 75 62 2d 6a * Open the sub-j
17666 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 68 61 ournal, if it ha
17667 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 62 65 s not already be
17668 65 6e 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 20 en opened */.
17669 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
1766a 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 >useJournal );.
1766b 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 assert( isOpe
1766c 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c n(pPager->jfd) |
1766d 7c 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 | pagerUseWal(pP
1766e 61 67 65 72 29 20 29 3b 0a 20 20 20 20 61 73 73 ager) );. ass
1766f 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
17670 65 72 2d 3e 73 6a 66 64 29 20 7c 7c 20 70 50 61 er->sjfd) || pPa
17671 67 65 72 2d 3e 6e 53 75 62 52 65 63 3d 3d 30 20 ger->nSubRec==0
17672 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
17673 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 agerUseWal(pPage
17674 72 29 20 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 r) . ||
17675 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 pageInJournal(pP
17676 67 29 20 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 g) . ||
17677 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 pPg->pgno>pPager
17678 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 0a 20 20 ->dbOrigSize .
17679 20 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 6f 70 );. rc = op
1767a 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61 enSubJournal(pPa
1767b 67 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 ger);.. /* If
1767c 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
1767d 20 77 61 73 20 6f 70 65 6e 65 64 20 73 75 63 63 was opened succ
1767e 65 73 73 66 75 6c 6c 79 20 28 6f 72 20 77 61 73 essfully (or was
1767f 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 29 2c 0a already open),.
17680 20 20 20 20 2a 2a 20 77 72 69 74 65 20 74 68 65 ** write the
17681 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 20 journal record
17682 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 20 into the file.
17683 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 */. if( rc==S
17684 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17685 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 void *pData =
17686 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 pPg->pData;.
17687 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 28 i64 offset = (
17688 69 36 34 29 70 50 61 67 65 72 2d 3e 6e 53 75 62 i64)pPager->nSub
17689 52 65 63 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 Rec*(4+pPager->p
1768a 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 ageSize);.
1768b 63 68 61 72 20 2a 70 44 61 74 61 32 3b 0a 20 20 char *pData2;.
1768c 0a 20 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 . CODEC2(pP
1768d 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 ager, pData, pPg
1768e 2d 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 74 75 72 ->pgno, 7, retur
1768f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 n SQLITE_NOMEM,
17690 70 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20 50 pData2);. P
17691 41 47 45 52 54 52 41 43 45 28 28 22 53 54 4d 54 AGERTRACE(("STMT
17692 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 -JOURNAL %d page
17693 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 %d\n", PAGERID(
17694 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 pPager), pPg->pg
17695 6e 6f 29 29 3b 0a 20 20 20 20 20 20 72 63 20 3d no));. rc =
17696 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 write32bits(pPa
17697 67 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 66 73 65 ger->sjfd, offse
17698 74 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 t, pPg->pgno);.
17699 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
1769a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1769b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1769c 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 73 6a Write(pPager->sj
1769d 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 61 67 fd, pData2, pPag
1769e 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 er->pageSize, of
1769f 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 20 20 7d fset+4);. }
176a0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
176a1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
176a2 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 {. pPager->nS
176a3 75 62 52 65 63 2b 2b 3b 0a 20 20 20 20 61 73 73 ubRec++;. ass
176a4 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 ert( pPager->nSa
176a5 76 65 70 6f 69 6e 74 3e 30 20 29 3b 0a 20 20 20 vepoint>0 );.
176a6 20 72 63 20 3d 20 61 64 64 54 6f 53 61 76 65 70 rc = addToSavep
176a7 6f 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 ointBitvecs(pPag
176a8 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a er, pPg->pgno);.
176a9 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
176aa 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
176ab 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
176ac 64 20 62 79 20 74 68 65 20 70 63 61 63 68 65 20 d by the pcache
176ad 6c 61 79 65 72 20 77 68 65 6e 20 69 74 20 68 61 layer when it ha
176ae 73 20 72 65 61 63 68 65 64 20 73 6f 6d 65 0a 2a s reached some.*
176af 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 79 20 6c 69 * soft memory li
176b0 6d 69 74 2e 20 54 68 65 20 66 69 72 73 74 20 61 mit. The first a
176b1 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 rgument is a poi
176b2 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 20 nter to a Pager
176b3 6f 62 6a 65 63 74 0a 2a 2a 20 28 63 61 73 74 20 object.** (cast
176b4 61 73 20 61 20 76 6f 69 64 2a 29 2e 20 54 68 65 as a void*). The
176b5 20 70 61 67 65 72 20 69 73 20 61 6c 77 61 79 73 pager is always
176b6 20 27 70 75 72 67 65 61 62 6c 65 27 20 28 6e 6f 'purgeable' (no
176b7 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a t an in-memory.*
176b8 2a 20 64 61 74 61 62 61 73 65 29 2e 20 54 68 65 * database). The
176b9 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
176ba 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 is a reference
176bb 74 6f 20 61 20 70 61 67 65 20 74 68 61 74 20 69 to a page that i
176bc 73 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 s .** currently
176bd 64 69 72 74 79 20 62 75 74 20 68 61 73 20 6e 6f dirty but has no
176be 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 outstanding ref
176bf 65 72 65 6e 63 65 73 2e 20 54 68 65 20 70 61 67 erences. The pag
176c0 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 e.** is always a
176c1 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
176c2 68 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 he Pager object
176c3 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
176c4 72 73 74 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 rst .** argument
176c5 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 62 20 ..**.** The job
176c6 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e of this function
176c7 20 69 73 20 74 6f 20 6d 61 6b 65 20 70 50 67 20 is to make pPg
176c8 63 6c 65 61 6e 20 62 79 20 77 72 69 74 69 6e 67 clean by writing
176c9 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a its contents.**
176ca 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 out to the data
176cb 62 61 73 65 20 66 69 6c 65 2c 20 69 66 20 70 6f base file, if po
176cc 73 73 69 62 6c 65 2e 20 54 68 69 73 20 6d 61 79 ssible. This may
176cd 20 69 6e 76 6f 6c 76 65 20 73 79 6e 63 69 6e 67 involve syncing
176ce 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the.** journal
176cf 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 file. .**.** If
176d0 73 75 63 63 65 73 73 66 75 6c 2c 20 73 71 6c 69 successful, sqli
176d1 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 te3PcacheMakeCle
176d2 61 6e 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f an() is called o
176d3 6e 20 74 68 65 20 70 61 67 65 20 61 6e 64 0a 2a n the page and.*
176d4 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 * SQLITE_OK retu
176d5 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 rned. If an IO e
176d6 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
176d7 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65 e trying to make
176d8 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 63 6c 65 the.** page cle
176d9 61 6e 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 an, the IO error
176da 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
176db 64 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 63 d. If the page c
176dc 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6d 61 64 65 annot be.** made
176dd 20 63 6c 65 61 6e 20 66 6f 72 20 73 6f 6d 65 20 clean for some
176de 6f 74 68 65 72 20 72 65 61 73 6f 6e 2c 20 62 75 other reason, bu
176df 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 t no error occur
176e0 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f s, then SQLITE_O
176e1 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 K.** is returned
176e2 20 62 79 20 73 71 6c 69 74 65 33 50 63 61 63 68 by sqlite3Pcach
176e3 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20 eMakeClean() is
176e4 6e 6f 74 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 not called..*/.s
176e5 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 53 tatic int pagerS
176e6 74 72 65 73 73 28 76 6f 69 64 20 2a 70 2c 20 50 tress(void *p, P
176e7 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 gHdr *pPg){. Pa
176e8 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 28 50 ger *pPager = (P
176e9 61 67 65 72 20 2a 29 70 3b 0a 20 20 69 6e 74 20 ager *)p;. int
176ea 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
176eb 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e . assert( pPg->
176ec 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 29 pPager==pPager )
176ed 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d ;. assert( pPg-
176ee 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
176ef 54 59 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 TY );.. /* The
176f0 64 6f 4e 6f 74 53 79 6e 63 53 70 69 6c 6c 20 66 doNotSyncSpill f
176f1 6c 61 67 20 69 73 20 73 65 74 20 64 75 72 69 6e lag is set durin
176f2 67 20 74 69 6d 65 73 20 77 68 65 6e 20 64 6f 69 g times when doi
176f3 6e 67 20 61 20 73 79 6e 63 20 6f 66 0a 20 20 2a ng a sync of. *
176f4 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 61 * journal (and a
176f5 64 64 69 6e 67 20 61 20 6e 65 77 20 68 65 61 64 dding a new head
176f6 65 72 29 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 er) is not allow
176f7 65 64 2e 20 20 54 68 69 73 20 6f 63 63 75 72 73 ed. This occurs
176f8 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 63 61 6c . ** during cal
176f9 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 ls to sqlite3Pag
176fa 65 72 57 72 69 74 65 28 29 20 77 68 69 6c 65 20 erWrite() while
176fb 74 72 79 69 6e 67 20 74 6f 20 6a 6f 75 72 6e 61 trying to journa
176fc 6c 20 6d 75 6c 74 69 70 6c 65 0a 20 20 2a 2a 20 l multiple. **
176fd 70 61 67 65 73 20 62 65 6c 6f 6e 67 69 6e 67 20 pages belonging
176fe 74 6f 20 74 68 65 20 73 61 6d 65 20 73 65 63 74 to the same sect
176ff 6f 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 or.. **. ** Th
17700 65 20 64 6f 4e 6f 74 53 70 69 6c 6c 20 66 6c 61 e doNotSpill fla
17701 67 20 69 6e 68 69 62 69 74 73 20 61 6c 6c 20 63 g inhibits all c
17702 61 63 68 65 20 73 70 69 6c 6c 69 6e 67 20 72 65 ache spilling re
17703 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 gardless of whet
17704 68 65 72 0a 20 20 2a 2a 20 6f 72 20 6e 6f 74 20 her. ** or not
17705 61 20 73 79 6e 63 20 69 73 20 72 65 71 75 69 72 a sync is requir
17706 65 64 2e 20 20 54 68 69 73 20 69 73 20 73 65 74 ed. This is set
17707 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 during a rollba
17708 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 70 ck.. **. ** Sp
17709 69 6c 6c 69 6e 67 20 69 73 20 61 6c 73 6f 20 70 illing is also p
1770a 72 6f 68 69 62 69 74 65 64 20 77 68 65 6e 20 69 rohibited when i
1770b 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 n an error state
1770c 20 73 69 6e 63 65 20 74 68 61 74 20 63 6f 75 6c since that coul
1770d 64 0a 20 20 2a 2a 20 6c 65 61 64 20 74 6f 20 64 d. ** lead to d
1770e 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 atabase corrupti
1770f 6f 6e 2e 20 20 20 49 6e 20 74 68 65 20 63 75 72 on. In the cur
17710 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 rent implementat
17711 6f 6e 20 69 74 20 0a 20 20 2a 2a 20 69 73 20 69 on it . ** is i
17712 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 73 71 mpossible for sq
17713 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 lite3PcacheFetch
17714 28 29 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 () to be called
17715 77 69 74 68 20 63 72 65 61 74 65 46 6c 61 67 3d with createFlag=
17716 3d 31 0a 20 20 2a 2a 20 77 68 69 6c 65 20 69 6e =1. ** while in
17717 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
17718 2c 20 68 65 6e 63 65 20 69 74 20 69 73 20 69 6d , hence it is im
17719 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 possible for thi
1771a 73 20 72 6f 75 74 69 6e 65 20 74 6f 0a 20 20 2a s routine to. *
1771b 2a 20 62 65 20 63 61 6c 6c 65 64 20 69 6e 20 74 * be called in t
1771c 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 he error state.
1771d 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 77 Nevertheless, w
1771e 65 20 69 6e 63 6c 75 64 65 20 61 20 4e 45 56 45 e include a NEVE
1771f 52 28 29 0a 20 20 2a 2a 20 74 65 73 74 20 66 6f R(). ** test fo
17720 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 r the error stat
17721 65 20 61 73 20 61 20 73 61 66 65 67 75 61 72 64 e as a safeguard
17722 20 61 67 61 69 6e 73 74 20 66 75 74 75 72 65 20 against future
17723 63 68 61 6e 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 changes.. */.
17724 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 if( NEVER(pPager
17725 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 74 ->errCode) ) ret
17726 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
17727 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e if( pPager->doN
17728 6f 74 53 70 69 6c 6c 20 29 20 72 65 74 75 72 6e otSpill ) return
17729 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
1772a 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 ( pPager->doNotS
1772b 79 6e 63 53 70 69 6c 6c 20 26 26 20 28 70 50 67 yncSpill && (pPg
1772c 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f ->flags & PGHDR_
1772d 4e 45 45 44 5f 53 59 4e 43 29 21 3d 30 20 29 7b NEED_SYNC)!=0 ){
1772e 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1772f 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70 50 TE_OK;. }.. pP
17730 67 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 g->pDirty = 0;.
17731 20 69 66 28 20 70 61 67 65 72 55 73 65 57 61 6c if( pagerUseWal
17732 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 (pPager) ){.
17733 2f 2a 20 57 72 69 74 65 20 61 20 73 69 6e 67 6c /* Write a singl
17734 65 20 66 72 61 6d 65 20 66 6f 72 20 74 68 69 73 e frame for this
17735 20 70 61 67 65 20 74 6f 20 74 68 65 20 6c 6f 67 page to the log
17736 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 75 62 . */. if( sub
17737 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 jRequiresPage(pP
17738 67 29 20 29 7b 20 0a 20 20 20 20 20 20 72 63 20 g) ){ . rc
17739 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 = subjournalPage
1773a 28 70 50 67 29 3b 20 0a 20 20 20 20 7d 0a 20 20 (pPg); . }.
1773b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1773c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 _OK ){. rc
1773d 3d 20 70 61 67 65 72 57 61 6c 46 72 61 6d 65 73 = pagerWalFrames
1773e 28 70 50 61 67 65 72 2c 20 70 50 67 2c 20 30 2c (pPager, pPg, 0,
1773f 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 0);. }. }el
17740 73 65 7b 0a 20 20 0a 20 20 20 20 2f 2a 20 53 79 se{. . /* Sy
17741 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 nc the journal f
17742 69 6c 65 20 69 66 20 72 65 71 75 69 72 65 64 2e ile if required.
17743 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 67 2d */. if( pPg-
17744 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
17745 44 5f 53 59 4e 43 20 0a 20 20 20 20 20 7c 7c 20 D_SYNC . ||
17746 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d pPager->eState==
17747 50 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 PAGER_WRITER_CAC
17748 48 45 4d 4f 44 0a 20 20 20 20 29 7b 0a 20 20 20 HEMOD. ){.
17749 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 rc = syncJour
1774a 6e 61 6c 28 70 50 61 67 65 72 2c 20 31 29 3b 0a nal(pPager, 1);.
1774b 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
1774c 49 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 If the page numb
1774d 65 72 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 er of this page
1774e 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 is larger than t
1774f 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 he current size
17750 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 of. ** the da
17751 74 61 62 61 73 65 20 69 6d 61 67 65 2c 20 69 74 tabase image, it
17752 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 may need to be
17753 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 written to the s
17754 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 ub-journal..
17755 2a 2a 20 54 68 69 73 20 69 73 20 62 65 63 61 75 ** This is becau
17756 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 70 se the call to p
17757 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c ager_write_pagel
17758 69 73 74 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c ist() below will
17759 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 61 63 74 75 not. ** actu
1775a 61 6c 6c 79 20 77 72 69 74 65 20 64 61 74 61 20 ally write data
1775b 74 6f 20 74 68 65 20 66 69 6c 65 20 69 6e 20 74 to the file in t
1775c 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 2a 2a his case.. **
1775d 0a 20 20 20 20 2a 2a 20 43 6f 6e 73 69 64 65 72 . ** Consider
1775e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
1775f 65 71 75 65 6e 63 65 20 6f 66 20 65 76 65 6e 74 equence of event
17760 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a s:. **. **
17761 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 2a 2a BEGIN;. **
17762 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61 <journal pa
17763 67 65 20 58 3e 0a 20 20 20 20 2a 2a 20 20 20 20 ge X>. **
17764 20 3c 6d 6f 64 69 66 79 20 70 61 67 65 20 58 3e <modify page X>
17765 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 41 56 45 . ** SAVE
17766 50 4f 49 4e 54 20 73 70 3b 0a 20 20 20 20 2a 2a POINT sp;. **
17767 20 20 20 20 20 20 20 3c 73 68 72 69 6e 6b 20 64 <shrink d
17768 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 atabase file to
17769 59 20 70 61 67 65 73 3e 0a 20 20 20 20 2a 2a 20 Y pages>. **
1776a 20 20 20 20 20 20 70 61 67 65 72 53 74 72 65 73 pagerStres
1776b 73 28 70 61 67 65 20 58 29 0a 20 20 20 20 2a 2a s(page X). **
1776c 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f ROLLBACK TO
1776d 20 73 70 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 sp;. **.
1776e 2a 2a 20 49 66 20 28 58 3e 59 29 2c 20 74 68 65 ** If (X>Y), the
1776f 6e 20 77 68 65 6e 20 70 61 67 65 72 53 74 72 65 n when pagerStre
17770 73 73 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67 ss is called pag
17771 65 20 58 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 e X will not be
17772 77 72 69 74 74 65 6e 0a 20 20 20 20 2a 2a 20 6f written. ** o
17773 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ut to the databa
17774 73 65 20 66 69 6c 65 2c 20 62 75 74 20 77 69 6c se file, but wil
17775 6c 20 62 65 20 64 72 6f 70 70 65 64 20 66 72 6f l be dropped fro
17776 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 m the cache. The
17777 6e 2c 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 n,. ** follow
17778 69 6e 67 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 ing the "ROLLBAC
17779 4b 20 54 4f 20 73 70 22 20 73 74 61 74 65 6d 65 K TO sp" stateme
1777a 6e 74 2c 20 72 65 61 64 69 6e 67 20 70 61 67 65 nt, reading page
1777b 20 58 20 77 69 6c 6c 20 72 65 61 64 0a 20 20 20 X will read.
1777c 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68 ** data from th
1777d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1777e 20 54 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 This will be th
1777f 65 20 63 6f 70 79 20 6f 66 20 70 61 67 65 20 58 e copy of page X
17780 20 61 73 20 69 74 0a 20 20 20 20 2a 2a 20 77 61 as it. ** wa
17781 73 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 s when the trans
17782 61 63 74 69 6f 6e 20 73 74 61 72 74 65 64 2c 20 action started,
17783 6e 6f 74 20 61 73 20 69 74 20 77 61 73 20 77 68 not as it was wh
17784 65 6e 20 22 53 41 56 45 50 4f 49 4e 54 20 73 70 en "SAVEPOINT sp
17785 22 0a 20 20 20 20 2a 2a 20 77 61 73 20 65 78 65 ". ** was exe
17786 63 75 74 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 cuted.. **.
17787 20 20 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f ** The solutio
17788 6e 20 69 73 20 74 6f 20 77 72 69 74 65 20 74 68 n is to write th
17789 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 20 66 e current data f
1778a 6f 72 20 70 61 67 65 20 58 20 69 6e 74 6f 20 74 or page X into t
1778b 68 65 20 0a 20 20 20 20 2a 2a 20 73 75 62 2d 6a he . ** sub-j
1778c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 6f 77 20 ournal file now
1778d 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c (if it is not al
1778e 72 65 61 64 79 20 74 68 65 72 65 29 2c 20 73 6f ready there), so
1778f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 0a 20 20 that it will.
17790 20 20 2a 2a 20 62 65 20 72 65 73 74 6f 72 65 64 ** be restored
17791 20 74 6f 20 69 74 73 20 63 75 72 72 65 6e 74 20 to its current
17792 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65 20 22 value when the "
17793 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 ROLLBACK TO sp"
17794 69 73 20 0a 20 20 20 20 2a 2a 20 65 78 65 63 75 is . ** execu
17795 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ted.. */.
17796 69 66 28 20 4e 45 56 45 52 28 0a 20 20 20 20 20 if( NEVER(.
17797 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b rc==SQLITE_OK
17798 20 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 && pPg->pgno>pP
17799 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 ager->dbSize &&
1779a 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 subjRequiresPage
1779b 28 70 50 67 29 0a 20 20 20 20 29 20 29 7b 0a 20 (pPg). ) ){.
1779c 20 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 rc = subjou
1779d 72 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a 20 rnalPage(pPg);.
1779e 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 57 }. . /* W
1779f 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 rite the content
177a0 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 6f 75 s of the page ou
177a1 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 t to the databas
177a2 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 e file. */. i
177a3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
177a4 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
177a5 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 ( (pPg->flags&PG
177a6 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3d 3d HDR_NEED_SYNC)==
177a7 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 );. rc =
177a8 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 pager_write_page
177a9 6c 69 73 74 28 70 50 61 67 65 72 2c 20 70 50 67 list(pPager, pPg
177aa 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
177ab 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 /* Mark the page
177ac 20 61 73 20 63 6c 65 61 6e 2e 20 2a 2f 0a 20 20 as clean. */.
177ad 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
177ae 4b 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52 K ){. PAGERTR
177af 41 43 45 28 28 22 53 54 52 45 53 53 20 25 64 20 ACE(("STRESS %d
177b0 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 page %d\n", PAGE
177b1 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 RID(pPager), pPg
177b2 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71 ->pgno));. sq
177b3 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
177b4 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 0a lean(pPg);. }..
177b5 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 return pager_e
177b6 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 rror(pPager, rc)
177b7 3b 20 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c ; .}.../*.** All
177b8 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 ocate and initia
177b9 6c 69 7a 65 20 61 20 6e 65 77 20 50 61 67 65 72 lize a new Pager
177ba 20 6f 62 6a 65 63 74 20 61 6e 64 20 70 75 74 20 object and put
177bb 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 0a a pointer to it.
177bc 2a 2a 20 69 6e 20 2a 70 70 50 61 67 65 72 2e 20 ** in *ppPager.
177bd 54 68 65 20 70 61 67 65 72 20 73 68 6f 75 6c 64 The pager should
177be 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 66 eventually be f
177bf 72 65 65 64 20 62 79 20 70 61 73 73 69 6e 67 20 reed by passing
177c0 69 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 it.** to sqlite3
177c1 50 61 67 65 72 43 6c 6f 73 65 28 29 2e 0a 2a 2a PagerClose()..**
177c2 0a 2a 2a 20 54 68 65 20 7a 46 69 6c 65 6e 61 6d .** The zFilenam
177c3 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 e argument is th
177c4 65 20 70 61 74 68 20 74 6f 20 74 68 65 20 64 61 e path to the da
177c5 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f tabase file to o
177c6 70 65 6e 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 pen..** If zFile
177c7 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 name is NULL the
177c8 6e 20 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e 61 6d n a randomly-nam
177c9 65 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c ed temporary fil
177ca 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 e is created.**
177cb 61 6e 64 20 75 73 65 64 20 61 73 20 74 68 65 20 and used as the
177cc 66 69 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65 file to be cache
177cd 64 2e 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c d. Temporary fil
177ce 65 73 20 61 72 65 20 62 65 20 64 65 6c 65 74 65 es are be delete
177cf 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c d.** automatical
177d0 6c 79 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 ly when they are
177d1 20 63 6c 6f 73 65 64 2e 20 49 66 20 7a 46 69 6c closed. If zFil
177d2 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 ename is ":memor
177d3 79 3a 22 20 74 68 65 6e 20 0a 2a 2a 20 61 6c 6c y:" then .** all
177d4 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 information is
177d5 68 65 6c 64 20 69 6e 20 63 61 63 68 65 2e 20 49 held in cache. I
177d6 74 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 t is never writt
177d7 65 6e 20 74 6f 20 64 69 73 6b 2e 20 0a 2a 2a 20 en to disk. .**
177d8 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 This can be used
177d9 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e to implement an
177da 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
177db 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e ase..**.** The n
177dc 45 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 Extra parameter
177dd 73 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 specifies the nu
177de 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
177df 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 space allocated
177e0 0a 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20 65 .** along with e
177e1 61 63 68 20 70 61 67 65 20 72 65 66 65 72 65 6e ach page referen
177e2 63 65 2e 20 54 68 69 73 20 73 70 61 63 65 20 69 ce. This space i
177e3 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 s available to t
177e4 68 65 20 75 73 65 72 0a 2a 2a 20 76 69 61 20 74 he user.** via t
177e5 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 he sqlite3PagerG
177e6 65 74 45 78 74 72 61 28 29 20 41 50 49 2e 0a 2a etExtra() API..*
177e7 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 *.** The flags a
177e8 72 67 75 6d 65 6e 74 20 69 73 20 75 73 65 64 20 rgument is used
177e9 74 6f 20 73 70 65 63 69 66 79 20 70 72 6f 70 65 to specify prope
177ea 72 74 69 65 73 20 74 68 61 74 20 61 66 66 65 63 rties that affec
177eb 74 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 69 t the.** operati
177ec 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 72 2e on of the pager.
177ed 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20 70 61 It should be pa
177ee 73 73 65 64 20 73 6f 6d 65 20 62 69 74 77 69 73 ssed some bitwis
177ef 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 0a 2a 2a e combination.**
177f0 20 6f 66 20 74 68 65 20 50 41 47 45 52 5f 2a 20 of the PAGER_*
177f1 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 flags..**.** The
177f2 20 76 66 73 46 6c 61 67 73 20 70 61 72 61 6d 65 vfsFlags parame
177f3 74 65 72 20 69 73 20 61 20 62 69 74 6d 61 73 6b ter is a bitmask
177f4 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20 to pass to the
177f5 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 0a flags parameter.
177f6 2a 2a 20 6f 66 20 74 68 65 20 78 4f 70 65 6e 28 ** of the xOpen(
177f7 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 ) method of the
177f8 73 75 70 70 6c 69 65 64 20 56 46 53 20 77 68 65 supplied VFS whe
177f9 6e 20 6f 70 65 6e 69 6e 67 20 66 69 6c 65 73 2e n opening files.
177fa 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 .**.** If the p
177fb 61 67 65 72 20 6f 62 6a 65 63 74 20 69 73 20 61 ager object is a
177fc 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 llocated and the
177fd 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 20 specified file
177fe 6f 70 65 6e 65 64 20 0a 2a 2a 20 73 75 63 63 65 opened .** succe
177ff 73 73 66 75 6c 6c 79 2c 20 53 51 4c 49 54 45 5f ssfully, SQLITE_
17800 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 OK is returned a
17801 6e 64 20 2a 70 70 50 61 67 65 72 20 73 65 74 20 nd *ppPager set
17802 74 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20 74 to point to.** t
17803 68 65 20 6e 65 77 20 70 61 67 65 72 20 6f 62 6a he new pager obj
17804 65 63 74 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 ect. If an error
17805 20 6f 63 63 75 72 73 2c 20 2a 70 70 50 61 67 65 occurs, *ppPage
17806 72 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c r is set to NULL
17807 0a 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 63 6f .** and error co
17808 64 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 de returned. Thi
17809 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 s function may r
1780a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
1780b 45 4d 0a 2a 2a 20 28 73 71 6c 69 74 65 33 4d 61 EM.** (sqlite3Ma
1780c 6c 6c 6f 63 28 29 20 69 73 20 75 73 65 64 20 74 lloc() is used t
1780d 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 o allocate memor
1780e 79 29 2c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f y), SQLITE_CANTO
1780f 50 45 4e 20 6f 72 20 0a 2a 2a 20 76 61 72 69 6f PEN or .** vario
17810 75 73 20 53 51 4c 49 54 45 5f 49 4f 5f 58 58 58 us SQLITE_IO_XXX
17811 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 errors..*/.SQLI
17812 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
17813 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 qlite3PagerOpen(
17814 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
17815 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 54 pVfs, /* T
17816 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 he virtual file
17817 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f system to use */
17818 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67 . Pager **ppPag
17819 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f er, /* O
1781a 55 54 3a 20 52 65 74 75 72 6e 20 74 68 65 20 50 UT: Return the P
1781b 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 68 ager structure h
1781c 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ere */. const c
1781d 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
1781e 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
1781f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
17820 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 o open */. int
17821 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 nExtra,
17822 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 62 79 /* Extra by
17823 74 65 73 20 61 70 70 65 6e 64 20 74 6f 20 65 61 tes append to ea
17824 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 ch in-memory pag
17825 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 e */. int flags
17826 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
17827 2f 2a 20 66 6c 61 67 73 20 63 6f 6e 74 72 6f 6c /* flags control
17828 6c 69 6e 67 20 74 68 69 73 20 66 69 6c 65 20 2a ling this file *
17829 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 /. int vfsFlags
1782a 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
1782b 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 flags passed thr
1782c 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f ough to sqlite3_
1782d 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 vfs.xOpen() */.
1782e 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 29 void (*xReinit)
1782f 28 44 62 50 61 67 65 2a 29 20 2f 2a 20 46 75 6e (DbPage*) /* Fun
17830 63 74 69 6f 6e 20 74 6f 20 72 65 69 6e 69 74 69 ction to reiniti
17831 61 6c 69 7a 65 20 70 61 67 65 73 20 2a 2f 0a 29 alize pages */.)
17832 7b 0a 20 20 75 38 20 2a 70 50 74 72 3b 0a 20 20 {. u8 *pPtr;.
17833 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
17834 30 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 0; /* Page
17835 72 20 6f 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f r object to allo
17836 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 cate and return
17837 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
17838 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a LITE_OK; /*
17839 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
1783a 20 20 69 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d int tempFile =
1783b 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
1783c 75 65 20 66 6f 72 20 74 65 6d 70 20 66 69 6c 65 ue for temp file
1783d 73 20 28 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f s (incl. in-memo
1783e 72 79 20 66 69 6c 65 73 29 20 2a 2f 0a 20 20 69 ry files) */. i
1783f 6e 74 20 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20 nt memDb = 0;
17840 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
17841 69 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e if this is an in
17842 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a -memory file */.
17843 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d int readOnly =
17844 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
17845 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 ue if this is a
17846 72 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a read-only file *
17847 2f 0a 20 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46 /. int journalF
17848 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 ileSize; /*
17849 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 Bytes to allocat
1784a 65 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e e for each journ
1784b 61 6c 20 66 64 20 2a 2f 0a 20 20 63 68 61 72 20 al fd */. char
1784c 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 *zPathname = 0;
1784d 20 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68 /* Full path
1784e 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c to database fil
1784f 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 68 e */. int nPath
17850 6e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 name = 0;
17851 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
17852 65 73 20 69 6e 20 7a 50 61 74 68 6e 61 6d 65 20 es in zPathname
17853 2a 2f 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 */. int useJour
17854 6e 61 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50 nal = (flags & P
17855 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 AGER_OMIT_JOURNA
17856 4c 29 3d 3d 30 3b 20 2f 2a 20 46 61 6c 73 65 20 L)==0; /* False
17857 74 6f 20 6f 6d 69 74 20 6a 6f 75 72 6e 61 6c 20 to omit journal
17858 2a 2f 0a 20 20 69 6e 74 20 70 63 61 63 68 65 53 */. int pcacheS
17859 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 50 63 61 ize = sqlite3Pca
1785a 63 68 65 53 69 7a 65 28 29 3b 20 20 20 20 20 20 cheSize();
1785b 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c /* Bytes to all
1785c 6f 63 61 74 65 20 66 6f 72 20 50 43 61 63 68 65 ocate for PCache
1785d 20 2a 2f 0a 20 20 75 33 32 20 73 7a 50 61 67 65 */. u32 szPage
1785e 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 Dflt = SQLITE_DE
1785f 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b FAULT_PAGE_SIZE;
17860 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 70 61 67 /* Default pag
17861 65 20 73 69 7a 65 20 2a 2f 0a 20 20 63 6f 6e 73 e size */. cons
17862 74 20 63 68 61 72 20 2a 7a 55 72 69 20 3d 20 30 t char *zUri = 0
17863 3b 20 20 20 20 2f 2a 20 55 52 49 20 61 72 67 73 ; /* URI args
17864 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e to copy */. in
17865 74 20 6e 55 72 69 20 3d 20 30 3b 20 20 20 20 20 t nUri = 0;
17866 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
17867 20 6f 66 20 62 79 74 65 73 20 6f 66 20 55 52 49 of bytes of URI
17868 20 61 72 67 73 20 61 74 20 2a 7a 55 72 69 20 2a args at *zUri *
17869 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f /.. /* Figure o
1786a 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 ut how much spac
1786b 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f e is required fo
1786c 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66 r each journal f
1786d 69 6c 65 2d 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 ile-handle. **
1786e 28 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 6f (there are two o
1786f 66 20 74 68 65 6d 2c 20 74 68 65 20 6d 61 69 6e f them, the main
17870 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 journal and the
17871 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54 sub-journal). T
17872 68 69 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 his. ** is the
17873 6d 61 78 69 6d 75 6d 20 73 70 61 63 65 20 72 65 maximum space re
17874 71 75 69 72 65 64 20 66 6f 72 20 61 6e 20 69 6e quired for an in
17875 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 -memory journal
17876 66 69 6c 65 20 68 61 6e 64 6c 65 20 0a 20 20 2a file handle . *
17877 2a 20 61 6e 64 20 61 20 72 65 67 75 6c 61 72 20 * and a regular
17878 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e journal file-han
17879 64 6c 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 61 dle. Note that a
1787a 20 22 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 "regular journa
1787b 6c 2d 68 61 6e 64 6c 65 22 0a 20 20 2a 2a 20 6d l-handle". ** m
1787c 61 79 20 62 65 20 61 20 77 72 61 70 70 65 72 20 ay be a wrapper
1787d 63 61 70 61 62 6c 65 20 6f 66 20 63 61 63 68 69 capable of cachi
1787e 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 6f 72 ng the first por
1787f 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 tion of the jour
17880 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e nal. ** file in
17881 20 6d 65 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65 memory to imple
17882 6d 65 6e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d ment the atomic-
17883 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 write optimizati
17884 6f 6e 20 28 73 65 65 20 0a 20 20 2a 2a 20 73 6f on (see . ** so
17885 75 72 63 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61 urce file journa
17886 6c 2e 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 l.c).. */. if(
17887 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 sqlite3JournalS
17888 69 7a 65 28 70 56 66 73 29 3e 73 71 6c 69 74 65 ize(pVfs)>sqlite
17889 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 3MemJournalSize(
1788a 29 20 29 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c ) ){. journal
1788b 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 FileSize = ROUND
1788c 38 28 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 8(sqlite3Journal
1788d 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 7d Size(pVfs));. }
1788e 65 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 else{. journa
1788f 6c 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e lFileSize = ROUN
17890 44 38 28 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 D8(sqlite3MemJou
17891 72 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 7d rnalSize());. }
17892 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f .. /* Set the o
17893 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 74 utput variable t
17894 6f 20 4e 55 4c 4c 20 69 6e 20 63 61 73 65 20 61 o NULL in case a
17895 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 n error occurs.
17896 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 */. *ppPager =
17897 30 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 0;..#ifndef SQLI
17898 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 TE_OMIT_MEMORYDB
17899 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 50 . if( flags & P
1789a 41 47 45 52 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 AGER_MEMORY ){.
1789b 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b 0a 20 20 memDb = 1;.
1789c 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 if( zFilename
1789d 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 && zFilename[0]
1789e 29 7b 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61 ){. zPathna
1789f 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 me = sqlite3DbSt
178a0 72 44 75 70 28 30 2c 20 7a 46 69 6c 65 6e 61 6d rDup(0, zFilenam
178a1 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 50 e);. if( zP
178a2 61 74 68 6e 61 6d 65 3d 3d 30 20 20 29 20 72 65 athname==0 ) re
178a3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
178a4 4d 3b 0a 20 20 20 20 20 20 6e 50 61 74 68 6e 61 M;. nPathna
178a5 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c me = sqlite3Strl
178a6 65 6e 33 30 28 7a 50 61 74 68 6e 61 6d 65 29 3b en30(zPathname);
178a7 0a 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 . zFilename
178a8 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
178a9 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 43 6f 6d #endif.. /* Com
178aa 70 75 74 65 20 61 6e 64 20 73 74 6f 72 65 20 74 pute and store t
178ab 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 he full pathname
178ac 20 69 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 in an allocated
178ad 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 0a buffer pointed.
178ae 20 20 2a 2a 20 74 6f 20 62 79 20 7a 50 61 74 68 ** to by zPath
178af 6e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 50 61 name, length nPa
178b0 74 68 6e 61 6d 65 2e 20 4f 72 2c 20 69 66 20 74 thname. Or, if t
178b1 68 69 73 20 69 73 20 61 20 74 65 6d 70 6f 72 61 his is a tempora
178b2 72 79 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 6c 65 ry file,. ** le
178b3 61 76 65 20 62 6f 74 68 20 6e 50 61 74 68 6e 61 ave both nPathna
178b4 6d 65 20 61 6e 64 20 7a 50 61 74 68 6e 61 6d 65 me and zPathname
178b5 20 73 65 74 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a set to 0.. */.
178b6 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 if( zFilename
178b7 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 && zFilename[0]
178b8 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 ){. const cha
178b9 72 20 2a 7a 3b 0a 20 20 20 20 6e 50 61 74 68 6e r *z;. nPathn
178ba 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 ame = pVfs->mxPa
178bb 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 7a 50 thname+1;. zP
178bc 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 athname = sqlite
178bd 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 30 2c 20 3DbMallocRaw(0,
178be 6e 50 61 74 68 6e 61 6d 65 2a 32 29 3b 0a 20 20 nPathname*2);.
178bf 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 3d if( zPathname=
178c0 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 =0 ){. retu
178c1 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
178c2 0a 20 20 20 20 7d 0a 20 20 20 20 7a 50 61 74 68 . }. zPath
178c3 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 20 2f 2a 20 name[0] = 0; /*
178c4 4d 61 6b 65 20 73 75 72 65 20 69 6e 69 74 69 61 Make sure initia
178c5 6c 69 7a 65 64 20 65 76 65 6e 20 69 66 20 46 75 lized even if Fu
178c6 6c 6c 50 61 74 68 6e 61 6d 65 28 29 20 66 61 69 llPathname() fai
178c7 6c 73 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 ls */. rc = s
178c8 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 qlite3OsFullPath
178c9 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 name(pVfs, zFile
178ca 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 2c name, nPathname,
178cb 20 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 zPathname);.
178cc 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c nPathname = sql
178cd 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 50 61 ite3Strlen30(zPa
178ce 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 20 3d thname);. z =
178cf 20 7a 55 72 69 20 3d 20 26 7a 46 69 6c 65 6e 61 zUri = &zFilena
178d0 6d 65 5b 73 71 6c 69 74 65 33 53 74 72 6c 65 6e me[sqlite3Strlen
178d1 33 30 28 7a 46 69 6c 65 6e 61 6d 65 29 2b 31 5d 30(zFilename)+1]
178d2 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 20 ;. while( *z
178d3 29 7b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 73 71 ){. z += sq
178d4 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 lite3Strlen30(z)
178d5 2b 31 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 73 +1;. z += s
178d6 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
178d7 29 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e )+1;. }. n
178d8 55 72 69 20 3d 20 28 69 6e 74 29 28 26 7a 5b 31 Uri = (int)(&z[1
178d9 5d 20 2d 20 7a 55 72 69 29 3b 0a 20 20 20 20 61 ] - zUri);. a
178da 73 73 65 72 74 28 20 6e 55 72 69 3e 3d 30 20 29 ssert( nUri>=0 )
178db 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
178dc 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 50 61 74 68 LITE_OK && nPath
178dd 6e 61 6d 65 2b 38 3e 70 56 66 73 2d 3e 6d 78 50 name+8>pVfs->mxP
178de 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 athname ){.
178df 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 /* This branch
178e0 69 73 20 74 61 6b 65 6e 20 77 68 65 6e 20 74 68 is taken when th
178e1 65 20 6a 6f 75 72 6e 61 6c 20 70 61 74 68 20 72 e journal path r
178e2 65 71 75 69 72 65 64 20 62 79 0a 20 20 20 20 20 equired by.
178e3 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 ** the database
178e4 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 77 69 being opened wi
178e5 6c 6c 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 ll be more than
178e6 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 pVfs->mxPathname
178e7 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 20 . ** bytes
178e8 69 6e 20 6c 65 6e 67 74 68 2e 20 54 68 69 73 20 in length. This
178e9 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 means the databa
178ea 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70 65 se cannot be ope
178eb 6e 65 64 2c 0a 20 20 20 20 20 20 2a 2a 20 61 73 ned,. ** as
178ec 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 it will not be
178ed 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f 70 65 6e possible to open
178ee 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
178ef 65 20 6f 72 20 65 76 65 6e 0a 20 20 20 20 20 20 e or even.
178f0 2a 2a 20 63 68 65 63 6b 20 66 6f 72 20 61 20 68 ** check for a h
178f1 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 ot-journal befor
178f2 65 20 72 65 61 64 69 6e 67 2e 0a 20 20 20 20 20 e reading..
178f3 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 */. rc = S
178f4 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 QLITE_CANTOPEN_B
178f5 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 KPT;. }. i
178f6 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
178f7 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
178f8 33 44 62 46 72 65 65 28 30 2c 20 7a 50 61 74 68 3DbFree(0, zPath
178f9 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 name);. ret
178fa 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
178fb 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 }.. /* Allocate
178fc 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 memory for the
178fd 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 2c Pager structure,
178fe 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2c 20 PCache object,
178ff 74 68 65 0a 20 20 2a 2a 20 74 68 72 65 65 20 66 the. ** three f
17900 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c ile descriptors,
17901 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
17902 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 le name and the
17903 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 journal . ** fi
17904 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c 61 79 le name. The lay
17905 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20 69 73 out in memory is
17906 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a as follows:. *
17907 2a 0a 20 20 2a 2a 20 20 20 20 20 50 61 67 65 72 *. ** Pager
17908 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 20 object
17909 20 20 20 20 20 20 20 20 20 20 20 28 73 69 7a 65 (size
1790a 6f 66 28 50 61 67 65 72 29 20 62 79 74 65 73 29 of(Pager) bytes)
1790b 0a 20 20 2a 2a 20 20 20 20 20 50 43 61 63 68 65 . ** PCache
1790c 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 20 object
1790d 20 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 (sqlit
1790e 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 20 62 e3PcacheSize() b
1790f 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 ytes). ** D
17910 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 6e atabase file han
17911 64 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 28 dle (
17912 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 62 pVfs->szOsFile b
17913 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 53 ytes). ** S
17914 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ub-journal file
17915 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 28 handle (
17916 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 journalFileSize
17917 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 bytes). **
17918 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c Main journal fil
17919 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 e handle
1791a 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 (journalFileSize
1791b 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 bytes). **
1791c 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e Database file n
1791d 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ame
1791e 20 28 6e 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 (nPathname+1 by
1791f 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4a 6f tes). ** Jo
17920 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
17921 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6e (n
17922 50 61 74 68 6e 61 6d 65 2b 38 2b 31 20 62 79 74 Pathname+8+1 byt
17923 65 73 29 0a 20 20 2a 2f 0a 20 20 70 50 74 72 20 es). */. pPtr
17924 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 4d = (u8 *)sqlite3M
17925 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 20 52 allocZero(. R
17926 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 OUND8(sizeof(*pP
17927 61 67 65 72 29 29 20 2b 20 20 20 20 20 20 2f 2a ager)) + /*
17928 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 Pager structure
17929 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 28 70 */. ROUND8(p
1792a 63 61 63 68 65 53 69 7a 65 29 20 2b 20 20 20 20 cacheSize) +
1792b 20 20 20 20 20 20 20 2f 2a 20 50 43 61 63 68 65 /* PCache
1792c 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 52 object */. R
1792d 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 OUND8(pVfs->szOs
1792e 46 69 6c 65 29 20 2b 20 20 20 20 20 20 20 2f 2a File) + /*
1792f 20 54 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c The main db fil
17930 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 6c e */. journal
17931 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b 20 20 FileSize * 2 +
17932 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
17933 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 wo journal files
17934 20 2a 2f 20 0a 20 20 20 20 6e 50 61 74 68 6e 61 */ . nPathna
17935 6d 65 20 2b 20 31 20 2b 20 6e 55 72 69 20 2b 20 me + 1 + nUri +
17936 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 /* zFile
17937 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6e 50 61 74 name */. nPat
17938 68 6e 61 6d 65 20 2b 20 38 20 2b 20 32 20 20 20 hname + 8 + 2
17939 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4a /* zJ
1793a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 69 66 6e 64 65 ournal */.#ifnde
1793b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 f SQLITE_OMIT_WA
1793c 4c 0a 20 20 20 20 2b 20 6e 50 61 74 68 6e 61 6d L. + nPathnam
1793d 65 20 2b 20 34 20 2b 20 32 20 20 20 20 20 20 20 e + 4 + 2
1793e 20 20 20 20 20 2f 2a 20 7a 57 61 6c 20 2a 2f 0a /* zWal */.
1793f 23 65 6e 64 69 66 0a 20 20 29 3b 0a 20 20 61 73 #endif. );. as
17940 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 sert( EIGHT_BYTE
17941 5f 41 4c 49 47 4e 4d 45 4e 54 28 53 51 4c 49 54 _ALIGNMENT(SQLIT
17942 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6a 6f 75 E_INT_TO_PTR(jou
17943 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 29 20 29 rnalFileSize)) )
17944 3b 0a 20 20 69 66 28 20 21 70 50 74 72 20 29 7b ;. if( !pPtr ){
17945 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
17946 65 65 28 30 2c 20 7a 50 61 74 68 6e 61 6d 65 29 ee(0, zPathname)
17947 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
17948 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 ITE_NOMEM;. }.
17949 20 70 50 61 67 65 72 20 3d 20 20 20 20 20 20 20 pPager =
1794a 20 20 20 20 20 20 20 28 50 61 67 65 72 2a 29 28 (Pager*)(
1794b 70 50 74 72 29 3b 0a 20 20 70 50 61 67 65 72 2d pPtr);. pPager-
1794c 3e 70 50 43 61 63 68 65 20 3d 20 20 20 20 28 50 >pPCache = (P
1794d 43 61 63 68 65 2a 29 28 70 50 74 72 20 2b 3d 20 Cache*)(pPtr +=
1794e 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 ROUND8(sizeof(*p
1794f 50 61 67 65 72 29 29 29 3b 0a 20 20 70 50 61 67 Pager)));. pPag
17950 65 72 2d 3e 66 64 20 3d 20 20 20 28 73 71 6c 69 er->fd = (sqli
17951 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 te3_file*)(pPtr
17952 2b 3d 20 52 4f 55 4e 44 38 28 70 63 61 63 68 65 += ROUND8(pcache
17953 53 69 7a 65 29 29 3b 0a 20 20 70 50 61 67 65 72 Size));. pPager
17954 2d 3e 73 6a 66 64 20 3d 20 28 73 71 6c 69 74 65 ->sjfd = (sqlite
17955 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 3_file*)(pPtr +=
17956 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a ROUND8(pVfs->sz
17957 4f 73 46 69 6c 65 29 29 3b 0a 20 20 70 50 61 67 OsFile));. pPag
17958 65 72 2d 3e 6a 66 64 20 3d 20 20 28 73 71 6c 69 er->jfd = (sqli
17959 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 te3_file*)(pPtr
1795a 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 += journalFileSi
1795b 7a 65 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a ze);. pPager->z
1795c 46 69 6c 65 6e 61 6d 65 20 3d 20 20 20 20 28 63 Filename = (c
1795d 68 61 72 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f har*)(pPtr += jo
1795e 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a urnalFileSize);.
1795f 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f assert( EIGHT_
17960 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 BYTE_ALIGNMENT(p
17961 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a Pager->jfd) );..
17962 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 /* Fill in the
17963 20 50 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d 65 Pager.zFilename
17964 20 61 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75 72 and Pager.zJour
17965 6e 61 6c 20 62 75 66 66 65 72 73 2c 20 69 66 20 nal buffers, if
17966 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 required. */. i
17967 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b 0a f( zPathname ){.
17968 20 20 20 20 61 73 73 65 72 74 28 20 6e 50 61 74 assert( nPat
17969 68 6e 61 6d 65 3e 30 20 29 3b 0a 20 20 20 20 70 hname>0 );. p
1796a 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 Pager->zJournal
1796b 3d 20 20 20 28 63 68 61 72 2a 29 28 70 50 74 72 = (char*)(pPtr
1796c 20 2b 3d 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 += nPathname +
1796d 31 20 2b 20 6e 55 72 69 29 3b 0a 20 20 20 20 6d 1 + nUri);. m
1796e 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a 46 emcpy(pPager->zF
1796f 69 6c 65 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 ilename, zPathna
17970 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a me, nPathname);.
17971 20 20 20 20 69 66 28 20 6e 55 72 69 20 29 20 6d if( nUri ) m
17972 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 7a emcpy(&pPager->z
17973 46 69 6c 65 6e 61 6d 65 5b 6e 50 61 74 68 6e 61 Filename[nPathna
17974 6d 65 2b 31 5d 2c 20 7a 55 72 69 2c 20 6e 55 72 me+1], zUri, nUr
17975 69 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 i);. memcpy(p
17976 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c Pager->zJournal,
17977 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 zPathname, nPat
17978 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63 hname);. memc
17979 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 py(&pPager->zJou
1797a 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 5d 2c rnal[nPathname],
1797b 20 22 2d 6a 6f 75 72 6e 61 6c 5c 30 30 30 22 2c "-journal\000",
1797c 20 38 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 8+2);. sqlit
1797d 65 33 46 69 6c 65 53 75 66 66 69 78 33 28 70 50 e3FileSuffix3(pP
1797e 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c ager->zFilename,
1797f 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 pPager->zJourna
17980 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 l);.#ifndef SQLI
17981 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20 20 20 20 TE_OMIT_WAL.
17982 70 50 61 67 65 72 2d 3e 7a 57 61 6c 20 3d 20 26 pPager->zWal = &
17983 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
17984 5b 6e 50 61 74 68 6e 61 6d 65 2b 38 2b 31 5d 3b [nPathname+8+1];
17985 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 . memcpy(pPag
17986 65 72 2d 3e 7a 57 61 6c 2c 20 7a 50 61 74 68 6e er->zWal, zPathn
17987 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b ame, nPathname);
17988 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 . memcpy(&pPa
17989 67 65 72 2d 3e 7a 57 61 6c 5b 6e 50 61 74 68 6e ger->zWal[nPathn
1798a 61 6d 65 5d 2c 20 22 2d 77 61 6c 5c 30 30 30 22 ame], "-wal\000"
1798b 2c 20 34 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 , 4+1);. sqli
1798c 74 65 33 46 69 6c 65 53 75 66 66 69 78 33 28 70 te3FileSuffix3(p
1798d 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 Pager->zFilename
1798e 2c 20 70 50 61 67 65 72 2d 3e 7a 57 61 6c 29 3b , pPager->zWal);
1798f 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 .#endif. sqli
17990 74 65 33 44 62 46 72 65 65 28 30 2c 20 7a 50 61 te3DbFree(0, zPa
17991 74 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 thname);. }. p
17992 50 61 67 65 72 2d 3e 70 56 66 73 20 3d 20 70 56 Pager->pVfs = pV
17993 66 73 3b 0a 20 20 70 50 61 67 65 72 2d 3e 76 66 fs;. pPager->vf
17994 73 46 6c 61 67 73 20 3d 20 76 66 73 46 6c 61 67 sFlags = vfsFlag
17995 73 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 s;.. /* Open th
17996 65 20 70 61 67 65 72 20 66 69 6c 65 2e 0a 20 20 e pager file..
17997 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 */. if( zFilena
17998 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b me && zFilename[
17999 30 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 6f 0] ){. int fo
1799a 75 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ut = 0;
1799b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46 /* VF
1799c 53 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65 64 S flags returned
1799d 20 62 79 20 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 by xOpen() */.
1799e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1799f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 sOpen(pVfs, pPag
179a0 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 er->zFilename, p
179a1 50 61 67 65 72 2d 3e 66 64 2c 20 76 66 73 46 6c Pager->fd, vfsFl
179a2 61 67 73 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20 ags, &fout);.
179a3 20 61 73 73 65 72 74 28 20 21 6d 65 6d 44 62 20 assert( !memDb
179a4 29 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 );. readOnly
179a5 3d 20 28 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f = (fout&SQLITE_O
179a6 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 0a PEN_READONLY);..
179a7 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 /* If the fi
179a8 6c 65 20 77 61 73 20 73 75 63 63 65 73 73 66 75 le was successfu
179a9 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 lly opened for r
179aa 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 ead/write access
179ab 2c 0a 20 20 20 20 2a 2a 20 63 68 6f 6f 73 65 20 ,. ** choose
179ac 61 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 a default page s
179ad 69 7a 65 20 69 6e 20 63 61 73 65 20 77 65 20 68 ize in case we h
179ae 61 76 65 20 74 6f 20 63 72 65 61 74 65 20 74 68 ave to create th
179af 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 e. ** databas
179b0 65 20 66 69 6c 65 2e 20 54 68 65 20 64 65 66 61 e file. The defa
179b1 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 69 73 ult page size is
179b2 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 3a the maximum of:
179b3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 . **. **
179b4 20 20 2b 20 53 51 4c 49 54 45 5f 44 45 46 41 55 + SQLITE_DEFAU
179b5 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 LT_PAGE_SIZE,.
179b6 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 61 ** + The va
179b7 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
179b8 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 sqlite3OsSectorS
179b9 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 20 20 20 ize(). **
179ba 2b 20 54 68 65 20 6c 61 72 67 65 73 74 20 70 61 + The largest pa
179bb 67 65 20 73 69 7a 65 20 74 68 61 74 20 63 61 6e ge size that can
179bc 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d be written atom
179bd 69 63 61 6c 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 ically.. */.
179be 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
179bf 45 5f 4f 4b 20 26 26 20 21 72 65 61 64 4f 6e 6c E_OK && !readOnl
179c0 79 20 29 7b 0a 20 20 20 20 20 20 73 65 74 53 65 y ){. setSe
179c1 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 ctorSize(pPager)
179c2 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 53 ;. assert(S
179c3 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 QLITE_DEFAULT_PA
179c4 47 45 5f 53 49 5a 45 3c 3d 53 51 4c 49 54 45 5f GE_SIZE<=SQLITE_
179c5 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 MAX_DEFAULT_PAGE
179c6 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 69 66 _SIZE);. if
179c7 28 20 73 7a 50 61 67 65 44 66 6c 74 3c 70 50 61 ( szPageDflt<pPa
179c8 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 ger->sectorSize
179c9 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
179ca 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a Pager->sectorSiz
179cb 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 e>SQLITE_MAX_DEF
179cc 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 29 AULT_PAGE_SIZE )
179cd 7b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 {. szPa
179ce 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f geDflt = SQLITE_
179cf 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 MAX_DEFAULT_PAGE
179d0 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 7d _SIZE;. }
179d1 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
179d2 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 28 75 33 szPageDflt = (u3
179d3 32 29 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 2)pPager->sector
179d4 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a Size;. }.
179d5 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 }.#ifdef S
179d6 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f QLITE_ENABLE_ATO
179d7 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20 20 MIC_WRITE.
179d8 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 {. int iD
179d9 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 c = sqlite3OsDev
179da 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
179db 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a cs(pPager->fd);.
179dc 20 20 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a int ii;.
179dd 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 53 assert(S
179de 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
179df 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 IC512==(512>>8))
179e0 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
179e1 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 (SQLITE_IOCAP_AT
179e2 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e OMIC64K==(65536>
179e3 3e 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73 >8));. as
179e4 73 65 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f sert(SQLITE_MAX_
179e5 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
179e6 45 3c 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 20 E<=65536);.
179e7 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67 65 for(ii=szPage
179e8 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54 45 Dflt; ii<=SQLITE
179e9 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 _MAX_DEFAULT_PAG
179ea 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29 E_SIZE; ii=ii*2)
179eb 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
179ec 69 44 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 iDc&(SQLITE_IOCA
179ed 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e 38 29 P_ATOMIC|(ii>>8)
179ee 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
179ef 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 69 69 szPageDflt = ii
179f0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
179f1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
179f2 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d #endif. }. }
179f3 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 else{. /* If
179f4 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
179f5 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 is requested, i
179f6 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 t is not opened
179f7 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 immediately..
179f8 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 ** In this case
179f9 20 77 65 20 61 63 63 65 70 74 20 74 68 65 20 64 we accept the d
179fa 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 efault page size
179fb 20 61 6e 64 20 64 65 6c 61 79 20 61 63 74 75 61 and delay actua
179fc 6c 6c 79 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 lly. ** openi
179fd 6e 67 20 74 68 65 20 66 69 6c 65 20 75 6e 74 69 ng the file unti
179fe 6c 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c l the first call
179ff 20 74 6f 20 4f 73 57 72 69 74 65 28 29 2e 0a 20 to OsWrite()..
17a00 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 **. ** Thi
17a01 73 20 62 72 61 6e 63 68 20 69 73 20 61 6c 73 6f s branch is also
17a02 20 72 75 6e 20 66 6f 72 20 61 6e 20 69 6e 2d 6d run for an in-m
17a03 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e 20 emory database.
17a04 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 20 An in-memory.
17a05 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 ** database is
17a06 74 68 65 20 73 61 6d 65 20 61 73 20 61 20 74 65 the same as a te
17a07 6d 70 2d 66 69 6c 65 20 74 68 61 74 20 69 73 20 mp-file that is
17a08 6e 65 76 65 72 20 77 72 69 74 74 65 6e 20 6f 75 never written ou
17a09 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 73 6b t to. ** disk
17a0a 20 61 6e 64 20 75 73 65 73 20 61 6e 20 69 6e 2d and uses an in-
17a0b 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 memory rollback
17a0c 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 20 journal.. */
17a0d 0a 20 20 20 20 74 65 6d 70 46 69 6c 65 20 3d 20 . tempFile =
17a0e 31 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 1;. pPager->e
17a0f 53 74 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45 State = PAGER_RE
17a10 41 44 45 52 3b 0a 20 20 20 20 70 50 61 67 65 72 ADER;. pPager
17a11 2d 3e 65 4c 6f 63 6b 20 3d 20 45 58 43 4c 55 53 ->eLock = EXCLUS
17a12 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 IVE_LOCK;. re
17a13 61 64 4f 6e 6c 79 20 3d 20 28 76 66 73 46 6c 61 adOnly = (vfsFla
17a14 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 gs&SQLITE_OPEN_R
17a15 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 7d 0a 0a 20 EADONLY);. }..
17a16 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e /* The followin
17a17 67 20 63 61 6c 6c 20 74 6f 20 50 61 67 65 72 53 g call to PagerS
17a18 65 74 50 61 67 65 73 69 7a 65 28 29 20 73 65 72 etPagesize() ser
17a19 76 65 73 20 74 6f 20 73 65 74 20 74 68 65 20 76 ves to set the v
17a1a 61 6c 75 65 20 6f 66 20 0a 20 20 2a 2a 20 50 61 alue of . ** Pa
17a1b 67 65 72 2e 70 61 67 65 53 69 7a 65 20 61 6e 64 ger.pageSize and
17a1c 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 to allocate the
17a1d 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 Pager.pTmpSpace
17a1e 20 62 75 66 66 65 72 2e 0a 20 20 2a 2f 0a 20 20 buffer.. */.
17a1f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
17a20 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 K ){. assert(
17a21 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d pPager->memDb==
17a22 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 0 );. rc = sq
17a23 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 lite3PagerSetPag
17a24 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26 73 esize(pPager, &s
17a25 7a 50 61 67 65 44 66 6c 74 2c 20 2d 31 29 3b 0a zPageDflt, -1);.
17a26 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 testcase( rc
17a27 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 !=SQLITE_OK );.
17a28 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 }.. /* If an e
17a29 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 69 6e rror occurred in
17a2a 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 62 either of the b
17a2b 6c 6f 63 6b 73 20 61 62 6f 76 65 2c 20 66 72 65 locks above, fre
17a2c 65 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 e the . ** Page
17a2d 72 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 r structure and
17a2e 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a close the file..
17a2f 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 */. if( rc!=S
17a30 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17a31 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d assert( !pPager-
17a32 3e 70 54 6d 70 53 70 61 63 65 20 29 3b 0a 20 20 >pTmpSpace );.
17a33 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
17a34 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 (pPager->fd);.
17a35 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
17a36 50 61 67 65 72 29 3b 0a 20 20 20 20 72 65 74 75 Pager);. retu
17a37 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rn rc;. }.. /*
17a38 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 Initialize the
17a39 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 2a PCache object. *
17a3a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e 45 78 74 /. assert( nExt
17a3b 72 61 3c 31 30 30 30 20 29 3b 0a 20 20 6e 45 78 ra<1000 );. nEx
17a3c 74 72 61 20 3d 20 52 4f 55 4e 44 38 28 6e 45 78 tra = ROUND8(nEx
17a3d 74 72 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 tra);. sqlite3P
17a3e 63 61 63 68 65 4f 70 65 6e 28 73 7a 50 61 67 65 cacheOpen(szPage
17a3f 44 66 6c 74 2c 20 6e 45 78 74 72 61 2c 20 21 6d Dflt, nExtra, !m
17a40 65 6d 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 emDb,.
17a41 20 20 20 20 20 20 20 20 20 20 21 6d 65 6d 44 62 !memDb
17a42 3f 70 61 67 65 72 53 74 72 65 73 73 3a 30 2c 20 ?pagerStress:0,
17a43 28 76 6f 69 64 20 2a 29 70 50 61 67 65 72 2c 20 (void *)pPager,
17a44 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
17a45 3b 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 ;.. PAGERTRACE(
17a46 28 22 4f 50 45 4e 20 25 64 20 25 73 5c 6e 22 2c ("OPEN %d %s\n",
17a47 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 70 50 FILEHANDLEID(pP
17a48 61 67 65 72 2d 3e 66 64 29 2c 20 70 50 61 67 65 ager->fd), pPage
17a49 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 3b 0a r->zFilename));.
17a4a 20 20 49 4f 54 52 41 43 45 28 28 22 4f 50 45 4e IOTRACE(("OPEN
17a4b 20 25 70 20 25 73 5c 6e 22 2c 20 70 50 61 67 65 %p %s\n", pPage
17a4c 72 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 r, pPager->zFile
17a4d 6e 61 6d 65 29 29 0a 0a 20 20 70 50 61 67 65 72 name)).. pPager
17a4e 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 ->useJournal = (
17a4f 75 38 29 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 u8)useJournal;.
17a50 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 /* pPager->stmt
17a51 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f Open = 0; */. /
17a52 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e * pPager->stmtIn
17a53 55 73 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a Use = 0; */. /*
17a54 20 70 50 61 67 65 72 2d 3e 6e 52 65 66 20 3d 20 pPager->nRef =
17a55 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 0; */. /* pPage
17a56 72 2d 3e 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b r->stmtSize = 0;
17a57 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d */. /* pPager-
17a58 3e 73 74 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20 >stmtJSize = 0;
17a59 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e */. /* pPager->
17a5a 6e 50 61 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 nPage = 0; */.
17a5b 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d pPager->mxPgno =
17a5c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 SQLITE_MAX_PAGE
17a5d 5f 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 _COUNT;. /* pPa
17a5e 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 ger->state = PAG
17a5f 45 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a 23 69 ER_UNLOCK; */.#i
17a60 66 20 30 0a 20 20 61 73 73 65 72 74 28 20 70 50 f 0. assert( pP
17a61 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d 20 28 ager->state == (
17a62 74 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 45 52 tempFile ? PAGER
17a63 5f 45 58 43 4c 55 53 49 56 45 20 3a 20 50 41 47 _EXCLUSIVE : PAG
17a64 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a 23 65 ER_UNLOCK) );.#e
17a65 6e 64 69 66 0a 20 20 2f 2a 20 70 50 61 67 65 72 ndif. /* pPager
17a66 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b 20 2a ->errMask = 0; *
17a67 2f 0a 20 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 /. pPager->temp
17a68 46 69 6c 65 20 3d 20 28 75 38 29 74 65 6d 70 46 File = (u8)tempF
17a69 69 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 74 ile;. assert( t
17a6a 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c empFile==PAGER_L
17a6b 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 OCKINGMODE_NORMA
17a6c 4c 20 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 L . ||
17a6d 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f tempFile==PAGER_
17a6e 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c LOCKINGMODE_EXCL
17a6f 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 USIVE );. asser
17a70 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 t( PAGER_LOCKING
17a71 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3d 3d MODE_EXCLUSIVE==
17a72 31 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 1 );. pPager->e
17a73 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 xclusiveMode = (
17a74 75 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20 u8)tempFile; .
17a75 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
17a76 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 72 untDone = pPager
17a77 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 ->tempFile;. pP
17a78 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 28 75 ager->memDb = (u
17a79 38 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61 67 65 8)memDb;. pPage
17a7a 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 28 75 r->readOnly = (u
17a7b 38 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 61 73 8)readOnly;. as
17a7c 73 65 72 74 28 20 75 73 65 4a 6f 75 72 6e 61 6c sert( useJournal
17a7d 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 || pPager->temp
17a7e 46 69 6c 65 20 29 3b 0a 20 20 70 50 61 67 65 72 File );. pPager
17a7f 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 50 61 67 65 ->noSync = pPage
17a80 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 69 r->tempFile;. i
17a81 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e f( pPager->noSyn
17a82 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 c ){. assert(
17a83 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e pPager->fullSyn
17a84 63 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 c==0 );. asse
17a85 72 74 28 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 rt( pPager->sync
17a86 46 6c 61 67 73 3d 3d 30 20 29 3b 0a 20 20 20 20 Flags==0 );.
17a87 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
17a88 77 61 6c 53 79 6e 63 46 6c 61 67 73 3d 3d 30 20 walSyncFlags==0
17a89 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
17a8a 50 61 67 65 72 2d 3e 63 6b 70 74 53 79 6e 63 46 Pager->ckptSyncF
17a8b 6c 61 67 73 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c lags==0 );. }el
17a8c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e se{. pPager->
17a8d 66 75 6c 6c 53 79 6e 63 20 3d 20 31 3b 0a 20 20 fullSync = 1;.
17a8e 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c pPager->syncFl
17a8f 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e ags = SQLITE_SYN
17a90 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 20 20 70 50 C_NORMAL;. pP
17a91 61 67 65 72 2d 3e 77 61 6c 53 79 6e 63 46 6c 61 ager->walSyncFla
17a92 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 gs = SQLITE_SYNC
17a93 5f 4e 4f 52 4d 41 4c 20 7c 20 57 41 4c 5f 53 59 _NORMAL | WAL_SY
17a94 4e 43 5f 54 52 41 4e 53 41 43 54 49 4f 4e 53 3b NC_TRANSACTIONS;
17a95 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 6b 70 . pPager->ckp
17a96 74 53 79 6e 63 46 6c 61 67 73 20 3d 20 53 51 4c tSyncFlags = SQL
17a97 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 3b ITE_SYNC_NORMAL;
17a98 0a 20 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72 . }. /* pPager
17a99 2d 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f ->pFirst = 0; */
17a9a 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 . /* pPager->pF
17a9b 69 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b 20 irstSynced = 0;
17a9c 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e */. /* pPager->
17a9d 70 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 pLast = 0; */.
17a9e 70 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d pPager->nExtra =
17a9f 20 28 75 31 36 29 6e 45 78 74 72 61 3b 0a 20 20 (u16)nExtra;.
17aa0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 pPager->journalS
17aa1 69 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 izeLimit = SQLIT
17aa2 45 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 E_DEFAULT_JOURNA
17aa3 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20 L_SIZE_LIMIT;.
17aa4 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
17aa5 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65 Pager->fd) || te
17aa6 6d 70 46 69 6c 65 20 29 3b 0a 20 20 73 65 74 53 mpFile );. setS
17aa7 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 ectorSize(pPager
17aa8 29 3b 0a 20 20 69 66 28 20 21 75 73 65 4a 6f 75 );. if( !useJou
17aa9 72 6e 61 6c 20 29 7b 0a 20 20 20 20 70 50 61 67 rnal ){. pPag
17aaa 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 er->journalMode
17aab 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d = PAGER_JOURNALM
17aac 4f 44 45 5f 4f 46 46 3b 0a 20 20 7d 65 6c 73 65 ODE_OFF;. }else
17aad 20 69 66 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20 if( memDb ){.
17aae 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
17aaf 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f lMode = PAGER_JO
17ab0 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 URNALMODE_MEMORY
17ab1 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 ;. }. /* pPage
17ab2 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 20 r->xBusyHandler
17ab3 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 = 0; */. /* pPa
17ab4 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 ger->pBusyHandle
17ab5 72 41 72 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 rArg = 0; */. p
17ab6 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 Pager->xReiniter
17ab7 20 3d 20 78 52 65 69 6e 69 74 3b 0a 20 20 2f 2a = xReinit;. /*
17ab8 20 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e memset(pPager->
17ab9 61 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 aHash, 0, sizeof
17aba 28 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29 (pPager->aHash))
17abb 3b 20 2a 2f 0a 0a 20 20 2a 70 70 50 61 67 65 72 ; */.. *ppPager
17abc 20 3d 20 70 50 61 67 65 72 3b 0a 20 20 72 65 74 = pPager;. ret
17abd 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
17abe 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 ..../*.** This f
17abf 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
17ac0 64 20 61 66 74 65 72 20 74 72 61 6e 73 69 74 69 d after transiti
17ac1 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 41 47 45 52 oning from PAGER
17ac2 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41 _UNLOCK to.** PA
17ac3 47 45 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 GER_SHARED state
17ac4 2e 20 49 74 20 74 65 73 74 73 20 69 66 20 74 68 . It tests if th
17ac5 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75 ere is a hot jou
17ac6 72 6e 61 6c 20 70 72 65 73 65 6e 74 20 69 6e 0a rnal present in.
17ac7 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 ** the file-syst
17ac8 65 6d 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e em for the given
17ac9 20 70 61 67 65 72 2e 20 41 20 68 6f 74 20 6a 6f pager. A hot jo
17aca 75 72 6e 61 6c 20 69 73 20 6f 6e 65 20 74 68 61 urnal is one tha
17acb 74 20 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 t .** needs to b
17acc 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 41 e played back. A
17acd 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 ccording to this
17ace 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 68 6f 74 function, a hot
17acf 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 -journal.** file
17ad0 20 65 78 69 73 74 73 20 69 66 20 74 68 65 20 66 exists if the f
17ad1 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69 ollowing criteri
17ad2 61 20 61 72 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a a are met:.**.**
17ad3 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c * The journal
17ad4 20 66 69 6c 65 20 65 78 69 73 74 73 20 69 6e 20 file exists in
17ad5 74 68 65 20 66 69 6c 65 20 73 79 73 74 65 6d 2c the file system,
17ad6 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70 and.** * No p
17ad7 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 61 20 52 rocess holds a R
17ad8 45 53 45 52 56 45 44 20 6f 72 20 67 72 65 61 74 ESERVED or great
17ad9 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 er lock on the d
17ada 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 61 6e atabase file, an
17adb 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 d.** * The dat
17adc 61 62 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c abase file itsel
17add 66 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 f is greater tha
17ade 6e 20 30 20 62 79 74 65 73 20 69 6e 20 73 69 7a n 0 bytes in siz
17adf 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 e, and.** * Th
17ae0 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 e first byte of
17ae1 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
17ae2 20 65 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e exists and is n
17ae3 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49 ot 0x00..**.** I
17ae4 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 f the current si
17ae5 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
17ae6 73 65 20 66 69 6c 65 20 69 73 20 30 20 62 75 74 se file is 0 but
17ae7 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a a journal file.
17ae8 2a 2a 20 65 78 69 73 74 73 2c 20 74 68 61 74 20 ** exists, that
17ae9 69 73 20 70 72 6f 62 61 62 6c 79 20 61 6e 20 6f is probably an o
17aea 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 ld journal left
17aeb 6f 76 65 72 20 66 72 6f 6d 20 61 20 70 72 69 6f over from a prio
17aec 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69 r.** database wi
17aed 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 th the same name
17aee 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
17aef 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
17af0 69 73 0a 2a 2a 20 6a 75 73 74 20 64 65 6c 65 74 is.** just delet
17af1 65 64 20 75 73 69 6e 67 20 4f 73 44 65 6c 65 74 ed using OsDelet
17af2 65 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 e, *pExists is s
17af3 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 et to 0 and SQLI
17af4 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 TE_OK.** is retu
17af5 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rned..**.** This
17af6 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f routine does no
17af7 74 20 63 68 65 63 6b 20 69 66 20 74 68 65 72 65 t check if there
17af8 20 69 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 is a master jou
17af9 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a rnal filename.**
17afa 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
17afb 68 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 he file. If ther
17afc 65 20 69 73 2c 20 61 6e 64 20 74 68 61 74 20 6d e is, and that m
17afd 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
17afe 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 le.** does not e
17aff 78 69 73 74 2c 20 74 68 65 6e 20 74 68 65 20 6a xist, then the j
17b00 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e ournal file is n
17b01 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 2e 20 49 ot really hot. I
17b02 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 n this.** case t
17b03 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
17b04 20 72 65 74 75 72 6e 20 61 20 66 61 6c 73 65 2d return a false-
17b05 70 6f 73 69 74 69 76 65 2e 20 54 68 65 20 70 61 positive. The pa
17b06 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 0a 2a ger_playback().*
17b07 2a 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 * routine will d
17b08 69 73 63 6f 76 65 72 20 74 68 61 74 20 74 68 65 iscover that the
17b09 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
17b0a 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 20 not really hot
17b0b 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 and .** will not
17b0c 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 0a roll it back. .
17b0d 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 6f 74 2d 6a **.** If a hot-j
17b0e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 66 ournal file is f
17b0f 6f 75 6e 64 20 74 6f 20 65 78 69 73 74 2c 20 2a ound to exist, *
17b10 70 45 78 69 73 74 73 20 69 73 20 73 65 74 20 74 pExists is set t
17b11 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 53 51 4c 49 o 1 and .** SQLI
17b12 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 TE_OK returned.
17b13 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 If no hot-journa
17b14 6c 20 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e l file is presen
17b15 74 2c 20 2a 70 45 78 69 73 74 73 20 69 73 0a 2a t, *pExists is.*
17b16 2a 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 * set to 0 and S
17b17 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 QLITE_OK returne
17b18 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f d. If an IO erro
17b19 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 r occurs while t
17b1a 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 64 65 74 65 rying.** to dete
17b1b 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 rmine whether or
17b1c 20 6e 6f 74 20 61 20 68 6f 74 2d 6a 6f 75 72 6e not a hot-journ
17b1d 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 al file exists,
17b1e 74 68 65 20 49 4f 20 65 72 72 6f 72 0a 2a 2a 20 the IO error.**
17b1f 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
17b20 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f and the value o
17b21 66 20 2a 70 45 78 69 73 74 73 20 69 73 20 75 6e f *pExists is un
17b22 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 defined..*/.stat
17b23 69 63 20 69 6e 74 20 68 61 73 48 6f 74 4a 6f 75 ic int hasHotJou
17b24 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 rnal(Pager *pPag
17b25 65 72 2c 20 69 6e 74 20 2a 70 45 78 69 73 74 73 er, int *pExists
17b26 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 ){. sqlite3_vfs
17b27 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 * const pVfs =
17b28 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 pPager->pVfs;.
17b29 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
17b2a 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a OK; /*
17b2b 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
17b2c 20 20 69 6e 74 20 65 78 69 73 74 73 20 3d 20 31 int exists = 1
17b2d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
17b2e 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6a 6f 75 /* True if a jou
17b2f 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65 rnal file is pre
17b30 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6a 72 sent */. int jr
17b31 6e 6c 4f 70 65 6e 20 3d 20 21 21 69 73 4f 70 65 nlOpen = !!isOpe
17b32 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a n(pPager->jfd);.
17b33 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
17b34 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b r->useJournal );
17b35 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 . assert( isOpe
17b36 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b n(pPager->fd) );
17b37 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
17b38 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 r->eState==PAGER
17b39 5f 4f 50 45 4e 20 29 3b 0a 0a 20 20 61 73 73 65 _OPEN );.. asse
17b3a 72 74 28 20 6a 72 6e 6c 4f 70 65 6e 3d 3d 30 20 rt( jrnlOpen==0
17b3b 7c 7c 20 28 20 73 71 6c 69 74 65 33 4f 73 44 65 || ( sqlite3OsDe
17b3c 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
17b3d 69 63 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 ics(pPager->jfd)
17b3e 20 26 0a 20 20 20 20 53 51 4c 49 54 45 5f 49 4f &. SQLITE_IO
17b3f 43 41 50 5f 55 4e 44 45 4c 45 54 41 42 4c 45 5f CAP_UNDELETABLE_
17b40 57 48 45 4e 5f 4f 50 45 4e 0a 20 20 29 29 3b 0a WHEN_OPEN. ));.
17b41 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 30 3b . *pExists = 0;
17b42 0a 20 20 69 66 28 20 21 6a 72 6e 6c 4f 70 65 6e . if( !jrnlOpen
17b43 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
17b44 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 ite3OsAccess(pVf
17b45 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 s, pPager->zJour
17b46 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 nal, SQLITE_ACCE
17b47 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69 73 SS_EXISTS, &exis
17b48 74 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 ts);. }. if( r
17b49 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
17b4a 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 69 6e exists ){. in
17b4b 74 20 6c 6f 63 6b 65 64 20 3d 20 30 3b 20 20 20 t locked = 0;
17b4c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
17b4d 65 20 69 66 20 73 6f 6d 65 20 70 72 6f 63 65 73 e if some proces
17b4e 73 20 68 6f 6c 64 73 20 61 20 52 45 53 45 52 56 s holds a RESERV
17b4f 45 44 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20 ED lock */..
17b50 2f 2a 20 52 61 63 65 20 63 6f 6e 64 69 74 69 6f /* Race conditio
17b51 6e 20 68 65 72 65 3a 20 20 41 6e 6f 74 68 65 72 n here: Another
17b52 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 68 process might h
17b53 61 76 65 20 62 65 65 6e 20 68 6f 6c 64 69 6e 67 ave been holding
17b54 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 the. ** the
17b55 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 61 6e RESERVED lock an
17b56 64 20 68 61 76 65 20 61 20 6a 6f 75 72 6e 61 6c d have a journal
17b57 20 6f 70 65 6e 20 61 74 20 74 68 65 20 73 71 6c open at the sql
17b58 69 74 65 33 4f 73 41 63 63 65 73 73 28 29 20 0a ite3OsAccess() .
17b59 20 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76 ** call abov
17b5a 65 2c 20 62 75 74 20 74 68 65 6e 20 64 65 6c 65 e, but then dele
17b5b 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 te the journal a
17b5c 6e 64 20 64 72 6f 70 20 74 68 65 20 6c 6f 63 6b nd drop the lock
17b5d 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 77 before. ** w
17b5e 65 20 67 65 74 20 74 6f 20 74 68 65 20 66 6f 6c e get to the fol
17b5f 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 lowing sqlite3Os
17b60 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
17b61 6b 28 29 20 63 61 6c 6c 2e 20 20 49 66 20 74 68 k() call. If th
17b62 61 74 0a 20 20 20 20 2a 2a 20 69 73 20 74 68 65 at. ** is the
17b63 20 63 61 73 65 2c 20 74 68 69 73 20 72 6f 75 74 case, this rout
17b64 69 6e 65 20 6d 69 67 68 74 20 74 68 69 6e 6b 20 ine might think
17b65 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a there is a hot j
17b66 6f 75 72 6e 61 6c 20 77 68 65 6e 0a 20 20 20 20 ournal when.
17b67 2a 2a 20 69 6e 20 66 61 63 74 20 74 68 65 72 65 ** in fact there
17b68 20 69 73 20 6e 6f 6e 65 2e 20 20 54 68 69 73 20 is none. This
17b69 72 65 73 75 6c 74 73 20 69 6e 20 61 20 66 61 6c results in a fal
17b6a 73 65 2d 70 6f 73 69 74 69 76 65 20 77 68 69 63 se-positive whic
17b6b 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 h will. ** be
17b6c 20 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 74 dealt with by t
17b6d 68 65 20 70 6c 61 79 62 61 63 6b 20 72 6f 75 74 he playback rout
17b6e 69 6e 65 2e 20 20 54 69 63 6b 65 74 20 23 33 38 ine. Ticket #38
17b6f 38 33 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 83.. */. r
17b70 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 68 65 c = sqlite3OsChe
17b71 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 70 ckReservedLock(p
17b72 50 61 67 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b Pager->fd, &lock
17b73 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ed);. if( rc=
17b74 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c =SQLITE_OK && !l
17b75 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 50 ocked ){. P
17b76 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20 gno nPage;
17b77 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
17b78 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
17b79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
17b7a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 /.. /* Chec
17b7b 6b 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 k the size of th
17b7c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
17b7d 20 49 66 20 69 74 20 63 6f 6e 73 69 73 74 73 20 If it consists
17b7e 6f 66 20 30 20 70 61 67 65 73 2c 0a 20 20 20 20 of 0 pages,.
17b7f 20 20 2a 2a 20 74 68 65 6e 20 64 65 6c 65 74 65 ** then delete
17b80 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
17b81 65 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 e. See the heade
17b82 72 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 r comment above
17b83 66 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 for . ** th
17b84 65 20 72 65 61 73 6f 6e 69 6e 67 20 68 65 72 65 e reasoning here
17b85 2e 20 20 44 65 6c 65 74 65 20 74 68 65 20 6f 62 . Delete the ob
17b86 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 solete journal f
17b87 69 6c 65 20 75 6e 64 65 72 0a 20 20 20 20 20 20 ile under.
17b88 2a 2a 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f ** a RESERVED lo
17b89 63 6b 20 74 6f 20 61 76 6f 69 64 20 72 61 63 65 ck to avoid race
17b8a 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 6e 64 20 conditions and
17b8b 74 6f 20 61 76 6f 69 64 20 76 69 6f 6c 61 74 69 to avoid violati
17b8c 6e 67 0a 20 20 20 20 20 20 2a 2a 20 5b 48 33 33 ng. ** [H33
17b8d 30 32 30 5d 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 020].. */.
17b8e 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50 rc = pagerP
17b8f 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
17b90 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20 &nPage);.
17b91 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
17b92 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 K ){. if(
17b93 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 nPage==0 ){.
17b94 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 sqlite3Be
17b95 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
17b96 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
17b97 20 70 61 67 65 72 4c 6f 63 6b 44 62 28 70 50 61 pagerLockDb(pPa
17b98 67 65 72 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f ger, RESERVED_LO
17b99 43 4b 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 CK)==SQLITE_OK )
17b9a 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 {. sq
17b9b 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 lite3OsDelete(pV
17b9c 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 fs, pPager->zJou
17b9d 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 rnal, 0);.
17b9e 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 if( !pPage
17b9f 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
17ba0 20 29 20 70 61 67 65 72 55 6e 6c 6f 63 6b 44 62 ) pagerUnlockDb
17ba1 28 70 50 61 67 65 72 2c 20 53 48 41 52 45 44 5f (pPager, SHARED_
17ba2 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 20 LOCK);.
17ba3 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c }. sql
17ba4 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
17ba5 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20 7d loc();. }
17ba6 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
17ba7 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 /* The journal f
17ba8 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 6e ile exists and n
17ba9 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 o other connecti
17baa 6f 6e 20 68 61 73 20 61 20 72 65 73 65 72 76 65 on has a reserve
17bab 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f d. ** o
17bac 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f r greater lock o
17bad 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
17bae 69 6c 65 2e 20 4e 6f 77 20 63 68 65 63 6b 20 74 ile. Now check t
17baf 68 61 74 20 74 68 65 72 65 20 69 73 0a 20 20 20 hat there is.
17bb0 20 20 20 20 20 20 20 2a 2a 20 61 74 20 6c 65 61 ** at lea
17bb1 73 74 20 6f 6e 65 20 6e 6f 6e 2d 7a 65 72 6f 20 st one non-zero
17bb2 62 79 74 65 73 20 61 74 20 74 68 65 20 73 74 61 bytes at the sta
17bb3 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 rt of the journa
17bb4 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 l file..
17bb5 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 ** If there is
17bb6 2c 20 74 68 65 6e 20 77 65 20 63 6f 6e 73 69 64 , then we consid
17bb7 65 72 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 er this journal
17bb8 74 6f 20 62 65 20 68 6f 74 2e 20 49 66 20 6e 6f to be hot. If no
17bb9 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a t, . **
17bba 20 69 74 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 it can be ignor
17bbb 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f ed.. */
17bbc 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 . if( !
17bbd 6a 72 6e 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 jrnlOpen ){.
17bbe 20 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20 int f =
17bbf 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
17bc0 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e ONLY|SQLITE_OPEN
17bc1 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 _MAIN_JOURNAL;.
17bc2 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
17bc3 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 sqlite3OsOpen(pV
17bc4 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 fs, pPager->zJou
17bc5 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 rnal, pPager->jf
17bc6 64 2c 20 66 2c 20 26 66 29 3b 0a 20 20 20 20 20 d, f, &f);.
17bc7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
17bc8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
17bc9 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
17bca 20 20 75 38 20 66 69 72 73 74 20 3d 20 30 3b 0a u8 first = 0;.
17bcb 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
17bcc 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 sqlite3OsRead(p
17bcd 50 61 67 65 72 2d 3e 6a 66 64 2c 20 28 76 6f 69 Pager->jfd, (voi
17bce 64 20 2a 29 26 66 69 72 73 74 2c 20 31 2c 20 30 d *)&first, 1, 0
17bcf 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 );. i
17bd0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f f( rc==SQLITE_IO
17bd1 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 ERR_SHORT_READ )
17bd2 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
17bd3 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
17bd4 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
17bd5 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 6a if( !j
17bd6 72 6e 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 20 rnlOpen ){.
17bd7 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
17bd8 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
17bd9 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 jfd);.
17bda 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
17bdb 2a 70 45 78 69 73 74 73 20 3d 20 28 66 69 72 73 *pExists = (firs
17bdc 74 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 20 t!=0);.
17bdd 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 }else if( rc==S
17bde 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 29 QLITE_CANTOPEN )
17bdf 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a {. /*
17be0 20 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 70 If we cannot op
17be1 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 en the rollback
17be2 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 journal file in
17be3 6f 72 64 65 72 20 74 6f 20 73 65 65 20 69 66 0a order to see if.
17be4 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 ** i
17be5 74 73 20 68 61 73 20 61 20 7a 65 72 6f 20 68 65 ts has a zero he
17be6 61 64 65 72 2c 20 74 68 61 74 20 6d 69 67 68 74 ader, that might
17be7 20 62 65 20 64 75 65 20 74 6f 20 61 6e 20 49 2f be due to an I/
17be8 4f 20 65 72 72 6f 72 2c 20 6f 72 0a 20 20 20 20 O error, or.
17be9 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 6d 69 ** it mi
17bea 67 68 74 20 62 65 20 64 75 65 20 74 6f 20 74 68 ght be due to th
17beb 65 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e e race condition
17bec 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 described above
17bed 20 61 6e 64 20 69 6e 0a 20 20 20 20 20 20 20 20 and in.
17bee 20 20 20 20 2a 2a 20 74 69 63 6b 65 74 20 23 33 ** ticket #3
17bef 38 38 33 2e 20 20 45 69 74 68 65 72 20 77 61 79 883. Either way
17bf0 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 , assume that th
17bf1 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68 6f 74 e journal is hot
17bf2 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a .. **
17bf3 20 54 68 69 73 20 6d 69 67 68 74 20 62 65 20 61 This might be a
17bf4 20 66 61 6c 73 65 20 70 6f 73 69 74 69 76 65 2e false positive.
17bf5 20 20 42 75 74 20 69 66 20 69 74 20 69 73 2c 20 But if it is,
17bf6 74 68 65 6e 20 74 68 65 0a 20 20 20 20 20 20 20 then the.
17bf7 20 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 ** automati
17bf8 63 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 79 62 61 c journal playba
17bf9 63 6b 20 61 6e 64 20 72 65 63 6f 76 65 72 79 20 ck and recovery
17bfa 6d 65 63 68 61 6e 69 73 6d 20 77 69 6c 6c 20 64 mechanism will d
17bfb 65 61 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 eal.
17bfc 2a 2a 20 77 69 74 68 20 69 74 20 75 6e 64 65 72 ** with it under
17bfd 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f an EXCLUSIVE lo
17bfe 63 6b 20 77 68 65 72 65 20 77 65 20 64 6f 20 6e ck where we do n
17bff 6f 74 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 ot need to.
17c00 20 20 20 20 20 20 20 2a 2a 20 77 6f 72 72 79 20 ** worry
17c01 73 6f 20 6d 75 63 68 20 77 69 74 68 20 72 61 63 so much with rac
17c02 65 20 63 6f 6e 64 69 74 69 6f 6e 73 2e 0a 20 20 e conditions..
17c03 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 */.
17c04 20 20 20 20 20 20 20 20 20 2a 70 45 78 69 73 74 *pExist
17c05 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 s = 1;.
17c06 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
17c07 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 K;. }.
17c08 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
17c09 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 . }. }.. re
17c0a 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
17c0b 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
17c0c 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 6f 62 74 is called to obt
17c0d 61 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f 63 ain a shared loc
17c0e 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
17c0f 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 74 20 69 73 e file..** It is
17c10 20 69 6c 6c 65 67 61 6c 20 74 6f 20 63 61 6c 6c illegal to call
17c11 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 sqlite3PagerAcq
17c12 75 69 72 65 28 29 20 75 6e 74 69 6c 20 61 66 74 uire() until aft
17c13 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e er this function
17c14 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 73 75 63 .** has been suc
17c15 63 65 73 73 66 75 6c 6c 79 20 63 61 6c 6c 65 64 cessfully called
17c16 2e 20 49 66 20 61 20 73 68 61 72 65 64 2d 6c 6f . If a shared-lo
17c17 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 ck is already he
17c18 6c 64 20 77 68 65 6e 0a 2a 2a 20 74 68 69 73 20 ld when.** this
17c19 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
17c1a 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f ed, it is a no-o
17c1b 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c p..**.** The fol
17c1c 6c 6f 77 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e lowing operation
17c1d 73 20 61 72 65 20 61 6c 73 6f 20 70 65 72 66 6f s are also perfo
17c1e 72 6d 65 64 20 62 79 20 74 68 69 73 20 66 75 6e rmed by this fun
17c1f 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 ction..**.** 1
17c20 29 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 ) If the pager i
17c21 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 50 s currently in P
17c22 41 47 45 52 5f 4f 50 45 4e 20 73 74 61 74 65 20 AGER_OPEN state
17c23 28 6e 6f 20 6c 6f 63 6b 20 68 65 6c 64 0a 2a 2a (no lock held.**
17c24 20 20 20 20 20 20 6f 6e 20 74 68 65 20 64 61 74 on the dat
17c25 61 62 61 73 65 20 66 69 6c 65 29 2c 20 74 68 65 abase file), the
17c26 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 n an attempt is
17c27 6d 61 64 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 made to obtain a
17c28 0a 2a 2a 20 20 20 20 20 20 53 48 41 52 45 44 20 .** SHARED
17c29 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
17c2a 62 61 73 65 20 66 69 6c 65 2e 20 49 6d 6d 65 64 base file. Immed
17c2b 69 61 74 65 6c 79 20 61 66 74 65 72 20 6f 62 74 iately after obt
17c2c 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 aining.** t
17c2d 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 he SHARED lock,
17c2e 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 the file-system
17c2f 69 73 20 63 68 65 63 6b 65 64 20 66 6f 72 20 61 is checked for a
17c30 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 0a 2a 2a hot-journal,.**
17c31 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20 70 which is p
17c32 6c 61 79 65 64 20 62 61 63 6b 20 69 66 20 70 72 layed back if pr
17c33 65 73 65 6e 74 2e 20 46 6f 6c 6c 6f 77 69 6e 67 esent. Following
17c34 20 61 6e 79 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c any hot-journal
17c35 20 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c 62 61 .** rollba
17c36 63 6b 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ck, the contents
17c37 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 61 72 of the cache ar
17c38 65 20 76 61 6c 69 64 61 74 65 64 20 62 79 20 63 e validated by c
17c39 68 65 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 hecking.**
17c3a 74 68 65 20 27 63 68 61 6e 67 65 2d 63 6f 75 6e the 'change-coun
17c3b 74 65 72 27 20 66 69 65 6c 64 20 6f 66 20 74 68 ter' field of th
17c3c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
17c3d 68 65 61 64 65 72 20 61 6e 64 0a 2a 2a 20 20 20 header and.**
17c3e 20 20 20 64 69 73 63 61 72 64 65 64 20 69 66 20 discarded if
17c3f 74 68 65 79 20 61 72 65 20 66 6f 75 6e 64 20 74 they are found t
17c40 6f 20 62 65 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a o be invalid..**
17c41 0a 2a 2a 20 20 20 32 29 20 49 66 20 74 68 65 20 .** 2) If the
17c42 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 pager is running
17c43 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f in exclusive-mo
17c44 64 65 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72 de, and there ar
17c45 65 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 20 e currently.**
17c46 20 20 20 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 no outstandi
17c47 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f ng references to
17c48 20 61 6e 79 20 70 61 67 65 73 2c 20 61 6e 64 20 any pages, and
17c49 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 is in the error
17c4a 73 74 61 74 65 2c 0a 2a 2a 20 20 20 20 20 20 74 state,.** t
17c4b 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 hen an attempt i
17c4c 73 20 6d 61 64 65 20 74 6f 20 63 6c 65 61 72 20 s made to clear
17c4d 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 the error state
17c4e 62 79 20 64 69 73 63 61 72 64 69 6e 67 0a 2a 2a by discarding.**
17c4f 20 20 20 20 20 20 74 68 65 20 63 6f 6e 74 65 6e the conten
17c50 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 ts of the page c
17c51 61 63 68 65 20 61 6e 64 20 72 6f 6c 6c 69 6e 67 ache and rolling
17c52 20 62 61 63 6b 20 61 6e 79 20 6f 70 65 6e 20 6a back any open j
17c53 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 66 ournal.** f
17c54 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 76 ile..**.** If ev
17c55 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 erything is succ
17c56 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f essful, SQLITE_O
17c57 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 K is returned. I
17c58 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a 2a f an IO error .*
17c59 2a 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6c * occurs while l
17c5a 6f 63 6b 69 6e 67 20 74 68 65 20 64 61 74 61 62 ocking the datab
17c5b 61 73 65 2c 20 63 68 65 63 6b 69 6e 67 20 66 6f ase, checking fo
17c5c 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 r a hot-journal
17c5d 66 69 6c 65 20 6f 72 20 0a 2a 2a 20 72 6f 6c 6c file or .** roll
17c5e 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e ing back a journ
17c5f 61 6c 20 66 69 6c 65 2c 20 74 68 65 20 49 4f 20 al file, the IO
17c60 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
17c61 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 turned..*/.SQLIT
17c62 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
17c63 6c 69 74 65 33 50 61 67 65 72 53 68 61 72 65 64 lite3PagerShared
17c64 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 Lock(Pager *pPag
17c65 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 er){. int rc =
17c66 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
17c67 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
17c68 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f urn code */.. /
17c69 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
17c6a 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 s only called fr
17c6b 6f 6d 20 62 2d 74 72 65 65 20 61 6e 64 20 6f 6e om b-tree and on
17c6c 6c 79 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 ly when there ar
17c6d 65 20 6e 6f 0a 20 20 2a 2a 20 6f 75 74 73 74 61 e no. ** outsta
17c6e 6e 64 69 6e 67 20 70 61 67 65 73 2e 20 54 68 69 nding pages. Thi
17c6f 73 20 69 6d 70 6c 69 65 73 20 74 68 61 74 20 74 s implies that t
17c70 68 65 20 70 61 67 65 72 20 73 74 61 74 65 20 73 he pager state s
17c71 68 6f 75 6c 64 20 65 69 74 68 65 72 0a 20 20 2a hould either. *
17c72 2a 20 62 65 20 4f 50 45 4e 20 6f 72 20 52 45 41 * be OPEN or REA
17c73 44 45 52 2e 20 52 45 41 44 45 52 20 69 73 20 6f DER. READER is o
17c74 6e 6c 79 20 70 6f 73 73 69 62 6c 65 20 69 66 20 nly possible if
17c75 74 68 65 20 70 61 67 65 72 20 69 73 20 6f 72 20 the pager is or
17c76 77 61 73 20 69 6e 20 0a 20 20 2a 2a 20 65 78 63 was in . ** exc
17c77 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 6d 6f lusive access mo
17c78 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 de.. */. asser
17c79 74 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 t( sqlite3Pcache
17c7a 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
17c7b 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a >pPCache)==0 );.
17c7c 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 assert( assert
17c7d 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 _pager_state(pPa
17c7e 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 ger) );. assert
17c7f 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 ( pPager->eState
17c80 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 7c 7c 20 ==PAGER_OPEN ||
17c81 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d pPager->eState==
17c82 50 41 47 45 52 5f 52 45 41 44 45 52 20 29 3b 0a PAGER_READER );.
17c83 20 20 69 66 28 20 4e 45 56 45 52 28 4d 45 4d 44 if( NEVER(MEMD
17c84 42 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 B && pPager->err
17c85 43 6f 64 65 29 20 29 7b 20 72 65 74 75 72 6e 20 Code) ){ return
17c86 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b pPager->errCode;
17c87 20 7d 0a 0a 20 20 69 66 28 20 21 70 61 67 65 72 }.. if( !pager
17c88 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 26 UseWal(pPager) &
17c89 26 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 & pPager->eState
17c8a 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 29 7b 0a ==PAGER_OPEN ){.
17c8b 20 20 20 20 69 6e 74 20 62 48 6f 74 4a 6f 75 72 int bHotJour
17c8c 6e 61 6c 20 3d 20 31 3b 20 20 20 20 20 20 20 20 nal = 1;
17c8d 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
17c8e 72 65 20 65 78 69 73 74 73 20 61 20 68 6f 74 20 re exists a hot
17c8f 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 2a 2f 0a journal-file */.
17c90 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 . assert( !ME
17c91 4d 44 42 20 29 3b 0a 0a 20 20 20 20 72 63 20 3d MDB );.. rc =
17c92 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c pager_wait_on_l
17c93 6f 63 6b 28 70 50 61 67 65 72 2c 20 53 48 41 52 ock(pPager, SHAR
17c94 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 ED_LOCK);. if
17c95 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
17c96 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
17c97 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d pPager->eLock==
17c98 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 70 50 61 67 65 NO_LOCK || pPage
17c99 72 2d 3e 65 4c 6f 63 6b 3d 3d 55 4e 4b 4e 4f 57 r->eLock==UNKNOW
17c9a 4e 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 N_LOCK );.
17c9b 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 goto failed;.
17c9c 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 }.. /* If a
17c9d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 journal file exi
17c9e 73 74 73 2c 20 61 6e 64 20 74 68 65 72 65 20 69 sts, and there i
17c9f 73 20 6e 6f 20 52 45 53 45 52 56 45 44 20 6c 6f s no RESERVED lo
17ca0 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a ck on the. **
17ca1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
17ca2 74 68 65 6e 20 69 74 20 65 69 74 68 65 72 20 6e then it either n
17ca3 65 65 64 73 20 74 6f 20 62 65 20 70 6c 61 79 65 eeds to be playe
17ca4 64 20 62 61 63 6b 20 6f 72 20 64 65 6c 65 74 65 d back or delete
17ca5 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 d.. */. if
17ca6 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3c ( pPager->eLock<
17ca7 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a =SHARED_LOCK ){.
17ca8 20 20 20 20 20 20 72 63 20 3d 20 68 61 73 48 6f rc = hasHo
17ca9 74 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c tJournal(pPager,
17caa 20 26 62 48 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a &bHotJournal);.
17cab 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
17cac 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
17cad 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 goto failed
17cae 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
17caf 62 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 bHotJournal ){.
17cb0 20 20 20 20 20 2f 2a 20 47 65 74 20 61 6e 20 45 /* Get an E
17cb1 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e XCLUSIVE lock on
17cb2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
17cb3 6c 65 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e le. At this poin
17cb4 74 20 69 74 20 69 73 0a 20 20 20 20 20 20 2a 2a t it is. **
17cb5 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 important that
17cb6 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
17cb7 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 is not obtained
17cb8 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 74 68 on the way to th
17cb9 65 0a 20 20 20 20 20 20 2a 2a 20 45 58 43 4c 55 e. ** EXCLU
17cba 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74 SIVE lock. If it
17cbb 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 72 20 70 were, another p
17cbc 72 6f 63 65 73 73 20 6d 69 67 68 74 20 6f 70 65 rocess might ope
17cbd 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 n the. ** d
17cbe 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 64 65 atabase file, de
17cbf 74 65 63 74 20 74 68 65 20 52 45 53 45 52 56 45 tect the RESERVE
17cc0 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63 D lock, and conc
17cc1 6c 75 64 65 20 74 68 61 74 20 74 68 65 0a 20 20 lude that the.
17cc2 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
17cc3 69 73 20 73 61 66 65 20 74 6f 20 72 65 61 64 20 is safe to read
17cc4 77 68 69 6c 65 20 74 68 69 73 20 70 72 6f 63 65 while this proce
17cc5 73 73 20 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c ss is still roll
17cc6 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 2a ing the . *
17cc7 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 61 * hot-journal ba
17cc8 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 ck.. ** .
17cc9 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65 20 74 ** Because t
17cca 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 he intermediate
17ccb 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 RESERVED lock is
17ccc 20 6e 6f 74 20 72 65 71 75 65 73 74 65 64 2c 20 not requested,
17ccd 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 any. ** oth
17cce 65 72 20 70 72 6f 63 65 73 73 20 61 74 74 65 6d er process attem
17ccf 70 74 69 6e 67 20 74 6f 20 61 63 63 65 73 73 20 pting to access
17cd0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
17cd1 65 20 77 69 6c 6c 20 67 65 74 20 74 6f 20 0a 20 e will get to .
17cd2 20 20 20 20 20 2a 2a 20 74 68 69 73 20 70 6f 69 ** this poi
17cd3 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 61 nt in the code a
17cd4 6e 64 20 66 61 69 6c 20 74 6f 20 6f 62 74 61 69 nd fail to obtai
17cd5 6e 20 69 74 73 20 6f 77 6e 20 45 58 43 4c 55 53 n its own EXCLUS
17cd6 49 56 45 20 6c 6f 63 6b 20 0a 20 20 20 20 20 20 IVE lock .
17cd7 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ** on the databa
17cd8 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a se file.. *
17cd9 2a 0a 20 20 20 20 20 20 2a 2a 20 55 6e 6c 65 73 *. ** Unles
17cda 73 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 s the pager is i
17cdb 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 n locking_mode=e
17cdc 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 xclusive mode, t
17cdd 68 65 20 6c 6f 63 6b 20 69 73 0a 20 20 20 20 20 he lock is.
17cde 20 2a 2a 20 64 6f 77 6e 67 72 61 64 65 64 20 74 ** downgraded t
17cdf 6f 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 62 65 o SHARED_LOCK be
17ce0 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 fore this functi
17ce1 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 20 20 20 20 on returns..
17ce2 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 */. rc =
17ce3 70 61 67 65 72 4c 6f 63 6b 44 62 28 70 50 61 67 pagerLockDb(pPag
17ce4 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f er, EXCLUSIVE_LO
17ce5 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 CK);. if( r
17ce6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
17ce7 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 goto fai
17ce8 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20 led;. }. .
17ce9 20 20 20 20 20 2f 2a 20 49 66 20 69 74 20 69 73 /* If it is
17cea 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 not already ope
17ceb 6e 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 65 n and the file e
17cec 78 69 73 74 73 20 6f 6e 20 64 69 73 6b 2c 20 6f xists on disk, o
17ced 70 65 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a pen the . *
17cee 2a 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65 * journal for re
17cef 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e ad/write access.
17cf0 20 57 72 69 74 65 20 61 63 63 65 73 73 20 69 73 Write access is
17cf1 20 72 65 71 75 69 72 65 64 20 62 65 63 61 75 73 required becaus
17cf2 65 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 65 e . ** in e
17cf3 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 xclusive-access
17cf4 6d 6f 64 65 20 74 68 65 20 66 69 6c 65 20 64 65 mode the file de
17cf5 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 scriptor will be
17cf6 20 6b 65 70 74 20 6f 70 65 6e 20 0a 20 20 20 20 kept open .
17cf7 20 20 2a 2a 20 61 6e 64 20 70 6f 73 73 69 62 6c ** and possibl
17cf8 79 20 75 73 65 64 20 66 6f 72 20 61 20 74 72 61 y used for a tra
17cf9 6e 73 61 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f nsaction later o
17cfa 6e 2e 20 41 6c 73 6f 2c 20 77 72 69 74 65 2d 61 n. Also, write-a
17cfb 63 63 65 73 73 20 0a 20 20 20 20 20 20 2a 2a 20 ccess . **
17cfc 69 73 20 75 73 75 61 6c 6c 79 20 72 65 71 75 69 is usually requi
17cfd 72 65 64 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 red to finalize
17cfe 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 6e 20 6a the journal in j
17cff 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 70 65 72 73 ournal_mode=pers
17d00 69 73 74 20 0a 20 20 20 20 20 20 2a 2a 20 6d 6f ist . ** mo
17d01 64 65 20 28 61 6e 64 20 61 6c 73 6f 20 66 6f 72 de (and also for
17d02 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 74 72 journal_mode=tr
17d03 75 6e 63 61 74 65 20 6f 6e 20 73 6f 6d 65 20 73 uncate on some s
17d04 79 73 74 65 6d 73 29 2e 0a 20 20 20 20 20 20 2a ystems).. *
17d05 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 *. ** If th
17d06 65 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e e journal does n
17d07 6f 74 20 65 78 69 73 74 2c 20 69 74 20 75 73 75 ot exist, it usu
17d08 61 6c 6c 79 20 6d 65 61 6e 73 20 74 68 61 74 20 ally means that
17d09 73 6f 6d 65 20 0a 20 20 20 20 20 20 2a 2a 20 6f some . ** o
17d0a 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 ther connection
17d0b 6d 61 6e 61 67 65 64 20 74 6f 20 67 65 74 20 69 managed to get i
17d0c 6e 20 61 6e 64 20 72 6f 6c 6c 20 69 74 20 62 61 n and roll it ba
17d0d 63 6b 20 62 65 66 6f 72 65 20 0a 20 20 20 20 20 ck before .
17d0e 20 2a 2a 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 ** this connect
17d0f 69 6f 6e 20 6f 62 74 61 69 6e 65 64 20 74 68 65 ion obtained the
17d10 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
17d11 61 62 6f 76 65 2e 20 4f 72 2c 20 69 74 20 0a 20 above. Or, it .
17d12 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d 65 61 6e ** may mean
17d13 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20 that the pager
17d14 77 61 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 was in the error
17d15 2d 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 -state when this
17d16 0a 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 . ** functi
17d17 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 20 61 6e on was called an
17d18 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 d the journal fi
17d19 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 le does not exis
17d1a 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 t.. */.
17d1b 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 if( !isOpen(pP
17d1c 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 ager->jfd) ){.
17d1d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 sqlite3_vf
17d1e 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d s * const pVfs =
17d1f 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 pPager->pVfs;.
17d20 20 20 20 20 20 20 20 69 6e 74 20 62 45 78 69 73 int bExis
17d21 74 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ts;
17d22 20 2f 2a 20 54 72 75 65 20 69 66 20 6a 6f 75 72 /* True if jour
17d23 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20 nal file exists
17d24 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 */. rc =
17d25 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 sqlite3OsAccess(
17d26 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 56 66 . pVf
17d27 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 s, pPager->zJour
17d28 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 nal, SQLITE_ACCE
17d29 53 53 5f 45 58 49 53 54 53 2c 20 26 62 45 78 69 SS_EXISTS, &bExi
17d2a 73 74 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 sts);. if
17d2b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
17d2c 26 26 20 62 45 78 69 73 74 73 20 29 7b 0a 20 20 && bExists ){.
17d2d 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74 int fout
17d2e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
17d2f 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f int f = SQLITE_O
17d30 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 PEN_READWRITE|SQ
17d31 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
17d32 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 OURNAL;.
17d33 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 assert( !pPage
17d34 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 r->tempFile );.
17d35 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
17d36 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 lite3OsOpen(pVfs
17d37 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e , pPager->zJourn
17d38 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c al, pPager->jfd,
17d39 20 66 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20 20 f, &fout);.
17d3a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
17d3b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 !=SQLITE_OK || i
17d3c 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
17d3d 64 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 d) );.
17d3e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
17d3f 4b 20 26 26 20 66 6f 75 74 26 53 51 4c 49 54 45 K && fout&SQLITE
17d40 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 29 _OPEN_READONLY )
17d41 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 {. rc
17d42 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 = SQLITE_CANTOP
17d43 45 4e 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 EN_BKPT;.
17d44 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c sqlite3OsCl
17d45 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 ose(pPager->jfd)
17d46 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
17d47 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
17d48 20 0a 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 62 . /* Playb
17d49 61 63 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 74 ack and delete t
17d4a 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f he journal. Dro
17d4b 70 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 p the database w
17d4c 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 6c 6f rite. ** lo
17d4d 63 6b 20 61 6e 64 20 72 65 61 63 71 75 69 72 65 ck and reacquire
17d4e 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 20 the read lock.
17d4f 50 75 72 67 65 20 74 68 65 20 63 61 63 68 65 20 Purge the cache
17d50 62 65 66 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 before. **
17d51 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65 playing back the
17d52 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 hot-journal so
17d53 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20 65 6e that we don't en
17d54 64 20 75 70 20 77 69 74 68 0a 20 20 20 20 20 20 d up with.
17d55 2a 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 ** an inconsiste
17d56 6e 74 20 63 61 63 68 65 2e 20 20 53 79 6e 63 20 nt cache. Sync
17d57 74 68 65 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 the hot journal
17d58 62 65 66 6f 72 65 20 70 6c 61 79 69 6e 67 0a 20 before playing.
17d59 20 20 20 20 20 2a 2a 20 69 74 20 62 61 63 6b 20 ** it back
17d5a 73 69 6e 63 65 20 74 68 65 20 70 72 6f 63 65 73 since the proces
17d5b 73 20 74 68 61 74 20 63 72 61 73 68 65 64 20 61 s that crashed a
17d5c 6e 64 20 6c 65 66 74 20 74 68 65 20 68 6f 74 20 nd left the hot
17d5d 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a journal. **
17d5e 20 70 72 6f 62 61 62 6c 79 20 64 69 64 20 6e 6f probably did no
17d5f 74 20 73 79 6e 63 20 69 74 20 61 6e 64 20 77 65 t sync it and we
17d60 20 61 72 65 20 72 65 71 75 69 72 65 64 20 74 6f are required to
17d61 20 61 6c 77 61 79 73 20 73 79 6e 63 0a 20 20 20 always sync.
17d62 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 ** the journa
17d63 6c 20 62 65 66 6f 72 65 20 70 6c 61 79 69 6e 67 l before playing
17d64 20 69 74 20 62 61 63 6b 2e 0a 20 20 20 20 20 20 it back..
17d65 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f */. if( isO
17d66 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
17d67 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
17d68 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rt( rc==SQLITE_O
17d69 4b 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 K );. rc
17d6a 3d 20 70 61 67 65 72 53 79 6e 63 48 6f 74 4a 6f = pagerSyncHotJo
17d6b 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 urnal(pPager);.
17d6c 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
17d6d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17d6e 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
17d6f 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 _playback(pPager
17d70 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 1);.
17d71 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 20 3d pPager->eState =
17d72 20 50 41 47 45 52 5f 4f 50 45 4e 3b 0a 20 20 20 PAGER_OPEN;.
17d73 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
17d74 73 65 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e se if( !pPager->
17d75 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b exclusiveMode ){
17d76 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 55 6e . pagerUn
17d77 6c 6f 63 6b 44 62 28 70 50 61 67 65 72 2c 20 53 lockDb(pPager, S
17d78 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 HARED_LOCK);.
17d79 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 }.. if(
17d7a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
17d7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 . /* This
17d7c 20 62 72 61 6e 63 68 20 69 73 20 74 61 6b 65 6e branch is taken
17d7d 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 if an error occ
17d7e 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 urs while trying
17d7f 20 74 6f 20 6f 70 65 6e 0a 20 20 20 20 20 20 20 to open.
17d80 20 2a 2a 20 6f 72 20 72 6f 6c 6c 20 62 61 63 6b ** or roll back
17d81 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77 a hot-journal w
17d82 68 69 6c 65 20 68 6f 6c 64 69 6e 67 20 61 6e 20 hile holding an
17d83 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 EXCLUSIVE lock.
17d84 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 The. ** p
17d85 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 72 6f ager_unlock() ro
17d86 75 74 69 6e 65 20 77 69 6c 6c 20 62 65 20 63 61 utine will be ca
17d87 6c 6c 65 64 20 62 65 66 6f 72 65 20 72 65 74 75 lled before retu
17d88 72 6e 69 6e 67 20 74 6f 20 75 6e 6c 6f 63 6b 0a rning to unlock.
17d89 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 ** the f
17d8a 69 6c 65 2e 20 49 66 20 74 68 65 20 75 6e 6c 6f ile. If the unlo
17d8b 63 6b 20 61 74 74 65 6d 70 74 20 66 61 69 6c 73 ck attempt fails
17d8c 2c 20 74 68 65 6e 20 50 61 67 65 72 2e 65 4c 6f , then Pager.eLo
17d8d 63 6b 20 6d 75 73 74 20 62 65 0a 20 20 20 20 20 ck must be.
17d8e 20 20 20 2a 2a 20 73 65 74 20 74 6f 20 55 4e 4b ** set to UNK
17d8f 4e 4f 57 4e 5f 4c 4f 43 4b 20 28 73 65 65 20 74 NOWN_LOCK (see t
17d90 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 he comment above
17d91 20 74 68 65 20 23 64 65 66 69 6e 65 20 66 6f 72 the #define for
17d92 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 55 4e 4b . ** UNK
17d93 4e 4f 57 4e 5f 4c 4f 43 4b 20 61 62 6f 76 65 20 NOWN_LOCK above
17d94 66 6f 72 20 61 6e 20 65 78 70 6c 61 6e 61 74 69 for an explanati
17d95 6f 6e 29 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a on). . **
17d96 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 20 6f . ** In o
17d97 72 64 65 72 20 74 6f 20 67 65 74 20 70 61 67 65 rder to get page
17d98 72 5f 75 6e 6c 6f 63 6b 28 29 20 74 6f 20 64 6f r_unlock() to do
17d99 20 74 68 69 73 2c 20 73 65 74 20 50 61 67 65 72 this, set Pager
17d9a 2e 65 53 74 61 74 65 20 74 6f 0a 20 20 20 20 20 .eState to.
17d9b 20 20 20 2a 2a 20 50 41 47 45 52 5f 45 52 52 4f ** PAGER_ERRO
17d9c 52 20 6e 6f 77 2e 20 54 68 69 73 20 69 73 20 6e R now. This is n
17d9d 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6f 75 6e ot actually coun
17d9e 74 65 64 20 61 73 20 61 20 74 72 61 6e 73 69 74 ted as a transit
17d9f 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 ion. ** t
17da0 6f 20 45 52 52 4f 52 20 73 74 61 74 65 20 69 6e o ERROR state in
17da1 20 74 68 65 20 73 74 61 74 65 20 64 69 61 67 72 the state diagr
17da2 61 6d 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 am at the top of
17da3 20 74 68 69 73 20 66 69 6c 65 2c 0a 20 20 20 20 this file,.
17da4 20 20 20 20 2a 2a 20 73 69 6e 63 65 20 77 65 20 ** since we
17da5 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 73 61 know that the sa
17da6 6d 65 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 me call to pager
17da7 5f 75 6e 6c 6f 63 6b 28 29 20 77 69 6c 6c 20 76 _unlock() will v
17da8 65 72 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 ery. ** s
17da9 68 6f 72 74 6c 79 20 74 72 61 6e 73 69 74 69 6f hortly transitio
17daa 6e 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 n the pager obje
17dab 63 74 20 74 6f 20 74 68 65 20 4f 50 45 4e 20 73 ct to the OPEN s
17dac 74 61 74 65 2e 20 43 61 6c 6c 69 6e 67 0a 20 20 tate. Calling.
17dad 20 20 20 20 20 20 2a 2a 20 61 73 73 65 72 74 5f ** assert_
17dae 70 61 67 65 72 5f 73 74 61 74 65 28 29 20 77 6f pager_state() wo
17daf 75 6c 64 20 66 61 69 6c 20 6e 6f 77 2c 20 61 73 uld fail now, as
17db0 20 69 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 it should not b
17db1 65 20 70 6f 73 73 69 62 6c 65 0a 20 20 20 20 20 e possible.
17db2 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e 20 45 ** to be in E
17db3 52 52 4f 52 20 73 74 61 74 65 20 77 68 65 6e 20 RROR state when
17db4 74 68 65 72 65 20 61 72 65 20 7a 65 72 6f 20 6f there are zero o
17db5 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 utstanding page
17db6 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 66 65 . ** refe
17db7 72 65 6e 63 65 73 2e 0a 20 20 20 20 20 20 20 20 rences..
17db8 2a 2f 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 */. pager
17db9 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 _error(pPager, r
17dba 63 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f c);. goto
17dbb 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d failed;. }
17dbc 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 .. assert(
17dbd 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d pPager->eState==
17dbe 50 41 47 45 52 5f 4f 50 45 4e 20 29 3b 0a 20 20 PAGER_OPEN );.
17dbf 20 20 20 20 61 73 73 65 72 74 28 20 28 70 50 61 assert( (pPa
17dc0 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 53 48 41 52 ger->eLock==SHAR
17dc1 45 44 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 20 20 ED_LOCK).
17dc2 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e || (pPager->
17dc3 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26 exclusiveMode &&
17dc4 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3e 53 pPager->eLock>S
17dc5 48 41 52 45 44 5f 4c 4f 43 4b 29 0a 20 20 20 20 HARED_LOCK).
17dc6 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 );. }..
17dc7 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d if( !pPager->tem
17dc8 70 46 69 6c 65 20 0a 20 20 20 20 20 26 26 20 28 pFile . && (
17dc9 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 pPager->pBackup
17dca 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 || sqlite3Pcache
17dcb 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 Pagecount(pPager
17dcc 2d 3e 70 50 43 61 63 68 65 29 3e 30 29 20 0a 20 ->pPCache)>0) .
17dcd 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 ){. /* T
17dce 68 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 68 he shared-lock h
17dcf 61 73 20 6a 75 73 74 20 62 65 65 6e 20 61 63 71 as just been acq
17dd0 75 69 72 65 64 20 6f 6e 20 74 68 65 20 64 61 74 uired on the dat
17dd1 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 abase file.
17dd2 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61 72 ** and there ar
17dd3 65 20 61 6c 72 65 61 64 79 20 70 61 67 65 73 20 e already pages
17dd4 69 6e 20 74 68 65 20 63 61 63 68 65 20 28 66 72 in the cache (fr
17dd5 6f 6d 20 61 20 70 72 65 76 69 6f 75 73 0a 20 20 om a previous.
17dd6 20 20 20 20 2a 2a 20 72 65 61 64 20 6f 72 20 77 ** read or w
17dd7 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
17dd8 29 2e 20 20 43 68 65 63 6b 20 74 6f 20 73 65 65 ). Check to see
17dd9 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 if the database
17dda 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20 62 65 . ** has be
17ddb 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 49 66 en modified. If
17ddc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
17ddd 73 20 63 68 61 6e 67 65 64 2c 20 66 6c 75 73 68 s changed, flush
17dde 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 63 61 the. ** ca
17ddf 63 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 che.. **.
17de0 20 20 20 20 2a 2a 20 44 61 74 61 62 61 73 65 20 ** Database
17de1 63 68 61 6e 67 65 73 20 69 73 20 64 65 74 65 63 changes is detec
17de2 74 65 64 20 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 ted by looking a
17de3 74 20 31 35 20 62 79 74 65 73 20 62 65 67 69 6e t 15 bytes begin
17de4 6e 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 74 ning. ** at
17de5 20 6f 66 66 73 65 74 20 32 34 20 69 6e 74 6f 20 offset 24 into
17de6 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 66 the file. The f
17de7 69 72 73 74 20 34 20 6f 66 20 74 68 65 73 65 20 irst 4 of these
17de8 31 36 20 62 79 74 65 73 20 61 72 65 0a 20 20 20 16 bytes are.
17de9 20 20 20 2a 2a 20 61 20 33 32 2d 62 69 74 20 63 ** a 32-bit c
17dea 6f 75 6e 74 65 72 20 74 68 61 74 20 69 73 20 69 ounter that is i
17deb 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68 20 ncremented with
17dec 65 61 63 68 20 63 68 61 6e 67 65 2e 20 20 54 68 each change. Th
17ded 65 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 e. ** other
17dee 20 62 79 74 65 73 20 63 68 61 6e 67 65 20 72 61 bytes change ra
17def 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65 61 63 68 ndomly with each
17df0 20 66 69 6c 65 20 63 68 61 6e 67 65 20 77 68 65 file change whe
17df1 6e 0a 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 64 n. ** a cod
17df2 65 63 20 69 73 20 69 6e 20 75 73 65 2e 0a 20 20 ec is in use..
17df3 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a ** . **
17df4 20 54 68 65 72 65 20 69 73 20 61 20 76 61 6e 69 There is a vani
17df5 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 63 68 shingly small ch
17df6 61 6e 63 65 20 74 68 61 74 20 61 20 63 68 61 6e ance that a chan
17df7 67 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 0a ge will not be .
17df8 20 20 20 20 20 20 2a 2a 20 64 65 74 65 63 74 65 ** detecte
17df9 64 2e 20 20 54 68 65 20 63 68 61 6e 63 65 20 6f d. The chance o
17dfa 66 20 61 6e 20 75 6e 64 65 74 65 63 74 65 64 20 f an undetected
17dfb 63 68 61 6e 67 65 20 69 73 20 73 6f 20 73 6d 61 change is so sma
17dfc 6c 6c 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a ll that. **
17dfd 20 69 74 20 63 61 6e 20 62 65 20 6e 65 67 6c 65 it can be negle
17dfe 63 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 cted.. */.
17dff 20 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 20 Pgno nPage
17e00 3d 20 30 3b 0a 20 20 20 20 20 20 63 68 61 72 20 = 0;. char
17e01 64 62 46 69 6c 65 56 65 72 73 5b 73 69 7a 65 6f dbFileVers[sizeo
17e02 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 f(pPager->dbFile
17e03 56 65 72 73 29 5d 3b 0a 0a 20 20 20 20 20 20 72 Vers)];.. r
17e04 63 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 c = pagerPagecou
17e05 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 50 61 67 nt(pPager, &nPag
17e06 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 e);. if( rc
17e07 20 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a ) goto failed;.
17e08 0a 20 20 20 20 20 20 69 66 28 20 6e 50 61 67 65 . if( nPage
17e09 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 49 4f >0 ){. IO
17e0a 54 52 41 43 45 28 28 22 43 4b 56 45 52 53 20 25 TRACE(("CKVERS %
17e0b 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %d\n", pPager,
17e0c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 sizeof(dbFileVe
17e0d 72 73 29 29 29 3b 0a 20 20 20 20 20 20 20 20 72 rs)));. r
17e0e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
17e0f 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 64 d(pPager->fd, &d
17e10 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f bFileVers, sizeo
17e11 66 28 64 62 46 69 6c 65 56 65 72 73 29 2c 20 32 f(dbFileVers), 2
17e12 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 4);. if(
17e13 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
17e14 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
17e15 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 failed;.
17e16 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
17e17 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 64 62 memset(db
17e18 46 69 6c 65 56 65 72 73 2c 20 30 2c 20 73 69 7a FileVers, 0, siz
17e19 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 eof(dbFileVers))
17e1a 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
17e1b 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 50 61 67 if( memcmp(pPag
17e1c 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 er->dbFileVers,
17e1d 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 dbFileVers, size
17e1e 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 21 of(dbFileVers))!
17e1f 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 61 =0 ){. pa
17e20 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 ger_reset(pPager
17e21 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
17e22 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 .. /* If ther
17e23 65 20 69 73 20 61 20 57 41 4c 20 66 69 6c 65 20 e is a WAL file
17e24 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 in the file-syst
17e25 65 6d 2c 20 6f 70 65 6e 20 74 68 69 73 20 64 61 em, open this da
17e26 74 61 62 61 73 65 20 69 6e 20 57 41 4c 0a 20 20 tabase in WAL.
17e27 20 20 2a 2a 20 6d 6f 64 65 2e 20 4f 74 68 65 72 ** mode. Other
17e28 77 69 73 65 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 wise, the follow
17e29 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c ing function cal
17e2a 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 l is a no-op..
17e2b 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 */. rc = pa
17e2c 67 65 72 4f 70 65 6e 57 61 6c 49 66 50 72 65 73 gerOpenWalIfPres
17e2d 65 6e 74 28 70 50 61 67 65 72 29 3b 0a 23 69 66 ent(pPager);.#if
17e2e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
17e2f 5f 57 41 4c 0a 20 20 20 20 61 73 73 65 72 74 28 _WAL. assert(
17e30 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 3d 3d 30 pPager->pWal==0
17e31 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f || rc==SQLITE_O
17e32 4b 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a K );.#endif. }.
17e33 0a 20 20 69 66 28 20 70 61 67 65 72 55 73 65 57 . if( pagerUseW
17e34 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 al(pPager) ){.
17e35 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
17e36 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 LITE_OK );. r
17e37 63 20 3d 20 70 61 67 65 72 42 65 67 69 6e 52 65 c = pagerBeginRe
17e38 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 adTransaction(pP
17e39 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 ager);. }.. if
17e3a 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 ( pPager->eState
17e3b 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 26 26 20 ==PAGER_OPEN &&
17e3c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
17e3d 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50 . rc = pagerP
17e3e 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
17e3f 20 26 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 &pPager->dbSize
17e40 29 3b 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a );. }.. failed:
17e41 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
17e42 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 E_OK ){. asse
17e43 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 rt( !MEMDB );.
17e44 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 pager_unlock(p
17e45 50 61 67 65 72 29 3b 0a 20 20 20 20 61 73 73 65 Pager);. asse
17e46 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 rt( pPager->eSta
17e47 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 29 te==PAGER_OPEN )
17e48 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
17e49 50 61 67 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 Pager->eState =
17e4a 50 41 47 45 52 5f 52 45 41 44 45 52 3b 0a 20 20 PAGER_READER;.
17e4b 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
17e4c 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 ../*.** If the r
17e4d 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 68 eference count h
17e4e 61 73 20 72 65 61 63 68 65 64 20 7a 65 72 6f 2c as reached zero,
17e4f 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 rollback any ac
17e50 74 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 tive.** transact
17e51 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 ion and unlock t
17e52 68 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 he pager..**.**
17e53 45 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63 6b 69 Except, in locki
17e54 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 49 56 ng_mode=EXCLUSIV
17e55 45 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 E when there is
17e56 6e 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a 2a 2a nothing to in.**
17e57 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
17e58 75 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c 6f 63 urnal, the unloc
17e59 6b 20 69 73 20 6e 6f 74 20 70 65 72 66 6f 72 6d k is not perform
17e5a 65 64 20 61 6e 64 20 74 68 65 72 65 20 69 73 0a ed and there is.
17e5b 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 72 6f ** nothing to ro
17e5c 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69 73 20 llback, so this
17e5d 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
17e5e 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 op..*/ .static v
17e5f 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 oid pagerUnlockI
17e60 66 55 6e 75 73 65 64 28 50 61 67 65 72 20 2a 70 fUnused(Pager *p
17e61 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 28 73 Pager){. if( (s
17e62 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
17e63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 ount(pPager->pPC
17e64 61 63 68 65 29 3d 3d 30 29 20 29 7b 0a 20 20 20 ache)==0) ){.
17e65 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 pagerUnlockAndR
17e66 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b ollback(pPager);
17e67 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 . }.}../*.** Ac
17e68 71 75 69 72 65 20 61 20 72 65 66 65 72 65 6e 63 quire a referenc
17e69 65 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 e to page number
17e6a 20 70 67 6e 6f 20 69 6e 20 70 61 67 65 72 20 70 pgno in pager p
17e6b 50 61 67 65 72 20 28 61 20 70 61 67 65 0a 2a 2a Pager (a page.**
17e6c 20 72 65 66 65 72 65 6e 63 65 20 68 61 73 20 74 reference has t
17e6d 79 70 65 20 44 62 50 61 67 65 2a 29 2e 20 49 66 ype DbPage*). If
17e6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 72 the requested r
17e6f 65 66 65 72 65 6e 63 65 20 69 73 20 0a 2a 2a 20 eference is .**
17e70 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74 successfully obt
17e71 61 69 6e 65 64 2c 20 69 74 20 69 73 20 63 6f 70 ained, it is cop
17e72 69 65 64 20 74 6f 20 2a 70 70 50 61 67 65 20 61 ied to *ppPage a
17e73 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 nd SQLITE_OK ret
17e74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
17e75 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 the requested pa
17e76 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e ge is already in
17e77 20 74 68 65 20 63 61 63 68 65 2c 20 69 74 20 69 the cache, it i
17e78 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 s returned. .**
17e79 4f 74 68 65 72 77 69 73 65 2c 20 61 20 6e 65 77 Otherwise, a new
17e7a 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 page object is
17e7b 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f allocated and po
17e7c 70 75 6c 61 74 65 64 20 77 69 74 68 20 64 61 74 pulated with dat
17e7d 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 74 a.** read from t
17e7e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
17e7f 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c . In some cases,
17e80 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75 the pcache modu
17e81 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f 6f 73 65 le may.** choose
17e82 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 not to allocate
17e83 20 61 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 a new page obje
17e84 63 74 20 61 6e 64 20 6d 61 79 20 72 65 75 73 65 ct and may reuse
17e85 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 an existing.**
17e86 6f 62 6a 65 63 74 20 77 69 74 68 20 6e 6f 20 6f object with no o
17e87 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 utstanding refer
17e88 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ences..**.** The
17e89 20 65 78 74 72 61 20 64 61 74 61 20 61 70 70 65 extra data appe
17e8a 6e 64 65 64 20 74 6f 20 61 20 70 61 67 65 20 69 nded to a page i
17e8b 73 20 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c s always initial
17e8c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20 74 68 ized to zeros th
17e8d 65 20 0a 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 e .** first time
17e8e 20 61 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 a page is loade
17e8f 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 d into memory. I
17e90 66 20 74 68 65 20 70 61 67 65 20 72 65 71 75 65 f the page reque
17e91 73 74 65 64 20 69 73 20 0a 2a 2a 20 61 6c 72 65 sted is .** alre
17e92 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 ady in the cache
17e93 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
17e94 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 ion is called, t
17e95 68 65 6e 20 74 68 65 20 65 78 74 72 61 0a 2a 2a hen the extra.**
17e96 20 64 61 74 61 20 69 73 20 6c 65 66 74 20 61 73 data is left as
17e97 20 69 74 20 77 61 73 20 77 68 65 6e 20 74 68 65 it was when the
17e98 20 70 61 67 65 20 6f 62 6a 65 63 74 20 77 61 73 page object was
17e99 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a last used..**.*
17e9a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
17e9b 65 20 69 6d 61 67 65 20 69 73 20 73 6d 61 6c 6c e image is small
17e9c 65 72 20 74 68 61 6e 20 74 68 65 20 72 65 71 75 er than the requ
17e9d 65 73 74 65 64 20 70 61 67 65 20 6f 72 20 69 66 ested page or if
17e9e 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 a .** non-zero
17e9f 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 64 20 value is passed
17ea0 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 as the noContent
17ea1 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 74 parameter and t
17ea2 68 65 20 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 he .** requested
17ea3 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 page is not alr
17ea4 65 61 64 79 20 73 74 6f 72 65 64 20 69 6e 20 74 eady stored in t
17ea5 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20 6e he cache, then n
17ea6 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20 64 69 73 o .** actual dis
17ea7 6b 20 72 65 61 64 20 6f 63 63 75 72 73 2e 20 49 k read occurs. I
17ea8 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 n this case the
17ea9 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 memory image of
17eaa 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 69 73 20 the .** page is
17eab 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 initialized to a
17eac 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a ll zeros. .**.**
17ead 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 If noContent is
17eae 20 74 72 75 65 2c 20 69 74 20 6d 65 61 6e 73 20 true, it means
17eaf 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63 that we do not c
17eb0 61 72 65 20 61 62 6f 75 74 20 74 68 65 20 63 6f are about the co
17eb1 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 ntents.** of the
17eb2 20 70 61 67 65 2e 20 54 68 69 73 20 6f 63 63 75 page. This occu
17eb3 72 73 20 69 6e 20 74 77 6f 20 73 65 70 65 72 61 rs in two sepera
17eb4 74 65 20 73 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a te scenarios:.**
17eb5 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e 20 72 65 .** a) When re
17eb6 61 64 69 6e 67 20 61 20 66 72 65 65 2d 6c 69 73 ading a free-lis
17eb7 74 20 6c 65 61 66 20 70 61 67 65 20 66 72 6f 6d t leaf page from
17eb8 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 61 the database, a
17eb9 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 57 68 nd.**.** b) Wh
17eba 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69 en a savepoint i
17ebb 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 s being rolled b
17ebc 61 63 6b 20 61 6e 64 20 77 65 20 6e 65 65 64 20 ack and we need
17ebd 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20 20 20 20 to load.**
17ebe 61 20 6e 65 77 20 70 61 67 65 20 69 6e 74 6f 20 a new page into
17ebf 74 68 65 20 63 61 63 68 65 20 74 6f 20 62 65 20 the cache to be
17ec0 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 filled with the
17ec1 64 61 74 61 20 72 65 61 64 0a 2a 2a 20 20 20 20 data read.**
17ec2 20 20 66 72 6f 6d 20 74 68 65 20 73 61 76 65 70 from the savep
17ec3 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a oint journal..**
17ec4 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 .** If noContent
17ec5 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
17ec6 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 he data returned
17ec7 20 69 73 20 7a 65 72 6f 65 64 20 69 6e 73 74 65 is zeroed inste
17ec8 61 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67 20 72 ad of.** being r
17ec9 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 ead from the dat
17eca 61 62 61 73 65 2e 20 41 64 64 69 74 69 6f 6e 61 abase. Additiona
17ecb 6c 6c 79 2c 20 74 68 65 20 62 69 74 73 20 63 6f lly, the bits co
17ecc 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 rresponding.** t
17ecd 6f 20 70 67 6e 6f 20 69 6e 20 50 61 67 65 72 2e o pgno in Pager.
17ece 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69 74 76 pInJournal (bitv
17ecf 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72 65 ec of pages alre
17ed0 61 64 79 20 77 72 69 74 74 65 6e 20 74 6f 20 74 ady written to t
17ed1 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 he.** journal fi
17ed2 6c 65 29 20 61 6e 64 20 74 68 65 20 50 61 67 65 le) and the Page
17ed3 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 rSavepoint.pInSa
17ed4 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 20 vepoint bitvecs
17ed5 6f 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 73 of any open.** s
17ed6 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 65 avepoints are se
17ed7 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66 t. This means if
17ed8 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 64 the page is mad
17ed9 65 20 77 72 69 74 61 62 6c 65 20 61 74 20 61 6e e writable at an
17eda 79 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 74 68 y.** point in th
17edb 65 20 66 75 74 75 72 65 2c 20 75 73 69 6e 67 20 e future, using
17edc 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 a call to sqlite
17edd 33 50 61 67 65 72 57 72 69 74 65 28 29 2c 20 69 3PagerWrite(), i
17ede 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 77 ts contents.** w
17edf 69 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75 72 6e ill not be journ
17ee0 61 6c 65 64 2e 20 54 68 69 73 20 73 61 76 65 73 aled. This saves
17ee1 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 IO..**.** The a
17ee2 63 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 68 74 cquisition might
17ee3 20 66 61 69 6c 20 66 6f 72 20 73 65 76 65 72 61 fail for severa
17ee4 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e 20 61 l reasons. In a
17ee5 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 6e 20 ll cases,.** an
17ee6 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f appropriate erro
17ee7 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
17ee8 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 20 69 ed and *ppPage i
17ee9 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a s set to NULL..*
17eea 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 *.** See also sq
17eeb 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 lite3PagerLookup
17eec 28 29 2e 20 20 42 6f 74 68 20 74 68 69 73 20 72 (). Both this r
17eed 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75 outine and Looku
17eee 70 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 p() attempt.** t
17eef 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20 69 6e o find a page in
17ef0 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 the in-memory c
17ef1 61 63 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 ache first. If
17ef2 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
17ef3 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 6d 65 already.** in me
17ef4 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 74 69 mory, this routi
17ef5 6e 65 20 67 6f 65 73 20 74 6f 20 64 69 73 6b 20 ne goes to disk
17ef6 74 6f 20 72 65 61 64 20 69 74 20 69 6e 20 77 68 to read it in wh
17ef7 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a ereas Lookup().*
17ef8 2a 20 6a 75 73 74 20 72 65 74 75 72 6e 73 20 30 * just returns 0
17ef9 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
17efa 61 63 71 75 69 72 65 73 20 61 20 72 65 61 64 2d acquires a read-
17efb 6c 6f 63 6b 20 74 68 65 20 66 69 72 73 74 20 74 lock the first t
17efc 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 74 6f ime it.** has to
17efd 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 6e 64 go to disk, and
17efe 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c 61 79 could also play
17eff 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 back an old jour
17f00 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 79 nal if necessary
17f01 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 ..** Since Looku
17f02 70 28 29 20 6e 65 76 65 72 20 67 6f 65 73 20 74 p() never goes t
17f03 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 65 72 o disk, it never
17f04 20 68 61 73 20 74 6f 20 64 65 61 6c 20 77 69 74 has to deal wit
17f05 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f h locks.** or jo
17f06 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a urnal files..*/.
17f07 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
17f08 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 nt sqlite3PagerA
17f09 63 71 75 69 72 65 28 0a 20 20 50 61 67 65 72 20 cquire(. Pager
17f0a 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f 2a *pPager, /*
17f0b 20 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e 20 The pager open
17f0c 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
17f0d 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 file */. Pgno p
17f0e 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a gno, /*
17f0f 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 Page number to
17f10 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61 67 fetch */. DbPag
17f11 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f e **ppPage, /
17f12 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 * Write a pointe
17f13 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 r to the page he
17f14 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f re */. int noCo
17f15 6e 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20 44 ntent /* D
17f16 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65 61 o not bother rea
17f17 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f ding content fro
17f18 6d 20 64 69 73 6b 20 69 66 20 74 72 75 65 20 2a m disk if true *
17f19 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 /.){. int rc;.
17f1a 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20 PgHdr *pPg;..
17f1b 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
17f1c 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 eState>=PAGER_RE
17f1d 41 44 45 52 20 29 3b 0a 20 20 61 73 73 65 72 74 ADER );. assert
17f1e 28 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 ( assert_pager_s
17f1f 74 61 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a tate(pPager) );.
17f20 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 . if( pgno==0 )
17f21 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
17f22 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
17f23 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 ;. }.. /* If t
17f24 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 he pager is in t
17f25 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 he error state,
17f26 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
17f27 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a 20 20 immediately. .
17f28 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 ** Otherwise, re
17f29 71 75 65 73 74 20 74 68 65 20 70 61 67 65 20 66 quest the page f
17f2a 72 6f 6d 20 74 68 65 20 50 43 61 63 68 65 20 6c rom the PCache l
17f2b 61 79 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 ayer. */. if( p
17f2c 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d Pager->errCode!=
17f2d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
17f2e 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 rc = pPager->er
17f2f 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a rCode;. }else{.
17f30 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
17f31 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67 PcacheFetch(pPag
17f32 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e er->pPCache, pgn
17f33 6f 2c 20 31 2c 20 70 70 50 61 67 65 29 3b 0a 20 o, 1, ppPage);.
17f34 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 }.. if( rc!=SQ
17f35 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f LITE_OK ){. /
17f36 2a 20 45 69 74 68 65 72 20 74 68 65 20 63 61 6c * Either the cal
17f37 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 63 61 63 l to sqlite3Pcac
17f38 68 65 46 65 74 63 68 28 29 20 72 65 74 75 72 6e heFetch() return
17f39 65 64 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 74 ed an error or t
17f3a 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 20 he. ** pager
17f3b 77 61 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 was already in t
17f3c 68 65 20 65 72 72 6f 72 2d 73 74 61 74 65 20 77 he error-state w
17f3d 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
17f3e 6e 20 77 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20 n was called..
17f3f 20 20 2a 2a 20 53 65 74 20 70 50 67 20 74 6f 20 ** Set pPg to
17f40 30 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 74 68 0 and jump to th
17f41 65 20 65 78 63 65 70 74 69 6f 6e 20 68 61 6e 64 e exception hand
17f42 6c 65 72 2e 20 20 2a 2f 0a 20 20 20 20 70 50 67 ler. */. pPg
17f43 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 70 = 0;. goto p
17f44 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 ager_acquire_err
17f45 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
17f46 28 2a 70 70 50 61 67 65 29 2d 3e 70 67 6e 6f 3d (*ppPage)->pgno=
17f47 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 =pgno );. asser
17f48 74 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 t( (*ppPage)->pP
17f49 61 67 65 72 3d 3d 70 50 61 67 65 72 20 7c 7c 20 ager==pPager ||
17f4a 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 (*ppPage)->pPage
17f4b 72 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 28 r==0 );.. if( (
17f4c 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72 *ppPage)->pPager
17f4d 20 26 26 20 21 6e 6f 43 6f 6e 74 65 6e 74 20 29 && !noContent )
17f4e 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 {. /* In this
17f4f 20 63 61 73 65 20 74 68 65 20 70 63 61 63 68 65 case the pcache
17f50 20 61 6c 72 65 61 64 79 20 63 6f 6e 74 61 69 6e already contain
17f51 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 s an initialized
17f52 20 63 6f 70 79 20 6f 66 0a 20 20 20 20 2a 2a 20 copy of. **
17f53 74 68 65 20 70 61 67 65 2e 20 52 65 74 75 72 6e the page. Return
17f54 20 77 69 74 68 6f 75 74 20 66 75 72 74 68 65 72 without further
17f55 20 61 64 6f 2e 20 20 2a 2f 0a 20 20 20 20 61 73 ado. */. as
17f56 73 65 72 74 28 20 70 67 6e 6f 3c 3d 50 41 47 45 sert( pgno<=PAGE
17f57 52 5f 4d 41 58 5f 50 47 4e 4f 20 26 26 20 70 67 R_MAX_PGNO && pg
17f58 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e no!=PAGER_MJ_PGN
17f59 4f 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 O(pPager) );.
17f5a 20 70 50 61 67 65 72 2d 3e 61 53 74 61 74 5b 50 pPager->aStat[P
17f5b 41 47 45 52 5f 53 54 41 54 5f 48 49 54 5d 2b 2b AGER_STAT_HIT]++
17f5c 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
17f5d 49 54 45 5f 4f 4b 3b 0a 0a 20 20 7d 65 6c 73 65 ITE_OK;.. }else
17f5e 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 {. /* The pag
17f5f 65 72 20 63 61 63 68 65 20 68 61 73 20 63 72 65 er cache has cre
17f60 61 74 65 64 20 61 20 6e 65 77 20 70 61 67 65 2e ated a new page.
17f61 20 49 74 73 20 63 6f 6e 74 65 6e 74 20 6e 65 65 Its content nee
17f62 64 73 20 74 6f 20 0a 20 20 20 20 2a 2a 20 62 65 ds to . ** be
17f63 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 2a initialized. *
17f64 2f 0a 0a 20 20 20 20 70 50 67 20 3d 20 2a 70 70 /.. pPg = *pp
17f65 50 61 67 65 3b 0a 20 20 20 20 70 50 67 2d 3e 70 Page;. pPg->p
17f66 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a Pager = pPager;.
17f67 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 . /* The maxi
17f68 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 65 72 20 mum page number
17f69 69 73 20 32 5e 33 31 2e 20 52 65 74 75 72 6e 20 is 2^31. Return
17f6a 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 SQLITE_CORRUPT i
17f6b 66 20 61 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 f a page. **
17f6c 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 number greater t
17f6d 68 61 6e 20 74 68 69 73 2c 20 6f 72 20 74 68 65 han this, or the
17f6e 20 75 6e 75 73 65 64 20 6c 6f 63 6b 69 6e 67 2d unused locking-
17f6f 70 61 67 65 2c 20 69 73 20 72 65 71 75 65 73 74 page, is request
17f70 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 ed. */. if( p
17f71 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 58 5f 50 47 gno>PAGER_MAX_PG
17f72 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 NO || pgno==PAGE
17f73 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 R_MJ_PGNO(pPager
17f74 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ) ){. rc =
17f75 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
17f76 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 KPT;. goto
17f77 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 pager_acquire_er
17f78 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 r;. }.. if
17f79 28 20 4d 45 4d 44 42 20 7c 7c 20 70 50 61 67 65 ( MEMDB || pPage
17f7a 72 2d 3e 64 62 53 69 7a 65 3c 70 67 6e 6f 20 7c r->dbSize<pgno |
17f7b 7c 20 6e 6f 43 6f 6e 74 65 6e 74 20 7c 7c 20 21 | noContent || !
17f7c 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
17f7d 64 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 d) ){. if(
17f7e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d 78 50 pgno>pPager->mxP
17f7f 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 gno ){. r
17f80 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b c = SQLITE_FULL;
17f81 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 70 61 . goto pa
17f82 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b ger_acquire_err;
17f83 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
17f84 66 28 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a f( noContent ){.
17f85 20 20 20 20 20 20 20 20 2f 2a 20 46 61 69 6c 75 /* Failu
17f86 72 65 20 74 6f 20 73 65 74 20 74 68 65 20 62 69 re to set the bi
17f87 74 73 20 69 6e 20 74 68 65 20 49 6e 4a 6f 75 72 ts in the InJour
17f88 6e 61 6c 20 62 69 74 2d 76 65 63 74 6f 72 73 20 nal bit-vectors
17f89 69 73 20 62 65 6e 69 67 6e 2e 0a 20 20 20 20 20 is benign..
17f8a 20 20 20 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20 ** It merely
17f8b 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 6d 69 means that we mi
17f8c 67 68 74 20 64 6f 20 73 6f 6d 65 20 65 78 74 72 ght do some extr
17f8d 61 20 77 6f 72 6b 20 74 6f 20 6a 6f 75 72 6e 61 a work to journa
17f8e 6c 20 61 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 l a . **
17f8f 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 6e page that does n
17f90 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6a 6f ot need to be jo
17f91 75 72 6e 61 6c 65 64 2e 20 20 4e 65 76 65 72 74 urnaled. Nevert
17f92 68 65 6c 65 73 73 2c 20 62 65 20 73 75 72 65 20 heless, be sure
17f93 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 . ** to t
17f94 65 73 74 20 74 68 65 20 63 61 73 65 20 77 68 65 est the case whe
17f95 72 65 20 61 20 6d 61 6c 6c 6f 63 20 65 72 72 6f re a malloc erro
17f96 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 r occurs while t
17f97 72 79 69 6e 67 20 74 6f 20 73 65 74 20 0a 20 20 rying to set .
17f98 20 20 20 20 20 20 2a 2a 20 61 20 62 69 74 20 69 ** a bit i
17f99 6e 20 61 20 62 69 74 20 76 65 63 74 6f 72 2e 0a n a bit vector..
17f9a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
17f9b 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 sqlite3BeginB
17f9c 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 enignMalloc();.
17f9d 20 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3c if( pgno<
17f9e 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 =pPager->dbOrigS
17f9f 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ize ){.
17fa0 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 TESTONLY( rc =
17fa1 29 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 ) sqlite3BitvecS
17fa2 65 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f et(pPager->pInJo
17fa3 75 72 6e 61 6c 2c 20 70 67 6e 6f 29 3b 0a 20 20 urnal, pgno);.
17fa4 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
17fa5 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
17fa6 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a EM );. }.
17fa7 20 20 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 TESTONLY
17fa8 28 20 72 63 20 3d 20 29 20 61 64 64 54 6f 53 61 ( rc = ) addToSa
17fa9 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70 vepointBitvecs(p
17faa 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 Pager, pgno);.
17fab 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
17fac 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
17fad 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
17fae 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
17faf 6f 63 28 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 oc();. }.
17fb0 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e memset(pPg->
17fb1 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65 72 pData, 0, pPager
17fb2 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 ->pageSize);.
17fb3 20 20 20 49 4f 54 52 41 43 45 28 28 22 5a 45 52 IOTRACE(("ZER
17fb4 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 O %p %d\n", pPag
17fb5 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 er, pgno));.
17fb6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
17fb7 65 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72 ert( pPg->pPager
17fb8 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20 20 20 20 ==pPager );.
17fb9 20 20 70 50 61 67 65 72 2d 3e 61 53 74 61 74 5b pPager->aStat[
17fba 50 41 47 45 52 5f 53 54 41 54 5f 4d 49 53 53 5d PAGER_STAT_MISS]
17fbb 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 ++;. rc = r
17fbc 65 61 64 44 62 50 61 67 65 28 70 50 67 29 3b 0a eadDbPage(pPg);.
17fbd 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
17fbe 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17fbf 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 goto pager_ac
17fc0 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 20 quire_err;.
17fc1 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67 }. }. pag
17fc2 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28 er_set_pagehash(
17fc3 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 pPg);. }.. ret
17fc4 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a urn SQLITE_OK;..
17fc5 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 pager_acquire_er
17fc6 72 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 r:. assert( rc!
17fc7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
17fc8 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 73 if( pPg ){. s
17fc9 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 qlite3PcacheDrop
17fca 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 70 61 67 (pPg);. }. pag
17fcb 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 erUnlockIfUnused
17fcc 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 2a 70 70 (pPager);.. *pp
17fcd 50 61 67 65 20 3d 20 30 3b 0a 20 20 72 65 74 75 Page = 0;. retu
17fce 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
17fcf 41 63 71 75 69 72 65 20 61 20 70 61 67 65 20 69 Acquire a page i
17fd0 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 f it is already
17fd1 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 in the in-memory
17fd2 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e cache. Do.** n
17fd3 6f 74 20 72 65 61 64 20 74 68 65 20 70 61 67 65 ot read the page
17fd4 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74 from disk. Ret
17fd5 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
17fd6 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 the page,.** or
17fd7 20 30 20 69 66 20 74 68 65 20 70 61 67 65 20 69 0 if the page i
17fd8 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e 20 s not in cache.
17fd9 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 .**.** See also
17fda 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
17fdb 29 2e 20 20 54 68 65 20 64 69 66 66 65 72 65 6e ). The differen
17fdc 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 ce between this
17fdd 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73 routine.** and s
17fde 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 qlite3PagerGet()
17fdf 20 69 73 20 74 68 61 74 20 5f 67 65 74 28 29 20 is that _get()
17fe0 77 69 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64 will go to the d
17fe1 69 73 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20 isk and read.**
17fe2 69 6e 20 74 68 65 20 70 61 67 65 20 69 66 20 74 in the page if t
17fe3 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 he page is not a
17fe4 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e lready in cache.
17fe5 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a This routine.*
17fe6 2a 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 * returns NULL i
17fe7 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f f the page is no
17fe8 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66 t in cache or if
17fe9 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f a disk I/O erro
17fea 72 20 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68 r .** has ever h
17feb 61 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 appened..*/.SQLI
17fec 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 TE_PRIVATE DbPag
17fed 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c e *sqlite3PagerL
17fee 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 ookup(Pager *pPa
17fef 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b ger, Pgno pgno){
17ff0 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 . PgHdr *pPg =
17ff1 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 0;. assert( pPa
17ff2 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ger!=0 );. asse
17ff3 72 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 20 rt( pgno!=0 );.
17ff4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
17ff5 3e 70 50 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 >pPCache!=0 );.
17ff6 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
17ff7 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 >eState>=PAGER_R
17ff8 45 41 44 45 52 20 26 26 20 70 50 61 67 65 72 2d EADER && pPager-
17ff9 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 5f 45 >eState!=PAGER_E
17ffa 52 52 4f 52 20 29 3b 0a 20 20 73 71 6c 69 74 65 RROR );. sqlite
17ffb 33 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 3PcacheFetch(pPa
17ffc 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 ger->pPCache, pg
17ffd 6e 6f 2c 20 30 2c 20 26 70 50 67 29 3b 0a 20 20 no, 0, &pPg);.
17ffe 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f return pPg;.}../
17fff 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 70 *.** Release a p
18000 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a age reference..*
18001 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 *.** If the numb
18002 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 er of references
18003 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f to the page dro
18004 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 p to zero, then
18005 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 the.** page is a
18006 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20 dded to the LRU
18007 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 list. When all
18008 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c references to al
18009 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72 l pages.** are r
1800a 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62 eleased, a rollb
1800b 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74 ack occurs and t
1800c 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 he lock on the d
1800d 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65 atabase is.** re
1800e 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 moved..*/.SQLITE
1800f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
18010 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
18011 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 DbPage *pPg){.
18012 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50 if( pPg ){. P
18013 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 ager *pPager = p
18014 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 Pg->pPager;.
18015 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c sqlite3PcacheRel
18016 65 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70 ease(pPg);. p
18017 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 agerUnlockIfUnus
18018 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a ed(pPager);. }.
18019 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
1801a 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
1801b 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
1801c 20 65 76 65 72 79 20 77 72 69 74 65 20 74 72 61 every write tra
1801d 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 nsaction..** The
1801e 72 65 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 re must already
1801f 62 65 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 be a RESERVED or
18020 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
18021 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
18022 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20 74 68 .** file when th
18023 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
18024 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e lled..**.** Open
18025 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
18026 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 61 67 e for pager pPag
18027 65 72 20 61 6e 64 20 77 72 69 74 65 20 61 20 6a er and write a j
18028 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a 2a ournal header.**
18029 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
1802a 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 61 72 it. If there ar
1802b 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 e active savepoi
1802c 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 20 73 75 nts, open the su
1802d 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 61 73 20 b-journal.** as
1802e 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e 63 74 well. This funct
1802f 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 ion is only used
18030 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 when the journa
18031 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e 67 20 l file is being
18032 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f 20 77 72 .** opened to wr
18033 69 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6c ite a rollback l
18034 6f 67 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 og for a transac
18035 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e 6f 74 20 tion. It is not
18036 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e 20 6f 70 used .** when op
18037 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a 6f 75 72 ening a hot jour
18038 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72 6f 6c 6c nal file to roll
18039 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 it back..**.**
1803a 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 If the journal f
1803b 69 6c 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f ile is already o
1803c 70 65 6e 20 28 61 73 20 69 74 20 6d 61 79 20 62 pen (as it may b
1803d 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d e in exclusive m
1803e 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 ode),.** then th
1803f 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a 75 73 74 is function just
18040 20 77 72 69 74 65 73 20 61 20 6a 6f 75 72 6e 61 writes a journa
18041 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68 65 20 l header to the
18042 73 74 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 start of the.**
18043 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 66 69 6c already open fil
18044 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 e. .**.** Whethe
18045 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75 r or not the jou
18046 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 rnal file is ope
18047 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 ned by this func
18048 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 50 61 67 tion, the.** Pag
18049 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 er.pInJournal bi
1804a 74 76 65 63 20 73 74 72 75 63 74 75 72 65 20 69 tvec structure i
1804b 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a s allocated..**.
1804c 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ** Return SQLITE
1804d 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e _OK if everythin
1804e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e g is successful.
1804f 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 Otherwise, retu
18050 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f rn .** SQLITE_NO
18051 4d 45 4d 20 69 66 20 74 68 65 20 61 74 74 65 6d MEM if the attem
18052 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 50 pt to allocate P
18053 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 ager.pInJournal
18054 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 61 6e fails, or .** an
18055 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 IO error code i
18056 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20 77 72 69 f opening or wri
18057 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c ting the journal
18058 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a 2a 2f 0a file fails..*/.
18059 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
1805a 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50 61 _open_journal(Pa
1805b 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
1805c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1805d 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 OK;
1805e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
1805f 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 turn code */. s
18060 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e qlite3_vfs * con
18061 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 72 st pVfs = pPager
18062 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20 4c 6f 63 ->pVfs; /* Loc
18063 61 6c 20 63 61 63 68 65 20 6f 66 20 76 66 73 20 al cache of vfs
18064 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20 20 61 73 pointer */.. as
18065 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 sert( pPager->eS
18066 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 tate==PAGER_WRIT
18067 45 52 5f 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 61 ER_LOCKED );. a
18068 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 ssert( assert_pa
18069 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 ger_state(pPager
1806a 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
1806b 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
1806c 6c 3d 3d 30 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 l==0 );. . /*
1806d 49 66 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 If already in th
1806e 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 74 e error state, t
1806f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
18070 61 20 6e 6f 2d 6f 70 2e 20 20 42 75 74 20 6f 6e a no-op. But on
18071 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 . ** the other
18072 68 61 6e 64 2c 20 74 68 69 73 20 72 6f 75 74 69 hand, this routi
18073 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c ne is never call
18074 65 64 20 69 66 20 77 65 20 61 72 65 20 61 6c 72 ed if we are alr
18075 65 61 64 79 20 69 6e 0a 20 20 2a 2a 20 61 6e 20 eady in. ** an
18076 65 72 72 6f 72 20 73 74 61 74 65 2e 20 2a 2f 0a error state. */.
18077 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 if( NEVER(pPag
18078 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72 er->errCode) ) r
18079 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 eturn pPager->er
1807a 72 43 6f 64 65 3b 0a 0a 20 20 69 66 28 20 21 70 rCode;.. if( !p
1807b 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 agerUseWal(pPage
1807c 72 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f r) && pPager->jo
1807d 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 urnalMode!=PAGER
1807e 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 _JOURNALMODE_OFF
1807f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
18080 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c pInJournal = sql
18081 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 ite3BitvecCreate
18082 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 (pPager->dbSize)
18083 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 ;. if( pPager
18084 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 ->pInJournal==0
18085 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
18086 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
18087 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 4f 70 }. . /* Op
18088 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 en the journal f
18089 69 6c 65 20 69 66 20 69 74 20 69 73 20 6e 6f 74 ile if it is not
1808a 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2e 20 2a already open. *
1808b 2f 0a 20 20 20 20 69 66 28 20 21 69 73 4f 70 65 /. if( !isOpe
1808c 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 n(pPager->jfd) )
1808d 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 {. if( pPag
1808e 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d er->journalMode=
1808f 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
18090 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 DE_MEMORY ){.
18091 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a sqlite3MemJ
18092 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 ournalOpen(pPage
18093 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 7d r->jfd);. }
18094 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f else{. co
18095 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 nst int flags =
18096 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18097 20 20 2f 2a 20 56 46 53 20 66 6c 61 67 73 20 74 /* VFS flags t
18098 6f 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 o open journal f
18099 69 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 ile */.
1809a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
1809b 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 DWRITE|SQLITE_OP
1809c 45 4e 5f 43 52 45 41 54 45 7c 0a 20 20 20 20 20 EN_CREATE|.
1809d 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 74 65 (pPager->te
1809e 6d 70 46 69 6c 65 20 3f 20 0a 20 20 20 20 20 20 mpFile ? .
1809f 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 (SQLITE_OP
180a0 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 EN_DELETEONCLOSE
180a1 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d |SQLITE_OPEN_TEM
180a2 50 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20 20 20 20 P_JOURNAL):.
180a3 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
180a4 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 OPEN_MAIN_JOURNA
180a5 4c 29 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a L). );.
180a6 20 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f #ifdef SQLITE_
180a7 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
180a8 49 54 45 0a 20 20 20 20 20 20 20 20 72 63 20 3d ITE. rc =
180a9 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f sqlite3JournalO
180aa 70 65 6e 28 0a 20 20 20 20 20 20 20 20 20 20 20 pen(.
180ab 20 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a pVfs, pPager->z
180ac 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d Journal, pPager-
180ad 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 6a 72 6e >jfd, flags, jrn
180ae 6c 42 75 66 66 65 72 53 69 7a 65 28 70 50 61 67 lBufferSize(pPag
180af 65 72 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 er). );.
180b0 20 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 72 #else. r
180b1 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 c = sqlite3OsOpe
180b2 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e n(pVfs, pPager->
180b3 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 zJournal, pPager
180b4 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 29 ->jfd, flags, 0)
180b5 3b 0a 20 20 23 65 6e 64 69 66 0a 20 20 20 20 20 ;. #endif.
180b6 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }. assert(
180b7 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
180b8 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d | isOpen(pPager-
180b9 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20 7d 0a 20 >jfd) );. }.
180ba 20 0a 20 20 0a 20 20 20 20 2f 2a 20 57 72 69 74 . . /* Writ
180bb 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 e the first jour
180bc 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68 nal header to th
180bd 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 e journal file a
180be 6e 64 20 6f 70 65 6e 20 0a 20 20 20 20 2a 2a 20 nd open . **
180bf 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 the sub-journal
180c0 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 if necessary..
180c1 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d */. if( rc=
180c2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
180c3 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65 /* TODO: Che
180c4 63 6b 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 ck if all of the
180c5 73 65 20 61 72 65 20 72 65 61 6c 6c 79 20 72 65 se are really re
180c6 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20 20 20 quired. */.
180c7 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 pPager->nRec =
180c8 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 0;. pPager-
180c9 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b >journalOff = 0;
180ca 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 . pPager->s
180cb 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 etMaster = 0;.
180cc 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
180cd 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 20 20 20 nalHdr = 0;.
180ce 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 rc = writeJour
180cf 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a nalHdr(pPager);.
180d0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
180d1 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
180d2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 {. sqlite3Bit
180d3 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 vecDestroy(pPage
180d4 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a r->pInJournal);.
180d5 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a pPager->pInJ
180d6 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 7d 65 ournal = 0;. }e
180d7 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
180d8 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d pPager->eState=
180d9 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f =PAGER_WRITER_LO
180da 43 4b 45 44 20 29 3b 0a 20 20 20 20 70 50 61 67 CKED );. pPag
180db 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 47 er->eState = PAG
180dc 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d ER_WRITER_CACHEM
180dd 4f 44 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 OD;. }.. retur
180de 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 n rc;.}../*.** B
180df 65 67 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 egin a write-tra
180e0 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 nsaction on the
180e1 73 70 65 63 69 66 69 65 64 20 70 61 67 65 72 20 specified pager
180e2 6f 62 6a 65 63 74 2e 20 49 66 20 61 20 0a 2a 2a object. If a .**
180e3 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
180e4 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 on has already b
180e5 65 65 6e 20 6f 70 65 6e 65 64 2c 20 74 68 69 73 een opened, this
180e6 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
180e7 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 o-op..**.** If t
180e8 68 65 20 65 78 46 6c 61 67 20 61 72 67 75 6d 65 he exFlag argume
180e9 6e 74 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 nt is false, the
180ea 6e 20 61 63 71 75 69 72 65 20 61 74 20 6c 65 61 n acquire at lea
180eb 73 74 20 61 20 52 45 53 45 52 56 45 44 0a 2a 2a st a RESERVED.**
180ec 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
180ed 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 65 abase file. If e
180ee 78 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 xFlag is true, t
180ef 68 65 6e 20 61 63 71 75 69 72 65 20 61 74 20 6c hen acquire at l
180f0 65 61 73 74 0a 2a 2a 20 61 6e 20 45 58 43 4c 55 east.** an EXCLU
180f1 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 73 75 SIVE lock. If su
180f2 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 61 6c 72 ch a lock is alr
180f3 65 61 64 79 20 68 65 6c 64 2c 20 6e 6f 20 6c 6f eady held, no lo
180f4 63 6b 69 6e 67 20 0a 2a 2a 20 66 75 6e 63 74 69 cking .** functi
180f5 6f 6e 73 20 6e 65 65 64 20 62 65 20 63 61 6c 6c ons need be call
180f6 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
180f7 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 subjInMemory ar
180f8 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 2d 7a 65 gument is non-ze
180f9 72 6f 2c 20 74 68 65 6e 20 61 6e 79 20 73 75 62 ro, then any sub
180fa 2d 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 65 64 0a -journal opened.
180fb 2a 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 ** within this t
180fc 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 ransaction will
180fd 62 65 20 6f 70 65 6e 65 64 20 61 73 20 61 6e 20 be opened as an
180fe 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2e 20 in-memory file.
180ff 54 68 69 73 0a 2a 2a 20 68 61 73 20 6e 6f 20 65 This.** has no e
18100 66 66 65 63 74 20 69 66 20 74 68 65 20 73 75 62 ffect if the sub
18101 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 72 65 -journal is alre
18102 61 64 79 20 6f 70 65 6e 65 64 20 28 61 73 20 69 ady opened (as i
18103 74 20 6d 61 79 20 62 65 20 77 68 65 6e 0a 2a 2a t may be when.**
18104 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c running in excl
18105 75 73 69 76 65 20 6d 6f 64 65 29 20 6f 72 20 69 usive mode) or i
18106 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f f the transactio
18107 6e 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 n does not requi
18108 72 65 20 61 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 re a.** sub-jour
18109 6e 61 6c 2e 20 49 66 20 74 68 65 20 73 75 62 6a nal. If the subj
1810a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65 6e InMemory argumen
1810b 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 t is zero, then
1810c 61 6e 79 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 any required.**
1810d 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 69 sub-journal is i
1810e 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 2d 6d 65 mplemented in-me
1810f 6d 6f 72 79 20 69 66 20 70 50 61 67 65 72 20 69 mory if pPager i
18110 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 s an in-memory d
18111 61 74 61 62 61 73 65 2c 20 0a 2a 2a 20 6f 72 20 atabase, .** or
18112 75 73 69 6e 67 20 61 20 74 65 6d 70 6f 72 61 72 using a temporar
18113 79 20 66 69 6c 65 20 6f 74 68 65 72 77 69 73 65 y file otherwise
18114 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18115 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
18116 61 67 65 72 42 65 67 69 6e 28 50 61 67 65 72 20 agerBegin(Pager
18117 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 78 46 *pPager, int exF
18118 6c 61 67 2c 20 69 6e 74 20 73 75 62 6a 49 6e 4d lag, int subjInM
18119 65 6d 6f 72 79 29 7b 0a 20 20 69 6e 74 20 72 63 emory){. int rc
1811a 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
1811b 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 if( pPager->err
1811c 43 6f 64 65 20 29 20 72 65 74 75 72 6e 20 70 50 Code ) return pP
1811d 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 ager->errCode;.
1811e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
1811f 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 >eState>=PAGER_R
18120 45 41 44 45 52 20 26 26 20 70 50 61 67 65 72 2d EADER && pPager-
18121 3e 65 53 74 61 74 65 3c 50 41 47 45 52 5f 45 52 >eState<PAGER_ER
18122 52 4f 52 20 29 3b 0a 20 20 70 50 61 67 65 72 2d ROR );. pPager-
18123 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 3d 20 >subjInMemory =
18124 28 75 38 29 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 (u8)subjInMemory
18125 3b 0a 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 ;.. if( ALWAYS(
18126 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d pPager->eState==
18127 50 41 47 45 52 5f 52 45 41 44 45 52 29 20 29 7b PAGER_READER) ){
18128 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
18129 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d ger->pInJournal=
1812a 3d 30 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 =0 );.. if( p
1812b 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 agerUseWal(pPage
1812c 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 r) ){. /* I
1812d 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 63 f the pager is c
1812e 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 75 73 65 onfigured to use
1812f 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78 locking_mode=ex
18130 63 6c 75 73 69 76 65 2c 20 61 6e 64 20 61 6e 0a clusive, and an.
18131 20 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 73 69 ** exclusi
18132 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 ve lock on the d
18133 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 61 atabase is not a
18134 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 lready held, obt
18135 61 69 6e 20 69 74 20 6e 6f 77 2e 0a 20 20 20 20 ain it now..
18136 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 */. if( p
18137 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 Pager->exclusive
18138 4d 6f 64 65 20 26 26 20 73 71 6c 69 74 65 33 57 Mode && sqlite3W
18139 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f 64 65 28 alExclusiveMode(
1813a 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 2d 31 pPager->pWal, -1
1813b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 ) ){. rc
1813c 3d 20 70 61 67 65 72 4c 6f 63 6b 44 62 28 70 50 = pagerLockDb(pP
1813d 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f ager, EXCLUSIVE_
1813e 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 LOCK);. i
1813f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
18140 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
18141 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
18142 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
18143 65 33 57 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f e3WalExclusiveMo
18144 64 65 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c de(pPager->pWal,
18145 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 1);. }..
18146 20 20 20 20 2f 2a 20 47 72 61 62 20 74 68 65 20 /* Grab the
18147 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 write lock on th
18148 65 20 6c 6f 67 20 66 69 6c 65 2e 20 49 66 20 73 e log file. If s
18149 75 63 63 65 73 73 66 75 6c 2c 20 75 70 67 72 61 uccessful, upgra
1814a 64 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 50 de to. ** P
1814b 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 74 AGER_RESERVED st
1814c 61 74 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ate. Otherwise,
1814d 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
1814e 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c code to the call
1814f 65 72 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 er.. ** The
18150 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 73 busy-handler is
18151 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 not invoked if
18152 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 another connecti
18153 6f 6e 20 61 6c 72 65 61 64 79 0a 20 20 20 20 20 on already.
18154 20 2a 2a 20 68 6f 6c 64 73 20 74 68 65 20 77 72 ** holds the wr
18155 69 74 65 2d 6c 6f 63 6b 2e 20 49 66 20 70 6f 73 ite-lock. If pos
18156 73 69 62 6c 65 2c 20 74 68 65 20 75 70 70 65 72 sible, the upper
18157 20 6c 61 79 65 72 20 77 69 6c 6c 20 63 61 6c 6c layer will call
18158 20 69 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 it.. */.
18159 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1815a 57 61 6c 42 65 67 69 6e 57 72 69 74 65 54 72 61 WalBeginWriteTra
1815b 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2d nsaction(pPager-
1815c 3e 70 57 61 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 >pWal);. }els
1815d 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 e{. /* Obta
1815e 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f in a RESERVED lo
1815f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
18160 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 se file. If the
18161 65 78 46 6c 61 67 20 70 61 72 61 6d 65 74 65 72 exFlag parameter
18162 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74 72 75 . ** is tru
18163 65 2c 20 74 68 65 6e 20 69 6d 6d 65 64 69 61 74 e, then immediat
18164 65 6c 79 20 75 70 67 72 61 64 65 20 74 68 69 73 ely upgrade this
18165 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 to an EXCLUSIVE
18166 20 6c 6f 63 6b 2e 20 54 68 65 0a 20 20 20 20 20 lock. The.
18167 20 2a 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 ** busy-handler
18168 20 63 61 6c 6c 62 61 63 6b 20 63 61 6e 20 62 65 callback can be
18169 20 75 73 65 64 20 77 68 65 6e 20 75 70 67 72 61 used when upgra
1816a 64 69 6e 67 20 74 6f 20 74 68 65 20 45 58 43 4c ding to the EXCL
1816b 55 53 49 56 45 0a 20 20 20 20 20 20 2a 2a 20 6c USIVE. ** l
1816c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20 77 68 65 ock, but not whe
1816d 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74 68 65 20 n obtaining the
1816e 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 0a 20 RESERVED lock..
1816f 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 */. rc
18170 20 3d 20 70 61 67 65 72 4c 6f 63 6b 44 62 28 70 = pagerLockDb(p
18171 50 61 67 65 72 2c 20 52 45 53 45 52 56 45 44 5f Pager, RESERVED_
18172 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 LOCK);. if(
18173 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
18174 26 20 65 78 46 6c 61 67 20 29 7b 0a 20 20 20 20 & exFlag ){.
18175 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 rc = pager_w
18176 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 ait_on_lock(pPag
18177 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f er, EXCLUSIVE_LO
18178 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 CK);. }.
18179 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d }.. if( rc==
1817a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1817b 20 20 20 2f 2a 20 43 68 61 6e 67 65 20 74 6f 20 /* Change to
1817c 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 73 74 WRITER_LOCKED st
1817d 61 74 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 ate.. **.
1817e 20 20 20 20 2a 2a 20 57 41 4c 20 6d 6f 64 65 20 ** WAL mode
1817f 73 65 74 73 20 50 61 67 65 72 2e 65 53 74 61 74 sets Pager.eStat
18180 65 20 74 6f 20 50 41 47 45 52 5f 57 52 49 54 45 e to PAGER_WRITE
18181 52 5f 4c 4f 43 4b 45 44 20 6f 72 20 43 41 43 48 R_LOCKED or CACH
18182 45 4d 4f 44 0a 20 20 20 20 20 20 2a 2a 20 77 68 EMOD. ** wh
18183 65 6e 20 69 74 20 68 61 73 20 61 6e 20 6f 70 65 en it has an ope
18184 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 62 n transaction, b
18185 75 74 20 6e 65 76 65 72 20 74 6f 20 44 42 4d 4f ut never to DBMO
18186 44 20 6f 72 20 46 49 4e 49 53 48 45 44 2e 0a 20 D or FINISHED..
18187 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 ** This is
18188 62 65 63 61 75 73 65 20 69 6e 20 74 68 6f 73 65 because in those
18189 20 73 74 61 74 65 73 20 74 68 65 20 63 6f 64 65 states the code
1818a 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 73 61 to roll back sa
1818b 76 65 70 6f 69 6e 74 20 0a 20 20 20 20 20 20 2a vepoint . *
1818c 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 6d * transactions m
1818d 61 79 20 63 6f 70 79 20 64 61 74 61 20 66 72 6f ay copy data fro
1818e 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 m the sub-journa
1818f 6c 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 l into the datab
18190 61 73 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 ase . ** fi
18191 6c 65 20 61 73 20 77 65 6c 6c 20 61 73 20 69 6e le as well as in
18192 74 6f 20 74 68 65 20 70 61 67 65 20 63 61 63 68 to the page cach
18193 65 2e 20 57 68 69 63 68 20 77 6f 75 6c 64 20 62 e. Which would b
18194 65 20 69 6e 63 6f 72 72 65 63 74 20 69 6e 20 0a e incorrect in .
18195 20 20 20 20 20 20 2a 2a 20 57 41 4c 20 6d 6f 64 ** WAL mod
18196 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
18197 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 pPager->eState
18198 20 3d 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f = PAGER_WRITER_
18199 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 20 20 70 50 LOCKED;. pP
1819a 61 67 65 72 2d 3e 64 62 48 69 6e 74 53 69 7a 65 ager->dbHintSize
1819b 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a = pPager->dbSiz
1819c 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d e;. pPager-
1819d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 50 >dbFileSize = pP
1819e 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 ager->dbSize;.
1819f 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 pPager->dbOr
181a0 69 67 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d igSize = pPager-
181a1 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 >dbSize;. p
181a2 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
181a3 66 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 f = 0;. }..
181a4 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
181a5 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 LITE_OK || pPage
181a6 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 r->eState==PAGER
181a7 5f 52 45 41 44 45 52 20 29 3b 0a 20 20 20 20 61 _READER );. a
181a8 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
181a9 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e E_OK || pPager->
181aa 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 eState==PAGER_WR
181ab 49 54 45 52 5f 4c 4f 43 4b 45 44 20 29 3b 0a 20 ITER_LOCKED );.
181ac 20 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 assert( asser
181ad 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 t_pager_state(pP
181ae 61 67 65 72 29 20 29 3b 0a 20 20 7d 0a 0a 20 20 ager) );. }..
181af 50 41 47 45 52 54 52 41 43 45 28 28 22 54 52 41 PAGERTRACE(("TRA
181b0 4e 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22 2c 20 NSACTION %d\n",
181b1 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 PAGERID(pPager))
181b2 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
181b3 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 }../*.** Mark a
181b4 73 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 65 single data page
181b5 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 as writeable. T
181b6 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 he page is writt
181b7 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 en into the .**
181b8 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 main journal or
181b9 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 72 sub-journal as r
181ba 65 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 20 equired. If the
181bb 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 page is written
181bc 69 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 into.** one of t
181bd 68 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 he journals, the
181be 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 corresponding b
181bf 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 it is set in the
181c0 20 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f .** Pager.pInJo
181c1 75 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e 64 urnal bitvec and
181c2 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f the PagerSavepo
181c3 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 int.pInSavepoint
181c4 20 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 61 bitvecs.** of a
181c5 6e 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e ny open savepoin
181c6 74 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 ts as appropriat
181c7 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
181c8 20 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 48 pager_write(PgH
181c9 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64 dr *pPg){. void
181ca 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 *pData = pPg->p
181cb 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70 Data;. Pager *p
181cc 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 Pager = pPg->pPa
181cd 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ger;. int rc =
181ce 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a SQLITE_OK;.. /*
181cf 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
181d0 20 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e 6c 65 not called unle
181d1 73 73 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 ss a write-trans
181d2 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 action has alrea
181d3 64 79 20 0a 20 20 2a 2a 20 62 65 65 6e 20 73 74 dy . ** been st
181d4 61 72 74 65 64 2e 20 54 68 65 20 6a 6f 75 72 6e arted. The journ
181d5 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f 72 20 6d al file may or m
181d6 61 79 20 6e 6f 74 20 62 65 20 6f 70 65 6e 20 61 ay not be open a
181d7 74 20 74 68 69 73 20 70 6f 69 6e 74 2e 0a 20 20 t this point..
181d8 2a 2a 20 49 74 20 69 73 20 6e 65 76 65 72 20 63 ** It is never c
181d9 61 6c 6c 65 64 20 69 6e 20 74 68 65 20 45 52 52 alled in the ERR
181da 4f 52 20 73 74 61 74 65 2e 0a 20 20 2a 2f 0a 20 OR state.. */.
181db 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
181dc 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 >eState==PAGER_W
181dd 52 49 54 45 52 5f 4c 4f 43 4b 45 44 0a 20 20 20 RITER_LOCKED.
181de 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 || pPager->e
181df 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 State==PAGER_WRI
181e0 54 45 52 5f 43 41 43 48 45 4d 4f 44 0a 20 20 20 TER_CACHEMOD.
181e1 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 || pPager->e
181e2 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 State==PAGER_WRI
181e3 54 45 52 5f 44 42 4d 4f 44 0a 20 20 29 3b 0a 20 TER_DBMOD. );.
181e4 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f assert( assert_
181e5 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 pager_state(pPag
181e6 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 er) );.. /* If
181e7 61 6e 20 65 72 72 6f 72 20 68 61 73 20 62 65 65 an error has bee
181e8 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 64 65 74 n previously det
181e9 65 63 74 65 64 2c 20 72 65 70 6f 72 74 20 74 68 ected, report th
181ea 65 20 73 61 6d 65 20 65 72 72 6f 72 0a 20 20 2a e same error. *
181eb 2a 20 61 67 61 69 6e 2e 20 54 68 69 73 20 73 68 * again. This sh
181ec 6f 75 6c 64 20 6e 6f 74 20 68 61 70 70 65 6e 2c ould not happen,
181ed 20 62 75 74 20 74 68 65 20 63 68 65 63 6b 20 70 but the check p
181ee 72 6f 76 69 64 65 73 20 72 6f 62 75 73 74 6e 65 rovides robustne
181ef 73 73 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 ss. */. if( NEV
181f0 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f ER(pPager->errCo
181f1 64 65 29 20 29 20 20 72 65 74 75 72 6e 20 70 50 de) ) return pP
181f2 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a ager->errCode;..
181f3 20 20 2f 2a 20 48 69 67 68 65 72 2d 6c 65 76 65 /* Higher-leve
181f4 6c 20 72 6f 75 74 69 6e 65 73 20 6e 65 76 65 72 l routines never
181f5 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 call this funct
181f6 69 6f 6e 20 69 66 20 64 61 74 61 62 61 73 65 20 ion if database
181f7 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 77 72 69 74 is not. ** writ
181f8 61 62 6c 65 2e 20 20 42 75 74 20 63 68 65 63 6b able. But check
181f9 20 61 6e 79 77 61 79 2c 20 6a 75 73 74 20 66 6f anyway, just fo
181fa 72 20 72 6f 62 75 73 74 6e 65 73 73 2e 20 2a 2f r robustness. */
181fb 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 . if( NEVER(pPa
181fc 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 ger->readOnly) )
181fd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 return SQLITE_P
181fe 45 52 4d 3b 0a 0a 20 20 43 48 45 43 4b 5f 50 41 ERM;.. CHECK_PA
181ff 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 54 GE(pPg);.. /* T
18200 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
18201 6e 65 65 64 73 20 74 6f 20 62 65 20 6f 70 65 6e needs to be open
18202 65 64 2e 20 48 69 67 68 65 72 20 6c 65 76 65 6c ed. Higher level
18203 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20 61 routines have a
18204 6c 72 65 61 64 79 0a 20 20 2a 2a 20 6f 62 74 61 lready. ** obta
18205 69 6e 65 64 20 74 68 65 20 6e 65 63 65 73 73 61 ined the necessa
18206 72 79 20 6c 6f 63 6b 73 20 74 6f 20 62 65 67 69 ry locks to begi
18207 6e 20 74 68 65 20 77 72 69 74 65 2d 74 72 61 6e n the write-tran
18208 73 61 63 74 69 6f 6e 2c 20 62 75 74 20 74 68 65 saction, but the
18209 0a 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6a . ** rollback j
1820a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 6e 6f 74 ournal might not
1820b 20 79 65 74 20 62 65 20 6f 70 65 6e 2e 20 4f 70 yet be open. Op
1820c 65 6e 20 69 74 20 6e 6f 77 20 69 66 20 74 68 69 en it now if thi
1820d 73 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 s is the case..
1820e 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 **. ** This is
1820f 20 64 6f 6e 65 20 62 65 66 6f 72 65 20 63 61 6c done before cal
18210 6c 69 6e 67 20 73 71 6c 69 74 65 33 50 63 61 63 ling sqlite3Pcac
18211 68 65 4d 61 6b 65 44 69 72 74 79 28 29 20 6f 6e heMakeDirty() on
18212 20 74 68 65 20 70 61 67 65 2e 20 0a 20 20 2a 2a the page. . **
18213 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 69 Otherwise, if i
18214 74 20 77 65 72 65 20 64 6f 6e 65 20 61 66 74 65 t were done afte
18215 72 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 r calling sqlite
18216 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 3PcacheMakeDirty
18217 28 29 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 61 6e (), then. ** an
18218 20 65 72 72 6f 72 20 6d 69 67 68 74 20 6f 63 63 error might occ
18219 75 72 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 ur and the pager
1821a 20 77 6f 75 6c 64 20 65 6e 64 20 75 70 20 69 6e would end up in
1821b 20 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 73 WRITER_LOCKED s
1821c 74 61 74 65 0a 20 20 2a 2a 20 77 69 74 68 20 70 tate. ** with p
1821d 61 67 65 73 20 6d 61 72 6b 65 64 20 61 73 20 64 ages marked as d
1821e 69 72 74 79 20 69 6e 20 74 68 65 20 63 61 63 68 irty in the cach
1821f 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 e.. */. if( pP
18220 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 ager->eState==PA
18221 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 GER_WRITER_LOCKE
18222 44 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 D ){. rc = pa
18223 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c ger_open_journal
18224 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 (pPager);. if
18225 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
18226 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d ) return rc;. }
18227 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
18228 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52 r->eState>=PAGER
18229 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 _WRITER_CACHEMOD
1822a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 73 );. assert( as
1822b 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 sert_pager_state
1822c 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 2f (pPager) );.. /
1822d 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 * Mark the page
1822e 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 74 68 as dirty. If th
1822f 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 61 e page has alrea
18230 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a dy been written.
18231 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72 ** to the jour
18232 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 nal then we can
18233 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61 return right awa
18234 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 y.. */. sqlite
18235 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 3PcacheMakeDirty
18236 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61 67 (pPg);. if( pag
18237 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 eInJournal(pPg)
18238 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 65 73 && !subjRequires
18239 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 Page(pPg) ){.
1823a 20 61 73 73 65 72 74 28 20 21 70 61 67 65 72 55 assert( !pagerU
1823b 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 3b seWal(pPager) );
1823c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 0a 20 20 20 . }else{. .
1823d 20 2f 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 /* The transact
1823e 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 ion journal now
1823f 65 78 69 73 74 73 20 61 6e 64 20 77 65 20 68 61 exists and we ha
18240 76 65 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 ve a RESERVED or
18241 20 61 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 an. ** EXCLU
18242 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 SIVE lock on the
18243 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
18244 69 6c 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 ile. Write the
18245 63 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a current page to.
18246 20 20 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 ** the trans
18247 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 action journal i
18248 66 20 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 f it is not ther
18249 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a e already.. *
1824a 2f 0a 20 20 20 20 69 66 28 20 21 70 61 67 65 49 /. if( !pageI
1824b 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 nJournal(pPg) &&
1824c 20 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 !pagerUseWal(pP
1824d 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 61 ager) ){. a
1824e 73 73 65 72 74 28 20 70 61 67 65 72 55 73 65 57 ssert( pagerUseW
1824f 61 6c 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b al(pPager)==0 );
18250 0a 20 20 20 20 20 20 69 66 28 20 70 50 67 2d 3e . if( pPg->
18251 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 pgno<=pPager->db
18252 4f 72 69 67 53 69 7a 65 20 26 26 20 69 73 4f 70 OrigSize && isOp
18253 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
18254 29 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 ){. u32 c
18255 6b 73 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 ksum;. ch
18256 61 72 20 2a 70 44 61 74 61 32 3b 0a 20 20 20 20 ar *pData2;.
18257 20 20 20 20 69 36 34 20 69 4f 66 66 20 3d 20 70 i64 iOff = p
18258 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
18259 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 f;.. /* W
1825a 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 77 e should never w
1825b 72 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 rite to the jour
1825c 6e 61 6c 20 66 69 6c 65 20 74 68 65 20 70 61 67 nal file the pag
1825d 65 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a e that. *
1825e 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 * contains the d
1825f 61 74 61 62 61 73 65 20 6c 6f 63 6b 73 2e 20 20 atabase locks.
18260 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 The following as
18261 73 65 72 74 20 76 65 72 69 66 69 65 73 0a 20 20 sert verifies.
18262 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65 ** that we
18263 20 64 6f 20 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20 do not. */.
18264 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d assert( pPg-
18265 3e 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f >pgno!=PAGER_MJ_
18266 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a PGNO(pPager) );.
18267 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
18268 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
18269 48 64 72 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 Hdr<=pPager->jou
1826a 72 6e 61 6c 4f 66 66 20 29 3b 0a 20 20 20 20 20 rnalOff );.
1826b 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 CODEC2(pPager
1826c 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 , pData, pPg->pg
1826d 6e 6f 2c 20 37 2c 20 72 65 74 75 72 6e 20 53 51 no, 7, return SQ
1826e 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 LITE_NOMEM, pDat
1826f 61 32 29 3b 0a 20 20 20 20 20 20 20 20 63 6b 73 a2);. cks
18270 75 6d 20 3d 20 70 61 67 65 72 5f 63 6b 73 75 6d um = pager_cksum
18271 28 70 50 61 67 65 72 2c 20 28 75 38 2a 29 70 44 (pPager, (u8*)pD
18272 61 74 61 32 29 3b 0a 0a 20 20 20 20 20 20 20 20 ata2);..
18273 2f 2a 20 45 76 65 6e 20 69 66 20 61 6e 20 49 4f /* Even if an IO
18274 20 6f 72 20 64 69 73 6b 66 75 6c 6c 20 65 72 72 or diskfull err
18275 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
18276 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68 65 0a journalling the.
18277 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 ** page
18278 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61 62 6f in the block abo
18279 76 65 2c 20 73 65 74 20 74 68 65 20 6e 65 65 64 ve, set the need
1827a 2d 73 79 6e 63 20 66 6c 61 67 20 66 6f 72 20 74 -sync flag for t
1827b 68 65 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 he page..
1827c 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 ** Otherwise, w
1827d 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 hen the transact
1827e 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 ion is rolled ba
1827f 63 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20 69 6e ck, the logic in
18280 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6c 61 79 . ** play
18281 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 back_one_page()
18282 77 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61 74 20 will think that
18283 74 68 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 the page needs t
18284 6f 20 62 65 20 72 65 73 74 6f 72 65 64 0a 20 20 o be restored.
18285 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 ** in the
18286 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41 database file. A
18287 6e 64 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f nd if an IO erro
18288 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 r occurs while d
18289 6f 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20 20 20 oing so,.
1828a 20 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75 70 74 ** then corrupt
1828b 69 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 2e 0a ion may follow..
1828c 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1828d 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d pPg->flags |=
1828e 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
1828f 3b 0a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ;.. rc =
18290 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 write32bits(pPag
18291 65 72 2d 3e 6a 66 64 2c 20 69 4f 66 66 2c 20 70 er->jfd, iOff, p
18292 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 Pg->pgno);.
18293 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
18294 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 E_OK ) return rc
18295 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
18296 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 qlite3OsWrite(pP
18297 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 61 74 61 ager->jfd, pData
18298 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 2, pPager->pageS
18299 69 7a 65 2c 20 69 4f 66 66 2b 34 29 3b 0a 20 20 ize, iOff+4);.
1829a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1829b 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e LITE_OK ) return
1829c 20 72 63 3b 0a 20 20 20 20 20 20 20 20 72 63 20 rc;. rc
1829d 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 = write32bits(pP
1829e 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4f 66 66 2b ager->jfd, iOff+
1829f 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
182a0 2b 34 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 20 +4, cksum);.
182a1 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
182a2 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 TE_OK ) return r
182a3 63 3b 0a 0a 20 20 20 20 20 20 20 20 49 4f 54 52 c;.. IOTR
182a4 41 43 45 28 28 22 4a 4f 55 54 20 25 70 20 25 64 ACE(("JOUT %p %d
182a5 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 %lld %d\n", pPa
182a6 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 ger, pPg->pgno,
182a7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
182a8 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
182a9 6c 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 lOff, pPager->pa
182aa 67 65 53 69 7a 65 29 29 3b 0a 20 20 20 20 20 20 geSize));.
182ab 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c PAGER_INCR(sql
182ac 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 ite3_pager_write
182ad 6a 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 j_count);.
182ae 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4a PAGERTRACE(("J
182af 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 OURNAL %d page %
182b0 64 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68 61 d needSync=%d ha
182b1 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 sh(%08x)\n",.
182b2 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 PAGERI
182b3 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e D(pPager), pPg->
182b4 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 pgno, .
182b5 20 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 ((pPg->flags
182b6 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
182b7 29 3f 31 3a 30 29 2c 20 70 61 67 65 72 5f 70 61 )?1:0), pager_pa
182b8 67 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a gehash(pPg)));..
182b9 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
182ba 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 38 20 journalOff += 8
182bb 2b 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 + pPager->pageSi
182bc 7a 65 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 ze;. pPag
182bd 65 72 2d 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20 er->nRec++;.
182be 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
182bf 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d er->pInJournal!=
182c0 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 0 );. rc
182c1 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 = sqlite3BitvecS
182c2 65 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f et(pPager->pInJo
182c3 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f urnal, pPg->pgno
182c4 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 );. testc
182c5 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ase( rc==SQLITE_
182c6 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 NOMEM );.
182c7 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
182c8 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 ITE_OK || rc==SQ
182c9 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 LITE_NOMEM );.
182ca 20 20 20 20 20 20 72 63 20 7c 3d 20 61 64 64 54 rc |= addT
182cb 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 oSavepointBitvec
182cc 73 28 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 s(pPager, pPg->p
182cd 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 gno);. if
182ce 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
182cf 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 ){. ass
182d0 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
182d1 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 NOMEM );.
182d2 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
182d3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
182d4 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 lse{. if(
182d5 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21 pPager->eState!
182d6 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 44 42 =PAGER_WRITER_DB
182d7 4d 4f 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 MOD ){.
182d8 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 pPg->flags |= P
182d9 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a GHDR_NEED_SYNC;.
182da 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
182db 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 41 PAGERTRACE(("A
182dc 50 50 45 4e 44 20 25 64 20 70 61 67 65 20 25 64 PPEND %d page %d
182dd 20 6e 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c needSync=%d\n",
182de 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
182df 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
182e0 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 , pPg->pgno,.
182e1 20 20 20 20 20 20 20 20 20 20 20 20 28 28 70 50 ((pP
182e2 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
182e3 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 29 EED_SYNC)?1:0)))
182e4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
182e5 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
182e6 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e statement journ
182e7 61 6c 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 al is open and t
182e8 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 he page is not i
182e9 6e 20 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 n it,. ** the
182ea 6e 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72 n write the curr
182eb 65 6e 74 20 70 61 67 65 20 74 6f 20 74 68 65 20 ent page to the
182ec 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 statement journa
182ed 6c 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 l. Note that.
182ee 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 ** the stateme
182ef 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 nt journal forma
182f0 74 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74 t differs from t
182f1 68 65 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72 he standard jour
182f2 6e 61 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a nal format. *
182f3 2a 20 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69 * in that it omi
182f4 74 73 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 ts the checksums
182f5 20 61 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e and the header.
182f6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
182f7 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 subjRequiresPage
182f8 28 70 50 67 29 20 29 7b 0a 20 20 20 20 20 20 72 (pPg) ){. r
182f9 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 c = subjournalPa
182fa 67 65 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 ge(pPg);. }.
182fb 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 }.. /* Update
182fc 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a the database siz
182fd 65 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 e and return..
182fe 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d */. if( pPager-
182ff 3e 64 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e >dbSize<pPg->pgn
18300 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d o ){. pPager-
18301 3e 64 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70 >dbSize = pPg->p
18302 67 6e 6f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 gno;. }. retur
18303 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d n rc;.}../*.** M
18304 61 72 6b 20 61 20 64 61 74 61 20 70 61 67 65 20 ark a data page
18305 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 as writeable. Th
18306 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 is routine must
18307 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 be called before
18308 20 0a 2a 2a 20 6d 61 6b 69 6e 67 20 63 68 61 6e .** making chan
18309 67 65 73 20 74 6f 20 61 20 70 61 67 65 2e 20 54 ges to a page. T
1830a 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 he caller must c
1830b 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 heck the return
1830c 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 74 68 69 value .** of thi
1830d 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 62 s function and b
1830e 65 20 63 61 72 65 66 75 6c 20 6e 6f 74 20 74 6f e careful not to
1830f 20 63 68 61 6e 67 65 20 61 6e 79 20 70 61 67 65 change any page
18310 20 64 61 74 61 20 75 6e 6c 65 73 73 20 0a 2a 2a data unless .**
18311 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
18312 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e turns SQLITE_OK.
18313 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 .**.** The diffe
18314 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 rence between th
18315 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 is function and
18316 70 61 67 65 72 5f 77 72 69 74 65 28 29 20 69 73 pager_write() is
18317 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 66 75 that this.** fu
18318 6e 63 74 69 6f 6e 20 61 6c 73 6f 20 64 65 61 6c nction also deal
18319 73 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69 s with the speci
1831a 61 6c 20 63 61 73 65 20 77 68 65 72 65 20 32 20 al case where 2
1831b 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 0a 2a 2a or more pages.**
1831c 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 fit on a single
1831d 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 49 6e disk sector. In
1831e 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c 20 63 this case all c
1831f 6f 2d 72 65 73 69 64 65 6e 74 20 70 61 67 65 73 o-resident pages
18320 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 .** must have be
18321 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 en written to th
18322 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 e journal file b
18323 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e efore returning.
18324 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 .**.** If an err
18325 6f 72 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 or occurs, SQLIT
18326 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f E_NOMEM or an IO
18327 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
18328 65 74 75 72 6e 65 64 0a 2a 2a 20 61 73 20 61 70 eturned.** as ap
18329 70 72 6f 70 72 69 61 74 65 2e 20 4f 74 68 65 72 propriate. Other
1832a 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e wise, SQLITE_OK.
1832b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1832c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
1832d 67 65 72 57 72 69 74 65 28 44 62 50 61 67 65 20 gerWrite(DbPage
1832e 2a 70 44 62 50 61 67 65 29 7b 0a 20 20 69 6e 74 *pDbPage){. int
1832f 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
18330 0a 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d .. PgHdr *pPg =
18331 20 70 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 pDbPage;. Page
18332 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d r *pPager = pPg-
18333 3e 70 50 61 67 65 72 3b 0a 20 20 50 67 6e 6f 20 >pPager;. Pgno
18334 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 20 3d nPagePerSector =
18335 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 (pPager->sector
18336 53 69 7a 65 2f 70 50 61 67 65 72 2d 3e 70 61 67 Size/pPager->pag
18337 65 53 69 7a 65 29 3b 0a 0a 20 20 61 73 73 65 72 eSize);.. asser
18338 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 t( pPager->eStat
18339 65 3e 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f e>=PAGER_WRITER_
1833a 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 61 73 73 65 LOCKED );. asse
1833b 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 rt( pPager->eSta
1833c 74 65 21 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 te!=PAGER_ERROR
1833d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 );. assert( ass
1833e 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 ert_pager_state(
1833f 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 69 66 pPager) );.. if
18340 28 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 ( nPagePerSector
18341 3e 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e >1 ){. Pgno n
18342 50 61 67 65 43 6f 75 6e 74 3b 20 20 20 20 20 20 PageCount;
18343 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
18344 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
18345 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
18346 0a 20 20 20 20 50 67 6e 6f 20 70 67 31 3b 20 20 . Pgno pg1;
18347 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18348 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 * First page of
18349 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 69 the sector pPg i
1834a 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20 2a 2f s located on. */
1834b 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d . int nPage =
1834c 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
1834d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
1834e 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67 s starting at pg
1834f 31 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 1 to journal */.
18350 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 int ii;
18351 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18352 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
18353 0a 20 20 20 20 69 6e 74 20 6e 65 65 64 53 79 6e . int needSyn
18354 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f c = 0; /
18355 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 * True if any pa
18356 67 65 20 68 61 73 20 50 47 48 44 52 5f 4e 45 45 ge has PGHDR_NEE
18357 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20 20 20 20 2f D_SYNC */.. /
18358 2a 20 53 65 74 20 74 68 65 20 64 6f 4e 6f 74 53 * Set the doNotS
18359 79 6e 63 53 70 69 6c 6c 20 66 6c 61 67 20 74 6f yncSpill flag to
1835a 20 31 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 1. This is beca
1835b 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 20 61 6c use we cannot al
1835c 6c 6f 77 0a 20 20 20 20 2a 2a 20 61 20 6a 6f 75 low. ** a jou
1835d 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 62 rnal header to b
1835e 65 20 77 72 69 74 74 65 6e 20 62 65 74 77 65 65 e written betwee
1835f 6e 20 74 68 65 20 70 61 67 65 73 20 6a 6f 75 72 n the pages jour
18360 6e 61 6c 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 naled by. **
18361 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 this function..
18362 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
18363 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 ( !MEMDB );.
18364 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
18365 64 6f 4e 6f 74 53 79 6e 63 53 70 69 6c 6c 3d 3d doNotSyncSpill==
18366 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 0 );. pPager-
18367 3e 64 6f 4e 6f 74 53 79 6e 63 53 70 69 6c 6c 2b >doNotSyncSpill+
18368 2b 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 +;.. /* This
18369 74 72 69 63 6b 20 61 73 73 75 6d 65 73 20 74 68 trick assumes th
1836a 61 74 20 62 6f 74 68 20 74 68 65 20 70 61 67 65 at both the page
1836b 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 -size and sector
1836c 2d 73 69 7a 65 20 61 72 65 0a 20 20 20 20 2a 2a -size are. **
1836d 20 61 6e 20 69 6e 74 65 67 65 72 20 70 6f 77 65 an integer powe
1836e 72 20 6f 66 20 32 2e 20 49 74 20 73 65 74 73 20 r of 2. It sets
1836f 76 61 72 69 61 62 6c 65 20 70 67 31 20 74 6f 20 variable pg1 to
18370 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 0a 20 the identifier.
18371 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 ** of the fir
18372 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73 st page of the s
18373 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 ector pPg is loc
18374 61 74 65 64 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a ated on.. */.
18375 20 20 20 20 70 67 31 20 3d 20 28 28 70 50 67 2d pg1 = ((pPg-
18376 3e 70 67 6e 6f 2d 31 29 20 26 20 7e 28 6e 50 61 >pgno-1) & ~(nPa
18377 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29 29 20 gePerSector-1))
18378 2b 20 31 3b 0a 0a 20 20 20 20 6e 50 61 67 65 43 + 1;.. nPageC
18379 6f 75 6e 74 20 3d 20 70 50 61 67 65 72 2d 3e 64 ount = pPager->d
1837a 62 53 69 7a 65 3b 0a 20 20 20 20 69 66 28 20 70 bSize;. if( p
1837b 50 67 2d 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f Pg->pgno>nPageCo
1837c 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 unt ){. nPa
1837d 67 65 20 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20 ge = (pPg->pgno
1837e 2d 20 70 67 31 29 2b 31 3b 0a 20 20 20 20 7d 65 - pg1)+1;. }e
1837f 6c 73 65 20 69 66 28 20 28 70 67 31 2b 6e 50 61 lse if( (pg1+nPa
18380 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29 3e 6e gePerSector-1)>n
18381 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 PageCount ){.
18382 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 nPage = nPage
18383 43 6f 75 6e 74 2b 31 2d 70 67 31 3b 0a 20 20 20 Count+1-pg1;.
18384 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 }else{. nP
18385 61 67 65 20 3d 20 6e 50 61 67 65 50 65 72 53 65 age = nPagePerSe
18386 63 74 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ctor;. }.
18387 61 73 73 65 72 74 28 6e 50 61 67 65 3e 30 29 3b assert(nPage>0);
18388 0a 20 20 20 20 61 73 73 65 72 74 28 70 67 31 3c . assert(pg1<
18389 3d 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 =pPg->pgno);.
1838a 20 61 73 73 65 72 74 28 28 70 67 31 2b 6e 50 61 assert((pg1+nPa
1838b 67 65 29 3e 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a ge)>pPg->pgno);.
1838c 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 . for(ii=0; i
1838d 69 3c 6e 50 61 67 65 20 26 26 20 72 63 3d 3d 53 i<nPage && rc==S
1838e 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b QLITE_OK; ii++){
1838f 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 20 3d . Pgno pg =
18390 20 70 67 31 2b 69 69 3b 0a 20 20 20 20 20 20 50 pg1+ii;. P
18391 67 48 64 72 20 2a 70 50 61 67 65 3b 0a 20 20 20 gHdr *pPage;.
18392 20 20 20 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e if( pg==pPg->
18393 70 67 6e 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33 pgno || !sqlite3
18394 42 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65 BitvecTest(pPage
18395 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 r->pInJournal, p
18396 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 g) ){. if
18397 28 20 70 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 ( pg!=PAGER_MJ_P
18398 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 GNO(pPager) ){.
18399 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
1839a 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50 lite3PagerGet(pP
1839b 61 67 65 72 2c 20 70 67 2c 20 26 70 50 61 67 65 ager, pg, &pPage
1839c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
1839d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1839e 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 {. rc
1839f 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 28 70 = pager_write(p
183a0 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
183a1 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c if( pPage->fl
183a2 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
183a3 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 YNC ){.
183a4 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 needSync =
183a5 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 1;. }
183a6 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
183a7 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
183a8 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
183a9 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
183aa 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 50 }else if( (pP
183ab 61 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b age = pager_look
183ac 75 70 28 70 50 61 67 65 72 2c 20 70 67 29 29 21 up(pPager, pg))!
183ad 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 =0 ){. if
183ae 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 ( pPage->flags&P
183af 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 GHDR_NEED_SYNC )
183b0 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 {. need
183b1 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 Sync = 1;.
183b2 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 }. sqli
183b3 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
183b4 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 age);. }.
183b5 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 }.. /* If t
183b6 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 he PGHDR_NEED_SY
183b7 4e 43 20 66 6c 61 67 20 69 73 20 73 65 74 20 66 NC flag is set f
183b8 6f 72 20 61 6e 79 20 6f 66 20 74 68 65 20 6e 50 or any of the nP
183b9 61 67 65 20 70 61 67 65 73 20 0a 20 20 20 20 2a age pages . *
183ba 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67 * starting at pg
183bb 31 2c 20 74 68 65 6e 20 69 74 20 6e 65 65 64 73 1, then it needs
183bc 20 74 6f 20 62 65 20 73 65 74 20 66 6f 72 20 61 to be set for a
183bd 6c 6c 20 6f 66 20 74 68 65 6d 2e 20 42 65 63 61 ll of them. Beca
183be 75 73 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 69 use. ** writi
183bf 6e 67 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 65 ng to any of the
183c0 73 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 6d se nPage pages m
183c1 61 79 20 64 61 6d 61 67 65 20 74 68 65 20 6f 74 ay damage the ot
183c2 68 65 72 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a hers, the. **
183c3 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 journal file mu
183c4 73 74 20 63 6f 6e 74 61 69 6e 20 73 79 6e 63 28 st contain sync(
183c5 29 65 64 20 63 6f 70 69 65 73 20 6f 66 20 61 6c )ed copies of al
183c6 6c 20 6f 66 20 74 68 65 6d 0a 20 20 20 20 2a 2a l of them. **
183c7 20 62 65 66 6f 72 65 20 61 6e 79 20 6f 66 20 74 before any of t
183c8 68 65 6d 20 63 61 6e 20 62 65 20 77 72 69 74 74 hem can be writt
183c9 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 en out to the da
183ca 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 tabase file..
183cb 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d */. if( rc==
183cc 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 65 65 SQLITE_OK && nee
183cd 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 61 dSync ){. a
183ce 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b ssert( !MEMDB );
183cf 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b . for(ii=0;
183d0 20 69 69 3c 6e 50 61 67 65 3b 20 69 69 2b 2b 29 ii<nPage; ii++)
183d1 7b 0a 20 20 20 20 20 20 20 20 50 67 48 64 72 20 {. PgHdr
183d2 2a 70 50 61 67 65 20 3d 20 70 61 67 65 72 5f 6c *pPage = pager_l
183d3 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 ookup(pPager, pg
183d4 31 2b 69 69 29 3b 0a 20 20 20 20 20 20 20 20 69 1+ii);. i
183d5 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 f( pPage ){.
183d6 20 20 20 20 20 20 70 50 61 67 65 2d 3e 66 6c 61 pPage->fla
183d7 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 gs |= PGHDR_NEED
183d8 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 _SYNC;.
183d9 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
183da 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ef(pPage);.
183db 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
183dc 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 }.. assert(
183dd 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e pPager->doNotSyn
183de 63 53 70 69 6c 6c 3d 3d 31 20 29 3b 0a 20 20 20 cSpill==1 );.
183df 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 pPager->doNotSy
183e0 6e 63 53 70 69 6c 6c 2d 2d 3b 0a 20 20 7d 65 6c ncSpill--;. }el
183e1 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 se{. rc = pag
183e2 65 72 5f 77 72 69 74 65 28 70 44 62 50 61 67 65 er_write(pDbPage
183e3 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
183e4 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 rc;.}../*.** Ret
183e5 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 urn TRUE if the
183e6 70 61 67 65 20 67 69 76 65 6e 20 69 6e 20 74 68 page given in th
183e7 65 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 70 e argument was p
183e8 72 65 76 69 6f 75 73 6c 79 20 70 61 73 73 65 64 reviously passed
183e9 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 .** to sqlite3Pa
183ea 67 65 72 57 72 69 74 65 28 29 2e 20 20 49 6e 20 gerWrite(). In
183eb 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65 74 other words, ret
183ec 75 72 6e 20 54 52 55 45 20 69 66 20 69 74 20 69 urn TRUE if it i
183ed 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 s ok.** to chang
183ee 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
183ef 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 the page..*/.#i
183f0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c fndef NDEBUG.SQL
183f1 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
183f2 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
183f3 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 20 2a iteable(DbPage *
183f4 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 pPg){. return p
183f5 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
183f6 44 49 52 54 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a DIRTY;.}.#endif.
183f7 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f ./*.** A call to
183f8 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 this routine te
183f9 6c 6c 73 20 74 68 65 20 70 61 67 65 72 20 74 68 lls the pager th
183fa 61 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 at it is not nec
183fb 65 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 essary to.** wri
183fc 74 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 te the informati
183fd 6f 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 20 62 on on page pPg b
183fe 61 63 6b 20 74 6f 20 74 68 65 20 64 69 73 6b 2c ack to the disk,
183ff 20 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 even though.**
18400 74 68 61 74 20 70 61 67 65 20 6d 69 67 68 74 20 that page might
18401 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 be marked as dir
18402 74 79 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e ty. This happen
18403 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 s, for example,
18404 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 when.** the page
18405 20 68 61 73 20 62 65 65 6e 20 61 64 64 65 64 20 has been added
18406 61 73 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 as a leaf of the
18407 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20 73 6f freelist and so
18408 20 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 its.** content
18409 6e 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72 no longer matter
1840a 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 s..**.** The ove
1840b 72 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 65 20 rlying software
1840c 6c 61 79 65 72 20 63 61 6c 6c 73 20 74 68 69 73 layer calls this
1840d 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 61 6c routine when al
1840e 6c 20 6f 66 20 74 68 65 20 64 61 74 61 0a 2a 2a l of the data.**
1840f 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70 61 on the given pa
18410 67 65 20 69 73 20 75 6e 75 73 65 64 2e 20 54 68 ge is unused. Th
18411 65 20 70 61 67 65 72 20 6d 61 72 6b 73 20 74 68 e pager marks th
18412 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 e page as clean
18413 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 64 6f so.** that it do
18414 65 73 20 6e 6f 74 20 67 65 74 20 77 72 69 74 74 es not get writt
18415 65 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a en to disk..**.*
18416 2a 20 54 65 73 74 73 20 73 68 6f 77 20 74 68 61 * Tests show tha
18417 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 t this optimizat
18418 69 6f 6e 20 63 61 6e 20 71 75 61 64 72 75 70 6c ion can quadrupl
18419 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 6c e the speed of l
1841a 61 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54 45 20 arge .** DELETE
1841b 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 operations..*/.S
1841c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1841d 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 id sqlite3PagerD
1841e 6f 6e 74 57 72 69 74 65 28 50 67 48 64 72 20 2a ontWrite(PgHdr *
1841f 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 pPg){. Pager *p
18420 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 Pager = pPg->pPa
18421 67 65 72 3b 0a 20 20 69 66 28 20 28 70 50 67 2d ger;. if( (pPg-
18422 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
18423 54 59 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6e TY) && pPager->n
18424 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 7b 0a Savepoint==0 ){.
18425 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 PAGERTRACE((
18426 22 44 4f 4e 54 5f 57 52 49 54 45 20 70 61 67 65 "DONT_WRITE page
18427 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 %d of %d\n", pP
18428 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 g->pgno, PAGERID
18429 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 (pPager)));.
1842a 49 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e 20 IOTRACE(("CLEAN
1842b 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 %p %d\n", pPager
1842c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 , pPg->pgno)).
1842d 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 pPg->flags |=
1842e 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 PGHDR_DONT_WRITE
1842f 3b 0a 20 20 20 20 70 61 67 65 72 5f 73 65 74 5f ;. pager_set_
18430 70 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 20 pagehash(pPg);.
18431 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 }.}../*.** This
18432 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
18433 65 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 ed to increment
18434 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
18435 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 0a database file .
18436 2a 2a 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 ** change-counte
18437 72 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 34 r, stored as a 4
18438 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e -byte big-endian
18439 20 69 6e 74 65 67 65 72 20 73 74 61 72 74 69 6e integer startin
1843a 67 20 61 74 20 0a 2a 2a 20 62 79 74 65 20 6f 66 g at .** byte of
1843b 66 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 70 fset 24 of the p
1843c 61 67 65 72 20 66 69 6c 65 2e 20 20 54 68 65 20 ager file. The
1843d 73 65 63 6f 6e 64 61 72 79 20 63 68 61 6e 67 65 secondary change
1843e 20 63 6f 75 6e 74 65 72 20 61 74 0a 2a 2a 20 39 counter at.** 9
1843f 32 20 69 73 20 61 6c 73 6f 20 75 70 64 61 74 65 2 is also update
18440 64 2c 20 61 73 20 69 73 20 74 68 65 20 53 51 4c d, as is the SQL
18441 69 74 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 ite version numb
18442 65 72 20 61 74 20 6f 66 66 73 65 74 20 39 36 2e er at offset 96.
18443 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 69 73 20 .**.** But this
18444 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 66 20 only happens if
18445 74 68 65 20 70 50 61 67 65 72 2d 3e 63 68 61 6e the pPager->chan
18446 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 geCountDone flag
18447 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 20 54 6f is false..** To
18448 20 61 76 6f 69 64 20 65 78 63 65 73 73 20 63 68 avoid excess ch
18449 75 72 6e 69 6e 67 20 6f 66 20 70 61 67 65 20 31 urning of page 1
1844a 2c 20 74 68 65 20 75 70 64 61 74 65 20 6f 6e 6c , the update onl
1844b 79 20 68 61 70 70 65 6e 73 20 6f 6e 63 65 2e 0a y happens once..
1844c 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 ** See also the
1844d 70 61 67 65 72 5f 77 72 69 74 65 5f 63 68 61 6e pager_write_chan
1844e 67 65 63 6f 75 6e 74 65 72 28 29 20 72 6f 75 74 gecounter() rout
1844f 69 6e 65 20 74 68 61 74 20 64 6f 65 73 20 61 6e ine that does an
18450 20 0a 2a 2a 20 75 6e 63 6f 6e 64 69 74 69 6f 6e .** uncondition
18451 61 6c 20 75 70 64 61 74 65 20 6f 66 20 74 68 65 al update of the
18452 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 73 change counters
18453 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 ..**.** If the i
18454 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61 67 sDirectMode flag
18455 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 is zero, then t
18456 68 69 73 20 69 73 20 64 6f 6e 65 20 62 79 20 63 his is done by c
18457 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 alling .** sqlit
18458 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 6f e3PagerWrite() o
18459 6e 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 6d n page 1, then m
1845a 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f 6e odifying the con
1845b 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 tents of the.**
1845c 70 61 67 65 20 64 61 74 61 2e 20 49 6e 20 74 68 page data. In th
1845d 69 73 20 63 61 73 65 20 74 68 65 20 66 69 6c 65 is case the file
1845e 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65 64 will be updated
1845f 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e when the curren
18460 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e t.** transaction
18461 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a is committed..*
18462 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69 72 65 63 *.** The isDirec
18463 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61 79 20 6f tMode flag may o
18464 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 20 nly be non-zero
18465 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 if the library w
18466 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 as compiled.** w
18467 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 45 ith the SQLITE_E
18468 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 NABLE_ATOMIC_WRI
18469 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 TE macro defined
1846a 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 0a . In this case,.
1846b 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74 20 69 ** if isDirect i
1846c 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
1846d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1846e 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 64 69 le is updated di
1846f 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77 72 69 rectly.** by wri
18470 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65 64 20 ting an updated
18471 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 20 version of page
18472 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 74 1 using a call t
18473 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 o the .** sqlite
18474 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e 63 74 3OsWrite() funct
18475 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ion..*/.static i
18476 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 nt pager_incr_ch
18477 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67 65 angecounter(Page
18478 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 r *pPager, int i
18479 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a 20 20 sDirectMode){.
1847a 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1847b 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 OK;.. assert( p
1847c 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 Pager->eState==P
1847d 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 AGER_WRITER_CACH
1847e 45 4d 4f 44 0a 20 20 20 20 20 20 20 7c 7c 20 70 EMOD. || p
1847f 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 Pager->eState==P
18480 41 47 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f AGER_WRITER_DBMO
18481 44 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 D. );. assert(
18482 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 assert_pager_st
18483 61 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a 0a ate(pPager) );..
18484 20 20 2f 2a 20 44 65 63 6c 61 72 65 20 61 6e 64 /* Declare and
18485 20 69 6e 69 74 69 61 6c 69 7a 65 20 63 6f 6e 73 initialize cons
18486 74 61 6e 74 20 69 6e 74 65 67 65 72 20 27 69 73 tant integer 'is
18487 44 69 72 65 63 74 27 2e 20 49 66 20 74 68 65 0a Direct'. If the.
18488 20 20 2a 2a 20 61 74 6f 6d 69 63 2d 77 72 69 74 ** atomic-writ
18489 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 e optimization i
1848a 73 20 65 6e 61 62 6c 65 64 20 69 6e 20 74 68 69 s enabled in thi
1848b 73 20 62 75 69 6c 64 2c 20 74 68 65 6e 20 69 73 s build, then is
1848c 44 69 72 65 63 74 0a 20 20 2a 2a 20 69 73 20 69 Direct. ** is i
1848d 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 74 68 nitialized to th
1848e 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 e value passed a
1848f 73 20 74 68 65 20 69 73 44 69 72 65 63 74 4d 6f s the isDirectMo
18490 64 65 20 70 61 72 61 6d 65 74 65 72 0a 20 20 2a de parameter. *
18491 2a 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 * to this functi
18492 6f 6e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 on. Otherwise, i
18493 74 20 69 73 20 61 6c 77 61 79 73 20 73 65 74 20 t is always set
18494 74 6f 20 7a 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 to zero.. **.
18495 2a 2a 20 54 68 65 20 69 64 65 61 20 69 73 20 74 ** The idea is t
18496 68 61 74 20 69 66 20 74 68 65 20 61 74 6f 6d 69 hat if the atomi
18497 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 c-write optimiza
18498 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 20 20 2a 2a tion is not. **
18499 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 enabled at comp
1849a 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 20 63 6f ile time, the co
1849b 6d 70 69 6c 65 72 20 63 61 6e 20 6f 6d 69 74 20 mpiler can omit
1849c 74 68 65 20 74 65 73 74 73 20 6f 66 0a 20 20 2a the tests of. *
1849d 2a 20 27 69 73 44 69 72 65 63 74 27 20 62 65 6c * 'isDirect' bel
1849e 6f 77 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 74 ow, as well as t
1849f 68 65 20 62 6c 6f 63 6b 20 65 6e 63 6c 6f 73 65 he block enclose
184a0 64 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 22 69 d in the. ** "i
184a1 66 28 20 69 73 44 69 72 65 63 74 20 29 22 20 63 f( isDirect )" c
184a2 6f 6e 64 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 ondition.. */.#
184a3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e ifndef SQLITE_EN
184a4 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 ABLE_ATOMIC_WRIT
184a5 45 0a 23 20 64 65 66 69 6e 65 20 44 49 52 45 43 E.# define DIREC
184a6 54 5f 4d 4f 44 45 20 30 0a 20 20 61 73 73 65 72 T_MODE 0. asser
184a7 74 28 20 69 73 44 69 72 65 63 74 4d 6f 64 65 3d t( isDirectMode=
184a8 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 =0 );. UNUSED_P
184a9 41 52 41 4d 45 54 45 52 28 69 73 44 69 72 65 63 ARAMETER(isDirec
184aa 74 4d 6f 64 65 29 3b 0a 23 65 6c 73 65 0a 23 20 tMode);.#else.#
184ab 64 65 66 69 6e 65 20 44 49 52 45 43 54 5f 4d 4f define DIRECT_MO
184ac 44 45 20 69 73 44 69 72 65 63 74 4d 6f 64 65 0a DE isDirectMode.
184ad 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 21 70 #endif.. if( !p
184ae 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 Pager->changeCou
184af 6e 74 44 6f 6e 65 20 26 26 20 41 4c 57 41 59 53 ntDone && ALWAYS
184b0 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e (pPager->dbSize>
184b1 30 29 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 0) ){. PgHdr
184b2 2a 70 50 67 48 64 72 3b 20 20 20 20 20 20 20 20 *pPgHdr;
184b3 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65 72 /* Refer
184b4 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 2a ence to page 1 *
184b5 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 /.. assert( !
184b6 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
184b7 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 && isOpen(pPage
184b8 72 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 20 20 2f r->fd) );.. /
184b9 2a 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f 66 * Open page 1 of
184ba 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77 72 the file for wr
184bb 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 iting. */. rc
184bc 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 = sqlite3PagerG
184bd 65 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26 70 et(pPager, 1, &p
184be 50 67 48 64 72 29 3b 0a 20 20 20 20 61 73 73 65 PgHdr);. asse
184bf 72 74 28 20 70 50 67 48 64 72 3d 3d 30 20 7c 7c rt( pPgHdr==0 ||
184c0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
184c1 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 61 67 ;.. /* If pag
184c2 65 20 6f 6e 65 20 77 61 73 20 66 65 74 63 68 65 e one was fetche
184c3 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 d successfully,
184c4 61 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f and this functio
184c5 6e 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 n is not. **
184c6 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 64 69 72 operating in dir
184c7 65 63 74 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20 70 ect-mode, make p
184c8 61 67 65 20 31 20 77 72 69 74 61 62 6c 65 2e 20 age 1 writable.
184c9 20 57 68 65 6e 20 6e 6f 74 20 69 6e 20 0a 20 20 When not in .
184ca 20 20 2a 2a 20 64 69 72 65 63 74 20 6d 6f 64 65 ** direct mode
184cb 2c 20 70 61 67 65 20 31 20 69 73 20 61 6c 77 61 , page 1 is alwa
184cc 79 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 ys held in cache
184cd 20 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 50 and hence the P
184ce 61 67 65 72 47 65 74 28 29 0a 20 20 20 20 2a 2a agerGet(). **
184cf 20 61 62 6f 76 65 20 69 73 20 61 6c 77 61 79 73 above is always
184d0 20 73 75 63 63 65 73 73 66 75 6c 20 2d 20 68 65 successful - he
184d1 6e 63 65 20 74 68 65 20 41 4c 57 41 59 53 20 6f nce the ALWAYS o
184d2 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 2e n rc==SQLITE_OK.
184d3 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
184d4 21 44 49 52 45 43 54 5f 4d 4f 44 45 20 26 26 20 !DIRECT_MODE &&
184d5 41 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c 49 54 ALWAYS(rc==SQLIT
184d6 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 E_OK) ){. r
184d7 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
184d8 57 72 69 74 65 28 70 50 67 48 64 72 29 3b 0a 20 Write(pPgHdr);.
184d9 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 }.. if( rc
184da 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
184db 20 20 20 20 20 2f 2a 20 41 63 74 75 61 6c 6c 79 /* Actually
184dc 20 64 6f 20 74 68 65 20 75 70 64 61 74 65 20 6f do the update o
184dd 66 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 f the change cou
184de 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 61 nter */. pa
184df 67 65 72 5f 77 72 69 74 65 5f 63 68 61 6e 67 65 ger_write_change
184e0 63 6f 75 6e 74 65 72 28 70 50 67 48 64 72 29 3b counter(pPgHdr);
184e1 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72 75 .. /* If ru
184e2 6e 6e 69 6e 67 20 69 6e 20 64 69 72 65 63 74 20 nning in direct
184e3 6d 6f 64 65 2c 20 77 72 69 74 65 20 74 68 65 20 mode, write the
184e4 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 67 65 contents of page
184e5 20 31 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 1 to the file.
184e6 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 44 49 52 */. if( DIR
184e7 45 43 54 5f 4d 4f 44 45 20 29 7b 0a 20 20 20 20 ECT_MODE ){.
184e8 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a const void *
184e9 7a 42 75 66 3b 0a 20 20 20 20 20 20 20 20 61 73 zBuf;. as
184ea 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 sert( pPager->db
184eb 46 69 6c 65 53 69 7a 65 3e 30 20 29 3b 0a 20 20 FileSize>0 );.
184ec 20 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 CODEC2(pPa
184ed 67 65 72 2c 20 70 50 67 48 64 72 2d 3e 70 44 61 ger, pPgHdr->pDa
184ee 74 61 2c 20 31 2c 20 36 2c 20 72 63 3d 53 51 4c ta, 1, 6, rc=SQL
184ef 49 54 45 5f 4e 4f 4d 45 4d 2c 20 7a 42 75 66 29 ITE_NOMEM, zBuf)
184f0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
184f1 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
184f2 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
184f3 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 lite3OsWrite(pPa
184f4 67 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70 ger->fd, zBuf, p
184f5 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c Pager->pageSize,
184f6 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 0);. p
184f7 50 61 67 65 72 2d 3e 61 53 74 61 74 5b 50 41 47 Pager->aStat[PAG
184f8 45 52 5f 53 54 41 54 5f 57 52 49 54 45 5d 2b 2b ER_STAT_WRITE]++
184f9 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
184fa 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
184fb 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
184fc 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 pPager->chang
184fd 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a eCountDone = 1;.
184fe 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
184ff 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
18500 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 Pager->changeCou
18501 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 ntDone = 1;.
18502 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
18503 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 70 61 * Release the pa
18504 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 2a 2f ge reference. */
18505 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 . sqlite3Page
18506 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a rUnref(pPgHdr);.
18507 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
18508 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 .}../*.** Sync t
18509 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1850a 20 74 6f 20 64 69 73 6b 2e 20 54 68 69 73 20 69 to disk. This i
1850b 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 69 6e s a no-op for in
1850c 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
1850d 73 0a 2a 2a 20 6f 72 20 70 61 67 65 73 20 77 69 s.** or pages wi
1850e 74 68 20 74 68 65 20 50 61 67 65 72 2e 6e 6f 53 th the Pager.noS
1850f 79 6e 63 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a ync flag set..**
18510 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 .** If successfu
18511 6c 2c 20 6f 72 20 69 66 20 63 61 6c 6c 65 64 20 l, or if called
18512 6f 6e 20 61 20 70 61 67 65 72 20 66 6f 72 20 77 on a pager for w
18513 68 69 63 68 20 69 74 20 69 73 20 61 20 6e 6f 2d hich it is a no-
18514 6f 70 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 op, this.** func
18515 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c tion returns SQL
18516 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 ITE_OK. Otherwis
18517 65 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 e, an IO error c
18518 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
18519 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1851a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
1851b 67 65 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70 gerSync(Pager *p
1851c 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 Pager){. int rc
1851d 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1851e 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 if( !pPager->noS
1851f 79 6e 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ync ){. asser
18520 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 t( !MEMDB );.
18521 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 rc = sqlite3OsS
18522 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ync(pPager->fd,
18523 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 pPager->syncFlag
18524 73 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 s);. }else if(
18525 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
18526 64 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 d) ){. assert
18527 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 ( !MEMDB );.
18528 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 rc = sqlite3OsFi
18529 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72 leControl(pPager
1852a 2d 3e 66 64 2c 20 53 51 4c 49 54 45 5f 46 43 4e ->fd, SQLITE_FCN
1852b 54 4c 5f 53 59 4e 43 5f 4f 4d 49 54 54 45 44 2c TL_SYNC_OMITTED,
1852c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 0);. if( rc=
1852d 3d 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 =SQLITE_NOTFOUND
1852e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
1852f 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
18530 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
18531 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
18532 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 unction may only
18533 20 62 65 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 be called while
18534 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 a write-transac
18535 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 69 tion is active i
18536 6e 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2e 20 49 n.** rollback. I
18537 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e f the connection
18538 20 69 73 20 69 6e 20 57 41 4c 20 6d 6f 64 65 2c is in WAL mode,
18539 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 this call is a
1853a 6e 6f 2d 6f 70 2e 20 0a 2a 2a 20 4f 74 68 65 72 no-op. .** Other
1853b 77 69 73 65 2c 20 69 66 20 74 68 65 20 63 6f 6e wise, if the con
1853c 6e 65 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 nection does not
1853d 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 6e already have an
1853e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
1853f 6f 6e 20 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 on .** the datab
18540 61 73 65 20 66 69 6c 65 2c 20 61 6e 20 61 74 74 ase file, an att
18541 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 empt is made to
18542 6f 62 74 61 69 6e 20 6f 6e 65 2e 0a 2a 2a 0a 2a obtain one..**.*
18543 2a 20 49 66 20 74 68 65 20 45 58 43 4c 55 53 49 * If the EXCLUSI
18544 56 45 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 VE lock is alrea
18545 64 79 20 68 65 6c 64 20 6f 72 20 74 68 65 20 61 dy held or the a
18546 74 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e ttempt to obtain
18547 20 69 74 20 69 73 0a 2a 2a 20 73 75 63 63 65 73 it is.** succes
18548 73 66 75 6c 2c 20 6f 72 20 74 68 65 20 63 6f 6e sful, or the con
18549 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 57 41 nection is in WA
1854a 4c 20 6d 6f 64 65 2c 20 53 51 4c 49 54 45 5f 4f L mode, SQLITE_O
1854b 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a K is returned..*
1854c 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 65 69 74 * Otherwise, eit
1854d 68 65 72 20 53 51 4c 49 54 45 5f 42 55 53 59 20 her SQLITE_BUSY
1854e 6f 72 20 61 6e 20 53 51 4c 49 54 45 5f 49 4f 45 or an SQLITE_IOE
1854f 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 RR_XXX error cod
18550 65 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 e is .** returne
18551 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
18552 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
18553 50 61 67 65 72 45 78 63 6c 75 73 69 76 65 4c 6f PagerExclusiveLo
18554 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ck(Pager *pPager
18555 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
18556 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 LITE_OK;. asser
18557 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 t( pPager->eStat
18558 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f e==PAGER_WRITER_
18559 43 41 43 48 45 4d 4f 44 20 0a 20 20 20 20 20 20 CACHEMOD .
1855a 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 61 || pPager->eSta
1855b 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 te==PAGER_WRITER
1855c 5f 44 42 4d 4f 44 20 0a 20 20 20 20 20 20 20 7c _DBMOD . |
1855d 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 | pPager->eState
1855e 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c ==PAGER_WRITER_L
1855f 4f 43 4b 45 44 20 0a 20 20 29 3b 0a 20 20 61 73 OCKED . );. as
18560 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 sert( assert_pag
18561 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 er_state(pPager)
18562 20 29 3b 0a 20 20 69 66 28 20 30 3d 3d 70 61 67 );. if( 0==pag
18563 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 erUseWal(pPager)
18564 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 ){. rc = pag
18565 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 er_wait_on_lock(
18566 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 pPager, EXCLUSIV
18567 45 5f 4c 4f 43 4b 29 3b 0a 20 20 7d 0a 20 20 72 E_LOCK);. }. r
18568 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
18569 2a 2a 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 ** Sync the data
1856a 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 base file for th
1856b 65 20 70 61 67 65 72 20 70 50 61 67 65 72 2e 20 e pager pPager.
1856c 7a 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 zMaster points t
1856d 6f 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 o the name.** of
1856e 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
1856f 6c 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 l file that shou
18570 6c 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e ld be written in
18571 74 6f 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 to the individua
18572 6c 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c l.** journal fil
18573 65 2e 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 62 e. zMaster may b
18574 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 e NULL, which is
18575 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 interpreted as
18576 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 no master.** jou
18577 72 6e 61 6c 20 28 61 20 73 69 6e 67 6c 65 20 64 rnal (a single d
18578 61 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 atabase transact
18579 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ion)..**.** This
1857a 20 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73 routine ensures
1857b 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a that:.**.** *
1857c 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 The database fi
1857d 6c 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 le change-counte
1857e 72 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a r is updated,.**
1857f 20 20 20 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c * the journal
18580 20 69 73 20 73 79 6e 63 65 64 20 28 75 6e 6c 65 is synced (unle
18581 73 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 ss the atomic-wr
18582 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ite optimization
18583 20 69 73 20 75 73 65 64 29 2c 0a 2a 2a 20 20 20 is used),.**
18584 2a 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 * all dirty page
18585 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 74 6f s are written to
18586 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
18587 6c 65 2c 20 0a 2a 2a 20 20 20 2a 20 74 68 65 20 le, .** * the
18588 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
18589 20 74 72 75 6e 63 61 74 65 64 20 28 69 66 20 72 truncated (if r
1858a 65 71 75 69 72 65 64 29 2c 20 61 6e 64 0a 2a 2a equired), and.**
1858b 20 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 73 * the databas
1858c 65 20 66 69 6c 65 20 73 79 6e 63 65 64 2e 20 0a e file synced. .
1858d 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74 **.** The only t
1858e 68 69 6e 67 20 74 68 61 74 20 72 65 6d 61 69 6e hing that remain
1858f 73 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 s to commit the
18590 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 transaction is t
18591 6f 20 66 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20 28 o finalize .** (
18592 64 65 6c 65 74 65 2c 20 74 72 75 6e 63 61 74 65 delete, truncate
18593 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 66 69 72 or zero the fir
18594 73 74 20 70 61 72 74 20 6f 66 29 20 74 68 65 20 st part of) the
18595 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 journal file (or
18596 20 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 .** delete the
18597 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
18598 69 6c 65 20 69 66 20 73 70 65 63 69 66 69 65 64 ile if specified
18599 29 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 )..**.** Note th
1859a 61 74 20 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e at if zMaster==N
1859b 55 4c 4c 2c 20 74 68 69 73 20 64 6f 65 73 20 6e ULL, this does n
1859c 6f 74 20 6f 76 65 72 77 72 69 74 65 20 61 20 70 ot overwrite a p
1859d 72 65 76 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a revious value.**
1859e 20 70 61 73 73 65 64 20 74 6f 20 61 6e 20 73 71 passed to an sq
1859f 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
185a0 50 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e PhaseOne() call.
185a1 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
185a2 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 20 2d 20 nal parameter -
185a3 6e 6f 53 79 6e 63 20 2d 20 69 73 20 74 72 75 65 noSync - is true
185a4 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 , then the datab
185a5 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 0a ase file itself.
185a6 2a 2a 20 69 73 20 6e 6f 74 20 73 79 6e 63 65 64 ** is not synced
185a7 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 . The caller mus
185a8 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 t call sqlite3Pa
185a9 67 65 72 53 79 6e 63 28 29 20 64 69 72 65 63 74 gerSync() direct
185aa 6c 79 20 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68 ly to.** sync th
185ab 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
185ac 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43 before calling C
185ad 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 ommitPhaseTwo()
185ae 74 6f 20 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a to delete the.**
185af 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e journal file in
185b0 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 53 this case..*/.S
185b1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
185b2 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f t sqlite3PagerCo
185b3 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 0a 20 20 mmitPhaseOne(.
185b4 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 Pager *pPager,
185b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185b6 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 /* Pager object
185b7 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
185b8 2a 7a 4d 61 73 74 65 72 2c 20 20 20 20 20 20 20 *zMaster,
185b9 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e /* If not N
185ba 55 4c 4c 2c 20 74 68 65 20 6d 61 73 74 65 72 20 ULL, the master
185bb 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a journal name */.
185bc 20 20 69 6e 74 20 6e 6f 53 79 6e 63 20 20 20 20 int noSync
185bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185be 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 /* True to omi
185bf 74 20 74 68 65 20 78 53 79 6e 63 20 6f 6e 20 74 t the xSync on t
185c0 68 65 20 64 62 20 66 69 6c 65 20 2a 2f 0a 29 7b he db file */.){
185c1 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
185c2 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
185c3 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
185c4 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 e */.. assert(
185c5 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d pPager->eState==
185c6 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 PAGER_WRITER_LOC
185c7 4b 45 44 0a 20 20 20 20 20 20 20 7c 7c 20 70 50 KED. || pP
185c8 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 ager->eState==PA
185c9 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45 GER_WRITER_CACHE
185ca 4d 4f 44 0a 20 20 20 20 20 20 20 7c 7c 20 70 50 MOD. || pP
185cb 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 ager->eState==PA
185cc 47 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 GER_WRITER_DBMOD
185cd 0a 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 . || pPage
185ce 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 r->eState==PAGER
185cf 5f 45 52 52 4f 52 0a 20 20 29 3b 0a 20 20 61 73 _ERROR. );. as
185d0 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 sert( assert_pag
185d1 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 er_state(pPager)
185d2 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20 70 );.. /* If a p
185d3 72 69 6f 72 20 65 72 72 6f 72 20 6f 63 63 75 72 rior error occur
185d4 72 65 64 2c 20 72 65 70 6f 72 74 20 74 68 61 74 red, report that
185d5 20 65 72 72 6f 72 20 61 67 61 69 6e 2e 20 2a 2f error again. */
185d6 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 . if( NEVER(pPa
185d7 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 ger->errCode) )
185d8 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 return pPager->e
185d9 72 72 43 6f 64 65 3b 0a 0a 20 20 50 41 47 45 52 rrCode;.. PAGER
185da 54 52 41 43 45 28 28 22 44 41 54 41 42 41 53 45 TRACE(("DATABASE
185db 20 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a SYNC: File=%s z
185dc 4d 61 73 74 65 72 3d 25 73 20 6e 53 69 7a 65 3d Master=%s nSize=
185dd 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 70 50 %d\n", . pP
185de 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c ager->zFilename,
185df 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 zMaster, pPager
185e0 2d 3e 64 62 53 69 7a 65 29 29 3b 0a 0a 20 20 2f ->dbSize));.. /
185e1 2a 20 49 66 20 6e 6f 20 64 61 74 61 62 61 73 65 * If no database
185e2 20 63 68 61 6e 67 65 73 20 68 61 76 65 20 62 65 changes have be
185e3 65 6e 20 6d 61 64 65 2c 20 72 65 74 75 72 6e 20 en made, return
185e4 65 61 72 6c 79 2e 20 2a 2f 0a 20 20 69 66 28 20 early. */. if(
185e5 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3c 50 pPager->eState<P
185e6 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 AGER_WRITER_CACH
185e7 45 4d 4f 44 20 29 20 72 65 74 75 72 6e 20 53 51 EMOD ) return SQ
185e8 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 LITE_OK;.. if(
185e9 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 2f 2a 20 MEMDB ){. /*
185ea 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e If this is an in
185eb 2d 6d 65 6d 6f 72 79 20 64 62 2c 20 6f 72 20 6e -memory db, or n
185ec 6f 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 o pages have bee
185ed 6e 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 6f 72 n written to, or
185ee 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 66 75 6e this. ** fun
185ef 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 ction has alread
185f0 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20 69 y been called, i
185f1 74 20 69 73 20 6d 6f 73 74 6c 79 20 61 20 6e 6f t is mostly a no
185f2 2d 6f 70 2e 20 20 48 6f 77 65 76 65 72 2c 20 61 -op. However, a
185f3 6e 79 0a 20 20 20 20 2a 2a 20 62 61 63 6b 75 70 ny. ** backup
185f4 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6e 65 65 in progress nee
185f5 64 73 20 74 6f 20 62 65 20 72 65 73 74 61 72 74 ds to be restart
185f6 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 ed.. */. s
185f7 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74 qlite3BackupRest
185f8 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 63 art(pPager->pBac
185f9 6b 75 70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 kup);. }else{.
185fa 20 20 20 69 66 28 20 70 61 67 65 72 55 73 65 57 if( pagerUseW
185fb 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 al(pPager) ){.
185fc 20 20 20 20 50 67 48 64 72 20 2a 70 4c 69 73 74 PgHdr *pList
185fd 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 = sqlite3Pcache
185fe 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 DirtyList(pPager
185ff 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20 ->pPCache);.
18600 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 4f 6e PgHdr *pPageOn
18601 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 e = 0;. if(
18602 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 pList==0 ){.
18603 20 20 20 20 20 2f 2a 20 4d 75 73 74 20 68 61 76 /* Must hav
18604 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 70 e at least one p
18605 61 67 65 20 66 6f 72 20 74 68 65 20 57 41 4c 20 age for the WAL
18606 63 6f 6d 6d 69 74 20 66 6c 61 67 2e 0a 20 20 20 commit flag..
18607 20 20 20 20 20 2a 2a 20 54 69 63 6b 65 74 20 5b ** Ticket [
18608 32 64 31 61 35 63 36 37 64 66 63 32 33 36 33 65 2d1a5c67dfc2363e
18609 34 34 66 32 39 64 39 62 62 64 35 37 66 5d 20 32 44f29d9bbd57f] 2
1860a 30 31 31 2d 30 35 2d 31 38 20 2a 2f 0a 20 20 20 011-05-18 */.
1860b 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1860c 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 3PagerGet(pPager
1860d 2c 20 31 2c 20 26 70 50 61 67 65 4f 6e 65 29 3b , 1, &pPageOne);
1860e 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 20 3d . pList =
1860f 20 70 50 61 67 65 4f 6e 65 3b 0a 20 20 20 20 20 pPageOne;.
18610 20 20 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79 pList->pDirty
18611 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
18612 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
18613 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 SQLITE_OK );.
18614 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 4c if( ALWAYS(pL
18615 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ist) ){.
18616 72 63 20 3d 20 70 61 67 65 72 57 61 6c 46 72 61 rc = pagerWalFra
18617 6d 65 73 28 70 50 61 67 65 72 2c 20 70 4c 69 73 mes(pPager, pLis
18618 74 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a t, pPager->dbSiz
18619 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 e, 1);. }.
1861a 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
1861b 72 55 6e 72 65 66 28 70 50 61 67 65 4f 6e 65 29 rUnref(pPageOne)
1861c 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
1861d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1861e 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 sqlite3Pcac
1861f 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 heCleanAll(pPage
18620 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 r->pPCache);.
18621 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
18622 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c /* The fol
18623 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 75 70 64 lowing block upd
18624 61 74 65 73 20 74 68 65 20 63 68 61 6e 67 65 2d ates the change-
18625 63 6f 75 6e 74 65 72 2e 20 45 78 61 63 74 6c 79 counter. Exactly
18626 20 68 6f 77 20 69 74 0a 20 20 20 20 20 20 2a 2a how it. **
18627 20 64 6f 65 73 20 74 68 69 73 20 64 65 70 65 6e does this depen
18628 64 73 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 ds on whether or
18629 20 6e 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d not the atomic-
1862a 75 70 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 update optimizat
1862b 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 77 61 73 ion. ** was
1862c 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 enabled at comp
1862d 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64 20 69 66 ile time, and if
1862e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f this transactio
1862f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a 20 20 20 n meets the .
18630 20 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63 72 ** runtime cr
18631 69 74 65 72 69 61 20 74 6f 20 75 73 65 20 74 68 iteria to use th
18632 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 20 20 e operation: .
18633 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
18634 20 20 20 2a 20 54 68 65 20 66 69 6c 65 2d 73 79 * The file-sy
18635 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 74 68 stem supports th
18636 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 70 e atomic-write p
18637 72 6f 70 65 72 74 79 20 66 6f 72 0a 20 20 20 20 roperty for.
18638 20 20 2a 2a 20 20 20 20 20 20 62 6c 6f 63 6b 73 ** blocks
18639 20 6f 66 20 73 69 7a 65 20 70 61 67 65 2d 73 69 of size page-si
1863a 7a 65 2c 20 61 6e 64 20 0a 20 20 20 20 20 20 2a ze, and . *
1863b 2a 20 20 20 20 2a 20 54 68 69 73 20 63 6f 6d 6d * * This comm
1863c 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f it is not part o
1863d 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 20 74 f a multi-file t
1863e 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 0a ransaction, and.
1863f 20 20 20 20 20 20 2a 2a 20 20 20 20 2a 20 45 78 ** * Ex
18640 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 68 actly one page h
18641 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 as been modified
18642 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 68 and store in th
18643 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a e journal file..
18644 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
18645 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a * If the optimiz
18646 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 65 6e ation was not en
18647 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 abled at compile
18648 20 74 69 6d 65 2c 20 74 68 65 6e 20 74 68 65 0a time, then the.
18649 20 20 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 69 ** pager_i
1864a 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 ncr_changecounte
1864b 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 r() function is
1864c 63 61 6c 6c 65 64 20 74 6f 20 75 70 64 61 74 65 called to update
1864d 20 74 68 65 20 63 68 61 6e 67 65 0a 20 20 20 20 the change.
1864e 20 20 2a 2a 20 63 6f 75 6e 74 65 72 20 69 6e 20 ** counter in
1864f 27 69 6e 64 69 72 65 63 74 2d 6d 6f 64 65 27 2e 'indirect-mode'.
18650 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 If the optimiza
18651 74 69 6f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64 tion is compiled
18652 20 69 6e 20 62 75 74 0a 20 20 20 20 20 20 2a 2a in but. **
18653 20 69 73 20 6e 6f 74 20 61 70 70 6c 69 63 61 62 is not applicab
18654 6c 65 20 74 6f 20 74 68 69 73 20 74 72 61 6e 73 le to this trans
18655 61 63 74 69 6f 6e 2c 20 63 61 6c 6c 20 73 71 6c action, call sql
18656 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 ite3JournalCreat
18657 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 e(). ** to
18658 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6a 6f make sure the jo
18659 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 61 urnal file has a
1865a 63 74 75 61 6c 6c 79 20 62 65 65 6e 20 63 72 65 ctually been cre
1865b 61 74 65 64 2c 20 74 68 65 6e 20 63 61 6c 6c 0a ated, then call.
1865c 20 20 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 69 ** pager_i
1865d 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 ncr_changecounte
1865e 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 74 68 r() to update th
1865f 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
18660 20 69 6e 20 69 6e 64 69 72 65 63 74 0a 20 20 20 in indirect.
18661 20 20 20 2a 2a 20 6d 6f 64 65 2e 20 0a 20 20 20 ** mode. .
18662 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f **. ** O
18663 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 therwise, if the
18664 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 optimization is
18665 20 62 6f 74 68 20 65 6e 61 62 6c 65 64 20 61 6e both enabled an
18666 64 20 61 70 70 6c 69 63 61 62 6c 65 2c 0a 20 20 d applicable,.
18667 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c 6c ** then call
18668 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e pager_incr_chan
18669 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 75 gecounter() to u
1866a 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 pdate the change
1866b 2d 63 6f 75 6e 74 65 72 0a 20 20 20 20 20 20 2a -counter. *
1866c 2a 20 69 6e 20 27 64 69 72 65 63 74 27 20 6d 6f * in 'direct' mo
1866d 64 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 de. In this case
1866e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1866f 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 0a e will never be.
18670 20 20 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 ** created
18671 20 66 6f 72 20 74 68 69 73 20 74 72 61 6e 73 61 for this transa
18672 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a ction.. */.
18673 20 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f #ifdef SQLITE_
18674 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
18675 49 54 45 0a 20 20 20 20 20 20 50 67 48 64 72 20 ITE. PgHdr
18676 2a 70 50 67 3b 0a 20 20 20 20 20 20 61 73 73 65 *pPg;. asse
18677 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 rt( isOpen(pPage
18678 72 2d 3e 6a 66 64 29 20 0a 20 20 20 20 20 20 20 r->jfd) .
18679 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a || pPager->j
1867a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
1867b 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 R_JOURNALMODE_OF
1867c 46 20 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c F . ||
1867d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
1867e 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
1867f 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 0a 20 20 20 NALMODE_WAL .
18680 20 20 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 );. if(
18681 21 7a 4d 61 73 74 65 72 20 26 26 20 69 73 4f 70 !zMaster && isOp
18682 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
18683 0a 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65 . && pPage
18684 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 6a r->journalOff==j
18685 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 28 70 50 rnlBufferSize(pP
18686 61 67 65 72 29 20 0a 20 20 20 20 20 20 20 26 26 ager) . &&
18687 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e pPager->dbSize>
18688 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 =pPager->dbOrigS
18689 69 7a 65 0a 20 20 20 20 20 20 20 26 26 20 28 30 ize. && (0
1868a 3d 3d 28 70 50 67 20 3d 20 73 71 6c 69 74 65 33 ==(pPg = sqlite3
1868b 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 PcacheDirtyList(
1868c 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
1868d 29 20 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 44 69 ) || 0==pPg->pDi
1868e 72 74 79 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 rty). ){.
1868f 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 /* Update
18690 74 68 65 20 64 62 20 66 69 6c 65 20 63 68 61 6e the db file chan
18691 67 65 20 63 6f 75 6e 74 65 72 20 76 69 61 20 74 ge counter via t
18692 68 65 20 64 69 72 65 63 74 2d 77 72 69 74 65 20 he direct-write
18693 6d 65 74 68 6f 64 2e 20 54 68 65 20 0a 20 20 20 method. The .
18694 20 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e ** followin
18695 67 20 63 61 6c 6c 20 77 69 6c 6c 20 6d 6f 64 69 g call will modi
18696 66 79 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 fy the in-memory
18697 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
18698 6f 66 20 70 61 67 65 20 31 20 0a 20 20 20 20 20 of page 1 .
18699 20 20 20 2a 2a 20 74 6f 20 69 6e 63 6c 75 64 65 ** to include
1869a 20 74 68 65 20 75 70 64 61 74 65 64 20 63 68 61 the updated cha
1869b 6e 67 65 20 63 6f 75 6e 74 65 72 20 61 6e 64 20 nge counter and
1869c 74 68 65 6e 20 77 72 69 74 65 20 70 61 67 65 20 then write page
1869d 31 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 69 1 . ** di
1869e 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20 64 61 rectly to the da
1869f 74 61 62 61 73 65 20 66 69 6c 65 2e 20 42 65 63 tabase file. Bec
186a0 61 75 73 65 20 6f 66 20 74 68 65 20 61 74 6f 6d ause of the atom
186a1 69 63 2d 77 72 69 74 65 20 0a 20 20 20 20 20 20 ic-write .
186a2 20 20 2a 2a 20 70 72 6f 70 65 72 74 79 20 6f 66 ** property of
186a3 20 74 68 65 20 68 6f 73 74 20 66 69 6c 65 2d 73 the host file-s
186a4 79 73 74 65 6d 2c 20 74 68 69 73 20 69 73 20 73 ystem, this is s
186a5 61 66 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a afe.. */.
186a6 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 rc = pag
186a7 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f er_incr_changeco
186a8 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 31 29 unter(pPager, 1)
186a9 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
186aa 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
186ab 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 te3JournalCreate
186ac 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 (pPager->jfd);.
186ad 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
186ae 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
186af 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
186b0 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e _incr_changecoun
186b1 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 3b 0a ter(pPager, 0);.
186b2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
186b3 7d 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 20 20 }. #else.
186b4 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f rc = pager_incr_
186b5 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 changecounter(pP
186b6 61 67 65 72 2c 20 30 29 3b 0a 20 20 23 65 6e 64 ager, 0);. #end
186b7 69 66 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 if. if( rc!
186b8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 =SQLITE_OK ) got
186b9 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f o commit_phase_o
186ba 6e 65 5f 65 78 69 74 3b 0a 20 20 0a 20 20 20 20 ne_exit;. .
186bb 20 20 2f 2a 20 49 66 20 74 68 69 73 20 74 72 61 /* If this tra
186bc 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6d 61 64 nsaction has mad
186bd 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 e the database s
186be 6d 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61 6c 6c maller, then all
186bf 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 pages. **
186c0 62 65 69 6e 67 20 64 69 73 63 61 72 64 65 64 20 being discarded
186c1 62 79 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f by the truncatio
186c2 6e 20 6d 75 73 74 20 62 65 20 77 72 69 74 74 65 n must be writte
186c3 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c n to the journal
186c4 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e 0a . ** file..
186c5 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
186c6 2a 20 42 65 66 6f 72 65 20 72 65 61 64 69 6e 67 * Before reading
186c7 20 74 68 65 20 70 61 67 65 73 20 77 69 74 68 20 the pages with
186c8 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6c 61 72 page numbers lar
186c9 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 ger than the .
186ca 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 76 ** current v
186cb 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62 alue of Pager.db
186cc 53 69 7a 65 2c 20 73 65 74 20 64 62 53 69 7a 65 Size, set dbSize
186cd 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c back to the val
186ce 75 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 ue. ** that
186cf 20 69 74 20 74 6f 6f 6b 20 61 74 20 74 68 65 20 it took at the
186d0 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 start of the tra
186d1 6e 73 61 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 nsaction. Otherw
186d2 69 73 65 2c 20 74 68 65 0a 20 20 20 20 20 20 2a ise, the. *
186d3 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 * calls to sqlit
186d4 65 33 50 61 67 65 72 47 65 74 28 29 20 72 65 74 e3PagerGet() ret
186d5 75 72 6e 20 7a 65 72 6f 65 64 20 70 61 67 65 73 urn zeroed pages
186d6 20 69 6e 73 74 65 61 64 20 6f 66 20 0a 20 20 20 instead of .
186d7 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 64 61 ** reading da
186d8 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ta from the data
186d9 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 base file..
186da 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50 */. if( pP
186db 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61 ager->dbSize<pPa
186dc 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 ger->dbOrigSize
186dd 0a 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65 . && pPage
186de 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d r->journalMode!=
186df 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
186e0 45 5f 4f 46 46 0a 20 20 20 20 20 20 29 7b 0a 20 E_OFF. ){.
186e1 20 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 20 20 Pgno i;
186e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
186e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
186e4 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 /* Iterator var
186e5 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 iable */.
186e6 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 6b 69 const Pgno iSki
186e7 70 20 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e p = PAGER_MJ_PGN
186e8 4f 28 70 50 61 67 65 72 29 3b 20 2f 2a 20 50 65 O(pPager); /* Pe
186e9 6e 64 69 6e 67 20 6c 6f 63 6b 20 70 61 67 65 20 nding lock page
186ea 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 */. const
186eb 20 50 67 6e 6f 20 64 62 53 69 7a 65 20 3d 20 70 Pgno dbSize = p
186ec 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 20 20 Pager->dbSize;
186ed 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
186ee 20 69 6d 61 67 65 20 73 69 7a 65 20 2a 2f 20 0a image size */ .
186ef 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
186f0 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d dbSize = pPager-
186f1 3e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 20 20 >dbOrigSize;.
186f2 20 20 20 20 20 66 6f 72 28 20 69 3d 64 62 53 69 for( i=dbSi
186f3 7a 65 2b 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d ze+1; i<=pPager-
186f4 3e 64 62 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b >dbOrigSize; i++
186f5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ){. if
186f6 28 20 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 ( !sqlite3Bitvec
186f7 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e Test(pPager->pIn
186f8 4a 6f 75 72 6e 61 6c 2c 20 69 29 20 26 26 20 69 Journal, i) && i
186f9 21 3d 69 53 6b 69 70 20 29 7b 0a 20 20 20 20 20 !=iSkip ){.
186fa 20 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 PgHdr *pP
186fb 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
186fc 20 2f 2a 20 50 61 67 65 20 74 6f 20 6a 6f 75 72 /* Page to jour
186fd 6e 61 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 nal */.
186fe 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
186ff 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 agerGet(pPager,
18700 69 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 20 20 i, &pPage);.
18701 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
18702 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
18703 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e commit_phase_on
18704 65 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 e_exit;.
18705 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
18706 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
18707 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 );. s
18708 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
18709 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 (pPage);.
1870a 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1870b 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f ITE_OK ) goto co
1870c 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 mmit_phase_one_e
1870d 78 69 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d xit;. }
1870e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1870f 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a pPager->dbSiz
18710 65 20 3d 20 64 62 53 69 7a 65 3b 0a 20 20 20 20 e = dbSize;.
18711 20 20 7d 20 0a 20 20 0a 20 20 20 20 20 20 2f 2a } . . /*
18712 20 57 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 Write the maste
18713 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 r journal name i
18714 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 nto the journal
18715 66 69 6c 65 2e 20 49 66 20 61 20 6d 61 73 74 65 file. If a maste
18716 72 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 r . ** jour
18717 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 68 61 nal file name ha
18718 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 s already been w
18719 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f ritten to the jo
1871a 75 72 6e 61 6c 20 66 69 6c 65 2c 20 0a 20 20 20 urnal file, .
1871b 20 20 20 2a 2a 20 6f 72 20 69 66 20 7a 4d 61 73 ** or if zMas
1871c 74 65 72 20 69 73 20 4e 55 4c 4c 20 28 6e 6f 20 ter is NULL (no
1871d 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 29 2c master journal),
1871e 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 20 then this call
1871f 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 is a no-op..
18720 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 */. rc =
18721 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e writeMasterJourn
18722 61 6c 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 al(pPager, zMast
18723 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 er);. if( r
18724 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 c!=SQLITE_OK ) g
18725 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 oto commit_phase
18726 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 0a 20 20 _one_exit;. .
18727 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 /* Sync the
18728 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 journal file and
18729 20 77 72 69 74 65 20 61 6c 6c 20 64 69 72 74 79 write all dirty
1872a 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 64 61 pages to the da
1872b 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 2a 2a tabase.. **
1872c 20 49 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 If the atomic-u
1872d 70 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 pdate optimizati
1872e 6f 6e 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 on is being used
1872f 2c 20 74 68 69 73 20 73 79 6e 63 20 77 69 6c 6c , this sync will
18730 20 6e 6f 74 20 0a 20 20 20 20 20 20 2a 2a 20 63 not . ** c
18731 72 65 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 reate the journa
18732 6c 20 66 69 6c 65 20 6f 72 20 70 65 72 66 6f 72 l file or perfor
18733 6d 20 61 6e 79 20 72 65 61 6c 20 49 4f 2e 0a 20 m any real IO..
18734 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
18735 20 42 65 63 61 75 73 65 20 74 68 65 20 63 68 61 Because the cha
18736 6e 67 65 2d 63 6f 75 6e 74 65 72 20 70 61 67 65 nge-counter page
18737 20 77 61 73 20 6a 75 73 74 20 6d 6f 64 69 66 69 was just modifi
18738 65 64 2c 20 75 6e 6c 65 73 73 20 74 68 65 0a 20 ed, unless the.
18739 20 20 20 20 20 2a 2a 20 61 74 6f 6d 69 63 2d 75 ** atomic-u
1873a 70 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 pdate optimizati
1873b 6f 6e 20 69 73 20 75 73 65 64 20 69 74 20 69 73 on is used it is
1873c 20 61 6c 6d 6f 73 74 20 63 65 72 74 61 69 6e 20 almost certain
1873d 74 68 61 74 20 74 68 65 0a 20 20 20 20 20 20 2a that the. *
1873e 2a 20 6a 6f 75 72 6e 61 6c 20 72 65 71 75 69 72 * journal requir
1873f 65 73 20 61 20 73 79 6e 63 20 68 65 72 65 2e 20 es a sync here.
18740 48 6f 77 65 76 65 72 2c 20 69 6e 20 6c 6f 63 6b However, in lock
18741 69 6e 67 5f 6d 6f 64 65 3d 65 78 63 6c 75 73 69 ing_mode=exclusi
18742 76 65 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 61 ve. ** on a
18743 20 73 79 73 74 65 6d 20 75 6e 64 65 72 20 6d 65 system under me
18744 6d 6f 72 79 20 70 72 65 73 73 75 72 65 20 69 74 mory pressure it
18745 20 69 73 20 6a 75 73 74 20 70 6f 73 73 69 62 6c is just possibl
18746 65 20 74 68 61 74 20 74 68 69 73 20 69 73 20 0a e that this is .
18747 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20 74 68 65 ** not the
18748 20 63 61 73 65 2e 20 49 6e 20 74 68 69 73 20 63 case. In this c
18749 61 73 65 20 69 74 20 69 73 20 6c 69 6b 65 6c 79 ase it is likely
1874a 20 65 6e 6f 75 67 68 20 74 68 61 74 20 74 68 65 enough that the
1874b 20 72 65 64 75 6e 64 61 6e 74 0a 20 20 20 20 20 redundant.
1874c 20 2a 2a 20 78 53 79 6e 63 28 29 20 63 61 6c 6c ** xSync() call
1874d 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 will be changed
1874e 20 74 6f 20 61 20 6e 6f 2d 6f 70 20 62 79 20 74 to a no-op by t
1874f 68 65 20 4f 53 20 61 6e 79 68 6f 77 2e 20 0a 20 he OS anyhow. .
18750 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 */. rc
18751 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 = syncJournal(p
18752 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 Pager, 0);.
18753 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
18754 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 OK ) goto commit
18755 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b _phase_one_exit;
18756 0a 20 20 0a 20 20 20 20 20 20 72 63 20 3d 20 70 . . rc = p
18757 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c ager_write_pagel
18758 69 73 74 28 70 50 61 67 65 72 2c 73 71 6c 69 74 ist(pPager,sqlit
18759 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 e3PcacheDirtyLis
1875a 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
1875b 65 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 e));. if( r
1875c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1875d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1875e 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 rc!=SQLITE_IOERR
1875f 5f 42 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20 _BLOCKED );.
18760 20 20 20 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f goto commit_
18761 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a phase_one_exit;.
18762 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
18763 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e lite3PcacheClean
18764 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 All(pPager->pPCa
18765 63 68 65 29 3b 0a 20 20 0a 20 20 20 20 20 20 2f che);. . /
18766 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f 6e * If the file on
18767 20 64 69 73 6b 20 69 73 20 6e 6f 74 20 74 68 65 disk is not the
18768 20 73 61 6d 65 20 73 69 7a 65 20 61 73 20 74 68 same size as th
18769 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 e database image
1876a 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 ,. ** then
1876b 75 73 65 20 70 61 67 65 72 5f 74 72 75 6e 63 61 use pager_trunca
1876c 74 65 20 74 6f 20 67 72 6f 77 20 6f 72 20 73 68 te to grow or sh
1876d 72 69 6e 6b 20 74 68 65 20 66 69 6c 65 20 68 65 rink the file he
1876e 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 re.. */.
1876f 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 if( pPager->d
18770 62 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 64 bSize!=pPager->d
18771 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 bFileSize ){.
18772 20 20 20 20 20 50 67 6e 6f 20 6e 4e 65 77 20 3d Pgno nNew =
18773 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 pPager->dbSize
18774 2d 20 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a - (pPager->dbSiz
18775 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f e==PAGER_MJ_PGNO
18776 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 (pPager));.
18777 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
18778 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 r->eState==PAGER
18779 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 29 3b _WRITER_DBMOD );
1877a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 . rc = pa
1877b 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61 ger_truncate(pPa
1877c 67 65 72 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 ger, nNew);.
1877d 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1877e 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d TE_OK ) goto com
1877f 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 mit_phase_one_ex
18780 69 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 it;. }. .
18781 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c /* Finally,
18782 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 sync the databa
18783 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 se file. */.
18784 20 20 69 66 28 20 21 6e 6f 53 79 6e 63 20 29 7b if( !noSync ){
18785 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
18786 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 70 lite3PagerSync(p
18787 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a Pager);. }.
18788 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 IOTRACE(("
18789 44 42 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 DBSYNC %p\n", pP
1878a 61 67 65 72 29 29 0a 20 20 20 20 7d 0a 20 20 7d ager)). }. }
1878b 0a 0a 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f ..commit_phase_o
1878c 6e 65 5f 65 78 69 74 3a 0a 20 20 69 66 28 20 72 ne_exit:. if( r
1878d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1878e 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 !pagerUseWal(pPa
1878f 67 65 72 29 20 29 7b 0a 20 20 20 20 70 50 61 67 ger) ){. pPag
18790 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 47 er->eState = PAG
18791 45 52 5f 57 52 49 54 45 52 5f 46 49 4e 49 53 48 ER_WRITER_FINISH
18792 45 44 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ED;. }. return
18793 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 rc;.}.../*.** W
18794 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
18795 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 n is called, the
18796 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 database file h
18797 61 73 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74 65 as been complete
18798 6c 79 0a 2a 2a 20 75 70 64 61 74 65 64 20 74 6f ly.** updated to
18799 20 72 65 66 6c 65 63 74 20 74 68 65 20 63 68 61 reflect the cha
1879a 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 65 nges made by the
1879b 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 current transac
1879c 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 79 6e 63 tion and.** sync
1879d 65 64 20 74 6f 20 64 69 73 6b 2e 20 54 68 65 20 ed to disk. The
1879e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 74 69 journal file sti
1879f 6c 6c 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 ll exists in the
187a0 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 0a 2a 2a file-system .**
187a1 20 74 68 6f 75 67 68 2c 20 61 6e 64 20 69 66 20 though, and if
187a2 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 a failure occurs
187a3 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69 at this point i
187a4 74 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c t will eventuall
187a5 79 0a 2a 2a 20 62 65 20 75 73 65 64 20 61 73 20 y.** be used as
187a6 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e a hot-journal an
187a7 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 d the current tr
187a8 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 65 64 ansaction rolled
187a9 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 back..**.** Thi
187aa 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c s function final
187ab 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c izes the journal
187ac 20 66 69 6c 65 2c 20 65 69 74 68 65 72 20 62 79 file, either by
187ad 20 64 65 6c 65 74 69 6e 67 2c 20 0a 2a 2a 20 74 deleting, .** t
187ae 72 75 6e 63 61 74 69 6e 67 20 6f 72 20 70 61 72 runcating or par
187af 74 69 61 6c 6c 79 20 7a 65 72 6f 69 6e 67 20 69 tially zeroing i
187b0 74 2c 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 t, so that it ca
187b1 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 0a 2a 2a nnot be used .**
187b2 20 66 6f 72 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c for hot-journal
187b3 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f 6e 63 65 20 rollback. Once
187b4 74 68 69 73 20 69 73 20 64 6f 6e 65 20 74 68 65 this is done the
187b5 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a transaction is.
187b6 2a 2a 20 69 72 72 65 76 6f 63 61 62 6c 79 20 63 ** irrevocably c
187b7 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 ommitted..**.**
187b8 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
187b9 72 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 rs, an IO error
187ba 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
187bb 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 0a 2a and the pager.*
187bc 2a 20 6d 6f 76 65 73 20 69 6e 74 6f 20 74 68 65 * moves into the
187bd 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 4f 74 error state. Ot
187be 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f herwise, SQLITE_
187bf 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a OK is returned..
187c0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
187c1 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
187c2 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f erCommitPhaseTwo
187c3 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
187c4 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
187c5 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
187c6 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
187c7 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 n code */.. /*
187c8 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f This routine sho
187c9 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 uld not be calle
187ca 64 20 69 66 20 61 20 70 72 69 6f 72 20 65 72 72 d if a prior err
187cb 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e or has occurred.
187cc 0a 20 20 2a 2a 20 42 75 74 20 69 66 20 28 64 75 . ** But if (du
187cd 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 e to a coding er
187ce 72 6f 72 20 65 6c 73 65 77 68 65 72 65 20 69 6e ror elsewhere in
187cf 20 74 68 65 20 73 79 73 74 65 6d 29 20 69 74 20 the system) it
187d0 64 6f 65 73 20 67 65 74 0a 20 20 2a 2a 20 63 61 does get. ** ca
187d1 6c 6c 65 64 2c 20 6a 75 73 74 20 72 65 74 75 72 lled, just retur
187d2 6e 20 74 68 65 20 73 61 6d 65 20 65 72 72 6f 72 n the same error
187d3 20 63 6f 64 65 20 77 69 74 68 6f 75 74 20 64 6f code without do
187d4 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 2a 2f ing anything. */
187d5 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 . if( NEVER(pPa
187d6 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 ger->errCode) )
187d7 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 return pPager->e
187d8 72 72 43 6f 64 65 3b 0a 0a 20 20 61 73 73 65 72 rrCode;.. asser
187d9 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 t( pPager->eStat
187da 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f e==PAGER_WRITER_
187db 4c 4f 43 4b 45 44 0a 20 20 20 20 20 20 20 7c 7c LOCKED. ||
187dc 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d pPager->eState=
187dd 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 46 49 =PAGER_WRITER_FI
187de 4e 49 53 48 45 44 0a 20 20 20 20 20 20 20 7c 7c NISHED. ||
187df 20 28 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 (pagerUseWal(pP
187e0 61 67 65 72 29 20 26 26 20 70 50 61 67 65 72 2d ager) && pPager-
187e1 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 >eState==PAGER_W
187e2 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 29 0a RITER_CACHEMOD).
187e3 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 );. assert( a
187e4 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 ssert_pager_stat
187e5 65 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 e(pPager) );..
187e6 2f 2a 20 41 6e 20 6f 70 74 69 6d 69 7a 61 74 69 /* An optimizati
187e7 6f 6e 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 on. If the datab
187e8 61 73 65 20 77 61 73 20 6e 6f 74 20 61 63 74 75 ase was not actu
187e9 61 6c 6c 79 20 6d 6f 64 69 66 69 65 64 20 64 75 ally modified du
187ea 72 69 6e 67 0a 20 20 2a 2a 20 74 68 69 73 20 74 ring. ** this t
187eb 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 ransaction, the
187ec 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 pager is running
187ed 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f in exclusive-mo
187ee 64 65 20 61 6e 64 20 69 73 0a 20 20 2a 2a 20 75 de and is. ** u
187ef 73 69 6e 67 20 70 65 72 73 69 73 74 65 6e 74 20 sing persistent
187f0 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 6e 20 74 journals, then t
187f1 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
187f2 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 a no-op.. **.
187f3 2a 2a 20 54 68 65 20 73 74 61 72 74 20 6f 66 20 ** The start of
187f4 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
187f5 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 currently conta
187f6 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 6a 6f 75 ins a single jou
187f7 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 rnal . ** heade
187f8 72 20 77 69 74 68 20 74 68 65 20 6e 52 65 63 20 r with the nRec
187f9 66 69 65 6c 64 20 73 65 74 20 74 6f 20 30 2e 20 field set to 0.
187fa 49 66 20 73 75 63 68 20 61 20 6a 6f 75 72 6e 61 If such a journa
187fb 6c 20 69 73 20 75 73 65 64 20 61 73 0a 20 20 2a l is used as. *
187fc 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 * a hot-journal
187fd 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e during hot-journ
187fe 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 30 20 63 al rollback, 0 c
187ff 68 61 6e 67 65 73 20 77 69 6c 6c 20 62 65 20 6d hanges will be m
18800 61 64 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 ade. ** to the
18801 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 53 database file. S
18802 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 o there is no ne
18803 65 64 20 74 6f 20 7a 65 72 6f 20 74 68 65 20 6a ed to zero the j
18804 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 ournal . ** hea
18805 64 65 72 2e 20 53 69 6e 63 65 20 74 68 65 20 70 der. Since the p
18806 61 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c 75 ager is in exclu
18807 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 72 65 sive mode, there
18808 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a is no need. **
18809 20 74 6f 20 64 72 6f 70 20 61 6e 79 20 6c 6f 63 to drop any loc
1880a 6b 73 20 65 69 74 68 65 72 2e 0a 20 20 2a 2f 0a ks either.. */.
1880b 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53 if( pPager->eS
1880c 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 tate==PAGER_WRIT
1880d 45 52 5f 4c 4f 43 4b 45 44 20 0a 20 20 20 26 26 ER_LOCKED . &&
1880e 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 pPager->exclusi
1880f 76 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 70 50 veMode . && pP
18810 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
18811 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
18812 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 29 MODE_PERSIST. )
18813 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
18814 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
18815 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a ==JOURNAL_HDR_SZ
18816 28 70 50 61 67 65 72 29 20 7c 7c 20 21 70 50 61 (pPager) || !pPa
18817 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
18818 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 );. pPager->e
18819 53 74 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45 State = PAGER_RE
1881a 41 44 45 52 3b 0a 20 20 20 20 72 65 74 75 72 6e ADER;. return
1881b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
1881c 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 . PAGERTRACE(("
1881d 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c 20 50 41 COMMIT %d\n", PA
1881e 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b GERID(pPager)));
1881f 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e . rc = pager_en
18820 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 d_transaction(pP
18821 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 ager, pPager->se
18822 74 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 75 tMaster);. retu
18823 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 rn pager_error(p
18824 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f Pager, rc);.}../
18825 2a 0a 2a 2a 20 49 66 20 61 20 77 72 69 74 65 20 *.** If a write
18826 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f transaction is o
18827 70 65 6e 2c 20 74 68 65 6e 20 61 6c 6c 20 63 68 pen, then all ch
18828 61 6e 67 65 73 20 6d 61 64 65 20 77 69 74 68 69 anges made withi
18829 6e 20 74 68 65 20 0a 2a 2a 20 74 72 61 6e 73 61 n the .** transa
1882a 63 74 69 6f 6e 20 61 72 65 20 72 65 76 65 72 74 ction are revert
1882b 65 64 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 ed and the curre
1882c 6e 74 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 nt write-transac
1882d 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a tion is closed..
1882e 2a 2a 20 54 68 65 20 70 61 67 65 72 20 66 61 6c ** The pager fal
1882f 6c 73 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 ls back to PAGER
18830 5f 52 45 41 44 45 52 20 73 74 61 74 65 20 69 66 _READER state if
18831 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 successful, or
18832 50 41 47 45 52 5f 45 52 52 4f 52 0a 2a 2a 20 73 PAGER_ERROR.** s
18833 74 61 74 65 20 69 66 20 61 6e 20 65 72 72 6f 72 tate if an error
18834 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 occurs..**.** I
18835 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 61 f the pager is a
18836 6c 72 65 61 64 79 20 69 6e 20 50 41 47 45 52 5f lready in PAGER_
18837 45 52 52 4f 52 20 73 74 61 74 65 20 77 68 65 6e ERROR state when
18838 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
18839 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 69 74 20 s called,.** it
1883a 72 65 74 75 72 6e 73 20 50 61 67 65 72 2e 65 72 returns Pager.er
1883b 72 43 6f 64 65 20 69 6d 6d 65 64 69 61 74 65 6c rCode immediatel
1883c 79 2e 20 4e 6f 20 77 6f 72 6b 20 69 73 20 70 65 y. No work is pe
1883d 72 66 6f 72 6d 65 64 20 69 6e 20 74 68 69 73 20 rformed in this
1883e 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 case..**.** Othe
1883f 72 77 69 73 65 2c 20 69 6e 20 72 6f 6c 6c 62 61 rwise, in rollba
18840 63 6b 20 6d 6f 64 65 2c 20 74 68 69 73 20 66 75 ck mode, this fu
18841 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20 nction performs
18842 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a two functions:.*
18843 2a 0a 2a 2a 20 20 20 31 29 20 49 74 20 72 6f 6c *.** 1) It rol
18844 6c 73 20 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 ls back the jour
18845 6e 61 6c 20 66 69 6c 65 2c 20 72 65 73 74 6f 72 nal file, restor
18846 69 6e 67 20 61 6c 6c 20 64 61 74 61 62 61 73 65 ing all database
18847 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 20 20 file and .**
18848 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 in-memory cac
18849 68 65 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 he pages to the
1884a 73 74 61 74 65 20 74 68 65 79 20 77 65 72 65 20 state they were
1884b 69 6e 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e in when the tran
1884c 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 saction.**
1884d 77 61 73 20 6f 70 65 6e 65 64 2c 20 61 6e 64 0a was opened, and.
1884e 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 74 20 66 69 **.** 2) It fi
1884f 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 nalizes the jour
18850 6e 61 6c 20 66 69 6c 65 2c 20 73 6f 20 74 68 61 nal file, so tha
18851 74 20 69 74 20 69 73 20 6e 6f 74 20 75 73 65 64 t it is not used
18852 20 66 6f 72 20 68 6f 74 0a 2a 2a 20 20 20 20 20 for hot.**
18853 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6e 79 rollback at any
18854 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 point in the fu
18855 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 61 ture..**.** Fina
18856 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 lization of the
18857 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 74 61 journal file (ta
18858 73 6b 20 32 29 20 69 73 20 6f 6e 6c 79 20 70 65 sk 2) is only pe
18859 72 66 6f 72 6d 65 64 20 69 66 20 74 68 65 20 0a rformed if the .
1885a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 73 ** rollback is s
1885b 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a 0a 2a 2a uccessful..**.**
1885c 20 49 6e 20 57 41 4c 20 6d 6f 64 65 2c 20 61 6c In WAL mode, al
1885d 6c 20 63 61 63 68 65 2d 65 6e 74 72 69 65 73 20 l cache-entries
1885e 63 6f 6e 74 61 69 6e 69 6e 67 20 64 61 74 61 20 containing data
1885f 6d 6f 64 69 66 69 65 64 20 77 69 74 68 69 6e 20 modified within
18860 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 the.** current t
18861 72 61 6e 73 61 63 74 69 6f 6e 20 61 72 65 20 65 ransaction are e
18862 69 74 68 65 72 20 65 78 70 65 6c 6c 65 64 20 66 ither expelled f
18863 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 6f 72 rom the cache or
18864 20 72 65 76 65 72 74 65 64 20 74 6f 0a 2a 2a 20 reverted to.**
18865 74 68 65 69 72 20 70 72 65 2d 74 72 61 6e 73 61 their pre-transa
18866 63 74 69 6f 6e 20 73 74 61 74 65 20 62 79 20 72 ction state by r
18867 65 2d 72 65 61 64 69 6e 67 20 64 61 74 61 20 66 e-reading data f
18868 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
18869 20 6f 72 0a 2a 2a 20 57 41 4c 20 66 69 6c 65 73 or.** WAL files
1886a 2e 20 54 68 65 20 57 41 4c 20 74 72 61 6e 73 61 . The WAL transa
1886b 63 74 69 6f 6e 20 69 73 20 74 68 65 6e 20 63 6c ction is then cl
1886c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f osed..*/.SQLITE_
1886d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1886e 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
1886f 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
18870 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
18871 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
18872 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
18873 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 41 47 45 n code */. PAGE
18874 52 54 52 41 43 45 28 28 22 52 4f 4c 4c 42 41 43 RTRACE(("ROLLBAC
18875 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 K %d\n", PAGERID
18876 28 70 50 61 67 65 72 29 29 29 3b 0a 0a 20 20 2f (pPager)));.. /
18877 2a 20 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 * PagerRollback(
18878 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 ) is a no-op if
18879 63 61 6c 6c 65 64 20 69 6e 20 52 45 41 44 45 52 called in READER
1887a 20 6f 72 20 4f 50 45 4e 20 73 74 61 74 65 2e 20 or OPEN state.
1887b 49 66 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 If. ** the page
1887c 72 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 r is already in
1887d 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 2c the ERROR state,
1887e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 69 73 the rollback is
1887f 20 6e 6f 74 20 0a 20 20 2a 2a 20 61 74 74 65 6d not . ** attem
18880 70 74 65 64 20 68 65 72 65 2e 20 49 6e 73 74 65 pted here. Inste
18881 61 64 2c 20 74 68 65 20 65 72 72 6f 72 20 63 6f ad, the error co
18882 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 de is returned t
18883 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 o the caller..
18884 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 */. assert( ass
18885 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 ert_pager_state(
18886 70 50 61 67 65 72 29 20 29 3b 0a 20 20 69 66 28 pPager) );. if(
18887 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d pPager->eState=
18888 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 29 20 72 =PAGER_ERROR ) r
18889 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 eturn pPager->er
1888a 72 43 6f 64 65 3b 0a 20 20 69 66 28 20 70 50 61 rCode;. if( pPa
1888b 67 65 72 2d 3e 65 53 74 61 74 65 3c 3d 50 41 47 ger->eState<=PAG
1888c 45 52 5f 52 45 41 44 45 52 20 29 20 72 65 74 75 ER_READER ) retu
1888d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 rn SQLITE_OK;..
1888e 20 69 66 28 20 70 61 67 65 72 55 73 65 57 61 6c if( pagerUseWal
1888f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 (pPager) ){.
18890 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 72 63 20 int rc2;. rc
18891 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 = sqlite3PagerSa
18892 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 2c 20 vepoint(pPager,
18893 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
18894 43 4b 2c 20 2d 31 29 3b 0a 20 20 20 20 72 63 32 CK, -1);. rc2
18895 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 = pager_end_tra
18896 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c nsaction(pPager,
18897 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
18898 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d er);. if( rc=
18899 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 =SQLITE_OK ) rc
1889a 3d 20 72 63 32 3b 0a 20 20 7d 65 6c 73 65 20 69 = rc2;. }else i
1889b 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 f( !isOpen(pPage
1889c 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 r->jfd) || pPage
1889d 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 r->eState==PAGER
1889e 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 29 _WRITER_LOCKED )
1889f 7b 0a 20 20 20 20 69 6e 74 20 65 53 74 61 74 65 {. int eState
188a0 20 3d 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 = pPager->eStat
188a1 65 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 e;. rc = page
188a2 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
188a3 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 n(pPager, 0);.
188a4 20 20 69 66 28 20 21 4d 45 4d 44 42 20 26 26 20 if( !MEMDB &&
188a5 65 53 74 61 74 65 3e 50 41 47 45 52 5f 57 52 49 eState>PAGER_WRI
188a6 54 45 52 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 TER_LOCKED ){.
188a7 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6e 20 /* This can
188a8 68 61 70 70 65 6e 20 75 73 69 6e 67 20 6a 6f 75 happen using jou
188a9 72 6e 61 6c 5f 6d 6f 64 65 3d 6f 66 66 2e 20 4d rnal_mode=off. M
188aa 6f 76 65 20 74 68 65 20 70 61 67 65 72 20 74 6f ove the pager to
188ab 20 74 68 65 20 65 72 72 6f 72 20 0a 20 20 20 20 the error .
188ac 20 20 2a 2a 20 73 74 61 74 65 20 74 6f 20 69 6e ** state to in
188ad 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 dicate that the
188ae 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
188af 63 61 63 68 65 20 6d 61 79 20 6e 6f 74 20 62 65 cache may not be
188b0 20 74 72 75 73 74 65 64 2e 0a 20 20 20 20 20 20 trusted..
188b1 2a 2a 20 41 6e 79 20 61 63 74 69 76 65 20 72 65 ** Any active re
188b2 61 64 65 72 73 20 77 69 6c 6c 20 67 65 74 20 53 aders will get S
188b3 51 4c 49 54 45 5f 41 42 4f 52 54 2e 0a 20 20 20 QLITE_ABORT..
188b4 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 67 */. pPag
188b5 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 er->errCode = SQ
188b6 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 20 20 LITE_ABORT;.
188b7 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 pPager->eState
188b8 20 3d 20 50 41 47 45 52 5f 45 52 52 4f 52 3b 0a = PAGER_ERROR;.
188b9 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
188ba 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
188bb 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 rc = pager_p
188bc 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 layback(pPager,
188bd 30 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 0);. }.. asser
188be 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 t( pPager->eStat
188bf 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20 e==PAGER_READER
188c0 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b || rc!=SQLITE_OK
188c1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 );. assert( rc
188c2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 ==SQLITE_OK || r
188c3 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 20 c==SQLITE_FULL.
188c4 20 20 20 20 20 20 20 20 20 7c 7c 20 72 63 3d 3d || rc==
188c5 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 SQLITE_NOMEM ||
188c6 28 72 63 26 30 78 46 46 29 3d 3d 53 51 4c 49 54 (rc&0xFF)==SQLIT
188c7 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 2f 2a E_IOERR );.. /*
188c8 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
188c9 75 72 73 20 64 75 72 69 6e 67 20 61 20 52 4f 4c urs during a ROL
188ca 4c 42 41 43 4b 2c 20 77 65 20 63 61 6e 20 6e 6f LBACK, we can no
188cb 20 6c 6f 6e 67 65 72 20 74 72 75 73 74 20 74 68 longer trust th
188cc 65 20 70 61 67 65 72 0a 20 20 2a 2a 20 63 61 63 e pager. ** cac
188cd 68 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61 67 65 he. So call page
188ce 72 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74 68 65 r_error() on the
188cf 20 77 61 79 20 6f 75 74 20 74 6f 20 6d 61 6b 65 way out to make
188d0 20 61 6e 79 20 65 72 72 6f 72 20 70 65 72 73 69 any error persi
188d1 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 72 65 stent.. */. re
188d2 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 turn pager_error
188d3 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a (pPager, rc);.}.
188d4 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
188d5 55 45 20 69 66 20 74 68 65 20 64 61 74 61 62 61 UE if the databa
188d6 73 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 se file is opene
188d7 64 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 52 65 d read-only. Re
188d8 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66 turn FALSE.** if
188d9 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
188da 20 28 69 6e 20 74 68 65 6f 72 79 29 20 77 72 69 (in theory) wri
188db 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 table..*/.SQLITE
188dc 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 _PRIVATE u8 sqli
188dd 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e te3PagerIsreadon
188de 6c 79 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ly(Pager *pPager
188df 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 ){. return pPag
188e0 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 3b 0a 7d 0a er->readOnly;.}.
188e1 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
188e2 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 e number of refe
188e3 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 rences to the pa
188e4 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ger..*/.SQLITE_P
188e5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
188e6 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 e3PagerRefcount(
188e7 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
188e8 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
188e9 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 PcacheRefCount(p
188ea 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b Pager->pPCache);
188eb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
188ec 20 74 68 65 20 61 70 70 72 6f 78 69 6d 61 74 65 the approximate
188ed 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
188ee 20 6f 66 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 of memory curre
188ef 6e 74 6c 79 0a 2a 2a 20 75 73 65 64 20 62 79 20 ntly.** used by
188f0 74 68 65 20 70 61 67 65 72 20 61 6e 64 20 69 74 the pager and it
188f1 73 20 61 73 73 6f 63 69 61 74 65 64 20 63 61 63 s associated cac
188f2 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 he..*/.SQLITE_PR
188f3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
188f4 33 50 61 67 65 72 4d 65 6d 55 73 65 64 28 50 61 3PagerMemUsed(Pa
188f5 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
188f6 69 6e 74 20 70 65 72 50 61 67 65 53 69 7a 65 20 int perPageSize
188f7 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 = pPager->pageSi
188f8 7a 65 20 2b 20 70 50 61 67 65 72 2d 3e 6e 45 78 ze + pPager->nEx
188f9 74 72 61 20 2b 20 73 69 7a 65 6f 66 28 50 67 48 tra + sizeof(PgH
188fa 64 72 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 dr).
188fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
188fc 20 20 20 20 20 20 20 20 20 2b 20 35 2a 73 69 7a + 5*siz
188fd 65 6f 66 28 76 6f 69 64 2a 29 3b 0a 20 20 72 65 eof(void*);. re
188fe 74 75 72 6e 20 70 65 72 50 61 67 65 53 69 7a 65 turn perPageSize
188ff 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 *sqlite3PcachePa
18900 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e gecount(pPager->
18901 70 50 43 61 63 68 65 29 0a 20 20 20 20 20 20 20 pPCache).
18902 20 20 20 20 2b 20 73 71 6c 69 74 65 33 4d 61 6c + sqlite3Mal
18903 6c 6f 63 53 69 7a 65 28 70 50 61 67 65 72 29 0a locSize(pPager).
18904 20 20 20 20 20 20 20 20 20 20 20 2b 20 70 50 61 + pPa
18905 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d ger->pageSize;.}
18906 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
18907 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 he number of ref
18908 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73 erences to the s
18909 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a pecified page..*
1890a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1890b 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
1890c 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 rPageRefcount(Db
1890d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 Page *pPage){.
1890e 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 return sqlite3Pc
1890f 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 achePageRefcount
18910 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 23 69 66 64 (pPage);.}..#ifd
18911 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f ef SQLITE_TEST./
18912 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
18913 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 e is used for te
18914 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 sting and analys
18915 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 is only..*/.SQLI
18916 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 2a TE_PRIVATE int *
18917 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 61 74 sqlite3PagerStat
18918 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 s(Pager *pPager)
18919 7b 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 61 {. static int a
1891a 5b 31 31 5d 3b 0a 20 20 61 5b 30 5d 20 3d 20 73 [11];. a[0] = s
1891b 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
1891c 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 ount(pPager->pPC
1891d 61 63 68 65 29 3b 0a 20 20 61 5b 31 5d 20 3d 20 ache);. a[1] =
1891e 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 sqlite3PcachePag
1891f 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 ecount(pPager->p
18920 50 43 61 63 68 65 29 3b 0a 20 20 61 5b 32 5d 20 PCache);. a[2]
18921 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 = sqlite3PcacheG
18922 65 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 67 etCachesize(pPag
18923 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 er->pPCache);.
18924 61 5b 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e 65 a[3] = pPager->e
18925 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 State==PAGER_OPE
18926 4e 20 3f 20 2d 31 20 3a 20 28 69 6e 74 29 20 70 N ? -1 : (int) p
18927 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 Pager->dbSize;.
18928 20 61 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[4] = pPager->
18929 65 53 74 61 74 65 3b 0a 20 20 61 5b 35 5d 20 3d eState;. a[5] =
1892a 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
1892b 3b 0a 20 20 61 5b 36 5d 20 3d 20 70 50 61 67 65 ;. a[6] = pPage
1892c 72 2d 3e 61 53 74 61 74 5b 50 41 47 45 52 5f 53 r->aStat[PAGER_S
1892d 54 41 54 5f 48 49 54 5d 3b 0a 20 20 61 5b 37 5d TAT_HIT];. a[7]
1892e 20 3d 20 70 50 61 67 65 72 2d 3e 61 53 74 61 74 = pPager->aStat
1892f 5b 50 41 47 45 52 5f 53 54 41 54 5f 4d 49 53 53 [PAGER_STAT_MISS
18930 5d 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b 20 20 ];. a[8] = 0;
18931 2f 2a 20 55 73 65 64 20 74 6f 20 62 65 20 70 50 /* Used to be pP
18932 61 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 ager->nOvfl */.
18933 20 61 5b 39 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[9] = pPager->
18934 6e 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d 20 3d nRead;. a[10] =
18935 20 70 50 61 67 65 72 2d 3e 61 53 74 61 74 5b 50 pPager->aStat[P
18936 41 47 45 52 5f 53 54 41 54 5f 57 52 49 54 45 5d AGER_STAT_WRITE]
18937 3b 0a 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d 0a ;. return a;.}.
18938 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 #endif../*.** Pa
18939 72 61 6d 65 74 65 72 20 65 53 74 61 74 20 6d 75 rameter eStat mu
1893a 73 74 20 62 65 20 65 69 74 68 65 72 20 53 51 4c st be either SQL
1893b 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43 ITE_DBSTATUS_CAC
1893c 48 45 5f 48 49 54 20 6f 72 0a 2a 2a 20 53 51 4c HE_HIT or.** SQL
1893d 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43 ITE_DBSTATUS_CAC
1893e 48 45 5f 4d 49 53 53 2e 20 42 65 66 6f 72 65 20 HE_MISS. Before
1893f 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 6e 56 61 returning, *pnVa
18940 6c 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 l is incremented
18941 20 62 79 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 by the.** curre
18942 6e 74 20 63 61 63 68 65 20 68 69 74 20 6f 72 20 nt cache hit or
18943 6d 69 73 73 20 63 6f 75 6e 74 2c 20 61 63 63 6f miss count, acco
18944 72 64 69 6e 67 20 74 6f 20 74 68 65 20 76 61 6c rding to the val
18945 75 65 20 6f 66 20 65 53 74 61 74 2e 20 49 66 20 ue of eStat. If
18946 74 68 65 20 0a 2a 2a 20 72 65 73 65 74 20 70 61 the .** reset pa
18947 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a rameter is non-z
18948 65 72 6f 2c 20 74 68 65 20 63 61 63 68 65 20 68 ero, the cache h
18949 69 74 20 6f 72 20 6d 69 73 73 20 63 6f 75 6e 74 it or miss count
1894a 20 69 73 20 7a 65 72 6f 65 64 20 62 65 66 6f 72 is zeroed befor
1894b 65 20 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 2e e .** returning.
1894c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1894d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
1894e 61 67 65 72 43 61 63 68 65 53 74 61 74 28 50 61 agerCacheStat(Pa
1894f 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
18950 20 65 53 74 61 74 2c 20 69 6e 74 20 72 65 73 65 eStat, int rese
18951 74 2c 20 69 6e 74 20 2a 70 6e 56 61 6c 29 7b 0a t, int *pnVal){.
18952 0a 20 20 61 73 73 65 72 74 28 20 65 53 74 61 74 . assert( eStat
18953 3d 3d 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 ==SQLITE_DBSTATU
18954 53 5f 43 41 43 48 45 5f 48 49 54 0a 20 20 20 20 S_CACHE_HIT.
18955 20 20 20 7c 7c 20 65 53 74 61 74 3d 3d 53 51 4c || eStat==SQL
18956 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43 ITE_DBSTATUS_CAC
18957 48 45 5f 4d 49 53 53 0a 20 20 20 20 20 20 20 7c HE_MISS. |
18958 7c 20 65 53 74 61 74 3d 3d 53 51 4c 49 54 45 5f | eStat==SQLITE_
18959 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f 57 DBSTATUS_CACHE_W
1895a 52 49 54 45 0a 20 20 29 3b 0a 0a 20 20 61 73 73 RITE. );.. ass
1895b 65 72 74 28 20 53 51 4c 49 54 45 5f 44 42 53 54 ert( SQLITE_DBST
1895c 41 54 55 53 5f 43 41 43 48 45 5f 48 49 54 2b 31 ATUS_CACHE_HIT+1
1895d 3d 3d 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 ==SQLITE_DBSTATU
1895e 53 5f 43 41 43 48 45 5f 4d 49 53 53 20 29 3b 0a S_CACHE_MISS );.
1895f 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 assert( SQLITE
18960 5f 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f _DBSTATUS_CACHE_
18961 48 49 54 2b 32 3d 3d 53 51 4c 49 54 45 5f 44 42 HIT+2==SQLITE_DB
18962 53 54 41 54 55 53 5f 43 41 43 48 45 5f 57 52 49 STATUS_CACHE_WRI
18963 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 TE );. assert(
18964 50 41 47 45 52 5f 53 54 41 54 5f 48 49 54 3d 3d PAGER_STAT_HIT==
18965 30 20 26 26 20 50 41 47 45 52 5f 53 54 41 54 5f 0 && PAGER_STAT_
18966 4d 49 53 53 3d 3d 31 20 26 26 20 50 41 47 45 52 MISS==1 && PAGER
18967 5f 53 54 41 54 5f 57 52 49 54 45 3d 3d 32 20 29 _STAT_WRITE==2 )
18968 3b 0a 0a 20 20 2a 70 6e 56 61 6c 20 2b 3d 20 70 ;.. *pnVal += p
18969 50 61 67 65 72 2d 3e 61 53 74 61 74 5b 65 53 74 Pager->aStat[eSt
1896a 61 74 20 2d 20 53 51 4c 49 54 45 5f 44 42 53 54 at - SQLITE_DBST
1896b 41 54 55 53 5f 43 41 43 48 45 5f 48 49 54 5d 3b ATUS_CACHE_HIT];
1896c 0a 20 20 69 66 28 20 72 65 73 65 74 20 29 7b 0a . if( reset ){.
1896d 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 74 61 pPager->aSta
1896e 74 5b 65 53 74 61 74 20 2d 20 53 51 4c 49 54 45 t[eStat - SQLITE
1896f 5f 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f _DBSTATUS_CACHE_
18970 48 49 54 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a HIT] = 0;. }.}.
18971 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 ./*.** Return tr
18972 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 6e ue if this is an
18973 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 in-memory pager
18974 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18975 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
18976 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 agerIsMemdb(Page
18977 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 r *pPager){. re
18978 74 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d 0a 0a 2f turn MEMDB;.}../
18979 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 *.** Check that
1897a 74 68 65 72 65 20 61 72 65 20 61 74 20 6c 65 61 there are at lea
1897b 73 74 20 6e 53 61 76 65 70 6f 69 6e 74 20 73 61 st nSavepoint sa
1897c 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2e 20 49 vepoints open. I
1897d 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 63 f there are.** c
1897e 75 72 72 65 6e 74 6c 79 20 6c 65 73 73 20 74 68 urrently less th
1897f 61 6e 20 6e 53 61 76 65 70 6f 69 6e 74 73 20 6f an nSavepoints o
18980 70 65 6e 2c 20 74 68 65 6e 20 6f 70 65 6e 20 6f pen, then open o
18981 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61 76 65 70 ne or more savep
18982 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 6d 61 6b 65 oints.** to make
18983 20 75 70 20 74 68 65 20 64 69 66 66 65 72 65 6e up the differen
18984 63 65 2e 20 49 66 20 74 68 65 20 6e 75 6d 62 65 ce. If the numbe
18985 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 20 r of savepoints
18986 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 71 is already.** eq
18987 75 61 6c 20 74 6f 20 6e 53 61 76 65 70 6f 69 6e ual to nSavepoin
18988 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e t, then this fun
18989 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
1898a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d ..**.** If a mem
1898b 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
1898c 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d ails, SQLITE_NOM
1898d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 EM is returned.
1898e 49 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20 If an error .**
1898f 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6f 70 65 occurs while ope
18990 6e 69 6e 67 20 74 68 65 20 73 75 62 2d 6a 6f 75 ning the sub-jou
18991 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 rnal file, then
18992 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 an IO error code
18993 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e is.** returned.
18994 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 Otherwise, SQLI
18995 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 TE_OK..*/.SQLITE
18996 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
18997 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76 ite3PagerOpenSav
18998 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 epoint(Pager *pP
18999 61 67 65 72 2c 20 69 6e 74 20 6e 53 61 76 65 70 ager, int nSavep
1899a 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 oint){. int rc
1899b 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
1899c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1899d 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
1899e 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72 e */. int nCurr
1899f 65 6e 74 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 ent = pPager->nS
189a0 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 avepoint;
189a1 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 /* Current numb
189a2 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 er of savepoints
189a3 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
189a4 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 Pager->eState>=P
189a5 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b AGER_WRITER_LOCK
189a6 45 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ED );. assert(
189a7 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 assert_pager_sta
189a8 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 te(pPager) );..
189a9 20 69 66 28 20 6e 53 61 76 65 70 6f 69 6e 74 3e if( nSavepoint>
189aa 6e 43 75 72 72 65 6e 74 20 26 26 20 70 50 61 67 nCurrent && pPag
189ab 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 er->useJournal )
189ac 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 {. int ii;
189ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
189ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
189af 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 Iterator variab
189b0 6c 65 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 53 le */. PagerS
189b1 61 76 65 70 6f 69 6e 74 20 2a 61 4e 65 77 3b 20 avepoint *aNew;
189b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
189b3 20 20 2f 2a 20 4e 65 77 20 50 61 67 65 72 2e 61 /* New Pager.a
189b4 53 61 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20 Savepoint array
189b5 2a 2f 0a 0a 20 20 20 20 2f 2a 20 47 72 6f 77 20 */.. /* Grow
189b6 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 70 the Pager.aSavep
189b7 6f 69 6e 74 20 61 72 72 61 79 20 75 73 69 6e 67 oint array using
189b8 20 72 65 61 6c 6c 6f 63 28 29 2e 20 52 65 74 75 realloc(). Retu
189b9 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a rn SQLITE_NOMEM.
189ba 20 20 20 20 2a 2a 20 69 66 20 74 68 65 20 61 6c ** if the al
189bb 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2e 20 location fails.
189bc 4f 74 68 65 72 77 69 73 65 2c 20 7a 65 72 6f 20 Otherwise, zero
189bd 74 68 65 20 6e 65 77 20 70 6f 72 74 69 6f 6e 20 the new portion
189be 69 6e 20 63 61 73 65 20 61 20 0a 20 20 20 20 2a in case a . *
189bf 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 * malloc failure
189c0 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 70 6f occurs while po
189c1 70 75 6c 61 74 69 6e 67 20 69 74 20 69 6e 20 74 pulating it in t
189c2 68 65 20 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f 70 he for(...) loop
189c3 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 below.. */.
189c4 20 20 20 61 4e 65 77 20 3d 20 28 50 61 67 65 72 aNew = (Pager
189c5 53 61 76 65 70 6f 69 6e 74 20 2a 29 73 71 6c 69 Savepoint *)sqli
189c6 74 65 33 52 65 61 6c 6c 6f 63 28 0a 20 20 20 20 te3Realloc(.
189c7 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 pPager->aSav
189c8 65 70 6f 69 6e 74 2c 20 73 69 7a 65 6f 66 28 50 epoint, sizeof(P
189c9 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29 2a 6e agerSavepoint)*n
189ca 53 61 76 65 70 6f 69 6e 74 0a 20 20 20 20 29 3b Savepoint. );
189cb 0a 20 20 20 20 69 66 28 20 21 61 4e 65 77 20 29 . if( !aNew )
189cc 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
189cd 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
189ce 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 61 }. memset(&a
189cf 4e 65 77 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 30 New[nCurrent], 0
189d0 2c 20 28 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 43 , (nSavepoint-nC
189d1 75 72 72 65 6e 74 29 20 2a 20 73 69 7a 65 6f 66 urrent) * sizeof
189d2 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29 (PagerSavepoint)
189d3 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 61 );. pPager->a
189d4 53 61 76 65 70 6f 69 6e 74 20 3d 20 61 4e 65 77 Savepoint = aNew
189d5 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 ;.. /* Popula
189d6 74 65 20 74 68 65 20 50 61 67 65 72 53 61 76 65 te the PagerSave
189d7 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 73 point structures
189d8 20 6a 75 73 74 20 61 6c 6c 6f 63 61 74 65 64 2e just allocated.
189d9 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e */. for(ii=n
189da 43 75 72 72 65 6e 74 3b 20 69 69 3c 6e 53 61 76 Current; ii<nSav
189db 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 epoint; ii++){.
189dc 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 6e 4f aNew[ii].nO
189dd 72 69 67 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 rig = pPager->db
189de 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 Size;. if(
189df 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
189e0 66 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a fd) && pPager->j
189e1 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 29 7b 0a 20 ournalOff>0 ){.
189e2 20 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e aNew[ii].
189e3 69 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 iOffset = pPager
189e4 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 ->journalOff;.
189e5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
189e6 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 aNew[ii].iOff
189e7 73 65 74 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 set = JOURNAL_HD
189e8 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 R_SZ(pPager);.
189e9 20 20 20 20 7d 0a 20 20 20 20 20 20 61 4e 65 77 }. aNew
189ea 5b 69 69 5d 2e 69 53 75 62 52 65 63 20 3d 20 70 [ii].iSubRec = p
189eb 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b 0a Pager->nSubRec;.
189ec 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 70 aNew[ii].p
189ed 49 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 73 71 InSavepoint = sq
189ee 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 lite3BitvecCreat
189ef 65 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 e(pPager->dbSize
189f0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 61 4e );. if( !aN
189f1 65 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f ew[ii].pInSavepo
189f2 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 int ){. r
189f3 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
189f4 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 EM;. }.
189f5 20 20 69 66 28 20 70 61 67 65 72 55 73 65 57 61 if( pagerUseWa
189f6 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 l(pPager) ){.
189f7 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 53 sqlite3WalS
189f8 61 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 2d avepoint(pPager-
189f9 3e 70 57 61 6c 2c 20 61 4e 65 77 5b 69 69 5d 2e >pWal, aNew[ii].
189fa 61 57 61 6c 44 61 74 61 29 3b 0a 20 20 20 20 20 aWalData);.
189fb 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d }. pPager-
189fc 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 69 69 >nSavepoint = ii
189fd 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 +1;. }. as
189fe 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 sert( pPager->nS
189ff 61 76 65 70 6f 69 6e 74 3d 3d 6e 53 61 76 65 70 avepoint==nSavep
18a00 6f 69 6e 74 20 29 3b 0a 20 20 20 20 61 73 73 65 oint );. asse
18a01 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 rtTruncateConstr
18a02 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a 20 20 aint(pPager);.
18a03 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }.. return rc;.
18a04 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
18a05 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
18a06 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 to rollback or
18a07 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 release (commit)
18a08 20 61 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a a savepoint..**
18a09 20 54 68 65 20 73 61 76 65 70 6f 69 6e 74 20 74 The savepoint t
18a0a 6f 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c o release or rol
18a0b 6c 62 61 63 6b 20 6e 65 65 64 20 6e 6f 74 20 62 lback need not b
18a0c 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e e the most recen
18a0d 74 6c 79 20 0a 2a 2a 20 63 72 65 61 74 65 64 20 tly .** created
18a0e 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a savepoint..**.**
18a0f 20 50 61 72 61 6d 65 74 65 72 20 6f 70 20 69 73 Parameter op is
18a10 20 61 6c 77 61 79 73 20 65 69 74 68 65 72 20 53 always either S
18a11 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
18a12 4b 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 K or SAVEPOINT_R
18a13 45 4c 45 41 53 45 2e 0a 2a 2a 20 49 66 20 69 74 ELEASE..** If it
18a14 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 is SAVEPOINT_RE
18a15 4c 45 41 53 45 2c 20 74 68 65 6e 20 72 65 6c 65 LEASE, then rele
18a16 61 73 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20 ase and destroy
18a17 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 77 69 the savepoint wi
18a18 74 68 0a 2a 2a 20 69 6e 64 65 78 20 69 53 61 76 th.** index iSav
18a19 65 70 6f 69 6e 74 2e 20 49 66 20 69 74 20 69 73 epoint. If it is
18a1a 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 SAVEPOINT_ROLLB
18a1b 41 43 4b 2c 20 74 68 65 6e 20 72 6f 6c 6c 62 61 ACK, then rollba
18a1c 63 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 0a 2a ck all changes.*
18a1d 2a 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 75 * that have occu
18a1e 72 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 73 rred since the s
18a1f 70 65 63 69 66 69 65 64 20 73 61 76 65 70 6f 69 pecified savepoi
18a20 6e 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 0a nt was created..
18a21 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 76 65 70 6f **.** The savepo
18a22 69 6e 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 int to rollback
18a23 6f 72 20 72 65 6c 65 61 73 65 20 69 73 20 69 64 or release is id
18a24 65 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61 entified by para
18a25 6d 65 74 65 72 20 0a 2a 2a 20 69 53 61 76 65 70 meter .** iSavep
18a26 6f 69 6e 74 2e 20 41 20 76 61 6c 75 65 20 6f 66 oint. A value of
18a27 20 30 20 6d 65 61 6e 73 20 74 6f 20 6f 70 65 72 0 means to oper
18a28 61 74 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 ate on the outer
18a29 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 0a 2a most savepoint.*
18a2a 2a 20 28 74 68 65 20 66 69 72 73 74 20 63 72 65 * (the first cre
18a2b 61 74 65 64 29 2e 20 41 20 76 61 6c 75 65 20 6f ated). A value o
18a2c 66 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f f (Pager.nSavepo
18a2d 69 6e 74 2d 31 29 20 6d 65 61 6e 73 20 6f 70 65 int-1) means ope
18a2e 72 61 74 65 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d rate.** on the m
18a2f 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72 65 ost recently cre
18a30 61 74 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 ated savepoint.
18a31 49 66 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 If iSavepoint is
18a32 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a greater than.**
18a33 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 (Pager.nSavepoi
18a34 6e 74 2d 31 29 2c 20 74 68 65 6e 20 74 68 69 73 nt-1), then this
18a35 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
18a36 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 o-op..**.** If a
18a37 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 negative value
18a38 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 is passed to thi
18a39 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e s function, then
18a3a 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
18a3b 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 transaction is r
18a3c 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 olled back. This
18a3d 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 74 6f is different to
18a3e 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c calling .** sql
18a3f 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 ite3PagerRollbac
18a40 6b 28 29 20 62 65 63 61 75 73 65 20 74 68 69 73 k() because this
18a41 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e function does n
18a42 6f 74 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 ot terminate.**
18a43 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
18a44 6f 72 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 or unlock the da
18a45 74 61 62 61 73 65 2c 20 69 74 20 6a 75 73 74 20 tabase, it just
18a46 72 65 73 74 6f 72 65 73 20 74 68 65 20 0a 2a 2a restores the .**
18a47 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
18a48 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 database to its
18a49 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 2e original state.
18a4a 20 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 79 20 63 .**.** In any c
18a4b 61 73 65 2c 20 61 6c 6c 20 73 61 76 65 70 6f 69 ase, all savepoi
18a4c 6e 74 73 20 77 69 74 68 20 61 6e 20 69 6e 64 65 nts with an inde
18a4d 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69 x greater than i
18a4e 53 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 61 72 Savepoint .** ar
18a4f 65 20 64 65 73 74 72 6f 79 65 64 2e 20 49 66 20 e destroyed. If
18a50 74 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73 this is a releas
18a51 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6f 70 3d e operation (op=
18a52 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 =SAVEPOINT_RELEA
18a53 53 45 29 2c 0a 2a 2a 20 74 68 65 6e 20 73 61 76 SE),.** then sav
18a54 65 70 6f 69 6e 74 20 69 53 61 76 65 70 6f 69 6e epoint iSavepoin
18a55 74 20 69 73 20 61 6c 73 6f 20 64 65 73 74 72 6f t is also destro
18a56 79 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 yed..**.** This
18a57 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 function may ret
18a58 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
18a59 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c if a memory all
18a5a 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a ocation fails,.*
18a5b 2a 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 * or an IO error
18a5c 20 63 6f 64 65 20 69 66 20 61 6e 20 49 4f 20 65 code if an IO e
18a5d 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
18a5e 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 e rolling back a
18a5f 20 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e 20 .** savepoint.
18a60 49 66 20 6e 6f 20 65 72 72 6f 72 73 20 6f 63 63 If no errors occ
18a61 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 ur, SQLITE_OK is
18a62 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 53 returned..*/ .S
18a63 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
18a64 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 t sqlite3PagerSa
18a65 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
18a66 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 Pager, int op, i
18a67 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a nt iSavepoint){.
18a68 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61 67 65 int rc = pPage
18a69 72 2d 3e 65 72 72 43 6f 64 65 3b 20 20 20 20 20 r->errCode;
18a6a 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
18a6b 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 6f */.. assert( o
18a6c 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c p==SAVEPOINT_REL
18a6d 45 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 EASE || op==SAVE
18a6e 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 POINT_ROLLBACK )
18a6f 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 61 76 ;. assert( iSav
18a70 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f 70 3d epoint>=0 || op=
18a71 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
18a72 41 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 ACK );.. if( rc
18a73 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 ==SQLITE_OK && i
18a74 53 61 76 65 70 6f 69 6e 74 3c 70 50 61 67 65 72 Savepoint<pPager
18a75 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a ->nSavepoint ){.
18a76 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 int ii;
18a77 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 /* Iterat
18a78 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 or variable */.
18a79 20 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20 int nNew;
18a7a 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
18a7b 6f 66 20 72 65 6d 61 69 6e 69 6e 67 20 73 61 76 of remaining sav
18a7c 65 70 6f 69 6e 74 73 20 61 66 74 65 72 20 74 68 epoints after th
18a7d 69 73 20 6f 70 2e 20 2a 2f 0a 0a 20 20 20 20 2f is op. */.. /
18a7e 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 * Figure out how
18a7f 20 6d 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 many savepoints
18a80 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 20 61 will still be a
18a81 63 74 69 76 65 20 61 66 74 65 72 20 74 68 69 73 ctive after this
18a82 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f . ** operatio
18a83 6e 2e 20 53 74 6f 72 65 20 74 68 69 73 20 76 61 n. Store this va
18a84 6c 75 65 20 69 6e 20 6e 4e 65 77 2e 20 54 68 65 lue in nNew. The
18a85 6e 20 66 72 65 65 20 72 65 73 6f 75 72 63 65 73 n free resources
18a86 20 61 73 73 6f 63 69 61 74 65 64 20 0a 20 20 20 associated .
18a87 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 73 61 76 ** with any sav
18a88 65 70 6f 69 6e 74 73 20 74 68 61 74 20 61 72 65 epoints that are
18a89 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 74 68 destroyed by th
18a8a 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 is operation..
18a8b 20 20 2a 2f 0a 20 20 20 20 6e 4e 65 77 20 3d 20 */. nNew =
18a8c 69 53 61 76 65 70 6f 69 6e 74 20 2b 20 28 28 20 iSavepoint + ((
18a8d 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 op==SAVEPOINT_RE
18a8e 4c 45 41 53 45 20 29 20 3f 20 30 20 3a 20 31 29 LEASE ) ? 0 : 1)
18a8f 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e 4e 65 ;. for(ii=nNe
18a90 77 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 w; ii<pPager->nS
18a91 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b avepoint; ii++){
18a92 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 . sqlite3Bi
18a93 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 tvecDestroy(pPag
18a94 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 er->aSavepoint[i
18a95 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 i].pInSavepoint)
18a96 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 ;. }. pPag
18a97 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d er->nSavepoint =
18a98 20 6e 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a 20 49 nNew;.. /* I
18a99 66 20 74 68 69 73 20 69 73 20 61 20 72 65 6c 65 f this is a rele
18a9a 61 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 ase of the outer
18a9b 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 2c 20 most savepoint,
18a9c 74 72 75 6e 63 61 74 65 20 0a 20 20 20 20 2a 2a truncate . **
18a9d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
18a9e 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 to zero bytes i
18a9f 6e 20 73 69 7a 65 2e 20 2a 2f 0a 20 20 20 20 69 n size. */. i
18aa0 66 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 f( op==SAVEPOINT
18aa1 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 _RELEASE ){.
18aa2 20 20 69 66 28 20 6e 4e 65 77 3d 3d 30 20 26 26 if( nNew==0 &&
18aa3 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
18aa4 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20 sjfd) ){.
18aa5 20 2f 2a 20 4f 6e 6c 79 20 74 72 75 6e 63 61 74 /* Only truncat
18aa6 65 20 69 66 20 69 74 20 69 73 20 61 6e 20 69 6e e if it is an in
18aa7 2d 6d 65 6d 6f 72 79 20 73 75 62 2d 6a 6f 75 72 -memory sub-jour
18aa8 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 nal. */.
18aa9 69 66 28 20 73 71 6c 69 74 65 33 49 73 4d 65 6d if( sqlite3IsMem
18aaa 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e Journal(pPager->
18aab 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20 sjfd) ){.
18aac 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
18aad 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 sTruncate(pPager
18aae 2d 3e 73 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 ->sjfd, 0);.
18aaf 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
18ab0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 ==SQLITE_OK );.
18ab1 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
18ab2 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 pPager->nSubRec
18ab3 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
18ab4 20 20 7d 0a 20 20 20 20 2f 2a 20 45 6c 73 65 20 }. /* Else
18ab5 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 this is a rollba
18ab6 63 6b 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 6c ck operation, pl
18ab7 61 79 62 61 63 6b 20 74 68 65 20 73 70 65 63 69 ayback the speci
18ab8 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a fied savepoint..
18ab9 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 ** If this i
18aba 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 2c 20 69 s a temp-file, i
18abb 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 t is possible th
18abc 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 at the journal f
18abd 69 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e ile has. ** n
18abe 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e ot yet been open
18abf 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
18ac0 20 74 68 65 72 65 20 68 61 76 65 20 62 65 65 6e there have been
18ac1 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 0a 20 no changes to.
18ac2 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 ** the databa
18ac3 73 65 20 66 69 6c 65 2c 20 73 6f 20 74 68 65 20 se file, so the
18ac4 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69 playback operati
18ac5 6f 6e 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 on can be skippe
18ac6 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 6c d.. */. el
18ac7 73 65 20 69 66 28 20 70 61 67 65 72 55 73 65 57 se if( pagerUseW
18ac8 61 6c 28 70 50 61 67 65 72 29 20 7c 7c 20 69 73 al(pPager) || is
18ac9 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
18aca 29 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 ) ){. Pager
18acb 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 Savepoint *pSave
18acc 70 6f 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30 point = (nNew==0
18acd 29 3f 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61 )?0:&pPager->aSa
18ace 76 65 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b vepoint[nNew-1];
18acf 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
18ad0 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 rPlaybackSavepoi
18ad1 6e 74 28 70 50 61 67 65 72 2c 20 70 53 61 76 65 nt(pPager, pSave
18ad2 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73 point);. as
18ad3 73 65 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f sert(rc!=SQLITE_
18ad4 44 4f 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 7d DONE);. }. }
18ad5 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
18ad6 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
18ad7 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 he full pathname
18ad8 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
18ad9 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 file..**.** Exc
18ada 65 70 74 2c 20 69 66 20 74 68 65 20 70 61 67 65 ept, if the page
18adb 72 20 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 6f r is in-memory o
18adc 6e 6c 79 2c 20 74 68 65 6e 20 72 65 74 75 72 6e nly, then return
18add 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 an empty string
18ade 20 69 66 0a 2a 2a 20 6e 75 6c 6c 49 66 4d 65 6d if.** nullIfMem
18adf 44 62 20 69 73 20 74 72 75 65 2e 20 20 54 68 69 Db is true. Thi
18ae0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
18ae1 6c 65 64 20 77 69 74 68 20 6e 75 6c 6c 49 66 4d led with nullIfM
18ae2 65 6d 44 62 3d 3d 31 20 77 68 65 6e 0a 2a 2a 20 emDb==1 when.**
18ae3 75 73 65 64 20 74 6f 20 72 65 70 6f 72 74 20 74 used to report t
18ae4 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 74 he filename to t
18ae5 68 65 20 75 73 65 72 2c 20 66 6f 72 20 63 6f 6d he user, for com
18ae6 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 patibility with
18ae7 6c 65 67 61 63 79 0a 2a 2a 20 62 65 68 61 76 69 legacy.** behavi
18ae8 6f 72 2e 20 20 42 75 74 20 77 68 65 6e 20 74 68 or. But when th
18ae9 65 20 42 74 72 65 65 20 6e 65 65 64 73 20 74 6f e Btree needs to
18aea 20 6b 6e 6f 77 20 74 68 65 20 66 69 6c 65 6e 61 know the filena
18aeb 6d 65 20 66 6f 72 20 6d 61 74 63 68 69 6e 67 20 me for matching
18aec 74 6f 0a 2a 2a 20 73 68 61 72 65 64 20 63 61 63 to.** shared cac
18aed 68 65 2c 20 69 74 20 75 73 65 73 20 6e 75 6c 6c he, it uses null
18aee 49 66 4d 65 6d 44 62 3d 3d 30 20 73 6f 20 74 68 IfMemDb==0 so th
18aef 61 74 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 at in-memory dat
18af0 61 62 61 73 65 73 20 63 61 6e 0a 2a 2a 20 70 61 abases can.** pa
18af1 72 74 69 63 69 70 61 74 65 20 69 6e 20 73 68 61 rticipate in sha
18af2 72 65 64 2d 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 red-cache..*/.SQ
18af3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
18af4 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
18af5 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 PagerFilename(Pa
18af6 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
18af7 20 6e 75 6c 6c 49 66 4d 65 6d 44 62 29 7b 0a 20 nullIfMemDb){.
18af8 20 72 65 74 75 72 6e 20 28 6e 75 6c 6c 49 66 4d return (nullIfM
18af9 65 6d 44 62 20 26 26 20 70 50 61 67 65 72 2d 3e emDb && pPager->
18afa 6d 65 6d 44 62 29 20 3f 20 22 22 20 3a 20 70 50 memDb) ? "" : pP
18afb 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b ager->zFilename;
18afc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
18afd 20 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 the VFS structu
18afe 72 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 72 re for the pager
18aff 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18b00 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ATE const sqlite
18b01 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 3_vfs *sqlite3Pa
18b02 67 65 72 56 66 73 28 50 61 67 65 72 20 2a 70 50 gerVfs(Pager *pP
18b03 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
18b04 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a pPager->pVfs;.}.
18b05 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
18b06 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f e file handle fo
18b07 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 r the database f
18b08 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a ile associated.*
18b09 2a 20 77 69 74 68 20 74 68 65 20 70 61 67 65 72 * with the pager
18b0a 2e 20 20 54 68 69 73 20 6d 69 67 68 74 20 72 65 . This might re
18b0b 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 turn NULL if the
18b0c 20 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74 file has.** not
18b0d 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 yet been opened
18b0e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18b0f 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ATE sqlite3_file
18b10 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 *sqlite3PagerFi
18b11 6c 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 le(Pager *pPager
18b12 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 ){. return pPag
18b13 65 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a er->fd;.}../*.**
18b14 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c Return the full
18b15 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 pathname of the
18b16 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a journal file..*
18b17 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18b18 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
18b19 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c ite3PagerJournal
18b1a 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 name(Pager *pPag
18b1b 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 er){. return pP
18b1c 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a ager->zJournal;.
18b1d 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
18b1e 74 72 75 65 20 69 66 20 66 73 79 6e 63 28 29 20 true if fsync()
18b1f 63 61 6c 6c 73 20 61 72 65 20 64 69 73 61 62 6c calls are disabl
18b20 65 64 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 ed for this page
18b21 72 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 r. Return FALSE
18b22 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28 29 73 20 .** if fsync()s
18b23 61 72 65 20 65 78 65 63 75 74 65 64 20 6e 6f 72 are executed nor
18b24 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 mally..*/.SQLITE
18b25 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
18b26 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 ite3PagerNosync(
18b27 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
18b28 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
18b29 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 64 >noSync;.}..#ifd
18b2a 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f ef SQLITE_HAS_CO
18b2b 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 DEC./*.** Set or
18b2c 20 72 65 74 72 69 65 76 65 20 74 68 65 20 63 6f retrieve the co
18b2d 64 65 63 20 66 6f 72 20 74 68 69 73 20 70 61 67 dec for this pag
18b2e 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 er.*/.SQLITE_PRI
18b2f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
18b30 33 50 61 67 65 72 53 65 74 43 6f 64 65 63 28 0a 3PagerSetCodec(.
18b31 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c Pager *pPager,
18b32 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f 64 65 . void *(*xCode
18b33 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 c)(void*,void*,P
18b34 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 64 gno,int),. void
18b35 20 28 2a 78 43 6f 64 65 63 53 69 7a 65 43 68 6e (*xCodecSizeChn
18b36 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 g)(void*,int,int
18b37 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 ),. void (*xCod
18b38 65 63 46 72 65 65 29 28 76 6f 69 64 2a 29 2c 0a ecFree)(void*),.
18b39 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 0a 29 void *pCodec.)
18b3a 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e {. if( pPager->
18b3b 78 43 6f 64 65 63 46 72 65 65 20 29 20 70 50 61 xCodecFree ) pPa
18b3c 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 28 ger->xCodecFree(
18b3d 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 29 3b pPager->pCodec);
18b3e 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 . pPager->xCode
18b3f 63 20 3d 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 c = pPager->memD
18b40 62 20 3f 20 30 20 3a 20 78 43 6f 64 65 63 3b 0a b ? 0 : xCodec;.
18b41 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 pPager->xCodec
18b42 53 69 7a 65 43 68 6e 67 20 3d 20 78 43 6f 64 65 SizeChng = xCode
18b43 63 53 69 7a 65 43 68 6e 67 3b 0a 20 20 70 50 61 cSizeChng;. pPa
18b44 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 ger->xCodecFree
18b45 3d 20 78 43 6f 64 65 63 46 72 65 65 3b 0a 20 20 = xCodecFree;.
18b46 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 20 3d pPager->pCodec =
18b47 20 70 43 6f 64 65 63 3b 0a 20 20 70 61 67 65 72 pCodec;. pager
18b48 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 65 ReportSize(pPage
18b49 72 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 r);.}.SQLITE_PRI
18b4a 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
18b4b 65 33 50 61 67 65 72 47 65 74 43 6f 64 65 63 28 e3PagerGetCodec(
18b4c 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
18b4d 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
18b4e 3e 70 43 6f 64 65 63 3b 0a 7d 0a 23 65 6e 64 69 >pCodec;.}.#endi
18b4f 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
18b50 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
18b51 4d 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 M./*.** Move the
18b52 20 70 61 67 65 20 70 50 67 20 74 6f 20 6c 6f 63 page pPg to loc
18b53 61 74 69 6f 6e 20 70 67 6e 6f 20 69 6e 20 74 68 ation pgno in th
18b54 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e file..**.** Th
18b55 65 72 65 20 6d 75 73 74 20 62 65 20 6e 6f 20 72 ere must be no r
18b56 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 eferences to the
18b57 20 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79 page previously
18b58 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 70 located at.** p
18b59 67 6e 6f 20 28 77 68 69 63 68 20 77 65 20 63 61 gno (which we ca
18b5a 6c 6c 20 70 50 67 4f 6c 64 29 20 74 68 6f 75 67 ll pPgOld) thoug
18b5b 68 20 74 68 61 74 20 70 61 67 65 20 69 73 20 61 h that page is a
18b5c 6c 6c 6f 77 65 64 20 74 6f 20 62 65 0a 2a 2a 20 llowed to be.**
18b5d 69 6e 20 63 61 63 68 65 2e 20 20 49 66 20 74 68 in cache. If th
18b5e 65 20 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c e page previousl
18b5f 79 20 6c 6f 63 61 74 65 64 20 61 74 20 70 67 6e y located at pgn
18b60 6f 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 o is not already
18b61 0a 2a 2a 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 .** in the rollb
18b62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 ack journal, it
18b63 69 73 20 6e 6f 74 20 70 75 74 20 74 68 65 72 65 is not put there
18b64 20 62 79 20 62 79 20 74 68 69 73 20 72 6f 75 74 by by this rout
18b65 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 ine..**.** Refer
18b66 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 ences to the pag
18b67 65 20 70 50 67 20 72 65 6d 61 69 6e 20 76 61 6c e pPg remain val
18b68 69 64 2e 20 55 70 64 61 74 69 6e 67 20 61 6e 79 id. Updating any
18b69 0a 2a 2a 20 6d 65 74 61 2d 64 61 74 61 20 61 73 .** meta-data as
18b6a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 50 sociated with pP
18b6b 67 20 28 69 2e 65 2e 20 64 61 74 61 20 73 74 6f g (i.e. data sto
18b6c 72 65 64 20 69 6e 20 74 68 65 20 6e 45 78 74 72 red in the nExtr
18b6d 61 20 62 79 74 65 73 0a 2a 2a 20 61 6c 6c 6f 63 a bytes.** alloc
18b6e 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 ated along with
18b6f 74 68 65 20 70 61 67 65 29 20 69 73 20 74 68 65 the page) is the
18b70 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 responsibility
18b71 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a of the caller..*
18b72 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69 *.** A transacti
18b73 6f 6e 20 6d 75 73 74 20 62 65 20 61 63 74 69 76 on must be activ
18b74 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 e when this rout
18b75 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 49 ine is called. I
18b76 74 20 75 73 65 64 20 74 6f 20 62 65 0a 2a 2a 20 t used to be.**
18b77 72 65 71 75 69 72 65 64 20 74 68 61 74 20 61 20 required that a
18b78 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
18b79 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 61 63 ction was not ac
18b7a 74 69 76 65 2c 20 62 75 74 20 74 68 69 73 20 72 tive, but this r
18b7b 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a 20 68 61 estriction.** ha
18b7c 73 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 28 s been removed (
18b7d 43 52 45 41 54 45 20 49 4e 44 45 58 20 6e 65 65 CREATE INDEX nee
18b7e 64 73 20 74 6f 20 6d 6f 76 65 20 61 20 70 61 67 ds to move a pag
18b7f 65 20 77 68 65 6e 20 61 20 73 74 61 74 65 6d 65 e when a stateme
18b80 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f nt.** transactio
18b81 6e 20 69 73 20 61 63 74 69 76 65 29 2e 0a 2a 2a n is active)..**
18b82 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 75 72 74 .** If the fourt
18b83 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 73 43 6f h argument, isCo
18b84 6d 6d 69 74 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 mmit, is non-zer
18b85 6f 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61 67 o, then this pag
18b86 65 20 69 73 20 62 65 69 6e 67 0a 2a 2a 20 6d 6f e is being.** mo
18b87 76 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 ved as part of a
18b88 20 64 61 74 61 62 61 73 65 20 72 65 6f 72 67 61 database reorga
18b89 6e 69 7a 61 74 69 6f 6e 20 6a 75 73 74 20 62 65 nization just be
18b8a 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 fore the transac
18b8b 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 62 65 69 6e tion .** is bein
18b8c 67 20 63 6f 6d 6d 69 74 74 65 64 2e 20 49 6e 20 g committed. In
18b8d 74 68 69 73 20 63 61 73 65 2c 20 69 74 20 69 73 this case, it is
18b8e 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 guaranteed that
18b8f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 the database pa
18b90 67 65 20 0a 2a 2a 20 70 50 67 20 72 65 66 65 72 ge .** pPg refer
18b91 73 20 74 6f 20 77 69 6c 6c 20 6e 6f 74 20 62 65 s to will not be
18b92 20 77 72 69 74 74 65 6e 20 74 6f 20 61 67 61 69 written to agai
18b93 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 n within this tr
18b94 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a ansaction..**.**
18b95 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d This function m
18b96 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ay return SQLITE
18b97 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20 _NOMEM or an IO
18b98 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e error code if an
18b99 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 error.** occurs
18b9a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 . Otherwise, it
18b9b 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f returns SQLITE_O
18b9c 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 K..*/.SQLITE_PRI
18b9d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
18b9e 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50 61 PagerMovepage(Pa
18b9f 67 65 72 20 2a 70 50 61 67 65 72 2c 20 44 62 50 ger *pPager, DbP
18ba0 61 67 65 20 2a 70 50 67 2c 20 50 67 6e 6f 20 70 age *pPg, Pgno p
18ba1 67 6e 6f 2c 20 69 6e 74 20 69 73 43 6f 6d 6d 69 gno, int isCommi
18ba2 74 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 t){. PgHdr *pPg
18ba3 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 Old;
18ba4 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 62 /* The page b
18ba5 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e eing overwritten
18ba6 2e 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 65 65 64 . */. Pgno need
18ba7 53 79 6e 63 50 67 6e 6f 20 3d 20 30 3b 20 20 20 SyncPgno = 0;
18ba8 20 20 20 20 2f 2a 20 4f 6c 64 20 76 61 6c 75 65 /* Old value
18ba9 20 6f 66 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 69 of pPg->pgno, i
18baa 66 20 73 79 6e 63 20 69 73 20 72 65 71 75 69 72 f sync is requir
18bab 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 ed */. int rc;
18bac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18bad 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
18bae 6f 64 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6f 72 ode */. Pgno or
18baf 69 67 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 igPgno;
18bb0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 /* The ori
18bb1 67 69 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 ginal page numbe
18bb2 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 r */.. assert(
18bb3 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 pPg->nRef>0 );.
18bb4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
18bb5 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 >eState==PAGER_W
18bb6 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 0a 20 RITER_CACHEMOD.
18bb7 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d || pPager-
18bb8 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 >eState==PAGER_W
18bb9 52 49 54 45 52 5f 44 42 4d 4f 44 0a 20 20 29 3b RITER_DBMOD. );
18bba 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 . assert( asser
18bbb 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 t_pager_state(pP
18bbc 61 67 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 ager) );.. /* I
18bbd 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20 61 62 n order to be ab
18bbe 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 le to rollback,
18bbf 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
18bc0 61 62 61 73 65 20 6d 75 73 74 20 6a 6f 75 72 6e abase must journ
18bc1 61 6c 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 al. ** the page
18bc2 20 77 65 20 61 72 65 20 6d 6f 76 69 6e 67 20 66 we are moving f
18bc3 72 6f 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 rom.. */. if(
18bc4 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 72 63 20 MEMDB ){. rc
18bc5 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
18bc6 69 74 65 28 70 50 67 29 3b 0a 20 20 20 20 69 66 ite(pPg);. if
18bc7 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
18bc8 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 ;. }.. /* If t
18bc9 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6d 6f he page being mo
18bca 76 65 64 20 69 73 20 64 69 72 74 79 20 61 6e 64 ved is dirty and
18bcb 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 61 has not been sa
18bcc 76 65 64 20 62 79 20 74 68 65 20 6c 61 74 65 73 ved by the lates
18bcd 74 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 t. ** savepoint
18bce 2c 20 74 68 65 6e 20 73 61 76 65 20 74 68 65 20 , then save the
18bcf 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 current contents
18bd0 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e 74 of the page int
18bd1 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d o the . ** sub-
18bd2 6a 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 68 69 journal now. Thi
18bd3 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f s is required to
18bd4 20 68 61 6e 64 6c 65 20 74 68 65 20 66 6f 6c 6c handle the foll
18bd5 6f 77 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a 0a owing scenario:.
18bd6 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 **. ** BEGI
18bd7 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 N;. ** <jou
18bd8 72 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 68 65 rnal page X, the
18bd9 6e 20 6d 6f 64 69 66 79 20 69 74 20 69 6e 20 6d n modify it in m
18bda 65 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 20 20 emory>. **
18bdb 53 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 SAVEPOINT one;.
18bdc 20 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65 20 ** <Move
18bdd 70 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74 69 page X to locati
18bde 6f 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 20 52 on Y>. ** R
18bdf 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b 0a OLLBACK TO one;.
18be0 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 61 67 **. ** If pag
18be1 65 20 58 20 77 65 72 65 20 6e 6f 74 20 77 72 69 e X were not wri
18be2 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 2d tten to the sub-
18be3 6a 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 69 74 journal here, it
18be4 20 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a 2a 20 would not. **
18be5 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 be possible to r
18be6 65 73 74 6f 72 65 20 69 74 73 20 63 6f 6e 74 65 estore its conte
18be7 6e 74 73 20 77 68 65 6e 20 74 68 65 20 22 52 4f nts when the "RO
18be8 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 0a 20 LLBACK TO one".
18be9 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 ** statement we
18bea 72 65 20 69 73 20 70 72 6f 63 65 73 73 65 64 2e re is processed.
18beb 0a 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 6a 6f . **. ** subjo
18bec 75 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 79 20 urnalPage() may
18bed 6e 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 need to allocate
18bee 20 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20 space to store
18bef 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f 0a 20 pPg->pgno into.
18bf0 20 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 ** one or more
18bf1 73 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 savepoint bitvec
18bf2 73 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 72 s. This is the r
18bf3 65 61 73 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 eason this funct
18bf4 69 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 65 74 ion. ** may ret
18bf5 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
18bf6 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 .. */. if( pPg
18bf7 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
18bf8 52 54 59 0a 20 20 20 26 26 20 73 75 62 6a 52 65 RTY. && subjRe
18bf9 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 0a quiresPage(pPg).
18bfa 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 && SQLITE_OK!
18bfb 3d 28 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 =(rc = subjourna
18bfc 6c 50 61 67 65 28 70 50 67 29 29 0a 20 20 29 7b lPage(pPg)). ){
18bfd 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
18bfe 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 }.. PAGERTRAC
18bff 45 28 28 22 4d 4f 56 45 20 25 64 20 70 61 67 65 E(("MOVE %d page
18c00 20 25 64 20 28 6e 65 65 64 53 79 6e 63 3d 25 64 %d (needSync=%d
18c01 29 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c 6e 22 ) moves to %d\n"
18c02 2c 20 0a 20 20 20 20 20 20 50 41 47 45 52 49 44 , . PAGERID
18c03 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 (pPager), pPg->p
18c04 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c 61 67 73 gno, (pPg->flags
18c05 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
18c06 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 3b 0a 20 )?1:0, pgno));.
18c07 20 49 4f 54 52 41 43 45 28 28 22 4d 4f 56 45 20 IOTRACE(("MOVE
18c08 25 70 20 25 64 20 25 64 5c 6e 22 2c 20 70 50 61 %p %d %d\n", pPa
18c09 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 ger, pPg->pgno,
18c0a 70 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 49 66 20 pgno)).. /* If
18c0b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 the journal need
18c0c 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 s to be sync()ed
18c0d 20 62 65 66 6f 72 65 20 70 61 67 65 20 70 50 67 before page pPg
18c0e 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a 2a 20 ->pgno can. **
18c0f 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 73 be written to, s
18c10 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f 20 69 tore pPg->pgno i
18c11 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 n local variable
18c12 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0a 20 needSyncPgno..
18c13 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 **. ** If the
18c14 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 isCommit flag is
18c15 20 73 65 74 2c 20 74 68 65 72 65 20 69 73 20 6e set, there is n
18c16 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 o need to rememb
18c17 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 er that. ** the
18c18 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 journal needs t
18c19 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 o be sync()ed be
18c1a 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61 fore database pa
18c1b 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a 20 20 ge pPg->pgno .
18c1c 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 ** can be writte
18c1d 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72 n to. The caller
18c1e 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f has already pro
18c1f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69 mised not to wri
18c20 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f 0a 20 te to it.. */.
18c21 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 if( (pPg->flags
18c22 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
18c23 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 20 29 ) && !isCommit )
18c24 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 50 67 {. needSyncPg
18c25 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a no = pPg->pgno;.
18c26 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 assert( page
18c27 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c InJournal(pPg) |
18c28 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 | pPg->pgno>pPag
18c29 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 er->dbOrigSize )
18c2a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 ;. assert( pP
18c2b 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 g->flags&PGHDR_D
18c2c 49 52 54 59 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f IRTY );. }.. /
18c2d 2a 20 49 66 20 74 68 65 20 63 61 63 68 65 20 63 * If the cache c
18c2e 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65 20 77 ontains a page w
18c2f 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 ith page-number
18c30 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 74 0a pgno, remove it.
18c31 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 68 61 ** from its ha
18c32 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f 2c 20 sh chain. Also,
18c33 69 66 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 if the PGHDR_NEE
18c34 44 5f 53 59 4e 43 20 66 6c 61 67 20 77 61 73 20 D_SYNC flag was
18c35 73 65 74 20 66 6f 72 20 0a 20 20 2a 2a 20 70 61 set for . ** pa
18c36 67 65 20 70 67 6e 6f 20 62 65 66 6f 72 65 20 74 ge pgno before t
18c37 68 65 20 27 6d 6f 76 65 27 20 6f 70 65 72 61 74 he 'move' operat
18c38 69 6f 6e 2c 20 69 74 20 6e 65 65 64 73 20 74 6f ion, it needs to
18c39 20 62 65 20 72 65 74 61 69 6e 65 64 20 0a 20 20 be retained .
18c3a 2a 2a 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 ** for the page
18c3b 6d 6f 76 65 64 20 74 68 65 72 65 2e 0a 20 20 2a moved there.. *
18c3c 2f 0a 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 26 /. pPg->flags &
18c3d 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 = ~PGHDR_NEED_SY
18c3e 4e 43 3b 0a 20 20 70 50 67 4f 6c 64 20 3d 20 70 NC;. pPgOld = p
18c3f 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 ager_lookup(pPag
18c40 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 61 73 73 er, pgno);. ass
18c41 65 72 74 28 20 21 70 50 67 4f 6c 64 20 7c 7c 20 ert( !pPgOld ||
18c42 70 50 67 4f 6c 64 2d 3e 6e 52 65 66 3d 3d 31 20 pPgOld->nRef==1
18c43 29 3b 0a 20 20 69 66 28 20 70 50 67 4f 6c 64 20 );. if( pPgOld
18c44 29 7b 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 ){. pPg->flag
18c45 73 20 7c 3d 20 28 70 50 67 4f 6c 64 2d 3e 66 6c s |= (pPgOld->fl
18c46 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
18c47 59 4e 43 29 3b 0a 20 20 20 20 69 66 28 20 4d 45 YNC);. if( ME
18c48 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 MDB ){. /*
18c49 44 6f 20 6e 6f 74 20 64 69 73 63 61 72 64 20 70 Do not discard p
18c4a 61 67 65 73 20 66 72 6f 6d 20 61 6e 20 69 6e 2d ages from an in-
18c4b 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 memory database
18c4c 73 69 6e 63 65 20 77 65 20 6d 69 67 68 74 0a 20 since we might.
18c4d 20 20 20 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 ** need to
18c4e 72 6f 6c 6c 62 61 63 6b 20 6c 61 74 65 72 2e 20 rollback later.
18c4f 20 4a 75 73 74 20 6d 6f 76 65 20 74 68 65 20 70 Just move the p
18c50 61 67 65 20 6f 75 74 20 6f 66 20 74 68 65 20 77 age out of the w
18c51 61 79 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c ay. */. sql
18c52 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70 ite3PcacheMove(p
18c53 50 67 4f 6c 64 2c 20 70 50 61 67 65 72 2d 3e 64 PgOld, pPager->d
18c54 62 53 69 7a 65 2b 31 29 3b 0a 20 20 20 20 7d 65 bSize+1);. }e
18c55 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
18c56 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50 67 e3PcacheDrop(pPg
18c57 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Old);. }. }.
18c58 0a 20 20 6f 72 69 67 50 67 6e 6f 20 3d 20 70 50 . origPgno = pP
18c59 67 2d 3e 70 67 6e 6f 3b 0a 20 20 73 71 6c 69 74 g->pgno;. sqlit
18c5a 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67 e3PcacheMove(pPg
18c5b 2c 20 70 67 6e 6f 29 3b 0a 20 20 73 71 6c 69 74 , pgno);. sqlit
18c5c 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 e3PcacheMakeDirt
18c5d 79 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 46 6f y(pPg);.. /* Fo
18c5e 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 r an in-memory d
18c5f 61 74 61 62 61 73 65 2c 20 6d 61 6b 65 20 73 75 atabase, make su
18c60 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 re the original
18c61 70 61 67 65 20 63 6f 6e 74 69 6e 75 65 73 0a 20 page continues.
18c62 20 2a 2a 20 74 6f 20 65 78 69 73 74 2c 20 69 6e ** to exist, in
18c63 20 63 61 73 65 20 74 68 65 20 74 72 61 6e 73 61 case the transa
18c64 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 72 ction needs to r
18c65 6f 6c 6c 20 62 61 63 6b 2e 20 20 55 73 65 20 70 oll back. Use p
18c66 50 67 4f 6c 64 0a 20 20 2a 2a 20 61 73 20 74 68 PgOld. ** as th
18c67 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 e original page
18c68 73 69 6e 63 65 20 69 74 20 68 61 73 20 61 6c 72 since it has alr
18c69 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 eady been alloca
18c6a 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ted.. */. if(
18c6b 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 61 73 73 MEMDB ){. ass
18c6c 65 72 74 28 20 70 50 67 4f 6c 64 20 29 3b 0a 20 ert( pPgOld );.
18c6d 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
18c6e 4d 6f 76 65 28 70 50 67 4f 6c 64 2c 20 6f 72 69 Move(pPgOld, ori
18c6f 67 50 67 6e 6f 29 3b 0a 20 20 20 20 73 71 6c 69 gPgno);. sqli
18c70 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 te3PagerUnref(pP
18c71 67 4f 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 gOld);. }.. if
18c72 28 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 29 ( needSyncPgno )
18c73 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 65 64 {. /* If need
18c74 53 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d SyncPgno is non-
18c75 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6a zero, then the j
18c76 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 65 64 ournal file need
18c77 73 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 s to be . **
18c78 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 sync()ed before
18c79 61 6e 79 20 64 61 74 61 20 69 73 20 77 72 69 74 any data is writ
18c7a 74 65 6e 20 74 6f 20 64 61 74 61 62 61 73 65 20 ten to database
18c7b 66 69 6c 65 20 70 61 67 65 20 6e 65 65 64 53 79 file page needSy
18c7c 6e 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43 ncPgno.. ** C
18c7d 75 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 75 63 urrently, no suc
18c7e 68 20 70 61 67 65 20 65 78 69 73 74 73 20 69 6e h page exists in
18c7f 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 the page-cache
18c80 61 6e 64 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 and the . **
18c81 22 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22 20 62 "is journaled" b
18c82 69 74 76 65 63 20 66 6c 61 67 20 68 61 73 20 62 itvec flag has b
18c83 65 65 6e 20 73 65 74 2e 20 54 68 69 73 20 6e 65 een set. This ne
18c84 65 64 73 20 74 6f 20 62 65 20 72 65 6d 65 64 69 eds to be remedi
18c85 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c 6f 61 ed by. ** loa
18c86 64 69 6e 67 20 74 68 65 20 70 61 67 65 20 69 6e ding the page in
18c87 74 6f 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 to the pager-cac
18c88 68 65 20 61 6e 64 20 73 65 74 74 69 6e 67 20 74 he and setting t
18c89 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 he PGHDR_NEED_SY
18c8a 4e 43 0a 20 20 20 20 2a 2a 20 66 6c 61 67 2e 0a NC. ** flag..
18c8b 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 **. ** If
18c8c 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 the attempt to
18c8d 6c 6f 61 64 20 74 68 65 20 70 61 67 65 20 69 6e load the page in
18c8e 74 6f 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 to the page-cach
18c8f 65 20 66 61 69 6c 73 2c 20 28 64 75 65 0a 20 20 e fails, (due.
18c90 20 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 ** to a malloc
18c91 28 29 20 6f 72 20 49 4f 20 66 61 69 6c 75 72 65 () or IO failure
18c92 29 2c 20 63 6c 65 61 72 20 74 68 65 20 62 69 74 ), clear the bit
18c93 20 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75 72 6e in the pInJourn
18c94 61 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61 al[]. ** arra
18c95 79 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 y. Otherwise, if
18c96 20 74 68 65 20 70 61 67 65 20 69 73 20 6c 6f 61 the page is loa
18c97 64 65 64 20 61 6e 64 20 77 72 69 74 74 65 6e 20 ded and written
18c98 61 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a 2a 20 again in. **
18c99 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
18c9a 2c 20 69 74 20 6d 61 79 20 62 65 20 77 72 69 74 , it may be writ
18c9b 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 ten to the datab
18c9c 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 0a ase file before.
18c9d 20 20 20 20 2a 2a 20 69 74 20 69 73 20 73 79 6e ** it is syn
18c9e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 ced into the jou
18c9f 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 rnal file. This
18ca0 77 61 79 2c 20 69 74 20 6d 61 79 20 65 6e 64 20 way, it may end
18ca1 75 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 up in. ** the
18ca2 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 77 journal file tw
18ca3 69 63 65 2c 20 62 75 74 20 74 68 61 74 20 69 73 ice, but that is
18ca4 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2e 0a not a problem..
18ca5 20 20 20 20 2a 2f 0a 20 20 20 20 50 67 48 64 72 */. PgHdr
18ca6 20 2a 70 50 67 48 64 72 3b 0a 20 20 20 20 72 63 *pPgHdr;. rc
18ca7 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 = sqlite3PagerG
18ca8 65 74 28 70 50 61 67 65 72 2c 20 6e 65 65 64 53 et(pPager, needS
18ca9 79 6e 63 50 67 6e 6f 2c 20 26 70 50 67 48 64 72 yncPgno, &pPgHdr
18caa 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
18cab 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
18cac 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50 67 if( needSyncPg
18cad 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 no<=pPager->dbOr
18cae 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 igSize ){.
18caf 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
18cb0 2d 3e 70 54 6d 70 53 70 61 63 65 21 3d 30 20 29 ->pTmpSpace!=0 )
18cb1 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
18cb2 33 42 69 74 76 65 63 43 6c 65 61 72 28 70 50 61 3BitvecClear(pPa
18cb3 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c ger->pInJournal,
18cb4 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20 70 needSyncPgno, p
18cb5 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 Pager->pTmpSpace
18cb6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
18cb7 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
18cb8 7d 0a 20 20 20 20 70 50 67 48 64 72 2d 3e 66 6c }. pPgHdr->fl
18cb9 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 ags |= PGHDR_NEE
18cba 44 5f 53 59 4e 43 3b 0a 20 20 20 20 73 71 6c 69 D_SYNC;. sqli
18cbb 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 te3PcacheMakeDir
18cbc 74 79 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20 ty(pPgHdr);.
18cbd 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
18cbe 66 28 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 0a f(pPgHdr);. }..
18cbf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
18cc0 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a OK;.}.#endif../*
18cc1 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 .** Return a poi
18cc2 6e 74 65 72 20 74 6f 20 74 68 65 20 64 61 74 61 nter to the data
18cc3 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 for the specifi
18cc4 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 ed page..*/.SQLI
18cc5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
18cc6 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 *sqlite3PagerGet
18cc7 44 61 74 61 28 44 62 50 61 67 65 20 2a 70 50 67 Data(DbPage *pPg
18cc8 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 ){. assert( pPg
18cc9 2d 3e 6e 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d ->nRef>0 || pPg-
18cca 3e 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 >pPager->memDb )
18ccb 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e ;. return pPg->
18ccc 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pData;.}../*.**
18ccd 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
18cce 20 74 6f 20 74 68 65 20 50 61 67 65 72 2e 6e 45 to the Pager.nE
18ccf 78 74 72 61 20 62 79 74 65 73 20 6f 66 20 22 65 xtra bytes of "e
18cd0 78 74 72 61 22 20 73 70 61 63 65 20 0a 2a 2a 20 xtra" space .**
18cd1 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 allocated along
18cd2 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69 with the specifi
18cd3 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 ed page..*/.SQLI
18cd4 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
18cd5 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 *sqlite3PagerGet
18cd6 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 70 50 Extra(DbPage *pP
18cd7 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 g){. return pPg
18cd8 2d 3e 70 45 78 74 72 61 3b 0a 7d 0a 0a 2f 2a 0a ->pExtra;.}../*.
18cd9 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 6c ** Get/set the l
18cda 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 ocking-mode for
18cdb 74 68 69 73 20 70 61 67 65 72 2e 20 50 61 72 61 this pager. Para
18cdc 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 73 74 meter eMode must
18cdd 20 62 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41 be one.** of PA
18cde 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
18cdf 51 55 45 52 59 2c 20 50 41 47 45 52 5f 4c 4f 43 QUERY, PAGER_LOC
18ce0 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 KINGMODE_NORMAL
18ce1 6f 72 20 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 or .** PAGER_LOC
18ce2 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 KINGMODE_EXCLUSI
18ce3 56 45 2e 20 49 66 20 74 68 65 20 70 61 72 61 6d VE. If the param
18ce4 65 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 eter is not _QUE
18ce5 52 59 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 RY, then.** the
18ce6 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 69 73 20 locking-mode is
18ce7 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 set to the value
18ce8 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a specified..**.*
18ce9 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 * The returned v
18cea 61 6c 75 65 20 69 73 20 65 69 74 68 65 72 20 50 alue is either P
18ceb 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
18cec 5f 4e 4f 52 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41 _NORMAL or.** PA
18ced 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
18cee 45 58 43 4c 55 53 49 56 45 2c 20 69 6e 64 69 63 EXCLUSIVE, indic
18cef 61 74 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e ating the curren
18cf0 74 20 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 t (possibly upda
18cf1 74 65 64 29 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d ted).** locking-
18cf2 6d 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f mode..*/.SQLITE_
18cf3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
18cf4 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d te3PagerLockingM
18cf5 6f 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ode(Pager *pPage
18cf6 72 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 r, int eMode){.
18cf7 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d assert( eMode==
18cf8 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 PAGER_LOCKINGMOD
18cf9 45 5f 51 55 45 52 59 0a 20 20 20 20 20 20 20 20 E_QUERY.
18cfa 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 || eMode==PA
18cfb 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
18cfc 4e 4f 52 4d 41 4c 0a 20 20 20 20 20 20 20 20 20 NORMAL.
18cfd 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
18cfe 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 ER_LOCKINGMODE_E
18cff 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 73 XCLUSIVE );. as
18d00 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b sert( PAGER_LOCK
18d01 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 INGMODE_QUERY<0
18d02 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 );. assert( PAG
18d03 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e ER_LOCKINGMODE_N
18d04 4f 52 4d 41 4c 3e 3d 30 20 26 26 20 50 41 47 45 ORMAL>=0 && PAGE
18d05 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 R_LOCKINGMODE_EX
18d06 43 4c 55 53 49 56 45 3e 3d 30 20 29 3b 0a 20 20 CLUSIVE>=0 );.
18d07 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
18d08 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c exclusiveMode ||
18d09 20 30 3d 3d 73 71 6c 69 74 65 33 57 61 6c 48 65 0==sqlite3WalHe
18d0a 61 70 4d 65 6d 6f 72 79 28 70 50 61 67 65 72 2d apMemory(pPager-
18d0b 3e 70 57 61 6c 29 20 29 3b 0a 20 20 69 66 28 20 >pWal) );. if(
18d0c 65 4d 6f 64 65 3e 3d 30 20 26 26 20 21 70 50 61 eMode>=0 && !pPa
18d0d 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26 ger->tempFile &&
18d0e 20 21 73 71 6c 69 74 65 33 57 61 6c 48 65 61 70 !sqlite3WalHeap
18d0f 4d 65 6d 6f 72 79 28 70 50 61 67 65 72 2d 3e 70 Memory(pPager->p
18d10 57 61 6c 29 20 29 7b 0a 20 20 20 20 70 50 61 67 Wal) ){. pPag
18d11 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 er->exclusiveMod
18d12 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 e = (u8)eMode;.
18d13 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 }. return (int
18d14 29 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 )pPager->exclusi
18d15 76 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a veMode;.}../*.**
18d16 20 53 65 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c Set the journal
18d17 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 -mode for this p
18d18 61 67 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 ager. Parameter
18d19 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e eMode must be on
18d1a 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 50 e of:.**.** P
18d1b 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
18d1c 5f 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 50 41 _DELETE.** PA
18d1d 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
18d1e 54 52 55 4e 43 41 54 45 0a 2a 2a 20 20 20 20 50 TRUNCATE.** P
18d1f 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
18d20 5f 50 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 50 _PERSIST.** P
18d21 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
18d22 5f 4f 46 46 0a 2a 2a 20 20 20 20 50 41 47 45 52 _OFF.** PAGER
18d23 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
18d24 4f 52 59 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f ORY.** PAGER_
18d25 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 0a JOURNALMODE_WAL.
18d26 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 **.** The journa
18d27 6c 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 lmode is set to
18d28 74 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 the value specif
18d29 69 65 64 20 69 66 20 74 68 65 20 63 68 61 6e 67 ied if the chang
18d2a 65 20 69 73 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a e is allowed..**
18d2b 20 54 68 65 20 63 68 61 6e 67 65 20 6d 61 79 20 The change may
18d2c 62 65 20 64 69 73 61 6c 6c 6f 77 65 64 20 66 6f be disallowed fo
18d2d 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 r the following
18d2e 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 reasons:.**.**
18d2f 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 * An in-memory
18d30 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 6f 6e database can on
18d31 6c 79 20 68 61 76 65 20 69 74 73 20 6a 6f 75 72 ly have its jour
18d32 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 6f 20 nal_mode set to
18d33 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f 72 20 _OFF.** or
18d34 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20 20 _MEMORY..**.**
18d35 20 2a 20 20 54 65 6d 70 6f 72 61 72 79 20 64 61 * Temporary da
18d36 74 61 62 61 73 65 73 20 63 61 6e 6e 6f 74 20 68 tabases cannot h
18d37 61 76 65 20 5f 57 41 4c 20 6a 6f 75 72 6e 61 6c ave _WAL journal
18d38 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 mode..**.** The
18d39 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 61 74 returned indicat
18d3a 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 28 70 e the current (p
18d3b 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64 29 ossibly updated)
18d3c 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e 0a 2a journal-mode..*
18d3d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18d3e 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
18d3f 72 53 65 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 rSetJournalMode(
18d40 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
18d41 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 75 38 20 nt eMode){. u8
18d42 65 4f 6c 64 20 3d 20 70 50 61 67 65 72 2d 3e 6a eOld = pPager->j
18d43 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 2f ournalMode; /
18d44 2a 20 50 72 69 6f 72 20 6a 6f 75 72 6e 61 6c 6d * Prior journalm
18d45 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 ode */..#ifdef S
18d46 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a QLITE_DEBUG. /*
18d47 20 54 68 65 20 70 72 69 6e 74 5f 70 61 67 65 72 The print_pager
18d48 5f 73 74 61 74 65 28 29 20 72 6f 75 74 69 6e 65 _state() routine
18d49 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 is intended to
18d4a 62 65 20 75 73 65 64 20 62 79 20 74 68 65 20 64 be used by the d
18d4b 65 62 75 67 67 65 72 0a 20 20 2a 2a 20 6f 6e 6c ebugger. ** onl
18d4c 79 2e 20 20 57 65 20 69 6e 76 6f 6b 65 20 69 74 y. We invoke it
18d4d 20 6f 6e 63 65 20 68 65 72 65 20 74 6f 20 73 75 once here to su
18d4e 70 70 72 65 73 73 20 61 20 63 6f 6d 70 69 6c 65 ppress a compile
18d4f 72 20 77 61 72 6e 69 6e 67 2e 20 2a 2f 0a 20 20 r warning. */.
18d50 70 72 69 6e 74 5f 70 61 67 65 72 5f 73 74 61 74 print_pager_stat
18d51 65 28 70 50 61 67 65 72 29 3b 0a 23 65 6e 64 69 e(pPager);.#endi
18d52 66 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 65 4d 6f f... /* The eMo
18d53 64 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 de parameter is
18d54 61 6c 77 61 79 73 20 76 61 6c 69 64 20 2a 2f 0a always valid */.
18d55 20 20 61 73 73 65 72 74 28 20 20 20 20 20 20 65 assert( e
18d56 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
18d57 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 20 NALMODE_DELETE.
18d58 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d || eM
18d59 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
18d5a 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a ALMODE_TRUNCATE.
18d5b 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
18d5c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
18d5d 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a NALMODE_PERSIST.
18d5e 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
18d5f 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
18d60 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20 NALMODE_OFF .
18d61 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 || eMod
18d62 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
18d63 4d 4f 44 45 5f 57 41 4c 20 0a 20 20 20 20 20 20 MODE_WAL .
18d64 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d || eMode==
18d65 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
18d66 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 0a 20 20 2f E_MEMORY );.. /
18d67 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
18d68 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 s only called fr
18d69 6f 6d 20 74 68 65 20 4f 50 5f 4a 6f 75 72 6e 61 om the OP_Journa
18d6a 6c 4d 6f 64 65 20 6f 70 63 6f 64 65 2c 20 61 6e lMode opcode, an
18d6b 64 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 67 69 63 d. ** the logic
18d6c 20 74 68 65 72 65 20 77 69 6c 6c 20 6e 65 76 65 there will neve
18d6d 72 20 61 6c 6c 6f 77 20 61 20 74 65 6d 70 6f 72 r allow a tempor
18d6e 61 72 79 20 66 69 6c 65 20 74 6f 20 62 65 20 63 ary file to be c
18d6f 68 61 6e 67 65 64 0a 20 20 2a 2a 20 74 6f 20 57 hanged. ** to W
18d70 41 4c 20 6d 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 AL mode.. */.
18d71 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
18d72 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20 65 tempFile==0 || e
18d73 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode!=PAGER_JOUR
18d74 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 29 3b 0a 0a NALMODE_WAL );..
18d75 20 20 2f 2a 20 44 6f 20 61 6c 6c 6f 77 20 74 68 /* Do allow th
18d76 65 20 6a 6f 75 72 6e 61 6c 6d 6f 64 65 20 6f 66 e journalmode of
18d77 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
18d78 74 61 62 61 73 65 20 74 6f 20 62 65 20 73 65 74 tabase to be set
18d79 20 74 6f 0a 20 20 2a 2a 20 61 6e 79 74 68 69 6e to. ** anythin
18d7a 67 20 6f 74 68 65 72 20 74 68 61 6e 20 4d 45 4d g other than MEM
18d7b 4f 52 59 20 6f 72 20 4f 46 46 0a 20 20 2a 2f 0a ORY or OFF. */.
18d7c 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 if( MEMDB ){.
18d7d 20 20 20 61 73 73 65 72 74 28 20 65 4f 6c 64 3d assert( eOld=
18d7e 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
18d7f 44 45 5f 4d 45 4d 4f 52 59 20 7c 7c 20 65 4f 6c DE_MEMORY || eOl
18d80 64 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c d==PAGER_JOURNAL
18d81 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 20 20 MODE_OFF );.
18d82 69 66 28 20 65 4d 6f 64 65 21 3d 50 41 47 45 52 if( eMode!=PAGER
18d83 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
18d84 4f 52 59 20 26 26 20 65 4d 6f 64 65 21 3d 50 41 ORY && eMode!=PA
18d85 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
18d86 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 65 4d 6f OFF ){. eMo
18d87 64 65 20 3d 20 65 4f 6c 64 3b 0a 20 20 20 20 7d de = eOld;. }
18d88 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 4d 6f 64 . }.. if( eMod
18d89 65 21 3d 65 4f 6c 64 20 29 7b 0a 0a 20 20 20 20 e!=eOld ){..
18d8a 2f 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6a 6f /* Change the jo
18d8b 75 72 6e 61 6c 20 6d 6f 64 65 2e 20 2a 2f 0a 20 urnal mode. */.
18d8c 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
18d8d 72 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 r->eState!=PAGER
18d8e 5f 45 52 52 4f 52 20 29 3b 0a 20 20 20 20 70 50 _ERROR );. pP
18d8f 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
18d90 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 0a e = (u8)eMode;..
18d91 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 72 61 6e /* When tran
18d92 73 69 73 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 sistioning from
18d93 54 52 55 4e 43 41 54 45 20 6f 72 20 50 45 52 53 TRUNCATE or PERS
18d94 49 53 54 20 74 6f 20 61 6e 79 20 6f 74 68 65 72 IST to any other
18d95 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 journal. **
18d96 6d 6f 64 65 20 65 78 63 65 70 74 20 57 41 4c 2c mode except WAL,
18d97 20 75 6e 6c 65 73 73 20 74 68 65 20 70 61 67 65 unless the page
18d98 72 20 69 73 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f r is in locking_
18d99 6d 6f 64 65 3d 65 78 63 6c 75 73 69 76 65 20 6d mode=exclusive m
18d9a 6f 64 65 2c 0a 20 20 20 20 2a 2a 20 64 65 6c 65 ode,. ** dele
18d9b 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 te the journal f
18d9c 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ile.. */.
18d9d 61 73 73 65 72 74 28 20 28 50 41 47 45 52 5f 4a assert( (PAGER_J
18d9e 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 OURNALMODE_TRUNC
18d9f 41 54 45 20 26 20 35 29 3d 3d 31 20 29 3b 0a 20 ATE & 5)==1 );.
18da0 20 20 20 61 73 73 65 72 74 28 20 28 50 41 47 45 assert( (PAGE
18da1 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 R_JOURNALMODE_PE
18da2 52 53 49 53 54 20 26 20 35 29 3d 3d 31 20 29 3b RSIST & 5)==1 );
18da3 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 50 41 . assert( (PA
18da4 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
18da5 44 45 4c 45 54 45 20 26 20 35 29 3d 3d 30 20 29 DELETE & 5)==0 )
18da6 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 50 ;. assert( (P
18da7 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
18da8 5f 4d 45 4d 4f 52 59 20 26 20 35 29 3d 3d 34 20 _MEMORY & 5)==4
18da9 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
18daa 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
18dab 45 5f 4f 46 46 20 26 20 35 29 3d 3d 30 20 29 3b E_OFF & 5)==0 );
18dac 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 50 41 . assert( (PA
18dad 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
18dae 57 41 4c 20 26 20 35 29 3d 3d 35 20 29 3b 0a 0a WAL & 5)==5 );..
18daf 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 assert( isOp
18db0 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c en(pPager->fd) |
18db1 7c 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 | pPager->exclus
18db2 69 76 65 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69 iveMode );. i
18db3 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c f( !pPager->excl
18db4 75 73 69 76 65 4d 6f 64 65 20 26 26 20 28 65 4f usiveMode && (eO
18db5 6c 64 20 26 20 35 29 3d 3d 31 20 26 26 20 28 65 ld & 5)==1 && (e
18db6 4d 6f 64 65 20 26 20 31 29 3d 3d 30 20 29 7b 0a Mode & 1)==0 ){.
18db7 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 . /* In thi
18db8 73 20 63 61 73 65 20 77 65 20 77 6f 75 6c 64 20 s case we would
18db9 6c 69 6b 65 20 74 6f 20 64 65 6c 65 74 65 20 74 like to delete t
18dba 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
18dbb 20 49 66 20 69 74 20 69 73 0a 20 20 20 20 20 20 If it is.
18dbc 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2c ** not possible,
18dbd 20 74 68 65 6e 20 74 68 61 74 20 69 73 20 6e 6f then that is no
18dbe 74 20 61 20 70 72 6f 62 6c 65 6d 2e 20 44 65 6c t a problem. Del
18dbf 65 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 eting the journa
18dc0 6c 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 l file. **
18dc1 68 65 72 65 20 69 73 20 61 6e 20 6f 70 74 69 6d here is an optim
18dc2 69 7a 61 74 69 6f 6e 20 6f 6e 6c 79 2e 0a 20 20 ization only..
18dc3 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
18dc4 42 65 66 6f 72 65 20 64 65 6c 65 74 69 6e 67 20 Before deleting
18dc5 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
18dc6 2c 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52 , obtain a RESER
18dc7 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a VED lock on the.
18dc8 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 ** databas
18dc9 65 20 66 69 6c 65 2e 20 54 68 69 73 20 65 6e 73 e file. This ens
18dca 75 72 65 73 20 74 68 61 74 20 74 68 65 20 6a 6f ures that the jo
18dcb 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f urnal file is no
18dcc 74 20 64 65 6c 65 74 65 64 0a 20 20 20 20 20 20 t deleted.
18dcd 2a 2a 20 77 68 69 6c 65 20 69 74 20 69 73 20 69 ** while it is i
18dce 6e 20 75 73 65 20 62 79 20 73 6f 6d 65 20 6f 74 n use by some ot
18dcf 68 65 72 20 63 6c 69 65 6e 74 2e 0a 20 20 20 20 her client..
18dd0 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
18dd1 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 e3OsClose(pPager
18dd2 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 69 66 ->jfd);. if
18dd3 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3e ( pPager->eLock>
18dd4 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 =RESERVED_LOCK )
18dd5 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
18dd6 33 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65 72 3OsDelete(pPager
18dd7 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e ->pVfs, pPager->
18dd8 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 zJournal, 0);.
18dd9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
18dda 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 int rc = SQLI
18ddb 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 69 TE_OK;. i
18ddc 6e 74 20 73 74 61 74 65 20 3d 20 70 50 61 67 65 nt state = pPage
18ddd 72 2d 3e 65 53 74 61 74 65 3b 0a 20 20 20 20 20 r->eState;.
18dde 20 20 20 61 73 73 65 72 74 28 20 73 74 61 74 65 assert( state
18ddf 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 7c 7c 20 ==PAGER_OPEN ||
18de0 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 41 state==PAGER_REA
18de1 44 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 69 DER );. i
18de2 66 28 20 73 74 61 74 65 3d 3d 50 41 47 45 52 5f f( state==PAGER_
18de3 4f 50 45 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 OPEN ){.
18de4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
18de5 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 70 50 gerSharedLock(pP
18de6 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d ager);. }
18de7 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 . if( pPa
18de8 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 ger->eState==PAG
18de9 45 52 5f 52 45 41 44 45 52 20 29 7b 0a 20 20 20 ER_READER ){.
18dea 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 assert( r
18deb 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c==SQLITE_OK );.
18dec 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 rc = p
18ded 61 67 65 72 4c 6f 63 6b 44 62 28 70 50 61 67 65 agerLockDb(pPage
18dee 72 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b r, RESERVED_LOCK
18def 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
18df0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
18df1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
18df2 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c sqlite3OsDel
18df3 65 74 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73 ete(pPager->pVfs
18df4 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e , pPager->zJourn
18df5 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 al, 0);.
18df6 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
18df7 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 ==SQLITE_OK && s
18df8 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 tate==PAGER_READ
18df9 45 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ER ){.
18dfa 70 61 67 65 72 55 6e 6c 6f 63 6b 44 62 28 70 50 pagerUnlockDb(pP
18dfb 61 67 65 72 2c 20 53 48 41 52 45 44 5f 4c 4f 43 ager, SHARED_LOC
18dfc 4b 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 K);. }els
18dfd 65 20 69 66 28 20 73 74 61 74 65 3d 3d 50 41 47 e if( state==PAG
18dfe 45 52 5f 4f 50 45 4e 20 29 7b 0a 20 20 20 20 20 ER_OPEN ){.
18dff 20 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 pager_unloc
18e00 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 k(pPager);.
18e01 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 }. ass
18e02 65 72 74 28 20 73 74 61 74 65 3d 3d 70 50 61 67 ert( state==pPag
18e03 65 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 er->eState );.
18e04 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
18e05 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 . /* Return the
18e06 20 6e 65 77 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 new journal mod
18e07 65 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 69 e */. return (i
18e08 6e 74 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e nt)pPager->journ
18e09 61 6c 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a alMode;.}../*.**
18e0a 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 Return the curr
18e0b 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 ent journal mode
18e0c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18e0d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
18e0e 61 67 65 72 47 65 74 4a 6f 75 72 6e 61 6c 4d 6f agerGetJournalMo
18e0f 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 de(Pager *pPager
18e10 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 ){. return (int
18e11 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c )pPager->journal
18e12 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 Mode;.}../*.** R
18e13 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 eturn TRUE if th
18e14 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 61 20 e pager is in a
18e15 73 74 61 74 65 20 77 68 65 72 65 20 69 74 20 69 state where it i
18e16 73 20 4f 4b 20 74 6f 20 63 68 61 6e 67 65 20 74 s OK to change t
18e17 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 6d 6f 64 he.** journalmod
18e18 65 2e 20 20 4a 6f 75 72 6e 61 6c 6d 6f 64 65 20 e. Journalmode
18e19 63 68 61 6e 67 65 73 20 63 61 6e 20 6f 6e 6c 79 changes can only
18e1a 20 68 61 70 70 65 6e 20 77 68 65 6e 20 74 68 65 happen when the
18e1b 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 database.** is
18e1c 75 6e 6d 6f 64 69 66 69 65 64 2e 0a 2a 2f 0a 53 unmodified..*/.S
18e1d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
18e1e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 6b t sqlite3PagerOk
18e1f 54 6f 43 68 61 6e 67 65 4a 6f 75 72 6e 61 6c 4d ToChangeJournalM
18e20 6f 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ode(Pager *pPage
18e21 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 61 73 r){. assert( as
18e22 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 sert_pager_state
18e23 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 69 66 (pPager) );. if
18e24 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 ( pPager->eState
18e25 3e 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 43 >=PAGER_WRITER_C
18e26 41 43 48 45 4d 4f 44 20 29 20 72 65 74 75 72 6e ACHEMOD ) return
18e27 20 30 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 0;. if( NEVER(
18e28 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
18e29 66 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a fd) && pPager->j
18e2a 6f 75 72 6e 61 6c 4f 66 66 3e 30 29 20 29 20 72 ournalOff>0) ) r
18e2b 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 eturn 0;. retur
18e2c 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 n 1;.}../*.** Ge
18e2d 74 2f 73 65 74 20 74 68 65 20 73 69 7a 65 2d 6c t/set the size-l
18e2e 69 6d 69 74 20 75 73 65 64 20 66 6f 72 20 70 65 imit used for pe
18e2f 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c rsistent journal
18e30 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 files..**.** Se
18e31 74 74 69 6e 67 20 74 68 65 20 73 69 7a 65 20 6c tting the size l
18e32 69 6d 69 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73 imit to -1 means
18e33 20 6e 6f 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 no limit is enf
18e34 6f 72 63 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 orced..** An att
18e35 65 6d 70 74 20 74 6f 20 73 65 74 20 61 20 6c 69 empt to set a li
18e36 6d 69 74 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e mit smaller than
18e37 20 2d 31 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a -1 is a no-op..
18e38 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18e39 45 20 69 36 34 20 73 71 6c 69 74 65 33 50 61 67 E i64 sqlite3Pag
18e3a 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d erJournalSizeLim
18e3b 69 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 it(Pager *pPager
18e3c 2c 20 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 , i64 iLimit){.
18e3d 20 69 66 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 if( iLimit>=-1
18e3e 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a ){. pPager->j
18e3f 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 ournalSizeLimit
18e40 3d 20 69 4c 69 6d 69 74 3b 0a 20 20 20 20 73 71 = iLimit;. sq
18e41 6c 69 74 65 33 57 61 6c 4c 69 6d 69 74 28 70 50 lite3WalLimit(pP
18e42 61 67 65 72 2d 3e 70 57 61 6c 2c 20 69 4c 69 6d ager->pWal, iLim
18e43 69 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 it);. }. retur
18e44 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 n pPager->journa
18e45 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a 0a 2f lSizeLimit;.}../
18e46 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f *.** Return a po
18e47 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 50 61 inter to the pPa
18e48 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76 61 72 ger->pBackup var
18e49 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63 6b 75 iable. The backu
18e4a 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e 20 62 p module.** in b
18e4b 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61 69 6e ackup.c maintain
18e4c 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 s the content of
18e4d 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 2e 20 this variable.
18e4e 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a 20 75 This module.** u
18e4f 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c 79 20 ses it opaquely
18e50 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 as an argument t
18e51 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 o sqlite3BackupR
18e52 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a 2a 20 estart() and.**
18e53 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 sqlite3BackupUpd
18e54 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 ate() only..*/.S
18e55 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
18e56 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 lite3_backup **s
18e57 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 qlite3PagerBacku
18e58 70 50 74 72 28 50 61 67 65 72 20 2a 70 50 61 67 pPtr(Pager *pPag
18e59 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 26 70 er){. return &p
18e5a 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 3b 0a Pager->pBackup;.
18e5b 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
18e5c 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 2f 2a E_OMIT_VACUUM./*
18e5d 0a 2a 2a 20 55 6e 6c 65 73 73 20 74 68 69 73 20 .** Unless this
18e5e 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 is an in-memory
18e5f 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 or temporary dat
18e60 61 62 61 73 65 2c 20 63 6c 65 61 72 20 74 68 65 abase, clear the
18e61 20 70 61 67 65 72 20 63 61 63 68 65 2e 0a 2a 2f pager cache..*/
18e62 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18e63 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
18e64 72 43 6c 65 61 72 43 61 63 68 65 28 50 61 67 65 rClearCache(Page
18e65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 r *pPager){. if
18e66 28 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67 ( !MEMDB && pPag
18e67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 er->tempFile==0
18e68 29 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 ) pager_reset(pP
18e69 61 67 65 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a ager);.}.#endif.
18e6a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
18e6b 4f 4d 49 54 5f 57 41 4c 0a 2f 2a 0a 2a 2a 20 54 OMIT_WAL./*.** T
18e6c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
18e6d 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 called when the
18e6e 75 73 65 72 20 69 6e 76 6f 6b 65 73 20 22 50 52 user invokes "PR
18e6f 41 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70 6f AGMA wal_checkpo
18e70 69 6e 74 22 2c 0a 2a 2a 20 22 50 52 41 47 4d 41 int",.** "PRAGMA
18e71 20 77 61 6c 5f 62 6c 6f 63 6b 69 6e 67 5f 63 68 wal_blocking_ch
18e72 65 63 6b 70 6f 69 6e 74 22 20 6f 72 20 63 61 6c eckpoint" or cal
18e73 6c 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 77 ls the sqlite3_w
18e74 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 28 29 0a al_checkpoint().
18e75 2a 2a 20 6f 72 20 77 61 6c 5f 62 6c 6f 63 6b 69 ** or wal_blocki
18e76 6e 67 5f 63 68 65 63 6b 70 6f 69 6e 74 28 29 20 ng_checkpoint()
18e77 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a API functions..*
18e78 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 65 *.** Parameter e
18e79 4d 6f 64 65 20 69 73 20 6f 6e 65 20 6f 66 20 53 Mode is one of S
18e7a 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 QLITE_CHECKPOINT
18e7b 5f 50 41 53 53 49 56 45 2c 20 46 55 4c 4c 20 6f _PASSIVE, FULL o
18e7c 72 20 52 45 53 54 41 52 54 2e 0a 2a 2f 0a 53 51 r RESTART..*/.SQ
18e7d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
18e7e 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 68 65 sqlite3PagerChe
18e7f 63 6b 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 ckpoint(Pager *p
18e80 50 61 67 65 72 2c 20 69 6e 74 20 65 4d 6f 64 65 Pager, int eMode
18e81 2c 20 69 6e 74 20 2a 70 6e 4c 6f 67 2c 20 69 6e , int *pnLog, in
18e82 74 20 2a 70 6e 43 6b 70 74 29 7b 0a 20 20 69 6e t *pnCkpt){. in
18e83 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
18e84 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
18e85 70 57 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d pWal ){. rc =
18e86 20 73 71 6c 69 74 65 33 57 61 6c 43 68 65 63 6b sqlite3WalCheck
18e87 70 6f 69 6e 74 28 70 50 61 67 65 72 2d 3e 70 57 point(pPager->pW
18e88 61 6c 2c 20 65 4d 6f 64 65 2c 0a 20 20 20 20 20 al, eMode,.
18e89 20 20 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79 pPager->xBusy
18e8a 48 61 6e 64 6c 65 72 2c 20 70 50 61 67 65 72 2d Handler, pPager-
18e8b 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 >pBusyHandlerArg
18e8c 2c 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 ,. pPager
18e8d 2d 3e 63 6b 70 74 53 79 6e 63 46 6c 61 67 73 2c ->ckptSyncFlags,
18e8e 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
18e8f 65 2c 20 28 75 38 20 2a 29 70 50 61 67 65 72 2d e, (u8 *)pPager-
18e90 3e 70 54 6d 70 53 70 61 63 65 2c 0a 20 20 20 20 >pTmpSpace,.
18e91 20 20 20 20 70 6e 4c 6f 67 2c 20 70 6e 43 6b 70 pnLog, pnCkp
18e92 74 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 20 20 72 t. );. }. r
18e93 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 53 51 4c eturn rc;.}..SQL
18e94 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
18e95 73 71 6c 69 74 65 33 50 61 67 65 72 57 61 6c 43 sqlite3PagerWalC
18e96 61 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 allback(Pager *p
18e97 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
18e98 20 73 71 6c 69 74 65 33 57 61 6c 43 61 6c 6c 62 sqlite3WalCallb
18e99 61 63 6b 28 70 50 61 67 65 72 2d 3e 70 57 61 6c ack(pPager->pWal
18e9a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
18e9b 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 75 rn true if the u
18e9c 6e 64 65 72 6c 79 69 6e 67 20 56 46 53 20 66 6f nderlying VFS fo
18e9d 72 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 r the given page
18e9e 72 20 73 75 70 70 6f 72 74 73 20 74 68 65 0a 2a r supports the.*
18e9f 2a 20 70 72 69 6d 69 74 69 76 65 73 20 6e 65 63 * primitives nec
18ea0 65 73 73 61 72 79 20 66 6f 72 20 77 72 69 74 65 essary for write
18ea1 2d 61 68 65 61 64 20 6c 6f 67 67 69 6e 67 2e 0a -ahead logging..
18ea2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18ea3 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
18ea4 65 72 57 61 6c 53 75 70 70 6f 72 74 65 64 28 50 erWalSupported(P
18ea5 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
18ea6 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
18ea7 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 o_methods *pMeth
18ea8 6f 64 73 20 3d 20 70 50 61 67 65 72 2d 3e 66 64 ods = pPager->fd
18ea9 2d 3e 70 4d 65 74 68 6f 64 73 3b 0a 20 20 72 65 ->pMethods;. re
18eaa 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 78 63 turn pPager->exc
18eab 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 28 70 lusiveMode || (p
18eac 4d 65 74 68 6f 64 73 2d 3e 69 56 65 72 73 69 6f Methods->iVersio
18ead 6e 3e 3d 32 20 26 26 20 70 4d 65 74 68 6f 64 73 n>=2 && pMethods
18eae 2d 3e 78 53 68 6d 4d 61 70 29 3b 0a 7d 0a 0a 2f ->xShmMap);.}../
18eaf 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 *.** Attempt to
18eb0 74 61 6b 65 20 61 6e 20 65 78 63 6c 75 73 69 76 take an exclusiv
18eb1 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 e lock on the da
18eb2 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 tabase file. If
18eb3 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 0a 2a a PENDING lock.*
18eb4 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69 6e * is obtained in
18eb5 73 74 65 61 64 2c 20 69 6d 6d 65 64 69 61 74 65 stead, immediate
18eb6 6c 79 20 72 65 6c 65 61 73 65 20 69 74 2e 0a 2a ly release it..*
18eb7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
18eb8 65 72 45 78 63 6c 75 73 69 76 65 4c 6f 63 6b 28 erExclusiveLock(
18eb9 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
18eba 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
18ebb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18ebc 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
18ebd 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
18ebe 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 53 48 Pager->eLock==SH
18ebf 41 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 50 61 ARED_LOCK || pPa
18ec0 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c ger->eLock==EXCL
18ec1 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 USIVE_LOCK );.
18ec2 72 63 20 3d 20 70 61 67 65 72 4c 6f 63 6b 44 62 rc = pagerLockDb
18ec3 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 (pPager, EXCLUSI
18ec4 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 VE_LOCK);. if(
18ec5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
18ec6 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 61 . /* If the a
18ec7 74 74 65 6d 70 74 20 74 6f 20 67 72 61 62 20 74 ttempt to grab t
18ec8 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 he exclusive loc
18ec9 6b 20 66 61 69 6c 65 64 2c 20 72 65 6c 65 61 73 k failed, releas
18eca 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 70 65 e the . ** pe
18ecb 6e 64 69 6e 67 20 6c 6f 63 6b 20 74 68 61 74 20 nding lock that
18ecc 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 6f 62 may have been ob
18ecd 74 61 69 6e 65 64 20 69 6e 73 74 65 61 64 2e 20 tained instead.
18ece 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 55 6e 6c */. pagerUnl
18ecf 6f 63 6b 44 62 28 70 50 61 67 65 72 2c 20 53 48 ockDb(pPager, SH
18ed0 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 7d 0a ARED_LOCK);. }.
18ed1 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
18ed2 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 ./*.** Call sqli
18ed3 74 65 33 57 61 6c 4f 70 65 6e 28 29 20 74 6f 20 te3WalOpen() to
18ed4 6f 70 65 6e 20 74 68 65 20 57 41 4c 20 68 61 6e open the WAL han
18ed5 64 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67 65 dle. If the page
18ed6 72 20 69 73 20 69 6e 20 0a 2a 2a 20 65 78 63 6c r is in .** excl
18ed7 75 73 69 76 65 2d 6c 6f 63 6b 69 6e 67 20 6d 6f usive-locking mo
18ed8 64 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e de when this fun
18ed9 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c ction is called,
18eda 20 74 61 6b 65 20 61 6e 20 45 58 43 4c 55 53 49 take an EXCLUSI
18edb 56 45 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 68 VE.** lock on th
18edc 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
18edd 61 6e 64 20 75 73 65 20 68 65 61 70 2d 6d 65 6d and use heap-mem
18ede 6f 72 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 ory to store the
18edf 20 77 61 6c 2d 69 6e 64 65 78 0a 2a 2a 20 69 6e wal-index.** in
18ee0 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 75 73 65 . Otherwise, use
18ee1 20 74 68 65 20 6e 6f 72 6d 61 6c 20 73 68 61 72 the normal shar
18ee2 65 64 2d 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 ed-memory..*/.st
18ee3 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 4f 70 atic int pagerOp
18ee4 65 6e 57 61 6c 28 50 61 67 65 72 20 2a 70 50 61 enWal(Pager *pPa
18ee5 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ger){. int rc =
18ee6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 SQLITE_OK;.. a
18ee7 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 ssert( pPager->p
18ee8 57 61 6c 3d 3d 30 20 26 26 20 70 50 61 67 65 72 Wal==0 && pPager
18ee9 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 29 3b ->tempFile==0 );
18eea 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
18eeb 72 2d 3e 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 r->eLock==SHARED
18eec 5f 4c 4f 43 4b 20 7c 7c 20 70 50 61 67 65 72 2d _LOCK || pPager-
18eed 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 >eLock==EXCLUSIV
18eee 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 E_LOCK );.. /*
18eef 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
18ef0 61 6c 72 65 61 64 79 20 69 6e 20 65 78 63 6c 75 already in exclu
18ef1 73 69 76 65 2d 6d 6f 64 65 2c 20 74 68 65 20 57 sive-mode, the W
18ef2 41 4c 20 6d 6f 64 75 6c 65 20 77 69 6c 6c 20 75 AL module will u
18ef3 73 65 20 0a 20 20 2a 2a 20 68 65 61 70 2d 6d 65 se . ** heap-me
18ef4 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 77 61 6c mory for the wal
18ef5 2d 69 6e 64 65 78 20 69 6e 73 74 65 61 64 20 6f -index instead o
18ef6 66 20 74 68 65 20 56 46 53 20 73 68 61 72 65 64 f the VFS shared
18ef7 2d 6d 65 6d 6f 72 79 20 0a 20 20 2a 2a 20 69 6d -memory . ** im
18ef8 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 61 plementation. Ta
18ef9 6b 65 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 ke the exclusive
18efa 20 6c 6f 63 6b 20 6e 6f 77 2c 20 62 65 66 6f 72 lock now, befor
18efb 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 57 41 e opening the WA
18efc 4c 0a 20 20 2a 2a 20 66 69 6c 65 2c 20 74 6f 20 L. ** file, to
18efd 6d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 69 make sure this i
18efe 73 20 73 61 66 65 2e 0a 20 20 2a 2f 0a 20 20 69 s safe.. */. i
18eff 66 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 f( pPager->exclu
18f00 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 siveMode ){.
18f01 72 63 20 3d 20 70 61 67 65 72 45 78 63 6c 75 73 rc = pagerExclus
18f02 69 76 65 4c 6f 63 6b 28 70 50 61 67 65 72 29 3b iveLock(pPager);
18f03 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 . }.. /* Open
18f04 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 the connection t
18f05 6f 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e 20 o the log file.
18f06 49 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f If this operatio
18f07 6e 20 66 61 69 6c 73 2c 20 0a 20 20 2a 2a 20 28 n fails, . ** (
18f08 65 2e 67 2e 20 64 75 65 20 74 6f 20 6d 61 6c 6c e.g. due to mall
18f09 6f 63 28 29 20 66 61 69 6c 75 72 65 29 2c 20 72 oc() failure), r
18f0a 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
18f0b 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ode.. */. if(
18f0c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
18f0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
18f0e 33 57 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3WalOpen(pPager-
18f0f 3e 70 56 66 73 2c 20 0a 20 20 20 20 20 20 20 20 >pVfs, .
18f10 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 pPager->fd, pPag
18f11 65 72 2d 3e 7a 57 61 6c 2c 20 70 50 61 67 65 72 er->zWal, pPager
18f12 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 2c ->exclusiveMode,
18f13 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
18f14 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 >journalSizeLimi
18f15 74 2c 20 26 70 50 61 67 65 72 2d 3e 70 57 61 6c t, &pPager->pWal
18f16 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 . );. }.. r
18f17 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
18f18 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d .** The caller m
18f19 75 73 74 20 62 65 20 68 6f 6c 64 69 6e 67 20 61 ust be holding a
18f1a 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 SHARED lock on
18f1b 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
18f1c 65 20 74 6f 20 63 61 6c 6c 0a 2a 2a 20 74 68 69 e to call.** thi
18f1d 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a s function..**.*
18f1e 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 70 * If the pager p
18f1f 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 assed as the fir
18f20 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f st argument is o
18f21 70 65 6e 20 6f 6e 20 61 20 72 65 61 6c 20 64 61 pen on a real da
18f22 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 28 tabase.** file (
18f23 6e 6f 74 20 61 20 74 65 6d 70 20 66 69 6c 65 20 not a temp file
18f24 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 or an in-memory
18f25 64 61 74 61 62 61 73 65 29 2c 20 61 6e 64 20 74 database), and t
18f26 68 65 20 57 41 4c 20 66 69 6c 65 0a 2a 2a 20 69 he WAL file.** i
18f27 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 s not already op
18f28 65 6e 2c 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 en, make an atte
18f29 6d 70 74 20 74 6f 20 6f 70 65 6e 20 69 74 20 6e mpt to open it n
18f2a 6f 77 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 ow. If successfu
18f2b 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c l,.** return SQL
18f2c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 65 72 ITE_OK. If an er
18f2d 72 6f 72 20 6f 63 63 75 72 73 20 6f 72 20 74 68 ror occurs or th
18f2e 65 20 56 46 53 20 75 73 65 64 20 62 79 20 74 68 e VFS used by th
18f2f 65 20 70 61 67 65 72 20 64 6f 65 73 20 0a 2a 2a e pager does .**
18f30 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74 68 65 not support the
18f31 20 78 53 68 6d 58 58 58 28 29 20 6d 65 74 68 6f xShmXXX() metho
18f32 64 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 ds, return an er
18f33 72 6f 72 20 63 6f 64 65 2e 20 2a 70 62 4f 70 65 ror code. *pbOpe
18f34 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 6d 6f 64 69 n is.** not modi
18f35 66 69 65 64 20 69 6e 20 65 69 74 68 65 72 20 63 fied in either c
18f36 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ase..**.** If th
18f37 65 20 70 61 67 65 72 20 69 73 20 6f 70 65 6e 20 e pager is open
18f38 6f 6e 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 28 on a temp-file (
18f39 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 or in-memory dat
18f3a 61 62 61 73 65 29 2c 20 6f 72 20 69 66 0a 2a 2a abase), or if.**
18f3b 20 74 68 65 20 57 41 4c 20 66 69 6c 65 20 69 73 the WAL file is
18f3c 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2c 20 73 already open, s
18f3d 65 74 20 2a 70 62 4f 70 65 6e 20 74 6f 20 31 20 et *pbOpen to 1
18f3e 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 and return SQLIT
18f3f 45 5f 4f 4b 0a 2a 2a 20 77 69 74 68 6f 75 74 20 E_OK.** without
18f40 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 0a doing anything..
18f41 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18f42 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
18f43 65 72 4f 70 65 6e 57 61 6c 28 0a 20 20 50 61 67 erOpenWal(. Pag
18f44 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 er *pPager,
18f45 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18f46 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a Pager object */.
18f47 20 20 69 6e 74 20 2a 70 62 4f 70 65 6e 20 20 20 int *pbOpen
18f48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18f49 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f /* OUT: Set to
18f4a 20 74 72 75 65 20 69 66 20 63 61 6c 6c 20 69 73 true if call is
18f4b 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 29 7b 0a 20 a no-op */.){.
18f4c 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
18f4d 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
18f4e 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
18f4f 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 61 73 */.. assert( as
18f50 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 sert_pager_state
18f51 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 61 73 (pPager) );. as
18f52 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 sert( pPager->eS
18f53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e tate==PAGER_OPEN
18f54 20 20 20 7c 7c 20 70 62 4f 70 65 6e 20 29 3b 0a || pbOpen );.
18f55 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
18f56 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f ->eState==PAGER_
18f57 52 45 41 44 45 52 20 7c 7c 20 21 70 62 4f 70 65 READER || !pbOpe
18f58 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 n );. assert( p
18f59 62 4f 70 65 6e 3d 3d 30 20 7c 7c 20 2a 70 62 4f bOpen==0 || *pbO
18f5a 70 65 6e 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 pen==0 );. asse
18f5b 72 74 28 20 70 62 4f 70 65 6e 21 3d 30 20 7c 7c rt( pbOpen!=0 ||
18f5c 20 28 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 (!pPager->tempF
18f5d 69 6c 65 20 26 26 20 21 70 50 61 67 65 72 2d 3e ile && !pPager->
18f5e 70 57 61 6c 29 20 29 3b 0a 0a 20 20 69 66 28 20 pWal) );.. if(
18f5f 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c !pPager->tempFil
18f60 65 20 26 26 20 21 70 50 61 67 65 72 2d 3e 70 57 e && !pPager->pW
18f61 61 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 21 73 al ){. if( !s
18f62 71 6c 69 74 65 33 50 61 67 65 72 57 61 6c 53 75 qlite3PagerWalSu
18f63 70 70 6f 72 74 65 64 28 70 50 61 67 65 72 29 20 pported(pPager)
18f64 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
18f65 43 41 4e 54 4f 50 45 4e 3b 0a 0a 20 20 20 20 2f CANTOPEN;.. /
18f66 2a 20 43 6c 6f 73 65 20 61 6e 79 20 72 6f 6c 6c * Close any roll
18f67 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 70 72 65 back journal pre
18f68 76 69 6f 75 73 6c 79 20 6f 70 65 6e 20 2a 2f 0a viously open */.
18f69 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
18f6a 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b se(pPager->jfd);
18f6b 0a 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 .. rc = pager
18f6c 4f 70 65 6e 57 61 6c 28 70 50 61 67 65 72 29 3b OpenWal(pPager);
18f6d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
18f6e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
18f6f 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
18f70 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 ode = PAGER_JOUR
18f71 4e 41 4c 4d 4f 44 45 5f 57 41 4c 3b 0a 20 20 20 NALMODE_WAL;.
18f72 20 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 pPager->eStat
18f73 65 20 3d 20 50 41 47 45 52 5f 4f 50 45 4e 3b 0a e = PAGER_OPEN;.
18f74 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
18f75 20 20 20 2a 70 62 4f 70 65 6e 20 3d 20 31 3b 0a *pbOpen = 1;.
18f76 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
18f77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
18f78 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
18f79 65 64 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 ed to close the
18f7a 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 connection to th
18f7b 65 20 6c 6f 67 20 66 69 6c 65 20 70 72 69 6f 72 e log file prior
18f7c 0a 2a 2a 20 74 6f 20 73 77 69 74 63 68 69 6e 67 .** to switching
18f7d 20 66 72 6f 6d 20 57 41 4c 20 74 6f 20 72 6f 6c from WAL to rol
18f7e 6c 62 61 63 6b 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a lback mode..**.*
18f7f 2a 20 42 65 66 6f 72 65 20 63 6c 6f 73 69 6e 67 * Before closing
18f80 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 2c 20 74 the log file, t
18f81 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 his function att
18f82 65 6d 70 74 73 20 74 6f 20 74 61 6b 65 20 61 6e empts to take an
18f83 20 0a 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c .** EXCLUSIVE l
18f84 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
18f85 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 69 ase file. If thi
18f86 73 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 s cannot be obta
18f87 69 6e 65 64 2c 20 61 6e 0a 2a 2a 20 65 72 72 6f ined, an.** erro
18f88 72 20 28 53 51 4c 49 54 45 5f 42 55 53 59 29 20 r (SQLITE_BUSY)
18f89 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
18f8a 74 68 65 20 6c 6f 67 20 63 6f 6e 6e 65 63 74 69 the log connecti
18f8b 6f 6e 20 69 73 20 6e 6f 74 20 63 6c 6f 73 65 64 on is not closed
18f8c 2e 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 ..** If successf
18f8d 75 6c 2c 20 74 68 65 20 45 58 43 4c 55 53 49 56 ul, the EXCLUSIV
18f8e 45 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65 E lock is not re
18f8f 6c 65 61 73 65 64 20 62 65 66 6f 72 65 20 72 65 leased before re
18f90 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 turning..*/.SQLI
18f91 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
18f92 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 qlite3PagerClose
18f93 57 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 Wal(Pager *pPage
18f94 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 r){. int rc = S
18f95 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 QLITE_OK;.. ass
18f96 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ert( pPager->jou
18f97 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
18f98 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 JOURNALMODE_WAL
18f99 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
18f9a 6c 6f 67 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 log file is not
18f9b 61 6c 72 65 61 64 79 20 6f 70 65 6e 2c 20 62 75 already open, bu
18f9c 74 20 64 6f 65 73 20 65 78 69 73 74 20 69 6e 20 t does exist in
18f9d 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c the file-system,
18f9e 0a 20 20 2a 2a 20 69 74 20 6d 61 79 20 6e 65 65 . ** it may nee
18f9f 64 20 74 6f 20 62 65 20 63 68 65 63 6b 70 6f 69 d to be checkpoi
18fa0 6e 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 nted before the
18fa1 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 20 73 connection can s
18fa2 77 69 74 63 68 20 74 6f 0a 20 20 2a 2a 20 72 6f witch to. ** ro
18fa3 6c 6c 62 61 63 6b 20 6d 6f 64 65 2e 20 4f 70 65 llback mode. Ope
18fa4 6e 20 69 74 20 6e 6f 77 20 73 6f 20 74 68 69 73 n it now so this
18fa5 20 63 61 6e 20 68 61 70 70 65 6e 2e 0a 20 20 2a can happen.. *
18fa6 2f 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d /. if( !pPager-
18fa7 3e 70 57 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 >pWal ){. int
18fa8 20 6c 6f 67 65 78 69 73 74 73 20 3d 20 30 3b 0a logexists = 0;.
18fa9 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 4c 6f rc = pagerLo
18faa 63 6b 44 62 28 70 50 61 67 65 72 2c 20 53 48 41 ckDb(pPager, SHA
18fab 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 RED_LOCK);. i
18fac 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
18fad 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
18fae 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 0a qlite3OsAccess(.
18faf 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
18fb0 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e ->pVfs, pPager->
18fb1 7a 57 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 zWal, SQLITE_ACC
18fb2 45 53 53 5f 45 58 49 53 54 53 2c 20 26 6c 6f 67 ESS_EXISTS, &log
18fb3 65 78 69 73 74 73 0a 20 20 20 20 20 20 29 3b 0a exists. );.
18fb4 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
18fb5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c ==SQLITE_OK && l
18fb6 6f 67 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 ogexists ){.
18fb7 20 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65 6e rc = pagerOpen
18fb8 57 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 Wal(pPager);.
18fb9 20 7d 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a }. }. . /*
18fba 20 43 68 65 63 6b 70 6f 69 6e 74 20 61 6e 64 20 Checkpoint and
18fbb 63 6c 6f 73 65 20 74 68 65 20 6c 6f 67 2e 20 42 close the log. B
18fbc 65 63 61 75 73 65 20 61 6e 20 45 58 43 4c 55 53 ecause an EXCLUS
18fbd 49 56 45 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 IVE lock is held
18fbe 20 6f 6e 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 on. ** the dat
18fbf 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20 abase file, the
18fc0 6c 6f 67 20 61 6e 64 20 6c 6f 67 2d 73 75 6d 6d log and log-summ
18fc1 61 72 79 20 66 69 6c 65 73 20 77 69 6c 6c 20 62 ary files will b
18fc2 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a e deleted.. */.
18fc3 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
18fc4 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 70 _OK && pPager->p
18fc5 57 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 Wal ){. rc =
18fc6 70 61 67 65 72 45 78 63 6c 75 73 69 76 65 4c 6f pagerExclusiveLo
18fc7 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 ck(pPager);.
18fc8 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
18fc9 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
18fca 73 71 6c 69 74 65 33 57 61 6c 43 6c 6f 73 65 28 sqlite3WalClose(
18fcb 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 70 50 pPager->pWal, pP
18fcc 61 67 65 72 2d 3e 63 6b 70 74 53 79 6e 63 46 6c ager->ckptSyncFl
18fcd 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ags,.
18fce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18fcf 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
18fd0 2c 20 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70 , (u8*)pPager->p
18fd1 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 20 TmpSpace);.
18fd2 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 20 3d 20 pPager->pWal =
18fd3 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 0;. }. }. r
18fd4 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e eturn rc;.}..#en
18fd5 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f dif /* !SQLITE_O
18fd6 4d 49 54 5f 57 41 4c 20 2a 2f 0a 0a 23 69 66 64 MIT_WAL */..#ifd
18fd7 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
18fd8 5f 5a 49 50 56 46 53 0a 2f 2a 0a 2a 2a 20 41 20 _ZIPVFS./*.** A
18fd9 72 65 61 64 2d 6c 6f 63 6b 20 6d 75 73 74 20 62 read-lock must b
18fda 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 70 61 e held on the pa
18fdb 67 65 72 20 77 68 65 6e 20 74 68 69 73 20 66 75 ger when this fu
18fdc 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
18fdd 2e 20 49 66 0a 2a 2a 20 74 68 65 20 70 61 67 65 . If.** the page
18fde 72 20 69 73 20 69 6e 20 57 41 4c 20 6d 6f 64 65 r is in WAL mode
18fdf 20 61 6e 64 20 74 68 65 20 57 41 4c 20 66 69 6c and the WAL fil
18fe0 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 e currently cont
18fe1 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 ains one or more
18fe2 0a 2a 2a 20 66 72 61 6d 65 73 2c 20 72 65 74 75 .** frames, retu
18fe3 72 6e 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 rn the size in b
18fe4 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 ytes of the page
18fe5 20 69 6d 61 67 65 73 20 73 74 6f 72 65 64 20 77 images stored w
18fe6 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 57 41 4c ithin the.** WAL
18fe7 20 66 72 61 6d 65 73 2e 20 4f 74 68 65 72 77 69 frames. Otherwi
18fe8 73 65 2c 20 69 66 20 74 68 69 73 20 69 73 20 6e se, if this is n
18fe9 6f 74 20 61 20 57 41 4c 20 64 61 74 61 62 61 73 ot a WAL databas
18fea 65 20 6f 72 20 74 68 65 20 57 41 4c 20 66 69 6c e or the WAL fil
18feb 65 0a 2a 2a 20 69 73 20 65 6d 70 74 79 2c 20 72 e.** is empty, r
18fec 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 eturn 0..*/.SQLI
18fed 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
18fee 71 6c 69 74 65 33 50 61 67 65 72 57 61 6c 46 72 qlite3PagerWalFr
18fef 61 6d 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 amesize(Pager *p
18ff0 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65 72 74 Pager){. assert
18ff1 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 ( pPager->eState
18ff2 3d 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20 29 ==PAGER_READER )
18ff3 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 ;. return sqlit
18ff4 65 33 57 61 6c 46 72 61 6d 65 73 69 7a 65 28 70 e3WalFramesize(p
18ff5 50 61 67 65 72 2d 3e 70 57 61 6c 29 3b 0a 7d 0a Pager->pWal);.}.
18ff6 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
18ff7 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a QLITE_HAS_CODEC.
18ff8 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
18ff9 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 ion is called by
18ffa 20 74 68 65 20 77 61 6c 20 6d 6f 64 75 6c 65 20 the wal module
18ffb 77 68 65 6e 20 77 72 69 74 69 6e 67 20 70 61 67 when writing pag
18ffc 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 6e 74 e content.** int
18ffd 6f 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e 0a o the log file..
18ffe 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
18fff 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f ion returns a po
19000 69 6e 74 65 72 20 74 6f 20 61 20 62 75 66 66 65 inter to a buffe
19001 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 r containing the
19002 20 65 6e 63 72 79 70 74 65 64 0a 2a 2a 20 70 61 encrypted.** pa
19003 67 65 20 63 6f 6e 74 65 6e 74 2e 20 49 66 20 61 ge content. If a
19004 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 malloc fails, t
19005 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 his function may
19006 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f return NULL..*/
19007 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19008 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
19009 65 72 43 6f 64 65 63 28 50 67 48 64 72 20 2a 70 erCodec(PgHdr *p
1900a 50 67 29 7b 0a 20 20 76 6f 69 64 20 2a 61 44 61 Pg){. void *aDa
1900b 74 61 20 3d 20 30 3b 0a 20 20 43 4f 44 45 43 32 ta = 0;. CODEC2
1900c 28 70 50 67 2d 3e 70 50 61 67 65 72 2c 20 70 50 (pPg->pPager, pP
1900d 67 2d 3e 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 g->pData, pPg->p
1900e 67 6e 6f 2c 20 36 2c 20 72 65 74 75 72 6e 20 30 gno, 6, return 0
1900f 2c 20 61 44 61 74 61 29 3b 0a 20 20 72 65 74 75 , aData);. retu
19010 72 6e 20 61 44 61 74 61 3b 0a 7d 0a 23 65 6e 64 rn aData;.}.#end
19011 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 48 41 53 if /* SQLITE_HAS
19012 5f 43 4f 44 45 43 20 2a 2f 0a 0a 23 65 6e 64 69 _CODEC */..#endi
19013 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
19014 5f 44 49 53 4b 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a _DISKIO */../***
19015 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
19016 6f 66 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a of pager.c *****
19017 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19018 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19019 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1901a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
1901b 6e 20 66 69 6c 65 20 77 61 6c 2e 63 20 2a 2a 2a n file wal.c ***
1901c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1901d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1901e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
1901f 2a 20 32 30 31 30 20 46 65 62 72 75 61 72 79 20 * 2010 February
19020 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 1.**.** The auth
19021 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
19022 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
19023 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
19024 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
19025 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
19026 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
19027 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
19028 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
19029 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
1902a 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
1902b 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
1902c 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
1902d 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
1902e 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
1902f 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
19030 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
19031 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
19032 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19033 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19034 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19035 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19036 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
19037 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d contains the im
19038 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
19039 61 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f a write-ahead lo
1903a 67 20 28 57 41 4c 29 20 75 73 65 64 20 69 6e 20 g (WAL) used in
1903b 0a 2a 2a 20 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 .** "journal_mod
1903c 65 3d 57 41 4c 22 20 6d 6f 64 65 2e 0a 2a 2a 0a e=WAL" mode..**.
1903d 2a 2a 20 57 52 49 54 45 2d 41 48 45 41 44 20 4c ** WRITE-AHEAD L
1903e 4f 47 20 28 57 41 4c 29 20 46 49 4c 45 20 46 4f OG (WAL) FILE FO
1903f 52 4d 41 54 0a 2a 2a 0a 2a 2a 20 41 20 57 41 4c RMAT.**.** A WAL
19040 20 66 69 6c 65 20 63 6f 6e 73 69 73 74 73 20 6f file consists o
19041 66 20 61 20 68 65 61 64 65 72 20 66 6f 6c 6c 6f f a header follo
19042 77 65 64 20 62 79 20 7a 65 72 6f 20 6f 72 20 6d wed by zero or m
19043 6f 72 65 20 22 66 72 61 6d 65 73 22 2e 0a 2a 2a ore "frames"..**
19044 20 45 61 63 68 20 66 72 61 6d 65 20 72 65 63 6f Each frame reco
19045 72 64 73 20 74 68 65 20 72 65 76 69 73 65 64 20 rds the revised
19046 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 73 69 6e content of a sin
19047 67 6c 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 gle page from th
19048 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 e.** database fi
19049 6c 65 2e 20 20 41 6c 6c 20 63 68 61 6e 67 65 73 le. All changes
1904a 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
1904b 20 61 72 65 20 72 65 63 6f 72 64 65 64 20 62 79 are recorded by
1904c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 66 72 61 6d writing.** fram
1904d 65 73 20 69 6e 74 6f 20 74 68 65 20 57 41 4c 2e es into the WAL.
1904e 20 20 54 72 61 6e 73 61 63 74 69 6f 6e 73 20 63 Transactions c
1904f 6f 6d 6d 69 74 20 77 68 65 6e 20 61 20 66 72 61 ommit when a fra
19050 6d 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 68 me is written th
19051 61 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61 at.** contains a
19052 20 63 6f 6d 6d 69 74 20 6d 61 72 6b 65 72 2e 20 commit marker.
19053 20 41 20 73 69 6e 67 6c 65 20 57 41 4c 20 63 61 A single WAL ca
19054 6e 20 61 6e 64 20 75 73 75 61 6c 6c 79 20 64 6f n and usually do
19055 65 73 20 72 65 63 6f 72 64 20 0a 2a 2a 20 6d 75 es record .** mu
19056 6c 74 69 70 6c 65 20 74 72 61 6e 73 61 63 74 69 ltiple transacti
19057 6f 6e 73 2e 20 20 50 65 72 69 6f 64 69 63 61 6c ons. Periodical
19058 6c 79 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ly, the content
19059 6f 66 20 74 68 65 20 57 41 4c 20 69 73 0a 2a 2a of the WAL is.**
1905a 20 74 72 61 6e 73 66 65 72 72 65 64 20 62 61 63 transferred bac
1905b 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 k into the datab
1905c 61 73 65 20 66 69 6c 65 20 69 6e 20 61 6e 20 6f ase file in an o
1905d 70 65 72 61 74 69 6f 6e 20 63 61 6c 6c 65 64 20 peration called
1905e 61 0a 2a 2a 20 22 63 68 65 63 6b 70 6f 69 6e 74 a.** "checkpoint
1905f 22 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c "..**.** A singl
19060 65 20 57 41 4c 20 66 69 6c 65 20 63 61 6e 20 62 e WAL file can b
19061 65 20 75 73 65 64 20 6d 75 6c 74 69 70 6c 65 20 e used multiple
19062 74 69 6d 65 73 2e 20 20 49 6e 20 6f 74 68 65 72 times. In other
19063 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a 2a 20 57 words, the.** W
19064 41 4c 20 63 61 6e 20 66 69 6c 6c 20 75 70 20 77 AL can fill up w
19065 69 74 68 20 66 72 61 6d 65 73 20 61 6e 64 20 74 ith frames and t
19066 68 65 6e 20 62 65 20 63 68 65 63 6b 70 6f 69 6e hen be checkpoin
19067 74 65 64 20 61 6e 64 20 74 68 65 6e 20 6e 65 77 ted and then new
19068 0a 2a 2a 20 66 72 61 6d 65 73 20 63 61 6e 20 6f .** frames can o
19069 76 65 72 77 72 69 74 65 20 74 68 65 20 6f 6c 64 verwrite the old
1906a 20 6f 6e 65 73 2e 20 20 41 20 57 41 4c 20 61 6c ones. A WAL al
1906b 77 61 79 73 20 67 72 6f 77 73 20 66 72 6f 6d 20 ways grows from
1906c 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 74 6f 77 beginning.** tow
1906d 61 72 64 20 74 68 65 20 65 6e 64 2e 20 20 43 68 ard the end. Ch
1906e 65 63 6b 73 75 6d 73 20 61 6e 64 20 63 6f 75 6e ecksums and coun
1906f 74 65 72 73 20 61 74 74 61 63 68 65 64 20 74 6f ters attached to
19070 20 65 61 63 68 20 66 72 61 6d 65 20 61 72 65 0a each frame are.
19071 2a 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 ** used to deter
19072 6d 69 6e 65 20 77 68 69 63 68 20 66 72 61 6d 65 mine which frame
19073 73 20 77 69 74 68 69 6e 20 74 68 65 20 57 41 4c s within the WAL
19074 20 61 72 65 20 76 61 6c 69 64 20 61 6e 64 20 77 are valid and w
19075 68 69 63 68 0a 2a 2a 20 61 72 65 20 6c 65 66 74 hich.** are left
19076 6f 76 65 72 73 20 66 72 6f 6d 20 70 72 69 6f 72 overs from prior
19077 20 63 68 65 63 6b 70 6f 69 6e 74 73 2e 0a 2a 2a checkpoints..**
19078 0a 2a 2a 20 54 68 65 20 57 41 4c 20 68 65 61 64 .** The WAL head
19079 65 72 20 69 73 20 33 32 20 62 79 74 65 73 20 69 er is 32 bytes i
1907a 6e 20 73 69 7a 65 20 61 6e 64 20 63 6f 6e 73 69 n size and consi
1907b 73 74 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f sts of the follo
1907c 77 69 6e 67 20 65 69 67 68 74 0a 2a 2a 20 62 69 wing eight.** bi
1907d 67 2d 65 6e 64 69 61 6e 20 33 32 2d 62 69 74 20 g-endian 32-bit
1907e 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 unsigned integer
1907f 20 76 61 6c 75 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 values:.**.**
19080 20 20 20 30 3a 20 4d 61 67 69 63 20 6e 75 6d 62 0: Magic numb
19081 65 72 2e 20 20 30 78 33 37 37 66 30 36 38 32 20 er. 0x377f0682
19082 6f 72 20 30 78 33 37 37 66 30 36 38 33 0a 2a 2a or 0x377f0683.**
19083 20 20 20 20 20 34 3a 20 46 69 6c 65 20 66 6f 72 4: File for
19084 6d 61 74 20 76 65 72 73 69 6f 6e 2e 20 20 43 75 mat version. Cu
19085 72 72 65 6e 74 6c 79 20 33 30 30 37 30 30 30 0a rrently 3007000.
19086 2a 2a 20 20 20 20 20 38 3a 20 44 61 74 61 62 61 ** 8: Databa
19087 73 65 20 70 61 67 65 20 73 69 7a 65 2e 20 20 45 se page size. E
19088 78 61 6d 70 6c 65 3a 20 31 30 32 34 0a 2a 2a 20 xample: 1024.**
19089 20 20 20 31 32 3a 20 43 68 65 63 6b 70 6f 69 6e 12: Checkpoin
1908a 74 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 t sequence numbe
1908b 72 0a 2a 2a 20 20 20 20 31 36 3a 20 53 61 6c 74 r.** 16: Salt
1908c 2d 31 2c 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 -1, random integ
1908d 65 72 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 er incremented w
1908e 69 74 68 20 65 61 63 68 20 63 68 65 63 6b 70 6f ith each checkpo
1908f 69 6e 74 0a 2a 2a 20 20 20 20 32 30 3a 20 53 61 int.** 20: Sa
19090 6c 74 2d 32 2c 20 61 20 64 69 66 66 65 72 65 6e lt-2, a differen
19091 74 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72 t random integer
19092 20 63 68 61 6e 67 69 6e 67 20 77 69 74 68 20 65 changing with e
19093 61 63 68 20 63 6b 70 74 0a 2a 2a 20 20 20 20 32 ach ckpt.** 2
19094 34 3a 20 43 68 65 63 6b 73 75 6d 2d 31 20 28 66 4: Checksum-1 (f
19095 69 72 73 74 20 70 61 72 74 20 6f 66 20 63 68 65 irst part of che
19096 63 6b 73 75 6d 20 66 6f 72 20 66 69 72 73 74 20 cksum for first
19097 32 34 20 62 79 74 65 73 20 6f 66 20 68 65 61 64 24 bytes of head
19098 65 72 29 2e 0a 2a 2a 20 20 20 20 32 38 3a 20 43 er)..** 28: C
19099 68 65 63 6b 73 75 6d 2d 32 20 28 73 65 63 6f 6e hecksum-2 (secon
1909a 64 20 70 61 72 74 20 6f 66 20 63 68 65 63 6b 73 d part of checks
1909b 75 6d 20 66 6f 72 20 66 69 72 73 74 20 32 34 20 um for first 24
1909c 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 29 bytes of header)
1909d 2e 0a 2a 2a 0a 2a 2a 20 49 6d 6d 65 64 69 61 74 ..**.** Immediat
1909e 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 ely following th
1909f 65 20 77 61 6c 2d 68 65 61 64 65 72 20 61 72 65 e wal-header are
190a0 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 66 72 zero or more fr
190a1 61 6d 65 73 2e 20 45 61 63 68 0a 2a 2a 20 66 72 ames. Each.** fr
190a2 61 6d 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 ame consists of
190a3 61 20 32 34 2d 62 79 74 65 20 66 72 61 6d 65 2d a 24-byte frame-
190a4 68 65 61 64 65 72 20 66 6f 6c 6c 6f 77 65 64 20 header followed
190a5 62 79 20 61 20 3c 70 61 67 65 2d 73 69 7a 65 3e by a <page-size>
190a6 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 70 61 67 bytes.** of pag
190a7 65 20 64 61 74 61 2e 20 54 68 65 20 66 72 61 6d e data. The fram
190a8 65 2d 68 65 61 64 65 72 20 69 73 20 73 69 78 20 e-header is six
190a9 62 69 67 2d 65 6e 64 69 61 6e 20 33 32 2d 62 69 big-endian 32-bi
190aa 74 20 75 6e 73 69 67 6e 65 64 20 0a 2a 2a 20 69 t unsigned .** i
190ab 6e 74 65 67 65 72 20 76 61 6c 75 65 73 2c 20 61 nteger values, a
190ac 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
190ad 20 20 20 20 20 30 3a 20 50 61 67 65 20 6e 75 6d 0: Page num
190ae 62 65 72 2e 0a 2a 2a 20 20 20 20 20 34 3a 20 46 ber..** 4: F
190af 6f 72 20 63 6f 6d 6d 69 74 20 72 65 63 6f 72 64 or commit record
190b0 73 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 s, the size of t
190b1 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 he database imag
190b2 65 20 69 6e 20 70 61 67 65 73 20 0a 2a 2a 20 20 e in pages .**
190b3 20 20 20 20 20 20 61 66 74 65 72 20 74 68 65 20 after the
190b4 63 6f 6d 6d 69 74 2e 20 46 6f 72 20 61 6c 6c 20 commit. For all
190b5 6f 74 68 65 72 20 72 65 63 6f 72 64 73 2c 20 7a other records, z
190b6 65 72 6f 2e 0a 2a 2a 20 20 20 20 20 38 3a 20 53 ero..** 8: S
190b7 61 6c 74 2d 31 20 28 63 6f 70 69 65 64 20 66 72 alt-1 (copied fr
190b8 6f 6d 20 74 68 65 20 68 65 61 64 65 72 29 0a 2a om the header).*
190b9 2a 20 20 20 20 31 32 3a 20 53 61 6c 74 2d 32 20 * 12: Salt-2
190ba 28 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 (copied from the
190bb 20 68 65 61 64 65 72 29 0a 2a 2a 20 20 20 20 31 header).** 1
190bc 36 3a 20 43 68 65 63 6b 73 75 6d 2d 31 2e 0a 2a 6: Checksum-1..*
190bd 2a 20 20 20 20 32 30 3a 20 43 68 65 63 6b 73 75 * 20: Checksu
190be 6d 2d 32 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 72 61 m-2..**.** A fra
190bf 6d 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 me is considered
190c0 20 76 61 6c 69 64 20 69 66 20 61 6e 64 20 6f 6e valid if and on
190c1 6c 79 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ly if the follow
190c2 69 6e 67 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 ing conditions a
190c3 72 65 0a 2a 2a 20 74 72 75 65 3a 0a 2a 2a 0a 2a re.** true:.**.*
190c4 2a 20 20 20 20 28 31 29 20 54 68 65 20 73 61 6c * (1) The sal
190c5 74 2d 31 20 61 6e 64 20 73 61 6c 74 2d 32 20 76 t-1 and salt-2 v
190c6 61 6c 75 65 73 20 69 6e 20 74 68 65 20 66 72 61 alues in the fra
190c7 6d 65 2d 68 65 61 64 65 72 20 6d 61 74 63 68 0a me-header match.
190c8 2a 2a 20 20 20 20 20 20 20 20 73 61 6c 74 20 76 ** salt v
190c9 61 6c 75 65 73 20 69 6e 20 74 68 65 20 77 61 6c alues in the wal
190ca 2d 68 65 61 64 65 72 0a 2a 2a 0a 2a 2a 20 20 20 -header.**.**
190cb 20 28 32 29 20 54 68 65 20 63 68 65 63 6b 73 75 (2) The checksu
190cc 6d 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 m values in the
190cd 66 69 6e 61 6c 20 38 20 62 79 74 65 73 20 6f 66 final 8 bytes of
190ce 20 74 68 65 20 66 72 61 6d 65 2d 68 65 61 64 65 the frame-heade
190cf 72 0a 2a 2a 20 20 20 20 20 20 20 20 65 78 61 63 r.** exac
190d0 74 6c 79 20 6d 61 74 63 68 20 74 68 65 20 63 68 tly match the ch
190d1 65 63 6b 73 75 6d 20 63 6f 6d 70 75 74 65 64 20 ecksum computed
190d2 63 6f 6e 73 65 63 75 74 69 76 65 6c 79 20 6f 6e consecutively on
190d3 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 57 the.** W
190d4 41 4c 20 68 65 61 64 65 72 20 61 6e 64 20 74 68 AL header and th
190d5 65 20 66 69 72 73 74 20 38 20 62 79 74 65 73 20 e first 8 bytes
190d6 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 and the content
190d7 6f 66 20 61 6c 6c 20 66 72 61 6d 65 73 0a 2a 2a of all frames.**
190d8 20 20 20 20 20 20 20 20 75 70 20 74 6f 20 61 6e up to an
190d9 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 d including the
190da 63 75 72 72 65 6e 74 20 66 72 61 6d 65 2e 0a 2a current frame..*
190db 2a 0a 2a 2a 20 54 68 65 20 63 68 65 63 6b 73 75 *.** The checksu
190dc 6d 20 69 73 20 63 6f 6d 70 75 74 65 64 20 75 73 m is computed us
190dd 69 6e 67 20 33 32 2d 62 69 74 20 62 69 67 2d 65 ing 32-bit big-e
190de 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 73 20 69 ndian integers i
190df 66 20 74 68 65 0a 2a 2a 20 6d 61 67 69 63 20 6e f the.** magic n
190e0 75 6d 62 65 72 20 69 6e 20 74 68 65 20 66 69 72 umber in the fir
190e1 73 74 20 34 20 62 79 74 65 73 20 6f 66 20 74 68 st 4 bytes of th
190e2 65 20 57 41 4c 20 69 73 20 30 78 33 37 37 66 30 e WAL is 0x377f0
190e3 36 38 33 20 61 6e 64 20 69 74 0a 2a 2a 20 69 73 683 and it.** is
190e4 20 63 6f 6d 70 75 74 65 64 20 75 73 69 6e 67 20 computed using
190e5 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 69 66 little-endian if
190e6 20 74 68 65 20 6d 61 67 69 63 20 6e 75 6d 62 65 the magic numbe
190e7 72 20 69 73 20 30 78 33 37 37 66 30 36 38 32 2e r is 0x377f0682.
190e8 0a 2a 2a 20 54 68 65 20 63 68 65 63 6b 73 75 6d .** The checksum
190e9 20 76 61 6c 75 65 73 20 61 72 65 20 61 6c 77 61 values are alwa
190ea 79 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 ys stored in the
190eb 20 66 72 61 6d 65 20 68 65 61 64 65 72 20 69 6e frame header in
190ec 20 61 0a 2a 2a 20 62 69 67 2d 65 6e 64 69 61 6e a.** big-endian
190ed 20 66 6f 72 6d 61 74 20 72 65 67 61 72 64 6c 65 format regardle
190ee 73 73 20 6f 66 20 77 68 69 63 68 20 62 79 74 65 ss of which byte
190ef 20 6f 72 64 65 72 20 69 73 20 75 73 65 64 20 74 order is used t
190f0 6f 20 63 6f 6d 70 75 74 65 0a 2a 2a 20 74 68 65 o compute.** the
190f1 20 63 68 65 63 6b 73 75 6d 2e 20 20 54 68 65 20 checksum. The
190f2 63 68 65 63 6b 73 75 6d 20 69 73 20 63 6f 6d 70 checksum is comp
190f3 75 74 65 64 20 62 79 20 69 6e 74 65 72 70 72 65 uted by interpre
190f4 74 69 6e 67 20 74 68 65 20 69 6e 70 75 74 20 61 ting the input a
190f5 73 0a 2a 2a 20 61 6e 20 65 76 65 6e 20 6e 75 6d s.** an even num
190f6 62 65 72 20 6f 66 20 75 6e 73 69 67 6e 65 64 20 ber of unsigned
190f7 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 73 3a 32-bit integers:
190f8 20 78 5b 30 5d 20 74 68 72 6f 75 67 68 20 78 5b x[0] through x[
190f9 4e 5d 2e 20 20 54 68 65 0a 2a 2a 20 61 6c 67 6f N]. The.** algo
190fa 72 69 74 68 6d 20 75 73 65 64 20 66 6f 72 20 74 rithm used for t
190fb 68 65 20 63 68 65 63 6b 73 75 6d 20 69 73 20 61 he checksum is a
190fc 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 0a 2a s follows:.** .*
190fd 2a 20 20 20 66 6f 72 20 69 20 66 72 6f 6d 20 30 * for i from 0
190fe 20 74 6f 20 6e 2d 31 20 73 74 65 70 20 32 3a 0a to n-1 step 2:.
190ff 2a 2a 20 20 20 20 20 73 30 20 2b 3d 20 78 5b 69 ** s0 += x[i
19100 5d 20 2b 20 73 31 3b 0a 2a 2a 20 20 20 20 20 73 ] + s1;.** s
19101 31 20 2b 3d 20 78 5b 69 2b 31 5d 20 2b 20 73 30 1 += x[i+1] + s0
19102 3b 0a 2a 2a 20 20 20 65 6e 64 66 6f 72 0a 2a 2a ;.** endfor.**
19103 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 73 30 .** Note that s0
19104 20 61 6e 64 20 73 31 20 61 72 65 20 62 6f 74 68 and s1 are both
19105 20 77 65 69 67 68 74 65 64 20 63 68 65 63 6b 73 weighted checks
19106 75 6d 73 20 75 73 69 6e 67 20 66 69 62 6f 6e 61 ums using fibona
19107 63 63 69 20 77 65 69 67 68 74 73 0a 2a 2a 20 69 cci weights.** i
19108 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 n reverse order
19109 28 74 68 65 20 6c 61 72 67 65 73 74 20 66 69 62 (the largest fib
1910a 6f 6e 61 63 63 69 20 77 65 69 67 68 74 20 6f 63 onacci weight oc
1910b 63 75 72 73 20 6f 6e 20 74 68 65 20 66 69 72 73 curs on the firs
1910c 74 20 65 6c 65 6d 65 6e 74 0a 2a 2a 20 6f 66 20 t element.** of
1910d 74 68 65 20 73 65 71 75 65 6e 63 65 20 62 65 69 the sequence bei
1910e 6e 67 20 73 75 6d 6d 65 64 2e 29 20 20 54 68 65 ng summed.) The
1910f 20 73 31 20 76 61 6c 75 65 20 73 70 61 6e 73 20 s1 value spans
19110 61 6c 6c 20 33 32 2d 62 69 74 20 0a 2a 2a 20 74 all 32-bit .** t
19111 65 72 6d 73 20 6f 66 20 74 68 65 20 73 65 71 75 erms of the sequ
19112 65 6e 63 65 20 77 68 65 72 65 61 73 20 73 30 20 ence whereas s0
19113 6f 6d 69 74 73 20 74 68 65 20 66 69 6e 61 6c 20 omits the final
19114 74 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 61 term..**.** On a
19115 20 63 68 65 63 6b 70 6f 69 6e 74 2c 20 74 68 65 checkpoint, the
19116 20 57 41 4c 20 69 73 20 66 69 72 73 74 20 56 46 WAL is first VF
19117 53 2e 78 53 79 6e 63 2d 65 64 2c 20 74 68 65 6e S.xSync-ed, then
19118 20 76 61 6c 69 64 20 63 6f 6e 74 65 6e 74 20 6f valid content o
19119 66 20 74 68 65 0a 2a 2a 20 57 41 4c 20 69 73 20 f the.** WAL is
1911a 74 72 61 6e 73 66 65 72 72 65 64 20 69 6e 74 6f transferred into
1911b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74 the database, t
1911c 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
1911d 20 69 73 20 56 46 53 2e 78 53 79 6e 63 2d 65 64 is VFS.xSync-ed
1911e 2e 0a 2a 2a 20 54 68 65 20 56 46 53 2e 78 53 79 ..** The VFS.xSy
1911f 6e 63 20 6f 70 65 72 61 74 69 6f 6e 73 20 73 65 nc operations se
19120 72 76 65 20 61 73 20 77 72 69 74 65 20 62 61 72 rve as write bar
19121 72 69 65 72 73 20 2d 20 61 6c 6c 20 77 72 69 74 riers - all writ
19122 65 73 20 6c 61 75 6e 63 68 65 64 0a 2a 2a 20 62 es launched.** b
19123 65 66 6f 72 65 20 74 68 65 20 78 53 79 6e 63 20 efore the xSync
19124 6d 75 73 74 20 63 6f 6d 70 6c 65 74 65 20 62 65 must complete be
19125 66 6f 72 65 20 61 6e 79 20 77 72 69 74 65 20 74 fore any write t
19126 68 61 74 20 6c 61 75 6e 63 68 65 73 20 61 66 74 hat launches aft
19127 65 72 20 74 68 65 0a 2a 2a 20 78 53 79 6e 63 20 er the.** xSync
19128 62 65 67 69 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 66 begins..**.** Af
19129 74 65 72 20 65 61 63 68 20 63 68 65 63 6b 70 6f ter each checkpo
1912a 69 6e 74 2c 20 74 68 65 20 73 61 6c 74 2d 31 20 int, the salt-1
1912b 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 6d 65 value is increme
1912c 6e 74 65 64 20 61 6e 64 20 74 68 65 20 73 61 6c nted and the sal
1912d 74 2d 32 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 t-2.** value is
1912e 72 61 6e 64 6f 6d 69 7a 65 64 2e 20 20 54 68 69 randomized. Thi
1912f 73 20 70 72 65 76 65 6e 74 73 20 6f 6c 64 20 61 s prevents old a
19130 6e 64 20 6e 65 77 20 66 72 61 6d 65 73 20 69 6e nd new frames in
19131 20 74 68 65 20 57 41 4c 20 66 72 6f 6d 0a 2a 2a the WAL from.**
19132 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65 being considere
19133 64 20 76 61 6c 69 64 20 61 74 20 74 68 65 20 73 d valid at the s
19134 61 6d 65 20 74 69 6d 65 20 61 6e 64 20 62 65 69 ame time and bei
19135 6e 67 20 63 68 65 63 6b 70 6f 69 6e 74 69 6e 67 ng checkpointing
19136 20 74 6f 67 65 74 68 65 72 0a 2a 2a 20 66 6f 6c together.** fol
19137 6c 6f 77 69 6e 67 20 61 20 63 72 61 73 68 2e 0a lowing a crash..
19138 2a 2a 0a 2a 2a 20 52 45 41 44 45 52 20 41 4c 47 **.** READER ALG
19139 4f 52 49 54 48 4d 0a 2a 2a 0a 2a 2a 20 54 6f 20 ORITHM.**.** To
1913a 72 65 61 64 20 61 20 70 61 67 65 20 66 72 6f 6d read a page from
1913b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 63 the database (c
1913c 61 6c 6c 20 69 74 20 70 61 67 65 20 6e 75 6d 62 all it page numb
1913d 65 72 20 50 29 2c 20 61 20 72 65 61 64 65 72 0a er P), a reader.
1913e 2a 2a 20 66 69 72 73 74 20 63 68 65 63 6b 73 20 ** first checks
1913f 74 68 65 20 57 41 4c 20 74 6f 20 73 65 65 20 69 the WAL to see i
19140 66 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 70 61 f it contains pa
19141 67 65 20 50 2e 20 20 49 66 20 73 6f 2c 20 74 68 ge P. If so, th
19142 65 6e 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 76 en the.** last v
19143 61 6c 69 64 20 69 6e 73 74 61 6e 63 65 20 6f 66 alid instance of
19144 20 70 61 67 65 20 50 20 74 68 61 74 20 69 73 20 page P that is
19145 61 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 a followed by a
19146 63 6f 6d 6d 69 74 20 66 72 61 6d 65 0a 2a 2a 20 commit frame.**
19147 6f 72 20 69 73 20 61 20 63 6f 6d 6d 69 74 20 66 or is a commit f
19148 72 61 6d 65 20 69 74 73 65 6c 66 20 62 65 63 6f rame itself beco
19149 6d 65 73 20 74 68 65 20 76 61 6c 75 65 20 72 65 mes the value re
1914a 61 64 2e 20 20 49 66 20 74 68 65 20 57 41 4c 0a ad. If the WAL.
1914b 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 ** contains no c
1914c 6f 70 69 65 73 20 6f 66 20 70 61 67 65 20 50 20 opies of page P
1914d 74 68 61 74 20 61 72 65 20 76 61 6c 69 64 20 61 that are valid a
1914e 6e 64 20 77 68 69 63 68 20 61 72 65 20 61 20 63 nd which are a c
1914f 6f 6d 6d 69 74 0a 2a 2a 20 66 72 61 6d 65 20 6f ommit.** frame o
19150 72 20 61 72 65 20 66 6f 6c 6c 6f 77 65 64 20 62 r are followed b
19151 79 20 61 20 63 6f 6d 6d 69 74 20 66 72 61 6d 65 y a commit frame
19152 2c 20 74 68 65 6e 20 70 61 67 65 20 50 20 69 73 , then page P is
19153 20 72 65 61 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 read from.** th
19154 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
19155 0a 2a 2a 0a 2a 2a 20 54 6f 20 73 74 61 72 74 20 .**.** To start
19156 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 a read transacti
19157 6f 6e 2c 20 74 68 65 20 72 65 61 64 65 72 20 72 on, the reader r
19158 65 63 6f 72 64 73 20 74 68 65 20 69 6e 64 65 78 ecords the index
19159 20 6f 66 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 of the last.**
1915a 76 61 6c 69 64 20 66 72 61 6d 65 20 69 6e 20 74 valid frame in t
1915b 68 65 20 57 41 4c 2e 20 20 54 68 65 20 72 65 61 he WAL. The rea
1915c 64 65 72 20 75 73 65 73 20 74 68 69 73 20 72 65 der uses this re
1915d 63 6f 72 64 65 64 20 22 6d 78 46 72 61 6d 65 22 corded "mxFrame"
1915e 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 61 6c value.** for al
1915f 6c 20 73 75 62 73 65 71 75 65 6e 74 20 72 65 61 l subsequent rea
19160 64 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 4e d operations. N
19161 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 ew transactions
19162 63 61 6e 20 62 65 20 61 70 70 65 6e 64 65 64 0a can be appended.
19163 2a 2a 20 74 6f 20 74 68 65 20 57 41 4c 2c 20 62 ** to the WAL, b
19164 75 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 ut as long as th
19165 65 20 72 65 61 64 65 72 20 75 73 65 73 20 69 74 e reader uses it
19166 73 20 6f 72 69 67 69 6e 61 6c 20 6d 78 46 72 61 s original mxFra
19167 6d 65 20 76 61 6c 75 65 0a 2a 2a 20 61 6e 64 20 me value.** and
19168 69 67 6e 6f 72 65 73 20 74 68 65 20 6e 65 77 6c ignores the newl
19169 79 20 61 70 70 65 6e 64 65 64 20 63 6f 6e 74 65 y appended conte
1916a 6e 74 2c 20 69 74 20 77 69 6c 6c 20 73 65 65 20 nt, it will see
1916b 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 73 6e 61 a consistent sna
1916c 70 73 68 6f 74 0a 2a 2a 20 6f 66 20 74 68 65 20 pshot.** of the
1916d 64 61 74 61 62 61 73 65 20 66 72 6f 6d 20 61 20 database from a
1916e 73 69 6e 67 6c 65 20 70 6f 69 6e 74 20 69 6e 20 single point in
1916f 74 69 6d 65 2e 20 20 54 68 69 73 20 74 65 63 68 time. This tech
19170 6e 69 71 75 65 20 61 6c 6c 6f 77 73 0a 2a 2a 20 nique allows.**
19171 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63 75 72 72 multiple concurr
19172 65 6e 74 20 72 65 61 64 65 72 73 20 74 6f 20 76 ent readers to v
19173 69 65 77 20 64 69 66 66 65 72 65 6e 74 20 76 65 iew different ve
19174 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 rsions of the da
19175 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 74 65 6e tabase.** conten
19176 74 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 t simultaneously
19177 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 61 64 ..**.** The read
19178 65 72 20 61 6c 67 6f 72 69 74 68 6d 20 69 6e 20 er algorithm in
19179 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 72 the previous par
1917a 61 67 72 61 70 68 73 20 77 6f 72 6b 73 20 63 6f agraphs works co
1917b 72 72 65 63 74 6c 79 2c 20 62 75 74 20 0a 2a 2a rrectly, but .**
1917c 20 62 65 63 61 75 73 65 20 66 72 61 6d 65 73 20 because frames
1917d 66 6f 72 20 70 61 67 65 20 50 20 63 61 6e 20 61 for page P can a
1917e 70 70 65 61 72 20 61 6e 79 77 68 65 72 65 20 77 ppear anywhere w
1917f 69 74 68 69 6e 20 74 68 65 20 57 41 4c 2c 20 74 ithin the WAL, t
19180 68 65 0a 2a 2a 20 72 65 61 64 65 72 20 68 61 73 he.** reader has
19181 20 74 6f 20 73 63 61 6e 20 74 68 65 20 65 6e 74 to scan the ent
19182 69 72 65 20 57 41 4c 20 6c 6f 6f 6b 69 6e 67 20 ire WAL looking
19183 66 6f 72 20 70 61 67 65 20 50 20 66 72 61 6d 65 for page P frame
19184 73 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 57 41 s. If the.** WA
19185 4c 20 69 73 20 6c 61 72 67 65 20 28 6d 75 6c 74 L is large (mult
19186 69 70 6c 65 20 6d 65 67 61 62 79 74 65 73 20 69 iple megabytes i
19187 73 20 74 79 70 69 63 61 6c 29 20 74 68 61 74 20 s typical) that
19188 73 63 61 6e 20 63 61 6e 20 62 65 20 73 6c 6f 77 scan can be slow
19189 2c 0a 2a 2a 20 61 6e 64 20 72 65 61 64 20 70 65 ,.** and read pe
1918a 72 66 6f 72 6d 61 6e 63 65 20 73 75 66 66 65 72 rformance suffer
1918b 73 2e 20 20 54 6f 20 6f 76 65 72 63 6f 6d 65 20 s. To overcome
1918c 74 68 69 73 20 70 72 6f 62 6c 65 6d 2c 20 61 20 this problem, a
1918d 73 65 70 61 72 61 74 65 0a 2a 2a 20 64 61 74 61 separate.** data
1918e 20 73 74 72 75 63 74 75 72 65 20 63 61 6c 6c 65 structure calle
1918f 64 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 d the wal-index
19190 69 73 20 6d 61 69 6e 74 61 69 6e 65 64 20 74 6f is maintained to
19191 20 65 78 70 65 64 69 74 65 20 74 68 65 0a 2a 2a expedite the.**
19192 20 73 65 61 72 63 68 20 66 6f 72 20 66 72 61 6d search for fram
19193 65 73 20 6f 66 20 61 20 70 61 72 74 69 63 75 6c es of a particul
19194 61 72 20 70 61 67 65 2e 0a 2a 2a 20 0a 2a 2a 20 ar page..** .**
19195 57 41 4c 2d 49 4e 44 45 58 20 46 4f 52 4d 41 54 WAL-INDEX FORMAT
19196 0a 2a 2a 0a 2a 2a 20 43 6f 6e 63 65 70 74 75 61 .**.** Conceptua
19197 6c 6c 79 2c 20 74 68 65 20 77 61 6c 2d 69 6e 64 lly, the wal-ind
19198 65 78 20 69 73 20 73 68 61 72 65 64 20 6d 65 6d ex is shared mem
19199 6f 72 79 2c 20 74 68 6f 75 67 68 20 56 46 53 20 ory, though VFS
1919a 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a implementations.
1919b 2a 2a 20 6d 69 67 68 74 20 63 68 6f 6f 73 65 20 ** might choose
1919c 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
1919d 20 77 61 6c 2d 69 6e 64 65 78 20 75 73 69 6e 67 wal-index using
1919e 20 61 20 6d 6d 61 70 70 65 64 20 66 69 6c 65 2e a mmapped file.
1919f 20 20 42 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 Because.** the
191a0 20 77 61 6c 2d 69 6e 64 65 78 20 69 73 20 73 68 wal-index is sh
191a1 61 72 65 64 20 6d 65 6d 6f 72 79 2c 20 53 51 4c ared memory, SQL
191a2 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 ite does not sup
191a3 70 6f 72 74 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 port journal_mod
191a4 65 3d 57 41 4c 20 0a 2a 2a 20 6f 6e 20 61 20 6e e=WAL .** on a n
191a5 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 73 74 65 etwork filesyste
191a6 6d 2e 20 20 41 6c 6c 20 75 73 65 72 73 20 6f 66 m. All users of
191a7 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 the database mu
191a8 73 74 20 62 65 20 61 62 6c 65 20 74 6f 0a 2a 2a st be able to.**
191a9 20 73 68 61 72 65 20 6d 65 6d 6f 72 79 2e 0a 2a share memory..*
191aa 2a 0a 2a 2a 20 54 68 65 20 77 61 6c 2d 69 6e 64 *.** The wal-ind
191ab 65 78 20 69 73 20 74 72 61 6e 73 69 65 6e 74 2e ex is transient.
191ac 20 20 41 66 74 65 72 20 61 20 63 72 61 73 68 2c After a crash,
191ad 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 63 the wal-index c
191ae 61 6e 20 28 61 6e 64 20 73 68 6f 75 6c 64 0a 2a an (and should.*
191af 2a 20 62 65 29 20 72 65 63 6f 6e 73 74 72 75 63 * be) reconstruc
191b0 74 65 64 20 66 72 6f 6d 20 74 68 65 20 6f 72 69 ted from the ori
191b1 67 69 6e 61 6c 20 57 41 4c 20 66 69 6c 65 2e 20 ginal WAL file.
191b2 20 49 6e 20 66 61 63 74 2c 20 74 68 65 20 56 46 In fact, the VF
191b3 53 20 69 73 20 72 65 71 75 69 72 65 64 0a 2a 2a S is required.**
191b4 20 74 6f 20 65 69 74 68 65 72 20 74 72 75 6e 63 to either trunc
191b5 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 ate or zero the
191b6 68 65 61 64 65 72 20 6f 66 20 74 68 65 20 77 61 header of the wa
191b7 6c 2d 69 6e 64 65 78 20 77 68 65 6e 20 74 68 65 l-index when the
191b8 20 6c 61 73 74 0a 2a 2a 20 63 6f 6e 6e 65 63 74 last.** connect
191b9 69 6f 6e 20 74 6f 20 69 74 20 63 6c 6f 73 65 73 ion to it closes
191ba 2e 20 20 42 65 63 61 75 73 65 20 74 68 65 20 77 . Because the w
191bb 61 6c 2d 69 6e 64 65 78 20 69 73 20 74 72 61 6e al-index is tran
191bc 73 69 65 6e 74 2c 20 69 74 20 63 61 6e 0a 2a 2a sient, it can.**
191bd 20 75 73 65 20 61 6e 20 61 72 63 68 69 74 65 63 use an architec
191be 74 75 72 65 2d 73 70 65 63 69 66 69 63 20 66 6f ture-specific fo
191bf 72 6d 61 74 3b 20 69 74 20 64 6f 65 73 20 6e 6f rmat; it does no
191c0 74 20 68 61 76 65 20 74 6f 20 62 65 20 63 72 6f t have to be cro
191c1 73 73 2d 70 6c 61 74 66 6f 72 6d 2e 0a 2a 2a 20 ss-platform..**
191c2 48 65 6e 63 65 2c 20 75 6e 6c 69 6b 65 20 74 68 Hence, unlike th
191c3 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 57 e database and W
191c4 41 4c 20 66 69 6c 65 20 66 6f 72 6d 61 74 73 20 AL file formats
191c5 77 68 69 63 68 20 73 74 6f 72 65 20 61 6c 6c 20 which store all
191c6 76 61 6c 75 65 73 0a 2a 2a 20 61 73 20 62 69 67 values.** as big
191c7 20 65 6e 64 69 61 6e 2c 20 74 68 65 20 77 61 6c endian, the wal
191c8 2d 69 6e 64 65 78 20 63 61 6e 20 73 74 6f 72 65 -index can store
191c9 20 6d 75 6c 74 69 2d 62 79 74 65 20 76 61 6c 75 multi-byte valu
191ca 65 73 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 es in the native
191cb 0a 2a 2a 20 62 79 74 65 20 6f 72 64 65 72 20 6f .** byte order o
191cc 66 20 74 68 65 20 68 6f 73 74 20 63 6f 6d 70 75 f the host compu
191cd 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ter..**.** The p
191ce 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 77 61 urpose of the wa
191cf 6c 2d 69 6e 64 65 78 20 69 73 20 74 6f 20 61 6e l-index is to an
191d0 73 77 65 72 20 74 68 69 73 20 71 75 65 73 74 69 swer this questi
191d1 6f 6e 20 71 75 69 63 6b 6c 79 3a 20 20 47 69 76 on quickly: Giv
191d2 65 6e 0a 2a 2a 20 61 20 70 61 67 65 20 6e 75 6d en.** a page num
191d3 62 65 72 20 50 20 61 6e 64 20 61 20 6d 61 78 69 ber P and a maxi
191d4 6d 75 6d 20 66 72 61 6d 65 20 69 6e 64 65 78 20 mum frame index
191d5 4d 2c 20 72 65 74 75 72 6e 20 74 68 65 20 69 6e M, return the in
191d6 64 65 78 20 6f 66 20 74 68 65 20 0a 2a 2a 20 6c dex of the .** l
191d7 61 73 74 20 66 72 61 6d 65 20 69 6e 20 74 68 65 ast frame in the
191d8 20 77 61 6c 20 62 65 66 6f 72 65 20 66 72 61 6d wal before fram
191d9 65 20 4d 20 66 6f 72 20 70 61 67 65 20 50 20 69 e M for page P i
191da 6e 20 74 68 65 20 57 41 4c 2c 20 6f 72 20 72 65 n the WAL, or re
191db 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 20 69 66 20 turn.** NULL if
191dc 74 68 65 72 65 20 61 72 65 20 6e 6f 20 66 72 61 there are no fra
191dd 6d 65 73 20 66 6f 72 20 70 61 67 65 20 50 20 69 mes for page P i
191de 6e 20 74 68 65 20 57 41 4c 20 70 72 69 6f 72 20 n the WAL prior
191df 74 6f 20 4d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 to M..**.** The
191e0 77 61 6c 2d 69 6e 64 65 78 20 63 6f 6e 73 69 73 wal-index consis
191e1 74 73 20 6f 66 20 61 20 68 65 61 64 65 72 20 72 ts of a header r
191e2 65 67 69 6f 6e 2c 20 66 6f 6c 6c 6f 77 65 64 20 egion, followed
191e3 62 79 20 61 6e 20 6f 6e 65 20 6f 72 0a 2a 2a 20 by an one or.**
191e4 6d 6f 72 65 20 69 6e 64 65 78 20 62 6c 6f 63 6b more index block
191e5 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 s. .**.** The w
191e6 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20 al-index header
191e7 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 6f 74 contains the tot
191e8 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 61 al number of fra
191e9 6d 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 57 mes within the W
191ea 41 4c 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 78 46 AL.** in the mxF
191eb 72 61 6d 65 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a rame field..**.*
191ec 2a 20 45 61 63 68 20 69 6e 64 65 78 20 62 6c 6f * Each index blo
191ed 63 6b 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 ck except for th
191ee 65 20 66 69 72 73 74 20 63 6f 6e 74 61 69 6e 73 e first contains
191ef 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 information on
191f0 0a 2a 2a 20 48 41 53 48 54 41 42 4c 45 5f 4e 50 .** HASHTABLE_NP
191f1 41 47 45 20 66 72 61 6d 65 73 2e 20 54 68 65 20 AGE frames. The
191f2 66 69 72 73 74 20 69 6e 64 65 78 20 62 6c 6f 63 first index bloc
191f3 6b 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 k contains infor
191f4 6d 61 74 69 6f 6e 20 6f 6e 0a 2a 2a 20 48 41 53 mation on.** HAS
191f5 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 HTABLE_NPAGE_ONE
191f6 20 66 72 61 6d 65 73 2e 20 54 68 65 20 76 61 6c frames. The val
191f7 75 65 73 20 6f 66 20 48 41 53 48 54 41 42 4c 45 ues of HASHTABLE
191f8 5f 4e 50 41 47 45 5f 4f 4e 45 20 61 6e 64 20 0a _NPAGE_ONE and .
191f9 2a 2a 20 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 ** HASHTABLE_NPA
191fa 47 45 20 61 72 65 20 73 65 6c 65 63 74 65 64 20 GE are selected
191fb 73 6f 20 74 68 61 74 20 74 6f 67 65 74 68 65 72 so that together
191fc 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 68 the wal-index h
191fd 65 61 64 65 72 20 61 6e 64 0a 2a 2a 20 66 69 72 eader and.** fir
191fe 73 74 20 69 6e 64 65 78 20 62 6c 6f 63 6b 20 61 st index block a
191ff 72 65 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 re the same size
19200 20 61 73 20 61 6c 6c 20 6f 74 68 65 72 20 69 6e as all other in
19201 64 65 78 20 62 6c 6f 63 6b 73 20 69 6e 20 74 68 dex blocks in th
19202 65 0a 2a 2a 20 77 61 6c 2d 69 6e 64 65 78 2e 0a e.** wal-index..
19203 2a 2a 0a 2a 2a 20 45 61 63 68 20 69 6e 64 65 78 **.** Each index
19204 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20 block contains
19205 74 77 6f 20 73 65 63 74 69 6f 6e 73 2c 20 61 20 two sections, a
19206 70 61 67 65 2d 6d 61 70 70 69 6e 67 20 74 68 61 page-mapping tha
19207 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a t contains the.*
19208 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 * database page
19209 6e 75 6d 62 65 72 20 61 73 73 6f 63 69 61 74 65 number associate
1920a 64 20 77 69 74 68 20 65 61 63 68 20 77 61 6c 20 d with each wal
1920b 66 72 61 6d 65 2c 20 61 6e 64 20 61 20 68 61 73 frame, and a has
1920c 68 2d 74 61 62 6c 65 20 0a 2a 2a 20 74 68 61 74 h-table .** that
1920d 20 61 6c 6c 6f 77 73 20 72 65 61 64 65 72 73 20 allows readers
1920e 74 6f 20 71 75 65 72 79 20 61 6e 20 69 6e 64 65 to query an inde
1920f 78 20 62 6c 6f 63 6b 20 66 6f 72 20 61 20 73 70 x block for a sp
19210 65 63 69 66 69 63 20 70 61 67 65 20 6e 75 6d 62 ecific page numb
19211 65 72 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65 2d er..** The page-
19212 6d 61 70 70 69 6e 67 20 69 73 20 61 6e 20 61 72 mapping is an ar
19213 72 61 79 20 6f 66 20 48 41 53 48 54 41 42 4c 45 ray of HASHTABLE
19214 5f 4e 50 41 47 45 20 28 6f 72 20 48 41 53 48 54 _NPAGE (or HASHT
19215 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 0a 2a ABLE_NPAGE_ONE.*
19216 2a 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 * for the first
19217 69 6e 64 65 78 20 62 6c 6f 63 6b 29 20 33 32 2d index block) 32-
19218 62 69 74 20 70 61 67 65 20 6e 75 6d 62 65 72 73 bit page numbers
19219 2e 20 54 68 65 20 66 69 72 73 74 20 65 6e 74 72 . The first entr
1921a 79 20 69 6e 20 74 68 65 20 0a 2a 2a 20 66 69 72 y in the .** fir
1921b 73 74 20 69 6e 64 65 78 2d 62 6c 6f 63 6b 20 63 st index-block c
1921c 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 61 ontains the data
1921d 62 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 base page number
1921e 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
1921f 6f 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 66 o the.** first f
19220 72 61 6d 65 20 69 6e 20 74 68 65 20 57 41 4c 20 rame in the WAL
19221 66 69 6c 65 2e 20 54 68 65 20 66 69 72 73 74 20 file. The first
19222 65 6e 74 72 79 20 69 6e 20 74 68 65 20 73 65 63 entry in the sec
19223 6f 6e 64 20 69 6e 64 65 78 20 62 6c 6f 63 6b 0a ond index block.
19224 2a 2a 20 69 6e 20 74 68 65 20 57 41 4c 20 66 69 ** in the WAL fi
19225 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 le corresponds t
19226 6f 20 74 68 65 20 28 48 41 53 48 54 41 42 4c 45 o the (HASHTABLE
19227 5f 4e 50 41 47 45 5f 4f 4e 45 2b 31 29 74 68 20 _NPAGE_ONE+1)th
19228 66 72 61 6d 65 20 69 6e 0a 2a 2a 20 74 68 65 20 frame in.** the
19229 6c 6f 67 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a log, and so on..
1922a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 69 **.** The last i
1922b 6e 64 65 78 20 62 6c 6f 63 6b 20 69 6e 20 61 20 ndex block in a
1922c 77 61 6c 2d 69 6e 64 65 78 20 75 73 75 61 6c 6c wal-index usuall
1922d 79 20 63 6f 6e 74 61 69 6e 73 20 6c 65 73 73 20 y contains less
1922e 74 68 61 6e 20 74 68 65 20 66 75 6c 6c 0a 2a 2a than the full.**
1922f 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 48 complement of H
19230 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 20 28 ASHTABLE_NPAGE (
19231 6f 72 20 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 or HASHTABLE_NPA
19232 47 45 5f 4f 4e 45 29 20 70 61 67 65 2d 6e 75 6d GE_ONE) page-num
19233 62 65 72 73 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 bers,.** dependi
19234 6e 67 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e ng on the conten
19235 74 73 20 6f 66 20 74 68 65 20 57 41 4c 20 66 69 ts of the WAL fi
19236 6c 65 2e 20 54 68 69 73 20 64 6f 65 73 20 6e 6f le. This does no
19237 74 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20 t change the.**
19238 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f allocated size o
19239 66 20 74 68 65 20 70 61 67 65 2d 6d 61 70 70 69 f the page-mappi
1923a 6e 67 20 61 72 72 61 79 20 2d 20 74 68 65 20 70 ng array - the p
1923b 61 67 65 2d 6d 61 70 70 69 6e 67 20 61 72 72 61 age-mapping arra
1923c 79 20 6d 65 72 65 6c 79 0a 2a 2a 20 63 6f 6e 74 y merely.** cont
1923d 61 69 6e 73 20 75 6e 75 73 65 64 20 65 6e 74 72 ains unused entr
1923e 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 6e 20 ies..**.** Even
1923f 77 69 74 68 6f 75 74 20 75 73 69 6e 67 20 74 68 without using th
19240 65 20 68 61 73 68 20 74 61 62 6c 65 2c 20 74 68 e hash table, th
19241 65 20 6c 61 73 74 20 66 72 61 6d 65 20 66 6f 72 e last frame for
19242 20 70 61 67 65 20 50 0a 2a 2a 20 63 61 6e 20 62 page P.** can b
19243 65 20 66 6f 75 6e 64 20 62 79 20 73 63 61 6e 6e e found by scann
19244 69 6e 67 20 74 68 65 20 70 61 67 65 2d 6d 61 70 ing the page-map
19245 70 69 6e 67 20 73 65 63 74 69 6f 6e 73 20 6f 66 ping sections of
19246 20 65 61 63 68 20 69 6e 64 65 78 20 62 6c 6f 63 each index bloc
19247 6b 0a 2a 2a 20 73 74 61 72 74 69 6e 67 20 77 69 k.** starting wi
19248 74 68 20 74 68 65 20 6c 61 73 74 20 69 6e 64 65 th the last inde
19249 78 20 62 6c 6f 63 6b 20 61 6e 64 20 6d 6f 76 69 x block and movi
1924a 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20 66 69 ng toward the fi
1924b 72 73 74 2c 20 61 6e 64 0a 2a 2a 20 77 69 74 68 rst, and.** with
1924c 69 6e 20 65 61 63 68 20 69 6e 64 65 78 20 62 6c in each index bl
1924d 6f 63 6b 2c 20 73 74 61 72 74 69 6e 67 20 61 74 ock, starting at
1924e 20 74 68 65 20 65 6e 64 20 61 6e 64 20 6d 6f 76 the end and mov
1924f 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65 0a 2a ing toward the.*
19250 2a 20 62 65 67 69 6e 6e 69 6e 67 2e 20 20 54 68 * beginning. Th
19251 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 74 68 e first entry th
19252 61 74 20 65 71 75 61 6c 73 20 50 20 63 6f 72 72 at equals P corr
19253 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 66 esponds to the f
19254 72 61 6d 65 0a 2a 2a 20 68 6f 6c 64 69 6e 67 20 rame.** holding
19255 74 68 65 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 the content for
19256 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a that page..**.**
19257 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 The hash table
19258 63 6f 6e 73 69 73 74 73 20 6f 66 20 48 41 53 48 consists of HASH
19259 54 41 42 4c 45 5f 4e 53 4c 4f 54 20 31 36 2d 62 TABLE_NSLOT 16-b
1925a 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 it unsigned inte
1925b 67 65 72 73 2e 0a 2a 2a 20 48 41 53 48 54 41 42 gers..** HASHTAB
1925c 4c 45 5f 4e 53 4c 4f 54 20 3d 20 32 2a 48 41 53 LE_NSLOT = 2*HAS
1925d 48 54 41 42 4c 45 5f 4e 50 41 47 45 2c 20 61 6e HTABLE_NPAGE, an
1925e 64 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 65 d there is one e
1925f 6e 74 72 79 20 69 6e 20 74 68 65 0a 2a 2a 20 68 ntry in the.** h
19260 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 65 61 ash table for ea
19261 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 ch page number i
19262 6e 20 74 68 65 20 6d 61 70 70 69 6e 67 20 73 65 n the mapping se
19263 63 74 69 6f 6e 2c 20 73 6f 20 74 68 65 20 68 61 ction, so the ha
19264 73 68 20 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 sh .** table is
19265 6e 65 76 65 72 20 6d 6f 72 65 20 74 68 61 6e 20 never more than
19266 68 61 6c 66 20 66 75 6c 6c 2e 20 20 54 68 65 20 half full. The
19267 65 78 70 65 63 74 65 64 20 6e 75 6d 62 65 72 20 expected number
19268 6f 66 20 63 6f 6c 6c 69 73 69 6f 6e 73 20 0a 2a of collisions .*
19269 2a 20 70 72 69 6f 72 20 74 6f 20 66 69 6e 64 69 * prior to findi
1926a 6e 67 20 61 20 6d 61 74 63 68 20 69 73 20 31 2e ng a match is 1.
1926b 20 20 45 61 63 68 20 65 6e 74 72 79 20 6f 66 20 Each entry of
1926c 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 the hash table i
1926d 73 20 61 6e 0a 2a 2a 20 31 2d 62 61 73 65 64 20 s an.** 1-based
1926e 69 6e 64 65 78 20 6f 66 20 61 6e 20 65 6e 74 72 index of an entr
1926f 79 20 69 6e 20 74 68 65 20 6d 61 70 70 69 6e 67 y in the mapping
19270 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 section of the
19271 73 61 6d 65 0a 2a 2a 20 69 6e 64 65 78 20 62 6c same.** index bl
19272 6f 63 6b 2e 20 20 20 4c 65 74 20 4b 20 62 65 20 ock. Let K be
19273 74 68 65 20 31 2d 62 61 73 65 64 20 69 6e 64 65 the 1-based inde
19274 78 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 x of the largest
19275 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 74 68 65 entry in.** the
19276 20 6d 61 70 70 69 6e 67 20 73 65 63 74 69 6f 6e mapping section
19277 2e 20 20 28 46 6f 72 20 69 6e 64 65 78 20 62 6c . (For index bl
19278 6f 63 6b 73 20 6f 74 68 65 72 20 74 68 61 6e 20 ocks other than
19279 74 68 65 20 6c 61 73 74 2c 20 4b 20 77 69 6c 6c the last, K will
1927a 0a 2a 2a 20 61 6c 77 61 79 73 20 62 65 20 65 78 .** always be ex
1927b 61 63 74 6c 79 20 48 41 53 48 54 41 42 4c 45 5f actly HASHTABLE_
1927c 4e 50 41 47 45 20 28 34 30 39 36 29 20 61 6e 64 NPAGE (4096) and
1927d 20 66 6f 72 20 74 68 65 20 6c 61 73 74 20 69 6e for the last in
1927e 64 65 78 20 62 6c 6f 63 6b 0a 2a 2a 20 4b 20 77 dex block.** K w
1927f 69 6c 6c 20 62 65 20 28 6d 78 46 72 61 6d 65 25 ill be (mxFrame%
19280 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 29 HASHTABLE_NPAGE)
19281 2e 29 20 20 55 6e 75 73 65 64 20 73 6c 6f 74 73 .) Unused slots
19282 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 62 of the hash tab
19283 6c 65 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 20 le.** contain a
19284 76 61 6c 75 65 20 6f 66 20 30 2e 0a 2a 2a 0a 2a value of 0..**.*
19285 2a 20 54 6f 20 6c 6f 6f 6b 20 66 6f 72 20 70 61 * To look for pa
19286 67 65 20 50 20 69 6e 20 74 68 65 20 68 61 73 68 ge P in the hash
19287 20 74 61 62 6c 65 2c 20 66 69 72 73 74 20 63 6f table, first co
19288 6d 70 75 74 65 20 61 20 68 61 73 68 20 69 4b 65 mpute a hash iKe
19289 79 20 6f 6e 0a 2a 2a 20 50 20 61 73 20 66 6f 6c y on.** P as fol
1928a 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 lows:.**.**
1928b 20 69 4b 65 79 20 3d 20 28 50 20 2a 20 33 38 33 iKey = (P * 383
1928c 29 20 25 20 48 41 53 48 54 41 42 4c 45 5f 4e 53 ) % HASHTABLE_NS
1928d 4c 4f 54 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 73 LOT.**.** Then s
1928e 74 61 72 74 20 73 63 61 6e 6e 69 6e 67 20 65 6e tart scanning en
1928f 74 72 69 65 73 20 6f 66 20 74 68 65 20 68 61 73 tries of the has
19290 68 20 74 61 62 6c 65 2c 20 73 74 61 72 74 69 6e h table, startin
19291 67 20 77 69 74 68 20 69 4b 65 79 0a 2a 2a 20 28 g with iKey.** (
19292 77 72 61 70 70 69 6e 67 20 61 72 6f 75 6e 64 20 wrapping around
19293 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 to the beginning
19294 20 77 68 65 6e 20 74 68 65 20 65 6e 64 20 6f 66 when the end of
19295 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
19296 69 73 0a 2a 2a 20 72 65 61 63 68 65 64 29 20 75 is.** reached) u
19297 6e 74 69 6c 20 61 6e 20 75 6e 75 73 65 64 20 68 ntil an unused h
19298 61 73 68 20 73 6c 6f 74 20 69 73 20 66 6f 75 6e ash slot is foun
19299 64 2e 20 4c 65 74 20 74 68 65 20 66 69 72 73 74 d. Let the first
1929a 20 75 6e 75 73 65 64 20 73 6c 6f 74 0a 2a 2a 20 unused slot.**
1929b 62 65 20 61 74 20 69 6e 64 65 78 20 69 55 6e 75 be at index iUnu
1929c 73 65 64 2e 20 20 28 69 55 6e 75 73 65 64 20 6d sed. (iUnused m
1929d 69 67 68 74 20 62 65 20 6c 65 73 73 20 74 68 61 ight be less tha
1929e 6e 20 69 4b 65 79 20 69 66 20 74 68 65 72 65 20 n iKey if there
1929f 77 61 73 0a 2a 2a 20 77 72 61 70 2d 61 72 6f 75 was.** wrap-arou
192a0 6e 64 2e 29 20 42 65 63 61 75 73 65 20 74 68 65 nd.) Because the
192a1 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 6e hash table is n
192a2 65 76 65 72 20 6d 6f 72 65 20 74 68 61 6e 20 68 ever more than h
192a3 61 6c 66 20 66 75 6c 6c 2c 0a 2a 2a 20 74 68 65 alf full,.** the
192a4 20 73 65 61 72 63 68 20 69 73 20 67 75 61 72 61 search is guara
192a5 6e 74 65 65 64 20 74 6f 20 65 76 65 6e 74 75 61 nteed to eventua
192a6 6c 6c 79 20 68 69 74 20 61 6e 20 75 6e 75 73 65 lly hit an unuse
192a7 64 20 65 6e 74 72 79 2e 20 20 4c 65 74 20 0a 2a d entry. Let .*
192a8 2a 20 69 4d 61 78 20 62 65 20 74 68 65 20 76 61 * iMax be the va
192a9 6c 75 65 20 62 65 74 77 65 65 6e 20 69 4b 65 79 lue between iKey
192aa 20 61 6e 64 20 69 55 6e 75 73 65 64 2c 20 63 6c and iUnused, cl
192ab 6f 73 65 73 74 20 74 6f 20 69 55 6e 75 73 65 64 osest to iUnused
192ac 2c 0a 2a 2a 20 77 68 65 72 65 20 61 48 61 73 68 ,.** where aHash
192ad 5b 69 4d 61 78 5d 3d 3d 50 2e 20 20 49 66 20 74 [iMax]==P. If t
192ae 68 65 72 65 20 69 73 20 6e 6f 20 69 4d 61 78 20 here is no iMax
192af 65 6e 74 72 79 20 28 69 66 20 74 68 65 72 65 20 entry (if there
192b0 65 78 69 73 74 73 0a 2a 2a 20 6e 6f 20 68 61 73 exists.** no has
192b1 68 20 73 6c 6f 74 20 73 75 63 68 20 74 68 61 74 h slot such that
192b2 20 61 48 61 73 68 5b 69 5d 3d 3d 70 29 20 74 68 aHash[i]==p) th
192b3 65 6e 20 70 61 67 65 20 50 20 69 73 20 6e 6f 74 en page P is not
192b4 20 69 6e 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 in the.** curre
192b5 6e 74 20 69 6e 64 65 78 20 62 6c 6f 63 6b 2e 20 nt index block.
192b6 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 69 Otherwise the i
192b7 4d 61 78 2d 74 68 20 6d 61 70 70 69 6e 67 20 65 Max-th mapping e
192b8 6e 74 72 79 20 6f 66 20 74 68 65 0a 2a 2a 20 63 ntry of the.** c
192b9 75 72 72 65 6e 74 20 69 6e 64 65 78 20 62 6c 6f urrent index blo
192ba 63 6b 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 ck corresponds t
192bb 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 o the last entry
192bc 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 65 73 that references
192bd 20 0a 2a 2a 20 70 61 67 65 20 50 2e 0a 2a 2a 0a .** page P..**.
192be 2a 2a 20 41 20 68 61 73 68 20 73 65 61 72 63 68 ** A hash search
192bf 20 62 65 67 69 6e 73 20 77 69 74 68 20 74 68 65 begins with the
192c0 20 6c 61 73 74 20 69 6e 64 65 78 20 62 6c 6f 63 last index bloc
192c1 6b 20 61 6e 64 20 6d 6f 76 65 73 20 74 6f 77 61 k and moves towa
192c2 72 64 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 rd the.** first
192c3 69 6e 64 65 78 20 62 6c 6f 63 6b 2c 20 6c 6f 6f index block, loo
192c4 6b 69 6e 67 20 66 6f 72 20 65 6e 74 72 69 65 73 king for entries
192c5 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
192c6 6f 20 70 61 67 65 20 50 2e 20 20 4f 6e 0a 2a 2a o page P. On.**
192c7 20 61 76 65 72 61 67 65 2c 20 6f 6e 6c 79 20 74 average, only t
192c8 77 6f 20 6f 72 20 74 68 72 65 65 20 73 6c 6f 74 wo or three slot
192c9 73 20 69 6e 20 65 61 63 68 20 69 6e 64 65 78 20 s in each index
192ca 62 6c 6f 63 6b 20 6e 65 65 64 20 74 6f 20 62 65 block need to be
192cb 0a 2a 2a 20 65 78 61 6d 69 6e 65 64 20 69 6e 20 .** examined in
192cc 6f 72 64 65 72 20 74 6f 20 65 69 74 68 65 72 20 order to either
192cd 66 69 6e 64 20 74 68 65 20 6c 61 73 74 20 65 6e find the last en
192ce 74 72 79 20 66 6f 72 20 70 61 67 65 20 50 2c 20 try for page P,
192cf 6f 72 20 74 6f 0a 2a 2a 20 65 73 74 61 62 6c 69 or to.** establi
192d0 73 68 20 74 68 61 74 20 6e 6f 20 73 75 63 68 20 sh that no such
192d1 65 6e 74 72 79 20 65 78 69 73 74 73 20 69 6e 20 entry exists in
192d2 74 68 65 20 62 6c 6f 63 6b 2e 20 20 45 61 63 68 the block. Each
192d3 20 69 6e 64 65 78 20 62 6c 6f 63 6b 0a 2a 2a 20 index block.**
192d4 68 6f 6c 64 73 20 6f 76 65 72 20 34 30 30 30 20 holds over 4000
192d5 65 6e 74 72 69 65 73 2e 20 20 53 6f 20 74 77 6f entries. So two
192d6 20 6f 72 20 74 68 72 65 65 20 69 6e 64 65 78 20 or three index
192d7 62 6c 6f 63 6b 73 20 61 72 65 20 73 75 66 66 69 blocks are suffi
192d8 63 69 65 6e 74 0a 2a 2a 20 74 6f 20 63 6f 76 65 cient.** to cove
192d9 72 20 61 20 74 79 70 69 63 61 6c 20 31 30 20 6d r a typical 10 m
192da 65 67 61 62 79 74 65 20 57 41 4c 20 66 69 6c 65 egabyte WAL file
192db 2c 20 61 73 73 75 6d 69 6e 67 20 31 4b 20 70 61 , assuming 1K pa
192dc 67 65 73 2e 20 20 38 20 6f 72 20 31 30 0a 2a 2a ges. 8 or 10.**
192dd 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20 28 6f 6e comparisons (on
192de 20 61 76 65 72 61 67 65 29 20 73 75 66 66 69 63 average) suffic
192df 65 20 74 6f 20 65 69 74 68 65 72 20 6c 6f 63 61 e to either loca
192e0 74 65 20 61 20 66 72 61 6d 65 20 69 6e 20 74 68 te a frame in th
192e1 65 0a 2a 2a 20 57 41 4c 20 6f 72 20 74 6f 20 65 e.** WAL or to e
192e2 73 74 61 62 6c 69 73 68 20 74 68 61 74 20 74 68 stablish that th
192e3 65 20 66 72 61 6d 65 20 64 6f 65 73 20 6e 6f 74 e frame does not
192e4 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 57 41 exist in the WA
192e5 4c 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 6d L. This.** is m
192e6 75 63 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 uch faster than
192e7 73 63 61 6e 6e 69 6e 67 20 74 68 65 20 65 6e 74 scanning the ent
192e8 69 72 65 20 31 30 4d 42 20 57 41 4c 2e 0a 2a 2a ire 10MB WAL..**
192e9 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 65 6e .** Note that en
192ea 74 72 69 65 73 20 61 72 65 20 61 64 64 65 64 20 tries are added
192eb 69 6e 20 6f 72 64 65 72 20 6f 66 20 69 6e 63 72 in order of incr
192ec 65 61 73 69 6e 67 20 4b 2e 20 20 48 65 6e 63 65 easing K. Hence
192ed 2c 20 6f 6e 65 0a 2a 2a 20 72 65 61 64 65 72 20 , one.** reader
192ee 6d 69 67 68 74 20 62 65 20 75 73 69 6e 67 20 73 might be using s
192ef 6f 6d 65 20 76 61 6c 75 65 20 4b 30 20 61 6e 64 ome value K0 and
192f0 20 61 20 73 65 63 6f 6e 64 20 72 65 61 64 65 72 a second reader
192f1 20 74 68 61 74 20 73 74 61 72 74 65 64 0a 2a 2a that started.**
192f2 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 at a later time
192f3 20 28 61 66 74 65 72 20 61 64 64 69 74 69 6f 6e (after addition
192f4 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 al transactions
192f5 77 65 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 were added to th
192f6 65 20 57 41 4c 0a 2a 2a 20 61 6e 64 20 74 6f 20 e WAL.** and to
192f7 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 29 20 6d the wal-index) m
192f8 69 67 68 74 20 62 65 20 75 73 69 6e 67 20 61 20 ight be using a
192f9 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 20 different value
192fa 4b 31 2c 20 77 68 65 72 65 20 4b 31 3e 4b 30 2e K1, where K1>K0.
192fb 0a 2a 2a 20 42 6f 74 68 20 72 65 61 64 65 72 73 .** Both readers
192fc 20 63 61 6e 20 75 73 65 20 74 68 65 20 73 61 6d can use the sam
192fd 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64 e hash table and
192fe 20 6d 61 70 70 69 6e 67 20 73 65 63 74 69 6f 6e mapping section
192ff 20 74 6f 20 67 65 74 0a 2a 2a 20 74 68 65 20 63 to get.** the c
19300 6f 72 72 65 63 74 20 72 65 73 75 6c 74 2e 20 20 orrect result.
19301 54 68 65 72 65 20 6d 61 79 20 62 65 20 65 6e 74 There may be ent
19302 72 69 65 73 20 69 6e 20 74 68 65 20 68 61 73 68 ries in the hash
19303 20 74 61 62 6c 65 20 77 69 74 68 0a 2a 2a 20 4b table with.** K
19304 3e 4b 30 20 62 75 74 20 74 6f 20 74 68 65 20 66 >K0 but to the f
19305 69 72 73 74 20 72 65 61 64 65 72 2c 20 74 68 6f irst reader, tho
19306 73 65 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 se entries will
19307 61 70 70 65 61 72 20 74 6f 20 62 65 20 75 6e 75 appear to be unu
19308 73 65 64 0a 2a 2a 20 73 6c 6f 74 73 20 69 6e 20 sed.** slots in
19309 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 the hash table a
1930a 6e 64 20 73 6f 20 74 68 65 20 66 69 72 73 74 20 nd so the first
1930b 72 65 61 64 65 72 20 77 69 6c 6c 20 67 65 74 20 reader will get
1930c 61 6e 20 61 6e 73 77 65 72 20 61 73 0a 2a 2a 20 an answer as.**
1930d 69 66 20 6e 6f 20 76 61 6c 75 65 73 20 67 72 65 if no values gre
1930e 61 74 65 72 20 74 68 61 6e 20 4b 30 20 68 61 64 ater than K0 had
1930f 20 65 76 65 72 20 62 65 65 6e 20 69 6e 73 65 72 ever been inser
19310 74 65 64 20 69 6e 74 6f 20 74 68 65 20 68 61 73 ted into the has
19311 68 20 74 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 h table.** in th
19312 65 20 66 69 72 73 74 20 70 6c 61 63 65 20 2d 20 e first place -
19313 77 68 69 63 68 20 69 73 20 77 68 61 74 20 72 65 which is what re
19314 61 64 65 72 20 6f 6e 65 20 77 61 6e 74 73 2e 20 ader one wants.
19315 20 4d 65 61 6e 77 68 69 6c 65 2c 20 74 68 65 0a Meanwhile, the.
19316 2a 2a 20 73 65 63 6f 6e 64 20 72 65 61 64 65 72 ** second reader
19317 20 75 73 69 6e 67 20 4b 31 20 77 69 6c 6c 20 73 using K1 will s
19318 65 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 76 61 ee additional va
19319 6c 75 65 73 20 74 68 61 74 20 77 65 72 65 20 69 lues that were i
1931a 6e 73 65 72 74 65 64 0a 2a 2a 20 6c 61 74 65 72 nserted.** later
1931b 2c 20 77 68 69 63 68 20 69 73 20 65 78 61 63 74 , which is exact
1931c 6c 79 20 77 68 61 74 20 72 65 61 64 65 72 20 74 ly what reader t
1931d 77 6f 20 77 61 6e 74 73 2e 20 20 0a 2a 2a 0a 2a wo wants. .**.*
1931e 2a 20 57 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 * When a rollbac
1931f 6b 20 6f 63 63 75 72 73 2c 20 74 68 65 20 76 61 k occurs, the va
19320 6c 75 65 20 6f 66 20 4b 20 69 73 20 64 65 63 72 lue of K is decr
19321 65 61 73 65 64 2e 20 48 61 73 68 20 74 61 62 6c eased. Hash tabl
19322 65 20 65 6e 74 72 69 65 73 0a 2a 2a 20 74 68 61 e entries.** tha
19323 74 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 t correspond to
19324 66 72 61 6d 65 73 20 67 72 65 61 74 65 72 20 74 frames greater t
19325 68 61 6e 20 74 68 65 20 6e 65 77 20 4b 20 76 61 han the new K va
19326 6c 75 65 20 61 72 65 20 72 65 6d 6f 76 65 64 0a lue are removed.
19327 2a 2a 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 ** from the hash
19328 20 74 61 62 6c 65 20 61 74 20 74 68 69 73 20 70 table at this p
19329 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 oint..*/.#ifndef
1932a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c SQLITE_OMIT_WAL
1932b 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 63 65 20 6f .../*.** Trace o
1932c 75 74 70 75 74 20 6d 61 63 72 6f 73 0a 2a 2f 0a utput macros.*/.
1932d 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
1932e 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66 69 TE_TEST) && defi
1932f 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
19330 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
19331 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 54 int sqlite3WalT
19332 72 61 63 65 20 3d 20 30 3b 0a 23 20 64 65 66 69 race = 0;.# defi
19333 6e 65 20 57 41 4c 54 52 41 43 45 28 58 29 20 20 ne WALTRACE(X)
19334 69 66 28 73 71 6c 69 74 65 33 57 61 6c 54 72 61 if(sqlite3WalTra
19335 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 ce) sqlite3Debug
19336 50 72 69 6e 74 66 20 58 0a 23 65 6c 73 65 0a 23 Printf X.#else.#
19337 20 64 65 66 69 6e 65 20 57 41 4c 54 52 41 43 45 define WALTRACE
19338 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a (X).#endif../*.*
19339 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 28 61 * The maximum (a
1933a 6e 64 20 6f 6e 6c 79 29 20 76 65 72 73 69 6f 6e nd only) version
1933b 73 20 6f 66 20 74 68 65 20 77 61 6c 20 61 6e 64 s of the wal and
1933c 20 77 61 6c 2d 69 6e 64 65 78 20 66 6f 72 6d 61 wal-index forma
1933d 74 73 0a 2a 2a 20 74 68 61 74 20 6d 61 79 20 62 ts.** that may b
1933e 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 62 79 e interpreted by
1933f 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 this version of
19340 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 SQLite..**.** I
19341 66 20 61 20 63 6c 69 65 6e 74 20 62 65 67 69 6e f a client begin
19342 73 20 72 65 63 6f 76 65 72 69 6e 67 20 61 20 57 s recovering a W
19343 41 4c 20 66 69 6c 65 20 61 6e 64 20 66 69 6e 64 AL file and find
19344 73 20 74 68 61 74 20 28 61 29 20 74 68 65 20 63 s that (a) the c
19345 68 65 63 6b 73 75 6d 0a 2a 2a 20 76 61 6c 75 65 hecksum.** value
19346 73 20 69 6e 20 74 68 65 20 77 61 6c 2d 68 65 61 s in the wal-hea
19347 64 65 72 20 61 72 65 20 63 6f 72 72 65 63 74 20 der are correct
19348 61 6e 64 20 28 62 29 20 74 68 65 20 76 65 72 73 and (b) the vers
19349 69 6f 6e 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 ion field is not
1934a 0a 2a 2a 20 57 41 4c 5f 4d 41 58 5f 56 45 52 53 .** WAL_MAX_VERS
1934b 49 4f 4e 2c 20 72 65 63 6f 76 65 72 79 20 66 61 ION, recovery fa
1934c 69 6c 73 20 61 6e 64 20 53 51 4c 69 74 65 20 72 ils and SQLite r
1934d 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 43 41 eturns SQLITE_CA
1934e 4e 54 4f 50 45 4e 2e 0a 2a 2a 0a 2a 2a 20 53 69 NTOPEN..**.** Si
1934f 6d 69 6c 61 72 6c 79 2c 20 69 66 20 61 20 63 6c milarly, if a cl
19350 69 65 6e 74 20 73 75 63 63 65 73 73 66 75 6c 6c ient successfull
19351 79 20 72 65 61 64 73 20 61 20 77 61 6c 2d 69 6e y reads a wal-in
19352 64 65 78 20 68 65 61 64 65 72 20 28 69 2e 65 2e dex header (i.e.
19353 20 74 68 65 20 0a 2a 2a 20 63 68 65 63 6b 73 75 the .** checksu
19354 6d 20 74 65 73 74 20 69 73 20 73 75 63 63 65 73 m test is succes
19355 73 66 75 6c 29 20 61 6e 64 20 66 69 6e 64 73 20 sful) and finds
19356 74 68 61 74 20 74 68 65 20 76 65 72 73 69 6f 6e that the version
19357 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 0a 2a 2a field is not.**
19358 20 57 41 4c 49 4e 44 45 58 5f 4d 41 58 5f 56 45 WALINDEX_MAX_VE
19359 52 53 49 4f 4e 2c 20 74 68 65 6e 20 6e 6f 20 72 RSION, then no r
1935a 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ead-transaction
1935b 69 73 20 6f 70 65 6e 65 64 20 61 6e 64 20 53 51 is opened and SQ
1935c 4c 69 74 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 Lite.** returns
1935d 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 2e SQLITE_CANTOPEN.
1935e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f .*/.#define WAL_
1935f 4d 41 58 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 MAX_VERSION
19360 20 33 30 30 37 30 30 30 0a 23 64 65 66 69 6e 65 3007000.#define
19361 20 57 41 4c 49 4e 44 45 58 5f 4d 41 58 5f 56 45 WALINDEX_MAX_VE
19362 52 53 49 4f 4e 20 33 30 30 37 30 30 30 0a 0a 2f RSION 3007000../
19363 2a 0a 2a 2a 20 49 6e 64 69 63 65 73 20 6f 66 20 *.** Indices of
19364 76 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 various locking
19365 62 79 74 65 73 2e 20 20 20 57 41 4c 5f 4e 52 45 bytes. WAL_NRE
19366 41 44 45 52 20 69 73 20 74 68 65 20 6e 75 6d 62 ADER is the numb
19367 65 72 0a 2a 2a 20 6f 66 20 61 76 61 69 6c 61 62 er.** of availab
19368 6c 65 20 72 65 61 64 65 72 20 6c 6f 63 6b 73 20 le reader locks
19369 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 61 74 and should be at
1936a 20 6c 65 61 73 74 20 33 2e 0a 2a 2f 0a 23 64 65 least 3..*/.#de
1936b 66 69 6e 65 20 57 41 4c 5f 57 52 49 54 45 5f 4c fine WAL_WRITE_L
1936c 4f 43 4b 20 20 20 20 20 20 20 20 20 30 0a 23 64 OCK 0.#d
1936d 65 66 69 6e 65 20 57 41 4c 5f 41 4c 4c 5f 42 55 efine WAL_ALL_BU
1936e 54 5f 57 52 49 54 45 20 20 20 20 20 20 31 0a 23 T_WRITE 1.#
1936f 64 65 66 69 6e 65 20 57 41 4c 5f 43 4b 50 54 5f define WAL_CKPT_
19370 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 31 0a LOCK 1.
19371 23 64 65 66 69 6e 65 20 57 41 4c 5f 52 45 43 4f #define WAL_RECO
19372 56 45 52 5f 4c 4f 43 4b 20 20 20 20 20 20 20 32 VER_LOCK 2
19373 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 52 45 41 .#define WAL_REA
19374 44 5f 4c 4f 43 4b 28 49 29 20 20 20 20 20 20 20 D_LOCK(I)
19375 28 33 2b 28 49 29 29 0a 23 64 65 66 69 6e 65 20 (3+(I)).#define
19376 57 41 4c 5f 4e 52 45 41 44 45 52 20 20 20 20 20 WAL_NREADER
19377 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 53 (SQLITE_S
19378 48 4d 5f 4e 4c 4f 43 4b 2d 33 29 0a 0a 0a 2f 2a HM_NLOCK-3).../*
19379 20 4f 62 6a 65 63 74 20 64 65 63 6c 61 72 61 74 Object declarat
1937a 69 6f 6e 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 ions */.typedef
1937b 73 74 72 75 63 74 20 57 61 6c 49 6e 64 65 78 48 struct WalIndexH
1937c 64 72 20 57 61 6c 49 6e 64 65 78 48 64 72 3b 0a dr WalIndexHdr;.
1937d 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 typedef struct W
1937e 61 6c 49 74 65 72 61 74 6f 72 20 57 61 6c 49 74 alIterator WalIt
1937f 65 72 61 74 6f 72 3b 0a 74 79 70 65 64 65 66 20 erator;.typedef
19380 73 74 72 75 63 74 20 57 61 6c 43 6b 70 74 49 6e struct WalCkptIn
19381 66 6f 20 57 61 6c 43 6b 70 74 49 6e 66 6f 3b 0a fo WalCkptInfo;.
19382 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
19383 6f 77 69 6e 67 20 6f 62 6a 65 63 74 20 68 6f 6c owing object hol
19384 64 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ds a copy of the
19385 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 wal-index heade
19386 72 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a r content..**.**
19387 20 54 68 65 20 61 63 74 75 61 6c 20 68 65 61 64 The actual head
19388 65 72 20 69 6e 20 74 68 65 20 77 61 6c 2d 69 6e er in the wal-in
19389 64 65 78 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 dex consists of
1938a 74 77 6f 20 63 6f 70 69 65 73 20 6f 66 20 74 68 two copies of th
1938b 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 2e 0a 2a 2a is.** object..**
1938c 0a 2a 2a 20 54 68 65 20 73 7a 50 61 67 65 20 76 .** The szPage v
1938d 61 6c 75 65 20 63 61 6e 20 62 65 20 61 6e 79 20 alue can be any
1938e 70 6f 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 power of 2 betwe
1938f 65 6e 20 35 31 32 20 61 6e 64 20 33 32 37 36 38 en 512 and 32768
19390 2c 20 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a 20 , inclusive..**
19391 4f 72 20 69 74 20 63 61 6e 20 62 65 20 31 20 74 Or it can be 1 t
19392 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20 36 35 o represent a 65
19393 35 33 36 2d 62 79 74 65 20 70 61 67 65 2e 20 20 536-byte page.
19394 54 68 65 20 6c 61 74 74 65 72 20 63 61 73 65 20 The latter case
19395 77 61 73 0a 2a 2a 20 61 64 64 65 64 20 69 6e 20 was.** added in
19396 33 2e 37 2e 31 20 77 68 65 6e 20 73 75 70 70 6f 3.7.1 when suppo
19397 72 74 20 66 6f 72 20 36 34 4b 20 70 61 67 65 73 rt for 64K pages
19398 20 77 61 73 20 61 64 64 65 64 2e 20 20 0a 2a 2f was added. .*/
19399 0a 73 74 72 75 63 74 20 57 61 6c 49 6e 64 65 78 .struct WalIndex
1939a 48 64 72 20 7b 0a 20 20 75 33 32 20 69 56 65 72 Hdr {. u32 iVer
1939b 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 sion;
1939c 20 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 2d 69 /* Wal-i
1939d 6e 64 65 78 20 76 65 72 73 69 6f 6e 20 2a 2f 0a ndex version */.
1939e 20 20 75 33 32 20 75 6e 75 73 65 64 3b 20 20 20 u32 unused;
1939f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
193a0 20 20 2f 2a 20 55 6e 75 73 65 64 20 28 70 61 64 /* Unused (pad
193a1 64 69 6e 67 29 20 66 69 65 6c 64 20 2a 2f 0a 20 ding) field */.
193a2 20 75 33 32 20 69 43 68 61 6e 67 65 3b 20 20 20 u32 iChange;
193a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
193a4 20 2f 2a 20 43 6f 75 6e 74 65 72 20 69 6e 63 72 /* Counter incr
193a5 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74 72 61 emented each tra
193a6 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 nsaction */. u8
193a7 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 isInit;
193a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
193a9 20 31 20 77 68 65 6e 20 69 6e 69 74 69 61 6c 69 1 when initiali
193aa 7a 65 64 20 2a 2f 0a 20 20 75 38 20 62 69 67 45 zed */. u8 bigE
193ab 6e 64 43 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 ndCksum;
193ac 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
193ad 20 69 66 20 63 68 65 63 6b 73 75 6d 73 20 69 6e if checksums in
193ae 20 57 41 4c 20 61 72 65 20 62 69 67 2d 65 6e 64 WAL are big-end
193af 69 61 6e 20 2a 2f 0a 20 20 75 31 36 20 73 7a 50 ian */. u16 szP
193b0 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
193b1 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
193b2 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 20 69 base page size i
193b3 6e 20 62 79 74 65 73 2e 20 31 3d 3d 36 34 4b 20 n bytes. 1==64K
193b4 2a 2f 0a 20 20 75 33 32 20 6d 78 46 72 61 6d 65 */. u32 mxFrame
193b5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
193b6 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
193b7 20 6c 61 73 74 20 76 61 6c 69 64 20 66 72 61 6d last valid fram
193b8 65 20 69 6e 20 74 68 65 20 57 41 4c 20 2a 2f 0a e in the WAL */.
193b9 20 20 75 33 32 20 6e 50 61 67 65 3b 20 20 20 20 u32 nPage;
193ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
193bb 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 61 74 /* Size of dat
193bc 61 62 61 73 65 20 69 6e 20 70 61 67 65 73 20 2a abase in pages *
193bd 2f 0a 20 20 75 33 32 20 61 46 72 61 6d 65 43 6b /. u32 aFrameCk
193be 73 75 6d 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 sum[2];
193bf 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 /* Checksum
193c0 6f 66 20 6c 61 73 74 20 66 72 61 6d 65 20 69 6e of last frame in
193c1 20 6c 6f 67 20 2a 2f 0a 20 20 75 33 32 20 61 53 log */. u32 aS
193c2 61 6c 74 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 alt[2];
193c3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 77 6f /* Two
193c4 20 73 61 6c 74 20 76 61 6c 75 65 73 20 63 6f 70 salt values cop
193c5 69 65 64 20 66 72 6f 6d 20 57 41 4c 20 68 65 61 ied from WAL hea
193c6 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 61 43 6b der */. u32 aCk
193c7 73 75 6d 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 sum[2];
193c8 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 /* Chec
193c9 6b 73 75 6d 20 6f 76 65 72 20 61 6c 6c 20 70 72 ksum over all pr
193ca 69 6f 72 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b ior fields */.};
193cb 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 70 79 20 6f ../*.** A copy o
193cc 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
193cd 6f 62 6a 65 63 74 20 6f 63 63 75 72 73 20 69 6e object occurs in
193ce 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 69 the wal-index i
193cf 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 66 6f mmediately.** fo
193d0 6c 6c 6f 77 69 6e 67 20 74 68 65 20 73 65 63 6f llowing the seco
193d1 6e 64 20 63 6f 70 79 20 6f 66 20 74 68 65 20 57 nd copy of the W
193d2 61 6c 49 6e 64 65 78 48 64 72 2e 20 20 54 68 69 alIndexHdr. Thi
193d3 73 20 6f 62 6a 65 63 74 20 73 74 6f 72 65 73 0a s object stores.
193d4 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 ** information u
193d5 73 65 64 20 62 79 20 63 68 65 63 6b 70 6f 69 6e sed by checkpoin
193d6 74 2e 0a 2a 2a 0a 2a 2a 20 6e 42 61 63 6b 66 69 t..**.** nBackfi
193d7 6c 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ll is the number
193d8 20 6f 66 20 66 72 61 6d 65 73 20 69 6e 20 74 68 of frames in th
193d9 65 20 57 41 4c 20 74 68 61 74 20 68 61 76 65 20 e WAL that have
193da 62 65 65 6e 20 77 72 69 74 74 65 6e 0a 2a 2a 20 been written.**
193db 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 back into the da
193dc 74 61 62 61 73 65 2e 20 28 57 65 20 63 61 6c 6c tabase. (We call
193dd 20 74 68 65 20 61 63 74 20 6f 66 20 6d 6f 76 69 the act of movi
193de 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 ng content from
193df 57 41 4c 20 74 6f 0a 2a 2a 20 64 61 74 61 62 61 WAL to.** databa
193e0 73 65 20 22 62 61 63 6b 66 69 6c 6c 69 6e 67 22 se "backfilling"
193e1 2e 29 20 20 54 68 65 20 6e 42 61 63 6b 66 69 6c .) The nBackfil
193e2 6c 20 6e 75 6d 62 65 72 20 69 73 20 6e 65 76 65 l number is neve
193e3 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a r greater than.*
193e4 2a 20 57 61 6c 49 6e 64 65 78 48 64 72 2e 6d 78 * WalIndexHdr.mx
193e5 46 72 61 6d 65 2e 20 20 6e 42 61 63 6b 66 69 6c Frame. nBackfil
193e6 6c 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 69 6e l can only be in
193e7 63 72 65 61 73 65 64 20 62 79 20 74 68 72 65 61 creased by threa
193e8 64 73 0a 2a 2a 20 68 6f 6c 64 69 6e 67 20 74 68 ds.** holding th
193e9 65 20 57 41 4c 5f 43 4b 50 54 5f 4c 4f 43 4b 20 e WAL_CKPT_LOCK
193ea 6c 6f 63 6b 20 28 77 68 69 63 68 20 69 6e 63 6c lock (which incl
193eb 75 64 65 73 20 61 20 72 65 63 6f 76 65 72 79 20 udes a recovery
193ec 74 68 72 65 61 64 29 2e 0a 2a 2a 20 48 6f 77 65 thread)..** Howe
193ed 76 65 72 2c 20 61 20 57 41 4c 5f 57 52 49 54 45 ver, a WAL_WRITE
193ee 5f 4c 4f 43 4b 20 74 68 72 65 61 64 20 63 61 6e _LOCK thread can
193ef 20 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20 move the value
193f0 6f 66 20 6e 42 61 63 6b 66 69 6c 6c 20 66 72 6f of nBackfill fro
193f1 6d 0a 2a 2a 20 6d 78 46 72 61 6d 65 20 62 61 63 m.** mxFrame bac
193f2 6b 20 74 6f 20 7a 65 72 6f 20 77 68 65 6e 20 74 k to zero when t
193f3 68 65 20 57 41 4c 20 69 73 20 72 65 73 65 74 2e he WAL is reset.
193f4 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 .**.** There is
193f5 6f 6e 65 20 65 6e 74 72 79 20 69 6e 20 61 52 65 one entry in aRe
193f6 61 64 4d 61 72 6b 5b 5d 20 66 6f 72 20 65 61 63 adMark[] for eac
193f7 68 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 20 20 h reader lock.
193f8 49 66 20 61 20 72 65 61 64 65 72 0a 2a 2a 20 68 If a reader.** h
193f9 6f 6c 64 73 20 72 65 61 64 2d 6c 6f 63 6b 20 4b olds read-lock K
193fa 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 , then the value
193fb 20 69 6e 20 61 52 65 61 64 4d 61 72 6b 5b 4b 5d in aReadMark[K]
193fc 20 69 73 20 6e 6f 20 67 72 65 61 74 65 72 20 74 is no greater t
193fd 68 61 6e 0a 2a 2a 20 74 68 65 20 6d 78 46 72 61 han.** the mxFra
193fe 6d 65 20 66 6f 72 20 74 68 61 74 20 72 65 61 64 me for that read
193ff 65 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 52 er. The value R
19400 45 41 44 4d 41 52 4b 5f 4e 4f 54 5f 55 53 45 44 EADMARK_NOT_USED
19401 20 28 30 78 66 66 66 66 66 66 66 66 29 0a 2a 2a (0xffffffff).**
19402 20 66 6f 72 20 61 6e 79 20 61 52 65 61 64 4d 61 for any aReadMa
19403 72 6b 5b 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 rk[] means that
19404 65 6e 74 72 79 20 69 73 20 75 6e 75 73 65 64 2e entry is unused.
19405 20 20 61 52 65 61 64 4d 61 72 6b 5b 30 5d 20 69 aReadMark[0] i
19406 73 20 0a 2a 2a 20 61 20 73 70 65 63 69 61 6c 20 s .** a special
19407 63 61 73 65 3b 20 69 74 73 20 76 61 6c 75 65 20 case; its value
19408 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 61 6e is never used an
19409 64 20 69 74 20 65 78 69 73 74 73 20 61 73 20 61 d it exists as a
1940a 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 0a 2a 2a place-holder.**
1940b 20 74 6f 20 61 76 6f 69 64 20 68 61 76 69 6e 67 to avoid having
1940c 20 74 6f 20 6f 66 66 73 65 74 20 61 52 65 61 64 to offset aRead
1940d 4d 61 72 6b 5b 5d 20 69 6e 64 65 78 73 20 62 79 Mark[] indexs by
1940e 20 6f 6e 65 2e 20 20 52 65 61 64 65 72 73 20 68 one. Readers h
1940f 6f 6c 64 69 6e 67 0a 2a 2a 20 57 41 4c 5f 52 45 olding.** WAL_RE
19410 41 44 5f 4c 4f 43 4b 28 30 29 20 61 6c 77 61 79 AD_LOCK(0) alway
19411 73 20 69 67 6e 6f 72 65 20 74 68 65 20 65 6e 74 s ignore the ent
19412 69 72 65 20 57 41 4c 20 61 6e 64 20 72 65 61 64 ire WAL and read
19413 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 all content.**
19414 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 directly from th
19415 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a e database..**.*
19416 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 61 * The value of a
19417 52 65 61 64 4d 61 72 6b 5b 4b 5d 20 6d 61 79 20 ReadMark[K] may
19418 6f 6e 6c 79 20 62 65 20 63 68 61 6e 67 65 64 20 only be changed
19419 62 79 20 61 20 74 68 72 65 61 64 20 74 68 61 74 by a thread that
1941a 0a 2a 2a 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 .** is holding a
1941b 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
1941c 20 6f 6e 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 on WAL_READ_LOC
1941d 4b 28 4b 29 2e 20 20 54 68 75 73 2c 20 74 68 65 K(K). Thus, the
1941e 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 61 52 65 value of.** aRe
1941f 61 64 4d 61 72 6b 5b 4b 5d 20 63 61 6e 6e 6f 74 adMark[K] cannot
19420 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 74 changed while t
19421 68 65 72 65 20 69 73 20 61 20 72 65 61 64 65 72 here is a reader
19422 20 69 73 20 75 73 69 6e 67 20 74 68 61 74 20 6d is using that m
19423 61 72 6b 0a 2a 2a 20 73 69 6e 63 65 20 74 68 65 ark.** since the
19424 20 72 65 61 64 65 72 20 77 69 6c 6c 20 62 65 20 reader will be
19425 68 6f 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64 holding a shared
19426 20 6c 6f 63 6b 20 6f 6e 20 57 41 4c 5f 52 45 41 lock on WAL_REA
19427 44 5f 4c 4f 43 4b 28 4b 29 2e 0a 2a 2a 0a 2a 2a D_LOCK(K)..**.**
19428 20 54 68 65 20 63 68 65 63 6b 70 6f 69 6e 74 65 The checkpointe
19429 72 20 6d 61 79 20 6f 6e 6c 79 20 74 72 61 6e 73 r may only trans
1942a 66 65 72 20 66 72 61 6d 65 73 20 66 72 6f 6d 20 fer frames from
1942b 57 41 4c 20 74 6f 20 64 61 74 61 62 61 73 65 20 WAL to database
1942c 77 68 65 72 65 0a 2a 2a 20 74 68 65 20 66 72 61 where.** the fra
1942d 6d 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6c me numbers are l
1942e 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ess than or equa
1942f 6c 20 74 6f 20 65 76 65 72 79 20 61 52 65 61 64 l to every aRead
19430 4d 61 72 6b 5b 5d 20 74 68 61 74 20 69 73 0a 2a Mark[] that is.*
19431 2a 20 69 6e 20 75 73 65 20 28 74 68 61 74 20 69 * in use (that i
19432 73 2c 20 65 76 65 72 79 20 61 52 65 61 64 4d 61 s, every aReadMa
19433 72 6b 5b 6a 5d 20 66 6f 72 20 77 68 69 63 68 20 rk[j] for which
19434 74 68 65 72 65 20 69 73 20 61 20 63 6f 72 72 65 there is a corre
19435 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 57 41 4c 5f sponding.** WAL_
19436 52 45 41 44 5f 4c 4f 43 4b 28 6a 29 29 2e 20 20 READ_LOCK(j)).
19437 4e 65 77 20 72 65 61 64 65 72 73 20 28 75 73 75 New readers (usu
19438 61 6c 6c 79 29 20 70 69 63 6b 20 74 68 65 20 61 ally) pick the a
19439 52 65 61 64 4d 61 72 6b 5b 5d 20 77 69 74 68 20 ReadMark[] with
1943a 74 68 65 0a 2a 2a 20 6c 61 72 67 65 73 74 20 76 the.** largest v
1943b 61 6c 75 65 20 61 6e 64 20 77 69 6c 6c 20 69 6e alue and will in
1943c 63 72 65 61 73 65 20 61 6e 20 75 6e 75 73 65 64 crease an unused
1943d 20 61 52 65 61 64 4d 61 72 6b 5b 5d 20 74 6f 20 aReadMark[] to
1943e 6d 78 46 72 61 6d 65 20 69 66 20 74 68 65 72 65 mxFrame if there
1943f 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 .** is not alrea
19440 64 79 20 61 6e 20 61 52 65 61 64 4d 61 72 6b 5b dy an aReadMark[
19441 5d 20 65 71 75 61 6c 20 74 6f 20 6d 78 46 72 61 ] equal to mxFra
19442 6d 65 2e 20 20 54 68 65 20 65 78 63 65 70 74 69 me. The excepti
19443 6f 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 70 72 65 on to the.** pre
19444 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 20 69 vious sentence i
19445 73 20 77 68 65 6e 20 6e 42 61 63 6b 66 69 6c 6c s when nBackfill
19446 20 65 71 75 61 6c 73 20 6d 78 46 72 61 6d 65 20 equals mxFrame
19447 28 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 65 76 (meaning that ev
19448 65 72 79 74 68 69 6e 67 0a 2a 2a 20 69 6e 20 74 erything.** in t
19449 68 65 20 57 41 4c 20 68 61 73 20 62 65 65 6e 20 he WAL has been
1944a 62 61 63 6b 66 69 6c 6c 65 64 20 69 6e 74 6f 20 backfilled into
1944b 74 68 65 20 64 61 74 61 62 61 73 65 29 20 74 68 the database) th
1944c 65 6e 20 6e 65 77 20 72 65 61 64 65 72 73 0a 2a en new readers.*
1944d 2a 20 77 69 6c 6c 20 63 68 6f 6f 73 65 20 61 52 * will choose aR
1944e 65 61 64 4d 61 72 6b 5b 30 5d 20 77 68 69 63 68 eadMark[0] which
1944f 20 68 61 73 20 76 61 6c 75 65 20 30 20 61 6e 64 has value 0 and
19450 20 68 65 6e 63 65 20 73 75 63 68 20 72 65 61 64 hence such read
19451 65 72 20 77 69 6c 6c 0a 2a 2a 20 67 65 74 20 61 er will.** get a
19452 6c 6c 20 74 68 65 69 72 20 61 6c 6c 20 63 6f 6e ll their all con
19453 74 65 6e 74 20 64 69 72 65 63 74 6c 79 20 66 72 tent directly fr
19454 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
19455 66 69 6c 65 20 61 6e 64 20 69 67 6e 6f 72 65 20 file and ignore
19456 0a 2a 2a 20 74 68 65 20 57 41 4c 2e 0a 2a 2a 0a .** the WAL..**.
19457 2a 2a 20 57 72 69 74 65 72 73 20 6e 6f 72 6d 61 ** Writers norma
19458 6c 6c 79 20 61 70 70 65 6e 64 20 6e 65 77 20 66 lly append new f
19459 72 61 6d 65 73 20 74 6f 20 74 68 65 20 65 6e 64 rames to the end
1945a 20 6f 66 20 74 68 65 20 57 41 4c 2e 20 20 48 6f of the WAL. Ho
1945b 77 65 76 65 72 2c 0a 2a 2a 20 69 66 20 6e 42 61 wever,.** if nBa
1945c 63 6b 66 69 6c 6c 20 65 71 75 61 6c 73 20 6d 78 ckfill equals mx
1945d 46 72 61 6d 65 20 28 6d 65 61 6e 69 6e 67 20 74 Frame (meaning t
1945e 68 61 74 20 61 6c 6c 20 57 41 4c 20 63 6f 6e 74 hat all WAL cont
1945f 65 6e 74 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 ent has been.**
19460 77 72 69 74 74 65 6e 20 62 61 63 6b 20 69 6e 74 written back int
19461 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 29 20 o the database)
19462 61 6e 64 20 69 66 20 6e 6f 20 72 65 61 64 65 72 and if no reader
19463 73 20 61 72 65 20 75 73 69 6e 67 20 74 68 65 20 s are using the
19464 57 41 4c 0a 2a 2a 20 28 69 6e 20 6f 74 68 65 72 WAL.** (in other
19465 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 72 65 words, if there
19466 20 61 72 65 20 6e 6f 20 57 41 4c 5f 52 45 41 44 are no WAL_READ
19467 5f 4c 4f 43 4b 28 69 29 20 77 68 65 72 65 20 69 _LOCK(i) where i
19468 3e 30 29 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 >0) then.** the
19469 77 72 69 74 65 72 20 77 69 6c 6c 20 66 69 72 73 writer will firs
1946a 74 20 22 72 65 73 65 74 22 20 74 68 65 20 57 41 t "reset" the WA
1946b 4c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65 L back to the be
1946c 67 69 6e 6e 69 6e 67 20 61 6e 64 20 73 74 61 72 ginning and star
1946d 74 0a 2a 2a 20 77 72 69 74 69 6e 67 20 6e 65 77 t.** writing new
1946e 20 63 6f 6e 74 65 6e 74 20 62 65 67 69 6e 6e 69 content beginni
1946f 6e 67 20 61 74 20 66 72 61 6d 65 20 31 2e 0a 2a ng at frame 1..*
19470 2a 0a 2a 2a 20 57 65 20 61 73 73 75 6d 65 20 74 *.** We assume t
19471 68 61 74 20 33 32 2d 62 69 74 20 6c 6f 61 64 73 hat 32-bit loads
19472 20 61 72 65 20 61 74 6f 6d 69 63 20 61 6e 64 20 are atomic and
19473 73 6f 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 so no locks are
19474 6e 65 65 64 65 64 20 69 6e 0a 2a 2a 20 6f 72 64 needed in.** ord
19475 65 72 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 er to read from
19476 61 6e 79 20 61 52 65 61 64 4d 61 72 6b 5b 5d 20 any aReadMark[]
19477 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 72 75 entries..*/.stru
19478 63 74 20 57 61 6c 43 6b 70 74 49 6e 66 6f 20 7b ct WalCkptInfo {
19479 0a 20 20 75 33 32 20 6e 42 61 63 6b 66 69 6c 6c . u32 nBackfill
1947a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1947b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1947c 57 41 4c 20 66 72 61 6d 65 73 20 62 61 63 6b 66 WAL frames backf
1947d 69 6c 6c 65 64 20 69 6e 74 6f 20 44 42 20 2a 2f illed into DB */
1947e 0a 20 20 75 33 32 20 61 52 65 61 64 4d 61 72 6b . u32 aReadMark
1947f 5b 57 41 4c 5f 4e 52 45 41 44 45 52 5d 3b 20 20 [WAL_NREADER];
19480 20 20 20 2f 2a 20 52 65 61 64 65 72 20 6d 61 72 /* Reader mar
19481 6b 73 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 ks */.};.#define
19482 20 52 45 41 44 4d 41 52 4b 5f 4e 4f 54 5f 55 53 READMARK_NOT_US
19483 45 44 20 20 30 78 66 66 66 66 66 66 66 66 0a 0a ED 0xffffffff..
19484 0a 2f 2a 20 41 20 62 6c 6f 63 6b 20 6f 66 20 57 ./* A block of W
19485 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b 5f 52 45 53 ALINDEX_LOCK_RES
19486 45 52 56 45 44 20 62 79 74 65 73 20 62 65 67 69 ERVED bytes begi
19487 6e 6e 69 6e 67 20 61 74 0a 2a 2a 20 57 41 4c 49 nning at.** WALI
19488 4e 44 45 58 5f 4c 4f 43 4b 5f 4f 46 46 53 45 54 NDEX_LOCK_OFFSET
19489 20 69 73 20 72 65 73 65 72 76 65 64 20 66 6f 72 is reserved for
1948a 20 6c 6f 63 6b 73 2e 20 53 69 6e 63 65 20 73 6f locks. Since so
1948b 6d 65 20 73 79 73 74 65 6d 73 0a 2a 2a 20 6f 6e me systems.** on
1948c 6c 79 20 73 75 70 70 6f 72 74 20 6d 61 6e 64 61 ly support manda
1948d 74 6f 72 79 20 66 69 6c 65 2d 6c 6f 63 6b 73 2c tory file-locks,
1948e 20 77 65 20 64 6f 20 6e 6f 74 20 72 65 61 64 20 we do not read
1948f 6f 72 20 77 72 69 74 65 20 64 61 74 61 0a 2a 2a or write data.**
19490 20 66 72 6f 6d 20 74 68 65 20 72 65 67 69 6f 6e from the region
19491 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 of the file on
19492 77 68 69 63 68 20 6c 6f 63 6b 73 20 61 72 65 20 which locks are
19493 61 70 70 6c 69 65 64 2e 0a 2a 2f 0a 23 64 65 66 applied..*/.#def
19494 69 6e 65 20 57 41 4c 49 4e 44 45 58 5f 4c 4f 43 ine WALINDEX_LOC
19495 4b 5f 4f 46 46 53 45 54 20 20 20 28 73 69 7a 65 K_OFFSET (size
19496 6f 66 28 57 61 6c 49 6e 64 65 78 48 64 72 29 2a of(WalIndexHdr)*
19497 32 20 2b 20 73 69 7a 65 6f 66 28 57 61 6c 43 6b 2 + sizeof(WalCk
19498 70 74 49 6e 66 6f 29 29 0a 23 64 65 66 69 6e 65 ptInfo)).#define
19499 20 57 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b 5f 52 WALINDEX_LOCK_R
1949a 45 53 45 52 56 45 44 20 31 36 0a 23 64 65 66 69 ESERVED 16.#defi
1949b 6e 65 20 57 41 4c 49 4e 44 45 58 5f 48 44 52 5f ne WALINDEX_HDR_
1949c 53 49 5a 45 20 20 20 20 20 20 28 57 41 4c 49 4e SIZE (WALIN
1949d 44 45 58 5f 4c 4f 43 4b 5f 4f 46 46 53 45 54 2b DEX_LOCK_OFFSET+
1949e 57 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b 5f 52 45 WALINDEX_LOCK_RE
1949f 53 45 52 56 45 44 29 0a 0a 2f 2a 20 53 69 7a 65 SERVED)../* Size
194a0 20 6f 66 20 68 65 61 64 65 72 20 62 65 66 6f 72 of header befor
194a1 65 20 65 61 63 68 20 66 72 61 6d 65 20 69 6e 20 e each frame in
194a2 77 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 wal */.#define W
194a3 41 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 5a 45 AL_FRAME_HDRSIZE
194a4 20 32 34 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 24../* Size of
194a5 77 72 69 74 65 20 61 68 65 61 64 20 6c 6f 67 20 write ahead log
194a6 68 65 61 64 65 72 2c 20 69 6e 63 6c 75 64 69 6e header, includin
194a7 67 20 63 68 65 63 6b 73 75 6d 2e 20 2a 2f 0a 2f g checksum. */./
194a8 2a 20 23 64 65 66 69 6e 65 20 57 41 4c 5f 48 44 * #define WAL_HD
194a9 52 53 49 5a 45 20 32 34 20 2a 2f 0a 23 64 65 66 RSIZE 24 */.#def
194aa 69 6e 65 20 57 41 4c 5f 48 44 52 53 49 5a 45 20 ine WAL_HDRSIZE
194ab 33 32 0a 0a 2f 2a 20 57 41 4c 20 6d 61 67 69 63 32../* WAL magic
194ac 20 76 61 6c 75 65 2e 20 45 69 74 68 65 72 20 74 value. Either t
194ad 68 69 73 20 76 61 6c 75 65 2c 20 6f 72 20 74 68 his value, or th
194ae 65 20 73 61 6d 65 20 76 61 6c 75 65 20 77 69 74 e same value wit
194af 68 20 74 68 65 20 6c 65 61 73 74 0a 2a 2a 20 73 h the least.** s
194b0 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74 20 61 ignificant bit a
194b1 6c 73 6f 20 73 65 74 20 28 57 41 4c 5f 4d 41 47 lso set (WAL_MAG
194b2 49 43 20 7c 20 30 78 30 30 30 30 30 30 30 31 29 IC | 0x00000001)
194b3 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 33 32 is stored in 32
194b4 2d 62 69 74 0a 2a 2a 20 62 69 67 2d 65 6e 64 69 -bit.** big-endi
194b5 61 6e 20 66 6f 72 6d 61 74 20 69 6e 20 74 68 65 an format in the
194b6 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6f first 4 bytes o
194b7 66 20 61 20 57 41 4c 20 66 69 6c 65 2e 0a 2a 2a f a WAL file..**
194b8 0a 2a 2a 20 49 66 20 74 68 65 20 4c 53 42 20 69 .** If the LSB i
194b9 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 s set, then the
194ba 63 68 65 63 6b 73 75 6d 73 20 66 6f 72 20 65 61 checksums for ea
194bb 63 68 20 66 72 61 6d 65 20 77 69 74 68 69 6e 20 ch frame within
194bc 74 68 65 20 57 41 4c 0a 2a 2a 20 66 69 6c 65 20 the WAL.** file
194bd 61 72 65 20 63 61 6c 63 75 6c 61 74 65 64 20 62 are calculated b
194be 79 20 74 72 65 61 74 69 6e 67 20 61 6c 6c 20 64 y treating all d
194bf 61 74 61 20 61 73 20 61 6e 20 61 72 72 61 79 20 ata as an array
194c0 6f 66 20 33 32 2d 62 69 74 20 0a 2a 2a 20 62 69 of 32-bit .** bi
194c1 67 2d 65 6e 64 69 61 6e 20 77 6f 72 64 73 2e 20 g-endian words.
194c2 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79 20 Otherwise, they
194c3 61 72 65 20 63 61 6c 63 75 6c 61 74 65 64 20 62 are calculated b
194c4 79 20 69 6e 74 65 72 70 72 65 74 69 6e 67 20 0a y interpreting .
194c5 2a 2a 20 61 6c 6c 20 64 61 74 61 20 61 73 20 33 ** all data as 3
194c6 32 2d 62 69 74 20 6c 69 74 74 6c 65 2d 65 6e 64 2-bit little-end
194c7 69 61 6e 20 77 6f 72 64 73 2e 0a 2a 2f 0a 23 64 ian words..*/.#d
194c8 65 66 69 6e 65 20 57 41 4c 5f 4d 41 47 49 43 20 efine WAL_MAGIC
194c9 30 78 33 37 37 66 30 36 38 32 0a 0a 2f 2a 0a 2a 0x377f0682../*.*
194ca 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 66 66 * Return the off
194cb 73 65 74 20 6f 66 20 66 72 61 6d 65 20 69 46 72 set of frame iFr
194cc 61 6d 65 20 69 6e 20 74 68 65 20 77 72 69 74 65 ame in the write
194cd 2d 61 68 65 61 64 20 6c 6f 67 20 66 69 6c 65 2c -ahead log file,
194ce 20 0a 2a 2a 20 61 73 73 75 6d 69 6e 67 20 61 20 .** assuming a
194cf 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73 69 database page si
194d0 7a 65 20 6f 66 20 73 7a 50 61 67 65 20 62 79 74 ze of szPage byt
194d1 65 73 2e 20 54 68 65 20 6f 66 66 73 65 74 20 72 es. The offset r
194d2 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 74 6f eturned.** is to
194d3 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
194d4 65 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f e write-ahead lo
194d5 67 20 66 72 61 6d 65 2d 68 65 61 64 65 72 2e 0a g frame-header..
194d6 2a 2f 0a 23 64 65 66 69 6e 65 20 77 61 6c 46 72 */.#define walFr
194d7 61 6d 65 4f 66 66 73 65 74 28 69 46 72 61 6d 65 ameOffset(iFrame
194d8 2c 20 73 7a 50 61 67 65 29 20 28 20 20 20 20 20 , szPage) (
194d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
194da 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 57 41 \. WA
194db 4c 5f 48 44 52 53 49 5a 45 20 2b 20 28 28 69 46 L_HDRSIZE + ((iF
194dc 72 61 6d 65 29 2d 31 29 2a 28 69 36 34 29 28 28 rame)-1)*(i64)((
194dd 73 7a 50 61 67 65 29 2b 57 41 4c 5f 46 52 41 4d szPage)+WAL_FRAM
194de 45 5f 48 44 52 53 49 5a 45 29 20 20 20 20 20 20 E_HDRSIZE)
194df 20 20 20 5c 0a 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e \.)../*.** An
194e0 20 6f 70 65 6e 20 77 72 69 74 65 2d 61 68 65 61 open write-ahea
194e1 64 20 6c 6f 67 20 66 69 6c 65 20 69 73 20 72 65 d log file is re
194e2 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 6e 20 presented by an
194e3 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 0a instance of the.
194e4 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a ** following obj
194e5 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 ect..*/.struct W
194e6 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 al {. sqlite3_v
194e7 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 fs *pVfs;
194e8 20 20 2f 2a 20 54 68 65 20 56 46 53 20 75 73 65 /* The VFS use
194e9 64 20 74 6f 20 63 72 65 61 74 65 20 70 44 62 46 d to create pDbF
194ea 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 d */. sqlite3_f
194eb 69 6c 65 20 2a 70 44 62 46 64 3b 20 20 20 20 20 ile *pDbFd;
194ec 20 20 2f 2a 20 46 69 6c 65 20 68 61 6e 64 6c 65 /* File handle
194ed 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 for the databas
194ee 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 e file */. sqli
194ef 74 65 33 5f 66 69 6c 65 20 2a 70 57 61 6c 46 64 te3_file *pWalFd
194f0 3b 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 68 ; /* File h
194f1 61 6e 64 6c 65 20 66 6f 72 20 57 41 4c 20 66 69 andle for WAL fi
194f2 6c 65 20 2a 2f 0a 20 20 75 33 32 20 69 43 61 6c le */. u32 iCal
194f3 6c 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 lback;
194f4 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 70 /* Value to p
194f5 61 73 73 20 74 6f 20 6c 6f 67 20 63 61 6c 6c 62 ass to log callb
194f6 61 63 6b 20 28 6f 72 20 30 29 20 2a 2f 0a 20 20 ack (or 0) */.
194f7 69 36 34 20 6d 78 57 61 6c 53 69 7a 65 3b 20 20 i64 mxWalSize;
194f8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
194f9 75 6e 63 61 74 65 20 57 41 4c 20 74 6f 20 74 68 uncate WAL to th
194fa 69 73 20 73 69 7a 65 20 75 70 6f 6e 20 72 65 73 is size upon res
194fb 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 57 69 44 et */. int nWiD
194fc 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 ata;
194fd 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72 /* Size of ar
194fe 72 61 79 20 61 70 57 69 44 61 74 61 20 2a 2f 0a ray apWiData */.
194ff 20 20 69 6e 74 20 73 7a 46 69 72 73 74 42 6c 6f int szFirstBlo
19500 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ck; /*
19501 53 69 7a 65 20 6f 66 20 66 69 72 73 74 20 62 6c Size of first bl
19502 6f 63 6b 20 77 72 69 74 74 65 6e 20 74 6f 20 57 ock written to W
19503 41 4c 20 66 69 6c 65 20 2a 2f 0a 20 20 76 6f 6c AL file */. vol
19504 61 74 69 6c 65 20 75 33 32 20 2a 2a 61 70 57 69 atile u32 **apWi
19505 44 61 74 61 3b 20 20 20 2f 2a 20 50 6f 69 6e 74 Data; /* Point
19506 65 72 20 74 6f 20 77 61 6c 2d 69 6e 64 65 78 20 er to wal-index
19507 63 6f 6e 74 65 6e 74 20 69 6e 20 6d 65 6d 6f 72 content in memor
19508 79 20 2a 2f 0a 20 20 75 33 32 20 73 7a 50 61 67 y */. u32 szPag
19509 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1950a 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 70 61 /* Database pa
1950b 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 31 36 ge size */. i16
1950c 20 72 65 61 64 4c 6f 63 6b 3b 20 20 20 20 20 20 readLock;
1950d 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 /* Which
1950e 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 62 65 read lock is be
1950f 69 6e 67 20 68 65 6c 64 2e 20 20 2d 31 20 66 6f ing held. -1 fo
19510 72 20 6e 6f 6e 65 20 2a 2f 0a 20 20 75 38 20 73 r none */. u8 s
19511 79 6e 63 46 6c 61 67 73 3b 20 20 20 20 20 20 20 yncFlags;
19512 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
19513 74 6f 20 75 73 65 20 74 6f 20 73 79 6e 63 20 68 to use to sync h
19514 65 61 64 65 72 20 77 72 69 74 65 73 20 2a 2f 0a eader writes */.
19515 20 20 75 38 20 65 78 63 6c 75 73 69 76 65 4d 6f u8 exclusiveMo
19516 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 de; /*
19517 4e 6f 6e 2d 7a 65 72 6f 20 69 66 20 63 6f 6e 6e Non-zero if conn
19518 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 65 78 63 ection is in exc
19519 6c 75 73 69 76 65 20 6d 6f 64 65 20 2a 2f 0a 20 lusive mode */.
1951a 20 75 38 20 77 72 69 74 65 4c 6f 63 6b 3b 20 20 u8 writeLock;
1951b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1951c 72 75 65 20 69 66 20 69 6e 20 61 20 77 72 69 74 rue if in a writ
1951d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f e transaction */
1951e 0a 20 20 75 38 20 63 6b 70 74 4c 6f 63 6b 3b 20 . u8 ckptLock;
1951f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19520 20 54 72 75 65 20 69 66 20 68 6f 6c 64 69 6e 67 True if holding
19521 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 20 6c 6f a checkpoint lo
19522 63 6b 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f ck */. u8 readO
19523 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 nly;
19524 20 20 20 2f 2a 20 57 41 4c 5f 52 44 57 52 2c 20 /* WAL_RDWR,
19525 57 41 4c 5f 52 44 4f 4e 4c 59 2c 20 6f 72 20 57 WAL_RDONLY, or W
19526 41 4c 5f 53 48 4d 5f 52 44 4f 4e 4c 59 20 2a 2f AL_SHM_RDONLY */
19527 0a 20 20 75 38 20 74 72 75 6e 63 61 74 65 4f 6e . u8 truncateOn
19528 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 2f 2a Commit; /*
19529 20 54 72 75 65 20 74 6f 20 74 72 75 6e 63 61 74 True to truncat
1952a 65 20 57 41 4c 20 66 69 6c 65 20 6f 6e 20 63 6f e WAL file on co
1952b 6d 6d 69 74 20 2a 2f 0a 20 20 75 38 20 73 79 6e mmit */. u8 syn
1952c 63 48 65 61 64 65 72 3b 20 20 20 20 20 20 20 20 cHeader;
1952d 20 20 20 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 /* Fsync th
1952e 65 20 57 41 4c 20 68 65 61 64 65 72 20 69 66 20 e WAL header if
1952f 74 72 75 65 20 2a 2f 0a 20 20 75 38 20 70 61 64 true */. u8 pad
19530 54 6f 53 65 63 74 6f 72 42 6f 75 6e 64 61 72 79 ToSectorBoundary
19531 3b 20 20 20 20 2f 2a 20 50 61 64 20 74 72 61 6e ; /* Pad tran
19532 73 61 63 74 69 6f 6e 73 20 6f 75 74 20 74 6f 20 sactions out to
19533 74 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 the next sector
19534 2a 2f 0a 20 20 57 61 6c 49 6e 64 65 78 48 64 72 */. WalIndexHdr
19535 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 hdr;
19536 2f 2a 20 57 61 6c 2d 69 6e 64 65 78 20 68 65 61 /* Wal-index hea
19537 64 65 72 20 66 6f 72 20 63 75 72 72 65 6e 74 20 der for current
19538 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 transaction */.
19539 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 57 61 const char *zWa
1953a 6c 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e lName; /* N
1953b 61 6d 65 20 6f 66 20 57 41 4c 20 66 69 6c 65 20 ame of WAL file
1953c 2a 2f 0a 20 20 75 33 32 20 6e 43 6b 70 74 3b 20 */. u32 nCkpt;
1953d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1953e 2f 2a 20 43 68 65 63 6b 70 6f 69 6e 74 20 73 65 /* Checkpoint se
1953f 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72 20 69 quence counter i
19540 6e 20 74 68 65 20 77 61 6c 2d 68 65 61 64 65 72 n the wal-header
19541 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
19542 45 5f 44 45 42 55 47 0a 20 20 75 38 20 6c 6f 63 E_DEBUG. u8 loc
19543 6b 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 kError;
19544 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
19545 61 20 6c 6f 63 6b 69 6e 67 20 65 72 72 6f 72 20 a locking error
19546 68 61 73 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a has occurred */.
19547 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a #endif.};../*.**
19548 20 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75 65 Candidate value
19549 73 20 66 6f 72 20 57 61 6c 2e 65 78 63 6c 75 73 s for Wal.exclus
1954a 69 76 65 4d 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 iveMode..*/.#def
1954b 69 6e 65 20 57 41 4c 5f 4e 4f 52 4d 41 4c 5f 4d ine WAL_NORMAL_M
1954c 4f 44 45 20 20 20 20 20 30 0a 23 64 65 66 69 6e ODE 0.#defin
1954d 65 20 57 41 4c 5f 45 58 43 4c 55 53 49 56 45 5f e WAL_EXCLUSIVE_
1954e 4d 4f 44 45 20 20 31 20 20 20 20 20 0a 23 64 65 MODE 1 .#de
1954f 66 69 6e 65 20 57 41 4c 5f 48 45 41 50 4d 45 4d fine WAL_HEAPMEM
19550 4f 52 59 5f 4d 4f 44 45 20 32 0a 0a 2f 2a 0a 2a ORY_MODE 2../*.*
19551 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 * Possible value
19552 73 20 66 6f 72 20 57 41 4c 2e 72 65 61 64 4f 6e s for WAL.readOn
19553 6c 79 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 41 ly.*/.#define WA
19554 4c 5f 52 44 57 52 20 20 20 20 20 20 20 20 30 20 L_RDWR 0
19555 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 72 65 61 /* Normal rea
19556 64 2f 77 72 69 74 65 20 63 6f 6e 6e 65 63 74 69 d/write connecti
19557 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 41 on */.#define WA
19558 4c 5f 52 44 4f 4e 4c 59 20 20 20 20 20 20 31 20 L_RDONLY 1
19559 20 20 20 2f 2a 20 54 68 65 20 57 41 4c 20 66 69 /* The WAL fi
1955a 6c 65 20 69 73 20 72 65 61 64 6f 6e 6c 79 20 2a le is readonly *
1955b 2f 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 53 48 /.#define WAL_SH
1955c 4d 5f 52 44 4f 4e 4c 59 20 20 32 20 20 20 20 2f M_RDONLY 2 /
1955d 2a 20 54 68 65 20 53 48 4d 20 66 69 6c 65 20 69 * The SHM file i
1955e 73 20 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a 0a 2f s readonly */../
1955f 2a 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20 6f *.** Each page o
19560 66 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 f the wal-index
19561 6d 61 70 70 69 6e 67 20 63 6f 6e 74 61 69 6e 73 mapping contains
19562 20 61 20 68 61 73 68 2d 74 61 62 6c 65 20 6d 61 a hash-table ma
19563 64 65 20 75 70 20 6f 66 0a 2a 2a 20 61 6e 20 61 de up of.** an a
19564 72 72 61 79 20 6f 66 20 48 41 53 48 54 41 42 4c rray of HASHTABL
19565 45 5f 4e 53 4c 4f 54 20 65 6c 65 6d 65 6e 74 73 E_NSLOT elements
19566 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
19567 67 20 74 79 70 65 2e 0a 2a 2f 0a 74 79 70 65 64 g type..*/.typed
19568 65 66 20 75 31 36 20 68 74 5f 73 6c 6f 74 3b 0a ef u16 ht_slot;.
19569 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 ./*.** This stru
1956a 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f cture is used to
1956b 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 74 implement an it
1956c 65 72 61 74 6f 72 20 74 68 61 74 20 6c 6f 6f 70 erator that loop
1956d 73 20 74 68 72 6f 75 67 68 0a 2a 2a 20 61 6c 6c s through.** all
1956e 20 66 72 61 6d 65 73 20 69 6e 20 74 68 65 20 57 frames in the W
1956f 41 4c 20 69 6e 20 64 61 74 61 62 61 73 65 20 70 AL in database p
19570 61 67 65 20 6f 72 64 65 72 2e 20 57 68 65 72 65 age order. Where
19571 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 66 72 61 two or more fra
19572 6d 65 73 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e mes.** correspon
19573 64 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61 d to the same da
19574 74 61 62 61 73 65 20 70 61 67 65 2c 20 74 68 65 tabase page, the
19575 20 69 74 65 72 61 74 6f 72 20 76 69 73 69 74 73 iterator visits
19576 20 6f 6e 6c 79 20 74 68 65 20 0a 2a 2a 20 66 72 only the .** fr
19577 61 6d 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c ame most recentl
19578 79 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 y written to the
19579 20 57 41 4c 20 28 69 6e 20 6f 74 68 65 72 20 77 WAL (in other w
1957a 6f 72 64 73 2c 20 74 68 65 20 66 72 61 6d 65 20 ords, the frame
1957b 77 69 74 68 0a 2a 2a 20 74 68 65 20 6c 61 72 67 with.** the larg
1957c 65 73 74 20 69 6e 64 65 78 29 2e 0a 2a 2a 0a 2a est index)..**.*
1957d 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 * The internals
1957e 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
1957f 65 20 61 72 65 20 6f 6e 6c 79 20 61 63 63 65 73 e are only acces
19580 73 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 sed by:.**.**
19581 77 61 6c 49 74 65 72 61 74 6f 72 49 6e 69 74 28 walIteratorInit(
19582 29 20 2d 20 43 72 65 61 74 65 20 61 20 6e 65 77 ) - Create a new
19583 20 69 74 65 72 61 74 6f 72 2c 0a 2a 2a 20 20 20 iterator,.**
19584 77 61 6c 49 74 65 72 61 74 6f 72 4e 65 78 74 28 walIteratorNext(
19585 29 20 2d 20 53 74 65 70 20 61 6e 20 69 74 65 72 ) - Step an iter
19586 61 74 6f 72 2c 0a 2a 2a 20 20 20 77 61 6c 49 74 ator,.** walIt
19587 65 72 61 74 6f 72 46 72 65 65 28 29 20 2d 20 46 eratorFree() - F
19588 72 65 65 20 61 6e 20 69 74 65 72 61 74 6f 72 2e ree an iterator.
19589 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
1958a 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 75 73 65 tionality is use
1958b 64 20 62 79 20 74 68 65 20 63 68 65 63 6b 70 6f d by the checkpo
1958c 69 6e 74 20 63 6f 64 65 20 28 73 65 65 20 77 61 int code (see wa
1958d 6c 43 68 65 63 6b 70 6f 69 6e 74 28 29 29 2e 0a lCheckpoint())..
1958e 2a 2f 0a 73 74 72 75 63 74 20 57 61 6c 49 74 65 */.struct WalIte
1958f 72 61 74 6f 72 20 7b 0a 20 20 69 6e 74 20 69 50 rator {. int iP
19590 72 69 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 rior;
19591 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 /* Las
19592 74 20 72 65 73 75 6c 74 20 72 65 74 75 72 6e 65 t result returne
19593 64 20 66 72 6f 6d 20 74 68 65 20 69 74 65 72 61 d from the itera
19594 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65 tor */. int nSe
19595 67 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 gment;
19596 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
19597 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e er of entries in
19598 20 61 53 65 67 6d 65 6e 74 5b 5d 20 2a 2f 0a 20 aSegment[] */.
19599 20 73 74 72 75 63 74 20 57 61 6c 53 65 67 6d 65 struct WalSegme
1959a 6e 74 20 7b 0a 20 20 20 20 69 6e 74 20 69 4e 65 nt {. int iNe
1959b 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 xt;
1959c 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 73 /* Next s
1959d 6c 6f 74 20 69 6e 20 61 49 6e 64 65 78 5b 5d 20 lot in aIndex[]
1959e 6e 6f 74 20 79 65 74 20 72 65 74 75 72 6e 65 64 not yet returned
1959f 20 2a 2f 0a 20 20 20 20 68 74 5f 73 6c 6f 74 20 */. ht_slot
195a0 2a 61 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 *aIndex;
195a1 20 20 20 20 20 20 2f 2a 20 69 30 2c 20 69 31 2c /* i0, i1,
195a2 20 69 32 2e 2e 2e 20 73 75 63 68 20 74 68 61 74 i2... such that
195a3 20 61 50 67 6e 6f 5b 69 4e 5d 20 61 73 63 65 6e aPgno[iN] ascen
195a4 64 20 2a 2f 0a 20 20 20 20 75 33 32 20 2a 61 50 d */. u32 *aP
195a5 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 gno;
195a6 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 /* Array
195a7 6f 66 20 70 61 67 65 20 6e 75 6d 62 65 72 73 2e of page numbers.
195a8 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 6e 74 */. int nEnt
195a9 72 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ry;
195aa 20 20 20 20 20 20 2f 2a 20 4e 72 2e 20 6f 66 20 /* Nr. of
195ab 65 6e 74 72 69 65 73 20 69 6e 20 61 50 67 6e 6f entries in aPgno
195ac 5b 5d 20 61 6e 64 20 61 49 6e 64 65 78 5b 5d 20 [] and aIndex[]
195ad 2a 2f 0a 20 20 20 20 69 6e 74 20 69 5a 65 72 6f */. int iZero
195ae 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
195af 20 20 20 20 20 2f 2a 20 46 72 61 6d 65 20 6e 75 /* Frame nu
195b0 6d 62 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 mber associated
195b1 77 69 74 68 20 61 50 67 6e 6f 5b 30 5d 20 2a 2f with aPgno[0] */
195b2 0a 20 20 7d 20 61 53 65 67 6d 65 6e 74 5b 31 5d . } aSegment[1]
195b3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
195b4 20 20 20 2f 2a 20 4f 6e 65 20 66 6f 72 20 65 76 /* One for ev
195b5 65 72 79 20 33 32 4b 42 20 70 61 67 65 20 69 6e ery 32KB page in
195b6 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 2a the wal-index *
195b7 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 /.};../*.** Defi
195b8 6e 65 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 ne the parameter
195b9 73 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 s of the hash ta
195ba 62 6c 65 73 20 69 6e 20 74 68 65 20 77 61 6c 2d bles in the wal-
195bb 69 6e 64 65 78 20 66 69 6c 65 2e 20 54 68 65 72 index file. Ther
195bc 65 0a 2a 2a 20 69 73 20 61 20 68 61 73 68 2d 74 e.** is a hash-t
195bd 61 62 6c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 able following e
195be 76 65 72 79 20 48 41 53 48 54 41 42 4c 45 5f 4e very HASHTABLE_N
195bf 50 41 47 45 20 70 61 67 65 20 6e 75 6d 62 65 72 PAGE page number
195c0 73 20 69 6e 20 74 68 65 0a 2a 2a 20 77 61 6c 2d s in the.** wal-
195c1 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 index..**.** Cha
195c2 6e 67 69 6e 67 20 61 6e 79 20 6f 66 20 74 68 65 nging any of the
195c3 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 6c se constants wil
195c4 6c 20 61 6c 74 65 72 20 74 68 65 20 77 61 6c 2d l alter the wal-
195c5 69 6e 64 65 78 20 66 6f 72 6d 61 74 20 61 6e 64 index format and
195c6 0a 2a 2a 20 63 72 65 61 74 65 20 69 6e 63 6f 6d .** create incom
195c7 70 61 74 69 62 69 6c 69 74 69 65 73 2e 0a 2a 2f patibilities..*/
195c8 0a 23 64 65 66 69 6e 65 20 48 41 53 48 54 41 42 .#define HASHTAB
195c9 4c 45 5f 4e 50 41 47 45 20 20 20 20 20 20 34 30 LE_NPAGE 40
195ca 39 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 96
195cb 20 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 70 6f /* Must be po
195cc 77 65 72 20 6f 66 20 32 20 2a 2f 0a 23 64 65 66 wer of 2 */.#def
195cd 69 6e 65 20 48 41 53 48 54 41 42 4c 45 5f 48 41 ine HASHTABLE_HA
195ce 53 48 5f 31 20 20 20 20 20 33 38 33 20 20 20 20 SH_1 383
195cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
195d0 20 53 68 6f 75 6c 64 20 62 65 20 70 72 69 6d 65 Should be prime
195d1 20 2a 2f 0a 23 64 65 66 69 6e 65 20 48 41 53 48 */.#define HASH
195d2 54 41 42 4c 45 5f 4e 53 4c 4f 54 20 20 20 20 20 TABLE_NSLOT
195d3 20 28 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 (HASHTABLE_NPAG
195d4 45 2a 32 29 20 20 2f 2a 20 4d 75 73 74 20 62 65 E*2) /* Must be
195d5 20 61 20 70 6f 77 65 72 20 6f 66 20 32 20 2a 2f a power of 2 */
195d6 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 62 6c 6f ../* .** The blo
195d7 63 6b 20 6f 66 20 70 61 67 65 20 6e 75 6d 62 65 ck of page numbe
195d8 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 rs associated wi
195d9 74 68 20 74 68 65 20 66 69 72 73 74 20 68 61 73 th the first has
195da 68 2d 74 61 62 6c 65 20 69 6e 20 61 0a 2a 2a 20 h-table in a.**
195db 77 61 6c 2d 69 6e 64 65 78 20 69 73 20 73 6d 61 wal-index is sma
195dc 6c 6c 65 72 20 74 68 61 6e 20 75 73 75 61 6c 2e ller than usual.
195dd 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 This is so that
195de 20 74 68 65 72 65 20 69 73 20 61 20 63 6f 6d 70 there is a comp
195df 6c 65 74 65 0a 2a 2a 20 68 61 73 68 2d 74 61 62 lete.** hash-tab
195e0 6c 65 20 6f 6e 20 65 61 63 68 20 61 6c 69 67 6e le on each align
195e1 65 64 20 33 32 4b 42 20 70 61 67 65 20 6f 66 20 ed 32KB page of
195e2 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 2e 0a 2a the wal-index..*
195e3 2f 0a 23 64 65 66 69 6e 65 20 48 41 53 48 54 41 /.#define HASHTA
195e4 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 20 20 28 BLE_NPAGE_ONE (
195e5 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 20 HASHTABLE_NPAGE
195e6 2d 20 28 57 41 4c 49 4e 44 45 58 5f 48 44 52 5f - (WALINDEX_HDR_
195e7 53 49 5a 45 2f 73 69 7a 65 6f 66 28 75 33 32 29 SIZE/sizeof(u32)
195e8 29 29 0a 0a 2f 2a 20 54 68 65 20 77 61 6c 2d 69 ))../* The wal-i
195e9 6e 64 65 78 20 69 73 20 64 69 76 69 64 65 64 20 ndex is divided
195ea 69 6e 74 6f 20 70 61 67 65 73 20 6f 66 20 57 41 into pages of WA
195eb 4c 49 4e 44 45 58 5f 50 47 53 5a 20 62 79 74 65 LINDEX_PGSZ byte
195ec 73 20 65 61 63 68 2e 20 2a 2f 0a 23 64 65 66 69 s each. */.#defi
195ed 6e 65 20 57 41 4c 49 4e 44 45 58 5f 50 47 53 5a ne WALINDEX_PGSZ
195ee 20 20 20 28 20 20 20 20 20 20 20 20 20 20 20 20 (
195ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
195f0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
195f1 20 20 20 73 69 7a 65 6f 66 28 68 74 5f 73 6c 6f sizeof(ht_slo
195f2 74 29 2a 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c t)*HASHTABLE_NSL
195f3 4f 54 20 2b 20 48 41 53 48 54 41 42 4c 45 5f 4e OT + HASHTABLE_N
195f4 50 41 47 45 2a 73 69 7a 65 6f 66 28 75 33 32 29 PAGE*sizeof(u32)
195f5 20 5c 0a 29 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 \.)../*.** Obta
195f6 69 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 in a pointer to
195f7 74 68 65 20 69 50 61 67 65 27 74 68 20 70 61 67 the iPage'th pag
195f8 65 20 6f 66 20 74 68 65 20 77 61 6c 2d 69 6e 64 e of the wal-ind
195f9 65 78 2e 20 54 68 65 20 77 61 6c 2d 69 6e 64 65 ex. The wal-inde
195fa 78 0a 2a 2a 20 69 73 20 62 72 6f 6b 65 6e 20 69 x.** is broken i
195fb 6e 74 6f 20 70 61 67 65 73 20 6f 66 20 57 41 4c nto pages of WAL
195fc 49 4e 44 45 58 5f 50 47 53 5a 20 62 79 74 65 73 INDEX_PGSZ bytes
195fd 2e 20 57 61 6c 2d 69 6e 64 65 78 20 70 61 67 65 . Wal-index page
195fe 73 20 61 72 65 0a 2a 2a 20 6e 75 6d 62 65 72 65 s are.** numbere
195ff 64 20 66 72 6f 6d 20 7a 65 72 6f 2e 0a 2a 2a 0a d from zero..**.
19600 2a 2a 20 49 66 20 74 68 69 73 20 63 61 6c 6c 20 ** If this call
19601 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a is successful, *
19602 70 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f ppPage is set to
19603 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 77 61 point to the wa
19604 6c 2d 69 6e 64 65 78 0a 2a 2a 20 70 61 67 65 20 l-index.** page
19605 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 and SQLITE_OK is
19606 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e returned. If an
19607 20 65 72 72 6f 72 20 28 61 6e 20 4f 4f 4d 20 6f error (an OOM o
19608 72 20 56 46 53 20 65 72 72 6f 72 29 20 6f 63 63 r VFS error) occ
19609 75 72 73 2c 0a 2a 2a 20 74 68 65 6e 20 61 6e 20 urs,.** then an
1960a 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 SQLite error cod
1960b 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e e is returned an
1960c 64 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 d *ppPage is set
1960d 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 to 0..*/.static
1960e 20 69 6e 74 20 77 61 6c 49 6e 64 65 78 50 61 67 int walIndexPag
1960f 65 28 57 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 e(Wal *pWal, int
19610 20 69 50 61 67 65 2c 20 76 6f 6c 61 74 69 6c 65 iPage, volatile
19611 20 75 33 32 20 2a 2a 70 70 50 61 67 65 29 7b 0a u32 **ppPage){.
19612 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
19613 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 45 6e 6c 61 E_OK;.. /* Enla
19614 72 67 65 20 74 68 65 20 70 57 61 6c 2d 3e 61 70 rge the pWal->ap
19615 57 69 44 61 74 61 5b 5d 20 61 72 72 61 79 20 69 WiData[] array i
19616 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 f required */.
19617 69 66 28 20 70 57 61 6c 2d 3e 6e 57 69 44 61 74 if( pWal->nWiDat
19618 61 3c 3d 69 50 61 67 65 20 29 7b 0a 20 20 20 20 a<=iPage ){.
19619 69 6e 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 int nByte = size
1961a 6f 66 28 75 33 32 2a 29 2a 28 69 50 61 67 65 2b of(u32*)*(iPage+
1961b 31 29 3b 0a 20 20 20 20 76 6f 6c 61 74 69 6c 65 1);. volatile
1961c 20 75 33 32 20 2a 2a 61 70 4e 65 77 3b 0a 20 20 u32 **apNew;.
1961d 20 20 61 70 4e 65 77 20 3d 20 28 76 6f 6c 61 74 apNew = (volat
1961e 69 6c 65 20 75 33 32 20 2a 2a 29 73 71 6c 69 74 ile u32 **)sqlit
1961f 65 33 5f 72 65 61 6c 6c 6f 63 28 28 76 6f 69 64 e3_realloc((void
19620 20 2a 29 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 *)pWal->apWiDat
19621 61 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 a, nByte);. i
19622 66 28 20 21 61 70 4e 65 77 20 29 7b 0a 20 20 20 f( !apNew ){.
19623 20 20 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a *ppPage = 0;.
19624 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
19625 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
19626 0a 20 20 20 20 6d 65 6d 73 65 74 28 28 76 6f 69 . memset((voi
19627 64 2a 29 26 61 70 4e 65 77 5b 70 57 61 6c 2d 3e d*)&apNew[pWal->
19628 6e 57 69 44 61 74 61 5d 2c 20 30 2c 0a 20 20 20 nWiData], 0,.
19629 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 75 sizeof(u
1962a 33 32 2a 29 2a 28 69 50 61 67 65 2b 31 2d 70 57 32*)*(iPage+1-pW
1962b 61 6c 2d 3e 6e 57 69 44 61 74 61 29 29 3b 0a 20 al->nWiData));.
1962c 20 20 20 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 pWal->apWiDat
1962d 61 20 3d 20 61 70 4e 65 77 3b 0a 20 20 20 20 70 a = apNew;. p
1962e 57 61 6c 2d 3e 6e 57 69 44 61 74 61 20 3d 20 69 Wal->nWiData = i
1962f 50 61 67 65 2b 31 3b 0a 20 20 7d 0a 0a 20 20 2f Page+1;. }.. /
19630 2a 20 52 65 71 75 65 73 74 20 61 20 70 6f 69 6e * Request a poin
19631 74 65 72 20 74 6f 20 74 68 65 20 72 65 71 75 69 ter to the requi
19632 72 65 64 20 70 61 67 65 20 66 72 6f 6d 20 74 68 red page from th
19633 65 20 56 46 53 20 2a 2f 0a 20 20 69 66 28 20 70 e VFS */. if( p
19634 57 61 6c 2d 3e 61 70 57 69 44 61 74 61 5b 69 50 Wal->apWiData[iP
19635 61 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 69 age]==0 ){. i
19636 66 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69 f( pWal->exclusi
19637 76 65 4d 6f 64 65 3d 3d 57 41 4c 5f 48 45 41 50 veMode==WAL_HEAP
19638 4d 45 4d 4f 52 59 5f 4d 4f 44 45 20 29 7b 0a 20 MEMORY_MODE ){.
19639 20 20 20 20 20 70 57 61 6c 2d 3e 61 70 57 69 44 pWal->apWiD
1963a 61 74 61 5b 69 50 61 67 65 5d 20 3d 20 28 75 33 ata[iPage] = (u3
1963b 32 20 76 6f 6c 61 74 69 6c 65 20 2a 29 73 71 6c 2 volatile *)sql
1963c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 57 ite3MallocZero(W
1963d 41 4c 49 4e 44 45 58 5f 50 47 53 5a 29 3b 0a 20 ALINDEX_PGSZ);.
1963e 20 20 20 20 20 69 66 28 20 21 70 57 61 6c 2d 3e if( !pWal->
1963f 61 70 57 69 44 61 74 61 5b 69 50 61 67 65 5d 20 apWiData[iPage]
19640 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f ) rc = SQLITE_NO
19641 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a MEM;. }else{.
19642 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
19643 65 33 4f 73 53 68 6d 4d 61 70 28 70 57 61 6c 2d e3OsShmMap(pWal-
19644 3e 70 44 62 46 64 2c 20 69 50 61 67 65 2c 20 57 >pDbFd, iPage, W
19645 41 4c 49 4e 44 45 58 5f 50 47 53 5a 2c 20 0a 20 ALINDEX_PGSZ, .
19646 20 20 20 20 20 20 20 20 20 70 57 61 6c 2d 3e 77 pWal->w
19647 72 69 74 65 4c 6f 63 6b 2c 20 28 76 6f 69 64 20 riteLock, (void
19648 76 6f 6c 61 74 69 6c 65 20 2a 2a 29 26 70 57 61 volatile **)&pWa
19649 6c 2d 3e 61 70 57 69 44 61 74 61 5b 69 50 61 67 l->apWiData[iPag
1964a 65 5d 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 e]. );.
1964b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1964c 5f 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 _READONLY ){.
1964d 20 20 20 20 20 70 57 61 6c 2d 3e 72 65 61 64 4f pWal->readO
1964e 6e 6c 79 20 7c 3d 20 57 41 4c 5f 53 48 4d 5f 52 nly |= WAL_SHM_R
1964f 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 20 20 72 DONLY;. r
19650 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
19651 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
19652 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 57 .. *ppPage = pW
19653 61 6c 2d 3e 61 70 57 69 44 61 74 61 5b 69 50 61 al->apWiData[iPa
19654 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 ge];. assert( i
19655 50 61 67 65 3d 3d 30 20 7c 7c 20 2a 70 70 50 61 Page==0 || *ppPa
19656 67 65 20 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 ge || rc!=SQLITE
19657 5f 4f 4b 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 _OK );. return
19658 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 rc;.}../*.** Ret
19659 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
1965a 20 74 68 65 20 57 61 6c 43 6b 70 74 49 6e 66 6f the WalCkptInfo
1965b 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 74 68 structure in th
1965c 65 20 77 61 6c 2d 69 6e 64 65 78 2e 0a 2a 2f 0a e wal-index..*/.
1965d 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65 20 static volatile
1965e 57 61 6c 43 6b 70 74 49 6e 66 6f 20 2a 77 61 6c WalCkptInfo *wal
1965f 43 6b 70 74 49 6e 66 6f 28 57 61 6c 20 2a 70 57 CkptInfo(Wal *pW
19660 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 al){. assert( p
19661 57 61 6c 2d 3e 6e 57 69 44 61 74 61 3e 30 20 26 Wal->nWiData>0 &
19662 26 20 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 61 & pWal->apWiData
19663 5b 30 5d 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 [0] );. return
19664 28 76 6f 6c 61 74 69 6c 65 20 57 61 6c 43 6b 70 (volatile WalCkp
19665 74 49 6e 66 6f 2a 29 26 28 70 57 61 6c 2d 3e 61 tInfo*)&(pWal->a
19666 70 57 69 44 61 74 61 5b 30 5d 5b 73 69 7a 65 6f pWiData[0][sizeo
19667 66 28 57 61 6c 49 6e 64 65 78 48 64 72 29 2f 32 f(WalIndexHdr)/2
19668 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ]);.}../*.** Ret
19669 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
1966a 20 74 68 65 20 57 61 6c 49 6e 64 65 78 48 64 72 the WalIndexHdr
1966b 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 74 68 structure in th
1966c 65 20 77 61 6c 2d 69 6e 64 65 78 2e 0a 2a 2f 0a e wal-index..*/.
1966d 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65 20 static volatile
1966e 57 61 6c 49 6e 64 65 78 48 64 72 20 2a 77 61 6c WalIndexHdr *wal
1966f 49 6e 64 65 78 48 64 72 28 57 61 6c 20 2a 70 57 IndexHdr(Wal *pW
19670 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 al){. assert( p
19671 57 61 6c 2d 3e 6e 57 69 44 61 74 61 3e 30 20 26 Wal->nWiData>0 &
19672 26 20 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 61 & pWal->apWiData
19673 5b 30 5d 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 [0] );. return
19674 28 76 6f 6c 61 74 69 6c 65 20 57 61 6c 49 6e 64 (volatile WalInd
19675 65 78 48 64 72 2a 29 70 57 61 6c 2d 3e 61 70 57 exHdr*)pWal->apW
19676 69 44 61 74 61 5b 30 5d 3b 0a 7d 0a 0a 2f 2a 0a iData[0];.}../*.
19677 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 ** The argument
19678 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 6d 75 to this macro mu
19679 73 74 20 62 65 20 6f 66 20 74 79 70 65 20 75 33 st be of type u3
1967a 32 2e 20 4f 6e 20 61 20 6c 69 74 74 6c 65 2d 65 2. On a little-e
1967b 6e 64 69 61 6e 0a 2a 2a 20 61 72 63 68 69 74 65 ndian.** archite
1967c 63 74 75 72 65 2c 20 69 74 20 72 65 74 75 72 6e cture, it return
1967d 73 20 74 68 65 20 75 33 32 20 76 61 6c 75 65 20 s the u32 value
1967e 74 68 61 74 20 72 65 73 75 6c 74 73 20 66 72 6f that results fro
1967f 6d 20 69 6e 74 65 72 70 72 65 74 69 6e 67 0a 2a m interpreting.*
19680 2a 20 74 68 65 20 34 20 62 79 74 65 73 20 61 73 * the 4 bytes as
19681 20 61 20 62 69 67 2d 65 6e 64 69 61 6e 20 76 61 a big-endian va
19682 6c 75 65 2e 20 4f 6e 20 61 20 62 69 67 2d 65 6e lue. On a big-en
19683 64 69 61 6e 20 61 72 63 68 69 74 65 63 74 75 72 dian architectur
19684 65 2c 20 69 74 0a 2a 2a 20 72 65 74 75 72 6e 73 e, it.** returns
19685 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 the value that
19686 77 6f 75 6c 64 20 62 65 20 70 72 6f 64 75 63 65 would be produce
19687 64 20 62 79 20 69 6e 74 65 70 72 65 74 69 6e 67 d by intepreting
19688 20 74 68 65 20 34 20 62 79 74 65 73 0a 2a 2a 20 the 4 bytes.**
19689 6f 66 20 74 68 65 20 69 6e 70 75 74 20 76 61 6c of the input val
1968a 75 65 20 61 73 20 61 20 6c 69 74 74 6c 65 2d 65 ue as a little-e
1968b 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a ndian integer..*
1968c 2f 0a 23 64 65 66 69 6e 65 20 42 59 54 45 53 57 /.#define BYTESW
1968d 41 50 33 32 28 78 29 20 28 20 5c 0a 20 20 20 20 AP32(x) ( \.
1968e 28 28 28 78 29 26 30 78 30 30 30 30 30 30 46 46 (((x)&0x000000FF
1968f 29 3c 3c 32 34 29 20 2b 20 28 28 28 78 29 26 30 )<<24) + (((x)&0
19690 78 30 30 30 30 46 46 30 30 29 3c 3c 38 29 20 20 x0000FF00)<<8)
19691 5c 0a 20 20 2b 20 28 28 28 78 29 26 30 78 30 30 \. + (((x)&0x00
19692 46 46 30 30 30 30 29 3e 3e 38 29 20 20 2b 20 28 FF0000)>>8) + (
19693 28 28 78 29 26 30 78 46 46 30 30 30 30 30 30 29 ((x)&0xFF000000)
19694 3e 3e 32 34 29 20 5c 0a 29 0a 0a 2f 2a 0a 2a 2a >>24) \.)../*.**
19695 20 47 65 6e 65 72 61 74 65 20 6f 72 20 65 78 74 Generate or ext
19696 65 6e 64 20 61 6e 20 38 20 62 79 74 65 20 63 68 end an 8 byte ch
19697 65 63 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 20 ecksum based on
19698 74 68 65 20 64 61 74 61 20 69 6e 20 0a 2a 2a 20 the data in .**
19699 61 72 72 61 79 20 61 42 79 74 65 5b 5d 20 61 6e array aByte[] an
1969a 64 20 74 68 65 20 69 6e 69 74 69 61 6c 20 76 61 d the initial va
1969b 6c 75 65 73 20 6f 66 20 61 49 6e 5b 30 5d 20 61 lues of aIn[0] a
1969c 6e 64 20 61 49 6e 5b 31 5d 20 28 6f 72 0a 2a 2a nd aIn[1] (or.**
1969d 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 73 20 initial values
1969e 6f 66 20 30 20 61 6e 64 20 30 20 69 66 20 61 49 of 0 and 0 if aI
1969f 6e 3d 3d 4e 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20 n==NULL)..**.**
196a0 54 68 65 20 63 68 65 63 6b 73 75 6d 20 69 73 20 The checksum is
196a1 77 72 69 74 74 65 6e 20 62 61 63 6b 20 69 6e 74 written back int
196a2 6f 20 61 4f 75 74 5b 5d 20 62 65 66 6f 72 65 20 o aOut[] before
196a3 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a returning..**.**
196a4 20 6e 42 79 74 65 20 6d 75 73 74 20 62 65 20 61 nByte must be a
196a5 20 70 6f 73 69 74 69 76 65 20 6d 75 6c 74 69 70 positive multip
196a6 6c 65 20 6f 66 20 38 2e 0a 2a 2f 0a 73 74 61 74 le of 8..*/.stat
196a7 69 63 20 76 6f 69 64 20 77 61 6c 43 68 65 63 6b ic void walCheck
196a8 73 75 6d 42 79 74 65 73 28 0a 20 20 69 6e 74 20 sumBytes(. int
196a9 6e 61 74 69 76 65 43 6b 73 75 6d 2c 20 2f 2a 20 nativeCksum, /*
196aa 54 72 75 65 20 66 6f 72 20 6e 61 74 69 76 65 20 True for native
196ab 62 79 74 65 2d 6f 72 64 65 72 2c 20 66 61 6c 73 byte-order, fals
196ac 65 20 66 6f 72 20 6e 6f 6e 2d 6e 61 74 69 76 65 e for non-native
196ad 20 2a 2f 0a 20 20 75 38 20 2a 61 2c 20 20 20 20 */. u8 *a,
196ae 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e /* Conten
196af 74 20 74 6f 20 62 65 20 63 68 65 63 6b 73 75 6d t to be checksum
196b0 6d 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 med */. int nBy
196b1 74 65 2c 20 20 20 20 20 20 20 2f 2a 20 42 79 74 te, /* Byt
196b2 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e es of content in
196b3 20 61 5b 5d 2e 20 20 4d 75 73 74 20 62 65 20 61 a[]. Must be a
196b4 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e 20 multiple of 8.
196b5 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 33 32 20 2a */. const u32 *
196b6 61 49 6e 2c 20 20 2f 2a 20 49 6e 69 74 69 61 6c aIn, /* Initial
196b7 20 63 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 20 checksum value
196b8 69 6e 70 75 74 20 2a 2f 0a 20 20 75 33 32 20 2a input */. u32 *
196b9 61 4f 75 74 20 20 20 20 20 20 20 20 2f 2a 20 4f aOut /* O
196ba 55 54 3a 20 46 69 6e 61 6c 20 63 68 65 63 6b 73 UT: Final checks
196bb 75 6d 20 76 61 6c 75 65 20 6f 75 74 70 75 74 20 um value output
196bc 2a 2f 0a 29 7b 0a 20 20 75 33 32 20 73 31 2c 20 */.){. u32 s1,
196bd 73 32 3b 0a 20 20 75 33 32 20 2a 61 44 61 74 61 s2;. u32 *aData
196be 20 3d 20 28 75 33 32 20 2a 29 61 3b 0a 20 20 75 = (u32 *)a;. u
196bf 33 32 20 2a 61 45 6e 64 20 3d 20 28 75 33 32 20 32 *aEnd = (u32
196c0 2a 29 26 61 5b 6e 42 79 74 65 5d 3b 0a 0a 20 20 *)&a[nByte];..
196c1 69 66 28 20 61 49 6e 20 29 7b 0a 20 20 20 20 73 if( aIn ){. s
196c2 31 20 3d 20 61 49 6e 5b 30 5d 3b 0a 20 20 20 20 1 = aIn[0];.
196c3 73 32 20 3d 20 61 49 6e 5b 31 5d 3b 0a 20 20 7d s2 = aIn[1];. }
196c4 65 6c 73 65 7b 0a 20 20 20 20 73 31 20 3d 20 73 else{. s1 = s
196c5 32 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 61 73 2 = 0;. }.. as
196c6 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 38 20 29 sert( nByte>=8 )
196c7 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 42 79 ;. assert( (nBy
196c8 74 65 26 30 78 30 30 30 30 30 30 30 37 29 3d 3d te&0x00000007)==
196c9 30 20 29 3b 0a 0a 20 20 69 66 28 20 6e 61 74 69 0 );.. if( nati
196ca 76 65 43 6b 73 75 6d 20 29 7b 0a 20 20 20 20 64 veCksum ){. d
196cb 6f 20 7b 0a 20 20 20 20 20 20 73 31 20 2b 3d 20 o {. s1 +=
196cc 2a 61 44 61 74 61 2b 2b 20 2b 20 73 32 3b 0a 20 *aData++ + s2;.
196cd 20 20 20 20 20 73 32 20 2b 3d 20 2a 61 44 61 74 s2 += *aDat
196ce 61 2b 2b 20 2b 20 73 31 3b 0a 20 20 20 20 7d 77 a++ + s1;. }w
196cf 68 69 6c 65 28 20 61 44 61 74 61 3c 61 45 6e 64 hile( aData<aEnd
196d0 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 );. }else{.
196d1 20 64 6f 20 7b 0a 20 20 20 20 20 20 73 31 20 2b do {. s1 +
196d2 3d 20 42 59 54 45 53 57 41 50 33 32 28 61 44 61 = BYTESWAP32(aDa
196d3 74 61 5b 30 5d 29 20 2b 20 73 32 3b 0a 20 20 20 ta[0]) + s2;.
196d4 20 20 20 73 32 20 2b 3d 20 42 59 54 45 53 57 41 s2 += BYTESWA
196d5 50 33 32 28 61 44 61 74 61 5b 31 5d 29 20 2b 20 P32(aData[1]) +
196d6 73 31 3b 0a 20 20 20 20 20 20 61 44 61 74 61 20 s1;. aData
196d7 2b 3d 20 32 3b 0a 20 20 20 20 7d 77 68 69 6c 65 += 2;. }while
196d8 28 20 61 44 61 74 61 3c 61 45 6e 64 20 29 3b 0a ( aData<aEnd );.
196d9 20 20 7d 0a 0a 20 20 61 4f 75 74 5b 30 5d 20 3d }.. aOut[0] =
196da 20 73 31 3b 0a 20 20 61 4f 75 74 5b 31 5d 20 3d s1;. aOut[1] =
196db 20 73 32 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 s2;.}..static v
196dc 6f 69 64 20 77 61 6c 53 68 6d 42 61 72 72 69 65 oid walShmBarrie
196dd 72 28 57 61 6c 20 2a 70 57 61 6c 29 7b 0a 20 20 r(Wal *pWal){.
196de 69 66 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 if( pWal->exclus
196df 69 76 65 4d 6f 64 65 21 3d 57 41 4c 5f 48 45 41 iveMode!=WAL_HEA
196e0 50 4d 45 4d 4f 52 59 5f 4d 4f 44 45 20 29 7b 0a PMEMORY_MODE ){.
196e1 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 68 6d sqlite3OsShm
196e2 42 61 72 72 69 65 72 28 70 57 61 6c 2d 3e 70 44 Barrier(pWal->pD
196e3 62 46 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a bFd);. }.}../*.
196e4 2a 2a 20 57 72 69 74 65 20 74 68 65 20 68 65 61 ** Write the hea
196e5 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 der information
196e6 69 6e 20 70 57 61 6c 2d 3e 68 64 72 20 69 6e 74 in pWal->hdr int
196e7 6f 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 2e o the wal-index.
196e8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 68 65 63 6b .**.** The check
196e9 73 75 6d 20 6f 6e 20 70 57 61 6c 2d 3e 68 64 72 sum on pWal->hdr
196ea 20 69 73 20 75 70 64 61 74 65 64 20 62 65 66 6f is updated befo
196eb 72 65 20 69 74 20 69 73 20 77 72 69 74 74 65 6e re it is written
196ec 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
196ed 20 77 61 6c 49 6e 64 65 78 57 72 69 74 65 48 64 walIndexWriteHd
196ee 72 28 57 61 6c 20 2a 70 57 61 6c 29 7b 0a 20 20 r(Wal *pWal){.
196ef 76 6f 6c 61 74 69 6c 65 20 57 61 6c 49 6e 64 65 volatile WalInde
196f0 78 48 64 72 20 2a 61 48 64 72 20 3d 20 77 61 6c xHdr *aHdr = wal
196f1 49 6e 64 65 78 48 64 72 28 70 57 61 6c 29 3b 0a IndexHdr(pWal);.
196f2 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 43 6b 73 const int nCks
196f3 75 6d 20 3d 20 6f 66 66 73 65 74 6f 66 28 57 61 um = offsetof(Wa
196f4 6c 49 6e 64 65 78 48 64 72 2c 20 61 43 6b 73 75 lIndexHdr, aCksu
196f5 6d 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 m);.. assert( p
196f6 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20 29 Wal->writeLock )
196f7 3b 0a 20 20 70 57 61 6c 2d 3e 68 64 72 2e 69 73 ;. pWal->hdr.is
196f8 49 6e 69 74 20 3d 20 31 3b 0a 20 20 70 57 61 6c Init = 1;. pWal
196f9 2d 3e 68 64 72 2e 69 56 65 72 73 69 6f 6e 20 3d ->hdr.iVersion =
196fa 20 57 41 4c 49 4e 44 45 58 5f 4d 41 58 5f 56 45 WALINDEX_MAX_VE
196fb 52 53 49 4f 4e 3b 0a 20 20 77 61 6c 43 68 65 63 RSION;. walChec
196fc 6b 73 75 6d 42 79 74 65 73 28 31 2c 20 28 75 38 ksumBytes(1, (u8
196fd 2a 29 26 70 57 61 6c 2d 3e 68 64 72 2c 20 6e 43 *)&pWal->hdr, nC
196fe 6b 73 75 6d 2c 20 30 2c 20 70 57 61 6c 2d 3e 68 ksum, 0, pWal->h
196ff 64 72 2e 61 43 6b 73 75 6d 29 3b 0a 20 20 6d 65 dr.aCksum);. me
19700 6d 63 70 79 28 28 76 6f 69 64 20 2a 29 26 61 48 mcpy((void *)&aH
19701 64 72 5b 31 5d 2c 20 28 76 6f 69 64 20 2a 29 26 dr[1], (void *)&
19702 70 57 61 6c 2d 3e 68 64 72 2c 20 73 69 7a 65 6f pWal->hdr, sizeo
19703 66 28 57 61 6c 49 6e 64 65 78 48 64 72 29 29 3b f(WalIndexHdr));
19704 0a 20 20 77 61 6c 53 68 6d 42 61 72 72 69 65 72 . walShmBarrier
19705 28 70 57 61 6c 29 3b 0a 20 20 6d 65 6d 63 70 79 (pWal);. memcpy
19706 28 28 76 6f 69 64 20 2a 29 26 61 48 64 72 5b 30 ((void *)&aHdr[0
19707 5d 2c 20 28 76 6f 69 64 20 2a 29 26 70 57 61 6c ], (void *)&pWal
19708 2d 3e 68 64 72 2c 20 73 69 7a 65 6f 66 28 57 61 ->hdr, sizeof(Wa
19709 6c 49 6e 64 65 78 48 64 72 29 29 3b 0a 7d 0a 0a lIndexHdr));.}..
1970a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
1970b 69 6f 6e 20 65 6e 63 6f 64 65 73 20 61 20 73 69 ion encodes a si
1970c 6e 67 6c 65 20 66 72 61 6d 65 20 68 65 61 64 65 ngle frame heade
1970d 72 20 61 6e 64 20 77 72 69 74 65 73 20 69 74 20 r and writes it
1970e 74 6f 20 61 20 62 75 66 66 65 72 0a 2a 2a 20 73 to a buffer.** s
1970f 75 70 70 6c 69 65 64 20 62 79 20 74 68 65 20 63 upplied by the c
19710 61 6c 6c 65 72 2e 20 41 20 66 72 61 6d 65 2d 68 aller. A frame-h
19711 65 61 64 65 72 20 69 73 20 6d 61 64 65 20 75 70 eader is made up
19712 20 6f 66 20 61 20 73 65 72 69 65 73 20 6f 66 20 of a series of
19713 0a 2a 2a 20 34 2d 62 79 74 65 20 62 69 67 2d 65 .** 4-byte big-e
19714 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 73 2c 20 ndian integers,
19715 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
19716 2a 20 20 20 20 20 30 3a 20 50 61 67 65 20 6e 75 * 0: Page nu
19717 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 34 3a 20 mber..** 4:
19718 46 6f 72 20 63 6f 6d 6d 69 74 20 72 65 63 6f 72 For commit recor
19719 64 73 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 ds, the size of
1971a 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 the database ima
1971b 67 65 20 69 6e 20 70 61 67 65 73 20 0a 2a 2a 20 ge in pages .**
1971c 20 20 20 20 20 20 20 61 66 74 65 72 20 74 68 65 after the
1971d 20 63 6f 6d 6d 69 74 2e 20 46 6f 72 20 61 6c 6c commit. For all
1971e 20 6f 74 68 65 72 20 72 65 63 6f 72 64 73 2c 20 other records,
1971f 7a 65 72 6f 2e 0a 2a 2a 20 20 20 20 20 38 3a 20 zero..** 8:
19720 53 61 6c 74 2d 31 20 28 63 6f 70 69 65 64 20 66 Salt-1 (copied f
19721 72 6f 6d 20 74 68 65 20 77 61 6c 2d 68 65 61 64 rom the wal-head
19722 65 72 29 0a 2a 2a 20 20 20 20 31 32 3a 20 53 61 er).** 12: Sa
19723 6c 74 2d 32 20 28 63 6f 70 69 65 64 20 66 72 6f lt-2 (copied fro
19724 6d 20 74 68 65 20 77 61 6c 2d 68 65 61 64 65 72 m the wal-header
19725 29 0a 2a 2a 20 20 20 20 31 36 3a 20 43 68 65 63 ).** 16: Chec
19726 6b 73 75 6d 2d 31 2e 0a 2a 2a 20 20 20 20 32 30 ksum-1..** 20
19727 3a 20 43 68 65 63 6b 73 75 6d 2d 32 2e 0a 2a 2f : Checksum-2..*/
19728 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 61 6c .static void wal
19729 45 6e 63 6f 64 65 46 72 61 6d 65 28 0a 20 20 57 EncodeFrame(. W
1972a 61 6c 20 2a 70 57 61 6c 2c 20 20 20 20 20 20 20 al *pWal,
1972b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1972c 2a 20 54 68 65 20 77 72 69 74 65 2d 61 68 65 61 * The write-ahea
1972d 64 20 6c 6f 67 20 2a 2f 0a 20 20 75 33 32 20 69 d log */. u32 i
1972e 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 Page,
1972f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 /* Da
19730 74 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d 62 tabase page numb
19731 65 72 20 66 6f 72 20 66 72 61 6d 65 20 2a 2f 0a er for frame */.
19732 20 20 75 33 32 20 6e 54 72 75 6e 63 61 74 65 2c u32 nTruncate,
19733 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19734 20 20 2f 2a 20 4e 65 77 20 64 62 20 73 69 7a 65 /* New db size
19735 20 28 6f 72 20 30 20 66 6f 72 20 6e 6f 6e 2d 63 (or 0 for non-c
19736 6f 6d 6d 69 74 20 66 72 61 6d 65 73 29 20 2a 2f ommit frames) */
19737 0a 20 20 75 38 20 2a 61 44 61 74 61 2c 20 20 20 . u8 *aData,
19738 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19739 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1973a 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 page data */.
1973b 75 38 20 2a 61 46 72 61 6d 65 20 20 20 20 20 20 u8 *aFrame
1973c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1973d 2f 2a 20 4f 55 54 3a 20 57 72 69 74 65 20 65 6e /* OUT: Write en
1973e 63 6f 64 65 64 20 66 72 61 6d 65 20 68 65 72 65 coded frame here
1973f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 61 74 */.){. int nat
19740 69 76 65 43 6b 73 75 6d 3b 20 20 20 20 20 20 20 iveCksum;
19741 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
19742 20 66 6f 72 20 6e 61 74 69 76 65 20 62 79 74 65 for native byte
19743 2d 6f 72 64 65 72 20 63 68 65 63 6b 73 75 6d 73 -order checksums
19744 20 2a 2f 0a 20 20 75 33 32 20 2a 61 43 6b 73 75 */. u32 *aCksu
19745 6d 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 m = pWal->hdr.aF
19746 72 61 6d 65 43 6b 73 75 6d 3b 0a 20 20 61 73 73 rameCksum;. ass
19747 65 72 74 28 20 57 41 4c 5f 46 52 41 4d 45 5f 48 ert( WAL_FRAME_H
19748 44 52 53 49 5a 45 3d 3d 32 34 20 29 3b 0a 20 20 DRSIZE==24 );.
19749 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 sqlite3Put4byte(
1974a 26 61 46 72 61 6d 65 5b 30 5d 2c 20 69 50 61 67 &aFrame[0], iPag
1974b 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 75 74 e);. sqlite3Put
1974c 34 62 79 74 65 28 26 61 46 72 61 6d 65 5b 34 5d 4byte(&aFrame[4]
1974d 2c 20 6e 54 72 75 6e 63 61 74 65 29 3b 0a 20 20 , nTruncate);.
1974e 6d 65 6d 63 70 79 28 26 61 46 72 61 6d 65 5b 38 memcpy(&aFrame[8
1974f 5d 2c 20 70 57 61 6c 2d 3e 68 64 72 2e 61 53 61 ], pWal->hdr.aSa
19750 6c 74 2c 20 38 29 3b 0a 0a 20 20 6e 61 74 69 76 lt, 8);.. nativ
19751 65 43 6b 73 75 6d 20 3d 20 28 70 57 61 6c 2d 3e eCksum = (pWal->
19752 68 64 72 2e 62 69 67 45 6e 64 43 6b 73 75 6d 3d hdr.bigEndCksum=
19753 3d 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 =SQLITE_BIGENDIA
19754 4e 29 3b 0a 20 20 77 61 6c 43 68 65 63 6b 73 75 N);. walChecksu
19755 6d 42 79 74 65 73 28 6e 61 74 69 76 65 43 6b 73 mBytes(nativeCks
19756 75 6d 2c 20 61 46 72 61 6d 65 2c 20 38 2c 20 61 um, aFrame, 8, a
19757 43 6b 73 75 6d 2c 20 61 43 6b 73 75 6d 29 3b 0a Cksum, aCksum);.
19758 20 20 77 61 6c 43 68 65 63 6b 73 75 6d 42 79 74 walChecksumByt
19759 65 73 28 6e 61 74 69 76 65 43 6b 73 75 6d 2c 20 es(nativeCksum,
1975a 61 44 61 74 61 2c 20 70 57 61 6c 2d 3e 73 7a 50 aData, pWal->szP
1975b 61 67 65 2c 20 61 43 6b 73 75 6d 2c 20 61 43 6b age, aCksum, aCk
1975c 73 75 6d 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 sum);.. sqlite3
1975d 50 75 74 34 62 79 74 65 28 26 61 46 72 61 6d 65 Put4byte(&aFrame
1975e 5b 31 36 5d 2c 20 61 43 6b 73 75 6d 5b 30 5d 29 [16], aCksum[0])
1975f 3b 0a 20 20 73 71 6c 69 74 65 33 50 75 74 34 62 ;. sqlite3Put4b
19760 79 74 65 28 26 61 46 72 61 6d 65 5b 32 30 5d 2c yte(&aFrame[20],
19761 20 61 43 6b 73 75 6d 5b 31 5d 29 3b 0a 7d 0a 0a aCksum[1]);.}..
19762 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 /*.** Check to s
19763 65 65 20 69 66 20 74 68 65 20 66 72 61 6d 65 20 ee if the frame
19764 77 69 74 68 20 68 65 61 64 65 72 20 69 6e 20 61 with header in a
19765 46 72 61 6d 65 5b 5d 20 61 6e 64 20 63 6f 6e 74 Frame[] and cont
19766 65 6e 74 0a 2a 2a 20 69 6e 20 61 44 61 74 61 5b ent.** in aData[
19767 5d 20 69 73 20 76 61 6c 69 64 2e 20 20 49 66 20 ] is valid. If
19768 69 74 20 69 73 20 61 20 76 61 6c 69 64 20 66 72 it is a valid fr
19769 61 6d 65 2c 20 66 69 6c 6c 20 2a 70 69 50 61 67 ame, fill *piPag
1976a 65 20 61 6e 64 0a 2a 2a 20 2a 70 6e 54 72 75 6e e and.** *pnTrun
1976b 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 cate and return
1976c 74 72 75 65 2e 20 20 52 65 74 75 72 6e 20 69 66 true. Return if
1976d 20 74 68 65 20 66 72 61 6d 65 20 69 73 20 6e 6f the frame is no
1976e 74 20 76 61 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74 t valid..*/.stat
1976f 69 63 20 69 6e 74 20 77 61 6c 44 65 63 6f 64 65 ic int walDecode
19770 46 72 61 6d 65 28 0a 20 20 57 61 6c 20 2a 70 57 Frame(. Wal *pW
19771 61 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 al,
19772 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
19773 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 20 write-ahead log
19774 2a 2f 0a 20 20 75 33 32 20 2a 70 69 50 61 67 65 */. u32 *piPage
19775 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
19776 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 44 61 74 /* OUT: Dat
19777 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65 abase page numbe
19778 72 20 66 6f 72 20 66 72 61 6d 65 20 2a 2f 0a 20 r for frame */.
19779 20 75 33 32 20 2a 70 6e 54 72 75 6e 63 61 74 65 u32 *pnTruncate
1977a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1977b 20 2f 2a 20 4f 55 54 3a 20 4e 65 77 20 64 62 20 /* OUT: New db
1977c 73 69 7a 65 20 28 6f 72 20 30 20 69 66 20 6e 6f size (or 0 if no
1977d 74 20 63 6f 6d 6d 69 74 29 20 2a 2f 0a 20 20 75 t commit) */. u
1977e 38 20 2a 61 44 61 74 61 2c 20 20 20 20 20 20 20 8 *aData,
1977f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19780 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 * Pointer to pag
19781 65 20 64 61 74 61 20 28 66 6f 72 20 63 68 65 63 e data (for chec
19782 6b 73 75 6d 29 20 2a 2f 0a 20 20 75 38 20 2a 61 ksum) */. u8 *a
19783 46 72 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 Frame
19784 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 /* Fr
19785 61 6d 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 ame data */.){.
19786 20 69 6e 74 20 6e 61 74 69 76 65 43 6b 73 75 6d int nativeCksum
19787 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
19788 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6e 61 74 /* True for nat
19789 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20 63 ive byte-order c
1978a 68 65 63 6b 73 75 6d 73 20 2a 2f 0a 20 20 75 33 hecksums */. u3
1978b 32 20 2a 61 43 6b 73 75 6d 20 3d 20 70 57 61 6c 2 *aCksum = pWal
1978c 2d 3e 68 64 72 2e 61 46 72 61 6d 65 43 6b 73 75 ->hdr.aFrameCksu
1978d 6d 3b 0a 20 20 75 33 32 20 70 67 6e 6f 3b 20 20 m;. u32 pgno;
1978e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1978f 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d /* Page num
19790 62 65 72 20 6f 66 20 74 68 65 20 66 72 61 6d 65 ber of the frame
19791 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 57 41 */. assert( WA
19792 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 5a 45 3d L_FRAME_HDRSIZE=
19793 3d 32 34 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 66 =24 );.. /* A f
19794 72 61 6d 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c rame is only val
19795 69 64 20 69 66 20 74 68 65 20 73 61 6c 74 20 76 id if the salt v
19796 61 6c 75 65 73 20 69 6e 20 74 68 65 20 66 72 61 alues in the fra
19797 6d 65 2d 68 65 61 64 65 72 0a 20 20 2a 2a 20 6d me-header. ** m
19798 61 74 63 68 20 74 68 65 20 73 61 6c 74 20 76 61 atch the salt va
19799 6c 75 65 73 20 69 6e 20 74 68 65 20 77 61 6c 2d lues in the wal-
1979a 68 65 61 64 65 72 2e 20 0a 20 20 2a 2f 0a 20 20 header. . */.
1979b 69 66 28 20 6d 65 6d 63 6d 70 28 26 70 57 61 6c if( memcmp(&pWal
1979c 2d 3e 68 64 72 2e 61 53 61 6c 74 2c 20 26 61 46 ->hdr.aSalt, &aF
1979d 72 61 6d 65 5b 38 5d 2c 20 38 29 21 3d 30 20 29 rame[8], 8)!=0 )
1979e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
1979f 20 20 7d 0a 0a 20 20 2f 2a 20 41 20 66 72 61 6d }.. /* A fram
197a0 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 e is only valid
197a1 69 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 if the page numb
197a2 65 72 20 69 73 20 63 72 65 61 74 65 72 20 74 68 er is creater th
197a3 61 6e 20 7a 65 72 6f 2e 0a 20 20 2a 2f 0a 20 20 an zero.. */.
197a4 70 67 6e 6f 20 3d 20 73 71 6c 69 74 65 33 47 65 pgno = sqlite3Ge
197a5 74 34 62 79 74 65 28 26 61 46 72 61 6d 65 5b 30 t4byte(&aFrame[0
197a6 5d 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d ]);. if( pgno==
197a7 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
197a8 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 20 66 0;. }.. /* A f
197a9 72 61 6d 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c rame is only val
197aa 69 64 20 69 66 20 61 20 63 68 65 63 6b 73 75 6d id if a checksum
197ab 20 6f 66 20 74 68 65 20 57 41 4c 20 68 65 61 64 of the WAL head
197ac 65 72 2c 0a 20 20 2a 2a 20 61 6c 6c 20 70 72 69 er,. ** all pri
197ad 6f 72 20 66 72 61 6d 73 2c 20 74 68 65 20 66 69 or frams, the fi
197ae 72 73 74 20 31 36 20 62 79 74 65 73 20 6f 66 20 rst 16 bytes of
197af 74 68 69 73 20 66 72 61 6d 65 2d 68 65 61 64 65 this frame-heade
197b0 72 2c 20 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 r, . ** and the
197b1 20 66 72 61 6d 65 2d 64 61 74 61 20 6d 61 74 63 frame-data matc
197b2 68 65 73 20 74 68 65 20 63 68 65 63 6b 73 75 6d hes the checksum
197b3 20 69 6e 20 74 68 65 20 6c 61 73 74 20 38 20 0a in the last 8 .
197b4 20 20 2a 2a 20 62 79 74 65 73 20 6f 66 20 74 68 ** bytes of th
197b5 69 73 20 66 72 61 6d 65 2d 68 65 61 64 65 72 2e is frame-header.
197b6 0a 20 20 2a 2f 0a 20 20 6e 61 74 69 76 65 43 6b . */. nativeCk
197b7 73 75 6d 20 3d 20 28 70 57 61 6c 2d 3e 68 64 72 sum = (pWal->hdr
197b8 2e 62 69 67 45 6e 64 43 6b 73 75 6d 3d 3d 53 51 .bigEndCksum==SQ
197b9 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 29 3b LITE_BIGENDIAN);
197ba 0a 20 20 77 61 6c 43 68 65 63 6b 73 75 6d 42 79 . walChecksumBy
197bb 74 65 73 28 6e 61 74 69 76 65 43 6b 73 75 6d 2c tes(nativeCksum,
197bc 20 61 46 72 61 6d 65 2c 20 38 2c 20 61 43 6b 73 aFrame, 8, aCks
197bd 75 6d 2c 20 61 43 6b 73 75 6d 29 3b 0a 20 20 77 um, aCksum);. w
197be 61 6c 43 68 65 63 6b 73 75 6d 42 79 74 65 73 28 alChecksumBytes(
197bf 6e 61 74 69 76 65 43 6b 73 75 6d 2c 20 61 44 61 nativeCksum, aDa
197c0 74 61 2c 20 70 57 61 6c 2d 3e 73 7a 50 61 67 65 ta, pWal->szPage
197c1 2c 20 61 43 6b 73 75 6d 2c 20 61 43 6b 73 75 6d , aCksum, aCksum
197c2 29 3b 0a 20 20 69 66 28 20 61 43 6b 73 75 6d 5b );. if( aCksum[
197c3 30 5d 21 3d 73 71 6c 69 74 65 33 47 65 74 34 62 0]!=sqlite3Get4b
197c4 79 74 65 28 26 61 46 72 61 6d 65 5b 31 36 5d 29 yte(&aFrame[16])
197c5 20 0a 20 20 20 7c 7c 20 61 43 6b 73 75 6d 5b 31 . || aCksum[1
197c6 5d 21 3d 73 71 6c 69 74 65 33 47 65 74 34 62 79 ]!=sqlite3Get4by
197c7 74 65 28 26 61 46 72 61 6d 65 5b 32 30 5d 29 20 te(&aFrame[20])
197c8 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 43 68 65 . ){. /* Che
197c9 63 6b 73 75 6d 20 66 61 69 6c 65 64 2e 20 2a 2f cksum failed. */
197ca 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
197cb 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 }.. /* If we r
197cc 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c each this point,
197cd 20 74 68 65 20 66 72 61 6d 65 20 69 73 20 76 61 the frame is va
197ce 6c 69 64 2e 20 20 52 65 74 75 72 6e 20 74 68 65 lid. Return the
197cf 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a page number. *
197d0 2a 20 61 6e 64 20 74 68 65 20 6e 65 77 20 64 61 * and the new da
197d1 74 61 62 61 73 65 20 73 69 7a 65 2e 0a 20 20 2a tabase size.. *
197d2 2f 0a 20 20 2a 70 69 50 61 67 65 20 3d 20 70 67 /. *piPage = pg
197d3 6e 6f 3b 0a 20 20 2a 70 6e 54 72 75 6e 63 61 74 no;. *pnTruncat
197d4 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62 e = sqlite3Get4b
197d5 79 74 65 28 26 61 46 72 61 6d 65 5b 34 5d 29 3b yte(&aFrame[4]);
197d6 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a . return 1;.}..
197d7 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
197d8 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66 ITE_TEST) && def
197d9 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
197da 47 29 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 73 20 6f G)./*.** Names o
197db 66 20 6c 6f 63 6b 73 2e 20 20 54 68 69 73 20 72 f locks. This r
197dc 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 outine is used t
197dd 6f 20 70 72 6f 76 69 64 65 20 64 65 62 75 67 67 o provide debugg
197de 69 6e 67 20 6f 75 74 70 75 74 20 61 6e 64 20 69 ing output and i
197df 73 20 6e 6f 74 0a 2a 2a 20 61 20 70 61 72 74 20 s not.** a part
197e0 6f 66 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 62 of an ordinary b
197e1 75 69 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 uild..*/.static
197e2 63 6f 6e 73 74 20 63 68 61 72 20 2a 77 61 6c 4c const char *walL
197e3 6f 63 6b 4e 61 6d 65 28 69 6e 74 20 6c 6f 63 6b ockName(int lock
197e4 49 64 78 29 7b 0a 20 20 69 66 28 20 6c 6f 63 6b Idx){. if( lock
197e5 49 64 78 3d 3d 57 41 4c 5f 57 52 49 54 45 5f 4c Idx==WAL_WRITE_L
197e6 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 OCK ){. retur
197e7 6e 20 22 57 52 49 54 45 2d 4c 4f 43 4b 22 3b 0a n "WRITE-LOCK";.
197e8 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b }else if( lock
197e9 49 64 78 3d 3d 57 41 4c 5f 43 4b 50 54 5f 4c 4f Idx==WAL_CKPT_LO
197ea 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e CK ){. return
197eb 20 22 43 4b 50 54 2d 4c 4f 43 4b 22 3b 0a 20 20 "CKPT-LOCK";.
197ec 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 49 64 }else if( lockId
197ed 78 3d 3d 57 41 4c 5f 52 45 43 4f 56 45 52 5f 4c x==WAL_RECOVER_L
197ee 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 OCK ){. retur
197ef 6e 20 22 52 45 43 4f 56 45 52 2d 4c 4f 43 4b 22 n "RECOVER-LOCK"
197f0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
197f1 74 61 74 69 63 20 63 68 61 72 20 7a 4e 61 6d 65 tatic char zName
197f2 5b 31 35 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 [15];. sqlite
197f3 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
197f4 66 28 7a 4e 61 6d 65 29 2c 20 7a 4e 61 6d 65 2c f(zName), zName,
197f5 20 22 52 45 41 44 2d 4c 4f 43 4b 5b 25 64 5d 22 "READ-LOCK[%d]"
197f6 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
197f7 20 20 20 20 20 20 20 6c 6f 63 6b 49 64 78 2d 57 lockIdx-W
197f8 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 30 29 29 AL_READ_LOCK(0))
197f9 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 4e 61 ;. return zNa
197fa 6d 65 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 me;. }.}.#endif
197fb 20 2f 2a 64 65 66 69 6e 65 64 28 53 51 4c 49 54 /*defined(SQLIT
197fc 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e E_TEST) || defin
197fd 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
197fe 20 2a 2f 0a 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 */. ../*.**
197ff 53 65 74 20 6f 72 20 72 65 6c 65 61 73 65 20 6c Set or release l
19800 6f 63 6b 73 20 6f 6e 20 74 68 65 20 57 41 4c 2e ocks on the WAL.
19801 20 20 4c 6f 63 6b 73 20 61 72 65 20 65 69 74 68 Locks are eith
19802 65 72 20 73 68 61 72 65 64 20 6f 72 20 65 78 63 er shared or exc
19803 6c 75 73 69 76 65 2e 0a 2a 2a 20 41 20 6c 6f 63 lusive..** A loc
19804 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 76 65 k cannot be move
19805 64 20 64 69 72 65 63 74 6c 79 20 62 65 74 77 65 d directly betwe
19806 65 6e 20 73 68 61 72 65 64 20 61 6e 64 20 65 78 en shared and ex
19807 63 6c 75 73 69 76 65 20 2d 20 69 74 20 6d 75 73 clusive - it mus
19808 74 20 67 6f 0a 2a 2a 20 74 68 72 6f 75 67 68 20 t go.** through
19809 74 68 65 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61 the unlocked sta
1980a 74 65 20 66 69 72 73 74 2e 0a 2a 2a 0a 2a 2a 20 te first..**.**
1980b 49 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d In locking_mode=
1980c 45 58 43 4c 55 53 49 56 45 2c 20 61 6c 6c 20 6f EXCLUSIVE, all o
1980d 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 f these routines
1980e 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 73 2e 0a become no-ops..
1980f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 */.static int wa
19810 6c 4c 6f 63 6b 53 68 61 72 65 64 28 57 61 6c 20 lLockShared(Wal
19811 2a 70 57 61 6c 2c 20 69 6e 74 20 6c 6f 63 6b 49 *pWal, int lockI
19812 64 78 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 dx){. int rc;.
19813 20 69 66 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75 if( pWal->exclu
19814 73 69 76 65 4d 6f 64 65 20 29 20 72 65 74 75 72 siveMode ) retur
19815 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72 n SQLITE_OK;. r
19816 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 68 6d c = sqlite3OsShm
19817 4c 6f 63 6b 28 70 57 61 6c 2d 3e 70 44 62 46 64 Lock(pWal->pDbFd
19818 2c 20 6c 6f 63 6b 49 64 78 2c 20 31 2c 0a 20 20 , lockIdx, 1,.
19819 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1981a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 48 4d SQLITE_SHM
1981b 5f 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53 _LOCK | SQLITE_S
1981c 48 4d 5f 53 48 41 52 45 44 29 3b 0a 20 20 57 41 HM_SHARED);. WA
1981d 4c 54 52 41 43 45 28 28 22 57 41 4c 25 70 3a 20 LTRACE(("WAL%p:
1981e 61 63 71 75 69 72 65 20 53 48 41 52 45 44 2d 25 acquire SHARED-%
1981f 73 20 25 73 5c 6e 22 2c 20 70 57 61 6c 2c 0a 20 s %s\n", pWal,.
19820 20 20 20 20 20 20 20 20 20 20 20 77 61 6c 4c 6f walLo
19821 63 6b 4e 61 6d 65 28 6c 6f 63 6b 49 64 78 29 2c ckName(lockIdx),
19822 20 72 63 20 3f 20 22 66 61 69 6c 65 64 22 20 3a rc ? "failed" :
19823 20 22 6f 6b 22 29 29 3b 0a 20 20 56 56 41 5f 4f "ok"));. VVA_O
19824 4e 4c 59 28 20 70 57 61 6c 2d 3e 6c 6f 63 6b 45 NLY( pWal->lockE
19825 72 72 6f 72 20 3d 20 28 75 38 29 28 72 63 21 3d rror = (u8)(rc!=
19826 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 SQLITE_OK && rc!
19827 3d 53 51 4c 49 54 45 5f 42 55 53 59 29 3b 20 29 =SQLITE_BUSY); )
19828 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
19829 73 74 61 74 69 63 20 76 6f 69 64 20 77 61 6c 55 static void walU
1982a 6e 6c 6f 63 6b 53 68 61 72 65 64 28 57 61 6c 20 nlockShared(Wal
1982b 2a 70 57 61 6c 2c 20 69 6e 74 20 6c 6f 63 6b 49 *pWal, int lockI
1982c 64 78 29 7b 0a 20 20 69 66 28 20 70 57 61 6c 2d dx){. if( pWal-
1982d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 >exclusiveMode )
1982e 20 72 65 74 75 72 6e 3b 0a 20 20 28 76 6f 69 64 return;. (void
1982f 29 73 71 6c 69 74 65 33 4f 73 53 68 6d 4c 6f 63 )sqlite3OsShmLoc
19830 6b 28 70 57 61 6c 2d 3e 70 44 62 46 64 2c 20 6c k(pWal->pDbFd, l
19831 6f 63 6b 49 64 78 2c 20 31 2c 0a 20 20 20 20 20 ockIdx, 1,.
19832 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19833 20 20 20 20 53 51 4c 49 54 45 5f 53 48 4d 5f 55 SQLITE_SHM_U
19834 4e 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53 NLOCK | SQLITE_S
19835 48 4d 5f 53 48 41 52 45 44 29 3b 0a 20 20 57 41 HM_SHARED);. WA
19836 4c 54 52 41 43 45 28 28 22 57 41 4c 25 70 3a 20 LTRACE(("WAL%p:
19837 72 65 6c 65 61 73 65 20 53 48 41 52 45 44 2d 25 release SHARED-%
19838 73 5c 6e 22 2c 20 70 57 61 6c 2c 20 77 61 6c 4c s\n", pWal, walL
19839 6f 63 6b 4e 61 6d 65 28 6c 6f 63 6b 49 64 78 29 ockName(lockIdx)
1983a 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 ));.}.static int
1983b 20 77 61 6c 4c 6f 63 6b 45 78 63 6c 75 73 69 76 walLockExclusiv
1983c 65 28 57 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 e(Wal *pWal, int
1983d 20 6c 6f 63 6b 49 64 78 2c 20 69 6e 74 20 6e 29 lockIdx, int n)
1983e 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 {. int rc;. if
1983f 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69 76 ( pWal->exclusiv
19840 65 4d 6f 64 65 20 29 20 72 65 74 75 72 6e 20 53 eMode ) return S
19841 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72 63 20 3d QLITE_OK;. rc =
19842 20 73 71 6c 69 74 65 33 4f 73 53 68 6d 4c 6f 63 sqlite3OsShmLoc
19843 6b 28 70 57 61 6c 2d 3e 70 44 62 46 64 2c 20 6c k(pWal->pDbFd, l
19844 6f 63 6b 49 64 78 2c 20 6e 2c 0a 20 20 20 20 20 ockIdx, n,.
19845 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19846 20 20 20 53 51 4c 49 54 45 5f 53 48 4d 5f 4c 4f SQLITE_SHM_LO
19847 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f CK | SQLITE_SHM_
19848 45 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 57 41 EXCLUSIVE);. WA
19849 4c 54 52 41 43 45 28 28 22 57 41 4c 25 70 3a 20 LTRACE(("WAL%p:
1984a 61 63 71 75 69 72 65 20 45 58 43 4c 55 53 49 56 acquire EXCLUSIV
1984b 45 2d 25 73 20 63 6e 74 3d 25 64 20 25 73 5c 6e E-%s cnt=%d %s\n
1984c 22 2c 20 70 57 61 6c 2c 0a 20 20 20 20 20 20 20 ", pWal,.
1984d 20 20 20 20 20 77 61 6c 4c 6f 63 6b 4e 61 6d 65 walLockName
1984e 28 6c 6f 63 6b 49 64 78 29 2c 20 6e 2c 20 72 63 (lockIdx), n, rc
1984f 20 3f 20 22 66 61 69 6c 65 64 22 20 3a 20 22 6f ? "failed" : "o
19850 6b 22 29 29 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 k"));. VVA_ONLY
19851 28 20 70 57 61 6c 2d 3e 6c 6f 63 6b 45 72 72 6f ( pWal->lockErro
19852 72 20 3d 20 28 75 38 29 28 72 63 21 3d 53 51 4c r = (u8)(rc!=SQL
19853 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 ITE_OK && rc!=SQ
19854 4c 49 54 45 5f 42 55 53 59 29 3b 20 29 0a 20 20 LITE_BUSY); ).
19855 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 73 74 61 return rc;.}.sta
19856 74 69 63 20 76 6f 69 64 20 77 61 6c 55 6e 6c 6f tic void walUnlo
19857 63 6b 45 78 63 6c 75 73 69 76 65 28 57 61 6c 20 ckExclusive(Wal
19858 2a 70 57 61 6c 2c 20 69 6e 74 20 6c 6f 63 6b 49 *pWal, int lockI
19859 64 78 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 66 dx, int n){. if
1985a 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69 76 ( pWal->exclusiv
1985b 65 4d 6f 64 65 20 29 20 72 65 74 75 72 6e 3b 0a eMode ) return;.
1985c 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 4f (void)sqlite3O
1985d 73 53 68 6d 4c 6f 63 6b 28 70 57 61 6c 2d 3e 70 sShmLock(pWal->p
1985e 44 62 46 64 2c 20 6c 6f 63 6b 49 64 78 2c 20 6e DbFd, lockIdx, n
1985f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
19860 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 SQLIT
19861 45 5f 53 48 4d 5f 55 4e 4c 4f 43 4b 20 7c 20 53 E_SHM_UNLOCK | S
19862 51 4c 49 54 45 5f 53 48 4d 5f 45 58 43 4c 55 53 QLITE_SHM_EXCLUS
19863 49 56 45 29 3b 0a 20 20 57 41 4c 54 52 41 43 45 IVE);. WALTRACE
19864 28 28 22 57 41 4c 25 70 3a 20 72 65 6c 65 61 73 (("WAL%p: releas
19865 65 20 45 58 43 4c 55 53 49 56 45 2d 25 73 20 63 e EXCLUSIVE-%s c
19866 6e 74 3d 25 64 5c 6e 22 2c 20 70 57 61 6c 2c 0a nt=%d\n", pWal,.
19867 20 20 20 20 20 20 20 20 20 20 20 20 20 77 61 6c wal
19868 4c 6f 63 6b 4e 61 6d 65 28 6c 6f 63 6b 49 64 78 LockName(lockIdx
19869 29 2c 20 6e 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ), n));.}../*.**
1986a 20 43 6f 6d 70 75 74 65 20 61 20 68 61 73 68 20 Compute a hash
1986b 6f 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 on a page number
1986c 2e 20 20 54 68 65 20 72 65 73 75 6c 74 69 6e 67 . The resulting
1986d 20 68 61 73 68 20 76 61 6c 75 65 20 6d 75 73 74 hash value must
1986e 20 6c 61 6e 64 0a 2a 2a 20 62 65 74 77 65 65 6e land.** between
1986f 20 30 20 61 6e 64 20 28 48 41 53 48 54 41 42 4c 0 and (HASHTABL
19870 45 5f 4e 53 4c 4f 54 2d 31 29 2e 20 20 54 68 65 E_NSLOT-1). The
19871 20 77 61 6c 48 61 73 68 4e 65 78 74 28 29 20 66 walHashNext() f
19872 75 6e 63 74 69 6f 6e 20 61 64 76 61 6e 63 65 73 unction advances
19873 0a 2a 2a 20 74 68 65 20 68 61 73 68 20 74 6f 20 .** the hash to
19874 74 68 65 20 6e 65 78 74 20 76 61 6c 75 65 20 69 the next value i
19875 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 n the event of a
19876 20 63 6f 6c 6c 69 73 69 6f 6e 2e 0a 2a 2f 0a 73 collision..*/.s
19877 74 61 74 69 63 20 69 6e 74 20 77 61 6c 48 61 73 tatic int walHas
19878 68 28 75 33 32 20 69 50 61 67 65 29 7b 0a 20 20 h(u32 iPage){.
19879 61 73 73 65 72 74 28 20 69 50 61 67 65 3e 30 20 assert( iPage>0
1987a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 48 41 );. assert( (HA
1987b 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54 20 26 20 SHTABLE_NSLOT &
1987c 28 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54 (HASHTABLE_NSLOT
1987d 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 72 65 74 -1))==0 );. ret
1987e 75 72 6e 20 28 69 50 61 67 65 2a 48 41 53 48 54 urn (iPage*HASHT
1987f 41 42 4c 45 5f 48 41 53 48 5f 31 29 20 26 20 28 ABLE_HASH_1) & (
19880 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54 2d HASHTABLE_NSLOT-
19881 31 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 1);.}.static int
19882 20 77 61 6c 4e 65 78 74 48 61 73 68 28 69 6e 74 walNextHash(int
19883 20 69 50 72 69 6f 72 48 61 73 68 29 7b 0a 20 20 iPriorHash){.
19884 72 65 74 75 72 6e 20 28 69 50 72 69 6f 72 48 61 return (iPriorHa
19885 73 68 2b 31 29 26 28 48 41 53 48 54 41 42 4c 45 sh+1)&(HASHTABLE
19886 5f 4e 53 4c 4f 54 2d 31 29 3b 0a 7d 0a 0a 2f 2a _NSLOT-1);.}../*
19887 20 0a 2a 2a 20 52 65 74 75 72 6e 20 70 6f 69 6e .** Return poin
19888 74 65 72 73 20 74 6f 20 74 68 65 20 68 61 73 68 ters to the hash
19889 20 74 61 62 6c 65 20 61 6e 64 20 70 61 67 65 20 table and page
1988a 6e 75 6d 62 65 72 20 61 72 72 61 79 20 73 74 6f number array sto
1988b 72 65 64 20 6f 6e 0a 2a 2a 20 70 61 67 65 20 69 red on.** page i
1988c 48 61 73 68 20 6f 66 20 74 68 65 20 77 61 6c 2d Hash of the wal-
1988d 69 6e 64 65 78 2e 20 54 68 65 20 77 61 6c 2d 69 index. The wal-i
1988e 6e 64 65 78 20 69 73 20 62 72 6f 6b 65 6e 20 69 ndex is broken i
1988f 6e 74 6f 20 33 32 4b 42 20 70 61 67 65 73 0a 2a nto 32KB pages.*
19890 2a 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 * numbered start
19891 69 6e 67 20 66 72 6f 6d 20 30 2e 0a 2a 2a 0a 2a ing from 0..**.*
19892 2a 20 53 65 74 20 6f 75 74 70 75 74 20 76 61 72 * Set output var
19893 69 61 62 6c 65 20 2a 70 61 48 61 73 68 20 74 6f iable *paHash to
19894 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 74 point to the st
19895 61 72 74 20 6f 66 20 74 68 65 20 68 61 73 68 20 art of the hash
19896 74 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20 table.** in the
19897 77 61 6c 2d 69 6e 64 65 78 20 66 69 6c 65 2e 20 wal-index file.
19898 53 65 74 20 2a 70 69 5a 65 72 6f 20 74 6f 20 6f Set *piZero to o
19899 6e 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 ne less than the
1989a 20 66 72 61 6d 65 20 0a 2a 2a 20 6e 75 6d 62 65 frame .** numbe
1989b 72 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 66 r of the first f
1989c 72 61 6d 65 20 69 6e 64 65 78 65 64 20 62 79 20 rame indexed by
1989d 74 68 69 73 20 68 61 73 68 20 74 61 62 6c 65 2e this hash table.
1989e 20 49 66 20 61 0a 2a 2a 20 73 6c 6f 74 20 69 6e If a.** slot in
1989f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
198a0 69 73 20 73 65 74 20 74 6f 20 4e 2c 20 69 74 20 is set to N, it
198a1 72 65 66 65 72 73 20 74 6f 20 66 72 61 6d 65 20 refers to frame
198a2 6e 75 6d 62 65 72 20 0a 2a 2a 20 28 2a 70 69 5a number .** (*piZ
198a3 65 72 6f 2b 4e 29 20 69 6e 20 74 68 65 20 6c 6f ero+N) in the lo
198a4 67 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 61 6c 6c 79 g..**.** Finally
198a5 2c 20 73 65 74 20 2a 70 61 50 67 6e 6f 20 73 6f , set *paPgno so
198a6 20 74 68 61 74 20 2a 70 61 50 67 6e 6f 5b 31 5d that *paPgno[1]
198a7 20 69 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d is the page num
198a8 62 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 ber of the.** fi
198a9 72 73 74 20 66 72 61 6d 65 20 69 6e 64 65 78 65 rst frame indexe
198aa 64 20 62 79 20 74 68 65 20 68 61 73 68 20 74 61 d by the hash ta
198ab 62 6c 65 2c 20 66 72 61 6d 65 20 28 2a 70 69 5a ble, frame (*piZ
198ac 65 72 6f 2b 31 29 2e 0a 2a 2f 0a 73 74 61 74 69 ero+1)..*/.stati
198ad 63 20 69 6e 74 20 77 61 6c 48 61 73 68 47 65 74 c int walHashGet
198ae 28 0a 20 20 57 61 6c 20 2a 70 57 61 6c 2c 20 20 (. Wal *pWal,
198af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
198b0 20 20 20 20 2f 2a 20 57 41 4c 20 68 61 6e 64 6c /* WAL handl
198b1 65 20 2a 2f 0a 20 20 69 6e 74 20 69 48 61 73 68 e */. int iHash
198b2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
198b3 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 /* Find t
198b4 68 65 20 69 48 61 73 68 27 74 68 20 74 61 62 6c he iHash'th tabl
198b5 65 20 2a 2f 0a 20 20 76 6f 6c 61 74 69 6c 65 20 e */. volatile
198b6 68 74 5f 73 6c 6f 74 20 2a 2a 70 61 48 61 73 68 ht_slot **paHash
198b7 2c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 , /* OUT: P
198b8 6f 69 6e 74 65 72 20 74 6f 20 68 61 73 68 20 69 ointer to hash i
198b9 6e 64 65 78 20 2a 2f 0a 20 20 76 6f 6c 61 74 69 ndex */. volati
198ba 6c 65 20 75 33 32 20 2a 2a 70 61 50 67 6e 6f 2c le u32 **paPgno,
198bb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
198bc 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 : Pointer to pag
198bd 65 20 6e 75 6d 62 65 72 20 61 72 72 61 79 20 2a e number array *
198be 2f 0a 20 20 75 33 32 20 2a 70 69 5a 65 72 6f 20 /. u32 *piZero
198bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
198c0 20 20 20 20 2f 2a 20 4f 55 54 3a 20 46 72 61 6d /* OUT: Fram
198c1 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
198c2 68 20 2a 70 61 50 67 6e 6f 5b 30 5d 20 2a 2f 0a h *paPgno[0] */.
198c3 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
198c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
198c5 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
198c6 6f 64 65 20 2a 2f 0a 20 20 76 6f 6c 61 74 69 6c ode */. volatil
198c7 65 20 75 33 32 20 2a 61 50 67 6e 6f 3b 0a 0a 20 e u32 *aPgno;..
198c8 20 72 63 20 3d 20 77 61 6c 49 6e 64 65 78 50 61 rc = walIndexPa
198c9 67 65 28 70 57 61 6c 2c 20 69 48 61 73 68 2c 20 ge(pWal, iHash,
198ca 26 61 50 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 &aPgno);. asser
198cb 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
198cc 20 7c 7c 20 69 48 61 73 68 3e 30 20 29 3b 0a 0a || iHash>0 );..
198cd 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
198ce 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 33 32 20 69 _OK ){. u32 i
198cf 5a 65 72 6f 3b 0a 20 20 20 20 76 6f 6c 61 74 69 Zero;. volati
198d0 6c 65 20 68 74 5f 73 6c 6f 74 20 2a 61 48 61 73 le ht_slot *aHas
198d1 68 3b 0a 0a 20 20 20 20 61 48 61 73 68 20 3d 20 h;.. aHash =
198d2 28 76 6f 6c 61 74 69 6c 65 20 68 74 5f 73 6c 6f (volatile ht_slo
198d3 74 20 2a 29 26 61 50 67 6e 6f 5b 48 41 53 48 54 t *)&aPgno[HASHT
198d4 41 42 4c 45 5f 4e 50 41 47 45 5d 3b 0a 20 20 20 ABLE_NPAGE];.
198d5 20 69 66 28 20 69 48 61 73 68 3d 3d 30 20 29 7b if( iHash==0 ){
198d6 0a 20 20 20 20 20 20 61 50 67 6e 6f 20 3d 20 26 . aPgno = &
198d7 61 50 67 6e 6f 5b 57 41 4c 49 4e 44 45 58 5f 48 aPgno[WALINDEX_H
198d8 44 52 5f 53 49 5a 45 2f 73 69 7a 65 6f 66 28 75 DR_SIZE/sizeof(u
198d9 33 32 29 5d 3b 0a 20 20 20 20 20 20 69 5a 65 72 32)];. iZer
198da 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 o = 0;. }else
198db 7b 0a 20 20 20 20 20 20 69 5a 65 72 6f 20 3d 20 {. iZero =
198dc 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f HASHTABLE_NPAGE_
198dd 4f 4e 45 20 2b 20 28 69 48 61 73 68 2d 31 29 2a ONE + (iHash-1)*
198de 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 3b HASHTABLE_NPAGE;
198df 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2a 70 . }. . *p
198e0 61 50 67 6e 6f 20 3d 20 26 61 50 67 6e 6f 5b 2d aPgno = &aPgno[-
198e1 31 5d 3b 0a 20 20 20 20 2a 70 61 48 61 73 68 20 1];. *paHash
198e2 3d 20 61 48 61 73 68 3b 0a 20 20 20 20 2a 70 69 = aHash;. *pi
198e3 5a 65 72 6f 20 3d 20 69 5a 65 72 6f 3b 0a 20 20 Zero = iZero;.
198e4 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
198e5 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
198e6 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 he number of the
198e7 20 77 61 6c 2d 69 6e 64 65 78 20 70 61 67 65 20 wal-index page
198e8 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
198e9 65 20 68 61 73 68 2d 74 61 62 6c 65 0a 2a 2a 20 e hash-table.**
198ea 61 6e 64 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 and page-number
198eb 61 72 72 61 79 20 74 68 61 74 20 63 6f 6e 74 61 array that conta
198ec 69 6e 20 65 6e 74 72 69 65 73 20 63 6f 72 72 65 in entries corre
198ed 73 70 6f 6e 64 69 6e 67 20 74 6f 20 57 41 4c 20 sponding to WAL
198ee 66 72 61 6d 65 0a 2a 2a 20 69 46 72 61 6d 65 2e frame.** iFrame.
198ef 20 54 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 69 The wal-index i
198f0 73 20 62 72 6f 6b 65 6e 20 75 70 20 69 6e 74 6f s broken up into
198f1 20 33 32 4b 42 20 70 61 67 65 73 2e 20 57 61 6c 32KB pages. Wal
198f2 2d 69 6e 64 65 78 20 70 61 67 65 73 20 0a 2a 2a -index pages .**
198f3 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 are numbered st
198f4 61 72 74 69 6e 67 20 66 72 6f 6d 20 30 2e 0a 2a arting from 0..*
198f5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c /.static int wal
198f6 46 72 61 6d 65 50 61 67 65 28 75 33 32 20 69 46 FramePage(u32 iF
198f7 72 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 48 61 rame){. int iHa
198f8 73 68 20 3d 20 28 69 46 72 61 6d 65 2b 48 41 53 sh = (iFrame+HAS
198f9 48 54 41 42 4c 45 5f 4e 50 41 47 45 2d 48 41 53 HTABLE_NPAGE-HAS
198fa 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 HTABLE_NPAGE_ONE
198fb 2d 31 29 20 2f 20 48 41 53 48 54 41 42 4c 45 5f -1) / HASHTABLE_
198fc 4e 50 41 47 45 3b 0a 20 20 61 73 73 65 72 74 28 NPAGE;. assert(
198fd 20 28 69 48 61 73 68 3d 3d 30 20 7c 7c 20 69 46 (iHash==0 || iF
198fe 72 61 6d 65 3e 48 41 53 48 54 41 42 4c 45 5f 4e rame>HASHTABLE_N
198ff 50 41 47 45 5f 4f 4e 45 29 0a 20 20 20 20 20 20 PAGE_ONE).
19900 20 26 26 20 28 69 48 61 73 68 3e 3d 31 20 7c 7c && (iHash>=1 ||
19901 20 69 46 72 61 6d 65 3c 3d 48 41 53 48 54 41 42 iFrame<=HASHTAB
19902 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 29 0a 20 20 LE_NPAGE_ONE).
19903 20 20 20 20 20 26 26 20 28 69 48 61 73 68 3c 3d && (iHash<=
19904 31 20 7c 7c 20 69 46 72 61 6d 65 3e 28 48 41 53 1 || iFrame>(HAS
19905 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 HTABLE_NPAGE_ONE
19906 2b 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 +HASHTABLE_NPAGE
19907 29 29 0a 20 20 20 20 20 20 20 26 26 20 28 69 48 )). && (iH
19908 61 73 68 3e 3d 32 20 7c 7c 20 69 46 72 61 6d 65 ash>=2 || iFrame
19909 3c 3d 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 <=HASHTABLE_NPAG
1990a 45 5f 4f 4e 45 2b 48 41 53 48 54 41 42 4c 45 5f E_ONE+HASHTABLE_
1990b 4e 50 41 47 45 29 0a 20 20 20 20 20 20 20 26 26 NPAGE). &&
1990c 20 28 69 48 61 73 68 3c 3d 32 20 7c 7c 20 69 46 (iHash<=2 || iF
1990d 72 61 6d 65 3e 28 48 41 53 48 54 41 42 4c 45 5f rame>(HASHTABLE_
1990e 4e 50 41 47 45 5f 4f 4e 45 2b 32 2a 48 41 53 48 NPAGE_ONE+2*HASH
1990f 54 41 42 4c 45 5f 4e 50 41 47 45 29 29 0a 20 20 TABLE_NPAGE)).
19910 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 48 61 73 );. return iHas
19911 68 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 h;.}../*.** Retu
19912 72 6e 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 rn the page numb
19913 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 er associated wi
19914 74 68 20 66 72 61 6d 65 20 69 46 72 61 6d 65 20 th frame iFrame
19915 69 6e 20 74 68 69 73 20 57 41 4c 2e 0a 2a 2f 0a in this WAL..*/.
19916 73 74 61 74 69 63 20 75 33 32 20 77 61 6c 46 72 static u32 walFr
19917 61 6d 65 50 67 6e 6f 28 57 61 6c 20 2a 70 57 61 amePgno(Wal *pWa
19918 6c 2c 20 75 33 32 20 69 46 72 61 6d 65 29 7b 0a l, u32 iFrame){.
19919 20 20 69 6e 74 20 69 48 61 73 68 20 3d 20 77 61 int iHash = wa
1991a 6c 46 72 61 6d 65 50 61 67 65 28 69 46 72 61 6d lFramePage(iFram
1991b 65 29 3b 0a 20 20 69 66 28 20 69 48 61 73 68 3d e);. if( iHash=
1991c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
1991d 20 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 61 5b pWal->apWiData[
1991e 30 5d 5b 57 41 4c 49 4e 44 45 58 5f 48 44 52 5f 0][WALINDEX_HDR_
1991f 53 49 5a 45 2f 73 69 7a 65 6f 66 28 75 33 32 29 SIZE/sizeof(u32)
19920 20 2b 20 69 46 72 61 6d 65 20 2d 20 31 5d 3b 0a + iFrame - 1];.
19921 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 57 61 }. return pWa
19922 6c 2d 3e 61 70 57 69 44 61 74 61 5b 69 48 61 73 l->apWiData[iHas
19923 68 5d 5b 28 69 46 72 61 6d 65 2d 31 2d 48 41 53 h][(iFrame-1-HAS
19924 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 HTABLE_NPAGE_ONE
19925 29 25 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 )%HASHTABLE_NPAG
19926 45 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d E];.}../*.** Rem
19927 6f 76 65 20 65 6e 74 72 69 65 73 20 66 72 6f 6d ove entries from
19928 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
19929 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 57 41 that point to WA
1992a 4c 20 73 6c 6f 74 73 20 67 72 65 61 74 65 72 0a L slots greater.
1992b 2a 2a 20 74 68 61 6e 20 70 57 61 6c 2d 3e 68 64 ** than pWal->hd
1992c 72 2e 6d 78 46 72 61 6d 65 2e 0a 2a 2a 0a 2a 2a r.mxFrame..**.**
1992d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
1992e 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76 65 s called wheneve
1992f 72 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 r pWal->hdr.mxFr
19930 61 6d 65 20 69 73 20 64 65 63 72 65 61 73 65 64 ame is decreased
19931 20 64 75 65 0a 2a 2a 20 74 6f 20 61 20 72 6f 6c due.** to a rol
19932 6c 62 61 63 6b 20 6f 72 20 73 61 76 65 70 6f 69 lback or savepoi
19933 6e 74 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 6d 6f 73 nt..**.** At mos
19934 74 20 6f 6e 6c 79 20 74 68 65 20 68 61 73 68 20 t only the hash
19935 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 table containing
19936 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 pWal->hdr.mxFra
19937 6d 65 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a me needs to be.*
19938 2a 20 75 70 64 61 74 65 64 2e 20 20 41 6e 79 20 * updated. Any
19939 6c 61 74 65 72 20 68 61 73 68 20 74 61 62 6c 65 later hash table
1993a 73 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 s will be automa
1993b 74 69 63 61 6c 6c 79 20 63 6c 65 61 72 65 64 20 tically cleared
1993c 77 68 65 6e 0a 2a 2a 20 70 57 61 6c 2d 3e 68 64 when.** pWal->hd
1993d 72 2e 6d 78 46 72 61 6d 65 20 61 64 76 61 6e 63 r.mxFrame advanc
1993e 65 73 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 20 es to the point
1993f 77 68 65 72 65 20 74 68 6f 73 65 20 68 61 73 68 where those hash
19940 20 74 61 62 6c 65 73 20 61 72 65 0a 2a 2a 20 61 tables are.** a
19941 63 74 75 61 6c 6c 79 20 6e 65 65 64 65 64 2e 0a ctually needed..
19942 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 */.static void w
19943 61 6c 43 6c 65 61 6e 75 70 48 61 73 68 28 57 61 alCleanupHash(Wa
19944 6c 20 2a 70 57 61 6c 29 7b 0a 20 20 76 6f 6c 61 l *pWal){. vola
19945 74 69 6c 65 20 68 74 5f 73 6c 6f 74 20 2a 61 48 tile ht_slot *aH
19946 61 73 68 20 3d 20 30 3b 20 20 20 20 2f 2a 20 50 ash = 0; /* P
19947 6f 69 6e 74 65 72 20 74 6f 20 68 61 73 68 20 74 ointer to hash t
19948 61 62 6c 65 20 74 6f 20 63 6c 65 61 72 20 2a 2f able to clear */
19949 0a 20 20 76 6f 6c 61 74 69 6c 65 20 75 33 32 20 . volatile u32
1994a 2a 61 50 67 6e 6f 20 3d 20 30 3b 20 20 20 20 20 *aPgno = 0;
1994b 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 /* Page numbe
1994c 72 20 61 72 72 61 79 20 66 6f 72 20 68 61 73 68 r array for hash
1994d 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 33 32 20 table */. u32
1994e 69 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 iZero = 0;
1994f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 /* f
19950 72 61 6d 65 20 3d 3d 20 28 61 48 61 73 68 5b 78 rame == (aHash[x
19951 5d 2b 69 5a 65 72 6f 29 20 2a 2f 0a 20 20 69 6e ]+iZero) */. in
19952 74 20 69 4c 69 6d 69 74 20 3d 20 30 3b 20 20 20 t iLimit = 0;
19953 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19954 20 5a 65 72 6f 20 76 61 6c 75 65 73 20 67 72 65 Zero values gre
19955 61 74 65 72 20 74 68 61 6e 20 74 68 69 73 20 2a ater than this *
19956 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 /. int nByte;
19957 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19958 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
19959 20 62 79 74 65 73 20 74 6f 20 7a 65 72 6f 20 69 bytes to zero i
1995a 6e 20 61 50 67 6e 6f 5b 5d 20 2a 2f 0a 20 20 69 n aPgno[] */. i
1995b 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
1995c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1995d 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72 61 74 * Used to iterat
1995e 65 20 74 68 72 6f 75 67 68 20 61 48 61 73 68 5b e through aHash[
1995f 5d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 ] */.. assert(
19960 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20 pWal->writeLock
19961 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 );. testcase( p
19962 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 Wal->hdr.mxFrame
19963 3d 3d 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 ==HASHTABLE_NPAG
19964 45 5f 4f 4e 45 2d 31 20 29 3b 0a 20 20 74 65 73 E_ONE-1 );. tes
19965 74 63 61 73 65 28 20 70 57 61 6c 2d 3e 68 64 72 tcase( pWal->hdr
19966 2e 6d 78 46 72 61 6d 65 3d 3d 48 41 53 48 54 41 .mxFrame==HASHTA
19967 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 20 29 3b BLE_NPAGE_ONE );
19968 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 57 61 . testcase( pWa
19969 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 3d 3d l->hdr.mxFrame==
1996a 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f HASHTABLE_NPAGE_
1996b 4f 4e 45 2b 31 20 29 3b 0a 0a 20 20 69 66 28 20 ONE+1 );.. if(
1996c 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d pWal->hdr.mxFram
1996d 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a e==0 ) return;..
1996e 20 20 2f 2a 20 4f 62 74 61 69 6e 20 70 6f 69 6e /* Obtain poin
1996f 74 65 72 73 20 74 6f 20 74 68 65 20 68 61 73 68 ters to the hash
19970 2d 74 61 62 6c 65 20 61 6e 64 20 70 61 67 65 2d -table and page-
19971 6e 75 6d 62 65 72 20 61 72 72 61 79 20 63 6f 6e number array con
19972 74 61 69 6e 69 6e 67 20 0a 20 20 2a 2a 20 74 68 taining . ** th
19973 65 20 65 6e 74 72 79 20 74 68 61 74 20 63 6f 72 e entry that cor
19974 72 65 73 70 6f 6e 64 73 20 74 6f 20 66 72 61 6d responds to fram
19975 65 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 e pWal->hdr.mxFr
19976 61 6d 65 2e 20 49 74 20 69 73 20 67 75 61 72 61 ame. It is guara
19977 6e 74 65 65 64 0a 20 20 2a 2a 20 74 68 61 74 20 nteed. ** that
19978 74 68 65 20 70 61 67 65 20 73 61 69 64 20 68 61 the page said ha
19979 73 68 2d 74 61 62 6c 65 20 61 6e 64 20 61 72 72 sh-table and arr
1997a 61 79 20 72 65 73 69 64 65 20 6f 6e 20 69 73 20 ay reside on is
1997b 61 6c 72 65 61 64 79 20 6d 61 70 70 65 64 2e 0a already mapped..
1997c 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
1997d 57 61 6c 2d 3e 6e 57 69 44 61 74 61 3e 77 61 6c Wal->nWiData>wal
1997e 46 72 61 6d 65 50 61 67 65 28 70 57 61 6c 2d 3e FramePage(pWal->
1997f 68 64 72 2e 6d 78 46 72 61 6d 65 29 20 29 3b 0a hdr.mxFrame) );.
19980 20 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e assert( pWal->
19981 61 70 57 69 44 61 74 61 5b 77 61 6c 46 72 61 6d apWiData[walFram
19982 65 50 61 67 65 28 70 57 61 6c 2d 3e 68 64 72 2e ePage(pWal->hdr.
19983 6d 78 46 72 61 6d 65 29 5d 20 29 3b 0a 20 20 77 mxFrame)] );. w
19984 61 6c 48 61 73 68 47 65 74 28 70 57 61 6c 2c 20 alHashGet(pWal,
19985 77 61 6c 46 72 61 6d 65 50 61 67 65 28 70 57 61 walFramePage(pWa
19986 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 29 2c l->hdr.mxFrame),
19987 20 26 61 48 61 73 68 2c 20 26 61 50 67 6e 6f 2c &aHash, &aPgno,
19988 20 26 69 5a 65 72 6f 29 3b 0a 0a 20 20 2f 2a 20 &iZero);.. /*
19989 5a 65 72 6f 20 61 6c 6c 20 68 61 73 68 2d 74 61 Zero all hash-ta
1998a 62 6c 65 20 65 6e 74 72 69 65 73 20 74 68 61 74 ble entries that
1998b 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 66 correspond to f
1998c 72 61 6d 65 20 6e 75 6d 62 65 72 73 20 67 72 65 rame numbers gre
1998d 61 74 65 72 0a 20 20 2a 2a 20 74 68 61 6e 20 70 ater. ** than p
1998e 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 Wal->hdr.mxFrame
1998f 2e 0a 20 20 2a 2f 0a 20 20 69 4c 69 6d 69 74 20 .. */. iLimit
19990 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 = pWal->hdr.mxFr
19991 61 6d 65 20 2d 20 69 5a 65 72 6f 3b 0a 20 20 61 ame - iZero;. a
19992 73 73 65 72 74 28 20 69 4c 69 6d 69 74 3e 30 20 ssert( iLimit>0
19993 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c );. for(i=0; i<
19994 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54 3b HASHTABLE_NSLOT;
19995 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 i++){. if( a
19996 48 61 73 68 5b 69 5d 3e 69 4c 69 6d 69 74 20 29 Hash[i]>iLimit )
19997 7b 0a 20 20 20 20 20 20 61 48 61 73 68 5b 69 5d {. aHash[i]
19998 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
19999 20 20 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68 65 . /* Zero the
1999a 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 entries in the
1999b 61 50 67 6e 6f 20 61 72 72 61 79 20 74 68 61 74 aPgno array that
1999c 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 66 correspond to f
1999d 72 61 6d 65 73 20 77 69 74 68 0a 20 20 2a 2a 20 rames with. **
1999e 66 72 61 6d 65 20 6e 75 6d 62 65 72 73 20 67 72 frame numbers gr
1999f 65 61 74 65 72 20 74 68 61 6e 20 70 57 61 6c 2d eater than pWal-
199a0 3e 68 64 72 2e 6d 78 46 72 61 6d 65 2e 20 0a 20 >hdr.mxFrame. .
199a1 20 2a 2f 0a 20 20 6e 42 79 74 65 20 3d 20 28 69 */. nByte = (i
199a2 6e 74 29 28 28 63 68 61 72 20 2a 29 61 48 61 73 nt)((char *)aHas
199a3 68 20 2d 20 28 63 68 61 72 20 2a 29 26 61 50 67 h - (char *)&aPg
199a4 6e 6f 5b 69 4c 69 6d 69 74 2b 31 5d 29 3b 0a 20 no[iLimit+1]);.
199a5 20 6d 65 6d 73 65 74 28 28 76 6f 69 64 20 2a 29 memset((void *)
199a6 26 61 50 67 6e 6f 5b 69 4c 69 6d 69 74 2b 31 5d &aPgno[iLimit+1]
199a7 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 0a 23 69 , 0, nByte);..#i
199a8 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
199a9 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 LE_EXPENSIVE_ASS
199aa 45 52 54 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 ERT. /* Verify
199ab 74 68 61 74 20 74 68 65 20 65 76 65 72 79 20 65 that the every e
199ac 6e 74 72 79 20 69 6e 20 74 68 65 20 6d 61 70 70 ntry in the mapp
199ad 69 6e 67 20 72 65 67 69 6f 6e 20 69 73 20 73 74 ing region is st
199ae 69 6c 6c 20 72 65 61 63 68 61 62 6c 65 0a 20 20 ill reachable.
199af 2a 2a 20 76 69 61 20 74 68 65 20 68 61 73 68 20 ** via the hash
199b0 74 61 62 6c 65 20 65 76 65 6e 20 61 66 74 65 72 table even after
199b1 20 74 68 65 20 63 6c 65 61 6e 75 70 2e 0a 20 20 the cleanup..
199b2 2a 2f 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 20 */. if( iLimit
199b3 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 ){. int i;
199b4 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
199b5 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69 counter */. i
199b6 6e 74 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20 nt iKey;
199b7 2f 2a 20 48 61 73 68 20 6b 65 79 20 2a 2f 0a 20 /* Hash key */.
199b8 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 69 for(i=1; i<=i
199b9 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 Limit; i++){.
199ba 20 20 20 66 6f 72 28 69 4b 65 79 3d 77 61 6c 48 for(iKey=walH
199bb 61 73 68 28 61 50 67 6e 6f 5b 69 5d 29 3b 20 61 ash(aPgno[i]); a
199bc 48 61 73 68 5b 69 4b 65 79 5d 3b 20 69 4b 65 79 Hash[iKey]; iKey
199bd 3d 77 61 6c 4e 65 78 74 48 61 73 68 28 69 4b 65 =walNextHash(iKe
199be 79 29 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 y)){. if(
199bf 20 61 48 61 73 68 5b 69 4b 65 79 5d 3d 3d 69 20 aHash[iKey]==i
199c0 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d ) break;. }
199c1 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61 . assert( a
199c2 48 61 73 68 5b 69 4b 65 79 5d 3d 3d 69 20 29 3b Hash[iKey]==i );
199c3 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
199c4 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 f /* SQLITE_ENAB
199c5 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 LE_EXPENSIVE_ASS
199c6 45 52 54 20 2a 2f 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a ERT */.}.../*.**
199c7 20 53 65 74 20 61 6e 20 65 6e 74 72 79 20 69 6e Set an entry in
199c8 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 74 the wal-index t
199c9 68 61 74 20 77 69 6c 6c 20 6d 61 70 20 64 61 74 hat will map dat
199ca 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65 abase page numbe
199cb 72 0a 2a 2a 20 70 50 61 67 65 20 69 6e 74 6f 20 r.** pPage into
199cc 57 41 4c 20 66 72 61 6d 65 20 69 46 72 61 6d 65 WAL frame iFrame
199cd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
199ce 77 61 6c 49 6e 64 65 78 41 70 70 65 6e 64 28 57 walIndexAppend(W
199cf 61 6c 20 2a 70 57 61 6c 2c 20 75 33 32 20 69 46 al *pWal, u32 iF
199d0 72 61 6d 65 2c 20 75 33 32 20 69 50 61 67 65 29 rame, u32 iPage)
199d1 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
199d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
199d3 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
199d4 64 65 20 2a 2f 0a 20 20 75 33 32 20 69 5a 65 72 de */. u32 iZer
199d5 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 o = 0;
199d6 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6c /* One l
199d7 65 73 73 20 74 68 61 6e 20 66 72 61 6d 65 20 6e ess than frame n
199d8 75 6d 62 65 72 20 6f 66 20 61 50 67 6e 6f 5b 31 umber of aPgno[1
199d9 5d 20 2a 2f 0a 20 20 76 6f 6c 61 74 69 6c 65 20 ] */. volatile
199da 75 33 32 20 2a 61 50 67 6e 6f 20 3d 20 30 3b 20 u32 *aPgno = 0;
199db 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
199dc 75 6d 62 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 umber array */.
199dd 20 76 6f 6c 61 74 69 6c 65 20 68 74 5f 73 6c 6f volatile ht_slo
199de 74 20 2a 61 48 61 73 68 20 3d 20 30 3b 20 20 20 t *aHash = 0;
199df 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 2a /* Hash table *
199e0 2f 0a 0a 20 20 72 63 20 3d 20 77 61 6c 48 61 73 /.. rc = walHas
199e1 68 47 65 74 28 70 57 61 6c 2c 20 77 61 6c 46 72 hGet(pWal, walFr
199e2 61 6d 65 50 61 67 65 28 69 46 72 61 6d 65 29 2c amePage(iFrame),
199e3 20 26 61 48 61 73 68 2c 20 26 61 50 67 6e 6f 2c &aHash, &aPgno,
199e4 20 26 69 5a 65 72 6f 29 3b 0a 0a 20 20 2f 2a 20 &iZero);.. /*
199e5 41 73 73 75 6d 69 6e 67 20 74 68 65 20 77 61 6c Assuming the wal
199e6 2d 69 6e 64 65 78 20 66 69 6c 65 20 77 61 73 20 -index file was
199e7 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6d 61 70 successfully map
199e8 70 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68 ped, populate th
199e9 65 0a 20 20 2a 2a 20 70 61 67 65 20 6e 75 6d 62 e. ** page numb
199ea 65 72 20 61 72 72 61 79 20 61 6e 64 20 68 61 73 er array and has
199eb 68 20 74 61 62 6c 65 20 65 6e 74 72 79 2e 0a 20 h table entry..
199ec 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 */. if( rc==SQ
199ed 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 LITE_OK ){. i
199ee 6e 74 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20 nt iKey;
199ef 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
199f0 48 61 73 68 20 74 61 62 6c 65 20 6b 65 79 20 2a Hash table key *
199f1 2f 0a 20 20 20 20 69 6e 74 20 69 64 78 3b 20 20 /. int idx;
199f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
199f3 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 /* Value to
199f4 77 72 69 74 65 20 74 6f 20 68 61 73 68 2d 74 61 write to hash-ta
199f5 62 6c 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 20 20 ble slot */.
199f6 69 6e 74 20 6e 43 6f 6c 6c 69 64 65 3b 20 20 20 int nCollide;
199f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
199f8 20 4e 75 6d 62 65 72 20 6f 66 20 68 61 73 68 20 Number of hash
199f9 63 6f 6c 6c 69 73 69 6f 6e 73 20 2a 2f 0a 0a 20 collisions */..
199fa 20 20 20 69 64 78 20 3d 20 69 46 72 61 6d 65 20 idx = iFrame
199fb 2d 20 69 5a 65 72 6f 3b 0a 20 20 20 20 61 73 73 - iZero;. ass
199fc 65 72 74 28 20 69 64 78 20 3c 3d 20 48 41 53 48 ert( idx <= HASH
199fd 54 41 42 4c 45 5f 4e 53 4c 4f 54 2f 32 20 2b 20 TABLE_NSLOT/2 +
199fe 31 20 29 3b 0a 20 20 20 20 0a 20 20 20 20 2f 2a 1 );. . /*
199ff 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
19a00 66 69 72 73 74 20 65 6e 74 72 79 20 74 6f 20 62 first entry to b
19a01 65 20 61 64 64 65 64 20 74 6f 20 74 68 69 73 20 e added to this
19a02 68 61 73 68 2d 74 61 62 6c 65 2c 20 7a 65 72 6f hash-table, zero
19a03 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 69 the. ** enti
19a04 72 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e re hash table an
19a05 64 20 61 50 67 6e 6f 5b 5d 20 61 72 72 61 79 20 d aPgno[] array
19a06 62 65 66 6f 72 65 20 70 72 6f 63 65 64 69 6e 67 before proceding
19a07 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 . . */. if
19a08 28 20 69 64 78 3d 3d 31 20 29 7b 0a 20 20 20 20 ( idx==1 ){.
19a09 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 69 int nByte = (i
19a0a 6e 74 29 28 28 75 38 20 2a 29 26 61 48 61 73 68 nt)((u8 *)&aHash
19a0b 5b 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54 [HASHTABLE_NSLOT
19a0c 5d 20 2d 20 28 75 38 20 2a 29 26 61 50 67 6e 6f ] - (u8 *)&aPgno
19a0d 5b 31 5d 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 [1]);. mems
19a0e 65 74 28 28 76 6f 69 64 2a 29 26 61 50 67 6e 6f et((void*)&aPgno
19a0f 5b 31 5d 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a [1], 0, nByte);.
19a10 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
19a11 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20 61 50 the entry in aP
19a12 67 6e 6f 5b 5d 20 69 73 20 61 6c 72 65 61 64 79 gno[] is already
19a13 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 70 set, then the p
19a14 72 65 76 69 6f 75 73 20 77 72 69 74 65 72 0a 20 revious writer.
19a15 20 20 20 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 ** must have
19a16 65 78 69 74 65 64 20 75 6e 65 78 70 65 63 74 65 exited unexpecte
19a17 64 6c 79 20 69 6e 20 74 68 65 20 6d 69 64 64 6c dly in the middl
19a18 65 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 e of a transacti
19a19 6f 6e 20 28 61 66 74 65 72 0a 20 20 20 20 2a 2a on (after. **
19a1a 20 77 72 69 74 69 6e 67 20 6f 6e 65 20 6f 72 20 writing one or
19a1b 6d 6f 72 65 20 64 69 72 74 79 20 70 61 67 65 73 more dirty pages
19a1c 20 74 6f 20 74 68 65 20 57 41 4c 20 74 6f 20 66 to the WAL to f
19a1d 72 65 65 20 75 70 20 6d 65 6d 6f 72 79 29 2e 20 ree up memory).
19a1e 0a 20 20 20 20 2a 2a 20 52 65 6d 6f 76 65 20 74 . ** Remove t
19a1f 68 65 20 72 65 6d 6e 61 6e 74 73 20 6f 66 20 74 he remnants of t
19a20 68 61 74 20 77 72 69 74 65 72 73 20 75 6e 63 6f hat writers unco
19a21 6d 6d 69 74 74 65 64 20 74 72 61 6e 73 61 63 74 mmitted transact
19a22 69 6f 6e 20 66 72 6f 6d 20 0a 20 20 20 20 2a 2a ion from . **
19a23 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65 20 the hash-table
19a24 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 61 before writing a
19a25 6e 79 20 6e 65 77 20 65 6e 74 72 69 65 73 2e 0a ny new entries..
19a26 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 61 */. if( a
19a27 50 67 6e 6f 5b 69 64 78 5d 20 29 7b 0a 20 20 20 Pgno[idx] ){.
19a28 20 20 20 77 61 6c 43 6c 65 61 6e 75 70 48 61 73 walCleanupHas
19a29 68 28 70 57 61 6c 29 3b 0a 20 20 20 20 20 20 61 h(pWal);. a
19a2a 73 73 65 72 74 28 20 21 61 50 67 6e 6f 5b 69 64 ssert( !aPgno[id
19a2b 78 5d 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 x] );. }..
19a2c 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 61 50 /* Write the aP
19a2d 67 6e 6f 5b 5d 20 61 72 72 61 79 20 65 6e 74 72 gno[] array entr
19a2e 79 20 61 6e 64 20 74 68 65 20 68 61 73 68 2d 74 y and the hash-t
19a2f 61 62 6c 65 20 73 6c 6f 74 2e 20 2a 2f 0a 20 20 able slot. */.
19a30 20 20 6e 43 6f 6c 6c 69 64 65 20 3d 20 69 64 78 nCollide = idx
19a31 3b 0a 20 20 20 20 66 6f 72 28 69 4b 65 79 3d 77 ;. for(iKey=w
19a32 61 6c 48 61 73 68 28 69 50 61 67 65 29 3b 20 61 alHash(iPage); a
19a33 48 61 73 68 5b 69 4b 65 79 5d 3b 20 69 4b 65 79 Hash[iKey]; iKey
19a34 3d 77 61 6c 4e 65 78 74 48 61 73 68 28 69 4b 65 =walNextHash(iKe
19a35 79 29 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 y)){. if( (
19a36 6e 43 6f 6c 6c 69 64 65 2d 2d 29 3d 3d 30 20 29 nCollide--)==0 )
19a37 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
19a38 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
19a39 20 7d 0a 20 20 20 20 61 50 67 6e 6f 5b 69 64 78 }. aPgno[idx
19a3a 5d 20 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 61 ] = iPage;. a
19a3b 48 61 73 68 5b 69 4b 65 79 5d 20 3d 20 28 68 74 Hash[iKey] = (ht
19a3c 5f 73 6c 6f 74 29 69 64 78 3b 0a 0a 23 69 66 64 _slot)idx;..#ifd
19a3d 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
19a3e 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 _EXPENSIVE_ASSER
19a3f 54 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 T. /* Verify
19a40 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 that the number
19a41 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 of entries in th
19a42 65 20 68 61 73 68 20 74 61 62 6c 65 20 65 78 61 e hash table exa
19a43 63 74 6c 79 20 65 71 75 61 6c 73 0a 20 20 20 20 ctly equals.
19a44 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** the number of
19a45 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 entries in the
19a46 6d 61 70 70 69 6e 67 20 72 65 67 69 6f 6e 2e 0a mapping region..
19a47 20 20 20 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 */. {.
19a48 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 int i;
19a49 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
19a4a 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ter */. int
19a4b 20 6e 45 6e 74 72 79 20 3d 20 30 3b 20 20 2f 2a nEntry = 0; /*
19a4c 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
19a4d 65 73 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 es in the hash t
19a4e 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 66 6f able */. fo
19a4f 72 28 69 3d 30 3b 20 69 3c 48 41 53 48 54 41 42 r(i=0; i<HASHTAB
19a50 4c 45 5f 4e 53 4c 4f 54 3b 20 69 2b 2b 29 7b 20 LE_NSLOT; i++){
19a51 69 66 28 20 61 48 61 73 68 5b 69 5d 20 29 20 6e if( aHash[i] ) n
19a52 45 6e 74 72 79 2b 2b 3b 20 7d 0a 20 20 20 20 20 Entry++; }.
19a53 20 61 73 73 65 72 74 28 20 6e 45 6e 74 72 79 3d assert( nEntry=
19a54 3d 69 64 78 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 =idx );. }..
19a55 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 /* Verify tha
19a56 74 20 74 68 65 20 65 76 65 72 79 20 65 6e 74 72 t the every entr
19a57 79 20 69 6e 20 74 68 65 20 6d 61 70 70 69 6e 67 y in the mapping
19a58 20 72 65 67 69 6f 6e 20 69 73 20 72 65 61 63 68 region is reach
19a59 61 62 6c 65 0a 20 20 20 20 2a 2a 20 76 69 61 20 able. ** via
19a5a 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 20 the hash table.
19a5b 20 54 68 69 73 20 74 75 72 6e 73 20 6f 75 74 20 This turns out
19a5c 74 6f 20 62 65 20 61 20 72 65 61 6c 6c 79 2c 20 to be a really,
19a5d 72 65 61 6c 6c 79 20 65 78 70 65 6e 73 69 76 65 really expensive
19a5e 0a 20 20 20 20 2a 2a 20 74 68 69 6e 67 20 74 6f . ** thing to
19a5f 20 63 68 65 63 6b 2c 20 73 6f 20 6f 6e 6c 79 20 check, so only
19a60 64 6f 20 74 68 69 73 20 6f 63 63 61 73 69 6f 6e do this occasion
19a61 61 6c 6c 79 20 2d 20 6e 6f 74 20 6f 6e 20 65 76 ally - not on ev
19a62 65 72 79 0a 20 20 20 20 2a 2a 20 69 74 65 72 61 ery. ** itera
19a63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tion.. */.
19a64 20 69 66 28 20 28 69 64 78 26 30 78 33 66 66 29 if( (idx&0x3ff)
19a65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 ==0 ){. int
19a66 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a i; /*
19a67 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
19a68 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 . for(i=1;
19a69 69 3c 3d 69 64 78 3b 20 69 2b 2b 29 7b 0a 20 20 i<=idx; i++){.
19a6a 20 20 20 20 20 20 66 6f 72 28 69 4b 65 79 3d 77 for(iKey=w
19a6b 61 6c 48 61 73 68 28 61 50 67 6e 6f 5b 69 5d 29 alHash(aPgno[i])
19a6c 3b 20 61 48 61 73 68 5b 69 4b 65 79 5d 3b 20 69 ; aHash[iKey]; i
19a6d 4b 65 79 3d 77 61 6c 4e 65 78 74 48 61 73 68 28 Key=walNextHash(
19a6e 69 4b 65 79 29 29 7b 0a 20 20 20 20 20 20 20 20 iKey)){.
19a6f 20 20 69 66 28 20 61 48 61 73 68 5b 69 4b 65 79 if( aHash[iKey
19a70 5d 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 ]==i ) break;.
19a71 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
19a72 61 73 73 65 72 74 28 20 61 48 61 73 68 5b 69 4b assert( aHash[iK
19a73 65 79 5d 3d 3d 69 20 29 3b 0a 20 20 20 20 20 20 ey]==i );.
19a74 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f }. }.#endif /
19a75 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f * SQLITE_ENABLE_
19a76 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 EXPENSIVE_ASSERT
19a77 20 2a 2f 0a 20 20 7d 0a 0a 0a 20 20 72 65 74 75 */. }... retu
19a78 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
19a79 20 52 65 63 6f 76 65 72 20 74 68 65 20 77 61 6c Recover the wal
19a7a 2d 69 6e 64 65 78 20 62 79 20 72 65 61 64 69 6e -index by readin
19a7b 67 20 74 68 65 20 77 72 69 74 65 2d 61 68 65 61 g the write-ahea
19a7c 64 20 6c 6f 67 20 66 69 6c 65 2e 20 0a 2a 2a 0a d log file. .**.
19a7d 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
19a7e 66 69 72 73 74 20 74 72 69 65 73 20 74 6f 20 65 first tries to e
19a7f 73 74 61 62 6c 69 73 68 20 61 6e 20 65 78 63 6c stablish an excl
19a80 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 usive lock on th
19a81 65 0a 2a 2a 20 77 61 6c 2d 69 6e 64 65 78 20 74 e.** wal-index t
19a82 6f 20 70 72 65 76 65 6e 74 20 6f 74 68 65 72 20 o prevent other
19a83 74 68 72 65 61 64 73 2f 70 72 6f 63 65 73 73 65 threads/processe
19a84 73 20 66 72 6f 6d 20 64 6f 69 6e 67 20 61 6e 79 s from doing any
19a85 74 68 69 6e 67 0a 2a 2a 20 77 69 74 68 20 74 68 thing.** with th
19a86 65 20 57 41 4c 20 6f 72 20 77 61 6c 2d 69 6e 64 e WAL or wal-ind
19a87 65 78 20 77 68 69 6c 65 20 72 65 63 6f 76 65 72 ex while recover
19a88 79 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20 54 y is running. T
19a89 68 65 0a 2a 2a 20 57 41 4c 5f 52 45 43 4f 56 45 he.** WAL_RECOVE
19a8a 52 5f 4c 4f 43 4b 20 69 73 20 61 6c 73 6f 20 68 R_LOCK is also h
19a8b 65 6c 64 20 73 6f 20 74 68 61 74 20 6f 74 68 65 eld so that othe
19a8c 72 20 74 68 72 65 61 64 73 20 77 69 6c 6c 20 6b r threads will k
19a8d 6e 6f 77 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 now.** that this
19a8e 20 74 68 72 65 61 64 20 69 73 20 72 75 6e 6e 69 thread is runni
19a8f 6e 67 20 72 65 63 6f 76 65 72 79 2e 20 20 49 66 ng recovery. If
19a90 20 75 6e 61 62 6c 65 20 74 6f 20 65 73 74 61 62 unable to estab
19a91 6c 69 73 68 0a 2a 2a 20 74 68 65 20 6e 65 63 65 lish.** the nece
19a92 73 73 61 72 79 20 6c 6f 63 6b 73 2c 20 74 68 69 ssary locks, thi
19a93 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e s routine return
19a94 73 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a s SQLITE_BUSY..*
19a95 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c /.static int wal
19a96 49 6e 64 65 78 52 65 63 6f 76 65 72 28 57 61 6c IndexRecover(Wal
19a97 20 2a 70 57 61 6c 29 7b 0a 20 20 69 6e 74 20 72 *pWal){. int r
19a98 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
19a99 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
19a9a 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 turn Code */. i
19a9b 36 34 20 6e 53 69 7a 65 3b 20 20 20 20 20 20 20 64 nSize;
19a9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19a9d 2a 20 53 69 7a 65 20 6f 66 20 6c 6f 67 20 66 69 * Size of log fi
19a9e 6c 65 20 2a 2f 0a 20 20 75 33 32 20 61 46 72 61 le */. u32 aFra
19a9f 6d 65 43 6b 73 75 6d 5b 32 5d 20 3d 20 7b 30 2c meCksum[2] = {0,
19aa0 20 30 7d 3b 0a 20 20 69 6e 74 20 69 4c 6f 63 6b 0};. int iLock
19aa1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
19aa2 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 6f /* Lock o
19aa3 66 66 73 65 74 20 74 6f 20 6c 6f 63 6b 20 66 6f ffset to lock fo
19aa4 72 20 63 68 65 63 6b 70 6f 69 6e 74 20 2a 2f 0a r checkpoint */.
19aa5 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20 20 20 int nLock;
19aa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19aa7 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c /* Number of l
19aa8 6f 63 6b 73 20 74 6f 20 68 6f 6c 64 20 2a 2f 0a ocks to hold */.
19aa9 0a 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 6e 20 . /* Obtain an
19aaa 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f exclusive lock o
19aab 6e 20 61 6c 6c 20 62 79 74 65 20 69 6e 20 74 68 n all byte in th
19aac 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 e locking range
19aad 6e 6f 74 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a not already. **
19aae 20 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 20 63 locked by the c
19aaf 61 6c 6c 65 72 2e 20 54 68 65 20 63 61 6c 6c 65 aller. The calle
19ab0 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 r is guaranteed
19ab1 74 6f 20 68 61 76 65 20 6c 6f 63 6b 65 64 20 74 to have locked t
19ab2 68 65 0a 20 20 2a 2a 20 57 41 4c 5f 57 52 49 54 he. ** WAL_WRIT
19ab3 45 5f 4c 4f 43 4b 20 62 79 74 65 2c 20 61 6e 64 E_LOCK byte, and
19ab4 20 6d 61 79 20 68 61 76 65 20 61 6c 73 6f 20 6c may have also l
19ab5 6f 63 6b 65 64 20 74 68 65 20 57 41 4c 5f 43 4b ocked the WAL_CK
19ab6 50 54 5f 4c 4f 43 4b 20 62 79 74 65 2e 0a 20 20 PT_LOCK byte..
19ab7 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c ** If successful
19ab8 2c 20 74 68 65 20 73 61 6d 65 20 62 79 74 65 73 , the same bytes
19ab9 20 74 68 61 74 20 61 72 65 20 6c 6f 63 6b 65 64 that are locked
19aba 20 68 65 72 65 20 61 72 65 20 75 6e 6c 6f 63 6b here are unlock
19abb 65 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 74 ed before. ** t
19abc 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
19abd 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 urns.. */. ass
19abe 65 72 74 28 20 70 57 61 6c 2d 3e 63 6b 70 74 4c ert( pWal->ckptL
19abf 6f 63 6b 3d 3d 31 20 7c 7c 20 70 57 61 6c 2d 3e ock==1 || pWal->
19ac0 63 6b 70 74 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 ckptLock==0 );.
19ac1 20 61 73 73 65 72 74 28 20 57 41 4c 5f 41 4c 4c assert( WAL_ALL
19ac2 5f 42 55 54 5f 57 52 49 54 45 3d 3d 57 41 4c 5f _BUT_WRITE==WAL_
19ac3 57 52 49 54 45 5f 4c 4f 43 4b 2b 31 20 29 3b 0a WRITE_LOCK+1 );.
19ac4 20 20 61 73 73 65 72 74 28 20 57 41 4c 5f 43 4b assert( WAL_CK
19ac5 50 54 5f 4c 4f 43 4b 3d 3d 57 41 4c 5f 41 4c 4c PT_LOCK==WAL_ALL
19ac6 5f 42 55 54 5f 57 52 49 54 45 20 29 3b 0a 20 20 _BUT_WRITE );.
19ac7 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 77 72 assert( pWal->wr
19ac8 69 74 65 4c 6f 63 6b 20 29 3b 0a 20 20 69 4c 6f iteLock );. iLo
19ac9 63 6b 20 3d 20 57 41 4c 5f 41 4c 4c 5f 42 55 54 ck = WAL_ALL_BUT
19aca 5f 57 52 49 54 45 20 2b 20 70 57 61 6c 2d 3e 63 _WRITE + pWal->c
19acb 6b 70 74 4c 6f 63 6b 3b 0a 20 20 6e 4c 6f 63 6b kptLock;. nLock
19acc 20 3d 20 53 51 4c 49 54 45 5f 53 48 4d 5f 4e 4c = SQLITE_SHM_NL
19acd 4f 43 4b 20 2d 20 69 4c 6f 63 6b 3b 0a 20 20 72 OCK - iLock;. r
19ace 63 20 3d 20 77 61 6c 4c 6f 63 6b 45 78 63 6c 75 c = walLockExclu
19acf 73 69 76 65 28 70 57 61 6c 2c 20 69 4c 6f 63 6b sive(pWal, iLock
19ad0 2c 20 6e 4c 6f 63 6b 29 3b 0a 20 20 69 66 28 20 , nLock);. if(
19ad1 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e rc ){. return
19ad2 20 72 63 3b 0a 20 20 7d 0a 20 20 57 41 4c 54 52 rc;. }. WALTR
19ad3 41 43 45 28 28 22 57 41 4c 25 70 3a 20 72 65 63 ACE(("WAL%p: rec
19ad4 6f 76 65 72 79 20 62 65 67 69 6e 2e 2e 2e 5c 6e overy begin...\n
19ad5 22 2c 20 70 57 61 6c 29 29 3b 0a 0a 20 20 6d 65 ", pWal));.. me
19ad6 6d 73 65 74 28 26 70 57 61 6c 2d 3e 68 64 72 2c mset(&pWal->hdr,
19ad7 20 30 2c 20 73 69 7a 65 6f 66 28 57 61 6c 49 6e 0, sizeof(WalIn
19ad8 64 65 78 48 64 72 29 29 3b 0a 0a 20 20 72 63 20 dexHdr));.. rc
19ad9 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 = sqlite3OsFileS
19ada 69 7a 65 28 70 57 61 6c 2d 3e 70 57 61 6c 46 64 ize(pWal->pWalFd
19adb 2c 20 26 6e 53 69 7a 65 29 3b 0a 20 20 69 66 28 , &nSize);. if(
19adc 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
19add 7b 0a 20 20 20 20 67 6f 74 6f 20 72 65 63 6f 76 {. goto recov
19ade 65 72 79 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 0a ery_error;. }..
19adf 20 20 69 66 28 20 6e 53 69 7a 65 3e 57 41 4c 5f if( nSize>WAL_
19ae0 48 44 52 53 49 5a 45 20 29 7b 0a 20 20 20 20 75 HDRSIZE ){. u
19ae1 38 20 61 42 75 66 5b 57 41 4c 5f 48 44 52 53 49 8 aBuf[WAL_HDRSI
19ae2 5a 45 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ZE]; /*
19ae3 42 75 66 66 65 72 20 74 6f 20 6c 6f 61 64 20 57 Buffer to load W
19ae4 41 4c 20 68 65 61 64 65 72 20 69 6e 74 6f 20 2a AL header into *
19ae5 2f 0a 20 20 20 20 75 38 20 2a 61 46 72 61 6d 65 /. u8 *aFrame
19ae6 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
19ae7 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 /* Malloc'd
19ae8 62 75 66 66 65 72 20 74 6f 20 6c 6f 61 64 20 65 buffer to load e
19ae9 6e 74 69 72 65 20 66 72 61 6d 65 20 2a 2f 0a 20 ntire frame */.
19aea 20 20 20 69 6e 74 20 73 7a 46 72 61 6d 65 3b 20 int szFrame;
19aeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19aec 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
19aed 74 65 73 20 69 6e 20 62 75 66 66 65 72 20 61 46 tes in buffer aF
19aee 72 61 6d 65 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 rame[] */. u8
19aef 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 *aData;
19af0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
19af1 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 20 70 ointer to data p
19af2 61 72 74 20 6f 66 20 61 46 72 61 6d 65 20 62 75 art of aFrame bu
19af3 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ffer */. int
19af4 69 46 72 61 6d 65 3b 20 20 20 20 20 20 20 20 20 iFrame;
19af5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
19af6 65 78 20 6f 66 20 6c 61 73 74 20 66 72 61 6d 65 ex of last frame
19af7 20 72 65 61 64 20 2a 2f 0a 20 20 20 20 69 36 34 read */. i64
19af8 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 iOffset;
19af9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 /* Ne
19afa 78 74 20 6f 66 66 73 65 74 20 74 6f 20 72 65 61 xt offset to rea
19afb 64 20 66 72 6f 6d 20 6c 6f 67 20 66 69 6c 65 20 d from log file
19afc 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 67 */. int szPag
19afd 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
19afe 20 20 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a /* Page siz
19aff 65 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 e according to t
19b00 68 65 20 6c 6f 67 20 2a 2f 0a 20 20 20 20 75 33 he log */. u3
19b01 32 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20 2 magic;
19b02 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
19b03 61 67 69 63 20 76 61 6c 75 65 20 72 65 61 64 20 agic value read
19b04 66 72 6f 6d 20 57 41 4c 20 68 65 61 64 65 72 20 from WAL header
19b05 2a 2f 0a 20 20 20 20 75 33 32 20 76 65 72 73 69 */. u32 versi
19b06 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 on;
19b07 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 76 61 /* Magic va
19b08 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 57 41 lue read from WA
19b09 4c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 L header */.
19b0a 69 6e 74 20 69 73 56 61 6c 69 64 3b 20 20 20 20 int isValid;
19b0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19b0c 20 54 72 75 65 20 69 66 20 74 68 69 73 20 66 72 True if this fr
19b0d 61 6d 65 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a ame is valid */.
19b0e 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 69 6e 20 . /* Read in
19b0f 74 68 65 20 57 41 4c 20 68 65 61 64 65 72 2e 20 the WAL header.
19b10 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
19b11 74 65 33 4f 73 52 65 61 64 28 70 57 61 6c 2d 3e te3OsRead(pWal->
19b12 70 57 61 6c 46 64 2c 20 61 42 75 66 2c 20 57 41 pWalFd, aBuf, WA
19b13 4c 5f 48 44 52 53 49 5a 45 2c 20 30 29 3b 0a 20 L_HDRSIZE, 0);.
19b14 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
19b15 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f E_OK ){. go
19b16 74 6f 20 72 65 63 6f 76 65 72 79 5f 65 72 72 6f to recovery_erro
19b17 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a r;. }.. /*
19b18 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 If the database
19b19 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f page size is no
19b1a 74 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f t a power of two
19b1b 2c 20 6f 72 20 69 73 20 67 72 65 61 74 65 72 20 , or is greater
19b1c 74 68 61 6e 0a 20 20 20 20 2a 2a 20 53 51 4c 49 than. ** SQLI
19b1d 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
19b1e 2c 20 63 6f 6e 63 6c 75 64 65 20 74 68 61 74 20 , conclude that
19b1f 74 68 65 20 57 41 4c 20 66 69 6c 65 20 63 6f 6e the WAL file con
19b20 74 61 69 6e 73 20 6e 6f 20 76 61 6c 69 64 20 0a tains no valid .
19b21 20 20 20 20 2a 2a 20 64 61 74 61 2e 20 53 69 6d ** data. Sim
19b22 69 6c 61 72 6c 79 2c 20 69 66 20 74 68 65 20 27 ilarly, if the '
19b23 6d 61 67 69 63 27 20 76 61 6c 75 65 20 69 73 20 magic' value is
19b24 69 6e 76 61 6c 69 64 2c 20 69 67 6e 6f 72 65 20 invalid, ignore
19b25 74 68 65 20 77 68 6f 6c 65 0a 20 20 20 20 2a 2a the whole. **
19b26 20 57 41 4c 20 66 69 6c 65 2e 0a 20 20 20 20 2a WAL file.. *
19b27 2f 0a 20 20 20 20 6d 61 67 69 63 20 3d 20 73 71 /. magic = sq
19b28 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 26 61 lite3Get4byte(&a
19b29 42 75 66 5b 30 5d 29 3b 0a 20 20 20 20 73 7a 50 Buf[0]);. szP
19b2a 61 67 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 age = sqlite3Get
19b2b 34 62 79 74 65 28 26 61 42 75 66 5b 38 5d 29 3b 4byte(&aBuf[8]);
19b2c 0a 20 20 20 20 69 66 28 20 28 6d 61 67 69 63 26 . if( (magic&
19b2d 30 78 46 46 46 46 46 46 46 45 29 21 3d 57 41 4c 0xFFFFFFFE)!=WAL
19b2e 5f 4d 41 47 49 43 20 0a 20 20 20 20 20 7c 7c 20 _MAGIC . ||
19b2f 73 7a 50 61 67 65 26 28 73 7a 50 61 67 65 2d 31 szPage&(szPage-1
19b30 29 20 0a 20 20 20 20 20 7c 7c 20 73 7a 50 61 67 ) . || szPag
19b31 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 e>SQLITE_MAX_PAG
19b32 45 5f 53 49 5a 45 20 0a 20 20 20 20 20 7c 7c 20 E_SIZE . ||
19b33 73 7a 50 61 67 65 3c 35 31 32 20 0a 20 20 20 20 szPage<512 .
19b34 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 69 ){. goto fi
19b35 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a 20 20 nished;. }.
19b36 20 20 70 57 61 6c 2d 3e 68 64 72 2e 62 69 67 45 pWal->hdr.bigE
19b37 6e 64 43 6b 73 75 6d 20 3d 20 28 75 38 29 28 6d ndCksum = (u8)(m
19b38 61 67 69 63 26 30 78 30 30 30 30 30 30 30 31 29 agic&0x00000001)
19b39 3b 0a 20 20 20 20 70 57 61 6c 2d 3e 73 7a 50 61 ;. pWal->szPa
19b3a 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 20 ge = szPage;.
19b3b 20 70 57 61 6c 2d 3e 6e 43 6b 70 74 20 3d 20 73 pWal->nCkpt = s
19b3c 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 26 qlite3Get4byte(&
19b3d 61 42 75 66 5b 31 32 5d 29 3b 0a 20 20 20 20 6d aBuf[12]);. m
19b3e 65 6d 63 70 79 28 26 70 57 61 6c 2d 3e 68 64 72 emcpy(&pWal->hdr
19b3f 2e 61 53 61 6c 74 2c 20 26 61 42 75 66 5b 31 36 .aSalt, &aBuf[16
19b40 5d 2c 20 38 29 3b 0a 0a 20 20 20 20 2f 2a 20 56 ], 8);.. /* V
19b41 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 57 erify that the W
19b42 41 4c 20 68 65 61 64 65 72 20 63 68 65 63 6b 73 AL header checks
19b43 75 6d 20 69 73 20 63 6f 72 72 65 63 74 20 2a 2f um is correct */
19b44 0a 20 20 20 20 77 61 6c 43 68 65 63 6b 73 75 6d . walChecksum
19b45 42 79 74 65 73 28 70 57 61 6c 2d 3e 68 64 72 2e Bytes(pWal->hdr.
19b46 62 69 67 45 6e 64 43 6b 73 75 6d 3d 3d 53 51 4c bigEndCksum==SQL
19b47 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 2c 20 0a ITE_BIGENDIAN, .
19b48 20 20 20 20 20 20 20 20 61 42 75 66 2c 20 57 41 aBuf, WA
19b49 4c 5f 48 44 52 53 49 5a 45 2d 32 2a 34 2c 20 30 L_HDRSIZE-2*4, 0
19b4a 2c 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72 61 , pWal->hdr.aFra
19b4b 6d 65 43 6b 73 75 6d 0a 20 20 20 20 29 3b 0a 20 meCksum. );.
19b4c 20 20 20 69 66 28 20 70 57 61 6c 2d 3e 68 64 72 if( pWal->hdr
19b4d 2e 61 46 72 61 6d 65 43 6b 73 75 6d 5b 30 5d 21 .aFrameCksum[0]!
19b4e 3d 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 =sqlite3Get4byte
19b4f 28 26 61 42 75 66 5b 32 34 5d 29 0a 20 20 20 20 (&aBuf[24]).
19b50 20 7c 7c 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 || pWal->hdr.aF
19b51 72 61 6d 65 43 6b 73 75 6d 5b 31 5d 21 3d 73 71 rameCksum[1]!=sq
19b52 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 26 61 lite3Get4byte(&a
19b53 42 75 66 5b 32 38 5d 29 0a 20 20 20 20 29 7b 0a Buf[28]). ){.
19b54 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 goto finis
19b55 68 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 hed;. }..
19b56 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 /* Verify that t
19b57 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 he version numbe
19b58 72 20 6f 6e 20 74 68 65 20 57 41 4c 20 66 6f 72 r on the WAL for
19b59 6d 61 74 20 69 73 20 6f 6e 65 20 74 68 61 74 0a mat is one that.
19b5a 20 20 20 20 2a 2a 20 61 72 65 20 61 62 6c 65 20 ** are able
19b5b 74 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 2a 2f to understand */
19b5c 0a 20 20 20 20 76 65 72 73 69 6f 6e 20 3d 20 73 . version = s
19b5d 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 26 qlite3Get4byte(&
19b5e 61 42 75 66 5b 34 5d 29 3b 0a 20 20 20 20 69 66 aBuf[4]);. if
19b5f 28 20 76 65 72 73 69 6f 6e 21 3d 57 41 4c 5f 4d ( version!=WAL_M
19b60 41 58 5f 56 45 52 53 49 4f 4e 20 29 7b 0a 20 20 AX_VERSION ){.
19b61 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
19b62 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 3b 0a 20 CANTOPEN_BKPT;.
19b63 20 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 goto finish
19b64 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f ed;. }.. /
19b65 2a 20 4d 61 6c 6c 6f 63 20 61 20 62 75 66 66 65 * Malloc a buffe
19b66 72 20 74 6f 20 72 65 61 64 20 66 72 61 6d 65 73 r to read frames
19b67 20 69 6e 74 6f 2e 20 2a 2f 0a 20 20 20 20 73 7a into. */. sz
19b68 46 72 61 6d 65 20 3d 20 73 7a 50 61 67 65 20 2b Frame = szPage +
19b69 20 57 41 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 WAL_FRAME_HDRSI
19b6a 5a 45 3b 0a 20 20 20 20 61 46 72 61 6d 65 20 3d ZE;. aFrame =
19b6b 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 5f 6d (u8 *)sqlite3_m
19b6c 61 6c 6c 6f 63 28 73 7a 46 72 61 6d 65 29 3b 0a alloc(szFrame);.
19b6d 20 20 20 20 69 66 28 20 21 61 46 72 61 6d 65 20 if( !aFrame
19b6e 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
19b6f 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
19b70 20 20 67 6f 74 6f 20 72 65 63 6f 76 65 72 79 5f goto recovery_
19b71 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 error;. }.
19b72 20 61 44 61 74 61 20 3d 20 26 61 46 72 61 6d 65 aData = &aFrame
19b73 5b 57 41 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 [WAL_FRAME_HDRSI
19b74 5a 45 5d 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 ZE];.. /* Rea
19b75 64 20 61 6c 6c 20 66 72 61 6d 65 73 20 66 72 6f d all frames fro
19b76 6d 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e 20 m the log file.
19b77 2a 2f 0a 20 20 20 20 69 46 72 61 6d 65 20 3d 20 */. iFrame =
19b78 30 3b 0a 20 20 20 20 66 6f 72 28 69 4f 66 66 73 0;. for(iOffs
19b79 65 74 3d 57 41 4c 5f 48 44 52 53 49 5a 45 3b 20 et=WAL_HDRSIZE;
19b7a 28 69 4f 66 66 73 65 74 2b 73 7a 46 72 61 6d 65 (iOffset+szFrame
19b7b 29 3c 3d 6e 53 69 7a 65 3b 20 69 4f 66 66 73 65 )<=nSize; iOffse
19b7c 74 2b 3d 73 7a 46 72 61 6d 65 29 7b 0a 20 20 20 t+=szFrame){.
19b7d 20 20 20 75 33 32 20 70 67 6e 6f 3b 20 20 20 20 u32 pgno;
19b7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19b7f 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 65 20 * Database page
19b80 6e 75 6d 62 65 72 20 66 6f 72 20 66 72 61 6d 65 number for frame
19b81 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 6e 54 */. u32 nT
19b82 72 75 6e 63 61 74 65 3b 20 20 20 20 20 20 20 20 runcate;
19b83 20 20 20 20 20 20 2f 2a 20 64 62 73 69 7a 65 20 /* dbsize
19b84 66 69 65 6c 64 20 66 72 6f 6d 20 66 72 61 6d 65 field from frame
19b85 20 68 65 61 64 65 72 20 2a 2f 0a 0a 20 20 20 20 header */..
19b86 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 64 65 /* Read and de
19b87 63 6f 64 65 20 74 68 65 20 6e 65 78 74 20 6c 6f code the next lo
19b88 67 20 66 72 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 g frame. */.
19b89 20 20 69 46 72 61 6d 65 2b 2b 3b 0a 20 20 20 20 iFrame++;.
19b8a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
19b8b 52 65 61 64 28 70 57 61 6c 2d 3e 70 57 61 6c 46 Read(pWal->pWalF
19b8c 64 2c 20 61 46 72 61 6d 65 2c 20 73 7a 46 72 61 d, aFrame, szFra
19b8d 6d 65 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 me, iOffset);.
19b8e 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
19b8f 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20 TE_OK ) break;.
19b90 20 20 20 20 20 69 73 56 61 6c 69 64 20 3d 20 77 isValid = w
19b91 61 6c 44 65 63 6f 64 65 46 72 61 6d 65 28 70 57 alDecodeFrame(pW
19b92 61 6c 2c 20 26 70 67 6e 6f 2c 20 26 6e 54 72 75 al, &pgno, &nTru
19b93 6e 63 61 74 65 2c 20 61 44 61 74 61 2c 20 61 46 ncate, aData, aF
19b94 72 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 rame);. if(
19b95 20 21 69 73 56 61 6c 69 64 20 29 20 62 72 65 61 !isValid ) brea
19b96 6b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 61 k;. rc = wa
19b97 6c 49 6e 64 65 78 41 70 70 65 6e 64 28 70 57 61 lIndexAppend(pWa
19b98 6c 2c 20 69 46 72 61 6d 65 2c 20 70 67 6e 6f 29 l, iFrame, pgno)
19b99 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
19b9a 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 SQLITE_OK ) brea
19b9b 6b 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 k;.. /* If
19b9c 6e 54 72 75 6e 63 61 74 65 20 69 73 20 6e 6f 6e nTruncate is non
19b9d 2d 7a 65 72 6f 2c 20 74 68 69 73 20 69 73 20 61 -zero, this is a
19b9e 20 63 6f 6d 6d 69 74 20 72 65 63 6f 72 64 2e 20 commit record.
19b9f 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 54 72 */. if( nTr
19ba0 75 6e 63 61 74 65 20 29 7b 0a 20 20 20 20 20 20 uncate ){.
19ba1 20 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 pWal->hdr.mxFr
19ba2 61 6d 65 20 3d 20 69 46 72 61 6d 65 3b 0a 20 20 ame = iFrame;.
19ba3 20 20 20 20 20 20 70 57 61 6c 2d 3e 68 64 72 2e pWal->hdr.
19ba4 6e 50 61 67 65 20 3d 20 6e 54 72 75 6e 63 61 74 nPage = nTruncat
19ba5 65 3b 0a 20 20 20 20 20 20 20 20 70 57 61 6c 2d e;. pWal-
19ba6 3e 68 64 72 2e 73 7a 50 61 67 65 20 3d 20 28 75 >hdr.szPage = (u
19ba7 31 36 29 28 28 73 7a 50 61 67 65 26 30 78 66 66 16)((szPage&0xff
19ba8 30 30 29 20 7c 20 28 73 7a 50 61 67 65 3e 3e 31 00) | (szPage>>1
19ba9 36 29 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 6));. tes
19baa 74 63 61 73 65 28 20 73 7a 50 61 67 65 3c 3d 33 tcase( szPage<=3
19bab 32 37 36 38 20 29 3b 0a 20 20 20 20 20 20 20 20 2768 );.
19bac 74 65 73 74 63 61 73 65 28 20 73 7a 50 61 67 65 testcase( szPage
19bad 3e 3d 36 35 35 33 36 20 29 3b 0a 20 20 20 20 20 >=65536 );.
19bae 20 20 20 61 46 72 61 6d 65 43 6b 73 75 6d 5b 30 aFrameCksum[0
19baf 5d 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 ] = pWal->hdr.aF
19bb0 72 61 6d 65 43 6b 73 75 6d 5b 30 5d 3b 0a 20 20 rameCksum[0];.
19bb1 20 20 20 20 20 20 61 46 72 61 6d 65 43 6b 73 75 aFrameCksu
19bb2 6d 5b 31 5d 20 3d 20 70 57 61 6c 2d 3e 68 64 72 m[1] = pWal->hdr
19bb3 2e 61 46 72 61 6d 65 43 6b 73 75 6d 5b 31 5d 3b .aFrameCksum[1];
19bb4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
19bb5 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
19bb6 28 61 46 72 61 6d 65 29 3b 0a 20 20 7d 0a 0a 66 (aFrame);. }..f
19bb7 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 28 20 72 inished:. if( r
19bb8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
19bb9 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 57 61 6c volatile Wal
19bba 43 6b 70 74 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b CkptInfo *pInfo;
19bbb 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
19bbc 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72 61 6d 65 pWal->hdr.aFrame
19bbd 43 6b 73 75 6d 5b 30 5d 20 3d 20 61 46 72 61 6d Cksum[0] = aFram
19bbe 65 43 6b 73 75 6d 5b 30 5d 3b 0a 20 20 20 20 70 eCksum[0];. p
19bbf 57 61 6c 2d 3e 68 64 72 2e 61 46 72 61 6d 65 43 Wal->hdr.aFrameC
19bc0 6b 73 75 6d 5b 31 5d 20 3d 20 61 46 72 61 6d 65 ksum[1] = aFrame
19bc1 43 6b 73 75 6d 5b 31 5d 3b 0a 20 20 20 20 77 61 Cksum[1];. wa
19bc2 6c 49 6e 64 65 78 57 72 69 74 65 48 64 72 28 70 lIndexWriteHdr(p
19bc3 57 61 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 Wal);.. /* Re
19bc4 73 65 74 20 74 68 65 20 63 68 65 63 6b 70 6f 69 set the checkpoi
19bc5 6e 74 2d 68 65 61 64 65 72 2e 20 54 68 69 73 20 nt-header. This
19bc6 69 73 20 73 61 66 65 20 62 65 63 61 75 73 65 20 is safe because
19bc7 74 68 69 73 20 74 68 72 65 61 64 20 69 73 20 0a this thread is .
19bc8 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 6c 79 ** currently
19bc9 20 68 6f 6c 64 69 6e 67 20 6c 6f 63 6b 73 20 74 holding locks t
19bca 68 61 74 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 hat exclude all
19bcb 6f 74 68 65 72 20 72 65 61 64 65 72 73 2c 20 77 other readers, w
19bcc 72 69 74 65 72 73 20 61 6e 64 0a 20 20 20 20 2a riters and. *
19bcd 2a 20 63 68 65 63 6b 70 6f 69 6e 74 65 72 73 2e * checkpointers.
19bce 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 6e 66 . */. pInf
19bcf 6f 20 3d 20 77 61 6c 43 6b 70 74 49 6e 66 6f 28 o = walCkptInfo(
19bd0 70 57 61 6c 29 3b 0a 20 20 20 20 70 49 6e 66 6f pWal);. pInfo
19bd1 2d 3e 6e 42 61 63 6b 66 69 6c 6c 20 3d 20 30 3b ->nBackfill = 0;
19bd2 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 61 52 65 61 . pInfo->aRea
19bd3 64 4d 61 72 6b 5b 30 5d 20 3d 20 30 3b 0a 20 20 dMark[0] = 0;.
19bd4 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 57 41 4c for(i=1; i<WAL
19bd5 5f 4e 52 45 41 44 45 52 3b 20 69 2b 2b 29 20 70 _NREADER; i++) p
19bd6 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61 72 6b 5b Info->aReadMark[
19bd7 69 5d 20 3d 20 52 45 41 44 4d 41 52 4b 5f 4e 4f i] = READMARK_NO
19bd8 54 5f 55 53 45 44 3b 0a 20 20 20 20 69 66 28 20 T_USED;. if(
19bd9 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d pWal->hdr.mxFram
19bda 65 20 29 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 e ) pInfo->aRead
19bdb 4d 61 72 6b 5b 31 5d 20 3d 20 70 57 61 6c 2d 3e Mark[1] = pWal->
19bdc 68 64 72 2e 6d 78 46 72 61 6d 65 3b 0a 0a 20 20 hdr.mxFrame;..
19bdd 20 20 2f 2a 20 49 66 20 6d 6f 72 65 20 74 68 61 /* If more tha
19bde 6e 20 6f 6e 65 20 66 72 61 6d 65 20 77 61 73 20 n one frame was
19bdf 72 65 63 6f 76 65 72 65 64 20 66 72 6f 6d 20 74 recovered from t
19be0 68 65 20 6c 6f 67 20 66 69 6c 65 2c 20 72 65 70 he log file, rep
19be1 6f 72 74 20 61 6e 0a 20 20 20 20 2a 2a 20 65 76 ort an. ** ev
19be2 65 6e 74 20 76 69 61 20 73 71 6c 69 74 65 33 5f ent via sqlite3_
19be3 6c 6f 67 28 29 2e 20 54 68 69 73 20 69 73 20 74 log(). This is t
19be4 6f 20 68 65 6c 70 20 77 69 74 68 20 69 64 65 6e o help with iden
19be5 74 69 66 79 69 6e 67 20 70 65 72 66 6f 72 6d 61 tifying performa
19be6 6e 63 65 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c nce. ** probl
19be7 65 6d 73 20 63 61 75 73 65 64 20 62 79 20 61 70 ems caused by ap
19be8 70 6c 69 63 61 74 69 6f 6e 73 20 72 6f 75 74 69 plications routi
19be9 6e 65 6c 79 20 73 68 75 74 74 69 6e 67 20 64 6f nely shutting do
19bea 77 6e 20 77 69 74 68 6f 75 74 0a 20 20 20 20 2a wn without. *
19beb 2a 20 63 68 65 63 6b 70 6f 69 6e 74 69 6e 67 20 * checkpointing
19bec 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e 0a 20 20 the log file..
19bed 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 57 61 */. if( pWa
19bee 6c 2d 3e 68 64 72 2e 6e 50 61 67 65 20 29 7b 0a l->hdr.nPage ){.
19bef 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f sqlite3_lo
19bf0 67 28 53 51 4c 49 54 45 5f 4f 4b 2c 20 22 52 65 g(SQLITE_OK, "Re
19bf1 63 6f 76 65 72 65 64 20 25 64 20 66 72 61 6d 65 covered %d frame
19bf2 73 20 66 72 6f 6d 20 57 41 4c 20 66 69 6c 65 20 s from WAL file
19bf3 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 %s",. p
19bf4 57 61 6c 2d 3e 68 64 72 2e 6e 50 61 67 65 2c 20 Wal->hdr.nPage,
19bf5 70 57 61 6c 2d 3e 7a 57 61 6c 4e 61 6d 65 0a 20 pWal->zWalName.
19bf6 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 );. }.
19bf7 7d 0a 0a 72 65 63 6f 76 65 72 79 5f 65 72 72 6f }..recovery_erro
19bf8 72 3a 0a 20 20 57 41 4c 54 52 41 43 45 28 28 22 r:. WALTRACE(("
19bf9 57 41 4c 25 70 3a 20 72 65 63 6f 76 65 72 79 20 WAL%p: recovery
19bfa 25 73 5c 6e 22 2c 20 70 57 61 6c 2c 20 72 63 20 %s\n", pWal, rc
19bfb 3f 20 22 66 61 69 6c 65 64 22 20 3a 20 22 6f 6b ? "failed" : "ok
19bfc 22 29 29 3b 0a 20 20 77 61 6c 55 6e 6c 6f 63 6b "));. walUnlock
19bfd 45 78 63 6c 75 73 69 76 65 28 70 57 61 6c 2c 20 Exclusive(pWal,
19bfe 69 4c 6f 63 6b 2c 20 6e 4c 6f 63 6b 29 3b 0a 20 iLock, nLock);.
19bff 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
19c00 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70 *.** Close an op
19c01 65 6e 20 77 61 6c 2d 69 6e 64 65 78 2e 0a 2a 2f en wal-index..*/
19c02 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 61 6c .static void wal
19c03 49 6e 64 65 78 43 6c 6f 73 65 28 57 61 6c 20 2a IndexClose(Wal *
19c04 70 57 61 6c 2c 20 69 6e 74 20 69 73 44 65 6c 65 pWal, int isDele
19c05 74 65 29 7b 0a 20 20 69 66 28 20 70 57 61 6c 2d te){. if( pWal-
19c06 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3d 3d >exclusiveMode==
19c07 57 41 4c 5f 48 45 41 50 4d 45 4d 4f 52 59 5f 4d WAL_HEAPMEMORY_M
19c08 4f 44 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 ODE ){. int i
19c09 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
19c0a 3c 70 57 61 6c 2d 3e 6e 57 69 44 61 74 61 3b 20 <pWal->nWiData;
19c0b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 i++){. sqli
19c0c 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20 2a te3_free((void *
19c0d 29 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 61 5b )pWal->apWiData[
19c0e 69 5d 29 3b 0a 20 20 20 20 20 20 70 57 61 6c 2d i]);. pWal-
19c0f 3e 61 70 57 69 44 61 74 61 5b 69 5d 20 3d 20 30 >apWiData[i] = 0
19c10 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b ;. }. }else{
19c11 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 68 . sqlite3OsSh
19c12 6d 55 6e 6d 61 70 28 70 57 61 6c 2d 3e 70 44 62 mUnmap(pWal->pDb
19c13 46 64 2c 20 69 73 44 65 6c 65 74 65 29 3b 0a 20 Fd, isDelete);.
19c14 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 }.}../* .** Ope
19c15 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 n a connection t
19c16 6f 20 74 68 65 20 57 41 4c 20 66 69 6c 65 20 7a o the WAL file z
19c17 57 61 6c 4e 61 6d 65 2e 20 54 68 65 20 64 61 74 WalName. The dat
19c18 61 62 61 73 65 20 66 69 6c 65 20 6d 75 73 74 20 abase file must
19c19 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 20 6f .** already be o
19c1a 70 65 6e 65 64 20 6f 6e 20 63 6f 6e 6e 65 63 74 pened on connect
19c1b 69 6f 6e 20 70 44 62 46 64 2e 20 54 68 65 20 62 ion pDbFd. The b
19c1c 75 66 66 65 72 20 74 68 61 74 20 7a 57 61 6c 4e uffer that zWalN
19c1d 61 6d 65 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f ame points.** to
19c1e 20 6d 75 73 74 20 72 65 6d 61 69 6e 20 76 61 6c must remain val
19c1f 69 64 20 66 6f 72 20 74 68 65 20 6c 69 66 65 74 id for the lifet
19c20 69 6d 65 20 6f 66 20 74 68 65 20 72 65 74 75 72 ime of the retur
19c21 6e 65 64 20 57 61 6c 2a 20 68 61 6e 64 6c 65 2e ned Wal* handle.
19c22 0a 2a 2a 0a 2a 2a 20 41 20 53 48 41 52 45 44 20 .**.** A SHARED
19c23 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 68 lock should be h
19c24 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 eld on the datab
19c25 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 74 68 ase file when th
19c26 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 is function.** i
19c27 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 20 70 75 s called. The pu
19c28 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 53 48 rpose of this SH
19c29 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 74 6f 20 ARED lock is to
19c2a 70 72 65 76 65 6e 74 20 61 6e 79 20 6f 74 68 65 prevent any othe
19c2b 72 0a 2a 2a 20 63 6c 69 65 6e 74 20 66 72 6f 6d r.** client from
19c2c 20 75 6e 6c 69 6e 6b 69 6e 67 20 74 68 65 20 57 unlinking the W
19c2d 41 4c 20 6f 72 20 77 61 6c 2d 69 6e 64 65 78 20 AL or wal-index
19c2e 66 69 6c 65 2e 20 49 66 20 61 6e 6f 74 68 65 72 file. If another
19c2f 20 70 72 6f 63 65 73 73 0a 2a 2a 20 77 65 72 65 process.** were
19c30 20 74 6f 20 64 6f 20 74 68 69 73 20 6a 75 73 74 to do this just
19c31 20 61 66 74 65 72 20 74 68 69 73 20 63 6c 69 65 after this clie
19c32 6e 74 20 6f 70 65 6e 65 64 20 6f 6e 65 20 6f 66 nt opened one of
19c33 20 74 68 65 73 65 20 66 69 6c 65 73 2c 20 74 68 these files, th
19c34 65 0a 2a 2a 20 73 79 73 74 65 6d 20 77 6f 75 6c e.** system woul
19c35 64 20 62 65 20 62 61 64 6c 79 20 62 72 6f 6b 65 d be badly broke
19c36 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 n..**.** If the
19c37 6c 6f 67 20 66 69 6c 65 20 69 73 20 73 75 63 63 log file is succ
19c38 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c essfully opened,
19c39 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
19c3a 74 75 72 6e 65 64 20 61 6e 64 20 0a 2a 2a 20 2a turned and .** *
19c3b 70 70 57 61 6c 20 69 73 20 73 65 74 20 74 6f 20 ppWal is set to
19c3c 70 6f 69 6e 74 20 74 6f 20 61 20 6e 65 77 20 57 point to a new W
19c3d 41 4c 20 68 61 6e 64 6c 65 2e 20 49 66 20 61 6e AL handle. If an
19c3e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a error occurs,.*
19c3f 2a 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f * an SQLite erro
19c40 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
19c41 65 64 20 61 6e 64 20 2a 70 70 57 61 6c 20 69 73 ed and *ppWal is
19c42 20 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65 64 left unmodified
19c43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19c44 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
19c45 61 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 alOpen(. sqlite
19c46 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 3_vfs *pVfs,
19c47 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76 66 73 /* vfs
19c48 20 6d 6f 64 75 6c 65 20 74 6f 20 6f 70 65 6e 20 module to open
19c49 77 61 6c 20 61 6e 64 20 77 61 6c 2d 69 6e 64 65 wal and wal-inde
19c4a 78 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 x */. sqlite3_f
19c4b 69 6c 65 20 2a 70 44 62 46 64 2c 20 20 20 20 20 ile *pDbFd,
19c4c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 /* The op
19c4d 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 en database file
19c4e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
19c4f 20 2a 7a 57 61 6c 4e 61 6d 65 2c 20 20 20 20 20 *zWalName,
19c50 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
19c51 20 74 68 65 20 57 41 4c 20 66 69 6c 65 20 2a 2f the WAL file */
19c52 0a 20 20 69 6e 74 20 62 4e 6f 53 68 6d 2c 20 20 . int bNoShm,
19c53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19c54 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 72 75 /* True to ru
19c55 6e 20 69 6e 20 68 65 61 70 2d 6d 65 6d 6f 72 79 n in heap-memory
19c56 20 6d 6f 64 65 20 2a 2f 0a 20 20 69 36 34 20 6d mode */. i64 m
19c57 78 57 61 6c 53 69 7a 65 2c 20 20 20 20 20 20 20 xWalSize,
19c58 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
19c59 75 6e 63 61 74 65 20 57 41 4c 20 74 6f 20 74 68 uncate WAL to th
19c5a 69 73 20 73 69 7a 65 20 6f 6e 20 72 65 73 65 74 is size on reset
19c5b 20 2a 2f 0a 20 20 57 61 6c 20 2a 2a 70 70 57 61 */. Wal **ppWa
19c5c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l
19c5d 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 6c /* OUT: Al
19c5e 6c 6f 63 61 74 65 64 20 57 61 6c 20 68 61 6e 64 located Wal hand
19c5f 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 le */.){. int r
19c60 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
19c61 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
19c62 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 57 turn Code */. W
19c63 61 6c 20 2a 70 52 65 74 3b 20 20 20 20 20 20 20 al *pRet;
19c64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19c65 2a 20 4f 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f * Object to allo
19c66 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 cate and return
19c67 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 */. int flags;
19c68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19c69 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 /* Flags pa
19c6a 73 73 65 64 20 74 6f 20 4f 73 4f 70 65 6e 28 29 ssed to OsOpen()
19c6b 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 7a */.. assert( z
19c6c 57 61 6c 4e 61 6d 65 20 26 26 20 7a 57 61 6c 4e WalName && zWalN
19c6d 61 6d 65 5b 30 5d 20 29 3b 0a 20 20 61 73 73 65 ame[0] );. asse
19c6e 72 74 28 20 70 44 62 46 64 20 29 3b 0a 0a 20 20 rt( pDbFd );..
19c6f 2f 2a 20 49 6e 20 74 68 65 20 61 6d 61 6c 67 61 /* In the amalga
19c70 6d 61 74 69 6f 6e 2c 20 74 68 65 20 6f 73 5f 75 mation, the os_u
19c71 6e 69 78 2e 63 20 61 6e 64 20 6f 73 5f 77 69 6e nix.c and os_win
19c72 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20 .c source files
19c73 63 6f 6d 65 20 62 65 66 6f 72 65 0a 20 20 2a 2a come before. **
19c74 20 74 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c this source fil
19c75 65 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20 e. Verify that
19c76 74 68 65 20 23 64 65 66 69 6e 65 73 20 6f 66 20 the #defines of
19c77 74 68 65 20 6c 6f 63 6b 69 6e 67 20 62 79 74 65 the locking byte
19c78 20 6f 66 66 73 65 74 73 0a 20 20 2a 2a 20 69 6e offsets. ** in
19c79 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6e 64 20 6f os_unix.c and o
19c7a 73 5f 77 69 6e 2e 63 20 61 67 72 65 65 20 77 69 s_win.c agree wi
19c7b 74 68 20 74 68 65 20 57 41 4c 49 4e 44 45 58 5f th the WALINDEX_
19c7c 4c 4f 43 4b 5f 4f 46 46 53 45 54 20 76 61 6c 75 LOCK_OFFSET valu
19c7d 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 57 e.. */.#ifdef W
19c7e 49 4e 5f 53 48 4d 5f 42 41 53 45 0a 20 20 61 73 IN_SHM_BASE. as
19c7f 73 65 72 74 28 20 57 49 4e 5f 53 48 4d 5f 42 41 sert( WIN_SHM_BA
19c80 53 45 3d 3d 57 41 4c 49 4e 44 45 58 5f 4c 4f 43 SE==WALINDEX_LOC
19c81 4b 5f 4f 46 46 53 45 54 20 29 3b 0a 23 65 6e 64 K_OFFSET );.#end
19c82 69 66 0a 23 69 66 64 65 66 20 55 4e 49 58 5f 53 if.#ifdef UNIX_S
19c83 48 4d 5f 42 41 53 45 0a 20 20 61 73 73 65 72 74 HM_BASE. assert
19c84 28 20 55 4e 49 58 5f 53 48 4d 5f 42 41 53 45 3d ( UNIX_SHM_BASE=
19c85 3d 57 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b 5f 4f =WALINDEX_LOCK_O
19c86 46 46 53 45 54 20 29 3b 0a 23 65 6e 64 69 66 0a FFSET );.#endif.
19c87 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 .. /* Allocate
19c88 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 73 an instance of s
19c89 74 72 75 63 74 20 57 61 6c 20 74 6f 20 72 65 74 truct Wal to ret
19c8a 75 72 6e 2e 20 2a 2f 0a 20 20 2a 70 70 57 61 6c urn. */. *ppWal
19c8b 20 3d 20 30 3b 0a 20 20 70 52 65 74 20 3d 20 28 = 0;. pRet = (
19c8c 57 61 6c 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c Wal*)sqlite3Mall
19c8d 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 57 61 ocZero(sizeof(Wa
19c8e 6c 29 20 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 l) + pVfs->szOsF
19c8f 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70 52 65 ile);. if( !pRe
19c90 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
19c91 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
19c92 7d 0a 0a 20 20 70 52 65 74 2d 3e 70 56 66 73 20 }.. pRet->pVfs
19c93 3d 20 70 56 66 73 3b 0a 20 20 70 52 65 74 2d 3e = pVfs;. pRet->
19c94 70 57 61 6c 46 64 20 3d 20 28 73 71 6c 69 74 65 pWalFd = (sqlite
19c95 33 5f 66 69 6c 65 20 2a 29 26 70 52 65 74 5b 31 3_file *)&pRet[1
19c96 5d 3b 0a 20 20 70 52 65 74 2d 3e 70 44 62 46 64 ];. pRet->pDbFd
19c97 20 3d 20 70 44 62 46 64 3b 0a 20 20 70 52 65 74 = pDbFd;. pRet
19c98 2d 3e 72 65 61 64 4c 6f 63 6b 20 3d 20 2d 31 3b ->readLock = -1;
19c99 0a 20 20 70 52 65 74 2d 3e 6d 78 57 61 6c 53 69 . pRet->mxWalSi
19c9a 7a 65 20 3d 20 6d 78 57 61 6c 53 69 7a 65 3b 0a ze = mxWalSize;.
19c9b 20 20 70 52 65 74 2d 3e 7a 57 61 6c 4e 61 6d 65 pRet->zWalName
19c9c 20 3d 20 7a 57 61 6c 4e 61 6d 65 3b 0a 20 20 70 = zWalName;. p
19c9d 52 65 74 2d 3e 73 79 6e 63 48 65 61 64 65 72 20 Ret->syncHeader
19c9e 3d 20 31 3b 0a 20 20 70 52 65 74 2d 3e 70 61 64 = 1;. pRet->pad
19c9f 54 6f 53 65 63 74 6f 72 42 6f 75 6e 64 61 72 79 ToSectorBoundary
19ca0 20 3d 20 31 3b 0a 20 20 70 52 65 74 2d 3e 65 78 = 1;. pRet->ex
19ca1 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 62 clusiveMode = (b
19ca2 4e 6f 53 68 6d 20 3f 20 57 41 4c 5f 48 45 41 50 NoShm ? WAL_HEAP
19ca3 4d 45 4d 4f 52 59 5f 4d 4f 44 45 3a 20 57 41 4c MEMORY_MODE: WAL
19ca4 5f 4e 4f 52 4d 41 4c 5f 4d 4f 44 45 29 3b 0a 0a _NORMAL_MODE);..
19ca5 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 68 /* Open file h
19ca6 61 6e 64 6c 65 20 6f 6e 20 74 68 65 20 77 72 69 andle on the wri
19ca7 74 65 2d 61 68 65 61 64 20 6c 6f 67 20 66 69 6c te-ahead log fil
19ca8 65 2e 20 2a 2f 0a 20 20 66 6c 61 67 73 20 3d 20 e. */. flags =
19ca9 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 (SQLITE_OPEN_REA
19caa 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 DWRITE|SQLITE_OP
19cab 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c 49 54 45 EN_CREATE|SQLITE
19cac 5f 4f 50 45 4e 5f 57 41 4c 29 3b 0a 20 20 72 63 _OPEN_WAL);. rc
19cad 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e = sqlite3OsOpen
19cae 28 70 56 66 73 2c 20 7a 57 61 6c 4e 61 6d 65 2c (pVfs, zWalName,
19caf 20 70 52 65 74 2d 3e 70 57 61 6c 46 64 2c 20 66 pRet->pWalFd, f
19cb0 6c 61 67 73 2c 20 26 66 6c 61 67 73 29 3b 0a 20 lags, &flags);.
19cb1 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
19cb2 4f 4b 20 26 26 20 66 6c 61 67 73 26 53 51 4c 49 OK && flags&SQLI
19cb3 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
19cb4 20 29 7b 0a 20 20 20 20 70 52 65 74 2d 3e 72 65 ){. pRet->re
19cb5 61 64 4f 6e 6c 79 20 3d 20 57 41 4c 5f 52 44 4f adOnly = WAL_RDO
19cb6 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 NLY;. }.. if(
19cb7 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
19cb8 0a 20 20 20 20 77 61 6c 49 6e 64 65 78 43 6c 6f . walIndexClo
19cb9 73 65 28 70 52 65 74 2c 20 30 29 3b 0a 20 20 20 se(pRet, 0);.
19cba 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
19cbb 70 52 65 74 2d 3e 70 57 61 6c 46 64 29 3b 0a 20 pRet->pWalFd);.
19cbc 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
19cbd 70 52 65 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a pRet);. }else{.
19cbe 20 20 20 20 69 6e 74 20 69 44 43 20 3d 20 73 71 int iDC = sq
19cbf 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 lite3OsDeviceCha
19cc0 72 61 63 74 65 72 69 73 74 69 63 73 28 70 52 65 racteristics(pRe
19cc1 74 2d 3e 70 57 61 6c 46 64 29 3b 0a 20 20 20 20 t->pWalFd);.
19cc2 69 66 28 20 69 44 43 20 26 20 53 51 4c 49 54 45 if( iDC & SQLITE
19cc3 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 _IOCAP_SEQUENTIA
19cc4 4c 20 29 7b 20 70 52 65 74 2d 3e 73 79 6e 63 48 L ){ pRet->syncH
19cc5 65 61 64 65 72 20 3d 20 30 3b 20 7d 0a 20 20 20 eader = 0; }.
19cc6 20 69 66 28 20 69 44 43 20 26 20 53 51 4c 49 54 if( iDC & SQLIT
19cc7 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52 53 41 46 E_IOCAP_POWERSAF
19cc8 45 5f 4f 56 45 52 57 52 49 54 45 20 29 7b 0a 20 E_OVERWRITE ){.
19cc9 20 20 20 20 20 70 52 65 74 2d 3e 70 61 64 54 6f pRet->padTo
19cca 53 65 63 74 6f 72 42 6f 75 6e 64 61 72 79 20 3d SectorBoundary =
19ccb 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 0;. }. *p
19ccc 70 57 61 6c 20 3d 20 70 52 65 74 3b 0a 20 20 20 pWal = pRet;.
19ccd 20 57 41 4c 54 52 41 43 45 28 28 22 57 41 4c 25 WALTRACE(("WAL%
19cce 64 3a 20 6f 70 65 6e 65 64 5c 6e 22 2c 20 70 52 d: opened\n", pR
19ccf 65 74 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 et));. }. retu
19cd0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
19cd1 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 Change the size
19cd2 74 6f 20 77 68 69 63 68 20 74 68 65 20 57 41 4c to which the WAL
19cd3 20 66 69 6c 65 20 69 73 20 74 72 75 63 61 74 65 file is trucate
19cd4 64 20 6f 6e 20 65 61 63 68 20 72 65 73 65 74 2e d on each reset.
19cd5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19cd6 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 57 TE void sqlite3W
19cd7 61 6c 4c 69 6d 69 74 28 57 61 6c 20 2a 70 57 61 alLimit(Wal *pWa
19cd8 6c 2c 20 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a l, i64 iLimit){.
19cd9 20 20 69 66 28 20 70 57 61 6c 20 29 20 70 57 61 if( pWal ) pWa
19cda 6c 2d 3e 6d 78 57 61 6c 53 69 7a 65 20 3d 20 69 l->mxWalSize = i
19cdb 4c 69 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Limit;.}../*.**
19cdc 46 69 6e 64 20 74 68 65 20 73 6d 61 6c 6c 65 73 Find the smalles
19cdd 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 75 t page number ou
19cde 74 20 6f 66 20 61 6c 6c 20 70 61 67 65 73 20 68 t of all pages h
19cdf 65 6c 64 20 69 6e 20 74 68 65 20 57 41 4c 20 74 eld in the WAL t
19ce0 68 61 74 0a 2a 2a 20 68 61 73 20 6e 6f 74 20 62 hat.** has not b
19ce1 65 65 6e 20 72 65 74 75 72 6e 65 64 20 62 79 20 een returned by
19ce2 61 6e 79 20 70 72 69 6f 72 20 69 6e 76 6f 63 61 any prior invoca
19ce3 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6d 65 74 tion of this met
19ce4 68 6f 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 61 hod on the.** sa
19ce5 6d 65 20 57 61 6c 49 74 65 72 61 74 6f 72 20 6f me WalIterator o
19ce6 62 6a 65 63 74 2e 20 20 20 57 72 69 74 65 20 69 bject. Write i
19ce7 6e 74 6f 20 2a 70 69 46 72 61 6d 65 20 74 68 65 nto *piFrame the
19ce8 20 66 72 61 6d 65 20 69 6e 64 65 78 20 77 68 65 frame index whe
19ce9 72 65 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 20 re.** that page
19cea 77 61 73 20 6c 61 73 74 20 77 72 69 74 74 65 6e was last written
19ceb 20 69 6e 74 6f 20 74 68 65 20 57 41 4c 2e 20 20 into the WAL.
19cec 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 50 61 Write into *piPa
19ced 67 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e ge the page.** n
19cee 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 umber..**.** Ret
19cef 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 urn 0 on success
19cf0 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 . If there are
19cf1 6e 6f 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 no pages in the
19cf2 57 41 4c 20 77 69 74 68 20 61 20 70 61 67 65 0a WAL with a page.
19cf3 2a 2a 20 6e 75 6d 62 65 72 20 6c 61 72 67 65 72 ** number larger
19cf4 20 74 68 61 6e 20 2a 70 69 50 61 67 65 2c 20 74 than *piPage, t
19cf5 68 65 6e 20 72 65 74 75 72 6e 20 31 2e 0a 2a 2f hen return 1..*/
19cf6 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c 49 .static int walI
19cf7 74 65 72 61 74 6f 72 4e 65 78 74 28 0a 20 20 57 teratorNext(. W
19cf8 61 6c 49 74 65 72 61 74 6f 72 20 2a 70 2c 20 20 alIterator *p,
19cf9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19cfa 49 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 33 Iterator */. u3
19cfb 32 20 2a 70 69 50 61 67 65 2c 20 20 20 20 20 20 2 *piPage,
19cfc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
19cfd 55 54 3a 20 54 68 65 20 70 61 67 65 20 6e 75 6d UT: The page num
19cfe 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 ber of the next
19cff 70 61 67 65 20 2a 2f 0a 20 20 75 33 32 20 2a 70 page */. u32 *p
19d00 69 46 72 61 6d 65 20 20 20 20 20 20 20 20 20 20 iFrame
19d01 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 /* OUT:
19d02 57 61 6c 20 66 72 61 6d 65 20 69 6e 64 65 78 20 Wal frame index
19d03 6f 66 20 6e 65 78 74 20 70 61 67 65 20 2a 2f 0a of next page */.
19d04 29 7b 0a 20 20 75 33 32 20 69 4d 69 6e 3b 20 20 ){. u32 iMin;
19d05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19d06 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 70 67 6e /* Result pgn
19d07 6f 20 6d 75 73 74 20 62 65 20 67 72 65 61 74 65 o must be greate
19d08 72 20 74 68 61 6e 20 69 4d 69 6e 20 2a 2f 0a 20 r than iMin */.
19d09 20 75 33 32 20 69 52 65 74 20 3d 20 30 78 46 46 u32 iRet = 0xFF
19d0a 46 46 46 46 46 46 3b 20 20 20 20 20 20 20 20 2f FFFFFF; /
19d0b 2a 20 30 78 66 66 66 66 66 66 66 66 20 69 73 20 * 0xffffffff is
19d0c 6e 65 76 65 72 20 61 20 76 61 6c 69 64 20 70 61 never a valid pa
19d0d 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 ge number */. i
19d0e 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
19d0f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19d10 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f For looping thro
19d11 75 67 68 20 73 65 67 6d 65 6e 74 73 20 2a 2f 0a ugh segments */.
19d12 0a 20 20 69 4d 69 6e 20 3d 20 70 2d 3e 69 50 72 . iMin = p->iPr
19d13 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 69 ior;. assert( i
19d14 4d 69 6e 3c 30 78 66 66 66 66 66 66 66 66 20 29 Min<0xffffffff )
19d15 3b 0a 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 53 65 ;. for(i=p->nSe
19d16 67 6d 65 6e 74 2d 31 3b 20 69 3e 3d 30 3b 20 69 gment-1; i>=0; i
19d17 2d 2d 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 --){. struct
19d18 57 61 6c 53 65 67 6d 65 6e 74 20 2a 70 53 65 67 WalSegment *pSeg
19d19 6d 65 6e 74 20 3d 20 26 70 2d 3e 61 53 65 67 6d ment = &p->aSegm
19d1a 65 6e 74 5b 69 5d 3b 0a 20 20 20 20 77 68 69 6c ent[i];. whil
19d1b 65 28 20 70 53 65 67 6d 65 6e 74 2d 3e 69 4e 65 e( pSegment->iNe
19d1c 78 74 3c 70 53 65 67 6d 65 6e 74 2d 3e 6e 45 6e xt<pSegment->nEn
19d1d 74 72 79 20 29 7b 0a 20 20 20 20 20 20 75 33 32 try ){. u32
19d1e 20 69 50 67 20 3d 20 70 53 65 67 6d 65 6e 74 2d iPg = pSegment-
19d1f 3e 61 50 67 6e 6f 5b 70 53 65 67 6d 65 6e 74 2d >aPgno[pSegment-
19d20 3e 61 49 6e 64 65 78 5b 70 53 65 67 6d 65 6e 74 >aIndex[pSegment
19d21 2d 3e 69 4e 65 78 74 5d 5d 3b 0a 20 20 20 20 20 ->iNext]];.
19d22 20 69 66 28 20 69 50 67 3e 69 4d 69 6e 20 29 7b if( iPg>iMin ){
19d23 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 50 67 . if( iPg
19d24 3c 69 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20 <iRet ){.
19d25 20 20 20 69 52 65 74 20 3d 20 69 50 67 3b 0a 20 iRet = iPg;.
19d26 20 20 20 20 20 20 20 20 20 2a 70 69 46 72 61 6d *piFram
19d27 65 20 3d 20 70 53 65 67 6d 65 6e 74 2d 3e 69 5a e = pSegment->iZ
19d28 65 72 6f 20 2b 20 70 53 65 67 6d 65 6e 74 2d 3e ero + pSegment->
19d29 61 49 6e 64 65 78 5b 70 53 65 67 6d 65 6e 74 2d aIndex[pSegment-
19d2a 3e 69 4e 65 78 74 5d 3b 0a 20 20 20 20 20 20 20 >iNext];.
19d2b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b }. break
19d2c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
19d2d 70 53 65 67 6d 65 6e 74 2d 3e 69 4e 65 78 74 2b pSegment->iNext+
19d2e 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 +;. }. }..
19d2f 2a 70 69 50 61 67 65 20 3d 20 70 2d 3e 69 50 72 *piPage = p->iPr
19d30 69 6f 72 20 3d 20 69 52 65 74 3b 0a 20 20 72 65 ior = iRet;. re
19d31 74 75 72 6e 20 28 69 52 65 74 3d 3d 30 78 46 46 turn (iRet==0xFF
19d32 46 46 46 46 46 46 29 3b 0a 7d 0a 0a 2f 2a 0a 2a FFFFFF);.}../*.*
19d33 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
19d34 6d 65 72 67 65 73 20 74 77 6f 20 73 6f 72 74 65 merges two sorte
19d35 64 20 6c 69 73 74 73 20 69 6e 74 6f 20 61 20 73 d lists into a s
19d36 69 6e 67 6c 65 20 73 6f 72 74 65 64 20 6c 69 73 ingle sorted lis
19d37 74 2e 0a 2a 2a 0a 2a 2a 20 61 4c 65 66 74 5b 5d t..**.** aLeft[]
19d38 20 61 6e 64 20 61 52 69 67 68 74 5b 5d 20 61 72 and aRight[] ar
19d39 65 20 61 72 72 61 79 73 20 6f 66 20 69 6e 64 69 e arrays of indi
19d3a 63 65 73 2e 20 20 54 68 65 20 73 6f 72 74 20 6b ces. The sort k
19d3b 65 79 20 69 73 0a 2a 2a 20 61 43 6f 6e 74 65 6e ey is.** aConten
19d3c 74 5b 61 4c 65 66 74 5b 5d 5d 20 61 6e 64 20 61 t[aLeft[]] and a
19d3d 43 6f 6e 74 65 6e 74 5b 61 52 69 67 68 74 5b 5d Content[aRight[]
19d3e 5d 2e 20 20 55 70 6f 6e 20 65 6e 74 72 79 2c 20 ]. Upon entry,
19d3f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a the following.**
19d40 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 66 is guaranteed f
19d41 6f 72 20 61 6c 6c 20 4a 3c 4b 3a 0a 2a 2a 0a 2a or all J<K:.**.*
19d42 2a 20 20 20 20 20 20 20 20 61 43 6f 6e 74 65 6e * aConten
19d43 74 5b 61 4c 65 66 74 5b 4a 5d 5d 20 3c 20 61 43 t[aLeft[J]] < aC
19d44 6f 6e 74 65 6e 74 5b 61 4c 65 66 74 5b 4b 5d 5d ontent[aLeft[K]]
19d45 0a 2a 2a 20 20 20 20 20 20 20 20 61 43 6f 6e 74 .** aCont
19d46 65 6e 74 5b 61 52 69 67 68 74 5b 4a 5d 5d 20 3c ent[aRight[J]] <
19d47 20 61 43 6f 6e 74 65 6e 74 5b 61 52 69 67 68 74 aContent[aRight
19d48 5b 4b 5d 5d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 [K]].**.** This
19d49 72 6f 75 74 69 6e 65 20 6f 76 65 72 77 72 69 74 routine overwrit
19d4a 65 73 20 61 52 69 67 68 74 5b 5d 20 77 69 74 68 es aRight[] with
19d4b 20 61 20 6e 65 77 20 28 70 72 6f 62 61 62 6c 79 a new (probably
19d4c 20 6c 6f 6e 67 65 72 29 20 73 65 71 75 65 6e 63 longer) sequenc
19d4d 65 0a 2a 2a 20 6f 66 20 69 6e 64 69 63 65 73 20 e.** of indices
19d4e 73 75 63 68 20 74 68 61 74 20 74 68 65 20 61 52 such that the aR
19d4f 69 67 68 74 5b 5d 20 63 6f 6e 74 61 69 6e 73 20 ight[] contains
19d50 65 76 65 72 79 20 69 6e 64 65 78 20 74 68 61 74 every index that
19d51 20 61 70 70 65 61 72 73 20 69 6e 0a 2a 2a 20 65 appears in.** e
19d52 69 74 68 65 72 20 61 4c 65 66 74 5b 5d 20 6f 72 ither aLeft[] or
19d53 20 74 68 65 20 6f 6c 64 20 61 52 69 67 68 74 5b the old aRight[
19d54 5d 20 61 6e 64 20 73 75 63 68 20 74 68 61 74 20 ] and such that
19d55 74 68 65 20 73 65 63 6f 6e 64 20 63 6f 6e 64 69 the second condi
19d56 74 69 6f 6e 0a 2a 2a 20 61 62 6f 76 65 20 69 73 tion.** above is
19d57 20 73 74 69 6c 6c 20 6d 65 74 2e 0a 2a 2a 0a 2a still met..**.*
19d58 2a 20 54 68 65 20 61 43 6f 6e 74 65 6e 74 5b 61 * The aContent[a
19d59 4c 65 66 74 5b 58 5d 5d 20 76 61 6c 75 65 73 20 Left[X]] values
19d5a 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 20 66 will be unique f
19d5b 6f 72 20 61 6c 6c 20 58 2e 20 20 41 6e 64 20 74 or all X. And t
19d5c 68 65 0a 2a 2a 20 61 43 6f 6e 74 65 6e 74 5b 61 he.** aContent[a
19d5d 52 69 67 68 74 5b 58 5d 5d 20 76 61 6c 75 65 73 Right[X]] values
19d5e 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 20 will be unique
19d5f 74 6f 6f 2e 20 20 42 75 74 20 74 68 65 72 65 20 too. But there
19d60 6d 69 67 68 74 20 62 65 0a 2a 2a 20 6f 6e 65 20 might be.** one
19d61 6f 72 20 6d 6f 72 65 20 63 6f 6d 62 69 6e 61 74 or more combinat
19d62 69 6f 6e 73 20 6f 66 20 58 20 61 6e 64 20 59 20 ions of X and Y
19d63 73 75 63 68 20 74 68 61 74 0a 2a 2a 0a 2a 2a 20 such that.**.**
19d64 20 20 20 20 20 61 4c 65 66 74 5b 58 5d 21 3d 61 aLeft[X]!=a
19d65 52 69 67 68 74 5b 59 5d 20 20 26 26 20 20 61 43 Right[Y] && aC
19d66 6f 6e 74 65 6e 74 5b 61 4c 65 66 74 5b 58 5d 5d ontent[aLeft[X]]
19d67 20 3d 3d 20 61 43 6f 6e 74 65 6e 74 5b 61 52 69 == aContent[aRi
19d68 67 68 74 5b 59 5d 5d 0a 2a 2a 0a 2a 2a 20 57 68 ght[Y]].**.** Wh
19d69 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c en that happens,
19d6a 20 6f 6d 69 74 20 74 68 65 20 61 4c 65 66 74 5b omit the aLeft[
19d6b 58 5d 20 61 6e 64 20 75 73 65 20 74 68 65 20 61 X] and use the a
19d6c 52 69 67 68 74 5b 59 5d 20 69 6e 64 65 78 2e 0a Right[Y] index..
19d6d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 */.static void w
19d6e 61 6c 4d 65 72 67 65 28 0a 20 20 63 6f 6e 73 74 alMerge(. const
19d6f 20 75 33 32 20 2a 61 43 6f 6e 74 65 6e 74 2c 20 u32 *aContent,
19d70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
19d71 67 65 73 20 69 6e 20 77 61 6c 20 2d 20 6b 65 79 ges in wal - key
19d72 73 20 66 6f 72 20 74 68 65 20 73 6f 72 74 20 2a s for the sort *
19d73 2f 0a 20 20 68 74 5f 73 6c 6f 74 20 2a 61 4c 65 /. ht_slot *aLe
19d74 66 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ft,
19d75 20 20 20 20 2f 2a 20 49 4e 3a 20 4c 65 66 74 20 /* IN: Left
19d76 68 61 6e 64 20 69 6e 70 75 74 20 6c 69 73 74 20 hand input list
19d77 2a 2f 0a 20 20 69 6e 74 20 6e 4c 65 66 74 2c 20 */. int nLeft,
19d78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19d79 20 20 20 20 20 2f 2a 20 49 4e 3a 20 45 6c 65 6d /* IN: Elem
19d7a 65 6e 74 73 20 69 6e 20 61 72 72 61 79 20 2a 70 ents in array *p
19d7b 61 4c 65 66 74 20 2a 2f 0a 20 20 68 74 5f 73 6c aLeft */. ht_sl
19d7c 6f 74 20 2a 2a 70 61 52 69 67 68 74 2c 20 20 20 ot **paRight,
19d7d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e /* IN
19d7e 2f 4f 55 54 3a 20 52 69 67 68 74 20 68 61 6e 64 /OUT: Right hand
19d7f 20 69 6e 70 75 74 20 6c 69 73 74 20 2a 2f 0a 20 input list */.
19d80 20 69 6e 74 20 2a 70 6e 52 69 67 68 74 2c 20 20 int *pnRight,
19d81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19d82 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 45 6c 65 6d /* IN/OUT: Elem
19d83 65 6e 74 73 20 69 6e 20 2a 70 61 52 69 67 68 74 ents in *paRight
19d84 20 2a 2f 0a 20 20 68 74 5f 73 6c 6f 74 20 2a 61 */. ht_slot *a
19d85 54 6d 70 20 20 20 20 20 20 20 20 20 20 20 20 20 Tmp
19d86 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 /* Tempora
19d87 72 79 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a ry buffer */.){.
19d88 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30 3b int iLeft = 0;
19d89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19d8a 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 64 /* Current ind
19d8b 65 78 20 69 6e 20 61 4c 65 66 74 20 2a 2f 0a 20 ex in aLeft */.
19d8c 20 69 6e 74 20 69 52 69 67 68 74 20 3d 20 30 3b int iRight = 0;
19d8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19d8e 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 64 65 /* Current inde
19d8f 78 20 69 6e 20 61 52 69 67 68 74 20 2a 2f 0a 20 x in aRight */.
19d90 20 69 6e 74 20 69 4f 75 74 20 3d 20 30 3b 20 20 int iOut = 0;
19d91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19d92 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 64 65 /* Current inde
19d93 78 20 69 6e 20 6f 75 74 70 75 74 20 62 75 66 66 x in output buff
19d94 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 69 67 er */. int nRig
19d95 68 74 20 3d 20 2a 70 6e 52 69 67 68 74 3b 0a 20 ht = *pnRight;.
19d96 20 68 74 5f 73 6c 6f 74 20 2a 61 52 69 67 68 74 ht_slot *aRight
19d97 20 3d 20 2a 70 61 52 69 67 68 74 3b 0a 0a 20 20 = *paRight;..
19d98 61 73 73 65 72 74 28 20 6e 4c 65 66 74 3e 30 20 assert( nLeft>0
19d99 26 26 20 6e 52 69 67 68 74 3e 30 20 29 3b 0a 20 && nRight>0 );.
19d9a 20 77 68 69 6c 65 28 20 69 52 69 67 68 74 3c 6e while( iRight<n
19d9b 52 69 67 68 74 20 7c 7c 20 69 4c 65 66 74 3c 6e Right || iLeft<n
19d9c 4c 65 66 74 20 29 7b 0a 20 20 20 20 68 74 5f 73 Left ){. ht_s
19d9d 6c 6f 74 20 6c 6f 67 70 61 67 65 3b 0a 20 20 20 lot logpage;.
19d9e 20 50 67 6e 6f 20 64 62 70 61 67 65 3b 0a 0a 20 Pgno dbpage;..
19d9f 20 20 20 69 66 28 20 28 69 4c 65 66 74 3c 6e 4c if( (iLeft<nL
19da0 65 66 74 29 20 0a 20 20 20 20 20 26 26 20 28 69 eft) . && (i
19da1 52 69 67 68 74 3e 3d 6e 52 69 67 68 74 20 7c 7c Right>=nRight ||
19da2 20 61 43 6f 6e 74 65 6e 74 5b 61 4c 65 66 74 5b aContent[aLeft[
19da3 69 4c 65 66 74 5d 5d 3c 61 43 6f 6e 74 65 6e 74 iLeft]]<aContent
19da4 5b 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d 5d [aRight[iRight]]
19da5 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6c ). ){. l
19da6 6f 67 70 61 67 65 20 3d 20 61 4c 65 66 74 5b 69 ogpage = aLeft[i
19da7 4c 65 66 74 2b 2b 5d 3b 0a 20 20 20 20 7d 65 6c Left++];. }el
19da8 73 65 7b 0a 20 20 20 20 20 20 6c 6f 67 70 61 67 se{. logpag
19da9 65 20 3d 20 61 52 69 67 68 74 5b 69 52 69 67 68 e = aRight[iRigh
19daa 74 2b 2b 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 t++];. }.
19dab 64 62 70 61 67 65 20 3d 20 61 43 6f 6e 74 65 6e dbpage = aConten
19dac 74 5b 6c 6f 67 70 61 67 65 5d 3b 0a 0a 20 20 20 t[logpage];..
19dad 20 61 54 6d 70 5b 69 4f 75 74 2b 2b 5d 20 3d 20 aTmp[iOut++] =
19dae 6c 6f 67 70 61 67 65 3b 0a 20 20 20 20 69 66 28 logpage;. if(
19daf 20 69 4c 65 66 74 3c 6e 4c 65 66 74 20 26 26 20 iLeft<nLeft &&
19db0 61 43 6f 6e 74 65 6e 74 5b 61 4c 65 66 74 5b 69 aContent[aLeft[i
19db1 4c 65 66 74 5d 5d 3d 3d 64 62 70 61 67 65 20 29 Left]]==dbpage )
19db2 20 69 4c 65 66 74 2b 2b 3b 0a 0a 20 20 20 20 61 iLeft++;.. a
19db3 73 73 65 72 74 28 20 69 4c 65 66 74 3e 3d 6e 4c ssert( iLeft>=nL
19db4 65 66 74 20 7c 7c 20 61 43 6f 6e 74 65 6e 74 5b eft || aContent[
19db5 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d 3e 64 62 aLeft[iLeft]]>db
19db6 70 61 67 65 20 29 3b 0a 20 20 20 20 61 73 73 65 page );. asse
19db7 72 74 28 20 69 52 69 67 68 74 3e 3d 6e 52 69 67 rt( iRight>=nRig
19db8 68 74 20 7c 7c 20 61 43 6f 6e 74 65 6e 74 5b 61 ht || aContent[a
19db9 52 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 3e 64 Right[iRight]]>d
19dba 62 70 61 67 65 20 29 3b 0a 20 20 7d 0a 0a 20 20 bpage );. }..
19dbb 2a 70 61 52 69 67 68 74 20 3d 20 61 4c 65 66 74 *paRight = aLeft
19dbc 3b 0a 20 20 2a 70 6e 52 69 67 68 74 20 3d 20 69 ;. *pnRight = i
19dbd 4f 75 74 3b 0a 20 20 6d 65 6d 63 70 79 28 61 4c Out;. memcpy(aL
19dbe 65 66 74 2c 20 61 54 6d 70 2c 20 73 69 7a 65 6f eft, aTmp, sizeo
19dbf 66 28 61 54 6d 70 5b 30 5d 29 2a 69 4f 75 74 29 f(aTmp[0])*iOut)
19dc0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 20 ;.}../*.** Sort
19dc1 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 the elements in
19dc2 6c 69 73 74 20 61 4c 69 73 74 20 75 73 69 6e 67 list aList using
19dc3 20 61 43 6f 6e 74 65 6e 74 5b 5d 20 61 73 20 74 aContent[] as t
19dc4 68 65 20 73 6f 72 74 20 6b 65 79 2e 0a 2a 2a 20 he sort key..**
19dc5 52 65 6d 6f 76 65 20 65 6c 65 6d 65 6e 74 73 20 Remove elements
19dc6 77 69 74 68 20 64 75 70 6c 69 63 61 74 65 20 6b with duplicate k
19dc7 65 79 73 2c 20 70 72 65 66 65 72 72 69 6e 67 20 eys, preferring
19dc8 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 6c to keep the.** l
19dc9 61 72 67 65 72 20 61 4c 69 73 74 5b 5d 20 76 61 arger aList[] va
19dca 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 lues..**.** The
19dcb 61 4c 69 73 74 5b 5d 20 65 6e 74 72 69 65 73 20 aList[] entries
19dcc 61 72 65 20 69 6e 64 69 63 65 73 20 69 6e 74 6f are indices into
19dcd 20 61 43 6f 6e 74 65 6e 74 5b 5d 2e 20 20 54 68 aContent[]. Th
19dce 65 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 61 e values in.** a
19dcf 4c 69 73 74 5b 5d 20 61 72 65 20 74 6f 20 62 65 List[] are to be
19dd0 20 73 6f 72 74 65 64 20 73 6f 20 74 68 61 74 20 sorted so that
19dd1 66 6f 72 20 61 6c 6c 20 4a 3c 4b 3a 0a 2a 2a 0a for all J<K:.**.
19dd2 2a 2a 20 20 20 20 20 20 61 43 6f 6e 74 65 6e 74 ** aContent
19dd3 5b 61 4c 69 73 74 5b 4a 5d 5d 20 3c 20 61 43 6f [aList[J]] < aCo
19dd4 6e 74 65 6e 74 5b 61 4c 69 73 74 5b 4b 5d 5d 0a ntent[aList[K]].
19dd5 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e 79 20 58 20 **.** For any X
19dd6 61 6e 64 20 59 20 73 75 63 68 20 74 68 61 74 0a and Y such that.
19dd7 2a 2a 0a 2a 2a 20 20 20 20 20 20 61 43 6f 6e 74 **.** aCont
19dd8 65 6e 74 5b 61 4c 69 73 74 5b 58 5d 5d 20 3d 3d ent[aList[X]] ==
19dd9 20 61 43 6f 6e 74 65 6e 74 5b 61 4c 69 73 74 5b aContent[aList[
19dda 59 5d 5d 0a 2a 2a 0a 2a 2a 20 4b 65 65 70 20 74 Y]].**.** Keep t
19ddb 68 65 20 6c 61 72 67 65 72 20 6f 66 20 74 68 65 he larger of the
19ddc 20 74 77 6f 20 76 61 6c 75 65 73 20 61 4c 69 73 two values aLis
19ddd 74 5b 58 5d 20 61 6e 64 20 61 4c 69 73 74 5b 59 t[X] and aList[Y
19dde 5d 20 61 6e 64 20 64 69 73 63 61 72 64 0a 2a 2a ] and discard.**
19ddf 20 74 68 65 20 73 6d 61 6c 6c 65 72 2e 0a 2a 2f the smaller..*/
19de0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 61 6c .static void wal
19de1 4d 65 72 67 65 73 6f 72 74 28 0a 20 20 63 6f 6e Mergesort(. con
19de2 73 74 20 75 33 32 20 2a 61 43 6f 6e 74 65 6e 74 st u32 *aContent
19de3 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
19de4 50 61 67 65 73 20 69 6e 20 77 61 6c 20 2a 2f 0a Pages in wal */.
19de5 20 20 68 74 5f 73 6c 6f 74 20 2a 61 42 75 66 66 ht_slot *aBuff
19de6 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 er,
19de7 20 20 2f 2a 20 42 75 66 66 65 72 20 6f 66 20 61 /* Buffer of a
19de8 74 20 6c 65 61 73 74 20 2a 70 6e 4c 69 73 74 20 t least *pnList
19de9 69 74 65 6d 73 20 74 6f 20 75 73 65 20 2a 2f 0a items to use */.
19dea 20 20 68 74 5f 73 6c 6f 74 20 2a 61 4c 69 73 74 ht_slot *aList
19deb 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
19dec 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 4c 69 73 /* IN/OUT: Lis
19ded 74 20 74 6f 20 73 6f 72 74 20 2a 2f 0a 20 20 69 t to sort */. i
19dee 6e 74 20 2a 70 6e 4c 69 73 74 20 20 20 20 20 20 nt *pnList
19def 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19df0 2a 20 49 4e 2f 4f 55 54 3a 20 4e 75 6d 62 65 72 * IN/OUT: Number
19df1 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 of elements in
19df2 61 4c 69 73 74 5b 5d 20 2a 2f 0a 29 7b 0a 20 20 aList[] */.){.
19df3 73 74 72 75 63 74 20 53 75 62 6c 69 73 74 20 7b struct Sublist {
19df4 0a 20 20 20 20 69 6e 74 20 6e 4c 69 73 74 3b 20 . int nList;
19df5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19df6 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
19df7 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 4c 69 73 elements in aLis
19df8 74 20 2a 2f 0a 20 20 20 20 68 74 5f 73 6c 6f 74 t */. ht_slot
19df9 20 2a 61 4c 69 73 74 3b 20 20 20 20 20 20 20 20 *aList;
19dfa 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
19dfb 72 20 74 6f 20 73 75 62 2d 6c 69 73 74 20 63 6f r to sub-list co
19dfc 6e 74 65 6e 74 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 ntent */. };..
19dfd 20 63 6f 6e 73 74 20 69 6e 74 20 6e 4c 69 73 74 const int nList
19dfe 20 3d 20 2a 70 6e 4c 69 73 74 3b 20 20 20 20 20 = *pnList;
19dff 20 2f 2a 20 53 69 7a 65 20 6f 66 20 69 6e 70 75 /* Size of inpu
19e00 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 t list */. int
19e01 6e 4d 65 72 67 65 20 3d 20 30 3b 20 20 20 20 20 nMerge = 0;
19e02 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
19e03 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 umber of element
19e04 73 20 69 6e 20 6c 69 73 74 20 61 4d 65 72 67 65 s in list aMerge
19e05 20 2a 2f 0a 20 20 68 74 5f 73 6c 6f 74 20 2a 61 */. ht_slot *a
19e06 4d 65 72 67 65 20 3d 20 30 3b 20 20 20 20 20 20 Merge = 0;
19e07 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f /* List to
19e08 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 20 20 be merged */.
19e09 69 6e 74 20 69 4c 69 73 74 3b 20 20 20 20 20 20 int iList;
19e0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e0b 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 69 6e /* Index into in
19e0c 70 75 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e put list */. in
19e0d 74 20 69 53 75 62 20 3d 20 30 3b 20 20 20 20 20 t iSub = 0;
19e0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19e0f 20 49 6e 64 65 78 20 69 6e 74 6f 20 61 53 75 62 Index into aSub
19e10 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 74 72 75 array */. stru
19e11 63 74 20 53 75 62 6c 69 73 74 20 61 53 75 62 5b ct Sublist aSub[
19e12 31 33 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 13]; /* A
19e13 72 72 61 79 20 6f 66 20 73 75 62 2d 6c 69 73 74 rray of sub-list
19e14 73 20 2a 2f 0a 0a 20 20 6d 65 6d 73 65 74 28 61 s */.. memset(a
19e15 53 75 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 Sub, 0, sizeof(a
19e16 53 75 62 29 29 3b 0a 20 20 61 73 73 65 72 74 28 Sub));. assert(
19e17 20 6e 4c 69 73 74 3c 3d 48 41 53 48 54 41 42 4c nList<=HASHTABL
19e18 45 5f 4e 50 41 47 45 20 26 26 20 6e 4c 69 73 74 E_NPAGE && nList
19e19 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
19e1a 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 3d HASHTABLE_NPAGE=
19e1b 3d 28 31 3c 3c 28 41 72 72 61 79 53 69 7a 65 28 =(1<<(ArraySize(
19e1c 61 53 75 62 29 2d 31 29 29 20 29 3b 0a 0a 20 20 aSub)-1)) );..
19e1d 66 6f 72 28 69 4c 69 73 74 3d 30 3b 20 69 4c 69 for(iList=0; iLi
19e1e 73 74 3c 6e 4c 69 73 74 3b 20 69 4c 69 73 74 2b st<nList; iList+
19e1f 2b 29 7b 0a 20 20 20 20 6e 4d 65 72 67 65 20 3d +){. nMerge =
19e20 20 31 3b 0a 20 20 20 20 61 4d 65 72 67 65 20 3d 1;. aMerge =
19e21 20 26 61 4c 69 73 74 5b 69 4c 69 73 74 5d 3b 0a &aList[iList];.
19e22 20 20 20 20 66 6f 72 28 69 53 75 62 3d 30 3b 20 for(iSub=0;
19e23 69 4c 69 73 74 20 26 20 28 31 3c 3c 69 53 75 62 iList & (1<<iSub
19e24 29 3b 20 69 53 75 62 2b 2b 29 7b 0a 20 20 20 20 ); iSub++){.
19e25 20 20 73 74 72 75 63 74 20 53 75 62 6c 69 73 74 struct Sublist
19e26 20 2a 70 20 3d 20 26 61 53 75 62 5b 69 53 75 62 *p = &aSub[iSub
19e27 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ];. assert(
19e28 20 70 2d 3e 61 4c 69 73 74 20 26 26 20 70 2d 3e p->aList && p->
19e29 6e 4c 69 73 74 3c 3d 28 31 3c 3c 69 53 75 62 29 nList<=(1<<iSub)
19e2a 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
19e2b 28 20 70 2d 3e 61 4c 69 73 74 3d 3d 26 61 4c 69 ( p->aList==&aLi
19e2c 73 74 5b 69 4c 69 73 74 26 7e 28 28 32 3c 3c 69 st[iList&~((2<<i
19e2d 53 75 62 29 2d 31 29 5d 20 29 3b 0a 20 20 20 20 Sub)-1)] );.
19e2e 20 20 77 61 6c 4d 65 72 67 65 28 61 43 6f 6e 74 walMerge(aCont
19e2f 65 6e 74 2c 20 70 2d 3e 61 4c 69 73 74 2c 20 70 ent, p->aList, p
19e30 2d 3e 6e 4c 69 73 74 2c 20 26 61 4d 65 72 67 65 ->nList, &aMerge
19e31 2c 20 26 6e 4d 65 72 67 65 2c 20 61 42 75 66 66 , &nMerge, aBuff
19e32 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 er);. }. a
19e33 53 75 62 5b 69 53 75 62 5d 2e 61 4c 69 73 74 20 Sub[iSub].aList
19e34 3d 20 61 4d 65 72 67 65 3b 0a 20 20 20 20 61 53 = aMerge;. aS
19e35 75 62 5b 69 53 75 62 5d 2e 6e 4c 69 73 74 20 3d ub[iSub].nList =
19e36 20 6e 4d 65 72 67 65 3b 0a 20 20 7d 0a 0a 20 20 nMerge;. }..
19e37 66 6f 72 28 69 53 75 62 2b 2b 3b 20 69 53 75 62 for(iSub++; iSub
19e38 3c 41 72 72 61 79 53 69 7a 65 28 61 53 75 62 29 <ArraySize(aSub)
19e39 3b 20 69 53 75 62 2b 2b 29 7b 0a 20 20 20 20 69 ; iSub++){. i
19e3a 66 28 20 6e 4c 69 73 74 20 26 20 28 31 3c 3c 69 f( nList & (1<<i
19e3b 53 75 62 29 20 29 7b 0a 20 20 20 20 20 20 73 74 Sub) ){. st
19e3c 72 75 63 74 20 53 75 62 6c 69 73 74 20 2a 70 20 ruct Sublist *p
19e3d 3d 20 26 61 53 75 62 5b 69 53 75 62 5d 3b 0a 20 = &aSub[iSub];.
19e3e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
19e3f 6e 4c 69 73 74 3c 3d 28 31 3c 3c 69 53 75 62 29 nList<=(1<<iSub)
19e40 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
19e41 28 20 70 2d 3e 61 4c 69 73 74 3d 3d 26 61 4c 69 ( p->aList==&aLi
19e42 73 74 5b 6e 4c 69 73 74 26 7e 28 28 32 3c 3c 69 st[nList&~((2<<i
19e43 53 75 62 29 2d 31 29 5d 20 29 3b 0a 20 20 20 20 Sub)-1)] );.
19e44 20 20 77 61 6c 4d 65 72 67 65 28 61 43 6f 6e 74 walMerge(aCont
19e45 65 6e 74 2c 20 70 2d 3e 61 4c 69 73 74 2c 20 70 ent, p->aList, p
19e46 2d 3e 6e 4c 69 73 74 2c 20 26 61 4d 65 72 67 65 ->nList, &aMerge
19e47 2c 20 26 6e 4d 65 72 67 65 2c 20 61 42 75 66 66 , &nMerge, aBuff
19e48 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 er);. }. }.
19e49 20 61 73 73 65 72 74 28 20 61 4d 65 72 67 65 3d assert( aMerge=
19e4a 3d 61 4c 69 73 74 20 29 3b 0a 20 20 2a 70 6e 4c =aList );. *pnL
19e4b 69 73 74 20 3d 20 6e 4d 65 72 67 65 3b 0a 0a 23 ist = nMerge;..#
19e4c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
19e4d 55 47 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 UG. {. int i
19e4e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 ;. for(i=1; i
19e4f 3c 2a 70 6e 4c 69 73 74 3b 20 69 2b 2b 29 7b 0a <*pnList; i++){.
19e50 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61 43 assert( aC
19e51 6f 6e 74 65 6e 74 5b 61 4c 69 73 74 5b 69 5d 5d ontent[aList[i]]
19e52 20 3e 20 61 43 6f 6e 74 65 6e 74 5b 61 4c 69 73 > aContent[aLis
19e53 74 5b 69 2d 31 5d 5d 20 29 3b 0a 20 20 20 20 7d t[i-1]] );. }
19e54 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f . }.#endif.}../
19e55 2a 20 0a 2a 2a 20 46 72 65 65 20 61 6e 20 69 74 * .** Free an it
19e56 65 72 61 74 6f 72 20 61 6c 6c 6f 63 61 74 65 64 erator allocated
19e57 20 62 79 20 77 61 6c 49 74 65 72 61 74 6f 72 49 by walIteratorI
19e58 6e 69 74 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 nit()..*/.static
19e59 20 76 6f 69 64 20 77 61 6c 49 74 65 72 61 74 6f void walIterato
19e5a 72 46 72 65 65 28 57 61 6c 49 74 65 72 61 74 6f rFree(WalIterato
19e5b 72 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 r *p){. sqlite3
19e5c 53 63 72 61 74 63 68 46 72 65 65 28 70 29 3b 0a ScratchFree(p);.
19e5d 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 }../*.** Constru
19e5e 63 74 20 61 20 57 61 6c 49 6e 74 65 72 61 74 6f ct a WalInterato
19e5f 72 20 6f 62 6a 65 63 74 20 74 68 61 74 20 63 61 r object that ca
19e60 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6c 6f 6f n be used to loo
19e61 70 20 6f 76 65 72 20 61 6c 6c 20 0a 2a 2a 20 70 p over all .** p
19e62 61 67 65 73 20 69 6e 20 74 68 65 20 57 41 4c 20 ages in the WAL
19e63 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 in ascending ord
19e64 65 72 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d er. The caller m
19e65 75 73 74 20 68 6f 6c 64 20 74 68 65 20 63 68 65 ust hold the che
19e66 63 6b 70 6f 69 6e 74 0a 2a 2a 20 6c 6f 63 6b 2e ckpoint.** lock.
19e67 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 .**.** On succes
19e68 73 2c 20 6d 61 6b 65 20 2a 70 70 20 70 6f 69 6e s, make *pp poin
19e69 74 20 74 6f 20 74 68 65 20 6e 65 77 6c 79 20 61 t to the newly a
19e6a 6c 6c 6f 63 61 74 65 64 20 57 61 6c 49 6e 74 65 llocated WalInte
19e6b 72 61 74 6f 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 rator object.**
19e6c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
19e6d 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 . Otherwise, ret
19e6e 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 urn an error cod
19e6f 65 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 e. If this routi
19e70 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 6e ne.** returns an
19e71 20 65 72 72 6f 72 2c 20 74 68 65 20 76 61 6c 75 error, the valu
19e72 65 20 6f 66 20 2a 70 70 20 69 73 20 75 6e 64 65 e of *pp is unde
19e73 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 fined..**.** The
19e74 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 calling routine
19e75 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 20 77 should invoke w
19e76 61 6c 49 74 65 72 61 74 6f 72 46 72 65 65 28 29 alIteratorFree()
19e77 20 74 6f 20 64 65 73 74 72 6f 79 20 74 68 65 0a to destroy the.
19e78 2a 2a 20 57 61 6c 49 74 65 72 61 74 6f 72 20 6f ** WalIterator o
19e79 62 6a 65 63 74 20 77 68 65 6e 20 69 74 20 68 61 bject when it ha
19e7a 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 s finished with
19e7b 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e it..*/.static in
19e7c 74 20 77 61 6c 49 74 65 72 61 74 6f 72 49 6e 69 t walIteratorIni
19e7d 74 28 57 61 6c 20 2a 70 57 61 6c 2c 20 57 61 6c t(Wal *pWal, Wal
19e7e 49 74 65 72 61 74 6f 72 20 2a 2a 70 70 29 7b 0a Iterator **pp){.
19e7f 20 20 57 61 6c 49 74 65 72 61 74 6f 72 20 2a 70 WalIterator *p
19e80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
19e81 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 /* Return valu
19e82 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65 67 6d e */. int nSegm
19e83 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ent;
19e84 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
19e85 20 6f 66 20 73 65 67 6d 65 6e 74 73 20 74 6f 20 of segments to
19e86 6d 65 72 67 65 20 2a 2f 0a 20 20 75 33 32 20 69 merge */. u32 i
19e87 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 Last;
19e88 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 /* La
19e89 73 74 20 66 72 61 6d 65 20 69 6e 20 6c 6f 67 20 st frame in log
19e8a 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 */. int nByte;
19e8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e8c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
19e8d 66 20 62 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 f bytes to alloc
19e8e 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 ate */. int i;
19e8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e90 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 /* Iter
19e91 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f ator variable */
19e92 0a 20 20 68 74 5f 73 6c 6f 74 20 2a 61 54 6d 70 . ht_slot *aTmp
19e93 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
19e94 20 20 20 2f 2a 20 54 65 6d 70 20 73 70 61 63 65 /* Temp space
19e95 20 75 73 65 64 20 62 79 20 6d 65 72 67 65 2d 73 used by merge-s
19e96 6f 72 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 ort */. int rc
19e97 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
19e98 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
19e99 72 6e 20 43 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a rn Code */.. /*
19e9a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e This routine on
19e9b 6c 79 20 72 75 6e 73 20 77 68 69 6c 65 20 68 6f ly runs while ho
19e9c 6c 64 69 6e 67 20 74 68 65 20 63 68 65 63 6b 70 lding the checkp
19e9d 6f 69 6e 74 20 6c 6f 63 6b 2e 20 41 6e 64 0a 20 oint lock. And.
19e9e 20 2a 2a 20 69 74 20 6f 6e 6c 79 20 72 75 6e 73 ** it only runs
19e9f 20 69 66 20 74 68 65 72 65 20 69 73 20 61 63 74 if there is act
19ea0 75 61 6c 6c 79 20 63 6f 6e 74 65 6e 74 20 69 6e ually content in
19ea1 20 74 68 65 20 6c 6f 67 20 28 6d 78 46 72 61 6d the log (mxFram
19ea2 65 3e 30 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 e>0).. */. ass
19ea3 65 72 74 28 20 70 57 61 6c 2d 3e 63 6b 70 74 4c ert( pWal->ckptL
19ea4 6f 63 6b 20 26 26 20 70 57 61 6c 2d 3e 68 64 72 ock && pWal->hdr
19ea5 2e 6d 78 46 72 61 6d 65 3e 30 20 29 3b 0a 20 20 .mxFrame>0 );.
19ea6 69 4c 61 73 74 20 3d 20 70 57 61 6c 2d 3e 68 64 iLast = pWal->hd
19ea7 72 2e 6d 78 46 72 61 6d 65 3b 0a 0a 20 20 2f 2a r.mxFrame;.. /*
19ea8 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 Allocate space
19ea9 66 6f 72 20 74 68 65 20 57 61 6c 49 74 65 72 61 for the WalItera
19eaa 74 6f 72 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 tor object. */.
19eab 20 6e 53 65 67 6d 65 6e 74 20 3d 20 77 61 6c 46 nSegment = walF
19eac 72 61 6d 65 50 61 67 65 28 69 4c 61 73 74 29 20 ramePage(iLast)
19ead 2b 20 31 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 + 1;. nByte = s
19eae 69 7a 65 6f 66 28 57 61 6c 49 74 65 72 61 74 6f izeof(WalIterato
19eaf 72 29 20 0a 20 20 20 20 20 20 20 20 2b 20 28 6e r) . + (n
19eb0 53 65 67 6d 65 6e 74 2d 31 29 2a 73 69 7a 65 6f Segment-1)*sizeo
19eb1 66 28 73 74 72 75 63 74 20 57 61 6c 53 65 67 6d f(struct WalSegm
19eb2 65 6e 74 29 0a 20 20 20 20 20 20 20 20 2b 20 69 ent). + i
19eb3 4c 61 73 74 2a 73 69 7a 65 6f 66 28 68 74 5f 73 Last*sizeof(ht_s
19eb4 6c 6f 74 29 3b 0a 20 20 70 20 3d 20 28 57 61 6c lot);. p = (Wal
19eb5 49 74 65 72 61 74 6f 72 20 2a 29 73 71 6c 69 74 Iterator *)sqlit
19eb6 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 e3ScratchMalloc(
19eb7 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 21 70 nByte);. if( !p
19eb8 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
19eb9 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
19eba 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 . memset(p, 0,
19ebb 6e 42 79 74 65 29 3b 0a 20 20 70 2d 3e 6e 53 65 nByte);. p->nSe
19ebc 67 6d 65 6e 74 20 3d 20 6e 53 65 67 6d 65 6e 74 gment = nSegment
19ebd 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 ;.. /* Allocate
19ebe 20 74 65 6d 70 6f 72 61 72 79 20 73 70 61 63 65 temporary space
19ebf 20 75 73 65 64 20 62 79 20 74 68 65 20 6d 65 72 used by the mer
19ec0 67 65 2d 73 6f 72 74 20 72 6f 75 74 69 6e 65 2e ge-sort routine.
19ec1 20 54 68 69 73 20 62 6c 6f 63 6b 0a 20 20 2a 2a This block. **
19ec2 20 6f 66 20 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 of memory will
19ec3 62 65 20 66 72 65 65 64 20 62 65 66 6f 72 65 20 be freed before
19ec4 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 this function re
19ec5 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61 54 turns.. */. aT
19ec6 6d 70 20 3d 20 28 68 74 5f 73 6c 6f 74 20 2a 29 mp = (ht_slot *)
19ec7 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 sqlite3ScratchMa
19ec8 6c 6c 6f 63 28 0a 20 20 20 20 20 20 73 69 7a 65 lloc(. size
19ec9 6f 66 28 68 74 5f 73 6c 6f 74 29 20 2a 20 28 69 of(ht_slot) * (i
19eca 4c 61 73 74 3e 48 41 53 48 54 41 42 4c 45 5f 4e Last>HASHTABLE_N
19ecb 50 41 47 45 3f 48 41 53 48 54 41 42 4c 45 5f 4e PAGE?HASHTABLE_N
19ecc 50 41 47 45 3a 69 4c 61 73 74 29 0a 20 20 29 3b PAGE:iLast). );
19ecd 0a 20 20 69 66 28 20 21 61 54 6d 70 20 29 7b 0a . if( !aTmp ){.
19ece 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
19ecf 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 66 6f NOMEM;. }.. fo
19ed0 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 r(i=0; rc==SQLIT
19ed1 45 5f 4f 4b 20 26 26 20 69 3c 6e 53 65 67 6d 65 E_OK && i<nSegme
19ed2 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 6f nt; i++){. vo
19ed3 6c 61 74 69 6c 65 20 68 74 5f 73 6c 6f 74 20 2a latile ht_slot *
19ed4 61 48 61 73 68 3b 0a 20 20 20 20 75 33 32 20 69 aHash;. u32 i
19ed5 5a 65 72 6f 3b 0a 20 20 20 20 76 6f 6c 61 74 69 Zero;. volati
19ed6 6c 65 20 75 33 32 20 2a 61 50 67 6e 6f 3b 0a 0a le u32 *aPgno;..
19ed7 20 20 20 20 72 63 20 3d 20 77 61 6c 48 61 73 68 rc = walHash
19ed8 47 65 74 28 70 57 61 6c 2c 20 69 2c 20 26 61 48 Get(pWal, i, &aH
19ed9 61 73 68 2c 20 26 61 50 67 6e 6f 2c 20 26 69 5a ash, &aPgno, &iZ
19eda 65 72 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ero);. if( rc
19edb 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
19edc 20 20 20 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 int j;
19edd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ede 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 69 /* Counter vari
19edf 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e able */. in
19ee0 74 20 6e 45 6e 74 72 79 3b 20 20 20 20 20 20 20 t nEntry;
19ee1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
19ee2 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
19ee3 6e 20 74 68 69 73 20 73 65 67 6d 65 6e 74 20 2a n this segment *
19ee4 2f 0a 20 20 20 20 20 20 68 74 5f 73 6c 6f 74 20 /. ht_slot
19ee5 2a 61 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 *aIndex;
19ee6 20 20 20 20 2f 2a 20 53 6f 72 74 65 64 20 69 6e /* Sorted in
19ee7 64 65 78 20 66 6f 72 20 74 68 69 73 20 73 65 67 dex for this seg
19ee8 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 20 20 61 ment */.. a
19ee9 50 67 6e 6f 2b 2b 3b 0a 20 20 20 20 20 20 69 66 Pgno++;. if
19eea 28 20 28 69 2b 31 29 3d 3d 6e 53 65 67 6d 65 6e ( (i+1)==nSegmen
19eeb 74 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 45 6e t ){. nEn
19eec 74 72 79 20 3d 20 28 69 6e 74 29 28 69 4c 61 73 try = (int)(iLas
19eed 74 20 2d 20 69 5a 65 72 6f 29 3b 0a 20 20 20 20 t - iZero);.
19eee 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
19eef 20 6e 45 6e 74 72 79 20 3d 20 28 69 6e 74 29 28 nEntry = (int)(
19ef0 28 75 33 32 2a 29 61 48 61 73 68 20 2d 20 28 75 (u32*)aHash - (u
19ef1 33 32 2a 29 61 50 67 6e 6f 29 3b 0a 20 20 20 20 32*)aPgno);.
19ef2 20 20 7d 0a 20 20 20 20 20 20 61 49 6e 64 65 78 }. aIndex
19ef3 20 3d 20 26 28 28 68 74 5f 73 6c 6f 74 20 2a 29 = &((ht_slot *)
19ef4 26 70 2d 3e 61 53 65 67 6d 65 6e 74 5b 70 2d 3e &p->aSegment[p->
19ef5 6e 53 65 67 6d 65 6e 74 5d 29 5b 69 5a 65 72 6f nSegment])[iZero
19ef6 5d 3b 0a 20 20 20 20 20 20 69 5a 65 72 6f 2b 2b ];. iZero++
19ef7 3b 0a 20 20 0a 20 20 20 20 20 20 66 6f 72 28 6a ;. . for(j
19ef8 3d 30 3b 20 6a 3c 6e 45 6e 74 72 79 3b 20 6a 2b =0; j<nEntry; j+
19ef9 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 49 6e 64 +){. aInd
19efa 65 78 5b 6a 5d 20 3d 20 28 68 74 5f 73 6c 6f 74 ex[j] = (ht_slot
19efb 29 6a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 )j;. }.
19efc 20 20 77 61 6c 4d 65 72 67 65 73 6f 72 74 28 28 walMergesort((
19efd 75 33 32 20 2a 29 61 50 67 6e 6f 2c 20 61 54 6d u32 *)aPgno, aTm
19efe 70 2c 20 61 49 6e 64 65 78 2c 20 26 6e 45 6e 74 p, aIndex, &nEnt
19eff 72 79 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 53 ry);. p->aS
19f00 65 67 6d 65 6e 74 5b 69 5d 2e 69 5a 65 72 6f 20 egment[i].iZero
19f01 3d 20 69 5a 65 72 6f 3b 0a 20 20 20 20 20 20 70 = iZero;. p
19f02 2d 3e 61 53 65 67 6d 65 6e 74 5b 69 5d 2e 6e 45 ->aSegment[i].nE
19f03 6e 74 72 79 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 ntry = nEntry;.
19f04 20 20 20 20 20 70 2d 3e 61 53 65 67 6d 65 6e 74 p->aSegment
19f05 5b 69 5d 2e 61 49 6e 64 65 78 20 3d 20 61 49 6e [i].aIndex = aIn
19f06 64 65 78 3b 0a 20 20 20 20 20 20 70 2d 3e 61 53 dex;. p->aS
19f07 65 67 6d 65 6e 74 5b 69 5d 2e 61 50 67 6e 6f 20 egment[i].aPgno
19f08 3d 20 28 75 33 32 20 2a 29 61 50 67 6e 6f 3b 0a = (u32 *)aPgno;.
19f09 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 }. }. sqli
19f0a 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28 61 te3ScratchFree(a
19f0b 54 6d 70 29 3b 0a 0a 20 20 69 66 28 20 72 63 21 Tmp);.. if( rc!
19f0c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
19f0d 20 20 77 61 6c 49 74 65 72 61 74 6f 72 46 72 65 walIteratorFre
19f0e 65 28 70 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 e(p);. }. *pp
19f0f 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 = p;. return rc
19f10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d ;.}../*.** Attem
19f11 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 pt to obtain the
19f12 20 65 78 63 6c 75 73 69 76 65 20 57 41 4c 20 6c exclusive WAL l
19f13 6f 63 6b 20 64 65 66 69 6e 65 64 20 62 79 20 70 ock defined by p
19f14 61 72 61 6d 65 74 65 72 73 20 6c 6f 63 6b 49 64 arameters lockId
19f15 78 20 61 6e 64 0a 2a 2a 20 6e 2e 20 49 66 20 74 x and.** n. If t
19f16 68 65 20 61 74 74 65 6d 70 74 20 66 61 69 6c 73 he attempt fails
19f17 20 61 6e 64 20 70 61 72 61 6d 65 74 65 72 20 78 and parameter x
19f18 42 75 73 79 20 69 73 20 6e 6f 74 20 4e 55 4c 4c Busy is not NULL
19f19 2c 20 74 68 65 6e 20 69 74 20 69 73 20 61 0a 2a , then it is a.*
19f1a 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 66 * busy-handler f
19f1b 75 6e 63 74 69 6f 6e 2e 20 49 6e 76 6f 6b 65 20 unction. Invoke
19f1c 69 74 20 61 6e 64 20 72 65 74 72 79 20 74 68 65 it and retry the
19f1d 20 6c 6f 63 6b 20 75 6e 74 69 6c 20 65 69 74 68 lock until eith
19f1e 65 72 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 er the.** lock i
19f1f 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f s successfully o
19f20 62 74 61 69 6e 65 64 20 6f 72 20 74 68 65 20 62 btained or the b
19f21 75 73 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 usy-handler retu
19f22 72 6e 73 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 rns 0..*/.static
19f23 20 69 6e 74 20 77 61 6c 42 75 73 79 4c 6f 63 6b int walBusyLock
19f24 28 0a 20 20 57 61 6c 20 2a 70 57 61 6c 2c 20 20 (. Wal *pWal,
19f25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19f26 20 20 20 20 2f 2a 20 57 41 4c 20 63 6f 6e 6e 65 /* WAL conne
19f27 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 ction */. int (
19f28 2a 78 42 75 73 79 29 28 76 6f 69 64 2a 29 2c 20 *xBusy)(void*),
19f29 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75 /* Fu
19f2a 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 nction to call w
19f2b 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f hen busy */. vo
19f2c 69 64 20 2a 70 42 75 73 79 41 72 67 2c 20 20 20 id *pBusyArg,
19f2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19f2e 20 43 6f 6e 74 65 78 74 20 61 72 67 75 6d 65 6e Context argumen
19f2f 74 20 66 6f 72 20 78 42 75 73 79 48 61 6e 64 6c t for xBusyHandl
19f30 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b er */. int lock
19f31 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 Idx,
19f32 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
19f33 74 20 6f 66 20 66 69 72 73 74 20 62 79 74 65 20 t of first byte
19f34 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 to lock */. int
19f35 20 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
19f36 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19f37 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
19f38 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20 to lock */.){.
19f39 69 6e 74 20 72 63 3b 0a 20 20 64 6f 20 7b 0a 20 int rc;. do {.
19f3a 20 20 20 72 63 20 3d 20 77 61 6c 4c 6f 63 6b 45 rc = walLockE
19f3b 78 63 6c 75 73 69 76 65 28 70 57 61 6c 2c 20 6c xclusive(pWal, l
19f3c 6f 63 6b 49 64 78 2c 20 6e 29 3b 0a 20 20 7d 77 ockIdx, n);. }w
19f3d 68 69 6c 65 28 20 78 42 75 73 79 20 26 26 20 72 hile( xBusy && r
19f3e 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 c==SQLITE_BUSY &
19f3f 26 20 78 42 75 73 79 28 70 42 75 73 79 41 72 67 & xBusy(pBusyArg
19f40 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ) );. return rc
19f41 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 ;.}../*.** The c
19f42 61 63 68 65 20 6f 66 20 74 68 65 20 77 61 6c 2d ache of the wal-
19f43 69 6e 64 65 78 20 68 65 61 64 65 72 20 6d 75 73 index header mus
19f44 74 20 62 65 20 76 61 6c 69 64 20 74 6f 20 63 61 t be valid to ca
19f45 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ll this function
19f46 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 ..** Return the
19f47 70 61 67 65 2d 73 69 7a 65 20 69 6e 20 62 79 74 page-size in byt
19f48 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 64 es used by the d
19f49 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 atabase..*/.stat
19f4a 69 63 20 69 6e 74 20 77 61 6c 50 61 67 65 73 69 ic int walPagesi
19f4b 7a 65 28 57 61 6c 20 2a 70 57 61 6c 29 7b 0a 20 ze(Wal *pWal){.
19f4c 20 72 65 74 75 72 6e 20 28 70 57 61 6c 2d 3e 68 return (pWal->h
19f4d 64 72 2e 73 7a 50 61 67 65 26 30 78 66 65 30 30 dr.szPage&0xfe00
19f4e 29 20 2b 20 28 28 70 57 61 6c 2d 3e 68 64 72 2e ) + ((pWal->hdr.
19f4f 73 7a 50 61 67 65 26 30 78 30 30 30 31 29 3c 3c szPage&0x0001)<<
19f50 31 36 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 16);.}../*.** Co
19f51 70 79 20 61 73 20 6d 75 63 68 20 63 6f 6e 74 65 py as much conte
19f52 6e 74 20 61 73 20 77 65 20 63 61 6e 20 66 72 6f nt as we can fro
19f53 6d 20 74 68 65 20 57 41 4c 20 62 61 63 6b 20 69 m the WAL back i
19f54 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 nto the database
19f55 20 66 69 6c 65 0a 2a 2a 20 69 6e 20 72 65 73 70 file.** in resp
19f56 6f 6e 73 65 20 74 6f 20 61 6e 20 73 71 6c 69 74 onse to an sqlit
19f57 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e e3_wal_checkpoin
19f58 74 28 29 20 72 65 71 75 65 73 74 20 6f 72 20 74 t() request or t
19f59 68 65 20 65 71 75 69 76 61 6c 65 6e 74 2e 0a 2a he equivalent..*
19f5a 2a 0a 2a 2a 20 54 68 65 20 61 6d 6f 75 6e 74 20 *.** The amount
19f5b 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 63 of information c
19f5c 6f 70 69 65 73 20 66 72 6f 6d 20 57 41 4c 20 74 opies from WAL t
19f5d 6f 20 64 61 74 61 62 61 73 65 20 6d 69 67 68 74 o database might
19f5e 20 62 65 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 62 be limited.** b
19f5f 79 20 61 63 74 69 76 65 20 72 65 61 64 65 72 73 y active readers
19f60 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
19f61 77 69 6c 6c 20 6e 65 76 65 72 20 6f 76 65 72 77 will never overw
19f62 72 69 74 65 20 61 20 64 61 74 61 62 61 73 65 20 rite a database
19f63 70 61 67 65 0a 2a 2a 20 74 68 61 74 20 61 20 63 page.** that a c
19f64 6f 6e 63 75 72 72 65 6e 74 20 72 65 61 64 65 72 oncurrent reader
19f65 20 6d 69 67 68 74 20 62 65 20 75 73 69 6e 67 2e might be using.
19f66 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 49 2f 4f 20 62 .**.** All I/O b
19f67 61 72 72 69 65 72 20 6f 70 65 72 61 74 69 6f 6e arrier operation
19f68 73 20 28 61 2e 6b 2e 61 20 66 73 79 6e 63 73 29 s (a.k.a fsyncs)
19f69 20 6f 63 63 75 72 20 69 6e 20 74 68 69 73 20 72 occur in this r
19f6a 6f 75 74 69 6e 65 20 77 68 65 6e 0a 2a 2a 20 53 outine when.** S
19f6b 51 4c 69 74 65 20 69 73 20 69 6e 20 57 41 4c 2d QLite is in WAL-
19f6c 6d 6f 64 65 20 69 6e 20 73 79 6e 63 68 72 6f 6e mode in synchron
19f6d 6f 75 73 3d 4e 4f 52 4d 41 4c 2e 20 20 54 68 61 ous=NORMAL. Tha
19f6e 74 20 6d 65 61 6e 73 20 74 68 61 74 20 69 66 20 t means that if
19f6f 0a 2a 2a 20 63 68 65 63 6b 70 6f 69 6e 74 73 20 .** checkpoints
19f70 61 72 65 20 61 6c 77 61 79 73 20 72 75 6e 20 62 are always run b
19f71 79 20 61 20 62 61 63 6b 67 72 6f 75 6e 64 20 74 y a background t
19f72 68 72 65 61 64 20 6f 72 20 62 61 63 6b 67 72 6f hread or backgro
19f73 75 6e 64 20 0a 2a 2a 20 70 72 6f 63 65 73 73 2c und .** process,
19f74 20 66 6f 72 65 67 72 6f 75 6e 64 20 74 68 72 65 foreground thre
19f75 61 64 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 ads will never b
19f76 6c 6f 63 6b 20 6f 6e 20 61 20 6c 65 6e 67 74 68 lock on a length
19f77 79 20 66 73 79 6e 63 20 63 61 6c 6c 2e 0a 2a 2a y fsync call..**
19f78 0a 2a 2a 20 46 73 79 6e 63 20 69 73 20 63 61 6c .** Fsync is cal
19f79 6c 65 64 20 6f 6e 20 74 68 65 20 57 41 4c 20 62 led on the WAL b
19f7a 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 63 6f efore writing co
19f7b 6e 74 65 6e 74 20 6f 75 74 20 6f 66 20 74 68 65 ntent out of the
19f7c 20 57 41 4c 20 61 6e 64 0a 2a 2a 20 69 6e 74 6f WAL and.** into
19f7d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
19f7e 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 This ensures tha
19f7f 74 20 69 66 20 74 68 65 20 6e 65 77 20 63 6f 6e t if the new con
19f80 74 65 6e 74 20 69 73 20 70 65 72 73 69 73 74 65 tent is persiste
19f81 6e 74 0a 2a 2a 20 69 6e 20 74 68 65 20 57 41 4c nt.** in the WAL
19f82 20 61 6e 64 20 63 61 6e 20 62 65 20 72 65 63 6f and can be reco
19f83 76 65 72 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 vered following
19f84 61 20 70 6f 77 65 72 2d 6c 6f 73 73 20 6f 72 20 a power-loss or
19f85 68 61 72 64 20 72 65 73 65 74 2e 0a 2a 2a 0a 2a hard reset..**.*
19f86 2a 20 46 73 79 6e 63 20 69 73 20 61 6c 73 6f 20 * Fsync is also
19f87 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 64 61 called on the da
19f88 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 28 tabase file if (
19f89 61 6e 64 20 6f 6e 6c 79 20 69 66 29 20 74 68 65 and only if) the
19f8a 20 65 6e 74 69 72 65 0a 2a 2a 20 57 41 4c 20 63 entire.** WAL c
19f8b 6f 6e 74 65 6e 74 20 69 73 20 63 6f 70 69 65 64 ontent is copied
19f8c 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 into the databa
19f8d 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 73 se file. This s
19f8e 65 63 6f 6e 64 20 66 73 79 6e 63 20 6d 61 6b 65 econd fsync make
19f8f 73 0a 2a 2a 20 69 74 20 73 61 66 65 20 74 6f 20 s.** it safe to
19f90 64 65 6c 65 74 65 20 74 68 65 20 57 41 4c 20 73 delete the WAL s
19f91 69 6e 63 65 20 74 68 65 20 6e 65 77 20 63 6f 6e ince the new con
19f92 74 65 6e 74 20 77 69 6c 6c 20 70 65 72 73 69 73 tent will persis
19f93 74 20 69 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 t in the.** data
19f94 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a base file..**.**
19f95 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 75 73 This routine us
19f96 65 73 20 61 6e 64 20 75 70 64 61 74 65 73 20 74 es and updates t
19f97 68 65 20 6e 42 61 63 6b 66 69 6c 6c 20 66 69 65 he nBackfill fie
19f98 6c 64 20 6f 66 20 74 68 65 20 77 61 6c 2d 69 6e ld of the wal-in
19f99 64 65 78 20 68 65 61 64 65 72 2e 0a 2a 2a 20 54 dex header..** T
19f9a 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 his is the only
19f9b 72 6f 75 74 69 6e 65 20 74 68 61 20 77 69 6c 6c routine tha will
19f9c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 76 61 increase the va
19f9d 6c 75 65 20 6f 66 20 6e 42 61 63 6b 66 69 6c 6c lue of nBackfill
19f9e 2e 20 20 0a 2a 2a 20 28 41 20 57 41 4c 20 72 65 . .** (A WAL re
19f9f 73 65 74 20 6f 72 20 72 65 63 6f 76 65 72 79 20 set or recovery
19fa0 77 69 6c 6c 20 72 65 76 65 72 74 20 6e 42 61 63 will revert nBac
19fa1 6b 66 69 6c 6c 20 74 6f 20 7a 65 72 6f 2c 20 62 kfill to zero, b
19fa2 75 74 20 6e 6f 74 20 69 6e 63 72 65 61 73 65 0a ut not increase.
19fa3 2a 2a 20 69 74 73 20 76 61 6c 75 65 2e 29 0a 2a ** its value.).*
19fa4 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 *.** The caller
19fa5 6d 75 73 74 20 62 65 20 68 6f 6c 64 69 6e 67 20 must be holding
19fa6 73 75 66 66 69 63 69 65 6e 74 20 6c 6f 63 6b 73 sufficient locks
19fa7 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 to ensure that
19fa8 6e 6f 20 6f 74 68 65 72 0a 2a 2a 20 63 68 65 63 no other.** chec
19fa9 6b 70 6f 69 6e 74 20 69 73 20 72 75 6e 6e 69 6e kpoint is runnin
19faa 67 20 28 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 g (in any other
19fab 74 68 72 65 61 64 20 6f 72 20 70 72 6f 63 65 73 thread or proces
19fac 73 29 20 61 74 20 74 68 65 20 73 61 6d 65 0a 2a s) at the same.*
19fad 2a 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 * time..*/.stati
19fae 63 20 69 6e 74 20 77 61 6c 43 68 65 63 6b 70 6f c int walCheckpo
19faf 69 6e 74 28 0a 20 20 57 61 6c 20 2a 70 57 61 6c int(. Wal *pWal
19fb0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
19fb1 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 20 63 6f /* Wal co
19fb2 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e nnection */. in
19fb3 74 20 65 4d 6f 64 65 2c 20 20 20 20 20 20 20 20 t eMode,
19fb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19fb5 20 4f 6e 65 20 6f 66 20 50 41 53 53 49 56 45 2c One of PASSIVE,
19fb6 20 46 55 4c 4c 20 6f 72 20 52 45 53 54 41 52 54 FULL or RESTART
19fb7 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 */. int (*xBus
19fb8 79 43 61 6c 6c 29 28 76 6f 69 64 2a 29 2c 20 20 yCall)(void*),
19fb9 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f /* Functio
19fba 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20 62 n to call when b
19fbb 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 usy */. void *p
19fbc 42 75 73 79 41 72 67 2c 20 20 20 20 20 20 20 20 BusyArg,
19fbd 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 /* Cont
19fbe 65 78 74 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 ext argument for
19fbf 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f xBusyHandler */
19fc0 0a 20 20 69 6e 74 20 73 79 6e 63 5f 66 6c 61 67 . int sync_flag
19fc1 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s,
19fc2 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72 20 /* Flags for
19fc3 4f 73 53 79 6e 63 28 29 20 28 6f 72 20 30 29 20 OsSync() (or 0)
19fc4 2a 2f 0a 20 20 75 38 20 2a 7a 42 75 66 20 20 20 */. u8 *zBuf
19fc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19fc6 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 /* Temporar
19fc7 79 20 62 75 66 66 65 72 20 74 6f 20 75 73 65 20 y buffer to use
19fc8 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 */.){. int rc;
19fc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19fca 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
19fcb 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 n code */. int
19fcc 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 szPage;
19fcd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
19fce 61 74 61 62 61 73 65 20 70 61 67 65 2d 73 69 7a atabase page-siz
19fcf 65 20 2a 2f 0a 20 20 57 61 6c 49 74 65 72 61 74 e */. WalIterat
19fd0 6f 72 20 2a 70 49 74 65 72 20 3d 20 30 3b 20 20 or *pIter = 0;
19fd1 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 20 69 74 /* Wal it
19fd2 65 72 61 74 6f 72 20 63 6f 6e 74 65 78 74 20 2a erator context *
19fd3 2f 0a 20 20 75 33 32 20 69 44 62 70 61 67 65 20 /. u32 iDbpage
19fd4 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
19fd5 20 20 20 20 2f 2a 20 4e 65 78 74 20 64 61 74 61 /* Next data
19fd6 62 61 73 65 20 70 61 67 65 20 74 6f 20 77 72 69 base page to wri
19fd7 74 65 20 2a 2f 0a 20 20 75 33 32 20 69 46 72 61 te */. u32 iFra
19fd8 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 me = 0;
19fd9 20 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 20 66 /* Wal f
19fda 72 61 6d 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 rame containing
19fdb 64 61 74 61 20 66 6f 72 20 69 44 62 70 61 67 65 data for iDbpage
19fdc 20 2a 2f 0a 20 20 75 33 32 20 6d 78 53 61 66 65 */. u32 mxSafe
19fdd 46 72 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 Frame;
19fde 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 66 72 61 /* Max fra
19fdf 6d 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 62 me that can be b
19fe0 61 63 6b 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 75 ackfilled */. u
19fe1 33 32 20 6d 78 50 61 67 65 3b 20 20 20 20 20 20 32 mxPage;
19fe2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19fe3 2a 20 4d 61 78 20 64 61 74 61 62 61 73 65 20 70 * Max database p
19fe4 61 67 65 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a age to write */.
19fe5 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
19fe6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19fe7 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
19fe8 72 20 2a 2f 0a 20 20 76 6f 6c 61 74 69 6c 65 20 r */. volatile
19fe9 57 61 6c 43 6b 70 74 49 6e 66 6f 20 2a 70 49 6e WalCkptInfo *pIn
19fea 66 6f 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 68 fo; /* The ch
19feb 65 63 6b 70 6f 69 6e 74 20 73 74 61 74 75 73 20 eckpoint status
19fec 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 information */.
19fed 20 69 6e 74 20 28 2a 78 42 75 73 79 29 28 76 6f int (*xBusy)(vo
19fee 69 64 2a 29 20 3d 20 30 3b 20 20 20 20 20 20 20 id*) = 0;
19fef 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 /* Function to
19ff0 63 61 6c 6c 20 77 68 65 6e 20 77 61 69 74 69 6e call when waitin
19ff1 67 20 66 6f 72 20 6c 6f 63 6b 73 20 2a 2f 0a 0a g for locks */..
19ff2 20 20 73 7a 50 61 67 65 20 3d 20 77 61 6c 50 61 szPage = walPa
19ff3 67 65 73 69 7a 65 28 70 57 61 6c 29 3b 0a 20 20 gesize(pWal);.
19ff4 74 65 73 74 63 61 73 65 28 20 73 7a 50 61 67 65 testcase( szPage
19ff5 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 74 65 73 <=32768 );. tes
19ff6 74 63 61 73 65 28 20 73 7a 50 61 67 65 3e 3d 36 tcase( szPage>=6
19ff7 35 35 33 36 20 29 3b 0a 20 20 70 49 6e 66 6f 20 5536 );. pInfo
19ff8 3d 20 77 61 6c 43 6b 70 74 49 6e 66 6f 28 70 57 = walCkptInfo(pW
19ff9 61 6c 29 3b 0a 20 20 69 66 28 20 70 49 6e 66 6f al);. if( pInfo
19ffa 2d 3e 6e 42 61 63 6b 66 69 6c 6c 3e 3d 70 57 61 ->nBackfill>=pWa
19ffb 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 20 29 l->hdr.mxFrame )
19ffc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
19ffd 4b 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 K;.. /* Allocat
19ffe 65 20 74 68 65 20 69 74 65 72 61 74 6f 72 20 2a e the iterator *
19fff 2f 0a 20 20 72 63 20 3d 20 77 61 6c 49 74 65 72 /. rc = walIter
1a000 61 74 6f 72 49 6e 69 74 28 70 57 61 6c 2c 20 26 atorInit(pWal, &
1a001 70 49 74 65 72 29 3b 0a 20 20 69 66 28 20 72 63 pIter);. if( rc
1a002 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1a003 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1a004 7d 0a 20 20 61 73 73 65 72 74 28 20 70 49 74 65 }. assert( pIte
1a005 72 20 29 3b 0a 0a 20 20 69 66 28 20 65 4d 6f 64 r );.. if( eMod
1a006 65 21 3d 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 e!=SQLITE_CHECKP
1a007 4f 49 4e 54 5f 50 41 53 53 49 56 45 20 29 20 78 OINT_PASSIVE ) x
1a008 42 75 73 79 20 3d 20 78 42 75 73 79 43 61 6c 6c Busy = xBusyCall
1a009 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 ;.. /* Compute
1a00a 69 6e 20 6d 78 53 61 66 65 46 72 61 6d 65 20 74 in mxSafeFrame t
1a00b 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
1a00c 6c 61 73 74 20 66 72 61 6d 65 20 6f 66 20 74 68 last frame of th
1a00d 65 20 57 41 4c 20 74 68 61 74 20 69 73 0a 20 20 e WAL that is.
1a00e 2a 2a 20 73 61 66 65 20 74 6f 20 77 72 69 74 65 ** safe to write
1a00f 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 into the databa
1a010 73 65 2e 20 20 46 72 61 6d 65 73 20 62 65 79 6f se. Frames beyo
1a011 6e 64 20 6d 78 53 61 66 65 46 72 61 6d 65 20 6d nd mxSafeFrame m
1a012 69 67 68 74 0a 20 20 2a 2a 20 6f 76 65 72 77 72 ight. ** overwr
1a013 69 74 65 20 64 61 74 61 62 61 73 65 20 70 61 67 ite database pag
1a014 65 73 20 74 68 61 74 20 61 72 65 20 69 6e 20 75 es that are in u
1a015 73 65 20 62 79 20 61 63 74 69 76 65 20 72 65 61 se by active rea
1a016 64 65 72 73 20 61 6e 64 20 74 68 75 73 0a 20 20 ders and thus.
1a017 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 62 61 63 ** cannot be bac
1a018 6b 66 69 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 kfilled from the
1a019 20 57 41 4c 2e 0a 20 20 2a 2f 0a 20 20 6d 78 53 WAL.. */. mxS
1a01a 61 66 65 46 72 61 6d 65 20 3d 20 70 57 61 6c 2d afeFrame = pWal-
1a01b 3e 68 64 72 2e 6d 78 46 72 61 6d 65 3b 0a 20 20 >hdr.mxFrame;.
1a01c 6d 78 50 61 67 65 20 3d 20 70 57 61 6c 2d 3e 68 mxPage = pWal->h
1a01d 64 72 2e 6e 50 61 67 65 3b 0a 20 20 66 6f 72 28 dr.nPage;. for(
1a01e 69 3d 31 3b 20 69 3c 57 41 4c 5f 4e 52 45 41 44 i=1; i<WAL_NREAD
1a01f 45 52 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 33 ER; i++){. u3
1a020 32 20 79 20 3d 20 70 49 6e 66 6f 2d 3e 61 52 65 2 y = pInfo->aRe
1a021 61 64 4d 61 72 6b 5b 69 5d 3b 0a 20 20 20 20 69 adMark[i];. i
1a022 66 28 20 6d 78 53 61 66 65 46 72 61 6d 65 3e 79 f( mxSafeFrame>y
1a023 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
1a024 28 20 79 3c 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d ( y<=pWal->hdr.m
1a025 78 46 72 61 6d 65 20 29 3b 0a 20 20 20 20 20 20 xFrame );.
1a026 72 63 20 3d 20 77 61 6c 42 75 73 79 4c 6f 63 6b rc = walBusyLock
1a027 28 70 57 61 6c 2c 20 78 42 75 73 79 2c 20 70 42 (pWal, xBusy, pB
1a028 75 73 79 41 72 67 2c 20 57 41 4c 5f 52 45 41 44 usyArg, WAL_READ
1a029 5f 4c 4f 43 4b 28 69 29 2c 20 31 29 3b 0a 20 20 _LOCK(i), 1);.
1a02a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1a02b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1a02c 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61 72 pInfo->aReadMar
1a02d 6b 5b 69 5d 20 3d 20 28 69 3d 3d 31 20 3f 20 6d k[i] = (i==1 ? m
1a02e 78 53 61 66 65 46 72 61 6d 65 20 3a 20 52 45 41 xSafeFrame : REA
1a02f 44 4d 41 52 4b 5f 4e 4f 54 5f 55 53 45 44 29 3b DMARK_NOT_USED);
1a030 0a 20 20 20 20 20 20 20 20 77 61 6c 55 6e 6c 6f . walUnlo
1a031 63 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61 6c ckExclusive(pWal
1a032 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 , WAL_READ_LOCK(
1a033 69 29 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 65 i), 1);. }e
1a034 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 lse if( rc==SQLI
1a035 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 TE_BUSY ){.
1a036 20 20 20 6d 78 53 61 66 65 46 72 61 6d 65 20 3d mxSafeFrame =
1a037 20 79 3b 0a 20 20 20 20 20 20 20 20 78 42 75 73 y;. xBus
1a038 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c y = 0;. }el
1a039 73 65 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f se{. goto
1a03a 20 77 61 6c 63 68 65 63 6b 70 6f 69 6e 74 5f 6f walcheckpoint_o
1a03b 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ut;. }.
1a03c 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 49 6e }. }.. if( pIn
1a03d 66 6f 2d 3e 6e 42 61 63 6b 66 69 6c 6c 3c 6d 78 fo->nBackfill<mx
1a03e 53 61 66 65 46 72 61 6d 65 0a 20 20 20 26 26 20 SafeFrame. &&
1a03f 28 72 63 20 3d 20 77 61 6c 42 75 73 79 4c 6f 63 (rc = walBusyLoc
1a040 6b 28 70 57 61 6c 2c 20 78 42 75 73 79 2c 20 70 k(pWal, xBusy, p
1a041 42 75 73 79 41 72 67 2c 20 57 41 4c 5f 52 45 41 BusyArg, WAL_REA
1a042 44 5f 4c 4f 43 4b 28 30 29 2c 20 31 29 29 3d 3d D_LOCK(0), 1))==
1a043 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 29 7b 0a 20 SQLITE_OK. ){.
1a044 20 20 20 69 36 34 20 6e 53 69 7a 65 3b 20 20 20 i64 nSize;
1a045 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a046 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 69 7a 65 /* Current size
1a047 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c of database fil
1a048 65 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 42 61 e */. u32 nBa
1a049 63 6b 66 69 6c 6c 20 3d 20 70 49 6e 66 6f 2d 3e ckfill = pInfo->
1a04a 6e 42 61 63 6b 66 69 6c 6c 3b 0a 0a 20 20 20 20 nBackfill;..
1a04b 2f 2a 20 53 79 6e 63 20 74 68 65 20 57 41 4c 20 /* Sync the WAL
1a04c 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 20 20 69 to disk */. i
1a04d 66 28 20 73 79 6e 63 5f 66 6c 61 67 73 20 29 7b f( sync_flags ){
1a04e 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1a04f 74 65 33 4f 73 53 79 6e 63 28 70 57 61 6c 2d 3e te3OsSync(pWal->
1a050 70 57 61 6c 46 64 2c 20 73 79 6e 63 5f 66 6c 61 pWalFd, sync_fla
1a051 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 gs);. }..
1a052 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 /* If the databa
1a053 73 65 20 66 69 6c 65 20 6d 61 79 20 67 72 6f 77 se file may grow
1a054 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 as a result of
1a055 74 68 69 73 20 63 68 65 63 6b 70 6f 69 6e 74 2c this checkpoint,
1a056 20 68 69 6e 74 0a 20 20 20 20 2a 2a 20 61 62 6f hint. ** abo
1a057 75 74 20 74 68 65 20 65 76 65 6e 74 75 61 6c 20 ut the eventual
1a058 73 69 7a 65 20 6f 66 20 74 68 65 20 64 62 20 66 size of the db f
1a059 69 6c 65 20 74 6f 20 74 68 65 20 56 46 53 20 6c ile to the VFS l
1a05a 61 79 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 ayer. . */.
1a05b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1a05c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 36 34 _OK ){. i64
1a05d 20 6e 52 65 71 20 3d 20 28 28 69 36 34 29 6d 78 nReq = ((i64)mx
1a05e 50 61 67 65 20 2a 20 73 7a 50 61 67 65 29 3b 0a Page * szPage);.
1a05f 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1a060 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 57 61 e3OsFileSize(pWa
1a061 6c 2d 3e 70 44 62 46 64 2c 20 26 6e 53 69 7a 65 l->pDbFd, &nSize
1a062 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
1a063 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 53 =SQLITE_OK && nS
1a064 69 7a 65 3c 6e 52 65 71 20 29 7b 0a 20 20 20 20 ize<nReq ){.
1a065 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69 6c sqlite3OsFil
1a066 65 43 6f 6e 74 72 6f 6c 48 69 6e 74 28 70 57 61 eControlHint(pWa
1a067 6c 2d 3e 70 44 62 46 64 2c 20 53 51 4c 49 54 45 l->pDbFd, SQLITE
1a068 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e 54 _FCNTL_SIZE_HINT
1a069 2c 20 26 6e 52 65 71 29 3b 0a 20 20 20 20 20 20 , &nReq);.
1a06a 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
1a06b 49 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 Iterate through
1a06c 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
1a06d 74 68 65 20 57 41 4c 2c 20 63 6f 70 79 69 6e 67 the WAL, copying
1a06e 20 64 61 74 61 20 74 6f 20 74 68 65 20 64 62 20 data to the db
1a06f 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 77 68 69 file. */. whi
1a070 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
1a071 4b 20 26 26 20 30 3d 3d 77 61 6c 49 74 65 72 61 K && 0==walItera
1a072 74 6f 72 4e 65 78 74 28 70 49 74 65 72 2c 20 26 torNext(pIter, &
1a073 69 44 62 70 61 67 65 2c 20 26 69 46 72 61 6d 65 iDbpage, &iFrame
1a074 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69 ) ){. i64 i
1a075 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 61 73 Offset;. as
1a076 73 65 72 74 28 20 77 61 6c 46 72 61 6d 65 50 67 sert( walFramePg
1a077 6e 6f 28 70 57 61 6c 2c 20 69 46 72 61 6d 65 29 no(pWal, iFrame)
1a078 3d 3d 69 44 62 70 61 67 65 20 29 3b 0a 20 20 20 ==iDbpage );.
1a079 20 20 20 69 66 28 20 69 46 72 61 6d 65 3c 3d 6e if( iFrame<=n
1a07a 42 61 63 6b 66 69 6c 6c 20 7c 7c 20 69 46 72 61 Backfill || iFra
1a07b 6d 65 3e 6d 78 53 61 66 65 46 72 61 6d 65 20 7c me>mxSafeFrame |
1a07c 7c 20 69 44 62 70 61 67 65 3e 6d 78 50 61 67 65 | iDbpage>mxPage
1a07d 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
1a07e 20 20 20 69 4f 66 66 73 65 74 20 3d 20 77 61 6c iOffset = wal
1a07f 46 72 61 6d 65 4f 66 66 73 65 74 28 69 46 72 61 FrameOffset(iFra
1a080 6d 65 2c 20 73 7a 50 61 67 65 29 20 2b 20 57 41 me, szPage) + WA
1a081 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 5a 45 3b L_FRAME_HDRSIZE;
1a082 0a 20 20 20 20 20 20 2f 2a 20 74 65 73 74 63 61 . /* testca
1a083 73 65 28 20 49 53 5f 42 49 47 5f 49 4e 54 28 69 se( IS_BIG_INT(i
1a084 4f 66 66 73 65 74 29 20 29 3b 20 2f 2f 20 72 65 Offset) ); // re
1a085 71 75 69 72 65 73 20 61 20 34 47 69 42 20 57 41 quires a 4GiB WA
1a086 4c 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 20 20 L file */.
1a087 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 rc = sqlite3OsRe
1a088 61 64 28 70 57 61 6c 2d 3e 70 57 61 6c 46 64 2c ad(pWal->pWalFd,
1a089 20 7a 42 75 66 2c 20 73 7a 50 61 67 65 2c 20 69 zBuf, szPage, i
1a08a 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 69 Offset);. i
1a08b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1a08c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 ) break;.
1a08d 69 4f 66 66 73 65 74 20 3d 20 28 69 44 62 70 61 iOffset = (iDbpa
1a08e 67 65 2d 31 29 2a 28 69 36 34 29 73 7a 50 61 67 ge-1)*(i64)szPag
1a08f 65 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 e;. testcas
1a090 65 28 20 49 53 5f 42 49 47 5f 49 4e 54 28 69 4f e( IS_BIG_INT(iO
1a091 66 66 73 65 74 29 20 29 3b 0a 20 20 20 20 20 20 ffset) );.
1a092 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
1a093 69 74 65 28 70 57 61 6c 2d 3e 70 44 62 46 64 2c ite(pWal->pDbFd,
1a094 20 7a 42 75 66 2c 20 73 7a 50 61 67 65 2c 20 69 zBuf, szPage, i
1a095 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 69 Offset);. i
1a096 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1a097 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a ) break;. }.
1a098 0a 20 20 20 20 2f 2a 20 49 66 20 77 6f 72 6b 20 . /* If work
1a099 77 61 73 20 61 63 74 75 61 6c 6c 79 20 61 63 63 was actually acc
1a09a 6f 6d 70 6c 69 73 68 65 64 2e 2e 2e 20 2a 2f 0a omplished... */.
1a09b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1a09c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 TE_OK ){. i
1a09d 66 28 20 6d 78 53 61 66 65 46 72 61 6d 65 3d 3d f( mxSafeFrame==
1a09e 77 61 6c 49 6e 64 65 78 48 64 72 28 70 57 61 6c walIndexHdr(pWal
1a09f 29 2d 3e 6d 78 46 72 61 6d 65 20 29 7b 0a 20 20 )->mxFrame ){.
1a0a0 20 20 20 20 20 20 69 36 34 20 73 7a 44 62 20 3d i64 szDb =
1a0a1 20 70 57 61 6c 2d 3e 68 64 72 2e 6e 50 61 67 65 pWal->hdr.nPage
1a0a2 2a 28 69 36 34 29 73 7a 50 61 67 65 3b 0a 20 20 *(i64)szPage;.
1a0a3 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1a0a4 49 53 5f 42 49 47 5f 49 4e 54 28 73 7a 44 62 29 IS_BIG_INT(szDb)
1a0a5 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d );. rc =
1a0a6 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
1a0a7 74 65 28 70 57 61 6c 2d 3e 70 44 62 46 64 2c 20 te(pWal->pDbFd,
1a0a8 73 7a 44 62 29 3b 0a 20 20 20 20 20 20 20 20 69 szDb);. i
1a0a9 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1a0aa 20 26 26 20 73 79 6e 63 5f 66 6c 61 67 73 20 29 && sync_flags )
1a0ab 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
1a0ac 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
1a0ad 57 61 6c 2d 3e 70 44 62 46 64 2c 20 73 79 6e 63 Wal->pDbFd, sync
1a0ae 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 _flags);.
1a0af 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
1a0b0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1a0b1 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49 OK ){. pI
1a0b2 6e 66 6f 2d 3e 6e 42 61 63 6b 66 69 6c 6c 20 3d nfo->nBackfill =
1a0b3 20 6d 78 53 61 66 65 46 72 61 6d 65 3b 0a 20 20 mxSafeFrame;.
1a0b4 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
1a0b5 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 /* Release the
1a0b6 72 65 61 64 65 72 20 6c 6f 63 6b 20 68 65 6c 64 reader lock held
1a0b7 20 77 68 69 6c 65 20 62 61 63 6b 66 69 6c 6c 69 while backfilli
1a0b8 6e 67 20 2a 2f 0a 20 20 20 20 77 61 6c 55 6e 6c ng */. walUnl
1a0b9 6f 63 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61 ockExclusive(pWa
1a0ba 6c 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b l, WAL_READ_LOCK
1a0bb 28 30 29 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 (0), 1);. }..
1a0bc 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 if( rc==SQLITE_B
1a0bd 55 53 59 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 USY ){. /* Re
1a0be 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63 set the return c
1a0bf 6f 64 65 20 73 6f 20 61 73 20 6e 6f 74 20 74 6f ode so as not to
1a0c0 20 72 65 70 6f 72 74 20 61 20 63 68 65 63 6b 70 report a checkp
1a0c1 6f 69 6e 74 20 66 61 69 6c 75 72 65 0a 20 20 20 oint failure.
1a0c2 20 2a 2a 20 6a 75 73 74 20 62 65 63 61 75 73 65 ** just because
1a0c3 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 there are activ
1a0c4 65 20 72 65 61 64 65 72 73 2e 20 20 2a 2f 0a 20 e readers. */.
1a0c5 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
1a0c6 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 K;. }.. /* If
1a0c7 74 68 69 73 20 69 73 20 61 6e 20 53 51 4c 49 54 this is an SQLIT
1a0c8 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 52 45 53 E_CHECKPOINT_RES
1a0c9 54 41 52 54 20 6f 70 65 72 61 74 69 6f 6e 2c 20 TART operation,
1a0ca 61 6e 64 20 74 68 65 20 65 6e 74 69 72 65 20 77 and the entire w
1a0cb 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 68 61 73 al. ** file has
1a0cc 20 62 65 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 been copied int
1a0cd 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
1a0ce 69 6c 65 2c 20 74 68 65 6e 20 62 6c 6f 63 6b 20 ile, then block
1a0cf 75 6e 74 69 6c 20 61 6c 6c 0a 20 20 2a 2a 20 72 until all. ** r
1a0d0 65 61 64 65 72 73 20 68 61 76 65 20 66 69 6e 69 eaders have fini
1a0d1 73 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 77 shed using the w
1a0d2 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 65 6e al file. This en
1a0d3 73 75 72 65 73 20 74 68 61 74 20 74 68 65 20 6e sures that the n
1a0d4 65 78 74 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 ext. ** process
1a0d5 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 to write to the
1a0d6 20 64 61 74 61 62 61 73 65 20 72 65 73 74 61 72 database restar
1a0d7 74 73 20 74 68 65 20 77 61 6c 20 66 69 6c 65 2e ts the wal file.
1a0d8 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d . */. if( rc==
1a0d9 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 4d 6f SQLITE_OK && eMo
1a0da 64 65 21 3d 53 51 4c 49 54 45 5f 43 48 45 43 4b de!=SQLITE_CHECK
1a0db 50 4f 49 4e 54 5f 50 41 53 53 49 56 45 20 29 7b POINT_PASSIVE ){
1a0dc 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 57 61 . assert( pWa
1a0dd 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20 29 3b 0a l->writeLock );.
1a0de 20 20 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e if( pInfo->n
1a0df 42 61 63 6b 66 69 6c 6c 3c 70 57 61 6c 2d 3e 68 Backfill<pWal->h
1a0e0 64 72 2e 6d 78 46 72 61 6d 65 20 29 7b 0a 20 20 dr.mxFrame ){.
1a0e1 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1a0e2 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 20 BUSY;. }else
1a0e3 69 66 28 20 65 4d 6f 64 65 3d 3d 53 51 4c 49 54 if( eMode==SQLIT
1a0e4 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 52 45 53 E_CHECKPOINT_RES
1a0e5 54 41 52 54 20 29 7b 0a 20 20 20 20 20 20 61 73 TART ){. as
1a0e6 73 65 72 74 28 20 6d 78 53 61 66 65 46 72 61 6d sert( mxSafeFram
1a0e7 65 3d 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 e==pWal->hdr.mxF
1a0e8 72 61 6d 65 20 29 3b 0a 20 20 20 20 20 20 72 63 rame );. rc
1a0e9 20 3d 20 77 61 6c 42 75 73 79 4c 6f 63 6b 28 70 = walBusyLock(p
1a0ea 57 61 6c 2c 20 78 42 75 73 79 2c 20 70 42 75 73 Wal, xBusy, pBus
1a0eb 79 41 72 67 2c 20 57 41 4c 5f 52 45 41 44 5f 4c yArg, WAL_READ_L
1a0ec 4f 43 4b 28 31 29 2c 20 57 41 4c 5f 4e 52 45 41 OCK(1), WAL_NREA
1a0ed 44 45 52 2d 31 29 3b 0a 20 20 20 20 20 20 69 66 DER-1);. if
1a0ee 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1a0ef 29 7b 0a 20 20 20 20 20 20 20 20 77 61 6c 55 6e ){. walUn
1a0f0 6c 6f 63 6b 45 78 63 6c 75 73 69 76 65 28 70 57 lockExclusive(pW
1a0f1 61 6c 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 al, WAL_READ_LOC
1a0f2 4b 28 31 29 2c 20 57 41 4c 5f 4e 52 45 41 44 45 K(1), WAL_NREADE
1a0f3 52 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 R-1);. }.
1a0f4 20 20 7d 0a 20 20 7d 0a 0a 20 77 61 6c 63 68 65 }. }.. walche
1a0f5 63 6b 70 6f 69 6e 74 5f 6f 75 74 3a 0a 20 20 77 ckpoint_out:. w
1a0f6 61 6c 49 74 65 72 61 74 6f 72 46 72 65 65 28 70 alIteratorFree(p
1a0f7 49 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 Iter);. return
1a0f8 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 rc;.}../*.** If
1a0f9 74 68 65 20 57 41 4c 20 66 69 6c 65 20 69 73 20 the WAL file is
1a0fa 63 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 currently larger
1a0fb 20 74 68 61 6e 20 6e 4d 61 78 20 62 79 74 65 73 than nMax bytes
1a0fc 20 69 6e 20 73 69 7a 65 2c 20 74 72 75 6e 63 61 in size, trunca
1a0fd 74 65 0a 2a 2a 20 69 74 20 74 6f 20 65 78 61 63 te.** it to exac
1a0fe 74 6c 79 20 6e 4d 61 78 20 62 79 74 65 73 2e 20 tly nMax bytes.
1a0ff 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
1a100 72 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 rs while doing s
1a101 6f 2c 20 69 67 6e 6f 72 65 20 69 74 2e 0a 2a 2f o, ignore it..*/
1a102 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 61 6c .static void wal
1a103 4c 69 6d 69 74 53 69 7a 65 28 57 61 6c 20 2a 70 LimitSize(Wal *p
1a104 57 61 6c 2c 20 69 36 34 20 6e 4d 61 78 29 7b 0a Wal, i64 nMax){.
1a105 20 20 69 36 34 20 73 7a 3b 0a 20 20 69 6e 74 20 i64 sz;. int
1a106 72 78 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 rx;. sqlite3Beg
1a107 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 inBenignMalloc()
1a108 3b 0a 20 20 72 78 20 3d 20 73 71 6c 69 74 65 33 ;. rx = sqlite3
1a109 4f 73 46 69 6c 65 53 69 7a 65 28 70 57 61 6c 2d OsFileSize(pWal-
1a10a 3e 70 57 61 6c 46 64 2c 20 26 73 7a 29 3b 0a 20 >pWalFd, &sz);.
1a10b 20 69 66 28 20 72 78 3d 3d 53 51 4c 49 54 45 5f if( rx==SQLITE_
1a10c 4f 4b 20 26 26 20 28 73 7a 20 3e 20 6e 4d 61 78 OK && (sz > nMax
1a10d 20 29 20 29 7b 0a 20 20 20 20 72 78 20 3d 20 73 ) ){. rx = s
1a10e 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 qlite3OsTruncate
1a10f 28 70 57 61 6c 2d 3e 70 57 61 6c 46 64 2c 20 6e (pWal->pWalFd, n
1a110 4d 61 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 Max);. }. sqli
1a111 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
1a112 6f 63 28 29 3b 0a 20 20 69 66 28 20 72 78 20 29 oc();. if( rx )
1a113 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f {. sqlite3_lo
1a114 67 28 72 78 2c 20 22 63 61 6e 6e 6f 74 20 6c 69 g(rx, "cannot li
1a115 6d 69 74 20 57 41 4c 20 73 69 7a 65 3a 20 25 73 mit WAL size: %s
1a116 22 2c 20 70 57 61 6c 2d 3e 7a 57 61 6c 4e 61 6d ", pWal->zWalNam
1a117 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a e);. }.}../*.**
1a118 20 43 6c 6f 73 65 20 61 20 63 6f 6e 6e 65 63 74 Close a connect
1a119 69 6f 6e 20 74 6f 20 61 20 6c 6f 67 20 66 69 6c ion to a log fil
1a11a 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1a11b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1a11c 57 61 6c 43 6c 6f 73 65 28 0a 20 20 57 61 6c 20 WalClose(. Wal
1a11d 2a 70 57 61 6c 2c 20 20 20 20 20 20 20 20 20 20 *pWal,
1a11e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
1a11f 61 6c 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 20 al to close */.
1a120 20 69 6e 74 20 73 79 6e 63 5f 66 6c 61 67 73 2c int sync_flags,
1a121 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a122 20 2f 2a 20 46 6c 61 67 73 20 74 6f 20 70 61 73 /* Flags to pas
1a123 73 20 74 6f 20 4f 73 53 79 6e 63 28 29 20 28 6f s to OsSync() (o
1a124 72 20 30 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 r 0) */. int nB
1a125 75 66 2c 0a 20 20 75 38 20 2a 7a 42 75 66 20 20 uf,. u8 *zBuf
1a126 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a127 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 /* Buffer
1a128 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42 75 66 of at least nBuf
1a129 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 bytes */.){. i
1a12a 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1a12b 4b 3b 0a 20 20 69 66 28 20 70 57 61 6c 20 29 7b K;. if( pWal ){
1a12c 0a 20 20 20 20 69 6e 74 20 69 73 44 65 6c 65 74 . int isDelet
1a12d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 e = 0;
1a12e 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 75 6e /* True to un
1a12f 6c 69 6e 6b 20 77 61 6c 20 61 6e 64 20 77 61 6c link wal and wal
1a130 2d 69 6e 64 65 78 20 66 69 6c 65 73 20 2a 2f 0a -index files */.
1a131 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 45 58 . /* If an EX
1a132 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 63 61 6e CLUSIVE lock can
1a133 20 62 65 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 be obtained on
1a134 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1a135 65 20 28 75 73 69 6e 67 20 74 68 65 0a 20 20 20 e (using the.
1a136 20 2a 2a 20 6f 72 64 69 6e 61 72 79 2c 20 72 6f ** ordinary, ro
1a137 6c 6c 62 61 63 6b 2d 6d 6f 64 65 20 6c 6f 63 6b llback-mode lock
1a138 69 6e 67 20 6d 65 74 68 6f 64 73 2c 20 74 68 69 ing methods, thi
1a139 73 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 s guarantees tha
1a13a 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6e t the. ** con
1a13b 6e 65 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 nection associat
1a13c 65 64 20 77 69 74 68 20 74 68 69 73 20 6c 6f 67 ed with this log
1a13d 20 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c file is the onl
1a13e 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 0a y connection to.
1a13f 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 ** the datab
1a140 61 73 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ase. In this cas
1a141 65 20 63 68 65 63 6b 70 6f 69 6e 74 20 74 68 65 e checkpoint the
1a142 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 75 6e database and un
1a143 6c 69 6e 6b 20 62 6f 74 68 0a 20 20 20 20 2a 2a link both. **
1a144 20 74 68 65 20 77 61 6c 20 61 6e 64 20 77 61 6c the wal and wal
1a145 2d 69 6e 64 65 78 20 66 69 6c 65 73 2e 0a 20 20 -index files..
1a146 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 **. ** The
1a147 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 EXCLUSIVE lock i
1a148 73 20 6e 6f 74 20 72 65 6c 65 61 73 65 64 20 62 s not released b
1a149 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e efore returning.
1a14a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
1a14b 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 sqlite3OsLock(p
1a14c 57 61 6c 2d 3e 70 44 62 46 64 2c 20 53 51 4c 49 Wal->pDbFd, SQLI
1a14d 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 TE_LOCK_EXCLUSIV
1a14e 45 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d E);. if( rc==
1a14f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1a150 20 20 20 69 66 28 20 70 57 61 6c 2d 3e 65 78 63 if( pWal->exc
1a151 6c 75 73 69 76 65 4d 6f 64 65 3d 3d 57 41 4c 5f lusiveMode==WAL_
1a152 4e 4f 52 4d 41 4c 5f 4d 4f 44 45 20 29 7b 0a 20 NORMAL_MODE ){.
1a153 20 20 20 20 20 20 20 70 57 61 6c 2d 3e 65 78 63 pWal->exc
1a154 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 57 41 4c lusiveMode = WAL
1a155 5f 45 58 43 4c 55 53 49 56 45 5f 4d 4f 44 45 3b _EXCLUSIVE_MODE;
1a156 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
1a157 63 20 3d 20 73 71 6c 69 74 65 33 57 61 6c 43 68 c = sqlite3WalCh
1a158 65 63 6b 70 6f 69 6e 74 28 0a 20 20 20 20 20 20 eckpoint(.
1a159 20 20 20 20 70 57 61 6c 2c 20 53 51 4c 49 54 45 pWal, SQLITE
1a15a 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 50 41 53 53 _CHECKPOINT_PASS
1a15b 49 56 45 2c 20 30 2c 20 30 2c 20 73 79 6e 63 5f IVE, 0, 0, sync_
1a15c 66 6c 61 67 73 2c 20 6e 42 75 66 2c 20 7a 42 75 flags, nBuf, zBu
1a15d 66 2c 20 30 2c 20 30 0a 20 20 20 20 20 20 29 3b f, 0, 0. );
1a15e 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
1a15f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1a160 20 20 20 20 69 6e 74 20 62 50 65 72 73 69 73 74 int bPersist
1a161 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 73 = -1;. s
1a162 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 qlite3OsFileCont
1a163 72 6f 6c 48 69 6e 74 28 0a 20 20 20 20 20 20 20 rolHint(.
1a164 20 20 20 20 20 70 57 61 6c 2d 3e 70 44 62 46 64 pWal->pDbFd
1a165 2c 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 , SQLITE_FCNTL_P
1a166 45 52 53 49 53 54 5f 57 41 4c 2c 20 26 62 50 65 ERSIST_WAL, &bPe
1a167 72 73 69 73 74 0a 20 20 20 20 20 20 20 20 29 3b rsist. );
1a168 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 50 65 . if( bPe
1a169 72 73 69 73 74 21 3d 31 20 29 7b 0a 20 20 20 20 rsist!=1 ){.
1a16a 20 20 20 20 20 20 2f 2a 20 54 72 79 20 74 6f 20 /* Try to
1a16b 64 65 6c 65 74 65 20 74 68 65 20 57 41 4c 20 66 delete the WAL f
1a16c 69 6c 65 20 69 66 20 74 68 65 20 63 68 65 63 6b ile if the check
1a16d 70 6f 69 6e 74 20 63 6f 6d 70 6c 65 74 65 64 20 point completed
1a16e 61 6e 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a and. **
1a16f 20 66 73 79 6e 65 64 20 28 72 63 3d 3d 53 51 4c fsyned (rc==SQL
1a170 49 54 45 5f 4f 4b 29 20 61 6e 64 20 69 66 20 77 ITE_OK) and if w
1a171 65 20 61 72 65 20 6e 6f 74 20 69 6e 20 70 65 72 e are not in per
1a172 73 69 73 74 65 6e 74 2d 77 61 6c 0a 20 20 20 20 sistent-wal.
1a173 20 20 20 20 20 20 2a 2a 20 6d 6f 64 65 20 28 21 ** mode (!
1a174 62 50 65 72 73 69 73 74 29 20 2a 2f 0a 20 20 20 bPersist) */.
1a175 20 20 20 20 20 20 20 69 73 44 65 6c 65 74 65 20 isDelete
1a176 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c = 1;. }el
1a177 73 65 20 69 66 28 20 70 57 61 6c 2d 3e 6d 78 57 se if( pWal->mxW
1a178 61 6c 53 69 7a 65 3e 3d 30 20 29 7b 0a 20 20 20 alSize>=0 ){.
1a179 20 20 20 20 20 20 20 2f 2a 20 54 72 79 20 74 6f /* Try to
1a17a 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 57 41 truncate the WA
1a17b 4c 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62 L file to zero b
1a17c 79 74 65 73 20 69 66 20 74 68 65 20 63 68 65 63 ytes if the chec
1a17d 6b 70 6f 69 6e 74 0a 20 20 20 20 20 20 20 20 20 kpoint.
1a17e 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20 61 6e ** completed an
1a17f 64 20 66 73 79 6e 63 65 64 20 28 72 63 3d 3d 53 d fsynced (rc==S
1a180 51 4c 49 54 45 5f 4f 4b 29 20 61 6e 64 20 77 65 QLITE_OK) and we
1a181 20 61 72 65 20 69 6e 20 70 65 72 73 69 73 74 65 are in persiste
1a182 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 nt. **
1a183 57 41 4c 20 6d 6f 64 65 20 28 62 50 65 72 73 69 WAL mode (bPersi
1a184 73 74 29 20 61 6e 64 20 69 66 20 74 68 65 20 50 st) and if the P
1a185 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69 RAGMA journal_si
1a186 7a 65 5f 6c 69 6d 69 74 20 69 73 20 61 0a 20 20 ze_limit is a.
1a187 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f 6e 2d 6e ** non-n
1a188 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 28 70 egative value (p
1a189 57 61 6c 2d 3e 6d 78 57 61 6c 53 69 7a 65 3e 3d Wal->mxWalSize>=
1a18a 30 29 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 77 0). Note that w
1a18b 65 20 74 72 75 6e 63 61 74 65 0a 20 20 20 20 20 e truncate.
1a18c 20 20 20 20 20 2a 2a 20 74 6f 20 7a 65 72 6f 20 ** to zero
1a18d 62 79 74 65 73 20 61 73 20 74 72 75 6e 63 61 74 bytes as truncat
1a18e 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ing to the journ
1a18f 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 20 6d 69 al_size_limit mi
1a190 67 68 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ght. **
1a191 20 6c 65 61 76 65 20 61 20 63 6f 72 72 75 70 74 leave a corrupt
1a192 20 57 41 4c 20 66 69 6c 65 20 6f 6e 20 64 69 73 WAL file on dis
1a193 6b 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 k. */.
1a194 77 61 6c 4c 69 6d 69 74 53 69 7a 65 28 70 57 61 walLimitSize(pWa
1a195 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d l, 0);. }
1a196 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
1a197 20 20 20 20 77 61 6c 49 6e 64 65 78 43 6c 6f 73 walIndexClos
1a198 65 28 70 57 61 6c 2c 20 69 73 44 65 6c 65 74 65 e(pWal, isDelete
1a199 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 );. sqlite3Os
1a19a 43 6c 6f 73 65 28 70 57 61 6c 2d 3e 70 57 61 6c Close(pWal->pWal
1a19b 46 64 29 3b 0a 20 20 20 20 69 66 28 20 69 73 44 Fd);. if( isD
1a19c 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20 73 elete ){. s
1a19d 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
1a19e 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 nMalloc();.
1a19f 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
1a1a0 28 70 57 61 6c 2d 3e 70 56 66 73 2c 20 70 57 61 (pWal->pVfs, pWa
1a1a1 6c 2d 3e 7a 57 61 6c 4e 61 6d 65 2c 20 30 29 3b l->zWalName, 0);
1a1a2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e . sqlite3En
1a1a3 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
1a1a4 0a 20 20 20 20 7d 0a 20 20 20 20 57 41 4c 54 52 . }. WALTR
1a1a5 41 43 45 28 28 22 57 41 4c 25 70 3a 20 63 6c 6f ACE(("WAL%p: clo
1a1a6 73 65 64 5c 6e 22 2c 20 70 57 61 6c 29 29 3b 0a sed\n", pWal));.
1a1a7 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
1a1a8 28 28 76 6f 69 64 20 2a 29 70 57 61 6c 2d 3e 61 ((void *)pWal->a
1a1a9 70 57 69 44 61 74 61 29 3b 0a 20 20 20 20 73 71 pWiData);. sq
1a1aa 6c 69 74 65 33 5f 66 72 65 65 28 70 57 61 6c 29 lite3_free(pWal)
1a1ab 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1a1ac 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 c;.}../*.** Try
1a1ad 74 6f 20 72 65 61 64 20 74 68 65 20 77 61 6c 2d to read the wal-
1a1ae 69 6e 64 65 78 20 68 65 61 64 65 72 2e 20 20 52 index header. R
1a1af 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 eturn 0 on succe
1a1b0 73 73 20 61 6e 64 20 31 20 69 66 0a 2a 2a 20 74 ss and 1 if.** t
1a1b1 68 65 72 65 20 69 73 20 61 20 70 72 6f 62 6c 65 here is a proble
1a1b2 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 61 6c m..**.** The wal
1a1b3 2d 69 6e 64 65 78 20 69 73 20 69 6e 20 73 68 61 -index is in sha
1a1b4 72 65 64 20 6d 65 6d 6f 72 79 2e 20 20 41 6e 6f red memory. Ano
1a1b5 74 68 65 72 20 74 68 72 65 61 64 20 6f 72 20 70 ther thread or p
1a1b6 72 6f 63 65 73 73 20 6d 69 67 68 74 0a 2a 2a 20 rocess might.**
1a1b7 62 65 20 77 72 69 74 69 6e 67 20 74 68 65 20 68 be writing the h
1a1b8 65 61 64 65 72 20 61 74 20 74 68 65 20 73 61 6d eader at the sam
1a1b9 65 20 74 69 6d 65 20 74 68 69 73 20 70 72 6f 63 e time this proc
1a1ba 65 64 75 72 65 20 69 73 20 74 72 79 69 6e 67 20 edure is trying
1a1bb 74 6f 0a 2a 2a 20 72 65 61 64 20 69 74 2c 20 77 to.** read it, w
1a1bc 68 69 63 68 20 6d 69 67 68 74 20 72 65 73 75 6c hich might resul
1a1bd 74 20 69 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e t in inconsisten
1a1be 63 79 2e 20 20 41 20 64 69 72 74 79 20 72 65 61 cy. A dirty rea
1a1bf 64 20 69 73 20 64 65 74 65 63 74 65 64 0a 2a 2a d is detected.**
1a1c0 20 62 79 20 76 65 72 69 66 79 69 6e 67 20 74 68 by verifying th
1a1c1 61 74 20 62 6f 74 68 20 63 6f 70 69 65 73 20 6f at both copies o
1a1c2 66 20 74 68 65 20 68 65 61 64 65 72 20 61 72 65 f the header are
1a1c3 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20 61 6c the same and al
1a1c4 73 6f 20 62 79 0a 2a 2a 20 61 20 63 68 65 63 6b so by.** a check
1a1c5 73 75 6d 20 6f 6e 20 74 68 65 20 68 65 61 64 65 sum on the heade
1a1c6 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 64 20 r..**.** If and
1a1c7 6f 6e 6c 79 20 69 66 20 74 68 65 20 72 65 61 64 only if the read
1a1c8 20 69 73 20 63 6f 6e 73 69 73 74 65 6e 74 20 61 is consistent a
1a1c9 6e 64 20 74 68 65 20 68 65 61 64 65 72 20 69 73 nd the header is
1a1ca 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a different from.
1a1cb 2a 2a 20 70 57 61 6c 2d 3e 68 64 72 2c 20 74 68 ** pWal->hdr, th
1a1cc 65 6e 20 70 57 61 6c 2d 3e 68 64 72 20 69 73 20 en pWal->hdr is
1a1cd 75 70 64 61 74 65 64 20 74 6f 20 74 68 65 20 63 updated to the c
1a1ce 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 ontent of the ne
1a1cf 77 20 68 65 61 64 65 72 0a 2a 2a 20 61 6e 64 20 w header.** and
1a1d0 2a 70 43 68 61 6e 67 65 64 20 69 73 20 73 65 74 *pChanged is set
1a1d1 20 74 6f 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 to 1..**.** If
1a1d2 74 68 65 20 63 68 65 63 6b 73 75 6d 20 63 61 6e the checksum can
1a1d3 6e 6f 74 20 62 65 20 76 65 72 69 66 69 65 64 20 not be verified
1a1d4 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e return non-zero.
1a1d5 20 49 66 20 74 68 65 20 68 65 61 64 65 72 0a 2a If the header.*
1a1d6 2a 20 69 73 20 72 65 61 64 20 73 75 63 63 65 73 * is read succes
1a1d7 73 66 75 6c 6c 79 20 61 6e 64 20 74 68 65 20 63 sfully and the c
1a1d8 68 65 63 6b 73 75 6d 20 76 65 72 69 66 69 65 64 hecksum verified
1a1d9 2c 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e 0a 2a , return zero..*
1a1da 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c /.static int wal
1a1db 49 6e 64 65 78 54 72 79 48 64 72 28 57 61 6c 20 IndexTryHdr(Wal
1a1dc 2a 70 57 61 6c 2c 20 69 6e 74 20 2a 70 43 68 61 *pWal, int *pCha
1a1dd 6e 67 65 64 29 7b 0a 20 20 75 33 32 20 61 43 6b nged){. u32 aCk
1a1de 73 75 6d 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 sum[2];
1a1df 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 /* Chec
1a1e0 6b 73 75 6d 20 6f 6e 20 74 68 65 20 68 65 61 64 ksum on the head
1a1e1 65 72 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 er content */.
1a1e2 57 61 6c 49 6e 64 65 78 48 64 72 20 68 31 2c 20 WalIndexHdr h1,
1a1e3 68 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 h2;
1a1e4 2f 2a 20 54 77 6f 20 63 6f 70 69 65 73 20 6f 66 /* Two copies of
1a1e5 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6e 74 the header cont
1a1e6 65 6e 74 20 2a 2f 0a 20 20 57 61 6c 49 6e 64 65 ent */. WalInde
1a1e7 78 48 64 72 20 76 6f 6c 61 74 69 6c 65 20 2a 61 xHdr volatile *a
1a1e8 48 64 72 3b 20 20 20 20 20 2f 2a 20 48 65 61 64 Hdr; /* Head
1a1e9 65 72 20 69 6e 20 73 68 61 72 65 64 20 6d 65 6d er in shared mem
1a1ea 6f 72 79 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 ory */.. /* The
1a1eb 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 first page of t
1a1ec 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 6d 75 73 he wal-index mus
1a1ed 74 20 62 65 20 6d 61 70 70 65 64 20 61 74 20 74 t be mapped at t
1a1ee 68 69 73 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 his point. */.
1a1ef 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 6e 57 assert( pWal->nW
1a1f0 69 44 61 74 61 3e 30 20 26 26 20 70 57 61 6c 2d iData>0 && pWal-
1a1f1 3e 61 70 57 69 44 61 74 61 5b 30 5d 20 29 3b 0a >apWiData[0] );.
1a1f2 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 68 . /* Read the h
1a1f3 65 61 64 65 72 2e 20 54 68 69 73 20 6d 69 67 68 eader. This migh
1a1f4 74 20 68 61 70 70 65 6e 20 63 6f 6e 63 75 72 72 t happen concurr
1a1f5 65 6e 74 6c 79 20 77 69 74 68 20 61 20 77 72 69 ently with a wri
1a1f6 74 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73 te to the. ** s
1a1f7 61 6d 65 20 61 72 65 61 20 6f 66 20 73 68 61 72 ame area of shar
1a1f8 65 64 20 6d 65 6d 6f 72 79 20 6f 6e 20 61 20 64 ed memory on a d
1a1f9 69 66 66 65 72 65 6e 74 20 43 50 55 20 69 6e 20 ifferent CPU in
1a1fa 61 20 53 4d 50 2c 0a 20 20 2a 2a 20 6d 65 61 6e a SMP,. ** mean
1a1fb 69 6e 67 20 69 74 20 69 73 20 70 6f 73 73 69 62 ing it is possib
1a1fc 6c 65 20 74 68 61 74 20 61 6e 20 69 6e 63 6f 6e le that an incon
1a1fd 73 69 73 74 65 6e 74 20 73 6e 61 70 73 68 6f 74 sistent snapshot
1a1fe 20 69 73 20 72 65 61 64 0a 20 20 2a 2a 20 66 72 is read. ** fr
1a1ff 6f 6d 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 om the file. If
1a200 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20 72 65 this happens, re
1a201 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 20 turn non-zero..
1a202 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 72 65 20 61 **. ** There a
1a203 72 65 20 74 77 6f 20 63 6f 70 69 65 73 20 6f 66 re two copies of
1a204 20 74 68 65 20 68 65 61 64 65 72 20 61 74 20 74 the header at t
1a205 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
1a206 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 2e 0a 20 the wal-index..
1a207 20 2a 2a 20 57 68 65 6e 20 72 65 61 64 69 6e 67 ** When reading
1a208 2c 20 72 65 61 64 20 5b 30 5d 20 66 69 72 73 74 , read [0] first
1a209 20 74 68 65 6e 20 5b 31 5d 2e 20 20 57 72 69 74 then [1]. Writ
1a20a 65 73 20 61 72 65 20 69 6e 20 74 68 65 20 72 65 es are in the re
1a20b 76 65 72 73 65 20 6f 72 64 65 72 2e 0a 20 20 2a verse order.. *
1a20c 2a 20 4d 65 6d 6f 72 79 20 62 61 72 72 69 65 72 * Memory barrier
1a20d 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 70 72 s are used to pr
1a20e 65 76 65 6e 74 20 74 68 65 20 63 6f 6d 70 69 6c event the compil
1a20f 65 72 20 6f 72 20 74 68 65 20 68 61 72 64 77 61 er or the hardwa
1a210 72 65 20 66 72 6f 6d 0a 20 20 2a 2a 20 72 65 6f re from. ** reo
1a211 72 64 65 72 69 6e 67 20 74 68 65 20 72 65 61 64 rdering the read
1a212 73 20 61 6e 64 20 77 72 69 74 65 73 2e 0a 20 20 s and writes..
1a213 2a 2f 0a 20 20 61 48 64 72 20 3d 20 77 61 6c 49 */. aHdr = walI
1a214 6e 64 65 78 48 64 72 28 70 57 61 6c 29 3b 0a 20 ndexHdr(pWal);.
1a215 20 6d 65 6d 63 70 79 28 26 68 31 2c 20 28 76 6f memcpy(&h1, (vo
1a216 69 64 20 2a 29 26 61 48 64 72 5b 30 5d 2c 20 73 id *)&aHdr[0], s
1a217 69 7a 65 6f 66 28 68 31 29 29 3b 0a 20 20 77 61 izeof(h1));. wa
1a218 6c 53 68 6d 42 61 72 72 69 65 72 28 70 57 61 6c lShmBarrier(pWal
1a219 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 68 32 2c );. memcpy(&h2,
1a21a 20 28 76 6f 69 64 20 2a 29 26 61 48 64 72 5b 31 (void *)&aHdr[1
1a21b 5d 2c 20 73 69 7a 65 6f 66 28 68 32 29 29 3b 0a ], sizeof(h2));.
1a21c 0a 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 26 68 . if( memcmp(&h
1a21d 31 2c 20 26 68 32 2c 20 73 69 7a 65 6f 66 28 68 1, &h2, sizeof(h
1a21e 31 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 1))!=0 ){. re
1a21f 74 75 72 6e 20 31 3b 20 20 20 2f 2a 20 44 69 72 turn 1; /* Dir
1a220 74 79 20 72 65 61 64 20 2a 2f 0a 20 20 7d 20 20 ty read */. }
1a221 0a 20 20 69 66 28 20 68 31 2e 69 73 49 6e 69 74 . if( h1.isInit
1a222 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
1a223 6e 20 31 3b 20 20 20 2f 2a 20 4d 61 6c 66 6f 72 n 1; /* Malfor
1a224 6d 65 64 20 68 65 61 64 65 72 20 2d 20 70 72 6f med header - pro
1a225 62 61 62 6c 79 20 61 6c 6c 20 7a 65 72 6f 73 20 bably all zeros
1a226 2a 2f 0a 20 20 7d 0a 20 20 77 61 6c 43 68 65 63 */. }. walChec
1a227 6b 73 75 6d 42 79 74 65 73 28 31 2c 20 28 75 38 ksumBytes(1, (u8
1a228 2a 29 26 68 31 2c 20 73 69 7a 65 6f 66 28 68 31 *)&h1, sizeof(h1
1a229 29 2d 73 69 7a 65 6f 66 28 68 31 2e 61 43 6b 73 )-sizeof(h1.aCks
1a22a 75 6d 29 2c 20 30 2c 20 61 43 6b 73 75 6d 29 3b um), 0, aCksum);
1a22b 0a 20 20 69 66 28 20 61 43 6b 73 75 6d 5b 30 5d . if( aCksum[0]
1a22c 21 3d 68 31 2e 61 43 6b 73 75 6d 5b 30 5d 20 7c !=h1.aCksum[0] |
1a22d 7c 20 61 43 6b 73 75 6d 5b 31 5d 21 3d 68 31 2e | aCksum[1]!=h1.
1a22e 61 43 6b 73 75 6d 5b 31 5d 20 29 7b 0a 20 20 20 aCksum[1] ){.
1a22f 20 72 65 74 75 72 6e 20 31 3b 20 20 20 2f 2a 20 return 1; /*
1a230 43 68 65 63 6b 73 75 6d 20 64 6f 65 73 20 6e 6f Checksum does no
1a231 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 7d 0a 0a t match */. }..
1a232 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 26 70 57 if( memcmp(&pW
1a233 61 6c 2d 3e 68 64 72 2c 20 26 68 31 2c 20 73 69 al->hdr, &h1, si
1a234 7a 65 6f 66 28 57 61 6c 49 6e 64 65 78 48 64 72 zeof(WalIndexHdr
1a235 29 29 20 29 7b 0a 20 20 20 20 2a 70 43 68 61 6e )) ){. *pChan
1a236 67 65 64 20 3d 20 31 3b 0a 20 20 20 20 6d 65 6d ged = 1;. mem
1a237 63 70 79 28 26 70 57 61 6c 2d 3e 68 64 72 2c 20 cpy(&pWal->hdr,
1a238 26 68 31 2c 20 73 69 7a 65 6f 66 28 57 61 6c 49 &h1, sizeof(WalI
1a239 6e 64 65 78 48 64 72 29 29 3b 0a 20 20 20 20 70 ndexHdr));. p
1a23a 57 61 6c 2d 3e 73 7a 50 61 67 65 20 3d 20 28 70 Wal->szPage = (p
1a23b 57 61 6c 2d 3e 68 64 72 2e 73 7a 50 61 67 65 26 Wal->hdr.szPage&
1a23c 30 78 66 65 30 30 29 20 2b 20 28 28 70 57 61 6c 0xfe00) + ((pWal
1a23d 2d 3e 68 64 72 2e 73 7a 50 61 67 65 26 30 78 30 ->hdr.szPage&0x0
1a23e 30 30 31 29 3c 3c 31 36 29 3b 0a 20 20 20 20 74 001)<<16);. t
1a23f 65 73 74 63 61 73 65 28 20 70 57 61 6c 2d 3e 73 estcase( pWal->s
1a240 7a 50 61 67 65 3c 3d 33 32 37 36 38 20 29 3b 0a zPage<=32768 );.
1a241 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 57 testcase( pW
1a242 61 6c 2d 3e 73 7a 50 61 67 65 3e 3d 36 35 35 33 al->szPage>=6553
1a243 36 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6 );. }.. /* T
1a244 68 65 20 68 65 61 64 65 72 20 77 61 73 20 73 75 he header was su
1a245 63 63 65 73 73 66 75 6c 6c 79 20 72 65 61 64 2e ccessfully read.
1a246 20 52 65 74 75 72 6e 20 7a 65 72 6f 2e 20 2a 2f Return zero. */
1a247 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
1a248 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 77 /*.** Read the w
1a249 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20 al-index header
1a24a 66 72 6f 6d 20 74 68 65 20 77 61 6c 2d 69 6e 64 from the wal-ind
1a24b 65 78 20 61 6e 64 20 69 6e 74 6f 20 70 57 61 6c ex and into pWal
1a24c 2d 3e 68 64 72 2e 0a 2a 2a 20 49 66 20 74 68 65 ->hdr..** If the
1a24d 20 77 61 6c 2d 68 65 61 64 65 72 20 61 70 70 65 wal-header appe
1a24e 61 72 73 20 74 6f 20 62 65 20 63 6f 72 72 75 70 ars to be corrup
1a24f 74 2c 20 74 72 79 20 74 6f 20 72 65 63 6f 6e 73 t, try to recons
1a250 74 72 75 63 74 20 74 68 65 0a 2a 2a 20 77 61 6c truct the.** wal
1a251 2d 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 -index from the
1a252 57 41 4c 20 62 65 66 6f 72 65 20 72 65 74 75 72 WAL before retur
1a253 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 20 ning..**.** Set
1a254 2a 70 43 68 61 6e 67 65 64 20 74 6f 20 31 20 69 *pChanged to 1 i
1a255 66 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 f the wal-index
1a256 68 65 61 64 65 72 20 76 61 6c 75 65 20 69 6e 20 header value in
1a257 70 57 61 6c 2d 3e 68 64 72 20 69 73 0a 2a 2a 20 pWal->hdr is.**
1a258 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 changed by this
1a259 6f 70 65 72 74 69 6f 6e 2e 20 20 49 66 20 70 57 opertion. If pW
1a25a 61 6c 2d 3e 68 64 72 20 69 73 20 75 6e 63 68 61 al->hdr is uncha
1a25b 6e 67 65 64 2c 20 73 65 74 20 2a 70 43 68 61 6e nged, set *pChan
1a25c 67 65 64 0a 2a 2a 20 74 6f 20 30 2e 0a 2a 2a 0a ged.** to 0..**.
1a25d 2a 2a 20 49 66 20 74 68 65 20 77 61 6c 2d 69 6e ** If the wal-in
1a25e 64 65 78 20 68 65 61 64 65 72 20 69 73 20 73 75 dex header is su
1a25f 63 63 65 73 73 66 75 6c 6c 79 20 72 65 61 64 2c ccessfully read,
1a260 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1a261 4b 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 K. .** Otherwise
1a262 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 an SQLite error
1a263 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 code..*/.static
1a264 20 69 6e 74 20 77 61 6c 49 6e 64 65 78 52 65 61 int walIndexRea
1a265 64 48 64 72 28 57 61 6c 20 2a 70 57 61 6c 2c 20 dHdr(Wal *pWal,
1a266 69 6e 74 20 2a 70 43 68 61 6e 67 65 64 29 7b 0a int *pChanged){.
1a267 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
1a268 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a269 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
1a26a 20 2a 2f 0a 20 20 69 6e 74 20 62 61 64 48 64 72 */. int badHdr
1a26b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1a26c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
1a26d 20 61 20 68 65 61 64 65 72 20 72 65 61 64 20 66 a header read f
1a26e 61 69 6c 65 64 20 2a 2f 0a 20 20 76 6f 6c 61 74 ailed */. volat
1a26f 69 6c 65 20 75 33 32 20 2a 70 61 67 65 30 3b 20 ile u32 *page0;
1a270 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 /* Ch
1a271 75 6e 6b 20 6f 66 20 77 61 6c 2d 69 6e 64 65 78 unk of wal-index
1a272 20 63 6f 6e 74 61 69 6e 69 6e 67 20 68 65 61 64 containing head
1a273 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 45 6e 73 75 er */.. /* Ensu
1a274 72 65 20 74 68 61 74 20 70 61 67 65 20 30 20 6f re that page 0 o
1a275 66 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 f the wal-index
1a276 28 74 68 65 20 70 61 67 65 20 74 68 61 74 20 63 (the page that c
1a277 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a 20 20 2a ontains the . *
1a278 2a 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 * wal-index head
1a279 65 72 29 20 69 73 20 6d 61 70 70 65 64 2e 20 52 er) is mapped. R
1a27a 65 74 75 72 6e 20 65 61 72 6c 79 20 69 66 20 61 eturn early if a
1a27b 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 68 n error occurs h
1a27c 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ere.. */. asse
1a27d 72 74 28 20 70 43 68 61 6e 67 65 64 20 29 3b 0a rt( pChanged );.
1a27e 20 20 72 63 20 3d 20 77 61 6c 49 6e 64 65 78 50 rc = walIndexP
1a27f 61 67 65 28 70 57 61 6c 2c 20 30 2c 20 26 70 61 age(pWal, 0, &pa
1a280 67 65 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d ge0);. if( rc!=
1a281 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1a282 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 3b return rc;. };
1a283 0a 20 20 61 73 73 65 72 74 28 20 70 61 67 65 30 . assert( page0
1a284 20 7c 7c 20 70 57 61 6c 2d 3e 77 72 69 74 65 4c || pWal->writeL
1a285 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 ock==0 );.. /*
1a286 49 66 20 74 68 65 20 66 69 72 73 74 20 70 61 67 If the first pag
1a287 65 20 6f 66 20 74 68 65 20 77 61 6c 2d 69 6e 64 e of the wal-ind
1a288 65 78 20 68 61 73 20 62 65 65 6e 20 6d 61 70 70 ex has been mapp
1a289 65 64 2c 20 74 72 79 20 74 6f 20 72 65 61 64 20 ed, try to read
1a28a 74 68 65 0a 20 20 2a 2a 20 77 61 6c 2d 69 6e 64 the. ** wal-ind
1a28b 65 78 20 68 65 61 64 65 72 20 69 6d 6d 65 64 69 ex header immedi
1a28c 61 74 65 6c 79 2c 20 77 69 74 68 6f 75 74 20 68 ately, without h
1a28d 6f 6c 64 69 6e 67 20 61 6e 79 20 6c 6f 63 6b 2e olding any lock.
1a28e 20 54 68 69 73 20 75 73 75 61 6c 6c 79 0a 20 20 This usually.
1a28f 2a 2a 20 77 6f 72 6b 73 2c 20 62 75 74 20 6d 61 ** works, but ma
1a290 79 20 66 61 69 6c 20 69 66 20 74 68 65 20 77 61 y fail if the wa
1a291 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20 69 l-index header i
1a292 73 20 63 6f 72 72 75 70 74 20 6f 72 20 63 75 72 s corrupt or cur
1a293 72 65 6e 74 6c 79 20 0a 20 20 2a 2a 20 62 65 69 rently . ** bei
1a294 6e 67 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 ng modified by a
1a295 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 6f 72 nother thread or
1a296 20 70 72 6f 63 65 73 73 2e 0a 20 20 2a 2f 0a 20 process.. */.
1a297 20 62 61 64 48 64 72 20 3d 20 28 70 61 67 65 30 badHdr = (page0
1a298 20 3f 20 77 61 6c 49 6e 64 65 78 54 72 79 48 64 ? walIndexTryHd
1a299 72 28 70 57 61 6c 2c 20 70 43 68 61 6e 67 65 64 r(pWal, pChanged
1a29a 29 20 3a 20 31 29 3b 0a 0a 20 20 2f 2a 20 49 66 ) : 1);.. /* If
1a29b 20 74 68 65 20 66 69 72 73 74 20 61 74 74 65 6d the first attem
1a29c 70 74 20 66 61 69 6c 65 64 2c 20 69 74 20 6d 69 pt failed, it mi
1a29d 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 64 75 ght have been du
1a29e 65 20 74 6f 20 61 20 72 61 63 65 0a 20 20 2a 2a e to a race. **
1a29f 20 77 69 74 68 20 61 20 77 72 69 74 65 72 2e 20 with a writer.
1a2a0 20 53 6f 20 67 65 74 20 61 20 57 52 49 54 45 20 So get a WRITE
1a2a1 6c 6f 63 6b 20 61 6e 64 20 74 72 79 20 61 67 61 lock and try aga
1a2a2 69 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 in.. */. asser
1a2a3 74 28 20 62 61 64 48 64 72 3d 3d 30 20 7c 7c 20 t( badHdr==0 ||
1a2a4 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 3d pWal->writeLock=
1a2a5 3d 30 20 29 3b 0a 20 20 69 66 28 20 62 61 64 48 =0 );. if( badH
1a2a6 64 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 57 dr ){. if( pW
1a2a7 61 6c 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 20 57 al->readOnly & W
1a2a8 41 4c 5f 53 48 4d 5f 52 44 4f 4e 4c 59 20 29 7b AL_SHM_RDONLY ){
1a2a9 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 . if( SQLIT
1a2aa 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 77 61 6c 4c E_OK==(rc = walL
1a2ab 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c 2c 20 ockShared(pWal,
1a2ac 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43 4b 29 29 WAL_WRITE_LOCK))
1a2ad 20 29 7b 0a 20 20 20 20 20 20 20 20 77 61 6c 55 ){. walU
1a2ae 6e 6c 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c nlockShared(pWal
1a2af 2c 20 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43 4b , WAL_WRITE_LOCK
1a2b0 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
1a2b1 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5f SQLITE_READONLY_
1a2b2 52 45 43 4f 56 45 52 59 3b 0a 20 20 20 20 20 20 RECOVERY;.
1a2b3 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
1a2b4 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d SQLITE_OK==(rc =
1a2b5 20 77 61 6c 4c 6f 63 6b 45 78 63 6c 75 73 69 76 walLockExclusiv
1a2b6 65 28 70 57 61 6c 2c 20 57 41 4c 5f 57 52 49 54 e(pWal, WAL_WRIT
1a2b7 45 5f 4c 4f 43 4b 2c 20 31 29 29 20 29 7b 0a 20 E_LOCK, 1)) ){.
1a2b8 20 20 20 20 20 70 57 61 6c 2d 3e 77 72 69 74 65 pWal->write
1a2b9 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 Lock = 1;.
1a2ba 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 if( SQLITE_OK==(
1a2bb 72 63 20 3d 20 77 61 6c 49 6e 64 65 78 50 61 67 rc = walIndexPag
1a2bc 65 28 70 57 61 6c 2c 20 30 2c 20 26 70 61 67 65 e(pWal, 0, &page
1a2bd 30 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62 0)) ){. b
1a2be 61 64 48 64 72 20 3d 20 77 61 6c 49 6e 64 65 78 adHdr = walIndex
1a2bf 54 72 79 48 64 72 28 70 57 61 6c 2c 20 70 43 68 TryHdr(pWal, pCh
1a2c0 61 6e 67 65 64 29 3b 0a 20 20 20 20 20 20 20 20 anged);.
1a2c1 69 66 28 20 62 61 64 48 64 72 20 29 7b 0a 20 20 if( badHdr ){.
1a2c2 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
1a2c3 65 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 e wal-index head
1a2c4 65 72 20 69 73 20 73 74 69 6c 6c 20 6d 61 6c 66 er is still malf
1a2c5 6f 72 6d 65 64 20 65 76 65 6e 20 77 68 69 6c 65 ormed even while
1a2c6 20 68 6f 6c 64 69 6e 67 0a 20 20 20 20 20 20 20 holding.
1a2c7 20 20 20 2a 2a 20 61 20 57 52 49 54 45 20 6c 6f ** a WRITE lo
1a2c8 63 6b 2c 20 69 74 20 63 61 6e 20 6f 6e 6c 79 20 ck, it can only
1a2c9 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 68 65 mean that the he
1a2ca 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 74 65 ader is corrupte
1a2cb 64 20 61 6e 64 0a 20 20 20 20 20 20 20 20 20 20 d and.
1a2cc 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 ** needs to be r
1a2cd 65 63 6f 6e 73 74 72 75 63 74 65 64 2e 20 20 53 econstructed. S
1a2ce 6f 20 72 75 6e 20 72 65 63 6f 76 65 72 79 20 74 o run recovery t
1a2cf 6f 20 64 6f 20 65 78 61 63 74 6c 79 20 74 68 61 o do exactly tha
1a2d0 74 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a t.. */.
1a2d1 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 rc = w
1a2d2 61 6c 49 6e 64 65 78 52 65 63 6f 76 65 72 28 70 alIndexRecover(p
1a2d3 57 61 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Wal);.
1a2d4 2a 70 43 68 61 6e 67 65 64 20 3d 20 31 3b 0a 20 *pChanged = 1;.
1a2d5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1a2d6 0a 20 20 20 20 20 20 70 57 61 6c 2d 3e 77 72 69 . pWal->wri
1a2d7 74 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 teLock = 0;.
1a2d8 20 20 77 61 6c 55 6e 6c 6f 63 6b 45 78 63 6c 75 walUnlockExclu
1a2d9 73 69 76 65 28 70 57 61 6c 2c 20 57 41 4c 5f 57 sive(pWal, WAL_W
1a2da 52 49 54 45 5f 4c 4f 43 4b 2c 20 31 29 3b 0a 20 RITE_LOCK, 1);.
1a2db 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
1a2dc 66 20 74 68 65 20 68 65 61 64 65 72 20 69 73 20 f the header is
1a2dd 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c read successfull
1a2de 79 2c 20 63 68 65 63 6b 20 74 68 65 20 76 65 72 y, check the ver
1a2df 73 69 6f 6e 20 6e 75 6d 62 65 72 20 74 6f 20 6d sion number to m
1a2e0 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20 74 68 ake. ** sure th
1a2e1 65 20 77 61 6c 2d 69 6e 64 65 78 20 77 61 73 20 e wal-index was
1a2e2 6e 6f 74 20 63 6f 6e 73 74 72 75 63 74 65 64 20 not constructed
1a2e3 77 69 74 68 20 73 6f 6d 65 20 66 75 74 75 72 65 with some future
1a2e4 20 66 6f 72 6d 61 74 20 74 68 61 74 0a 20 20 2a format that. *
1a2e5 2a 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * this version o
1a2e6 66 20 53 51 4c 69 74 65 20 63 61 6e 6e 6f 74 20 f SQLite cannot
1a2e7 75 6e 64 65 72 73 74 61 6e 64 2e 0a 20 20 2a 2f understand.. */
1a2e8 0a 20 20 69 66 28 20 62 61 64 48 64 72 3d 3d 30 . if( badHdr==0
1a2e9 20 26 26 20 70 57 61 6c 2d 3e 68 64 72 2e 69 56 && pWal->hdr.iV
1a2ea 65 72 73 69 6f 6e 21 3d 57 41 4c 49 4e 44 45 58 ersion!=WALINDEX
1a2eb 5f 4d 41 58 5f 56 45 52 53 49 4f 4e 20 29 7b 0a _MAX_VERSION ){.
1a2ec 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1a2ed 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 3b 0a 20 CANTOPEN_BKPT;.
1a2ee 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }.. return rc;
1a2ef 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 .}../*.** This i
1a2f0 73 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 s the value that
1a2f1 20 77 61 6c 54 72 79 42 65 67 69 6e 52 65 61 64 walTryBeginRead
1a2f2 20 72 65 74 75 72 6e 73 20 77 68 65 6e 20 69 74 returns when it
1a2f3 20 6e 65 65 64 73 20 74 6f 0a 2a 2a 20 62 65 20 needs to.** be
1a2f4 72 65 74 72 69 65 64 2e 0a 2a 2f 0a 23 64 65 66 retried..*/.#def
1a2f5 69 6e 65 20 57 41 4c 5f 52 45 54 52 59 20 20 28 ine WAL_RETRY (
1a2f6 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d -1)../*.** Attem
1a2f7 70 74 20 74 6f 20 73 74 61 72 74 20 61 20 72 65 pt to start a re
1a2f8 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ad transaction.
1a2f9 20 54 68 69 73 20 6d 69 67 68 74 20 66 61 69 6c This might fail
1a2fa 20 64 75 65 20 74 6f 20 61 20 72 61 63 65 20 6f due to a race o
1a2fb 72 0a 2a 2a 20 6f 74 68 65 72 20 74 72 61 6e 73 r.** other trans
1a2fc 69 65 6e 74 20 63 6f 6e 64 69 74 69 6f 6e 2e 20 ient condition.
1a2fd 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 When that happe
1a2fe 6e 73 2c 20 69 74 20 72 65 74 75 72 6e 73 20 57 ns, it returns W
1a2ff 41 4c 5f 52 45 54 52 59 20 74 6f 0a 2a 2a 20 69 AL_RETRY to.** i
1a300 6e 64 69 63 61 74 65 20 74 6f 20 74 68 65 20 63 ndicate to the c
1a301 61 6c 6c 65 72 20 74 68 61 74 20 69 74 20 69 73 aller that it is
1a302 20 73 61 66 65 20 74 6f 20 72 65 74 72 79 20 69 safe to retry i
1a303 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a mmediately..**.*
1a304 2a 20 4f 6e 20 73 75 63 63 65 73 73 20 72 65 74 * On success ret
1a305 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 urn SQLITE_OK.
1a306 4f 6e 20 61 20 70 65 72 6d 61 6e 65 6e 74 20 66 On a permanent f
1a307 61 69 6c 75 72 65 20 28 73 75 63 68 20 61 6e 0a ailure (such an.
1a308 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6f 72 20 ** I/O error or
1a309 61 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 62 an SQLITE_BUSY b
1a30a 65 63 61 75 73 65 20 61 6e 6f 74 68 65 72 20 70 ecause another p
1a30b 72 6f 63 65 73 73 20 69 73 20 72 75 6e 6e 69 6e rocess is runnin
1a30c 67 0a 2a 2a 20 72 65 63 6f 76 65 72 79 29 20 72 g.** recovery) r
1a30d 65 74 75 72 6e 20 61 20 70 6f 73 69 74 69 76 65 eturn a positive
1a30e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a error code..**.
1a30f 2a 2a 20 54 68 65 20 75 73 65 57 61 6c 20 70 61 ** The useWal pa
1a310 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 20 rameter is true
1a311 74 6f 20 66 6f 72 63 65 20 74 68 65 20 75 73 65 to force the use
1a312 20 6f 66 20 74 68 65 20 57 41 4c 20 61 6e 64 20 of the WAL and
1a313 64 69 73 61 62 6c 65 0a 2a 2a 20 74 68 65 20 63 disable.** the c
1a314 61 73 65 20 77 68 65 72 65 20 74 68 65 20 57 41 ase where the WA
1a315 4c 20 69 73 20 62 79 70 61 73 73 65 64 20 62 65 L is bypassed be
1a316 63 61 75 73 65 20 69 74 20 68 61 73 20 62 65 65 cause it has bee
1a317 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 n completely.**
1a318 63 68 65 63 6b 70 6f 69 6e 74 65 64 2e 20 20 49 checkpointed. I
1a319 66 20 75 73 65 57 61 6c 3d 3d 30 20 74 68 65 6e f useWal==0 then
1a31a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 this routine ca
1a31b 6c 6c 73 20 77 61 6c 49 6e 64 65 78 52 65 61 64 lls walIndexRead
1a31c 48 64 72 28 29 20 0a 2a 2a 20 74 6f 20 6d 61 6b Hdr() .** to mak
1a31d 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 e a copy of the
1a31e 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 wal-index header
1a31f 20 69 6e 74 6f 20 70 57 61 6c 2d 3e 68 64 72 2e into pWal->hdr.
1a320 20 20 49 66 20 74 68 65 20 0a 2a 2a 20 77 61 6c If the .** wal
1a321 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20 68 61 -index header ha
1a322 73 20 63 68 61 6e 67 65 64 2c 20 2a 70 43 68 61 s changed, *pCha
1a323 6e 67 65 64 20 69 73 20 73 65 74 20 74 6f 20 31 nged is set to 1
1a324 20 28 61 73 20 61 6e 20 69 6e 64 69 63 61 74 69 (as an indicati
1a325 6f 6e 20 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 on .** to the ca
1a326 6c 6c 65 72 20 74 68 61 74 20 74 68 65 20 6c 6f ller that the lo
1a327 63 61 6c 20 70 61 67 65 74 20 63 61 63 68 65 20 cal paget cache
1a328 69 73 20 6f 62 73 6f 6c 65 74 65 20 61 6e 64 20 is obsolete and
1a329 6e 65 65 64 73 20 74 6f 20 62 65 20 0a 2a 2a 20 needs to be .**
1a32a 66 6c 75 73 68 65 64 2e 29 20 20 57 68 65 6e 20 flushed.) When
1a32b 75 73 65 57 61 6c 3d 3d 31 2c 20 74 68 65 20 77 useWal==1, the w
1a32c 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20 al-index header
1a32d 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 61 6c is assumed to al
1a32e 72 65 61 64 79 0a 2a 2a 20 62 65 20 6c 6f 61 64 ready.** be load
1a32f 65 64 20 61 6e 64 20 74 68 65 20 70 43 68 61 6e ed and the pChan
1a330 67 65 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 ged parameter is
1a331 20 75 6e 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 unused..**.** T
1a332 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 73 he caller must s
1a333 65 74 20 74 68 65 20 63 6e 74 20 70 61 72 61 6d et the cnt param
1a334 65 74 65 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 eter to the numb
1a335 65 72 20 6f 66 20 70 72 69 6f 72 20 63 61 6c 6c er of prior call
1a336 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 s to.** this rou
1a337 74 69 6e 65 20 64 75 72 69 6e 67 20 74 68 65 20 tine during the
1a338 63 75 72 72 65 6e 74 20 72 65 61 64 20 61 74 74 current read att
1a339 65 6d 70 74 20 74 68 61 74 20 72 65 74 75 72 6e empt that return
1a33a 65 64 20 57 41 4c 5f 52 45 54 52 59 2e 0a 2a 2a ed WAL_RETRY..**
1a33b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 This routine wi
1a33c 6c 6c 20 73 74 61 72 74 20 74 61 6b 69 6e 67 20 ll start taking
1a33d 6d 6f 72 65 20 61 67 67 72 65 73 73 69 76 65 20 more aggressive
1a33e 6d 65 61 73 75 72 65 73 20 74 6f 20 63 6c 65 61 measures to clea
1a33f 72 20 74 68 65 0a 2a 2a 20 72 61 63 65 20 63 6f r the.** race co
1a340 6e 64 69 74 69 6f 6e 73 20 61 66 74 65 72 20 6d nditions after m
1a341 75 6c 74 69 70 6c 65 20 57 41 4c 5f 52 45 54 52 ultiple WAL_RETR
1a342 59 20 72 65 74 75 72 6e 73 2c 20 61 6e 64 20 61 Y returns, and a
1a343 66 74 65 72 20 61 6e 20 65 78 63 65 73 73 69 76 fter an excessiv
1a344 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65 e.** number of e
1a345 72 72 6f 72 73 20 77 69 6c 6c 20 75 6c 74 69 6d rrors will ultim
1a346 61 74 65 6c 79 20 72 65 74 75 72 6e 20 53 51 4c ately return SQL
1a347 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 2e 20 20 54 ITE_PROTOCOL. T
1a348 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 4f he.** SQLITE_PRO
1a349 54 4f 43 4f 4c 20 72 65 74 75 72 6e 20 69 6e 64 TOCOL return ind
1a34a 69 63 61 74 65 73 20 74 68 61 74 20 73 6f 6d 65 icates that some
1a34b 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 other process h
1a34c 61 73 20 67 6f 6e 65 20 72 6f 67 75 65 0a 2a 2a as gone rogue.**
1a34d 20 61 6e 64 20 69 73 20 6e 6f 74 20 68 6f 6e 6f and is not hono
1a34e 72 69 6e 67 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ring the locking
1a34f 20 70 72 6f 74 6f 63 6f 6c 2e 20 20 54 68 65 72 protocol. Ther
1a350 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67 e is a vanishing
1a351 6c 79 20 73 6d 61 6c 6c 0a 2a 2a 20 63 68 61 6e ly small.** chan
1a352 63 65 20 74 68 61 74 20 53 51 4c 49 54 45 5f 50 ce that SQLITE_P
1a353 52 4f 54 4f 43 4f 4c 20 63 6f 75 6c 64 20 62 65 ROTOCOL could be
1a354 20 72 65 74 75 72 6e 65 64 20 62 65 63 61 75 73 returned becaus
1a355 65 20 6f 66 20 61 20 72 75 6e 20 6f 66 20 72 65 e of a run of re
1a356 61 6c 6c 79 0a 2a 2a 20 62 61 64 20 6c 75 63 6b ally.** bad luck
1a357 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 6c when there is l
1a358 6f 74 73 20 6f 66 20 63 6f 6e 74 65 6e 74 69 6f ots of contentio
1a359 6e 20 66 6f 72 20 74 68 65 20 77 61 6c 2d 69 6e n for the wal-in
1a35a 64 65 78 2c 20 62 75 74 20 74 68 61 74 0a 2a 2a dex, but that.**
1a35b 20 70 6f 73 73 69 62 69 6c 69 74 79 20 69 73 20 possibility is
1a35c 73 6f 20 73 6d 61 6c 6c 20 74 68 61 74 20 69 74 so small that it
1a35d 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79 20 6e can be safely n
1a35e 65 67 6c 65 63 74 65 64 2c 20 77 65 20 62 65 6c eglected, we bel
1a35f 69 65 76 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 ieve..**.** On s
1a360 75 63 63 65 73 73 2c 20 74 68 69 73 20 72 6f 75 uccess, this rou
1a361 74 69 6e 65 20 6f 62 74 61 69 6e 73 20 61 20 72 tine obtains a r
1a362 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 0a 2a 2a 20 ead lock on .**
1a363 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 70 57 WAL_READ_LOCK(pW
1a364 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 29 2e 20 20 al->readLock).
1a365 54 68 65 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f The pWal->readLo
1a366 63 6b 20 69 6e 74 65 67 65 72 20 69 73 0a 2a 2a ck integer is.**
1a367 20 69 6e 20 74 68 65 20 72 61 6e 67 65 20 30 20 in the range 0
1a368 3c 3d 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 <= pWal->readLoc
1a369 6b 20 3c 20 57 41 4c 5f 4e 52 45 41 44 45 52 2e k < WAL_NREADER.
1a36a 20 20 49 66 20 70 57 61 6c 2d 3e 72 65 61 64 4c If pWal->readL
1a36b 6f 63 6b 3d 3d 28 2d 31 29 0a 2a 2a 20 74 68 61 ock==(-1).** tha
1a36c 74 20 6d 65 61 6e 73 20 74 68 65 20 57 61 6c 20 t means the Wal
1a36d 64 6f 65 73 20 6e 6f 74 20 68 6f 6c 64 20 61 6e does not hold an
1a36e 79 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 54 68 y read lock. Th
1a36f 65 20 72 65 61 64 65 72 20 6d 75 73 74 20 6e 6f e reader must no
1a370 74 0a 2a 2a 20 61 63 63 65 73 73 20 61 6e 79 20 t.** access any
1a371 64 61 74 61 62 61 73 65 20 70 61 67 65 20 74 68 database page th
1a372 61 74 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 at is modified b
1a373 79 20 61 20 57 41 4c 20 66 72 61 6d 65 20 75 70 y a WAL frame up
1a374 20 74 6f 20 61 6e 64 0a 2a 2a 20 69 6e 63 6c 75 to and.** inclu
1a375 64 69 6e 67 20 66 72 61 6d 65 20 6e 75 6d 62 65 ding frame numbe
1a376 72 20 61 52 65 61 64 4d 61 72 6b 5b 70 57 61 6c r aReadMark[pWal
1a377 2d 3e 72 65 61 64 4c 6f 63 6b 5d 2e 20 20 54 68 ->readLock]. Th
1a378 65 20 72 65 61 64 65 72 20 77 69 6c 6c 0a 2a 2a e reader will.**
1a379 20 75 73 65 20 57 41 4c 20 66 72 61 6d 65 73 20 use WAL frames
1a37a 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64 up to and includ
1a37b 69 6e 67 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 ing pWal->hdr.mx
1a37c 46 72 61 6d 65 20 69 66 20 70 57 61 6c 2d 3e 72 Frame if pWal->r
1a37d 65 61 64 4c 6f 63 6b 3e 30 0a 2a 2a 20 4f 72 20 eadLock>0.** Or
1a37e 69 66 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 if pWal->readLoc
1a37f 6b 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 72 k==0, then the r
1a380 65 61 64 65 72 20 77 69 6c 6c 20 69 67 6e 6f 72 eader will ignor
1a381 65 20 74 68 65 20 57 41 4c 0a 2a 2a 20 63 6f 6d e the WAL.** com
1a382 70 6c 65 74 65 6c 79 20 61 6e 64 20 67 65 74 20 pletely and get
1a383 61 6c 6c 20 63 6f 6e 74 65 6e 74 20 64 69 72 65 all content dire
1a384 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 64 61 ctly from the da
1a385 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 tabase file..**
1a386 49 66 20 74 68 65 20 75 73 65 57 61 6c 20 70 61 If the useWal pa
1a387 72 61 6d 65 74 65 72 20 69 73 20 31 20 74 68 65 rameter is 1 the
1a388 6e 20 74 68 65 20 57 41 4c 20 77 69 6c 6c 20 6e n the WAL will n
1a389 65 76 65 72 20 62 65 20 69 67 6e 6f 72 65 64 20 ever be ignored
1a38a 61 6e 64 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 and.** this rout
1a38b 69 6e 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 ine will always
1a38c 73 65 74 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f set pWal->readLo
1a38d 63 6b 3e 30 20 6f 6e 20 73 75 63 63 65 73 73 2e ck>0 on success.
1a38e 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 72 65 61 .** When the rea
1a38f 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 d transaction is
1a390 20 63 6f 6d 70 6c 65 74 65 64 2c 20 74 68 65 20 completed, the
1a391 63 61 6c 6c 65 72 20 6d 75 73 74 20 72 65 6c 65 caller must rele
1a392 61 73 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 ase the.** lock
1a393 6f 6e 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b on WAL_READ_LOCK
1a394 28 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 29 (pWal->readLock)
1a395 20 61 6e 64 20 73 65 74 20 70 57 61 6c 2d 3e 72 and set pWal->r
1a396 65 61 64 4c 6f 63 6b 20 74 6f 20 2d 31 2e 0a 2a eadLock to -1..*
1a397 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1a398 65 20 75 73 65 73 20 74 68 65 20 6e 42 61 63 6b e uses the nBack
1a399 66 69 6c 6c 20 61 6e 64 20 61 52 65 61 64 4d 61 fill and aReadMa
1a39a 72 6b 5b 5d 20 66 69 65 6c 64 73 20 6f 66 20 74 rk[] fields of t
1a39b 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 74 6f 20 he header.** to
1a39c 73 65 6c 65 63 74 20 61 20 70 61 72 74 69 63 75 select a particu
1a39d 6c 61 72 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 lar WAL_READ_LOC
1a39e 4b 28 29 20 74 68 61 74 20 73 74 72 69 76 65 73 K() that strives
1a39f 20 74 6f 20 6c 65 74 20 74 68 65 0a 2a 2a 20 63 to let the.** c
1a3a0 68 65 63 6b 70 6f 69 6e 74 20 70 72 6f 63 65 73 heckpoint proces
1a3a1 73 20 64 6f 20 61 73 20 6d 75 63 68 20 77 6f 72 s do as much wor
1a3a2 6b 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 20 20 k as possible.
1a3a3 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 This routine mig
1a3a4 68 74 0a 2a 2a 20 75 70 64 61 74 65 20 76 61 6c ht.** update val
1a3a5 75 65 73 20 6f 66 20 74 68 65 20 61 52 65 61 64 ues of the aRead
1a3a6 4d 61 72 6b 5b 5d 20 61 72 72 61 79 20 69 6e 20 Mark[] array in
1a3a7 74 68 65 20 68 65 61 64 65 72 2c 20 62 75 74 20 the header, but
1a3a8 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 73 6f if it does.** so
1a3a9 20 69 74 20 74 61 6b 65 73 20 63 61 72 65 20 74 it takes care t
1a3aa 6f 20 68 6f 6c 64 20 61 6e 20 65 78 63 6c 75 73 o hold an exclus
1a3ab 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ive lock on the
1a3ac 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a corresponding.**
1a3ad 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 29 WAL_READ_LOCK()
1a3ae 20 77 68 69 6c 65 20 63 68 61 6e 67 69 6e 67 20 while changing
1a3af 76 61 6c 75 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 values..*/.stati
1a3b0 63 20 69 6e 74 20 77 61 6c 54 72 79 42 65 67 69 c int walTryBegi
1a3b1 6e 52 65 61 64 28 57 61 6c 20 2a 70 57 61 6c 2c nRead(Wal *pWal,
1a3b2 20 69 6e 74 20 2a 70 43 68 61 6e 67 65 64 2c 20 int *pChanged,
1a3b3 69 6e 74 20 75 73 65 57 61 6c 2c 20 69 6e 74 20 int useWal, int
1a3b4 63 6e 74 29 7b 0a 20 20 76 6f 6c 61 74 69 6c 65 cnt){. volatile
1a3b5 20 57 61 6c 43 6b 70 74 49 6e 66 6f 20 2a 70 49 WalCkptInfo *pI
1a3b6 6e 66 6f 3b 20 20 20 20 2f 2a 20 43 68 65 63 6b nfo; /* Check
1a3b7 70 6f 69 6e 74 20 69 6e 66 6f 72 6d 61 74 69 6f point informatio
1a3b8 6e 20 69 6e 20 77 61 6c 2d 69 6e 64 65 78 20 2a n in wal-index *
1a3b9 2f 0a 20 20 75 33 32 20 6d 78 52 65 61 64 4d 61 /. u32 mxReadMa
1a3ba 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rk;
1a3bb 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 61 /* Largest a
1a3bc 52 65 61 64 4d 61 72 6b 5b 5d 20 76 61 6c 75 65 ReadMark[] value
1a3bd 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 49 3b 20 20 */. int mxI;
1a3be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a3bf 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1a3c0 66 20 6c 61 72 67 65 73 74 20 61 52 65 61 64 4d f largest aReadM
1a3c1 61 72 6b 5b 5d 20 76 61 6c 75 65 20 2a 2f 0a 20 ark[] value */.
1a3c2 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
1a3c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a3c4 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
1a3c5 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 */. int rc = S
1a3c6 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 QLITE_OK;
1a3c7 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
1a3c8 63 6f 64 65 20 20 2a 2f 0a 0a 20 20 61 73 73 65 code */.. asse
1a3c9 72 74 28 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f rt( pWal->readLo
1a3ca 63 6b 3c 30 20 29 3b 20 20 20 20 20 2f 2a 20 4e ck<0 ); /* N
1a3cb 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f 63 ot currently loc
1a3cc 6b 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 61 6b ked */.. /* Tak
1a3cd 65 20 73 74 65 70 73 20 74 6f 20 61 76 6f 69 64 e steps to avoid
1a3ce 20 73 70 69 6e 6e 69 6e 67 20 66 6f 72 65 76 65 spinning foreve
1a3cf 72 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 r if there is a
1a3d0 70 72 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 2e 0a protocol error..
1a3d1 20 20 2a 2a 0a 20 20 2a 2a 20 43 69 72 63 75 6d **. ** Circum
1a3d2 73 74 61 6e 63 65 73 20 74 68 61 74 20 63 61 75 stances that cau
1a3d3 73 65 20 61 20 52 45 54 52 59 20 73 68 6f 75 6c se a RETRY shoul
1a3d4 64 20 6f 6e 6c 79 20 6c 61 73 74 20 66 6f 72 20 d only last for
1a3d5 74 68 65 20 62 72 69 65 66 65 73 74 0a 20 20 2a the briefest. *
1a3d6 2a 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 * instances of t
1a3d7 69 6d 65 2e 20 20 4e 6f 20 49 2f 4f 20 6f 72 20 ime. No I/O or
1a3d8 6f 74 68 65 72 20 73 79 73 74 65 6d 20 63 61 6c other system cal
1a3d9 6c 73 20 61 72 65 20 64 6f 6e 65 20 77 68 69 6c ls are done whil
1a3da 65 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 63 6b 73 e the. ** locks
1a3db 20 61 72 65 20 68 65 6c 64 2c 20 73 6f 20 74 68 are held, so th
1a3dc 65 20 6c 6f 63 6b 73 20 73 68 6f 75 6c 64 20 6e e locks should n
1a3dd 6f 74 20 62 65 20 68 65 6c 64 20 66 6f 72 20 76 ot be held for v
1a3de 65 72 79 20 6c 6f 6e 67 2e 20 42 75 74 20 0a 20 ery long. But .
1a3df 20 2a 2a 20 69 66 20 77 65 20 61 72 65 20 75 6e ** if we are un
1a3e0 6c 75 63 6b 79 2c 20 61 6e 6f 74 68 65 72 20 70 lucky, another p
1a3e1 72 6f 63 65 73 73 20 74 68 61 74 20 69 73 20 68 rocess that is h
1a3e2 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b 20 6d 69 olding a lock mi
1a3e3 67 68 74 20 67 65 74 0a 20 20 2a 2a 20 70 61 67 ght get. ** pag
1a3e4 65 64 20 6f 75 74 20 6f 72 20 74 61 6b 65 20 61 ed out or take a
1a3e5 20 70 61 67 65 2d 66 61 75 6c 74 20 74 68 61 74 page-fault that
1a3e6 20 69 73 20 74 69 6d 65 2d 63 6f 6e 73 75 6d 69 is time-consumi
1a3e7 6e 67 20 74 6f 20 72 65 73 6f 6c 76 65 2c 20 0a ng to resolve, .
1a3e8 20 20 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20 ** during the
1a3e9 66 65 77 20 6e 61 6e 6f 73 65 63 6f 6e 64 73 20 few nanoseconds
1a3ea 74 68 61 74 20 69 74 20 69 73 20 68 6f 6c 64 69 that it is holdi
1a3eb 6e 67 20 74 68 65 20 6c 6f 63 6b 2e 20 20 49 6e ng the lock. In
1a3ec 20 74 68 61 74 20 63 61 73 65 2c 0a 20 20 2a 2a that case,. **
1a3ed 20 69 74 20 6d 69 67 68 74 20 74 61 6b 65 20 6c it might take l
1a3ee 6f 6e 67 65 72 20 74 68 61 6e 20 6e 6f 72 6d 61 onger than norma
1a3ef 6c 20 66 6f 72 20 74 68 65 20 6c 6f 63 6b 20 74 l for the lock t
1a3f0 6f 20 66 72 65 65 2e 0a 20 20 2a 2a 0a 20 20 2a o free.. **. *
1a3f1 2a 20 41 66 74 65 72 20 35 20 52 45 54 52 59 73 * After 5 RETRYs
1a3f2 2c 20 77 65 20 62 65 67 69 6e 20 63 61 6c 6c 69 , we begin calli
1a3f3 6e 67 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 ng sqlite3OsSlee
1a3f4 70 28 29 2e 20 20 54 68 65 20 66 69 72 73 74 20 p(). The first
1a3f5 66 65 77 0a 20 20 2a 2a 20 63 61 6c 6c 73 20 74 few. ** calls t
1a3f6 6f 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 o sqlite3OsSleep
1a3f7 28 29 20 68 61 76 65 20 61 20 64 65 6c 61 79 20 () have a delay
1a3f8 6f 66 20 31 20 6d 69 63 72 6f 73 65 63 6f 6e 64 of 1 microsecond
1a3f9 2e 20 20 52 65 61 6c 6c 79 20 74 68 69 73 0a 20 . Really this.
1a3fa 20 2a 2a 20 69 73 20 6d 6f 72 65 20 6f 66 20 61 ** is more of a
1a3fb 20 73 63 68 65 64 75 6c 65 72 20 79 69 65 6c 64 scheduler yield
1a3fc 20 74 68 61 6e 20 61 6e 20 61 63 74 75 61 6c 20 than an actual
1a3fd 64 65 6c 61 79 2e 20 20 42 75 74 20 6f 6e 20 74 delay. But on t
1a3fe 68 65 20 31 30 74 68 0a 20 20 2a 2a 20 61 6e 20 he 10th. ** an
1a3ff 73 75 62 73 65 71 75 65 6e 74 20 72 65 74 72 69 subsequent retri
1a400 65 73 2c 20 74 68 65 20 64 65 6c 61 79 73 20 73 es, the delays s
1a401 74 61 72 74 20 62 65 63 6f 6d 69 6e 67 20 6c 6f tart becoming lo
1a402 6e 67 65 72 20 61 6e 64 20 6c 6f 6e 67 65 72 2c nger and longer,
1a403 20 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 6f . ** so that o
1a404 6e 20 74 68 65 20 31 30 30 74 68 20 28 61 6e 64 n the 100th (and
1a405 20 6c 61 73 74 29 20 52 45 54 52 59 20 77 65 20 last) RETRY we
1a406 64 65 6c 61 79 20 66 6f 72 20 32 31 20 6d 69 6c delay for 21 mil
1a407 6c 69 73 65 63 6f 6e 64 73 2e 0a 20 20 2a 2a 20 liseconds.. **
1a408 54 68 65 20 74 6f 74 61 6c 20 64 65 6c 61 79 20 The total delay
1a409 74 69 6d 65 20 62 65 66 6f 72 65 20 67 69 76 69 time before givi
1a40a 6e 67 20 75 70 20 69 73 20 6c 65 73 73 20 74 68 ng up is less th
1a40b 61 6e 20 31 20 73 65 63 6f 6e 64 2e 0a 20 20 2a an 1 second.. *
1a40c 2f 0a 20 20 69 66 28 20 63 6e 74 3e 35 20 29 7b /. if( cnt>5 ){
1a40d 0a 20 20 20 20 69 6e 74 20 6e 44 65 6c 61 79 20 . int nDelay
1a40e 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 1;
1a40f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 75 /* Pau
1a410 73 65 20 74 69 6d 65 20 69 6e 20 6d 69 63 72 6f se time in micro
1a411 73 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 20 20 69 seconds */. i
1a412 66 28 20 63 6e 74 3e 31 30 30 20 29 7b 0a 20 20 f( cnt>100 ){.
1a413 20 20 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70 57 VVA_ONLY( pW
1a414 61 6c 2d 3e 6c 6f 63 6b 45 72 72 6f 72 20 3d 20 al->lockError =
1a415 31 3b 20 29 0a 20 20 20 20 20 20 72 65 74 75 72 1; ). retur
1a416 6e 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f n SQLITE_PROTOCO
1a417 4c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 L;. }. if(
1a418 20 63 6e 74 3e 3d 31 30 20 29 20 6e 44 65 6c 61 cnt>=10 ) nDela
1a419 79 20 3d 20 28 63 6e 74 2d 39 29 2a 32 33 38 3b y = (cnt-9)*238;
1a41a 20 20 2f 2a 20 4d 61 78 20 64 65 6c 61 79 20 32 /* Max delay 2
1a41b 31 6d 73 2e 20 54 6f 74 61 6c 20 64 65 6c 61 79 1ms. Total delay
1a41c 20 39 39 36 6d 73 20 2a 2f 0a 20 20 20 20 73 71 996ms */. sq
1a41d 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 70 57 61 lite3OsSleep(pWa
1a41e 6c 2d 3e 70 56 66 73 2c 20 6e 44 65 6c 61 79 29 l->pVfs, nDelay)
1a41f 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 75 73 ;. }.. if( !us
1a420 65 57 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d eWal ){. rc =
1a421 20 77 61 6c 49 6e 64 65 78 52 65 61 64 48 64 72 walIndexReadHdr
1a422 28 70 57 61 6c 2c 20 70 43 68 61 6e 67 65 64 29 (pWal, pChanged)
1a423 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1a424 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 LITE_BUSY ){.
1a425 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 /* If there i
1a426 73 20 6e 6f 74 20 61 20 72 65 63 6f 76 65 72 79 s not a recovery
1a427 20 72 75 6e 6e 69 6e 67 20 69 6e 20 61 6e 6f 74 running in anot
1a428 68 65 72 20 74 68 72 65 61 64 20 6f 72 20 70 72 her thread or pr
1a429 6f 63 65 73 73 0a 20 20 20 20 20 20 2a 2a 20 74 ocess. ** t
1a42a 68 65 6e 20 63 6f 6e 76 65 72 74 20 42 55 53 59 hen convert BUSY
1a42b 20 65 72 72 6f 72 73 20 74 6f 20 57 41 4c 5f 52 errors to WAL_R
1a42c 45 54 52 59 2e 20 20 49 66 20 72 65 63 6f 76 65 ETRY. If recove
1a42d 72 79 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 0a 20 ry is known to.
1a42e 20 20 20 20 20 2a 2a 20 62 65 20 72 75 6e 6e 69 ** be runni
1a42f 6e 67 2c 20 63 6f 6e 76 65 72 74 20 42 55 53 59 ng, convert BUSY
1a430 20 74 6f 20 42 55 53 59 5f 52 45 43 4f 56 45 52 to BUSY_RECOVER
1a431 59 2e 20 20 54 68 65 72 65 20 69 73 20 61 20 72 Y. There is a r
1a432 61 63 65 20 68 65 72 65 0a 20 20 20 20 20 20 2a ace here. *
1a433 2a 20 77 68 69 63 68 20 6d 69 67 68 74 20 63 61 * which might ca
1a434 75 73 65 20 57 41 4c 5f 52 45 54 52 59 20 74 6f use WAL_RETRY to
1a435 20 62 65 20 72 65 74 75 72 6e 65 64 20 65 76 65 be returned eve
1a436 6e 20 69 66 20 42 55 53 59 5f 52 45 43 4f 56 45 n if BUSY_RECOVE
1a437 52 59 0a 20 20 20 20 20 20 2a 2a 20 77 6f 75 6c RY. ** woul
1a438 64 20 62 65 20 74 65 63 68 6e 69 63 61 6c 6c 79 d be technically
1a439 20 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20 74 correct. But t
1a43a 68 65 20 72 61 63 65 20 69 73 20 62 65 6e 69 67 he race is benig
1a43b 6e 20 73 69 6e 63 65 20 77 69 74 68 0a 20 20 20 n since with.
1a43c 20 20 20 2a 2a 20 57 41 4c 5f 52 45 54 52 59 20 ** WAL_RETRY
1a43d 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c this routine wil
1a43e 6c 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 l be called agai
1a43f 6e 20 61 6e 64 20 77 69 6c 6c 20 70 72 6f 62 61 n and will proba
1a440 62 6c 79 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 bly be. **
1a441 72 69 67 68 74 20 6f 6e 20 74 68 65 20 73 65 63 right on the sec
1a442 6f 6e 64 20 69 74 65 72 61 74 69 6f 6e 2e 0a 20 ond iteration..
1a443 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
1a444 28 20 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 61 ( pWal->apWiData
1a445 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 [0]==0 ){.
1a446 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 /* This branch
1a447 20 69 73 20 74 61 6b 65 6e 20 77 68 65 6e 20 74 is taken when t
1a448 68 65 20 78 53 68 6d 4d 61 70 28 29 20 6d 65 74 he xShmMap() met
1a449 68 6f 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49 hod returns SQLI
1a44a 54 45 5f 42 55 53 59 2e 0a 20 20 20 20 20 20 20 TE_BUSY..
1a44b 20 2a 2a 20 57 65 20 61 73 73 75 6d 65 20 74 68 ** We assume th
1a44c 69 73 20 69 73 20 61 20 74 72 61 6e 73 69 65 6e is is a transien
1a44d 74 20 63 6f 6e 64 69 74 69 6f 6e 2c 20 73 6f 20 t condition, so
1a44e 72 65 74 75 72 6e 20 57 41 4c 5f 52 45 54 52 59 return WAL_RETRY
1a44f 2e 20 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a . The. **
1a450 20 78 53 68 6d 4d 61 70 28 29 20 69 6d 70 6c 65 xShmMap() imple
1a451 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 64 20 62 mentation used b
1a452 79 20 74 68 65 20 64 65 66 61 75 6c 74 20 75 6e y the default un
1a453 69 78 20 61 6e 64 20 77 69 6e 33 32 20 56 46 53 ix and win32 VFS
1a454 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64 . ** mod
1a455 75 6c 65 73 20 6d 61 79 20 72 65 74 75 72 6e 20 ules may return
1a456 53 51 4c 49 54 45 5f 42 55 53 59 20 64 75 65 20 SQLITE_BUSY due
1a457 74 6f 20 61 20 72 61 63 65 20 63 6f 6e 64 69 74 to a race condit
1a458 69 6f 6e 20 69 6e 20 74 68 65 20 0a 20 20 20 20 ion in the .
1a459 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 68 61 74 ** code that
1a45a 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 determines whet
1a45b 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 73 her or not the s
1a45c 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 hared-memory reg
1a45d 69 6f 6e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 ion . **
1a45e 6d 75 73 74 20 62 65 20 7a 65 72 6f 65 64 20 62 must be zeroed b
1a45f 65 66 6f 72 65 20 74 68 65 20 72 65 71 75 65 73 efore the reques
1a460 74 65 64 20 70 61 67 65 20 69 73 20 72 65 74 75 ted page is retu
1a461 72 6e 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f rned.. */
1a462 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 57 41 . rc = WA
1a463 4c 5f 52 45 54 52 59 3b 0a 20 20 20 20 20 20 7d L_RETRY;. }
1a464 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f else if( SQLITE_
1a465 4f 4b 3d 3d 28 72 63 20 3d 20 77 61 6c 4c 6f 63 OK==(rc = walLoc
1a466 6b 53 68 61 72 65 64 28 70 57 61 6c 2c 20 57 41 kShared(pWal, WA
1a467 4c 5f 52 45 43 4f 56 45 52 5f 4c 4f 43 4b 29 29 L_RECOVER_LOCK))
1a468 20 29 7b 0a 20 20 20 20 20 20 20 20 77 61 6c 55 ){. walU
1a469 6e 6c 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c nlockShared(pWal
1a46a 2c 20 57 41 4c 5f 52 45 43 4f 56 45 52 5f 4c 4f , WAL_RECOVER_LO
1a46b 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 CK);. rc
1a46c 3d 20 57 41 4c 5f 52 45 54 52 59 3b 0a 20 20 20 = WAL_RETRY;.
1a46d 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d }else if( rc=
1a46e 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a =SQLITE_BUSY ){.
1a46f 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1a470 49 54 45 5f 42 55 53 59 5f 52 45 43 4f 56 45 52 ITE_BUSY_RECOVER
1a471 59 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d Y;. }. }
1a472 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1a473 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1a474 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
1a475 0a 20 20 7d 0a 0a 20 20 70 49 6e 66 6f 20 3d 20 . }.. pInfo =
1a476 77 61 6c 43 6b 70 74 49 6e 66 6f 28 70 57 61 6c walCkptInfo(pWal
1a477 29 3b 0a 20 20 69 66 28 20 21 75 73 65 57 61 6c );. if( !useWal
1a478 20 26 26 20 70 49 6e 66 6f 2d 3e 6e 42 61 63 6b && pInfo->nBack
1a479 66 69 6c 6c 3d 3d 70 57 61 6c 2d 3e 68 64 72 2e fill==pWal->hdr.
1a47a 6d 78 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 2f mxFrame ){. /
1a47b 2a 20 54 68 65 20 57 41 4c 20 68 61 73 20 62 65 * The WAL has be
1a47c 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62 61 en completely ba
1a47d 63 6b 66 69 6c 6c 65 64 20 28 6f 72 20 69 74 20 ckfilled (or it
1a47e 69 73 20 65 6d 70 74 79 29 2e 0a 20 20 20 20 2a is empty).. *
1a47f 2a 20 61 6e 64 20 63 61 6e 20 62 65 20 73 61 66 * and can be saf
1a480 65 6c 79 20 69 67 6e 6f 72 65 64 2e 0a 20 20 20 ely ignored..
1a481 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77 61 6c */. rc = wal
1a482 4c 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c 2c LockShared(pWal,
1a483 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 30 WAL_READ_LOCK(0
1a484 29 29 3b 0a 20 20 20 20 77 61 6c 53 68 6d 42 61 ));. walShmBa
1a485 72 72 69 65 72 28 70 57 61 6c 29 3b 0a 20 20 20 rrier(pWal);.
1a486 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1a487 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 OK ){. if(
1a488 6d 65 6d 63 6d 70 28 28 76 6f 69 64 20 2a 29 77 memcmp((void *)w
1a489 61 6c 49 6e 64 65 78 48 64 72 28 70 57 61 6c 29 alIndexHdr(pWal)
1a48a 2c 20 26 70 57 61 6c 2d 3e 68 64 72 2c 20 73 69 , &pWal->hdr, si
1a48b 7a 65 6f 66 28 57 61 6c 49 6e 64 65 78 48 64 72 zeof(WalIndexHdr
1a48c 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a )) ){. /*
1a48d 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 It is not safe
1a48e 74 6f 20 61 6c 6c 6f 77 20 74 68 65 20 72 65 61 to allow the rea
1a48f 64 65 72 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 der to continue
1a490 68 65 72 65 20 69 66 20 66 72 61 6d 65 73 0a 20 here if frames.
1a491 20 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 68 61 ** may ha
1a492 76 65 20 62 65 65 6e 20 61 70 70 65 6e 64 65 64 ve been appended
1a493 20 74 6f 20 74 68 65 20 6c 6f 67 20 62 65 66 6f to the log befo
1a494 72 65 20 52 45 41 44 5f 4c 4f 43 4b 28 30 29 20 re READ_LOCK(0)
1a495 77 61 73 20 6f 62 74 61 69 6e 65 64 2e 0a 20 20 was obtained..
1a496 20 20 20 20 20 20 2a 2a 20 57 68 65 6e 20 68 6f ** When ho
1a497 6c 64 69 6e 67 20 52 45 41 44 5f 4c 4f 43 4b 28 lding READ_LOCK(
1a498 30 29 2c 20 74 68 65 20 72 65 61 64 65 72 20 69 0), the reader i
1a499 67 6e 6f 72 65 73 20 74 68 65 20 65 6e 74 69 72 gnores the entir
1a49a 65 20 6c 6f 67 20 66 69 6c 65 2c 0a 20 20 20 20 e log file,.
1a49b 20 20 20 20 2a 2a 20 77 68 69 63 68 20 69 6d 70 ** which imp
1a49c 6c 69 65 73 20 74 68 61 74 20 74 68 65 20 64 61 lies that the da
1a49d 74 61 62 61 73 65 20 66 69 6c 65 20 63 6f 6e 74 tabase file cont
1a49e 61 69 6e 73 20 61 20 74 72 75 73 74 77 6f 72 74 ains a trustwort
1a49f 68 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 6e hy. ** sn
1a4a0 61 70 73 68 6f 54 2e 20 53 69 6e 63 65 20 68 6f apshoT. Since ho
1a4a1 6c 64 69 6e 67 20 52 45 41 44 5f 4c 4f 43 4b 28 lding READ_LOCK(
1a4a2 30 29 20 70 72 65 76 65 6e 74 73 20 61 20 63 68 0) prevents a ch
1a4a3 65 63 6b 70 6f 69 6e 74 20 66 72 6f 6d 0a 20 20 eckpoint from.
1a4a4 20 20 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 69 ** happeni
1a4a5 6e 67 2c 20 74 68 69 73 20 69 73 20 75 73 75 61 ng, this is usua
1a4a6 6c 6c 79 20 63 6f 72 72 65 63 74 2e 0a 20 20 20 lly correct..
1a4a7 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 **.
1a4a8 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 66 20 66 ** However, if f
1a4a9 72 61 6d 65 73 20 68 61 76 65 20 62 65 65 6e 20 rames have been
1a4aa 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 appended to the
1a4ab 6c 6f 67 20 28 6f 72 20 69 66 20 74 68 65 20 6c log (or if the l
1a4ac 6f 67 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 og . ** i
1a4ad 73 20 77 72 61 70 70 65 64 20 61 6e 64 20 77 72 s wrapped and wr
1a4ae 69 74 74 65 6e 20 66 6f 72 20 74 68 61 74 20 6d itten for that m
1a4af 61 74 74 65 72 29 20 62 65 66 6f 72 65 20 74 68 atter) before th
1a4b0 65 20 52 45 41 44 5f 4c 4f 43 4b 28 30 29 0a 20 e READ_LOCK(0).
1a4b1 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6f 62 74 ** is obt
1a4b2 61 69 6e 65 64 2c 20 74 68 61 74 20 69 73 20 6e ained, that is n
1a4b3 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 74 ot necessarily t
1a4b4 72 75 65 2e 20 41 20 63 68 65 63 6b 70 6f 69 6e rue. A checkpoin
1a4b5 74 65 72 20 6d 61 79 0a 20 20 20 20 20 20 20 20 ter may.
1a4b6 2a 2a 20 68 61 76 65 20 73 74 61 72 74 65 64 20 ** have started
1a4b7 74 6f 20 62 61 63 6b 66 69 6c 6c 20 74 68 65 20 to backfill the
1a4b8 61 70 70 65 6e 64 65 64 20 66 72 61 6d 65 73 20 appended frames
1a4b9 62 75 74 20 63 72 61 73 68 65 64 20 62 65 66 6f but crashed befo
1a4ba 72 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74 re. ** it
1a4bb 20 66 69 6e 69 73 68 65 64 2e 20 4c 65 61 76 69 finished. Leavi
1a4bc 6e 67 20 61 20 63 6f 72 72 75 70 74 20 69 6d 61 ng a corrupt ima
1a4bd 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ge in the databa
1a4be 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 se file..
1a4bf 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 61 6c 55 */. walU
1a4c0 6e 6c 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c nlockShared(pWal
1a4c1 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 , WAL_READ_LOCK(
1a4c2 30 29 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 0));. ret
1a4c3 75 72 6e 20 57 41 4c 5f 52 45 54 52 59 3b 0a 20 urn WAL_RETRY;.
1a4c4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 57 61 }. pWa
1a4c5 6c 2d 3e 72 65 61 64 4c 6f 63 6b 20 3d 20 30 3b l->readLock = 0;
1a4c6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
1a4c7 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c LITE_OK;. }el
1a4c8 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 se if( rc!=SQLIT
1a4c9 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 E_BUSY ){.
1a4ca 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
1a4cb 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 . }.. /* If we
1a4cc 20 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 get this far, i
1a4cd 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 t means that the
1a4ce 20 72 65 61 64 65 72 20 77 69 6c 6c 20 77 61 6e reader will wan
1a4cf 74 20 74 6f 20 75 73 65 0a 20 20 2a 2a 20 74 68 t to use. ** th
1a4d0 65 20 57 41 4c 20 74 6f 20 67 65 74 20 61 74 20 e WAL to get at
1a4d1 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 63 content from rec
1a4d2 65 6e 74 20 63 6f 6d 6d 69 74 73 2e 20 20 54 68 ent commits. Th
1a4d3 65 20 6a 6f 62 20 6e 6f 77 20 69 73 0a 20 20 2a e job now is. *
1a4d4 2a 20 74 6f 20 73 65 6c 65 63 74 20 6f 6e 65 20 * to select one
1a4d5 6f 66 20 74 68 65 20 61 52 65 61 64 4d 61 72 6b of the aReadMark
1a4d6 5b 5d 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 [] entries that
1a4d7 69 73 20 63 6c 6f 73 65 73 74 20 74 6f 0a 20 20 is closest to.
1a4d8 2a 2a 20 62 75 74 20 6e 6f 74 20 65 78 63 65 65 ** but not excee
1a4d9 64 69 6e 67 20 70 57 61 6c 2d 3e 68 64 72 2e 6d ding pWal->hdr.m
1a4da 78 46 72 61 6d 65 20 61 6e 64 20 6c 6f 63 6b 20 xFrame and lock
1a4db 74 68 61 74 20 65 6e 74 72 79 2e 0a 20 20 2a 2f that entry.. */
1a4dc 0a 20 20 6d 78 52 65 61 64 4d 61 72 6b 20 3d 20 . mxReadMark =
1a4dd 30 3b 0a 20 20 6d 78 49 20 3d 20 30 3b 0a 20 20 0;. mxI = 0;.
1a4de 66 6f 72 28 69 3d 31 3b 20 69 3c 57 41 4c 5f 4e for(i=1; i<WAL_N
1a4df 52 45 41 44 45 52 3b 20 69 2b 2b 29 7b 0a 20 20 READER; i++){.
1a4e0 20 20 75 33 32 20 74 68 69 73 4d 61 72 6b 20 3d u32 thisMark =
1a4e1 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61 72 pInfo->aReadMar
1a4e2 6b 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 6d 78 k[i];. if( mx
1a4e3 52 65 61 64 4d 61 72 6b 3c 3d 74 68 69 73 4d 61 ReadMark<=thisMa
1a4e4 72 6b 20 26 26 20 74 68 69 73 4d 61 72 6b 3c 3d rk && thisMark<=
1a4e5 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d pWal->hdr.mxFram
1a4e6 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 e ){. asser
1a4e7 74 28 20 74 68 69 73 4d 61 72 6b 21 3d 52 45 41 t( thisMark!=REA
1a4e8 44 4d 41 52 4b 5f 4e 4f 54 5f 55 53 45 44 20 29 DMARK_NOT_USED )
1a4e9 3b 0a 20 20 20 20 20 20 6d 78 52 65 61 64 4d 61 ;. mxReadMa
1a4ea 72 6b 20 3d 20 74 68 69 73 4d 61 72 6b 3b 0a 20 rk = thisMark;.
1a4eb 20 20 20 20 20 6d 78 49 20 3d 20 69 3b 0a 20 20 mxI = i;.
1a4ec 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 54 68 65 }. }. /* The
1a4ed 72 65 20 77 61 73 20 6f 6e 63 65 20 61 6e 20 22 re was once an "
1a4ee 69 66 22 20 68 65 72 65 2e 20 54 68 65 20 65 78 if" here. The ex
1a4ef 74 72 61 20 22 7b 22 20 69 73 20 74 6f 20 70 72 tra "{" is to pr
1a4f0 65 73 65 72 76 65 20 69 6e 64 65 6e 74 61 74 69 eserve indentati
1a4f1 6f 6e 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 69 on. */. {. i
1a4f2 66 28 20 28 70 57 61 6c 2d 3e 72 65 61 64 4f 6e f( (pWal->readOn
1a4f3 6c 79 20 26 20 57 41 4c 5f 53 48 4d 5f 52 44 4f ly & WAL_SHM_RDO
1a4f4 4e 4c 59 29 3d 3d 30 0a 20 20 20 20 20 26 26 20 NLY)==0. &&
1a4f5 28 6d 78 52 65 61 64 4d 61 72 6b 3c 70 57 61 6c (mxReadMark<pWal
1a4f6 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 20 7c 7c ->hdr.mxFrame ||
1a4f7 20 6d 78 49 3d 3d 30 29 0a 20 20 20 20 29 7b 0a mxI==0). ){.
1a4f8 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 for(i=1; i
1a4f9 3c 57 41 4c 5f 4e 52 45 41 44 45 52 3b 20 69 2b <WAL_NREADER; i+
1a4fa 2b 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d +){. rc =
1a4fb 20 77 61 6c 4c 6f 63 6b 45 78 63 6c 75 73 69 76 walLockExclusiv
1a4fc 65 28 70 57 61 6c 2c 20 57 41 4c 5f 52 45 41 44 e(pWal, WAL_READ
1a4fd 5f 4c 4f 43 4b 28 69 29 2c 20 31 29 3b 0a 20 20 _LOCK(i), 1);.
1a4fe 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1a4ff 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1a500 20 20 20 20 20 6d 78 52 65 61 64 4d 61 72 6b 20 mxReadMark
1a501 3d 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61 = pInfo->aReadMa
1a502 72 6b 5b 69 5d 20 3d 20 70 57 61 6c 2d 3e 68 64 rk[i] = pWal->hd
1a503 72 2e 6d 78 46 72 61 6d 65 3b 0a 20 20 20 20 20 r.mxFrame;.
1a504 20 20 20 20 20 6d 78 49 20 3d 20 69 3b 0a 20 20 mxI = i;.
1a505 20 20 20 20 20 20 20 20 77 61 6c 55 6e 6c 6f 63 walUnloc
1a506 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61 6c 2c kExclusive(pWal,
1a507 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 69 WAL_READ_LOCK(i
1a508 29 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 ), 1);.
1a509 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1a50a 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 }else if( rc!=SQ
1a50b 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 LITE_BUSY ){.
1a50c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1a50d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1a50e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
1a50f 28 20 6d 78 49 3d 3d 30 20 29 7b 0a 20 20 20 20 ( mxI==0 ){.
1a510 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
1a511 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 28 70 57 LITE_BUSY || (pW
1a512 61 6c 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 20 57 al->readOnly & W
1a513 41 4c 5f 53 48 4d 5f 52 44 4f 4e 4c 59 29 21 3d AL_SHM_RDONLY)!=
1a514 30 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 0 );. retur
1a515 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 n rc==SQLITE_BUS
1a516 59 20 3f 20 57 41 4c 5f 52 45 54 52 59 20 3a 20 Y ? WAL_RETRY :
1a517 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5f SQLITE_READONLY_
1a518 43 41 4e 54 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a CANTLOCK;. }.
1a519 0a 20 20 20 20 72 63 20 3d 20 77 61 6c 4c 6f 63 . rc = walLoc
1a51a 6b 53 68 61 72 65 64 28 70 57 61 6c 2c 20 57 41 kShared(pWal, WA
1a51b 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 6d 78 49 29 L_READ_LOCK(mxI)
1a51c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b );. if( rc ){
1a51d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
1a51e 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 3f 20 ==SQLITE_BUSY ?
1a51f 57 41 4c 5f 52 45 54 52 59 20 3a 20 72 63 3b 0a WAL_RETRY : rc;.
1a520 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 4e 6f 77 }. /* Now
1a521 20 74 68 61 74 20 74 68 65 20 72 65 61 64 2d 6c that the read-l
1a522 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 ock has been obt
1a523 61 69 6e 65 64 2c 20 63 68 65 63 6b 20 74 68 61 ained, check tha
1a524 74 20 6e 65 69 74 68 65 72 20 74 68 65 0a 20 20 t neither the.
1a525 20 20 2a 2a 20 76 61 6c 75 65 20 69 6e 20 74 68 ** value in th
1a526 65 20 61 52 65 61 64 4d 61 72 6b 5b 5d 20 61 72 e aReadMark[] ar
1a527 72 61 79 20 6f 72 20 74 68 65 20 63 6f 6e 74 65 ray or the conte
1a528 6e 74 73 20 6f 66 20 74 68 65 20 77 61 6c 2d 69 nts of the wal-i
1a529 6e 64 65 78 0a 20 20 20 20 2a 2a 20 68 65 61 64 ndex. ** head
1a52a 65 72 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e er have changed.
1a52b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 . **. ** I
1a52c 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 t is necessary t
1a52d 6f 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 o check that the
1a52e 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 wal-index heade
1a52f 72 20 64 69 64 20 6e 6f 74 20 63 68 61 6e 67 65 r did not change
1a530 0a 20 20 20 20 2a 2a 20 62 65 74 77 65 65 6e 20 . ** between
1a531 74 68 65 20 74 69 6d 65 20 69 74 20 77 61 73 20 the time it was
1a532 72 65 61 64 20 61 6e 64 20 77 68 65 6e 20 74 68 read and when th
1a533 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 77 61 e shared-lock wa
1a534 73 20 6f 62 74 61 69 6e 65 64 0a 20 20 20 20 2a s obtained. *
1a535 2a 20 6f 6e 20 57 41 4c 5f 52 45 41 44 5f 4c 4f * on WAL_READ_LO
1a536 43 4b 28 6d 78 49 29 20 77 61 73 20 6f 62 74 61 CK(mxI) was obta
1a537 69 6e 65 64 20 74 6f 20 61 63 63 6f 75 6e 74 20 ined to account
1a538 66 6f 72 20 74 68 65 20 70 6f 73 73 69 62 69 6c for the possibil
1a539 69 74 79 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 ity. ** that
1a53a 74 68 65 20 6c 6f 67 20 66 69 6c 65 20 6d 61 79 the log file may
1a53b 20 68 61 76 65 20 62 65 65 6e 20 77 72 61 70 70 have been wrapp
1a53c 65 64 20 62 79 20 61 20 77 72 69 74 65 72 2c 20 ed by a writer,
1a53d 6f 72 20 74 68 61 74 20 66 72 61 6d 65 73 0a 20 or that frames.
1a53e 20 20 20 2a 2a 20 74 68 61 74 20 6f 63 63 75 72 ** that occur
1a53f 20 6c 61 74 65 72 20 69 6e 20 74 68 65 20 6c 6f later in the lo
1a540 67 20 74 68 61 6e 20 70 57 61 6c 2d 3e 68 64 72 g than pWal->hdr
1a541 2e 6d 78 46 72 61 6d 65 20 6d 61 79 20 68 61 76 .mxFrame may hav
1a542 65 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 63 6f e been. ** co
1a543 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 64 61 pied into the da
1a544 74 61 62 61 73 65 20 62 79 20 61 20 63 68 65 63 tabase by a chec
1a545 6b 70 6f 69 6e 74 65 72 2e 20 49 66 20 65 69 74 kpointer. If eit
1a546 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 68 69 her of these thi
1a547 6e 67 73 0a 20 20 20 20 2a 2a 20 68 61 70 70 65 ngs. ** happe
1a548 6e 65 64 2c 20 74 68 65 6e 20 72 65 61 64 69 6e ned, then readin
1a549 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 g the database w
1a54a 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 20 ith the current
1a54b 76 61 6c 75 65 20 6f 66 0a 20 20 20 20 2a 2a 20 value of. **
1a54c 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d pWal->hdr.mxFram
1a54d 65 20 72 69 73 6b 73 20 72 65 61 64 69 6e 67 20 e risks reading
1a54e 61 20 63 6f 72 72 75 70 74 65 64 20 73 6e 61 70 a corrupted snap
1a54f 73 68 6f 74 2e 20 53 6f 2c 20 72 65 74 72 79 0a shot. So, retry.
1a550 20 20 20 20 2a 2a 20 69 6e 73 74 65 61 64 2e 0a ** instead..
1a551 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
1a552 69 73 20 64 6f 65 73 20 6e 6f 74 20 67 75 61 72 is does not guar
1a553 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 63 antee that the c
1a554 6f 70 79 20 6f 66 20 74 68 65 20 77 61 6c 2d 69 opy of the wal-i
1a555 6e 64 65 78 20 68 65 61 64 65 72 20 69 73 20 75 ndex header is u
1a556 70 20 74 6f 0a 20 20 20 20 2a 2a 20 64 61 74 65 p to. ** date
1a557 20 62 65 66 6f 72 65 20 70 72 6f 63 65 65 64 69 before proceedi
1a558 6e 67 2e 20 54 68 61 74 20 77 6f 75 6c 64 20 6e ng. That would n
1a559 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 20 77 ot be possible w
1a55a 69 74 68 6f 75 74 20 73 6f 6d 65 68 6f 77 0a 20 ithout somehow.
1a55b 20 20 20 2a 2a 20 62 6c 6f 63 6b 69 6e 67 20 77 ** blocking w
1a55c 72 69 74 65 72 73 2e 20 49 74 20 6f 6e 6c 79 20 riters. It only
1a55d 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 guarantees that
1a55e 61 20 64 61 6e 67 65 72 6f 75 73 20 63 68 65 63 a dangerous chec
1a55f 6b 70 6f 69 6e 74 20 6f 72 20 0a 20 20 20 20 2a kpoint or . *
1a560 2a 20 6c 6f 67 2d 77 72 61 70 20 28 65 69 74 68 * log-wrap (eith
1a561 65 72 20 6f 66 20 77 68 69 63 68 20 77 6f 75 6c er of which woul
1a562 64 20 72 65 71 75 69 72 65 20 61 6e 20 65 78 63 d require an exc
1a563 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 0a 20 lusive lock on.
1a564 20 20 20 2a 2a 20 57 41 4c 5f 52 45 41 44 5f 4c ** WAL_READ_L
1a565 4f 43 4b 28 6d 78 49 29 29 20 68 61 73 20 6e 6f OCK(mxI)) has no
1a566 74 20 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65 t occurred since
1a567 20 74 68 65 20 73 6e 61 70 73 68 6f 74 20 77 61 the snapshot wa
1a568 73 20 76 61 6c 69 64 2e 0a 20 20 20 20 2a 2f 0a s valid.. */.
1a569 20 20 20 20 77 61 6c 53 68 6d 42 61 72 72 69 65 walShmBarrie
1a56a 72 28 70 57 61 6c 29 3b 0a 20 20 20 20 69 66 28 r(pWal);. if(
1a56b 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61 72 pInfo->aReadMar
1a56c 6b 5b 6d 78 49 5d 21 3d 6d 78 52 65 61 64 4d 61 k[mxI]!=mxReadMa
1a56d 72 6b 0a 20 20 20 20 20 7c 7c 20 6d 65 6d 63 6d rk. || memcm
1a56e 70 28 28 76 6f 69 64 20 2a 29 77 61 6c 49 6e 64 p((void *)walInd
1a56f 65 78 48 64 72 28 70 57 61 6c 29 2c 20 26 70 57 exHdr(pWal), &pW
1a570 61 6c 2d 3e 68 64 72 2c 20 73 69 7a 65 6f 66 28 al->hdr, sizeof(
1a571 57 61 6c 49 6e 64 65 78 48 64 72 29 29 0a 20 20 WalIndexHdr)).
1a572 20 20 29 7b 0a 20 20 20 20 20 20 77 61 6c 55 6e ){. walUn
1a573 6c 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c 2c lockShared(pWal,
1a574 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 6d WAL_READ_LOCK(m
1a575 78 49 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 xI));. retu
1a576 72 6e 20 57 41 4c 5f 52 45 54 52 59 3b 0a 20 20 rn WAL_RETRY;.
1a577 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
1a578 73 73 65 72 74 28 20 6d 78 52 65 61 64 4d 61 72 ssert( mxReadMar
1a579 6b 3c 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 k<=pWal->hdr.mxF
1a57a 72 61 6d 65 20 29 3b 0a 20 20 20 20 20 20 70 57 rame );. pW
1a57b 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 20 3d 20 28 al->readLock = (
1a57c 69 31 36 29 6d 78 49 3b 0a 20 20 20 20 7d 0a 20 i16)mxI;. }.
1a57d 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1a57e 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 }../*.** Begin a
1a57f 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f read transactio
1a580 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 n on the databas
1a581 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
1a582 75 74 69 6e 65 20 75 73 65 64 20 74 6f 20 62 65 utine used to be
1a583 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 4f called sqlite3O
1a584 70 65 6e 53 6e 61 70 73 68 6f 74 28 29 20 61 6e penSnapshot() an
1a585 64 20 77 69 74 68 20 67 6f 6f 64 20 72 65 61 73 d with good reas
1a586 6f 6e 3a 0a 2a 2a 20 69 74 20 74 61 6b 65 73 20 on:.** it takes
1a587 61 20 73 6e 61 70 73 68 6f 74 20 6f 66 20 74 68 a snapshot of th
1a588 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 57 e state of the W
1a589 41 4c 20 61 6e 64 20 77 61 6c 2d 69 6e 64 65 78 AL and wal-index
1a58a 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 for the current
1a58b 0a 2a 2a 20 69 6e 73 74 61 6e 74 20 69 6e 20 74 .** instant in t
1a58c 69 6d 65 2e 20 20 54 68 65 20 63 75 72 72 65 6e ime. The curren
1a58d 74 20 74 68 72 65 61 64 20 77 69 6c 6c 20 63 6f t thread will co
1a58e 6e 74 69 6e 75 65 20 74 6f 20 75 73 65 20 74 68 ntinue to use th
1a58f 69 73 20 73 6e 61 70 73 68 6f 74 2e 0a 2a 2a 20 is snapshot..**
1a590 4f 74 68 65 72 20 74 68 72 65 61 64 73 20 6d 69 Other threads mi
1a591 67 68 74 20 61 70 70 65 6e 64 20 6e 65 77 20 63 ght append new c
1a592 6f 6e 74 65 6e 74 20 74 6f 20 74 68 65 20 57 41 ontent to the WA
1a593 4c 20 61 6e 64 20 77 61 6c 2d 69 6e 64 65 78 20 L and wal-index
1a594 62 75 74 0a 2a 2a 20 74 68 61 74 20 65 78 74 72 but.** that extr
1a595 61 20 63 6f 6e 74 65 6e 74 20 69 73 20 69 67 6e a content is ign
1a596 6f 72 65 64 20 62 79 20 74 68 65 20 63 75 72 72 ored by the curr
1a597 65 6e 74 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a ent thread..**.*
1a598 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
1a599 65 20 63 6f 6e 74 65 6e 74 73 20 68 61 76 65 20 e contents have
1a59a 63 68 61 6e 67 65 73 20 73 69 6e 63 65 20 74 68 changes since th
1a59b 65 20 70 72 65 76 69 6f 75 73 20 72 65 61 64 0a e previous read.
1a59c 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 ** transaction,
1a59d 74 68 65 6e 20 2a 70 43 68 61 6e 67 65 64 20 69 then *pChanged i
1a59e 73 20 73 65 74 20 74 6f 20 31 20 62 65 66 6f 72 s set to 1 befor
1a59f 65 20 72 65 74 75 72 6e 69 6e 67 2e 20 20 54 68 e returning. Th
1a5a0 65 0a 2a 2a 20 50 61 67 65 72 20 6c 61 79 65 72 e.** Pager layer
1a5a1 20 77 69 6c 6c 20 75 73 65 20 74 68 69 73 20 74 will use this t
1a5a2 6f 20 6b 6e 6f 77 20 74 68 61 74 20 69 73 20 63 o know that is c
1a5a3 61 63 68 65 20 69 73 20 73 74 61 6c 65 20 61 6e ache is stale an
1a5a4 64 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 d.** needs to be
1a5a5 20 66 6c 75 73 68 65 64 2e 0a 2a 2f 0a 53 51 4c flushed..*/.SQL
1a5a6 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1a5a7 73 71 6c 69 74 65 33 57 61 6c 42 65 67 69 6e 52 sqlite3WalBeginR
1a5a8 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 57 eadTransaction(W
1a5a9 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 20 2a 70 al *pWal, int *p
1a5aa 43 68 61 6e 67 65 64 29 7b 0a 20 20 69 6e 74 20 Changed){. int
1a5ab 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1a5ac 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1a5ad 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
1a5ae 69 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 20 int cnt = 0;
1a5af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a5b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 54 72 79 /* Number of Try
1a5b1 42 65 67 69 6e 52 65 61 64 20 61 74 74 65 6d 70 BeginRead attemp
1a5b2 74 73 20 2a 2f 0a 0a 20 20 64 6f 7b 0a 20 20 20 ts */.. do{.
1a5b3 20 72 63 20 3d 20 77 61 6c 54 72 79 42 65 67 69 rc = walTryBegi
1a5b4 6e 52 65 61 64 28 70 57 61 6c 2c 20 70 43 68 61 nRead(pWal, pCha
1a5b5 6e 67 65 64 2c 20 30 2c 20 2b 2b 63 6e 74 29 3b nged, 0, ++cnt);
1a5b6 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 57 . }while( rc==W
1a5b7 41 4c 5f 52 45 54 52 59 20 29 3b 0a 20 20 74 65 AL_RETRY );. te
1a5b8 73 74 63 61 73 65 28 20 28 72 63 26 30 78 66 66 stcase( (rc&0xff
1a5b9 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 )==SQLITE_BUSY )
1a5ba 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 28 72 ;. testcase( (r
1a5bb 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f c&0xff)==SQLITE_
1a5bc 49 4f 45 52 52 20 29 3b 0a 20 20 74 65 73 74 63 IOERR );. testc
1a5bd 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ase( rc==SQLITE_
1a5be 50 52 4f 54 4f 43 4f 4c 20 29 3b 0a 20 20 74 65 PROTOCOL );. te
1a5bf 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 stcase( rc==SQLI
1a5c0 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 65 74 75 72 TE_OK );. retur
1a5c1 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 n rc;.}../*.** F
1a5c2 69 6e 69 73 68 20 77 69 74 68 20 61 20 72 65 61 inish with a rea
1a5c3 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 d transaction.
1a5c4 41 6c 6c 20 74 68 69 73 20 64 6f 65 73 20 69 73 All this does is
1a5c5 20 72 65 6c 65 61 73 65 20 74 68 65 0a 2a 2a 20 release the.**
1a5c6 72 65 61 64 2d 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 read-lock..*/.SQ
1a5c7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1a5c8 64 20 73 71 6c 69 74 65 33 57 61 6c 45 6e 64 52 d sqlite3WalEndR
1a5c9 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 57 eadTransaction(W
1a5ca 61 6c 20 2a 70 57 61 6c 29 7b 0a 20 20 73 71 6c al *pWal){. sql
1a5cb 69 74 65 33 57 61 6c 45 6e 64 57 72 69 74 65 54 ite3WalEndWriteT
1a5cc 72 61 6e 73 61 63 74 69 6f 6e 28 70 57 61 6c 29 ransaction(pWal)
1a5cd 3b 0a 20 20 69 66 28 20 70 57 61 6c 2d 3e 72 65 ;. if( pWal->re
1a5ce 61 64 4c 6f 63 6b 3e 3d 30 20 29 7b 0a 20 20 20 adLock>=0 ){.
1a5cf 20 77 61 6c 55 6e 6c 6f 63 6b 53 68 61 72 65 64 walUnlockShared
1a5d0 28 70 57 61 6c 2c 20 57 41 4c 5f 52 45 41 44 5f (pWal, WAL_READ_
1a5d1 4c 4f 43 4b 28 70 57 61 6c 2d 3e 72 65 61 64 4c LOCK(pWal->readL
1a5d2 6f 63 6b 29 29 3b 0a 20 20 20 20 70 57 61 6c 2d ock));. pWal-
1a5d3 3e 72 65 61 64 4c 6f 63 6b 20 3d 20 2d 31 3b 0a >readLock = -1;.
1a5d4 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 }.}../*.** Rea
1a5d5 64 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 d a page from th
1a5d6 65 20 57 41 4c 2c 20 69 66 20 69 74 20 69 73 20 e WAL, if it is
1a5d7 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 57 present in the W
1a5d8 41 4c 20 61 6e 64 20 69 66 20 74 68 65 20 0a 2a AL and if the .*
1a5d9 2a 20 63 75 72 72 65 6e 74 20 72 65 61 64 20 74 * current read t
1a5da 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f ransaction is co
1a5db 6e 66 69 67 75 72 65 64 20 74 6f 20 75 73 65 20 nfigured to use
1a5dc 74 68 65 20 57 41 4c 2e 20 20 0a 2a 2a 0a 2a 2a the WAL. .**.**
1a5dd 20 54 68 65 20 2a 70 49 6e 57 61 6c 20 69 73 20 The *pInWal is
1a5de 73 65 74 20 74 6f 20 31 20 69 66 20 74 68 65 20 set to 1 if the
1a5df 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 requested page i
1a5e0 73 20 69 6e 20 74 68 65 20 57 41 4c 20 61 6e 64 s in the WAL and
1a5e1 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 .** has been loa
1a5e2 64 65 64 2e 20 20 4f 72 20 2a 70 49 6e 57 61 6c ded. Or *pInWal
1a5e3 20 69 73 20 73 65 74 20 74 6f 20 30 20 69 66 20 is set to 0 if
1a5e4 74 68 65 20 70 61 67 65 20 77 61 73 20 6e 6f 74 the page was not
1a5e5 20 69 6e 20 0a 2a 2a 20 74 68 65 20 57 41 4c 20 in .** the WAL
1a5e6 61 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65 20 and needs to be
1a5e7 72 65 61 64 20 6f 75 74 20 6f 66 20 74 68 65 20 read out of the
1a5e8 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c database..*/.SQL
1a5e9 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1a5ea 73 71 6c 69 74 65 33 57 61 6c 52 65 61 64 28 0a sqlite3WalRead(.
1a5eb 20 20 57 61 6c 20 2a 70 57 61 6c 2c 20 20 20 20 Wal *pWal,
1a5ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a5ed 20 20 2f 2a 20 57 41 4c 20 68 61 6e 64 6c 65 20 /* WAL handle
1a5ee 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 */. Pgno pgno,
1a5ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a5f0 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
1a5f1 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 page number to
1a5f2 72 65 61 64 20 64 61 74 61 20 66 6f 72 20 2a 2f read data for */
1a5f3 0a 20 20 69 6e 74 20 2a 70 49 6e 57 61 6c 2c 20 . int *pInWal,
1a5f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a5f5 20 20 20 2f 2a 20 4f 55 54 3a 20 54 72 75 65 20 /* OUT: True
1a5f6 69 66 20 64 61 74 61 20 69 73 20 72 65 61 64 20 if data is read
1a5f7 66 72 6f 6d 20 57 41 4c 20 2a 2f 0a 20 20 69 6e from WAL */. in
1a5f8 74 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20 t nOut,
1a5f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a5fa 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20 Size of buffer
1a5fb 70 4f 75 74 20 69 6e 20 62 79 74 65 73 20 2a 2f pOut in bytes */
1a5fc 0a 20 20 75 38 20 2a 70 4f 75 74 20 20 20 20 20 . u8 *pOut
1a5fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a5fe 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 /* Buffer to
1a5ff 77 72 69 74 65 20 70 61 67 65 20 64 61 74 61 20 write page data
1a600 74 6f 20 2a 2f 0a 29 7b 0a 20 20 75 33 32 20 69 to */.){. u32 i
1a601 52 65 61 64 20 3d 20 30 3b 20 20 20 20 20 20 20 Read = 0;
1a602 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
1a603 20 21 3d 30 2c 20 57 41 4c 20 66 72 61 6d 65 20 !=0, WAL frame
1a604 74 6f 20 72 65 74 75 72 6e 20 64 61 74 61 20 66 to return data f
1a605 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 69 4c 61 rom */. u32 iLa
1a606 73 74 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 6d st = pWal->hdr.m
1a607 78 46 72 61 6d 65 3b 20 20 2f 2a 20 4c 61 73 74 xFrame; /* Last
1a608 20 70 61 67 65 20 69 6e 20 57 41 4c 20 66 6f 72 page in WAL for
1a609 20 74 68 69 73 20 72 65 61 64 65 72 20 2a 2f 0a this reader */.
1a60a 20 20 69 6e 74 20 69 48 61 73 68 3b 20 20 20 20 int iHash;
1a60b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a60c 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f /* Used to loo
1a60d 70 20 74 68 72 6f 75 67 68 20 4e 20 68 61 73 68 p through N hash
1a60e 20 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a tables */.. /*
1a60f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
1a610 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 only be called
1a611 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 72 65 from within a re
1a612 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ad transaction.
1a613 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 57 61 */. assert( pWa
1a614 6c 2d 3e 72 65 61 64 4c 6f 63 6b 3e 3d 30 20 7c l->readLock>=0 |
1a615 7c 20 70 57 61 6c 2d 3e 6c 6f 63 6b 45 72 72 6f | pWal->lockErro
1a616 72 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 r );.. /* If th
1a617 65 20 22 6c 61 73 74 20 70 61 67 65 22 20 66 69 e "last page" fi
1a618 65 6c 64 20 6f 66 20 74 68 65 20 77 61 6c 2d 69 eld of the wal-i
1a619 6e 64 65 78 20 68 65 61 64 65 72 20 73 6e 61 70 ndex header snap
1a61a 73 68 6f 74 20 69 73 20 30 2c 20 74 68 65 6e 0a shot is 0, then.
1a61b 20 20 2a 2a 20 6e 6f 20 64 61 74 61 20 77 69 6c ** no data wil
1a61c 6c 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 l be read from t
1a61d 68 65 20 77 61 6c 20 75 6e 64 65 72 20 61 6e 79 he wal under any
1a61e 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 2e 20 circumstances.
1a61f 52 65 74 75 72 6e 20 65 61 72 6c 79 0a 20 20 2a Return early. *
1a620 2a 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 * in this case a
1a621 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f s an optimizatio
1a622 6e 2e 20 20 4c 69 6b 65 77 69 73 65 2c 20 69 66 n. Likewise, if
1a623 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 3d pWal->readLock=
1a624 3d 30 2c 20 0a 20 20 2a 2a 20 74 68 65 6e 20 74 =0, . ** then t
1a625 68 65 20 57 41 4c 20 69 73 20 69 67 6e 6f 72 65 he WAL is ignore
1a626 64 20 62 79 20 74 68 65 20 72 65 61 64 65 72 20 d by the reader
1a627 73 6f 20 72 65 74 75 72 6e 20 65 61 72 6c 79 2c so return early,
1a628 20 61 73 20 69 66 20 74 68 65 20 0a 20 20 2a 2a as if the . **
1a629 20 57 41 4c 20 77 65 72 65 20 65 6d 70 74 79 2e WAL were empty.
1a62a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 4c 61 73 . */. if( iLas
1a62b 74 3d 3d 30 20 7c 7c 20 70 57 61 6c 2d 3e 72 65 t==0 || pWal->re
1a62c 61 64 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 adLock==0 ){.
1a62d 20 2a 70 49 6e 57 61 6c 20 3d 20 30 3b 0a 20 20 *pInWal = 0;.
1a62e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1a62f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 OK;. }.. /* Se
1a630 61 72 63 68 20 74 68 65 20 68 61 73 68 20 74 61 arch the hash ta
1a631 62 6c 65 20 6f 72 20 74 61 62 6c 65 73 20 66 6f ble or tables fo
1a632 72 20 61 6e 20 65 6e 74 72 79 20 6d 61 74 63 68 r an entry match
1a633 69 6e 67 20 70 61 67 65 20 6e 75 6d 62 65 72 0a ing page number.
1a634 20 20 2a 2a 20 70 67 6e 6f 2e 20 45 61 63 68 20 ** pgno. Each
1a635 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 iteration of the
1a636 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 28 29 following for()
1a637 20 6c 6f 6f 70 20 73 65 61 72 63 68 65 73 20 6f loop searches o
1a638 6e 65 0a 20 20 2a 2a 20 68 61 73 68 20 74 61 62 ne. ** hash tab
1a639 6c 65 20 28 65 61 63 68 20 68 61 73 68 20 74 61 le (each hash ta
1a63a 62 6c 65 20 69 6e 64 65 78 65 73 20 75 70 20 74 ble indexes up t
1a63b 6f 20 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 o HASHTABLE_NPAG
1a63c 45 20 66 72 61 6d 65 73 29 2e 0a 20 20 2a 2a 0a E frames).. **.
1a63d 20 20 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 6d ** This code m
1a63e 69 67 68 74 20 72 75 6e 20 63 6f 6e 63 75 72 72 ight run concurr
1a63f 65 6e 74 6c 79 20 74 6f 20 74 68 65 20 63 6f 64 ently to the cod
1a640 65 20 69 6e 20 77 61 6c 49 6e 64 65 78 41 70 70 e in walIndexApp
1a641 65 6e 64 28 29 0a 20 20 2a 2a 20 74 68 61 74 20 end(). ** that
1a642 61 64 64 73 20 65 6e 74 72 69 65 73 20 74 6f 20 adds entries to
1a643 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 28 61 the wal-index (a
1a644 6e 64 20 70 6f 73 73 69 62 6c 79 20 74 6f 20 74 nd possibly to t
1a645 68 69 73 20 68 61 73 68 20 0a 20 20 2a 2a 20 74 his hash . ** t
1a646 61 62 6c 65 29 2e 20 54 68 69 73 20 6d 65 61 6e able). This mean
1a647 73 20 74 68 65 20 76 61 6c 75 65 20 6a 75 73 74 s the value just
1a648 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 68 read from the h
1a649 61 73 68 20 0a 20 20 2a 2a 20 73 6c 6f 74 20 28 ash . ** slot (
1a64a 61 48 61 73 68 5b 69 4b 65 79 5d 29 20 6d 61 79 aHash[iKey]) may
1a64b 20 68 61 76 65 20 62 65 65 6e 20 61 64 64 65 64 have been added
1a64c 20 62 65 66 6f 72 65 20 6f 72 20 61 66 74 65 72 before or after
1a64d 20 74 68 65 20 0a 20 20 2a 2a 20 63 75 72 72 65 the . ** curre
1a64e 6e 74 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 nt read transact
1a64f 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e 20 ion was opened.
1a650 56 61 6c 75 65 73 20 61 64 64 65 64 20 61 66 74 Values added aft
1a651 65 72 20 74 68 65 0a 20 20 2a 2a 20 72 65 61 64 er the. ** read
1a652 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 transaction was
1a653 20 6f 70 65 6e 65 64 20 6d 61 79 20 68 61 76 65 opened may have
1a654 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 69 6e been written in
1a655 63 6f 72 72 65 63 74 6c 79 20 2d 0a 20 20 2a 2a correctly -. **
1a656 20 69 2e 65 2e 20 74 68 65 73 65 20 73 6c 6f 74 i.e. these slot
1a657 73 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 67 61 s may contain ga
1a658 72 62 61 67 65 20 64 61 74 61 2e 20 48 6f 77 65 rbage data. Howe
1a659 76 65 72 2c 20 77 65 20 61 73 73 75 6d 65 0a 20 ver, we assume.
1a65a 20 2a 2a 20 74 68 61 74 20 61 6e 79 20 73 6c 6f ** that any slo
1a65b 74 73 20 77 72 69 74 74 65 6e 20 62 65 66 6f 72 ts written befor
1a65c 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 65 e the current re
1a65d 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 ad transaction w
1a65e 61 73 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 20 72 as. ** opened r
1a65f 65 6d 61 69 6e 20 75 6e 6d 6f 64 69 66 69 65 64 emain unmodified
1a660 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 .. **. ** For
1a661 74 68 65 20 72 65 61 73 6f 6e 73 20 61 62 6f 76 the reasons abov
1a662 65 2c 20 74 68 65 20 69 66 28 2e 2e 2e 29 20 63 e, the if(...) c
1a663 6f 6e 64 69 74 69 6f 6e 20 66 65 61 74 75 72 65 ondition feature
1a664 64 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 0a 20 d in the inner.
1a665 20 2a 2a 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 ** loop of the
1a666 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 following block
1a667 69 73 20 6d 6f 72 65 20 73 74 72 69 6e 67 65 6e is more stringen
1a668 74 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 t that would be
1a669 72 65 71 75 69 72 65 64 20 0a 20 20 2a 2a 20 69 required . ** i
1a66a 66 20 77 65 20 68 61 64 20 65 78 63 6c 75 73 69 f we had exclusi
1a66b 76 65 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 ve access to the
1a66c 20 68 61 73 68 2d 74 61 62 6c 65 3a 0a 20 20 2a hash-table:. *
1a66d 2a 0a 20 20 2a 2a 20 20 20 28 61 50 67 6e 6f 5b *. ** (aPgno[
1a66e 69 46 72 61 6d 65 5d 3d 3d 70 67 6e 6f 29 3a 20 iFrame]==pgno):
1a66f 0a 20 20 2a 2a 20 20 20 20 20 54 68 69 73 20 63 . ** This c
1a670 6f 6e 64 69 74 69 6f 6e 20 66 69 6c 74 65 72 73 ondition filters
1a671 20 6f 75 74 20 6e 6f 72 6d 61 6c 20 68 61 73 68 out normal hash
1a672 2d 74 61 62 6c 65 20 63 6f 6c 6c 69 73 69 6f 6e -table collision
1a673 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 s.. **. ** (
1a674 69 46 72 61 6d 65 3c 3d 69 4c 61 73 74 29 3a 20 iFrame<=iLast):
1a675 0a 20 20 2a 2a 20 20 20 20 20 54 68 69 73 20 63 . ** This c
1a676 6f 6e 64 69 74 69 6f 6e 20 66 69 6c 74 65 72 73 ondition filters
1a677 20 6f 75 74 20 65 6e 74 72 69 65 73 20 74 68 61 out entries tha
1a678 74 20 77 65 72 65 20 61 64 64 65 64 20 74 6f 20 t were added to
1a679 74 68 65 20 68 61 73 68 0a 20 20 2a 2a 20 20 20 the hash. **
1a67a 20 20 74 61 62 6c 65 20 61 66 74 65 72 20 74 68 table after th
1a67b 65 20 63 75 72 72 65 6e 74 20 72 65 61 64 2d 74 e current read-t
1a67c 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 64 20 73 ransaction had s
1a67d 74 61 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 66 tarted.. */. f
1a67e 6f 72 28 69 48 61 73 68 3d 77 61 6c 46 72 61 6d or(iHash=walFram
1a67f 65 50 61 67 65 28 69 4c 61 73 74 29 3b 20 69 48 ePage(iLast); iH
1a680 61 73 68 3e 3d 30 20 26 26 20 69 52 65 61 64 3d ash>=0 && iRead=
1a681 3d 30 3b 20 69 48 61 73 68 2d 2d 29 7b 0a 20 20 =0; iHash--){.
1a682 20 20 76 6f 6c 61 74 69 6c 65 20 68 74 5f 73 6c volatile ht_sl
1a683 6f 74 20 2a 61 48 61 73 68 3b 20 20 20 20 20 20 ot *aHash;
1a684 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 68 61 /* Pointer to ha
1a685 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 sh table */.
1a686 76 6f 6c 61 74 69 6c 65 20 75 33 32 20 2a 61 50 volatile u32 *aP
1a687 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 2f 2a gno; /*
1a688 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 72 72 61 Pointer to arra
1a689 79 20 6f 66 20 70 61 67 65 20 6e 75 6d 62 65 72 y of page number
1a68a 73 20 2a 2f 0a 20 20 20 20 75 33 32 20 69 5a 65 s */. u32 iZe
1a68b 72 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ro;
1a68c 20 20 20 20 20 20 20 2f 2a 20 46 72 61 6d 65 20 /* Frame
1a68d 6e 75 6d 62 65 72 20 63 6f 72 72 65 73 70 6f 6e number correspon
1a68e 64 69 6e 67 20 74 6f 20 61 50 67 6e 6f 5b 30 5d ding to aPgno[0]
1a68f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4b 65 79 */. int iKey
1a690 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1a691 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 73 6c /* Hash sl
1a692 6f 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 ot index */.
1a693 69 6e 74 20 6e 43 6f 6c 6c 69 64 65 3b 20 20 20 int nCollide;
1a694 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a695 20 4e 75 6d 62 65 72 20 6f 66 20 68 61 73 68 20 Number of hash
1a696 63 6f 6c 6c 69 73 69 6f 6e 73 20 72 65 6d 61 69 collisions remai
1a697 6e 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ning */. int
1a698 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1a699 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 /* Err
1a69a 6f 72 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 20 20 or code */..
1a69b 72 63 20 3d 20 77 61 6c 48 61 73 68 47 65 74 28 rc = walHashGet(
1a69c 70 57 61 6c 2c 20 69 48 61 73 68 2c 20 26 61 48 pWal, iHash, &aH
1a69d 61 73 68 2c 20 26 61 50 67 6e 6f 2c 20 26 69 5a ash, &aPgno, &iZ
1a69e 65 72 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ero);. if( rc
1a69f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1a6a0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1a6a1 20 20 20 20 7d 0a 20 20 20 20 6e 43 6f 6c 6c 69 }. nColli
1a6a2 64 65 20 3d 20 48 41 53 48 54 41 42 4c 45 5f 4e de = HASHTABLE_N
1a6a3 53 4c 4f 54 3b 0a 20 20 20 20 66 6f 72 28 69 4b SLOT;. for(iK
1a6a4 65 79 3d 77 61 6c 48 61 73 68 28 70 67 6e 6f 29 ey=walHash(pgno)
1a6a5 3b 20 61 48 61 73 68 5b 69 4b 65 79 5d 3b 20 69 ; aHash[iKey]; i
1a6a6 4b 65 79 3d 77 61 6c 4e 65 78 74 48 61 73 68 28 Key=walNextHash(
1a6a7 69 4b 65 79 29 29 7b 0a 20 20 20 20 20 20 75 33 iKey)){. u3
1a6a8 32 20 69 46 72 61 6d 65 20 3d 20 61 48 61 73 68 2 iFrame = aHash
1a6a9 5b 69 4b 65 79 5d 20 2b 20 69 5a 65 72 6f 3b 0a [iKey] + iZero;.
1a6aa 20 20 20 20 20 20 69 66 28 20 69 46 72 61 6d 65 if( iFrame
1a6ab 3c 3d 69 4c 61 73 74 20 26 26 20 61 50 67 6e 6f <=iLast && aPgno
1a6ac 5b 61 48 61 73 68 5b 69 4b 65 79 5d 5d 3d 3d 70 [aHash[iKey]]==p
1a6ad 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f gno ){. /
1a6ae 2a 20 61 73 73 65 72 74 28 20 69 46 72 61 6d 65 * assert( iFrame
1a6af 3e 69 52 65 61 64 20 29 3b 20 2d 2d 20 6e 6f 74 >iRead ); -- not
1a6b0 20 74 72 75 65 20 69 66 20 74 68 65 72 65 20 69 true if there i
1a6b1 73 20 63 6f 72 72 75 70 74 69 6f 6e 20 2a 2f 0a s corruption */.
1a6b2 20 20 20 20 20 20 20 20 69 52 65 61 64 20 3d 20 iRead =
1a6b3 69 46 72 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a iFrame;. }.
1a6b4 20 20 20 20 20 20 69 66 28 20 28 6e 43 6f 6c 6c if( (nColl
1a6b5 69 64 65 2d 2d 29 3d 3d 30 20 29 7b 0a 20 20 20 ide--)==0 ){.
1a6b6 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1a6b7 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1a6b8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1a6b9 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }..#ifdef SQLIT
1a6ba 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 E_ENABLE_EXPENSI
1a6bb 56 45 5f 41 53 53 45 52 54 0a 20 20 2f 2a 20 49 VE_ASSERT. /* I
1a6bc 66 20 65 78 70 65 6e 73 69 76 65 20 61 73 73 65 f expensive asse
1a6bd 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 rt() statements
1a6be 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 64 are available, d
1a6bf 6f 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 o a linear searc
1a6c0 68 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 77 61 h. ** of the wa
1a6c1 6c 2d 69 6e 64 65 78 20 66 69 6c 65 20 63 6f 6e l-index file con
1a6c2 74 65 6e 74 2e 20 4d 61 6b 65 20 73 75 72 65 20 tent. Make sure
1a6c3 74 68 65 20 72 65 73 75 6c 74 73 20 61 67 72 65 the results agre
1a6c4 65 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 e with the. **
1a6c5 72 65 73 75 6c 74 20 6f 62 74 61 69 6e 65 64 20 result obtained
1a6c6 75 73 69 6e 67 20 74 68 65 20 68 61 73 68 20 69 using the hash i
1a6c7 6e 64 65 78 65 73 20 61 62 6f 76 65 2e 20 20 2a ndexes above. *
1a6c8 2f 0a 20 20 7b 0a 20 20 20 20 75 33 32 20 69 52 /. {. u32 iR
1a6c9 65 61 64 32 20 3d 20 30 3b 0a 20 20 20 20 75 33 ead2 = 0;. u3
1a6ca 32 20 69 54 65 73 74 3b 0a 20 20 20 20 66 6f 72 2 iTest;. for
1a6cb 28 69 54 65 73 74 3d 69 4c 61 73 74 3b 20 69 54 (iTest=iLast; iT
1a6cc 65 73 74 3e 30 3b 20 69 54 65 73 74 2d 2d 29 7b est>0; iTest--){
1a6cd 0a 20 20 20 20 20 20 69 66 28 20 77 61 6c 46 72 . if( walFr
1a6ce 61 6d 65 50 67 6e 6f 28 70 57 61 6c 2c 20 69 54 amePgno(pWal, iT
1a6cf 65 73 74 29 3d 3d 70 67 6e 6f 20 29 7b 0a 20 20 est)==pgno ){.
1a6d0 20 20 20 20 20 20 69 52 65 61 64 32 20 3d 20 69 iRead2 = i
1a6d1 54 65 73 74 3b 0a 20 20 20 20 20 20 20 20 62 72 Test;. br
1a6d2 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
1a6d3 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 }. assert( i
1a6d4 52 65 61 64 3d 3d 69 52 65 61 64 32 20 29 3b 0a Read==iRead2 );.
1a6d5 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
1a6d6 20 49 66 20 69 52 65 61 64 20 69 73 20 6e 6f 6e If iRead is non
1a6d7 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69 -zero, then it i
1a6d8 73 20 74 68 65 20 6c 6f 67 20 66 72 61 6d 65 20 s the log frame
1a6d9 6e 75 6d 62 65 72 20 74 68 61 74 20 63 6f 6e 74 number that cont
1a6da 61 69 6e 73 20 74 68 65 0a 20 20 2a 2a 20 72 65 ains the. ** re
1a6db 71 75 69 72 65 64 20 70 61 67 65 2e 20 52 65 61 quired page. Rea
1a6dc 64 20 61 6e 64 20 72 65 74 75 72 6e 20 64 61 74 d and return dat
1a6dd 61 20 66 72 6f 6d 20 74 68 65 20 6c 6f 67 20 66 a from the log f
1a6de 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ile.. */. if(
1a6df 69 52 65 61 64 20 29 7b 0a 20 20 20 20 69 6e 74 iRead ){. int
1a6e0 20 73 7a 3b 0a 20 20 20 20 69 36 34 20 69 4f 66 sz;. i64 iOf
1a6e1 66 73 65 74 3b 0a 20 20 20 20 73 7a 20 3d 20 70 fset;. sz = p
1a6e2 57 61 6c 2d 3e 68 64 72 2e 73 7a 50 61 67 65 3b Wal->hdr.szPage;
1a6e3 0a 20 20 20 20 73 7a 20 3d 20 28 73 7a 26 30 78 . sz = (sz&0x
1a6e4 66 65 30 30 29 20 2b 20 28 28 73 7a 26 30 78 30 fe00) + ((sz&0x0
1a6e5 30 30 31 29 3c 3c 31 36 29 3b 0a 20 20 20 20 74 001)<<16);. t
1a6e6 65 73 74 63 61 73 65 28 20 73 7a 3c 3d 33 32 37 estcase( sz<=327
1a6e7 36 38 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 68 );. testca
1a6e8 73 65 28 20 73 7a 3e 3d 36 35 35 33 36 20 29 3b se( sz>=65536 );
1a6e9 0a 20 20 20 20 69 4f 66 66 73 65 74 20 3d 20 77 . iOffset = w
1a6ea 61 6c 46 72 61 6d 65 4f 66 66 73 65 74 28 69 52 alFrameOffset(iR
1a6eb 65 61 64 2c 20 73 7a 29 20 2b 20 57 41 4c 5f 46 ead, sz) + WAL_F
1a6ec 52 41 4d 45 5f 48 44 52 53 49 5a 45 3b 0a 20 20 RAME_HDRSIZE;.
1a6ed 20 20 2a 70 49 6e 57 61 6c 20 3d 20 31 3b 0a 20 *pInWal = 1;.
1a6ee 20 20 20 2f 2a 20 74 65 73 74 63 61 73 65 28 20 /* testcase(
1a6ef 49 53 5f 42 49 47 5f 49 4e 54 28 69 4f 66 66 73 IS_BIG_INT(iOffs
1a6f0 65 74 29 20 29 3b 20 2f 2f 20 72 65 71 75 69 72 et) ); // requir
1a6f1 65 73 20 61 20 34 47 69 42 20 57 41 4c 20 2a 2f es a 4GiB WAL */
1a6f2 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 . return sqli
1a6f3 74 65 33 4f 73 52 65 61 64 28 70 57 61 6c 2d 3e te3OsRead(pWal->
1a6f4 70 57 61 6c 46 64 2c 20 70 4f 75 74 2c 20 28 6e pWalFd, pOut, (n
1a6f5 4f 75 74 3e 73 7a 20 3f 20 73 7a 20 3a 20 6e 4f Out>sz ? sz : nO
1a6f6 75 74 29 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 ut), iOffset);.
1a6f7 20 7d 0a 0a 20 20 2a 70 49 6e 57 61 6c 20 3d 20 }.. *pInWal =
1a6f8 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 0;. return SQLI
1a6f9 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 20 0a 2a TE_OK;.}.../* .*
1a6fa 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a * Return the siz
1a6fb 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
1a6fc 65 20 69 6e 20 70 61 67 65 73 20 28 6f 72 20 7a e in pages (or z
1a6fd 65 72 6f 2c 20 69 66 20 75 6e 6b 6e 6f 77 6e 29 ero, if unknown)
1a6fe 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a6ff 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 ATE Pgno sqlite3
1a700 57 61 6c 44 62 73 69 7a 65 28 57 61 6c 20 2a 70 WalDbsize(Wal *p
1a701 57 61 6c 29 7b 0a 20 20 69 66 28 20 70 57 61 6c Wal){. if( pWal
1a702 20 26 26 20 41 4c 57 41 59 53 28 70 57 61 6c 2d && ALWAYS(pWal-
1a703 3e 72 65 61 64 4c 6f 63 6b 3e 3d 30 29 20 29 7b >readLock>=0) ){
1a704 0a 20 20 20 20 72 65 74 75 72 6e 20 70 57 61 6c . return pWal
1a705 2d 3e 68 64 72 2e 6e 50 61 67 65 3b 0a 20 20 7d ->hdr.nPage;. }
1a706 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
1a707 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e ./* .** This fun
1a708 63 74 69 6f 6e 20 73 74 61 72 74 73 20 61 20 77 ction starts a w
1a709 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
1a70a 20 6f 6e 20 74 68 65 20 57 41 4c 2e 0a 2a 2a 0a on the WAL..**.
1a70b 2a 2a 20 41 20 72 65 61 64 20 74 72 61 6e 73 61 ** A read transa
1a70c 63 74 69 6f 6e 20 6d 75 73 74 20 68 61 76 65 20 ction must have
1a70d 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73 74 61 already been sta
1a70e 72 74 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 rted by a prior
1a70f 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 call.** to sqlit
1a710 65 33 57 61 6c 42 65 67 69 6e 52 65 61 64 54 72 e3WalBeginReadTr
1a711 61 6e 73 61 63 74 69 6f 6e 28 29 2e 0a 2a 2a 0a ansaction()..**.
1a712 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 ** If another th
1a713 72 65 61 64 20 6f 72 20 70 72 6f 63 65 73 73 20 read or process
1a714 68 61 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f has written into
1a715 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69 the database si
1a716 6e 63 65 0a 2a 2a 20 74 68 65 20 72 65 61 64 20 nce.** the read
1a717 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 transaction was
1a718 73 74 61 72 74 65 64 2c 20 74 68 65 6e 20 69 74 started, then it
1a719 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
1a71a 20 66 6f 72 20 74 68 69 73 0a 2a 2a 20 74 68 72 for this.** thr
1a71b 65 61 64 20 74 6f 20 77 72 69 74 65 20 61 73 20 ead to write as
1a71c 64 6f 69 6e 67 20 73 6f 20 77 6f 75 6c 64 20 63 doing so would c
1a71d 61 75 73 65 20 61 20 66 6f 72 6b 2e 20 20 53 6f ause a fork. So
1a71e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
1a71f 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
1a720 42 55 53 59 20 69 6e 20 74 68 61 74 20 63 61 73 BUSY in that cas
1a721 65 20 61 6e 64 20 6e 6f 20 77 72 69 74 65 20 74 e and no write t
1a722 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 ransaction is st
1a723 61 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 arted..**.** The
1a724 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 61 re can only be a
1a725 20 73 69 6e 67 6c 65 20 77 72 69 74 65 72 20 61 single writer a
1a726 63 74 69 76 65 20 61 74 20 61 20 74 69 6d 65 2e ctive at a time.
1a727 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a728 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 TE int sqlite3Wa
1a729 6c 42 65 67 69 6e 57 72 69 74 65 54 72 61 6e 73 lBeginWriteTrans
1a72a 61 63 74 69 6f 6e 28 57 61 6c 20 2a 70 57 61 6c action(Wal *pWal
1a72b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 ){. int rc;..
1a72c 2f 2a 20 43 61 6e 6e 6f 74 20 73 74 61 72 74 20 /* Cannot start
1a72d 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 a write transact
1a72e 69 6f 6e 20 77 69 74 68 6f 75 74 20 66 69 72 73 ion without firs
1a72f 74 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61 64 t holding a read
1a730 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f . ** transactio
1a731 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 n. */. assert(
1a732 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 3e 3d pWal->readLock>=
1a733 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 57 61 6c 0 );.. if( pWal
1a734 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 ->readOnly ){.
1a735 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1a736 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 READONLY;. }..
1a737 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 77 72 69 /* Only one wri
1a738 74 65 72 20 61 6c 6c 6f 77 65 64 20 61 74 20 61 ter allowed at a
1a739 20 74 69 6d 65 2e 20 20 47 65 74 20 74 68 65 20 time. Get the
1a73a 77 72 69 74 65 20 6c 6f 63 6b 2e 20 20 52 65 74 write lock. Ret
1a73b 75 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f urn. ** SQLITE_
1a73c 42 55 53 59 20 69 66 20 75 6e 61 62 6c 65 2e 0a BUSY if unable..
1a73d 20 20 2a 2f 0a 20 20 72 63 20 3d 20 77 61 6c 4c */. rc = walL
1a73e 6f 63 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61 ockExclusive(pWa
1a73f 6c 2c 20 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43 l, WAL_WRITE_LOC
1a740 4b 2c 20 31 29 3b 0a 20 20 69 66 28 20 72 63 20 K, 1);. if( rc
1a741 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
1a742 3b 0a 20 20 7d 0a 20 20 70 57 61 6c 2d 3e 77 72 ;. }. pWal->wr
1a743 69 74 65 4c 6f 63 6b 20 3d 20 31 3b 0a 0a 20 20 iteLock = 1;..
1a744 2f 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 63 6f /* If another co
1a745 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 77 72 69 nnection has wri
1a746 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 tten to the data
1a747 62 61 73 65 20 66 69 6c 65 20 73 69 6e 63 65 20 base file since
1a748 74 68 65 0a 20 20 2a 2a 20 74 69 6d 65 20 74 68 the. ** time th
1a749 65 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 e read transacti
1a74a 6f 6e 20 6f 6e 20 74 68 69 73 20 63 6f 6e 6e 65 on on this conne
1a74b 63 74 69 6f 6e 20 77 61 73 20 73 74 61 72 74 65 ction was starte
1a74c 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65 d, then. ** the
1a74d 20 77 72 69 74 65 20 69 73 20 64 69 73 61 6c 6c write is disall
1a74e 6f 77 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 owed.. */. if(
1a74f 20 6d 65 6d 63 6d 70 28 26 70 57 61 6c 2d 3e 68 memcmp(&pWal->h
1a750 64 72 2c 20 28 76 6f 69 64 20 2a 29 77 61 6c 49 dr, (void *)walI
1a751 6e 64 65 78 48 64 72 28 70 57 61 6c 29 2c 20 73 ndexHdr(pWal), s
1a752 69 7a 65 6f 66 28 57 61 6c 49 6e 64 65 78 48 64 izeof(WalIndexHd
1a753 72 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 77 61 r))!=0 ){. wa
1a754 6c 55 6e 6c 6f 63 6b 45 78 63 6c 75 73 69 76 65 lUnlockExclusive
1a755 28 70 57 61 6c 2c 20 57 41 4c 5f 57 52 49 54 45 (pWal, WAL_WRITE
1a756 5f 4c 4f 43 4b 2c 20 31 29 3b 0a 20 20 20 20 70 _LOCK, 1);. p
1a757 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20 3d Wal->writeLock =
1a758 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 0;. rc = SQL
1a759 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 ITE_BUSY;. }..
1a75a 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1a75b 2a 0a 2a 2a 20 45 6e 64 20 61 20 77 72 69 74 65 *.** End a write
1a75c 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 transaction. T
1a75d 68 65 20 63 6f 6d 6d 69 74 20 68 61 73 20 61 6c he commit has al
1a75e 72 65 61 64 79 20 62 65 65 6e 20 64 6f 6e 65 2e ready been done.
1a75f 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e This.** routin
1a760 65 20 6d 65 72 65 6c 79 20 72 65 6c 65 61 73 65 e merely release
1a761 73 20 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 s the lock..*/.S
1a762 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1a763 74 20 73 71 6c 69 74 65 33 57 61 6c 45 6e 64 57 t sqlite3WalEndW
1a764 72 69 74 65 54 72 61 6e 73 61 63 74 69 6f 6e 28 riteTransaction(
1a765 57 61 6c 20 2a 70 57 61 6c 29 7b 0a 20 20 69 66 Wal *pWal){. if
1a766 28 20 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 ( pWal->writeLoc
1a767 6b 20 29 7b 0a 20 20 20 20 77 61 6c 55 6e 6c 6f k ){. walUnlo
1a768 63 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61 6c ckExclusive(pWal
1a769 2c 20 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43 4b , WAL_WRITE_LOCK
1a76a 2c 20 31 29 3b 0a 20 20 20 20 70 57 61 6c 2d 3e , 1);. pWal->
1a76b 77 72 69 74 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 writeLock = 0;.
1a76c 20 20 20 70 57 61 6c 2d 3e 74 72 75 6e 63 61 74 pWal->truncat
1a76d 65 4f 6e 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 eOnCommit = 0;.
1a76e 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
1a76f 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
1a770 49 66 20 61 6e 79 20 64 61 74 61 20 68 61 73 20 If any data has
1a771 62 65 65 6e 20 77 72 69 74 74 65 6e 20 28 62 75 been written (bu
1a772 74 20 6e 6f 74 20 63 6f 6d 6d 69 74 74 65 64 29 t not committed)
1a773 20 74 6f 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 to the log file
1a774 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 , this.** functi
1a775 6f 6e 20 6d 6f 76 65 73 20 74 68 65 20 77 72 69 on moves the wri
1a776 74 65 2d 70 6f 69 6e 74 65 72 20 62 61 63 6b 20 te-pointer back
1a777 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 to the start of
1a778 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
1a779 0a 2a 2a 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 .**.** Additiona
1a77a 6c 6c 79 2c 20 74 68 65 20 63 61 6c 6c 62 61 63 lly, the callbac
1a77b 6b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e k function is in
1a77c 76 6f 6b 65 64 20 66 6f 72 20 65 61 63 68 20 66 voked for each f
1a77d 72 61 6d 65 20 77 72 69 74 74 65 6e 0a 2a 2a 20 rame written.**
1a77e 74 6f 20 74 68 65 20 57 41 4c 20 73 69 6e 63 65 to the WAL since
1a77f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
1a780 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 e transaction. I
1a781 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 f the callback r
1a782 65 74 75 72 6e 73 0a 2a 2a 20 6f 74 68 65 72 20 eturns.** other
1a783 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 than SQLITE_OK,
1a784 69 74 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 it is not invoke
1a785 64 20 61 67 61 69 6e 20 61 6e 64 20 74 68 65 20 d again and the
1a786 65 72 72 6f 72 20 63 6f 64 65 20 69 73 0a 2a 2a error code is.**
1a787 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
1a788 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f caller..**.** O
1a789 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 therwise, if the
1a78a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 callback functi
1a78b 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 72 65 74 75 on does not retu
1a78c 72 6e 20 61 6e 20 65 72 72 6f 72 2c 20 74 68 69 rn an error, thi
1a78d 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 s.** function re
1a78e 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e turns SQLITE_OK.
1a78f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a790 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 TE int sqlite3Wa
1a791 6c 55 6e 64 6f 28 57 61 6c 20 2a 70 57 61 6c 2c lUndo(Wal *pWal,
1a792 20 69 6e 74 20 28 2a 78 55 6e 64 6f 29 28 76 6f int (*xUndo)(vo
1a793 69 64 20 2a 2c 20 50 67 6e 6f 29 2c 20 76 6f 69 id *, Pgno), voi
1a794 64 20 2a 70 55 6e 64 6f 43 74 78 29 7b 0a 20 20 d *pUndoCtx){.
1a795 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1a796 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 OK;. if( ALWAYS
1a797 28 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b (pWal->writeLock
1a798 29 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4d ) ){. Pgno iM
1a799 61 78 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 6d ax = pWal->hdr.m
1a79a 78 46 72 61 6d 65 3b 0a 20 20 20 20 50 67 6e 6f xFrame;. Pgno
1a79b 20 69 46 72 61 6d 65 3b 0a 20 20 0a 20 20 20 20 iFrame;. .
1a79c 2f 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 63 /* Restore the c
1a79d 6c 69 65 6e 74 73 20 63 61 63 68 65 20 6f 66 20 lients cache of
1a79e 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 the wal-index he
1a79f 61 64 65 72 20 74 6f 20 74 68 65 20 73 74 61 74 ader to the stat
1a7a0 65 20 69 74 0a 20 20 20 20 2a 2a 20 77 61 73 20 e it. ** was
1a7a1 69 6e 20 62 65 66 6f 72 65 20 74 68 65 20 63 6c in before the cl
1a7a2 69 65 6e 74 20 62 65 67 61 6e 20 77 72 69 74 69 ient began writi
1a7a3 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ng to the databa
1a7a4 73 65 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 se. . */.
1a7a5 6d 65 6d 63 70 79 28 26 70 57 61 6c 2d 3e 68 64 memcpy(&pWal->hd
1a7a6 72 2c 20 28 76 6f 69 64 20 2a 29 77 61 6c 49 6e r, (void *)walIn
1a7a7 64 65 78 48 64 72 28 70 57 61 6c 29 2c 20 73 69 dexHdr(pWal), si
1a7a8 7a 65 6f 66 28 57 61 6c 49 6e 64 65 78 48 64 72 zeof(WalIndexHdr
1a7a9 29 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 46 72 ));.. for(iFr
1a7aa 61 6d 65 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 ame=pWal->hdr.mx
1a7ab 46 72 61 6d 65 2b 31 3b 20 0a 20 20 20 20 20 20 Frame+1; .
1a7ac 20 20 41 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c ALWAYS(rc==SQL
1a7ad 49 54 45 5f 4f 4b 29 20 26 26 20 69 46 72 61 6d ITE_OK) && iFram
1a7ae 65 3c 3d 69 4d 61 78 3b 20 0a 20 20 20 20 20 20 e<=iMax; .
1a7af 20 20 69 46 72 61 6d 65 2b 2b 0a 20 20 20 20 29 iFrame++. )
1a7b0 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 {. /* This
1a7b1 63 61 6c 6c 20 63 61 6e 6e 6f 74 20 66 61 69 6c call cannot fail
1a7b2 2e 20 55 6e 6c 65 73 73 20 74 68 65 20 70 61 67 . Unless the pag
1a7b3 65 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 e for which the
1a7b4 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 page number.
1a7b5 20 20 2a 2a 20 69 73 20 70 61 73 73 65 64 20 61 ** is passed a
1a7b6 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 s the second arg
1a7b7 75 6d 65 6e 74 20 69 73 20 28 61 29 20 69 6e 20 ument is (a) in
1a7b8 74 68 65 20 63 61 63 68 65 20 61 6e 64 20 0a 20 the cache and .
1a7b9 20 20 20 20 20 2a 2a 20 28 62 29 20 68 61 73 20 ** (b) has
1a7ba 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 an outstanding r
1a7bb 65 66 65 72 65 6e 63 65 2c 20 74 68 65 6e 20 78 eference, then x
1a7bc 55 6e 64 6f 20 69 73 20 65 69 74 68 65 72 20 61 Undo is either a
1a7bd 20 6e 6f 2d 6f 70 0a 20 20 20 20 20 20 2a 2a 20 no-op. **
1a7be 28 69 66 20 28 61 29 20 69 73 20 66 61 6c 73 65 (if (a) is false
1a7bf 29 20 6f 72 20 73 69 6d 70 6c 79 20 65 78 70 65 ) or simply expe
1a7c0 6c 73 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d ls the page from
1a7c1 20 74 68 65 20 63 61 63 68 65 20 28 69 66 20 28 the cache (if (
1a7c2 62 29 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 66 b). ** is f
1a7c3 61 6c 73 65 29 2e 0a 20 20 20 20 20 20 2a 2a 0a alse).. **.
1a7c4 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 ** If the
1a7c5 75 70 70 65 72 20 6c 61 79 65 72 20 69 73 20 64 upper layer is d
1a7c6 6f 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c oing a rollback,
1a7c7 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 it is guarantee
1a7c8 64 20 74 68 61 74 20 74 68 65 72 65 0a 20 20 20 d that there.
1a7c9 20 20 20 2a 2a 20 61 72 65 20 6e 6f 20 6f 75 74 ** are no out
1a7ca 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e standing referen
1a7cb 63 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 20 ces to any page
1a7cc 6f 74 68 65 72 20 74 68 61 6e 20 70 61 67 65 20 other than page
1a7cd 31 2e 20 41 6e 64 0a 20 20 20 20 20 20 2a 2a 20 1. And. **
1a7ce 70 61 67 65 20 31 20 69 73 20 6e 65 76 65 72 20 page 1 is never
1a7cf 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6c written to the l
1a7d0 6f 67 20 75 6e 74 69 6c 20 74 68 65 20 74 72 61 og until the tra
1a7d1 6e 73 61 63 74 69 6f 6e 20 69 73 0a 20 20 20 20 nsaction is.
1a7d2 20 20 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 ** committed.
1a7d3 41 73 20 61 20 72 65 73 75 6c 74 2c 20 74 68 65 As a result, the
1a7d4 20 63 61 6c 6c 20 74 6f 20 78 55 6e 64 6f 20 6d call to xUndo m
1a7d5 61 79 20 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 20 ay not fail..
1a7d6 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 */. asse
1a7d7 72 74 28 20 77 61 6c 46 72 61 6d 65 50 67 6e 6f rt( walFramePgno
1a7d8 28 70 57 61 6c 2c 20 69 46 72 61 6d 65 29 21 3d (pWal, iFrame)!=
1a7d9 31 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 1 );. rc =
1a7da 78 55 6e 64 6f 28 70 55 6e 64 6f 43 74 78 2c 20 xUndo(pUndoCtx,
1a7db 77 61 6c 46 72 61 6d 65 50 67 6e 6f 28 70 57 61 walFramePgno(pWa
1a7dc 6c 2c 20 69 46 72 61 6d 65 29 29 3b 0a 20 20 20 l, iFrame));.
1a7dd 20 7d 0a 20 20 20 20 69 66 28 20 69 4d 61 78 21 }. if( iMax!
1a7de 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 =pWal->hdr.mxFra
1a7df 6d 65 20 29 20 77 61 6c 43 6c 65 61 6e 75 70 48 me ) walCleanupH
1a7e0 61 73 68 28 70 57 61 6c 29 3b 0a 20 20 7d 0a 20 ash(pWal);. }.
1a7e1 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
1a7e2 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 65 74 75 ITE_OK );. retu
1a7e3 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a rn rc;.}../* .**
1a7e4 20 41 72 67 75 6d 65 6e 74 20 61 57 61 6c 44 61 Argument aWalDa
1a7e5 74 61 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f ta must point to
1a7e6 20 61 6e 20 61 72 72 61 79 20 6f 66 20 57 41 4c an array of WAL
1a7e7 5f 53 41 56 45 50 4f 49 4e 54 5f 4e 44 41 54 41 _SAVEPOINT_NDATA
1a7e8 20 75 33 32 20 0a 2a 2a 20 76 61 6c 75 65 73 2e u32 .** values.
1a7e9 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 70 This function p
1a7ea 6f 70 75 6c 61 74 65 73 20 74 68 65 20 61 72 72 opulates the arr
1a7eb 61 79 20 77 69 74 68 20 76 61 6c 75 65 73 20 72 ay with values r
1a7ec 65 71 75 69 72 65 64 20 74 6f 20 0a 2a 2a 20 22 equired to .** "
1a7ed 72 6f 6c 6c 62 61 63 6b 22 20 74 68 65 20 77 72 rollback" the wr
1a7ee 69 74 65 20 70 6f 73 69 74 69 6f 6e 20 6f 66 20 ite position of
1a7ef 74 68 65 20 57 41 4c 20 68 61 6e 64 6c 65 20 62 the WAL handle b
1a7f0 61 63 6b 20 74 6f 20 74 68 65 20 63 75 72 72 65 ack to the curre
1a7f1 6e 74 20 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 nt .** point in
1a7f2 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 73 the event of a s
1a7f3 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 avepoint rollbac
1a7f4 6b 20 28 76 69 61 20 57 61 6c 53 61 76 65 70 6f k (via WalSavepo
1a7f5 69 6e 74 55 6e 64 6f 28 29 29 2e 0a 2a 2f 0a 53 intUndo())..*/.S
1a7f6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1a7f7 69 64 20 73 71 6c 69 74 65 33 57 61 6c 53 61 76 id sqlite3WalSav
1a7f8 65 70 6f 69 6e 74 28 57 61 6c 20 2a 70 57 61 6c epoint(Wal *pWal
1a7f9 2c 20 75 33 32 20 2a 61 57 61 6c 44 61 74 61 29 , u32 *aWalData)
1a7fa 7b 0a 20 20 61 73 73 65 72 74 28 20 70 57 61 6c {. assert( pWal
1a7fb 2d 3e 77 72 69 74 65 4c 6f 63 6b 20 29 3b 0a 20 ->writeLock );.
1a7fc 20 61 57 61 6c 44 61 74 61 5b 30 5d 20 3d 20 70 aWalData[0] = p
1a7fd 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 Wal->hdr.mxFrame
1a7fe 3b 0a 20 20 61 57 61 6c 44 61 74 61 5b 31 5d 20 ;. aWalData[1]
1a7ff 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72 61 = pWal->hdr.aFra
1a800 6d 65 43 6b 73 75 6d 5b 30 5d 3b 0a 20 20 61 57 meCksum[0];. aW
1a801 61 6c 44 61 74 61 5b 32 5d 20 3d 20 70 57 61 6c alData[2] = pWal
1a802 2d 3e 68 64 72 2e 61 46 72 61 6d 65 43 6b 73 75 ->hdr.aFrameCksu
1a803 6d 5b 31 5d 3b 0a 20 20 61 57 61 6c 44 61 74 61 m[1];. aWalData
1a804 5b 33 5d 20 3d 20 70 57 61 6c 2d 3e 6e 43 6b 70 [3] = pWal->nCkp
1a805 74 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 4d 6f 76 t;.}../* .** Mov
1a806 65 20 74 68 65 20 77 72 69 74 65 20 70 6f 73 69 e the write posi
1a807 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 41 4c 20 tion of the WAL
1a808 62 61 63 6b 20 74 6f 20 74 68 65 20 70 6f 69 6e back to the poin
1a809 74 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 0a t identified by.
1a80a 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e ** the values in
1a80b 20 74 68 65 20 61 57 61 6c 44 61 74 61 5b 5d 20 the aWalData[]
1a80c 61 72 72 61 79 2e 20 61 57 61 6c 44 61 74 61 20 array. aWalData
1a80d 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e must point to an
1a80e 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 57 41 4c array.** of WAL
1a80f 5f 53 41 56 45 50 4f 49 4e 54 5f 4e 44 41 54 41 _SAVEPOINT_NDATA
1a810 20 75 33 32 20 76 61 6c 75 65 73 20 74 68 61 74 u32 values that
1a811 20 68 61 73 20 62 65 65 6e 20 70 72 65 76 69 6f has been previo
1a812 75 73 6c 79 20 70 6f 70 75 6c 61 74 65 64 0a 2a usly populated.*
1a813 2a 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 57 * by a call to W
1a814 61 6c 53 61 76 65 70 6f 69 6e 74 28 29 2e 0a 2a alSavepoint()..*
1a815 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a816 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 53 int sqlite3WalS
1a817 61 76 65 70 6f 69 6e 74 55 6e 64 6f 28 57 61 6c avepointUndo(Wal
1a818 20 2a 70 57 61 6c 2c 20 75 33 32 20 2a 61 57 61 *pWal, u32 *aWa
1a819 6c 44 61 74 61 29 7b 0a 20 20 69 6e 74 20 72 63 lData){. int rc
1a81a 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
1a81b 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 77 assert( pWal->w
1a81c 72 69 74 65 4c 6f 63 6b 20 29 3b 0a 20 20 61 73 riteLock );. as
1a81d 73 65 72 74 28 20 61 57 61 6c 44 61 74 61 5b 33 sert( aWalData[3
1a81e 5d 21 3d 70 57 61 6c 2d 3e 6e 43 6b 70 74 20 7c ]!=pWal->nCkpt |
1a81f 7c 20 61 57 61 6c 44 61 74 61 5b 30 5d 3c 3d 70 | aWalData[0]<=p
1a820 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 Wal->hdr.mxFrame
1a821 20 29 3b 0a 0a 20 20 69 66 28 20 61 57 61 6c 44 );.. if( aWalD
1a822 61 74 61 5b 33 5d 21 3d 70 57 61 6c 2d 3e 6e 43 ata[3]!=pWal->nC
1a823 6b 70 74 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 kpt ){. /* Th
1a824 69 73 20 73 61 76 65 70 6f 69 6e 74 20 77 61 73 is savepoint was
1a825 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64 69 61 74 opened immediat
1a826 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 77 72 ely after the wr
1a827 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a ite-transaction.
1a828 20 20 20 20 2a 2a 20 77 61 73 20 73 74 61 72 74 ** was start
1a829 65 64 2e 20 52 69 67 68 74 20 61 66 74 65 72 20 ed. Right after
1a82a 74 68 61 74 2c 20 74 68 65 20 77 72 69 74 65 72 that, the writer
1a82b 20 64 65 63 69 64 65 64 20 74 6f 20 77 72 61 70 decided to wrap
1a82c 20 61 72 6f 75 6e 64 0a 20 20 20 20 2a 2a 20 74 around. ** t
1a82d 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 o the start of t
1a82e 68 65 20 6c 6f 67 2e 20 55 70 64 61 74 65 20 74 he log. Update t
1a82f 68 65 20 73 61 76 65 70 6f 69 6e 74 20 76 61 6c he savepoint val
1a830 75 65 73 20 74 6f 20 6d 61 74 63 68 2e 0a 20 20 ues to match..
1a831 20 20 2a 2f 0a 20 20 20 20 61 57 61 6c 44 61 74 */. aWalDat
1a832 61 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 61 57 a[0] = 0;. aW
1a833 61 6c 44 61 74 61 5b 33 5d 20 3d 20 70 57 61 6c alData[3] = pWal
1a834 2d 3e 6e 43 6b 70 74 3b 0a 20 20 7d 0a 0a 20 20 ->nCkpt;. }..
1a835 69 66 28 20 61 57 61 6c 44 61 74 61 5b 30 5d 3c if( aWalData[0]<
1a836 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d pWal->hdr.mxFram
1a837 65 20 29 7b 0a 20 20 20 20 70 57 61 6c 2d 3e 68 e ){. pWal->h
1a838 64 72 2e 6d 78 46 72 61 6d 65 20 3d 20 61 57 61 dr.mxFrame = aWa
1a839 6c 44 61 74 61 5b 30 5d 3b 0a 20 20 20 20 70 57 lData[0];. pW
1a83a 61 6c 2d 3e 68 64 72 2e 61 46 72 61 6d 65 43 6b al->hdr.aFrameCk
1a83b 73 75 6d 5b 30 5d 20 3d 20 61 57 61 6c 44 61 74 sum[0] = aWalDat
1a83c 61 5b 31 5d 3b 0a 20 20 20 20 70 57 61 6c 2d 3e a[1];. pWal->
1a83d 68 64 72 2e 61 46 72 61 6d 65 43 6b 73 75 6d 5b hdr.aFrameCksum[
1a83e 31 5d 20 3d 20 61 57 61 6c 44 61 74 61 5b 32 5d 1] = aWalData[2]
1a83f 3b 0a 20 20 20 20 77 61 6c 43 6c 65 61 6e 75 70 ;. walCleanup
1a840 48 61 73 68 28 70 57 61 6c 29 3b 0a 20 20 7d 0a Hash(pWal);. }.
1a841 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1a842 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
1a843 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
1a844 6a 75 73 74 20 62 65 66 6f 72 65 20 77 72 69 74 just before writ
1a845 69 6e 67 20 61 20 73 65 74 20 6f 66 20 66 72 61 ing a set of fra
1a846 6d 65 73 20 74 6f 20 74 68 65 20 6c 6f 67 0a 2a mes to the log.*
1a847 2a 20 66 69 6c 65 20 28 73 65 65 20 73 71 6c 69 * file (see sqli
1a848 74 65 33 57 61 6c 46 72 61 6d 65 73 28 29 29 2e te3WalFrames()).
1a849 20 49 74 20 63 68 65 63 6b 73 20 74 6f 20 73 65 It checks to se
1a84a 65 20 69 66 2c 20 69 6e 73 74 65 61 64 20 6f 66 e if, instead of
1a84b 20 61 70 70 65 6e 64 69 6e 67 0a 2a 2a 20 74 6f appending.** to
1a84c 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 67 the current log
1a84d 20 66 69 6c 65 2c 20 69 74 20 69 73 20 70 6f 73 file, it is pos
1a84e 73 69 62 6c 65 20 74 6f 20 6f 76 65 72 77 72 69 sible to overwri
1a84f 74 65 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 te the start of
1a850 74 68 65 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 the.** existing
1a851 6c 6f 67 20 66 69 6c 65 20 77 69 74 68 20 74 68 log file with th
1a852 65 20 6e 65 77 20 66 72 61 6d 65 73 20 28 69 2e e new frames (i.
1a853 65 2e 20 22 72 65 73 65 74 22 20 74 68 65 20 6c e. "reset" the l
1a854 6f 67 29 2e 20 49 66 20 73 6f 2c 0a 2a 2a 20 69 og). If so,.** i
1a855 74 20 73 65 74 73 20 70 57 61 6c 2d 3e 68 64 72 t sets pWal->hdr
1a856 2e 6d 78 46 72 61 6d 65 20 74 6f 20 30 2e 20 4f .mxFrame to 0. O
1a857 74 68 65 72 77 69 73 65 2c 20 70 57 61 6c 2d 3e therwise, pWal->
1a858 68 64 72 2e 6d 78 46 72 61 6d 65 20 69 73 20 6c hdr.mxFrame is l
1a859 65 66 74 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 eft.** unchanged
1a85a 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f ..**.** SQLITE_O
1a85b 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 K is returned if
1a85c 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 65 6e 63 no error is enc
1a85d 6f 75 6e 74 65 72 65 64 20 28 72 65 67 61 72 64 ountered (regard
1a85e 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 0a less of whether.
1a85f 2a 2a 20 6f 72 20 6e 6f 74 20 70 57 61 6c 2d 3e ** or not pWal->
1a860 68 64 72 2e 6d 78 46 72 61 6d 65 20 69 73 20 6d hdr.mxFrame is m
1a861 6f 64 69 66 69 65 64 29 2e 20 41 6e 20 53 51 4c odified). An SQL
1a862 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 ite error code i
1a863 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 66 s returned.** if
1a864 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
1a865 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1a866 77 61 6c 52 65 73 74 61 72 74 4c 6f 67 28 57 61 walRestartLog(Wa
1a867 6c 20 2a 70 57 61 6c 29 7b 0a 20 20 69 6e 74 20 l *pWal){. int
1a868 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1a869 20 20 69 6e 74 20 63 6e 74 3b 0a 0a 20 20 69 66 int cnt;.. if
1a86a 28 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b ( pWal->readLock
1a86b 3d 3d 30 20 29 7b 0a 20 20 20 20 76 6f 6c 61 74 ==0 ){. volat
1a86c 69 6c 65 20 57 61 6c 43 6b 70 74 49 6e 66 6f 20 ile WalCkptInfo
1a86d 2a 70 49 6e 66 6f 20 3d 20 77 61 6c 43 6b 70 74 *pInfo = walCkpt
1a86e 49 6e 66 6f 28 70 57 61 6c 29 3b 0a 20 20 20 20 Info(pWal);.
1a86f 61 73 73 65 72 74 28 20 70 49 6e 66 6f 2d 3e 6e assert( pInfo->n
1a870 42 61 63 6b 66 69 6c 6c 3d 3d 70 57 61 6c 2d 3e Backfill==pWal->
1a871 68 64 72 2e 6d 78 46 72 61 6d 65 20 29 3b 0a 20 hdr.mxFrame );.
1a872 20 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e 42 if( pInfo->nB
1a873 61 63 6b 66 69 6c 6c 3e 30 20 29 7b 0a 20 20 20 ackfill>0 ){.
1a874 20 20 20 75 33 32 20 73 61 6c 74 31 3b 0a 20 20 u32 salt1;.
1a875 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 sqlite3_rand
1a876 6f 6d 6e 65 73 73 28 34 2c 20 26 73 61 6c 74 31 omness(4, &salt1
1a877 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 61 );. rc = wa
1a878 6c 4c 6f 63 6b 45 78 63 6c 75 73 69 76 65 28 70 lLockExclusive(p
1a879 57 61 6c 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f Wal, WAL_READ_LO
1a87a 43 4b 28 31 29 2c 20 57 41 4c 5f 4e 52 45 41 44 CK(1), WAL_NREAD
1a87b 45 52 2d 31 29 3b 0a 20 20 20 20 20 20 69 66 28 ER-1);. if(
1a87c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1a87d 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 {. /* If
1a87e 61 6c 6c 20 72 65 61 64 65 72 73 20 61 72 65 20 all readers are
1a87f 75 73 69 6e 67 20 57 41 4c 5f 52 45 41 44 5f 4c using WAL_READ_L
1a880 4f 43 4b 28 30 29 20 28 69 6e 20 6f 74 68 65 72 OCK(0) (in other
1a881 20 77 6f 72 64 73 20 69 66 20 6e 6f 0a 20 20 20 words if no.
1a882 20 20 20 20 20 2a 2a 20 72 65 61 64 65 72 73 20 ** readers
1a883 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 75 73 are currently us
1a884 69 6e 67 20 74 68 65 20 57 41 4c 29 2c 20 74 68 ing the WAL), th
1a885 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 en the transacti
1a886 6f 6e 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 ons. ** f
1a887 72 61 6d 65 73 20 77 69 6c 6c 20 6f 76 65 72 77 rames will overw
1a888 72 69 74 65 20 74 68 65 20 73 74 61 72 74 20 6f rite the start o
1a889 66 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 6c f the existing l
1a88a 6f 67 2e 20 55 70 64 61 74 65 20 74 68 65 0a 20 og. Update the.
1a88b 20 20 20 20 20 20 20 2a 2a 20 77 61 6c 2d 69 6e ** wal-in
1a88c 64 65 78 20 68 65 61 64 65 72 20 74 6f 20 72 65 dex header to re
1a88d 66 6c 65 63 74 20 74 68 69 73 2e 0a 20 20 20 20 flect this..
1a88e 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a **. *
1a88f 2a 20 49 6e 20 74 68 65 6f 72 79 20 69 74 20 77 * In theory it w
1a890 6f 75 6c 64 20 62 65 20 4f 6b 20 74 6f 20 75 70 ould be Ok to up
1a891 64 61 74 65 20 74 68 65 20 63 61 63 68 65 20 6f date the cache o
1a892 66 20 74 68 65 20 68 65 61 64 65 72 20 6f 6e 6c f the header onl
1a893 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 74 20 y. ** at
1a894 74 68 69 73 20 70 6f 69 6e 74 2e 20 42 75 74 20 this point. But
1a895 75 70 64 61 74 69 6e 67 20 74 68 65 20 61 63 74 updating the act
1a896 75 61 6c 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 ual wal-index he
1a897 61 64 65 72 20 69 73 20 61 6c 73 6f 0a 20 20 20 ader is also.
1a898 20 20 20 20 20 2a 2a 20 73 61 66 65 20 61 6e 64 ** safe and
1a899 20 6d 65 61 6e 73 20 74 68 65 72 65 20 69 73 20 means there is
1a89a 6e 6f 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 no special case
1a89b 66 6f 72 20 73 71 6c 69 74 65 33 57 61 6c 55 6e for sqlite3WalUn
1a89c 64 6f 28 29 0a 20 20 20 20 20 20 20 20 2a 2a 20 do(). **
1a89d 74 6f 20 68 61 6e 64 6c 65 20 69 66 20 74 68 69 to handle if thi
1a89e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 s transaction is
1a89f 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 20 20 rolled back..
1a8a0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
1a8a1 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
1a8a2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
1a8a3 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
1a8a4 20 20 20 20 20 20 75 33 32 20 2a 61 53 61 6c 74 u32 *aSalt
1a8a5 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 61 53 61 = pWal->hdr.aSa
1a8a6 6c 74 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 67 lt; /* Big
1a8a7 2d 65 6e 64 69 61 6e 20 73 61 6c 74 20 76 61 6c -endian salt val
1a8a8 75 65 73 20 2a 2f 0a 0a 20 20 20 20 20 20 20 20 ues */..
1a8a9 70 57 61 6c 2d 3e 6e 43 6b 70 74 2b 2b 3b 0a 20 pWal->nCkpt++;.
1a8aa 20 20 20 20 20 20 20 70 57 61 6c 2d 3e 68 64 72 pWal->hdr
1a8ab 2e 6d 78 46 72 61 6d 65 20 3d 20 30 3b 0a 20 20 .mxFrame = 0;.
1a8ac 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 75 74 sqlite3Put
1a8ad 34 62 79 74 65 28 28 75 38 2a 29 26 61 53 61 6c 4byte((u8*)&aSal
1a8ae 74 5b 30 5d 2c 20 31 20 2b 20 73 71 6c 69 74 65 t[0], 1 + sqlite
1a8af 33 47 65 74 34 62 79 74 65 28 28 75 38 2a 29 26 3Get4byte((u8*)&
1a8b0 61 53 61 6c 74 5b 30 5d 29 29 3b 0a 20 20 20 20 aSalt[0]));.
1a8b1 20 20 20 20 61 53 61 6c 74 5b 31 5d 20 3d 20 73 aSalt[1] = s
1a8b2 61 6c 74 31 3b 0a 20 20 20 20 20 20 20 20 77 61 alt1;. wa
1a8b3 6c 49 6e 64 65 78 57 72 69 74 65 48 64 72 28 70 lIndexWriteHdr(p
1a8b4 57 61 6c 29 3b 0a 20 20 20 20 20 20 20 20 70 49 Wal);. pI
1a8b5 6e 66 6f 2d 3e 6e 42 61 63 6b 66 69 6c 6c 20 3d nfo->nBackfill =
1a8b6 20 30 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 66 0;. pInf
1a8b7 6f 2d 3e 61 52 65 61 64 4d 61 72 6b 5b 31 5d 20 o->aReadMark[1]
1a8b8 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 = 0;. for
1a8b9 28 69 3d 32 3b 20 69 3c 57 41 4c 5f 4e 52 45 41 (i=2; i<WAL_NREA
1a8ba 44 45 52 3b 20 69 2b 2b 29 20 70 49 6e 66 6f 2d DER; i++) pInfo-
1a8bb 3e 61 52 65 61 64 4d 61 72 6b 5b 69 5d 20 3d 20 >aReadMark[i] =
1a8bc 52 45 41 44 4d 41 52 4b 5f 4e 4f 54 5f 55 53 45 READMARK_NOT_USE
1a8bd 44 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 D;. asser
1a8be 74 28 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 4d t( pInfo->aReadM
1a8bf 61 72 6b 5b 30 5d 3d 3d 30 20 29 3b 0a 20 20 20 ark[0]==0 );.
1a8c0 20 20 20 20 20 77 61 6c 55 6e 6c 6f 63 6b 45 78 walUnlockEx
1a8c1 63 6c 75 73 69 76 65 28 70 57 61 6c 2c 20 57 41 clusive(pWal, WA
1a8c2 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 31 29 2c 20 L_READ_LOCK(1),
1a8c3 57 41 4c 5f 4e 52 45 41 44 45 52 2d 31 29 3b 0a WAL_NREADER-1);.
1a8c4 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1a8c5 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 rc!=SQLITE_BUSY
1a8c6 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
1a8c7 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
1a8c8 20 20 7d 0a 20 20 20 20 77 61 6c 55 6e 6c 6f 63 }. walUnloc
1a8c9 6b 53 68 61 72 65 64 28 70 57 61 6c 2c 20 57 41 kShared(pWal, WA
1a8ca 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 30 29 29 3b L_READ_LOCK(0));
1a8cb 0a 20 20 20 20 70 57 61 6c 2d 3e 72 65 61 64 4c . pWal->readL
1a8cc 6f 63 6b 20 3d 20 2d 31 3b 0a 20 20 20 20 63 6e ock = -1;. cn
1a8cd 74 20 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 t = 0;. do{.
1a8ce 20 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 int notUsed
1a8cf 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 61 6c ;. rc = wal
1a8d0 54 72 79 42 65 67 69 6e 52 65 61 64 28 70 57 61 TryBeginRead(pWa
1a8d1 6c 2c 20 26 6e 6f 74 55 73 65 64 2c 20 31 2c 20 l, ¬Used, 1,
1a8d2 2b 2b 63 6e 74 29 3b 0a 20 20 20 20 7d 77 68 69 ++cnt);. }whi
1a8d3 6c 65 28 20 72 63 3d 3d 57 41 4c 5f 52 45 54 52 le( rc==WAL_RETR
1a8d4 59 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 Y );. assert(
1a8d5 20 28 72 63 26 30 78 66 66 29 21 3d 53 51 4c 49 (rc&0xff)!=SQLI
1a8d6 54 45 5f 42 55 53 59 20 29 3b 20 2f 2a 20 42 55 TE_BUSY ); /* BU
1a8d7 53 59 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 SY not possible
1a8d8 77 68 65 6e 20 75 73 65 57 61 6c 3d 3d 31 20 2a when useWal==1 *
1a8d9 2f 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 /. testcase(
1a8da 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 (rc&0xff)==SQLIT
1a8db 45 5f 49 4f 45 52 52 20 29 3b 0a 20 20 20 20 74 E_IOERR );. t
1a8dc 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c estcase( rc==SQL
1a8dd 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 29 3b 0a ITE_PROTOCOL );.
1a8de 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 testcase( rc
1a8df 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 ==SQLITE_OK );.
1a8e0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1a8e1 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 }../*.** Informa
1a8e2 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 tion about the c
1a8e3 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 urrent state of
1a8e4 74 68 65 20 57 41 4c 20 66 69 6c 65 20 61 6e 64 the WAL file and
1a8e5 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 20 6e 65 where.** the ne
1a8e6 78 74 20 66 73 79 6e 63 20 73 68 6f 75 6c 64 20 xt fsync should
1a8e7 6f 63 63 75 72 20 2d 20 70 61 73 73 65 64 20 66 occur - passed f
1a8e8 72 6f 6d 20 73 71 6c 69 74 65 33 57 61 6c 46 72 rom sqlite3WalFr
1a8e9 61 6d 65 73 28 29 20 69 6e 74 6f 0a 2a 2a 20 77 ames() into.** w
1a8ea 61 6c 57 72 69 74 65 54 6f 4c 6f 67 28 29 2e 0a alWriteToLog()..
1a8eb 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
1a8ec 74 20 57 61 6c 57 72 69 74 65 72 20 7b 0a 20 20 t WalWriter {.
1a8ed 57 61 6c 20 2a 70 57 61 6c 3b 20 20 20 20 20 20 Wal *pWal;
1a8ee 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a8ef 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 57 41 4c The complete WAL
1a8f0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a information */.
1a8f1 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1a8f2 70 46 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f pFd; /
1a8f3 2a 20 54 68 65 20 57 41 4c 20 66 69 6c 65 20 74 * The WAL file t
1a8f4 6f 20 77 68 69 63 68 20 77 65 20 77 72 69 74 65 o which we write
1a8f5 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e */. sqlite3_in
1a8f6 74 36 34 20 69 53 79 6e 63 50 6f 69 6e 74 3b 20 t64 iSyncPoint;
1a8f7 20 20 20 2f 2a 20 46 73 79 6e 63 20 61 74 20 74 /* Fsync at t
1a8f8 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 his offset */.
1a8f9 69 6e 74 20 73 79 6e 63 46 6c 61 67 73 3b 20 20 int syncFlags;
1a8fa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a8fb 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20 66 73 Flags for the fs
1a8fc 79 6e 63 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 ync */. int szP
1a8fd 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
1a8fe 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
1a8ff 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 7d 20 57 one page */.} W
1a900 61 6c 57 72 69 74 65 72 3b 0a 0a 2f 2a 0a 2a 2a alWriter;../*.**
1a901 20 57 72 69 74 65 20 69 41 6d 74 20 62 79 74 65 Write iAmt byte
1a902 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e 74 s of content int
1a903 6f 20 74 68 65 20 57 41 4c 20 66 69 6c 65 20 62 o the WAL file b
1a904 65 67 69 6e 6e 69 6e 67 20 61 74 20 69 4f 66 66 eginning at iOff
1a905 73 65 74 2e 0a 2a 2a 20 44 6f 20 61 20 73 79 6e set..** Do a syn
1a906 63 20 77 68 65 6e 20 63 72 6f 73 73 69 6e 67 20 c when crossing
1a907 74 68 65 20 70 2d 3e 69 53 79 6e 63 50 6f 69 6e the p->iSyncPoin
1a908 74 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a t boundary..**.*
1a909 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * In other words
1a90a 2c 20 69 66 20 69 53 79 6e 63 50 6f 69 6e 74 20 , if iSyncPoint
1a90b 69 73 20 69 6e 20 62 65 74 77 65 65 6e 20 69 4f is in between iO
1a90c 66 66 73 65 74 20 61 6e 64 20 69 4f 66 66 73 65 ffset and iOffse
1a90d 74 2b 69 41 6d 74 2c 0a 2a 2a 20 66 69 72 73 74 t+iAmt,.** first
1a90e 20 77 72 69 74 65 20 74 68 65 20 70 61 72 74 20 write the part
1a90f 62 65 66 6f 72 65 20 69 53 79 6e 63 50 6f 69 6e before iSyncPoin
1a910 74 2c 20 74 68 65 6e 20 73 79 6e 63 2c 20 74 68 t, then sync, th
1a911 65 6e 20 77 72 69 74 65 20 74 68 65 0a 2a 2a 20 en write the.**
1a912 72 65 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rest..*/.static
1a913 69 6e 74 20 77 61 6c 57 72 69 74 65 54 6f 4c 6f int walWriteToLo
1a914 67 28 0a 20 20 57 61 6c 57 72 69 74 65 72 20 2a g(. WalWriter *
1a915 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p,
1a916 2f 2a 20 57 41 4c 20 74 6f 20 77 72 69 74 65 20 /* WAL to write
1a917 74 6f 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 to */. void *pC
1a918 6f 6e 74 65 6e 74 2c 20 20 20 20 20 20 20 20 20 ontent,
1a919 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 74 6f /* Content to
1a91a 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 be written */.
1a91b 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 int iAmt,
1a91c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1a91d 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
1a91e 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c o write */. sql
1a91f 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 73 ite3_int64 iOffs
1a920 65 74 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 et /* Start
1a921 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69 73 writing at this
1a922 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 offset */.){.
1a923 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 69 4f int rc;. if( iO
1a924 66 66 73 65 74 3c 70 2d 3e 69 53 79 6e 63 50 6f ffset<p->iSyncPo
1a925 69 6e 74 20 26 26 20 69 4f 66 66 73 65 74 2b 69 int && iOffset+i
1a926 41 6d 74 3e 3d 70 2d 3e 69 53 79 6e 63 50 6f 69 Amt>=p->iSyncPoi
1a927 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 46 nt ){. int iF
1a928 69 72 73 74 41 6d 74 20 3d 20 28 69 6e 74 29 28 irstAmt = (int)(
1a929 70 2d 3e 69 53 79 6e 63 50 6f 69 6e 74 20 2d 20 p->iSyncPoint -
1a92a 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 72 63 iOffset);. rc
1a92b 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
1a92c 65 28 70 2d 3e 70 46 64 2c 20 70 43 6f 6e 74 65 e(p->pFd, pConte
1a92d 6e 74 2c 20 69 46 69 72 73 74 41 6d 74 2c 20 69 nt, iFirstAmt, i
1a92e 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 69 66 28 Offset);. if(
1a92f 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
1a930 0a 20 20 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 . iOffset +=
1a931 69 46 69 72 73 74 41 6d 74 3b 0a 20 20 20 20 69 iFirstAmt;. i
1a932 41 6d 74 20 2d 3d 20 69 46 69 72 73 74 41 6d 74 Amt -= iFirstAmt
1a933 3b 0a 20 20 20 20 70 43 6f 6e 74 65 6e 74 20 3d ;. pContent =
1a934 20 28 76 6f 69 64 2a 29 28 69 46 69 72 73 74 41 (void*)(iFirstA
1a935 6d 74 20 2b 20 28 63 68 61 72 2a 29 70 43 6f 6e mt + (char*)pCon
1a936 74 65 6e 74 29 3b 0a 20 20 20 20 61 73 73 65 72 tent);. asser
1a937 74 28 20 70 2d 3e 73 79 6e 63 46 6c 61 67 73 20 t( p->syncFlags
1a938 26 20 28 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e & (SQLITE_SYNC_N
1a939 4f 52 4d 41 4c 7c 53 51 4c 49 54 45 5f 53 59 4e ORMAL|SQLITE_SYN
1a93a 43 5f 46 55 4c 4c 29 20 29 3b 0a 20 20 20 20 72 C_FULL) );. r
1a93b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e c = sqlite3OsSyn
1a93c 63 28 70 2d 3e 70 46 64 2c 20 70 2d 3e 73 79 6e c(p->pFd, p->syn
1a93d 63 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 cFlags);. if(
1a93e 20 69 41 6d 74 3d 3d 30 20 7c 7c 20 72 63 20 29 iAmt==0 || rc )
1a93f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
1a940 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1a941 57 72 69 74 65 28 70 2d 3e 70 46 64 2c 20 70 43 Write(p->pFd, pC
1a942 6f 6e 74 65 6e 74 2c 20 69 41 6d 74 2c 20 69 4f ontent, iAmt, iO
1a943 66 66 73 65 74 29 3b 0a 20 20 72 65 74 75 72 6e ffset);. return
1a944 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 rc;.}../*.** Wr
1a945 69 74 65 20 6f 75 74 20 61 20 73 69 6e 67 6c 65 ite out a single
1a946 20 66 72 61 6d 65 20 6f 66 20 74 68 65 20 57 41 frame of the WA
1a947 4c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 L.*/.static int
1a948 77 61 6c 57 72 69 74 65 4f 6e 65 46 72 61 6d 65 walWriteOneFrame
1a949 28 0a 20 20 57 61 6c 57 72 69 74 65 72 20 2a 70 (. WalWriter *p
1a94a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1a94b 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 /* Where to writ
1a94c 65 20 74 68 65 20 66 72 61 6d 65 20 2a 2f 0a 20 e the frame */.
1a94d 20 50 67 48 64 72 20 2a 70 50 61 67 65 2c 20 20 PgHdr *pPage,
1a94e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a94f 54 68 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 The page of the
1a950 66 72 61 6d 65 20 74 6f 20 62 65 20 77 72 69 74 frame to be writ
1a951 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 72 ten */. int nTr
1a952 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 uncate,
1a953 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 6d /* The comm
1a954 69 74 20 66 6c 61 67 2e 20 20 55 73 75 61 6c 6c it flag. Usuall
1a955 79 20 30 2e 20 20 3e 30 20 66 6f 72 20 63 6f 6d y 0. >0 for com
1a956 6d 69 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 mit */. sqlite3
1a957 5f 69 6e 74 36 34 20 69 4f 66 66 73 65 74 20 20 _int64 iOffset
1a958 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 /* Byte off
1a959 73 65 74 20 61 74 20 77 68 69 63 68 20 74 6f 20 set at which to
1a95a 77 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e write */.){. in
1a95b 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
1a95c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a95d 20 52 65 73 75 6c 74 20 63 6f 64 65 20 66 72 6f Result code fro
1a95e 6d 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a m subfunctions *
1a95f 2f 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b /. void *pData;
1a960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a961 20 20 20 20 2f 2a 20 44 61 74 61 20 61 63 74 75 /* Data actu
1a962 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 2a 2f 0a ally written */.
1a963 20 20 75 38 20 61 46 72 61 6d 65 5b 57 41 4c 5f u8 aFrame[WAL_
1a964 46 52 41 4d 45 5f 48 44 52 53 49 5a 45 5d 3b 20 FRAME_HDRSIZE];
1a965 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 61 /* Buffer to a
1a966 73 73 65 6d 62 6c 65 20 66 72 61 6d 65 2d 68 65 ssemble frame-he
1a967 61 64 65 72 20 69 6e 20 2a 2f 0a 23 69 66 20 64 ader in */.#if d
1a968 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 efined(SQLITE_HA
1a969 53 5f 43 4f 44 45 43 29 0a 20 20 69 66 28 20 28 S_CODEC). if( (
1a96a 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 pData = sqlite3P
1a96b 61 67 65 72 43 6f 64 65 63 28 70 50 61 67 65 29 agerCodec(pPage)
1a96c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 )==0 ) return SQ
1a96d 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 23 65 6c 73 LITE_NOMEM;.#els
1a96e 65 0a 20 20 70 44 61 74 61 20 3d 20 70 50 61 67 e. pData = pPag
1a96f 65 2d 3e 70 44 61 74 61 3b 0a 23 65 6e 64 69 66 e->pData;.#endif
1a970 0a 20 20 77 61 6c 45 6e 63 6f 64 65 46 72 61 6d . walEncodeFram
1a971 65 28 70 2d 3e 70 57 61 6c 2c 20 70 50 61 67 65 e(p->pWal, pPage
1a972 2d 3e 70 67 6e 6f 2c 20 6e 54 72 75 6e 63 61 74 ->pgno, nTruncat
1a973 65 2c 20 70 44 61 74 61 2c 20 61 46 72 61 6d 65 e, pData, aFrame
1a974 29 3b 0a 20 20 72 63 20 3d 20 77 61 6c 57 72 69 );. rc = walWri
1a975 74 65 54 6f 4c 6f 67 28 70 2c 20 61 46 72 61 6d teToLog(p, aFram
1a976 65 2c 20 73 69 7a 65 6f 66 28 61 46 72 61 6d 65 e, sizeof(aFrame
1a977 29 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 69 ), iOffset);. i
1a978 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1a979 63 3b 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 c;. /* Write th
1a97a 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 e page data */.
1a97b 20 72 63 20 3d 20 77 61 6c 57 72 69 74 65 54 6f rc = walWriteTo
1a97c 4c 6f 67 28 70 2c 20 70 44 61 74 61 2c 20 70 2d Log(p, pData, p-
1a97d 3e 73 7a 50 61 67 65 2c 20 69 4f 66 66 73 65 74 >szPage, iOffset
1a97e 2b 73 69 7a 65 6f 66 28 61 46 72 61 6d 65 29 29 +sizeof(aFrame))
1a97f 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1a980 0a 0a 2f 2a 20 0a 2a 2a 20 57 72 69 74 65 20 61 ../* .** Write a
1a981 20 73 65 74 20 6f 66 20 66 72 61 6d 65 73 20 74 set of frames t
1a982 6f 20 74 68 65 20 6c 6f 67 2e 20 54 68 65 20 63 o the log. The c
1a983 61 6c 6c 65 72 20 6d 75 73 74 20 68 6f 6c 64 20 aller must hold
1a984 74 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 0a 2a the write-lock.*
1a985 2a 20 6f 6e 20 74 68 65 20 6c 6f 67 20 66 69 6c * on the log fil
1a986 65 20 28 6f 62 74 61 69 6e 65 64 20 75 73 69 6e e (obtained usin
1a987 67 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67 69 g sqlite3WalBegi
1a988 6e 57 72 69 74 65 54 72 61 6e 73 61 63 74 69 6f nWriteTransactio
1a989 6e 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f n())..*/.SQLITE_
1a98a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1a98b 74 65 33 57 61 6c 46 72 61 6d 65 73 28 0a 20 20 te3WalFrames(.
1a98c 57 61 6c 20 2a 70 57 61 6c 2c 20 20 20 20 20 20 Wal *pWal,
1a98d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a98e 2f 2a 20 57 61 6c 20 68 61 6e 64 6c 65 20 74 6f /* Wal handle to
1a98f 20 77 72 69 74 65 20 74 6f 20 2a 2f 0a 20 20 69 write to */. i
1a990 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 nt szPage,
1a991 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a992 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 65 2d * Database page-
1a993 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 2a 2f size in bytes */
1a994 0a 20 20 50 67 48 64 72 20 2a 70 4c 69 73 74 2c . PgHdr *pList,
1a995 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a996 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 /* List of di
1a997 72 74 79 20 70 61 67 65 73 20 74 6f 20 77 72 69 rty pages to wri
1a998 74 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 54 72 te */. Pgno nTr
1a999 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 uncate,
1a99a 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
1a99b 61 73 65 20 73 69 7a 65 20 61 66 74 65 72 20 74 ase size after t
1a99c 68 69 73 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 his commit */.
1a99d 69 6e 74 20 69 73 43 6f 6d 6d 69 74 2c 20 20 20 int isCommit,
1a99e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a99f 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
1a9a0 69 73 20 61 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 is a commit */.
1a9a1 20 69 6e 74 20 73 79 6e 63 5f 66 6c 61 67 73 20 int sync_flags
1a9a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a9a3 20 2f 2a 20 46 6c 61 67 73 20 74 6f 20 70 61 73 /* Flags to pas
1a9a4 73 20 74 6f 20 4f 73 53 79 6e 63 28 29 20 28 6f s to OsSync() (o
1a9a5 72 20 30 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 r 0) */.){. int
1a9a6 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1a9a7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a9a8 55 73 65 64 20 74 6f 20 63 61 74 63 68 20 72 65 Used to catch re
1a9a9 74 75 72 6e 20 63 6f 64 65 73 20 2a 2f 0a 20 20 turn codes */.
1a9aa 75 33 32 20 69 46 72 61 6d 65 3b 20 20 20 20 20 u32 iFrame;
1a9ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a9ac 2f 2a 20 4e 65 78 74 20 66 72 61 6d 65 20 61 64 /* Next frame ad
1a9ad 64 72 65 73 73 20 2a 2f 0a 20 20 50 67 48 64 72 dress */. PgHdr
1a9ae 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 *p;
1a9af 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 /* It
1a9b0 65 72 61 74 6f 72 20 74 6f 20 72 75 6e 20 74 68 erator to run th
1a9b1 72 6f 75 67 68 20 70 4c 69 73 74 20 77 69 74 68 rough pList with
1a9b2 2e 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 4c . */. PgHdr *pL
1a9b3 61 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ast = 0;
1a9b4 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 /* Last f
1a9b5 72 61 6d 65 20 69 6e 20 6c 69 73 74 20 2a 2f 0a rame in list */.
1a9b6 20 20 69 6e 74 20 6e 45 78 74 72 61 20 3d 20 30 int nExtra = 0
1a9b7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1a9b8 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
1a9b9 78 74 72 61 20 63 6f 70 69 65 73 20 6f 66 20 6c xtra copies of l
1a9ba 61 73 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e ast page */. in
1a9bb 74 20 73 7a 46 72 61 6d 65 3b 20 20 20 20 20 20 t szFrame;
1a9bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a9bd 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20 73 The size of a s
1a9be 69 6e 67 6c 65 20 66 72 61 6d 65 20 2a 2f 0a 20 ingle frame */.
1a9bf 20 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20 i64 iOffset;
1a9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a9c1 20 2f 2a 20 4e 65 78 74 20 62 79 74 65 20 74 6f /* Next byte to
1a9c2 20 77 72 69 74 65 20 69 6e 20 57 41 4c 20 66 69 write in WAL fi
1a9c3 6c 65 20 2a 2f 0a 20 20 57 61 6c 57 72 69 74 65 le */. WalWrite
1a9c4 72 20 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 r w;
1a9c5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 /* The w
1a9c6 72 69 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 riter */.. asse
1a9c7 72 74 28 20 70 4c 69 73 74 20 29 3b 0a 20 20 61 rt( pList );. a
1a9c8 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 77 72 69 ssert( pWal->wri
1a9c9 74 65 4c 6f 63 6b 20 29 3b 0a 0a 20 20 2f 2a 20 teLock );.. /*
1a9ca 49 66 20 74 68 69 73 20 66 72 61 6d 65 20 73 65 If this frame se
1a9cb 74 20 63 6f 6d 70 6c 65 74 65 73 20 61 20 74 72 t completes a tr
1a9cc 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 ansaction, then
1a9cd 6e 54 72 75 6e 63 61 74 65 3e 30 2e 20 20 49 66 nTruncate>0. If
1a9ce 0a 20 20 2a 2a 20 6e 54 72 75 6e 63 61 74 65 3d . ** nTruncate=
1a9cf 3d 30 20 74 68 65 6e 20 74 68 69 73 20 66 72 61 =0 then this fra
1a9d0 6d 65 20 73 65 74 20 64 6f 65 73 20 6e 6f 74 20 me set does not
1a9d1 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 74 72 61 complete the tra
1a9d2 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 61 nsaction. */. a
1a9d3 73 73 65 72 74 28 20 28 69 73 43 6f 6d 6d 69 74 ssert( (isCommit
1a9d4 21 3d 30 29 3d 3d 28 6e 54 72 75 6e 63 61 74 65 !=0)==(nTruncate
1a9d5 21 3d 30 29 20 29 3b 0a 0a 23 69 66 20 64 65 66 !=0) );..#if def
1a9d6 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
1a9d7 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c ) && defined(SQL
1a9d8 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 20 69 ITE_DEBUG). { i
1a9d9 6e 74 20 63 6e 74 3b 20 66 6f 72 28 63 6e 74 3d nt cnt; for(cnt=
1a9da 30 2c 20 70 3d 70 4c 69 73 74 3b 20 70 3b 20 70 0, p=pList; p; p
1a9db 3d 70 2d 3e 70 44 69 72 74 79 2c 20 63 6e 74 2b =p->pDirty, cnt+
1a9dc 2b 29 7b 7d 0a 20 20 20 20 57 41 4c 54 52 41 43 +){}. WALTRAC
1a9dd 45 28 28 22 57 41 4c 25 70 3a 20 66 72 61 6d 65 E(("WAL%p: frame
1a9de 20 77 72 69 74 65 20 62 65 67 69 6e 2e 20 25 64 write begin. %d
1a9df 20 66 72 61 6d 65 73 2e 20 6d 78 46 72 61 6d 65 frames. mxFrame
1a9e0 3d 25 64 2e 20 25 73 5c 6e 22 2c 0a 20 20 20 20 =%d. %s\n",.
1a9e1 20 20 20 20 20 20 20 20 20 20 70 57 61 6c 2c 20 pWal,
1a9e2 63 6e 74 2c 20 70 57 61 6c 2d 3e 68 64 72 2e 6d cnt, pWal->hdr.m
1a9e3 78 46 72 61 6d 65 2c 20 69 73 43 6f 6d 6d 69 74 xFrame, isCommit
1a9e4 20 3f 20 22 43 6f 6d 6d 69 74 22 20 3a 20 22 53 ? "Commit" : "S
1a9e5 70 69 6c 6c 22 29 29 3b 0a 20 20 7d 0a 23 65 6e pill"));. }.#en
1a9e6 64 69 66 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 dif.. /* See if
1a9e7 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 it is possible
1a9e8 74 6f 20 77 72 69 74 65 20 74 68 65 73 65 20 66 to write these f
1a9e9 72 61 6d 65 73 20 69 6e 74 6f 20 74 68 65 20 73 rames into the s
1a9ea 74 61 72 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a tart of the. **
1a9eb 20 6c 6f 67 20 66 69 6c 65 2c 20 69 6e 73 74 65 log file, inste
1a9ec 61 64 20 6f 66 20 61 70 70 65 6e 64 69 6e 67 20 ad of appending
1a9ed 74 6f 20 69 74 20 61 74 20 70 57 61 6c 2d 3e 68 to it at pWal->h
1a9ee 64 72 2e 6d 78 46 72 61 6d 65 2e 0a 20 20 2a 2f dr.mxFrame.. */
1a9ef 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b . if( SQLITE_OK
1a9f0 21 3d 28 72 63 20 3d 20 77 61 6c 52 65 73 74 61 !=(rc = walResta
1a9f1 72 74 4c 6f 67 28 70 57 61 6c 29 29 20 29 7b 0a rtLog(pWal)) ){.
1a9f2 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1a9f3 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 }.. /* If this
1a9f4 20 69 73 20 74 68 65 20 66 69 72 73 74 20 66 72 is the first fr
1a9f5 61 6d 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f ame written into
1a9f6 20 74 68 65 20 6c 6f 67 2c 20 77 72 69 74 65 20 the log, write
1a9f7 74 68 65 20 57 41 4c 0a 20 20 2a 2a 20 68 65 61 the WAL. ** hea
1a9f8 64 65 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 der to the start
1a9f9 20 6f 66 20 74 68 65 20 57 41 4c 20 66 69 6c 65 of the WAL file
1a9fa 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 . See comments a
1a9fb 74 20 74 68 65 20 74 6f 70 20 6f 66 0a 20 20 2a t the top of. *
1a9fc 2a 20 74 68 69 73 20 73 6f 75 72 63 65 20 66 69 * this source fi
1a9fd 6c 65 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 le for a descrip
1a9fe 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 41 4c 20 tion of the WAL
1a9ff 68 65 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a 20 header format..
1aa00 20 2a 2f 0a 20 20 69 46 72 61 6d 65 20 3d 20 70 */. iFrame = p
1aa01 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 Wal->hdr.mxFrame
1aa02 3b 0a 20 20 69 66 28 20 69 46 72 61 6d 65 3d 3d ;. if( iFrame==
1aa03 30 20 29 7b 0a 20 20 20 20 75 38 20 61 57 61 6c 0 ){. u8 aWal
1aa04 48 64 72 5b 57 41 4c 5f 48 44 52 53 49 5a 45 5d Hdr[WAL_HDRSIZE]
1aa05 3b 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 ; /* Buffer
1aa06 20 74 6f 20 61 73 73 65 6d 62 6c 65 20 77 61 6c to assemble wal
1aa07 2d 68 65 61 64 65 72 20 69 6e 20 2a 2f 0a 20 20 -header in */.
1aa08 20 20 75 33 32 20 61 43 6b 73 75 6d 5b 32 5d 3b u32 aCksum[2];
1aa09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aa0a 2f 2a 20 43 68 65 63 6b 73 75 6d 20 66 6f 72 20 /* Checksum for
1aa0b 77 61 6c 2d 68 65 61 64 65 72 20 2a 2f 0a 0a 20 wal-header */..
1aa0c 20 20 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 sqlite3Put4by
1aa0d 74 65 28 26 61 57 61 6c 48 64 72 5b 30 5d 2c 20 te(&aWalHdr[0],
1aa0e 28 57 41 4c 5f 4d 41 47 49 43 20 7c 20 53 51 4c (WAL_MAGIC | SQL
1aa0f 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 29 29 3b ITE_BIGENDIAN));
1aa10 0a 20 20 20 20 73 71 6c 69 74 65 33 50 75 74 34 . sqlite3Put4
1aa11 62 79 74 65 28 26 61 57 61 6c 48 64 72 5b 34 5d byte(&aWalHdr[4]
1aa12 2c 20 57 41 4c 5f 4d 41 58 5f 56 45 52 53 49 4f , WAL_MAX_VERSIO
1aa13 4e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 N);. sqlite3P
1aa14 75 74 34 62 79 74 65 28 26 61 57 61 6c 48 64 72 ut4byte(&aWalHdr
1aa15 5b 38 5d 2c 20 73 7a 50 61 67 65 29 3b 0a 20 20 [8], szPage);.
1aa16 20 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 sqlite3Put4byt
1aa17 65 28 26 61 57 61 6c 48 64 72 5b 31 32 5d 2c 20 e(&aWalHdr[12],
1aa18 70 57 61 6c 2d 3e 6e 43 6b 70 74 29 3b 0a 20 20 pWal->nCkpt);.
1aa19 20 20 69 66 28 20 70 57 61 6c 2d 3e 6e 43 6b 70 if( pWal->nCkp
1aa1a 74 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 5f 72 t==0 ) sqlite3_r
1aa1b 61 6e 64 6f 6d 6e 65 73 73 28 38 2c 20 70 57 61 andomness(8, pWa
1aa1c 6c 2d 3e 68 64 72 2e 61 53 61 6c 74 29 3b 0a 20 l->hdr.aSalt);.
1aa1d 20 20 20 6d 65 6d 63 70 79 28 26 61 57 61 6c 48 memcpy(&aWalH
1aa1e 64 72 5b 31 36 5d 2c 20 70 57 61 6c 2d 3e 68 64 dr[16], pWal->hd
1aa1f 72 2e 61 53 61 6c 74 2c 20 38 29 3b 0a 20 20 20 r.aSalt, 8);.
1aa20 20 77 61 6c 43 68 65 63 6b 73 75 6d 42 79 74 65 walChecksumByte
1aa21 73 28 31 2c 20 61 57 61 6c 48 64 72 2c 20 57 41 s(1, aWalHdr, WA
1aa22 4c 5f 48 44 52 53 49 5a 45 2d 32 2a 34 2c 20 30 L_HDRSIZE-2*4, 0
1aa23 2c 20 61 43 6b 73 75 6d 29 3b 0a 20 20 20 20 73 , aCksum);. s
1aa24 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 26 qlite3Put4byte(&
1aa25 61 57 61 6c 48 64 72 5b 32 34 5d 2c 20 61 43 6b aWalHdr[24], aCk
1aa26 73 75 6d 5b 30 5d 29 3b 0a 20 20 20 20 73 71 6c sum[0]);. sql
1aa27 69 74 65 33 50 75 74 34 62 79 74 65 28 26 61 57 ite3Put4byte(&aW
1aa28 61 6c 48 64 72 5b 32 38 5d 2c 20 61 43 6b 73 75 alHdr[28], aCksu
1aa29 6d 5b 31 5d 29 3b 0a 20 20 20 20 0a 20 20 20 20 m[1]);. .
1aa2a 70 57 61 6c 2d 3e 73 7a 50 61 67 65 20 3d 20 73 pWal->szPage = s
1aa2b 7a 50 61 67 65 3b 0a 20 20 20 20 70 57 61 6c 2d zPage;. pWal-
1aa2c 3e 68 64 72 2e 62 69 67 45 6e 64 43 6b 73 75 6d >hdr.bigEndCksum
1aa2d 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 = SQLITE_BIGEND
1aa2e 49 41 4e 3b 0a 20 20 20 20 70 57 61 6c 2d 3e 68 IAN;. pWal->h
1aa2f 64 72 2e 61 46 72 61 6d 65 43 6b 73 75 6d 5b 30 dr.aFrameCksum[0
1aa30 5d 20 3d 20 61 43 6b 73 75 6d 5b 30 5d 3b 0a 20 ] = aCksum[0];.
1aa31 20 20 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72 pWal->hdr.aFr
1aa32 61 6d 65 43 6b 73 75 6d 5b 31 5d 20 3d 20 61 43 ameCksum[1] = aC
1aa33 6b 73 75 6d 5b 31 5d 3b 0a 20 20 20 20 70 57 61 ksum[1];. pWa
1aa34 6c 2d 3e 74 72 75 6e 63 61 74 65 4f 6e 43 6f 6d l->truncateOnCom
1aa35 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 20 20 72 63 mit = 1;.. rc
1aa36 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
1aa37 65 28 70 57 61 6c 2d 3e 70 57 61 6c 46 64 2c 20 e(pWal->pWalFd,
1aa38 61 57 61 6c 48 64 72 2c 20 73 69 7a 65 6f 66 28 aWalHdr, sizeof(
1aa39 61 57 61 6c 48 64 72 29 2c 20 30 29 3b 0a 20 20 aWalHdr), 0);.
1aa3a 20 20 57 41 4c 54 52 41 43 45 28 28 22 57 41 4c WALTRACE(("WAL
1aa3b 25 70 3a 20 77 61 6c 2d 68 65 61 64 65 72 20 77 %p: wal-header w
1aa3c 72 69 74 65 20 25 73 5c 6e 22 2c 20 70 57 61 6c rite %s\n", pWal
1aa3d 2c 20 72 63 20 3f 20 22 66 61 69 6c 65 64 22 20 , rc ? "failed"
1aa3e 3a 20 22 6f 6b 22 29 29 3b 0a 20 20 20 20 69 66 : "ok"));. if
1aa3f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1aa40 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1aa41 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f rc;. }.. /
1aa42 2a 20 53 79 6e 63 20 74 68 65 20 68 65 61 64 65 * Sync the heade
1aa43 72 20 28 75 6e 6c 65 73 73 20 53 51 4c 49 54 45 r (unless SQLITE
1aa44 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 _IOCAP_SEQUENTIA
1aa45 4c 20 69 73 20 74 72 75 65 20 6f 72 20 75 6e 6c L is true or unl
1aa46 65 73 73 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 73 ess. ** all s
1aa47 79 6e 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64 yncing is turned
1aa48 20 6f 66 66 20 62 79 20 50 52 41 47 4d 41 20 73 off by PRAGMA s
1aa49 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 29 2e ynchronous=OFF).
1aa4a 20 20 4f 74 68 65 72 77 69 73 65 0a 20 20 20 20 Otherwise.
1aa4b 2a 2a 20 61 6e 20 6f 75 74 2d 6f 66 2d 6f 72 64 ** an out-of-ord
1aa4c 65 72 20 77 72 69 74 65 20 66 6f 6c 6c 6f 77 69 er write followi
1aa4d 6e 67 20 61 20 57 41 4c 20 72 65 73 74 61 72 74 ng a WAL restart
1aa4e 20 63 6f 75 6c 64 20 72 65 73 75 6c 74 20 69 6e could result in
1aa4f 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 . ** database
1aa50 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 20 53 65 corruption. Se
1aa51 65 20 74 68 65 20 74 69 63 6b 65 74 3a 0a 20 20 e the ticket:.
1aa52 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 **. **
1aa53 68 74 74 70 3a 2f 2f 6c 6f 63 61 6c 68 6f 73 74 http://localhost
1aa54 3a 35 39 31 2f 73 71 6c 69 74 65 2f 69 6e 66 6f :591/sqlite/info
1aa55 2f 66 66 35 62 65 37 33 64 65 65 0a 20 20 20 20 /ff5be73dee.
1aa56 2a 2f 0a 20 20 20 20 69 66 28 20 70 57 61 6c 2d */. if( pWal-
1aa57 3e 73 79 6e 63 48 65 61 64 65 72 20 26 26 20 73 >syncHeader && s
1aa58 79 6e 63 5f 66 6c 61 67 73 20 29 7b 0a 20 20 20 ync_flags ){.
1aa59 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1aa5a 73 53 79 6e 63 28 70 57 61 6c 2d 3e 70 57 61 6c sSync(pWal->pWal
1aa5b 46 64 2c 20 73 79 6e 63 5f 66 6c 61 67 73 20 26 Fd, sync_flags &
1aa5c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4d 41 53 SQLITE_SYNC_MAS
1aa5d 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 K);. if( rc
1aa5e 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
1aa5f 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 }. }. assert
1aa60 28 20 28 69 6e 74 29 70 57 61 6c 2d 3e 73 7a 50 ( (int)pWal->szP
1aa61 61 67 65 3d 3d 73 7a 50 61 67 65 20 29 3b 0a 0a age==szPage );..
1aa62 20 20 2f 2a 20 53 65 74 75 70 20 69 6e 66 6f 72 /* Setup infor
1aa63 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f mation needed to
1aa64 20 77 72 69 74 65 20 66 72 61 6d 65 73 20 69 6e write frames in
1aa65 74 6f 20 74 68 65 20 57 41 4c 20 2a 2f 0a 20 20 to the WAL */.
1aa66 77 2e 70 57 61 6c 20 3d 20 70 57 61 6c 3b 0a 20 w.pWal = pWal;.
1aa67 20 77 2e 70 46 64 20 3d 20 70 57 61 6c 2d 3e 70 w.pFd = pWal->p
1aa68 57 61 6c 46 64 3b 0a 20 20 77 2e 69 53 79 6e 63 WalFd;. w.iSync
1aa69 50 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 77 2e 73 Point = 0;. w.s
1aa6a 79 6e 63 46 6c 61 67 73 20 3d 20 73 79 6e 63 5f yncFlags = sync_
1aa6b 66 6c 61 67 73 3b 0a 20 20 77 2e 73 7a 50 61 67 flags;. w.szPag
1aa6c 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 69 4f e = szPage;. iO
1aa6d 66 66 73 65 74 20 3d 20 77 61 6c 46 72 61 6d 65 ffset = walFrame
1aa6e 4f 66 66 73 65 74 28 69 46 72 61 6d 65 2b 31 2c Offset(iFrame+1,
1aa6f 20 73 7a 50 61 67 65 29 3b 0a 20 20 73 7a 46 72 szPage);. szFr
1aa70 61 6d 65 20 3d 20 73 7a 50 61 67 65 20 2b 20 57 ame = szPage + W
1aa71 41 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 5a 45 AL_FRAME_HDRSIZE
1aa72 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 61 6c ;.. /* Write al
1aa73 6c 20 66 72 61 6d 65 73 20 69 6e 74 6f 20 74 68 l frames into th
1aa74 65 20 6c 6f 67 20 66 69 6c 65 20 65 78 61 63 74 e log file exact
1aa75 6c 79 20 6f 6e 63 65 20 2a 2f 0a 20 20 66 6f 72 ly once */. for
1aa76 28 70 3d 70 4c 69 73 74 3b 20 70 3b 20 70 3d 70 (p=pList; p; p=p
1aa77 2d 3e 70 44 69 72 74 79 29 7b 0a 20 20 20 20 69 ->pDirty){. i
1aa78 6e 74 20 6e 44 62 53 69 7a 65 3b 20 20 20 2f 2a nt nDbSize; /*
1aa79 20 30 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 50 6f 0 normally. Po
1aa7a 73 69 74 69 76 65 20 3d 3d 20 63 6f 6d 6d 69 74 sitive == commit
1aa7b 20 66 6c 61 67 20 2a 2f 0a 20 20 20 20 69 46 72 flag */. iFr
1aa7c 61 6d 65 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 ame++;. asser
1aa7d 74 28 20 69 4f 66 66 73 65 74 3d 3d 77 61 6c 46 t( iOffset==walF
1aa7e 72 61 6d 65 4f 66 66 73 65 74 28 69 46 72 61 6d rameOffset(iFram
1aa7f 65 2c 20 73 7a 50 61 67 65 29 20 29 3b 0a 20 20 e, szPage) );.
1aa80 20 20 6e 44 62 53 69 7a 65 20 3d 20 28 69 73 43 nDbSize = (isC
1aa81 6f 6d 6d 69 74 20 26 26 20 70 2d 3e 70 44 69 72 ommit && p->pDir
1aa82 74 79 3d 3d 30 29 20 3f 20 6e 54 72 75 6e 63 61 ty==0) ? nTrunca
1aa83 74 65 20 3a 20 30 3b 0a 20 20 20 20 72 63 20 3d te : 0;. rc =
1aa84 20 77 61 6c 57 72 69 74 65 4f 6e 65 46 72 61 6d walWriteOneFram
1aa85 65 28 26 77 2c 20 70 2c 20 6e 44 62 53 69 7a 65 e(&w, p, nDbSize
1aa86 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 , iOffset);.
1aa87 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
1aa88 72 63 3b 0a 20 20 20 20 70 4c 61 73 74 20 3d 20 rc;. pLast =
1aa89 70 3b 0a 20 20 20 20 69 4f 66 66 73 65 74 20 2b p;. iOffset +
1aa8a 3d 20 73 7a 46 72 61 6d 65 3b 0a 20 20 7d 0a 0a = szFrame;. }..
1aa8b 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
1aa8c 74 68 65 20 65 6e 64 20 6f 66 20 61 20 74 72 61 the end of a tra
1aa8d 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 77 nsaction, then w
1aa8e 65 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20 e might need to
1aa8f 70 61 64 0a 20 20 2a 2a 20 74 68 65 20 74 72 61 pad. ** the tra
1aa90 6e 73 61 63 74 69 6f 6e 20 61 6e 64 2f 6f 72 20 nsaction and/or
1aa91 73 79 6e 63 20 74 68 65 20 57 41 4c 20 66 69 6c sync the WAL fil
1aa92 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 50 61 64 e.. **. ** Pad
1aa93 64 69 6e 67 20 61 6e 64 20 73 79 6e 63 69 6e 67 ding and syncing
1aa94 20 6f 6e 6c 79 20 6f 63 63 75 72 20 69 66 20 74 only occur if t
1aa95 68 69 73 20 73 65 74 20 6f 66 20 66 72 61 6d 65 his set of frame
1aa96 73 20 63 6f 6d 70 6c 65 74 65 20 61 0a 20 20 2a s complete a. *
1aa97 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e * transaction an
1aa98 64 20 69 66 20 50 52 41 47 4d 41 20 73 79 6e 63 d if PRAGMA sync
1aa99 68 72 6f 6e 6f 75 73 3d 46 55 4c 4c 2e 20 20 49 hronous=FULL. I
1aa9a 66 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 3d 4e f synchronous==N
1aa9b 4f 52 4d 41 4c 0a 20 20 2a 2a 20 6f 72 20 73 79 ORMAL. ** or sy
1aa9c 6e 63 68 6f 6e 6f 75 73 3d 3d 4f 46 46 2c 20 74 nchonous==OFF, t
1aa9d 68 65 6e 20 6e 6f 20 70 61 64 64 69 6e 67 20 6f hen no padding o
1aa9e 72 20 73 79 6e 63 69 6e 67 20 61 72 65 20 6e 65 r syncing are ne
1aa9f 65 64 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 eded.. **. **
1aaa0 49 66 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f If SQLITE_IOCAP_
1aaa1 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 52 POWERSAFE_OVERWR
1aaa2 49 54 45 20 69 73 20 64 65 66 69 6e 65 64 2c 20 ITE is defined,
1aaa3 74 68 65 6e 20 70 61 64 64 69 6e 67 20 69 73 20 then padding is
1aaa4 6e 6f 74 0a 20 20 2a 2a 20 6e 65 65 64 65 64 20 not. ** needed
1aaa5 61 6e 64 20 6f 6e 6c 79 20 74 68 65 20 73 79 6e and only the syn
1aaa6 63 20 69 73 20 64 6f 6e 65 2e 20 20 49 66 20 70 c is done. If p
1aaa7 61 64 64 69 6e 67 20 69 73 20 6e 65 65 64 65 64 adding is needed
1aaa8 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 , then the. **
1aaa9 66 69 6e 61 6c 20 66 72 61 6d 65 20 69 73 20 72 final frame is r
1aaaa 65 70 65 61 74 65 64 20 28 77 69 74 68 20 69 74 epeated (with it
1aaab 73 20 63 6f 6d 6d 69 74 20 6d 61 72 6b 29 20 75 s commit mark) u
1aaac 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 73 65 ntil the next se
1aaad 63 74 6f 72 0a 20 20 2a 2a 20 62 6f 75 6e 64 61 ctor. ** bounda
1aaae 72 79 20 69 73 20 63 72 6f 73 73 65 64 2e 20 20 ry is crossed.
1aaaf 4f 6e 6c 79 20 74 68 65 20 70 61 72 74 20 6f 66 Only the part of
1aab0 20 74 68 65 20 57 41 4c 20 70 72 69 6f 72 20 74 the WAL prior t
1aab1 6f 20 74 68 65 20 6c 61 73 74 0a 20 20 2a 2a 20 o the last. **
1aab2 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 sector boundary
1aab3 69 73 20 73 79 6e 63 65 64 3b 20 74 68 65 20 70 is synced; the p
1aab4 61 72 74 20 6f 66 20 74 68 65 20 6c 61 73 74 20 art of the last
1aab5 66 72 61 6d 65 20 74 68 61 74 20 65 78 74 65 6e frame that exten
1aab6 64 73 0a 20 20 2a 2a 20 70 61 73 74 20 74 68 65 ds. ** past the
1aab7 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 sector boundary
1aab8 20 69 73 20 77 72 69 74 74 65 6e 20 61 66 74 65 is written afte
1aab9 72 20 74 68 65 20 73 79 6e 63 2e 0a 20 20 2a 2f r the sync.. */
1aaba 0a 20 20 69 66 28 20 69 73 43 6f 6d 6d 69 74 20 . if( isCommit
1aabb 26 26 20 28 73 79 6e 63 5f 66 6c 61 67 73 20 26 && (sync_flags &
1aabc 20 57 41 4c 5f 53 59 4e 43 5f 54 52 41 4e 53 41 WAL_SYNC_TRANSA
1aabd 43 54 49 4f 4e 53 29 21 3d 30 20 29 7b 0a 20 20 CTIONS)!=0 ){.
1aabe 20 20 69 66 28 20 70 57 61 6c 2d 3e 70 61 64 54 if( pWal->padT
1aabf 6f 53 65 63 74 6f 72 42 6f 75 6e 64 61 72 79 20 oSectorBoundary
1aac0 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 65 63 ){. int sec
1aac1 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 torSize = sqlite
1aac2 33 53 65 63 74 6f 72 53 69 7a 65 28 70 57 61 6c 3SectorSize(pWal
1aac3 2d 3e 70 57 61 6c 46 64 29 3b 0a 20 20 20 20 20 ->pWalFd);.
1aac4 20 77 2e 69 53 79 6e 63 50 6f 69 6e 74 20 3d 20 w.iSyncPoint =
1aac5 28 28 69 4f 66 66 73 65 74 2b 73 65 63 74 6f 72 ((iOffset+sector
1aac6 53 69 7a 65 2d 31 29 2f 73 65 63 74 6f 72 53 69 Size-1)/sectorSi
1aac7 7a 65 29 2a 73 65 63 74 6f 72 53 69 7a 65 3b 0a ze)*sectorSize;.
1aac8 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 4f 66 while( iOf
1aac9 66 73 65 74 3c 77 2e 69 53 79 6e 63 50 6f 69 6e fset<w.iSyncPoin
1aaca 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 t ){. rc
1aacb 3d 20 77 61 6c 57 72 69 74 65 4f 6e 65 46 72 61 = walWriteOneFra
1aacc 6d 65 28 26 77 2c 20 70 4c 61 73 74 2c 20 6e 54 me(&w, pLast, nT
1aacd 72 75 6e 63 61 74 65 2c 20 69 4f 66 66 73 65 74 runcate, iOffset
1aace 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
1aacf 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
1aad0 20 20 20 20 20 20 20 69 4f 66 66 73 65 74 20 2b iOffset +
1aad1 3d 20 73 7a 46 72 61 6d 65 3b 0a 20 20 20 20 20 = szFrame;.
1aad2 20 20 20 6e 45 78 74 72 61 2b 2b 3b 0a 20 20 20 nExtra++;.
1aad3 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
1aad4 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1aad5 65 33 4f 73 53 79 6e 63 28 77 2e 70 46 64 2c 20 e3OsSync(w.pFd,
1aad6 73 79 6e 63 5f 66 6c 61 67 73 20 26 20 53 51 4c sync_flags & SQL
1aad7 49 54 45 5f 53 59 4e 43 5f 4d 41 53 4b 29 3b 0a ITE_SYNC_MASK);.
1aad8 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
1aad9 49 66 20 74 68 69 73 20 66 72 61 6d 65 20 73 65 If this frame se
1aada 74 20 63 6f 6d 70 6c 65 74 65 73 20 74 68 65 20 t completes the
1aadb 66 69 72 73 74 20 74 72 61 6e 73 61 63 74 69 6f first transactio
1aadc 6e 20 69 6e 20 74 68 65 20 57 41 4c 20 61 6e 64 n in the WAL and
1aadd 0a 20 20 2a 2a 20 69 66 20 50 52 41 47 4d 41 20 . ** if PRAGMA
1aade 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d journal_size_lim
1aadf 69 74 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 it is set, then
1aae0 74 72 75 6e 63 61 74 65 20 74 68 65 20 57 41 4c truncate the WAL
1aae1 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 to the. ** jou
1aae2 72 6e 61 6c 20 73 69 7a 65 20 6c 69 6d 69 74 2c rnal size limit,
1aae3 20 69 66 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 if possible..
1aae4 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f 6d 6d 69 */. if( isCommi
1aae5 74 20 26 26 20 70 57 61 6c 2d 3e 74 72 75 6e 63 t && pWal->trunc
1aae6 61 74 65 4f 6e 43 6f 6d 6d 69 74 20 26 26 20 70 ateOnCommit && p
1aae7 57 61 6c 2d 3e 6d 78 57 61 6c 53 69 7a 65 3e 3d Wal->mxWalSize>=
1aae8 30 20 29 7b 0a 20 20 20 20 69 36 34 20 73 7a 20 0 ){. i64 sz
1aae9 3d 20 70 57 61 6c 2d 3e 6d 78 57 61 6c 53 69 7a = pWal->mxWalSiz
1aaea 65 3b 0a 20 20 20 20 69 66 28 20 77 61 6c 46 72 e;. if( walFr
1aaeb 61 6d 65 4f 66 66 73 65 74 28 69 46 72 61 6d 65 ameOffset(iFrame
1aaec 2b 6e 45 78 74 72 61 2b 31 2c 20 73 7a 50 61 67 +nExtra+1, szPag
1aaed 65 29 3e 70 57 61 6c 2d 3e 6d 78 57 61 6c 53 69 e)>pWal->mxWalSi
1aaee 7a 65 20 29 7b 0a 20 20 20 20 20 20 73 7a 20 3d ze ){. sz =
1aaef 20 77 61 6c 46 72 61 6d 65 4f 66 66 73 65 74 28 walFrameOffset(
1aaf0 69 46 72 61 6d 65 2b 6e 45 78 74 72 61 2b 31 2c iFrame+nExtra+1,
1aaf1 20 73 7a 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a szPage);. }.
1aaf2 20 20 20 20 77 61 6c 4c 69 6d 69 74 53 69 7a 65 walLimitSize
1aaf3 28 70 57 61 6c 2c 20 73 7a 29 3b 0a 20 20 20 20 (pWal, sz);.
1aaf4 70 57 61 6c 2d 3e 74 72 75 6e 63 61 74 65 4f 6e pWal->truncateOn
1aaf5 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 0a Commit = 0;. }.
1aaf6 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 64 61 74 . /* Append dat
1aaf7 61 20 74 6f 20 74 68 65 20 77 61 6c 2d 69 6e 64 a to the wal-ind
1aaf8 65 78 2e 20 49 74 20 69 73 20 6e 6f 74 20 6e 65 ex. It is not ne
1aaf9 63 65 73 73 61 72 79 20 74 6f 20 6c 6f 63 6b 20 cessary to lock
1aafa 74 68 65 20 0a 20 20 2a 2a 20 77 61 6c 2d 69 6e the . ** wal-in
1aafb 64 65 78 20 74 6f 20 64 6f 20 74 68 69 73 20 61 dex to do this a
1aafc 73 20 74 68 65 20 53 51 4c 49 54 45 5f 53 48 4d s the SQLITE_SHM
1aafd 5f 57 52 49 54 45 20 6c 6f 63 6b 20 68 65 6c 64 _WRITE lock held
1aafe 20 6f 6e 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 on the wal-inde
1aaff 78 0a 20 20 2a 2a 20 67 75 61 72 61 6e 74 65 65 x. ** guarantee
1ab00 73 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65 s that there are
1ab01 20 6e 6f 20 6f 74 68 65 72 20 77 72 69 74 65 72 no other writer
1ab02 73 2c 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 74 s, and no data t
1ab03 68 61 74 20 6d 61 79 0a 20 20 2a 2a 20 62 65 20 hat may. ** be
1ab04 69 6e 20 75 73 65 20 62 79 20 65 78 69 73 74 69 in use by existi
1ab05 6e 67 20 72 65 61 64 65 72 73 20 69 73 20 62 65 ng readers is be
1ab06 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e ing overwritten.
1ab07 0a 20 20 2a 2f 0a 20 20 69 46 72 61 6d 65 20 3d . */. iFrame =
1ab08 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 pWal->hdr.mxFra
1ab09 6d 65 3b 0a 20 20 66 6f 72 28 70 3d 70 4c 69 73 me;. for(p=pLis
1ab0a 74 3b 20 70 20 26 26 20 72 63 3d 3d 53 51 4c 49 t; p && rc==SQLI
1ab0b 54 45 5f 4f 4b 3b 20 70 3d 70 2d 3e 70 44 69 72 TE_OK; p=p->pDir
1ab0c 74 79 29 7b 0a 20 20 20 20 69 46 72 61 6d 65 2b ty){. iFrame+
1ab0d 2b 3b 0a 20 20 20 20 72 63 20 3d 20 77 61 6c 49 +;. rc = walI
1ab0e 6e 64 65 78 41 70 70 65 6e 64 28 70 57 61 6c 2c ndexAppend(pWal,
1ab0f 20 69 46 72 61 6d 65 2c 20 70 2d 3e 70 67 6e 6f iFrame, p->pgno
1ab10 29 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 );. }. while(
1ab11 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1ab12 20 6e 45 78 74 72 61 3e 30 20 29 7b 0a 20 20 20 nExtra>0 ){.
1ab13 20 69 46 72 61 6d 65 2b 2b 3b 0a 20 20 20 20 6e iFrame++;. n
1ab14 45 78 74 72 61 2d 2d 3b 0a 20 20 20 20 72 63 20 Extra--;. rc
1ab15 3d 20 77 61 6c 49 6e 64 65 78 41 70 70 65 6e 64 = walIndexAppend
1ab16 28 70 57 61 6c 2c 20 69 46 72 61 6d 65 2c 20 70 (pWal, iFrame, p
1ab17 4c 61 73 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d Last->pgno);. }
1ab18 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
1ab19 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 TE_OK ){. /*
1ab1a 55 70 64 61 74 65 20 74 68 65 20 70 72 69 76 61 Update the priva
1ab1b 74 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 68 te copy of the h
1ab1c 65 61 64 65 72 2e 20 2a 2f 0a 20 20 20 20 70 57 eader. */. pW
1ab1d 61 6c 2d 3e 68 64 72 2e 73 7a 50 61 67 65 20 3d al->hdr.szPage =
1ab1e 20 28 75 31 36 29 28 28 73 7a 50 61 67 65 26 30 (u16)((szPage&0
1ab1f 78 66 66 30 30 29 20 7c 20 28 73 7a 50 61 67 65 xff00) | (szPage
1ab20 3e 3e 31 36 29 29 3b 0a 20 20 20 20 74 65 73 74 >>16));. test
1ab21 63 61 73 65 28 20 73 7a 50 61 67 65 3c 3d 33 32 case( szPage<=32
1ab22 37 36 38 20 29 3b 0a 20 20 20 20 74 65 73 74 63 768 );. testc
1ab23 61 73 65 28 20 73 7a 50 61 67 65 3e 3d 36 35 35 ase( szPage>=655
1ab24 33 36 20 29 3b 0a 20 20 20 20 70 57 61 6c 2d 3e 36 );. pWal->
1ab25 68 64 72 2e 6d 78 46 72 61 6d 65 20 3d 20 69 46 hdr.mxFrame = iF
1ab26 72 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 69 73 rame;. if( is
1ab27 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 Commit ){.
1ab28 70 57 61 6c 2d 3e 68 64 72 2e 69 43 68 61 6e 67 pWal->hdr.iChang
1ab29 65 2b 2b 3b 0a 20 20 20 20 20 20 70 57 61 6c 2d e++;. pWal-
1ab2a 3e 68 64 72 2e 6e 50 61 67 65 20 3d 20 6e 54 72 >hdr.nPage = nTr
1ab2b 75 6e 63 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 uncate;. }.
1ab2c 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
1ab2d 61 20 63 6f 6d 6d 69 74 2c 20 75 70 64 61 74 65 a commit, update
1ab2e 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 68 the wal-index h
1ab2f 65 61 64 65 72 20 74 6f 6f 2e 20 2a 2f 0a 20 20 eader too. */.
1ab30 20 20 69 66 28 20 69 73 43 6f 6d 6d 69 74 20 29 if( isCommit )
1ab31 7b 0a 20 20 20 20 20 20 77 61 6c 49 6e 64 65 78 {. walIndex
1ab32 57 72 69 74 65 48 64 72 28 70 57 61 6c 29 3b 0a WriteHdr(pWal);.
1ab33 20 20 20 20 20 20 70 57 61 6c 2d 3e 69 43 61 6c pWal->iCal
1ab34 6c 62 61 63 6b 20 3d 20 69 46 72 61 6d 65 3b 0a lback = iFrame;.
1ab35 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 57 41 4c }. }.. WAL
1ab36 54 52 41 43 45 28 28 22 57 41 4c 25 70 3a 20 66 TRACE(("WAL%p: f
1ab37 72 61 6d 65 20 77 72 69 74 65 20 25 73 5c 6e 22 rame write %s\n"
1ab38 2c 20 70 57 61 6c 2c 20 72 63 20 3f 20 22 66 61 , pWal, rc ? "fa
1ab39 69 6c 65 64 22 20 3a 20 22 6f 6b 22 29 29 3b 0a iled" : "ok"));.
1ab3a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1ab3b 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 /* .** This rout
1ab3c 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f ine is called to
1ab3d 20 69 6d 70 6c 65 6d 65 6e 74 20 73 71 6c 69 74 implement sqlit
1ab3e 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e e3_wal_checkpoin
1ab3f 74 28 29 20 61 6e 64 0a 2a 2a 20 72 65 6c 61 74 t() and.** relat
1ab40 65 64 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a ed interfaces..*
1ab41 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 43 48 *.** Obtain a CH
1ab42 45 43 4b 50 4f 49 4e 54 20 6c 6f 63 6b 20 61 6e ECKPOINT lock an
1ab43 64 20 74 68 65 6e 20 62 61 63 6b 66 69 6c 6c 20 d then backfill
1ab44 61 73 20 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74 as much informat
1ab45 69 6f 6e 20 61 73 0a 2a 2a 20 77 65 20 63 61 6e ion as.** we can
1ab46 20 66 72 6f 6d 20 57 41 4c 20 69 6e 74 6f 20 74 from WAL into t
1ab47 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a he database..**.
1ab48 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 ** If parameter
1ab49 78 42 75 73 79 20 69 73 20 6e 6f 74 20 4e 55 4c xBusy is not NUL
1ab4a 4c 2c 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 L, it is a point
1ab4b 65 72 20 74 6f 20 61 20 62 75 73 79 2d 68 61 6e er to a busy-han
1ab4c 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b dler.** callback
1ab4d 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
1ab4e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 75 6e his function run
1ab4f 73 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63 68 65 s a blocking che
1ab50 63 6b 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 ckpoint..*/.SQLI
1ab51 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1ab52 71 6c 69 74 65 33 57 61 6c 43 68 65 63 6b 70 6f qlite3WalCheckpo
1ab53 69 6e 74 28 0a 20 20 57 61 6c 20 2a 70 57 61 6c int(. Wal *pWal
1ab54 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1ab55 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 20 63 6f /* Wal co
1ab56 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e nnection */. in
1ab57 74 20 65 4d 6f 64 65 2c 20 20 20 20 20 20 20 20 t eMode,
1ab58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ab59 20 50 41 53 53 49 56 45 2c 20 46 55 4c 4c 20 6f PASSIVE, FULL o
1ab5a 72 20 52 45 53 54 41 52 54 20 2a 2f 0a 20 20 69 r RESTART */. i
1ab5b 6e 74 20 28 2a 78 42 75 73 79 29 28 76 6f 69 64 nt (*xBusy)(void
1ab5c 2a 29 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f *), /
1ab5d 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 * Function to ca
1ab5e 6c 6c 20 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a ll when busy */.
1ab5f 20 20 76 6f 69 64 20 2a 70 42 75 73 79 41 72 67 void *pBusyArg
1ab60 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1ab61 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 61 72 67 /* Context arg
1ab62 75 6d 65 6e 74 20 66 6f 72 20 78 42 75 73 79 48 ument for xBusyH
1ab63 61 6e 64 6c 65 72 20 2a 2f 0a 20 20 69 6e 74 20 andler */. int
1ab64 73 79 6e 63 5f 66 6c 61 67 73 2c 20 20 20 20 20 sync_flags,
1ab65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
1ab66 6c 61 67 73 20 74 6f 20 73 79 6e 63 20 64 62 20 lags to sync db
1ab67 66 69 6c 65 20 77 69 74 68 20 28 6f 72 20 30 29 file with (or 0)
1ab68 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 66 2c 20 */. int nBuf,
1ab69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ab6a 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
1ab6b 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 temporary buffe
1ab6c 72 20 2a 2f 0a 20 20 75 38 20 2a 7a 42 75 66 2c r */. u8 *zBuf,
1ab6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ab6e 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 /* Tempor
1ab6f 61 72 79 20 62 75 66 66 65 72 20 74 6f 20 75 73 ary buffer to us
1ab70 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4c 6f e */. int *pnLo
1ab71 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g,
1ab72 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e /* OUT: N
1ab73 75 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73 20 umber of frames
1ab74 69 6e 20 57 41 4c 20 2a 2f 0a 20 20 69 6e 74 20 in WAL */. int
1ab75 2a 70 6e 43 6b 70 74 20 20 20 20 20 20 20 20 20 *pnCkpt
1ab76 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1ab77 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 UT: Number of ba
1ab78 63 6b 66 69 6c 6c 65 64 20 66 72 61 6d 65 73 20 ckfilled frames
1ab79 69 6e 20 57 41 4c 20 2a 2f 0a 29 7b 0a 20 20 69 in WAL */.){. i
1ab7a 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
1ab7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1ab7c 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
1ab7d 0a 20 20 69 6e 74 20 69 73 43 68 61 6e 67 65 64 . int isChanged
1ab7e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1ab7f 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 /* True if a
1ab80 6e 65 77 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 new wal-index he
1ab81 61 64 65 72 20 69 73 20 6c 6f 61 64 65 64 20 2a ader is loaded *
1ab82 2f 0a 20 20 69 6e 74 20 65 4d 6f 64 65 32 20 3d /. int eMode2 =
1ab83 20 65 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 eMode;
1ab84 20 20 20 20 2f 2a 20 4d 6f 64 65 20 74 6f 20 70 /* Mode to p
1ab85 61 73 73 20 74 6f 20 77 61 6c 43 68 65 63 6b 70 ass to walCheckp
1ab86 6f 69 6e 74 28 29 20 2a 2f 0a 0a 20 20 61 73 73 oint() */.. ass
1ab87 65 72 74 28 20 70 57 61 6c 2d 3e 63 6b 70 74 4c ert( pWal->ckptL
1ab88 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 ock==0 );. asse
1ab89 72 74 28 20 70 57 61 6c 2d 3e 77 72 69 74 65 4c rt( pWal->writeL
1ab8a 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 ock==0 );.. if(
1ab8b 20 70 57 61 6c 2d 3e 72 65 61 64 4f 6e 6c 79 20 pWal->readOnly
1ab8c 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
1ab8d 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 57 41 4c 54 READONLY;. WALT
1ab8e 52 41 43 45 28 28 22 57 41 4c 25 70 3a 20 63 68 RACE(("WAL%p: ch
1ab8f 65 63 6b 70 6f 69 6e 74 20 62 65 67 69 6e 73 5c eckpoint begins\
1ab90 6e 22 2c 20 70 57 61 6c 29 29 3b 0a 20 20 72 63 n", pWal));. rc
1ab91 20 3d 20 77 61 6c 4c 6f 63 6b 45 78 63 6c 75 73 = walLockExclus
1ab92 69 76 65 28 70 57 61 6c 2c 20 57 41 4c 5f 43 4b ive(pWal, WAL_CK
1ab93 50 54 5f 4c 4f 43 4b 2c 20 31 29 3b 0a 20 20 69 PT_LOCK, 1);. i
1ab94 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20 f( rc ){. /*
1ab95 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 73 20 Usually this is
1ab96 53 51 4c 49 54 45 5f 42 55 53 59 20 6d 65 61 6e SQLITE_BUSY mean
1ab97 69 6e 67 20 74 68 61 74 20 61 6e 6f 74 68 65 72 ing that another
1ab98 20 74 68 72 65 61 64 20 6f 72 20 70 72 6f 63 65 thread or proce
1ab99 73 73 0a 20 20 20 20 2a 2a 20 69 73 20 61 6c 72 ss. ** is alr
1ab9a 65 61 64 79 20 72 75 6e 6e 69 6e 67 20 61 20 63 eady running a c
1ab9b 68 65 63 6b 70 6f 69 6e 74 2c 20 6f 72 20 6d 61 heckpoint, or ma
1ab9c 79 62 65 20 61 20 72 65 63 6f 76 65 72 79 2e 20 ybe a recovery.
1ab9d 20 42 75 74 20 69 74 20 6d 69 67 68 74 0a 20 20 But it might.
1ab9e 20 20 2a 2a 20 61 6c 73 6f 20 62 65 20 53 51 4c ** also be SQL
1ab9f 49 54 45 5f 49 4f 45 52 52 2e 20 2a 2f 0a 20 20 ITE_IOERR. */.
1aba0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
1aba1 0a 20 20 70 57 61 6c 2d 3e 63 6b 70 74 4c 6f 63 . pWal->ckptLoc
1aba2 6b 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 49 66 20 k = 1;.. /* If
1aba3 74 68 69 73 20 69 73 20 61 20 62 6c 6f 63 6b 69 this is a blocki
1aba4 6e 67 2d 63 68 65 63 6b 70 6f 69 6e 74 2c 20 74 ng-checkpoint, t
1aba5 68 65 6e 20 6f 62 74 61 69 6e 20 74 68 65 20 77 hen obtain the w
1aba6 72 69 74 65 2d 6c 6f 63 6b 20 61 73 20 77 65 6c rite-lock as wel
1aba7 6c 0a 20 20 2a 2a 20 74 6f 20 70 72 65 76 65 6e l. ** to preven
1aba8 74 20 61 6e 79 20 77 72 69 74 65 72 73 20 66 72 t any writers fr
1aba9 6f 6d 20 72 75 6e 6e 69 6e 67 20 77 68 69 6c 65 om running while
1abaa 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e 74 20 the checkpoint
1abab 69 73 20 75 6e 64 65 72 77 61 79 2e 0a 20 20 2a is underway.. *
1abac 2a 20 54 68 69 73 20 68 61 73 20 74 6f 20 62 65 * This has to be
1abad 20 64 6f 6e 65 20 62 65 66 6f 72 65 20 74 68 65 done before the
1abae 20 63 61 6c 6c 20 74 6f 20 77 61 6c 49 6e 64 65 call to walInde
1abaf 78 52 65 61 64 48 64 72 28 29 20 62 65 6c 6f 77 xReadHdr() below
1abb0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 .. **. ** If t
1abb1 68 65 20 77 72 69 74 65 72 20 6c 6f 63 6b 20 63 he writer lock c
1abb2 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 annot be obtaine
1abb3 64 2c 20 74 68 65 6e 20 61 20 70 61 73 73 69 76 d, then a passiv
1abb4 65 20 63 68 65 63 6b 70 6f 69 6e 74 20 69 73 0a e checkpoint is.
1abb5 20 20 2a 2a 20 72 75 6e 20 69 6e 73 74 65 61 64 ** run instead
1abb6 2e 20 53 69 6e 63 65 20 74 68 65 20 63 68 65 63 . Since the chec
1abb7 6b 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 kpointer is not
1abb8 68 6f 6c 64 69 6e 67 20 74 68 65 20 77 72 69 74 holding the writ
1abb9 65 72 20 6c 6f 63 6b 2c 0a 20 20 2a 2a 20 74 68 er lock,. ** th
1abba 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74 20 ere is no point
1abbb 69 6e 20 62 6c 6f 63 6b 69 6e 67 20 77 61 69 74 in blocking wait
1abbc 69 6e 67 20 66 6f 72 20 61 6e 79 20 72 65 61 64 ing for any read
1abbd 65 72 73 2e 20 41 73 73 75 6d 69 6e 67 20 6e 6f ers. Assuming no
1abbe 20 0a 20 20 2a 2a 20 6f 74 68 65 72 20 65 72 72 . ** other err
1abbf 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 69 73 20 or occurs, this
1abc0 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 72 65 function will re
1abc1 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 turn SQLITE_BUSY
1abc2 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a to the caller..
1abc3 20 20 2a 2f 0a 20 20 69 66 28 20 65 4d 6f 64 65 */. if( eMode
1abc4 21 3d 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f !=SQLITE_CHECKPO
1abc5 49 4e 54 5f 50 41 53 53 49 56 45 20 29 7b 0a 20 INT_PASSIVE ){.
1abc6 20 20 20 72 63 20 3d 20 77 61 6c 42 75 73 79 4c rc = walBusyL
1abc7 6f 63 6b 28 70 57 61 6c 2c 20 78 42 75 73 79 2c ock(pWal, xBusy,
1abc8 20 70 42 75 73 79 41 72 67 2c 20 57 41 4c 5f 57 pBusyArg, WAL_W
1abc9 52 49 54 45 5f 4c 4f 43 4b 2c 20 31 29 3b 0a 20 RITE_LOCK, 1);.
1abca 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1abcb 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 57 E_OK ){. pW
1abcc 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20 3d 20 al->writeLock =
1abcd 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 1;. }else if(
1abce 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 rc==SQLITE_BUSY
1abcf 20 29 7b 0a 20 20 20 20 20 20 65 4d 6f 64 65 32 ){. eMode2
1abd0 20 3d 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 = SQLITE_CHECKP
1abd1 4f 49 4e 54 5f 50 41 53 53 49 56 45 3b 0a 20 20 OINT_PASSIVE;.
1abd2 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1abd3 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 OK;. }. }..
1abd4 20 2f 2a 20 52 65 61 64 20 74 68 65 20 77 61 6c /* Read the wal
1abd5 2d 69 6e 64 65 78 20 68 65 61 64 65 72 2e 20 2a -index header. *
1abd6 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
1abd7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 TE_OK ){. rc
1abd8 3d 20 77 61 6c 49 6e 64 65 78 52 65 61 64 48 64 = walIndexReadHd
1abd9 72 28 70 57 61 6c 2c 20 26 69 73 43 68 61 6e 67 r(pWal, &isChang
1abda 65 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 ed);. }.. /* C
1abdb 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 74 68 opy data from th
1abdc 65 20 6c 6f 67 20 74 6f 20 74 68 65 20 64 61 74 e log to the dat
1abdd 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 abase file. */.
1abde 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1abdf 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 57 OK ){. if( pW
1abe0 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 20 al->hdr.mxFrame
1abe1 26 26 20 77 61 6c 50 61 67 65 73 69 7a 65 28 70 && walPagesize(p
1abe2 57 61 6c 29 21 3d 6e 42 75 66 20 29 7b 0a 20 20 Wal)!=nBuf ){.
1abe3 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1abe4 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1abe5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
1abe6 63 20 3d 20 77 61 6c 43 68 65 63 6b 70 6f 69 6e c = walCheckpoin
1abe7 74 28 70 57 61 6c 2c 20 65 4d 6f 64 65 32 2c 20 t(pWal, eMode2,
1abe8 78 42 75 73 79 2c 20 70 42 75 73 79 41 72 67 2c xBusy, pBusyArg,
1abe9 20 73 79 6e 63 5f 66 6c 61 67 73 2c 20 7a 42 75 sync_flags, zBu
1abea 66 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f f);. }.. /
1abeb 2a 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 * If no error oc
1abec 63 75 72 72 65 64 2c 20 73 65 74 20 74 68 65 20 curred, set the
1abed 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 73 output variables
1abee 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d . */. if( rc=
1abef 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 =SQLITE_OK || rc
1abf0 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b ==SQLITE_BUSY ){
1abf1 0a 20 20 20 20 20 20 69 66 28 20 70 6e 4c 6f 67 . if( pnLog
1abf2 20 29 20 2a 70 6e 4c 6f 67 20 3d 20 28 69 6e 74 ) *pnLog = (int
1abf3 29 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 )pWal->hdr.mxFra
1abf4 6d 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 6e me;. if( pn
1abf5 43 6b 70 74 20 29 20 2a 70 6e 43 6b 70 74 20 3d Ckpt ) *pnCkpt =
1abf6 20 28 69 6e 74 29 28 77 61 6c 43 6b 70 74 49 6e (int)(walCkptIn
1abf7 66 6f 28 70 57 61 6c 29 2d 3e 6e 42 61 63 6b 66 fo(pWal)->nBackf
1abf8 69 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ill);. }. }.
1abf9 0a 20 20 69 66 28 20 69 73 43 68 61 6e 67 65 64 . if( isChanged
1abfa 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 ){. /* If a
1abfb 6e 65 77 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 new wal-index he
1abfc 61 64 65 72 20 77 61 73 20 6c 6f 61 64 65 64 20 ader was loaded
1abfd 62 65 66 6f 72 65 20 74 68 65 20 63 68 65 63 6b before the check
1abfe 70 6f 69 6e 74 20 77 61 73 20 0a 20 20 20 20 2a point was . *
1abff 2a 20 70 65 72 66 6f 72 6d 65 64 2c 20 74 68 65 * performed, the
1ac00 6e 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 n the pager-cach
1ac01 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
1ac02 68 20 70 57 61 6c 20 69 73 20 6e 6f 77 0a 20 20 h pWal is now.
1ac03 20 20 2a 2a 20 6f 75 74 20 6f 66 20 64 61 74 65 ** out of date
1ac04 2e 20 53 6f 20 7a 65 72 6f 20 74 68 65 20 63 61 . So zero the ca
1ac05 63 68 65 64 20 77 61 6c 2d 69 6e 64 65 78 20 68 ched wal-index h
1ac06 65 61 64 65 72 20 74 6f 20 65 6e 73 75 72 65 20 eader to ensure
1ac07 74 68 61 74 0a 20 20 20 20 2a 2a 20 6e 65 78 74 that. ** next
1ac08 20 74 69 6d 65 20 74 68 65 20 70 61 67 65 72 20 time the pager
1ac09 6f 70 65 6e 73 20 61 20 73 6e 61 70 73 68 6f 74 opens a snapshot
1ac0a 20 6f 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 on this databas
1ac0b 65 20 69 74 20 6b 6e 6f 77 73 20 74 68 61 74 0a e it knows that.
1ac0c 20 20 20 20 2a 2a 20 74 68 65 20 63 61 63 68 65 ** the cache
1ac0d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 needs to be res
1ac0e 65 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d et.. */. m
1ac0f 65 6d 73 65 74 28 26 70 57 61 6c 2d 3e 68 64 72 emset(&pWal->hdr
1ac10 2c 20 30 2c 20 73 69 7a 65 6f 66 28 57 61 6c 49 , 0, sizeof(WalI
1ac11 6e 64 65 78 48 64 72 29 29 3b 0a 20 20 7d 0a 0a ndexHdr));. }..
1ac12 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 /* Release the
1ac13 20 6c 6f 63 6b 73 2e 20 2a 2f 0a 20 20 73 71 6c locks. */. sql
1ac14 69 74 65 33 57 61 6c 45 6e 64 57 72 69 74 65 54 ite3WalEndWriteT
1ac15 72 61 6e 73 61 63 74 69 6f 6e 28 70 57 61 6c 29 ransaction(pWal)
1ac16 3b 0a 20 20 77 61 6c 55 6e 6c 6f 63 6b 45 78 63 ;. walUnlockExc
1ac17 6c 75 73 69 76 65 28 70 57 61 6c 2c 20 57 41 4c lusive(pWal, WAL
1ac18 5f 43 4b 50 54 5f 4c 4f 43 4b 2c 20 31 29 3b 0a _CKPT_LOCK, 1);.
1ac19 20 20 70 57 61 6c 2d 3e 63 6b 70 74 4c 6f 63 6b pWal->ckptLock
1ac1a 20 3d 20 30 3b 0a 20 20 57 41 4c 54 52 41 43 45 = 0;. WALTRACE
1ac1b 28 28 22 57 41 4c 25 70 3a 20 63 68 65 63 6b 70 (("WAL%p: checkp
1ac1c 6f 69 6e 74 20 25 73 5c 6e 22 2c 20 70 57 61 6c oint %s\n", pWal
1ac1d 2c 20 72 63 20 3f 20 22 66 61 69 6c 65 64 22 20 , rc ? "failed"
1ac1e 3a 20 22 6f 6b 22 29 29 3b 0a 20 20 72 65 74 75 : "ok"));. retu
1ac1f 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rn (rc==SQLITE_O
1ac20 4b 20 26 26 20 65 4d 6f 64 65 21 3d 65 4d 6f 64 K && eMode!=eMod
1ac21 65 32 20 3f 20 53 51 4c 49 54 45 5f 42 55 53 59 e2 ? SQLITE_BUSY
1ac22 20 3a 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 : rc);.}../* Re
1ac23 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 74 turn the value t
1ac24 6f 20 70 61 73 73 20 74 6f 20 61 20 73 71 6c 69 o pass to a sqli
1ac25 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b 20 63 61 6c te3_wal_hook cal
1ac26 6c 62 61 63 6b 2c 20 74 68 65 0a 2a 2a 20 6e 75 lback, the.** nu
1ac27 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73 20 69 mber of frames i
1ac28 6e 20 74 68 65 20 57 41 4c 20 61 74 20 74 68 65 n the WAL at the
1ac29 20 70 6f 69 6e 74 20 6f 66 20 74 68 65 20 6c 61 point of the la
1ac2a 73 74 20 63 6f 6d 6d 69 74 20 73 69 6e 63 65 0a st commit since.
1ac2b 2a 2a 20 73 71 6c 69 74 65 33 57 61 6c 43 61 6c ** sqlite3WalCal
1ac2c 6c 62 61 63 6b 28 29 20 77 61 73 20 63 61 6c 6c lback() was call
1ac2d 65 64 2e 20 20 49 66 20 6e 6f 20 63 6f 6d 6d 69 ed. If no commi
1ac2e 74 73 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 ts have occurred
1ac2f 20 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 6c 61 since.** the la
1ac30 73 74 20 63 61 6c 6c 2c 20 74 68 65 6e 20 72 65 st call, then re
1ac31 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 turn 0..*/.SQLIT
1ac32 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1ac33 6c 69 74 65 33 57 61 6c 43 61 6c 6c 62 61 63 6b lite3WalCallback
1ac34 28 57 61 6c 20 2a 70 57 61 6c 29 7b 0a 20 20 75 (Wal *pWal){. u
1ac35 33 32 20 72 65 74 20 3d 20 30 3b 0a 20 20 69 66 32 ret = 0;. if
1ac36 28 20 70 57 61 6c 20 29 7b 0a 20 20 20 20 72 65 ( pWal ){. re
1ac37 74 20 3d 20 70 57 61 6c 2d 3e 69 43 61 6c 6c 62 t = pWal->iCallb
1ac38 61 63 6b 3b 0a 20 20 20 20 70 57 61 6c 2d 3e 69 ack;. pWal->i
1ac39 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 Callback = 0;.
1ac3a 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 }. return (int)
1ac3b 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 ret;.}../*.** Th
1ac3c 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
1ac3d 61 6c 6c 65 64 20 74 6f 20 63 68 61 6e 67 65 20 alled to change
1ac3e 74 68 65 20 57 41 4c 20 73 75 62 73 79 73 74 65 the WAL subsyste
1ac3f 6d 20 69 6e 74 6f 20 6f 72 20 6f 75 74 0a 2a 2a m into or out.**
1ac40 20 6f 66 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 of locking_mode
1ac41 3d 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2a 0a 2a =EXCLUSIVE..**.*
1ac42 2a 20 49 66 20 6f 70 20 69 73 20 7a 65 72 6f 2c * If op is zero,
1ac43 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 74 6f then attempt to
1ac44 20 63 68 61 6e 67 65 20 66 72 6f 6d 20 6c 6f 63 change from loc
1ac45 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 king_mode=EXCLUS
1ac46 49 56 45 0a 2a 2a 20 69 6e 74 6f 20 6c 6f 63 6b IVE.** into lock
1ac47 69 6e 67 5f 6d 6f 64 65 3d 4e 4f 52 4d 41 4c 2e ing_mode=NORMAL.
1ac48 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 This means tha
1ac49 74 20 77 65 20 6d 75 73 74 20 61 63 71 75 69 72 t we must acquir
1ac4a 65 20 61 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 74 e a lock.** on t
1ac4b 68 65 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 he pWal->readLoc
1ac4c 6b 20 62 79 74 65 2e 20 20 49 66 20 74 68 65 20 k byte. If the
1ac4d 57 41 4c 20 69 73 20 61 6c 72 65 61 64 79 20 69 WAL is already i
1ac4e 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 4e n locking_mode=N
1ac4f 4f 52 4d 41 4c 0a 2a 2a 20 6f 72 20 69 66 20 74 ORMAL.** or if t
1ac50 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f he acquisition o
1ac51 66 20 74 68 65 20 6c 6f 63 6b 20 66 61 69 6c 73 f the lock fails
1ac52 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 30 2e , then return 0.
1ac53 20 20 49 66 20 74 68 65 0a 2a 2a 20 74 72 61 6e If the.** tran
1ac54 73 69 74 69 6f 6e 20 6f 75 74 20 6f 66 20 65 78 sition out of ex
1ac55 63 6c 75 73 69 76 65 2d 6d 6f 64 65 20 69 73 20 clusive-mode is
1ac56 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 successful, retu
1ac57 72 6e 20 31 2e 20 20 54 68 69 73 0a 2a 2a 20 6f rn 1. This.** o
1ac58 70 65 72 61 74 69 6f 6e 20 6d 75 73 74 20 6f 63 peration must oc
1ac59 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 70 61 cur while the pa
1ac5a 67 65 72 20 69 73 20 73 74 69 6c 6c 20 68 6f 6c ger is still hol
1ac5b 64 69 6e 67 20 74 68 65 20 65 78 63 6c 75 73 69 ding the exclusi
1ac5c 76 65 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 68 ve.** lock on th
1ac5d 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1ac5e 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6f file..**.** If o
1ac5f 70 20 69 73 20 6f 6e 65 2c 20 74 68 65 6e 20 63 p is one, then c
1ac60 68 61 6e 67 65 20 66 72 6f 6d 20 6c 6f 63 6b 69 hange from locki
1ac61 6e 67 5f 6d 6f 64 65 3d 4e 4f 52 4d 41 4c 20 69 ng_mode=NORMAL i
1ac62 6e 74 6f 20 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 5f nto .** locking_
1ac63 6d 6f 64 65 3d 45 58 43 4c 55 53 49 56 45 2e 20 mode=EXCLUSIVE.
1ac64 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 This means that
1ac65 20 74 68 65 20 70 57 61 6c 2d 3e 72 65 61 64 4c the pWal->readL
1ac66 6f 63 6b 20 6d 75 73 74 0a 2a 2a 20 62 65 20 72 ock must.** be r
1ac67 65 6c 65 61 73 65 64 2e 20 20 52 65 74 75 72 6e eleased. Return
1ac68 20 31 20 69 66 20 74 68 65 20 74 72 61 6e 73 69 1 if the transi
1ac69 74 69 6f 6e 20 69 73 20 6d 61 64 65 20 61 6e 64 tion is made and
1ac6a 20 30 20 69 66 20 74 68 65 0a 2a 2a 20 57 41 4c 0 if the.** WAL
1ac6b 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 65 is already in e
1ac6c 78 63 6c 75 73 69 76 65 2d 6c 6f 63 6b 69 6e 67 xclusive-locking
1ac6d 20 6d 6f 64 65 20 2d 20 6d 65 61 6e 69 6e 67 20 mode - meaning
1ac6e 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 72 6f 75 that this.** rou
1ac6f 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tine is a no-op.
1ac70 20 20 54 68 65 20 70 61 67 65 72 20 6d 75 73 74 The pager must
1ac71 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 74 68 already hold th
1ac72 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b e exclusive lock
1ac73 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 .** on the main
1ac74 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65 database file be
1ac75 66 6f 72 65 20 69 6e 76 6f 6b 69 6e 67 20 74 68 fore invoking th
1ac76 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a is operation..**
1ac77 0a 2a 2a 20 49 66 20 6f 70 20 69 73 20 6e 65 67 .** If op is neg
1ac78 61 74 69 76 65 2c 20 74 68 65 6e 20 64 6f 20 61 ative, then do a
1ac79 20 64 72 79 2d 72 75 6e 20 6f 66 20 74 68 65 20 dry-run of the
1ac7a 6f 70 3d 3d 31 20 63 61 73 65 20 62 75 74 20 64 op==1 case but d
1ac7b 6f 0a 2a 2a 20 6e 6f 74 20 61 63 74 75 61 6c 6c o.** not actuall
1ac7c 79 20 63 68 61 6e 67 65 20 61 6e 79 74 68 69 6e y change anythin
1ac7d 67 2e 20 54 68 65 20 70 61 67 65 72 20 75 73 65 g. The pager use
1ac7e 73 20 74 68 69 73 20 74 6f 20 73 65 65 20 69 66 s this to see if
1ac7f 20 69 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 61 63 it.** should ac
1ac80 71 75 69 72 65 20 74 68 65 20 64 61 74 61 62 61 quire the databa
1ac81 73 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 se exclusive loc
1ac82 6b 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b k prior to invok
1ac83 69 6e 67 0a 2a 2a 20 74 68 65 20 6f 70 3d 3d 31 ing.** the op==1
1ac84 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 case..*/.SQLITE
1ac85 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1ac86 69 74 65 33 57 61 6c 45 78 63 6c 75 73 69 76 65 ite3WalExclusive
1ac87 4d 6f 64 65 28 57 61 6c 20 2a 70 57 61 6c 2c 20 Mode(Wal *pWal,
1ac88 69 6e 74 20 6f 70 29 7b 0a 20 20 69 6e 74 20 72 int op){. int r
1ac89 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 61 c;. assert( pWa
1ac8a 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 l->writeLock==0
1ac8b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 61 );. assert( pWa
1ac8c 6c 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 l->exclusiveMode
1ac8d 21 3d 57 41 4c 5f 48 45 41 50 4d 45 4d 4f 52 59 !=WAL_HEAPMEMORY
1ac8e 5f 4d 4f 44 45 20 7c 7c 20 6f 70 3d 3d 2d 31 20 _MODE || op==-1
1ac8f 29 3b 0a 0a 20 20 2f 2a 20 70 57 61 6c 2d 3e 72 );.. /* pWal->r
1ac90 65 61 64 4c 6f 63 6b 20 69 73 20 75 73 75 61 6c eadLock is usual
1ac91 6c 79 20 73 65 74 2c 20 62 75 74 20 6d 69 67 68 ly set, but migh
1ac92 74 20 62 65 20 2d 31 20 69 66 20 74 68 65 72 65 t be -1 if there
1ac93 20 77 61 73 20 61 20 0a 20 20 2a 2a 20 70 72 69 was a . ** pri
1ac94 6f 72 20 65 72 72 6f 72 20 77 68 69 6c 65 20 61 or error while a
1ac95 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 61 63 71 ttempting to acq
1ac96 75 69 72 65 20 61 72 65 20 72 65 61 64 2d 6c 6f uire are read-lo
1ac97 63 6b 2e 20 54 68 69 73 20 63 61 6e 6e 6f 74 20 ck. This cannot
1ac98 0a 20 20 2a 2a 20 68 61 70 70 65 6e 20 69 66 20 . ** happen if
1ac99 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 the connection i
1ac9a 73 20 61 63 74 75 61 6c 6c 79 20 69 6e 20 65 78 s actually in ex
1ac9b 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 28 61 73 clusive mode (as
1ac9c 20 6e 6f 20 78 53 68 6d 4c 6f 63 6b 0a 20 20 2a no xShmLock. *
1ac9d 2a 20 6c 6f 63 6b 73 20 61 72 65 20 74 61 6b 65 * locks are take
1ac9e 6e 20 69 6e 20 74 68 69 73 20 63 61 73 65 29 2e n in this case).
1ac9f 20 4e 6f 72 20 73 68 6f 75 6c 64 20 74 68 65 20 Nor should the
1aca0 70 61 67 65 72 20 61 74 74 65 6d 70 74 20 74 6f pager attempt to
1aca1 0a 20 20 2a 2a 20 75 70 67 72 61 64 65 20 74 6f . ** upgrade to
1aca2 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 20 exclusive-mode
1aca3 66 6f 6c 6c 6f 77 69 6e 67 20 73 75 63 68 20 61 following such a
1aca4 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 n error.. */.
1aca5 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 72 65 assert( pWal->re
1aca6 61 64 4c 6f 63 6b 3e 3d 30 20 7c 7c 20 70 57 61 adLock>=0 || pWa
1aca7 6c 2d 3e 6c 6f 63 6b 45 72 72 6f 72 20 29 3b 0a l->lockError );.
1aca8 20 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e assert( pWal->
1aca9 72 65 61 64 4c 6f 63 6b 3e 3d 30 20 7c 7c 20 28 readLock>=0 || (
1acaa 6f 70 3c 3d 30 20 26 26 20 70 57 61 6c 2d 3e 65 op<=0 && pWal->e
1acab 78 63 6c 75 73 69 76 65 4d 6f 64 65 3d 3d 30 29 xclusiveMode==0)
1acac 20 29 3b 0a 0a 20 20 69 66 28 20 6f 70 3d 3d 30 );.. if( op==0
1acad 20 29 7b 0a 20 20 20 20 69 66 28 20 70 57 61 6c ){. if( pWal
1acae 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 ->exclusiveMode
1acaf 29 7b 0a 20 20 20 20 20 20 70 57 61 6c 2d 3e 65 ){. pWal->e
1acb0 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 30 xclusiveMode = 0
1acb1 3b 0a 20 20 20 20 20 20 69 66 28 20 77 61 6c 4c ;. if( walL
1acb2 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c 2c 20 ockShared(pWal,
1acb3 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 70 57 WAL_READ_LOCK(pW
1acb4 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 29 29 21 3d al->readLock))!=
1acb5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1acb6 20 20 20 20 20 70 57 61 6c 2d 3e 65 78 63 6c 75 pWal->exclu
1acb7 73 69 76 65 4d 6f 64 65 20 3d 20 31 3b 0a 20 20 siveMode = 1;.
1acb8 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d }. rc =
1acb9 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69 76 65 pWal->exclusive
1acba 4d 6f 64 65 3d 3d 30 3b 0a 20 20 20 20 7d 65 6c Mode==0;. }el
1acbb 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6c 72 se{. /* Alr
1acbc 65 61 64 79 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f eady in locking_
1acbd 6d 6f 64 65 3d 4e 4f 52 4d 41 4c 20 2a 2f 0a 20 mode=NORMAL */.
1acbe 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
1acbf 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f }. }else if( o
1acc0 70 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 p>0 ){. asser
1acc1 74 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69 t( pWal->exclusi
1acc2 76 65 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 20 veMode==0 );.
1acc3 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 72 assert( pWal->r
1acc4 65 61 64 4c 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20 eadLock>=0 );.
1acc5 20 20 77 61 6c 55 6e 6c 6f 63 6b 53 68 61 72 65 walUnlockShare
1acc6 64 28 70 57 61 6c 2c 20 57 41 4c 5f 52 45 41 44 d(pWal, WAL_READ
1acc7 5f 4c 4f 43 4b 28 70 57 61 6c 2d 3e 72 65 61 64 _LOCK(pWal->read
1acc8 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 70 57 61 6c Lock));. pWal
1acc9 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 ->exclusiveMode
1acca 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b = 1;. rc = 1;
1accb 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 . }else{. rc
1accc 20 3d 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69 = pWal->exclusi
1accd 76 65 4d 6f 64 65 3d 3d 30 3b 0a 20 20 7d 0a 20 veMode==0;. }.
1acce 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1accf 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 * .** Return tru
1acd0 65 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e e if the argumen
1acd1 74 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 20 61 6e t is non-NULL an
1acd2 64 20 74 68 65 20 57 41 4c 20 6d 6f 64 75 6c 65 d the WAL module
1acd3 20 69 73 20 75 73 69 6e 67 0a 2a 2a 20 68 65 61 is using.** hea
1acd4 70 2d 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 p-memory for the
1acd5 20 77 61 6c 2d 69 6e 64 65 78 2e 20 4f 74 68 65 wal-index. Othe
1acd6 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 61 72 rwise, if the ar
1acd7 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c 4c 20 6f gument is NULL o
1acd8 72 20 74 68 65 0a 2a 2a 20 57 41 4c 20 6d 6f 64 r the.** WAL mod
1acd9 75 6c 65 20 69 73 20 75 73 69 6e 67 20 73 68 61 ule is using sha
1acda 72 65 64 2d 6d 65 6d 6f 72 79 2c 20 72 65 74 75 red-memory, retu
1acdb 72 6e 20 66 61 6c 73 65 2e 20 0a 2a 2f 0a 53 51 rn false. .*/.SQ
1acdc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1acdd 20 73 71 6c 69 74 65 33 57 61 6c 48 65 61 70 4d sqlite3WalHeapM
1acde 65 6d 6f 72 79 28 57 61 6c 20 2a 70 57 61 6c 29 emory(Wal *pWal)
1acdf 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 57 61 6c {. return (pWal
1ace0 20 26 26 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 && pWal->exclus
1ace1 69 76 65 4d 6f 64 65 3d 3d 57 41 4c 5f 48 45 41 iveMode==WAL_HEA
1ace2 50 4d 45 4d 4f 52 59 5f 4d 4f 44 45 20 29 3b 0a PMEMORY_MODE );.
1ace3 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
1ace4 5f 45 4e 41 42 4c 45 5f 5a 49 50 56 46 53 0a 2f _ENABLE_ZIPVFS./
1ace5 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 *.** If the argu
1ace6 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c ment is not NULL
1ace7 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 , it points to a
1ace8 20 57 61 6c 20 6f 62 6a 65 63 74 20 74 68 61 74 Wal object that
1ace9 20 68 6f 6c 64 73 20 61 0a 2a 2a 20 72 65 61 64 holds a.** read
1acea 2d 6c 6f 63 6b 2e 20 54 68 69 73 20 66 75 6e 63 -lock. This func
1aceb 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
1acec 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2d 73 database page-s
1aced 69 7a 65 20 69 66 20 69 74 20 69 73 20 6b 6e 6f ize if it is kno
1acee 77 6e 2c 0a 2a 2a 20 6f 72 20 7a 65 72 6f 20 69 wn,.** or zero i
1acef 66 20 69 74 20 69 73 20 6e 6f 74 20 28 6f 72 20 f it is not (or
1acf0 69 66 20 70 57 61 6c 20 69 73 20 4e 55 4c 4c 29 if pWal is NULL)
1acf1 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1acf2 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
1acf3 61 6c 46 72 61 6d 65 73 69 7a 65 28 57 61 6c 20 alFramesize(Wal
1acf4 2a 70 57 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 *pWal){. assert
1acf5 28 20 70 57 61 6c 3d 3d 30 20 7c 7c 20 70 57 61 ( pWal==0 || pWa
1acf6 6c 2d 3e 72 65 61 64 4c 6f 63 6b 3e 3d 30 20 29 l->readLock>=0 )
1acf7 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 57 61 6c ;. return (pWal
1acf8 20 3f 20 70 57 61 6c 2d 3e 73 7a 50 61 67 65 20 ? pWal->szPage
1acf9 3a 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a : 0);.}.#endif..
1acfa 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 #endif /* #ifnde
1acfb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 f SQLITE_OMIT_WA
1acfc 4c 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a L */../*********
1acfd 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 77 61 6c ***** End of wal
1acfe 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1acff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad01 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1ad02 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1ad03 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a btmutex.c *****
1ad04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad06 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
1ad07 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a August 27.**.**
1ad08 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1ad09 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1ad0a 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1ad0b 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1ad0c 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1ad0d 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1ad0e 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
1ad0f 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
1ad10 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1ad11 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1ad12 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1ad13 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1ad14 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1ad15 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1ad16 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1ad17 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1ad18 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1ad19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
1ad1e 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
1ad1f 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 ns code used to
1ad20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 implement mutexe
1ad21 73 20 6f 6e 20 42 74 72 65 65 20 6f 62 6a 65 63 s on Btree objec
1ad22 74 73 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 ts..** This code
1ad23 20 72 65 61 6c 6c 79 20 62 65 6c 6f 6e 67 73 20 really belongs
1ad24 69 6e 20 62 74 72 65 65 2e 63 2e 20 20 42 75 74 in btree.c. But
1ad25 20 62 74 72 65 65 2e 63 20 69 73 20 67 65 74 74 btree.c is gett
1ad26 69 6e 67 20 74 6f 6f 0a 2a 2a 20 62 69 67 20 61 ing too.** big a
1ad27 6e 64 20 77 65 20 77 61 6e 74 20 74 6f 20 62 72 nd we want to br
1ad28 65 61 6b 20 69 74 20 64 6f 77 6e 20 73 6f 6d 65 eak it down some
1ad29 2e 20 20 54 68 69 73 20 70 61 63 6b 61 67 65 64 . This packaged
1ad2a 20 73 65 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20 seemed like.**
1ad2b 61 20 67 6f 6f 64 20 62 72 65 61 6b 6f 75 74 2e a good breakout.
1ad2c 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .*/./***********
1ad2d 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65 *** Include btre
1ad2e 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 eInt.h in the mi
1ad2f 64 64 6c 65 20 6f 66 20 62 74 6d 75 74 65 78 2e ddle of btmutex.
1ad30 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1ad31 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1ad32 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 *** Begin file b
1ad33 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a treeInt.h ******
1ad34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad36 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 **/./*.** 2004 A
1ad37 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 pril 6.**.** The
1ad38 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
1ad39 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
1ad3a 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
1ad3b 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
1ad3c 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
1ad3d 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
1ad3e 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
1ad3f 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
1ad40 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
1ad41 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
1ad42 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
1ad43 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
1ad44 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
1ad45 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
1ad46 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
1ad47 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
1ad48 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
1ad49 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ad4d 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
1ad4e 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 le implements a
1ad4f 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 external (disk-b
1ad50 61 73 65 64 29 20 64 61 74 61 62 61 73 65 20 75 ased) database u
1ad51 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 sing BTrees..**
1ad52 46 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64 For a detailed d
1ad53 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 42 54 72 iscussion of BTr
1ad54 65 65 73 2c 20 72 65 66 65 72 20 74 6f 0a 2a 2a ees, refer to.**
1ad55 0a 2a 2a 20 20 20 20 20 44 6f 6e 61 6c 64 20 45 .** Donald E
1ad56 2e 20 4b 6e 75 74 68 2c 20 54 48 45 20 41 52 54 . Knuth, THE ART
1ad57 20 4f 46 20 43 4f 4d 50 55 54 45 52 20 50 52 4f OF COMPUTER PRO
1ad58 47 52 41 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65 GRAMMING, Volume
1ad59 20 33 3a 0a 2a 2a 20 20 20 20 20 22 53 6f 72 74 3:.** "Sort
1ad5a 69 6e 67 20 41 6e 64 20 53 65 61 72 63 68 69 6e ing And Searchin
1ad5b 67 22 2c 20 70 61 67 65 73 20 34 37 33 2d 34 38 g", pages 473-48
1ad5c 30 2e 20 41 64 64 69 73 6f 6e 2d 57 65 73 6c 65 0. Addison-Wesle
1ad5d 79 0a 2a 2a 20 20 20 20 20 50 75 62 6c 69 73 68 y.** Publish
1ad5e 69 6e 67 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61 ing Company, Rea
1ad5f 64 69 6e 67 2c 20 4d 61 73 73 61 63 68 75 73 65 ding, Massachuse
1ad60 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 tts..**.** The b
1ad61 61 73 69 63 20 69 64 65 61 20 69 73 20 74 68 61 asic idea is tha
1ad62 74 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 t each page of t
1ad63 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 he file contains
1ad64 20 4e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 65 N database.** e
1ad65 6e 74 72 69 65 73 20 61 6e 64 20 4e 2b 31 20 70 ntries and N+1 p
1ad66 6f 69 6e 74 65 72 73 20 74 6f 20 73 75 62 70 61 ointers to subpa
1ad67 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d ges..**.** ---
1ad68 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1ad69 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1ad6a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1ad6b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a -------------.**
1ad6c 20 20 20 7c 20 20 50 74 72 28 30 29 20 7c 20 4b | Ptr(0) | K
1ad6d 65 79 28 30 29 20 7c 20 50 74 72 28 31 29 20 7c ey(0) | Ptr(1) |
1ad6e 20 4b 65 79 28 31 29 20 7c 20 2e 2e 2e 20 7c 20 Key(1) | ... |
1ad6f 4b 65 79 28 4e 2d 31 29 20 7c 20 50 74 72 28 4e Key(N-1) | Ptr(N
1ad70 29 20 7c 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d ) |.** -------
1ad71 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1ad72 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1ad73 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1ad74 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 ---------.**.**
1ad75 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20 All of the keys
1ad76 6f 6e 20 74 68 65 20 70 61 67 65 20 74 68 61 74 on the page that
1ad77 20 50 74 72 28 30 29 20 70 6f 69 6e 74 73 20 74 Ptr(0) points t
1ad78 6f 20 68 61 76 65 20 76 61 6c 75 65 73 20 6c 65 o have values le
1ad79 73 73 0a 2a 2a 20 74 68 61 6e 20 4b 65 79 28 30 ss.** than Key(0
1ad7a 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b ). All of the k
1ad7b 65 79 73 20 6f 6e 20 70 61 67 65 20 50 74 72 28 eys on page Ptr(
1ad7c 31 29 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 1) and its subpa
1ad7d 67 65 73 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75 ges have.** valu
1ad7e 65 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 es greater than
1ad7f 4b 65 79 28 30 29 20 61 6e 64 20 6c 65 73 73 20 Key(0) and less
1ad80 74 68 61 6e 20 4b 65 79 28 31 29 2e 20 20 41 6c than Key(1). Al
1ad81 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 0a 2a 2a l of the keys.**
1ad82 20 6f 6e 20 50 74 72 28 4e 29 20 61 6e 64 20 69 on Ptr(N) and i
1ad83 74 73 20 73 75 62 70 61 67 65 73 20 68 61 76 65 ts subpages have
1ad84 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 values greater
1ad85 74 68 61 6e 20 4b 65 79 28 4e 2d 31 29 2e 20 20 than Key(N-1).
1ad86 41 6e 64 0a 2a 2a 20 73 6f 20 66 6f 72 74 68 2e And.** so forth.
1ad87 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 69 6e 67 20 61 .**.** Finding a
1ad88 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79 20 particular key
1ad89 72 65 71 75 69 72 65 73 20 72 65 61 64 69 6e 67 requires reading
1ad8a 20 4f 28 6c 6f 67 28 4d 29 29 20 70 61 67 65 73 O(log(M)) pages
1ad8b 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 64 69 from the .** di
1ad8c 73 6b 20 77 68 65 72 65 20 4d 20 69 73 20 74 68 sk where M is th
1ad8d 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 e number of entr
1ad8e 69 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 2e ies in the tree.
1ad8f 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 .**.** In this i
1ad90 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61 mplementation, a
1ad91 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e single file can
1ad92 20 68 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 hold one or mor
1ad93 65 20 73 65 70 61 72 61 74 65 20 0a 2a 2a 20 42 e separate .** B
1ad94 54 72 65 65 73 2e 20 20 45 61 63 68 20 42 54 72 Trees. Each BTr
1ad95 65 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 ee is identified
1ad96 20 62 79 20 74 68 65 20 69 6e 64 65 78 20 6f 66 by the index of
1ad97 20 69 74 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 its root page.
1ad98 20 54 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 The.** key and
1ad99 64 61 74 61 20 66 6f 72 20 61 6e 79 20 65 6e 74 data for any ent
1ad9a 72 79 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 ry are combined
1ad9b 74 6f 20 66 6f 72 6d 20 74 68 65 20 22 70 61 79 to form the "pay
1ad9c 6c 6f 61 64 22 2e 20 20 41 0a 2a 2a 20 66 69 78 load". A.** fix
1ad9d 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 ed amount of pay
1ad9e 6c 6f 61 64 20 63 61 6e 20 62 65 20 63 61 72 72 load can be carr
1ad9f 69 65 64 20 64 69 72 65 63 74 6c 79 20 6f 6e 20 ied directly on
1ada0 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
1ada1 70 61 67 65 2e 20 20 49 66 20 74 68 65 20 70 61 page. If the pa
1ada2 79 6c 6f 61 64 20 69 73 20 6c 61 72 67 65 72 20 yload is larger
1ada3 74 68 61 6e 20 74 68 65 20 70 72 65 73 65 74 20 than the preset
1ada4 61 6d 6f 75 6e 74 20 74 68 65 6e 20 73 75 72 70 amount then surp
1ada5 6c 75 73 0a 2a 2a 20 62 79 74 65 73 20 61 72 65 lus.** bytes are
1ada6 20 73 74 6f 72 65 64 20 6f 6e 20 6f 76 65 72 66 stored on overf
1ada7 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 low pages. The
1ada8 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 6e 20 65 payload for an e
1ada9 6e 74 72 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 ntry.** and the
1adaa 70 72 65 63 65 64 69 6e 67 20 70 6f 69 6e 74 65 preceding pointe
1adab 72 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 r are combined t
1adac 6f 20 66 6f 72 6d 20 61 20 22 43 65 6c 6c 22 2e o form a "Cell".
1adad 20 20 45 61 63 68 20 0a 2a 2a 20 70 61 67 65 20 Each .** page
1adae 68 61 73 20 61 20 73 6d 61 6c 6c 20 68 65 61 64 has a small head
1adaf 65 72 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e er which contain
1adb0 73 20 74 68 65 20 50 74 72 28 4e 29 20 70 6f 69 s the Ptr(N) poi
1adb1 6e 74 65 72 20 61 6e 64 20 6f 74 68 65 72 0a 2a nter and other.*
1adb2 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75 * information su
1adb3 63 68 20 61 73 20 74 68 65 20 73 69 7a 65 20 6f ch as the size o
1adb4 66 20 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a f key and data..
1adb5 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 54 20 44 45 54 **.** FORMAT DET
1adb6 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 AILS.**.** The f
1adb7 69 6c 65 20 69 73 20 64 69 76 69 64 65 64 20 69 ile is divided i
1adb8 6e 74 6f 20 70 61 67 65 73 2e 20 20 54 68 65 20 nto pages. The
1adb9 66 69 72 73 74 20 70 61 67 65 20 69 73 20 63 61 first page is ca
1adba 6c 6c 65 64 20 70 61 67 65 20 31 2c 0a 2a 2a 20 lled page 1,.**
1adbb 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 70 61 the second is pa
1adbc 67 65 20 32 2c 20 61 6e 64 20 73 6f 20 66 6f 72 ge 2, and so for
1adbd 74 68 2e 20 20 41 20 70 61 67 65 20 6e 75 6d 62 th. A page numb
1adbe 65 72 20 6f 66 20 7a 65 72 6f 20 69 6e 64 69 63 er of zero indic
1adbf 61 74 65 73 0a 2a 2a 20 22 6e 6f 20 73 75 63 68 ates.** "no such
1adc0 20 70 61 67 65 22 2e 20 20 54 68 65 20 70 61 67 page". The pag
1adc1 65 20 73 69 7a 65 20 63 61 6e 20 62 65 20 61 6e e size can be an
1adc2 79 20 70 6f 77 65 72 20 6f 66 20 32 20 62 65 74 y power of 2 bet
1adc3 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 ween 512 and 655
1adc4 33 36 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 36..** Each page
1adc5 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 can be either a
1adc6 20 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 btree page, a f
1adc7 72 65 65 6c 69 73 74 20 70 61 67 65 2c 20 61 6e reelist page, an
1adc8 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 overflow.** pag
1adc9 65 2c 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 2d e, or a pointer-
1adca 6d 61 70 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 map page..**.**
1adcb 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69 The first page i
1adcc 73 20 61 6c 77 61 79 73 20 61 20 62 74 72 65 65 s always a btree
1adcd 20 70 61 67 65 2e 20 20 54 68 65 20 66 69 72 73 page. The firs
1adce 74 20 31 30 30 20 62 79 74 65 73 20 6f 66 20 74 t 100 bytes of t
1adcf 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 67 65 he first.** page
1add0 20 63 6f 6e 74 61 69 6e 20 61 20 73 70 65 63 69 contain a speci
1add1 61 6c 20 68 65 61 64 65 72 20 28 74 68 65 20 22 al header (the "
1add2 66 69 6c 65 20 68 65 61 64 65 72 22 29 20 74 68 file header") th
1add3 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 at describes the
1add4 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 66 6f file..** The fo
1add5 72 6d 61 74 20 6f 66 20 74 68 65 20 66 69 6c 65 rmat of the file
1add6 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 6f header is as fo
1add7 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f llows:.**.** O
1add8 46 46 53 45 54 20 20 20 53 49 5a 45 20 20 20 20 FFSET SIZE
1add9 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 DESCRIPTION.**
1adda 20 20 20 20 30 20 20 20 20 20 20 31 36 20 20 20 0 16
1addb 20 20 48 65 61 64 65 72 20 73 74 72 69 6e 67 3a Header string:
1addc 20 22 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 "SQLite format
1addd 33 5c 30 30 30 22 0a 2a 2a 20 20 20 20 20 31 36 3\000".** 16
1adde 20 20 20 20 20 20 20 32 20 20 20 20 20 50 61 67 2 Pag
1addf 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2e e size in bytes.
1ade0 20 20 0a 2a 2a 20 20 20 20 20 31 38 20 20 20 20 .** 18
1ade1 20 20 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 1 File fo
1ade2 72 6d 61 74 20 77 72 69 74 65 20 76 65 72 73 69 rmat write versi
1ade3 6f 6e 0a 2a 2a 20 20 20 20 20 31 39 20 20 20 20 on.** 19
1ade4 20 20 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 1 File fo
1ade5 72 6d 61 74 20 72 65 61 64 20 76 65 72 73 69 6f rmat read versio
1ade6 6e 0a 2a 2a 20 20 20 20 20 32 30 20 20 20 20 20 n.** 20
1ade7 20 20 31 20 20 20 20 20 42 79 74 65 73 20 6f 66 1 Bytes of
1ade8 20 75 6e 75 73 65 64 20 73 70 61 63 65 20 61 74 unused space at
1ade9 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 the end of each
1adea 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 32 31 20 page.** 21
1adeb 20 20 20 20 20 20 31 20 20 20 20 20 4d 61 78 20 1 Max
1adec 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 embedded payload
1aded 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 fraction.**
1adee 20 32 32 20 20 20 20 20 20 20 31 20 20 20 20 20 22 1
1adef 4d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79 Min embedded pay
1adf0 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a load fraction.**
1adf1 20 20 20 20 20 32 33 20 20 20 20 20 20 20 31 20 23 1
1adf2 20 20 20 20 4d 69 6e 20 6c 65 61 66 20 70 61 79 Min leaf pay
1adf3 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a load fraction.**
1adf4 20 20 20 20 20 32 34 20 20 20 20 20 20 20 34 20 24 4
1adf5 20 20 20 20 46 69 6c 65 20 63 68 61 6e 67 65 20 File change
1adf6 63 6f 75 6e 74 65 72 0a 2a 2a 20 20 20 20 20 32 counter.** 2
1adf7 38 20 20 20 20 20 20 20 34 20 20 20 20 20 52 65 8 4 Re
1adf8 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 served for futur
1adf9 65 20 75 73 65 0a 2a 2a 20 20 20 20 20 33 32 20 e use.** 32
1adfa 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73 4 Firs
1adfb 74 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 0a t freelist page.
1adfc 2a 2a 20 20 20 20 20 33 36 20 20 20 20 20 20 20 ** 36
1adfd 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 4 Number of
1adfe 66 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 69 freelist pages i
1adff 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 20 20 n the file.**
1ae00 20 20 34 30 20 20 20 20 20 20 36 30 20 20 20 20 40 60
1ae01 20 31 35 20 34 2d 62 79 74 65 20 6d 65 74 61 20 15 4-byte meta
1ae02 76 61 6c 75 65 73 20 70 61 73 73 65 64 20 74 6f values passed to
1ae03 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 0a 2a higher layers.*
1ae04 2a 0a 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20 *.** 40
1ae05 20 20 34 20 20 20 20 20 53 63 68 65 6d 61 20 63 4 Schema c
1ae06 6f 6f 6b 69 65 0a 2a 2a 20 20 20 20 20 34 34 20 ookie.** 44
1ae07 20 20 20 20 20 20 34 20 20 20 20 20 46 69 6c 65 4 File
1ae08 20 66 6f 72 6d 61 74 20 6f 66 20 73 63 68 65 6d format of schem
1ae09 61 20 6c 61 79 65 72 0a 2a 2a 20 20 20 20 20 34 a layer.** 4
1ae0a 38 20 20 20 20 20 20 20 34 20 20 20 20 20 53 69 8 4 Si
1ae0b 7a 65 20 6f 66 20 70 61 67 65 20 63 61 63 68 65 ze of page cache
1ae0c 0a 2a 2a 20 20 20 20 20 35 32 20 20 20 20 20 20 .** 52
1ae0d 20 34 20 20 20 20 20 4c 61 72 67 65 73 74 20 72 4 Largest r
1ae0e 6f 6f 74 2d 70 61 67 65 20 28 61 75 74 6f 2f 69 oot-page (auto/i
1ae0f 6e 63 72 5f 76 61 63 75 75 6d 29 0a 2a 2a 20 20 ncr_vacuum).**
1ae10 20 20 20 35 36 20 20 20 20 20 20 20 34 20 20 20 56 4
1ae11 20 20 31 3d 55 54 46 2d 38 20 32 3d 55 54 46 31 1=UTF-8 2=UTF1
1ae12 36 6c 65 20 33 3d 55 54 46 31 36 62 65 0a 2a 2a 6le 3=UTF16be.**
1ae13 20 20 20 20 20 36 30 20 20 20 20 20 20 20 34 20 60 4
1ae14 20 20 20 20 55 73 65 72 20 76 65 72 73 69 6f 6e User version
1ae15 0a 2a 2a 20 20 20 20 20 36 34 20 20 20 20 20 20 .** 64
1ae16 20 34 20 20 20 20 20 49 6e 63 72 65 6d 65 6e 74 4 Increment
1ae17 61 6c 20 76 61 63 75 75 6d 20 6d 6f 64 65 0a 2a al vacuum mode.*
1ae18 2a 20 20 20 20 20 36 38 20 20 20 20 20 20 20 34 * 68 4
1ae19 20 20 20 20 20 75 6e 75 73 65 64 0a 2a 2a 20 20 unused.**
1ae1a 20 20 20 37 32 20 20 20 20 20 20 20 34 20 20 20 72 4
1ae1b 20 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 unused.**
1ae1c 37 36 20 20 20 20 20 20 20 34 20 20 20 20 20 75 76 4 u
1ae1d 6e 75 73 65 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 nused.**.** All
1ae1e 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 of the integer v
1ae1f 61 6c 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e alues are big-en
1ae20 64 69 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 dian (most signi
1ae21 66 69 63 61 6e 74 20 62 79 74 65 20 66 69 72 73 ficant byte firs
1ae22 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 t)..**.** The fi
1ae23 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 le change counte
1ae24 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 r is incremented
1ae25 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 when the databa
1ae26 73 65 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a se is changed.**
1ae27 20 54 68 69 73 20 63 6f 75 6e 74 65 72 20 61 6c This counter al
1ae28 6c 6f 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 lows other proce
1ae29 73 73 65 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 sses to know whe
1ae2a 6e 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 n the file has c
1ae2b 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 hanged.** and th
1ae2c 75 73 20 77 68 65 6e 20 74 68 65 79 20 6e 65 65 us when they nee
1ae2d 64 20 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 d to flush their
1ae2e 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 cache..**.** Th
1ae2f 65 20 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70 e max embedded p
1ae30 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 ayload fraction
1ae31 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 is the amount of
1ae32 20 74 68 65 20 74 6f 74 61 6c 20 75 73 61 62 6c the total usabl
1ae33 65 0a 2a 2a 20 73 70 61 63 65 20 69 6e 20 61 20 e.** space in a
1ae34 70 61 67 65 20 74 68 61 74 20 63 61 6e 20 62 65 page that can be
1ae35 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 61 20 73 consumed by a s
1ae36 69 6e 67 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 ingle cell for s
1ae37 74 61 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 tandard.** B-tre
1ae38 65 20 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 e (non-LEAFDATA)
1ae39 20 74 61 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 tables. A valu
1ae3a 65 20 6f 66 20 32 35 35 20 6d 65 61 6e 73 20 31 e of 255 means 1
1ae3b 30 30 25 2e 20 20 54 68 65 20 64 65 66 61 75 6c 00%. The defaul
1ae3c 74 0a 2a 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 t.** is to limit
1ae3d 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c the maximum cel
1ae3e 6c 20 73 69 7a 65 20 73 6f 20 74 68 61 74 20 61 l size so that a
1ae3f 74 20 6c 65 61 73 74 20 34 20 63 65 6c 6c 73 20 t least 4 cells
1ae40 77 69 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f will fit.** on o
1ae41 6e 65 20 70 61 67 65 2e 20 20 54 68 75 73 20 74 ne page. Thus t
1ae42 68 65 20 64 65 66 61 75 6c 74 20 6d 61 78 20 65 he default max e
1ae43 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 mbedded payload
1ae44 66 72 61 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a fraction is 64..
1ae45 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 79 **.** If the pay
1ae46 6c 6f 61 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 load for a cell
1ae47 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 is larger than t
1ae48 68 65 20 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 he max payload,
1ae49 74 68 65 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 then extra.** pa
1ae4a 79 6c 6f 61 64 20 69 73 20 73 70 69 6c 6c 65 64 yload is spilled
1ae4b 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 to overflow pag
1ae4c 65 73 2e 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 es. Once an ove
1ae4d 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 61 6c rflow page is al
1ae4e 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d located,.** as m
1ae4f 61 6e 79 20 62 79 74 65 73 20 61 73 20 70 6f 73 any bytes as pos
1ae50 73 69 62 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 sible are moved
1ae51 69 6e 74 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f into the overflo
1ae52 77 20 70 61 67 65 73 20 77 69 74 68 6f 75 74 20 w pages without
1ae53 6c 65 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 letting.** the c
1ae54 65 6c 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65 ell size drop be
1ae55 6c 6f 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 low the min embe
1ae56 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 dded payload fra
1ae57 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ction..**.** The
1ae58 20 6d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 min leaf payloa
1ae59 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 d fraction is li
1ae5a 6b 65 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 ke the min embed
1ae5b 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 ded payload frac
1ae5c 74 69 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 tion.** except t
1ae5d 68 61 74 20 69 74 20 61 70 70 6c 69 65 73 20 74 hat it applies t
1ae5e 6f 20 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 o leaf nodes in
1ae5f 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 2e a LEAFDATA tree.
1ae60 20 20 54 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a The maximum.**
1ae61 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f payload fractio
1ae62 6e 20 66 6f 72 20 61 20 4c 45 41 46 44 41 54 41 n for a LEAFDATA
1ae63 20 74 72 65 65 20 69 73 20 61 6c 77 61 79 73 20 tree is always
1ae64 31 30 30 25 20 28 6f 72 20 32 35 35 29 20 61 6e 100% (or 255) an
1ae65 64 20 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 d it.** not spec
1ae66 69 66 69 65 64 20 69 6e 20 74 68 65 20 68 65 61 ified in the hea
1ae67 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 der..**.** Each
1ae68 62 74 72 65 65 20 70 61 67 65 73 20 69 73 20 64 btree pages is d
1ae69 69 76 69 64 65 64 20 69 6e 74 6f 20 74 68 72 65 ivided into thre
1ae6a 65 20 73 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 e sections: The
1ae6b 20 68 65 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 header, the.**
1ae6c 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 cell pointer arr
1ae6d 61 79 2c 20 61 6e 64 20 74 68 65 20 63 65 6c 6c ay, and the cell
1ae6e 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 content area.
1ae6f 50 61 67 65 20 31 20 61 6c 73 6f 20 68 61 73 20 Page 1 also has
1ae70 61 20 31 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 a 100-byte.** fi
1ae71 6c 65 20 68 65 61 64 65 72 20 74 68 61 74 20 6f le header that o
1ae72 63 63 75 72 73 20 62 65 66 6f 72 65 20 74 68 65 ccurs before the
1ae73 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a page header..**
1ae74 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d .** |------
1ae75 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 ----------|.**
1ae76 20 20 20 20 7c 20 66 69 6c 65 20 68 65 61 64 65 | file heade
1ae77 72 20 20 20 20 7c 20 20 20 31 30 30 20 62 79 74 r | 100 byt
1ae78 65 73 2e 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 es. Page 1 only
1ae79 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d ..** |-----
1ae7a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 -----------|.**
1ae7b 20 20 20 20 20 7c 20 70 61 67 65 20 68 65 61 64 | page head
1ae7c 65 72 20 20 20 20 7c 20 20 20 38 20 62 79 74 65 er | 8 byte
1ae7d 73 20 66 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 s for leaves. 1
1ae7e 32 20 62 79 74 65 73 20 66 6f 72 20 69 6e 74 65 2 bytes for inte
1ae7f 72 69 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 rior nodes.**
1ae80 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |------------
1ae81 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 ----|.** |
1ae82 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c cell pointer |
1ae83 20 20 20 7c 20 20 32 20 62 79 74 65 73 20 70 65 | 2 bytes pe
1ae84 72 20 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 r cell. Sorted
1ae85 6f 72 64 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c order..** |
1ae86 20 61 72 72 61 79 20 20 20 20 20 20 20 20 20 20 array
1ae87 7c 20 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 | | Grows dow
1ae88 6e 77 61 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 nward.** |
1ae89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
1ae8a 20 20 20 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d v.** |--
1ae8b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a --------------|.
1ae8c 2a 2a 20 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f ** | unallo
1ae8d 63 61 74 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 cated |.**
1ae8e 20 20 20 7c 20 73 70 61 63 65 20 20 20 20 20 20 | space
1ae8f 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d |.** |-
1ae90 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c ---------------|
1ae91 20 20 20 5e 20 20 47 72 6f 77 73 20 75 70 77 61 ^ Grows upwa
1ae92 72 64 73 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 rds.** | ce
1ae93 6c 6c 20 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 ll content |
1ae94 20 7c 20 20 41 72 62 69 74 72 61 72 79 20 6f 72 | Arbitrary or
1ae95 64 65 72 20 69 6e 74 65 72 73 70 65 72 73 65 64 der interspersed
1ae96 20 77 69 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 with freeblocks
1ae97 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 65 61 ..** | area
1ae98 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c | |
1ae99 20 20 61 6e 64 20 66 72 65 65 20 73 70 61 63 65 and free space
1ae9a 20 66 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 fragments..**
1ae9b 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |-----------
1ae9c 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 -----|.**.** The
1ae9d 20 70 61 67 65 20 68 65 61 64 65 72 73 20 6c 6f page headers lo
1ae9e 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a oks like this:.*
1ae9f 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 *.** OFFSET
1aea0 53 49 5a 45 20 20 20 20 20 44 45 53 43 52 49 50 SIZE DESCRIP
1aea1 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 TION.** 0
1aea2 20 20 20 20 20 31 20 20 20 20 20 20 46 6c 61 67 1 Flag
1aea3 73 2e 20 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a s. 1: intkey, 2:
1aea4 20 7a 65 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 zerodata, 4: le
1aea5 61 66 64 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a afdata, 8: leaf.
1aea6 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20 ** 1
1aea7 32 20 20 20 20 20 20 62 79 74 65 20 6f 66 66 73 2 byte offs
1aea8 65 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 et to the first
1aea9 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 freeblock.**
1aeaa 20 20 33 20 20 20 20 20 20 20 32 20 20 20 20 20 3 2
1aeab 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 number of cells
1aeac 20 6f 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a on this page.**
1aead 20 20 20 20 20 20 35 20 20 20 20 20 20 20 32 20 5 2
1aeae 20 20 20 20 20 66 69 72 73 74 20 62 79 74 65 20 first byte
1aeaf 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 of the cell cont
1aeb0 65 6e 74 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 ent area.**
1aeb1 20 37 20 20 20 20 20 20 20 31 20 20 20 20 20 20 7 1
1aeb2 6e 75 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65 number of fragme
1aeb3 6e 74 65 64 20 66 72 65 65 20 62 79 74 65 73 0a nted free bytes.
1aeb4 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20 20 20 ** 8
1aeb5 34 20 20 20 20 20 20 52 69 67 68 74 20 63 68 69 4 Right chi
1aeb6 6c 64 20 28 74 68 65 20 50 74 72 28 4e 29 20 76 ld (the Ptr(N) v
1aeb7 61 6c 75 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 alue). Omitted
1aeb8 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a on leaves..**.**
1aeb9 20 54 68 65 20 66 6c 61 67 73 20 64 65 66 69 6e The flags defin
1aeba 65 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 e the format of
1aebb 74 68 69 73 20 62 74 72 65 65 20 70 61 67 65 2e this btree page.
1aebc 20 20 54 68 65 20 6c 65 61 66 20 66 6c 61 67 20 The leaf flag
1aebd 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 means that.** th
1aebe 69 73 20 70 61 67 65 20 68 61 73 20 6e 6f 20 63 is page has no c
1aebf 68 69 6c 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 hildren. The ze
1aec0 72 6f 64 61 74 61 20 66 6c 61 67 20 6d 65 61 6e rodata flag mean
1aec1 73 20 74 68 61 74 20 74 68 69 73 20 70 61 67 65 s that this page
1aec2 20 63 61 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 carries.** only
1aec3 20 6b 65 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 keys and no dat
1aec4 61 2e 20 20 54 68 65 20 69 6e 74 6b 65 79 20 66 a. The intkey f
1aec5 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74 lag means that t
1aec6 68 65 20 6b 65 79 20 69 73 20 61 20 69 6e 74 65 he key is a inte
1aec7 67 65 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 ger.** which is
1aec8 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65 stored in the ke
1aec9 79 20 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 y size entry of
1aeca 74 68 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20 the cell header
1aecb 72 61 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a rather than in.*
1aecc 2a 20 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 * the payload ar
1aecd 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 ea..**.** The ce
1aece 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 ll pointer array
1aecf 20 62 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 begins on the f
1aed0 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20 irst byte after
1aed1 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2e the page header.
1aed2 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 .** The cell poi
1aed3 6e 74 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61 nter array conta
1aed4 69 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 ins zero or more
1aed5 20 32 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20 2-byte numbers
1aed6 77 68 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 which are.** off
1aed7 73 65 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65 sets from the be
1aed8 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 ginning of the p
1aed9 61 67 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 age to the cell
1aeda 63 6f 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63 content in the c
1aedb 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 ell.** content a
1aedc 72 65 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 rea. The cell p
1aedd 6f 69 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e ointers occur in
1aede 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 sorted order.
1aedf 54 68 65 20 73 79 73 74 65 6d 20 73 74 72 69 76 The system striv
1aee0 65 73 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 es.** to keep fr
1aee1 65 65 20 73 70 61 63 65 20 61 66 74 65 72 20 74 ee space after t
1aee2 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 he last cell poi
1aee3 6e 74 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77 nter so that new
1aee4 20 63 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 cells can.** be
1aee5 20 65 61 73 69 6c 79 20 61 64 64 65 64 20 77 69 easily added wi
1aee6 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 thout having to
1aee7 64 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 defragment the p
1aee8 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 age..**.** Cell
1aee9 63 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65 content is store
1aeea 64 20 61 74 20 74 68 65 20 76 65 72 79 20 65 6e d at the very en
1aeeb 64 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e d of the page an
1aeec 64 20 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74 d grows toward t
1aeed 68 65 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 he.** beginning
1aeee 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a of the page..**.
1aeef 2a 2a 20 55 6e 75 73 65 64 20 73 70 61 63 65 20 ** Unused space
1aef0 77 69 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 within the cell
1aef1 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 content area is
1aef2 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 collected into a
1aef3 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a linked list of.
1aef4 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 ** freeblocks.
1aef5 45 61 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 Each freeblock i
1aef6 73 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 s at least 4 byt
1aef7 65 73 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 es in size. The
1aef8 20 62 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 byte offset.**
1aef9 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 65 to the first fre
1aefa 65 62 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 eblock is given
1aefb 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 in the header.
1aefc 46 72 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 Freeblocks occur
1aefd 20 69 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e in.** increasin
1aefe 67 20 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73 g order. Becaus
1aeff 65 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 e a freeblock mu
1af00 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 st be at least 4
1af01 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a bytes in size,.
1af02 2a 2a 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 ** any group of
1af03 33 20 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65 3 or fewer unuse
1af04 64 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 d bytes in the c
1af05 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
1af06 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 cannot.** exist
1af07 20 6f 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63 on the freebloc
1af08 6b 20 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 k chain. A grou
1af09 70 20 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 p of 3 or fewer
1af0a 66 72 65 65 20 62 79 74 65 73 20 69 73 20 63 61 free bytes is ca
1af0b 6c 6c 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 lled.** a fragme
1af0c 6e 74 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e nt. The total n
1af0d 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
1af0e 6e 20 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 n all fragments
1af0f 69 73 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 is recorded..**
1af10 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 64 in the page head
1af11 65 72 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a er at offset 7..
1af12 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 **.** SIZE
1af13 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 DESCRIPTION.**
1af14 20 20 20 20 20 32 20 20 20 20 20 42 79 74 65 20 2 Byte
1af15 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65 offset of the ne
1af16 78 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 xt freeblock.**
1af17 20 20 20 20 20 32 20 20 20 20 20 42 79 74 65 73 2 Bytes
1af18 20 69 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f in this freeblo
1af19 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 ck.**.** Cells a
1af1a 72 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c re of variable l
1af1b 65 6e 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 ength. Cells ar
1af1c 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 e stored in the
1af1d 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
1af1e 61 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 a at.** the end
1af1f 6f 66 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f of the page. Po
1af20 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 63 65 inters to the ce
1af21 6c 6c 73 20 61 72 65 20 69 6e 20 74 68 65 20 63 lls are in the c
1af22 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 ell pointer arra
1af23 79 0a 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 y.** that immedi
1af24 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 ately follows th
1af25 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 e page header.
1af26 43 65 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 Cells is not nec
1af27 65 73 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 essarily.** cont
1af28 69 67 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 iguous or in ord
1af29 65 72 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 er, but cell poi
1af2a 6e 74 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67 nters are contig
1af2b 75 6f 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 uous and in orde
1af2c 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f r..**.** Cell co
1af2d 6e 74 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 ntent makes use
1af2e 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 of variable leng
1af2f 74 68 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20 th integers. A
1af30 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 variable.** leng
1af31 74 68 20 69 6e 74 65 67 65 72 20 69 73 20 31 20 th integer is 1
1af32 74 6f 20 39 20 62 79 74 65 73 20 77 68 65 72 65 to 9 bytes where
1af33 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 the lower 7 bit
1af34 73 20 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 s of each .** by
1af35 74 65 20 61 72 65 20 75 73 65 64 2e 20 20 54 68 te are used. Th
1af36 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 e integer consis
1af37 74 73 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 ts of all bytes
1af38 74 68 61 74 20 68 61 76 65 20 62 69 74 20 38 20 that have bit 8
1af39 73 65 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 set and.** the f
1af3a 69 72 73 74 20 62 79 74 65 20 77 69 74 68 20 62 irst byte with b
1af3b 69 74 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65 it 8 clear. The
1af3c 20 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e most significan
1af3d 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e t byte of the in
1af3e 74 65 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 teger.** appears
1af3f 20 66 69 72 73 74 2e 20 20 41 20 76 61 72 69 61 first. A varia
1af40 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 ble-length integ
1af41 65 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f er may not be mo
1af42 72 65 20 74 68 61 6e 20 39 20 62 79 74 65 73 20 re than 9 bytes
1af43 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 long..** As a sp
1af44 65 63 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 ecial case, all
1af45 38 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 39 8 bytes of the 9
1af46 74 68 20 62 79 74 65 20 61 72 65 20 75 73 65 64 th byte are used
1af47 20 61 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a as data. This.
1af48 2a 2a 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 ** allows a 64-b
1af49 69 74 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65 it integer to be
1af4a 20 65 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79 encoded in 9 by
1af4b 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 tes..**.** 0x
1af4c 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 00
1af4d 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 becomes
1af4e 20 30 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 0x00000000.**
1af4f 20 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20 0x7f
1af50 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f beco
1af51 6d 65 73 20 20 30 78 30 30 30 30 30 30 37 66 0a mes 0x0000007f.
1af52 2a 2a 20 20 20 20 30 78 38 31 20 30 78 30 30 20 ** 0x81 0x00
1af53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1af54 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 becomes 0x00000
1af55 30 38 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 080.** 0x82 0
1af56 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 x00
1af57 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 becomes 0x0
1af58 30 30 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 0000100.** 0x
1af59 38 30 20 30 78 37 66 20 20 20 20 20 20 20 20 20 80 0x7f
1af5a 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 becomes
1af5b 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 0x0000007f.**
1af5c 20 20 30 78 38 61 20 30 78 39 31 20 30 78 64 31 0x8a 0x91 0xd1
1af5d 20 30 78 61 63 20 30 78 37 38 20 20 62 65 63 6f 0xac 0x78 beco
1af5e 6d 65 73 20 20 30 78 31 32 33 34 35 36 37 38 0a mes 0x12345678.
1af5f 2a 2a 20 20 20 20 30 78 38 31 20 30 78 38 31 20 ** 0x81 0x81
1af60 30 78 38 31 20 30 78 38 31 20 30 78 30 31 20 20 0x81 0x81 0x01
1af61 62 65 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34 becomes 0x10204
1af62 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 081.**.** Variab
1af63 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 le length intege
1af64 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 rs are used for
1af65 72 6f 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f rowids and to ho
1af66 6c 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ld the number of
1af67 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 .** bytes of key
1af68 20 61 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62 and data in a b
1af69 74 72 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a tree cell..**.**
1af6a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 The content of
1af6b 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b a cell looks lik
1af6c 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 e this:.**.**
1af6d 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 SIZE DESCRIP
1af6e 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 TION.** 4
1af6f 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f Page number o
1af70 66 20 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64 f the left child
1af71 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61 . Omitted if lea
1af72 66 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a f flag is set..*
1af73 2a 20 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d * var Num
1af74 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
1af75 64 61 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 data. Omitted if
1af76 20 74 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c the zerodata fl
1af77 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 ag is set..**
1af78 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 var Number
1af79 6f 66 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e of bytes of key.
1af7a 20 4f 72 20 74 68 65 20 6b 65 79 20 69 74 73 65 Or the key itse
1af7b 6c 66 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 lf if intkey fla
1af7c 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 g is set..**
1af7d 20 20 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a * Payload.
1af7e 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 46 69 ** 4 Fi
1af7f 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 rst page of the
1af80 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 overflow chain.
1af81 20 4f 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f Omitted if no o
1af82 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 verflow.**.** Ov
1af83 65 72 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 erflow pages for
1af84 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e m a linked list.
1af85 20 20 45 61 63 68 20 70 61 67 65 20 65 78 63 65 Each page exce
1af86 70 74 20 74 68 65 20 6c 61 73 74 20 69 73 20 63 pt the last is c
1af87 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c ompletely.** fil
1af88 6c 65 64 20 77 69 74 68 20 64 61 74 61 20 28 70 led with data (p
1af89 61 67 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65 agesize - 4 byte
1af8a 73 29 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61 s). The last pa
1af8b 67 65 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c ge can have as l
1af8c 69 74 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 ittle.** as 1 by
1af8d 74 65 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a te of data..**.*
1af8e 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 * SIZE DES
1af8f 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 CRIPTION.**
1af90 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 4 Page numb
1af91 65 72 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 er of next overf
1af92 6c 6f 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 low page.**
1af93 20 2a 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a * Data.**.*
1af94 2a 20 46 72 65 65 6c 69 73 74 20 70 61 67 65 73 * Freelist pages
1af95 20 63 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 come in two sub
1af96 74 79 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 types: trunk pag
1af97 65 73 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65 es and leaf page
1af98 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 s. The.** file
1af99 68 65 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f header points to
1af9a 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 the first in a
1af9b 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 linked list of t
1af9c 72 75 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68 runk page. Each
1af9d 20 74 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 trunk.** page p
1af9e 6f 69 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c oints to multipl
1af9f 65 20 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 e leaf pages. T
1afa0 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 he content of a
1afa1 6c 65 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 leaf page is.**
1afa2 75 6e 73 70 65 63 69 66 69 65 64 2e 20 20 41 20 unspecified. A
1afa3 74 72 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 trunk page looks
1afa4 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a like this:.**.*
1afa5 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 * SIZE DES
1afa6 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 CRIPTION.**
1afa7 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 4 Page numb
1afa8 65 72 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b er of next trunk
1afa9 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 page.** 4
1afaa 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 Number of le
1afab 61 66 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 af pointers on t
1afac 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 his page.**
1afad 20 2a 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d * zero or m
1afae 6f 72 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72 ore pages number
1afaf 73 20 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a s of leaves.*/..
1afb0 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e ./* The followin
1afb1 67 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d g value is the m
1afb2 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 aximum cell size
1afb3 20 61 73 73 75 6d 69 6e 67 20 61 20 6d 61 78 69 assuming a maxi
1afb4 6d 75 6d 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 mum page.** size
1afb5 20 67 69 76 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a give above..*/.
1afb6 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f #define MX_CELL_
1afb7 53 49 5a 45 28 70 42 74 29 20 20 28 28 69 6e 74 SIZE(pBt) ((int
1afb8 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d )(pBt->pageSize-
1afb9 38 29 29 0a 0a 2f 2a 20 54 68 65 20 6d 61 78 69 8))../* The maxi
1afba 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 mum number of ce
1afbb 6c 6c 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 lls on a single
1afbc 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
1afbd 62 61 73 65 2e 20 20 54 68 69 73 0a 2a 2a 20 61 base. This.** a
1afbe 73 73 75 6d 65 73 20 61 20 6d 69 6e 69 6d 75 6d ssumes a minimum
1afbf 20 63 65 6c 6c 20 73 69 7a 65 20 6f 66 20 36 20 cell size of 6
1afc0 62 79 74 65 73 20 20 28 34 20 62 79 74 65 73 20 bytes (4 bytes
1afc1 66 6f 72 20 74 68 65 20 63 65 6c 6c 20 69 74 73 for the cell its
1afc2 65 6c 66 0a 2a 2a 20 70 6c 75 73 20 32 20 62 79 elf.** plus 2 by
1afc3 74 65 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65 tes for the inde
1afc4 78 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e x to the cell in
1afc5 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
1afc6 29 2e 20 20 53 75 63 68 0a 2a 2a 20 73 6d 61 6c ). Such.** smal
1afc7 6c 20 63 65 6c 6c 73 20 77 69 6c 6c 20 62 65 20 l cells will be
1afc8 72 61 72 65 2c 20 62 75 74 20 74 68 65 79 20 61 rare, but they a
1afc9 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a re possible..*/.
1afca 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 28 #define MX_CELL(
1afcb 70 42 74 29 20 28 28 70 42 74 2d 3e 70 61 67 65 pBt) ((pBt->page
1afcc 53 69 7a 65 2d 38 29 2f 36 29 0a 0a 2f 2a 20 46 Size-8)/6)../* F
1afcd 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 orward declarati
1afce 6f 6e 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 ons */.typedef s
1afcf 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 4d 65 truct MemPage Me
1afd0 6d 50 61 67 65 3b 0a 74 79 70 65 64 65 66 20 73 mPage;.typedef s
1afd1 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 42 74 4c truct BtLock BtL
1afd2 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ock;../*.** This
1afd3 20 69 73 20 61 20 6d 61 67 69 63 20 73 74 72 69 is a magic stri
1afd4 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 73 20 ng that appears
1afd5 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
1afd6 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c of every.** SQL
1afd7 69 74 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 ite database in
1afd8 6f 72 64 65 72 20 74 6f 20 69 64 65 6e 74 69 66 order to identif
1afd9 79 20 74 68 65 20 66 69 6c 65 20 61 73 20 61 20 y the file as a
1afda 72 65 61 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a real database..*
1afdb 2a 0a 2a 2a 20 59 6f 75 20 63 61 6e 20 63 68 61 *.** You can cha
1afdc 6e 67 65 20 74 68 69 73 20 76 61 6c 75 65 20 61 nge this value a
1afdd 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 62 t compile-time b
1afde 79 20 73 70 65 63 69 66 79 69 6e 67 20 61 0a 2a y specifying a.*
1afdf 2a 20 2d 44 53 51 4c 49 54 45 5f 46 49 4c 45 5f * -DSQLITE_FILE_
1afe0 48 45 41 44 45 52 3d 22 2e 2e 2e 22 20 6f 6e 20 HEADER="..." on
1afe1 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d the compiler com
1afe2 6d 61 6e 64 2d 6c 69 6e 65 2e 20 20 54 68 65 0a mand-line. The.
1afe3 2a 2a 20 68 65 61 64 65 72 20 6d 75 73 74 20 62 ** header must b
1afe4 65 20 65 78 61 63 74 6c 79 20 31 36 20 62 79 74 e exactly 16 byt
1afe5 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 es including the
1afe6 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 zero-terminator
1afe7 20 73 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e so.** the strin
1afe8 67 20 69 74 73 65 6c 66 20 73 68 6f 75 6c 64 20 g itself should
1afe9 62 65 20 31 35 20 63 68 61 72 61 63 74 65 72 73 be 15 characters
1afea 20 6c 6f 6e 67 2e 20 20 49 66 20 79 6f 75 20 63 long. If you c
1afeb 68 61 6e 67 65 0a 2a 2a 20 74 68 65 20 68 65 61 hange.** the hea
1afec 64 65 72 2c 20 74 68 65 6e 20 79 6f 75 72 20 63 der, then your c
1afed 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 20 77 69 ustom library wi
1afee 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 ll not be able t
1afef 6f 20 72 65 61 64 20 0a 2a 2a 20 64 61 74 61 62 o read .** datab
1aff0 61 73 65 73 20 67 65 6e 65 72 61 74 65 64 20 62 ases generated b
1aff1 79 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 74 y the standard t
1aff2 6f 6f 6c 73 20 61 6e 64 20 74 68 65 20 73 74 61 ools and the sta
1aff3 6e 64 61 72 64 20 74 6f 6f 6c 73 0a 2a 2a 20 77 ndard tools.** w
1aff4 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 ill not be able
1aff5 74 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65 to read database
1aff6 73 20 63 72 65 61 74 65 64 20 62 79 20 79 6f 75 s created by you
1aff7 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 r custom library
1aff8 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
1aff9 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 ITE_FILE_HEADER
1affa 2f 2a 20 31 32 33 34 35 36 37 38 39 20 31 32 33 /* 123456789 123
1affb 34 35 36 20 2a 2f 0a 23 20 20 64 65 66 69 6e 65 456 */.# define
1affc 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 SQLITE_FILE_HEA
1affd 44 45 52 20 22 53 51 4c 69 74 65 20 66 6f 72 6d DER "SQLite form
1affe 61 74 20 33 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a at 3".#endif../*
1afff 0a 2a 2a 20 50 61 67 65 20 74 79 70 65 20 66 6c .** Page type fl
1b000 61 67 73 2e 20 20 41 6e 20 4f 52 65 64 20 63 6f ags. An ORed co
1b001 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 mbination of the
1b002 73 65 20 66 6c 61 67 73 20 61 70 70 65 61 72 20 se flags appear
1b003 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 as the.** first
1b004 62 79 74 65 20 6f 66 20 6f 6e 2d 64 69 73 6b 20 byte of on-disk
1b005 69 6d 61 67 65 20 6f 66 20 65 76 65 72 79 20 42 image of every B
1b006 54 72 65 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 64 Tree page..*/.#d
1b007 65 66 69 6e 65 20 50 54 46 5f 49 4e 54 4b 45 59 efine PTF_INTKEY
1b008 20 20 20 20 30 78 30 31 0a 23 64 65 66 69 6e 65 0x01.#define
1b009 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 20 30 PTF_ZERODATA 0
1b00a 78 30 32 0a 23 64 65 66 69 6e 65 20 50 54 46 5f x02.#define PTF_
1b00b 4c 45 41 46 44 41 54 41 20 20 30 78 30 34 0a 23 LEAFDATA 0x04.#
1b00c 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 20 define PTF_LEAF
1b00d 20 20 20 20 20 30 78 30 38 0a 0a 2f 2a 0a 2a 2a 0x08../*.**
1b00e 20 41 73 20 65 61 63 68 20 70 61 67 65 20 6f 66 As each page of
1b00f 20 74 68 65 20 66 69 6c 65 20 69 73 20 6c 6f 61 the file is loa
1b010 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2c ded into memory,
1b011 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
1b012 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a the following.**
1b013 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70 structure is ap
1b014 70 65 6e 64 65 64 20 61 6e 64 20 69 6e 69 74 69 pended and initi
1b015 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2e 20 alized to zero.
1b016 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
1b017 73 74 6f 72 65 73 0a 2a 2a 20 69 6e 66 6f 72 6d stores.** inform
1b018 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 ation about the
1b019 70 61 67 65 20 74 68 61 74 20 69 73 20 64 65 63 page that is dec
1b01a 6f 64 65 64 20 66 72 6f 6d 20 74 68 65 20 72 61 oded from the ra
1b01b 77 20 66 69 6c 65 20 70 61 67 65 2e 0a 2a 2a 0a w file page..**.
1b01c 2a 2a 20 54 68 65 20 70 50 61 72 65 6e 74 20 66 ** The pParent f
1b01d 69 65 6c 64 20 70 6f 69 6e 74 73 20 62 61 63 6b ield points back
1b01e 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 to the parent p
1b01f 61 67 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 age. This allow
1b020 73 20 75 73 20 74 6f 0a 2a 2a 20 77 61 6c 6b 20 s us to.** walk
1b021 75 70 20 74 68 65 20 42 54 72 65 65 20 66 72 6f up the BTree fro
1b022 6d 20 61 6e 79 20 6c 65 61 66 20 74 6f 20 74 68 m any leaf to th
1b023 65 20 72 6f 6f 74 2e 20 20 43 61 72 65 20 6d 75 e root. Care mu
1b024 73 74 20 62 65 20 74 61 6b 65 6e 20 74 6f 0a 2a st be taken to.*
1b025 2a 20 75 6e 72 65 66 28 29 20 74 68 65 20 70 61 * unref() the pa
1b026 72 65 6e 74 20 70 61 67 65 20 70 6f 69 6e 74 65 rent page pointe
1b027 72 20 77 68 65 6e 20 74 68 69 73 20 70 61 67 65 r when this page
1b028 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 is no longer re
1b029 66 65 72 65 6e 63 65 64 2e 0a 2a 2a 20 54 68 65 ferenced..** The
1b02a 20 70 61 67 65 44 65 73 74 72 75 63 74 6f 72 28 pageDestructor(
1b02b 29 20 72 6f 75 74 69 6e 65 20 68 61 6e 64 6c 65 ) routine handle
1b02c 73 20 74 68 61 74 20 63 68 6f 72 65 2e 0a 2a 2a s that chore..**
1b02d 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f 20 61 6c .** Access to al
1b02e 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 l fields of this
1b02f 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 6f structure is co
1b030 6e 74 72 6f 6c 6c 65 64 20 62 79 20 74 68 65 20 ntrolled by the
1b031 6d 75 74 65 78 0a 2a 2a 20 73 74 6f 72 65 64 20 mutex.** stored
1b032 69 6e 20 4d 65 6d 50 61 67 65 2e 70 42 74 2d 3e in MemPage.pBt->
1b033 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 mutex..*/.struct
1b034 20 4d 65 6d 50 61 67 65 20 7b 0a 20 20 75 38 20 MemPage {. u8
1b035 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 isInit;
1b036 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 65 /* True if pre
1b037 76 69 6f 75 73 6c 79 20 69 6e 69 74 69 61 6c 69 viously initiali
1b038 7a 65 64 2e 20 4d 55 53 54 20 42 45 20 46 49 52 zed. MUST BE FIR
1b039 53 54 21 20 2a 2f 0a 20 20 75 38 20 6e 4f 76 65 ST! */. u8 nOve
1b03a 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 20 2f 2a rflow; /*
1b03b 20 4e 75 6d 62 65 72 20 6f 66 20 6f 76 65 72 66 Number of overf
1b03c 6c 6f 77 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 low cell bodies
1b03d 69 6e 20 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 in aCell[] */.
1b03e 75 38 20 69 6e 74 4b 65 79 3b 20 20 20 20 20 20 u8 intKey;
1b03f 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1b040 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73 intkey flag is s
1b041 65 74 20 2a 2f 0a 20 20 75 38 20 6c 65 61 66 3b et */. u8 leaf;
1b042 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b043 54 72 75 65 20 69 66 20 6c 65 61 66 20 66 6c 61 True if leaf fla
1b044 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 g is set */. u8
1b045 20 68 61 73 44 61 74 61 3b 20 20 20 20 20 20 20 hasData;
1b046 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
1b047 69 73 20 70 61 67 65 20 73 74 6f 72 65 73 20 64 is page stores d
1b048 61 74 61 20 2a 2f 0a 20 20 75 38 20 68 64 72 4f ata */. u8 hdrO
1b049 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a ffset; /*
1b04a 20 31 30 30 20 66 6f 72 20 70 61 67 65 20 31 2e 100 for page 1.
1b04b 20 20 30 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0 otherwise */
1b04c 0a 20 20 75 38 20 63 68 69 6c 64 50 74 72 53 69 . u8 childPtrSi
1b04d 7a 65 3b 20 20 20 20 20 2f 2a 20 30 20 69 66 20 ze; /* 0 if
1b04e 6c 65 61 66 3d 3d 31 2e 20 20 34 20 69 66 20 6c leaf==1. 4 if l
1b04f 65 61 66 3d 3d 30 20 2a 2f 0a 20 20 75 38 20 6d eaf==0 */. u8 m
1b050 61 78 31 62 79 74 65 50 61 79 6c 6f 61 64 3b 20 ax1bytePayload;
1b051 20 2f 2a 20 6d 69 6e 28 6d 61 78 4c 6f 63 61 6c /* min(maxLocal
1b052 2c 31 32 37 29 20 2a 2f 0a 20 20 75 31 36 20 6d ,127) */. u16 m
1b053 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 axLocal;
1b054 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68 61 /* Copy of BtSha
1b055 72 65 64 2e 6d 61 78 4c 6f 63 61 6c 20 6f 72 20 red.maxLocal or
1b056 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 65 61 66 BtShared.maxLeaf
1b057 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 */. u16 minLoc
1b058 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f al; /* Co
1b059 70 79 20 6f 66 20 42 74 53 68 61 72 65 64 2e 6d py of BtShared.m
1b05a 69 6e 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 inLocal or BtSha
1b05b 72 65 64 2e 6d 69 6e 4c 65 61 66 20 2a 2f 0a 20 red.minLeaf */.
1b05c 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b u16 cellOffset;
1b05d 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 /* Index i
1b05e 6e 20 61 44 61 74 61 20 6f 66 20 66 69 72 73 74 n aData of first
1b05f 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f cell pointer */
1b060 0a 20 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 . u16 nFree;
1b061 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1b062 72 20 6f 66 20 66 72 65 65 20 62 79 74 65 73 20 r of free bytes
1b063 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 on the page */.
1b064 20 75 31 36 20 6e 43 65 6c 6c 3b 20 20 20 20 20 u16 nCell;
1b065 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1b066 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69 73 of cells on this
1b067 20 70 61 67 65 2c 20 6c 6f 63 61 6c 20 61 6e 64 page, local and
1b068 20 6f 76 66 6c 20 2a 2f 0a 20 20 75 31 36 20 6d ovfl */. u16 m
1b069 61 73 6b 50 61 67 65 3b 20 20 20 20 20 20 20 20 askPage;
1b06a 2f 2a 20 4d 61 73 6b 20 66 6f 72 20 70 61 67 65 /* Mask for page
1b06b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 75 31 36 offset */. u16
1b06c 20 61 69 4f 76 66 6c 5b 35 5d 3b 20 20 20 20 20 aiOvfl[5];
1b06d 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65 20 /* Insert the
1b06e 69 2d 74 68 20 6f 76 65 72 66 6c 6f 77 20 63 65 i-th overflow ce
1b06f 6c 6c 20 62 65 66 6f 72 65 20 74 68 65 20 61 69 ll before the ai
1b070 4f 76 66 6c 2d 74 68 0a 20 20 20 20 20 20 20 20 Ovfl-th.
1b071 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
1b072 2a 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77 20 63 * non-overflow c
1b073 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 61 70 4f ell */. u8 *apO
1b074 76 66 6c 5b 35 5d 3b 20 20 20 20 20 20 20 2f 2a vfl[5]; /*
1b075 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 Pointers to the
1b076 20 62 6f 64 79 20 6f 66 20 6f 76 65 72 66 6c 6f body of overflo
1b077 77 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 42 74 53 w cells */. BtS
1b078 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 hared *pBt;
1b079 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
1b07a 42 74 53 68 61 72 65 64 20 74 68 61 74 20 74 68 BtShared that th
1b07b 69 73 20 70 61 67 65 20 69 73 20 70 61 72 74 20 is page is part
1b07c 6f 66 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61 74 of */. u8 *aDat
1b07d 61 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 a; /*
1b07e 50 6f 69 6e 74 65 72 20 74 6f 20 64 69 73 6b 20 Pointer to disk
1b07f 69 6d 61 67 65 20 6f 66 20 74 68 65 20 70 61 67 image of the pag
1b080 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a e data */. u8 *
1b081 61 44 61 74 61 45 6e 64 3b 20 20 20 20 20 20 20 aDataEnd;
1b082 20 2f 2a 20 4f 6e 65 20 62 79 74 65 20 70 61 73 /* One byte pas
1b083 74 20 74 68 65 20 65 6e 64 20 6f 66 20 75 73 61 t the end of usa
1b084 62 6c 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 ble data */. u8
1b085 20 2a 61 43 65 6c 6c 49 64 78 3b 20 20 20 20 20 *aCellIdx;
1b086 20 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 69 /* The cell i
1b087 6e 64 65 78 20 61 72 65 61 20 2a 2f 0a 20 20 44 ndex area */. D
1b088 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 bPage *pDbPage;
1b089 20 20 20 20 2f 2a 20 50 61 67 65 72 20 70 61 67 /* Pager pag
1b08a 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 e handle */. Pg
1b08b 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 no pgno;
1b08c 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 /* Page numbe
1b08d 72 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 20 r for this page
1b08e 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 */.};../*.** The
1b08f 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 6d 61 67 65 in-memory image
1b090 20 6f 66 20 61 20 64 69 73 6b 20 70 61 67 65 20 of a disk page
1b091 68 61 73 20 74 68 65 20 61 75 78 69 6c 69 61 72 has the auxiliar
1b092 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 y information ap
1b093 70 65 6e 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 pended.** to the
1b094 20 65 6e 64 2e 20 20 45 58 54 52 41 5f 53 49 5a end. EXTRA_SIZ
1b095 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 E is the number
1b096 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 of bytes of spac
1b097 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 e needed to hold
1b098 0a 2a 2a 20 74 68 61 74 20 65 78 74 72 61 20 69 .** that extra i
1b099 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 nformation..*/.#
1b09a 64 65 66 69 6e 65 20 45 58 54 52 41 5f 53 49 5a define EXTRA_SIZ
1b09b 45 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 E sizeof(MemPage
1b09c 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 6e 6b 65 )../*.** A linke
1b09d 64 20 6c 69 73 74 20 6f 66 20 74 68 65 20 66 6f d list of the fo
1b09e 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
1b09f 65 73 20 69 73 20 73 74 6f 72 65 64 20 61 74 20 es is stored at
1b0a0 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 2e 0a BtShared.pLock..
1b0a1 2a 2a 20 4c 6f 63 6b 73 20 61 72 65 20 61 64 64 ** Locks are add
1b0a2 65 64 20 28 6f 72 20 75 70 67 72 61 64 65 64 20 ed (or upgraded
1b0a3 66 72 6f 6d 20 52 45 41 44 5f 4c 4f 43 4b 20 74 from READ_LOCK t
1b0a4 6f 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 77 68 o WRITE_LOCK) wh
1b0a5 65 6e 20 61 20 63 75 72 73 6f 72 20 0a 2a 2a 20 en a cursor .**
1b0a6 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 is opened on the
1b0a7 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 table with root
1b0a8 20 70 61 67 65 20 42 74 53 68 61 72 65 64 2e 69 page BtShared.i
1b0a9 54 61 62 6c 65 2e 20 4c 6f 63 6b 73 20 61 72 65 Table. Locks are
1b0aa 20 72 65 6d 6f 76 65 64 0a 2a 2a 20 66 72 6f 6d removed.** from
1b0ab 20 74 68 69 73 20 6c 69 73 74 20 77 68 65 6e 20 this list when
1b0ac 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
1b0ad 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f committed or ro
1b0ae 6c 6c 65 64 20 62 61 63 6b 2c 20 6f 72 20 77 68 lled back, or wh
1b0af 65 6e 0a 2a 2a 20 61 20 62 74 72 65 65 20 68 61 en.** a btree ha
1b0b0 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e 0a ndle is closed..
1b0b1 2a 2f 0a 73 74 72 75 63 74 20 42 74 4c 6f 63 6b */.struct BtLock
1b0b2 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 {. Btree *pBtr
1b0b3 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 74 ee; /* Bt
1b0b4 72 65 65 20 68 61 6e 64 6c 65 20 68 6f 6c 64 69 ree handle holdi
1b0b5 6e 67 20 74 68 69 73 20 6c 6f 63 6b 20 2a 2f 0a ng this lock */.
1b0b6 20 20 50 67 6e 6f 20 69 54 61 62 6c 65 3b 20 20 Pgno iTable;
1b0b7 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 /* Root
1b0b8 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 2a 2f page of table */
1b0b9 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20 20 20 20 . u8 eLock;
1b0ba 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41 44 /* READ
1b0bb 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c _LOCK or WRITE_L
1b0bc 4f 43 4b 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 OCK */. BtLock
1b0bd 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 2f *pNext; /
1b0be 2a 20 4e 65 78 74 20 69 6e 20 42 74 53 68 61 72 * Next in BtShar
1b0bf 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 20 2a 2f ed.pLock list */
1b0c0 0a 7d 3b 0a 0a 2f 2a 20 43 61 6e 64 69 64 61 74 .};../* Candidat
1b0c1 65 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 4c e values for BtL
1b0c2 6f 63 6b 2e 65 4c 6f 63 6b 20 2a 2f 0a 23 64 65 ock.eLock */.#de
1b0c3 66 69 6e 65 20 52 45 41 44 5f 4c 4f 43 4b 20 20 fine READ_LOCK
1b0c4 20 20 20 31 0a 23 64 65 66 69 6e 65 20 57 52 49 1.#define WRI
1b0c5 54 45 5f 4c 4f 43 4b 20 20 20 20 32 0a 0a 2f 2a TE_LOCK 2../*
1b0c6 20 41 20 42 74 72 65 65 20 68 61 6e 64 6c 65 0a A Btree handle.
1b0c7 2a 2a 0a 2a 2a 20 41 20 64 61 74 61 62 61 73 65 **.** A database
1b0c8 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 74 connection cont
1b0c9 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 ains a pointer t
1b0ca 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 o an instance of
1b0cb 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65 63 74 20 .** this object
1b0cc 66 6f 72 20 65 76 65 72 79 20 64 61 74 61 62 61 for every databa
1b0cd 73 65 20 66 69 6c 65 20 74 68 61 74 20 69 74 20 se file that it
1b0ce 68 61 73 20 6f 70 65 6e 2e 20 20 54 68 69 73 20 has open. This
1b0cf 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 structure.** is
1b0d0 6f 70 61 71 75 65 20 74 6f 20 74 68 65 20 64 61 opaque to the da
1b0d1 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1b0d2 6e 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 n. The database
1b0d3 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 6e connection cann
1b0d4 6f 74 0a 2a 2a 20 73 65 65 20 74 68 65 20 69 6e ot.** see the in
1b0d5 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 ternals of this
1b0d6 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 6f 6e structure and on
1b0d7 6c 79 20 64 65 61 6c 73 20 77 69 74 68 20 70 6f ly deals with po
1b0d8 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69 inters to.** thi
1b0d9 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a s structure..**.
1b0da 2a 2a 20 46 6f 72 20 73 6f 6d 65 20 64 61 74 61 ** For some data
1b0db 62 61 73 65 20 66 69 6c 65 73 2c 20 74 68 65 20 base files, the
1b0dc 73 61 6d 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 same underlying
1b0dd 64 61 74 61 62 61 73 65 20 63 61 63 68 65 20 6d database cache m
1b0de 69 67 68 74 20 62 65 20 0a 2a 2a 20 73 68 61 72 ight be .** shar
1b0df 65 64 20 62 65 74 77 65 65 6e 20 6d 75 6c 74 69 ed between multi
1b0e0 70 6c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e ple connections.
1b0e1 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 In that case,
1b0e2 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a each connection.
1b0e3 2a 2a 20 68 61 73 20 69 74 20 6f 77 6e 20 69 6e ** has it own in
1b0e4 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f stance of this o
1b0e5 62 6a 65 63 74 2e 20 20 42 75 74 20 65 61 63 68 bject. But each
1b0e6 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
1b0e7 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e s object.** poin
1b0e8 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 42 ts to the same B
1b0e9 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 tShared object.
1b0ea 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 61 The database ca
1b0eb 63 68 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 che and the.** s
1b0ec 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64 chema associated
1b0ed 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 with the databa
1b0ee 73 65 20 66 69 6c 65 20 61 72 65 20 61 6c 6c 20 se file are all
1b0ef 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e contained within
1b0f0 0a 2a 2a 20 74 68 65 20 42 74 53 68 61 72 65 64 .** the BtShared
1b0f1 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 object..**.** A
1b0f2 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20 74 68 69 ll fields in thi
1b0f3 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 s structure are
1b0f4 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 73 accessed under s
1b0f5 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2a qlite3.mutex..**
1b0f6 20 54 68 65 20 70 42 74 20 70 6f 69 6e 74 65 72 The pBt pointer
1b0f7 20 69 74 73 65 6c 66 20 6d 61 79 20 6e 6f 74 20 itself may not
1b0f8 62 65 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 be changed while
1b0f9 20 74 68 65 72 65 20 65 78 69 73 74 73 20 63 75 there exists cu
1b0fa 72 73 6f 72 73 20 0a 2a 2a 20 69 6e 20 74 68 65 rsors .** in the
1b0fb 20 72 65 66 65 72 65 6e 63 65 64 20 42 74 53 68 referenced BtSh
1b0fc 61 72 65 64 20 74 68 61 74 20 70 6f 69 6e 74 20 ared that point
1b0fd 62 61 63 6b 20 74 6f 20 74 68 69 73 20 42 74 72 back to this Btr
1b0fe 65 65 20 73 69 6e 63 65 20 74 68 6f 73 65 0a 2a ee since those.*
1b0ff 2a 20 63 75 72 73 6f 72 73 20 68 61 76 65 20 74 * cursors have t
1b100 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 74 68 69 o go through thi
1b101 73 20 42 74 72 65 65 20 74 6f 20 66 69 6e 64 20 s Btree to find
1b102 74 68 65 69 72 20 42 74 53 68 61 72 65 64 20 61 their BtShared a
1b103 6e 64 0a 2a 2a 20 74 68 65 79 20 6f 66 74 65 6e nd.** they often
1b104 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 68 do so without h
1b105 6f 6c 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d olding sqlite3.m
1b106 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 utex..*/.struct
1b107 42 74 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 Btree {. sqlite
1b108 33 20 2a 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 3 *db; /*
1b109 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
1b10a 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 nection holding
1b10b 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 this btree */.
1b10c 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 BtShared *pBt;
1b10d 20 20 20 2f 2a 20 53 68 61 72 61 62 6c 65 20 63 /* Sharable c
1b10e 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 62 ontent of this b
1b10f 74 72 65 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 tree */. u8 inT
1b110 72 61 6e 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 rans; /*
1b111 54 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e TRANS_NONE, TRAN
1b112 53 5f 52 45 41 44 20 6f 72 20 54 52 41 4e 53 5f S_READ or TRANS_
1b113 57 52 49 54 45 20 2a 2f 0a 20 20 75 38 20 73 68 WRITE */. u8 sh
1b114 61 72 61 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a arable; /*
1b115 20 54 72 75 65 20 69 66 20 77 65 20 63 61 6e 20 True if we can
1b116 73 68 61 72 65 20 70 42 74 20 77 69 74 68 20 61 share pBt with a
1b117 6e 6f 74 68 65 72 20 64 62 20 2a 2f 0a 20 20 75 nother db */. u
1b118 38 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 8 locked;
1b119 20 20 2f 2a 20 54 72 75 65 20 69 66 20 64 62 20 /* True if db
1b11a 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 70 42 currently has pB
1b11b 74 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e t locked */. in
1b11c 74 20 77 61 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20 t wantToLock;
1b11d 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 /* Number of ne
1b11e 73 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 sted calls to sq
1b11f 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
1b120 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b ) */. int nBack
1b121 75 70 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d up; /* Num
1b122 62 65 72 20 6f 66 20 62 61 63 6b 75 70 20 6f 70 ber of backup op
1b123 65 72 61 74 69 6f 6e 73 20 72 65 61 64 69 6e 67 erations reading
1b124 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 this btree */.
1b125 20 42 74 72 65 65 20 2a 70 4e 65 78 74 3b 20 20 Btree *pNext;
1b126 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f /* List of o
1b127 74 68 65 72 20 73 68 61 72 61 62 6c 65 20 42 74 ther sharable Bt
1b128 72 65 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61 rees from the sa
1b129 6d 65 20 64 62 20 2a 2f 0a 20 20 42 74 72 65 65 me db */. Btree
1b12a 20 2a 70 50 72 65 76 3b 20 20 20 20 20 20 2f 2a *pPrev; /*
1b12b 20 42 61 63 6b 20 70 6f 69 6e 74 65 72 20 6f 66 Back pointer of
1b12c 20 74 68 65 20 73 61 6d 65 20 6c 69 73 74 20 2a the same list *
1b12d 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1b12e 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
1b12f 48 45 0a 20 20 42 74 4c 6f 63 6b 20 6c 6f 63 6b HE. BtLock lock
1b130 3b 20 20 20 20 20 20 20 2f 2a 20 4f 62 6a 65 63 ; /* Objec
1b131 74 20 75 73 65 64 20 74 6f 20 6c 6f 63 6b 20 70 t used to lock p
1b132 61 67 65 20 31 20 2a 2f 0a 23 65 6e 64 69 66 0a age 1 */.#endif.
1b133 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74 72 65 65 2e };../*.** Btree.
1b134 69 6e 54 72 61 6e 73 20 6d 61 79 20 74 61 6b 65 inTrans may take
1b135 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
1b136 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a owing values..**
1b137 0a 2a 2a 20 49 66 20 74 68 65 20 73 68 61 72 65 .** If the share
1b138 64 2d 64 61 74 61 20 65 78 74 65 6e 73 69 6f 6e d-data extension
1b139 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 is enabled, the
1b13a 72 65 20 6d 61 79 20 62 65 20 6d 75 6c 74 69 70 re may be multip
1b13b 6c 65 20 75 73 65 72 73 0a 2a 2a 20 6f 66 20 74 le users.** of t
1b13c 68 65 20 42 74 72 65 65 20 73 74 72 75 63 74 75 he Btree structu
1b13d 72 65 2e 20 41 74 20 6d 6f 73 74 20 6f 6e 65 20 re. At most one
1b13e 6f 66 20 74 68 65 73 65 20 6d 61 79 20 6f 70 65 of these may ope
1b13f 6e 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 n a write transa
1b140 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20 61 6e ction,.** but an
1b141 79 20 6e 75 6d 62 65 72 20 6d 61 79 20 68 61 76 y number may hav
1b142 65 20 61 63 74 69 76 65 20 72 65 61 64 20 74 72 e active read tr
1b143 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 ansactions..*/.#
1b144 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 4e 4f 4e define TRANS_NON
1b145 45 20 20 30 0a 23 64 65 66 69 6e 65 20 54 52 41 E 0.#define TRA
1b146 4e 53 5f 52 45 41 44 20 20 31 0a 23 64 65 66 69 NS_READ 1.#defi
1b147 6e 65 20 54 52 41 4e 53 5f 57 52 49 54 45 20 32 ne TRANS_WRITE 2
1b148 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
1b149 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
1b14a 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 ct represents a
1b14b 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 single database
1b14c 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 41 20 73 file..** .** A s
1b14d 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 ingle database f
1b14e 69 6c 65 20 63 61 6e 20 62 65 20 69 6e 20 75 73 ile can be in us
1b14f 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 e at the same ti
1b150 6d 65 20 62 79 20 74 77 6f 0a 2a 2a 20 6f 72 20 me by two.** or
1b151 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 63 6f more database co
1b152 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 57 68 65 6e nnections. When
1b153 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 6f 6e two or more con
1b154 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 nections are.**
1b155 73 68 61 72 69 6e 67 20 74 68 65 20 73 61 6d 65 sharing the same
1b156 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
1b157 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 each connection
1b158 68 61 73 20 69 74 20 6f 77 6e 0a 2a 2a 20 70 72 has it own.** pr
1b159 69 76 61 74 65 20 42 74 72 65 65 20 6f 62 6a 65 ivate Btree obje
1b15a 63 74 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 ct for the file
1b15b 61 6e 64 20 65 61 63 68 20 6f 66 20 74 68 6f 73 and each of thos
1b15c 65 20 42 74 72 65 65 73 20 70 6f 69 6e 74 73 0a e Btrees points.
1b15d 2a 2a 20 74 6f 20 74 68 69 73 20 6f 6e 65 20 42 ** to this one B
1b15e 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 tShared object.
1b15f 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 69 BtShared.nRef i
1b160 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a s the number of.
1b161 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 ** connections c
1b162 75 72 72 65 6e 74 6c 79 20 73 68 61 72 69 6e 67 urrently sharing
1b163 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66 this database f
1b164 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 ile..**.** Field
1b165 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 s in this struct
1b166 75 72 65 20 61 72 65 20 61 63 63 65 73 73 65 64 ure are accessed
1b167 20 75 6e 64 65 72 20 74 68 65 20 42 74 53 68 61 under the BtSha
1b168 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 6d 75 74 red.mutex.** mut
1b169 65 78 2c 20 65 78 63 65 70 74 20 66 6f 72 20 6e ex, except for n
1b16a 52 65 66 20 61 6e 64 20 70 4e 65 78 74 20 77 68 Ref and pNext wh
1b16b 69 63 68 20 61 72 65 20 61 63 63 65 73 73 65 64 ich are accessed
1b16c 20 75 6e 64 65 72 20 74 68 65 0a 2a 2a 20 67 6c under the.** gl
1b16d 6f 62 61 6c 20 53 51 4c 49 54 45 5f 4d 55 54 45 obal SQLITE_MUTE
1b16e 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 X_STATIC_MASTER
1b16f 6d 75 74 65 78 2e 20 20 54 68 65 20 70 50 61 67 mutex. The pPag
1b170 65 72 20 66 69 65 6c 64 0a 2a 2a 20 6d 61 79 20 er field.** may
1b171 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20 not be modified
1b172 6f 6e 63 65 20 69 74 20 69 73 20 69 6e 69 74 69 once it is initi
1b173 61 6c 6c 79 20 73 65 74 20 61 73 20 6c 6f 6e 67 ally set as long
1b174 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a 20 54 as nRef>0..** T
1b175 68 65 20 70 53 63 68 65 6d 61 20 66 69 65 6c 64 he pSchema field
1b176 20 6d 61 79 20 62 65 20 73 65 74 20 6f 6e 63 65 may be set once
1b177 20 75 6e 64 65 72 20 42 74 53 68 61 72 65 64 2e under BtShared.
1b178 6d 75 74 65 78 20 61 6e 64 0a 2a 2a 20 74 68 65 mutex and.** the
1b179 72 65 61 66 74 65 72 20 69 73 20 75 6e 63 68 61 reafter is uncha
1b17a 6e 67 65 64 20 61 73 20 6c 6f 6e 67 20 61 73 20 nged as long as
1b17b 6e 52 65 66 3e 30 2e 0a 2a 2a 0a 2a 2a 20 69 73 nRef>0..**.** is
1b17c 50 65 6e 64 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 Pending:.**.**
1b17d 20 49 66 20 61 20 42 74 53 68 61 72 65 64 20 63 If a BtShared c
1b17e 6c 69 65 6e 74 20 66 61 69 6c 73 20 74 6f 20 6f lient fails to o
1b17f 62 74 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f btain a write-lo
1b180 63 6b 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 ck on a database
1b181 0a 2a 2a 20 20 20 74 61 62 6c 65 20 28 62 65 63 .** table (bec
1b182 61 75 73 65 20 74 68 65 72 65 20 65 78 69 73 74 ause there exist
1b183 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 s one or more re
1b184 61 64 2d 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 ad-locks on the
1b185 74 61 62 6c 65 29 2c 0a 2a 2a 20 20 20 74 68 65 table),.** the
1b186 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 65 6e shared-cache en
1b187 74 65 72 73 20 27 70 65 6e 64 69 6e 67 2d 6c 6f ters 'pending-lo
1b188 63 6b 27 20 73 74 61 74 65 20 61 6e 64 20 69 73 ck' state and is
1b189 50 65 6e 64 69 6e 67 20 69 73 0a 2a 2a 20 20 20 Pending is.**
1b18a 73 65 74 20 74 6f 20 74 72 75 65 2e 0a 2a 2a 0a set to true..**.
1b18b 2a 2a 20 20 20 54 68 65 20 73 68 61 72 65 64 2d ** The shared-
1b18c 63 61 63 68 65 20 6c 65 61 76 65 73 20 74 68 65 cache leaves the
1b18d 20 27 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 27 20 'pending lock'
1b18e 73 74 61 74 65 20 77 68 65 6e 20 65 69 74 68 65 state when eithe
1b18f 72 20 6f 66 0a 2a 2a 20 20 20 74 68 65 20 66 6f r of.** the fo
1b190 6c 6c 6f 77 69 6e 67 20 6f 63 63 75 72 3a 0a 2a llowing occur:.*
1b191 2a 0a 2a 2a 20 20 20 20 20 31 29 20 54 68 65 20 *.** 1) The
1b192 63 75 72 72 65 6e 74 20 77 72 69 74 65 72 20 28 current writer (
1b193 42 74 53 68 61 72 65 64 2e 70 57 72 69 74 65 72 BtShared.pWriter
1b194 29 20 63 6f 6e 63 6c 75 64 65 73 20 69 74 73 20 ) concludes its
1b195 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 4f 52 0a transaction, OR.
1b196 2a 2a 20 20 20 20 20 32 29 20 54 68 65 20 6e 75 ** 2) The nu
1b197 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65 mber of locks he
1b198 6c 64 20 62 79 20 6f 74 68 65 72 20 63 6f 6e 6e ld by other conn
1b199 65 63 74 69 6f 6e 73 20 64 72 6f 70 73 20 74 6f ections drops to
1b19a 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 77 zero..**.** w
1b19b 68 69 6c 65 20 69 6e 20 74 68 65 20 27 70 65 6e hile in the 'pen
1b19c 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74 65 ding-lock' state
1b19d 2c 20 6e 6f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 , no connection
1b19e 6d 61 79 20 73 74 61 72 74 20 61 20 6e 65 77 0a may start a new.
1b19f 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
1b1a0 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 66 ..**.** This f
1b1a1 65 61 74 75 72 65 20 69 73 20 69 6e 63 6c 75 64 eature is includ
1b1a2 65 64 20 74 6f 20 68 65 6c 70 20 70 72 65 76 65 ed to help preve
1b1a3 6e 74 20 77 72 69 74 65 72 2d 73 74 61 72 76 61 nt writer-starva
1b1a4 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 tion..*/.struct
1b1a5 42 74 53 68 61 72 65 64 20 7b 0a 20 20 50 61 67 BtShared {. Pag
1b1a6 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 er *pPager;
1b1a7 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 63 /* The page c
1b1a8 61 63 68 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ache */. sqlite
1b1a9 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 3 *db;
1b1aa 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e /* Database conn
1b1ab 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 ection currently
1b1ac 20 75 73 69 6e 67 20 74 68 69 73 20 42 74 72 65 using this Btre
1b1ad 65 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 e */. BtCursor
1b1ae 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 *pCursor; /*
1b1af 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 70 A list of all op
1b1b0 65 6e 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 en cursors */.
1b1b1 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b MemPage *pPage1;
1b1b2 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 /* First p
1b1b3 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 age of the datab
1b1b4 61 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70 65 6e ase */. u8 open
1b1b5 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f Flags; /
1b1b6 2a 20 46 6c 61 67 73 20 74 6f 20 73 71 6c 69 74 * Flags to sqlit
1b1b7 65 33 42 74 72 65 65 4f 70 65 6e 28 29 20 2a 2f e3BtreeOpen() */
1b1b8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1b1b9 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
1b1ba 20 20 75 38 20 61 75 74 6f 56 61 63 75 75 6d 3b u8 autoVacuum;
1b1bb 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
1b1bc 69 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 if auto-vacuum i
1b1bd 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 75 s enabled */. u
1b1be 38 20 69 6e 63 72 56 61 63 75 75 6d 3b 20 20 20 8 incrVacuum;
1b1bf 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1b1c0 69 6e 63 72 2d 76 61 63 75 75 6d 20 69 73 20 65 incr-vacuum is e
1b1c1 6e 61 62 6c 65 64 20 2a 2f 0a 23 65 6e 64 69 66 nabled */.#endif
1b1c2 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 61 63 74 . u8 inTransact
1b1c3 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 54 72 61 6e ion; /* Tran
1b1c4 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 2a 2f saction state */
1b1c5 0a 20 20 75 38 20 6d 61 78 31 62 79 74 65 50 61 . u8 max1bytePa
1b1c6 79 6c 6f 61 64 3b 20 20 20 2f 2a 20 4d 61 78 69 yload; /* Maxi
1b1c7 6d 75 6d 20 66 69 72 73 74 20 62 79 74 65 20 6f mum first byte o
1b1c8 66 20 63 65 6c 6c 20 66 6f 72 20 61 20 31 2d 62 f cell for a 1-b
1b1c9 79 74 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 yte payload */.
1b1ca 20 75 31 36 20 62 74 73 46 6c 61 67 73 3b 20 20 u16 btsFlags;
1b1cb 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 /* Boolea
1b1cc 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 53 n parameters. S
1b1cd 65 65 20 42 54 53 5f 2a 20 6d 61 63 72 6f 73 20 ee BTS_* macros
1b1ce 62 65 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6d below */. u16 m
1b1cf 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 axLocal;
1b1d0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 /* Maximum loca
1b1d1 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e l payload in non
1b1d2 2d 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 73 -LEAFDATA tables
1b1d3 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 */. u16 minLoc
1b1d4 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d al; /* M
1b1d5 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 inimum local pay
1b1d6 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 load in non-LEAF
1b1d7 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 DATA tables */.
1b1d8 20 75 31 36 20 6d 61 78 4c 65 61 66 3b 20 20 20 u16 maxLeaf;
1b1d9 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
1b1da 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 m local payload
1b1db 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 61 in a LEAFDATA ta
1b1dc 62 6c 65 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e ble */. u16 min
1b1dd 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f Leaf; /
1b1de 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 * Minimum local
1b1df 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 payload in a LEA
1b1e0 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 FDATA table */.
1b1e1 20 75 33 32 20 70 61 67 65 53 69 7a 65 3b 20 20 u32 pageSize;
1b1e2 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 /* Total
1b1e3 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
1b1e4 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 75 on a page */. u
1b1e5 33 32 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 32 usableSize;
1b1e6 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1b1e7 66 20 75 73 61 62 6c 65 20 62 79 74 65 73 20 6f f usable bytes o
1b1e8 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 n each page */.
1b1e9 20 69 6e 74 20 6e 54 72 61 6e 73 61 63 74 69 6f int nTransactio
1b1ea 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 n; /* Number
1b1eb 20 6f 66 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 of open transac
1b1ec 74 69 6f 6e 73 20 28 72 65 61 64 20 2b 20 77 72 tions (read + wr
1b1ed 69 74 65 29 20 2a 2f 0a 20 20 75 33 32 20 6e 50 ite) */. u32 nP
1b1ee 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
1b1ef 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
1b1f0 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 es in the databa
1b1f1 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 se */. void *pS
1b1f2 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a chema; /*
1b1f3 20 50 6f 69 6e 74 65 72 20 74 6f 20 73 70 61 63 Pointer to spac
1b1f4 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 e allocated by s
1b1f5 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d qlite3BtreeSchem
1b1f6 61 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a a() */. void (*
1b1f7 78 46 72 65 65 53 63 68 65 6d 61 29 28 76 6f 69 xFreeSchema)(voi
1b1f8 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 d*); /* Destruc
1b1f9 74 6f 72 20 66 6f 72 20 42 74 53 68 61 72 65 64 tor for BtShared
1b1fa 2e 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 .pSchema */. sq
1b1fb 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
1b1fc 65 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72 ex; /* Non-recur
1b1fd 73 69 76 65 20 6d 75 74 65 78 20 72 65 71 75 69 sive mutex requi
1b1fe 72 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 red to access th
1b1ff 69 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 42 is object */. B
1b200 69 74 76 65 63 20 2a 70 48 61 73 43 6f 6e 74 65 itvec *pHasConte
1b201 6e 74 3b 20 20 2f 2a 20 53 65 74 20 6f 66 20 70 nt; /* Set of p
1b202 61 67 65 73 20 6d 6f 76 65 64 20 74 6f 20 66 72 ages moved to fr
1b203 65 65 2d 6c 69 73 74 20 74 68 69 73 20 74 72 61 ee-list this tra
1b204 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e nsaction */.#ifn
1b205 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1b206 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 SHARED_CACHE. i
1b207 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 nt nRef;
1b208 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1b209 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 f references to
1b20a 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a this structure *
1b20b 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4e /. BtShared *pN
1b20c 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 ext; /* Nex
1b20d 74 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66 20 73 t on a list of s
1b20e 68 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 harable BtShared
1b20f 20 73 74 72 75 63 74 73 20 2a 2f 0a 20 20 42 74 structs */. Bt
1b210 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 Lock *pLock;
1b211 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6c /* List of l
1b212 6f 63 6b 73 20 68 65 6c 64 20 6f 6e 20 74 68 69 ocks held on thi
1b213 73 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73 s shared-btree s
1b214 74 72 75 63 74 20 2a 2f 0a 20 20 42 74 72 65 65 truct */. Btree
1b215 20 2a 70 57 72 69 74 65 72 3b 20 20 20 20 20 20 *pWriter;
1b216 20 2f 2a 20 42 74 72 65 65 20 77 69 74 68 20 63 /* Btree with c
1b217 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 20 77 72 urrently open wr
1b218 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite transaction
1b219 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 38 20 2a */.#endif. u8 *
1b21a 70 54 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20 pTmpSpace;
1b21b 20 20 2f 2a 20 42 74 53 68 61 72 65 64 2e 70 61 /* BtShared.pa
1b21c 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 geSize bytes of
1b21d 73 70 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 space for tmp us
1b21e 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 e */.};../*.** A
1b21f 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f llowed values fo
1b220 72 20 42 74 53 68 61 72 65 64 2e 62 74 73 46 6c r BtShared.btsFl
1b221 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 ags.*/.#define B
1b222 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 20 20 20 20 TS_READ_ONLY
1b223 20 20 20 20 30 78 30 30 30 31 20 20 20 2f 2a 20 0x0001 /*
1b224 55 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 Underlying file
1b225 69 73 20 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a 23 is readonly */.#
1b226 64 65 66 69 6e 65 20 42 54 53 5f 50 41 47 45 53 define BTS_PAGES
1b227 49 5a 45 5f 46 49 58 45 44 20 20 20 30 78 30 30 IZE_FIXED 0x00
1b228 30 32 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 02 /* Page siz
1b229 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 e can no longer
1b22a 62 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 be changed */.#d
1b22b 65 66 69 6e 65 20 42 54 53 5f 53 45 43 55 52 45 efine BTS_SECURE
1b22c 5f 44 45 4c 45 54 45 20 20 20 20 30 78 30 30 30 _DELETE 0x000
1b22d 34 20 20 20 2f 2a 20 50 52 41 47 4d 41 20 73 65 4 /* PRAGMA se
1b22e 63 75 72 65 5f 64 65 6c 65 74 65 20 69 73 20 65 cure_delete is e
1b22f 6e 61 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e nabled */.#defin
1b230 65 20 42 54 53 5f 49 4e 49 54 49 41 4c 4c 59 5f e BTS_INITIALLY_
1b231 45 4d 50 54 59 20 20 30 78 30 30 30 38 20 20 20 EMPTY 0x0008
1b232 2f 2a 20 44 61 74 61 62 61 73 65 20 77 61 73 20 /* Database was
1b233 65 6d 70 74 79 20 61 74 20 74 72 61 6e 73 20 73 empty at trans s
1b234 74 61 72 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tart */.#define
1b235 42 54 53 5f 4e 4f 5f 57 41 4c 20 20 20 20 20 20 BTS_NO_WAL
1b236 20 20 20 20 20 30 78 30 30 31 30 20 20 20 2f 2a 0x0010 /*
1b237 20 44 6f 20 6e 6f 74 20 6f 70 65 6e 20 77 72 69 Do not open wri
1b238 74 65 2d 61 68 65 61 64 2d 6c 6f 67 20 66 69 6c te-ahead-log fil
1b239 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 es */.#define BT
1b23a 53 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20 S_EXCLUSIVE
1b23b 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a 20 70 0x0020 /* p
1b23c 57 72 69 74 65 72 20 68 61 73 20 61 6e 20 65 78 Writer has an ex
1b23d 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a clusive lock */.
1b23e 23 64 65 66 69 6e 65 20 42 54 53 5f 50 45 4e 44 #define BTS_PEND
1b23f 49 4e 47 20 20 20 20 20 20 20 20 20 20 30 78 30 ING 0x0
1b240 30 34 30 20 20 20 2f 2a 20 57 61 69 74 69 6e 67 040 /* Waiting
1b241 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b 73 20 for read-locks
1b242 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 0a 2f 2a 0a to clear */../*.
1b243 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
1b244 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
1b245 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 structure is use
1b246 64 20 74 6f 20 68 6f 6c 64 20 69 6e 66 6f 72 6d d to hold inform
1b247 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 61 ation.** about a
1b248 20 63 65 6c 6c 2e 20 20 54 68 65 20 70 61 72 73 cell. The pars
1b249 65 43 65 6c 6c 50 74 72 28 29 20 66 75 6e 63 74 eCellPtr() funct
1b24a 69 6f 6e 20 66 69 6c 6c 73 20 69 6e 20 74 68 69 ion fills in thi
1b24b 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 62 s structure.** b
1b24c 61 73 65 64 20 6f 6e 20 69 6e 66 6f 72 6d 61 74 ased on informat
1b24d 69 6f 6e 20 65 78 74 72 61 63 74 20 66 72 6f 6d ion extract from
1b24e 20 74 68 65 20 72 61 77 20 64 69 73 6b 20 70 61 the raw disk pa
1b24f 67 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ge..*/.typedef s
1b250 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 43 truct CellInfo C
1b251 65 6c 6c 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20 ellInfo;.struct
1b252 43 65 6c 6c 49 6e 66 6f 20 7b 0a 20 20 69 36 34 CellInfo {. i64
1b253 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 nKey; /* T
1b254 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54 4b 45 he key for INTKE
1b255 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e 75 6d Y tables, or num
1b256 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
1b257 6b 65 79 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 key */. u8 *pCe
1b258 6c 6c 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 ll; /* Point
1b259 65 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 er to the start
1b25a 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 of cell content
1b25b 2a 2f 0a 20 20 75 33 32 20 6e 44 61 74 61 3b 20 */. u32 nData;
1b25c 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1b25d 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 2a bytes of data *
1b25e 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 64 /. u32 nPayload
1b25f 3b 20 20 2f 2a 20 54 6f 74 61 6c 20 61 6d 6f 75 ; /* Total amou
1b260 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 2a 2f nt of payload */
1b261 0a 20 20 75 31 36 20 6e 48 65 61 64 65 72 3b 20 . u16 nHeader;
1b262 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
1b263 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 cell content he
1b264 61 64 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f ader in bytes */
1b265 0a 20 20 75 31 36 20 6e 4c 6f 63 61 6c 3b 20 20 . u16 nLocal;
1b266 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 70 /* Amount of p
1b267 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 ayload held loca
1b268 6c 6c 79 20 2a 2f 0a 20 20 75 31 36 20 69 4f 76 lly */. u16 iOv
1b269 65 72 66 6c 6f 77 3b 20 2f 2a 20 4f 66 66 73 65 erflow; /* Offse
1b26a 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 t to overflow pa
1b26b 67 65 20 6e 75 6d 62 65 72 2e 20 20 5a 65 72 6f ge number. Zero
1b26c 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 if no overflow
1b26d 2a 2f 0a 20 20 75 31 36 20 6e 53 69 7a 65 3b 20 */. u16 nSize;
1b26e 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
1b26f 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
1b270 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74 72 on the main b-tr
1b271 65 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f ee page */.};../
1b272 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70 *.** Maximum dep
1b273 74 68 20 6f 66 20 61 6e 20 53 51 4c 69 74 65 20 th of an SQLite
1b274 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 65 B-Tree structure
1b275 2e 20 41 6e 79 20 42 2d 54 72 65 65 20 64 65 65 . Any B-Tree dee
1b276 70 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 69 73 per than.** this
1b277 20 77 69 6c 6c 20 62 65 20 64 65 63 6c 61 72 65 will be declare
1b278 64 20 63 6f 72 72 75 70 74 2e 20 54 68 69 73 20 d corrupt. This
1b279 76 61 6c 75 65 20 69 73 20 63 61 6c 63 75 6c 61 value is calcula
1b27a 74 65 64 20 62 61 73 65 64 20 6f 6e 20 61 0a 2a ted based on a.*
1b27b 2a 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61 * maximum databa
1b27c 73 65 20 73 69 7a 65 20 6f 66 20 32 5e 33 31 20 se size of 2^31
1b27d 70 61 67 65 73 20 61 20 6d 69 6e 69 6d 75 6d 20 pages a minimum
1b27e 66 61 6e 6f 75 74 20 6f 66 20 32 20 66 6f 72 20 fanout of 2 for
1b27f 61 0a 2a 2a 20 72 6f 6f 74 2d 6e 6f 64 65 20 61 a.** root-node a
1b280 6e 64 20 33 20 66 6f 72 20 61 6c 6c 20 6f 74 68 nd 3 for all oth
1b281 65 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 er internal node
1b282 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 s..**.** If a tr
1b283 65 65 20 74 68 61 74 20 61 70 70 65 61 72 73 20 ee that appears
1b284 74 6f 20 62 65 20 74 61 6c 6c 65 72 20 74 68 61 to be taller tha
1b285 6e 20 74 68 69 73 20 69 73 20 65 6e 63 6f 75 6e n this is encoun
1b286 74 65 72 65 64 2c 20 69 74 20 69 73 0a 2a 2a 20 tered, it is.**
1b287 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 assumed that the
1b288 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 database is cor
1b289 72 75 70 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 rupt..*/.#define
1b28a 20 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 BTCURSOR_MAX_DE
1b28b 50 54 48 20 32 30 0a 0a 2f 2a 0a 2a 2a 20 41 20 PTH 20../*.** A
1b28c 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e cursor is a poin
1b28d 74 65 72 20 74 6f 20 61 20 70 61 72 74 69 63 75 ter to a particu
1b28e 6c 61 72 20 65 6e 74 72 79 20 77 69 74 68 69 6e lar entry within
1b28f 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a a particular.**
1b290 20 62 2d 74 72 65 65 20 77 69 74 68 69 6e 20 61 b-tree within a
1b291 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
1b292 2a 2a 0a 2a 2a 20 54 68 65 20 65 6e 74 72 79 20 **.** The entry
1b293 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 is identified by
1b294 20 69 74 73 20 4d 65 6d 50 61 67 65 20 61 6e 64 its MemPage and
1b295 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a the index in.**
1b296 20 4d 65 6d 50 61 67 65 2e 61 43 65 6c 6c 5b 5d MemPage.aCell[]
1b297 20 6f 66 20 74 68 65 20 65 6e 74 72 79 2e 0a 2a of the entry..*
1b298 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 *.** A single da
1b299 74 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 tabase file can
1b29a 62 65 20 73 68 61 72 65 64 20 62 79 20 74 77 6f be shared by two
1b29b 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 63 more database c
1b29c 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 62 onnections,.** b
1b29d 75 74 20 63 75 72 73 6f 72 73 20 63 61 6e 6e 6f ut cursors canno
1b29e 74 20 62 65 20 73 68 61 72 65 64 2e 20 20 45 61 t be shared. Ea
1b29f 63 68 20 63 75 72 73 6f 72 20 69 73 20 61 73 73 ch cursor is ass
1b2a0 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 0a 2a ociated with a.*
1b2a1 2a 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 * particular dat
1b2a2 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1b2a3 20 69 64 65 6e 74 69 66 69 65 64 20 42 74 43 75 identified BtCu
1b2a4 72 73 6f 72 2e 70 42 74 72 65 65 2e 64 62 2e 0a rsor.pBtree.db..
1b2a5 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20 **.** Fields in
1b2a6 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 this structure a
1b2a7 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 re accessed unde
1b2a8 72 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6d r the BtShared.m
1b2a9 75 74 65 78 0a 2a 2a 20 66 6f 75 6e 64 20 61 74 utex.** found at
1b2aa 20 73 65 6c 66 2d 3e 70 42 74 2d 3e 6d 75 74 65 self->pBt->mute
1b2ab 78 2e 20 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 x. .*/.struct Bt
1b2ac 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 72 65 65 Cursor {. Btree
1b2ad 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 20 20 *pBtree;
1b2ae 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 72 65 /* The Btre
1b2af 65 20 74 6f 20 77 68 69 63 68 20 74 68 69 73 20 e to which this
1b2b0 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 2a cursor belongs *
1b2b1 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 /. BtShared *pB
1b2b2 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
1b2b3 20 54 68 65 20 42 74 53 68 61 72 65 64 20 74 68 The BtShared th
1b2b4 69 73 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 is cursor points
1b2b5 20 74 6f 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f to */. BtCurso
1b2b6 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 r *pNext, *pPrev
1b2b7 3b 20 20 2f 2a 20 46 6f 72 6d 73 20 61 20 6c 69 ; /* Forms a li
1b2b8 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c nked list of all
1b2b9 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 73 74 cursors */. st
1b2ba 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b ruct KeyInfo *pK
1b2bb 65 79 49 6e 66 6f 3b 20 2f 2a 20 41 72 67 75 6d eyInfo; /* Argum
1b2bc 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 63 6f ent passed to co
1b2bd 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f mparison functio
1b2be 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c n */.#ifndef SQL
1b2bf 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
1b2c0 42 0a 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72 66 B. Pgno *aOverf
1b2c1 6c 6f 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a low; /*
1b2c2 20 43 61 63 68 65 20 6f 66 20 6f 76 65 72 66 6c Cache of overfl
1b2c3 6f 77 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e ow page location
1b2c4 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 50 67 s */.#endif. Pg
1b2c5 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 20 no pgnoRoot;
1b2c6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 /* The r
1b2c7 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69 73 oot page of this
1b2c8 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c 69 74 tree */. sqlit
1b2c9 65 33 5f 69 6e 74 36 34 20 63 61 63 68 65 64 52 e3_int64 cachedR
1b2ca 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 72 6f owid; /* Next ro
1b2cb 77 69 64 20 63 61 63 68 65 2e 20 20 30 20 6d 65 wid cache. 0 me
1b2cc 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 2a 2f ans not valid */
1b2cd 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f . CellInfo info
1b2ce 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1b2cf 41 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 63 A parse of the c
1b2d0 65 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e 74 ell we are point
1b2d1 69 6e 67 20 61 74 20 2a 2f 0a 20 20 69 36 34 20 ing at */. i64
1b2d2 6e 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 nKey; /*
1b2d3 53 69 7a 65 20 6f 66 20 70 4b 65 79 2c 20 6f 72 Size of pKey, or
1b2d4 20 6c 61 73 74 20 69 6e 74 65 67 65 72 20 6b 65 last integer ke
1b2d5 79 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65 y */. void *pKe
1b2d6 79 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 y; /* Saved
1b2d7 20 6b 65 79 20 74 68 61 74 20 77 61 73 20 63 75 key that was cu
1b2d8 72 73 6f 72 27 73 20 6c 61 73 74 20 6b 6e 6f 77 rsor's last know
1b2d9 6e 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 20 20 n position */.
1b2da 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b 20 20 20 int skipNext;
1b2db 20 2f 2a 20 50 72 65 76 28 29 20 69 73 20 6e 6f /* Prev() is no
1b2dc 6f 70 20 69 66 20 6e 65 67 61 74 69 76 65 2e 20 op if negative.
1b2dd 4e 65 78 74 28 29 20 69 73 20 6e 6f 6f 70 20 69 Next() is noop i
1b2de 66 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 20 f positive */.
1b2df 75 38 20 77 72 46 6c 61 67 3b 20 20 20 20 20 20 u8 wrFlag;
1b2e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1b2e1 65 20 69 66 20 77 72 69 74 61 62 6c 65 20 2a 2f e if writable */
1b2e2 0a 20 20 75 38 20 61 74 4c 61 73 74 3b 20 20 20 . u8 atLast;
1b2e3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b2e4 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 Cursor pointing
1b2e5 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 to the last entr
1b2e6 79 20 2a 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e y */. u8 validN
1b2e7 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 Key;
1b2e8 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 66 6f /* True if info
1b2e9 2e 6e 4b 65 79 20 69 73 20 76 61 6c 69 64 20 2a .nKey is valid *
1b2ea 2f 0a 20 20 75 38 20 65 53 74 61 74 65 3b 20 20 /. u8 eState;
1b2eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1b2ec 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 55 52 53 One of the CURS
1b2ed 4f 52 5f 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 OR_XXX constants
1b2ee 20 28 73 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a (see below) */.
1b2ef 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1b2f0 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 75 MIT_INCRBLOB. u
1b2f1 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 8 isIncrblobHand
1b2f2 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 le; /* True
1b2f3 20 69 66 20 74 68 69 73 20 63 75 72 73 6f 72 20 if this cursor
1b2f4 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20 68 is an incr. io h
1b2f5 61 6e 64 6c 65 20 2a 2f 0a 23 65 6e 64 69 66 0a andle */.#endif.
1b2f6 20 20 75 38 20 68 69 6e 74 73 3b 20 20 20 20 20 u8 hints;
1b2f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b2f8 20 20 20 20 20 20 20 20 2f 2a 20 41 73 20 63 6f /* As co
1b2f9 6e 66 69 67 75 72 65 64 20 62 79 20 43 75 72 73 nfigured by Curs
1b2fa 6f 72 53 65 74 48 69 6e 74 73 28 29 20 2a 2f 0a orSetHints() */.
1b2fb 20 20 69 31 36 20 69 50 61 67 65 3b 20 20 20 20 i16 iPage;
1b2fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b2fd 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
1b2fe 20 6f 66 20 63 75 72 72 65 6e 74 20 70 61 67 65 of current page
1b2ff 20 69 6e 20 61 70 50 61 67 65 20 2a 2f 0a 20 20 in apPage */.
1b300 75 31 36 20 61 69 49 64 78 5b 42 54 43 55 52 53 u16 aiIdx[BTCURS
1b301 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 OR_MAX_DEPTH];
1b302 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 /* Current
1b303 20 69 6e 64 65 78 20 69 6e 20 61 70 50 61 67 65 index in apPage
1b304 5b 69 5d 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 [i] */. MemPage
1b305 20 2a 61 70 50 61 67 65 5b 42 54 43 55 52 53 4f *apPage[BTCURSO
1b306 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 2f R_MAX_DEPTH]; /
1b307 2a 20 50 61 67 65 73 20 66 72 6f 6d 20 72 6f 6f * Pages from roo
1b308 74 20 74 6f 20 63 75 72 72 65 6e 74 20 70 61 67 t to current pag
1b309 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 e */.};../*.** P
1b30a 6f 74 65 6e 74 69 61 6c 20 76 61 6c 75 65 73 20 otential values
1b30b 66 6f 72 20 42 74 43 75 72 73 6f 72 2e 65 53 74 for BtCursor.eSt
1b30c 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f ate..**.** CURSO
1b30d 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 R_VALID:.** Cu
1b30e 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 rsor points to a
1b30f 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 67 65 valid entry. ge
1b310 74 50 61 79 6c 6f 61 64 28 29 20 65 74 63 2e 20 tPayload() etc.
1b311 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 2e 0a 2a may be called..*
1b312 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41 *.** CURSOR_INVA
1b313 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f 72 LID:.** Cursor
1b314 20 64 6f 65 73 20 6e 6f 74 20 70 6f 69 6e 74 20 does not point
1b315 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 to a valid entry
1b316 2e 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 . This can happe
1b317 6e 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 20 n (for example)
1b318 0a 2a 2a 20 20 20 62 65 63 61 75 73 65 20 74 68 .** because th
1b319 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 e table is empty
1b31a 20 6f 72 20 62 65 63 61 75 73 65 20 42 74 72 65 or because Btre
1b31b 65 43 75 72 73 6f 72 46 69 72 73 74 28 29 20 68 eCursorFirst() h
1b31c 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 20 as not been.**
1b31d 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 called..**.** C
1b31e 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
1b31f 4b 3a 0a 2a 2a 20 20 20 54 68 65 20 74 61 62 6c K:.** The tabl
1b320 65 20 74 68 61 74 20 74 68 69 73 20 63 75 72 73 e that this curs
1b321 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e or was opened on
1b322 20 73 74 69 6c 6c 20 65 78 69 73 74 73 2c 20 62 still exists, b
1b323 75 74 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 ut has been .**
1b324 20 20 6d 6f 64 69 66 69 65 64 20 73 69 6e 63 65 modified since
1b325 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 the cursor was
1b326 6c 61 73 74 20 75 73 65 64 2e 20 54 68 65 20 63 last used. The c
1b327 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 ursor position i
1b328 73 20 73 61 76 65 64 0a 2a 2a 20 20 20 69 6e 20 s saved.** in
1b329 76 61 72 69 61 62 6c 65 73 20 42 74 43 75 72 73 variables BtCurs
1b32a 6f 72 2e 70 4b 65 79 20 61 6e 64 20 42 74 43 75 or.pKey and BtCu
1b32b 72 73 6f 72 2e 6e 4b 65 79 2e 20 57 68 65 6e 20 rsor.nKey. When
1b32c 61 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 0a a cursor is in .
1b32d 2a 2a 20 20 20 74 68 69 73 20 73 74 61 74 65 2c ** this state,
1b32e 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
1b32f 73 69 74 69 6f 6e 28 29 20 63 61 6e 20 62 65 20 sition() can be
1b330 63 61 6c 6c 65 64 20 74 6f 20 61 74 74 65 6d 70 called to attemp
1b331 74 20 74 6f 0a 2a 2a 20 20 20 73 65 65 6b 20 74 t to.** seek t
1b332 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 he cursor to the
1b333 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 2e saved position.
1b334 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 46 41 .**.** CURSOR_FA
1b335 55 4c 54 3a 0a 2a 2a 20 20 20 41 20 75 6e 72 65 ULT:.** A unre
1b336 63 6f 76 65 72 61 62 6c 65 20 65 72 72 6f 72 20 coverable error
1b337 28 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 72 (an I/O error or
1b338 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 a malloc failur
1b339 65 29 20 68 61 73 20 6f 63 63 75 72 72 65 64 0a e) has occurred.
1b33a 2a 2a 20 20 20 6f 6e 20 61 20 64 69 66 66 65 72 ** on a differ
1b33b 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 ent connection t
1b33c 68 61 74 20 73 68 61 72 65 73 20 74 68 65 20 42 hat shares the B
1b33d 74 53 68 61 72 65 64 20 63 61 63 68 65 20 77 69 tShared cache wi
1b33e 74 68 20 74 68 69 73 0a 2a 2a 20 20 20 63 75 72 th this.** cur
1b33f 73 6f 72 2e 20 20 54 68 65 20 65 72 72 6f 72 20 sor. The error
1b340 68 61 73 20 6c 65 66 74 20 74 68 65 20 63 61 63 has left the cac
1b341 68 65 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 he in an inconsi
1b342 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a 2a 20 stent state..**
1b343 20 20 44 6f 20 6e 6f 74 68 69 6e 67 20 65 6c 73 Do nothing els
1b344 65 20 77 69 74 68 20 74 68 69 73 20 63 75 72 73 e with this curs
1b345 6f 72 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 or. Any attempt
1b346 20 74 6f 20 75 73 65 20 74 68 65 20 63 75 72 73 to use the curs
1b347 6f 72 0a 2a 2a 20 20 20 73 68 6f 75 6c 64 20 72 or.** should r
1b348 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 eturn the error
1b349 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e 20 42 code stored in B
1b34a 74 43 75 72 73 6f 72 2e 73 6b 69 70 0a 2a 2f 0a tCursor.skip.*/.
1b34b 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 49 #define CURSOR_I
1b34c 4e 56 41 4c 49 44 20 20 20 20 20 20 20 20 20 20 NVALID
1b34d 20 30 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 0.#define CURSO
1b34e 52 5f 56 41 4c 49 44 20 20 20 20 20 20 20 20 20 R_VALID
1b34f 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 43 55 1.#define CU
1b350 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
1b351 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 2.#define
1b352 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 CURSOR_FAULT
1b353 20 20 20 20 20 20 20 20 20 20 33 0a 0a 2f 2a 20 3../*
1b354 0a 2a 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 .** The database
1b355 20 70 61 67 65 20 74 68 65 20 50 45 4e 44 49 4e page the PENDIN
1b356 47 5f 42 59 54 45 20 6f 63 63 75 70 69 65 73 2e G_BYTE occupies.
1b357 20 54 68 69 73 20 70 61 67 65 20 69 73 20 6e 65 This page is ne
1b358 76 65 72 20 75 73 65 64 2e 0a 2a 2f 0a 23 20 64 ver used..*/.# d
1b359 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59 efine PENDING_BY
1b35a 54 45 5f 50 41 47 45 28 70 42 74 29 20 50 41 47 TE_PAGE(pBt) PAG
1b35b 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 42 74 29 0a ER_MJ_PGNO(pBt).
1b35c 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 ./*.** These mac
1b35d 72 6f 73 20 64 65 66 69 6e 65 20 74 68 65 20 6c ros define the l
1b35e 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 ocation of the p
1b35f 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 ointer-map entry
1b360 20 66 6f 72 20 61 20 0a 2a 2a 20 64 61 74 61 62 for a .** datab
1b361 61 73 65 20 70 61 67 65 2e 20 54 68 65 20 66 69 ase page. The fi
1b362 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
1b363 65 61 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 each is the numb
1b364 65 72 20 6f 66 20 75 73 61 62 6c 65 0a 2a 2a 20 er of usable.**
1b365 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 bytes on each pa
1b366 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ge of the databa
1b367 73 65 20 28 6f 66 74 65 6e 20 31 30 32 34 29 2e se (often 1024).
1b368 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74 The second is t
1b369 68 65 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 he.** page numbe
1b36a 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 20 69 6e 20 r to look up in
1b36b 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e the pointer map.
1b36c 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 50 41 .**.** PTRMAP_PA
1b36d 47 45 4e 4f 20 72 65 74 75 72 6e 73 20 74 68 65 GENO returns the
1b36e 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 6e database page n
1b36f 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 6f 69 umber of the poi
1b370 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 70 61 67 65 nter-map.** page
1b371 20 74 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 that stores the
1b372 20 72 65 71 75 69 72 65 64 20 70 6f 69 6e 74 65 required pointe
1b373 72 2e 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 r. PTRMAP_PTROFF
1b374 53 45 54 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 SET returns.** t
1b375 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 he offset of the
1b376 20 72 65 71 75 65 73 74 65 64 20 6d 61 70 20 65 requested map e
1b377 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ntry..**.** If t
1b378 68 65 20 70 67 6e 6f 20 61 72 67 75 6d 65 6e 74 he pgno argument
1b379 20 70 61 73 73 65 64 20 74 6f 20 50 54 52 4d 41 passed to PTRMA
1b37a 50 5f 50 41 47 45 4e 4f 20 69 73 20 61 20 70 6f P_PAGENO is a po
1b37b 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 0a inter-map page,.
1b37c 2a 2a 20 74 68 65 6e 20 70 67 6e 6f 20 69 73 20 ** then pgno is
1b37d 72 65 74 75 72 6e 65 64 2e 20 53 6f 20 28 70 67 returned. So (pg
1b37e 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e no==PTRMAP_PAGEN
1b37f 4f 28 70 67 73 7a 2c 20 70 67 6e 6f 29 29 20 63 O(pgsz, pgno)) c
1b380 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f an be.** used to
1b381 20 74 65 73 74 20 69 66 20 70 67 6e 6f 20 69 73 test if pgno is
1b382 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 a pointer-map p
1b383 61 67 65 2e 20 50 54 52 4d 41 50 5f 49 53 50 41 age. PTRMAP_ISPA
1b384 47 45 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a GE implements.**
1b385 20 74 68 69 73 20 74 65 73 74 2e 0a 2a 2f 0a 23 this test..*/.#
1b386 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 50 41 define PTRMAP_PA
1b387 47 45 4e 4f 28 70 42 74 2c 20 70 67 6e 6f 29 20 GENO(pBt, pgno)
1b388 70 74 72 6d 61 70 50 61 67 65 6e 6f 28 70 42 74 ptrmapPageno(pBt
1b389 2c 20 70 67 6e 6f 29 0a 23 64 65 66 69 6e 65 20 , pgno).#define
1b38a 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 PTRMAP_PTROFFSET
1b38b 28 70 67 70 74 72 6d 61 70 2c 20 70 67 6e 6f 29 (pgptrmap, pgno)
1b38c 20 28 35 2a 28 70 67 6e 6f 2d 70 67 70 74 72 6d (5*(pgno-pgptrm
1b38d 61 70 2d 31 29 29 0a 23 64 65 66 69 6e 65 20 50 ap-1)).#define P
1b38e 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 TRMAP_ISPAGE(pBt
1b38f 2c 20 70 67 6e 6f 29 20 28 50 54 52 4d 41 50 5f , pgno) (PTRMAP_
1b390 50 41 47 45 4e 4f 28 28 70 42 74 29 2c 28 70 67 PAGENO((pBt),(pg
1b391 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29 29 0a 0a 2f no))==(pgno))../
1b392 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 *.** The pointer
1b393 20 6d 61 70 20 69 73 20 61 20 6c 6f 6f 6b 75 70 map is a lookup
1b394 20 74 61 62 6c 65 20 74 68 61 74 20 69 64 65 6e table that iden
1b395 74 69 66 69 65 73 20 74 68 65 20 70 61 72 65 6e tifies the paren
1b396 74 20 70 61 67 65 20 66 6f 72 0a 2a 2a 20 65 61 t page for.** ea
1b397 63 68 20 63 68 69 6c 64 20 70 61 67 65 20 69 6e ch child page in
1b398 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1b399 6c 65 2e 20 20 54 68 65 20 70 61 72 65 6e 74 20 le. The parent
1b39a 70 61 67 65 20 69 73 20 74 68 65 20 70 61 67 65 page is the page
1b39b 20 74 68 61 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e that.** contain
1b39c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
1b39d 68 65 20 63 68 69 6c 64 2e 20 20 45 76 65 72 79 he child. Every
1b39e 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 page in the dat
1b39f 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 73 0a 2a abase contains.*
1b3a0 2a 20 30 20 6f 72 20 31 20 70 61 72 65 6e 74 20 * 0 or 1 parent
1b3a1 70 61 67 65 73 2e 20 20 28 49 6e 20 74 68 69 73 pages. (In this
1b3a2 20 63 6f 6e 74 65 78 74 20 27 64 61 74 61 62 61 context 'databa
1b3a3 73 65 20 70 61 67 65 27 20 72 65 66 65 72 73 0a se page' refers.
1b3a4 2a 2a 20 74 6f 20 61 6e 79 20 70 61 67 65 20 74 ** to any page t
1b3a5 68 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 hat is not part
1b3a6 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d of the pointer m
1b3a7 61 70 20 69 74 73 65 6c 66 2e 29 20 20 45 61 63 ap itself.) Eac
1b3a8 68 20 70 6f 69 6e 74 65 72 20 6d 61 70 0a 2a 2a h pointer map.**
1b3a9 20 65 6e 74 72 79 20 63 6f 6e 73 69 73 74 73 20 entry consists
1b3aa 6f 66 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 of a single byte
1b3ab 20 27 74 79 70 65 27 20 61 6e 64 20 61 20 34 20 'type' and a 4
1b3ac 62 79 74 65 20 70 61 72 65 6e 74 20 70 61 67 65 byte parent page
1b3ad 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 54 68 65 20 number..** The
1b3ae 50 54 52 4d 41 50 5f 58 58 58 20 69 64 65 6e 74 PTRMAP_XXX ident
1b3af 69 66 69 65 72 73 20 62 65 6c 6f 77 20 61 72 65 ifiers below are
1b3b0 20 74 68 65 20 76 61 6c 69 64 20 74 79 70 65 73 the valid types
1b3b1 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 75 72 70 ..**.** The purp
1b3b2 6f 73 65 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 ose of the point
1b3b3 65 72 20 6d 61 70 20 69 73 20 74 6f 20 66 61 63 er map is to fac
1b3b4 69 6c 69 74 79 20 6d 6f 76 69 6e 67 20 70 61 67 ility moving pag
1b3b5 65 73 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 70 es from one.** p
1b3b6 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 osition in the f
1b3b7 69 6c 65 20 74 6f 20 61 6e 6f 74 68 65 72 20 61 ile to another a
1b3b8 73 20 70 61 72 74 20 6f 66 20 61 75 74 6f 76 61 s part of autova
1b3b9 63 75 75 6d 2e 20 20 57 68 65 6e 20 61 20 70 61 cuum. When a pa
1b3ba 67 65 0a 2a 2a 20 69 73 20 6d 6f 76 65 64 2c 20 ge.** is moved,
1b3bb 74 68 65 20 70 6f 69 6e 74 65 72 20 69 6e 20 69 the pointer in i
1b3bc 74 73 20 70 61 72 65 6e 74 20 6d 75 73 74 20 62 ts parent must b
1b3bd 65 20 75 70 64 61 74 65 64 20 74 6f 20 70 6f 69 e updated to poi
1b3be 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 77 nt to the.** new
1b3bf 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 location. The
1b3c0 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 75 pointer map is u
1b3c1 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 sed to locate th
1b3c2 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 71 75 e parent page qu
1b3c3 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 ickly..**.** PTR
1b3c4 4d 41 50 5f 52 4f 4f 54 50 41 47 45 3a 20 54 68 MAP_ROOTPAGE: Th
1b3c5 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 e database page
1b3c6 69 73 20 61 20 72 6f 6f 74 2d 70 61 67 65 2e 20 is a root-page.
1b3c7 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 The page-number
1b3c8 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 is not.**
1b3c9 20 20 20 20 20 20 20 20 20 20 20 75 73 65 64 20 used
1b3ca 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a in this case..**
1b3cb 0a 2a 2a 20 50 54 52 4d 41 50 5f 46 52 45 45 50 .** PTRMAP_FREEP
1b3cc 41 47 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 AGE: The databas
1b3cd 65 20 70 61 67 65 20 69 73 20 61 6e 20 75 6e 75 e page is an unu
1b3ce 73 65 64 20 28 66 72 65 65 29 20 70 61 67 65 2e sed (free) page.
1b3cf 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 The page-number
1b3d0 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 .**
1b3d1 20 20 20 20 20 20 69 73 20 6e 6f 74 20 75 73 65 is not use
1b3d2 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a d in this case..
1b3d3 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 **.** PTRMAP_OVE
1b3d4 52 46 4c 4f 57 31 3a 20 54 68 65 20 64 61 74 61 RFLOW1: The data
1b3d5 62 61 73 65 20 70 61 67 65 20 69 73 20 74 68 65 base page is the
1b3d6 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20 61 first page in a
1b3d7 20 6c 69 73 74 20 6f 66 20 0a 2a 2a 20 20 20 20 list of .**
1b3d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f o
1b3d9 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 verflow pages. T
1b3da 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 he page number i
1b3db 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 dentifies the pa
1b3dc 67 65 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 ge that.**
1b3dd 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e con
1b3de 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 77 tains the cell w
1b3df 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ith a pointer to
1b3e0 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 70 this overflow p
1b3e1 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 age..**.** PTRMA
1b3e2 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 20 54 68 65 P_OVERFLOW2: The
1b3e3 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 database page i
1b3e4 73 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 72 20 s the second or
1b3e5 6c 61 74 65 72 20 70 61 67 65 20 69 6e 20 61 20 later page in a
1b3e6 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 list of.**
1b3e7 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 ove
1b3e8 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 rflow pages. The
1b3e9 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 64 65 page-number ide
1b3ea 6e 74 69 66 69 65 73 20 74 68 65 20 70 72 65 76 ntifies the prev
1b3eb 69 6f 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ious.**
1b3ec 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20 69 page i
1b3ed 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 n the overflow p
1b3ee 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 age list..**.**
1b3ef 50 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 54 68 PTRMAP_BTREE: Th
1b3f0 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 e database page
1b3f1 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f 74 20 62 74 is a non-root bt
1b3f2 72 65 65 20 70 61 67 65 2e 20 54 68 65 20 70 61 ree page. The pa
1b3f3 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 ge number.**
1b3f4 20 20 20 20 20 20 20 20 20 20 20 69 64 65 6e 74 ident
1b3f5 69 66 69 65 73 20 74 68 65 20 70 61 72 65 6e 74 ifies the parent
1b3f6 20 70 61 67 65 20 69 6e 20 74 68 65 20 62 74 72 page in the btr
1b3f7 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ee..*/.#define P
1b3f8 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 31 TRMAP_ROOTPAGE 1
1b3f9 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f .#define PTRMAP_
1b3fa 46 52 45 45 50 41 47 45 20 32 0a 23 64 65 66 69 FREEPAGE 2.#defi
1b3fb 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c ne PTRMAP_OVERFL
1b3fc 4f 57 31 20 33 0a 23 64 65 66 69 6e 65 20 50 54 OW1 3.#define PT
1b3fd 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 34 RMAP_OVERFLOW2 4
1b3fe 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f .#define PTRMAP_
1b3ff 42 54 52 45 45 20 35 0a 0a 2f 2a 20 41 20 62 75 BTREE 5../* A bu
1b400 6e 63 68 20 6f 66 20 61 73 73 65 72 74 28 29 20 nch of assert()
1b401 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 63 68 statements to ch
1b402 65 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 eck the transact
1b403 69 6f 6e 20 73 74 61 74 65 20 76 61 72 69 61 62 ion state variab
1b404 6c 65 73 0a 2a 2a 20 6f 66 20 68 61 6e 64 6c 65 les.** of handle
1b405 20 70 20 28 74 79 70 65 20 42 74 72 65 65 2a 29 p (type Btree*)
1b406 20 61 72 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 are internally
1b407 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 23 consistent..*/.#
1b408 64 65 66 69 6e 65 20 62 74 72 65 65 49 6e 74 65 define btreeInte
1b409 67 72 69 74 79 28 70 29 20 5c 0a 20 20 61 73 73 grity(p) \. ass
1b40a 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 ert( p->pBt->inT
1b40b 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41 4e ransaction!=TRAN
1b40c 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d 3e 70 42 74 S_NONE || p->pBt
1b40d 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d ->nTransaction==
1b40e 30 20 29 3b 20 5c 0a 20 20 61 73 73 65 72 74 28 0 ); \. assert(
1b40f 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 p->pBt->inTrans
1b410 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 6e 54 72 61 action>=p->inTra
1b411 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a 2a 20 54 ns ); .../*.** T
1b412 68 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 he ISAUTOVACUUM
1b413 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 77 69 macro is used wi
1b414 74 68 69 6e 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e thin balance_non
1b415 72 6f 6f 74 28 29 20 74 6f 20 64 65 74 65 72 6d root() to determ
1b416 69 6e 65 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 ine.** if the da
1b417 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 tabase supports
1b418 61 75 74 6f 2d 76 61 63 75 75 6d 20 6f 72 20 6e auto-vacuum or n
1b419 6f 74 2e 20 42 65 63 61 75 73 65 20 69 74 20 69 ot. Because it i
1b41a 73 20 75 73 65 64 0a 2a 2a 20 77 69 74 68 69 6e s used.** within
1b41b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 an expression t
1b41c 68 61 74 20 69 73 20 61 6e 20 61 72 67 75 6d 65 hat is an argume
1b41d 6e 74 20 74 6f 20 61 6e 6f 74 68 65 72 20 6d 61 nt to another ma
1b41e 63 72 6f 20 0a 2a 2a 20 28 73 71 6c 69 74 65 4d cro .** (sqliteM
1b41f 61 6c 6c 6f 63 52 61 77 29 2c 20 69 74 20 69 73 allocRaw), it is
1b420 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
1b421 20 75 73 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c use conditional
1b422 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 0a 2a 2a compilation..**
1b423 20 53 6f 2c 20 74 68 69 73 20 6d 61 63 72 6f 20 So, this macro
1b424 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73 74 65 is defined inste
1b425 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ad..*/.#ifndef S
1b426 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
1b427 41 43 55 55 4d 0a 23 64 65 66 69 6e 65 20 49 53 ACUUM.#define IS
1b428 41 55 54 4f 56 41 43 55 55 4d 20 28 70 42 74 2d AUTOVACUUM (pBt-
1b429 3e 61 75 74 6f 56 61 63 75 75 6d 29 0a 23 65 6c >autoVacuum).#el
1b42a 73 65 0a 23 64 65 66 69 6e 65 20 49 53 41 55 54 se.#define ISAUT
1b42b 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 OVACUUM 0.#endif
1b42c 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 .../*.** This st
1b42d 72 75 63 74 75 72 65 20 69 73 20 70 61 73 73 65 ructure is passe
1b42e 64 20 61 72 6f 75 6e 64 20 74 68 72 6f 75 67 68 d around through
1b42f 20 61 6c 6c 20 74 68 65 20 73 61 6e 69 74 79 20 all the sanity
1b430 63 68 65 63 6b 69 6e 67 20 72 6f 75 74 69 6e 65 checking routine
1b431 73 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f s.** in order to
1b432 20 6b 65 65 70 20 74 72 61 63 6b 20 6f 66 20 73 keep track of s
1b433 6f 6d 65 20 67 6c 6f 62 61 6c 20 73 74 61 74 65 ome global state
1b434 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a information..**
1b435 0a 2a 2a 20 54 68 65 20 61 52 65 66 5b 5d 20 61 .** The aRef[] a
1b436 72 72 61 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 rray is allocate
1b437 64 20 73 6f 20 74 68 61 74 20 74 68 65 72 65 20 d so that there
1b438 69 73 20 31 20 62 69 74 20 66 6f 72 20 65 61 63 is 1 bit for eac
1b439 68 20 70 61 67 65 20 69 6e 0a 2a 2a 20 74 68 65 h page in.** the
1b43a 20 64 61 74 61 62 61 73 65 2e 20 41 73 20 74 68 database. As th
1b43b 65 20 69 6e 74 65 67 72 69 74 79 2d 63 68 65 63 e integrity-chec
1b43c 6b 20 70 72 6f 63 65 65 64 73 2c 20 66 6f 72 20 k proceeds, for
1b43d 65 61 63 68 20 70 61 67 65 20 75 73 65 64 20 69 each page used i
1b43e 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 n.** the databas
1b43f 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 e the correspond
1b440 69 6e 67 20 62 69 74 20 69 73 20 73 65 74 2e 20 ing bit is set.
1b441 54 68 69 73 20 61 6c 6c 6f 77 73 20 69 6e 74 65 This allows inte
1b442 67 72 69 74 79 2d 63 68 65 63 6b 20 74 6f 20 0a grity-check to .
1b443 2a 2a 20 64 65 74 65 63 74 20 70 61 67 65 73 20 ** detect pages
1b444 74 68 61 74 20 61 72 65 20 75 73 65 64 20 74 77 that are used tw
1b445 69 63 65 20 61 6e 64 20 6f 72 70 68 61 6e 65 64 ice and orphaned
1b446 20 70 61 67 65 73 20 28 62 6f 74 68 20 6f 66 20 pages (both of
1b447 77 68 69 63 68 20 0a 2a 2a 20 69 6e 64 69 63 61 which .** indica
1b448 74 65 20 63 6f 72 72 75 70 74 69 6f 6e 29 2e 0a te corruption)..
1b449 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
1b44a 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 49 6e t IntegrityCk In
1b44b 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72 75 63 tegrityCk;.struc
1b44c 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 7b 0a t IntegrityCk {.
1b44d 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b BtShared *pBt;
1b44e 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65 65 20 /* The tree
1b44f 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20 6f 75 being checked ou
1b450 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70 50 t */. Pager *pP
1b451 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68 65 20 ager; /* The
1b452 61 73 73 6f 63 69 61 74 65 64 20 70 61 67 65 72 associated pager
1b453 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73 69 62 . Also accessib
1b454 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61 67 65 le by pBt->pPage
1b455 72 20 2a 2f 0a 20 20 75 38 20 2a 61 50 67 52 65 r */. u8 *aPgRe
1b456 66 3b 20 20 20 20 20 20 20 2f 2a 20 31 20 62 69 f; /* 1 bi
1b457 74 20 70 65 72 20 70 61 67 65 20 69 6e 20 74 68 t per page in th
1b458 65 20 64 62 20 28 73 65 65 20 61 62 6f 76 65 29 e db (see above)
1b459 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 */. Pgno nPage
1b45a 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
1b45b 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
1b45c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
1b45d 69 6e 74 20 6d 78 45 72 72 3b 20 20 20 20 20 20 int mxErr;
1b45e 20 20 2f 2a 20 53 74 6f 70 20 61 63 63 75 6d 75 /* Stop accumu
1b45f 6c 61 74 69 6e 67 20 65 72 72 6f 72 73 20 77 68 lating errors wh
1b460 65 6e 20 74 68 69 73 20 72 65 61 63 68 65 73 20 en this reaches
1b461 7a 65 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 zero */. int nE
1b462 72 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e rr; /* N
1b463 75 6d 62 65 72 20 6f 66 20 6d 65 73 73 61 67 65 umber of message
1b464 73 20 77 72 69 74 74 65 6e 20 74 6f 20 7a 45 72 s written to zEr
1b465 72 4d 73 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 rMsg so far */.
1b466 20 69 6e 74 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 int mallocFaile
1b467 64 3b 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20 61 d; /* A memory a
1b468 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 llocation error
1b469 68 61 73 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a has occurred */.
1b46a 20 20 53 74 72 41 63 63 75 6d 20 65 72 72 4d 73 StrAccum errMs
1b46b 67 3b 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 g; /* Accumulat
1b46c 65 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 e the error mess
1b46d 61 67 65 20 74 65 78 74 20 68 65 72 65 20 2a 2f age text here */
1b46e 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 .};../*.** Routi
1b46f 6e 65 73 20 74 6f 20 72 65 61 64 20 6f 72 20 77 nes to read or w
1b470 72 69 74 65 20 61 20 74 77 6f 2d 20 61 6e 64 20 rite a two- and
1b471 66 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e four-byte big-en
1b472 64 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c dian integer val
1b473 75 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ues..*/.#define
1b474 67 65 74 32 62 79 74 65 28 78 29 20 20 20 28 28 get2byte(x) ((
1b475 78 29 5b 30 5d 3c 3c 38 20 7c 20 28 78 29 5b 31 x)[0]<<8 | (x)[1
1b476 5d 29 0a 23 64 65 66 69 6e 65 20 70 75 74 32 62 ]).#define put2b
1b477 79 74 65 28 70 2c 76 29 20 28 28 70 29 5b 30 5d yte(p,v) ((p)[0]
1b478 20 3d 20 28 75 38 29 28 28 76 29 3e 3e 38 29 2c = (u8)((v)>>8),
1b479 20 28 70 29 5b 31 5d 20 3d 20 28 75 38 29 28 76 (p)[1] = (u8)(v
1b47a 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74 34 62 )).#define get4b
1b47b 79 74 65 20 73 71 6c 69 74 65 33 47 65 74 34 62 yte sqlite3Get4b
1b47c 79 74 65 0a 23 64 65 66 69 6e 65 20 70 75 74 34 yte.#define put4
1b47d 62 79 74 65 20 73 71 6c 69 74 65 33 50 75 74 34 byte sqlite3Put4
1b47e 62 79 74 65 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a byte../*********
1b47f 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 ***** End of btr
1b480 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a eeInt.h ********
1b481 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b482 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b483 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1b484 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
1b485 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
1b486 66 66 20 69 6e 20 62 74 6d 75 74 65 78 2e 63 20 ff in btmutex.c
1b487 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b488 2a 2a 2a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 ****/.#ifndef SQ
1b489 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
1b48a 5f 43 41 43 48 45 0a 23 69 66 20 53 51 4c 49 54 _CACHE.#if SQLIT
1b48b 45 5f 54 48 52 45 41 44 53 41 46 45 0a 0a 2f 2a E_THREADSAFE../*
1b48c 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 42 .** Obtain the B
1b48d 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 61 73 tShared mutex as
1b48e 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 42 2d sociated with B-
1b48f 54 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 41 Tree handle p. A
1b490 6c 73 6f 2c 0a 2a 2a 20 73 65 74 20 42 74 53 68 lso,.** set BtSh
1b491 61 72 65 64 2e 64 62 20 74 6f 20 74 68 65 20 64 ared.db to the d
1b492 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 61 atabase handle a
1b493 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 ssociated with p
1b494 20 61 6e 64 20 74 68 65 0a 2a 2a 20 70 2d 3e 6c and the.** p->l
1b495 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 20 74 6f ocked boolean to
1b496 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 true..*/.static
1b497 20 76 6f 69 64 20 6c 6f 63 6b 42 74 72 65 65 4d void lockBtreeM
1b498 75 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a utex(Btree *p){.
1b499 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 assert( p->loc
1b49a 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 ked==0 );. asse
1b49b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1b49c 78 5f 6e 6f 74 68 65 6c 64 28 70 2d 3e 70 42 74 x_notheld(p->pBt
1b49d 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
1b49e 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1b49f 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e tex_held(p->db->
1b4a0 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 73 71 6c mutex) );.. sql
1b4a1 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
1b4a2 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b (p->pBt->mutex);
1b4a3 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 . p->pBt->db =
1b4a4 70 2d 3e 64 62 3b 0a 20 20 70 2d 3e 6c 6f 63 6b p->db;. p->lock
1b4a5 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ed = 1;.}../*.**
1b4a6 20 52 65 6c 65 61 73 65 20 74 68 65 20 42 74 53 Release the BtS
1b4a7 68 61 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f hared mutex asso
1b4a8 63 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72 ciated with B-Tr
1b4a9 65 65 20 68 61 6e 64 6c 65 20 70 20 61 6e 64 0a ee handle p and.
1b4aa 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 70 2d 3e ** clear the p->
1b4ab 6c 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 2e 0a locked boolean..
1b4ac 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 */.static void u
1b4ad 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 nlockBtreeMutex(
1b4ae 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 Btree *p){. BtS
1b4af 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
1b4b0 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 pBt;. assert( p
1b4b1 2d 3e 6c 6f 63 6b 65 64 3d 3d 31 20 29 3b 0a 20 ->locked==1 );.
1b4b2 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1b4b3 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
1b4b4 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
1b4b5 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b4b6 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
1b4b7 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
1b4b8 74 28 20 70 2d 3e 64 62 3d 3d 70 42 74 2d 3e 64 t( p->db==pBt->d
1b4b9 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f b );.. sqlite3_
1b4ba 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 42 74 2d mutex_leave(pBt-
1b4bb 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f >mutex);. p->lo
1b4bc 63 6b 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a cked = 0;.}../*.
1b4bd 2a 2a 20 45 6e 74 65 72 20 61 20 6d 75 74 65 78 ** Enter a mutex
1b4be 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 42 54 on the given BT
1b4bf 72 65 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a ree object..**.*
1b4c0 2a 20 49 66 20 74 68 65 20 6f 62 6a 65 63 74 20 * If the object
1b4c1 69 73 20 6e 6f 74 20 73 68 61 72 61 62 6c 65 2c is not sharable,
1b4c2 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20 69 then no mutex i
1b4c3 73 20 65 76 65 72 20 72 65 71 75 69 72 65 64 0a s ever required.
1b4c4 2a 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 ** and this rout
1b4c5 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 ine is a no-op.
1b4c6 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 The underlying
1b4c7 6d 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72 65 63 mutex is non-rec
1b4c8 75 72 73 69 76 65 2e 0a 2a 2a 20 42 75 74 20 77 ursive..** But w
1b4c9 65 20 6b 65 65 70 20 61 20 72 65 66 65 72 65 6e e keep a referen
1b4ca 63 65 20 63 6f 75 6e 74 20 69 6e 20 42 74 72 65 ce count in Btre
1b4cb 65 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20 e.wantToLock so
1b4cc 74 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 the behavior.**
1b4cd 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 of this interfac
1b4ce 65 20 69 73 20 72 65 63 75 72 73 69 76 65 2e 0a e is recursive..
1b4cf 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 64 **.** To avoid d
1b4d0 65 61 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 70 eadlocks, multip
1b4d1 6c 65 20 42 74 72 65 65 73 20 61 72 65 20 6c 6f le Btrees are lo
1b4d2 63 6b 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 cked in the same
1b4d3 20 6f 72 64 65 72 0a 2a 2a 20 62 79 20 61 6c 6c order.** by all
1b4d4 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1b4d5 74 69 6f 6e 73 2e 20 20 54 68 65 20 70 2d 3e 70 tions. The p->p
1b4d6 4e 65 78 74 20 69 73 20 61 20 6c 69 73 74 20 6f Next is a list o
1b4d7 66 20 6f 74 68 65 72 0a 2a 2a 20 42 74 72 65 65 f other.** Btree
1b4d8 73 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 s belonging to t
1b4d9 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
1b4da 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74 connection as t
1b4db 68 65 20 70 20 42 74 72 65 65 0a 2a 2a 20 77 68 he p Btree.** wh
1b4dc 69 63 68 20 6e 65 65 64 20 74 6f 20 62 65 20 6c ich need to be l
1b4dd 6f 63 6b 65 64 20 61 66 74 65 72 20 70 2e 20 20 ocked after p.
1b4de 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 67 65 74 If we cannot get
1b4df 20 61 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c a lock on.** p,
1b4e0 20 74 68 65 6e 20 66 69 72 73 74 20 75 6e 6c 6f then first unlo
1b4e1 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 ck all of the ot
1b4e2 68 65 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 74 hers on p->pNext
1b4e3 2c 20 74 68 65 6e 20 77 61 69 74 0a 2a 2a 20 66 , then wait.** f
1b4e4 6f 72 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20 62 or the lock to b
1b4e5 65 63 6f 6d 65 20 61 76 61 69 6c 61 62 6c 65 20 ecome available
1b4e6 6f 6e 20 70 2c 20 74 68 65 6e 20 72 65 6c 6f 63 on p, then reloc
1b4e7 6b 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 k all of the.**
1b4e8 73 75 62 73 65 71 75 65 6e 74 20 42 74 72 65 65 subsequent Btree
1b4e9 73 20 74 68 61 74 20 64 65 73 69 72 65 20 61 20 s that desire a
1b4ea 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f lock..*/.SQLITE_
1b4eb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1b4ec 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 ite3BtreeEnter(B
1b4ed 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 72 65 tree *p){. Btre
1b4ee 65 20 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a e *pLater;.. /*
1b4ef 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 Some basic sani
1b4f0 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 ty checking on t
1b4f1 68 65 20 42 74 72 65 65 2e 20 20 54 68 65 20 6c he Btree. The l
1b4f2 69 73 74 20 6f 66 20 42 74 72 65 65 73 0a 20 20 ist of Btrees.
1b4f3 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 ** connected by
1b4f4 70 4e 65 78 74 20 61 6e 64 20 70 50 72 65 76 20 pNext and pPrev
1b4f5 73 68 6f 75 6c 64 20 62 65 20 69 6e 20 73 6f 72 should be in sor
1b4f6 74 65 64 20 6f 72 64 65 72 20 62 79 0a 20 20 2a ted order by. *
1b4f7 2a 20 42 74 72 65 65 2e 70 42 74 20 76 61 6c 75 * Btree.pBt valu
1b4f8 65 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 e. All elements
1b4f9 6f 66 20 74 68 65 20 6c 69 73 74 20 73 68 6f 75 of the list shou
1b4fa 6c 64 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a ld belong to. *
1b4fb 2a 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 * the same conne
1b4fc 63 74 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 72 ction. Only shar
1b4fd 65 64 20 42 74 72 65 65 73 20 61 72 65 20 6f 6e ed Btrees are on
1b4fe 20 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 the list. */.
1b4ff 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 assert( p->pNext
1b500 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d ==0 || p->pNext-
1b501 3e 70 42 74 3e 70 2d 3e 70 42 74 20 29 3b 0a 20 >pBt>p->pBt );.
1b502 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65 assert( p->pPre
1b503 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 v==0 || p->pPrev
1b504 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a ->pBt<p->pBt );.
1b505 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 assert( p->pNe
1b506 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 xt==0 || p->pNex
1b507 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a t->db==p->db );.
1b508 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 assert( p->pPr
1b509 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 ev==0 || p->pPre
1b50a 76 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a v->db==p->db );.
1b50b 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 assert( p->sha
1b50c 72 61 62 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e 65 rable || (p->pNe
1b50d 78 74 3d 3d 30 20 26 26 20 70 2d 3e 70 50 72 65 xt==0 && p->pPre
1b50e 76 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 v==0) );.. /* C
1b50f 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 heck for locking
1b510 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 2a 2f 0a consistency */.
1b511 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f assert( !p->lo
1b512 63 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 cked || p->wantT
1b513 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 oLock>0 );. ass
1b514 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 ert( p->sharable
1b515 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 || p->wantToLoc
1b516 6b 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 65 k==0 );.. /* We
1b517 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 should already
1b518 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 hold a lock on t
1b519 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
1b51a 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 ection */. asse
1b51b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1b51c 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
1b51d 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 55 6e tex) );.. /* Un
1b51e 6c 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 less the databas
1b51f 65 20 69 73 20 73 68 61 72 61 62 6c 65 20 61 6e e is sharable an
1b520 64 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e d unlocked, then
1b521 20 42 74 53 68 61 72 65 64 2e 64 62 0a 20 20 2a BtShared.db. *
1b522 2a 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 * should already
1b523 20 62 65 20 73 65 74 20 63 6f 72 72 65 63 74 6c be set correctl
1b524 79 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 y. */. assert(
1b525 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 (p->locked==0 &&
1b526 20 70 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c 7c p->sharable) ||
1b527 20 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e p->pBt->db==p->
1b528 64 62 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 2d db );.. if( !p-
1b529 3e 73 68 61 72 61 62 6c 65 20 29 20 72 65 74 75 >sharable ) retu
1b52a 72 6e 3b 0a 20 20 70 2d 3e 77 61 6e 74 54 6f 4c rn;. p->wantToL
1b52b 6f 63 6b 2b 2b 3b 0a 20 20 69 66 28 20 70 2d 3e ock++;. if( p->
1b52c 6c 6f 63 6b 65 64 20 29 20 72 65 74 75 72 6e 3b locked ) return;
1b52d 0a 0a 20 20 2f 2a 20 49 6e 20 6d 6f 73 74 20 63 .. /* In most c
1b52e 61 73 65 73 2c 20 77 65 20 73 68 6f 75 6c 64 20 ases, we should
1b52f 62 65 20 61 62 6c 65 20 74 6f 20 61 63 71 75 69 be able to acqui
1b530 72 65 20 74 68 65 20 6c 6f 63 6b 20 77 65 0a 20 re the lock we.
1b531 20 2a 2a 20 77 61 6e 74 20 77 69 74 68 6f 75 74 ** want without
1b532 20 68 61 76 69 6e 67 20 74 6f 20 67 6f 20 74 68 having to go th
1b533 72 6f 75 67 68 74 20 74 68 65 20 61 73 63 65 6e rought the ascen
1b534 64 69 6e 67 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70 ding lock. ** p
1b535 72 6f 63 65 64 75 72 65 20 74 68 61 74 20 66 6f rocedure that fo
1b536 6c 6c 6f 77 73 2e 20 20 4a 75 73 74 20 62 65 20 llows. Just be
1b537 73 75 72 65 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 sure not to bloc
1b538 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 k.. */. if( sq
1b539 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
1b53a 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3d 3d p->pBt->mutex)==
1b53b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1b53c 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d p->pBt->db = p-
1b53d 3e 64 62 3b 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b >db;. p->lock
1b53e 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 ed = 1;. retu
1b53f 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f rn;. }.. /* To
1b540 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2c avoid deadlock,
1b541 20 66 69 72 73 74 20 72 65 6c 65 61 73 65 20 61 first release a
1b542 6c 6c 20 6c 6f 63 6b 73 20 77 69 74 68 20 61 20 ll locks with a
1b543 6c 61 72 67 65 72 0a 20 20 2a 2a 20 42 74 53 68 larger. ** BtSh
1b544 61 72 65 64 20 61 64 64 72 65 73 73 2e 20 20 54 ared address. T
1b545 68 65 6e 20 61 63 71 75 69 72 65 20 6f 75 72 20 hen acquire our
1b546 6c 6f 63 6b 2e 20 20 54 68 65 6e 20 72 65 61 63 lock. Then reac
1b547 71 75 69 72 65 0a 20 20 2a 2a 20 74 68 65 20 6f quire. ** the o
1b548 74 68 65 72 20 42 74 53 68 61 72 65 64 20 6c 6f ther BtShared lo
1b549 63 6b 73 20 74 68 61 74 20 77 65 20 75 73 65 64 cks that we used
1b54a 20 74 6f 20 68 6f 6c 64 20 69 6e 20 61 73 63 65 to hold in asce
1b54b 6e 64 69 6e 67 0a 20 20 2a 2a 20 6f 72 64 65 72 nding. ** order
1b54c 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 61 .. */. for(pLa
1b54d 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c ter=p->pNext; pL
1b54e 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 ater; pLater=pLa
1b54f 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
1b550 20 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d assert( pLater-
1b551 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 20 >sharable );.
1b552 20 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d assert( pLater-
1b553 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 4c 61 >pNext==0 || pLa
1b554 74 65 72 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e ter->pNext->pBt>
1b555 70 4c 61 74 65 72 2d 3e 70 42 74 20 29 3b 0a 20 pLater->pBt );.
1b556 20 20 20 61 73 73 65 72 74 28 20 21 70 4c 61 74 assert( !pLat
1b557 65 72 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c er->locked || pL
1b558 61 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b ater->wantToLock
1b559 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c >0 );. if( pL
1b55a 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a ater->locked ){.
1b55b 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 unlockBtre
1b55c 65 4d 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a eMutex(pLater);.
1b55d 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6c 6f 63 6b }. }. lock
1b55e 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 BtreeMutex(p);.
1b55f 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 for(pLater=p->p
1b560 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c Next; pLater; pL
1b561 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 ater=pLater->pNe
1b562 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 61 xt){. if( pLa
1b563 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 ter->wantToLock
1b564 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 ){. lockBtr
1b565 65 65 4d 75 74 65 78 28 70 4c 61 74 65 72 29 3b eeMutex(pLater);
1b566 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
1b567 0a 2a 2a 20 45 78 69 74 20 74 68 65 20 72 65 63 .** Exit the rec
1b568 75 72 73 69 76 65 20 6d 75 74 65 78 20 6f 6e 20 ursive mutex on
1b569 61 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 a Btree..*/.SQLI
1b56a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1b56b 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1b56c 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 e(Btree *p){. i
1b56d 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 f( p->sharable )
1b56e 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d {. assert( p-
1b56f 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b >wantToLock>0 );
1b570 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f . p->wantToLo
1b571 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d ck--;. if( p-
1b572 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 >wantToLock==0 )
1b573 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 {. unlockBt
1b574 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 reeMutex(p);.
1b575 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 }. }.}..#ifnde
1b576 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 f NDEBUG./*.** R
1b577 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 eturn true if th
1b578 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 e BtShared mutex
1b579 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 is held on the
1b57a 62 74 72 65 65 2c 20 6f 72 20 69 66 20 74 68 65 btree, or if the
1b57b 0a 2a 2a 20 42 2d 54 72 65 65 20 69 73 20 6e 6f .** B-Tree is no
1b57c 74 20 6d 61 72 6b 65 64 20 61 73 20 73 68 61 72 t marked as shar
1b57d 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 able..**.** This
1b57e 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
1b57f 20 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 only from withi
1b580 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 n assert() state
1b581 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ments..*/.SQLITE
1b582 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1b583 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
1b584 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 tex(Btree *p){.
1b585 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 assert( p->shar
1b586 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f able==0 || p->lo
1b587 63 6b 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 77 61 cked==0 || p->wa
1b588 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 ntToLock>0 );.
1b589 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 assert( p->shara
1b58a 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 ble==0 || p->loc
1b58b 6b 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 3d ked==0 || p->db=
1b58c 3d 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 20 =p->pBt->db );.
1b58d 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 assert( p->shar
1b58e 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f able==0 || p->lo
1b58f 63 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 cked==0 || sqlit
1b590 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
1b591 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
1b592 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 assert( p->sha
1b593 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c rable==0 || p->l
1b594 6f 63 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 ocked==0 || sqli
1b595 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1b596 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
1b597 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 73 68 . return (p->sh
1b598 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e arable==0 || p->
1b599 6c 6f 63 6b 65 64 29 3b 0a 7d 0a 23 65 6e 64 69 locked);.}.#endi
1b59a 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 f...#ifndef SQLI
1b59b 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
1b59c 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 ./*.** Enter and
1b59d 20 6c 65 61 76 65 20 61 20 6d 75 74 65 78 20 6f leave a mutex o
1b59e 6e 20 61 20 42 74 72 65 65 20 67 69 76 65 6e 20 n a Btree given
1b59f 61 20 63 75 72 73 6f 72 20 6f 77 6e 65 64 20 62 a cursor owned b
1b5a0 79 20 74 68 61 74 0a 2a 2a 20 42 74 72 65 65 2e y that.** Btree.
1b5a1 20 20 54 68 65 73 65 20 65 6e 74 72 79 20 70 6f These entry po
1b5a2 69 6e 74 73 20 61 72 65 20 75 73 65 64 20 62 79 ints are used by
1b5a3 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f incremental I/O
1b5a4 20 61 6e 64 20 63 61 6e 20 62 65 0a 2a 2a 20 6f and can be.** o
1b5a5 6d 69 74 74 65 64 20 69 66 20 74 68 61 74 20 6d mitted if that m
1b5a6 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20 75 73 65 odule is not use
1b5a7 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1b5a8 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1b5a9 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 3BtreeEnterCurso
1b5aa 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 r(BtCursor *pCur
1b5ab 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 ){. sqlite3Btre
1b5ac 65 45 6e 74 65 72 28 70 43 75 72 2d 3e 70 42 74 eEnter(pCur->pBt
1b5ad 72 65 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 ree);.}.SQLITE_P
1b5ae 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1b5af 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 te3BtreeLeaveCur
1b5b0 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 sor(BtCursor *pC
1b5b1 75 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 ur){. sqlite3Bt
1b5b2 72 65 65 4c 65 61 76 65 28 70 43 75 72 2d 3e 70 reeLeave(pCur->p
1b5b3 42 74 72 65 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 Btree);.}.#endif
1b5b4 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1b5b5 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f 2a INCRBLOB */.../*
1b5b6 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 .** Enter the mu
1b5b7 74 65 78 20 6f 6e 20 65 76 65 72 79 20 42 74 72 tex on every Btr
1b5b8 65 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ee associated wi
1b5b9 74 68 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a th a database.**
1b5ba 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 connection. Th
1b5bb 69 73 20 69 73 20 6e 65 65 64 65 64 20 28 66 6f is is needed (fo
1b5bc 72 20 65 78 61 6d 70 6c 65 29 20 70 72 69 6f 72 r example) prior
1b5bd 20 74 6f 20 70 61 72 73 69 6e 67 0a 2a 2a 20 61 to parsing.** a
1b5be 20 73 74 61 74 65 6d 65 6e 74 20 73 69 6e 63 65 statement since
1b5bf 20 77 65 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 we will be comp
1b5c0 61 72 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 aring table and
1b5c1 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a 20 column names.**
1b5c2 61 67 61 69 6e 73 74 20 61 6c 6c 20 73 63 68 65 against all sche
1b5c3 6d 61 73 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f mas and we do no
1b5c4 74 20 77 61 6e 74 20 74 68 6f 73 65 20 73 63 68 t want those sch
1b5c5 65 6d 61 73 20 62 65 69 6e 67 0a 2a 2a 20 72 65 emas being.** re
1b5c6 73 65 74 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 set out from und
1b5c7 65 72 20 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 er us..**.** The
1b5c8 72 65 20 69 73 20 61 20 63 6f 72 72 65 73 70 6f re is a correspo
1b5c9 6e 64 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c 20 nding leave-all
1b5ca 70 72 6f 63 65 64 75 72 65 73 2e 0a 2a 2a 0a 2a procedures..**.*
1b5cb 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 * Enter the mute
1b5cc 78 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 xes in accending
1b5cd 20 6f 72 64 65 72 20 62 79 20 42 74 53 68 61 72 order by BtShar
1b5ce 65 64 20 70 6f 69 6e 74 65 72 20 61 64 64 72 65 ed pointer addre
1b5cf 73 73 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 74 ss.** to avoid t
1b5d0 68 65 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f he possibility o
1b5d1 66 20 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e 20 f deadlock when
1b5d2 74 77 6f 20 74 68 72 65 61 64 73 20 77 69 74 68 two threads with
1b5d3 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 .** two or more
1b5d4 62 74 72 65 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e btrees in common
1b5d5 20 62 6f 74 68 20 74 72 79 20 74 6f 20 6c 6f 63 both try to loc
1b5d6 6b 20 61 6c 6c 20 74 68 65 69 72 20 62 74 72 65 k all their btre
1b5d7 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 6d es.** at the sam
1b5d8 65 20 69 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 e instant..*/.SQ
1b5d9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1b5da 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e d sqlite3BtreeEn
1b5db 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a terAll(sqlite3 *
1b5dc 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 db){. int i;.
1b5dd 42 74 72 65 65 20 2a 70 3b 0a 20 20 61 73 73 65 Btree *p;. asse
1b5de 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1b5df 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 x_held(db->mutex
1b5e0 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 ) );. for(i=0;
1b5e1 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
1b5e2 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 . p = db->aDb
1b5e3 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 [i].pBt;. if(
1b5e4 20 70 20 29 20 73 71 6c 69 74 65 33 42 74 72 65 p ) sqlite3Btre
1b5e5 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 7d 0a 7d eEnter(p);. }.}
1b5e6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1b5e7 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
1b5e8 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 eLeaveAll(sqlite
1b5e9 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 3 *db){. int i;
1b5ea 0a 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 61 . Btree *p;. a
1b5eb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1b5ec 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 utex_held(db->mu
1b5ed 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d tex) );. for(i=
1b5ee 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
1b5ef 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e +){. p = db->
1b5f0 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 aDb[i].pBt;.
1b5f1 69 66 28 20 70 20 29 20 73 71 6c 69 74 65 33 42 if( p ) sqlite3B
1b5f2 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
1b5f3 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 }.}../*.** Retur
1b5f4 6e 20 74 72 75 65 20 69 66 20 61 20 70 61 72 74 n true if a part
1b5f5 69 63 75 6c 61 72 20 42 74 72 65 65 20 72 65 71 icular Btree req
1b5f6 75 69 72 65 73 20 61 20 6c 6f 63 6b 2e 20 20 52 uires a lock. R
1b5f7 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 0a 2a eturn FALSE if.*
1b5f8 2a 20 6e 6f 20 6c 6f 63 6b 20 69 73 20 65 76 65 * no lock is eve
1b5f9 72 20 72 65 71 75 69 72 65 64 20 73 69 6e 63 65 r required since
1b5fa 20 69 74 20 69 73 20 6e 6f 74 20 73 68 61 72 61 it is not shara
1b5fb 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ble..*/.SQLITE_P
1b5fc 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1b5fd 65 33 42 74 72 65 65 53 68 61 72 61 62 6c 65 28 e3BtreeSharable(
1b5fe 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74 Btree *p){. ret
1b5ff 75 72 6e 20 70 2d 3e 73 68 61 72 61 62 6c 65 3b urn p->sharable;
1b600 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 .}..#ifndef NDEB
1b601 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 UG./*.** Return
1b602 74 72 75 65 20 69 66 20 74 68 65 20 63 75 72 72 true if the curr
1b603 65 6e 74 20 74 68 72 65 61 64 20 68 6f 6c 64 73 ent thread holds
1b604 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
1b605 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 nnection.** mute
1b606 78 20 61 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 x and all requir
1b607 65 64 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 ed BtShared mute
1b608 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 xes..**.** This
1b609 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
1b60a 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 inside assert()
1b60b 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e statements only.
1b60c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1b60d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
1b60e 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 reeHoldsAllMutex
1b60f 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b es(sqlite3 *db){
1b610 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 . int i;. if(
1b611 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 !sqlite3_mutex_h
1b612 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
1b613 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
1b614 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
1b615 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a <db->nDb; i++){.
1b616 20 20 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 Btree *p;.
1b617 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d p = db->aDb[i]
1b618 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20 .pBt;. if( p
1b619 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 && p->sharable &
1b61a 26 0a 20 20 20 20 20 20 20 20 20 28 70 2d 3e 77 &. (p->w
1b61b 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 antToLock==0 ||
1b61c 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 !sqlite3_mutex_h
1b61d 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 eld(p->pBt->mute
1b61e 78 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 x)) ){. ret
1b61f 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 0;. }. }
1b620 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 . return 1;.}.#
1b621 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 endif /* NDEBUG
1b622 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 */..#ifndef NDEB
1b623 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 UG./*.** Return
1b624 74 72 75 65 20 69 66 20 74 68 65 20 63 6f 72 72 true if the corr
1b625 65 63 74 20 6d 75 74 65 78 65 73 20 61 72 65 20 ect mutexes are
1b626 68 65 6c 64 20 66 6f 72 20 61 63 63 65 73 73 69 held for accessi
1b627 6e 67 20 74 68 65 0a 2a 2a 20 64 62 2d 3e 61 44 ng the.** db->aD
1b628 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20 73 b[iDb].pSchema s
1b629 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 6d tructure. The m
1b62a 75 74 65 78 65 73 20 72 65 71 75 69 72 65 64 20 utexes required
1b62b 66 6f 72 20 73 63 68 65 6d 61 0a 2a 2a 20 61 63 for schema.** ac
1b62c 63 65 73 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 cess are:.**.**
1b62d 20 20 28 31 29 20 54 68 65 20 6d 75 74 65 78 20 (1) The mutex
1b62e 6f 6e 20 64 62 0a 2a 2a 20 20 20 28 32 29 20 69 on db.** (2) i
1b62f 66 20 69 44 62 21 3d 31 2c 20 74 68 65 6e 20 74 f iDb!=1, then t
1b630 68 65 20 6d 75 74 65 78 20 6f 6e 20 64 62 2d 3e he mutex on db->
1b631 61 44 62 5b 69 44 62 5d 2e 70 42 74 2e 0a 2a 2a aDb[iDb].pBt..**
1b632 0a 2a 2a 20 49 66 20 70 53 63 68 65 6d 61 20 69 .** If pSchema i
1b633 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e s not NULL, then
1b634 20 69 44 62 20 69 73 20 63 6f 6d 70 75 74 65 64 iDb is computed
1b635 20 66 72 6f 6d 20 70 53 63 68 65 6d 61 20 61 6e from pSchema an
1b636 64 0a 2a 2a 20 64 62 20 75 73 69 6e 67 20 73 71 d.** db using sq
1b637 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 lite3SchemaToInd
1b638 65 78 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ex()..*/.SQLITE_
1b639 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1b63a 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 te3SchemaMutexHe
1b63b 6c 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ld(sqlite3 *db,
1b63c 69 6e 74 20 69 44 62 2c 20 53 63 68 65 6d 61 20 int iDb, Schema
1b63d 2a 70 53 63 68 65 6d 61 29 7b 0a 20 20 42 74 72 *pSchema){. Btr
1b63e 65 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 ee *p;. assert(
1b63f 20 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 db!=0 );. if(
1b640 70 53 63 68 65 6d 61 20 29 20 69 44 62 20 3d 20 pSchema ) iDb =
1b641 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
1b642 6e 64 65 78 28 64 62 2c 20 70 53 63 68 65 6d 61 ndex(db, pSchema
1b643 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 );. assert( iDb
1b644 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e >=0 && iDb<db->n
1b645 44 62 20 29 3b 0a 20 20 69 66 28 20 21 73 71 6c Db );. if( !sql
1b646 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1b647 64 62 2d 3e 6d 75 74 65 78 29 20 29 20 72 65 74 db->mutex) ) ret
1b648 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 69 44 62 urn 0;. if( iDb
1b649 3d 3d 31 20 29 20 72 65 74 75 72 6e 20 31 3b 0a ==1 ) return 1;.
1b64a 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 p = db->aDb[iD
1b64b 62 5d 2e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 b].pBt;. assert
1b64c 28 20 70 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 ( p!=0 );. retu
1b64d 72 6e 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d rn p->sharable==
1b64e 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 0 || p->locked==
1b64f 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 1;.}.#endif /* N
1b650 44 45 42 55 47 20 2a 2f 0a 0a 23 65 6c 73 65 20 DEBUG */..#else
1b651 2f 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 /* SQLITE_THREAD
1b652 53 41 46 45 3e 30 20 61 62 6f 76 65 2e 20 20 53 SAFE>0 above. S
1b653 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
1b654 3d 3d 30 20 62 65 6c 6f 77 20 2a 2f 0a 2f 2a 0a ==0 below */./*.
1b655 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
1b656 20 61 72 65 20 73 70 65 63 69 61 6c 20 63 61 73 are special cas
1b657 65 73 20 66 6f 72 20 6d 75 74 65 78 20 65 6e 74 es for mutex ent
1b658 65 72 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 er routines for
1b659 75 73 65 0a 2a 2a 20 69 6e 20 73 69 6e 67 6c 65 use.** in single
1b65a 20 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 threaded applic
1b65b 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 ations that use
1b65c 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 45 shared cache. E
1b65d 78 63 65 70 74 20 66 6f 72 0a 2a 2a 20 74 68 65 xcept for.** the
1b65e 73 65 20 74 77 6f 20 72 6f 75 74 69 6e 65 73 2c se two routines,
1b65f 20 61 6c 6c 20 6d 75 74 65 78 20 6f 70 65 72 61 all mutex opera
1b660 74 69 6f 6e 73 20 61 72 65 20 6e 6f 2d 6f 70 73 tions are no-ops
1b661 20 69 6e 20 74 68 61 74 20 63 61 73 65 20 61 6e in that case an
1b662 64 0a 2a 2a 20 61 72 65 20 6e 75 6c 6c 20 23 64 d.** are null #d
1b663 65 66 69 6e 65 73 20 69 6e 20 62 74 72 65 65 2e efines in btree.
1b664 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 68 61 72 h..**.** If shar
1b665 65 64 20 63 61 63 68 65 20 69 73 20 64 69 73 61 ed cache is disa
1b666 62 6c 65 64 2c 20 74 68 65 6e 20 61 6c 6c 20 62 bled, then all b
1b667 74 72 65 65 20 6d 75 74 65 78 20 72 6f 75 74 69 tree mutex routi
1b668 6e 65 73 2c 20 69 6e 63 6c 75 64 69 6e 67 0a 2a nes, including.*
1b669 2a 20 74 68 65 20 6f 6e 65 73 20 62 65 6c 6f 77 * the ones below
1b66a 2c 20 61 72 65 20 6e 6f 2d 6f 70 73 20 61 6e 64 , are no-ops and
1b66b 20 61 72 65 20 6e 75 6c 6c 20 23 64 65 66 69 6e are null #defin
1b66c 65 73 20 69 6e 20 62 74 72 65 65 2e 68 2e 0a 2a es in btree.h..*
1b66d 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 /..SQLITE_PRIVAT
1b66e 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
1b66f 72 65 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a reeEnter(Btree *
1b670 70 29 7b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 p){. p->pBt->db
1b671 20 3d 20 70 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49 = p->db;.}.SQLI
1b672 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1b673 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1b674 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 rAll(sqlite3 *db
1b675 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f ){. int i;. fo
1b676 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 r(i=0; i<db->nDb
1b677 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 ; i++){. Btre
1b678 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 e *p = db->aDb[i
1b679 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 ].pBt;. if( p
1b67a 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 42 74 ){. p->pBt
1b67b 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 ->db = p->db;.
1b67c 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 }. }.}.#endif
1b67d 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 54 48 /* if SQLITE_TH
1b67e 52 45 41 44 53 41 46 45 20 2a 2f 0a 23 65 6e 64 READSAFE */.#end
1b67f 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c if /* ifndef SQL
1b680 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
1b681 43 41 43 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a CACHE */../*****
1b682 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
1b683 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a btmutex.c *****
1b684 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b685 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b686 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
1b687 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
1b688 66 69 6c 65 20 62 74 72 65 65 2e 63 20 2a 2a 2a file btree.c ***
1b689 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b68a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b68b 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
1b68c 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2004 April 6.**.
1b68d 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
1b68e 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
1b68f 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
1b690 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
1b691 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
1b692 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
1b693 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
1b694 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
1b695 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
1b696 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1b697 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
1b698 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
1b699 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
1b69a 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1b69b 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
1b69c 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
1b69d 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
1b69e 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
1b69f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
1b6a3 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 his file impleme
1b6a4 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 nts a external (
1b6a5 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 disk-based) data
1b6a6 62 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 base using BTree
1b6a7 73 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 68 65 s..** See the he
1b6a8 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 ader comment on
1b6a9 22 62 74 72 65 65 49 6e 74 2e 68 22 20 66 6f 72 "btreeInt.h" for
1b6aa 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
1b6ab 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c rmation..** Incl
1b6ac 75 64 69 6e 67 20 61 20 64 65 73 63 72 69 70 74 uding a descript
1b6ad 69 6f 6e 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d ion of file form
1b6ae 61 74 20 61 6e 64 20 61 6e 20 6f 76 65 72 76 69 at and an overvi
1b6af 65 77 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e ew of operation.
1b6b0 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 .*/../*.** The h
1b6b1 65 61 64 65 72 20 73 74 72 69 6e 67 20 74 68 61 eader string tha
1b6b2 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65 t appears at the
1b6b3 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 beginning of ev
1b6b4 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 ery.** SQLite da
1b6b5 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 tabase..*/.stati
1b6b6 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61 c const char zMa
1b6b7 67 69 63 48 65 61 64 65 72 5b 5d 20 3d 20 53 51 gicHeader[] = SQ
1b6b8 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 LITE_FILE_HEADER
1b6b9 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 ;../*.** Set thi
1b6ba 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c s global variabl
1b6bb 65 20 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 e to 1 to enable
1b6bc 20 74 72 61 63 69 6e 67 20 75 73 69 6e 67 20 74 tracing using t
1b6bd 68 65 20 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72 he TRACE.** macr
1b6be 6f 2e 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 o..*/.#if 0.int
1b6bf 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 sqlite3BtreeTrac
1b6c0 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f e=1; /* True to
1b6c1 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 enable tracing
1b6c2 2a 2f 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 */.# define TRAC
1b6c3 45 28 58 29 20 20 69 66 28 73 71 6c 69 74 65 33 E(X) if(sqlite3
1b6c4 42 74 72 65 65 54 72 61 63 65 29 7b 70 72 69 6e BtreeTrace){prin
1b6c5 74 66 20 58 3b 66 66 6c 75 73 68 28 73 74 64 6f tf X;fflush(stdo
1b6c6 75 74 29 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65 ut);}.#else.# de
1b6c7 66 69 6e 65 20 54 52 41 43 45 28 58 29 0a 23 65 fine TRACE(X).#e
1b6c8 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 ndif../*.** Extr
1b6c9 61 63 74 20 61 20 32 2d 62 79 74 65 20 62 69 67 act a 2-byte big
1b6ca 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 -endian integer
1b6cb 66 72 6f 6d 20 61 6e 20 61 72 72 61 79 20 6f 66 from an array of
1b6cc 20 75 6e 73 69 67 6e 65 64 20 62 79 74 65 73 2e unsigned bytes.
1b6cd 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 20 76 .** But if the v
1b6ce 61 6c 75 65 20 69 73 20 7a 65 72 6f 2c 20 6d 61 alue is zero, ma
1b6cf 6b 65 20 69 74 20 36 35 35 33 36 2e 0a 2a 2a 0a ke it 65536..**.
1b6d0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1b6d1 69 73 20 75 73 65 64 20 74 6f 20 65 78 74 72 61 is used to extra
1b6d2 63 74 20 74 68 65 20 22 6f 66 66 73 65 74 20 74 ct the "offset t
1b6d3 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 o cell content a
1b6d4 72 65 61 22 20 76 61 6c 75 65 0a 2a 2a 20 66 72 rea" value.** fr
1b6d5 6f 6d 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 om the header of
1b6d6 20 61 20 62 74 72 65 65 20 70 61 67 65 2e 20 20 a btree page.
1b6d7 49 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 If the page size
1b6d8 20 69 73 20 36 35 35 33 36 20 61 6e 64 20 74 68 is 65536 and th
1b6d9 65 20 70 61 67 65 0a 2a 2a 20 69 73 20 65 6d 70 e page.** is emp
1b6da 74 79 2c 20 74 68 65 20 6f 66 66 73 65 74 20 73 ty, the offset s
1b6db 68 6f 75 6c 64 20 62 65 20 36 35 35 33 36 2c 20 hould be 65536,
1b6dc 62 75 74 20 74 68 65 20 32 2d 62 79 74 65 20 76 but the 2-byte v
1b6dd 61 6c 75 65 20 73 74 6f 72 65 73 20 7a 65 72 6f alue stores zero
1b6de 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e ..** This routin
1b6df 65 20 6d 61 6b 65 73 20 74 68 65 20 6e 65 63 65 e makes the nece
1b6e0 73 73 61 72 79 20 61 64 6a 75 73 74 6d 65 6e 74 ssary adjustment
1b6e1 20 74 6f 20 36 35 35 33 36 2e 0a 2a 2f 0a 23 64 to 65536..*/.#d
1b6e2 65 66 69 6e 65 20 67 65 74 32 62 79 74 65 4e 6f efine get2byteNo
1b6e3 74 5a 65 72 6f 28 58 29 20 20 28 28 28 28 28 69 tZero(X) (((((i
1b6e4 6e 74 29 67 65 74 32 62 79 74 65 28 58 29 29 2d nt)get2byte(X))-
1b6e5 31 29 26 30 78 66 66 66 66 29 2b 31 29 0a 0a 23 1)&0xffff)+1)..#
1b6e6 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1b6e7 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
1b6e8 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 /*.** A list of
1b6e9 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 73 BtShared objects
1b6ea 20 74 68 61 74 20 61 72 65 20 65 6c 69 67 69 62 that are eligib
1b6eb 6c 65 20 66 6f 72 20 70 61 72 74 69 63 69 70 61 le for participa
1b6ec 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72 65 tion.** in share
1b6ed 64 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 76 d cache. This v
1b6ee 61 72 69 61 62 6c 65 20 68 61 73 20 66 69 6c 65 ariable has file
1b6ef 20 73 63 6f 70 65 20 64 75 72 69 6e 67 20 6e 6f scope during no
1b6f0 72 6d 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a 20 rmal builds,.**
1b6f1 62 75 74 20 74 68 65 20 74 65 73 74 20 68 61 72 but the test har
1b6f2 6e 65 73 73 20 6e 65 65 64 73 20 74 6f 20 61 63 ness needs to ac
1b6f3 63 65 73 73 20 69 74 20 73 6f 20 77 65 20 6d 61 cess it so we ma
1b6f4 6b 65 20 69 74 20 67 6c 6f 62 61 6c 20 66 6f 72 ke it global for
1b6f5 20 0a 2a 2a 20 74 65 73 74 20 62 75 69 6c 64 73 .** test builds
1b6f6 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 ..**.** Access t
1b6f7 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 o this variable
1b6f8 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 is protected by
1b6f9 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
1b6fa 54 49 43 5f 4d 41 53 54 45 52 2e 0a 2a 2f 0a 23 TIC_MASTER..*/.#
1b6fb 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1b6fc 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
1b6fd 20 42 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 BtShared *SQLIT
1b6fe 45 5f 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 E_WSD sqlite3Sha
1b6ff 72 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 redCacheList = 0
1b700 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 42 ;.#else.static B
1b701 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f tShared *SQLITE_
1b702 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 65 WSD sqlite3Share
1b703 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a dCacheList = 0;.
1b704 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a #endif.#endif /*
1b705 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
1b706 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 RED_CACHE */..#i
1b707 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1b708 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f T_SHARED_CACHE./
1b709 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 *.** Enable or d
1b70a 69 73 61 62 6c 65 20 74 68 65 20 73 68 61 72 65 isable the share
1b70b 64 20 70 61 67 65 72 20 61 6e 64 20 73 63 68 65 d pager and sche
1b70c 6d 61 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a ma features..**.
1b70d 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1b70e 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e has no effect on
1b70f 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 existing databa
1b710 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a se connections..
1b711 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 63 61 ** The shared ca
1b712 63 68 65 20 73 65 74 74 69 6e 67 20 65 66 66 65 che setting effe
1b713 63 74 73 20 6f 6e 6c 79 20 66 75 74 75 72 65 20 cts only future
1b714 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 calls to.** sqli
1b715 74 65 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69 te3_open(), sqli
1b716 74 65 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72 te3_open16(), or
1b717 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 sqlite3_open_v2
1b718 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ()..*/.SQLITE_AP
1b719 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e I int sqlite3_en
1b71a 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 able_shared_cach
1b71b 65 28 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20 e(int enable){.
1b71c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
1b71d 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65 nfig.sharedCache
1b71e 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 Enabled = enable
1b71f 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1b720 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a E_OK;.}.#endif..
1b721 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
1b722 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
1b723 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 E. /*. ** The
1b724 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 79 53 functions queryS
1b725 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
1b726 6f 63 6b 28 29 2c 20 73 65 74 53 68 61 72 65 64 ock(), setShared
1b727 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 CacheTableLock()
1b728 2c 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 ,. ** and clear
1b729 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 AllSharedCacheTa
1b72a 62 6c 65 4c 6f 63 6b 73 28 29 0a 20 20 2a 2a 20 bleLocks(). **
1b72b 6d 61 6e 69 70 75 6c 61 74 65 20 65 6e 74 72 69 manipulate entri
1b72c 65 73 20 69 6e 20 74 68 65 20 42 74 53 68 61 72 es in the BtShar
1b72d 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20 ed.pLock linked
1b72e 6c 69 73 74 20 75 73 65 64 20 74 6f 20 73 74 6f list used to sto
1b72f 72 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 re. ** shared-c
1b730 61 63 68 65 20 74 61 62 6c 65 20 6c 65 76 65 6c ache table level
1b731 20 6c 6f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c locks. If the l
1b732 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c ibrary is compil
1b733 65 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a ed with the. **
1b734 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 65 shared-cache fe
1b735 61 74 75 72 65 20 64 69 73 61 62 6c 65 64 2c 20 ature disabled,
1b736 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e then there is on
1b737 6c 79 20 65 76 65 72 20 6f 6e 65 20 75 73 65 72 ly ever one user
1b738 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 42 74 . ** of each Bt
1b739 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 Shared structure
1b73a 20 61 6e 64 20 73 6f 20 74 68 69 73 20 6c 6f 63 and so this loc
1b73b 6b 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 king is not nece
1b73c 73 73 61 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20 ssary. . ** So
1b73d 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20 define the lock
1b73e 72 65 6c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e related function
1b73f 73 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a s as no-ops.. *
1b740 2f 0a 20 20 23 64 65 66 69 6e 65 20 71 75 65 72 /. #define quer
1b741 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c ySharedCacheTabl
1b742 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c eLock(a,b,c) SQL
1b743 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 ITE_OK. #define
1b744 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 setSharedCacheT
1b745 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 ableLock(a,b,c)
1b746 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 SQLITE_OK. #def
1b747 69 6e 65 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 ine clearAllShar
1b748 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
1b749 73 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 64 s(a). #define d
1b74a 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 65 owngradeAllShare
1b74b 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 dCacheTableLocks
1b74c 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 68 61 (a). #define ha
1b74d 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c sSharedCacheTabl
1b74e 65 4c 6f 63 6b 28 61 2c 62 2c 63 2c 64 29 20 31 eLock(a,b,c,d) 1
1b74f 0a 20 20 23 64 65 66 69 6e 65 20 68 61 73 52 65 . #define hasRe
1b750 61 64 43 6f 6e 66 6c 69 63 74 73 28 61 2c 20 62 adConflicts(a, b
1b751 29 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e ) 0.#endif..#ifn
1b752 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1b753 53 48 41 52 45 44 5f 43 41 43 48 45 0a 0a 23 69 SHARED_CACHE..#i
1b754 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1b755 47 0a 2f 2a 0a 2a 2a 2a 2a 20 54 68 69 73 20 66 G./*.**** This f
1b756 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 unction is only
1b757 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 used as part of
1b758 61 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 an assert() stat
1b759 65 6d 65 6e 74 2e 20 2a 2a 2a 0a 2a 2a 0a 2a 2a ement. ***.**.**
1b75a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 Check to see if
1b75b 20 70 42 74 72 65 65 20 68 6f 6c 64 73 20 74 68 pBtree holds th
1b75c 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 e required locks
1b75d 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 to read or writ
1b75e 65 20 74 6f 20 74 68 65 20 0a 2a 2a 20 74 61 62 e to the .** tab
1b75f 6c 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 le with root pag
1b760 65 20 69 52 6f 6f 74 2e 20 20 20 52 65 74 75 72 e iRoot. Retur
1b761 6e 20 31 20 69 66 20 69 74 20 64 6f 65 73 20 61 n 1 if it does a
1b762 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a nd 0 if not..**.
1b763 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 ** For example,
1b764 77 68 65 6e 20 77 72 69 74 69 6e 67 20 74 6f 20 when writing to
1b765 61 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f a table with roo
1b766 74 2d 70 61 67 65 20 69 52 6f 6f 74 20 76 69 61 t-page iRoot via
1b767 20 0a 2a 2a 20 42 74 72 65 65 20 63 6f 6e 6e 65 .** Btree conne
1b768 63 74 69 6f 6e 20 70 42 74 72 65 65 3a 0a 2a 2a ction pBtree:.**
1b769 0a 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 68 .** assert( h
1b76a 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 asSharedCacheTab
1b76b 6c 65 4c 6f 63 6b 28 70 42 74 72 65 65 2c 20 69 leLock(pBtree, i
1b76c 52 6f 6f 74 2c 20 30 2c 20 57 52 49 54 45 5f 4c Root, 0, WRITE_L
1b76d 4f 43 4b 29 20 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 OCK) );.**.** Wh
1b76e 65 6e 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e en writing to an
1b76f 20 69 6e 64 65 78 20 74 68 61 74 20 72 65 73 69 index that resi
1b770 64 65 73 20 69 6e 20 61 20 73 68 61 72 61 62 6c des in a sharabl
1b771 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 e database, the
1b772 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c .** caller shoul
1b773 64 20 68 61 76 65 20 66 69 72 73 74 20 6f 62 74 d have first obt
1b774 61 69 6e 65 64 20 61 20 6c 6f 63 6b 20 73 70 65 ained a lock spe
1b775 63 69 66 79 69 6e 67 20 74 68 65 20 72 6f 6f 74 cifying the root
1b776 20 70 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65 20 page of.** the
1b777 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 corresponding ta
1b778 62 6c 65 2e 20 54 68 69 73 20 6d 61 6b 65 73 20 ble. This makes
1b779 74 68 69 6e 67 73 20 61 20 62 69 74 20 6d 6f 72 things a bit mor
1b77a 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2c 0a 2a e complicated,.*
1b77b 2a 20 61 73 20 74 68 69 73 20 6d 6f 64 75 6c 65 * as this module
1b77c 20 74 72 65 61 74 73 20 65 61 63 68 20 74 61 62 treats each tab
1b77d 6c 65 20 61 73 20 61 20 73 65 70 61 72 61 74 65 le as a separate
1b77e 20 73 74 72 75 63 74 75 72 65 2e 20 54 6f 20 64 structure. To d
1b77f 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 74 68 65 20 etermine.** the
1b780 74 61 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 table correspond
1b781 69 6e 67 20 74 6f 20 74 68 65 20 69 6e 64 65 78 ing to the index
1b782 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 being written,
1b783 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e this.** function
1b784 20 68 61 73 20 74 6f 20 73 65 61 72 63 68 20 74 has to search t
1b785 68 72 6f 75 67 68 20 74 68 65 20 64 61 74 61 62 hrough the datab
1b786 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a ase schema..**.*
1b787 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 61 20 6c * Instead of a l
1b788 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 ock on the table
1b789 2f 69 6e 64 65 78 20 72 6f 6f 74 65 64 20 61 74 /index rooted at
1b78a 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 74 68 65 page iRoot, the
1b78b 20 63 61 6c 6c 65 72 20 6d 61 79 0a 2a 2a 20 68 caller may.** h
1b78c 6f 6c 64 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b old a write-lock
1b78d 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 20 74 on the schema t
1b78e 61 62 6c 65 20 28 72 6f 6f 74 20 70 61 67 65 20 able (root page
1b78f 31 29 2e 20 54 68 69 73 20 69 73 20 61 6c 73 6f 1). This is also
1b790 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65 2e 0a .** acceptable..
1b791 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 */.static int ha
1b792 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c sSharedCacheTabl
1b793 65 4c 6f 63 6b 28 0a 20 20 42 74 72 65 65 20 2a eLock(. Btree *
1b794 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 pBtree,
1b795 2f 2a 20 48 61 6e 64 6c 65 20 74 68 61 74 20 6d /* Handle that m
1b796 75 73 74 20 68 6f 6c 64 20 6c 6f 63 6b 20 2a 2f ust hold lock */
1b797 0a 20 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 20 . Pgno iRoot,
1b798 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f /* Roo
1b799 74 20 70 61 67 65 20 6f 66 20 62 2d 74 72 65 65 t page of b-tree
1b79a 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 64 65 */. int isInde
1b79b 78 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 x, /*
1b79c 54 72 75 65 20 69 66 20 69 52 6f 6f 74 20 69 73 True if iRoot is
1b79d 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e 20 the root of an
1b79e 69 6e 64 65 78 20 62 2d 74 72 65 65 20 2a 2f 0a index b-tree */.
1b79f 20 20 69 6e 74 20 65 4c 6f 63 6b 54 79 70 65 20 int eLockType
1b7a0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 71 75 /* Requ
1b7a1 69 72 65 64 20 6c 6f 63 6b 20 74 79 70 65 20 28 ired lock type (
1b7a2 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 READ_LOCK or WRI
1b7a3 54 45 5f 4c 4f 43 4b 29 20 2a 2f 0a 29 7b 0a 20 TE_LOCK) */.){.
1b7a4 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
1b7a5 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 42 74 = (Schema *)pBt
1b7a6 72 65 65 2d 3e 70 42 74 2d 3e 70 53 63 68 65 6d ree->pBt->pSchem
1b7a7 61 3b 0a 20 20 50 67 6e 6f 20 69 54 61 62 20 3d a;. Pgno iTab =
1b7a8 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 0;. BtLock *pL
1b7a9 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ock;.. /* If th
1b7aa 69 73 20 64 61 74 61 62 61 73 65 20 69 73 20 6e is database is n
1b7ab 6f 74 20 73 68 61 72 65 61 62 6c 65 2c 20 6f 72 ot shareable, or
1b7ac 20 69 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 if the client i
1b7ad 73 20 72 65 61 64 69 6e 67 0a 20 20 2a 2a 20 61 s reading. ** a
1b7ae 6e 64 20 68 61 73 20 74 68 65 20 72 65 61 64 2d nd has the read-
1b7af 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 uncommitted flag
1b7b0 20 73 65 74 2c 20 74 68 65 6e 20 6e 6f 20 6c 6f set, then no lo
1b7b1 63 6b 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 ck is required.
1b7b2 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 . ** Return tru
1b7b3 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 e immediately..
1b7b4 20 2a 2f 0a 20 20 69 66 28 20 28 70 42 74 72 65 */. if( (pBtre
1b7b5 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 29 0a e->sharable==0).
1b7b6 20 20 20 7c 7c 20 28 65 4c 6f 63 6b 54 79 70 65 || (eLockType
1b7b7 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 26 26 20 28 ==READ_LOCK && (
1b7b8 70 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 pBtree->db->flag
1b7b9 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 s & SQLITE_ReadU
1b7ba 6e 63 6f 6d 6d 69 74 74 65 64 29 29 0a 20 20 29 ncommitted)). )
1b7bb 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a {. return 1;.
1b7bc 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
1b7bd 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 client is readi
1b7be 6e 67 20 20 6f 72 20 77 72 69 74 69 6e 67 20 61 ng or writing a
1b7bf 6e 20 69 6e 64 65 78 20 61 6e 64 20 74 68 65 20 n index and the
1b7c0 73 63 68 65 6d 61 20 69 73 0a 20 20 2a 2a 20 6e schema is. ** n
1b7c1 6f 74 20 6c 6f 61 64 65 64 2c 20 74 68 65 6e 20 ot loaded, then
1b7c2 69 74 20 69 73 20 74 6f 6f 20 64 69 66 66 69 63 it is too diffic
1b7c3 75 6c 74 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 ult to actually
1b7c4 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 0a check to see if.
1b7c5 20 20 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 74 ** the correct
1b7c6 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e locks are held.
1b7c7 20 20 53 6f 20 64 6f 20 6e 6f 74 20 62 6f 74 68 So do not both
1b7c8 65 72 20 2d 20 6a 75 73 74 20 72 65 74 75 72 6e er - just return
1b7c9 20 74 72 75 65 2e 0a 20 20 2a 2a 20 54 68 69 73 true.. ** This
1b7ca 20 63 61 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 case does not c
1b7cb 6f 6d 65 20 75 70 20 76 65 72 79 20 6f 66 74 65 ome up very ofte
1b7cc 6e 20 61 6e 79 68 6f 77 2e 0a 20 20 2a 2f 0a 20 n anyhow.. */.
1b7cd 20 69 66 28 20 69 73 49 6e 64 65 78 20 26 26 20 if( isIndex &&
1b7ce 28 21 70 53 63 68 65 6d 61 20 7c 7c 20 28 70 53 (!pSchema || (pS
1b7cf 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 44 42 5f chema->flags&DB_
1b7d0 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 3d 3d 30 SchemaLoaded)==0
1b7d1 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
1b7d2 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 1;. }.. /* Fig
1b7d3 75 72 65 20 6f 75 74 20 74 68 65 20 72 6f 6f 74 ure out the root
1b7d4 2d 70 61 67 65 20 74 68 61 74 20 74 68 65 20 6c -page that the l
1b7d5 6f 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 68 65 ock should be he
1b7d6 6c 64 20 6f 6e 2e 20 46 6f 72 20 74 61 62 6c 65 ld on. For table
1b7d7 0a 20 20 2a 2a 20 62 2d 74 72 65 65 73 2c 20 74 . ** b-trees, t
1b7d8 68 69 73 20 69 73 20 6a 75 73 74 20 74 68 65 20 his is just the
1b7d9 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 root page of the
1b7da 20 62 2d 74 72 65 65 20 62 65 69 6e 67 20 72 65 b-tree being re
1b7db 61 64 20 6f 72 0a 20 20 2a 2a 20 77 72 69 74 74 ad or. ** writt
1b7dc 65 6e 2e 20 46 6f 72 20 69 6e 64 65 78 20 62 2d en. For index b-
1b7dd 74 72 65 65 73 2c 20 69 74 20 69 73 20 74 68 65 trees, it is the
1b7de 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 root page of th
1b7df 65 20 61 73 73 6f 63 69 61 74 65 64 0a 20 20 2a e associated. *
1b7e0 2a 20 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 69 * table. */. i
1b7e1 66 28 20 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 f( isIndex ){.
1b7e2 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 HashElem *p;.
1b7e3 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 for(p=sqliteH
1b7e4 61 73 68 46 69 72 73 74 28 26 70 53 63 68 65 6d ashFirst(&pSchem
1b7e5 61 2d 3e 69 64 78 48 61 73 68 29 3b 20 70 3b 20 a->idxHash); p;
1b7e6 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 p=sqliteHashNext
1b7e7 28 70 29 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 (p)){. Inde
1b7e8 78 20 2a 70 49 64 78 20 3d 20 28 49 6e 64 65 78 x *pIdx = (Index
1b7e9 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 *)sqliteHashDat
1b7ea 61 28 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 a(p);. if(
1b7eb 70 49 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69 6e 74 pIdx->tnum==(int
1b7ec 29 69 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 )iRoot ){.
1b7ed 20 20 69 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 iTab = pIdx->p
1b7ee 54 61 62 6c 65 2d 3e 74 6e 75 6d 3b 0a 20 20 20 Table->tnum;.
1b7ef 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c }. }. }el
1b7f0 73 65 7b 0a 20 20 20 20 69 54 61 62 20 3d 20 69 se{. iTab = i
1b7f1 52 6f 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Root;. }.. /*
1b7f2 53 65 61 72 63 68 20 66 6f 72 20 74 68 65 20 72 Search for the r
1b7f3 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 45 69 equired lock. Ei
1b7f4 74 68 65 72 20 61 20 77 72 69 74 65 2d 6c 6f 63 ther a write-loc
1b7f5 6b 20 6f 6e 20 72 6f 6f 74 2d 70 61 67 65 20 69 k on root-page i
1b7f6 54 61 62 2c 20 61 20 0a 20 20 2a 2a 20 77 72 69 Tab, a . ** wri
1b7f7 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 te-lock on the s
1b7f8 63 68 65 6d 61 20 74 61 62 6c 65 2c 20 6f 72 20 chema table, or
1b7f9 28 69 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 (if the client i
1b7fa 73 20 72 65 61 64 69 6e 67 29 20 61 0a 20 20 2a s reading) a. *
1b7fb 2a 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 69 * read-lock on i
1b7fc 54 61 62 20 77 69 6c 6c 20 73 75 66 66 69 63 65 Tab will suffice
1b7fd 2e 20 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e . Return 1 if an
1b7fe 79 20 6f 66 20 74 68 65 73 65 20 61 72 65 20 66 y of these are f
1b7ff 6f 75 6e 64 2e 20 20 2a 2f 0a 20 20 66 6f 72 28 ound. */. for(
1b800 70 4c 6f 63 6b 3d 70 42 74 72 65 65 2d 3e 70 42 pLock=pBtree->pB
1b801 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b t->pLock; pLock;
1b802 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e pLock=pLock->pN
1b803 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c ext){. if( pL
1b804 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 42 74 ock->pBtree==pBt
1b805 72 65 65 20 0a 20 20 20 20 20 26 26 20 28 70 4c ree . && (pL
1b806 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 ock->iTable==iTa
1b807 62 20 7c 7c 20 28 70 4c 6f 63 6b 2d 3e 65 4c 6f b || (pLock->eLo
1b808 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 26 ck==WRITE_LOCK &
1b809 26 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d & pLock->iTable=
1b80a 3d 31 29 29 0a 20 20 20 20 20 26 26 20 70 4c 6f =1)). && pLo
1b80b 63 6b 2d 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b ck->eLock>=eLock
1b80c 54 79 70 65 20 0a 20 20 20 20 29 7b 0a 20 20 20 Type . ){.
1b80d 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1b80e 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 61 69 }. }.. /* Fai
1b80f 6c 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 led to find the
1b810 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 2a required lock. *
1b811 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a /. return 0;.}.
1b812 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1b813 5f 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 64 65 _DEBUG */..#ifde
1b814 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f f SQLITE_DEBUG./
1b815 2a 0a 2a 2a 2a 2a 20 54 68 69 73 20 66 75 6e 63 *.**** This func
1b816 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64 tion may be used
1b817 20 61 73 20 70 61 72 74 20 6f 66 20 61 73 73 65 as part of asse
1b818 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 rt() statements
1b819 6f 6e 6c 79 2e 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a only. ****.**.**
1b81a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
1b81b 69 74 20 77 6f 75 6c 64 20 62 65 20 69 6c 6c 65 it would be ille
1b81c 67 61 6c 20 66 6f 72 20 70 42 74 72 65 65 20 74 gal for pBtree t
1b81d 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 74 68 65 o write into the
1b81e 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 .** table or ind
1b81f 65 78 20 72 6f 6f 74 65 64 20 61 74 20 69 52 6f ex rooted at iRo
1b820 6f 74 20 62 65 63 61 75 73 65 20 6f 74 68 65 72 ot because other
1b821 20 73 68 61 72 65 64 20 63 6f 6e 6e 65 63 74 69 shared connecti
1b822 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 69 6d 75 6c ons are.** simul
1b823 74 61 6e 65 6f 75 73 6c 79 20 72 65 61 64 69 6e taneously readin
1b824 67 20 74 68 61 74 20 73 61 6d 65 20 74 61 62 6c g that same tabl
1b825 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a e or index..**.*
1b826 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 * It is illegal
1b827 66 6f 72 20 70 42 74 72 65 65 20 74 6f 20 77 72 for pBtree to wr
1b828 69 74 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 ite if some othe
1b829 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 74 r Btree object t
1b82a 68 61 74 0a 2a 2a 20 73 68 61 72 65 73 20 74 68 hat.** shares th
1b82b 65 20 73 61 6d 65 20 42 74 53 68 61 72 65 64 20 e same BtShared
1b82c 6f 62 6a 65 63 74 20 69 73 20 63 75 72 72 65 6e object is curren
1b82d 74 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 20 77 tly reading or w
1b82e 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 69 52 riting.** the iR
1b82f 6f 6f 74 20 74 61 62 6c 65 2e 20 20 45 78 63 65 oot table. Exce
1b830 70 74 2c 20 69 66 20 74 68 65 20 6f 74 68 65 72 pt, if the other
1b831 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 68 61 Btree object ha
1b832 73 20 74 68 65 0a 2a 2a 20 72 65 61 64 2d 75 6e s the.** read-un
1b833 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 73 committed flag s
1b834 65 74 2c 20 74 68 65 6e 20 69 74 20 69 73 20 4f et, then it is O
1b835 4b 20 66 6f 72 20 74 68 65 20 6f 74 68 65 72 20 K for the other
1b836 6f 62 6a 65 63 74 20 74 6f 0a 2a 2a 20 68 61 76 object to.** hav
1b837 65 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 2e e a read cursor.
1b838 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 .**.** For examp
1b839 6c 65 2c 20 62 65 66 6f 72 65 20 77 72 69 74 69 le, before writi
1b83a 6e 67 20 74 6f 20 61 6e 79 20 70 61 72 74 20 6f ng to any part o
1b83b 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 f the table or i
1b83c 6e 64 65 78 0a 2a 2a 20 72 6f 6f 74 65 64 20 61 ndex.** rooted a
1b83d 74 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 6f 6e t page iRoot, on
1b83e 65 20 73 68 6f 75 6c 64 20 63 61 6c 6c 3a 0a 2a e should call:.*
1b83f 2a 0a 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 *.** assert(
1b840 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 !hasReadConflict
1b841 73 28 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 29 s(pBtree, iRoot)
1b842 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e );.*/.static in
1b843 74 20 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 t hasReadConflic
1b844 74 73 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 ts(Btree *pBtree
1b845 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 29 7b 0a 20 , Pgno iRoot){.
1b846 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 BtCursor *p;.
1b847 66 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 for(p=pBtree->pB
1b848 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 t->pCursor; p; p
1b849 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 =p->pNext){.
1b84a 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d if( p->pgnoRoot=
1b84b 3d 69 52 6f 6f 74 20 0a 20 20 20 20 20 26 26 20 =iRoot . &&
1b84c 70 2d 3e 70 42 74 72 65 65 21 3d 70 42 74 72 65 p->pBtree!=pBtre
1b84d 65 0a 20 20 20 20 20 26 26 20 30 3d 3d 28 70 2d e. && 0==(p-
1b84e 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 >pBtree->db->fla
1b84f 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61 64 gs & SQLITE_Read
1b850 55 6e 63 6f 6d 6d 69 74 74 65 64 29 0a 20 20 20 Uncommitted).
1b851 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1b852 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
1b853 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 return 0;.}.#end
1b854 69 66 20 20 20 20 2f 2a 20 23 69 66 64 65 66 20 if /* #ifdef
1b855 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a SQLITE_DEBUG */.
1b856 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20 ./*.** Query to
1b857 73 65 65 20 69 66 20 42 74 72 65 65 20 68 61 6e see if Btree han
1b858 64 6c 65 20 70 20 6d 61 79 20 6f 62 74 61 69 6e dle p may obtain
1b859 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 a lock of type
1b85a 65 4c 6f 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f eLock .** (READ_
1b85b 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f LOCK or WRITE_LO
1b85c 43 4b 29 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 CK) on the table
1b85d 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 with root-page
1b85e 69 54 61 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 iTab. Return.**
1b85f 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 65 SQLITE_OK if the
1b860 20 6c 6f 63 6b 20 6d 61 79 20 62 65 20 6f 62 74 lock may be obt
1b861 61 69 6e 65 64 20 28 62 79 20 63 61 6c 6c 69 6e ained (by callin
1b862 67 0a 2a 2a 20 73 65 74 53 68 61 72 65 64 43 61 g.** setSharedCa
1b863 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2c cheTableLock()),
1b864 20 6f 72 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 or SQLITE_LOCKE
1b865 44 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 D if not..*/.sta
1b866 74 69 63 20 69 6e 74 20 71 75 65 72 79 53 68 61 tic int querySha
1b867 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
1b868 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f k(Btree *p, Pgno
1b869 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63 6b 29 iTab, u8 eLock)
1b86a 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
1b86b 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 t = p->pBt;. Bt
1b86c 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20 Lock *pIter;..
1b86d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
1b86e 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 treeHoldsMutex(p
1b86f 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 ) );. assert( e
1b870 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 Lock==READ_LOCK
1b871 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f || eLock==WRITE_
1b872 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
1b873 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 ( p->db!=0 );.
1b874 61 73 73 65 72 74 28 20 21 28 70 2d 3e 64 62 2d assert( !(p->db-
1b875 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 >flags&SQLITE_Re
1b876 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 7c 7c adUncommitted)||
1b877 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 eLock==WRITE_LOC
1b878 4b 7c 7c 69 54 61 62 3d 3d 31 20 29 3b 0a 20 20 K||iTab==1 );.
1b879 0a 20 20 2f 2a 20 49 66 20 72 65 71 75 65 73 74 . /* If request
1b87a 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b ing a write-lock
1b87b 2c 20 74 68 65 6e 20 74 68 65 20 42 74 72 65 65 , then the Btree
1b87c 20 6d 75 73 74 20 68 61 76 65 20 61 6e 20 6f 70 must have an op
1b87d 65 6e 20 77 72 69 74 65 0a 20 20 2a 2a 20 74 72 en write. ** tr
1b87e 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 69 ansaction on thi
1b87f 73 20 66 69 6c 65 2e 20 41 6e 64 2c 20 6f 62 76 s file. And, obv
1b880 69 6f 75 73 6c 79 2c 20 66 6f 72 20 74 68 69 73 iously, for this
1b881 20 74 6f 20 62 65 20 73 6f 20 74 68 65 72 65 20 to be so there
1b882 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 61 6e . ** must be an
1b883 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e open write tran
1b884 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 66 saction on the f
1b885 69 6c 65 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f ile itself.. */
1b886 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b . assert( eLock
1b887 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 28 ==READ_LOCK || (
1b888 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 20 p==pBt->pWriter
1b889 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 && p->inTrans==T
1b88a 52 41 4e 53 5f 57 52 49 54 45 29 20 29 3b 0a 20 RANS_WRITE) );.
1b88b 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d assert( eLock==
1b88c 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 42 74 READ_LOCK || pBt
1b88d 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
1b88e 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a =TRANS_WRITE );.
1b88f 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 . /* This rou
1b890 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 tine is a no-op
1b891 69 66 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 if the shared-ca
1b892 63 68 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c che is not enabl
1b893 65 64 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e ed */. if( !p->
1b894 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
1b895 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1b896 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 ;. }.. /* If s
1b897 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 ome other connec
1b898 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 tion is holding
1b899 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
1b89a 6b 2c 20 74 68 65 0a 20 20 2a 2a 20 72 65 71 75 k, the. ** requ
1b89b 65 73 74 65 64 20 6c 6f 63 6b 20 6d 61 79 20 6e ested lock may n
1b89c 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a ot be obtained..
1b89d 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e */. if( pBt->
1b89e 70 57 72 69 74 65 72 21 3d 70 20 26 26 20 28 70 pWriter!=p && (p
1b89f 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 Bt->btsFlags & B
1b8a0 54 53 5f 45 58 43 4c 55 53 49 56 45 29 21 3d 30 TS_EXCLUSIVE)!=0
1b8a1 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 ){. sqlite3C
1b8a2 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 onnectionBlocked
1b8a3 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e 70 57 72 (p->db, pBt->pWr
1b8a4 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20 20 20 72 iter->db);. r
1b8a5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 eturn SQLITE_LOC
1b8a6 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b KED_SHAREDCACHE;
1b8a7 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70 49 74 65 . }.. for(pIte
1b8a8 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 r=pBt->pLock; pI
1b8a9 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 ter; pIter=pIter
1b8aa 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 2f 2a ->pNext){. /*
1b8ab 20 54 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 28 The condition (
1b8ac 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c pIter->eLock!=eL
1b8ad 6f 63 6b 29 20 69 6e 20 74 68 65 20 66 6f 6c 6c ock) in the foll
1b8ae 6f 77 69 6e 67 20 69 66 28 2e 2e 2e 29 20 0a 20 owing if(...) .
1b8af 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 ** statement
1b8b0 69 73 20 61 20 73 69 6d 70 6c 69 66 69 63 61 74 is a simplificat
1b8b1 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 ion of:. **.
1b8b2 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63 6b 3d 3d ** (eLock==
1b8b3 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c 20 70 49 WRITE_LOCK || pI
1b8b4 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 ter->eLock==WRIT
1b8b5 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 2a 2a 0a 20 E_LOCK). **.
1b8b6 20 20 20 2a 2a 20 73 69 6e 63 65 20 77 65 20 6b ** since we k
1b8b7 6e 6f 77 20 74 68 61 74 20 69 66 20 65 4c 6f 63 now that if eLoc
1b8b8 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 2c 20 74 k==WRITE_LOCK, t
1b8b9 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e hen no other con
1b8ba 6e 65 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 6d nection. ** m
1b8bb 61 79 20 68 6f 6c 64 20 61 20 57 52 49 54 45 5f ay hold a WRITE_
1b8bc 4c 4f 43 4b 20 6f 6e 20 61 6e 79 20 74 61 62 6c LOCK on any tabl
1b8bd 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 28 e in this file (
1b8be 73 69 6e 63 65 20 74 68 65 72 65 20 63 61 6e 0a since there can.
1b8bf 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 62 65 20 61 ** only be a
1b8c0 20 73 69 6e 67 6c 65 20 77 72 69 74 65 72 29 2e single writer).
1b8c1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
1b8c2 72 74 28 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b rt( pIter->eLock
1b8c3 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 ==READ_LOCK || p
1b8c4 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 Iter->eLock==WRI
1b8c5 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 TE_LOCK );. a
1b8c6 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 ssert( eLock==RE
1b8c7 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 72 AD_LOCK || pIter
1b8c8 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c 7c 20 70 ->pBtree==p || p
1b8c9 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 Iter->eLock==REA
1b8ca 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 D_LOCK);. if(
1b8cb 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 21 3d pIter->pBtree!=
1b8cc 70 20 26 26 20 70 49 74 65 72 2d 3e 69 54 61 62 p && pIter->iTab
1b8cd 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 49 74 65 le==iTab && pIte
1b8ce 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 20 r->eLock!=eLock
1b8cf 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1b8d0 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 ConnectionBlocke
1b8d1 64 28 70 2d 3e 64 62 2c 20 70 49 74 65 72 2d 3e d(p->db, pIter->
1b8d2 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 20 pBtree->db);.
1b8d3 20 20 20 69 66 28 20 65 4c 6f 63 6b 3d 3d 57 52 if( eLock==WR
1b8d4 49 54 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 ITE_LOCK ){.
1b8d5 20 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 assert( p==p
1b8d6 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 Bt->pWriter );.
1b8d7 20 20 20 20 20 20 20 70 42 74 2d 3e 62 74 73 46 pBt->btsF
1b8d8 6c 61 67 73 20 7c 3d 20 42 54 53 5f 50 45 4e 44 lags |= BTS_PEND
1b8d9 49 4e 47 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ING;. }.
1b8da 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1b8db 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 _LOCKED_SHAREDCA
1b8dc 43 48 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 CHE;. }. }.
1b8dd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1b8de 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 K;.}.#endif /* !
1b8df 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
1b8e0 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 ED_CACHE */..#if
1b8e1 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1b8e2 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a _SHARED_CACHE./*
1b8e3 0a 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f .** Add a lock o
1b8e4 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 n the table with
1b8e5 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c root-page iTabl
1b8e6 65 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d e to the shared-
1b8e7 62 74 72 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 btree used.** by
1b8e8 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e Btree handle p.
1b8e9 20 50 61 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b Parameter eLock
1b8ea 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
1b8eb 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a READ_LOCK or .**
1b8ec 20 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a WRITE_LOCK..**.
1b8ed 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
1b8ee 20 61 73 73 75 6d 65 73 20 74 68 65 20 66 6f 6c assumes the fol
1b8ef 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 lowing:.**.**
1b8f0 28 61 29 20 54 68 65 20 73 70 65 63 69 66 69 65 (a) The specifie
1b8f1 64 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 70 d Btree object p
1b8f2 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f is connected to
1b8f3 20 61 20 73 68 61 72 61 62 6c 65 0a 2a 2a 20 20 a sharable.**
1b8f4 20 20 20 20 20 64 61 74 61 62 61 73 65 20 28 6f database (o
1b8f5 6e 65 20 77 69 74 68 20 74 68 65 20 42 74 53 68 ne with the BtSh
1b8f6 61 72 65 64 2e 73 68 61 72 61 62 6c 65 20 66 6c ared.sharable fl
1b8f7 61 67 20 73 65 74 29 2c 20 61 6e 64 0a 2a 2a 0a ag set), and.**.
1b8f8 2a 2a 20 20 20 28 62 29 20 4e 6f 20 6f 74 68 65 ** (b) No othe
1b8f9 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 20 r Btree objects
1b8fa 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 74 68 61 74 hold a lock that
1b8fb 20 63 6f 6e 66 6c 69 63 74 73 0a 2a 2a 20 20 20 conflicts.**
1b8fc 20 20 20 20 77 69 74 68 20 74 68 65 20 72 65 71 with the req
1b8fd 75 65 73 74 65 64 20 6c 6f 63 6b 20 28 69 2e 65 uested lock (i.e
1b8fe 2e 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 . querySharedCac
1b8ff 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 68 61 heTableLock() ha
1b900 73 0a 2a 2a 20 20 20 20 20 20 20 61 6c 72 65 61 s.** alrea
1b901 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 61 dy been called a
1b902 6e 64 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 nd returned SQLI
1b903 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 TE_OK)..**.** SQ
1b904 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
1b905 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 ned if the lock
1b906 69 73 20 61 64 64 65 64 20 73 75 63 63 65 73 73 is added success
1b907 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f 4e 4f fully. SQLITE_NO
1b908 4d 45 4d 20 0a 2a 2a 20 69 73 20 72 65 74 75 72 MEM .** is retur
1b909 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 ned if a malloc
1b90a 61 74 74 65 6d 70 74 20 66 61 69 6c 73 2e 0a 2a attempt fails..*
1b90b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 /.static int set
1b90c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
1b90d 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 Lock(Btree *p, P
1b90e 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65 gno iTable, u8 e
1b90f 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 Lock){. BtShare
1b910 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
1b911 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b . BtLock *pLock
1b912 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a = 0;. BtLock *
1b913 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 pIter;.. assert
1b914 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
1b915 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 ldsMutex(p) );.
1b916 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d assert( eLock==
1b917 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f READ_LOCK || eLo
1b918 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 ck==WRITE_LOCK )
1b919 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 ;. assert( p->d
1b91a 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 b!=0 );.. /* A
1b91b 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 connection with
1b91c 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 the read-uncommi
1b91d 74 74 65 64 20 66 6c 61 67 20 73 65 74 20 77 69 tted flag set wi
1b91e 6c 6c 20 6e 65 76 65 72 20 74 72 79 20 74 6f 0a ll never try to.
1b91f 20 20 2a 2a 20 6f 62 74 61 69 6e 20 61 20 72 65 ** obtain a re
1b920 61 64 2d 6c 6f 63 6b 20 75 73 69 6e 67 20 74 68 ad-lock using th
1b921 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 is function. The
1b922 20 6f 6e 6c 79 20 72 65 61 64 2d 6c 6f 63 6b 20 only read-lock
1b923 6f 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20 62 79 obtained. ** by
1b924 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e a connection in
1b925 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 read-uncommitte
1b926 64 20 6d 6f 64 65 20 69 73 20 6f 6e 20 74 68 65 d mode is on the
1b927 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 0a sqlite_master .
1b928 20 20 2a 2a 20 74 61 62 6c 65 2c 20 61 6e 64 20 ** table, and
1b929 74 68 61 74 20 6c 6f 63 6b 20 69 73 20 6f 62 74 that lock is obt
1b92a 61 69 6e 65 64 20 69 6e 20 42 74 72 65 65 42 65 ained in BtreeBe
1b92b 67 69 6e 54 72 61 6e 73 28 29 2e 20 20 2a 2f 0a ginTrans(). */.
1b92c 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 2d assert( 0==(p-
1b92d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 >db->flags&SQLIT
1b92e 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 E_ReadUncommitte
1b92f 64 29 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 d) || eLock==WRI
1b930 54 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a TE_LOCK );.. /*
1b931 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 This function s
1b932 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 hould only be ca
1b933 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 61 62 lled on a sharab
1b934 6c 65 20 62 2d 74 72 65 65 20 61 66 74 65 72 20 le b-tree after
1b935 69 74 20 0a 20 20 2a 2a 20 68 61 73 20 62 65 65 it . ** has bee
1b936 6e 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68 61 n determined tha
1b937 74 20 6e 6f 20 6f 74 68 65 72 20 62 2d 74 72 65 t no other b-tre
1b938 65 20 68 6f 6c 64 73 20 61 20 63 6f 6e 66 6c 69 e holds a confli
1b939 63 74 69 6e 67 20 6c 6f 63 6b 2e 20 20 2a 2f 0a cting lock. */.
1b93a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 assert( p->sha
1b93b 72 61 62 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 rable );. asser
1b93c 74 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 t( SQLITE_OK==qu
1b93d 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 erySharedCacheTa
1b93e 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c bleLock(p, iTabl
1b93f 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 e, eLock) );..
1b940 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 68 20 /* First search
1b941 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61 6e 20 the list for an
1b942 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20 6f 6e existing lock on
1b943 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a this table. */.
1b944 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d for(pIter=pBt-
1b945 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 >pLock; pIter; p
1b946 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 Iter=pIter->pNex
1b947 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 t){. if( pIte
1b948 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c r->iTable==iTabl
1b949 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42 74 72 e && pIter->pBtr
1b94a 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 70 ee==p ){. p
1b94b 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a 20 20 Lock = pIter;.
1b94c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1b94d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
1b94e 65 20 61 62 6f 76 65 20 73 65 61 72 63 68 20 64 e above search d
1b94f 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20 42 74 id not find a Bt
1b950 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73 73 6f Lock struct asso
1b951 63 69 61 74 69 6e 67 20 42 74 72 65 65 20 70 0a ciating Btree p.
1b952 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c 65 20 ** with table
1b953 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 74 65 iTable, allocate
1b954 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74 one and link it
1b955 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 2e 0a into the list..
1b956 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c 6f 63 */. if( !pLoc
1b957 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d k ){. pLock =
1b958 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c 69 74 (BtLock *)sqlit
1b959 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a e3MallocZero(siz
1b95a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a 20 20 eof(BtLock));.
1b95b 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a if( !pLock ){.
1b95c 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
1b95d 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
1b95e 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 . pLock->iTab
1b95f 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20 le = iTable;.
1b960 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20 3d pLock->pBtree =
1b961 20 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 p;. pLock->p
1b962 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 Next = pBt->pLoc
1b963 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c 6f 63 k;. pBt->pLoc
1b964 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a k = pLock;. }..
1b965 20 20 2f 2a 20 53 65 74 20 74 68 65 20 42 74 4c /* Set the BtL
1b966 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69 61 62 ock.eLock variab
1b967 6c 65 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 le to the maximu
1b968 6d 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 m of the current
1b969 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64 20 74 lock. ** and t
1b96a 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
1b96b 6b 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66 k. This means if
1b96c 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 61 a write-lock wa
1b96d 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 0a 20 s already held.
1b96e 20 2a 2a 20 61 6e 64 20 61 20 72 65 61 64 2d 6c ** and a read-l
1b96f 6f 63 6b 20 72 65 71 75 65 73 74 65 64 2c 20 77 ock requested, w
1b970 65 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72 65 63 e don't incorrec
1b971 74 6c 79 20 64 6f 77 6e 67 72 61 64 65 20 74 68 tly downgrade th
1b972 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 e lock.. */. a
1b973 73 73 65 72 74 28 20 57 52 49 54 45 5f 4c 4f 43 ssert( WRITE_LOC
1b974 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b 0a 20 K>READ_LOCK );.
1b975 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63 6b if( eLock>pLock
1b976 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 ->eLock ){. p
1b977 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65 4c Lock->eLock = eL
1b978 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 ock;. }.. retu
1b979 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1b97a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 #endif /* !SQLIT
1b97b 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
1b97c 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 CHE */..#ifndef
1b97d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
1b97e 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 52 ED_CACHE./*.** R
1b97f 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 74 elease all the t
1b980 61 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f 63 6b able locks (lock
1b981 73 20 6f 62 74 61 69 6e 65 64 20 76 69 61 20 63 s obtained via c
1b982 61 6c 6c 73 20 74 6f 0a 2a 2a 20 74 68 65 20 73 alls to.** the s
1b983 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 etSharedCacheTab
1b984 6c 65 4c 6f 63 6b 28 29 20 70 72 6f 63 65 64 75 leLock() procedu
1b985 72 65 29 20 68 65 6c 64 20 62 79 20 42 74 72 65 re) held by Btre
1b986 65 20 6f 62 6a 65 63 74 20 70 2e 0a 2a 2a 0a 2a e object p..**.*
1b987 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1b988 61 73 73 75 6d 65 73 20 74 68 61 74 20 42 74 72 assumes that Btr
1b989 65 65 20 70 20 68 61 73 20 61 6e 20 6f 70 65 6e ee p has an open
1b98a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 0a read or write .
1b98b 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ** transaction.
1b98c 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2c 20 If it does not,
1b98d 74 68 65 6e 20 74 68 65 20 42 54 53 5f 50 45 4e then the BTS_PEN
1b98e 44 49 4e 47 20 66 6c 61 67 0a 2a 2a 20 6d 61 79 DING flag.** may
1b98f 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 be incorrectly
1b990 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 cleared..*/.stat
1b991 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41 6c 6c ic void clearAll
1b992 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
1b993 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29 7b Locks(Btree *p){
1b994 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
1b995 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c = p->pBt;. BtL
1b996 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d 20 26 ock **ppIter = &
1b997 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 61 pBt->pLock;.. a
1b998 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
1b999 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 reeHoldsMutex(p)
1b99a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
1b99b 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30 3d 3d >sharable || 0==
1b99c 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61 73 73 *ppIter );. ass
1b99d 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e ert( p->inTrans>
1b99e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 2a 0 );.. while( *
1b99f 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20 42 74 ppIter ){. Bt
1b9a0 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a 70 Lock *pLock = *p
1b9a1 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73 65 72 pIter;. asser
1b9a2 74 28 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 t( (pBt->btsFlag
1b9a3 73 20 26 20 42 54 53 5f 45 58 43 4c 55 53 49 56 s & BTS_EXCLUSIV
1b9a4 45 29 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 E)==0 || pBt->pW
1b9a5 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d 3e 70 42 riter==pLock->pB
1b9a6 74 72 65 65 20 29 3b 0a 20 20 20 20 61 73 73 65 tree );. asse
1b9a7 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 rt( pLock->pBtre
1b9a8 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70 4c 6f 63 e->inTrans>=pLoc
1b9a9 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 k->eLock );.
1b9aa 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 if( pLock->pBtre
1b9ab 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 2a 70 e==p ){. *p
1b9ac 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e 70 pIter = pLock->p
1b9ad 4e 65 78 74 3b 0a 20 20 20 20 20 20 61 73 73 65 Next;. asse
1b9ae 72 74 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c rt( pLock->iTabl
1b9af 65 21 3d 31 20 7c 7c 20 70 4c 6f 63 6b 3d 3d 26 e!=1 || pLock==&
1b9b0 70 2d 3e 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 p->lock );.
1b9b1 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 if( pLock->iTab
1b9b2 6c 65 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 le!=1 ){.
1b9b3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c sqlite3_free(pL
1b9b4 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ock);. }.
1b9b5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
1b9b6 70 49 74 65 72 20 3d 20 26 70 4c 6f 63 6b 2d 3e pIter = &pLock->
1b9b7 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d pNext;. }. }
1b9b8 0a 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 74 .. assert( (pBt
1b9b9 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 ->btsFlags & BTS
1b9ba 5f 50 45 4e 44 49 4e 47 29 3d 3d 30 20 7c 7c 20 _PENDING)==0 ||
1b9bb 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a pBt->pWriter );.
1b9bc 20 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 if( pBt->pWrit
1b9bd 65 72 3d 3d 70 20 29 7b 0a 20 20 20 20 70 42 74 er==p ){. pBt
1b9be 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 ->pWriter = 0;.
1b9bf 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 pBt->btsFlags
1b9c0 20 26 3d 20 7e 28 42 54 53 5f 45 58 43 4c 55 53 &= ~(BTS_EXCLUS
1b9c1 49 56 45 7c 42 54 53 5f 50 45 4e 44 49 4e 47 29 IVE|BTS_PENDING)
1b9c2 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 ;. }else if( pB
1b9c3 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d t->nTransaction=
1b9c4 3d 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 =2 ){. /* Thi
1b9c5 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
1b9c6 6c 6c 65 64 20 77 68 65 6e 20 42 74 72 65 65 20 lled when Btree
1b9c7 70 20 69 73 20 63 6f 6e 63 6c 75 64 69 6e 67 20 p is concluding
1b9c8 69 74 73 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e its . ** tran
1b9c9 73 61 63 74 69 6f 6e 2e 20 49 66 20 74 68 65 72 saction. If ther
1b9ca 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78 69 73 e currently exis
1b9cb 74 73 20 61 20 77 72 69 74 65 72 2c 20 61 6e 64 ts a writer, and
1b9cc 20 70 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a p is not. **
1b9cd 20 74 68 61 74 20 77 72 69 74 65 72 2c 20 74 68 that writer, th
1b9ce 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 en the number of
1b9cf 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 63 locks held by c
1b9d0 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 74 68 65 72 onnections other
1b9d1 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 68 65 . ** than the
1b9d2 20 77 72 69 74 65 72 20 6d 75 73 74 20 62 65 20 writer must be
1b9d3 61 62 6f 75 74 20 74 6f 20 64 72 6f 70 20 74 6f about to drop to
1b9d4 20 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 20 63 zero. In this c
1b9d5 61 73 65 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 ase. ** set t
1b9d6 68 65 20 42 54 53 5f 50 45 4e 44 49 4e 47 20 66 he BTS_PENDING f
1b9d7 6c 61 67 20 74 6f 20 30 2e 0a 20 20 20 20 2a 2a lag to 0.. **
1b9d8 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65 . ** If there
1b9d9 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
1b9da 79 20 61 20 77 72 69 74 65 72 2c 20 74 68 65 6e y a writer, then
1b9db 20 42 54 53 5f 50 45 4e 44 49 4e 47 20 6d 75 73 BTS_PENDING mus
1b9dc 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f t. ** be zero
1b9dd 20 61 6c 72 65 61 64 79 2e 20 53 6f 20 74 68 69 already. So thi
1b9de 73 20 6e 65 78 74 20 6c 69 6e 65 20 69 73 20 68 s next line is h
1b9df 61 72 6d 6c 65 73 73 20 69 6e 20 74 68 61 74 20 armless in that
1b9e0 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 case.. */.
1b9e1 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 pBt->btsFlags &
1b9e2 3d 20 7e 42 54 53 5f 50 45 4e 44 49 4e 47 3b 0a = ~BTS_PENDING;.
1b9e3 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
1b9e4 73 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e 67 s function chang
1b9e5 65 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c 6f 63 es all write-loc
1b9e6 6b 73 20 68 65 6c 64 20 62 79 20 42 74 72 65 65 ks held by Btree
1b9e7 20 70 20 69 6e 74 6f 20 72 65 61 64 2d 6c 6f 63 p into read-loc
1b9e8 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ks..*/.static vo
1b9e9 69 64 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 id downgradeAllS
1b9ea 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
1b9eb 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29 7b 0a ocks(Btree *p){.
1b9ec 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
1b9ed 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 = p->pBt;. if(
1b9ee 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 pBt->pWriter==p
1b9ef 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 ){. BtLock *p
1b9f0 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 Lock;. pBt->p
1b9f1 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Writer = 0;.
1b9f2 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 3d pBt->btsFlags &=
1b9f3 20 7e 28 42 54 53 5f 45 58 43 4c 55 53 49 56 45 ~(BTS_EXCLUSIVE
1b9f4 7c 42 54 53 5f 50 45 4e 44 49 4e 47 29 3b 0a 20 |BTS_PENDING);.
1b9f5 20 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 74 for(pLock=pBt
1b9f6 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 ->pLock; pLock;
1b9f7 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 pLock=pLock->pNe
1b9f8 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 xt){. asser
1b9f9 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3d t( pLock->eLock=
1b9fa 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c =READ_LOCK || pL
1b9fb 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 ock->pBtree==p )
1b9fc 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 ;. pLock->e
1b9fd 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b Lock = READ_LOCK
1b9fe 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 ;. }. }.}..#
1b9ff 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1ba00 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
1ba01 45 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69 E */..static voi
1ba02 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 d releasePage(Me
1ba03 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b 20 20 mPage *pPage);
1ba04 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 /* Forward refer
1ba05 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 2a 2a ence */../*.****
1ba06 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1ba07 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 6f 66 s used inside of
1ba08 20 61 73 73 65 72 74 28 29 20 6f 6e 6c 79 20 2a assert() only *
1ba09 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 56 65 72 69 66 79 ***.**.** Verify
1ba0a 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 that the cursor
1ba0b 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78 holds the mutex
1ba0c 20 6f 6e 20 69 74 73 20 42 74 53 68 61 72 65 64 on its BtShared
1ba0d 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
1ba0e 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 E_DEBUG.static i
1ba0f 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 nt cursorHoldsMu
1ba10 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a 70 29 tex(BtCursor *p)
1ba11 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
1ba12 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
1ba13 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a >pBt->mutex);.}.
1ba14 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 #endif...#ifndef
1ba15 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
1ba16 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 RBLOB./*.** Inva
1ba17 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 lidate the overf
1ba18 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 low page-list ca
1ba19 63 68 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 che for cursor p
1ba1a 43 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a Cur, if any..*/.
1ba1b 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 static void inva
1ba1c 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 lidateOverflowCa
1ba1d 63 68 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 che(BtCursor *pC
1ba1e 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 ur){. assert( c
1ba1f 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
1ba20 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74 pCur) );. sqlit
1ba21 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 61 4f e3_free(pCur->aO
1ba22 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 verflow);. pCur
1ba23 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b ->aOverflow = 0;
1ba24 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 .}../*.** Invali
1ba25 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f date the overflo
1ba26 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 w page-list cach
1ba27 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 6f 72 e for all cursor
1ba28 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20 74 s opened.** on t
1ba29 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 20 he shared btree
1ba2a 73 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a 2a structure pBt..*
1ba2b 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e /.static void in
1ba2c 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 validateAllOverf
1ba2d 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61 72 65 lowCache(BtShare
1ba2e 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 d *pBt){. BtCur
1ba2f 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 sor *p;. assert
1ba30 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1ba31 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
1ba32 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d );. for(p=pBt-
1ba33 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 >pCursor; p; p=p
1ba34 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e ->pNext){. in
1ba35 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 validateOverflow
1ba36 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a Cache(p);. }.}.
1ba37 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
1ba38 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 tion is called b
1ba39 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67 20 efore modifying
1ba3a 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
1ba3b 61 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 69 6e a table.** to in
1ba3c 76 61 6c 69 64 61 74 65 20 61 6e 79 20 69 6e 63 validate any inc
1ba3d 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 74 68 rblob cursors th
1ba3e 61 74 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 at are open on t
1ba3f 68 65 0a 2a 2a 20 72 6f 77 20 6f 72 20 6f 6e 65 he.** row or one
1ba40 20 6f 66 20 74 68 65 20 72 6f 77 73 20 62 65 69 of the rows bei
1ba41 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a ng modified..**.
1ba42 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 69 ** If argument i
1ba43 73 43 6c 65 61 72 54 61 62 6c 65 20 69 73 20 74 sClearTable is t
1ba44 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 65 6e rue, then the en
1ba45 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 tire contents of
1ba46 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 69 73 the.** table is
1ba47 20 61 62 6f 75 74 20 74 6f 20 62 65 20 64 65 6c about to be del
1ba48 65 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 eted. In this ca
1ba49 73 65 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c se invalidate al
1ba4a 6c 20 69 6e 63 72 62 6c 6f 62 0a 2a 2a 20 63 75 l incrblob.** cu
1ba4b 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 61 6e rsors open on an
1ba4c 79 20 72 6f 77 20 77 69 74 68 69 6e 20 74 68 65 y row within the
1ba4d 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 table with root
1ba4e 2d 70 61 67 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a -page pgnoRoot..
1ba4f 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
1ba50 20 69 66 20 61 72 67 75 6d 65 6e 74 20 69 73 43 if argument isC
1ba51 6c 65 61 72 54 61 62 6c 65 20 69 73 20 66 61 6c learTable is fal
1ba52 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 se, then the row
1ba53 20 77 69 74 68 0a 2a 2a 20 72 6f 77 69 64 20 69 with.** rowid i
1ba54 52 6f 77 20 69 73 20 62 65 69 6e 67 20 72 65 70 Row is being rep
1ba55 6c 61 63 65 64 20 6f 72 20 64 65 6c 65 74 65 64 laced or deleted
1ba56 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 69 . In this case i
1ba57 6e 76 61 6c 69 64 61 74 65 0a 2a 2a 20 6f 6e 6c nvalidate.** onl
1ba58 79 20 74 68 6f 73 65 20 69 6e 63 72 62 6c 6f 62 y those incrblob
1ba59 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e cursors open on
1ba5a 20 74 68 61 74 20 73 70 65 63 69 66 69 63 20 72 that specific r
1ba5b 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ow..*/.static vo
1ba5c 69 64 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 id invalidateInc
1ba5d 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 0a 20 20 rblobCursors(.
1ba5e 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 Btree *pBtree,
1ba5f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
1ba60 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 atabase file to
1ba61 63 68 65 63 6b 20 2a 2f 0a 20 20 69 36 34 20 69 check */. i64 i
1ba62 52 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 Row,
1ba63 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 /* The rowid
1ba64 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 63 68 that might be ch
1ba65 61 6e 67 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 anging */. int
1ba66 69 73 43 6c 65 61 72 54 61 62 6c 65 20 20 20 20 isClearTable
1ba67 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
1ba68 6c 6c 20 72 6f 77 73 20 61 72 65 20 62 65 69 6e ll rows are bein
1ba69 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 29 7b 0a g deleted */.){.
1ba6a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 BtCursor *p;.
1ba6b 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
1ba6c 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 pBtree->pBt;.
1ba6d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
1ba6e 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 treeHoldsMutex(p
1ba6f 42 74 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 Btree) );. for(
1ba70 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 p=pBt->pCursor;
1ba71 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a p; p=p->pNext){.
1ba72 20 20 20 20 69 66 28 20 70 2d 3e 69 73 49 6e 63 if( p->isInc
1ba73 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 28 rblobHandle && (
1ba74 69 73 43 6c 65 61 72 54 61 62 6c 65 20 7c 7c 20 isClearTable ||
1ba75 70 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52 p->info.nKey==iR
1ba76 6f 77 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e ow) ){. p->
1ba77 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
1ba78 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 20 INVALID;. }.
1ba79 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 20 20 2f 2a }.}..#else. /*
1ba7a 20 53 74 75 62 20 66 75 6e 63 74 69 6f 6e 73 20 Stub functions
1ba7b 77 68 65 6e 20 49 4e 43 52 42 4c 4f 42 20 69 73 when INCRBLOB is
1ba7c 20 6f 6d 69 74 74 65 64 20 2a 2f 0a 20 20 23 64 omitted */. #d
1ba7d 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 efine invalidate
1ba7e 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29 OverflowCache(x)
1ba7f 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c . #define inval
1ba80 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 idateAllOverflow
1ba81 43 61 63 68 65 28 78 29 0a 20 20 23 64 65 66 69 Cache(x). #defi
1ba82 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 ne invalidateInc
1ba83 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 78 2c 79 rblobCursors(x,y
1ba84 2c 7a 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ,z).#endif /* SQ
1ba85 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
1ba86 4f 42 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 OB */../*.** Set
1ba87 20 62 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65 bit pgno of the
1ba88 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f BtShared.pHasCo
1ba89 6e 74 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 ntent bitvec. Th
1ba8a 69 73 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a is is called .**
1ba8b 20 77 68 65 6e 20 61 20 70 61 67 65 20 74 68 61 when a page tha
1ba8c 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e t previously con
1ba8d 74 61 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f tained data beco
1ba8e 6d 65 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20 mes a free-list
1ba8f 6c 65 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a leaf .** page..*
1ba90 2a 0a 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65 *.** The BtShare
1ba91 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 d.pHasContent bi
1ba92 74 76 65 63 20 65 78 69 73 74 73 20 74 6f 20 77 tvec exists to w
1ba93 6f 72 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62 ork around an ob
1ba94 73 63 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75 scure.** bug cau
1ba95 73 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 72 sed by the inter
1ba96 61 63 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73 action of two us
1ba97 65 66 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61 eful IO optimiza
1ba98 74 69 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e tions surroundin
1ba99 67 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c g.** free-list l
1ba9a 65 61 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a eaf pages:.**.**
1ba9b 20 20 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64 1) When all d
1ba9c 61 74 61 20 69 73 20 64 65 6c 65 74 65 64 20 66 ata is deleted f
1ba9d 72 6f 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74 rom a page and t
1ba9e 68 65 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a he page becomes.
1ba9f 2a 2a 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c ** a free-l
1baa0 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 ist leaf page, t
1baa1 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77 he page is not w
1baa2 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 ritten to the da
1baa3 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28 tabase.** (
1baa4 61 73 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 as free-list lea
1baa5 66 20 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 f pages contain
1baa6 6e 6f 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61 no meaningful da
1baa7 74 61 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a ta). Sometimes.*
1baa8 2a 20 20 20 20 20 20 73 75 63 68 20 61 20 70 61 * such a pa
1baa9 67 65 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a ge is not even j
1baaa 6f 75 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74 ournalled (as it
1baab 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 will not be mod
1baac 69 66 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77 ified,.** w
1baad 68 79 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61 hy bother journa
1baae 6c 6c 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a lling it?)..**.*
1baaf 2a 20 20 20 32 29 20 57 68 65 6e 20 61 20 66 72 * 2) When a fr
1bab0 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 ee-list leaf pag
1bab1 65 20 69 73 20 72 65 75 73 65 64 2c 20 69 74 73 e is reused, its
1bab2 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 content is not
1bab3 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f read.** fro
1bab4 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f m the database o
1bab5 72 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 r written to the
1bab6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77 journal file (w
1bab7 68 79 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 hy should it.**
1bab8 20 20 20 20 20 62 65 2c 20 69 66 20 69 74 20 69 be, if it i
1bab9 73 20 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61 s not at all mea
1baba 6e 69 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a ningful?)..**.**
1babb 20 42 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20 By themselves,
1babc 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 these optimizati
1babd 6f 6e 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e ons work fine an
1babe 64 20 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64 d provide a hand
1babf 79 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 y.** performance
1bac0 20 62 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64 boost to bulk d
1bac1 65 6c 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20 elete or insert
1bac2 6f 70 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65 operations. Howe
1bac3 76 65 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67 ver, if.** a pag
1bac4 65 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 e is moved to th
1bac5 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 e free-list and
1bac6 74 68 65 6e 20 72 65 75 73 65 64 20 77 69 74 68 then reused with
1bac7 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 in the same.** t
1bac8 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72 ransaction, a pr
1bac9 6f 62 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20 oblem comes up.
1baca 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e If the page is n
1bacb 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 ot journalled wh
1bacc 65 6e 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65 en.** it is move
1bacd 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 d to the free-li
1bace 73 74 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73 st and it is als
1bacf 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 o not journalled
1bad0 20 77 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65 when it.** is e
1bad1 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 xtracted from th
1bad2 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 e free-list and
1bad3 72 65 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 reused, then the
1bad4 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a original data.*
1bad5 2a 20 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49 * may be lost. I
1bad6 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 n the event of a
1bad7 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61 rollback, it ma
1bad8 79 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c y not be possibl
1bad9 65 0a 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 e.** to restore
1bada 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 the database to
1badb 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e its original con
1badc 66 69 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a figuration..**.*
1badd 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 * The solution i
1bade 73 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 s the BtShared.p
1badf 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 HasContent bitve
1bae0 63 2e 20 57 68 65 6e 65 76 65 72 20 61 20 70 61 c. Whenever a pa
1bae1 67 65 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 ge is .** moved
1bae2 74 6f 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65 to become a free
1bae3 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c -list leaf page,
1bae4 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
1bae5 6e 67 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74 ng bit is.** set
1bae6 20 69 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20 in the bitvec.
1bae7 57 68 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20 Whenever a leaf
1bae8 70 61 67 65 20 69 73 20 65 78 74 72 61 63 74 65 page is extracte
1bae9 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d d from the free-
1baea 6c 69 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a list,.** optimiz
1baeb 61 74 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73 ation 2 above is
1baec 20 6f 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 omitted if the
1baed 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 corresponding bi
1baee 74 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 t is already.**
1baef 73 65 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e set in BtShared.
1baf0 70 48 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 pHasContent. The
1baf1 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
1baf2 20 62 69 74 76 65 63 20 61 72 65 20 63 6c 65 61 bitvec are clea
1baf3 72 65 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e red.** at the en
1baf4 64 20 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73 d of every trans
1baf5 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 action..*/.stati
1baf6 63 20 69 6e 74 20 62 74 72 65 65 53 65 74 48 61 c int btreeSetHa
1baf7 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 sContent(BtShare
1baf8 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e d *pBt, Pgno pgn
1baf9 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 o){. int rc = S
1bafa 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
1bafb 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e !pBt->pHasConten
1bafc 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 t ){. assert(
1bafd 20 70 67 6e 6f 3c 3d 70 42 74 2d 3e 6e 50 61 67 pgno<=pBt->nPag
1bafe 65 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 48 e );. pBt->pH
1baff 61 73 43 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69 asContent = sqli
1bb00 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 te3BitvecCreate(
1bb01 70 42 74 2d 3e 6e 50 61 67 65 29 3b 0a 20 20 20 pBt->nPage);.
1bb02 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43 if( !pBt->pHasC
1bb03 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 ontent ){.
1bb04 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
1bb05 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 M;. }. }. i
1bb06 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1bb07 20 26 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 && pgno<=sqlite
1bb08 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 74 2d 3BitvecSize(pBt-
1bb09 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b >pHasContent) ){
1bb0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1bb0b 33 42 69 74 76 65 63 53 65 74 28 70 42 74 2d 3e 3BitvecSet(pBt->
1bb0c 70 48 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e pHasContent, pgn
1bb0d 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e o);. }. return
1bb0e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 rc;.}../*.** Qu
1bb0f 65 72 79 20 74 68 65 20 42 74 53 68 61 72 65 64 ery the BtShared
1bb10 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 76 65 63 .pHasContent vec
1bb11 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 tor..**.** This
1bb12 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
1bb13 65 64 20 77 68 65 6e 20 61 20 66 72 65 65 2d 6c ed when a free-l
1bb14 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 69 73 ist leaf page is
1bb15 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 removed from th
1bb16 65 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 66 e.** free-list f
1bb17 6f 72 20 72 65 75 73 65 2e 20 49 74 20 72 65 74 or reuse. It ret
1bb18 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 69 74 urns false if it
1bb19 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 74 72 is safe to retr
1bb1a 69 65 76 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 ieve the.** page
1bb1b 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 from the pager
1bb1c 6c 61 79 65 72 20 77 69 74 68 20 74 68 65 20 27 layer with the '
1bb1d 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67 no-content' flag
1bb1e 20 73 65 74 2e 20 54 72 75 65 20 6f 74 68 65 72 set. True other
1bb1f 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 wise..*/.static
1bb20 69 6e 74 20 62 74 72 65 65 47 65 74 48 61 73 43 int btreeGetHasC
1bb21 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 20 ontent(BtShared
1bb22 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 *pBt, Pgno pgno)
1bb23 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 20 3d 20 {. Bitvec *p =
1bb24 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 pBt->pHasContent
1bb25 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26 26 ;. return (p &&
1bb26 20 28 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69 (pgno>sqlite3Bi
1bb27 74 76 65 63 53 69 7a 65 28 70 29 20 7c 7c 20 73 tvecSize(p) || s
1bb28 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
1bb29 28 70 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a (p, pgno)));.}..
1bb2a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 28 64 65 73 /*.** Clear (des
1bb2b 74 72 6f 79 29 20 74 68 65 20 42 74 53 68 61 72 troy) the BtShar
1bb2c 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 ed.pHasContent b
1bb2d 69 74 76 65 63 2e 20 54 68 69 73 20 73 68 6f 75 itvec. This shou
1bb2e 6c 64 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 ld be.** invoked
1bb2f 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 at the conclusi
1bb30 6f 6e 20 6f 66 20 65 61 63 68 20 77 72 69 74 65 on of each write
1bb31 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f -transaction..*/
1bb32 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 .static void btr
1bb33 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e eeClearHasConten
1bb34 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 t(BtShared *pBt)
1bb35 7b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 {. sqlite3Bitve
1bb36 63 44 65 73 74 72 6f 79 28 70 42 74 2d 3e 70 48 cDestroy(pBt->pH
1bb37 61 73 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 42 asContent);. pB
1bb38 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d t->pHasContent =
1bb39 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 0;.}../*.** Sav
1bb3a 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 e the current cu
1bb3b 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e rsor position in
1bb3c 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20 42 the variables B
1bb3d 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a tCursor.nKey .**
1bb3e 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70 4b and BtCursor.pK
1bb3f 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27 73 ey. The cursor's
1bb40 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f state is set to
1bb41 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
1bb42 45 45 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 EEK..**.** The c
1bb43 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 aller must ensur
1bb44 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f e that the curso
1bb45 72 20 69 73 20 76 61 6c 69 64 20 28 68 61 73 20 r is valid (has
1bb46 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
1bb47 41 4c 49 44 29 0a 2a 2a 20 70 72 69 6f 72 20 74 ALID).** prior t
1bb48 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 o calling this r
1bb49 6f 75 74 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61 outine. .*/.sta
1bb4a 74 69 63 20 69 6e 74 20 73 61 76 65 43 75 72 73 tic int saveCurs
1bb4b 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 orPosition(BtCur
1bb4c 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e sor *pCur){. in
1bb4d 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
1bb4e 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 CURSOR_VALID==p
1bb4f 43 75 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 Cur->eState );.
1bb50 20 61 73 73 65 72 74 28 20 30 3d 3d 70 43 75 72 assert( 0==pCur
1bb51 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 ->pKey );. asse
1bb52 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
1bb53 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 utex(pCur) );..
1bb54 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1bb55 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 eeKeySize(pCur,
1bb56 26 70 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 &pCur->nKey);.
1bb57 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1bb58 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65 79 TE_OK ); /* Key
1bb59 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 Size() cannot fa
1bb5a 69 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 il */.. /* If t
1bb5b 68 69 73 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 his is an intKey
1bb5c 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 table, then the
1bb5d 20 61 62 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 above call to B
1bb5e 74 72 65 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 treeKeySize().
1bb5f 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20 69 6e ** stores the in
1bb60 74 65 67 65 72 20 6b 65 79 20 69 6e 20 70 43 75 teger key in pCu
1bb61 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 r->nKey. In this
1bb62 20 63 61 73 65 20 74 68 69 73 20 76 61 6c 75 65 case this value
1bb63 20 69 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 is. ** all tha
1bb64 74 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 4f t is required. O
1bb65 74 68 65 72 77 69 73 65 2c 20 69 66 20 70 43 75 therwise, if pCu
1bb66 72 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e r is not open on
1bb67 20 61 6e 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 an intKey. **
1bb68 74 61 62 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c table, then mall
1bb69 6f 63 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 oc space for and
1bb6a 20 73 74 6f 72 65 20 74 68 65 20 70 43 75 72 2d store the pCur-
1bb6b 3e 6e 4b 65 79 20 62 79 74 65 73 20 6f 66 20 6b >nKey bytes of k
1bb6c 65 79 20 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 ey . ** data..
1bb6d 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 70 43 75 */. if( 0==pCu
1bb6e 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e r->apPage[0]->in
1bb6f 74 4b 65 79 20 29 7b 0a 20 20 20 20 76 6f 69 64 tKey ){. void
1bb70 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 *pKey = sqlite3
1bb71 4d 61 6c 6c 6f 63 28 20 28 69 6e 74 29 70 43 75 Malloc( (int)pCu
1bb72 72 2d 3e 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 r->nKey );. i
1bb73 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 f( pKey ){.
1bb74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1bb75 65 65 4b 65 79 28 70 43 75 72 2c 20 30 2c 20 28 eeKey(pCur, 0, (
1bb76 69 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 int)pCur->nKey,
1bb77 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 66 28 pKey);. if(
1bb78 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1bb79 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e {. pCur->
1bb7a 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 pKey = pKey;.
1bb7b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1bb7c 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
1bb7d 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Key);. }.
1bb7e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
1bb7f 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
1bb80 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 ;. }. }. as
1bb81 73 65 72 74 28 20 21 70 43 75 72 2d 3e 61 70 50 sert( !pCur->apP
1bb82 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c age[0]->intKey |
1bb83 7c 20 21 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b | !pCur->pKey );
1bb84 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
1bb85 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 TE_OK ){. int
1bb86 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b i;. for(i=0;
1bb87 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b i<=pCur->iPage;
1bb88 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c i++){. rel
1bb89 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 easePage(pCur->a
1bb8a 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 pPage[i]);.
1bb8b 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d pCur->apPage[i]
1bb8c 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
1bb8d 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 pCur->iPage = -1
1bb8e 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 ;. pCur->eSta
1bb8f 74 65 20 3d 20 43 55 52 53 4f 52 5f 52 45 51 55 te = CURSOR_REQU
1bb90 49 52 45 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 IRESEEK;. }..
1bb91 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c invalidateOverfl
1bb92 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 owCache(pCur);.
1bb93 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1bb94 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 70 6f *.** Save the po
1bb95 73 69 74 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 sitions of all c
1bb96 75 72 73 6f 72 73 20 28 65 78 63 65 70 74 20 70 ursors (except p
1bb97 45 78 63 65 70 74 29 20 74 68 61 74 20 61 72 65 Except) that are
1bb98 20 6f 70 65 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 open on.** the
1bb99 74 61 62 6c 65 20 20 77 69 74 68 20 72 6f 6f 74 table with root
1bb9a 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55 73 75 -page iRoot. Usu
1bb9b 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 ally, this is ca
1bb9c 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f 72 65 lled just before
1bb9d 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78 63 65 cursor.** pExce
1bb9e 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 6d 6f pt is used to mo
1bb9f 64 69 66 79 20 74 68 65 20 74 61 62 6c 65 20 28 dify the table (
1bba0 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72 BtreeDelete() or
1bba1 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 29 2e BtreeInsert()).
1bba2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
1bba3 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 aveAllCursors(Bt
1bba4 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e Shared *pBt, Pgn
1bba5 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72 73 6f o iRoot, BtCurso
1bba6 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20 20 42 r *pExcept){. B
1bba7 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 tCursor *p;. as
1bba8 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1bba9 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
1bbaa 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
1bbab 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c 7c 20 ( pExcept==0 ||
1bbac 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d 70 42 pExcept->pBt==pB
1bbad 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 t );. for(p=pBt
1bbae 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d ->pCursor; p; p=
1bbaf 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 p->pNext){. i
1bbb0 66 28 20 70 21 3d 70 45 78 63 65 70 74 20 26 26 f( p!=pExcept &&
1bbb1 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d (0==iRoot || p-
1bbb2 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 >pgnoRoot==iRoot
1bbb3 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 70 2d ) && . p-
1bbb4 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
1bbb5 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 69 VALID ){. i
1bbb6 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75 72 73 nt rc = saveCurs
1bbb7 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b 0a 20 orPosition(p);.
1bbb8 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f if( SQLITE_
1bbb9 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 OK!=rc ){.
1bbba 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1bbbb 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
1bbbc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1bbbd 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 K;.}../*.** Clea
1bbbe 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 r the current cu
1bbbf 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a rsor position..*
1bbc0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1bbc1 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
1bbc2 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 eeClearCursor(Bt
1bbc3 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
1bbc4 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
1bbc5 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
1bbc6 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
1bbc7 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 e(pCur->pKey);.
1bbc8 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b pCur->pKey = 0;
1bbc9 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 . pCur->eState
1bbca 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
1bbcb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 ;.}../*.** In th
1bbcc 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 42 74 is version of Bt
1bbcd 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b 65 79 20 reeMoveto, pKey
1bbce 69 73 20 61 20 70 61 63 6b 65 64 20 69 6e 64 65 is a packed inde
1bbcf 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 75 63 68 x record.** such
1bbd0 20 61 73 20 69 73 20 67 65 6e 65 72 61 74 65 64 as is generated
1bbd1 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 by the OP_MakeR
1bbd2 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20 20 55 ecord opcode. U
1bbd3 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 72 65 63 npack the.** rec
1bbd4 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63 61 6c ord and then cal
1bbd5 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 l BtreeMovetoUnp
1bbd6 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20 74 68 acked() to do th
1bbd7 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 73 74 61 74 69 e work..*/.stati
1bbd8 63 20 69 6e 74 20 62 74 72 65 65 4d 6f 76 65 74 c int btreeMovet
1bbd9 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 o(. BtCursor *p
1bbda 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 72 73 Cur, /* Curs
1bbdb 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 62 or open on the b
1bbdc 74 72 65 65 20 74 6f 20 62 65 20 73 65 61 72 63 tree to be searc
1bbdd 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 hed */. const v
1bbde 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 oid *pKey, /*
1bbdf 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20 74 68 Packed key if th
1bbe0 65 20 62 74 72 65 65 20 69 73 20 61 6e 20 69 6e e btree is an in
1bbe1 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 dex */. i64 nKe
1bbe2 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 y, /*
1bbe3 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20 Integer key for
1bbe4 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20 6f 66 tables. Size of
1bbe5 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69 63 65 pKey for indice
1bbe6 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 2c s */. int bias,
1bbe7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 /* Bi
1bbe8 61 73 20 73 65 61 72 63 68 20 74 6f 20 74 68 65 as search to the
1bbe9 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 high end */. i
1bbea 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 20 nt *pRes
1bbeb 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72 /* Write sear
1bbec 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 ch results here
1bbed 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 */.){. int rc;
1bbee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bbef 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63 6f 64 /* Status cod
1bbf0 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 e */. UnpackedR
1bbf1 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 20 ecord *pIdxKey;
1bbf2 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e /* Unpacked in
1bbf3 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 63 68 61 dex key */. cha
1bbf4 72 20 61 53 70 61 63 65 5b 31 35 30 5d 3b 20 20 r aSpace[150];
1bbf5 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 /* Temp
1bbf6 73 70 61 63 65 20 66 6f 72 20 70 49 64 78 4b 65 space for pIdxKe
1bbf7 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61 20 6d y - to avoid a m
1bbf8 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 alloc */. char
1bbf9 2a 70 46 72 65 65 20 3d 20 30 3b 0a 0a 20 20 69 *pFree = 0;.. i
1bbfa 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 f( pKey ){. a
1bbfb 73 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 ssert( nKey==(i6
1bbfc 34 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 4)(int)nKey );.
1bbfd 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c pIdxKey = sql
1bbfe 69 74 65 33 56 64 62 65 41 6c 6c 6f 63 55 6e 70 ite3VdbeAllocUnp
1bbff 61 63 6b 65 64 52 65 63 6f 72 64 28 0a 20 20 20 ackedRecord(.
1bc00 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 pCur->pKeyI
1bc01 6e 66 6f 2c 20 61 53 70 61 63 65 2c 20 73 69 7a nfo, aSpace, siz
1bc02 65 6f 66 28 61 53 70 61 63 65 29 2c 20 26 70 46 eof(aSpace), &pF
1bc03 72 65 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 ree. );. i
1bc04 66 28 20 70 49 64 78 4b 65 79 3d 3d 30 20 29 20 f( pIdxKey==0 )
1bc05 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
1bc06 4d 45 4d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 MEM;. sqlite3
1bc07 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b VdbeRecordUnpack
1bc08 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2c (pCur->pKeyInfo,
1bc09 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70 4b 65 79 (int)nKey, pKey
1bc0a 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 65 , pIdxKey);. }e
1bc0b 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b 65 79 lse{. pIdxKey
1bc0c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 20 3d = 0;. }. rc =
1bc0d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
1bc0e 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 75 72 etoUnpacked(pCur
1bc0f 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 79 2c , pIdxKey, nKey,
1bc10 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a 20 20 bias, pRes);.
1bc11 69 66 28 20 70 46 72 65 65 20 29 7b 0a 20 20 20 if( pFree ){.
1bc12 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
1bc13 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64 Cur->pKeyInfo->d
1bc14 62 2c 20 70 46 72 65 65 29 3b 0a 20 20 7d 0a 20 b, pFree);. }.
1bc15 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1bc16 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65 *.** Restore the
1bc17 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 70 cursor to the p
1bc18 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20 69 osition it was i
1bc19 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 74 n (or as close t
1bc1a 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a 2a o as possible).*
1bc1b 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73 6f * when saveCurso
1bc1c 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73 20 rPosition() was
1bc1d 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68 61 called. Note tha
1bc1e 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c 65 t this call dele
1bc1f 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76 65 tes the .** save
1bc20 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f 20 d position info
1bc21 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 43 75 stored by saveCu
1bc22 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c 20 rsorPosition(),
1bc23 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a so there can be.
1bc24 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 65 ** at most one e
1bc25 66 66 65 63 74 69 76 65 20 72 65 73 74 6f 72 65 ffective restore
1bc26 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 CursorPosition()
1bc27 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63 68 call after each
1bc28 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72 50 .** saveCursorP
1bc29 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73 74 osition()..*/.st
1bc2a 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 52 65 atic int btreeRe
1bc2b 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
1bc2c 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ion(BtCursor *pC
1bc2d 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ur){. int rc;.
1bc2e 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
1bc2f 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
1bc30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
1bc31 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f r->eState>=CURSO
1bc32 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b R_REQUIRESEEK );
1bc33 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 . if( pCur->eSt
1bc34 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c ate==CURSOR_FAUL
1bc35 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 T ){. return
1bc36 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a pCur->skipNext;.
1bc37 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 }. pCur->eSta
1bc38 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 te = CURSOR_INVA
1bc39 4c 49 44 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 LID;. rc = btre
1bc3a 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 43 eMoveto(pCur, pC
1bc3b 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d 3e ur->pKey, pCur->
1bc3c 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d 3e nKey, 0, &pCur->
1bc3d 73 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66 28 skipNext);. if(
1bc3e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1bc3f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 {. sqlite3_fr
1bc40 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a ee(pCur->pKey);.
1bc41 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d pCur->pKey =
1bc42 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 0;. assert(
1bc43 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
1bc44 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 RSOR_VALID || pC
1bc45 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
1bc46 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 OR_INVALID );.
1bc47 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1bc48 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f 72 ..#define restor
1bc49 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
1bc4a 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74 p) \. (p->eStat
1bc4b 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 e>=CURSOR_REQUIR
1bc4c 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20 ESEEK ? \.
1bc4d 20 20 20 62 74 72 65 65 52 65 73 74 6f 72 65 43 btreeRestoreC
1bc4e 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 ursorPosition(p)
1bc4f 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53 51 : \. SQ
1bc50 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20 LITE_OK)../*.**
1bc51 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 Determine whethe
1bc52 72 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 73 6f r or not a curso
1bc53 72 20 68 61 73 20 6d 6f 76 65 64 20 66 72 6f 6d r has moved from
1bc54 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 the position it
1bc55 0a 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 6c 61 .** was last pla
1bc56 63 65 64 20 61 74 2e 20 20 43 75 72 73 6f 72 73 ced at. Cursors
1bc57 20 63 61 6e 20 6d 6f 76 65 20 77 68 65 6e 20 74 can move when t
1bc58 68 65 20 72 6f 77 20 74 68 65 79 20 61 72 65 20 he row they are
1bc59 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 69 pointing.** at i
1bc5a 73 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66 72 s deleted out fr
1bc5b 6f 6d 20 75 6e 64 65 72 20 74 68 65 6d 2e 0a 2a om under them..*
1bc5c 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1bc5d 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 e returns an err
1bc5e 6f 72 20 63 6f 64 65 20 69 66 20 73 6f 6d 65 74 or code if somet
1bc5f 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e hing goes wrong.
1bc60 20 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 The.** integer
1bc61 20 2a 70 48 61 73 4d 6f 76 65 64 20 69 73 20 73 *pHasMoved is s
1bc62 65 74 20 74 6f 20 6f 6e 65 20 69 66 20 74 68 65 et to one if the
1bc63 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 cursor has move
1bc64 64 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a d and 0 if not..
1bc65 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1bc66 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1bc67 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 eeCursorHasMoved
1bc68 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
1bc69 20 69 6e 74 20 2a 70 48 61 73 4d 6f 76 65 64 29 int *pHasMoved)
1bc6a 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 {. int rc;.. r
1bc6b 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f c = restoreCurso
1bc6c 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b rPosition(pCur);
1bc6d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
1bc6e 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b *pHasMoved = 1;
1bc6f 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
1bc70 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e }. if( pCur->
1bc71 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 eState!=CURSOR_V
1bc72 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 73 6b ALID || pCur->sk
1bc73 69 70 4e 65 78 74 21 3d 30 20 29 7b 0a 20 20 20 ipNext!=0 ){.
1bc74 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b *pHasMoved = 1;
1bc75 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 . }else{. *p
1bc76 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a 20 20 HasMoved = 0;.
1bc77 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
1bc78 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 E_OK;.}..#ifndef
1bc79 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1bc7a 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 69 OVACUUM./*.** Gi
1bc7b 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 65 ven a page numbe
1bc7c 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20 64 r of a regular d
1bc7d 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 72 65 atabase page, re
1bc7e 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a turn the page.**
1bc7f 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 number for the
1bc80 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 pointer-map page
1bc81 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
1bc82 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 he entry for the
1bc83 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20 6e .** input page n
1bc84 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 umber..**.** Ret
1bc85 75 72 6e 20 30 20 28 6e 6f 74 20 61 20 76 61 6c urn 0 (not a val
1bc86 69 64 20 70 61 67 65 29 20 66 6f 72 20 70 67 6e id page) for pgn
1bc87 6f 3d 3d 31 20 73 69 6e 63 65 20 74 68 65 72 65 o==1 since there
1bc88 20 69 73 0a 2a 2a 20 6e 6f 20 70 6f 69 6e 74 65 is.** no pointe
1bc89 72 20 6d 61 70 20 61 73 73 6f 63 69 61 74 65 64 r map associated
1bc8a 20 77 69 74 68 20 70 61 67 65 20 31 2e 20 20 54 with page 1. T
1bc8b 68 65 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65 he integrity_che
1bc8c 63 6b 20 6c 6f 67 69 63 0a 2a 2a 20 72 65 71 75 ck logic.** requ
1bc8d 69 72 65 73 20 74 68 61 74 20 70 74 72 6d 61 70 ires that ptrmap
1bc8e 50 61 67 65 6e 6f 28 2a 2c 31 29 21 3d 31 2e 0a Pageno(*,1)!=1..
1bc8f 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 */.static Pgno p
1bc90 74 72 6d 61 70 50 61 67 65 6e 6f 28 42 74 53 68 trmapPageno(BtSh
1bc91 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 ared *pBt, Pgno
1bc92 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 6e 50 61 pgno){. int nPa
1bc93 67 65 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 gesPerMapPage;.
1bc94 20 50 67 6e 6f 20 69 50 74 72 4d 61 70 2c 20 72 Pgno iPtrMap, r
1bc95 65 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 et;. assert( sq
1bc96 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1bc97 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
1bc98 20 20 69 66 28 20 70 67 6e 6f 3c 32 20 29 20 72 if( pgno<2 ) r
1bc99 65 74 75 72 6e 20 30 3b 0a 20 20 6e 50 61 67 65 eturn 0;. nPage
1bc9a 73 50 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70 sPerMapPage = (p
1bc9b 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 Bt->usableSize/5
1bc9c 29 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d )+1;. iPtrMap =
1bc9d 20 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 (pgno-2)/nPages
1bc9e 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 PerMapPage;. re
1bc9f 74 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 t = (iPtrMap*nPa
1bca0 67 65 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b gesPerMapPage) +
1bca1 20 32 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 2; . if( ret==
1bca2 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
1bca3 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 E(pBt) ){. re
1bca4 74 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 t++;. }. retur
1bca5 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n ret;.}../*.**
1bca6 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 Write an entry i
1bca7 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 nto the pointer
1bca8 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 map..**.** This
1bca9 72 6f 75 74 69 6e 65 20 75 70 64 61 74 65 73 20 routine updates
1bcaa 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 the pointer map
1bcab 65 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e entry for page n
1bcac 75 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 umber 'key'.** s
1bcad 6f 20 74 68 61 74 20 69 74 20 6d 61 70 73 20 74 o that it maps t
1bcae 6f 20 74 79 70 65 20 27 65 54 79 70 65 27 20 61 o type 'eType' a
1bcaf 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e nd parent page n
1bcb0 75 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a umber 'pgno'..**
1bcb1 0a 2a 2a 20 49 66 20 2a 70 52 43 20 69 73 20 69 .** If *pRC is i
1bcb2 6e 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 nitially non-zer
1bcb3 6f 20 28 6e 6f 6e 2d 53 51 4c 49 54 45 5f 4f 4b o (non-SQLITE_OK
1bcb4 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 ) then this rout
1bcb5 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f ine is.** a no-o
1bcb6 70 2e 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20 p. If an error
1bcb7 6f 63 63 75 72 73 2c 20 74 68 65 20 61 70 70 72 occurs, the appr
1bcb8 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f opriate error co
1bcb9 64 65 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a de is written.**
1bcba 20 69 6e 74 6f 20 2a 70 52 43 2e 0a 2a 2f 0a 73 into *pRC..*/.s
1bcbb 74 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 tatic void ptrma
1bcbc 70 50 75 74 28 42 74 53 68 61 72 65 64 20 2a 70 pPut(BtShared *p
1bcbd 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 Bt, Pgno key, u8
1bcbe 20 65 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 eType, Pgno par
1bcbf 65 6e 74 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a ent, int *pRC){.
1bcc0 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
1bcc1 65 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 e; /* The point
1bcc2 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 er map page */.
1bcc3 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 u8 *pPtrmap;
1bcc4 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 /* The pointe
1bcc5 72 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a 20 20 r map data */.
1bcc6 50 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20 20 20 Pgno iPtrmap;
1bcc7 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 /* The pointer
1bcc8 20 6d 61 70 20 70 61 67 65 20 6e 75 6d 62 65 72 map page number
1bcc9 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 */. int offset
1bcca 3b 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 ; /* Offse
1bccb 74 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 t in pointer map
1bccc 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 page */. int r
1bccd 63 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 c; /*
1bcce 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d Return code from
1bccf 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f subfunctions */
1bcd0 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 .. if( *pRC ) r
1bcd1 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 eturn;.. assert
1bcd2 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1bcd3 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
1bcd4 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 );. /* The mas
1bcd5 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 ter-journal page
1bcd6 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 number must nev
1bcd7 65 72 20 62 65 20 75 73 65 64 20 61 73 20 61 20 er be used as a
1bcd8 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 pointer map page
1bcd9 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d */. assert( 0=
1bcda 3d 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 =PTRMAP_ISPAGE(p
1bcdb 42 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 Bt, PENDING_BYTE
1bcdc 5f 50 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a _PAGE(pBt)) );..
1bcdd 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 assert( pBt->a
1bcde 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 utoVacuum );. i
1bcdf 66 28 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 f( key==0 ){.
1bce0 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 *pRC = SQLITE_C
1bce1 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
1bce2 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
1bce3 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f Ptrmap = PTRMAP_
1bce4 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 PAGENO(pBt, key)
1bce5 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1bce6 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 PagerGet(pBt->pP
1bce7 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 ager, iPtrmap, &
1bce8 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 pDbPage);. if(
1bce9 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1bcea 0a 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a . *pRC = rc;.
1bceb 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
1bcec 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 offset = PTRMA
1bced 50 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 P_PTROFFSET(iPtr
1bcee 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 map, key);. if(
1bcef 20 6f 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 offset<0 ){.
1bcf0 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 *pRC = SQLITE_C
1bcf1 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
1bcf2 20 67 6f 74 6f 20 70 74 72 6d 61 70 5f 65 78 69 goto ptrmap_exi
1bcf3 74 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 t;. }. assert(
1bcf4 20 6f 66 66 73 65 74 20 3c 3d 20 28 69 6e 74 29 offset <= (int)
1bcf5 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d pBt->usableSize-
1bcf6 35 20 29 3b 0a 20 20 70 50 74 72 6d 61 70 20 3d 5 );. pPtrmap =
1bcf7 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 (u8 *)sqlite3Pa
1bcf8 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 61 gerGetData(pDbPa
1bcf9 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 ge);.. if( eTyp
1bcfa 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 e!=pPtrmap[offse
1bcfb 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28 26 t] || get4byte(&
1bcfc 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 pPtrmap[offset+1
1bcfd 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 20 ])!=parent ){.
1bcfe 20 20 54 52 41 43 45 28 28 22 50 54 52 4d 41 50 TRACE(("PTRMAP
1bcff 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 64 _UPDATE: %d->(%d
1bd00 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 ,%d)\n", key, eT
1bd01 79 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a 20 ype, parent));.
1bd02 20 20 20 2a 70 52 43 3d 20 72 63 20 3d 20 73 71 *pRC= rc = sq
1bd03 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
1bd04 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 pDbPage);. if
1bd05 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1bd06 29 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61 70 ){. pPtrmap
1bd07 5b 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70 65 [offset] = eType
1bd08 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 ;. put4byte
1bd09 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 (&pPtrmap[offset
1bd0a 2b 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20 +1], parent);.
1bd0b 20 20 7d 0a 20 20 7d 0a 0a 70 74 72 6d 61 70 5f }. }..ptrmap_
1bd0c 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 50 exit:. sqlite3P
1bd0d 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 agerUnref(pDbPag
1bd0e 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 e);.}../*.** Rea
1bd0f 64 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 d an entry from
1bd10 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e the pointer map.
1bd11 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
1bd12 69 6e 65 20 72 65 74 72 69 65 76 65 73 20 74 68 ine retrieves th
1bd13 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e e pointer map en
1bd14 74 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b 65 try for page 'ke
1bd15 79 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 y', writing.** t
1bd16 68 65 20 74 79 70 65 20 61 6e 64 20 70 61 72 65 he type and pare
1bd17 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 nt page number t
1bd18 6f 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a 70 o *pEType and *p
1bd19 50 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65 6c Pgno respectivel
1bd1a 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 y..** An error c
1bd1b 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ode is returned
1bd1c 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 if something goe
1bd1d 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 s wrong, otherwi
1bd1e 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f se SQLITE_OK..*/
1bd1f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d .static int ptrm
1bd20 61 70 47 65 74 28 42 74 53 68 61 72 65 64 20 2a apGet(BtShared *
1bd21 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 pBt, Pgno key, u
1bd22 38 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f 20 8 *pEType, Pgno
1bd23 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 *pPgno){. DbPag
1bd24 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f 2a e *pDbPage; /*
1bd25 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 The pointer map
1bd26 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 page */. int i
1bd27 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a Ptrmap; /*
1bd28 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 Pointer map pag
1bd29 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 e index */. u8
1bd2a 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 *pPtrmap;
1bd2b 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 /* Pointer map p
1bd2c 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e age data */. in
1bd2d 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 t offset;
1bd2e 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65 6e /* Offset of en
1bd2f 74 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d try in pointer m
1bd30 61 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a ap */. int rc;.
1bd31 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1bd32 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
1bd33 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 t->mutex) );..
1bd34 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 iPtrmap = PTRMAP
1bd35 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 _PAGENO(pBt, key
1bd36 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1bd37 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 3PagerGet(pBt->p
1bd38 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 Pager, iPtrmap,
1bd39 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 &pDbPage);. if(
1bd3a 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 rc!=0 ){. re
1bd3b 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 turn rc;. }. p
1bd3c 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 Ptrmap = (u8 *)s
1bd3d 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
1bd3e 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 ta(pDbPage);..
1bd3f 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f offset = PTRMAP_
1bd40 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61 PTROFFSET(iPtrma
1bd41 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 20 6f p, key);. if( o
1bd42 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 20 73 ffset<0 ){. s
1bd43 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
1bd44 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 72 (pDbPage);. r
1bd45 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
1bd46 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 RUPT_BKPT;. }.
1bd47 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 20 assert( offset
1bd48 3c 3d 20 28 69 6e 74 29 70 42 74 2d 3e 75 73 61 <= (int)pBt->usa
1bd49 62 6c 65 53 69 7a 65 2d 35 20 29 3b 0a 20 20 61 bleSize-5 );. a
1bd4a 73 73 65 72 74 28 20 70 45 54 79 70 65 21 3d 30 ssert( pEType!=0
1bd4b 20 29 3b 0a 20 20 2a 70 45 54 79 70 65 20 3d 20 );. *pEType =
1bd4c 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b pPtrmap[offset];
1bd4d 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 29 20 2a . if( pPgno ) *
1bd4e 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 pPgno = get4byte
1bd4f 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 (&pPtrmap[offset
1bd50 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 +1]);.. sqlite3
1bd51 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 PagerUnref(pDbPa
1bd52 67 65 29 3b 0a 20 20 69 66 28 20 2a 70 45 54 79 ge);. if( *pETy
1bd53 70 65 3c 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e pe<1 || *pEType>
1bd54 35 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 5 ) return SQLIT
1bd55 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
1bd56 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1bd57 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 OK;.}..#else /*
1bd58 69 66 20 64 65 66 69 6e 65 64 20 53 51 4c 49 54 if defined SQLIT
1bd59 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
1bd5a 4d 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 70 M */. #define p
1bd5b 74 72 6d 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a trmapPut(w,x,y,z
1bd5c 2c 72 63 29 0a 20 20 23 64 65 66 69 6e 65 20 70 ,rc). #define p
1bd5d 74 72 6d 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a trmapGet(w,x,y,z
1bd5e 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 ) SQLITE_OK. #d
1bd5f 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74 4f efine ptrmapPutO
1bd60 76 66 6c 50 74 72 28 78 2c 20 79 2c 20 72 63 29 vflPtr(x, y, rc)
1bd61 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 .#endif../*.** G
1bd62 69 76 65 6e 20 61 20 62 74 72 65 65 20 70 61 67 iven a btree pag
1bd63 65 20 61 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 e and a cell ind
1bd64 65 78 20 28 30 20 6d 65 61 6e 73 20 74 68 65 20 ex (0 means the
1bd65 66 69 72 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a first cell on.**
1bd66 20 74 68 65 20 70 61 67 65 2c 20 31 20 6d 65 61 the page, 1 mea
1bd67 6e 73 20 74 68 65 20 73 65 63 6f 6e 64 20 63 65 ns the second ce
1bd68 6c 6c 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 ll, and so forth
1bd69 29 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 ) return a point
1bd6a 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c er.** to the cel
1bd6b 6c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a l content..**.**
1bd6c 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f This routine wo
1bd6d 72 6b 73 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 rks only for pag
1bd6e 65 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 es that do not c
1bd6f 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 ontain overflow
1bd70 63 65 6c 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e cells..*/.#defin
1bd71 65 20 66 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 e findCell(P,I)
1bd72 5c 0a 20 20 28 28 50 29 2d 3e 61 44 61 74 61 20 \. ((P)->aData
1bd73 2b 20 28 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 + ((P)->maskPage
1bd74 20 26 20 67 65 74 32 62 79 74 65 28 26 28 50 29 & get2byte(&(P)
1bd75 2d 3e 61 43 65 6c 6c 49 64 78 5b 32 2a 28 49 29 ->aCellIdx[2*(I)
1bd76 5d 29 29 29 0a 23 64 65 66 69 6e 65 20 66 69 6e ]))).#define fin
1bd77 64 43 65 6c 6c 76 32 28 44 2c 4d 2c 4f 2c 49 29 dCellv2(D,M,O,I)
1bd78 20 28 44 2b 28 4d 26 67 65 74 32 62 79 74 65 28 (D+(M&get2byte(
1bd79 44 2b 28 4f 2b 32 2a 28 49 29 29 29 29 29 0a 0a D+(O+2*(I)))))..
1bd7a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f ./*.** This a mo
1bd7b 72 65 20 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 re complex versi
1bd7c 6f 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 on of findCell()
1bd7d 20 74 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a that works for.
1bd7e 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 64 6f ** pages that do
1bd7f 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f contain overflo
1bd80 77 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 w cells..*/.stat
1bd81 69 63 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 ic u8 *findOverf
1bd82 6c 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 lowCell(MemPage
1bd83 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c *pPage, int iCel
1bd84 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 l){. int i;. a
1bd85 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1bd86 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
1bd87 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
1bd88 20 20 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e for(i=pPage->n
1bd89 4f 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 Overflow-1; i>=0
1bd8a 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 ; i--){. int
1bd8b 6b 3b 0a 20 20 20 20 6b 20 3d 20 70 50 61 67 65 k;. k = pPage
1bd8c 2d 3e 61 69 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 ->aiOvfl[i];.
1bd8d 20 69 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b if( k<=iCell ){
1bd8e 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 69 43 . if( k==iC
1bd8f 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ell ){. r
1bd90 65 74 75 72 6e 20 70 50 61 67 65 2d 3e 61 70 4f eturn pPage->apO
1bd91 76 66 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a vfl[i];. }.
1bd92 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 iCell--;.
1bd93 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1bd94 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 n findCell(pPage
1bd95 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a , iCell);.}../*.
1bd96 2a 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20 ** Parse a cell
1bd97 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e content block an
1bd98 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65 d fill in the Ce
1bd99 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 llInfo structure
1bd9a 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20 . There.** are
1bd9b 74 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 two versions of
1bd9c 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 this function.
1bd9d 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 btreeParseCell()
1bd9e 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c takes a .** cel
1bd9f 6c 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 73 l index as the s
1bda0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61 econd argument a
1bda1 6e 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c nd btreeParseCel
1bda2 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73 lPtr() .** takes
1bda3 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
1bda4 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65 e body of the ce
1bda5 6c 6c 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 ll as its second
1bda6 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
1bda7 20 57 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c Within this fil
1bda8 65 2c 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c e, the parseCell
1bda9 28 29 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 () macro can be
1bdaa 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f called instead o
1bdab 66 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 65 43 f.** btreeParseC
1bdac 65 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 20 ellPtr(). Using
1bdad 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 some compilers,
1bdae 74 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61 73 this will be fas
1bdaf 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ter..*/.static v
1bdb0 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 43 65 oid btreeParseCe
1bdb1 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65 llPtr(. MemPage
1bdb2 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 *pPage,
1bdb3 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e /* Page contain
1bdb4 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a ing the cell */.
1bdb5 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 u8 *pCell,
1bdb6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
1bdb7 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c nter to the cell
1bdb8 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c text. */. Cell
1bdb9 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 Info *pInfo
1bdba 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 /* Fill in t
1bdbb 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f his structure */
1bdbc 0a 29 7b 0a 20 20 75 31 36 20 6e 3b 20 20 20 20 .){. u16 n;
1bdbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1bdbe 20 4e 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e Number bytes in
1bdbf 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 cell content he
1bdc0 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50 ader */. u32 nP
1bdc1 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20 ayload;
1bdc2 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1bdc3 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79 ytes of cell pay
1bdc4 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 load */.. asser
1bdc5 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1bdc6 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
1bdc7 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 ->mutex) );.. p
1bdc8 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 Info->pCell = pC
1bdc9 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ell;. assert( p
1bdca 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c Page->leaf==0 ||
1bdcb 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 pPage->leaf==1
1bdcc 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e );. n = pPage->
1bdcd 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 childPtrSize;.
1bdce 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 assert( n==4-4*p
1bdcf 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 Page->leaf );.
1bdd0 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 if( pPage->intKe
1bdd1 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 y ){. if( pPa
1bdd2 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 ge->hasData ){.
1bdd3 20 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 n += getVar
1bdd4 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c int32(&pCell[n],
1bdd5 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 nPayload);.
1bdd6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 }else{. nPa
1bdd7 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d yload = 0;. }
1bdd8 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 . n += getVar
1bdd9 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 int(&pCell[n], (
1bdda 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 u64*)&pInfo->nKe
1bddb 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e y);. pInfo->n
1bddc 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b Data = nPayload;
1bddd 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 . }else{. pI
1bdde 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a nfo->nData = 0;.
1bddf 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 n += getVari
1bde0 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 nt32(&pCell[n],
1bde1 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70 nPayload);. p
1bde2 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 Info->nKey = nPa
1bde3 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e yload;. }. pIn
1bde4 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e fo->nPayload = n
1bde5 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f Payload;. pInfo
1bde6 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20 ->nHeader = n;.
1bde7 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c testcase( nPayl
1bde8 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c oad==pPage->maxL
1bde9 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61 ocal );. testca
1bdea 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 se( nPayload==pP
1bdeb 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 age->maxLocal+1
1bdec 29 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28 );. if( likely(
1bded 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d nPayload<=pPage-
1bdee 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 20 >maxLocal) ){.
1bdef 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 /* This is the
1bdf0 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 (easy) common c
1bdf1 61 73 65 20 77 68 65 72 65 20 74 68 65 20 65 6e ase where the en
1bdf2 74 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74 tire payload fit
1bdf3 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 s. ** on the
1bdf4 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20 local page. No
1bdf5 6f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75 overflow is requ
1bdf6 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ired.. */.
1bdf7 20 69 66 28 20 28 70 49 6e 66 6f 2d 3e 6e 53 69 if( (pInfo->nSi
1bdf8 7a 65 20 3d 20 28 75 31 36 29 28 6e 2b 6e 50 61 ze = (u16)(n+nPa
1bdf9 79 6c 6f 61 64 29 29 3c 34 20 29 20 70 49 6e 66 yload))<4 ) pInf
1bdfa 6f 2d 3e 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20 o->nSize = 4;.
1bdfb 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 pInfo->nLocal
1bdfc 3d 20 28 75 31 36 29 6e 50 61 79 6c 6f 61 64 3b = (u16)nPayload;
1bdfd 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 . pInfo->iOve
1bdfe 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 7d 65 6c rflow = 0;. }el
1bdff 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 se{. /* If th
1be00 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e e payload will n
1be01 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c ot fit completel
1be02 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 y on the local p
1be03 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20 age, we have.
1be04 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f ** to decide ho
1be05 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20 w much to store
1be06 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20 locally and how
1be07 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e much to spill on
1be08 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c to. ** overfl
1be09 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 73 ow pages. The s
1be0a 74 72 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69 trategy is to mi
1be0b 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e nimize the amoun
1be0c 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20 t of unused.
1be0d 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72 ** space on over
1be0e 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65 flow pages while
1be0f 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f keeping the amo
1be10 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f unt of local sto
1be11 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62 rage. ** in b
1be12 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20 etween minLocal
1be13 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20 and maxLocal..
1be14 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e **. ** Warn
1be15 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74 ing: changing t
1be16 68 65 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20 he way overflow
1be17 70 61 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72 payload is distr
1be18 69 62 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20 ibuted in any.
1be19 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65 ** way will re
1be1a 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d sult in an incom
1be1b 70 61 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72 patible file for
1be1c 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 mat.. */.
1be1d 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f int minLocal; /
1be1e 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 * Minimum amount
1be1f 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 of payload held
1be20 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 locally */.
1be21 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f int maxLocal; /
1be22 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 * Maximum amount
1be23 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 of payload held
1be24 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 locally */.
1be25 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f int surplus; /
1be26 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f * Overflow paylo
1be27 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 ad available for
1be28 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a local storage *
1be29 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20 /.. minLocal
1be2a 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 = pPage->minLoca
1be2b 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20 l;. maxLocal
1be2c 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 = pPage->maxLoca
1be2d 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d l;. surplus =
1be2e 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61 minLocal + (nPa
1be2f 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c yload - minLocal
1be30 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 )%(pPage->pBt->u
1be31 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a sableSize - 4);.
1be32 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73 75 testcase( su
1be33 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20 rplus==maxLocal
1be34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
1be35 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 surplus==maxLoc
1be36 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 al+1 );. if(
1be37 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f surplus <= maxLo
1be38 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e cal ){. pIn
1be39 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 fo->nLocal = (u1
1be3a 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d 6)surplus;. }
1be3b 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66 else{. pInf
1be3c 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 o->nLocal = (u16
1be3d 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d )minLocal;. }
1be3e 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 . pInfo->iOve
1be3f 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 28 70 49 rflow = (u16)(pI
1be40 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29 nfo->nLocal + n)
1be41 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 ;. pInfo->nSi
1be42 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 ze = pInfo->iOve
1be43 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d rflow + 4;. }.}
1be44 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 43 65 .#define parseCe
1be45 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c ll(pPage, iCell,
1be46 20 70 49 6e 66 6f 29 20 5c 0a 20 20 62 74 72 65 pInfo) \. btre
1be47 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70 eParseCellPtr((p
1be48 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28 Page), findCell(
1be49 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29 (pPage), (iCell)
1be4a 29 2c 20 28 70 49 6e 66 6f 29 29 0a 73 74 61 74 ), (pInfo)).stat
1be4b 69 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 ic void btreePar
1be4c 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 seCell(. MemPag
1be4d 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 e *pPage,
1be4e 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 /* Page contai
1be4f 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f ning the cell */
1be50 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 . int iCell,
1be51 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1be52 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 e cell index. F
1be53 69 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a irst cell is 0 *
1be54 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 /. CellInfo *pI
1be55 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 nfo /* F
1be56 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 ill in this stru
1be57 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 cture */.){. pa
1be58 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 rseCell(pPage, i
1be59 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a Cell, pInfo);.}.
1be5a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 ./*.** Compute t
1be5b 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
1be5c 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20 of bytes that a
1be5d 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 Cell needs in th
1be5e 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 e cell.** data a
1be5f 72 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65 rea of the btree
1be60 2d 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75 -page. The retu
1be61 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 rn number includ
1be62 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 es the cell.** d
1be63 61 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74 ata header and t
1be64 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 he local payload
1be65 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 , but not any ov
1be66 65 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a erflow page or.*
1be67 2a 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64 * the space used
1be68 20 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 by the cell poi
1be69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 nter..*/.static
1be6a 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 u16 cellSizePtr(
1be6b 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
1be6c 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38 u8 *pCell){. u8
1be6d 20 2a 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c *pIter = &pCell
1be6e 5b 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 [pPage->childPtr
1be6f 53 69 7a 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69 Size];. u32 nSi
1be70 7a 65 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ze;..#ifdef SQLI
1be71 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 TE_DEBUG. /* Th
1be72 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
1be73 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
1be74 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 n should always
1be75 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 0a 20 be the same as.
1be76 20 2a 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e 66 ** the (CellInf
1be77 6f 2e 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 66 o.nSize) value f
1be78 6f 75 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 20 ound by doing a
1be79 66 75 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 68 full parse of th
1be7a 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 e. ** cell. If
1be7b 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 SQLITE_DEBUG is
1be7c 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 defined, an asse
1be7d 72 74 28 29 20 61 74 20 74 68 65 20 62 6f 74 74 rt() at the bott
1be7e 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 om of. ** this
1be7f 66 75 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 65 function verifie
1be80 73 20 74 68 61 74 20 74 68 69 73 20 69 6e 76 61 s that this inva
1be81 72 69 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f riant is not vio
1be82 6c 61 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c lated. */. Cell
1be83 49 6e 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a Info debuginfo;.
1be84 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
1be85 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c Ptr(pPage, pCell
1be86 2c 20 26 64 65 62 75 67 69 6e 66 6f 29 3b 0a 23 , &debuginfo);.#
1be87 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 50 61 endif.. if( pPa
1be88 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ge->intKey ){.
1be89 20 20 75 38 20 2a 70 45 6e 64 3b 0a 20 20 20 20 u8 *pEnd;.
1be8a 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 if( pPage->hasDa
1be8b 74 61 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65 ta ){. pIte
1be8c 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 r += getVarint32
1be8d 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a (pIter, nSize);.
1be8e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1be8f 20 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 nSize = 0;.
1be90 7d 0a 0a 20 20 20 20 2f 2a 20 70 49 74 65 72 20 }.. /* pIter
1be91 6e 6f 77 20 70 6f 69 6e 74 73 20 61 74 20 74 68 now points at th
1be92 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 e 64-bit integer
1be93 20 6b 65 79 20 76 61 6c 75 65 2c 20 61 20 76 61 key value, a va
1be94 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 0a 20 riable length .
1be95 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 54 ** integer. T
1be96 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
1be97 63 6b 20 6d 6f 76 65 73 20 70 49 74 65 72 20 74 ck moves pIter t
1be98 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 66 o point at the f
1be99 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 2a 2a irst byte. **
1be9a 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
1be9b 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 the key value.
1be9c 2a 2f 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 70 */. pEnd = &p
1be9d 49 74 65 72 5b 39 5d 3b 0a 20 20 20 20 77 68 69 Iter[9];. whi
1be9e 6c 65 28 20 28 2a 70 49 74 65 72 2b 2b 29 26 30 le( (*pIter++)&0
1be9f 78 38 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e x80 && pIter<pEn
1bea0 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 d );. }else{.
1bea1 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61 pIter += getVa
1bea2 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53 rint32(pIter, nS
1bea3 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 74 65 73 ize);. }.. tes
1bea4 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 tcase( nSize==pP
1bea5 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b age->maxLocal );
1bea6 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 . testcase( nSi
1bea7 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f ze==pPage->maxLo
1bea8 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e cal+1 );. if( n
1bea9 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c Size>pPage->maxL
1beaa 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 ocal ){. int
1beab 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 minLocal = pPage
1beac 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 ->minLocal;.
1bead 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c nSize = minLocal
1beae 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c + (nSize - minL
1beaf 6f 63 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e ocal) % (pPage->
1beb0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
1beb1 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 - 4);. testca
1beb2 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 se( nSize==pPage
1beb3 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 ->maxLocal );.
1beb4 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a testcase( nSiz
1beb5 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 e==pPage->maxLoc
1beb6 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 al+1 );. if(
1beb7 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 nSize>pPage->max
1beb8 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 6e Local ){. n
1beb9 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b Size = minLocal;
1beba 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53 69 7a 65 . }. nSize
1bebb 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 6e 53 69 += 4;. }. nSi
1bebc 7a 65 20 2b 3d 20 28 75 33 32 29 28 70 49 74 65 ze += (u32)(pIte
1bebd 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f r - pCell);.. /
1bebe 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 * The minimum si
1bebf 7a 65 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 69 ze of any cell i
1bec0 73 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 s 4 bytes. */.
1bec1 69 66 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a 20 if( nSize<4 ){.
1bec2 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20 nSize = 4;.
1bec3 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69 }.. assert( nSi
1bec4 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53 ze==debuginfo.nS
1bec5 69 7a 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ize );. return
1bec6 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 0a 23 (u16)nSize;.}..#
1bec7 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
1bec8 55 47 0a 2f 2a 20 54 68 69 73 20 76 61 72 69 61 UG./* This varia
1bec9 74 69 6f 6e 20 6f 6e 20 63 65 6c 6c 53 69 7a 65 tion on cellSize
1beca 50 74 72 28 29 20 69 73 20 75 73 65 64 20 69 6e Ptr() is used in
1becb 73 69 64 65 20 6f 66 20 61 73 73 65 72 74 28 29 side of assert()
1becc 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 6f statements.** o
1becd 6e 6c 79 2e 20 2a 2f 0a 73 74 61 74 69 63 20 75 nly. */.static u
1bece 31 36 20 63 65 6c 6c 53 69 7a 65 28 4d 65 6d 50 16 cellSize(MemP
1becf 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 age *pPage, int
1bed0 69 43 65 6c 6c 29 7b 0a 20 20 72 65 74 75 72 6e iCell){. return
1bed1 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
1bed2 67 65 2c 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 ge, findCell(pPa
1bed3 67 65 2c 20 69 43 65 6c 6c 29 29 3b 0a 7d 0a 23 ge, iCell));.}.#
1bed4 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
1bed5 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
1bed6 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 ACUUM./*.** If t
1bed7 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c 2c 20 70 he cell pCell, p
1bed8 61 72 74 20 6f 66 20 70 61 67 65 20 70 50 61 67 art of page pPag
1bed9 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 e contains a poi
1beda 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 nter.** to an ov
1bedb 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73 erflow page, ins
1bedc 65 72 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 ert an entry int
1bedd 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 o the pointer-ma
1bede 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76 65 p.** for the ove
1bedf 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 rflow page..*/.s
1bee0 74 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 tatic void ptrma
1bee1 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d 50 pPutOvflPtr(MemP
1bee2 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a age *pPage, u8 *
1bee3 70 43 65 6c 6c 2c 20 69 6e 74 20 2a 70 52 43 29 pCell, int *pRC)
1bee4 7b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 {. CellInfo inf
1bee5 6f 3b 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 o;. if( *pRC )
1bee6 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 return;. assert
1bee7 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b 0a 20 20 ( pCell!=0 );.
1bee8 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
1bee9 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 r(pPage, pCell,
1beea 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 &info);. assert
1beeb 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 ( (info.nData+(p
1beec 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 Page->intKey?0:i
1beed 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66 6f nfo.nKey))==info
1beee 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 69 .nPayload );. i
1beef 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f f( info.iOverflo
1bef0 77 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f 76 w ){. Pgno ov
1bef1 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 fl = get4byte(&p
1bef2 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 Cell[info.iOverf
1bef3 6c 6f 77 5d 29 3b 0a 20 20 20 20 70 74 72 6d 61 low]);. ptrma
1bef4 70 50 75 74 28 70 50 61 67 65 2d 3e 70 42 74 2c pPut(pPage->pBt,
1bef5 20 6f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 ovfl, PTRMAP_OV
1bef6 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d 3e ERFLOW1, pPage->
1bef7 70 67 6e 6f 2c 20 70 52 43 29 3b 0a 20 20 7d 0a pgno, pRC);. }.
1bef8 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a }.#endif.../*.**
1bef9 20 44 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 Defragment the
1befa 70 61 67 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c page given. All
1befb 20 43 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64 Cells are moved
1befc 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f to the.** end o
1befd 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 61 f the page and a
1befe 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20 69 73 ll free space is
1beff 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 collected into
1bf00 6f 6e 65 0a 2a 2a 20 62 69 67 20 46 72 65 65 42 one.** big FreeB
1bf01 6c 6b 20 74 68 61 74 20 6f 63 63 75 72 73 20 69 lk that occurs i
1bf02 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 68 65 n between the he
1bf03 61 64 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a ader and cell.**
1bf04 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 61 pointer array a
1bf05 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 nd the cell cont
1bf06 65 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 ent area..*/.sta
1bf07 74 69 63 20 69 6e 74 20 64 65 66 72 61 67 6d 65 tic int defragme
1bf08 6e 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a ntPage(MemPage *
1bf09 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b pPage){. int i;
1bf0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bf0b 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
1bf0c 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 nter */. int pc
1bf0d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1bf0e 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 /* Address
1bf0f 6f 66 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a of a i-th cell *
1bf10 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 /. int hdr;
1bf11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1bf12 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 * Offset to the
1bf13 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 page header */.
1bf14 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20 int size;
1bf15 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1bf16 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f ize of a cell */
1bf17 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a . int usableSiz
1bf18 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
1bf19 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c Number of usabl
1bf1a 65 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 e bytes on a pag
1bf1b 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f e */. int cellO
1bf1c 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 ffset;
1bf1d 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 /* Offset to t
1bf1e 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 he cell pointer
1bf1f 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63 array */. int c
1bf20 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 brk;
1bf21 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
1bf22 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 to the cell cont
1bf23 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e ent area */. in
1bf24 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 t nCell;
1bf25 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1bf26 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 er of cells on t
1bf27 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 he page */. uns
1bf28 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 igned char *data
1bf29 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 ; /* The p
1bf2a 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e age data */. un
1bf2b 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d signed char *tem
1bf2c 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 p; /* Temp
1bf2d 20 61 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 area for cell c
1bf2e 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 ontent */. int
1bf2f 69 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 20 iCellFirst;
1bf30 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
1bf31 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 69 allowable cell i
1bf32 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43 ndex */. int iC
1bf33 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20 20 20 20 ellLast;
1bf34 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73 /* Last pos
1bf35 73 69 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78 sible cell index
1bf36 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 72 74 28 20 */... assert(
1bf37 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
1bf38 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
1bf39 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 DbPage) );. ass
1bf3a 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 ert( pPage->pBt!
1bf3b 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1bf3c 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
1bf3d 6c 65 53 69 7a 65 20 3c 3d 20 53 51 4c 49 54 45 leSize <= SQLITE
1bf3e 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 29 _MAX_PAGE_SIZE )
1bf3f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
1bf40 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 e->nOverflow==0
1bf41 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
1bf42 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1bf43 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
1bf44 78 29 20 29 3b 0a 20 20 74 65 6d 70 20 3d 20 73 x) );. temp = s
1bf45 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 qlite3PagerTempS
1bf46 70 61 63 65 28 70 50 61 67 65 2d 3e 70 42 74 2d pace(pPage->pBt-
1bf47 3e 70 50 61 67 65 72 29 3b 0a 20 20 64 61 74 61 >pPager);. data
1bf48 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b = pPage->aData;
1bf49 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e . hdr = pPage->
1bf4a 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 63 65 6c hdrOffset;. cel
1bf4b 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d lOffset = pPage-
1bf4c 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 6e >cellOffset;. n
1bf4d 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 Cell = pPage->nC
1bf4e 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 6e ell;. assert( n
1bf4f 43 65 6c 6c 3d 3d 67 65 74 32 62 79 74 65 28 26 Cell==get2byte(&
1bf50 64 61 74 61 5b 68 64 72 2b 33 5d 29 20 29 3b 0a data[hdr+3]) );.
1bf51 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 usableSize = p
1bf52 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
1bf53 65 53 69 7a 65 3b 0a 20 20 63 62 72 6b 20 3d 20 eSize;. cbrk =
1bf54 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
1bf55 64 72 2b 35 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 dr+5]);. memcpy
1bf56 28 26 74 65 6d 70 5b 63 62 72 6b 5d 2c 20 26 64 (&temp[cbrk], &d
1bf57 61 74 61 5b 63 62 72 6b 5d 2c 20 75 73 61 62 6c ata[cbrk], usabl
1bf58 65 53 69 7a 65 20 2d 20 63 62 72 6b 29 3b 0a 20 eSize - cbrk);.
1bf59 20 63 62 72 6b 20 3d 20 75 73 61 62 6c 65 53 69 cbrk = usableSi
1bf5a 7a 65 3b 0a 20 20 69 43 65 6c 6c 46 69 72 73 74 ze;. iCellFirst
1bf5b 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 = cellOffset +
1bf5c 32 2a 6e 43 65 6c 6c 3b 0a 20 20 69 43 65 6c 6c 2*nCell;. iCell
1bf5d 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 53 69 7a Last = usableSiz
1bf5e 65 20 2d 20 34 3b 0a 20 20 66 6f 72 28 69 3d 30 e - 4;. for(i=0
1bf5f 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b ; i<nCell; i++){
1bf60 0a 20 20 20 20 75 38 20 2a 70 41 64 64 72 3b 20 . u8 *pAddr;
1bf61 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 20 /* The i-th
1bf62 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a cell pointer */.
1bf63 20 20 20 20 70 41 64 64 72 20 3d 20 26 64 61 74 pAddr = &dat
1bf64 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 69 a[cellOffset + i
1bf65 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67 65 *2];. pc = ge
1bf66 74 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a 20 t2byte(pAddr);.
1bf67 20 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d testcase( pc=
1bf68 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 =iCellFirst );.
1bf69 20 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d testcase( pc=
1bf6a 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 23 69 =iCellLast );.#i
1bf6b 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
1bf6c 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a E_ENABLE_OVERSIZ
1bf6d 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 E_CELL_CHECK).
1bf6e 20 20 2f 2a 20 54 68 65 73 65 20 63 6f 6e 64 69 /* These condi
1bf6f 74 69 6f 6e 73 20 68 61 76 65 20 61 6c 72 65 61 tions have alrea
1bf70 64 79 20 62 65 65 6e 20 76 65 72 69 66 69 65 64 dy been verified
1bf71 20 69 6e 20 62 74 72 65 65 49 6e 69 74 50 61 67 in btreeInitPag
1bf72 65 28 29 0a 20 20 20 20 2a 2a 20 69 66 20 53 51 e(). ** if SQ
1bf73 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 LITE_ENABLE_OVER
1bf74 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 20 SIZE_CELL_CHECK
1bf75 69 73 20 64 65 66 69 6e 65 64 20 0a 20 20 20 20 is defined .
1bf76 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 3c 69 43 */. if( pc<iC
1bf77 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 ellFirst || pc>i
1bf78 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 CellLast ){.
1bf79 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1bf7a 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1bf7b 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 }.#endif. a
1bf7c 73 73 65 72 74 28 20 70 63 3e 3d 69 43 65 6c 6c ssert( pc>=iCell
1bf7d 46 69 72 73 74 20 26 26 20 70 63 3c 3d 69 43 65 First && pc<=iCe
1bf7e 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 20 73 69 llLast );. si
1bf7f 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 ze = cellSizePtr
1bf80 28 70 50 61 67 65 2c 20 26 74 65 6d 70 5b 70 63 (pPage, &temp[pc
1bf81 5d 29 3b 0a 20 20 20 20 63 62 72 6b 20 2d 3d 20 ]);. cbrk -=
1bf82 73 69 7a 65 3b 0a 23 69 66 20 64 65 66 69 6e 65 size;.#if define
1bf83 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
1bf84 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 OVERSIZE_CELL_CH
1bf85 45 43 4b 29 0a 20 20 20 20 69 66 28 20 63 62 72 ECK). if( cbr
1bf86 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20 29 7b 0a k<iCellFirst ){.
1bf87 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
1bf88 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1bf89 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 ;. }.#else.
1bf8a 20 20 69 66 28 20 63 62 72 6b 3c 69 43 65 6c 6c if( cbrk<iCell
1bf8b 46 69 72 73 74 20 7c 7c 20 70 63 2b 73 69 7a 65 First || pc+size
1bf8c 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 >usableSize ){.
1bf8d 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1bf8e 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1bf8f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
1bf90 20 20 61 73 73 65 72 74 28 20 63 62 72 6b 2b 73 assert( cbrk+s
1bf91 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20 ize<=usableSize
1bf92 26 26 20 63 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 && cbrk>=iCellFi
1bf93 72 73 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63 rst );. testc
1bf94 61 73 65 28 20 63 62 72 6b 2b 73 69 7a 65 3d 3d ase( cbrk+size==
1bf95 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 usableSize );.
1bf96 20 20 74 65 73 74 63 61 73 65 28 20 70 63 2b 73 testcase( pc+s
1bf97 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 ize==usableSize
1bf98 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 );. memcpy(&d
1bf99 61 74 61 5b 63 62 72 6b 5d 2c 20 26 74 65 6d 70 ata[cbrk], &temp
1bf9a 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 20 [pc], size);.
1bf9b 20 70 75 74 32 62 79 74 65 28 70 41 64 64 72 2c put2byte(pAddr,
1bf9c 20 63 62 72 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 cbrk);. }. as
1bf9d 73 65 72 74 28 20 63 62 72 6b 3e 3d 69 43 65 6c sert( cbrk>=iCel
1bf9e 6c 46 69 72 73 74 20 29 3b 0a 20 20 70 75 74 32 lFirst );. put2
1bf9f 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
1bfa0 5d 2c 20 63 62 72 6b 29 3b 0a 20 20 64 61 74 61 ], cbrk);. data
1bfa1 5b 68 64 72 2b 31 5d 20 3d 20 30 3b 0a 20 20 64 [hdr+1] = 0;. d
1bfa2 61 74 61 5b 68 64 72 2b 32 5d 20 3d 20 30 3b 0a ata[hdr+2] = 0;.
1bfa3 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 data[hdr+7] =
1bfa4 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 0;. memset(&dat
1bfa5 61 5b 69 43 65 6c 6c 46 69 72 73 74 5d 2c 20 30 a[iCellFirst], 0
1bfa6 2c 20 63 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73 , cbrk-iCellFirs
1bfa7 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 t);. assert( sq
1bfa8 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
1bfa9 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
1bfaa 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20 63 Page) );. if( c
1bfab 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73 74 21 3d brk-iCellFirst!=
1bfac 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a pPage->nFree ){.
1bfad 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1bfae 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
1bfaf 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
1bfb0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
1bfb1 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 Allocate nByte
1bfb2 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 bytes of space f
1bfb3 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 42 rom within the B
1bfb4 2d 54 72 65 65 20 70 61 67 65 20 70 61 73 73 65 -Tree page passe
1bfb5 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 d.** as the firs
1bfb6 74 20 61 72 67 75 6d 65 6e 74 2e 20 57 72 69 74 t argument. Writ
1bfb7 65 20 69 6e 74 6f 20 2a 70 49 64 78 20 74 68 65 e into *pIdx the
1bfb8 20 69 6e 64 65 78 20 69 6e 74 6f 20 70 50 61 67 index into pPag
1bfb9 65 2d 3e 61 44 61 74 61 5b 5d 0a 2a 2a 20 6f 66 e->aData[].** of
1bfba 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 the first byte
1bfbb 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 of allocated spa
1bfbc 63 65 2e 20 52 65 74 75 72 6e 20 65 69 74 68 65 ce. Return eithe
1bfbd 72 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 0a 2a r SQLITE_OK or.*
1bfbe 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 * an error code
1bfbf 28 75 73 75 61 6c 6c 79 20 53 51 4c 49 54 45 5f (usually SQLITE_
1bfc0 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 CORRUPT)..**.**
1bfc1 54 68 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61 The caller guara
1bfc2 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 72 65 ntees that there
1bfc3 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 73 is sufficient s
1bfc4 70 61 63 65 20 74 6f 20 6d 61 6b 65 20 74 68 65 pace to make the
1bfc5 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 .** allocation.
1bfc6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 This routine mi
1bfc7 67 68 74 20 6e 65 65 64 20 74 6f 20 64 65 66 72 ght need to defr
1bfc8 61 67 6d 65 6e 74 20 69 6e 20 6f 72 64 65 72 20 agment in order
1bfc9 74 6f 20 62 72 69 6e 67 0a 2a 2a 20 61 6c 6c 20 to bring.** all
1bfca 74 68 65 20 73 70 61 63 65 20 74 6f 67 65 74 68 the space togeth
1bfcb 65 72 2c 20 68 6f 77 65 76 65 72 2e 20 20 54 68 er, however. Th
1bfcc 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
1bfcd 61 76 6f 69 64 20 75 73 69 6e 67 0a 2a 2a 20 74 avoid using.** t
1bfce 68 65 20 66 69 72 73 74 20 74 77 6f 20 62 79 74 he first two byt
1bfcf 65 73 20 70 61 73 74 20 74 68 65 20 63 65 6c 6c es past the cell
1bfd0 20 70 6f 69 6e 74 65 72 20 61 72 65 61 20 73 69 pointer area si
1bfd1 6e 63 65 20 70 72 65 73 75 6d 61 62 6c 79 20 74 nce presumably t
1bfd2 68 69 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f his.** allocatio
1bfd3 6e 20 69 73 20 62 65 69 6e 67 20 6d 61 64 65 20 n is being made
1bfd4 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 73 65 in order to inse
1bfd5 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 2c 20 73 rt a new cell, s
1bfd6 6f 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 o we will.** als
1bfd7 6f 20 65 6e 64 20 75 70 20 6e 65 65 64 69 6e 67 o end up needing
1bfd8 20 61 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e a new cell poin
1bfd9 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ter..*/.static i
1bfda 6e 74 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 nt allocateSpace
1bfdb 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
1bfdc 20 69 6e 74 20 6e 42 79 74 65 2c 20 69 6e 74 20 int nByte, int
1bfdd 2a 70 49 64 78 29 7b 0a 20 20 63 6f 6e 73 74 20 *pIdx){. const
1bfde 69 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 2d int hdr = pPage-
1bfdf 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 2f >hdrOffset; /
1bfe0 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 * Local cache of
1bfe1 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
1bfe2 74 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 t */. u8 * cons
1bfe3 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e t data = pPage->
1bfe4 61 44 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 4c aData; /* L
1bfe5 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 50 ocal cache of pP
1bfe6 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 age->aData */.
1bfe7 69 6e 74 20 6e 46 72 61 67 3b 20 20 20 20 20 20 int nFrag;
1bfe8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bfe9 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1bfea 66 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 f fragmented byt
1bfeb 65 73 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20 es on pPage */.
1bfec 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20 int top;
1bfed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bfee 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 /* First b
1bfef 79 74 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 yte of cell cont
1bff0 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e ent area */. in
1bff1 74 20 67 61 70 3b 20 20 20 20 20 20 20 20 2f 2a t gap; /*
1bff2 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 67 First byte of g
1bff3 61 70 20 62 65 74 77 65 65 6e 20 63 65 6c 6c 20 ap between cell
1bff4 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 63 65 6c pointers and cel
1bff5 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 l content */. i
1bff6 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 2f nt rc; /
1bff7 2a 20 49 6e 74 65 67 65 72 20 72 65 74 75 72 6e * Integer return
1bff8 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 75 code */. int u
1bff9 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a 20 55 73 sableSize; /* Us
1bffa 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 74 68 65 able size of the
1bffb 20 70 61 67 65 20 2a 2f 0a 20 20 0a 20 20 61 73 page */. . as
1bffc 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
1bffd 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
1bffe 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
1bfff 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
1c000 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 ->pBt );. asser
1c001 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1c002 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
1c003 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
1c004 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 30 20 29 sert( nByte>=0 )
1c005 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 ; /* Minimum ce
1c006 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a ll size is 4 */.
1c007 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
1c008 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 20 29 3b >nFree>=nByte );
1c009 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
1c00a 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 ->nOverflow==0 )
1c00b 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d ;. usableSize =
1c00c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 pPage->pBt->usa
1c00d 62 6c 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 bleSize;. asser
1c00e 74 28 20 6e 42 79 74 65 20 3c 20 75 73 61 62 6c t( nByte < usabl
1c00f 65 53 69 7a 65 2d 38 20 29 3b 0a 0a 20 20 6e 46 eSize-8 );.. nF
1c010 72 61 67 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 rag = data[hdr+7
1c011 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ];. assert( pPa
1c012 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d ge->cellOffset =
1c013 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 = hdr + 12 - 4*p
1c014 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 Page->leaf );.
1c015 67 61 70 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c gap = pPage->cel
1c016 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 lOffset + 2*pPag
1c017 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 74 6f 70 20 e->nCell;. top
1c018 3d 20 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 = get2byteNotZer
1c019 6f 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b o(&data[hdr+5]);
1c01a 0a 20 20 69 66 28 20 67 61 70 3e 74 6f 70 20 29 . if( gap>top )
1c01b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
1c01c 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 74 ORRUPT_BKPT;. t
1c01d 65 73 74 63 61 73 65 28 20 67 61 70 2b 32 3d 3d estcase( gap+2==
1c01e 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 61 73 top );. testcas
1c01f 65 28 20 67 61 70 2b 31 3d 3d 74 6f 70 20 29 3b e( gap+1==top );
1c020 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70 . testcase( gap
1c021 3d 3d 74 6f 70 20 29 3b 0a 0a 20 20 69 66 28 20 ==top );.. if(
1c022 6e 46 72 61 67 3e 3d 36 30 20 29 7b 0a 20 20 20 nFrag>=60 ){.
1c023 20 2f 2a 20 41 6c 77 61 79 73 20 64 65 66 72 61 /* Always defra
1c024 67 6d 65 6e 74 20 68 69 67 68 6c 79 20 66 72 61 gment highly fra
1c025 67 6d 65 6e 74 65 64 20 70 61 67 65 73 20 2a 2f gmented pages */
1c026 0a 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 67 . rc = defrag
1c027 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b mentPage(pPage);
1c028 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
1c029 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 turn rc;. top
1c02a 20 3d 20 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 = get2byteNotZe
1c02b 72 6f 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 ro(&data[hdr+5])
1c02c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 61 ;. }else if( ga
1c02d 70 2b 32 3c 3d 74 6f 70 20 29 7b 0a 20 20 20 20 p+2<=top ){.
1c02e 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 66 72 /* Search the fr
1c02f 65 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67 20 66 eelist looking f
1c030 6f 72 20 61 20 66 72 65 65 20 73 6c 6f 74 20 62 or a free slot b
1c031 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61 74 ig enough to sat
1c032 69 73 66 79 20 0a 20 20 20 20 2a 2a 20 74 68 65 isfy . ** the
1c033 20 72 65 71 75 65 73 74 2e 20 54 68 65 20 61 6c request. The al
1c034 6c 6f 63 61 74 69 6f 6e 20 69 73 20 6d 61 64 65 location is made
1c035 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 from the first
1c036 66 72 65 65 20 73 6c 6f 74 20 69 6e 20 0a 20 20 free slot in .
1c037 20 20 2a 2a 20 74 68 65 20 6c 69 73 74 20 74 68 ** the list th
1c038 61 74 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 at is large enou
1c039 67 68 20 74 6f 20 61 63 63 6f 6d 61 64 61 74 65 gh to accomadate
1c03a 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 it.. */.
1c03b 69 6e 74 20 70 63 2c 20 61 64 64 72 3b 0a 20 20 int pc, addr;.
1c03c 20 20 66 6f 72 28 61 64 64 72 3d 68 64 72 2b 31 for(addr=hdr+1
1c03d 3b 20 28 70 63 20 3d 20 67 65 74 32 62 79 74 65 ; (pc = get2byte
1c03e 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 (&data[addr]))>0
1c03f 3b 20 61 64 64 72 3d 70 63 29 7b 0a 20 20 20 20 ; addr=pc){.
1c040 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 int size;
1c041 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1c042 66 20 74 68 65 20 66 72 65 65 20 73 6c 6f 74 20 f the free slot
1c043 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 63 3e */. if( pc>
1c044 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 7c 7c 20 usableSize-4 ||
1c045 70 63 3c 61 64 64 72 2b 34 20 29 7b 0a 20 20 20 pc<addr+4 ){.
1c046 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1c047 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1c048 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
1c049 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 ize = get2byte(&
1c04a 64 61 74 61 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 data[pc+2]);.
1c04b 20 20 20 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79 if( size>=nBy
1c04c 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e te ){. in
1c04d 74 20 78 20 3d 20 73 69 7a 65 20 2d 20 6e 42 79 t x = size - nBy
1c04e 74 65 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 te;. test
1c04f 63 61 73 65 28 20 78 3d 3d 34 20 29 3b 0a 20 20 case( x==4 );.
1c050 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1c051 78 3d 3d 33 20 29 3b 0a 20 20 20 20 20 20 20 20 x==3 );.
1c052 69 66 28 20 78 3c 34 20 29 7b 0a 20 20 20 20 20 if( x<4 ){.
1c053 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 /* Remove t
1c054 68 65 20 73 6c 6f 74 20 66 72 6f 6d 20 74 68 65 he slot from the
1c055 20 66 72 65 65 2d 6c 69 73 74 2e 20 55 70 64 61 free-list. Upda
1c056 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 te the number of
1c057 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 72 . ** fr
1c058 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73 20 77 agmented bytes w
1c059 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e 20 ithin the page.
1c05a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d */. mem
1c05b 63 70 79 28 26 64 61 74 61 5b 61 64 64 72 5d 2c cpy(&data[addr],
1c05c 20 26 64 61 74 61 5b 70 63 5d 2c 20 32 29 3b 0a &data[pc], 2);.
1c05d 20 20 20 20 20 20 20 20 20 20 64 61 74 61 5b 68 data[h
1c05e 64 72 2b 37 5d 20 3d 20 28 75 38 29 28 6e 46 72 dr+7] = (u8)(nFr
1c05f 61 67 20 2b 20 78 29 3b 0a 20 20 20 20 20 20 20 ag + x);.
1c060 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65 2b }else if( size+
1c061 70 63 20 3e 20 75 73 61 62 6c 65 53 69 7a 65 20 pc > usableSize
1c062 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 ){. ret
1c063 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
1c064 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 PT_BKPT;.
1c065 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1c066 20 20 2f 2a 20 54 68 65 20 73 6c 6f 74 20 72 65 /* The slot re
1c067 6d 61 69 6e 73 20 6f 6e 20 74 68 65 20 66 72 65 mains on the fre
1c068 65 2d 6c 69 73 74 2e 20 52 65 64 75 63 65 20 69 e-list. Reduce i
1c069 74 73 20 73 69 7a 65 20 74 6f 20 61 63 63 6f 75 ts size to accou
1c06a 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 nt. **
1c06b 66 6f 72 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 for the portion
1c06c 75 73 65 64 20 62 79 20 74 68 65 20 6e 65 77 20 used by the new
1c06d 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 2a 2f 0a 20 allocation. */.
1c06e 20 20 20 20 20 20 20 20 20 70 75 74 32 62 79 74 put2byt
1c06f 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 2c 20 78 e(&data[pc+2], x
1c070 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1c071 20 20 20 20 20 2a 70 49 64 78 20 3d 20 70 63 20 *pIdx = pc
1c072 2b 20 78 3b 0a 20 20 20 20 20 20 20 20 72 65 74 + x;. ret
1c073 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
1c074 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1c075 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 .. /* Check to
1c076 6d 61 6b 65 20 73 75 72 65 20 74 68 65 72 65 20 make sure there
1c077 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 is enough space
1c078 69 6e 20 74 68 65 20 67 61 70 20 74 6f 20 73 61 in the gap to sa
1c079 74 69 73 66 79 0a 20 20 2a 2a 20 74 68 65 20 61 tisfy. ** the a
1c07a 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66 20 6e llocation. If n
1c07b 6f 74 2c 20 64 65 66 72 61 67 6d 65 6e 74 2e 0a ot, defragment..
1c07c 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 */. testcase(
1c07d 20 67 61 70 2b 32 2b 6e 42 79 74 65 3d 3d 74 6f gap+2+nByte==to
1c07e 70 20 29 3b 0a 20 20 69 66 28 20 67 61 70 2b 32 p );. if( gap+2
1c07f 2b 6e 42 79 74 65 3e 74 6f 70 20 29 7b 0a 20 20 +nByte>top ){.
1c080 20 20 72 63 20 3d 20 64 65 66 72 61 67 6d 65 6e rc = defragmen
1c081 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 tPage(pPage);.
1c082 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
1c083 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 20 3d 20 n rc;. top =
1c084 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 6f 28 get2byteNotZero(
1c085 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 &data[hdr+5]);.
1c086 20 20 20 61 73 73 65 72 74 28 20 67 61 70 2b 6e assert( gap+n
1c087 42 79 74 65 3c 3d 74 6f 70 20 29 3b 0a 20 20 7d Byte<=top );. }
1c088 0a 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 ... /* Allocate
1c089 20 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 memory from the
1c08a 20 67 61 70 20 69 6e 20 62 65 74 77 65 65 6e 20 gap in between
1c08b 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 the cell pointer
1c08c 20 61 72 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20 array. ** and
1c08d 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
1c08e 20 61 72 65 61 2e 20 20 54 68 65 20 62 74 72 65 area. The btre
1c08f 65 49 6e 69 74 50 61 67 65 28 29 20 63 61 6c 6c eInitPage() call
1c090 20 68 61 73 20 61 6c 72 65 61 64 79 0a 20 20 2a has already. *
1c091 2a 20 76 61 6c 69 64 61 74 65 64 20 74 68 65 20 * validated the
1c092 66 72 65 65 6c 69 73 74 2e 20 20 47 69 76 65 6e freelist. Given
1c093 20 74 68 61 74 20 74 68 65 20 66 72 65 65 6c 69 that the freeli
1c094 73 74 20 69 73 20 76 61 6c 69 64 2c 20 74 68 65 st is valid, the
1c095 72 65 0a 20 20 2a 2a 20 69 73 20 6e 6f 20 77 61 re. ** is no wa
1c096 79 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 y that the alloc
1c097 61 74 69 6f 6e 20 63 61 6e 20 65 78 74 65 6e 64 ation can extend
1c098 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66 20 off the end of
1c099 74 68 65 20 70 61 67 65 2e 0a 20 20 2a 2a 20 54 the page.. ** T
1c09a 68 65 20 61 73 73 65 72 74 28 29 20 62 65 6c 6f he assert() belo
1c09b 77 20 76 65 72 69 66 69 65 73 20 74 68 65 20 70 w verifies the p
1c09c 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 revious sentence
1c09d 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 2d 3d 20 .. */. top -=
1c09e 6e 42 79 74 65 3b 0a 20 20 70 75 74 32 62 79 74 nByte;. put2byt
1c09f 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 e(&data[hdr+5],
1c0a0 74 6f 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 top);. assert(
1c0a1 74 6f 70 2b 6e 42 79 74 65 20 3c 3d 20 28 69 6e top+nByte <= (in
1c0a2 74 29 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 t)pPage->pBt->us
1c0a3 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 2a 70 ableSize );. *p
1c0a4 49 64 78 20 3d 20 74 6f 70 3b 0a 20 20 72 65 74 Idx = top;. ret
1c0a5 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1c0a6 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
1c0a7 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 section of the
1c0a8 70 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f 20 pPage->aData to
1c0a9 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a the freelist..**
1c0aa 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20 The first byte
1c0ab 6f 66 20 74 68 65 20 6e 65 77 20 66 72 65 65 20 of the new free
1c0ac 62 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d 3e block is pPage->
1c0ad 61 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a 20 aDisk[start].**
1c0ae 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 and the size of
1c0af 74 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73 69 the block is "si
1c0b0 7a 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a ze" bytes..**.**
1c0b1 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66 66 Most of the eff
1c0b2 6f 72 74 20 68 65 72 65 20 69 73 20 69 6e 76 6f ort here is invo
1c0b3 6c 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69 6e lved in coalesin
1c0b4 67 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 72 g adjacent.** fr
1c0b5 65 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 61 ee blocks into a
1c0b6 20 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65 65 single big free
1c0b7 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 block..*/.stati
1c0b8 63 20 69 6e 74 20 66 72 65 65 53 70 61 63 65 28 c int freeSpace(
1c0b9 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
1c0ba 69 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20 73 int start, int s
1c0bb 69 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 ize){. int addr
1c0bc 2c 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a 20 , pbegin, hdr;.
1c0bd 20 69 6e 74 20 69 4c 61 73 74 3b 20 20 20 20 20 int iLast;
1c0be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c0bf 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 6f /* Largest po
1c0c0 73 73 69 62 6c 65 20 66 72 65 65 62 6c 6f 63 6b ssible freeblock
1c0c1 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 75 6e 73 offset */. uns
1c0c2 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 igned char *data
1c0c3 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b = pPage->aData;
1c0c4 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
1c0c5 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 e->pBt!=0 );. a
1c0c6 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
1c0c7 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
1c0c8 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
1c0c9 3b 0a 20 20 61 73 73 65 72 74 28 20 73 74 61 72 ;. assert( star
1c0ca 74 3e 3d 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 t>=pPage->hdrOff
1c0cb 73 65 74 2b 36 2b 70 50 61 67 65 2d 3e 63 68 69 set+6+pPage->chi
1c0cc 6c 64 50 74 72 53 69 7a 65 20 29 3b 0a 20 20 61 ldPtrSize );. a
1c0cd 73 73 65 72 74 28 20 28 73 74 61 72 74 20 2b 20 ssert( (start +
1c0ce 73 69 7a 65 29 20 3c 3d 20 28 69 6e 74 29 70 50 size) <= (int)pP
1c0cf 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
1c0d0 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 Size );. assert
1c0d1 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1c0d2 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
1c0d3 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
1c0d4 65 72 74 28 20 73 69 7a 65 3e 3d 30 20 29 3b 20 ert( size>=0 );
1c0d5 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c /* Minimum cel
1c0d6 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 0a l size is 4 */..
1c0d7 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42 74 if( pPage->pBt
1c0d8 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 ->btsFlags & BTS
1c0d9 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 29 _SECURE_DELETE )
1c0da 7b 0a 20 20 20 20 2f 2a 20 4f 76 65 72 77 72 69 {. /* Overwri
1c0db 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f 72 te deleted infor
1c0dc 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f mation with zero
1c0dd 73 20 77 68 65 6e 20 74 68 65 20 73 65 63 75 72 s when the secur
1c0de 65 5f 64 65 6c 65 74 65 0a 20 20 20 20 2a 2a 20 e_delete. **
1c0df 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 option is enable
1c0e0 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 d */. memset(
1c0e1 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c &data[start], 0,
1c0e2 20 73 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f size);. }.. /
1c0e3 2a 20 41 64 64 20 74 68 65 20 73 70 61 63 65 20 * Add the space
1c0e4 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 6c 69 back into the li
1c0e5 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 66 72 65 nked list of fre
1c0e6 65 62 6c 6f 63 6b 73 2e 20 20 4e 6f 74 65 20 74 eblocks. Note t
1c0e7 68 61 74 0a 20 20 2a 2a 20 65 76 65 6e 20 74 68 hat. ** even th
1c0e8 6f 75 67 68 20 74 68 65 20 66 72 65 65 62 6c 6f ough the freeblo
1c0e9 63 6b 20 6c 69 73 74 20 77 61 73 20 63 68 65 63 ck list was chec
1c0ea 6b 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 ked by btreeInit
1c0eb 50 61 67 65 28 29 2c 0a 20 20 2a 2a 20 62 74 72 Page(),. ** btr
1c0ec 65 65 49 6e 69 74 50 61 67 65 28 29 20 64 69 64 eeInitPage() did
1c0ed 20 6e 6f 74 20 64 65 74 65 63 74 20 6f 76 65 72 not detect over
1c0ee 6c 61 70 70 69 6e 67 20 63 65 6c 6c 73 20 6f 72 lapping cells or
1c0ef 0a 20 20 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73 . ** freeblocks
1c0f0 20 74 68 61 74 20 6f 76 65 72 6c 61 70 70 65 64 that overlapped
1c0f1 20 63 65 6c 6c 73 2e 20 20 20 4e 6f 72 20 64 6f cells. Nor do
1c0f2 65 73 20 69 74 20 64 65 74 65 63 74 20 77 68 65 es it detect whe
1c0f3 6e 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 20 n the. ** cell
1c0f4 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 65 78 63 content area exc
1c0f5 65 65 64 73 20 74 68 65 20 76 61 6c 75 65 20 69 eeds the value i
1c0f6 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 n the page heade
1c0f7 72 2e 20 20 49 66 20 74 68 65 73 65 0a 20 20 2a r. If these. *
1c0f8 2a 20 73 69 74 75 61 74 69 6f 6e 73 20 61 72 69 * situations ari
1c0f9 73 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71 75 se, then subsequ
1c0fa 65 6e 74 20 69 6e 73 65 72 74 20 6f 70 65 72 61 ent insert opera
1c0fb 74 69 6f 6e 73 20 6d 69 67 68 74 20 63 6f 72 72 tions might corr
1c0fc 75 70 74 0a 20 20 2a 2a 20 74 68 65 20 66 72 65 upt. ** the fre
1c0fd 65 6c 69 73 74 2e 20 20 53 6f 20 77 65 20 64 6f elist. So we do
1c0fe 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 66 need to check f
1c0ff 6f 72 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 68 or corruption wh
1c100 69 6c 65 20 73 63 61 6e 6e 69 6e 67 0a 20 20 2a ile scanning. *
1c101 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a * the freelist..
1c102 20 20 2a 2f 0a 20 20 68 64 72 20 3d 20 70 50 61 */. hdr = pPa
1c103 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 ge->hdrOffset;.
1c104 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 3b addr = hdr + 1;
1c105 0a 20 20 69 4c 61 73 74 20 3d 20 70 50 61 67 65 . iLast = pPage
1c106 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
1c107 65 20 2d 20 34 3b 0a 20 20 61 73 73 65 72 74 28 e - 4;. assert(
1c108 20 73 74 61 72 74 3c 3d 69 4c 61 73 74 20 29 3b start<=iLast );
1c109 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69 . while( (pbegi
1c10a 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 n = get2byte(&da
1c10b 74 61 5b 61 64 64 72 5d 29 29 3c 73 74 61 72 74 ta[addr]))<start
1c10c 20 26 26 20 70 62 65 67 69 6e 3e 30 20 29 7b 0a && pbegin>0 ){.
1c10d 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 3c 61 if( pbegin<a
1c10e 64 64 72 2b 34 20 29 7b 0a 20 20 20 20 20 20 72 ddr+4 ){. r
1c10f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
1c110 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
1c111 0a 20 20 20 20 61 64 64 72 20 3d 20 70 62 65 67 . addr = pbeg
1c112 69 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 62 in;. }. if( pb
1c113 65 67 69 6e 3e 69 4c 61 73 74 20 29 7b 0a 20 20 egin>iLast ){.
1c114 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1c115 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1c116 7d 0a 20 20 61 73 73 65 72 74 28 20 70 62 65 67 }. assert( pbeg
1c117 69 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65 67 69 in>addr || pbegi
1c118 6e 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32 62 79 n==0 );. put2by
1c119 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 te(&data[addr],
1c11a 73 74 61 72 74 29 3b 0a 20 20 70 75 74 32 62 79 start);. put2by
1c11b 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c te(&data[start],
1c11c 20 70 62 65 67 69 6e 29 3b 0a 20 20 70 75 74 32 pbegin);. put2
1c11d 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 byte(&data[start
1c11e 2b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 70 50 +2], size);. pP
1c11f 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 70 50 61 age->nFree = pPa
1c120 67 65 2d 3e 6e 46 72 65 65 20 2b 20 28 75 31 36 ge->nFree + (u16
1c121 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 6f 61 )size;.. /* Coa
1c122 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66 lesce adjacent f
1c123 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20 ree blocks */.
1c124 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 3b 0a addr = hdr + 1;.
1c125 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e while( (pbegin
1c126 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
1c127 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20 a[addr]))>0 ){.
1c128 20 20 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 73 int pnext, ps
1c129 69 7a 65 2c 20 78 3b 0a 20 20 20 20 61 73 73 65 ize, x;. asse
1c12a 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 rt( pbegin>addr
1c12b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1c12c 62 65 67 69 6e 20 3c 3d 20 28 69 6e 74 29 70 50 begin <= (int)pP
1c12d 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
1c12e 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 70 6e Size-4 );. pn
1c12f 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 28 26 ext = get2byte(&
1c130 64 61 74 61 5b 70 62 65 67 69 6e 5d 29 3b 0a 20 data[pbegin]);.
1c131 20 20 20 70 73 69 7a 65 20 3d 20 67 65 74 32 62 psize = get2b
1c132 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e yte(&data[pbegin
1c133 2b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 62 +2]);. if( pb
1c134 65 67 69 6e 20 2b 20 70 73 69 7a 65 20 2b 20 33 egin + psize + 3
1c135 20 3e 3d 20 70 6e 65 78 74 20 26 26 20 70 6e 65 >= pnext && pne
1c136 78 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e xt>0 ){. in
1c137 74 20 66 72 61 67 20 3d 20 70 6e 65 78 74 20 2d t frag = pnext -
1c138 20 28 70 62 65 67 69 6e 2b 70 73 69 7a 65 29 3b (pbegin+psize);
1c139 0a 20 20 20 20 20 20 69 66 28 20 28 66 72 61 67 . if( (frag
1c13a 3c 30 29 20 7c 7c 20 28 66 72 61 67 3e 28 69 6e <0) || (frag>(in
1c13b 74 29 64 61 74 61 5b 68 64 72 2b 37 5d 29 20 29 t)data[hdr+7]) )
1c13c 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
1c13d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1c13e 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 BKPT;. }.
1c13f 20 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 data[hdr+7]
1c140 2d 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20 20 -= (u8)frag;.
1c141 20 20 20 78 20 3d 20 67 65 74 32 62 79 74 65 28 x = get2byte(
1c142 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a 20 &data[pnext]);.
1c143 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 put2byte(&d
1c144 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 3b ata[pbegin], x);
1c145 0a 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 74 . x = pnext
1c146 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74 + get2byte(&dat
1c147 61 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 62 a[pnext+2]) - pb
1c148 65 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74 32 egin;. put2
1c149 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 byte(&data[pbegi
1c14a 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d 65 n+2], x);. }e
1c14b 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 20 lse{. addr
1c14c 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d 0a = pbegin;. }.
1c14d 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
1c14e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
1c14f 65 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 61 ea begins with a
1c150 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d 6f freeblock, remo
1c151 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 ve it. */. if(
1c152 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 74 data[hdr+1]==dat
1c153 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 61 a[hdr+5] && data
1c154 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 64 [hdr+2]==data[hd
1c155 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 r+6] ){. int
1c156 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e 20 top;. pbegin
1c157 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
1c158 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d 65 [hdr+1]);. me
1c159 6d 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b 31 mcpy(&data[hdr+1
1c15a 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 5d ], &data[pbegin]
1c15b 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d 20 , 2);. top =
1c15c 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
1c15d 64 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79 74 dr+5]) + get2byt
1c15e 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 e(&data[pbegin+2
1c15f 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 ]);. put2byte
1c160 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74 (&data[hdr+5], t
1c161 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 op);. }. asser
1c162 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
1c163 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
1c164 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
1c165 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1c166 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64 ;.}../*.** Decod
1c167 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 e the flags byte
1c168 20 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65 (the first byte
1c169 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 20 of the header)
1c16a 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e for a page.** an
1c16b 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 65 d initialize fie
1c16c 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 lds of the MemPa
1c16d 67 65 20 73 74 72 75 63 74 75 72 65 20 61 63 63 ge structure acc
1c16e 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 ordingly..**.**
1c16f 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 Only the followi
1c170 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 ng combinations
1c171 61 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 are supported.
1c172 41 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 65 Anything differe
1c173 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 nt.** indicates
1c174 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 a corrupt databa
1c175 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 se files:.**.**
1c176 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 4f PTF_ZERO
1c177 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 DATA.**
1c178 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 50 PTF_ZERODATA | P
1c179 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 20 TF_LEAF.**
1c17a 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 PTF_LEAFDATA
1c17b 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a 20 | PTF_INTKEY.**
1c17c 20 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 PTF_LEAF
1c17d 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 DATA | PTF_INTKE
1c17e 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f 0a Y | PTF_LEAF.*/.
1c17f 73 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f 64 static int decod
1c180 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 2a eFlags(MemPage *
1c181 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 42 pPage, int flagB
1c182 79 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 yte){. BtShared
1c183 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 20 *pBt; /* A
1c184 63 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e 70 copy of pPage->p
1c185 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 Bt */.. assert(
1c186 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
1c187 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f 3d t==(pPage->pgno=
1c188 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 3b =1 ? 100 : 0) );
1c189 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1c18a 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
1c18b 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
1c18c 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 61 );. pPage->lea
1c18d 66 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 74 f = (u8)(flagByt
1c18e 65 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 20 e>>3); assert(
1c18f 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33 PTF_LEAF == 1<<3
1c190 20 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 26 );. flagByte &
1c191 3d 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70 = ~PTF_LEAF;. p
1c192 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 Page->childPtrSi
1c193 7a 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e ze = 4-4*pPage->
1c194 6c 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 50 leaf;. pBt = pP
1c195 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 age->pBt;. if(
1c196 66 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c flagByte==(PTF_L
1c197 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e EAFDATA | PTF_IN
1c198 54 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 61 TKEY) ){. pPa
1c199 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a ge->intKey = 1;.
1c19a 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61 pPage->hasDa
1c19b 74 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66 ta = pPage->leaf
1c19c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 ;. pPage->max
1c19d 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 Local = pBt->max
1c19e 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d Leaf;. pPage-
1c19f 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d >minLocal = pBt-
1c1a0 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73 >minLeaf;. }els
1c1a1 65 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d e if( flagByte==
1c1a2 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a PTF_ZERODATA ){.
1c1a3 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 pPage->intKe
1c1a4 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 y = 0;. pPage
1c1a5 2d 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a 20 ->hasData = 0;.
1c1a6 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 pPage->maxLoc
1c1a7 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 al = pBt->maxLoc
1c1a8 61 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d al;. pPage->m
1c1a9 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d inLocal = pBt->m
1c1aa 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65 inLocal;. }else
1c1ab 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1c1ac 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1c1ad 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6d ;. }. pPage->m
1c1ae 61 78 31 62 79 74 65 50 61 79 6c 6f 61 64 20 3d ax1bytePayload =
1c1af 20 70 42 74 2d 3e 6d 61 78 31 62 79 74 65 50 61 pBt->max1bytePa
1c1b0 79 6c 6f 61 64 3b 0a 20 20 72 65 74 75 72 6e 20 yload;. return
1c1b1 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
1c1b2 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 .** Initialize t
1c1b3 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 he auxiliary inf
1c1b4 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64 ormation for a d
1c1b5 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a isk block..**.**
1c1b6 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
1c1b7 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 K on success. I
1c1b8 66 20 77 65 20 73 65 65 20 74 68 61 74 20 74 68 f we see that th
1c1b9 65 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e e page does.** n
1c1ba 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c ot contain a wel
1c1bb 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 l-formed databas
1c1bc 65 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74 e page, then ret
1c1bd 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 urn .** SQLITE_C
1c1be 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 ORRUPT. Note th
1c1bf 61 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53 at a return of S
1c1c0 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f QLITE_OK does no
1c1c1 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 t.** guarantee t
1c1c2 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 hat the page is
1c1c3 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 well-formed. It
1c1c4 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 only shows that
1c1c5 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f .** we failed to
1c1c6 20 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 detect any corr
1c1c7 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 uption..*/.stati
1c1c8 63 20 69 6e 74 20 62 74 72 65 65 49 6e 69 74 50 c int btreeInitP
1c1c9 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 age(MemPage *pPa
1c1ca 67 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 ge){.. assert(
1c1cb 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b pPage->pBt!=0 );
1c1cc 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1c1cd 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
1c1ce 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
1c1cf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
1c1d0 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 age->pgno==sqlit
1c1d1 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 e3PagerPagenumbe
1c1d2 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 r(pPage->pDbPage
1c1d3 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
1c1d4 50 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 Page == sqlite3P
1c1d5 61 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 agerGetExtra(pPa
1c1d6 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
1c1d7 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
1c1d8 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 >aData == sqlite
1c1d9 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 3PagerGetData(pP
1c1da 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
1c1db 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e .. if( !pPage->
1c1dc 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 isInit ){. u1
1c1dd 36 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 6 pc;
1c1de 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 /* Address of a
1c1df 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 freeblock withi
1c1e0 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d n pPage->aData[]
1c1e1 20 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20 */. u8 hdr;
1c1e2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 /* Of
1c1e3 66 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e fset to beginnin
1c1e4 67 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 g of page header
1c1e5 20 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61 */. u8 *data
1c1e6 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 ; /* Eq
1c1e7 75 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 ual to pPage->aD
1c1e8 61 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 ata */. BtSha
1c1e9 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 red *pBt;
1c1ea 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 /* The main btr
1c1eb 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a ee structure */.
1c1ec 20 20 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 int usableSi
1c1ed 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 ze; /* Amount
1c1ee 20 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 of usable space
1c1ef 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f on each page */
1c1f0 0a 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 . u16 cellOff
1c1f1 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 set; /* Offse
1c1f2 74 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 t from start of
1c1f3 70 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 page to first ce
1c1f4 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 ll pointer */.
1c1f5 20 20 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 int nFree;
1c1f6 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1c1f7 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f f unused bytes o
1c1f8 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 n the page */.
1c1f9 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 int top;
1c1fa 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 /* First by
1c1fb 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 te of the cell c
1c1fc 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 ontent area */.
1c1fd 20 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 int iCellFirs
1c1fe 74 3b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 t; /* First a
1c1ff 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 6f 72 llowable cell or
1c200 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 freeblock offse
1c201 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 t */. int iCe
1c202 6c 6c 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c llLast; /* L
1c203 61 73 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c ast possible cel
1c204 6c 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f l or freeblock o
1c205 66 66 73 65 74 20 2a 2f 0a 0a 20 20 20 20 70 42 ffset */.. pB
1c206 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a t = pPage->pBt;.
1c207 0a 20 20 20 20 68 64 72 20 3d 20 70 50 61 67 65 . hdr = pPage
1c208 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 ->hdrOffset;.
1c209 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
1c20a 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 65 Data;. if( de
1c20b 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c codeFlags(pPage,
1c20c 20 64 61 74 61 5b 68 64 72 5d 29 20 29 20 72 65 data[hdr]) ) re
1c20d 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1c20e 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61 73 UPT_BKPT;. as
1c20f 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 sert( pBt->pageS
1c210 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d ize>=512 && pBt-
1c211 3e 70 61 67 65 53 69 7a 65 3c 3d 36 35 35 33 36 >pageSize<=65536
1c212 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d );. pPage->m
1c213 61 73 6b 50 61 67 65 20 3d 20 28 75 31 36 29 28 askPage = (u16)(
1c214 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 pBt->pageSize -
1c215 31 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 1);. pPage->n
1c216 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 Overflow = 0;.
1c217 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 usableSize = p
1c218 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a Bt->usableSize;.
1c219 20 20 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f pPage->cellO
1c21a 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66 73 ffset = cellOffs
1c21b 65 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 et = hdr + 12 -
1c21c 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 4*pPage->leaf;.
1c21d 20 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 45 pPage->aDataE
1c21e 6e 64 20 3d 20 26 64 61 74 61 5b 75 73 61 62 6c nd = &data[usabl
1c21f 65 53 69 7a 65 5d 3b 0a 20 20 20 20 70 50 61 67 eSize];. pPag
1c220 65 2d 3e 61 43 65 6c 6c 49 64 78 20 3d 20 26 64 e->aCellIdx = &d
1c221 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 5d 3b ata[cellOffset];
1c222 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 . top = get2b
1c223 79 74 65 4e 6f 74 5a 65 72 6f 28 26 64 61 74 61 yteNotZero(&data
1c224 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70 50 [hdr+5]);. pP
1c225 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65 74 age->nCell = get
1c226 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
1c227 33 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 3]);. if( pPa
1c228 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45 4c ge->nCell>MX_CEL
1c229 4c 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 L(pBt) ){.
1c22a 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c 73 /* To many cells
1c22b 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 61 for a single pa
1c22c 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 6d 75 ge. The page mu
1c22d 73 74 20 62 65 20 63 6f 72 72 75 70 74 20 2a 2f st be corrupt */
1c22e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
1c22f 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
1c230 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73 T;. }. tes
1c231 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 43 tcase( pPage->nC
1c232 65 6c 6c 3d 3d 4d 58 5f 43 45 4c 4c 28 70 42 74 ell==MX_CELL(pBt
1c233 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 6d ) );.. /* A m
1c234 61 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 alformed databas
1c235 65 20 70 61 67 65 20 6d 69 67 68 74 20 63 61 75 e page might cau
1c236 73 65 20 75 73 20 74 6f 20 72 65 61 64 20 70 61 se us to read pa
1c237 73 74 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a st the end. *
1c238 2a 20 6f 66 20 70 61 67 65 20 77 68 65 6e 20 70 * of page when p
1c239 61 72 73 69 6e 67 20 61 20 63 65 6c 6c 2e 20 20 arsing a cell.
1c23a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
1c23b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
1c23c 63 6b 20 6f 66 20 63 6f 64 65 20 63 68 65 63 6b ck of code check
1c23d 73 20 65 61 72 6c 79 20 74 6f 20 73 65 65 20 69 s early to see i
1c23e 66 20 61 20 63 65 6c 6c 20 65 78 74 65 6e 64 73 f a cell extends
1c23f 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 65 . ** past the
1c240 20 65 6e 64 20 6f 66 20 61 20 70 61 67 65 20 62 end of a page b
1c241 6f 75 6e 64 61 72 79 20 61 6e 64 20 63 61 75 73 oundary and caus
1c242 65 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 es SQLITE_CORRUP
1c243 54 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 T to be . **
1c244 72 65 74 75 72 6e 65 64 20 69 66 20 69 74 20 64 returned if it d
1c245 6f 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 oes.. */.
1c246 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c iCellFirst = cel
1c247 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 lOffset + 2*pPag
1c248 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 43 e->nCell;. iC
1c249 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 ellLast = usable
1c24a 53 69 7a 65 20 2d 20 34 3b 0a 23 69 66 20 64 65 Size - 4;.#if de
1c24b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 fined(SQLITE_ENA
1c24c 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c BLE_OVERSIZE_CEL
1c24d 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 7b 0a 20 L_CHECK). {.
1c24e 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 int i;
1c24f 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
1c250 69 6e 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f into the cell po
1c251 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 inter array */.
1c252 20 20 20 20 20 69 6e 74 20 73 7a 3b 20 20 20 20 int sz;
1c253 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1c254 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 20 f a cell */..
1c255 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c if( !pPage->l
1c256 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 2d eaf ) iCellLast-
1c257 2d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 -;. for(i=0
1c258 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ; i<pPage->nCell
1c259 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; i++){.
1c25a 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 pc = get2byte(&d
1c25b 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 2b 69 ata[cellOffset+i
1c25c 2a 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 *2]);. te
1c25d 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c stcase( pc==iCel
1c25e 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 lFirst );.
1c25f 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d testcase( pc==
1c260 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 iCellLast );.
1c261 20 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c if( pc<iCel
1c262 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65 lFirst || pc>iCe
1c263 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 llLast ){.
1c264 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1c265 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
1c266 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1c267 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 sz = cellSizeP
1c268 74 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b tr(pPage, &data[
1c269 70 63 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 pc]);. te
1c26a 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 75 stcase( pc+sz==u
1c26b 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 sableSize );.
1c26c 20 20 20 20 20 69 66 28 20 70 63 2b 73 7a 3e 75 if( pc+sz>u
1c26d 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 sableSize ){.
1c26e 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
1c26f 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
1c270 54 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 T;. }.
1c271 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 }. if( !
1c272 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 43 pPage->leaf ) iC
1c273 65 6c 6c 4c 61 73 74 2b 2b 3b 0a 20 20 20 20 7d ellLast++;. }
1c274 20 20 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f .#endif.. /
1c275 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f * Compute the to
1c276 74 61 6c 20 66 72 65 65 20 73 70 61 63 65 20 6f tal free space o
1c277 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 n the page */.
1c278 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 pc = get2byte(
1c279 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 &data[hdr+1]);.
1c27a 20 20 20 6e 46 72 65 65 20 3d 20 64 61 74 61 5b nFree = data[
1c27b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 3b 0a 20 20 hdr+7] + top;.
1c27c 20 20 77 68 69 6c 65 28 20 70 63 3e 30 20 29 7b while( pc>0 ){
1c27d 0a 20 20 20 20 20 20 75 31 36 20 6e 65 78 74 2c . u16 next,
1c27e 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 size;. if(
1c27f 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c pc<iCellFirst |
1c280 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 | pc>iCellLast )
1c281 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 {. /* Sta
1c282 72 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b rt of free block
1c283 20 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 is off the page
1c284 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 */. retu
1c285 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
1c286 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d T_BKPT; . }
1c287 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 . next = ge
1c288 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 5d t2byte(&data[pc]
1c289 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 );. size =
1c28a 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 get2byte(&data[p
1c28b 63 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 c+2]);. if(
1c28c 20 28 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74 (next>0 && next
1c28d 3c 3d 70 63 2b 73 69 7a 65 2b 33 29 20 7c 7c 20 <=pc+size+3) ||
1c28e 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 pc+size>usableSi
1c28f 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a ze ){. /*
1c290 20 46 72 65 65 20 62 6c 6f 63 6b 73 20 6d 75 73 Free blocks mus
1c291 74 20 62 65 20 69 6e 20 61 73 63 65 6e 64 69 6e t be in ascendin
1c292 67 20 6f 72 64 65 72 2e 20 41 6e 64 20 74 68 65 g order. And the
1c293 20 6c 61 73 74 20 62 79 74 65 20 6f 66 0a 20 20 last byte of.
1c294 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 72 65 ** the fre
1c295 65 2d 62 6c 6f 63 6b 20 6d 75 73 74 20 6c 69 65 e-block must lie
1c296 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
1c297 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 page. */.
1c298 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1c299 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a _CORRUPT_BKPT; .
1c29a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 46 }. nF
1c29b 72 65 65 20 3d 20 6e 46 72 65 65 20 2b 20 73 69 ree = nFree + si
1c29c 7a 65 3b 0a 20 20 20 20 20 20 70 63 20 3d 20 6e ze;. pc = n
1c29d 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 ext;. }..
1c29e 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 /* At this point
1c29f 2c 20 6e 46 72 65 65 20 63 6f 6e 74 61 69 6e 73 , nFree contains
1c2a0 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 the sum of the
1c2a1 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 73 74 offset to the st
1c2a2 61 72 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 art. ** of th
1c2a3 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 20 61 e cell-content a
1c2a4 72 65 61 20 70 6c 75 73 20 74 68 65 20 6e 75 6d rea plus the num
1c2a5 62 65 72 20 6f 66 20 66 72 65 65 20 62 79 74 65 ber of free byte
1c2a6 73 20 77 69 74 68 69 6e 0a 20 20 20 20 2a 2a 20 s within. **
1c2a7 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 the cell-content
1c2a8 20 61 72 65 61 2e 20 49 66 20 74 68 69 73 20 69 area. If this i
1c2a9 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 s greater than t
1c2aa 68 65 20 75 73 61 62 6c 65 2d 73 69 7a 65 0a 20 he usable-size.
1c2ab 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 ** of the pag
1c2ac 65 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 e, then the page
1c2ad 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 must be corrupt
1c2ae 65 64 2e 20 54 68 69 73 20 63 68 65 63 6b 20 61 ed. This check a
1c2af 6c 73 6f 0a 20 20 20 20 2a 2a 20 73 65 72 76 65 lso. ** serve
1c2b0 73 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 s to verify that
1c2b1 20 74 68 65 20 6f 66 66 73 65 74 20 74 6f 20 74 the offset to t
1c2b2 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 he start of the
1c2b3 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 0a 20 20 20 cell-content.
1c2b4 20 2a 2a 20 61 72 65 61 2c 20 61 63 63 6f 72 64 ** area, accord
1c2b5 69 6e 67 20 74 6f 20 74 68 65 20 70 61 67 65 20 ing to the page
1c2b6 68 65 61 64 65 72 2c 20 6c 69 65 73 20 77 69 74 header, lies wit
1c2b7 68 69 6e 20 74 68 65 20 70 61 67 65 2e 0a 20 20 hin the page..
1c2b8 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 46 72 */. if( nFr
1c2b9 65 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b ee>usableSize ){
1c2ba 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
1c2bb 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
1c2bc 54 3b 20 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 T; . }. pP
1c2bd 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 28 75 31 age->nFree = (u1
1c2be 36 29 28 6e 46 72 65 65 20 2d 20 69 43 65 6c 6c 6)(nFree - iCell
1c2bf 46 69 72 73 74 29 3b 0a 20 20 20 20 70 50 61 67 First);. pPag
1c2c0 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 e->isInit = 1;.
1c2c1 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
1c2c2 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
1c2c3 53 65 74 20 75 70 20 61 20 72 61 77 20 70 61 67 Set up a raw pag
1c2c4 65 20 73 6f 20 74 68 61 74 20 69 74 20 6c 6f 6f e so that it loo
1c2c5 6b 73 20 6c 69 6b 65 20 61 20 64 61 74 61 62 61 ks like a databa
1c2c6 73 65 20 70 61 67 65 20 68 6f 6c 64 69 6e 67 0a se page holding.
1c2c7 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 73 2e 0a 2a ** no entries..*
1c2c8 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 7a 65 /.static void ze
1c2c9 72 6f 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a roPage(MemPage *
1c2ca 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 73 pPage, int flags
1c2cb 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 ){. unsigned ch
1c2cc 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61 67 65 ar *data = pPage
1c2cd 2d 3e 61 44 61 74 61 3b 0a 20 20 42 74 53 68 61 ->aData;. BtSha
1c2ce 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 red *pBt = pPage
1c2cf 2d 3e 70 42 74 3b 0a 20 20 75 38 20 68 64 72 20 ->pBt;. u8 hdr
1c2d0 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 = pPage->hdrOffs
1c2d1 65 74 3b 0a 20 20 75 31 36 20 66 69 72 73 74 3b et;. u16 first;
1c2d2 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
1c2d3 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 te3PagerPagenumb
1c2d4 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 er(pPage->pDbPag
1c2d5 65 29 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 e)==pPage->pgno
1c2d6 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
1c2d7 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 ite3PagerGetExtr
1c2d8 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 a(pPage->pDbPage
1c2d9 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 ) == (void*)pPag
1c2da 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 e );. assert( s
1c2db 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
1c2dc 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 ta(pPage->pDbPag
1c2dd 65 29 20 3d 3d 20 64 61 74 61 20 29 3b 0a 20 20 e) == data );.
1c2de 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
1c2df 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
1c2e0 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
1c2e1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
1c2e2 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1c2e3 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1c2e4 20 69 66 28 20 70 42 74 2d 3e 62 74 73 46 6c 61 if( pBt->btsFla
1c2e5 67 73 20 26 20 42 54 53 5f 53 45 43 55 52 45 5f gs & BTS_SECURE_
1c2e6 44 45 4c 45 54 45 20 29 7b 0a 20 20 20 20 6d 65 DELETE ){. me
1c2e7 6d 73 65 74 28 26 64 61 74 61 5b 68 64 72 5d 2c mset(&data[hdr],
1c2e8 20 30 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 0, pBt->usableS
1c2e9 69 7a 65 20 2d 20 68 64 72 29 3b 0a 20 20 7d 0a ize - hdr);. }.
1c2ea 20 20 64 61 74 61 5b 68 64 72 5d 20 3d 20 28 63 data[hdr] = (c
1c2eb 68 61 72 29 66 6c 61 67 73 3b 0a 20 20 66 69 72 har)flags;. fir
1c2ec 73 74 20 3d 20 68 64 72 20 2b 20 38 20 2b 20 34 st = hdr + 8 + 4
1c2ed 2a 28 28 66 6c 61 67 73 26 50 54 46 5f 4c 45 41 *((flags&PTF_LEA
1c2ee 46 29 3d 3d 30 20 3f 31 3a 30 29 3b 0a 20 20 6d F)==0 ?1:0);. m
1c2ef 65 6d 73 65 74 28 26 64 61 74 61 5b 68 64 72 2b emset(&data[hdr+
1c2f0 31 5d 2c 20 30 2c 20 34 29 3b 0a 20 20 64 61 74 1], 0, 4);. dat
1c2f1 61 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 a[hdr+7] = 0;.
1c2f2 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 put2byte(&data[h
1c2f3 64 72 2b 35 5d 2c 20 70 42 74 2d 3e 75 73 61 62 dr+5], pBt->usab
1c2f4 6c 65 53 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 leSize);. pPage
1c2f5 2d 3e 6e 46 72 65 65 20 3d 20 28 75 31 36 29 28 ->nFree = (u16)(
1c2f6 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
1c2f7 2d 20 66 69 72 73 74 29 3b 0a 20 20 64 65 63 6f - first);. deco
1c2f8 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 66 deFlags(pPage, f
1c2f9 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e lags);. pPage->
1c2fa 68 64 72 4f 66 66 73 65 74 20 3d 20 68 64 72 3b hdrOffset = hdr;
1c2fb 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 . pPage->cellOf
1c2fc 66 73 65 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 fset = first;.
1c2fd 70 50 61 67 65 2d 3e 61 44 61 74 61 45 6e 64 20 pPage->aDataEnd
1c2fe 3d 20 26 64 61 74 61 5b 70 42 74 2d 3e 75 73 61 = &data[pBt->usa
1c2ff 62 6c 65 53 69 7a 65 5d 3b 0a 20 20 70 50 61 67 bleSize];. pPag
1c300 65 2d 3e 61 43 65 6c 6c 49 64 78 20 3d 20 26 64 e->aCellIdx = &d
1c301 61 74 61 5b 66 69 72 73 74 5d 3b 0a 20 20 70 50 ata[first];. pP
1c302 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d age->nOverflow =
1c303 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 0;. assert( pB
1c304 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 t->pageSize>=512
1c305 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 7a && pBt->pageSiz
1c306 65 3c 3d 36 35 35 33 36 20 29 3b 0a 20 20 70 50 e<=65536 );. pP
1c307 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 age->maskPage =
1c308 28 75 31 36 29 28 70 42 74 2d 3e 70 61 67 65 53 (u16)(pBt->pageS
1c309 69 7a 65 20 2d 20 31 29 3b 0a 20 20 70 50 61 67 ize - 1);. pPag
1c30a 65 2d 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 e->nCell = 0;.
1c30b 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 pPage->isInit =
1c30c 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 1;.}.../*.** Con
1c30d 76 65 72 74 20 61 20 44 62 50 61 67 65 20 6f 62 vert a DbPage ob
1c30e 74 61 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 tained from the
1c30f 70 61 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d pager into a Mem
1c310 50 61 67 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 Page used by.**
1c311 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e the btree layer.
1c312 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 .*/.static MemPa
1c313 67 65 20 2a 62 74 72 65 65 50 61 67 65 46 72 6f ge *btreePageFro
1c314 6d 44 62 50 61 67 65 28 44 62 50 61 67 65 20 2a mDbPage(DbPage *
1c315 70 44 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 pDbPage, Pgno pg
1c316 6e 6f 2c 20 42 74 53 68 61 72 65 64 20 2a 70 42 no, BtShared *pB
1c317 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 t){. MemPage *p
1c318 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a Page = (MemPage*
1c319 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 )sqlite3PagerGet
1c31a 45 78 74 72 61 28 70 44 62 50 61 67 65 29 3b 0a Extra(pDbPage);.
1c31b 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d pPage->aData =
1c31c 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
1c31d 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 Data(pDbPage);.
1c31e 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 20 pPage->pDbPage
1c31f 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 70 50 61 = pDbPage;. pPa
1c320 67 65 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 ge->pBt = pBt;.
1c321 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 pPage->pgno = p
1c322 67 6e 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 gno;. pPage->hd
1c323 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d rOffset = pPage-
1c324 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a >pgno==1 ? 100 :
1c325 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 0;. return pPa
1c326 67 65 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 ge; .}../*.** Ge
1c327 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 t a page from th
1c328 65 20 70 61 67 65 72 2e 20 20 49 6e 69 74 69 61 e pager. Initia
1c329 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65 lize the MemPage
1c32a 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 .pBt and.** MemP
1c32b 61 67 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e age.aData elemen
1c32c 74 73 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a ts if needed..**
1c32d 0a 2a 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e .** If the noCon
1c32e 74 65 6e 74 20 66 6c 61 67 20 69 73 20 73 65 74 tent flag is set
1c32f 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
1c330 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 we do not care a
1c331 62 6f 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 bout.** the cont
1c332 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 ent of the page
1c333 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53 at this time. S
1c334 6f 20 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 o do not go to t
1c335 68 65 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 he disk.** to fe
1c336 74 63 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e tch the content.
1c337 20 20 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 Just fill in t
1c338 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 he content with
1c339 7a 65 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a zeros for now..*
1c33a 2a 20 49 66 20 69 6e 20 74 68 65 20 66 75 74 75 * If in the futu
1c33b 72 65 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 re we call sqlit
1c33c 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 6f e3PagerWrite() o
1c33d 6e 20 74 68 69 73 20 70 61 67 65 2c 20 74 68 61 n this page, tha
1c33e 74 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 t.** means we ha
1c33f 76 65 20 73 74 61 72 74 65 64 20 74 6f 20 62 65 ve started to be
1c340 20 63 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 concerned about
1c341 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 content and the
1c342 20 64 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 disk.** read sh
1c343 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68 ould occur at th
1c344 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 at point..*/.sta
1c345 74 69 63 20 69 6e 74 20 62 74 72 65 65 47 65 74 tic int btreeGet
1c346 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 Page(. BtShared
1c347 20 2a 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 *pBt, /*
1c348 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 The btree */. P
1c349 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 gno pgno,
1c34a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1c34b 20 74 68 65 20 70 61 67 65 20 74 6f 20 66 65 74 the page to fet
1c34c 63 68 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 ch */. MemPage
1c34d 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 **ppPage, /*
1c34e 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 Return the page
1c34f 69 6e 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 in this paramete
1c350 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e r */. int noCon
1c351 74 65 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 44 tent /* D
1c352 6f 20 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 20 o not load page
1c353 63 6f 6e 74 65 6e 74 20 69 66 20 74 72 75 65 20 content if true
1c354 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a */.){. int rc;.
1c355 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
1c356 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 e;.. assert( sq
1c357 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1c358 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
1c359 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1c35a 67 65 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e gerAcquire(pBt->
1c35b 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 pPager, pgno, (D
1c35c 62 50 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 bPage**)&pDbPage
1c35d 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 , noContent);.
1c35e 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
1c35f 72 63 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 rc;. *ppPage =
1c360 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 btreePageFromDbP
1c361 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e age(pDbPage, pgn
1c362 6f 2c 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72 o, pBt);. retur
1c363 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1c364 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 /*.** Retrieve a
1c365 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 page from the p
1c366 61 67 65 72 20 63 61 63 68 65 2e 20 49 66 20 74 ager cache. If t
1c367 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67 he requested pag
1c368 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 e is not.** alre
1c369 61 64 79 20 69 6e 20 74 68 65 20 70 61 67 65 72 ady in the pager
1c36a 20 63 61 63 68 65 20 72 65 74 75 72 6e 20 4e 55 cache return NU
1c36b 4c 4c 2e 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 LL. Initialize t
1c36c 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 he MemPage.pBt a
1c36d 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 nd.** MemPage.aD
1c36e 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 ata elements if
1c36f 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 needed..*/.stati
1c370 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 65 c MemPage *btree
1c371 50 61 67 65 4c 6f 6f 6b 75 70 28 42 74 53 68 61 PageLookup(BtSha
1c372 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 red *pBt, Pgno p
1c373 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a gno){. DbPage *
1c374 70 44 62 50 61 67 65 3b 0a 20 20 61 73 73 65 72 pDbPage;. asser
1c375 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1c376 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
1c377 29 20 29 3b 0a 20 20 70 44 62 50 61 67 65 20 3d ) );. pDbPage =
1c378 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f sqlite3PagerLoo
1c379 6b 75 70 28 70 42 74 2d 3e 70 50 61 67 65 72 2c kup(pBt->pPager,
1c37a 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 44 pgno);. if( pD
1c37b 62 50 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74 bPage ){. ret
1c37c 75 72 6e 20 62 74 72 65 65 50 61 67 65 46 72 6f urn btreePageFro
1c37d 6d 44 62 50 61 67 65 28 70 44 62 50 61 67 65 2c mDbPage(pDbPage,
1c37e 20 70 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 7d pgno, pBt);. }
1c37f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
1c380 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
1c381 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
1c382 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 abase file in pa
1c383 67 65 73 2e 20 49 66 20 74 68 65 72 65 20 69 73 ges. If there is
1c384 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 any kind of.**
1c385 65 72 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 28 error, return ((
1c386 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 29 unsigned int)-1)
1c387 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f ..*/.static Pgno
1c388 20 62 74 72 65 65 50 61 67 65 63 6f 75 6e 74 28 btreePagecount(
1c389 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
1c38a 20 20 72 65 74 75 72 6e 20 70 42 74 2d 3e 6e 50 return pBt->nP
1c38b 61 67 65 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 age;.}.SQLITE_PR
1c38c 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 IVATE u32 sqlite
1c38d 33 42 74 72 65 65 4c 61 73 74 50 61 67 65 28 42 3BtreeLastPage(B
1c38e 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 tree *p){. asse
1c38f 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
1c390 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b HoldsMutex(p) );
1c391 0a 20 20 61 73 73 65 72 74 28 20 28 28 70 2d 3e . assert( ((p->
1c392 70 42 74 2d 3e 6e 50 61 67 65 29 26 30 78 38 30 pBt->nPage)&0x80
1c393 30 30 30 30 30 29 3d 3d 30 20 29 3b 0a 20 20 72 00000)==0 );. r
1c394 65 74 75 72 6e 20 28 69 6e 74 29 62 74 72 65 65 eturn (int)btree
1c395 50 61 67 65 63 6f 75 6e 74 28 70 2d 3e 70 42 74 Pagecount(p->pBt
1c396 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 );.}../*.** Get
1c397 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
1c398 70 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69 61 pager and initia
1c399 6c 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20 72 lize it. This r
1c39a 6f 75 74 69 6e 65 20 69 73 20 6a 75 73 74 20 61 outine is just a
1c39b 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 .** convenience
1c39c 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 wrapper around s
1c39d 65 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74 6f eparate calls to
1c39e 20 62 74 72 65 65 47 65 74 50 61 67 65 28 29 20 btreeGetPage()
1c39f 61 6e 64 20 0a 2a 2a 20 62 74 72 65 65 49 6e 69 and .** btreeIni
1c3a0 74 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 tPage()..**.** I
1c3a1 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
1c3a2 73 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 s, then the valu
1c3a3 65 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 e *ppPage is set
1c3a4 20 74 6f 20 69 73 20 75 6e 64 65 66 69 6e 65 64 to is undefined
1c3a5 2e 20 49 74 0a 2a 2a 20 6d 61 79 20 72 65 6d 61 . It.** may rema
1c3a6 69 6e 20 75 6e 63 68 61 6e 67 65 64 2c 20 6f 72 in unchanged, or
1c3a7 20 69 74 20 6d 61 79 20 62 65 20 73 65 74 20 74 it may be set t
1c3a8 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c o an invalid val
1c3a9 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ue..*/.static in
1c3aa 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 t getAndInitPage
1c3ab 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 (. BtShared *pB
1c3ac 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 t, /* T
1c3ad 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1c3ae 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c */. Pgno pgno,
1c3af 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1c3b0 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 mber of the page
1c3b1 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 6d to get */. Mem
1c3b2 50 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20 20 Page **ppPage
1c3b3 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 /* Write the p
1c3b4 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72 65 age pointer here
1c3b5 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
1c3b6 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1c3b7 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
1c3b8 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 t->mutex) );..
1c3b9 69 66 28 20 70 67 6e 6f 3e 62 74 72 65 65 50 61 if( pgno>btreePa
1c3ba 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a gecount(pBt) ){.
1c3bb 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1c3bc 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1c3bd 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
1c3be 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 btreeGetPage(pBt
1c3bf 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 , pgno, ppPage,
1c3c0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 0);. if( rc==
1c3c1 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1c3c2 20 20 20 72 63 20 3d 20 62 74 72 65 65 49 6e 69 rc = btreeIni
1c3c3 74 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a tPage(*ppPage);.
1c3c4 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1c3c5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1c3c6 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a releasePage(*
1c3c7 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d ppPage);. }
1c3c8 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 74 65 . }. }.. te
1c3c9 73 74 63 61 73 65 28 20 70 67 6e 6f 3d 3d 30 20 stcase( pgno==0
1c3ca 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e );. assert( pgn
1c3cb 6f 21 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 o!=0 || rc==SQLI
1c3cc 54 45 5f 43 4f 52 52 55 50 54 20 29 3b 0a 20 20 TE_CORRUPT );.
1c3cd 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1c3ce 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 4d 65 .** Release a Me
1c3cf 6d 50 61 67 65 2e 20 20 54 68 69 73 20 73 68 6f mPage. This sho
1c3d0 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e uld be called on
1c3d1 63 65 20 66 6f 72 20 65 61 63 68 20 70 72 69 6f ce for each prio
1c3d2 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 74 72 r.** call to btr
1c3d3 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f 0a 73 74 eeGetPage..*/.st
1c3d4 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 atic void releas
1c3d5 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 ePage(MemPage *p
1c3d6 50 61 67 65 29 7b 0a 20 20 69 66 28 20 70 50 61 Page){. if( pPa
1c3d7 67 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 ge ){. assert
1c3d8 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 29 ( pPage->aData )
1c3d9 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 ;. assert( pP
1c3da 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 age->pBt );.
1c3db 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
1c3dc 61 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 agerGetExtra(pPa
1c3dd 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 ge->pDbPage) ==
1c3de 28 76 6f 69 64 2a 29 70 50 61 67 65 20 29 3b 0a (void*)pPage );.
1c3df 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
1c3e0 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
1c3e1 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3d pPage->pDbPage)=
1c3e2 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 20 29 3b =pPage->aData );
1c3e3 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
1c3e4 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1c3e5 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
1c3e6 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 x) );. sqlite
1c3e7 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 3PagerUnref(pPag
1c3e8 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d e->pDbPage);. }
1c3e9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67 .}../*.** During
1c3ea 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 77 68 65 a rollback, whe
1c3eb 6e 20 74 68 65 20 70 61 67 65 72 20 72 65 6c 6f n the pager relo
1c3ec 61 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ads information
1c3ed 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65 0a 2a into the cache.*
1c3ee 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 61 * so that the ca
1c3ef 63 68 65 20 69 73 20 72 65 73 74 6f 72 65 64 20 che is restored
1c3f0 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 to its original
1c3f1 73 74 61 74 65 20 61 74 20 74 68 65 20 73 74 61 state at the sta
1c3f2 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 74 72 61 rt of.** the tra
1c3f3 6e 73 61 63 74 69 6f 6e 2c 20 66 6f 72 20 65 61 nsaction, for ea
1c3f4 63 68 20 70 61 67 65 20 72 65 73 74 6f 72 65 64 ch page restored
1c3f5 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1c3f6 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 called..**.** T
1c3f7 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 65 64 his routine need
1c3f8 73 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 65 s to reset the e
1c3f9 78 74 72 61 20 64 61 74 61 20 73 65 63 74 69 6f xtra data sectio
1c3fa 6e 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 n at the end of
1c3fb 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f 20 61 the.** page to a
1c3fc 67 72 65 65 20 77 69 74 68 20 74 68 65 20 72 65 gree with the re
1c3fd 73 74 6f 72 65 64 20 64 61 74 61 2e 0a 2a 2f 0a stored data..*/.
1c3fe 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 static void page
1c3ff 52 65 69 6e 69 74 28 44 62 50 61 67 65 20 2a 70 Reinit(DbPage *p
1c400 44 61 74 61 29 7b 0a 20 20 4d 65 6d 50 61 67 65 Data){. MemPage
1c401 20 2a 70 50 61 67 65 3b 0a 20 20 70 50 61 67 65 *pPage;. pPage
1c402 20 3d 20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71 = (MemPage *)sq
1c403 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 lite3PagerGetExt
1c404 72 61 28 70 44 61 74 61 29 3b 0a 20 20 61 73 73 ra(pData);. ass
1c405 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
1c406 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 44 rPageRefcount(pD
1c407 61 74 61 29 3e 30 20 29 3b 0a 20 20 69 66 28 20 ata)>0 );. if(
1c408 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 7b pPage->isInit ){
1c409 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
1c40a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1c40b 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
1c40c 78 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d x) );. pPage-
1c40d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 >isInit = 0;.
1c40e 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 if( sqlite3Page
1c40f 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 44 rPageRefcount(pD
1c410 61 74 61 29 3e 31 20 29 7b 0a 20 20 20 20 20 20 ata)>1 ){.
1c411 2f 2a 20 70 50 61 67 65 20 6d 69 67 68 74 20 6e /* pPage might n
1c412 6f 74 20 62 65 20 61 20 62 74 72 65 65 20 70 61 ot be a btree pa
1c413 67 65 3b 20 20 69 74 20 6d 69 67 68 74 20 62 65 ge; it might be
1c414 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
1c415 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 70 74 e. ** or pt
1c416 72 6d 61 70 20 70 61 67 65 20 6f 72 20 61 20 66 rmap page or a f
1c417 72 65 65 20 70 61 67 65 2e 20 20 49 6e 20 74 68 ree page. In th
1c418 6f 73 65 20 63 61 73 65 73 2c 20 74 68 65 20 66 ose cases, the f
1c419 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 20 20 2a ollowing. *
1c41a 2a 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 65 49 * call to btreeI
1c41b 6e 69 74 50 61 67 65 28 29 20 77 69 6c 6c 20 6c nitPage() will l
1c41c 69 6b 65 6c 79 20 72 65 74 75 72 6e 20 53 51 4c ikely return SQL
1c41d 49 54 45 5f 43 4f 52 52 55 50 54 2e 0a 20 20 20 ITE_CORRUPT..
1c41e 20 20 20 2a 2a 20 42 75 74 20 6e 6f 20 68 61 72 ** But no har
1c41f 6d 20 69 73 20 64 6f 6e 65 20 62 79 20 74 68 69 m is done by thi
1c420 73 2e 20 20 41 6e 64 20 69 74 20 69 73 20 76 65 s. And it is ve
1c421 72 79 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 ry important tha
1c422 74 0a 20 20 20 20 20 20 2a 2a 20 62 74 72 65 65 t. ** btree
1c423 49 6e 69 74 50 61 67 65 28 29 20 62 65 20 63 61 InitPage() be ca
1c424 6c 6c 65 64 20 6f 6e 20 65 76 65 72 79 20 62 74 lled on every bt
1c425 72 65 65 20 70 61 67 65 20 73 6f 20 77 65 20 6d ree page so we m
1c426 61 6b 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 ake. ** the
1c427 20 63 61 6c 6c 20 66 6f 72 20 65 76 65 72 79 20 call for every
1c428 70 61 67 65 20 74 68 61 74 20 63 6f 6d 65 73 20 page that comes
1c429 69 6e 20 66 6f 72 20 72 65 2d 69 6e 69 74 69 6e in for re-initin
1c42a 67 2e 20 2a 2f 0a 20 20 20 20 20 20 62 74 72 65 g. */. btre
1c42b 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 eInitPage(pPage)
1c42c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
1c42d 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 *.** Invoke the
1c42e 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 busy handler for
1c42f 20 61 20 62 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 a btree..*/.sta
1c430 74 69 63 20 69 6e 74 20 62 74 72 65 65 49 6e 76 tic int btreeInv
1c431 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 76 okeBusyHandler(v
1c432 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 42 74 oid *pArg){. Bt
1c433 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 28 42 Shared *pBt = (B
1c434 74 53 68 61 72 65 64 2a 29 70 41 72 67 3b 0a 20 tShared*)pArg;.
1c435 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 64 62 assert( pBt->db
1c436 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
1c437 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1c438 28 70 42 74 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 (pBt->db->mutex)
1c439 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c );. return sql
1c43a 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 ite3InvokeBusyHa
1c43b 6e 64 6c 65 72 28 26 70 42 74 2d 3e 64 62 2d 3e ndler(&pBt->db->
1c43c 62 75 73 79 48 61 6e 64 6c 65 72 29 3b 0a 7d 0a busyHandler);.}.
1c43d 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 64 61 ./*.** Open a da
1c43e 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 tabase file..**
1c43f 0a 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 .** zFilename is
1c440 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1c441 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
1c442 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 If zFilename is
1c443 20 4e 55 4c 4c 0a 2a 2a 20 74 68 65 6e 20 61 6e NULL.** then an
1c444 20 65 70 68 65 6d 65 72 61 6c 20 64 61 74 61 62 ephemeral datab
1c445 61 73 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 ase is created.
1c446 20 54 68 65 20 65 70 68 65 6d 65 72 61 6c 20 64 The ephemeral d
1c447 61 74 61 62 61 73 65 20 6d 69 67 68 74 0a 2a 2a atabase might.**
1c448 20 62 65 20 65 78 63 6c 75 73 69 76 65 6c 79 20 be exclusively
1c449 69 6e 20 6d 65 6d 6f 72 79 2c 20 6f 72 20 69 74 in memory, or it
1c44a 20 6d 69 67 68 74 20 75 73 65 20 61 20 64 69 73 might use a dis
1c44b 6b 2d 62 61 73 65 64 20 6d 65 6d 6f 72 79 20 63 k-based memory c
1c44c 61 63 68 65 2e 0a 2a 2a 20 45 69 74 68 65 72 20 ache..** Either
1c44d 77 61 79 2c 20 74 68 65 20 65 70 68 65 6d 65 72 way, the ephemer
1c44e 61 6c 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c al database will
1c44f 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c be automaticall
1c450 79 20 64 65 6c 65 74 65 64 20 0a 2a 2a 20 77 68 y deleted .** wh
1c451 65 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 en sqlite3BtreeC
1c452 6c 6f 73 65 28 29 20 69 73 20 63 61 6c 6c 65 64 lose() is called
1c453 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 ..**.** If zFile
1c454 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 name is ":memory
1c455 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 :" then an in-me
1c456 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 69 73 mory database is
1c457 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 74 created.** that
1c458 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c is automaticall
1c459 79 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e y destroyed when
1c45a 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a it is closed..*
1c45b 2a 0a 2a 2a 20 54 68 65 20 22 66 6c 61 67 73 22 *.** The "flags"
1c45c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 parameter is a
1c45d 62 69 74 6d 61 73 6b 20 74 68 61 74 20 6d 69 67 bitmask that mig
1c45e 68 74 20 63 6f 6e 74 61 69 6e 20 62 69 74 73 20 ht contain bits
1c45f 6c 69 6b 65 0a 2a 2a 20 42 54 52 45 45 5f 4f 4d like.** BTREE_OM
1c460 49 54 5f 4a 4f 55 52 4e 41 4c 20 61 6e 64 2f 6f IT_JOURNAL and/o
1c461 72 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 2e 0a r BTREE_MEMORY..
1c462 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 **.** If the dat
1c463 61 62 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 abase is already
1c464 20 6f 70 65 6e 65 64 20 69 6e 20 74 68 65 20 73 opened in the s
1c465 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ame database con
1c466 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 77 nection.** and w
1c467 65 20 61 72 65 20 69 6e 20 73 68 61 72 65 64 20 e are in shared
1c468 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e cache mode, then
1c469 20 74 68 65 20 6f 70 65 6e 20 77 69 6c 6c 20 66 the open will f
1c46a 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 ail with an.** S
1c46b 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 QLITE_CONSTRAINT
1c46c 20 65 72 72 6f 72 2e 20 20 57 65 20 63 61 6e 6e error. We cann
1c46d 6f 74 20 61 6c 6c 6f 77 20 74 77 6f 20 6f 72 20 ot allow two or
1c46e 6d 6f 72 65 20 42 74 53 68 61 72 65 64 0a 2a 2a more BtShared.**
1c46f 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20 objects in the
1c470 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
1c471 6e 6e 65 63 74 69 6f 6e 20 73 69 6e 63 65 20 64 nnection since d
1c472 6f 69 6e 67 20 73 6f 20 77 69 6c 6c 20 6c 65 61 oing so will lea
1c473 64 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c 65 6d 73 d.** to problems
1c474 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a with locking..*
1c475 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1c476 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
1c477 65 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 eOpen(. sqlite3
1c478 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
1c479 20 2f 2a 20 56 46 53 20 74 6f 20 75 73 65 20 66 /* VFS to use f
1c47a 6f 72 20 74 68 69 73 20 62 2d 74 72 65 65 20 2a or this b-tree *
1c47b 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1c47c 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e zFilename, /* N
1c47d 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ame of the file
1c47e 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 42 containing the B
1c47f 54 72 65 65 20 64 61 74 61 62 61 73 65 20 2a 2f Tree database */
1c480 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
1c481 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 /* As
1c482 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 sociated databas
1c483 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 e handle */. Bt
1c484 72 65 65 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 ree **ppBtree,
1c485 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
1c486 20 74 6f 20 6e 65 77 20 42 74 72 65 65 20 6f 62 to new Btree ob
1c487 6a 65 63 74 20 77 72 69 74 74 65 6e 20 68 65 72 ject written her
1c488 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 e */. int flags
1c489 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1c48a 2a 20 4f 70 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 * Options */. i
1c48b 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 nt vfsFlags
1c48c 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
1c48d 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 passed through t
1c48e 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f o sqlite3_vfs.xO
1c48f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a 20 20 42 74 pen() */.){. Bt
1c490 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 30 3b Shared *pBt = 0;
1c491 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c492 53 68 61 72 65 64 20 70 61 72 74 20 6f 66 20 62 Shared part of b
1c493 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a tree structure *
1c494 2f 0a 20 20 42 74 72 65 65 20 2a 70 3b 20 20 20 /. Btree *p;
1c495 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c496 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20 /* Handle to
1c497 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 return */. sqli
1c498 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
1c499 4f 70 65 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72 Open = 0; /* Pr
1c49a 65 76 65 6e 74 73 20 61 20 72 61 63 65 20 63 6f events a race co
1c49b 6e 64 69 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20 ndition. Ticket
1c49c 23 33 35 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72 #3537 */. int r
1c49d 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
1c49e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
1c49f 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 ult code from th
1c4a0 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 is function */.
1c4a1 20 75 38 20 6e 52 65 73 65 72 76 65 3b 20 20 20 u8 nReserve;
1c4a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c4a3 2f 2a 20 42 79 74 65 20 6f 66 20 75 6e 75 73 65 /* Byte of unuse
1c4a4 64 20 73 70 61 63 65 20 6f 6e 20 65 61 63 68 20 d space on each
1c4a5 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e page */. unsign
1c4a6 65 64 20 63 68 61 72 20 7a 44 62 48 65 61 64 65 ed char zDbHeade
1c4a7 72 5b 31 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61 r[100]; /* Data
1c4a8 62 61 73 65 20 68 65 61 64 65 72 20 63 6f 6e 74 base header cont
1c4a9 65 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 54 72 75 ent */.. /* Tru
1c4aa 65 20 69 66 20 6f 70 65 6e 69 6e 67 20 61 6e 20 e if opening an
1c4ab 65 70 68 65 6d 65 72 61 6c 2c 20 74 65 6d 70 6f ephemeral, tempo
1c4ac 72 61 72 79 20 64 61 74 61 62 61 73 65 20 2a 2f rary database */
1c4ad 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 54 . const int isT
1c4ae 65 6d 70 44 62 20 3d 20 7a 46 69 6c 65 6e 61 6d empDb = zFilenam
1c4af 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d e==0 || zFilenam
1c4b0 65 5b 30 5d 3d 3d 30 3b 0a 0a 20 20 2f 2a 20 53 e[0]==0;.. /* S
1c4b1 65 74 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 et the variable
1c4b2 69 73 4d 65 6d 64 62 20 74 6f 20 74 72 75 65 20 isMemdb to true
1c4b3 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 for an in-memory
1c4b4 20 64 61 74 61 62 61 73 65 2c 20 6f 72 20 0a 20 database, or .
1c4b5 20 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20 61 20 ** false for a
1c4b6 66 69 6c 65 2d 62 61 73 65 64 20 64 61 74 61 62 file-based datab
1c4b7 61 73 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 ase.. */.#ifdef
1c4b8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d SQLITE_OMIT_MEM
1c4b9 4f 52 59 44 42 0a 20 20 63 6f 6e 73 74 20 69 6e ORYDB. const in
1c4ba 74 20 69 73 4d 65 6d 64 62 20 3d 20 30 3b 0a 23 t isMemdb = 0;.#
1c4bb 65 6c 73 65 0a 20 20 63 6f 6e 73 74 20 69 6e 74 else. const int
1c4bc 20 69 73 4d 65 6d 64 62 20 3d 20 28 7a 46 69 6c isMemdb = (zFil
1c4bd 65 6e 61 6d 65 20 26 26 20 73 74 72 63 6d 70 28 ename && strcmp(
1c4be 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d zFilename, ":mem
1c4bf 6f 72 79 3a 22 29 3d 3d 30 29 0a 20 20 20 20 20 ory:")==0).
1c4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c4c1 20 20 7c 7c 20 28 69 73 54 65 6d 70 44 62 20 26 || (isTempDb &
1c4c2 26 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d & sqlite3TempInM
1c4c3 65 6d 6f 72 79 28 64 62 29 29 0a 20 20 20 20 20 emory(db)).
1c4c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c4c5 20 20 7c 7c 20 28 76 66 73 46 6c 61 67 73 20 26 || (vfsFlags &
1c4c6 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 45 4d SQLITE_OPEN_MEM
1c4c7 4f 52 59 29 21 3d 30 3b 0a 23 65 6e 64 69 66 0a ORY)!=0;.#endif.
1c4c8 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 . assert( db!=0
1c4c9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 );. assert( pV
1c4ca 66 73 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 fs!=0 );. asser
1c4cb 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1c4cc 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 _held(db->mutex)
1c4cd 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 66 );. assert( (f
1c4ce 6c 61 67 73 26 30 78 66 66 29 3d 3d 66 6c 61 67 lags&0xff)==flag
1c4cf 73 20 29 3b 20 20 20 2f 2a 20 66 6c 61 67 73 20 s ); /* flags
1c4d0 66 69 74 20 69 6e 20 38 20 62 69 74 73 20 2a 2f fit in 8 bits */
1c4d1 0a 0a 20 20 2f 2a 20 4f 6e 6c 79 20 61 20 42 54 .. /* Only a BT
1c4d2 52 45 45 5f 53 49 4e 47 4c 45 20 64 61 74 61 62 REE_SINGLE datab
1c4d3 61 73 65 20 63 61 6e 20 62 65 20 42 54 52 45 45 ase can be BTREE
1c4d4 5f 55 4e 4f 52 44 45 52 45 44 20 2a 2f 0a 20 20 _UNORDERED */.
1c4d5 61 73 73 65 72 74 28 20 28 66 6c 61 67 73 20 26 assert( (flags &
1c4d6 20 42 54 52 45 45 5f 55 4e 4f 52 44 45 52 45 44 BTREE_UNORDERED
1c4d7 29 3d 3d 30 20 7c 7c 20 28 66 6c 61 67 73 20 26 )==0 || (flags &
1c4d8 20 42 54 52 45 45 5f 53 49 4e 47 4c 45 29 21 3d BTREE_SINGLE)!=
1c4d9 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 42 54 52 0 );.. /* A BTR
1c4da 45 45 5f 53 49 4e 47 4c 45 20 64 61 74 61 62 61 EE_SINGLE databa
1c4db 73 65 20 69 73 20 61 6c 77 61 79 73 20 61 20 74 se is always a t
1c4dc 65 6d 70 6f 72 61 72 79 20 61 6e 64 2f 6f 72 20 emporary and/or
1c4dd 65 70 68 65 6d 65 72 61 6c 20 2a 2f 0a 20 20 61 ephemeral */. a
1c4de 73 73 65 72 74 28 20 28 66 6c 61 67 73 20 26 20 ssert( (flags &
1c4df 42 54 52 45 45 5f 53 49 4e 47 4c 45 29 3d 3d 30 BTREE_SINGLE)==0
1c4e0 20 7c 7c 20 69 73 54 65 6d 70 44 62 20 29 3b 0a || isTempDb );.
1c4e1 0a 20 20 69 66 28 20 69 73 4d 65 6d 64 62 20 29 . if( isMemdb )
1c4e2 7b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 42 {. flags |= B
1c4e3 54 52 45 45 5f 4d 45 4d 4f 52 59 3b 0a 20 20 7d TREE_MEMORY;. }
1c4e4 0a 20 20 69 66 28 20 28 76 66 73 46 6c 61 67 73 . if( (vfsFlags
1c4e5 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d & SQLITE_OPEN_M
1c4e6 41 49 4e 5f 44 42 29 21 3d 30 20 26 26 20 28 69 AIN_DB)!=0 && (i
1c4e7 73 4d 65 6d 64 62 20 7c 7c 20 69 73 54 65 6d 70 sMemdb || isTemp
1c4e8 44 62 29 20 29 7b 0a 20 20 20 20 76 66 73 46 6c Db) ){. vfsFl
1c4e9 61 67 73 20 3d 20 28 76 66 73 46 6c 61 67 73 20 ags = (vfsFlags
1c4ea 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d & ~SQLITE_OPEN_M
1c4eb 41 49 4e 5f 44 42 29 20 7c 20 53 51 4c 49 54 45 AIN_DB) | SQLITE
1c4ec 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20 _OPEN_TEMP_DB;.
1c4ed 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 }. p = sqlite3
1c4ee 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f MallocZero(sizeo
1c4ef 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 66 28 f(Btree));. if(
1c4f0 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 !p ){. retur
1c4f1 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
1c4f2 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 6e 73 }. p->inTrans
1c4f3 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 = TRANS_NONE;.
1c4f4 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 23 69 66 p->db = db;.#if
1c4f5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1c4f6 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 _SHARED_CACHE.
1c4f7 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 20 3d p->lock.pBtree =
1c4f8 20 70 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69 54 p;. p->lock.iT
1c4f9 61 62 6c 65 20 3d 20 31 3b 0a 23 65 6e 64 69 66 able = 1;.#endif
1c4fa 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
1c4fb 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
1c4fc 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 D_CACHE) && !def
1c4fd 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1c4fe 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 _DISKIO). /*.
1c4ff 2a 2a 20 49 66 20 74 68 69 73 20 42 74 72 65 65 ** If this Btree
1c500 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 65 20 is a candidate
1c501 66 6f 72 20 73 68 61 72 65 64 20 63 61 63 68 65 for shared cache
1c502 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 6e , try to find an
1c503 0a 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 . ** existing B
1c504 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74 tShared object t
1c505 68 61 74 20 77 65 20 63 61 6e 20 73 68 61 72 65 hat we can share
1c506 20 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 with. */. if(
1c507 20 69 73 54 65 6d 70 44 62 3d 3d 30 20 26 26 20 isTempDb==0 &&
1c508 28 69 73 4d 65 6d 64 62 3d 3d 30 20 7c 7c 20 28 (isMemdb==0 || (
1c509 76 66 73 46 6c 61 67 73 26 53 51 4c 49 54 45 5f vfsFlags&SQLITE_
1c50a 4f 50 45 4e 5f 55 52 49 29 21 3d 30 29 20 29 7b OPEN_URI)!=0) ){
1c50b 0a 20 20 20 20 69 66 28 20 76 66 73 46 6c 61 67 . if( vfsFlag
1c50c 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
1c50d 53 48 41 52 45 44 43 41 43 48 45 20 29 7b 0a 20 SHAREDCACHE ){.
1c50e 20 20 20 20 20 69 6e 74 20 6e 46 75 6c 6c 50 61 int nFullPa
1c50f 74 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d thname = pVfs->m
1c510 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 xPathname+1;.
1c511 20 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 50 61 char *zFullPa
1c512 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 thname = sqlite3
1c513 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 50 61 74 68 Malloc(nFullPath
1c514 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 4d 55 54 name);. MUT
1c515 45 58 5f 4c 4f 47 49 43 28 20 73 71 6c 69 74 65 EX_LOGIC( sqlite
1c516 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 3_mutex *mutexSh
1c517 61 72 65 64 3b 20 29 0a 20 20 20 20 20 20 70 2d ared; ). p-
1c518 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 >sharable = 1;.
1c519 20 20 20 20 20 69 66 28 20 21 7a 46 75 6c 6c 50 if( !zFullP
1c51a 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 athname ){.
1c51b 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
1c51c 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 p);. retu
1c51d 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
1c51e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
1c51f 66 28 20 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20 f( isMemdb ){.
1c520 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 46 75 memcpy(zFu
1c521 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 7a 46 69 6c llPathname, zFil
1c522 65 6e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 ename, sqlite3St
1c523 72 6c 65 6e 33 30 28 7a 46 69 6c 65 6e 61 6d 65 rlen30(zFilename
1c524 29 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 )+1);. }els
1c525 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 e{. rc =
1c526 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 sqlite3OsFullPat
1c527 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c hname(pVfs, zFil
1c528 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 ename,.
1c529 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c52a 20 20 20 20 20 20 20 20 20 20 6e 46 75 6c 6c 50 nFullP
1c52b 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 athname, zFullPa
1c52c 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 thname);.
1c52d 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
1c52e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
1c52f 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 e(zFullPathname)
1c530 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
1c531 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 te3_free(p);.
1c532 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1c533 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1c534 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 }.#if SQLITE_T
1c535 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 20 20 HREADSAFE.
1c536 6d 75 74 65 78 4f 70 65 6e 20 3d 20 73 71 6c 69 mutexOpen = sqli
1c537 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
1c538 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
1c539 43 5f 4f 50 45 4e 29 3b 0a 20 20 20 20 20 20 73 C_OPEN);. s
1c53a 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
1c53b 65 72 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 er(mutexOpen);.
1c53c 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 65 64 mutexShared
1c53d 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 = sqlite3MutexA
1c53e 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
1c53f 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
1c540 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
1c541 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
1c542 78 53 68 61 72 65 64 29 3b 0a 23 65 6e 64 69 66 xShared);.#endif
1c543 0a 20 20 20 20 20 20 66 6f 72 28 70 42 74 3d 47 . for(pBt=G
1c544 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c LOBAL(BtShared*,
1c545 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 sqlite3SharedCac
1c546 68 65 4c 69 73 74 29 3b 20 70 42 74 3b 20 70 42 heList); pBt; pB
1c547 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b 0a 20 t=pBt->pNext){.
1c548 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1c549 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 Bt->nRef>0 );.
1c54a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 74 72 if( 0==str
1c54b 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d cmp(zFullPathnam
1c54c 65 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 e, sqlite3PagerF
1c54d 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e 70 50 61 ilename(pBt->pPa
1c54e 67 65 72 2c 20 30 29 29 0a 20 20 20 20 20 20 20 ger, 0)).
1c54f 20 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c && sql
1c550 69 74 65 33 50 61 67 65 72 56 66 73 28 70 42 74 ite3PagerVfs(pBt
1c551 2d 3e 70 50 61 67 65 72 29 3d 3d 70 56 66 73 20 ->pPager)==pVfs
1c552 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 ){. int
1c553 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 20 20 iDb;.
1c554 66 6f 72 28 69 44 62 3d 64 62 2d 3e 6e 44 62 2d for(iDb=db->nDb-
1c555 31 3b 20 69 44 62 3e 3d 30 3b 20 69 44 62 2d 2d 1; iDb>=0; iDb--
1c556 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 42 ){. B
1c557 74 72 65 65 20 2a 70 45 78 69 73 74 69 6e 67 20 tree *pExisting
1c558 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 = db->aDb[iDb].p
1c559 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 Bt;.
1c55a 69 66 28 20 70 45 78 69 73 74 69 6e 67 20 26 26 if( pExisting &&
1c55b 20 70 45 78 69 73 74 69 6e 67 2d 3e 70 42 74 3d pExisting->pBt=
1c55c 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 =pBt ){.
1c55d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
1c55e 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 tex_leave(mutexS
1c55f 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 20 20 hared);.
1c560 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
1c561 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 4f tex_leave(mutexO
1c562 70 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 pen);.
1c563 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
1c564 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b (zFullPathname);
1c565 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 . s
1c566 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
1c567 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 re
1c568 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 turn SQLITE_CONS
1c569 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 TRAINT;.
1c56a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1c56b 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 }. p->p
1c56c 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 Bt = pBt;.
1c56d 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b pBt->nRef++;
1c56e 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
1c56f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1c570 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1c571 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
1c572 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 texShared);.
1c573 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
1c574 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 FullPathname);.
1c575 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 }.#ifdef SQLI
1c576 54 45 5f 44 45 42 55 47 0a 20 20 20 20 65 6c 73 TE_DEBUG. els
1c577 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 e{. /* In d
1c578 65 62 75 67 20 6d 6f 64 65 2c 20 77 65 20 6d 61 ebug mode, we ma
1c579 72 6b 20 61 6c 6c 20 70 65 72 73 69 73 74 65 6e rk all persisten
1c57a 74 20 64 61 74 61 62 61 73 65 73 20 61 73 20 73 t databases as s
1c57b 68 61 72 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a harable. **
1c57c 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 79 20 even when they
1c57d 61 72 65 20 6e 6f 74 2e 20 20 54 68 69 73 20 65 are not. This e
1c57e 78 65 72 63 69 73 65 73 20 74 68 65 20 6c 6f 63 xercises the loc
1c57f 6b 69 6e 67 20 63 6f 64 65 20 61 6e 64 0a 20 20 king code and.
1c580 20 20 20 20 2a 2a 20 67 69 76 65 73 20 6d 6f 72 ** gives mor
1c581 65 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 66 6f e opportunity fo
1c582 72 20 61 73 73 65 72 74 73 28 73 71 6c 69 74 65 r asserts(sqlite
1c583 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 29 0a 3_mutex_held()).
1c584 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 ** stateme
1c585 6e 74 73 20 74 6f 20 66 69 6e 64 20 6c 6f 63 6b nts to find lock
1c586 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 ing problems..
1c587 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e */. p->
1c588 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 sharable = 1;.
1c589 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 }.#endif. }.#
1c58a 65 6e 64 69 66 0a 20 20 69 66 28 20 70 42 74 3d endif. if( pBt=
1c58b 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 =0 ){. /*.
1c58c 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e ** The followin
1c58d 67 20 61 73 73 65 72 74 73 20 6d 61 6b 65 20 73 g asserts make s
1c58e 75 72 65 20 74 68 61 74 20 73 74 72 75 63 74 75 ure that structu
1c58f 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 res used by the
1c590 62 74 72 65 65 20 61 72 65 0a 20 20 20 20 2a 2a btree are. **
1c591 20 74 68 65 20 72 69 67 68 74 20 73 69 7a 65 2e the right size.
1c592 20 20 54 68 69 73 20 69 73 20 74 6f 20 67 75 61 This is to gua
1c593 72 64 20 61 67 61 69 6e 73 74 20 73 69 7a 65 20 rd against size
1c594 63 68 61 6e 67 65 73 20 74 68 61 74 20 72 65 73 changes that res
1c595 75 6c 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 ult. ** when
1c596 63 6f 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 20 64 compiling on a d
1c597 69 66 66 65 72 65 6e 74 20 61 72 63 68 69 74 65 ifferent archite
1c598 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 cture.. */.
1c599 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
1c59a 28 69 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 (i64)==8 || size
1c59b 6f 66 28 69 36 34 29 3d 3d 34 20 29 3b 0a 20 20 of(i64)==4 );.
1c59c 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
1c59d 28 75 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 (u64)==8 || size
1c59e 6f 66 28 75 36 34 29 3d 3d 34 20 29 3b 0a 20 20 of(u64)==4 );.
1c59f 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
1c5a0 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20 20 20 (u32)==4 );.
1c5a1 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 assert( sizeof(u
1c5a2 31 36 29 3d 3d 32 20 29 3b 0a 20 20 20 20 61 73 16)==2 );. as
1c5a3 73 65 72 74 28 20 73 69 7a 65 6f 66 28 50 67 6e sert( sizeof(Pgn
1c5a4 6f 29 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 20 20 o)==4 );. .
1c5a5 70 42 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c pBt = sqlite3Mal
1c5a6 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 locZero( sizeof(
1c5a7 2a 70 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28 *pBt) );. if(
1c5a8 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 pBt==0 ){.
1c5a9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
1c5aa 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 EM;. goto b
1c5ab 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 tree_open_out;.
1c5ac 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
1c5ad 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70 lite3PagerOpen(p
1c5ae 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65 Vfs, &pBt->pPage
1c5af 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 r, zFilename,.
1c5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c5b1 20 20 20 20 20 20 20 20 45 58 54 52 41 5f 53 49 EXTRA_SI
1c5b2 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c ZE, flags, vfsFl
1c5b3 61 67 73 2c 20 70 61 67 65 52 65 69 6e 69 74 29 ags, pageReinit)
1c5b4 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1c5b5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1c5b6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1c5b7 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 erReadFileheader
1c5b8 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 73 69 7a (pBt->pPager,siz
1c5b9 65 6f 66 28 7a 44 62 48 65 61 64 65 72 29 2c 7a eof(zDbHeader),z
1c5ba 44 62 48 65 61 64 65 72 29 3b 0a 20 20 20 20 7d DbHeader);. }
1c5bb 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1c5bc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1c5bd 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f goto btree_open_
1c5be 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 out;. }. p
1c5bf 42 74 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20 Bt->openFlags =
1c5c0 28 75 38 29 66 6c 61 67 73 3b 0a 20 20 20 20 70 (u8)flags;. p
1c5c1 42 74 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 Bt->db = db;.
1c5c2 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
1c5c3 42 75 73 79 68 61 6e 64 6c 65 72 28 70 42 74 2d Busyhandler(pBt-
1c5c4 3e 70 50 61 67 65 72 2c 20 62 74 72 65 65 49 6e >pPager, btreeIn
1c5c5 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 2c vokeBusyHandler,
1c5c6 20 70 42 74 29 3b 0a 20 20 20 20 70 2d 3e 70 42 pBt);. p->pB
1c5c7 74 20 3d 20 70 42 74 3b 0a 20 20 0a 20 20 20 20 t = pBt;. .
1c5c8 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 pBt->pCursor = 0
1c5c9 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65 ;. pBt->pPage
1c5ca 31 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 1 = 0;. if( s
1c5cb 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61 qlite3PagerIsrea
1c5cc 64 6f 6e 6c 79 28 70 42 74 2d 3e 70 50 61 67 65 donly(pBt->pPage
1c5cd 72 29 20 29 20 70 42 74 2d 3e 62 74 73 46 6c 61 r) ) pBt->btsFla
1c5ce 67 73 20 7c 3d 20 42 54 53 5f 52 45 41 44 5f 4f gs |= BTS_READ_O
1c5cf 4e 4c 59 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 NLY;.#ifdef SQLI
1c5d0 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 TE_SECURE_DELETE
1c5d1 0a 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 . pBt->btsFla
1c5d2 67 73 20 7c 3d 20 42 54 53 5f 53 45 43 55 52 45 gs |= BTS_SECURE
1c5d3 5f 44 45 4c 45 54 45 3b 0a 23 65 6e 64 69 66 0a _DELETE;.#endif.
1c5d4 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a pBt->pageSiz
1c5d5 65 20 3d 20 28 7a 44 62 48 65 61 64 65 72 5b 31 e = (zDbHeader[1
1c5d6 36 5d 3c 3c 38 29 20 7c 20 28 7a 44 62 48 65 61 6]<<8) | (zDbHea
1c5d7 64 65 72 5b 31 37 5d 3c 3c 31 36 29 3b 0a 20 20 der[17]<<16);.
1c5d8 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 if( pBt->pageS
1c5d9 69 7a 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d 3e ize<512 || pBt->
1c5da 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f pageSize>SQLITE_
1c5db 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20 MAX_PAGE_SIZE.
1c5dc 20 20 20 20 20 20 20 7c 7c 20 28 28 70 42 74 2d || ((pBt-
1c5dd 3e 70 61 67 65 53 69 7a 65 2d 31 29 26 70 42 74 >pageSize-1)&pBt
1c5de 2d 3e 70 61 67 65 53 69 7a 65 29 21 3d 30 20 29 ->pageSize)!=0 )
1c5df 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 {. pBt->pag
1c5e0 65 53 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e 64 eSize = 0;.#ifnd
1c5e1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1c5e2 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
1c5e3 2f 2a 20 49 66 20 74 68 65 20 6d 61 67 69 63 20 /* If the magic
1c5e4 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 name ":memory:"
1c5e5 77 69 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 69 will create an i
1c5e6 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
1c5e7 65 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a e, then. **
1c5e8 20 6c 65 61 76 65 20 74 68 65 20 61 75 74 6f 56 leave the autoV
1c5e9 61 63 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 acuum mode at 0
1c5ea 28 64 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 (do not auto-vac
1c5eb 75 75 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 uum), even if.
1c5ec 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 ** SQLITE_DE
1c5ed 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d FAULT_AUTOVACUUM
1c5ee 20 69 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 is true. On the
1c5ef 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 66 0a other hand, if.
1c5f0 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f ** SQLITE_
1c5f1 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 OMIT_MEMORYDB ha
1c5f2 73 20 62 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 s been defined,
1c5f3 74 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 then ":memory:"
1c5f4 69 73 20 6a 75 73 74 20 61 0a 20 20 20 20 20 20 is just a.
1c5f5 2a 2a 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d ** regular file-
1c5f6 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 name. In this ca
1c5f7 73 65 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 se the auto-vacu
1c5f8 75 6d 20 61 70 70 6c 69 65 73 20 61 73 20 70 65 um applies as pe
1c5f9 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 r normal..
1c5fa 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69 */. if( zFi
1c5fb 6c 65 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d lename && !isMem
1c5fc 64 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 db ){. pB
1c5fd 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 t->autoVacuum =
1c5fe 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f (SQLITE_DEFAULT_
1c5ff 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a AUTOVACUUM ? 1 :
1c600 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 0);. pBt
1c601 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 ->incrVacuum = (
1c602 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 SQLITE_DEFAULT_A
1c603 55 54 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 UTOVACUUM==2 ? 1
1c604 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 : 0);. }.#
1c605 65 6e 64 69 66 0a 20 20 20 20 20 20 6e 52 65 73 endif. nRes
1c606 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 erve = 0;. }e
1c607 6c 73 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 65 lse{. nRese
1c608 72 76 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b rve = zDbHeader[
1c609 32 30 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 20];. pBt->
1c60a 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f btsFlags |= BTS_
1c60b 50 41 47 45 53 49 5a 45 5f 46 49 58 45 44 3b 0a PAGESIZE_FIXED;.
1c60c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1c60d 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
1c60e 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 pBt->autoVa
1c60f 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 cuum = (get4byte
1c610 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b (&zDbHeader[36 +
1c611 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 4*4])?1:0);.
1c612 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 pBt->incrVacu
1c613 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 um = (get4byte(&
1c614 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 37 zDbHeader[36 + 7
1c615 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 *4])?1:0);.#endi
1c616 66 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d f. }. rc =
1c617 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
1c618 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 Pagesize(pBt->pP
1c619 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 ager, &pBt->page
1c61a 53 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 3b Size, nReserve);
1c61b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f . if( rc ) go
1c61c 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 to btree_open_ou
1c61d 74 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62 t;. pBt->usab
1c61e 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 leSize = pBt->pa
1c61f 67 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 geSize - nReserv
1c620 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 e;. assert( (
1c621 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 20 pBt->pageSize &
1c622 37 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62 7)==0 ); /* 8-b
1c623 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 yte alignment of
1c624 20 70 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20 pageSize */.
1c625 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
1c626 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
1c627 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69 _CACHE) && !defi
1c628 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
1c629 44 49 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41 DISKIO). /* A
1c62a 64 64 20 74 68 65 20 6e 65 77 20 42 74 53 68 61 dd the new BtSha
1c62b 72 65 64 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 red object to th
1c62c 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 68 e linked list sh
1c62d 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 73 arable BtShareds
1c62e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1c62f 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a p->sharable ){.
1c630 20 20 20 20 20 20 4d 55 54 45 58 5f 4c 4f 47 49 MUTEX_LOGI
1c631 43 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 C( sqlite3_mutex
1c632 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 20 29 *mutexShared; )
1c633 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 . pBt->nRef
1c634 20 3d 20 31 3b 0a 20 20 20 20 20 20 4d 55 54 45 = 1;. MUTE
1c635 58 5f 4c 4f 47 49 43 28 20 6d 75 74 65 78 53 68 X_LOGIC( mutexSh
1c636 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 ared = sqlite3Mu
1c637 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
1c638 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
1c639 54 45 52 29 3b 29 0a 20 20 20 20 20 20 69 66 28 TER);). if(
1c63a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
1c63b 46 45 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f FE && sqlite3Glo
1c63c 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d balConfig.bCoreM
1c63d 75 74 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 utex ){.
1c63e 70 42 74 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c pBt->mutex = sql
1c63f 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
1c640 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
1c641 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
1c642 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a Bt->mutex==0 ){.
1c643 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
1c644 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
1c645 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f db->mallo
1c646 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 cFailed = 0;.
1c647 20 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 goto btre
1c648 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 e_open_out;.
1c649 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1c64a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1c64b 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 x_enter(mutexSha
1c64c 72 65 64 29 3b 0a 20 20 20 20 20 20 70 42 74 2d red);. pBt-
1c64d 3e 70 4e 65 78 74 20 3d 20 47 4c 4f 42 41 4c 28 >pNext = GLOBAL(
1c64e 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 BtShared*,sqlite
1c64f 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 3SharedCacheList
1c650 29 3b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 );. GLOBAL(
1c651 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 BtShared*,sqlite
1c652 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 3SharedCacheList
1c653 29 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 73 ) = pBt;. s
1c654 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1c655 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b ve(mutexShared);
1c656 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
1c657 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 }..#if !defined(
1c658 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
1c659 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 ED_CACHE) && !de
1c65a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1c65b 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49 T_DISKIO). /* I
1c65c 66 20 74 68 65 20 6e 65 77 20 42 74 72 65 65 20 f the new Btree
1c65d 75 73 65 73 20 61 20 73 68 61 72 61 62 6c 65 20 uses a sharable
1c65e 70 42 74 53 68 61 72 65 64 2c 20 74 68 65 6e 20 pBtShared, then
1c65f 6c 69 6e 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a link the new. *
1c660 2a 20 42 74 72 65 65 20 69 6e 74 6f 20 74 68 65 * Btree into the
1c661 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 68 61 list of all sha
1c662 72 61 62 6c 65 20 42 74 72 65 65 73 20 66 6f 72 rable Btrees for
1c663 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 the same connec
1c664 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c tion.. ** The l
1c665 69 73 74 20 69 73 20 6b 65 70 74 20 69 6e 20 61 ist is kept in a
1c666 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 scending order b
1c667 79 20 70 42 74 20 61 64 64 72 65 73 73 2e 0a 20 y pBt address..
1c668 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 */. if( p->sha
1c669 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 rable ){. int
1c66a 20 69 3b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 i;. Btree *p
1c66b 53 69 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 Sib;. for(i=0
1c66c 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
1c66d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 53 ){. if( (pS
1c66e 69 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e ib = db->aDb[i].
1c66f 70 42 74 29 21 3d 30 20 26 26 20 70 53 69 62 2d pBt)!=0 && pSib-
1c670 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 >sharable ){.
1c671 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 while( pSib
1c672 2d 3e 70 50 72 65 76 20 29 7b 20 70 53 69 62 20 ->pPrev ){ pSib
1c673 3d 20 70 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d = pSib->pPrev; }
1c674 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e . if( p->
1c675 70 42 74 3c 70 53 69 62 2d 3e 70 42 74 20 29 7b pBt<pSib->pBt ){
1c676 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e . p->pN
1c677 65 78 74 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 ext = pSib;.
1c678 20 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d p->pPrev =
1c679 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 0;. pS
1c67a 69 62 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 ib->pPrev = p;.
1c67b 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1c67c 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 while( p
1c67d 53 69 62 2d 3e 70 4e 65 78 74 20 26 26 20 70 53 Sib->pNext && pS
1c67e 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3c 70 ib->pNext->pBt<p
1c67f 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 ->pBt ){.
1c680 20 20 20 20 20 70 53 69 62 20 3d 20 70 53 69 62 pSib = pSib
1c681 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 ->pNext;.
1c682 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 }. p
1c683 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e ->pNext = pSib->
1c684 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 pNext;.
1c685 20 70 2d 3e 70 50 72 65 76 20 3d 20 70 53 69 62 p->pPrev = pSib
1c686 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
1c687 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 p->pNext ){.
1c688 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 p->pNext
1c689 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 ->pPrev = p;.
1c68a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1c68b 20 20 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 3d pSib->pNext =
1c68c 20 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 p;. }.
1c68d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1c68e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 }. }. }.#
1c68f 65 6e 64 69 66 0a 20 20 2a 70 70 42 74 72 65 65 endif. *ppBtree
1c690 20 3d 20 70 3b 0a 0a 62 74 72 65 65 5f 6f 70 65 = p;..btree_ope
1c691 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21 n_out:. if( rc!
1c692 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1c693 20 20 69 66 28 20 70 42 74 20 26 26 20 70 42 74 if( pBt && pBt
1c694 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20 ->pPager ){.
1c695 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c sqlite3PagerCl
1c696 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 ose(pBt->pPager)
1c697 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
1c698 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 te3_free(pBt);.
1c699 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
1c69a 70 29 3b 0a 20 20 20 20 2a 70 70 42 74 72 65 65 p);. *ppBtree
1c69b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
1c69c 20 20 20 2f 2a 20 49 66 20 74 68 65 20 42 2d 54 /* If the B-T
1c69d 72 65 65 20 77 61 73 20 73 75 63 63 65 73 73 66 ree was successf
1c69e 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 73 65 74 ully opened, set
1c69f 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 the pager-cache
1c6a0 20 73 69 7a 65 20 74 6f 20 74 68 65 0a 20 20 20 size to the.
1c6a1 20 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c 75 ** default valu
1c6a2 65 2e 20 45 78 63 65 70 74 2c 20 77 68 65 6e 20 e. Except, when
1c6a3 6f 70 65 6e 69 6e 67 20 6f 6e 20 61 6e 20 65 78 opening on an ex
1c6a4 69 73 74 69 6e 67 20 73 68 61 72 65 64 20 70 61 isting shared pa
1c6a5 67 65 72 2d 63 61 63 68 65 2c 0a 20 20 20 20 2a ger-cache,. *
1c6a6 2a 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 * do not change
1c6a7 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 the pager-cache
1c6a8 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 size.. */.
1c6a9 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 if( sqlite3Btre
1c6aa 65 53 63 68 65 6d 61 28 70 2c 20 30 2c 20 30 29 eSchema(p, 0, 0)
1c6ab 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
1c6ac 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 ite3PagerSetCach
1c6ad 65 73 69 7a 65 28 70 2d 3e 70 42 74 2d 3e 70 50 esize(p->pBt->pP
1c6ae 61 67 65 72 2c 20 53 51 4c 49 54 45 5f 44 45 46 ager, SQLITE_DEF
1c6af 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 29 AULT_CACHE_SIZE)
1c6b0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
1c6b1 28 20 6d 75 74 65 78 4f 70 65 6e 20 29 7b 0a 20 ( mutexOpen ){.
1c6b2 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
1c6b3 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 75 e3_mutex_held(mu
1c6b4 74 65 78 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 texOpen) );.
1c6b5 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1c6b6 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a ave(mutexOpen);.
1c6b7 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1c6b8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d .}../*.** Decrem
1c6b9 65 6e 74 20 74 68 65 20 42 74 53 68 61 72 65 64 ent the BtShared
1c6ba 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e 20 20 .nRef counter.
1c6bb 57 68 65 6e 20 69 74 20 72 65 61 63 68 65 73 20 When it reaches
1c6bc 7a 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65 20 zero,.** remove
1c6bd 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74 72 the BtShared str
1c6be 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68 65 20 ucture from the
1c6bf 73 68 61 72 69 6e 67 20 6c 69 73 74 2e 20 20 52 sharing list. R
1c6c0 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 eturn.** true if
1c6c1 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52 the BtShared.nR
1c6c2 65 66 20 63 6f 75 6e 74 65 72 20 72 65 61 63 68 ef counter reach
1c6c3 65 73 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 es zero and retu
1c6c4 72 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 rn.** false if i
1c6c5 74 20 69 73 20 73 74 69 6c 6c 20 70 6f 73 69 74 t is still posit
1c6c6 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ive..*/.static i
1c6c7 6e 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61 nt removeFromSha
1c6c8 72 69 6e 67 4c 69 73 74 28 42 74 53 68 61 72 65 ringList(BtShare
1c6c9 64 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65 66 d *pBt){.#ifndef
1c6ca 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
1c6cb 52 45 44 5f 43 41 43 48 45 0a 20 20 4d 55 54 45 RED_CACHE. MUTE
1c6cc 58 5f 4c 4f 47 49 43 28 20 73 71 6c 69 74 65 33 X_LOGIC( sqlite3
1c6cd 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3b _mutex *pMaster;
1c6ce 20 29 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ). BtShared *p
1c6cf 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72 65 6d 6f List;. int remo
1c6d0 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 ved = 0;.. asse
1c6d1 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1c6d2 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d 3e 6d x_notheld(pBt->m
1c6d3 75 74 65 78 29 20 29 3b 0a 20 20 4d 55 54 45 58 utex) );. MUTEX
1c6d4 5f 4c 4f 47 49 43 28 20 70 4d 61 73 74 65 72 20 _LOGIC( pMaster
1c6d5 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c = sqlite3MutexAl
1c6d6 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 loc(SQLITE_MUTEX
1c6d7 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b _STATIC_MASTER);
1c6d8 20 29 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 ). sqlite3_mut
1c6d9 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 ex_enter(pMaster
1c6da 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d 2d );. pBt->nRef--
1c6db 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 52 65 ;. if( pBt->nRe
1c6dc 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 f<=0 ){. if(
1c6dd 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a GLOBAL(BtShared*
1c6de 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 ,sqlite3SharedCa
1c6df 63 68 65 4c 69 73 74 29 3d 3d 70 42 74 20 29 7b cheList)==pBt ){
1c6e0 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 . GLOBAL(Bt
1c6e1 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 Shared*,sqlite3S
1c6e2 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 20 haredCacheList)
1c6e3 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 = pBt->pNext;.
1c6e4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
1c6e5 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 List = GLOBAL(Bt
1c6e6 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 Shared*,sqlite3S
1c6e7 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b haredCacheList);
1c6e8 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 41 4c . while( AL
1c6e9 57 41 59 53 28 70 4c 69 73 74 29 20 26 26 20 70 WAYS(pList) && p
1c6ea 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42 74 List->pNext!=pBt
1c6eb 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 ){. pLis
1c6ec 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a t=pList->pNext;.
1c6ed 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
1c6ee 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20 ( ALWAYS(pList)
1c6ef 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 ){. pList
1c6f0 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 ->pNext = pBt->p
1c6f1 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Next;. }.
1c6f2 20 20 7d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 }. if( SQLI
1c6f3 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 29 7b TE_THREADSAFE ){
1c6f4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
1c6f5 75 74 65 78 5f 66 72 65 65 28 70 42 74 2d 3e 6d utex_free(pBt->m
1c6f6 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 utex);. }.
1c6f7 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 removed = 1;.
1c6f8 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 }. sqlite3_mute
1c6f9 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29 x_leave(pMaster)
1c6fa 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 6d 6f 76 ;. return remov
1c6fb 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 ed;.#else. retu
1c6fc 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a rn 1;.#endif.}..
1c6fd 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
1c6fe 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 70 pBt->pTmpSpace p
1c6ff 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f oints to an allo
1c700 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d 58 cation of .** MX
1c701 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 _CELL_SIZE(pBt)
1c702 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 bytes..*/.static
1c703 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74 65 54 65 void allocateTe
1c704 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 mpSpace(BtShared
1c705 20 2a 70 42 74 29 7b 0a 20 20 69 66 28 20 21 70 *pBt){. if( !p
1c706 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b Bt->pTmpSpace ){
1c707 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 . pBt->pTmpSp
1c708 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 ace = sqlite3Pag
1c709 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 eMalloc( pBt->pa
1c70a 67 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 7d 0a geSize );. }.}.
1c70b 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 ./*.** Free the
1c70c 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 61 pBt->pTmpSpace a
1c70d 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 llocation.*/.sta
1c70e 74 69 63 20 76 6f 69 64 20 66 72 65 65 54 65 6d tic void freeTem
1c70f 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 20 pSpace(BtShared
1c710 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 *pBt){. sqlite3
1c711 50 61 67 65 46 72 65 65 28 20 70 42 74 2d 3e 70 PageFree( pBt->p
1c712 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 70 42 74 TmpSpace);. pBt
1c713 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 30 3b ->pTmpSpace = 0;
1c714 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
1c715 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 an open database
1c716 20 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20 and invalidate
1c717 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a all cursors..*/.
1c718 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1c719 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
1c71a 6c 6f 73 65 28 42 74 72 65 65 20 2a 70 29 7b 0a lose(Btree *p){.
1c71b 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
1c71c 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 = p->pBt;. BtCu
1c71d 72 73 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f rsor *pCur;.. /
1c71e 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 * Close all curs
1c71f 6f 72 73 20 6f 70 65 6e 65 64 20 76 69 61 20 74 ors opened via t
1c720 68 69 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a his handle. */.
1c721 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1c722 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
1c723 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1c724 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1c725 72 28 70 29 3b 0a 20 20 70 43 75 72 20 3d 20 70 r(p);. pCur = p
1c726 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 Bt->pCursor;. w
1c727 68 69 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 hile( pCur ){.
1c728 20 20 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 BtCursor *pTmp
1c729 20 3d 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75 = pCur;. pCu
1c72a 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b r = pCur->pNext;
1c72b 0a 20 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 . if( pTmp->p
1c72c 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 Btree==p ){.
1c72d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c sqlite3BtreeCl
1c72e 6f 73 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b oseCursor(pTmp);
1c72f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
1c730 20 52 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 Rollback any ac
1c731 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e tive transaction
1c732 20 61 6e 64 20 66 72 65 65 20 74 68 65 20 68 61 and free the ha
1c733 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a ndle structure..
1c734 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f ** The call to
1c735 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c sqlite3BtreeRol
1c736 6c 62 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e lback() drops an
1c737 79 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 y table-locks he
1c738 6c 64 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 ld by. ** this
1c739 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 handle.. */. s
1c73a 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 qlite3BtreeRollb
1c73b 61 63 6b 28 70 2c 20 53 51 4c 49 54 45 5f 4f 4b ack(p, SQLITE_OK
1c73c 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
1c73d 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20 20 2f 2a eLeave(p);.. /*
1c73e 20 49 66 20 74 68 65 72 65 20 61 72 65 20 73 74 If there are st
1c73f 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74 73 74 61 ill other outsta
1c740 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 nding references
1c741 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 to the shared-b
1c742 74 72 65 65 0a 20 20 2a 2a 20 73 74 72 75 63 74 tree. ** struct
1c743 75 72 65 2c 20 72 65 74 75 72 6e 20 6e 6f 77 2e ure, return now.
1c744 20 54 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f The remainder o
1c745 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 f this procedure
1c746 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a 20 75 70 cleans . ** up
1c747 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 the shared-btre
1c748 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 e.. */. assert
1c749 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d ( p->wantToLock=
1c74a 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 3d =0 && p->locked=
1c74b 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 70 2d 3e =0 );. if( !p->
1c74c 73 68 61 72 61 62 6c 65 20 7c 7c 20 72 65 6d 6f sharable || remo
1c74d 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73 veFromSharingLis
1c74e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 2f 2a t(pBt) ){. /*
1c74f 20 54 68 65 20 70 42 74 20 69 73 20 6e 6f 20 6c The pBt is no l
1c750 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20 73 68 61 onger on the sha
1c751 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f 20 77 65 ring list, so we
1c752 20 63 61 6e 20 61 63 63 65 73 73 0a 20 20 20 20 can access.
1c753 2a 2a 20 69 74 20 77 69 74 68 6f 75 74 20 68 61 ** it without ha
1c754 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20 74 68 65 ving to hold the
1c755 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 mutex.. **.
1c756 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f 75 74 20 ** Clean out
1c757 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 42 and delete the B
1c758 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a tShared object..
1c759 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 */. asser
1c75a 74 28 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 t( !pBt->pCursor
1c75b 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 );. sqlite3P
1c75c 61 67 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 agerClose(pBt->p
1c75d 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 Pager);. if(
1c75e 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 pBt->xFreeSchema
1c75f 20 26 26 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 && pBt->pSchema
1c760 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 78 ){. pBt->x
1c761 46 72 65 65 53 63 68 65 6d 61 28 70 42 74 2d 3e FreeSchema(pBt->
1c762 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a pSchema);. }.
1c763 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1c764 65 28 30 2c 20 70 42 74 2d 3e 70 53 63 68 65 6d e(0, pBt->pSchem
1c765 61 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 a);. freeTemp
1c766 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 Space(pBt);.
1c767 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 sqlite3_free(pBt
1c768 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 );. }..#ifndef
1c769 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
1c76a 45 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 72 ED_CACHE. asser
1c76b 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b t( p->wantToLock
1c76c 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
1c76d 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b p->locked==0 );
1c76e 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 . if( p->pPrev
1c76f 29 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 ) p->pPrev->pNex
1c770 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 t = p->pNext;.
1c771 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 if( p->pNext ) p
1c772 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d ->pNext->pPrev =
1c773 20 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69 p->pPrev;.#endi
1c774 66 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 f.. sqlite3_fre
1c775 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 e(p);. return S
1c776 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1c777 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c 69 ** Change the li
1c778 6d 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 mit on the numbe
1c779 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 r of pages allow
1c77a 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e ed in the cache.
1c77b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d .**.** The maxim
1c77c 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 um number of cac
1c77d 68 65 20 70 61 67 65 73 20 69 73 20 73 65 74 20 he pages is set
1c77e 74 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0a to the absolute.
1c77f 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 ** value of mxPa
1c780 67 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69 ge. If mxPage i
1c781 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 s negative, the
1c782 70 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 pager will.** op
1c783 65 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f erate asynchrono
1c784 75 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e usly - it will n
1c785 6f 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 73 ot stop to do fs
1c786 79 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 ync()s.** to ins
1c787 75 72 65 20 64 61 74 61 20 69 73 20 77 72 69 74 ure data is writ
1c788 74 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b 20 ten to the disk
1c789 73 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a 2a surface before.*
1c78a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54 * continuing. T
1c78b 72 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 6c ransactions stil
1c78c 6c 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 72 l work if synchr
1c78d 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a onous is off,.**
1c78e 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 and the databas
1c78f 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72 e cannot be corr
1c790 75 70 74 65 64 20 69 66 20 74 68 69 73 20 70 72 upted if this pr
1c791 6f 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73 ogram.** crashes
1c792 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 6f 70 . But if the op
1c793 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 erating system c
1c794 72 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 20 rashes or there
1c795 69 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 20 is.** an abrupt
1c796 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 68 power failure wh
1c797 65 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 en synchronous i
1c798 73 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 62 s off, the datab
1c799 61 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20 ase.** could be
1c79a 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e left in an incon
1c79b 73 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 65 sistent and unre
1c79c 63 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 2e coverable state.
1c79d 0a 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20 .** Synchronous
1c79e 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 is on by default
1c79f 20 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 so database cor
1c7a0 72 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a ruption is not.*
1c7a1 2a 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72 * normally a wor
1c7a2 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ry..*/.SQLITE_PR
1c7a3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1c7a4 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 3BtreeSetCacheSi
1c7a5 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 ze(Btree *p, int
1c7a6 20 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 68 mxPage){. BtSh
1c7a7 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
1c7a8 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 Bt;. assert( sq
1c7a9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1c7aa 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
1c7ab 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1c7ac 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 Enter(p);. sqli
1c7ad 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 te3PagerSetCache
1c7ae 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 size(pBt->pPager
1c7af 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c , mxPage);. sql
1c7b0 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1c7b1 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
1c7b2 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
1c7b3 43 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 64 Change the way d
1c7b4 61 74 61 20 69 73 20 73 79 6e 63 65 64 20 74 6f ata is synced to
1c7b5 20 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 20 74 disk in order t
1c7b6 6f 20 69 6e 63 72 65 61 73 65 20 6f 72 20 64 65 o increase or de
1c7b7 63 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77 65 crease.** how we
1c7b8 6c 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ll the database
1c7b9 72 65 73 69 73 74 73 20 64 61 6d 61 67 65 20 64 resists damage d
1c7ba 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 ue to OS crashes
1c7bb 20 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66 61 and power.** fa
1c7bc 69 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c 20 31 ilures. Level 1
1c7bd 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
1c7be 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e 6f asynchronous (no
1c7bf 20 73 79 6e 63 73 28 29 20 6f 63 63 75 72 20 61 syncs() occur a
1c7c0 6e 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 nd.** there is a
1c7c1 20 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 high probabilit
1c7c2 79 20 6f 66 20 64 61 6d 61 67 65 29 20 20 4c 65 y of damage) Le
1c7c3 76 65 6c 20 32 20 69 73 20 74 68 65 20 64 65 66 vel 2 is the def
1c7c4 61 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a 20 ault. There.**
1c7c5 69 73 20 61 20 76 65 72 79 20 6c 6f 77 20 62 75 is a very low bu
1c7c6 74 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 t non-zero proba
1c7c7 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 bility of damage
1c7c8 2e 20 20 4c 65 76 65 6c 20 33 20 72 65 64 75 63 . Level 3 reduc
1c7c9 65 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61 62 es the.** probab
1c7ca 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 20 ility of damage
1c7cb 74 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75 74 to near zero but
1c7cc 20 77 69 74 68 20 61 20 77 72 69 74 65 20 70 65 with a write pe
1c7cd 72 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 63 74 rformance reduct
1c7ce 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ion..*/.#ifndef
1c7cf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 SQLITE_OMIT_PAGE
1c7d0 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 R_PRAGMAS.SQLITE
1c7d1 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1c7d2 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65 ite3BtreeSetSafe
1c7d3 74 79 4c 65 76 65 6c 28 0a 20 20 42 74 72 65 65 tyLevel(. Btree
1c7d4 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 *p,
1c7d5 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 74 /* The btree t
1c7d6 6f 20 73 65 74 20 74 68 65 20 73 61 66 65 74 79 o set the safety
1c7d7 20 6c 65 76 65 6c 20 6f 6e 20 2a 2f 0a 20 20 69 level on */. i
1c7d8 6e 74 20 6c 65 76 65 6c 2c 20 20 20 20 20 20 20 nt level,
1c7d9 20 20 20 20 20 20 2f 2a 20 50 52 41 47 4d 41 20 /* PRAGMA
1c7da 73 79 6e 63 68 72 6f 6e 6f 75 73 2e 20 20 31 3d synchronous. 1=
1c7db 4f 46 46 2c 20 32 3d 4e 4f 52 4d 41 4c 2c 20 33 OFF, 2=NORMAL, 3
1c7dc 3d 46 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 66 =FULL */. int f
1c7dd 75 6c 6c 53 79 6e 63 2c 20 20 20 20 20 20 20 20 ullSync,
1c7de 20 20 2f 2a 20 50 52 41 47 4d 41 20 66 75 6c 6c /* PRAGMA full
1c7df 66 73 79 6e 63 2e 20 2a 2f 0a 20 20 69 6e 74 20 fsync. */. int
1c7e0 63 6b 70 74 46 75 6c 6c 53 79 6e 63 20 20 20 20 ckptFullSync
1c7e1 20 20 20 2f 2a 20 50 52 41 47 4d 41 20 63 68 65 /* PRAGMA che
1c7e2 63 6b 70 6f 69 6e 74 5f 66 75 6c 6c 66 79 6e 63 ckpoint_fullfync
1c7e3 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 */.){. BtShare
1c7e4 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
1c7e5 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1c7e6 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
1c7e7 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
1c7e8 20 61 73 73 65 72 74 28 20 6c 65 76 65 6c 3e 3d assert( level>=
1c7e9 31 20 26 26 20 6c 65 76 65 6c 3c 3d 33 20 29 3b 1 && level<=3 );
1c7ea 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1c7eb 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 nter(p);. sqlit
1c7ec 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 e3PagerSetSafety
1c7ed 4c 65 76 65 6c 28 70 42 74 2d 3e 70 50 61 67 65 Level(pBt->pPage
1c7ee 72 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c 53 79 r, level, fullSy
1c7ef 6e 63 2c 20 63 6b 70 74 46 75 6c 6c 53 79 6e 63 nc, ckptFullSync
1c7f0 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
1c7f1 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
1c7f2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1c7f3 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
1c7f4 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 eturn TRUE if th
1c7f5 65 20 67 69 76 65 6e 20 62 74 72 65 65 20 69 73 e given btree is
1c7f6 20 73 65 74 20 74 6f 20 73 61 66 65 74 79 20 6c set to safety l
1c7f7 65 76 65 6c 20 31 2e 20 20 49 6e 20 6f 74 68 65 evel 1. In othe
1c7f8 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 72 65 74 75 r.** words, retu
1c7f9 72 6e 20 54 52 55 45 20 69 66 20 6e 6f 20 73 79 rn TRUE if no sy
1c7fa 6e 63 28 29 20 6f 63 63 75 72 73 20 6f 6e 20 74 nc() occurs on t
1c7fb 68 65 20 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a he disk files..*
1c7fc 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1c7fd 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
1c7fe 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 42 74 eSyncDisabled(Bt
1c7ff 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 ree *p){. BtSha
1c800 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
1c801 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 t;. int rc;. a
1c802 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1c803 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d utex_held(p->db-
1c804 3e 6d 75 74 65 78 29 20 29 3b 20 20 0a 20 20 73 >mutex) ); . s
1c805 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1c806 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 (p);. assert( p
1c807 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 Bt && pBt->pPage
1c808 72 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 r );. rc = sqli
1c809 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 70 te3PagerNosync(p
1c80a 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 73 Bt->pPager);. s
1c80b 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
1c80c 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
1c80d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
1c80e 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 e the default pa
1c80f 67 65 73 20 73 69 7a 65 20 61 6e 64 20 74 68 65 ges size and the
1c810 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 number of reser
1c811 76 65 64 20 62 79 74 65 73 20 70 65 72 20 70 61 ved bytes per pa
1c812 67 65 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 74 68 ge..** Or, if th
1c813 65 20 70 61 67 65 20 73 69 7a 65 20 68 61 73 20 e page size has
1c814 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66 69 78 already been fix
1c815 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ed, return SQLIT
1c816 45 5f 52 45 41 44 4f 4e 4c 59 20 0a 2a 2a 20 77 E_READONLY .** w
1c817 69 74 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 20 ithout changing
1c818 61 6e 79 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 anything..**.**
1c819 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d 75 The page size mu
1c81a 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 st be a power of
1c81b 20 32 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 2 between 512 a
1c81c 6e 64 20 36 35 35 33 36 2e 20 20 49 66 20 74 68 nd 65536. If th
1c81d 65 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 73 e page.** size s
1c81e 75 70 70 6c 69 65 64 20 64 6f 65 73 20 6e 6f 74 upplied does not
1c81f 20 6d 65 65 74 20 74 68 69 73 20 63 6f 6e 73 74 meet this const
1c820 72 61 69 6e 74 20 74 68 65 6e 20 74 68 65 20 70 raint then the p
1c821 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 0a age size is not.
1c822 2a 2a 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a ** changed..**.*
1c823 2a 20 50 61 67 65 20 73 69 7a 65 73 20 61 72 65 * Page sizes are
1c824 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74 6f 20 constrained to
1c825 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 be a power of tw
1c826 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 72 65 o so that the re
1c827 67 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 64 gion.** of the d
1c828 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 atabase file use
1c829 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 28 62 d for locking (b
1c82a 65 67 69 6e 6e 69 6e 67 20 61 74 20 50 45 4e 44 eginning at PEND
1c82b 49 4e 47 5f 42 59 54 45 2c 0a 2a 2a 20 74 68 65 ING_BYTE,.** the
1c82c 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73 74 first byte past
1c82d 20 74 68 65 20 31 47 42 20 62 6f 75 6e 64 61 72 the 1GB boundar
1c82e 79 2c 20 30 78 34 30 30 30 30 30 30 30 29 20 6e y, 0x40000000) n
1c82f 65 65 64 73 20 74 6f 20 6f 63 63 75 72 0a 2a 2a eeds to occur.**
1c830 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e at the beginnin
1c831 67 20 6f 66 20 61 20 70 61 67 65 2e 0a 2a 2a 0a g of a page..**.
1c832 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 ** If parameter
1c833 6e 52 65 73 65 72 76 65 20 69 73 20 6c 65 73 73 nReserve is less
1c834 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e than zero, then
1c835 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 the number of r
1c836 65 73 65 72 76 65 64 0a 2a 2a 20 62 79 74 65 73 eserved.** bytes
1c837 20 70 65 72 20 70 61 67 65 20 69 73 20 6c 65 66 per page is lef
1c838 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a t unchanged..**.
1c839 2a 2a 20 49 66 20 74 68 65 20 69 46 69 78 21 3d ** If the iFix!=
1c83a 30 20 74 68 65 6e 20 74 68 65 20 42 54 53 5f 50 0 then the BTS_P
1c83b 41 47 45 53 49 5a 45 5f 46 49 58 45 44 20 66 6c AGESIZE_FIXED fl
1c83c 61 67 20 69 73 20 73 65 74 20 73 6f 20 74 68 61 ag is set so tha
1c83d 74 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 0a t the page size.
1c83e 2a 2a 20 61 6e 64 20 61 75 74 6f 76 61 63 75 75 ** and autovacuu
1c83f 6d 20 6d 6f 64 65 20 63 61 6e 20 6e 6f 20 6c 6f m mode can no lo
1c840 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65 64 2e nger be changed.
1c841 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1c842 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
1c843 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42 reeSetPageSize(B
1c844 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70 61 67 tree *p, int pag
1c845 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 eSize, int nRese
1c846 72 76 65 2c 20 69 6e 74 20 69 46 69 78 29 7b 0a rve, int iFix){.
1c847 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1c848 45 5f 4f 4b 3b 0a 20 20 42 74 53 68 61 72 65 64 E_OK;. BtShared
1c849 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
1c84a 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 assert( nReser
1c84b 76 65 3e 3d 2d 31 20 26 26 20 6e 52 65 73 65 72 ve>=-1 && nReser
1c84c 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 73 71 6c ve<=255 );. sql
1c84d 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
1c84e 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 62 74 );. if( pBt->bt
1c84f 73 46 6c 61 67 73 20 26 20 42 54 53 5f 50 41 47 sFlags & BTS_PAG
1c850 45 53 49 5a 45 5f 46 49 58 45 44 20 29 7b 0a 20 ESIZE_FIXED ){.
1c851 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
1c852 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 eave(p);. ret
1c853 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f urn SQLITE_READO
1c854 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e NLY;. }. if( n
1c855 52 65 73 65 72 76 65 3c 30 20 29 7b 0a 20 20 20 Reserve<0 ){.
1c856 20 6e 52 65 73 65 72 76 65 20 3d 20 70 42 74 2d nReserve = pBt-
1c857 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d >pageSize - pBt-
1c858 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d >usableSize;. }
1c859 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 . assert( nRese
1c85a 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 rve>=0 && nReser
1c85b 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 69 66 28 ve<=255 );. if(
1c85c 20 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 pageSize>=512 &
1c85d 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 & pageSize<=SQLI
1c85e 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
1c85f 20 26 26 0a 20 20 20 20 20 20 20 20 28 28 70 61 &&. ((pa
1c860 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 geSize-1)&pageSi
1c861 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 ze)==0 ){. as
1c862 73 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 sert( (pageSize
1c863 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 & 7)==0 );. a
1c864 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 50 61 ssert( !pBt->pPa
1c865 67 65 31 20 26 26 20 21 70 42 74 2d 3e 70 43 75 ge1 && !pBt->pCu
1c866 72 73 6f 72 20 29 3b 0a 20 20 20 20 70 42 74 2d rsor );. pBt-
1c867 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 33 32 >pageSize = (u32
1c868 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 66 )pageSize;. f
1c869 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 reeTempSpace(pBt
1c86a 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 );. }. rc = sq
1c86b 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 lite3PagerSetPag
1c86c 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 esize(pBt->pPage
1c86d 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a r, &pBt->pageSiz
1c86e 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 e, nReserve);.
1c86f 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
1c870 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 = pBt->pageSize
1c871 2d 20 28 75 31 36 29 6e 52 65 73 65 72 76 65 3b - (u16)nReserve;
1c872 0a 20 20 69 66 28 20 69 46 69 78 20 29 20 70 42 . if( iFix ) pB
1c873 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 t->btsFlags |= B
1c874 54 53 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 45 TS_PAGESIZE_FIXE
1c875 44 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 D;. sqlite3Btre
1c876 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
1c877 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1c878 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 Return the curr
1c879 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 ently defined pa
1c87a 67 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 ge size.*/.SQLIT
1c87b 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1c87c 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
1c87d 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b eSize(Btree *p){
1c87e 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 . return p->pBt
1c87f 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 23 ->pageSize;.}..#
1c880 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
1c881 45 5f 48 41 53 5f 43 4f 44 45 43 29 20 7c 7c 20 E_HAS_CODEC) ||
1c882 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
1c883 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 EBUG)./*.** This
1c884 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 69 6d function is sim
1c885 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 42 ilar to sqlite3B
1c886 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28 29 treeGetReserve()
1c887 2c 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 , except that it
1c888 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 .** may only be
1c889 63 61 6c 6c 65 64 20 69 66 20 69 74 20 69 73 20 called if it is
1c88a 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 guaranteed that
1c88b 74 68 65 20 62 2d 74 72 65 65 20 6d 75 74 65 78 the b-tree mutex
1c88c 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 68 is already.** h
1c88d 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 eld..**.** This
1c88e 69 73 20 75 73 65 66 75 6c 20 69 6e 20 6f 6e 65 is useful in one
1c88f 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 69 6e special case in
1c890 20 74 68 65 20 62 61 63 6b 75 70 20 41 50 49 20 the backup API
1c891 63 6f 64 65 20 77 68 65 72 65 20 69 74 20 69 73 code where it is
1c892 0a 2a 2a 20 6b 6e 6f 77 6e 20 74 68 61 74 20 74 .** known that t
1c893 68 65 20 73 68 61 72 65 64 20 62 2d 74 72 65 65 he shared b-tree
1c894 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 2c 20 mutex is held,
1c895 62 75 74 20 74 68 65 20 6d 75 74 65 78 20 6f 6e but the mutex on
1c896 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 the .** databas
1c897 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 6f 77 e handle that ow
1c898 6e 73 20 2a 70 20 69 73 20 6e 6f 74 2e 20 49 6e ns *p is not. In
1c899 20 74 68 69 73 20 63 61 73 65 20 69 66 20 73 71 this case if sq
1c89a 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
1c89b 29 0a 2a 2a 20 77 65 72 65 20 74 6f 20 62 65 20 ).** were to be
1c89c 63 61 6c 6c 65 64 2c 20 69 74 20 6d 69 67 68 74 called, it might
1c89d 20 63 6f 6c 6c 69 64 65 20 77 69 74 68 20 73 6f collide with so
1c89e 6d 65 20 6f 74 68 65 72 20 6f 70 65 72 61 74 69 me other operati
1c89f 6f 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 74 on on the.** dat
1c8a0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 abase handle tha
1c8a1 74 20 6f 77 6e 73 20 2a 70 2c 20 63 61 75 73 69 t owns *p, causi
1c8a2 6e 67 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68 ng undefined beh
1c8a3 61 76 69 6f 75 72 2e 0a 2a 2f 0a 53 51 4c 49 54 aviour..*/.SQLIT
1c8a4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1c8a5 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 65 73 lite3BtreeGetRes
1c8a6 65 72 76 65 4e 6f 4d 75 74 65 78 28 42 74 72 65 erveNoMutex(Btre
1c8a7 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 e *p){. assert(
1c8a8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1c8a9 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 eld(p->pBt->mute
1c8aa 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 x) );. return p
1c8ab 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 ->pBt->pageSize
1c8ac 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 - p->pBt->usable
1c8ad 53 69 7a 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f Size;.}.#endif /
1c8ae 2a 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 * SQLITE_HAS_COD
1c8af 45 43 20 7c 7c 20 53 51 4c 49 54 45 5f 44 45 42 EC || SQLITE_DEB
1c8b0 55 47 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 UG */..#if !defi
1c8b1 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
1c8b2 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c PAGER_PRAGMAS) |
1c8b3 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 | !defined(SQLIT
1c8b4 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 0a 2f E_OMIT_VACUUM)./
1c8b5 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1c8b6 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
1c8b7 6f 66 20 73 70 61 63 65 20 61 74 20 74 68 65 20 of space at the
1c8b8 65 6e 64 20 6f 66 20 65 76 65 72 79 20 70 61 67 end of every pag
1c8b9 65 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 6e e that.** are in
1c8ba 74 65 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 75 tentually left u
1c8bb 6e 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20 nused. This is
1c8bc 74 68 65 20 22 72 65 73 65 72 76 65 64 22 20 73 the "reserved" s
1c8bd 70 61 63 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 pace that is.**
1c8be 73 6f 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62 sometimes used b
1c8bf 79 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f y extensions..*/
1c8c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1c8c1 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
1c8c2 47 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65 GetReserve(Btree
1c8c3 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 *p){. int n;.
1c8c4 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
1c8c5 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e er(p);. n = p->
1c8c6 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 pBt->pageSize -
1c8c7 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 p->pBt->usableSi
1c8c8 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 ze;. sqlite3Btr
1c8c9 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
1c8ca 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn n;.}../*.**
1c8cb 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d Set the maximum
1c8cc 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20 page count for
1c8cd 61 20 64 61 74 61 62 61 73 65 20 69 66 20 6d 78 a database if mx
1c8ce 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 Page is positive
1c8cf 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20 ..** No changes
1c8d0 61 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61 are made if mxPa
1c8d1 67 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74 ge is 0 or negat
1c8d2 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 ive..** Regardle
1c8d3 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 ss of the value
1c8d4 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 of mxPage, retur
1c8d5 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 n the maximum pa
1c8d6 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c ge count..*/.SQL
1c8d7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1c8d8 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 sqlite3BtreeMaxP
1c8d9 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a ageCount(Btree *
1c8da 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a p, int mxPage){.
1c8db 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 int n;. sqlit
1c8dc 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
1c8dd 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61 . n = sqlite3Pa
1c8de 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 gerMaxPageCount(
1c8df 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 p->pBt->pPager,
1c8e0 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 mxPage);. sqlit
1c8e1 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
1c8e2 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a . return n;.}..
1c8e3 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 42 54 /*.** Set the BT
1c8e4 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 S_SECURE_DELETE
1c8e5 66 6c 61 67 20 69 66 20 6e 65 77 46 6c 61 67 20 flag if newFlag
1c8e6 69 73 20 30 20 6f 72 20 31 2e 20 20 49 66 20 6e is 0 or 1. If n
1c8e7 65 77 46 6c 61 67 20 69 73 20 2d 31 2c 0a 2a 2a ewFlag is -1,.**
1c8e8 20 74 68 65 6e 20 6d 61 6b 65 20 6e 6f 20 63 68 then make no ch
1c8e9 61 6e 67 65 73 2e 20 20 41 6c 77 61 79 73 20 72 anges. Always r
1c8ea 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 eturn the value
1c8eb 6f 66 20 74 68 65 20 42 54 53 5f 53 45 43 55 52 of the BTS_SECUR
1c8ec 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 73 65 74 74 E_DELETE.** sett
1c8ed 69 6e 67 20 61 66 74 65 72 20 74 68 65 20 63 68 ing after the ch
1c8ee 61 6e 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ange..*/.SQLITE_
1c8ef 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1c8f0 74 65 33 42 74 72 65 65 53 65 63 75 72 65 44 65 te3BtreeSecureDe
1c8f1 6c 65 74 65 28 42 74 72 65 65 20 2a 70 2c 20 69 lete(Btree *p, i
1c8f2 6e 74 20 6e 65 77 46 6c 61 67 29 7b 0a 20 20 69 nt newFlag){. i
1c8f3 6e 74 20 62 3b 0a 20 20 69 66 28 20 70 3d 3d 30 nt b;. if( p==0
1c8f4 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 73 ) return 0;. s
1c8f5 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1c8f6 28 70 29 3b 0a 20 20 69 66 28 20 6e 65 77 46 6c (p);. if( newFl
1c8f7 61 67 3e 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e ag>=0 ){. p->
1c8f8 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 3d pBt->btsFlags &=
1c8f9 20 7e 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c ~BTS_SECURE_DEL
1c8fa 45 54 45 3b 0a 20 20 20 20 69 66 28 20 6e 65 77 ETE;. if( new
1c8fb 46 6c 61 67 20 29 20 70 2d 3e 70 42 74 2d 3e 62 Flag ) p->pBt->b
1c8fc 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 53 tsFlags |= BTS_S
1c8fd 45 43 55 52 45 5f 44 45 4c 45 54 45 3b 0a 20 20 ECURE_DELETE;.
1c8fe 7d 20 0a 20 20 62 20 3d 20 28 70 2d 3e 70 42 74 } . b = (p->pBt
1c8ff 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 ->btsFlags & BTS
1c900 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 29 21 _SECURE_DELETE)!
1c901 3d 30 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 =0;. sqlite3Btr
1c902 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
1c903 74 75 72 6e 20 62 3b 0a 7d 0a 23 65 6e 64 69 66 turn b;.}.#endif
1c904 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c /* !defined(SQL
1c905 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 ITE_OMIT_PAGER_P
1c906 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69 RAGMAS) || !defi
1c907 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
1c908 56 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f 2a 0a 2a VACUUM) */../*.*
1c909 2a 20 43 68 61 6e 67 65 20 74 68 65 20 27 61 75 * Change the 'au
1c90a 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 to-vacuum' prope
1c90b 72 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 rty of the datab
1c90c 61 73 65 2e 20 49 66 20 74 68 65 20 27 61 75 74 ase. If the 'aut
1c90d 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72 61 oVacuum'.** para
1c90e 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 meter is non-zer
1c90f 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 61 63 o, then auto-vac
1c910 75 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 uum mode is enab
1c911 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69 74 led. If zero, it
1c912 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64 2e .** is disabled.
1c913 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c The default val
1c914 75 65 20 66 6f 72 20 74 68 65 20 61 75 74 6f 2d ue for the auto-
1c915 76 61 63 75 75 6d 20 70 72 6f 70 65 72 74 79 20 vacuum property
1c916 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 is .** determine
1c917 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f d by the SQLITE_
1c918 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 DEFAULT_AUTOVACU
1c919 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 53 51 4c UM macro..*/.SQL
1c91a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1c91b 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 sqlite3BtreeSetA
1c91c 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 utoVacuum(Btree
1c91d 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 63 75 *p, int autoVacu
1c91e 75 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 um){.#ifdef SQLI
1c91f 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
1c920 55 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 UM. return SQLI
1c921 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65 6c TE_READONLY;.#el
1c922 73 65 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 se. BtShared *p
1c923 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 Bt = p->pBt;. i
1c924 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1c925 4b 3b 0a 20 20 75 38 20 61 76 20 3d 20 28 75 38 K;. u8 av = (u8
1c926 29 61 75 74 6f 56 61 63 75 75 6d 3b 0a 0a 20 20 )autoVacuum;..
1c927 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1c928 72 28 70 29 3b 0a 20 20 69 66 28 20 28 70 42 74 r(p);. if( (pBt
1c929 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 ->btsFlags & BTS
1c92a 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 45 44 29 _PAGESIZE_FIXED)
1c92b 21 3d 30 20 26 26 20 28 61 76 20 3f 31 3a 30 29 !=0 && (av ?1:0)
1c92c 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 !=pBt->autoVacuu
1c92d 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 m ){. rc = SQ
1c92e 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 LITE_READONLY;.
1c92f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d }else{. pBt-
1c930 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 >autoVacuum = av
1c931 20 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e ?1:0;. pBt->
1c932 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d incrVacuum = av=
1c933 3d 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 =2 ?1:0;. }. s
1c934 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
1c935 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
1c936 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a ;.#endif.}../*.*
1c937 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c * Return the val
1c938 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d ue of the 'auto-
1c939 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79 vacuum' property
1c93a 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d . If auto-vacuum
1c93b 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 is .** enabled
1c93c 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 1 is returned. O
1c93d 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 therwise 0..*/.S
1c93e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1c93f 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 t sqlite3BtreeGe
1c940 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 tAutoVacuum(Btre
1c941 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 e *p){.#ifdef SQ
1c942 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
1c943 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54 CUUM. return BT
1c944 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e REE_AUTOVACUUM_N
1c945 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 ONE;.#else. int
1c946 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 rc;. sqlite3Bt
1c947 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 reeEnter(p);. r
1c948 63 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70 c = (. (!p->p
1c949 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f Bt->autoVacuum)?
1c94a 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d BTREE_AUTOVACUUM
1c94b 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e _NONE:. (!p->
1c94c 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 pBt->incrVacuum)
1c94d 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 ?BTREE_AUTOVACUU
1c94e 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45 M_FULL:. BTRE
1c94f 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 E_AUTOVACUUM_INC
1c950 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 R. );. sqlite3
1c951 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
1c952 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 return rc;.#end
1c953 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 if.}.../*.** Get
1c954 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
1c955 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 61 pPage1 of the da
1c956 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 tabase file. Th
1c957 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 is will.** also
1c958 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f acquire a readlo
1c959 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e ck on that file.
1c95a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
1c95b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
1c95c 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65 success. If the
1c95d 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a file is not a.*
1c95e 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 * well-formed da
1c95f 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 tabase file, the
1c960 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
1c961 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
1c962 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 SQLITE_BUSY is
1c963 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 returned if the
1c964 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b database is lock
1c965 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 ed. SQLITE_NOME
1c966 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 M.** is returned
1c967 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f if we run out o
1c968 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 f memory. .*/.st
1c969 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 atic int lockBtr
1c96a 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ee(BtShared *pBt
1c96b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
1c96c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
1c96d 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 ult code from su
1c96e 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 bfunctions */.
1c96f 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b MemPage *pPage1;
1c970 20 20 20 20 20 2f 2a 20 50 61 67 65 20 31 20 6f /* Page 1 o
1c971 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
1c972 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 ile */. int nPa
1c973 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ge; /*
1c974 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
1c975 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1c976 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 46 */. int nPageF
1c977 69 6c 65 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75 ile = 0; /* Nu
1c978 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
1c979 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1c97a 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 le */. int nPag
1c97b 65 48 65 61 64 65 72 3b 20 20 20 20 20 2f 2a 20 eHeader; /*
1c97c 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
1c97d 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1c97e 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 68 64 72 according to hdr
1c97f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 */.. assert( s
1c980 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1c981 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
1c982 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
1c983 70 50 61 67 65 31 3d 3d 30 20 29 3b 0a 20 20 72 pPage1==0 );. r
1c984 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1c985 53 68 61 72 65 64 4c 6f 63 6b 28 70 42 74 2d 3e SharedLock(pBt->
1c986 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 72 pPager);. if( r
1c987 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c!=SQLITE_OK ) r
1c988 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d eturn rc;. rc =
1c989 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
1c98a 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c 20 30 t, 1, &pPage1, 0
1c98b 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
1c98c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 ITE_OK ) return
1c98d 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d rc;.. /* Do som
1c98e 65 20 63 68 65 63 6b 69 6e 67 20 74 6f 20 68 65 e checking to he
1c98f 6c 70 20 69 6e 73 75 72 65 20 74 68 65 20 66 69 lp insure the fi
1c990 6c 65 20 77 65 20 6f 70 65 6e 65 64 20 72 65 61 le we opened rea
1c991 6c 6c 79 20 69 73 0a 20 20 2a 2a 20 61 20 76 61 lly is. ** a va
1c992 6c 69 64 20 64 61 74 61 62 61 73 65 20 66 69 6c lid database fil
1c993 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e 50 61 67 65 e. . */. nPage
1c994 20 3d 20 6e 50 61 67 65 48 65 61 64 65 72 20 3d = nPageHeader =
1c995 20 67 65 74 34 62 79 74 65 28 32 38 2b 28 75 38 get4byte(28+(u8
1c996 2a 29 70 50 61 67 65 31 2d 3e 61 44 61 74 61 29 *)pPage1->aData)
1c997 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 ;. sqlite3Pager
1c998 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 Pagecount(pBt->p
1c999 50 61 67 65 72 2c 20 26 6e 50 61 67 65 46 69 6c Pager, &nPageFil
1c99a 65 29 3b 0a 20 20 69 66 28 20 6e 50 61 67 65 3d e);. if( nPage=
1c99b 3d 30 20 7c 7c 20 6d 65 6d 63 6d 70 28 32 34 2b =0 || memcmp(24+
1c99c 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 61 44 61 (u8*)pPage1->aDa
1c99d 74 61 2c 20 39 32 2b 28 75 38 2a 29 70 50 61 67 ta, 92+(u8*)pPag
1c99e 65 31 2d 3e 61 44 61 74 61 2c 34 29 21 3d 30 20 e1->aData,4)!=0
1c99f 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 6e ){. nPage = n
1c9a0 50 61 67 65 46 69 6c 65 3b 0a 20 20 7d 0a 20 20 PageFile;. }.
1c9a1 69 66 28 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 if( nPage>0 ){.
1c9a2 20 20 20 75 33 32 20 70 61 67 65 53 69 7a 65 3b u32 pageSize;
1c9a3 0a 20 20 20 20 75 33 32 20 75 73 61 62 6c 65 53 . u32 usableS
1c9a4 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a 70 61 67 ize;. u8 *pag
1c9a5 65 31 20 3d 20 70 50 61 67 65 31 2d 3e 61 44 61 e1 = pPage1->aDa
1c9a6 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ta;. rc = SQL
1c9a7 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20 ITE_NOTADB;.
1c9a8 69 66 28 20 6d 65 6d 63 6d 70 28 70 61 67 65 31 if( memcmp(page1
1c9a9 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20 , zMagicHeader,
1c9aa 31 36 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 16)!=0 ){.
1c9ab 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
1c9ac 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 23 failed;. }..#
1c9ad 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
1c9ae 54 5f 57 41 4c 0a 20 20 20 20 69 66 28 20 70 61 T_WAL. if( pa
1c9af 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0a 20 20 20 ge1[18]>1 ){.
1c9b0 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 pBt->btsFlags
1c9b1 20 7c 3d 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c |= BTS_READ_ONL
1c9b2 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 Y;. }. if(
1c9b3 20 70 61 67 65 31 5b 31 39 5d 3e 31 20 29 7b 0a page1[19]>1 ){.
1c9b4 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 goto page1
1c9b5 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 _init_failed;.
1c9b6 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 }.#else. if
1c9b7 28 20 70 61 67 65 31 5b 31 38 5d 3e 32 20 29 7b ( page1[18]>2 ){
1c9b8 0a 20 20 20 20 20 20 70 42 74 2d 3e 62 74 73 46 . pBt->btsF
1c9b9 6c 61 67 73 20 7c 3d 20 42 54 53 5f 52 45 41 44 lags |= BTS_READ
1c9ba 5f 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 20 _ONLY;. }.
1c9bb 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3e 32 if( page1[19]>2
1c9bc 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 ){. goto p
1c9bd 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 age1_init_failed
1c9be 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
1c9bf 49 66 20 74 68 65 20 77 72 69 74 65 20 76 65 72 If the write ver
1c9c0 73 69 6f 6e 20 69 73 20 73 65 74 20 74 6f 20 32 sion is set to 2
1c9c1 2c 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 , this database
1c9c2 73 68 6f 75 6c 64 20 62 65 20 61 63 63 65 73 73 should be access
1c9c3 65 64 0a 20 20 20 20 2a 2a 20 69 6e 20 57 41 4c ed. ** in WAL
1c9c4 20 6d 6f 64 65 2e 20 49 66 20 74 68 65 20 6c 6f mode. If the lo
1c9c5 67 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 g is not already
1c9c6 20 6f 70 65 6e 2c 20 6f 70 65 6e 20 69 74 20 6e open, open it n
1c9c7 6f 77 2e 20 54 68 65 6e 20 0a 20 20 20 20 2a 2a ow. Then . **
1c9c8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1c9c9 4b 20 61 6e 64 20 72 65 74 75 72 6e 20 77 69 74 K and return wit
1c9ca 68 6f 75 74 20 70 6f 70 75 6c 61 74 69 6e 67 20 hout populating
1c9cb 42 74 53 68 61 72 65 64 2e 70 50 61 67 65 31 2e BtShared.pPage1.
1c9cc 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c . ** The call
1c9cd 65 72 20 64 65 74 65 63 74 73 20 74 68 69 73 20 er detects this
1c9ce 61 6e 64 20 63 61 6c 6c 73 20 74 68 69 73 20 66 and calls this f
1c9cf 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 2e 20 54 unction again. T
1c9d0 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 his is. ** re
1c9d1 71 75 69 72 65 64 20 61 73 20 74 68 65 20 76 65 quired as the ve
1c9d2 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 20 31 20 rsion of page 1
1c9d3 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 currently in the
1c9d4 20 70 61 67 65 31 20 62 75 66 66 65 72 0a 20 20 page1 buffer.
1c9d5 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 ** may not be
1c9d6 74 68 65 20 6c 61 74 65 73 74 20 76 65 72 73 69 the latest versi
1c9d7 6f 6e 20 2d 20 74 68 65 72 65 20 6d 61 79 20 62 on - there may b
1c9d8 65 20 61 20 6e 65 77 65 72 20 6f 6e 65 20 69 6e e a newer one in
1c9d9 20 74 68 65 20 6c 6f 67 0a 20 20 20 20 2a 2a 20 the log. **
1c9da 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 file.. */.
1c9db 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3d 3d if( page1[19]==
1c9dc 32 20 26 26 20 28 70 42 74 2d 3e 62 74 73 46 6c 2 && (pBt->btsFl
1c9dd 61 67 73 20 26 20 42 54 53 5f 4e 4f 5f 57 41 4c ags & BTS_NO_WAL
1c9de 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e )==0 ){. in
1c9df 74 20 69 73 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 t isOpen = 0;.
1c9e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1c9e1 50 61 67 65 72 4f 70 65 6e 57 61 6c 28 70 42 74 PagerOpenWal(pBt
1c9e2 2d 3e 70 50 61 67 65 72 2c 20 26 69 73 4f 70 65 ->pPager, &isOpe
1c9e3 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 n);. if( rc
1c9e4 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1c9e5 20 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 goto page
1c9e6 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 1_init_failed;.
1c9e7 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 }else if( i
1c9e8 73 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 sOpen==0 ){.
1c9e9 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1c9ea 70 50 61 67 65 31 29 3b 0a 20 20 20 20 20 20 20 pPage1);.
1c9eb 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1c9ec 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 K;. }.
1c9ed 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 rc = SQLITE_NOT
1c9ee 41 44 42 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 ADB;. }.#endi
1c9ef 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 f.. /* The ma
1c9f0 78 69 6d 75 6d 20 65 6d 62 65 64 64 65 64 20 66 ximum embedded f
1c9f1 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 raction must be
1c9f2 65 78 61 63 74 6c 79 20 32 35 25 2e 20 20 41 6e exactly 25%. An
1c9f3 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 d the minimum.
1c9f4 20 20 2a 2a 20 65 6d 62 65 64 64 65 64 20 66 72 ** embedded fr
1c9f5 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 31 action must be 1
1c9f6 32 2e 35 25 20 66 6f 72 20 62 6f 74 68 20 6c 65 2.5% for both le
1c9f7 61 66 2d 64 61 74 61 20 61 6e 64 20 6e 6f 6e 2d af-data and non-
1c9f8 6c 65 61 66 2d 64 61 74 61 2e 0a 20 20 20 20 2a leaf-data.. *
1c9f9 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 * The original d
1c9fa 65 73 69 67 6e 20 61 6c 6c 6f 77 65 64 20 74 68 esign allowed th
1c9fb 65 73 65 20 61 6d 6f 75 6e 74 73 20 74 6f 20 76 ese amounts to v
1c9fc 61 72 79 2c 20 62 75 74 20 61 73 20 6f 66 0a 20 ary, but as of.
1c9fd 20 20 20 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e ** version 3.
1c9fe 36 2e 30 2c 20 77 65 20 72 65 71 75 69 72 65 20 6.0, we require
1c9ff 74 68 65 6d 20 74 6f 20 62 65 20 66 69 78 65 64 them to be fixed
1ca00 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1ca01 20 6d 65 6d 63 6d 70 28 26 70 61 67 65 31 5b 32 memcmp(&page1[2
1ca02 31 5d 2c 20 22 5c 31 30 30 5c 30 34 30 5c 30 34 1], "\100\040\04
1ca03 30 22 2c 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 0",3)!=0 ){.
1ca04 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 goto page1_ini
1ca05 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a t_failed;. }.
1ca06 20 20 20 20 70 61 67 65 53 69 7a 65 20 3d 20 28 pageSize = (
1ca07 70 61 67 65 31 5b 31 36 5d 3c 3c 38 29 20 7c 20 page1[16]<<8) |
1ca08 28 70 61 67 65 31 5b 31 37 5d 3c 3c 31 36 29 3b (page1[17]<<16);
1ca09 0a 20 20 20 20 69 66 28 20 28 28 70 61 67 65 53 . if( ((pageS
1ca0a 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29 ize-1)&pageSize)
1ca0b 21 3d 30 0a 20 20 20 20 20 7c 7c 20 70 61 67 65 !=0. || page
1ca0c 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f Size>SQLITE_MAX_
1ca0d 50 41 47 45 5f 53 49 5a 45 20 0a 20 20 20 20 20 PAGE_SIZE .
1ca0e 7c 7c 20 70 61 67 65 53 69 7a 65 3c 3d 32 35 36 || pageSize<=256
1ca0f 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 67 . ){. g
1ca10 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 oto page1_init_f
1ca11 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 ailed;. }.
1ca12 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53 69 assert( (pageSi
1ca13 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 ze & 7)==0 );.
1ca14 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 usableSize = p
1ca15 61 67 65 53 69 7a 65 20 2d 20 70 61 67 65 31 5b ageSize - page1[
1ca16 32 30 5d 3b 0a 20 20 20 20 69 66 28 20 28 75 33 20];. if( (u3
1ca17 32 29 70 61 67 65 53 69 7a 65 21 3d 70 42 74 2d 2)pageSize!=pBt-
1ca18 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 >pageSize ){.
1ca19 20 20 20 2f 2a 20 41 66 74 65 72 20 72 65 61 64 /* After read
1ca1a 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61 ing the first pa
1ca1b 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ge of the databa
1ca1c 73 65 20 61 73 73 75 6d 69 6e 67 20 61 20 70 61 se assuming a pa
1ca1d 67 65 20 73 69 7a 65 0a 20 20 20 20 20 20 2a 2a ge size. **
1ca1e 20 6f 66 20 42 74 53 68 61 72 65 64 2e 70 61 67 of BtShared.pag
1ca1f 65 53 69 7a 65 2c 20 77 65 20 68 61 76 65 20 64 eSize, we have d
1ca20 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 20 74 iscovered that t
1ca21 68 65 20 70 61 67 65 2d 73 69 7a 65 20 69 73 0a he page-size is.
1ca22 20 20 20 20 20 20 2a 2a 20 61 63 74 75 61 6c 6c ** actuall
1ca23 79 20 70 61 67 65 53 69 7a 65 2e 20 55 6e 6c 6f y pageSize. Unlo
1ca24 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c ck the database,
1ca25 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 61 67 leave pBt->pPag
1ca26 65 31 20 61 74 0a 20 20 20 20 20 20 2a 2a 20 7a e1 at. ** z
1ca27 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 20 53 ero and return S
1ca28 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 20 63 61 QLITE_OK. The ca
1ca29 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c 6c 20 74 ller will call t
1ca2a 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 his function.
1ca2b 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 74 68 ** again with
1ca2c 20 74 68 65 20 63 6f 72 72 65 63 74 20 70 61 67 the correct pag
1ca2d 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 20 2a 2f e-size.. */
1ca2e 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
1ca2f 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 20 ge(pPage1);.
1ca30 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a pBt->usableSiz
1ca31 65 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a e = usableSize;.
1ca32 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 pBt->pageS
1ca33 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 3b 0a ize = pageSize;.
1ca34 20 20 20 20 20 20 66 72 65 65 54 65 6d 70 53 70 freeTempSp
1ca35 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20 ace(pBt);.
1ca36 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1ca37 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 rSetPagesize(pBt
1ca38 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e ->pPager, &pBt->
1ca39 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 pageSize,.
1ca3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ca3b 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 pag
1ca3c 65 53 69 7a 65 2d 75 73 61 62 6c 65 53 69 7a 65 eSize-usableSize
1ca3d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1ca3e 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 rc;. }. if
1ca3f 28 20 28 70 42 74 2d 3e 64 62 2d 3e 66 6c 61 67 ( (pBt->db->flag
1ca40 73 20 26 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 s & SQLITE_Recov
1ca41 65 72 79 4d 6f 64 65 29 3d 3d 30 20 26 26 20 6e eryMode)==0 && n
1ca42 50 61 67 65 3e 6e 50 61 67 65 46 69 6c 65 20 29 Page>nPageFile )
1ca43 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
1ca44 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1ca45 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 ;. goto pag
1ca46 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a e1_init_failed;.
1ca47 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 73 }. if( us
1ca48 61 62 6c 65 53 69 7a 65 3c 34 38 30 20 29 7b 0a ableSize<480 ){.
1ca49 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 goto page1
1ca4a 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 _init_failed;.
1ca4b 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 }. pBt->pag
1ca4c 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 eSize = pageSize
1ca4d 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c ;. pBt->usabl
1ca4e 65 53 69 7a 65 20 3d 20 75 73 61 62 6c 65 53 69 eSize = usableSi
1ca4f 7a 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ze;.#ifndef SQLI
1ca50 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
1ca51 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f UM. pBt->auto
1ca52 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 Vacuum = (get4by
1ca53 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b 20 34 te(&page1[36 + 4
1ca54 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70 *4])?1:0);. p
1ca55 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d Bt->incrVacuum =
1ca56 20 28 67 65 74 34 62 79 74 65 28 26 70 61 67 65 (get4byte(&page
1ca57 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 1[36 + 7*4])?1:0
1ca58 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 );.#endif. }..
1ca59 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20 /* maxLocal is
1ca5a 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 the maximum amou
1ca5b 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f nt of payload to
1ca5c 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66 store locally f
1ca5d 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20 or. ** a cell.
1ca5e 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 Make sure it is
1ca5f 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f small enough so
1ca60 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 20 6d that at least m
1ca61 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65 inFanout. ** ce
1ca62 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 74 lls can will fit
1ca63 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 57 on one page. W
1ca64 65 20 61 73 73 75 6d 65 20 61 20 31 30 2d 62 79 e assume a 10-by
1ca65 74 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a te page header..
1ca66 20 20 2a 2a 20 42 65 73 69 64 65 73 20 74 68 65 ** Besides the
1ca67 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 65 payload, the ce
1ca68 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20 ll must store:.
1ca69 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 65 20 70 ** 2-byte p
1ca6a 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 ointer to the ce
1ca6b 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 ll. ** 4-by
1ca6c 74 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 te child pointer
1ca6d 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 65 . ** 9-byte
1ca6e 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a nKey value. **
1ca6f 20 20 20 20 20 34 2d 62 79 74 65 20 6e 44 61 74 4-byte nDat
1ca70 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 a value. **
1ca71 20 34 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 4-byte overflow
1ca72 20 70 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 20 page pointer.
1ca73 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e ** So a cell con
1ca74 73 69 73 74 73 20 6f 66 20 61 20 32 2d 62 79 74 sists of a 2-byt
1ca75 65 20 70 6f 69 6e 74 65 72 2c 20 61 20 68 65 61 e pointer, a hea
1ca76 64 65 72 20 77 68 69 63 68 20 69 73 20 61 73 20 der which is as
1ca77 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 much as. ** 17
1ca78 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f bytes long, 0 to
1ca79 20 4e 20 62 79 74 65 73 20 6f 66 20 70 61 79 6c N bytes of payl
1ca7a 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 oad, and an opti
1ca7b 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f 76 65 72 onal 4 byte over
1ca7c 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 flow. ** page p
1ca7d 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 ointer.. */. p
1ca7e 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 Bt->maxLocal = (
1ca7f 75 31 36 29 28 28 70 42 74 2d 3e 75 73 61 62 6c u16)((pBt->usabl
1ca80 65 53 69 7a 65 2d 31 32 29 2a 36 34 2f 32 35 35 eSize-12)*64/255
1ca81 20 2d 20 32 33 29 3b 0a 20 20 70 42 74 2d 3e 6d - 23);. pBt->m
1ca82 69 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 28 inLocal = (u16)(
1ca83 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 (pBt->usableSize
1ca84 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 -12)*32/255 - 23
1ca85 29 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65 61 );. pBt->maxLea
1ca86 66 20 3d 20 28 75 31 36 29 28 70 42 74 2d 3e 75 f = (u16)(pBt->u
1ca87 73 61 62 6c 65 53 69 7a 65 20 2d 20 33 35 29 3b sableSize - 35);
1ca88 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 20 . pBt->minLeaf
1ca89 3d 20 28 75 31 36 29 28 28 70 42 74 2d 3e 75 73 = (u16)((pBt->us
1ca8a 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f ableSize-12)*32/
1ca8b 32 35 35 20 2d 20 32 33 29 3b 0a 20 20 69 66 28 255 - 23);. if(
1ca8c 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3e 31 pBt->maxLocal>1
1ca8d 32 37 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 6d 27 ){. pBt->m
1ca8e 61 78 31 62 79 74 65 50 61 79 6c 6f 61 64 20 3d ax1bytePayload =
1ca8f 20 31 32 37 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 127;. }else{.
1ca90 20 20 20 70 42 74 2d 3e 6d 61 78 31 62 79 74 65 pBt->max1byte
1ca91 50 61 79 6c 6f 61 64 20 3d 20 28 75 38 29 70 42 Payload = (u8)pB
1ca92 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 7d t->maxLocal;. }
1ca93 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
1ca94 6d 61 78 4c 65 61 66 20 2b 20 32 33 20 3c 3d 20 maxLeaf + 23 <=
1ca95 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 MX_CELL_SIZE(pBt
1ca96 29 20 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 ) );. pBt->pPag
1ca97 65 31 20 3d 20 70 50 61 67 65 31 3b 0a 20 20 70 e1 = pPage1;. p
1ca98 42 74 2d 3e 6e 50 61 67 65 20 3d 20 6e 50 61 67 Bt->nPage = nPag
1ca99 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 e;. return SQLI
1ca9a 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 5f 69 6e TE_OK;..page1_in
1ca9b 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 72 65 6c it_failed:. rel
1ca9c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 29 easePage(pPage1)
1ca9d 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 ;. pBt->pPage1
1ca9e 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 = 0;. return rc
1ca9f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 ;.}../*.** If th
1caa0 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 ere are no outst
1caa1 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 20 61 anding cursors a
1caa2 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e nd we are not in
1caa3 20 74 68 65 20 6d 69 64 64 6c 65 0a 2a 2a 20 6f the middle.** o
1caa4 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
1caa5 62 75 74 20 74 68 65 72 65 20 69 73 20 61 20 72 but there is a r
1caa6 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ead lock on the
1caa7 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 2a database, then.*
1caa8 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 75 * this routine u
1caa9 6e 72 65 66 73 20 74 68 65 20 66 69 72 73 74 20 nrefs the first
1caaa 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 page of the data
1caab 62 61 73 65 20 66 69 6c 65 20 77 68 69 63 68 20 base file which
1caac 0a 2a 2a 20 68 61 73 20 74 68 65 20 65 66 66 65 .** has the effe
1caad 63 74 20 6f 66 20 72 65 6c 65 61 73 69 6e 67 20 ct of releasing
1caae 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a the read lock..*
1caaf 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 *.** If there is
1cab0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
1cab1 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68 69 73 n progress, this
1cab2 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
1cab3 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 -op..*/.static v
1cab4 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 oid unlockBtreeI
1cab5 66 55 6e 75 73 65 64 28 42 74 53 68 61 72 65 64 fUnused(BtShared
1cab6 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65 72 74 *pBt){. assert
1cab7 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1cab8 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
1cab9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 );. assert( pB
1caba 74 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 7c 7c t->pCursor==0 ||
1cabb 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
1cabc 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 ion>TRANS_NONE )
1cabd 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e 54 ;. if( pBt->inT
1cabe 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e ransaction==TRAN
1cabf 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d 3e 70 S_NONE && pBt->p
1cac0 50 61 67 65 31 21 3d 30 20 29 7b 0a 20 20 20 20 Page1!=0 ){.
1cac1 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 assert( pBt->pPa
1cac2 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 ge1->aData );.
1cac3 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1cac4 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 3PagerRefcount(p
1cac5 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 31 20 29 Bt->pPager)==1 )
1cac6 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 ;. assert( pB
1cac7 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
1cac8 20 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 );. releaseP
1cac9 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 31 29 age(pBt->pPage1)
1caca 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65 ;. pBt->pPage
1cacb 31 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 1 = 0;. }.}../*
1cacc 0a 2a 2a 20 49 66 20 70 42 74 20 70 6f 69 6e 74 .** If pBt point
1cacd 73 20 74 6f 20 61 6e 20 65 6d 70 74 79 20 66 69 s to an empty fi
1cace 6c 65 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 le then convert
1cacf 74 68 61 74 20 65 6d 70 74 79 20 66 69 6c 65 0a that empty file.
1cad0 2a 2a 20 69 6e 74 6f 20 61 20 6e 65 77 20 65 6d ** into a new em
1cad1 70 74 79 20 64 61 74 61 62 61 73 65 20 62 79 20 pty database by
1cad2 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 initializing the
1cad3 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 0a 2a first page of.*
1cad4 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a * the database..
1cad5 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65 */.static int ne
1cad6 77 44 61 74 61 62 61 73 65 28 42 74 53 68 61 72 wDatabase(BtShar
1cad7 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 6d 50 ed *pBt){. MemP
1cad8 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e 73 69 age *pP1;. unsi
1cad9 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b gned char *data;
1cada 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 . int rc;.. as
1cadb 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1cadc 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
1cadd 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 42 tex) );. if( pB
1cade 74 2d 3e 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 t->nPage>0 ){.
1cadf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1cae0 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 31 20 3d 20 OK;. }. pP1 =
1cae1 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 61 pBt->pPage1;. a
1cae2 73 73 65 72 74 28 20 70 50 31 21 3d 30 20 29 3b ssert( pP1!=0 );
1cae3 0a 20 20 64 61 74 61 20 3d 20 70 50 31 2d 3e 61 . data = pP1->a
1cae4 44 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c Data;. rc = sql
1cae5 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
1cae6 50 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 P1->pDbPage);.
1cae7 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
1cae8 72 63 3b 0a 20 20 6d 65 6d 63 70 79 28 64 61 74 rc;. memcpy(dat
1cae9 61 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c a, zMagicHeader,
1caea 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65 sizeof(zMagicHe
1caeb 61 64 65 72 29 29 3b 0a 20 20 61 73 73 65 72 74 ader));. assert
1caec 28 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 ( sizeof(zMagicH
1caed 65 61 64 65 72 29 3d 3d 31 36 20 29 3b 0a 20 20 eader)==16 );.
1caee 64 61 74 61 5b 31 36 5d 20 3d 20 28 75 38 29 28 data[16] = (u8)(
1caef 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3e (pBt->pageSize>>
1caf0 38 29 26 30 78 66 66 29 3b 0a 20 20 64 61 74 61 8)&0xff);. data
1caf1 5b 31 37 5d 20 3d 20 28 75 38 29 28 28 70 42 74 [17] = (u8)((pBt
1caf2 2d 3e 70 61 67 65 53 69 7a 65 3e 3e 31 36 29 26 ->pageSize>>16)&
1caf3 30 78 66 66 29 3b 0a 20 20 64 61 74 61 5b 31 38 0xff);. data[18
1caf4 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b 31 39 ] = 1;. data[19
1caf5 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 ] = 1;. assert(
1caf6 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
1caf7 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 <=pBt->pageSize
1caf8 26 26 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 && pBt->usableSi
1caf9 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e 70 61 67 ze+255>=pBt->pag
1cafa 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 32 eSize);. data[2
1cafb 30 5d 20 3d 20 28 75 38 29 28 70 42 74 2d 3e 70 0] = (u8)(pBt->p
1cafc 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 ageSize - pBt->u
1cafd 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 64 61 sableSize);. da
1cafe 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a 20 20 64 ta[21] = 64;. d
1caff 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b 0a 20 20 ata[22] = 32;.
1cb00 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 3b 0a 20 data[23] = 32;.
1cb01 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 32 34 memset(&data[24
1cb02 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 3b 0a 20 ], 0, 100-24);.
1cb03 20 7a 65 72 6f 50 61 67 65 28 70 50 31 2c 20 50 zeroPage(pP1, P
1cb04 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 TF_INTKEY|PTF_LE
1cb05 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 20 AF|PTF_LEAFDATA
1cb06 29 3b 0a 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 );. pBt->btsFla
1cb07 67 73 20 7c 3d 20 42 54 53 5f 50 41 47 45 53 49 gs |= BTS_PAGESI
1cb08 5a 45 5f 46 49 58 45 44 3b 0a 23 69 66 6e 64 65 ZE_FIXED;.#ifnde
1cb09 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1cb0a 54 4f 56 41 43 55 55 4d 0a 20 20 61 73 73 65 72 TOVACUUM. asser
1cb0b 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 t( pBt->autoVacu
1cb0c 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 75 um==1 || pBt->au
1cb0d 74 6f 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 toVacuum==0 );.
1cb0e 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e assert( pBt->in
1cb0f 63 72 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 crVacuum==1 || p
1cb10 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d Bt->incrVacuum==
1cb11 30 20 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 0 );. put4byte(
1cb12 26 64 61 74 61 5b 33 36 20 2b 20 34 2a 34 5d 2c &data[36 + 4*4],
1cb13 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
1cb14 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 );. put4byte(&d
1cb15 61 74 61 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 70 ata[36 + 7*4], p
1cb16 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3b Bt->incrVacuum);
1cb17 0a 23 65 6e 64 69 66 0a 20 20 70 42 74 2d 3e 6e .#endif. pBt->n
1cb18 50 61 67 65 20 3d 20 31 3b 0a 20 20 64 61 74 61 Page = 1;. data
1cb19 5b 33 31 5d 20 3d 20 31 3b 0a 20 20 72 65 74 75 [31] = 1;. retu
1cb1a 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1cb1b 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
1cb1c 65 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 e the first page
1cb1d 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1cb1e 20 66 69 6c 65 20 28 63 72 65 61 74 69 6e 67 20 file (creating
1cb1f 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f a database.** co
1cb20 6e 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73 69 nsisting of a si
1cb21 6e 67 6c 65 20 70 61 67 65 20 61 6e 64 20 6e 6f ngle page and no
1cb22 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 29 schema objects)
1cb23 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f . Return SQLITE_
1cb24 4f 4b 0a 2a 2a 20 69 66 20 73 75 63 63 65 73 73 OK.** if success
1cb25 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 ful, or an SQLit
1cb26 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68 e error code oth
1cb27 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 erwise..*/.SQLIT
1cb28 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1cb29 6c 69 74 65 33 42 74 72 65 65 4e 65 77 44 62 28 lite3BtreeNewDb(
1cb2a 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 Btree *p){. int
1cb2b 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 rc;. sqlite3Bt
1cb2c 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 reeEnter(p);. p
1cb2d 2d 3e 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 30 ->pBt->nPage = 0
1cb2e 3b 0a 20 20 72 63 20 3d 20 6e 65 77 44 61 74 61 ;. rc = newData
1cb2f 62 61 73 65 28 70 2d 3e 70 42 74 29 3b 0a 20 20 base(p->pBt);.
1cb30 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1cb31 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
1cb32 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 c;.}../*.** Atte
1cb33 6d 70 74 20 74 6f 20 73 74 61 72 74 20 61 20 6e mpt to start a n
1cb34 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ew transaction.
1cb35 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 A write-transact
1cb36 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 72 74 65 ion.** is starte
1cb37 64 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 d if the second
1cb38 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a argument is nonz
1cb39 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 61 ero, otherwise a
1cb3a 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61 read-.** transa
1cb3b 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 73 ction. If the s
1cb3c 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
1cb3d 73 20 32 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20 s 2 or more and
1cb3e 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61 exclusive.** tra
1cb3f 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 nsaction is star
1cb40 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 ted, meaning tha
1cb41 74 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 t no other proce
1cb42 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a ss is allowed.**
1cb43 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 to access the d
1cb44 61 74 61 62 61 73 65 2e 20 20 41 20 70 72 65 65 atabase. A pree
1cb45 78 69 73 74 69 6e 67 20 74 72 61 6e 73 61 63 74 xisting transact
1cb46 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a ion may not be.*
1cb47 2a 20 75 70 67 72 61 64 65 64 20 74 6f 20 65 78 * upgraded to ex
1cb48 63 6c 75 73 69 76 65 20 62 79 20 63 61 6c 6c 69 clusive by calli
1cb49 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ng this routine
1cb4a 61 20 73 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20 a second time -
1cb4b 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69 the.** exclusivi
1cb4c 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72 ty flag only wor
1cb4d 6b 73 20 66 6f 72 20 61 20 6e 65 77 20 74 72 61 ks for a new tra
1cb4e 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
1cb4f 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 A write-transact
1cb50 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 ion must be star
1cb51 74 65 64 20 62 65 66 6f 72 65 20 61 74 74 65 6d ted before attem
1cb52 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68 pting any .** ch
1cb53 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 anges to the dat
1cb54 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 abase. None of
1cb55 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f the following ro
1cb56 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20 utines .** will
1cb57 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61 20 74 72 work unless a tr
1cb58 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 ansaction is sta
1cb59 72 74 65 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a rted first:.**.*
1cb5a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 * sqlite3Bt
1cb5b 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 reeCreateTable()
1cb5c 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
1cb5d 42 74 72 65 65 43 72 65 61 74 65 49 6e 64 65 78 BtreeCreateIndex
1cb5e 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 ().** sqlit
1cb5f 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c e3BtreeClearTabl
1cb60 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 e().** sqli
1cb61 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c te3BtreeDropTabl
1cb62 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 e().** sqli
1cb63 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 29 te3BtreeInsert()
1cb64 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
1cb65 42 74 72 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a BtreeDelete().**
1cb66 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
1cb67 65 65 55 70 64 61 74 65 4d 65 74 61 28 29 0a 2a eeUpdateMeta().*
1cb68 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 69 74 69 *.** If an initi
1cb69 61 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 al attempt to ac
1cb6a 71 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 66 quire the lock f
1cb6b 61 69 6c 73 20 62 65 63 61 75 73 65 20 6f 66 20 ails because of
1cb6c 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a lock contention.
1cb6d 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 ** and the datab
1cb6e 61 73 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 ase was previous
1cb6f 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 ly unlocked, the
1cb70 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 n invoke the bus
1cb71 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20 y handler.** if
1cb72 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 42 there is one. B
1cb73 75 74 20 69 66 20 74 68 65 72 65 20 77 61 73 20 ut if there was
1cb74 70 72 65 76 69 6f 75 73 6c 79 20 61 20 72 65 61 previously a rea
1cb75 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a d-lock, do not.*
1cb76 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 * invoke the bus
1cb77 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74 y handler - just
1cb78 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
1cb79 55 53 59 2e 20 20 53 51 4c 49 54 45 5f 42 55 53 USY. SQLITE_BUS
1cb7a 59 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 Y is .** returne
1cb7b 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 d when there is
1cb7c 61 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c already a read-l
1cb7d 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ock in order to
1cb7e 61 76 6f 69 64 20 61 20 64 65 61 64 6c 6f 63 6b avoid a deadlock
1cb7f 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 ..**.** Suppose
1cb80 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 70 72 there are two pr
1cb81 6f 63 65 73 73 65 73 20 41 20 61 6e 64 20 42 2e ocesses A and B.
1cb82 20 20 41 20 68 61 73 20 61 20 72 65 61 64 20 6c A has a read l
1cb83 6f 63 6b 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a ock and B has.**
1cb84 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b a reserved lock
1cb85 2e 20 20 42 20 74 72 69 65 73 20 74 6f 20 70 72 . B tries to pr
1cb86 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c 75 73 69 omote to exclusi
1cb87 76 65 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 ve but is blocke
1cb88 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20 d because.** of
1cb89 41 27 73 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 A's read lock.
1cb8a 41 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f A tries to promo
1cb8b 74 65 20 74 6f 20 72 65 73 65 72 76 65 64 20 62 te to reserved b
1cb8c 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 ut is blocked by
1cb8d 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 B..** One or th
1cb8e 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 74 e other of the t
1cb8f 77 6f 20 70 72 6f 63 65 73 73 65 73 20 6d 75 73 wo processes mus
1cb90 74 20 67 69 76 65 20 77 61 79 20 6f 72 20 74 68 t give way or th
1cb91 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f ere can be.** no
1cb92 20 70 72 6f 67 72 65 73 73 2e 20 20 42 79 20 72 progress. By r
1cb93 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f eturning SQLITE_
1cb94 42 55 53 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 BUSY and not inv
1cb95 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 63 oking the busy c
1cb96 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20 allback.** when
1cb97 41 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 A already has a
1cb98 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e read lock, we en
1cb99 63 6f 75 72 61 67 65 20 41 20 74 6f 20 67 69 76 courage A to giv
1cb9a 65 20 75 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a e up and let B.*
1cb9b 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51 * proceed..*/.SQ
1cb9c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1cb9d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 sqlite3BtreeBeg
1cb9e 69 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 inTrans(Btree *p
1cb9f 2c 20 69 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20 , int wrflag){.
1cba0 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b sqlite3 *pBlock
1cba1 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64 = 0;. BtShared
1cba2 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
1cba3 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1cba4 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 E_OK;.. sqlite3
1cba5 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
1cba6 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 btreeIntegrity(
1cba7 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 p);.. /* If the
1cba8 20 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 64 btree is alread
1cba9 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 y in a write-tra
1cbaa 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a nsaction, or it.
1cbab 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 ** is already
1cbac 69 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 in a read-transa
1cbad 63 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 ction and a read
1cbae 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a -transaction. *
1cbaf 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 * is requested,
1cbb0 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e this is a no-op.
1cbb1 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 . */. if( p->i
1cbb2 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
1cbb3 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 ITE || (p->inTra
1cbb4 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 ns==TRANS_READ &
1cbb5 26 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 & !wrflag) ){.
1cbb6 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 goto trans_beg
1cbb7 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 un;. }.. /* Wr
1cbb8 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 ite transactions
1cbb9 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c are not possibl
1cbba 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 e on a read-only
1cbbb 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 database */. i
1cbbc 66 28 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 f( (pBt->btsFlag
1cbbd 73 20 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c s & BTS_READ_ONL
1cbbe 59 29 21 3d 30 20 26 26 20 77 72 66 6c 61 67 20 Y)!=0 && wrflag
1cbbf 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
1cbc0 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 TE_READONLY;.
1cbc1 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 goto trans_begu
1cbc2 6e 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 n;. }..#ifndef
1cbc3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
1cbc4 45 44 5f 43 41 43 48 45 0a 20 20 2f 2a 20 49 66 ED_CACHE. /* If
1cbc5 20 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 another databas
1cbc6 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 6c 72 e handle has alr
1cbc7 65 61 64 79 20 6f 70 65 6e 65 64 20 61 20 77 72 eady opened a wr
1cbc8 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite transaction
1cbc9 0a 20 20 2a 2a 20 6f 6e 20 74 68 69 73 20 73 68 . ** on this sh
1cbca 61 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 ared-btree struc
1cbcb 74 75 72 65 20 61 6e 64 20 61 20 73 65 63 6f 6e ture and a secon
1cbcc 64 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 d write transact
1cbcd 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 65 71 75 ion is. ** requ
1cbce 65 73 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51 ested, return SQ
1cbcf 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 2a LITE_LOCKED.. *
1cbd0 2f 0a 20 20 69 66 28 20 28 77 72 66 6c 61 67 20 /. if( (wrflag
1cbd1 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 && pBt->inTransa
1cbd2 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 ction==TRANS_WRI
1cbd3 54 45 29 0a 20 20 20 7c 7c 20 28 70 42 74 2d 3e TE). || (pBt->
1cbd4 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 50 btsFlags & BTS_P
1cbd5 45 4e 44 49 4e 47 29 21 3d 30 0a 20 20 29 7b 0a ENDING)!=0. ){.
1cbd6 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70 42 74 pBlock = pBt
1cbd7 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 3b 0a 20 ->pWriter->db;.
1cbd8 20 7d 65 6c 73 65 20 69 66 28 20 77 72 66 6c 61 }else if( wrfla
1cbd9 67 3e 31 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 g>1 ){. BtLoc
1cbda 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20 20 66 6f k *pIter;. fo
1cbdb 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f r(pIter=pBt->pLo
1cbdc 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 ck; pIter; pIter
1cbdd 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a =pIter->pNext){.
1cbde 20 20 20 20 20 20 69 66 28 20 70 49 74 65 72 2d if( pIter-
1cbdf 3e 70 42 74 72 65 65 21 3d 70 20 29 7b 0a 20 20 >pBtree!=p ){.
1cbe0 20 20 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70 pBlock = p
1cbe1 49 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 Iter->pBtree->db
1cbe2 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
1cbe3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1cbe4 20 7d 0a 20 20 69 66 28 20 70 42 6c 6f 63 6b 20 }. if( pBlock
1cbe5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f ){. sqlite3Co
1cbe6 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 nnectionBlocked(
1cbe7 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b 29 3b 0a p->db, pBlock);.
1cbe8 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1cbe9 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 LOCKED_SHAREDCAC
1cbea 48 45 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 HE;. goto tra
1cbeb 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 23 65 ns_begun;. }.#e
1cbec 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e 79 20 72 ndif.. /* Any r
1cbed 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 65 61 64 ead-only or read
1cbee 2d 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 -write transacti
1cbef 6f 6e 20 69 6d 70 6c 69 65 73 20 61 20 72 65 61 on implies a rea
1cbf0 64 2d 6c 6f 63 6b 20 6f 6e 20 0a 20 20 2a 2a 20 d-lock on . **
1cbf1 70 61 67 65 20 31 2e 20 53 6f 20 69 66 20 73 6f page 1. So if so
1cbf2 6d 65 20 6f 74 68 65 72 20 73 68 61 72 65 64 2d me other shared-
1cbf3 63 61 63 68 65 20 63 6c 69 65 6e 74 20 61 6c 72 cache client alr
1cbf4 65 61 64 79 20 68 61 73 20 61 20 77 72 69 74 65 eady has a write
1cbf5 2d 6c 6f 63 6b 20 0a 20 20 2a 2a 20 6f 6e 20 70 -lock . ** on p
1cbf6 61 67 65 20 31 2c 20 74 68 65 20 74 72 61 6e 73 age 1, the trans
1cbf7 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 action cannot be
1cbf8 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 72 63 opened. */. rc
1cbf9 20 3d 20 71 75 65 72 79 53 68 61 72 65 64 43 61 = querySharedCa
1cbfa 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 cheTableLock(p,
1cbfb 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41 MASTER_ROOT, REA
1cbfc 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 53 D_LOCK);. if( S
1cbfd 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 20 67 QLITE_OK!=rc ) g
1cbfe 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b oto trans_begun;
1cbff 0a 0a 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 .. pBt->btsFlag
1cc00 73 20 26 3d 20 7e 42 54 53 5f 49 4e 49 54 49 41 s &= ~BTS_INITIA
1cc01 4c 4c 59 5f 45 4d 50 54 59 3b 0a 20 20 69 66 28 LLY_EMPTY;. if(
1cc02 20 70 42 74 2d 3e 6e 50 61 67 65 3d 3d 30 20 29 pBt->nPage==0 )
1cc03 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c pBt->btsFlags |
1cc04 3d 20 42 54 53 5f 49 4e 49 54 49 41 4c 4c 59 5f = BTS_INITIALLY_
1cc05 45 4d 50 54 59 3b 0a 20 20 64 6f 20 7b 0a 20 20 EMPTY;. do {.
1cc06 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63 6b 42 74 /* Call lockBt
1cc07 72 65 65 28 29 20 75 6e 74 69 6c 20 65 69 74 68 ree() until eith
1cc08 65 72 20 70 42 74 2d 3e 70 50 61 67 65 31 20 69 er pBt->pPage1 i
1cc09 73 20 70 6f 70 75 6c 61 74 65 64 20 6f 72 0a 20 s populated or.
1cc0a 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72 65 65 28 ** lockBtree(
1cc0b 29 20 72 65 74 75 72 6e 73 20 73 6f 6d 65 74 68 ) returns someth
1cc0c 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 53 ing other than S
1cc0d 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63 6b 42 74 QLITE_OK. lockBt
1cc0e 72 65 65 28 29 0a 20 20 20 20 2a 2a 20 6d 61 79 ree(). ** may
1cc0f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1cc10 4b 20 62 75 74 20 6c 65 61 76 65 20 70 42 74 2d K but leave pBt-
1cc11 3e 70 50 61 67 65 31 20 73 65 74 20 74 6f 20 30 >pPage1 set to 0
1cc12 20 69 66 20 61 66 74 65 72 0a 20 20 20 20 2a 2a if after. **
1cc13 20 72 65 61 64 69 6e 67 20 70 61 67 65 20 31 20 reading page 1
1cc14 69 74 20 64 69 73 63 6f 76 65 72 73 20 74 68 61 it discovers tha
1cc15 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 t the page-size
1cc16 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1cc17 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 69 73 20 . ** file is
1cc18 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65 53 69 7a not pBt->pageSiz
1cc19 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 e. In this case
1cc1a 6c 6f 63 6b 42 74 72 65 65 28 29 20 77 69 6c 6c lockBtree() will
1cc1b 20 75 70 64 61 74 65 0a 20 20 20 20 2a 2a 20 70 update. ** p
1cc1c 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 74 6f 20 Bt->pageSize to
1cc1d 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 the page-size of
1cc1e 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 the file on dis
1cc1f 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 k.. */. wh
1cc20 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 67 65 31 ile( pBt->pPage1
1cc21 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b ==0 && SQLITE_OK
1cc22 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65 ==(rc = lockBtre
1cc23 65 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 20 20 e(pBt)) );..
1cc24 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1cc25 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 K && wrflag ){.
1cc26 20 20 20 20 20 69 66 28 20 28 70 42 74 2d 3e 62 if( (pBt->b
1cc27 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 52 45 tsFlags & BTS_RE
1cc28 41 44 5f 4f 4e 4c 59 29 21 3d 30 20 29 7b 0a 20 AD_ONLY)!=0 ){.
1cc29 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
1cc2a 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 TE_READONLY;.
1cc2b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1cc2c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1cc2d 67 65 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 gerBegin(pBt->pP
1cc2e 61 67 65 72 2c 77 72 66 6c 61 67 3e 31 2c 73 71 ager,wrflag>1,sq
1cc2f 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 lite3TempInMemor
1cc30 79 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 y(p->db));.
1cc31 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1cc32 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1cc33 20 20 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 rc = newDataba
1cc34 73 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 se(pBt);.
1cc35 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
1cc36 0a 20 20 0a 20 20 20 20 69 66 28 20 72 63 21 3d . . if( rc!=
1cc37 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1cc38 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 unlockBtreeIf
1cc39 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 Unused(pBt);.
1cc3a 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 28 72 63 }. }while( (rc
1cc3b 26 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 42 &0xFF)==SQLITE_B
1cc3c 55 53 59 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 USY && pBt->inTr
1cc3d 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
1cc3e 5f 4e 4f 4e 45 20 26 26 0a 20 20 20 20 20 20 20 _NONE &&.
1cc3f 20 20 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 btreeInvokeBu
1cc40 73 79 48 61 6e 64 6c 65 72 28 70 42 74 29 20 29 syHandler(pBt) )
1cc41 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c ;.. if( rc==SQL
1cc42 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 ITE_OK ){. if
1cc43 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
1cc44 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 ANS_NONE ){.
1cc45 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 pBt->nTransact
1cc46 69 6f 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66 20 53 ion++;.#ifndef S
1cc47 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
1cc48 44 5f 43 41 43 48 45 0a 20 20 20 20 20 20 69 66 D_CACHE. if
1cc49 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b ( p->sharable ){
1cc4a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1cc4b 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 3d p->lock.pBtree=
1cc4c 3d 70 20 26 26 20 70 2d 3e 6c 6f 63 6b 2e 69 54 =p && p->lock.iT
1cc4d 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20 able==1 );.
1cc4e 20 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b p->lock.eLock
1cc4f 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a 20 20 = READ_LOCK;.
1cc50 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 70 4e p->lock.pN
1cc51 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b ext = pBt->pLock
1cc52 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 70 ;. pBt->p
1cc53 4c 6f 63 6b 20 3d 20 26 70 2d 3e 6c 6f 63 6b 3b Lock = &p->lock;
1cc54 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
1cc55 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 }. p->inT
1cc56 72 61 6e 73 20 3d 20 28 77 72 66 6c 61 67 3f 54 rans = (wrflag?T
1cc57 52 41 4e 53 5f 57 52 49 54 45 3a 54 52 41 4e 53 RANS_WRITE:TRANS
1cc58 5f 52 45 41 44 29 3b 0a 20 20 20 20 69 66 28 20 _READ);. if(
1cc59 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e p->inTrans>pBt->
1cc5a 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b inTransaction ){
1cc5b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 . pBt->inTr
1cc5c 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 ansaction = p->i
1cc5d 6e 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 nTrans;. }.
1cc5e 20 20 69 66 28 20 77 72 66 6c 61 67 20 29 7b 0a if( wrflag ){.
1cc5f 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 MemPage *p
1cc60 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 Page1 = pBt->pPa
1cc61 67 65 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c ge1;.#ifndef SQL
1cc62 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
1cc63 43 41 43 48 45 0a 20 20 20 20 20 20 61 73 73 65 CACHE. asse
1cc64 72 74 28 20 21 70 42 74 2d 3e 70 57 72 69 74 65 rt( !pBt->pWrite
1cc65 72 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e r );. pBt->
1cc66 70 57 72 69 74 65 72 20 3d 20 70 3b 0a 20 20 20 pWriter = p;.
1cc67 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 pBt->btsFlags
1cc68 20 26 3d 20 7e 42 54 53 5f 45 58 43 4c 55 53 49 &= ~BTS_EXCLUSI
1cc69 56 45 3b 0a 20 20 20 20 20 20 69 66 28 20 77 72 VE;. if( wr
1cc6a 66 6c 61 67 3e 31 20 29 20 70 42 74 2d 3e 62 74 flag>1 ) pBt->bt
1cc6b 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 45 58 sFlags |= BTS_EX
1cc6c 43 4c 55 53 49 56 45 3b 0a 23 65 6e 64 69 66 0a CLUSIVE;.#endif.
1cc6d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
1cc6e 20 64 62 2d 73 69 7a 65 20 68 65 61 64 65 72 20 db-size header
1cc6f 66 69 65 6c 64 20 69 73 20 69 6e 63 6f 72 72 65 field is incorre
1cc70 63 74 20 28 61 73 20 69 74 20 6d 61 79 20 62 65 ct (as it may be
1cc71 20 69 66 20 61 6e 20 6f 6c 64 0a 20 20 20 20 20 if an old.
1cc72 20 2a 2a 20 63 6c 69 65 6e 74 20 68 61 73 20 62 ** client has b
1cc73 65 65 6e 20 77 72 69 74 69 6e 67 20 74 68 65 20 een writing the
1cc74 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 2c 20 database file),
1cc75 75 70 64 61 74 65 20 69 74 20 6e 6f 77 2e 20 44 update it now. D
1cc76 6f 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 74 68 oing. ** th
1cc77 69 73 20 73 6f 6f 6e 65 72 20 72 61 74 68 65 72 is sooner rather
1cc78 20 74 68 61 6e 20 6c 61 74 65 72 20 6d 65 61 6e than later mean
1cc79 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 s the database s
1cc7a 69 7a 65 20 63 61 6e 20 73 61 66 65 6c 79 20 0a ize can safely .
1cc7b 20 20 20 20 20 20 2a 2a 20 72 65 2d 72 65 61 64 ** re-read
1cc7c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69 the database si
1cc7d 7a 65 20 66 72 6f 6d 20 70 61 67 65 20 31 20 69 ze from page 1 i
1cc7e 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 6f 72 f a savepoint or
1cc7f 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 transaction.
1cc80 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6f ** rollback o
1cc81 63 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 65 ccurs within the
1cc82 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 transaction..
1cc83 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
1cc84 20 70 42 74 2d 3e 6e 50 61 67 65 21 3d 67 65 74 pBt->nPage!=get
1cc85 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 4byte(&pPage1->a
1cc86 44 61 74 61 5b 32 38 5d 29 20 29 7b 0a 20 20 20 Data[28]) ){.
1cc87 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1cc88 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 3PagerWrite(pPag
1cc89 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 e1->pDbPage);.
1cc8a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1cc8b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1cc8c 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
1cc8d 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 32 38 5d Page1->aData[28]
1cc8e 2c 20 70 42 74 2d 3e 6e 50 61 67 65 29 3b 0a 20 , pBt->nPage);.
1cc8f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1cc90 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 74 72 61 . }. }...tra
1cc91 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69 66 28 20 ns_begun:. if(
1cc92 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1cc93 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 2f wrflag ){. /
1cc94 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 61 6b 65 * This call make
1cc95 73 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 s sure that the
1cc96 70 61 67 65 72 20 68 61 73 20 74 68 65 20 63 6f pager has the co
1cc97 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66 0a rrect number of.
1cc98 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 61 76 65 ** open save
1cc99 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 65 20 73 points. If the s
1cc9a 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
1cc9b 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
1cc9c 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 68 65 0 and. ** the
1cc9d 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 sub-journal is
1cc9e 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e not already open
1cc9f 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 , then it will b
1cca0 65 20 6f 70 65 6e 65 64 20 68 65 72 65 2e 0a 20 e opened here..
1cca1 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 */. rc = s
1cca2 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 qlite3PagerOpenS
1cca3 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 avepoint(pBt->pP
1cca4 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e 6e 53 61 ager, p->db->nSa
1cca5 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a 0a 20 vepoint);. }..
1cca6 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 btreeIntegrity(
1cca7 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 p);. sqlite3Btr
1cca8 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
1cca9 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e turn rc;.}..#ifn
1ccaa 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1ccab 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a 0a 2a AUTOVACUUM../*.*
1ccac 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74 65 * Set the pointe
1ccad 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f r-map entries fo
1ccae 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f r all children o
1ccaf 66 20 70 61 67 65 20 70 50 61 67 65 2e 20 41 6c f page pPage. Al
1ccb0 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67 65 20 so, if.** pPage
1ccb1 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73 20 74 contains cells t
1ccb2 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 hat point to ove
1ccb3 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 73 65 74 rflow pages, set
1ccb4 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 the pointer.**
1ccb5 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 map entries for
1ccb6 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 the overflow pag
1ccb7 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 73 es as well..*/.s
1ccb8 74 61 74 69 63 20 69 6e 74 20 73 65 74 43 68 69 tatic int setChi
1ccb9 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d 50 61 67 ldPtrmaps(MemPag
1ccba 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 e *pPage){. int
1ccbb 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1ccbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ccbd 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 69 61 /* Counter varia
1ccbe 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 ble */. int nCe
1ccbf 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ll;
1ccc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1ccc1 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 umber of cells i
1ccc2 6e 20 70 61 67 65 20 70 50 61 67 65 20 2a 2f 0a n page pPage */.
1ccc3 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
1ccc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ccc5 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
1ccc6 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 ode */. BtShare
1ccc7 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e d *pBt = pPage->
1ccc8 70 42 74 3b 0a 20 20 75 38 20 69 73 49 6e 69 74 pBt;. u8 isInit
1ccc9 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73 Orig = pPage->is
1ccca 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e Init;. Pgno pgn
1cccb 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3b o = pPage->pgno;
1cccc 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
1cccd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1ccce 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
1cccf 29 20 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 ) );. rc = btre
1ccd0 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 eInitPage(pPage)
1ccd1 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
1ccd2 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 TE_OK ){. got
1ccd3 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d o set_child_ptrm
1ccd4 61 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e aps_out;. }. n
1ccd5 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 Cell = pPage->nC
1ccd6 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b ell;.. for(i=0;
1ccd7 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a i<nCell; i++){.
1ccd8 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 u8 *pCell =
1ccd9 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
1ccda 69 29 3b 0a 0a 20 20 20 20 70 74 72 6d 61 70 50 i);.. ptrmapP
1ccdb 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 2c utOvflPtr(pPage,
1ccdc 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 0a 20 pCell, &rc);..
1ccdd 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c if( !pPage->l
1ccde 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e eaf ){. Pgn
1ccdf 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65 o childPgno = ge
1cce0 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 20 t4byte(pCell);.
1cce1 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 ptrmapPut(p
1cce2 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 Bt, childPgno, P
1cce3 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e TRMAP_BTREE, pgn
1cce4 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 o, &rc);. }.
1cce5 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 }.. if( !pPage
1cce6 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67 ->leaf ){. Pg
1cce7 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 no childPgno = g
1cce8 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
1cce9 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
1ccea 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 Offset+8]);.
1cceb 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 63 ptrmapPut(pBt, c
1ccec 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 4d 41 50 hildPgno, PTRMAP
1cced 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c 20 26 72 _BTREE, pgno, &r
1ccee 63 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63 68 69 c);. }..set_chi
1ccef 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3a 0a ld_ptrmaps_out:.
1ccf0 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 pPage->isInit
1ccf1 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 20 = isInitOrig;.
1ccf2 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1ccf3 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20 6f 6e .** Somewhere on
1ccf4 20 70 50 61 67 65 20 69 73 20 61 20 70 6f 69 6e pPage is a poin
1ccf5 74 65 72 20 74 6f 20 70 61 67 65 20 69 46 72 6f ter to page iFro
1ccf6 6d 2e 20 20 4d 6f 64 69 66 79 20 74 68 69 73 20 m. Modify this
1ccf7 70 6f 69 6e 74 65 72 20 73 6f 0a 2a 2a 20 74 68 pointer so.** th
1ccf8 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 at it points to
1ccf9 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72 20 65 iTo. Parameter e
1ccfa 54 79 70 65 20 64 65 73 63 72 69 62 65 73 20 74 Type describes t
1ccfb 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69 6e 74 he type of point
1ccfc 65 72 20 74 6f 0a 2a 2a 20 62 65 20 6d 6f 64 69 er to.** be modi
1ccfd 66 69 65 64 2c 20 61 73 20 20 66 6f 6c 6c 6f 77 fied, as follow
1ccfe 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f s:.**.** PTRMAP_
1ccff 42 54 52 45 45 3a 20 20 20 20 20 70 50 61 67 65 BTREE: pPage
1cd00 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 67 65 is a btree-page
1cd01 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f . The pointer po
1cd02 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c 64 20 ints at a child
1cd03 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1cd04 20 20 20 20 20 20 70 61 67 65 20 6f 66 20 70 50 page of pP
1cd05 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 age..**.** PTRMA
1cd06 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70 50 61 P_OVERFLOW1: pPa
1cd07 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 ge is a btree-pa
1cd08 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 ge. The pointer
1cd09 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f 76 65 points at an ove
1cd0a 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 rflow.**
1cd0b 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20 page
1cd0c 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 6f 6e pointed to by on
1cd0d 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f e of the cells o
1cd0e 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 n pPage..**.** P
1cd0f 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a TRMAP_OVERFLOW2:
1cd10 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 pPage is an ove
1cd11 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68 65 20 rflow-page. The
1cd12 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 pointer points a
1cd13 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 t the next.**
1cd14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cd15 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e overflow page in
1cd16 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 the list..*/.st
1cd17 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66 79 50 atic int modifyP
1cd18 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d 50 61 agePointer(MemPa
1cd19 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e 6f 20 ge *pPage, Pgno
1cd1a 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54 6f 2c iFrom, Pgno iTo,
1cd1b 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20 61 73 u8 eType){. as
1cd1c 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1cd1d 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
1cd1e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1cd1f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1cd20 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
1cd21 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
1cd22 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70 65 3d );. if( eType=
1cd23 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 =PTRMAP_OVERFLOW
1cd24 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 2 ){. /* The
1cd25 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77 61 79 pointer is alway
1cd26 73 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79 s the first 4 by
1cd27 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 tes of the page
1cd28 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a in this case. *
1cd29 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34 62 79 /. if( get4by
1cd2a 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 te(pPage->aData)
1cd2b 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 !=iFrom ){.
1cd2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
1cd2d 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
1cd2e 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 }. put4byte(
1cd2f 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 pPage->aData, iT
1cd30 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 o);. }else{.
1cd31 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67 20 3d u8 isInitOrig =
1cd32 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a pPage->isInit;.
1cd33 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 int i;. i
1cd34 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 62 nt nCell;.. b
1cd35 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 treeInitPage(pPa
1cd36 67 65 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d ge);. nCell =
1cd37 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a pPage->nCell;..
1cd38 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e for(i=0; i<n
1cd39 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Cell; i++){.
1cd3a 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 u8 *pCell = fi
1cd3b 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 ndCell(pPage, i)
1cd3c 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70 ;. if( eTyp
1cd3d 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c e==PTRMAP_OVERFL
1cd3e 4f 57 31 20 29 7b 0a 20 20 20 20 20 20 20 20 43 OW1 ){. C
1cd3f 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 ellInfo info;.
1cd40 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 65 btreeParse
1cd41 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 CellPtr(pPage, p
1cd42 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 Cell, &info);.
1cd43 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 if( info.i
1cd44 4f 76 65 72 66 6c 6f 77 0a 20 20 20 20 20 20 20 Overflow.
1cd45 20 20 26 26 20 70 43 65 6c 6c 2b 69 6e 66 6f 2e && pCell+info.
1cd46 69 4f 76 65 72 66 6c 6f 77 2b 33 3c 3d 70 50 61 iOverflow+3<=pPa
1cd47 67 65 2d 3e 61 44 61 74 61 2b 70 50 61 67 65 2d ge->aData+pPage-
1cd48 3e 6d 61 73 6b 50 61 67 65 0a 20 20 20 20 20 20 >maskPage.
1cd49 20 20 20 26 26 20 69 46 72 6f 6d 3d 3d 67 65 74 && iFrom==get
1cd4a 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 4byte(&pCell[inf
1cd4b 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 0a 20 20 o.iOverflow]).
1cd4c 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
1cd4d 20 20 20 70 75 74 34 62 79 74 65 28 26 70 43 65 put4byte(&pCe
1cd4e 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
1cd4f 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 w], iTo);.
1cd50 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1cd51 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
1cd52 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 67 65 {. if( ge
1cd53 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3d 3d 69 t4byte(pCell)==i
1cd54 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 From ){.
1cd55 20 20 70 75 74 34 62 79 74 65 28 70 43 65 6c 6c put4byte(pCell
1cd56 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 , iTo);.
1cd57 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
1cd58 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
1cd59 0a 20 20 0a 20 20 20 20 69 66 28 20 69 3d 3d 6e . . if( i==n
1cd5a 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 Cell ){. if
1cd5b 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f ( eType!=PTRMAP_
1cd5c 42 54 52 45 45 20 7c 7c 20 0a 20 20 20 20 20 20 BTREE || .
1cd5d 20 20 20 20 67 65 74 34 62 79 74 65 28 26 70 50 get4byte(&pP
1cd5e 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 age->aData[pPage
1cd5f 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 21 ->hdrOffset+8])!
1cd60 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 =iFrom ){.
1cd61 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1cd62 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1cd63 20 20 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 }. put4
1cd64 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 byte(&pPage->aDa
1cd65 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 ta[pPage->hdrOff
1cd66 73 65 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 set+8], iTo);.
1cd67 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 2d 3e }.. pPage->
1cd68 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f isInit = isInitO
1cd69 72 69 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 rig;. }. retur
1cd6a 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1cd6b 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 ./*.** Move the
1cd6c 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 open database pa
1cd6d 67 65 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f ge pDbPage to lo
1cd6e 63 61 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65 cation iFreePage
1cd6f 20 69 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 in the .** data
1cd70 62 61 73 65 2e 20 54 68 65 20 70 44 62 50 61 67 base. The pDbPag
1cd71 65 20 72 65 66 65 72 65 6e 63 65 20 72 65 6d 61 e reference rema
1cd72 69 6e 73 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a ins valid..**.**
1cd73 20 54 68 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c The isCommit fl
1cd74 61 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 ag indicates tha
1cd75 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 t there is no ne
1cd76 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 ed to remember t
1cd77 68 61 74 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e hat.** the journ
1cd78 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 al needs to be s
1cd79 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 64 ync()ed before d
1cd7a 61 74 61 62 61 73 65 20 70 61 67 65 20 70 44 62 atabase page pDb
1cd7b 50 61 67 65 2d 3e 70 67 6e 6f 20 0a 2a 2a 20 63 Page->pgno .** c
1cd7c 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f an be written to
1cd7d 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73 . The caller has
1cd7e 20 61 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65 already promise
1cd7f 64 20 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74 d not to write t
1cd80 6f 20 74 68 61 74 0a 2a 2a 20 70 61 67 65 2e 0a o that.** page..
1cd81 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
1cd82 6c 6f 63 61 74 65 50 61 67 65 28 0a 20 20 42 74 locatePage(. Bt
1cd83 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 Shared *pBt,
1cd84 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 /* Btree
1cd85 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 44 */. MemPage *pD
1cd86 62 50 61 67 65 2c 20 20 20 20 20 20 20 20 2f 2a bPage, /*
1cd87 20 4f 70 65 6e 20 70 61 67 65 20 74 6f 20 6d 6f Open page to mo
1cd88 76 65 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 65 ve */. u8 eType
1cd89 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1cd8a 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 /* Pointer map
1cd8b 27 74 79 70 65 27 20 65 6e 74 72 79 20 66 6f 72 'type' entry for
1cd8c 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 pDbPage */. Pg
1cd8d 6e 6f 20 69 50 74 72 50 61 67 65 2c 20 20 20 20 no iPtrPage,
1cd8e 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
1cd8f 72 20 6d 61 70 20 27 70 61 67 65 2d 6e 6f 27 20 r map 'page-no'
1cd90 65 6e 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 entry for pDbPag
1cd91 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 46 72 65 e */. Pgno iFre
1cd92 65 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 ePage,
1cd93 2f 2a 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 /* The location
1cd94 74 6f 20 6d 6f 76 65 20 70 44 62 50 61 67 65 20 to move pDbPage
1cd95 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f to */. int isCo
1cd96 6d 6d 69 74 20 20 20 20 20 20 20 20 20 20 20 20 mmit
1cd97 20 2f 2a 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 /* isCommit fla
1cd98 67 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 g passed to sqli
1cd99 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 te3PagerMovepage
1cd9a 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 */.){. MemPage
1cd9b 20 2a 70 50 74 72 50 61 67 65 3b 20 20 20 2f 2a *pPtrPage; /*
1cd9c 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63 The page that c
1cd9d 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 ontains a pointe
1cd9e 72 20 74 6f 20 70 44 62 50 61 67 65 20 2a 2f 0a r to pDbPage */.
1cd9f 20 20 50 67 6e 6f 20 69 44 62 50 61 67 65 20 3d Pgno iDbPage =
1cda0 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a pDbPage->pgno;.
1cda1 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 Pager *pPager
1cda2 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 = pBt->pPager;.
1cda3 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 int rc;.. asse
1cda4 72 74 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 rt( eType==PTRMA
1cda5 50 5f 4f 56 45 52 46 4c 4f 57 32 20 7c 7c 20 65 P_OVERFLOW2 || e
1cda6 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 Type==PTRMAP_OVE
1cda7 52 46 4c 4f 57 31 20 7c 7c 20 0a 20 20 20 20 20 RFLOW1 || .
1cda8 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 eType==PTRMAP_B
1cda9 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 TREE || eType==P
1cdaa 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 TRMAP_ROOTPAGE )
1cdab 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
1cdac 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1cdad 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
1cdae 61 73 73 65 72 74 28 20 70 44 62 50 61 67 65 2d assert( pDbPage-
1cdaf 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a 0a 20 20 >pBt==pBt );..
1cdb0 2f 2a 20 4d 6f 76 65 20 70 61 67 65 20 69 44 62 /* Move page iDb
1cdb1 50 61 67 65 20 66 72 6f 6d 20 69 74 73 20 63 75 Page from its cu
1cdb2 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 74 rrent location t
1cdb3 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 46 o page number iF
1cdb4 72 65 65 50 61 67 65 20 2a 2f 0a 20 20 54 52 41 reePage */. TRA
1cdb5 43 45 28 28 22 41 55 54 4f 56 41 43 55 55 4d 3a CE(("AUTOVACUUM:
1cdb6 20 4d 6f 76 69 6e 67 20 25 64 20 74 6f 20 66 72 Moving %d to fr
1cdb7 65 65 20 70 61 67 65 20 25 64 20 28 70 74 72 20 ee page %d (ptr
1cdb8 70 61 67 65 20 25 64 20 74 79 70 65 20 25 64 29 page %d type %d)
1cdb9 5c 6e 22 2c 20 0a 20 20 20 20 20 20 69 44 62 50 \n", . iDbP
1cdba 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20 age, iFreePage,
1cdbb 69 50 74 72 50 61 67 65 2c 20 65 54 79 70 65 29 iPtrPage, eType)
1cdbc 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1cdbd 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 70 3PagerMovepage(p
1cdbe 50 61 67 65 72 2c 20 70 44 62 50 61 67 65 2d 3e Pager, pDbPage->
1cdbf 70 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 pDbPage, iFreePa
1cdc0 67 65 2c 20 69 73 43 6f 6d 6d 69 74 29 3b 0a 20 ge, isCommit);.
1cdc1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1cdc2 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OK ){. return
1cdc3 20 72 63 3b 0a 20 20 7d 0a 20 20 70 44 62 50 61 rc;. }. pDbPa
1cdc4 67 65 2d 3e 70 67 6e 6f 20 3d 20 69 46 72 65 65 ge->pgno = iFree
1cdc5 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 Page;.. /* If p
1cdc6 44 62 50 61 67 65 20 77 61 73 20 61 20 62 74 72 DbPage was a btr
1cdc7 65 65 2d 70 61 67 65 2c 20 74 68 65 6e 20 69 74 ee-page, then it
1cdc8 20 6d 61 79 20 68 61 76 65 20 63 68 69 6c 64 20 may have child
1cdc9 70 61 67 65 73 20 61 6e 64 2f 6f 72 20 63 65 6c pages and/or cel
1cdca 6c 73 0a 20 20 2a 2a 20 74 68 61 74 20 70 6f 69 ls. ** that poi
1cdcb 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 nt to overflow p
1cdcc 61 67 65 73 2e 20 54 68 65 20 70 6f 69 6e 74 65 ages. The pointe
1cdcd 72 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f r map entries fo
1cdce 72 20 61 6c 6c 20 74 68 65 73 65 0a 20 20 2a 2a r all these. **
1cdcf 20 70 61 67 65 73 20 6e 65 65 64 20 74 6f 20 62 pages need to b
1cdd0 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a e changed.. **.
1cdd1 20 20 2a 2a 20 49 66 20 70 44 62 50 61 67 65 20 ** If pDbPage
1cdd2 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 is an overflow p
1cdd3 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 66 69 age, then the fi
1cdd4 72 73 74 20 34 20 62 79 74 65 73 20 6d 61 79 20 rst 4 bytes may
1cdd5 73 74 6f 72 65 20 61 0a 20 20 2a 2a 20 70 6f 69 store a. ** poi
1cdd6 6e 74 65 72 20 74 6f 20 61 20 73 75 62 73 65 71 nter to a subseq
1cdd7 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 uent overflow pa
1cdd8 67 65 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 ge. If this is t
1cdd9 68 65 20 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 he case, then.
1cdda 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d ** the pointer m
1cddb 61 70 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 ap needs to be u
1cddc 70 64 61 74 65 64 20 66 6f 72 20 74 68 65 20 73 pdated for the s
1cddd 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c ubsequent overfl
1cdde 6f 77 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 ow page.. */.
1cddf 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 if( eType==PTRMA
1cde0 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65 P_BTREE || eType
1cde1 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 ==PTRMAP_ROOTPAG
1cde2 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 65 E ){. rc = se
1cde3 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 70 44 tChildPtrmaps(pD
1cde4 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 bPage);. if(
1cde5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1cde6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
1cde7 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b ;. }. }else{
1cde8 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 4f 76 . Pgno nextOv
1cde9 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 70 44 fl = get4byte(pD
1cdea 62 50 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 bPage->aData);.
1cdeb 20 20 20 69 66 28 20 6e 65 78 74 4f 76 66 6c 21 if( nextOvfl!
1cdec 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d =0 ){. ptrm
1cded 61 70 50 75 74 28 70 42 74 2c 20 6e 65 78 74 4f apPut(pBt, nextO
1cdee 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 vfl, PTRMAP_OVER
1cdef 46 4c 4f 57 32 2c 20 69 46 72 65 65 50 61 67 65 FLOW2, iFreePage
1cdf0 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66 , &rc);. if
1cdf1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1cdf2 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
1cdf3 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
1cdf4 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 }. }.. /* Fi
1cdf5 78 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 x the database p
1cdf6 6f 69 6e 74 65 72 20 6f 6e 20 70 61 67 65 20 69 ointer on page i
1cdf7 50 74 72 50 61 67 65 20 74 68 61 74 20 70 6f 69 PtrPage that poi
1cdf8 6e 74 65 64 20 61 74 20 69 44 62 50 61 67 65 20 nted at iDbPage
1cdf9 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20 so. ** that it
1cdfa 70 6f 69 6e 74 73 20 61 74 20 69 46 72 65 65 50 points at iFreeP
1cdfb 61 67 65 2e 20 41 6c 73 6f 20 66 69 78 20 74 68 age. Also fix th
1cdfc 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e e pointer map en
1cdfd 74 72 79 20 66 6f 72 0a 20 20 2a 2a 20 69 50 74 try for. ** iPt
1cdfe 72 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 rPage.. */. if
1cdff 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f ( eType!=PTRMAP_
1ce00 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 ROOTPAGE ){.
1ce01 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
1ce02 65 28 70 42 74 2c 20 69 50 74 72 50 61 67 65 2c e(pBt, iPtrPage,
1ce03 20 26 70 50 74 72 50 61 67 65 2c 20 30 29 3b 0a &pPtrPage, 0);.
1ce04 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1ce05 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
1ce06 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
1ce07 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1ce08 50 61 67 65 72 57 72 69 74 65 28 70 50 74 72 50 PagerWrite(pPtrP
1ce09 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
1ce0a 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1ce0b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
1ce0c 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50 61 leasePage(pPtrPa
1ce0d 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ge);. retur
1ce0e 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
1ce0f 72 63 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 rc = modifyPageP
1ce10 6f 69 6e 74 65 72 28 70 50 74 72 50 61 67 65 2c ointer(pPtrPage,
1ce11 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 iDbPage, iFreeP
1ce12 61 67 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 age, eType);.
1ce13 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 releasePage(pPt
1ce14 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 rPage);. if(
1ce15 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1ce16 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 . ptrmapPut
1ce17 28 70 42 74 2c 20 69 46 72 65 65 50 61 67 65 2c (pBt, iFreePage,
1ce18 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 eType, iPtrPage
1ce19 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 , &rc);. }.
1ce1a 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1ce1b 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 ../* Forward dec
1ce1c 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65 laration require
1ce1d 64 20 62 79 20 69 6e 63 72 56 61 63 75 75 6d 53 d by incrVacuumS
1ce1e 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 tep(). */.static
1ce1f 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 int allocateBtr
1ce20 65 65 50 61 67 65 28 42 74 53 68 61 72 65 64 20 eePage(BtShared
1ce21 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 *, MemPage **, P
1ce22 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 gno *, Pgno, u8)
1ce23 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d ;../*.** Perform
1ce24 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f a single step o
1ce25 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c f an incremental
1ce26 2d 76 61 63 75 75 6d 2e 20 49 66 20 73 75 63 63 -vacuum. If succ
1ce27 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 essful,.** retur
1ce28 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 n SQLITE_OK. If
1ce29 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b there is no work
1ce2a 20 74 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 72 to do (and ther
1ce2b 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e efore no.** poin
1ce2c 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68 69 t in calling thi
1ce2d 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e s function again
1ce2e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ), return SQLITE
1ce2f 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 _DONE..**.** Mor
1ce30 65 20 73 70 65 63 69 66 69 63 6c 79 2c 20 74 68 e specificly, th
1ce31 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 is function atte
1ce32 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e mpts to re-organ
1ce33 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 ize the .** data
1ce34 62 61 73 65 20 73 6f 20 74 68 61 74 20 74 68 65 base so that the
1ce35 20 6c 61 73 74 20 70 61 67 65 20 6f 66 20 74 68 last page of th
1ce36 65 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 e file currently
1ce37 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e 6f in use.** is no
1ce38 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a longer in use..
1ce39 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46 69 **.** If the nFi
1ce3a 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e n parameter is n
1ce3b 6f 6e 2d 7a 65 72 6f 2c 20 74 68 69 73 20 66 75 on-zero, this fu
1ce3c 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 0a 2a nction assumes.*
1ce3d 2a 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 * that the calle
1ce3e 72 20 77 69 6c 6c 20 6b 65 65 70 20 63 61 6c 6c r will keep call
1ce3f 69 6e 67 20 69 6e 63 72 56 61 63 75 75 6d 53 74 ing incrVacuumSt
1ce40 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a 20 69 74 ep() until.** it
1ce41 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
1ce42 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72 72 6f 72 DONE or an error
1ce43 2c 20 61 6e 64 20 74 68 61 74 20 6e 46 69 6e 20 , and that nFin
1ce44 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 is the.** number
1ce45 20 6f 66 20 70 61 67 65 73 20 74 68 65 20 64 61 of pages the da
1ce46 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c tabase file will
1ce47 20 63 6f 6e 74 61 69 6e 20 61 66 74 65 72 20 74 contain after t
1ce48 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65 73 73 20 his .** process
1ce49 69 73 20 63 6f 6d 70 6c 65 74 65 2e 20 20 49 66 is complete. If
1ce4a 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 69 nFin is zero, i
1ce4b 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 t is assumed tha
1ce4c 74 0a 2a 2a 20 69 6e 63 72 56 61 63 75 75 6d 53 t.** incrVacuumS
1ce4d 74 65 70 28 29 20 77 69 6c 6c 20 62 65 20 63 61 tep() will be ca
1ce4e 6c 6c 65 64 20 61 20 66 69 6e 69 74 65 20 61 6d lled a finite am
1ce4f 6f 75 6e 74 20 6f 66 20 74 69 6d 65 73 0a 2a 2a ount of times.**
1ce50 20 77 68 69 63 68 20 6d 61 79 20 6f 72 20 6d 61 which may or ma
1ce51 79 20 6e 6f 74 20 65 6d 70 74 79 20 74 68 65 20 y not empty the
1ce52 66 72 65 65 6c 69 73 74 2e 20 20 41 20 66 75 6c freelist. A ful
1ce53 6c 20 61 75 74 6f 76 61 63 75 75 6d 0a 2a 2a 20 l autovacuum.**
1ce54 68 61 73 20 6e 46 69 6e 3e 30 2e 20 20 41 20 22 has nFin>0. A "
1ce55 50 52 41 47 4d 41 20 69 6e 63 72 65 6d 65 6e 74 PRAGMA increment
1ce56 61 6c 5f 76 61 63 75 75 6d 22 20 68 61 73 20 6e al_vacuum" has n
1ce57 46 69 6e 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 74 69 Fin==0..*/.stati
1ce58 63 20 69 6e 74 20 69 6e 63 72 56 61 63 75 75 6d c int incrVacuum
1ce59 53 74 65 70 28 42 74 53 68 61 72 65 64 20 2a 70 Step(BtShared *p
1ce5a 42 74 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c 20 50 Bt, Pgno nFin, P
1ce5b 67 6e 6f 20 69 4c 61 73 74 50 67 29 7b 0a 20 20 gno iLastPg){.
1ce5c 50 67 6e 6f 20 6e 46 72 65 65 4c 69 73 74 3b 20 Pgno nFreeList;
1ce5d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1ce5e 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 ber of pages sti
1ce5f 6c 6c 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c ll on the free-l
1ce60 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b ist */. int rc;
1ce61 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
1ce62 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1ce63 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
1ce64 61 73 73 65 72 74 28 20 69 4c 61 73 74 50 67 3e assert( iLastPg>
1ce65 6e 46 69 6e 20 29 3b 0a 0a 20 20 69 66 28 20 21 nFin );.. if( !
1ce66 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 PTRMAP_ISPAGE(pB
1ce67 74 2c 20 69 4c 61 73 74 50 67 29 20 26 26 20 69 t, iLastPg) && i
1ce68 4c 61 73 74 50 67 21 3d 50 45 4e 44 49 4e 47 5f LastPg!=PENDING_
1ce69 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
1ce6a 7b 0a 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a {. u8 eType;.
1ce6b 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 Pgno iPtrPag
1ce6c 65 3b 0a 0a 20 20 20 20 6e 46 72 65 65 4c 69 73 e;.. nFreeLis
1ce6d 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 t = get4byte(&pB
1ce6e 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
1ce6f 5b 33 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e [36]);. if( n
1ce70 46 72 65 65 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 FreeList==0 ){.
1ce71 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1ce72 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a TE_DONE;. }..
1ce73 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 rc = ptrmapG
1ce74 65 74 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c et(pBt, iLastPg,
1ce75 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 &eType, &iPtrPa
1ce76 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 ge);. if( rc!
1ce77 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1ce78 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1ce79 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 54 79 }. if( eTy
1ce7a 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 pe==PTRMAP_ROOTP
1ce7b 41 47 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 AGE ){. ret
1ce7c 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
1ce7d 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a PT_BKPT;. }..
1ce7e 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 if( eType==P
1ce7f 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 TRMAP_FREEPAGE )
1ce80 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e {. if( nFin
1ce81 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ==0 ){. /
1ce82 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67 * Remove the pag
1ce83 65 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73 e from the files
1ce84 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 free-list. This
1ce85 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 is not required
1ce86 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20 6e . ** if n
1ce87 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e Fin is non-zero.
1ce88 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74 In that case, t
1ce89 68 65 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c he free-list wil
1ce8a 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 l be. **
1ce8b 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 truncated to zer
1ce8c 6f 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e o after this fun
1ce8d 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73 ction returns, s
1ce8e 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20 o it doesn't .
1ce8f 20 20 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20 ** matter
1ce90 69 66 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74 if it still cont
1ce91 61 69 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67 ains some garbag
1ce92 65 20 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 e entries..
1ce93 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 */. Pg
1ce94 6e 6f 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20 no iFreePg;.
1ce95 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 MemPage *pFr
1ce96 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 eePg;. rc
1ce97 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
1ce98 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 Page(pBt, &pFree
1ce99 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c Pg, &iFreePg, iL
1ce9a 61 73 74 50 67 2c 20 31 29 3b 0a 20 20 20 20 20 astPg, 1);.
1ce9b 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1ce9c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1ce9d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1ce9e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 }. a
1ce9f 73 73 65 72 74 28 20 69 46 72 65 65 50 67 3d 3d ssert( iFreePg==
1cea0 69 4c 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 iLastPg );.
1cea1 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1cea2 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d FreePg);. }
1cea3 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 . } else {.
1cea4 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 Pgno iFreePg
1cea5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1cea6 20 49 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70 Index of free p
1cea7 61 67 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73 age to move pLas
1cea8 74 50 67 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 tPg to */.
1cea9 4d 65 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67 MemPage *pLastPg
1ceaa 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 ;.. rc = bt
1ceab 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
1ceac 69 4c 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50 iLastPg, &pLastP
1cead 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 g, 0);. if(
1ceae 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1ceaf 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
1ceb0 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 rc;. }..
1ceb1 20 20 20 20 2f 2a 20 49 66 20 6e 46 69 6e 20 69 /* If nFin i
1ceb2 73 20 7a 65 72 6f 2c 20 74 68 69 73 20 6c 6f 6f s zero, this loo
1ceb3 70 20 72 75 6e 73 20 65 78 61 63 74 6c 79 20 6f p runs exactly o
1ceb4 6e 63 65 20 61 6e 64 20 70 61 67 65 20 70 4c 61 nce and page pLa
1ceb5 73 74 50 67 0a 20 20 20 20 20 20 2a 2a 20 69 73 stPg. ** is
1ceb6 20 73 77 61 70 70 65 64 20 77 69 74 68 20 74 68 swapped with th
1ceb7 65 20 66 69 72 73 74 20 66 72 65 65 20 70 61 67 e first free pag
1ceb8 65 20 70 75 6c 6c 65 64 20 6f 66 66 20 74 68 65 e pulled off the
1ceb9 20 66 72 65 65 20 6c 69 73 74 2e 0a 20 20 20 20 free list..
1ceba 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f 6e **. ** On
1cebb 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c the other hand,
1cebc 20 69 66 20 6e 46 69 6e 20 69 73 20 67 72 65 61 if nFin is grea
1cebd 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 ter than zero, t
1cebe 68 65 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a hen keep. *
1cebf 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 * looping until
1cec0 61 20 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61 a free-page loca
1cec1 74 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 66 ted within the f
1cec2 69 72 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a irst nFin pages.
1cec3 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 ** of the
1cec4 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20 file is found..
1cec5 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f */. do
1cec6 20 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 {. MemPa
1cec7 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 ge *pFreePg;.
1cec8 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 rc = alloca
1cec9 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
1ceca 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 &pFreePg, &iFre
1cecb 65 50 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 ePg, 0, 0);.
1cecc 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1cecd 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1cece 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1cecf 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20 LastPg);.
1ced0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1ced1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1ced2 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 releasePage(pFre
1ced3 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 ePg);. }whi
1ced4 6c 65 28 20 6e 46 69 6e 21 3d 30 20 26 26 20 69 le( nFin!=0 && i
1ced5 46 72 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20 FreePg>nFin );.
1ced6 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46 72 assert( iFr
1ced7 65 65 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a eePg<iLastPg );.
1ced8 20 20 20 20 20 20 0a 20 20 20 20 20 20 72 63 20 . rc
1ced9 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1ceda 69 74 65 28 70 4c 61 73 74 50 67 2d 3e 70 44 62 ite(pLastPg->pDb
1cedb 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 Page);. if(
1cedc 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1cedd 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 {. rc = r
1cede 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c elocatePage(pBt,
1cedf 20 70 4c 61 73 74 50 67 2c 20 65 54 79 70 65 2c pLastPg, eType,
1cee0 20 69 50 74 72 50 61 67 65 2c 20 69 46 72 65 65 iPtrPage, iFree
1cee1 50 67 2c 20 6e 46 69 6e 21 3d 30 29 3b 0a 20 20 Pg, nFin!=0);.
1cee2 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 }. rele
1cee3 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 asePage(pLastPg)
1cee4 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
1cee5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1cee6 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1cee7 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1cee8 7d 0a 0a 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30 }.. if( nFin==0
1cee9 20 29 7b 0a 20 20 20 20 69 4c 61 73 74 50 67 2d ){. iLastPg-
1ceea 2d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c -;. while( iL
1ceeb 61 73 74 50 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 astPg==PENDING_B
1ceec 59 54 45 5f 50 41 47 45 28 70 42 74 29 7c 7c 50 YTE_PAGE(pBt)||P
1ceed 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 TRMAP_ISPAGE(pBt
1ceee 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 , iLastPg) ){.
1ceef 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49 if( PTRMAP_I
1cef0 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 SPAGE(pBt, iLast
1cef1 50 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 4d Pg) ){. M
1cef2 65 6d 50 61 67 65 20 2a 70 50 67 3b 0a 20 20 20 emPage *pPg;.
1cef3 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 rc = btreeG
1cef4 65 74 50 61 67 65 28 70 42 74 2c 20 69 4c 61 73 etPage(pBt, iLas
1cef5 74 50 67 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20 tPg, &pPg, 0);.
1cef6 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
1cef7 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1cef8 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1cef9 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1cefa 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1cefb 61 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70 agerWrite(pPg->p
1cefc 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
1cefd 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 67 releasePage(pPg
1cefe 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
1ceff 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1cf00 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
1cf01 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc;. }.
1cf02 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 4c 61 }. iLa
1cf03 73 74 50 67 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 stPg--;. }.
1cf04 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 sqlite3PagerTr
1cf05 75 6e 63 61 74 65 49 6d 61 67 65 28 70 42 74 2d uncateImage(pBt-
1cf06 3e 70 50 61 67 65 72 2c 20 69 4c 61 73 74 50 67 >pPager, iLastPg
1cf07 29 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 );. pBt->nPag
1cf08 65 20 3d 20 69 4c 61 73 74 50 67 3b 0a 20 20 7d e = iLastPg;. }
1cf09 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1cf0a 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 _OK;.}../*.** A
1cf0b 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
1cf0c 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 n must be opened
1cf0d 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 before calling
1cf0e 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a this function..*
1cf0f 2a 20 49 74 20 70 65 72 66 6f 72 6d 73 20 61 20 * It performs a
1cf10 73 69 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 77 single unit of w
1cf11 6f 72 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 69 ork towards an i
1cf12 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 ncremental vacuu
1cf13 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 m..**.** If the
1cf14 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 incremental vacu
1cf15 75 6d 20 69 73 20 66 69 6e 69 73 68 65 64 20 61 um is finished a
1cf16 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 fter this functi
1cf17 6f 6e 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 on has run,.** S
1cf18 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 QLITE_DONE is re
1cf19 74 75 72 6e 65 64 2e 20 49 66 20 69 74 20 69 73 turned. If it is
1cf1a 20 6e 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 62 not finished, b
1cf1b 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 ut no error occu
1cf1c 72 72 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f rred,.** SQLITE_
1cf1d 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 OK is returned.
1cf1e 4f 74 68 65 72 77 69 73 65 20 61 6e 20 53 51 4c Otherwise an SQL
1cf1f 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 ite error code.
1cf20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1cf21 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
1cf22 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 42 74 reeIncrVacuum(Bt
1cf23 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 ree *p){. int r
1cf24 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 c;. BtShared *p
1cf25 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 Bt = p->pBt;..
1cf26 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1cf27 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r(p);. assert(
1cf28 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
1cf29 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 on==TRANS_WRITE
1cf2a 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 && p->inTrans==T
1cf2b 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 RANS_WRITE );.
1cf2c 69 66 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 if( !pBt->autoVa
1cf2d 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d cuum ){. rc =
1cf2e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
1cf2f 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c }else{. inval
1cf30 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 idateAllOverflow
1cf31 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 20 20 Cache(pBt);.
1cf32 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 rc = incrVacuumS
1cf33 74 65 70 28 70 42 74 2c 20 30 2c 20 62 74 72 65 tep(pBt, 0, btre
1cf34 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 29 ePagecount(pBt))
1cf35 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1cf36 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1cf37 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1cf38 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61 erWrite(pBt->pPa
1cf39 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 ge1->pDbPage);.
1cf3a 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
1cf3b 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
1cf3c 61 5b 32 38 5d 2c 20 70 42 74 2d 3e 6e 50 61 67 a[28], pBt->nPag
1cf3d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 e);. }. }.
1cf3e 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1cf3f 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
1cf40 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
1cf41 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
1cf42 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71 6c 69 ed prior to sqli
1cf43 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 20 77 te3PagerCommit w
1cf44 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f hen a transactio
1cf45 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 65 64 n.** is commited
1cf46 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 61 63 for an auto-vac
1cf47 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a uum database..**
1cf48 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b .** If SQLITE_OK
1cf49 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 is returned, th
1cf4a 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73 20 73 en *pnTrunc is s
1cf4b 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 et to the number
1cf4c 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74 68 65 of pages.** the
1cf4d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 database file s
1cf4e 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61 74 hould be truncat
1cf4f 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74 68 65 ed to during the
1cf50 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e commit process.
1cf51 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 64 61 .** i.e. the da
1cf52 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 tabase has been
1cf53 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f 20 74 reorganized so t
1cf54 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66 69 72 hat only the fir
1cf55 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a 20 70 st *pnTrunc.** p
1cf56 61 67 65 73 20 61 72 65 20 69 6e 20 75 73 65 2e ages are in use.
1cf57 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
1cf58 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 utoVacuumCommit(
1cf59 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
1cf5a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1cf5b 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70 E_OK;. Pager *p
1cf5c 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 Pager = pBt->pPa
1cf5d 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 ger;. VVA_ONLY(
1cf5e 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71 6c 69 int nRef = sqli
1cf5f 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 te3PagerRefcount
1cf60 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 61 (pPager) );.. a
1cf61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1cf62 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
1cf63 75 74 65 78 29 20 29 3b 0a 20 20 69 6e 76 61 6c utex) );. inval
1cf64 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 idateAllOverflow
1cf65 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 61 73 Cache(pBt);. as
1cf66 73 65 72 74 28 70 42 74 2d 3e 61 75 74 6f 56 61 sert(pBt->autoVa
1cf67 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 21 70 42 cuum);. if( !pB
1cf68 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 29 7b t->incrVacuum ){
1cf69 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e 3b 20 . Pgno nFin;
1cf6a 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1cf6b 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 64 61 r of pages in da
1cf6c 74 61 62 61 73 65 20 61 66 74 65 72 20 61 75 74 tabase after aut
1cf6d 6f 76 61 63 75 75 6d 69 6e 67 20 2a 2f 0a 20 20 ovacuuming */.
1cf6e 20 20 50 67 6e 6f 20 6e 46 72 65 65 3b 20 20 20 Pgno nFree;
1cf6f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1cf70 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 f pages on the f
1cf71 72 65 65 6c 69 73 74 20 69 6e 69 74 69 61 6c 6c reelist initiall
1cf72 79 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 50 y */. Pgno nP
1cf73 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 4e trmap; /* N
1cf74 75 6d 62 65 72 20 6f 66 20 50 74 72 4d 61 70 20 umber of PtrMap
1cf75 70 61 67 65 73 20 74 6f 20 62 65 20 66 72 65 65 pages to be free
1cf76 64 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 69 46 d */. Pgno iF
1cf77 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 ree; /* T
1cf78 68 65 20 6e 65 78 74 20 70 61 67 65 20 74 6f 20 he next page to
1cf79 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 be freed */.
1cf7a 69 6e 74 20 6e 45 6e 74 72 79 3b 20 20 20 20 20 int nEntry;
1cf7b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1cf7c 65 6e 74 72 69 65 73 20 6f 6e 20 6f 6e 65 20 70 entries on one p
1cf7d 74 72 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 trmap page */.
1cf7e 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 Pgno nOrig;
1cf7f 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
1cf80 20 73 69 7a 65 20 62 65 66 6f 72 65 20 66 72 65 size before fre
1cf81 65 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 6e 4f 72 eing */.. nOr
1cf82 69 67 20 3d 20 62 74 72 65 65 50 61 67 65 63 6f ig = btreePageco
1cf83 75 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 69 66 unt(pBt);. if
1cf84 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 ( PTRMAP_ISPAGE(
1cf85 70 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c 20 6e pBt, nOrig) || n
1cf86 4f 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 Orig==PENDING_BY
1cf87 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a TE_PAGE(pBt) ){.
1cf88 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e /* It is n
1cf89 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 ot possible to c
1cf8a 72 65 61 74 65 20 61 20 64 61 74 61 62 61 73 65 reate a database
1cf8b 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 66 for which the f
1cf8c 69 6e 61 6c 20 70 61 67 65 0a 20 20 20 20 20 20 inal page.
1cf8d 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 70 ** is either a p
1cf8e 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20 ointer-map page
1cf8f 6f 72 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 62 or the pending-b
1cf90 79 74 65 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 yte page. If one
1cf91 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 6e 63 . ** is enc
1cf92 6f 75 6e 74 65 72 65 64 2c 20 74 68 69 73 20 69 ountered, this i
1cf93 6e 64 69 63 61 74 65 73 20 63 6f 72 72 75 70 74 ndicates corrupt
1cf94 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ion.. */.
1cf95 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1cf96 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
1cf97 20 20 20 20 7d 0a 0a 20 20 20 20 6e 46 72 65 65 }.. nFree
1cf98 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74 = get4byte(&pBt
1cf99 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b ->pPage1->aData[
1cf9a 33 36 5d 29 3b 0a 20 20 20 20 6e 45 6e 74 72 79 36]);. nEntry
1cf9b 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = pBt->usableSi
1cf9c 7a 65 2f 35 3b 0a 20 20 20 20 6e 50 74 72 6d 61 ze/5;. nPtrma
1cf9d 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67 p = (nFree-nOrig
1cf9e 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 +PTRMAP_PAGENO(p
1cf9f 42 74 2c 20 6e 4f 72 69 67 29 2b 6e 45 6e 74 72 Bt, nOrig)+nEntr
1cfa0 79 29 2f 6e 45 6e 74 72 79 3b 0a 20 20 20 20 6e y)/nEntry;. n
1cfa1 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20 6e 46 Fin = nOrig - nF
1cfa2 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b 0a 20 ree - nPtrmap;.
1cfa3 20 20 20 69 66 28 20 6e 4f 72 69 67 3e 50 45 4e if( nOrig>PEN
1cfa4 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
1cfa5 42 74 29 20 26 26 20 6e 46 69 6e 3c 50 45 4e 44 Bt) && nFin<PEND
1cfa6 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
1cfa7 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 69 6e t) ){. nFin
1cfa8 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 --;. }. wh
1cfa9 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 ile( PTRMAP_ISPA
1cfaa 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 7c 7c GE(pBt, nFin) ||
1cfab 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42 nFin==PENDING_B
1cfac 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
1cfad 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 . nFin--;.
1cfae 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 46 69 }. if( nFi
1cfaf 6e 3e 6e 4f 72 69 67 20 29 20 72 65 74 75 72 6e n>nOrig ) return
1cfb0 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1cfb1 42 4b 50 54 3b 0a 0a 20 20 20 20 66 6f 72 28 69 BKPT;.. for(i
1cfb2 46 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46 72 65 Free=nOrig; iFre
1cfb3 65 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d 53 51 e>nFin && rc==SQ
1cfb4 4c 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65 2d 2d LITE_OK; iFree--
1cfb5 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 6e ){. rc = in
1cfb6 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 42 74 crVacuumStep(pBt
1cfb7 2c 20 6e 46 69 6e 2c 20 69 46 72 65 65 29 3b 0a , nFin, iFree);.
1cfb8 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 72 }. if( (r
1cfb9 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 7c c==SQLITE_DONE |
1cfba 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 | rc==SQLITE_OK)
1cfbb 20 26 26 20 6e 46 72 65 65 3e 30 20 29 7b 0a 20 && nFree>0 ){.
1cfbc 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1cfbd 33 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3PagerWrite(pBt-
1cfbe 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 >pPage1->pDbPage
1cfbf 29 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 );. put4byt
1cfc0 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e e(&pBt->pPage1->
1cfc1 61 44 61 74 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 aData[32], 0);.
1cfc2 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
1cfc3 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
1cfc4 61 5b 33 36 5d 2c 20 30 29 3b 0a 20 20 20 20 20 a[36], 0);.
1cfc5 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e put4byte(&pBt->
1cfc6 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 32 38 pPage1->aData[28
1cfc7 5d 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 20 20 ], nFin);.
1cfc8 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e sqlite3PagerTrun
1cfc9 63 61 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 cateImage(pBt->p
1cfca 50 61 67 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20 Pager, nFin);.
1cfcb 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d pBt->nPage =
1cfcc 20 6e 46 69 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 nFin;. }.
1cfcd 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1cfce 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
1cfcf 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
1cfd0 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a (pPager);. }.
1cfd1 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e }.. assert( n
1cfd2 52 65 66 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 Ref==sqlite3Page
1cfd3 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 rRefcount(pPager
1cfd4 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ) );. return rc
1cfd5 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 ;.}..#else /* if
1cfd6 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1cfd7 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 23 _AUTOVACUUM */.#
1cfd8 20 64 65 66 69 6e 65 20 73 65 74 43 68 69 6c 64 define setChild
1cfd9 50 74 72 6d 61 70 73 28 78 29 20 53 51 4c 49 54 Ptrmaps(x) SQLIT
1cfda 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a E_OK.#endif../*.
1cfdb 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1cfdc 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70 does the first p
1cfdd 68 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68 hase of a two-ph
1cfde 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69 ase commit. Thi
1cfdf 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75 s routine.** cau
1cfe0 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a ses a rollback j
1cfe1 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65 ournal to be cre
1cfe2 61 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 73 ated (if it does
1cfe3 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 not already exi
1cfe4 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c st).** and popul
1cfe5 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68 ated with enough
1cfe6 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20 information so
1cfe7 74 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 20 that if a power
1cfe8 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74 loss occurs.** t
1cfe9 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 he database can
1cfea 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 be restored to i
1cfeb 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 ts original stat
1cfec 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61 63 e by playing bac
1cfed 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c k.** the journal
1cfee 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e 74 . Then the cont
1cfef 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 ents of the jour
1cff0 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64 20 nal are flushed
1cff1 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69 out to.** the di
1cff2 73 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20 6a sk. After the j
1cff3 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c 79 ournal is safely
1cff4 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 63 on oxide, the c
1cff5 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a hanges to the.**
1cff6 20 64 61 74 61 62 61 73 65 20 61 72 65 20 77 72 database are wr
1cff7 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64 itten into the d
1cff8 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1cff9 20 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 64 flushed to oxid
1cffa 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 e..** At the end
1cffb 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 74 of this call, t
1cffc 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 he rollback jour
1cffd 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 73 nal still exists
1cffe 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 on the.** disk
1cfff 61 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c 6c and we are still
1d000 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63 holding all loc
1d001 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e 73 ks, so the trans
1d002 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a action has not.*
1d003 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65 * committed. Se
1d004 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f e sqlite3BtreeCo
1d005 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 66 mmitPhaseTwo() f
1d006 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 or the second ph
1d007 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f ase of the.** co
1d008 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a mmit process..**
1d009 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 .** This call is
1d00a 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77 a no-op if no w
1d00b 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
1d00c 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 63 is currently ac
1d00d 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a tive on pBt..**.
1d00e 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 79 ** Otherwise, sy
1d00f 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 nc the database
1d010 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74 72 file for the btr
1d011 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 20 ee pBt. zMaster
1d012 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 points to.** the
1d013 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 name of a maste
1d014 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 r journal file t
1d015 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 hat should be wr
1d016 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a itten into the.*
1d017 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 * individual jou
1d018 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73 rnal file, or is
1d019 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e NULL, indicatin
1d01a 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 g no master jour
1d01b 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69 nal file .** (si
1d01c 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 ngle database tr
1d01d 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a ansaction)..**.*
1d01e 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 * When this is c
1d01f 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 65 alled, the maste
1d020 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64 r journal should
1d021 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62 65 already have be
1d022 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 70 en.** created, p
1d023 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 opulated with th
1d024 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 is journal point
1d025 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f er and synced to
1d026 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 disk..**.** Onc
1d027 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69 6e e this is routin
1d028 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c 20 e has returned,
1d029 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72 the only thing r
1d02a 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69 equired to commi
1d02b 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d 74 t.** the write-t
1d02c 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 74 ransaction for t
1d02d 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c his database fil
1d02e 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74 e is to delete t
1d02f 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 53 he journal..*/.S
1d030 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1d031 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
1d032 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72 mmitPhaseOne(Btr
1d033 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 ee *p, const cha
1d034 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 r *zMaster){. i
1d035 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1d036 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 K;. if( p->inTr
1d037 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
1d038 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 ){. BtShared
1d039 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
1d03a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1d03b 45 6e 74 65 72 28 70 29 3b 0a 23 69 66 6e 64 65 Enter(p);.#ifnde
1d03c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1d03d 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
1d03e 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
1d03f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 ){. rc = a
1d040 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 utoVacuumCommit(
1d041 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 pBt);. if(
1d042 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1d043 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1d044 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
1d045 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1d046 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1d047 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 #endif. rc =
1d048 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
1d049 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e itPhaseOne(pBt->
1d04a 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c pPager, zMaster,
1d04b 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0);. sqlite3
1d04c 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
1d04d 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1d04e 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
1d04f 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
1d050 20 66 72 6f 6d 20 62 6f 74 68 20 42 74 72 65 65 from both Btree
1d051 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 CommitPhaseTwo()
1d052 20 61 6e 64 20 42 74 72 65 65 52 6f 6c 6c 62 61 and BtreeRollba
1d053 63 6b 28 29 0a 2a 2a 20 61 74 20 74 68 65 20 63 ck().** at the c
1d054 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 61 20 74 onclusion of a t
1d055 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 ransaction..*/.s
1d056 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 tatic void btree
1d057 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 42 EndTransaction(B
1d058 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 tree *p){. BtSh
1d059 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
1d05a 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 Bt;. assert( sq
1d05b 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
1d05c 75 74 65 78 28 70 29 20 29 3b 0a 0a 20 20 62 74 utex(p) );.. bt
1d05d 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 reeClearHasConte
1d05e 6e 74 28 70 42 74 29 3b 0a 20 20 69 66 28 20 70 nt(pBt);. if( p
1d05f 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f ->inTrans>TRANS_
1d060 4e 4f 4e 45 20 26 26 20 70 2d 3e 64 62 2d 3e 61 NONE && p->db->a
1d061 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 ctiveVdbeCnt>1 )
1d062 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 {. /* If ther
1d063 65 20 61 72 65 20 6f 74 68 65 72 20 61 63 74 69 e are other acti
1d064 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 ve statements th
1d065 61 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 69 at belong to thi
1d066 73 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a s database. *
1d067 2a 20 68 61 6e 64 6c 65 2c 20 64 6f 77 6e 67 72 * handle, downgr
1d068 61 64 65 20 74 6f 20 61 20 72 65 61 64 2d 6f 6e ade to a read-on
1d069 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ly transaction.
1d06a 54 68 65 20 6f 74 68 65 72 20 73 74 61 74 65 6d The other statem
1d06b 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 ents. ** may
1d06c 73 74 69 6c 6c 20 62 65 20 72 65 61 64 69 6e 67 still be reading
1d06d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
1d06e 73 65 2e 20 20 2a 2f 0a 20 20 20 20 64 6f 77 6e se. */. down
1d06f 67 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 gradeAllSharedCa
1d070 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 cheTableLocks(p)
1d071 3b 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 ;. p->inTrans
1d072 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 = TRANS_READ;.
1d073 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 }else{. /* I
1d074 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68 61 64 f the handle had
1d075 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 74 72 61 any kind of tra
1d076 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 64 nsaction open, d
1d077 65 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 ecrement the .
1d078 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
1d079 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 68 count of the sh
1d07a 61 72 65 64 20 62 74 72 65 65 2e 20 49 66 20 74 ared btree. If t
1d07b 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 he transaction c
1d07c 6f 75 6e 74 20 0a 20 20 20 20 2a 2a 20 72 65 61 ount . ** rea
1d07d 63 68 65 73 20 30 2c 20 73 65 74 20 74 68 65 20 ches 0, set the
1d07e 73 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 shared state to
1d07f 54 52 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 TRANS_NONE. The
1d080 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 unlockBtreeIfUnu
1d081 73 65 64 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c sed(). ** cal
1d082 6c 20 62 65 6c 6f 77 20 77 69 6c 6c 20 75 6e 6c l below will unl
1d083 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2e 20 20 ock the pager.
1d084 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e */. if( p->in
1d085 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e Trans!=TRANS_NON
1d086 45 20 29 7b 0a 20 20 20 20 20 20 63 6c 65 61 72 E ){. clear
1d087 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 AllSharedCacheTa
1d088 62 6c 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20 20 bleLocks(p);.
1d089 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 pBt->nTransac
1d08a 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 20 20 69 66 tion--;. if
1d08b 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 ( 0==pBt->nTrans
1d08c 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 action ){.
1d08d 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 pBt->inTransac
1d08e 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e tion = TRANS_NON
1d08f 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d E;. }. }
1d090 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 .. /* Set the
1d091 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 current transac
1d092 74 69 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52 tion state to TR
1d093 41 4e 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c ANS_NONE and unl
1d094 6f 63 6b 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 ock the . **
1d095 70 61 67 65 72 20 69 66 20 74 68 69 73 20 63 61 pager if this ca
1d096 6c 6c 20 63 6c 6f 73 65 64 20 74 68 65 20 6f 6e ll closed the on
1d097 6c 79 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 ly read or write
1d098 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 2a transaction. *
1d099 2f 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 /. p->inTrans
1d09a 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 = TRANS_NONE;.
1d09b 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 unlockBtreeIf
1d09c 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 7d Unused(pBt);. }
1d09d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 .. btreeIntegri
1d09e 74 79 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ty(p);.}../*.**
1d09f 43 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 Commit the trans
1d0a0 61 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 action currently
1d0a1 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a in progress..**
1d0a2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1d0a3 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
1d0a4 73 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 second phase of
1d0a5 61 20 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 a 2-phase commit
1d0a6 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 . The.** sqlite
1d0a7 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
1d0a8 65 4f 6e 65 28 29 20 72 6f 75 74 69 6e 65 20 64 eOne() routine d
1d0a9 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70 68 oes the first ph
1d0aa 61 73 65 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a ase and should.*
1d0ab 2a 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 69 * be invoked pri
1d0ac 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 or to calling th
1d0ad 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 is routine. The
1d0ae 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
1d0af 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 0a 2a 2a mitPhaseOne().**
1d0b0 20 72 6f 75 74 69 6e 65 20 64 69 64 20 61 6c 6c routine did all
1d0b1 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 77 72 69 the work of wri
1d0b2 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ting information
1d0b3 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 6e 64 out to disk and
1d0b4 20 66 6c 75 73 68 69 6e 67 20 74 68 65 0a 2a 2a flushing the.**
1d0b5 20 63 6f 6e 74 65 6e 74 73 20 73 6f 20 74 68 61 contents so tha
1d0b6 74 20 74 68 65 79 20 61 72 65 20 77 72 69 74 74 t they are writt
1d0b7 65 6e 20 6f 6e 74 6f 20 74 68 65 20 64 69 73 6b en onto the disk
1d0b8 20 70 6c 61 74 74 65 72 2e 20 20 41 6c 6c 20 74 platter. All t
1d0b9 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 68 his.** routine h
1d0ba 61 73 20 74 6f 20 64 6f 20 69 73 20 64 65 6c 65 as to do is dele
1d0bb 74 65 20 6f 72 20 74 72 75 6e 63 61 74 65 20 6f te or truncate o
1d0bc 72 20 7a 65 72 6f 20 74 68 65 20 68 65 61 64 65 r zero the heade
1d0bd 72 20 69 6e 20 74 68 65 0a 2a 2a 20 74 68 65 20 r in the.** the
1d0be 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
1d0bf 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20 74 (which causes t
1d0c0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 he transaction t
1d0c1 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 0a 2a 2a o commit) and.**
1d0c2 20 64 72 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a drop locks..**.
1d0c3 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 20 ** Normally, if
1d0c4 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
1d0c5 77 68 69 6c 65 20 74 68 65 20 70 61 67 65 72 20 while the pager
1d0c6 6c 61 79 65 72 20 69 73 20 61 74 74 65 6d 70 74 layer is attempt
1d0c7 69 6e 67 20 74 6f 20 0a 2a 2a 20 66 69 6e 61 6c ing to .** final
1d0c8 69 7a 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 ize the underlyi
1d0c9 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c ng journal file,
1d0ca 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
1d0cb 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 eturns an error
1d0cc 61 6e 64 0a 2a 2a 20 74 68 65 20 75 70 70 65 72 and.** the upper
1d0cd 20 6c 61 79 65 72 20 77 69 6c 6c 20 61 74 74 65 layer will atte
1d0ce 6d 70 74 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 20 mpt a rollback.
1d0cf 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 However, if the
1d0d0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a second argument.
1d0d1 2a 2a 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 ** is non-zero t
1d0d2 68 65 6e 20 74 68 69 73 20 62 2d 74 72 65 65 20 hen this b-tree
1d0d3 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 70 transaction is p
1d0d4 61 72 74 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 art of a multi-f
1d0d5 69 6c 65 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 ile .** transact
1d0d6 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ion. In this cas
1d0d7 65 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 e, the transacti
1d0d8 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 on has already b
1d0d9 65 65 6e 20 63 6f 6d 6d 69 74 74 65 64 20 0a 2a een committed .*
1d0da 2a 20 28 62 79 20 64 65 6c 65 74 69 6e 67 20 61 * (by deleting a
1d0db 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1d0dc 66 69 6c 65 29 20 61 6e 64 20 74 68 65 20 63 61 file) and the ca
1d0dd 6c 6c 65 72 20 77 69 6c 6c 20 69 67 6e 6f 72 65 ller will ignore
1d0de 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 this .** functi
1d0df 6f 6e 73 20 72 65 74 75 72 6e 20 63 6f 64 65 2e ons return code.
1d0e0 20 53 6f 2c 20 65 76 65 6e 20 69 66 20 61 6e 20 So, even if an
1d0e1 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 error occurs in
1d0e2 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 2c the pager layer,
1d0e3 0a 2a 2a 20 72 65 73 65 74 20 74 68 65 20 62 2d .** reset the b-
1d0e4 74 72 65 65 20 6f 62 6a 65 63 74 73 20 69 6e 74 tree objects int
1d0e5 65 72 6e 61 6c 20 73 74 61 74 65 20 74 6f 20 69 ernal state to i
1d0e6 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 ndicate that the
1d0e7 20 77 72 69 74 65 0a 2a 2a 20 74 72 61 6e 73 61 write.** transa
1d0e8 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 63 ction has been c
1d0e9 6c 6f 73 65 64 2e 20 54 68 69 73 20 69 73 20 71 losed. This is q
1d0ea 75 69 74 65 20 73 61 66 65 2c 20 61 73 20 74 68 uite safe, as th
1d0eb 65 20 70 61 67 65 72 20 77 69 6c 6c 20 68 61 76 e pager will hav
1d0ec 65 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 65 e.** transitione
1d0ed 64 20 74 6f 20 74 68 65 20 65 72 72 6f 72 20 73 d to the error s
1d0ee 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tate..**.** This
1d0ef 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 will release th
1d0f0 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 e write lock on
1d0f1 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1d0f2 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 e. If there.**
1d0f3 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75 are no active cu
1d0f4 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 rsors, it also r
1d0f5 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61 64 eleases the read
1d0f6 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 lock..*/.SQLITE
1d0f7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1d0f8 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
1d0f9 68 61 73 65 54 77 6f 28 42 74 72 65 65 20 2a 70 haseTwo(Btree *p
1d0fa 2c 20 69 6e 74 20 62 43 6c 65 61 6e 75 70 29 7b , int bCleanup){
1d0fb 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 .. if( p->inTra
1d0fc 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 ns==TRANS_NONE )
1d0fd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1d0fe 4b 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 K;. sqlite3Btre
1d0ff 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 62 74 72 eEnter(p);. btr
1d100 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a eeIntegrity(p);.
1d101 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e . /* If the han
1d102 64 6c 65 20 68 61 73 20 61 20 77 72 69 74 65 2d dle has a write-
1d103 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e transaction open
1d104 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 73 68 61 , commit the sha
1d105 72 65 64 2d 62 74 72 65 65 73 20 0a 20 20 2a 2a red-btrees . **
1d106 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 transaction and
1d107 20 73 65 74 20 74 68 65 20 73 68 61 72 65 64 20 set the shared
1d108 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 52 state to TRANS_R
1d109 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 EAD.. */. if(
1d10a 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
1d10b 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69 S_WRITE ){. i
1d10c 6e 74 20 72 63 3b 0a 20 20 20 20 42 74 53 68 61 nt rc;. BtSha
1d10d 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
1d10e 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 t;. assert( p
1d10f 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
1d110 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 n==TRANS_WRITE )
1d111 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 ;. assert( pB
1d112 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e t->nTransaction>
1d113 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 0 );. rc = sq
1d114 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
1d115 50 68 61 73 65 54 77 6f 28 70 42 74 2d 3e 70 50 PhaseTwo(pBt->pP
1d116 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 ager);. if( r
1d117 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c!=SQLITE_OK &&
1d118 62 43 6c 65 61 6e 75 70 3d 3d 30 20 29 7b 0a 20 bCleanup==0 ){.
1d119 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1d11a 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 20 eLeave(p);.
1d11b 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1d11c 7d 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 }. pBt->inTra
1d11d 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 nsaction = TRANS
1d11e 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 _READ;. }.. bt
1d11f 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 6f reeEndTransactio
1d120 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 n(p);. sqlite3B
1d121 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
1d122 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1d123 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f ;.}../*.** Do bo
1d124 74 68 20 70 68 61 73 65 73 20 6f 66 20 61 20 63 th phases of a c
1d125 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ommit..*/.SQLITE
1d126 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1d127 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 ite3BtreeCommit(
1d128 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 Btree *p){. int
1d129 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 rc;. sqlite3Bt
1d12a 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 reeEnter(p);. r
1d12b 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1d12c 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 CommitPhaseOne(p
1d12d 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d , 0);. if( rc==
1d12e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1d12f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1d130 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f eeCommitPhaseTwo
1d131 28 70 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 73 71 (p, 0);. }. sq
1d132 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
1d133 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
1d134 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 .}..#ifndef NDEB
1d135 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 UG./*.** Return
1d136 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 72 the number of wr
1d137 69 74 65 2d 63 75 72 73 6f 72 73 20 6f 70 65 6e ite-cursors open
1d138 20 6f 6e 20 74 68 69 73 20 68 61 6e 64 6c 65 2e on this handle.
1d139 20 54 68 69 73 20 69 73 20 66 6f 72 20 75 73 65 This is for use
1d13a 0a 2a 2a 20 69 6e 20 61 73 73 65 72 74 28 29 20 .** in assert()
1d13b 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 73 6f 20 expressions, so
1d13c 69 74 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 it is only compi
1d13d 6c 65 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 led if NDEBUG is
1d13e 20 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e 65 64 2e not.** defined.
1d13f 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 .**.** For the p
1d140 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 urposes of this
1d141 72 6f 75 74 69 6e 65 2c 20 61 20 77 72 69 74 65 routine, a write
1d142 2d 63 75 72 73 6f 72 20 69 73 20 61 6e 79 20 63 -cursor is any c
1d143 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 69 73 ursor that.** is
1d144 20 63 61 70 61 62 6c 65 20 6f 66 20 77 72 69 74 capable of writ
1d145 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 ing to the datab
1d146 73 65 2e 20 20 54 68 61 74 20 6d 65 61 6e 73 20 se. That means
1d147 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 0a 2a the cursor was.*
1d148 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 6f 70 65 * originally ope
1d149 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 20 ned for writing
1d14a 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 68 and the cursor h
1d14b 61 73 20 6e 6f 74 20 62 65 20 64 69 73 61 62 6c as not be disabl
1d14c 65 64 0a 2a 2a 20 62 79 20 68 61 76 69 6e 67 20 ed.** by having
1d14d 69 74 73 20 73 74 61 74 65 20 63 68 61 6e 67 65 its state change
1d14e 64 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c d to CURSOR_FAUL
1d14f 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 T..*/.static int
1d150 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73 6f countWriteCurso
1d151 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 rs(BtShared *pBt
1d152 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 ){. BtCursor *p
1d153 43 75 72 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30 Cur;. int r = 0
1d154 3b 0a 20 20 66 6f 72 28 70 43 75 72 3d 70 42 74 ;. for(pCur=pBt
1d155 2d 3e 70 43 75 72 73 6f 72 3b 20 70 43 75 72 3b ->pCursor; pCur;
1d156 20 70 43 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78 pCur=pCur->pNex
1d157 74 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 t){. if( pCur
1d158 2d 3e 77 72 46 6c 61 67 20 26 26 20 70 43 75 72 ->wrFlag && pCur
1d159 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 ->eState!=CURSOR
1d15a 5f 46 41 55 4c 54 20 29 20 72 2b 2b 3b 20 0a 20 _FAULT ) r++; .
1d15b 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d }. return r;.}
1d15c 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
1d15d 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 his routine sets
1d15e 20 74 68 65 20 73 74 61 74 65 20 74 6f 20 43 55 the state to CU
1d15f 52 53 4f 52 5f 46 41 55 4c 54 20 61 6e 64 20 74 RSOR_FAULT and t
1d160 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 he error.** code
1d161 20 74 6f 20 65 72 72 43 6f 64 65 20 66 6f 72 20 to errCode for
1d162 65 76 65 72 79 20 63 75 72 73 6f 72 20 6f 6e 20 every cursor on
1d163 42 74 53 68 61 72 65 64 20 74 68 61 74 20 70 42 BtShared that pB
1d164 74 72 65 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 tree.** referenc
1d165 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 es..**.** Every
1d166 63 75 72 73 6f 72 20 69 73 20 74 72 69 70 70 65 cursor is trippe
1d167 64 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 75 72 d, including cur
1d168 73 6f 72 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 sors that belong
1d169 0a 2a 2a 20 74 6f 20 6f 74 68 65 72 20 64 61 74 .** to other dat
1d16a 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1d16b 73 20 74 68 61 74 20 68 61 70 70 65 6e 20 74 6f s that happen to
1d16c 20 62 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20 74 be sharing.** t
1d16d 68 65 20 63 61 63 68 65 20 77 69 74 68 20 70 42 he cache with pB
1d16e 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tree..**.** This
1d16f 20 72 6f 75 74 69 6e 65 20 67 65 74 73 20 63 61 routine gets ca
1d170 6c 6c 65 64 20 77 68 65 6e 20 61 20 72 6f 6c 6c lled when a roll
1d171 62 61 63 6b 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 back occurs..**
1d172 41 6c 6c 20 63 75 72 73 6f 72 73 20 75 73 69 6e All cursors usin
1d173 67 20 74 68 65 20 73 61 6d 65 20 63 61 63 68 65 g the same cache
1d174 20 6d 75 73 74 20 62 65 20 74 72 69 70 70 65 64 must be tripped
1d175 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74 20 74 .** to prevent t
1d176 68 65 6d 20 66 72 6f 6d 20 74 72 79 69 6e 67 20 hem from trying
1d177 74 6f 20 75 73 65 20 74 68 65 20 62 74 72 65 65 to use the btree
1d178 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 72 6f after.** the ro
1d179 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20 72 6f 6c llback. The rol
1d17a 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 65 20 64 lback may have d
1d17b 65 6c 65 74 65 64 20 74 61 62 6c 65 73 0a 2a 2a eleted tables.**
1d17c 20 6f 72 20 6d 6f 76 65 64 20 72 6f 6f 74 20 70 or moved root p
1d17d 61 67 65 73 2c 20 73 6f 20 69 74 20 69 73 20 6e ages, so it is n
1d17e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f ot sufficient to
1d17f 0a 2a 2a 20 73 61 76 65 20 74 68 65 20 73 74 61 .** save the sta
1d180 74 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 te of the cursor
1d181 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 . The cursor mu
1d182 73 74 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 st be.** invalid
1d183 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ated..*/.SQLITE_
1d184 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1d185 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c ite3BtreeTripAll
1d186 43 75 72 73 6f 72 73 28 42 74 72 65 65 20 2a 70 Cursors(Btree *p
1d187 42 74 72 65 65 2c 20 69 6e 74 20 65 72 72 43 6f Btree, int errCo
1d188 64 65 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 de){. BtCursor
1d189 2a 70 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 *p;. if( pBtree
1d18a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
1d18b 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1d18c 72 28 70 42 74 72 65 65 29 3b 0a 20 20 66 6f 72 r(pBtree);. for
1d18d 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e (p=pBtree->pBt->
1d18e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d pCursor; p; p=p-
1d18f 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 >pNext){. int
1d190 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 i;. sqlite3B
1d191 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 treeClearCursor(
1d192 70 29 3b 0a 20 20 20 20 70 2d 3e 65 53 74 61 74 p);. p->eStat
1d193 65 20 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 e = CURSOR_FAULT
1d194 3b 0a 20 20 20 20 70 2d 3e 73 6b 69 70 4e 65 78 ;. p->skipNex
1d195 74 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 20 t = errCode;.
1d196 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d 3e for(i=0; i<=p->
1d197 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 iPage; i++){.
1d198 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1d199 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 ->apPage[i]);.
1d19a 20 20 20 20 70 2d 3e 61 70 50 61 67 65 5b 69 5d p->apPage[i]
1d19b 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
1d19c 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1d19d 61 76 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a 0a ave(pBtree);.}..
1d19e 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 74 /*.** Rollback t
1d19f 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
1d1a0 6e 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c 6c n progress. All
1d1a1 20 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65 cursors will be
1d1a2 0a 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62 79 .** invalided by
1d1a3 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e this operation.
1d1a4 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f Any attempt to
1d1a5 20 75 73 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a use a cursor.**
1d1a6 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61 that was open a
1d1a7 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
1d1a8 6f 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f of this operatio
1d1a9 6e 20 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a 2a n will result.**
1d1aa 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a in an error..**
1d1ab 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 .** This will re
1d1ac 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20 lease the write
1d1ad 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
1d1ae 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 base file. If t
1d1af 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 here.** are no a
1d1b0 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 ctive cursors, i
1d1b1 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 t also releases
1d1b2 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a the read lock..*
1d1b3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1d1b4 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
1d1b5 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 20 eRollback(Btree
1d1b6 2a 70 2c 20 69 6e 74 20 74 72 69 70 43 6f 64 65 *p, int tripCode
1d1b7 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 ){. int rc;. B
1d1b8 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
1d1b9 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 ->pBt;. MemPage
1d1ba 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 73 71 6c *pPage1;.. sql
1d1bb 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
1d1bc 29 3b 0a 20 20 69 66 28 20 74 72 69 70 43 6f 64 );. if( tripCod
1d1bd 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a e==SQLITE_OK ){.
1d1be 20 20 20 20 72 63 20 3d 20 74 72 69 70 43 6f 64 rc = tripCod
1d1bf 65 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f e = saveAllCurso
1d1c0 72 73 28 70 42 74 2c 20 30 2c 20 30 29 3b 0a 20 rs(pBt, 0, 0);.
1d1c1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
1d1c2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
1d1c3 20 20 69 66 28 20 74 72 69 70 43 6f 64 65 20 29 if( tripCode )
1d1c4 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 {. sqlite3Btr
1d1c5 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 eeTripAllCursors
1d1c6 28 70 2c 20 74 72 69 70 43 6f 64 65 29 3b 0a 20 (p, tripCode);.
1d1c7 20 7d 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 }. btreeIntegr
1d1c8 69 74 79 28 70 29 3b 0a 0a 20 20 69 66 28 20 70 ity(p);.. if( p
1d1c9 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
1d1ca 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69 6e _WRITE ){. in
1d1cb 74 20 72 63 32 3b 0a 0a 20 20 20 20 61 73 73 65 t rc2;.. asse
1d1cc 72 74 28 20 54 52 41 4e 53 5f 57 52 49 54 45 3d rt( TRANS_WRITE=
1d1cd 3d 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 =pBt->inTransact
1d1ce 69 6f 6e 20 29 3b 0a 20 20 20 20 72 63 32 20 3d ion );. rc2 =
1d1cf 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c sqlite3PagerRol
1d1d0 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50 61 67 65 lback(pBt->pPage
1d1d1 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 32 21 r);. if( rc2!
1d1d2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1d1d3 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 rc = rc2;.
1d1d4 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 }.. /* The
1d1d5 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 rollback may hav
1d1d6 65 20 64 65 73 74 72 6f 79 65 64 20 74 68 65 20 e destroyed the
1d1d7 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 76 61 pPage1->aData va
1d1d8 6c 75 65 2e 20 20 53 6f 0a 20 20 20 20 2a 2a 20 lue. So. **
1d1d9 63 61 6c 6c 20 62 74 72 65 65 47 65 74 50 61 67 call btreeGetPag
1d1da 65 28 29 20 6f 6e 20 70 61 67 65 20 31 20 61 67 e() on page 1 ag
1d1db 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20 20 20 20 ain to make.
1d1dc 2a 2a 20 73 75 72 65 20 70 50 61 67 65 31 2d 3e ** sure pPage1->
1d1dd 61 44 61 74 61 20 69 73 20 73 65 74 20 63 6f 72 aData is set cor
1d1de 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 69 rectly. */. i
1d1df 66 28 20 62 74 72 65 65 47 65 74 50 61 67 65 28 f( btreeGetPage(
1d1e0 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c pBt, 1, &pPage1,
1d1e1 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 0)==SQLITE_OK )
1d1e2 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 {. int nPag
1d1e3 65 20 3d 20 67 65 74 34 62 79 74 65 28 32 38 2b e = get4byte(28+
1d1e4 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 61 44 61 (u8*)pPage1->aDa
1d1e5 74 61 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 ta);. testc
1d1e6 61 73 65 28 20 6e 50 61 67 65 3d 3d 30 20 29 3b ase( nPage==0 );
1d1e7 0a 20 20 20 20 20 20 69 66 28 20 6e 50 61 67 65 . if( nPage
1d1e8 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 50 61 67 ==0 ) sqlite3Pag
1d1e9 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d erPagecount(pBt-
1d1ea 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 >pPager, &nPage)
1d1eb 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1d1ec 28 20 70 42 74 2d 3e 6e 50 61 67 65 21 3d 6e 50 ( pBt->nPage!=nP
1d1ed 61 67 65 20 29 3b 0a 20 20 20 20 20 20 70 42 74 age );. pBt
1d1ee 2d 3e 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 3b ->nPage = nPage;
1d1ef 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
1d1f0 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 20 ge(pPage1);.
1d1f1 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 6f }. assert( co
1d1f2 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72 73 28 untWriteCursors(
1d1f3 70 42 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 pBt)==0 );. p
1d1f4 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
1d1f5 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a n = TRANS_READ;.
1d1f6 20 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e 64 54 }.. btreeEndT
1d1f7 72 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b 0a 20 ransaction(p);.
1d1f8 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1d1f9 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
1d1fa 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 rc;.}../*.** Sta
1d1fb 72 74 20 61 20 73 74 61 74 65 6d 65 6e 74 20 73 rt a statement s
1d1fc 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 ubtransaction. T
1d1fd 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f he subtransactio
1d1fe 6e 20 63 61 6e 20 63 61 6e 20 62 65 20 72 6f 6c n can can be rol
1d1ff 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 69 6e 64 65 led.** back inde
1d200 70 65 6e 64 65 6e 74 6c 79 20 6f 66 20 74 68 65 pendently of the
1d201 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69 6f main transactio
1d202 6e 2e 20 59 6f 75 20 6d 75 73 74 20 73 74 61 72 n. You must star
1d203 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 t a transaction
1d204 0a 2a 2a 20 62 65 66 6f 72 65 20 73 74 61 72 74 .** before start
1d205 69 6e 67 20 61 20 73 75 62 74 72 61 6e 73 61 63 ing a subtransac
1d206 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72 61 tion. The subtra
1d207 6e 73 61 63 74 69 6f 6e 20 69 73 20 65 6e 64 65 nsaction is ende
1d208 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 d automatically
1d209 0a 2a 2a 20 69 66 20 74 68 65 20 6d 61 69 6e 20 .** if the main
1d20a 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d transaction comm
1d20b 69 74 73 20 6f 72 20 72 6f 6c 6c 73 20 62 61 63 its or rolls bac
1d20c 6b 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d 65 k..**.** Stateme
1d20d 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f nt subtransactio
1d20e 6e 73 20 61 72 65 20 75 73 65 64 20 61 72 6f 75 ns are used arou
1d20f 6e 64 20 69 6e 64 69 76 69 64 75 61 6c 20 53 51 nd individual SQ
1d210 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 L statements.**
1d211 74 68 61 74 20 61 72 65 20 63 6f 6e 74 61 69 6e that are contain
1d212 65 64 20 77 69 74 68 69 6e 20 61 20 42 45 47 49 ed within a BEGI
1d213 4e 2e 2e 2e 43 4f 4d 4d 49 54 20 62 6c 6f 63 6b N...COMMIT block
1d214 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72 61 69 . If a constrai
1d215 6e 74 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63 75 nt.** error occu
1d216 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 74 rs within the st
1d217 61 74 65 6d 65 6e 74 2c 20 74 68 65 20 65 66 66 atement, the eff
1d218 65 63 74 20 6f 66 20 74 68 61 74 20 6f 6e 65 20 ect of that one
1d219 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e statement.** can
1d21a 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 be rolled back
1d21b 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 without having t
1d21c 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 65 o rollback the e
1d21d 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f ntire transactio
1d21e 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 74 61 74 65 n..**.** A state
1d21f 6d 65 6e 74 20 73 75 62 2d 74 72 61 6e 73 61 63 ment sub-transac
1d220 74 69 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e tion is implemen
1d221 74 65 64 20 61 73 20 61 6e 20 61 6e 6f 6e 79 6d ted as an anonym
1d222 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2e 20 54 ous savepoint. T
1d223 68 65 0a 2a 2a 20 76 61 6c 75 65 20 70 61 73 73 he.** value pass
1d224 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
1d225 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 parameter is th
1d226 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
1d227 66 20 73 61 76 65 70 6f 69 6e 74 73 2c 0a 2a 2a f savepoints,.**
1d228 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 6e including the n
1d229 65 77 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 ew anonymous sav
1d22a 65 70 6f 69 6e 74 2c 20 6f 70 65 6e 20 6f 6e 20 epoint, open on
1d22b 74 68 65 20 42 2d 54 72 65 65 2e 20 69 2e 65 2e the B-Tree. i.e.
1d22c 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 if there.** are
1d22d 20 6e 6f 20 61 63 74 69 76 65 20 73 61 76 65 70 no active savep
1d22e 6f 69 6e 74 73 20 61 6e 64 20 6e 6f 20 6f 74 68 oints and no oth
1d22f 65 72 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 er statement-tra
1d230 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e 2c 0a nsactions open,.
1d231 2a 2a 20 69 53 74 61 74 65 6d 65 6e 74 20 69 73 ** iStatement is
1d232 20 31 2e 20 54 68 69 73 20 61 6e 6f 6e 79 6d 6f 1. This anonymo
1d233 75 73 20 73 61 76 65 70 6f 69 6e 74 20 63 61 6e us savepoint can
1d234 20 62 65 20 72 65 6c 65 61 73 65 64 20 6f 72 20 be released or
1d235 72 6f 6c 6c 65 64 20 62 61 63 6b 0a 2a 2a 20 75 rolled back.** u
1d236 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 sing the sqlite3
1d237 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 29 BtreeSavepoint()
1d238 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 function..*/.SQ
1d239 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1d23a 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 sqlite3BtreeBeg
1d23b 69 6e 53 74 6d 74 28 42 74 72 65 65 20 2a 70 2c inStmt(Btree *p,
1d23c 20 69 6e 74 20 69 53 74 61 74 65 6d 65 6e 74 29 int iStatement)
1d23d 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 {. int rc;. Bt
1d23e 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
1d23f 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 >pBt;. sqlite3B
1d240 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
1d241 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 assert( p->inTra
1d242 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns==TRANS_WRITE
1d243 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 );. assert( (pB
1d244 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 t->btsFlags & BT
1d245 53 5f 52 45 41 44 5f 4f 4e 4c 59 29 3d 3d 30 20 S_READ_ONLY)==0
1d246 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 );. assert( iSt
1d247 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20 20 61 atement>0 );. a
1d248 73 73 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e ssert( iStatemen
1d249 74 3e 70 2d 3e 64 62 2d 3e 6e 53 61 76 65 70 6f t>p->db->nSavepo
1d24a 69 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 int );. assert(
1d24b 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
1d24c 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
1d24d 20 29 3b 0a 20 20 2f 2a 20 41 74 20 74 68 65 20 );. /* At the
1d24e 70 61 67 65 72 20 6c 65 76 65 6c 2c 20 61 20 73 pager level, a s
1d24f 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
1d250 74 69 6f 6e 20 69 73 20 61 20 73 61 76 65 70 6f tion is a savepo
1d251 69 6e 74 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e int with. ** an
1d252 20 69 6e 64 65 78 20 67 72 65 61 74 65 72 20 74 index greater t
1d253 68 61 6e 20 61 6c 6c 20 73 61 76 65 70 6f 69 6e han all savepoin
1d254 74 73 20 63 72 65 61 74 65 64 20 65 78 70 6c 69 ts created expli
1d255 63 69 74 6c 79 20 75 73 69 6e 67 0a 20 20 2a 2a citly using. **
1d256 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e SQL statements.
1d257 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 It is illegal t
1d258 6f 20 6f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 o open, release
1d259 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a or rollback any.
1d25a 20 20 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f ** such savepo
1d25b 69 6e 74 73 20 77 68 69 6c 65 20 74 68 65 20 73 ints while the s
1d25c 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
1d25d 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 tion savepoint i
1d25e 73 20 61 63 74 69 76 65 2e 0a 20 20 2a 2f 0a 20 s active.. */.
1d25f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1d260 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 erOpenSavepoint(
1d261 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 53 74 pBt->pPager, iSt
1d262 61 74 65 6d 65 6e 74 29 3b 0a 20 20 73 71 6c 69 atement);. sqli
1d263 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
1d264 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1d265 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f ../*.** The seco
1d266 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 nd argument to t
1d267 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 70 his function, op
1d268 2c 20 69 73 20 61 6c 77 61 79 73 20 53 41 56 45 , is always SAVE
1d269 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 0a 2a POINT_ROLLBACK.*
1d26a 2a 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 * or SAVEPOINT_R
1d26b 45 4c 45 41 53 45 2e 20 54 68 69 73 20 66 75 6e ELEASE. This fun
1d26c 63 74 69 6f 6e 20 65 69 74 68 65 72 20 72 65 6c ction either rel
1d26d 65 61 73 65 73 20 6f 72 20 72 6f 6c 6c 73 20 62 eases or rolls b
1d26e 61 63 6b 20 74 68 65 0a 2a 2a 20 73 61 76 65 70 ack the.** savep
1d26f 6f 69 6e 74 20 69 64 65 6e 74 69 66 69 65 64 20 oint identified
1d270 62 79 20 70 61 72 61 6d 65 74 65 72 20 69 53 61 by parameter iSa
1d271 76 65 70 6f 69 6e 74 2c 20 64 65 70 65 6e 64 69 vepoint, dependi
1d272 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 ng on the value
1d273 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a 2a 0a 2a 2a .** of op..**.**
1d274 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 53 61 76 65 Normally, iSave
1d275 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72 point is greater
1d276 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
1d277 6f 20 7a 65 72 6f 2e 20 48 6f 77 65 76 65 72 2c o zero. However,
1d278 20 69 66 20 6f 70 20 69 73 0a 2a 2a 20 53 41 56 if op is.** SAV
1d279 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c EPOINT_ROLLBACK,
1d27a 20 74 68 65 6e 20 69 53 61 76 65 70 6f 69 6e 74 then iSavepoint
1d27b 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 2d 31 2e may also be -1.
1d27c 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
1d27d 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f e .** contents o
1d27e 66 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61 f the entire tra
1d27f 6e 73 61 63 74 69 6f 6e 20 61 72 65 20 72 6f 6c nsaction are rol
1d280 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 69 led back. This i
1d281 73 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 s different.** f
1d282 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c 20 74 72 61 rom a normal tra
1d283 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 62 61 63 nsaction rollbac
1d284 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 6b 73 20 61 k, as no locks a
1d285 72 65 20 72 65 6c 65 61 73 65 64 20 61 6e 64 20 re released and
1d286 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 the.** transacti
1d287 6f 6e 20 72 65 6d 61 69 6e 73 20 6f 70 65 6e 2e on remains open.
1d288 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1d289 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
1d28a 72 65 65 53 61 76 65 70 6f 69 6e 74 28 42 74 72 reeSavepoint(Btr
1d28b 65 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 ee *p, int op, i
1d28c 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a nt iSavepoint){.
1d28d 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1d28e 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 26 26 E_OK;. if( p &&
1d28f 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
1d290 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 NS_WRITE ){.
1d291 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
1d292 70 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 65 p->pBt;. asse
1d293 72 74 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e rt( op==SAVEPOIN
1d294 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d T_RELEASE || op=
1d295 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
1d296 41 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ACK );. asser
1d297 74 28 20 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30 t( iSavepoint>=0
1d298 20 7c 7c 20 28 69 53 61 76 65 70 6f 69 6e 74 3d || (iSavepoint=
1d299 3d 2d 31 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 =-1 && op==SAVEP
1d29a 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 20 29 OINT_ROLLBACK) )
1d29b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1d29c 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 eeEnter(p);.
1d29d 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1d29e 72 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e rSavepoint(pBt->
1d29f 70 50 61 67 65 72 2c 20 6f 70 2c 20 69 53 61 76 pPager, op, iSav
1d2a0 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 69 66 28 epoint);. if(
1d2a1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1d2a2 7b 0a 20 20 20 20 20 20 69 66 28 20 69 53 61 76 {. if( iSav
1d2a3 65 70 6f 69 6e 74 3c 30 20 26 26 20 28 70 42 74 epoint<0 && (pBt
1d2a4 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 ->btsFlags & BTS
1d2a5 5f 49 4e 49 54 49 41 4c 4c 59 5f 45 4d 50 54 59 _INITIALLY_EMPTY
1d2a6 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )!=0 ){.
1d2a7 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 30 3b 0a pBt->nPage = 0;.
1d2a8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
1d2a9 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 = newDatabase(p
1d2aa 42 74 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e Bt);. pBt->
1d2ab 6e 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 nPage = get4byte
1d2ac 28 32 38 20 2b 20 70 42 74 2d 3e 70 50 61 67 65 (28 + pBt->pPage
1d2ad 31 2d 3e 61 44 61 74 61 29 3b 0a 0a 20 20 20 20 1->aData);..
1d2ae 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
1d2af 65 20 73 69 7a 65 20 77 61 73 20 77 72 69 74 74 e size was writt
1d2b0 65 6e 20 69 6e 74 6f 20 74 68 65 20 6f 66 66 73 en into the offs
1d2b1 65 74 20 32 38 20 6f 66 20 74 68 65 20 68 65 61 et 28 of the hea
1d2b2 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 77 68 65 der. ** whe
1d2b3 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f n the transactio
1d2b4 6e 20 73 74 61 72 74 65 64 2c 20 73 6f 20 77 65 n started, so we
1d2b5 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 76 know that the v
1d2b6 61 6c 75 65 20 61 74 20 6f 66 66 73 65 74 0a 20 alue at offset.
1d2b7 20 20 20 20 20 2a 2a 20 32 38 20 69 73 20 6e 6f ** 28 is no
1d2b8 6e 7a 65 72 6f 2e 20 2a 2f 0a 20 20 20 20 20 20 nzero. */.
1d2b9 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 50 61 assert( pBt->nPa
1d2ba 67 65 3e 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 ge>0 );. }.
1d2bb 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1d2bc 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 ave(p);. }. re
1d2bd 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1d2be 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63 * Create a new c
1d2bf 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 42 54 ursor for the BT
1d2c0 72 65 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 ree whose root i
1d2c1 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a s on the page.**
1d2c2 20 69 54 61 62 6c 65 2e 20 49 66 20 61 20 72 65 iTable. If a re
1d2c3 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20 69 ad-only cursor i
1d2c4 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 74 20 s requested, it
1d2c5 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a is assumed that.
1d2c6 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 61 6c ** the caller al
1d2c7 72 65 61 64 79 20 68 61 73 20 61 74 20 6c 65 61 ready has at lea
1d2c8 73 74 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 st a read-only t
1d2c9 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 0a ransaction open.
1d2ca 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ** on the databa
1d2cb 73 65 20 61 6c 72 65 61 64 79 2e 20 49 66 20 61 se already. If a
1d2cc 20 77 72 69 74 65 2d 63 75 72 73 6f 72 20 69 73 write-cursor is
1d2cd 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 6e requested, then
1d2ce 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 69 .** the caller i
1d2cf 73 20 61 73 73 75 6d 65 64 20 74 6f 20 68 61 76 s assumed to hav
1d2d0 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 e an open write
1d2d1 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a transaction..**.
1d2d2 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d 30 2c ** If wrFlag==0,
1d2d3 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 then the cursor
1d2d4 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 can only be use
1d2d5 64 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a d for reading..*
1d2d6 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d 31 2c 20 * If wrFlag==1,
1d2d7 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 then the cursor
1d2d8 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 can be used for
1d2d9 72 65 61 64 69 6e 67 20 6f 72 20 66 6f 72 0a 2a reading or for.*
1d2da 2a 20 77 72 69 74 69 6e 67 20 69 66 20 6f 74 68 * writing if oth
1d2db 65 72 20 63 6f 6e 64 69 74 69 6f 6e 73 20 66 6f er conditions fo
1d2dc 72 20 77 72 69 74 69 6e 67 20 61 72 65 20 61 6c r writing are al
1d2dd 73 6f 20 6d 65 74 2e 20 20 54 68 65 73 65 0a 2a so met. These.*
1d2de 2a 20 61 72 65 20 74 68 65 20 63 6f 6e 64 69 74 * are the condit
1d2df 69 6f 6e 73 20 74 68 61 74 20 6d 75 73 74 20 62 ions that must b
1d2e0 65 20 6d 65 74 20 69 6e 20 6f 72 64 65 72 20 66 e met in order f
1d2e1 6f 72 20 77 72 69 74 69 6e 67 20 74 6f 0a 2a 2a or writing to.**
1d2e2 20 62 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a be allowed:.**.
1d2e3 2a 2a 20 31 3a 20 20 54 68 65 20 63 75 72 73 6f ** 1: The curso
1d2e4 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e r must have been
1d2e5 20 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72 46 opened with wrF
1d2e6 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a 20 32 3a 20 lag==1.**.** 2:
1d2e7 20 4f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 Other database
1d2e8 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 connections that
1d2e9 20 73 68 61 72 65 20 74 68 65 20 73 61 6d 65 20 share the same
1d2ea 70 61 67 65 72 20 63 61 63 68 65 0a 2a 2a 20 20 pager cache.**
1d2eb 20 20 20 62 75 74 20 77 68 69 63 68 20 61 72 65 but which are
1d2ec 20 6e 6f 74 20 69 6e 20 74 68 65 20 52 45 41 44 not in the READ
1d2ed 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 73 74 61 _UNCOMMITTED sta
1d2ee 74 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 0a te may not have.
1d2ef 2a 2a 20 20 20 20 20 63 75 72 73 6f 72 73 20 6f ** cursors o
1d2f0 70 65 6e 20 77 69 74 68 20 77 72 46 6c 61 67 3d pen with wrFlag=
1d2f1 3d 30 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 =0 on the same t
1d2f2 61 62 6c 65 2e 20 20 4f 74 68 65 72 77 69 73 65 able. Otherwise
1d2f3 0a 2a 2a 20 20 20 20 20 74 68 65 20 63 68 61 6e .** the chan
1d2f4 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 69 73 ges made by this
1d2f5 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 77 6f write cursor wo
1d2f6 75 6c 64 20 62 65 20 76 69 73 69 62 6c 65 20 74 uld be visible t
1d2f7 6f 0a 2a 2a 20 20 20 20 20 74 68 65 20 72 65 61 o.** the rea
1d2f8 64 20 63 75 72 73 6f 72 73 20 69 6e 20 74 68 65 d cursors in the
1d2f9 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 other database
1d2fa 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a connection..**.*
1d2fb 2a 20 33 3a 20 20 54 68 65 20 64 61 74 61 62 61 * 3: The databa
1d2fc 73 65 20 6d 75 73 74 20 62 65 20 77 72 69 74 61 se must be writa
1d2fd 62 6c 65 20 28 6e 6f 74 20 6f 6e 20 72 65 61 64 ble (not on read
1d2fe 2d 6f 6e 6c 79 20 6d 65 64 69 61 29 0a 2a 2a 0a -only media).**.
1d2ff 2a 2a 20 34 3a 20 20 54 68 65 72 65 20 6d 75 73 ** 4: There mus
1d300 74 20 62 65 20 61 6e 20 61 63 74 69 76 65 20 74 t be an active t
1d301 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a ransaction..**.*
1d302 2a 20 4e 6f 20 63 68 65 63 6b 69 6e 67 20 69 73 * No checking is
1d303 20 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 done to make su
1d304 72 65 20 74 68 61 74 20 70 61 67 65 20 69 54 61 re that page iTa
1d305 62 6c 65 20 72 65 61 6c 6c 79 20 69 73 20 74 68 ble really is th
1d306 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f e.** root page o
1d307 66 20 61 20 62 2d 74 72 65 65 2e 20 20 49 66 20 f a b-tree. If
1d308 69 74 20 69 73 20 6e 6f 74 2c 20 74 68 65 6e 20 it is not, then
1d309 74 68 65 20 63 75 72 73 6f 72 20 61 63 71 75 69 the cursor acqui
1d30a 72 65 64 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 red.** will not
1d30b 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 2e 0a work correctly..
1d30c 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75 **.** It is assu
1d30d 6d 65 64 20 74 68 61 74 20 74 68 65 20 73 71 6c med that the sql
1d30e 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a ite3BtreeCursorZ
1d30f 65 72 6f 28 29 20 68 61 73 20 62 65 65 6e 20 63 ero() has been c
1d310 61 6c 6c 65 64 0a 2a 2a 20 6f 6e 20 70 43 75 72 alled.** on pCur
1d311 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 to initialize t
1d312 68 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 he memory space
1d313 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e prior to invokin
1d314 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a g this routine..
1d315 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 */.static int bt
1d316 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72 reeCursor(. Btr
1d317 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 ee *p,
1d318 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d319 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 /* The btree
1d31a 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 */. int iTable
1d31b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
1d31c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d31d 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 Root page of tab
1d31e 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 le to open */.
1d31f 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 int wrFlag,
1d320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d321 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 /* 1 to w
1d322 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c rite. 0 read-onl
1d323 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 y */. struct Ke
1d324 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c yInfo *pKeyInfo,
1d325 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d326 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 63 6f First arg to co
1d327 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f mparison functio
1d328 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 n */. BtCursor
1d329 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20 *pCur
1d32a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d32b 20 53 70 61 63 65 20 66 6f 72 20 6e 65 77 20 63 Space for new c
1d32c 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 42 74 ursor */.){. Bt
1d32d 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
1d32e 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 >pBt;
1d32f 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 62 /* Shared b
1d330 2d 74 72 65 65 20 68 61 6e 64 6c 65 20 2a 2f 0a -tree handle */.
1d331 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1d332 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
1d333 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 x(p) );. assert
1d334 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 77 ( wrFlag==0 || w
1d335 72 46 6c 61 67 3d 3d 31 20 29 3b 0a 0a 20 20 2f rFlag==1 );.. /
1d336 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1d337 61 73 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74 assert statement
1d338 73 20 76 65 72 69 66 79 20 74 68 61 74 20 69 66 s verify that if
1d339 20 74 68 69 73 20 69 73 20 61 20 73 68 61 72 61 this is a shara
1d33a 62 6c 65 20 0a 20 20 2a 2a 20 62 2d 74 72 65 65 ble . ** b-tree
1d33b 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 63 database, the c
1d33c 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c onnection is hol
1d33d 64 69 6e 67 20 74 68 65 20 72 65 71 75 69 72 65 ding the require
1d33e 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 2c 20 0a d table locks, .
1d33f 20 20 2a 2a 20 61 6e 64 20 74 68 61 74 20 6e 6f ** and that no
1d340 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f other connectio
1d341 6e 20 68 61 73 20 61 6e 79 20 6f 70 65 6e 20 63 n has any open c
1d342 75 72 73 6f 72 20 74 68 61 74 20 63 6f 6e 66 6c ursor that confl
1d343 69 63 74 73 20 77 69 74 68 20 0a 20 20 2a 2a 20 icts with . **
1d344 74 68 69 73 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 this lock. */.
1d345 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61 72 assert( hasShar
1d346 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
1d347 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 4b 65 79 (p, iTable, pKey
1d348 49 6e 66 6f 21 3d 30 2c 20 77 72 46 6c 61 67 2b Info!=0, wrFlag+
1d349 31 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 1) );. assert(
1d34a 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 21 68 61 wrFlag==0 || !ha
1d34b 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 sReadConflicts(p
1d34c 2c 20 69 54 61 62 6c 65 29 20 29 3b 0a 0a 20 20 , iTable) );..
1d34d 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 /* Assert that t
1d34e 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 70 he caller has op
1d34f 65 6e 65 64 20 74 68 65 20 72 65 71 75 69 72 65 ened the require
1d350 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 2a d transaction. *
1d351 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 /. assert( p->i
1d352 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e nTrans>TRANS_NON
1d353 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 E );. assert( w
1d354 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 70 2d 3e 69 rFlag==0 || p->i
1d355 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
1d356 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ITE );. assert(
1d357 20 70 42 74 2d 3e 70 50 61 67 65 31 20 26 26 20 pBt->pPage1 &&
1d358 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
1d359 74 61 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 ta );.. if( NEV
1d35a 45 52 28 77 72 46 6c 61 67 20 26 26 20 28 70 42 ER(wrFlag && (pB
1d35b 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 t->btsFlags & BT
1d35c 53 5f 52 45 41 44 5f 4f 4e 4c 59 29 21 3d 30 29 S_READ_ONLY)!=0)
1d35d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
1d35e 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a QLITE_READONLY;.
1d35f 20 20 7d 0a 20 20 69 66 28 20 69 54 61 62 6c 65 }. if( iTable
1d360 3d 3d 31 20 26 26 20 62 74 72 65 65 50 61 67 65 ==1 && btreePage
1d361 63 6f 75 6e 74 28 70 42 74 29 3d 3d 30 20 29 7b count(pBt)==0 ){
1d362 0a 20 20 20 20 61 73 73 65 72 74 28 20 77 72 46 . assert( wrF
1d363 6c 61 67 3d 3d 30 20 29 3b 0a 20 20 20 20 69 54 lag==0 );. iT
1d364 61 62 6c 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 able = 0;. }..
1d365 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20 /* Now that no
1d366 6f 74 68 65 72 20 65 72 72 6f 72 73 20 63 61 6e other errors can
1d367 20 6f 63 63 75 72 2c 20 66 69 6e 69 73 68 20 66 occur, finish f
1d368 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 74 illing in the Bt
1d369 43 75 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69 Cursor. ** vari
1d36a 61 62 6c 65 73 20 61 6e 64 20 6c 69 6e 6b 20 74 ables and link t
1d36b 68 65 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 he cursor into t
1d36c 68 65 20 42 74 53 68 61 72 65 64 20 6c 69 73 74 he BtShared list
1d36d 2e 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 70 67 . */. pCur->pg
1d36e 6e 6f 52 6f 6f 74 20 3d 20 28 50 67 6e 6f 29 69 noRoot = (Pgno)i
1d36f 54 61 62 6c 65 3b 0a 20 20 70 43 75 72 2d 3e 69 Table;. pCur->i
1d370 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 70 43 75 Page = -1;. pCu
1d371 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b r->pKeyInfo = pK
1d372 65 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72 2d 3e eyInfo;. pCur->
1d373 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 70 43 pBtree = p;. pC
1d374 75 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 ur->pBt = pBt;.
1d375 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 3d 20 pCur->wrFlag =
1d376 28 75 38 29 77 72 46 6c 61 67 3b 0a 20 20 70 43 (u8)wrFlag;. pC
1d377 75 72 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d ur->pNext = pBt-
1d378 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 >pCursor;. if(
1d379 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 pCur->pNext ){.
1d37a 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e pCur->pNext->
1d37b 70 50 72 65 76 20 3d 20 70 43 75 72 3b 0a 20 20 pPrev = pCur;.
1d37c 7d 0a 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 }. pBt->pCursor
1d37d 20 3d 20 70 43 75 72 3b 0a 20 20 70 43 75 72 2d = pCur;. pCur-
1d37e 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
1d37f 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 70 43 75 72 _INVALID;. pCur
1d380 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 20 3d 20 ->cachedRowid =
1d381 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 0;. return SQLI
1d382 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f TE_OK;.}.SQLITE_
1d383 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1d384 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a te3BtreeCursor(.
1d385 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 Btree *p,
1d386 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d387 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d388 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 The btree */.
1d389 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 int iTable,
1d38a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d38b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1d38c 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c oot page of tabl
1d38d 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 e to open */. i
1d38e 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 nt wrFlag,
1d38f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d390 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 /* 1
1d391 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64 to write. 0 read
1d392 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 -only */. struc
1d393 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 t KeyInfo *pKeyI
1d394 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 nfo,
1d395 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
1d396 61 72 67 20 74 6f 20 78 43 6f 6d 70 61 72 65 28 arg to xCompare(
1d397 29 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 ) */. BtCursor
1d398 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20 *pCur
1d399 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d39a 20 20 20 2f 2a 20 57 72 69 74 65 20 6e 65 77 20 /* Write new
1d39b 63 75 72 73 6f 72 20 68 65 72 65 20 2a 2f 0a 29 cursor here */.)
1d39c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 {. int rc;. sq
1d39d 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
1d39e 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 p);. rc = btree
1d39f 43 75 72 73 6f 72 28 70 2c 20 69 54 61 62 6c 65 Cursor(p, iTable
1d3a0 2c 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e , wrFlag, pKeyIn
1d3a1 66 6f 2c 20 70 43 75 72 29 3b 0a 20 20 73 71 6c fo, pCur);. sql
1d3a2 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1d3a3 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1d3a4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1d3a5 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 74 the size of a Bt
1d3a6 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 6e Cursor object in
1d3a7 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 bytes..**.** Th
1d3a8 69 73 20 69 6e 74 65 72 66 61 63 65 73 20 69 73 is interfaces is
1d3a9 20 6e 65 65 64 65 64 20 73 6f 20 74 68 61 74 20 needed so that
1d3aa 75 73 65 72 73 20 6f 66 20 63 75 72 73 6f 72 73 users of cursors
1d3ab 20 63 61 6e 20 70 72 65 61 6c 6c 6f 63 61 74 65 can preallocate
1d3ac 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 73 .** sufficient s
1d3ad 74 6f 72 61 67 65 20 74 6f 20 68 6f 6c 64 20 61 torage to hold a
1d3ae 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 42 74 cursor. The Bt
1d3af 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 73 Cursor object is
1d3b0 20 6f 70 61 71 75 65 0a 2a 2a 20 74 6f 20 75 73 opaque.** to us
1d3b1 65 72 73 20 73 6f 20 74 68 65 79 20 63 61 6e 6e ers so they cann
1d3b2 6f 74 20 64 6f 20 74 68 65 20 73 69 7a 65 6f 66 ot do the sizeof
1d3b3 28 29 20 74 68 65 6d 73 65 6c 76 65 73 20 2d 20 () themselves -
1d3b4 74 68 65 79 20 6d 75 73 74 20 63 61 6c 6c 0a 2a they must call.*
1d3b5 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a * this routine..
1d3b6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1d3b7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1d3b8 65 65 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 eeCursorSize(voi
1d3b9 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 d){. return ROU
1d3ba 4e 44 38 28 73 69 7a 65 6f 66 28 42 74 43 75 72 ND8(sizeof(BtCur
1d3bb 73 6f 72 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 sor));.}../*.**
1d3bc 49 6e 69 74 69 61 6c 69 7a 65 20 6d 65 6d 6f 72 Initialize memor
1d3bd 79 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 63 y that will be c
1d3be 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 20 onverted into a
1d3bf 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e BtCursor object.
1d3c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 6d 70 6c .**.** The simpl
1d3c1 65 20 61 70 70 72 6f 61 63 68 20 68 65 72 65 20 e approach here
1d3c2 77 6f 75 6c 64 20 62 65 20 74 6f 20 6d 65 6d 73 would be to mems
1d3c3 65 74 28 29 20 74 68 65 20 65 6e 74 69 72 65 20 et() the entire
1d3c4 6f 62 6a 65 63 74 0a 2a 2a 20 74 6f 20 7a 65 72 object.** to zer
1d3c5 6f 2e 20 20 42 75 74 20 69 74 20 74 75 72 6e 73 o. But it turns
1d3c6 20 6f 75 74 20 74 68 61 74 20 74 68 65 20 61 70 out that the ap
1d3c7 50 61 67 65 5b 5d 20 61 6e 64 20 61 69 49 64 78 Page[] and aiIdx
1d3c8 5b 5d 20 61 72 72 61 79 73 0a 2a 2a 20 64 6f 20 [] arrays.** do
1d3c9 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 7a not need to be z
1d3ca 65 72 6f 65 64 20 61 6e 64 20 74 68 65 79 20 61 eroed and they a
1d3cb 72 65 20 6c 61 72 67 65 2c 20 73 6f 20 77 65 20 re large, so we
1d3cc 63 61 6e 20 73 61 76 65 20 61 20 6c 6f 74 0a 2a can save a lot.*
1d3cd 2a 20 6f 66 20 72 75 6e 2d 74 69 6d 65 20 62 79 * of run-time by
1d3ce 20 73 6b 69 70 70 69 6e 67 20 74 68 65 20 69 6e skipping the in
1d3cf 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 itialization of
1d3d0 74 68 6f 73 65 20 65 6c 65 6d 65 6e 74 73 2e 0a those elements..
1d3d1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1d3d2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
1d3d3 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 42 74 reeCursorZero(Bt
1d3d4 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 6d 65 Cursor *p){. me
1d3d5 6d 73 65 74 28 70 2c 20 30 2c 20 6f 66 66 73 65 mset(p, 0, offse
1d3d6 74 6f 66 28 42 74 43 75 72 73 6f 72 2c 20 69 50 tof(BtCursor, iP
1d3d7 61 67 65 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 age));.}../*.**
1d3d8 53 65 74 20 74 68 65 20 63 61 63 68 65 64 20 72 Set the cached r
1d3d9 6f 77 69 64 20 76 61 6c 75 65 20 6f 66 20 65 76 owid value of ev
1d3da 65 72 79 20 63 75 72 73 6f 72 20 69 6e 20 74 68 ery cursor in th
1d3db 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
1d3dc 66 69 6c 65 0a 2a 2a 20 61 73 20 70 43 75 72 20 file.** as pCur
1d3dd 61 6e 64 20 68 61 76 69 6e 67 20 74 68 65 20 73 and having the s
1d3de 61 6d 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 ame root page nu
1d3df 6d 62 65 72 20 61 73 20 70 43 75 72 2e 20 20 54 mber as pCur. T
1d3e0 68 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 73 he value is.** s
1d3e1 65 74 20 74 6f 20 69 52 6f 77 69 64 2e 0a 2a 2a et to iRowid..**
1d3e2 0a 2a 2a 20 4f 6e 6c 79 20 70 6f 73 69 74 69 76 .** Only positiv
1d3e3 65 20 72 6f 77 69 64 20 76 61 6c 75 65 73 20 61 e rowid values a
1d3e4 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 76 61 re considered va
1d3e5 6c 69 64 20 66 6f 72 20 74 68 69 73 20 63 61 63 lid for this cac
1d3e6 68 65 2e 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 he..** The cache
1d3e7 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 is initialized
1d3e8 74 6f 20 7a 65 72 6f 2c 20 69 6e 64 69 63 61 74 to zero, indicat
1d3e9 69 6e 67 20 61 6e 20 69 6e 76 61 6c 69 64 20 63 ing an invalid c
1d3ea 61 63 68 65 2e 0a 2a 2a 20 41 20 62 74 72 65 65 ache..** A btree
1d3eb 20 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 20 will work fine
1d3ec 77 69 74 68 20 7a 65 72 6f 20 6f 72 20 6e 65 67 with zero or neg
1d3ed 61 74 69 76 65 20 72 6f 77 69 64 73 2e 20 20 57 ative rowids. W
1d3ee 65 20 6a 75 73 74 20 63 61 6e 6e 6f 74 0a 2a 2a e just cannot.**
1d3ef 20 63 61 63 68 65 20 7a 65 72 6f 20 6f 72 20 6e cache zero or n
1d3f0 65 67 61 74 69 76 65 20 72 6f 77 69 64 73 2c 20 egative rowids,
1d3f1 77 68 69 63 68 20 6d 65 61 6e 73 20 74 61 62 6c which means tabl
1d3f2 65 73 20 74 68 61 74 20 75 73 65 20 7a 65 72 6f es that use zero
1d3f3 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 or.** negative
1d3f4 72 6f 77 69 64 73 20 6d 69 67 68 74 20 72 75 6e rowids might run
1d3f5 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 a little slower
1d3f6 2e 20 20 42 75 74 20 69 6e 20 70 72 61 63 74 69 . But in practi
1d3f7 63 65 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 6e ce, zero.** or n
1d3f8 65 67 61 74 69 76 65 20 72 6f 77 69 64 73 20 61 egative rowids a
1d3f9 72 65 20 76 65 72 79 20 75 6e 63 6f 6d 6d 6f 6e re very uncommon
1d3fa 20 73 6f 20 74 68 69 73 20 73 68 6f 75 6c 64 20 so this should
1d3fb 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 6c 65 6d not be a problem
1d3fc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1d3fd 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1d3fe 42 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f BtreeSetCachedRo
1d3ff 77 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 wid(BtCursor *pC
1d400 75 72 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ur, sqlite3_int6
1d401 34 20 69 52 6f 77 69 64 29 7b 0a 20 20 42 74 43 4 iRowid){. BtC
1d402 75 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28 ursor *p;. for(
1d403 70 3d 70 43 75 72 2d 3e 70 42 74 2d 3e 70 43 75 p=pCur->pBt->pCu
1d404 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e rsor; p; p=p->pN
1d405 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d ext){. if( p-
1d406 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 70 43 75 72 2d >pgnoRoot==pCur-
1d407 3e 70 67 6e 6f 52 6f 6f 74 20 29 20 70 2d 3e 63 >pgnoRoot ) p->c
1d408 61 63 68 65 64 52 6f 77 69 64 20 3d 20 69 52 6f achedRowid = iRo
1d409 77 69 64 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 wid;. }. asser
1d40a 74 28 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 t( pCur->cachedR
1d40b 6f 77 69 64 3d 3d 69 52 6f 77 69 64 20 29 3b 0a owid==iRowid );.
1d40c 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1d40d 74 68 65 20 63 61 63 68 65 64 20 72 6f 77 69 64 the cached rowid
1d40e 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 63 for the given c
1d40f 75 72 73 6f 72 2e 20 20 41 20 6e 65 67 61 74 69 ursor. A negati
1d410 76 65 20 6f 72 20 7a 65 72 6f 0a 2a 2a 20 72 65 ve or zero.** re
1d411 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 turn value indic
1d412 61 74 65 73 20 74 68 61 74 20 74 68 65 20 72 6f ates that the ro
1d413 77 69 64 20 63 61 63 68 65 20 69 73 20 69 6e 76 wid cache is inv
1d414 61 6c 69 64 20 61 6e 64 20 73 68 6f 75 6c 64 20 alid and should
1d415 62 65 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 be.** ignored.
1d416 49 66 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 If the rowid cac
1d417 68 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 66 he has never bef
1d418 6f 72 65 20 62 65 65 6e 20 73 65 74 2c 20 74 68 ore been set, th
1d419 65 6e 20 61 0a 2a 2a 20 7a 65 72 6f 20 69 73 20 en a.** zero is
1d41a 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c returned..*/.SQL
1d41b 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
1d41c 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 te3_int64 sqlite
1d41d 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64 52 3BtreeGetCachedR
1d41e 6f 77 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 owid(BtCursor *p
1d41f 43 75 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 Cur){. return p
1d420 43 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 Cur->cachedRowid
1d421 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 ;.}../*.** Close
1d422 20 61 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 a cursor. The
1d423 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 read lock on the
1d424 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
1d425 73 20 72 65 6c 65 61 73 65 64 0a 2a 2a 20 77 68 s released.** wh
1d426 65 6e 20 74 68 65 20 6c 61 73 74 20 63 75 72 73 en the last curs
1d427 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f or is closed..*/
1d428 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1d429 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
1d42a 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43 75 CloseCursor(BtCu
1d42b 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 42 rsor *pCur){. B
1d42c 74 72 65 65 20 2a 70 42 74 72 65 65 20 3d 20 70 tree *pBtree = p
1d42d 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 69 Cur->pBtree;. i
1d42e 66 28 20 70 42 74 72 65 65 20 29 7b 0a 20 20 20 f( pBtree ){.
1d42f 20 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 53 68 int i;. BtSh
1d430 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 ared *pBt = pCur
1d431 2d 3e 70 42 74 3b 0a 20 20 20 20 73 71 6c 69 74 ->pBt;. sqlit
1d432 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 e3BtreeEnter(pBt
1d433 72 65 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ree);. sqlite
1d434 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 3BtreeClearCurso
1d435 72 28 70 43 75 72 29 3b 0a 20 20 20 20 69 66 28 r(pCur);. if(
1d436 20 70 43 75 72 2d 3e 70 50 72 65 76 20 29 7b 0a pCur->pPrev ){.
1d437 20 20 20 20 20 20 70 43 75 72 2d 3e 70 50 72 65 pCur->pPre
1d438 76 2d 3e 70 4e 65 78 74 20 3d 20 70 43 75 72 2d v->pNext = pCur-
1d439 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 >pNext;. }els
1d43a 65 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 43 e{. pBt->pC
1d43b 75 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 4e ursor = pCur->pN
1d43c 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ext;. }. i
1d43d 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 f( pCur->pNext )
1d43e 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4e {. pCur->pN
1d43f 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 ext->pPrev = pCu
1d440 72 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 7d 0a r->pPrev;. }.
1d441 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d for(i=0; i<=
1d442 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b pCur->iPage; i++
1d443 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 ){. release
1d444 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 Page(pCur->apPag
1d445 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 e[i]);. }.
1d446 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e unlockBtreeIfUn
1d447 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 69 used(pBt);. i
1d448 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f nvalidateOverflo
1d449 77 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 wCache(pCur);.
1d44a 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 /* sqlite3_fre
1d44b 65 28 70 43 75 72 29 3b 20 2a 2f 0a 20 20 20 20 e(pCur); */.
1d44c 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1d44d 65 28 70 42 74 72 65 65 29 3b 0a 20 20 7d 0a 20 e(pBtree);. }.
1d44e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1d44f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 K;.}../*.** Make
1d450 20 73 75 72 65 20 74 68 65 20 42 74 43 75 72 73 sure the BtCurs
1d451 6f 72 2a 20 67 69 76 65 6e 20 69 6e 20 74 68 65 or* given in the
1d452 20 61 72 67 75 6d 65 6e 74 20 68 61 73 20 61 20 argument has a
1d453 76 61 6c 69 64 0a 2a 2a 20 42 74 43 75 72 73 6f valid.** BtCurso
1d454 72 2e 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 r.info structure
1d455 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 . If it is not
1d456 61 6c 72 65 61 64 79 20 76 61 6c 69 64 2c 20 63 already valid, c
1d457 61 6c 6c 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 all.** btreePars
1d458 65 43 65 6c 6c 28 29 20 74 6f 20 66 69 6c 6c 20 eCell() to fill
1d459 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 42 74 43 it in..**.** BtC
1d45a 75 72 73 6f 72 2e 69 6e 66 6f 20 69 73 20 61 20 ursor.info is a
1d45b 63 61 63 68 65 20 6f 66 20 74 68 65 20 69 6e 66 cache of the inf
1d45c 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 ormation in the
1d45d 63 75 72 72 65 6e 74 20 63 65 6c 6c 2e 0a 2a 2a current cell..**
1d45e 20 55 73 69 6e 67 20 74 68 69 73 20 63 61 63 68 Using this cach
1d45f 65 20 72 65 64 75 63 65 73 20 74 68 65 20 6e 75 e reduces the nu
1d460 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f mber of calls to
1d461 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 btreeParseCell(
1d462 29 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 37 2d 30 36 )..**.** 2007-06
1d463 2d 32 35 3a 20 20 54 68 65 72 65 20 69 73 20 61 -25: There is a
1d464 20 62 75 67 20 69 6e 20 73 6f 6d 65 20 76 65 72 bug in some ver
1d465 73 69 6f 6e 73 20 6f 66 20 4d 53 56 43 20 74 68 sions of MSVC th
1d466 61 74 20 63 61 75 73 65 20 74 68 65 0a 2a 2a 20 at cause the.**
1d467 63 6f 6d 70 69 6c 65 72 20 74 6f 20 63 72 61 73 compiler to cras
1d468 68 20 77 68 65 6e 20 67 65 74 43 65 6c 6c 49 6e h when getCellIn
1d469 66 6f 28 29 20 69 73 20 69 6d 70 6c 65 6d 65 6e fo() is implemen
1d46a 74 65 64 20 61 73 20 61 20 6d 61 63 72 6f 2e 0a ted as a macro..
1d46b 2a 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 20 ** But there is
1d46c 61 20 6d 65 61 73 75 72 65 61 62 6c 65 20 73 70 a measureable sp
1d46d 65 65 64 20 61 64 76 61 6e 74 61 67 65 20 74 6f eed advantage to
1d46e 20 75 73 69 6e 67 20 74 68 65 20 6d 61 63 72 6f using the macro
1d46f 20 6f 6e 20 67 63 63 0a 2a 2a 20 28 77 68 65 6e on gcc.** (when
1d470 20 6c 65 73 73 20 63 6f 6d 70 69 6c 65 72 20 6f less compiler o
1d471 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 6c 69 6b ptimizations lik
1d472 65 20 2d 4f 73 20 6f 72 20 2d 4f 30 20 61 72 65 e -Os or -O0 are
1d473 20 75 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a used and the.**
1d474 20 63 6f 6d 70 69 6c 65 72 20 69 73 20 6e 6f 74 compiler is not
1d475 20 64 6f 69 6e 67 20 61 67 72 65 73 73 69 76 65 doing agressive
1d476 20 69 6e 6c 69 6e 69 6e 67 2e 29 20 20 53 6f 20 inlining.) So
1d477 77 65 20 75 73 65 20 61 20 72 65 61 6c 20 66 75 we use a real fu
1d478 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 4d 53 nction.** for MS
1d479 56 43 20 61 6e 64 20 61 20 6d 61 63 72 6f 20 66 VC and a macro f
1d47a 6f 72 20 65 76 65 72 79 74 68 69 6e 67 20 65 6c or everything el
1d47b 73 65 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35 se. Ticket #245
1d47c 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 7..*/.#ifndef ND
1d47d 45 42 55 47 0a 20 20 73 74 61 74 69 63 20 76 6f EBUG. static vo
1d47e 69 64 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 id assertCellInf
1d47f 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 o(BtCursor *pCur
1d480 29 7b 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 ){. CellInfo
1d481 69 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 69 50 info;. int iP
1d482 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 age = pCur->iPag
1d483 65 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 69 e;. memset(&i
1d484 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 69 nfo, 0, sizeof(i
1d485 6e 66 6f 29 29 3b 0a 20 20 20 20 62 74 72 65 65 nfo));. btree
1d486 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e ParseCell(pCur->
1d487 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 20 70 apPage[iPage], p
1d488 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 Cur->aiIdx[iPage
1d489 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61 ], &info);. a
1d48a 73 73 65 72 74 28 20 6d 65 6d 63 6d 70 28 26 69 ssert( memcmp(&i
1d48b 6e 66 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f nfo, &pCur->info
1d48c 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3d , sizeof(info))=
1d48d 3d 30 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a =0 );. }.#else.
1d48e 20 20 23 64 65 66 69 6e 65 20 61 73 73 65 72 74 #define assert
1d48f 43 65 6c 6c 49 6e 66 6f 28 78 29 0a 23 65 6e 64 CellInfo(x).#end
1d490 69 66 0a 23 69 66 64 65 66 20 5f 4d 53 43 5f 56 if.#ifdef _MSC_V
1d491 45 52 0a 20 20 2f 2a 20 55 73 65 20 61 20 72 65 ER. /* Use a re
1d492 61 6c 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 4d al function in M
1d493 53 56 43 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 SVC to work arou
1d494 6e 64 20 62 75 67 73 20 69 6e 20 74 68 61 74 20 nd bugs in that
1d495 63 6f 6d 70 69 6c 65 72 2e 20 2a 2f 0a 20 20 73 compiler. */. s
1d496 74 61 74 69 63 20 76 6f 69 64 20 67 65 74 43 65 tatic void getCe
1d497 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20 llInfo(BtCursor
1d498 2a 70 43 75 72 29 7b 0a 20 20 20 20 69 66 28 20 *pCur){. if(
1d499 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
1d49a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 ==0 ){. int
1d49b 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 iPage = pCur->i
1d49c 50 61 67 65 3b 0a 20 20 20 20 20 20 62 74 72 65 Page;. btre
1d49d 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d eParseCell(pCur-
1d49e 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 >apPage[iPage],p
1d49f 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 Cur->aiIdx[iPage
1d4a0 5d 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a ],&pCur->info);.
1d4a1 20 20 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 pCur->vali
1d4a2 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 7d dNKey = 1;. }
1d4a3 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 else{. asse
1d4a4 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 rtCellInfo(pCur)
1d4a5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 ;. }. }.#els
1d4a6 65 20 2f 2a 20 69 66 20 6e 6f 74 20 5f 4d 53 43 e /* if not _MSC
1d4a7 5f 56 45 52 20 2a 2f 0a 20 20 2f 2a 20 55 73 65 _VER */. /* Use
1d4a8 20 61 20 6d 61 63 72 6f 20 69 6e 20 61 6c 6c 20 a macro in all
1d4a9 6f 74 68 65 72 20 63 6f 6d 70 69 6c 65 72 73 20 other compilers
1d4aa 73 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 so that the func
1d4ab 74 69 6f 6e 20 69 73 20 69 6e 6c 69 6e 65 64 20 tion is inlined
1d4ac 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 43 65 */.#define getCe
1d4ad 6c 6c 49 6e 66 6f 28 70 43 75 72 29 20 20 20 20 llInfo(pCur)
1d4ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4b1 20 20 5c 0a 20 20 69 66 28 20 70 43 75 72 2d 3e \. if( pCur->
1d4b2 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b info.nSize==0 ){
1d4b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4b6 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 69 50 61 \. int iPa
1d4b7 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 ge = pCur->iPage
1d4b8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1d4b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4bb 20 20 20 20 5c 0a 20 20 20 20 62 74 72 65 65 50 \. btreeP
1d4bc 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 arseCell(pCur->a
1d4bd 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 75 pPage[iPage],pCu
1d4be 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c r->aiIdx[iPage],
1d4bf 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 5c 0a &pCur->info); \.
1d4c0 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e pCur->validN
1d4c1 4b 65 79 20 3d 20 31 3b 20 20 20 20 20 20 20 20 Key = 1;
1d4c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
1d4c5 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 . }else{
1d4c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4ca 5c 0a 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c \. assertCell
1d4cb 49 6e 66 6f 28 70 43 75 72 29 3b 20 20 20 20 20 Info(pCur);
1d4cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d4cf 20 5c 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a \. }.#endif /*
1d4d0 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 23 69 _MSC_VER */..#i
1d4d1 66 6e 64 65 66 20 4e 44 45 42 55 47 20 20 2f 2a fndef NDEBUG /*
1d4d2 20 54 68 65 20 6e 65 78 74 20 72 6f 75 74 69 6e The next routin
1d4d3 65 20 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 e used only with
1d4d4 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 in assert() stat
1d4d5 65 6d 65 6e 74 73 20 2a 2f 0a 2f 2a 0a 2a 2a 20 ements */./*.**
1d4d6 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 Return true if t
1d4d7 68 65 20 67 69 76 65 6e 20 42 74 43 75 72 73 6f he given BtCurso
1d4d8 72 20 69 73 20 76 61 6c 69 64 2e 20 20 41 20 76 r is valid. A v
1d4d9 61 6c 69 64 20 63 75 72 73 6f 72 20 69 73 20 6f alid cursor is o
1d4da 6e 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 63 75 ne.** that is cu
1d4db 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
1d4dc 20 74 6f 20 61 20 72 6f 77 20 69 6e 20 61 20 28 to a row in a (
1d4dd 6e 6f 6e 2d 65 6d 70 74 79 29 20 74 61 62 6c 65 non-empty) table
1d4de 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 76 ..** This is a v
1d4df 65 72 69 66 69 63 61 74 69 6f 6e 20 72 6f 75 74 erification rout
1d4e0 69 6e 65 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 ine is used only
1d4e1 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29 within assert()
1d4e2 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a statements..*/.
1d4e3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1d4e4 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
1d4e5 75 72 73 6f 72 49 73 56 61 6c 69 64 28 42 74 43 ursorIsValid(BtC
1d4e6 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
1d4e7 72 65 74 75 72 6e 20 70 43 75 72 20 26 26 20 70 return pCur && p
1d4e8 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
1d4e9 53 4f 52 5f 56 41 4c 49 44 3b 0a 7d 0a 23 65 6e SOR_VALID;.}.#en
1d4ea 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f dif /* NDEBUG */
1d4eb 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 ../*.** Set *pSi
1d4ec 7a 65 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f ze to the size o
1d4ed 66 20 74 68 65 20 62 75 66 66 65 72 20 6e 65 65 f the buffer nee
1d4ee 64 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ded to hold the
1d4ef 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 74 68 65 20 value of.** the
1d4f0 6b 65 79 20 66 6f 72 20 74 68 65 20 63 75 72 72 key for the curr
1d4f1 65 6e 74 20 65 6e 74 72 79 2e 20 20 49 66 20 74 ent entry. If t
1d4f2 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 he cursor is not
1d4f3 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 20 pointing.** to
1d4f4 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2c 20 2a a valid entry, *
1d4f5 70 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 pSize is set to
1d4f6 30 2e 20 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 0. .**.** For a
1d4f7 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 49 table with the I
1d4f8 4e 54 4b 45 59 20 66 6c 61 67 20 73 65 74 2c 20 NTKEY flag set,
1d4f9 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 this routine ret
1d4fa 75 72 6e 73 20 74 68 65 20 6b 65 79 0a 2a 2a 20 urns the key.**
1d4fb 69 74 73 65 6c 66 2c 20 6e 6f 74 20 74 68 65 20 itself, not the
1d4fc 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
1d4fd 69 6e 20 74 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a in the key..**.*
1d4fe 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 * The caller mus
1d4ff 74 20 70 6f 73 69 74 69 6f 6e 20 74 68 65 20 63 t position the c
1d500 75 72 73 6f 72 20 70 72 69 6f 72 20 74 6f 20 69 ursor prior to i
1d501 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 nvoking this rou
1d502 74 69 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 69 tine..** .** Thi
1d503 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 6e 6f 74 s routine cannot
1d504 20 66 61 69 6c 2e 20 20 49 74 20 61 6c 77 61 79 fail. It alway
1d505 73 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 s returns SQLITE
1d506 5f 4f 4b 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 _OK. .*/.SQLITE
1d507 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1d508 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 ite3BtreeKeySize
1d509 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
1d50a 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 i64 *pSize){.
1d50b 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
1d50c 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
1d50d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
1d50e 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
1d50f 5f 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 _INVALID || pCur
1d510 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
1d511 5f 56 41 4c 49 44 20 29 3b 0a 20 20 69 66 28 20 _VALID );. if(
1d512 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 pCur->eState!=CU
1d513 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 RSOR_VALID ){.
1d514 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 *pSize = 0;.
1d515 7d 65 6c 73 65 7b 0a 20 20 20 20 67 65 74 43 65 }else{. getCe
1d516 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 llInfo(pCur);.
1d517 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 2d *pSize = pCur-
1d518 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a >info.nKey;. }.
1d519 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1d51a 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 OK;.}../*.** Set
1d51b 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20 6e *pSize to the n
1d51c 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1d51d 66 20 64 61 74 61 20 69 6e 20 74 68 65 20 65 6e f data in the en
1d51e 74 72 79 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f try the.** curso
1d51f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e r currently poin
1d520 74 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ts to..**.** The
1d521 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 67 75 61 caller must gua
1d522 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 rantee that the
1d523 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 cursor is pointi
1d524 6e 67 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c ng to a non-NULL
1d525 0a 2a 2a 20 76 61 6c 69 64 20 65 6e 74 72 79 2e .** valid entry.
1d526 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 In other words
1d527 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 , the calling pr
1d528 6f 63 65 64 75 72 65 20 6d 75 73 74 20 67 75 61 ocedure must gua
1d529 72 61 6e 74 65 65 0a 2a 2a 20 74 68 61 74 20 74 rantee.** that t
1d52a 68 65 20 63 75 72 73 6f 72 20 68 61 73 20 43 75 he cursor has Cu
1d52b 72 73 6f 72 2e 65 53 74 61 74 65 3d 3d 43 55 52 rsor.eState==CUR
1d52c 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a SOR_VALID..**.**
1d52d 20 46 61 69 6c 75 72 65 20 69 73 20 6e 6f 74 20 Failure is not
1d52e 70 6f 73 73 69 62 6c 65 2e 20 20 54 68 69 73 20 possible. This
1d52f 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 function always
1d530 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f returns SQLITE_O
1d531 4b 2e 0a 2a 2a 20 49 74 20 6d 69 67 68 74 20 6a K..** It might j
1d532 75 73 74 20 61 73 20 77 65 6c 6c 20 62 65 20 61 ust as well be a
1d533 20 70 72 6f 63 65 64 75 72 65 20 28 72 65 74 75 procedure (retu
1d534 72 6e 69 6e 67 20 76 6f 69 64 29 20 62 75 74 20 rning void) but
1d535 77 65 20 63 6f 6e 74 69 6e 75 65 0a 2a 2a 20 74 we continue.** t
1d536 6f 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 o return an inte
1d537 67 65 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20 ger result code
1d538 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 72 for historical r
1d539 65 61 73 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 easons..*/.SQLIT
1d53a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1d53b 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 53 69 lite3BtreeDataSi
1d53c 7a 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ze(BtCursor *pCu
1d53d 72 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 7b 0a r, u32 *pSize){.
1d53e 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
1d53f 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
1d540 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
1d541 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
1d542 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 67 65 OR_VALID );. ge
1d543 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b tCellInfo(pCur);
1d544 0a 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 . *pSize = pCur
1d545 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 ->info.nData;.
1d546 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1d547 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e ;.}../*.** Given
1d548 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
1d549 20 6f 66 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 of an overflow
1d54a 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 page in the data
1d54b 62 61 73 65 20 28 70 61 72 61 6d 65 74 65 72 0a base (parameter.
1d54c 2a 2a 20 6f 76 66 6c 29 2c 20 74 68 69 73 20 66 ** ovfl), this f
1d54d 75 6e 63 74 69 6f 6e 20 66 69 6e 64 73 20 74 68 unction finds th
1d54e 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 e page number of
1d54f 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 the next page i
1d550 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 n the .** linked
1d551 20 6c 69 73 74 20 6f 66 20 6f 76 65 72 66 6c 6f list of overflo
1d552 77 20 70 61 67 65 73 2e 20 49 66 20 70 6f 73 73 w pages. If poss
1d553 69 62 6c 65 2c 20 69 74 20 75 73 65 73 20 74 68 ible, it uses th
1d554 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a e auto-vacuum.**
1d555 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 64 61 74 pointer-map dat
1d556 61 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 61 a instead of rea
1d557 64 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 ding the content
1d558 20 6f 66 20 70 61 67 65 20 6f 76 66 6c 20 74 6f of page ovfl to
1d559 20 64 6f 20 73 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 do so. .**.** I
1d55a 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
1d55b 73 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f s an SQLite erro
1d55c 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
1d55d 65 64 2e 20 4f 74 68 65 72 77 69 73 65 3a 0a 2a ed. Otherwise:.*
1d55e 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 6e 75 *.** The page nu
1d55f 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 mber of the next
1d560 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 overflow page i
1d561 6e 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 n the linked lis
1d562 74 20 69 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e t is .** written
1d563 20 74 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 2e 20 to *pPgnoNext.
1d564 49 66 20 70 61 67 65 20 6f 76 66 6c 20 69 73 20 If page ovfl is
1d565 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e the last page in
1d566 20 69 74 73 20 6c 69 6e 6b 65 64 20 0a 2a 2a 20 its linked .**
1d567 6c 69 73 74 2c 20 2a 70 50 67 6e 6f 4e 65 78 74 list, *pPgnoNext
1d568 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e is set to zero.
1d569 20 0a 2a 2a 0a 2a 2a 20 49 66 20 70 70 50 61 67 .**.** If ppPag
1d56a 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 61 e is not NULL, a
1d56b 6e 64 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 nd a reference t
1d56c 6f 20 74 68 65 20 4d 65 6d 50 61 67 65 20 6f 62 o the MemPage ob
1d56d 6a 65 63 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 ject correspondi
1d56e 6e 67 0a 2a 2a 20 74 6f 20 70 61 67 65 20 6e 75 ng.** to page nu
1d56f 6d 62 65 72 20 70 4f 76 66 6c 20 77 61 73 20 6f mber pOvfl was o
1d570 62 74 61 69 6e 65 64 2c 20 74 68 65 6e 20 2a 70 btained, then *p
1d571 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 pPage is set to
1d572 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a point to that.**
1d573 20 72 65 66 65 72 65 6e 63 65 2e 20 49 74 20 69 reference. It i
1d574 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 s the responsibi
1d575 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c lity of the call
1d576 65 72 20 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 er to call relea
1d577 73 65 50 61 67 65 28 29 0a 2a 2a 20 6f 6e 20 2a sePage().** on *
1d578 70 70 50 61 67 65 20 74 6f 20 66 72 65 65 20 74 ppPage to free t
1d579 68 65 20 72 65 66 65 72 65 6e 63 65 2e 20 49 6e he reference. In
1d57a 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61 no reference wa
1d57b 73 20 6f 62 74 61 69 6e 65 64 20 28 62 65 63 61 s obtained (beca
1d57c 75 73 65 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 use.** the point
1d57d 65 72 2d 6d 61 70 20 77 61 73 20 75 73 65 64 20 er-map was used
1d57e 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 76 61 to obtain the va
1d57f 6c 75 65 20 66 6f 72 20 2a 70 50 67 6e 6f 4e 65 lue for *pPgnoNe
1d580 78 74 29 2c 20 74 68 65 6e 0a 2a 2a 20 2a 70 70 xt), then.** *pp
1d581 50 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 7a Page is set to z
1d582 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ero..*/.static i
1d583 6e 74 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 nt getOverflowPa
1d584 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a ge(. BtShared *
1d585 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 pBt,
1d586 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
1d587 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e se file */. Pgn
1d588 6f 20 6f 76 66 6c 2c 20 20 20 20 20 20 20 20 20 o ovfl,
1d589 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
1d58a 72 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 rent overflow pa
1d58b 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 4d ge number */. M
1d58c 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c emPage **ppPage,
1d58d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1d58e 55 54 3a 20 4d 65 6d 50 61 67 65 20 68 61 6e 64 UT: MemPage hand
1d58f 6c 65 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 le (may be NULL)
1d590 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e */. Pgno *pPgn
1d591 6f 4e 65 78 74 20 20 20 20 20 20 20 20 20 20 20 oNext
1d592 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 78 74 20 /* OUT: Next
1d593 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 overflow page nu
1d594 6d 62 65 72 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e mber */.){. Pgn
1d595 6f 20 6e 65 78 74 20 3d 20 30 3b 0a 20 20 4d 65 o next = 0;. Me
1d596 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 mPage *pPage = 0
1d597 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
1d598 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 ITE_OK;.. asser
1d599 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1d59a 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
1d59b 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 70 50 ) );. assert(pP
1d59c 67 6e 6f 4e 65 78 74 29 3b 0a 0a 23 69 66 6e 64 gnoNext);..#ifnd
1d59d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1d59e 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 54 UTOVACUUM. /* T
1d59f 72 79 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e ry to find the n
1d5a0 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 ext page in the
1d5a1 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 75 73 overflow list us
1d5a2 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 61 75 74 ing the. ** aut
1d5a3 6f 76 61 63 75 75 6d 20 70 6f 69 6e 74 65 72 2d ovacuum pointer-
1d5a4 6d 61 70 20 70 61 67 65 73 2e 20 47 75 65 73 73 map pages. Guess
1d5a5 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 70 that the next p
1d5a6 61 67 65 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65 age in . ** the
1d5a7 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 69 overflow list i
1d5a8 73 20 70 61 67 65 20 6e 75 6d 62 65 72 20 28 6f s page number (o
1d5a9 76 66 6c 2b 31 29 2e 20 49 66 20 74 68 61 74 20 vfl+1). If that
1d5aa 67 75 65 73 73 20 74 75 72 6e 73 20 0a 20 20 2a guess turns . *
1d5ab 2a 20 6f 75 74 20 74 6f 20 62 65 20 77 72 6f 6e * out to be wron
1d5ac 67 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 g, fall back to
1d5ad 6c 6f 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 loading the data
1d5ae 20 6f 66 20 70 61 67 65 20 0a 20 20 2a 2a 20 6e of page . ** n
1d5af 75 6d 62 65 72 20 6f 76 66 6c 20 74 6f 20 64 65 umber ovfl to de
1d5b0 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 65 78 74 termine the next
1d5b1 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 20 20 page number..
1d5b2 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 */. if( pBt->au
1d5b3 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 toVacuum ){.
1d5b4 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 20 20 50 Pgno pgno;. P
1d5b5 67 6e 6f 20 69 47 75 65 73 73 20 3d 20 6f 76 66 gno iGuess = ovf
1d5b6 6c 2b 31 3b 0a 20 20 20 20 75 38 20 65 54 79 70 l+1;. u8 eTyp
1d5b7 65 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 50 e;.. while( P
1d5b8 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 TRMAP_ISPAGE(pBt
1d5b9 2c 20 69 47 75 65 73 73 29 20 7c 7c 20 69 47 75 , iGuess) || iGu
1d5ba 65 73 73 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 ess==PENDING_BYT
1d5bb 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 E_PAGE(pBt) ){.
1d5bc 20 20 20 20 20 69 47 75 65 73 73 2b 2b 3b 0a 20 iGuess++;.
1d5bd 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 69 47 }.. if( iG
1d5be 75 65 73 73 3c 3d 62 74 72 65 65 50 61 67 65 63 uess<=btreePagec
1d5bf 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 ount(pBt) ){.
1d5c0 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 rc = ptrmapGe
1d5c1 74 28 70 42 74 2c 20 69 47 75 65 73 73 2c 20 26 t(pBt, iGuess, &
1d5c2 65 54 79 70 65 2c 20 26 70 67 6e 6f 29 3b 0a 20 eType, &pgno);.
1d5c3 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
1d5c4 49 54 45 5f 4f 4b 20 26 26 20 65 54 79 70 65 3d ITE_OK && eType=
1d5c5 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 =PTRMAP_OVERFLOW
1d5c6 32 20 26 26 20 70 67 6e 6f 3d 3d 6f 76 66 6c 20 2 && pgno==ovfl
1d5c7 29 7b 0a 20 20 20 20 20 20 20 20 6e 65 78 74 20 ){. next
1d5c8 3d 20 69 47 75 65 73 73 3b 0a 20 20 20 20 20 20 = iGuess;.
1d5c9 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f rc = SQLITE_DO
1d5ca 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 NE;. }.
1d5cb 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }. }.#endif..
1d5cc 61 73 73 65 72 74 28 20 6e 65 78 74 3d 3d 30 20 assert( next==0
1d5cd 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f || rc==SQLITE_DO
1d5ce 4e 45 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d NE );. if( rc==
1d5cf 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1d5d0 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 rc = btreeGetPa
1d5d1 67 65 28 70 42 74 2c 20 6f 76 66 6c 2c 20 26 70 ge(pBt, ovfl, &p
1d5d2 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 61 73 Page, 0);. as
1d5d3 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
1d5d4 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 3d 3d 30 20 _OK || pPage==0
1d5d5 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
1d5d6 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1d5d7 20 20 6e 65 78 74 20 3d 20 67 65 74 34 62 79 74 next = get4byt
1d5d8 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 3b e(pPage->aData);
1d5d9 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 . }. }.. *p
1d5da 50 67 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74 3b PgnoNext = next;
1d5db 0a 20 20 69 66 28 20 70 70 50 61 67 65 20 29 7b . if( ppPage ){
1d5dc 0a 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 . *ppPage = p
1d5dd 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Page;. }else{.
1d5de 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1d5df 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Page);. }. ret
1d5e0 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f urn (rc==SQLITE_
1d5e1 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b DONE ? SQLITE_OK
1d5e2 20 3a 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a : rc);.}../*.**
1d5e3 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 Copy data from
1d5e4 61 20 62 75 66 66 65 72 20 74 6f 20 61 20 70 61 a buffer to a pa
1d5e5 67 65 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 61 ge, or from a pa
1d5e6 67 65 20 74 6f 20 61 20 62 75 66 66 65 72 2e 0a ge to a buffer..
1d5e7 2a 2a 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 69 **.** pPayload i
1d5e8 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 64 s a pointer to d
1d5e9 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 61 ata stored on da
1d5ea 74 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 tabase page pDbP
1d5eb 61 67 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 6d age..** If argum
1d5ec 65 6e 74 20 65 4f 70 20 69 73 20 66 61 6c 73 65 ent eOp is false
1d5ed 2c 20 74 68 65 6e 20 6e 42 79 74 65 20 62 79 74 , then nByte byt
1d5ee 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 es of data are c
1d5ef 6f 70 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 opied.** from pP
1d5f0 61 79 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 75 ayload to the bu
1d5f1 66 66 65 72 20 70 6f 69 6e 74 65 64 20 61 74 20 ffer pointed at
1d5f2 62 79 20 70 42 75 66 2e 20 49 66 20 65 4f 70 20 by pBuf. If eOp
1d5f3 69 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e is true,.** then
1d5f4 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
1d5f5 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f te() is called o
1d5f6 6e 20 70 44 62 50 61 67 65 20 61 6e 64 20 6e 42 n pDbPage and nB
1d5f7 79 74 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 yte bytes.** of
1d5f8 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 data are copied
1d5f9 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 from the buffer
1d5fa 70 42 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 64 pBuf to pPayload
1d5fb 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f ..**.** SQLITE_O
1d5fc 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e K is returned on
1d5fd 20 73 75 63 63 65 73 73 2c 20 6f 74 68 65 72 77 success, otherw
1d5fe 69 73 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 ise an error cod
1d5ff 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1d600 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 20 copyPayload(.
1d601 76 6f 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20 void *pPayload,
1d602 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
1d603 6e 74 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 nter to page dat
1d604 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 a */. void *pBu
1d605 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f,
1d606 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 /* Pointer to b
1d607 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e uffer */. int n
1d608 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 Byte,
1d609 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1d60a 66 20 62 79 74 65 73 20 74 6f 20 63 6f 70 79 20 f bytes to copy
1d60b 2a 2f 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 20 */. int eOp,
1d60c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1d60d 2a 20 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f 6d * 0 -> copy from
1d60e 20 70 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 79 page, 1 -> copy
1d60f 20 74 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 62 to page */. Db
1d610 50 61 67 65 20 2a 70 44 62 50 61 67 65 20 20 20 Page *pDbPage
1d611 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
1d612 63 6f 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 6c containing pPayl
1d613 6f 61 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 oad */.){. if(
1d614 65 4f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f eOp ){. /* Co
1d615 70 79 20 64 61 74 61 20 66 72 6f 6d 20 62 75 66 py data from buf
1d616 66 65 72 20 74 6f 20 70 61 67 65 20 28 61 20 77 fer to page (a w
1d617 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 20 rite operation)
1d618 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 */. int rc =
1d619 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
1d61a 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 e(pDbPage);.
1d61b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1d61c 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
1d61d 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
1d61e 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c memcpy(pPayload,
1d61f 20 70 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a 20 pBuf, nByte);.
1d620 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 }else{. /* C
1d621 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 70 61 opy data from pa
1d622 67 65 20 74 6f 20 62 75 66 66 65 72 20 28 61 20 ge to buffer (a
1d623 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 20 read operation)
1d624 2a 2f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 */. memcpy(pB
1d625 75 66 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42 uf, pPayload, nB
1d626 79 74 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 yte);. }. retu
1d627 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1d628 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
1d629 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 tion is used to
1d62a 72 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 read or overwrit
1d62b 65 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d e payload inform
1d62c 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 ation.** for the
1d62d 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 entry that the
1d62e 70 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 pCur cursor is p
1d62f 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 49 66 20 74 ointing to. If t
1d630 68 65 20 65 4f 70 0a 2a 2a 20 70 61 72 61 6d 65 he eOp.** parame
1d631 74 65 72 20 69 73 20 30 2c 20 74 68 69 73 20 69 ter is 0, this i
1d632 73 20 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 s a read operati
1d633 6f 6e 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 on (data copied
1d634 69 6e 74 6f 0a 2a 2a 20 62 75 66 66 65 72 20 70 into.** buffer p
1d635 42 75 66 29 2e 20 49 66 20 69 74 20 69 73 20 6e Buf). If it is n
1d636 6f 6e 2d 7a 65 72 6f 2c 20 61 20 77 72 69 74 65 on-zero, a write
1d637 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 (data copied fr
1d638 6f 6d 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 om.** buffer pBu
1d639 66 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 f)..**.** A tota
1d63a 6c 20 6f 66 20 22 61 6d 74 22 20 62 79 74 65 73 l of "amt" bytes
1d63b 20 61 72 65 20 72 65 61 64 20 6f 72 20 77 72 69 are read or wri
1d63c 74 74 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20 61 tten beginning a
1d63d 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 44 t "offset"..** D
1d63e 61 74 61 20 69 73 20 72 65 61 64 20 74 6f 20 6f ata is read to o
1d63f 72 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 r from the buffe
1d640 72 20 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 r pBuf..**.** Th
1d641 65 20 63 6f 6e 74 65 6e 74 20 62 65 69 6e 67 20 e content being
1d642 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 20 read or written
1d643 6d 69 67 68 74 20 61 70 70 65 61 72 20 6f 6e 20 might appear on
1d644 74 68 65 20 6d 61 69 6e 20 70 61 67 65 0a 2a 2a the main page.**
1d645 20 6f 72 20 62 65 20 73 63 61 74 74 65 72 65 64 or be scattered
1d646 20 6f 75 74 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 out on multiple
1d647 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
1d648 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 74 .**.** If the Bt
1d649 43 75 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c 6f Cursor.isIncrblo
1d64a 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 bHandle flag is
1d64b 73 65 74 2c 20 61 6e 64 20 74 68 65 20 63 75 72 set, and the cur
1d64c 72 65 6e 74 0a 2a 2a 20 63 75 72 73 6f 72 20 65 rent.** cursor e
1d64d 6e 74 72 79 20 75 73 65 73 20 6f 6e 65 20 6f 72 ntry uses one or
1d64e 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 more overflow p
1d64f 61 67 65 73 2c 20 74 68 69 73 20 66 75 6e 63 74 ages, this funct
1d650 69 6f 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 73 ion.** allocates
1d651 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 6c space for and l
1d652 61 7a 69 6c 79 20 70 6f 70 6c 75 61 74 65 73 20 azily popluates
1d653 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 the overflow pag
1d654 65 2d 6c 69 73 74 20 0a 2a 2a 20 63 61 63 68 65 e-list .** cache
1d655 20 61 72 72 61 79 20 28 42 74 43 75 72 73 6f 72 array (BtCursor
1d656 2e 61 4f 76 65 72 66 6c 6f 77 29 2e 20 53 75 62 .aOverflow). Sub
1d657 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 73 sequent calls us
1d658 65 20 74 68 69 73 0a 2a 2a 20 63 61 63 68 65 20 e this.** cache
1d659 74 6f 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67 20 to make seeking
1d65a 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 to the supplied
1d65b 6f 66 66 73 65 74 20 6d 6f 72 65 20 65 66 66 69 offset more effi
1d65c 63 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 cient..**.** Onc
1d65d 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 e an overflow pa
1d65e 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 ge-list cache ha
1d65f 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 s been allocated
1d660 2c 20 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 69 , it may be.** i
1d661 6e 76 61 6c 69 64 61 74 65 64 20 69 66 20 73 6f nvalidated if so
1d662 6d 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 20 me other cursor
1d663 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 73 61 writes to the sa
1d664 6d 65 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 0a me table, or if.
1d665 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 ** the cursor is
1d666 20 6d 6f 76 65 64 20 74 6f 20 61 20 64 69 66 66 moved to a diff
1d667 65 72 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 74 erent row. Addit
1d668 69 6f 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 6f ionally, in auto
1d669 2d 76 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c -vacuum.** mode,
1d66a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 the following e
1d66b 76 65 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c 69 vents may invali
1d66c 64 61 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 date an overflow
1d66d 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 page-list cache
1d66e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 ..**.** * An i
1d66f 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 ncremental vacuu
1d670 6d 2c 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d m,.** * A comm
1d671 69 74 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 75 it in auto_vacuu
1d672 6d 3d 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a m="full" mode,.*
1d673 2a 20 20 20 2a 20 43 72 65 61 74 69 6e 67 20 61 * * Creating a
1d674 20 74 61 62 6c 65 20 28 6d 61 79 20 72 65 71 75 table (may requ
1d675 69 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 ire moving an ov
1d676 65 72 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a 2f erflow page)..*/
1d677 0a 73 74 61 74 69 63 20 69 6e 74 20 61 63 63 65 .static int acce
1d678 73 73 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 ssPayload(. BtC
1d679 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 ursor *pCur,
1d67a 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e /* Cursor poin
1d67b 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f ting to entry to
1d67c 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 read from */.
1d67d 75 33 32 20 6f 66 66 73 65 74 2c 20 20 20 20 20 u32 offset,
1d67e 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 /* Begin re
1d67f 61 64 69 6e 67 20 74 68 69 73 20 66 61 72 20 69 ading this far i
1d680 6e 74 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 nto payload */.
1d681 20 75 33 32 20 61 6d 74 2c 20 20 20 20 20 20 20 u32 amt,
1d682 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 /* Read th
1d683 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 2a 2f is many bytes */
1d684 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
1d685 20 2a 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 65 *pBuf, /* Write
1d686 20 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 the bytes into
1d687 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 20 0a this buffer */ .
1d688 20 20 69 6e 74 20 65 4f 70 20 20 20 20 20 20 20 int eOp
1d689 20 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20 74 /* zero t
1d68a 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72 6f o read. non-zero
1d68b 20 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a 29 7b to write. */.){
1d68c 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
1d68d 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 69 6e *aPayload;. in
1d68e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
1d68f 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 ;. u32 nKey;.
1d690 69 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 int iIdx = 0;.
1d691 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d MemPage *pPage =
1d692 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
1d693 75 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 42 ur->iPage]; /* B
1d694 74 72 65 65 20 70 61 67 65 20 6f 66 20 63 75 72 tree page of cur
1d695 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 rent entry */.
1d696 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
1d697 70 43 75 72 2d 3e 70 42 74 3b 20 20 20 20 20 20 pCur->pBt;
1d698 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
1d699 74 72 65 65 20 74 68 69 73 20 63 75 72 73 6f 72 tree this cursor
1d69a 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 0a belongs to */..
1d69b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 20 assert( pPage
1d69c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
1d69d 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
1d69e 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 R_VALID );. ass
1d69f 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
1d6a0 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 [pCur->iPage]<pP
1d6a1 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 age->nCell );.
1d6a2 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
1d6a3 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
1d6a4 3b 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f ;.. getCellInfo
1d6a5 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79 6c 6f (pCur);. aPaylo
1d6a6 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e ad = pCur->info.
1d6a7 70 43 65 6c 6c 20 2b 20 70 43 75 72 2d 3e 69 6e pCell + pCur->in
1d6a8 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 6e 4b fo.nHeader;. nK
1d6a9 65 79 20 3d 20 28 70 50 61 67 65 2d 3e 69 6e 74 ey = (pPage->int
1d6aa 4b 65 79 20 3f 20 30 20 3a 20 28 69 6e 74 29 70 Key ? 0 : (int)p
1d6ab 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b Cur->info.nKey);
1d6ac 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6f 66 .. if( NEVER(of
1d6ad 66 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65 79 2b fset+amt > nKey+
1d6ae 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61 pCur->info.nData
1d6af 29 20 0a 20 20 20 7c 7c 20 26 61 50 61 79 6c 6f ) . || &aPaylo
1d6b0 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c ad[pCur->info.nL
1d6b1 6f 63 61 6c 5d 20 3e 20 26 70 50 61 67 65 2d 3e ocal] > &pPage->
1d6b2 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c aData[pBt->usabl
1d6b3 65 53 69 7a 65 5d 0a 20 20 29 7b 0a 20 20 20 20 eSize]. ){.
1d6b4 2f 2a 20 54 72 79 69 6e 67 20 74 6f 20 72 65 61 /* Trying to rea
1d6b5 64 20 6f 72 20 77 72 69 74 65 20 70 61 73 74 20 d or write past
1d6b6 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 the end of the d
1d6b7 61 74 61 20 69 73 20 61 6e 20 65 72 72 6f 72 20 ata is an error
1d6b8 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 */. return SQ
1d6b9 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
1d6ba 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 T;. }.. /* Che
1d6bb 63 6b 20 69 66 20 64 61 74 61 20 6d 75 73 74 20 ck if data must
1d6bc 62 65 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 be read/written
1d6bd 74 6f 2f 66 72 6f 6d 20 74 68 65 20 62 74 72 65 to/from the btre
1d6be 65 20 70 61 67 65 20 69 74 73 65 6c 66 2e 20 2a e page itself. *
1d6bf 2f 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c 70 /. if( offset<p
1d6c0 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c Cur->info.nLocal
1d6c1 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d 20 ){. int a =
1d6c2 61 6d 74 3b 0a 20 20 20 20 69 66 28 20 61 2b 6f amt;. if( a+o
1d6c3 66 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66 6f ffset>pCur->info
1d6c4 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 .nLocal ){.
1d6c5 20 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e a = pCur->info.
1d6c6 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74 3b nLocal - offset;
1d6c7 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
1d6c8 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 copyPayload(&aPa
1d6c9 79 6c 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20 70 yload[offset], p
1d6ca 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50 61 Buf, a, eOp, pPa
1d6cb 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ge->pDbPage);.
1d6cc 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 offset = 0;.
1d6cd 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 pBuf += a;.
1d6ce 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65 6c amt -= a;. }el
1d6cf 73 65 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2d se{. offset -
1d6d0 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f = pCur->info.nLo
1d6d1 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 cal;. }.. if(
1d6d2 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1d6d3 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63 6f amt>0 ){. co
1d6d4 6e 73 74 20 75 33 32 20 6f 76 66 6c 53 69 7a 65 nst u32 ovflSize
1d6d5 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = pBt->usableSi
1d6d6 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74 65 ze - 4; /* Byte
1d6d7 73 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f 76 s content per ov
1d6d8 66 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 50 fl page */. P
1d6d9 67 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a 20 gno nextPage;..
1d6da 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65 nextPage = ge
1d6db 74 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61 64 t4byte(&aPayload
1d6dc 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 [pCur->info.nLoc
1d6dd 61 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 al]);..#ifndef S
1d6de 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
1d6df 4c 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 LOB. /* If th
1d6e0 65 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 e isIncrblobHand
1d6e1 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 le flag is set a
1d6e2 6e 64 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e nd the BtCursor.
1d6e3 61 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20 20 aOverflow[].
1d6e4 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 ** has not been
1d6e5 61 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f 63 allocated, alloc
1d6e6 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 ate it now. The
1d6e7 61 72 72 61 79 20 69 73 20 73 69 7a 65 64 20 61 array is sized a
1d6e8 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e 74 t. ** one ent
1d6e9 72 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65 72 ry for each over
1d6ea 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 flow page in the
1d6eb 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e overflow chain.
1d6ec 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 The. ** page
1d6ed 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 66 number of the f
1d6ee 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 irst overflow pa
1d6ef 67 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 ge is stored in
1d6f0 61 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20 20 aOverflow[0],.
1d6f1 20 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c 75 ** etc. A valu
1d6f2 65 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61 4f e of 0 in the aO
1d6f3 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 20 verflow[] array
1d6f4 6d 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20 6b means "not yet k
1d6f5 6e 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74 68 nown". ** (th
1d6f6 65 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69 6c e cache is lazil
1d6f7 79 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20 20 y populated)..
1d6f8 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 */. if( pCu
1d6f9 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e r->isIncrblobHan
1d6fa 64 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61 4f dle && !pCur->aO
1d6fb 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 verflow ){.
1d6fc 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70 43 int nOvfl = (pC
1d6fd 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 ur->info.nPayloa
1d6fe 64 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f d-pCur->info.nLo
1d6ff 63 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29 2f cal+ovflSize-1)/
1d700 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 ovflSize;.
1d701 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 pCur->aOverflow
1d702 3d 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74 65 = (Pgno *)sqlite
1d703 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 3MallocZero(size
1d704 6f 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c 29 3b of(Pgno)*nOvfl);
1d705 0a 20 20 20 20 20 20 2f 2a 20 6e 4f 76 66 6c 20 . /* nOvfl
1d706 69 73 20 61 6c 77 61 79 73 20 70 6f 73 69 74 69 is always positi
1d707 76 65 2e 20 20 49 66 20 69 74 20 77 65 72 65 20 ve. If it were
1d708 7a 65 72 6f 2c 20 66 65 74 63 68 50 61 79 6c 6f zero, fetchPaylo
1d709 61 64 20 77 6f 75 6c 64 20 68 61 76 65 0a 20 20 ad would have.
1d70a 20 20 20 20 2a 2a 20 62 65 65 6e 20 75 73 65 64 ** been used
1d70b 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 69 73 instead of this
1d70c 20 72 6f 75 74 69 6e 65 2e 20 2a 2f 0a 20 20 20 routine. */.
1d70d 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f if( ALWAYS(nO
1d70e 76 66 6c 29 20 26 26 20 21 70 43 75 72 2d 3e 61 vfl) && !pCur->a
1d70f 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 Overflow ){.
1d710 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1d711 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 NOMEM;. }.
1d712 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
1d713 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 the overflow pag
1d714 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73 e-list cache has
1d715 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 been allocated
1d716 61 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 and the. ** e
1d717 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 72 ntry for the fir
1d718 73 74 20 72 65 71 75 69 72 65 64 20 6f 76 65 72 st required over
1d719 66 6c 6f 77 20 70 61 67 65 20 69 73 20 76 61 6c flow page is val
1d71a 69 64 2c 20 73 6b 69 70 0a 20 20 20 20 2a 2a 20 id, skip. **
1d71b 64 69 72 65 63 74 6c 79 20 74 6f 20 69 74 2e 0a directly to it..
1d71c 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
1d71d 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 Cur->aOverflow &
1d71e 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f & pCur->aOverflo
1d71f 77 5b 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a w[offset/ovflSiz
1d720 65 5d 20 29 7b 0a 20 20 20 20 20 20 69 49 64 78 e] ){. iIdx
1d721 20 3d 20 28 6f 66 66 73 65 74 2f 6f 76 66 6c 53 = (offset/ovflS
1d722 69 7a 65 29 3b 0a 20 20 20 20 20 20 6e 65 78 74 ize);. next
1d723 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 Page = pCur->aOv
1d724 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3b 0a 20 20 erflow[iIdx];.
1d725 20 20 20 20 6f 66 66 73 65 74 20 3d 20 28 6f 66 offset = (of
1d726 66 73 65 74 25 6f 76 66 6c 53 69 7a 65 29 3b 0a fset%ovflSize);.
1d727 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }.#endif..
1d728 20 20 66 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c for( ; rc==SQL
1d729 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 ITE_OK && amt>0
1d72a 26 26 20 6e 65 78 74 50 61 67 65 3b 20 69 49 64 && nextPage; iId
1d72b 78 2b 2b 29 7b 0a 0a 23 69 66 6e 64 65 66 20 53 x++){..#ifndef S
1d72c 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
1d72d 4c 4f 42 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 LOB. /* If
1d72e 72 65 71 75 69 72 65 64 2c 20 70 6f 70 75 6c 61 required, popula
1d72f 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 te the overflow
1d730 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e page-list cache.
1d731 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43 */. if( pC
1d732 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b ur->aOverflow ){
1d733 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1d734 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 !pCur->aOverflow
1d735 5b 69 49 64 78 5d 20 7c 7c 20 70 43 75 72 2d 3e [iIdx] || pCur->
1d736 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3d aOverflow[iIdx]=
1d737 3d 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 =nextPage);.
1d738 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 pCur->aOverf
1d739 6c 6f 77 5b 69 49 64 78 5d 20 3d 20 6e 65 78 74 low[iIdx] = next
1d73a 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 23 65 Page;. }.#e
1d73b 6e 64 69 66 0a 0a 20 20 20 20 20 20 69 66 28 20 ndif.. if(
1d73c 6f 66 66 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 offset>=ovflSize
1d73d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 ){. /* T
1d73e 68 65 20 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 he only reason t
1d73f 6f 20 72 65 61 64 20 74 68 69 73 20 70 61 67 65 o read this page
1d740 20 69 73 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 is to obtain th
1d741 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a e page. *
1d742 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 * number for the
1d743 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 next page in th
1d744 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e e overflow chain
1d745 2e 20 54 68 65 20 70 61 67 65 0a 20 20 20 20 20 . The page.
1d746 20 20 20 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f ** data is no
1d747 74 20 72 65 71 75 69 72 65 64 2e 20 53 6f 20 66 t required. So f
1d748 69 72 73 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b irst try to look
1d749 75 70 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a up the overflow.
1d74a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 2d ** page-
1d74b 6c 69 73 74 20 63 61 63 68 65 2c 20 69 66 20 61 list cache, if a
1d74c 6e 79 2c 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 ny, then fall ba
1d74d 63 6b 20 74 6f 20 74 68 65 20 67 65 74 4f 76 65 ck to the getOve
1d74e 72 66 6c 6f 77 50 61 67 65 28 29 0a 20 20 20 20 rflowPage().
1d74f 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e ** function.
1d750 0a 20 20 20 20 20 20 20 20 2a 2f 0a 23 69 66 6e . */.#ifn
1d751 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1d752 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 20 20 INCRBLOB.
1d753 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 if( pCur->aOver
1d754 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f flow && pCur->aO
1d755 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 verflow[iIdx+1]
1d756 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 ){. nex
1d757 74 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f tPage = pCur->aO
1d758 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b verflow[iIdx+1];
1d759 0a 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 . } else
1d75a 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 .#endif.
1d75b 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72 66 6c rc = getOverfl
1d75c 6f 77 50 61 67 65 28 70 42 74 2c 20 6e 65 78 74 owPage(pBt, next
1d75d 50 61 67 65 2c 20 30 2c 20 26 6e 65 78 74 50 61 Page, 0, &nextPa
1d75e 67 65 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 ge);. off
1d75f 73 65 74 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b set -= ovflSize;
1d760 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1d761 20 20 20 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f /* Need to
1d762 20 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20 read this page
1d763 70 72 6f 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e properly. It con
1d764 74 61 69 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68 tains some of th
1d765 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 61 6e e. ** ran
1d766 67 65 20 6f 66 20 64 61 74 61 20 74 68 61 74 20 ge of data that
1d767 69 73 20 62 65 69 6e 67 20 72 65 61 64 20 28 65 is being read (e
1d768 4f 70 3d 3d 30 29 20 6f 72 20 77 72 69 74 74 65 Op==0) or writte
1d769 6e 20 28 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20 n (eOp!=0)..
1d76a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 */.#ifdef SQ
1d76b 4c 49 54 45 5f 44 49 52 45 43 54 5f 4f 56 45 52 LITE_DIRECT_OVER
1d76c 46 4c 4f 57 5f 52 45 41 44 0a 20 20 20 20 20 20 FLOW_READ.
1d76d 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1d76e 66 64 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 fd;.#endif.
1d76f 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a int a = amt;.
1d770 20 20 20 20 20 20 20 20 69 66 28 20 61 20 2b 20 if( a +
1d771 6f 66 66 73 65 74 20 3e 20 6f 76 66 6c 53 69 7a offset > ovflSiz
1d772 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 e ){. a
1d773 20 3d 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f 66 = ovflSize - of
1d774 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a fset;. }.
1d775 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1d776 49 52 45 43 54 5f 4f 56 45 52 46 4c 4f 57 5f 52 IRECT_OVERFLOW_R
1d777 45 41 44 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 EAD. /* I
1d778 66 20 61 6c 6c 20 74 68 65 20 66 6f 6c 6c 6f 77 f all the follow
1d779 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 20 20 ing are true:.
1d77a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
1d77b 20 2a 2a 20 20 20 31 29 20 74 68 69 73 20 69 73 ** 1) this is
1d77c 20 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f a read operatio
1d77d 6e 2c 20 61 6e 64 20 0a 20 20 20 20 20 20 20 20 n, and .
1d77e 2a 2a 20 20 20 32 29 20 64 61 74 61 20 69 73 20 ** 2) data is
1d77f 72 65 71 75 69 72 65 64 20 66 72 6f 6d 20 74 68 required from th
1d780 65 20 73 74 61 72 74 20 6f 66 20 74 68 69 73 20 e start of this
1d781 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 61 overflow page, a
1d782 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 nd. **
1d783 33 29 20 74 68 65 20 64 61 74 61 62 61 73 65 20 3) the database
1d784 69 73 20 66 69 6c 65 2d 62 61 63 6b 65 64 2c 20 is file-backed,
1d785 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 and. **
1d786 20 34 29 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4) there is no
1d787 6f 70 65 6e 20 77 72 69 74 65 2d 74 72 61 6e 73 open write-trans
1d788 61 63 74 69 6f 6e 2c 20 61 6e 64 0a 20 20 20 20 action, and.
1d789 20 20 20 20 2a 2a 20 20 20 35 29 20 74 68 65 20 ** 5) the
1d78a 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 database is not
1d78b 61 20 57 41 4c 20 64 61 74 61 62 61 73 65 2c 0a a WAL database,.
1d78c 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
1d78d 20 20 20 2a 2a 20 74 68 65 6e 20 64 61 74 61 20 ** then data
1d78e 63 61 6e 20 62 65 20 72 65 61 64 20 64 69 72 65 can be read dire
1d78f 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 64 61 ctly from the da
1d790 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 74 6f tabase file into
1d791 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 the. **
1d792 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c 20 62 output buffer, b
1d793 79 70 61 73 73 69 6e 67 20 74 68 65 20 70 61 67 ypassing the pag
1d794 65 2d 63 61 63 68 65 20 61 6c 74 6f 67 65 74 68 e-cache altogeth
1d795 65 72 2e 20 54 68 69 73 20 73 70 65 65 64 73 0a er. This speeds.
1d796 20 20 20 20 20 20 20 20 2a 2a 20 75 70 20 6c 6f ** up lo
1d797 61 64 69 6e 67 20 6c 61 72 67 65 20 72 65 63 6f ading large reco
1d798 72 64 73 20 74 68 61 74 20 73 70 61 6e 20 6d 61 rds that span ma
1d799 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 ny overflow page
1d79a 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 s.. */.
1d79b 20 20 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 30 if( eOp==0
1d79c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d79d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d79e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d79f 28 31 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 (1) */.
1d7a0 26 26 20 6f 66 66 73 65 74 3d 3d 30 20 20 20 20 && offset==0
1d7a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d7a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d7a3 20 20 20 20 20 20 2f 2a 20 28 32 29 20 2a 2f 0a /* (2) */.
1d7a4 20 20 20 20 20 20 20 20 20 26 26 20 70 42 74 2d && pBt-
1d7a5 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d >inTransaction==
1d7a6 54 52 41 4e 53 5f 52 45 41 44 20 20 20 20 20 20 TRANS_READ
1d7a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1d7a8 2a 20 28 34 29 20 2a 2f 0a 20 20 20 20 20 20 20 * (4) */.
1d7a9 20 20 26 26 20 28 66 64 20 3d 20 73 71 6c 69 74 && (fd = sqlit
1d7aa 65 33 50 61 67 65 72 46 69 6c 65 28 70 42 74 2d e3PagerFile(pBt-
1d7ab 3e 70 50 61 67 65 72 29 29 2d 3e 70 4d 65 74 68 >pPager))->pMeth
1d7ac 6f 64 73 20 20 20 20 20 2f 2a 20 28 33 29 20 2a ods /* (3) *
1d7ad 2f 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 42 /. && pB
1d7ae 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
1d7af 5b 31 39 5d 3d 3d 30 78 30 31 20 20 20 20 20 20 [19]==0x01
1d7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d7b1 20 2f 2a 20 28 35 29 20 2a 2f 0a 20 20 20 20 20 /* (5) */.
1d7b2 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ){.
1d7b3 75 38 20 61 53 61 76 65 5b 34 5d 3b 0a 20 20 20 u8 aSave[4];.
1d7b4 20 20 20 20 20 20 20 75 38 20 2a 61 57 72 69 74 u8 *aWrit
1d7b5 65 20 3d 20 26 70 42 75 66 5b 2d 34 5d 3b 0a 20 e = &pBuf[-4];.
1d7b6 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
1d7b7 61 53 61 76 65 2c 20 61 57 72 69 74 65 2c 20 34 aSave, aWrite, 4
1d7b8 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 );. rc
1d7b9 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
1d7ba 66 64 2c 20 61 57 72 69 74 65 2c 20 61 2b 34 2c fd, aWrite, a+4,
1d7bb 20 28 69 36 34 29 70 42 74 2d 3e 70 61 67 65 53 (i64)pBt->pageS
1d7bc 69 7a 65 2a 28 6e 65 78 74 50 61 67 65 2d 31 29 ize*(nextPage-1)
1d7bd 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 );. nex
1d7be 74 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 tPage = get4byte
1d7bf 28 61 57 72 69 74 65 29 3b 0a 20 20 20 20 20 20 (aWrite);.
1d7c0 20 20 20 20 6d 65 6d 63 70 79 28 61 57 72 69 74 memcpy(aWrit
1d7c1 65 2c 20 61 53 61 76 65 2c 20 34 29 3b 0a 20 20 e, aSave, 4);.
1d7c2 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 }else.#end
1d7c3 69 66 0a 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 if.. {.
1d7c4 20 20 20 20 20 20 20 20 44 62 50 61 67 65 20 2a DbPage *
1d7c5 70 44 62 50 61 67 65 3b 0a 20 20 20 20 20 20 20 pDbPage;.
1d7c6 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1d7c7 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 agerGet(pBt->pPa
1d7c8 67 65 72 2c 20 6e 65 78 74 50 61 67 65 2c 20 26 ger, nextPage, &
1d7c9 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
1d7ca 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1d7cb 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1d7cc 20 20 20 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 aPayload =
1d7cd 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
1d7ce 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 ata(pDbPage);.
1d7cf 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 nextPa
1d7d0 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 61 50 ge = get4byte(aP
1d7d1 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 20 ayload);.
1d7d2 20 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 rc = copyPa
1d7d3 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b yload(&aPayload[
1d7d4 6f 66 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c offset+4], pBuf,
1d7d5 20 61 2c 20 65 4f 70 2c 20 70 44 62 50 61 67 65 a, eOp, pDbPage
1d7d6 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 );. s
1d7d7 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
1d7d8 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 (pDbPage);.
1d7d9 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20 offset =
1d7da 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 0;. }.
1d7db 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1d7dc 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 20 20 20 amt -= a;.
1d7dd 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 pBuf += a;.
1d7de 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1d7df 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1d7e0 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b E_OK && amt>0 ){
1d7e1 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1d7e2 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1d7e3 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1d7e4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
1d7e5 70 61 72 74 20 6f 66 20 74 68 65 20 6b 65 79 20 part of the key
1d7e6 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1d7e7 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45 78 cursor pCur. Ex
1d7e8 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 62 actly.** "amt" b
1d7e9 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72 61 ytes will be tra
1d7ea 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42 75 nsfered into pBu
1d7eb 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66 f[]. The transf
1d7ec 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20 er.** begins at
1d7ed 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 "offset"..**.**
1d7ee 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 The caller must
1d7ef 65 6e 73 75 72 65 20 74 68 61 74 20 70 43 75 72 ensure that pCur
1d7f0 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 is pointing to
1d7f1 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a 2a 20 69 a valid row.** i
1d7f2 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a n the table..**.
1d7f3 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ** Return SQLITE
1d7f4 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f _OK on success o
1d7f5 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 r an error code
1d7f6 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 if anything goes
1d7f7 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 .** wrong. An e
1d7f8 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 rror is returned
1d7f9 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 if "offset+amt"
1d7fa 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a is larger than.
1d7fb 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 ** the available
1d7fc 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c payload..*/.SQL
1d7fd 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1d7fe 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 sqlite3BtreeKey(
1d7ff 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
1d800 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 u32 offset, u32
1d801 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 amt, void *pBuf)
1d802 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 {. assert( curs
1d803 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
1d804 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r) );. assert(
1d805 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
1d806 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
1d807 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 assert( pCur->iP
1d808 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e age>=0 && pCur->
1d809 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1d80a 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge] );. assert(
1d80b 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
1d80c 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e r->iPage]<pCur->
1d80d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1d80e 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 ge]->nCell );.
1d80f 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 79 return accessPay
1d810 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 65 load(pCur, offse
1d811 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 t, amt, (unsigne
1d812 64 20 63 68 61 72 2a 29 70 42 75 66 2c 20 30 29 d char*)pBuf, 0)
1d813 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
1d814 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 part of the data
1d815 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1d816 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45 cursor pCur. E
1d817 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 xactly.** "amt"
1d818 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72 bytes will be tr
1d819 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42 ansfered into pB
1d81a 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 uf[]. The trans
1d81b 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 fer.** begins at
1d81c 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a "offset"..**.**
1d81d 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
1d81e 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 K on success or
1d81f 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 an error code if
1d820 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a anything goes.*
1d821 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72 * wrong. An err
1d822 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 or is returned i
1d823 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 69 f "offset+amt" i
1d824 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a s larger than.**
1d825 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 70 the available p
1d826 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54 ayload..*/.SQLIT
1d827 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1d828 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 42 lite3BtreeData(B
1d829 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 tCursor *pCur, u
1d82a 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 32 offset, u32 a
1d82b 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b mt, void *pBuf){
1d82c 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 6e . int rc;..#ifn
1d82d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1d82e 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28 20 INCRBLOB. if (
1d82f 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
1d830 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a RSOR_INVALID ){.
1d831 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1d832 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65 6e E_ABORT;. }.#en
1d833 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 63 dif.. assert( c
1d834 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
1d835 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 pCur) );. rc =
1d836 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 restoreCursorPos
1d837 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 ition(pCur);. i
1d838 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1d839 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1d83a 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
1d83b 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
1d83c 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1d83d 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 iPage>=0 && pCur
1d83e 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
1d83f 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73 73 Page] );. ass
1d840 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
1d841 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 [pCur->iPage]<pC
1d842 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
1d843 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 >iPage]->nCell )
1d844 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 ;. rc = acces
1d845 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f sPayload(pCur, o
1d846 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 66 ffset, amt, pBuf
1d847 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , 0);. }. retu
1d848 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1d849 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
1d84a 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f to payload info
1d84b 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 rmation from the
1d84c 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 entry that the
1d84d 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 6f 72 20 .** pCur cursor
1d84e 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 is pointing to.
1d84f 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 The pointer is
1d850 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 to the beginning
1d851 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 69 of.** the key i
1d852 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 61 6e 64 f skipKey==0 and
1d853 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 it points to th
1d854 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 64 e beginning of d
1d855 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 70 4b 65 ata if.** skipKe
1d856 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 6d 62 65 y==1. The numbe
1d857 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 61 76 r of bytes of av
1d858 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 61 74 61 ailable key/data
1d859 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 is written.** i
1d85a 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 66 20 2a nto *pAmt. If *
1d85b 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e 20 74 68 pAmt==0, then th
1d85c 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
1d85d 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a 2a 20 will not be.**
1d85e 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e a valid pointer.
1d85f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
1d860 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 ine is an optimi
1d861 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 73 20 63 zation. It is c
1d862 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 20 65 6e ommon for the en
1d863 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 6e 64 20 tire key.** and
1d864 64 61 74 61 20 74 6f 20 66 69 74 20 6f 6e 20 74 data to fit on t
1d865 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 20 61 6e he local page an
1d866 64 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 d for there to b
1d867 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a e no overflow.**
1d868 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20 74 68 pages. When th
1d869 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73 20 72 at is so, this r
1d86a 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 outine can be us
1d86b 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 ed to access the
1d86c 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 61 .** key and data
1d86d 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e 67 20 without making
1d86e 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68 65 20 a copy. If the
1d86f 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74 61 20 key and/or data
1d870 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f 20 6f spills.** onto o
1d871 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 verflow pages, t
1d872 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 hen accessPayloa
1d873 64 28 29 20 6d 75 73 74 20 62 65 20 75 73 65 64 d() must be used
1d874 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 65 0a 2a to reassemble.*
1d875 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 61 20 61 * the key/data a
1d876 6e 64 20 63 6f 70 79 20 69 74 20 69 6e 74 6f 20 nd copy it into
1d877 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 20 62 a preallocated b
1d878 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 uffer..**.** The
1d879 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 pointer returne
1d87a 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e d by this routin
1d87b 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 74 6c 79 e looks directly
1d87c 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65 64 into the cached
1d87d 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 68 65 20 .** page of the
1d87e 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 64 database. The d
1d87f 61 74 61 20 6d 69 67 68 74 20 63 68 61 6e 67 65 ata might change
1d880 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 6e 65 78 or move the nex
1d881 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 20 62 74 t time.** any bt
1d882 72 65 65 20 72 6f 75 74 69 6e 65 20 69 73 20 63 ree routine is c
1d883 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 alled..*/.static
1d884 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
1d885 63 68 61 72 20 2a 66 65 74 63 68 50 61 79 6c 6f char *fetchPaylo
1d886 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a ad(. BtCursor *
1d887 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 pCur, /* Cu
1d888 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f rsor pointing to
1d889 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 entry to read f
1d88a 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 rom */. int *pA
1d88b 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a mt, /*
1d88c 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 Write the numbe
1d88d 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 r of available b
1d88e 79 74 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 ytes here */. i
1d88f 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 20 20 20 nt skipKey
1d890 20 20 20 20 2f 2a 20 72 65 61 64 20 62 65 67 69 /* read begi
1d891 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 20 69 66 nning at data if
1d892 20 74 68 69 73 20 69 73 20 74 72 75 65 20 2a 2f this is true */
1d893 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 .){. unsigned c
1d894 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 har *aPayload;.
1d895 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
1d896 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 75 . u32 nKey;. u
1d897 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20 61 73 32 nLocal;.. as
1d898 73 65 72 74 28 20 70 43 75 72 21 3d 30 20 26 26 sert( pCur!=0 &&
1d899 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 pCur->iPage>=0
1d89a 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b && pCur->apPage[
1d89b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20 pCur->iPage]);.
1d89c 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
1d89d 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
1d89e 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 LID );. assert(
1d89f 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
1d8a0 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70 50 61 x(pCur) );. pPa
1d8a1 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
1d8a2 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
1d8a3 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1d8a4 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
1d8a5 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 e]<pPage->nCell
1d8a6 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 );. if( NEVER(p
1d8a7 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d Cur->info.nSize=
1d8a8 3d 30 29 20 29 7b 0a 20 20 20 20 62 74 72 65 65 =0) ){. btree
1d8a9 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e ParseCell(pCur->
1d8aa 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1d8ab 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 ge], pCur->aiIdx
1d8ac 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2c 0a 20 [pCur->iPage],.
1d8ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d8ae 20 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a &pCur->info);.
1d8af 20 20 7d 0a 20 20 61 50 61 79 6c 6f 61 64 20 3d }. aPayload =
1d8b0 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c pCur->info.pCel
1d8b1 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d l;. aPayload +=
1d8b2 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 pCur->info.nHea
1d8b3 64 65 72 3b 0a 20 20 69 66 28 20 70 50 61 67 65 der;. if( pPage
1d8b4 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 ->intKey ){.
1d8b5 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 nKey = 0;. }els
1d8b6 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 28 69 e{. nKey = (i
1d8b7 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b nt)pCur->info.nK
1d8b8 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 6b ey;. }. if( sk
1d8b9 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 50 61 ipKey ){. aPa
1d8ba 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a 20 yload += nKey;.
1d8bb 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72 nLocal = pCur
1d8bc 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 ->info.nLocal -
1d8bd 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 nKey;. }else{.
1d8be 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72 nLocal = pCur
1d8bf 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 ->info.nLocal;.
1d8c0 20 20 20 61 73 73 65 72 74 28 20 6e 4c 6f 63 61 assert( nLoca
1d8c1 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 20 7d 0a 20 l<=nKey );. }.
1d8c2 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 61 6c 3b *pAmt = nLocal;
1d8c3 0a 20 20 72 65 74 75 72 6e 20 61 50 61 79 6c 6f . return aPaylo
1d8c4 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f ad;.}.../*.** Fo
1d8c5 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 r the entry that
1d8c6 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 20 cursor pCur is
1d8c7 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75 72 6e point to, return
1d8c8 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 74 65 as.** many byte
1d8c9 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 72 20 s of the key or
1d8ca 64 61 74 61 20 61 73 20 61 72 65 20 61 76 61 69 data as are avai
1d8cb 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 lable on the loc
1d8cc 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 70 61 67 al.** b-tree pag
1d8cd 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 6e 75 e. Write the nu
1d8ce 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c mber of availabl
1d8cf 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a 70 41 e bytes into *pA
1d8d0 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f mt..**.** The po
1d8d1 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 69 inter returned i
1d8d2 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 54 68 s ephemeral. Th
1d8d3 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 79 20 6d e key/data may m
1d8d4 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 64 65 73 ove.** or be des
1d8d5 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 20 6e 65 troyed on the ne
1d8d6 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 42 xt call to any B
1d8d7 74 72 65 65 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a tree routine,.**
1d8d8 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 including calls
1d8d9 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 68 72 65 from other thre
1d8da 61 64 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 ads against the
1d8db 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a 2a 20 48 same cache..** H
1d8dc 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 20 6f 6e ence, a mutex on
1d8dd 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 68 the BtShared sh
1d8de 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 70 72 69 ould be held pri
1d8df 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a or to calling.**
1d8e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
1d8e1 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
1d8e2 6e 65 73 20 69 73 20 75 73 65 64 20 74 6f 20 67 nes is used to g
1d8e3 65 74 20 71 75 69 63 6b 20 61 63 63 65 73 73 20 et quick access
1d8e4 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 74 61 0a to key and data.
1d8e5 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e ** in the common
1d8e6 20 63 61 73 65 20 77 68 65 72 65 20 6e 6f 20 6f case where no o
1d8e7 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72 verflow pages ar
1d8e8 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 e used..*/.SQLIT
1d8e9 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
1d8ea 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 void *sqlite3Btr
1d8eb 65 65 4b 65 79 46 65 74 63 68 28 42 74 43 75 72 eeKeyFetch(BtCur
1d8ec 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a sor *pCur, int *
1d8ed 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20 76 pAmt){. const v
1d8ee 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61 73 oid *p = 0;. as
1d8ef 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1d8f0 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 tex_held(pCur->p
1d8f1 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 Btree->db->mutex
1d8f2 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 ) );. assert( c
1d8f3 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
1d8f4 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 41 pCur) );. if( A
1d8f5 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 61 LWAYS(pCur->eSta
1d8f6 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
1d8f7 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63 6f ) ){. p = (co
1d8f8 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50 nst void*)fetchP
1d8f9 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d ayload(pCur, pAm
1d8fa 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 t, 0);. }. ret
1d8fb 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49 54 45 5f urn p;.}.SQLITE_
1d8fc 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f PRIVATE const vo
1d8fd 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 id *sqlite3Btree
1d8fe 44 61 74 61 46 65 74 63 68 28 42 74 43 75 72 73 DataFetch(BtCurs
1d8ff 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 or *pCur, int *p
1d900 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f Amt){. const vo
1d901 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61 73 73 id *p = 0;. ass
1d902 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1d903 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 ex_held(pCur->pB
1d904 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 tree->db->mutex)
1d905 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 );. assert( cu
1d906 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
1d907 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 41 4c Cur) );. if( AL
1d908 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 61 74 WAYS(pCur->eStat
1d909 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 29 e==CURSOR_VALID)
1d90a 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63 6f 6e ){. p = (con
1d90b 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50 61 st void*)fetchPa
1d90c 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d 74 yload(pCur, pAmt
1d90d 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , 1);. }. retu
1d90e 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 rn p;.}.../*.**
1d90f 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
1d910 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20 63 68 down to a new ch
1d911 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65 20 6e ild page. The n
1d912 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 ewPgno argument
1d913 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e is the.** page n
1d914 75 6d 62 65 72 20 6f 66 20 74 68 65 20 63 68 69 umber of the chi
1d915 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 ld page to move
1d916 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 to..**.** This f
1d917 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
1d918 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 SQLITE_CORRUPT i
1d919 66 20 74 68 65 20 70 61 67 65 2d 68 65 61 64 65 f the page-heade
1d91a 72 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 r flags field of
1d91b 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 68 69 6c .** the new chil
1d91c 64 20 70 61 67 65 20 64 6f 65 73 20 6e 6f 74 20 d page does not
1d91d 6d 61 74 63 68 20 74 68 65 20 66 6c 61 67 73 20 match the flags
1d91e 66 69 65 6c 64 20 6f 66 20 74 68 65 20 70 61 72 field of the par
1d91f 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20 69 66 20 ent (i.e..** if
1d920 61 6e 20 69 6e 74 6b 65 79 20 70 61 67 65 20 61 an intkey page a
1d921 70 70 65 61 72 73 20 74 6f 20 62 65 20 74 68 65 ppears to be the
1d922 20 70 61 72 65 6e 74 20 6f 66 20 61 20 6e 6f 6e parent of a non
1d923 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c 20 6f 72 -intkey page, or
1d924 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73 61 29 2e .** vice-versa).
1d925 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
1d926 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 43 75 72 oveToChild(BtCur
1d927 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6e sor *pCur, u32 n
1d928 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 ewPgno){. int r
1d929 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 43 75 c;. int i = pCu
1d92a 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d 65 6d 50 r->iPage;. MemP
1d92b 61 67 65 20 2a 70 4e 65 77 50 61 67 65 3b 0a 20 age *pNewPage;.
1d92c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
1d92d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a 20 20 61 pCur->pBt;.. a
1d92e 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
1d92f 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
1d930 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
1d931 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
1d932 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 VALID );. asser
1d933 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3c 42 t( pCur->iPage<B
1d934 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 TCURSOR_MAX_DEPT
1d935 48 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d H );. if( pCur-
1d936 3e 69 50 61 67 65 3e 3d 28 42 54 43 55 52 53 4f >iPage>=(BTCURSO
1d937 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31 29 20 29 R_MAX_DEPTH-1) )
1d938 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1d939 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1d93a 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 ;. }. rc = get
1d93b 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c AndInitPage(pBt,
1d93c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e 65 77 50 newPgno, &pNewP
1d93d 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 age);. if( rc )
1d93e 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70 43 return rc;. pC
1d93f 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b 31 5d 20 ur->apPage[i+1]
1d940 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 20 70 43 = pNewPage;. pC
1d941 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 5d 20 3d ur->aiIdx[i+1] =
1d942 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 0;. pCur->iPag
1d943 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e e++;.. pCur->in
1d944 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
1d945 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
1d946 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e 65 77 50 = 0;. if( pNewP
1d947 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 7c 7c 20 age->nCell<1 ||
1d948 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74 4b 65 79 pNewPage->intKey
1d949 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 !=pCur->apPage[i
1d94a 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 ]->intKey ){.
1d94b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
1d94c 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
1d94d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1d94e 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 30 0a 2f 2a _OK;.}..#if 0./*
1d94f 0a 2a 2a 20 50 61 67 65 20 70 50 61 72 65 6e 74 .** Page pParent
1d950 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 is an internal
1d951 28 6e 6f 6e 2d 6c 65 61 66 29 20 74 72 65 65 20 (non-leaf) tree
1d952 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63 74 page. This funct
1d953 69 6f 6e 20 0a 2a 2a 20 61 73 73 65 72 74 73 20 ion .** asserts
1d954 74 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 that page number
1d955 20 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 6c iChild is the l
1d956 65 66 74 2d 63 68 69 6c 64 20 69 66 20 74 68 65 eft-child if the
1d957 20 69 49 64 78 27 74 68 0a 2a 2a 20 63 65 6c 6c iIdx'th.** cell
1d958 20 69 6e 20 70 61 67 65 20 70 50 61 72 65 6e 74 in page pParent
1d959 2e 20 4f 72 2c 20 69 66 20 69 49 64 78 20 69 73 . Or, if iIdx is
1d95a 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74 6f equal to the to
1d95b 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a tal number of.**
1d95c 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e cells in pParen
1d95d 74 2c 20 74 68 61 74 20 70 61 67 65 20 6e 75 6d t, that page num
1d95e 62 65 72 20 69 43 68 69 6c 64 20 69 73 20 74 68 ber iChild is th
1d95f 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 e right-child of
1d960 0a 2a 2a 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f .** the page..*/
1d961 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 .static void ass
1d962 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 4d ertParentIndex(M
1d963 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c emPage *pParent,
1d964 20 69 6e 74 20 69 49 64 78 2c 20 50 67 6e 6f 20 int iIdx, Pgno
1d965 69 43 68 69 6c 64 29 7b 0a 20 20 61 73 73 65 72 iChild){. asser
1d966 74 28 20 69 49 64 78 3c 3d 70 50 61 72 65 6e 74 t( iIdx<=pParent
1d967 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 69 66 28 ->nCell );. if(
1d968 20 69 49 64 78 3d 3d 70 50 61 72 65 6e 74 2d 3e iIdx==pParent->
1d969 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 61 73 73 nCell ){. ass
1d96a 65 72 74 28 20 67 65 74 34 62 79 74 65 28 26 70 ert( get4byte(&p
1d96b 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 Parent->aData[pP
1d96c 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 arent->hdrOffset
1d96d 2b 38 5d 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a +8])==iChild );.
1d96e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
1d96f 65 72 74 28 20 67 65 74 34 62 79 74 65 28 66 69 ert( get4byte(fi
1d970 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 ndCell(pParent,
1d971 69 49 64 78 29 29 3d 3d 69 43 68 69 6c 64 20 29 iIdx))==iChild )
1d972 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 ;. }.}.#else.#
1d973 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 50 61 define assertPa
1d974 72 65 6e 74 49 6e 64 65 78 28 78 2c 79 2c 7a 29 rentIndex(x,y,z)
1d975 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 .#endif../*.**
1d976 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
1d977 75 70 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 up to the parent
1d978 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 page..**.** pCu
1d979 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 74 6f r->idx is set to
1d97a 20 74 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 the cell index
1d97b 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
1d97c 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 e pointer.** to
1d97d 74 68 65 20 70 61 67 65 20 77 65 20 61 72 65 20 the page we are
1d97e 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20 20 49 66 coming from. If
1d97f 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 66 we are coming f
1d980 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 69 67 68 74 rom the.** right
1d981 2d 6d 6f 73 74 20 63 68 69 6c 64 20 70 61 67 65 -most child page
1d982 20 74 68 65 6e 20 70 43 75 72 2d 3e 69 64 78 20 then pCur->idx
1d983 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 6d 6f is set to one mo
1d984 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 6c re than.** the l
1d985 61 72 67 65 73 74 20 63 65 6c 6c 20 69 6e 64 65 argest cell inde
1d986 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 x..*/.static voi
1d987 64 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 d moveToParent(B
1d988 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
1d989 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
1d98a 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
1d98b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
1d98c 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
1d98d 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 OR_VALID );. as
1d98e 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 sert( pCur->iPag
1d98f 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 e>0 );. assert(
1d990 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
1d991 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 0a 20 ur->iPage] );..
1d992 20 2f 2a 20 55 50 44 41 54 45 3a 20 49 74 20 69 /* UPDATE: It i
1d993 73 20 61 63 74 75 61 6c 6c 79 20 70 6f 73 73 69 s actually possi
1d994 62 6c 65 20 66 6f 72 20 74 68 65 20 63 6f 6e 64 ble for the cond
1d995 69 74 69 6f 6e 20 74 65 73 74 65 64 20 62 79 20 ition tested by
1d996 74 68 65 20 61 73 73 65 72 74 0a 20 20 2a 2a 20 the assert. **
1d997 62 65 6c 6f 77 20 74 6f 20 62 65 20 75 6e 74 72 below to be untr
1d998 75 65 20 69 66 20 74 68 65 20 64 61 74 61 62 61 ue if the databa
1d999 73 65 20 66 69 6c 65 20 69 73 20 63 6f 72 72 75 se file is corru
1d99a 70 74 2e 20 54 68 69 73 20 63 61 6e 20 6f 63 63 pt. This can occ
1d99b 75 72 20 69 66 0a 20 20 2a 2a 20 6f 6e 65 20 63 ur if. ** one c
1d99c 75 72 73 6f 72 20 68 61 73 20 6d 6f 64 69 66 69 ursor has modifi
1d99d 65 64 20 70 61 67 65 20 70 50 61 72 65 6e 74 20 ed page pParent
1d99e 77 68 69 6c 65 20 61 20 72 65 66 65 72 65 6e 63 while a referenc
1d99f 65 20 74 6f 20 69 74 20 69 73 20 68 65 6c 64 20 e to it is held
1d9a0 0a 20 20 2a 2a 20 62 79 20 61 20 73 65 63 6f 6e . ** by a secon
1d9a1 64 20 63 75 72 73 6f 72 2e 20 57 68 69 63 68 20 d cursor. Which
1d9a2 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 can only happen
1d9a3 69 66 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 if a single page
1d9a4 20 69 73 20 6c 69 6e 6b 65 64 0a 20 20 2a 2a 20 is linked. **
1d9a5 69 6e 74 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f into more than o
1d9a6 6e 65 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 ne b-tree struct
1d9a7 75 72 65 20 69 6e 20 61 20 63 6f 72 72 75 70 74 ure in a corrupt
1d9a8 20 64 61 74 61 62 61 73 65 2e 20 20 2a 2f 0a 23 database. */.#
1d9a9 69 66 20 30 0a 20 20 61 73 73 65 72 74 50 61 72 if 0. assertPar
1d9aa 65 6e 74 49 6e 64 65 78 28 0a 20 20 20 20 70 43 entIndex(. pC
1d9ab 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
1d9ac 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 >iPage-1], .
1d9ad 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
1d9ae 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 ->iPage-1], .
1d9af 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
1d9b0 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 70 67 6e 6f ur->iPage]->pgno
1d9b1 0a 20 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 74 . );.#endif. t
1d9b2 65 73 74 63 61 73 65 28 20 70 43 75 72 2d 3e 61 estcase( pCur->a
1d9b3 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
1d9b4 2d 31 5d 20 3e 20 70 43 75 72 2d 3e 61 70 50 61 -1] > pCur->apPa
1d9b5 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 ge[pCur->iPage-1
1d9b6 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 72 ]->nCell );.. r
1d9b7 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d eleasePage(pCur-
1d9b8 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
1d9b9 61 67 65 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 age]);. pCur->i
1d9ba 50 61 67 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e Page--;. pCur->
1d9bb 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a info.nSize = 0;.
1d9bc 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
1d9bd 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 y = 0;.}../*.**
1d9be 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
1d9bf 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 to point to the
1d9c0 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 69 74 73 root page of its
1d9c1 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 b-tree structur
1d9c2 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
1d9c3 74 61 62 6c 65 20 68 61 73 20 61 20 76 69 72 74 table has a virt
1d9c4 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 ual root page, t
1d9c5 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 hen the cursor i
1d9c6 73 20 6d 6f 76 65 64 20 74 6f 20 70 6f 69 6e 74 s moved to point
1d9c7 0a 2a 2a 20 74 6f 20 74 68 65 20 76 69 72 74 75 .** to the virtu
1d9c8 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 73 al root page ins
1d9c9 74 65 61 64 20 6f 66 20 74 68 65 20 61 63 74 75 tead of the actu
1d9ca 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 41 20 al root page. A
1d9cb 74 61 62 6c 65 20 68 61 73 20 61 0a 2a 2a 20 76 table has a.** v
1d9cc 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 irtual root page
1d9cd 20 77 68 65 6e 20 74 68 65 20 61 63 74 75 61 6c when the actual
1d9ce 20 72 6f 6f 74 20 70 61 67 65 20 63 6f 6e 74 61 root page conta
1d9cf 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 ins no cells and
1d9d0 20 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 68 a .** single ch
1d9d1 69 6c 64 20 70 61 67 65 2e 20 54 68 69 73 20 63 ild page. This c
1d9d2 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77 an only happen w
1d9d3 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 72 6f ith the table ro
1d9d4 6f 74 65 64 20 61 74 20 70 61 67 65 20 31 2e 0a oted at page 1..
1d9d5 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 2d 74 **.** If the b-t
1d9d6 72 65 65 20 73 74 72 75 63 74 75 72 65 20 69 73 ree structure is
1d9d7 20 65 6d 70 74 79 2c 20 74 68 65 20 63 75 72 73 empty, the curs
1d9d8 6f 72 20 73 74 61 74 65 20 69 73 20 73 65 74 20 or state is set
1d9d9 74 6f 20 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e to .** CURSOR_IN
1d9da 56 41 4c 49 44 2e 20 4f 74 68 65 72 77 69 73 65 VALID. Otherwise
1d9db 2c 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 , the cursor is
1d9dc 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 set to point to
1d9dd 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 63 65 6c the first.** cel
1d9de 6c 20 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 l located on the
1d9df 20 72 6f 6f 74 20 28 6f 72 20 76 69 72 74 75 61 root (or virtua
1d9e0 6c 20 72 6f 6f 74 29 20 70 61 67 65 20 61 6e 64 l root) page and
1d9e1 20 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 the cursor stat
1d9e2 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 43 e.** is set to C
1d9e3 55 52 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a URSOR_VALID..**.
1d9e4 2a 2a 20 49 66 20 74 68 69 73 20 66 75 6e 63 74 ** If this funct
1d9e5 69 6f 6e 20 72 65 74 75 72 6e 73 20 73 75 63 63 ion returns succ
1d9e6 65 73 73 66 75 6c 6c 79 2c 20 69 74 20 6d 61 79 essfully, it may
1d9e7 20 62 65 20 61 73 73 75 6d 65 64 20 74 68 61 74 be assumed that
1d9e8 20 74 68 65 0a 2a 2a 20 70 61 67 65 2d 68 65 61 the.** page-hea
1d9e9 64 65 72 20 66 6c 61 67 73 20 69 6e 64 69 63 61 der flags indica
1d9ea 74 65 20 74 68 61 74 20 74 68 65 20 5b 76 69 72 te that the [vir
1d9eb 74 75 61 6c 5d 20 72 6f 6f 74 2d 70 61 67 65 20 tual] root-page
1d9ec 69 73 20 74 68 65 20 65 78 70 65 63 74 65 64 20 is the expected
1d9ed 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 62 2d 74 72 .** kind of b-tr
1d9ee 65 65 20 70 61 67 65 20 28 69 2e 65 2e 20 69 66 ee page (i.e. if
1d9ef 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 74 68 when opening th
1d9f0 65 20 63 75 72 73 6f 72 20 74 68 65 20 63 61 6c e cursor the cal
1d9f1 6c 65 72 20 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 ler did not.** s
1d9f2 70 65 63 69 66 79 20 61 20 4b 65 79 49 6e 66 6f pecify a KeyInfo
1d9f3 20 73 74 72 75 63 74 75 72 65 20 74 68 65 20 66 structure the f
1d9f4 6c 61 67 73 20 62 79 74 65 20 69 73 20 73 65 74 lags byte is set
1d9f5 20 74 6f 20 30 78 30 35 20 6f 72 20 30 78 30 44 to 0x05 or 0x0D
1d9f6 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 ,.** indicating
1d9f7 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2c 20 a table b-tree,
1d9f8 6f 72 20 69 66 20 74 68 65 20 63 61 6c 6c 65 72 or if the caller
1d9f9 20 64 69 64 20 73 70 65 63 69 66 79 20 61 20 4b did specify a K
1d9fa 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 eyInfo .** struc
1d9fb 74 75 72 65 20 74 68 65 20 66 6c 61 67 73 20 62 ture the flags b
1d9fc 79 74 65 20 69 73 20 73 65 74 20 74 6f 20 30 78 yte is set to 0x
1d9fd 30 32 20 6f 72 20 30 78 30 41 2c 20 69 6e 64 69 02 or 0x0A, indi
1d9fe 63 61 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 0a cating an index.
1d9ff 2a 2a 20 62 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 ** b-tree)..*/.s
1da00 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f tatic int moveTo
1da01 52 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 Root(BtCursor *p
1da02 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 Cur){. MemPage
1da03 2a 70 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 *pRoot;. int rc
1da04 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1da05 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d Btree *p = pCur-
1da06 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 >pBtree;. BtSha
1da07 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
1da08 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 t;.. assert( cu
1da09 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
1da0a 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
1da0b 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 ( CURSOR_INVALID
1da0c 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 < CURSOR_REQUIR
1da0d 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 ESEEK );. asser
1da0e 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 t( CURSOR_VALID
1da0f 20 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 < CURSOR_REQUI
1da10 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 RESEEK );. asse
1da11 72 74 28 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 rt( CURSOR_FAULT
1da12 20 20 20 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 > CURSOR_REQU
1da13 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 IRESEEK );. if(
1da14 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 pCur->eState>=C
1da15 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
1da16 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 K ){. if( pCu
1da17 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
1da18 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 R_FAULT ){.
1da19 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 assert( pCur->s
1da1a 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f kipNext!=SQLITE_
1da1b 4f 4b 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 OK );. retu
1da1c 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 rn pCur->skipNex
1da1d 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c t;. }. sql
1da1e 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 ite3BtreeClearCu
1da1f 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a rsor(pCur);. }.
1da20 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 . if( pCur->iPa
1da21 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 ge>=0 ){. int
1da22 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b i;. for(i=1;
1da23 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b i<=pCur->iPage;
1da24 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c i++){. rel
1da25 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 easePage(pCur->a
1da26 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d pPage[i]);. }
1da27 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 . pCur->iPage
1da28 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = 0;. }else if
1da29 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 ( pCur->pgnoRoot
1da2a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d ==0 ){. pCur-
1da2b 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
1da2c 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 72 65 _INVALID;. re
1da2d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1da2e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
1da2f 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 = getAndInitPage
1da30 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f (pBt, pCur->pgno
1da31 52 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61 70 50 Root, &pCur->apP
1da32 61 67 65 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 age[0]);. if(
1da33 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1da34 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 {. pCur->eS
1da35 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e tate = CURSOR_IN
1da36 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 72 65 74 VALID;. ret
1da37 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
1da38 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 pCur->iPage =
1da39 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 43 0;.. /* If pC
1da3a 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 69 73 20 ur->pKeyInfo is
1da3b 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 not NULL, then t
1da3c 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20 6f he caller that o
1da3d 70 65 6e 65 64 20 74 68 69 73 20 63 75 72 73 6f pened this curso
1da3e 72 0a 20 20 20 20 2a 2a 20 65 78 70 65 63 74 65 r. ** expecte
1da3f 64 20 74 6f 20 6f 70 65 6e 20 69 74 20 6f 6e 20 d to open it on
1da40 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2e an index b-tree.
1da41 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 70 Otherwise, if p
1da42 4b 65 79 49 6e 66 6f 20 69 73 0a 20 20 20 20 2a KeyInfo is. *
1da43 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 63 61 6c 6c * NULL, the call
1da44 65 72 20 65 78 70 65 63 74 73 20 61 20 74 61 62 er expects a tab
1da45 6c 65 20 62 2d 74 72 65 65 2e 20 49 66 20 74 68 le b-tree. If th
1da46 69 73 20 69 73 20 6e 6f 74 20 74 68 65 20 63 61 is is not the ca
1da47 73 65 2c 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 se,. ** retur
1da48 6e 20 61 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 n an SQLITE_CORR
1da49 55 50 54 20 65 72 72 6f 72 2e 20 20 2a 2f 0a 20 UPT error. */.
1da4a 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
1da4b 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b >apPage[0]->intK
1da4c 65 79 3d 3d 31 20 7c 7c 20 70 43 75 72 2d 3e 61 ey==1 || pCur->a
1da4d 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 pPage[0]->intKey
1da4e 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28 ==0 );. if( (
1da4f 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d pCur->pKeyInfo==
1da50 30 29 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 0)!=pCur->apPage
1da51 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 [0]->intKey ){.
1da52 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1da53 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1da54 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
1da55 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 Assert that the
1da56 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 6f 66 root page is of
1da57 20 74 68 65 20 63 6f 72 72 65 63 74 20 74 79 70 the correct typ
1da58 65 2e 20 54 68 69 73 20 6d 75 73 74 20 62 65 20 e. This must be
1da59 74 68 65 0a 20 20 2a 2a 20 63 61 73 65 20 61 73 the. ** case as
1da5a 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 68 69 the call to thi
1da5b 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 s function that
1da5c 6c 6f 61 64 65 64 20 74 68 65 20 72 6f 6f 74 2d loaded the root-
1da5d 70 61 67 65 20 28 65 69 74 68 65 72 0a 20 20 2a page (either. *
1da5e 2a 20 74 68 69 73 20 63 61 6c 6c 20 6f 72 20 61 * this call or a
1da5f 20 70 72 65 76 69 6f 75 73 20 69 6e 76 6f 63 61 previous invoca
1da60 74 69 6f 6e 29 20 77 6f 75 6c 64 20 68 61 76 65 tion) would have
1da61 20 64 65 74 65 63 74 65 64 20 63 6f 72 72 75 70 detected corrup
1da62 74 69 6f 6e 20 0a 20 20 2a 2a 20 69 66 20 74 68 tion . ** if th
1da63 65 20 61 73 73 75 6d 70 74 69 6f 6e 20 77 65 72 e assumption wer
1da64 65 20 6e 6f 74 20 74 72 75 65 2c 20 61 6e 64 20 e not true, and
1da65 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 it is not possib
1da66 6c 65 20 66 6f 72 20 74 68 65 20 66 6c 61 67 73 le for the flags
1da67 20 0a 20 20 2a 2a 20 62 79 74 65 20 74 6f 20 68 . ** byte to h
1da68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 ave been modifie
1da69 64 20 77 68 69 6c 65 20 74 68 69 73 20 63 75 72 d while this cur
1da6a 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 sor is holding a
1da6b 20 72 65 66 65 72 65 6e 63 65 0a 20 20 2a 2a 20 reference. **
1da6c 74 6f 20 74 68 65 20 70 61 67 65 2e 20 20 2a 2f to the page. */
1da6d 0a 20 20 70 52 6f 6f 74 20 3d 20 70 43 75 72 2d . pRoot = pCur-
1da6e 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 61 73 >apPage[0];. as
1da6f 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 70 67 6e sert( pRoot->pgn
1da70 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f o==pCur->pgnoRoo
1da71 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t );. assert( p
1da72 52 6f 6f 74 2d 3e 69 73 49 6e 69 74 20 26 26 20 Root->isInit &&
1da73 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d (pCur->pKeyInfo=
1da74 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e 69 6e 74 4b =0)==pRoot->intK
1da75 65 79 20 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 61 ey );.. pCur->a
1da76 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 70 iIdx[0] = 0;. p
1da77 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cur->info.nSize
1da78 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 61 74 4c = 0;. pCur->atL
1da79 61 73 74 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d ast = 0;. pCur-
1da7a 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a >validNKey = 0;.
1da7b 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 6e 43 . if( pRoot->nC
1da7c 65 6c 6c 3d 3d 30 20 26 26 20 21 70 52 6f 6f 74 ell==0 && !pRoot
1da7d 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67 ->leaf ){. Pg
1da7e 6e 6f 20 73 75 62 70 61 67 65 3b 0a 20 20 20 20 no subpage;.
1da7f 69 66 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 21 if( pRoot->pgno!
1da80 3d 31 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 =1 ) return SQLI
1da81 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1da82 0a 20 20 20 20 73 75 62 70 61 67 65 20 3d 20 67 . subpage = g
1da83 65 74 34 62 79 74 65 28 26 70 52 6f 6f 74 2d 3e et4byte(&pRoot->
1da84 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 aData[pRoot->hdr
1da85 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 Offset+8]);.
1da86 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 pCur->eState = C
1da87 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 20 URSOR_VALID;.
1da88 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c rc = moveToChil
1da89 64 28 70 43 75 72 2c 20 73 75 62 70 61 67 65 29 d(pCur, subpage)
1da8a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1da8b 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 28 28 Cur->eState = ((
1da8c 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e 30 29 3f pRoot->nCell>0)?
1da8d 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 43 55 52 CURSOR_VALID:CUR
1da8e 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b 0a 20 20 SOR_INVALID);.
1da8f 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1da90 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 ../*.** Move the
1da91 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 cursor down to
1da92 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 the left-most le
1da93 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 af entry beneath
1da94 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79 20 74 6f the.** entry to
1da95 20 77 68 69 63 68 20 69 74 20 69 73 20 63 75 72 which it is cur
1da96 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e rently pointing.
1da97 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d .**.** The left-
1da98 6d 6f 73 74 20 6c 65 61 66 20 69 73 20 74 68 65 most leaf is the
1da99 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 73 6d one with the sm
1da9a 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20 74 68 65 allest key - the
1da9b 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20 61 73 63 first.** in asc
1da9c 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f ending order..*/
1da9d 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 .static int move
1da9e 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74 43 75 72 ToLeftmost(BtCur
1da9f 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 sor *pCur){. Pg
1daa0 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 no pgno;. int r
1daa1 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1daa2 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
1daa3 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 .. assert( curs
1daa4 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
1daa5 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r) );. assert(
1daa6 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
1daa7 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
1daa8 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 while( rc==SQLIT
1daa9 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65 20 E_OK && !(pPage
1daaa 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
1daab 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 Cur->iPage])->le
1daac 61 66 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 af ){. assert
1daad 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 ( pCur->aiIdx[pC
1daae 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65 ur->iPage]<pPage
1daaf 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 70 ->nCell );. p
1dab0 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 66 gno = get4byte(f
1dab1 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 indCell(pPage, p
1dab2 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
1dab3 3e 69 50 61 67 65 5d 29 29 3b 0a 20 20 20 20 72 >iPage]));. r
1dab4 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 c = moveToChild(
1dab5 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d pCur, pgno);. }
1dab6 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1dab7 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 ./*.** Move the
1dab8 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74 cursor down to t
1dab9 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65 he right-most le
1daba 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 af entry beneath
1dabb 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f 20 the.** page to
1dabc 77 68 69 63 68 20 69 74 20 69 73 20 63 75 72 72 which it is curr
1dabd 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 20 ently pointing.
1dabe 20 4e 6f 74 69 63 65 20 74 68 65 20 64 69 66 66 Notice the diff
1dabf 65 72 65 6e 63 65 0a 2a 2a 20 62 65 74 77 65 65 erence.** betwee
1dac0 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 n moveToLeftmost
1dac1 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f 52 69 67 () and moveToRig
1dac2 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f 76 65 54 htmost(). moveT
1dac3 6f 4c 65 66 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 oLeftmost().** f
1dac4 69 6e 64 73 20 74 68 65 20 6c 65 66 74 2d 6d 6f inds the left-mo
1dac5 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 st entry beneath
1dac6 20 74 68 65 20 2a 65 6e 74 72 79 2a 20 77 68 65 the *entry* whe
1dac7 72 65 61 73 20 6d 6f 76 65 54 6f 52 69 67 68 74 reas moveToRight
1dac8 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20 most().** finds
1dac9 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 the right-most e
1daca 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 ntry beneath the
1dacb 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 54 *page*..**.** T
1dacc 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e he right-most en
1dacd 74 72 79 20 69 73 20 74 68 65 20 6f 6e 65 20 77 try is the one w
1dace 69 74 68 20 74 68 65 20 6c 61 72 67 65 73 74 20 ith the largest
1dacf 6b 65 79 20 2d 20 74 68 65 20 6c 61 73 74 0a 2a key - the last.*
1dad0 2a 20 6b 65 79 20 69 6e 20 61 73 63 65 6e 64 69 * key in ascendi
1dad1 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 ng order..*/.sta
1dad2 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 69 tic int moveToRi
1dad3 67 68 74 6d 6f 73 74 28 42 74 43 75 72 73 6f 72 ghtmost(BtCursor
1dad4 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f 20 *pCur){. Pgno
1dad5 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 3d pgno;. int rc =
1dad6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 SQLITE_OK;. Me
1dad7 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 mPage *pPage = 0
1dad8 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
1dad9 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
1dada 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
1dadb 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
1dadc 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 URSOR_VALID );.
1dadd 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 while( rc==SQLI
1dade 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65 TE_OK && !(pPage
1dadf 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
1dae0 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c pCur->iPage])->l
1dae1 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 eaf ){. pgno
1dae2 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 = get4byte(&pPag
1dae3 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
1dae4 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 hdrOffset+8]);.
1dae5 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 pCur->aiIdx[p
1dae6 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 Cur->iPage] = pP
1dae7 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 age->nCell;.
1dae8 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 rc = moveToChild
1dae9 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 (pCur, pgno);.
1daea 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 }. if( rc==SQLI
1daeb 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 43 75 TE_OK ){. pCu
1daec 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
1daed 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d 3e 6e Page] = pPage->n
1daee 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70 43 75 72 Cell-1;. pCur
1daef 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 ->info.nSize = 0
1daf0 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 ;. pCur->vali
1daf1 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 dNKey = 0;. }.
1daf2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1daf3 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
1daf4 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 r to the first e
1daf5 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c ntry in the tabl
1daf6 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 e. Return SQLIT
1daf7 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 E_OK.** on succe
1daf8 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73 20 74 ss. Set *pRes t
1daf9 6f 20 30 20 69 66 20 74 68 65 20 63 75 72 73 6f o 0 if the curso
1dafa 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74 r actually point
1dafb 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a s to something.*
1dafc 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73 20 74 * or set *pRes t
1dafd 6f 20 31 20 69 66 20 74 68 65 20 74 61 62 6c 65 o 1 if the table
1dafe 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 is empty..*/.SQ
1daff 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1db00 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 sqlite3BtreeFir
1db01 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 st(BtCursor *pCu
1db02 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 r, int *pRes){.
1db03 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 int rc;.. asse
1db04 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
1db05 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
1db06 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1db07 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d mutex_held(pCur-
1db08 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 >pBtree->db->mut
1db09 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d 6f ex) );. rc = mo
1db0a 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a veToRoot(pCur);.
1db0b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1db0c 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 _OK ){. if( p
1db0d 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
1db0e 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 SOR_INVALID ){.
1db0f 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 assert( pCu
1db10 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20 7c r->pgnoRoot==0 |
1db11 7c 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 | pCur->apPage[p
1db12 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 Cur->iPage]->nCe
1db13 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a ll==0 );. *
1db14 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 7d 65 pRes = 1;. }e
1db15 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 lse{. asser
1db16 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b t( pCur->apPage[
1db17 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 pCur->iPage]->nC
1db18 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 2a ell>0 );. *
1db19 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 pRes = 0;.
1db1a 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d rc = moveToLeftm
1db1b 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 7d ost(pCur);. }
1db1c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1db1d 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 ;.}../* Move the
1db1e 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6c cursor to the l
1db1f 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 ast entry in the
1db20 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 table. Return
1db21 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 SQLITE_OK.** on
1db22 73 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 success. Set *p
1db23 52 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 Res to 0 if the
1db24 63 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 cursor actually
1db25 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 points to someth
1db26 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 ing.** or set *p
1db27 52 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 Res to 1 if the
1db28 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a table is empty..
1db29 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1db2a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1db2b 65 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 20 eeLast(BtCursor
1db2c 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 *pCur, int *pRes
1db2d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 0a 20 ){. int rc;. .
1db2e 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
1db2f 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
1db30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
1db31 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1db32 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
1db33 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f ->mutex) );.. /
1db34 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 * If the cursor
1db35 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 73 20 74 already points t
1db36 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 o the last entry
1db37 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f , this is a no-o
1db38 70 2e 20 2a 2f 0a 20 20 69 66 28 20 43 55 52 53 p. */. if( CURS
1db39 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e OR_VALID==pCur->
1db3a 65 53 74 61 74 65 20 26 26 20 70 43 75 72 2d 3e eState && pCur->
1db3b 61 74 4c 61 73 74 20 29 7b 0a 23 69 66 64 65 66 atLast ){.#ifdef
1db3c 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
1db3d 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 /* This block
1db3e 73 65 72 76 65 73 20 74 6f 20 61 73 73 65 72 74 serves to assert
1db3f 28 29 20 74 68 61 74 20 74 68 65 20 63 75 72 73 () that the curs
1db40 6f 72 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 70 or really does p
1db41 6f 69 6e 74 20 0a 20 20 20 20 2a 2a 20 74 6f 20 oint . ** to
1db42 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 the last entry i
1db43 6e 20 74 68 65 20 62 2d 74 72 65 65 2e 20 2a 2f n the b-tree. */
1db44 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 . int ii;.
1db45 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 for(ii=0; ii<pC
1db46 75 72 2d 3e 69 50 61 67 65 3b 20 69 69 2b 2b 29 ur->iPage; ii++)
1db47 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1db48 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 69 5d 3d pCur->aiIdx[ii]=
1db49 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 69 =pCur->apPage[ii
1db4a 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 ]->nCell );.
1db4b 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
1db4c 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
1db4d 69 50 61 67 65 5d 3d 3d 70 43 75 72 2d 3e 61 70 iPage]==pCur->ap
1db4e 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
1db4f 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20 20 ]->nCell-1 );.
1db50 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1db51 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1db52 67 65 5d 2d 3e 6c 65 61 66 20 29 3b 0a 23 65 6e ge]->leaf );.#en
1db53 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 dif. return S
1db54 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 QLITE_OK;. }..
1db55 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 rc = moveToRoot
1db56 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
1db57 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1db58 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e if( CURSOR_IN
1db59 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 VALID==pCur->eSt
1db5a 61 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ate ){. ass
1db5b 65 72 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 ert( pCur->pgnoR
1db5c 6f 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e oot==0 || pCur->
1db5d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1db5e 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b ge]->nCell==0 );
1db5f 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 . *pRes = 1
1db60 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1db61 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
1db62 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
1db63 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a VALID );. *
1db64 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 pRes = 0;.
1db65 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 rc = moveToRight
1db66 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 most(pCur);.
1db67 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d pCur->atLast =
1db68 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f rc==SQLITE_OK ?
1db69 31 3a 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 1:0;. }. }.
1db6a 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1db6b 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
1db6c 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 r so that it poi
1db6d 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 nts to an entry
1db6e 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a near the key .**
1db6f 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 49 specified by pI
1db70 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e dxKey or intKey.
1db71 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 Return a succ
1db72 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 ess code..**.**
1db73 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 For INTKEY table
1db74 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61 s, the intKey pa
1db75 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e rameter is used.
1db76 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 pIdxKey .** mu
1db77 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 st be NULL. For
1db78 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70 index tables, p
1db79 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 61 IdxKey is used a
1db7a 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 nd intKey.** is
1db7b 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 ignored..**.** I
1db7c 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 f an exact match
1db7d 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 is not found, t
1db7e 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 hen the cursor i
1db7f 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 s always.** left
1db80 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c pointing at a l
1db81 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 77 eaf page which w
1db82 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e ould hold the en
1db83 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 try if it.** wer
1db84 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20 e present. The
1db85 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 cursor might poi
1db86 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 nt to an entry t
1db87 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 hat comes.** bef
1db88 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 65 ore or after the
1db89 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 key..**.** An i
1db8a 6e 74 65 67 65 72 20 69 73 20 77 72 69 74 74 65 nteger is writte
1db8b 6e 20 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69 n into *pRes whi
1db8c 63 68 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 ch is the result
1db8d 20 6f 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 of.** comparing
1db8e 20 74 68 65 20 6b 65 79 20 77 69 74 68 20 74 68 the key with th
1db8f 65 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 e entry to which
1db90 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a the cursor is .
1db91 2a 2a 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68 ** pointing. Th
1db92 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65 e meaning of the
1db93 20 69 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e integer written
1db94 20 69 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 into.** *pRes i
1db95 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
1db96 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20 .** *pRes<0
1db97 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 The cursor
1db98 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 is left pointing
1db99 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 at an entry tha
1db9a 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
1db9b 20 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 is smaller
1db9c 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 than intKey/pId
1db9d 78 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20 74 xKey or if the t
1db9e 61 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a able is empty.**
1db9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dba0 20 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 and the cursor
1dba1 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65 is therefore le
1dba2 66 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 ft point to noth
1dba3 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a ing..**.** *
1dba4 70 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20 pRes==0 The
1dba5 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 cursor is left p
1dba6 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e ointing at an en
1dba7 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 try that.**
1dba8 20 20 20 20 20 20 20 20 20 20 20 20 20 65 78 61 exa
1dba9 63 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74 ctly matches int
1dbaa 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a Key/pIdxKey..**.
1dbab 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20 ** *pRes>0
1dbac 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 The cursor i
1dbad 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
1dbae 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 at an entry that
1dbaf 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1dbb0 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 74 is larger t
1dbb1 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b han intKey/pIdxK
1dbb2 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 ey..**.*/.SQLITE
1dbb3 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1dbb4 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 ite3BtreeMovetoU
1dbb5 6e 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 npacked(. BtCur
1dbb6 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 sor *pCur,
1dbb7 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f /* The curso
1dbb8 72 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f r to be moved */
1dbb9 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
1dbba 64 20 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55 d *pIdxKey, /* U
1dbbb 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 npacked index ke
1dbbc 79 20 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65 y */. i64 intKe
1dbbd 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y,
1dbbe 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6b 65 79 /* The table key
1dbbf 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69 */. int biasRi
1dbc0 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f ght, /
1dbc1 2a 20 49 66 20 74 72 75 65 2c 20 62 69 61 73 20 * If true, bias
1dbc2 74 68 65 20 73 65 61 72 63 68 20 74 6f 20 74 68 the search to th
1dbc3 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 e high end */.
1dbc4 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 int *pRes
1dbc5 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
1dbc6 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 e search results
1dbc7 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e here */.){. in
1dbc8 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
1dbc9 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
1dbca 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
1dbcb 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1dbcc 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 ex_held(pCur->pB
1dbcd 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 tree->db->mutex)
1dbce 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 );. assert( pR
1dbcf 65 73 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 es );. assert(
1dbd0 28 70 49 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 70 (pIdxKey==0)==(p
1dbd1 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 Cur->pKeyInfo==0
1dbd2 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ) );.. /* If th
1dbd3 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 e cursor is alre
1dbd4 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 61 ady positioned a
1dbd5 74 20 74 68 65 20 70 6f 69 6e 74 20 77 65 20 61 t the point we a
1dbd6 72 65 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74 re trying. ** t
1dbd7 6f 20 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20 o move to, then
1dbd8 6a 75 73 74 20 72 65 74 75 72 6e 20 77 69 74 68 just return with
1dbd9 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f out doing any wo
1dbda 72 6b 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 rk */. if( pCur
1dbdb 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
1dbdc 5f 56 41 4c 49 44 20 26 26 20 70 43 75 72 2d 3e _VALID && pCur->
1dbdd 76 61 6c 69 64 4e 4b 65 79 20 0a 20 20 20 26 26 validNKey . &&
1dbde 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d pCur->apPage[0]
1dbdf 2d 3e 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 ->intKey . ){.
1dbe0 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 if( pCur->inf
1dbe1 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 o.nKey==intKey )
1dbe2 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 {. *pRes =
1dbe3 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 0;. return
1dbe4 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
1dbe5 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 . if( pCur->a
1dbe6 74 4c 61 73 74 20 26 26 20 70 43 75 72 2d 3e 69 tLast && pCur->i
1dbe7 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 nfo.nKey<intKey
1dbe8 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d ){. *pRes =
1dbe9 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 -1;. retur
1dbea 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
1dbeb 20 7d 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d }. }.. rc = m
1dbec 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b oveToRoot(pCur);
1dbed 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
1dbee 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
1dbef 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1dbf0 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20 7c 7c 20 70 pgnoRoot==0 || p
1dbf1 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
1dbf2 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 ->iPage] );. as
1dbf3 73 65 72 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f sert( pCur->pgno
1dbf4 52 6f 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d Root==0 || pCur-
1dbf5 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
1dbf6 61 67 65 5d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a age]->isInit );.
1dbf7 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1dbf8 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 eState==CURSOR_I
1dbf9 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e NVALID || pCur->
1dbfa 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1dbfb 67 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a ge]->nCell>0 );.
1dbfc 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 if( pCur->eSta
1dbfd 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c te==CURSOR_INVAL
1dbfe 49 44 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 ID ){. *pRes
1dbff 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74 = -1;. assert
1dc00 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 ( pCur->pgnoRoot
1dc01 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 61 70 50 ==0 || pCur->apP
1dc02 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
1dc03 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 ->nCell==0 );.
1dc04 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1dc05 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 OK;. }. assert
1dc06 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 ( pCur->apPage[0
1dc07 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 64 ]->intKey || pId
1dc08 78 4b 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b 3b xKey );. for(;;
1dc09 29 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c 20 ){. int lwr,
1dc0a 75 70 72 2c 20 69 64 78 3b 0a 20 20 20 20 50 67 upr, idx;. Pg
1dc0b 6e 6f 20 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d no chldPg;. M
1dc0c 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 emPage *pPage =
1dc0d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
1dc0e 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 r->iPage];. i
1dc0f 6e 74 20 63 3b 0a 0a 20 20 20 20 2f 2a 20 70 50 nt c;.. /* pP
1dc10 61 67 65 2d 3e 6e 43 65 6c 6c 20 6d 75 73 74 20 age->nCell must
1dc11 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 be greater than
1dc12 7a 65 72 6f 2e 20 49 66 20 74 68 69 73 20 69 73 zero. If this is
1dc13 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20 the root-page.
1dc14 20 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 ** the cursor
1dc15 20 77 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e would have been
1dc16 20 49 4e 56 41 4c 49 44 20 61 62 6f 76 65 20 61 INVALID above a
1dc17 6e 64 20 74 68 69 73 20 66 6f 72 28 3b 3b 29 20 nd this for(;;)
1dc18 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 loop. ** not
1dc19 72 75 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20 run. If this is
1dc1a 6e 6f 74 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 not the root-pag
1dc1b 65 2c 20 74 68 65 6e 20 74 68 65 20 6d 6f 76 65 e, then the move
1dc1c 54 6f 43 68 69 6c 64 28 29 20 72 6f 75 74 69 6e ToChild() routin
1dc1d 65 0a 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20 68 e. ** would h
1dc1e 61 76 65 20 61 6c 72 65 61 64 79 20 64 65 74 65 ave already dete
1dc1f 63 74 65 64 20 64 62 20 63 6f 72 72 75 70 74 69 cted db corrupti
1dc20 6f 6e 2e 20 53 69 6d 69 6c 61 72 6c 79 2c 20 70 on. Similarly, p
1dc21 50 61 67 65 20 6d 75 73 74 0a 20 20 20 20 2a 2a Page must. **
1dc22 20 62 65 20 74 68 65 20 72 69 67 68 74 20 6b 69 be the right ki
1dc23 6e 64 20 28 69 6e 64 65 78 20 6f 72 20 74 61 62 nd (index or tab
1dc24 6c 65 29 20 6f 66 20 62 2d 74 72 65 65 20 70 61 le) of b-tree pa
1dc25 67 65 2e 20 4f 74 68 65 72 77 69 73 65 0a 20 20 ge. Otherwise.
1dc26 20 20 2a 2a 20 61 20 6d 6f 76 65 54 6f 43 68 69 ** a moveToChi
1dc27 6c 64 28 29 20 6f 72 20 6d 6f 76 65 54 6f 52 6f ld() or moveToRo
1dc28 6f 74 28 29 20 63 61 6c 6c 20 77 6f 75 6c 64 20 ot() call would
1dc29 68 61 76 65 20 64 65 74 65 63 74 65 64 20 63 6f have detected co
1dc2a 72 72 75 70 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 rruption. */.
1dc2b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
1dc2c 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 >nCell>0 );.
1dc2d 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 assert( pPage->i
1dc2e 6e 74 4b 65 79 3d 3d 28 70 49 64 78 4b 65 79 3d ntKey==(pIdxKey=
1dc2f 3d 30 29 20 29 3b 0a 20 20 20 20 6c 77 72 20 3d =0) );. lwr =
1dc30 20 30 3b 0a 20 20 20 20 75 70 72 20 3d 20 70 50 0;. upr = pP
1dc31 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 age->nCell-1;.
1dc32 20 20 69 66 28 20 62 69 61 73 52 69 67 68 74 20 if( biasRight
1dc33 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 ){. pCur->a
1dc34 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
1dc35 5d 20 3d 20 28 75 31 36 29 28 69 64 78 20 3d 20 ] = (u16)(idx =
1dc36 75 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b upr);. }else{
1dc37 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 . pCur->aiI
1dc38 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 dx[pCur->iPage]
1dc39 3d 20 28 75 31 36 29 28 69 64 78 20 3d 20 28 75 = (u16)(idx = (u
1dc3a 70 72 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20 pr+lwr)/2);.
1dc3b 7d 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 }. for(;;){.
1dc3c 20 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 u8 *pCell;
1dc3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dc3e 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
1dc3f 74 65 72 20 74 6f 20 63 75 72 72 65 6e 74 20 63 ter to current c
1dc40 65 6c 6c 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a ell in pPage */.
1dc41 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
1dc42 64 78 3d 3d 70 43 75 72 2d 3e 61 69 49 64 78 5b dx==pCur->aiIdx[
1dc43 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a pCur->iPage] );.
1dc44 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f pCur->info
1dc45 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 .nSize = 0;.
1dc46 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 pCell = findCe
1dc47 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b ll(pPage, idx) +
1dc48 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 pPage->childPtr
1dc49 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 Size;. if(
1dc4a 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b pPage->intKey ){
1dc4b 0a 20 20 20 20 20 20 20 20 69 36 34 20 6e 43 65 . i64 nCe
1dc4c 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 llKey;. i
1dc4d 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 f( pPage->hasDat
1dc4e 61 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 a ){. u
1dc4f 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 32 dummy;.
1dc50 20 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 pCell += get
1dc51 56 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 Varint32(pCell,
1dc52 64 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 dummy);.
1dc53 7d 0a 20 20 20 20 20 20 20 20 67 65 74 56 61 72 }. getVar
1dc54 69 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a int(pCell, (u64*
1dc55 29 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 )&nCellKey);.
1dc56 20 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 if( nCellKe
1dc57 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 y==intKey ){.
1dc58 20 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 c = 0;.
1dc59 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1dc5a 6e 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 nCellKey<intKey
1dc5b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d ){. c =
1dc5c 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c -1;. }el
1dc5d 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 se{. as
1dc5e 73 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 sert( nCellKey>i
1dc5f 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 ntKey );.
1dc60 20 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 c = +1;.
1dc61 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 43 75 }. pCu
1dc62 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 r->validNKey = 1
1dc63 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e ;. pCur->
1dc64 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c info.nKey = nCel
1dc65 6c 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 lKey;. }els
1dc66 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 e{. /* Th
1dc67 65 20 6d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 e maximum suppor
1dc68 74 65 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73 ted page-size is
1dc69 20 36 35 35 33 36 20 62 79 74 65 73 2e 20 54 68 65536 bytes. Th
1dc6a 69 73 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 is means that.
1dc6b 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 ** the max
1dc6c 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 imum number of r
1dc6d 65 63 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72 ecord bytes stor
1dc6e 65 64 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 ed on an index B
1dc6f 2d 54 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a -Tree. **
1dc70 20 70 61 67 65 20 69 73 20 6c 65 73 73 20 74 68 page is less th
1dc71 61 6e 20 31 36 33 38 34 20 62 79 74 65 73 20 61 an 16384 bytes a
1dc72 6e 64 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 nd may be stored
1dc73 20 61 73 20 61 20 32 2d 62 79 74 65 0a 20 20 20 as a 2-byte.
1dc74 20 20 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 ** varint.
1dc75 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e This information
1dc76 20 69 73 20 75 73 65 64 20 74 6f 20 61 74 74 65 is used to atte
1dc77 6d 70 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72 mpt to avoid par
1dc78 73 69 6e 67 20 0a 20 20 20 20 20 20 20 20 2a 2a sing . **
1dc79 20 74 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c the entire cell
1dc7a 20 62 79 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 by checking for
1dc7b 20 74 68 65 20 63 61 73 65 73 20 77 68 65 72 65 the cases where
1dc7c 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 0a the record is .
1dc7d 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 ** store
1dc7e 64 20 65 6e 74 69 72 65 6c 79 20 77 69 74 68 69 d entirely withi
1dc7f 6e 20 74 68 65 20 62 2d 74 72 65 65 20 70 61 67 n the b-tree pag
1dc80 65 20 62 79 20 69 6e 73 70 65 63 74 69 6e 67 20 e by inspecting
1dc81 74 68 65 20 66 69 72 73 74 20 0a 20 20 20 20 20 the first .
1dc82 20 20 20 2a 2a 20 32 20 62 79 74 65 73 20 6f 66 ** 2 bytes of
1dc83 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20 the cell..
1dc84 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e */. in
1dc85 74 20 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b t nCell = pCell[
1dc86 30 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0];. if(
1dc87 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d 61 nCell<=pPage->ma
1dc88 78 31 62 79 74 65 50 61 79 6c 6f 61 64 0a 20 20 x1bytePayload.
1dc89 20 20 20 20 20 20 20 2f 2a 20 26 26 20 28 70 43 /* && (pC
1dc8a 65 6c 6c 2b 6e 43 65 6c 6c 29 3c 70 50 61 67 65 ell+nCell)<pPage
1dc8b 2d 3e 61 44 61 74 61 45 6e 64 20 2a 2f 0a 20 20 ->aDataEnd */.
1dc8c 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
1dc8d 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 /* This branc
1dc8e 68 20 72 75 6e 73 20 69 66 20 74 68 65 20 72 65 h runs if the re
1dc8f 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 cord-size field
1dc90 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 61 of the cell is a
1dc91 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69 . ** si
1dc92 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69 6e 74 ngle byte varint
1dc93 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 and the record
1dc94 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e fits entirely on
1dc95 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 20 20 the main.
1dc96 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 70 61 ** b-tree pa
1dc97 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 ge. */.
1dc98 20 20 74 65 73 74 63 61 73 65 28 20 70 43 65 6c testcase( pCel
1dc99 6c 2b 6e 43 65 6c 6c 2b 31 3d 3d 70 50 61 67 65 l+nCell+1==pPage
1dc9a 2d 3e 61 44 61 74 61 45 6e 64 20 29 3b 0a 20 20 ->aDataEnd );.
1dc9b 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 c = sqli
1dc9c 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d te3VdbeRecordCom
1dc9d 70 61 72 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 pare(nCell, (voi
1dc9e 64 2a 29 26 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 d*)&pCell[1], pI
1dc9f 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 dxKey);.
1dca0 7d 65 6c 73 65 20 69 66 28 20 21 28 70 43 65 6c }else if( !(pCel
1dca1 6c 5b 31 5d 20 26 20 30 78 38 30 29 20 0a 20 20 l[1] & 0x80) .
1dca2 20 20 20 20 20 20 20 20 26 26 20 28 6e 43 65 6c && (nCel
1dca3 6c 20 3d 20 28 28 6e 43 65 6c 6c 26 30 78 37 66 l = ((nCell&0x7f
1dca4 29 3c 3c 37 29 20 2b 20 70 43 65 6c 6c 5b 31 5d )<<7) + pCell[1]
1dca5 29 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 )<=pPage->maxLoc
1dca6 61 6c 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 al. /*
1dca7 26 26 20 28 70 43 65 6c 6c 2b 6e 43 65 6c 6c 2b && (pCell+nCell+
1dca8 32 29 3c 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 2)<=pPage->aData
1dca9 45 6e 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 29 End */. )
1dcaa 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 {. /* T
1dcab 68 65 20 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 he record-size f
1dcac 69 65 6c 64 20 69 73 20 61 20 32 20 62 79 74 65 ield is a 2 byte
1dcad 20 76 61 72 69 6e 74 20 61 6e 64 20 74 68 65 20 varint and the
1dcae 72 65 63 6f 72 64 20 0a 20 20 20 20 20 20 20 20 record .
1dcaf 20 20 2a 2a 20 66 69 74 73 20 65 6e 74 69 72 65 ** fits entire
1dcb0 6c 79 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 ly on the main b
1dcb1 2d 74 72 65 65 20 70 61 67 65 2e 20 20 2a 2f 0a -tree page. */.
1dcb2 20 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 testca
1dcb3 73 65 28 20 70 43 65 6c 6c 2b 6e 43 65 6c 6c 2b se( pCell+nCell+
1dcb4 32 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 45 2==pPage->aDataE
1dcb5 6e 64 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 nd );.
1dcb6 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 c = sqlite3VdbeR
1dcb7 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 ecordCompare(nCe
1dcb8 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 65 6c ll, (void*)&pCel
1dcb9 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29 3b 0a l[2], pIdxKey);.
1dcba 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1dcbb 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1dcbc 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f 76 65 record flows ove
1dcbd 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f r onto one or mo
1dcbe 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 re overflow page
1dcbf 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 20 20 20 s. In.
1dcc0 2a 2a 20 74 68 69 73 20 63 61 73 65 20 74 68 65 ** this case the
1dcc1 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65 65 64 whole cell need
1dcc2 73 20 74 6f 20 62 65 20 70 61 72 73 65 64 2c 20 s to be parsed,
1dcc3 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 a buffer allocat
1dcc4 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 ed. **
1dcc5 61 6e 64 20 61 63 63 65 73 73 50 61 79 6c 6f 61 and accessPayloa
1dcc6 64 28 29 20 75 73 65 64 20 74 6f 20 72 65 74 72 d() used to retr
1dcc7 69 65 76 65 20 74 68 65 20 72 65 63 6f 72 64 20 ieve the record
1dcc8 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 20 20 into the.
1dcc9 20 20 20 2a 2a 20 62 75 66 66 65 72 20 62 65 66 ** buffer bef
1dcca 6f 72 65 20 56 64 62 65 52 65 63 6f 72 64 43 6f ore VdbeRecordCo
1dccb 6d 70 61 72 65 28 29 20 63 61 6e 20 62 65 20 63 mpare() can be c
1dccc 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 alled. */.
1dccd 20 20 20 20 76 6f 69 64 20 2a 70 43 65 6c 6c 4b void *pCellK
1dcce 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 75 38 ey;. u8
1dccf 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c 42 6f * const pCellBo
1dcd0 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70 50 61 dy = pCell - pPa
1dcd1 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 ge->childPtrSize
1dcd2 3b 0a 20 20 20 20 20 20 20 20 20 20 62 74 72 65 ;. btre
1dcd3 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
1dcd4 61 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79 2c 20 age, pCellBody,
1dcd5 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 &pCur->info);.
1dcd6 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20 nCell =
1dcd7 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e (int)pCur->info.
1dcd8 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 nKey;.
1dcd9 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74 pCellKey = sqlit
1dcda 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c 20 e3Malloc( nCell
1dcdb 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
1dcdc 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a pCellKey==0 ){.
1dcdd 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
1dcde 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1dcdf 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
1dce0 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 moveto_finish;.
1dce1 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1dce2 20 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 73 rc = access
1dce3 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 30 2c Payload(pCur, 0,
1dce4 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 67 6e 65 nCell, (unsigne
1dce5 64 20 63 68 61 72 2a 29 70 43 65 6c 6c 4b 65 79 d char*)pCellKey
1dce6 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 0);.
1dce7 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
1dce8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
1dce9 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 ee(pCellKey);.
1dcea 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d goto m
1dceb 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 oveto_finish;.
1dcec 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1dced 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 c = sqlite3V
1dcee 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 dbeRecordCompare
1dcef 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b 65 79 (nCell, pCellKey
1dcf0 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 , pIdxKey);.
1dcf1 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
1dcf2 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 ee(pCellKey);.
1dcf3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1dcf4 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 if( c==0 )
1dcf5 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 {. if( pP
1dcf6 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 age->intKey && !
1dcf7 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
1dcf8 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 lwr = i
1dcf9 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 dx;. br
1dcfa 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c eak;. }el
1dcfb 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 se{. *p
1dcfc 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 Res = 0;.
1dcfd 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
1dcfe 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 K;. got
1dcff 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b o moveto_finish;
1dd00 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1dd01 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3c 30 }. if( c<0
1dd02 20 29 7b 0a 20 20 20 20 20 20 20 20 6c 77 72 20 ){. lwr
1dd03 3d 20 69 64 78 2b 31 3b 0a 20 20 20 20 20 20 7d = idx+1;. }
1dd04 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75 70 else{. up
1dd05 72 20 3d 20 69 64 78 2d 31 3b 0a 20 20 20 20 20 r = idx-1;.
1dd06 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6c 77 72 }. if( lwr
1dd07 3e 75 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 >upr ){.
1dd08 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
1dd09 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 pCur->aiIdx
1dd0a 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 [pCur->iPage] =
1dd0b 28 75 31 36 29 28 69 64 78 20 3d 20 28 6c 77 72 (u16)(idx = (lwr
1dd0c 2b 75 70 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a +upr)/2);. }.
1dd0d 20 20 20 20 61 73 73 65 72 74 28 20 6c 77 72 3d assert( lwr=
1dd0e 3d 75 70 72 2b 31 20 7c 7c 20 28 70 50 61 67 65 =upr+1 || (pPage
1dd0f 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 50 61 ->intKey && !pPa
1dd10 67 65 2d 3e 6c 65 61 66 29 20 29 3b 0a 20 20 20 ge->leaf) );.
1dd11 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
1dd12 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66 isInit );. if
1dd13 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b ( pPage->leaf ){
1dd14 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 . chldPg =
1dd15 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 0;. }else if(
1dd16 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 lwr>=pPage->nCe
1dd17 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 ll ){. chld
1dd18 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 Pg = get4byte(&p
1dd19 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
1dd1a 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
1dd1b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1dd1c 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 chldPg = get4
1dd1d 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 byte(findCell(pP
1dd1e 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20 age, lwr));.
1dd1f 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 67 }. if( chldPg
1dd20 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ==0 ){. ass
1dd21 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
1dd22 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 [pCur->iPage]<pC
1dd23 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
1dd24 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 >iPage]->nCell )
1dd25 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 ;. *pRes =
1dd26 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 c;. rc = SQ
1dd27 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 67 LITE_OK;. g
1dd28 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 oto moveto_finis
1dd29 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 h;. }. pCu
1dd2a 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
1dd2b 50 61 67 65 5d 20 3d 20 28 75 31 36 29 6c 77 72 Page] = (u16)lwr
1dd2c 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f ;. pCur->info
1dd2d 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 .nSize = 0;.
1dd2e 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
1dd2f 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f = 0;. rc = mo
1dd30 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 veToChild(pCur,
1dd31 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 66 28 chldPg);. if(
1dd32 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 65 74 rc ) goto movet
1dd33 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 6d 6f o_finish;. }.mo
1dd34 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20 72 veto_finish:. r
1dd35 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
1dd36 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 .** Return TRUE
1dd37 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 if the cursor is
1dd38 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 not pointing at
1dd39 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 65 an entry of the
1dd3a 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 52 table..**.** TR
1dd3b 55 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 UE will be retur
1dd3c 6e 65 64 20 61 66 74 65 72 20 61 20 63 61 6c 6c ned after a call
1dd3d 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 to sqlite3Btree
1dd3e 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a 20 Next() moves.**
1dd3f 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 65 6e past the last en
1dd40 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 try in the table
1dd41 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 or sqlite3Btree
1dd42 50 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61 73 Prev() moves pas
1dd43 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 65 t.** the first e
1dd44 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73 20 61 ntry. TRUE is a
1dd45 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20 lso returned if
1dd46 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 the table is emp
1dd47 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ty..*/.SQLITE_PR
1dd48 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1dd49 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73 3BtreeEof(BtCurs
1dd4a 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 or *pCur){. /*
1dd4b 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74 68 TODO: What if th
1dd4c 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 43 e cursor is in C
1dd4d 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
1dd4e 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65 20 K but all table
1dd4f 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61 76 entries. ** hav
1dd50 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f 20 e been deleted?
1dd51 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e 65 This API will ne
1dd52 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f 20 ed to change to
1dd53 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
1dd54 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65 6c code. ** as wel
1dd55 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61 6e l as the boolean
1dd56 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a 20 result value..
1dd57 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43 55 */. return (CU
1dd58 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75 72 RSOR_VALID!=pCur
1dd59 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f 2a ->eState);.}../*
1dd5a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 65 20 .** Advance the
1dd5b 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6e 65 cursor to the ne
1dd5c 78 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 xt entry in the
1dd5d 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a database. If.**
1dd5e 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 6e successful then
1dd5f 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 49 set *pRes=0. I
1dd60 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 f the cursor.**
1dd61 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e was already poin
1dd62 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 ting to the last
1dd63 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 entry in the da
1dd64 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a tabase before.**
1dd65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 this routine wa
1dd66 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73 s called, then s
1dd67 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 et *pRes=1..*/.S
1dd68 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1dd69 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 t sqlite3BtreeNe
1dd6a 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 xt(BtCursor *pCu
1dd6b 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 r, int *pRes){.
1dd6c 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 int rc;. int i
1dd6d 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 dx;. MemPage *p
1dd6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 Page;.. assert(
1dd6f 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
1dd70 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 x(pCur) );. rc
1dd71 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 = restoreCursorP
1dd72 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 osition(pCur);.
1dd73 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1dd74 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OK ){. return
1dd75 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 rc;. }. asser
1dd76 74 28 20 70 52 65 73 21 3d 30 20 29 3b 0a 20 20 t( pRes!=0 );.
1dd77 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c if( CURSOR_INVAL
1dd78 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 ID==pCur->eState
1dd79 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 ){. *pRes =
1dd7a 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 1;. return SQ
1dd7b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 LITE_OK;. }. i
1dd7c 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 f( pCur->skipNex
1dd7d 74 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d t>0 ){. pCur-
1dd7e 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20 >skipNext = 0;.
1dd7f 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
1dd80 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1dd81 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e OK;. }. pCur->
1dd82 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 skipNext = 0;..
1dd83 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 pPage = pCur->a
1dd84 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
1dd85 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 43 e];. idx = ++pC
1dd86 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
1dd87 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 iPage];. assert
1dd88 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 ( pPage->isInit
1dd89 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
1dd8a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
1dd8b 20 63 6f 72 72 75 70 74 2c 20 69 74 20 69 73 20 corrupt, it is
1dd8c 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 possible for the
1dd8d 20 76 61 6c 75 65 20 6f 66 20 69 64 78 20 0a 20 value of idx .
1dd8e 20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 ** to be invali
1dd8f 64 20 68 65 72 65 2e 20 54 68 69 73 20 63 61 6e d here. This can
1dd90 20 6f 6e 6c 79 20 6f 63 63 75 72 20 69 66 20 61 only occur if a
1dd91 20 73 65 63 6f 6e 64 20 63 75 72 73 6f 72 20 6d second cursor m
1dd92 6f 64 69 66 69 65 73 0a 20 20 2a 2a 20 74 68 65 odifies. ** the
1dd93 20 70 61 67 65 20 77 68 69 6c 65 20 63 75 72 73 page while curs
1dd94 6f 72 20 70 43 75 72 20 69 73 20 68 6f 6c 64 69 or pCur is holdi
1dd95 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 ng a reference t
1dd96 6f 20 69 74 2e 20 57 68 69 63 68 20 63 61 6e 0a o it. Which can.
1dd97 20 20 2a 2a 20 6f 6e 6c 79 20 68 61 70 70 65 6e ** only happen
1dd98 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 if the database
1dd99 20 69 73 20 63 6f 72 72 75 70 74 20 69 6e 20 73 is corrupt in s
1dd9a 75 63 68 20 61 20 77 61 79 20 61 73 20 74 6f 20 uch a way as to
1dd9b 6c 69 6e 6b 20 74 68 65 0a 20 20 2a 2a 20 70 61 link the. ** pa
1dd9c 67 65 20 69 6e 74 6f 20 6d 6f 72 65 20 74 68 61 ge into more tha
1dd9d 6e 20 6f 6e 65 20 62 2d 74 72 65 65 20 73 74 72 n one b-tree str
1dd9e 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 74 65 73 ucture. */. tes
1dd9f 74 63 61 73 65 28 20 69 64 78 3e 70 50 61 67 65 tcase( idx>pPage
1dda0 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 70 43 ->nCell );.. pC
1dda1 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d ur->info.nSize =
1dda2 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 0;. pCur->vali
1dda3 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 dNKey = 0;. if(
1dda4 20 69 64 78 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 idx>=pPage->nCe
1dda5 6c 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70 ll ){. if( !p
1dda6 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
1dda7 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 rc = moveToC
1dda8 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 hild(pCur, get4b
1dda9 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 yte(&pPage->aDat
1ddaa 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 a[pPage->hdrOffs
1ddab 65 74 2b 38 5d 29 29 3b 0a 20 20 20 20 20 20 69 et+8]));. i
1ddac 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1ddad 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f c;. rc = mo
1ddae 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 veToLeftmost(pCu
1ddaf 72 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 r);. *pRes
1ddb0 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 = 0;. retur
1ddb1 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
1ddb2 64 6f 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 do{. if( pC
1ddb3 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a ur->iPage==0 ){.
1ddb4 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 *pRes =
1ddb5 31 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 1;. pCur-
1ddb6 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
1ddb7 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 _INVALID;.
1ddb8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1ddb9 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 OK;. }.
1ddba 20 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 moveToParent(p
1ddbb 43 75 72 29 3b 0a 20 20 20 20 20 20 70 50 61 67 Cur);. pPag
1ddbc 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
1ddbd 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
1ddbe 20 20 20 7d 77 68 69 6c 65 28 20 70 43 75 72 2d }while( pCur-
1ddbf 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
1ddc0 67 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c ge]>=pPage->nCel
1ddc1 6c 20 29 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d l );. *pRes =
1ddc2 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 0;. if( pPag
1ddc3 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 e->intKey ){.
1ddc4 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1ddc5 74 72 65 65 4e 65 78 74 28 70 43 75 72 2c 20 70 treeNext(pCur, p
1ddc6 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Res);. }else{
1ddc7 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1ddc8 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 TE_OK;. }.
1ddc9 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
1ddca 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 69 *pRes = 0;. i
1ddcb 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 f( pPage->leaf )
1ddcc 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1ddcd 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 ITE_OK;. }. rc
1ddce 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 = moveToLeftmos
1ddcf 74 28 70 43 75 72 29 3b 0a 20 20 72 65 74 75 72 t(pCur);. retur
1ddd0 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 n rc;.}.../*.**
1ddd1 53 74 65 70 20 74 68 65 20 63 75 72 73 6f 72 20 Step the cursor
1ddd2 74 6f 20 74 68 65 20 62 61 63 6b 20 74 6f 20 74 to the back to t
1ddd3 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 he previous entr
1ddd4 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 y in the databas
1ddd5 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 e. If.** succes
1ddd6 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 sful then set *p
1ddd7 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 20 63 Res=0. If the c
1ddd8 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 ursor.** was alr
1ddd9 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f eady pointing to
1ddda 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 the first entry
1dddb 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1dddc 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 before.** this
1dddd 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c routine was call
1ddde 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 ed, then set *pR
1dddf 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f es=1..*/.SQLITE_
1dde0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1dde1 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 te3BtreePrevious
1dde2 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
1dde3 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 int *pRes){. i
1dde4 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 nt rc;. MemPage
1dde5 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 *pPage;.. asse
1dde6 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
1dde7 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
1dde8 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 rc = restoreCurs
1dde9 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 orPosition(pCur)
1ddea 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
1ddeb 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 TE_OK ){. ret
1ddec 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 43 urn rc;. }. pC
1dded 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a ur->atLast = 0;.
1ddee 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 if( CURSOR_INV
1ddef 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 ALID==pCur->eSta
1ddf0 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 te ){. *pRes
1ddf1 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 1;. return
1ddf2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1ddf3 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e if( pCur->skipN
1ddf4 65 78 74 3c 30 20 29 7b 0a 20 20 20 20 70 43 75 ext<0 ){. pCu
1ddf5 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b r->skipNext = 0;
1ddf6 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a . *pRes = 0;.
1ddf7 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1ddf8 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 E_OK;. }. pCur
1ddf9 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a ->skipNext = 0;.
1ddfa 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d . pPage = pCur-
1ddfb 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
1ddfc 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 age];. assert(
1ddfd 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b pPage->isInit );
1ddfe 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c . if( !pPage->l
1ddff 65 61 66 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 eaf ){. int i
1de00 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 dx = pCur->aiIdx
1de01 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
1de02 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 rc = moveToCh
1de03 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 ild(pCur, get4by
1de04 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 te(findCell(pPag
1de05 65 2c 20 69 64 78 29 29 29 3b 0a 20 20 20 20 69 e, idx)));. i
1de06 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 f( rc ){. r
1de07 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
1de08 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 rc = moveToR
1de09 69 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a ightmost(pCur);.
1de0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 }else{. whi
1de0b 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b le( pCur->aiIdx[
1de0c 70 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 30 20 pCur->iPage]==0
1de0d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 ){. if( pCu
1de0e 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 r->iPage==0 ){.
1de0f 20 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 pCur->eSt
1de10 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
1de11 41 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 2a 70 ALID;. *p
1de12 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 Res = 1;.
1de13 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1de14 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 K;. }.
1de15 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43 moveToParent(pC
1de16 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ur);. }. p
1de17 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cur->info.nSize
1de18 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 = 0;. pCur->v
1de19 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 alidNKey = 0;..
1de1a 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 pCur->aiIdx[p
1de1b 43 75 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b 0a 20 Cur->iPage]--;.
1de1c 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d pPage = pCur-
1de1d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
1de1e 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 70 50 age];. if( pP
1de1f 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 age->intKey && !
1de20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
1de21 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1de22 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 3BtreePrevious(p
1de23 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 Cur, pRes);.
1de24 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
1de25 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
1de26 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d }. }. *pRes =
1de27 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0;. return rc;
1de28 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
1de29 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 66 72 te a new page fr
1de2a 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
1de2b 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 file..**.** The
1de2c 6e 65 77 20 70 61 67 65 20 69 73 20 6d 61 72 6b new page is mark
1de2d 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 28 49 ed as dirty. (I
1de2e 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 73 n other words, s
1de2f 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
1de30 28 29 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 ().** has alread
1de31 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e y been called on
1de32 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 29 20 the new page.)
1de33 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 68 61 The new page ha
1de34 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e 20 72 s also.** been r
1de35 65 66 65 72 65 6e 63 65 64 20 61 6e 64 20 74 68 eferenced and th
1de36 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e e calling routin
1de37 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 e is responsible
1de38 20 66 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 for calling.**
1de39 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
1de3a 66 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20 70 f() on the new p
1de3b 61 67 65 20 77 68 65 6e 20 69 74 20 69 73 20 64 age when it is d
1de3c 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 one..**.** SQLIT
1de3d 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
1de3e 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 41 6e on success. An
1de3f 79 20 6f 74 68 65 72 20 72 65 74 75 72 6e 20 76 y other return v
1de40 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 0a 2a alue indicates.*
1de41 2a 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a 70 70 * an error. *pp
1de42 50 61 67 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 Page and *pPgno
1de43 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 69 6e are undefined in
1de44 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 6e the event of an
1de45 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20 6e 6f error..** Do no
1de46 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 t invoke sqlite3
1de47 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 PagerUnref() on
1de48 2a 70 70 50 61 67 65 20 69 66 20 61 6e 20 65 72 *ppPage if an er
1de49 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ror is returned.
1de4a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 6e .**.** If the "n
1de4b 65 61 72 62 79 22 20 70 61 72 61 6d 65 74 65 72 earby" parameter
1de4c 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 is not 0, then
1de4d 61 20 28 66 65 65 62 6c 65 29 20 65 66 66 6f 72 a (feeble) effor
1de4e 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a 2a 2a t is made to .**
1de4f 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65 20 63 locate a page c
1de50 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61 67 65 lose to the page
1de51 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62 79 22 number "nearby"
1de52 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 . This can be u
1de53 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 74 74 sed in an.** att
1de54 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 65 6c empt to keep rel
1de55 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f 73 65 ated pages close
1de56 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 20 69 to each other i
1de57 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
1de58 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69 6e ile,.** which in
1de59 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20 64 turn can make d
1de5a 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 66 atabase access f
1de5b 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 aster..**.** If
1de5c 74 68 65 20 22 65 78 61 63 74 22 20 70 61 72 61 the "exact" para
1de5d 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 meter is not 0,
1de5e 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e 75 6d and the page-num
1de5f 62 65 72 20 6e 65 61 72 62 79 20 65 78 69 73 74 ber nearby exist
1de60 73 20 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20 6f s .** anywhere o
1de61 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c n the free-list,
1de62 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 61 72 then it is guar
1de63 65 6e 74 65 65 64 20 74 6f 20 62 65 20 72 65 74 enteed to be ret
1de64 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20 69 urned. This.** i
1de65 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 61 s only used by a
1de66 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 uto-vacuum datab
1de67 61 73 65 73 20 77 68 65 6e 20 61 6c 6c 6f 63 61 ases when alloca
1de68 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65 ting a new table
1de69 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1de6a 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 allocateBtreePag
1de6b 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e(. BtShared *p
1de6c 42 74 2c 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a Bt, . MemPage *
1de6d 2a 70 70 50 61 67 65 2c 20 0a 20 20 50 67 6e 6f *ppPage, . Pgno
1de6e 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 50 67 6e 6f *pPgno, . Pgno
1de6f 20 6e 65 61 72 62 79 2c 0a 20 20 75 38 20 65 78 nearby,. u8 ex
1de70 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 act.){. MemPage
1de71 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74 20 *pPage1;. int
1de72 72 63 3b 0a 20 20 75 33 32 20 6e 3b 20 20 20 20 rc;. u32 n;
1de73 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
1de74 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c ges on the freel
1de75 69 73 74 20 2a 2f 0a 20 20 75 33 32 20 6b 3b 20 ist */. u32 k;
1de76 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1de77 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65 20 74 leaves on the t
1de78 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72 65 65 runk of the free
1de79 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 list */. MemPag
1de7a 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 e *pTrunk = 0;.
1de7b 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65 76 54 MemPage *pPrevT
1de7c 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 50 67 6e 6f runk = 0;. Pgno
1de7d 20 6d 78 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 mxPage; /*
1de7e 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 Total size of th
1de7f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1de80 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 */.. assert( sq
1de81 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1de82 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
1de83 20 20 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e pPage1 = pBt->
1de84 70 50 61 67 65 31 3b 0a 20 20 6d 78 50 61 67 65 pPage1;. mxPage
1de85 20 3d 20 62 74 72 65 65 50 61 67 65 63 6f 75 6e = btreePagecoun
1de86 74 28 70 42 74 29 3b 0a 20 20 6e 20 3d 20 67 65 t(pBt);. n = ge
1de87 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e t4byte(&pPage1->
1de88 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 74 65 aData[36]);. te
1de89 73 74 63 61 73 65 28 20 6e 3d 3d 6d 78 50 61 67 stcase( n==mxPag
1de8a 65 2d 31 20 29 3b 0a 20 20 69 66 28 20 6e 3e 3d e-1 );. if( n>=
1de8b 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20 72 65 mxPage ){. re
1de8c 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1de8d 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 UPT_BKPT;. }.
1de8e 69 66 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20 2f if( n>0 ){. /
1de8f 2a 20 54 68 65 72 65 20 61 72 65 20 70 61 67 65 * There are page
1de90 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 s on the freelis
1de91 74 2e 20 20 52 65 75 73 65 20 6f 6e 65 20 6f 66 t. Reuse one of
1de92 20 74 68 6f 73 65 20 70 61 67 65 73 2e 20 2a 2f those pages. */
1de93 0a 20 20 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b . Pgno iTrunk
1de94 3b 0a 20 20 20 20 75 38 20 73 65 61 72 63 68 4c ;. u8 searchL
1de95 69 73 74 20 3d 20 30 3b 20 2f 2a 20 49 66 20 74 ist = 0; /* If t
1de96 68 65 20 66 72 65 65 2d 6c 69 73 74 20 6d 75 73 he free-list mus
1de97 74 20 62 65 20 73 65 61 72 63 68 65 64 20 66 6f t be searched fo
1de98 72 20 27 6e 65 61 72 62 79 27 20 2a 2f 0a 20 20 r 'nearby' */.
1de99 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
1de9a 20 27 65 78 61 63 74 27 20 70 61 72 61 6d 65 74 'exact' paramet
1de9b 65 72 20 77 61 73 20 74 72 75 65 20 61 6e 64 20 er was true and
1de9c 61 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 70 a query of the p
1de9d 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a ointer-map. *
1de9e 2a 20 73 68 6f 77 73 20 74 68 61 74 20 74 68 65 * shows that the
1de9f 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 69 page 'nearby' i
1dea0 73 20 73 6f 6d 65 77 68 65 72 65 20 6f 6e 20 74 s somewhere on t
1dea1 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 he free-list, th
1dea2 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 6e en. ** the en
1dea3 74 69 72 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62 tire-list will b
1dea4 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 74 e searched for t
1dea5 68 61 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f hat page.. */
1dea6 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1dea7 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
1dea8 20 20 20 20 69 66 28 20 65 78 61 63 74 20 26 26 if( exact &&
1dea9 20 6e 65 61 72 62 79 3c 3d 6d 78 50 61 67 65 20 nearby<=mxPage
1deaa 29 7b 0a 20 20 20 20 20 20 75 38 20 65 54 79 70 ){. u8 eTyp
1deab 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 e;. assert(
1deac 20 6e 65 61 72 62 79 3e 30 20 29 3b 0a 20 20 20 nearby>0 );.
1dead 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
1deae 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 autoVacuum );.
1deaf 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 rc = ptrmapG
1deb0 65 74 28 70 42 74 2c 20 6e 65 61 72 62 79 2c 20 et(pBt, nearby,
1deb1 26 65 54 79 70 65 2c 20 30 29 3b 0a 20 20 20 20 &eType, 0);.
1deb2 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
1deb3 6e 20 72 63 3b 0a 20 20 20 20 20 20 69 66 28 20 n rc;. if(
1deb4 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 eType==PTRMAP_FR
1deb5 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 EEPAGE ){.
1deb6 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 31 searchList = 1
1deb7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1deb8 2a 70 50 67 6e 6f 20 3d 20 6e 65 61 72 62 79 3b *pPgno = nearby;
1deb9 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
1deba 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 /* Decrement
1debb 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 63 6f the free-list co
1debc 75 6e 74 20 62 79 20 31 2e 20 53 65 74 20 69 54 unt by 1. Set iT
1debd 72 75 6e 6b 20 74 6f 20 74 68 65 20 69 6e 64 65 runk to the inde
1debe 78 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 x of the. **
1debf 66 69 72 73 74 20 66 72 65 65 2d 6c 69 73 74 20 first free-list
1dec0 74 72 75 6e 6b 20 70 61 67 65 2e 20 69 50 72 65 trunk page. iPre
1dec1 76 54 72 75 6e 6b 20 69 73 20 69 6e 69 74 69 61 vTrunk is initia
1dec2 6c 6c 79 20 31 2e 0a 20 20 20 20 2a 2f 0a 20 20 lly 1.. */.
1dec3 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1dec4 67 65 72 57 72 69 74 65 28 70 50 61 67 65 31 2d gerWrite(pPage1-
1dec5 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 >pDbPage);. i
1dec6 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1dec7 63 3b 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 c;. put4byte(
1dec8 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
1dec9 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 20 20 20 2f 6], n-1);.. /
1deca 2a 20 54 68 65 20 63 6f 64 65 20 77 69 74 68 69 * The code withi
1decb 6e 20 74 68 69 73 20 6c 6f 6f 70 20 69 73 20 72 n this loop is r
1decc 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 20 69 66 20 un only once if
1decd 74 68 65 20 27 73 65 61 72 63 68 4c 69 73 74 27 the 'searchList'
1dece 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a variable. **
1decf 20 69 73 20 6e 6f 74 20 74 72 75 65 2e 20 4f 74 is not true. Ot
1ded0 68 65 72 77 69 73 65 2c 20 69 74 20 72 75 6e 73 herwise, it runs
1ded1 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74 once for each t
1ded2 72 75 6e 6b 2d 70 61 67 65 20 6f 6e 20 74 68 65 runk-page on the
1ded3 0a 20 20 20 20 2a 2a 20 66 72 65 65 2d 6c 69 73 . ** free-lis
1ded4 74 20 75 6e 74 69 6c 20 74 68 65 20 70 61 67 65 t until the page
1ded5 20 27 6e 65 61 72 62 79 27 20 69 73 20 6c 6f 63 'nearby' is loc
1ded6 61 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ated.. */.
1ded7 20 64 6f 20 7b 0a 20 20 20 20 20 20 70 50 72 65 do {. pPre
1ded8 76 54 72 75 6e 6b 20 3d 20 70 54 72 75 6e 6b 3b vTrunk = pTrunk;
1ded9 0a 20 20 20 20 20 20 69 66 28 20 70 50 72 65 76 . if( pPrev
1deda 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 Trunk ){.
1dedb 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 iTrunk = get4by
1dedc 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e te(&pPrevTrunk->
1dedd 61 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 aData[0]);.
1dede 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1dedf 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 iTrunk = get4byt
1dee0 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
1dee1 5b 33 32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 [32]);. }.
1dee2 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 testcase( i
1dee3 54 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b Trunk==mxPage );
1dee4 0a 20 20 20 20 20 20 69 66 28 20 69 54 72 75 6e . if( iTrun
1dee5 6b 3e 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20 k>mxPage ){.
1dee6 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1dee7 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1dee8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1dee9 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 rc = btreeGet
1deea 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e 6b Page(pBt, iTrunk
1deeb 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 , &pTrunk, 0);.
1deec 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1deed 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 70 rc ){. p
1deee 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 Trunk = 0;.
1deef 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f goto end_allo
1def0 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 cate_page;.
1def1 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1def2 20 70 54 72 75 6e 6b 21 3d 30 20 29 3b 0a 20 20 pTrunk!=0 );.
1def3 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72 75 assert( pTru
1def4 6e 6b 2d 3e 61 44 61 74 61 21 3d 30 20 29 3b 0a nk->aData!=0 );.
1def5 0a 20 20 20 20 20 20 6b 20 3d 20 67 65 74 34 62 . k = get4b
1def6 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 yte(&pTrunk->aDa
1def7 74 61 5b 34 5d 29 3b 20 2f 2a 20 23 20 6f 66 20 ta[4]); /* # of
1def8 6c 65 61 76 65 73 20 6f 6e 20 74 68 69 73 20 74 leaves on this t
1def9 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 20 20 20 runk page */.
1defa 20 20 20 69 66 28 20 6b 3d 3d 30 20 26 26 20 21 if( k==0 && !
1defb 73 65 61 72 63 68 4c 69 73 74 20 29 7b 0a 20 20 searchList ){.
1defc 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 /* The tru
1defd 6e 6b 20 68 61 73 20 6e 6f 20 6c 65 61 76 65 73 nk has no leaves
1defe 20 61 6e 64 20 74 68 65 20 6c 69 73 74 20 69 73 and the list is
1deff 20 6e 6f 74 20 62 65 69 6e 67 20 73 65 61 72 63 not being searc
1df00 68 65 64 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a hed. . **
1df01 20 53 6f 20 65 78 74 72 61 63 74 20 74 68 65 20 So extract the
1df02 74 72 75 6e 6b 20 70 61 67 65 20 69 74 73 65 6c trunk page itsel
1df03 66 20 61 6e 64 20 75 73 65 20 69 74 20 61 73 20 f and use it as
1df04 74 68 65 20 6e 65 77 6c 79 20 0a 20 20 20 20 20 the newly .
1df05 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 ** allocated
1df06 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 page */.
1df07 61 73 73 65 72 74 28 20 70 50 72 65 76 54 72 75 assert( pPrevTru
1df08 6e 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 nk==0 );.
1df09 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1df0a 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e erWrite(pTrunk->
1df0b 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
1df0c 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
1df0d 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 goto end_a
1df0e 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 llocate_page;.
1df0f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1df10 2a 70 50 67 6e 6f 20 3d 20 69 54 72 75 6e 6b 3b *pPgno = iTrunk;
1df11 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
1df12 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
1df13 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 2], &pTrunk->aDa
1df14 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 ta[0], 4);.
1df15 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72 *ppPage = pTr
1df16 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 70 54 72 unk;. pTr
1df17 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 unk = 0;.
1df18 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 TRACE(("ALLOCAT
1df19 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 E: %d trunk - %d
1df1a 20 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 74 free pages left
1df1b 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 \n", *pPgno, n-1
1df1c 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 ));. }else
1df1d 69 66 28 20 6b 3e 28 75 33 32 29 28 70 42 74 2d if( k>(u32)(pBt-
1df1e 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 >usableSize/4 -
1df1f 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 2) ){. /*
1df20 20 56 61 6c 75 65 20 6f 66 20 6b 20 69 73 20 6f Value of k is o
1df21 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 44 61 ut of range. Da
1df22 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f tabase corruptio
1df23 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 n */. rc
1df24 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
1df25 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 67 _BKPT;. g
1df26 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 oto end_allocate
1df27 5f 70 61 67 65 3b 0a 23 69 66 6e 64 65 66 20 53 _page;.#ifndef S
1df28 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
1df29 41 43 55 55 4d 0a 20 20 20 20 20 20 7d 65 6c 73 ACUUM. }els
1df2a 65 20 69 66 28 20 73 65 61 72 63 68 4c 69 73 74 e if( searchList
1df2b 20 26 26 20 6e 65 61 72 62 79 3d 3d 69 54 72 75 && nearby==iTru
1df2c 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a nk ){. /*
1df2d 20 54 68 65 20 6c 69 73 74 20 69 73 20 62 65 69 The list is bei
1df2e 6e 67 20 73 65 61 72 63 68 65 64 20 61 6e 64 20 ng searched and
1df2f 74 68 69 73 20 74 72 75 6e 6b 20 70 61 67 65 20 this trunk page
1df30 69 73 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 is the page.
1df31 20 20 20 20 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 ** to alloca
1df32 74 65 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f te, regardless o
1df33 66 20 77 68 65 74 68 65 72 20 69 74 20 68 61 73 f whether it has
1df34 20 6c 65 61 76 65 73 2e 0a 20 20 20 20 20 20 20 leaves..
1df35 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 */. asse
1df36 72 74 28 20 2a 70 50 67 6e 6f 3d 3d 69 54 72 75 rt( *pPgno==iTru
1df37 6e 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 nk );. *p
1df38 70 50 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a pPage = pTrunk;.
1df39 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 searchLi
1df3a 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 st = 0;.
1df3b 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1df3c 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 rWrite(pTrunk->p
1df3d 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
1df3e 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
1df3f 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c goto end_al
1df40 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 locate_page;.
1df41 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1df42 66 28 20 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 f( k==0 ){.
1df43 20 20 20 20 20 69 66 28 20 21 70 50 72 65 76 54 if( !pPrevT
1df44 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 runk ){.
1df45 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 memcpy(&pPag
1df46 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 e1->aData[32], &
1df47 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d pTrunk->aData[0]
1df48 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 4);.
1df49 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
1df4a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1df4b 61 67 65 72 57 72 69 74 65 28 70 50 72 65 76 54 agerWrite(pPrevT
1df4c 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a runk->pDbPage);.
1df4d 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
1df4e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1df4f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 . g
1df50 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 oto end_allocate
1df51 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 _page;.
1df52 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
1df53 20 6d 65 6d 63 70 79 28 26 70 50 72 65 76 54 72 memcpy(&pPrevTr
1df54 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 unk->aData[0], &
1df55 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d pTrunk->aData[0]
1df56 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 4);.
1df57 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
1df58 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 . /* Th
1df59 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 e trunk page is
1df5a 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 required by the
1df5b 63 61 6c 6c 65 72 20 62 75 74 20 69 74 20 63 6f caller but it co
1df5c 6e 74 61 69 6e 73 20 0a 20 20 20 20 20 20 20 20 ntains .
1df5d 20 20 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f ** pointers to
1df5e 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 76 65 free-list leave
1df5f 73 2e 20 54 68 65 20 66 69 72 73 74 20 6c 65 61 s. The first lea
1df60 66 20 62 65 63 6f 6d 65 73 20 61 20 74 72 75 6e f becomes a trun
1df61 6b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 k. ** p
1df62 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 age in this case
1df63 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 .. */.
1df64 20 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 MemPage
1df65 20 2a 70 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20 *pNewTrunk;.
1df66 20 20 20 20 20 20 20 50 67 6e 6f 20 69 4e 65 77 Pgno iNew
1df67 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 Trunk = get4byte
1df68 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b (&pTrunk->aData[
1df69 38 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 8]);. i
1df6a 66 28 20 69 4e 65 77 54 72 75 6e 6b 3e 6d 78 50 f( iNewTrunk>mxP
1df6b 61 67 65 20 29 7b 20 0a 20 20 20 20 20 20 20 20 age ){ .
1df6c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1df6d 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1df6e 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
1df6f 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 nd_allocate_page
1df70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1df71 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
1df72 28 20 69 4e 65 77 54 72 75 6e 6b 3d 3d 6d 78 50 ( iNewTrunk==mxP
1df73 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 age );.
1df74 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 rc = btreeGetPa
1df75 67 65 28 70 42 74 2c 20 69 4e 65 77 54 72 75 6e ge(pBt, iNewTrun
1df76 6b 2c 20 26 70 4e 65 77 54 72 75 6e 6b 2c 20 30 k, &pNewTrunk, 0
1df77 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
1df78 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1df79 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f {. go
1df7a 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f to end_allocate_
1df7b 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 page;.
1df7c 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d }. rc =
1df7d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
1df7e 74 65 28 70 4e 65 77 54 72 75 6e 6b 2d 3e 70 44 te(pNewTrunk->pD
1df7f 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
1df80 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1df81 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
1df82 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1df83 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 NewTrunk);.
1df84 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
1df85 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
1df86 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1df87 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 memcpy(&pNe
1df88 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d wTrunk->aData[0]
1df89 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 , &pTrunk->aData
1df8a 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 [0], 4);.
1df8b 20 20 20 70 75 74 34 62 79 74 65 28 26 70 4e 65 put4byte(&pNe
1df8c 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d wTrunk->aData[4]
1df8d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 , k-1);.
1df8e 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54 72 memcpy(&pNewTr
1df8f 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 26 unk->aData[8], &
1df90 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 31 32 pTrunk->aData[12
1df91 5d 2c 20 28 6b 2d 31 29 2a 34 29 3b 0a 20 20 20 ], (k-1)*4);.
1df92 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
1df93 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 ge(pNewTrunk);.
1df94 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 50 if( !pP
1df95 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 revTrunk ){.
1df96 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1df97 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
1df98 69 74 65 61 62 6c 65 28 70 50 61 67 65 31 2d 3e iteable(pPage1->
1df99 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 pDbPage) );.
1df9a 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
1df9b 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
1df9c 33 32 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 32], iNewTrunk);
1df9d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
1df9e 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 {. rc
1df9f 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
1dfa0 72 69 74 65 28 70 50 72 65 76 54 72 75 6e 6b 2d rite(pPrevTrunk-
1dfa1 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
1dfa2 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b if( rc ){
1dfa3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 . g
1dfa4 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 oto end_allocate
1dfa5 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 _page;.
1dfa6 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
1dfa7 20 70 75 74 34 62 79 74 65 28 26 70 50 72 65 76 put4byte(&pPrev
1dfa8 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c Trunk->aData[0],
1dfa9 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 iNewTrunk);.
1dfaa 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1dfab 20 7d 0a 20 20 20 20 20 20 20 20 70 54 72 75 6e }. pTrun
1dfac 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 54 k = 0;. T
1dfad 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a RACE(("ALLOCATE:
1dfae 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20 66 %d trunk - %d f
1dfaf 72 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c 6e ree pages left\n
1dfb0 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 ", *pPgno, n-1))
1dfb1 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7d ;.#endif. }
1dfb2 65 6c 73 65 20 69 66 28 20 6b 3e 30 20 29 7b 0a else if( k>0 ){.
1dfb3 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 /* Extra
1dfb4 63 74 20 61 20 6c 65 61 66 20 66 72 6f 6d 20 74 ct a leaf from t
1dfb5 68 65 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 he trunk */.
1dfb6 20 20 20 20 75 33 32 20 63 6c 6f 73 65 73 74 3b u32 closest;
1dfb7 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 50 . Pgno iP
1dfb8 61 67 65 3b 0a 20 20 20 20 20 20 20 20 75 6e 73 age;. uns
1dfb9 69 67 6e 65 64 20 63 68 61 72 20 2a 61 44 61 74 igned char *aDat
1dfba 61 20 3d 20 70 54 72 75 6e 6b 2d 3e 61 44 61 74 a = pTrunk->aDat
1dfbb 61 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e a;. if( n
1dfbc 65 61 72 62 79 3e 30 20 29 7b 0a 20 20 20 20 20 earby>0 ){.
1dfbd 20 20 20 20 20 75 33 32 20 69 3b 0a 20 20 20 20 u32 i;.
1dfbe 20 20 20 20 20 20 69 6e 74 20 64 69 73 74 3b 0a int dist;.
1dfbf 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 closes
1dfc0 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 t = 0;.
1dfc1 20 64 69 73 74 20 3d 20 73 71 6c 69 74 65 33 41 dist = sqlite3A
1dfc2 62 73 49 6e 74 33 32 28 67 65 74 34 62 79 74 65 bsInt32(get4byte
1dfc3 28 26 61 44 61 74 61 5b 38 5d 29 20 2d 20 6e 65 (&aData[8]) - ne
1dfc4 61 72 62 79 29 3b 0a 20 20 20 20 20 20 20 20 20 arby);.
1dfc5 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 for(i=1; i<k; i
1dfc6 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ++){.
1dfc7 20 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65 int d2 = sqlite
1dfc8 33 41 62 73 49 6e 74 33 32 28 67 65 74 34 62 79 3AbsInt32(get4by
1dfc9 74 65 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d te(&aData[8+i*4]
1dfca 29 20 2d 20 6e 65 61 72 62 79 29 3b 0a 20 20 20 ) - nearby);.
1dfcb 20 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c if( d2<
1dfcc 64 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 dist ){.
1dfcd 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 closest =
1dfce 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 i;.
1dfcf 20 64 69 73 74 20 3d 20 64 32 3b 0a 20 20 20 20 dist = d2;.
1dfd0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1dfd1 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 }. }e
1dfd2 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 lse{. c
1dfd3 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 losest = 0;.
1dfd4 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 }.. i
1dfd5 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 Page = get4byte(
1dfd6 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 &aData[8+closest
1dfd7 2a 34 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 *4]);. te
1dfd8 73 74 63 61 73 65 28 20 69 50 61 67 65 3d 3d 6d stcase( iPage==m
1dfd9 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20 xPage );.
1dfda 20 69 66 28 20 69 50 61 67 65 3e 6d 78 50 61 67 if( iPage>mxPag
1dfdb 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 e ){. r
1dfdc 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
1dfdd 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 PT_BKPT;.
1dfde 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f goto end_allo
1dfdf 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 cate_page;.
1dfe0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 74 65 73 }. tes
1dfe1 74 63 61 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 tcase( iPage==mx
1dfe2 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20 Page );.
1dfe3 69 66 28 20 21 73 65 61 72 63 68 4c 69 73 74 20 if( !searchList
1dfe4 7c 7c 20 69 50 61 67 65 3d 3d 6e 65 61 72 62 79 || iPage==nearby
1dfe5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e ){. in
1dfe6 74 20 6e 6f 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 t noContent;.
1dfe7 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 *pPgno =
1dfe8 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 iPage;.
1dfe9 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 TRACE(("ALLOCAT
1dfea 45 3a 20 25 64 20 77 61 73 20 6c 65 61 66 20 25 E: %d was leaf %
1dfeb 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 75 6e 6b d of %d on trunk
1dfec 20 25 64 22 0a 20 20 20 20 20 20 20 20 20 20 20 %d".
1dfed 20 20 20 20 20 20 22 3a 20 25 64 20 6d 6f 72 65 ": %d more
1dfee 20 66 72 65 65 20 70 61 67 65 73 5c 6e 22 2c 0a free pages\n",.
1dfef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dff0 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 65 73 74 *pPgno, closest
1dff1 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e 70 +1, k, pTrunk->p
1dff2 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 gno, n-1));.
1dff3 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1dff4 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 e3PagerWrite(pTr
1dff5 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 unk->pDbPage);.
1dff6 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 if( rc
1dff7 29 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 ) goto end_alloc
1dff8 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 ate_page;.
1dff9 20 20 20 20 69 66 28 20 63 6c 6f 73 65 73 74 3c if( closest<
1dffa 6b 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 k-1 ){.
1dffb 20 20 20 6d 65 6d 63 70 79 28 26 61 44 61 74 61 memcpy(&aData
1dffc 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 [8+closest*4], &
1dffd 61 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 aData[4+k*4], 4)
1dffe 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1dfff 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
1e000 28 26 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 (&aData[4], k-1)
1e001 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 43 6f ;. noCo
1e002 6e 74 65 6e 74 20 3d 20 21 62 74 72 65 65 47 65 ntent = !btreeGe
1e003 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c tHasContent(pBt,
1e004 20 2a 70 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20 *pPgno);.
1e005 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 rc = btreeGe
1e006 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e tPage(pBt, *pPgn
1e007 6f 2c 20 70 70 50 61 67 65 2c 20 6e 6f 43 6f 6e o, ppPage, noCon
1e008 74 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 tent);.
1e009 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1e00a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
1e00b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1e00c 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 61 67 gerWrite((*ppPag
1e00d 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 e)->pDbPage);.
1e00e 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
1e00f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1e010 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c rel
1e011 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65 easePage(*ppPage
1e012 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d );. }
1e013 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1e014 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 searchLis
1e015 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d t = 0;. }
1e016 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
1e017 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 65 76 eleasePage(pPrev
1e018 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 70 50 Trunk);. pP
1e019 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 revTrunk = 0;.
1e01a 20 20 7d 77 68 69 6c 65 28 20 73 65 61 72 63 68 }while( search
1e01b 4c 69 73 74 20 29 3b 0a 20 20 7d 65 6c 73 65 7b List );. }else{
1e01c 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72 . /* There ar
1e01d 65 20 6e 6f 20 70 61 67 65 73 20 6f 6e 20 74 68 e no pages on th
1e01e 65 20 66 72 65 65 6c 69 73 74 2c 20 73 6f 20 63 e freelist, so c
1e01f 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 reate a new page
1e020 20 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 at the. ** e
1e021 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a nd of the file *
1e022 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
1e023 65 33 50 61 67 65 72 57 72 69 74 65 28 70 42 74 e3PagerWrite(pBt
1e024 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 ->pPage1->pDbPag
1e025 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 e);. if( rc )
1e026 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1e027 70 42 74 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20 pBt->nPage++;.
1e028 20 20 69 66 28 20 70 42 74 2d 3e 6e 50 61 67 65 if( pBt->nPage
1e029 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 ==PENDING_BYTE_P
1e02a 41 47 45 28 70 42 74 29 20 29 20 70 42 74 2d 3e AGE(pBt) ) pBt->
1e02b 6e 50 61 67 65 2b 2b 3b 0a 0a 23 69 66 6e 64 65 nPage++;..#ifnde
1e02c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1e02d 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
1e02e 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
1e02f 20 26 26 20 50 54 52 4d 41 50 5f 49 53 50 41 47 && PTRMAP_ISPAG
1e030 45 28 70 42 74 2c 20 70 42 74 2d 3e 6e 50 61 67 E(pBt, pBt->nPag
1e031 65 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 e) ){. /* I
1e032 66 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20 f *pPgno refers
1e033 74 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 to a pointer-map
1e034 20 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 page, allocate
1e035 74 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20 two new pages.
1e036 20 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e ** at the en
1e037 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e d of the file in
1e038 73 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 stead of one. Th
1e039 65 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 e first allocate
1e03a 64 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 d page. **
1e03b 62 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f becomes a new po
1e03c 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 inter-map page,
1e03d 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73 the second is us
1e03e 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 ed by the caller
1e03f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1e040 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 MemPage *pPg =
1e041 30 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 0;. TRACE((
1e042 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 "ALLOCATE: %d fr
1e043 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 om end of file (
1e044 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 pointer-map page
1e045 29 5c 6e 22 2c 20 70 42 74 2d 3e 6e 50 61 67 65 )\n", pBt->nPage
1e046 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ));. assert
1e047 28 20 70 42 74 2d 3e 6e 50 61 67 65 21 3d 50 45 ( pBt->nPage!=PE
1e048 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
1e049 70 42 74 29 20 29 3b 0a 20 20 20 20 20 20 72 63 pBt) );. rc
1e04a 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
1e04b 70 42 74 2c 20 70 42 74 2d 3e 6e 50 61 67 65 2c pBt, pBt->nPage,
1e04c 20 26 70 50 67 2c 20 31 29 3b 0a 20 20 20 20 20 &pPg, 1);.
1e04d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1e04e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
1e04f 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
1e050 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50 61 67 rite(pPg->pDbPag
1e051 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 e);. rele
1e052 61 73 65 50 61 67 65 28 70 50 67 29 3b 0a 20 20 asePage(pPg);.
1e053 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1e054 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
1e055 20 20 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 pBt->nPage
1e056 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 ++;. if( pB
1e057 74 2d 3e 6e 50 61 67 65 3d 3d 50 45 4e 44 49 4e t->nPage==PENDIN
1e058 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
1e059 20 29 7b 20 70 42 74 2d 3e 6e 50 61 67 65 2b 2b ){ pBt->nPage++
1e05a 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 ; }. }.#endif
1e05b 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 32 38 . put4byte(28
1e05c 20 2b 20 28 75 38 2a 29 70 42 74 2d 3e 70 50 61 + (u8*)pBt->pPa
1e05d 67 65 31 2d 3e 61 44 61 74 61 2c 20 70 42 74 2d ge1->aData, pBt-
1e05e 3e 6e 50 61 67 65 29 3b 0a 20 20 20 20 2a 70 50 >nPage);. *pP
1e05f 67 6e 6f 20 3d 20 70 42 74 2d 3e 6e 50 61 67 65 gno = pBt->nPage
1e060 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a ;.. assert( *
1e061 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 pPgno!=PENDING_B
1e062 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b YTE_PAGE(pBt) );
1e063 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 . rc = btreeG
1e064 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 etPage(pBt, *pPg
1e065 6e 6f 2c 20 70 70 50 61 67 65 2c 20 31 29 3b 0a no, ppPage, 1);.
1e066 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
1e067 75 72 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 3d urn rc;. rc =
1e068 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
1e069 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 te((*ppPage)->pD
1e06a 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 bPage);. if(
1e06b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1e06c 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
1e06d 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 ge(*ppPage);.
1e06e 20 7d 0a 20 20 20 20 54 52 41 43 45 28 28 22 41 }. TRACE(("A
1e06f 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d LLOCATE: %d from
1e070 20 65 6e 64 20 6f 66 20 66 69 6c 65 5c 6e 22 2c end of file\n",
1e071 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a *pPgno));. }..
1e072 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f assert( *pPgno
1e073 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 !=PENDING_BYTE_P
1e074 41 47 45 28 70 42 74 29 20 29 3b 0a 0a 65 6e 64 AGE(pBt) );..end
1e075 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3a 0a _allocate_page:.
1e076 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 releasePage(pT
1e077 72 75 6e 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65 runk);. release
1e078 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 Page(pPrevTrunk)
1e079 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1e07a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
1e07b 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
1e07c 65 52 65 66 63 6f 75 6e 74 28 28 2a 70 70 50 61 eRefcount((*ppPa
1e07d 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 ge)->pDbPage)>1
1e07e 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 ){. release
1e07f 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 Page(*ppPage);.
1e080 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1e081 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1e082 0a 20 20 20 20 7d 0a 20 20 20 20 28 2a 70 70 50 . }. (*ppP
1e083 61 67 65 29 2d 3e 69 73 49 6e 69 74 20 3d 20 30 age)->isInit = 0
1e084 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a ;. }else{. *
1e085 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a ppPage = 0;. }.
1e086 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 assert( rc!=SQ
1e087 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 71 6c 69 74 LITE_OK || sqlit
1e088 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
1e089 6c 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 le((*ppPage)->pD
1e08a 62 50 61 67 65 29 20 29 3b 0a 20 20 72 65 74 75 bPage) );. retu
1e08b 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1e08c 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
1e08d 20 75 73 65 64 20 74 6f 20 61 64 64 20 70 61 67 used to add pag
1e08e 65 20 69 50 61 67 65 20 74 6f 20 74 68 65 20 64 e iPage to the d
1e08f 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 72 65 atabase file fre
1e090 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 20 69 e-list. .** It i
1e091 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 s assumed that t
1e092 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 he page is not a
1e093 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 6f 66 lready a part of
1e094 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a the free-list..
1e095 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 **.** The value
1e096 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
1e097 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f cond argument to
1e098 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
1e099 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a 20 49 s optional..** I
1e09a 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 70 f the caller hap
1e09b 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61 20 70 pens to have a p
1e09c 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65 ointer to the Me
1e09d 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a 2a 2a mPage object .**
1e09e 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
1e09f 6f 20 70 61 67 65 20 69 50 61 67 65 20 68 61 6e o page iPage han
1e0a0 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73 73 20 dy, it may pass
1e0a1 69 74 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 it as the second
1e0a2 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 68 65 value. .** Othe
1e0a3 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20 70 61 rwise, it may pa
1e0a4 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 ss NULL..**.** I
1e0a5 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 f a pointer to a
1e0a6 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 MemPage object
1e0a7 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 is passed as the
1e0a8 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
1e0a9 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 65 6e ,.** its referen
1e0aa 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f 74 20 ce count is not
1e0ab 61 6c 74 65 72 65 64 20 62 79 20 74 68 69 73 20 altered by this
1e0ac 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 function..*/.sta
1e0ad 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 65 tic int freePage
1e0ae 32 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 2(BtShared *pBt,
1e0af 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d 50 61 MemPage *pMemPa
1e0b0 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 29 7b ge, Pgno iPage){
1e0b1 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 . MemPage *pTru
1e0b2 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 nk = 0;
1e0b3 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 2d 6c /* Free-l
1e0b4 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a ist trunk page *
1e0b5 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b 20 /. Pgno iTrunk
1e0b6 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
1e0b7 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
1e0b8 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 2d 6c number of free-l
1e0b9 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a ist trunk page *
1e0ba 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 / . MemPage *pP
1e0bb 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 age1 = pBt->pPag
1e0bc 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 e1; /* Loca
1e0bd 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 l reference to p
1e0be 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d 50 61 age 1 */. MemPa
1e0bf 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 ge *pPage;
1e0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1e0c1 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66 72 65 * Page being fre
1e0c2 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 2e ed. May be NULL.
1e0c3 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
1e0c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e0c5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
1e0c6 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 6e urn Code */. in
1e0c7 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 t nFree;
1e0c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e0c9 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75 6d /* Initial num
1e0ca 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 ber of pages on
1e0cb 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 free-list */..
1e0cc 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1e0cd 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
1e0ce 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
1e0cf 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b 0a 20 rt( iPage>1 );.
1e0d0 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d 50 61 assert( !pMemPa
1e0d1 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 2d 3e ge || pMemPage->
1e0d2 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b 0a 0a pgno==iPage );..
1e0d3 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65 20 29 if( pMemPage )
1e0d4 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 4d {. pPage = pM
1e0d5 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 6c 69 emPage;. sqli
1e0d6 74 65 33 50 61 67 65 72 52 65 66 28 70 50 61 67 te3PagerRef(pPag
1e0d7 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d e->pDbPage);. }
1e0d8 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 20 else{. pPage
1e0d9 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b 75 = btreePageLooku
1e0da 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a 20 p(pBt, iPage);.
1e0db 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d 65 }.. /* Increme
1e0dc 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 67 65 nt the free page
1e0dd 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 65 31 count on pPage1
1e0de 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 */. rc = sqlit
1e0df 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 e3PagerWrite(pPa
1e0e0 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 ge1->pDbPage);.
1e0e1 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 66 if( rc ) goto f
1e0e2 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 6e reepage_out;. n
1e0e3 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 28 Free = get4byte(
1e0e4 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
1e0e5 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 6]);. put4byte(
1e0e6 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
1e0e7 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a 0a 20 6], nFree+1);..
1e0e8 20 69 66 28 20 70 42 74 2d 3e 62 74 73 46 6c 61 if( pBt->btsFla
1e0e9 67 73 20 26 20 42 54 53 5f 53 45 43 55 52 45 5f gs & BTS_SECURE_
1e0ea 44 45 4c 45 54 45 20 29 7b 0a 20 20 20 20 2f 2a DELETE ){. /*
1e0eb 20 49 66 20 74 68 65 20 73 65 63 75 72 65 5f 64 If the secure_d
1e0ec 65 6c 65 74 65 20 6f 70 74 69 6f 6e 20 69 73 20 elete option is
1e0ed 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 0a 20 20 enabled, then.
1e0ee 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 75 6c 6c ** always full
1e0ef 79 20 6f 76 65 72 77 72 69 74 65 20 64 65 6c 65 y overwrite dele
1e0f0 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ted information
1e0f1 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20 with zeros..
1e0f2 2a 2f 0a 20 20 20 20 69 66 28 20 28 21 70 50 61 */. if( (!pPa
1e0f3 67 65 20 26 26 20 28 28 72 63 20 3d 20 62 74 72 ge && ((rc = btr
1e0f4 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 eeGetPage(pBt, i
1e0f5 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 Page, &pPage, 0)
1e0f6 29 21 3d 30 29 20 29 0a 20 20 20 20 20 7c 7c 20 )!=0) ). ||
1e0f7 20 20 20 20 20 20 20 20 20 20 20 28 28 72 63 20 ((rc
1e0f8 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1e0f9 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
1e0fa 67 65 29 29 21 3d 30 29 0a 20 20 20 20 29 7b 0a ge))!=0). ){.
1e0fb 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 goto freep
1e0fc 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 age_out;. }.
1e0fd 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67 65 2d memset(pPage-
1e0fe 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65 >aData, 0, pPage
1e0ff 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 ->pBt->pageSize)
1e100 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 ;. }.. /* If t
1e101 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 he database supp
1e102 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d orts auto-vacuum
1e103 2c 20 77 72 69 74 65 20 61 6e 20 65 6e 74 72 79 , write an entry
1e104 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d in the pointer-
1e105 6d 61 70 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69 map. ** to indi
1e106 63 61 74 65 20 74 68 61 74 20 74 68 65 20 70 61 cate that the pa
1e107 67 65 20 69 73 20 66 72 65 65 2e 0a 20 20 2a 2f ge is free.. */
1e108 0a 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 . if( ISAUTOVAC
1e109 55 55 4d 20 29 7b 0a 20 20 20 20 70 74 72 6d 61 UUM ){. ptrma
1e10a 70 50 75 74 28 70 42 74 2c 20 69 50 61 67 65 2c pPut(pBt, iPage,
1e10b 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 PTRMAP_FREEPAGE
1e10c 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 , 0, &rc);. i
1e10d 66 28 20 72 63 20 29 20 67 6f 74 6f 20 66 72 65 f( rc ) goto fre
1e10e 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a epage_out;. }..
1e10f 20 20 2f 2a 20 4e 6f 77 20 6d 61 6e 69 70 75 6c /* Now manipul
1e110 61 74 65 20 74 68 65 20 61 63 74 75 61 6c 20 64 ate the actual d
1e111 61 74 61 62 61 73 65 20 66 72 65 65 2d 6c 69 73 atabase free-lis
1e112 74 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 t structure. The
1e113 72 65 20 61 72 65 20 74 77 6f 0a 20 20 2a 2a 20 re are two. **
1e114 70 6f 73 73 69 62 69 6c 69 74 69 65 73 2e 20 49 possibilities. I
1e115 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 f the free-list
1e116 69 73 20 63 75 72 72 65 6e 74 6c 79 20 65 6d 70 is currently emp
1e117 74 79 2c 20 6f 72 20 69 66 20 74 68 65 20 66 69 ty, or if the fi
1e118 72 73 74 0a 20 20 2a 2a 20 74 72 75 6e 6b 20 70 rst. ** trunk p
1e119 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d age in the free-
1e11a 6c 69 73 74 20 69 73 20 66 75 6c 6c 2c 20 74 68 list is full, th
1e11b 65 6e 20 74 68 69 73 20 70 61 67 65 20 77 69 6c en this page wil
1e11c 6c 20 62 65 63 6f 6d 65 20 61 0a 20 20 2a 2a 20 l become a. **
1e11d 6e 65 77 20 66 72 65 65 2d 6c 69 73 74 20 74 72 new free-list tr
1e11e 75 6e 6b 20 70 61 67 65 2e 20 4f 74 68 65 72 77 unk page. Otherw
1e11f 69 73 65 2c 20 69 74 20 77 69 6c 6c 20 62 65 63 ise, it will bec
1e120 6f 6d 65 20 61 20 6c 65 61 66 20 6f 66 20 74 68 ome a leaf of th
1e121 65 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 72 75 e. ** first tru
1e122 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 nk page in the c
1e123 75 72 72 65 6e 74 20 66 72 65 65 2d 6c 69 73 74 urrent free-list
1e124 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 74 65 73 . This block tes
1e125 74 73 20 69 66 20 69 74 0a 20 20 2a 2a 20 69 73 ts if it. ** is
1e126 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 64 64 possible to add
1e127 20 74 68 65 20 70 61 67 65 20 61 73 20 61 20 6e the page as a n
1e128 65 77 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 ew free-list lea
1e129 66 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 46 f.. */. if( nF
1e12a 72 65 65 21 3d 30 20 29 7b 0a 20 20 20 20 75 33 ree!=0 ){. u3
1e12b 32 20 6e 4c 65 61 66 3b 20 20 20 20 20 20 20 20 2 nLeaf;
1e12c 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 /* Initi
1e12d 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 61 al number of lea
1e12e 66 20 63 65 6c 6c 73 20 6f 6e 20 74 72 75 6e 6b f cells on trunk
1e12f 20 70 61 67 65 20 2a 2f 0a 0a 20 20 20 20 69 54 page */.. iT
1e130 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 runk = get4byte(
1e131 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
1e132 32 5d 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 74 2]);. rc = bt
1e133 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
1e134 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c iTrunk, &pTrunk,
1e135 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 0);. if( rc!
1e136 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1e137 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 goto freepag
1e138 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 e_out;. }..
1e139 20 20 6e 4c 65 61 66 20 3d 20 67 65 74 34 62 79 nLeaf = get4by
1e13a 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 te(&pTrunk->aDat
1e13b 61 5b 34 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 a[4]);. asser
1e13c 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 t( pBt->usableSi
1e13d 7a 65 3e 33 32 20 29 3b 0a 20 20 20 20 69 66 28 ze>32 );. if(
1e13e 20 6e 4c 65 61 66 20 3e 20 28 75 33 32 29 70 42 nLeaf > (u32)pB
1e13f 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 t->usableSize/4
1e140 2d 20 32 20 29 7b 0a 20 20 20 20 20 20 72 63 20 - 2 ){. rc
1e141 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
1e142 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 _BKPT;. got
1e143 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a o freepage_out;.
1e144 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c }. if( nL
1e145 65 61 66 20 3c 20 28 75 33 32 29 70 42 74 2d 3e eaf < (u32)pBt->
1e146 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 usableSize/4 - 8
1e147 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 ){. /* In
1e148 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20 this case there
1e149 69 73 20 72 6f 6f 6d 20 6f 6e 20 74 68 65 20 74 is room on the t
1e14a 72 75 6e 6b 20 70 61 67 65 20 74 6f 20 69 6e 73 runk page to ins
1e14b 65 72 74 20 74 68 65 20 70 61 67 65 0a 20 20 20 ert the page.
1e14c 20 20 20 2a 2a 20 62 65 69 6e 67 20 66 72 65 65 ** being free
1e14d 64 20 61 73 20 61 20 6e 65 77 20 6c 65 61 66 2e d as a new leaf.
1e14e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
1e14f 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 ** Note that the
1e150 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 6e trunk page is n
1e151 6f 74 20 72 65 61 6c 6c 79 20 66 75 6c 6c 20 75 ot really full u
1e152 6e 74 69 6c 20 69 74 20 63 6f 6e 74 61 69 6e 73 ntil it contains
1e153 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 62 6c 65 . ** usable
1e154 53 69 7a 65 2f 34 20 2d 20 32 20 65 6e 74 72 69 Size/4 - 2 entri
1e155 65 73 2c 20 6e 6f 74 20 75 73 61 62 6c 65 53 69 es, not usableSi
1e156 7a 65 2f 34 20 2d 20 38 20 65 6e 74 72 69 65 73 ze/4 - 8 entries
1e157 20 61 73 20 77 65 20 68 61 76 65 0a 20 20 20 20 as we have.
1e158 20 20 2a 2a 20 63 6f 64 65 64 2e 20 20 42 75 74 ** coded. But
1e159 20 64 75 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 due to a coding
1e15a 20 65 72 72 6f 72 20 69 6e 20 76 65 72 73 69 6f error in versio
1e15b 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 70 72 69 ns of SQLite pri
1e15c 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 33 or to. ** 3
1e15d 2e 36 2e 30 2c 20 64 61 74 61 62 61 73 65 73 20 .6.0, databases
1e15e 77 69 74 68 20 66 72 65 65 6c 69 73 74 20 74 72 with freelist tr
1e15f 75 6e 6b 20 70 61 67 65 73 20 68 6f 6c 64 69 6e unk pages holdin
1e160 67 20 6d 6f 72 65 20 74 68 61 6e 0a 20 20 20 20 g more than.
1e161 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f ** usableSize/
1e162 34 20 2d 20 38 20 65 6e 74 72 69 65 73 20 77 69 4 - 8 entries wi
1e163 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 20 61 ll be reported a
1e164 73 20 63 6f 72 72 75 70 74 2e 20 20 49 6e 20 6f s corrupt. In o
1e165 72 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 74 6f rder. ** to
1e166 20 6d 61 69 6e 74 61 69 6e 20 62 61 63 6b 77 61 maintain backwa
1e167 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 rds compatibilit
1e168 79 20 77 69 74 68 20 6f 6c 64 65 72 20 76 65 72 y with older ver
1e169 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c sions of SQLite,
1e16a 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 77 69 6c . ** we wil
1e16b 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 72 65 l continue to re
1e16c 73 74 72 69 63 74 20 74 68 65 20 6e 75 6d 62 65 strict the numbe
1e16d 72 20 6f 66 20 65 6e 74 72 69 65 73 20 74 6f 20 r of entries to
1e16e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 usableSize/4 - 8
1e16f 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 6e 6f . ** for no
1e170 77 2e 20 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e w. At some poin
1e171 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 20 t in the future
1e172 28 6f 6e 63 65 20 65 76 65 72 79 6f 6e 65 20 68 (once everyone h
1e173 61 73 20 75 70 67 72 61 64 65 64 0a 20 20 20 20 as upgraded.
1e174 20 20 2a 2a 20 74 6f 20 33 2e 36 2e 30 20 6f 72 ** to 3.6.0 or
1e175 20 6c 61 74 65 72 29 20 77 65 20 73 68 6f 75 6c later) we shoul
1e176 64 20 63 6f 6e 73 69 64 65 72 20 66 69 78 69 6e d consider fixin
1e177 67 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 61 g the conditiona
1e178 6c 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a l above. **
1e179 20 74 6f 20 72 65 61 64 20 22 75 73 61 62 6c 65 to read "usable
1e17a 53 69 7a 65 2f 34 2d 32 22 20 69 6e 73 74 65 61 Size/4-2" instea
1e17b 64 20 6f 66 20 22 75 73 61 62 6c 65 53 69 7a 65 d of "usableSize
1e17c 2f 34 2d 38 22 2e 0a 20 20 20 20 20 20 2a 2f 0a /4-8".. */.
1e17d 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1e17e 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 e3PagerWrite(pTr
1e17f 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 unk->pDbPage);.
1e180 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
1e181 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1e182 20 20 70 75 74 34 62 79 74 65 28 26 70 54 72 75 put4byte(&pTru
1e183 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6e 4c nk->aData[4], nL
1e184 65 61 66 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 eaf+1);.
1e185 70 75 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b put4byte(&pTrunk
1e186 2d 3e 61 44 61 74 61 5b 38 2b 6e 4c 65 61 66 2a ->aData[8+nLeaf*
1e187 34 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 4], iPage);.
1e188 20 20 20 20 69 66 28 20 70 50 61 67 65 20 26 26 if( pPage &&
1e189 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 (pBt->btsFlags
1e18a 26 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c & BTS_SECURE_DEL
1e18b 45 54 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ETE)==0 ){.
1e18c 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
1e18d 72 44 6f 6e 74 57 72 69 74 65 28 70 50 61 67 65 rDontWrite(pPage
1e18e 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
1e18f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 }. rc
1e190 20 3d 20 62 74 72 65 65 53 65 74 48 61 73 43 6f = btreeSetHasCo
1e191 6e 74 65 6e 74 28 70 42 74 2c 20 69 50 61 67 65 ntent(pBt, iPage
1e192 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1e193 20 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 TRACE(("FREE-PA
1e194 47 45 3a 20 25 64 20 6c 65 61 66 20 6f 6e 20 74 GE: %d leaf on t
1e195 72 75 6e 6b 20 70 61 67 65 20 25 64 5c 6e 22 2c runk page %d\n",
1e196 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 54 72 75 pPage->pgno,pTru
1e197 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 nk->pgno));.
1e198 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f goto freepage_
1e199 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a out;. }. }..
1e19a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 /* If control
1e19b 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70 6f flows to this po
1e19c 69 6e 74 2c 20 74 68 65 6e 20 69 74 20 77 61 73 int, then it was
1e19d 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
1e19e 20 61 64 64 20 74 68 65 0a 20 20 2a 2a 20 74 68 add the. ** th
1e19f 65 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 e page being fre
1e1a0 65 64 20 61 73 20 61 20 6c 65 61 66 20 70 61 67 ed as a leaf pag
1e1a1 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 e of the first t
1e1a2 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65 runk in the free
1e1a3 2d 6c 69 73 74 2e 0a 20 20 2a 2a 20 50 6f 73 73 -list.. ** Poss
1e1a4 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 65 ibly because the
1e1a5 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 65 6d free-list is em
1e1a6 70 74 79 2c 20 6f 72 20 70 6f 73 73 69 62 6c 79 pty, or possibly
1e1a7 20 62 65 63 61 75 73 65 20 74 68 65 20 0a 20 20 because the .
1e1a8 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 69 ** first trunk i
1e1a9 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 n the free-list
1e1aa 69 73 20 66 75 6c 6c 2e 20 45 69 74 68 65 72 20 is full. Either
1e1ab 77 61 79 2c 20 74 68 65 20 70 61 67 65 20 62 65 way, the page be
1e1ac 69 6e 67 20 66 72 65 65 64 0a 20 20 2a 2a 20 77 ing freed. ** w
1e1ad 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e ill become the n
1e1ae 65 77 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 ew first trunk p
1e1af 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d age in the free-
1e1b0 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 list.. */. if(
1e1b1 20 70 50 61 67 65 3d 3d 30 20 26 26 20 53 51 4c pPage==0 && SQL
1e1b2 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 62 74 ITE_OK!=(rc = bt
1e1b3 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
1e1b4 69 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 iPage, &pPage, 0
1e1b5 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 )) ){. goto f
1e1b6 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d reepage_out;. }
1e1b7 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
1e1b8 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d agerWrite(pPage-
1e1b9 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 >pDbPage);. if(
1e1ba 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1e1bb 7b 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 {. goto freep
1e1bc 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 age_out;. }. p
1e1bd 75 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 ut4byte(pPage->a
1e1be 44 61 74 61 2c 20 69 54 72 75 6e 6b 29 3b 0a 20 Data, iTrunk);.
1e1bf 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 put4byte(&pPage
1e1c0 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 30 29 3b 0a ->aData[4], 0);.
1e1c1 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 put4byte(&pPag
1e1c2 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 e1->aData[32], i
1e1c3 50 61 67 65 29 3b 0a 20 20 54 52 41 43 45 28 28 Page);. TRACE((
1e1c4 22 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6e "FREE-PAGE: %d n
1e1c5 65 77 20 74 72 75 6e 6b 20 70 61 67 65 20 72 65 ew trunk page re
1e1c6 70 6c 61 63 69 6e 67 20 25 64 5c 6e 22 2c 20 70 placing %d\n", p
1e1c7 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 69 54 72 75 Page->pgno, iTru
1e1c8 6e 6b 29 29 3b 0a 0a 66 72 65 65 70 61 67 65 5f nk));..freepage_
1e1c9 6f 75 74 3a 0a 20 20 69 66 28 20 70 50 61 67 65 out:. if( pPage
1e1ca 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 ){. pPage->i
1e1cb 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 sInit = 0;. }.
1e1cc 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
1e1cd 67 65 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 ge);. releasePa
1e1ce 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72 65 ge(pTrunk);. re
1e1cf 74 75 72 6e 20 72 63 3b 0a 7d 0a 73 74 61 74 69 turn rc;.}.stati
1e1d0 63 20 76 6f 69 64 20 66 72 65 65 50 61 67 65 28 c void freePage(
1e1d1 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
1e1d2 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 69 66 28 int *pRC){. if(
1e1d3 20 28 2a 70 52 43 29 3d 3d 53 51 4c 49 54 45 5f (*pRC)==SQLITE_
1e1d4 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d OK ){. *pRC =
1e1d5 20 66 72 65 65 50 61 67 65 32 28 70 50 61 67 65 freePage2(pPage
1e1d6 2d 3e 70 42 74 2c 20 70 50 61 67 65 2c 20 70 50 ->pBt, pPage, pP
1e1d7 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a age->pgno);. }.
1e1d8 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e }../*.** Free an
1e1d9 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 y overflow pages
1e1da 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1e1db 20 74 68 65 20 67 69 76 65 6e 20 43 65 6c 6c 2e the given Cell.
1e1dc 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
1e1dd 6c 65 61 72 43 65 6c 6c 28 4d 65 6d 50 61 67 65 learCell(MemPage
1e1de 20 2a 70 50 61 67 65 2c 20 75 6e 73 69 67 6e 65 *pPage, unsigne
1e1df 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 29 7b 0a d char *pCell){.
1e1e0 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
1e1e1 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 = pPage->pBt;.
1e1e2 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
1e1e3 20 50 67 6e 6f 20 6f 76 66 6c 50 67 6e 6f 3b 0a Pgno ovflPgno;.
1e1e4 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 int rc;. int
1e1e5 6e 4f 76 66 6c 3b 0a 20 20 75 33 32 20 6f 76 66 nOvfl;. u32 ovf
1e1e6 6c 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 61 73 lPageSize;.. as
1e1e7 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1e1e8 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
1e1e9 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1e1ea 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
1e1eb 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
1e1ec 20 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69 &info);. if( i
1e1ed 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 nfo.iOverflow==0
1e1ee 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
1e1ef 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f QLITE_OK; /* No
1e1f0 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
1e1f1 20 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 Return without
1e1f2 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a doing anything *
1e1f3 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 65 6c /. }. if( pCel
1e1f4 6c 2b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 l+info.iOverflow
1e1f5 2b 33 20 3e 20 70 50 61 67 65 2d 3e 61 44 61 74 +3 > pPage->aDat
1e1f6 61 2b 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 a+pPage->maskPag
1e1f7 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
1e1f8 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
1e1f9 4b 50 54 3b 20 20 2f 2a 20 43 65 6c 6c 20 65 78 KPT; /* Cell ex
1e1fa 74 65 6e 64 73 20 70 61 73 74 20 65 6e 64 20 6f tends past end o
1e1fb 66 20 70 61 67 65 20 2a 2f 0a 20 20 7d 0a 20 20 f page */. }.
1e1fc 6f 76 66 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62 ovflPgno = get4b
1e1fd 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e yte(&pCell[info.
1e1fe 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 iOverflow]);. a
1e1ff 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 ssert( pBt->usab
1e200 6c 65 53 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20 leSize > 4 );.
1e201 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 3d 20 70 ovflPageSize = p
1e202 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d Bt->usableSize -
1e203 20 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69 4;. nOvfl = (i
1e204 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69 nfo.nPayload - i
1e205 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 nfo.nLocal + ovf
1e206 6c 50 61 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f lPageSize - 1)/o
1e207 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a 20 20 61 vflPageSize;. a
1e208 73 73 65 72 74 28 20 6f 76 66 6c 50 67 6e 6f 3d ssert( ovflPgno=
1e209 3d 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b =0 || nOvfl>0 );
1e20a 0a 20 20 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d . while( nOvfl-
1e20b 2d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e - ){. Pgno iN
1e20c 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d ext = 0;. Mem
1e20d 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b Page *pOvfl = 0;
1e20e 0a 20 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e . if( ovflPgn
1e20f 6f 3c 32 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e o<2 || ovflPgno>
1e210 62 74 72 65 65 50 61 67 65 63 6f 75 6e 74 28 70 btreePagecount(p
1e211 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 Bt) ){. /*
1e212 30 20 69 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c 0 is not a legal
1e213 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 page number and
1e214 20 70 61 67 65 20 31 20 63 61 6e 6e 6f 74 20 62 page 1 cannot b
1e215 65 20 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f e an . ** o
1e216 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 54 68 verflow page. Th
1e217 65 72 65 66 6f 72 65 20 69 66 20 6f 76 66 6c 50 erefore if ovflP
1e218 67 6e 6f 3c 32 20 6f 72 20 70 61 73 74 20 74 68 gno<2 or past th
1e219 65 20 65 6e 64 20 6f 66 20 74 68 65 20 0a 20 20 e end of the .
1e21a 20 20 20 20 2a 2a 20 66 69 6c 65 20 74 68 65 20 ** file the
1e21b 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 database must be
1e21c 20 63 6f 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20 corrupt. */.
1e21d 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1e21e 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
1e21f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76 }. if( nOv
1e220 66 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d fl ){. rc =
1e221 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 getOverflowPage
1e222 28 70 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 (pBt, ovflPgno,
1e223 26 70 4f 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b &pOvfl, &iNext);
1e224 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 . if( rc )
1e225 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
1e226 0a 0a 20 20 20 20 69 66 28 20 28 20 70 4f 76 66 .. if( ( pOvf
1e227 6c 20 7c 7c 20 28 28 70 4f 76 66 6c 20 3d 20 62 l || ((pOvfl = b
1e228 74 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70 treePageLookup(p
1e229 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 29 29 21 3d Bt, ovflPgno))!=
1e22a 30 29 20 29 0a 20 20 20 20 20 26 26 20 73 71 6c 0) ). && sql
1e22b 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 ite3PagerPageRef
1e22c 63 6f 75 6e 74 28 70 4f 76 66 6c 2d 3e 70 44 62 count(pOvfl->pDb
1e22d 50 61 67 65 29 21 3d 31 0a 20 20 20 20 29 7b 0a Page)!=1. ){.
1e22e 20 20 20 20 20 20 2f 2a 20 54 68 65 72 65 20 69 /* There i
1e22f 73 20 6e 6f 20 72 65 61 73 6f 6e 20 61 6e 79 20 s no reason any
1e230 63 75 72 73 6f 72 20 73 68 6f 75 6c 64 20 68 61 cursor should ha
1e231 76 65 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e ve an outstandin
1e232 67 20 72 65 66 65 72 65 6e 63 65 20 0a 20 20 20 g reference .
1e233 20 20 20 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 ** to an over
1e234 66 6c 6f 77 20 70 61 67 65 20 62 65 6c 6f 6e 67 flow page belong
1e235 69 6e 67 20 74 6f 20 61 20 63 65 6c 6c 20 74 68 ing to a cell th
1e236 61 74 20 69 73 20 62 65 69 6e 67 20 64 65 6c 65 at is being dele
1e237 74 65 64 2f 75 70 64 61 74 65 64 2e 0a 20 20 20 ted/updated..
1e238 20 20 20 2a 2a 20 53 6f 20 69 66 20 74 68 65 72 ** So if ther
1e239 65 20 65 78 69 73 74 73 20 6d 6f 72 65 20 74 68 e exists more th
1e23a 61 6e 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 an one reference
1e23b 20 74 6f 20 74 68 69 73 20 70 61 67 65 2c 20 74 to this page, t
1e23c 68 65 6e 20 69 74 20 0a 20 20 20 20 20 20 2a 2a hen it . **
1e23d 20 6d 75 73 74 20 6e 6f 74 20 72 65 61 6c 6c 79 must not really
1e23e 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 be an overflow
1e23f 70 61 67 65 20 61 6e 64 20 74 68 65 20 64 61 74 page and the dat
1e240 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 63 6f abase must be co
1e241 72 72 75 70 74 2e 20 0a 20 20 20 20 20 20 2a 2a rrupt. . **
1e242 20 49 74 20 69 73 20 68 65 6c 70 66 75 6c 20 74 It is helpful t
1e243 6f 20 64 65 74 65 63 74 20 74 68 69 73 20 62 65 o detect this be
1e244 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 66 72 65 fore calling fre
1e245 65 50 61 67 65 32 28 29 2c 20 61 73 20 0a 20 20 ePage2(), as .
1e246 20 20 20 20 2a 2a 20 66 72 65 65 50 61 67 65 32 ** freePage2
1e247 28 29 20 6d 61 79 20 7a 65 72 6f 20 74 68 65 20 () may zero the
1e248 70 61 67 65 20 63 6f 6e 74 65 6e 74 73 20 69 66 page contents if
1e249 20 73 65 63 75 72 65 2d 64 65 6c 65 74 65 20 6d secure-delete m
1e24a 6f 64 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 ode is. **
1e24b 65 6e 61 62 6c 65 64 2e 20 49 66 20 74 68 69 73 enabled. If this
1e24c 20 27 6f 76 65 72 66 6c 6f 77 27 20 70 61 67 65 'overflow' page
1e24d 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 61 happens to be a
1e24e 20 70 61 67 65 20 74 68 61 74 20 74 68 65 0a 20 page that the.
1e24f 20 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 20 69 ** caller i
1e250 73 20 69 74 65 72 61 74 69 6e 67 20 74 68 72 6f s iterating thro
1e251 75 67 68 20 6f 72 20 75 73 69 6e 67 20 69 6e 20 ugh or using in
1e252 73 6f 6d 65 20 6f 74 68 65 72 20 77 61 79 2c 20 some other way,
1e253 74 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 63 61 this. ** ca
1e254 6e 20 62 65 20 70 72 6f 62 6c 65 6d 61 74 69 63 n be problematic
1e255 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1e256 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
1e257 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
1e258 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
1e259 20 66 72 65 65 50 61 67 65 32 28 70 42 74 2c 20 freePage2(pBt,
1e25a 70 4f 76 66 6c 2c 20 6f 76 66 6c 50 67 6e 6f 29 pOvfl, ovflPgno)
1e25b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 ;. }.. if(
1e25c 20 70 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 pOvfl ){.
1e25d 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
1e25e 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65 f(pOvfl->pDbPage
1e25f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
1e260 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
1e261 0a 20 20 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 . ovflPgno =
1e262 69 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 iNext;. }. ret
1e263 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1e264 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 ../*.** Create t
1e265 68 65 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 he byte sequence
1e266 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 used to represe
1e267 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61 67 nt a cell on pag
1e268 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20 77 e pPage.** and w
1e269 72 69 74 65 20 74 68 61 74 20 62 79 74 65 20 73 rite that byte s
1e26a 65 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43 65 equence into pCe
1e26b 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 20 ll[]. Overflow
1e26c 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c 6c pages are.** all
1e26d 6f 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c 65 ocated and fille
1e26e 64 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61 72 d in as necessar
1e26f 79 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 y. The calling
1e270 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 20 procedure.** is
1e271 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
1e272 6d 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66 66 making sure suff
1e273 69 63 69 65 6e 74 20 73 70 61 63 65 20 68 61 73 icient space has
1e274 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a been allocated.
1e275 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a ** for pCell[]..
1e276 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
1e277 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 6e pCell does not n
1e278 65 63 65 73 73 61 72 79 20 6e 65 65 64 20 74 6f ecessary need to
1e279 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70 50 point to the pP
1e27a 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 72 age->aData.** ar
1e27b 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68 74 ea. pCell might
1e27c 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 74 point to some t
1e27d 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 emporary storage
1e27e 2e 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c 6c . The cell will
1e27f 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63 74 .** be construct
1e280 65 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70 6f ed in this tempo
1e281 72 61 72 79 20 61 72 65 61 20 74 68 65 6e 20 63 rary area then c
1e282 6f 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67 65 opied into pPage
1e283 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65 72 ->aData.** later
1e284 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1e285 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d 65 fillInCell(. Me
1e286 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 mPage *pPage,
1e287 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e288 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f The page that co
1e289 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 ntains the cell
1e28a 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
1e28b 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 ar *pCell,
1e28c 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 /* Complete
1e28d 74 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c 6c text of the cell
1e28e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
1e28f 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 *pKey, i64 nKey
1e290 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 , /* The key
1e291 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
1e292 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74 61 *pData,int nData
1e293 2c 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 , /* The data
1e294 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 */. int nZero,
1e295 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e296 20 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65 72 /* Extra zer
1e297 6f 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e o bytes to appen
1e298 64 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20 20 d to pData */.
1e299 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 int *pnSize
1e29a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1e29b 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69 7a * Write cell siz
1e29c 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 e here */.){. i
1e29d 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 63 nt nPayload;. c
1e29e 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a 20 onst u8 *pSrc;.
1e29f 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72 63 int nSrc, n, rc
1e2a0 3b 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65 66 ;. int spaceLef
1e2a1 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f t;. MemPage *pO
1e2a2 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 vfl = 0;. MemPa
1e2a3 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20 3d ge *pToRelease =
1e2a4 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 0;. unsigned c
1e2a5 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20 75 har *pPrior;. u
1e2a6 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 nsigned char *pP
1e2a7 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61 72 ayload;. BtShar
1e2a8 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d ed *pBt = pPage-
1e2a9 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e >pBt;. Pgno pgn
1e2aa 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 oOvfl = 0;. int
1e2ab 20 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c 6c nHeader;. Cell
1e2ac 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61 73 Info info;.. as
1e2ad 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1e2ae 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
1e2af 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a pBt->mutex) );..
1e2b0 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 6e 6f /* pPage is no
1e2b1 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 77 72 t necessarily wr
1e2b2 69 74 65 61 62 6c 65 20 73 69 6e 63 65 20 70 43 iteable since pC
1e2b3 65 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 75 78 ell might be aux
1e2b4 69 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 66 66 iliary. ** buff
1e2b5 65 72 20 73 70 61 63 65 20 74 68 61 74 20 69 73 er space that is
1e2b6 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 separate from t
1e2b7 68 65 20 70 50 61 67 65 20 62 75 66 66 65 72 20 he pPage buffer
1e2b8 61 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 72 74 area */. assert
1e2b9 28 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e 61 ( pCell<pPage->a
1e2ba 44 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26 Data || pCell>=&
1e2bb 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 pPage->aData[pBt
1e2bc 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 ->pageSize].
1e2bd 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 || sqlit
1e2be 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
1e2bf 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
1e2c0 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c e) );.. /* Fill
1e2c1 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 in the header.
1e2c2 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30 */. nHeader = 0
1e2c3 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e ;. if( !pPage->
1e2c4 6c 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61 leaf ){. nHea
1e2c5 64 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 der += 4;. }.
1e2c6 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 if( pPage->hasDa
1e2c7 74 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 ta ){. nHeade
1e2c8 72 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 r += putVarint(&
1e2c9 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 pCell[nHeader],
1e2ca 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 nData+nZero);.
1e2cb 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61 }else{. nData
1e2cc 20 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 = nZero = 0;.
1e2cd 7d 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 }. nHeader += p
1e2ce 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b utVarint(&pCell[
1e2cf 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a nHeader], *(u64*
1e2d0 29 26 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65 65 )&nKey);. btree
1e2d1 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 ParseCellPtr(pPa
1e2d2 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f ge, pCell, &info
1e2d3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 );. assert( inf
1e2d4 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 64 o.nHeader==nHead
1e2d5 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 er );. assert(
1e2d6 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 20 info.nKey==nKey
1e2d7 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 );. assert( inf
1e2d8 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28 6e o.nData==(u32)(n
1e2d9 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a 20 Data+nZero) );.
1e2da 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 . /* Fill in t
1e2db 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 he payload */.
1e2dc 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61 nPayload = nData
1e2dd 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 + nZero;. if(
1e2de 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b pPage->intKey ){
1e2df 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 74 . pSrc = pDat
1e2e0 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 a;. nSrc = nD
1e2e1 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 3d ata;. nData =
1e2e2 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20 0;. }else{ .
1e2e3 20 20 69 66 28 20 4e 45 56 45 52 28 6e 4b 65 79 if( NEVER(nKey
1e2e4 3e 30 78 37 66 66 66 66 66 66 66 20 7c 7c 20 70 >0x7fffffff || p
1e2e5 4b 65 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 Key==0) ){.
1e2e6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
1e2e7 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
1e2e8 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 }. nPayload
1e2e9 2b 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 += (int)nKey;.
1e2ea 20 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20 pSrc = pKey;.
1e2eb 20 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e nSrc = (int)n
1e2ec 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69 Key;. }. *pnSi
1e2ed 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b ze = info.nSize;
1e2ee 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69 . spaceLeft = i
1e2ef 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50 nfo.nLocal;. pP
1e2f0 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b ayload = &pCell[
1e2f1 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69 nHeader];. pPri
1e2f2 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f or = &pCell[info
1e2f3 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20 .iOverflow];..
1e2f4 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e while( nPayload>
1e2f5 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61 0 ){. if( spa
1e2f6 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66 ceLeft==0 ){.#if
1e2f7 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1e2f8 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
1e2f9 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61 Pgno pgnoPtrma
1e2fa 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a p = pgnoOvfl; /*
1e2fb 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 Overflow page p
1e2fc 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 ointer-map entry
1e2fd 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69 page */. i
1e2fe 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 f( pBt->autoVacu
1e2ff 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f um ){. do
1e300 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f {. pgno
1e301 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 Ovfl++;.
1e302 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20 } while( .
1e303 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47 PTRMAP_ISPAG
1e304 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29 E(pBt, pgnoOvfl)
1e305 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45 || pgnoOvfl==PE
1e306 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
1e307 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b pBt) . );
1e308 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
1e309 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 rc = alloc
1e30a 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 ateBtreePage(pBt
1e30b 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f , &pOvfl, &pgnoO
1e30c 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30 vfl, pgnoOvfl, 0
1e30d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
1e30e 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
1e30f 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 M. /* If th
1e310 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f e database suppo
1e311 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c rts auto-vacuum,
1e312 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 and the second
1e313 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20 or subsequent.
1e314 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 ** overflow
1e315 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c page is being al
1e316 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20 located, add an
1e317 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69 entry to the poi
1e318 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a nter-map. *
1e319 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 20 * for that page
1e31a 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 now. . **.
1e31b 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 ** If this
1e31c 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 is the first ove
1e31d 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e rflow page, then
1e31e 20 77 72 69 74 65 20 61 20 70 61 72 74 69 61 6c write a partial
1e31f 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a entry . **
1e320 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d to the pointer-
1e321 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 74 65 map. If we write
1e322 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73 nothing to this
1e323 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f pointer-map slo
1e324 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e t,. ** then
1e325 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20 the optimistic
1e326 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 overflow chain p
1e327 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 rocessing in cle
1e328 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a arCell(). *
1e329 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72 * may misinterpr
1e32a 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c et the uninitial
1e32b 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20 ised values and
1e32c 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 delete the.
1e32d 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20 ** wrong pages
1e32e 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
1e32f 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
1e330 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
1e331 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c acuum && rc==SQL
1e332 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1e333 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 u8 eType = (pg
1e334 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f noPtrmap?PTRMAP_
1e335 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 OVERFLOW2:PTRMAP
1e336 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 _OVERFLOW1);.
1e337 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 ptrmapPut(p
1e338 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 54 Bt, pgnoOvfl, eT
1e339 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70 2c ype, pgnoPtrmap,
1e33a 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69 &rc);. i
1e33b 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 f( rc ){.
1e33c 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1e33d 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d Ovfl);. }
1e33e 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
1e33f 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
1e340 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
1e341 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b age(pToRelease);
1e342 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1e343 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 rc;. }..
1e344 20 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 /* If pToRele
1e345 61 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 ase is not zero
1e346 74 68 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e than pPrior poin
1e347 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 ts into the data
1e348 20 61 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f area. ** o
1e349 66 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d f pToRelease. M
1e34a 61 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 ake sure pToRele
1e34b 61 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 ase is still wri
1e34c 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 teable. */.
1e34d 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65 assert( pToRele
1e34e 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 ase==0 || sqlite
1e34f 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
1e350 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 e(pToRelease->pD
1e351 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 bPage) );..
1e352 20 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73 /* If pPrior is
1e353 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 part of the dat
1e354 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c a area of pPage,
1e355 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 then make sure
1e356 70 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 pPage. ** i
1e357 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c s still writeabl
1e358 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 e */. asser
1e359 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d t( pPrior<pPage-
1e35a 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72 >aData || pPrior
1e35b 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b >=&pPage->aData[
1e35c 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 pBt->pageSize].
1e35d 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 || sq
1e35e 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
1e35f 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
1e360 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 Page) );..
1e361 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c put4byte(pPrior,
1e362 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20 pgnoOvfl);.
1e363 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 releasePage(pT
1e364 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 oRelease);.
1e365 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f pToRelease = pO
1e366 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f vfl;. pPrio
1e367 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61 r = pOvfl->aData
1e368 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 ;. put4byte
1e369 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20 (pPrior, 0);.
1e36a 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 pPayload = &p
1e36b 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a Ovfl->aData[4];.
1e36c 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 spaceLeft
1e36d 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
1e36e 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 e - 4;. }.
1e36f 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 n = nPayload;.
1e370 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 if( n>spaceLe
1e371 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 ft ) n = spaceLe
1e372 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 ft;.. /* If p
1e373 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 ToRelease is not
1e374 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c zero than pPayl
1e375 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 oad points into
1e376 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 the data area.
1e377 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 ** of pToRelea
1e378 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 se. Make sure p
1e379 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 ToRelease is sti
1e37a 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f ll writeable. */
1e37b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f . assert( pTo
1e37c 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 Release==0 || sq
1e37d 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
1e37e 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 eable(pToRelease
1e37f 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 ->pDbPage) );..
1e380 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61 /* If pPayloa
1e381 64 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 d is part of the
1e382 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 50 data area of pP
1e383 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 age, then make s
1e384 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a ure pPage. **
1e385 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 is still writea
1e386 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 ble */. asser
1e387 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67 t( pPayload<pPag
1e388 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79 e->aData || pPay
1e389 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44 load>=&pPage->aD
1e38a 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a ata[pBt->pageSiz
1e38b 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c e]. |
1e38c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 | sqlite3PagerIs
1e38d 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
1e38e 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 >pDbPage) );..
1e38f 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a if( nSrc>0 ){.
1e390 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63 if( n>nSrc
1e391 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20 ) n = nSrc;.
1e392 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 20 assert( pSrc
1e393 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 );. memcpy(
1e394 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20 pPayload, pSrc,
1e395 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 n);. }else{.
1e396 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79 memset(pPay
1e397 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20 load, 0, n);.
1e398 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 }. nPayload
1e399 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f -= n;. pPaylo
1e39a 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72 ad += n;. pSr
1e39b 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63 c += n;. nSrc
1e39c 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65 -= n;. space
1e39d 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69 Left -= n;. i
1e39e 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 f( nSrc==0 ){.
1e39f 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 nSrc = nData
1e3a0 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70 ;. pSrc = p
1e3a1 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Data;. }. }.
1e3a2 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 releasePage(pT
1e3a3 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74 oRelease);. ret
1e3a4 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1e3a5 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 ../*.** Remove t
1e3a6 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f he i-th cell fro
1e3a7 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73 20 72 m pPage. This r
1e3a8 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73 20 70 outine effects p
1e3a9 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 Page only..** Th
1e3aa 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 e cell content i
1e3ab 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64 s not freed or d
1e3ac 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 eallocated. It
1e3ad 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a is assumed that.
1e3ae 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 ** the cell cont
1e3af 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70 ent has been cop
1e3b0 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c ied someplace el
1e3b1 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e se. This routin
1e3b2 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65 e just.** remove
1e3b3 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 s the reference
1e3b4 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d to the cell from
1e3b5 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73 pPage..**.** "s
1e3b6 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e z" must be the n
1e3b7 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
1e3b8 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 n the cell..*/.s
1e3b9 74 61 74 69 63 20 76 6f 69 64 20 64 72 6f 70 43 tatic void dropC
1e3ba 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 ell(MemPage *pPa
1e3bb 67 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 ge, int idx, int
1e3bc 20 73 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a sz, int *pRC){.
1e3bd 20 20 75 33 32 20 70 63 3b 20 20 20 20 20 20 20 u32 pc;
1e3be 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63 /* Offset to c
1e3bf 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 20 63 ell content of c
1e3c0 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65 ell being delete
1e3c1 64 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b d */. u8 *data;
1e3c2 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 2d /* pPage-
1e3c3 3e 61 44 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a >aData */. u8 *
1e3c4 70 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 ptr; /* U
1e3c5 73 65 64 20 74 6f 20 6d 6f 76 65 20 62 79 74 65 sed to move byte
1e3c6 73 20 61 72 6f 75 6e 64 20 77 69 74 68 69 6e 20 s around within
1e3c7 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a data[] */. u8 *
1e3c8 65 6e 64 50 74 72 3b 20 20 20 20 20 2f 2a 20 45 endPtr; /* E
1e3c9 6e 64 20 6f 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20 nd of loop */.
1e3ca 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
1e3cb 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f /* The return co
1e3cc 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b de */. int hdr;
1e3cd 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e /* Begin
1e3ce 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 65 61 64 ning of the head
1e3cf 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 61 67 65 er. 0 most page
1e3d0 73 2e 20 20 31 30 30 20 70 61 67 65 20 31 20 2a s. 100 page 1 *
1e3d1 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 /.. if( *pRC )
1e3d2 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 return;.. asser
1e3d3 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64 78 t( idx>=0 && idx
1e3d4 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b <pPage->nCell );
1e3d5 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 . assert( sz==c
1e3d6 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c 20 69 ellSize(pPage, i
1e3d7 64 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 dx) );. assert(
1e3d8 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
1e3d9 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e riteable(pPage->
1e3da 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 pDbPage) );. as
1e3db 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1e3dc 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
1e3dd 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1e3de 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
1e3df 44 61 74 61 3b 0a 20 20 70 74 72 20 3d 20 26 70 Data;. ptr = &p
1e3e0 50 61 67 65 2d 3e 61 43 65 6c 6c 49 64 78 5b 32 Page->aCellIdx[2
1e3e1 2a 69 64 78 5d 3b 0a 20 20 70 63 20 3d 20 67 65 *idx];. pc = ge
1e3e2 74 32 62 79 74 65 28 70 74 72 29 3b 0a 20 20 68 t2byte(ptr);. h
1e3e3 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f dr = pPage->hdrO
1e3e4 66 66 73 65 74 3b 0a 20 20 74 65 73 74 63 61 73 ffset;. testcas
1e3e5 65 28 20 70 63 3d 3d 67 65 74 32 62 79 74 65 28 e( pc==get2byte(
1e3e6 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 29 3b &data[hdr+5]) );
1e3e7 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 63 2b . testcase( pc+
1e3e8 73 7a 3d 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e sz==pPage->pBt->
1e3e9 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 usableSize );.
1e3ea 69 66 28 20 70 63 20 3c 20 28 75 33 32 29 67 65 if( pc < (u32)ge
1e3eb 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
1e3ec 2b 35 5d 29 20 7c 7c 20 70 63 2b 73 7a 20 3e 20 +5]) || pc+sz >
1e3ed 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
1e3ee 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 2a 70 leSize ){. *p
1e3ef 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 RC = SQLITE_CORR
1e3f0 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 72 65 UPT_BKPT;. re
1e3f1 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 72 63 20 3d turn;. }. rc =
1e3f2 20 66 72 65 65 53 70 61 63 65 28 70 50 61 67 65 freeSpace(pPage
1e3f3 2c 20 70 63 2c 20 73 7a 29 3b 0a 20 20 69 66 28 , pc, sz);. if(
1e3f4 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 rc ){. *pRC
1e3f5 3d 20 72 63 3b 0a 20 20 20 20 72 65 74 75 72 6e = rc;. return
1e3f6 3b 0a 20 20 7d 0a 20 20 65 6e 64 50 74 72 20 3d ;. }. endPtr =
1e3f7 20 26 70 50 61 67 65 2d 3e 61 43 65 6c 6c 49 64 &pPage->aCellId
1e3f8 78 5b 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c x[2*pPage->nCell
1e3f9 20 2d 20 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 - 2];. assert(
1e3fa 20 28 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f (SQLITE_PTR_TO_
1e3fb 49 4e 54 28 70 74 72 29 26 31 29 3d 3d 30 20 29 INT(ptr)&1)==0 )
1e3fc 3b 20 20 2f 2a 20 70 74 72 20 69 73 20 61 6c 77 ; /* ptr is alw
1e3fd 61 79 73 20 32 2d 62 79 74 65 20 61 6c 69 67 6e ays 2-byte align
1e3fe 65 64 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 70 ed */. while( p
1e3ff 74 72 3c 65 6e 64 50 74 72 20 29 7b 0a 20 20 20 tr<endPtr ){.
1e400 20 2a 28 75 31 36 2a 29 70 74 72 20 3d 20 2a 28 *(u16*)ptr = *(
1e401 75 31 36 2a 29 26 70 74 72 5b 32 5d 3b 0a 20 20 u16*)&ptr[2];.
1e402 20 20 70 74 72 20 2b 3d 20 32 3b 0a 20 20 7d 0a ptr += 2;. }.
1e403 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 2d pPage->nCell--
1e404 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 ;. put2byte(&da
1e405 74 61 5b 68 64 72 2b 33 5d 2c 20 70 50 61 67 65 ta[hdr+3], pPage
1e406 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50 61 67 ->nCell);. pPag
1e407 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b 0a 7d e->nFree += 2;.}
1e408 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 ../*.** Insert a
1e409 20 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61 new cell on pPa
1e40a 67 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65 78 ge at cell index
1e40b 20 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69 "i". pCell poi
1e40c 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f nts to the.** co
1e40d 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 6c ntent of the cel
1e40e 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 l..**.** If the
1e40f 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69 6c cell content wil
1e410 6c 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67 l fit on the pag
1e411 65 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20 74 e, then put it t
1e412 68 65 72 65 2e 20 20 49 66 20 69 74 0a 2a 2a 20 here. If it.**
1e413 77 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20 74 68 will not fit, th
1e414 65 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f en make a copy o
1e415 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 f the cell conte
1e416 6e 74 20 69 6e 74 6f 20 70 54 65 6d 70 20 69 66 nt into pTemp if
1e417 0a 2a 2a 20 70 54 65 6d 70 20 69 73 20 6e 6f 74 .** pTemp is not
1e418 20 6e 75 6c 6c 2e 20 20 52 65 67 61 72 64 6c 65 null. Regardle
1e419 73 73 20 6f 66 20 70 54 65 6d 70 2c 20 61 6c 6c ss of pTemp, all
1e41a 6f 63 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72 ocate a new entr
1e41b 79 0a 2a 2a 20 69 6e 20 70 50 61 67 65 2d 3e 61 y.** in pPage->a
1e41c 70 4f 76 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65 pOvfl[] and make
1e41d 20 69 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 it point to the
1e41e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65 cell content (e
1e41f 69 74 68 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d ither.** in pTem
1e420 70 20 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61 p or the origina
1e421 6c 20 70 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73 l pCell) and als
1e422 6f 20 72 65 63 6f 72 64 20 69 74 73 20 69 6e 64 o record its ind
1e423 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69 ex. .** Allocati
1e424 6e 67 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 ng a new entry i
1e425 6e 20 70 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d n pPage->aCell[]
1e426 20 69 6d 70 6c 69 65 73 20 74 68 61 74 20 0a 2a implies that .*
1e427 2a 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c * pPage->nOverfl
1e428 6f 77 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 ow is incremente
1e429 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69 d..**.** If nSki
1e42a 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 p is non-zero, t
1e42b 68 65 6e 20 64 6f 20 6e 6f 74 20 63 6f 70 79 20 hen do not copy
1e42c 74 68 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20 the first nSkip
1e42d 62 79 74 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 bytes of the.**
1e42e 63 65 6c 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 cell. The caller
1e42f 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 will overwrite
1e430 74 68 65 6d 20 61 66 74 65 72 20 74 68 69 73 20 them after this
1e431 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
1e432 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69 70 20 69 73 . If.** nSkip is
1e433 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 non-zero, then
1e434 70 43 65 6c 6c 20 6d 61 79 20 6e 6f 74 20 70 6f pCell may not po
1e435 69 6e 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 int to an invali
1e436 64 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f d memory locatio
1e437 6e 20 0a 2a 2a 20 28 62 75 74 20 70 43 65 6c 6c n .** (but pCell
1e438 2b 6e 53 6b 69 70 20 69 73 20 61 6c 77 61 79 73 +nSkip is always
1e439 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a 73 74 61 74 valid)..*/.stat
1e43a 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 43 65 ic void insertCe
1e43b 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 ll(. MemPage *p
1e43c 50 61 67 65 2c 20 20 20 2f 2a 20 50 61 67 65 20 Page, /* Page
1e43d 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 into which we ar
1e43e 65 20 63 6f 70 79 69 6e 67 20 2a 2f 0a 20 20 69 e copying */. i
1e43f 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 nt i,
1e440 20 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 62 65 63 /* New cell bec
1e441 6f 6d 65 73 20 74 68 65 20 69 2d 74 68 20 63 65 omes the i-th ce
1e442 6c 6c 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a ll of the page *
1e443 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 /. u8 *pCell,
1e444 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 /* Content
1e445 20 6f 66 20 74 68 65 20 6e 65 77 20 63 65 6c 6c of the new cell
1e446 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c 20 20 20 */. int sz,
1e447 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 /* Bytes
1e448 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70 of content in p
1e449 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 54 Cell */. u8 *pT
1e44a 65 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 emp, /* T
1e44b 65 6d 70 20 73 74 6f 72 61 67 65 20 73 70 61 63 emp storage spac
1e44c 65 20 66 6f 72 20 70 43 65 6c 6c 2c 20 69 66 20 e for pCell, if
1e44d 6e 65 65 64 65 64 20 2a 2f 0a 20 20 50 67 6e 6f needed */. Pgno
1e44e 20 69 43 68 69 6c 64 2c 20 20 20 20 20 20 2f 2a iChild, /*
1e44f 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 72 65 If non-zero, re
1e450 70 6c 61 63 65 20 66 69 72 73 74 20 34 20 62 79 place first 4 by
1e451 74 65 73 20 77 69 74 68 20 74 68 69 73 20 76 61 tes with this va
1e452 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 lue */. int *pR
1e453 43 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 C /* Re
1e454 61 64 20 61 6e 64 20 77 72 69 74 65 20 72 65 74 ad and write ret
1e455 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 68 65 urn code from he
1e456 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 re */.){. int i
1e457 64 78 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 dx = 0; /*
1e458 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 6e Where to write n
1e459 65 77 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 ew cell content
1e45a 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 in data[] */. i
1e45b 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 nt j;
1e45c 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
1e45d 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 3b 20 20 */. int end;
1e45e 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
1e45f 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 6c byte past the l
1e460 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 ast cell pointer
1e461 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 in data[] */.
1e462 69 6e 74 20 69 6e 73 3b 20 20 20 20 20 20 20 20 int ins;
1e463 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 64 61 /* Index in da
1e464 74 61 5b 5d 20 77 68 65 72 65 20 6e 65 77 20 63 ta[] where new c
1e465 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73 20 69 ell pointer is i
1e466 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74 nserted */. int
1e467 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 2f cellOffset; /
1e468 2a 20 41 64 64 72 65 73 73 20 6f 66 20 66 69 72 * Address of fir
1e469 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 st cell pointer
1e46a 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 75 in data[] */. u
1e46b 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 8 *data;
1e46c 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 /* The content
1e46d 6f 66 20 74 68 65 20 77 68 6f 6c 65 20 70 61 67 of the whole pag
1e46e 65 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 e */. u8 *ptr;
1e46f 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 /* Used
1e470 20 66 6f 72 20 6d 6f 76 69 6e 67 20 69 6e 66 6f for moving info
1e471 72 6d 61 74 69 6f 6e 20 61 72 6f 75 6e 64 20 69 rmation around i
1e472 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 75 38 n data[] */. u8
1e473 20 2a 65 6e 64 50 74 72 3b 20 20 20 20 20 20 20 *endPtr;
1e474 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6c 6f /* End of the lo
1e475 6f 70 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 53 6b op */.. int nSk
1e476 69 70 20 3d 20 28 69 43 68 69 6c 64 20 3f 20 34 ip = (iChild ? 4
1e477 20 3a 20 30 29 3b 0a 0a 20 20 69 66 28 20 2a 70 : 0);.. if( *p
1e478 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 RC ) return;..
1e479 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
1e47a 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b i<=pPage->nCell+
1e47b 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
1e47c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
1e47d 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 age->nCell<=MX_C
1e47e 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20 ELL(pPage->pBt)
1e47f 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 && MX_CELL(pPage
1e480 2d 3e 70 42 74 29 3c 3d 31 30 39 32 31 20 29 3b ->pBt)<=10921 );
1e481 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
1e482 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3c 3d 41 72 72 ->nOverflow<=Arr
1e483 61 79 53 69 7a 65 28 70 50 61 67 65 2d 3e 61 70 aySize(pPage->ap
1e484 4f 76 66 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 Ovfl) );. asser
1e485 74 28 20 41 72 72 61 79 53 69 7a 65 28 70 50 61 t( ArraySize(pPa
1e486 67 65 2d 3e 61 70 4f 76 66 6c 29 3d 3d 41 72 72 ge->apOvfl)==Arr
1e487 61 79 53 69 7a 65 28 70 50 61 67 65 2d 3e 61 69 aySize(pPage->ai
1e488 4f 76 66 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 Ovfl) );. asser
1e489 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1e48a 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
1e48b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a ->mutex) );. /*
1e48c 20 54 68 65 20 63 65 6c 6c 20 73 68 6f 75 6c 64 The cell should
1e48d 20 6e 6f 72 6d 61 6c 6c 79 20 62 65 20 73 69 7a normally be siz
1e48e 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 48 ed correctly. H
1e48f 6f 77 65 76 65 72 2c 20 77 68 65 6e 20 6d 6f 76 owever, when mov
1e490 69 6e 67 20 61 0a 20 20 2a 2a 20 6d 61 6c 66 6f ing a. ** malfo
1e491 72 6d 65 64 20 63 65 6c 6c 20 66 72 6f 6d 20 61 rmed cell from a
1e492 20 6c 65 61 66 20 70 61 67 65 20 74 6f 20 61 6e leaf page to an
1e493 20 69 6e 74 65 72 69 6f 72 20 70 61 67 65 2c 20 interior page,
1e494 69 66 20 74 68 65 20 63 65 6c 6c 20 73 69 7a 65 if the cell size
1e495 0a 20 20 2a 2a 20 77 61 6e 74 65 64 20 74 6f 20 . ** wanted to
1e496 62 65 20 6c 65 73 73 20 74 68 61 6e 20 34 20 62 be less than 4 b
1e497 75 74 20 67 6f 74 20 72 6f 75 6e 64 65 64 20 75 ut got rounded u
1e498 70 20 74 6f 20 34 20 6f 6e 20 74 68 65 20 6c 65 p to 4 on the le
1e499 61 66 2c 20 74 68 65 6e 20 73 69 7a 65 0a 20 20 af, then size.
1e49a 2a 2a 20 6d 69 67 68 74 20 62 65 20 6c 65 73 73 ** might be less
1e49b 20 74 68 61 6e 20 38 20 28 6c 65 61 66 2d 73 69 than 8 (leaf-si
1e49c 7a 65 20 2b 20 70 6f 69 6e 74 65 72 29 20 6f 6e ze + pointer) on
1e49d 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f the interior no
1e49e 64 65 2e 20 20 48 65 6e 63 65 0a 20 20 2a 2a 20 de. Hence. **
1e49f 74 68 65 20 74 65 72 6d 20 61 66 74 65 72 20 74 the term after t
1e4a0 68 65 20 7c 7c 20 69 6e 20 74 68 65 20 66 6f 6c he || in the fol
1e4a1 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 28 29 2e lowing assert().
1e4a2 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 7a */. assert( sz
1e4a3 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 ==cellSizePtr(pP
1e4a4 61 67 65 2c 20 70 43 65 6c 6c 29 20 7c 7c 20 28 age, pCell) || (
1e4a5 73 7a 3d 3d 38 20 26 26 20 69 43 68 69 6c 64 3e sz==8 && iChild>
1e4a6 30 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 0) );. if( pPag
1e4a7 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 e->nOverflow ||
1e4a8 73 7a 2b 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 sz+2>pPage->nFre
1e4a9 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 e ){. if( pTe
1e4aa 6d 70 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 mp ){. memc
1e4ab 70 79 28 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 py(pTemp+nSkip,
1e4ac 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d pCell+nSkip, sz-
1e4ad 6e 53 6b 69 70 29 3b 0a 20 20 20 20 20 20 70 43 nSkip);. pC
1e4ae 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 ell = pTemp;.
1e4af 20 7d 0a 20 20 20 20 69 66 28 20 69 43 68 69 6c }. if( iChil
1e4b0 64 20 29 7b 0a 20 20 20 20 20 20 70 75 74 34 62 d ){. put4b
1e4b1 79 74 65 28 70 43 65 6c 6c 2c 20 69 43 68 69 6c yte(pCell, iChil
1e4b2 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 20 d);. }. j
1e4b3 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c = pPage->nOverfl
1e4b4 6f 77 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 ow++;. assert
1e4b5 28 20 6a 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 ( j<(int)(sizeof
1e4b6 28 70 50 61 67 65 2d 3e 61 70 4f 76 66 6c 29 2f (pPage->apOvfl)/
1e4b7 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 70 sizeof(pPage->ap
1e4b8 4f 76 66 6c 5b 30 5d 29 29 20 29 3b 0a 20 20 20 Ovfl[0])) );.
1e4b9 20 70 50 61 67 65 2d 3e 61 70 4f 76 66 6c 5b 6a pPage->apOvfl[j
1e4ba 5d 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70 ] = pCell;. p
1e4bb 50 61 67 65 2d 3e 61 69 4f 76 66 6c 5b 6a 5d 20 Page->aiOvfl[j]
1e4bc 3d 20 28 75 31 36 29 69 3b 0a 20 20 7d 65 6c 73 = (u16)i;. }els
1e4bd 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 e{. int rc =
1e4be 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
1e4bf 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
1e4c0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
1e4c1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1e4c2 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 *pRC = rc;.
1e4c3 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
1e4c4 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
1e4c5 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
1e4c6 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 able(pPage->pDbP
1e4c7 61 67 65 29 20 29 3b 0a 20 20 20 20 64 61 74 61 age) );. data
1e4c8 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b = pPage->aData;
1e4c9 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 . cellOffset
1e4ca 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 = pPage->cellOff
1e4cb 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20 63 set;. end = c
1e4cc 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 ellOffset + 2*pP
1e4cd 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 age->nCell;.
1e4ce 69 6e 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 ins = cellOffset
1e4cf 20 2b 20 32 2a 69 3b 0a 20 20 20 20 72 63 20 3d + 2*i;. rc =
1e4d0 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 70 allocateSpace(p
1e4d1 50 61 67 65 2c 20 73 7a 2c 20 26 69 64 78 29 3b Page, sz, &idx);
1e4d2 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 20 2a . if( rc ){ *
1e4d3 70 52 43 20 3d 20 72 63 3b 20 72 65 74 75 72 6e pRC = rc; return
1e4d4 3b 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 ; }. /* The a
1e4d5 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 29 20 72 llocateSpace() r
1e4d6 6f 75 74 69 6e 65 20 67 75 61 72 61 6e 74 65 65 outine guarantee
1e4d7 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 s the following
1e4d8 74 77 6f 20 70 72 6f 70 65 72 74 69 65 73 0a 20 two properties.
1e4d9 20 20 20 2a 2a 20 69 66 20 69 74 20 72 65 74 75 ** if it retu
1e4da 72 6e 73 20 73 75 63 63 65 73 73 20 2a 2f 0a 20 rns success */.
1e4db 20 20 20 61 73 73 65 72 74 28 20 69 64 78 20 3e assert( idx >
1e4dc 3d 20 65 6e 64 2b 32 20 29 3b 0a 20 20 20 20 61 = end+2 );. a
1e4dd 73 73 65 72 74 28 20 69 64 78 2b 73 7a 20 3c 3d ssert( idx+sz <=
1e4de 20 28 69 6e 74 29 70 50 61 67 65 2d 3e 70 42 74 (int)pPage->pBt
1e4df 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a ->usableSize );.
1e4e0 20 20 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c pPage->nCell
1e4e1 2b 2b 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e ++;. pPage->n
1e4e2 46 72 65 65 20 2d 3d 20 28 75 31 36 29 28 32 20 Free -= (u16)(2
1e4e3 2b 20 73 7a 29 3b 0a 20 20 20 20 6d 65 6d 63 70 + sz);. memcp
1e4e4 79 28 26 64 61 74 61 5b 69 64 78 2b 6e 53 6b 69 y(&data[idx+nSki
1e4e5 70 5d 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c p], pCell+nSkip,
1e4e6 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 sz-nSkip);.
1e4e7 69 66 28 20 69 43 68 69 6c 64 20 29 7b 0a 20 20 if( iChild ){.
1e4e8 20 20 20 20 70 75 74 34 62 79 74 65 28 26 64 61 put4byte(&da
1e4e9 74 61 5b 69 64 78 5d 2c 20 69 43 68 69 6c 64 29 ta[idx], iChild)
1e4ea 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 74 72 20 ;. }. ptr
1e4eb 3d 20 26 64 61 74 61 5b 65 6e 64 5d 3b 0a 20 20 = &data[end];.
1e4ec 20 20 65 6e 64 50 74 72 20 3d 20 26 64 61 74 61 endPtr = &data
1e4ed 5b 69 6e 73 5d 3b 0a 20 20 20 20 61 73 73 65 72 [ins];. asser
1e4ee 74 28 20 28 53 51 4c 49 54 45 5f 50 54 52 5f 54 t( (SQLITE_PTR_T
1e4ef 4f 5f 49 4e 54 28 70 74 72 29 26 31 29 3d 3d 30 O_INT(ptr)&1)==0
1e4f0 20 29 3b 20 20 2f 2a 20 70 74 72 20 69 73 20 61 ); /* ptr is a
1e4f1 6c 77 61 79 73 20 32 2d 62 79 74 65 20 61 6c 69 lways 2-byte ali
1e4f2 67 6e 65 64 20 2a 2f 0a 20 20 20 20 77 68 69 6c gned */. whil
1e4f3 65 28 20 70 74 72 3e 65 6e 64 50 74 72 20 29 7b e( ptr>endPtr ){
1e4f4 0a 20 20 20 20 20 20 2a 28 75 31 36 2a 29 70 74 . *(u16*)pt
1e4f5 72 20 3d 20 2a 28 75 31 36 2a 29 26 70 74 72 5b r = *(u16*)&ptr[
1e4f6 2d 32 5d 3b 0a 20 20 20 20 20 20 70 74 72 20 2d -2];. ptr -
1e4f7 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 = 2;. }. p
1e4f8 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 6e ut2byte(&data[in
1e4f9 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20 70 75 s], idx);. pu
1e4fa 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 50 61 t2byte(&data[pPa
1e4fb 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 33 5d ge->hdrOffset+3]
1e4fc 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b , pPage->nCell);
1e4fd 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1e4fe 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
1e4ff 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 if( pPage->p
1e500 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
1e501 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 {. /* The c
1e502 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 ell may contain
1e503 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
1e504 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 overflow page. I
1e505 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 f so, write.
1e506 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 ** the entry f
1e507 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 or the overflow
1e508 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f page into the po
1e509 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 inter map..
1e50a 20 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d 61 70 */. ptrmap
1e50b 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 PutOvflPtr(pPage
1e50c 2c 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b 0a 20 , pCell, pRC);.
1e50d 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a }.#endif. }.
1e50e 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c }../*.** Add a l
1e50f 69 73 74 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 ist of cells to
1e510 61 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 a page. The pag
1e511 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 e should be init
1e512 69 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 ially empty..**
1e513 54 68 65 20 63 65 6c 6c 73 20 61 72 65 20 67 75 The cells are gu
1e514 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 aranteed to fit
1e515 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a on the page..*/.
1e516 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 static void asse
1e517 6d 62 6c 65 50 61 67 65 28 0a 20 20 4d 65 6d 50 mblePage(. MemP
1e518 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a age *pPage, /*
1e519 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 The page to be
1e51a 61 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 assemblied */.
1e51b 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 int nCell,
1e51c 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 /* The number
1e51d 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 of cells to add
1e51e 74 6f 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a to this page */.
1e51f 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 u8 **apCell,
1e520 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 /* Pointers
1e521 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a to cell bodies *
1e522 2f 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 20 20 /. u16 *aSize
1e523 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f /* Sizes o
1e524 66 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 f the cells */.)
1e525 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 {. int i;
1e526 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
1e527 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 70 unter */. u8 *p
1e528 43 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f 2a 20 Cellptr; /*
1e529 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 Address of next
1e52a 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a cell pointer */.
1e52b 20 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 int cellbody;
1e52c 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f /* Address o
1e52d 66 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 f next cell body
1e52e 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 */. u8 * const
1e52f 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
1e530 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 Data;
1e531 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
1e532 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 20 2a data for pPage *
1e533 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 /. const int hd
1e534 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
1e535 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 fset;
1e536 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61 /* Offset of hea
1e537 64 65 72 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a der on pPage */.
1e538 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55 73 61 const int nUsa
1e539 62 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 ble = pPage->pBt
1e53a 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a ->usableSize; /*
1e53b 20 55 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 Usable size of
1e53c 70 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 page */.. asser
1e53d 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
1e53e 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 low==0 );. asse
1e53f 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1e540 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
1e541 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
1e542 73 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d 30 20 ssert( nCell>=0
1e543 26 26 20 6e 43 65 6c 6c 3c 3d 28 69 6e 74 29 4d && nCell<=(int)M
1e544 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 X_CELL(pPage->pB
1e545 74 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 26 t). &
1e546 26 20 28 69 6e 74 29 4d 58 5f 43 45 4c 4c 28 70 & (int)MX_CELL(p
1e547 50 61 67 65 2d 3e 70 42 74 29 3c 3d 31 30 39 32 Page->pBt)<=1092
1e548 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 1);. assert( sq
1e549 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
1e54a 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
1e54b 50 61 67 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 Page) );.. /* C
1e54c 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 61 heck that the pa
1e54d 67 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e ge has just been
1e54e 20 7a 65 72 6f 65 64 20 62 79 20 7a 65 72 6f 50 zeroed by zeroP
1e54f 61 67 65 28 29 20 2a 2f 0a 20 20 61 73 73 65 72 age() */. asser
1e550 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d t( pPage->nCell=
1e551 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1e552 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 6f 28 get2byteNotZero(
1e553 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3d 3d 6e &data[hdr+5])==n
1e554 55 73 61 62 6c 65 20 29 3b 0a 0a 20 20 70 43 65 Usable );.. pCe
1e555 6c 6c 70 74 72 20 3d 20 26 70 50 61 67 65 2d 3e llptr = &pPage->
1e556 61 43 65 6c 6c 49 64 78 5b 6e 43 65 6c 6c 2a 32 aCellIdx[nCell*2
1e557 5d 3b 0a 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20 ];. cellbody =
1e558 6e 55 73 61 62 6c 65 3b 0a 20 20 66 6f 72 28 69 nUsable;. for(i
1e559 3d 6e 43 65 6c 6c 2d 31 3b 20 69 3e 3d 30 3b 20 =nCell-1; i>=0;
1e55a 69 2d 2d 29 7b 0a 20 20 20 20 75 31 36 20 73 7a i--){. u16 sz
1e55b 20 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20 20 = aSize[i];.
1e55c 20 70 43 65 6c 6c 70 74 72 20 2d 3d 20 32 3b 0a pCellptr -= 2;.
1e55d 20 20 20 20 63 65 6c 6c 62 6f 64 79 20 2d 3d 20 cellbody -=
1e55e 73 7a 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 sz;. put2byte
1e55f 28 70 43 65 6c 6c 70 74 72 2c 20 63 65 6c 6c 62 (pCellptr, cellb
1e560 6f 64 79 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 ody);. memcpy
1e561 28 26 64 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d (&data[cellbody]
1e562 2c 20 61 70 43 65 6c 6c 5b 69 5d 2c 20 73 7a 29 , apCell[i], sz)
1e563 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62 79 74 65 ;. }. put2byte
1e564 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e (&data[hdr+3], n
1e565 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79 74 Cell);. put2byt
1e566 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 e(&data[hdr+5],
1e567 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61 cellbody);. pPa
1e568 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e 43 ge->nFree -= (nC
1e569 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65 20 ell*2 + nUsable
1e56a 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 - cellbody);. p
1e56b 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 75 Page->nCell = (u
1e56c 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 16)nCell;.}../*.
1e56d 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
1e56e 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 65 parameters dete
1e56f 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61 rmine how many a
1e570 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 65 djacent pages ge
1e571 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e t involved.** in
1e572 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 a balancing ope
1e573 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74 ration. NN is t
1e574 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69 he number of nei
1e575 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 ghbors on either
1e576 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 side.** of the
1e577 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69 63 page that partic
1e578 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c ipate in the bal
1e579 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ancing operation
1e57a 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20 . NB is the.**
1e57b 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
1e57c 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74 69 pages that parti
1e57d 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e cipate, includin
1e57e 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61 67 g the target pag
1e57f 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67 e and.** NN neig
1e580 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 hbors on either
1e581 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 side..**.** The
1e582 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 minimum value of
1e583 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75 NN is 1 (of cou
1e584 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e rse). Increasin
1e585 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20 g NN above 1.**
1e586 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65 (to 2 or 3) give
1e587 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f s a modest impro
1e588 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54 vement in SELECT
1e589 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66 and DELETE perf
1e58a 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78 ormance.** in ex
1e58b 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72 change for a lar
1e58c 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20 ger degradation
1e58d 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50 in INSERT and UP
1e58e 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 DATE performance
1e58f 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f ..** The value o
1e590 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20 f NN appears to
1e591 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72 65 give the best re
1e592 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a sults overall..*
1e593 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 /.#define NN 1
1e594 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1e595 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 mber of neighbor
1e596 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 s on either side
1e597 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65 of pPage */.#de
1e598 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29 fine NB (NN*2+1)
1e599 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70 /* Total p
1e59a 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e ages involved in
1e59b 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a the balance */.
1e59c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1e59d 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e _OMIT_QUICKBALAN
1e59e 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 CE./*.** This ve
1e59f 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 65 rsion of balance
1e5a0 28 29 20 68 61 6e 64 6c 65 73 20 74 68 65 20 63 () handles the c
1e5a1 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 63 61 ommon special ca
1e5a2 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 6e 65 se where.** a ne
1e5a3 77 20 65 6e 74 72 79 20 69 73 20 62 65 69 6e 67 w entry is being
1e5a4 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 68 65 inserted on the
1e5a5 20 65 78 74 72 65 6d 65 20 72 69 67 68 74 2d 65 extreme right-e
1e5a6 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72 65 nd of the.** tre
1e5a7 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 e, in other word
1e5a8 73 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 77 20 s, when the new
1e5a9 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 6f 6d entry will becom
1e5aa 65 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a e the largest.**
1e5ab 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72 entry in the tr
1e5ac 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 ee..**.** Instea
1e5ad 64 20 6f 66 20 74 72 79 69 6e 67 20 74 6f 20 62 d of trying to b
1e5ae 61 6c 61 6e 63 65 20 74 68 65 20 33 20 72 69 67 alance the 3 rig
1e5af 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 70 61 67 ht-most leaf pag
1e5b0 65 73 2c 20 6a 75 73 74 20 61 64 64 0a 2a 2a 20 es, just add.**
1e5b1 61 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74 68 a new page to th
1e5b2 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 e right-hand sid
1e5b3 65 20 61 6e 64 20 70 75 74 20 74 68 65 20 6f 6e e and put the on
1e5b4 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 0a 2a e new entry in.*
1e5b5 2a 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 68 * that page. Th
1e5b6 69 73 20 6c 65 61 76 65 73 20 74 68 65 20 72 69 is leaves the ri
1e5b7 67 68 74 20 73 69 64 65 20 6f 66 20 74 68 65 20 ght side of the
1e5b8 74 72 65 65 20 73 6f 6d 65 77 68 61 74 0a 2a 2a tree somewhat.**
1e5b9 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20 20 42 75 unbalanced. Bu
1e5ba 74 20 6f 64 64 73 20 61 72 65 20 74 68 61 74 20 t odds are that
1e5bb 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 we will be inser
1e5bc 74 69 6e 67 20 6e 65 77 20 65 6e 74 72 69 65 73 ting new entries
1e5bd 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 73 .** at the end s
1e5be 6f 6f 6e 20 61 66 74 65 72 77 61 72 64 73 20 73 oon afterwards s
1e5bf 6f 20 74 68 65 20 6e 65 61 72 6c 79 20 65 6d 70 o the nearly emp
1e5c0 74 79 20 70 61 67 65 20 77 69 6c 6c 20 71 75 69 ty page will qui
1e5c1 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e ckly.** fill up.
1e5c2 20 20 4f 6e 20 61 76 65 72 61 67 65 2e 0a 2a 2a On average..**
1e5c3 0a 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68 65 .** pPage is the
1e5c4 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68 leaf page which
1e5c5 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f is the right-mo
1e5c6 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 74 st page in the t
1e5c7 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65 6e 74 20 ree..** pParent
1e5c8 69 73 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20 is its parent.
1e5c9 70 50 61 67 65 20 6d 75 73 74 20 68 61 76 65 20 pPage must have
1e5ca 61 20 73 69 6e 67 6c 65 20 6f 76 65 72 66 6c 6f a single overflo
1e5cb 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68 69 63 68 w entry.** which
1e5cc 20 69 73 20 61 6c 73 6f 20 74 68 65 20 72 69 67 is also the rig
1e5cd 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 6f 6e ht-most entry on
1e5ce 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
1e5cf 20 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66 The pSpace buff
1e5d0 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 er is used to st
1e5d1 6f 72 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 ore a temporary
1e5d2 63 6f 70 79 20 6f 66 20 74 68 65 20 64 69 76 69 copy of the divi
1e5d3 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74 68 61 74 der.** cell that
1e5d4 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 65 will be inserte
1e5d5 64 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20 d into pParent.
1e5d6 53 75 63 68 20 61 20 63 65 6c 6c 20 63 6f 6e 73 Such a cell cons
1e5d7 69 73 74 73 20 6f 66 20 61 20 34 0a 2a 2a 20 62 ists of a 4.** b
1e5d8 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 yte page number
1e5d9 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 61 followed by a va
1e5da 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e riable length in
1e5db 74 65 67 65 72 2e 20 49 6e 20 6f 74 68 65 72 0a teger. In other.
1e5dc 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20 6d 6f 73 ** words, at mos
1e5dd 74 20 31 33 20 62 79 74 65 73 2e 20 48 65 6e 63 t 13 bytes. Henc
1e5de 65 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 e the pSpace buf
1e5df 66 65 72 20 6d 75 73 74 20 62 65 20 61 74 0a 2a fer must be at.*
1e5e0 2a 20 6c 65 61 73 74 20 31 33 20 62 79 74 65 73 * least 13 bytes
1e5e1 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 in size..*/.sta
1e5e2 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f tic int balance_
1e5e3 71 75 69 63 6b 28 4d 65 6d 50 61 67 65 20 2a 70 quick(MemPage *p
1e5e4 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61 67 65 20 Parent, MemPage
1e5e5 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 53 70 61 *pPage, u8 *pSpa
1e5e6 63 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 ce){. BtShared
1e5e7 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 50 61 *const pBt = pPa
1e5e8 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 42 ge->pBt; /* B
1e5e9 2d 54 72 65 65 20 44 61 74 61 62 61 73 65 20 2a -Tree Database *
1e5ea 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 /. MemPage *pNe
1e5eb 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 w;
1e5ec 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 6c /* Newl
1e5ed 79 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 y allocated page
1e5ee 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
1e5ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e5f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
1e5f1 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 50 turn Code */. P
1e5f2 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 20 20 20 gno pgnoNew;
1e5f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e5f4 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
1e5f5 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f 0a 0a 20 er of pNew */..
1e5f6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1e5f7 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
1e5f8 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
1e5f9 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
1e5fa 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
1e5fb 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 ble(pParent->pDb
1e5fc 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 Page) );. asser
1e5fd 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
1e5fe 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 low==1 );.. /*
1e5ff 54 68 69 73 20 65 72 72 6f 72 20 63 6f 6e 64 69 This error condi
1e600 74 69 6f 6e 20 69 73 20 6e 6f 77 20 63 61 75 67 tion is now caug
1e601 68 74 20 70 72 69 6f 72 20 74 6f 20 72 65 61 63 ht prior to reac
1e602 68 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 hing this functi
1e603 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 on */. if( pPag
1e604 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 20 72 65 e->nCell==0 ) re
1e605 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1e606 55 50 54 5f 42 4b 50 54 3b 0a 0a 20 20 2f 2a 20 UPT_BKPT;.. /*
1e607 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 Allocate a new p
1e608 61 67 65 2e 20 54 68 69 73 20 70 61 67 65 20 77 age. This page w
1e609 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 72 ill become the r
1e60a 69 67 68 74 2d 73 69 62 6c 69 6e 67 20 6f 66 20 ight-sibling of
1e60b 0a 20 20 2a 2a 20 70 50 61 67 65 2e 20 4d 61 6b . ** pPage. Mak
1e60c 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 e the parent pag
1e60d 65 20 77 72 69 74 61 62 6c 65 2c 20 73 6f 20 74 e writable, so t
1e60e 68 61 74 20 74 68 65 20 6e 65 77 20 64 69 76 69 hat the new divi
1e60f 64 65 72 20 63 65 6c 6c 0a 20 20 2a 2a 20 6d 61 der cell. ** ma
1e610 79 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20 49 y be inserted. I
1e611 66 20 62 6f 74 68 20 74 68 65 73 65 20 6f 70 65 f both these ope
1e612 72 61 74 69 6f 6e 73 20 61 72 65 20 73 75 63 63 rations are succ
1e613 65 73 73 66 75 6c 2c 20 70 72 6f 63 65 65 64 2e essful, proceed.
1e614 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 61 6c 6c . */. rc = all
1e615 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 ocateBtreePage(p
1e616 42 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f Bt, &pNew, &pgno
1e617 4e 65 77 2c 20 30 2c 20 30 29 3b 0a 0a 20 20 69 New, 0, 0);.. i
1e618 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1e619 20 29 7b 0a 0a 20 20 20 20 75 38 20 2a 70 4f 75 ){.. u8 *pOu
1e61a 74 20 3d 20 26 70 53 70 61 63 65 5b 34 5d 3b 0a t = &pSpace[4];.
1e61b 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 u8 *pCell =
1e61c 70 50 61 67 65 2d 3e 61 70 4f 76 66 6c 5b 30 5d pPage->apOvfl[0]
1e61d 3b 0a 20 20 20 20 75 31 36 20 73 7a 43 65 6c 6c ;. u16 szCell
1e61e 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 = cellSizePtr(p
1e61f 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 Page, pCell);.
1e620 20 20 75 38 20 2a 70 53 74 6f 70 3b 0a 0a 20 20 u8 *pStop;..
1e621 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1e622 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
1e623 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 e(pNew->pDbPage)
1e624 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1e625 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3d pPage->aData[0]=
1e626 3d 28 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 =(PTF_INTKEY|PTF
1e627 5f 4c 45 41 46 44 41 54 41 7c 50 54 46 5f 4c 45 _LEAFDATA|PTF_LE
1e628 41 46 29 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 AF) );. zeroP
1e629 61 67 65 28 70 4e 65 77 2c 20 50 54 46 5f 49 4e age(pNew, PTF_IN
1e62a 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 44 41 54 TKEY|PTF_LEAFDAT
1e62b 41 7c 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 20 A|PTF_LEAF);.
1e62c 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e assemblePage(pN
1e62d 65 77 2c 20 31 2c 20 26 70 43 65 6c 6c 2c 20 26 ew, 1, &pCell, &
1e62e 73 7a 43 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a szCell);.. /*
1e62f 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 If this is an a
1e630 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 uto-vacuum datab
1e631 61 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 ase, update the
1e632 70 6f 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 pointer map.
1e633 2a 2a 20 77 69 74 68 20 65 6e 74 72 69 65 73 20 ** with entries
1e634 66 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67 65 for the new page
1e635 2c 20 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 , and any pointe
1e636 72 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 r from the .
1e637 2a 2a 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 ** cell on the p
1e638 61 67 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c age to an overfl
1e639 6f 77 20 70 61 67 65 2e 20 49 66 20 65 69 74 68 ow page. If eith
1e63a 65 72 20 6f 66 20 74 68 65 73 65 0a 20 20 20 20 er of these.
1e63b 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 73 20 66 61 ** operations fa
1e63c 69 6c 73 2c 20 74 68 65 20 72 65 74 75 72 6e 20 ils, the return
1e63d 63 6f 64 65 20 69 73 20 73 65 74 2c 20 62 75 74 code is set, but
1e63e 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20 the contents.
1e63f 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 65 ** of the pare
1e640 6e 74 20 70 61 67 65 20 61 72 65 20 73 74 69 6c nt page are stil
1e641 6c 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 62 79 l manipulated by
1e642 20 74 68 68 20 63 6f 64 65 20 62 65 6c 6f 77 2e thh code below.
1e643 0a 20 20 20 20 2a 2a 20 54 68 61 74 20 69 73 20 . ** That is
1e644 4f 6b 2c 20 61 74 20 74 68 69 73 20 70 6f 69 6e Ok, at this poin
1e645 74 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 t the parent pag
1e646 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 e is guaranteed
1e647 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 6d 61 72 to. ** be mar
1e648 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 52 65 ked as dirty. Re
1e649 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 turning an error
1e64a 20 63 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 65 code will cause
1e64b 20 61 0a 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 a. ** rollba
1e64c 63 6b 2c 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 ck, undoing any
1e64d 63 68 61 6e 67 65 73 20 6d 61 64 65 20 74 6f 20 changes made to
1e64e 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e the parent page.
1e64f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1e650 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a ISAUTOVACUUM ){.
1e651 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 ptrmapPut(
1e652 70 42 74 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 54 pBt, pgnoNew, PT
1e653 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 RMAP_BTREE, pPar
1e654 65 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b ent->pgno, &rc);
1e655 0a 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c . if( szCel
1e656 6c 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 6c l>pNew->minLocal
1e657 20 29 7b 0a 20 20 20 20 20 20 20 20 70 74 72 6d ){. ptrm
1e658 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 4e 65 apPutOvflPtr(pNe
1e659 77 2c 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a w, pCell, &rc);.
1e65a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1e65b 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 . /* Create a
1e65c 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 74 6f divider cell to
1e65d 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 70 50 61 insert into pPa
1e65e 72 65 6e 74 2e 20 54 68 65 20 64 69 76 69 64 65 rent. The divide
1e65f 72 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 63 6f r cell. ** co
1e660 6e 73 69 73 74 73 20 6f 66 20 61 20 34 2d 62 79 nsists of a 4-by
1e661 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 28 te page number (
1e662 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
1e663 6f 66 20 70 50 61 67 65 29 20 61 6e 64 0a 20 20 of pPage) and.
1e664 20 20 2a 2a 20 61 20 76 61 72 69 61 62 6c 65 20 ** a variable
1e665 6c 65 6e 67 74 68 20 6b 65 79 20 76 61 6c 75 65 length key value
1e666 20 28 77 68 69 63 68 20 6d 75 73 74 20 62 65 20 (which must be
1e667 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 the same value a
1e668 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 6c 61 72 s the. ** lar
1e669 67 65 73 74 20 6b 65 79 20 6f 6e 20 70 50 61 67 gest key on pPag
1e66a 65 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a e).. **. *
1e66b 2a 20 54 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 * To find the la
1e66c 72 67 65 73 74 20 6b 65 79 20 76 61 6c 75 65 20 rgest key value
1e66d 6f 6e 20 70 50 61 67 65 2c 20 66 69 72 73 74 20 on pPage, first
1e66e 66 69 6e 64 20 74 68 65 20 72 69 67 68 74 2d 6d find the right-m
1e66f 6f 73 74 20 0a 20 20 20 20 2a 2a 20 63 65 6c 6c ost . ** cell
1e670 20 6f 6e 20 70 50 61 67 65 2e 20 54 68 65 20 66 on pPage. The f
1e671 69 72 73 74 20 74 77 6f 20 66 69 65 6c 64 73 20 irst two fields
1e672 6f 66 20 74 68 69 73 20 63 65 6c 6c 20 61 72 65 of this cell are
1e673 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 63 the . ** rec
1e674 6f 72 64 2d 6c 65 6e 67 74 68 20 28 61 20 76 61 ord-length (a va
1e675 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e riable length in
1e676 74 65 67 65 72 20 61 74 20 6d 6f 73 74 20 33 32 teger at most 32
1e677 2d 62 69 74 73 20 69 6e 20 73 69 7a 65 29 0a 20 -bits in size).
1e678 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 6b 65 ** and the ke
1e679 79 20 76 61 6c 75 65 20 28 61 20 76 61 72 69 61 y value (a varia
1e67a 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 ble length integ
1e67b 65 72 2c 20 6d 61 79 20 68 61 76 65 20 61 6e 79 er, may have any
1e67c 20 76 61 6c 75 65 29 2e 0a 20 20 20 20 2a 2a 20 value).. **
1e67d 54 68 65 20 66 69 72 73 74 20 6f 66 20 74 68 65 The first of the
1e67e 20 77 68 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70 while(...) loop
1e67f 73 20 62 65 6c 6f 77 20 73 6b 69 70 73 20 6f 76 s below skips ov
1e680 65 72 20 74 68 65 20 72 65 63 6f 72 64 2d 6c 65 er the record-le
1e681 6e 67 74 68 0a 20 20 20 20 2a 2a 20 66 69 65 6c ngth. ** fiel
1e682 64 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 77 68 d. The second wh
1e683 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70 20 63 6f ile(...) loop co
1e684 70 69 65 73 20 74 68 65 20 6b 65 79 20 76 61 6c pies the key val
1e685 75 65 20 66 72 6f 6d 20 74 68 65 0a 20 20 20 20 ue from the.
1e686 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 ** cell on pPage
1e687 20 69 6e 74 6f 20 74 68 65 20 70 53 70 61 63 65 into the pSpace
1e688 20 62 75 66 66 65 72 2e 0a 20 20 20 20 2a 2f 0a buffer.. */.
1e689 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 pCell = find
1e68a 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 50 61 67 Cell(pPage, pPag
1e68b 65 2d 3e 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 e->nCell-1);.
1e68c 20 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b pStop = &pCell[
1e68d 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 9];. while( (
1e68e 2a 28 70 43 65 6c 6c 2b 2b 29 26 30 78 38 30 29 *(pCell++)&0x80)
1e68f 20 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 && pCell<pStop
1e690 29 3b 0a 20 20 20 20 70 53 74 6f 70 20 3d 20 26 );. pStop = &
1e691 70 43 65 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77 68 pCell[9];. wh
1e692 69 6c 65 28 20 28 28 2a 28 70 4f 75 74 2b 2b 29 ile( ((*(pOut++)
1e693 20 3d 20 2a 28 70 43 65 6c 6c 2b 2b 29 29 26 30 = *(pCell++))&0
1e694 78 38 30 29 20 26 26 20 70 43 65 6c 6c 3c 70 53 x80) && pCell<pS
1e695 74 6f 70 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 top );.. /* I
1e696 6e 73 65 72 74 20 74 68 65 20 6e 65 77 20 64 69 nsert the new di
1e697 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 vider cell into
1e698 70 50 61 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 pParent. */.
1e699 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65 insertCell(pPare
1e69a 6e 74 2c 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 nt, pParent->nCe
1e69b 6c 6c 2c 20 70 53 70 61 63 65 2c 20 28 69 6e 74 ll, pSpace, (int
1e69c 29 28 70 4f 75 74 2d 70 53 70 61 63 65 29 2c 0a )(pOut-pSpace),.
1e69d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
1e69e 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 26 , pPage->pgno, &
1e69f 72 63 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 rc);.. /* Set
1e6a0 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 the right-child
1e6a1 20 70 6f 69 6e 74 65 72 20 6f 66 20 70 50 61 72 pointer of pPar
1e6a2 65 6e 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 ent to point to
1e6a3 74 68 65 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f the new page. */
1e6a4 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 . put4byte(&p
1e6a5 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 Parent->aData[pP
1e6a6 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 arent->hdrOffset
1e6a7 2b 38 5d 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a 20 +8], pgnoNew);.
1e6a8 20 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 . /* Release
1e6a9 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74 the reference t
1e6aa 6f 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 20 o the new page.
1e6ab 2a 2f 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 */. releasePa
1e6ac 67 65 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 ge(pNew);. }..
1e6ad 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 return rc;.}.#e
1e6ae 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1e6af 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 MIT_QUICKBALANCE
1e6b0 20 2a 2f 0a 0a 23 69 66 20 30 0a 2f 2a 0a 2a 2a */..#if 0./*.**
1e6b1 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 This function d
1e6b2 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72 69 62 75 oes not contribu
1e6b3 74 65 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 te anything to t
1e6b4 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 he operation of
1e6b5 53 51 4c 69 74 65 2e 0a 2a 2a 20 69 74 20 69 73 SQLite..** it is
1e6b6 20 73 6f 6d 65 74 69 6d 65 73 20 61 63 74 69 76 sometimes activ
1e6b7 61 74 65 64 20 74 65 6d 70 6f 72 61 72 69 6c 79 ated temporarily
1e6b8 20 77 68 69 6c 65 20 64 65 62 75 67 67 69 6e 67 while debugging
1e6b9 20 63 6f 64 65 20 72 65 73 70 6f 6e 73 69 62 6c code responsibl
1e6ba 65 20 0a 2a 2a 20 66 6f 72 20 73 65 74 74 69 6e e .** for settin
1e6bb 67 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e g pointer-map en
1e6bc 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 tries..*/.static
1e6bd 20 69 6e 74 20 70 74 72 6d 61 70 43 68 65 63 6b int ptrmapCheck
1e6be 50 61 67 65 73 28 4d 65 6d 50 61 67 65 20 2a 2a Pages(MemPage **
1e6bf 61 70 50 61 67 65 2c 20 69 6e 74 20 6e 50 61 67 apPage, int nPag
1e6c0 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a e){. int i, j;.
1e6c1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 50 61 for(i=0; i<nPa
1e6c2 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 67 ge; i++){. Pg
1e6c3 6e 6f 20 6e 3b 0a 20 20 20 20 75 38 20 65 3b 0a no n;. u8 e;.
1e6c4 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 MemPage *pPa
1e6c5 67 65 20 3d 20 61 70 50 61 67 65 5b 69 5d 3b 0a ge = apPage[i];.
1e6c6 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 BtShared *pB
1e6c7 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a t = pPage->pBt;.
1e6c8 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
1e6c9 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 0a 20 20 e->isInit );..
1e6ca 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 61 for(j=0; j<pPa
1e6cb 67 65 2d 3e 6e 43 65 6c 6c 3b 20 6a 2b 2b 29 7b ge->nCell; j++){
1e6cc 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 . CellInfo
1e6cd 69 6e 66 6f 3b 0a 20 20 20 20 20 20 75 38 20 2a info;. u8 *
1e6ce 7a 3b 0a 20 20 20 20 20 0a 20 20 20 20 20 20 7a z;. . z
1e6cf 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 = findCell(pPag
1e6d0 65 2c 20 6a 29 3b 0a 20 20 20 20 20 20 62 74 72 e, j);. btr
1e6d1 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 eeParseCellPtr(p
1e6d2 50 61 67 65 2c 20 7a 2c 20 26 69 6e 66 6f 29 3b Page, z, &info);
1e6d3 0a 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e . if( info.
1e6d4 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 iOverflow ){.
1e6d5 20 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d Pgno ovfl =
1e6d6 20 67 65 74 34 62 79 74 65 28 26 7a 5b 69 6e 66 get4byte(&z[inf
1e6d7 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 o.iOverflow]);.
1e6d8 20 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 74 ptrmapGet
1e6d9 28 70 42 74 2c 20 6f 76 66 6c 2c 20 26 65 2c 20 (pBt, ovfl, &e,
1e6da 26 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 &n);. ass
1e6db 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 ert( n==pPage->p
1e6dc 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 gno && e==PTRMAP
1e6dd 5f 4f 56 45 52 46 4c 4f 57 31 20 29 3b 0a 20 20 _OVERFLOW1 );.
1e6de 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1e6df 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
1e6e0 20 20 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 Pgno chi
1e6e1 6c 64 20 3d 20 67 65 74 34 62 79 74 65 28 7a 29 ld = get4byte(z)
1e6e2 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 ;. ptrmap
1e6e3 47 65 74 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 Get(pBt, child,
1e6e4 26 65 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 20 &e, &n);.
1e6e5 20 61 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 assert( n==pPag
1e6e6 65 2d 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 e->pgno && e==PT
1e6e7 52 4d 41 50 5f 42 54 52 45 45 20 29 3b 0a 20 20 RMAP_BTREE );.
1e6e8 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1e6e9 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
1e6ea 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 ){. Pgno c
1e6eb 68 69 6c 64 20 3d 20 67 65 74 34 62 79 74 65 28 hild = get4byte(
1e6ec 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 &pPage->aData[pP
1e6ed 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 age->hdrOffset+8
1e6ee 5d 29 3b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 ]);. ptrmap
1e6ef 47 65 74 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 Get(pBt, child,
1e6f0 26 65 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 61 &e, &n);. a
1e6f1 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d ssert( n==pPage-
1e6f2 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d >pgno && e==PTRM
1e6f3 41 50 5f 42 54 52 45 45 20 29 3b 0a 20 20 20 20 AP_BTREE );.
1e6f4 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 }. }. return 1
1e6f5 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1e6f6 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1e6f7 69 73 20 75 73 65 64 20 74 6f 20 63 6f 70 79 20 is used to copy
1e6f8 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
1e6f9 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 65 20 the b-tree node
1e6fa 73 74 6f 72 65 64 20 0a 2a 2a 20 6f 6e 20 70 61 stored .** on pa
1e6fb 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 ge pFrom to page
1e6fc 20 70 54 6f 2e 20 49 66 20 70 61 67 65 20 70 46 pTo. If page pF
1e6fd 72 6f 6d 20 77 61 73 20 6e 6f 74 20 61 20 6c 65 rom was not a le
1e6fe 61 66 20 70 61 67 65 2c 20 74 68 65 6e 0a 2a 2a af page, then.**
1e6ff 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
1e700 20 65 6e 74 72 69 65 73 20 66 6f 72 20 65 61 63 entries for eac
1e701 68 20 63 68 69 6c 64 20 70 61 67 65 20 61 72 65 h child page are
1e702 20 75 70 64 61 74 65 64 20 73 6f 20 74 68 61 74 updated so that
1e703 20 74 68 65 0a 2a 2a 20 70 61 72 65 6e 74 20 70 the.** parent p
1e704 61 67 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 age stored in th
1e705 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 e pointer map is
1e706 20 70 61 67 65 20 70 54 6f 2e 20 49 66 20 70 46 page pTo. If pF
1e707 72 6f 6d 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a rom contained.**
1e708 20 61 6e 79 20 63 65 6c 6c 73 20 77 69 74 68 20 any cells with
1e709 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 6f overflow page po
1e70a 69 6e 74 65 72 73 2c 20 74 68 65 6e 20 74 68 65 inters, then the
1e70b 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 corresponding p
1e70c 6f 69 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e ointer.** map en
1e70d 74 72 69 65 73 20 61 72 65 20 61 6c 73 6f 20 75 tries are also u
1e70e 70 64 61 74 65 64 20 73 6f 20 74 68 61 74 20 74 pdated so that t
1e70f 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 he parent page i
1e710 73 20 70 61 67 65 20 70 54 6f 2e 0a 2a 2a 0a 2a s page pTo..**.*
1e711 2a 20 49 66 20 70 46 72 6f 6d 20 69 73 20 63 75 * If pFrom is cu
1e712 72 72 65 6e 74 6c 79 20 63 61 72 72 79 69 6e 67 rrently carrying
1e713 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 65 any overflow ce
1e714 6c 6c 73 20 28 65 6e 74 72 69 65 73 20 69 6e 20 lls (entries in
1e715 74 68 65 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 the.** MemPage.a
1e716 70 4f 76 66 6c 5b 5d 20 61 72 72 61 79 29 2c 20 pOvfl[] array),
1e717 74 68 65 79 20 61 72 65 20 6e 6f 74 20 63 6f 70 they are not cop
1e718 69 65 64 20 74 6f 20 70 54 6f 2e 20 0a 2a 2a 0a ied to pTo. .**.
1e719 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e ** Before return
1e71a 69 6e 67 2c 20 70 61 67 65 20 70 54 6f 20 69 73 ing, page pTo is
1e71b 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 20 75 reinitialized u
1e71c 73 69 6e 67 20 62 74 72 65 65 49 6e 69 74 50 61 sing btreeInitPa
1e71d 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ge()..**.** The
1e71e 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 66 20 74 performance of t
1e71f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
1e720 6e 6f 74 20 63 72 69 74 69 63 61 6c 2e 20 49 74 not critical. It
1e721 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 is only used by
1e722 20 0a 2a 2a 20 74 68 65 20 62 61 6c 61 6e 63 65 .** the balance
1e723 5f 73 68 61 6c 6c 6f 77 65 72 28 29 20 61 6e 64 _shallower() and
1e724 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 balance_deeper(
1e725 29 20 70 72 6f 63 65 64 75 72 65 73 2c 20 6e 65 ) procedures, ne
1e726 69 74 68 65 72 20 6f 66 0a 2a 2a 20 77 68 69 63 ither of.** whic
1e727 68 20 61 72 65 20 63 61 6c 6c 65 64 20 6f 66 74 h are called oft
1e728 65 6e 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 en under normal
1e729 63 69 72 63 75 6d 73 74 61 6e 63 65 73 2e 0a 2a circumstances..*
1e72a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f /.static void co
1e72b 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 4d 65 pyNodeContent(Me
1e72c 6d 50 61 67 65 20 2a 70 46 72 6f 6d 2c 20 4d 65 mPage *pFrom, Me
1e72d 6d 50 61 67 65 20 2a 70 54 6f 2c 20 69 6e 74 20 mPage *pTo, int
1e72e 2a 70 52 43 29 7b 0a 20 20 69 66 28 20 28 2a 70 *pRC){. if( (*p
1e72f 52 43 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 RC)==SQLITE_OK )
1e730 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a {. BtShared *
1e731 20 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 46 72 const pBt = pFr
1e732 6f 6d 2d 3e 70 42 74 3b 0a 20 20 20 20 75 38 20 om->pBt;. u8
1e733 2a 20 63 6f 6e 73 74 20 61 46 72 6f 6d 20 3d 20 * const aFrom =
1e734 70 46 72 6f 6d 2d 3e 61 44 61 74 61 3b 0a 20 20 pFrom->aData;.
1e735 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 61 54 6f u8 * const aTo
1e736 20 3d 20 70 54 6f 2d 3e 61 44 61 74 61 3b 0a 20 = pTo->aData;.
1e737 20 20 20 69 6e 74 20 63 6f 6e 73 74 20 69 46 72 int const iFr
1e738 6f 6d 48 64 72 20 3d 20 70 46 72 6f 6d 2d 3e 68 omHdr = pFrom->h
1e739 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 69 6e drOffset;. in
1e73a 74 20 63 6f 6e 73 74 20 69 54 6f 48 64 72 20 3d t const iToHdr =
1e73b 20 28 28 70 54 6f 2d 3e 70 67 6e 6f 3d 3d 31 29 ((pTo->pgno==1)
1e73c 20 3f 20 31 30 30 20 3a 20 30 29 3b 0a 20 20 20 ? 100 : 0);.
1e73d 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74 int rc;. int
1e73e 20 69 44 61 74 61 3b 0a 20 20 0a 20 20 0a 20 20 iData;. . .
1e73f 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d assert( pFrom-
1e740 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 61 >isInit );. a
1e741 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 6e 46 ssert( pFrom->nF
1e742 72 65 65 3e 3d 69 54 6f 48 64 72 20 29 3b 0a 20 ree>=iToHdr );.
1e743 20 20 20 61 73 73 65 72 74 28 20 67 65 74 32 62 assert( get2b
1e744 79 74 65 28 26 61 46 72 6f 6d 5b 69 46 72 6f 6d yte(&aFrom[iFrom
1e745 48 64 72 2b 35 5d 29 20 3c 3d 20 28 69 6e 74 29 Hdr+5]) <= (int)
1e746 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
1e747 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f 70 );. . /* Cop
1e748 79 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 y the b-tree nod
1e749 65 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 70 e content from p
1e74a 61 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 age pFrom to pag
1e74b 65 20 70 54 6f 2e 20 2a 2f 0a 20 20 20 20 69 44 e pTo. */. iD
1e74c 61 74 61 20 3d 20 67 65 74 32 62 79 74 65 28 26 ata = get2byte(&
1e74d 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 aFrom[iFromHdr+5
1e74e 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 ]);. memcpy(&
1e74f 61 54 6f 5b 69 44 61 74 61 5d 2c 20 26 61 46 72 aTo[iData], &aFr
1e750 6f 6d 5b 69 44 61 74 61 5d 2c 20 70 42 74 2d 3e om[iData], pBt->
1e751 75 73 61 62 6c 65 53 69 7a 65 2d 69 44 61 74 61 usableSize-iData
1e752 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 );. memcpy(&a
1e753 54 6f 5b 69 54 6f 48 64 72 5d 2c 20 26 61 46 72 To[iToHdr], &aFr
1e754 6f 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c 20 70 46 om[iFromHdr], pF
1e755 72 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 rom->cellOffset
1e756 2b 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c + 2*pFrom->nCell
1e757 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 69 );. . /* Rei
1e758 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 65 20 70 nitialize page p
1e759 54 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 To so that the c
1e75a 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 4d ontents of the M
1e75b 65 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 emPage structure
1e75c 0a 20 20 20 20 2a 2a 20 6d 61 74 63 68 20 74 68 . ** match th
1e75d 65 20 6e 65 77 20 64 61 74 61 2e 20 54 68 65 20 e new data. The
1e75e 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f initialization o
1e75f 66 20 70 54 6f 20 63 61 6e 20 61 63 74 75 61 6c f pTo can actual
1e760 6c 79 20 66 61 69 6c 20 75 6e 64 65 72 0a 20 20 ly fail under.
1e761 20 20 2a 2a 20 66 61 69 72 6c 79 20 6f 62 73 63 ** fairly obsc
1e762 75 72 65 20 63 69 72 63 75 6d 73 74 61 6e 63 65 ure circumstance
1e763 73 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 69 s, even though i
1e764 74 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 69 t is a copy of i
1e765 6e 69 74 69 61 6c 69 7a 65 64 20 0a 20 20 20 20 nitialized .
1e766 2a 2a 20 70 61 67 65 20 70 46 72 6f 6d 2e 0a 20 ** page pFrom..
1e767 20 20 20 2a 2f 0a 20 20 20 20 70 54 6f 2d 3e 69 */. pTo->i
1e768 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 72 sInit = 0;. r
1e769 63 20 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67 c = btreeInitPag
1e76a 65 28 70 54 6f 29 3b 0a 20 20 20 20 69 66 28 20 e(pTo);. if(
1e76b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1e76c 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20 72 63 . *pRC = rc
1e76d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a ;. return;.
1e76e 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
1e76f 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 If this is an au
1e770 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 to-vacuum databa
1e771 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 se, update the p
1e772 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 ointer-map entri
1e773 65 73 0a 20 20 20 20 2a 2a 20 66 6f 72 20 61 6e es. ** for an
1e774 79 20 62 2d 74 72 65 65 20 6f 72 20 6f 76 65 72 y b-tree or over
1e775 66 6c 6f 77 20 70 61 67 65 73 20 74 68 61 74 20 flow pages that
1e776 70 54 6f 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 pTo now contains
1e777 20 74 68 65 20 70 6f 69 6e 74 65 72 73 20 74 6f the pointers to
1e778 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1e779 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b ISAUTOVACUUM ){
1e77a 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20 73 65 . *pRC = se
1e77b 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 70 54 tChildPtrmaps(pT
1e77c 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a o);. }. }.}.
1e77d 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
1e77e 69 6e 65 20 72 65 64 69 73 74 72 69 62 75 74 65 ine redistribute
1e77f 73 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 69 s cells on the i
1e780 50 61 72 65 6e 74 49 64 78 27 74 68 20 63 68 69 ParentIdx'th chi
1e781 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 0a 2a 2a ld of pParent.**
1e782 20 28 68 65 72 65 61 66 74 65 72 20 22 74 68 65 (hereafter "the
1e783 20 70 61 67 65 22 29 20 61 6e 64 20 75 70 20 74 page") and up t
1e784 6f 20 32 20 73 69 62 6c 69 6e 67 73 20 73 6f 20 o 2 siblings so
1e785 74 68 61 74 20 61 6c 6c 20 70 61 67 65 73 20 68 that all pages h
1e786 61 76 65 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a ave about the.**
1e787 20 73 61 6d 65 20 61 6d 6f 75 6e 74 20 6f 66 20 same amount of
1e788 66 72 65 65 20 73 70 61 63 65 2e 20 55 73 75 61 free space. Usua
1e789 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 73 69 62 lly a single sib
1e78a 6c 69 6e 67 20 6f 6e 20 65 69 74 68 65 72 20 73 ling on either s
1e78b 69 64 65 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 ide of the.** pa
1e78c 67 65 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 ge are used in t
1e78d 68 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 he balancing, th
1e78e 6f 75 67 68 20 62 6f 74 68 20 73 69 62 6c 69 6e ough both siblin
1e78f 67 73 20 6d 69 67 68 74 20 63 6f 6d 65 20 66 72 gs might come fr
1e790 6f 6d 20 6f 6e 65 0a 2a 2a 20 73 69 64 65 20 69 om one.** side i
1e791 66 20 74 68 65 20 70 61 67 65 20 69 73 20 74 68 f the page is th
1e792 65 20 66 69 72 73 74 20 6f 72 20 6c 61 73 74 20 e first or last
1e793 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61 72 child of its par
1e794 65 6e 74 2e 20 49 66 20 74 68 65 20 70 61 67 65 ent. If the page
1e795 20 0a 2a 2a 20 68 61 73 20 66 65 77 65 72 20 74 .** has fewer t
1e796 68 61 6e 20 32 20 73 69 62 6c 69 6e 67 73 20 28 han 2 siblings (
1e797 73 6f 6d 65 74 68 69 6e 67 20 77 68 69 63 68 20 something which
1e798 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 can only happen
1e799 69 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 if the page.** i
1e79a 73 20 61 20 72 6f 6f 74 20 70 61 67 65 20 6f 72 s a root page or
1e79b 20 61 20 63 68 69 6c 64 20 6f 66 20 61 20 72 6f a child of a ro
1e79c 6f 74 20 70 61 67 65 29 20 74 68 65 6e 20 61 6c ot page) then al
1e79d 6c 20 61 76 61 69 6c 61 62 6c 65 20 73 69 62 6c l available sibl
1e79e 69 6e 67 73 0a 2a 2a 20 70 61 72 74 69 63 69 70 ings.** particip
1e79f 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e ate in the balan
1e7a0 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 cing..**.** The
1e7a1 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e number of siblin
1e7a2 67 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 6d gs of the page m
1e7a3 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 65 ight be increase
1e7a4 64 20 6f 72 20 64 65 63 72 65 61 73 65 64 20 62 d or decreased b
1e7a5 79 20 0a 2a 2a 20 6f 6e 65 20 6f 72 20 74 77 6f y .** one or two
1e7a6 20 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f in an effort to
1e7a7 20 6b 65 65 70 20 70 61 67 65 73 20 6e 65 61 72 keep pages near
1e7a8 6c 79 20 66 75 6c 6c 20 62 75 74 20 6e 6f 74 20 ly full but not
1e7a9 6f 76 65 72 20 66 75 6c 6c 2e 20 0a 2a 2a 0a 2a over full. .**.*
1e7aa 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e * Note that when
1e7ab 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1e7ac 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f 66 called, some of
1e7ad 20 74 68 65 20 63 65 6c 6c 73 20 6f 6e 20 74 68 the cells on th
1e7ae 65 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20 e page.** might
1e7af 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20 not actually be
1e7b0 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 stored in MemPag
1e7b1 65 2e 61 44 61 74 61 5b 5d 2e 20 54 68 69 73 20 e.aData[]. This
1e7b2 63 61 6e 20 68 61 70 70 65 6e 0a 2a 2a 20 69 66 can happen.** if
1e7b3 20 74 68 65 20 70 61 67 65 20 69 73 20 6f 76 65 the page is ove
1e7b4 72 66 75 6c 6c 2e 20 54 68 69 73 20 72 6f 75 74 rfull. This rout
1e7b5 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68 61 74 ine ensures that
1e7b6 20 61 6c 6c 20 63 65 6c 6c 73 20 61 6c 6c 6f 63 all cells alloc
1e7b7 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 70 ated.** to the p
1e7b8 61 67 65 20 61 6e 64 20 69 74 73 20 73 69 62 6c age and its sibl
1e7b9 69 6e 67 73 20 66 69 74 20 69 6e 74 6f 20 4d 65 ings fit into Me
1e7ba 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d 20 62 65 mPage.aData[] be
1e7bb 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a fore returning..
1e7bc 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f 75 **.** In the cou
1e7bd 72 73 65 20 6f 66 20 62 61 6c 61 6e 63 69 6e 67 rse of balancing
1e7be 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 69 74 the page and it
1e7bf 73 20 73 69 62 6c 69 6e 67 73 2c 20 63 65 6c 6c s siblings, cell
1e7c0 73 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 73 65 s may be.** inse
1e7c1 72 74 65 64 20 69 6e 74 6f 20 6f 72 20 72 65 6d rted into or rem
1e7c2 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 oved from the pa
1e7c3 72 65 6e 74 20 70 61 67 65 20 28 70 50 61 72 65 rent page (pPare
1e7c4 6e 74 29 2e 20 44 6f 69 6e 67 20 73 6f 0a 2a 2a nt). Doing so.**
1e7c5 20 6d 61 79 20 63 61 75 73 65 20 74 68 65 20 70 may cause the p
1e7c6 61 72 65 6e 74 20 70 61 67 65 20 74 6f 20 62 65 arent page to be
1e7c7 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f 72 come overfull or
1e7c8 20 75 6e 64 65 72 66 75 6c 6c 2e 20 49 66 20 74 underfull. If t
1e7c9 68 69 73 0a 2a 2a 20 68 61 70 70 65 6e 73 2c 20 his.** happens,
1e7ca 69 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e it is the respon
1e7cb 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 sibility of the
1e7cc 63 61 6c 6c 65 72 20 74 6f 20 69 6e 76 6f 6b 65 caller to invoke
1e7cd 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 the correct.**
1e7ce 62 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e balancing routin
1e7cf 65 20 74 6f 20 66 69 78 20 74 68 69 73 20 70 72 e to fix this pr
1e7d0 6f 62 6c 65 6d 20 28 73 65 65 20 74 68 65 20 62 oblem (see the b
1e7d1 61 6c 61 6e 63 65 28 29 20 72 6f 75 74 69 6e 65 alance() routine
1e7d2 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 ). .**.** If thi
1e7d3 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 s routine fails
1e7d4 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2c 20 for any reason,
1e7d5 69 74 20 6d 69 67 68 74 20 6c 65 61 76 65 20 74 it might leave t
1e7d6 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 he database.** i
1e7d7 6e 20 61 20 63 6f 72 72 75 70 74 65 64 20 73 74 n a corrupted st
1e7d8 61 74 65 2e 20 53 6f 20 69 66 20 74 68 69 73 20 ate. So if this
1e7d9 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 2c 20 74 routine fails, t
1e7da 68 65 20 64 61 74 61 62 61 73 65 20 73 68 6f 75 he database shou
1e7db 6c 64 0a 2a 2a 20 62 65 20 72 6f 6c 6c 65 64 20 ld.** be rolled
1e7dc 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 back..**.** The
1e7dd 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 74 third argument t
1e7de 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c o this function,
1e7df 20 61 4f 76 66 6c 53 70 61 63 65 2c 20 69 73 20 aOvflSpace, is
1e7e0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 0a 2a a pointer to a.*
1e7e1 2a 20 62 75 66 66 65 72 20 62 69 67 20 65 6e 6f * buffer big eno
1e7e2 75 67 68 20 74 6f 20 68 6f 6c 64 20 6f 6e 65 20 ugh to hold one
1e7e3 70 61 67 65 2e 20 49 66 20 77 68 69 6c 65 20 69 page. If while i
1e7e4 6e 73 65 72 74 69 6e 67 20 63 65 6c 6c 73 20 69 nserting cells i
1e7e5 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 0a 2a nto the parent.*
1e7e6 2a 20 70 61 67 65 20 28 70 50 61 72 65 6e 74 29 * page (pParent)
1e7e7 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
1e7e8 20 62 65 63 6f 6d 65 73 20 6f 76 65 72 66 75 6c becomes overful
1e7e9 6c 2c 20 74 68 69 73 20 62 75 66 66 65 72 20 69 l, this buffer i
1e7ea 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f s.** used to sto
1e7eb 72 65 20 74 68 65 20 70 61 72 65 6e 74 27 73 20 re the parent's
1e7ec 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 20 overflow cells.
1e7ed 42 65 63 61 75 73 65 20 74 68 69 73 20 66 75 6e Because this fun
1e7ee 63 74 69 6f 6e 20 69 6e 73 65 72 74 73 0a 2a 2a ction inserts.**
1e7ef 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20 66 6f a maximum of fo
1e7f0 75 72 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 ur divider cells
1e7f1 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 into the parent
1e7f2 20 70 61 67 65 2c 20 61 6e 64 20 74 68 65 20 6d page, and the m
1e7f3 61 78 69 6d 75 6d 0a 2a 2a 20 73 69 7a 65 20 6f aximum.** size o
1e7f4 66 20 61 20 63 65 6c 6c 20 73 74 6f 72 65 64 20 f a cell stored
1e7f5 77 69 74 68 69 6e 20 61 6e 20 69 6e 74 65 72 6e within an intern
1e7f6 61 6c 20 6e 6f 64 65 20 69 73 20 61 6c 77 61 79 al node is alway
1e7f7 73 20 6c 65 73 73 20 74 68 61 6e 20 31 2f 34 0a s less than 1/4.
1e7f8 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2d 73 ** of the page-s
1e7f9 69 7a 65 2c 20 74 68 65 20 61 4f 76 66 6c 53 70 ize, the aOvflSp
1e7fa 61 63 65 5b 5d 20 62 75 66 66 65 72 20 69 73 20 ace[] buffer is
1e7fb 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 guaranteed to be
1e7fc 20 6c 61 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68 large.** enough
1e7fd 20 66 6f 72 20 61 6c 6c 20 6f 76 65 72 66 6c 6f for all overflo
1e7fe 77 20 63 65 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49 w cells..**.** I
1e7ff 66 20 61 4f 76 66 6c 53 70 61 63 65 20 69 73 20 f aOvflSpace is
1e800 73 65 74 20 74 6f 20 61 20 6e 75 6c 6c 20 70 6f set to a null po
1e801 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e 63 inter, this func
1e802 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a 2a 2a tion returns .**
1e803 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a SQLITE_NOMEM..*
1e804 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 4d /.#if defined(_M
1e805 53 43 5f 56 45 52 29 20 26 26 20 5f 4d 53 43 5f SC_VER) && _MSC_
1e806 56 45 52 20 3e 3d 20 31 37 30 30 20 26 26 20 64 VER >= 1700 && d
1e807 65 66 69 6e 65 64 28 5f 4d 5f 41 52 4d 29 0a 23 efined(_M_ARM).#
1e808 70 72 61 67 6d 61 20 6f 70 74 69 6d 69 7a 65 28 pragma optimize(
1e809 22 22 2c 20 6f 66 66 29 0a 23 65 6e 64 69 66 0a "", off).#endif.
1e80a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e static int balan
1e80b 63 65 5f 6e 6f 6e 72 6f 6f 74 28 0a 20 20 4d 65 ce_nonroot(. Me
1e80c 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 mPage *pParent,
1e80d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1e80e 20 50 61 72 65 6e 74 20 70 61 67 65 20 6f 66 20 Parent page of
1e80f 73 69 62 6c 69 6e 67 73 20 62 65 69 6e 67 20 62 siblings being b
1e810 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 69 6e 74 alanced */. int
1e811 20 69 50 61 72 65 6e 74 49 64 78 2c 20 20 20 20 iParentIdx,
1e812 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e813 49 6e 64 65 78 20 6f 66 20 22 74 68 65 20 70 61 Index of "the pa
1e814 67 65 22 20 69 6e 20 70 50 61 72 65 6e 74 20 2a ge" in pParent *
1e815 2f 0a 20 20 75 38 20 2a 61 4f 76 66 6c 53 70 61 /. u8 *aOvflSpa
1e816 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ce,
1e817 20 20 20 20 2f 2a 20 70 61 67 65 2d 73 69 7a 65 /* page-size
1e818 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 bytes of space
1e819 66 6f 72 20 70 61 72 65 6e 74 20 6f 76 66 6c 20 for parent ovfl
1e81a 2a 2f 0a 20 20 69 6e 74 20 69 73 52 6f 6f 74 2c */. int isRoot,
1e81b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e81c 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1e81d 70 50 61 72 65 6e 74 20 69 73 20 61 20 72 6f 6f pParent is a roo
1e81e 74 2d 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 t-page */. int
1e81f 62 42 75 6c 6b 20 20 20 20 20 20 20 20 20 20 20 bBulk
1e820 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1e821 72 75 65 20 69 66 20 74 68 69 73 20 63 61 6c 6c rue if this call
1e822 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 62 75 is part of a bu
1e823 6c 6b 20 6c 6f 61 64 20 2a 2f 0a 29 7b 0a 20 20 lk load */.){.
1e824 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 BtShared *pBt;
1e825 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e826 54 68 65 20 77 68 6f 6c 65 20 64 61 74 61 62 61 The whole databa
1e827 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c se */. int nCel
1e828 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 l = 0;
1e829 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1e82a 66 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c f cells in apCel
1e82b 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 l[] */. int nMa
1e82c 78 43 65 6c 6c 73 20 3d 20 30 3b 20 20 20 20 20 xCells = 0;
1e82d 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 /* Allocat
1e82e 65 64 20 73 69 7a 65 20 6f 66 20 61 70 43 65 6c ed size of apCel
1e82f 6c 2c 20 73 7a 43 65 6c 6c 2c 20 61 46 72 6f 6d l, szCell, aFrom
1e830 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 77 20 . */. int nNew
1e831 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
1e832 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1e833 20 70 61 67 65 73 20 69 6e 20 61 70 4e 65 77 5b pages in apNew[
1e834 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b ] */. int nOld;
1e835 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e836 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1e837 20 70 61 67 65 73 20 69 6e 20 61 70 4f 6c 64 5b pages in apOld[
1e838 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 2c ] */. int i, j,
1e839 20 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 k;
1e83a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1e83b 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 78 ters */. int nx
1e83c 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 Div;
1e83d 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 64 /* Next d
1e83e 69 76 69 64 65 72 20 73 6c 6f 74 20 69 6e 20 70 ivider slot in p
1e83f 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 Parent->aCell[]
1e840 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
1e841 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 LITE_OK;
1e842 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 /* The return
1e843 63 6f 64 65 20 2a 2f 0a 20 20 75 31 36 20 6c 65 code */. u16 le
1e844 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 20 20 20 afCorrection;
1e845 20 20 20 20 20 20 20 2f 2a 20 34 20 69 66 20 70 /* 4 if p
1e846 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20 Page is a leaf.
1e847 20 30 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 69 0 if not */. i
1e848 6e 74 20 6c 65 61 66 44 61 74 61 3b 20 20 20 20 nt leafData;
1e849 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1e84a 72 75 65 20 69 66 20 70 50 61 67 65 20 69 73 20 rue if pPage is
1e84b 61 20 6c 65 61 66 20 6f 66 20 61 20 4c 45 41 46 a leaf of a LEAF
1e84c 44 41 54 41 20 74 72 65 65 20 2a 2f 0a 20 20 69 DATA tree */. i
1e84d 6e 74 20 75 73 61 62 6c 65 53 70 61 63 65 3b 20 nt usableSpace;
1e84e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
1e84f 79 74 65 73 20 69 6e 20 70 50 61 67 65 20 62 65 ytes in pPage be
1e850 79 6f 6e 64 20 74 68 65 20 68 65 61 64 65 72 20 yond the header
1e851 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65 46 6c 61 */. int pageFla
1e852 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
1e853 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 70 50 /* Value of pP
1e854 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 2a 2f age->aData[0] */
1e855 0a 20 20 69 6e 74 20 73 75 62 74 6f 74 61 6c 3b . int subtotal;
1e856 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e857 2f 2a 20 53 75 62 74 6f 74 61 6c 20 6f 66 20 62 /* Subtotal of b
1e858 79 74 65 73 20 69 6e 20 63 65 6c 6c 73 20 6f 6e ytes in cells on
1e859 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 one page */. i
1e85a 6e 74 20 69 53 70 61 63 65 31 20 3d 20 30 3b 20 nt iSpace1 = 0;
1e85b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
1e85c 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65 irst unused byte
1e85d 20 6f 66 20 61 53 70 61 63 65 31 5b 5d 20 2a 2f of aSpace1[] */
1e85e 0a 20 20 69 6e 74 20 69 4f 76 66 6c 53 70 61 63 . int iOvflSpac
1e85f 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 e = 0;
1e860 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20 /* First unused
1e861 62 79 74 65 20 6f 66 20 61 4f 76 66 6c 53 70 61 byte of aOvflSpa
1e862 63 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 7a ce[] */. int sz
1e863 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 Scratch;
1e864 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1e865 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 f scratch memory
1e866 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 requested */.
1e867 4d 65 6d 50 61 67 65 20 2a 61 70 4f 6c 64 5b 4e MemPage *apOld[N
1e868 42 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 B]; /*
1e869 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 pPage and up to
1e86a 74 77 6f 20 73 69 62 6c 69 6e 67 73 20 2a 2f 0a two siblings */.
1e86b 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 43 6f 70 MemPage *apCop
1e86c 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 2f y[NB]; /
1e86d 2a 20 50 72 69 76 61 74 65 20 63 6f 70 69 65 73 * Private copies
1e86e 20 6f 66 20 61 70 4f 6c 64 5b 5d 20 70 61 67 65 of apOld[] page
1e86f 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a s */. MemPage *
1e870 61 70 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 apNew[NB+2];
1e871 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e 64 /* pPage and
1e872 20 75 70 20 74 6f 20 4e 42 20 73 69 62 6c 69 6e up to NB siblin
1e873 67 73 20 61 66 74 65 72 20 62 61 6c 61 6e 63 69 gs after balanci
1e874 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 70 52 69 67 ng */. u8 *pRig
1e875 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ht;
1e876 20 20 20 20 20 2f 2a 20 4c 6f 63 61 74 69 6f 6e /* Location
1e877 20 69 6e 20 70 61 72 65 6e 74 20 6f 66 20 72 69 in parent of ri
1e878 67 68 74 2d 73 69 62 6c 69 6e 67 20 70 6f 69 6e ght-sibling poin
1e879 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44 ter */. u8 *apD
1e87a 69 76 5b 4e 42 2d 31 5d 3b 20 20 20 20 20 20 20 iv[NB-1];
1e87b 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64 65 72 /* Divider
1e87c 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e cells in pParen
1e87d 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65 t */. int cntNe
1e87e 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 w[NB+2];
1e87f 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 /* Index in
1e880 61 43 65 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 aCell[] of cell
1e881 61 66 74 65 72 20 69 2d 74 68 20 70 61 67 65 20 after i-th page
1e882 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e */. int szNew[N
1e883 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 B+2];
1e884 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69 /* Combined si
1e885 7a 65 20 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63 ze of cells plac
1e886 65 20 6f 6e 20 69 2d 74 68 20 70 61 67 65 20 2a e on i-th page *
1e887 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20 /. u8 **apCell
1e888 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
1e889 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65 /* All cells be
1e88a 67 69 6e 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a gin balanced */.
1e88b 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 u16 *szCell;
1e88c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1e88d 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 * Local size of
1e88e 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 all cells in apC
1e88f 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 ell[] */. u8 *a
1e890 53 70 61 63 65 31 3b 20 20 20 20 20 20 20 20 20 Space1;
1e891 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 /* Space
1e892 20 66 6f 72 20 63 6f 70 69 65 73 20 6f 66 20 64 for copies of d
1e893 69 76 69 64 65 72 73 20 63 65 6c 6c 73 20 2a 2f ividers cells */
1e894 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 . Pgno pgno;
1e895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e896 2f 2a 20 54 65 6d 70 20 76 61 72 20 74 6f 20 73 /* Temp var to s
1e897 74 6f 72 65 20 61 20 70 61 67 65 20 6e 75 6d 62 tore a page numb
1e898 65 72 20 69 6e 20 2a 2f 0a 0a 20 20 70 42 74 20 er in */.. pBt
1e899 3d 20 70 50 61 72 65 6e 74 2d 3e 70 42 74 3b 0a = pParent->pBt;.
1e89a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1e89b 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
1e89c 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
1e89d 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
1e89e 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
1e89f 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 arent->pDbPage)
1e8a0 29 3b 0a 0a 23 69 66 20 30 0a 20 20 54 52 41 43 );..#if 0. TRAC
1e8a1 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 62 65 67 E(("BALANCE: beg
1e8a2 69 6e 20 70 61 67 65 20 25 64 20 63 68 69 6c 64 in page %d child
1e8a3 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 of %d\n", pPage
1e8a4 2d 3e 70 67 6e 6f 2c 20 70 50 61 72 65 6e 74 2d ->pgno, pParent-
1e8a5 3e 70 67 6e 6f 29 29 3b 0a 23 65 6e 64 69 66 0a >pgno));.#endif.
1e8a6 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f . /* At this po
1e8a7 69 6e 74 20 70 50 61 72 65 6e 74 20 6d 61 79 20 int pParent may
1e8a8 68 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 have at most one
1e8a9 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2e 20 overflow cell.
1e8aa 41 6e 64 20 69 66 0a 20 20 2a 2a 20 74 68 69 73 And if. ** this
1e8ab 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69 overflow cell i
1e8ac 73 20 70 72 65 73 65 6e 74 2c 20 69 74 20 6d 75 s present, it mu
1e8ad 73 74 20 62 65 20 74 68 65 20 63 65 6c 6c 20 77 st be the cell w
1e8ae 69 74 68 20 0a 20 20 2a 2a 20 69 6e 64 65 78 20 ith . ** index
1e8af 69 50 61 72 65 6e 74 49 64 78 2e 20 54 68 69 73 iParentIdx. This
1e8b0 20 73 63 65 6e 61 72 69 6f 20 63 6f 6d 65 73 20 scenario comes
1e8b1 61 62 6f 75 74 20 77 68 65 6e 20 74 68 69 73 20 about when this
1e8b2 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 function. ** is
1e8b3 20 63 61 6c 6c 65 64 20 28 69 6e 64 69 72 65 63 called (indirec
1e8b4 74 6c 79 29 20 66 72 6f 6d 20 73 71 6c 69 74 65 tly) from sqlite
1e8b5 33 42 74 72 65 65 44 65 6c 65 74 65 28 29 2e 0a 3BtreeDelete()..
1e8b6 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
1e8b7 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f Parent->nOverflo
1e8b8 77 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e 74 2d w==0 || pParent-
1e8b9 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 29 3b >nOverflow==1 );
1e8ba 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 . assert( pPare
1e8bb 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 nt->nOverflow==0
1e8bc 20 7c 7c 20 70 50 61 72 65 6e 74 2d 3e 61 69 4f || pParent->aiO
1e8bd 76 66 6c 5b 30 5d 3d 3d 69 50 61 72 65 6e 74 49 vfl[0]==iParentI
1e8be 64 78 20 29 3b 0a 0a 20 20 69 66 28 20 21 61 4f dx );.. if( !aO
1e8bf 76 66 6c 53 70 61 63 65 20 29 7b 0a 20 20 20 20 vflSpace ){.
1e8c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
1e8c1 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 MEM;. }.. /* F
1e8c2 69 6e 64 20 74 68 65 20 73 69 62 6c 69 6e 67 20 ind the sibling
1e8c3 70 61 67 65 73 20 74 6f 20 62 61 6c 61 6e 63 65 pages to balance
1e8c4 2e 20 41 6c 73 6f 20 6c 6f 63 61 74 65 20 74 68 . Also locate th
1e8c5 65 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 e cells in pPare
1e8c6 6e 74 20 0a 20 20 2a 2a 20 74 68 61 74 20 64 69 nt . ** that di
1e8c7 76 69 64 65 20 74 68 65 20 73 69 62 6c 69 6e 67 vide the sibling
1e8c8 73 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 69 73 s. An attempt is
1e8c9 20 6d 61 64 65 20 74 6f 20 66 69 6e 64 20 4e 4e made to find NN
1e8ca 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 0a 20 20 siblings on .
1e8cb 2a 2a 20 65 69 74 68 65 72 20 73 69 64 65 20 6f ** either side o
1e8cc 66 20 70 50 61 67 65 2e 20 4d 6f 72 65 20 73 69 f pPage. More si
1e8cd 62 6c 69 6e 67 73 20 61 72 65 20 74 61 6b 65 6e blings are taken
1e8ce 20 66 72 6f 6d 20 6f 6e 65 20 73 69 64 65 2c 20 from one side,
1e8cf 68 6f 77 65 76 65 72 2c 20 0a 20 20 2a 2a 20 69 however, . ** i
1e8d0 66 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65 f there are fewe
1e8d1 72 20 74 68 61 6e 20 4e 4e 20 73 69 62 6c 69 6e r than NN siblin
1e8d2 67 73 20 6f 6e 20 74 68 65 20 6f 74 68 65 72 20 gs on the other
1e8d3 73 69 64 65 2e 20 49 66 20 70 50 61 72 65 6e 74 side. If pParent
1e8d4 0a 20 20 2a 2a 20 68 61 73 20 4e 42 20 6f 72 20 . ** has NB or
1e8d5 66 65 77 65 72 20 63 68 69 6c 64 72 65 6e 20 74 fewer children t
1e8d6 68 65 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e hen all children
1e8d7 20 6f 66 20 70 50 61 72 65 6e 74 20 61 72 65 20 of pParent are
1e8d8 74 61 6b 65 6e 2e 20 20 0a 20 20 2a 2a 0a 20 20 taken. . **.
1e8d9 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20 61 6c 73 ** This loop als
1e8da 6f 20 64 72 6f 70 73 20 74 68 65 20 64 69 76 69 o drops the divi
1e8db 64 65 72 20 63 65 6c 6c 73 20 66 72 6f 6d 20 74 der cells from t
1e8dc 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 he parent page.
1e8dd 54 68 69 73 0a 20 20 2a 2a 20 77 61 79 2c 20 74 This. ** way, t
1e8de 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 he remainder of
1e8df 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 the function doe
1e8e0 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 64 65 s not have to de
1e8e1 61 6c 20 77 69 74 68 20 61 6e 79 0a 20 20 2a 2a al with any. **
1e8e2 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 overflow cells
1e8e3 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 in the parent pa
1e8e4 67 65 2c 20 73 69 6e 63 65 20 69 66 20 61 6e 79 ge, since if any
1e8e5 20 65 78 69 73 74 65 64 20 74 68 65 79 20 77 69 existed they wi
1e8e6 6c 6c 0a 20 20 2a 2a 20 68 61 76 65 20 61 6c 72 ll. ** have alr
1e8e7 65 61 64 79 20 62 65 65 6e 20 72 65 6d 6f 76 65 eady been remove
1e8e8 64 2e 0a 20 20 2a 2f 0a 20 20 69 20 3d 20 70 50 d.. */. i = pP
1e8e9 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 arent->nOverflow
1e8ea 20 2b 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c + pParent->nCel
1e8eb 6c 3b 0a 20 20 69 66 28 20 69 3c 32 20 29 7b 0a l;. if( i<2 ){.
1e8ec 20 20 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 nxDiv = 0;.
1e8ed 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
1e8ee 72 74 28 20 62 42 75 6c 6b 3d 3d 30 20 7c 7c 20 rt( bBulk==0 ||
1e8ef 62 42 75 6c 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 bBulk==1 );.
1e8f0 69 66 28 20 69 50 61 72 65 6e 74 49 64 78 3d 3d if( iParentIdx==
1e8f1 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 0 ){
1e8f2 20 20 20 20 20 0a 20 20 20 20 20 20 6e 78 44 69 . nxDi
1e8f3 76 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 v = 0;. }else
1e8f4 20 69 66 28 20 69 50 61 72 65 6e 74 49 64 78 3d if( iParentIdx=
1e8f5 3d 69 20 29 7b 0a 20 20 20 20 20 20 6e 78 44 69 =i ){. nxDi
1e8f6 76 20 3d 20 69 2d 32 2b 62 42 75 6c 6b 3b 0a 20 v = i-2+bBulk;.
1e8f7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1e8f8 61 73 73 65 72 74 28 20 62 42 75 6c 6b 3d 3d 30 assert( bBulk==0
1e8f9 20 29 3b 0a 20 20 20 20 20 20 6e 78 44 69 76 20 );. nxDiv
1e8fa 3d 20 69 50 61 72 65 6e 74 49 64 78 2d 31 3b 0a = iParentIdx-1;.
1e8fb 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 32 2d }. i = 2-
1e8fc 62 42 75 6c 6b 3b 0a 20 20 7d 0a 20 20 6e 4f 6c bBulk;. }. nOl
1e8fd 64 20 3d 20 69 2b 31 3b 0a 20 20 69 66 28 20 28 d = i+1;. if( (
1e8fe 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d i+nxDiv-pParent-
1e8ff 3e 6e 4f 76 65 72 66 6c 6f 77 29 3d 3d 70 50 61 >nOverflow)==pPa
1e900 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 rent->nCell ){.
1e901 20 20 20 70 52 69 67 68 74 20 3d 20 26 70 50 61 pRight = &pPa
1e902 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 rent->aData[pPar
1e903 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 ent->hdrOffset+8
1e904 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ];. }else{.
1e905 70 52 69 67 68 74 20 3d 20 66 69 6e 64 43 65 6c pRight = findCel
1e906 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 l(pParent, i+nxD
1e907 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 iv-pParent->nOve
1e908 72 66 6c 6f 77 29 3b 0a 20 20 7d 0a 20 20 70 67 rflow);. }. pg
1e909 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 52 no = get4byte(pR
1e90a 69 67 68 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 ight);. while(
1e90b 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 67 65 1 ){. rc = ge
1e90c 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 tAndInitPage(pBt
1e90d 2c 20 70 67 6e 6f 2c 20 26 61 70 4f 6c 64 5b 69 , pgno, &apOld[i
1e90e 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 ]);. if( rc )
1e90f 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 61 {. memset(a
1e910 70 4f 6c 64 2c 20 30 2c 20 28 69 2b 31 29 2a 73 pOld, 0, (i+1)*s
1e911 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 2a 29 29 izeof(MemPage*))
1e912 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c ;. goto bal
1e913 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 ance_cleanup;.
1e914 20 20 7d 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c }. nMaxCell
1e915 73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d s += 1+apOld[i]-
1e916 3e 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d >nCell+apOld[i]-
1e917 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 >nOverflow;.
1e918 69 66 28 20 28 69 2d 2d 29 3d 3d 30 20 29 20 62 if( (i--)==0 ) b
1e919 72 65 61 6b 3b 0a 0a 20 20 20 20 69 66 28 20 69 reak;.. if( i
1e91a 2b 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e 74 2d +nxDiv==pParent-
1e91b 3e 61 69 4f 76 66 6c 5b 30 5d 20 26 26 20 70 50 >aiOvfl[0] && pP
1e91c 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 arent->nOverflow
1e91d 20 29 7b 0a 20 20 20 20 20 20 61 70 44 69 76 5b ){. apDiv[
1e91e 69 5d 20 3d 20 70 50 61 72 65 6e 74 2d 3e 61 70 i] = pParent->ap
1e91f 4f 76 66 6c 5b 30 5d 3b 0a 20 20 20 20 20 20 70 Ovfl[0];. p
1e920 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 61 gno = get4byte(a
1e921 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 pDiv[i]);.
1e922 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 6c 6c 53 szNew[i] = cellS
1e923 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20 izePtr(pParent,
1e924 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 apDiv[i]);.
1e925 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 pParent->nOverf
1e926 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c low = 0;. }el
1e927 73 65 7b 0a 20 20 20 20 20 20 61 70 44 69 76 5b se{. apDiv[
1e928 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 i] = findCell(pP
1e929 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70 arent, i+nxDiv-p
1e92a 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f Parent->nOverflo
1e92b 77 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d w);. pgno =
1e92c 20 67 65 74 34 62 79 74 65 28 61 70 44 69 76 5b get4byte(apDiv[
1e92d 69 5d 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 i]);. szNew
1e92e 5b 69 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 [i] = cellSizePt
1e92f 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44 69 76 r(pParent, apDiv
1e930 5b 69 5d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 [i]);.. /*
1e931 44 72 6f 70 20 74 68 65 20 63 65 6c 6c 20 66 72 Drop the cell fr
1e932 6f 6d 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 om the parent pa
1e933 67 65 2e 20 61 70 44 69 76 5b 69 5d 20 73 74 69 ge. apDiv[i] sti
1e934 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 0a 20 20 20 ll points to.
1e935 20 20 20 2a 2a 20 74 68 65 20 63 65 6c 6c 20 77 ** the cell w
1e936 69 74 68 69 6e 20 74 68 65 20 70 61 72 65 6e 74 ithin the parent
1e937 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 69 74 , even though it
1e938 20 68 61 73 20 62 65 65 6e 20 64 72 6f 70 70 65 has been droppe
1e939 64 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 d.. ** This
1e93a 20 69 73 20 73 61 66 65 20 62 65 63 61 75 73 65 is safe because
1e93b 20 64 72 6f 70 70 69 6e 67 20 61 20 63 65 6c 6c dropping a cell
1e93c 20 6f 6e 6c 79 20 6f 76 65 72 77 72 69 74 65 73 only overwrites
1e93d 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20 20 the first.
1e93e 20 2a 2a 20 66 6f 75 72 20 62 79 74 65 73 20 6f ** four bytes o
1e93f 66 20 69 74 2c 20 61 6e 64 20 74 68 69 73 20 66 f it, and this f
1e940 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 unction does not
1e941 20 6e 65 65 64 20 74 68 65 20 66 69 72 73 74 0a need the first.
1e942 20 20 20 20 20 20 2a 2a 20 66 6f 75 72 20 62 79 ** four by
1e943 74 65 73 20 6f 66 20 74 68 65 20 64 69 76 69 64 tes of the divid
1e944 65 72 20 63 65 6c 6c 2e 20 53 6f 20 74 68 65 20 er cell. So the
1e945 70 6f 69 6e 74 65 72 20 69 73 20 73 61 66 65 20 pointer is safe
1e946 74 6f 20 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 to use. **
1e947 6c 61 74 65 72 20 6f 6e 2e 20 20 0a 20 20 20 20 later on. .
1e948 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 42 75 **. ** Bu
1e949 74 20 6e 6f 74 20 69 66 20 77 65 20 61 72 65 20 t not if we are
1e94a 69 6e 20 73 65 63 75 72 65 2d 64 65 6c 65 74 65 in secure-delete
1e94b 20 6d 6f 64 65 2e 20 49 6e 20 73 65 63 75 72 65 mode. In secure
1e94c 2d 64 65 6c 65 74 65 20 6d 6f 64 65 2c 0a 20 20 -delete mode,.
1e94d 20 20 20 20 2a 2a 20 74 68 65 20 64 72 6f 70 43 ** the dropC
1e94e 65 6c 6c 28 29 20 72 6f 75 74 69 6e 65 20 77 69 ell() routine wi
1e94f 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68 65 ll overwrite the
1e950 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 77 69 74 entire cell wit
1e951 68 20 7a 65 72 6f 65 73 2e 0a 20 20 20 20 20 20 h zeroes..
1e952 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c ** In this case,
1e953 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 63 6f 70 temporarily cop
1e954 79 20 74 68 65 20 63 65 6c 6c 20 69 6e 74 6f 20 y the cell into
1e955 74 68 65 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d the aOvflSpace[]
1e956 0a 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 . ** buffer
1e957 2e 20 49 74 20 77 69 6c 6c 20 62 65 20 63 6f 70 . It will be cop
1e958 69 65 64 20 6f 75 74 20 61 67 61 69 6e 20 61 73 ied out again as
1e959 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 61 53 70 soon as the aSp
1e95a 61 63 65 5b 5d 20 62 75 66 66 65 72 0a 20 20 20 ace[] buffer.
1e95b 20 20 20 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 ** is allocat
1e95c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 ed. */. if
1e95d 28 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 ( pBt->btsFlags
1e95e 26 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c & BTS_SECURE_DEL
1e95f 45 54 45 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ETE ){. i
1e960 6e 74 20 69 4f 66 66 3b 0a 0a 20 20 20 20 20 20 nt iOff;..
1e961 20 20 69 4f 66 66 20 3d 20 53 51 4c 49 54 45 5f iOff = SQLITE_
1e962 50 54 52 5f 54 4f 5f 49 4e 54 28 61 70 44 69 76 PTR_TO_INT(apDiv
1e963 5b 69 5d 29 20 2d 20 53 51 4c 49 54 45 5f 50 54 [i]) - SQLITE_PT
1e964 52 5f 54 4f 5f 49 4e 54 28 70 50 61 72 65 6e 74 R_TO_INT(pParent
1e965 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20 ->aData);.
1e966 20 20 69 66 28 20 28 69 4f 66 66 2b 73 7a 4e 65 if( (iOff+szNe
1e967 77 5b 69 5d 29 3e 28 69 6e 74 29 70 42 74 2d 3e w[i])>(int)pBt->
1e968 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 usableSize ){.
1e969 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1e96a 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1e96b 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 ;. mems
1e96c 65 74 28 61 70 4f 6c 64 2c 20 30 2c 20 28 69 2b et(apOld, 0, (i+
1e96d 31 29 2a 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 1)*sizeof(MemPag
1e96e 65 2a 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 e*));.
1e96f 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
1e970 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d 65 anup;. }e
1e971 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6d lse{. m
1e972 65 6d 63 70 79 28 26 61 4f 76 66 6c 53 70 61 63 emcpy(&aOvflSpac
1e973 65 5b 69 4f 66 66 5d 2c 20 61 70 44 69 76 5b 69 e[iOff], apDiv[i
1e974 5d 2c 20 73 7a 4e 65 77 5b 69 5d 29 3b 0a 20 20 ], szNew[i]);.
1e975 20 20 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d apDiv[i]
1e976 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 61 = &aOvflSpace[a
1e977 70 44 69 76 5b 69 5d 2d 70 50 61 72 65 6e 74 2d pDiv[i]-pParent-
1e978 3e 61 44 61 74 61 5d 3b 0a 20 20 20 20 20 20 20 >aData];.
1e979 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
1e97a 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 72 65 6e dropCell(pParen
1e97b 74 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 t, i+nxDiv-pPare
1e97c 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2c 20 73 nt->nOverflow, s
1e97d 7a 4e 65 77 5b 69 5d 2c 20 26 72 63 29 3b 0a 20 zNew[i], &rc);.
1e97e 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d }. }.. /* M
1e97f 61 6b 65 20 6e 4d 61 78 43 65 6c 6c 73 20 61 20 ake nMaxCells a
1e980 6d 75 6c 74 69 70 6c 65 20 6f 66 20 34 20 69 6e multiple of 4 in
1e981 20 6f 72 64 65 72 20 74 6f 20 70 72 65 73 65 72 order to preser
1e982 76 65 20 38 2d 62 79 74 65 0a 20 20 2a 2a 20 61 ve 8-byte. ** a
1e983 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a 20 20 6e 4d lignment */. nM
1e984 61 78 43 65 6c 6c 73 20 3d 20 28 6e 4d 61 78 43 axCells = (nMaxC
1e985 65 6c 6c 73 20 2b 20 33 29 26 7e 33 3b 0a 0a 20 ells + 3)&~3;..
1e986 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 /*. ** Allocat
1e987 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 6d 6f e space for memo
1e988 72 79 20 73 74 72 75 63 74 75 72 65 73 0a 20 20 ry structures.
1e989 2a 2f 0a 20 20 6b 20 3d 20 70 42 74 2d 3e 70 61 */. k = pBt->pa
1e98a 67 65 53 69 7a 65 20 2b 20 52 4f 55 4e 44 38 28 geSize + ROUND8(
1e98b 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 sizeof(MemPage))
1e98c 3b 0a 20 20 73 7a 53 63 72 61 74 63 68 20 3d 0a ;. szScratch =.
1e98d 20 20 20 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 nMaxCells
1e98e 2a 73 69 7a 65 6f 66 28 75 38 2a 29 20 20 20 20 *sizeof(u8*)
1e98f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e990 20 20 20 2f 2a 20 61 70 43 65 6c 6c 20 2a 2f 0a /* apCell */.
1e991 20 20 20 20 20 2b 20 6e 4d 61 78 43 65 6c 6c 73 + nMaxCells
1e992 2a 73 69 7a 65 6f 66 28 75 31 36 29 20 20 20 20 *sizeof(u16)
1e993 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e994 20 20 20 2f 2a 20 73 7a 43 65 6c 6c 20 2a 2f 0a /* szCell */.
1e995 20 20 20 20 20 2b 20 70 42 74 2d 3e 70 61 67 65 + pBt->page
1e996 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 Size
1e997 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e998 20 20 20 2f 2a 20 61 53 70 61 63 65 31 20 2a 2f /* aSpace1 */
1e999 0a 20 20 20 20 20 2b 20 6b 2a 6e 4f 6c 64 3b 20 . + k*nOld;
1e99a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e99b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e99c 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 70 69 /* Page copi
1e99d 65 73 20 28 61 70 43 6f 70 79 29 20 2a 2f 0a 20 es (apCopy) */.
1e99e 20 61 70 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 apCell = sqlite
1e99f 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 20 3ScratchMalloc(
1e9a0 73 7a 53 63 72 61 74 63 68 20 29 3b 20 0a 20 20 szScratch ); .
1e9a1 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 29 7b if( apCell==0 ){
1e9a2 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1e9a3 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f _NOMEM;. goto
1e9a4 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 balance_cleanup
1e9a5 3b 0a 20 20 7d 0a 20 20 73 7a 43 65 6c 6c 20 3d ;. }. szCell =
1e9a6 20 28 75 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6e (u16*)&apCell[n
1e9a7 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 61 53 70 MaxCells];. aSp
1e9a8 61 63 65 31 20 3d 20 28 75 38 2a 29 26 73 7a 43 ace1 = (u8*)&szC
1e9a9 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a ell[nMaxCells];.
1e9aa 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f assert( EIGHT_
1e9ab 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 61 BYTE_ALIGNMENT(a
1e9ac 53 70 61 63 65 31 29 20 29 3b 0a 0a 20 20 2f 2a Space1) );.. /*
1e9ad 0a 20 20 2a 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 . ** Load point
1e9ae 65 72 73 20 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 ers to all cells
1e9af 20 6f 6e 20 73 69 62 6c 69 6e 67 20 70 61 67 65 on sibling page
1e9b0 73 20 61 6e 64 20 74 68 65 20 64 69 76 69 64 65 s and the divide
1e9b1 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 r cells. ** int
1e9b2 6f 20 74 68 65 20 6c 6f 63 61 6c 20 61 70 43 65 o the local apCe
1e9b3 6c 6c 5b 5d 20 61 72 72 61 79 2e 20 20 4d 61 6b ll[] array. Mak
1e9b4 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 e copies of the
1e9b5 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 divider cells.
1e9b6 2a 2a 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 ** into space ob
1e9b7 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 53 70 61 tained from aSpa
1e9b8 63 65 31 5b 5d 20 61 6e 64 20 72 65 6d 6f 76 65 ce1[] and remove
1e9b9 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c the divider cel
1e9ba 6c 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61 ls. ** from pPa
1e9bb 72 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 rent.. **. **
1e9bc 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 If the siblings
1e9bd 61 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 65 are on leaf page
1e9be 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c s, then the chil
1e9bf 64 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 68 d pointers of th
1e9c0 65 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20 63 e. ** divider c
1e9c1 65 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70 65 ells are strippe
1e9c2 64 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73 d from the cells
1e9c3 20 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 before they are
1e9c4 20 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74 copied. ** int
1e9c5 6f 20 61 53 70 61 63 65 31 5b 5d 2e 20 20 49 6e o aSpace1[]. In
1e9c6 20 74 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63 this way, all c
1e9c7 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d ells in apCell[]
1e9c8 20 61 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a are without. *
1e9c9 2a 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 * child pointers
1e9ca 2e 20 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61 . If siblings a
1e9cb 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 re not leaves, t
1e9cc 68 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a hen all cell in.
1e9cd 20 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e ** apCell[] in
1e9ce 63 6c 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e clude child poin
1e9cf 74 65 72 73 2e 20 20 45 69 74 68 65 72 20 77 61 ters. Either wa
1e9d0 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 y, all cells in
1e9d1 61 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 apCell[]. ** ar
1e9d2 65 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 e alike.. **.
1e9d3 2a 2a 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f ** leafCorrectio
1e9d4 6e 3a 20 20 34 20 69 66 20 70 50 61 67 65 20 69 n: 4 if pPage i
1e9d5 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 s a leaf. 0 if
1e9d6 70 50 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c pPage is not a l
1e9d7 65 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20 eaf.. **
1e9d8 6c 65 61 66 44 61 74 61 3a 20 20 31 20 69 66 20 leafData: 1 if
1e9d9 70 50 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b pPage holds key+
1e9da 64 61 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74 data and pParent
1e9db 20 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73 holds only keys
1e9dc 2e 0a 20 20 2a 2f 0a 20 20 6c 65 61 66 43 6f 72 .. */. leafCor
1e9dd 72 65 63 74 69 6f 6e 20 3d 20 61 70 4f 6c 64 5b rection = apOld[
1e9de 30 5d 2d 3e 6c 65 61 66 2a 34 3b 0a 20 20 6c 65 0]->leaf*4;. le
1e9df 61 66 44 61 74 61 20 3d 20 61 70 4f 6c 64 5b 30 afData = apOld[0
1e9e0 5d 2d 3e 68 61 73 44 61 74 61 3b 0a 20 20 66 6f ]->hasData;. fo
1e9e1 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 r(i=0; i<nOld; i
1e9e2 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 69 6d ++){. int lim
1e9e3 69 74 3b 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 it;. . /*
1e9e4 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 79 Before doing any
1e9e5 74 68 69 6e 67 20 65 6c 73 65 2c 20 74 61 6b 65 thing else, take
1e9e6 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 a copy of the i
1e9e7 27 74 68 20 6f 72 69 67 69 6e 61 6c 20 73 69 62 'th original sib
1e9e8 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 54 68 65 20 ling. ** The
1e9e9 72 65 73 74 20 6f 66 20 74 68 69 73 20 66 75 6e rest of this fun
1e9ea 63 74 69 6f 6e 20 77 69 6c 6c 20 75 73 65 20 64 ction will use d
1e9eb 61 74 61 20 66 72 6f 6d 20 74 68 65 20 63 6f 70 ata from the cop
1e9ec 69 65 73 20 72 61 74 68 65 72 0a 20 20 20 20 2a ies rather. *
1e9ed 2a 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 * that the origi
1e9ee 6e 61 6c 20 70 61 67 65 73 20 73 69 6e 63 65 20 nal pages since
1e9ef 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 the original pag
1e9f0 65 73 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 es will be in th
1e9f1 65 0a 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 e. ** process
1e9f2 20 6f 66 20 62 65 69 6e 67 20 6f 76 65 72 77 72 of being overwr
1e9f3 69 74 74 65 6e 2e 20 20 2a 2f 0a 20 20 20 20 4d itten. */. M
1e9f4 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61 emPage *pOld = a
1e9f5 70 43 6f 70 79 5b 69 5d 20 3d 20 28 4d 65 6d 50 pCopy[i] = (MemP
1e9f6 61 67 65 2a 29 26 61 53 70 61 63 65 31 5b 70 42 age*)&aSpace1[pB
1e9f7 74 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 6b 2a t->pageSize + k*
1e9f8 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 i];. memcpy(p
1e9f9 4f 6c 64 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 Old, apOld[i], s
1e9fa 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b izeof(MemPage));
1e9fb 0a 20 20 20 20 70 4f 6c 64 2d 3e 61 44 61 74 61 . pOld->aData
1e9fc 20 3d 20 28 76 6f 69 64 2a 29 26 70 4f 6c 64 5b = (void*)&pOld[
1e9fd 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 1];. memcpy(p
1e9fe 4f 6c 64 2d 3e 61 44 61 74 61 2c 20 61 70 4f 6c Old->aData, apOl
1e9ff 64 5b 69 5d 2d 3e 61 44 61 74 61 2c 20 70 42 74 d[i]->aData, pBt
1ea00 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 ->pageSize);..
1ea01 20 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e limit = pOld->
1ea02 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 nCell+pOld->nOve
1ea03 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20 70 rflow;. if( p
1ea04 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 Old->nOverflow>0
1ea05 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d ){. for(j=
1ea06 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 0; j<limit; j++)
1ea07 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
1ea08 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c ( nCell<nMaxCell
1ea09 73 20 29 3b 0a 20 20 20 20 20 20 20 20 61 70 43 s );. apC
1ea0a 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e ell[nCell] = fin
1ea0b 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f dOverflowCell(pO
1ea0c 6c 64 2c 20 6a 29 3b 0a 20 20 20 20 20 20 20 20 ld, j);.
1ea0d 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 szCell[nCell] =
1ea0e 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c 64 cellSizePtr(pOld
1ea0f 2c 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 , apCell[nCell])
1ea10 3b 0a 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 2b ;. nCell+
1ea11 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d +;. }. }
1ea12 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 38 20 2a else{. u8 *
1ea13 61 44 61 74 61 20 3d 20 70 4f 6c 64 2d 3e 61 44 aData = pOld->aD
1ea14 61 74 61 3b 0a 20 20 20 20 20 20 75 31 36 20 6d ata;. u16 m
1ea15 61 73 6b 50 61 67 65 20 3d 20 70 4f 6c 64 2d 3e askPage = pOld->
1ea16 6d 61 73 6b 50 61 67 65 3b 0a 20 20 20 20 20 20 maskPage;.
1ea17 75 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d u16 cellOffset =
1ea18 20 70 4f 6c 64 2d 3e 63 65 6c 6c 4f 66 66 73 65 pOld->cellOffse
1ea19 74 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 t;. for(j=0
1ea1a 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 7b ; j<limit; j++){
1ea1b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1ea1c 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 nCell<nMaxCells
1ea1d 20 29 3b 0a 20 20 20 20 20 20 20 20 61 70 43 65 );. apCe
1ea1e 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 ll[nCell] = find
1ea1f 43 65 6c 6c 76 32 28 61 44 61 74 61 2c 20 6d 61 Cellv2(aData, ma
1ea20 73 6b 50 61 67 65 2c 20 63 65 6c 6c 4f 66 66 73 skPage, cellOffs
1ea21 65 74 2c 20 6a 29 3b 0a 20 20 20 20 20 20 20 20 et, j);.
1ea22 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 szCell[nCell] =
1ea23 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c 64 cellSizePtr(pOld
1ea24 2c 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 , apCell[nCell])
1ea25 3b 0a 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 2b ;. nCell+
1ea26 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d +;. }. }
1ea27 20 20 20 20 20 20 20 0a 20 20 20 20 69 66 28 20 . if(
1ea28 69 3c 6e 4f 6c 64 2d 31 20 26 26 20 21 6c 65 61 i<nOld-1 && !lea
1ea29 66 44 61 74 61 29 7b 0a 20 20 20 20 20 20 75 31 fData){. u1
1ea2a 36 20 73 7a 20 3d 20 28 75 31 36 29 73 7a 4e 65 6 sz = (u16)szNe
1ea2b 77 5b 69 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a w[i];. u8 *
1ea2c 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 61 73 73 pTemp;. ass
1ea2d 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 ert( nCell<nMaxC
1ea2e 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 73 7a ells );. sz
1ea2f 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a Cell[nCell] = sz
1ea30 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 ;. pTemp =
1ea31 26 61 53 70 61 63 65 31 5b 69 53 70 61 63 65 31 &aSpace1[iSpace1
1ea32 5d 3b 0a 20 20 20 20 20 20 69 53 70 61 63 65 31 ];. iSpace1
1ea33 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 += sz;. as
1ea34 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 6d sert( sz<=pBt->m
1ea35 61 78 4c 6f 63 61 6c 2b 32 33 20 29 3b 0a 20 20 axLocal+23 );.
1ea36 20 20 20 20 61 73 73 65 72 74 28 20 69 53 70 61 assert( iSpa
1ea37 63 65 31 20 3c 3d 20 28 69 6e 74 29 70 42 74 2d ce1 <= (int)pBt-
1ea38 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 >pageSize );.
1ea39 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 2c memcpy(pTemp,
1ea3a 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29 3b 0a apDiv[i], sz);.
1ea3b 20 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 apCell[nCe
1ea3c 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b 6c 65 61 66 ll] = pTemp+leaf
1ea3d 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 Correction;.
1ea3e 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f assert( leafCo
1ea3f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c rrection==0 || l
1ea40 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 eafCorrection==4
1ea41 20 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c );. szCell
1ea42 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a 43 65 6c 6c [nCell] = szCell
1ea43 5b 6e 43 65 6c 6c 5d 20 2d 20 6c 65 61 66 43 6f [nCell] - leafCo
1ea44 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 rrection;.
1ea45 69 66 28 20 21 70 4f 6c 64 2d 3e 6c 65 61 66 20 if( !pOld->leaf
1ea46 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
1ea47 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f t( leafCorrectio
1ea48 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 n==0 );.
1ea49 61 73 73 65 72 74 28 20 70 4f 6c 64 2d 3e 68 64 assert( pOld->hd
1ea4a 72 4f 66 66 73 65 74 3d 3d 30 20 29 3b 0a 20 20 rOffset==0 );.
1ea4b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 /* The rig
1ea4c 68 74 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 ht pointer of th
1ea4d 65 20 63 68 69 6c 64 20 70 61 67 65 20 70 4f 6c e child page pOl
1ea4e 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 6c 65 d becomes the le
1ea4f 66 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f ft. ** po
1ea50 69 6e 74 65 72 20 6f 66 20 74 68 65 20 64 69 76 inter of the div
1ea51 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 ider cell */.
1ea52 20 20 20 20 20 6d 65 6d 63 70 79 28 61 70 43 65 memcpy(apCe
1ea53 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 ll[nCell], &pOld
1ea54 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 34 29 3b 0a ->aData[8], 4);.
1ea55 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1ea56 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 assert( lea
1ea57 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 fCorrection==4 )
1ea58 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 7a ;. if( sz
1ea59 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b Cell[nCell]<4 ){
1ea5a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f . /* Do
1ea5b 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 6e 79 20 63 not allow any c
1ea5c 65 6c 6c 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 ells smaller tha
1ea5d 6e 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 n 4 bytes. */.
1ea5e 20 20 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e szCell[n
1ea5f 43 65 6c 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20 Cell] = 4;.
1ea60 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1ea61 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 nCell++;.
1ea62 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a }. }.. /*. **
1ea63 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 Figure out the
1ea64 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
1ea65 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 61 needed to hold a
1ea66 6c 6c 20 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a ll nCell cells..
1ea67 20 20 2a 2a 20 53 74 6f 72 65 20 74 68 69 73 20 ** Store this
1ea68 6e 75 6d 62 65 72 20 69 6e 20 22 6b 22 2e 20 20 number in "k".
1ea69 41 6c 73 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e Also compute szN
1ea6a 65 77 5b 5d 20 77 68 69 63 68 20 69 73 20 74 68 ew[] which is th
1ea6b 65 20 74 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 7a e total. ** siz
1ea6c 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f e of all cells o
1ea6d 6e 20 74 68 65 20 69 2d 74 68 20 70 61 67 65 20 n the i-th page
1ea6e 61 6e 64 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69 and cntNew[] whi
1ea6f 63 68 20 69 73 20 74 68 65 20 69 6e 64 65 78 0a ch is the index.
1ea70 20 20 2a 2a 20 69 6e 20 61 70 43 65 6c 6c 5b 5d ** in apCell[]
1ea71 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 of the cell tha
1ea72 74 20 64 69 76 69 64 65 73 20 70 61 67 65 20 69 t divides page i
1ea73 20 66 72 6f 6d 20 70 61 67 65 20 69 2b 31 2e 20 from page i+1.
1ea74 20 0a 20 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d . ** cntNew[k]
1ea75 20 73 68 6f 75 6c 64 20 65 71 75 61 6c 20 6e 43 should equal nC
1ea76 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 ell.. **. ** V
1ea77 61 6c 75 65 73 20 63 6f 6d 70 75 74 65 64 20 62 alues computed b
1ea78 79 20 74 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 20 y this block:.
1ea79 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 **. **
1ea7a 20 20 6b 3a 20 54 68 65 20 74 6f 74 61 6c 20 6e k: The total n
1ea7b 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 umber of sibling
1ea7c 20 70 61 67 65 73 0a 20 20 2a 2a 20 20 20 20 73 pages. ** s
1ea7d 7a 4e 65 77 5b 69 5d 3a 20 53 70 61 63 65 64 20 zNew[i]: Spaced
1ea7e 75 73 65 64 20 6f 6e 20 74 68 65 20 69 2d 74 68 used on the i-th
1ea7f 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 sibling page..
1ea80 20 2a 2a 20 20 20 63 6e 74 4e 65 77 5b 69 5d 3a ** cntNew[i]:
1ea81 20 49 6e 64 65 78 20 69 6e 20 61 70 43 65 6c 6c Index in apCell
1ea82 5b 5d 20 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 [] and szCell[]
1ea83 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 63 65 for the first ce
1ea84 6c 6c 20 74 6f 0a 20 20 2a 2a 20 20 20 20 20 20 ll to. **
1ea85 20 20 20 20 20 20 20 20 74 68 65 20 72 69 67 68 the righ
1ea86 74 20 6f 66 20 74 68 65 20 69 2d 74 68 20 73 69 t of the i-th si
1ea87 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a bling page.. **
1ea88 20 75 73 61 62 6c 65 53 70 61 63 65 3a 20 4e 75 usableSpace: Nu
1ea89 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
1ea8a 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65 space available
1ea8b 20 6f 6e 20 65 61 63 68 20 73 69 62 6c 69 6e 67 on each sibling
1ea8c 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75 .. ** . */. u
1ea8d 73 61 62 6c 65 53 70 61 63 65 20 3d 20 70 42 74 sableSpace = pBt
1ea8e 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 31 ->usableSize - 1
1ea8f 32 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 2 + leafCorrecti
1ea90 6f 6e 3b 0a 20 20 66 6f 72 28 73 75 62 74 6f 74 on;. for(subtot
1ea91 61 6c 3d 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c al=k=i=0; i<nCel
1ea92 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 l; i++){. ass
1ea93 65 72 74 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 ert( i<nMaxCells
1ea94 20 29 3b 0a 20 20 20 20 73 75 62 74 6f 74 61 6c );. subtotal
1ea95 20 2b 3d 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20 += szCell[i] +
1ea96 32 3b 0a 20 20 20 20 69 66 28 20 73 75 62 74 6f 2;. if( subto
1ea97 74 61 6c 20 3e 20 75 73 61 62 6c 65 53 70 61 63 tal > usableSpac
1ea98 65 20 29 7b 0a 20 20 20 20 20 20 73 7a 4e 65 77 e ){. szNew
1ea99 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c 20 2d [k] = subtotal -
1ea9a 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 szCell[i];.
1ea9b 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b cntNew[k] = i;
1ea9c 0a 20 20 20 20 20 20 69 66 28 20 6c 65 61 66 44 . if( leafD
1ea9d 61 74 61 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20 ata ){ i--; }.
1ea9e 20 20 20 20 73 75 62 74 6f 74 61 6c 20 3d 20 30 subtotal = 0
1ea9f 3b 0a 20 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 20 ;. k++;.
1eaa0 20 20 20 69 66 28 20 6b 3e 4e 42 2b 31 20 29 7b if( k>NB+1 ){
1eaa1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
1eaa2 52 55 50 54 5f 42 4b 50 54 3b 20 67 6f 74 6f 20 RUPT_BKPT; goto
1eaa3 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b balance_cleanup;
1eaa4 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 }. }. }. s
1eaa5 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 zNew[k] = subtot
1eaa6 61 6c 3b 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 al;. cntNew[k]
1eaa7 3d 20 6e 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a = nCell;. k++;.
1eaa8 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70 . /*. ** The p
1eaa9 61 63 6b 69 6e 67 20 63 6f 6d 70 75 74 65 64 20 acking computed
1eaaa 62 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 by the previous
1eaab 62 6c 6f 63 6b 20 69 73 20 62 69 61 73 65 64 20 block is biased
1eaac 74 6f 77 61 72 64 20 74 68 65 20 73 69 62 6c 69 toward the sibli
1eaad 6e 67 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 ngs. ** on the
1eaae 6c 65 66 74 20 73 69 64 65 2e 20 20 54 68 65 20 left side. The
1eaaf 6c 65 66 74 20 73 69 62 6c 69 6e 67 73 20 61 72 left siblings ar
1eab0 65 20 61 6c 77 61 79 73 20 6e 65 61 72 6c 79 20 e always nearly
1eab1 66 75 6c 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a full, while the.
1eab2 20 20 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 ** right-most
1eab3 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 sibling might be
1eab4 20 6e 65 61 72 6c 79 20 65 6d 70 74 79 2e 20 20 nearly empty.
1eab5 54 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f This block of co
1eab6 64 65 20 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a de attempts. **
1eab7 20 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 70 to adjust the p
1eab8 61 63 6b 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e acking of siblin
1eab9 67 73 20 74 6f 20 67 65 74 20 61 20 62 65 74 74 gs to get a bett
1eaba 65 72 20 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a er balance.. **
1eabb 0a 20 20 2a 2a 20 54 68 69 73 20 61 64 6a 75 73 . ** This adjus
1eabc 74 6d 65 6e 74 20 69 73 20 6d 6f 72 65 20 74 68 tment is more th
1eabd 61 6e 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 an an optimizati
1eabe 6f 6e 2e 20 20 54 68 65 20 70 61 63 6b 69 6e 67 on. The packing
1eabf 20 61 62 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a above might. *
1eac0 2a 20 62 65 20 73 6f 20 6f 75 74 20 6f 66 20 62 * be so out of b
1eac1 61 6c 61 6e 63 65 20 61 73 20 74 6f 20 62 65 20 alance as to be
1eac2 69 6c 6c 65 67 61 6c 2e 20 20 46 6f 72 20 65 78 illegal. For ex
1eac3 61 6d 70 6c 65 2c 20 74 68 65 20 72 69 67 68 74 ample, the right
1eac4 2d 6d 6f 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69 -most. ** sibli
1eac5 6e 67 20 6d 69 67 68 74 20 62 65 20 63 6f 6d 70 ng might be comp
1eac6 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e 20 20 54 letely empty. T
1eac7 68 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 his adjustment i
1eac8 73 20 6e 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a s not optional..
1eac9 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 */. for(i=k-1
1eaca 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 ; i>0; i--){.
1eacb 20 69 6e 74 20 73 7a 52 69 67 68 74 20 3d 20 73 int szRight = s
1eacc 7a 4e 65 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a zNew[i]; /* Siz
1eacd 65 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 e of sibling on
1eace 74 68 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20 the right */.
1eacf 20 69 6e 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a int szLeft = sz
1ead0 4e 65 77 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a New[i-1]; /* Siz
1ead1 65 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 e of sibling on
1ead2 74 68 65 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20 the left */.
1ead3 69 6e 74 20 72 3b 20 20 20 20 20 20 20 20 20 20 int r;
1ead4 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
1ead5 72 69 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 right-most cell
1ead6 69 6e 20 6c 65 66 74 20 73 69 62 6c 69 6e 67 20 in left sibling
1ead7 2a 2f 0a 20 20 20 20 69 6e 74 20 64 3b 20 20 20 */. int d;
1ead8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
1ead9 64 65 78 20 6f 66 20 66 69 72 73 74 20 63 65 6c dex of first cel
1eada 6c 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 l to the left of
1eadb 20 72 69 67 68 74 20 73 69 62 6c 69 6e 67 20 2a right sibling *
1eadc 2f 0a 0a 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 /.. r = cntNe
1eadd 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 w[i-1] - 1;.
1eade 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 d = r + 1 - leaf
1eadf 44 61 74 61 3b 0a 20 20 20 20 61 73 73 65 72 74 Data;. assert
1eae0 28 20 64 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b ( d<nMaxCells );
1eae1 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 3c 6e . assert( r<n
1eae2 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 MaxCells );.
1eae3 77 68 69 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d while( szRight==
1eae4 30 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 21 62 0 . || (!b
1eae5 42 75 6c 6b 20 26 26 20 73 7a 52 69 67 68 74 2b Bulk && szRight+
1eae6 73 7a 43 65 6c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c szCell[d]+2<=szL
1eae7 65 66 74 2d 28 73 7a 43 65 6c 6c 5b 72 5d 2b 32 eft-(szCell[r]+2
1eae8 29 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 )) . ){.
1eae9 20 73 7a 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 szRight += szCe
1eaea 6c 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 ll[d] + 2;.
1eaeb 20 73 7a 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c szLeft -= szCel
1eaec 6c 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 l[r] + 2;.
1eaed 63 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 cntNew[i-1]--;.
1eaee 20 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b r = cntNew[
1eaef 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 i-1] - 1;.
1eaf0 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 d = r + 1 - leaf
1eaf1 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Data;. }.
1eaf2 73 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 szNew[i] = szRig
1eaf3 68 74 3b 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d ht;. szNew[i-
1eaf4 31 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 1] = szLeft;. }
1eaf5 0a 0a 20 20 2f 2a 20 45 69 74 68 65 72 20 77 65 .. /* Either we
1eaf6 20 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f found one or mo
1eaf7 72 65 20 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 re cells (cntnew
1eaf8 5b 30 5d 29 3e 30 29 20 6f 72 20 70 50 61 67 65 [0])>0) or pPage
1eaf9 20 69 73 0a 20 20 2a 2a 20 61 20 76 69 72 74 75 is. ** a virtu
1eafa 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 41 al root page. A
1eafb 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 virtual root pa
1eafc 67 65 20 69 73 20 77 68 65 6e 20 74 68 65 20 72 ge is when the r
1eafd 65 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 eal root. ** pa
1eafe 67 65 20 69 73 20 70 61 67 65 20 31 20 61 6e 64 ge is page 1 and
1eaff 20 77 65 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 we are the only
1eb00 20 63 68 69 6c 64 20 6f 66 20 74 68 61 74 20 70 child of that p
1eb01 61 67 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 55 age.. **. ** U
1eb02 50 44 41 54 45 3a 20 20 54 68 65 20 61 73 73 65 PDATE: The asse
1eb03 72 74 28 29 20 62 65 6c 6f 77 20 69 73 20 6e 6f rt() below is no
1eb04 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 74 72 t necessarily tr
1eb05 75 65 20 69 66 20 74 68 65 20 64 61 74 61 62 61 ue if the databa
1eb06 73 65 0a 20 20 2a 2a 20 66 69 6c 65 20 69 73 20 se. ** file is
1eb07 63 6f 72 72 75 70 74 2e 20 20 54 68 65 20 63 6f corrupt. The co
1eb08 72 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 62 65 rruption will be
1eb09 20 64 65 74 65 63 74 65 64 20 61 6e 64 20 72 65 detected and re
1eb0a 70 6f 72 74 65 64 20 6c 61 74 65 72 0a 20 20 2a ported later. *
1eb0b 2a 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 64 * in this proced
1eb0c 75 72 65 20 73 6f 20 74 68 65 72 65 20 69 73 20 ure so there is
1eb0d 6e 6f 20 6e 65 65 64 20 74 6f 20 61 63 74 20 75 no need to act u
1eb0e 70 6f 6e 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f pon it now.. */
1eb0f 0a 23 69 66 20 30 0a 20 20 61 73 73 65 72 74 28 .#if 0. assert(
1eb10 20 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 7c 7c 20 cntNew[0]>0 ||
1eb11 28 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 3d 3d (pParent->pgno==
1eb12 31 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 1 && pParent->nC
1eb13 65 6c 6c 3d 3d 30 29 20 29 3b 0a 23 65 6e 64 69 ell==0) );.#endi
1eb14 66 0a 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c f.. TRACE(("BAL
1eb15 41 4e 43 45 3a 20 6f 6c 64 3a 20 25 64 20 25 64 ANCE: old: %d %d
1eb16 20 25 64 20 20 22 2c 0a 20 20 20 20 61 70 4f 6c %d ",. apOl
1eb17 64 5b 30 5d 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 d[0]->pgno, .
1eb18 20 6e 4f 6c 64 3e 3d 32 20 3f 20 61 70 4f 6c 64 nOld>=2 ? apOld
1eb19 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 0a 20 [1]->pgno : 0,.
1eb1a 20 20 20 6e 4f 6c 64 3e 3d 33 20 3f 20 61 70 4f nOld>=3 ? apO
1eb1b 6c 64 5b 32 5d 2d 3e 70 67 6e 6f 20 3a 20 30 0a ld[2]->pgno : 0.
1eb1c 20 20 29 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a ));.. /*. **
1eb1d 20 41 6c 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20 Allocate k new
1eb1e 70 61 67 65 73 2e 20 20 52 65 75 73 65 20 6f 6c pages. Reuse ol
1eb1f 64 20 70 61 67 65 73 20 77 68 65 72 65 20 70 6f d pages where po
1eb20 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 ssible.. */. i
1eb21 66 28 20 61 70 4f 6c 64 5b 30 5d 2d 3e 70 67 6e f( apOld[0]->pgn
1eb22 6f 3c 3d 31 20 29 7b 0a 20 20 20 20 72 63 20 3d o<=1 ){. rc =
1eb23 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1eb24 42 4b 50 54 3b 0a 20 20 20 20 67 6f 74 6f 20 62 BKPT;. goto b
1eb25 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a alance_cleanup;.
1eb26 20 20 7d 0a 20 20 70 61 67 65 46 6c 61 67 73 20 }. pageFlags
1eb27 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 61 44 61 74 = apOld[0]->aDat
1eb28 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b a[0];. for(i=0;
1eb29 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 i<k; i++){.
1eb2a 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 MemPage *pNew;.
1eb2b 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 7b if( i<nOld ){
1eb2c 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70 . pNew = ap
1eb2d 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b 69 New[i] = apOld[i
1eb2e 5d 3b 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b 69 ];. apOld[i
1eb2f 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 ] = 0;. rc
1eb30 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1eb31 69 74 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 ite(pNew->pDbPag
1eb32 65 29 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b e);. nNew++
1eb33 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
1eb34 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
1eb35 65 61 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65 eanup;. }else
1eb36 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1eb37 69 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 i>0 );. rc
1eb38 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 = allocateBtreeP
1eb39 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 age(pBt, &pNew,
1eb3a 26 70 67 6e 6f 2c 20 28 62 42 75 6c 6b 20 3f 20 &pgno, (bBulk ?
1eb3b 31 20 3a 20 70 67 6e 6f 29 2c 20 30 29 3b 0a 20 1 : pgno), 0);.
1eb3c 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f if( rc ) go
1eb3d 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
1eb3e 75 70 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b up;. apNew[
1eb3f 69 5d 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 i] = pNew;.
1eb40 20 6e 4e 65 77 2b 2b 3b 0a 0a 20 20 20 20 20 20 nNew++;..
1eb41 2f 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74 /* Set the point
1eb42 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 er-map entry for
1eb43 20 74 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 the new sibling
1eb44 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 page. */.
1eb45 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d if( ISAUTOVACUUM
1eb46 20 29 7b 0a 20 20 20 20 20 20 20 20 70 74 72 6d ){. ptrm
1eb47 61 70 50 75 74 28 70 42 74 2c 20 70 4e 65 77 2d apPut(pBt, pNew-
1eb48 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 >pgno, PTRMAP_BT
1eb49 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 REE, pParent->pg
1eb4a 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 no, &rc);.
1eb4b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1eb4c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
1eb4d 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
1eb4e 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d eanup;. }
1eb4f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1eb50 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e }.. /* Free an
1eb51 79 20 6f 6c 64 20 70 61 67 65 73 20 74 68 61 74 y old pages that
1eb52 20 77 65 72 65 20 6e 6f 74 20 72 65 75 73 65 64 were not reused
1eb53 20 61 73 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 as new pages..
1eb54 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e */. while( i<n
1eb55 4f 6c 64 20 29 7b 0a 20 20 20 20 66 72 65 65 50 Old ){. freeP
1eb56 61 67 65 28 61 70 4f 6c 64 5b 69 5d 2c 20 26 72 age(apOld[i], &r
1eb57 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 c);. if( rc )
1eb58 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
1eb59 65 61 6e 75 70 3b 0a 20 20 20 20 72 65 6c 65 61 eanup;. relea
1eb5a 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 sePage(apOld[i])
1eb5b 3b 0a 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d ;. apOld[i] =
1eb5c 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0;. i++;. }
1eb5d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 .. /*. ** Put
1eb5e 74 68 65 20 6e 65 77 20 70 61 67 65 73 20 69 6e the new pages in
1eb5f 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 accending order
1eb60 2e 20 20 54 68 69 73 20 68 65 6c 70 73 20 74 6f . This helps to
1eb61 0a 20 20 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69 . ** keep entri
1eb62 65 73 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66 es in the disk f
1eb63 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 73 6f 20 ile in order so
1eb64 74 68 61 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a that a scan. **
1eb65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 of the table is
1eb66 20 61 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 a linear scan t
1eb67 68 72 6f 75 67 68 20 74 68 65 20 66 69 6c 65 2e hrough the file.
1eb68 20 20 54 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74 That. ** in t
1eb69 75 72 6e 20 68 65 6c 70 73 20 74 68 65 20 6f 70 urn helps the op
1eb6a 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 74 erating system t
1eb6b 6f 20 64 65 6c 69 76 65 72 20 70 61 67 65 73 0a o deliver pages.
1eb6c 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 69 ** from the di
1eb6d 73 6b 20 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e sk more rapidly.
1eb6e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 . **. ** An O(
1eb6f 6e 5e 32 29 20 69 6e 73 65 72 74 69 6f 6e 20 73 n^2) insertion s
1eb70 6f 72 74 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 ort algorithm is
1eb71 20 75 73 65 64 2c 20 62 75 74 20 73 69 6e 63 65 used, but since
1eb72 0a 20 20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72 . ** n is never
1eb73 20 6d 6f 72 65 20 74 68 61 6e 20 4e 42 20 28 61 more than NB (a
1eb74 20 73 6d 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 small constant)
1eb75 2c 20 74 68 61 74 20 73 68 6f 75 6c 64 0a 20 20 , that should.
1eb76 2a 2a 20 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 ** not be a prob
1eb77 6c 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 lem.. **. ** W
1eb78 68 65 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20 hen NB==3, this
1eb79 6f 6e 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e one optimization
1eb7a 20 6d 61 6b 65 73 20 74 68 65 20 64 61 74 61 62 makes the datab
1eb7b 61 73 65 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32 ase. ** about 2
1eb7c 35 25 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61 5% faster for la
1eb7d 72 67 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61 rge insertions a
1eb7e 6e 64 20 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 nd deletions..
1eb7f 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c */. for(i=0; i<
1eb80 6b 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 k-1; i++){. i
1eb81 6e 74 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b nt minV = apNew[
1eb82 69 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 69 6e i]->pgno;. in
1eb83 74 20 6d 69 6e 49 20 3d 20 69 3b 0a 20 20 20 20 t minI = i;.
1eb84 66 6f 72 28 6a 3d 69 2b 31 3b 20 6a 3c 6b 3b 20 for(j=i+1; j<k;
1eb85 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 j++){. if(
1eb86 61 70 4e 65 77 5b 6a 5d 2d 3e 70 67 6e 6f 3c 28 apNew[j]->pgno<(
1eb87 75 6e 73 69 67 6e 65 64 29 6d 69 6e 56 20 29 7b unsigned)minV ){
1eb88 0a 20 20 20 20 20 20 20 20 6d 69 6e 49 20 3d 20 . minI =
1eb89 6a 3b 0a 20 20 20 20 20 20 20 20 6d 69 6e 56 20 j;. minV
1eb8a 3d 20 61 70 4e 65 77 5b 6a 5d 2d 3e 70 67 6e 6f = apNew[j]->pgno
1eb8b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1eb8c 20 20 20 20 69 66 28 20 6d 69 6e 49 3e 69 20 29 if( minI>i )
1eb8d 7b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 {. MemPage
1eb8e 2a 70 54 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 *pT;. pT =
1eb8f 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 apNew[i];.
1eb90 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77 apNew[i] = apNew
1eb91 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 61 70 [minI];. ap
1eb92 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a New[minI] = pT;.
1eb93 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43 }. }. TRAC
1eb94 45 28 28 22 6e 65 77 3a 20 25 64 28 25 64 29 20 E(("new: %d(%d)
1eb95 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64 %d(%d) %d(%d) %d
1eb96 28 25 64 29 20 25 64 28 25 64 29 5c 6e 22 2c 0a (%d) %d(%d)\n",.
1eb97 20 20 20 20 61 70 4e 65 77 5b 30 5d 2d 3e 70 67 apNew[0]->pg
1eb98 6e 6f 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 no, szNew[0],.
1eb99 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 61 70 4e 65 nNew>=2 ? apNe
1eb9a 77 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 w[1]->pgno : 0,
1eb9b 6e 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b nNew>=2 ? szNew[
1eb9c 31 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 1] : 0,. nNew
1eb9d 3e 3d 33 20 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e >=3 ? apNew[2]->
1eb9e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d pgno : 0, nNew>=
1eb9f 33 20 3f 20 73 7a 4e 65 77 5b 32 5d 20 3a 20 30 3 ? szNew[2] : 0
1eba0 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 34 20 3f 20 ,. nNew>=4 ?
1eba1 61 70 4e 65 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a apNew[3]->pgno :
1eba2 20 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 0, nNew>=4 ? sz
1eba3 4e 65 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 New[3] : 0,.
1eba4 6e 4e 65 77 3e 3d 35 20 3f 20 61 70 4e 65 77 5b nNew>=5 ? apNew[
1eba5 34 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 4]->pgno : 0, nN
1eba6 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d ew>=5 ? szNew[4]
1eba7 20 3a 20 30 29 29 3b 0a 0a 20 20 61 73 73 65 72 : 0));.. asser
1eba8 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
1eba9 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 swriteable(pPare
1ebaa 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a nt->pDbPage) );.
1ebab 20 20 70 75 74 34 62 79 74 65 28 70 52 69 67 68 put4byte(pRigh
1ebac 74 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d t, apNew[nNew-1]
1ebad 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 ->pgno);.. /*.
1ebae 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 72 ** Evenly distr
1ebaf 69 62 75 74 65 20 74 68 65 20 64 61 74 61 20 69 ibute the data i
1ebb0 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 n apCell[] acros
1ebb1 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 2e s the new pages.
1ebb2 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 . ** Insert div
1ebb3 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 ider cells into
1ebb4 70 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 73 pParent as neces
1ebb5 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d sary.. */. j =
1ebb6 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 0;. for(i=0; i
1ebb7 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nNew; i++){.
1ebb8 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 65 /* Assemble the
1ebb9 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67 new sibling pag
1ebba 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 e. */. MemPag
1ebbb 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b e *pNew = apNew[
1ebbc 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 i];. assert(
1ebbd 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 j<nMaxCells );.
1ebbe 20 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 zeroPage(pNew
1ebbf 2c 20 70 61 67 65 46 6c 61 67 73 29 3b 0a 20 20 , pageFlags);.
1ebc0 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 assemblePage(p
1ebc1 4e 65 77 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a New, cntNew[i]-j
1ebc2 2c 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 , &apCell[j], &s
1ebc3 7a 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 zCell[j]);. a
1ebc4 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65 ssert( pNew->nCe
1ebc5 6c 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 ll>0 || (nNew==1
1ebc6 20 26 26 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 && cntNew[0]==0
1ebc7 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ) );. assert(
1ebc8 20 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pNew->nOverflow
1ebc9 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 6a 20 3d 20 ==0 );.. j =
1ebca 63 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 cntNew[i];..
1ebcb 2f 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e /* If the siblin
1ebcc 67 20 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 g page assembled
1ebcd 20 61 62 6f 76 65 20 77 61 73 20 6e 6f 74 20 74 above was not t
1ebce 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 he right-most si
1ebcf 62 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a 20 69 6e bling,. ** in
1ebd0 73 65 72 74 20 61 20 64 69 76 69 64 65 72 20 63 sert a divider c
1ebd1 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20 70 61 72 ell into the par
1ebd2 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f ent page.. */
1ebd3 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e . assert( i<n
1ebd4 4e 65 77 2d 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c New-1 || j==nCel
1ebd5 6c 20 29 3b 0a 20 20 20 20 69 66 28 20 6a 3c 6e l );. if( j<n
1ebd6 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 Cell ){. u8
1ebd7 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 *pCell;. u
1ebd8 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 8 *pTemp;.
1ebd9 69 6e 74 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 int sz;.. a
1ebda 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c ssert( j<nMaxCel
1ebdb 6c 73 20 29 3b 0a 20 20 20 20 20 20 70 43 65 6c ls );. pCel
1ebdc 6c 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 l = apCell[j];.
1ebdd 20 20 20 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c sz = szCell
1ebde 5b 6a 5d 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 [j] + leafCorrec
1ebdf 74 69 6f 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d tion;. pTem
1ebe0 70 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b p = &aOvflSpace[
1ebe1 69 4f 76 66 6c 53 70 61 63 65 5d 3b 0a 20 20 20 iOvflSpace];.
1ebe2 20 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65 if( !pNew->le
1ebe3 61 66 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 af ){. me
1ebe4 6d 63 70 79 28 26 70 4e 65 77 2d 3e 61 44 61 74 mcpy(&pNew->aDat
1ebe5 61 5b 38 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b a[8], pCell, 4);
1ebe6 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
1ebe7 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 leafData ){.
1ebe8 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 /* If the t
1ebe9 72 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61 ree is a leaf-da
1ebea 74 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65 ta tree, and the
1ebeb 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 siblings are le
1ebec 61 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a aves, . *
1ebed 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 * then there is
1ebee 6e 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 no divider cell
1ebef 69 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 in apCell[]. Ins
1ebf0 74 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65 tead, the divide
1ebf1 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 r . ** ce
1ebf2 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 ll consists of t
1ebf3 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66 he integer key f
1ebf4 6f 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 or the right-mos
1ebf5 74 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 t cell of .
1ebf6 20 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e ** the siblin
1ebf7 67 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 g-page assembled
1ebf8 20 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 above only..
1ebf9 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
1ebfa 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
1ebfb 20 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 j--;.
1ebfc 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 btreeParseCe
1ebfd 6c 6c 50 74 72 28 70 4e 65 77 2c 20 61 70 43 65 llPtr(pNew, apCe
1ebfe 6c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 ll[j], &info);.
1ebff 20 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 pCell = p
1ec00 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 73 7a Temp;. sz
1ec01 20 3d 20 34 20 2b 20 70 75 74 56 61 72 69 6e 74 = 4 + putVarint
1ec02 28 26 70 43 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f (&pCell[4], info
1ec03 2e 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 .nKey);.
1ec04 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 pTemp = 0;.
1ec05 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1ec06 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 pCell -= 4;.
1ec07 20 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 /* Obscure c
1ec08 61 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 ase for non-leaf
1ec09 2d 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20 -data trees: If
1ec0a 74 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c the cell at pCel
1ec0b 6c 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a l was. **
1ec0c 20 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 previously stor
1ec0d 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 ed on a leaf nod
1ec0e 65 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f 72 e, and its repor
1ec0f 74 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20 ted size was 4.
1ec10 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c ** bytes,
1ec11 20 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74 then it may act
1ec12 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 ually be smaller
1ec13 20 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20 than this .
1ec14 20 20 20 20 2a 2a 20 28 73 65 65 20 62 74 72 65 ** (see btre
1ec15 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c eParseCellPtr(),
1ec16 20 34 20 62 79 74 65 73 20 69 73 20 74 68 65 20 4 bytes is the
1ec17 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 0a minimum size of.
1ec18 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 63 ** any c
1ec19 65 6c 6c 29 2e 20 42 75 74 20 69 74 20 69 73 20 ell). But it is
1ec1a 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 70 61 73 important to pas
1ec1b 73 20 74 68 65 20 63 6f 72 72 65 63 74 20 73 69 s the correct si
1ec1c 7a 65 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a ze to . *
1ec1d 2a 20 69 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20 * insertCell(),
1ec1e 73 6f 20 72 65 70 61 72 73 65 20 74 68 65 20 63 so reparse the c
1ec1f 65 6c 6c 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20 ell now..
1ec20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e **. ** N
1ec21 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 63 61 ote that this ca
1ec22 6e 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 69 n never happen i
1ec23 6e 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 n an SQLite data
1ec24 20 66 69 6c 65 2c 20 61 73 20 61 6c 6c 0a 20 20 file, as all.
1ec25 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61 ** cells a
1ec26 72 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 re at least 4 by
1ec27 74 65 73 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70 tes. It only hap
1ec28 70 65 6e 73 20 69 6e 20 62 2d 74 72 65 65 73 20 pens in b-trees
1ec29 75 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 used. **
1ec2a 74 6f 20 65 76 61 6c 75 61 74 65 20 22 49 4e 20 to evaluate "IN
1ec2b 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 61 6e (SELECT ...)" an
1ec2c 64 20 73 69 6d 69 6c 61 72 20 63 6c 61 75 73 65 d similar clause
1ec2d 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 s.. */.
1ec2e 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c if( szCell
1ec2f 5b 6a 5d 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20 [j]==4 ){.
1ec30 20 20 20 20 61 73 73 65 72 74 28 6c 65 61 66 43 assert(leafC
1ec31 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 orrection==4);.
1ec32 20 20 20 20 20 20 20 20 20 73 7a 20 3d 20 63 65 sz = ce
1ec33 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e llSizePtr(pParen
1ec34 74 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 t, pCell);.
1ec35 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1ec36 20 20 20 69 4f 76 66 6c 53 70 61 63 65 20 2b 3d iOvflSpace +=
1ec37 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 sz;. asser
1ec38 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 6d 61 78 4c t( sz<=pBt->maxL
1ec39 6f 63 61 6c 2b 32 33 20 29 3b 0a 20 20 20 20 20 ocal+23 );.
1ec3a 20 61 73 73 65 72 74 28 20 69 4f 76 66 6c 53 70 assert( iOvflSp
1ec3b 61 63 65 20 3c 3d 20 28 69 6e 74 29 70 42 74 2d ace <= (int)pBt-
1ec3c 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 >pageSize );.
1ec3d 20 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 insertCell(pP
1ec3e 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43 arent, nxDiv, pC
1ec3f 65 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20 ell, sz, pTemp,
1ec40 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 pNew->pgno, &rc)
1ec41 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
1ec42 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
1ec43 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 balance_cleanup
1ec44 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1ec45 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
1ec46 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d iteable(pParent-
1ec47 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 >pDbPage) );..
1ec48 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 6e j++;. n
1ec49 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 xDiv++;. }.
1ec4a 7d 0a 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 6e }. assert( j==n
1ec4b 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 Cell );. assert
1ec4c 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a 20 20 61 73 ( nOld>0 );. as
1ec4d 73 65 72 74 28 20 6e 4e 65 77 3e 30 20 29 3b 0a sert( nNew>0 );.
1ec4e 20 20 69 66 28 20 28 70 61 67 65 46 6c 61 67 73 if( (pageFlags
1ec4f 20 26 20 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 & PTF_LEAF)==0
1ec50 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 68 69 6c ){. u8 *zChil
1ec51 64 20 3d 20 26 61 70 43 6f 70 79 5b 6e 4f 6c 64 d = &apCopy[nOld
1ec52 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 3b 0a 20 -1]->aData[8];.
1ec53 20 20 20 6d 65 6d 63 70 79 28 26 61 70 4e 65 77 memcpy(&apNew
1ec54 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44 61 74 61 5b [nNew-1]->aData[
1ec55 38 5d 2c 20 7a 43 68 69 6c 64 2c 20 34 29 3b 0a 8], zChild, 4);.
1ec56 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 52 6f 6f }.. if( isRoo
1ec57 74 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 t && pParent->nC
1ec58 65 6c 6c 3d 3d 30 20 26 26 20 70 50 61 72 65 6e ell==0 && pParen
1ec59 74 2d 3e 68 64 72 4f 66 66 73 65 74 3c 3d 61 70 t->hdrOffset<=ap
1ec5a 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20 29 7b New[0]->nFree ){
1ec5b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 . /* The root
1ec5c 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 page of the b-t
1ec5d 72 65 65 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 ree now contains
1ec5e 20 6e 6f 20 63 65 6c 6c 73 2e 20 54 68 65 20 6f no cells. The o
1ec5f 6e 6c 79 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 nly sibling.
1ec60 2a 2a 20 70 61 67 65 20 69 73 20 74 68 65 20 72 ** page is the r
1ec61 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 74 68 ight-child of th
1ec62 65 20 70 61 72 65 6e 74 2e 20 43 6f 70 79 20 74 e parent. Copy t
1ec63 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
1ec64 68 65 0a 20 20 20 20 2a 2a 20 63 68 69 6c 64 20 he. ** child
1ec65 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 61 page into the pa
1ec66 72 65 6e 74 2c 20 64 65 63 72 65 61 73 69 6e 67 rent, decreasing
1ec67 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 68 65 69 the overall hei
1ec68 67 68 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a ght of the. *
1ec69 2a 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 * b-tree structu
1ec6a 72 65 20 62 79 20 6f 6e 65 2e 20 54 68 69 73 20 re by one. This
1ec6b 69 73 20 64 65 73 63 72 69 62 65 64 20 61 73 20 is described as
1ec6c 74 68 65 20 22 62 61 6c 61 6e 63 65 2d 73 68 61 the "balance-sha
1ec6d 6c 6c 6f 77 65 72 22 0a 20 20 20 20 2a 2a 20 73 llower". ** s
1ec6e 75 62 2d 61 6c 67 6f 72 69 74 68 6d 20 69 6e 20 ub-algorithm in
1ec6f 73 6f 6d 65 20 64 6f 63 75 6d 65 6e 74 61 74 69 some documentati
1ec70 6f 6e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a on.. **. *
1ec71 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 * If this is an
1ec72 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 auto-vacuum data
1ec73 62 61 73 65 2c 20 74 68 65 20 63 61 6c 6c 20 74 base, the call t
1ec74 6f 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e o copyNodeConten
1ec75 74 28 29 20 0a 20 20 20 20 2a 2a 20 73 65 74 73 t() . ** sets
1ec76 20 61 6c 6c 20 70 6f 69 6e 74 65 72 2d 6d 61 70 all pointer-map
1ec77 20 65 6e 74 72 69 65 73 20 63 6f 72 72 65 73 70 entries corresp
1ec78 6f 6e 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 onding to databa
1ec79 73 65 20 69 6d 61 67 65 20 70 61 67 65 73 20 0a se image pages .
1ec7a 20 20 20 20 2a 2a 20 66 6f 72 20 77 68 69 63 68 ** for which
1ec7b 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 the pointer is
1ec7c 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 74 68 stored within th
1ec7d 65 20 63 6f 6e 74 65 6e 74 20 62 65 69 6e 67 20 e content being
1ec7e 63 6f 70 69 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 copied.. **.
1ec7f 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 ** The second
1ec80 20 61 73 73 65 72 74 20 62 65 6c 6f 77 20 76 65 assert below ve
1ec81 72 69 66 69 65 73 20 74 68 61 74 20 74 68 65 20 rifies that the
1ec82 63 68 69 6c 64 20 70 61 67 65 20 69 73 20 64 65 child page is de
1ec83 66 72 61 67 6d 65 6e 74 65 64 0a 20 20 20 20 2a fragmented. *
1ec84 2a 20 28 69 74 20 6d 75 73 74 20 62 65 2c 20 61 * (it must be, a
1ec85 73 20 69 74 20 77 61 73 20 6a 75 73 74 20 72 65 s it was just re
1ec86 63 6f 6e 73 74 72 75 63 74 65 64 20 75 73 69 6e constructed usin
1ec87 67 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 29 g assemblePage()
1ec88 29 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a 20 69 ). This. ** i
1ec89 73 20 69 6d 70 6f 72 74 61 6e 74 20 69 66 20 74 s important if t
1ec8a 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 68 he parent page h
1ec8b 61 70 70 65 6e 73 20 74 6f 20 62 65 20 70 61 67 appens to be pag
1ec8c 65 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 e 1 of the datab
1ec8d 61 73 65 0a 20 20 20 20 2a 2a 20 69 6d 61 67 65 ase. ** image
1ec8e 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 . */. assert
1ec8f 28 20 6e 4e 65 77 3d 3d 31 20 29 3b 0a 20 20 20 ( nNew==1 );.
1ec90 20 61 73 73 65 72 74 28 20 61 70 4e 65 77 5b 30 assert( apNew[0
1ec91 5d 2d 3e 6e 46 72 65 65 20 3d 3d 20 0a 20 20 20 ]->nFree == .
1ec92 20 20 20 20 20 28 67 65 74 32 62 79 74 65 28 26 (get2byte(&
1ec93 61 70 4e 65 77 5b 30 5d 2d 3e 61 44 61 74 61 5b apNew[0]->aData[
1ec94 35 5d 29 2d 61 70 4e 65 77 5b 30 5d 2d 3e 63 65 5])-apNew[0]->ce
1ec95 6c 6c 4f 66 66 73 65 74 2d 61 70 4e 65 77 5b 30 llOffset-apNew[0
1ec96 5d 2d 3e 6e 43 65 6c 6c 2a 32 29 20 0a 20 20 20 ]->nCell*2) .
1ec97 20 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64 65 );. copyNode
1ec98 43 6f 6e 74 65 6e 74 28 61 70 4e 65 77 5b 30 5d Content(apNew[0]
1ec99 2c 20 70 50 61 72 65 6e 74 2c 20 26 72 63 29 3b , pParent, &rc);
1ec9a 0a 20 20 20 20 66 72 65 65 50 61 67 65 28 61 70 . freePage(ap
1ec9b 4e 65 77 5b 30 5d 2c 20 26 72 63 29 3b 0a 20 20 New[0], &rc);.
1ec9c 7d 65 6c 73 65 20 69 66 28 20 49 53 41 55 54 4f }else if( ISAUTO
1ec9d 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 2f 2a VACUUM ){. /*
1ec9e 20 46 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72 Fix the pointer
1ec9f 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 -map entries for
1eca0 20 61 6c 6c 20 74 68 65 20 63 65 6c 6c 73 20 74 all the cells t
1eca1 68 61 74 20 77 65 72 65 20 73 68 69 66 74 65 64 hat were shifted
1eca2 20 61 72 6f 75 6e 64 2e 20 0a 20 20 20 20 2a 2a around. . **
1eca3 20 54 68 65 72 65 20 61 72 65 20 73 65 76 65 72 There are sever
1eca4 61 6c 20 64 69 66 66 65 72 65 6e 74 20 74 79 70 al different typ
1eca5 65 73 20 6f 66 20 70 6f 69 6e 74 65 72 2d 6d 61 es of pointer-ma
1eca6 70 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 6e p entries that n
1eca7 65 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 eed to. ** be
1eca8 20 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 74 dealt with by t
1eca9 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 53 6f 6d his routine. Som
1ecaa 65 20 6f 66 20 74 68 65 73 65 20 68 61 76 65 20 e of these have
1ecab 62 65 65 6e 20 73 65 74 20 61 6c 72 65 61 64 79 been set already
1ecac 2c 20 62 75 74 0a 20 20 20 20 2a 2a 20 6d 61 6e , but. ** man
1ecad 79 20 68 61 76 65 20 6e 6f 74 2e 20 54 68 65 20 y have not. The
1ecae 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 20 73 following is a s
1ecaf 75 6d 6d 61 72 79 3a 0a 20 20 20 20 2a 2a 0a 20 ummary:. **.
1ecb0 20 20 20 2a 2a 20 20 20 31 29 20 54 68 65 20 65 ** 1) The e
1ecb1 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 ntries associate
1ecb2 64 20 77 69 74 68 20 6e 65 77 20 73 69 62 6c 69 d with new sibli
1ecb3 6e 67 20 70 61 67 65 73 20 74 68 61 74 20 77 65 ng pages that we
1ecb4 72 65 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 20 20 re not. **
1ecb5 20 20 20 73 69 62 6c 69 6e 67 73 20 77 68 65 6e siblings when
1ecb6 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 this function w
1ecb7 61 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 73 65 as called. These
1ecb8 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 20 20 have already.
1ecb9 20 20 2a 2a 20 20 20 20 20 20 62 65 65 6e 20 73 ** been s
1ecba 65 74 2e 20 57 65 20 64 6f 6e 27 74 20 6e 65 65 et. We don't nee
1ecbb 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 d to worry about
1ecbc 20 6f 6c 64 20 73 69 62 6c 69 6e 67 73 20 74 68 old siblings th
1ecbd 61 74 20 77 65 72 65 0a 20 20 20 20 2a 2a 20 20 at were. **
1ecbe 20 20 20 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 moved to the
1ecbf 20 66 72 65 65 2d 6c 69 73 74 20 2d 20 74 68 65 free-list - the
1ecc0 20 66 72 65 65 50 61 67 65 28 29 20 63 6f 64 65 freePage() code
1ecc1 20 68 61 73 20 74 61 6b 65 6e 20 63 61 72 65 0a has taken care.
1ecc2 20 20 20 20 2a 2a 20 20 20 20 20 20 6f 66 20 74 ** of t
1ecc3 68 6f 73 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 hose.. **.
1ecc4 20 2a 2a 20 20 20 32 29 20 54 68 65 20 70 6f 69 ** 2) The poi
1ecc5 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 nter-map entries
1ecc6 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1ecc7 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 the first overf
1ecc8 6c 6f 77 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 low. **
1ecc9 70 61 67 65 20 69 6e 20 61 6e 79 20 6f 76 65 72 page in any over
1ecca 66 6c 6f 77 20 63 68 61 69 6e 73 20 75 73 65 64 flow chains used
1eccb 20 62 79 20 6e 65 77 20 64 69 76 69 64 65 72 20 by new divider
1eccc 63 65 6c 6c 73 2e 20 54 68 65 73 65 20 0a 20 20 cells. These .
1eccd 20 20 2a 2a 20 20 20 20 20 20 68 61 76 65 20 61 ** have a
1ecce 6c 73 6f 20 61 6c 72 65 61 64 79 20 62 65 65 6e lso already been
1eccf 20 74 61 6b 65 6e 20 63 61 72 65 20 6f 66 20 62 taken care of b
1ecd0 79 20 74 68 65 20 69 6e 73 65 72 74 43 65 6c 6c y the insertCell
1ecd1 28 29 20 63 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a () code.. **.
1ecd2 20 20 20 20 2a 2a 20 20 20 33 29 20 49 66 20 74 ** 3) If t
1ecd3 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 he sibling pages
1ecd4 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c are not leaves,
1ecd5 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64 20 then the child
1ecd6 70 61 67 65 73 20 6f 66 0a 20 20 20 20 2a 2a 20 pages of. **
1ecd7 20 20 20 20 20 63 65 6c 6c 73 20 73 74 6f 72 65 cells store
1ecd8 64 20 6f 6e 20 74 68 65 20 73 69 62 6c 69 6e 67 d on the sibling
1ecd9 20 70 61 67 65 73 20 6d 61 79 20 6e 65 65 64 20 pages may need
1ecda 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 to be updated..
1ecdb 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 34 **. ** 4
1ecdc 29 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 ) If the sibling
1ecdd 20 70 61 67 65 73 20 61 72 65 20 6e 6f 74 20 69 pages are not i
1ecde 6e 74 65 72 6e 61 6c 20 69 6e 74 6b 65 79 20 6e nternal intkey n
1ecdf 6f 64 65 73 2c 20 74 68 65 6e 20 61 6e 79 0a 20 odes, then any.
1ece0 20 20 20 2a 2a 20 20 20 20 20 20 6f 76 65 72 66 ** overf
1ece1 6c 6f 77 20 70 61 67 65 73 20 75 73 65 64 20 62 low pages used b
1ece2 79 20 74 68 65 73 65 20 63 65 6c 6c 73 20 6d 61 y these cells ma
1ece3 79 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 y need to be upd
1ece4 61 74 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 ated. **
1ece5 20 28 69 6e 74 65 72 6e 61 6c 20 69 6e 74 6b 65 (internal intke
1ece6 79 20 6e 6f 64 65 73 20 6e 65 76 65 72 20 63 6f y nodes never co
1ece7 6e 74 61 69 6e 20 70 6f 69 6e 74 65 72 73 20 74 ntain pointers t
1ece8 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 o overflow pages
1ece9 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a ).. **. **
1ecea 20 20 20 35 29 20 49 66 20 74 68 65 20 73 69 62 5) If the sib
1eceb 6c 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 6e ling pages are n
1ecec 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 ot leaves, then
1eced 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a the pointer-map.
1ecee 20 20 20 20 2a 2a 20 20 20 20 20 20 65 6e 74 72 ** entr
1ecef 69 65 73 20 66 6f 72 20 74 68 65 20 72 69 67 68 ies for the righ
1ecf0 74 2d 63 68 69 6c 64 20 70 61 67 65 73 20 6f 66 t-child pages of
1ecf1 20 65 61 63 68 20 73 69 62 6c 69 6e 67 20 6d 61 each sibling ma
1ecf2 79 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 20 20 y need. **
1ecf3 20 20 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 to be updated
1ecf4 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1ecf5 43 61 73 65 73 20 31 20 61 6e 64 20 32 20 61 72 Cases 1 and 2 ar
1ecf6 65 20 64 65 61 6c 74 20 77 69 74 68 20 61 62 6f e dealt with abo
1ecf7 76 65 20 62 79 20 6f 74 68 65 72 20 63 6f 64 65 ve by other code
1ecf8 2e 20 54 68 65 20 6e 65 78 74 0a 20 20 20 20 2a . The next. *
1ecf9 2a 20 62 6c 6f 63 6b 20 64 65 61 6c 73 20 77 69 * block deals wi
1ecfa 74 68 20 63 61 73 65 73 20 33 20 61 6e 64 20 34 th cases 3 and 4
1ecfb 20 61 6e 64 20 74 68 65 20 6f 6e 65 20 61 66 74 and the one aft
1ecfc 65 72 20 74 68 61 74 2c 20 63 61 73 65 20 35 2e er that, case 5.
1ecfd 20 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20 73 65 Since. ** se
1ecfe 74 74 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 tting a pointer
1ecff 6d 61 70 20 65 6e 74 72 79 20 69 73 20 61 20 72 map entry is a r
1ed00 65 6c 61 74 69 76 65 6c 79 20 65 78 70 65 6e 73 elatively expens
1ed01 69 76 65 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 ive operation, t
1ed02 68 69 73 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 his. ** code
1ed03 6f 6e 6c 79 20 73 65 74 73 20 70 6f 69 6e 74 65 only sets pointe
1ed04 72 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f r map entries fo
1ed05 72 20 63 68 69 6c 64 20 6f 72 20 6f 76 65 72 66 r child or overf
1ed06 6c 6f 77 20 70 61 67 65 73 20 74 68 61 74 20 68 low pages that h
1ed07 61 76 65 0a 20 20 20 20 2a 2a 20 61 63 74 75 61 ave. ** actua
1ed08 6c 6c 79 20 6d 6f 76 65 64 20 62 65 74 77 65 65 lly moved betwee
1ed09 6e 20 70 61 67 65 73 2e 20 20 2a 2f 0a 20 20 20 n pages. */.
1ed0a 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 20 3d MemPage *pNew =
1ed0b 20 61 70 4e 65 77 5b 30 5d 3b 0a 20 20 20 20 4d apNew[0];. M
1ed0c 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61 emPage *pOld = a
1ed0d 70 43 6f 70 79 5b 30 5d 3b 0a 20 20 20 20 69 6e pCopy[0];. in
1ed0e 74 20 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 4f t nOverflow = pO
1ed0f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 ld->nOverflow;.
1ed10 20 20 20 69 6e 74 20 69 4e 65 78 74 4f 6c 64 20 int iNextOld
1ed11 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 20 2b 20 = pOld->nCell +
1ed12 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 nOverflow;. i
1ed13 6e 74 20 69 4f 76 65 72 66 6c 6f 77 20 3d 20 28 nt iOverflow = (
1ed14 6e 4f 76 65 72 66 6c 6f 77 20 3f 20 70 4f 6c 64 nOverflow ? pOld
1ed15 2d 3e 61 69 4f 76 66 6c 5b 30 5d 20 3a 20 2d 31 ->aiOvfl[0] : -1
1ed16 29 3b 0a 20 20 20 20 6a 20 3d 20 30 3b 20 20 20 );. j = 0;
1ed17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ed18 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
1ed19 72 65 6e 74 20 27 6f 6c 64 27 20 73 69 62 6c 69 rent 'old' sibli
1ed1a 6e 67 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 6b ng page */. k
1ed1b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1ed1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ed1d 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 27 6e 65 /* Current 'ne
1ed1e 77 27 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 w' sibling page
1ed1f 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 */. for(i=0;
1ed20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 i<nCell; i++){.
1ed21 20 20 20 20 20 69 6e 74 20 69 73 44 69 76 69 64 int isDivid
1ed22 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 er = 0;. wh
1ed23 69 6c 65 28 20 69 3d 3d 69 4e 65 78 74 4f 6c 64 ile( i==iNextOld
1ed24 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 ){. /* C
1ed25 65 6c 6c 20 69 20 69 73 20 74 68 65 20 63 65 6c ell i is the cel
1ed26 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f l immediately fo
1ed27 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74 llowing the last
1ed28 20 63 65 6c 6c 20 6f 6e 20 6f 6c 64 0a 20 20 20 cell on old.
1ed29 20 20 20 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 ** sibling
1ed2a 70 61 67 65 20 6a 2e 20 49 66 20 74 68 65 20 73 page j. If the s
1ed2b 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 iblings are not
1ed2c 6c 65 61 66 20 70 61 67 65 73 20 6f 66 20 61 6e leaf pages of an
1ed2d 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6b . ** intk
1ed2e 65 79 20 62 2d 74 72 65 65 2c 20 74 68 65 6e 20 ey b-tree, then
1ed2f 63 65 6c 6c 20 69 20 77 61 73 20 61 20 64 69 76 cell i was a div
1ed30 69 64 65 72 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 ider cell. */.
1ed31 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 2b assert( j+
1ed32 31 20 3c 20 41 72 72 61 79 53 69 7a 65 28 61 70 1 < ArraySize(ap
1ed33 43 6f 70 79 29 20 29 3b 0a 20 20 20 20 20 20 20 Copy) );.
1ed34 20 61 73 73 65 72 74 28 20 6a 2b 31 20 3c 20 6e assert( j+1 < n
1ed35 4f 6c 64 20 29 3b 0a 20 20 20 20 20 20 20 20 70 Old );. p
1ed36 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 2b 2b 6a Old = apCopy[++j
1ed37 5d 3b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74 ];. iNext
1ed38 4f 6c 64 20 3d 20 69 20 2b 20 21 6c 65 61 66 44 Old = i + !leafD
1ed39 61 74 61 20 2b 20 70 4f 6c 64 2d 3e 6e 43 65 6c ata + pOld->nCel
1ed3a 6c 20 2b 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 l + pOld->nOverf
1ed3b 6c 6f 77 3b 0a 20 20 20 20 20 20 20 20 69 66 28 low;. if(
1ed3c 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pOld->nOverflow
1ed3d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 4f ){. nO
1ed3e 76 65 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 2d 3e verflow = pOld->
1ed3f 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 20 nOverflow;.
1ed40 20 20 20 20 20 69 4f 76 65 72 66 6c 6f 77 20 3d iOverflow =
1ed41 20 69 20 2b 20 21 6c 65 61 66 44 61 74 61 20 2b i + !leafData +
1ed42 20 70 4f 6c 64 2d 3e 61 69 4f 76 66 6c 5b 30 5d pOld->aiOvfl[0]
1ed43 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1ed44 20 20 20 20 69 73 44 69 76 69 64 65 72 20 3d 20 isDivider =
1ed45 21 6c 65 61 66 44 61 74 61 3b 20 20 0a 20 20 20 !leafData; .
1ed46 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 }.. asse
1ed47 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c rt(nOverflow>0 |
1ed48 7c 20 69 4f 76 65 72 66 6c 6f 77 3c 69 20 29 3b | iOverflow<i );
1ed49 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e 4f . assert(nO
1ed4a 76 65 72 66 6c 6f 77 3c 32 20 7c 7c 20 70 4f 6c verflow<2 || pOl
1ed4b 64 2d 3e 61 69 4f 76 66 6c 5b 30 5d 3d 3d 70 4f d->aiOvfl[0]==pO
1ed4c 6c 64 2d 3e 61 69 4f 76 66 6c 5b 31 5d 2d 31 29 ld->aiOvfl[1]-1)
1ed4d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e ;. assert(n
1ed4e 4f 76 65 72 66 6c 6f 77 3c 33 20 7c 7c 20 70 4f Overflow<3 || pO
1ed4f 6c 64 2d 3e 61 69 4f 76 66 6c 5b 31 5d 3d 3d 70 ld->aiOvfl[1]==p
1ed50 4f 6c 64 2d 3e 61 69 4f 76 66 6c 5b 32 5d 2d 31 Old->aiOvfl[2]-1
1ed51 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d );. if( i==
1ed52 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 iOverflow ){.
1ed53 20 20 20 20 20 69 73 44 69 76 69 64 65 72 20 3d isDivider =
1ed54 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 1;. if(
1ed55 28 2d 2d 6e 4f 76 65 72 66 6c 6f 77 29 3e 30 20 (--nOverflow)>0
1ed56 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 4f 76 ){. iOv
1ed57 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 20 20 erflow++;.
1ed58 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 }. }..
1ed59 20 20 20 69 66 28 20 69 3d 3d 63 6e 74 4e 65 77 if( i==cntNew
1ed5a 5b 6b 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f [k] ){. /
1ed5b 2a 20 43 65 6c 6c 20 69 20 69 73 20 74 68 65 20 * Cell i is the
1ed5c 63 65 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 cell immediately
1ed5d 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c following the l
1ed5e 61 73 74 20 63 65 6c 6c 20 6f 6e 20 6e 65 77 0a ast cell on new.
1ed5f 20 20 20 20 20 20 20 20 2a 2a 20 73 69 62 6c 69 ** sibli
1ed60 6e 67 20 70 61 67 65 20 6b 2e 20 49 66 20 74 68 ng page k. If th
1ed61 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e e siblings are n
1ed62 6f 74 20 6c 65 61 66 20 70 61 67 65 73 20 6f 66 ot leaf pages of
1ed63 20 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 an. ** i
1ed64 6e 74 6b 65 79 20 62 2d 74 72 65 65 2c 20 74 68 ntkey b-tree, th
1ed65 65 6e 20 63 65 6c 6c 20 69 20 69 73 20 61 20 64 en cell i is a d
1ed66 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 20 2a 2f ivider cell. */
1ed67 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 . pNew =
1ed68 61 70 4e 65 77 5b 2b 2b 6b 5d 3b 0a 20 20 20 20 apNew[++k];.
1ed69 20 20 20 20 69 66 28 20 21 6c 65 61 66 44 61 74 if( !leafDat
1ed6a 61 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 a ) continue;.
1ed6b 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 }. asse
1ed6c 72 74 28 20 6a 3c 6e 4f 6c 64 20 29 3b 0a 20 20 rt( j<nOld );.
1ed6d 20 20 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4e assert( k<nN
1ed6e 65 77 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ew );.. /*
1ed6f 49 66 20 74 68 65 20 63 65 6c 6c 20 77 61 73 20 If the cell was
1ed70 6f 72 69 67 69 6e 61 6c 6c 79 20 64 69 76 69 64 originally divid
1ed71 65 72 20 63 65 6c 6c 20 28 61 6e 64 20 69 73 20 er cell (and is
1ed72 6e 6f 74 20 6e 6f 77 29 20 6f 72 0a 20 20 20 20 not now) or.
1ed73 20 20 2a 2a 20 61 6e 20 6f 76 65 72 66 6c 6f 77 ** an overflow
1ed74 20 63 65 6c 6c 2c 20 6f 72 20 69 66 20 74 68 65 cell, or if the
1ed75 20 63 65 6c 6c 20 77 61 73 20 6c 6f 63 61 74 65 cell was locate
1ed76 64 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 d on a different
1ed77 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 20 20 2a sibling. *
1ed78 2a 20 70 61 67 65 20 62 65 66 6f 72 65 20 74 68 * page before th
1ed79 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 e balancing, the
1ed7a 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 n the pointer ma
1ed7b 70 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 p entries associ
1ed7c 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 77 69 ated. ** wi
1ed7d 74 68 20 61 6e 79 20 63 68 69 6c 64 20 6f 72 20 th any child or
1ed7e 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 6e overflow pages n
1ed7f 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 eed to be update
1ed80 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 d. */. if(
1ed81 20 69 73 44 69 76 69 64 65 72 20 7c 7c 20 70 4f isDivider || pO
1ed82 6c 64 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e ld->pgno!=pNew->
1ed83 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 pgno ){.
1ed84 69 66 28 20 21 6c 65 61 66 43 6f 72 72 65 63 74 if( !leafCorrect
1ed85 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ion ){.
1ed86 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
1ed87 67 65 74 34 62 79 74 65 28 61 70 43 65 6c 6c 5b get4byte(apCell[
1ed88 69 5d 29 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 i]), PTRMAP_BTRE
1ed89 45 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 E, pNew->pgno, &
1ed8a 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc);. }.
1ed8b 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c if( szCel
1ed8c 6c 5b 69 5d 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f l[i]>pNew->minLo
1ed8d 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 cal ){.
1ed8e 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 ptrmapPutOvflPt
1ed8f 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 69 r(pNew, apCell[i
1ed90 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 ], &rc);.
1ed91 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
1ed92 0a 0a 20 20 20 20 69 66 28 20 21 6c 65 61 66 43 .. if( !leafC
1ed93 6f 72 72 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20 orrection ){.
1ed94 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e for(i=0; i<nN
1ed95 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ew; i++){.
1ed96 20 20 75 33 32 20 6b 65 79 20 3d 20 67 65 74 34 u32 key = get4
1ed97 62 79 74 65 28 26 61 70 4e 65 77 5b 69 5d 2d 3e byte(&apNew[i]->
1ed98 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20 aData[8]);.
1ed99 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 ptrmapPut(pBt
1ed9a 2c 20 6b 65 79 2c 20 50 54 52 4d 41 50 5f 42 54 , key, PTRMAP_BT
1ed9b 52 45 45 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 REE, apNew[i]->p
1ed9c 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 gno, &rc);.
1ed9d 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a }. }..#if 0.
1ed9e 20 20 20 20 2f 2a 20 54 68 65 20 70 74 72 6d 61 /* The ptrma
1ed9f 70 43 68 65 63 6b 50 61 67 65 73 28 29 20 63 6f pCheckPages() co
1eda0 6e 74 61 69 6e 73 20 61 73 73 65 72 74 28 29 20 ntains assert()
1eda1 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 statements that
1eda2 76 65 72 69 66 79 20 74 68 61 74 0a 20 20 20 20 verify that.
1eda3 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74 65 72 20 6d ** all pointer m
1eda4 61 70 20 70 61 67 65 73 20 61 72 65 20 73 65 74 ap pages are set
1eda5 20 63 6f 72 72 65 63 74 6c 79 2e 20 54 68 69 73 correctly. This
1eda6 20 69 73 20 68 65 6c 70 66 75 6c 20 77 68 69 6c is helpful whil
1eda7 65 20 0a 20 20 20 20 2a 2a 20 64 65 62 75 67 67 e . ** debugg
1eda8 69 6e 67 2e 20 54 68 69 73 20 69 73 20 75 73 75 ing. This is usu
1eda9 61 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 62 65 ally disabled be
1edaa 63 61 75 73 65 20 61 20 63 6f 72 72 75 70 74 20 cause a corrupt
1edab 64 61 74 61 62 61 73 65 20 6d 61 79 0a 20 20 20 database may.
1edac 20 2a 2a 20 63 61 75 73 65 20 61 6e 20 61 73 73 ** cause an ass
1edad 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20 ert() statement
1edae 74 6f 20 66 61 69 6c 2e 20 20 2a 2f 0a 20 20 20 to fail. */.
1edaf 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 ptrmapCheckPage
1edb0 73 28 61 70 4e 65 77 2c 20 6e 4e 65 77 29 3b 0a s(apNew, nNew);.
1edb1 20 20 20 20 70 74 72 6d 61 70 43 68 65 63 6b 50 ptrmapCheckP
1edb2 61 67 65 73 28 26 70 50 61 72 65 6e 74 2c 20 31 ages(&pParent, 1
1edb3 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 );.#endif. }..
1edb4 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 assert( pParent
1edb5 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 54 52 ->isInit );. TR
1edb6 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 66 ACE(("BALANCE: f
1edb7 69 6e 69 73 68 65 64 3a 20 6f 6c 64 3d 25 64 20 inished: old=%d
1edb8 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c new=%d cells=%d\
1edb9 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 6e 4f n",. nO
1edba 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 ld, nNew, nCell)
1edbb 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c );.. /*. ** Cl
1edbc 65 61 6e 75 70 20 62 65 66 6f 72 65 20 72 65 74 eanup before ret
1edbd 75 72 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c urning.. */.bal
1edbe 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 ance_cleanup:.
1edbf 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 sqlite3ScratchFr
1edc0 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20 20 66 6f ee(apCell);. fo
1edc1 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 r(i=0; i<nOld; i
1edc2 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 ++){. release
1edc3 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a Page(apOld[i]);.
1edc4 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
1edc5 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nNew; i++){.
1edc6 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4e releasePage(apN
1edc7 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 ew[i]);. }.. r
1edc8 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 69 66 20 eturn rc;.}.#if
1edc9 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 defined(_MSC_VER
1edca 29 20 26 26 20 5f 4d 53 43 5f 56 45 52 20 3e 3d ) && _MSC_VER >=
1edcb 20 31 37 30 30 20 26 26 20 64 65 66 69 6e 65 64 1700 && defined
1edcc 28 5f 4d 5f 41 52 4d 29 0a 23 70 72 61 67 6d 61 (_M_ARM).#pragma
1edcd 20 6f 70 74 69 6d 69 7a 65 28 22 22 2c 20 6f 6e optimize("", on
1edce 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a ).#endif.../*.**
1edcf 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
1edd0 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 s called when th
1edd1 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 e root page of a
1edd2 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 b-tree structur
1edd3 65 20 69 73 0a 2a 2a 20 6f 76 65 72 66 75 6c 6c e is.** overfull
1edd4 20 28 68 61 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 (has one or mor
1edd5 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 e overflow pages
1edd6 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 63 )..**.** A new c
1edd7 68 69 6c 64 20 70 61 67 65 20 69 73 20 61 6c 6c hild page is all
1edd8 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 63 ocated and the c
1edd9 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 ontents of the c
1edda 75 72 72 65 6e 74 20 72 6f 6f 74 0a 2a 2a 20 70 urrent root.** p
1eddb 61 67 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 6f age, including o
1eddc 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2c 20 61 verflow cells, a
1eddd 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 re copied into t
1edde 68 65 20 63 68 69 6c 64 2e 20 54 68 65 20 72 6f he child. The ro
1eddf 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 74 68 ot.** page is th
1ede0 65 6e 20 6f 76 65 72 77 72 69 74 74 65 6e 20 74 en overwritten t
1ede1 6f 20 6d 61 6b 65 20 69 74 20 61 6e 20 65 6d 70 o make it an emp
1ede2 74 79 20 70 61 67 65 20 77 69 74 68 20 74 68 65 ty page with the
1ede3 20 72 69 67 68 74 2d 63 68 69 6c 64 20 0a 2a 2a right-child .**
1ede4 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 69 6e pointer pointin
1ede5 67 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67 g to the new pag
1ede6 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65 20 e..**.** Before
1ede7 72 65 74 75 72 6e 69 6e 67 2c 20 61 6c 6c 20 70 returning, all p
1ede8 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 ointer-map entri
1ede9 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 es corresponding
1edea 20 74 6f 20 70 61 67 65 73 20 0a 2a 2a 20 74 68 to pages .** th
1edeb 61 74 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64 at the new child
1edec 2d 70 61 67 65 20 6e 6f 77 20 63 6f 6e 74 61 69 -page now contai
1eded 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 ns pointers to a
1edee 72 65 20 75 70 64 61 74 65 64 2e 20 54 68 65 0a re updated. The.
1edef 2a 2a 20 65 6e 74 72 79 20 63 6f 72 72 65 73 70 ** entry corresp
1edf0 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 onding to the ne
1edf1 77 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f w right-child po
1edf2 69 6e 74 65 72 20 6f 66 20 74 68 65 20 72 6f 6f inter of the roo
1edf3 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 6c 73 t.** page is als
1edf4 6f 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a o updated..**.**
1edf5 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
1edf6 2a 70 70 43 68 69 6c 64 20 69 73 20 73 65 74 20 *ppChild is set
1edf7 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 66 to contain a ref
1edf8 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63 68 erence to the ch
1edf9 69 6c 64 20 0a 2a 2a 20 70 61 67 65 20 61 6e 64 ild .** page and
1edfa 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
1edfb 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 turned. In this
1edfc 63 61 73 65 20 74 68 65 20 63 61 6c 6c 65 72 20 case the caller
1edfd 69 73 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 74 is required.** t
1edfe 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 61 o call releasePa
1edff 67 65 28 29 20 6f 6e 20 2a 70 70 43 68 69 6c 64 ge() on *ppChild
1ee00 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 2e 20 49 exactly once. I
1ee01 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
1ee02 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 s,.** an error c
1ee03 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ode is returned
1ee04 61 6e 64 20 2a 70 70 43 68 69 6c 64 20 69 73 20 and *ppChild is
1ee05 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61 set to 0..*/.sta
1ee06 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f tic int balance_
1ee07 64 65 65 70 65 72 28 4d 65 6d 50 61 67 65 20 2a deeper(MemPage *
1ee08 70 52 6f 6f 74 2c 20 4d 65 6d 50 61 67 65 20 2a pRoot, MemPage *
1ee09 2a 70 70 43 68 69 6c 64 29 7b 0a 20 20 69 6e 74 *ppChild){. int
1ee0a 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1ee0b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1ee0c 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d eturn value from
1ee0d 20 73 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a subprocedures *
1ee0e 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 /. MemPage *pCh
1ee0f 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ild = 0;
1ee10 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1ee11 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 a new child pag
1ee12 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
1ee13 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 Child = 0;
1ee14 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
1ee15 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20 mber of the new
1ee16 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20 child page */.
1ee17 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
1ee18 70 52 6f 6f 74 2d 3e 70 42 74 3b 20 20 20 20 2f pRoot->pBt; /
1ee19 2a 20 54 68 65 20 42 54 72 65 65 20 2a 2f 0a 0a * The BTree */..
1ee1a 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d assert( pRoot-
1ee1b 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a >nOverflow>0 );.
1ee1c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1ee1d 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
1ee1e 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f ->mutex) );.. /
1ee1f 2a 20 4d 61 6b 65 20 70 52 6f 6f 74 2c 20 74 68 * Make pRoot, th
1ee20 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 e root page of t
1ee21 68 65 20 62 2d 74 72 65 65 2c 20 77 72 69 74 61 he b-tree, writa
1ee22 62 6c 65 2e 20 41 6c 6c 6f 63 61 74 65 20 61 20 ble. Allocate a
1ee23 6e 65 77 20 0a 20 20 2a 2a 20 70 61 67 65 20 74 new . ** page t
1ee24 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 hat will become
1ee25 74 68 65 20 6e 65 77 20 72 69 67 68 74 2d 63 68 the new right-ch
1ee26 69 6c 64 20 6f 66 20 70 50 61 67 65 2e 20 43 6f ild of pPage. Co
1ee27 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a py the contents.
1ee28 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 6f 64 65 ** of the node
1ee29 20 73 74 6f 72 65 64 20 6f 6e 20 70 52 6f 6f 74 stored on pRoot
1ee2a 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 20 63 68 into the new ch
1ee2b 69 6c 64 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 ild page.. */.
1ee2c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1ee2d 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 erWrite(pRoot->p
1ee2e 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 DbPage);. if( r
1ee2f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1ee30 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 rc = allocat
1ee31 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 26 eBtreePage(pBt,&
1ee32 70 43 68 69 6c 64 2c 26 70 67 6e 6f 43 68 69 6c pChild,&pgnoChil
1ee33 64 2c 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 30 29 d,pRoot->pgno,0)
1ee34 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64 65 43 6f ;. copyNodeCo
1ee35 6e 74 65 6e 74 28 70 52 6f 6f 74 2c 20 70 43 68 ntent(pRoot, pCh
1ee36 69 6c 64 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 ild, &rc);. i
1ee37 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 f( ISAUTOVACUUM
1ee38 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 ){. ptrmapP
1ee39 75 74 28 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c ut(pBt, pgnoChil
1ee3a 64 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c d, PTRMAP_BTREE,
1ee3b 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 20 26 72 pRoot->pgno, &r
1ee3c 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 c);. }. }.
1ee3d 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 if( rc ){. *p
1ee3e 70 43 68 69 6c 64 20 3d 20 30 3b 0a 20 20 20 20 pChild = 0;.
1ee3f 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 68 69 releasePage(pChi
1ee40 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ld);. return
1ee41 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 rc;. }. assert
1ee42 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
1ee43 77 72 69 74 65 61 62 6c 65 28 70 43 68 69 6c 64 writeable(pChild
1ee44 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
1ee45 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
1ee46 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
1ee47 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 20 pRoot->pDbPage)
1ee48 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 68 );. assert( pCh
1ee49 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 52 6f 6f ild->nCell==pRoo
1ee4a 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 54 t->nCell );.. T
1ee4b 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 RACE(("BALANCE:
1ee4c 63 6f 70 79 20 72 6f 6f 74 20 25 64 20 69 6e 74 copy root %d int
1ee4d 6f 20 25 64 5c 6e 22 2c 20 70 52 6f 6f 74 2d 3e o %d\n", pRoot->
1ee4e 70 67 6e 6f 2c 20 70 43 68 69 6c 64 2d 3e 70 67 pgno, pChild->pg
1ee4f 6e 6f 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79 no));.. /* Copy
1ee50 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 the overflow ce
1ee51 6c 6c 73 20 66 72 6f 6d 20 70 52 6f 6f 74 20 74 lls from pRoot t
1ee52 6f 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 6d 65 o pChild */. me
1ee53 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e 61 69 4f mcpy(pChild->aiO
1ee54 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 61 69 4f 76 vfl, pRoot->aiOv
1ee55 66 6c 2c 0a 20 20 20 20 20 20 20 20 20 70 52 6f fl,. pRo
1ee56 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2a 73 69 ot->nOverflow*si
1ee57 7a 65 6f 66 28 70 52 6f 6f 74 2d 3e 61 69 4f 76 zeof(pRoot->aiOv
1ee58 66 6c 5b 30 5d 29 29 3b 0a 20 20 6d 65 6d 63 70 fl[0]));. memcp
1ee59 79 28 70 43 68 69 6c 64 2d 3e 61 70 4f 76 66 6c y(pChild->apOvfl
1ee5a 2c 20 70 52 6f 6f 74 2d 3e 61 70 4f 76 66 6c 2c , pRoot->apOvfl,
1ee5b 0a 20 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d . pRoot-
1ee5c 3e 6e 4f 76 65 72 66 6c 6f 77 2a 73 69 7a 65 6f >nOverflow*sizeo
1ee5d 66 28 70 52 6f 6f 74 2d 3e 61 70 4f 76 66 6c 5b f(pRoot->apOvfl[
1ee5e 30 5d 29 29 3b 0a 20 20 70 43 68 69 6c 64 2d 3e 0]));. pChild->
1ee5f 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 52 6f 6f nOverflow = pRoo
1ee60 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 0a 20 t->nOverflow;..
1ee61 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20 63 6f 6e /* Zero the con
1ee62 74 65 6e 74 73 20 6f 66 20 70 52 6f 6f 74 2e 20 tents of pRoot.
1ee63 54 68 65 6e 20 69 6e 73 74 61 6c 6c 20 70 43 68 Then install pCh
1ee64 69 6c 64 20 61 73 20 74 68 65 20 72 69 67 68 74 ild as the right
1ee65 2d 63 68 69 6c 64 2e 20 2a 2f 0a 20 20 7a 65 72 -child. */. zer
1ee66 6f 50 61 67 65 28 70 52 6f 6f 74 2c 20 70 43 68 oPage(pRoot, pCh
1ee67 69 6c 64 2d 3e 61 44 61 74 61 5b 30 5d 20 26 20 ild->aData[0] &
1ee68 7e 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 70 75 ~PTF_LEAF);. pu
1ee69 74 34 62 79 74 65 28 26 70 52 6f 6f 74 2d 3e 61 t4byte(&pRoot->a
1ee6a 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 4f Data[pRoot->hdrO
1ee6b 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 ffset+8], pgnoCh
1ee6c 69 6c 64 29 3b 0a 0a 20 20 2a 70 70 43 68 69 6c ild);.. *ppChil
1ee6d 64 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 72 65 d = pChild;. re
1ee6e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1ee6f 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 }../*.** The pag
1ee70 65 20 74 68 61 74 20 70 43 75 72 20 63 75 72 72 e that pCur curr
1ee71 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 ently points to
1ee72 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 6d 6f has just been mo
1ee73 64 69 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f 6d dified in.** som
1ee74 65 20 77 61 79 2e 20 54 68 69 73 20 66 75 6e 63 e way. This func
1ee75 74 69 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 74 tion figures out
1ee76 20 69 66 20 74 68 69 73 20 6d 6f 64 69 66 69 63 if this modific
1ee77 61 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0a ation means the.
1ee78 2a 2a 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f ** tree needs to
1ee79 20 62 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e be balanced, an
1ee7a 64 20 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68 d if so calls th
1ee7b 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 62 61 e appropriate ba
1ee7c 6c 61 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 74 lancing .** rout
1ee7d 69 6e 65 2e 20 42 61 6c 61 6e 63 69 6e 67 20 72 ine. Balancing r
1ee7e 6f 75 74 69 6e 65 73 20 61 72 65 3a 0a 2a 2a 0a outines are:.**.
1ee7f 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f 71 75 69 ** balance_qui
1ee80 63 6b 28 29 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 ck().** balanc
1ee81 65 5f 64 65 65 70 65 72 28 29 0a 2a 2a 20 20 20 e_deeper().**
1ee82 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 balance_nonroot(
1ee83 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ).*/.static int
1ee84 62 61 6c 61 6e 63 65 28 42 74 43 75 72 73 6f 72 balance(BtCursor
1ee85 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 *pCur){. int r
1ee86 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1ee87 20 63 6f 6e 73 74 20 69 6e 74 20 6e 4d 69 6e 20 const int nMin
1ee88 3d 20 70 43 75 72 2d 3e 70 42 74 2d 3e 75 73 61 = pCur->pBt->usa
1ee89 62 6c 65 53 69 7a 65 20 2a 20 32 20 2f 20 33 3b bleSize * 2 / 3;
1ee8a 0a 20 20 75 38 20 61 42 61 6c 61 6e 63 65 51 75 . u8 aBalanceQu
1ee8b 69 63 6b 53 70 61 63 65 5b 31 33 5d 3b 0a 20 20 ickSpace[13];.
1ee8c 75 38 20 2a 70 46 72 65 65 20 3d 20 30 3b 0a 0a u8 *pFree = 0;..
1ee8d 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 TESTONLY( int
1ee8e 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 5f 63 61 balance_quick_ca
1ee8f 6c 6c 65 64 20 3d 20 30 20 29 3b 0a 20 20 54 45 lled = 0 );. TE
1ee90 53 54 4f 4e 4c 59 28 20 69 6e 74 20 62 61 6c 61 STONLY( int bala
1ee91 6e 63 65 5f 64 65 65 70 65 72 5f 63 61 6c 6c 65 nce_deeper_calle
1ee92 64 20 3d 20 30 20 29 3b 0a 0a 20 20 64 6f 20 7b d = 0 );.. do {
1ee93 0a 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d . int iPage =
1ee94 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 pCur->iPage;.
1ee95 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
1ee96 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
1ee97 69 50 61 67 65 5d 3b 0a 0a 20 20 20 20 69 66 28 iPage];.. if(
1ee98 20 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 iPage==0 ){.
1ee99 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f if( pPage->nO
1ee9a 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 verflow ){.
1ee9b 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 /* The root p
1ee9c 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 age of the b-tre
1ee9d 65 20 69 73 20 6f 76 65 72 66 75 6c 6c 2e 20 49 e is overfull. I
1ee9e 6e 20 74 68 69 73 20 63 61 73 65 20 63 61 6c 6c n this case call
1ee9f 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 the. **
1eea0 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29 balance_deeper()
1eea1 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 72 65 function to cre
1eea2 61 74 65 20 61 20 6e 65 77 20 63 68 69 6c 64 20 ate a new child
1eea3 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 for the root-pag
1eea4 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 e. ** and
1eea5 20 63 6f 70 79 20 74 68 65 20 63 75 72 72 65 6e copy the curren
1eea6 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 t contents of th
1eea7 65 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f 20 69 e root-page to i
1eea8 74 2e 20 54 68 65 0a 20 20 20 20 20 20 20 20 2a t. The. *
1eea9 2a 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e * next iteration
1eeaa 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 20 of the do-loop
1eeab 77 69 6c 6c 20 62 61 6c 61 6e 63 65 20 74 68 65 will balance the
1eeac 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 child page..
1eead 20 20 20 20 20 2a 2f 20 0a 20 20 20 20 20 20 20 */ .
1eeae 20 61 73 73 65 72 74 28 20 28 62 61 6c 61 6e 63 assert( (balanc
1eeaf 65 5f 64 65 65 70 65 72 5f 63 61 6c 6c 65 64 2b e_deeper_called+
1eeb0 2b 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 +)==0 );.
1eeb1 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 64 65 rc = balance_de
1eeb2 65 70 65 72 28 70 50 61 67 65 2c 20 26 70 43 75 eper(pPage, &pCu
1eeb3 72 2d 3e 61 70 50 61 67 65 5b 31 5d 29 3b 0a 20 r->apPage[1]);.
1eeb4 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
1eeb5 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1eeb6 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 pCur->iPag
1eeb7 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 e = 1;.
1eeb8 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 pCur->aiIdx[0]
1eeb9 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 = 0;. p
1eeba 43 75 72 2d 3e 61 69 49 64 78 5b 31 5d 20 3d 20 Cur->aiIdx[1] =
1eebb 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 0;. ass
1eebc 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 ert( pCur->apPag
1eebd 65 5b 31 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 e[1]->nOverflow
1eebe 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
1eebf 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1eec0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
1eec1 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 . }else if( p
1eec2 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d Page->nOverflow=
1eec3 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 6e 46 72 =0 && pPage->nFr
1eec4 65 65 3c 3d 6e 4d 69 6e 20 29 7b 0a 20 20 20 20 ee<=nMin ){.
1eec5 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 65 6c break;. }el
1eec6 73 65 7b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 se{. MemPag
1eec7 65 20 2a 20 63 6f 6e 73 74 20 70 50 61 72 65 6e e * const pParen
1eec8 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 t = pCur->apPage
1eec9 5b 69 50 61 67 65 2d 31 5d 3b 0a 20 20 20 20 20 [iPage-1];.
1eeca 20 69 6e 74 20 63 6f 6e 73 74 20 69 49 64 78 20 int const iIdx
1eecb 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 = pCur->aiIdx[iP
1eecc 61 67 65 2d 31 5d 3b 0a 0a 20 20 20 20 20 20 72 age-1];.. r
1eecd 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1eece 57 72 69 74 65 28 70 50 61 72 65 6e 74 2d 3e 70 Write(pParent->p
1eecf 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 DbPage);. i
1eed0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1eed1 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ){.#ifndef SQLI
1eed2 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c TE_OMIT_QUICKBAL
1eed3 41 4e 43 45 0a 20 20 20 20 20 20 20 20 69 66 28 ANCE. if(
1eed4 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 0a pPage->hasData.
1eed5 20 20 20 20 20 20 20 20 20 26 26 20 70 50 61 67 && pPag
1eed6 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 0a e->nOverflow==1.
1eed7 20 20 20 20 20 20 20 20 20 26 26 20 70 50 61 67 && pPag
1eed8 65 2d 3e 61 69 4f 76 66 6c 5b 30 5d 3d 3d 70 50 e->aiOvfl[0]==pP
1eed9 61 67 65 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20 20 age->nCell.
1eeda 20 20 20 20 26 26 20 70 50 61 72 65 6e 74 2d 3e && pParent->
1eedb 70 67 6e 6f 21 3d 31 0a 20 20 20 20 20 20 20 20 pgno!=1.
1eedc 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 && pParent->nCe
1eedd 6c 6c 3d 3d 69 49 64 78 0a 20 20 20 20 20 20 20 ll==iIdx.
1eede 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
1eedf 20 43 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 71 75 Call balance_qu
1eee0 69 63 6b 28 29 20 74 6f 20 63 72 65 61 74 65 20 ick() to create
1eee1 61 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 6f 66 a new sibling of
1eee2 20 70 50 61 67 65 20 6f 6e 20 77 68 69 63 68 0a pPage on which.
1eee3 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 ** to
1eee4 73 74 6f 72 65 20 74 68 65 20 6f 76 65 72 66 6c store the overfl
1eee5 6f 77 20 63 65 6c 6c 2e 20 62 61 6c 61 6e 63 65 ow cell. balance
1eee6 5f 71 75 69 63 6b 28 29 20 69 6e 73 65 72 74 73 _quick() inserts
1eee7 20 61 20 6e 65 77 20 63 65 6c 6c 0a 20 20 20 20 a new cell.
1eee8 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 70 50 ** into pP
1eee9 61 72 65 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 arent, which may
1eeea 20 63 61 75 73 65 20 70 50 61 72 65 6e 74 20 6f cause pParent o
1eeeb 76 65 72 66 6c 6f 77 2e 20 49 66 20 74 68 69 73 verflow. If this
1eeec 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61 . ** ha
1eeed 70 70 65 6e 73 2c 20 74 68 65 20 6e 65 78 74 20 ppens, the next
1eeee 69 6e 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 interation of th
1eeef 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 e do-loop will b
1eef0 61 6c 61 6e 63 65 20 70 50 61 72 65 6e 74 20 0a alance pParent .
1eef1 20 20 20 20 20 20 20 20 20 20 2a 2a 20 75 73 65 ** use
1eef2 20 65 69 74 68 65 72 20 62 61 6c 61 6e 63 65 5f either balance_
1eef3 6e 6f 6e 72 6f 6f 74 28 29 20 6f 72 20 62 61 6c nonroot() or bal
1eef4 61 6e 63 65 5f 64 65 65 70 65 72 28 29 2e 20 55 ance_deeper(). U
1eef5 6e 74 69 6c 20 74 68 69 73 0a 20 20 20 20 20 20 ntil this.
1eef6 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 73 2c 20 ** happens,
1eef7 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c the overflow cel
1eef8 6c 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 l is stored in t
1eef9 68 65 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b he aBalanceQuick
1eefa 53 70 61 63 65 5b 5d 0a 20 20 20 20 20 20 20 20 Space[].
1eefb 20 20 2a 2a 20 62 75 66 66 65 72 2e 20 0a 20 20 ** buffer. .
1eefc 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
1eefd 20 20 20 20 20 2a 2a 20 54 68 65 20 70 75 72 70 ** The purp
1eefe 6f 73 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f ose of the follo
1eeff 77 69 6e 67 20 61 73 73 65 72 74 28 29 20 69 73 wing assert() is
1ef00 20 74 6f 20 63 68 65 63 6b 20 74 68 61 74 20 6f to check that o
1ef01 6e 6c 79 20 61 0a 20 20 20 20 20 20 20 20 20 20 nly a.
1ef02 2a 2a 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20 74 ** single call t
1ef03 6f 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 o balance_quick(
1ef04 29 20 69 73 20 6d 61 64 65 20 66 6f 72 20 65 61 ) is made for ea
1ef05 63 68 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 0a ch call to this.
1ef06 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e ** fun
1ef07 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 77 ction. If this w
1ef08 65 72 65 20 6e 6f 74 20 76 65 72 69 66 69 65 64 ere not verified
1ef09 2c 20 61 20 73 75 62 74 6c 65 20 62 75 67 20 69 , a subtle bug i
1ef0a 6e 76 6f 6c 76 69 6e 67 20 72 65 75 73 65 0a 20 nvolving reuse.
1ef0b 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 ** of t
1ef0c 68 65 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b he aBalanceQuick
1ef0d 53 70 61 63 65 5b 5d 20 6d 69 67 68 74 20 73 6e Space[] might sn
1ef0e 65 61 6b 20 69 6e 2e 0a 20 20 20 20 20 20 20 20 eak in..
1ef0f 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 */. a
1ef10 73 73 65 72 74 28 20 28 62 61 6c 61 6e 63 65 5f ssert( (balance_
1ef11 71 75 69 63 6b 5f 63 61 6c 6c 65 64 2b 2b 29 3d quick_called++)=
1ef12 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 =0 );.
1ef13 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 71 75 69 rc = balance_qui
1ef14 63 6b 28 70 50 61 72 65 6e 74 2c 20 70 50 61 67 ck(pParent, pPag
1ef15 65 2c 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b e, aBalanceQuick
1ef16 53 70 61 63 65 29 3b 0a 20 20 20 20 20 20 20 20 Space);.
1ef17 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 }else.#endif.
1ef18 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 {.
1ef19 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 /* In this case
1ef1a 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 6e , call balance_n
1ef1b 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 72 65 64 69 onroot() to redi
1ef1c 73 74 72 69 62 75 74 65 20 63 65 6c 6c 73 0a 20 stribute cells.
1ef1d 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 74 77 ** betw
1ef1e 65 65 6e 20 70 50 61 67 65 20 61 6e 64 20 75 70 een pPage and up
1ef1f 20 74 6f 20 32 20 6f 66 20 69 74 73 20 73 69 62 to 2 of its sib
1ef20 6c 69 6e 67 20 70 61 67 65 73 2e 20 54 68 69 73 ling pages. This
1ef21 20 69 6e 76 6f 6c 76 65 73 0a 20 20 20 20 20 20 involves.
1ef22 20 20 20 20 2a 2a 20 6d 6f 64 69 66 79 69 6e 67 ** modifying
1ef23 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
1ef24 20 70 50 61 72 65 6e 74 2c 20 77 68 69 63 68 20 pParent, which
1ef25 6d 61 79 20 63 61 75 73 65 20 70 50 61 72 65 6e may cause pParen
1ef26 74 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a t to. *
1ef27 2a 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c * become overful
1ef28 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 l or underfull.
1ef29 54 68 65 20 6e 65 78 74 20 69 74 65 72 61 74 69 The next iterati
1ef2a 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f on of the do-loo
1ef2b 70 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 p. ** w
1ef2c 69 6c 6c 20 62 61 6c 61 6e 63 65 20 74 68 65 20 ill balance the
1ef2d 70 61 72 65 6e 74 20 70 61 67 65 20 74 6f 20 63 parent page to c
1ef2e 6f 72 72 65 63 74 20 74 68 69 73 2e 0a 20 20 20 orrect this..
1ef2f 20 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 ** .
1ef30 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70 ** If the p
1ef31 61 72 65 6e 74 20 70 61 67 65 20 62 65 63 6f 6d arent page becom
1ef32 65 73 20 6f 76 65 72 66 75 6c 6c 2c 20 74 68 65 es overfull, the
1ef33 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 6f overflow cell o
1ef34 72 20 63 65 6c 6c 73 0a 20 20 20 20 20 20 20 20 r cells.
1ef35 20 20 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 20 ** are stored
1ef36 69 6e 20 74 68 65 20 70 53 70 61 63 65 20 62 75 in the pSpace bu
1ef37 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 69 ffer allocated i
1ef38 6d 6d 65 64 69 61 74 65 6c 79 20 62 65 6c 6f 77 mmediately below
1ef39 2e 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . . **
1ef3a 41 20 73 75 62 73 65 71 75 65 6e 74 20 69 74 65 A subsequent ite
1ef3b 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f ration of the do
1ef3c 2d 6c 6f 6f 70 20 77 69 6c 6c 20 64 65 61 6c 20 -loop will deal
1ef3d 77 69 74 68 20 74 68 69 73 20 62 79 0a 20 20 20 with this by.
1ef3e 20 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 69 6e ** callin
1ef3f 67 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f g balance_nonroo
1ef40 74 28 29 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 t() (balance_dee
1ef41 70 65 72 28 29 20 6d 61 79 20 62 65 20 63 61 6c per() may be cal
1ef42 6c 65 64 20 66 69 72 73 74 2c 0a 20 20 20 20 20 led first,.
1ef43 20 20 20 20 20 2a 2a 20 62 75 74 20 69 74 20 64 ** but it d
1ef44 6f 65 73 6e 27 74 20 64 65 61 6c 20 77 69 74 68 oesn't deal with
1ef45 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 overflow cells
1ef46 2d 20 6a 75 73 74 20 6d 6f 76 65 73 20 74 68 65 - just moves the
1ef47 6d 20 74 6f 20 61 0a 20 20 20 20 20 20 20 20 20 m to a.
1ef48 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 70 61 ** different pa
1ef49 67 65 29 2e 20 4f 6e 63 65 20 74 68 69 73 20 73 ge). Once this s
1ef4a 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 ubsequent call t
1ef4b 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f o balance_nonroo
1ef4c 74 28 29 20 0a 20 20 20 20 20 20 20 20 20 20 2a t() . *
1ef4d 2a 20 68 61 73 20 63 6f 6d 70 6c 65 74 65 64 2c * has completed,
1ef4e 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 72 it is safe to r
1ef4f 65 6c 65 61 73 65 20 74 68 65 20 70 53 70 61 63 elease the pSpac
1ef50 65 20 62 75 66 66 65 72 20 75 73 65 64 20 62 79 e buffer used by
1ef51 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 . ** th
1ef52 65 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2c e previous call,
1ef53 20 61 73 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 as the overflow
1ef54 20 63 65 6c 6c 20 64 61 74 61 20 77 69 6c 6c 20 cell data will
1ef55 68 61 76 65 20 62 65 65 6e 20 0a 20 20 20 20 20 have been .
1ef56 20 20 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 65 ** copied e
1ef57 69 74 68 65 72 20 69 6e 74 6f 20 74 68 65 20 62 ither into the b
1ef58 6f 64 79 20 6f 66 20 61 20 64 61 74 61 62 61 73 ody of a databas
1ef59 65 20 70 61 67 65 20 6f 72 20 69 6e 74 6f 20 74 e page or into t
1ef5a 68 65 20 6e 65 77 0a 20 20 20 20 20 20 20 20 20 he new.
1ef5b 20 2a 2a 20 70 53 70 61 63 65 20 62 75 66 66 65 ** pSpace buffe
1ef5c 72 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 r passed to the
1ef5d 6c 61 74 74 65 72 20 63 61 6c 6c 20 74 6f 20 62 latter call to b
1ef5e 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 alance_nonroot()
1ef5f 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 .. */.
1ef60 20 20 20 20 20 20 20 20 20 75 38 20 2a 70 53 70 u8 *pSp
1ef61 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 ace = sqlite3Pag
1ef62 65 4d 61 6c 6c 6f 63 28 70 43 75 72 2d 3e 70 42 eMalloc(pCur->pB
1ef63 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 t->pageSize);.
1ef64 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c rc = bal
1ef65 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 50 61 ance_nonroot(pPa
1ef66 72 65 6e 74 2c 20 69 49 64 78 2c 20 70 53 70 61 rent, iIdx, pSpa
1ef67 63 65 2c 20 69 50 61 67 65 3d 3d 31 2c 20 70 43 ce, iPage==1, pC
1ef68 75 72 2d 3e 68 69 6e 74 73 29 3b 0a 20 20 20 20 ur->hints);.
1ef69 20 20 20 20 20 20 69 66 28 20 70 46 72 65 65 20 if( pFree
1ef6a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f ){. /
1ef6b 2a 20 49 66 20 70 46 72 65 65 20 69 73 20 6e 6f * If pFree is no
1ef6c 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 t NULL, it point
1ef6d 73 20 74 6f 20 74 68 65 20 70 53 70 61 63 65 20 s to the pSpace
1ef6e 62 75 66 66 65 72 20 75 73 65 64 20 0a 20 20 20 buffer used .
1ef6f 20 20 20 20 20 20 20 20 20 2a 2a 20 62 79 20 61 ** by a
1ef70 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 previous call t
1ef71 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f o balance_nonroo
1ef72 74 28 29 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 t(). Its content
1ef73 73 20 61 72 65 0a 20 20 20 20 20 20 20 20 20 20 s are.
1ef74 20 20 2a 2a 20 6e 6f 77 20 73 74 6f 72 65 64 20 ** now stored
1ef75 65 69 74 68 65 72 20 6f 6e 20 72 65 61 6c 20 64 either on real d
1ef76 61 74 61 62 61 73 65 20 70 61 67 65 73 20 6f 72 atabase pages or
1ef77 20 77 69 74 68 69 6e 20 74 68 65 20 0a 20 20 20 within the .
1ef78 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 77 20 ** new
1ef79 70 53 70 61 63 65 20 62 75 66 66 65 72 2c 20 73 pSpace buffer, s
1ef7a 6f 20 69 74 20 6d 61 79 20 62 65 20 73 61 66 65 o it may be safe
1ef7b 6c 79 20 66 72 65 65 64 20 68 65 72 65 2e 20 2a ly freed here. *
1ef7c 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 /. sq
1ef7d 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 46 lite3PageFree(pF
1ef7e 72 65 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ree);.
1ef7f 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 }.. /*
1ef80 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 The pSpace buffe
1ef81 72 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 r will be freed
1ef82 61 66 74 65 72 20 74 68 65 20 6e 65 78 74 20 63 after the next c
1ef83 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 20 20 20 all to.
1ef84 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 ** balance_nonr
1ef85 6f 6f 74 28 29 2c 20 6f 72 20 6a 75 73 74 20 62 oot(), or just b
1ef86 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 efore this funct
1ef87 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 77 68 69 ion returns, whi
1ef88 63 68 65 76 65 72 0a 20 20 20 20 20 20 20 20 20 chever.
1ef89 20 2a 2a 20 63 6f 6d 65 73 20 66 69 72 73 74 2e ** comes first.
1ef8a 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 46 */. pF
1ef8b 72 65 65 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 ree = pSpace;.
1ef8c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1ef8d 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f . pPage->nO
1ef8e 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 verflow = 0;..
1ef8f 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 /* The next
1ef90 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 iteration of the
1ef91 20 64 6f 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63 65 do-loop balance
1ef92 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 s the parent pag
1ef93 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 e. */. rele
1ef94 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
1ef95 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 pCur->iPag
1ef96 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68 e--;. }. }wh
1ef97 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ile( rc==SQLITE_
1ef98 4f 4b 20 29 3b 0a 0a 20 20 69 66 28 20 70 46 72 OK );.. if( pFr
1ef99 65 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ee ){. sqlite
1ef9a 33 50 61 67 65 46 72 65 65 28 70 46 72 65 65 29 3PageFree(pFree)
1ef9b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1ef9c 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 c;.}.../*.** Ins
1ef9d 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64 ert a new record
1ef9e 20 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e into the BTree.
1ef9f 20 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 76 The key is giv
1efa0 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 en by (pKey,nKey
1efa1 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 ).** and the dat
1efa2 61 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 a is given by (p
1efa3 44 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 Data,nData). Th
1efa4 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 e cursor is used
1efa5 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 only to.** defi
1efa6 6e 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 68 ne what table th
1efa7 65 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 e record should
1efa8 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f be inserted into
1efa9 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a . The cursor.**
1efaa 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
1efab 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f g at a random lo
1efac 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f cation..**.** Fo
1efad 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c r an INTKEY tabl
1efae 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 e, only the nKey
1efaf 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 value of the ke
1efb0 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79 y is used. pKey
1efb1 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 is.** ignored.
1efb2 20 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20 For a ZERODATA
1efb3 74 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61 table, the pData
1efb4 20 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62 and nData are b
1efb5 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a oth ignored..**.
1efb6 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52 65 ** If the seekRe
1efb7 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69 sult parameter i
1efb8 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
1efb9 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 a successful ca
1efba 6c 6c 20 74 6f 0a 2a 2a 20 4d 6f 76 65 74 6f 55 ll to.** MovetoU
1efbb 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 73 65 65 npacked() to see
1efbc 6b 20 63 75 72 73 6f 72 20 70 43 75 72 20 74 6f k cursor pCur to
1efbd 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 68 61 (pKey, nKey) ha
1efbe 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 s already.** bee
1efbf 6e 20 70 65 72 66 6f 72 6d 65 64 2e 20 73 65 65 n performed. see
1efc0 6b 52 65 73 75 6c 74 20 69 73 20 74 68 65 20 73 kResult is the s
1efc1 65 61 72 63 68 20 72 65 73 75 6c 74 20 72 65 74 earch result ret
1efc2 75 72 6e 65 64 20 28 61 20 6e 65 67 61 74 69 76 urned (a negativ
1efc3 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 66 20 70 e.** number if p
1efc4 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e Cur points at an
1efc5 20 65 6e 74 72 79 20 74 68 61 74 20 69 73 20 73 entry that is s
1efc6 6d 61 6c 6c 65 72 20 74 68 61 6e 20 28 70 4b 65 maller than (pKe
1efc7 79 2c 20 6e 4b 65 79 29 2c 20 6f 72 0a 2a 2a 20 y, nKey), or.**
1efc8 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65 a positive value
1efc9 20 69 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20 if pCur points
1efca 61 74 20 61 6e 20 65 74 72 79 20 74 68 61 74 20 at an etry that
1efcb 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a is larger than .
1efcc 2a 2a 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 29 ** (pKey, nKey))
1efcd 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 . .**.** If the
1efce 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d seekResult param
1efcf 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f eter is non-zero
1efd0 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 , then the calle
1efd1 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 r guarantees tha
1efd2 74 0a 2a 2a 20 63 75 72 73 6f 72 20 70 43 75 72 t.** cursor pCur
1efd3 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 is pointing at
1efd4 74 68 65 20 65 78 69 73 74 69 6e 67 20 63 6f 70 the existing cop
1efd5 79 20 6f 66 20 61 20 72 6f 77 20 74 68 61 74 20 y of a row that
1efd6 69 73 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 is to be.** over
1efd7 77 72 69 74 74 65 6e 2e 20 20 49 66 20 74 68 65 written. If the
1efd8 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 seekResult para
1efd9 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e meter is 0, then
1efda 20 63 75 72 73 6f 72 20 70 43 75 72 20 6d 61 79 cursor pCur may
1efdb 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79 .** point to any
1efdc 20 65 6e 74 72 79 20 6f 72 20 74 6f 20 6e 6f 20 entry or to no
1efdd 65 6e 74 72 79 20 61 74 20 61 6c 6c 20 61 6e 64 entry at all and
1efde 20 73 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f so this functio
1efdf 6e 20 68 61 73 20 74 6f 20 73 65 65 6b 0a 2a 2a n has to seek.**
1efe0 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 66 6f the cursor befo
1efe1 72 65 20 74 68 65 20 6e 65 77 20 6b 65 79 20 63 re the new key c
1efe2 61 6e 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a an be inserted..
1efe3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1efe4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1efe5 65 65 49 6e 73 65 72 74 28 0a 20 20 42 74 43 75 eeInsert(. BtCu
1efe6 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 rsor *pCur,
1efe7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
1efe8 73 65 72 74 20 64 61 74 61 20 69 6e 74 6f 20 74 sert data into t
1efe9 68 65 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 he table of this
1efea 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e cursor */. con
1efeb 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 st void *pKey, i
1efec 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 64 nKey, /* T
1efed 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 6e 65 he key of the ne
1efee 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f w record */. co
1efef 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c nst void *pData,
1eff0 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20 int nData, /*
1eff1 54 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 20 The data of the
1eff2 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 new record */.
1eff3 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 int nZero,
1eff4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1eff5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72 * Number of extr
1eff6 61 20 30 20 62 79 74 65 73 20 74 6f 20 61 70 70 a 0 bytes to app
1eff7 65 6e 64 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20 end to data */.
1eff8 20 69 6e 74 20 61 70 70 65 6e 64 42 69 61 73 2c int appendBias,
1eff9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1effa 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
1effb 69 73 20 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70 is likely an app
1effc 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65 end */. int see
1effd 6b 52 65 73 75 6c 74 20 20 20 20 20 20 20 20 20 kResult
1effe 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c /* Resul
1efff 74 20 6f 66 20 70 72 69 6f 72 20 4d 6f 76 65 74 t of prior Movet
1f000 6f 55 6e 70 61 63 6b 65 64 28 29 20 63 61 6c 6c oUnpacked() call
1f001 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
1f002 0a 20 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65 . int loc = see
1f003 6b 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 kResult;
1f004 20 20 2f 2a 20 2d 31 3a 20 62 65 66 6f 72 65 20 /* -1: before
1f005 64 65 73 69 72 65 64 20 6c 6f 63 61 74 69 6f 6e desired location
1f006 20 20 2b 31 3a 20 61 66 74 65 72 20 2a 2f 0a 20 +1: after */.
1f007 20 69 6e 74 20 73 7a 4e 65 77 20 3d 20 30 3b 0a int szNew = 0;.
1f008 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d int idx;. Mem
1f009 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 42 Page *pPage;. B
1f00a 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e tree *p = pCur->
1f00b 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 pBtree;. BtShar
1f00c 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
1f00d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ;. unsigned cha
1f00e 72 20 2a 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e r *oldCell;. un
1f00f 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6e 65 77 signed char *new
1f010 43 65 6c 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 Cell = 0;.. if(
1f011 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
1f012 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 URSOR_FAULT ){.
1f013 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
1f014 3e 73 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 >skipNext!=SQLIT
1f015 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 65 74 75 E_OK );. retu
1f016 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 rn pCur->skipNex
1f017 74 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 t;. }.. assert
1f018 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
1f019 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
1f01a 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c sert( pCur->wrFl
1f01b 61 67 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 ag && pBt->inTra
1f01c 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
1f01d 57 52 49 54 45 0a 20 20 20 20 20 20 20 20 20 20 WRITE.
1f01e 20 20 20 20 26 26 20 28 70 42 74 2d 3e 62 74 73 && (pBt->bts
1f01f 46 6c 61 67 73 20 26 20 42 54 53 5f 52 45 41 44 Flags & BTS_READ
1f020 5f 4f 4e 4c 59 29 3d 3d 30 20 29 3b 0a 20 20 61 _ONLY)==0 );. a
1f021 73 73 65 72 74 28 20 68 61 73 53 68 61 72 65 64 ssert( hasShared
1f022 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 CacheTableLock(p
1f023 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 , pCur->pgnoRoot
1f024 2c 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f , pCur->pKeyInfo
1f025 21 3d 30 2c 20 32 29 20 29 3b 0a 0a 20 20 2f 2a !=0, 2) );.. /*
1f026 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 Assert that the
1f027 20 63 61 6c 6c 65 72 20 68 61 73 20 62 65 65 6e caller has been
1f028 20 63 6f 6e 73 69 73 74 65 6e 74 2e 20 49 66 20 consistent. If
1f029 74 68 69 73 20 63 75 72 73 6f 72 20 77 61 73 20 this cursor was
1f02a 6f 70 65 6e 65 64 0a 20 20 2a 2a 20 65 78 70 65 opened. ** expe
1f02b 63 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 62 cting an index b
1f02c 2d 74 72 65 65 2c 20 74 68 65 6e 20 74 68 65 20 -tree, then the
1f02d 63 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65 caller should be
1f02e 20 69 6e 73 65 72 74 69 6e 67 20 62 6c 6f 62 0a inserting blob.
1f02f 20 20 2a 2a 20 6b 65 79 73 20 77 69 74 68 20 6e ** keys with n
1f030 6f 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 o associated dat
1f031 61 2e 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 a. If the cursor
1f032 20 77 61 73 20 6f 70 65 6e 65 64 20 65 78 70 65 was opened expe
1f033 63 74 69 6e 67 20 61 6e 0a 20 20 2a 2a 20 69 6e cting an. ** in
1f034 74 6b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 20 tkey table, the
1f035 63 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65 caller should be
1f036 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 65 67 inserting integ
1f037 65 72 20 6b 65 79 73 20 77 69 74 68 20 61 0a 20 er keys with a.
1f038 20 2a 2a 20 62 6c 6f 62 20 6f 66 20 61 73 73 6f ** blob of asso
1f039 63 69 61 74 65 64 20 64 61 74 61 2e 20 20 2a 2f ciated data. */
1f03a 0a 20 20 61 73 73 65 72 74 28 20 28 70 4b 65 79 . assert( (pKey
1f03b 3d 3d 30 29 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 ==0)==(pCur->pKe
1f03c 79 49 6e 66 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20 yInfo==0) );..
1f03d 2f 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 /* Save the posi
1f03e 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 tions of any oth
1f03f 65 72 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 er cursors open
1f040 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 0a 20 on this table..
1f041 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 73 6f 6d 65 **. ** In some
1f042 20 63 61 73 65 73 2c 20 74 68 65 20 63 61 6c 6c cases, the call
1f043 20 74 6f 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 to btreeMoveto(
1f044 29 20 62 65 6c 6f 77 20 69 73 20 61 20 6e 6f 2d ) below is a no-
1f045 6f 70 2e 20 46 6f 72 0a 20 20 2a 2a 20 65 78 61 op. For. ** exa
1f046 6d 70 6c 65 2c 20 77 68 65 6e 20 69 6e 73 65 72 mple, when inser
1f047 74 69 6e 67 20 64 61 74 61 20 69 6e 74 6f 20 61 ting data into a
1f048 20 74 61 62 6c 65 20 77 69 74 68 20 61 75 74 6f table with auto
1f049 2d 67 65 6e 65 72 61 74 65 64 20 69 6e 74 65 67 -generated integ
1f04a 65 72 0a 20 20 2a 2a 20 6b 65 79 73 2c 20 74 68 er. ** keys, th
1f04b 65 20 56 44 42 45 20 6c 61 79 65 72 20 69 6e 76 e VDBE layer inv
1f04c 6f 6b 65 73 20 73 71 6c 69 74 65 33 42 74 72 65 okes sqlite3Btre
1f04d 65 4c 61 73 74 28 29 20 74 6f 20 66 69 67 75 72 eLast() to figur
1f04e 65 20 6f 75 74 20 74 68 65 20 0a 20 20 2a 2a 20 e out the . **
1f04f 69 6e 74 65 67 65 72 20 6b 65 79 20 74 6f 20 75 integer key to u
1f050 73 65 2e 20 49 74 20 74 68 65 6e 20 63 61 6c 6c se. It then call
1f051 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 s this function
1f052 74 6f 20 61 63 74 75 61 6c 6c 79 20 69 6e 73 65 to actually inse
1f053 72 74 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 rt the . ** dat
1f054 61 20 69 6e 74 6f 20 74 68 65 20 69 6e 74 6b 65 a into the intke
1f055 79 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 68 69 y B-Tree. In thi
1f056 73 20 63 61 73 65 20 62 74 72 65 65 4d 6f 76 65 s case btreeMove
1f057 74 6f 28 29 20 72 65 63 6f 67 6e 69 7a 65 73 0a to() recognizes.
1f058 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63 75 ** that the cu
1f059 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 rsor is already
1f05a 77 68 65 72 65 20 69 74 20 6e 65 65 64 73 20 74 where it needs t
1f05b 6f 20 62 65 20 61 6e 64 20 72 65 74 75 72 6e 73 o be and returns
1f05c 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 64 6f without. ** do
1f05d 69 6e 67 20 61 6e 79 20 77 6f 72 6b 2e 20 54 6f ing any work. To
1f05e 20 61 76 6f 69 64 20 74 68 77 61 72 74 69 6e 67 avoid thwarting
1f05f 20 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 these optimizat
1f060 69 6f 6e 73 2c 20 69 74 20 69 73 20 69 6d 70 6f ions, it is impo
1f061 72 74 61 6e 74 0a 20 20 2a 2a 20 6e 6f 74 20 74 rtant. ** not t
1f062 6f 20 63 6c 65 61 72 20 74 68 65 20 63 75 72 73 o clear the curs
1f063 6f 72 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 or here.. */.
1f064 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 rc = saveAllCurs
1f065 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 ors(pBt, pCur->p
1f066 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 3b 0a gnoRoot, pCur);.
1f067 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
1f068 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 n rc;.. /* If t
1f069 68 69 73 20 69 73 20 61 6e 20 69 6e 73 65 72 74 his is an insert
1f06a 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 62 2d into a table b-
1f06b 74 72 65 65 2c 20 69 6e 76 61 6c 69 64 61 74 65 tree, invalidate
1f06c 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 0a 20 any incrblob .
1f06d 20 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 6e ** cursors open
1f06e 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e on the row bein
1f06f 67 20 72 65 70 6c 61 63 65 64 20 28 61 73 73 75 g replaced (assu
1f070 6d 69 6e 67 20 74 68 69 73 20 69 73 20 61 20 72 ming this is a r
1f071 65 70 6c 61 63 65 0a 20 20 2a 2a 20 6f 70 65 72 eplace. ** oper
1f072 61 74 69 6f 6e 20 2d 20 69 66 20 69 74 20 69 73 ation - if it is
1f073 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 not, the follow
1f074 69 6e 67 20 69 73 20 61 20 6e 6f 2d 6f 70 29 2e ing is a no-op).
1f075 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d */. if( pCur-
1f076 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29 7b 0a >pKeyInfo==0 ){.
1f077 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 49 6e invalidateIn
1f078 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c crblobCursors(p,
1f079 20 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a nKey, 0);. }..
1f07a 20 20 69 66 28 20 21 6c 6f 63 20 29 7b 0a 20 20 if( !loc ){.
1f07b 20 20 72 63 20 3d 20 62 74 72 65 65 4d 6f 76 65 rc = btreeMove
1f07c 74 6f 28 70 43 75 72 2c 20 70 4b 65 79 2c 20 6e to(pCur, pKey, n
1f07d 4b 65 79 2c 20 61 70 70 65 6e 64 42 69 61 73 2c Key, appendBias,
1f07e 20 26 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 &loc);. if(
1f07f 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
1f080 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
1f081 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
1f082 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 28 70 43 75 OR_VALID || (pCu
1f083 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
1f084 52 5f 49 4e 56 41 4c 49 44 20 26 26 20 6c 6f 63 R_INVALID && loc
1f085 29 20 29 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20 ) );.. pPage =
1f086 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
1f087 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 r->iPage];. ass
1f088 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b ert( pPage->intK
1f089 65 79 20 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b ey || nKey>=0 );
1f08a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
1f08b 2d 3e 6c 65 61 66 20 7c 7c 20 21 70 50 61 67 65 ->leaf || !pPage
1f08c 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 54 ->intKey );.. T
1f08d 52 41 43 45 28 28 22 49 4e 53 45 52 54 3a 20 74 RACE(("INSERT: t
1f08e 61 62 6c 65 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c able=%d nkey=%ll
1f08f 64 20 6e 64 61 74 61 3d 25 64 20 70 61 67 65 3d d ndata=%d page=
1f090 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 %d %s\n",.
1f091 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f pCur->pgnoRo
1f092 6f 74 2c 20 6e 4b 65 79 2c 20 6e 44 61 74 61 2c ot, nKey, nData,
1f093 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20 pPage->pgno,.
1f094 20 20 20 20 20 20 20 20 6c 6f 63 3d 3d 30 20 3f loc==0 ?
1f095 20 22 6f 76 65 72 77 72 69 74 65 22 20 3a 20 22 "overwrite" : "
1f096 6e 65 77 20 65 6e 74 72 79 22 29 29 3b 0a 20 20 new entry"));.
1f097 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 assert( pPage->i
1f098 73 49 6e 69 74 20 29 3b 0a 20 20 61 6c 6c 6f 63 sInit );. alloc
1f099 61 74 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 ateTempSpace(pBt
1f09a 29 3b 0a 20 20 6e 65 77 43 65 6c 6c 20 3d 20 70 );. newCell = p
1f09b 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 Bt->pTmpSpace;.
1f09c 20 69 66 28 20 6e 65 77 43 65 6c 6c 3d 3d 30 20 if( newCell==0
1f09d 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
1f09e 4e 4f 4d 45 4d 3b 0a 20 20 72 63 20 3d 20 66 69 NOMEM;. rc = fi
1f09f 6c 6c 49 6e 43 65 6c 6c 28 70 50 61 67 65 2c 20 llInCell(pPage,
1f0a0 6e 65 77 43 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e newCell, pKey, n
1f0a1 4b 65 79 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 Key, pData, nDat
1f0a2 61 2c 20 6e 5a 65 72 6f 2c 20 26 73 7a 4e 65 77 a, nZero, &szNew
1f0a3 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f );. if( rc ) go
1f0a4 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 to end_insert;.
1f0a5 20 61 73 73 65 72 74 28 20 73 7a 4e 65 77 3d 3d assert( szNew==
1f0a6 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 cellSizePtr(pPag
1f0a7 65 2c 20 6e 65 77 43 65 6c 6c 29 20 29 3b 0a 20 e, newCell) );.
1f0a8 20 61 73 73 65 72 74 28 20 73 7a 4e 65 77 20 3c assert( szNew <
1f0a9 3d 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 = MX_CELL_SIZE(p
1f0aa 42 74 29 20 29 3b 0a 20 20 69 64 78 20 3d 20 70 Bt) );. idx = p
1f0ab 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
1f0ac 3e 69 50 61 67 65 5d 3b 0a 20 20 69 66 28 20 6c >iPage];. if( l
1f0ad 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20 75 31 36 oc==0 ){. u16
1f0ae 20 73 7a 4f 6c 64 3b 0a 20 20 20 20 61 73 73 65 szOld;. asse
1f0af 72 74 28 20 69 64 78 3c 70 50 61 67 65 2d 3e 6e rt( idx<pPage->n
1f0b0 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d Cell );. rc =
1f0b1 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
1f0b2 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 te(pPage->pDbPag
1f0b3 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 e);. if( rc )
1f0b4 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 {. goto end
1f0b5 5f 69 6e 73 65 72 74 3b 0a 20 20 20 20 7d 0a 20 _insert;. }.
1f0b6 20 20 20 6f 6c 64 43 65 6c 6c 20 3d 20 66 69 6e oldCell = fin
1f0b7 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 dCell(pPage, idx
1f0b8 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 );. if( !pPag
1f0b9 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 e->leaf ){.
1f0ba 20 6d 65 6d 63 70 79 28 6e 65 77 43 65 6c 6c 2c memcpy(newCell,
1f0bb 20 6f 6c 64 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 oldCell, 4);.
1f0bc 20 20 7d 0a 20 20 20 20 73 7a 4f 6c 64 20 3d 20 }. szOld =
1f0bd 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 cellSizePtr(pPag
1f0be 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 e, oldCell);.
1f0bf 20 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 rc = clearCell(
1f0c0 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b pPage, oldCell);
1f0c1 0a 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 . dropCell(pP
1f0c2 61 67 65 2c 20 69 64 78 2c 20 73 7a 4f 6c 64 2c age, idx, szOld,
1f0c3 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72 &rc);. if( r
1f0c4 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 c ) goto end_ins
1f0c5 65 72 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ert;. }else if(
1f0c6 20 6c 6f 63 3c 30 20 26 26 20 70 50 61 67 65 2d loc<0 && pPage-
1f0c7 3e 6e 43 65 6c 6c 3e 30 20 29 7b 0a 20 20 20 20 >nCell>0 ){.
1f0c8 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c assert( pPage->l
1f0c9 65 61 66 20 29 3b 0a 20 20 20 20 69 64 78 20 3d eaf );. idx =
1f0ca 20 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 ++pCur->aiIdx[p
1f0cb 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 7d Cur->iPage];. }
1f0cc 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
1f0cd 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b ( pPage->leaf );
1f0ce 0a 20 20 7d 0a 20 20 69 6e 73 65 72 74 43 65 6c . }. insertCel
1f0cf 6c 28 70 50 61 67 65 2c 20 69 64 78 2c 20 6e 65 l(pPage, idx, ne
1f0d0 77 43 65 6c 6c 2c 20 73 7a 4e 65 77 2c 20 30 2c wCell, szNew, 0,
1f0d1 20 30 2c 20 26 72 63 29 3b 0a 20 20 61 73 73 65 0, &rc);. asse
1f0d2 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f rt( rc!=SQLITE_O
1f0d3 4b 20 7c 7c 20 70 50 61 67 65 2d 3e 6e 43 65 6c K || pPage->nCel
1f0d4 6c 3e 30 20 7c 7c 20 70 50 61 67 65 2d 3e 6e 4f l>0 || pPage->nO
1f0d5 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20 verflow>0 );..
1f0d6 2f 2a 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 68 /* If no error h
1f0d7 61 73 20 6f 63 63 75 72 65 64 20 61 6e 64 20 70 as occured and p
1f0d8 50 61 67 65 20 68 61 73 20 61 6e 20 6f 76 65 72 Page has an over
1f0d9 66 6c 6f 77 20 63 65 6c 6c 2c 20 63 61 6c 6c 20 flow cell, call
1f0da 62 61 6c 61 6e 63 65 28 29 20 0a 20 20 2a 2a 20 balance() . **
1f0db 74 6f 20 72 65 64 69 73 74 72 69 62 75 74 65 20 to redistribute
1f0dc 74 68 65 20 63 65 6c 6c 73 20 77 69 74 68 69 6e the cells within
1f0dd 20 74 68 65 20 74 72 65 65 2e 20 53 69 6e 63 65 the tree. Since
1f0de 20 62 61 6c 61 6e 63 65 28 29 20 6d 61 79 20 6d balance() may m
1f0df 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 ove. ** the cur
1f0e0 73 6f 72 2c 20 7a 65 72 6f 20 74 68 65 20 42 74 sor, zero the Bt
1f0e1 43 75 72 73 6f 72 2e 69 6e 66 6f 2e 6e 53 69 7a Cursor.info.nSiz
1f0e2 65 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 76 e and BtCursor.v
1f0e3 61 6c 69 64 4e 4b 65 79 0a 20 20 2a 2a 20 76 61 alidNKey. ** va
1f0e4 72 69 61 62 6c 65 73 2e 0a 20 20 2a 2a 0a 20 20 riables.. **.
1f0e5 2a 2a 20 50 72 65 76 69 6f 75 73 20 76 65 72 73 ** Previous vers
1f0e6 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 63 ions of SQLite c
1f0e7 61 6c 6c 65 64 20 6d 6f 76 65 54 6f 52 6f 6f 74 alled moveToRoot
1f0e8 28 29 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 63 () to move the c
1f0e9 75 72 73 6f 72 0a 20 20 2a 2a 20 62 61 63 6b 20 ursor. ** back
1f0ea 74 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 to the root page
1f0eb 20 61 73 20 62 61 6c 61 6e 63 65 28 29 20 75 73 as balance() us
1f0ec 65 64 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 ed to invalidate
1f0ed 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20 the contents.
1f0ee 2a 2a 20 6f 66 20 42 74 43 75 72 73 6f 72 2e 61 ** of BtCursor.a
1f0ef 70 50 61 67 65 5b 5d 20 61 6e 64 20 42 74 43 75 pPage[] and BtCu
1f0f0 72 73 6f 72 2e 61 69 49 64 78 5b 5d 2e 20 49 6e rsor.aiIdx[]. In
1f0f1 73 74 65 61 64 20 6f 66 20 64 6f 69 6e 67 20 74 stead of doing t
1f0f2 68 61 74 2c 0a 20 20 2a 2a 20 73 65 74 20 74 68 hat,. ** set th
1f0f3 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 20 74 e cursor state t
1f0f4 6f 20 22 69 6e 76 61 6c 69 64 22 2e 20 54 68 69 o "invalid". Thi
1f0f5 73 20 6d 61 6b 65 73 20 63 6f 6d 6d 6f 6e 20 69 s makes common i
1f0f6 6e 73 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 nsert operations
1f0f7 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 66 . ** slightly f
1f0f8 61 73 74 65 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a aster.. **. **
1f0f9 20 54 68 65 72 65 20 69 73 20 61 20 73 75 62 74 There is a subt
1f0fa 6c 65 20 62 75 74 20 69 6d 70 6f 72 74 61 6e 74 le but important
1f0fb 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 68 65 optimization he
1f0fc 72 65 20 74 6f 6f 2e 20 57 68 65 6e 20 69 6e 73 re too. When ins
1f0fd 65 72 74 69 6e 67 0a 20 20 2a 2a 20 6d 75 6c 74 erting. ** mult
1f0fe 69 70 6c 65 20 72 65 63 6f 72 64 73 20 69 6e 74 iple records int
1f0ff 6f 20 61 6e 20 69 6e 74 6b 65 79 20 62 2d 74 72 o an intkey b-tr
1f100 65 65 20 75 73 69 6e 67 20 61 20 73 69 6e 67 6c ee using a singl
1f101 65 20 63 75 72 73 6f 72 20 28 61 73 20 63 61 6e e cursor (as can
1f102 0a 20 20 2a 2a 20 68 61 70 70 65 6e 20 77 68 69 . ** happen whi
1f103 6c 65 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e le processing an
1f104 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e "INSERT INTO ..
1f105 2e 20 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d . SELECT" statem
1f106 65 6e 74 29 2c 20 69 74 0a 20 20 2a 2a 20 69 73 ent), it. ** is
1f107 20 61 64 76 61 6e 74 61 67 65 6f 75 73 20 74 6f advantageous to
1f108 20 6c 65 61 76 65 20 74 68 65 20 63 75 72 73 6f leave the curso
1f109 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 r pointing to th
1f10a 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 0a e last entry in.
1f10b 20 20 2a 2a 20 74 68 65 20 62 2d 74 72 65 65 20 ** the b-tree
1f10c 69 66 20 70 6f 73 73 69 62 6c 65 2e 20 49 66 20 if possible. If
1f10d 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 the cursor is le
1f10e 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 ft pointing to t
1f10f 68 65 20 6c 61 73 74 0a 20 20 2a 2a 20 65 6e 74 he last. ** ent
1f110 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c ry in the table,
1f111 20 61 6e 64 20 74 68 65 20 6e 65 78 74 20 72 6f and the next ro
1f112 77 20 69 6e 73 65 72 74 65 64 20 68 61 73 20 61 w inserted has a
1f113 6e 20 69 6e 74 65 67 65 72 20 6b 65 79 0a 20 20 n integer key.
1f114 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 ** larger than t
1f115 68 65 20 6c 61 72 67 65 73 74 20 65 78 69 73 74 he largest exist
1f116 69 6e 67 20 6b 65 79 2c 20 69 74 20 69 73 20 70 ing key, it is p
1f117 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e 73 65 72 ossible to inser
1f118 74 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 77 t the. ** row w
1f119 69 74 68 6f 75 74 20 73 65 65 6b 69 6e 67 20 74 ithout seeking t
1f11a 68 65 20 63 75 72 73 6f 72 2e 20 54 68 69 73 20 he cursor. This
1f11b 63 61 6e 20 62 65 20 61 20 62 69 67 20 70 65 72 can be a big per
1f11c 66 6f 72 6d 61 6e 63 65 20 62 6f 6f 73 74 2e 0a formance boost..
1f11d 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 69 6e 66 */. pCur->inf
1f11e 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 o.nSize = 0;. p
1f11f 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d Cur->validNKey =
1f120 20 30 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 0;. if( rc==SQ
1f121 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 LITE_OK && pPage
1f122 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 ->nOverflow ){.
1f123 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 rc = balance(
1f124 70 43 75 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d pCur);.. /* M
1f125 75 73 74 20 6d 61 6b 65 20 73 75 72 65 20 6e 4f ust make sure nO
1f126 76 65 72 66 6c 6f 77 20 69 73 20 72 65 73 65 74 verflow is reset
1f127 20 74 6f 20 7a 65 72 6f 20 65 76 65 6e 20 69 66 to zero even if
1f128 20 74 68 65 20 62 61 6c 61 6e 63 65 28 29 0a 20 the balance().
1f129 20 20 20 2a 2a 20 66 61 69 6c 73 2e 20 49 6e 74 ** fails. Int
1f12a 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 ernal data struc
1f12b 74 75 72 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 ture corruption
1f12c 77 69 6c 6c 20 72 65 73 75 6c 74 20 6f 74 68 65 will result othe
1f12d 72 77 69 73 65 2e 20 0a 20 20 20 20 2a 2a 20 41 rwise. . ** A
1f12e 6c 73 6f 2c 20 73 65 74 20 74 68 65 20 63 75 72 lso, set the cur
1f12f 73 6f 72 20 73 74 61 74 65 20 74 6f 20 69 6e 76 sor state to inv
1f130 61 6c 69 64 2e 20 54 68 69 73 20 73 74 6f 70 73 alid. This stops
1f131 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 saveCursorPosit
1f132 69 6f 6e 28 29 0a 20 20 20 20 2a 2a 20 66 72 6f ion(). ** fro
1f133 6d 20 74 72 79 69 6e 67 20 74 6f 20 73 61 76 65 m trying to save
1f134 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 6f 73 the current pos
1f135 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 75 72 ition of the cur
1f136 73 6f 72 2e 20 20 2a 2f 0a 20 20 20 20 70 43 75 sor. */. pCu
1f137 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
1f138 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72 66 6c 6f iPage]->nOverflo
1f139 77 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d w = 0;. pCur-
1f13a 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
1f13b 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 7d 0a 20 20 _INVALID;. }.
1f13c 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 assert( pCur->ap
1f13d 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
1f13e 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 ]->nOverflow==0
1f13f 29 3b 0a 0a 65 6e 64 5f 69 6e 73 65 72 74 3a 0a );..end_insert:.
1f140 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1f141 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 /*.** Delete the
1f142 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 entry that the
1f143 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 cursor is pointi
1f144 6e 67 20 74 6f 2e 20 20 54 68 65 20 63 75 72 73 ng to. The curs
1f145 6f 72 0a 2a 2a 20 69 73 20 6c 65 66 74 20 70 6f or.** is left po
1f146 69 6e 74 69 6e 67 20 61 74 20 61 20 61 72 62 69 inting at a arbi
1f147 74 72 61 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 0a trary location..
1f148 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1f149 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1f14a 65 65 44 65 6c 65 74 65 28 42 74 43 75 72 73 6f eeDelete(BtCurso
1f14b 72 20 2a 70 43 75 72 29 7b 0a 20 20 42 74 72 65 r *pCur){. Btre
1f14c 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 e *p = pCur->pBt
1f14d 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 ree;. BtShared
1f14e 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 20 20 *pBt = p->pBt;
1f14f 20 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 69 . i
1f150 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
1f151 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f152 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
1f153 64 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 de */. MemPage
1f154 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 *pPage;
1f155 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f156 50 61 67 65 20 74 6f 20 64 65 6c 65 74 65 20 63 Page to delete c
1f157 65 6c 6c 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 6e ell from */. un
1f158 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 signed char *pCe
1f159 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ll;
1f15a 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1f15b 20 63 65 6c 6c 20 74 6f 20 64 65 6c 65 74 65 20 cell to delete
1f15c 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 49 64 */. int iCellId
1f15d 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
1f15e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
1f15f 65 78 20 6f 66 20 63 65 6c 6c 20 74 6f 20 64 65 ex of cell to de
1f160 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 lete */. int iC
1f161 65 6c 6c 44 65 70 74 68 3b 20 20 20 20 20 20 20 ellDepth;
1f162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1f163 2a 20 44 65 70 74 68 20 6f 66 20 6e 6f 64 65 20 * Depth of node
1f164 63 6f 6e 74 61 69 6e 69 6e 67 20 70 43 65 6c 6c containing pCell
1f165 20 2a 2f 20 0a 0a 20 20 61 73 73 65 72 74 28 20 */ .. assert(
1f166 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
1f167 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
1f168 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 rt( pBt->inTrans
1f169 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 action==TRANS_WR
1f16a 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ITE );. assert(
1f16b 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 (pBt->btsFlags
1f16c 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 29 & BTS_READ_ONLY)
1f16d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
1f16e 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 3b pCur->wrFlag );
1f16f 0a 20 20 61 73 73 65 72 74 28 20 68 61 73 53 68 . assert( hasSh
1f170 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
1f171 63 6b 28 70 2c 20 70 43 75 72 2d 3e 70 67 6e 6f ck(p, pCur->pgno
1f172 52 6f 6f 74 2c 20 70 43 75 72 2d 3e 70 4b 65 79 Root, pCur->pKey
1f173 49 6e 66 6f 21 3d 30 2c 20 32 29 20 29 3b 0a 20 Info!=0, 2) );.
1f174 20 61 73 73 65 72 74 28 20 21 68 61 73 52 65 61 assert( !hasRea
1f175 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 70 43 dConflicts(p, pC
1f176 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 29 20 29 3b ur->pgnoRoot) );
1f177 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 43 .. if( NEVER(pC
1f178 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
1f179 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 iPage]>=pCur->ap
1f17a 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
1f17b 5d 2d 3e 6e 43 65 6c 6c 29 20 0a 20 20 20 7c 7c ]->nCell) . ||
1f17c 20 4e 45 56 45 52 28 70 43 75 72 2d 3e 65 53 74 NEVER(pCur->eSt
1f17d 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate!=CURSOR_VALI
1f17e 44 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 D). ){. retu
1f17f 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
1f180 20 20 2f 2a 20 53 6f 6d 65 74 68 69 6e 67 20 68 /* Something h
1f181 61 73 20 67 6f 6e 65 20 61 77 72 79 2e 20 2a 2f as gone awry. */
1f182 0a 20 20 7d 0a 0a 20 20 69 43 65 6c 6c 44 65 70 . }.. iCellDep
1f183 74 68 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 th = pCur->iPage
1f184 3b 0a 20 20 69 43 65 6c 6c 49 64 78 20 3d 20 70 ;. iCellIdx = p
1f185 43 75 72 2d 3e 61 69 49 64 78 5b 69 43 65 6c 6c Cur->aiIdx[iCell
1f186 44 65 70 74 68 5d 3b 0a 20 20 70 50 61 67 65 20 Depth];. pPage
1f187 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 = pCur->apPage[i
1f188 43 65 6c 6c 44 65 70 74 68 5d 3b 0a 20 20 70 43 CellDepth];. pC
1f189 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
1f18a 50 61 67 65 2c 20 69 43 65 6c 6c 49 64 78 29 3b Page, iCellIdx);
1f18b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 .. /* If the pa
1f18c 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ge containing th
1f18d 65 20 65 6e 74 72 79 20 74 6f 20 64 65 6c 65 74 e entry to delet
1f18e 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 e is not a leaf
1f18f 70 61 67 65 2c 20 6d 6f 76 65 0a 20 20 2a 2a 20 page, move. **
1f190 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 the cursor to th
1f191 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20 e largest entry
1f192 69 6e 20 74 68 65 20 74 72 65 65 20 74 68 61 74 in the tree that
1f193 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e is smaller than
1f194 0a 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 . ** the entry
1f195 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 20 54 being deleted. T
1f196 68 69 73 20 63 65 6c 6c 20 77 69 6c 6c 20 72 65 his cell will re
1f197 70 6c 61 63 65 20 74 68 65 20 63 65 6c 6c 20 62 place the cell b
1f198 65 69 6e 67 20 64 65 6c 65 74 65 64 0a 20 20 2a eing deleted. *
1f199 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 * from the inter
1f19a 6e 61 6c 20 6e 6f 64 65 2e 20 54 68 65 20 27 70 nal node. The 'p
1f19b 72 65 76 69 6f 75 73 27 20 65 6e 74 72 79 20 69 revious' entry i
1f19c 73 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20 s used for this
1f19d 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 instead. ** of
1f19e 74 68 65 20 27 6e 65 78 74 27 20 65 6e 74 72 79 the 'next' entry
1f19f 2c 20 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 , as the previou
1f1a0 73 20 65 6e 74 72 79 20 69 73 20 61 6c 77 61 79 s entry is alway
1f1a1 73 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 0a s a part of the.
1f1a2 20 20 2a 2a 20 73 75 62 2d 74 72 65 65 20 68 65 ** sub-tree he
1f1a3 61 64 65 64 20 62 79 20 74 68 65 20 63 68 69 6c aded by the chil
1f1a4 64 20 70 61 67 65 20 6f 66 20 74 68 65 20 63 65 d page of the ce
1f1a5 6c 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 ll being deleted
1f1a6 2e 20 54 68 69 73 20 6d 61 6b 65 73 0a 20 20 2a . This makes. *
1f1a7 2a 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 * balancing the
1f1a8 74 72 65 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 tree following t
1f1a9 68 65 20 64 65 6c 65 74 65 20 6f 70 65 72 61 74 he delete operat
1f1aa 69 6f 6e 20 65 61 73 69 65 72 2e 20 20 2a 2f 0a ion easier. */.
1f1ab 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 if( !pPage->le
1f1ac 61 66 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 6f af ){. int no
1f1ad 74 55 73 65 64 3b 0a 20 20 20 20 72 63 20 3d 20 tUsed;. rc =
1f1ae 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 sqlite3BtreePrev
1f1af 69 6f 75 73 28 70 43 75 72 2c 20 26 6e 6f 74 55 ious(pCur, ¬U
1f1b0 73 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 sed);. if( rc
1f1b1 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
1f1b2 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 }.. /* Save the
1f1b3 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e positions of an
1f1b4 79 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 y other cursors
1f1b5 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 open on this tab
1f1b6 6c 65 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 6d le before. ** m
1f1b7 61 6b 69 6e 67 20 61 6e 79 20 6d 6f 64 69 66 69 aking any modifi
1f1b8 63 61 74 69 6f 6e 73 2e 20 4d 61 6b 65 20 74 68 cations. Make th
1f1b9 65 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e e page containin
1f1ba 67 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20 62 g the entry to b
1f1bb 65 20 0a 20 20 2a 2a 20 64 65 6c 65 74 65 64 20 e . ** deleted
1f1bc 77 72 69 74 61 62 6c 65 2e 20 54 68 65 6e 20 66 writable. Then f
1f1bd 72 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 ree any overflow
1f1be 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 pages associate
1f1bf 64 20 77 69 74 68 20 74 68 65 20 0a 20 20 2a 2a d with the . **
1f1c0 20 65 6e 74 72 79 20 61 6e 64 20 66 69 6e 61 6c entry and final
1f1c1 6c 79 20 72 65 6d 6f 76 65 20 74 68 65 20 63 65 ly remove the ce
1f1c2 6c 6c 20 69 74 73 65 6c 66 20 66 72 6f 6d 20 77 ll itself from w
1f1c3 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e 20 ithin the page.
1f1c4 20 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 61 . */. rc = sa
1f1c5 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 veAllCursors(pBt
1f1c6 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 , pCur->pgnoRoot
1f1c7 2c 20 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 , pCur);. if( r
1f1c8 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a c ) return rc;..
1f1c9 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
1f1ca 61 20 64 65 6c 65 74 65 20 6f 70 65 72 61 74 69 a delete operati
1f1cb 6f 6e 20 74 6f 20 72 65 6d 6f 76 65 20 61 20 72 on to remove a r
1f1cc 6f 77 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 ow from a table
1f1cd 62 2d 74 72 65 65 2c 0a 20 20 2a 2a 20 69 6e 76 b-tree,. ** inv
1f1ce 61 6c 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72 alidate any incr
1f1cf 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 blob cursors ope
1f1d0 6e 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 69 n on the row bei
1f1d1 6e 67 20 64 65 6c 65 74 65 64 2e 20 20 2a 2f 0a ng deleted. */.
1f1d2 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4b 65 79 if( pCur->pKey
1f1d3 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 69 Info==0 ){. i
1f1d4 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f nvalidateIncrblo
1f1d5 62 43 75 72 73 6f 72 73 28 70 2c 20 70 43 75 72 bCursors(p, pCur
1f1d6 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 29 3b ->info.nKey, 0);
1f1d7 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c . }.. rc = sql
1f1d8 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
1f1d9 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a Page->pDbPage);.
1f1da 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
1f1db 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 63 6c 65 n rc;. rc = cle
1f1dc 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 arCell(pPage, pC
1f1dd 65 6c 6c 29 3b 0a 20 20 64 72 6f 70 43 65 6c 6c ell);. dropCell
1f1de 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 49 64 78 (pPage, iCellIdx
1f1df 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 , cellSizePtr(pP
1f1e0 61 67 65 2c 20 70 43 65 6c 6c 29 2c 20 26 72 63 age, pCell), &rc
1f1e1 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
1f1e2 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 49 turn rc;.. /* I
1f1e3 66 20 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 f the cell delet
1f1e4 65 64 20 77 61 73 20 6e 6f 74 20 6c 6f 63 61 74 ed was not locat
1f1e5 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 ed on a leaf pag
1f1e6 65 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 e, then the curs
1f1e7 6f 72 0a 20 20 2a 2a 20 69 73 20 63 75 72 72 65 or. ** is curre
1f1e8 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f ntly pointing to
1f1e9 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 the largest ent
1f1ea 72 79 20 69 6e 20 74 68 65 20 73 75 62 2d 74 72 ry in the sub-tr
1f1eb 65 65 20 68 65 61 64 65 64 0a 20 20 2a 2a 20 62 ee headed. ** b
1f1ec 79 20 74 68 65 20 63 68 69 6c 64 2d 70 61 67 65 y the child-page
1f1ed 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 of the cell tha
1f1ee 74 20 77 61 73 20 6a 75 73 74 20 64 65 6c 65 74 t was just delet
1f1ef 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e 74 65 72 ed from an inter
1f1f0 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 2e 20 54 nal. ** node. T
1f1f1 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 he cell from the
1f1f2 20 6c 65 61 66 20 6e 6f 64 65 20 6e 65 65 64 73 leaf node needs
1f1f3 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 to be moved to
1f1f4 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 20 20 2a the internal. *
1f1f5 2a 20 6e 6f 64 65 20 74 6f 20 72 65 70 6c 61 63 * node to replac
1f1f6 65 20 74 68 65 20 64 65 6c 65 74 65 64 20 63 65 e the deleted ce
1f1f7 6c 6c 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 ll. */. if( !p
1f1f8 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
1f1f9 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 65 61 66 MemPage *pLeaf
1f1fa 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
1f1fb 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
1f1fc 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 20 int nCell;.
1f1fd 20 50 67 6e 6f 20 6e 20 3d 20 70 43 75 72 2d 3e Pgno n = pCur->
1f1fe 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 70 74 apPage[iCellDept
1f1ff 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 h+1]->pgno;.
1f200 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
1f201 54 6d 70 3b 0a 0a 20 20 20 20 70 43 65 6c 6c 20 Tmp;.. pCell
1f202 3d 20 66 69 6e 64 43 65 6c 6c 28 70 4c 65 61 66 = findCell(pLeaf
1f203 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 , pLeaf->nCell-1
1f204 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 63 );. nCell = c
1f205 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c 65 61 66 ellSizePtr(pLeaf
1f206 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 61 73 , pCell);. as
1f207 73 65 72 74 28 20 4d 58 5f 43 45 4c 4c 5f 53 49 sert( MX_CELL_SI
1f208 5a 45 28 70 42 74 29 20 3e 3d 20 6e 43 65 6c 6c ZE(pBt) >= nCell
1f209 20 29 3b 0a 0a 20 20 20 20 61 6c 6c 6f 63 61 74 );.. allocat
1f20a 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b eTempSpace(pBt);
1f20b 0a 20 20 20 20 70 54 6d 70 20 3d 20 70 42 74 2d . pTmp = pBt-
1f20c 3e 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 20 20 >pTmpSpace;..
1f20d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1f20e 65 72 57 72 69 74 65 28 70 4c 65 61 66 2d 3e 70 erWrite(pLeaf->p
1f20f 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 6e 73 DbPage);. ins
1f210 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 ertCell(pPage, i
1f211 43 65 6c 6c 49 64 78 2c 20 70 43 65 6c 6c 2d 34 CellIdx, pCell-4
1f212 2c 20 6e 43 65 6c 6c 2b 34 2c 20 70 54 6d 70 2c , nCell+4, pTmp,
1f213 20 6e 2c 20 26 72 63 29 3b 0a 20 20 20 20 64 72 n, &rc);. dr
1f214 6f 70 43 65 6c 6c 28 70 4c 65 61 66 2c 20 70 4c opCell(pLeaf, pL
1f215 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20 6e 43 eaf->nCell-1, nC
1f216 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 ell, &rc);. i
1f217 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1f218 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 61 6c c;. }.. /* Bal
1f219 61 6e 63 65 20 74 68 65 20 74 72 65 65 2e 20 49 ance the tree. I
1f21a 66 20 74 68 65 20 65 6e 74 72 79 20 64 65 6c 65 f the entry dele
1f21b 74 65 64 20 77 61 73 20 6c 6f 63 61 74 65 64 20 ted was located
1f21c 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65 2c 0a on a leaf page,.
1f21d 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 75 ** then the cu
1f21e 72 73 6f 72 20 73 74 69 6c 6c 20 70 6f 69 6e 74 rsor still point
1f21f 73 20 74 6f 20 74 68 61 74 20 70 61 67 65 2e 20 s to that page.
1f220 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
1f221 20 66 69 72 73 74 0a 20 20 2a 2a 20 63 61 6c 6c first. ** call
1f222 20 74 6f 20 62 61 6c 61 6e 63 65 28 29 20 72 65 to balance() re
1f223 70 61 69 72 73 20 74 68 65 20 74 72 65 65 2c 20 pairs the tree,
1f224 61 6e 64 20 74 68 65 20 69 66 28 2e 2e 2e 29 20 and the if(...)
1f225 63 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 20 2a condition is. *
1f226 2a 20 6e 65 76 65 72 20 74 72 75 65 2e 0a 20 20 * never true..
1f227 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 **. ** Otherwis
1f228 65 2c 20 69 66 20 74 68 65 20 65 6e 74 72 79 20 e, if the entry
1f229 64 65 6c 65 74 65 64 20 77 61 73 20 6f 6e 20 61 deleted was on a
1f22a 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 n internal node
1f22b 70 61 67 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 page, then. **
1f22c 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 pCur is pointing
1f22d 20 74 6f 20 74 68 65 20 6c 65 61 66 20 70 61 67 to the leaf pag
1f22e 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 63 e from which a c
1f22f 65 6c 6c 20 77 61 73 20 72 65 6d 6f 76 65 64 20 ell was removed
1f230 74 6f 0a 20 20 2a 2a 20 72 65 70 6c 61 63 65 20 to. ** replace
1f231 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 65 64 the cell deleted
1f232 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e from the intern
1f233 61 6c 20 6e 6f 64 65 2e 20 54 68 69 73 20 69 73 al node. This is
1f234 20 73 6c 69 67 68 74 6c 79 0a 20 20 2a 2a 20 74 slightly. ** t
1f235 72 69 63 6b 79 20 61 73 20 74 68 65 20 6c 65 61 ricky as the lea
1f236 66 20 6e 6f 64 65 20 6d 61 79 20 62 65 20 75 6e f node may be un
1f237 64 65 72 66 75 6c 6c 2c 20 61 6e 64 20 74 68 65 derfull, and the
1f238 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 6d internal node m
1f239 61 79 0a 20 20 2a 2a 20 62 65 20 65 69 74 68 65 ay. ** be eithe
1f23a 72 20 75 6e 64 65 72 20 6f 72 20 6f 76 65 72 66 r under or overf
1f23b 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ull. In this cas
1f23c 65 20 72 75 6e 20 74 68 65 20 62 61 6c 61 6e 63 e run the balanc
1f23d 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20 ing algorithm.
1f23e 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 61 66 20 6e ** on the leaf n
1f23f 6f 64 65 20 66 69 72 73 74 2e 20 49 66 20 74 68 ode first. If th
1f240 65 20 62 61 6c 61 6e 63 65 20 70 72 6f 63 65 65 e balance procee
1f241 64 73 20 66 61 72 20 65 6e 6f 75 67 68 20 75 70 ds far enough up
1f242 20 74 68 65 0a 20 20 2a 2a 20 74 72 65 65 20 74 the. ** tree t
1f243 68 61 74 20 77 65 20 63 61 6e 20 62 65 20 73 75 hat we can be su
1f244 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 6f 62 re that any prob
1f245 6c 65 6d 20 69 6e 20 74 68 65 20 69 6e 74 65 72 lem in the inter
1f246 6e 61 6c 20 6e 6f 64 65 20 68 61 73 0a 20 20 2a nal node has. *
1f247 2a 20 62 65 65 6e 20 63 6f 72 72 65 63 74 65 64 * been corrected
1f248 2c 20 73 6f 20 62 65 20 69 74 2e 20 4f 74 68 65 , so be it. Othe
1f249 72 77 69 73 65 2c 20 61 66 74 65 72 20 62 61 6c rwise, after bal
1f24a 61 6e 63 69 6e 67 20 74 68 65 20 6c 65 61 66 20 ancing the leaf
1f24b 6e 6f 64 65 2c 0a 20 20 2a 2a 20 77 61 6c 6b 20 node,. ** walk
1f24c 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 68 the cursor up th
1f24d 65 20 74 72 65 65 20 74 6f 20 74 68 65 20 69 6e e tree to the in
1f24e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e 64 20 ternal node and
1f24f 62 61 6c 61 6e 63 65 20 69 74 20 61 73 20 0a 20 balance it as .
1f250 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0a 20 20 ** well. */.
1f251 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 rc = balance(pCu
1f252 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 r);. if( rc==SQ
1f253 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75 72 2d LITE_OK && pCur-
1f254 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 70 74 >iPage>iCellDept
1f255 68 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 h ){. while(
1f256 70 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c pCur->iPage>iCel
1f257 6c 44 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20 lDepth ){.
1f258 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 releasePage(pCur
1f259 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
1f25a 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 20 20 7d 0a Page--]);. }.
1f25b 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 rc = balance
1f25c 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 (pCur);. }.. i
1f25d 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1f25e 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f ){. moveToRo
1f25f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 ot(pCur);. }.
1f260 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1f261 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
1f262 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57 BTree table. W
1f263 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62 rite into *piTab
1f264 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e le the page.** n
1f265 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f umber for the ro
1f266 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e ot page of the n
1f267 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ew table..**.**
1f268 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 65 The type of type
1f269 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 is determined b
1f26a 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 y the flags para
1f26b 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65 meter. Only the
1f26c 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 .** following va
1f26d 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72 lues of flags ar
1f26e 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 e currently in u
1f26f 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65 se. Other value
1f270 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d s for.** flags m
1f271 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a ight not work:.*
1f272 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49 *.** BTREE_I
1f273 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46 NTKEY|BTREE_LEAF
1f274 44 41 54 41 20 20 20 20 20 55 73 65 64 20 66 6f DATA Used fo
1f275 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74 r SQL tables wit
1f276 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20 h rowid keys.**
1f277 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 BTREE_ZERODA
1f278 54 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TA
1f279 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c Used for SQL
1f27a 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74 indices.*/.stat
1f27b 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 61 ic int btreeCrea
1f27c 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 teTable(Btree *p
1f27d 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 , int *piTable,
1f27e 69 6e 74 20 63 72 65 61 74 65 54 61 62 46 6c 61 int createTabFla
1f27f 67 73 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 gs){. BtShared
1f280 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
1f281 20 4d 65 6d 50 61 67 65 20 2a 70 52 6f 6f 74 3b MemPage *pRoot;
1f282 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 . Pgno pgnoRoot
1f283 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e ;. int rc;. in
1f284 74 20 70 74 66 46 6c 61 67 73 3b 20 20 20 20 20 t ptfFlags;
1f285 20 20 20 20 20 2f 2a 20 50 61 67 65 2d 74 79 70 /* Page-typ
1f286 65 20 66 6c 61 67 65 20 66 6f 72 20 74 68 65 20 e flage for the
1f287 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 6e 65 77 root page of new
1f288 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 table */.. ass
1f289 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1f28a 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 eHoldsMutex(p) )
1f28b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d ;. assert( pBt-
1f28c 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d >inTransaction==
1f28d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 TRANS_WRITE );.
1f28e 20 61 73 73 65 72 74 28 20 28 70 42 74 2d 3e 62 assert( (pBt->b
1f28f 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 52 45 tsFlags & BTS_RE
1f290 41 44 5f 4f 4e 4c 59 29 3d 3d 30 20 29 3b 0a 0a AD_ONLY)==0 );..
1f291 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
1f292 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
1f293 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 rc = allocateBtr
1f294 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 52 6f eePage(pBt, &pRo
1f295 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f 74 2c 20 31 ot, &pgnoRoot, 1
1f296 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 , 0);. if( rc )
1f297 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
1f298 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 . }.#else. if(
1f299 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
1f29a 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e ){. Pgno pgn
1f29b 6f 4d 6f 76 65 3b 20 20 20 20 20 20 2f 2a 20 4d oMove; /* M
1f29c 6f 76 65 20 61 20 70 61 67 65 20 68 65 72 65 20 ove a page here
1f29d 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 to make room for
1f29e 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 2a the root-page *
1f29f 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 /. MemPage *p
1f2a0 50 61 67 65 4d 6f 76 65 3b 20 2f 2a 20 54 68 65 PageMove; /* The
1f2a1 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 74 6f page to move to
1f2a2 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 43 72 65 . */.. /* Cre
1f2a3 61 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c ating a new tabl
1f2a4 65 20 6d 61 79 20 70 72 6f 62 61 62 6c 79 20 72 e may probably r
1f2a5 65 71 75 69 72 65 20 6d 6f 76 69 6e 67 20 61 6e equire moving an
1f2a6 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 existing databa
1f2a7 73 65 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b se. ** to mak
1f2a8 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 6e e room for the n
1f2a9 65 77 20 74 61 62 6c 65 73 20 72 6f 6f 74 20 70 ew tables root p
1f2aa 61 67 65 2e 20 49 6e 20 63 61 73 65 20 74 68 69 age. In case thi
1f2ab 73 20 70 61 67 65 20 74 75 72 6e 73 0a 20 20 20 s page turns.
1f2ac 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 20 61 6e ** out to be an
1f2ad 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 overflow page,
1f2ae 64 65 6c 65 74 65 20 61 6c 6c 20 6f 76 65 72 66 delete all overf
1f2af 6c 6f 77 20 70 61 67 65 2d 6d 61 70 20 63 61 63 low page-map cac
1f2b0 68 65 73 0a 20 20 20 20 2a 2a 20 68 65 6c 64 20 hes. ** held
1f2b1 62 79 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e by open cursors.
1f2b2 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 76 61 . */. inva
1f2b3 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f lidateAllOverflo
1f2b4 77 43 61 63 68 65 28 70 42 74 29 3b 0a 0a 20 20 wCache(pBt);..
1f2b5 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 76 61 /* Read the va
1f2b6 6c 75 65 20 6f 66 20 6d 65 74 61 5b 33 5d 20 66 lue of meta[3] f
1f2b7 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
1f2b8 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 to determine wh
1f2b9 65 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 ere the. ** r
1f2ba 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 oot page of the
1f2bb 6e 65 77 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 new table should
1f2bc 20 67 6f 2e 20 6d 65 74 61 5b 33 5d 20 69 73 20 go. meta[3] is
1f2bd 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 the largest root
1f2be 2d 70 61 67 65 0a 20 20 20 20 2a 2a 20 63 72 65 -page. ** cre
1f2bf 61 74 65 64 20 73 6f 20 66 61 72 2c 20 73 6f 20 ated so far, so
1f2c0 74 68 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61 67 the new root-pag
1f2c1 65 20 69 73 20 28 6d 65 74 61 5b 33 5d 2b 31 29 e is (meta[3]+1)
1f2c2 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c .. */. sql
1f2c3 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 ite3BtreeGetMeta
1f2c4 28 70 2c 20 42 54 52 45 45 5f 4c 41 52 47 45 53 (p, BTREE_LARGES
1f2c5 54 5f 52 4f 4f 54 5f 50 41 47 45 2c 20 26 70 67 T_ROOT_PAGE, &pg
1f2c6 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 70 67 6e noRoot);. pgn
1f2c7 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20 20 20 2f 2a oRoot++;.. /*
1f2c8 20 54 68 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61 The new root-pa
1f2c9 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c ge may not be al
1f2ca 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 70 6f 69 located on a poi
1f2cb 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 6f nter-map page, o
1f2cc 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 50 45 4e r the. ** PEN
1f2cd 44 49 4e 47 5f 42 59 54 45 20 70 61 67 65 2e 0a DING_BYTE page..
1f2ce 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 */. while
1f2cf 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50 54 52 4d ( pgnoRoot==PTRM
1f2d0 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 AP_PAGENO(pBt, p
1f2d1 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a 20 20 20 20 gnoRoot) ||.
1f2d2 20 20 20 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50 45 pgnoRoot==PE
1f2d3 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
1f2d4 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 70 67 pBt) ){. pg
1f2d5 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20 20 20 7d 0a noRoot++;. }.
1f2d6 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f assert( pgno
1f2d7 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a 20 20 20 20 Root>=3 );..
1f2d8 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 70 61 /* Allocate a pa
1f2d9 67 65 2e 20 54 68 65 20 70 61 67 65 20 74 68 61 ge. The page tha
1f2da 74 20 63 75 72 72 65 6e 74 6c 79 20 72 65 73 69 t currently resi
1f2db 64 65 73 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 des at pgnoRoot
1f2dc 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 20 6d will. ** be m
1f2dd 6f 76 65 64 20 74 6f 20 74 68 65 20 61 6c 6c 6f oved to the allo
1f2de 63 61 74 65 64 20 70 61 67 65 20 28 75 6e 6c 65 cated page (unle
1f2df 73 73 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 ss the allocated
1f2e0 20 70 61 67 65 20 68 61 70 70 65 6e 73 0a 20 20 page happens.
1f2e1 20 20 2a 2a 20 74 6f 20 72 65 73 69 64 65 20 61 ** to reside a
1f2e2 74 20 70 67 6e 6f 52 6f 6f 74 29 2e 0a 20 20 20 t pgnoRoot)..
1f2e3 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c */. rc = all
1f2e4 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 ocateBtreePage(p
1f2e5 42 74 2c 20 26 70 50 61 67 65 4d 6f 76 65 2c 20 Bt, &pPageMove,
1f2e6 26 70 67 6e 6f 4d 6f 76 65 2c 20 70 67 6e 6f 52 &pgnoMove, pgnoR
1f2e7 6f 6f 74 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 oot, 1);. if(
1f2e8 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1f2e9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
1f2ea 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 c;. }.. if
1f2eb 28 20 70 67 6e 6f 4d 6f 76 65 21 3d 70 67 6e 6f ( pgnoMove!=pgno
1f2ec 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a Root ){. /*
1f2ed 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20 74 68 65 pgnoRoot is the
1f2ee 20 70 61 67 65 20 74 68 61 74 20 77 69 6c 6c 20 page that will
1f2ef 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 be used for the
1f2f0 72 6f 6f 74 2d 70 61 67 65 20 6f 66 0a 20 20 20 root-page of.
1f2f1 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 74 61 ** the new ta
1f2f2 62 6c 65 20 28 61 73 73 75 6d 69 6e 67 20 61 6e ble (assuming an
1f2f3 20 65 72 72 6f 72 20 64 69 64 20 6e 6f 74 20 6f error did not o
1f2f4 63 63 75 72 29 2e 20 42 75 74 20 77 65 20 77 65 ccur). But we we
1f2f5 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f re. ** allo
1f2f6 63 61 74 65 64 20 70 67 6e 6f 4d 6f 76 65 2e 20 cated pgnoMove.
1f2f7 49 66 20 72 65 71 75 69 72 65 64 20 28 69 2e 65 If required (i.e
1f2f8 2e 20 69 66 20 69 74 20 77 61 73 20 6e 6f 74 20 . if it was not
1f2f9 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 20 20 allocated.
1f2fa 2a 2a 20 62 79 20 65 78 74 65 6e 64 69 6e 67 20 ** by extending
1f2fb 74 68 65 20 66 69 6c 65 29 2c 20 74 68 65 20 63 the file), the c
1f2fc 75 72 72 65 6e 74 20 70 61 67 65 20 61 74 20 70 urrent page at p
1f2fd 6f 73 69 74 69 6f 6e 20 70 67 6e 6f 4d 6f 76 65 osition pgnoMove
1f2fe 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 61 6c 72 . ** is alr
1f2ff 65 61 64 79 20 6a 6f 75 72 6e 61 6c 65 64 2e 0a eady journaled..
1f300 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 75 */. u
1f301 38 20 65 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 8 eType = 0;.
1f302 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 Pgno iPtrPage
1f303 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 72 65 6c = 0;.. rel
1f304 65 61 73 65 50 61 67 65 28 70 50 61 67 65 4d 6f easePage(pPageMo
1f305 76 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d ve);.. /* M
1f306 6f 76 65 20 74 68 65 20 70 61 67 65 20 63 75 72 ove the page cur
1f307 72 65 6e 74 6c 79 20 61 74 20 70 67 6e 6f 52 6f rently at pgnoRo
1f308 6f 74 20 74 6f 20 70 67 6e 6f 4d 6f 76 65 2e 20 ot to pgnoMove.
1f309 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 */. rc = bt
1f30a 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
1f30b 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 pgnoRoot, &pRoot
1f30c 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
1f30d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1f30e 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1f30f 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
1f310 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 rc = ptrmapGet
1f311 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 (pBt, pgnoRoot,
1f312 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 67 &eType, &iPtrPag
1f313 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 e);. if( eT
1f314 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 ype==PTRMAP_ROOT
1f315 50 41 47 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 PAGE || eType==P
1f316 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 TRMAP_FREEPAGE )
1f317 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 {. rc = S
1f318 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
1f319 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 PT;. }.
1f31a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1f31b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
1f31c 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 eleasePage(pRoot
1f31d 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
1f31e 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
1f31f 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79 70 assert( eTyp
1f320 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 e!=PTRMAP_ROOTPA
1f321 47 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 GE );. asse
1f322 72 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 rt( eType!=PTRMA
1f323 50 5f 46 52 45 45 50 41 47 45 20 29 3b 0a 20 20 P_FREEPAGE );.
1f324 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 rc = relocat
1f325 65 50 61 67 65 28 70 42 74 2c 20 70 52 6f 6f 74 ePage(pBt, pRoot
1f326 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 , eType, iPtrPag
1f327 65 2c 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 29 3b e, pgnoMove, 0);
1f328 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
1f329 67 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 ge(pRoot);..
1f32a 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 /* Obtain the
1f32b 70 61 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 page at pgnoRoot
1f32c 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 */. if( rc
1f32d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1f32e 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1f32f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1f330 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
1f331 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c e(pBt, pgnoRoot,
1f332 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 &pRoot, 0);.
1f333 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1f334 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1f335 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1f336 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
1f337 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
1f338 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b pRoot->pDbPage);
1f339 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1f33a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1f33b 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1f33c 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 pRoot);.
1f33d 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1f33e 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1f33f 20 20 20 20 70 52 6f 6f 74 20 3d 20 70 50 61 67 pRoot = pPag
1f340 65 4d 6f 76 65 3b 0a 20 20 20 20 7d 20 0a 0a 20 eMove;. } ..
1f341 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 /* Update the
1f342 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 61 6e 64 pointer-map and
1f343 20 6d 65 74 61 2d 64 61 74 61 20 77 69 74 68 20 meta-data with
1f344 74 68 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61 67 the new root-pag
1f345 65 20 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 e number. */.
1f346 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
1f347 70 67 6e 6f 52 6f 6f 74 2c 20 50 54 52 4d 41 50 pgnoRoot, PTRMAP
1f348 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20 26 72 _ROOTPAGE, 0, &r
1f349 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 c);. if( rc )
1f34a 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 {. releaseP
1f34b 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 age(pRoot);.
1f34c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1f34d 20 7d 0a 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 }.. /* When
1f34e 74 68 65 20 6e 65 77 20 72 6f 6f 74 20 70 61 67 the new root pag
1f34f 65 20 77 61 73 20 61 6c 6c 6f 63 61 74 65 64 2c e was allocated,
1f350 20 70 61 67 65 20 31 20 77 61 73 20 6d 61 64 65 page 1 was made
1f351 20 77 72 69 74 61 62 6c 65 20 69 6e 0a 20 20 20 writable in.
1f352 20 2a 2a 20 6f 72 64 65 72 20 65 69 74 68 65 72 ** order either
1f353 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 to increase the
1f354 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 69 database filesi
1f355 7a 65 2c 20 6f 72 20 74 6f 20 64 65 63 72 65 6d ze, or to decrem
1f356 65 6e 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 ent the. ** f
1f357 72 65 65 6c 69 73 74 20 63 6f 75 6e 74 2e 20 20 reelist count.
1f358 48 65 6e 63 65 2c 20 74 68 65 20 73 71 6c 69 74 Hence, the sqlit
1f359 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 e3BtreeUpdateMet
1f35a 61 28 29 20 63 61 6c 6c 20 63 61 6e 6e 6f 74 20 a() call cannot
1f35b 66 61 69 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 fail.. */.
1f35c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1f35d 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
1f35e 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 (pBt->pPage1->pD
1f35f 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 72 63 bPage) );. rc
1f360 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 = sqlite3BtreeU
1f361 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 pdateMeta(p, 4,
1f362 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 pgnoRoot);. i
1f363 66 28 20 4e 45 56 45 52 28 72 63 29 20 29 7b 0a f( NEVER(rc) ){.
1f364 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
1f365 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 e(pRoot);.
1f366 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
1f367 0a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 .. }else{. r
1f368 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 c = allocateBtre
1f369 65 50 61 67 65 28 70 42 74 2c 20 26 70 52 6f 6f ePage(pBt, &pRoo
1f36a 74 2c 20 26 70 67 6e 6f 52 6f 6f 74 2c 20 31 2c t, &pgnoRoot, 1,
1f36b 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 0);. if( rc
1f36c 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d ) return rc;. }
1f36d 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 .#endif. assert
1f36e 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
1f36f 77 72 69 74 65 61 62 6c 65 28 70 52 6f 6f 74 2d writeable(pRoot-
1f370 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 >pDbPage) );. i
1f371 66 28 20 63 72 65 61 74 65 54 61 62 46 6c 61 67 f( createTabFlag
1f372 73 20 26 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 s & BTREE_INTKEY
1f373 20 29 7b 0a 20 20 20 20 70 74 66 46 6c 61 67 73 ){. ptfFlags
1f374 20 3d 20 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20 = PTF_INTKEY |
1f375 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 PTF_LEAFDATA | P
1f376 54 46 5f 4c 45 41 46 3b 0a 20 20 7d 65 6c 73 65 TF_LEAF;. }else
1f377 7b 0a 20 20 20 20 70 74 66 46 6c 61 67 73 20 3d {. ptfFlags =
1f378 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 PTF_ZERODATA |
1f379 50 54 46 5f 4c 45 41 46 3b 0a 20 20 7d 0a 20 20 PTF_LEAF;. }.
1f37a 7a 65 72 6f 50 61 67 65 28 70 52 6f 6f 74 2c 20 zeroPage(pRoot,
1f37b 70 74 66 46 6c 61 67 73 29 3b 0a 20 20 73 71 6c ptfFlags);. sql
1f37c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
1f37d 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a Root->pDbPage);.
1f37e 20 20 61 73 73 65 72 74 28 20 28 70 42 74 2d 3e assert( (pBt->
1f37f 6f 70 65 6e 46 6c 61 67 73 20 26 20 42 54 52 45 openFlags & BTRE
1f380 45 5f 53 49 4e 47 4c 45 29 3d 3d 30 20 7c 7c 20 E_SINGLE)==0 ||
1f381 70 67 6e 6f 52 6f 6f 74 3d 3d 32 20 29 3b 0a 20 pgnoRoot==2 );.
1f382 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 *piTable = (int
1f383 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 )pgnoRoot;. ret
1f384 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1f385 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f386 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
1f387 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 CreateTable(Btre
1f388 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 e *p, int *piTab
1f389 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a le, int flags){.
1f38a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 int rc;. sqli
1f38b 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
1f38c 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 72 ;. rc = btreeCr
1f38d 65 61 74 65 54 61 62 6c 65 28 70 2c 20 70 69 54 eateTable(p, piT
1f38e 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 able, flags);.
1f38f 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1f390 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
1f391 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 c;.}../*.** Eras
1f392 65 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 e the given data
1f393 62 61 73 65 20 70 61 67 65 20 61 6e 64 20 61 6c base page and al
1f394 6c 20 69 74 73 20 63 68 69 6c 64 72 65 6e 2e 20 l its children.
1f395 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 70 Return.** the p
1f396 61 67 65 20 74 6f 20 74 68 65 20 66 72 65 65 6c age to the freel
1f397 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ist..*/.static i
1f398 6e 74 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 nt clearDatabase
1f399 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 Page(. BtShared
1f39a 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 *pBt,
1f39b 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 74 68 /* The BTree th
1f39c 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
1f39d 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 table */. Pgno
1f39e 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 pgno,
1f39f 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
1f3a0 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 er to clear */.
1f3a1 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c 61 int freePageFla
1f3a2 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 44 65 61 g, /* Dea
1f3a3 6c 6c 6f 63 61 74 65 20 70 61 67 65 20 69 66 20 llocate page if
1f3a4 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 true */. int *p
1f3a5 6e 43 68 61 6e 67 65 20 20 20 20 20 20 20 20 20 nChange
1f3a6 20 20 20 2f 2a 20 41 64 64 20 6e 75 6d 62 65 72 /* Add number
1f3a7 20 6f 66 20 43 65 6c 6c 73 20 66 72 65 65 64 20 of Cells freed
1f3a8 74 6f 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20 to this counter
1f3a9 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 */.){. MemPage
1f3aa 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 72 63 *pPage;. int rc
1f3ab 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ;. unsigned cha
1f3ac 72 20 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 r *pCell;. int
1f3ad 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 i;.. assert( sq
1f3ae 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1f3af 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
1f3b0 20 20 69 66 28 20 70 67 6e 6f 3e 62 74 72 65 65 if( pgno>btree
1f3b1 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 Pagecount(pBt) )
1f3b2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1f3b3 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1f3b4 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 ;. }.. rc = ge
1f3b5 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 tAndInitPage(pBt
1f3b6 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67 65 29 3b , pgno, &pPage);
1f3b7 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
1f3b8 72 6e 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 rn rc;. for(i=0
1f3b9 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ; i<pPage->nCell
1f3ba 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65 6c ; i++){. pCel
1f3bb 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 l = findCell(pPa
1f3bc 67 65 2c 20 69 29 3b 0a 20 20 20 20 69 66 28 20 ge, i);. if(
1f3bd 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
1f3be 20 20 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 rc = clear
1f3bf 44 61 74 61 62 61 73 65 50 61 67 65 28 70 42 74 DatabasePage(pBt
1f3c0 2c 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c , get4byte(pCell
1f3c1 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b ), 1, pnChange);
1f3c2 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 . if( rc )
1f3c3 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 goto cleardataba
1f3c4 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 sepage_out;.
1f3c5 7d 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 }. rc = clear
1f3c6 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c Cell(pPage, pCel
1f3c7 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 l);. if( rc )
1f3c8 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 goto cleardatab
1f3c9 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d asepage_out;. }
1f3ca 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c . if( !pPage->l
1f3cb 65 61 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 eaf ){. rc =
1f3cc 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67 clearDatabasePag
1f3cd 65 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 e(pBt, get4byte(
1f3ce 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 38 5d &pPage->aData[8]
1f3cf 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b ), 1, pnChange);
1f3d0 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f . if( rc ) go
1f3d1 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 to cleardatabase
1f3d2 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 page_out;. }els
1f3d3 65 20 69 66 28 20 70 6e 43 68 61 6e 67 65 20 29 e if( pnChange )
1f3d4 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
1f3d5 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 age->intKey );.
1f3d6 20 20 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d 20 *pnChange +=
1f3d7 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 pPage->nCell;.
1f3d8 7d 0a 20 20 69 66 28 20 66 72 65 65 50 61 67 65 }. if( freePage
1f3d9 46 6c 61 67 20 29 7b 0a 20 20 20 20 66 72 65 65 Flag ){. free
1f3da 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 Page(pPage, &rc)
1f3db 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 ;. }else if( (r
1f3dc 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1f3dd 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 Write(pPage->pDb
1f3de 50 61 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 Page))==0 ){.
1f3df 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c zeroPage(pPage,
1f3e0 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d pPage->aData[0]
1f3e1 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 | PTF_LEAF);.
1f3e2 7d 0a 0a 63 6c 65 61 72 64 61 74 61 62 61 73 65 }..cleardatabase
1f3e3 70 61 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c 65 page_out:. rele
1f3e4 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
1f3e5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1f3e6 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c /*.** Delete all
1f3e7 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f information fro
1f3e8 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 m a single table
1f3e9 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1f3ea 2e 20 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a 20 . iTable is.**
1f3eb 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
1f3ec 6f 66 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 of the root of t
1f3ed 68 65 20 74 61 62 6c 65 2e 20 20 41 66 74 65 72 he table. After
1f3ee 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
1f3ef 74 75 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72 6f turns,.** the ro
1f3f0 6f 74 20 70 61 67 65 20 69 73 20 65 6d 70 74 79 ot page is empty
1f3f1 2c 20 62 75 74 20 73 74 69 6c 6c 20 65 78 69 73 , but still exis
1f3f2 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ts..**.** This r
1f3f3 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c outine will fail
1f3f4 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 with SQLITE_LOC
1f3f5 4b 45 44 20 69 66 20 74 68 65 72 65 20 61 72 65 KED if there are
1f3f6 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65 61 any open.** rea
1f3f7 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 d cursors on the
1f3f8 20 74 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 72 table. Open wr
1f3f9 69 74 65 20 63 75 72 73 6f 72 73 20 61 72 65 20 ite cursors are
1f3fa 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 moved to the.**
1f3fb 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c root of the tabl
1f3fc 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43 68 e..**.** If pnCh
1f3fd 61 6e 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c ange is not NULL
1f3fe 2c 20 74 68 65 6e 20 74 61 62 6c 65 20 69 54 61 , then table iTa
1f3ff 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6e 20 69 ble must be an i
1f400 6e 74 6b 65 79 20 74 61 62 6c 65 2e 20 54 68 65 ntkey table. The
1f401 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 .** integer valu
1f402 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 e pointed to by
1f403 70 6e 43 68 61 6e 67 65 20 69 73 20 69 6e 63 72 pnChange is incr
1f404 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e emented by the n
1f405 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 72 umber of.** entr
1f406 69 65 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 ies in the table
1f407 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f408 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
1f409 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 42 treeClearTable(B
1f40a 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 tree *p, int iTa
1f40b 62 6c 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61 6e ble, int *pnChan
1f40c 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ge){. int rc;.
1f40d 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
1f40e 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 p->pBt;. sqlit
1f40f 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
1f410 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e . assert( p->in
1f411 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 Trans==TRANS_WRI
1f412 54 45 20 29 3b 0a 0a 20 20 72 63 20 3d 20 73 61 TE );.. rc = sa
1f413 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 veAllCursors(pBt
1f414 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 , (Pgno)iTable,
1f415 30 29 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 0);.. if( SQLIT
1f416 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 E_OK==rc ){.
1f417 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c /* Invalidate al
1f418 6c 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f l incrblob curso
1f419 72 73 20 6f 70 65 6e 20 6f 6e 20 74 61 62 6c 65 rs open on table
1f41a 20 69 54 61 62 6c 65 20 28 61 73 73 75 6d 69 6e iTable (assumin
1f41b 67 20 69 54 61 62 6c 65 0a 20 20 20 20 2a 2a 20 g iTable. **
1f41c 69 73 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 is the root of a
1f41d 20 74 61 62 6c 65 20 62 2d 74 72 65 65 20 2d 20 table b-tree -
1f41e 69 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 74 68 if it is not, th
1f41f 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c e following call
1f420 20 69 73 0a 20 20 20 20 2a 2a 20 61 20 6e 6f 2d is. ** a no-
1f421 6f 70 29 2e 20 20 2a 2f 0a 20 20 20 20 69 6e 76 op). */. inv
1f422 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 alidateIncrblobC
1f423 75 72 73 6f 72 73 28 70 2c 20 30 2c 20 31 29 3b ursors(p, 0, 1);
1f424 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 . rc = clearD
1f425 61 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c atabasePage(pBt,
1f426 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30 (Pgno)iTable, 0
1f427 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20 20 7d , pnChange);. }
1f428 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
1f429 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
1f42a 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 n rc;.}../*.** E
1f42b 72 61 73 65 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 rase all informa
1f42c 74 69 6f 6e 20 69 6e 20 61 20 74 61 62 6c 65 20 tion in a table
1f42d 61 6e 64 20 61 64 64 20 74 68 65 20 72 6f 6f 74 and add the root
1f42e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f of the table to
1f42f 0a 2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 .** the freelist
1f430 2e 20 20 45 78 63 65 70 74 2c 20 74 68 65 20 72 . Except, the r
1f431 6f 6f 74 20 6f 66 20 74 68 65 20 70 72 69 6e 63 oot of the princ
1f432 69 70 6c 65 20 74 61 62 6c 65 20 28 74 68 65 20 iple table (the
1f433 6f 6e 65 20 6f 6e 0a 2a 2a 20 70 61 67 65 20 31 one on.** page 1
1f434 29 20 69 73 20 6e 65 76 65 72 20 61 64 64 65 64 ) is never added
1f435 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 to the freelist
1f436 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1f437 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20 77 tine will fail w
1f438 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 ith SQLITE_LOCKE
1f439 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 D if there are a
1f43a 6e 79 20 6f 70 65 6e 0a 2a 2a 20 63 75 72 73 6f ny open.** curso
1f43b 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e rs on the table.
1f43c 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 .**.** If AUTOVA
1f43d 43 55 55 4d 20 69 73 20 65 6e 61 62 6c 65 64 20 CUUM is enabled
1f43e 61 6e 64 20 74 68 65 20 70 61 67 65 20 61 74 20 and the page at
1f43f 69 54 61 62 6c 65 20 69 73 20 6e 6f 74 20 74 68 iTable is not th
1f440 65 20 6c 61 73 74 0a 2a 2a 20 72 6f 6f 74 20 70 e last.** root p
1f441 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 age in the datab
1f442 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 74 ase file, then t
1f443 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 he last root pag
1f444 65 20 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 e .** in the dat
1f445 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d 6f abase file is mo
1f446 76 65 64 20 69 6e 74 6f 20 74 68 65 20 73 6c 6f ved into the slo
1f447 74 20 66 6f 72 6d 65 72 6c 79 20 6f 63 63 75 70 t formerly occup
1f448 69 65 64 20 62 79 0a 2a 2a 20 69 54 61 62 6c 65 ied by.** iTable
1f449 20 61 6e 64 20 74 68 61 74 20 6c 61 73 74 20 73 and that last s
1f44a 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f 63 63 lot formerly occ
1f44b 75 70 69 65 64 20 62 79 20 74 68 65 20 6c 61 73 upied by the las
1f44c 74 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69 t root page.** i
1f44d 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 s added to the f
1f44e 72 65 65 6c 69 73 74 20 69 6e 73 74 65 61 64 20 reelist instead
1f44f 6f 66 20 69 54 61 62 6c 65 2e 20 20 49 6e 20 74 of iTable. In t
1f450 68 69 73 20 73 61 79 2c 20 61 6c 6c 0a 2a 2a 20 his say, all.**
1f451 72 6f 6f 74 20 70 61 67 65 73 20 61 72 65 20 6b root pages are k
1f452 65 70 74 20 61 74 20 74 68 65 20 62 65 67 69 6e ept at the begin
1f453 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 ning of the data
1f454 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68 base file, which
1f455 0a 2a 2a 20 69 73 20 6e 65 63 65 73 73 61 72 79 .** is necessary
1f456 20 66 6f 72 20 41 55 54 4f 56 41 43 55 55 4d 20 for AUTOVACUUM
1f457 74 6f 20 77 6f 72 6b 20 72 69 67 68 74 2e 20 20 to work right.
1f458 2a 70 69 4d 6f 76 65 64 20 69 73 20 73 65 74 20 *piMoved is set
1f459 74 6f 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 to the .** page
1f45a 6e 75 6d 62 65 72 20 74 68 61 74 20 75 73 65 64 number that used
1f45b 20 74 6f 20 62 65 20 74 68 65 20 6c 61 73 74 20 to be the last
1f45c 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 root page in the
1f45d 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 2a 2a 20 file before.**
1f45e 74 68 65 20 6d 6f 76 65 2e 20 20 49 66 20 6e 6f the move. If no
1f45f 20 70 61 67 65 20 67 65 74 73 20 6d 6f 76 65 64 page gets moved
1f460 2c 20 2a 70 69 4d 6f 76 65 64 20 69 73 20 73 65 , *piMoved is se
1f461 74 20 74 6f 20 30 2e 0a 2a 2a 20 54 68 65 20 6c t to 0..** The l
1f462 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 69 73 ast root page is
1f463 20 72 65 63 6f 72 64 65 64 20 69 6e 20 6d 65 74 recorded in met
1f464 61 5b 33 5d 20 61 6e 64 20 74 68 65 20 76 61 6c a[3] and the val
1f465 75 65 20 6f 66 0a 2a 2a 20 6d 65 74 61 5b 33 5d ue of.** meta[3]
1f466 20 69 73 20 75 70 64 61 74 65 64 20 62 79 20 74 is updated by t
1f467 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e 0a 2a his procedure..*
1f468 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 /.static int btr
1f469 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65 eeDropTable(Btre
1f46a 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 6c e *p, Pgno iTabl
1f46b 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64 29 e, int *piMoved)
1f46c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 {. int rc;. Me
1f46d 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 mPage *pPage = 0
1f46e 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
1f46f 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 t = p->pBt;.. a
1f470 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
1f471 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 reeHoldsMutex(p)
1f472 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
1f473 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
1f474 57 52 49 54 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 WRITE );.. /* I
1f475 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 t is illegal to
1f476 64 72 6f 70 20 61 20 74 61 62 6c 65 20 69 66 20 drop a table if
1f477 61 6e 79 20 63 75 72 73 6f 72 73 20 61 72 65 20 any cursors are
1f478 6f 70 65 6e 20 6f 6e 20 74 68 65 0a 20 20 2a 2a open on the. **
1f479 20 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 20 database. This
1f47a 69 73 20 62 65 63 61 75 73 65 20 69 6e 20 61 75 is because in au
1f47b 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 74 to-vacuum mode t
1f47c 68 65 20 62 61 63 6b 65 6e 64 20 6d 61 79 0a 20 he backend may.
1f47d 20 2a 2a 20 6e 65 65 64 20 74 6f 20 6d 6f 76 65 ** need to move
1f47e 20 61 6e 6f 74 68 65 72 20 72 6f 6f 74 2d 70 61 another root-pa
1f47f 67 65 20 74 6f 20 66 69 6c 6c 20 61 20 67 61 70 ge to fill a gap
1f480 20 6c 65 66 74 20 62 79 20 74 68 65 20 64 65 6c left by the del
1f481 65 74 65 64 0a 20 20 2a 2a 20 72 6f 6f 74 20 70 eted. ** root p
1f482 61 67 65 2e 20 49 66 20 61 6e 20 6f 70 65 6e 20 age. If an open
1f483 63 75 72 73 6f 72 20 77 61 73 20 75 73 69 6e 67 cursor was using
1f484 20 74 68 69 73 20 70 61 67 65 20 61 20 70 72 6f this page a pro
1f485 62 6c 65 6d 20 77 6f 75 6c 64 20 0a 20 20 2a 2a blem would . **
1f486 20 6f 63 63 75 72 2e 0a 20 20 2a 2a 0a 20 20 2a occur.. **. *
1f487 2a 20 54 68 69 73 20 65 72 72 6f 72 20 69 73 20 * This error is
1f488 63 61 75 67 68 74 20 6c 6f 6e 67 20 62 65 66 6f caught long befo
1f489 72 65 20 63 6f 6e 74 72 6f 6c 20 72 65 61 63 68 re control reach
1f48a 65 73 20 74 68 69 73 20 70 6f 69 6e 74 2e 0a 20 es this point..
1f48b 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 */. if( NEVER(
1f48c 70 42 74 2d 3e 70 43 75 72 73 6f 72 29 20 29 7b pBt->pCursor) ){
1f48d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e . sqlite3Conn
1f48e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d ectionBlocked(p-
1f48f 3e 64 62 2c 20 70 42 74 2d 3e 70 43 75 72 73 6f >db, pBt->pCurso
1f490 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a r->pBtree->db);.
1f491 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1f492 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 E_LOCKED_SHAREDC
1f493 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 ACHE;. }.. rc
1f494 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
1f495 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 Bt, (Pgno)iTable
1f496 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 , &pPage, 0);.
1f497 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
1f498 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 rc;. rc = sqlit
1f499 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c e3BtreeClearTabl
1f49a 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b e(p, iTable, 0);
1f49b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
1f49c 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
1f49d 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ge);. return
1f49e 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 4d 6f rc;. }.. *piMo
1f49f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 ved = 0;.. if(
1f4a0 69 54 61 62 6c 65 3e 31 20 29 7b 0a 23 69 66 64 iTable>1 ){.#ifd
1f4a1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1f4a2 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 66 72 UTOVACUUM. fr
1f4a3 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 eePage(pPage, &r
1f4a4 63 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 c);. releaseP
1f4a5 61 67 65 28 70 50 61 67 65 29 3b 0a 23 65 6c 73 age(pPage);.#els
1f4a6 65 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 e. if( pBt->a
1f4a7 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 utoVacuum ){.
1f4a8 20 20 20 50 67 6e 6f 20 6d 61 78 52 6f 6f 74 50 Pgno maxRootP
1f4a9 67 6e 6f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 gno;. sqlit
1f4aa 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 e3BtreeGetMeta(p
1f4ab 2c 20 42 54 52 45 45 5f 4c 41 52 47 45 53 54 5f , BTREE_LARGEST_
1f4ac 52 4f 4f 54 5f 50 41 47 45 2c 20 26 6d 61 78 52 ROOT_PAGE, &maxR
1f4ad 6f 6f 74 50 67 6e 6f 29 3b 0a 0a 20 20 20 20 20 ootPgno);..
1f4ae 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 6d 61 78 if( iTable==max
1f4af 52 6f 6f 74 50 67 6e 6f 20 29 7b 0a 20 20 20 20 RootPgno ){.
1f4b0 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61 /* If the ta
1f4b1 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 ble being droppe
1f4b2 64 20 69 73 20 74 68 65 20 74 61 62 6c 65 20 77 d is the table w
1f4b3 69 74 68 20 74 68 65 20 6c 61 72 67 65 73 74 20 ith the largest
1f4b4 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20 root-page.
1f4b5 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74 ** number in t
1f4b6 68 65 20 64 61 74 61 62 61 73 65 2c 20 70 75 74 he database, put
1f4b7 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f the root page o
1f4b8 6e 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e n the free list.
1f4b9 20 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 . */.
1f4ba 20 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50 freePage(pP
1f4bb 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 age, &rc);.
1f4bc 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1f4bd 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 Page);. i
1f4be 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1f4bf 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
1f4c0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
1f4c1 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
1f4c2 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
1f4c3 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 able being dropp
1f4c4 65 64 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 ed does not have
1f4c5 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f the largest roo
1f4c6 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a t-page. *
1f4c7 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20 * number in the
1f4c8 64 61 74 61 62 61 73 65 2e 20 53 6f 20 6d 6f 76 database. So mov
1f4c9 65 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 e the page that
1f4ca 64 6f 65 73 20 69 6e 74 6f 20 74 68 65 20 0a 20 does into the .
1f4cb 20 20 20 20 20 20 20 2a 2a 20 67 61 70 20 6c 65 ** gap le
1f4cc 66 74 20 62 79 20 74 68 65 20 64 65 6c 65 74 65 ft by the delete
1f4cd 64 20 72 6f 6f 74 2d 70 61 67 65 2e 0a 20 20 20 d root-page..
1f4ce 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
1f4cf 4d 65 6d 50 61 67 65 20 2a 70 4d 6f 76 65 3b 0a MemPage *pMove;.
1f4d0 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
1f4d1 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 age(pPage);.
1f4d2 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 rc = btreeGe
1f4d3 74 50 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f tPage(pBt, maxRo
1f4d4 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 otPgno, &pMove,
1f4d5 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0);. if(
1f4d6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1f4d7 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
1f4d8 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
1f4d9 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c rc = rel
1f4da 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 70 ocatePage(pBt, p
1f4db 4d 6f 76 65 2c 20 50 54 52 4d 41 50 5f 52 4f 4f Move, PTRMAP_ROO
1f4dc 54 50 41 47 45 2c 20 30 2c 20 69 54 61 62 6c 65 TPAGE, 0, iTable
1f4dd 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 72 65 , 0);. re
1f4de 6c 65 61 73 65 50 61 67 65 28 70 4d 6f 76 65 29 leasePage(pMove)
1f4df 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
1f4e0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1f4e1 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
1f4e2 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
1f4e3 20 20 20 20 20 20 70 4d 6f 76 65 20 3d 20 30 3b pMove = 0;
1f4e4 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 . rc = bt
1f4e5 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
1f4e6 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d maxRootPgno, &pM
1f4e7 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 ove, 0);.
1f4e8 20 66 72 65 65 50 61 67 65 28 70 4d 6f 76 65 2c freePage(pMove,
1f4e9 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 72 &rc);. r
1f4ea 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f 76 65 eleasePage(pMove
1f4eb 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
1f4ec 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1f4ed 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
1f4ee 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc;. }.
1f4ef 20 20 20 20 20 20 20 2a 70 69 4d 6f 76 65 64 20 *piMoved
1f4f0 3d 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 = maxRootPgno;.
1f4f1 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
1f4f2 20 53 65 74 20 74 68 65 20 6e 65 77 20 27 6d 61 Set the new 'ma
1f4f3 78 2d 72 6f 6f 74 2d 70 61 67 65 27 20 76 61 6c x-root-page' val
1f4f4 75 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ue in the databa
1f4f5 73 65 20 68 65 61 64 65 72 2e 20 54 68 69 73 0a se header. This.
1f4f6 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 ** is the
1f4f7 6f 6c 64 20 76 61 6c 75 65 20 6c 65 73 73 20 6f old value less o
1f4f8 6e 65 2c 20 6c 65 73 73 20 6f 6e 65 20 6d 6f 72 ne, less one mor
1f4f9 65 20 69 66 20 74 68 61 74 20 68 61 70 70 65 6e e if that happen
1f4fa 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 62 65 s to. ** be
1f4fb 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d a root-page num
1f4fc 62 65 72 2c 20 6c 65 73 73 20 6f 6e 65 20 61 67 ber, less one ag
1f4fd 61 69 6e 20 69 66 20 74 68 61 74 20 69 73 20 74 ain if that is t
1f4fe 68 65 0a 20 20 20 20 20 20 2a 2a 20 50 45 4e 44 he. ** PEND
1f4ff 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 2e 0a 20 ING_BYTE_PAGE..
1f500 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6d 61 */. ma
1f501 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 xRootPgno--;.
1f502 20 20 20 77 68 69 6c 65 28 20 6d 61 78 52 6f 6f while( maxRoo
1f503 74 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42 tPgno==PENDING_B
1f504 59 54 45 5f 50 41 47 45 28 70 42 74 29 0a 20 20 YTE_PAGE(pBt).
1f505 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 50 54 || PT
1f506 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c RMAP_ISPAGE(pBt,
1f507 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 20 29 7b maxRootPgno) ){
1f508 0a 20 20 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 . maxRoot
1f509 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a Pgno--;. }.
1f50a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d 61 assert( ma
1f50b 78 52 6f 6f 74 50 67 6e 6f 21 3d 50 45 4e 44 49 xRootPgno!=PENDI
1f50c 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
1f50d 29 20 29 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d ) );.. rc =
1f50e 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 sqlite3BtreeUpd
1f50f 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 6d 61 ateMeta(p, 4, ma
1f510 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 20 20 xRootPgno);.
1f511 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 72 65 }else{. fre
1f512 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 ePage(pPage, &rc
1f513 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 );. release
1f514 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
1f515 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 }.#endif. }els
1f516 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 73 71 6c e{. /* If sql
1f517 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 ite3BtreeDropTab
1f518 6c 65 20 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e le was called on
1f519 20 70 61 67 65 20 31 2e 0a 20 20 20 20 2a 2a 20 page 1.. **
1f51a 54 68 69 73 20 72 65 61 6c 6c 79 20 6e 65 76 65 This really neve
1f51b 72 20 73 68 6f 75 6c 64 20 68 61 70 70 65 6e 20 r should happen
1f51c 65 78 63 65 70 74 20 69 6e 20 61 20 63 6f 72 72 except in a corr
1f51d 75 70 74 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 upt. ** datab
1f51e 61 73 65 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 ase. . */.
1f51f 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c zeroPage(pPage,
1f520 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f PTF_INTKEY|PTF_
1f521 4c 45 41 46 20 29 3b 0a 20 20 20 20 72 65 6c 65 LEAF );. rele
1f522 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
1f523 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1f524 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 .}.SQLITE_PRIV
1f525 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
1f526 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 treeDropTable(Bt
1f527 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 ree *p, int iTab
1f528 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64 le, int *piMoved
1f529 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 ){. int rc;. s
1f52a 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1f52b 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 (p);. rc = btre
1f52c 65 44 72 6f 70 54 61 62 6c 65 28 70 2c 20 69 54 eDropTable(p, iT
1f52d 61 62 6c 65 2c 20 70 69 4d 6f 76 65 64 29 3b 0a able, piMoved);.
1f52e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1f52f 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
1f530 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 rc;.}.../*.** T
1f531 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 his function may
1f532 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 only be called
1f533 69 66 20 74 68 65 20 62 2d 74 72 65 65 20 63 6f if the b-tree co
1f534 6e 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 64 79 nnection already
1f535 0a 2a 2a 20 68 61 73 20 61 20 72 65 61 64 20 6f .** has a read o
1f536 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 r write transact
1f537 69 6f 6e 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 ion open on the
1f538 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 database..**.**
1f539 52 65 61 64 20 74 68 65 20 6d 65 74 61 2d 69 6e Read the meta-in
1f53a 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 6f 66 formation out of
1f53b 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 a database file
1f53c 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 . Meta[0].** is
1f53d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 the number of f
1f53e 72 65 65 20 70 61 67 65 73 20 63 75 72 72 65 6e ree pages curren
1f53f 74 6c 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 tly in the datab
1f540 61 73 65 2e 20 20 4d 65 74 61 5b 31 5d 0a 2a 2a ase. Meta[1].**
1f541 20 74 68 72 6f 75 67 68 20 6d 65 74 61 5b 31 35 through meta[15
1f542 5d 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 ] are available
1f543 66 6f 72 20 75 73 65 20 62 79 20 68 69 67 68 65 for use by highe
1f544 72 20 6c 61 79 65 72 73 2e 20 20 4d 65 74 61 5b r layers. Meta[
1f545 30 5d 0a 2a 2a 20 69 73 20 72 65 61 64 2d 6f 6e 0].** is read-on
1f546 6c 79 2c 20 74 68 65 20 6f 74 68 65 72 73 20 61 ly, the others a
1f547 72 65 20 72 65 61 64 2f 77 72 69 74 65 2e 0a 2a re read/write..*
1f548 2a 20 0a 2a 2a 20 54 68 65 20 73 63 68 65 6d 61 * .** The schema
1f549 20 6c 61 79 65 72 20 6e 75 6d 62 65 72 73 20 6d layer numbers m
1f54a 65 74 61 20 76 61 6c 75 65 73 20 64 69 66 66 65 eta values diffe
1f54b 72 65 6e 74 6c 79 2e 20 20 41 74 20 74 68 65 20 rently. At the
1f54c 73 63 68 65 6d 61 0a 2a 2a 20 6c 61 79 65 72 20 schema.** layer
1f54d 28 61 6e 64 20 74 68 65 20 53 65 74 43 6f 6f 6b (and the SetCook
1f54e 69 65 20 61 6e 64 20 52 65 61 64 43 6f 6f 6b 69 ie and ReadCooki
1f54f 65 20 6f 70 63 6f 64 65 73 29 20 74 68 65 20 6e e opcodes) the n
1f550 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 66 72 65 65 umber of.** free
1f551 20 70 61 67 65 73 20 69 73 20 6e 6f 74 20 76 69 pages is not vi
1f552 73 69 62 6c 65 2e 20 20 53 6f 20 43 6f 6f 6b 69 sible. So Cooki
1f553 65 5b 30 5d 20 69 73 20 74 68 65 20 73 61 6d 65 e[0] is the same
1f554 20 61 73 20 4d 65 74 61 5b 31 5d 2e 0a 2a 2f 0a as Meta[1]..*/.
1f555 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1f556 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
1f557 47 65 74 4d 65 74 61 28 42 74 72 65 65 20 2a 70 GetMeta(Btree *p
1f558 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 2a , int idx, u32 *
1f559 70 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 61 72 pMeta){. BtShar
1f55a 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
1f55b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 ;.. sqlite3Btre
1f55c 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 eEnter(p);. ass
1f55d 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e ert( p->inTrans>
1f55e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 TRANS_NONE );.
1f55f 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f 4f assert( SQLITE_O
1f560 4b 3d 3d 71 75 65 72 79 53 68 61 72 65 64 43 61 K==querySharedCa
1f561 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 cheTableLock(p,
1f562 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41 MASTER_ROOT, REA
1f563 44 5f 4c 4f 43 4b 29 20 29 3b 0a 20 20 61 73 73 D_LOCK) );. ass
1f564 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 ert( pBt->pPage1
1f565 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 );. assert( id
1f566 78 3e 3d 30 20 26 26 20 69 64 78 3c 3d 31 35 20 x>=0 && idx<=15
1f567 29 3b 0a 0a 20 20 2a 70 4d 65 74 61 20 3d 20 67 );.. *pMeta = g
1f568 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 et4byte(&pBt->pP
1f569 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 20 2b age1->aData[36 +
1f56a 20 69 64 78 2a 34 5d 29 3b 0a 0a 20 20 2f 2a 20 idx*4]);.. /*
1f56b 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 If auto-vacuum i
1f56c 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20 74 68 s disabled in th
1f56d 69 73 20 62 75 69 6c 64 20 61 6e 64 20 74 68 69 is build and thi
1f56e 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 s is an auto-vac
1f56f 75 75 6d 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 uum. ** databas
1f570 65 2c 20 6d 61 72 6b 20 74 68 65 20 64 61 74 61 e, mark the data
1f571 62 61 73 65 20 61 73 20 72 65 61 64 2d 6f 6e 6c base as read-onl
1f572 79 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 y. */.#ifdef SQ
1f573 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
1f574 43 55 55 4d 0a 20 20 69 66 28 20 69 64 78 3d 3d CUUM. if( idx==
1f575 42 54 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f BTREE_LARGEST_RO
1f576 4f 54 5f 50 41 47 45 20 26 26 20 2a 70 4d 65 74 OT_PAGE && *pMet
1f577 61 3e 30 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e a>0 ){. pBt->
1f578 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f btsFlags |= BTS_
1f579 52 45 41 44 5f 4f 4e 4c 59 3b 0a 20 20 7d 0a 23 READ_ONLY;. }.#
1f57a 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 endif.. sqlite3
1f57b 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 7d BtreeLeave(p);.}
1f57c 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 65 ../*.** Write me
1f57d 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 ta-information b
1f57e 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 ack into the dat
1f57f 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d 20 abase. Meta[0]
1f580 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20 is.** read-only
1f581 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 77 and may not be w
1f582 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 ritten..*/.SQLIT
1f583 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1f584 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
1f585 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69 Meta(Btree *p, i
1f586 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65 74 nt idx, u32 iMet
1f587 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a a){. BtShared *
1f588 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1f589 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
1f58a 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 P1;. int rc;.
1f58b 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20 26 assert( idx>=1 &
1f58c 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 73 & idx<=15 );. s
1f58d 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1f58e 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 (p);. assert( p
1f58f 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
1f590 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 _WRITE );. asse
1f591 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 21 rt( pBt->pPage1!
1f592 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d 20 70 42 =0 );. pP1 = pB
1f593 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
1f594 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1f595 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e PagerWrite(pBt->
1f596 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
1f597 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1f598 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 75 74 TE_OK ){. put
1f599 34 62 79 74 65 28 26 70 50 31 5b 33 36 20 2b 20 4byte(&pP1[36 +
1f59a 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61 29 3b 0a idx*4], iMeta);.
1f59b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1f59c 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
1f59d 20 20 20 69 66 28 20 69 64 78 3d 3d 42 54 52 45 if( idx==BTRE
1f59e 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 29 7b E_INCR_VACUUM ){
1f59f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1f5a0 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 7c Bt->autoVacuum |
1f5a1 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a 20 20 | iMeta==0 );.
1f5a2 20 20 20 20 61 73 73 65 72 74 28 20 69 4d 65 74 assert( iMet
1f5a3 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 3d 3d 31 a==0 || iMeta==1
1f5a4 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 );. pBt->i
1f5a5 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 75 38 29 ncrVacuum = (u8)
1f5a6 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a 23 65 6e iMeta;. }.#en
1f5a7 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 dif. }. sqlite
1f5a8 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1f5a9 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1f5aa 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1f5ab 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 2f MIT_BTREECOUNT./
1f5ac 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 *.** The first a
1f5ad 72 67 75 6d 65 6e 74 2c 20 70 43 75 72 2c 20 69 rgument, pCur, i
1f5ae 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 s a cursor opene
1f5af 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74 72 65 65 d on some b-tree
1f5b0 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a 2a 20 6e . Count the.** n
1f5b1 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
1f5b2 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 61 in the b-tree a
1f5b3 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65 73 nd write the res
1f5b4 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74 72 79 2e ult to *pnEntry.
1f5b5 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
1f5b6 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
1f5b7 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 the operation is
1f5b8 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 65 78 successfully ex
1f5b9 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 ecuted. .** Othe
1f5ba 72 77 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 rwise, if an err
1f5bb 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 or is encountere
1f5bc 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f 20 65 72 d (i.e. an IO er
1f5bd 72 6f 72 20 6f 72 20 64 61 74 61 62 61 73 65 0a ror or database.
1f5be 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 29 20 61 ** corruption) a
1f5bf 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 n SQLite error c
1f5c0 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
1f5c1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1f5c2 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
1f5c3 72 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f reeCount(BtCurso
1f5c4 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 6e r *pCur, i64 *pn
1f5c5 45 6e 74 72 79 29 7b 0a 20 20 69 36 34 20 6e 45 Entry){. i64 nE
1f5c6 6e 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20 ntry = 0;
1f5c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1f5c8 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 * Value to retur
1f5c9 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a 2f n in *pnEntry */
1f5ca 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
1f5cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f5cc 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
1f5cd 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69 66 28 n code */.. if(
1f5ce 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d pCur->pgnoRoot=
1f5cf 3d 30 20 29 7b 0a 20 20 20 20 2a 70 6e 45 6e 74 =0 ){. *pnEnt
1f5d0 72 79 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 ry = 0;. retu
1f5d1 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
1f5d2 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 }. rc = moveToR
1f5d3 6f 6f 74 28 70 43 75 72 29 3b 0a 0a 20 20 2f 2a oot(pCur);.. /*
1f5d4 20 55 6e 6c 65 73 73 20 61 6e 20 65 72 72 6f 72 Unless an error
1f5d5 20 6f 63 63 75 72 73 2c 20 74 68 65 20 66 6f 6c occurs, the fol
1f5d6 6c 6f 77 69 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 lowing loop runs
1f5d7 20 6f 6e 65 20 69 74 65 72 61 74 69 6f 6e 20 66 one iteration f
1f5d8 6f 72 20 65 61 63 68 0a 20 20 2a 2a 20 70 61 67 or each. ** pag
1f5d9 65 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 20 e in the B-Tree
1f5da 73 74 72 75 63 74 75 72 65 20 28 6e 6f 74 20 69 structure (not i
1f5db 6e 63 6c 75 64 69 6e 67 20 6f 76 65 72 66 6c 6f ncluding overflo
1f5dc 77 20 70 61 67 65 73 29 2e 20 0a 20 20 2a 2f 0a w pages). . */.
1f5dd 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c while( rc==SQL
1f5de 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e ITE_OK ){. in
1f5df 74 20 69 49 64 78 3b 20 20 20 20 20 20 20 20 20 t iIdx;
1f5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f5e1 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 68 69 /* Index of chi
1f5e2 6c 64 20 6e 6f 64 65 20 69 6e 20 70 61 72 65 6e ld node in paren
1f5e3 74 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 t */. MemPage
1f5e4 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 *pPage;
1f5e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
1f5e6 75 72 72 65 6e 74 20 70 61 67 65 20 6f 66 20 74 urrent page of t
1f5e7 68 65 20 62 2d 74 72 65 65 20 2a 2f 0a 0a 20 20 he b-tree */..
1f5e8 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
1f5e9 61 20 6c 65 61 66 20 70 61 67 65 20 6f 72 20 74 a leaf page or t
1f5ea 68 65 20 74 72 65 65 20 69 73 20 6e 6f 74 20 61 he tree is not a
1f5eb 6e 20 69 6e 74 2d 6b 65 79 20 74 72 65 65 2c 20 n int-key tree,
1f5ec 74 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74 68 69 then . ** thi
1f5ed 73 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 s page contains
1f5ee 63 6f 75 6e 74 61 62 6c 65 20 65 6e 74 72 69 65 countable entrie
1f5ef 73 2e 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 s. Increment the
1f5f0 20 65 6e 74 72 79 20 63 6f 75 6e 74 65 72 0a 20 entry counter.
1f5f1 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 6c ** accordingl
1f5f2 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 y.. */. pP
1f5f3 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
1f5f4 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b ge[pCur->iPage];
1f5f5 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
1f5f6 6c 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e leaf || !pPage->
1f5f7 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 intKey ){.
1f5f8 6e 45 6e 74 72 79 20 2b 3d 20 70 50 61 67 65 2d nEntry += pPage-
1f5f9 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 7d 0a 0a 20 >nCell;. }..
1f5fa 20 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 61 /* pPage is a
1f5fb 20 6c 65 61 66 20 6e 6f 64 65 2e 20 54 68 69 73 leaf node. This
1f5fc 20 6c 6f 6f 70 20 6e 61 76 69 67 61 74 65 73 20 loop navigates
1f5fd 74 68 65 20 63 75 72 73 6f 72 20 73 6f 20 74 68 the cursor so th
1f5fe 61 74 20 69 74 20 0a 20 20 20 20 2a 2a 20 70 6f at it . ** po
1f5ff 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 ints to the firs
1f600 74 20 69 6e 74 65 72 69 6f 72 20 63 65 6c 6c 20 t interior cell
1f601 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 that it points t
1f602 6f 20 74 68 65 20 70 61 72 65 6e 74 20 6f 66 0a o the parent of.
1f603 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 78 74 20 ** the next
1f604 70 61 67 65 20 69 6e 20 74 68 65 20 74 72 65 65 page in the tree
1f605 20 74 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 that has not ye
1f606 74 20 62 65 65 6e 20 76 69 73 69 74 65 64 2e 20 t been visited.
1f607 54 68 65 0a 20 20 20 20 2a 2a 20 70 43 75 72 2d The. ** pCur-
1f608 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
1f609 67 65 5d 20 76 61 6c 75 65 20 69 73 20 73 65 74 ge] value is set
1f60a 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 to the index of
1f60b 20 74 68 65 20 70 61 72 65 6e 74 20 63 65 6c 6c the parent cell
1f60c 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 . ** of the p
1f60d 61 67 65 2c 20 6f 72 20 74 6f 20 74 68 65 20 6e age, or to the n
1f60e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 umber of cells i
1f60f 6e 20 74 68 65 20 70 61 67 65 20 69 66 20 74 68 n the page if th
1f610 65 20 6e 65 78 74 20 70 61 67 65 0a 20 20 20 20 e next page.
1f611 2a 2a 20 74 6f 20 76 69 73 69 74 20 69 73 20 74 ** to visit is t
1f612 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f he right-child o
1f613 66 20 69 74 73 20 70 61 72 65 6e 74 2e 0a 20 20 f its parent..
1f614 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 61 **. ** If a
1f615 6c 6c 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 ll pages in the
1f616 74 72 65 65 20 68 61 76 65 20 62 65 65 6e 20 76 tree have been v
1f617 69 73 69 74 65 64 2c 20 72 65 74 75 72 6e 20 53 isited, return S
1f618 51 4c 49 54 45 5f 4f 4b 20 74 6f 20 74 68 65 0a QLITE_OK to the.
1f619 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 0a 20 ** caller..
1f61a 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 */. if( pP
1f61b 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
1f61c 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 do {.
1f61d 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d if( pCur->iPage=
1f61e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
1f61f 2f 2a 20 41 6c 6c 20 70 61 67 65 73 20 6f 66 20 /* All pages of
1f620 74 68 65 20 62 2d 74 72 65 65 20 68 61 76 65 20 the b-tree have
1f621 62 65 65 6e 20 76 69 73 69 74 65 64 2e 20 52 65 been visited. Re
1f622 74 75 72 6e 20 73 75 63 63 65 73 73 66 75 6c 6c turn successfull
1f623 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 y. */.
1f624 2a 70 6e 45 6e 74 72 79 20 3d 20 6e 45 6e 74 72 *pnEntry = nEntr
1f625 79 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 y;. ret
1f626 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
1f627 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1f628 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43 moveToParent(pC
1f629 75 72 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c ur);. }whil
1f62a 65 20 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b e ( pCur->aiIdx[
1f62b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 pCur->iPage]>=pC
1f62c 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
1f62d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 >iPage]->nCell )
1f62e 3b 0a 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 ;.. pCur->a
1f62f 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
1f630 5d 2b 2b 3b 0a 20 20 20 20 20 20 70 50 61 67 65 ]++;. pPage
1f631 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
1f632 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
1f633 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 73 63 }.. /* Desc
1f634 65 6e 64 20 74 6f 20 74 68 65 20 63 68 69 6c 64 end to the child
1f635 20 6e 6f 64 65 20 6f 66 20 74 68 65 20 63 65 6c node of the cel
1f636 6c 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f l that the curso
1f637 72 20 63 75 72 72 65 6e 74 6c 79 20 0a 20 20 20 r currently .
1f638 20 2a 2a 20 70 6f 69 6e 74 73 20 61 74 2e 20 54 ** points at. T
1f639 68 69 73 20 69 73 20 74 68 65 20 72 69 67 68 74 his is the right
1f63a 2d 63 68 69 6c 64 20 69 66 20 28 69 49 64 78 3d -child if (iIdx=
1f63b 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 2e 0a =pPage->nCell)..
1f63c 20 20 20 20 2a 2f 0a 20 20 20 20 69 49 64 78 20 */. iIdx
1f63d 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 = pCur->aiIdx[pC
1f63e 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
1f63f 69 66 28 20 69 49 64 78 3d 3d 70 50 61 67 65 2d if( iIdx==pPage-
1f640 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 >nCell ){.
1f641 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 rc = moveToChild
1f642 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 (pCur, get4byte(
1f643 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 &pPage->aData[pP
1f644 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 age->hdrOffset+8
1f645 5d 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ]));. }else{.
1f646 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 rc = moveT
1f647 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 oChild(pCur, get
1f648 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 4byte(findCell(p
1f649 50 61 67 65 2c 20 69 49 64 78 29 29 29 3b 0a 20 Page, iIdx)));.
1f64a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 }. }.. /* A
1f64b 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 n error has occu
1f64c 72 72 65 64 2e 20 52 65 74 75 72 6e 20 61 6e 20 rred. Return an
1f64d 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 error code. */.
1f64e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 return rc;.}.#e
1f64f 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
1f650 72 6e 20 74 68 65 20 70 61 67 65 72 20 61 73 73 rn the pager ass
1f651 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 42 ociated with a B
1f652 54 72 65 65 2e 20 20 54 68 69 73 20 72 6f 75 74 Tree. This rout
1f653 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 0a ine is used for.
1f654 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 ** testing and d
1f655 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a ebugging only..*
1f656 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1f657 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65 33 42 Pager *sqlite3B
1f658 74 72 65 65 50 61 67 65 72 28 42 74 72 65 65 20 treePager(Btree
1f659 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d *p){. return p-
1f65a 3e 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 7d 0a >pBt->pPager;.}.
1f65b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f65c 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 OMIT_INTEGRITY_C
1f65d 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e HECK./*.** Appen
1f65e 64 20 61 20 6d 65 73 73 61 67 65 20 74 6f 20 74 d a message to t
1f65f 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
1f660 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 string..*/.stat
1f661 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41 70 70 ic void checkApp
1f662 65 6e 64 4d 73 67 28 0a 20 20 49 6e 74 65 67 72 endMsg(. Integr
1f663 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 0a 20 ityCk *pCheck,.
1f664 20 63 68 61 72 20 2a 7a 4d 73 67 31 2c 0a 20 20 char *zMsg1,.
1f665 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
1f666 6d 61 74 2c 0a 20 20 2e 2e 2e 0a 29 7b 0a 20 20 mat,. ....){.
1f667 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 va_list ap;. if
1f668 28 20 21 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 ( !pCheck->mxErr
1f669 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 43 68 ) return;. pCh
1f66a 65 63 6b 2d 3e 6d 78 45 72 72 2d 2d 3b 0a 20 20 eck->mxErr--;.
1f66b 70 43 68 65 63 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a pCheck->nErr++;.
1f66c 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
1f66d 46 6f 72 6d 61 74 29 3b 0a 20 20 69 66 28 20 70 Format);. if( p
1f66e 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e 6e 43 Check->errMsg.nC
1f66f 68 61 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 har ){. sqlit
1f670 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
1f671 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 (&pCheck->errMsg
1f672 2c 20 22 5c 6e 22 2c 20 31 29 3b 0a 20 20 7d 0a , "\n", 1);. }.
1f673 20 20 69 66 28 20 7a 4d 73 67 31 20 29 7b 0a 20 if( zMsg1 ){.
1f674 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
1f675 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b umAppend(&pCheck
1f676 2d 3e 65 72 72 4d 73 67 2c 20 7a 4d 73 67 31 2c ->errMsg, zMsg1,
1f677 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 -1);. }. sqli
1f678 74 65 33 56 58 50 72 69 6e 74 66 28 26 70 43 68 te3VXPrintf(&pCh
1f679 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 31 2c 20 eck->errMsg, 1,
1f67a 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
1f67b 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 va_end(ap);. if
1f67c 28 20 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 ( pCheck->errMsg
1f67d 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b .mallocFailed ){
1f67e 0a 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c . pCheck->mal
1f67f 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
1f680 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 }.}.#endif /* S
1f681 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
1f682 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 RITY_CHECK */..#
1f683 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f684 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 IT_INTEGRITY_CHE
1f685 43 4b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e CK../*.** Return
1f686 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 non-zero if the
1f687 20 62 69 74 20 69 6e 20 74 68 65 20 49 6e 74 65 bit in the Inte
1f688 67 72 69 74 79 43 6b 2e 61 50 67 52 65 66 5b 5d grityCk.aPgRef[]
1f689 20 61 72 72 61 79 20 74 68 61 74 0a 2a 2a 20 63 array that.** c
1f68a 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 70 61 orresponds to pa
1f68b 67 65 20 69 50 67 20 69 73 20 61 6c 72 65 61 64 ge iPg is alread
1f68c 79 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 y set..*/.static
1f68d 20 69 6e 74 20 67 65 74 50 61 67 65 52 65 66 65 int getPageRefe
1f68e 72 65 6e 63 65 64 28 49 6e 74 65 67 72 69 74 79 renced(Integrity
1f68f 43 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f Ck *pCheck, Pgno
1f690 20 69 50 67 29 7b 0a 20 20 61 73 73 65 72 74 28 iPg){. assert(
1f691 20 69 50 67 3c 3d 70 43 68 65 63 6b 2d 3e 6e 50 iPg<=pCheck->nP
1f692 61 67 65 20 26 26 20 73 69 7a 65 6f 66 28 70 43 age && sizeof(pC
1f693 68 65 63 6b 2d 3e 61 50 67 52 65 66 5b 30 5d 29 heck->aPgRef[0])
1f694 3d 3d 31 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ==1 );. return
1f695 28 70 43 68 65 63 6b 2d 3e 61 50 67 52 65 66 5b (pCheck->aPgRef[
1f696 69 50 67 2f 38 5d 20 26 20 28 31 20 3c 3c 20 28 iPg/8] & (1 << (
1f697 69 50 67 20 26 20 30 78 30 37 29 29 29 3b 0a 7d iPg & 0x07)));.}
1f698 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
1f699 62 69 74 20 69 6e 20 74 68 65 20 49 6e 74 65 67 bit in the Integ
1f69a 72 69 74 79 43 6b 2e 61 50 67 52 65 66 5b 5d 20 rityCk.aPgRef[]
1f69b 61 72 72 61 79 20 74 68 61 74 20 63 6f 72 72 65 array that corre
1f69c 73 70 6f 6e 64 73 20 74 6f 20 70 61 67 65 20 69 sponds to page i
1f69d 50 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f Pg..*/.static vo
1f69e 69 64 20 73 65 74 50 61 67 65 52 65 66 65 72 65 id setPageRefere
1f69f 6e 63 65 64 28 49 6e 74 65 67 72 69 74 79 43 6b nced(IntegrityCk
1f6a0 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f 20 69 *pCheck, Pgno i
1f6a1 50 67 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 Pg){. assert( i
1f6a2 50 67 3c 3d 70 43 68 65 63 6b 2d 3e 6e 50 61 67 Pg<=pCheck->nPag
1f6a3 65 20 26 26 20 73 69 7a 65 6f 66 28 70 43 68 65 e && sizeof(pChe
1f6a4 63 6b 2d 3e 61 50 67 52 65 66 5b 30 5d 29 3d 3d ck->aPgRef[0])==
1f6a5 31 20 29 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 61 1 );. pCheck->a
1f6a6 50 67 52 65 66 5b 69 50 67 2f 38 5d 20 7c 3d 20 PgRef[iPg/8] |=
1f6a7 28 31 20 3c 3c 20 28 69 50 67 20 26 20 30 78 30 (1 << (iPg & 0x0
1f6a8 37 29 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 7));.}.../*.** A
1f6a9 64 64 20 31 20 74 6f 20 74 68 65 20 72 65 66 65 dd 1 to the refe
1f6aa 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 rence count for
1f6ab 70 61 67 65 20 69 50 61 67 65 2e 20 20 49 66 20 page iPage. If
1f6ac 74 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f this is the seco
1f6ad 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 nd.** reference
1f6ae 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61 64 64 to the page, add
1f6af 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
1f6b0 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a 45 72 e to pCheck->zEr
1f6b1 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 rMsg..** Return
1f6b2 31 20 69 66 20 74 68 65 72 65 20 61 72 65 20 32 1 if there are 2
1f6b3 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 65 72 65 ore more refere
1f6b4 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 nces to the page
1f6b5 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 69 66 20 and 0 if.** if
1f6b6 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 this is the firs
1f6b7 74 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 t reference to t
1f6b8 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 he page..**.** A
1f6b9 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 74 20 74 lso check that t
1f6ba 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 he page number i
1f6bb 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a 2f 0a s in bounds..*/.
1f6bc 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b static int check
1f6bd 52 65 66 28 49 6e 74 65 67 72 69 74 79 43 6b 20 Ref(IntegrityCk
1f6be 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f 20 69 50 *pCheck, Pgno iP
1f6bf 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f 6e 74 age, char *zCont
1f6c0 65 78 74 29 7b 0a 20 20 69 66 28 20 69 50 61 67 ext){. if( iPag
1f6c1 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b e==0 ) return 1;
1f6c2 0a 20 20 69 66 28 20 69 50 61 67 65 3e 70 43 68 . if( iPage>pCh
1f6c3 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b 0a 20 20 eck->nPage ){.
1f6c4 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
1f6c5 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 (pCheck, zContex
1f6c6 74 2c 20 22 69 6e 76 61 6c 69 64 20 70 61 67 65 t, "invalid page
1f6c7 20 6e 75 6d 62 65 72 20 25 64 22 2c 20 69 50 61 number %d", iPa
1f6c8 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ge);. return
1f6c9 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 74 1;. }. if( get
1f6ca 50 61 67 65 52 65 66 65 72 65 6e 63 65 64 28 70 PageReferenced(p
1f6cb 43 68 65 63 6b 2c 20 69 50 61 67 65 29 20 29 7b Check, iPage) ){
1f6cc 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 . checkAppend
1f6cd 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e Msg(pCheck, zCon
1f6ce 74 65 78 74 2c 20 22 32 6e 64 20 72 65 66 65 72 text, "2nd refer
1f6cf 65 6e 63 65 20 74 6f 20 70 61 67 65 20 25 64 22 ence to page %d"
1f6d0 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65 , iPage);. re
1f6d1 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 73 65 turn 1;. }. se
1f6d2 74 50 61 67 65 52 65 66 65 72 65 6e 63 65 64 28 tPageReferenced(
1f6d3 70 43 68 65 63 6b 2c 20 69 50 61 67 65 29 3b 0a pCheck, iPage);.
1f6d4 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23 return 0;.}..#
1f6d5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f6d6 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a IT_AUTOVACUUM./*
1f6d7 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 .** Check that t
1f6d8 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 he entry in the
1f6d9 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20 pointer-map for
1f6da 70 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73 page iChild maps
1f6db 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61 to .** page iPa
1f6dc 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79 rent, pointer ty
1f6dd 70 65 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e pe ptrType. If n
1f6de 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72 ot, append an er
1f6df 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 ror message.** t
1f6e0 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 o pCheck..*/.sta
1f6e1 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74 tic void checkPt
1f6e2 72 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74 rmap(. Integrit
1f6e3 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f yCk *pCheck, /
1f6e4 2a 20 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 * Integrity chec
1f6e5 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50 k context */. P
1f6e6 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 gno iChild,
1f6e7 20 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70 /* Child p
1f6e8 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 age number */.
1f6e9 75 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 u8 eType,
1f6ea 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 /* Expect
1f6eb 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74 ed pointer map t
1f6ec 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 ype */. Pgno iP
1f6ed 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 arent,
1f6ee 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e /* Expected poin
1f6ef 74 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70 ter map parent p
1f6f0 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 age number */.
1f6f1 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 char *zContext
1f6f2 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 /* Contex
1f6f3 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75 t description (u
1f6f4 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73 sed for error ms
1f6f5 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 g) */.){. int r
1f6f6 63 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54 c;. u8 ePtrmapT
1f6f7 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 ype;. Pgno iPtr
1f6f8 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63 mapParent;.. rc
1f6f9 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43 68 = ptrmapGet(pCh
1f6fa 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64 eck->pBt, iChild
1f6fb 2c 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20 , &ePtrmapType,
1f6fc 26 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b &iPtrmapParent);
1f6fd 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
1f6fe 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 E_OK ){. if(
1f6ff 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
1f700 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 || rc==SQLITE_I
1f701 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 OERR_NOMEM ) pCh
1f702 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 eck->mallocFaile
1f703 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b d = 1;. check
1f704 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b AppendMsg(pCheck
1f705 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 , zContext, "Fai
1f706 6c 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d led to read ptrm
1f707 61 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 ap key=%d", iChi
1f708 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b ld);. return;
1f709 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 . }.. if( ePtr
1f70a 6d 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c mapType!=eType |
1f70b 7c 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 | iPtrmapParent!
1f70c 3d 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 =iParent ){.
1f70d 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
1f70e 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c Check, zContext,
1f70f 20 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72 . "Bad ptr
1f710 20 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 map entry key=%
1f711 64 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 d expected=(%d,%
1f712 64 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c d) got=(%d,%d)",
1f713 20 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 . iChild,
1f714 65 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 eType, iParent,
1f715 65 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 ePtrmapType, iPt
1f716 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d rmapParent);. }
1f717 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
1f718 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 Check the integ
1f719 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 rity of the free
1f71a 6c 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 list or of an ov
1f71b 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 erflow page list
1f71c 2e 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 ..** Verify that
1f71d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
1f71e 61 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 ages on the list
1f71f 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 is N..*/.static
1f720 20 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 void checkList(
1f721 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a . IntegrityCk *
1f722 70 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 pCheck, /* Inte
1f723 67 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 grity checking c
1f724 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 ontext */. int
1f725 69 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 isFreeList,
1f726 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 /* True for a
1f727 66 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 freelist. False
1f728 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 for overflow pa
1f729 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 ge list */. int
1f72a 20 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 iPage,
1f72b 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 /* Page numbe
1f72c 72 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 r for first page
1f72d 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a in the list */.
1f72e 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 int N,
1f72f 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 /* Expec
1f730 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 ted number of pa
1f731 67 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 ges in the list
1f732 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 */. char *zCont
1f733 65 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f ext /* Co
1f734 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 ntext for error
1f735 6d 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 messages */.){.
1f736 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 int i;. int ex
1f737 70 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e pected = N;. in
1f738 74 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 t iFirst = iPage
1f739 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e ;. while( N-- >
1f73a 20 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 0 && pCheck->mx
1f73b 45 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 Err ){. DbPag
1f73c 65 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 e *pOvflPage;.
1f73d 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
1f73e 2a 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 *pOvflData;.
1f73f 69 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 if( iPage<1 ){.
1f740 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 checkAppend
1f741 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e Msg(pCheck, zCon
1f742 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 text,. "
1f743 25 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d %d of %d pages m
1f744 69 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 issing from over
1f745 66 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 flow list starti
1f746 6e 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 ng at %d",.
1f747 20 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 N+1, expect
1f748 65 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 ed, iFirst);.
1f749 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1f74a 20 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 if( checkRef
1f74b 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 (pCheck, iPage,
1f74c 7a 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 zContext) ) brea
1f74d 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 k;. if( sqlit
1f74e 65 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63 e3PagerGet(pChec
1f74f 6b 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f k->pPager, (Pgno
1f750 29 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 )iPage, &pOvflPa
1f751 67 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 ge) ){. che
1f752 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
1f753 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 ck, zContext, "f
1f754 61 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 ailed to get pag
1f755 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 e %d", iPage);.
1f756 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1f757 7d 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 }. pOvflData
1f758 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
1f759 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 *)sqlite3PagerG
1f75a 65 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 etData(pOvflPage
1f75b 29 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 );. if( isFre
1f75c 65 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 eList ){. i
1f75d 6e 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 nt n = get4byte(
1f75e 26 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a &pOvflData[4]);.
1f75f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1f760 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
1f761 20 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d if( pCheck-
1f762 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d >pBt->autoVacuum
1f763 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 ){. chec
1f764 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 kPtrmap(pCheck,
1f765 69 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 iPage, PTRMAP_FR
1f766 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 EEPAGE, 0, zCont
1f767 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 ext);. }.#e
1f768 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e ndif. if( n
1f769 3e 28 69 6e 74 29 70 43 68 65 63 6b 2d 3e 70 42 >(int)pCheck->pB
1f76a 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d t->usableSize/4-
1f76b 32 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 2 ){. che
1f76c 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
1f76d 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 ck, zContext,.
1f76e 20 20 20 20 20 20 20 20 20 22 66 72 65 65 6c 69 "freeli
1f76f 73 74 20 6c 65 61 66 20 63 6f 75 6e 74 20 74 6f st leaf count to
1f770 6f 20 62 69 67 20 6f 6e 20 70 61 67 65 20 25 64 o big on page %d
1f771 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 ", iPage);.
1f772 20 20 20 4e 2d 2d 3b 0a 20 20 20 20 20 20 7d 65 N--;. }e
1f773 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 lse{. for
1f774 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b (i=0; i<n; i++){
1f775 0a 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 . Pgno
1f776 69 46 72 65 65 50 61 67 65 20 3d 20 67 65 74 34 iFreePage = get4
1f777 62 79 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b byte(&pOvflData[
1f778 38 2b 69 2a 34 5d 29 3b 0a 23 69 66 6e 64 65 66 8+i*4]);.#ifndef
1f779 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1f77a 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 20 20 OVACUUM.
1f77b 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 if( pCheck->pB
1f77c 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b t->autoVacuum ){
1f77d 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 65 . che
1f77e 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c ckPtrmap(pCheck,
1f77f 20 69 46 72 65 65 50 61 67 65 2c 20 50 54 52 4d iFreePage, PTRM
1f780 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 AP_FREEPAGE, 0,
1f781 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 zContext);.
1f782 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
1f783 20 20 20 20 20 20 20 20 63 68 65 63 6b 52 65 66 checkRef
1f784 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61 (pCheck, iFreePa
1f785 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 ge, zContext);.
1f786 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1f787 20 4e 20 2d 3d 20 6e 3b 0a 20 20 20 20 20 20 7d N -= n;. }
1f788 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 . }.#ifndef S
1f789 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
1f78a 41 43 55 55 4d 0a 20 20 20 20 65 6c 73 65 7b 0a ACUUM. else{.
1f78b 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 /* If this
1f78c 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 database suppor
1f78d 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 ts auto-vacuum a
1f78e 6e 64 20 69 50 61 67 65 20 69 73 20 6e 6f 74 20 nd iPage is not
1f78f 74 68 65 20 6c 61 73 74 0a 20 20 20 20 20 20 2a the last. *
1f790 2a 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 6f * page in this o
1f791 76 65 72 66 6c 6f 77 20 6c 69 73 74 2c 20 63 68 verflow list, ch
1f792 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 6f 69 eck that the poi
1f793 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 nter-map entry f
1f794 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 or. ** the
1f795 66 6f 6c 6c 6f 77 69 6e 67 20 70 61 67 65 20 6d following page m
1f796 61 74 63 68 65 73 20 69 50 61 67 65 2e 0a 20 20 atches iPage..
1f797 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
1f798 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 pCheck->pBt->au
1f799 74 6f 56 61 63 75 75 6d 20 26 26 20 4e 3e 30 20 toVacuum && N>0
1f79a 29 7b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 67 ){. i = g
1f79b 65 74 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74 et4byte(pOvflDat
1f79c 61 29 3b 0a 20 20 20 20 20 20 20 20 63 68 65 63 a);. chec
1f79d 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 kPtrmap(pCheck,
1f79e 69 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c i, PTRMAP_OVERFL
1f79f 4f 57 32 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e OW2, iPage, zCon
1f7a0 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 text);. }.
1f7a1 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
1f7a2 69 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 iPage = get4byte
1f7a3 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 (pOvflData);.
1f7a4 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
1f7a5 65 66 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 ef(pOvflPage);.
1f7a6 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 }.}.#endif /* S
1f7a7 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
1f7a8 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 RITY_CHECK */..#
1f7a9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f7aa 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 IT_INTEGRITY_CHE
1f7ab 43 4b 0a 2f 2a 0a 2a 2a 20 44 6f 20 76 61 72 69 CK./*.** Do vari
1f7ac 6f 75 73 20 73 61 6e 69 74 79 20 63 68 65 63 6b ous sanity check
1f7ad 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 s on a single pa
1f7ae 67 65 20 6f 66 20 61 20 74 72 65 65 2e 20 20 52 ge of a tree. R
1f7af 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 74 72 65 eturn.** the tre
1f7b0 65 20 64 65 70 74 68 2e 20 20 52 6f 6f 74 20 70 e depth. Root p
1f7b1 61 67 65 73 20 72 65 74 75 72 6e 20 30 2e 20 20 ages return 0.
1f7b2 50 61 72 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20 Parents of root
1f7b3 70 61 67 65 73 0a 2a 2a 20 72 65 74 75 72 6e 20 pages.** return
1f7b4 31 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 1, and so forth.
1f7b5 0a 2a 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 68 .** .** These ch
1f7b6 65 63 6b 73 20 61 72 65 20 64 6f 6e 65 3a 0a 2a ecks are done:.*
1f7b7 2a 0a 2a 2a 20 20 20 20 20 20 31 2e 20 20 4d 61 *.** 1. Ma
1f7b8 6b 65 20 73 75 72 65 20 74 68 61 74 20 63 65 6c ke sure that cel
1f7b9 6c 73 20 61 6e 64 20 66 72 65 65 62 6c 6f 63 6b ls and freeblock
1f7ba 73 20 64 6f 20 6e 6f 74 20 6f 76 65 72 6c 61 70 s do not overlap
1f7bb 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 75 74 .** but
1f7bc 20 63 6f 6d 62 69 6e 65 20 74 6f 20 63 6f 6d 70 combine to comp
1f7bd 6c 65 74 65 6c 79 20 63 6f 76 65 72 20 74 68 65 letely cover the
1f7be 20 70 61 67 65 2e 0a 2a 2a 20 20 4e 4f 20 20 32 page..** NO 2
1f7bf 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 63 65 6c . Make sure cel
1f7c0 6c 20 6b 65 79 73 20 61 72 65 20 69 6e 20 6f 72 l keys are in or
1f7c1 64 65 72 2e 0a 2a 2a 20 20 4e 4f 20 20 33 2e 20 der..** NO 3.
1f7c2 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 Make sure no ke
1f7c3 79 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f y is less than o
1f7c4 72 20 65 71 75 61 6c 20 74 6f 20 7a 4c 6f 77 65 r equal to zLowe
1f7c5 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 4e 4f 20 20 rBound..** NO
1f7c6 34 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 4. Make sure no
1f7c7 20 6b 65 79 20 69 73 20 67 72 65 61 74 65 72 20 key is greater
1f7c8 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
1f7c9 20 7a 55 70 70 65 72 42 6f 75 6e 64 2e 0a 2a 2a zUpperBound..**
1f7ca 20 20 20 20 20 20 35 2e 20 20 43 68 65 63 6b 20 5. Check
1f7cb 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 the integrity of
1f7cc 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
1f7cd 0a 2a 2a 20 20 20 20 20 20 36 2e 20 20 52 65 63 .** 6. Rec
1f7ce 75 72 73 69 76 65 6c 79 20 63 61 6c 6c 20 63 68 ursively call ch
1f7cf 65 63 6b 54 72 65 65 50 61 67 65 20 6f 6e 20 61 eckTreePage on a
1f7d0 6c 6c 20 63 68 69 6c 64 72 65 6e 2e 0a 2a 2a 20 ll children..**
1f7d1 20 20 20 20 20 37 2e 20 20 56 65 72 69 66 79 20 7. Verify
1f7d2 74 68 61 74 20 74 68 65 20 64 65 70 74 68 20 6f that the depth o
1f7d3 66 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 69 f all children i
1f7d4 73 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 20 20 s the same..**
1f7d5 20 20 20 20 38 2e 20 20 4d 61 6b 65 20 73 75 72 8. Make sur
1f7d6 65 20 74 68 69 73 20 70 61 67 65 20 69 73 20 61 e this page is a
1f7d7 74 20 6c 65 61 73 74 20 33 33 25 20 66 75 6c 6c t least 33% full
1f7d8 20 6f 72 20 65 6c 73 65 20 69 74 20 69 73 0a 2a or else it is.*
1f7d9 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 72 * the r
1f7da 6f 6f 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e oot of the tree.
1f7db 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
1f7dc 68 65 63 6b 54 72 65 65 50 61 67 65 28 0a 20 20 heckTreePage(.
1f7dd 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 IntegrityCk *pCh
1f7de 65 63 6b 2c 20 20 2f 2a 20 43 6f 6e 74 65 78 74 eck, /* Context
1f7df 20 66 6f 72 20 74 68 65 20 73 61 6e 69 74 79 20 for the sanity
1f7e0 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 check */. int i
1f7e1 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 Page,
1f7e2 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
1f7e3 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 63 of the page to c
1f7e4 68 65 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a heck */. char *
1f7e5 7a 50 61 72 65 6e 74 43 6f 6e 74 65 78 74 2c 20 zParentContext,
1f7e6 2f 2a 20 50 61 72 65 6e 74 20 63 6f 6e 74 65 78 /* Parent contex
1f7e7 74 20 2a 2f 0a 20 20 69 36 34 20 2a 70 6e 50 61 t */. i64 *pnPa
1f7e8 72 65 6e 74 4d 69 6e 4b 65 79 2c 20 0a 20 20 69 rentMinKey, . i
1f7e9 36 34 20 2a 70 6e 50 61 72 65 6e 74 4d 61 78 4b 64 *pnParentMaxK
1f7ea 65 79 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 ey.){. MemPage
1f7eb 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69 2c *pPage;. int i,
1f7ec 20 72 63 2c 20 64 65 70 74 68 2c 20 64 32 2c 20 rc, depth, d2,
1f7ed 70 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 pgno, cnt;. int
1f7ee 20 68 64 72 2c 20 63 65 6c 6c 53 74 61 72 74 3b hdr, cellStart;
1f7ef 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 . int nCell;.
1f7f0 75 38 20 2a 64 61 74 61 3b 0a 20 20 42 74 53 68 u8 *data;. BtSh
1f7f1 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 6e 74 ared *pBt;. int
1f7f2 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 usableSize;. c
1f7f3 68 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31 30 30 har zContext[100
1f7f4 5d 3b 0a 20 20 63 68 61 72 20 2a 68 69 74 20 3d ];. char *hit =
1f7f5 20 30 3b 0a 20 20 69 36 34 20 6e 4d 69 6e 4b 65 0;. i64 nMinKe
1f7f6 79 20 3d 20 30 3b 0a 20 20 69 36 34 20 6e 4d 61 y = 0;. i64 nMa
1f7f7 78 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 73 71 6c xKey = 0;.. sql
1f7f8 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 ite3_snprintf(si
1f7f9 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 zeof(zContext),
1f7fa 7a 43 6f 6e 74 65 78 74 2c 20 22 50 61 67 65 20 zContext, "Page
1f7fb 25 64 3a 20 22 2c 20 69 50 61 67 65 29 3b 0a 0a %d: ", iPage);..
1f7fc 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 /* Check that
1f7fd 74 68 65 20 70 61 67 65 20 65 78 69 73 74 73 0a the page exists.
1f7fe 20 20 2a 2f 0a 20 20 70 42 74 20 3d 20 70 43 68 */. pBt = pCh
1f7ff 65 63 6b 2d 3e 70 42 74 3b 0a 20 20 75 73 61 62 eck->pBt;. usab
1f800 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 leSize = pBt->us
1f801 61 62 6c 65 53 69 7a 65 3b 0a 20 20 69 66 28 20 ableSize;. if(
1f802 69 50 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 iPage==0 ) retur
1f803 6e 20 30 3b 0a 20 20 69 66 28 20 63 68 65 63 6b n 0;. if( check
1f804 52 65 66 28 70 43 68 65 63 6b 2c 20 69 50 61 67 Ref(pCheck, iPag
1f805 65 2c 20 7a 50 61 72 65 6e 74 43 6f 6e 74 65 78 e, zParentContex
1f806 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 t) ) return 0;.
1f807 20 69 66 28 20 28 72 63 20 3d 20 62 74 72 65 65 if( (rc = btree
1f808 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 50 67 GetPage(pBt, (Pg
1f809 6e 6f 29 69 50 61 67 65 2c 20 26 70 50 61 67 65 no)iPage, &pPage
1f80a 2c 20 30 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 , 0))!=0 ){.
1f80b 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
1f80c 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c Check, zContext,
1f80d 0a 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 . "unable
1f80e 74 6f 20 67 65 74 20 74 68 65 20 70 61 67 65 2e to get the page.
1f80f 20 65 72 72 6f 72 20 63 6f 64 65 3d 25 64 22 2c error code=%d",
1f810 20 72 63 29 3b 0a 20 20 20 20 72 65 74 75 72 6e rc);. return
1f811 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 0;. }.. /* Cl
1f812 65 61 72 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e ear MemPage.isIn
1f813 69 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 it to make sure
1f814 74 68 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 64 the corruption d
1f815 65 74 65 63 74 69 6f 6e 20 63 6f 64 65 20 69 6e etection code in
1f816 0a 20 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 . ** btreeInitP
1f817 61 67 65 28 29 20 69 73 20 65 78 65 63 75 74 65 age() is execute
1f818 64 2e 20 20 2a 2f 0a 20 20 70 50 61 67 65 2d 3e d. */. pPage->
1f819 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 69 66 isInit = 0;. if
1f81a 28 20 28 72 63 20 3d 20 62 74 72 65 65 49 6e 69 ( (rc = btreeIni
1f81b 74 50 61 67 65 28 70 50 61 67 65 29 29 21 3d 30 tPage(pPage))!=0
1f81c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1f81d 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52 55 rc==SQLITE_CORRU
1f81e 50 54 20 29 3b 20 20 2f 2a 20 54 68 65 20 6f 6e PT ); /* The on
1f81f 6c 79 20 70 6f 73 73 69 62 6c 65 20 65 72 72 6f ly possible erro
1f820 72 20 66 72 6f 6d 20 49 6e 69 74 50 61 67 65 20 r from InitPage
1f821 2a 2f 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 */. checkAppe
1f822 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 ndMsg(pCheck, zC
1f823 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 ontext, .
1f824 20 20 20 20 20 20 20 20 20 20 20 20 22 62 74 72 "btr
1f825 65 65 49 6e 69 74 50 61 67 65 28 29 20 72 65 74 eeInitPage() ret
1f826 75 72 6e 73 20 65 72 72 6f 72 20 63 6f 64 65 20 urns error code
1f827 25 64 22 2c 20 72 63 29 3b 0a 20 20 20 20 72 65 %d", rc);. re
1f828 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
1f829 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
1f82a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 }.. /* Check
1f82b 6f 75 74 20 61 6c 6c 20 74 68 65 20 63 65 6c 6c out all the cell
1f82c 73 2e 0a 20 20 2a 2f 0a 20 20 64 65 70 74 68 20 s.. */. depth
1f82d 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 = 0;. for(i=0;
1f82e 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 26 i<pPage->nCell &
1f82f 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 3b & pCheck->mxErr;
1f830 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 i++){. u8 *p
1f831 43 65 6c 6c 3b 0a 20 20 20 20 75 33 32 20 73 7a Cell;. u32 sz
1f832 3b 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 ;. CellInfo i
1f833 6e 66 6f 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 nfo;.. /* Che
1f834 63 6b 20 70 61 79 6c 6f 61 64 20 6f 76 65 72 66 ck payload overf
1f835 6c 6f 77 20 70 61 67 65 73 0a 20 20 20 20 2a 2f low pages. */
1f836 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
1f837 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f rintf(sizeof(zCo
1f838 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 ntext), zContext
1f839 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 22 ,. "
1f83a 4f 6e 20 74 72 65 65 20 70 61 67 65 20 25 64 20 On tree page %d
1f83b 63 65 6c 6c 20 25 64 3a 20 22 2c 20 69 50 61 67 cell %d: ", iPag
1f83c 65 2c 20 69 29 3b 0a 20 20 20 20 70 43 65 6c 6c e, i);. pCell
1f83d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 = findCell(pPag
1f83e 65 2c 69 29 3b 0a 20 20 20 20 62 74 72 65 65 50 e,i);. btreeP
1f83f 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 arseCellPtr(pPag
1f840 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 e, pCell, &info)
1f841 3b 0a 20 20 20 20 73 7a 20 3d 20 69 6e 66 6f 2e ;. sz = info.
1f842 6e 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 21 nData;. if( !
1f843 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 20 pPage->intKey )
1f844 73 7a 20 2b 3d 20 28 69 6e 74 29 69 6e 66 6f 2e sz += (int)info.
1f845 6e 4b 65 79 3b 0a 20 20 20 20 2f 2a 20 46 6f 72 nKey;. /* For
1f846 20 69 6e 74 4b 65 79 20 70 61 67 65 73 2c 20 63 intKey pages, c
1f847 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 6b 65 heck that the ke
1f848 79 73 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e ys are in order.
1f849 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 6c 73 65 . */. else
1f84a 20 69 66 28 20 69 3d 3d 30 20 29 20 6e 4d 69 6e if( i==0 ) nMin
1f84b 4b 65 79 20 3d 20 6e 4d 61 78 4b 65 79 20 3d 20 Key = nMaxKey =
1f84c 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 65 info.nKey;. e
1f84d 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 69 lse{. if( i
1f84e 6e 66 6f 2e 6e 4b 65 79 20 3c 3d 20 6e 4d 61 78 nfo.nKey <= nMax
1f84f 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 63 Key ){. c
1f850 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
1f851 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 heck, zContext,
1f852 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 52 6f . "Ro
1f853 77 69 64 20 25 6c 6c 64 20 6f 75 74 20 6f 66 20 wid %lld out of
1f854 6f 72 64 65 72 20 28 70 72 65 76 69 6f 75 73 20 order (previous
1f855 77 61 73 20 25 6c 6c 64 29 22 2c 20 69 6e 66 6f was %lld)", info
1f856 2e 6e 4b 65 79 2c 20 6e 4d 61 78 4b 65 79 29 3b .nKey, nMaxKey);
1f857 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e . }. n
1f858 4d 61 78 4b 65 79 20 3d 20 69 6e 66 6f 2e 6e 4b MaxKey = info.nK
1f859 65 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 ey;. }. as
1f85a 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66 6f 2e 6e sert( sz==info.n
1f85b 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 20 20 69 Payload );. i
1f85c 66 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e 4c 6f 63 f( (sz>info.nLoc
1f85d 61 6c 29 20 0a 20 20 20 20 20 26 26 20 28 26 70 al) . && (&p
1f85e 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 Cell[info.iOverf
1f85f 6c 6f 77 5d 3c 3d 26 70 50 61 67 65 2d 3e 61 44 low]<=&pPage->aD
1f860 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65 53 ata[pBt->usableS
1f861 69 7a 65 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20 ize]). ){.
1f862 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 28 int nPage = (
1f863 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c sz - info.nLocal
1f864 20 2b 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 + usableSize -
1f865 35 29 2f 28 75 73 61 62 6c 65 53 69 7a 65 20 2d 5)/(usableSize -
1f866 20 34 29 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 4);. Pgno
1f867 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 pgnoOvfl = get4b
1f868 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e yte(&pCell[info.
1f869 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 23 69 66 iOverflow]);.#if
1f86a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1f86b 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
1f86c 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
1f86d 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 acuum ){.
1f86e 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 checkPtrmap(pCh
1f86f 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 50 eck, pgnoOvfl, P
1f870 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c TRMAP_OVERFLOW1,
1f871 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 iPage, zContext
1f872 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 );. }.#endi
1f873 66 0a 20 20 20 20 20 20 63 68 65 63 6b 4c 69 73 f. checkLis
1f874 74 28 70 43 68 65 63 6b 2c 20 30 2c 20 70 67 6e t(pCheck, 0, pgn
1f875 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c 20 7a 43 oOvfl, nPage, zC
1f876 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a 0a ontext);. }..
1f877 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 73 61 6e /* Check san
1f878 69 74 79 20 6f 66 20 6c 65 66 74 20 63 68 69 6c ity of left chil
1f879 64 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 d page.. */.
1f87a 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c if( !pPage->l
1f87b 65 61 66 20 29 7b 0a 20 20 20 20 20 20 70 67 6e eaf ){. pgn
1f87c 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 o = get4byte(pCe
1f87d 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c ll);.#ifndef SQL
1f87e 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
1f87f 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42 UUM. if( pB
1f880 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b t->autoVacuum ){
1f881 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 . checkPt
1f882 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e rmap(pCheck, pgn
1f883 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c o, PTRMAP_BTREE,
1f884 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 iPage, zContext
1f885 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 );. }.#endi
1f886 66 0a 20 20 20 20 20 20 64 32 20 3d 20 63 68 65 f. d2 = che
1f887 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 65 63 ckTreePage(pChec
1f888 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 78 k, pgno, zContex
1f889 74 2c 20 26 6e 4d 69 6e 4b 65 79 2c 20 69 3d 3d t, &nMinKey, i==
1f88a 30 20 3f 20 4e 55 4c 4c 20 3a 20 26 6e 4d 61 78 0 ? NULL : &nMax
1f88b 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 Key);. if(
1f88c 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74 68 i>0 && d2!=depth
1f88d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 ){. chec
1f88e 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
1f88f 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68 k, zContext, "Ch
1f890 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 20 64 ild page depth d
1f891 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20 20 iffers");.
1f892 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d 20 }. depth =
1f893 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 d2;. }. }..
1f894 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
1f895 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 f ){. pgno =
1f896 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d get4byte(&pPage-
1f897 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 >aData[pPage->hd
1f898 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 rOffset+8]);.
1f899 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1f89a 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 f(sizeof(zContex
1f89b 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 t), zContext, .
1f89c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f89d 20 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20 "On page %d
1f89e 61 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20 at right child:
1f89f 22 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 ", iPage);.#ifnd
1f8a0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1f8a1 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
1f8a2 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
1f8a3 6d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b m ){. check
1f8a4 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 Ptrmap(pCheck, p
1f8a5 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 gno, PTRMAP_BTRE
1f8a6 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 E, iPage, zConte
1f8a7 78 74 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 xt);. }.#endi
1f8a8 66 0a 20 20 20 20 63 68 65 63 6b 54 72 65 65 50 f. checkTreeP
1f8a9 61 67 65 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f age(pCheck, pgno
1f8aa 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 4e 55 4c 4c , zContext, NULL
1f8ab 2c 20 21 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 , !pPage->nCell
1f8ac 3f 20 4e 55 4c 4c 20 3a 20 26 6e 4d 61 78 4b 65 ? NULL : &nMaxKe
1f8ad 79 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 46 y);. }. . /* F
1f8ae 6f 72 20 69 6e 74 4b 65 79 20 6c 65 61 66 20 70 or intKey leaf p
1f8af 61 67 65 73 2c 20 63 68 65 63 6b 20 74 68 61 74 ages, check that
1f8b0 20 74 68 65 20 6d 69 6e 2f 6d 61 78 20 6b 65 79 the min/max key
1f8b1 73 20 61 72 65 20 69 6e 20 6f 72 64 65 72 0a 20 s are in order.
1f8b2 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 6c 65 66 ** with any lef
1f8b3 74 2f 70 61 72 65 6e 74 2f 72 69 67 68 74 20 70 t/parent/right p
1f8b4 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ages.. */. if(
1f8b5 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 26 26 20 pPage->leaf &&
1f8b6 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b pPage->intKey ){
1f8b7 0a 20 20 20 20 2f 2a 20 69 66 20 77 65 20 61 72 . /* if we ar
1f8b8 65 20 61 20 6c 65 66 74 20 63 68 69 6c 64 20 70 e a left child p
1f8b9 61 67 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 age */. if( p
1f8ba 6e 50 61 72 65 6e 74 4d 69 6e 4b 65 79 20 29 7b nParentMinKey ){
1f8bb 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 . /* if we
1f8bc 61 72 65 20 74 68 65 20 6c 65 66 74 20 6d 6f 73 are the left mos
1f8bd 74 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a t child page */.
1f8be 20 20 20 20 20 20 69 66 28 20 21 70 6e 50 61 72 if( !pnPar
1f8bf 65 6e 74 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 entMaxKey ){.
1f8c0 20 20 20 20 20 69 66 28 20 6e 4d 61 78 4b 65 79 if( nMaxKey
1f8c1 20 3e 20 2a 70 6e 50 61 72 65 6e 74 4d 69 6e 4b > *pnParentMinK
1f8c2 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ey ){.
1f8c3 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
1f8c4 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c Check, zContext,
1f8c5 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1f8c6 22 52 6f 77 69 64 20 25 6c 6c 64 20 6f 75 74 20 "Rowid %lld out
1f8c7 6f 66 20 6f 72 64 65 72 20 28 6d 61 78 20 6c 61 of order (max la
1f8c8 72 67 65 72 20 74 68 61 6e 20 70 61 72 65 6e 74 rger than parent
1f8c9 20 6d 69 6e 20 6f 66 20 25 6c 6c 64 29 22 2c 0a min of %lld)",.
1f8ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 4d nM
1f8cb 61 78 4b 65 79 2c 20 2a 70 6e 50 61 72 65 6e 74 axKey, *pnParent
1f8cc 4d 69 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 MinKey);.
1f8cd 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
1f8ce 20 20 20 20 20 20 20 20 69 66 28 20 6e 4d 69 6e if( nMin
1f8cf 4b 65 79 20 3c 3d 20 2a 70 6e 50 61 72 65 6e 74 Key <= *pnParent
1f8d0 4d 69 6e 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 MinKey ){.
1f8d1 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
1f8d2 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 sg(pCheck, zCont
1f8d3 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 ext, .
1f8d4 20 20 20 20 22 52 6f 77 69 64 20 25 6c 6c 64 20 "Rowid %lld
1f8d5 6f 75 74 20 6f 66 20 6f 72 64 65 72 20 28 6d 69 out of order (mi
1f8d6 6e 20 6c 65 73 73 20 74 68 61 6e 20 70 61 72 65 n less than pare
1f8d7 6e 74 20 6d 69 6e 20 6f 66 20 25 6c 6c 64 29 22 nt min of %lld)"
1f8d8 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1f8d9 6e 4d 69 6e 4b 65 79 2c 20 2a 70 6e 50 61 72 65 nMinKey, *pnPare
1f8da 6e 74 4d 69 6e 4b 65 79 29 3b 0a 20 20 20 20 20 ntMinKey);.
1f8db 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
1f8dc 20 6e 4d 61 78 4b 65 79 20 3e 20 2a 70 6e 50 61 nMaxKey > *pnPa
1f8dd 72 65 6e 74 4d 61 78 4b 65 79 20 29 7b 0a 20 20 rentMaxKey ){.
1f8de 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 checkApp
1f8df 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a endMsg(pCheck, z
1f8e0 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 Context, .
1f8e1 20 20 20 20 20 20 20 20 22 52 6f 77 69 64 20 25 "Rowid %
1f8e2 6c 6c 64 20 6f 75 74 20 6f 66 20 6f 72 64 65 72 lld out of order
1f8e3 20 28 6d 61 78 20 6c 61 72 67 65 72 20 74 68 61 (max larger tha
1f8e4 6e 20 70 61 72 65 6e 74 20 6d 61 78 20 6f 66 20 n parent max of
1f8e5 25 6c 6c 64 29 22 2c 0a 20 20 20 20 20 20 20 20 %lld)",.
1f8e6 20 20 20 20 20 20 6e 4d 61 78 4b 65 79 2c 20 2a nMaxKey, *
1f8e7 70 6e 50 61 72 65 6e 74 4d 61 78 4b 65 79 29 3b pnParentMaxKey);
1f8e8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1f8e9 20 20 20 2a 70 6e 50 61 72 65 6e 74 4d 69 6e 4b *pnParentMinK
1f8ea 65 79 20 3d 20 6e 4d 61 78 4b 65 79 3b 0a 20 20 ey = nMaxKey;.
1f8eb 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 }. /* els
1f8ec 65 20 69 66 20 77 65 27 72 65 20 61 20 72 69 67 e if we're a rig
1f8ed 68 74 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f ht child page */
1f8ee 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28 20 . } else if(
1f8ef 70 6e 50 61 72 65 6e 74 4d 61 78 4b 65 79 20 29 pnParentMaxKey )
1f8f0 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 4d 69 6e {. if( nMin
1f8f1 4b 65 79 20 3c 3d 20 2a 70 6e 50 61 72 65 6e 74 Key <= *pnParent
1f8f2 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 MaxKey ){.
1f8f3 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
1f8f4 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 (pCheck, zContex
1f8f5 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 t, .
1f8f6 22 52 6f 77 69 64 20 25 6c 6c 64 20 6f 75 74 20 "Rowid %lld out
1f8f7 6f 66 20 6f 72 64 65 72 20 28 6d 69 6e 20 6c 65 of order (min le
1f8f8 73 73 20 74 68 61 6e 20 70 61 72 65 6e 74 20 6d ss than parent m
1f8f9 61 78 20 6f 66 20 25 6c 6c 64 29 22 2c 0a 20 20 ax of %lld)",.
1f8fa 20 20 20 20 20 20 20 20 20 20 6e 4d 69 6e 4b 65 nMinKe
1f8fb 79 2c 20 2a 70 6e 50 61 72 65 6e 74 4d 61 78 4b y, *pnParentMaxK
1f8fc 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ey);. }.
1f8fd 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 }. }.. /* Che
1f8fe 63 6b 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 20 ck for complete
1f8ff 63 6f 76 65 72 61 67 65 20 6f 66 20 74 68 65 20 coverage of the
1f900 70 61 67 65 0a 20 20 2a 2f 0a 20 20 64 61 74 61 page. */. data
1f901 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b = pPage->aData;
1f902 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e . hdr = pPage->
1f903 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 68 69 74 hdrOffset;. hit
1f904 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 = sqlite3PageMa
1f905 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53 lloc( pBt->pageS
1f906 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 68 69 74 ize );. if( hit
1f907 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43 68 65 63 ==0 ){. pChec
1f908 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 k->mallocFailed
1f909 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 1;. }else{.
1f90a 20 20 69 6e 74 20 63 6f 6e 74 65 6e 74 4f 66 66 int contentOff
1f90b 73 65 74 20 3d 20 67 65 74 32 62 79 74 65 4e 6f set = get2byteNo
1f90c 74 5a 65 72 6f 28 26 64 61 74 61 5b 68 64 72 2b tZero(&data[hdr+
1f90d 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 5]);. assert(
1f90e 20 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 3c 3d contentOffset<=
1f90f 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 20 20 2f usableSize ); /
1f910 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 * Enforced by bt
1f911 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f reeInitPage() */
1f912 0a 20 20 20 20 6d 65 6d 73 65 74 28 68 69 74 2b . memset(hit+
1f913 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 2c 20 30 contentOffset, 0
1f914 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 6f 6e , usableSize-con
1f915 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 tentOffset);.
1f916 20 6d 65 6d 73 65 74 28 68 69 74 2c 20 31 2c 20 memset(hit, 1,
1f917 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a contentOffset);.
1f918 20 20 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74 32 nCell = get2
1f919 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 byte(&data[hdr+3
1f91a 5d 29 3b 0a 20 20 20 20 63 65 6c 6c 53 74 61 72 ]);. cellStar
1f91b 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 t = hdr + 12 - 4
1f91c 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 *pPage->leaf;.
1f91d 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 for(i=0; i<nCe
1f91e 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ll; i++){.
1f91f 69 6e 74 20 70 63 20 3d 20 67 65 74 32 62 79 74 int pc = get2byt
1f920 65 28 26 64 61 74 61 5b 63 65 6c 6c 53 74 61 72 e(&data[cellStar
1f921 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 75 t+i*2]);. u
1f922 33 32 20 73 69 7a 65 20 3d 20 36 35 35 33 36 3b 32 size = 65536;
1f923 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 . int j;.
1f924 20 20 20 20 69 66 28 20 70 63 3c 3d 75 73 61 62 if( pc<=usab
1f925 6c 65 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20 leSize-4 ){.
1f926 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 size = cellS
1f927 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 64 izePtr(pPage, &d
1f928 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 ata[pc]);.
1f929 7d 0a 20 20 20 20 20 20 69 66 28 20 28 69 6e 74 }. if( (int
1f92a 29 28 70 63 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 )(pc+size-1)>=us
1f92b 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 ableSize ){.
1f92c 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
1f92d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20 sg(pCheck, 0, .
1f92e 20 20 20 20 20 20 20 20 20 20 20 22 43 6f 72 72 "Corr
1f92f 75 70 74 69 6f 6e 20 64 65 74 65 63 74 65 64 20 uption detected
1f930 69 6e 20 63 65 6c 6c 20 25 64 20 6f 6e 20 70 61 in cell %d on pa
1f931 67 65 20 25 64 22 2c 69 2c 69 50 61 67 65 29 3b ge %d",i,iPage);
1f932 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1f933 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 63 2b 73 for(j=pc+s
1f934 69 7a 65 2d 31 3b 20 6a 3e 3d 70 63 3b 20 6a 2d ize-1; j>=pc; j-
1f935 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 -) hit[j]++;.
1f936 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
1f937 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
1f938 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 77 a[hdr+1]);. w
1f939 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 hile( i>0 ){.
1f93a 20 20 20 69 6e 74 20 73 69 7a 65 2c 20 6a 3b 0a int size, j;.
1f93b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c assert( i<
1f93c 3d 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b =usableSize-4 );
1f93d 20 20 20 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64 /* Enforced
1f93e 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61 67 by btreeInitPag
1f93f 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a e() */. siz
1f940 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 e = get2byte(&da
1f941 74 61 5b 69 2b 32 5d 29 3b 0a 20 20 20 20 20 20 ta[i+2]);.
1f942 61 73 73 65 72 74 28 20 69 2b 73 69 7a 65 3c 3d assert( i+size<=
1f943 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 20 20 2f usableSize ); /
1f944 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 * Enforced by bt
1f945 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f reeInitPage() */
1f946 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 69 2b 73 . for(j=i+s
1f947 69 7a 65 2d 31 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d ize-1; j>=i; j--
1f948 29 20 68 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 ) hit[j]++;.
1f949 20 20 6a 20 3d 20 67 65 74 32 62 79 74 65 28 26 j = get2byte(&
1f94a 64 61 74 61 5b 69 5d 29 3b 0a 20 20 20 20 20 20 data[i]);.
1f94b 61 73 73 65 72 74 28 20 6a 3d 3d 30 20 7c 7c 20 assert( j==0 ||
1f94c 6a 3e 69 2b 73 69 7a 65 20 29 3b 20 20 2f 2a 20 j>i+size ); /*
1f94d 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 Enforced by btre
1f94e 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 eInitPage() */.
1f94f 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 3d assert( j<=
1f950 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 20 usableSize-4 );
1f951 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 /* Enforced by
1f952 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 btreeInitPage()
1f953 20 2a 2f 0a 20 20 20 20 20 20 69 20 3d 20 6a 3b */. i = j;
1f954 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
1f955 3d 63 6e 74 3d 30 3b 20 69 3c 75 73 61 62 6c 65 =cnt=0; i<usable
1f956 53 69 7a 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Size; i++){.
1f957 20 20 69 66 28 20 68 69 74 5b 69 5d 3d 3d 30 20 if( hit[i]==0
1f958 29 7b 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b ){. cnt++
1f959 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
1f95a 28 20 68 69 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 ( hit[i]>1 ){.
1f95b 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e checkAppen
1f95c 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 0a dMsg(pCheck, 0,.
1f95d 20 20 20 20 20 20 20 20 20 20 22 4d 75 6c 74 69 "Multi
1f95e 70 6c 65 20 75 73 65 73 20 66 6f 72 20 62 79 74 ple uses for byt
1f95f 65 20 25 64 20 6f 66 20 70 61 67 65 20 25 64 22 e %d of page %d"
1f960 2c 20 69 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 , i, iPage);.
1f961 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1f962 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
1f963 28 20 63 6e 74 21 3d 64 61 74 61 5b 68 64 72 2b ( cnt!=data[hdr+
1f964 37 5d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 7] ){. chec
1f965 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
1f966 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 k, 0, .
1f967 20 22 46 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 "Fragmentation
1f968 6f 66 20 25 64 20 62 79 74 65 73 20 72 65 70 6f of %d bytes repo
1f969 72 74 65 64 20 61 73 20 25 64 20 6f 6e 20 70 61 rted as %d on pa
1f96a 67 65 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20 ge %d",.
1f96b 20 20 63 6e 74 2c 20 64 61 74 61 5b 68 64 72 2b cnt, data[hdr+
1f96c 37 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 7], iPage);.
1f96d 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 50 }. }. sqlite3P
1f96e 61 67 65 46 72 65 65 28 68 69 74 29 3b 0a 20 20 ageFree(hit);.
1f96f 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
1f970 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 64 65 70 e);. return dep
1f971 74 68 2b 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f th+1;.}.#endif /
1f972 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e * SQLITE_OMIT_IN
1f973 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f TEGRITY_CHECK */
1f974 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1f975 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f _OMIT_INTEGRITY_
1f976 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 CHECK./*.** This
1f977 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 61 20 routine does a
1f978 63 6f 6d 70 6c 65 74 65 20 63 68 65 63 6b 20 6f complete check o
1f979 66 20 74 68 65 20 67 69 76 65 6e 20 42 54 72 65 f the given BTre
1f97a 65 20 66 69 6c 65 2e 20 20 61 52 6f 6f 74 5b 5d e file. aRoot[]
1f97b 20 69 73 0a 2a 2a 20 61 6e 20 61 72 72 61 79 20 is.** an array
1f97c 6f 66 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 of pages numbers
1f97d 20 77 65 72 65 20 65 61 63 68 20 70 61 67 65 20 were each page
1f97e 6e 75 6d 62 65 72 20 69 73 20 74 68 65 20 72 6f number is the ro
1f97f 6f 74 20 70 61 67 65 20 6f 66 0a 2a 2a 20 61 20 ot page of.** a
1f980 74 61 62 6c 65 2e 20 20 6e 52 6f 6f 74 20 69 73 table. nRoot is
1f981 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
1f982 6e 74 72 69 65 73 20 69 6e 20 61 52 6f 6f 74 2e ntries in aRoot.
1f983 0a 2a 2a 0a 2a 2a 20 41 20 72 65 61 64 2d 6f 6e .**.** A read-on
1f984 6c 79 20 6f 72 20 72 65 61 64 2d 77 72 69 74 65 ly or read-write
1f985 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 transaction mus
1f986 74 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66 6f t be opened befo
1f987 72 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 re calling.** th
1f988 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a is function..**.
1f989 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d ** Write the num
1f98a 62 65 72 20 6f 66 20 65 72 72 6f 72 20 73 65 65 ber of error see
1f98b 6e 20 69 6e 20 2a 70 6e 45 72 72 2e 20 20 45 78 n in *pnErr. Ex
1f98c 63 65 70 74 20 66 6f 72 20 73 6f 6d 65 20 6d 65 cept for some me
1f98d 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 mory.** allocati
1f98e 6f 6e 20 65 72 72 6f 72 73 2c 20 20 61 6e 20 65 on errors, an e
1f98f 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 6c rror message hel
1f990 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 d in memory obta
1f991 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c ined from.** mal
1f992 6c 6f 63 20 69 73 20 72 65 74 75 72 6e 65 64 20 loc is returned
1f993 69 66 20 2a 70 6e 45 72 72 20 69 73 20 6e 6f 6e if *pnErr is non
1f994 2d 7a 65 72 6f 2e 20 20 49 66 20 2a 70 6e 45 72 -zero. If *pnEr
1f995 72 3d 3d 30 20 74 68 65 6e 20 4e 55 4c 4c 20 69 r==0 then NULL i
1f996 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 20 s.** returned.
1f997 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f If a memory allo
1f998 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 cation error occ
1f999 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 urs, NULL is ret
1f99a 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 urned..*/.SQLITE
1f99b 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
1f99c 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 qlite3BtreeInteg
1f99d 72 69 74 79 43 68 65 63 6b 28 0a 20 20 42 74 72 rityCheck(. Btr
1f99e 65 65 20 2a 70 2c 20 20 20 20 20 2f 2a 20 54 68 ee *p, /* Th
1f99f 65 20 62 74 72 65 65 20 74 6f 20 62 65 20 63 68 e btree to be ch
1f9a0 65 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a ecked */. int *
1f9a1 61 52 6f 6f 74 2c 20 20 20 2f 2a 20 41 6e 20 61 aRoot, /* An a
1f9a2 72 72 61 79 20 6f 66 20 72 6f 6f 74 20 70 61 67 rray of root pag
1f9a3 65 73 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 69 es numbers for i
1f9a4 6e 64 69 76 69 64 75 61 6c 20 74 72 65 65 73 20 ndividual trees
1f9a5 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 */. int nRoot,
1f9a6 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1f9a7 65 6e 74 72 69 65 73 20 69 6e 20 61 52 6f 6f 74 entries in aRoot
1f9a8 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 [] */. int mxEr
1f9a9 72 2c 20 20 20 20 2f 2a 20 53 74 6f 70 20 72 65 r, /* Stop re
1f9aa 70 6f 72 74 69 6e 67 20 65 72 72 6f 72 73 20 61 porting errors a
1f9ab 66 74 65 72 20 74 68 69 73 20 6d 61 6e 79 20 2a fter this many *
1f9ac 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 72 72 20 20 /. int *pnErr
1f9ad 20 20 2f 2a 20 57 72 69 74 65 20 6e 75 6d 62 65 /* Write numbe
1f9ae 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e r of errors seen
1f9af 20 74 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c to this variabl
1f9b0 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 69 e */.){. Pgno i
1f9b1 3b 0a 20 20 69 6e 74 20 6e 52 65 66 3b 0a 20 20 ;. int nRef;.
1f9b2 49 6e 74 65 67 72 69 74 79 43 6b 20 73 43 68 65 IntegrityCk sChe
1f9b3 63 6b 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a ck;. BtShared *
1f9b4 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1f9b5 63 68 61 72 20 7a 45 72 72 5b 31 30 30 5d 3b 0a char zErr[100];.
1f9b6 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1f9b7 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 nter(p);. asser
1f9b8 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 t( p->inTrans>TR
1f9b9 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d ANS_NONE && pBt-
1f9ba 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 54 >inTransaction>T
1f9bb 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 6e RANS_NONE );. n
1f9bc 52 65 66 20 3d 20 73 71 6c 69 74 65 33 50 61 67 Ref = sqlite3Pag
1f9bd 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e erRefcount(pBt->
1f9be 70 50 61 67 65 72 29 3b 0a 20 20 73 43 68 65 63 pPager);. sChec
1f9bf 6b 2e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 73 k.pBt = pBt;. s
1f9c0 43 68 65 63 6b 2e 70 50 61 67 65 72 20 3d 20 70 Check.pPager = p
1f9c1 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 43 Bt->pPager;. sC
1f9c2 68 65 63 6b 2e 6e 50 61 67 65 20 3d 20 62 74 72 heck.nPage = btr
1f9c3 65 65 50 61 67 65 63 6f 75 6e 74 28 73 43 68 65 eePagecount(sChe
1f9c4 63 6b 2e 70 42 74 29 3b 0a 20 20 73 43 68 65 63 ck.pBt);. sChec
1f9c5 6b 2e 6d 78 45 72 72 20 3d 20 6d 78 45 72 72 3b k.mxErr = mxErr;
1f9c6 0a 20 20 73 43 68 65 63 6b 2e 6e 45 72 72 20 3d . sCheck.nErr =
1f9c7 20 30 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 61 6c 0;. sCheck.mal
1f9c8 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 locFailed = 0;.
1f9c9 20 2a 70 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 *pnErr = 0;. i
1f9ca 66 28 20 73 43 68 65 63 6b 2e 6e 50 61 67 65 3d f( sCheck.nPage=
1f9cb 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1f9cc 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1f9cd 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
1f9ce 7d 0a 0a 20 20 73 43 68 65 63 6b 2e 61 50 67 52 }.. sCheck.aPgR
1f9cf 65 66 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c ef = sqlite3Mall
1f9d0 6f 63 5a 65 72 6f 28 28 73 43 68 65 63 6b 2e 6e ocZero((sCheck.n
1f9d1 50 61 67 65 20 2f 20 38 29 2b 20 31 29 3b 0a 20 Page / 8)+ 1);.
1f9d2 20 69 66 28 20 21 73 43 68 65 63 6b 2e 61 50 67 if( !sCheck.aPg
1f9d3 52 65 66 20 29 7b 0a 20 20 20 20 2a 70 6e 45 72 Ref ){. *pnEr
1f9d4 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 r = 1;. sqlit
1f9d5 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
1f9d6 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
1f9d7 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 }. i = PENDING
1f9d8 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 3b _BYTE_PAGE(pBt);
1f9d9 0a 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 6b . if( i<=sCheck
1f9da 2e 6e 50 61 67 65 20 29 20 73 65 74 50 61 67 65 .nPage ) setPage
1f9db 52 65 66 65 72 65 6e 63 65 64 28 26 73 43 68 65 Referenced(&sChe
1f9dc 63 6b 2c 20 69 29 3b 0a 20 20 73 71 6c 69 74 65 ck, i);. sqlite
1f9dd 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73 3StrAccumInit(&s
1f9de 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a 45 Check.errMsg, zE
1f9df 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 rr, sizeof(zErr)
1f9e0 2c 20 32 30 30 30 30 29 3b 0a 20 20 73 43 68 65 , 20000);. sChe
1f9e1 63 6b 2e 65 72 72 4d 73 67 2e 75 73 65 4d 61 6c ck.errMsg.useMal
1f9e2 6c 6f 63 20 3d 20 32 3b 0a 0a 20 20 2f 2a 20 43 loc = 2;.. /* C
1f9e3 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 heck the integri
1f9e4 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 ty of the freeli
1f9e5 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b 4c st. */. checkL
1f9e6 69 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c 20 ist(&sCheck, 1,
1f9e7 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 get4byte(&pBt->p
1f9e8 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d Page1->aData[32]
1f9e9 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 ),. g
1f9ea 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 et4byte(&pBt->pP
1f9eb 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 age1->aData[36])
1f9ec 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73 74 , "Main freelist
1f9ed 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 : ");.. /* Chec
1f9ee 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 73 k all the tables
1f9ef 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 .. */. for(i=0
1f9f0 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 26 ; (int)i<nRoot &
1f9f1 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 & sCheck.mxErr;
1f9f2 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52 i++){. if( aR
1f9f3 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 oot[i]==0 ) cont
1f9f4 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 inue;.#ifndef SQ
1f9f5 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
1f9f6 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 CUUM. if( pBt
1f9f7 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 ->autoVacuum &&
1f9f8 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 aRoot[i]>1 ){.
1f9f9 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 checkPtrmap(
1f9fa 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 &sCheck, aRoot[i
1f9fb 5d 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 ], PTRMAP_ROOTPA
1f9fc 47 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d GE, 0, 0);. }
1f9fd 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 .#endif. chec
1f9fe 6b 54 72 65 65 50 61 67 65 28 26 73 43 68 65 63 kTreePage(&sChec
1f9ff 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69 k, aRoot[i], "Li
1fa00 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 73 st of tree roots
1fa01 3a 20 22 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 : ", NULL, NULL)
1fa02 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ;. }.. /* Make
1fa03 20 73 75 72 65 20 65 76 65 72 79 20 70 61 67 65 sure every page
1fa04 20 69 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20 in the file is
1fa05 72 65 66 65 72 65 6e 63 65 64 0a 20 20 2a 2f 0a referenced. */.
1fa06 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 43 for(i=1; i<=sC
1fa07 68 65 63 6b 2e 6e 50 61 67 65 20 26 26 20 73 43 heck.nPage && sC
1fa08 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b 29 heck.mxErr; i++)
1fa09 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f {.#ifdef SQLITE_
1fa0a 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
1fa0b 20 20 20 20 69 66 28 20 67 65 74 50 61 67 65 52 if( getPageR
1fa0c 65 66 65 72 65 6e 63 65 64 28 26 73 43 68 65 63 eferenced(&sChec
1fa0d 6b 2c 20 69 29 3d 3d 30 20 29 7b 0a 20 20 20 20 k, i)==0 ){.
1fa0e 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
1fa0f 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 61 (&sCheck, 0, "Pa
1fa10 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 75 ge %d is never u
1fa11 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a sed", i);. }.
1fa12 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20 49 66 20 #else. /* If
1fa13 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 the database sup
1fa14 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 ports auto-vacuu
1fa15 6d 2c 20 6d 61 6b 65 20 73 75 72 65 20 6e 6f 20 m, make sure no
1fa16 74 61 62 6c 65 73 20 63 6f 6e 74 61 69 6e 0a 20 tables contain.
1fa17 20 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 ** references
1fa18 20 74 6f 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 to pointer-map
1fa19 70 61 67 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 pages.. */.
1fa1a 20 20 69 66 28 20 67 65 74 50 61 67 65 52 65 66 if( getPageRef
1fa1b 65 72 65 6e 63 65 64 28 26 73 43 68 65 63 6b 2c erenced(&sCheck,
1fa1c 20 69 29 3d 3d 30 20 26 26 20 0a 20 20 20 20 20 i)==0 && .
1fa1d 20 20 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f (PTRMAP_PAGENO
1fa1e 28 70 42 74 2c 20 69 29 21 3d 69 20 7c 7c 20 21 (pBt, i)!=i || !
1fa1f 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 pBt->autoVacuum)
1fa20 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 ){. checkA
1fa21 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b ppendMsg(&sCheck
1fa22 2c 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 73 , 0, "Page %d is
1fa23 20 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 29 never used", i)
1fa24 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1fa25 67 65 74 50 61 67 65 52 65 66 65 72 65 6e 63 65 getPageReference
1fa26 64 28 26 73 43 68 65 63 6b 2c 20 69 29 21 3d 30 d(&sCheck, i)!=0
1fa27 20 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 && . (PTR
1fa28 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 MAP_PAGENO(pBt,
1fa29 69 29 3d 3d 69 20 26 26 20 70 42 74 2d 3e 61 75 i)==i && pBt->au
1fa2a 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 toVacuum) ){.
1fa2b 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
1fa2c 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 g(&sCheck, 0, "P
1fa2d 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 ointer map page
1fa2e 25 64 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 %d is referenced
1fa2f 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6e ", i);. }.#en
1fa30 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 dif. }.. /* Ma
1fa31 6b 65 20 73 75 72 65 20 74 68 69 73 20 61 6e 61 ke sure this ana
1fa32 6c 79 73 69 73 20 64 69 64 20 6e 6f 74 20 6c 65 lysis did not le
1fa33 61 76 65 20 61 6e 79 20 75 6e 72 65 66 28 29 20 ave any unref()
1fa34 70 61 67 65 73 2e 0a 20 20 2a 2a 20 54 68 69 73 pages.. ** This
1fa35 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 is an internal
1fa36 63 6f 6e 73 69 73 74 65 6e 63 79 20 63 68 65 63 consistency chec
1fa37 6b 3b 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20 k; an integrity
1fa38 63 68 65 63 6b 0a 20 20 2a 2a 20 6f 66 20 74 68 check. ** of th
1fa39 65 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 e integrity chec
1fa3a 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 k.. */. if( NE
1fa3b 56 45 52 28 6e 52 65 66 20 21 3d 20 73 71 6c 69 VER(nRef != sqli
1fa3c 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 te3PagerRefcount
1fa3d 28 70 42 74 2d 3e 70 50 61 67 65 72 29 29 20 29 (pBt->pPager)) )
1fa3e 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e {. checkAppen
1fa3f 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c dMsg(&sCheck, 0,
1fa40 20 0a 20 20 20 20 20 20 22 4f 75 74 73 74 61 6e . "Outstan
1fa41 64 69 6e 67 20 70 61 67 65 20 63 6f 75 6e 74 20 ding page count
1fa42 67 6f 65 73 20 66 72 6f 6d 20 25 64 20 74 6f 20 goes from %d to
1fa43 25 64 20 64 75 72 69 6e 67 20 74 68 69 73 20 61 %d during this a
1fa44 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20 20 20 nalysis",.
1fa45 6e 52 65 66 2c 20 73 71 6c 69 74 65 33 50 61 67 nRef, sqlite3Pag
1fa46 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e erRefcount(pBt->
1fa47 70 50 61 67 65 72 29 0a 20 20 20 20 29 3b 0a 20 pPager). );.
1fa48 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 20 }.. /* Clean
1fa49 75 70 20 61 6e 64 20 72 65 70 6f 72 74 20 65 72 up and report er
1fa4a 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c rors.. */. sql
1fa4b 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1fa4c 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
1fa4d 65 28 73 43 68 65 63 6b 2e 61 50 67 52 65 66 29 e(sCheck.aPgRef)
1fa4e 3b 0a 20 20 69 66 28 20 73 43 68 65 63 6b 2e 6d ;. if( sCheck.m
1fa4f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1fa50 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
1fa51 75 6d 52 65 73 65 74 28 26 73 43 68 65 63 6b 2e umReset(&sCheck.
1fa52 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 2a 70 6e errMsg);. *pn
1fa53 45 72 72 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72 Err = sCheck.nEr
1fa54 72 2b 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 r+1;. return
1fa55 30 3b 0a 20 20 7d 0a 20 20 2a 70 6e 45 72 72 20 0;. }. *pnErr
1fa56 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 3b 0a 20 = sCheck.nErr;.
1fa57 20 69 66 28 20 73 43 68 65 63 6b 2e 6e 45 72 72 if( sCheck.nErr
1fa58 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 53 74 72 ==0 ) sqlite3Str
1fa59 41 63 63 75 6d 52 65 73 65 74 28 26 73 43 68 65 AccumReset(&sChe
1fa5a 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 20 20 72 65 ck.errMsg);. re
1fa5b 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72 41 turn sqlite3StrA
1fa5c 63 63 75 6d 46 69 6e 69 73 68 28 26 73 43 68 65 ccumFinish(&sChe
1fa5d 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 7d 0a 23 65 ck.errMsg);.}.#e
1fa5e 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1fa5f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 MIT_INTEGRITY_CH
1fa60 45 43 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 ECK */../*.** Re
1fa61 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 turn the full pa
1fa62 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 75 6e thname of the un
1fa63 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61 73 derlying databas
1fa64 65 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 0a e file. Return.
1fa65 2a 2a 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 ** an empty stri
1fa66 6e 67 20 69 66 20 74 68 65 20 64 61 74 61 62 61 ng if the databa
1fa67 73 65 20 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 se is in-memory
1fa68 6f 72 20 61 20 54 45 4d 50 20 64 61 74 61 62 61 or a TEMP databa
1fa69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 se..**.** The pa
1fa6a 67 65 72 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 ger filename is
1fa6b 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e invariant as lon
1fa6c 67 20 61 73 20 74 68 65 20 70 61 67 65 72 20 69 g as the pager i
1fa6d 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 s.** open so it
1fa6e 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 is safe to acces
1fa6f 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 42 74 s without the Bt
1fa70 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f Shared mutex..*/
1fa71 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1fa72 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
1fa73 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e te3BtreeGetFilen
1fa74 61 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 ame(Btree *p){.
1fa75 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d assert( p->pBt-
1fa76 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 >pPager!=0 );.
1fa77 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 61 return sqlite3Pa
1fa78 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 gerFilename(p->p
1fa79 42 74 2d 3e 70 50 61 67 65 72 2c 20 31 29 3b 0a Bt->pPager, 1);.
1fa7a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1fa7b 74 68 65 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 the pathname of
1fa7c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1fa7d 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 for this databa
1fa7e 73 65 2e 20 54 68 65 20 72 65 74 75 72 6e 0a 2a se. The return.*
1fa7f 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 * value of this
1fa80 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 73 routine is the s
1fa81 61 6d 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f ame regardless o
1fa82 66 20 77 68 65 74 68 65 72 20 74 68 65 20 6a 6f f whether the jo
1fa83 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 68 61 urnal file.** ha
1fa84 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 6f s been created o
1fa85 72 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 r not..**.** The
1fa86 20 70 61 67 65 72 20 6a 6f 75 72 6e 61 6c 20 66 pager journal f
1fa87 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 ilename is invar
1fa88 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 iant as long as
1fa89 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 the pager is.**
1fa8a 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61 open so it is sa
1fa8b 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 74 fe to access wit
1fa8c 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 65 hout the BtShare
1fa8d 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 d mutex..*/.SQLI
1fa8e 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
1fa8f 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 char *sqlite3Bt
1fa90 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d reeGetJournalnam
1fa91 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 e(Btree *p){. a
1fa92 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 ssert( p->pBt->p
1fa93 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 Pager!=0 );. re
1fa94 74 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 turn sqlite3Page
1fa95 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 2d 3e rJournalname(p->
1fa96 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a pBt->pPager);.}.
1fa97 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f ./*.** Return no
1fa98 6e 2d 7a 65 72 6f 20 69 66 20 61 20 74 72 61 6e n-zero if a tran
1fa99 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 saction is activ
1fa9a 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1fa9b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1fa9c 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 42 BtreeIsInTrans(B
1fa9d 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 tree *p){. asse
1fa9e 72 74 28 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 rt( p==0 || sqli
1fa9f 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1faa0 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
1faa1 20 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 return (p && (
1faa2 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
1faa3 53 5f 57 52 49 54 45 29 29 3b 0a 7d 0a 0a 23 69 S_WRITE));.}..#i
1faa4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1faa5 54 5f 57 41 4c 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 T_WAL./*.** Run
1faa6 61 20 63 68 65 63 6b 70 6f 69 6e 74 20 6f 6e 20 a checkpoint on
1faa7 74 68 65 20 42 74 72 65 65 20 70 61 73 73 65 64 the Btree passed
1faa8 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
1faa9 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 gument..**.** Re
1faaa 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b turn SQLITE_LOCK
1faab 45 44 20 69 66 20 74 68 69 73 20 6f 72 20 61 6e ED if this or an
1faac 79 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 y other connecti
1faad 6f 6e 20 68 61 73 20 61 6e 20 6f 70 65 6e 20 0a on has an open .
1faae 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f ** transaction o
1faaf 6e 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 n the shared-cac
1fab0 68 65 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 he the argument
1fab1 42 74 72 65 65 20 69 73 20 63 6f 6e 6e 65 63 74 Btree is connect
1fab2 65 64 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 ed to..**.** Par
1fab3 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 69 73 20 ameter eMode is
1fab4 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 43 48 one of SQLITE_CH
1fab5 45 43 4b 50 4f 49 4e 54 5f 50 41 53 53 49 56 45 ECKPOINT_PASSIVE
1fab6 2c 20 46 55 4c 4c 20 6f 72 20 52 45 53 54 41 52 , FULL or RESTAR
1fab7 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 T..*/.SQLITE_PRI
1fab8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1fab9 42 74 72 65 65 43 68 65 63 6b 70 6f 69 6e 74 28 BtreeCheckpoint(
1faba 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 65 4d Btree *p, int eM
1fabb 6f 64 65 2c 20 69 6e 74 20 2a 70 6e 4c 6f 67 2c ode, int *pnLog,
1fabc 20 69 6e 74 20 2a 70 6e 43 6b 70 74 29 7b 0a 20 int *pnCkpt){.
1fabd 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1fabe 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a _OK;. if( p ){.
1fabf 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 BtShared *pB
1fac0 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 t = p->pBt;.
1fac1 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1fac2 72 28 70 29 3b 0a 20 20 20 20 69 66 28 20 70 42 r(p);. if( pB
1fac3 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
1fac4 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a !=TRANS_NONE ){.
1fac5 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1fac6 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 7d 65 E_LOCKED;. }e
1fac7 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
1fac8 73 71 6c 69 74 65 33 50 61 67 65 72 43 68 65 63 sqlite3PagerChec
1fac9 6b 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 kpoint(pBt->pPag
1faca 65 72 2c 20 65 4d 6f 64 65 2c 20 70 6e 4c 6f 67 er, eMode, pnLog
1facb 2c 20 70 6e 43 6b 70 74 29 3b 0a 20 20 20 20 7d , pnCkpt);. }
1facc 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1facd 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 eLeave(p);. }.
1face 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 return rc;.}.#e
1facf 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
1fad0 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 rn non-zero if a
1fad1 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 65 29 read (or write)
1fad2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1fad3 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 active..*/.SQLIT
1fad4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1fad5 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 lite3BtreeIsInRe
1fad6 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 adTrans(Btree *p
1fad7 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 ){. assert( p )
1fad8 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
1fad9 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1fada 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
1fadb 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e 54 72 return p->inTr
1fadc 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 3b ans!=TRANS_NONE;
1fadd 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .}..SQLITE_PRIVA
1fade 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
1fadf 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 42 74 reeIsInBackup(Bt
1fae0 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ree *p){. asser
1fae1 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 t( p );. assert
1fae2 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1fae3 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 held(p->db->mute
1fae4 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 x) );. return p
1fae5 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a 7d 0a ->nBackup!=0;.}.
1fae6 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
1fae7 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 tion returns a p
1fae8 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f 62 ointer to a blob
1fae9 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 of memory assoc
1faea 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 iated with.** a
1faeb 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d 62 74 single shared-bt
1faec 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72 79 20 ree. The memory
1faed 69 73 20 75 73 65 64 20 62 79 20 63 6c 69 65 6e is used by clien
1faee 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73 20 6f t code for its o
1faef 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 28 wn.** purposes (
1faf0 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 for example, to
1faf1 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c 65 76 store a high-lev
1faf2 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 el schema associ
1faf3 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 ated with .** th
1faf4 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 29 2e e shared-btree).
1faf5 20 54 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 The btree layer
1faf6 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72 65 6e manages referen
1faf7 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73 73 75 ce counting issu
1faf8 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 es..**.** The fi
1faf9 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 73 rst time this is
1fafa 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 called on a sha
1fafb 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 74 65 red-btree, nByte
1fafc 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 s bytes of memor
1fafd 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61 74 y.** are allocat
1fafe 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64 20 ed, zeroed, and
1faff 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
1fb00 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63 68 caller. For each
1fb01 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a 2a 20 subsequent .**
1fb02 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65 73 20 call the nBytes
1fb03 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 67 6e parameter is ign
1fb04 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69 6e 74 ored and a point
1fb05 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 20 62 er to the same b
1fb06 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 lob.** of memory
1fb07 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a 2a returned. .**.*
1fb08 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65 73 20 * If the nBytes
1fb09 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 20 61 parameter is 0 a
1fb0a 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d nd the blob of m
1fb0b 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20 79 65 emory has not ye
1fb0c 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 t been.** alloca
1fb0d 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f 69 6e ted, a null poin
1fb0e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e ter is returned.
1fb0f 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68 61 73 If the blob has
1fb10 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a 2a already been.**
1fb11 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 69 allocated, it i
1fb12 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 6e 6f s returned as no
1fb13 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73 74 rmal..**.** Just
1fb14 20 62 65 66 6f 72 65 20 74 68 65 20 73 68 61 72 before the shar
1fb15 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c 6f 73 ed-btree is clos
1fb16 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e ed, the function
1fb17 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 0a passed as the .
1fb18 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d 65 6e ** xFree argumen
1fb19 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72 t when the memor
1fb1a 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 y allocation was
1fb1b 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b 65 64 made is invoked
1fb1c 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f 62 on the .** blob
1fb1d 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 of allocated me
1fb1e 6d 6f 72 79 2e 20 54 68 65 20 78 46 72 65 65 20 mory. The xFree
1fb1f 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 function should
1fb20 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 not call sqlite3
1fb21 5f 66 72 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 _free().** on th
1fb22 65 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 62 74 e memory, the bt
1fb23 72 65 65 20 6c 61 79 65 72 20 64 6f 65 73 20 74 ree layer does t
1fb24 68 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 hat..*/.SQLITE_P
1fb25 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
1fb26 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 ite3BtreeSchema(
1fb27 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42 Btree *p, int nB
1fb28 79 74 65 73 2c 20 76 6f 69 64 28 2a 78 46 72 65 ytes, void(*xFre
1fb29 65 29 28 76 6f 69 64 20 2a 29 29 7b 0a 20 20 42 e)(void *)){. B
1fb2a 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
1fb2b 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 ->pBt;. sqlite3
1fb2c 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
1fb2d 20 69 66 28 20 21 70 42 74 2d 3e 70 53 63 68 65 if( !pBt->pSche
1fb2e 6d 61 20 26 26 20 6e 42 79 74 65 73 20 29 7b 0a ma && nBytes ){.
1fb2f 20 20 20 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 pBt->pSchema
1fb30 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
1fb31 6f 63 5a 65 72 6f 28 30 2c 20 6e 42 79 74 65 73 ocZero(0, nBytes
1fb32 29 3b 0a 20 20 20 20 70 42 74 2d 3e 78 46 72 65 );. pBt->xFre
1fb33 65 53 63 68 65 6d 61 20 3d 20 78 46 72 65 65 3b eSchema = xFree;
1fb34 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
1fb35 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
1fb36 65 74 75 72 6e 20 70 42 74 2d 3e 70 53 63 68 65 eturn pBt->pSche
1fb37 6d 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ma;.}../*.** Ret
1fb38 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 urn SQLITE_LOCKE
1fb39 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 69 66 D_SHAREDCACHE if
1fb3a 20 61 6e 6f 74 68 65 72 20 75 73 65 72 20 6f 66 another user of
1fb3b 20 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 64 the same shared
1fb3c 20 0a 2a 2a 20 62 74 72 65 65 20 61 73 20 74 68 .** btree as th
1fb3d 65 20 61 72 67 75 6d 65 6e 74 20 68 61 6e 64 6c e argument handl
1fb3e 65 20 68 6f 6c 64 73 20 61 6e 20 65 78 63 6c 75 e holds an exclu
1fb3f 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 sive lock on the
1fb40 20 0a 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 .** sqlite_mast
1fb41 65 72 20 74 61 62 6c 65 2e 20 4f 74 68 65 72 77 er table. Otherw
1fb42 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a ise SQLITE_OK..*
1fb43 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1fb44 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
1fb45 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74 eSchemaLocked(Bt
1fb46 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 ree *p){. int r
1fb47 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c c;. assert( sql
1fb48 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1fb49 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
1fb4a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1fb4b 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 nter(p);. rc =
1fb4c 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 querySharedCache
1fb4d 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 TableLock(p, MAS
1fb4e 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c TER_ROOT, READ_L
1fb4f 4f 43 4b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 OCK);. assert(
1fb50 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
1fb51 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b rc==SQLITE_LOCK
1fb52 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 29 ED_SHAREDCACHE )
1fb53 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1fb54 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
1fb55 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 rn rc;.}...#ifnd
1fb56 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
1fb57 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a HARED_CACHE./*.*
1fb58 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 * Obtain a lock
1fb59 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 68 6f on the table who
1fb5a 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 se root page is
1fb5b 69 54 61 62 2e 20 20 54 68 65 0a 2a 2a 20 6c 6f iTab. The.** lo
1fb5c 63 6b 20 69 73 20 61 20 77 72 69 74 65 20 6c 6f ck is a write lo
1fb5d 63 6b 20 69 66 20 69 73 57 72 69 74 65 6c 6f 63 ck if isWriteloc
1fb5e 6b 20 69 73 20 74 72 75 65 20 6f 72 20 61 20 72 k is true or a r
1fb5f 65 61 64 20 6c 6f 63 6b 0a 2a 2a 20 69 66 20 69 ead lock.** if i
1fb60 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 t is false..*/.S
1fb61 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1fb62 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f t sqlite3BtreeLo
1fb63 63 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 ckTable(Btree *p
1fb64 2c 20 69 6e 74 20 69 54 61 62 2c 20 75 38 20 69 , int iTab, u8 i
1fb65 73 57 72 69 74 65 4c 6f 63 6b 29 7b 0a 20 20 69 sWriteLock){. i
1fb66 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1fb67 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e K;. assert( p->
1fb68 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e inTrans!=TRANS_N
1fb69 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e ONE );. if( p->
1fb6a 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
1fb6b 75 38 20 6c 6f 63 6b 54 79 70 65 20 3d 20 52 45 u8 lockType = RE
1fb6c 41 44 5f 4c 4f 43 4b 20 2b 20 69 73 57 72 69 74 AD_LOCK + isWrit
1fb6d 65 4c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 72 eLock;. asser
1fb6e 74 28 20 52 45 41 44 5f 4c 4f 43 4b 2b 31 3d 3d t( READ_LOCK+1==
1fb6f 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 WRITE_LOCK );.
1fb70 20 20 61 73 73 65 72 74 28 20 69 73 57 72 69 74 assert( isWrit
1fb71 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 eLock==0 || isWr
1fb72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 0a 20 iteLock==1 );..
1fb73 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 sqlite3BtreeE
1fb74 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 72 63 20 nter(p);. rc
1fb75 3d 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 = querySharedCac
1fb76 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 heTableLock(p, i
1fb77 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a Tab, lockType);.
1fb78 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1fb79 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
1fb7a 63 20 3d 20 73 65 74 53 68 61 72 65 64 43 61 63 c = setSharedCac
1fb7b 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 heTableLock(p, i
1fb7c 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a Tab, lockType);.
1fb7d 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1fb7e 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
1fb7f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1fb80 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 .}.#endif..#ifnd
1fb81 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
1fb82 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 41 72 NCRBLOB./*.** Ar
1fb83 67 75 6d 65 6e 74 20 70 43 73 72 20 6d 75 73 74 gument pCsr must
1fb84 20 62 65 20 61 20 63 75 72 73 6f 72 20 6f 70 65 be a cursor ope
1fb85 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 20 ned for writing
1fb86 6f 6e 20 61 6e 20 0a 2a 2a 20 49 4e 54 4b 45 59 on an .** INTKEY
1fb87 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 table currently
1fb88 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 76 pointing at a v
1fb89 61 6c 69 64 20 74 61 62 6c 65 20 65 6e 74 72 79 alid table entry
1fb8a 2e 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 . .** This funct
1fb8b 69 6f 6e 20 6d 6f 64 69 66 69 65 73 20 74 68 65 ion modifies the
1fb8c 20 64 61 74 61 20 73 74 6f 72 65 64 20 61 73 20 data stored as
1fb8d 70 61 72 74 20 6f 66 20 74 68 61 74 20 65 6e 74 part of that ent
1fb8e 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 ry..**.** Only t
1fb8f 68 65 20 64 61 74 61 20 63 6f 6e 74 65 6e 74 20 he data content
1fb90 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6d 6f 64 69 may only be modi
1fb91 66 69 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 fied, it is not
1fb92 70 6f 73 73 69 62 6c 65 20 74 6f 20 0a 2a 2a 20 possible to .**
1fb93 63 68 61 6e 67 65 20 74 68 65 20 6c 65 6e 67 74 change the lengt
1fb94 68 20 6f 66 20 74 68 65 20 64 61 74 61 20 73 74 h of the data st
1fb95 6f 72 65 64 2e 20 49 66 20 74 68 69 73 20 66 75 ored. If this fu
1fb96 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
1fb97 20 77 69 74 68 0a 2a 2a 20 70 61 72 61 6d 65 74 with.** paramet
1fb98 65 72 73 20 74 68 61 74 20 61 74 74 65 6d 70 74 ers that attempt
1fb99 20 74 6f 20 77 72 69 74 65 20 70 61 73 74 20 74 to write past t
1fb9a 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 65 78 he end of the ex
1fb9b 69 73 74 69 6e 67 20 64 61 74 61 2c 0a 2a 2a 20 isting data,.**
1fb9c 6e 6f 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 no modifications
1fb9d 20 61 72 65 20 6d 61 64 65 20 61 6e 64 20 53 51 are made and SQ
1fb9e 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 73 20 LITE_CORRUPT is
1fb9f 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c returned..*/.SQL
1fba0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1fba1 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 44 sqlite3BtreePutD
1fba2 61 74 61 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ata(BtCursor *pC
1fba3 73 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 sr, u32 offset,
1fba4 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 7a u32 amt, void *z
1fba5 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 ){. int rc;. a
1fba6 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
1fba7 64 73 4d 75 74 65 78 28 70 43 73 72 29 20 29 3b dsMutex(pCsr) );
1fba8 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1fba9 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
1fbaa 73 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e sr->pBtree->db->
1fbab 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
1fbac 72 74 28 20 70 43 73 72 2d 3e 69 73 49 6e 63 72 rt( pCsr->isIncr
1fbad 62 6c 6f 62 48 61 6e 64 6c 65 20 29 3b 0a 0a 20 blobHandle );..
1fbae 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 rc = restoreCur
1fbaf 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 73 72 sorPosition(pCsr
1fbb0 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
1fbb1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 ITE_OK ){. re
1fbb2 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 turn rc;. }. a
1fbb3 73 73 65 72 74 28 20 70 43 73 72 2d 3e 65 53 74 ssert( pCsr->eSt
1fbb4 61 74 65 21 3d 43 55 52 53 4f 52 5f 52 45 51 55 ate!=CURSOR_REQU
1fbb5 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 IRESEEK );. if(
1fbb6 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 pCsr->eState!=C
1fbb7 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 URSOR_VALID ){.
1fbb8 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1fbb9 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 0a 20 20 2f _ABORT;. }.. /
1fbba 2a 20 43 68 65 63 6b 20 73 6f 6d 65 20 61 73 73 * Check some ass
1fbbb 75 6d 70 74 69 6f 6e 73 3a 20 0a 20 20 2a 2a 20 umptions: . **
1fbbc 20 20 28 61 29 20 74 68 65 20 63 75 72 73 6f 72 (a) the cursor
1fbbd 20 69 73 20 6f 70 65 6e 20 66 6f 72 20 77 72 69 is open for wri
1fbbe 74 69 6e 67 2c 0a 20 20 2a 2a 20 20 20 28 62 29 ting,. ** (b)
1fbbf 20 74 68 65 72 65 20 69 73 20 61 20 72 65 61 64 there is a read
1fbc0 2f 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 /write transacti
1fbc1 6f 6e 20 6f 70 65 6e 2c 0a 20 20 2a 2a 20 20 20 on open,. **
1fbc2 28 63 29 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 (c) the connecti
1fbc3 6f 6e 20 68 6f 6c 64 73 20 61 20 77 72 69 74 65 on holds a write
1fbc4 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 -lock on the tab
1fbc5 6c 65 20 28 69 66 20 72 65 71 75 69 72 65 64 29 le (if required)
1fbc6 2c 0a 20 20 2a 2a 20 20 20 28 64 29 20 74 68 65 ,. ** (d) the
1fbc7 72 65 20 61 72 65 20 6e 6f 20 63 6f 6e 66 6c 69 re are no confli
1fbc8 63 74 69 6e 67 20 72 65 61 64 2d 6c 6f 63 6b 73 cting read-locks
1fbc9 2c 20 61 6e 64 0a 20 20 2a 2a 20 20 20 28 65 29 , and. ** (e)
1fbca 20 74 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e the cursor poin
1fbcb 74 73 20 61 74 20 61 20 76 61 6c 69 64 20 72 6f ts at a valid ro
1fbcc 77 20 6f 66 20 61 6e 20 69 6e 74 4b 65 79 20 74 w of an intKey t
1fbcd 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 able.. */. if(
1fbce 20 21 70 43 73 72 2d 3e 77 72 46 6c 61 67 20 29 !pCsr->wrFlag )
1fbcf 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1fbd0 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 ITE_READONLY;.
1fbd1 7d 0a 20 20 61 73 73 65 72 74 28 20 28 70 43 73 }. assert( (pCs
1fbd2 72 2d 3e 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 r->pBt->btsFlags
1fbd3 20 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 & BTS_READ_ONLY
1fbd4 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 )==0.
1fbd5 20 20 20 26 26 20 70 43 73 72 2d 3e 70 42 74 2d && pCsr->pBt-
1fbd6 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d >inTransaction==
1fbd7 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 TRANS_WRITE );.
1fbd8 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61 72 assert( hasShar
1fbd9 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
1fbda 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2c 20 70 (pCsr->pBtree, p
1fbdb 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 30 Csr->pgnoRoot, 0
1fbdc 2c 20 32 29 20 29 3b 0a 20 20 61 73 73 65 72 74 , 2) );. assert
1fbdd 28 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 ( !hasReadConfli
1fbde 63 74 73 28 70 43 73 72 2d 3e 70 42 74 72 65 65 cts(pCsr->pBtree
1fbdf 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74 , pCsr->pgnoRoot
1fbe0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
1fbe1 43 73 72 2d 3e 61 70 50 61 67 65 5b 70 43 73 72 Csr->apPage[pCsr
1fbe2 2d 3e 69 50 61 67 65 5d 2d 3e 69 6e 74 4b 65 79 ->iPage]->intKey
1fbe3 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 61 63 );.. return ac
1fbe4 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 73 72 cessPayload(pCsr
1fbe5 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 28 , offset, amt, (
1fbe6 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 unsigned char *)
1fbe7 7a 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a z, 1);.}../* .**
1fbe8 20 53 65 74 20 61 20 66 6c 61 67 20 6f 6e 20 74 Set a flag on t
1fbe9 68 69 73 20 63 75 72 73 6f 72 20 74 6f 20 63 61 his cursor to ca
1fbea 63 68 65 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e che the location
1fbeb 73 20 6f 66 20 70 61 67 65 73 20 66 72 6f 6d 20 s of pages from
1fbec 74 68 65 20 0a 2a 2a 20 6f 76 65 72 66 6c 6f 77 the .** overflow
1fbed 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 63 75 list for the cu
1fbee 72 72 65 6e 74 20 72 6f 77 2e 20 54 68 69 73 20 rrent row. This
1fbef 69 73 20 75 73 65 64 20 62 79 20 63 75 72 73 6f is used by curso
1fbf0 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72 rs opened.** for
1fbf1 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f incremental blo
1fbf2 62 20 49 4f 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a b IO only..**.**
1fbf3 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 This function s
1fbf4 65 74 73 20 61 20 66 6c 61 67 20 6f 6e 6c 79 2e ets a flag only.
1fbf5 20 54 68 65 20 61 63 74 75 61 6c 20 70 61 67 65 The actual page
1fbf6 20 6c 6f 63 61 74 69 6f 6e 20 63 61 63 68 65 0a location cache.
1fbf7 2a 2a 20 28 73 74 6f 72 65 64 20 69 6e 20 42 74 ** (stored in Bt
1fbf8 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 Cursor.aOverflow
1fbf9 5b 5d 29 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 []) is allocated
1fbfa 20 61 6e 64 20 75 73 65 64 20 62 79 20 66 75 6e and used by fun
1fbfb 63 74 69 6f 6e 0a 2a 2a 20 61 63 63 65 73 73 50 ction.** accessP
1fbfc 61 79 6c 6f 61 64 28 29 20 28 74 68 65 20 77 6f ayload() (the wo
1fbfd 72 6b 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f rker function fo
1fbfe 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 r sqlite3BtreeDa
1fbff 74 61 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 ta() and.** sqli
1fc00 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28 te3BtreePutData(
1fc01 29 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ))..*/.SQLITE_PR
1fc02 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1fc03 65 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 e3BtreeCacheOver
1fc04 66 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 70 flow(BtCursor *p
1fc05 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Cur){. assert(
1fc06 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
1fc07 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
1fc08 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1fc09 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 x_held(pCur->pBt
1fc0a 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 ree->db->mutex)
1fc0b 29 3b 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 4f );. invalidateO
1fc0c 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75 verflowCache(pCu
1fc0d 72 29 3b 0a 20 20 70 43 75 72 2d 3e 69 73 49 6e r);. pCur->isIn
1fc0e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 3d 20 31 crblobHandle = 1
1fc0f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1fc10 2a 20 53 65 74 20 62 6f 74 68 20 74 68 65 20 22 * Set both the "
1fc11 72 65 61 64 20 76 65 72 73 69 6f 6e 22 20 28 73 read version" (s
1fc12 69 6e 67 6c 65 20 62 79 74 65 20 61 74 20 62 79 ingle byte at by
1fc13 74 65 20 6f 66 66 73 65 74 20 31 38 29 20 61 6e te offset 18) an
1fc14 64 20 0a 2a 2a 20 22 77 72 69 74 65 20 76 65 72 d .** "write ver
1fc15 73 69 6f 6e 22 20 28 73 69 6e 67 6c 65 20 62 79 sion" (single by
1fc16 74 65 20 61 74 20 62 79 74 65 20 6f 66 66 73 65 te at byte offse
1fc17 74 20 31 39 29 20 66 69 65 6c 64 73 20 69 6e 20 t 19) fields in
1fc18 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
1fc19 68 65 61 64 65 72 20 74 6f 20 69 56 65 72 73 69 header to iVersi
1fc1a 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
1fc1b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1fc1c 33 42 74 72 65 65 53 65 74 56 65 72 73 69 6f 6e 3BtreeSetVersion
1fc1d 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 (Btree *pBtree,
1fc1e 69 6e 74 20 69 56 65 72 73 69 6f 6e 29 7b 0a 20 int iVersion){.
1fc1f 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
1fc20 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 pBtree->pBt;.
1fc21 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
1fc22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc23 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
1fc24 2f 0a 20 0a 20 20 61 73 73 65 72 74 28 20 69 56 /. . assert( iV
1fc25 65 72 73 69 6f 6e 3d 3d 31 20 7c 7c 20 69 56 65 ersion==1 || iVe
1fc26 72 73 69 6f 6e 3d 3d 32 20 29 3b 0a 0a 20 20 2f rsion==2 );.. /
1fc27 2a 20 49 66 20 73 65 74 74 69 6e 67 20 74 68 65 * If setting the
1fc28 20 76 65 72 73 69 6f 6e 20 66 69 65 6c 64 73 20 version fields
1fc29 74 6f 20 31 2c 20 64 6f 20 6e 6f 74 20 61 75 74 to 1, do not aut
1fc2a 6f 6d 61 74 69 63 61 6c 6c 79 20 6f 70 65 6e 20 omatically open
1fc2b 74 68 65 0a 20 20 2a 2a 20 57 41 4c 20 63 6f 6e the. ** WAL con
1fc2c 6e 65 63 74 69 6f 6e 2c 20 65 76 65 6e 20 69 66 nection, even if
1fc2d 20 74 68 65 20 76 65 72 73 69 6f 6e 20 66 69 65 the version fie
1fc2e 6c 64 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c lds are currentl
1fc2f 79 20 73 65 74 20 74 6f 20 32 2e 0a 20 20 2a 2f y set to 2.. */
1fc30 0a 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 . pBt->btsFlags
1fc31 20 26 3d 20 7e 42 54 53 5f 4e 4f 5f 57 41 4c 3b &= ~BTS_NO_WAL;
1fc32 0a 20 20 69 66 28 20 69 56 65 72 73 69 6f 6e 3d . if( iVersion=
1fc33 3d 31 20 29 20 70 42 74 2d 3e 62 74 73 46 6c 61 =1 ) pBt->btsFla
1fc34 67 73 20 7c 3d 20 42 54 53 5f 4e 4f 5f 57 41 4c gs |= BTS_NO_WAL
1fc35 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 ;.. rc = sqlite
1fc36 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 3BtreeBeginTrans
1fc37 28 70 42 74 72 65 65 2c 20 30 29 3b 0a 20 20 69 (pBtree, 0);. i
1fc38 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1fc39 20 29 7b 0a 20 20 20 20 75 38 20 2a 61 44 61 74 ){. u8 *aDat
1fc3a 61 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 2d a = pBt->pPage1-
1fc3b 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 >aData;. if(
1fc3c 61 44 61 74 61 5b 31 38 5d 21 3d 28 75 38 29 69 aData[18]!=(u8)i
1fc3d 56 65 72 73 69 6f 6e 20 7c 7c 20 61 44 61 74 61 Version || aData
1fc3e 5b 31 39 5d 21 3d 28 75 38 29 69 56 65 72 73 69 [19]!=(u8)iVersi
1fc3f 6f 6e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d on ){. rc =
1fc40 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 sqlite3BtreeBeg
1fc41 69 6e 54 72 61 6e 73 28 70 42 74 72 65 65 2c 20 inTrans(pBtree,
1fc42 32 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 2);. if( rc
1fc43 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1fc44 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1fc45 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 42 te3PagerWrite(pB
1fc46 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 t->pPage1->pDbPa
1fc47 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ge);. if(
1fc48 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1fc49 7b 0a 20 20 20 20 20 20 20 20 20 20 61 44 61 74 {. aDat
1fc4a 61 5b 31 38 5d 20 3d 20 28 75 38 29 69 56 65 72 a[18] = (u8)iVer
1fc4b 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 sion;.
1fc4c 61 44 61 74 61 5b 31 39 5d 20 3d 20 28 75 38 29 aData[19] = (u8)
1fc4d 69 56 65 72 73 69 6f 6e 3b 0a 20 20 20 20 20 20 iVersion;.
1fc4e 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1fc4f 7d 0a 20 20 7d 0a 0a 20 20 70 42 74 2d 3e 62 74 }. }.. pBt->bt
1fc50 73 46 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f 4e sFlags &= ~BTS_N
1fc51 4f 5f 57 41 4c 3b 0a 20 20 72 65 74 75 72 6e 20 O_WAL;. return
1fc52 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 65 74 rc;.}../*.** set
1fc53 20 74 68 65 20 6d 61 73 6b 20 6f 66 20 68 69 6e the mask of hin
1fc54 74 20 66 6c 61 67 73 20 66 6f 72 20 63 75 72 73 t flags for curs
1fc55 6f 72 20 70 43 73 72 2e 20 43 75 72 72 65 6e 74 or pCsr. Current
1fc56 6c 79 20 74 68 65 20 6f 6e 6c 79 20 76 61 6c 69 ly the only vali
1fc57 64 0a 2a 2a 20 76 61 6c 75 65 73 20 61 72 65 20 d.** values are
1fc58 30 20 61 6e 64 20 42 54 52 45 45 5f 42 55 4c 4b 0 and BTREE_BULK
1fc59 4c 4f 41 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f LOAD..*/.SQLITE_
1fc5a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1fc5b 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 ite3BtreeCursorH
1fc5c 69 6e 74 73 28 42 74 43 75 72 73 6f 72 20 2a 70 ints(BtCursor *p
1fc5d 43 73 72 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e Csr, unsigned in
1fc5e 74 20 6d 61 73 6b 29 7b 0a 20 20 61 73 73 65 72 t mask){. asser
1fc5f 74 28 20 6d 61 73 6b 3d 3d 42 54 52 45 45 5f 42 t( mask==BTREE_B
1fc60 55 4c 4b 4c 4f 41 44 20 7c 7c 20 6d 61 73 6b 3d ULKLOAD || mask=
1fc61 3d 30 20 29 3b 0a 20 20 70 43 73 72 2d 3e 68 69 =0 );. pCsr->hi
1fc62 6e 74 73 20 3d 20 6d 61 73 6b 3b 0a 7d 0a 0a 2f nts = mask;.}../
1fc63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
1fc64 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a nd of btree.c **
1fc65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1fc68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
1fc69 65 67 69 6e 20 66 69 6c 65 20 62 61 63 6b 75 70 egin file backup
1fc6a 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1fc6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1fc6d 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61 72 *.** 2009 Januar
1fc6e 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 y 28.**.** The a
1fc6f 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
1fc70 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
1fc71 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
1fc72 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
1fc73 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
1fc74 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
1fc75 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
1fc76 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
1fc77 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
1fc78 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
1fc79 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
1fc7a 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
1fc7b 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
1fc7c 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
1fc7d 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
1fc7e 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
1fc7f 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
1fc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc84 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
1fc85 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d contains the im
1fc86 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
1fc87 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b the sqlite3_back
1fc88 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 41 50 49 up_XXX() .** API
1fc89 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 74 functions and t
1fc8a 68 65 20 72 65 6c 61 74 65 64 20 66 65 61 74 75 he related featu
1fc8b 72 65 73 2e 0a 2a 2f 0a 0a 2f 2a 20 4d 61 63 72 res..*/../* Macr
1fc8c 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 o to find the mi
1fc8d 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d nimum of two num
1fc8e 65 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a eric values..*/.
1fc8f 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 #ifndef MIN.# de
1fc90 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 fine MIN(x,y) ((
1fc91 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 0a x)<(y)?(x):(y)).
1fc92 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 74 #endif../*.** St
1fc93 72 75 63 74 75 72 65 20 61 6c 6c 6f 63 61 74 65 ructure allocate
1fc94 64 20 66 6f 72 20 65 61 63 68 20 62 61 63 6b 75 d for each backu
1fc95 70 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a p operation..*/.
1fc96 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62 struct sqlite3_b
1fc97 61 63 6b 75 70 20 7b 0a 20 20 73 71 6c 69 74 65 ackup {. sqlite
1fc98 33 2a 20 70 44 65 73 74 44 62 3b 20 20 20 20 20 3* pDestDb;
1fc99 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f /* Destinatio
1fc9a 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c n database handl
1fc9b 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 44 e */. Btree *pD
1fc9c 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 est;
1fc9d 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 62 /* Destination b
1fc9e 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0a 20 20 -tree file */.
1fc9f 75 33 32 20 69 44 65 73 74 53 63 68 65 6d 61 3b u32 iDestSchema;
1fca0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 /* Orig
1fca1 69 6e 61 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b inal schema cook
1fca2 69 65 20 69 6e 20 64 65 73 74 69 6e 61 74 69 6f ie in destinatio
1fca3 6e 20 2a 2f 0a 20 20 69 6e 74 20 62 44 65 73 74 n */. int bDest
1fca4 4c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 Locked;
1fca5 2f 2a 20 54 72 75 65 20 6f 6e 63 65 20 61 20 77 /* True once a w
1fca6 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
1fca7 20 69 73 20 6f 70 65 6e 20 6f 6e 20 70 44 65 73 is open on pDes
1fca8 74 20 2a 2f 0a 0a 20 20 50 67 6e 6f 20 69 4e 65 t */.. Pgno iNe
1fca9 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 xt;
1fcaa 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 /* Page number
1fcab 6f 66 20 74 68 65 20 6e 65 78 74 20 73 6f 75 72 of the next sour
1fcac 63 65 20 70 61 67 65 20 74 6f 20 63 6f 70 79 20 ce page to copy
1fcad 2a 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53 */. sqlite3* pS
1fcae 72 63 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a rcDb; /*
1fcaf 20 53 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 Source database
1fcb0 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 handle */. Btr
1fcb1 65 65 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20 ee *pSrc;
1fcb2 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 /* Source
1fcb3 62 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0a 0a b-tree file */..
1fcb4 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
1fcb5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 /* Ba
1fcb6 63 6b 75 70 20 70 72 6f 63 65 73 73 20 65 72 72 ckup process err
1fcb7 6f 72 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a or code */.. /*
1fcb8 20 54 68 65 73 65 20 74 77 6f 20 76 61 72 69 61 These two varia
1fcb9 62 6c 65 73 20 61 72 65 20 73 65 74 20 62 79 20 bles are set by
1fcba 65 76 65 72 79 20 63 61 6c 6c 20 74 6f 20 62 61 every call to ba
1fcbb 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 54 68 65 ckup_step(). The
1fcbc 79 20 61 72 65 0a 20 20 2a 2a 20 72 65 61 64 20 y are. ** read
1fcbd 62 79 20 63 61 6c 6c 73 20 74 6f 20 62 61 63 6b by calls to back
1fcbe 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 up_remaining() a
1fcbf 6e 64 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f nd backup_pageco
1fcc0 75 6e 74 28 29 2e 0a 20 20 2a 2f 0a 20 20 50 67 unt().. */. Pg
1fcc1 6e 6f 20 6e 52 65 6d 61 69 6e 69 6e 67 3b 20 20 no nRemaining;
1fcc2 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1fcc3 20 6f 66 20 70 61 67 65 73 20 6c 65 66 74 20 74 of pages left t
1fcc4 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 50 67 6e 6f o copy */. Pgno
1fcc5 20 6e 50 61 67 65 63 6f 75 6e 74 3b 20 20 20 20 nPagecount;
1fcc6 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 /* Total nu
1fcc7 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f mber of pages to
1fcc8 20 63 6f 70 79 20 2a 2f 0a 0a 20 20 69 6e 74 20 copy */.. int
1fcc9 69 73 41 74 74 61 63 68 65 64 3b 20 20 20 20 20 isAttached;
1fcca 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63 /* True onc
1fccb 65 20 62 61 63 6b 75 70 20 68 61 73 20 62 65 65 e backup has bee
1fccc 6e 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 n registered wit
1fccd 68 20 70 61 67 65 72 20 2a 2f 0a 20 20 73 71 6c h pager */. sql
1fcce 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 4e 65 ite3_backup *pNe
1fccf 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 62 61 xt; /* Next ba
1fcd0 63 6b 75 70 20 61 73 73 6f 63 69 61 74 65 64 20 ckup associated
1fcd1 77 69 74 68 20 73 6f 75 72 63 65 20 70 61 67 65 with source page
1fcd2 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 r */.};../*.** T
1fcd3 48 52 45 41 44 20 53 41 46 45 54 59 20 4e 4f 54 HREAD SAFETY NOT
1fcd4 45 53 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 6e 63 65 ES:.**.** Once
1fcd5 20 69 74 20 68 61 73 20 62 65 65 6e 20 63 72 65 it has been cre
1fcd6 61 74 65 64 20 75 73 69 6e 67 20 62 61 63 6b 75 ated using backu
1fcd7 70 5f 69 6e 69 74 28 29 2c 20 61 20 73 69 6e 67 p_init(), a sing
1fcd8 6c 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 le sqlite3_backu
1fcd9 70 0a 2a 2a 20 20 20 73 74 72 75 63 74 75 72 65 p.** structure
1fcda 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64 may be accessed
1fcdb 20 76 69 61 20 74 77 6f 20 67 72 6f 75 70 73 20 via two groups
1fcdc 6f 66 20 74 68 72 65 61 64 2d 73 61 66 65 20 65 of thread-safe e
1fcdd 6e 74 72 79 20 70 6f 69 6e 74 73 3a 0a 2a 2a 0a ntry points:.**.
1fcde 2a 2a 20 20 20 20 20 2a 20 56 69 61 20 74 68 65 ** * Via the
1fcdf 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
1fce0 58 58 58 28 29 20 41 50 49 20 66 75 6e 63 74 69 XXX() API functi
1fce1 6f 6e 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 on backup_step()
1fce2 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 20 20 62 and .** b
1fce3 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20 ackup_finish().
1fce4 42 6f 74 68 20 74 68 65 73 65 20 66 75 6e 63 74 Both these funct
1fce5 69 6f 6e 73 20 6f 62 74 61 69 6e 20 74 68 65 20 ions obtain the
1fce6 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 0a source database.
1fce7 2a 2a 20 20 20 20 20 20 20 68 61 6e 64 6c 65 20 ** handle
1fce8 6d 75 74 65 78 20 61 6e 64 20 74 68 65 20 6d 75 mutex and the mu
1fce9 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 tex associated w
1fcea 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 42 ith the source B
1fceb 74 53 68 61 72 65 64 20 0a 2a 2a 20 20 20 20 20 tShared .**
1fcec 20 20 73 74 72 75 63 74 75 72 65 2c 20 69 6e 20 structure, in
1fced 74 68 61 74 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a that order..**.*
1fcee 2a 20 20 20 20 20 2a 20 56 69 61 20 74 68 65 20 * * Via the
1fcef 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 20 61 BackupUpdate() a
1fcf0 6e 64 20 42 61 63 6b 75 70 52 65 73 74 61 72 74 nd BackupRestart
1fcf1 28 29 20 66 75 6e 63 74 69 6f 6e 73 2c 20 77 68 () functions, wh
1fcf2 69 63 68 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 ich are.**
1fcf3 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20 invoked by the
1fcf4 70 61 67 65 72 20 6c 61 79 65 72 20 74 6f 20 72 pager layer to r
1fcf5 65 70 6f 72 74 20 76 61 72 69 6f 75 73 20 73 74 eport various st
1fcf6 61 74 65 20 63 68 61 6e 67 65 73 20 69 6e 0a 2a ate changes in.*
1fcf7 2a 20 20 20 20 20 20 20 74 68 65 20 70 61 67 65 * the page
1fcf8 20 63 61 63 68 65 20 61 73 73 6f 63 69 61 74 65 cache associate
1fcf9 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 72 63 d with the sourc
1fcfa 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 e database. The
1fcfb 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20 20 20 61 mutex.** a
1fcfc 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
1fcfd 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
1fcfe 73 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 se BtShared stru
1fcff 63 74 75 72 65 20 77 69 6c 6c 20 61 6c 77 61 79 cture will alway
1fd00 73 20 0a 2a 2a 20 20 20 20 20 20 20 62 65 20 68 s .** be h
1fd01 65 6c 64 20 77 68 65 6e 20 65 69 74 68 65 72 20 eld when either
1fd02 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f of these functio
1fd03 6e 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 2e 0a ns are invoked..
1fd04 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 6f 74 68 65 **.** The othe
1fd05 72 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 r sqlite3_backup
1fd06 5f 58 58 58 28 29 20 41 50 49 20 66 75 6e 63 74 _XXX() API funct
1fd07 69 6f 6e 73 2c 20 62 61 63 6b 75 70 5f 72 65 6d ions, backup_rem
1fd08 61 69 6e 69 6e 67 28 29 20 61 6e 64 0a 2a 2a 20 aining() and.**
1fd09 20 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 backup_pagecou
1fd0a 6e 74 28 29 20 61 72 65 20 6e 6f 74 20 74 68 72 nt() are not thr
1fd0b 65 61 64 2d 73 61 66 65 20 66 75 6e 63 74 69 6f ead-safe functio
1fd0c 6e 73 2e 20 49 66 20 74 68 65 79 20 61 72 65 20 ns. If they are
1fd0d 63 61 6c 6c 65 64 0a 2a 2a 20 20 20 77 68 69 6c called.** whil
1fd0e 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 e some other thr
1fd0f 65 61 64 20 69 73 20 63 61 6c 6c 69 6e 67 20 62 ead is calling b
1fd10 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6f 72 20 ackup_step() or
1fd11 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2c backup_finish(),
1fd12 0a 2a 2a 20 20 20 74 68 65 20 76 61 6c 75 65 73 .** the values
1fd13 20 72 65 74 75 72 6e 65 64 20 6d 61 79 20 62 65 returned may be
1fd14 20 69 6e 76 61 6c 69 64 2e 20 54 68 65 72 65 20 invalid. There
1fd15 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 61 20 is no way for a
1fd16 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 42 61 63 call to.** Bac
1fd17 6b 75 70 55 70 64 61 74 65 28 29 20 6f 72 20 42 kupUpdate() or B
1fd18 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20 74 ackupRestart() t
1fd19 6f 20 69 6e 74 65 72 66 65 72 65 20 77 69 74 68 o interfere with
1fd1a 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e backup_remainin
1fd1b 67 28 29 0a 2a 2a 20 20 20 6f 72 20 62 61 63 6b g().** or back
1fd1c 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a up_pagecount()..
1fd1d 2a 2a 0a 2a 2a 20 20 20 44 65 70 65 6e 64 69 6e **.** Dependin
1fd1e 67 20 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20 g on the SQLite
1fd1f 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 74 configuration, t
1fd20 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 he database hand
1fd21 6c 65 73 20 61 6e 64 2f 6f 72 0a 2a 2a 20 20 20 les and/or.**
1fd22 74 68 65 20 42 74 72 65 65 20 6f 62 6a 65 63 74 the Btree object
1fd23 73 20 6d 61 79 20 68 61 76 65 20 74 68 65 69 72 s may have their
1fd24 20 6f 77 6e 20 6d 75 74 65 78 65 73 20 74 68 61 own mutexes tha
1fd25 74 20 72 65 71 75 69 72 65 20 6c 6f 63 6b 69 6e t require lockin
1fd26 67 2e 0a 2a 2a 20 20 20 4e 6f 6e 2d 73 68 61 72 g..** Non-shar
1fd27 61 62 6c 65 20 42 74 72 65 65 73 20 28 69 6e 2d able Btrees (in-
1fd28 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73 memory databases
1fd29 20 66 6f 72 20 65 78 61 6d 70 6c 65 29 2c 20 64 for example), d
1fd2a 6f 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 o not have.**
1fd2b 61 73 73 6f 63 69 61 74 65 64 20 6d 75 74 65 78 associated mutex
1fd2c 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 es..*/../*.** Re
1fd2d 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 63 turn a pointer c
1fd2e 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
1fd2f 64 61 74 61 62 61 73 65 20 7a 44 62 20 28 69 2e database zDb (i.
1fd30 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 e. "main", "temp
1fd31 22 29 0a 2a 2a 20 69 6e 20 63 6f 6e 6e 65 63 74 ").** in connect
1fd32 69 6f 6e 20 68 61 6e 64 6c 65 20 70 44 62 2e 20 ion handle pDb.
1fd33 49 66 20 73 75 63 68 20 61 20 64 61 74 61 62 61 If such a databa
1fd34 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 se cannot be fou
1fd35 6e 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 nd, return.** a
1fd36 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 6e 64 NULL pointer and
1fd37 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 write an error
1fd38 6d 65 73 73 61 67 65 20 74 6f 20 70 45 72 72 6f message to pErro
1fd39 72 44 62 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 rDb..**.** If th
1fd3a 65 20 22 74 65 6d 70 22 20 64 61 74 61 62 61 73 e "temp" databas
1fd3b 65 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 e is requested,
1fd3c 69 74 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 it may need to b
1fd3d 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73 e opened by this
1fd3e 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 49 .** function. I
1fd3f 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
1fd40 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f s while doing so
1fd41 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 77 , return 0 and w
1fd42 72 69 74 65 20 61 6e 20 0a 2a 2a 20 65 72 72 6f rite an .** erro
1fd43 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 45 72 r message to pEr
1fd44 72 6f 72 44 62 2e 0a 2a 2f 0a 73 74 61 74 69 63 rorDb..*/.static
1fd45 20 42 74 72 65 65 20 2a 66 69 6e 64 42 74 72 65 Btree *findBtre
1fd46 65 28 73 71 6c 69 74 65 33 20 2a 70 45 72 72 6f e(sqlite3 *pErro
1fd47 72 44 62 2c 20 73 71 6c 69 74 65 33 20 2a 70 44 rDb, sqlite3 *pD
1fd48 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
1fd49 44 62 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 73 Db){. int i = s
1fd4a 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 qlite3FindDbName
1fd4b 28 70 44 62 2c 20 7a 44 62 29 3b 0a 0a 20 20 69 (pDb, zDb);.. i
1fd4c 66 28 20 69 3d 3d 31 20 29 7b 0a 20 20 20 20 50 f( i==1 ){. P
1fd4d 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 20 20 arse *pParse;.
1fd4e 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 int rc = 0;.
1fd4f 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c 69 74 pParse = sqlit
1fd50 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f e3StackAllocZero
1fd51 28 70 45 72 72 6f 72 44 62 2c 20 73 69 7a 65 6f (pErrorDb, sizeo
1fd52 66 28 2a 70 50 61 72 73 65 29 29 3b 0a 20 20 20 f(*pParse));.
1fd53 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 29 if( pParse==0 )
1fd54 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1fd55 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 53 rror(pErrorDb, S
1fd56 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 22 6f 75 QLITE_NOMEM, "ou
1fd57 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 t of memory");.
1fd58 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1fd59 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 _NOMEM;. }els
1fd5a 65 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d e{. pParse-
1fd5b 3e 64 62 20 3d 20 70 44 62 3b 0a 20 20 20 20 20 >db = pDb;.
1fd5c 20 69 66 28 20 73 71 6c 69 74 65 33 4f 70 65 6e if( sqlite3Open
1fd5d 54 65 6d 70 44 61 74 61 62 61 73 65 28 70 50 61 TempDatabase(pPa
1fd5e 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 rse) ){.
1fd5f 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45 72 sqlite3Error(pEr
1fd60 72 6f 72 44 62 2c 20 70 50 61 72 73 65 2d 3e 72 rorDb, pParse->r
1fd61 63 2c 20 22 25 73 22 2c 20 70 50 61 72 73 65 2d c, "%s", pParse-
1fd62 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 >zErrMsg);.
1fd63 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
1fd64 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 RROR;. }.
1fd65 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1fd66 65 28 70 45 72 72 6f 72 44 62 2c 20 70 50 61 72 e(pErrorDb, pPar
1fd67 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 se->zErrMsg);.
1fd68 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b sqlite3Stack
1fd69 46 72 65 65 28 70 45 72 72 6f 72 44 62 2c 20 70 Free(pErrorDb, p
1fd6a 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20 Parse);. }.
1fd6b 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
1fd6c 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
1fd6d 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3c 30 }. }.. if( i<0
1fd6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
1fd6f 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 53 rror(pErrorDb, S
1fd70 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 75 6e QLITE_ERROR, "un
1fd71 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 65 20 25 known database %
1fd72 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20 72 65 s", zDb);. re
1fd73 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 turn 0;. }.. r
1fd74 65 74 75 72 6e 20 70 44 62 2d 3e 61 44 62 5b 69 eturn pDb->aDb[i
1fd75 5d 2e 70 42 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ].pBt;.}../*.**
1fd76 41 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20 74 Attempt to set t
1fd77 68 65 20 70 61 67 65 20 73 69 7a 65 20 6f 66 20 he page size of
1fd78 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
1fd79 74 6f 20 6d 61 74 63 68 20 74 68 65 20 70 61 67 to match the pag
1fd7a 65 20 73 69 7a 65 0a 2a 2a 20 6f 66 20 74 68 65 e size.** of the
1fd7b 20 73 6f 75 72 63 65 2e 0a 2a 2f 0a 73 74 61 74 source..*/.stat
1fd7c 69 63 20 69 6e 74 20 73 65 74 44 65 73 74 50 67 ic int setDestPg
1fd7d 73 7a 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 sz(sqlite3_backu
1fd7e 70 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b p *p){. int rc;
1fd7f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 . rc = sqlite3B
1fd80 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 treeSetPageSize(
1fd81 70 2d 3e 70 44 65 73 74 2c 73 71 6c 69 74 65 33 p->pDest,sqlite3
1fd82 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 BtreeGetPageSize
1fd83 28 70 2d 3e 70 53 72 63 29 2c 2d 31 2c 30 29 3b (p->pSrc),-1,0);
1fd84 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1fd85 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 6e ./*.** Create an
1fd86 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
1fd87 70 72 6f 63 65 73 73 20 74 6f 20 63 6f 70 79 20 process to copy
1fd88 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
1fd89 7a 53 72 63 44 62 20 66 72 6f 6d 0a 2a 2a 20 63 zSrcDb from.** c
1fd8a 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 onnection handle
1fd8b 20 70 53 72 63 44 62 20 74 6f 20 7a 44 65 73 74 pSrcDb to zDest
1fd8c 44 62 20 69 6e 20 70 44 65 73 74 44 62 2e 20 49 Db in pDestDb. I
1fd8d 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 f successful, re
1fd8e 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 turn.** a pointe
1fd8f 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 71 6c r to the new sql
1fd90 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 ite3_backup obje
1fd91 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ct..**.** If an
1fd92 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 error occurs, NU
1fd93 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 LL is returned a
1fd94 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 nd an error code
1fd95 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 and error messa
1fd96 67 65 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 ge.** stored in
1fd97 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 database handle
1fd98 70 44 65 73 74 44 62 2e 0a 2a 2f 0a 53 51 4c 49 pDestDb..*/.SQLI
1fd99 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 62 TE_API sqlite3_b
1fd9a 61 63 6b 75 70 20 2a 73 71 6c 69 74 65 33 5f 62 ackup *sqlite3_b
1fd9b 61 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20 73 71 ackup_init(. sq
1fd9c 6c 69 74 65 33 2a 20 70 44 65 73 74 44 62 2c 20 lite3* pDestDb,
1fd9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fd9e 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
1fd9f 74 6f 20 77 72 69 74 65 20 74 6f 20 2a 2f 0a 20 to write to */.
1fda0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 65 const char *zDe
1fda1 73 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 stDb,
1fda2 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
1fda3 66 20 64 61 74 61 62 61 73 65 20 77 69 74 68 69 f database withi
1fda4 6e 20 70 44 65 73 74 44 62 20 2a 2f 0a 20 20 73 n pDestDb */. s
1fda5 71 6c 69 74 65 33 2a 20 70 53 72 63 44 62 2c 20 qlite3* pSrcDb,
1fda6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fda7 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
1fda8 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 connection to r
1fda9 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 63 6f ead from */. co
1fdaa 6e 73 74 20 63 68 61 72 20 2a 7a 53 72 63 44 62 nst char *zSrcDb
1fdab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdac 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 /* Name of d
1fdad 61 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 70 atabase within p
1fdae 53 72 63 44 62 20 2a 2f 0a 29 7b 0a 20 20 73 71 SrcDb */.){. sq
1fdaf 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b lite3_backup *p;
1fdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdb1 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 /* Value to
1fdb2 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 return */.. /*
1fdb3 4c 6f 63 6b 20 74 68 65 20 73 6f 75 72 63 65 20 Lock the source
1fdb4 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e database handle.
1fdb5 20 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e The destination
1fdb6 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 68 database. ** h
1fdb7 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63 andle is not loc
1fdb8 6b 65 64 20 69 6e 20 74 68 69 73 20 72 6f 75 74 ked in this rout
1fdb9 69 6e 65 2c 20 62 75 74 20 69 74 20 69 73 20 6c ine, but it is l
1fdba 6f 63 6b 65 64 20 69 6e 0a 20 20 2a 2a 20 73 71 ocked in. ** sq
1fdbb 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
1fdbc 70 28 29 2e 20 54 68 65 20 75 73 65 72 20 69 73 p(). The user is
1fdbd 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 6e 73 required to ens
1fdbe 75 72 65 20 74 68 61 74 20 6e 6f 0a 20 20 2a 2a ure that no. **
1fdbf 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 61 63 other thread ac
1fdc0 63 65 73 73 65 73 20 74 68 65 20 64 65 73 74 69 cesses the desti
1fdc1 6e 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 6f nation handle fo
1fdc2 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 0a 20 r the duration.
1fdc3 20 2a 2a 20 6f 66 20 74 68 65 20 62 61 63 6b 75 ** of the backu
1fdc4 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e p operation. An
1fdc5 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 y attempt to use
1fdc6 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
1fdc7 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63 . ** database c
1fdc8 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 onnection while
1fdc9 61 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20 70 a backup is in p
1fdca 72 6f 67 72 65 73 73 20 6d 61 79 20 63 61 75 73 rogress may caus
1fdcb 65 0a 20 20 2a 2a 20 61 20 6d 61 6c 66 75 6e 63 e. ** a malfunc
1fdcc 74 69 6f 6e 20 6f 72 20 61 20 64 65 61 64 6c 6f tion or a deadlo
1fdcd 63 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 ck.. */. sqlit
1fdce 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 e3_mutex_enter(p
1fdcf 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 SrcDb->mutex);.
1fdd0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1fdd1 6e 74 65 72 28 70 44 65 73 74 44 62 2d 3e 6d 75 nter(pDestDb->mu
1fdd2 74 65 78 29 3b 0a 0a 20 20 69 66 28 20 70 53 72 tex);.. if( pSr
1fdd3 63 44 62 3d 3d 70 44 65 73 74 44 62 20 29 7b 0a cDb==pDestDb ){.
1fdd4 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1fdd5 28 0a 20 20 20 20 20 20 20 20 70 44 65 73 74 44 (. pDestD
1fdd6 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c b, SQLITE_ERROR,
1fdd7 20 22 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 "source and des
1fdd8 74 69 6e 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 tination must be
1fdd9 20 64 69 73 74 69 6e 63 74 22 0a 20 20 20 20 29 distinct". )
1fdda 3b 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d ;. p = 0;. }
1fddb 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 41 6c else {. /* Al
1fddc 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 locate space for
1fddd 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 62 a new sqlite3_b
1fdde 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 2e 2e 0a ackup object....
1fddf 20 20 20 20 2a 2a 20 45 56 49 44 45 4e 43 45 2d ** EVIDENCE-
1fde0 4f 46 3a 20 52 2d 36 34 38 35 32 2d 32 31 35 39 OF: R-64852-2159
1fde1 31 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 61 1 The sqlite3_ba
1fde2 63 6b 75 70 20 6f 62 6a 65 63 74 20 69 73 20 63 ckup object is c
1fde3 72 65 61 74 65 64 20 62 79 20 61 0a 20 20 20 20 reated by a.
1fde4 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 ** call to sqlit
1fde5 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 e3_backup_init()
1fde6 20 61 6e 64 20 69 73 20 64 65 73 74 72 6f 79 65 and is destroye
1fde7 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 20 d by a call to.
1fde8 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 ** sqlite3_ba
1fde9 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20 2a ckup_finish(). *
1fdea 2f 0a 20 20 20 20 70 20 3d 20 28 73 71 6c 69 74 /. p = (sqlit
1fdeb 65 33 5f 62 61 63 6b 75 70 20 2a 29 73 71 6c 69 e3_backup *)sqli
1fdec 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 te3MallocZero(si
1fded 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63 zeof(sqlite3_bac
1fdee 6b 75 70 29 29 3b 0a 20 20 20 20 69 66 28 20 21 kup));. if( !
1fdef 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 p ){. sqlit
1fdf0 65 33 45 72 72 6f 72 28 70 44 65 73 74 44 62 2c e3Error(pDestDb,
1fdf1 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 SQLITE_NOMEM, 0
1fdf2 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
1fdf3 2f 2a 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 /* If the alloca
1fdf4 74 69 6f 6e 20 73 75 63 63 65 65 64 65 64 2c 20 tion succeeded,
1fdf5 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65 77 populate the new
1fdf6 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 69 66 object. */. if
1fdf7 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 ( p ){. p->pS
1fdf8 72 63 20 3d 20 66 69 6e 64 42 74 72 65 65 28 70 rc = findBtree(p
1fdf9 44 65 73 74 44 62 2c 20 70 53 72 63 44 62 2c 20 DestDb, pSrcDb,
1fdfa 7a 53 72 63 44 62 29 3b 0a 20 20 20 20 70 2d 3e zSrcDb);. p->
1fdfb 70 44 65 73 74 20 3d 20 66 69 6e 64 42 74 72 65 pDest = findBtre
1fdfc 65 28 70 44 65 73 74 44 62 2c 20 70 44 65 73 74 e(pDestDb, pDest
1fdfd 44 62 2c 20 7a 44 65 73 74 44 62 29 3b 0a 20 20 Db, zDestDb);.
1fdfe 20 20 70 2d 3e 70 44 65 73 74 44 62 20 3d 20 70 p->pDestDb = p
1fdff 44 65 73 74 44 62 3b 0a 20 20 20 20 70 2d 3e 70 DestDb;. p->p
1fe00 53 72 63 44 62 20 3d 20 70 53 72 63 44 62 3b 0a SrcDb = pSrcDb;.
1fe01 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31 p->iNext = 1
1fe02 3b 0a 20 20 20 20 70 2d 3e 69 73 41 74 74 61 63 ;. p->isAttac
1fe03 68 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 hed = 0;.. if
1fe04 28 20 30 3d 3d 70 2d 3e 70 53 72 63 20 7c 7c 20 ( 0==p->pSrc ||
1fe05 30 3d 3d 70 2d 3e 70 44 65 73 74 20 7c 7c 20 73 0==p->pDest || s
1fe06 65 74 44 65 73 74 50 67 73 7a 28 70 29 3d 3d 53 etDestPgsz(p)==S
1fe07 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 QLITE_NOMEM ){.
1fe08 20 20 20 20 20 2f 2a 20 4f 6e 65 20 28 6f 72 20 /* One (or
1fe09 62 6f 74 68 29 20 6f 66 20 74 68 65 20 6e 61 6d both) of the nam
1fe0a 65 64 20 64 61 74 61 62 61 73 65 73 20 64 69 64 ed databases did
1fe0b 20 6e 6f 74 20 65 78 69 73 74 20 6f 72 20 61 6e not exist or an
1fe0c 20 4f 4f 4d 0a 20 20 20 20 20 20 2a 2a 20 65 72 OOM. ** er
1fe0d 72 6f 72 20 77 61 73 20 68 69 74 2e 20 20 54 68 ror was hit. Th
1fe0e 65 20 65 72 72 6f 72 20 68 61 73 20 61 6c 72 65 e error has alre
1fe0f 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e ady been written
1fe10 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 20 into the.
1fe11 2a 2a 20 70 44 65 73 74 44 62 20 68 61 6e 64 6c ** pDestDb handl
1fe12 65 2e 20 20 41 6c 6c 20 74 68 61 74 20 69 73 20 e. All that is
1fe13 6c 65 66 74 20 74 6f 20 64 6f 20 68 65 72 65 20 left to do here
1fe14 69 73 20 66 72 65 65 20 74 68 65 0a 20 20 20 20 is free the.
1fe15 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 ** sqlite3_bac
1fe16 6b 75 70 20 73 74 72 75 63 74 75 72 65 2e 0a 20 kup structure..
1fe17 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 */. sq
1fe18 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 lite3_free(p);.
1fe19 20 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 20 20 p = 0;.
1fe1a 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b }. }. if( p ){
1fe1b 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e 6e 42 . p->pSrc->nB
1fe1c 61 63 6b 75 70 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 ackup++;. }..
1fe1d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1fe1e 61 76 65 28 70 44 65 73 74 44 62 2d 3e 6d 75 74 ave(pDestDb->mut
1fe1f 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d ex);. sqlite3_m
1fe20 75 74 65 78 5f 6c 65 61 76 65 28 70 53 72 63 44 utex_leave(pSrcD
1fe21 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 b->mutex);. ret
1fe22 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
1fe23 41 72 67 75 6d 65 6e 74 20 72 63 20 69 73 20 61 Argument rc is a
1fe24 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 n SQLite error c
1fe25 6f 64 65 2e 20 52 65 74 75 72 6e 20 74 72 75 65 ode. Return true
1fe26 20 69 66 20 74 68 69 73 20 65 72 72 6f 72 20 69 if this error i
1fe27 73 20 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 s .** considered
1fe28 20 66 61 74 61 6c 20 69 66 20 65 6e 63 6f 75 6e fatal if encoun
1fe29 74 65 72 65 64 20 64 75 72 69 6e 67 20 61 20 62 tered during a b
1fe2a 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e ackup operation.
1fe2b 20 41 6c 6c 20 65 72 72 6f 72 73 0a 2a 2a 20 61 All errors.** a
1fe2c 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61 re considered fa
1fe2d 74 61 6c 20 65 78 63 65 70 74 20 66 6f 72 20 53 tal except for S
1fe2e 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 20 53 QLITE_BUSY and S
1fe2f 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f QLITE_LOCKED..*/
1fe30 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 46 61 .static int isFa
1fe31 74 61 6c 45 72 72 6f 72 28 69 6e 74 20 72 63 29 talError(int rc)
1fe32 7b 0a 20 20 72 65 74 75 72 6e 20 28 72 63 21 3d {. return (rc!=
1fe33 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 SQLITE_OK && rc!
1fe34 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 =SQLITE_BUSY &&
1fe35 41 4c 57 41 59 53 28 72 63 21 3d 53 51 4c 49 54 ALWAYS(rc!=SQLIT
1fe36 45 5f 4c 4f 43 4b 45 44 29 29 3b 0a 7d 0a 0a 2f E_LOCKED));.}../
1fe37 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a *.** Parameter z
1fe38 53 72 63 44 61 74 61 20 70 6f 69 6e 74 73 20 74 SrcData points t
1fe39 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61 o a buffer conta
1fe3a 69 6e 69 6e 67 20 74 68 65 20 64 61 74 61 20 66 ining the data f
1fe3b 6f 72 20 0a 2a 2a 20 70 61 67 65 20 69 53 72 63 or .** page iSrc
1fe3c 50 67 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 Pg from the sour
1fe3d 63 65 20 64 61 74 61 62 61 73 65 2e 20 43 6f 70 ce database. Cop
1fe3e 79 20 74 68 69 73 20 64 61 74 61 20 69 6e 74 6f y this data into
1fe3f 20 74 68 65 20 0a 2a 2a 20 64 65 73 74 69 6e 61 the .** destina
1fe40 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 0a 2a tion database..*
1fe41 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 63 /.static int bac
1fe42 6b 75 70 4f 6e 65 50 61 67 65 28 73 71 6c 69 74 kupOnePage(sqlit
1fe43 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 50 67 e3_backup *p, Pg
1fe44 6e 6f 20 69 53 72 63 50 67 2c 20 63 6f 6e 73 74 no iSrcPg, const
1fe45 20 75 38 20 2a 7a 53 72 63 44 61 74 61 29 7b 0a u8 *zSrcData){.
1fe46 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 Pager * const
1fe47 70 44 65 73 74 50 61 67 65 72 20 3d 20 73 71 6c pDestPager = sql
1fe48 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 ite3BtreePager(p
1fe49 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73 ->pDest);. cons
1fe4a 74 20 69 6e 74 20 6e 53 72 63 50 67 73 7a 20 3d t int nSrcPgsz =
1fe4b 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
1fe4c 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53 72 63 PageSize(p->pSrc
1fe4d 29 3b 0a 20 20 69 6e 74 20 6e 44 65 73 74 50 67 );. int nDestPg
1fe4e 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 sz = sqlite3Btre
1fe4f 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e eGetPageSize(p->
1fe50 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73 74 20 pDest);. const
1fe51 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e 28 int nCopy = MIN(
1fe52 6e 53 72 63 50 67 73 7a 2c 20 6e 44 65 73 74 50 nSrcPgsz, nDestP
1fe53 67 73 7a 29 3b 0a 20 20 63 6f 6e 73 74 20 69 36 gsz);. const i6
1fe54 34 20 69 45 6e 64 20 3d 20 28 69 36 34 29 69 53 4 iEnd = (i64)iS
1fe55 72 63 50 67 2a 28 69 36 34 29 6e 53 72 63 50 67 rcPg*(i64)nSrcPg
1fe56 73 7a 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 sz;.#ifdef SQLIT
1fe57 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 2f 2a E_HAS_CODEC. /*
1fe58 20 55 73 65 20 42 74 72 65 65 47 65 74 52 65 73 Use BtreeGetRes
1fe59 65 72 76 65 4e 6f 4d 75 74 65 78 28 29 20 66 6f erveNoMutex() fo
1fe5a 72 20 74 68 65 20 73 6f 75 72 63 65 20 62 2d 74 r the source b-t
1fe5b 72 65 65 2c 20 61 73 20 61 6c 74 68 6f 75 67 68 ree, as although
1fe5c 20 69 74 20 69 73 0a 20 20 2a 2a 20 67 75 61 72 it is. ** guar
1fe5d 61 6e 74 65 65 64 20 74 68 61 74 20 74 68 65 20 anteed that the
1fe5e 73 68 61 72 65 64 2d 6d 75 74 65 78 20 69 73 20 shared-mutex is
1fe5f 68 65 6c 64 20 62 79 20 74 68 69 73 20 74 68 72 held by this thr
1fe60 65 61 64 2c 20 68 61 6e 64 6c 65 0a 20 20 2a 2a ead, handle. **
1fe61 20 70 2d 3e 70 53 72 63 20 6d 61 79 20 6e 6f 74 p->pSrc may not
1fe62 20 61 63 74 75 61 6c 6c 79 20 62 65 20 74 68 65 actually be the
1fe63 20 6f 77 6e 65 72 2e 20 20 2a 2f 0a 20 20 69 6e owner. */. in
1fe64 74 20 6e 53 72 63 52 65 73 65 72 76 65 20 3d 20 t nSrcReserve =
1fe65 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 sqlite3BtreeGetR
1fe66 65 73 65 72 76 65 4e 6f 4d 75 74 65 78 28 70 2d eserveNoMutex(p-
1fe67 3e 70 53 72 63 29 3b 0a 20 20 69 6e 74 20 6e 44 >pSrc);. int nD
1fe68 65 73 74 52 65 73 65 72 76 65 20 3d 20 73 71 6c estReserve = sql
1fe69 69 74 65 33 42 74 72 65 65 47 65 74 52 65 73 65 ite3BtreeGetRese
1fe6a 72 76 65 28 70 2d 3e 70 44 65 73 74 29 3b 0a 23 rve(p->pDest);.#
1fe6b 65 6e 64 69 66 0a 20 20 69 6e 74 20 72 63 20 3d endif. int rc =
1fe6c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 36 SQLITE_OK;. i6
1fe6d 34 20 69 4f 66 66 3b 0a 0a 20 20 61 73 73 65 72 4 iOff;.. asser
1fe6e 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 t( sqlite3BtreeG
1fe6f 65 74 52 65 73 65 72 76 65 4e 6f 4d 75 74 65 78 etReserveNoMutex
1fe70 28 70 2d 3e 70 53 72 63 29 3e 3d 30 20 29 3b 0a (p->pSrc)>=0 );.
1fe71 20 20 61 73 73 65 72 74 28 20 70 2d 3e 62 44 65 assert( p->bDe
1fe72 73 74 4c 6f 63 6b 65 64 20 29 3b 0a 20 20 61 73 stLocked );. as
1fe73 73 65 72 74 28 20 21 69 73 46 61 74 61 6c 45 72 sert( !isFatalEr
1fe74 72 6f 72 28 70 2d 3e 72 63 29 20 29 3b 0a 20 20 ror(p->rc) );.
1fe75 61 73 73 65 72 74 28 20 69 53 72 63 50 67 21 3d assert( iSrcPg!=
1fe76 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
1fe77 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 E(p->pSrc->pBt)
1fe78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53 72 );. assert( zSr
1fe79 63 44 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 43 cData );.. /* C
1fe7a 61 74 63 68 20 74 68 65 20 63 61 73 65 20 77 68 atch the case wh
1fe7b 65 72 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 ere the destinat
1fe7c 69 6f 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d ion is an in-mem
1fe7d 6f 72 79 20 64 61 74 61 62 61 73 65 20 61 6e 64 ory database and
1fe7e 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 73 the. ** page s
1fe7f 69 7a 65 73 20 6f 66 20 74 68 65 20 73 6f 75 72 izes of the sour
1fe80 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 ce and destinati
1fe81 6f 6e 20 64 69 66 66 65 72 2e 20 0a 20 20 2a 2f on differ. . */
1fe82 0a 20 20 69 66 28 20 6e 53 72 63 50 67 73 7a 21 . if( nSrcPgsz!
1fe83 3d 6e 44 65 73 74 50 67 73 7a 20 26 26 20 73 71 =nDestPgsz && sq
1fe84 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 lite3PagerIsMemd
1fe85 62 28 70 44 65 73 74 50 61 67 65 72 29 20 29 7b b(pDestPager) ){
1fe86 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1fe87 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a _READONLY;. }..
1fe88 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 #ifdef SQLITE_HA
1fe89 53 5f 43 4f 44 45 43 0a 20 20 2f 2a 20 42 61 63 S_CODEC. /* Bac
1fe8a 6b 75 70 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 kup is not possi
1fe8b 62 6c 65 20 69 66 20 74 68 65 20 70 61 67 65 20 ble if the page
1fe8c 73 69 7a 65 20 6f 66 20 74 68 65 20 64 65 73 74 size of the dest
1fe8d 69 6e 61 74 69 6f 6e 20 69 73 20 63 68 61 6e 67 ination is chang
1fe8e 69 6e 67 0a 20 20 2a 2a 20 61 6e 64 20 61 20 63 ing. ** and a c
1fe8f 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 2e 0a odec is in use..
1fe90 20 20 2a 2f 0a 20 20 69 66 28 20 6e 53 72 63 50 */. if( nSrcP
1fe91 67 73 7a 21 3d 6e 44 65 73 74 50 67 73 7a 20 26 gsz!=nDestPgsz &
1fe92 26 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 & sqlite3PagerGe
1fe93 74 43 6f 64 65 63 28 70 44 65 73 74 50 61 67 65 tCodec(pDestPage
1fe94 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 r)!=0 ){. rc
1fe95 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c = SQLITE_READONL
1fe96 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 61 63 Y;. }.. /* Bac
1fe97 6b 75 70 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 kup is not possi
1fe98 62 6c 65 20 69 66 20 74 68 65 20 6e 75 6d 62 65 ble if the numbe
1fe99 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 72 65 r of bytes of re
1fe9a 73 65 72 76 65 20 73 70 61 63 65 20 64 69 66 66 serve space diff
1fe9b 65 72 0a 20 20 2a 2a 20 62 65 74 77 65 65 6e 20 er. ** between
1fe9c 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 source and desti
1fe9d 6e 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 72 nation. If ther
1fe9e 65 20 69 73 20 61 20 64 69 66 66 65 72 65 6e 63 e is a differenc
1fe9f 65 2c 20 74 72 79 20 74 6f 0a 20 20 2a 2a 20 66 e, try to. ** f
1fea0 69 78 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 ix the destinati
1fea1 6f 6e 20 74 6f 20 61 67 72 65 65 20 77 69 74 68 on to agree with
1fea2 20 74 68 65 20 73 6f 75 72 63 65 2e 20 20 49 66 the source. If
1fea3 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 that is not pos
1fea4 73 69 62 6c 65 2c 0a 20 20 2a 2a 20 74 68 65 6e sible,. ** then
1fea5 20 74 68 65 20 62 61 63 6b 75 70 20 63 61 6e 6e the backup cann
1fea6 6f 74 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f ot proceed.. */
1fea7 0a 20 20 69 66 28 20 6e 53 72 63 52 65 73 65 72 . if( nSrcReser
1fea8 76 65 21 3d 6e 44 65 73 74 52 65 73 65 72 76 65 ve!=nDestReserve
1fea9 20 29 7b 0a 20 20 20 20 75 33 32 20 6e 65 77 50 ){. u32 newP
1feaa 67 73 7a 20 3d 20 6e 53 72 63 50 67 73 7a 3b 0a gsz = nSrcPgsz;.
1feab 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1feac 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
1fead 28 70 44 65 73 74 50 61 67 65 72 2c 20 26 6e 65 (pDestPager, &ne
1feae 77 50 67 73 7a 2c 20 6e 53 72 63 52 65 73 65 72 wPgsz, nSrcReser
1feaf 76 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ve);. if( rc=
1feb0 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 65 =SQLITE_OK && ne
1feb1 77 50 67 73 7a 21 3d 6e 53 72 63 50 67 73 7a 20 wPgsz!=nSrcPgsz
1feb2 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 ) rc = SQLITE_RE
1feb3 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 23 65 6e 64 ADONLY;. }.#end
1feb4 69 66 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f if.. /* This lo
1feb5 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 op runs once for
1feb6 20 65 61 63 68 20 64 65 73 74 69 6e 61 74 69 6f each destinatio
1feb7 6e 20 70 61 67 65 20 73 70 61 6e 6e 65 64 20 62 n page spanned b
1feb8 79 20 74 68 65 20 73 6f 75 72 63 65 20 0a 20 20 y the source .
1feb9 2a 2a 20 70 61 67 65 2e 20 46 6f 72 20 65 61 63 ** page. For eac
1feba 68 20 69 74 65 72 61 74 69 6f 6e 2c 20 76 61 72 h iteration, var
1febb 69 61 62 6c 65 20 69 4f 66 66 20 69 73 20 73 65 iable iOff is se
1febc 74 20 74 6f 20 74 68 65 20 62 79 74 65 20 6f 66 t to the byte of
1febd 66 73 65 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65 fset. ** of the
1febe 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 destination pag
1febf 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 4f e.. */. for(iO
1fec0 66 66 3d 69 45 6e 64 2d 28 69 36 34 29 6e 53 72 ff=iEnd-(i64)nSr
1fec1 63 50 67 73 7a 3b 20 72 63 3d 3d 53 51 4c 49 54 cPgsz; rc==SQLIT
1fec2 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 45 6e E_OK && iOff<iEn
1fec3 64 3b 20 69 4f 66 66 2b 3d 6e 44 65 73 74 50 67 d; iOff+=nDestPg
1fec4 73 7a 29 7b 0a 20 20 20 20 44 62 50 61 67 65 20 sz){. DbPage
1fec5 2a 70 44 65 73 74 50 67 20 3d 20 30 3b 0a 20 20 *pDestPg = 0;.
1fec6 20 20 50 67 6e 6f 20 69 44 65 73 74 20 3d 20 28 Pgno iDest = (
1fec7 50 67 6e 6f 29 28 69 4f 66 66 2f 6e 44 65 73 74 Pgno)(iOff/nDest
1fec8 50 67 73 7a 29 2b 31 3b 0a 20 20 20 20 69 66 28 Pgsz)+1;. if(
1fec9 20 69 44 65 73 74 3d 3d 50 45 4e 44 49 4e 47 5f iDest==PENDING_
1feca 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 BYTE_PAGE(p->pDe
1fecb 73 74 2d 3e 70 42 74 29 20 29 20 63 6f 6e 74 69 st->pBt) ) conti
1fecc 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 53 51 4c nue;. if( SQL
1fecd 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 ITE_OK==(rc = sq
1fece 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 44 lite3PagerGet(pD
1fecf 65 73 74 50 61 67 65 72 2c 20 69 44 65 73 74 2c estPager, iDest,
1fed0 20 26 70 44 65 73 74 50 67 29 29 0a 20 20 20 20 &pDestPg)).
1fed1 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 && SQLITE_OK==(
1fed2 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1fed3 72 57 72 69 74 65 28 70 44 65 73 74 50 67 29 29 rWrite(pDestPg))
1fed4 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63 6f . ){. co
1fed5 6e 73 74 20 75 38 20 2a 7a 49 6e 20 3d 20 26 7a nst u8 *zIn = &z
1fed6 53 72 63 44 61 74 61 5b 69 4f 66 66 25 6e 53 72 SrcData[iOff%nSr
1fed7 63 50 67 73 7a 5d 3b 0a 20 20 20 20 20 20 75 38 cPgsz];. u8
1fed8 20 2a 7a 44 65 73 74 44 61 74 61 20 3d 20 73 71 *zDestData = sq
1fed9 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
1feda 61 28 70 44 65 73 74 50 67 29 3b 0a 20 20 20 20 a(pDestPg);.
1fedb 20 20 75 38 20 2a 7a 4f 75 74 20 3d 20 26 7a 44 u8 *zOut = &zD
1fedc 65 73 74 44 61 74 61 5b 69 4f 66 66 25 6e 44 65 estData[iOff%nDe
1fedd 73 74 50 67 73 7a 5d 3b 0a 0a 20 20 20 20 20 20 stPgsz];..
1fede 2f 2a 20 43 6f 70 79 20 74 68 65 20 64 61 74 61 /* Copy the data
1fedf 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 from the source
1fee0 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 64 page into the d
1fee1 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e estination page.
1fee2 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20 63 . ** Then c
1fee3 6c 65 61 72 20 74 68 65 20 42 74 72 65 65 20 6c lear the Btree l
1fee4 61 79 65 72 20 4d 65 6d 50 61 67 65 2e 69 73 49 ayer MemPage.isI
1fee5 6e 69 74 20 66 6c 61 67 2e 20 42 6f 74 68 20 74 nit flag. Both t
1fee6 68 69 73 20 6d 6f 64 75 6c 65 0a 20 20 20 20 20 his module.
1fee7 20 2a 2a 20 61 6e 64 20 74 68 65 20 70 61 67 65 ** and the page
1fee8 72 20 63 6f 64 65 20 75 73 65 20 74 68 69 73 20 r code use this
1fee9 74 72 69 63 6b 20 28 63 6c 65 61 72 69 6e 67 20 trick (clearing
1feea 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 the first byte.
1feeb 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 ** of the p
1feec 61 67 65 20 27 65 78 74 72 61 27 20 73 70 61 63 age 'extra' spac
1feed 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 e to invalidate
1feee 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72 73 the Btree layers
1feef 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 64 . ** cached
1fef0 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 70 61 parse of the pa
1fef1 67 65 29 2e 20 4d 65 6d 50 61 67 65 2e 69 73 49 ge). MemPage.isI
1fef2 6e 69 74 20 69 73 20 6d 61 72 6b 65 64 20 0a 20 nit is marked .
1fef3 20 20 20 20 20 2a 2a 20 22 4d 55 53 54 20 42 45 ** "MUST BE
1fef4 20 46 49 52 53 54 22 20 66 6f 72 20 74 68 69 73 FIRST" for this
1fef5 20 70 75 72 70 6f 73 65 2e 0a 20 20 20 20 20 20 purpose..
1fef6 2a 2f 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 */. memcpy(
1fef7 7a 4f 75 74 2c 20 7a 49 6e 2c 20 6e 43 6f 70 79 zOut, zIn, nCopy
1fef8 29 3b 0a 20 20 20 20 20 20 28 28 75 38 20 2a 29 );. ((u8 *)
1fef9 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 sqlite3PagerGetE
1fefa 78 74 72 61 28 70 44 65 73 74 50 67 29 29 5b 30 xtra(pDestPg))[0
1fefb 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 ] = 0;. }.
1fefc 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
1fefd 65 66 28 70 44 65 73 74 50 67 29 3b 0a 20 20 7d ef(pDestPg);. }
1fefe 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
1feff 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 6c 65 ../*.** If pFile
1ff00 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6c 61 is currently la
1ff01 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65 20 rger than iSize
1ff02 62 79 74 65 73 2c 20 74 68 65 6e 20 74 72 75 6e bytes, then trun
1ff03 63 61 74 65 20 69 74 20 74 6f 0a 2a 2a 20 65 78 cate it to.** ex
1ff04 61 63 74 6c 79 20 69 53 69 7a 65 20 62 79 74 65 actly iSize byte
1ff05 73 2e 20 49 66 20 70 46 69 6c 65 20 69 73 20 6e s. If pFile is n
1ff06 6f 74 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 ot larger than i
1ff07 53 69 7a 65 20 62 79 74 65 73 2c 20 74 68 65 6e Size bytes, then
1ff08 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f .** this functio
1ff09 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a n is a no-op..**
1ff0a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
1ff0b 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 E_OK if everythi
1ff0c 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c ng is successful
1ff0d 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 , or an SQLite e
1ff0e 72 72 6f 72 20 0a 2a 2a 20 63 6f 64 65 20 69 66 rror .** code if
1ff0f 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
1ff10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1ff11 62 61 63 6b 75 70 54 72 75 6e 63 61 74 65 46 69 backupTruncateFi
1ff12 6c 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 le(sqlite3_file
1ff13 2a 70 46 69 6c 65 2c 20 69 36 34 20 69 53 69 7a *pFile, i64 iSiz
1ff14 65 29 7b 0a 20 20 69 36 34 20 69 43 75 72 72 65 e){. i64 iCurre
1ff15 6e 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 nt;. int rc = s
1ff16 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 qlite3OsFileSize
1ff17 28 70 46 69 6c 65 2c 20 26 69 43 75 72 72 65 6e (pFile, &iCurren
1ff18 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 t);. if( rc==SQ
1ff19 4c 49 54 45 5f 4f 4b 20 26 26 20 69 43 75 72 72 LITE_OK && iCurr
1ff1a 65 6e 74 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 ent>iSize ){.
1ff1b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
1ff1c 72 75 6e 63 61 74 65 28 70 46 69 6c 65 2c 20 69 runcate(pFile, i
1ff1d 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Size);. }. ret
1ff1e 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1ff1f 20 52 65 67 69 73 74 65 72 20 74 68 69 73 20 62 Register this b
1ff20 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 77 69 74 ackup object wit
1ff21 68 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 h the associated
1ff22 20 73 6f 75 72 63 65 20 70 61 67 65 72 20 66 6f source pager fo
1ff23 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 73 20 77 r.** callbacks w
1ff24 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 63 68 hen pages are ch
1ff25 61 6e 67 65 64 20 6f 72 20 74 68 65 20 63 61 63 anged or the cac
1ff26 68 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a he invalidated..
1ff27 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 */.static void a
1ff28 74 74 61 63 68 42 61 63 6b 75 70 4f 62 6a 65 63 ttachBackupObjec
1ff29 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 t(sqlite3_backup
1ff2a 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f *p){. sqlite3_
1ff2b 62 61 63 6b 75 70 20 2a 2a 70 70 3b 0a 20 20 61 backup **pp;. a
1ff2c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
1ff2d 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 2d reeHoldsMutex(p-
1ff2e 3e 70 53 72 63 29 20 29 3b 0a 20 20 70 70 20 3d >pSrc) );. pp =
1ff2f 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 sqlite3PagerBac
1ff30 6b 75 70 50 74 72 28 73 71 6c 69 74 65 33 42 74 kupPtr(sqlite3Bt
1ff31 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53 72 63 reePager(p->pSrc
1ff32 29 29 3b 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d ));. p->pNext =
1ff33 20 2a 70 70 3b 0a 20 20 2a 70 70 20 3d 20 70 3b *pp;. *pp = p;
1ff34 0a 20 20 70 2d 3e 69 73 41 74 74 61 63 68 65 64 . p->isAttached
1ff35 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 = 1;.}../*.** C
1ff36 6f 70 79 20 6e 50 61 67 65 20 70 61 67 65 73 20 opy nPage pages
1ff37 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 from the source
1ff38 62 2d 74 72 65 65 20 74 6f 20 74 68 65 20 64 65 b-tree to the de
1ff39 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 stination..*/.SQ
1ff3a 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1ff3b 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
1ff3c 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
1ff3d 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a *p, int nPage){.
1ff3e 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 int rc;. int
1ff3f 64 65 73 74 4d 6f 64 65 3b 20 20 20 20 20 20 20 destMode;
1ff40 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 6a /* Destination j
1ff41 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 2a 2f 0a 20 ournal mode */.
1ff42 20 69 6e 74 20 70 67 73 7a 53 72 63 20 3d 20 30 int pgszSrc = 0
1ff43 3b 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 ; /* Source p
1ff44 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e age size */. in
1ff45 74 20 70 67 73 7a 44 65 73 74 20 3d 20 30 3b 20 t pgszDest = 0;
1ff46 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e /* Destination
1ff47 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 page size */..
1ff48 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1ff49 6e 74 65 72 28 70 2d 3e 70 53 72 63 44 62 2d 3e nter(p->pSrcDb->
1ff4a 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 mutex);. sqlite
1ff4b 33 42 74 72 65 65 45 6e 74 65 72 28 70 2d 3e 70 3BtreeEnter(p->p
1ff4c 53 72 63 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 Src);. if( p->p
1ff4d 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 DestDb ){. sq
1ff4e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1ff4f 72 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 r(p->pDestDb->mu
1ff50 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 tex);. }.. rc
1ff51 3d 20 70 2d 3e 72 63 3b 0a 20 20 69 66 28 20 21 = p->rc;. if( !
1ff52 69 73 46 61 74 61 6c 45 72 72 6f 72 28 72 63 29 isFatalError(rc)
1ff53 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 20 ){. Pager *
1ff54 63 6f 6e 73 74 20 70 53 72 63 50 61 67 65 72 20 const pSrcPager
1ff55 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 = sqlite3BtreePa
1ff56 67 65 72 28 70 2d 3e 70 53 72 63 29 3b 20 20 20 ger(p->pSrc);
1ff57 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65 /* Source page
1ff58 72 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 20 2a r */. Pager *
1ff59 20 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65 const pDestPage
1ff5a 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 r = sqlite3Btree
1ff5b 50 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b Pager(p->pDest);
1ff5c 20 20 20 2f 2a 20 44 65 73 74 20 70 61 67 65 72 /* Dest pager
1ff5d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 */. int ii;
1ff5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff5f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 /* It
1ff60 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 erator variable
1ff61 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 72 63 50 */. int nSrcP
1ff62 61 67 65 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 age = -1;
1ff63 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
1ff64 65 20 6f 66 20 73 6f 75 72 63 65 20 64 62 20 69 e of source db i
1ff65 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 69 n pages */. i
1ff66 6e 74 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d nt bCloseTrans =
1ff67 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1ff68 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 72 63 /* True if src
1ff69 20 64 62 20 72 65 71 75 69 72 65 73 20 75 6e 6c db requires unl
1ff6a 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 2f ocking */.. /
1ff6b 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 * If the source
1ff6c 70 61 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 pager is current
1ff6d 6c 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 ly in a write-tr
1ff6e 61 6e 73 61 63 74 69 6f 6e 2c 20 72 65 74 75 72 ansaction, retur
1ff6f 6e 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f n. ** SQLITE_
1ff70 42 55 53 59 20 69 6d 6d 65 64 69 61 74 65 6c 79 BUSY immediately
1ff71 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1ff72 20 70 2d 3e 70 44 65 73 74 44 62 20 26 26 20 70 p->pDestDb && p
1ff73 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e 69 6e 54 ->pSrc->pBt->inT
1ff74 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e ransaction==TRAN
1ff75 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 S_WRITE ){.
1ff76 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
1ff77 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 Y;. }else{.
1ff78 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1ff79 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f OK;. }.. /
1ff7a 2a 20 4c 6f 63 6b 20 74 68 65 20 64 65 73 74 69 * Lock the desti
1ff7b 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2c nation database,
1ff7c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 6c 6f if it is not lo
1ff7d 63 6b 65 64 20 61 6c 72 65 61 64 79 2e 20 2a 2f cked already. */
1ff7e 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f . if( SQLITE_
1ff7f 4f 4b 3d 3d 72 63 20 26 26 20 70 2d 3e 62 44 65 OK==rc && p->bDe
1ff80 73 74 4c 6f 63 6b 65 64 3d 3d 30 0a 20 20 20 20 stLocked==0.
1ff81 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 && SQLITE_OK==(
1ff82 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1ff83 65 42 65 67 69 6e 54 72 61 6e 73 28 70 2d 3e 70 eBeginTrans(p->p
1ff84 44 65 73 74 2c 20 32 29 29 20 0a 20 20 20 20 29 Dest, 2)) . )
1ff85 7b 0a 20 20 20 20 20 20 70 2d 3e 62 44 65 73 74 {. p->bDest
1ff86 4c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 Locked = 1;.
1ff87 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 sqlite3BtreeGe
1ff88 74 4d 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 20 tMeta(p->pDest,
1ff89 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 BTREE_SCHEMA_VER
1ff8a 53 49 4f 4e 2c 20 26 70 2d 3e 69 44 65 73 74 53 SION, &p->iDestS
1ff8b 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 0a 20 chema);. }..
1ff8c 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 /* If there i
1ff8d 73 20 6e 6f 20 6f 70 65 6e 20 72 65 61 64 2d 74 s no open read-t
1ff8e 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 ransaction on th
1ff8f 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
1ff90 65 2c 20 6f 70 65 6e 0a 20 20 20 20 2a 2a 20 6f e, open. ** o
1ff91 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72 61 ne now. If a tra
1ff92 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e nsaction is open
1ff93 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20 69 74 ed here, then it
1ff94 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64 0a will be closed.
1ff95 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 68 ** before th
1ff96 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 74 is function exit
1ff97 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 s.. */. if
1ff98 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1ff99 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 74 72 && 0==sqlite3Btr
1ff9a 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 eeIsInReadTrans(
1ff9b 70 2d 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 20 p->pSrc) ){.
1ff9c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1ff9d 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 2d reeBeginTrans(p-
1ff9e 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 >pSrc, 0);.
1ff9f 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20 31 bCloseTrans = 1
1ffa0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
1ffa1 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 62 61 63 Do not allow bac
1ffa2 6b 75 70 20 69 66 20 74 68 65 20 64 65 73 74 69 kup if the desti
1ffa3 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
1ffa4 69 73 20 69 6e 20 57 41 4c 20 6d 6f 64 65 0a 20 is in WAL mode.
1ffa5 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 70 61 ** and the pa
1ffa6 67 65 20 73 69 7a 65 73 20 61 72 65 20 64 69 66 ge sizes are dif
1ffa7 66 65 72 65 6e 74 20 62 65 74 77 65 65 6e 20 73 ferent between s
1ffa8 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e ource and destin
1ffa9 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 70 67 73 ation */. pgs
1ffaa 7a 53 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 zSrc = sqlite3Bt
1ffab 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 reeGetPageSize(p
1ffac 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 70 67 73 ->pSrc);. pgs
1ffad 7a 44 65 73 74 20 3d 20 73 71 6c 69 74 65 33 42 zDest = sqlite3B
1ffae 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 treeGetPageSize(
1ffaf 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 20 20 64 p->pDest);. d
1ffb0 65 73 74 4d 6f 64 65 20 3d 20 73 71 6c 69 74 65 estMode = sqlite
1ffb1 33 50 61 67 65 72 47 65 74 4a 6f 75 72 6e 61 6c 3PagerGetJournal
1ffb2 4d 6f 64 65 28 73 71 6c 69 74 65 33 42 74 72 65 Mode(sqlite3Btre
1ffb3 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 ePager(p->pDest)
1ffb4 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 );. if( SQLIT
1ffb5 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 64 65 73 74 E_OK==rc && dest
1ffb6 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
1ffb7 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 26 26 20 70 NALMODE_WAL && p
1ffb8 67 73 7a 53 72 63 21 3d 70 67 73 7a 44 65 73 74 gszSrc!=pgszDest
1ffb9 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
1ffba 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a QLITE_READONLY;.
1ffbb 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
1ffbc 4e 6f 77 20 74 68 61 74 20 74 68 65 72 65 20 69 Now that there i
1ffbd 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e s a read-lock on
1ffbe 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
1ffbf 62 61 73 65 2c 20 71 75 65 72 79 20 74 68 65 0a base, query the.
1ffc0 20 20 20 20 2a 2a 20 73 6f 75 72 63 65 20 70 61 ** source pa
1ffc1 67 65 72 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 ger for the numb
1ffc2 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
1ffc3 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 he database..
1ffc4 20 2a 2f 0a 20 20 20 20 6e 53 72 63 50 61 67 65 */. nSrcPage
1ffc5 20 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33 42 = (int)sqlite3B
1ffc6 74 72 65 65 4c 61 73 74 50 61 67 65 28 70 2d 3e treeLastPage(p->
1ffc7 70 53 72 63 29 3b 0a 20 20 20 20 61 73 73 65 72 pSrc);. asser
1ffc8 74 28 20 6e 53 72 63 50 61 67 65 3e 3d 30 20 29 t( nSrcPage>=0 )
1ffc9 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 ;. for(ii=0;
1ffca 28 6e 50 61 67 65 3c 30 20 7c 7c 20 69 69 3c 6e (nPage<0 || ii<n
1ffcb 50 61 67 65 29 20 26 26 20 70 2d 3e 69 4e 65 78 Page) && p->iNex
1ffcc 74 3c 3d 28 50 67 6e 6f 29 6e 53 72 63 50 61 67 t<=(Pgno)nSrcPag
1ffcd 65 20 26 26 20 21 72 63 3b 20 69 69 2b 2b 29 7b e && !rc; ii++){
1ffce 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e . const Pgn
1ffcf 6f 20 69 53 72 63 50 67 20 3d 20 70 2d 3e 69 4e o iSrcPg = p->iN
1ffd0 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ext;
1ffd1 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 /* Source p
1ffd2 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 age number */.
1ffd3 20 20 20 20 69 66 28 20 69 53 72 63 50 67 21 3d if( iSrcPg!=
1ffd4 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
1ffd5 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 E(p->pSrc->pBt)
1ffd6 29 7b 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 ){. DbPag
1ffd7 65 20 2a 70 53 72 63 50 67 3b 20 20 20 20 20 20 e *pSrcPg;
1ffd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ffd9 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 /* Source
1ffda 20 70 61 67 65 20 6f 62 6a 65 63 74 20 2a 2f 0a page object */.
1ffdb 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1ffdc 69 74 65 33 50 61 67 65 72 47 65 74 28 70 53 72 ite3PagerGet(pSr
1ffdd 63 50 61 67 65 72 2c 20 69 53 72 63 50 67 2c 20 cPager, iSrcPg,
1ffde 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 &pSrcPg);.
1ffdf 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1ffe0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
1ffe1 20 72 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65 50 rc = backupOneP
1ffe2 61 67 65 28 70 2c 20 69 53 72 63 50 67 2c 20 73 age(p, iSrcPg, s
1ffe3 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
1ffe4 74 61 28 70 53 72 63 50 67 29 29 3b 0a 20 20 20 ta(pSrcPg));.
1ffe5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 sqlite3Pa
1ffe6 67 65 72 55 6e 72 65 66 28 70 53 72 63 50 67 29 gerUnref(pSrcPg)
1ffe7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1ffe8 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 69 4e 65 }. p->iNe
1ffe9 78 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 xt++;. }.
1ffea 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1ffeb 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 50 K ){. p->nP
1ffec 61 67 65 63 6f 75 6e 74 20 3d 20 6e 53 72 63 50 agecount = nSrcP
1ffed 61 67 65 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 age;. p->nR
1ffee 65 6d 61 69 6e 69 6e 67 20 3d 20 6e 53 72 63 50 emaining = nSrcP
1ffef 61 67 65 2b 31 2d 70 2d 3e 69 4e 65 78 74 3b 0a age+1-p->iNext;.
1fff0 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 4e 65 if( p->iNe
1fff1 78 74 3e 28 50 67 6e 6f 29 6e 53 72 63 50 61 67 xt>(Pgno)nSrcPag
1fff2 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 e ){. rc
1fff3 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 = SQLITE_DONE;.
1fff4 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 }else if( !
1fff5 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 29 7b p->isAttached ){
1fff6 0a 20 20 20 20 20 20 20 20 61 74 74 61 63 68 42 . attachB
1fff7 61 63 6b 75 70 4f 62 6a 65 63 74 28 70 29 3b 0a ackupObject(p);.
1fff8 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1fff9 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 . /* Update t
1fffa 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f he schema versio
1fffb 6e 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 64 n field in the d
1fffc 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
1fffd 61 73 65 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a ase. This. **
1fffe 20 69 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 is to make sure
1ffff 20 74 68 61 74 20 74 68 65 20 73 63 68 65 6d 61 that the schema
20000 2d 76 65 72 73 69 6f 6e 20 72 65 61 6c 6c 79 20 -version really
20001 64 6f 65 73 20 63 68 61 6e 67 65 20 69 6e 0a 20 does change in.
20002 20 20 20 2a 2a 20 74 68 65 20 63 61 73 65 20 77 ** the case w
20003 68 65 72 65 20 74 68 65 20 73 6f 75 72 63 65 20 here the source
20004 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 and destination
20005 64 61 74 61 62 61 73 65 73 20 68 61 76 65 20 74 databases have t
20006 68 65 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 73 he. ** same s
20007 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 0a 20 chema version..
20008 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 */. if( rc
20009 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b ==SQLITE_DONE ){
2000a 0a 20 20 20 20 20 20 69 66 28 20 6e 53 72 63 50 . if( nSrcP
2000b 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 age==0 ){.
2000c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
2000d 72 65 65 4e 65 77 44 62 28 70 2d 3e 70 44 65 73 reeNewDb(p->pDes
2000e 74 29 3b 0a 20 20 20 20 20 20 20 20 6e 53 72 63 t);. nSrc
2000f 50 61 67 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 Page = 1;.
20010 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
20011 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d SQLITE_OK || rc=
20012 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a =SQLITE_DONE ){.
20013 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
20014 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d ite3BtreeUpdateM
20015 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 31 2c 70 eta(p->pDest,1,p
20016 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 2b 31 29 ->iDestSchema+1)
20017 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
20018 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
20019 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 K ){. if(
2001a 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 p->pDestDb ){.
2001b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
2001c 52 65 73 65 74 41 6c 6c 53 63 68 65 6d 61 73 4f ResetAllSchemasO
2001d 66 43 6f 6e 6e 65 63 74 69 6f 6e 28 70 2d 3e 70 fConnection(p->p
2001e 44 65 73 74 44 62 29 3b 0a 20 20 20 20 20 20 20 DestDb);.
2001f 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 }. if( d
20020 65 73 74 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a estMode==PAGER_J
20021 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 29 OURNALMODE_WAL )
20022 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
20023 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
20024 56 65 72 73 69 6f 6e 28 70 2d 3e 70 44 65 73 74 Version(p->pDest
20025 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a , 2);. }.
20026 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
20027 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
20028 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e ){. int n
20029 44 65 73 74 54 72 75 6e 63 61 74 65 3b 0a 20 20 DestTruncate;.
2002a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6e 44 65 /* Set nDe
2002b 73 74 54 72 75 6e 63 61 74 65 20 74 6f 20 74 68 stTruncate to th
2002c 65 20 66 69 6e 61 6c 20 6e 75 6d 62 65 72 20 6f e final number o
2002d 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
2002e 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20 estination.
2002f 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 ** database.
20030 54 68 65 20 63 6f 6d 70 6c 69 63 61 74 69 6f 6e The complication
20031 20 68 65 72 65 20 69 73 20 74 68 61 74 20 74 68 here is that th
20032 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 e destination pa
20033 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 69 ge. ** si
20034 7a 65 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 ze may be differ
20035 65 6e 74 20 74 6f 20 74 68 65 20 73 6f 75 72 63 ent to the sourc
20036 65 20 70 61 67 65 20 73 69 7a 65 2e 20 0a 20 20 e page size. .
20037 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
20038 20 2a 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 ** If the sourc
20039 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 73 e page size is s
2003a 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 maller than the
2003b 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 destination page
2003c 20 73 69 7a 65 2c 20 0a 20 20 20 20 20 20 20 20 size, .
2003d 2a 2a 20 72 6f 75 6e 64 20 75 70 2e 20 49 6e 20 ** round up. In
2003e 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 this case the ca
2003f 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 54 ll to sqlite3OsT
20040 72 75 6e 63 61 74 65 28 29 20 62 65 6c 6f 77 20 runcate() below
20041 77 69 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 will. **
20042 66 69 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 fix the size of
20043 74 68 65 20 66 69 6c 65 2e 20 48 6f 77 65 76 65 the file. Howeve
20044 72 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e r it is importan
20045 74 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 20 20 t to call.
20046 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 ** sqlite3Page
20047 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 rTruncateImage()
20048 20 68 65 72 65 20 73 6f 20 74 68 61 74 20 61 6e here so that an
20049 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 0a y pages in the .
2004a 20 20 20 20 20 20 20 20 2a 2a 20 64 65 73 74 69 ** desti
2004b 6e 61 74 69 6f 6e 20 66 69 6c 65 20 74 68 61 74 nation file that
2004c 20 6c 69 65 20 62 65 79 6f 6e 64 20 74 68 65 20 lie beyond the
2004d 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 70 61 nDestTruncate pa
2004e 67 65 20 6d 61 72 6b 20 61 72 65 0a 20 20 20 20 ge mark are.
2004f 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 ** journalle
20050 64 20 62 79 20 50 61 67 65 72 43 6f 6d 6d 69 74 d by PagerCommit
20051 50 68 61 73 65 4f 6e 65 28 29 20 62 65 66 6f 72 PhaseOne() befor
20052 65 20 74 68 65 79 20 61 72 65 20 64 65 73 74 72 e they are destr
20053 6f 79 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 oyed. **
20054 62 79 20 74 68 65 20 66 69 6c 65 20 74 72 75 6e by the file trun
20055 63 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 cation..
20056 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 */. asser
20057 74 28 20 70 67 73 7a 53 72 63 3d 3d 73 71 6c 69 t( pgszSrc==sqli
20058 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 te3BtreeGetPageS
20059 69 7a 65 28 70 2d 3e 70 53 72 63 29 20 29 3b 0a ize(p->pSrc) );.
2005a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
2005b 70 67 73 7a 44 65 73 74 3d 3d 73 71 6c 69 74 65 pgszDest==sqlite
2005c 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 3BtreeGetPageSiz
2005d 65 28 70 2d 3e 70 44 65 73 74 29 20 29 3b 0a 20 e(p->pDest) );.
2005e 20 20 20 20 20 20 20 69 66 28 20 70 67 73 7a 53 if( pgszS
2005f 72 63 3c 70 67 73 7a 44 65 73 74 20 29 7b 0a 20 rc<pgszDest ){.
20060 20 20 20 20 20 20 20 20 20 69 6e 74 20 72 61 74 int rat
20061 69 6f 20 3d 20 70 67 73 7a 44 65 73 74 2f 70 67 io = pgszDest/pg
20062 73 7a 53 72 63 3b 0a 20 20 20 20 20 20 20 20 20 szSrc;.
20063 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 3d nDestTruncate =
20064 20 28 6e 53 72 63 50 61 67 65 2b 72 61 74 69 6f (nSrcPage+ratio
20065 2d 31 29 2f 72 61 74 69 6f 3b 0a 20 20 20 20 20 -1)/ratio;.
20066 20 20 20 20 20 69 66 28 20 6e 44 65 73 74 54 72 if( nDestTr
20067 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29 50 45 4e uncate==(int)PEN
20068 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
20069 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20 29 7b ->pDest->pBt) ){
2006a 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65 . nDe
2006b 73 74 54 72 75 6e 63 61 74 65 2d 2d 3b 0a 20 20 stTruncate--;.
2006c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2006d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
2006e 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 nDestTruncate
2006f 20 3d 20 6e 53 72 63 50 61 67 65 20 2a 20 28 70 = nSrcPage * (p
20070 67 73 7a 53 72 63 2f 70 67 73 7a 44 65 73 74 29 gszSrc/pgszDest)
20071 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
20072 20 20 20 20 61 73 73 65 72 74 28 20 6e 44 65 73 assert( nDes
20073 74 54 72 75 6e 63 61 74 65 3e 30 20 29 3b 0a 20 tTruncate>0 );.
20074 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 sqlite3Pa
20075 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 gerTruncateImage
20076 28 70 44 65 73 74 50 61 67 65 72 2c 20 6e 44 65 (pDestPager, nDe
20077 73 74 54 72 75 6e 63 61 74 65 29 3b 0a 0a 20 20 stTruncate);..
20078 20 20 20 20 20 20 69 66 28 20 70 67 73 7a 53 72 if( pgszSr
20079 63 3c 70 67 73 7a 44 65 73 74 20 29 7b 0a 20 20 c<pgszDest ){.
2007a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
2007b 65 20 73 6f 75 72 63 65 20 70 61 67 65 2d 73 69 e source page-si
2007c 7a 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 ze is smaller th
2007d 61 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 an the destinati
2007e 6f 6e 20 70 61 67 65 2d 73 69 7a 65 2c 0a 20 20 on page-size,.
2007f 20 20 20 20 20 20 20 20 2a 2a 20 74 77 6f 20 65 ** two e
20080 78 74 72 61 20 74 68 69 6e 67 73 20 6d 61 79 20 xtra things may
20081 6e 65 65 64 20 74 6f 20 68 61 70 70 65 6e 3a 0a need to happen:.
20082 20 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 **.
20083 20 20 20 20 20 20 20 2a 2a 20 20 20 2a 20 54 68 ** * Th
20084 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 61 e destination ma
20085 79 20 6e 65 65 64 20 74 6f 20 62 65 20 74 72 75 y need to be tru
20086 6e 63 61 74 65 64 2c 20 61 6e 64 0a 20 20 20 20 ncated, and.
20087 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
20088 20 20 20 2a 2a 20 20 20 2a 20 44 61 74 61 20 73 ** * Data s
20089 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 70 61 67 tored on the pag
2008a 65 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 es immediately f
2008b 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 0a 20 20 ollowing the .
2008c 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 70 ** p
2008d 65 6e 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65 ending-byte page
2008e 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 in the source d
2008f 61 74 61 62 61 73 65 20 6d 61 79 20 6e 65 65 64 atabase may need
20090 20 74 6f 20 62 65 0a 20 20 20 20 20 20 20 20 20 to be.
20091 20 2a 2a 20 20 20 20 20 63 6f 70 69 65 64 20 69 ** copied i
20092 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 nto the destinat
20093 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 0a 20 20 ion database..
20094 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
20095 20 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20 69 const i64 i
20096 53 69 7a 65 20 3d 20 28 69 36 34 29 70 67 73 7a Size = (i64)pgsz
20097 53 72 63 20 2a 20 28 69 36 34 29 6e 53 72 63 50 Src * (i64)nSrcP
20098 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 73 age;. s
20099 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 20 63 6f qlite3_file * co
2009a 6e 73 74 20 70 46 69 6c 65 20 3d 20 73 71 6c 69 nst pFile = sqli
2009b 74 65 33 50 61 67 65 72 46 69 6c 65 28 70 44 65 te3PagerFile(pDe
2009c 73 74 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 stPager);.
2009d 20 20 20 20 69 36 34 20 69 4f 66 66 3b 0a 20 20 i64 iOff;.
2009e 20 20 20 20 20 20 20 20 69 36 34 20 69 45 6e 64 i64 iEnd
2009f 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 ;.. ass
200a0 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
200a1 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
200a2 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3d 3d 30 nDestTruncate==0
200a3 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
200a4 7c 7c 20 28 69 36 34 29 6e 44 65 73 74 54 72 75 || (i64)nDestTru
200a5 6e 63 61 74 65 2a 28 69 36 34 29 70 67 73 7a 44 ncate*(i64)pgszD
200a6 65 73 74 20 3e 3d 20 69 53 69 7a 65 20 7c 7c 20 est >= iSize ||
200a7 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 (.
200a8 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3d nDestTruncate=
200a9 3d 28 69 6e 74 29 28 50 45 4e 44 49 4e 47 5f 42 =(int)(PENDING_B
200aa 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 73 YTE_PAGE(p->pDes
200ab 74 2d 3e 70 42 74 29 2d 31 29 0a 20 20 20 20 20 t->pBt)-1).
200ac 20 20 20 20 20 20 20 20 26 26 20 69 53 69 7a 65 && iSize
200ad 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 >=PENDING_BYTE &
200ae 26 20 69 53 69 7a 65 3c 3d 50 45 4e 44 49 4e 47 & iSize<=PENDING
200af 5f 42 59 54 45 2b 70 67 73 7a 44 65 73 74 0a 20 _BYTE+pgszDest.
200b0 20 20 20 20 20 20 20 20 20 29 29 3b 0a 0a 20 20 ));..
200b1 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
200b2 63 61 6c 6c 20 65 6e 73 75 72 65 73 20 74 68 61 call ensures tha
200b3 74 20 61 6c 6c 20 64 61 74 61 20 72 65 71 75 69 t all data requi
200b4 72 65 64 20 74 6f 20 72 65 63 72 65 61 74 65 20 red to recreate
200b5 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 20 20 20 the original.
200b6 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 ** databa
200b7 73 65 20 68 61 73 20 62 65 65 6e 20 73 74 6f 72 se has been stor
200b8 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ed in the journa
200b9 6c 20 66 6f 72 20 70 44 65 73 74 50 61 67 65 72 l for pDestPager
200ba 20 61 6e 64 20 74 68 65 0a 20 20 20 20 20 20 20 and the.
200bb 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 73 79 ** journal sy
200bc 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 53 6f nced to disk. So
200bd 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 77 at this point w
200be 65 20 6d 61 79 20 73 61 66 65 6c 79 20 6d 6f 64 e may safely mod
200bf 69 66 79 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ify. **
200c0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
200c1 6c 65 20 69 6e 20 61 6e 79 20 77 61 79 2c 20 6b le in any way, k
200c2 6e 6f 77 69 6e 67 20 74 68 61 74 20 69 66 20 61 nowing that if a
200c3 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 0a 20 power failure.
200c4 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 63 63 75 ** occu
200c5 72 73 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c rs, the original
200c6 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 database will b
200c7 65 20 72 65 63 6f 6e 73 74 72 75 63 74 65 64 20 e reconstructed
200c8 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 20 20 from the .
200c9 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 ** journal f
200ca 69 6c 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 ile. */.
200cb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
200cc 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f agerCommitPhaseO
200cd 6e 65 28 70 44 65 73 74 50 61 67 65 72 2c 20 30 ne(pDestPager, 0
200ce 2c 20 31 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 , 1);..
200cf 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 65 78 /* Write the ex
200d0 74 72 61 20 70 61 67 65 73 20 61 6e 64 20 74 72 tra pages and tr
200d1 75 6e 63 61 74 65 20 74 68 65 20 64 61 74 61 62 uncate the datab
200d2 61 73 65 20 66 69 6c 65 20 61 73 20 72 65 71 75 ase file as requ
200d3 69 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 ired */.
200d4 20 20 69 45 6e 64 20 3d 20 4d 49 4e 28 50 45 4e iEnd = MIN(PEN
200d5 44 49 4e 47 5f 42 59 54 45 20 2b 20 70 67 73 7a DING_BYTE + pgsz
200d6 44 65 73 74 2c 20 69 53 69 7a 65 29 3b 0a 20 20 Dest, iSize);.
200d7 20 20 20 20 20 20 20 20 66 6f 72 28 0a 20 20 20 for(.
200d8 20 20 20 20 20 20 20 20 20 69 4f 66 66 3d 50 45 iOff=PE
200d9 4e 44 49 4e 47 5f 42 59 54 45 2b 70 67 73 7a 53 NDING_BYTE+pgszS
200da 72 63 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 rc; .
200db 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
200dc 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 0a 20 20 & iOff<iEnd; .
200dd 20 20 20 20 20 20 20 20 20 20 69 4f 66 66 2b 3d iOff+=
200de 70 67 73 7a 53 72 63 0a 20 20 20 20 20 20 20 20 pgszSrc.
200df 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ){.
200e0 20 50 67 48 64 72 20 2a 70 53 72 63 50 67 20 3d PgHdr *pSrcPg =
200e1 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 0;.
200e2 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 72 63 50 const Pgno iSrcP
200e3 67 20 3d 20 28 50 67 6e 6f 29 28 28 69 4f 66 66 g = (Pgno)((iOff
200e4 2f 70 67 73 7a 53 72 63 29 2b 31 29 3b 0a 20 20 /pgszSrc)+1);.
200e5 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
200e6 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
200e7 53 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 67 SrcPager, iSrcPg
200e8 2c 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20 , &pSrcPg);.
200e9 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
200ea 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
200eb 20 20 20 20 20 20 20 20 20 20 20 75 38 20 2a 7a u8 *z
200ec 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 Data = sqlite3Pa
200ed 67 65 72 47 65 74 44 61 74 61 28 70 53 72 63 50 gerGetData(pSrcP
200ee 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 g);.
200ef 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
200f0 57 72 69 74 65 28 70 46 69 6c 65 2c 20 7a 44 61 Write(pFile, zDa
200f1 74 61 2c 20 70 67 73 7a 53 72 63 2c 20 69 4f 66 ta, pgszSrc, iOf
200f2 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 f);.
200f3 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 }. sq
200f4 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
200f5 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 pSrcPg);.
200f6 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 }. i
200f7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
200f8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
200f9 72 63 20 3d 20 62 61 63 6b 75 70 54 72 75 6e 63 rc = backupTrunc
200fa 61 74 65 46 69 6c 65 28 70 46 69 6c 65 2c 20 69 ateFile(pFile, i
200fb 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Size);.
200fc 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a }.. /*
200fd 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 Sync the databa
200fe 73 65 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e se file to disk.
200ff 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 */. if
20100 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
20101 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 ){. r
20102 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
20103 53 79 6e 63 28 70 44 65 73 74 50 61 67 65 72 29 Sync(pDestPager)
20104 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
20105 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
20106 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
20107 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 te3PagerCommitPh
20108 61 73 65 4f 6e 65 28 70 44 65 73 74 50 61 67 65 aseOne(pDestPage
20109 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 r, 0, 0);.
2010a 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 20 20 20 }. .
2010b 20 2f 2a 20 46 69 6e 69 73 68 20 63 6f 6d 6d 69 /* Finish commi
2010c 74 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 tting the transa
2010d 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 64 65 73 ction to the des
2010e 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
2010f 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 e. */. if
20110 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0a ( SQLITE_OK==rc.
20111 20 20 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 && SQLI
20112 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c TE_OK==(rc = sql
20113 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
20114 68 61 73 65 54 77 6f 28 70 2d 3e 70 44 65 73 74 haseTwo(p->pDest
20115 2c 20 30 29 29 0a 20 20 20 20 20 20 20 20 29 7b , 0)). ){
20116 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
20117 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 SQLITE_DONE;.
20118 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
20119 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 }. . /* I
2011a 66 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 69 73 f bCloseTrans is
2011b 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 69 73 true, then this
2011c 20 66 75 6e 63 74 69 6f 6e 20 6f 70 65 6e 65 64 function opened
2011d 20 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 a read transact
2011e 69 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 ion. ** on th
2011f 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
20120 65 2e 20 43 6c 6f 73 65 20 74 68 65 20 72 65 61 e. Close the rea
20121 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 65 d transaction he
20122 72 65 2e 20 54 68 65 72 65 20 69 73 0a 20 20 20 re. There is.
20123 20 2a 2a 20 6e 6f 20 6e 65 65 64 20 74 6f 20 63 ** no need to c
20124 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 heck the return
20125 76 61 6c 75 65 73 20 6f 66 20 74 68 65 20 62 74 values of the bt
20126 72 65 65 20 6d 65 74 68 6f 64 73 20 68 65 72 65 ree methods here
20127 2c 20 61 73 0a 20 20 20 20 2a 2a 20 22 63 6f 6d , as. ** "com
20128 6d 69 74 74 69 6e 67 22 20 61 20 72 65 61 64 2d mitting" a read-
20129 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e only transaction
2012a 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 cannot fail..
2012b 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 43 6c */. if( bCl
2012c 6f 73 65 54 72 61 6e 73 20 29 7b 0a 20 20 20 20 oseTrans ){.
2012d 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 TESTONLY( int
2012e 72 63 32 20 29 3b 0a 20 20 20 20 20 20 54 45 53 rc2 );. TES
2012f 54 4f 4e 4c 59 28 20 72 63 32 20 20 3d 20 29 20 TONLY( rc2 = )
20130 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
20131 69 74 50 68 61 73 65 4f 6e 65 28 70 2d 3e 70 53 itPhaseOne(p->pS
20132 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 54 45 rc, 0);. TE
20133 53 54 4f 4e 4c 59 28 20 72 63 32 20 7c 3d 20 29 STONLY( rc2 |= )
20134 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
20135 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 mitPhaseTwo(p->p
20136 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 Src, 0);. a
20137 73 73 65 72 74 28 20 72 63 32 3d 3d 53 51 4c 49 ssert( rc2==SQLI
20138 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a 20 TE_OK );. }.
20139 20 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 . if( rc==SQ
2013a 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d LITE_IOERR_NOMEM
2013b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
2013c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
2013d 20 7d 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 }. p->rc = r
2013e 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e c;. }. if( p->
2013f 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 pDestDb ){. s
20140 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
20141 76 65 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d ve(p->pDestDb->m
20142 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c utex);. }. sql
20143 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
20144 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c 69 74 ->pSrc);. sqlit
20145 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
20146 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 ->pSrcDb->mutex)
20147 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
20148 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 ../*.** Release
20149 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61 73 all resources as
2014a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6e sociated with an
2014b 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2a sqlite3_backup*
2014c 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 handle..*/.SQLI
2014d 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
2014e 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 e3_backup_finish
2014f 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
20150 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 *p){. sqlite3_b
20151 61 63 6b 75 70 20 2a 2a 70 70 3b 20 20 20 20 20 ackup **pp;
20152 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
20153 74 72 20 74 6f 20 68 65 61 64 20 6f 66 20 70 61 tr to head of pa
20154 67 65 72 73 20 62 61 63 6b 75 70 20 6c 69 73 74 gers backup list
20155 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 */. sqlite3 *p
20156 53 72 63 44 62 3b 20 20 20 20 20 20 20 20 20 20 SrcDb;
20157 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f /* So
20158 75 72 63 65 20 64 61 74 61 62 61 73 65 20 63 6f urce database co
20159 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e nnection */. in
2015a 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
2015b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2015c 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 /* Value to r
2015d 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 45 eturn */.. /* E
2015e 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 65 73 nter the mutexes
2015f 20 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 */. if( p==0 )
20160 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
20161 4b 3b 0a 20 20 70 53 72 63 44 62 20 3d 20 70 2d K;. pSrcDb = p-
20162 3e 70 53 72 63 44 62 3b 0a 20 20 73 71 6c 69 74 >pSrcDb;. sqlit
20163 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 e3_mutex_enter(p
20164 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 SrcDb->mutex);.
20165 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
20166 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 er(p->pSrc);. i
20167 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b f( p->pDestDb ){
20168 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
20169 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73 ex_enter(p->pDes
2016a 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d tDb->mutex);. }
2016b 0a 0a 20 20 2f 2a 20 44 65 74 61 63 68 20 74 68 .. /* Detach th
2016c 69 73 20 62 61 63 6b 75 70 20 66 72 6f 6d 20 74 is backup from t
2016d 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e he source pager.
2016e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 */. if( p->pDe
2016f 73 74 44 62 20 29 7b 0a 20 20 20 20 70 2d 3e 70 stDb ){. p->p
20170 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d 3b 0a Src->nBackup--;.
20171 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 73 41 }. if( p->isA
20172 74 74 61 63 68 65 64 20 29 7b 0a 20 20 20 20 70 ttached ){. p
20173 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 p = sqlite3Pager
20174 42 61 63 6b 75 70 50 74 72 28 73 71 6c 69 74 65 BackupPtr(sqlite
20175 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 3BtreePager(p->p
20176 53 72 63 29 29 3b 0a 20 20 20 20 77 68 69 6c 65 Src));. while
20177 28 20 2a 70 70 21 3d 70 20 29 7b 0a 20 20 20 20 ( *pp!=p ){.
20178 20 20 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 pp = &(*pp)->p
20179 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Next;. }.
2017a 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a *pp = p->pNext;.
2017b 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74 }.. /* If a t
2017c 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 ransaction is st
2017d 69 6c 6c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 ill open on the
2017e 42 74 72 65 65 2c 20 72 6f 6c 6c 20 69 74 20 62 Btree, roll it b
2017f 61 63 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ack. */. sqlite
20180 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 3BtreeRollback(p
20181 2d 3e 70 44 65 73 74 2c 20 53 51 4c 49 54 45 5f ->pDest, SQLITE_
20182 4f 4b 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 OK);.. /* Set t
20183 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 he error code of
20184 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
20185 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
20186 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 28 70 2d 3e . */. rc = (p->
20187 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 rc==SQLITE_DONE)
20188 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 70 ? SQLITE_OK : p
20189 2d 3e 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 45 ->rc;. sqlite3E
2018a 72 72 6f 72 28 70 2d 3e 70 44 65 73 74 44 62 2c rror(p->pDestDb,
2018b 20 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 45 rc, 0);.. /* E
2018c 78 69 74 20 74 68 65 20 6d 75 74 65 78 65 73 20 xit the mutexes
2018d 61 6e 64 20 66 72 65 65 20 74 68 65 20 62 61 63 and free the bac
2018e 6b 75 70 20 63 6f 6e 74 65 78 74 20 73 74 72 75 kup context stru
2018f 63 74 75 72 65 2e 20 2a 2f 0a 20 20 69 66 28 20 cture. */. if(
20190 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 p->pDestDb ){.
20191 20 20 73 71 6c 69 74 65 33 4c 65 61 76 65 4d 75 sqlite3LeaveMu
20192 74 65 78 41 6e 64 43 6c 6f 73 65 5a 6f 6d 62 69 texAndCloseZombi
20193 65 28 70 2d 3e 70 44 65 73 74 44 62 29 3b 0a 20 e(p->pDestDb);.
20194 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 }. sqlite3Btre
20195 65 4c 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b eLeave(p->pSrc);
20196 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 . if( p->pDestD
20197 62 20 29 7b 0a 20 20 20 20 2f 2a 20 45 56 49 44 b ){. /* EVID
20198 45 4e 43 45 2d 4f 46 3a 20 52 2d 36 34 38 35 32 ENCE-OF: R-64852
20199 2d 32 31 35 39 31 20 54 68 65 20 73 71 6c 69 74 -21591 The sqlit
2019a 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 e3_backup object
2019b 20 69 73 20 63 72 65 61 74 65 64 20 62 79 20 61 is created by a
2019c 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 . ** call to
2019d 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 sqlite3_backup_i
2019e 6e 69 74 28 29 20 61 6e 64 20 69 73 20 64 65 73 nit() and is des
2019f 74 72 6f 79 65 64 20 62 79 20 61 20 63 61 6c 6c troyed by a call
201a0 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 to. ** sqlit
201a1 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 e3_backup_finish
201a2 28 29 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 (). */. sqlit
201a3 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a e3_free(p);. }.
201a4 20 20 73 71 6c 69 74 65 33 4c 65 61 76 65 4d 75 sqlite3LeaveMu
201a5 74 65 78 41 6e 64 43 6c 6f 73 65 5a 6f 6d 62 69 texAndCloseZombi
201a6 65 28 70 53 72 63 44 62 29 3b 0a 20 20 72 65 74 e(pSrcDb);. ret
201a7 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
201a8 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
201a9 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c er of pages stil
201aa 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 64 20 75 l to be backed u
201ab 70 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 p as of the most
201ac 20 72 65 63 65 6e 74 0a 2a 2a 20 63 61 6c 6c 20 recent.** call
201ad 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
201ae 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c p_step()..*/.SQL
201af 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
201b0 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 te3_backup_remai
201b1 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 ning(sqlite3_bac
201b2 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 kup *p){. retur
201b3 6e 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 3b n p->nRemaining;
201b4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
201b5 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 the total numbe
201b6 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
201b7 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
201b8 65 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 e as of the most
201b9 20 0a 2a 2a 20 72 65 63 65 6e 74 20 63 61 6c 6c .** recent call
201ba 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
201bb 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 up_step()..*/.SQ
201bc 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
201bd 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 ite3_backup_page
201be 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61 count(sqlite3_ba
201bf 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 ckup *p){. retu
201c0 72 6e 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 rn p->nPagecount
201c1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
201c2 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
201c3 65 64 20 61 66 74 65 72 20 74 68 65 20 63 6f 6e ed after the con
201c4 74 65 6e 74 73 20 6f 66 20 70 61 67 65 20 69 50 tents of page iP
201c5 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 6f age of the.** so
201c6 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61 urce database ha
201c7 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 ve been modified
201c8 2e 20 49 66 20 70 61 67 65 20 69 50 61 67 65 20 . If page iPage
201c9 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
201ca 20 0a 2a 2a 20 63 6f 70 69 65 64 20 69 6e 74 6f .** copied into
201cb 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
201cc 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 database, then
201cd 74 68 65 20 64 61 74 61 20 77 72 69 74 74 65 6e the data written
201ce 20 74 6f 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 to the.** desti
201cf 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 77 20 69 6e nation is now in
201d0 76 61 6c 69 64 61 74 65 64 2e 20 54 68 65 20 64 validated. The d
201d1 65 73 74 69 6e 61 74 69 6f 6e 20 63 6f 70 79 20 estination copy
201d2 6f 66 20 69 50 61 67 65 20 6e 65 65 64 73 0a 2a of iPage needs.*
201d3 2a 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 * to be updated
201d4 77 69 74 68 20 74 68 65 20 6e 65 77 20 64 61 74 with the new dat
201d5 61 20 62 65 66 6f 72 65 20 74 68 65 20 62 61 63 a before the bac
201d6 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 kup operation is
201d7 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a .** complete..**
201d8 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 .** It is assume
201d9 64 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 d that the mutex
201da 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
201db 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 the BtShared ob
201dc 6a 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f ject.** correspo
201dd 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 nding to the sou
201de 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 rce database is
201df 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 held when this f
201e0 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 unction is.** ca
201e1 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f lled..*/.SQLITE_
201e2 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
201e3 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65 ite3BackupUpdate
201e4 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
201e5 2a 70 42 61 63 6b 75 70 2c 20 50 67 6e 6f 20 69 *pBackup, Pgno i
201e6 50 61 67 65 2c 20 63 6f 6e 73 74 20 75 38 20 2a Page, const u8 *
201e7 61 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74 65 aData){. sqlite
201e8 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 3_backup *p;
201e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
201ea 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 * Iterator varia
201eb 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 ble */. for(p=p
201ec 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e Backup; p; p=p->
201ed 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 pNext){. asse
201ee 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
201ef 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e x_held(p->pSrc->
201f0 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
201f1 20 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45 if( !isFatalE
201f2 72 72 6f 72 28 70 2d 3e 72 63 29 20 26 26 20 69 rror(p->rc) && i
201f3 50 61 67 65 3c 70 2d 3e 69 4e 65 78 74 20 29 7b Page<p->iNext ){
201f4 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 61 . /* The ba
201f5 63 6b 75 70 20 70 72 6f 63 65 73 73 20 70 20 68 ckup process p h
201f6 61 73 20 61 6c 72 65 61 64 79 20 63 6f 70 69 65 as already copie
201f7 64 20 70 61 67 65 20 69 50 61 67 65 2e 20 42 75 d page iPage. Bu
201f8 74 20 6e 6f 77 20 69 74 0a 20 20 20 20 20 20 2a t now it. *
201f9 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 * has been modif
201fa 69 65 64 20 62 79 20 61 20 74 72 61 6e 73 61 63 ied by a transac
201fb 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72 tion on the sour
201fc 63 65 20 70 61 67 65 72 2e 20 43 6f 70 79 0a 20 ce pager. Copy.
201fd 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 ** the new
201fe 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 62 61 data into the ba
201ff 63 6b 75 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ckup.. */.
20200 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 int rc;.
20201 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 44 assert( p->pD
20202 65 73 74 44 62 20 29 3b 0a 20 20 20 20 20 20 73 estDb );. s
20203 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
20204 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d er(p->pDestDb->m
20205 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72 63 20 utex);. rc
20206 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 = backupOnePage(
20207 70 2c 20 69 50 61 67 65 2c 20 61 44 61 74 61 29 p, iPage, aData)
20208 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
20209 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 mutex_leave(p->p
2020a 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a DestDb->mutex);.
2020b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
2020c 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 !=SQLITE_BUSY &&
2020d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b rc!=SQLITE_LOCK
2020e 45 44 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ED );. if(
2020f 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
20210 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d . p->rc =
20211 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
20212 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
20213 52 65 73 74 61 72 74 20 74 68 65 20 62 61 63 6b Restart the back
20214 75 70 20 70 72 6f 63 65 73 73 2e 20 54 68 69 73 up process. This
20215 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 is called when
20216 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 0a the pager layer.
20217 2a 2a 20 64 65 74 65 63 74 73 20 74 68 61 74 20 ** detects that
20218 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 the database has
20219 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 62 been modified b
2021a 79 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 64 61 y an external da
2021b 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 tabase.** connec
2021c 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 tion. In this ca
2021d 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 se there is no w
2021e 61 79 20 6f 66 20 6b 6e 6f 77 69 6e 67 20 77 68 ay of knowing wh
2021f 69 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 ich of the.** pa
20220 67 65 73 20 74 68 61 74 20 68 61 76 65 20 62 65 ges that have be
20221 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 en copied into t
20222 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 he destination d
20223 61 74 61 62 61 73 65 20 61 72 65 20 73 74 69 6c atabase are stil
20224 6c 20 0a 2a 2a 20 76 61 6c 69 64 20 61 6e 64 20 l .** valid and
20225 77 68 69 63 68 20 61 72 65 20 6e 6f 74 2c 20 73 which are not, s
20226 6f 20 74 68 65 20 65 6e 74 69 72 65 20 70 72 6f o the entire pro
20227 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62 65 cess needs to be
20228 20 72 65 73 74 61 72 74 65 64 2e 0a 2a 2a 0a 2a restarted..**.*
20229 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 * It is assumed
2022a 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20 61 that the mutex a
2022b 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
2022c 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 he BtShared obje
2022d 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 ct.** correspond
2022e 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72 63 ing to the sourc
2022f 65 20 64 61 74 61 62 61 73 65 20 69 73 20 68 65 e database is he
20230 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e ld when this fun
20231 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c ction is.** call
20232 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
20233 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
20234 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 e3BackupRestart(
20235 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
20236 70 42 61 63 6b 75 70 29 7b 0a 20 20 73 71 6c 69 pBackup){. sqli
20237 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 te3_backup *p;
20238 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20239 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 /* Iterator var
2023a 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 iable */. for(p
2023b 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 =pBackup; p; p=p
2023c 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 ->pNext){. as
2023d 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
2023e 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 tex_held(p->pSrc
2023f 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
20240 0a 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 . p->iNext =
20241 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 1;. }.}..#ifnde
20242 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 f SQLITE_OMIT_VA
20243 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 CUUM./*.** Copy
20244 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e the complete con
20245 74 65 6e 74 20 6f 66 20 70 42 74 46 72 6f 6d 20 tent of pBtFrom
20246 69 6e 74 6f 20 70 42 74 54 6f 2e 20 20 41 20 74 into pBtTo. A t
20247 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 ransaction.** mu
20248 73 74 20 62 65 20 61 63 74 69 76 65 20 66 6f 72 st be active for
20249 20 62 6f 74 68 20 66 69 6c 65 73 2e 0a 2a 2a 0a both files..**.
2024a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 66 ** The size of f
2024b 69 6c 65 20 70 54 6f 20 6d 61 79 20 62 65 20 72 ile pTo may be r
2024c 65 64 75 63 65 64 20 62 79 20 74 68 69 73 20 6f educed by this o
2024d 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 61 6e 79 peration. If any
2024e 74 68 69 6e 67 20 0a 2a 2a 20 67 6f 65 73 20 77 thing .** goes w
2024f 72 6f 6e 67 2c 20 74 68 65 20 74 72 61 6e 73 61 rong, the transa
20250 63 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 20 ction on pTo is
20251 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 rolled back. If
20252 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 20 successful, the
20253 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 .** transaction
20254 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 65 66 is committed bef
20255 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
20256 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20257 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
20258 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20 eCopyFile(Btree
20259 2a 70 54 6f 2c 20 42 74 72 65 65 20 2a 70 46 72 *pTo, Btree *pFr
2025a 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 om){. int rc;.
2025b 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
2025c 46 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Fd;
2025d 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 /* File descrip
2025e 74 6f 72 20 66 6f 72 20 64 61 74 61 62 61 73 65 tor for database
2025f 20 70 54 6f 20 2a 2f 0a 20 20 73 71 6c 69 74 65 pTo */. sqlite
20260 33 5f 62 61 63 6b 75 70 20 62 3b 0a 20 20 73 71 3_backup b;. sq
20261 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
20262 70 54 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 pTo);. sqlite3B
20263 74 72 65 65 45 6e 74 65 72 28 70 46 72 6f 6d 29 treeEnter(pFrom)
20264 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
20265 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 ite3BtreeIsInTra
20266 6e 73 28 70 54 6f 29 20 29 3b 0a 20 20 70 46 64 ns(pTo) );. pFd
20267 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 = sqlite3PagerF
20268 69 6c 65 28 73 71 6c 69 74 65 33 42 74 72 65 65 ile(sqlite3Btree
20269 50 61 67 65 72 28 70 54 6f 29 29 3b 0a 20 20 69 Pager(pTo));. i
2026a 66 28 20 70 46 64 2d 3e 70 4d 65 74 68 6f 64 73 f( pFd->pMethods
2026b 20 29 7b 0a 20 20 20 20 69 36 34 20 6e 42 79 74 ){. i64 nByt
2026c 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 e = sqlite3Btree
2026d 47 65 74 50 61 67 65 53 69 7a 65 28 70 46 72 6f GetPageSize(pFro
2026e 6d 29 2a 28 69 36 34 29 73 71 6c 69 74 65 33 42 m)*(i64)sqlite3B
2026f 74 72 65 65 4c 61 73 74 50 61 67 65 28 70 46 72 treeLastPage(pFr
20270 6f 6d 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 om);. rc = sq
20271 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 lite3OsFileContr
20272 6f 6c 28 70 46 64 2c 20 53 51 4c 49 54 45 5f 46 ol(pFd, SQLITE_F
20273 43 4e 54 4c 5f 4f 56 45 52 57 52 49 54 45 2c 20 CNTL_OVERWRITE,
20274 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 &nByte);. if(
20275 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 54 46 rc==SQLITE_NOTF
20276 4f 55 4e 44 20 29 20 72 63 20 3d 20 53 51 4c 49 OUND ) rc = SQLI
20277 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 66 28 20 72 TE_OK;. if( r
20278 63 20 29 20 67 6f 74 6f 20 63 6f 70 79 5f 66 69 c ) goto copy_fi
20279 6e 69 73 68 65 64 3b 0a 20 20 7d 0a 0a 20 20 2f nished;. }.. /
2027a 2a 20 53 65 74 20 75 70 20 61 6e 20 73 71 6c 69 * Set up an sqli
2027b 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 te3_backup objec
2027c 74 2e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 t. sqlite3_backu
2027d 70 2e 70 44 65 73 74 44 62 20 6d 75 73 74 20 62 p.pDestDb must b
2027e 65 20 73 65 74 0a 20 20 2a 2a 20 74 6f 20 30 2e e set. ** to 0.
2027f 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 This is used by
20280 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
20281 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f ions of sqlite3_
20282 62 61 63 6b 75 70 5f 73 74 65 70 28 29 0a 20 20 backup_step().
20283 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 ** and sqlite3_b
20284 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 74 ackup_finish() t
20285 6f 20 64 65 74 65 63 74 20 74 68 61 74 20 74 68 o detect that th
20286 65 79 20 61 72 65 20 62 65 69 6e 67 20 63 61 6c ey are being cal
20287 6c 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 led. ** from th
20288 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 6e 6f 74 is function, not
20289 20 64 69 72 65 63 74 6c 79 20 62 79 20 74 68 65 directly by the
2028a 20 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 6d 65 user.. */. me
2028b 6d 73 65 74 28 26 62 2c 20 30 2c 20 73 69 7a 65 mset(&b, 0, size
2028c 6f 66 28 62 29 29 3b 0a 20 20 62 2e 70 53 72 63 of(b));. b.pSrc
2028d 44 62 20 3d 20 70 46 72 6f 6d 2d 3e 64 62 3b 0a Db = pFrom->db;.
2028e 20 20 62 2e 70 53 72 63 20 3d 20 70 46 72 6f 6d b.pSrc = pFrom
2028f 3b 0a 20 20 62 2e 70 44 65 73 74 20 3d 20 70 54 ;. b.pDest = pT
20290 6f 3b 0a 20 20 62 2e 69 4e 65 78 74 20 3d 20 31 o;. b.iNext = 1
20291 3b 0a 0a 20 20 2f 2a 20 30 78 37 46 46 46 46 46 ;.. /* 0x7FFFFF
20292 46 46 20 69 73 20 74 68 65 20 68 61 72 64 20 6c FF is the hard l
20293 69 6d 69 74 20 66 6f 72 20 74 68 65 20 6e 75 6d imit for the num
20294 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
20295 61 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 a database. **
20296 66 69 6c 65 2e 20 42 79 20 70 61 73 73 69 6e 67 file. By passing
20297 20 74 68 69 73 20 61 73 20 74 68 65 20 6e 75 6d this as the num
20298 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 ber of pages to
20299 63 6f 70 79 20 74 6f 0a 20 20 2a 2a 20 73 71 6c copy to. ** sql
2029a 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
2029b 28 29 2c 20 77 65 20 63 61 6e 20 67 75 61 72 61 (), we can guara
2029c 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 63 6f ntee that the co
2029d 70 79 20 66 69 6e 69 73 68 65 73 20 0a 20 20 2a py finishes . *
2029e 2a 20 77 69 74 68 69 6e 20 61 20 73 69 6e 67 6c * within a singl
2029f 65 20 63 61 6c 6c 20 28 75 6e 6c 65 73 73 20 61 e call (unless a
202a0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 29 2e n error occurs).
202a1 20 54 68 65 20 61 73 73 65 72 74 28 29 20 73 74 The assert() st
202a2 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 63 68 65 atement. ** che
202a3 63 6b 73 20 74 68 69 73 20 61 73 73 75 6d 70 74 cks this assumpt
202a4 69 6f 6e 20 2d 20 28 70 2d 3e 72 63 29 20 73 68 ion - (p->rc) sh
202a5 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 65 ould be set to e
202a6 69 74 68 65 72 20 53 51 4c 49 54 45 5f 44 4f 4e ither SQLITE_DON
202a7 45 20 0a 20 20 2a 2a 20 6f 72 20 61 6e 20 65 72 E . ** or an er
202a8 72 6f 72 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 ror code.. */.
202a9 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
202aa 73 74 65 70 28 26 62 2c 20 30 78 37 46 46 46 46 step(&b, 0x7FFFF
202ab 46 46 46 29 3b 0a 20 20 61 73 73 65 72 74 28 20 FFF);. assert(
202ac 62 2e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 b.rc!=SQLITE_OK
202ad 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
202ae 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 3_backup_finish(
202af 26 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 &b);. if( rc==S
202b0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
202b1 70 54 6f 2d 3e 70 42 74 2d 3e 62 74 73 46 6c 61 pTo->pBt->btsFla
202b2 67 73 20 26 3d 20 7e 42 54 53 5f 50 41 47 45 53 gs &= ~BTS_PAGES
202b3 49 5a 45 5f 46 49 58 45 44 3b 0a 20 20 7d 65 6c IZE_FIXED;. }el
202b4 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 se{. sqlite3P
202b5 61 67 65 72 43 6c 65 61 72 43 61 63 68 65 28 73 agerClearCache(s
202b6 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 qlite3BtreePager
202b7 28 62 2e 70 44 65 73 74 29 29 3b 0a 20 20 7d 0a (b.pDest));. }.
202b8 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
202b9 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 e3BtreeIsInTrans
202ba 28 70 54 6f 29 3d 3d 30 20 29 3b 0a 63 6f 70 79 (pTo)==0 );.copy
202bb 5f 66 69 6e 69 73 68 65 64 3a 0a 20 20 73 71 6c _finished:. sql
202bc 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
202bd 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 From);. sqlite3
202be 42 74 72 65 65 4c 65 61 76 65 28 70 54 6f 29 3b BtreeLeave(pTo);
202bf 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
202c0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
202c1 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 2a 2f 0a _OMIT_VACUUM */.
202c2 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
202c3 20 45 6e 64 20 6f 66 20 62 61 63 6b 75 70 2e 63 End of backup.c
202c4 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
202c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
202c7 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
202c8 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 Begin file vdbe
202c9 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mem.c **********
202ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
202cc 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 ./*.** 2004 May
202cd 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 26.**.** The aut
202ce 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
202cf 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
202d0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
202d1 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
202d2 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
202d3 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
202d4 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
202d5 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
202d6 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
202d7 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
202d8 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
202d9 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
202da 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
202db 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
202dc 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
202dd 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
202de 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
202df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
202e3 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
202e4 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 e contains code
202e5 75 73 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61 74 use to manipulat
202e6 65 20 22 4d 65 6d 22 20 73 74 72 75 63 74 75 72 e "Mem" structur
202e7 65 2e 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 73 e. A "Mem".** s
202e8 74 6f 72 65 73 20 61 20 73 69 6e 67 6c 65 20 76 tores a single v
202e9 61 6c 75 65 20 69 6e 20 74 68 65 20 56 44 42 45 alue in the VDBE
202ea 2e 20 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70 61 . Mem is an opa
202eb 71 75 65 20 73 74 72 75 63 74 75 72 65 20 76 69 que structure vi
202ec 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 69 sible.** only wi
202ed 74 68 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 thin the VDBE.
202ee 49 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e Interface routin
202ef 65 73 20 72 65 66 65 72 20 74 6f 20 61 20 4d 65 es refer to a Me
202f0 6d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 6e m using the.** n
202f1 61 6d 65 20 73 71 6c 69 74 65 5f 76 61 6c 75 65 ame sqlite_value
202f2 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 4d .*/../*.** If pM
202f3 65 6d 20 69 73 20 61 6e 20 6f 62 6a 65 63 74 20 em is an object
202f4 77 69 74 68 20 61 20 76 61 6c 69 64 20 73 74 72 with a valid str
202f5 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 ing representati
202f6 6f 6e 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 on, this routine
202f7 0a 2a 2a 20 65 6e 73 75 72 65 73 20 74 68 65 20 .** ensures the
202f8 69 6e 74 65 72 6e 61 6c 20 65 6e 63 6f 64 69 6e internal encodin
202f9 67 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 g for the string
202fa 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
202fb 69 73 0a 2a 2a 20 27 64 65 73 69 72 65 64 45 6e is.** 'desiredEn
202fc 63 27 2c 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 c', one of SQLIT
202fd 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 55 E_UTF8, SQLITE_U
202fe 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45 TF16LE or SQLITE
202ff 5f 55 54 46 31 36 42 45 2e 0a 2a 2a 0a 2a 2a 20 _UTF16BE..**.**
20300 49 66 20 70 4d 65 6d 20 69 73 20 6e 6f 74 20 61 If pMem is not a
20301 20 73 74 72 69 6e 67 20 6f 62 6a 65 63 74 2c 20 string object,
20302 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 or the encoding
20303 6f 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a of the string.**
20304 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
20305 69 73 20 61 6c 72 65 61 64 79 20 73 74 6f 72 65 is already store
20306 64 20 75 73 69 6e 67 20 74 68 65 20 72 65 71 75 d using the requ
20307 65 73 74 65 64 20 65 6e 63 6f 64 69 6e 67 2c 20 ested encoding,
20308 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75 then this.** rou
20309 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tine is a no-op.
2030a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
2030b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
2030c 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 the conversion i
2030d 73 20 73 75 63 63 65 73 73 66 75 6c 20 28 6f 72 s successful (or
2030e 20 6e 6f 74 20 72 65 71 75 69 72 65 64 29 2e 0a not required)..
2030f 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 ** SQLITE_NOMEM
20310 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65 64 20 may be returned
20311 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 if a malloc() fa
20312 69 6c 73 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 ils during conve
20313 72 73 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e rsion.** between
20314 20 66 6f 72 6d 61 74 73 2e 0a 2a 2f 0a 53 51 4c formats..*/.SQL
20315 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
20316 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
20317 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 2a 70 eEncoding(Mem *p
20318 4d 65 6d 2c 20 69 6e 74 20 64 65 73 69 72 65 64 Mem, int desired
20319 45 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Enc){. int rc;.
2031a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
2031b 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 >flags&MEM_RowSe
2031c 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 t)==0 );. asser
2031d 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 t( desiredEnc==S
2031e 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 64 65 QLITE_UTF8 || de
2031f 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 siredEnc==SQLITE
20320 5f 55 54 46 31 36 4c 45 0a 20 20 20 20 20 20 20 _UTF16LE.
20321 20 20 20 20 7c 7c 20 64 65 73 69 72 65 64 45 6e || desiredEn
20322 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 c==SQLITE_UTF16B
20323 45 20 29 3b 0a 20 20 69 66 28 20 21 28 70 4d 65 E );. if( !(pMe
20324 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 m->flags&MEM_Str
20325 29 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d ) || pMem->enc==
20326 64 65 73 69 72 65 64 45 6e 63 20 29 7b 0a 20 20 desiredEnc ){.
20327 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
20328 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 OK;. }. assert
20329 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
2032a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
2032b 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
2032c 74 65 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53 tex) );.#ifdef S
2032d 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
2032e 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
2032f 5f 45 52 52 4f 52 3b 0a 23 65 6c 73 65 0a 0a 20 _ERROR;.#else..
20330 20 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c 61 74 65 /* MemTranslate
20331 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 () may return SQ
20332 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54 LITE_OK or SQLIT
20333 45 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f 4d 45 E_NOMEM. If NOME
20334 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 20 M is returned,.
20335 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 65 6e 63 ** then the enc
20336 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 76 61 6c oding of the val
20337 75 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 ue may not have
20338 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 changed.. */.
20339 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
2033a 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 70 4d 65 MemTranslate(pMe
2033b 6d 2c 20 28 75 38 29 64 65 73 69 72 65 64 45 6e m, (u8)desiredEn
2033c 63 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d c);. assert(rc=
2033d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 7c 7c =SQLITE_OK ||
2033e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc==SQLITE_NOME
2033f 4d 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d M);. assert(rc=
20340 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 7c 7c =SQLITE_OK ||
20341 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 pMem->enc!=desi
20342 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73 65 72 redEnc);. asser
20343 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d t(rc==SQLITE_NOM
20344 45 4d 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d EM || pMem->enc=
20345 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 =desiredEnc);.
20346 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 return rc;.#endi
20347 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 f.}../*.** Make
20348 73 75 72 65 20 70 4d 65 6d 2d 3e 7a 20 70 6f 69 sure pMem->z poi
20349 6e 74 73 20 74 6f 20 61 20 77 72 69 74 61 62 6c nts to a writabl
2034a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 e allocation of
2034b 61 74 20 6c 65 61 73 74 20 0a 2a 2a 20 6e 20 62 at least .** n b
2034c 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ytes..**.** If t
2034d 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
2034e 74 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 t passed to this
2034f 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 function is tru
20350 65 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 0a 2a e, then memory.*
20351 2a 20 63 65 6c 6c 20 70 4d 65 6d 20 6d 75 73 74 * cell pMem must
20352 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e contain a strin
20353 67 20 6f 72 20 62 6c 6f 62 2e 20 49 6e 20 74 68 g or blob. In th
20354 69 73 20 63 61 73 65 20 74 68 65 20 63 6f 6e 74 is case the cont
20355 65 6e 74 20 69 73 0a 2a 2a 20 70 72 65 73 65 72 ent is.** preser
20356 76 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ved. Otherwise,
20357 69 66 20 74 68 65 20 74 68 69 72 64 20 70 61 72 if the third par
20358 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73 20 66 ameter to this f
20359 75 6e 63 74 69 6f 6e 20 69 73 20 66 61 6c 73 65 unction is false
2035a 2c 0a 2a 2a 20 61 6e 79 20 63 75 72 72 65 6e 74 ,.** any current
2035b 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 string or blob
2035c 76 61 6c 75 65 20 6d 61 79 20 62 65 20 64 69 73 value may be dis
2035d 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 carded..**.** Th
2035e 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 is function sets
2035f 20 74 68 65 20 4d 45 4d 5f 44 79 6e 20 66 6c 61 the MEM_Dyn fla
20360 67 20 61 6e 64 20 63 6c 65 61 72 73 20 61 6e 79 g and clears any
20361 20 78 44 65 6c 20 63 61 6c 6c 62 61 63 6b 2e 0a xDel callback..
20362 2a 2a 20 49 74 20 61 6c 73 6f 20 63 6c 65 61 72 ** It also clear
20363 73 20 4d 45 4d 5f 45 70 68 65 6d 20 61 6e 64 20 s MEM_Ephem and
20364 4d 45 4d 5f 53 74 61 74 69 63 2e 20 49 66 20 74 MEM_Static. If t
20365 68 65 20 70 72 65 73 65 72 76 65 20 66 6c 61 67 he preserve flag
20366 20 69 73 20 0a 2a 2a 20 6e 6f 74 20 73 65 74 2c is .** not set,
20367 20 4d 65 6d 2e 6e 20 69 73 20 7a 65 72 6f 65 64 Mem.n is zeroed
20368 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20369 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
2036a 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65 6d 20 2a dbeMemGrow(Mem *
2036b 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 pMem, int n, int
2036c 20 70 72 65 73 65 72 76 65 29 7b 0a 20 20 61 73 preserve){. as
2036d 73 65 72 74 28 20 31 20 3e 3d 0a 20 20 20 20 28 sert( 1 >=. (
2036e 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 (pMem->zMalloc &
2036f 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3d & pMem->zMalloc=
20370 3d 70 4d 65 6d 2d 3e 7a 29 20 3f 20 31 20 3a 20 =pMem->z) ? 1 :
20371 30 29 20 2b 0a 20 20 20 20 28 28 28 70 4d 65 6d 0) +. (((pMem
20372 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 ->flags&MEM_Dyn)
20373 26 26 70 4d 65 6d 2d 3e 78 44 65 6c 29 20 3f 20 &&pMem->xDel) ?
20374 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28 1 : 0) + . ((
20375 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
20376 45 70 68 65 6d 29 20 3f 20 31 20 3a 20 30 29 20 Ephem) ? 1 : 0)
20377 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66 + . ((pMem->f
20378 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 63 29 lags&MEM_Static)
20379 20 3f 20 31 20 3a 20 30 29 0a 20 20 29 3b 0a 20 ? 1 : 0). );.
2037a 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
2037b 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 flags&MEM_RowSet
2037c 29 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 )==0 );.. /* If
2037d 20 74 68 65 20 70 72 65 73 65 72 76 65 20 66 6c the preserve fl
2037e 61 67 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 ag is set to tru
2037f 65 2c 20 74 68 65 6e 20 74 68 65 20 6d 65 6d 6f e, then the memo
20380 72 79 20 63 65 6c 6c 20 6d 75 73 74 20 61 6c 72 ry cell must alr
20381 65 61 64 79 0a 20 20 2a 2a 20 63 6f 6e 74 61 69 eady. ** contai
20382 6e 20 61 20 76 61 6c 69 64 20 73 74 72 69 6e 67 n a valid string
20383 20 6f 72 20 62 6c 6f 62 20 76 61 6c 75 65 2e 20 or blob value.
20384 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 72 */. assert( pr
20385 65 73 65 72 76 65 3d 3d 30 20 7c 7c 20 70 4d 65 eserve==0 || pMe
20386 6d 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 42 6c m->flags&(MEM_Bl
20387 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 3b 0a 0a ob|MEM_Str) );..
20388 20 20 69 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d if( n<32 ) n =
20389 20 33 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 32;. if( sqlit
2038a 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 e3DbMallocSize(p
2038b 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a Mem->db, pMem->z
2038c 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 Malloc)<n ){.
2038d 20 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26 if( preserve &&
2038e 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e pMem->z==pMem->
2038f 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 zMalloc ){.
20390 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d pMem->z = pMem-
20391 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 >zMalloc = sqlit
20392 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 e3DbReallocOrFre
20393 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d e(pMem->db, pMem
20394 2d 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 70 ->z, n);. p
20395 72 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 reserve = 0;.
20396 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 }else{. sq
20397 6c 69 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d lite3DbFree(pMem
20398 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c ->db, pMem->zMal
20399 6c 6f 63 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d loc);. pMem
2039a 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 ->zMalloc = sqli
2039b 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 te3DbMallocRaw(p
2039c 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20 Mem->db, n);.
2039d 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d }. }.. if( pM
2039e 65 6d 2d 3e 7a 20 26 26 20 70 72 65 73 65 72 76 em->z && preserv
2039f 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c e && pMem->zMall
203a0 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 oc && pMem->z!=p
203a1 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a Mem->zMalloc ){.
203a2 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d memcpy(pMem-
203a3 3e 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e >zMalloc, pMem->
203a4 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d z, pMem->n);. }
203a5 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 . if( pMem->fla
203a6 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 4d gs&MEM_Dyn && pM
203a7 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 em->xDel ){.
203a8 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 78 44 assert( pMem->xD
203a9 65 6c 21 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d el!=SQLITE_DYNAM
203aa 49 43 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e IC );. pMem->
203ab 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28 70 4d xDel((void *)(pM
203ac 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20 20 em->z));. }..
203ad 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e pMem->z = pMem->
203ae 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 66 28 20 70 zMalloc;. if( p
203af 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 Mem->z==0 ){.
203b0 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
203b1 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 EM_Null;. }else
203b2 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 {. pMem->flag
203b3 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d s &= ~(MEM_Ephem
203b4 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 |MEM_Static);.
203b5 7d 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d }. pMem->xDel =
203b6 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 4d 0;. return (pM
203b7 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45 5f 4f em->z ? SQLITE_O
203b8 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d K : SQLITE_NOMEM
203b9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 );.}../*.** Make
203ba 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 6f the given Mem o
203bb 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e 20 20 bject MEM_Dyn.
203bc 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
203bd 6d 61 6b 65 20 69 74 20 73 6f 0a 2a 2a 20 74 68 make it so.** th
203be 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72 20 42 at any TEXT or B
203bf 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 LOB content is s
203c0 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 tored in memory
203c1 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a obtained from.**
203c2 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e 20 74 malloc(). In t
203c3 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e 6f 77 his way, we know
203c4 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 that the memory
203c5 20 69 73 20 73 61 66 65 20 74 6f 20 62 65 0a 2a is safe to be.*
203c6 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 * overwritten or
203c7 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 altered..**.**
203c8 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
203c9 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 53 on success or S
203ca 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 6d QLITE_NOMEM if m
203cb 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a alloc fails..*/.
203cc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
203cd 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
203ce 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 4d mMakeWriteable(M
203cf 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 em *pMem){. int
203d0 20 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d f;. assert( pM
203d1 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
203d2 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
203d3 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
203d4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
203d5 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 Mem->flags&MEM_R
203d6 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 45 owSet)==0 );. E
203d7 78 70 61 6e 64 42 6c 6f 62 28 70 4d 65 6d 29 3b xpandBlob(pMem);
203d8 0a 20 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 . f = pMem->fla
203d9 67 73 3b 0a 20 20 69 66 28 20 28 66 26 28 4d 45 gs;. if( (f&(ME
203da 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 M_Str|MEM_Blob))
203db 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 && pMem->z!=pMe
203dc 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 m->zMalloc ){.
203dd 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
203de 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 70 eMemGrow(pMem, p
203df 4d 65 6d 2d 3e 6e 20 2b 20 32 2c 20 31 29 20 29 Mem->n + 2, 1) )
203e0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
203e1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
203e2 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 }. pMem->z[p
203e3 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 20 Mem->n] = 0;.
203e4 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e pMem->z[pMem->n
203e5 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 +1] = 0;. pMe
203e6 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f m->flags |= MEM_
203e7 54 65 72 6d 3b 0a 23 69 66 64 65 66 20 53 51 4c Term;.#ifdef SQL
203e8 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 70 4d ITE_DEBUG. pM
203e9 65 6d 2d 3e 70 53 63 6f 70 79 46 72 6f 6d 20 3d em->pScopyFrom =
203ea 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 0;.#endif. }..
203eb 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
203ec 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 OK;.}../*.** If
203ed 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20 68 the given Mem* h
203ee 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 as a zero-filled
203ef 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20 69 tail, turn it i
203f0 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79 0a nto an ordinary.
203f1 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20 69 ** blob stored i
203f2 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c n dynamically al
203f3 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a 2a located space..*
203f4 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
203f5 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 _OMIT_INCRBLOB.S
203f6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
203f7 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
203f8 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a ExpandBlob(Mem *
203f9 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 pMem){. if( pMe
203fa 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a m->flags & MEM_Z
203fb 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e ero ){. int n
203fc 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72 74 Byte;. assert
203fd 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 ( pMem->flags&ME
203fe 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61 73 M_Blob );. as
203ff 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
20400 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d gs&MEM_RowSet)==
20401 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
20402 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pMem->db==0 ||
20403 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
20404 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 ld(pMem->db->mut
20405 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 ex) );.. /* S
20406 65 74 20 6e 42 79 74 65 20 74 6f 20 74 68 65 20 et nByte to the
20407 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
20408 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 required to stor
20409 65 20 74 68 65 20 65 78 70 61 6e 64 65 64 20 62 e the expanded b
2040a 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e 42 79 74 lob. */. nByt
2040b 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d e = pMem->n + pM
2040c 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 em->u.nZero;.
2040d 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29 7b if( nByte<=0 ){
2040e 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 31 . nByte = 1
2040f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
20410 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
20411 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 ow(pMem, nByte,
20412 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 1) ){. retu
20413 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
20414 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 73 . }.. mems
20415 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d et(&pMem->z[pMem
20416 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75 ->n], 0, pMem->u
20417 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 70 4d 65 .nZero);. pMe
20418 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e m->n += pMem->u.
20419 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d nZero;. pMem-
2041a 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
2041b 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a Zero|MEM_Term);.
2041c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
2041d 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 ITE_OK;.}.#endif
2041e 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 .../*.** Make su
2041f 72 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d re the given Mem
20420 20 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d 69 is \u0000 termi
20421 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nated..*/.SQLITE
20422 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
20423 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 ite3VdbeMemNulTe
20424 72 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65 rminate(Mem *pMe
20425 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d m){. assert( pM
20426 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
20427 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
20428 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
20429 20 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d );. if( (pMem-
2042a 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 >flags & MEM_Ter
2042b 6d 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e m)!=0 || (pMem->
2042c 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 flags & MEM_Str)
2042d 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
2042e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f n SQLITE_OK; /
2042f 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 * Nothing to do
20430 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c */. }. if( sql
20431 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
20432 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c pMem, pMem->n+2,
20433 20 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 1) ){. retur
20434 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
20435 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d }. pMem->z[pM
20436 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d em->n] = 0;. pM
20437 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d em->z[pMem->n+1]
20438 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c = 0;. pMem->fl
20439 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b ags |= MEM_Term;
2043a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
2043b 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 _OK;.}../*.** Ad
2043c 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68 65 d MEM_Str to the
2043d 20 73 65 74 20 6f 66 20 72 65 70 72 65 73 65 6e set of represen
2043e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 tations for the
2043f 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 given Mem. Numb
20440 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 ers.** are conve
20441 72 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 rted using sqlit
20442 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 e3_snprintf().
20443 43 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c 4f Converting a BLO
20444 42 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a 2a B to a string.**
20445 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
20446 2a 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70 72 ** Existing repr
20447 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d 5f esentations MEM_
20448 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c Int and MEM_Real
20449 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c are *not* inval
2044a 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 idated..**.** A
2044b 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 77 MEM_Null value w
2044c 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61 73 ill never be pas
2044d 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 sed to this func
2044e 74 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63 74 tion. This funct
2044f 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20 66 ion is.** used f
20450 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76 61 or converting va
20451 6c 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f 72 lues to text for
20452 20 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74 68 returning to th
20453 65 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69 61 e user (i.e. via
20454 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 .** sqlite3_valu
20455 65 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66 6f e_text()), or fo
20456 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20 r ensuring that
20457 76 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73 65 values to be use
20458 64 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b 65 d as btree.** ke
20459 79 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e 20 ys are strings.
2045a 49 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63 61 In the former ca
2045b 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 se a NULL pointe
2045c 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 68 r is returned th
2045d 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74 68 e.** user and th
2045e 65 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69 6e e later is an in
2045f 74 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d 69 ternal programmi
20460 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c ng error..*/.SQL
20461 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
20462 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 sqlite3VdbeMemSt
20463 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d 65 ringify(Mem *pMe
20464 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20 69 m, int enc){. i
20465 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
20466 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70 4d K;. int fg = pM
20467 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6e em->flags;. con
20468 73 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 33 st int nByte = 3
20469 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 2;.. assert( pM
2046a 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
2046b 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
2046c 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
2046d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28 );. assert( !(
2046e 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a fg&MEM_Zero) );.
2046f 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 28 assert( !(fg&(
20470 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 MEM_Str|MEM_Blob
20471 29 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 )) );. assert(
20472 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f fg&(MEM_Int|MEM_
20473 52 65 61 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 Real) );. asser
20474 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 t( (pMem->flags&
20475 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
20476 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 ;. assert( EIGH
20477 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 T_BYTE_ALIGNMENT
20478 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 20 69 66 (pMem) );... if
20479 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d ( sqlite3VdbeMem
2047a 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 Grow(pMem, nByte
2047b 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 , 0) ){. retu
2047c 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
2047d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 . }.. /* For a
2047e 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65 72 Real or Integer
2047f 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 70 , use sqlite3_mp
20480 72 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64 75 rintf() to produ
20481 63 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20 2a ce the UTF-8. *
20482 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 * string represe
20483 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 ntation of the v
20484 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20 74 alue. Then, if t
20485 68 65 20 72 65 71 75 69 72 65 64 20 65 6e 63 6f he required enco
20486 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54 46 ding. ** is UTF
20487 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36 62 -16le or UTF-16b
20488 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74 69 e do a translati
20489 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 46 on.. ** . ** F
2048a 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64 20 IX ME: It would
2048b 62 65 20 62 65 74 74 65 72 20 69 66 20 73 71 6c be better if sql
2048c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 ite3_snprintf()
2048d 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36 2e could do UTF-16.
2048e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20 26 . */. if( fg &
2048f 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 MEM_Int ){.
20490 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
20491 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c (nByte, pMem->z,
20492 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 "%lld", pMem->u
20493 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 .i);. }else{.
20494 20 20 61 73 73 65 72 74 28 20 66 67 20 26 20 4d assert( fg & M
20495 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 73 EM_Real );. s
20496 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
20497 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 nByte, pMem->z,
20498 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d 3e "%!.15g", pMem->
20499 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e r);. }. pMem->
2049a 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
2049b 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 n30(pMem->z);.
2049c 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pMem->enc = SQLI
2049d 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d 2d TE_UTF8;. pMem-
2049e 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 >flags |= MEM_St
2049f 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 71 r|MEM_Term;. sq
204a0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
204a1 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e ncoding(pMem, en
204a2 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b c);. return rc;
204a3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 .}../*.** Memory
204a4 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61 cell pMem conta
204a5 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20 ins the context
204a6 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 of an aggregate
204a7 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 function..** Thi
204a8 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 s routine calls
204a9 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74 the finalize met
204aa 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e hod for that fun
204ab 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 72 ction. The.** r
204ac 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67 esult of the agg
204ad 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64 regate is stored
204ae 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e back into pMem.
204af 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 .**.** Return SQ
204b0 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68 LITE_ERROR if th
204b1 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 6f e finalizer repo
204b2 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 rts an error. S
204b3 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65 QLITE_OK.** othe
204b4 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rwise..*/.SQLITE
204b5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
204b6 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c ite3VdbeMemFinal
204b7 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46 ize(Mem *pMem, F
204b8 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b 0a uncDef *pFunc){.
204b9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
204ba 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57 41 E_OK;. if( ALWA
204bb 59 53 28 70 46 75 6e 63 20 26 26 20 70 46 75 6e YS(pFunc && pFun
204bc 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 29 20 29 7b c->xFinalize) ){
204bd 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e . sqlite3_con
204be 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 61 73 text ctx;. as
204bf 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
204c0 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d gs & MEM_Null)!=
204c1 30 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 0 || pFunc==pMem
204c2 2d 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20 20 20 ->u.pDef );.
204c3 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
204c4 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
204c5 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
204c6 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
204c7 20 20 6d 65 6d 73 65 74 28 26 63 74 78 2c 20 30 memset(&ctx, 0
204c8 2c 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b 0a , sizeof(ctx));.
204c9 20 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73 20 ctx.s.flags
204ca 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 = MEM_Null;.
204cb 63 74 78 2e 73 2e 64 62 20 3d 20 70 4d 65 6d 2d ctx.s.db = pMem-
204cc 3e 64 62 3b 0a 20 20 20 20 63 74 78 2e 70 4d 65 >db;. ctx.pMe
204cd 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63 74 m = pMem;. ct
204ce 78 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63 3b x.pFunc = pFunc;
204cf 0a 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69 6e . pFunc->xFin
204d0 61 6c 69 7a 65 28 26 63 74 78 29 3b 20 2f 2a 20 alize(&ctx); /*
204d1 49 4d 50 3a 20 52 2d 32 34 35 30 35 2d 32 33 32 IMP: R-24505-232
204d2 33 30 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 30 */. assert
204d3 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67 ( 0==(pMem->flag
204d4 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26 20 21 70 s&MEM_Dyn) && !p
204d5 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20 Mem->xDel );.
204d6 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
204d7 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a Mem->db, pMem->z
204d8 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 6d 65 6d Malloc);. mem
204d9 63 70 79 28 70 4d 65 6d 2c 20 26 63 74 78 2e 73 cpy(pMem, &ctx.s
204da 2c 20 73 69 7a 65 6f 66 28 63 74 78 2e 73 29 29 , sizeof(ctx.s))
204db 3b 0a 20 20 20 20 72 63 20 3d 20 63 74 78 2e 69 ;. rc = ctx.i
204dc 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 sError;. }. re
204dd 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
204de 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 * If the memory
204df 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 61 20 cell contains a
204e0 73 74 72 69 6e 67 20 76 61 6c 75 65 20 74 68 61 string value tha
204e1 74 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 t must be freed
204e2 62 79 0a 2a 2a 20 69 6e 76 6f 6b 69 6e 67 20 61 by.** invoking a
204e3 6e 20 65 78 74 65 72 6e 61 6c 20 63 61 6c 6c 62 n external callb
204e4 61 63 6b 2c 20 66 72 65 65 20 69 74 20 6e 6f 77 ack, free it now
204e5 2e 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 . Calling this f
204e6 75 6e 63 74 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 unction.** does
204e7 6e 6f 74 20 66 72 65 65 20 61 6e 79 20 4d 65 6d not free any Mem
204e8 2e 7a 4d 61 6c 6c 6f 63 20 62 75 66 66 65 72 2e .zMalloc buffer.
204e9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
204ea 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
204eb 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 dbeMemReleaseExt
204ec 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 7b 0a 20 ernal(Mem *p){.
204ed 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 3d 3d assert( p->db==
204ee 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
204ef 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
204f0 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
204f1 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 ->flags&MEM_Agg
204f2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
204f3 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c beMemFinalize(p,
204f4 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20 p->u.pDef);.
204f5 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 assert( (p->fla
204f6 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 gs & MEM_Agg)==0
204f7 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 );. sqlite3V
204f8 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 29 dbeMemRelease(p)
204f9 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d ;. }else if( p-
204fa 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 20 26 >flags&MEM_Dyn &
204fb 26 20 70 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 & p->xDel ){.
204fc 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 assert( (p->fla
204fd 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d gs&MEM_RowSet)==
204fe 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
204ff 20 70 2d 3e 78 44 65 6c 21 3d 53 51 4c 49 54 45 p->xDel!=SQLITE
20500 5f 44 59 4e 41 4d 49 43 20 29 3b 0a 20 20 20 20 _DYNAMIC );.
20501 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 p->xDel((void *)
20502 70 2d 3e 7a 29 3b 0a 20 20 20 20 70 2d 3e 78 44 p->z);. p->xD
20503 65 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 el = 0;. }else
20504 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d if( p->flags&MEM
20505 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 73 _RowSet ){. s
20506 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 qlite3RowSetClea
20507 72 28 70 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b r(p->u.pRowSet);
20508 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e . }else if( p->
20509 66 6c 61 67 73 26 4d 45 4d 5f 46 72 61 6d 65 20 flags&MEM_Frame
2050a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
2050b 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 29 3b beMemSetNull(p);
2050c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
2050d 6c 65 61 73 65 20 61 6e 79 20 6d 65 6d 6f 72 79 lease any memory
2050e 20 68 65 6c 64 20 62 79 20 74 68 65 20 4d 65 6d held by the Mem
2050f 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 76 65 . This may leave
20510 20 74 68 65 20 4d 65 6d 20 69 6e 20 61 6e 0a 2a the Mem in an.*
20511 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 * inconsistent s
20512 74 61 74 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c tate, for exampl
20513 65 20 77 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d 30 e with (Mem.z==0
20514 29 20 61 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74 79 ) and.** (Mem.ty
20515 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 29 pe==SQLITE_TEXT)
20516 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20517 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
20518 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d VdbeMemRelease(M
20519 65 6d 20 2a 70 29 7b 0a 20 20 56 64 62 65 4d 65 em *p){. VdbeMe
2051a 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a 20 20 73 mRelease(p);. s
2051b 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e qlite3DbFree(p->
2051c 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b db, p->zMalloc);
2051d 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a 20 20 70 . p->z = 0;. p
2051e 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 ->zMalloc = 0;.
2051f 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 7d 0a p->xDel = 0;.}.
20520 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 ./*.** Convert a
20521 20 36 34 2d 62 69 74 20 49 45 45 45 20 64 6f 75 64-bit IEEE dou
20522 62 6c 65 20 69 6e 74 6f 20 61 20 36 34 2d 62 69 ble into a 64-bi
20523 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
20524 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f 75 62 ..** If the doub
20525 6c 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c le is too large,
20526 20 72 65 74 75 72 6e 20 30 78 38 30 30 30 30 30 return 0x800000
20527 30 30 30 30 30 30 30 30 30 30 2e 0a 2a 2a 0a 2a 0000000000..**.*
20528 2a 20 4d 6f 73 74 20 73 79 73 74 65 6d 73 20 61 * Most systems a
20529 70 70 65 61 72 20 74 6f 20 64 6f 20 74 68 69 73 ppear to do this
2052a 20 73 69 6d 70 6c 79 20 62 79 20 61 73 73 69 67 simply by assig
2052b 6e 69 6e 67 0a 2a 2a 20 76 61 72 69 61 62 6c 65 ning.** variable
2052c 73 20 61 6e 64 20 77 69 74 68 6f 75 74 20 74 68 s and without th
2052d 65 20 65 78 74 72 61 20 72 61 6e 67 65 20 74 65 e extra range te
2052e 73 74 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 sts. But.** the
2052f 72 65 20 61 72 65 20 72 65 70 6f 72 74 73 20 74 re are reports t
20530 68 61 74 20 77 69 6e 64 6f 77 73 20 74 68 72 6f hat windows thro
20531 77 73 20 61 6e 20 65 78 70 65 63 74 69 6f 6e 0a ws an expection.
20532 2a 2a 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 ** if the floati
20533 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 ng point value i
20534 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 s out of range.
20535 28 53 65 65 20 74 69 63 6b 65 74 20 23 32 38 38 (See ticket #288
20536 30 2e 29 0a 2a 2a 20 42 65 63 61 75 73 65 20 77 0.).** Because w
20537 65 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 e do not complet
20538 65 6c 79 20 75 6e 64 65 72 73 74 61 6e 64 20 74 ely understand t
20539 68 65 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20 77 he problem, we w
2053a 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 ill.** take the
2053b 63 6f 6e 73 65 72 76 61 74 69 76 65 20 61 70 70 conservative app
2053c 72 6f 61 63 68 20 61 6e 64 20 61 6c 77 61 79 73 roach and always
2053d 20 64 6f 20 72 61 6e 67 65 20 74 65 73 74 73 0a do range tests.
2053e 2a 2a 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 ** before attemp
2053f 74 69 6e 67 20 74 68 65 20 63 6f 6e 76 65 72 73 ting the convers
20540 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ion..*/.static i
20541 36 34 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 64 doubleToInt64
20542 28 64 6f 75 62 6c 65 20 72 29 7b 0a 23 69 66 64 (double r){.#ifd
20543 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
20544 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 LOATING_POINT.
20545 2f 2a 20 57 68 65 6e 20 66 6c 6f 61 74 69 6e 67 /* When floating
20546 2d 70 6f 69 6e 74 20 69 73 20 6f 6d 69 74 74 65 -point is omitte
20547 64 2c 20 64 6f 75 62 6c 65 20 61 6e 64 20 69 6e d, double and in
20548 74 36 34 20 61 72 65 20 74 68 65 20 73 61 6d 65 t64 are the same
20549 20 74 68 69 6e 67 20 2a 2f 0a 20 20 72 65 74 75 thing */. retu
2054a 72 6e 20 72 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a rn r;.#else. /*
2054b 0a 20 20 2a 2a 20 4d 61 6e 79 20 63 6f 6d 70 69 . ** Many compi
2054c 6c 65 72 73 20 77 65 20 65 6e 63 6f 75 6e 74 65 lers we encounte
2054d 72 20 64 6f 20 6e 6f 74 20 64 65 66 69 6e 65 20 r do not define
2054e 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68 constants for th
2054f 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75 6d 20 61 e. ** minimum a
20550 6e 64 20 6d 61 78 69 6d 75 6d 20 36 34 2d 62 69 nd maximum 64-bi
20551 74 20 69 6e 74 65 67 65 72 73 2c 20 6f 72 20 74 t integers, or t
20552 68 65 79 20 64 65 66 69 6e 65 20 74 68 65 6d 0a hey define them.
20553 20 20 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e ** inconsisten
20554 74 6c 79 2e 20 20 41 6e 64 20 6d 61 6e 79 20 64 tly. And many d
20555 6f 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 o not understand
20556 20 74 68 65 20 22 4c 4c 22 20 6e 6f 74 61 74 69 the "LL" notati
20557 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77 65 20 64 on.. ** So we d
20558 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e 20 73 74 efine our own st
20559 61 74 69 63 20 63 6f 6e 73 74 61 6e 74 73 20 68 atic constants h
2055a 65 72 65 20 75 73 69 6e 67 20 6e 6f 74 68 69 6e ere using nothin
2055b 67 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 g. ** larger th
2055c 61 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 an a 32-bit inte
2055d 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e 0a 20 20 ger constant..
2055e 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 */. static cons
2055f 74 20 69 36 34 20 6d 61 78 49 6e 74 20 3d 20 4c t i64 maxInt = L
20560 41 52 47 45 53 54 5f 49 4e 54 36 34 3b 0a 20 20 ARGEST_INT64;.
20561 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36 34 static const i64
20562 20 6d 69 6e 49 6e 74 20 3d 20 53 4d 41 4c 4c 45 minInt = SMALLE
20563 53 54 5f 49 4e 54 36 34 3b 0a 0a 20 20 69 66 28 ST_INT64;.. if(
20564 20 72 3c 28 64 6f 75 62 6c 65 29 6d 69 6e 49 6e r<(double)minIn
20565 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
20566 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 minInt;. }else
20567 69 66 28 20 72 3e 28 64 6f 75 62 6c 65 29 6d 61 if( r>(double)ma
20568 78 49 6e 74 20 29 7b 0a 20 20 20 20 2f 2a 20 6d xInt ){. /* m
20569 69 6e 49 6e 74 20 69 73 20 63 6f 72 72 65 63 74 inInt is correct
2056a 20 68 65 72 65 20 2d 20 6e 6f 74 20 6d 61 78 49 here - not maxI
2056b 6e 74 2e 20 20 49 74 20 74 75 72 6e 73 20 6f 75 nt. It turns ou
2056c 74 20 74 68 61 74 20 61 73 73 69 67 6e 69 6e 67 t that assigning
2056d 0a 20 20 20 20 2a 2a 20 61 20 76 65 72 79 20 6c . ** a very l
2056e 61 72 67 65 20 70 6f 73 69 74 69 76 65 20 6e 75 arge positive nu
2056f 6d 62 65 72 20 74 6f 20 61 6e 20 69 6e 74 65 67 mber to an integ
20570 65 72 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 er results in a
20571 76 65 72 79 20 6c 61 72 67 65 0a 20 20 20 20 2a very large. *
20572 2a 20 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67 * negative integ
20573 65 72 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 er. This makes
20574 6e 6f 20 73 65 6e 73 65 2c 20 62 75 74 20 69 74 no sense, but it
20575 20 69 73 20 77 68 61 74 20 78 38 36 20 68 61 72 is what x86 har
20576 64 77 61 72 65 0a 20 20 20 20 2a 2a 20 64 6f 65 dware. ** doe
20577 73 20 73 6f 20 66 6f 72 20 63 6f 6d 70 61 74 69 s so for compati
20578 62 69 6c 69 74 79 20 77 65 20 77 69 6c 6c 20 64 bility we will d
20579 6f 20 74 68 65 20 73 61 6d 65 20 69 6e 20 73 6f o the same in so
2057a 66 74 77 61 72 65 2e 20 2a 2f 0a 20 20 20 20 72 ftware. */. r
2057b 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 eturn minInt;.
2057c 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 }else{. retur
2057d 6e 20 28 69 36 34 29 72 3b 0a 20 20 7d 0a 23 65 n (i64)r;. }.#e
2057e 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ndif.}../*.** Re
2057f 74 75 72 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f turn some kind o
20580 66 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 f integer value
20581 77 68 69 63 68 20 69 73 20 74 68 65 20 62 65 73 which is the bes
20582 74 20 77 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 t we can do.** a
20583 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 t representing t
20584 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 2a 70 he value that *p
20585 4d 65 6d 20 64 65 73 63 72 69 62 65 73 20 61 73 Mem describes as
20586 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 an integer..**
20587 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e If pMem is an in
20588 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 teger, then the
20589 76 61 6c 75 65 20 69 73 20 65 78 61 63 74 2e 20 value is exact.
2058a 20 49 66 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 If pMem is.** a
2058b 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 floating-point
2058c 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 then the value r
2058d 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 69 eturned is the i
2058e 6e 74 65 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20 nteger part..**
2058f 49 66 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72 If pMem is a str
20590 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 ing or blob, the
20591 6e 20 77 65 20 6d 61 6b 65 20 61 6e 20 61 74 74 n we make an att
20592 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a empt to convert.
20593 2a 2a 20 69 74 20 69 6e 74 6f 20 61 20 69 6e 74 ** it into a int
20594 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 eger and return
20595 74 68 61 74 2e 20 20 49 66 20 70 4d 65 6d 20 72 that. If pMem r
20596 65 70 72 65 73 65 6e 74 73 20 61 6e 0a 2a 2a 20 epresents an.**
20597 61 6e 20 53 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75 an SQL-NULL valu
20598 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a e, return 0..**.
20599 2a 2a 20 49 66 20 70 4d 65 6d 20 72 65 70 72 65 ** If pMem repre
2059a 73 65 6e 74 73 20 61 20 73 74 72 69 6e 67 20 76 sents a string v
2059b 61 6c 75 65 2c 20 69 74 73 20 65 6e 63 6f 64 69 alue, its encodi
2059c 6e 67 20 6d 69 67 68 74 20 62 65 20 63 68 61 6e ng might be chan
2059d 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ged..*/.SQLITE_P
2059e 52 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 RIVATE i64 sqlit
2059f 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d e3VdbeIntValue(M
205a0 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 em *pMem){. int
205a1 20 66 6c 61 67 73 3b 0a 20 20 61 73 73 65 72 74 flags;. assert
205a2 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
205a3 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
205a4 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
205a5 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
205a6 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 ( EIGHT_BYTE_ALI
205a7 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a GNMENT(pMem) );.
205a8 20 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e flags = pMem->
205a9 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 flags;. if( fla
205aa 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a gs & MEM_Int ){.
205ab 20 20 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d return pMem-
205ac 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 >u.i;. }else if
205ad 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 ( flags & MEM_Re
205ae 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e al ){. return
205af 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 doubleToInt64(p
205b0 4d 65 6d 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 Mem->r);. }else
205b1 20 69 66 28 20 66 6c 61 67 73 20 26 20 28 4d 45 if( flags & (ME
205b2 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 M_Str|MEM_Blob)
205b3 29 7b 0a 20 20 20 20 69 36 34 20 76 61 6c 75 65 ){. i64 value
205b4 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 = 0;. assert
205b5 28 20 70 4d 65 6d 2d 3e 7a 20 7c 7c 20 70 4d 65 ( pMem->z || pMe
205b6 6d 2d 3e 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 74 m->n==0 );. t
205b7 65 73 74 63 61 73 65 28 20 70 4d 65 6d 2d 3e 7a estcase( pMem->z
205b8 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ==0 );. sqlit
205b9 65 33 41 74 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a e3Atoi64(pMem->z
205ba 2c 20 26 76 61 6c 75 65 2c 20 70 4d 65 6d 2d 3e , &value, pMem->
205bb 6e 2c 20 70 4d 65 6d 2d 3e 65 6e 63 29 3b 0a 20 n, pMem->enc);.
205bc 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b return value;
205bd 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
205be 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f turn 0;. }.}../
205bf 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
205c0 62 65 73 74 20 72 65 70 72 65 73 65 6e 74 61 74 best representat
205c1 69 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74 ion of pMem that
205c2 20 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f we can get into
205c3 20 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49 a.** double. I
205c4 66 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64 f pMem is alread
205c5 79 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e y a double or an
205c6 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e integer, return
205c7 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 its.** value.
205c8 49 66 20 69 74 20 69 73 20 61 20 73 74 72 69 6e If it is a strin
205c9 67 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 g or blob, try t
205ca 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 o convert it to
205cb 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20 a double..** If
205cc 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65 it is a NULL, re
205cd 74 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c turn 0.0..*/.SQL
205ce 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 ITE_PRIVATE doub
205cf 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 le sqlite3VdbeRe
205d0 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 alValue(Mem *pMe
205d1 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d m){. assert( pM
205d2 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
205d3 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
205d4 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
205d5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 );. assert( EI
205d6 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
205d7 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 69 66 NT(pMem) );. if
205d8 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ( pMem->flags &
205d9 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
205da 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a return pMem->r;.
205db 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d }else if( pMem
205dc 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
205dd 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
205de 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e (double)pMem->u.
205df 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 i;. }else if( p
205e0 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 Mem->flags & (ME
205e1 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 M_Str|MEM_Blob)
205e2 29 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c ){. /* (doubl
205e3 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 e)0 In case of S
205e4 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
205e5 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a ING_POINT... */.
205e6 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d double val =
205e7 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 (double)0;.
205e8 73 71 6c 69 74 65 33 41 74 6f 46 28 70 4d 65 6d sqlite3AtoF(pMem
205e9 2d 3e 7a 2c 20 26 76 61 6c 2c 20 70 4d 65 6d 2d ->z, &val, pMem-
205ea 3e 6e 2c 20 70 4d 65 6d 2d 3e 65 6e 63 29 3b 0a >n, pMem->enc);.
205eb 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a return val;.
205ec 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
205ed 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 (double)0 In cas
205ee 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 e of SQLITE_OMIT
205ef 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e _FLOATING_POINT.
205f0 2e 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e .. */. return
205f1 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a (double)0;. }.
205f2 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d }../*.** The MEM
205f3 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c structure is al
205f4 72 65 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c ready a MEM_Real
205f5 2e 20 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d . Try to also m
205f6 61 6b 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f ake it a.** MEM_
205f7 49 6e 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a Int if we can..*
205f8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
205f9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
205fa 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 eIntegerAffinity
205fb 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 (Mem *pMem){. a
205fc 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 ssert( pMem->fla
205fd 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b gs & MEM_Real );
205fe 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
205ff 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
20600 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 wSet)==0 );. as
20601 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d sert( pMem->db==
20602 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
20603 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 ex_held(pMem->db
20604 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
20605 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 sert( EIGHT_BYTE
20606 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 _ALIGNMENT(pMem)
20607 20 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 );.. pMem->u.i
20608 20 3d 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 = doubleToInt64
20609 28 70 4d 65 6d 2d 3e 72 29 3b 0a 0a 20 20 2f 2a (pMem->r);.. /*
2060a 20 4f 6e 6c 79 20 6d 61 72 6b 20 74 68 65 20 76 Only mark the v
2060b 61 6c 75 65 20 61 73 20 61 6e 20 69 6e 74 65 67 alue as an integ
2060c 65 72 20 69 66 0a 20 20 2a 2a 0a 20 20 2a 2a 20 er if. **. **
2060d 20 20 20 28 31 29 20 74 68 65 20 72 6f 75 6e 64 (1) the round
2060e 2d 74 72 69 70 20 63 6f 6e 76 65 72 73 69 6f 6e -trip conversion
2060f 20 72 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65 61 6c real->int->real
20610 20 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 61 6e 64 is a no-op, and
20611 0a 20 20 2a 2a 20 20 20 20 28 32 29 20 54 68 65 . ** (2) The
20612 20 69 6e 74 65 67 65 72 20 69 73 20 6e 65 69 74 integer is neit
20613 68 65 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 her the largest
20614 6e 6f 72 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 nor the smallest
20615 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 70 6f 73 . ** pos
20616 73 69 62 6c 65 20 69 6e 74 65 67 65 72 20 28 74 sible integer (t
20617 69 63 6b 65 74 20 23 33 39 32 32 29 0a 20 20 2a icket #3922). *
20618 2a 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e *. ** The secon
20619 64 20 61 6e 64 20 74 68 69 72 64 20 74 65 72 6d d and third term
2061a 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 s in the followi
2061b 6e 67 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65 ng conditional e
2061c 6e 66 6f 72 63 65 73 0a 20 20 2a 2a 20 74 68 65 nforces. ** the
2061d 20 73 65 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f second conditio
2061e 6e 20 75 6e 64 65 72 20 74 68 65 20 61 73 73 75 n under the assu
2061f 6d 70 74 69 6f 6e 20 74 68 61 74 20 61 64 64 69 mption that addi
20620 74 69 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 63 61 tion overflow ca
20621 75 73 65 73 0a 20 20 2a 2a 20 76 61 6c 75 65 73 uses. ** values
20622 20 74 6f 20 77 72 61 70 20 61 72 6f 75 6e 64 2e to wrap around.
20623 20 20 4f 6e 20 78 38 36 20 68 61 72 64 77 61 72 On x86 hardwar
20624 65 2c 20 74 68 65 20 74 68 69 72 64 20 74 65 72 e, the third ter
20625 6d 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a m is always. **
20626 20 74 72 75 65 20 61 6e 64 20 63 6f 75 6c 64 20 true and could
20627 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 be omitted. But
20628 20 77 65 20 6c 65 61 76 65 20 69 74 20 69 6e 20 we leave it in
20629 62 65 63 61 75 73 65 20 6f 74 68 65 72 0a 20 20 because other.
2062a 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72 65 73 ** architectures
2062b 20 6d 69 67 68 74 20 62 65 68 61 76 65 20 64 69 might behave di
2062c 66 66 65 72 65 6e 74 6c 79 2e 0a 20 20 2a 2f 0a fferently.. */.
2062d 20 20 69 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28 if( pMem->r==(
2062e 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 double)pMem->u.i
2062f 0a 20 20 20 26 26 20 70 4d 65 6d 2d 3e 75 2e 69 . && pMem->u.i
20630 3e 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 0a >SMALLEST_INT64.
20631 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 69 34 #if defined(__i4
20632 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 86__) || defined
20633 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 0a 20 20 20 (__x86_64__).
20634 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d 2d 3e && ALWAYS(pMem->
20635 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e 54 36 u.i<LARGEST_INT6
20636 34 29 0a 23 65 6c 73 65 0a 20 20 20 26 26 20 70 4).#else. && p
20637 4d 65 6d 2d 3e 75 2e 69 3c 4c 41 52 47 45 53 54 Mem->u.i<LARGEST
20638 5f 49 4e 54 36 34 0a 23 65 6e 64 69 66 0a 20 20 _INT64.#endif.
20639 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 ){. pMem->fla
2063a 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 gs |= MEM_Int;.
2063b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 }.}../*.** Conv
2063c 65 72 74 20 70 4d 65 6d 20 74 6f 20 74 79 70 65 ert pMem to type
2063d 20 69 6e 74 65 67 65 72 2e 20 20 49 6e 76 61 6c integer. Inval
2063e 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 idate any prior
2063f 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e representations.
20640 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20641 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
20642 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 beMemIntegerify(
20643 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 Mem *pMem){. as
20644 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d sert( pMem->db==
20645 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
20646 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 ex_held(pMem->db
20647 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
20648 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
20649 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
2064a 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
2064b 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
2064c 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a NMENT(pMem) );..
2064d 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 71 pMem->u.i = sq
2064e 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
2064f 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 e(pMem);. MemSe
20650 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 tTypeFlag(pMem,
20651 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 72 65 74 75 MEM_Int);. retu
20652 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
20653 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 ./*.** Convert p
20654 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 69 Mem so that it i
20655 73 20 6f 66 20 74 79 70 65 20 4d 45 4d 5f 52 65 s of type MEM_Re
20656 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 al..** Invalidat
20657 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72 e any prior repr
20658 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a esentations..*/.
20659 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
2065a 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
2065b 6d 52 65 61 6c 69 66 79 28 4d 65 6d 20 2a 70 4d mRealify(Mem *pM
2065c 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 em){. assert( p
2065d 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
2065e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
2065f 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
20660 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 ) );. assert( E
20661 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d IGHT_BYTE_ALIGNM
20662 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20 20 ENT(pMem) );..
20663 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65 pMem->r = sqlite
20664 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 3VdbeRealValue(p
20665 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 Mem);. MemSetTy
20666 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d peFlag(pMem, MEM
20667 5f 52 65 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e _Real);. return
20668 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
20669 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65 *.** Convert pMe
2066a 6d 20 73 6f 20 74 68 61 74 20 69 74 20 68 61 73 m so that it has
2066b 20 74 79 70 65 73 20 4d 45 4d 5f 52 65 61 6c 20 types MEM_Real
2066c 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72 20 62 6f or MEM_Int or bo
2066d 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 th..** Invalidat
2066e 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72 e any prior repr
2066f 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a esentations..**.
20670 2a 2a 20 45 76 65 72 79 20 65 66 66 6f 72 74 20 ** Every effort
20671 69 73 20 6d 61 64 65 20 74 6f 20 66 6f 72 63 65 is made to force
20672 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2c the conversion,
20673 20 65 76 65 6e 20 69 66 20 74 68 65 20 69 6e 70 even if the inp
20674 75 74 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e ut.** is a strin
20675 67 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 g that does not
20676 6c 6f 6f 6b 20 63 6f 6d 70 6c 65 74 65 6c 79 20 look completely
20677 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 2e 20 20 like a number.
20678 43 6f 6e 76 65 72 74 0a 2a 2a 20 61 73 20 6d 75 Convert.** as mu
20679 63 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 ch of the string
2067a 20 61 73 20 77 65 20 63 61 6e 20 61 6e 64 20 69 as we can and i
2067b 67 6e 6f 72 65 20 74 68 65 20 72 65 73 74 2e 0a gnore the rest..
2067c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
2067d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
2067e 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d eMemNumerify(Mem
2067f 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 28 *pMem){. if( (
20680 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d pMem->flags & (M
20681 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c EM_Int|MEM_Real|
20682 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 7b MEM_Null))==0 ){
20683 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4d . assert( (pM
20684 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d em->flags & (MEM
20685 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 _Blob|MEM_Str))!
20686 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
20687 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
20688 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
20689 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
2068a 74 65 78 29 20 29 3b 0a 20 20 20 20 69 66 28 20 tex) );. if(
2068b 30 3d 3d 73 71 6c 69 74 65 33 41 74 6f 69 36 34 0==sqlite3Atoi64
2068c 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d (pMem->z, &pMem-
2068d 3e 75 2e 69 2c 20 70 4d 65 6d 2d 3e 6e 2c 20 70 >u.i, pMem->n, p
2068e 4d 65 6d 2d 3e 65 6e 63 29 20 29 7b 0a 20 20 20 Mem->enc) ){.
2068f 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
20690 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 g(pMem, MEM_Int)
20691 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
20692 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c pMem->r = sql
20693 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 ite3VdbeRealValu
20694 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 4d e(pMem);. M
20695 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d emSetTypeFlag(pM
20696 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 em, MEM_Real);.
20697 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
20698 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 IntegerAffinity(
20699 70 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d pMem);. }. }
2069a 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
2069b 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 ->flags & (MEM_I
2069c 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f nt|MEM_Real|MEM_
2069d 4e 75 6c 6c 29 29 21 3d 30 20 29 3b 0a 20 20 70 Null))!=0 );. p
2069e 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 Mem->flags &= ~(
2069f 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 MEM_Str|MEM_Blob
206a0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
206a1 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
206a2 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 Delete any previ
206a3 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 ous value and se
206a4 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 t the value stor
206a5 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 4e ed in *pMem to N
206a6 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ULL..*/.SQLITE_P
206a7 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
206a8 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c te3VdbeMemSetNul
206a9 6c 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 l(Mem *pMem){.
206aa 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 if( pMem->flags
206ab 26 20 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a 20 & MEM_Frame ){.
206ac 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 VdbeFrame *pF
206ad 72 61 6d 65 20 3d 20 70 4d 65 6d 2d 3e 75 2e 70 rame = pMem->u.p
206ae 46 72 61 6d 65 3b 0a 20 20 20 20 70 46 72 61 6d Frame;. pFram
206af 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 46 72 e->pParent = pFr
206b0 61 6d 65 2d 3e 76 2d 3e 70 44 65 6c 46 72 61 6d ame->v->pDelFram
206b1 65 3b 0a 20 20 20 20 70 46 72 61 6d 65 2d 3e 76 e;. pFrame->v
206b2 2d 3e 70 44 65 6c 46 72 61 6d 65 20 3d 20 70 46 ->pDelFrame = pF
206b3 72 61 6d 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rame;. }. if(
206b4 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
206b5 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 M_RowSet ){.
206b6 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 sqlite3RowSetCle
206b7 61 72 28 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 ar(pMem->u.pRowS
206b8 65 74 29 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 et);. }. MemSe
206b9 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 tTypeFlag(pMem,
206ba 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 70 4d 65 MEM_Null);. pMe
206bb 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
206bc 5f 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 _NULL;.}../*.**
206bd 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 Delete any previ
206be 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 ous value and se
206bf 74 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 t the value to b
206c0 65 20 61 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 e a BLOB of leng
206c1 74 68 0a 2a 2a 20 6e 20 63 6f 6e 74 61 69 6e 69 th.** n containi
206c2 6e 67 20 61 6c 6c 20 7a 65 72 6f 73 2e 0a 2a 2f ng all zeros..*/
206c3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
206c4 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
206c5 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d MemSetZeroBlob(M
206c6 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 29 em *pMem, int n)
206c7 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d {. sqlite3VdbeM
206c8 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b emRelease(pMem);
206c9 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d . pMem->flags =
206ca 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 MEM_Blob|MEM_Ze
206cb 72 6f 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 ro;. pMem->type
206cc 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a = SQLITE_BLOB;.
206cd 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 pMem->n = 0;.
206ce 20 69 66 28 20 6e 3c 30 20 29 20 6e 20 3d 20 30 if( n<0 ) n = 0
206cf 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 ;. pMem->u.nZer
206d0 6f 20 3d 20 6e 3b 0a 20 20 70 4d 65 6d 2d 3e 65 o = n;. pMem->e
206d1 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 nc = SQLITE_UTF8
206d2 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
206d3 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 _OMIT_INCRBLOB.
206d4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
206d5 72 6f 77 28 70 4d 65 6d 2c 20 6e 2c 20 30 29 3b row(pMem, n, 0);
206d6 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 29 . if( pMem->z )
206d7 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 {. pMem->n =
206d8 6e 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 4d n;. memset(pM
206d9 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 29 3b 0a 20 20 em->z, 0, n);.
206da 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a }.#endif.}../*.*
206db 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 * Delete any pre
206dc 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 vious value and
206dd 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 set the value st
206de 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f ored in *pMem to
206df 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 val,.** manifes
206e0 74 20 74 79 70 65 20 49 4e 54 45 47 45 52 2e 0a t type INTEGER..
206e1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
206e2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
206e3 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d 65 beMemSetInt64(Me
206e4 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 20 76 61 6c m *pMem, i64 val
206e5 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 ){. sqlite3Vdbe
206e6 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 MemRelease(pMem)
206e7 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 ;. pMem->u.i =
206e8 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 val;. pMem->fla
206e9 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
206ea 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
206eb 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 7d 0a 0a ITE_INTEGER;.}..
206ec 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
206ed 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
206ee 4e 54 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 NT./*.** Delete
206ef 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c any previous val
206f0 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 ue and set the v
206f1 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a alue stored in *
206f2 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 pMem to val,.**
206f3 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 52 45 manifest type RE
206f4 41 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 AL..*/.SQLITE_PR
206f5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
206f6 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 e3VdbeMemSetDoub
206f7 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 6f le(Mem *pMem, do
206f8 75 62 6c 65 20 76 61 6c 29 7b 0a 20 20 69 66 28 uble val){. if(
206f9 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 sqlite3IsNaN(va
206fa 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 l) ){. sqlite
206fb 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
206fc 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a pMem);. }else{.
206fd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
206fe 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b emRelease(pMem);
206ff 0a 20 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 76 . pMem->r = v
20700 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c al;. pMem->fl
20701 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a ags = MEM_Real;.
20702 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
20703 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 SQLITE_FLOAT;.
20704 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a }.}.#endif../*.
20705 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 ** Delete any pr
20706 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 evious value and
20707 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f set the value o
20708 66 20 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e 0a f pMem to be an.
20709 2a 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61 6e ** empty boolean
2070a 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 index..*/.SQLIT
2070b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
2070c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
2070d 52 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65 6d RowSet(Mem *pMem
2070e 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
2070f 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 61 = pMem->db;. a
20710 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a ssert( db!=0 );.
20711 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
20712 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
20713 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c Set)==0 );. sql
20714 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
20715 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d se(pMem);. pMem
20716 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 ->zMalloc = sqli
20717 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
20718 62 2c 20 36 34 29 3b 0a 20 20 69 66 28 20 64 62 b, 64);. if( db
20719 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
2071a 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 {. pMem->flag
2071b 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
2071c 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
2071d 74 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 t( pMem->zMalloc
2071e 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e );. pMem->u.
2071f 70 52 6f 77 53 65 74 20 3d 20 73 71 6c 69 74 65 pRowSet = sqlite
20720 33 52 6f 77 53 65 74 49 6e 69 74 28 64 62 2c 20 3RowSetInit(db,
20721 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a pMem->zMalloc, .
20722 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20723 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20724 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
20725 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70 MallocSize(db, p
20726 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0a Mem->zMalloc));.
20727 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
20728 2d 3e 75 2e 70 52 6f 77 53 65 74 21 3d 30 20 29 ->u.pRowSet!=0 )
20729 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 ;. pMem->flag
2072a 73 20 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b 0a s = MEM_RowSet;.
2072b 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
2072c 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 urn true if the
2072d 4d 65 6d 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 Mem object conta
2072e 69 6e 73 20 61 20 54 45 58 54 20 6f 72 20 42 4c ins a TEXT or BL
2072f 4f 42 20 74 68 61 74 20 69 73 0a 2a 2a 20 74 6f OB that is.** to
20730 6f 20 6c 61 72 67 65 20 2d 20 77 68 6f 73 65 20 o large - whose
20731 73 69 7a 65 20 65 78 63 65 65 64 73 20 53 51 4c size exceeds SQL
20732 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a ITE_MAX_LENGTH..
20733 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20734 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
20735 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 20 2a eMemTooBig(Mem *
20736 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p){. assert( p-
20737 3e 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 >db!=0 );. if(
20738 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f p->flags & (MEM_
20739 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b Str|MEM_Blob) ){
2073a 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e . int n = p->
2073b 6e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c n;. if( p->fl
2073c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 ags & MEM_Zero )
2073d 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d 3e {. n += p->
2073e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 0a 20 u.nZero;. }.
2073f 20 20 20 72 65 74 75 72 6e 20 6e 3e 70 2d 3e 64 return n>p->d
20740 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
20741 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a _LIMIT_LENGTH];.
20742 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 20 }. return 0;
20743 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .}..#ifdef SQLIT
20744 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 E_DEBUG./*.** Th
20745 69 73 20 72 6f 75 74 69 6e 65 20 70 72 65 70 61 is routine prepa
20746 72 65 73 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c res a memory cel
20747 6c 20 66 6f 72 20 6d 6f 64 69 63 61 74 69 6f 6e l for modication
20748 20 62 79 20 62 72 65 61 6b 69 6e 67 0a 2a 2a 20 by breaking.**
20749 69 74 73 20 6c 69 6e 6b 20 74 6f 20 61 20 73 68 its link to a sh
2074a 61 6c 6c 6f 77 20 63 6f 70 79 20 61 6e 64 20 62 allow copy and b
2074b 79 20 6d 61 72 6b 69 6e 67 20 61 6e 79 20 63 75 y marking any cu
2074c 72 72 65 6e 74 20 73 68 61 6c 6c 6f 77 0a 2a 2a rrent shallow.**
2074d 20 63 6f 70 69 65 73 20 6f 66 20 74 68 69 73 20 copies of this
2074e 63 65 6c 6c 20 61 73 20 69 6e 76 61 6c 69 64 2e cell as invalid.
2074f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 75 .**.** This is u
20750 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 sed for testing
20751 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e and debugging on
20752 6c 79 20 2d 20 74 6f 20 6d 61 6b 65 20 73 75 72 ly - to make sur
20753 65 20 73 68 61 6c 6c 6f 77 0a 2a 2a 20 63 6f 70 e shallow.** cop
20754 69 65 73 20 61 72 65 20 6e 6f 74 20 6d 69 73 75 ies are not misu
20755 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 sed..*/.SQLITE_P
20756 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
20757 74 65 33 56 64 62 65 4d 65 6d 41 62 6f 75 74 54 te3VdbeMemAboutT
20758 6f 43 68 61 6e 67 65 28 56 64 62 65 20 2a 70 56 oChange(Vdbe *pV
20759 64 62 65 2c 20 4d 65 6d 20 2a 70 4d 65 6d 29 7b dbe, Mem *pMem){
2075a 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 . int i;. Mem
2075b 2a 70 58 3b 0a 20 20 66 6f 72 28 69 3d 31 2c 20 *pX;. for(i=1,
2075c 70 58 3d 26 70 56 64 62 65 2d 3e 61 4d 65 6d 5b pX=&pVdbe->aMem[
2075d 31 5d 3b 20 69 3c 3d 70 56 64 62 65 2d 3e 6e 4d 1]; i<=pVdbe->nM
2075e 65 6d 3b 20 69 2b 2b 2c 20 70 58 2b 2b 29 7b 0a em; i++, pX++){.
2075f 20 20 20 20 69 66 28 20 70 58 2d 3e 70 53 63 6f if( pX->pSco
20760 70 79 46 72 6f 6d 3d 3d 70 4d 65 6d 20 29 7b 0a pyFrom==pMem ){.
20761 20 20 20 20 20 20 70 58 2d 3e 66 6c 61 67 73 20 pX->flags
20762 7c 3d 20 4d 45 4d 5f 49 6e 76 61 6c 69 64 3b 0a |= MEM_Invalid;.
20763 20 20 20 20 20 20 70 58 2d 3e 70 53 63 6f 70 79 pX->pScopy
20764 46 72 6f 6d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a From = 0;. }.
20765 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 70 53 63 6f }. pMem->pSco
20766 70 79 46 72 6f 6d 20 3d 20 30 3b 0a 7d 0a 23 65 pyFrom = 0;.}.#e
20767 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 ndif /* SQLITE_D
20768 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 EBUG */../*.** S
20769 69 7a 65 20 6f 66 20 73 74 72 75 63 74 20 4d 65 ize of struct Me
2076a 6d 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 m not including
2076b 74 68 65 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 the Mem.zMalloc
2076c 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 member..*/.#defi
2076d 6e 65 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 20 28 ne MEMCELLSIZE (
2076e 73 69 7a 65 5f 74 29 28 26 28 28 28 4d 65 6d 20 size_t)(&(((Mem
2076f 2a 29 30 29 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 0a *)0)->zMalloc)).
20770 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 6e 20 73 ./*.** Make an s
20771 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20 70 hallow copy of p
20772 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20 From into pTo.
20773 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f Prior contents o
20774 66 0a 2a 2a 20 70 54 6f 20 61 72 65 20 66 72 65 f.** pTo are fre
20775 65 64 2e 20 20 54 68 65 20 70 46 72 6f 6d 2d 3e ed. The pFrom->
20776 7a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 64 z field is not d
20777 75 70 6c 69 63 61 74 65 64 2e 20 20 49 66 0a 2a uplicated. If.*
20778 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69 73 20 75 73 * pFrom->z is us
20779 65 64 2c 20 74 68 65 6e 20 70 54 6f 2d 3e 7a 20 ed, then pTo->z
2077a 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61 points to the sa
2077b 6d 65 20 74 68 69 6e 67 20 61 73 20 70 46 72 6f me thing as pFro
2077c 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20 66 6c 61 67 m->z.** and flag
2077d 73 20 67 65 74 73 20 73 72 63 54 79 70 65 20 28 s gets srcType (
2077e 65 69 74 68 65 72 20 4d 45 4d 5f 45 70 68 65 6d either MEM_Ephem
2077f 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 2e or MEM_Static).
20780 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20781 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
20782 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 dbeMemShallowCop
20783 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 y(Mem *pTo, cons
20784 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 2c 20 69 6e t Mem *pFrom, in
20785 74 20 73 72 63 54 79 70 65 29 7b 0a 20 20 61 73 t srcType){. as
20786 73 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e 66 6c sert( (pFrom->fl
20787 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 ags & MEM_RowSet
20788 29 3d 3d 30 20 29 3b 0a 20 20 56 64 62 65 4d 65 )==0 );. VdbeMe
20789 6d 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a 20 mRelease(pTo);.
2078a 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 memcpy(pTo, pFr
2078b 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 om, MEMCELLSIZE)
2078c 3b 0a 20 20 70 54 6f 2d 3e 78 44 65 6c 20 3d 20 ;. pTo->xDel =
2078d 30 3b 0a 20 20 69 66 28 20 28 70 46 72 6f 6d 2d 0;. if( (pFrom-
2078e 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 >flags&MEM_Stati
2078f 63 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54 6f c)==0 ){. pTo
20790 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d ->flags &= ~(MEM
20791 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c _Dyn|MEM_Static|
20792 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 MEM_Ephem);.
20793 61 73 73 65 72 74 28 20 73 72 63 54 79 70 65 3d assert( srcType=
20794 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 73 72 =MEM_Ephem || sr
20795 63 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 74 69 cType==MEM_Stati
20796 63 20 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c c );. pTo->fl
20797 61 67 73 20 7c 3d 20 73 72 63 54 79 70 65 3b 0a ags |= srcType;.
20798 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b }.}../*.** Mak
20799 65 20 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66 e a full copy of
2079a 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e pFrom into pTo.
2079b 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 Prior contents
2079c 20 6f 66 20 70 54 6f 20 61 72 65 0a 2a 2a 20 66 of pTo are.** f
2079d 72 65 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 reed before the
2079e 63 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2f copy is made..*/
2079f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
207a0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
207a1 65 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c emCopy(Mem *pTo,
207a2 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f const Mem *pFro
207a3 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 m){. int rc = S
207a4 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 QLITE_OK;.. ass
207a5 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 ert( (pFrom->fla
207a6 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
207a7 3d 3d 30 20 29 3b 0a 20 20 56 64 62 65 4d 65 6d ==0 );. VdbeMem
207a8 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a 20 20 Release(pTo);.
207a9 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 6f memcpy(pTo, pFro
207aa 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 3b m, MEMCELLSIZE);
207ab 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d . pTo->flags &=
207ac 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20 69 66 ~MEM_Dyn;.. if
207ad 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 28 4d 45 ( pTo->flags&(ME
207ae 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 M_Str|MEM_Blob)
207af 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70 ){. if( 0==(p
207b0 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f From->flags&MEM_
207b1 53 74 61 74 69 63 29 20 29 7b 0a 20 20 20 20 20 Static) ){.
207b2 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d pTo->flags |= M
207b3 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 EM_Ephem;.
207b4 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
207b5 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 MemMakeWriteable
207b6 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d (pTo);. }. }
207b7 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
207b8 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 ../*.** Transfer
207b9 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
207ba 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 2e 20 41 pFrom to pTo. A
207bb 6e 79 20 65 78 69 73 74 69 6e 67 20 76 61 6c 75 ny existing valu
207bc 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a 2a 20 66 e in pTo is.** f
207bd 72 65 65 64 2e 20 49 66 20 70 46 72 6f 6d 20 63 reed. If pFrom c
207be 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d 65 72 61 ontains ephemera
207bf 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 79 20 69 l data, a copy i
207c0 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 70 46 s made..**.** pF
207c1 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 rom contains an
207c2 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e 20 74 68 SQL NULL when th
207c3 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 is routine retur
207c4 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ns..*/.SQLITE_PR
207c5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
207c6 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 e3VdbeMemMove(Me
207c7 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70 46 72 m *pTo, Mem *pFr
207c8 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 om){. assert( p
207c9 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 From->db==0 || s
207ca 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
207cb 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d 75 74 d(pFrom->db->mut
207cc 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
207cd 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pTo->db==0 || s
207ce 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
207cf 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 d(pTo->db->mutex
207d0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
207d1 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 From->db==0 || p
207d2 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 46 72 To->db==0 || pFr
207d3 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62 20 om->db==pTo->db
207d4 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );.. sqlite3Vdb
207d5 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54 6f 29 eMemRelease(pTo)
207d6 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 ;. memcpy(pTo,
207d7 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 4d 65 pFrom, sizeof(Me
207d8 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e 66 6c m));. pFrom->fl
207d9 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a ags = MEM_Null;.
207da 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c 20 3d 20 pFrom->xDel =
207db 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 0;. pFrom->zMal
207dc 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a loc = 0;.}../*.*
207dd 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c * Change the val
207de 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 6f 20 62 ue of a Mem to b
207df 65 20 61 20 73 74 72 69 6e 67 20 6f 72 20 61 20 e a string or a
207e0 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 BLOB..**.** The
207e1 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e memory managemen
207e2 74 20 73 74 72 61 74 65 67 79 20 64 65 70 65 6e t strategy depen
207e3 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 ds on the value
207e4 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a 2a 20 70 of the xDel.** p
207e5 61 72 61 6d 65 74 65 72 2e 20 49 66 20 74 68 65 arameter. If the
207e6 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 69 73 value passed is
207e7 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
207e8 54 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 T, then the .**
207e9 73 74 72 69 6e 67 20 69 73 20 63 6f 70 69 65 64 string is copied
207ea 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 69 62 6c into a (possibl
207eb 79 20 65 78 69 73 74 69 6e 67 29 20 62 75 66 66 y existing) buff
207ec 65 72 20 6d 61 6e 61 67 65 64 20 62 79 20 74 68 er managed by th
207ed 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 75 63 74 e .** Mem struct
207ee 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ure. Otherwise,
207ef 61 6e 79 20 65 78 69 73 74 69 6e 67 20 62 75 66 any existing buf
207f0 66 65 72 20 69 73 20 66 72 65 65 64 20 61 6e 64 fer is freed and
207f1 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 the.** pointer
207f2 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 copied..**.** If
207f3 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 74 the string is t
207f4 6f 6f 20 6c 61 72 67 65 20 28 69 66 20 69 74 20 oo large (if it
207f5 65 78 63 65 65 64 73 20 74 68 65 20 53 51 4c 49 exceeds the SQLI
207f6 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 0a TE_LIMIT_LENGTH.
207f7 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74 29 20 74 ** size limit) t
207f8 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20 61 6c hen no memory al
207f9 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72 73 2e location occurs.
207fa 20 20 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 If the string
207fb 63 61 6e 20 62 65 0a 2a 2a 20 73 74 6f 72 65 64 can be.** stored
207fc 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f 63 61 74 without allocat
207fd 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74 68 65 6e ing memory, then
207fe 20 69 74 20 69 73 2e 20 20 49 66 20 61 20 6d 65 it is. If a me
207ff 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a mory allocation.
20800 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20 74 ** is required t
20801 6f 20 73 74 6f 72 65 20 74 68 65 20 73 74 72 69 o store the stri
20802 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75 65 20 6f ng, then value o
20803 66 20 70 4d 65 6d 20 69 73 20 75 6e 63 68 61 6e f pMem is unchan
20804 67 65 64 2e 20 20 49 6e 0a 2a 2a 20 65 69 74 68 ged. In.** eith
20805 65 72 20 63 61 73 65 2c 20 53 51 4c 49 54 45 5f er case, SQLITE_
20806 54 4f 4f 42 49 47 20 69 73 20 72 65 74 75 72 6e TOOBIG is return
20807 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
20808 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
20809 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 0a 3VdbeMemSetStr(.
2080a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 20 Mem *pMem,
2080b 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 /* Memory
2080c 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20 73 cell to set to s
2080d 74 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a 20 tring value */.
2080e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
2080f 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 70 /* String p
20810 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ointer */. int
20811 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n,
20812 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 /* Bytes in stri
20813 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 ng, or negative
20814 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 */. u8 enc,
20815 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f /* Enco
20816 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f ding of z. 0 fo
20817 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 r BLOBs */. voi
20818 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 d (*xDel)(void*)
20819 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 /* Destructor f
2081a 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 unction */.){.
2081b 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20 int nByte = n;
2081c 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 /* New value
2081d 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0a for pMem->n */.
2081e 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20 int iLimit;
2081f 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
20820 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 allowed string
20821 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0a or blob size */.
20822 20 20 75 31 36 20 66 6c 61 67 73 20 3d 20 30 3b u16 flags = 0;
20823 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c /* New val
20824 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61 ue for pMem->fla
20825 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 gs */.. assert(
20826 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pMem->db==0 ||
20827 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
20828 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 ld(pMem->db->mut
20829 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
2082a 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 (pMem->flags &
2082b 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
2082c 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73 20 ;.. /* If z is
2082d 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 a NULL pointer,
2082e 73 65 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 set pMem to cont
2082f 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e ain an SQL NULL.
20830 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a */. if( !z ){.
20831 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
20832 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b emSetNull(pMem);
20833 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
20834 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 TE_OK;. }.. if
20835 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 ( pMem->db ){.
20836 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d 2d iLimit = pMem-
20837 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
20838 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
20839 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
2083a 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d Limit = SQLITE_M
2083b 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 AX_LENGTH;. }.
2083c 20 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30 flags = (enc==0
2083d 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 ?MEM_Blob:MEM_St
2083e 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c r);. if( nByte<
2083f 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
20840 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20 69 enc!=0 );. i
20841 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 f( enc==SQLITE_U
20842 54 46 38 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 TF8 ){. for
20843 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c (nByte=0; nByte<
20844 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42 79 =iLimit && z[nBy
20845 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a te]; nByte++){}.
20846 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
20847 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 for(nByte=0; nB
20848 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 28 yte<=iLimit && (
20849 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 z[nByte] | z[nBy
2084a 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 te+1]); nByte+=2
2084b 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c ){}. }. fl
2084c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b ags |= MEM_Term;
2084d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 . }.. /* The f
2084e 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 ollowing block s
2084f 65 74 73 20 74 68 65 20 6e 65 77 20 76 61 6c 75 ets the new valu
20850 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 es of Mem.z and
20851 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a Mem.xDel. It. *
20852 2a 20 61 6c 73 6f 20 73 65 74 73 20 61 20 66 6c * also sets a fl
20853 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 ag in local vari
20854 61 62 6c 65 20 22 66 6c 61 67 73 22 20 74 6f 20 able "flags" to
20855 69 6e 64 69 63 61 74 65 20 74 68 65 20 6d 65 6d indicate the mem
20856 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d ory. ** managem
20857 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f ent (one of MEM_
20858 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 Dyn or MEM_Stati
20859 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78 c).. */. if( x
2085a 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e Del==SQLITE_TRAN
2085b 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74 SIENT ){. int
2085c 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b nAlloc = nByte;
2085d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 26 4d . if( flags&M
2085e 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 EM_Term ){.
2085f 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d nAlloc += (enc=
20860 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 =SQLITE_UTF8?1:2
20861 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
20862 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b nByte>iLimit ){
20863 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
20864 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 LITE_TOOBIG;.
20865 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 }. if( sqlit
20866 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d e3VdbeMemGrow(pM
20867 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 em, nAlloc, 0) )
20868 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
20869 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
2086a 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d }. memcpy(pM
2086b 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 em->z, z, nAlloc
2086c 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 );. }else if( x
2086d 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e 41 Del==SQLITE_DYNA
2086e 4d 49 43 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 MIC ){. sqlit
2086f 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
20870 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d (pMem);. pMem
20871 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d ->zMalloc = pMem
20872 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b ->z = (char *)z;
20873 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 . pMem->xDel
20874 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
20875 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
20876 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 Release(pMem);.
20877 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 pMem->z = (ch
20878 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d ar *)z;. pMem
20879 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 ->xDel = xDel;.
2087a 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 28 78 44 flags |= ((xD
2087b 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54 49 el==SQLITE_STATI
2087c 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d 45 C)?MEM_Static:ME
2087d 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a 20 20 70 M_Dyn);. }.. p
2087e 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a Mem->n = nByte;.
2087f 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
20880 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e 65 flags;. pMem->e
20881 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 nc = (enc==0 ? S
20882 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 6e 63 QLITE_UTF8 : enc
20883 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 );. pMem->type
20884 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c 49 = (enc==0 ? SQLI
20885 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54 45 TE_BLOB : SQLITE
20886 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65 66 _TEXT);..#ifndef
20887 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
20888 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 16. if( pMem->e
20889 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc!=SQLITE_UTF8
2088a 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 && sqlite3VdbeMe
2088b 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d 29 mHandleBom(pMem)
2088c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
2088d 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
2088e 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e .#endif.. if( n
2088f 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 Byte>iLimit ){.
20890 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
20891 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 20 _TOOBIG;. }..
20892 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
20893 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 ;.}../*.** Compa
20894 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f re the values co
20895 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 74 ntained by the t
20896 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c wo memory cells,
20897 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 returning.** ne
20898 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 gative, zero or
20899 70 6f 73 69 74 69 76 65 20 69 66 20 70 4d 65 6d positive if pMem
2089a 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 1 is less than,
2089b 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 equal to, or gre
2089c 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 ater.** than pMe
2089d 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64 65 m2. Sorting orde
2089e 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 69 72 73 r is NULL's firs
2089f 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e t, followed by n
208a0 75 6d 62 65 72 73 20 28 69 6e 74 65 67 65 72 73 umbers (integers
208a1 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 29 20 73 .** and reals) s
208a2 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c orted numericall
208a3 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 y, followed by t
208a4 65 78 74 20 6f 72 64 65 72 65 64 20 62 79 20 74 ext ordered by t
208a5 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 he collating.**
208a6 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61 sequence pColl a
208a7 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 nd finally blob'
208a8 73 20 6f 72 64 65 72 65 64 20 62 79 20 6d 65 6d s ordered by mem
208a9 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f cmp()..**.** Two
208aa 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 NULL values are
208ab 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71 75 61 considered equa
208ac 6c 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 l by this functi
208ad 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
208ae 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
208af 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 3MemCompare(cons
208b0 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f t Mem *pMem1, co
208b1 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 nst Mem *pMem2,
208b2 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70 const CollSeq *p
208b3 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b Coll){. int rc;
208b4 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20 . int f1, f2;.
208b5 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c int combined_fl
208b6 61 67 73 3b 0a 0a 20 20 66 31 20 3d 20 70 4d 65 ags;.. f1 = pMe
208b7 6d 31 2d 3e 66 6c 61 67 73 3b 0a 20 20 66 32 20 m1->flags;. f2
208b8 3d 20 70 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b 0a = pMem2->flags;.
208b9 20 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 combined_flags
208ba 20 3d 20 66 31 7c 66 32 3b 0a 20 20 61 73 73 65 = f1|f2;. asse
208bb 72 74 28 20 28 63 6f 6d 62 69 6e 65 64 5f 66 6c rt( (combined_fl
208bc 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 ags & MEM_RowSet
208bd 29 3d 3d 30 20 29 3b 0a 20 0a 20 20 2f 2a 20 49 )==0 );. . /* I
208be 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 4e f one value is N
208bf 55 4c 4c 2c 20 69 74 20 69 73 20 6c 65 73 73 20 ULL, it is less
208c0 74 68 61 6e 20 74 68 65 20 6f 74 68 65 72 2e 20 than the other.
208c1 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 0a 20 If both values.
208c2 20 2a 2a 20 61 72 65 20 4e 55 4c 4c 2c 20 72 65 ** are NULL, re
208c3 74 75 72 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 turn 0.. */. i
208c4 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 f( combined_flag
208c5 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 s&MEM_Null ){.
208c6 20 20 72 65 74 75 72 6e 20 28 66 32 26 4d 45 4d return (f2&MEM
208c7 5f 4e 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d _Null) - (f1&MEM
208c8 5f 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f _Null);. }.. /
208c9 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 * If one value i
208ca 73 20 61 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 s a number and t
208cb 68 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2c he other is not,
208cc 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20 6c the number is l
208cd 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 ess.. ** If bot
208ce 68 20 61 72 65 20 6e 75 6d 62 65 72 73 2c 20 63 h are numbers, c
208cf 6f 6d 70 61 72 65 20 61 73 20 72 65 61 6c 73 20 ompare as reals
208d0 69 66 20 6f 6e 65 20 69 73 20 61 20 72 65 61 6c if one is a real
208d1 2c 20 6f 72 20 61 73 20 69 6e 74 65 67 65 72 73 , or as integers
208d2 0a 20 20 2a 2a 20 69 66 20 62 6f 74 68 20 76 61 . ** if both va
208d3 6c 75 65 73 20 61 72 65 20 69 6e 74 65 67 65 72 lues are integer
208d4 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f s.. */. if( co
208d5 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 28 4d 45 mbined_flags&(ME
208d6 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 M_Int|MEM_Real)
208d7 29 7b 0a 20 20 20 20 69 66 28 20 21 28 66 31 26 ){. if( !(f1&
208d8 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 (MEM_Int|MEM_Rea
208d9 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 l)) ){. ret
208da 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 1;. }.
208db 20 69 66 28 20 21 28 66 32 26 28 4d 45 4d 5f 49 if( !(f2&(MEM_I
208dc 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b nt|MEM_Real)) ){
208dd 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 . return -1
208de 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
208df 28 66 31 20 26 20 66 32 20 26 20 4d 45 4d 5f 49 (f1 & f2 & MEM_I
208e0 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 nt)==0 ){.
208e1 64 6f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 double r1, r2;.
208e2 20 20 20 20 20 69 66 28 20 28 66 31 26 4d 45 4d if( (f1&MEM
208e3 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 _Real)==0 ){.
208e4 20 20 20 20 20 72 31 20 3d 20 28 64 6f 75 62 6c r1 = (doubl
208e5 65 29 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20 e)pMem1->u.i;.
208e6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
208e7 20 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72 r1 = pMem1->r
208e8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
208e9 69 66 28 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c if( (f2&MEM_Real
208ea 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
208eb 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 r2 = (double)pMe
208ec 6d 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d m2->u.i;. }
208ed 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 32 else{. r2
208ee 20 3d 20 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 20 = pMem2->r;.
208ef 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 }. if( r
208f0 31 3c 72 32 20 29 20 72 65 74 75 72 6e 20 2d 31 1<r2 ) return -1
208f1 3b 0a 20 20 20 20 20 20 69 66 28 20 72 31 3e 72 ;. if( r1>r
208f2 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 2 ) return 1;.
208f3 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
208f4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
208f5 73 73 65 72 74 28 20 66 31 26 4d 45 4d 5f 49 6e ssert( f1&MEM_In
208f6 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 t );. asser
208f7 74 28 20 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b t( f2&MEM_Int );
208f8 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 . if( pMem1
208f9 2d 3e 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75 ->u.i < pMem2->u
208fa 2e 69 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a .i ) return -1;.
208fb 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d if( pMem1-
208fc 3e 75 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e >u.i > pMem2->u.
208fd 69 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 i ) return 1;.
208fe 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
208ff 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 }. }.. /* If
20900 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61 20 one value is a
20901 73 74 72 69 6e 67 20 61 6e 64 20 74 68 65 20 6f string and the o
20902 74 68 65 72 20 69 73 20 61 20 62 6c 6f 62 2c 20 ther is a blob,
20903 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 6c 65 the string is le
20904 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 ss.. ** If both
20905 20 61 72 65 20 73 74 72 69 6e 67 73 2c 20 63 6f are strings, co
20906 6d 70 61 72 65 20 75 73 69 6e 67 20 74 68 65 20 mpare using the
20907 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 collating functi
20908 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ons.. */. if(
20909 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d combined_flags&M
2090a 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 66 EM_Str ){. if
2090b 28 20 28 66 31 20 26 20 4d 45 4d 5f 53 74 72 29 ( (f1 & MEM_Str)
2090c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
2090d 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 1;. }.
2090e 20 69 66 28 20 28 66 32 20 26 20 4d 45 4d 5f 53 if( (f2 & MEM_S
2090f 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 tr)==0 ){.
20910 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d return -1;. }
20911 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d .. assert( pM
20912 65 6d 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d em1->enc==pMem2-
20913 3e 65 6e 63 20 29 3b 0a 20 20 20 20 61 73 73 65 >enc );. asse
20914 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d rt( pMem1->enc==
20915 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a SQLITE_UTF8 || .
20916 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 65 6d pMem
20917 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 1->enc==SQLITE_U
20918 54 46 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 2d TF16LE || pMem1-
20919 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 >enc==SQLITE_UTF
2091a 31 36 42 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 16BE );.. /*
2091b 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 The collation se
2091c 71 75 65 6e 63 65 20 6d 75 73 74 20 62 65 20 64 quence must be d
2091d 65 66 69 6e 65 64 20 61 74 20 74 68 69 73 20 70 efined at this p
2091e 6f 69 6e 74 2c 20 65 76 65 6e 20 69 66 0a 20 20 oint, even if.
2091f 20 20 2a 2a 20 74 68 65 20 75 73 65 72 20 64 65 ** the user de
20920 6c 65 74 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 letes the collat
20921 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 61 66 74 ion sequence aft
20922 65 72 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 er the vdbe prog
20923 72 61 6d 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f ram is. ** co
20924 6d 70 69 6c 65 64 20 28 74 68 69 73 20 77 61 73 mpiled (this was
20925 20 6e 6f 74 20 61 6c 77 61 79 73 20 74 68 65 20 not always the
20926 63 61 73 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 case).. */.
20927 20 20 61 73 73 65 72 74 28 20 21 70 43 6f 6c 6c assert( !pColl
20928 20 7c 7c 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 || pColl->xCmp
20929 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 43 6f 6c );.. if( pCol
2092a 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 l ){. if( p
2092b 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c Mem1->enc==pColl
2092c 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 ->enc ){.
2092d 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 /* The strings
2092e 61 72 65 20 61 6c 72 65 61 64 79 20 69 6e 20 74 are already in t
2092f 68 65 20 63 6f 72 72 65 63 74 20 65 6e 63 6f 64 he correct encod
20930 69 6e 67 2e 20 20 43 61 6c 6c 20 74 68 65 0a 20 ing. Call the.
20931 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 ** compar
20932 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 64 69 ison function di
20933 72 65 63 74 6c 79 20 2a 2f 0a 20 20 20 20 20 20 rectly */.
20934 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e return pColl->
20935 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 xCmp(pColl->pUse
20936 72 2c 70 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 r,pMem1->n,pMem1
20937 2d 3e 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 ->z,pMem2->n,pMe
20938 6d 32 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 65 m2->z);. }e
20939 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e lse{. con
2093a 73 74 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32 st void *v1, *v2
2093b 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 31 ;. int n1
2093c 2c 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 4d 65 , n2;. Me
2093d 6d 20 63 31 3b 0a 20 20 20 20 20 20 20 20 4d 65 m c1;. Me
2093e 6d 20 63 32 3b 0a 20 20 20 20 20 20 20 20 6d 65 m c2;. me
2093f 6d 73 65 74 28 26 63 31 2c 20 30 2c 20 73 69 7a mset(&c1, 0, siz
20940 65 6f 66 28 63 31 29 29 3b 0a 20 20 20 20 20 20 eof(c1));.
20941 20 20 6d 65 6d 73 65 74 28 26 63 32 2c 20 30 2c memset(&c2, 0,
20942 20 73 69 7a 65 6f 66 28 63 32 29 29 3b 0a 20 20 sizeof(c2));.
20943 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
20944 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 eMemShallowCopy(
20945 26 63 31 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d 5f &c1, pMem1, MEM_
20946 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 Ephem);.
20947 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 sqlite3VdbeMemSh
20948 61 6c 6c 6f 77 43 6f 70 79 28 26 63 32 2c 20 70 allowCopy(&c2, p
20949 4d 65 6d 32 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 Mem2, MEM_Ephem)
2094a 3b 0a 20 20 20 20 20 20 20 20 76 31 20 3d 20 73 ;. v1 = s
2094b 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 qlite3ValueText(
2094c 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
2094d 26 63 31 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 &c1, pColl->enc)
2094e 3b 0a 20 20 20 20 20 20 20 20 6e 31 20 3d 20 76 ;. n1 = v
2094f 31 3d 3d 30 20 3f 20 30 20 3a 20 63 31 2e 6e 3b 1==0 ? 0 : c1.n;
20950 0a 20 20 20 20 20 20 20 20 76 32 20 3d 20 73 71 . v2 = sq
20951 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 lite3ValueText((
20952 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 sqlite3_value*)&
20953 63 32 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b c2, pColl->enc);
20954 0a 20 20 20 20 20 20 20 20 6e 32 20 3d 20 76 32 . n2 = v2
20955 3d 3d 30 20 3f 20 30 20 3a 20 63 32 2e 6e 3b 0a ==0 ? 0 : c2.n;.
20956 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 43 6f rc = pCo
20957 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e ll->xCmp(pColl->
20958 70 55 73 65 72 2c 20 6e 31 2c 20 76 31 2c 20 6e pUser, n1, v1, n
20959 32 2c 20 76 32 29 3b 0a 20 20 20 20 20 20 20 20 2, v2);.
2095a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
2095b 6c 65 61 73 65 28 26 63 31 29 3b 0a 20 20 20 20 lease(&c1);.
2095c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
2095d 65 6d 52 65 6c 65 61 73 65 28 26 63 32 29 3b 0a emRelease(&c2);.
2095e 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
2095f 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
20960 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 4e 55 4c . /* If a NUL
20961 4c 20 70 6f 69 6e 74 65 72 20 77 61 73 20 70 61 L pointer was pa
20962 73 73 65 64 20 61 73 20 74 68 65 20 63 6f 6c 6c ssed as the coll
20963 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 61 ate function, fa
20964 6c 6c 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a ll through. *
20965 2a 20 74 6f 20 74 68 65 20 62 6c 6f 62 20 63 61 * to the blob ca
20966 73 65 20 61 6e 64 20 75 73 65 20 6d 65 6d 63 6d se and use memcm
20967 70 28 29 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a 20 p(). */. }. .
20968 20 2f 2a 20 42 6f 74 68 20 76 61 6c 75 65 73 20 /* Both values
20969 6d 75 73 74 20 62 65 20 62 6c 6f 62 73 2e 20 20 must be blobs.
2096a 43 6f 6d 70 61 72 65 20 75 73 69 6e 67 20 6d 65 Compare using me
2096b 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 72 63 mcmp(). */. rc
2096c 20 3d 20 6d 65 6d 63 6d 70 28 70 4d 65 6d 31 2d = memcmp(pMem1-
2096d 3e 7a 2c 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28 70 >z, pMem2->z, (p
2096e 4d 65 6d 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e 6e Mem1->n>pMem2->n
2096f 29 3f 70 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d 31 )?pMem2->n:pMem1
20970 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d ->n);. if( rc==
20971 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 4d 0 ){. rc = pM
20972 65 6d 31 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d 3e em1->n - pMem2->
20973 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 n;. }. return
20974 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 rc;.}../*.** Mov
20975 65 20 64 61 74 61 20 6f 75 74 20 6f 66 20 61 20 e data out of a
20976 62 74 72 65 65 20 6b 65 79 20 6f 72 20 64 61 74 btree key or dat
20977 61 20 66 69 65 6c 64 20 61 6e 64 20 69 6e 74 6f a field and into
20978 20 61 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 a Mem structure
20979 2e 0a 2a 2a 20 54 68 65 20 64 61 74 61 20 6f 72 ..** The data or
2097a 20 6b 65 79 20 69 73 20 74 61 6b 65 6e 20 66 72 key is taken fr
2097b 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 om the entry tha
2097c 74 20 70 43 75 72 20 69 73 20 63 75 72 72 65 6e t pCur is curren
2097d 74 6c 79 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 tly pointing.**
2097e 74 6f 2e 20 20 6f 66 66 73 65 74 20 61 6e 64 20 to. offset and
2097f 61 6d 74 20 64 65 74 65 72 6d 69 6e 65 20 77 68 amt determine wh
20980 61 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 at portion of th
20981 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 74 6f e data or key to
20982 20 72 65 74 72 69 65 76 65 2e 0a 2a 2a 20 6b 65 retrieve..** ke
20983 79 20 69 73 20 74 72 75 65 20 74 6f 20 67 65 74 y is true to get
20984 20 74 68 65 20 6b 65 79 20 6f 72 20 66 61 6c 73 the key or fals
20985 65 20 74 6f 20 67 65 74 20 64 61 74 61 2e 20 20 e to get data.
20986 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 77 72 The result is wr
20987 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 itten.** into th
20988 65 20 70 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e 0a e pMem element..
20989 2a 2a 0a 2a 2a 20 54 68 65 20 70 4d 65 6d 20 73 **.** The pMem s
2098a 74 72 75 63 74 75 72 65 20 69 73 20 61 73 73 75 tructure is assu
2098b 6d 65 64 20 74 6f 20 62 65 20 75 6e 69 6e 69 74 med to be uninit
2098c 69 61 6c 69 7a 65 64 2e 20 20 41 6e 79 20 70 72 ialized. Any pr
2098d 69 6f 72 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 ior content.** i
2098e 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 s overwritten wi
2098f 74 68 6f 75 74 20 62 65 69 6e 67 20 66 72 65 65 thout being free
20990 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 d..**.** If this
20991 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 66 routine fails f
20992 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 20 28 6d or any reason (m
20993 61 6c 6c 6f 63 20 72 65 74 75 72 6e 73 20 4e 55 alloc returns NU
20994 4c 4c 20 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a 20 LL or unable.**
20995 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 to read from the
20996 20 64 69 73 6b 29 20 74 68 65 6e 20 74 68 65 20 disk) then the
20997 70 4d 65 6d 20 69 73 20 6c 65 66 74 20 69 6e 20 pMem is left in
20998 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 an inconsistent
20999 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 state..*/.SQLITE
2099a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
2099b 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 ite3VdbeMemFromB
2099c 74 72 65 65 28 0a 20 20 42 74 43 75 72 73 6f 72 tree(. BtCursor
2099d 20 2a 70 43 75 72 2c 20 20 20 2f 2a 20 43 75 72 *pCur, /* Cur
2099e 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 sor pointing at
2099f 72 65 63 6f 72 64 20 74 6f 20 72 65 74 72 69 65 record to retrie
209a0 76 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 ve. */. int off
209a1 73 65 74 2c 20 20 20 20 20 20 20 2f 2a 20 4f 66 set, /* Of
209a2 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 73 74 fset from the st
209a3 61 72 74 20 6f 66 20 64 61 74 61 20 74 6f 20 72 art of data to r
209a4 65 74 75 72 6e 20 62 79 74 65 73 20 66 72 6f 6d eturn bytes from
209a5 2e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 . */. int amt,
209a6 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
209a7 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 er of bytes to r
209a8 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 eturn. */. int
209a9 6b 65 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a key, /*
209aa 20 49 66 20 74 72 75 65 2c 20 72 65 74 72 69 65 If true, retrie
209ab 76 65 20 66 72 6f 6d 20 74 68 65 20 62 74 72 65 ve from the btre
209ac 65 20 6b 65 79 2c 20 6e 6f 74 20 64 61 74 61 2e e key, not data.
209ad 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 */. Mem *pMem
209ae 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 /* OUT:
209af 52 65 74 75 72 6e 20 64 61 74 61 20 69 6e 20 74 Return data in t
209b0 68 69 73 20 4d 65 6d 20 73 74 72 75 63 74 75 72 his Mem structur
209b1 65 2e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 e. */.){. char
209b2 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20 20 2f *zData; /
209b3 2a 20 44 61 74 61 20 66 72 6f 6d 20 74 68 65 20 * Data from the
209b4 62 74 72 65 65 20 6c 61 79 65 72 20 2a 2f 0a 20 btree layer */.
209b5 20 69 6e 74 20 61 76 61 69 6c 61 62 6c 65 20 3d int available =
209b6 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 0; /* Number o
209b7 66 20 62 79 74 65 73 20 61 76 61 69 6c 61 62 6c f bytes availabl
209b8 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 62 e on the local b
209b9 74 72 65 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 tree page */. i
209ba 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
209bb 4b 3b 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 K; /* Return cod
209bc 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 e */.. assert(
209bd 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
209be 6f 72 49 73 56 61 6c 69 64 28 70 43 75 72 29 20 orIsValid(pCur)
209bf 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 );.. /* Note: t
209c0 68 65 20 63 61 6c 6c 73 20 74 6f 20 42 74 72 65 he calls to Btre
209c1 65 4b 65 79 46 65 74 63 68 28 29 20 61 6e 64 20 eKeyFetch() and
209c2 44 61 74 61 46 65 74 63 68 28 29 20 62 65 6c 6f DataFetch() belo
209c3 77 20 61 73 73 65 72 74 28 29 20 0a 20 20 2a 2a w assert() . **
209c4 20 74 68 61 74 20 62 6f 74 68 20 74 68 65 20 42 that both the B
209c5 74 53 68 61 72 65 64 20 61 6e 64 20 64 61 74 61 tShared and data
209c6 62 61 73 65 20 68 61 6e 64 6c 65 20 6d 75 74 65 base handle mute
209c7 78 65 73 20 61 72 65 20 68 65 6c 64 2e 20 2a 2f xes are held. */
209c8 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
209c9 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
209ca 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 69 66 wSet)==0 );. if
209cb 28 20 6b 65 79 20 29 7b 0a 20 20 20 20 7a 44 61 ( key ){. zDa
209cc 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c ta = (char *)sql
209cd 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 ite3BtreeKeyFetc
209ce 68 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 h(pCur, &availab
209cf 6c 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 le);. }else{.
209d0 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72 20 zData = (char
209d1 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 *)sqlite3BtreeDa
209d2 74 61 46 65 74 63 68 28 70 43 75 72 2c 20 26 61 taFetch(pCur, &a
209d3 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 vailable);. }.
209d4 20 61 73 73 65 72 74 28 20 7a 44 61 74 61 21 3d assert( zData!=
209d5 30 20 29 3b 0a 0a 20 20 69 66 28 20 6f 66 66 73 0 );.. if( offs
209d6 65 74 2b 61 6d 74 3c 3d 61 76 61 69 6c 61 62 6c et+amt<=availabl
209d7 65 20 26 26 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 e && (pMem->flag
209d8 73 26 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 7b s&MEM_Dyn)==0 ){
209d9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
209da 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 MemRelease(pMem)
209db 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 ;. pMem->z =
209dc 26 7a 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a &zData[offset];.
209dd 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
209de 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 = MEM_Blob|MEM_E
209df 70 68 65 6d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 phem;. }else if
209e0 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 ( SQLITE_OK==(rc
209e1 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 = sqlite3VdbeMe
209e2 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 61 6d 74 2b mGrow(pMem, amt+
209e3 32 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 70 4d 2, 0)) ){. pM
209e4 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
209e5 42 6c 6f 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d Blob|MEM_Dyn|MEM
209e6 5f 54 65 72 6d 3b 0a 20 20 20 20 70 4d 65 6d 2d _Term;. pMem-
209e7 3e 65 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 4d >enc = 0;. pM
209e8 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 em->type = SQLIT
209e9 45 5f 42 4c 4f 42 3b 0a 20 20 20 20 69 66 28 20 E_BLOB;. if(
209ea 6b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 key ){. rc
209eb 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 = sqlite3BtreeKe
209ec 79 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 y(pCur, offset,
209ed 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 amt, pMem->z);.
209ee 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
209ef 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
209f0 65 44 61 74 61 28 70 43 75 72 2c 20 6f 66 66 73 eData(pCur, offs
209f1 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a et, amt, pMem->z
209f2 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 );. }. pMe
209f3 6d 2d 3e 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a 20 m->z[amt] = 0;.
209f4 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31 pMem->z[amt+1
209f5 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 ] = 0;. if( r
209f6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
209f7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
209f8 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d eMemRelease(pMem
209f9 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 );. }. }. p
209fa 4d 65 6d 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a 20 Mem->n = amt;..
209fb 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
209fc 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
209fd 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c is only availabl
209fe 65 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 e internally, it
209ff 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 is not part of
20a00 74 68 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 the.** external
20a01 41 50 49 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e API. It works in
20a02 20 61 20 73 69 6d 69 6c 61 72 20 77 61 79 20 74 a similar way t
20a03 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f o sqlite3_value_
20a04 74 65 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 text(),.** excep
20a05 74 20 74 68 65 20 64 61 74 61 20 72 65 74 75 72 t the data retur
20a06 6e 65 64 20 69 73 20 69 6e 20 74 68 65 20 65 6e ned is in the en
20a07 63 6f 64 69 6e 67 20 73 70 65 63 69 66 69 65 64 coding specified
20a08 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a by the second.*
20a09 2a 20 70 61 72 61 6d 65 74 65 72 2c 20 77 68 69 * parameter, whi
20a0a 63 68 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f ch must be one o
20a0b 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 f SQLITE_UTF16BE
20a0c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 , SQLITE_UTF16LE
20a0d 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 or.** SQLITE_UT
20a0e 46 38 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d F8..**.** (2006-
20a0f 30 32 2d 31 36 3a 29 20 20 54 68 65 20 65 6e 63 02-16:) The enc
20a10 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 value can be or
20a11 2d 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f -ed with SQLITE_
20a12 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a UTF16_ALIGNED..*
20a13 2a 20 49 66 20 74 68 61 74 20 69 73 20 74 68 65 * If that is the
20a14 20 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 case, then the
20a15 72 65 73 75 6c 74 20 6d 75 73 74 20 62 65 20 61 result must be a
20a16 6c 69 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 ligned on an eve
20a17 6e 20 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 n byte.** bounda
20a18 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ry..*/.SQLITE_PR
20a19 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 IVATE const void
20a1a 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 *sqlite3ValueTe
20a1b 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 xt(sqlite3_value
20a1c 2a 20 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b * pVal, u8 enc){
20a1d 0a 20 20 69 66 28 20 21 70 56 61 6c 20 29 20 72 . if( !pVal ) r
20a1e 65 74 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 eturn 0;.. asse
20a1f 72 74 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 rt( pVal->db==0
20a20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
20a21 5f 68 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e _held(pVal->db->
20a22 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
20a23 72 74 28 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e rt( (enc&3)==(en
20a24 63 26 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f c&~SQLITE_UTF16_
20a25 41 4c 49 47 4e 45 44 29 20 29 3b 0a 20 20 61 73 ALIGNED) );. as
20a26 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 sert( (pVal->fla
20a27 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
20a28 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 56 ==0 );.. if( pV
20a29 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 al->flags&MEM_Nu
20a2a 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ll ){. return
20a2b 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 0;. }. assert
20a2c 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 ( (MEM_Blob>>3)
20a2d 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 == MEM_Str );.
20a2e 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 pVal->flags |= (
20a2f 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pVal->flags & ME
20a30 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 45 78 M_Blob)>>3;. Ex
20a31 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a pandBlob(pVal);.
20a32 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 if( pVal->flag
20a33 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 s&MEM_Str ){.
20a34 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
20a35 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c geEncoding(pVal,
20a36 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 enc & ~SQLITE_U
20a37 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20 TF16_ALIGNED);.
20a38 20 20 20 69 66 28 20 28 65 6e 63 20 26 20 53 51 if( (enc & SQ
20a39 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e LITE_UTF16_ALIGN
20a3a 45 44 29 21 3d 30 20 26 26 20 31 3d 3d 28 31 26 ED)!=0 && 1==(1&
20a3b 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e SQLITE_PTR_TO_IN
20a3c 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a 20 T(pVal->z)) ){.
20a3d 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56 assert( (pV
20a3e 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d al->flags & (MEM
20a3f 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 _Ephem|MEM_Stati
20a40 63 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 c))!=0 );.
20a41 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
20a42 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 emMakeWriteable(
20a43 70 56 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b pVal)!=SQLITE_OK
20a44 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
20a45 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rn 0;. }.
20a46 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 }. sqlite3V
20a47 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 dbeMemNulTermina
20a48 74 65 28 70 56 61 6c 29 3b 20 2f 2a 20 49 4d 50 te(pVal); /* IMP
20a49 3a 20 52 2d 33 31 32 37 35 2d 34 34 30 36 30 20 : R-31275-44060
20a4a 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 */. }else{.
20a4b 61 73 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 assert( (pVal->f
20a4c 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3d 3d lags&MEM_Blob)==
20a4d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 );. sqlite3
20a4e 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 VdbeMemStringify
20a4f 28 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 (pVal, enc);.
20a50 20 61 73 73 65 72 74 28 20 30 3d 3d 28 31 26 53 assert( 0==(1&S
20a51 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 QLITE_PTR_TO_INT
20a52 28 70 56 61 6c 2d 3e 7a 29 29 20 29 3b 0a 20 20 (pVal->z)) );.
20a53 7d 0a 20 20 61 73 73 65 72 74 28 70 56 61 6c 2d }. assert(pVal-
20a54 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 >enc==(enc & ~SQ
20a55 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e LITE_UTF16_ALIGN
20a56 45 44 29 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d ED) || pVal->db=
20a57 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 =0.
20a58 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61 || pVal->db->ma
20a59 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
20a5a 69 66 28 20 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 if( pVal->enc==(
20a5b 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 enc & ~SQLITE_UT
20a5c 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 29 7b 0a F16_ALIGNED) ){.
20a5d 20 20 20 20 72 65 74 75 72 6e 20 70 56 61 6c 2d return pVal-
20a5e 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 >z;. }else{.
20a5f 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d return 0;. }.}
20a60 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
20a61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c new sqlite3_val
20a62 75 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 ue object..*/.SQ
20a63 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
20a64 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 ite3_value *sqli
20a65 74 65 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 te3ValueNew(sqli
20a66 74 65 33 20 2a 64 62 29 7b 0a 20 20 4d 65 6d 20 te3 *db){. Mem
20a67 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 *p = sqlite3DbMa
20a68 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a llocZero(db, siz
20a69 65 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 eof(*p));. if(
20a6a 70 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 p ){. p->flag
20a6b 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
20a6c 20 20 70 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 p->type = SQLI
20a6d 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e TE_NULL;. p->
20a6e 64 62 20 3d 20 64 62 3b 0a 20 20 7d 0a 20 20 72 db = db;. }. r
20a6f 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
20a70 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 * Create a new s
20a71 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
20a72 65 63 74 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ect, containing
20a73 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45 78 the value of pEx
20a74 70 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f pr..**.** This o
20a75 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 76 65 nly works for ve
20a76 72 79 20 73 69 6d 70 6c 65 20 65 78 70 72 65 73 ry simple expres
20a77 73 69 6f 6e 73 20 74 68 61 74 20 63 6f 6e 73 69 sions that consi
20a78 73 74 20 6f 66 20 6f 6e 65 20 63 6f 6e 73 74 61 st of one consta
20a79 6e 74 0a 2a 2a 20 74 6f 6b 65 6e 20 28 69 2e 65 nt.** token (i.e
20a7a 2e 20 22 35 22 2c 20 22 35 2e 31 22 2c 20 22 27 . "5", "5.1", "'
20a7b 61 20 73 74 72 69 6e 67 27 22 29 2e 20 49 66 20 a string'"). If
20a7c 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63 the expression c
20a7d 61 6e 0a 2a 2a 20 62 65 20 63 6f 6e 76 65 72 74 an.** be convert
20a7e 65 64 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f ed directly into
20a7f 20 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 a value, then t
20a80 68 65 20 76 61 6c 75 65 20 69 73 20 61 6c 6c 6f he value is allo
20a81 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 61 20 70 cated and.** a p
20a82 6f 69 6e 74 65 72 20 77 72 69 74 74 65 6e 20 74 ointer written t
20a83 6f 20 2a 70 70 56 61 6c 2e 20 54 68 65 20 63 61 o *ppVal. The ca
20a84 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 ller is responsi
20a85 62 6c 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61 ble for dealloca
20a86 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75 ting.** the valu
20a87 65 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 20 e by passing it
20a88 74 6f 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 to sqlite3ValueF
20a89 72 65 65 28 29 20 6c 61 74 65 72 20 6f 6e 2e 20 ree() later on.
20a8a 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f If the expressio
20a8b 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63 n.** cannot be c
20a8c 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 20 76 61 onverted to a va
20a8d 6c 75 65 2c 20 74 68 65 6e 20 2a 70 70 56 61 6c lue, then *ppVal
20a8e 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e is set to NULL.
20a8f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20a90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 TE int sqlite3Va
20a91 6c 75 65 46 72 6f 6d 45 78 70 72 28 0a 20 20 73 lueFromExpr(. s
20a92 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 qlite3 *db,
20a93 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
20a94 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
20a95 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ion */. Expr *p
20a96 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 Expr,
20a97 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 /* The expres
20a98 73 69 6f 6e 20 74 6f 20 65 76 61 6c 75 61 74 65 sion to evaluate
20a99 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 */. u8 enc,
20a9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20a9b 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 /* Encoding to u
20a9c 73 65 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e se */. u8 affin
20a9d 69 74 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 ity,
20a9e 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 74 6f /* Affinity to
20a9f 20 75 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 use */. sqlite
20aa0 33 5f 76 61 6c 75 65 20 2a 2a 70 70 56 61 6c 20 3_value **ppVal
20aa1 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
20aa2 20 6e 65 77 20 76 61 6c 75 65 20 68 65 72 65 20 new value here
20aa3 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a */.){. int op;.
20aa4 20 20 63 68 61 72 20 2a 7a 56 61 6c 20 3d 20 30 char *zVal = 0
20aa5 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ;. sqlite3_valu
20aa6 65 20 2a 70 56 61 6c 20 3d 20 30 3b 0a 20 20 69 e *pVal = 0;. i
20aa7 6e 74 20 6e 65 67 49 6e 74 20 3d 20 31 3b 0a 20 nt negInt = 1;.
20aa8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 const char *zNe
20aa9 67 20 3d 20 22 22 3b 0a 0a 20 20 69 66 28 20 21 g = "";.. if( !
20aaa 70 45 78 70 72 20 29 7b 0a 20 20 20 20 2a 70 70 pExpr ){. *pp
20aab 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 Val = 0;. ret
20aac 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
20aad 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d }. op = pExpr-
20aae 3e 6f 70 3b 0a 0a 20 20 2f 2a 20 6f 70 20 63 61 >op;.. /* op ca
20aaf 6e 20 6f 6e 6c 79 20 62 65 20 54 4b 5f 52 45 47 n only be TK_REG
20ab0 49 53 54 45 52 20 69 66 20 77 65 20 68 61 76 65 ISTER if we have
20ab1 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53 compiled with S
20ab2 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 QLITE_ENABLE_STA
20ab3 54 33 2e 0a 20 20 2a 2a 20 54 68 65 20 69 66 64 T3.. ** The ifd
20ab4 65 66 20 68 65 72 65 20 69 73 20 74 6f 20 65 6e ef here is to en
20ab5 61 62 6c 65 20 75 73 20 74 6f 20 61 63 68 69 65 able us to achie
20ab6 76 65 20 31 30 30 25 20 62 72 61 6e 63 68 20 74 ve 100% branch t
20ab7 65 73 74 20 63 6f 76 65 72 61 67 65 20 65 76 65 est coverage eve
20ab8 6e 0a 20 20 2a 2a 20 77 68 65 6e 20 53 51 4c 49 n. ** when SQLI
20ab9 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 20 TE_ENABLE_STAT3
20aba 69 73 20 6f 6d 69 74 74 65 64 2e 0a 20 20 2a 2f is omitted.. */
20abb 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
20abc 4e 41 42 4c 45 5f 53 54 41 54 33 0a 20 20 69 66 NABLE_STAT3. if
20abd 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 ( op==TK_REGISTE
20abe 52 20 29 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e R ) op = pExpr->
20abf 6f 70 32 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 op2;.#else. if(
20ac0 20 4e 45 56 45 52 28 6f 70 3d 3d 54 4b 5f 52 45 NEVER(op==TK_RE
20ac1 47 49 53 54 45 52 29 20 29 20 6f 70 20 3d 20 70 GISTER) ) op = p
20ac2 45 78 70 72 2d 3e 6f 70 32 3b 0a 23 65 6e 64 69 Expr->op2;.#endi
20ac3 66 0a 0a 20 20 2f 2a 20 48 61 6e 64 6c 65 20 6e f.. /* Handle n
20ac4 65 67 61 74 69 76 65 20 69 6e 74 65 67 65 72 73 egative integers
20ac5 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 73 74 65 in a single ste
20ac6 70 2e 20 20 54 68 69 73 20 69 73 20 6e 65 65 64 p. This is need
20ac7 65 64 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 63 ed in the. ** c
20ac8 61 73 65 20 77 68 65 6e 20 74 68 65 20 76 61 6c ase when the val
20ac9 75 65 20 69 73 20 2d 39 32 32 33 33 37 32 30 33 ue is -922337203
20aca 36 38 35 34 37 37 35 38 30 38 2e 0a 20 20 2a 2f 6854775808.. */
20acb 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 55 4d . if( op==TK_UM
20acc 49 4e 55 53 0a 20 20 20 26 26 20 28 70 45 78 70 INUS. && (pExp
20acd 72 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b r->pLeft->op==TK
20ace 5f 49 4e 54 45 47 45 52 20 7c 7c 20 70 45 78 70 _INTEGER || pExp
20acf 72 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b r->pLeft->op==TK
20ad0 5f 46 4c 4f 41 54 29 20 29 7b 0a 20 20 20 20 70 _FLOAT) ){. p
20ad1 45 78 70 72 20 3d 20 70 45 78 70 72 2d 3e 70 4c Expr = pExpr->pL
20ad2 65 66 74 3b 0a 20 20 20 20 6f 70 20 3d 20 70 45 eft;. op = pE
20ad3 78 70 72 2d 3e 6f 70 3b 0a 20 20 20 20 6e 65 67 xpr->op;. neg
20ad4 49 6e 74 20 3d 20 2d 31 3b 0a 20 20 20 20 7a 4e Int = -1;. zN
20ad5 65 67 20 3d 20 22 2d 22 3b 0a 20 20 7d 0a 0a 20 eg = "-";. }..
20ad6 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 54 52 49 if( op==TK_STRI
20ad7 4e 47 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c 4f NG || op==TK_FLO
20ad8 41 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 54 AT || op==TK_INT
20ad9 45 47 45 52 20 29 7b 0a 20 20 20 20 70 56 61 6c EGER ){. pVal
20ada 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e = sqlite3ValueN
20adb 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 ew(db);. if(
20adc 70 56 61 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e pVal==0 ) goto n
20add 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69 66 28 20 45 o_mem;. if( E
20ade 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
20adf 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 Expr, EP_IntValu
20ae0 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 e) ){. sqli
20ae1 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 te3VdbeMemSetInt
20ae2 36 34 28 70 56 61 6c 2c 20 28 69 36 34 29 70 45 64(pVal, (i64)pE
20ae3 78 70 72 2d 3e 75 2e 69 56 61 6c 75 65 2a 6e 65 xpr->u.iValue*ne
20ae4 67 49 6e 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 gInt);. }else
20ae5 7b 0a 20 20 20 20 20 20 7a 56 61 6c 20 3d 20 73 {. zVal = s
20ae6 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 qlite3MPrintf(db
20ae7 2c 20 22 25 73 25 73 22 2c 20 7a 4e 65 67 2c 20 , "%s%s", zNeg,
20ae8 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 pExpr->u.zToken)
20ae9 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 56 61 6c ;. if( zVal
20aea 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 ==0 ) goto no_me
20aeb 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 m;. sqlite3
20aec 56 61 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c ValueSetStr(pVal
20aed 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 4c 49 , -1, zVal, SQLI
20aee 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f TE_UTF8, SQLITE_
20aef 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 DYNAMIC);.
20af0 69 66 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 if( op==TK_FLOAT
20af1 20 29 20 70 56 61 6c 2d 3e 74 79 70 65 20 3d 20 ) pVal->type =
20af2 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 SQLITE_FLOAT;.
20af3 20 20 7d 0a 20 20 20 20 69 66 28 20 28 6f 70 3d }. if( (op=
20af4 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 6f =TK_INTEGER || o
20af5 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26 26 p==TK_FLOAT ) &&
20af6 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 affinity==SQLIT
20af7 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 E_AFF_NONE ){.
20af8 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 sqlite3Value
20af9 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56 ApplyAffinity(pV
20afa 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e al, SQLITE_AFF_N
20afb 55 4d 45 52 49 43 2c 20 53 51 4c 49 54 45 5f 55 UMERIC, SQLITE_U
20afc 54 46 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b TF8);. }else{
20afd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 . sqlite3Va
20afe 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 lueApplyAffinity
20aff 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c (pVal, affinity,
20b00 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 SQLITE_UTF8);.
20b01 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 56 61 }. if( pVa
20b02 6c 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f l->flags & (MEM_
20b03 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 20 Int|MEM_Real) )
20b04 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pVal->flags &= ~
20b05 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 MEM_Str;. if(
20b06 20 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 enc!=SQLITE_UTF
20b07 38 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 8 ){. sqlit
20b08 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
20b09 64 69 6e 67 28 70 56 61 6c 2c 20 65 6e 63 29 3b ding(pVal, enc);
20b0a 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
20b0b 66 28 20 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 f( op==TK_UMINUS
20b0c 20 29 20 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 ) {. /* This
20b0d 20 62 72 61 6e 63 68 20 68 61 70 70 65 6e 73 20 branch happens
20b0e 66 6f 72 20 6d 75 6c 74 69 70 6c 65 20 6e 65 67 for multiple neg
20b0f 61 74 69 76 65 20 73 69 67 6e 73 2e 20 20 45 78 ative signs. Ex
20b10 3a 20 2d 28 2d 35 29 20 2a 2f 0a 20 20 20 20 69 : -(-5) */. i
20b11 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 f( SQLITE_OK==sq
20b12 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 lite3ValueFromEx
20b13 70 72 28 64 62 2c 70 45 78 70 72 2d 3e 70 4c 65 pr(db,pExpr->pLe
20b14 66 74 2c 65 6e 63 2c 61 66 66 69 6e 69 74 79 2c ft,enc,affinity,
20b15 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 20 &pVal) ){.
20b16 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 sqlite3VdbeMemNu
20b17 6d 65 72 69 66 79 28 70 56 61 6c 29 3b 0a 20 20 merify(pVal);.
20b18 20 20 20 20 69 66 28 20 70 56 61 6c 2d 3e 75 2e if( pVal->u.
20b19 69 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 i==SMALLEST_INT6
20b1a 34 20 29 7b 0a 20 20 20 20 20 20 20 20 70 56 61 4 ){. pVa
20b1b 6c 2d 3e 66 6c 61 67 73 20 26 3d 20 4d 45 4d 5f l->flags &= MEM_
20b1c 49 6e 74 3b 0a 20 20 20 20 20 20 20 20 70 56 61 Int;. pVa
20b1d 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f l->flags |= MEM_
20b1e 52 65 61 6c 3b 0a 20 20 20 20 20 20 20 20 70 56 Real;. pV
20b1f 61 6c 2d 3e 72 20 3d 20 28 64 6f 75 62 6c 65 29 al->r = (double)
20b20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 3b 0a 20 LARGEST_INT64;.
20b21 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
20b22 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69 20 3d 20 pVal->u.i =
20b23 2d 70 56 61 6c 2d 3e 75 2e 69 3b 0a 20 20 20 20 -pVal->u.i;.
20b24 20 20 7d 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e }. pVal->
20b25 72 20 3d 20 2d 70 56 61 6c 2d 3e 72 3b 0a 20 20 r = -pVal->r;.
20b26 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 sqlite3Value
20b27 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56 ApplyAffinity(pV
20b28 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e al, affinity, en
20b29 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 c);. }. }els
20b2a 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 4e 55 4c e if( op==TK_NUL
20b2b 4c 20 29 7b 0a 20 20 20 20 70 56 61 6c 20 3d 20 L ){. pVal =
20b2c 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 sqlite3ValueNew(
20b2d 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70 56 61 db);. if( pVa
20b2e 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d l==0 ) goto no_m
20b2f 65 6d 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 em;. }.#ifndef
20b30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 SQLITE_OMIT_BLOB
20b31 5f 4c 49 54 45 52 41 4c 0a 20 20 65 6c 73 65 20 _LITERAL. else
20b32 69 66 28 20 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 if( op==TK_BLOB
20b33 29 7b 0a 20 20 20 20 69 6e 74 20 6e 56 61 6c 3b ){. int nVal;
20b34 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 . assert( pEx
20b35 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d pr->u.zToken[0]=
20b36 3d 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 75 ='x' || pExpr->u
20b37 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20 .zToken[0]=='X'
20b38 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
20b39 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 Expr->u.zToken[1
20b3a 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 70 ]=='\'' );. p
20b3b 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c Val = sqlite3Val
20b3c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 ueNew(db);. i
20b3d 66 28 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20 f( !pVal ) goto
20b3e 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7a 56 61 6c no_mem;. zVal
20b3f 20 3d 20 26 70 45 78 70 72 2d 3e 75 2e 7a 54 6f = &pExpr->u.zTo
20b40 6b 65 6e 5b 32 5d 3b 0a 20 20 20 20 6e 56 61 6c ken[2];. nVal
20b41 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
20b42 33 30 28 7a 56 61 6c 29 2d 31 3b 0a 20 20 20 20 30(zVal)-1;.
20b43 61 73 73 65 72 74 28 20 7a 56 61 6c 5b 6e 56 61 assert( zVal[nVa
20b44 6c 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 l]=='\'' );.
20b45 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
20b46 74 53 74 72 28 70 56 61 6c 2c 20 73 71 6c 69 74 tStr(pVal, sqlit
20b47 65 33 48 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20 e3HexToBlob(db,
20b48 7a 56 61 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61 zVal, nVal), nVa
20b49 6c 2f 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 l/2,.
20b4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0,
20b4b 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 SQLITE_DYNAMIC)
20b4c 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
20b4d 69 66 28 20 70 56 61 6c 20 29 7b 0a 20 20 20 20 if( pVal ){.
20b4e 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 sqlite3VdbeMemSt
20b4f 6f 72 65 54 79 70 65 28 70 56 61 6c 29 3b 0a 20 oreType(pVal);.
20b50 20 7d 0a 20 20 2a 70 70 56 61 6c 20 3d 20 70 56 }. *ppVal = pV
20b51 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c al;. return SQL
20b52 49 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a ITE_OK;..no_mem:
20b53 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 . db->mallocFai
20b54 6c 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 led = 1;. sqlit
20b55 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 56 61 e3DbFree(db, zVa
20b56 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c l);. sqlite3Val
20b57 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 ueFree(pVal);.
20b58 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20 72 65 *ppVal = 0;. re
20b59 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
20b5a 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e M;.}../*.** Chan
20b5b 67 65 20 74 68 65 20 73 74 72 69 6e 67 20 76 61 ge the string va
20b5c 6c 75 65 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 lue of an sqlite
20b5d 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 3_value object.*
20b5e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20b5f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c void sqlite3Val
20b60 75 65 53 65 74 53 74 72 28 0a 20 20 73 71 6c 69 ueSetStr(. sqli
20b61 74 65 33 5f 76 61 6c 75 65 20 2a 76 2c 20 20 20 te3_value *v,
20b62 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 62 65 /* Value to be
20b63 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c set */. int n,
20b64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20b65 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 /* Length of str
20b66 69 6e 67 20 7a 20 2a 2f 0a 20 20 63 6f 6e 73 74 ing z */. const
20b67 20 76 6f 69 64 20 2a 7a 2c 20 20 20 20 20 20 20 void *z,
20b68 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 /* Text of the
20b69 6e 65 77 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 new string */.
20b6a 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 u8 enc,
20b6b 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e /* Encodin
20b6c 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 76 6f g to use */. vo
20b6d 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a id (*xDel)(void*
20b6e 29 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f ) /* Destructo
20b6f 72 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 r for the string
20b70 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 76 20 29 */.){. if( v )
20b71 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
20b72 65 74 53 74 72 28 28 4d 65 6d 20 2a 29 76 2c 20 etStr((Mem *)v,
20b73 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 65 6c 29 z, n, enc, xDel)
20b74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
20b75 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 an sqlite3_value
20b76 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 object.*/.SQLIT
20b77 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
20b78 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 qlite3ValueFree(
20b79 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 sqlite3_value *v
20b7a 29 7b 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 ){. if( !v ) re
20b7b 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 turn;. sqlite3V
20b7c 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 28 4d dbeMemRelease((M
20b7d 65 6d 20 2a 29 76 29 3b 0a 20 20 73 71 6c 69 74 em *)v);. sqlit
20b7e 65 33 44 62 46 72 65 65 28 28 28 4d 65 6d 2a 29 e3DbFree(((Mem*)
20b7f 76 29 2d 3e 64 62 2c 20 76 29 3b 0a 7d 0a 0a 2f v)->db, v);.}../
20b80 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
20b81 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
20b82 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 in the sqlite3_v
20b83 61 6c 75 65 20 6f 62 6a 65 63 74 20 61 73 73 75 alue object assu
20b84 6d 69 6e 67 0a 2a 2a 20 74 68 61 74 20 69 74 20 ming.** that it
20b85 75 73 65 73 20 74 68 65 20 65 6e 63 6f 64 69 6e uses the encodin
20b86 67 20 22 65 6e 63 22 0a 2a 2f 0a 53 51 4c 49 54 g "enc".*/.SQLIT
20b87 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
20b88 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 lite3ValueBytes(
20b89 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
20b8a 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 Val, u8 enc){.
20b8b 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 Mem *p = (Mem*)p
20b8c 56 61 6c 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 Val;. if( (p->f
20b8d 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 lags & MEM_Blob)
20b8e 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 56 61 !=0 || sqlite3Va
20b8f 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 65 6e lueText(pVal, en
20b90 63 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d c) ){. if( p-
20b91 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 >flags & MEM_Zer
20b92 6f 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 o ){. retur
20b93 6e 20 70 2d 3e 6e 20 2b 20 70 2d 3e 75 2e 6e 5a n p->n + p->u.nZ
20b94 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ero;. }else{.
20b95 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e return p->
20b96 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 n;. }. }. r
20b97 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a eturn 0;.}../***
20b98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
20b99 6f 66 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a of vdbemem.c ***
20b9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
20b9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
20b9e 6e 20 66 69 6c 65 20 76 64 62 65 61 75 78 2e 63 n file vdbeaux.c
20b9f 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
20ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ba1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
20ba2 2a 20 32 30 30 33 20 53 65 70 74 65 6d 62 65 72 * 2003 September
20ba3 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 6.**.** The aut
20ba4 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
20ba5 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
20ba6 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
20ba7 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
20ba8 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
20ba9 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
20baa 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
20bab 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
20bac 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
20bad 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
20bae 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
20baf 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
20bb0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
20bb1 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
20bb2 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
20bb3 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
20bb4 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
20bb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20bb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20bb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20bb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20bb9 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
20bba 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 ontains code use
20bbb 64 20 66 6f 72 20 63 72 65 61 74 69 6e 67 2c 20 d for creating,
20bbc 64 65 73 74 72 6f 79 69 6e 67 2c 20 61 6e 64 20 destroying, and
20bbd 70 6f 70 75 6c 61 74 69 6e 67 0a 2a 2a 20 61 20 populating.** a
20bbe 56 44 42 45 20 28 6f 72 20 61 6e 20 22 73 71 6c VDBE (or an "sql
20bbf 69 74 65 33 5f 73 74 6d 74 22 20 61 73 20 69 74 ite3_stmt" as it
20bc0 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 20 74 68 65 is known to the
20bc1 20 6f 75 74 73 69 64 65 20 77 6f 72 6c 64 2e 29 outside world.)
20bc2 20 20 50 72 69 6f 72 0a 2a 2a 20 74 6f 20 76 65 Prior.** to ve
20bc3 72 73 69 6f 6e 20 32 2e 38 2e 37 2c 20 61 6c 6c rsion 2.8.7, all
20bc4 20 74 68 69 73 20 63 6f 64 65 20 77 61 73 20 63 this code was c
20bc5 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 ombined into the
20bc6 20 76 64 62 65 2e 63 20 73 6f 75 72 63 65 20 66 vdbe.c source f
20bc7 69 6c 65 2e 0a 2a 2a 20 42 75 74 20 74 68 61 74 ile..** But that
20bc8 20 66 69 6c 65 20 77 61 73 20 67 65 74 74 69 6e file was gettin
20bc9 67 20 74 6f 6f 20 62 69 67 20 73 6f 20 74 68 69 g too big so thi
20bca 73 20 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65 s subroutines we
20bcb 72 65 20 73 70 6c 69 74 20 6f 75 74 2e 0a 2a 2f re split out..*/
20bcc 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64 ..../*.** When d
20bcd 65 62 75 67 67 69 6e 67 20 74 68 65 20 63 6f 64 ebugging the cod
20bce 65 20 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 61 e generator in a
20bcf 20 73 79 6d 62 6f 6c 69 63 20 64 65 62 75 67 67 symbolic debugg
20bd0 65 72 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73 er, one can.** s
20bd1 65 74 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 et the sqlite3Vd
20bd2 62 65 41 64 64 6f 70 54 72 61 63 65 20 74 6f 20 beAddopTrace to
20bd3 31 20 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65 1 and all opcode
20bd4 73 20 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65 s will be printe
20bd5 64 0a 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65 d.** as they are
20bd6 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e added to the in
20bd7 73 74 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d struction stream
20bd8 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
20bd9 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
20bda 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
20bdb 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 te3VdbeAddopTrac
20bdc 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a e = 0;.#endif...
20bdd 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e /*.** Create a n
20bde 65 77 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 ew virtual datab
20bdf 61 73 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 ase engine..*/.S
20be0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 QLITE_PRIVATE Vd
20be1 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 be *sqlite3VdbeC
20be2 72 65 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 reate(sqlite3 *d
20be3 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 b){. Vdbe *p;.
20be4 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 p = sqlite3DbMa
20be5 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a llocZero(db, siz
20be6 65 6f 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69 eof(Vdbe) );. i
20be7 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
20be8 20 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 0;. p->db = db
20be9 3b 0a 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 ;. if( db->pVdb
20bea 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64 e ){. db->pVd
20beb 62 65 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 be->pPrev = p;.
20bec 20 7d 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 }. p->pNext =
20bed 64 62 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e db->pVdbe;. p->
20bee 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d pPrev = 0;. db-
20bef 3e 70 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d >pVdbe = p;. p-
20bf0 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 >magic = VDBE_MA
20bf1 47 49 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 GIC_INIT;. retu
20bf2 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn p;.}../*.** R
20bf3 65 6d 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20 emember the SQL
20bf4 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65 string for a pre
20bf5 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e pared statement.
20bf6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20bf7 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
20bf8 64 62 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a dbeSetSql(Vdbe *
20bf9 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a p, const char *z
20bfa 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 73 50 , int n, int isP
20bfb 72 65 70 61 72 65 56 32 29 7b 0a 20 20 61 73 73 repareV2){. ass
20bfc 65 72 74 28 20 69 73 50 72 65 70 61 72 65 56 32 ert( isPrepareV2
20bfd 3d 3d 31 20 7c 7c 20 69 73 50 72 65 70 61 72 65 ==1 || isPrepare
20bfe 56 32 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 V2==0 );. if( p
20bff 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 ==0 ) return;.#i
20c00 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
20c01 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26 20 _OMIT_TRACE) &&
20c02 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
20c03 45 4e 41 42 4c 45 5f 53 51 4c 4c 4f 47 29 0a 20 ENABLE_SQLLOG).
20c04 20 69 66 28 20 21 69 73 50 72 65 70 61 72 65 56 if( !isPrepareV
20c05 32 20 29 20 72 65 74 75 72 6e 3b 0a 23 65 6e 64 2 ) return;.#end
20c06 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e if. assert( p->
20c07 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e zSql==0 );. p->
20c08 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 zSql = sqlite3Db
20c09 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 7a StrNDup(p->db, z
20c0a 2c 20 6e 29 3b 0a 20 20 70 2d 3e 69 73 50 72 65 , n);. p->isPre
20c0b 70 61 72 65 56 32 20 3d 20 28 75 38 29 69 73 50 pareV2 = (u8)isP
20c0c 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 2f 2a 0a repareV2;.}../*.
20c0d 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 53 51 ** Return the SQ
20c0e 4c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 L associated wit
20c0f 68 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61 h a prepared sta
20c10 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 tement.*/.SQLITE
20c11 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
20c12 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73 71 6c *sqlite3_sql(sql
20c13 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
20c14 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 ){. Vdbe *p = (
20c15 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
20c16 72 65 74 75 72 6e 20 28 70 20 26 26 20 70 2d 3e return (p && p->
20c17 69 73 50 72 65 70 61 72 65 56 32 29 20 3f 20 70 isPrepareV2) ? p
20c18 2d 3e 7a 53 71 6c 20 3a 20 30 3b 0a 7d 0a 0a 2f ->zSql : 0;.}../
20c19 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63 6f *.** Swap all co
20c1a 6e 74 65 6e 74 20 62 65 74 77 65 65 6e 20 74 77 ntent between tw
20c1b 6f 20 56 44 42 45 20 73 74 72 75 63 74 75 72 65 o VDBE structure
20c1c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
20c1d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
20c1e 33 56 64 62 65 53 77 61 70 28 56 64 62 65 20 2a 3VdbeSwap(Vdbe *
20c1f 70 41 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a 20 pA, Vdbe *pB){.
20c20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d 70 Vdbe tmp, *pTmp
20c21 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a ;. char *zTmp;.
20c22 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 2a tmp = *pA;. *
20c23 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 20 pA = *pB;. *pB
20c24 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 = tmp;. pTmp =
20c25 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 2d pA->pNext;. pA-
20c26 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e 65 >pNext = pB->pNe
20c27 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 20 xt;. pB->pNext
20c28 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 3d = pTmp;. pTmp =
20c29 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 41 pA->pPrev;. pA
20c2a 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 50 ->pPrev = pB->pP
20c2b 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 76 rev;. pB->pPrev
20c2c 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 20 = pTmp;. zTmp
20c2d 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 41 = pA->zSql;. pA
20c2e 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 71 ->zSql = pB->zSq
20c2f 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d 20 l;. pB->zSql =
20c30 7a 54 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50 72 zTmp;. pB->isPr
20c31 65 70 61 72 65 56 32 20 3d 20 70 41 2d 3e 69 73 epareV2 = pA->is
20c32 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23 69 PrepareV2;.}..#i
20c33 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
20c34 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 61 G./*.** Turn tra
20c35 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a 2a cing on or off.*
20c36 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20c37 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
20c38 65 54 72 61 63 65 28 56 64 62 65 20 2a 70 2c 20 eTrace(Vdbe *p,
20c39 46 49 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 20 FILE *trace){.
20c3a 70 2d 3e 74 72 61 63 65 20 3d 20 74 72 61 63 65 p->trace = trace
20c3b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
20c3c 2a 20 52 65 73 69 7a 65 20 74 68 65 20 56 64 62 * Resize the Vdb
20c3d 65 2e 61 4f 70 20 61 72 72 61 79 20 73 6f 20 74 e.aOp array so t
20c3e 68 61 74 20 69 74 20 69 73 20 61 74 20 6c 65 61 hat it is at lea
20c3f 73 74 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72 st one op larger
20c40 20 74 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 73 than .** it was
20c41 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 ..**.** If an ou
20c42 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f t-of-memory erro
20c43 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 r occurs while r
20c44 65 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61 esizing the arra
20c45 79 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c y, return.** SQL
20c46 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68 ITE_NOMEM. In th
20c47 69 73 20 63 61 73 65 20 56 64 62 65 2e 61 4f 70 is case Vdbe.aOp
20c48 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c and Vdbe.nOpAll
20c49 6f 63 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 6e oc remain .** un
20c4a 63 68 61 6e 67 65 64 20 28 74 68 69 73 20 69 73 changed (this is
20c4b 20 73 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 63 so that any opc
20c4c 6f 64 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c odes already all
20c4d 6f 63 61 74 65 64 20 63 61 6e 20 62 65 20 0a 2a ocated can be .*
20c4e 2a 20 63 6f 72 72 65 63 74 6c 79 20 64 65 61 6c * correctly deal
20c4f 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 located along wi
20c50 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 th the rest of t
20c51 68 65 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61 he Vdbe)..*/.sta
20c52 74 69 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 72 tic int growOpAr
20c53 72 61 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 ray(Vdbe *p){.
20c54 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 20 VdbeOp *pNew;.
20c55 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e 6e int nNew = (p->n
20c56 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70 OpAlloc ? p->nOp
20c57 41 6c 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 28 Alloc*2 : (int)(
20c58 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29 1024/sizeof(Op))
20c59 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 );. pNew = sqli
20c5a 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e te3DbRealloc(p->
20c5b 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 77 db, p->aOp, nNew
20c5c 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20 *sizeof(Op));.
20c5d 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 if( pNew ){.
20c5e 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73 71 p->nOpAlloc = sq
20c5f 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a lite3DbMallocSiz
20c60 65 28 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f 73 e(p->db, pNew)/s
20c61 69 7a 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20 70 izeof(Op);. p
20c62 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20 20 ->aOp = pNew;.
20c63 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 4e 65 77 }. return (pNew
20c64 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 ? SQLITE_OK : S
20c65 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a QLITE_NOMEM);.}.
20c66 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 ./*.** Add a new
20c67 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 instruction to
20c68 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 73 74 the list of inst
20c69 72 75 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 ructions current
20c6a 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e in the.** VDBE.
20c6b 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 64 64 Return the add
20c6c 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 77 20 ress of the new
20c6d 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a instruction..**.
20c6e 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 3a 0a 2a ** Parameters:.*
20c6f 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20 20 20 20 *.** p
20c70 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 Pointer
20c71 74 6f 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a 2a to the VDBE.**.*
20c72 2a 20 20 20 20 6f 70 20 20 20 20 20 20 20 20 20 * op
20c73 20 20 20 20 20 54 68 65 20 6f 70 63 6f 64 65 20 The opcode
20c74 66 6f 72 20 74 68 69 73 20 69 6e 73 74 72 75 63 for this instruc
20c75 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70 31 tion.**.** p1
20c76 2c 20 70 32 2c 20 70 33 20 20 20 20 20 20 4f 70 , p2, p3 Op
20c77 65 72 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73 65 erands.**.** Use
20c78 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 the sqlite3Vdbe
20c79 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20 66 ResolveLabel() f
20c7a 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 78 20 61 unction to fix a
20c7b 6e 20 61 64 64 72 65 73 73 20 61 6e 64 0a 2a 2a n address and.**
20c7c 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 the sqlite3Vdbe
20c7d 43 68 61 6e 67 65 50 34 28 29 20 66 75 6e 63 74 ChangeP4() funct
20c7e 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 20 74 68 ion to change th
20c7f 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 e value of the P
20c80 34 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a 2f 4.** operand..*/
20c81 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20c82 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
20c83 64 64 4f 70 33 28 56 64 62 65 20 2a 70 2c 20 69 ddOp3(Vdbe *p, i
20c84 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 nt op, int p1, i
20c85 6e 74 20 70 32 2c 20 69 6e 74 20 70 33 29 7b 0a nt p2, int p3){.
20c86 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 4f int i;. VdbeO
20c87 70 20 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20 70 p *pOp;.. i = p
20c88 2d 3e 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74 28 ->nOp;. assert(
20c89 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
20c8a 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 MAGIC_INIT );.
20c8b 61 73 73 65 72 74 28 20 6f 70 3e 30 20 26 26 20 assert( op>0 &&
20c8c 6f 70 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 28 op<0xff );. if(
20c8d 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 20 p->nOpAlloc<=i
20c8e 29 7b 0a 20 20 20 20 69 66 28 20 67 72 6f 77 4f ){. if( growO
20c8f 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 pArray(p) ){.
20c90 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
20c91 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70 2b }. }. p->nOp+
20c92 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 +;. pOp = &p->a
20c93 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f 70 Op[i];. pOp->op
20c94 63 6f 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a 20 code = (u8)op;.
20c95 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 20 pOp->p5 = 0;.
20c96 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 20 pOp->p1 = p1;.
20c97 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 20 pOp->p2 = p2;.
20c98 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 20 pOp->p3 = p3;.
20c99 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 pOp->p4.p = 0;.
20c9a 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 pOp->p4type = P
20c9b 34 5f 4e 4f 54 55 53 45 44 3b 0a 23 69 66 64 65 4_NOTUSED;.#ifde
20c9c 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
20c9d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d pOp->zComment =
20c9e 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 0;. if( sqlite
20c9f 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 3VdbeAddopTrace
20ca0 29 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 ) sqlite3VdbePri
20ca1 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 2d 3e 61 ntOp(0, i, &p->a
20ca2 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 66 0a 23 Op[i]);.#endif.#
20ca3 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 ifdef VDBE_PROFI
20ca4 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 LE. pOp->cycles
20ca5 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 63 6e 74 = 0;. pOp->cnt
20ca6 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 = 0;.#endif. r
20ca7 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54 eturn i;.}.SQLIT
20ca8 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
20ca9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 lite3VdbeAddOp0(
20caa 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 29 Vdbe *p, int op)
20cab 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
20cac 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 e3VdbeAddOp3(p,
20cad 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 7d 0a op, 0, 0, 0);.}.
20cae 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
20caf 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
20cb0 64 4f 70 31 28 56 64 62 65 20 2a 70 2c 20 69 6e dOp1(Vdbe *p, in
20cb1 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 7b 0a 20 t op, int p1){.
20cb2 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
20cb3 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c dbeAddOp3(p, op,
20cb4 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d 0a 53 51 p1, 0, 0);.}.SQ
20cb5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
20cb6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20cb7 70 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 p2(Vdbe *p, int
20cb8 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74 20 op, int p1, int
20cb9 70 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 p2){. return sq
20cba 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
20cbb 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 30 p, op, p1, p2, 0
20cbc 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 );.}.../*.** Add
20cbd 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 61 74 20 an opcode that
20cbe 69 6e 63 6c 75 64 65 73 20 74 68 65 20 70 34 20 includes the p4
20cbf 76 61 6c 75 65 20 61 73 20 61 20 70 6f 69 6e 74 value as a point
20cc0 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
20cc1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
20cc2 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20 20 56 3VdbeAddOp4(. V
20cc3 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 dbe *p,
20cc4 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6f 70 /* Add the op
20cc5 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 4d 20 code to this VM
20cc6 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 */. int op,
20cc7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
20cc8 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 new opcode */.
20cc9 69 6e 74 20 70 31 2c 20 20 20 20 20 20 20 20 20 int p1,
20cca 20 20 20 20 2f 2a 20 54 68 65 20 50 31 20 6f 70 /* The P1 op
20ccb 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 erand */. int p
20ccc 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2, /
20ccd 2a 20 54 68 65 20 50 32 20 6f 70 65 72 61 6e 64 * The P2 operand
20cce 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c 20 20 20 */. int p3,
20ccf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
20cd0 20 50 33 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 P3 operand */.
20cd1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 const char *zP4
20cd2 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 34 20 6f , /* The P4 o
20cd3 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 perand */. int
20cd4 70 34 74 79 70 65 20 20 20 20 20 20 20 20 20 20 p4type
20cd5 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 20 74 79 /* P4 operand ty
20cd6 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 pe */.){. int a
20cd7 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ddr = sqlite3Vdb
20cd8 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 eAddOp3(p, op, p
20cd9 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 20 73 71 1, p2, p3);. sq
20cda 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
20cdb 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 34 2c 20 4(p, addr, zP4,
20cdc 70 34 74 79 70 65 29 3b 0a 20 20 72 65 74 75 72 p4type);. retur
20cdd 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n addr;.}../*.**
20cde 20 41 64 64 20 61 6e 20 4f 50 5f 50 61 72 73 65 Add an OP_Parse
20cdf 53 63 68 65 6d 61 20 6f 70 63 6f 64 65 2e 20 20 Schema opcode.
20ce0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
20ce1 62 72 6f 6b 65 6e 20 6f 75 74 20 66 72 6f 6d 0a broken out from.
20ce2 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 ** sqlite3VdbeAd
20ce3 64 4f 70 34 28 29 20 73 69 6e 63 65 20 69 74 20 dOp4() since it
20ce4 6e 65 65 64 73 20 74 6f 20 61 6c 73 6f 20 6e 65 needs to also ne
20ce5 65 64 73 20 74 6f 20 6d 61 72 6b 20 61 6c 6c 20 eds to mark all
20ce6 62 74 72 65 65 73 0a 2a 2a 20 61 73 20 68 61 76 btrees.** as hav
20ce7 69 6e 67 20 62 65 65 6e 20 75 73 65 64 2e 0a 2a ing been used..*
20ce8 2a 0a 2a 2a 20 54 68 65 20 7a 57 68 65 72 65 20 *.** The zWhere
20ce9 73 74 72 69 6e 67 20 6d 75 73 74 20 68 61 76 65 string must have
20cea 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 been obtained f
20ceb 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c rom sqlite3_mall
20cec 6f 63 28 29 2e 0a 2a 2a 20 54 68 69 73 20 72 6f oc()..** This ro
20ced 75 74 69 6e 65 20 77 69 6c 6c 20 74 61 6b 65 20 utine will take
20cee 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 74 68 65 ownership of the
20cef 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 allocated memor
20cf0 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
20cf1 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
20cf2 33 56 64 62 65 41 64 64 50 61 72 73 65 53 63 68 3VdbeAddParseSch
20cf3 65 6d 61 4f 70 28 56 64 62 65 20 2a 70 2c 20 69 emaOp(Vdbe *p, i
20cf4 6e 74 20 69 44 62 2c 20 63 68 61 72 20 2a 7a 57 nt iDb, char *zW
20cf5 68 65 72 65 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a here){. int j;.
20cf6 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c int addr = sql
20cf7 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 ite3VdbeAddOp3(p
20cf8 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 , OP_ParseSchema
20cf9 2c 20 69 44 62 2c 20 30 2c 20 30 29 3b 0a 20 20 , iDb, 0, 0);.
20cfa 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
20cfb 65 50 34 28 70 2c 20 61 64 64 72 2c 20 7a 57 68 eP4(p, addr, zWh
20cfc 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 ere, P4_DYNAMIC)
20cfd 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 ;. for(j=0; j<p
20cfe 2d 3e 64 62 2d 3e 6e 44 62 3b 20 6a 2b 2b 29 20 ->db->nDb; j++)
20cff 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 sqlite3VdbeUsesB
20d00 74 72 65 65 28 70 2c 20 6a 29 3b 0a 7d 0a 0a 2f tree(p, j);.}../
20d01 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63 6f *.** Add an opco
20d02 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65 73 de that includes
20d03 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61 73 the p4 value as
20d04 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a an integer..*/.
20d05 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
20d06 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
20d07 64 4f 70 34 49 6e 74 28 0a 20 20 56 64 62 65 20 dOp4Int(. Vdbe
20d08 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f *p, /
20d09 2a 20 41 64 64 20 74 68 65 20 6f 70 63 6f 64 65 * Add the opcode
20d0a 20 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 to this VM */.
20d0b 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 int op,
20d0c 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 /* The new
20d0d 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 opcode */. int
20d0e 70 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 p1,
20d0f 2f 2a 20 54 68 65 20 50 31 20 6f 70 65 72 61 6e /* The P1 operan
20d10 64 20 2a 2f 0a 20 20 69 6e 74 20 70 32 2c 20 20 d */. int p2,
20d11 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
20d12 65 20 50 32 20 6f 70 65 72 61 6e 64 20 2a 2f 0a e P2 operand */.
20d13 20 20 69 6e 74 20 70 33 2c 20 20 20 20 20 20 20 int p3,
20d14 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33 20 /* The P3
20d15 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 operand */. int
20d16 20 70 34 20 20 20 20 20 20 20 20 20 20 20 20 20 p4
20d17 20 2f 2a 20 54 68 65 20 50 34 20 6f 70 65 72 61 /* The P4 opera
20d18 6e 64 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 nd as an integer
20d19 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64 64 */.){. int add
20d1a 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 r = sqlite3VdbeA
20d1b 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c ddOp3(p, op, p1,
20d1c 20 70 32 2c 20 70 33 29 3b 0a 20 20 73 71 6c 69 p2, p3);. sqli
20d1d 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 te3VdbeChangeP4(
20d1e 70 2c 20 61 64 64 72 2c 20 53 51 4c 49 54 45 5f p, addr, SQLITE_
20d1f 49 4e 54 5f 54 4f 5f 50 54 52 28 70 34 29 2c 20 INT_TO_PTR(p4),
20d20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 72 65 74 P4_INT32);. ret
20d21 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a urn addr;.}../*.
20d22 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
20d23 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 66 symbolic label f
20d24 6f 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f or an instructio
20d25 6e 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74 n that has yet t
20d26 6f 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 20 o be.** coded.
20d27 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 The symbolic lab
20d28 65 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 el is really jus
20d29 74 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d t a negative num
20d2a 62 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 62 ber. The.** lab
20d2b 65 6c 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 el can be used a
20d2c 73 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 6f s the P2 value o
20d2d 66 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 f an operation.
20d2e 20 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a 20 Later, when.**
20d2f 74 68 65 20 6c 61 62 65 6c 20 69 73 20 72 65 73 the label is res
20d30 6f 6c 76 65 64 20 74 6f 20 61 20 73 70 65 63 69 olved to a speci
20d31 66 69 63 20 61 64 64 72 65 73 73 2c 20 74 68 65 fic address, the
20d32 20 56 44 42 45 20 77 69 6c 6c 20 73 63 61 6e 0a VDBE will scan.
20d33 2a 2a 20 74 68 72 6f 75 67 68 20 69 74 73 20 6f ** through its o
20d34 70 65 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 6e peration list an
20d35 64 20 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 6c d change all val
20d36 75 65 73 20 6f 66 20 50 32 20 77 68 69 63 68 20 ues of P2 which
20d37 6d 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 62 match.** the lab
20d38 65 6c 20 69 6e 74 6f 20 74 68 65 20 72 65 73 6f el into the reso
20d39 6c 76 65 64 20 61 64 64 72 65 73 73 2e 0a 2a 2a lved address..**
20d3a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6b 6e 6f .** The VDBE kno
20d3b 77 73 20 74 68 61 74 20 61 20 50 32 20 76 61 6c ws that a P2 val
20d3c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 20 62 65 ue is a label be
20d3d 63 61 75 73 65 20 6c 61 62 65 6c 73 20 61 72 65 cause labels are
20d3e 0a 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 74 .** always negat
20d3f 69 76 65 20 61 6e 64 20 50 32 20 76 61 6c 75 65 ive and P2 value
20d40 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f s are suppose to
20d41 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 be non-negative
20d42 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e 65 ..** Hence, a ne
20d43 67 61 74 69 76 65 20 50 32 20 76 61 6c 75 65 20 gative P2 value
20d44 69 73 20 61 20 6c 61 62 65 6c 20 74 68 61 74 20 is a label that
20d45 68 61 73 20 79 65 74 20 74 6f 20 62 65 20 72 65 has yet to be re
20d46 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 solved..**.** Ze
20d47 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 ro is returned i
20d48 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 f a malloc() fai
20d49 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ls..*/.SQLITE_PR
20d4a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
20d4b 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 3VdbeMakeLabel(V
20d4c 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 dbe *p){. int i
20d4d 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a = p->nLabel++;.
20d4e 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 assert( p->mag
20d4f 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 ic==VDBE_MAGIC_I
20d50 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 28 69 20 NIT );. if( (i
20d51 26 20 28 69 2d 31 29 29 3d 3d 30 20 29 7b 0a 20 & (i-1))==0 ){.
20d52 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 73 p->aLabel = s
20d53 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f qlite3DbReallocO
20d54 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e rFree(p->db, p->
20d55 61 4c 61 62 65 6c 2c 20 0a 20 20 20 20 20 20 20 aLabel, .
20d56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d58 28 69 2a 32 2b 31 29 2a 73 69 7a 65 6f 66 28 70 (i*2+1)*sizeof(p
20d59 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 29 3b 0a 20 ->aLabel[0]));.
20d5a 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 }. if( p->aLab
20d5b 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 el ){. p->aLa
20d5c 62 65 6c 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d bel[i] = -1;. }
20d5d 0a 20 20 72 65 74 75 72 6e 20 2d 31 2d 69 3b 0a . return -1-i;.
20d5e 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 }../*.** Resolve
20d5f 20 6c 61 62 65 6c 20 22 78 22 20 74 6f 20 62 65 label "x" to be
20d60 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 the address of
20d61 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 the next instruc
20d62 74 69 6f 6e 20 74 6f 0a 2a 2a 20 62 65 20 69 6e tion to.** be in
20d63 73 65 72 74 65 64 2e 20 20 54 68 65 20 70 61 72 serted. The par
20d64 61 6d 65 74 65 72 20 22 78 22 20 6d 75 73 74 20 ameter "x" must
20d65 68 61 76 65 20 62 65 65 6e 20 6f 62 74 61 69 6e have been obtain
20d66 65 64 20 66 72 6f 6d 0a 2a 2a 20 61 20 70 72 69 ed from.** a pri
20d67 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 or call to sqlit
20d68 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 e3VdbeMakeLabel(
20d69 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
20d6a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
20d6b 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 3VdbeResolveLabe
20d6c 6c 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 78 l(Vdbe *p, int x
20d6d 29 7b 0a 20 20 69 6e 74 20 6a 20 3d 20 2d 31 2d ){. int j = -1-
20d6e 78 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e x;. assert( p->
20d6f 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 magic==VDBE_MAGI
20d70 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 C_INIT );. asse
20d71 72 74 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 70 2d rt( j>=0 && j<p-
20d72 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20 20 69 66 28 >nLabel );. if(
20d73 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 p->aLabel ){.
20d74 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 6a 5d 20 3d p->aLabel[j] =
20d75 20 70 2d 3e 6e 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a p->nOp;. }.}..
20d76 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 74 68 65 20 56 /*.** Mark the V
20d77 44 42 45 20 61 73 20 6f 6e 65 20 74 68 61 74 20 DBE as one that
20d78 63 61 6e 20 6f 6e 6c 79 20 62 65 20 72 75 6e 20 can only be run
20d79 6f 6e 65 20 74 69 6d 65 2e 0a 2a 2f 0a 53 51 4c one time..*/.SQL
20d7a 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
20d7b 20 73 71 6c 69 74 65 33 56 64 62 65 52 75 6e 4f sqlite3VdbeRunO
20d7c 6e 6c 79 4f 6e 63 65 28 56 64 62 65 20 2a 70 29 nlyOnce(Vdbe *p)
20d7d 7b 0a 20 20 70 2d 3e 72 75 6e 4f 6e 6c 79 4f 6e {. p->runOnlyOn
20d7e 63 65 20 3d 20 31 3b 0a 7d 0a 0a 23 69 66 64 65 ce = 1;.}..#ifde
20d7f 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2f f SQLITE_DEBUG /
20d80 2a 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 4d * sqlite3AssertM
20d81 61 79 41 62 6f 72 74 28 29 20 6c 6f 67 69 63 20 ayAbort() logic
20d82 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f */../*.** The fo
20d83 6c 6c 6f 77 69 6e 67 20 74 79 70 65 20 61 6e 64 llowing type and
20d84 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 75 73 function are us
20d85 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 ed to iterate th
20d86 72 6f 75 67 68 20 61 6c 6c 20 6f 70 63 6f 64 65 rough all opcode
20d87 73 0a 2a 2a 20 69 6e 20 61 20 56 64 62 65 20 6d s.** in a Vdbe m
20d88 61 69 6e 20 70 72 6f 67 72 61 6d 20 61 6e 64 20 ain program and
20d89 65 61 63 68 20 6f 66 20 74 68 65 20 73 75 62 2d each of the sub-
20d8a 70 72 6f 67 72 61 6d 73 20 28 74 72 69 67 67 65 programs (trigge
20d8b 72 73 29 20 69 74 20 6d 61 79 20 0a 2a 2a 20 69 rs) it may .** i
20d8c 6e 76 6f 6b 65 20 64 69 72 65 63 74 6c 79 20 6f nvoke directly o
20d8d 72 20 69 6e 64 69 72 65 63 74 6c 79 2e 20 49 74 r indirectly. It
20d8e 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 should be used
20d8f 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
20d90 2a 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 2a 2a 20 * Op *pOp;.**
20d91 20 20 56 64 62 65 4f 70 49 74 65 72 20 73 49 74 VdbeOpIter sIt
20d92 65 72 3b 0a 2a 2a 0a 2a 2a 20 20 20 6d 65 6d 73 er;.**.** mems
20d93 65 74 28 26 73 49 74 65 72 2c 20 30 2c 20 73 69 et(&sIter, 0, si
20d94 7a 65 6f 66 28 73 49 74 65 72 29 29 3b 0a 2a 2a zeof(sIter));.**
20d95 20 20 20 73 49 74 65 72 2e 76 20 3d 20 76 3b 20 sIter.v = v;
20d96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d97 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 76 20 // v
20d98 69 73 20 6f 66 20 74 79 70 65 20 56 64 62 65 2a is of type Vdbe*
20d99 20 0a 2a 2a 20 20 20 77 68 69 6c 65 28 20 28 70 .** while( (p
20d9a 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65 78 74 28 Op = opIterNext(
20d9b 26 73 49 74 65 72 29 29 20 29 7b 0a 2a 2a 20 20 &sIter)) ){.**
20d9c 20 20 20 2f 2f 20 44 6f 20 73 6f 6d 65 74 68 69 // Do somethi
20d9d 6e 67 20 77 69 74 68 20 70 4f 70 0a 2a 2a 20 20 ng with pOp.**
20d9e 20 7d 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 44 }.** sqlite3D
20d9f 62 46 72 65 65 28 76 2d 3e 64 62 2c 20 73 49 74 bFree(v->db, sIt
20da0 65 72 2e 61 70 53 75 62 29 3b 0a 2a 2a 20 0a 2a er.apSub);.** .*
20da1 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
20da2 20 56 64 62 65 4f 70 49 74 65 72 20 56 64 62 65 VdbeOpIter Vdbe
20da3 4f 70 49 74 65 72 3b 0a 73 74 72 75 63 74 20 56 OpIter;.struct V
20da4 64 62 65 4f 70 49 74 65 72 20 7b 0a 20 20 56 64 dbeOpIter {. Vd
20da5 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 be *v;
20da6 20 20 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 /* Vdbe
20da7 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f to iterate thro
20da8 75 67 68 20 74 68 65 20 6f 70 63 6f 64 65 73 20 ugh the opcodes
20da9 6f 66 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 of */. SubProgr
20daa 61 6d 20 2a 2a 61 70 53 75 62 3b 20 20 20 20 20 am **apSub;
20dab 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 /* Array of s
20dac 75 62 70 72 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 ubprograms */.
20dad 69 6e 74 20 6e 53 75 62 3b 20 20 20 20 20 20 20 int nSub;
20dae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
20daf 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
20db0 69 6e 20 61 70 53 75 62 20 2a 2f 0a 20 20 69 6e in apSub */. in
20db1 74 20 69 41 64 64 72 3b 20 20 20 20 20 20 20 20 t iAddr;
20db2 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 /* Addr
20db3 65 73 73 20 6f 66 20 6e 65 78 74 20 69 6e 73 74 ess of next inst
20db4 72 75 63 74 69 6f 6e 20 74 6f 20 72 65 74 75 72 ruction to retur
20db5 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 75 62 3b n */. int iSub;
20db6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20db7 20 20 2f 2a 20 30 20 3d 20 6d 61 69 6e 20 70 72 /* 0 = main pr
20db8 6f 67 72 61 6d 2c 20 31 20 3d 20 66 69 72 73 74 ogram, 1 = first
20db9 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 65 74 63 sub-program etc
20dba 2e 20 2a 2f 0a 7d 3b 0a 73 74 61 74 69 63 20 4f . */.};.static O
20dbb 70 20 2a 6f 70 49 74 65 72 4e 65 78 74 28 56 64 p *opIterNext(Vd
20dbc 62 65 4f 70 49 74 65 72 20 2a 70 29 7b 0a 20 20 beOpIter *p){.
20dbd 56 64 62 65 20 2a 76 20 3d 20 70 2d 3e 76 3b 0a Vdbe *v = p->v;.
20dbe 20 20 4f 70 20 2a 70 52 65 74 20 3d 20 30 3b 0a Op *pRet = 0;.
20dbf 20 20 4f 70 20 2a 61 4f 70 3b 0a 20 20 69 6e 74 Op *aOp;. int
20dc0 20 6e 4f 70 3b 0a 0a 20 20 69 66 28 20 70 2d 3e nOp;.. if( p->
20dc1 69 53 75 62 3c 3d 70 2d 3e 6e 53 75 62 20 29 7b iSub<=p->nSub ){
20dc2 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 53 75 .. if( p->iSu
20dc3 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 4f b==0 ){. aO
20dc4 70 20 3d 20 76 2d 3e 61 4f 70 3b 0a 20 20 20 20 p = v->aOp;.
20dc5 20 20 6e 4f 70 20 3d 20 76 2d 3e 6e 4f 70 3b 0a nOp = v->nOp;.
20dc6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
20dc7 20 61 4f 70 20 3d 20 70 2d 3e 61 70 53 75 62 5b aOp = p->apSub[
20dc8 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e 61 4f 70 3b p->iSub-1]->aOp;
20dc9 0a 20 20 20 20 20 20 6e 4f 70 20 3d 20 70 2d 3e . nOp = p->
20dca 61 70 53 75 62 5b 70 2d 3e 69 53 75 62 2d 31 5d apSub[p->iSub-1]
20dcb 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a 20 20 20 ->nOp;. }.
20dcc 20 61 73 73 65 72 74 28 20 70 2d 3e 69 41 64 64 assert( p->iAdd
20dcd 72 3c 6e 4f 70 20 29 3b 0a 0a 20 20 20 20 70 52 r<nOp );.. pR
20dce 65 74 20 3d 20 26 61 4f 70 5b 70 2d 3e 69 41 64 et = &aOp[p->iAd
20dcf 64 72 5d 3b 0a 20 20 20 20 70 2d 3e 69 41 64 64 dr];. p->iAdd
20dd0 72 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e r++;. if( p->
20dd1 69 41 64 64 72 3d 3d 6e 4f 70 20 29 7b 0a 20 20 iAddr==nOp ){.
20dd2 20 20 20 20 70 2d 3e 69 53 75 62 2b 2b 3b 0a 20 p->iSub++;.
20dd3 20 20 20 20 20 70 2d 3e 69 41 64 64 72 20 3d 20 p->iAddr =
20dd4 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 0;. }. .
20dd5 69 66 28 20 70 52 65 74 2d 3e 70 34 74 79 70 65 if( pRet->p4type
20dd6 3d 3d 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 ==P4_SUBPROGRAM
20dd7 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 79 ){. int nBy
20dd8 74 65 20 3d 20 28 70 2d 3e 6e 53 75 62 2b 31 29 te = (p->nSub+1)
20dd9 2a 73 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 *sizeof(SubProgr
20dda 61 6d 2a 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 am*);. int
20ddb 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 j;. for(j=0
20ddc 3b 20 6a 3c 70 2d 3e 6e 53 75 62 3b 20 6a 2b 2b ; j<p->nSub; j++
20ddd 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
20dde 2d 3e 61 70 53 75 62 5b 6a 5d 3d 3d 70 52 65 74 ->apSub[j]==pRet
20ddf 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d 20 29 20 ->p4.pProgram )
20de0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
20de1 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 2d 3e 6e if( j==p->n
20de2 53 75 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70 Sub ){. p
20de3 2d 3e 61 70 53 75 62 20 3d 20 73 71 6c 69 74 65 ->apSub = sqlite
20de4 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 3DbReallocOrFree
20de5 28 76 2d 3e 64 62 2c 20 70 2d 3e 61 70 53 75 62 (v->db, p->apSub
20de6 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 , nByte);.
20de7 20 20 69 66 28 20 21 70 2d 3e 61 70 53 75 62 20 if( !p->apSub
20de8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 52 65 ){. pRe
20de9 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d t = 0;. }
20dea 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
20deb 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 6e 53 75 62 p->apSub[p->nSub
20dec 2b 2b 5d 20 3d 20 70 52 65 74 2d 3e 70 34 2e 70 ++] = pRet->p4.p
20ded 50 72 6f 67 72 61 6d 3b 0a 20 20 20 20 20 20 20 Program;.
20dee 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
20def 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 . }.. return p
20df0 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 Ret;.}../*.** Ch
20df1 65 63 6b 20 69 66 20 74 68 65 20 70 72 6f 67 72 eck if the progr
20df2 61 6d 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 am stored in the
20df3 20 56 4d 20 61 73 73 6f 63 69 61 74 65 64 20 77 VM associated w
20df4 69 74 68 20 70 50 61 72 73 65 20 6d 61 79 0a 2a ith pParse may.*
20df5 2a 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 54 * throw an ABORT
20df6 20 65 78 63 65 70 74 69 6f 6e 20 28 63 61 75 73 exception (caus
20df7 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65 6e ing the statemen
20df8 74 2c 20 62 75 74 20 6e 6f 74 20 65 6e 74 69 72 t, but not entir
20df9 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a e transaction.**
20dfa 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 to be rolled ba
20dfb 63 6b 29 2e 20 54 68 69 73 20 63 6f 6e 64 69 74 ck). This condit
20dfc 69 6f 6e 20 69 73 20 74 72 75 65 20 69 66 20 74 ion is true if t
20dfd 68 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20 he main program
20dfe 6f 72 20 61 6e 79 0a 2a 2a 20 73 75 62 2d 70 72 or any.** sub-pr
20dff 6f 67 72 61 6d 73 20 63 6f 6e 74 61 69 6e 73 20 ograms contains
20e00 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f any of the follo
20e01 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 wing:.**.** *
20e02 20 4f 50 5f 48 61 6c 74 20 77 69 74 68 20 50 31 OP_Halt with P1
20e03 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 =SQLITE_CONSTRAI
20e04 4e 54 20 61 6e 64 20 50 32 3d 4f 45 5f 41 62 6f NT and P2=OE_Abo
20e05 72 74 2e 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 rt..** * OP_H
20e06 61 6c 74 49 66 4e 75 6c 6c 20 77 69 74 68 20 50 altIfNull with P
20e07 31 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 1=SQLITE_CONSTRA
20e08 49 4e 54 20 61 6e 64 20 50 32 3d 4f 45 5f 41 62 INT and P2=OE_Ab
20e09 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f ort..** * OP_
20e0a 44 65 73 74 72 6f 79 0a 2a 2a 20 20 20 2a 20 20 Destroy.** *
20e0b 4f 50 5f 56 55 70 64 61 74 65 0a 2a 2a 20 20 20 OP_VUpdate.**
20e0c 2a 20 20 4f 50 5f 56 52 65 6e 61 6d 65 0a 2a 2a * OP_VRename.**
20e0d 20 20 20 2a 20 20 4f 50 5f 46 6b 43 6f 75 6e 74 * OP_FkCount
20e0e 65 72 20 77 69 74 68 20 50 32 3d 3d 30 20 28 69 er with P2==0 (i
20e0f 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67 6e mmediate foreign
20e10 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 29 key constraint)
20e11 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 63 68 65 63 .**.** Then chec
20e12 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 k that the value
20e13 20 6f 66 20 50 61 72 73 65 2e 6d 61 79 41 62 6f of Parse.mayAbo
20e14 72 74 20 69 73 20 74 72 75 65 20 69 66 20 61 6e rt is true if an
20e15 0a 2a 2a 20 41 42 4f 52 54 20 6d 61 79 20 62 65 .** ABORT may be
20e16 20 74 68 72 6f 77 6e 2c 20 6f 72 20 66 61 6c 73 thrown, or fals
20e17 65 20 6f 74 68 65 72 77 69 73 65 2e 20 52 65 74 e otherwise. Ret
20e18 75 72 6e 20 74 72 75 65 20 69 66 20 69 74 20 64 urn true if it d
20e19 6f 65 73 0a 2a 2a 20 6d 61 74 63 68 2c 20 6f 72 oes.** match, or
20e1a 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 false otherwise
20e1b 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
20e1c 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 is intended to b
20e1d 65 20 75 73 65 64 20 61 73 0a 2a 2a 20 70 61 72 e used as.** par
20e1e 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74 20 73 t of an assert s
20e1f 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 tatement in the
20e20 63 6f 6d 70 69 6c 65 72 2e 20 53 69 6d 69 6c 61 compiler. Simila
20e21 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 73 r to:.**.** as
20e22 73 65 72 74 28 20 73 71 6c 69 74 65 33 56 64 62 sert( sqlite3Vdb
20e23 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 eAssertMayAbort(
20e24 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 70 pParse->pVdbe, p
20e25 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72 74 29 Parse->mayAbort)
20e26 20 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 );.*/.SQLITE_PR
20e27 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
20e28 33 56 64 62 65 41 73 73 65 72 74 4d 61 79 41 62 3VdbeAssertMayAb
20e29 6f 72 74 28 56 64 62 65 20 2a 76 2c 20 69 6e 74 ort(Vdbe *v, int
20e2a 20 6d 61 79 41 62 6f 72 74 29 7b 0a 20 20 69 6e mayAbort){. in
20e2b 74 20 68 61 73 41 62 6f 72 74 20 3d 20 30 3b 0a t hasAbort = 0;.
20e2c 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62 Op *pOp;. Vdb
20e2d 65 4f 70 49 74 65 72 20 73 49 74 65 72 3b 0a 20 eOpIter sIter;.
20e2e 20 6d 65 6d 73 65 74 28 26 73 49 74 65 72 2c 20 memset(&sIter,
20e2f 30 2c 20 73 69 7a 65 6f 66 28 73 49 74 65 72 29 0, sizeof(sIter)
20e30 29 3b 0a 20 20 73 49 74 65 72 2e 76 20 3d 20 76 );. sIter.v = v
20e31 3b 0a 0a 20 20 77 68 69 6c 65 28 20 28 70 4f 70 ;.. while( (pOp
20e32 20 3d 20 6f 70 49 74 65 72 4e 65 78 74 28 26 73 = opIterNext(&s
20e33 49 74 65 72 29 29 21 3d 30 20 29 7b 0a 20 20 20 Iter))!=0 ){.
20e34 20 69 6e 74 20 6f 70 63 6f 64 65 20 3d 20 70 4f int opcode = pO
20e35 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 69 p->opcode;. i
20e36 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 44 65 f( opcode==OP_De
20e37 73 74 72 6f 79 20 7c 7c 20 6f 70 63 6f 64 65 3d stroy || opcode=
20e38 3d 4f 50 5f 56 55 70 64 61 74 65 20 7c 7c 20 6f =OP_VUpdate || o
20e39 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 65 6e 61 6d pcode==OP_VRenam
20e3a 65 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 e .#ifndef SQLIT
20e3b 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b E_OMIT_FOREIGN_K
20e3c 45 59 0a 20 20 20 20 20 7c 7c 20 28 6f 70 63 6f EY. || (opco
20e3d 64 65 3d 3d 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 de==OP_FkCounter
20e3e 20 26 26 20 70 4f 70 2d 3e 70 31 3d 3d 30 20 26 && pOp->p1==0 &
20e3f 26 20 70 4f 70 2d 3e 70 32 3d 3d 31 29 20 0a 23 & pOp->p2==1) .#
20e40 65 6e 64 69 66 0a 20 20 20 20 20 7c 7c 20 28 28 endif. || ((
20e41 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20 opcode==OP_Halt
20e42 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 || opcode==OP_Ha
20e43 6c 74 49 66 4e 75 6c 6c 29 20 0a 20 20 20 20 20 ltIfNull) .
20e44 20 26 26 20 28 70 4f 70 2d 3e 70 31 3d 3d 53 51 && (pOp->p1==SQ
20e45 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 LITE_CONSTRAINT
20e46 26 26 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41 && pOp->p2==OE_A
20e47 62 6f 72 74 29 29 0a 20 20 20 20 29 7b 0a 20 20 bort)). ){.
20e48 20 20 20 20 68 61 73 41 62 6f 72 74 20 3d 20 31 hasAbort = 1
20e49 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
20e4a 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 }. }. sqlit
20e4b 65 33 44 62 46 72 65 65 28 76 2d 3e 64 62 2c 20 e3DbFree(v->db,
20e4c 73 49 74 65 72 2e 61 70 53 75 62 29 3b 0a 0a 20 sIter.apSub);..
20e4d 20 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 /* Return true
20e4e 69 66 20 68 61 73 41 62 6f 72 74 3d 3d 6d 61 79 if hasAbort==may
20e4f 41 62 6f 72 74 2e 20 4f 72 20 69 66 20 61 20 6d Abort. Or if a m
20e50 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 alloc failure oc
20e51 63 75 72 65 64 2e 0a 20 20 2a 2a 20 49 66 20 6d cured.. ** If m
20e52 61 6c 6c 6f 63 20 66 61 69 6c 65 64 2c 20 74 68 alloc failed, th
20e53 65 6e 20 74 68 65 20 77 68 69 6c 65 28 29 20 6c en the while() l
20e54 6f 6f 70 20 61 62 6f 76 65 20 6d 61 79 20 6e 6f oop above may no
20e55 74 20 68 61 76 65 20 69 74 65 72 61 74 65 64 0a t have iterated.
20e56 20 20 2a 2a 20 74 68 72 6f 75 67 68 20 61 6c 6c ** through all
20e57 20 6f 70 63 6f 64 65 73 20 61 6e 64 20 68 61 73 opcodes and has
20e58 41 62 6f 72 74 20 6d 61 79 20 62 65 20 73 65 74 Abort may be set
20e59 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e 20 52 65 incorrectly. Re
20e5a 74 75 72 6e 0a 20 20 2a 2a 20 74 72 75 65 20 66 turn. ** true f
20e5b 6f 72 20 74 68 69 73 20 63 61 73 65 20 74 6f 20 or this case to
20e5c 70 72 65 76 65 6e 74 20 74 68 65 20 61 73 73 65 prevent the asse
20e5d 72 74 28 29 20 69 6e 20 74 68 65 20 63 61 6c 6c rt() in the call
20e5e 65 72 73 20 66 72 61 6d 65 0a 20 20 2a 2a 20 66 ers frame. ** f
20e5f 72 6f 6d 20 66 61 69 6c 69 6e 67 2e 20 20 2a 2f rom failing. */
20e60 0a 20 20 72 65 74 75 72 6e 20 28 20 76 2d 3e 64 . return ( v->d
20e61 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
20e62 7c 7c 20 68 61 73 41 62 6f 72 74 3d 3d 6d 61 79 || hasAbort==may
20e63 41 62 6f 72 74 20 29 3b 0a 7d 0a 23 65 6e 64 69 Abort );.}.#endi
20e64 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 f /* SQLITE_DEBU
20e65 47 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33 41 G - the sqlite3A
20e66 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 29 20 ssertMayAbort()
20e67 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a function */../*.
20e68 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 ** Loop through
20e69 74 68 65 20 70 72 6f 67 72 61 6d 20 6c 6f 6f 6b the program look
20e6a 69 6e 67 20 66 6f 72 20 50 32 20 76 61 6c 75 65 ing for P2 value
20e6b 73 20 74 68 61 74 20 61 72 65 20 6e 65 67 61 74 s that are negat
20e6c 69 76 65 0a 2a 2a 20 6f 6e 20 6a 75 6d 70 20 69 ive.** on jump i
20e6d 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 45 61 nstructions. Ea
20e6e 63 68 20 73 75 63 68 20 76 61 6c 75 65 20 69 73 ch such value is
20e6f 20 61 20 6c 61 62 65 6c 2e 20 20 52 65 73 6f 6c a label. Resol
20e70 76 65 20 74 68 65 0a 2a 2a 20 6c 61 62 65 6c 20 ve the.** label
20e71 62 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 by setting the P
20e72 32 20 76 61 6c 75 65 20 74 6f 20 69 74 73 20 63 2 value to its c
20e73 6f 72 72 65 63 74 20 6e 6f 6e 2d 7a 65 72 6f 20 orrect non-zero
20e74 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 value..**.** Thi
20e75 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
20e76 6c 65 64 20 6f 6e 63 65 20 61 66 74 65 72 20 61 led once after a
20e77 6c 6c 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 ll opcodes have
20e78 62 65 65 6e 20 69 6e 73 65 72 74 65 64 2e 0a 2a been inserted..*
20e79 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 2a 70 *.** Variable *p
20e7a 4d 61 78 46 75 6e 63 41 72 67 73 20 69 73 20 73 MaxFuncArgs is s
20e7b 65 74 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 et to the maximu
20e7c 6d 20 76 61 6c 75 65 20 6f 66 20 61 6e 79 20 50 m value of any P
20e7d 32 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74 2 argument .** t
20e7e 6f 20 61 6e 20 4f 50 5f 46 75 6e 63 74 69 6f 6e o an OP_Function
20e7f 2c 20 4f 50 5f 41 67 67 53 74 65 70 20 6f 72 20 , OP_AggStep or
20e80 4f 50 5f 56 46 69 6c 74 65 72 20 6f 70 63 6f 64 OP_VFilter opcod
20e81 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 e. This is used
20e82 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 by .** sqlite3Vd
20e83 62 65 4d 61 6b 65 52 65 61 64 79 28 29 20 74 6f beMakeReady() to
20e84 20 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61 size the Vdbe.a
20e85 70 41 72 67 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a pArg[] array..**
20e86 0a 2a 2a 20 54 68 65 20 4f 70 2e 6f 70 66 6c 61 .** The Op.opfla
20e87 67 73 20 66 69 65 6c 64 20 69 73 20 73 65 74 20 gs field is set
20e88 6f 6e 20 61 6c 6c 20 6f 70 63 6f 64 65 73 2e 0a on all opcodes..
20e89 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 */.static void r
20e8a 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 56 esolveP2Values(V
20e8b 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70 4d 61 dbe *p, int *pMa
20e8c 78 46 75 6e 63 41 72 67 73 29 7b 0a 20 20 69 6e xFuncArgs){. in
20e8d 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4d 61 78 41 t i;. int nMaxA
20e8e 72 67 73 20 3d 20 2a 70 4d 61 78 46 75 6e 63 41 rgs = *pMaxFuncA
20e8f 72 67 73 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a rgs;. Op *pOp;.
20e90 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 20 3d 20 int *aLabel =
20e91 70 2d 3e 61 4c 61 62 65 6c 3b 0a 20 20 70 2d 3e p->aLabel;. p->
20e92 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 readOnly = 1;.
20e93 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c 20 for(pOp=p->aOp,
20e94 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d 30 i=p->nOp-1; i>=0
20e95 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 20 ; i--, pOp++){.
20e96 20 20 20 75 38 20 6f 70 63 6f 64 65 20 3d 20 70 u8 opcode = p
20e97 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 20 20 Op->opcode;..
20e98 20 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 3d 20 pOp->opflags =
20e99 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 50 72 6f sqlite3OpcodePro
20e9a 70 65 72 74 79 5b 6f 70 63 6f 64 65 5d 3b 0a 20 perty[opcode];.
20e9b 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f if( opcode==O
20e9c 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70 P_Function || op
20e9d 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 70 code==OP_AggStep
20e9e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f ){. if( pO
20e9f 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 29 p->p5>nMaxArgs )
20ea0 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d nMaxArgs = pOp-
20ea1 3e 70 35 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 >p5;. }else i
20ea2 66 28 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 f( (opcode==OP_T
20ea3 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 20 70 4f ransaction && pO
20ea4 70 2d 3e 70 32 21 3d 30 29 20 7c 7c 20 6f 70 63 p->p2!=0) || opc
20ea5 6f 64 65 3d 3d 4f 50 5f 56 61 63 75 75 6d 20 29 ode==OP_Vacuum )
20ea6 7b 0a 20 20 20 20 20 20 70 2d 3e 72 65 61 64 4f {. p->readO
20ea7 6e 6c 79 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 nly = 0;.#ifndef
20ea8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
20ea9 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 TUALTABLE. }e
20eaa 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d lse if( opcode==
20eab 4f 50 5f 56 55 70 64 61 74 65 20 29 7b 0a 20 20 OP_VUpdate ){.
20eac 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e if( pOp->p2>
20ead 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 nMaxArgs ) nMaxA
20eae 72 67 73 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 rgs = pOp->p2;.
20eaf 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 }else if( opc
20eb0 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 20 ode==OP_VFilter
20eb1 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a ){. int n;.
20eb2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d assert( p-
20eb3 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 20 29 3b >nOp - i >= 3 );
20eb4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
20eb5 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f Op[-1].opcode==O
20eb6 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a 20 20 20 P_Integer );.
20eb7 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 n = pOp[-1].p
20eb8 31 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 1;. if( n>n
20eb9 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 MaxArgs ) nMaxAr
20eba 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a 20 gs = n;.#endif.
20ebb 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 }else if( opc
20ebc 6f 64 65 3d 3d 4f 50 5f 4e 65 78 74 20 7c 7c 20 ode==OP_Next ||
20ebd 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 6f 72 74 65 opcode==OP_Sorte
20ebe 72 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 rNext ){. p
20ebf 4f 70 2d 3e 70 34 2e 78 41 64 76 61 6e 63 65 20 Op->p4.xAdvance
20ec0 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 = sqlite3BtreeNe
20ec1 78 74 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 xt;. pOp->p
20ec2 34 74 79 70 65 20 3d 20 50 34 5f 41 44 56 41 4e 4type = P4_ADVAN
20ec3 43 45 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 CE;. }else if
20ec4 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 50 72 65 ( opcode==OP_Pre
20ec5 76 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 2d 3e v ){. pOp->
20ec6 70 34 2e 78 41 64 76 61 6e 63 65 20 3d 20 73 71 p4.xAdvance = sq
20ec7 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f lite3BtreePrevio
20ec8 75 73 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 us;. pOp->p
20ec9 34 74 79 70 65 20 3d 20 50 34 5f 41 44 56 41 4e 4type = P4_ADVAN
20eca 43 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 CE;. }.. i
20ecb 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 f( (pOp->opflags
20ecc 20 26 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 21 3d & OPFLG_JUMP)!=
20ecd 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29 0 && pOp->p2<0 )
20ece 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
20ecf 2d 31 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c -1-pOp->p2<p->nL
20ed0 61 62 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 4f abel );. pO
20ed1 70 2d 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d p->p2 = aLabel[-
20ed2 31 2d 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 1-pOp->p2];.
20ed3 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 }. }. sqlite3D
20ed4 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e bFree(p->db, p->
20ed5 61 4c 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c aLabel);. p->aL
20ed6 61 62 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d abel = 0;.. *pM
20ed7 61 78 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61 axFuncArgs = nMa
20ed8 78 41 72 67 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 xArgs;.}../*.**
20ed9 52 65 74 75 72 6e 20 74 68 65 20 61 64 64 72 65 Return the addre
20eda 73 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 ss of the next i
20edb 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 nstruction to be
20edc 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 inserted..*/.SQ
20edd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
20ede 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 sqlite3VdbeCurr
20edf 65 6e 74 41 64 64 72 28 56 64 62 65 20 2a 70 29 entAddr(Vdbe *p)
20ee0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d {. assert( p->m
20ee1 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
20ee2 5f 49 4e 49 54 20 29 3b 0a 20 20 72 65 74 75 72 _INIT );. retur
20ee3 6e 20 70 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a n p->nOp;.}../*.
20ee4 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
20ee5 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
20ee6 65 72 20 74 6f 20 74 68 65 20 61 72 72 61 79 20 er to the array
20ee7 6f 66 20 6f 70 63 6f 64 65 73 20 61 73 73 6f 63 of opcodes assoc
20ee8 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 iated with.** th
20ee9 65 20 56 64 62 65 20 70 61 73 73 65 64 20 61 73 e Vdbe passed as
20eea 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
20eeb 65 6e 74 2e 20 49 74 20 69 73 20 74 68 65 20 63 ent. It is the c
20eec 61 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 allers responsib
20eed 69 6c 69 74 79 0a 2a 2a 20 74 6f 20 61 72 72 61 ility.** to arra
20eee 6e 67 65 20 66 6f 72 20 74 68 65 20 72 65 74 75 nge for the retu
20eef 72 6e 65 64 20 61 72 72 61 79 20 74 6f 20 62 65 rned array to be
20ef0 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 eventually free
20ef1 64 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 d using the .**
20ef2 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 vdbeFreeOpArray(
20ef3 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a ) function..**.*
20ef4 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 * Before returni
20ef5 6e 67 2c 20 2a 70 6e 4f 70 20 69 73 20 73 65 74 ng, *pnOp is set
20ef6 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
20ef7 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 f entries in the
20ef8 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 72 72 returned.** arr
20ef9 61 79 2e 20 41 6c 73 6f 2c 20 2a 70 6e 4d 61 78 ay. Also, *pnMax
20efa 41 72 67 20 69 73 20 73 65 74 20 74 6f 20 74 68 Arg is set to th
20efb 65 20 6c 61 72 67 65 72 20 6f 66 20 69 74 73 20 e larger of its
20efc 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 61 6e current value an
20efd 64 20 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 d .** the number
20efe 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 of entries in t
20eff 68 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 he Vdbe.apArg[]
20f00 61 72 72 61 79 20 72 65 71 75 69 72 65 64 20 74 array required t
20f01 6f 20 65 78 65 63 75 74 65 20 74 68 65 20 0a 2a o execute the .*
20f02 2a 20 72 65 74 75 72 6e 65 64 20 70 72 6f 67 72 * returned progr
20f03 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 am..*/.SQLITE_PR
20f04 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 IVATE VdbeOp *sq
20f05 6c 69 74 65 33 56 64 62 65 54 61 6b 65 4f 70 41 lite3VdbeTakeOpA
20f06 72 72 61 79 28 56 64 62 65 20 2a 70 2c 20 69 6e rray(Vdbe *p, in
20f07 74 20 2a 70 6e 4f 70 2c 20 69 6e 74 20 2a 70 6e t *pnOp, int *pn
20f08 4d 61 78 41 72 67 29 7b 0a 20 20 56 64 62 65 4f MaxArg){. VdbeO
20f09 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b p *aOp = p->aOp;
20f0a 0a 20 20 61 73 73 65 72 74 28 20 61 4f 70 20 26 . assert( aOp &
20f0b 26 20 21 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 & !p->db->malloc
20f0c 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 2f 2a 20 Failed );.. /*
20f0d 43 68 65 63 6b 20 74 68 61 74 20 73 71 6c 69 74 Check that sqlit
20f0e 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 e3VdbeUsesBtree(
20f0f 29 20 77 61 73 20 6e 6f 74 20 63 61 6c 6c 65 64 ) was not called
20f10 20 6f 6e 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 on this VM */.
20f11 20 61 73 73 65 72 74 28 20 70 2d 3e 62 74 72 65 assert( p->btre
20f12 65 4d 61 73 6b 3d 3d 30 20 29 3b 0a 0a 20 20 72 eMask==0 );.. r
20f13 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 70 esolveP2Values(p
20f14 2c 20 70 6e 4d 61 78 41 72 67 29 3b 0a 20 20 2a , pnMaxArg);. *
20f15 70 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 pnOp = p->nOp;.
20f16 20 70 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20 20 72 p->aOp = 0;. r
20f17 65 74 75 72 6e 20 61 4f 70 3b 0a 7d 0a 0a 2f 2a eturn aOp;.}../*
20f18 0a 2a 2a 20 41 64 64 20 61 20 77 68 6f 6c 65 20 .** Add a whole
20f19 6c 69 73 74 20 6f 66 20 6f 70 65 72 61 74 69 6f list of operatio
20f1a 6e 73 20 74 6f 20 74 68 65 20 6f 70 65 72 61 74 ns to the operat
20f1b 69 6f 6e 20 73 74 61 63 6b 2e 20 20 52 65 74 75 ion stack. Retu
20f1c 72 6e 20 74 68 65 0a 2a 2a 20 61 64 64 72 65 73 rn the.** addres
20f1d 73 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 6f s of the first o
20f1e 70 65 72 61 74 69 6f 6e 20 61 64 64 65 64 2e 0a peration added..
20f1f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20f20 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
20f21 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 65 20 eAddOpList(Vdbe
20f22 2a 70 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 *p, int nOp, Vdb
20f23 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 eOpList const *a
20f24 4f 70 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b Op){. int addr;
20f25 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 . assert( p->ma
20f26 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic==VDBE_MAGIC_
20f27 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d INIT );. if( p-
20f28 3e 6e 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e >nOp + nOp > p->
20f29 6e 4f 70 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77 nOpAlloc && grow
20f2a 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 OpArray(p) ){.
20f2b 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
20f2c 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b addr = p->nOp;
20f2d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f . if( ALWAYS(nO
20f2e 70 3e 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 p>0) ){. int
20f2f 69 3b 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73 i;. VdbeOpLis
20f30 74 20 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 t const *pIn = a
20f31 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b Op;. for(i=0;
20f32 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e i<nOp; i++, pIn
20f33 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 ++){. int p
20f34 32 20 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 2 = pIn->p2;.
20f35 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 VdbeOp *pOut
20f36 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 = &p->aOp[i+addr
20f37 5d 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f ];. pOut->o
20f38 70 63 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 pcode = pIn->opc
20f39 6f 64 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d ode;. pOut-
20f3a 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 >p1 = pIn->p1;.
20f3b 20 20 20 20 20 69 66 28 20 70 32 3c 30 20 26 26 if( p2<0 &&
20f3c 20 28 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 50 (sqlite3OpcodeP
20f3d 72 6f 70 65 72 74 79 5b 70 4f 75 74 2d 3e 6f 70 roperty[pOut->op
20f3e 63 6f 64 65 5d 20 26 20 4f 50 46 4c 47 5f 4a 55 code] & OPFLG_JU
20f3f 4d 50 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 MP)!=0 ){.
20f40 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 pOut->p2 = add
20f41 72 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20 r + ADDR(p2);.
20f42 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
20f43 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 pOut->p2 = p2
20f44 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
20f45 70 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e pOut->p3 = pIn->
20f46 70 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e p3;. pOut->
20f47 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 p4type = P4_NOTU
20f48 53 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d SED;. pOut-
20f49 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 >p4.p = 0;.
20f4a 20 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 pOut->p5 = 0;.#
20f4b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
20f4c 55 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a UG. pOut->z
20f4d 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 Comment = 0;.
20f4e 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 if( sqlite3Vd
20f4f 62 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a beAddopTrace ){.
20f50 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
20f51 64 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b dbePrintOp(0, i+
20f52 61 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b addr, &p->aOp[i+
20f53 61 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a addr]);. }.
20f54 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 #endif. }.
20f55 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a p->nOp += nOp;.
20f56 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 }. return add
20f57 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e r;.}../*.** Chan
20f58 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ge the value of
20f59 74 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 the P1 operand f
20f5a 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e or a specific in
20f5b 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 struction..** Th
20f5c 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
20f5d 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 eful when a larg
20f5e 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 e program is loa
20f5f 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 ded from a.** st
20f60 61 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 atic array using
20f61 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20f62 70 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e pList but we wan
20f63 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 t to make a.** f
20f64 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 ew minor changes
20f65 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e to the program.
20f66 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20f67 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
20f68 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 dbeChangeP1(Vdbe
20f69 20 2a 70 2c 20 75 33 32 20 61 64 64 72 2c 20 69 *p, u32 addr, i
20f6a 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 nt val){. asser
20f6b 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 t( p!=0 );. if(
20f6c 20 28 28 75 33 32 29 70 2d 3e 6e 4f 70 29 3e 61 ((u32)p->nOp)>a
20f6d 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f ddr ){. p->aO
20f6e 70 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c p[addr].p1 = val
20f6f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 ;. }.}../*.** C
20f70 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 hange the value
20f71 6f 66 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e of the P2 operan
20f72 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 d for a specific
20f73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a instruction..**
20f74 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
20f75 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74 useful for sett
20f76 69 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69 ing a jump desti
20f77 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 nation..*/.SQLIT
20f78 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
20f79 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
20f7a 50 32 28 56 64 62 65 20 2a 70 2c 20 75 33 32 20 P2(Vdbe *p, u32
20f7b 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a addr, int val){.
20f7c 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 assert( p!=0 )
20f7d 3b 0a 20 20 69 66 28 20 28 28 75 33 32 29 70 2d ;. if( ((u32)p-
20f7e 3e 6e 4f 70 29 3e 61 64 64 72 20 29 7b 0a 20 20 >nOp)>addr ){.
20f7f 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 p->aOp[addr].p
20f80 32 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2 = val;. }.}..
20f81 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
20f82 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33 value of the P3
20f83 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 operand for a s
20f84 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 pecific instruct
20f85 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
20f86 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
20f87 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 te3VdbeChangeP3(
20f88 56 64 62 65 20 2a 70 2c 20 75 33 32 20 61 64 64 Vdbe *p, u32 add
20f89 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 r, int val){. a
20f8a 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
20f8b 20 69 66 28 20 28 28 75 33 32 29 70 2d 3e 6e 4f if( ((u32)p->nO
20f8c 70 29 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 p)>addr ){. p
20f8d 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d ->aOp[addr].p3 =
20f8e 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a val;. }.}../*.
20f8f 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 ** Change the va
20f90 6c 75 65 20 6f 66 20 74 68 65 20 50 35 20 6f 70 lue of the P5 op
20f91 65 72 61 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f erand for the mo
20f92 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 st recently.** a
20f93 64 64 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a dded operation..
20f94 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20f95 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
20f96 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 20 beChangeP5(Vdbe
20f97 2a 70 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 61 *p, u8 val){. a
20f98 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
20f99 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 if( p->aOp ){.
20f9a 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f assert( p->nO
20f9b 70 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 61 4f p>0 );. p->aO
20f9c 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20 3d p[p->nOp-1].p5 =
20f9d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a val;. }.}../*.
20f9e 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50 32 ** Change the P2
20f9f 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 73 74 operand of inst
20fa0 72 75 63 74 69 6f 6e 20 61 64 64 72 20 73 6f 20 ruction addr so
20fa1 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 that it points t
20fa2 6f 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73 73 o.** the address
20fa3 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 of the next ins
20fa4 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 truction to be c
20fa5 6f 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oded..*/.SQLITE_
20fa6 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
20fa7 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
20fa8 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 (Vdbe *p, int ad
20fa9 64 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 61 dr){. assert( a
20faa 64 64 72 3e 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d ddr>=0 || p->db-
20fab 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
20fac 0a 20 20 69 66 28 20 61 64 64 72 3e 3d 30 20 29 . if( addr>=0 )
20fad 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
20fae 67 65 50 32 28 70 2c 20 61 64 64 72 2c 20 70 2d geP2(p, addr, p-
20faf 3e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a >nOp);.}.../*.**
20fb0 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 46 75 If the input Fu
20fb1 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 ncDef structure
20fb2 69 73 20 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 is ephemeral, th
20fb3 65 6e 20 66 72 65 65 20 69 74 2e 20 20 49 66 0a en free it. If.
20fb4 2a 2a 20 74 68 65 20 46 75 6e 63 44 65 66 20 69 ** the FuncDef i
20fb5 73 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 s not ephermal,
20fb6 74 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e then do nothing.
20fb7 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
20fb8 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e freeEphemeralFun
20fb9 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 ction(sqlite3 *d
20fba 62 2c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 b, FuncDef *pDef
20fbb 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 ){. if( ALWAYS(
20fbc 70 44 65 66 29 20 26 26 20 28 70 44 65 66 2d 3e pDef) && (pDef->
20fbd 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 flags & SQLITE_F
20fbe 55 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b UNC_EPHEM)!=0 ){
20fbf 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
20fc0 65 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 ee(db, pDef);.
20fc1 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 }.}..static void
20fc2 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 vdbeFreeOpArray
20fc3 28 73 71 6c 69 74 65 33 20 2a 2c 20 4f 70 20 2a (sqlite3 *, Op *
20fc4 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 44 , int);../*.** D
20fc5 65 6c 65 74 65 20 61 20 50 34 20 76 61 6c 75 65 elete a P4 value
20fc6 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a if necessary..*
20fc7 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 /.static void fr
20fc8 65 65 50 34 28 73 71 6c 69 74 65 33 20 2a 64 62 eeP4(sqlite3 *db
20fc9 2c 20 69 6e 74 20 70 34 74 79 70 65 2c 20 76 6f , int p4type, vo
20fca 69 64 20 2a 70 34 29 7b 0a 20 20 69 66 28 20 70 id *p4){. if( p
20fcb 34 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 4 ){. assert(
20fcc 20 64 62 20 29 3b 0a 20 20 20 20 73 77 69 74 63 db );. switc
20fcd 68 28 20 70 34 74 79 70 65 20 29 7b 0a 20 20 20 h( p4type ){.
20fce 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a case P4_REAL:
20fcf 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 49 . case P4_I
20fd0 4e 54 36 34 3a 0a 20 20 20 20 20 20 63 61 73 65 NT64:. case
20fd1 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20 P4_DYNAMIC:.
20fd2 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e case P4_KEYIN
20fd3 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 FO:. case P
20fd4 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20 4_INTARRAY:.
20fd5 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 case P4_KEYINF
20fd6 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20 O_HANDOFF: {.
20fd7 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
20fd8 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20 ee(db, p4);.
20fd9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
20fda 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 }. case P4
20fdb 5f 4d 50 52 49 4e 54 46 3a 20 7b 0a 20 20 20 20 _MPRINTF: {.
20fdc 20 20 20 20 69 66 28 20 64 62 2d 3e 70 6e 42 79 if( db->pnBy
20fdd 74 65 73 46 72 65 65 64 3d 3d 30 20 29 20 73 71 tesFreed==0 ) sq
20fde 6c 69 74 65 33 5f 66 72 65 65 28 70 34 29 3b 0a lite3_free(p4);.
20fdf 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
20fe0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 }. cas
20fe1 65 20 50 34 5f 56 44 42 45 46 55 4e 43 3a 20 7b e P4_VDBEFUNC: {
20fe2 0a 20 20 20 20 20 20 20 20 56 64 62 65 46 75 6e . VdbeFun
20fe3 63 20 2a 70 56 64 62 65 46 75 6e 63 20 3d 20 28 c *pVdbeFunc = (
20fe4 56 64 62 65 46 75 6e 63 20 2a 29 70 34 3b 0a 20 VdbeFunc *)p4;.
20fe5 20 20 20 20 20 20 20 66 72 65 65 45 70 68 65 6d freeEphem
20fe6 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c eralFunction(db,
20fe7 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e pVdbeFunc->pFun
20fe8 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 c);. if(
20fe9 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 65 64 db->pnBytesFreed
20fea 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64 62 ==0 ) sqlite3Vdb
20feb 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 70 eDeleteAuxData(p
20fec 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a 20 20 VdbeFunc, 0);.
20fed 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
20fee 72 65 65 28 64 62 2c 20 70 56 64 62 65 46 75 6e ree(db, pVdbeFun
20fef 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 c);. brea
20ff0 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
20ff1 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44 45 46 case P4_FUNCDEF
20ff2 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72 65 65 : {. free
20ff3 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f EphemeralFunctio
20ff4 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66 2a 29 n(db, (FuncDef*)
20ff5 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 p4);. bre
20ff6 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
20ff7 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a 20 7b case P4_MEM: {
20ff8 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d . if( db-
20ff9 3e 70 6e 42 79 74 65 73 46 72 65 65 64 3d 3d 30 >pnBytesFreed==0
20ffa 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
20ffb 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 28 lite3ValueFree((
20ffc 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 sqlite3_value*)p
20ffd 34 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 4);. }els
20ffe 65 7b 0a 20 20 20 20 20 20 20 20 20 20 4d 65 6d e{. Mem
20fff 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 34 3b 0a *p = (Mem*)p4;.
21000 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
21001 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
21002 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 Malloc);.
21003 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
21004 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 (db, p);.
21005 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b }. break
21006 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
21007 63 61 73 65 20 50 34 5f 56 54 41 42 20 3a 20 7b case P4_VTAB : {
21008 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d . if( db-
21009 3e 70 6e 42 79 74 65 73 46 72 65 65 64 3d 3d 30 >pnBytesFreed==0
2100a 20 29 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e ) sqlite3VtabUn
2100b 6c 6f 63 6b 28 28 56 54 61 62 6c 65 20 2a 29 70 lock((VTable *)p
2100c 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 4);. brea
2100d 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
2100e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 . }.}../*.** Fr
2100f 65 65 20 74 68 65 20 73 70 61 63 65 20 61 6c 6c ee the space all
21010 6f 63 61 74 65 64 20 66 6f 72 20 61 4f 70 20 61 ocated for aOp a
21011 6e 64 20 61 6e 79 20 70 34 20 76 61 6c 75 65 73 nd any p4 values
21012 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 allocated for t
21013 68 65 0a 2a 2a 20 6f 70 63 6f 64 65 73 20 63 6f he.** opcodes co
21014 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 2e 20 ntained within.
21015 49 66 20 61 4f 70 20 69 73 20 6e 6f 74 20 4e 55 If aOp is not NU
21016 4c 4c 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 LL it is assumed
21017 20 74 6f 20 63 6f 6e 74 61 69 6e 20 0a 2a 2a 20 to contain .**
21018 6e 4f 70 20 65 6e 74 72 69 65 73 2e 20 0a 2a 2f nOp entries. .*/
21019 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 64 62 .static void vdb
2101a 65 46 72 65 65 4f 70 41 72 72 61 79 28 73 71 6c eFreeOpArray(sql
2101b 69 74 65 33 20 2a 64 62 2c 20 4f 70 20 2a 61 4f ite3 *db, Op *aO
2101c 70 2c 20 69 6e 74 20 6e 4f 70 29 7b 0a 20 20 69 p, int nOp){. i
2101d 66 28 20 61 4f 70 20 29 7b 0a 20 20 20 20 4f 70 f( aOp ){. Op
2101e 20 2a 70 4f 70 3b 0a 20 20 20 20 66 6f 72 28 70 *pOp;. for(p
2101f 4f 70 3d 61 4f 70 3b 20 70 4f 70 3c 26 61 4f 70 Op=aOp; pOp<&aOp
21020 5b 6e 4f 70 5d 3b 20 70 4f 70 2b 2b 29 7b 0a 20 [nOp]; pOp++){.
21021 20 20 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 freeP4(db,
21022 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 pOp->p4type, pOp
21023 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 64 65 66 20 ->p4.p);.#ifdef
21024 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
21025 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
21026 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 (db, pOp->zComme
21027 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20 20 20 20 nt);.#endif
21028 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c . }. }. sql
21029 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 ite3DbFree(db, a
2102a 4f 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 Op);.}../*.** Li
2102b 6e 6b 20 74 68 65 20 53 75 62 50 72 6f 67 72 61 nk the SubProgra
2102c 6d 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 m object passed
2102d 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 as the second ar
2102e 67 75 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 gument into the
2102f 6c 69 6e 6b 65 64 0a 2a 2a 20 6c 69 73 74 20 61 linked.** list a
21030 74 20 56 64 62 65 2e 70 53 75 62 50 72 6f 67 72 t Vdbe.pSubProgr
21031 61 6d 2e 20 54 68 69 73 20 6c 69 73 74 20 69 73 am. This list is
21032 20 75 73 65 64 20 74 6f 20 64 65 6c 65 74 65 20 used to delete
21033 61 6c 6c 20 73 75 62 2d 70 72 6f 67 72 61 6d 0a all sub-program.
21034 2a 2a 20 6f 62 6a 65 63 74 73 20 77 68 65 6e 20 ** objects when
21035 74 68 65 20 56 4d 20 69 73 20 6e 6f 20 6c 6f 6e the VM is no lon
21036 67 65 72 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f ger required..*/
21037 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21038 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
21039 4c 69 6e 6b 53 75 62 50 72 6f 67 72 61 6d 28 56 LinkSubProgram(V
2103a 64 62 65 20 2a 70 56 64 62 65 2c 20 53 75 62 50 dbe *pVdbe, SubP
2103b 72 6f 67 72 61 6d 20 2a 70 29 7b 0a 20 20 70 2d rogram *p){. p-
2103c 3e 70 4e 65 78 74 20 3d 20 70 56 64 62 65 2d 3e >pNext = pVdbe->
2103d 70 50 72 6f 67 72 61 6d 3b 0a 20 20 70 56 64 62 pProgram;. pVdb
2103e 65 2d 3e 70 50 72 6f 67 72 61 6d 20 3d 20 70 3b e->pProgram = p;
2103f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
21040 20 74 68 65 20 6f 70 63 6f 64 65 20 61 74 20 61 the opcode at a
21041 64 64 72 20 69 6e 74 6f 20 4f 50 5f 4e 6f 6f 70 ddr into OP_Noop
21042 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
21043 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
21044 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 dbeChangeToNoop(
21045 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 Vdbe *p, int add
21046 72 29 7b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 r){. if( p->aOp
21047 20 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a ){. VdbeOp *
21048 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 pOp = &p->aOp[ad
21049 64 72 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 dr];. sqlite3
2104a 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 *db = p->db;.
2104b 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70 freeP4(db, pOp
2104c 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 ->p4type, pOp->p
2104d 34 2e 70 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 4.p);. memset
2104e 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 (pOp, 0, sizeof(
2104f 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20 70 4f pOp[0]));. pO
21050 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e p->opcode = OP_N
21051 6f 6f 70 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a oop;. }.}../*.*
21052 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c * Change the val
21053 75 65 20 6f 66 20 74 68 65 20 50 34 20 6f 70 65 ue of the P4 ope
21054 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 rand for a speci
21055 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e fic instruction.
21056 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
21057 20 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 is useful when
21058 61 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 a large program
21059 69 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 is loaded from a
2105a 0a 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 .** static array
2105b 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 using sqlite3Vd
2105c 62 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 beAddOpList but
2105d 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 we want to make
2105e 61 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 a.** few minor c
2105f 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 hanges to the pr
21060 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ogram..**.** If
21061 6e 3e 3d 30 20 74 68 65 6e 20 74 68 65 20 50 34 n>=0 then the P4
21062 20 6f 70 65 72 61 6e 64 20 69 73 20 64 79 6e 61 operand is dyna
21063 6d 69 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 mic, meaning tha
21064 74 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 t a copy of.** t
21065 68 65 20 73 74 72 69 6e 67 20 69 73 20 6d 61 64 he string is mad
21066 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 e into memory ob
21067 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
21068 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a te3_malloc()..**
21069 20 41 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d 30 A value of n==0
2106a 20 6d 65 61 6e 73 20 63 6f 70 79 20 62 79 74 65 means copy byte
2106b 73 20 6f 66 20 7a 50 34 20 75 70 20 74 6f 20 61 s of zP4 up to a
2106c 6e 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 nd including the
2106d 0a 2a 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20 62 .** first null b
2106e 79 74 65 2e 20 20 49 66 20 6e 3e 30 20 74 68 65 yte. If n>0 the
2106f 6e 20 63 6f 70 79 20 6e 2b 31 20 62 79 74 65 73 n copy n+1 bytes
21070 20 6f 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 of zP4..**.** I
21071 66 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 f n==P4_KEYINFO
21072 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 50 it means that zP
21073 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 4 is a pointer t
21074 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 o a KeyInfo stru
21075 63 74 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70 79 cture..** A copy
21076 20 69 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 is made of the
21077 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
21078 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 e into memory ob
21079 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 tained from.** s
2107a 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74 qlite3_malloc, t
2107b 6f 20 62 65 20 66 72 65 65 64 20 77 68 65 6e 20 o be freed when
2107c 74 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 the Vdbe is fina
2107d 6c 69 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f lized..** n==P4_
2107e 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 KEYINFO_HANDOFF
2107f 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 7a indicates that z
21080 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b P4 points to a K
21081 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 eyInfo structure
21082 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 .** stored in me
21083 6d 6f 72 79 20 74 68 61 74 20 74 68 65 20 63 61 mory that the ca
21084 6c 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 ller has obtaine
21085 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d d from sqlite3_m
21086 61 6c 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 63 alloc. The .** c
21087 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74 aller should not
21088 20 66 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 61 free the alloca
21089 74 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65 tion, it will be
2108a 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 freed when the
2108b 56 64 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 6c Vdbe is.** final
2108c 69 7a 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 ized..** .** Oth
2108d 65 72 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28 er values of n (
2108e 50 34 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 4f P4_STATIC, P4_CO
2108f 4c 4c 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69 LLSEQ etc.) indi
21090 63 61 74 65 20 74 68 61 74 20 7a 50 34 20 70 6f cate that zP4 po
21091 69 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 72 ints.** to a str
21092 69 6e 67 20 6f 72 20 73 74 72 75 63 74 75 72 65 ing or structure
21093 20 74 68 61 74 20 69 73 20 67 75 61 72 61 6e 74 that is guarant
21094 65 65 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72 eed to exist for
21095 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 the lifetime of
21096 0a 2a 2a 20 74 68 65 20 56 64 62 65 2e 20 49 6e .** the Vdbe. In
21097 20 74 68 65 73 65 20 63 61 73 65 73 20 77 65 20 these cases we
21098 63 61 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68 can just copy th
21099 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a e pointer..**.**
2109a 20 49 66 20 61 64 64 72 3c 30 20 74 68 65 6e 20 If addr<0 then
2109b 63 68 61 6e 67 65 20 50 34 20 6f 6e 20 74 68 65 change P4 on the
2109c 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 most recently i
2109d 6e 73 65 72 74 65 64 20 69 6e 73 74 72 75 63 74 nserted instruct
2109e 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
2109f 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
210a0 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 te3VdbeChangeP4(
210a1 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 Vdbe *p, int add
210a2 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a r, const char *z
210a3 50 34 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f 70 P4, int n){. Op
210a4 20 2a 70 4f 70 3b 0a 20 20 73 71 6c 69 74 65 33 *pOp;. sqlite3
210a5 20 2a 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 *db;. assert(
210a6 70 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 p!=0 );. db = p
210a7 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 ->db;. assert(
210a8 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
210a9 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 AGIC_INIT );. i
210aa 66 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 f( p->aOp==0 ||
210ab 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
210ac 20 29 7b 0a 20 20 20 20 69 66 20 28 20 6e 21 3d ){. if ( n!=
210ad 50 34 5f 4b 45 59 49 4e 46 4f 20 26 26 20 6e 21 P4_KEYINFO && n!
210ae 3d 50 34 5f 56 54 41 42 20 29 20 7b 0a 20 20 20 =P4_VTAB ) {.
210af 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 6e 2c freeP4(db, n,
210b0 20 28 76 6f 69 64 2a 29 2a 28 63 68 61 72 2a 2a (void*)*(char**
210b1 29 26 7a 50 34 29 3b 0a 20 20 20 20 7d 0a 20 20 )&zP4);. }.
210b2 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
210b3 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 assert( p->nOp>0
210b4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 64 );. assert( ad
210b5 64 72 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 dr<p->nOp );. i
210b6 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 20 20 20 f( addr<0 ){.
210b7 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20 2d addr = p->nOp -
210b8 20 31 3b 0a 20 20 7d 0a 20 20 70 4f 70 20 3d 20 1;. }. pOp =
210b9 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 &p->aOp[addr];.
210ba 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
210bb 74 79 70 65 3d 3d 50 34 5f 4e 4f 54 55 53 45 44 type==P4_NOTUSED
210bc 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d || pOp->p4type=
210bd 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 66 =P4_INT32 );. f
210be 72 65 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 reeP4(db, pOp->p
210bf 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 4type, pOp->p4.p
210c0 29 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d );. pOp->p4.p =
210c1 20 30 3b 0a 20 20 69 66 28 20 6e 3d 3d 50 34 5f 0;. if( n==P4_
210c2 49 4e 54 33 32 20 29 7b 0a 20 20 20 20 2f 2a 20 INT32 ){. /*
210c3 4e 6f 74 65 3a 20 74 68 69 73 20 63 61 73 74 20 Note: this cast
210c4 69 73 20 73 61 66 65 2c 20 62 65 63 61 75 73 65 is safe, because
210c5 20 74 68 65 20 6f 72 69 67 69 6e 20 64 61 74 61 the origin data
210c6 20 70 6f 69 6e 74 20 77 61 73 20 61 6e 20 69 6e point was an in
210c7 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 61 t. ** that wa
210c8 73 20 63 61 73 74 20 74 6f 20 61 20 28 63 6f 6e s cast to a (con
210c9 73 74 20 63 68 61 72 20 2a 29 2e 20 2a 2f 0a 20 st char *). */.
210ca 20 20 20 70 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 pOp->p4.i = S
210cb 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 QLITE_PTR_TO_INT
210cc 28 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e (zP4);. pOp->
210cd 70 34 74 79 70 65 20 3d 20 50 34 5f 49 4e 54 33 p4type = P4_INT3
210ce 32 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 2;. }else if( z
210cf 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 P4==0 ){. pOp
210d0 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 ->p4.p = 0;.
210d1 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 pOp->p4type = P4
210d2 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 7d 65 6c 73 _NOTUSED;. }els
210d3 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 e if( n==P4_KEYI
210d4 4e 46 4f 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e NFO ){. KeyIn
210d5 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 fo *pKeyInfo;.
210d6 20 20 69 6e 74 20 6e 46 69 65 6c 64 2c 20 6e 42 int nField, nB
210d7 79 74 65 3b 0a 0a 20 20 20 20 6e 46 69 65 6c 64 yte;.. nField
210d8 20 3d 20 28 28 4b 65 79 49 6e 66 6f 2a 29 7a 50 = ((KeyInfo*)zP
210d9 34 29 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 4)->nField;.
210da 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a nByte = sizeof(*
210db 70 4b 65 79 49 6e 66 6f 29 20 2b 20 28 6e 46 69 pKeyInfo) + (nFi
210dc 65 6c 64 2d 31 29 2a 73 69 7a 65 6f 66 28 70 4b eld-1)*sizeof(pK
210dd 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d eyInfo->aColl[0]
210de 29 20 2b 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 ) + nField;.
210df 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74 pKeyInfo = sqlit
210e0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 30 2c e3DbMallocRaw(0,
210e1 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 70 4f 70 nByte);. pOp
210e2 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 ->p4.pKeyInfo =
210e3 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 66 pKeyInfo;. if
210e4 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 ( pKeyInfo ){.
210e5 20 20 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64 u8 *aSortOrd
210e6 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 er;. memcpy
210e7 28 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f ((char*)pKeyInfo
210e8 2c 20 7a 50 34 2c 20 6e 42 79 74 65 20 2d 20 6e , zP4, nByte - n
210e9 46 69 65 6c 64 29 3b 0a 20 20 20 20 20 20 61 53 Field);. aS
210ea 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79 49 ortOrder = pKeyI
210eb 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 3b nfo->aSortOrder;
210ec 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61 . assert( a
210ed 53 6f 72 74 4f 72 64 65 72 21 3d 30 20 29 3b 0a SortOrder!=0 );.
210ee 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e pKeyInfo->
210ef 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 6e aSortOrder = (un
210f0 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 70 4b signed char*)&pK
210f1 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 eyInfo->aColl[nF
210f2 69 65 6c 64 5d 3b 0a 20 20 20 20 20 20 6d 65 6d ield];. mem
210f3 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 cpy(pKeyInfo->aS
210f4 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f ortOrder, aSortO
210f5 72 64 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 rder, nField);.
210f6 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 pOp->p4type
210f7 20 3d 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 = P4_KEYINFO;.
210f8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
210f9 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 p->db->mallocFai
210fa 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 led = 1;. p
210fb 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f Op->p4type = P4_
210fc 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20 7d 0a 20 NOTUSED;. }.
210fd 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 }else if( n==P4
210fe 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 _KEYINFO_HANDOFF
210ff 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e ){. pOp->p4.
21100 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a p = (void*)zP4;.
21101 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 pOp->p4type
21102 3d 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 = P4_KEYINFO;.
21103 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f }else if( n==P4_
21104 56 54 41 42 20 29 7b 0a 20 20 20 20 70 4f 70 2d VTAB ){. pOp-
21105 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a >p4.p = (void*)z
21106 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 P4;. pOp->p4t
21107 79 70 65 20 3d 20 50 34 5f 56 54 41 42 3b 0a 20 ype = P4_VTAB;.
21108 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f sqlite3VtabLo
21109 63 6b 28 28 56 54 61 62 6c 65 20 2a 29 7a 50 34 ck((VTable *)zP4
2110a 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
2110b 28 56 54 61 62 6c 65 20 2a 29 7a 50 34 29 2d 3e (VTable *)zP4)->
2110c 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 7d db==p->db );. }
2110d 65 6c 73 65 20 69 66 28 20 6e 3c 30 20 29 7b 0a else if( n<0 ){.
2110e 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 pOp->p4.p =
2110f 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 (void*)zP4;.
21110 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 28 73 pOp->p4type = (s
21111 69 67 6e 65 64 20 63 68 61 72 29 6e 3b 0a 20 20 igned char)n;.
21112 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 6e }else{. if( n
21113 3d 3d 30 20 29 20 6e 20 3d 20 73 71 6c 69 74 65 ==0 ) n = sqlite
21114 33 53 74 72 6c 65 6e 33 30 28 7a 50 34 29 3b 0a 3Strlen30(zP4);.
21115 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 pOp->p4.z =
21116 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 sqlite3DbStrNDup
21117 28 70 2d 3e 64 62 2c 20 7a 50 34 2c 20 6e 29 3b (p->db, zP4, n);
21118 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 . pOp->p4type
21119 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 = P4_DYNAMIC;.
2111a 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 }.}..#ifndef ND
2111b 45 42 55 47 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 EBUG./*.** Chang
2111c 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e e the comment on
2111d 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
2111e 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63 ly coded instruc
2111f 74 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 tion. Or.** ins
21120 65 72 74 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 ert a No-op and
21121 61 64 64 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 add the comment
21122 74 6f 20 74 68 61 74 20 6e 65 77 20 69 6e 73 74 to that new inst
21123 72 75 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a ruction. This.*
21124 2a 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65 * makes the code
21125 20 65 61 73 69 65 72 20 74 6f 20 72 65 61 64 20 easier to read
21126 64 75 72 69 6e 67 20 64 65 62 75 67 67 69 6e 67 during debugging
21127 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 69 73 20 . None of this
21128 68 61 70 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 happens.** in a
21129 70 72 6f 64 75 63 74 69 6f 6e 20 62 75 69 6c 64 production build
2112a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
2112b 20 76 64 62 65 56 43 6f 6d 6d 65 6e 74 28 56 64 vdbeVComment(Vd
2112c 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 be *p, const cha
2112d 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c r *zFormat, va_l
2112e 69 73 74 20 61 70 29 7b 0a 20 20 61 73 73 65 72 ist ap){. asser
2112f 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 t( p->nOp>0 || p
21130 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 ->aOp==0 );. as
21131 73 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 sert( p->aOp==0
21132 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 || p->aOp[p->nOp
21133 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 -1].zComment==0
21134 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 || p->db->malloc
21135 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 Failed );. if(
21136 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 61 73 p->nOp ){. as
21137 73 65 72 74 28 20 70 2d 3e 61 4f 70 20 29 3b 0a sert( p->aOp );.
21138 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
21139 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4f 70 5b e(p->db, p->aOp[
2113a 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 p->nOp-1].zComme
2113b 6e 74 29 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b nt);. p->aOp[
2113c 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 p->nOp-1].zComme
2113d 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 nt = sqlite3VMPr
2113e 69 6e 74 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 intf(p->db, zFor
2113f 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7d 0a 7d 0a mat, ap);. }.}.
21140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
21141 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
21142 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 omment(Vdbe *p,
21143 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
21144 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f mat, ...){. va_
21145 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 70 list ap;. if( p
21146 20 29 7b 0a 20 20 20 20 76 61 5f 73 74 61 72 74 ){. va_start
21147 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 (ap, zFormat);.
21148 20 20 20 76 64 62 65 56 43 6f 6d 6d 65 6e 74 28 vdbeVComment(
21149 70 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b p, zFormat, ap);
2114a 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b . va_end(ap);
2114b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 . }.}.SQLITE_PR
2114c 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
2114d 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e e3VdbeNoopCommen
2114e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 t(Vdbe *p, const
2114f 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
21150 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
21151 61 70 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 ap;. if( p ){.
21152 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21153 64 4f 70 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29 dOp0(p, OP_Noop)
21154 3b 0a 20 20 20 20 76 61 5f 73 74 61 72 74 28 61 ;. va_start(a
21155 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 p, zFormat);.
21156 20 76 64 62 65 56 43 6f 6d 6d 65 6e 74 28 70 2c vdbeVComment(p,
21157 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 zFormat, ap);.
21158 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 va_end(ap);.
21159 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 }.}.#endif /*
2115a 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a NDEBUG */../*.**
2115b 20 52 65 74 75 72 6e 20 74 68 65 20 6f 70 63 6f Return the opco
2115c 64 65 20 66 6f 72 20 61 20 67 69 76 65 6e 20 61 de for a given a
2115d 64 64 72 65 73 73 2e 20 20 49 66 20 74 68 65 20 ddress. If the
2115e 61 64 64 72 65 73 73 20 69 73 20 2d 31 2c 20 74 address is -1, t
2115f 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68 hen.** return th
21160 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 e most recently
21161 69 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65 2e inserted opcode.
21162 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f .**.** If a memo
21163 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 ry allocation er
21164 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 ror has occurred
21165 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 61 prior to the ca
21166 6c 6c 69 6e 67 20 6f 66 20 74 68 69 73 0a 2a 2a lling of this.**
21167 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e 20 61 routine, then a
21168 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 75 pointer to a du
21169 6d 6d 79 20 56 64 62 65 4f 70 20 77 69 6c 6c 20 mmy VdbeOp will
2116a 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 be returned. Th
2116b 61 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20 at opcode.** is
2116c 72 65 61 64 61 62 6c 65 20 62 75 74 20 6e 6f 74 readable but not
2116d 20 77 72 69 74 61 62 6c 65 2c 20 74 68 6f 75 67 writable, thoug
2116e 68 20 69 74 20 69 73 20 63 61 73 74 20 74 6f 20 h it is cast to
2116f 61 20 77 72 69 74 61 62 6c 65 20 76 61 6c 75 65 a writable value
21170 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 ..** The return
21171 6f 66 20 61 20 64 75 6d 6d 79 20 6f 70 63 6f 64 of a dummy opcod
21172 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 61 6c e allows the cal
21173 6c 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 66 75 l to continue fu
21174 6e 63 74 69 6f 6e 69 6e 67 0a 2a 2a 20 61 66 74 nctioning.** aft
21175 65 72 20 61 20 4f 4f 4d 20 66 61 75 6c 74 20 77 er a OOM fault w
21176 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f ithout having to
21177 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 check to see if
21178 20 74 68 65 20 72 65 74 75 72 6e 20 66 72 6f 6d the return from
21179 20 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e .** this routin
2117a 65 20 69 73 20 61 20 76 61 6c 69 64 20 70 6f 69 e is a valid poi
2117b 6e 74 65 72 2e 20 20 42 75 74 20 62 65 63 61 75 nter. But becau
2117c 73 65 20 74 68 65 20 64 75 6d 6d 79 2e 6f 70 63 se the dummy.opc
2117d 6f 64 65 20 69 73 20 30 2c 0a 2a 2a 20 64 75 6d ode is 0,.** dum
2117e 6d 79 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 my will never be
2117f 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 20 54 68 written to. Th
21180 69 73 20 69 73 20 76 65 72 69 66 69 65 64 20 62 is is verified b
21181 79 20 63 6f 64 65 20 69 6e 73 70 65 63 74 69 6f y code inspectio
21182 6e 20 61 6e 64 0a 2a 2a 20 62 79 20 72 75 6e 6e n and.** by runn
21183 69 6e 67 20 77 69 74 68 20 56 61 6c 67 72 69 6e ing with Valgrin
21184 64 2e 0a 2a 2a 0a 2a 2a 20 41 62 6f 75 74 20 74 d..**.** About t
21185 68 65 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 he #ifdef SQLITE
21186 5f 4f 4d 49 54 5f 54 52 41 43 45 3a 20 20 4e 6f _OMIT_TRACE: No
21187 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 72 6f 75 rmally, this rou
21188 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 tine is never ca
21189 6c 6c 65 64 0a 2a 2a 20 75 6e 6c 65 73 73 20 70 lled.** unless p
2118a 2d 3e 6e 4f 70 3e 30 2e 20 20 54 68 69 73 20 69 ->nOp>0. This i
2118b 73 20 62 65 63 61 75 73 65 20 69 6e 20 74 68 65 s because in the
2118c 20 61 62 73 65 6e 73 65 20 6f 66 20 53 51 4c 49 absense of SQLI
2118d 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 2c 0a 2a TE_OMIT_TRACE,.*
2118e 2a 20 61 6e 20 4f 50 5f 54 72 61 63 65 20 69 6e * an OP_Trace in
2118f 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c 77 struction is alw
21190 61 79 73 20 69 6e 73 65 72 74 65 64 20 62 79 20 ays inserted by
21191 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 28 29 sqlite3VdbeGet()
21192 20 61 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 61 as soon as.** a
21193 20 6e 65 77 20 56 44 42 45 20 69 73 20 63 72 65 new VDBE is cre
21194 61 74 65 64 2e 20 20 53 6f 20 77 65 20 61 72 65 ated. So we are
21195 20 66 72 65 65 20 74 6f 20 73 65 74 20 61 64 64 free to set add
21196 72 20 74 6f 20 70 2d 3e 6e 4f 70 2d 31 20 77 69 r to p->nOp-1 wi
21197 74 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20 thout.** having
21198 74 6f 20 64 6f 75 62 6c 65 2d 63 68 65 63 6b 20 to double-check
21199 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
2119a 74 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 t the result is
2119b 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 42 75 non-negative. Bu
2119c 74 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f t.** if SQLITE_O
2119d 4d 49 54 5f 54 52 41 43 45 20 69 73 20 64 65 66 MIT_TRACE is def
2119e 69 6e 65 64 2c 20 74 68 65 20 4f 50 5f 54 72 61 ined, the OP_Tra
2119f 63 65 20 69 73 20 6f 6d 69 74 74 65 64 20 61 6e ce is omitted an
211a0 64 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 0a d we do need to.
211a1 2a 2a 20 63 68 65 63 6b 20 74 68 65 20 76 61 6c ** check the val
211a2 75 65 20 6f 66 20 70 2d 3e 6e 4f 70 2d 31 20 62 ue of p->nOp-1 b
211a3 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 efore continuing
211a4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
211a5 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 ATE VdbeOp *sqli
211a6 74 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 te3VdbeGetOp(Vdb
211a7 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b e *p, int addr){
211a8 0a 20 20 2f 2a 20 43 38 39 20 73 70 65 63 69 66 . /* C89 specif
211a9 69 65 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e ies that the con
211aa 73 74 61 6e 74 20 22 64 75 6d 6d 79 22 20 77 69 stant "dummy" wi
211ab 6c 6c 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 ll be initialize
211ac 64 20 74 6f 20 61 6c 6c 0a 20 20 2a 2a 20 7a 65 d to all. ** ze
211ad 72 6f 73 2c 20 77 68 69 63 68 20 69 73 20 63 6f ros, which is co
211ae 72 72 65 63 74 2e 20 20 4d 53 56 43 20 67 65 6e rrect. MSVC gen
211af 65 72 61 74 65 73 20 61 20 77 61 72 6e 69 6e 67 erates a warning
211b0 2c 20 6e 65 76 65 72 74 68 65 6c 65 73 73 2e 20 , nevertheless.
211b1 2a 2f 0a 20 20 73 74 61 74 69 63 20 56 64 62 65 */. static Vdbe
211b2 4f 70 20 64 75 6d 6d 79 3b 20 20 2f 2a 20 49 67 Op dummy; /* Ig
211b3 6e 6f 72 65 20 74 68 65 20 4d 53 56 43 20 77 61 nore the MSVC wa
211b4 72 6e 69 6e 67 20 61 62 6f 75 74 20 6e 6f 20 69 rning about no i
211b5 6e 69 74 69 61 6c 69 7a 65 72 20 2a 2f 0a 20 20 nitializer */.
211b6 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
211b7 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
211b8 54 20 29 3b 0a 20 20 69 66 28 20 61 64 64 72 3c T );. if( addr<
211b9 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 0 ){.#ifdef SQLI
211ba 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 TE_OMIT_TRACE.
211bb 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3d 3d 30 20 if( p->nOp==0
211bc 29 20 72 65 74 75 72 6e 20 28 56 64 62 65 4f 70 ) return (VdbeOp
211bd 2a 29 26 64 75 6d 6d 79 3b 0a 23 65 6e 64 69 66 *)&dummy;.#endif
211be 0a 20 20 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e . addr = p->n
211bf 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 61 73 Op - 1;. }. as
211c0 73 65 72 74 28 20 28 61 64 64 72 3e 3d 30 20 26 sert( (addr>=0 &
211c1 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c & addr<p->nOp) |
211c2 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 | p->db->mallocF
211c3 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 ailed );. if( p
211c4 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
211c5 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ed ){. return
211c6 20 28 56 64 62 65 4f 70 2a 29 26 64 75 6d 6d 79 (VdbeOp*)&dummy
211c7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
211c8 65 74 75 72 6e 20 26 70 2d 3e 61 4f 70 5b 61 64 eturn &p->aOp[ad
211c9 64 72 5d 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 dr];. }.}..#if
211ca 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
211cb 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c OMIT_EXPLAIN) ||
211cc 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
211cd 29 20 5c 0a 20 20 20 20 20 7c 7c 20 64 65 66 69 ) \. || defi
211ce 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 ned(VDBE_PROFILE
211cf 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
211d0 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a ITE_DEBUG)./*.**
211d1 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69 6e Compute a strin
211d2 67 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 g that describes
211d3 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 the P4 paramete
211d4 72 20 66 6f 72 20 61 6e 20 6f 70 63 6f 64 65 2e r for an opcode.
211d5 0a 2a 2a 20 55 73 65 20 7a 54 65 6d 70 20 66 6f .** Use zTemp fo
211d6 72 20 61 6e 79 20 72 65 71 75 69 72 65 64 20 74 r any required t
211d7 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72 20 emporary buffer
211d8 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 space..*/.static
211d9 20 63 68 61 72 20 2a 64 69 73 70 6c 61 79 50 34 char *displayP4
211da 28 4f 70 20 2a 70 4f 70 2c 20 63 68 61 72 20 2a (Op *pOp, char *
211db 7a 54 65 6d 70 2c 20 69 6e 74 20 6e 54 65 6d 70 zTemp, int nTemp
211dc 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34 20 3d ){. char *zP4 =
211dd 20 7a 54 65 6d 70 3b 0a 20 20 61 73 73 65 72 74 zTemp;. assert
211de 28 20 6e 54 65 6d 70 3e 3d 32 30 20 29 3b 0a 20 ( nTemp>=20 );.
211df 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 70 34 switch( pOp->p4
211e0 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 type ){. case
211e1 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54 P4_KEYINFO_STAT
211e2 49 43 3a 0a 20 20 20 20 63 61 73 65 20 50 34 5f IC:. case P4_
211e3 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 20 20 20 20 KEYINFO: {.
211e4 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20 20 int i, j;.
211e5 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
211e6 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 fo = pOp->p4.pKe
211e7 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 61 73 73 yInfo;. ass
211e8 65 72 74 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 ert( pKeyInfo->a
211e9 53 6f 72 74 4f 72 64 65 72 21 3d 30 20 29 3b 0a SortOrder!=0 );.
211ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
211eb 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 printf(nTemp, zT
211ec 65 6d 70 2c 20 22 6b 65 79 69 6e 66 6f 28 25 64 emp, "keyinfo(%d
211ed 22 2c 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 ", pKeyInfo->nFi
211ee 65 6c 64 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 eld);. i =
211ef 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
211f0 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 66 6f zTemp);. fo
211f1 72 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 6e 66 r(j=0; j<pKeyInf
211f2 6f 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b o->nField; j++){
211f3 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 . CollSeq
211f4 20 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e *pColl = pKeyIn
211f5 66 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 fo->aColl[j];.
211f6 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 if( pColl
211f7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 ){. int
211f8 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c n = sqlite3Strl
211f9 65 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d en30(pColl->zNam
211fa 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 e);. if
211fb 28 20 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b ( i+n>nTemp-6 ){
211fc 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d . mem
211fd 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c cpy(&zTemp[i],",
211fe 2e 2e 2e 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 ...",4);.
211ff 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21200 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
21201 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 zTemp[i++] = '
21202 2c 27 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ,';. if
21203 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 ( pKeyInfo->aSor
21204 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20 20 20 tOrder[j] ){.
21205 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 zTemp[i
21206 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 ++] = '-';.
21207 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
21208 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b 69 memcpy(&zTemp[i
21209 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c ], pColl->zName,
2120a 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 n+1);.
2120b 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20 i += n;.
2120c 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e 54 }else if( i+4<nT
2120d 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 20 emp-6 ){.
2120e 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 memcpy(&zTemp
2120f 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0a 20 [i],",nil",4);.
21210 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34 3b i += 4;
21211 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
21212 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 }. zTemp[i
21213 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20 20 20 ++] = ')';.
21214 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b 0a 20 zTemp[i] = 0;.
21215 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e assert( i<n
21216 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 20 62 72 Temp );. br
21217 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
21218 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 51 3a 20 ase P4_COLLSEQ:
21219 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 {. CollSeq
2121a 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d 3e 70 34 *pColl = pOp->p4
2121b 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 73 71 .pColl;. sq
2121c 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
2121d 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 63 6f Temp, zTemp, "co
2121e 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 2c 20 70 llseq(%.20s)", p
2121f 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 Coll->zName);.
21220 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21221 0a 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e . case P4_FUN
21222 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 46 75 CDEF: {. Fu
21223 6e 63 44 65 66 20 2a 70 44 65 66 20 3d 20 70 4f ncDef *pDef = pO
21224 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 20 p->p4.pFunc;.
21225 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
21226 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 ntf(nTemp, zTemp
21227 2c 20 22 25 73 28 25 64 29 22 2c 20 70 44 65 66 , "%s(%d)", pDef
21228 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e 6e ->zName, pDef->n
21229 41 72 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 Arg);. brea
2122a 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
2122b 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b 0a 20 20 e P4_INT64: {.
2122c 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
2122d 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
2122e 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 4f 70 2d p, "%lld", *pOp-
2122f 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 20 20 20 >p4.pI64);.
21230 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
21231 20 20 63 61 73 65 20 50 34 5f 49 4e 54 33 32 3a case P4_INT32:
21232 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 {. sqlite3
21233 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c _snprintf(nTemp,
21234 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c 20 70 4f zTemp, "%d", pO
21235 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 20 20 20 p->p4.i);.
21236 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
21237 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 20 7b case P4_REAL: {
21238 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
21239 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a nprintf(nTemp, z
2123a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 2a Temp, "%.16g", *
2123b 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 3b 0a pOp->p4.pReal);.
2123c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
2123d 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 4d }. case P4_M
2123e 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 EM: {. Mem
2123f 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e 70 34 2e *pMem = pOp->p4.
21240 70 4d 65 6d 3b 0a 20 20 20 20 20 20 69 66 28 20 pMem;. if(
21241 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
21242 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 M_Str ){.
21243 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a zP4 = pMem->z;.
21244 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
21245 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
21246 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 M_Int ){.
21247 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
21248 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 f(nTemp, zTemp,
21249 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e "%lld", pMem->u.
2124a 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 i);. }else
2124b 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 if( pMem->flags
2124c 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 & MEM_Real ){.
2124d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
2124e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 printf(nTemp, zT
2124f 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d emp, "%.16g", pM
21250 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 65 em->r);. }e
21251 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c lse if( pMem->fl
21252 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 ags & MEM_Null )
21253 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
21254 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
21255 2c 20 7a 54 65 6d 70 2c 20 22 4e 55 4c 4c 22 29 , zTemp, "NULL")
21256 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
21257 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
21258 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
21259 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 20 _Blob );.
2125a 20 7a 50 34 20 3d 20 22 28 62 6c 6f 62 29 22 3b zP4 = "(blob)";
2125b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
2125c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e reak;. }.#ifn
2125d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
2125e 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 VIRTUALTABLE.
2125f 20 63 61 73 65 20 50 34 5f 56 54 41 42 3a 20 7b case P4_VTAB: {
21260 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 . sqlite3_v
21261 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 tab *pVtab = pOp
21262 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 ->p4.pVtab->pVta
21263 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 b;. sqlite3
21264 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c _snprintf(nTemp,
21265 20 7a 54 65 6d 70 2c 20 22 76 74 61 62 3a 25 70 zTemp, "vtab:%p
21266 3a 25 70 22 2c 20 70 56 74 61 62 2c 20 70 56 74 :%p", pVtab, pVt
21267 61 62 2d 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 ab->pModule);.
21268 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21269 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 .#endif. case
2126a 20 50 34 5f 49 4e 54 41 52 52 41 59 3a 20 7b 0a P4_INTARRAY: {.
2126b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
2126c 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 printf(nTemp, zT
2126d 65 6d 70 2c 20 22 69 6e 74 61 72 72 61 79 22 29 emp, "intarray")
2126e 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
2126f 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 }. case P4
21270 5f 53 55 42 50 52 4f 47 52 41 4d 3a 20 7b 0a 20 _SUBPROGRAM: {.
21271 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
21272 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 rintf(nTemp, zTe
21273 6d 70 2c 20 22 70 72 6f 67 72 61 6d 22 29 3b 0a mp, "program");.
21274 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21275 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 41 }. case P4_A
21276 44 56 41 4e 43 45 3a 20 7b 0a 20 20 20 20 20 20 DVANCE: {.
21277 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20 zTemp[0] = 0;.
21278 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21279 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a . default: {.
2127a 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4f 70 2d zP4 = pOp-
2127b 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 69 66 28 >p4.z;. if(
2127c 20 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20 20 zP4==0 ){.
2127d 20 20 20 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a zP4 = zTemp;.
2127e 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 30 5d zTemp[0]
2127f 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
21280 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 }. }. assert
21281 28 20 7a 50 34 21 3d 30 20 29 3b 0a 20 20 72 65 ( zP4!=0 );. re
21282 74 75 72 6e 20 7a 50 34 3b 0a 7d 0a 23 65 6e 64 turn zP4;.}.#end
21283 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61 72 if../*.** Declar
21284 65 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68 e to the Vdbe th
21285 61 74 20 74 68 65 20 42 54 72 65 65 20 6f 62 6a at the BTree obj
21286 65 63 74 20 61 74 20 64 62 2d 3e 61 44 62 5b 69 ect at db->aDb[i
21287 5d 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a ] is used..**.**
21288 20 54 68 65 20 70 72 65 70 61 72 65 64 20 73 74 The prepared st
21289 61 74 65 6d 65 6e 74 73 20 6e 65 65 64 20 74 6f atements need to
2128a 20 6b 6e 6f 77 20 69 6e 20 61 64 76 61 6e 63 65 know in advance
2128b 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 73 65 the complete se
2128c 74 20 6f 66 0a 2a 2a 20 61 74 74 61 63 68 65 64 t of.** attached
2128d 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20 databases that
2128e 77 69 6c 6c 20 62 65 20 75 73 65 2e 20 20 41 20 will be use. A
2128f 6d 61 73 6b 20 6f 66 20 74 68 65 73 65 20 64 61 mask of these da
21290 74 61 62 61 73 65 73 0a 2a 2a 20 69 73 20 6d 61 tabases.** is ma
21291 69 6e 74 61 69 6e 65 64 20 69 6e 20 70 2d 3e 62 intained in p->b
21292 74 72 65 65 4d 61 73 6b 2e 20 20 54 68 65 20 70 treeMask. The p
21293 2d 3e 6c 6f 63 6b 4d 61 73 6b 20 76 61 6c 75 65 ->lockMask value
21294 20 69 73 20 74 68 65 20 73 75 62 73 65 74 20 6f is the subset o
21295 66 0a 2a 2a 20 70 2d 3e 62 74 72 65 65 4d 61 73 f.** p->btreeMas
21296 6b 20 6f 66 20 64 61 74 61 62 61 73 65 73 20 74 k of databases t
21297 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65 hat will require
21298 20 61 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 a lock..*/.SQLI
21299 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
2129a 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 sqlite3VdbeUsesB
2129b 74 72 65 65 28 56 64 62 65 20 2a 70 2c 20 69 6e tree(Vdbe *p, in
2129c 74 20 69 29 7b 0a 20 20 61 73 73 65 72 74 28 20 t i){. assert(
2129d 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 64 62 2d i>=0 && i<p->db-
2129e 3e 6e 44 62 20 26 26 20 69 3c 28 69 6e 74 29 73 >nDb && i<(int)s
2129f 69 7a 65 6f 66 28 79 44 62 4d 61 73 6b 29 2a 38 izeof(yDbMask)*8
212a0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3c );. assert( i<
212a1 28 69 6e 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62 (int)sizeof(p->b
212a2 74 72 65 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20 treeMask)*8 );.
212a3 20 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 7c 3d p->btreeMask |=
212a4 20 28 28 79 44 62 4d 61 73 6b 29 31 29 3c 3c 69 ((yDbMask)1)<<i
212a5 3b 0a 20 20 69 66 28 20 69 21 3d 31 20 26 26 20 ;. if( i!=1 &&
212a6 73 71 6c 69 74 65 33 42 74 72 65 65 53 68 61 72 sqlite3BtreeShar
212a7 61 62 6c 65 28 70 2d 3e 64 62 2d 3e 61 44 62 5b able(p->db->aDb[
212a8 69 5d 2e 70 42 74 29 20 29 7b 0a 20 20 20 20 70 i].pBt) ){. p
212a9 2d 3e 6c 6f 63 6b 4d 61 73 6b 20 7c 3d 20 28 28 ->lockMask |= ((
212aa 79 44 62 4d 61 73 6b 29 31 29 3c 3c 69 3b 0a 20 yDbMask)1)<<i;.
212ab 20 7d 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e }.}..#if !defin
212ac 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
212ad 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 HARED_CACHE) &&
212ae 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
212af 45 3e 30 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c E>0./*.** If SQL
212b0 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 ite is compiled
212b1 74 6f 20 73 75 70 70 6f 72 74 20 73 68 61 72 65 to support share
212b2 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 61 6e 64 d-cache mode and
212b3 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 61 66 to be threadsaf
212b4 65 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 e,.** this routi
212b5 6e 65 20 6f 62 74 61 69 6e 73 20 74 68 65 20 6d ne obtains the m
212b6 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 utex associated
212b7 77 69 74 68 20 65 61 63 68 20 42 74 53 68 61 72 with each BtShar
212b8 65 64 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 ed structure.**
212b9 74 68 61 74 20 6d 61 79 20 62 65 20 61 63 63 65 that may be acce
212ba 73 73 65 64 20 62 79 20 74 68 65 20 56 4d 20 70 ssed by the VM p
212bb 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 assed as an argu
212bc 6d 65 6e 74 2e 20 49 6e 20 64 6f 69 6e 67 20 73 ment. In doing s
212bd 6f 20 69 74 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 o it also.** set
212be 73 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 64 s the BtShared.d
212bf 62 20 6d 65 6d 62 65 72 20 6f 66 20 65 61 63 68 b member of each
212c0 20 6f 66 20 74 68 65 20 42 74 53 68 61 72 65 64 of the BtShared
212c1 20 73 74 72 75 63 74 75 72 65 73 2c 20 65 6e 73 structures, ens
212c2 75 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74 68 uring.** that th
212c3 65 20 63 6f 72 72 65 63 74 20 62 75 73 79 2d 68 e correct busy-h
212c4 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 andler callback
212c5 69 73 20 69 6e 76 6f 6b 65 64 20 69 66 20 72 65 is invoked if re
212c6 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 quired..**.** If
212c7 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 SQLite is not t
212c8 68 72 65 61 64 73 61 66 65 20 62 75 74 20 64 6f hreadsafe but do
212c9 65 73 20 73 75 70 70 6f 72 74 20 73 68 61 72 65 es support share
212ca 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 d-cache mode, th
212cb 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 en.** sqlite3Btr
212cc 65 65 45 6e 74 65 72 28 29 20 69 73 20 69 6e 76 eeEnter() is inv
212cd 6f 6b 65 64 20 74 6f 20 73 65 74 20 74 68 65 20 oked to set the
212ce 42 74 53 68 61 72 65 64 2e 64 62 20 76 61 72 69 BtShared.db vari
212cf 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 61 6c 6c 20 ables.** of all
212d0 6f 66 20 42 74 53 68 61 72 65 64 20 73 74 72 75 of BtShared stru
212d1 63 74 75 72 65 73 20 61 63 63 65 73 73 69 62 6c ctures accessibl
212d2 65 20 76 69 61 20 74 68 65 20 64 61 74 61 62 61 e via the databa
212d3 73 65 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 61 73 se handle .** as
212d4 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
212d5 65 20 56 4d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 e VM..**.** If S
212d6 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 68 72 QLite is not thr
212d7 65 61 64 73 61 66 65 20 61 6e 64 20 64 6f 65 73 eadsafe and does
212d8 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 68 61 not support sha
212d9 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 red-cache mode,
212da 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e this.** function
212db 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
212dc 2a 2a 20 54 68 65 20 70 2d 3e 62 74 72 65 65 4d ** The p->btreeM
212dd 61 73 6b 20 66 69 65 6c 64 20 69 73 20 61 20 62 ask field is a b
212de 69 74 6d 61 73 6b 20 6f 66 20 61 6c 6c 20 62 74 itmask of all bt
212df 72 65 65 73 20 74 68 61 74 20 74 68 65 20 70 72 rees that the pr
212e0 65 70 61 72 65 64 20 0a 2a 2a 20 73 74 61 74 65 epared .** state
212e1 6d 65 6e 74 20 70 20 77 69 6c 6c 20 65 76 65 72 ment p will ever
212e2 20 75 73 65 2e 20 20 4c 65 74 20 4e 20 62 65 20 use. Let N be
212e3 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 the number of bi
212e4 74 73 20 69 6e 20 70 2d 3e 62 74 72 65 65 4d 61 ts in p->btreeMa
212e5 73 6b 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 sk.** correspond
212e6 69 6e 67 20 74 6f 20 62 74 72 65 65 73 20 74 68 ing to btrees th
212e7 61 74 20 75 73 65 20 73 68 61 72 65 64 20 63 61 at use shared ca
212e8 63 68 65 2e 20 20 54 68 65 6e 20 74 68 65 20 72 che. Then the r
212e9 75 6e 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 69 untime of.** thi
212ea 73 20 72 6f 75 74 69 6e 65 20 69 73 20 4e 2a 4e s routine is N*N
212eb 2e 20 20 42 75 74 20 61 73 20 4e 20 69 73 20 72 . But as N is r
212ec 61 72 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 arely more than
212ed 31 2c 20 74 68 69 73 20 73 68 6f 75 6c 64 20 6e 1, this should n
212ee 6f 74 0a 2a 2a 20 62 65 20 61 20 70 72 6f 62 6c ot.** be a probl
212ef 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 em..*/.SQLITE_PR
212f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
212f1 65 33 56 64 62 65 45 6e 74 65 72 28 56 64 62 65 e3VdbeEnter(Vdbe
212f2 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *p){. int i;.
212f3 20 79 44 62 4d 61 73 6b 20 6d 61 73 6b 3b 0a 20 yDbMask mask;.
212f4 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 sqlite3 *db;.
212f5 44 62 20 2a 61 44 62 3b 0a 20 20 69 6e 74 20 6e Db *aDb;. int n
212f6 44 62 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f 63 Db;. if( p->loc
212f7 6b 4d 61 73 6b 3d 3d 30 20 29 20 72 65 74 75 72 kMask==0 ) retur
212f8 6e 3b 20 20 2f 2a 20 54 68 65 20 63 6f 6d 6d 6f n; /* The commo
212f9 6e 20 63 61 73 65 20 2a 2f 0a 20 20 64 62 20 3d n case */. db =
212fa 20 70 2d 3e 64 62 3b 0a 20 20 61 44 62 20 3d 20 p->db;. aDb =
212fb 64 62 2d 3e 61 44 62 3b 0a 20 20 6e 44 62 20 3d db->aDb;. nDb =
212fc 20 64 62 2d 3e 6e 44 62 3b 0a 20 20 66 6f 72 28 db->nDb;. for(
212fd 69 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 3c 6e i=0, mask=1; i<n
212fe 44 62 3b 20 69 2b 2b 2c 20 6d 61 73 6b 20 2b 3d Db; i++, mask +=
212ff 20 6d 61 73 6b 29 7b 0a 20 20 20 20 69 66 28 20 mask){. if(
21300 69 21 3d 31 20 26 26 20 28 6d 61 73 6b 20 26 20 i!=1 && (mask &
21301 70 2d 3e 6c 6f 63 6b 4d 61 73 6b 29 21 3d 30 20 p->lockMask)!=0
21302 26 26 20 41 4c 57 41 59 53 28 61 44 62 5b 69 5d && ALWAYS(aDb[i]
21303 2e 70 42 74 21 3d 30 29 20 29 7b 0a 20 20 20 20 .pBt!=0) ){.
21304 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
21305 74 65 72 28 61 44 62 5b 69 5d 2e 70 42 74 29 3b ter(aDb[i].pBt);
21306 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e . }. }.}.#en
21307 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 dif..#if !define
21308 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 d(SQLITE_OMIT_SH
21309 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 53 ARED_CACHE) && S
2130a 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
2130b 3e 30 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 >0./*.** Unlock
2130c 61 6c 6c 20 6f 66 20 74 68 65 20 62 74 72 65 65 all of the btree
2130d 73 20 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 s previously loc
2130e 6b 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f ked by a call to
2130f 20 73 71 6c 69 74 65 33 56 64 62 65 45 6e 74 65 sqlite3VdbeEnte
21310 72 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 r()..*/.SQLITE_P
21311 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
21312 74 65 33 56 64 62 65 4c 65 61 76 65 28 56 64 62 te3VdbeLeave(Vdb
21313 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a e *p){. int i;.
21314 20 20 79 44 62 4d 61 73 6b 20 6d 61 73 6b 3b 0a yDbMask mask;.
21315 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 sqlite3 *db;.
21316 20 44 62 20 2a 61 44 62 3b 0a 20 20 69 6e 74 20 Db *aDb;. int
21317 6e 44 62 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f nDb;. if( p->lo
21318 63 6b 4d 61 73 6b 3d 3d 30 20 29 20 72 65 74 75 ckMask==0 ) retu
21319 72 6e 3b 20 20 2f 2a 20 54 68 65 20 63 6f 6d 6d rn; /* The comm
2131a 6f 6e 20 63 61 73 65 20 2a 2f 0a 20 20 64 62 20 on case */. db
2131b 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 44 62 20 3d = p->db;. aDb =
2131c 20 64 62 2d 3e 61 44 62 3b 0a 20 20 6e 44 62 20 db->aDb;. nDb
2131d 3d 20 64 62 2d 3e 6e 44 62 3b 0a 20 20 66 6f 72 = db->nDb;. for
2131e 28 69 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 3c (i=0, mask=1; i<
2131f 6e 44 62 3b 20 69 2b 2b 2c 20 6d 61 73 6b 20 2b nDb; i++, mask +
21320 3d 20 6d 61 73 6b 29 7b 0a 20 20 20 20 69 66 28 = mask){. if(
21321 20 69 21 3d 31 20 26 26 20 28 6d 61 73 6b 20 26 i!=1 && (mask &
21322 20 70 2d 3e 6c 6f 63 6b 4d 61 73 6b 29 21 3d 30 p->lockMask)!=0
21323 20 26 26 20 41 4c 57 41 59 53 28 61 44 62 5b 69 && ALWAYS(aDb[i
21324 5d 2e 70 42 74 21 3d 30 29 20 29 7b 0a 20 20 20 ].pBt!=0) ){.
21325 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
21326 65 61 76 65 28 61 44 62 5b 69 5d 2e 70 42 74 29 eave(aDb[i].pBt)
21327 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 ;. }. }.}.#e
21328 6e 64 69 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 ndif..#if define
21329 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 d(VDBE_PROFILE)
2132a 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 || defined(SQLIT
2132b 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 50 E_DEBUG)./*.** P
2132c 72 69 6e 74 20 61 20 73 69 6e 67 6c 65 20 6f 70 rint a single op
2132d 63 6f 64 65 2e 20 20 54 68 69 73 20 72 6f 75 74 code. This rout
2132e 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 ine is used for
2132f 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a debugging only..
21330 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21331 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
21332 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 20 2a bePrintOp(FILE *
21333 70 4f 75 74 2c 20 69 6e 74 20 70 63 2c 20 4f 70 pOut, int pc, Op
21334 20 2a 70 4f 70 29 7b 0a 20 20 63 68 61 72 20 2a *pOp){. char *
21335 7a 50 34 3b 0a 20 20 63 68 61 72 20 7a 50 74 72 zP4;. char zPtr
21336 5b 35 30 5d 3b 0a 20 20 73 74 61 74 69 63 20 63 [50];. static c
21337 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
21338 61 74 31 20 3d 20 22 25 34 64 20 25 2d 31 33 73 at1 = "%4d %-13s
21339 20 25 34 64 20 25 34 64 20 25 34 64 20 25 2d 34 %4d %4d %4d %-4
2133a 73 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0a 20 20 s %.2X %s\n";.
2133b 69 66 28 20 70 4f 75 74 3d 3d 30 20 29 20 70 4f if( pOut==0 ) pO
2133c 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7a ut = stdout;. z
2133d 50 34 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70 P4 = displayP4(p
2133e 4f 70 2c 20 7a 50 74 72 2c 20 73 69 7a 65 6f 66 Op, zPtr, sizeof
2133f 28 7a 50 74 72 29 29 3b 0a 20 20 66 70 72 69 6e (zPtr));. fprin
21340 74 66 28 70 4f 75 74 2c 20 7a 46 6f 72 6d 61 74 tf(pOut, zFormat
21341 31 2c 20 70 63 2c 20 0a 20 20 20 20 20 20 73 71 1, pc, . sq
21342 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 lite3OpcodeName(
21343 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c 20 70 4f pOp->opcode), pO
21344 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 p->p1, pOp->p2,
21345 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c 20 70 4f pOp->p3, zP4, pO
21346 70 2d 3e 70 35 2c 0a 23 69 66 64 65 66 20 53 51 p->p5,.#ifdef SQ
21347 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 LITE_DEBUG.
21348 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3f pOp->zComment ?
21349 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3a pOp->zComment :
2134a 20 22 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 "".#else.
2134b 22 22 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a 20 "".#endif. );.
2134c 20 66 66 6c 75 73 68 28 70 4f 75 74 29 3b 0a 7d fflush(pOut);.}
2134d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
2134e 65 6c 65 61 73 65 20 61 6e 20 61 72 72 61 79 20 elease an array
2134f 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e 74 of N Mem element
21350 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 s.*/.static void
21351 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 releaseMemArray
21352 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 7b (Mem *p, int N){
21353 0a 20 20 69 66 28 20 70 20 26 26 20 4e 20 29 7b . if( p && N ){
21354 0a 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 0a . Mem *pEnd;.
21355 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 sqlite3 *db
21356 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 75 38 20 = p->db;. u8
21357 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 3d 20 malloc_failed =
21358 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
21359 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 70 6e ;. if( db->pn
2135a 42 79 74 65 73 46 72 65 65 64 20 29 7b 0a 20 20 BytesFreed ){.
2135b 20 20 20 20 66 6f 72 28 70 45 6e 64 3d 26 70 5b for(pEnd=&p[
2135c 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 2b 2b 29 N]; p<pEnd; p++)
2135d 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
2135e 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
2135f 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 7d Malloc);. }
21360 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
21361 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 45 6e }. for(pEn
21362 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b d=&p[N]; p<pEnd;
21363 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 p++){. ass
21364 65 72 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 45 ert( (&p[1])==pE
21365 6e 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70 nd || p[0].db==p
21366 5b 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20 20 [1].db );..
21367 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 69 /* This block i
21368 73 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c 69 s really an inli
21369 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 ned version of s
2136a 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
2136b 65 61 73 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 ease(). **
2136c 74 68 61 74 20 74 61 6b 65 73 20 61 64 76 61 6e that takes advan
2136d 74 61 67 65 20 6f 66 20 74 68 65 20 66 61 63 74 tage of the fact
2136e 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 that the memory
2136f 20 63 65 6c 6c 20 76 61 6c 75 65 20 69 73 20 0a cell value is .
21370 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 73 ** being s
21371 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65 72 et to NULL after
21372 20 72 65 6c 65 61 73 69 6e 67 20 61 6e 79 20 64 releasing any d
21373 79 6e 61 6d 69 63 20 72 65 73 6f 75 72 63 65 73 ynamic resources
21374 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 .. **.
21375 20 2a 2a 20 54 68 65 20 6a 75 73 74 69 66 69 63 ** The justific
21376 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63 ation for duplic
21377 61 74 69 6e 67 20 63 6f 64 65 20 69 73 20 74 68 ating code is th
21378 61 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 at according to
21379 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67 72 . ** callgr
2137a 69 6e 64 2c 20 74 68 69 73 20 63 61 75 73 65 73 ind, this causes
2137b 20 61 20 63 65 72 74 61 69 6e 20 74 65 73 74 20 a certain test
2137c 63 61 73 65 20 74 6f 20 68 69 74 20 74 68 65 20 case to hit the
2137d 43 50 55 20 34 2e 37 20 0a 20 20 20 20 20 20 2a CPU 4.7 . *
2137e 2a 20 70 65 72 63 65 6e 74 20 6c 65 73 73 20 28 * percent less (
2137f 78 38 36 20 6c 69 6e 75 78 2c 20 67 63 63 20 76 x86 linux, gcc v
21380 65 72 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d 4f ersion 4.1.2, -O
21381 36 29 20 74 68 61 6e 20 69 66 20 0a 20 20 20 20 6) than if .
21382 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 52 ** sqlite3MemR
21383 65 6c 65 61 73 65 28 29 20 77 65 72 65 20 63 61 elease() were ca
21384 6c 6c 65 64 20 66 72 6f 6d 20 68 65 72 65 2e 20 lled from here.
21385 57 69 74 68 20 2d 4f 32 2c 20 74 68 69 73 20 6a With -O2, this j
21386 75 6d 70 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f umps. ** to
21387 20 36 2e 36 20 70 65 72 63 65 6e 74 2e 20 54 68 6.6 percent. Th
21388 65 20 74 65 73 74 20 63 61 73 65 20 69 73 20 69 e test case is i
21389 6e 73 65 72 74 69 6e 67 20 31 30 30 30 20 72 6f nserting 1000 ro
2138a 77 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 ws into a table
2138b 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 6e . ** with n
2138c 6f 20 69 6e 64 65 78 65 73 20 75 73 69 6e 67 20 o indexes using
2138d 61 20 73 69 6e 67 6c 65 20 70 72 65 70 61 72 65 a single prepare
2138e 64 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 d INSERT stateme
2138f 6e 74 2c 20 62 69 6e 64 28 29 20 0a 20 20 20 20 nt, bind() .
21390 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 28 29 ** and reset()
21391 2e 20 49 6e 73 65 72 74 73 20 61 72 65 20 67 72 . Inserts are gr
21392 6f 75 70 65 64 20 69 6e 74 6f 20 61 20 74 72 61 ouped into a tra
21393 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 nsaction..
21394 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e */. if( p->
21395 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d flags&(MEM_Agg|M
21396 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 46 72 61 6d 65 EM_Dyn|MEM_Frame
21397 7c 4d 45 4d 5f 52 6f 77 53 65 74 29 20 29 7b 0a |MEM_RowSet) ){.
21398 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
21399 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 29 dbeMemRelease(p)
2139a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
2139b 28 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a ( p->zMalloc ){.
2139c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
2139d 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 4d 61 bFree(db, p->zMa
2139e 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 70 lloc);. p
2139f 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 ->zMalloc = 0;.
213a0 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 70 2d }.. p-
213a1 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 76 >flags = MEM_Inv
213a2 61 6c 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 alid;. }.
213a3 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
213a4 20 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 = malloc_failed
213a5 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ;. }.}../*.** D
213a6 65 6c 65 74 65 20 61 20 56 64 62 65 46 72 61 6d elete a VdbeFram
213a7 65 20 6f 62 6a 65 63 74 20 61 6e 64 20 69 74 73 e object and its
213a8 20 63 6f 6e 74 65 6e 74 73 2e 20 56 64 62 65 46 contents. VdbeF
213a9 72 61 6d 65 20 6f 62 6a 65 63 74 73 20 61 72 65 rame objects are
213aa 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 .** allocated by
213ab 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 the OP_Program
213ac 6f 70 63 6f 64 65 20 69 6e 20 73 71 6c 69 74 65 opcode in sqlite
213ad 33 56 64 62 65 45 78 65 63 28 29 2e 0a 2a 2f 0a 3VdbeExec()..*/.
213ae 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
213af 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 oid sqlite3VdbeF
213b0 72 61 6d 65 44 65 6c 65 74 65 28 56 64 62 65 46 rameDelete(VdbeF
213b1 72 61 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 rame *p){. int
213b2 69 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d i;. Mem *aMem =
213b3 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29 VdbeFrameMem(p)
213b4 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a ;. VdbeCursor *
213b5 2a 61 70 43 73 72 20 3d 20 28 56 64 62 65 43 75 *apCsr = (VdbeCu
213b6 72 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d rsor **)&aMem[p-
213b7 3e 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 66 >nChildMem];. f
213b8 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 68 or(i=0; i<p->nCh
213b9 69 6c 64 43 73 72 3b 20 69 2b 2b 29 7b 0a 20 20 ildCsr; i++){.
213ba 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 sqlite3VdbeFre
213bb 65 43 75 72 73 6f 72 28 70 2d 3e 76 2c 20 61 70 eCursor(p->v, ap
213bc 43 73 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 Csr[i]);. }. r
213bd 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 61 eleaseMemArray(a
213be 4d 65 6d 2c 20 70 2d 3e 6e 43 68 69 6c 64 4d 65 Mem, p->nChildMe
213bf 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 m);. sqlite3DbF
213c0 72 65 65 28 70 2d 3e 76 2d 3e 64 62 2c 20 70 29 ree(p->v->db, p)
213c1 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
213c2 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e ITE_OMIT_EXPLAIN
213c3 0a 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20 6c 69 ./*.** Give a li
213c4 73 74 69 6e 67 20 6f 66 20 74 68 65 20 70 72 6f sting of the pro
213c5 67 72 61 6d 20 69 6e 20 74 68 65 20 76 69 72 74 gram in the virt
213c6 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a ual machine..**.
213c7 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 ** The interface
213c8 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
213c9 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 sqlite3VdbeExec(
213ca 29 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64 20 ). But instead
213cb 6f 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 74 68 of.** running th
213cc 65 20 63 6f 64 65 2c 20 69 74 20 69 6e 76 6f 6b e code, it invok
213cd 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 es the callback
213ce 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e once for each in
213cf 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 struction..** Th
213d0 69 73 20 66 65 61 74 75 72 65 20 69 73 20 75 73 is feature is us
213d1 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 ed to implement
213d2 22 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a "EXPLAIN"..**.**
213d3 20 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69 6e When p->explain
213d4 3d 3d 31 2c 20 65 61 63 68 20 69 6e 73 74 72 75 ==1, each instru
213d5 63 74 69 6f 6e 20 69 73 20 6c 69 73 74 65 64 2e ction is listed.
213d6 20 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 78 70 When.** p->exp
213d7 6c 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 4f 50 lain==2, only OP
213d8 5f 45 78 70 6c 61 69 6e 20 69 6e 73 74 72 75 63 _Explain instruc
213d9 74 69 6f 6e 73 20 61 72 65 20 6c 69 73 74 65 64 tions are listed
213da 20 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20 61 72 and these.** ar
213db 65 20 73 68 6f 77 6e 20 69 6e 20 61 20 64 69 66 e shown in a dif
213dc 66 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e 20 20 ferent format.
213dd 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 69 73 p->explain==2 is
213de 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 used to impleme
213df 6e 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 51 55 nt.** EXPLAIN QU
213e0 45 52 59 20 50 4c 41 4e 2e 0a 2a 2a 0a 2a 2a 20 ERY PLAN..**.**
213e1 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69 6e 3d When p->explain=
213e2 3d 31 2c 20 66 69 72 73 74 20 74 68 65 20 6d 61 =1, first the ma
213e3 69 6e 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 69 in program is li
213e4 73 74 65 64 2c 20 74 68 65 6e 20 65 61 63 68 20 sted, then each
213e5 6f 66 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 of.** the trigge
213e6 72 20 73 75 62 70 72 6f 67 72 61 6d 73 20 61 72 r subprograms ar
213e7 65 20 6c 69 73 74 65 64 20 6f 6e 65 20 62 79 20 e listed one by
213e8 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 one..*/.SQLITE_P
213e9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
213ea 65 33 56 64 62 65 4c 69 73 74 28 0a 20 20 56 64 e3VdbeList(. Vd
213eb 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 be *p
213ec 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
213ed 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 DBE */.){. int
213ee 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20 nRow;
213ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
213f0 20 2f 2a 20 53 74 6f 70 20 77 68 65 6e 20 72 6f /* Stop when ro
213f1 77 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 w count reaches
213f2 74 68 69 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 this */. int nS
213f3 75 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ub = 0;
213f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
213f5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 75 62 2d * Number of sub-
213f6 76 64 62 65 73 20 73 65 65 6e 20 73 6f 20 66 61 vdbes seen so fa
213f7 72 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 r */. SubProgra
213f8 6d 20 2a 2a 61 70 53 75 62 20 3d 20 30 3b 20 20 m **apSub = 0;
213f9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
213fa 72 72 61 79 20 6f 66 20 73 75 62 2d 76 64 62 65 rray of sub-vdbe
213fb 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 53 75 62 s */. Mem *pSub
213fc 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
213fd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
213fe 65 6d 6f 72 79 20 63 65 6c 6c 20 68 6f 6c 64 20 emory cell hold
213ff 61 72 72 61 79 20 6f 66 20 73 75 62 70 72 6f 67 array of subprog
21400 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a s */. sqlite3 *
21401 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20 db = p->db;
21402 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
21403 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
21404 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 ection */. int
21405 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
21406 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21407 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
21408 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 */. int rc = S
21409 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 QLITE_OK;
2140a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
2140b 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 4d turn code */. M
2140c 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 em *pMem = &p->a
2140d 4d 65 6d 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 Mem[1];
2140e 20 20 20 20 2f 2a 20 46 69 72 73 74 20 4d 65 6d /* First Mem
2140f 20 6f 66 20 72 65 73 75 6c 74 20 73 65 74 20 2a of result set *
21410 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e /.. assert( p->
21411 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20 61 73 73 explain );. ass
21412 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 ert( p->magic==V
21413 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 3b DBE_MAGIC_RUN );
21414 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 . assert( p->rc
21415 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 ==SQLITE_OK || p
21416 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 ->rc==SQLITE_BUS
21417 59 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 Y || p->rc==SQLI
21418 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f TE_NOMEM );.. /
21419 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 * Even though th
2141a 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e is opcode does n
2141b 6f 74 20 75 73 65 20 64 79 6e 61 6d 69 63 20 73 ot use dynamic s
2141c 74 72 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 trings for. **
2141d 74 68 65 20 72 65 73 75 6c 74 2c 20 72 65 73 75 the result, resu
2141e 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 lt columns may b
2141f 65 63 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69 66 ecome dynamic if
21420 20 74 68 65 20 75 73 65 72 20 63 61 6c 6c 73 0a the user calls.
21421 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c ** sqlite3_col
21422 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 20 63 61 umn_text16(), ca
21423 75 73 69 6e 67 20 61 20 74 72 61 6e 73 6c 61 74 using a translat
21424 69 6f 6e 20 74 6f 20 55 54 46 2d 31 36 20 65 6e ion to UTF-16 en
21425 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72 coding.. */. r
21426 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 eleaseMemArray(p
21427 4d 65 6d 2c 20 38 29 3b 0a 20 20 70 2d 3e 70 52 Mem, 8);. p->pR
21428 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 0a 20 esultSet = 0;..
21429 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 if( p->rc==SQLI
2142a 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 TE_NOMEM ){.
2142b 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 /* This happens
2142c 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e if a malloc() in
2142d 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73 side a call to s
2142e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
2142f 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73 xt() or. ** s
21430 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
21431 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20 xt16() failed.
21432 2a 2f 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f */. db->mallo
21433 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 cFailed = 1;.
21434 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
21435 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 RROR;. }.. /*
21436 57 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 When the number
21437 6f 66 20 6f 75 74 70 75 74 20 72 6f 77 73 20 72 of output rows r
21438 65 61 63 68 65 73 20 6e 52 6f 77 2c 20 74 68 61 eaches nRow, tha
21439 74 20 6d 65 61 6e 73 20 74 68 65 0a 20 20 2a 2a t means the. **
2143a 20 6c 69 73 74 69 6e 67 20 68 61 73 20 66 69 6e listing has fin
2143b 69 73 68 65 64 20 61 6e 64 20 73 71 6c 69 74 65 ished and sqlite
2143c 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20 3_step() should
2143d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f return SQLITE_DO
2143e 4e 45 2e 0a 20 20 2a 2a 20 6e 52 6f 77 20 69 73 NE.. ** nRow is
2143f 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 the sum of the
21440 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 number of rows i
21441 6e 20 74 68 65 20 6d 61 69 6e 20 70 72 6f 67 72 n the main progr
21442 61 6d 2c 20 70 6c 75 73 0a 20 20 2a 2a 20 74 68 am, plus. ** th
21443 65 20 73 75 6d 20 6f 66 20 74 68 65 20 6e 75 6d e sum of the num
21444 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 61 ber of rows in a
21445 6c 6c 20 74 72 69 67 67 65 72 20 73 75 62 70 72 ll trigger subpr
21446 6f 67 72 61 6d 73 20 65 6e 63 6f 75 6e 74 65 72 ograms encounter
21447 65 64 0a 20 20 2a 2a 20 73 6f 20 66 61 72 2e 20 ed. ** so far.
21448 20 54 68 65 20 6e 52 6f 77 20 76 61 6c 75 65 20 The nRow value
21449 77 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 61 73 will increase as
2144a 20 6e 65 77 20 74 72 69 67 67 65 72 20 73 75 62 new trigger sub
2144b 70 72 6f 67 72 61 6d 73 20 61 72 65 0a 20 20 2a programs are. *
2144c 2a 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 62 * encountered, b
2144d 75 74 20 70 2d 3e 70 63 20 77 69 6c 6c 20 65 76 ut p->pc will ev
2144e 65 6e 74 75 61 6c 6c 79 20 63 61 74 63 68 20 75 entually catch u
2144f 70 20 74 6f 20 6e 52 6f 77 2e 0a 20 20 2a 2f 0a p to nRow.. */.
21450 20 20 6e 52 6f 77 20 3d 20 70 2d 3e 6e 4f 70 3b nRow = p->nOp;
21451 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 . if( p->explai
21452 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54 n==1 ){. /* T
21453 68 65 20 66 69 72 73 74 20 38 20 6d 65 6d 6f 72 he first 8 memor
21454 79 20 63 65 6c 6c 73 20 61 72 65 20 75 73 65 64 y cells are used
21455 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20 for the result
21456 73 65 74 2e 20 20 53 6f 20 77 65 20 77 69 6c 6c set. So we will
21457 0a 20 20 20 20 2a 2a 20 63 6f 6d 6d 61 6e 64 65 . ** commande
21458 65 72 20 74 68 65 20 39 74 68 20 63 65 6c 6c 20 er the 9th cell
21459 74 6f 20 75 73 65 20 61 73 20 73 74 6f 72 61 67 to use as storag
2145a 65 20 66 6f 72 20 61 6e 20 61 72 72 61 79 20 6f e for an array o
2145b 66 20 70 6f 69 6e 74 65 72 73 0a 20 20 20 20 2a f pointers. *
2145c 2a 20 74 6f 20 74 72 69 67 67 65 72 20 73 75 62 * to trigger sub
2145d 70 72 6f 67 72 61 6d 73 2e 20 20 54 68 65 20 56 programs. The V
2145e 44 42 45 20 69 73 20 67 75 61 72 61 6e 74 65 65 DBE is guarantee
2145f 64 20 74 6f 20 68 61 76 65 20 61 74 20 6c 65 61 d to have at lea
21460 73 74 20 39 0a 20 20 20 20 2a 2a 20 63 65 6c 6c st 9. ** cell
21461 73 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 s. */. asser
21462 74 28 20 70 2d 3e 6e 4d 65 6d 3e 39 20 29 3b 0a t( p->nMem>9 );.
21463 20 20 20 20 70 53 75 62 20 3d 20 26 70 2d 3e 61 pSub = &p->a
21464 4d 65 6d 5b 39 5d 3b 0a 20 20 20 20 69 66 28 20 Mem[9];. if(
21465 70 53 75 62 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pSub->flags&MEM_
21466 42 6c 6f 62 20 29 7b 0a 20 20 20 20 20 20 2f 2a Blob ){. /*
21467 20 4f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 On the first ca
21468 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 74 ll to sqlite3_st
21469 65 70 28 29 2c 20 70 53 75 62 20 77 69 6c 6c 20 ep(), pSub will
2146a 68 6f 6c 64 20 61 20 4e 55 4c 4c 2e 20 20 49 74 hold a NULL. It
2146b 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 6e 69 is. ** ini
2146c 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 42 4c tialized to a BL
2146d 4f 42 20 62 79 20 74 68 65 20 50 34 5f 53 55 42 OB by the P4_SUB
2146e 50 52 4f 47 52 41 4d 20 70 72 6f 63 65 73 73 69 PROGRAM processi
2146f 6e 67 20 6c 6f 67 69 63 20 62 65 6c 6f 77 20 2a ng logic below *
21470 2f 0a 20 20 20 20 20 20 6e 53 75 62 20 3d 20 70 /. nSub = p
21471 53 75 62 2d 3e 6e 2f 73 69 7a 65 6f 66 28 56 64 Sub->n/sizeof(Vd
21472 62 65 2a 29 3b 0a 20 20 20 20 20 20 61 70 53 75 be*);. apSu
21473 62 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d 20 b = (SubProgram
21474 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 **)pSub->z;.
21475 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
21476 3c 6e 53 75 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nSub; i++){.
21477 20 20 20 6e 52 6f 77 20 2b 3d 20 61 70 53 75 62 nRow += apSub
21478 5b 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a [i]->nOp;. }.
21479 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69 }.. do{. i
2147a 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 = p->pc++;. }w
2147b 68 69 6c 65 28 20 69 3c 6e 52 6f 77 20 26 26 20 hile( i<nRow &&
2147c 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26 p->explain==2 &&
2147d 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 p->aOp[i].opcod
2147e 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b e!=OP_Explain );
2147f 0a 20 20 69 66 28 20 69 3e 3d 6e 52 6f 77 20 29 . if( i>=nRow )
21480 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 {. p->rc = SQ
21481 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 LITE_OK;. rc
21482 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 = SQLITE_DONE;.
21483 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 75 }else if( db->u
21484 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 1.isInterrupted
21485 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 ){. p->rc = S
21486 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b QLITE_INTERRUPT;
21487 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
21488 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 _ERROR;. sqli
21489 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
2148a 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 >zErrMsg, db, "%
2148b 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 s", sqlite3ErrSt
2148c 72 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d 65 6c r(p->rc));. }el
2148d 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b se{. char *z;
2148e 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 . Op *pOp;.
2148f 20 20 69 66 28 20 69 3c 70 2d 3e 6e 4f 70 20 29 if( i<p->nOp )
21490 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f {. /* The o
21491 75 74 70 75 74 20 6c 69 6e 65 20 6e 75 6d 62 65 utput line numbe
21492 72 20 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 r is small enoug
21493 68 20 74 68 61 74 20 77 65 20 61 72 65 20 73 74 h that we are st
21494 69 6c 6c 20 69 6e 20 74 68 65 0a 20 20 20 20 20 ill in the.
21495 20 2a 2a 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d ** main program
21496 2e 20 2a 2f 0a 20 20 20 20 20 20 70 4f 70 20 3d . */. pOp =
21497 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 &p->aOp[i];.
21498 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a }else{. /*
21499 20 57 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c We are currentl
2149a 79 20 6c 69 73 74 69 6e 67 20 73 75 62 70 72 6f y listing subpro
2149b 67 72 61 6d 73 2e 20 20 46 69 67 75 72 65 20 6f grams. Figure o
2149c 75 74 20 77 68 69 63 68 20 6f 6e 65 20 61 6e 64 ut which one and
2149d 0a 20 20 20 20 20 20 2a 2a 20 70 69 63 6b 20 75 . ** pick u
2149e 70 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 p the appropriat
2149f 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a 20 20 20 e opcode. */.
214a0 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 int j;.
214a1 69 20 2d 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 20 i -= p->nOp;.
214a2 20 20 20 66 6f 72 28 6a 3d 30 3b 20 69 3e 3d 61 for(j=0; i>=a
214a3 70 53 75 62 5b 6a 5d 2d 3e 6e 4f 70 3b 20 6a 2b pSub[j]->nOp; j+
214a4 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2d 3d +){. i -=
214a5 20 61 70 53 75 62 5b 6a 5d 2d 3e 6e 4f 70 3b 0a apSub[j]->nOp;.
214a6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f }. pO
214a7 70 20 3d 20 26 61 70 53 75 62 5b 6a 5d 2d 3e 61 p = &apSub[j]->a
214a8 4f 70 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 Op[i];. }.
214a9 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d if( p->explain=
214aa 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d =1 ){. pMem
214ab 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
214ac 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 t;. pMem->t
214ad 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 ype = SQLITE_INT
214ae 45 47 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d EGER;. pMem
214af 2d 3e 75 2e 69 20 3d 20 69 3b 20 20 20 20 20 20 ->u.i = i;
214b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
214b1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f /* Pro
214b2 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a gram counter */.
214b3 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 pMem++;.
214b4 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 . pMem->fla
214b5 67 73 20 3d 20 4d 45 4d 5f 53 74 61 74 69 63 7c gs = MEM_Static|
214b6 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d MEM_Str|MEM_Term
214b7 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 ;. pMem->z
214b8 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 = (char*)sqlite3
214b9 4f 70 63 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e OpcodeName(pOp->
214ba 6f 70 63 6f 64 65 29 3b 20 20 2f 2a 20 4f 70 63 opcode); /* Opc
214bb 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 ode */. ass
214bc 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 ert( pMem->z!=0
214bd 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e );. pMem->n
214be 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
214bf 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 30(pMem->z);.
214c0 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
214c1 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 SQLITE_TEXT;.
214c2 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 pMem->enc = S
214c3 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 QLITE_UTF8;.
214c4 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 20 pMem++;..
214c5 20 2f 2a 20 57 68 65 6e 20 61 6e 20 4f 50 5f 50 /* When an OP_P
214c6 72 6f 67 72 61 6d 20 6f 70 63 6f 64 65 20 69 73 rogram opcode is
214c7 20 65 6e 63 6f 75 6e 74 65 72 20 28 74 68 65 20 encounter (the
214c8 6f 6e 6c 79 20 6f 70 63 6f 64 65 20 74 68 61 74 only opcode that
214c9 20 68 61 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 has. ** a
214ca 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 61 72 P4_SUBPROGRAM ar
214cb 67 75 6d 65 6e 74 29 2c 20 65 78 70 61 6e 64 20 gument), expand
214cc 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
214cd 61 72 72 61 79 20 6f 66 20 73 75 62 70 72 6f 67 array of subprog
214ce 72 61 6d 73 0a 20 20 20 20 20 20 2a 2a 20 6b 65 rams. ** ke
214cf 70 74 20 69 6e 20 70 2d 3e 61 4d 65 6d 5b 39 5d pt in p->aMem[9]
214d0 2e 7a 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e .z to hold the n
214d1 65 77 20 70 72 6f 67 72 61 6d 20 2d 20 61 73 73 ew program - ass
214d2 75 6d 69 6e 67 20 74 68 69 73 20 73 75 62 70 72 uming this subpr
214d3 6f 67 72 61 6d 0a 20 20 20 20 20 20 2a 2a 20 68 ogram. ** h
214d4 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 62 as not already b
214d5 65 65 6e 20 73 65 65 6e 2e 0a 20 20 20 20 20 20 een seen..
214d6 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 */. if( pOp
214d7 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 ->p4type==P4_SUB
214d8 50 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 PROGRAM ){.
214d9 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 int nByte = (
214da 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 nSub+1)*sizeof(S
214db 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 ubProgram*);.
214dc 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 int j;.
214dd 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e for(j=0; j<n
214de 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 Sub; j++){.
214df 20 20 20 20 20 69 66 28 20 61 70 53 75 62 5b 6a if( apSub[j
214e0 5d 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 ]==pOp->p4.pProg
214e1 72 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ram ) break;.
214e2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
214e3 66 28 20 6a 3d 3d 6e 53 75 62 20 26 26 20 53 51 f( j==nSub && SQ
214e4 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 LITE_OK==sqlite3
214e5 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 53 75 62 VdbeMemGrow(pSub
214e6 2c 20 6e 42 79 74 65 2c 20 6e 53 75 62 21 3d 30 , nByte, nSub!=0
214e7 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 ) ){. a
214e8 70 53 75 62 20 3d 20 28 53 75 62 50 72 6f 67 72 pSub = (SubProgr
214e9 61 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 am **)pSub->z;.
214ea 20 20 20 20 20 20 20 20 20 61 70 53 75 62 5b 6e apSub[n
214eb 53 75 62 2b 2b 5d 20 3d 20 70 4f 70 2d 3e 70 34 Sub++] = pOp->p4
214ec 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 20 20 20 .pProgram;.
214ed 20 20 20 20 20 70 53 75 62 2d 3e 66 6c 61 67 73 pSub->flags
214ee 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 |= MEM_Blob;.
214ef 20 20 20 20 20 20 20 20 70 53 75 62 2d 3e 6e 20 pSub->n
214f0 3d 20 6e 53 75 62 2a 73 69 7a 65 6f 66 28 53 75 = nSub*sizeof(Su
214f1 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 bProgram*);.
214f2 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
214f3 20 20 7d 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 }.. pMem->f
214f4 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
214f5 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 pMem->u.i =
214f6 70 4f 70 2d 3e 70 31 3b 20 20 20 20 20 20 20 20 pOp->p1;
214f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
214f8 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 20 20 20 70 /* P1 */. p
214f9 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
214fa 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 TE_INTEGER;.
214fb 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65 pMem++;.. pMe
214fc 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 m->flags = MEM_I
214fd 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e nt;. pMem->u.
214fe 69 20 3d 20 70 4f 70 2d 3e 70 32 3b 20 20 20 20 i = pOp->p2;
214ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21500 20 20 20 20 20 20 2f 2a 20 50 32 20 2a 2f 0a 20 /* P2 */.
21501 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
21502 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a SQLITE_INTEGER;.
21503 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 pMem++;..
21504 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
21505 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d EM_Int;. pMem
21506 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 33 3b ->u.i = pOp->p3;
21507 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21508 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 33 20 /* P3
21509 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 */. pMem->typ
2150a 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 e = SQLITE_INTEG
2150b 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a ER;. pMem++;.
2150c 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
2150d 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d VdbeMemGrow(pMem
2150e 2c 20 33 32 2c 20 30 29 20 29 7b 20 20 20 20 20 , 32, 0) ){
2150f 20 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f 0a /* P4 */.
21510 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d assert( p-
21511 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
21512 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 d );. retur
21513 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
21514 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e }. pMem->
21515 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c flags = MEM_Dyn|
21516 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d MEM_Str|MEM_Term
21517 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 70 6c 61 ;. z = displa
21518 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e 7a yP4(pOp, pMem->z
21519 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 28 20 7a , 32);. if( z
2151a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 !=pMem->z ){.
2151b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
2151c 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a 2c mSetStr(pMem, z,
2151d 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 -1, SQLITE_UTF8
2151e 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b , 0);. }else{
2151f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
21520 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 Mem->z!=0 );.
21521 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c pMem->n = sql
21522 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 ite3Strlen30(pMe
21523 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 m->z);. pMe
21524 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f m->enc = SQLITE_
21525 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 UTF8;. }.
21526 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
21527 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 4d ITE_TEXT;. pM
21528 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 em++;.. if( p
21529 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a ->explain==1 ){.
2152a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
2152b 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 3VdbeMemGrow(pMe
2152c 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 20 m, 4, 0) ){.
2152d 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 assert( p->d
2152e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
2152f 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
21530 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
21531 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4d }. pM
21532 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
21533 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f Dyn|MEM_Str|MEM_
21534 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d Term;. pMem
21535 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 20 20 20 73 ->n = 2;. s
21536 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
21537 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e 32 3, pMem->z, "%.2
21538 78 22 2c 20 70 4f 70 2d 3e 70 35 29 3b 20 20 20 x", pOp->p5);
21539 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 20 20 20 70 /* P5 */. p
2153a 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
2153b 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70 TE_TEXT;. p
2153c 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 Mem->enc = SQLIT
2153d 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d E_UTF8;. pM
2153e 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 64 65 66 20 em++;. .#ifdef
2153f 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
21540 20 20 20 69 66 28 20 70 4f 70 2d 3e 7a 43 6f 6d if( pOp->zCom
21541 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 ment ){.
21542 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
21543 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a M_Str|MEM_Term;.
21544 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 pMem->z
21545 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 3b = pOp->zComment;
21546 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e . pMem->n
21547 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
21548 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 30(pMem->z);.
21549 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d pMem->enc =
2154a 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 SQLITE_UTF8;.
2154b 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 pMem->type
2154c 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a = SQLITE_TEXT;.
2154d 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 }else.#end
2154e 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 if. {.
2154f 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
21550 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 20 MEM_Null;
21551 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21552 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 /* Comment */.
21553 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 pMem->typ
21554 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b e = SQLITE_NULL;
21555 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
21556 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d p->nResColum
21557 6e 20 3d 20 38 20 2d 20 34 2a 28 70 2d 3e 65 78 n = 8 - 4*(p->ex
21558 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70 2d plain-1);. p-
21559 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26 70 >pResultSet = &p
2155a 2d 3e 61 4d 65 6d 5b 31 5d 3b 0a 20 20 20 20 70 ->aMem[1];. p
2155b 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b ->rc = SQLITE_OK
2155c 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
2155d 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74 E_ROW;. }. ret
2155e 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 urn rc;.}.#endif
2155f 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
21560 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64 EXPLAIN */..#ifd
21561 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
21562 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 /*.** Print the
21563 53 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65 SQL that was use
21564 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 d to generate a
21565 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f VDBE program..*/
21566 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21567 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
21568 50 72 69 6e 74 53 71 6c 28 56 64 62 65 20 2a 70 PrintSql(Vdbe *p
21569 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 ){. int nOp = p
2156a 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 ->nOp;. VdbeOp
2156b 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c *pOp;. if( nOp<
2156c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 1 ) return;. pO
2156d 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a p = &p->aOp[0];.
2156e 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 if( pOp->opcod
2156f 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 e==OP_Trace && p
21570 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 Op->p4.z!=0 ){.
21571 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
21572 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 = pOp->p4.z;.
21573 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
21574 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b Isspace(*z) ) z+
21575 2b 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53 +;. printf("S
21576 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b QL: [%s]\n", z);
21577 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 . }.}.#endif..#
21578 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
21579 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 TE_OMIT_TRACE) &
2157a 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 & defined(SQLITE
2157b 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 _ENABLE_IOTRACE)
2157c 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20 ./*.** Print an
2157d 49 4f 54 52 41 43 45 20 6d 65 73 73 61 67 65 20 IOTRACE message
2157e 73 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74 showing SQL cont
2157f 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ent..*/.SQLITE_P
21580 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
21581 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 te3VdbeIOTraceSq
21582 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e l(Vdbe *p){. in
21583 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a t nOp = p->nOp;.
21584 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 VdbeOp *pOp;.
21585 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72 if( sqlite3IoTr
21586 61 63 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ace==0 ) return;
21587 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 . if( nOp<1 ) r
21588 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 eturn;. pOp = &
21589 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 p->aOp[0];. if(
2158a 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
2158b 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 _Trace && pOp->p
2158c 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 4.z!=0 ){. in
2158d 74 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72 t i, j;. char
2158e 20 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 20 73 71 z[1000];. sq
2158f 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
21590 69 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73 izeof(z), z, "%s
21591 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 ", pOp->p4.z);.
21592 20 20 20 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69 for(i=0; sqli
21593 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 te3Isspace(z[i])
21594 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72 ; i++){}. for
21595 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 (j=0; z[i]; i++)
21596 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 {. if( sqli
21597 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 te3Isspace(z[i])
21598 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
21599 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20 z[i-1]!=' ' ){.
2159a 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 z[j++]
2159b 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 7d = ' ';. }
2159c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
2159d 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a z[j++] = z
2159e 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 [i];. }.
2159f 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b }. z[j] = 0;
215a0 0a 20 20 20 20 73 71 6c 69 74 65 33 49 6f 54 72 . sqlite3IoTr
215a1 61 63 65 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20 ace("SQL %s\n",
215a2 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 z);. }.}.#endif
215a3 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 /* !SQLITE_OMIT
215a4 5f 54 52 41 43 45 20 26 26 20 53 51 4c 49 54 45 _TRACE && SQLITE
215a5 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 _ENABLE_IOTRACE
215a6 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 */../*.** Alloca
215a7 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 61 20 te space from a
215a8 66 69 78 65 64 20 73 69 7a 65 20 62 75 66 66 65 fixed size buffe
215a9 72 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 r and return a p
215aa 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 61 ointer to.** tha
215ab 74 20 73 70 61 63 65 2e 20 20 49 66 20 69 6e 73 t space. If ins
215ac 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 ufficient space
215ad 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 is available, re
215ae 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a turn NULL..**.**
215af 20 54 68 65 20 70 42 75 66 20 70 61 72 61 6d 65 The pBuf parame
215b0 74 65 72 20 69 73 20 74 68 65 20 69 6e 69 74 69 ter is the initi
215b1 61 6c 20 76 61 6c 75 65 20 6f 66 20 61 20 70 6f al value of a po
215b2 69 6e 74 65 72 20 77 68 69 63 68 20 77 69 6c 6c inter which will
215b3 0a 2a 2a 20 72 65 63 65 69 76 65 20 74 68 65 20 .** receive the
215b4 6e 65 77 20 6d 65 6d 6f 72 79 2e 20 20 70 42 75 new memory. pBu
215b5 66 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 4e 55 f is normally NU
215b6 4c 4c 2e 20 20 49 66 20 70 42 75 66 20 69 73 20 LL. If pBuf is
215b7 6e 6f 74 0a 2a 2a 20 4e 55 4c 4c 2c 20 69 74 20 not.** NULL, it
215b8 6d 65 61 6e 73 20 74 68 61 74 20 6d 65 6d 6f 72 means that memor
215b9 79 20 73 70 61 63 65 20 68 61 73 20 61 6c 72 65 y space has alre
215ba 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 ady been allocat
215bb 65 64 20 61 6e 64 20 74 68 61 74 0a 2a 2a 20 74 ed and that.** t
215bc 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 his routine shou
215bd 6c 64 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 20 ld not allocate
215be 61 6e 79 20 6e 65 77 20 6d 65 6d 6f 72 79 2e 20 any new memory.
215bf 20 57 68 65 6e 20 70 42 75 66 20 69 73 20 6e 6f When pBuf is no
215c0 74 0a 2a 2a 20 4e 55 4c 4c 20 73 69 6d 70 6c 79 t.** NULL simply
215c1 20 72 65 74 75 72 6e 20 70 42 75 66 2e 20 20 4f return pBuf. O
215c2 6e 6c 79 20 61 6c 6c 6f 63 61 74 65 20 6e 65 77 nly allocate new
215c3 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 77 68 memory space wh
215c4 65 6e 20 70 42 75 66 0a 2a 2a 20 69 73 20 4e 55 en pBuf.** is NU
215c5 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 65 20 LL..**.** nByte
215c6 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
215c7 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 bytes of space
215c8 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 needed..**.** *p
215c9 70 46 72 6f 6d 20 70 6f 69 6e 74 73 20 74 6f 20 pFrom points to
215ca 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20 available space
215cb 61 6e 64 20 70 45 6e 64 20 70 6f 69 6e 74 73 20 and pEnd points
215cc 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 to the end of th
215cd 65 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 73 e.** available s
215ce 70 61 63 65 2e 20 20 57 68 65 6e 20 73 70 61 63 pace. When spac
215cf 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 e is allocated,
215d0 2a 70 70 46 72 6f 6d 20 69 73 20 61 64 76 61 6e *ppFrom is advan
215d1 63 65 64 20 70 61 73 74 0a 2a 2a 20 74 68 65 20 ced past.** the
215d2 65 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 end of the alloc
215d3 61 74 65 64 20 73 70 61 63 65 2e 0a 2a 2a 0a 2a ated space..**.*
215d4 2a 20 2a 70 6e 42 79 74 65 20 69 73 20 61 20 63 * *pnByte is a c
215d5 6f 75 6e 74 65 72 20 6f 66 20 74 68 65 20 6e 75 ounter of the nu
215d6 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
215d7 20 73 70 61 63 65 20 74 68 61 74 20 68 61 76 65 space that have
215d8 20 66 61 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 6c failed.** to al
215d9 6c 6f 63 61 74 65 2e 20 20 49 66 20 74 68 65 72 locate. If ther
215da 65 20 69 73 20 69 6e 73 75 66 66 69 63 69 65 6e e is insufficien
215db 74 20 73 70 61 63 65 20 69 6e 20 2a 70 70 46 72 t space in *ppFr
215dc 6f 6d 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 om to satisfy th
215dd 65 0a 2a 2a 20 72 65 71 75 65 73 74 2c 20 74 68 e.** request, th
215de 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e en increment *pn
215df 42 79 74 65 20 62 79 20 74 68 65 20 61 6d 6f 75 Byte by the amou
215e0 6e 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 nt of the reques
215e1 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
215e2 64 20 2a 61 6c 6c 6f 63 53 70 61 63 65 28 0a 20 d *allocSpace(.
215e3 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 void *pBuf,
215e4 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 72 /* Where r
215e5 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 20 77 69 eturn pointer wi
215e6 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 2a 2f 0a ll be stored */.
215e7 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 int nByte,
215e8 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
215e9 20 6f 66 20 62 79 74 65 73 20 74 6f 20 61 6c 6c of bytes to all
215ea 6f 63 61 74 65 20 2a 2f 0a 20 20 75 38 20 2a 2a ocate */. u8 **
215eb 70 70 46 72 6f 6d 2c 20 20 20 20 20 20 20 20 20 ppFrom,
215ec 2f 2a 20 49 4e 2f 4f 55 54 3a 20 41 6c 6c 6f 63 /* IN/OUT: Alloc
215ed 61 74 65 20 66 72 6f 6d 20 2a 70 70 46 72 6f 6d ate from *ppFrom
215ee 20 2a 2f 0a 20 20 75 38 20 2a 70 45 6e 64 2c 20 */. u8 *pEnd,
215ef 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
215f0 69 6e 74 65 72 20 74 6f 20 31 20 62 79 74 65 20 inter to 1 byte
215f1 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 past the end of
215f2 2a 70 70 46 72 6f 6d 20 62 75 66 66 65 72 20 2a *ppFrom buffer *
215f3 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74 65 20 /. int *pnByte
215f4 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 /* If a
215f5 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 llocation cannot
215f6 20 62 65 20 6d 61 64 65 2c 20 69 6e 63 72 65 6d be made, increm
215f7 65 6e 74 20 2a 70 6e 42 79 74 65 20 2a 2f 0a 29 ent *pnByte */.)
215f8 7b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 {. assert( EIGH
215f9 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 T_BYTE_ALIGNMENT
215fa 28 2a 70 70 46 72 6f 6d 29 20 29 3b 0a 20 20 69 (*ppFrom) );. i
215fb 66 28 20 70 42 75 66 20 29 20 72 65 74 75 72 6e f( pBuf ) return
215fc 20 70 42 75 66 3b 0a 20 20 6e 42 79 74 65 20 3d pBuf;. nByte =
215fd 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a ROUND8(nByte);.
215fe 20 20 69 66 28 20 26 28 2a 70 70 46 72 6f 6d 29 if( &(*ppFrom)
215ff 5b 6e 42 79 74 65 5d 20 3c 3d 20 70 45 6e 64 20 [nByte] <= pEnd
21600 29 7b 0a 20 20 20 20 70 42 75 66 20 3d 20 28 76 ){. pBuf = (v
21601 6f 69 64 2a 29 2a 70 70 46 72 6f 6d 3b 0a 20 20 oid*)*ppFrom;.
21602 20 20 2a 70 70 46 72 6f 6d 20 2b 3d 20 6e 42 79 *ppFrom += nBy
21603 74 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 te;. }else{.
21604 20 2a 70 6e 42 79 74 65 20 2b 3d 20 6e 42 79 74 *pnByte += nByt
21605 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 e;. }. return
21606 70 42 75 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 pBuf;.}../*.** R
21607 65 77 69 6e 64 20 74 68 65 20 56 44 42 45 20 62 ewind the VDBE b
21608 61 63 6b 20 74 6f 20 74 68 65 20 62 65 67 69 6e ack to the begin
21609 6e 69 6e 67 20 69 6e 20 70 72 65 70 61 72 61 74 ning in preparat
2160a 69 6f 6e 20 66 6f 72 0a 2a 2a 20 72 75 6e 6e 69 ion for.** runni
2160b 6e 67 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ng it..*/.SQLITE
2160c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
2160d 6c 69 74 65 33 56 64 62 65 52 65 77 69 6e 64 28 lite3VdbeRewind(
2160e 56 64 62 65 20 2a 70 29 7b 0a 23 69 66 20 64 65 Vdbe *p){.#if de
2160f 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
21610 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 UG) || defined(V
21611 44 42 45 5f 50 52 4f 46 49 4c 45 29 0a 20 20 69 DBE_PROFILE). i
21612 6e 74 20 69 3b 0a 23 65 6e 64 69 66 0a 20 20 61 nt i;.#endif. a
21613 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
21614 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 assert( p->magi
21615 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e c==VDBE_MAGIC_IN
21616 49 54 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 IT );.. /* Ther
21617 65 20 73 68 6f 75 6c 64 20 62 65 20 61 74 20 6c e should be at l
21618 65 61 73 74 20 6f 6e 65 20 6f 70 63 6f 64 65 2e east one opcode.
21619 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
2161a 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f p->nOp>0 );.. /
2161b 2a 20 53 65 74 20 74 68 65 20 6d 61 67 69 63 20 * Set the magic
2161c 74 6f 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 to VDBE_MAGIC_RU
2161d 4e 20 73 6f 6f 6e 65 72 20 72 61 74 68 65 72 20 N sooner rather
2161e 74 68 61 6e 20 6c 61 74 65 72 2e 20 2a 2f 0a 20 than later. */.
2161f 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 p->magic = VDBE
21620 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 0a 23 69 66 _MAGIC_RUN;..#if
21621 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
21622 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 70 2d . for(i=1; i<p-
21623 3e 6e 4d 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 >nMem; i++){.
21624 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4d 65 6d assert( p->aMem
21625 5b 69 5d 2e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b [i].db==p->db );
21626 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d . }.#endif. p-
21627 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 70 2d 3e 72 >pc = -1;. p->r
21628 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
21629 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 p->errorAction
2162a 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 70 2d = OE_Abort;. p-
2162b 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 >magic = VDBE_MA
2162c 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43 GIC_RUN;. p->nC
2162d 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e hange = 0;. p->
2162e 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20 cacheCtr = 1;.
2162f 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 p->minWriteFileF
21630 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70 ormat = 255;. p
21631 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 ->iStatement = 0
21632 3b 0a 20 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 ;. p->nFkConstr
21633 61 69 6e 74 20 3d 20 30 3b 0a 23 69 66 64 65 66 aint = 0;.#ifdef
21634 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 VDBE_PROFILE.
21635 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f for(i=0; i<p->nO
21636 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 2d 3e p; i++){. p->
21637 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a aOp[i].cnt = 0;.
21638 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 p->aOp[i].cy
21639 63 6c 65 73 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 cles = 0;. }.#e
2163a 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 ndif.}../*.** Pr
2163b 65 70 61 72 65 20 61 20 76 69 72 74 75 61 6c 20 epare a virtual
2163c 6d 61 63 68 69 6e 65 20 66 6f 72 20 65 78 65 63 machine for exec
2163d 75 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66 69 ution for the fi
2163e 72 73 74 20 74 69 6d 65 20 61 66 74 65 72 0a 2a rst time after.*
2163f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 76 * creating the v
21640 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 irtual machine.
21641 20 54 68 69 73 20 69 6e 76 6f 6c 76 65 73 20 74 This involves t
21642 68 69 6e 67 73 20 73 75 63 68 0a 2a 2a 20 61 73 hings such.** as
21643 20 61 6c 6c 6f 63 61 74 69 6e 67 20 73 74 61 63 allocating stac
21644 6b 20 73 70 61 63 65 20 61 6e 64 20 69 6e 69 74 k space and init
21645 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 70 72 6f ializing the pro
21646 67 72 61 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a gram counter..**
21647 20 41 66 74 65 72 20 74 68 65 20 56 44 42 45 20 After the VDBE
21648 68 61 73 20 62 65 20 70 72 65 70 70 65 64 2c 20 has be prepped,
21649 69 74 20 63 61 6e 20 62 65 20 65 78 65 63 75 74 it can be execut
2164a 65 64 20 62 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 ed by one or mor
2164b 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 e.** calls to sq
2164c 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e lite3VdbeExec().
2164d 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 .**.** This fu
2164e 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 nction may be ca
2164f 6c 6c 65 64 20 65 78 61 63 74 20 6f 6e 63 65 20 lled exact once
21650 6f 6e 20 61 20 65 61 63 68 20 76 69 72 74 75 61 on a each virtua
21651 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 20 41 66 l machine..** Af
21652 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ter this routine
21653 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 56 is called the V
21654 4d 20 68 61 73 20 62 65 65 6e 20 22 70 61 63 6b M has been "pack
21655 61 67 65 64 22 20 61 6e 64 20 69 73 20 72 65 61 aged" and is rea
21656 64 79 0a 2a 2a 20 74 6f 20 72 75 6e 2e 20 20 41 dy.** to run. A
21657 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e fter this routin
21658 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 66 75 74 e is called, fut
21659 68 65 72 20 63 61 6c 6c 73 20 74 6f 20 0a 2a 2a her calls to .**
2165a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
2165b 70 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 p() functions ar
2165c 65 20 70 72 6f 68 69 62 69 74 65 64 2e 20 20 54 e prohibited. T
2165d 68 69 73 20 72 6f 75 74 69 6e 65 20 64 69 73 63 his routine disc
2165e 6f 6e 6e 65 63 74 73 0a 2a 2a 20 74 68 65 20 56 onnects.** the V
2165f 64 62 65 20 66 72 6f 6d 20 74 68 65 20 50 61 72 dbe from the Par
21660 73 65 20 6f 62 6a 65 63 74 20 74 68 61 74 20 68 se object that h
21661 65 6c 70 65 64 20 67 65 6e 65 72 61 74 65 20 69 elped generate i
21662 74 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a t so that the.**
21663 20 74 68 65 20 56 64 62 65 20 62 65 63 6f 6d 65 the Vdbe become
21664 73 20 61 6e 20 69 6e 64 65 70 65 6e 64 65 6e 74 s an independent
21665 20 65 6e 74 69 74 79 20 61 6e 64 20 74 68 65 20 entity and the
21666 50 61 72 73 65 20 6f 62 6a 65 63 74 20 63 61 6e Parse object can
21667 20 62 65 0a 2a 2a 20 64 65 73 74 72 6f 79 65 64 be.** destroyed
21668 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 ..**.** Use the
21669 73 71 6c 69 74 65 33 56 64 62 65 52 65 77 69 6e sqlite3VdbeRewin
2166a 64 28 29 20 70 72 6f 63 65 64 75 72 65 20 74 6f d() procedure to
2166b 20 72 65 73 74 6f 72 65 20 61 20 76 69 72 74 75 restore a virtu
2166c 61 6c 20 6d 61 63 68 69 6e 65 20 62 61 63 6b 0a al machine back.
2166d 2a 2a 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 ** to its initia
2166e 6c 20 73 74 61 74 65 20 61 66 74 65 72 20 69 74 l state after it
2166f 20 68 61 73 20 62 65 65 6e 20 72 75 6e 2e 0a 2a has been run..*
21670 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21671 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
21672 65 4d 61 6b 65 52 65 61 64 79 28 0a 20 20 56 64 eMakeReady(. Vd
21673 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 be *p,
21674 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21675 54 68 65 20 56 44 42 45 20 2a 2f 0a 20 20 50 61 The VDBE */. Pa
21676 72 73 65 20 2a 70 50 61 72 73 65 20 20 20 20 20 rse *pParse
21677 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21678 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 Parsing context
21679 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 */.){. sqlite3
2167a 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 *db;
2167b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
2167c 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
2167d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 3b n */. int nVar;
2167e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2167f 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
21680 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f of parameters */
21681 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 . int nMem;
21682 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21683 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 56 /* Number of V
21684 4d 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 M memory registe
21685 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 rs */. int nCur
21686 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 sor;
21687 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
21688 20 6f 66 20 63 75 72 73 6f 72 73 20 72 65 71 75 of cursors requ
21689 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 ired */. int nA
2168a 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rg;
2168b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
2168c 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
2168d 69 6e 20 73 75 62 70 72 6f 67 72 61 6d 73 20 2a in subprograms *
2168e 2f 0a 20 20 69 6e 74 20 6e 4f 6e 63 65 3b 20 20 /. int nOnce;
2168f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21690 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
21691 4f 50 5f 4f 6e 63 65 20 69 6e 73 74 72 75 63 74 OP_Once instruct
21692 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b ions */. int n;
21693 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21694 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
21695 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 counter */. u8
21696 20 2a 7a 43 73 72 3b 20 20 20 20 20 20 20 20 20 *zCsr;
21697 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21698 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 Memory available
21699 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 for allocation
2169a 2a 2f 0a 20 20 75 38 20 2a 7a 45 6e 64 3b 20 20 */. u8 *zEnd;
2169b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2169c 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 /* First byt
2169d 65 20 70 61 73 74 20 61 6c 6c 6f 63 61 74 65 64 e past allocated
2169e 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 memory */. int
2169f 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 nByte;
216a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
216a1 6f 77 20 6d 75 63 68 20 65 78 74 72 61 20 6d 65 ow much extra me
216a2 6d 6f 72 79 20 69 73 20 6e 65 65 64 65 64 20 2a mory is needed *
216a3 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d /.. assert( p!=
216a4 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
216a5 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 61 73 73 ->nOp>0 );. ass
216a6 65 72 74 28 20 70 50 61 72 73 65 21 3d 30 20 29 ert( pParse!=0 )
216a7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d ;. assert( p->m
216a8 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
216a9 5f 49 4e 49 54 20 29 3b 0a 20 20 64 62 20 3d 20 _INIT );. db =
216aa 70 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 p->db;. assert(
216ab 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
216ac 64 3d 3d 30 20 29 3b 0a 20 20 6e 56 61 72 20 3d d==0 );. nVar =
216ad 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b 0a 20 pParse->nVar;.
216ae 20 6e 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e nMem = pParse->
216af 6e 4d 65 6d 3b 0a 20 20 6e 43 75 72 73 6f 72 20 nMem;. nCursor
216b0 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a = pParse->nTab;.
216b1 20 20 6e 41 72 67 20 3d 20 70 50 61 72 73 65 2d nArg = pParse-
216b2 3e 6e 4d 61 78 41 72 67 3b 0a 20 20 6e 4f 6e 63 >nMaxArg;. nOnc
216b3 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4f 6e 63 e = pParse->nOnc
216b4 65 3b 0a 20 20 69 66 28 20 6e 4f 6e 63 65 3d 3d e;. if( nOnce==
216b5 30 20 29 20 6e 4f 6e 63 65 20 3d 20 31 3b 20 2f 0 ) nOnce = 1; /
216b6 2a 20 45 6e 73 75 72 65 20 61 74 20 6c 65 61 73 * Ensure at leas
216b7 74 20 6f 6e 65 20 62 79 74 65 20 69 6e 20 70 2d t one byte in p-
216b8 3e 61 4f 6e 63 65 46 6c 61 67 5b 5d 20 2a 2f 0a >aOnceFlag[] */.
216b9 20 20 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 . /* For each
216ba 20 63 75 72 73 6f 72 20 72 65 71 75 69 72 65 64 cursor required
216bb 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 20 , also allocate
216bc 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 4d a memory cell. M
216bd 65 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c 73 emory. ** cells
216be 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f 72 (nMem+1-nCursor
216bf 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 69 )..nMem, inclusi
216c0 76 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 ve, will never b
216c1 65 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 e used by. ** t
216c2 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e he vdbe program.
216c3 20 49 6e 73 74 65 61 64 20 74 68 65 79 20 61 72 Instead they ar
216c4 65 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 e used to alloca
216c5 74 65 20 73 70 61 63 65 20 66 6f 72 0a 20 20 2a te space for. *
216c6 2a 20 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43 * VdbeCursor/BtC
216c7 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 73 ursor structures
216c8 2e 20 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 . The blob of me
216c9 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 mory associated
216ca 77 69 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 6f with . ** curso
216cb 72 20 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e r 0 is stored in
216cc 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65 memory cell nMe
216cd 6d 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 m. Memory cell (
216ce 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 6f nMem-1). ** sto
216cf 72 65 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 res the blob of
216d0 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 memory associate
216d1 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c d with cursor 1,
216d2 20 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 etc.. **. **
216d3 53 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 61 See also: alloca
216d4 74 65 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a 2f teCursor().. */
216d5 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 73 . nMem += nCurs
216d6 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 or;.. /* Alloca
216d7 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 6d te space for mem
216d8 6f 72 79 20 72 65 67 69 73 74 65 72 73 2c 20 53 ory registers, S
216d9 51 4c 20 76 61 72 69 61 62 6c 65 73 2c 20 56 44 QL variables, VD
216da 42 45 20 63 75 72 73 6f 72 73 20 61 6e 64 20 0a BE cursors and .
216db 20 20 2a 2a 20 61 6e 20 61 72 72 61 79 20 74 6f ** an array to
216dc 20 6d 61 72 73 68 61 6c 20 53 51 4c 20 66 75 6e marshal SQL fun
216dd 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 ction arguments
216de 69 6e 2e 0a 20 20 2a 2f 0a 20 20 7a 43 73 72 20 in.. */. zCsr
216df 3d 20 28 75 38 2a 29 26 70 2d 3e 61 4f 70 5b 70 = (u8*)&p->aOp[p
216e0 2d 3e 6e 4f 70 5d 3b 20 20 20 20 20 20 20 2f 2a ->nOp]; /*
216e1 20 4d 65 6d 6f 72 79 20 61 76 61 6c 69 61 62 6c Memory avaliabl
216e2 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e e for allocation
216e3 20 2a 2f 0a 20 20 7a 45 6e 64 20 3d 20 28 75 38 */. zEnd = (u8
216e4 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 *)&p->aOp[p->nOp
216e5 41 6c 6c 6f 63 5d 3b 20 20 2f 2a 20 46 69 72 73 Alloc]; /* Firs
216e6 74 20 62 79 74 65 20 70 61 73 74 20 65 6e 64 20 t byte past end
216e7 6f 66 20 7a 43 73 72 5b 5d 20 2a 2f 0a 0a 20 20 of zCsr[] */..
216e8 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 resolveP2Values(
216e9 70 2c 20 26 6e 41 72 67 29 3b 0a 20 20 70 2d 3e p, &nArg);. p->
216ea 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 usesStmtJournal
216eb 3d 20 28 75 38 29 28 70 50 61 72 73 65 2d 3e 69 = (u8)(pParse->i
216ec 73 4d 75 6c 74 69 57 72 69 74 65 20 26 26 20 70 sMultiWrite && p
216ed 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72 74 29 Parse->mayAbort)
216ee 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e ;. if( pParse->
216ef 65 78 70 6c 61 69 6e 20 26 26 20 6e 4d 65 6d 3c explain && nMem<
216f0 31 30 20 29 7b 0a 20 20 20 20 6e 4d 65 6d 20 3d 10 ){. nMem =
216f1 20 31 30 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 10;. }. memse
216f2 74 28 7a 43 73 72 2c 20 30 2c 20 7a 45 6e 64 2d t(zCsr, 0, zEnd-
216f3 7a 43 73 72 29 3b 0a 20 20 7a 43 73 72 20 2b 3d zCsr);. zCsr +=
216f4 20 28 7a 43 73 72 20 2d 20 28 75 38 2a 29 30 29 (zCsr - (u8*)0)
216f5 26 37 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 &7;. assert( EI
216f6 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
216f7 4e 54 28 7a 43 73 72 29 20 29 3b 0a 20 20 70 2d NT(zCsr) );. p-
216f8 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 0a 20 >expired = 0;..
216f9 20 2f 2a 20 4d 65 6d 6f 72 79 20 66 6f 72 20 72 /* Memory for r
216fa 65 67 69 73 74 65 72 73 2c 20 70 61 72 61 6d 65 egisters, parame
216fb 74 65 72 73 2c 20 63 75 72 73 6f 72 2c 20 65 74 ters, cursor, et
216fc 63 2c 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 c, is allocated
216fd 69 6e 20 74 77 6f 0a 20 20 2a 2a 20 70 61 73 73 in two. ** pass
216fe 65 73 2e 20 20 4f 6e 20 74 68 65 20 66 69 72 73 es. On the firs
216ff 74 20 70 61 73 73 2c 20 77 65 20 74 72 79 20 74 t pass, we try t
21700 6f 20 72 65 75 73 65 20 75 6e 75 73 65 64 20 73 o reuse unused s
21701 70 61 63 65 20 61 74 20 74 68 65 20 0a 20 20 2a pace at the . *
21702 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 6f 70 63 * end of the opc
21703 6f 64 65 20 61 72 72 61 79 2e 20 20 49 66 20 77 ode array. If w
21704 65 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 e are unable to
21705 73 61 74 69 73 66 79 20 61 6c 6c 20 6d 65 6d 6f satisfy all memo
21706 72 79 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 6d ry. ** requirem
21707 65 6e 74 73 20 62 79 20 72 65 75 73 69 6e 67 20 ents by reusing
21708 74 68 65 20 6f 70 63 6f 64 65 20 61 72 72 61 79 the opcode array
21709 20 74 61 69 6c 2c 20 74 68 65 6e 20 74 68 65 20 tail, then the
2170a 73 65 63 6f 6e 64 0a 20 20 2a 2a 20 70 61 73 73 second. ** pass
2170b 20 77 69 6c 6c 20 66 69 6c 6c 20 69 6e 20 74 68 will fill in th
2170c 65 20 72 65 73 74 20 75 73 69 6e 67 20 61 20 66 e rest using a f
2170d 72 65 73 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e resh allocation.
2170e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 . **. ** Thi
2170f 73 20 74 77 6f 2d 70 61 73 73 20 61 70 70 72 6f s two-pass appro
21710 61 63 68 20 74 68 61 74 20 72 65 75 73 65 73 20 ach that reuses
21711 61 73 20 6d 75 63 68 20 6d 65 6d 6f 72 79 20 61 as much memory a
21712 73 20 70 6f 73 73 69 62 6c 65 20 66 72 6f 6d 0a s possible from.
21713 20 20 2a 2a 20 74 68 65 20 6c 65 66 74 6f 76 65 ** the leftove
21714 72 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65 r space at the e
21715 6e 64 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 nd of the opcode
21716 20 61 72 72 61 79 20 63 61 6e 20 73 69 67 6e 69 array can signi
21717 66 69 63 61 6e 74 6c 79 0a 20 20 2a 2a 20 72 65 ficantly. ** re
21718 64 75 63 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 duce the amount
21719 6f 66 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 of memory held b
2171a 79 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61 y a prepared sta
2171b 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 64 tement.. */. d
2171c 6f 20 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 o {. nByte =
2171d 30 3b 0a 20 20 20 20 70 2d 3e 61 4d 65 6d 20 3d 0;. p->aMem =
2171e 20 61 6c 6c 6f 63 53 70 61 63 65 28 70 2d 3e 61 allocSpace(p->a
2171f 4d 65 6d 2c 20 6e 4d 65 6d 2a 73 69 7a 65 6f 66 Mem, nMem*sizeof
21720 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 (Mem), &zCsr, zE
21721 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 nd, &nByte);.
21722 20 70 2d 3e 61 56 61 72 20 3d 20 61 6c 6c 6f 63 p->aVar = alloc
21723 53 70 61 63 65 28 70 2d 3e 61 56 61 72 2c 20 6e Space(p->aVar, n
21724 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c Var*sizeof(Mem),
21725 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e &zCsr, zEnd, &n
21726 42 79 74 65 29 3b 0a 20 20 20 20 70 2d 3e 61 70 Byte);. p->ap
21727 41 72 67 20 3d 20 61 6c 6c 6f 63 53 70 61 63 65 Arg = allocSpace
21728 28 70 2d 3e 61 70 41 72 67 2c 20 6e 41 72 67 2a (p->apArg, nArg*
21729 73 69 7a 65 6f 66 28 4d 65 6d 2a 29 2c 20 26 7a sizeof(Mem*), &z
2172a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 Csr, zEnd, &nByt
2172b 65 29 3b 0a 20 20 20 20 70 2d 3e 61 7a 56 61 72 e);. p->azVar
2172c 20 3d 20 61 6c 6c 6f 63 53 70 61 63 65 28 70 2d = allocSpace(p-
2172d 3e 61 7a 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a >azVar, nVar*siz
2172e 65 6f 66 28 63 68 61 72 2a 29 2c 20 26 7a 43 73 eof(char*), &zCs
2172f 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 r, zEnd, &nByte)
21730 3b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 20 3d ;. p->apCsr =
21731 20 61 6c 6c 6f 63 53 70 61 63 65 28 70 2d 3e 61 allocSpace(p->a
21732 70 43 73 72 2c 20 6e 43 75 72 73 6f 72 2a 73 69 pCsr, nCursor*si
21733 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 2a zeof(VdbeCursor*
21734 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ),.
21735 20 20 20 20 20 20 20 20 20 20 20 20 20 26 7a 43 &zC
21736 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 sr, zEnd, &nByte
21737 29 3b 0a 20 20 20 20 70 2d 3e 61 4f 6e 63 65 46 );. p->aOnceF
21738 6c 61 67 20 3d 20 61 6c 6c 6f 63 53 70 61 63 65 lag = allocSpace
21739 28 70 2d 3e 61 4f 6e 63 65 46 6c 61 67 2c 20 6e (p->aOnceFlag, n
2173a 4f 6e 63 65 2c 20 26 7a 43 73 72 2c 20 7a 45 6e Once, &zCsr, zEn
2173b 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 d, &nByte);.
2173c 69 66 28 20 6e 42 79 74 65 20 29 7b 0a 20 20 20 if( nByte ){.
2173d 20 20 20 70 2d 3e 70 46 72 65 65 20 3d 20 73 71 p->pFree = sq
2173e 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
2173f 6f 28 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 o(db, nByte);.
21740 20 20 7d 0a 20 20 20 20 7a 43 73 72 20 3d 20 70 }. zCsr = p
21741 2d 3e 70 46 72 65 65 3b 0a 20 20 20 20 7a 45 6e ->pFree;. zEn
21742 64 20 3d 20 26 7a 43 73 72 5b 6e 42 79 74 65 5d d = &zCsr[nByte]
21743 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e 42 79 74 ;. }while( nByt
21744 65 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 e && !db->malloc
21745 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 70 2d 3e Failed );.. p->
21746 6e 43 75 72 73 6f 72 20 3d 20 28 75 31 36 29 6e nCursor = (u16)n
21747 43 75 72 73 6f 72 3b 0a 20 20 70 2d 3e 6e 4f 6e Cursor;. p->nOn
21748 63 65 46 6c 61 67 20 3d 20 6e 4f 6e 63 65 3b 0a ceFlag = nOnce;.
21749 20 20 69 66 28 20 70 2d 3e 61 56 61 72 20 29 7b if( p->aVar ){
2174a 0a 20 20 20 20 70 2d 3e 6e 56 61 72 20 3d 20 28 . p->nVar = (
2174b 79 6e 56 61 72 29 6e 56 61 72 3b 0a 20 20 20 20 ynVar)nVar;.
2174c 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b for(n=0; n<nVar;
2174d 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e n++){. p->
2174e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 aVar[n].flags =
2174f 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 MEM_Null;.
21750 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 62 20 3d 20 p->aVar[n].db =
21751 64 62 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 db;. }. }.
21752 69 66 28 20 70 2d 3e 61 7a 56 61 72 20 29 7b 0a if( p->azVar ){.
21753 20 20 20 20 70 2d 3e 6e 7a 56 61 72 20 3d 20 70 p->nzVar = p
21754 50 61 72 73 65 2d 3e 6e 7a 56 61 72 3b 0a 20 20 Parse->nzVar;.
21755 20 20 6d 65 6d 63 70 79 28 70 2d 3e 61 7a 56 61 memcpy(p->azVa
21756 72 2c 20 70 50 61 72 73 65 2d 3e 61 7a 56 61 72 r, pParse->azVar
21757 2c 20 70 2d 3e 6e 7a 56 61 72 2a 73 69 7a 65 6f , p->nzVar*sizeo
21758 66 28 70 2d 3e 61 7a 56 61 72 5b 30 5d 29 29 3b f(p->azVar[0]));
21759 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 72 . memset(pPar
2175a 73 65 2d 3e 61 7a 56 61 72 2c 20 30 2c 20 70 50 se->azVar, 0, pP
2175b 61 72 73 65 2d 3e 6e 7a 56 61 72 2a 73 69 7a 65 arse->nzVar*size
2175c 6f 66 28 70 50 61 72 73 65 2d 3e 61 7a 56 61 72 of(pParse->azVar
2175d 5b 30 5d 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 [0]));. }. if(
2175e 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 p->aMem ){.
2175f 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 p->aMem--;
21760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21761 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20 66 /* aMem[] goes f
21762 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a 20 rom 1..nMem */.
21763 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e 4d 65 p->nMem = nMe
21764 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
21765 20 20 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74 20 /* not
21766 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a from 0..nMem-1 *
21767 2f 0a 20 20 20 20 66 6f 72 28 6e 3d 31 3b 20 6e /. for(n=1; n
21768 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 <=nMem; n++){.
21769 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 66 p->aMem[n].f
2176a 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 76 61 6c lags = MEM_Inval
2176b 69 64 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4d 65 id;. p->aMe
2176c 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 m[n].db = db;.
2176d 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 65 78 70 }. }. p->exp
2176e 6c 61 69 6e 20 3d 20 70 50 61 72 73 65 2d 3e 65 lain = pParse->e
2176f 78 70 6c 61 69 6e 3b 0a 20 20 73 71 6c 69 74 65 xplain;. sqlite
21770 33 56 64 62 65 52 65 77 69 6e 64 28 70 29 3b 0a 3VdbeRewind(p);.
21771 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
21772 20 56 44 42 45 20 63 75 72 73 6f 72 20 61 6e 64 VDBE cursor and
21773 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 release all the
21774 20 72 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 resources that
21775 63 75 72 73 6f 72 20 0a 2a 2a 20 68 61 70 70 65 cursor .** happe
21776 6e 73 20 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 ns to hold..*/.S
21777 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
21778 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 id sqlite3VdbeFr
21779 65 65 43 75 72 73 6f 72 28 56 64 62 65 20 2a 70 eeCursor(Vdbe *p
2177a 2c 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 , VdbeCursor *pC
2177b 78 29 7b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 x){. if( pCx==0
2177c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ){. return;.
2177d 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
2177e 65 53 6f 72 74 65 72 43 6c 6f 73 65 28 70 2d 3e eSorterClose(p->
2177f 64 62 2c 20 70 43 78 29 3b 0a 20 20 69 66 28 20 db, pCx);. if(
21780 70 43 78 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 pCx->pBt ){.
21781 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 sqlite3BtreeClos
21782 65 28 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 20 e(pCx->pBt);.
21783 20 2f 2a 20 54 68 65 20 70 43 78 2d 3e 70 43 75 /* The pCx->pCu
21784 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 63 6c 6f rsor will be clo
21785 73 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 se automatically
21786 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 2c 20 , if it exists,
21787 62 79 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 by. ** the ca
21788 6c 6c 20 61 62 6f 76 65 2e 20 2a 2f 0a 20 20 7d ll above. */. }
21789 65 6c 73 65 20 69 66 28 20 70 43 78 2d 3e 70 43 else if( pCx->pC
2178a 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c ursor ){. sql
2178b 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 ite3BtreeCloseCu
2178c 72 73 6f 72 28 70 43 78 2d 3e 70 43 75 72 73 6f rsor(pCx->pCurso
2178d 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 r);. }.#ifndef
2178e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
2178f 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70 UALTABLE. if( p
21790 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 Cx->pVtabCursor
21791 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 ){. sqlite3_v
21792 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 tab_cursor *pVta
21793 62 43 75 72 73 6f 72 20 3d 20 70 43 78 2d 3e 70 bCursor = pCx->p
21794 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 VtabCursor;.
21795 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f const sqlite3_mo
21796 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 dule *pModule =
21797 70 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 pCx->pModule;.
21798 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f p->inVtabMetho
21799 64 20 3d 20 31 3b 0a 20 20 20 20 70 4d 6f 64 75 d = 1;. pModu
2179a 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 56 74 61 62 le->xClose(pVtab
2179b 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 70 2d 3e Cursor);. p->
2179c 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 inVtabMethod = 0
2179d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a ;. }.#endif.}..
2179e 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 76 /*.** Copy the v
2179f 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 alues stored in
217a0 74 68 65 20 56 64 62 65 46 72 61 6d 65 20 73 74 the VdbeFrame st
217a1 72 75 63 74 75 72 65 20 74 6f 20 69 74 73 20 56 ructure to its V
217a2 64 62 65 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 dbe. This.** is
217a3 75 73 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c used, for exampl
217a4 65 2c 20 77 68 65 6e 20 61 20 74 72 69 67 67 65 e, when a trigge
217a5 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 r sub-program is
217a6 20 68 61 6c 74 65 64 20 74 6f 20 72 65 73 74 6f halted to resto
217a7 72 65 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 20 74 6f re.** control to
217a8 20 74 68 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 the main progra
217a9 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 m..*/.SQLITE_PRI
217aa 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
217ab 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 VdbeFrameRestore
217ac 28 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 (VdbeFrame *pFra
217ad 6d 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d me){. Vdbe *v =
217ae 20 70 46 72 61 6d 65 2d 3e 76 3b 0a 20 20 76 2d pFrame->v;. v-
217af 3e 61 4f 6e 63 65 46 6c 61 67 20 3d 20 70 46 72 >aOnceFlag = pFr
217b0 61 6d 65 2d 3e 61 4f 6e 63 65 46 6c 61 67 3b 0a ame->aOnceFlag;.
217b1 20 20 76 2d 3e 6e 4f 6e 63 65 46 6c 61 67 20 3d v->nOnceFlag =
217b2 20 70 46 72 61 6d 65 2d 3e 6e 4f 6e 63 65 46 6c pFrame->nOnceFl
217b3 61 67 3b 0a 20 20 76 2d 3e 61 4f 70 20 3d 20 70 ag;. v->aOp = p
217b4 46 72 61 6d 65 2d 3e 61 4f 70 3b 0a 20 20 76 2d Frame->aOp;. v-
217b5 3e 6e 4f 70 20 3d 20 70 46 72 61 6d 65 2d 3e 6e >nOp = pFrame->n
217b6 4f 70 3b 0a 20 20 76 2d 3e 61 4d 65 6d 20 3d 20 Op;. v->aMem =
217b7 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 3b 0a 20 20 pFrame->aMem;.
217b8 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46 72 61 6d 65 v->nMem = pFrame
217b9 2d 3e 6e 4d 65 6d 3b 0a 20 20 76 2d 3e 61 70 43 ->nMem;. v->apC
217ba 73 72 20 3d 20 70 46 72 61 6d 65 2d 3e 61 70 43 sr = pFrame->apC
217bb 73 72 3b 0a 20 20 76 2d 3e 6e 43 75 72 73 6f 72 sr;. v->nCursor
217bc 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 43 75 72 73 = pFrame->nCurs
217bd 6f 72 3b 0a 20 20 76 2d 3e 64 62 2d 3e 6c 61 73 or;. v->db->las
217be 74 52 6f 77 69 64 20 3d 20 70 46 72 61 6d 65 2d tRowid = pFrame-
217bf 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 76 2d >lastRowid;. v-
217c0 3e 6e 43 68 61 6e 67 65 20 3d 20 70 46 72 61 6d >nChange = pFram
217c1 65 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 72 65 e->nChange;. re
217c2 74 75 72 6e 20 70 46 72 61 6d 65 2d 3e 70 63 3b turn pFrame->pc;
217c3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
217c4 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2a 0a all cursors..**.
217c5 2a 2a 20 41 6c 73 6f 20 72 65 6c 65 61 73 65 20 ** Also release
217c6 61 6e 79 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f any dynamic memo
217c7 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 56 ry held by the V
217c8 4d 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61 4d M in the Vdbe.aM
217c9 65 6d 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 em memory .** ce
217ca 6c 6c 20 61 72 72 61 79 2e 20 54 68 69 73 20 69 ll array. This i
217cb 73 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 74 s necessary as t
217cc 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 he memory cell a
217cd 72 72 61 79 20 6d 61 79 20 63 6f 6e 74 61 69 6e rray may contain
217ce 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 .** pointers to
217cf 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 63 74 VdbeFrame object
217d0 73 2c 20 77 68 69 63 68 20 6d 61 79 20 69 6e 20 s, which may in
217d1 74 75 72 6e 20 63 6f 6e 74 61 69 6e 20 70 6f 69 turn contain poi
217d2 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 6f 70 65 6e nters to.** open
217d3 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 73 74 61 cursors..*/.sta
217d4 74 69 63 20 76 6f 69 64 20 63 6c 6f 73 65 41 6c tic void closeAl
217d5 6c 43 75 72 73 6f 72 73 28 56 64 62 65 20 2a 70 lCursors(Vdbe *p
217d6 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 46 72 61 ){. if( p->pFra
217d7 6d 65 20 29 7b 0a 20 20 20 20 56 64 62 65 46 72 me ){. VdbeFr
217d8 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 20 ame *pFrame;.
217d9 20 66 6f 72 28 70 46 72 61 6d 65 3d 70 2d 3e 70 for(pFrame=p->p
217da 46 72 61 6d 65 3b 20 70 46 72 61 6d 65 2d 3e 70 Frame; pFrame->p
217db 50 61 72 65 6e 74 3b 20 70 46 72 61 6d 65 3d 70 Parent; pFrame=p
217dc 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b Frame->pParent);
217dd 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
217de 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72 FrameRestore(pFr
217df 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 ame);. }. p->p
217e0 46 72 61 6d 65 20 3d 20 30 3b 0a 20 20 70 2d 3e Frame = 0;. p->
217e1 6e 46 72 61 6d 65 20 3d 20 30 3b 0a 0a 20 20 69 nFrame = 0;.. i
217e2 66 28 20 70 2d 3e 61 70 43 73 72 20 29 7b 0a 20 f( p->apCsr ){.
217e3 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
217e4 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 r(i=0; i<p->nCur
217e5 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 sor; i++){.
217e6 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20 VdbeCursor *pC
217e7 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 = p->apCsr[i];.
217e8 20 20 20 20 20 69 66 28 20 70 43 20 29 7b 0a 20 if( pC ){.
217e9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
217ea 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 beFreeCursor(p,
217eb 70 43 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e pC);. p->
217ec 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 apCsr[i] = 0;.
217ed 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
217ee 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b if( p->aMem ){
217ef 0a 20 20 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 . releaseMemA
217f0 72 72 61 79 28 26 70 2d 3e 61 4d 65 6d 5b 31 5d rray(&p->aMem[1]
217f1 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 7d 0a , p->nMem);. }.
217f2 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 44 65 6c while( p->pDel
217f3 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62 Frame ){. Vdb
217f4 65 46 72 61 6d 65 20 2a 70 44 65 6c 20 3d 20 70 eFrame *pDel = p
217f5 2d 3e 70 44 65 6c 46 72 61 6d 65 3b 0a 20 20 20 ->pDelFrame;.
217f6 20 70 2d 3e 70 44 65 6c 46 72 61 6d 65 20 3d 20 p->pDelFrame =
217f7 70 44 65 6c 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 pDel->pParent;.
217f8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 sqlite3VdbeFr
217f9 61 6d 65 44 65 6c 65 74 65 28 70 44 65 6c 29 3b ameDelete(pDel);
217fa 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c . }.}../*.** Cl
217fb 65 61 6e 20 75 70 20 74 68 65 20 56 4d 20 61 66 ean up the VM af
217fc 74 65 72 20 65 78 65 63 75 74 69 6f 6e 2e 0a 2a ter execution..*
217fd 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
217fe 65 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 e will automatic
217ff 61 6c 6c 79 20 63 6c 6f 73 65 20 61 6e 79 20 63 ally close any c
21800 75 72 73 6f 72 73 2c 20 6c 69 73 74 73 2c 20 61 ursors, lists, a
21801 6e 64 2f 6f 72 0a 2a 2a 20 73 6f 72 74 65 72 73 nd/or.** sorters
21802 20 74 68 61 74 20 77 65 72 65 20 6c 65 66 74 20 that were left
21803 6f 70 65 6e 2e 20 20 49 74 20 61 6c 73 6f 20 64 open. It also d
21804 65 6c 65 74 65 73 20 74 68 65 20 76 61 6c 75 65 eletes the value
21805 73 20 6f 66 0a 2a 2a 20 76 61 72 69 61 62 6c 65 s of.** variable
21806 73 20 69 6e 20 74 68 65 20 61 56 61 72 5b 5d 20 s in the aVar[]
21807 61 72 72 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 array..*/.static
21808 20 76 6f 69 64 20 43 6c 65 61 6e 75 70 28 56 64 void Cleanup(Vd
21809 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 be *p){. sqlite
2180a 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 3 *db = p->db;..
2180b 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
2180c 42 55 47 0a 20 20 2f 2a 20 45 78 65 63 75 74 65 BUG. /* Execute
2180d 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
2180e 65 6e 74 73 20 74 6f 20 65 6e 73 75 72 65 20 74 ents to ensure t
2180f 68 61 74 20 74 68 65 20 56 64 62 65 2e 61 70 43 hat the Vdbe.apC
21810 73 72 5b 5d 20 61 6e 64 20 0a 20 20 2a 2a 20 56 sr[] and . ** V
21811 64 62 65 2e 61 4d 65 6d 5b 5d 20 61 72 72 61 79 dbe.aMem[] array
21812 73 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 s have already b
21813 65 65 6e 20 63 6c 65 61 6e 65 64 20 75 70 2e 20 een cleaned up.
21814 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 */. int i;. i
21815 66 28 20 70 2d 3e 61 70 43 73 72 20 29 20 66 6f f( p->apCsr ) fo
21816 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 r(i=0; i<p->nCur
21817 73 6f 72 3b 20 69 2b 2b 29 20 61 73 73 65 72 74 sor; i++) assert
21818 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3d 3d 30 ( p->apCsr[i]==0
21819 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65 );. if( p->aMe
2181a 6d 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 31 m ){. for(i=1
2181b 3b 20 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 2b ; i<=p->nMem; i+
2181c 2b 29 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4d +) assert( p->aM
2181d 65 6d 5b 69 5d 2e 66 6c 61 67 73 3d 3d 4d 45 4d em[i].flags==MEM
2181e 5f 49 6e 76 61 6c 69 64 20 29 3b 0a 20 20 7d 0a _Invalid );. }.
2181f 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 #endif.. sqlite
21820 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
21821 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 ErrMsg);. p->zE
21822 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e rrMsg = 0;. p->
21823 70 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a pResultSet = 0;.
21824 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 }../*.** Set the
21825 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c number of resul
21826 74 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 t columns that w
21827 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 ill be returned
21828 62 79 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 by this SQL.** s
21829 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 tatement. This i
2182a 73 20 6e 6f 77 20 73 65 74 20 61 74 20 63 6f 6d s now set at com
2182b 70 69 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 65 pile time, rathe
2182c 72 20 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a r than during.**
2182d 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 execution of th
2182e 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 73 e vdbe program s
2182f 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 o that sqlite3_c
21830 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 olumn_count() ca
21831 6e 0a 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f n.** be called o
21832 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 n an SQL stateme
21833 6e 74 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 nt before sqlite
21834 33 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 3_step()..*/.SQL
21835 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
21836 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e sqlite3VdbeSetN
21837 75 6d 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 umCols(Vdbe *p,
21838 69 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b int nResColumn){
21839 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 . Mem *pColName
2183a 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c ;. int n;. sql
2183b 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 ite3 *db = p->db
2183c 3b 0a 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 ;.. releaseMemA
2183d 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 rray(p->aColName
2183e 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a , p->nResColumn*
2183f 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 COLNAME_N);. sq
21840 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
21841 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 p->aColName);.
21842 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 n = nResColumn*C
21843 4f 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e OLNAME_N;. p->n
21844 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 28 75 31 36 ResColumn = (u16
21845 29 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 )nResColumn;. p
21846 2d 3e 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f ->aColName = pCo
21847 6c 4e 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 lName = (Mem*)sq
21848 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
21849 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d o(db, sizeof(Mem
2184a 29 2a 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e )*n );. if( p->
2184b 61 43 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 aColName==0 ) re
2184c 74 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e turn;. while( n
2184d 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 -- > 0 ){. pC
2184e 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 olName->flags =
2184f 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 MEM_Null;. pC
21850 6f 6c 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e olName->db = p->
21851 64 62 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 db;. pColName
21852 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a ++;. }.}../*.**
21853 20 53 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 Set the name of
21854 20 74 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75 the idx'th colu
21855 6d 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 mn to be returne
21856 64 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 d by the SQL sta
21857 74 65 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 tement..** zName
21858 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 must be a point
21859 65 72 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d er to a nul term
2185a 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a inated string..*
2185b 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d *.** This call m
2185c 75 73 74 20 62 65 20 6d 61 64 65 20 61 66 74 65 ust be made afte
2185d 72 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 r a call to sqli
2185e 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c te3VdbeSetNumCol
2185f 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 s()..**.** The f
21860 69 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 inal parameter,
21861 78 44 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e xDel, must be on
21862 65 20 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 e of SQLITE_DYNA
21863 4d 49 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 MIC, SQLITE_STAT
21864 49 43 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f IC.** or SQLITE_
21865 54 52 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 TRANSIENT. If it
21866 20 69 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d is SQLITE_DYNAM
21867 49 43 2c 20 74 68 65 6e 20 74 68 65 20 62 75 66 IC, then the buf
21868 66 65 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 fer pointed.** t
21869 6f 20 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 o by zName will
2186a 62 65 20 66 72 65 65 64 20 62 79 20 73 71 6c 69 be freed by sqli
2186b 74 65 33 44 62 46 72 65 65 28 29 20 77 68 65 6e te3DbFree() when
2186c 20 74 68 65 20 76 64 62 65 20 69 73 20 64 65 73 the vdbe is des
2186d 74 72 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 troyed..*/.SQLIT
2186e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
2186f 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e lite3VdbeSetColN
21870 61 6d 65 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 ame(. Vdbe *p,
21871 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21872 20 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 /* Vdbe
21873 62 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 being configured
21874 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 */. int idx,
21875 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21876 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
21877 6f 66 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 of column zName
21878 61 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 applies to */.
21879 69 6e 74 20 76 61 72 2c 20 20 20 20 20 20 20 20 int var,
2187a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2187b 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 /* One of the C
2187c 4f 4c 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e OLNAME_* constan
2187d 74 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ts */. const ch
2187e 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 ar *zName,
2187f 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
21880 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f ter to buffer co
21881 6e 74 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f ntaining name */
21882 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
21883 76 6f 69 64 2a 29 20 20 20 20 20 20 20 20 20 20 void*)
21884 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 /* Memory ma
21885 6e 61 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 nagement strateg
21886 79 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 y for zName */.)
21887 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 {. int rc;. Me
21888 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 m *pColName;. a
21889 73 73 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 ssert( idx<p->nR
2188a 65 73 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 esColumn );. as
2188b 73 65 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d sert( var<COLNAM
2188c 45 5f 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e E_N );. if( p->
2188d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
2188e 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
2188f 21 7a 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d !zName || xDel!=
21890 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 SQLITE_DYNAMIC )
21891 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
21892 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 ITE_NOMEM;. }.
21893 20 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c assert( p->aCol
21894 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f Name!=0 );. pCo
21895 6c 4e 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f lName = &(p->aCo
21896 6c 4e 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d lName[idx+var*p-
21897 3e 6e 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 >nResColumn]);.
21898 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
21899 65 4d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e eMemSetStr(pColN
2189a 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 ame, zName, -1,
2189b 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 SQLITE_UTF8, xDe
2189c 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 l);. assert( rc
2189d 21 3d 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c !=0 || !zName ||
2189e 20 28 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 (pColName->flag
2189f 73 26 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 s&MEM_Term)!=0 )
218a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
218a1 0a 0a 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f ../*.** A read o
218a2 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 r write transact
218a3 69 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e ion may or may n
218a4 6f 74 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20 ot be active on
218a5 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a database handle.
218a6 2a 2a 20 64 62 2e 20 49 66 20 61 20 74 72 61 6e ** db. If a tran
218a7 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 saction is activ
218a8 65 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 e, commit it. If
218a9 20 74 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 there is a.** w
218aa 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
218ab 20 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 spanning more t
218ac 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 han one database
218ad 20 66 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 file, this rout
218ae 69 6e 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 ine.** takes car
218af 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 e of the master
218b0 6a 6f 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 journal trickery
218b1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
218b2 76 64 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 vdbeCommit(sqlit
218b3 65 33 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 e3 *db, Vdbe *p)
218b4 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 {. int i;. int
218b5 20 6e 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a nTrans = 0; /*
218b6 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 Number of datab
218b7 61 73 65 73 20 77 69 74 68 20 61 6e 20 61 63 74 ases with an act
218b8 69 76 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 ive write-transa
218b9 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 ction */. int r
218ba 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
218bb 20 69 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 int needXcommit
218bc 20 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 = 0;..#ifdef SQ
218bd 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
218be 4c 54 41 42 4c 45 0a 20 20 2f 2a 20 57 69 74 68 LTABLE. /* With
218bf 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 73 71 this option, sq
218c0 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 29 20 lite3VtabSync()
218c1 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 is defined to be
218c2 20 73 69 6d 70 6c 79 20 0a 20 20 2a 2a 20 53 51 simply . ** SQ
218c3 4c 49 54 45 5f 4f 4b 20 73 6f 20 70 20 69 73 20 LITE_OK so p is
218c4 6e 6f 74 20 75 73 65 64 2e 20 0a 20 20 2a 2f 0a not used. . */.
218c5 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
218c6 45 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 ER(p);.#endif..
218c7 20 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 /* Before doing
218c8 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 anything else,
218c9 63 61 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 29 call the xSync()
218ca 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e callback for an
218cb 79 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d y. ** virtual m
218cc 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 77 72 69 odule tables wri
218cd 74 74 65 6e 20 69 6e 20 74 68 69 73 20 74 72 61 tten in this tra
218ce 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 nsaction. This h
218cf 61 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f as to. ** be do
218d0 6e 65 20 62 65 66 6f 72 65 20 64 65 74 65 72 6d ne before determ
218d1 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 61 20 ining whether a
218d2 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
218d3 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 ile is . ** req
218d4 75 69 72 65 64 2c 20 61 73 20 61 6e 20 78 53 79 uired, as an xSy
218d5 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 nc() callback ma
218d6 79 20 61 64 64 20 61 6e 20 61 74 74 61 63 68 65 y add an attache
218d7 64 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 d database. **
218d8 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 to the transacti
218d9 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 on.. */. rc =
218da 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 sqlite3VtabSync(
218db 64 62 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 db, &p->zErrMsg)
218dc 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f ;.. /* This loo
218dd 70 20 64 65 74 65 72 6d 69 6e 65 73 20 28 61 29 p determines (a)
218de 20 69 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 if the commit h
218df 6f 6f 6b 20 73 68 6f 75 6c 64 20 62 65 20 69 6e ook should be in
218e0 76 6f 6b 65 64 20 61 6e 64 0a 20 20 2a 2a 20 28 voked and. ** (
218e1 62 29 20 68 6f 77 20 6d 61 6e 79 20 64 61 74 61 b) how many data
218e2 62 61 73 65 20 66 69 6c 65 73 20 68 61 76 65 20 base files have
218e3 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 open write trans
218e4 61 63 74 69 6f 6e 73 2c 20 6e 6f 74 20 0a 20 20 actions, not .
218e5 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 ** including the
218e6 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e 20 temp database.
218e7 28 62 29 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 (b) is important
218e8 20 62 65 63 61 75 73 65 20 69 66 20 6d 6f 72 65 because if more
218e9 20 74 68 61 6e 20 0a 20 20 2a 2a 20 6f 6e 65 20 than . ** one
218ea 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 database file ha
218eb 73 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 s an open write
218ec 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 6d transaction, a m
218ed 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 20 20 aster journal.
218ee 2a 2a 20 66 69 6c 65 20 69 73 20 72 65 71 75 69 ** file is requi
218ef 72 65 64 20 66 6f 72 20 61 6e 20 61 74 6f 6d 69 red for an atomi
218f0 63 20 63 6f 6d 6d 69 74 2e 0a 20 20 2a 2f 20 0a c commit.. */ .
218f1 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 for(i=0; rc==S
218f2 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 QLITE_OK && i<db
218f3 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 ->nDb; i++){ .
218f4 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 Btree *pBt = d
218f5 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
218f6 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 if( sqlite3Bt
218f7 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 reeIsInTrans(pBt
218f8 29 20 29 7b 0a 20 20 20 20 20 20 6e 65 65 64 58 ) ){. needX
218f9 63 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 commit = 1;.
218fa 20 20 69 66 28 20 69 21 3d 31 20 29 20 6e 54 72 if( i!=1 ) nTr
218fb 61 6e 73 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c ans++;. sql
218fc 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
218fd 42 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 Bt);. rc =
218fe 73 71 6c 69 74 65 33 50 61 67 65 72 45 78 63 6c sqlite3PagerExcl
218ff 75 73 69 76 65 4c 6f 63 6b 28 73 71 6c 69 74 65 usiveLock(sqlite
21900 33 42 74 72 65 65 50 61 67 65 72 28 70 42 74 29 3BtreePager(pBt)
21901 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21902 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 29 3b BtreeLeave(pBt);
21903 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
21904 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
21905 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
21906 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
21907 65 72 65 20 61 72 65 20 61 6e 79 20 77 72 69 74 ere are any writ
21908 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 e-transactions a
21909 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20 74 68 t all, invoke th
2190a 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 2a 2f e commit hook */
2190b 0a 20 20 69 66 28 20 6e 65 65 64 58 63 6f 6d 6d . if( needXcomm
2190c 69 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d 6d 69 it && db->xCommi
2190d 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 tCallback ){.
2190e 20 72 63 20 3d 20 64 62 2d 3e 78 43 6f 6d 6d 69 rc = db->xCommi
2190f 74 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 43 tCallback(db->pC
21910 6f 6d 6d 69 74 41 72 67 29 3b 0a 20 20 20 20 69 ommitArg);. i
21911 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 f( rc ){. r
21912 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e eturn SQLITE_CON
21913 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 STRAINT;. }.
21914 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 6d }.. /* The sim
21915 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f 20 6d 6f ple case - no mo
21916 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 re than one data
21917 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 63 base file (not c
21918 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 20 2a 2a ounting the. **
21919 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 29 20 TEMP database)
2191a 68 61 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f has a transactio
2191b 6e 20 61 63 74 69 76 65 2e 20 20 20 54 68 65 72 n active. Ther
2191c 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 e is no need for
2191d 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 74 65 72 the. ** master
2191e 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 -journal.. **.
2191f 20 2a 2a 20 49 66 20 74 68 65 20 72 65 74 75 72 ** If the retur
21920 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 n value of sqlit
21921 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 e3BtreeGetFilena
21922 6d 65 28 29 20 69 73 20 61 20 7a 65 72 6f 20 6c me() is a zero l
21923 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 72 69 6e ength. ** strin
21924 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20 g, it means the
21925 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 73 main database is
21926 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20 61 20 74 :memory: or a t
21927 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e 20 0a 20 emp file. In .
21928 20 2a 2a 20 74 68 61 74 20 63 61 73 65 20 77 65 ** that case we
21929 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 do not support
2192a 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c atomic multi-fil
2192b 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73 e commits, so us
2192c 65 20 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 e the . ** simp
2192d 6c 65 20 63 61 73 65 20 74 68 65 6e 20 74 6f 6f le case then too
2192e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d .. */. if( 0==
2192f 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
21930 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 sqlite3BtreeGetF
21931 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b ilename(db->aDb[
21932 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c 7c 20 6e 0].pBt)). || n
21933 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b 0a 20 20 Trans<=1. ){.
21934 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 for(i=0; rc==S
21935 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 QLITE_OK && i<db
21936 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
21937 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 Btree *pBt =
21938 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
21939 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b if( pBt ){
2193a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
2193b 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
2193c 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 30 29 PhaseOne(pBt, 0)
2193d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
2193e 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 . /* Do the c
2193f 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c ommit only if al
21940 6c 20 64 61 74 61 62 61 73 65 73 20 73 75 63 63 l databases succ
21941 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 essfully complet
21942 65 20 70 68 61 73 65 20 31 2e 20 0a 20 20 20 20 e phase 1. .
21943 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 ** If one of the
21944 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 BtreeCommitPhas
21945 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 66 61 69 eOne() calls fai
21946 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 ls, this indicat
21947 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 es an. ** IO
21948 65 72 72 6f 72 20 77 68 69 6c 65 20 64 65 6c 65 error while dele
21949 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 ting or truncati
2194a 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ng a journal fil
2194b 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c e. It is unlikel
2194c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 20 63 6f y,. ** but co
2194d 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 6e 20 74 uld happen. In t
2194e 68 69 73 20 63 61 73 65 20 61 62 61 6e 64 6f 6e his case abandon
2194f 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 processing and
21950 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
21951 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 .. */. for
21952 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 (i=0; rc==SQLITE
21953 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 _OK && i<db->nDb
21954 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 ; i++){. Bt
21955 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 ree *pBt = db->a
21956 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 Db[i].pBt;.
21957 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 if( pBt ){.
21958 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
21959 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
2195a 54 77 6f 28 70 42 74 2c 20 30 29 3b 0a 20 20 20 Two(pBt, 0);.
2195b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
2195c 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
2195d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
2195e 33 56 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 3VtabCommit(db);
2195f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
21960 20 54 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 The complex cas
21961 65 20 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d e - There is a m
21962 75 6c 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d ulti-file write-
21963 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 transaction acti
21964 76 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 ve.. ** This re
21965 71 75 69 72 65 73 20 61 20 6d 61 73 74 65 72 20 quires a master
21966 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 journal file to
21967 65 6e 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 ensure the trans
21968 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 action is. ** c
21969 6f 6d 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c ommitted atomicl
2196a 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 y.. */.#ifndef
2196b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b SQLITE_OMIT_DISK
2196c 49 4f 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73 IO. else{. s
2196d 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
2196e 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20 = db->pVfs;.
2196f 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 int needSync =
21970 30 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 0;. char *zMa
21971 73 74 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 ster = 0; /* F
21972 69 6c 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 ile-name for the
21973 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
21974 2a 2f 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 */. char cons
21975 74 20 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 t *zMainFile = s
21976 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 qlite3BtreeGetFi
21977 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 lename(db->aDb[0
21978 5d 2e 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 ].pBt);. sqli
21979 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 te3_file *pMaste
2197a 72 20 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f r = 0;. i64 o
2197b 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 69 ffset = 0;. i
2197c 6e 74 20 72 65 73 3b 0a 20 20 20 20 69 6e 74 20 nt res;. int
2197d 72 65 74 72 79 43 6f 75 6e 74 20 3d 20 30 3b 0a retryCount = 0;.
2197e 20 20 20 20 69 6e 74 20 6e 4d 61 69 6e 46 69 6c int nMainFil
2197f 65 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 e;.. /* Selec
21980 74 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e t a master journ
21981 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a al file name */.
21982 20 20 20 20 6e 4d 61 69 6e 46 69 6c 65 20 3d 20 nMainFile =
21983 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
21984 7a 4d 61 69 6e 46 69 6c 65 29 3b 0a 20 20 20 20 zMainFile);.
21985 7a 4d 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65 zMaster = sqlite
21986 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 3MPrintf(db, "%s
21987 2d 6d 6a 58 58 58 58 58 58 39 58 58 7a 22 2c 20 -mjXXXXXX9XXz",
21988 7a 4d 61 69 6e 46 69 6c 65 29 3b 0a 20 20 20 20 zMainFile);.
21989 69 66 28 20 7a 4d 61 73 74 65 72 3d 3d 30 20 29 if( zMaster==0 )
2198a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
2198b 4f 4d 45 4d 3b 0a 20 20 20 20 64 6f 20 7b 0a 20 OMEM;. do {.
2198c 20 20 20 20 20 75 33 32 20 69 52 61 6e 64 6f 6d u32 iRandom
2198d 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65 74 72 ;. if( retr
2198e 79 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 yCount ){.
2198f 20 20 69 66 28 20 72 65 74 72 79 43 6f 75 6e 74 if( retryCount
21990 3e 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 >100 ){.
21991 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 sqlite3_log(SQ
21992 4c 49 54 45 5f 46 55 4c 4c 2c 20 22 4d 4a 20 64 LITE_FULL, "MJ d
21993 65 6c 65 74 65 3a 20 25 73 22 2c 20 7a 4d 61 73 elete: %s", zMas
21994 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ter);.
21995 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
21996 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 pVfs, zMaster, 0
21997 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 );. bre
21998 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 ak;. }els
21999 65 20 69 66 28 20 72 65 74 72 79 43 6f 75 6e 74 e if( retryCount
2199a 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==1 ){.
2199b 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c sqlite3_log(SQL
2199c 49 54 45 5f 46 55 4c 4c 2c 20 22 4d 4a 20 63 6f ITE_FULL, "MJ co
2199d 6c 6c 69 64 65 3a 20 25 73 22 2c 20 7a 4d 61 73 llide: %s", zMas
2199e 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a ter);. }.
2199f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
219a0 74 72 79 43 6f 75 6e 74 2b 2b 3b 0a 20 20 20 20 tryCount++;.
219a1 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d sqlite3_random
219a2 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 52 61 6e ness(sizeof(iRan
219a3 64 6f 6d 29 2c 20 26 69 52 61 6e 64 6f 6d 29 3b dom), &iRandom);
219a4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
219a5 6e 70 72 69 6e 74 66 28 31 33 2c 20 26 7a 4d 61 nprintf(13, &zMa
219a6 73 74 65 72 5b 6e 4d 61 69 6e 46 69 6c 65 5d 2c ster[nMainFile],
219a7 20 22 2d 6d 6a 25 30 36 58 39 25 30 32 58 22 2c "-mj%06X9%02X",
219a8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
219a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
219aa 28 69 52 61 6e 64 6f 6d 3e 3e 38 29 26 30 78 66 (iRandom>>8)&0xf
219ab 66 66 66 66 66 2c 20 69 52 61 6e 64 6f 6d 26 30 fffff, iRandom&0
219ac 78 66 66 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 xff);. /* T
219ad 68 65 20 61 6e 74 69 70 65 6e 75 6c 74 69 6d 61 he antipenultima
219ae 74 65 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 te character of
219af 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
219b0 61 6c 20 6e 61 6d 65 20 6d 75 73 74 0a 20 20 20 al name must.
219b1 20 20 20 2a 2a 20 62 65 20 22 39 22 20 74 6f 20 ** be "9" to
219b2 61 76 6f 69 64 20 6e 61 6d 65 20 63 6f 6c 6c 69 avoid name colli
219b3 73 69 6f 6e 73 20 77 68 65 6e 20 75 73 69 6e 67 sions when using
219b4 20 38 2b 33 20 66 69 6c 65 6e 61 6d 65 73 2e 20 8+3 filenames.
219b5 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 */. assert(
219b6 20 7a 4d 61 73 74 65 72 5b 73 71 6c 69 74 65 33 zMaster[sqlite3
219b7 53 74 72 6c 65 6e 33 30 28 7a 4d 61 73 74 65 72 Strlen30(zMaster
219b8 29 2d 33 5d 3d 3d 27 39 27 20 29 3b 0a 20 20 20 )-3]=='9' );.
219b9 20 20 20 73 71 6c 69 74 65 33 46 69 6c 65 53 75 sqlite3FileSu
219ba 66 66 69 78 33 28 7a 4d 61 69 6e 46 69 6c 65 2c ffix3(zMainFile,
219bb 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 zMaster);.
219bc 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 rc = sqlite3OsA
219bd 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 ccess(pVfs, zMas
219be 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 ter, SQLITE_ACCE
219bf 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 SS_EXISTS, &res)
219c0 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 ;. }while( rc
219c1 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 ==SQLITE_OK && r
219c2 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 es );. if( rc
219c3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
219c4 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 /* Open the
219c5 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e master journal.
219c6 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 */. rc = s
219c7 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c qlite3OsOpenMall
219c8 6f 63 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 oc(pVfs, zMaster
219c9 2c 20 26 70 4d 61 73 74 65 72 2c 20 0a 20 20 20 , &pMaster, .
219ca 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 SQLITE_OP
219cb 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c EN_READWRITE|SQL
219cc 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c ITE_OPEN_CREATE|
219cd 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 . SQLIT
219ce 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
219cf 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 |SQLITE_OPEN_MAS
219d0 54 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 0a 20 TER_JOURNAL, 0.
219d1 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 );. }.
219d2 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
219d3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
219d4 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
219d5 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72 Master);. r
219d6 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
219d7 20 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 . /* Write t
219d8 68 65 20 6e 61 6d 65 20 6f 66 20 65 61 63 68 20 he name of each
219d9 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e database file in
219da 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
219db 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 20 into the new.
219dc 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 ** master jour
219dd 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e 20 nal file. If an
219de 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 74 20 error occurs at
219df 74 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73 65 this point close
219e0 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64 65 6c 65 . ** and dele
219e1 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f te the master jo
219e2 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c 6c 20 urnal file. All
219e3 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 6a the individual j
219e4 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0a 20 20 20 ournal files.
219e5 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76 65 20 27 ** still have '
219e6 6e 75 6c 6c 27 20 61 73 20 74 68 65 20 6d 61 73 null' as the mas
219e7 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e ter journal poin
219e8 74 65 72 2c 20 73 6f 20 74 68 65 79 20 77 69 6c ter, so they wil
219e9 6c 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 62 61 l roll. ** ba
219ea 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 ck independently
219eb 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 if a failure oc
219ec 63 75 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 curs.. */.
219ed 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e for(i=0; i<db->
219ee 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 nDb; i++){.
219ef 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 Btree *pBt = db
219f0 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 ->aDb[i].pBt;.
219f1 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 if( sqlite3B
219f2 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 treeIsInTrans(pB
219f3 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 t) ){. ch
219f4 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c 65 20 ar const *zFile
219f5 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 = sqlite3BtreeGe
219f6 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 42 74 tJournalname(pBt
219f7 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a );. if( z
219f8 46 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 File==0 ){.
219f9 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 20 20 continue;
219fa 2f 2a 20 49 67 6e 6f 72 65 20 54 45 4d 50 20 61 /* Ignore TEMP a
219fb 6e 64 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61 nd :memory: data
219fc 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 bases */.
219fd 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 }. asser
219fe 74 28 20 7a 46 69 6c 65 5b 30 5d 21 3d 30 20 29 t( zFile[0]!=0 )
219ff 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6e ;. if( !n
21a00 65 65 64 53 79 6e 63 20 26 26 20 21 73 71 6c 69 eedSync && !sqli
21a01 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 61 te3BtreeSyncDisa
21a02 62 6c 65 64 28 70 42 74 29 20 29 7b 0a 20 20 20 bled(pBt) ){.
21a03 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 needSync
21a04 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 1;. }.
21a05 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
21a06 74 65 33 4f 73 57 72 69 74 65 28 70 4d 61 73 74 te3OsWrite(pMast
21a07 65 72 2c 20 7a 46 69 6c 65 2c 20 73 71 6c 69 74 er, zFile, sqlit
21a08 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 e3Strlen30(zFile
21a09 29 2b 31 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 )+1, offset);.
21a0a 20 20 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 offset +=
21a0b 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
21a0c 7a 46 69 6c 65 29 2b 31 3b 0a 20 20 20 20 20 20 zFile)+1;.
21a0d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
21a0e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
21a0f 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 sqlite3OsCloseF
21a10 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 ree(pMaster);.
21a11 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f sqlite3O
21a12 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d sDelete(pVfs, zM
21a13 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 aster, 0);.
21a14 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
21a15 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b ee(db, zMaster);
21a16 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
21a17 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
21a18 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
21a19 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6d /* Sync the m
21a1a 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
21a1b 6c 65 2e 20 49 66 20 74 68 65 20 49 4f 43 41 50 le. If the IOCAP
21a1c 5f 53 45 51 55 45 4e 54 49 41 4c 20 64 65 76 69 _SEQUENTIAL devi
21a1d 63 65 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 ce. ** flag i
21a1e 73 20 73 65 74 20 74 68 69 73 20 69 73 20 6e 6f s set this is no
21a1f 74 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 t required..
21a20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 65 64 53 */. if( needS
21a21 79 6e 63 20 0a 20 20 20 20 20 26 26 20 30 3d 3d ync . && 0==
21a22 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 (sqlite3OsDevice
21a23 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
21a24 70 4d 61 73 74 65 72 29 26 53 51 4c 49 54 45 5f pMaster)&SQLITE_
21a25 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c IOCAP_SEQUENTIAL
21a26 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 ). && SQLITE
21a27 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK!=(rc = sqlit
21a28 65 33 4f 73 53 79 6e 63 28 70 4d 61 73 74 65 72 e3OsSync(pMaster
21a29 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f , SQLITE_SYNC_NO
21a2a 52 4d 41 4c 29 29 0a 20 20 20 20 29 7b 0a 20 20 RMAL)). ){.
21a2b 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
21a2c 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b seFree(pMaster);
21a2d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 . sqlite3Os
21a2e 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 Delete(pVfs, zMa
21a2f 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 ster, 0);.
21a30 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
21a31 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 , zMaster);.
21a32 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
21a33 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 }.. /* Sync
21a34 61 6c 6c 20 74 68 65 20 64 62 20 66 69 6c 65 73 all the db files
21a35 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 65 involved in the
21a36 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 transaction. Th
21a37 65 20 73 61 6d 65 20 63 61 6c 6c 0a 20 20 20 20 e same call.
21a38 2a 2a 20 73 65 74 73 20 74 68 65 20 6d 61 73 74 ** sets the mast
21a39 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 er journal point
21a3a 65 72 20 69 6e 20 65 61 63 68 20 69 6e 64 69 76 er in each indiv
21a3b 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 2e 20 49 idual journal. I
21a3c 66 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f f. ** an erro
21a3d 72 20 6f 63 63 75 72 73 20 68 65 72 65 2c 20 64 r occurs here, d
21a3e 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 o not delete the
21a3f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
21a40 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 file.. **.
21a41 20 2a 2a 20 49 66 20 74 68 65 20 65 72 72 6f 72 ** If the error
21a42 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 occurs during t
21a43 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f he first call to
21a44 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 . ** sqlite3B
21a45 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f treeCommitPhaseO
21a46 6e 65 28 29 2c 20 74 68 65 6e 20 74 68 65 72 65 ne(), then there
21a47 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 68 61 is a chance tha
21a48 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 6d 61 73 t the. ** mas
21a49 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
21a4a 20 77 69 6c 6c 20 62 65 20 6f 72 70 68 61 6e 65 will be orphane
21a4b 64 2e 20 42 75 74 20 77 65 20 63 61 6e 6e 6f 74 d. But we cannot
21a4c 20 64 65 6c 65 74 65 20 69 74 2c 0a 20 20 20 20 delete it,.
21a4d 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 65 20 6d ** in case the m
21a4e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
21a4f 6c 65 20 6e 61 6d 65 20 77 61 73 20 77 72 69 74 le name was writ
21a50 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 ten into the jou
21a51 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 rnal. ** file
21a52 20 62 65 66 6f 72 65 20 74 68 65 20 66 61 69 6c before the fail
21a53 75 72 65 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 ure occurred..
21a54 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 */. for(i=0
21a55 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ; rc==SQLITE_OK
21a56 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b && i<db->nDb; i+
21a57 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65 +){ . Btree
21a58 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b *pBt = db->aDb[
21a59 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 i].pBt;. if
21a5a 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 ( pBt ){.
21a5b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
21a5c 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 eeCommitPhaseOne
21a5d 28 70 42 74 2c 20 7a 4d 61 73 74 65 72 29 3b 0a (pBt, zMaster);.
21a5e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
21a5f 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
21a60 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 Free(pMaster);.
21a61 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
21a62 51 4c 49 54 45 5f 42 55 53 59 20 29 3b 0a 20 20 QLITE_BUSY );.
21a63 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
21a64 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
21a65 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
21a66 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72 Master);. r
21a67 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
21a68 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 . /* Delete t
21a69 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
21a6a 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 63 6f 6d l file. This com
21a6b 6d 69 74 73 20 74 68 65 20 74 72 61 6e 73 61 63 mits the transac
21a6c 74 69 6f 6e 2e 20 41 66 74 65 72 0a 20 20 20 20 tion. After.
21a6d 2a 2a 20 64 6f 69 6e 67 20 74 68 69 73 20 74 68 ** doing this th
21a6e 65 20 64 69 72 65 63 74 6f 72 79 20 69 73 20 73 e directory is s
21a6f 79 6e 63 65 64 20 61 67 61 69 6e 20 62 65 66 6f ynced again befo
21a70 72 65 20 61 6e 79 20 69 6e 64 69 76 69 64 75 61 re any individua
21a71 6c 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 l. ** transac
21a72 74 69 6f 6e 20 66 69 6c 65 73 20 61 72 65 20 64 tion files are d
21a73 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 eleted.. */.
21a74 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
21a75 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d sDelete(pVfs, zM
21a76 61 73 74 65 72 2c 20 31 29 3b 0a 20 20 20 20 73 aster, 1);. s
21a77 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
21a78 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 7a zMaster);. z
21a79 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
21a7a 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
21a7b 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
21a7c 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 66 69 6c .. /* All fil
21a7d 65 73 20 61 6e 64 20 64 69 72 65 63 74 6f 72 69 es and directori
21a7e 65 73 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 es have already
21a7f 62 65 65 6e 20 73 79 6e 63 65 64 2c 20 73 6f 20 been synced, so
21a80 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 the following.
21a81 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 ** calls to sq
21a82 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
21a83 50 68 61 73 65 54 77 6f 28 29 20 61 72 65 20 6f PhaseTwo() are o
21a84 6e 6c 79 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 nly closing file
21a85 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20 64 65 6c s and. ** del
21a86 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 eting or truncat
21a87 69 6e 67 20 6a 6f 75 72 6e 61 6c 73 2e 20 49 66 ing journals. If
21a88 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 something goes
21a89 77 72 6f 6e 67 20 77 68 69 6c 65 0a 20 20 20 20 wrong while.
21a8a 2a 2a 20 74 68 69 73 20 69 73 20 68 61 70 70 65 ** this is happe
21a8b 6e 69 6e 67 20 77 65 20 64 6f 6e 27 74 20 72 65 ning we don't re
21a8c 61 6c 6c 79 20 63 61 72 65 2e 20 54 68 65 20 69 ally care. The i
21a8d 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65 0a ntegrity of the.
21a8e 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 ** transacti
21a8f 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20 67 75 on is already gu
21a90 61 72 61 6e 74 65 65 64 2c 20 62 75 74 20 73 6f aranteed, but so
21a91 6d 65 20 73 74 72 61 79 20 27 63 6f 6c 64 27 20 me stray 'cold'
21a92 6a 6f 75 72 6e 61 6c 73 0a 20 20 20 20 2a 2a 20 journals. **
21a93 6d 61 79 20 62 65 20 6c 79 69 6e 67 20 61 72 6f may be lying aro
21a94 75 6e 64 2e 20 52 65 74 75 72 6e 69 6e 67 20 61 und. Returning a
21a95 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 77 6f 6e n error code won
21a96 27 74 20 68 65 6c 70 20 6d 61 74 74 65 72 73 2e 't help matters.
21a97 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 69 73 61 . */. disa
21a98 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f ble_simulated_io
21a99 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 20 20 73 _errors();. s
21a9a 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
21a9b 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 66 nMalloc();. f
21a9c 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
21a9d 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 b; i++){ .
21a9e 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d Btree *pBt = db-
21a9f 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 >aDb[i].pBt;.
21aa0 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 if( pBt ){.
21aa1 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
21aa2 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f eeCommitPhaseTwo
21aa3 28 70 42 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 (pBt, 1);.
21aa4 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 }. }. sqli
21aa5 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
21aa6 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62 6c 65 oc();. enable
21aa7 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
21aa8 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 71 6c rors();.. sql
21aa9 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 64 ite3VtabCommit(d
21aaa 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a b);. }.#endif..
21aab 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
21aac 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 /* .** This rout
21aad 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 74 20 ine checks that
21aae 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 74 69 the sqlite3.acti
21aaf 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e 74 20 veVdbeCnt count
21ab0 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 74 63 variable.** matc
21ab1 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f hes the number o
21ab2 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 65 20 f vdbe's in the
21ab3 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 56 64 list sqlite3.pVd
21ab4 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63 be that are.** c
21ab5 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 2e urrently active.
21ab6 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 An assertion fa
21ab7 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f 20 63 ils if the two c
21ab8 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 74 ounts do not mat
21ab9 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 ch..** This is a
21aba 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c 66 2d n internal self-
21abb 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 74 20 check only - it
21abc 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 6e 74 is not an essent
21abd 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a ial processing.*
21abe 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 * step..**.** Th
21abf 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 is is a no-op if
21ac0 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e NDEBUG is defin
21ac1 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e ed..*/.#ifndef N
21ac2 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 6f 69 DEBUG.static voi
21ac3 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 d checkActiveVdb
21ac4 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62 eCnt(sqlite3 *db
21ac5 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 ){. Vdbe *p;.
21ac6 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69 int cnt = 0;. i
21ac7 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b 0a 20 nt nWrite = 0;.
21ac8 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a p = db->pVdbe;.
21ac9 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 while( p ){.
21aca 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d if( p->magic==
21acb 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 VDBE_MAGIC_RUN &
21acc 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 & p->pc>=0 ){.
21acd 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 cnt++;.
21ace 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 if( p->readOnly
21acf 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b 3b 0a ==0 ) nWrite++;.
21ad0 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d }. p = p-
21ad1 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 61 73 >pNext;. }. as
21ad2 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d 3e 61 sert( cnt==db->a
21ad3 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 3b 0a ctiveVdbeCnt );.
21ad4 20 20 61 73 73 65 72 74 28 20 6e 57 72 69 74 65 assert( nWrite
21ad5 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 ==db->writeVdbeC
21ad6 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 nt );.}.#else.#d
21ad7 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74 69 76 efine checkActiv
21ad8 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65 6e 64 eVdbeCnt(x).#end
21ad9 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 if../*.** If the
21ada 20 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20 Vdbe passed as
21adb 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
21adc 6e 74 20 6f 70 65 6e 65 64 20 61 20 73 74 61 74 nt opened a stat
21add 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f ement-transactio
21ade 6e 2c 0a 2a 2a 20 63 6c 6f 73 65 20 69 74 20 6e n,.** close it n
21adf 6f 77 2e 20 41 72 67 75 6d 65 6e 74 20 65 4f 70 ow. Argument eOp
21ae0 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
21ae1 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
21ae2 43 4b 20 6f 72 0a 2a 2a 20 53 41 56 45 50 4f 49 CK or.** SAVEPOI
21ae3 4e 54 5f 52 45 4c 45 41 53 45 2e 20 49 66 20 69 NT_RELEASE. If i
21ae4 74 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 t is SAVEPOINT_R
21ae5 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 74 68 OLLBACK, then th
21ae6 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 e statement.** t
21ae7 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f ransaction is ro
21ae8 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 65 4f lled back. If eO
21ae9 70 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 p is SAVEPOINT_R
21aea 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 74 68 65 ELEASE, then the
21aeb 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 .** statement t
21aec 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f ransaction is co
21aed 6d 6d 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 mmtted..**.** If
21aee 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 an IO error occ
21aef 75 72 73 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49 urs, an SQLITE_I
21af0 4f 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 OERR_XXX error c
21af1 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
21af2 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 53 .** Otherwise S
21af3 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c QLITE_OK..*/.SQL
21af4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
21af5 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 sqlite3VdbeClose
21af6 53 74 61 74 65 6d 65 6e 74 28 56 64 62 65 20 2a Statement(Vdbe *
21af7 70 2c 20 69 6e 74 20 65 4f 70 29 7b 0a 20 20 73 p, int eOp){. s
21af8 71 6c 69 74 65 33 20 2a 63 6f 6e 73 74 20 64 62 qlite3 *const db
21af9 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 = p->db;. int
21afa 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
21afb 0a 20 20 2f 2a 20 49 66 20 70 2d 3e 69 53 74 61 . /* If p->iSta
21afc 74 65 6d 65 6e 74 20 69 73 20 67 72 65 61 74 65 tement is greate
21afd 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 r than zero, the
21afe 6e 20 74 68 69 73 20 56 64 62 65 20 6f 70 65 6e n this Vdbe open
21aff 65 64 20 61 20 0a 20 20 2a 2a 20 73 74 61 74 65 ed a . ** state
21b00 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
21b01 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
21b02 63 6c 6f 73 65 64 20 68 65 72 65 2e 20 54 68 65 closed here. The
21b03 20 6f 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 0a only exception.
21b04 20 20 2a 2a 20 69 73 20 74 68 61 74 20 61 6e 20 ** is that an
21b05 49 4f 20 65 72 72 6f 72 20 6d 61 79 20 68 61 76 IO error may hav
21b06 65 20 6f 63 63 75 72 65 64 2c 20 63 61 75 73 69 e occured, causi
21b07 6e 67 20 61 6e 20 65 6d 65 72 67 65 6e 63 79 20 ng an emergency
21b08 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 2a 2a 20 49 rollback.. ** I
21b09 6e 20 74 68 69 73 20 63 61 73 65 20 28 64 62 2d n this case (db-
21b0a 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 29 2c >nStatement==0),
21b0b 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f and there is no
21b0c 74 68 69 6e 67 20 74 6f 20 64 6f 2e 0a 20 20 2a thing to do.. *
21b0d 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6e 53 74 61 /. if( db->nSta
21b0e 74 65 6d 65 6e 74 20 26 26 20 70 2d 3e 69 53 74 tement && p->iSt
21b0f 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69 atement ){. i
21b10 6e 74 20 69 3b 0a 20 20 20 20 63 6f 6e 73 74 20 nt i;. const
21b11 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 20 3d int iSavepoint =
21b12 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 2d 31 p->iStatement-1
21b13 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 ;.. assert( e
21b14 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f Op==SAVEPOINT_RO
21b15 4c 4c 42 41 43 4b 20 7c 7c 20 65 4f 70 3d 3d 53 LLBACK || eOp==S
21b16 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 AVEPOINT_RELEASE
21b17 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 );. assert( d
21b18 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3e 30 20 b->nStatement>0
21b19 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
21b1a 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 28 64 ->iStatement==(d
21b1b 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2b 64 62 b->nStatement+db
21b1c 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 20 29 3b ->nSavepoint) );
21b1d 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 .. for(i=0; i
21b1e 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 <db->nDb; i++){
21b1f 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 20 3d . int rc2 =
21b20 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
21b21 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 Btree *pBt = d
21b22 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
21b23 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a if( pBt ){.
21b24 20 20 20 20 20 20 20 20 69 66 28 20 65 4f 70 3d if( eOp=
21b25 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
21b26 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ACK ){.
21b27 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42 74 rc2 = sqlite3Bt
21b28 72 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42 74 reeSavepoint(pBt
21b29 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c , SAVEPOINT_ROLL
21b2a 42 41 43 4b 2c 20 69 53 61 76 65 70 6f 69 6e 74 BACK, iSavepoint
21b2b 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
21b2c 20 20 20 20 20 69 66 28 20 72 63 32 3d 3d 53 51 if( rc2==SQ
21b2d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
21b2e 20 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 rc2 = sqlit
21b2f 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 e3BtreeSavepoint
21b30 28 70 42 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f (pBt, SAVEPOINT_
21b31 52 45 4c 45 41 53 45 2c 20 69 53 61 76 65 70 6f RELEASE, iSavepo
21b32 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a int);. }.
21b33 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
21b34 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
21b35 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b rc = rc2;
21b36 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
21b37 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d }. }. db-
21b38 3e 6e 53 74 61 74 65 6d 65 6e 74 2d 2d 3b 0a 20 >nStatement--;.
21b39 20 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 p->iStatement
21b3a 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 72 = 0;.. if( r
21b3b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
21b3c 20 20 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 if( eOp==S
21b3d 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
21b3e 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 K ){. rc
21b3f 3d 20 73 71 6c 69 74 65 33 56 74 61 62 53 61 76 = sqlite3VtabSav
21b40 65 70 6f 69 6e 74 28 64 62 2c 20 53 41 56 45 50 epoint(db, SAVEP
21b41 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 69 OINT_ROLLBACK, i
21b42 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 Savepoint);.
21b43 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
21b44 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
21b45 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
21b46 74 65 33 56 74 61 62 53 61 76 65 70 6f 69 6e 74 te3VtabSavepoint
21b47 28 64 62 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 (db, SAVEPOINT_R
21b48 45 4c 45 41 53 45 2c 20 69 53 61 76 65 70 6f 69 ELEASE, iSavepoi
21b49 6e 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nt);. }.
21b4a 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 }.. /* If th
21b4b 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e e statement tran
21b4c 73 61 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 saction is being
21b4d 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 61 6c rolled back, al
21b4e 73 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 0a so restore the .
21b4f 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
21b50 68 61 6e 64 6c 65 73 20 64 65 66 65 72 72 65 64 handles deferred
21b51 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e constraint coun
21b52 74 65 72 20 74 6f 20 74 68 65 20 76 61 6c 75 65 ter to the value
21b53 20 69 74 20 68 61 64 20 77 68 65 6e 20 0a 20 20 it had when .
21b54 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 ** the stateme
21b55 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 nt transaction w
21b56 61 73 20 6f 70 65 6e 65 64 2e 20 20 2a 2f 0a 20 as opened. */.
21b57 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 41 56 45 if( eOp==SAVE
21b58 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 POINT_ROLLBACK )
21b59 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 {. db->nDef
21b5a 65 72 72 65 64 43 6f 6e 73 20 3d 20 70 2d 3e 6e erredCons = p->n
21b5b 53 74 6d 74 44 65 66 43 6f 6e 73 3b 0a 20 20 20 StmtDefCons;.
21b5c 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
21b5d 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
21b5e 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
21b5f 6c 6c 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e lled when a tran
21b60 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 62 saction opened b
21b61 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a y the database .
21b62 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 ** handle associ
21b63 61 74 65 64 20 77 69 74 68 20 74 68 65 20 56 4d ated with the VM
21b64 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 passed as an ar
21b65 67 75 6d 65 6e 74 20 69 73 20 61 62 6f 75 74 20 gument is about
21b66 74 6f 20 62 65 20 0a 2a 2a 20 63 6f 6d 6d 69 74 to be .** commit
21b67 74 65 64 2e 20 49 66 20 74 68 65 72 65 20 61 72 ted. If there ar
21b68 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 65 e outstanding de
21b69 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 20 6b ferred foreign k
21b6a 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a ey constraint.**
21b6b 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20 72 65 74 violations, ret
21b6c 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
21b6d 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c . Otherwise, SQL
21b6e 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 ITE_OK..**.** If
21b6f 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 there are outst
21b70 61 6e 64 69 6e 67 20 46 4b 20 76 69 6f 6c 61 74 anding FK violat
21b71 69 6f 6e 73 20 61 6e 64 20 74 68 69 73 20 66 75 ions and this fu
21b72 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a nction returns .
21b73 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c ** SQLITE_ERROR,
21b74 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 set the result
21b75 6f 66 20 74 68 65 20 56 4d 20 74 6f 20 53 51 4c of the VM to SQL
21b76 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 ITE_CONSTRAINT a
21b77 6e 64 20 77 72 69 74 65 0a 2a 2a 20 61 6e 20 65 nd write.** an e
21b78 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 rror message to
21b79 69 74 2e 20 54 68 65 6e 20 72 65 74 75 72 6e 20 it. Then return
21b7a 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2f SQLITE_ERROR..*/
21b7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
21b7c 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 OMIT_FOREIGN_KEY
21b7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21b7e 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 int sqlite3VdbeC
21b7f 68 65 63 6b 46 6b 28 56 64 62 65 20 2a 70 2c 20 heckFk(Vdbe *p,
21b80 69 6e 74 20 64 65 66 65 72 72 65 64 29 7b 0a 20 int deferred){.
21b81 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
21b82 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 64 65 66 ->db;. if( (def
21b83 65 72 72 65 64 20 26 26 20 64 62 2d 3e 6e 44 65 erred && db->nDe
21b84 66 65 72 72 65 64 43 6f 6e 73 3e 30 29 20 7c 7c ferredCons>0) ||
21b85 20 28 21 64 65 66 65 72 72 65 64 20 26 26 20 70 (!deferred && p
21b86 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 3e ->nFkConstraint>
21b87 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 0) ){. p->rc
21b88 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 = SQLITE_CONSTRA
21b89 49 4e 54 3b 0a 20 20 20 20 70 2d 3e 65 72 72 6f INT;. p->erro
21b8a 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f rAction = OE_Abo
21b8b 72 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 rt;. sqlite3S
21b8c 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
21b8d 72 4d 73 67 2c 20 64 62 2c 20 22 66 6f 72 65 69 rMsg, db, "forei
21b8e 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e gn key constrain
21b8f 74 20 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 20 t failed");.
21b90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
21b91 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ROR;. }. retur
21b92 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 n SQLITE_OK;.}.#
21b93 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 endif../*.** Thi
21b94 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
21b95 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61 20 56 led the when a V
21b96 44 42 45 20 74 72 69 65 73 20 74 6f 20 68 61 6c DBE tries to hal
21b97 74 2e 20 20 49 66 20 74 68 65 20 56 44 42 45 0a t. If the VDBE.
21b98 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68 61 6e ** has made chan
21b99 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61 75 ges and is in au
21b9a 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 74 tocommit mode, t
21b9b 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f 73 65 hen commit those
21b9c 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 49 66 .** changes. If
21b9d 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6e a rollback is n
21b9e 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f 20 74 eeded, then do t
21b9f 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a he rollback..**.
21ba0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
21ba1 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 is the only way
21ba2 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 74 61 74 to move the stat
21ba3 65 20 6f 66 20 61 20 56 4d 20 66 72 6f 6d 0a 2a e of a VM from.*
21ba4 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 52 * SQLITE_MAGIC_R
21ba5 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 UN to SQLITE_MAG
21ba6 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 69 73 20 IC_HALT. It is
21ba7 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 63 harmless to.** c
21ba8 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 20 56 4d all this on a VM
21ba9 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 that is in the
21baa 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 4c SQLITE_MAGIC_HAL
21bab 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 T state..**.** R
21bac 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
21bad 6f 64 65 2e 20 20 49 66 20 74 68 65 20 63 6f 6d ode. If the com
21bae 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 20 63 6f mit could not co
21baf 6d 70 6c 65 74 65 20 62 65 63 61 75 73 65 20 6f mplete because o
21bb0 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e f.** lock conten
21bb1 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 53 51 4c tion, return SQL
21bb2 49 54 45 5f 42 55 53 59 2e 20 20 49 66 20 53 51 ITE_BUSY. If SQ
21bb3 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 LITE_BUSY is ret
21bb4 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d 65 61 urned, it.** mea
21bb5 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 64 69 64 ns the close did
21bb6 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 6e 64 20 not happen and
21bb7 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 70 65 needs to be repe
21bb8 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ated..*/.SQLITE_
21bb9 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
21bba 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62 65 te3VdbeHalt(Vdbe
21bbb 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 *p){. int rc;
21bbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21bbd 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
21bbe 74 6f 20 73 74 6f 72 65 20 74 72 61 6e 73 69 65 to store transie
21bbf 6e 74 20 72 65 74 75 72 6e 20 63 6f 64 65 73 20 nt return codes
21bc0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
21bc1 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 = p->db;.. /*
21bc2 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f This function co
21bc3 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 69 63 ntains the logic
21bc4 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 that determines
21bc5 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 if a statement
21bc6 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 or. ** transact
21bc7 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d ion will be comm
21bc8 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 itted or rolled
21bc9 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74 back as a result
21bca 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65 78 65 of the. ** exe
21bcb 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 76 cution of this v
21bcc 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 irtual machine.
21bcd 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6e . **. ** If an
21bce 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 y of the followi
21bcf 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75 72 3a ng errors occur:
21bd0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 . **. ** S
21bd1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a QLITE_NOMEM. **
21bd2 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 SQLITE_IOER
21bd3 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 R. ** SQLIT
21bd4 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 E_FULL. **
21bd5 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 SQLITE_INTERRUPT
21bd6 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 . **. ** Then
21bd7 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 63 the internal cac
21bd8 68 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 65 he might have be
21bd9 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e en left in an in
21bda 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a 2a 20 consistent. **
21bdb 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65 64 20 state. We need
21bdc 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 to rollback the
21bdd 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
21bde 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72 65 20 ction, if there
21bdf 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72 20 is. ** one, or
21be0 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 72 61 the complete tra
21be1 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 72 nsaction if ther
21be2 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e e is no statemen
21be3 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 t transaction..
21be4 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 64 62 */.. if( p->db
21be5 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
21be6 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 {. p->rc = SQ
21be7 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
21be8 20 20 69 66 28 20 70 2d 3e 61 4f 6e 63 65 46 6c if( p->aOnceFl
21be9 61 67 20 29 20 6d 65 6d 73 65 74 28 70 2d 3e 61 ag ) memset(p->a
21bea 4f 6e 63 65 46 6c 61 67 2c 20 30 2c 20 70 2d 3e OnceFlag, 0, p->
21beb 6e 4f 6e 63 65 46 6c 61 67 29 3b 0a 20 20 63 6c nOnceFlag);. cl
21bec 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 29 oseAllCursors(p)
21bed 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 ;. if( p->magic
21bee 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e !=VDBE_MAGIC_RUN
21bef 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
21bf0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
21bf1 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 checkActiveVdbeC
21bf2 6e 74 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4e 6f nt(db);.. /* No
21bf3 20 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 commit or rollb
21bf4 61 63 6b 20 6e 65 65 64 65 64 20 69 66 20 74 68 ack needed if th
21bf5 65 20 70 72 6f 67 72 61 6d 20 6e 65 76 65 72 20 e program never
21bf6 73 74 61 72 74 65 64 20 2a 2f 0a 20 20 69 66 28 started */. if(
21bf7 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 p->pc>=0 ){.
21bf8 20 69 6e 74 20 6d 72 63 3b 20 20 20 2f 2a 20 50 int mrc; /* P
21bf9 72 69 6d 61 72 79 20 65 72 72 6f 72 20 63 6f 64 rimary error cod
21bfa 65 20 66 72 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0a e from p->rc */.
21bfb 20 20 20 20 69 6e 74 20 65 53 74 61 74 65 6d 65 int eStateme
21bfc 6e 74 4f 70 20 3d 20 30 3b 0a 20 20 20 20 69 6e ntOp = 0;. in
21bfd 74 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 t isSpecialError
21bfe 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
21bff 53 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 Set to true if a
21c00 20 27 73 70 65 63 69 61 6c 27 20 65 72 72 6f 72 'special' error
21c01 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b */.. /* Lock
21c02 20 61 6c 6c 20 62 74 72 65 65 73 20 75 73 65 64 all btrees used
21c03 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e by the statemen
21c04 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 t */. sqlite3
21c05 56 64 62 65 45 6e 74 65 72 28 70 29 3b 0a 0a 20 VdbeEnter(p);..
21c06 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 /* Check for
21c07 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 one of the speci
21c08 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 al errors */.
21c09 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 30 mrc = p->rc & 0
21c0a 78 66 66 3b 0a 20 20 20 20 61 73 73 65 72 74 28 xff;. assert(
21c0b 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 49 p->rc!=SQLITE_I
21c0c 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b 20 OERR_BLOCKED );
21c0d 20 2f 2a 20 54 68 69 73 20 65 72 72 6f 72 20 6e /* This error n
21c0e 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73 20 o longer exists
21c0f 2a 2f 0a 20 20 20 20 69 73 53 70 65 63 69 61 6c */. isSpecial
21c10 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 51 4c Error = mrc==SQL
21c11 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63 ITE_NOMEM || mrc
21c12 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 ==SQLITE_IOERR.
21c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c14 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 || mrc==SQLI
21c15 54 45 5f 49 4e 54 45 52 52 55 50 54 20 7c 7c 20 TE_INTERRUPT ||
21c16 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c mrc==SQLITE_FULL
21c17 3b 0a 20 20 20 20 69 66 28 20 69 73 53 70 65 63 ;. if( isSpec
21c18 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 ialError ){.
21c19 20 20 2f 2a 20 49 66 20 74 68 65 20 71 75 65 72 /* If the quer
21c1a 79 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79 20 y was read-only
21c1b 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f and the error co
21c1c 64 65 20 69 73 20 53 51 4c 49 54 45 5f 49 4e 54 de is SQLITE_INT
21c1d 45 52 52 55 50 54 2c 20 0a 20 20 20 20 20 20 2a ERRUPT, . *
21c1e 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 69 73 * no rollback is
21c1f 20 6e 65 63 65 73 73 61 72 79 2e 20 4f 74 68 65 necessary. Othe
21c20 72 77 69 73 65 2c 20 61 74 20 6c 65 61 73 74 20 rwise, at least
21c21 61 20 73 61 76 65 70 6f 69 6e 74 20 0a 20 20 20 a savepoint .
21c22 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f ** transactio
21c23 6e 20 6d 75 73 74 20 62 65 20 72 6f 6c 6c 65 64 n must be rolled
21c24 20 62 61 63 6b 20 74 6f 20 72 65 73 74 6f 72 65 back to restore
21c25 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f the database to
21c26 20 61 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e a . ** con
21c27 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 20 sistent state..
21c28 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
21c29 20 45 76 65 6e 20 69 66 20 74 68 65 20 73 74 61 Even if the sta
21c2a 74 65 6d 65 6e 74 20 69 73 20 72 65 61 64 2d 6f tement is read-o
21c2b 6e 6c 79 2c 20 69 74 20 69 73 20 69 6d 70 6f 72 nly, it is impor
21c2c 74 61 6e 74 20 74 6f 20 70 65 72 66 6f 72 6d 0a tant to perform.
21c2d 20 20 20 20 20 20 2a 2a 20 61 20 73 74 61 74 65 ** a state
21c2e 6d 65 6e 74 20 6f 72 20 74 72 61 6e 73 61 63 74 ment or transact
21c2f 69 6f 6e 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65 ion rollback ope
21c30 72 61 74 69 6f 6e 2e 20 49 66 20 74 68 65 20 65 ration. If the e
21c31 72 72 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 6f rror . ** o
21c32 63 63 75 72 65 64 20 77 68 69 6c 65 20 77 72 69 ccured while wri
21c33 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 ting to the jour
21c34 6e 61 6c 2c 20 73 75 62 2d 6a 6f 75 72 6e 61 6c nal, sub-journal
21c35 20 6f 72 20 64 61 74 61 62 61 73 65 0a 20 20 20 or database.
21c36 20 20 20 2a 2a 20 66 69 6c 65 20 61 73 20 70 61 ** file as pa
21c37 72 74 20 6f 66 20 61 6e 20 65 66 66 6f 72 74 20 rt of an effort
21c38 74 6f 20 66 72 65 65 20 75 70 20 63 61 63 68 65 to free up cache
21c39 20 73 70 61 63 65 20 28 73 65 65 20 66 75 6e 63 space (see func
21c3a 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 70 61 tion. ** pa
21c3b 67 65 72 53 74 72 65 73 73 28 29 20 69 6e 20 70 gerStress() in p
21c3c 61 67 65 72 2e 63 29 2c 20 74 68 65 20 72 6f 6c ager.c), the rol
21c3d 6c 62 61 63 6b 20 69 73 20 72 65 71 75 69 72 65 lback is require
21c3e 64 20 74 6f 20 72 65 73 74 6f 72 65 20 0a 20 20 d to restore .
21c3f 20 20 20 20 2a 2a 20 74 68 65 20 70 61 67 65 72 ** the pager
21c40 20 74 6f 20 61 20 63 6f 6e 73 69 73 74 65 6e 74 to a consistent
21c41 20 73 74 61 74 65 2e 0a 20 20 20 20 20 20 2a 2f state.. */
21c42 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 72 . if( !p->r
21c43 65 61 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d eadOnly || mrc!=
21c44 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 SQLITE_INTERRUPT
21c45 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
21c46 28 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d (mrc==SQLITE_NOM
21c47 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 EM || mrc==SQLIT
21c48 45 5f 46 55 4c 4c 29 20 26 26 20 70 2d 3e 75 73 E_FULL) && p->us
21c49 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b esStmtJournal ){
21c4a 0a 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74 . eStat
21c4b 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f ementOp = SAVEPO
21c4c 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 INT_ROLLBACK;.
21c4d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
21c4e 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65 /* We are
21c4f 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 forced to roll
21c50 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20 back the active
21c51 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 transaction. Bef
21c52 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 ore doing.
21c53 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 ** so, abort
21c54 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 any other state
21c55 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c ments this handl
21c56 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 e currently has
21c57 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20 active..
21c58 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 */. s
21c59 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c qlite3RollbackAl
21c5a 6c 28 64 62 2c 20 53 51 4c 49 54 45 5f 41 42 4f l(db, SQLITE_ABO
21c5b 52 54 5f 52 4f 4c 4c 42 41 43 4b 29 3b 0a 20 20 RT_ROLLBACK);.
21c5c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 sqlite3C
21c5d 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 64 loseSavepoints(d
21c5e 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 b);. db
21c5f 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 ->autoCommit = 1
21c60 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
21c61 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
21c62 2a 20 43 68 65 63 6b 20 66 6f 72 20 69 6d 6d 65 * Check for imme
21c63 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 diate foreign ke
21c64 79 20 76 69 6f 6c 61 74 69 6f 6e 73 2e 20 2a 2f y violations. */
21c65 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d . if( p->rc==
21c66 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
21c67 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
21c68 65 63 6b 46 6b 28 70 2c 20 30 29 3b 0a 20 20 20 eckFk(p, 0);.
21c69 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 }. . /* If
21c6a 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 the auto-commit
21c6b 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 20 flag is set and
21c6c 74 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 this is the only
21c6d 20 61 63 74 69 76 65 20 77 72 69 74 65 72 20 0a active writer .
21c6e 20 20 20 20 2a 2a 20 56 4d 2c 20 74 68 65 6e 20 ** VM, then
21c6f 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20 63 we do either a c
21c70 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 63 ommit or rollbac
21c71 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 k of the current
21c72 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 20 transaction. .
21c73 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 **. ** Not
21c74 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61 6c e: This block al
21c75 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20 6f so runs if one o
21c76 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 72 f the special er
21c77 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20 20 rors handled .
21c78 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20 6f ** above has o
21c79 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a 2f ccurred. . */
21c7a 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 . if( !sqlite
21c7b 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 3VtabInSync(db)
21c7c 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75 74 . && db->aut
21c7d 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26 26 oCommit . &&
21c7e 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e db->writeVdbeCn
21c7f 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d t==(p->readOnly=
21c80 3d 30 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 =0) . ){.
21c81 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c if( p->rc==SQL
21c82 49 54 45 5f 4f 4b 20 7c 7c 20 28 70 2d 3e 65 72 ITE_OK || (p->er
21c83 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 rorAction==OE_Fa
21c84 69 6c 20 26 26 20 21 69 73 53 70 65 63 69 61 6c il && !isSpecial
21c85 45 72 72 6f 72 29 20 29 7b 0a 20 20 20 20 20 20 Error) ){.
21c86 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
21c87 62 65 43 68 65 63 6b 46 6b 28 70 2c 20 31 29 3b beCheckFk(p, 1);
21c88 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
21c89 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
21c8a 20 20 20 20 20 20 20 20 69 66 28 20 4e 45 56 45 if( NEVE
21c8b 52 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 R(p->readOnly) )
21c8c 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 {. sq
21c8d 6c 69 74 65 33 56 64 62 65 4c 65 61 76 65 28 70 lite3VdbeLeave(p
21c8e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 );. r
21c8f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
21c90 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a OR;. }.
21c91 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
21c92 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 QLITE_CONSTRAINT
21c93 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
21c94 20 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 . /* T
21c95 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 he auto-commit f
21c96 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 lag is true, the
21c97 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 61 vdbe program wa
21c98 73 20 73 75 63 63 65 73 73 66 75 6c 20 0a 20 20 s successful .
21c99 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 68 69 ** or hi
21c9a 74 20 61 6e 20 27 4f 52 20 46 41 49 4c 27 20 63 t an 'OR FAIL' c
21c9b 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74 68 onstraint and th
21c9c 65 72 65 20 61 72 65 20 6e 6f 20 64 65 66 65 72 ere are no defer
21c9d 72 65 64 20 66 6f 72 65 69 67 6e 0a 20 20 20 20 red foreign.
21c9e 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6e ** key con
21c9f 73 74 72 61 69 6e 74 73 20 74 6f 20 68 6f 6c 64 straints to hold
21ca0 20 75 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 up the transact
21ca1 69 6f 6e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 ion. This means
21ca2 61 20 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20 a commit .
21ca3 20 20 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 ** is requir
21ca4 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 ed. */.
21ca5 20 72 63 20 3d 20 76 64 62 65 43 6f 6d 6d 69 74 rc = vdbeCommit
21ca6 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 (db, p);.
21ca7 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 }. if( r
21ca8 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 c==SQLITE_BUSY &
21ca9 26 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b & p->readOnly ){
21caa 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
21cab 65 33 56 64 62 65 4c 65 61 76 65 28 70 29 3b 0a e3VdbeLeave(p);.
21cac 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
21cad 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
21cae 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
21caf 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
21cb0 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 . p->rc
21cb1 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 20 20 20 = rc;.
21cb2 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b sqlite3Rollback
21cb3 41 6c 6c 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f All(db, SQLITE_O
21cb4 4b 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 K);. }els
21cb5 65 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d e{. db-
21cb6 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20 3d >nDeferredCons =
21cb7 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 0;. sq
21cb8 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 lite3CommitInter
21cb9 6e 61 6c 43 68 61 6e 67 65 73 28 64 62 29 3b 0a nalChanges(db);.
21cba 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
21cbb 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 }else{. s
21cbc 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c qlite3RollbackAl
21cbd 6c 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29 l(db, SQLITE_OK)
21cbe 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
21cbf 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d db->nStatement =
21cc0 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 0;. }else if
21cc1 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 3d 3d ( eStatementOp==
21cc2 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 0 ){. if( p
21cc3 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ->rc==SQLITE_OK
21cc4 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f || p->errorActio
21cc5 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20 n==OE_Fail ){.
21cc6 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e 74 eStatement
21cc7 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 Op = SAVEPOINT_R
21cc8 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 7d 65 ELEASE;. }e
21cc9 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f 72 lse if( p->error
21cca 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74 Action==OE_Abort
21ccb 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74 61 ){. eSta
21ccc 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 tementOp = SAVEP
21ccd 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 OINT_ROLLBACK;.
21cce 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
21ccf 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 sqlite3Rollb
21cd0 61 63 6b 41 6c 6c 28 64 62 2c 20 53 51 4c 49 54 ackAll(db, SQLIT
21cd1 45 5f 41 42 4f 52 54 5f 52 4f 4c 4c 42 41 43 4b E_ABORT_ROLLBACK
21cd2 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
21cd3 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 e3CloseSavepoint
21cd4 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 64 s(db);. d
21cd5 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 b->autoCommit =
21cd6 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
21cd7 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 65 53 . . /* If eS
21cd8 74 61 74 65 6d 65 6e 74 4f 70 20 69 73 20 6e 6f tatementOp is no
21cd9 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 73 n-zero, then a s
21cda 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
21cdb 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 0a 20 20 tion needs to.
21cdc 20 20 2a 2a 20 62 65 20 63 6f 6d 6d 69 74 74 65 ** be committe
21cdd 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b d or rolled back
21cde 2e 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 . Call sqlite3Vd
21cdf 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 beCloseStatement
21ce0 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 64 6f 20 () to. ** do
21ce1 73 6f 2e 20 49 66 20 74 68 69 73 20 6f 70 65 72 so. If this oper
21ce2 61 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 6e ation returns an
21ce3 20 65 72 72 6f 72 2c 20 61 6e 64 20 74 68 65 20 error, and the
21ce4 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e current statemen
21ce5 74 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20 63 t. ** error c
21ce6 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f 4f 4b ode is SQLITE_OK
21ce7 20 6f 72 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 or SQLITE_CONST
21ce8 52 41 49 4e 54 2c 20 74 68 65 6e 20 70 72 6f 6d RAINT, then prom
21ce9 6f 74 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 ote the. ** c
21cea 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 urrent statement
21ceb 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 20 20 20 error code..
21cec 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 53 74 61 */. if( eSta
21ced 74 65 6d 65 6e 74 4f 70 20 29 7b 0a 20 20 20 20 tementOp ){.
21cee 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
21cef 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 beCloseStatement
21cf0 28 70 2c 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 (p, eStatementOp
21cf1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 );. if( rc
21cf2 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
21cf3 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ->rc==SQLITE_OK
21cf4 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 || p->rc==SQLITE
21cf5 5f 43 4f 4e 53 54 52 41 49 4e 54 20 29 7b 0a 20 _CONSTRAINT ){.
21cf6 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d p->rc =
21cf7 20 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73 rc;. s
21cf8 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
21cf9 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
21cfa 20 20 20 20 20 20 20 20 70 2d 3e 7a 45 72 72 4d p->zErrM
21cfb 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 sg = 0;.
21cfc 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
21cfd 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 2c 3RollbackAll(db,
21cfe 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 5f 52 4f SQLITE_ABORT_RO
21cff 4c 4c 42 41 43 4b 29 3b 0a 20 20 20 20 20 20 20 LLBACK);.
21d00 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 sqlite3CloseSav
21d01 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20 epoints(db);.
21d02 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d db->autoCom
21d03 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d mit = 1;. }
21d04 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
21d05 20 49 66 20 74 68 69 73 20 77 61 73 20 61 6e 20 If this was an
21d06 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20 6f INSERT, UPDATE o
21d07 72 20 44 45 4c 45 54 45 20 61 6e 64 20 6e 6f 20 r DELETE and no
21d08 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
21d09 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 68 61 73 ction. ** has
21d0a 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 63 been rolled bac
21d0b 6b 2c 20 75 70 64 61 74 65 20 74 68 65 20 64 61 k, update the da
21d0c 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
21d0d 6e 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 n change-counter
21d0e 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 . . */. if
21d0f 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e ( p->changeCntOn
21d10 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 53 ){. if( eS
21d11 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56 45 tatementOp!=SAVE
21d12 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 POINT_ROLLBACK )
21d13 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
21d14 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 3VdbeSetChanges(
21d15 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b db, p->nChange);
21d16 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
21d17 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21d18 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 eSetChanges(db,
21d19 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0);. }.
21d1a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 p->nChange = 0
21d1b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
21d1c 52 65 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b Release the lock
21d1d 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 s */. sqlite3
21d1e 56 64 62 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 VdbeLeave(p);.
21d1f 7d 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76 65 20 }.. /* We have
21d20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 68 61 6c successfully hal
21d21 74 65 64 20 61 6e 64 20 63 6c 6f 73 65 64 20 74 ted and closed t
21d22 68 65 20 56 4d 2e 20 20 52 65 63 6f 72 64 20 74 he VM. Record t
21d23 68 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20 20 69 his fact. */. i
21d24 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 f( p->pc>=0 ){.
21d25 20 20 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 db->activeVdb
21d26 65 43 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 eCnt--;. if(
21d27 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a !p->readOnly ){.
21d28 20 20 20 20 20 20 64 62 2d 3e 77 72 69 74 65 56 db->writeV
21d29 64 62 65 43 6e 74 2d 2d 3b 0a 20 20 20 20 7d 0a dbeCnt--;. }.
21d2a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
21d2b 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 3d 64 activeVdbeCnt>=d
21d2c 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 20 b->writeVdbeCnt
21d2d 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6d 61 67 69 );. }. p->magi
21d2e 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 c = VDBE_MAGIC_H
21d2f 41 4c 54 3b 0a 20 20 63 68 65 63 6b 41 63 74 69 ALT;. checkActi
21d30 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 20 veVdbeCnt(db);.
21d31 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c if( p->db->mall
21d32 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
21d33 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e p->rc = SQLITE_N
21d34 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 OMEM;. }.. /*
21d35 49 66 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d If the auto-comm
21d36 69 74 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 it flag is set t
21d37 6f 20 74 72 75 65 2c 20 74 68 65 6e 20 61 6e 79 o true, then any
21d38 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 65 72 65 locks that were
21d39 20 68 65 6c 64 0a 20 20 2a 2a 20 62 79 20 63 6f held. ** by co
21d3a 6e 6e 65 63 74 69 6f 6e 20 64 62 20 68 61 76 65 nnection db have
21d3b 20 6e 6f 77 20 62 65 65 6e 20 72 65 6c 65 61 73 now been releas
21d3c 65 64 2e 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 ed. Call sqlite3
21d3d 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b ConnectionUnlock
21d3e 65 64 28 29 20 0a 20 20 2a 2a 20 74 6f 20 69 6e ed() . ** to in
21d3f 76 6f 6b 65 20 61 6e 79 20 72 65 71 75 69 72 65 voke any require
21d40 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 d unlock-notify
21d41 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a callbacks.. */.
21d42 20 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f if( db->autoCo
21d43 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 mmit ){. sqli
21d44 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c te3ConnectionUnl
21d45 6f 63 6b 65 64 28 64 62 29 3b 0a 20 20 7d 0a 0a ocked(db);. }..
21d46 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63 assert( db->ac
21d47 74 69 76 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c tiveVdbeCnt>0 ||
21d48 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d db->autoCommit=
21d49 3d 30 20 7c 7c 20 64 62 2d 3e 6e 53 74 61 74 65 =0 || db->nState
21d4a 6d 65 6e 74 3d 3d 30 20 29 3b 0a 20 20 72 65 74 ment==0 );. ret
21d4b 75 72 6e 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 urn (p->rc==SQLI
21d4c 54 45 5f 42 55 53 59 20 3f 20 53 51 4c 49 54 45 TE_BUSY ? SQLITE
21d4d 5f 42 55 53 59 20 3a 20 53 51 4c 49 54 45 5f 4f _BUSY : SQLITE_O
21d4e 4b 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 K);.}.../*.** Ea
21d4f 63 68 20 56 44 42 45 20 68 6f 6c 64 73 20 74 68 ch VDBE holds th
21d50 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 e result of the
21d51 6d 6f 73 74 20 72 65 63 65 6e 74 20 73 71 6c 69 most recent sqli
21d52 74 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 0a te3_step() call.
21d53 2a 2a 20 69 6e 20 70 2d 3e 72 63 2e 20 20 54 68 ** in p->rc. Th
21d54 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 is routine sets
21d55 74 68 61 74 20 72 65 73 75 6c 74 20 62 61 63 6b that result back
21d56 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a to SQLITE_OK..*
21d57 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21d58 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
21d59 65 52 65 73 65 74 53 74 65 70 52 65 73 75 6c 74 eResetStepResult
21d5a 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 70 2d 3e (Vdbe *p){. p->
21d5b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
21d5c 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 }../*.** Copy th
21d5d 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 e error code and
21d5e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 error message b
21d5f 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 68 65 20 elonging to the
21d60 56 44 42 45 20 70 61 73 73 65 64 0a 2a 2a 20 61 VDBE passed.** a
21d61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 s the first argu
21d62 6d 65 6e 74 20 74 6f 20 69 74 73 20 64 61 74 61 ment to its data
21d63 62 61 73 65 20 68 61 6e 64 6c 65 20 28 73 6f 20 base handle (so
21d64 74 68 61 74 20 74 68 65 79 20 77 69 6c 6c 20 62 that they will b
21d65 65 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 e .** returned b
21d66 79 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 y calls to sqlit
21d67 65 33 5f 65 72 72 63 6f 64 65 28 29 20 61 6e 64 e3_errcode() and
21d68 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 sqlite3_errmsg(
21d69 29 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ))..**.** This f
21d6a 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 unction does not
21d6b 20 63 6c 65 61 72 20 74 68 65 20 56 44 42 45 20 clear the VDBE
21d6c 65 72 72 6f 72 20 63 6f 64 65 20 6f 72 20 6d 65 error code or me
21d6d 73 73 61 67 65 2c 20 6a 75 73 74 0a 2a 2a 20 63 ssage, just.** c
21d6e 6f 70 69 65 73 20 74 68 65 6d 20 74 6f 20 74 68 opies them to th
21d6f 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
21d70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
21d71 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
21d72 56 64 62 65 54 72 61 6e 73 66 65 72 45 72 72 6f VdbeTransferErro
21d73 72 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 r(Vdbe *p){. sq
21d74 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 lite3 *db = p->d
21d75 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 2d b;. int rc = p-
21d76 3e 72 63 3b 0a 20 20 69 66 28 20 70 2d 3e 7a 45 >rc;. if( p->zE
21d77 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 75 38 20 rrMsg ){. u8
21d78 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 64 mallocFailed = d
21d79 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b b->mallocFailed;
21d7a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 . sqlite3Begi
21d7b 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b nBenignMalloc();
21d7c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 . sqlite3Valu
21d7d 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 eSetStr(db->pErr
21d7e 2c 20 2d 31 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 , -1, p->zErrMsg
21d7f 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 , SQLITE_UTF8, S
21d80 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
21d81 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 ;. sqlite3End
21d82 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
21d83 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
21d84 69 6c 65 64 20 3d 20 6d 61 6c 6c 6f 63 46 61 69 iled = mallocFai
21d85 6c 65 64 3b 0a 20 20 20 20 64 62 2d 3e 65 72 72 led;. db->err
21d86 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 7d 65 6c Code = rc;. }el
21d87 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 se{. sqlite3E
21d88 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b rror(db, rc, 0);
21d89 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
21d8a 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
21d8b 54 45 5f 45 4e 41 42 4c 45 5f 53 51 4c 4c 4f 47 TE_ENABLE_SQLLOG
21d8c 0a 2f 2a 0a 2a 2a 20 49 66 20 61 6e 20 53 51 4c ./*.** If an SQL
21d8d 49 54 45 5f 43 4f 4e 46 49 47 5f 53 51 4c 4c 4f ITE_CONFIG_SQLLO
21d8e 47 20 68 6f 6f 6b 20 69 73 20 72 65 67 69 73 74 G hook is regist
21d8f 65 72 65 64 20 61 6e 64 20 74 68 65 20 56 4d 20 ered and the VM
21d90 68 61 73 20 62 65 65 6e 20 72 75 6e 2c 20 0a 2a has been run, .*
21d91 2a 20 69 6e 76 6f 6b 65 20 69 74 2e 0a 2a 2f 0a * invoke it..*/.
21d92 73 74 61 74 69 63 20 76 6f 69 64 20 76 64 62 65 static void vdbe
21d93 49 6e 76 6f 6b 65 53 71 6c 6c 6f 67 28 56 64 62 InvokeSqllog(Vdb
21d94 65 20 2a 76 29 7b 0a 20 20 69 66 28 20 73 71 6c e *v){. if( sql
21d95 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
21d96 2e 78 53 71 6c 6c 6f 67 20 26 26 20 76 2d 3e 72 .xSqllog && v->r
21d97 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
21d98 76 2d 3e 7a 53 71 6c 20 26 26 20 76 2d 3e 70 63 v->zSql && v->pc
21d99 3e 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 >=0 ){. char
21d9a 2a 7a 45 78 70 61 6e 64 65 64 20 3d 20 73 71 6c *zExpanded = sql
21d9b 69 74 65 33 56 64 62 65 45 78 70 61 6e 64 53 71 ite3VdbeExpandSq
21d9c 6c 28 76 2c 20 76 2d 3e 7a 53 71 6c 29 3b 0a 20 l(v, v->zSql);.
21d9d 20 20 20 61 73 73 65 72 74 28 20 76 2d 3e 64 62 assert( v->db
21d9e 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 ->init.busy==0 )
21d9f 3b 0a 20 20 20 20 69 66 28 20 7a 45 78 70 61 6e ;. if( zExpan
21da0 64 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ded ){. sql
21da1 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
21da2 2e 78 53 71 6c 6c 6f 67 28 0a 20 20 20 20 20 20 .xSqllog(.
21da3 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
21da4 6c 43 6f 6e 66 69 67 2e 70 53 71 6c 6c 6f 67 41 lConfig.pSqllogA
21da5 72 67 2c 20 76 2d 3e 64 62 2c 20 7a 45 78 70 61 rg, v->db, zExpa
21da6 6e 64 65 64 2c 20 31 0a 20 20 20 20 20 20 29 3b nded, 1. );
21da7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
21da8 46 72 65 65 28 76 2d 3e 64 62 2c 20 7a 45 78 70 Free(v->db, zExp
21da9 61 6e 64 65 64 29 3b 0a 20 20 20 20 7d 0a 20 20 anded);. }.
21daa 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 }.}.#else.# defi
21dab 6e 65 20 76 64 62 65 49 6e 76 6f 6b 65 53 71 6c ne vdbeInvokeSql
21dac 6c 6f 67 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f log(x).#endif../
21dad 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61 20 *.** Clean up a
21dae 56 44 42 45 20 61 66 74 65 72 20 65 78 65 63 75 VDBE after execu
21daf 74 69 6f 6e 20 62 75 74 20 64 6f 20 6e 6f 74 20 tion but do not
21db0 64 65 6c 65 74 65 20 74 68 65 20 56 44 42 45 20 delete the VDBE
21db1 6a 75 73 74 20 79 65 74 2e 0a 2a 2a 20 57 72 69 just yet..** Wri
21db2 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 te any error mes
21db3 73 61 67 65 73 20 69 6e 74 6f 20 2a 70 7a 45 72 sages into *pzEr
21db4 72 4d 73 67 2e 20 20 52 65 74 75 72 6e 20 74 68 rMsg. Return th
21db5 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a 2a e result code..*
21db6 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 *.** After this
21db7 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e 2c 20 routine is run,
21db8 74 68 65 20 56 44 42 45 20 73 68 6f 75 6c 64 20 the VDBE should
21db9 62 65 20 72 65 61 64 79 20 74 6f 20 62 65 20 65 be ready to be e
21dba 78 65 63 75 74 65 64 0a 2a 2a 20 61 67 61 69 6e xecuted.** again
21dbb 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 6c 6f 6f 6b 20 ..**.** To look
21dbc 61 74 20 69 74 20 61 6e 6f 74 68 65 72 20 77 61 at it another wa
21dbd 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 y, this routine
21dbe 72 65 73 65 74 73 20 74 68 65 20 73 74 61 74 65 resets the state
21dbf 20 6f 66 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 of the.** virtu
21dc0 61 6c 20 6d 61 63 68 69 6e 65 20 66 72 6f 6d 20 al machine from
21dc1 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 6f VDBE_MAGIC_RUN o
21dc2 72 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c r VDBE_MAGIC_HAL
21dc3 54 20 62 61 63 6b 20 74 6f 0a 2a 2a 20 56 44 42 T back to.** VDB
21dc4 45 5f 4d 41 47 49 43 5f 49 4e 49 54 2e 0a 2a 2f E_MAGIC_INIT..*/
21dc5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21dc6 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 int sqlite3VdbeR
21dc7 65 73 65 74 28 56 64 62 65 20 2a 70 29 7b 0a 20 eset(Vdbe *p){.
21dc8 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 sqlite3 *db;.
21dc9 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f db = p->db;.. /
21dca 2a 20 49 66 20 74 68 65 20 56 4d 20 64 69 64 20 * If the VM did
21dcb 6e 6f 74 20 72 75 6e 20 74 6f 20 63 6f 6d 70 6c not run to compl
21dcc 65 74 69 6f 6e 20 6f 72 20 69 66 20 69 74 20 65 etion or if it e
21dcd 6e 63 6f 75 6e 74 65 72 65 64 20 61 6e 0a 20 20 ncountered an.
21dce 2a 2a 20 65 72 72 6f 72 2c 20 74 68 65 6e 20 69 ** error, then i
21dcf 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 65 t might not have
21dd0 20 62 65 65 6e 20 68 61 6c 74 65 64 20 70 72 6f been halted pro
21dd1 70 65 72 6c 79 2e 20 20 53 6f 20 68 61 6c 74 0a perly. So halt.
21dd2 20 20 2a 2a 20 69 74 20 6e 6f 77 2e 0a 20 20 2a ** it now.. *
21dd3 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 /. sqlite3VdbeH
21dd4 61 6c 74 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 alt(p);.. /* If
21dd5 20 74 68 65 20 56 44 42 45 20 68 61 73 20 62 65 the VDBE has be
21dd6 20 72 75 6e 20 65 76 65 6e 20 70 61 72 74 69 61 run even partia
21dd7 6c 6c 79 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 lly, then transf
21dd8 65 72 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 er the error cod
21dd9 65 0a 20 20 2a 2a 20 61 6e 64 20 65 72 72 6f 72 e. ** and error
21dda 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 message from th
21ddb 65 20 56 44 42 45 20 69 6e 74 6f 20 74 68 65 20 e VDBE into the
21ddc 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 main database st
21ddd 72 75 63 74 75 72 65 2e 20 20 42 75 74 0a 20 20 ructure. But.
21dde 2a 2a 20 69 66 20 74 68 65 20 56 44 42 45 20 68 ** if the VDBE h
21ddf 61 73 20 6a 75 73 74 20 62 65 65 6e 20 73 65 74 as just been set
21de0 20 74 6f 20 72 75 6e 20 62 75 74 20 68 61 73 20 to run but has
21de1 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 65 78 65 not actually exe
21de2 63 75 74 65 64 20 61 6e 79 0a 20 20 2a 2a 20 69 cuted any. ** i
21de3 6e 73 74 72 75 63 74 69 6f 6e 73 20 79 65 74 2c nstructions yet,
21de4 20 6c 65 61 76 65 20 74 68 65 20 6d 61 69 6e 20 leave the main
21de5 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 20 69 database error i
21de6 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 6e 63 68 61 nformation uncha
21de7 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 nged.. */. if(
21de8 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 p->pc>=0 ){.
21de9 20 76 64 62 65 49 6e 76 6f 6b 65 53 71 6c 6c 6f vdbeInvokeSqllo
21dea 67 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 g(p);. sqlite
21deb 33 56 64 62 65 54 72 61 6e 73 66 65 72 45 72 72 3VdbeTransferErr
21dec 6f 72 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 or(p);. sqlit
21ded 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
21dee 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d zErrMsg);. p-
21def 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
21df0 20 20 69 66 28 20 70 2d 3e 72 75 6e 4f 6e 6c 79 if( p->runOnly
21df1 4f 6e 63 65 20 29 20 70 2d 3e 65 78 70 69 72 65 Once ) p->expire
21df2 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 d = 1;. }else i
21df3 66 28 20 70 2d 3e 72 63 20 26 26 20 70 2d 3e 65 f( p->rc && p->e
21df4 78 70 69 72 65 64 20 29 7b 0a 20 20 20 20 2f 2a xpired ){. /*
21df5 20 54 68 65 20 65 78 70 69 72 65 64 20 66 6c 61 The expired fla
21df6 67 20 77 61 73 20 73 65 74 20 6f 6e 20 74 68 65 g was set on the
21df7 20 56 44 42 45 20 62 65 66 6f 72 65 20 74 68 65 VDBE before the
21df8 20 66 69 72 73 74 20 63 61 6c 6c 0a 20 20 20 20 first call.
21df9 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 74 ** to sqlite3_st
21dfa 65 70 28 29 2e 20 46 6f 72 20 63 6f 6e 73 69 73 ep(). For consis
21dfb 74 65 6e 63 79 20 28 73 69 6e 63 65 20 73 71 6c tency (since sql
21dfc 69 74 65 33 5f 73 74 65 70 28 29 20 77 61 73 0a ite3_step() was.
21dfd 20 20 20 20 2a 2a 20 63 61 6c 6c 65 64 29 2c 20 ** called),
21dfe 73 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 set the database
21dff 20 65 72 72 6f 72 20 69 6e 20 74 68 69 73 20 63 error in this c
21e00 61 73 65 20 61 73 20 77 65 6c 6c 2e 0a 20 20 20 ase as well..
21e01 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 */. sqlite3E
21e02 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 2c 20 rror(db, p->rc,
21e03 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 0);. sqlite3V
21e04 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 alueSetStr(db->p
21e05 45 72 72 2c 20 2d 31 2c 20 70 2d 3e 7a 45 72 72 Err, -1, p->zErr
21e06 4d 73 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 Msg, SQLITE_UTF8
21e07 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 , SQLITE_TRANSIE
21e08 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 NT);. sqlite3
21e09 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
21e0a 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a rrMsg);. p->z
21e0b 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a ErrMsg = 0;. }.
21e0c 0a 20 20 2f 2a 20 52 65 63 6c 61 69 6d 20 61 6c . /* Reclaim al
21e0d 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 l memory used by
21e0e 20 74 68 65 20 56 44 42 45 0a 20 20 2a 2f 0a 20 the VDBE. */.
21e0f 20 43 6c 65 61 6e 75 70 28 70 29 3b 0a 0a 20 20 Cleanup(p);..
21e10 2f 2a 20 53 61 76 65 20 70 72 6f 66 69 6c 69 6e /* Save profilin
21e11 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 g information fr
21e12 6f 6d 20 74 68 69 73 20 56 44 42 45 20 72 75 6e om this VDBE run
21e13 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 56 44 .. */.#ifdef VD
21e14 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 BE_PROFILE. {.
21e15 20 20 20 46 49 4c 45 20 2a 6f 75 74 20 3d 20 66 FILE *out = f
21e16 6f 70 65 6e 28 22 76 64 62 65 5f 70 72 6f 66 69 open("vdbe_profi
21e17 6c 65 2e 6f 75 74 22 2c 20 22 61 22 29 3b 0a 20 le.out", "a");.
21e18 20 20 20 69 66 28 20 6f 75 74 20 29 7b 0a 20 20 if( out ){.
21e19 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 int i;.
21e1a 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2d fprintf(out, "-
21e1b 2d 2d 2d 20 22 29 3b 0a 20 20 20 20 20 20 66 6f --- ");. fo
21e1c 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b r(i=0; i<p->nOp;
21e1d 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 i++){. f
21e1e 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 30 32 printf(out, "%02
21e1f 78 22 2c 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 x", p->aOp[i].op
21e20 63 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 code);. }.
21e21 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
21e22 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 20 20 66 , "\n");. f
21e23 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 or(i=0; i<p->nOp
21e24 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; i++){.
21e25 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 36 fprintf(out, "%6
21e26 64 20 25 31 30 6c 6c 64 20 25 38 6c 6c 64 20 22 d %10lld %8lld "
21e27 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e ,. p->
21e28 61 4f 70 5b 69 5d 2e 63 6e 74 2c 0a 20 20 20 20 aOp[i].cnt,.
21e29 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d p->aOp[i]
21e2a 2e 63 79 63 6c 65 73 2c 0a 20 20 20 20 20 20 20 .cycles,.
21e2b 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e p->aOp[i].cn
21e2c 74 3e 30 20 3f 20 70 2d 3e 61 4f 70 5b 69 5d 2e t>0 ? p->aOp[i].
21e2d 63 79 63 6c 65 73 2f 70 2d 3e 61 4f 70 5b 69 5d cycles/p->aOp[i]
21e2e 2e 63 6e 74 20 3a 20 30 0a 20 20 20 20 20 20 20 .cnt : 0.
21e2f 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
21e30 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 6f te3VdbePrintOp(o
21e31 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 ut, i, &p->aOp[i
21e32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ]);. }.
21e33 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 fclose(out);.
21e34 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
21e35 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 p->magic = VDB
21e36 45 5f 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20 E_MAGIC_INIT;.
21e37 72 65 74 75 72 6e 20 70 2d 3e 72 63 20 26 20 64 return p->rc & d
21e38 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d 0a 20 0a b->errMask;.}. .
21e39 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61 /*.** Clean up a
21e3a 6e 64 20 64 65 6c 65 74 65 20 61 20 56 44 42 45 nd delete a VDBE
21e3b 20 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e after execution
21e3c 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 . Return an int
21e3d 65 67 65 72 20 77 68 69 63 68 20 69 73 0a 2a 2a eger which is.**
21e3e 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 the result code
21e3f 2e 20 20 57 72 69 74 65 20 61 6e 79 20 65 72 72 . Write any err
21e40 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 or message text
21e41 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 0a into *pzErrMsg..
21e42 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21e43 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
21e44 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 20 2a eFinalize(Vdbe *
21e45 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 p){. int rc = S
21e46 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
21e47 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
21e48 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 6d AGIC_RUN || p->m
21e49 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
21e4a 5f 48 41 4c 54 20 29 7b 0a 20 20 20 20 72 63 20 _HALT ){. rc
21e4b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 = sqlite3VdbeRes
21e4c 65 74 28 70 29 3b 0a 20 20 20 20 61 73 73 65 72 et(p);. asser
21e4d 74 28 20 28 72 63 20 26 20 70 2d 3e 64 62 2d 3e t( (rc & p->db->
21e4e 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a errMask)==rc );.
21e4f 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
21e50 65 44 65 6c 65 74 65 28 70 29 3b 0a 20 20 72 65 eDelete(p);. re
21e51 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
21e52 2a 20 43 61 6c 6c 20 74 68 65 20 64 65 73 74 72 * Call the destr
21e53 75 63 74 6f 72 20 66 6f 72 20 65 61 63 68 20 61 uctor for each a
21e54 75 78 64 61 74 61 20 65 6e 74 72 79 20 69 6e 20 uxdata entry in
21e55 70 56 64 62 65 46 75 6e 63 20 66 6f 72 20 77 68 pVdbeFunc for wh
21e56 69 63 68 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 ich.** the corre
21e57 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 6e 20 sponding bit in
21e58 6d 61 73 6b 20 69 73 20 63 6c 65 61 72 2e 20 20 mask is clear.
21e59 41 75 78 64 61 74 61 20 65 6e 74 72 69 65 73 20 Auxdata entries
21e5a 62 65 79 6f 6e 64 20 33 31 0a 2a 2a 20 61 72 65 beyond 31.** are
21e5b 20 61 6c 77 61 79 73 20 64 65 73 74 72 6f 79 65 always destroye
21e5c 64 2e 20 20 54 6f 20 64 65 73 74 72 6f 79 20 61 d. To destroy a
21e5d 6c 6c 20 61 75 78 64 61 74 61 20 65 6e 74 72 69 ll auxdata entri
21e5e 65 73 2c 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a es, call this.**
21e5f 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 6d 61 routine with ma
21e60 73 6b 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 sk==0..*/.SQLITE
21e61 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
21e62 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 lite3VdbeDeleteA
21e63 75 78 44 61 74 61 28 56 64 62 65 46 75 6e 63 20 uxData(VdbeFunc
21e64 2a 70 56 64 62 65 46 75 6e 63 2c 20 69 6e 74 20 *pVdbeFunc, int
21e65 6d 61 73 6b 29 7b 0a 20 20 69 6e 74 20 69 3b 0a mask){. int i;.
21e66 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 56 64 for(i=0; i<pVd
21e67 62 65 46 75 6e 63 2d 3e 6e 41 75 78 3b 20 69 2b beFunc->nAux; i+
21e68 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 41 +){. struct A
21e69 75 78 44 61 74 61 20 2a 70 41 75 78 20 3d 20 26 uxData *pAux = &
21e6a 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 pVdbeFunc->apAux
21e6b 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 28 69 3e [i];. if( (i>
21e6c 33 31 20 7c 7c 20 21 28 6d 61 73 6b 26 28 28 28 31 || !(mask&(((
21e6d 75 33 32 29 31 29 3c 3c 69 29 29 29 20 26 26 20 u32)1)<<i))) &&
21e6e 70 41 75 78 2d 3e 70 41 75 78 20 29 7b 0a 20 20 pAux->pAux ){.
21e6f 20 20 20 20 69 66 28 20 70 41 75 78 2d 3e 78 44 if( pAux->xD
21e70 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20 20 elete ){.
21e71 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 28 70 pAux->xDelete(p
21e72 41 75 78 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 Aux->pAux);.
21e73 20 20 7d 0a 20 20 20 20 20 20 70 41 75 78 2d 3e }. pAux->
21e74 70 41 75 78 20 3d 20 30 3b 0a 20 20 20 20 7d 0a pAux = 0;. }.
21e75 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 }.}../*.** Fre
21e76 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 73 73 e all memory ass
21e77 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
21e78 20 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20 Vdbe passed as
21e79 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
21e7a 65 6e 74 2c 0a 2a 2a 20 65 78 63 65 70 74 20 66 ent,.** except f
21e7b 6f 72 20 6f 62 6a 65 63 74 20 69 74 73 65 6c 66 or object itself
21e7c 2c 20 77 68 69 63 68 20 69 73 20 70 72 65 73 65 , which is prese
21e7d 72 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rved..**.** The
21e7e 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 difference betwe
21e7f 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
21e80 20 61 6e 64 20 73 71 6c 69 74 65 33 56 64 62 65 and sqlite3Vdbe
21e81 44 65 6c 65 74 65 28 29 20 69 73 20 74 68 61 74 Delete()